An AI-powered search engine with a generative UI
TypeScript generative-aigenerative-uinextjsollamareactsearxngshadcn-uitailwindcsstavilytypescriptupstashvercel-ai-sdk
chore: add perf logging around chat stream
Yoshiki Miura21a3edb73777
diff --git a/app/api/chat/route.ts b/app/api/chat/route.tsindex 01283c9d..d8736690 100644--- a/app/api/chat/route.ts+++ b/app/api/chat/route.ts@@ -92,6 +92,11 @@ export async function POST(req: Request) {)}+ const streamStart = performance.now()+ perfLog(+ `createChatStreamResponse - Start: model=${selectedModel.providerId}:${selectedModel.id}, searchMode=${searchMode}`+ )+const response = await createChatStreamResponse({message,model: selectedModel,@@ -104,6 +109,8 @@ export async function POST(req: Request) {searchMode})+ perfTime('createChatStreamResponse resolved', streamStart)+// Invalidate the cache for this specific chat after creating the response// This ensures the next load will get fresh dataif (chatId) {diff --git a/lib/streaming/create-chat-stream-response.ts b/lib/streaming/create-chat-stream-response.tsindex 3463a797..05e0b5ce 100644--- a/lib/streaming/create-chat-stream-response.ts+++ b/lib/streaming/create-chat-stream-response.ts@@ -116,7 +116,12 @@ export async function createChatStreamResponse(execute: async ({ writer }: { writer: UIMessageStreamWriter }) => {try {// Prepare messages for the model+ const prepareStart = performance.now()+ perfLog(+ `prepareMessages - Invoked: trigger=${trigger}, isNewChat=${isNewChat}`+ )const messagesToModel = await prepareMessages(context, message)+ perfTime('prepareMessages completed (stream)', prepareStart)// Get the researcher agent with parent trace ID and search modeconst researchAgent = researcher({@@ -161,6 +166,10 @@ export async function createChatStreamResponse(})}+ const llmStart = performance.now()+ perfLog(+ `researchAgent.stream - Start: model=${context.modelId}, searchMode=${searchMode}`+ )const result = researchAgent.stream({ messages: modelMessages })result.consumeStream()// Stream with the research agent, including metadata@@ -180,6 +189,7 @@ export async function createChatStreamResponse()const responseMessages = (await result.response).messages+ perfTime('researchAgent.stream completed', llmStart)// Generate related questionsif (responseMessages && responseMessages.length > 0) {// Find the last user message