Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

Chore/self hosted assistant disabled (#30198)

Terry Suttond30ec48b50f4
diff --git a/apps/studio/components/interfaces/SQLEditor/AiAssistantPanel/index.tsx b/apps/studio/components/interfaces/SQLEditor/AiAssistantPanel/index.tsx
index 9a55270116d..994de7cb037 100644
--- a/apps/studio/components/interfaces/SQLEditor/AiAssistantPanel/index.tsx
+++ b/apps/studio/components/interfaces/SQLEditor/AiAssistantPanel/index.tsx
@@ -4,10 +4,14 @@ import { Info } from 'lucide-react'
import { useEffect, useMemo, useRef, useState } from 'react'
import { toast } from 'sonner'
+import { Alert, AlertDescription, AlertTitle } from '@ui/components/shadcn/ui/alert'
import { Message as MessageType } from 'ai'
import { useChat } from 'ai/react'
import { useParams } from 'common'
+import { IS_PLATFORM } from 'common/constants/environment'
+import { Markdown } from 'components/interfaces/Markdown'
import { SchemaComboBox } from 'components/ui/SchemaComboBox'
+import { useCheckOpenAIKeyQuery } from 'data/ai/check-api-key-query'
import { useEntityDefinitionsQuery } from 'data/database/entity-definitions-query'
import { useOrganizationUpdateMutation } from 'data/organizations/organization-update-mutation'
import { useSendEventMutation } from 'data/telemetry/send-event-mutation'
@@ -73,6 +77,9 @@ export const AiAssistantPanel = ({
const shouldShowNotOptimizedAlert =
selectedOrganization && !includeSchemaMetadata && showAiNotOptimizedWarningSetting
+ const { data: check } = useCheckOpenAIKeyQuery()
+ const isApiKeySet = IS_PLATFORM || !!check?.hasKey
+
const { data } = useEntityDefinitionsQuery(
{
schemas: selectedSchemas,
@@ -289,11 +296,21 @@ export const AiAssistantPanel = ({
<div ref={bottomRef} className="h-1" />
</div>
- <div className="sticky p-5 flex-0 border-t">
+ <div className="sticky p-5 flex-0 border-t grid gap-4">
+ {!isApiKeySet && (
+ <Alert>
+ <AlertTitle>OpenAI API key not set</AlertTitle>
+ <AlertDescription>
+ <Markdown
+ content={'Add your `OPENAI_API_KEY` to `./docker/.env` to use the AI Assistant.'}
+ />
+ </AlertDescription>
+ </Alert>
+ )}
<AssistantChatForm
textAreaRef={inputRef}
loading={isLoading}
- disabled={isLoading}
+ disabled={isLoading || !isApiKeySet}
icon={
<AiIconAnimation
allowHoverEffect
diff --git a/apps/studio/components/ui/AIAssistantPanel/AIAssistant.tsx b/apps/studio/components/ui/AIAssistantPanel/AIAssistant.tsx
index 368489cebad..6105135bba8 100644
--- a/apps/studio/components/ui/AIAssistantPanel/AIAssistant.tsx
+++ b/apps/studio/components/ui/AIAssistantPanel/AIAssistant.tsx
@@ -9,9 +9,11 @@ import { toast } from 'sonner'
import type { Message as MessageType } from 'ai/react'
import { useChat } from 'ai/react'
import { useParams } from 'common'
+import { Markdown } from 'components/interfaces/Markdown'
import OptInToOpenAIToggle from 'components/interfaces/Organization/GeneralSettings/OptInToOpenAIToggle'
import { MessageWithDebug } from 'components/interfaces/SQLEditor/AiAssistantPanel'
import { DiffType } from 'components/interfaces/SQLEditor/SQLEditor.types'
+import { useCheckOpenAIKeyQuery } from 'data/ai/check-api-key-query'
import { useSqlDebugMutation } from 'data/ai/sql-debug-mutation'
import { useEntityDefinitionQuery } from 'data/database/entity-definition-query'
import { useEntityDefinitionsQuery } from 'data/database/entity-definitions-query'
@@ -116,6 +118,9 @@ export const AIAssistant = ({
selectedSchemas.length === 0 &&
selectedTables.length === 0
+ const { data: check } = useCheckOpenAIKeyQuery()
+ const isApiKeySet = IS_PLATFORM || !!check?.hasKey
+
const { data: existingDefinition } = useEntityDefinitionQuery({
id: entity?.id,
type: editor,
@@ -457,6 +462,19 @@ export const AIAssistant = ({
description="Give us a moment while we work on bringing the Assistant back online"
/>
)}
+ {!isApiKeySet && (
+ <Admonition
+ type="warning"
+ title="OpenAI API key not set"
+ description={
+ <Markdown
+ content={
+ 'Add your `OPENAI_API_KEY` to `./docker/.env` to use the AI Assistant.'
+ }
+ />
+ }
+ />
+ )}
<div className="w-full border rounded">
<div className="py-2 px-3 border-b flex gap-2 flex-wrap">
<DropdownMenu>
@@ -619,7 +637,7 @@ export const AIAssistant = ({
'[&>textarea]:rounded-none [&>textarea]:border-0 [&>textarea]:!outline-none [&>textarea]:!ring-offset-0 [&>textarea]:!ring-0'
)}
loading={isLoading}
- disabled={disablePrompts || isLoading}
+ disabled={!isApiKeySet || disablePrompts || isLoading}
placeholder={
hasMessages ? 'Reply to the assistant...' : 'How can we help you today?'
}
@@ -630,7 +648,7 @@ export const AIAssistant = ({
sendMessageToAssistant(value)
}}
/>
- {!hasMessages && (
+ {!hasMessages && IS_PLATFORM && (
<div className="text-xs text-foreground-lighter text-opacity-60 bg-control px-3 pb-2">
The Assistant is in Alpha and your prompts might be rate limited
</div>
diff --git a/apps/studio/data/ai/check-api-key-query.ts b/apps/studio/data/ai/check-api-key-query.ts
new file mode 100644
index 00000000000..8e21abd1df2
--- /dev/null
+++ b/apps/studio/data/ai/check-api-key-query.ts
@@ -0,0 +1,31 @@
+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 }
+ )
diff --git a/apps/studio/data/ai/keys.ts b/apps/studio/data/ai/keys.ts
new file mode 100644
index 00000000000..051620ea5e1
--- /dev/null
+++ b/apps/studio/data/ai/keys.ts
@@ -0,0 +1,3 @@
+export const resourceKeys = {
+ apiKey: () => ['api-key'] as const,
+}
diff --git a/apps/studio/pages/api/ai/sql/check-api-key.ts b/apps/studio/pages/api/ai/sql/check-api-key.ts
new file mode 100644
index 00000000000..5c95b625b26
--- /dev/null
+++ b/apps/studio/pages/api/ai/sql/check-api-key.ts
@@ -0,0 +1,24 @@
+import apiWrapper from 'lib/api/apiWrapper'
+import { NextApiRequest, NextApiResponse } from 'next'
+
+export default (req: NextApiRequest, res: NextApiResponse) => apiWrapper(req, res, handler)
+
+async function handler(req: NextApiRequest, res: NextApiResponse) {
+ const { method } = req
+
+ switch (method) {
+ case 'GET':
+ return handleGet(req, res)
+ default:
+ res.setHeader('Allow', ['POST'])
+ res.status(405).json({ data: null, error: { message: `Method ${method} Not Allowed` } })
+ }
+}
+
+const handleGet = async (req: NextApiRequest, res: NextApiResponse) => {
+ if (process.env.OPENAI_API_KEY) {
+ return res.status(200).json({ hasKey: true })
+ } else {
+ return res.status(404).json({ hasKey: false })
+ }
+}