mirror of
https://github.com/wu736139669/hapi.git
synced 2026-08-05 06:24:37 +00:00
50 lines
1.5 KiB
TypeScript
50 lines
1.5 KiB
TypeScript
import { useQuery } from '@tanstack/react-query'
|
|
import type { ApiClient } from '@/api/client'
|
|
import type { CursorModelSummary } from '@/types/api'
|
|
import { queryKeys } from '@/lib/query-keys'
|
|
|
|
export function useCursorModels(args: {
|
|
api: ApiClient | null
|
|
sessionId?: string | null
|
|
enabled?: boolean
|
|
}): {
|
|
availableModels: CursorModelSummary[]
|
|
currentModelId: string | null
|
|
isLoading: boolean
|
|
error: string | null
|
|
} {
|
|
const { api, sessionId } = args
|
|
const enabled = Boolean(args.enabled && api && sessionId)
|
|
|
|
const query = useQuery({
|
|
queryKey: sessionId
|
|
? queryKeys.sessionCursorModels(sessionId)
|
|
: ['session-cursor-models', 'unknown'] as const,
|
|
queryFn: async () => {
|
|
if (!api) {
|
|
throw new Error('API unavailable')
|
|
}
|
|
if (!sessionId) {
|
|
throw new Error('Cursor models target unavailable')
|
|
}
|
|
return await api.getSessionCursorModels(sessionId)
|
|
},
|
|
enabled,
|
|
staleTime: 60_000,
|
|
retry: false,
|
|
})
|
|
|
|
return {
|
|
availableModels: query.data?.availableModels ?? [],
|
|
currentModelId: query.data?.currentModelId ?? null,
|
|
isLoading: query.isLoading,
|
|
error: query.data?.success === false
|
|
? (query.data.error ?? 'Failed to load Cursor models')
|
|
: query.error instanceof Error
|
|
? query.error.message
|
|
: query.error
|
|
? 'Failed to load Cursor models'
|
|
: null,
|
|
}
|
|
}
|