import { useQuery } from "@tanstack/react-query"; import { useTranslation } from "react-i18next"; import type { DaemonClient } from "@getpaseo/client/internal/daemon-client"; import { useHostRuntimeClient, useHostRuntimeIsConnected } from "@/runtime/host-runtime"; import { agentCommandsQueryKey, type AgentCommandsDraftConfig } from "@/hooks/agent-commands-query"; const DRAFT_COMMANDS_STALE_TIME = Number.POSITIVE_INFINITY; const SESSION_COMMANDS_STALE_TIME = 60_000; export interface AgentSlashCommand { name: string; description: string; argumentHint: string; kind?: string; } export type DraftCommandConfig = AgentCommandsDraftConfig; export type AgentCommandsClient = Pick; export async function fetchAgentCommands(input: { client: AgentCommandsClient; agentId: string; draftConfig?: DraftCommandConfig; }): Promise { const response = await input.client.listCommands(input.agentId, { draftConfig: input.draftConfig, }); return response.commands as AgentSlashCommand[]; } interface UseAgentCommandsQueryOptions { serverId: string; agentId: string; enabled?: boolean; draftConfig?: DraftCommandConfig; } export function useAgentCommandsQuery({ serverId, agentId, enabled = true, draftConfig, }: UseAgentCommandsQueryOptions) { const { t } = useTranslation(); const client = useHostRuntimeClient(serverId); const isConnected = useHostRuntimeIsConnected(serverId); const query = useQuery({ queryKey: agentCommandsQueryKey({ serverId, agentId, draftConfig }), queryFn: async () => { if (!client) { throw new Error(t("common.errors.daemonClientUnavailable")); } return fetchAgentCommands({ client, agentId, draftConfig }); }, enabled: enabled && !!client && isConnected && (!!agentId || !!draftConfig), staleTime: draftConfig ? DRAFT_COMMANDS_STALE_TIME : SESSION_COMMANDS_STALE_TIME, retry: 3, retryDelay: (attemptIndex) => Math.min(1000 * 2 ** attemptIndex, 5000), }); // isPending is true when the query has never run yet (no cached data and not fetching) // isLoading is true when fetching and no data yet const isLoading = query.isPending || query.isLoading; return { commands: query.data ?? [], isLoading, isError: query.isError, error: query.error, }; }