Spaces:
Sleeping
Sleeping
| import { smoothStream, streamText } from "ai"; | |
| import { customModelProvider } from "lib/ai/models"; | |
| import { CREATE_THREAD_TITLE_PROMPT } from "lib/ai/prompts"; | |
| import globalLogger from "logger"; | |
| import { ChatModel } from "app-types/chat"; | |
| import { chatRepository } from "lib/db/repository"; | |
| import { getSession } from "auth/server"; | |
| import { colorize } from "consola/utils"; | |
| import { handleError } from "../shared.chat"; | |
| const logger = globalLogger.withDefaults({ | |
| message: colorize("blackBright", `Title API: `), | |
| }); | |
| export async function POST(request: Request) { | |
| try { | |
| const json = await request.json(); | |
| const { | |
| chatModel, | |
| message = "hello", | |
| threadId, | |
| } = json as { | |
| chatModel?: ChatModel; | |
| message: string; | |
| threadId: string; | |
| }; | |
| const session = await getSession(); | |
| if (!session) { | |
| return new Response("Unauthorized", { status: 401 }); | |
| } | |
| logger.info( | |
| `chatModel: ${chatModel?.provider}/${chatModel?.model}, threadId: ${threadId}`, | |
| ); | |
| const thread = await chatRepository.selectThreadDetails(threadId); | |
| const result = streamText({ | |
| model: customModelProvider.getDynamicModel(chatModel, thread?.userPreferences), | |
| system: CREATE_THREAD_TITLE_PROMPT, | |
| experimental_transform: smoothStream({ chunking: "word" }), | |
| prompt: message, | |
| abortSignal: request.signal, | |
| onFinish: (ctx) => { | |
| chatRepository | |
| .upsertThread({ | |
| id: threadId, | |
| title: ctx.text, | |
| userId: session.user.id, | |
| }) | |
| .catch((err) => logger.error(err)); | |
| }, | |
| }); | |
| return result.toUIMessageStreamResponse(); | |
| } catch (err) { | |
| return new Response(handleError(err), { status: 500 }); | |
| } | |
| } | |