From ce5376debfbd1c5020c236e7c13915fa61dfdd4e Mon Sep 17 00:00:00 2001 From: Sidney Swift <158200036+sidneyswift@users.noreply.github.com> Date: Fri, 16 Jan 2026 21:48:12 -0500 Subject: [PATCH] Wire chat completion for streaming --- lib/chat/handleChatStream.ts | 12 ++++++++++++ 1 file changed, 12 insertions(+) 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({