2025-09-09 20:48:29 -04:00
|
|
|
import { useMemo } from 'react';
|
2025-09-22 09:48:05 -04:00
|
|
|
import { Tools, EToolResources } from 'librechat-data-provider';
|
2025-09-18 14:44:55 -04:00
|
|
|
import type { TEphemeralAgent } from 'librechat-data-provider';
|
2025-09-09 20:48:29 -04:00
|
|
|
import { useGetAgentByIdQuery } from '~/data-provider';
|
|
|
|
|
import { useAgentsMapContext } from '~/Providers';
|
2025-09-22 09:48:05 -04:00
|
|
|
import { isEphemeralAgent } from '~/common';
|
2025-09-09 20:48:29 -04:00
|
|
|
|
|
|
|
|
interface AgentToolPermissionsResult {
|
|
|
|
|
fileSearchAllowedByAgent: boolean;
|
|
|
|
|
codeAllowedByAgent: boolean;
|
|
|
|
|
tools: string[] | undefined;
|
2025-10-06 17:30:16 -04:00
|
|
|
provider?: string;
|
2025-09-09 20:48:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Hook to determine whether specific tools are allowed for a given agent.
|
|
|
|
|
*
|
2025-09-18 14:44:55 -04:00
|
|
|
* @param agentId - The ID of the agent. If null/undefined/empty, checks ephemeralAgent settings
|
|
|
|
|
* @param ephemeralAgent - Optional ephemeral agent settings for tool permissions
|
2025-09-09 20:48:29 -04:00
|
|
|
* @returns Object with boolean flags for file_search and execute_code permissions, plus the tools array
|
|
|
|
|
*/
|
|
|
|
|
export default function useAgentToolPermissions(
|
|
|
|
|
agentId: string | null | undefined,
|
2025-09-18 14:44:55 -04:00
|
|
|
ephemeralAgent?: TEphemeralAgent | null,
|
2025-09-09 20:48:29 -04:00
|
|
|
): AgentToolPermissionsResult {
|
|
|
|
|
const agentsMap = useAgentsMapContext();
|
|
|
|
|
|
|
|
|
|
const selectedAgent = useMemo(() => {
|
|
|
|
|
return agentId != null && agentId !== '' ? agentsMap?.[agentId] : undefined;
|
|
|
|
|
}, [agentId, agentsMap]);
|
|
|
|
|
|
2025-09-10 18:38:33 -04:00
|
|
|
const { data: agentData } = useGetAgentByIdQuery(agentId);
|
2025-09-09 20:48:29 -04:00
|
|
|
|
|
|
|
|
const tools = useMemo(
|
|
|
|
|
() =>
|
|
|
|
|
(agentData?.tools as string[] | undefined) || (selectedAgent?.tools as string[] | undefined),
|
|
|
|
|
[agentData?.tools, selectedAgent?.tools],
|
|
|
|
|
);
|
|
|
|
|
|
2025-10-09 00:31:04 -07:00
|
|
|
const provider = useMemo(
|
|
|
|
|
() => agentData?.provider || selectedAgent?.provider,
|
|
|
|
|
[agentData?.provider, selectedAgent?.provider],
|
|
|
|
|
);
|
2025-10-06 17:30:16 -04:00
|
|
|
|
2025-09-09 20:48:29 -04:00
|
|
|
const fileSearchAllowedByAgent = useMemo(() => {
|
2025-09-18 14:44:55 -04:00
|
|
|
// Check ephemeral agent settings
|
|
|
|
|
if (isEphemeralAgent(agentId)) {
|
|
|
|
|
return ephemeralAgent?.[EToolResources.file_search] ?? false;
|
|
|
|
|
}
|
2025-09-09 20:48:29 -04:00
|
|
|
// 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;
|
2025-09-18 14:44:55 -04:00
|
|
|
}, [agentId, selectedAgent, tools, ephemeralAgent]);
|
2025-09-09 20:48:29 -04:00
|
|
|
|
|
|
|
|
const codeAllowedByAgent = useMemo(() => {
|
2025-09-18 14:44:55 -04:00
|
|
|
// Check ephemeral agent settings
|
|
|
|
|
if (isEphemeralAgent(agentId)) {
|
|
|
|
|
return ephemeralAgent?.[EToolResources.execute_code] ?? false;
|
|
|
|
|
}
|
2025-09-09 20:48:29 -04:00
|
|
|
// 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;
|
2025-09-18 14:44:55 -04:00
|
|
|
}, [agentId, selectedAgent, tools, ephemeralAgent]);
|
2025-09-09 20:48:29 -04:00
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
fileSearchAllowedByAgent,
|
|
|
|
|
codeAllowedByAgent,
|
2025-10-06 17:30:16 -04:00
|
|
|
provider,
|
2025-09-09 20:48:29 -04:00
|
|
|
tools,
|
|
|
|
|
};
|
|
|
|
|
}
|