All files / src/hooks useNodeOverrideOptions.ts

100% Statements 14/14
100% Branches 9/9
100% Functions 7/7
100% Lines 12/12

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62                                        31x   31x   12x           31x     2x       31x 31x       31x 29x       31x   31x               31x        
import { Model } from "@/objects/Model";
import { MCPServer } from "@/objects/MCPServer";
import type { MCPServerOption } from "@/apps/user/pages/agents/skills/GraphBuilder";
import { QUERY_KEYS } from "@/services/query_keys";
import { useApiHandler } from "@/services/useApiHandler";
import { useQuery } from "@tanstack/react-query";
import { useMemo } from "react";
import { MCP_AVAILABLE } from "@/util/mcp";
 
/**
 * What a graph node's model and MCP pickers are chosen from.
 *
 * Both builder hosts — a new skill and an existing one — need the same two
 * catalogues, so they live here rather than in either page.
 *
 * Servers are narrowed to `MCPServerOption` on the way out: the full row
 * carries `connection_config`, which holds credentials, and the builder only
 * ever stores an id.
 */
export function useNodeOverrideOptions(enabled: boolean = true) {
  const { getApiHandler } = useApiHandler();
 
  const { data: modelData } = useQuery<Model[]>({
    queryKey: [QUERY_KEYS.GET.MODELS],
    queryFn: () => getApiHandler().model_router.list() as Promise<Model[]>,
    enabled,
  });
 
  // Not asked for while MCP servers are unavailable (util/mcp.ts): the
  // builder offers no MCP picker, so the list is always empty.
  const { data: serverData } = useQuery<MCPServer[]>({
    queryKey: [QUERY_KEYS.GET.USER_MCP_SERVERS],
    queryFn: () =>
      getApiHandler().mcp_server_router.list() as Promise<MCPServer[]>,
    enabled: enabled && MCP_AVAILABLE,
  });
 
  const rawModels: Model[] = modelData ?? [];
  const rawServers: MCPServer[] = serverData ?? [];
 
  // A node is a chat completion, so an embedding model is not a choice; an
  // unavailable one would fail the run svc-core just validated the pin for.
  const models = useMemo(
    () => rawModels.filter((m) => m.is_chat && m.is_available),
    [rawModels],
  );
 
  const mcpServers = useMemo(
    (): MCPServerOption[] =>
      rawServers.map((s) => ({
        id: s.id,
        name: s.name,
        is_active: s.is_active,
      })),
    [rawServers],
  );
 
  return { models, mcpServers };
}
 
export default useNodeOverrideOptions;