mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-09-22 06:00:56 +02:00
🤖 refactor: Auto-validate IDs in Agent Query (#9555)
* 🤖 refactor: Auto-validate IDs in Agent Query
* chore: remove comments in useAgentToolPermissions
This commit is contained in:
parent
f3eca8c7a7
commit
f125f5bd32
6 changed files with 21 additions and 33 deletions
|
@ -47,11 +47,7 @@ export default function AgentPanel() {
|
||||||
const { onSelect: onSelectAgent } = useSelectAgent();
|
const { onSelect: onSelectAgent } = useSelectAgent();
|
||||||
|
|
||||||
const modelsQuery = useGetModelsQuery();
|
const modelsQuery = useGetModelsQuery();
|
||||||
|
const basicAgentQuery = useGetAgentByIdQuery(current_agent_id);
|
||||||
// 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 { hasPermission, isLoading: permissionsLoading } = useResourcePermissions(
|
const { hasPermission, isLoading: permissionsLoading } = useResourcePermissions(
|
||||||
ResourceType.AGENT,
|
ResourceType.AGENT,
|
||||||
|
|
|
@ -16,14 +16,7 @@ export default function VersionPanel() {
|
||||||
|
|
||||||
const selectedAgentId = agent_id ?? '';
|
const selectedAgentId = agent_id ?? '';
|
||||||
|
|
||||||
const {
|
const { data: agent, isLoading, error, refetch } = useGetAgentByIdQuery(selectedAgentId);
|
||||||
data: agent,
|
|
||||||
isLoading,
|
|
||||||
error,
|
|
||||||
refetch,
|
|
||||||
} = useGetAgentByIdQuery(selectedAgentId, {
|
|
||||||
enabled: !!selectedAgentId && selectedAgentId !== '',
|
|
||||||
});
|
|
||||||
|
|
||||||
const revertAgentVersion = useRevertAgentVersionMutation({
|
const revertAgentVersion = useRevertAgentVersionMutation({
|
||||||
onSuccess: () => {
|
onSuccess: () => {
|
||||||
|
|
|
@ -1,5 +1,11 @@
|
||||||
import { useQuery, useInfiniteQuery, useQueryClient } from '@tanstack/react-query';
|
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 {
|
import type {
|
||||||
QueryObserverResult,
|
QueryObserverResult,
|
||||||
UseQueryOptions,
|
UseQueryOptions,
|
||||||
|
@ -64,20 +70,27 @@ export const useListAgentsQuery = <TData = t.AgentListResponse>(
|
||||||
* Hook for retrieving basic details about a single agent (VIEW permission)
|
* Hook for retrieving basic details about a single agent (VIEW permission)
|
||||||
*/
|
*/
|
||||||
export const useGetAgentByIdQuery = (
|
export const useGetAgentByIdQuery = (
|
||||||
agent_id: string,
|
agent_id: string | null | undefined,
|
||||||
config?: UseQueryOptions<t.Agent>,
|
config?: UseQueryOptions<t.Agent>,
|
||||||
): QueryObserverResult<t.Agent> => {
|
): QueryObserverResult<t.Agent> => {
|
||||||
|
const isValidAgentId = !!(
|
||||||
|
agent_id &&
|
||||||
|
agent_id !== '' &&
|
||||||
|
agent_id !== Constants.EPHEMERAL_AGENT_ID
|
||||||
|
);
|
||||||
|
|
||||||
return useQuery<t.Agent>(
|
return useQuery<t.Agent>(
|
||||||
[QueryKeys.agent, agent_id],
|
[QueryKeys.agent, agent_id],
|
||||||
() =>
|
() =>
|
||||||
dataService.getAgentById({
|
dataService.getAgentById({
|
||||||
agent_id,
|
agent_id: agent_id as string,
|
||||||
}),
|
}),
|
||||||
{
|
{
|
||||||
refetchOnWindowFocus: false,
|
refetchOnWindowFocus: false,
|
||||||
refetchOnReconnect: false,
|
refetchOnReconnect: false,
|
||||||
refetchOnMount: false,
|
refetchOnMount: false,
|
||||||
retry: false,
|
retry: false,
|
||||||
|
enabled: isValidAgentId && (config?.enabled ?? true),
|
||||||
...config,
|
...config,
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
|
@ -20,24 +20,18 @@ export default function useAgentToolPermissions(
|
||||||
): AgentToolPermissionsResult {
|
): AgentToolPermissionsResult {
|
||||||
const agentsMap = useAgentsMapContext();
|
const agentsMap = useAgentsMapContext();
|
||||||
|
|
||||||
// Get the agent from the map if available
|
|
||||||
const selectedAgent = useMemo(() => {
|
const selectedAgent = useMemo(() => {
|
||||||
return agentId != null && agentId !== '' ? agentsMap?.[agentId] : undefined;
|
return agentId != null && agentId !== '' ? agentsMap?.[agentId] : undefined;
|
||||||
}, [agentId, agentsMap]);
|
}, [agentId, agentsMap]);
|
||||||
|
|
||||||
// Query for agent data from the API
|
const { data: agentData } = useGetAgentByIdQuery(agentId);
|
||||||
const { data: agentData } = useGetAgentByIdQuery(agentId ?? '', {
|
|
||||||
enabled: !!agentId,
|
|
||||||
});
|
|
||||||
|
|
||||||
// Get tools from either the API data or the agents map
|
|
||||||
const tools = useMemo(
|
const tools = useMemo(
|
||||||
() =>
|
() =>
|
||||||
(agentData?.tools as string[] | undefined) || (selectedAgent?.tools as string[] | undefined),
|
(agentData?.tools as string[] | undefined) || (selectedAgent?.tools as string[] | undefined),
|
||||||
[agentData?.tools, selectedAgent?.tools],
|
[agentData?.tools, selectedAgent?.tools],
|
||||||
);
|
);
|
||||||
|
|
||||||
// Determine if file_search is allowed
|
|
||||||
const fileSearchAllowedByAgent = useMemo(() => {
|
const fileSearchAllowedByAgent = useMemo(() => {
|
||||||
// If no agentId, allow for ephemeral agents
|
// If no agentId, allow for ephemeral agents
|
||||||
if (!agentId) return true;
|
if (!agentId) return true;
|
||||||
|
@ -47,7 +41,6 @@ export default function useAgentToolPermissions(
|
||||||
return tools?.includes(Tools.file_search) ?? false;
|
return tools?.includes(Tools.file_search) ?? false;
|
||||||
}, [agentId, selectedAgent, tools]);
|
}, [agentId, selectedAgent, tools]);
|
||||||
|
|
||||||
// Determine if execute_code is allowed
|
|
||||||
const codeAllowedByAgent = useMemo(() => {
|
const codeAllowedByAgent = useMemo(() => {
|
||||||
// If no agentId, allow for ephemeral agents
|
// If no agentId, allow for ephemeral agents
|
||||||
if (!agentId) return true;
|
if (!agentId) return true;
|
||||||
|
|
|
@ -22,9 +22,7 @@ export default function useSelectAgent() {
|
||||||
conversation?.agent_id ?? null,
|
conversation?.agent_id ?? null,
|
||||||
);
|
);
|
||||||
|
|
||||||
const agentQuery = useGetAgentByIdQuery(selectedAgentId ?? '', {
|
const agentQuery = useGetAgentByIdQuery(selectedAgentId);
|
||||||
enabled: !!(selectedAgentId ?? '') && selectedAgentId !== Constants.EPHEMERAL_AGENT_ID,
|
|
||||||
});
|
|
||||||
|
|
||||||
const updateConversation = useCallback(
|
const updateConversation = useCallback(
|
||||||
(agent: Partial<Agent>, template: Partial<TPreset | TConversation>) => {
|
(agent: Partial<Agent>, template: Partial<TPreset | TConversation>) => {
|
||||||
|
|
|
@ -125,13 +125,8 @@ export default function useQueryParams({
|
||||||
const queryClient = useQueryClient();
|
const queryClient = useQueryClient();
|
||||||
const { conversation, newConversation } = useChatContext();
|
const { conversation, newConversation } = useChatContext();
|
||||||
|
|
||||||
// Extract agent_id from URL for proactive fetching
|
|
||||||
const urlAgentId = searchParams.get('agent_id') || '';
|
const urlAgentId = searchParams.get('agent_id') || '';
|
||||||
|
const { data: urlAgent } = useGetAgentByIdQuery(urlAgentId);
|
||||||
// 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
|
|
||||||
});
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Applies settings from URL query parameters to create a new conversation.
|
* Applies settings from URL query parameters to create a new conversation.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue