| import { useQuery, useInfiniteQuery, useQueryClient } from '@tanstack/react-query'; |
| import { QueryKeys, dataService, EModelEndpoint, PermissionBits } from 'librechat-data-provider'; |
| import type { |
| QueryObserverResult, |
| UseQueryOptions, |
| UseInfiniteQueryOptions, |
| } from '@tanstack/react-query'; |
| import type t from 'librechat-data-provider'; |
| import { isEphemeralAgent } from '~/common'; |
|
|
| |
| |
| |
| export const defaultAgentParams: t.AgentListParams = { |
| limit: 10, |
| requiredPermission: PermissionBits.EDIT, |
| }; |
| |
| |
| |
| export const useAvailableAgentToolsQuery = (): QueryObserverResult<t.TPlugin[]> => { |
| const queryClient = useQueryClient(); |
| const endpointsConfig = queryClient.getQueryData<t.TEndpointsConfig>([QueryKeys.endpoints]); |
|
|
| const enabled = !!endpointsConfig?.[EModelEndpoint.agents]; |
| return useQuery<t.TPlugin[]>([QueryKeys.tools], () => dataService.getAvailableAgentTools(), { |
| refetchOnWindowFocus: false, |
| refetchOnReconnect: false, |
| refetchOnMount: false, |
| enabled, |
| }); |
| }; |
|
|
| |
| |
| |
| export const useListAgentsQuery = <TData = t.AgentListResponse>( |
| params: t.AgentListParams = defaultAgentParams, |
| config?: UseQueryOptions<t.AgentListResponse, unknown, TData>, |
| ): QueryObserverResult<TData> => { |
| const queryClient = useQueryClient(); |
| const endpointsConfig = queryClient.getQueryData<t.TEndpointsConfig>([QueryKeys.endpoints]); |
|
|
| const enabled = !!endpointsConfig?.[EModelEndpoint.agents]; |
| return useQuery<t.AgentListResponse, unknown, TData>( |
| [QueryKeys.agents, params], |
| () => dataService.listAgents(params), |
| { |
| |
| |
| |
| |
| staleTime: 1000 * 5, |
| refetchOnWindowFocus: false, |
| refetchOnReconnect: false, |
| refetchOnMount: false, |
| retry: false, |
| ...config, |
| enabled: config?.enabled !== undefined ? config.enabled && enabled : enabled, |
| }, |
| ); |
| }; |
|
|
| |
| |
| |
| export const useGetAgentByIdQuery = ( |
| agent_id: string | null | undefined, |
| config?: UseQueryOptions<t.Agent>, |
| ): QueryObserverResult<t.Agent> => { |
| const isValidAgentId = !!agent_id && !isEphemeralAgent(agent_id); |
|
|
| return useQuery<t.Agent>( |
| [QueryKeys.agent, agent_id], |
| () => |
| dataService.getAgentById({ |
| agent_id: agent_id as string, |
| }), |
| { |
| refetchOnWindowFocus: false, |
| refetchOnReconnect: false, |
| refetchOnMount: false, |
| retry: false, |
| enabled: isValidAgentId && (config?.enabled ?? true), |
| ...config, |
| }, |
| ); |
| }; |
|
|
| |
| |
| |
| export const useGetExpandedAgentByIdQuery = ( |
| agent_id: string, |
| config?: UseQueryOptions<t.Agent>, |
| ): QueryObserverResult<t.Agent> => { |
| return useQuery<t.Agent>( |
| [QueryKeys.agent, agent_id, 'expanded'], |
| () => |
| dataService.getExpandedAgentById({ |
| agent_id, |
| }), |
| { |
| refetchOnWindowFocus: false, |
| refetchOnReconnect: false, |
| refetchOnMount: false, |
| retry: false, |
| ...config, |
| }, |
| ); |
| }; |
|
|
| |
| |
| |
| |
| |
| |
| export const useGetAgentCategoriesQuery = ( |
| config?: UseQueryOptions<t.TMarketplaceCategory[]>, |
| ): QueryObserverResult<t.TMarketplaceCategory[]> => { |
| return useQuery<t.TMarketplaceCategory[]>( |
| [QueryKeys.agentCategories], |
| () => dataService.getAgentCategories(), |
| { |
| refetchOnWindowFocus: false, |
| refetchOnReconnect: false, |
| refetchOnMount: false, |
| staleTime: 5 * 60 * 1000, |
| ...config, |
| }, |
| ); |
| }; |
|
|
| |
| |
| |
| export const useMarketplaceAgentsInfiniteQuery = ( |
| params: { |
| requiredPermission: number; |
| category?: string; |
| search?: string; |
| limit?: number; |
| promoted?: 0 | 1; |
| cursor?: string; // For pagination |
| }, |
| config?: UseInfiniteQueryOptions<t.AgentListResponse, unknown>, |
| ) => { |
| return useInfiniteQuery<t.AgentListResponse>({ |
| queryKey: [QueryKeys.marketplaceAgents, params], |
| queryFn: ({ pageParam }) => { |
| const queryParams = { ...params }; |
| if (pageParam) { |
| queryParams.cursor = pageParam.toString(); |
| } |
| return dataService.getMarketplaceAgents(queryParams); |
| }, |
| getNextPageParam: (lastPage) => lastPage?.after ?? undefined, |
| enabled: !!params.requiredPermission, |
| keepPreviousData: true, |
| staleTime: 2 * 60 * 1000, |
| cacheTime: 10 * 60 * 1000, |
| refetchOnWindowFocus: false, |
| refetchOnReconnect: false, |
| refetchOnMount: false, |
| ...config, |
| }); |
| }; |
|
|