2025-06-13 15:47:41 -04:00
|
|
|
import React, { createContext, useContext, useState } from 'react';
|
2025-06-19 07:01:50 -07:00
|
|
|
import { Constants, EModelEndpoint } from 'librechat-data-provider';
|
|
|
|
|
import type { TPlugin, AgentToolType, Action, MCP } from 'librechat-data-provider';
|
2025-06-13 15:47:41 -04:00
|
|
|
import type { AgentPanelContextType } from '~/common';
|
2025-06-19 07:01:50 -07:00
|
|
|
import { useAvailableToolsQuery, useGetActionsQuery } from '~/data-provider';
|
|
|
|
|
import { useLocalize } from '~/hooks';
|
2025-06-13 15:47:41 -04:00
|
|
|
import { Panel } from '~/common';
|
|
|
|
|
|
|
|
|
|
const AgentPanelContext = createContext<AgentPanelContextType | undefined>(undefined);
|
|
|
|
|
|
|
|
|
|
export function useAgentPanelContext() {
|
|
|
|
|
const context = useContext(AgentPanelContext);
|
|
|
|
|
if (context === undefined) {
|
|
|
|
|
throw new Error('useAgentPanelContext must be used within an AgentPanelProvider');
|
|
|
|
|
}
|
|
|
|
|
return context;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Houses relevant state for the Agent Form Panels (formerly 'commonProps') */
|
|
|
|
|
export function AgentPanelProvider({ children }: { children: React.ReactNode }) {
|
2025-06-19 07:01:50 -07:00
|
|
|
const localize = useLocalize();
|
2025-06-13 15:47:41 -04:00
|
|
|
const [mcp, setMcp] = useState<MCP | undefined>(undefined);
|
|
|
|
|
const [mcps, setMcps] = useState<MCP[] | undefined>(undefined);
|
|
|
|
|
const [action, setAction] = useState<Action | undefined>(undefined);
|
|
|
|
|
const [activePanel, setActivePanel] = useState<Panel>(Panel.builder);
|
|
|
|
|
const [agent_id, setCurrentAgentId] = useState<string | undefined>(undefined);
|
|
|
|
|
|
|
|
|
|
const { data: actions } = useGetActionsQuery(EModelEndpoint.agents, {
|
|
|
|
|
enabled: !!agent_id,
|
|
|
|
|
});
|
|
|
|
|
|
2025-06-19 07:01:50 -07:00
|
|
|
const { data: pluginTools } = useAvailableToolsQuery(EModelEndpoint.agents, {
|
|
|
|
|
enabled: !!agent_id,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const tools =
|
|
|
|
|
pluginTools?.map((tool) => ({
|
|
|
|
|
tool_id: tool.pluginKey,
|
|
|
|
|
metadata: tool as TPlugin,
|
|
|
|
|
agent_id: agent_id || '',
|
|
|
|
|
})) || [];
|
|
|
|
|
|
2025-06-26 18:50:15 -04:00
|
|
|
const groupedTools = tools?.reduce(
|
|
|
|
|
(acc, tool) => {
|
|
|
|
|
if (tool.tool_id.includes(Constants.mcp_delimiter)) {
|
|
|
|
|
const [_toolName, serverName] = tool.tool_id.split(Constants.mcp_delimiter);
|
|
|
|
|
const groupKey = `${serverName.toLowerCase()}`;
|
|
|
|
|
if (!acc[groupKey]) {
|
|
|
|
|
acc[groupKey] = {
|
|
|
|
|
tool_id: groupKey,
|
|
|
|
|
metadata: {
|
|
|
|
|
name: `${serverName}`,
|
|
|
|
|
pluginKey: groupKey,
|
|
|
|
|
description: `${localize('com_ui_tool_collection_prefix')} ${serverName}`,
|
|
|
|
|
icon: tool.metadata.icon || '',
|
|
|
|
|
} as TPlugin,
|
2025-06-19 07:01:50 -07:00
|
|
|
agent_id: agent_id || '',
|
2025-06-26 18:50:15 -04:00
|
|
|
tools: [],
|
2025-06-19 07:01:50 -07:00
|
|
|
};
|
|
|
|
|
}
|
2025-06-26 18:50:15 -04:00
|
|
|
acc[groupKey].tools?.push({
|
|
|
|
|
tool_id: tool.tool_id,
|
|
|
|
|
metadata: tool.metadata,
|
|
|
|
|
agent_id: agent_id || '',
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
acc[tool.tool_id] = {
|
|
|
|
|
tool_id: tool.tool_id,
|
|
|
|
|
metadata: tool.metadata,
|
|
|
|
|
agent_id: agent_id || '',
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
return acc;
|
|
|
|
|
},
|
|
|
|
|
{} as Record<string, AgentToolType & { tools?: AgentToolType[] }>,
|
|
|
|
|
);
|
2025-06-19 07:01:50 -07:00
|
|
|
|
2025-06-13 15:47:41 -04:00
|
|
|
const value = {
|
|
|
|
|
action,
|
|
|
|
|
setAction,
|
|
|
|
|
mcp,
|
|
|
|
|
setMcp,
|
|
|
|
|
mcps,
|
|
|
|
|
setMcps,
|
|
|
|
|
activePanel,
|
|
|
|
|
setActivePanel,
|
|
|
|
|
setCurrentAgentId,
|
|
|
|
|
agent_id,
|
2025-06-19 07:01:50 -07:00
|
|
|
groupedTools,
|
|
|
|
|
/** Query data for actions and tools */
|
2025-06-13 15:47:41 -04:00
|
|
|
actions,
|
2025-06-19 07:01:50 -07:00
|
|
|
tools,
|
2025-06-13 15:47:41 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return <AgentPanelContext.Provider value={value}>{children}</AgentPanelContext.Provider>;
|
|
|
|
|
}
|