diff --git a/lib/chat/handleChatStream.ts b/lib/chat/handleChatStream.ts index 396a66e..56cfb96 100644 --- a/lib/chat/handleChatStream.ts +++ b/lib/chat/handleChatStream.ts @@ -1,5 +1,6 @@ import { NextRequest, NextResponse } from "next/server"; import { createUIMessageStream, createUIMessageStreamResponse } from "ai"; +import { handleChatCompletion } from "./handleChatCompletion"; import { validateChatRequest } from "./validateChatRequest"; import { setupChatRequest } from "./setupChatRequest"; import { getCorsHeaders } from "@/lib/networking/getCorsHeaders"; @@ -37,6 +38,17 @@ export async function handleChatStream(request: NextRequest): Promise // Note: Credit handling and chat completion handling will be added // as part of the handleChatCredits and handleChatCompletion migrations }, + onFinish: async (event) => { + if (event.isAborted) { + return; + } + const assistantMessages = event.messages.filter( + (message) => message.role === "assistant", + ); + const responseMessages = + assistantMessages.length > 0 ? assistantMessages : [event.responseMessage]; + await handleChatCompletion(body, responseMessages); + }, onError: (e) => { console.error("/api/chat onError:", e); return JSON.stringify({