mirror of
https://github.com/supabase/supabase.git
synced 2026-10-15 22:29:11 +03:00
* Add a API endpoint for generating queries for the SQL editor. * Merge all multiline props. * Add a new component for diff actions. * Copy components for the AI panel. * Add useChat hook. * Add a feature preview for the this feature. The preview is dependent on the feature flag. * Reorder the nesting in the SQL editor to accomodate the AI assistant. * Try to fit both AI assistants in the SQL editor, available via a feature preview. * Refactor the SQL editor to make the diff work correctly in all cases. * Minor fixes for the old AI feature. * Fix the debug functionality to work with both assistants. * Fix some copy-paste leftovers. * Remove unneeded code. * Make the icons softer. * Fix the name of the panel component. * Fix console.logs. * Add overflow to the AI assistant. * surface opt in config in ai settings button * Skip diffing if editor is empty * Add selected state when selecting a message to insert/replace code * Add sample prompts * Add SQL ai dislaimer when replacing code * Light mode action bar nudges * Add text for the feature preview. * lang nudges * Hide the command suggestions for now. * Set the discussion url to undefined. * Don't add the disclaimer twice. --------- Co-authored-by: Joshen Lim <joshenlimek@gmail.com> Co-authored-by: Terry Sutton <saltcod@gmail.com>
71 lines
1.8 KiB
TypeScript
71 lines
1.8 KiB
TypeScript
import { StreamingTextResponse } from 'ai'
|
|
import { generateV2 } from 'ai-commands/edge'
|
|
import { NextRequest } from 'next/server'
|
|
import OpenAI from 'openai'
|
|
|
|
export const runtime = 'edge'
|
|
|
|
const openAiKey = process.env.OPENAI_KEY
|
|
|
|
export default async function handler(req: NextRequest) {
|
|
if (!openAiKey) {
|
|
return new Response(
|
|
JSON.stringify({
|
|
error: 'No OPENAI_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' }[]
|
|
existingSql?: string
|
|
entityDefinitions: string[]
|
|
}>)
|
|
|
|
const { messages, existingSql, entityDefinitions } = body
|
|
|
|
try {
|
|
const stream = await generateV2(openai, messages, existingSql, entityDefinitions)
|
|
return new StreamingTextResponse(stream)
|
|
} catch (error) {
|
|
if (error instanceof Error) {
|
|
console.error(`AI SQL generation-v2 failed: ${error.message}`)
|
|
} else {
|
|
console.error(`AI SQL generation-v2 failed: ${error}`)
|
|
}
|
|
|
|
return new Response(
|
|
JSON.stringify({
|
|
error: 'There was an error processing your request',
|
|
}),
|
|
{
|
|
status: 500,
|
|
headers: { 'Content-Type': 'application/json' },
|
|
}
|
|
)
|
|
}
|
|
}
|