Skip to content

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

fix(cmdk): add back docs ai endpoint (#38453)

Greg Richardson1feafb604b63
diff --git a/apps/studio/.env b/apps/studio/.env
index addb3f02120..aa475c4b298 100644
--- a/apps/studio/.env
+++ b/apps/studio/.env
@@ -21,7 +21,7 @@ NEXT_PUBLIC_GOTRUE_URL=$SUPABASE_PUBLIC_URL/auth/v1
NEXT_PUBLIC_HCAPTCHA_SITE_KEY=10000000-ffff-ffff-ffff-000000000001
# CmdK / AI
-NEXT_PUBLIC_SUPABASE_ANON_KEY=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6InhndWloeHV6cWlid3hqbmlteGV2Iiwicm9sZSI6ImFub24iLCJpYXQiOjE2NzUwOTQ4MzUsImV4cCI6MTk5MDY3MDgzNX0.0PMlOxtKL4O9GGZuAP_Xl4f-Tut1qOnW4bNEmAtoB8w
+NEXT_PUBLIC_SUPABASE_ANON_KEY=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6InhndWloeHV6cWlid3hqbmlteGV2Iiwicm9sZSI6ImFub24iLCJpYXQiOjE3MTgzNzc1MTgsImV4cCI6MjAzMzk1MzUxOH0.aIqjQ9V7djMxYit-DT1fYNV_VWMHSqldh_18XfX2_BE
NEXT_PUBLIC_SUPABASE_URL=https://xguihxuzqibwxjnimxev.supabase.co
DOCKER_SOCKET_LOCATION=/var/run/docker.sock
diff --git a/apps/studio/middleware.ts b/apps/studio/middleware.ts
index 0e34bbb48f2..5327cb589e3 100644
--- a/apps/studio/middleware.ts
+++ b/apps/studio/middleware.ts
@@ -13,6 +13,7 @@ const HOSTED_SUPPORTED_API_URLS = [
'/ai/sql/title-v2',
'/ai/onboarding/design',
'/ai/feedback/classify',
+ '/ai/docs',
'/get-ip-address',
'/get-utc-time',
'/get-deployment-commit',
diff --git a/apps/studio/pages/api/ai/docs.ts b/apps/studio/pages/api/ai/docs.ts
new file mode 100644
index 00000000000..21aaf2da7fa
--- /dev/null
+++ b/apps/studio/pages/api/ai/docs.ts
@@ -0,0 +1,153 @@
+import { SupabaseClient } from '@supabase/supabase-js'
+import { ApplicationError, UserError, clippy } from 'ai-commands/edge'
+import { NextRequest } from 'next/server'
+import OpenAI from 'openai'
+
+export const config = {
+ runtime: 'edge',
+ /* To avoid OpenAI errors, restrict to the Vercel Edge Function regions that
+ overlap with the OpenAI API regions.
+
+ Reference for Vercel regions: https://vercel.com/docs/edge-network/regions#region-list
+ Reference for OpenAI regions: https://help.openai.com/en/articles/5347006-openai-api-supported-countries-and-territories
+ */
+ regions: [
+ 'arn1',
+ 'bom1',
+ 'cdg1',
+ 'cle1',
+ 'cpt1',
+ 'dub1',
+ 'fra1',
+ 'gru1',
+ 'hnd1',
+ 'iad1',
+ 'icn1',
+ 'kix1',
+ 'lhr1',
+ 'pdx1',
+ 'sfo1',
+ 'sin1',
+ 'syd1',
+ ],
+}
+
+const openAiKey = process.env.OPENAI_API_KEY
+const supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL
+const supabaseServiceKey = process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY
+
+export default async function handler(req: NextRequest) {
+ console.log('AI Docs request received', supabaseUrl, supabaseServiceKey)
+ if (!openAiKey) {
+ return new Response(
+ JSON.stringify({
+ error: 'No OPENAI_API_KEY set. Create this environment variable to use AI features.',
+ }),
+ {
+ status: 500,
+ headers: { 'Content-Type': 'application/json' },
+ }
+ )
+ }
+
+ if (!supabaseUrl) {
+ return new Response(
+ JSON.stringify({
+ error:
+ 'No NEXT_PUBLIC_SUPABASE_URL set. Create this environment variable to use AI features.',
+ }),
+ {
+ status: 500,
+ headers: { 'Content-Type': 'application/json' },
+ }
+ )
+ }
+
+ if (!supabaseServiceKey) {
+ return new Response(
+ JSON.stringify({
+ error:
+ 'No NEXT_PUBLIC_SUPABASE_ANON_KEY set. Create this environment variable to use AI features.',
+ }),
+ {
+ status: 500,
+ headers: { 'Content-Type': 'application/json' },
+ }
+ )
+ }
+
+ const { method } = req
+
+ switch (method) {
+ case 'POST':
+ return handlePost(req)
+ default:
+ return new Response(
+ JSON.stringify({ data: null, error: { message: `Method ${method} Not Allowed` } }),
+ {
+ status: 405,
+ headers: { 'Content-Type': 'application/json', Allow: 'POST' },
+ }
+ )
+ }
+}
+
+async function handlePost(request: NextRequest) {
+ const openai = new OpenAI({ apiKey: openAiKey })
+
+ const body = await (request.json() as Promise<{
+ messages: { content: string; role: 'user' | 'assistant' }[]
+ }>)
+
+ const { messages } = body
+
+ if (!messages) {
+ throw new UserError('Missing messages in request data')
+ }
+
+ const supabaseClient = new SupabaseClient(supabaseUrl!, supabaseServiceKey!)
+
+ try {
+ const response = await clippy(openai, supabaseClient, messages)
+
+ // Proxy the streamed SSE response from OpenAI
+ return new Response(response.body, {
+ headers: {
+ 'Content-Type': 'text/event-stream',
+ },
+ })
+ } catch (error: unknown) {
+ console.error(error)
+ if (error instanceof UserError) {
+ return new Response(
+ JSON.stringify({
+ error: error.message,
+ data: error.data,
+ }),
+ {
+ status: 400,
+ headers: { 'Content-Type': 'application/json' },
+ }
+ )
+ } else if (error instanceof ApplicationError) {
+ // Print out application errors with their additional data
+ console.error(`${error.message}: ${JSON.stringify(error.data)}`)
+ } else {
+ // Print out unexpected errors as is to help with debugging
+ console.error(error)
+ }
+
+ console.log('Returning generic 500 ApplicationError to client')
+
+ // TODO: include more response info in debug environments
+ return new Response(
+ JSON.stringify({
+ error: 'There was an error processing your request',
+ }),
+ {
+ status: 500,
+ headers: { 'Content-Type': 'application/json' },
+ }
+ )
+ }
+}