mirror of
https://github.com/supabase/supabase.git
synced 2026-10-05 09:25:06 +03:00
* page components * page component changes * settings but broken saving * rvert * use sheet for provider * styling * remove things * Some refactoring and fixing, added JSDocs to layouts * Smol refactor * Fix * Update JSDocs * updated scaffolding * update edge functions layout * remove params * Clean up * Spelling * Clean up FormFieldWrappers * One last clean up * create edge functon * use component * flag * fix merge errors * fix imports * fix merge issues * fix import * fix flag * remove unused * move templates * re-add flag * Small refactors * Smol refactors * Refactor * add rename and delete via context * create function fixes * moved function name * Smol update * Small fix for selected state in edge functions side menu when on /new --------- Co-authored-by: Joshen Lim <joshenlimek@gmail.com>
102 lines
2.4 KiB
TypeScript
102 lines
2.4 KiB
TypeScript
import { StreamingTextResponse } from 'ai'
|
|
import { chatRlsPolicy } from 'ai-commands/edge'
|
|
import { DatabasePoliciesData } from 'data/database-policies/database-policies-query'
|
|
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
|
|
|
|
export default async function handler(req: NextRequest) {
|
|
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' },
|
|
}
|
|
)
|
|
}
|
|
|
|
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' }[]
|
|
entityDefinitions: string[]
|
|
existingPolicies: DatabasePoliciesData
|
|
policyDefinition: string
|
|
}>)
|
|
|
|
const { messages, entityDefinitions, existingPolicies, policyDefinition } = body
|
|
|
|
try {
|
|
const stream = await chatRlsPolicy(
|
|
openai,
|
|
messages,
|
|
entityDefinitions,
|
|
existingPolicies,
|
|
policyDefinition
|
|
)
|
|
return new StreamingTextResponse(stream)
|
|
} catch (error) {
|
|
console.error(error)
|
|
|
|
return new Response(
|
|
JSON.stringify({
|
|
error: 'There was an error processing your request',
|
|
}),
|
|
{
|
|
status: 500,
|
|
headers: { 'Content-Type': 'application/json' },
|
|
}
|
|
)
|
|
}
|
|
}
|