Files
supabase/apps/database-new/app/api/ai/sql/threads/create/route.ts
T
1245ee7236 db design now saving messages to supabase db (#19380)
* First

* Fix the parsing of SQL.

* Get thread data action

* Remove old page

* Wait for data to complete

* SSR everywhere

* Remove unused

* Add monaco settings

* update loaders

* Add sorting

* supress hydration warnings

* Fix profile layout

* more server action action

* fixes

* Update page.tsx

* Update actions.ts

* Update AssistantChatForm.tsx

* init

* add polling

* Update actions.ts

* redirect to message id

* init

* Update SchemaFlow.tsx

* Add latest message to thread record

* Fix object object on new thread load

* Fix build

* Prettier

* Fix type errors for build

* Add proper loaders

* Remove badger

---------

Co-authored-by: Terry Sutton <saltcod@gmail.com>
Co-authored-by: Ivan Vasilov <vasilov.ivan@gmail.com>
2023-12-11 21:08:40 -03:30

59 lines
1.5 KiB
TypeScript

import { cookies } from 'next/headers'
import { createClient } from '@/lib/supabase/server'
import OpenAI from 'openai'
import { revalidatePath } from 'next/cache'
const openai = new OpenAI()
export async function POST(req: Request) {
const cookieStore = cookies()
const supabase = createClient(cookieStore)
if (!req.body) {
return Response.error()
}
const { prompt, userID } = await req.json()
const thread = await openai.beta.threads.create()
await openai.beta.threads.messages.create(thread.id, {
role: 'user',
content: prompt,
})
const createRun = await openai.beta.threads.runs.create(thread.id, {
assistant_id: 'asst_oLWrK8lScZVNEpfjwUIvBAnq',
})
const [run, { data: messages }] = await Promise.all([
openai.beta.threads.runs.retrieve(thread.id, createRun.id),
openai.beta.threads.messages.list(thread.id),
])
const threadTitle = messages
.filter((m) => m.role === 'user' && m.content[0]?.type === 'text')
.map((m) => {
if (m.content[0]?.type === 'text') {
return m.content[0]?.text?.value
}
return undefined
})
.find((text) => text !== undefined)
try {
const { error } = await supabase.from('threads').insert({
thread_id: thread.id,
run_id: run.id,
user_id: userID,
thread_title: threadTitle as string,
})
if (error) throw error
} catch (error) {
console.error(error)
}
revalidatePath('/profile')
return Response.json({ threadId: thread.id, runId: run.id })
}