MCPcopy Create free account
hub / github.com/tiann/hapi / useCodexModels

Function useCodexModels

web/src/hooks/queries/useCodexModels.ts:6–52  ·  view source on GitHub ↗
(args: {
    api: ApiClient | null
    sessionId?: string | null
    machineId?: string | null
    enabled?: boolean
})

Source from the content-addressed store, hash-verified

4import { queryKeys } from '@/lib/query-keys'
5
6export function useCodexModels(args: {
7 api: ApiClient | null
8 sessionId?: string | null
9 machineId?: string | null
10 enabled?: boolean
11}): {
12 models: CodexModelSummary[]
13 isLoading: boolean
14 error: string | null
15} {
16 const { api, sessionId, machineId } = args
17 const enabled = Boolean(args.enabled && api && (sessionId || machineId))
18 const queryKey = sessionId
19 ? queryKeys.sessionCodexModels(sessionId)
20 : queryKeys.machineCodexModels(machineId ?? 'unknown')
21
22 const query = useQuery({
23 queryKey,
24 queryFn: async () => {
25 if (!api) {
26 throw new Error('API unavailable')
27 }
28 if (sessionId) {
29 return await api.getSessionCodexModels(sessionId)
30 }
31 if (machineId) {
32 return await api.getMachineCodexModels(machineId)
33 }
34 throw new Error('Codex models target unavailable')
35 },
36 enabled,
37 staleTime: 30_000,
38 retry: false,
39 })
40
41 return {
42 models: query.data?.models ?? [],
43 isLoading: query.isLoading,
44 error: query.data?.success === false
45 ? (query.data.error ?? 'Failed to load Codex models')
46 : query.error instanceof Error
47 ? query.error.message
48 : query.error
49 ? 'Failed to load Codex models'
50 : null,
51 }
52}

Callers 2

SessionChatInnerFunction · 0.90
NewSessionFunction · 0.90

Calls 2

getSessionCodexModelsMethod · 0.80
getMachineCodexModelsMethod · 0.80

Tested by

no test coverage detected