mirror of
https://github.com/wu736139669/hapi.git
synced 2026-08-08 07:17:39 +00:00
feat(cursor): support model selection (#684)
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
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,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
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 useCursorModelsForMachine(args: {
|
||||
api: ApiClient | null
|
||||
machineId?: string | null
|
||||
enabled?: boolean
|
||||
}): {
|
||||
availableModels: CursorModelSummary[]
|
||||
currentModelId: string | null
|
||||
isLoading: boolean
|
||||
error: string | null
|
||||
refetch: () => void
|
||||
} {
|
||||
const { api, machineId } = args
|
||||
const enabled = Boolean(args.enabled && api && machineId)
|
||||
|
||||
const query = useQuery({
|
||||
queryKey: machineId
|
||||
? queryKeys.machineCursorModels(machineId)
|
||||
: ['machine-cursor-models', 'unknown'] as const,
|
||||
queryFn: async () => {
|
||||
if (!api) {
|
||||
throw new Error('API unavailable')
|
||||
}
|
||||
if (!machineId) {
|
||||
throw new Error('Cursor models target unavailable')
|
||||
}
|
||||
return await api.getMachineCursorModels(machineId)
|
||||
},
|
||||
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,
|
||||
refetch: () => {
|
||||
void query.refetch()
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user