From f125f5bd32e1b86086aaad7db38a87d2d43583dd Mon Sep 17 00:00:00 2001 From: Danny Avila Date: Wed, 10 Sep 2025 18:38:33 -0400 Subject: [PATCH] =?UTF-8?q?=F0=9F=A4=96=20refactor:=20Auto-validate=20IDs?= =?UTF-8?q?=20in=20Agent=20Query=20(#9555)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 🤖 refactor: Auto-validate IDs in Agent Query * chore: remove comments in useAgentToolPermissions --- .../SidePanel/Agents/AgentPanel.tsx | 6 +----- .../SidePanel/Agents/Version/VersionPanel.tsx | 9 +-------- client/src/data-provider/Agents/queries.ts | 19 ++++++++++++++++--- .../hooks/Agents/useAgentToolPermissions.ts | 9 +-------- client/src/hooks/Agents/useSelectAgent.ts | 4 +--- client/src/hooks/Input/useQueryParams.ts | 7 +------ 6 files changed, 21 insertions(+), 33 deletions(-) diff --git a/client/src/components/SidePanel/Agents/AgentPanel.tsx b/client/src/components/SidePanel/Agents/AgentPanel.tsx index 082d91bd45..4ea6ef7168 100644 --- a/client/src/components/SidePanel/Agents/AgentPanel.tsx +++ b/client/src/components/SidePanel/Agents/AgentPanel.tsx @@ -47,11 +47,7 @@ export default function AgentPanel() { const { onSelect: onSelectAgent } = useSelectAgent(); const modelsQuery = useGetModelsQuery(); - - // Basic agent query for initial permission check - const basicAgentQuery = useGetAgentByIdQuery(current_agent_id ?? '', { - enabled: !!(current_agent_id ?? '') && current_agent_id !== Constants.EPHEMERAL_AGENT_ID, - }); + const basicAgentQuery = useGetAgentByIdQuery(current_agent_id); const { hasPermission, isLoading: permissionsLoading } = useResourcePermissions( ResourceType.AGENT, diff --git a/client/src/components/SidePanel/Agents/Version/VersionPanel.tsx b/client/src/components/SidePanel/Agents/Version/VersionPanel.tsx index 871e92bce2..f9829f1a7d 100644 --- a/client/src/components/SidePanel/Agents/Version/VersionPanel.tsx +++ b/client/src/components/SidePanel/Agents/Version/VersionPanel.tsx @@ -16,14 +16,7 @@ export default function VersionPanel() { const selectedAgentId = agent_id ?? ''; - const { - data: agent, - isLoading, - error, - refetch, - } = useGetAgentByIdQuery(selectedAgentId, { - enabled: !!selectedAgentId && selectedAgentId !== '', - }); + const { data: agent, isLoading, error, refetch } = useGetAgentByIdQuery(selectedAgentId); const revertAgentVersion = useRevertAgentVersionMutation({ onSuccess: () => { diff --git a/client/src/data-provider/Agents/queries.ts b/client/src/data-provider/Agents/queries.ts index 416dec6e47..300a06f5d3 100644 --- a/client/src/data-provider/Agents/queries.ts +++ b/client/src/data-provider/Agents/queries.ts @@ -1,5 +1,11 @@ import { useQuery, useInfiniteQuery, useQueryClient } from '@tanstack/react-query'; -import { QueryKeys, dataService, EModelEndpoint, PermissionBits } from 'librechat-data-provider'; +import { + Constants, + QueryKeys, + dataService, + EModelEndpoint, + PermissionBits, +} from 'librechat-data-provider'; import type { QueryObserverResult, UseQueryOptions, @@ -64,20 +70,27 @@ export const useListAgentsQuery = ( * Hook for retrieving basic details about a single agent (VIEW permission) */ export const useGetAgentByIdQuery = ( - agent_id: string, + agent_id: string | null | undefined, config?: UseQueryOptions, ): QueryObserverResult => { + const isValidAgentId = !!( + agent_id && + agent_id !== '' && + agent_id !== Constants.EPHEMERAL_AGENT_ID + ); + return useQuery( [QueryKeys.agent, agent_id], () => dataService.getAgentById({ - agent_id, + agent_id: agent_id as string, }), { refetchOnWindowFocus: false, refetchOnReconnect: false, refetchOnMount: false, retry: false, + enabled: isValidAgentId && (config?.enabled ?? true), ...config, }, ); diff --git a/client/src/hooks/Agents/useAgentToolPermissions.ts b/client/src/hooks/Agents/useAgentToolPermissions.ts index 747e4f5d0d..da27492f3a 100644 --- a/client/src/hooks/Agents/useAgentToolPermissions.ts +++ b/client/src/hooks/Agents/useAgentToolPermissions.ts @@ -20,24 +20,18 @@ export default function useAgentToolPermissions( ): AgentToolPermissionsResult { const agentsMap = useAgentsMapContext(); - // Get the agent from the map if available const selectedAgent = useMemo(() => { return agentId != null && agentId !== '' ? agentsMap?.[agentId] : undefined; }, [agentId, agentsMap]); - // Query for agent data from the API - const { data: agentData } = useGetAgentByIdQuery(agentId ?? '', { - enabled: !!agentId, - }); + const { data: agentData } = useGetAgentByIdQuery(agentId); - // Get tools from either the API data or the agents map const tools = useMemo( () => (agentData?.tools as string[] | undefined) || (selectedAgent?.tools as string[] | undefined), [agentData?.tools, selectedAgent?.tools], ); - // Determine if file_search is allowed const fileSearchAllowedByAgent = useMemo(() => { // If no agentId, allow for ephemeral agents if (!agentId) return true; @@ -47,7 +41,6 @@ export default function useAgentToolPermissions( return tools?.includes(Tools.file_search) ?? false; }, [agentId, selectedAgent, tools]); - // Determine if execute_code is allowed const codeAllowedByAgent = useMemo(() => { // If no agentId, allow for ephemeral agents if (!agentId) return true; diff --git a/client/src/hooks/Agents/useSelectAgent.ts b/client/src/hooks/Agents/useSelectAgent.ts index 585554fc39..00c2753d93 100644 --- a/client/src/hooks/Agents/useSelectAgent.ts +++ b/client/src/hooks/Agents/useSelectAgent.ts @@ -22,9 +22,7 @@ export default function useSelectAgent() { conversation?.agent_id ?? null, ); - const agentQuery = useGetAgentByIdQuery(selectedAgentId ?? '', { - enabled: !!(selectedAgentId ?? '') && selectedAgentId !== Constants.EPHEMERAL_AGENT_ID, - }); + const agentQuery = useGetAgentByIdQuery(selectedAgentId); const updateConversation = useCallback( (agent: Partial, template: Partial) => { diff --git a/client/src/hooks/Input/useQueryParams.ts b/client/src/hooks/Input/useQueryParams.ts index b676913db9..d2d1f66a4d 100644 --- a/client/src/hooks/Input/useQueryParams.ts +++ b/client/src/hooks/Input/useQueryParams.ts @@ -125,13 +125,8 @@ export default function useQueryParams({ const queryClient = useQueryClient(); const { conversation, newConversation } = useChatContext(); - // Extract agent_id from URL for proactive fetching const urlAgentId = searchParams.get('agent_id') || ''; - - // Use the existing query hook to fetch agent if present in URL - const { data: urlAgent } = useGetAgentByIdQuery(urlAgentId, { - enabled: !!urlAgentId, // Only fetch if agent_id exists in URL - }); + const { data: urlAgent } = useGetAgentByIdQuery(urlAgentId); /** * Applies settings from URL query parameters to create a new conversation.