mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-12-21 10:50:14 +01:00
* refactor: agent tool permissions to support ephemeral agent settings * ci: rename render tests and correct typing for `useAgentToolPermissions` hook * refactor: implement `DragDropContext` to minimize effect of `useChatContext` in `DragDropModal`
69 lines
2.5 KiB
TypeScript
69 lines
2.5 KiB
TypeScript
import { useMemo } from 'react';
|
|
import { Tools, Constants, EToolResources } from 'librechat-data-provider';
|
|
import type { TEphemeralAgent } from 'librechat-data-provider';
|
|
import { useGetAgentByIdQuery } from '~/data-provider';
|
|
import { useAgentsMapContext } from '~/Providers';
|
|
|
|
interface AgentToolPermissionsResult {
|
|
fileSearchAllowedByAgent: boolean;
|
|
codeAllowedByAgent: boolean;
|
|
tools: string[] | undefined;
|
|
}
|
|
|
|
function isEphemeralAgent(agentId: string | null | undefined): boolean {
|
|
return agentId == null || agentId === '' || agentId === Constants.EPHEMERAL_AGENT_ID;
|
|
}
|
|
|
|
/**
|
|
* Hook to determine whether specific tools are allowed for a given agent.
|
|
*
|
|
* @param agentId - The ID of the agent. If null/undefined/empty, checks ephemeralAgent settings
|
|
* @param ephemeralAgent - Optional ephemeral agent settings for tool permissions
|
|
* @returns Object with boolean flags for file_search and execute_code permissions, plus the tools array
|
|
*/
|
|
export default function useAgentToolPermissions(
|
|
agentId: string | null | undefined,
|
|
ephemeralAgent?: TEphemeralAgent | null,
|
|
): AgentToolPermissionsResult {
|
|
const agentsMap = useAgentsMapContext();
|
|
|
|
const selectedAgent = useMemo(() => {
|
|
return agentId != null && agentId !== '' ? agentsMap?.[agentId] : undefined;
|
|
}, [agentId, agentsMap]);
|
|
|
|
const { data: agentData } = useGetAgentByIdQuery(agentId);
|
|
|
|
const tools = useMemo(
|
|
() =>
|
|
(agentData?.tools as string[] | undefined) || (selectedAgent?.tools as string[] | undefined),
|
|
[agentData?.tools, selectedAgent?.tools],
|
|
);
|
|
|
|
const fileSearchAllowedByAgent = useMemo(() => {
|
|
// Check ephemeral agent settings
|
|
if (isEphemeralAgent(agentId)) {
|
|
return ephemeralAgent?.[EToolResources.file_search] ?? false;
|
|
}
|
|
// If agentId exists but agent not found, disallow
|
|
if (!selectedAgent) return false;
|
|
// Check if the agent has the file_search tool
|
|
return tools?.includes(Tools.file_search) ?? false;
|
|
}, [agentId, selectedAgent, tools, ephemeralAgent]);
|
|
|
|
const codeAllowedByAgent = useMemo(() => {
|
|
// Check ephemeral agent settings
|
|
if (isEphemeralAgent(agentId)) {
|
|
return ephemeralAgent?.[EToolResources.execute_code] ?? false;
|
|
}
|
|
// If agentId exists but agent not found, disallow
|
|
if (!selectedAgent) return false;
|
|
// Check if the agent has the execute_code tool
|
|
return tools?.includes(Tools.execute_code) ?? false;
|
|
}, [agentId, selectedAgent, tools, ephemeralAgent]);
|
|
|
|
return {
|
|
fileSearchAllowedByAgent,
|
|
codeAllowedByAgent,
|
|
tools,
|
|
};
|
|
}
|