Files
supabase/apps/studio/data/ai/check-api-key-query.ts
T
Terry SuttonandJoshen Lim d30ec48b50 Chore/self hosted assistant disabled (#30198)
* Add a message if openai_api_key not set

* Fix disabled state

* Some fixes

* Add same check in global assistant

* Remove check for api key for platform

---------

Co-authored-by: Joshen Lim <joshenlimek@gmail.com>
2024-10-31 09:19:03 -02:30

32 lines
1.1 KiB
TypeScript

import { useQuery, UseQueryOptions } from '@tanstack/react-query'
import { get, isResponseOk } from 'lib/common/fetch'
import { BASE_PATH, IS_PLATFORM } from 'lib/constants'
import { resourceKeys } from './keys'
// check to see if the OPENAI_API_KEY env var is set in self-hosted
// so we can disable the chat editor and add a warning about manually adding the key
export async function checkOpenAIKey(signal?: AbortSignal) {
const response = await get(`${BASE_PATH}/api/ai/sql/check-api-key`, { signal })
if (!isResponseOk(response)) {
throw (response as any).error
}
return response as { hasKey: boolean }
}
export type ResourceData = Awaited<ReturnType<typeof checkOpenAIKey>>
export type ResourceError = { errorEventId: string; message: string }
export const useCheckOpenAIKeyQuery = <TData = ResourceData>({
enabled = true,
...options
}: UseQueryOptions<ResourceData, ResourceError, TData> = {}) =>
useQuery<ResourceData, ResourceError, TData>(
resourceKeys.apiKey(),
({ signal }) => checkOpenAIKey(signal),
{ enabled: !IS_PLATFORM && enabled, ...options }
)