Skip to content

Job application tracker and AI-powered job search assistant. Helps job seekers manage their search journey with AI resume review, job matching, task logging, and application analytics, all while keeping your data private.

refactor: build the chat response with createUIMessageStream

Khuram Niaz57ed0d6b2b04
diff --git a/__tests__/agentChatRoute.spec.ts b/__tests__/agentChatRoute.spec.ts
index ea822d5..4f83e98 100644
--- a/__tests__/agentChatRoute.spec.ts
+++ b/__tests__/agentChatRoute.spec.ts
@@ -16,8 +16,17 @@ vi.mock("@/lib/agent/tools", async (importOriginal) => {
return { ...actual, buildAgentTools: vi.fn(actual.buildAgentTools) };
});
-const toUIMessageStreamResponse = vi.fn(() => ({ status: 200, __stream: true }));
-const streamText = vi.fn((..._args: any[]) => ({ toUIMessageStreamResponse }));
+// The route merges this into createUIMessageStream now. An immediately
+// closed stream is enough — these tests assert on the streamText ARGS.
+const toUIMessageStream = vi.fn(
+ () =>
+ new ReadableStream({
+ start(controller) {
+ controller.close();
+ },
+ }),
+);
+const streamText = vi.fn((..._args: any[]) => ({ toUIMessageStream }));
const streamArgs = (): any => streamText.mock.calls[0]![0];
vi.mock("ai", async (importOriginal) => {
const actual = await importOriginal<typeof import("ai")>();
diff --git a/src/app/api/ai/chat/route.ts b/src/app/api/ai/chat/route.ts
index c5925d4..fcc4630 100644
--- a/src/app/api/ai/chat/route.ts
+++ b/src/app/api/ai/chat/route.ts
@@ -4,6 +4,8 @@ import { auth } from "@/auth";
import { NextRequest, NextResponse } from "next/server";
import {
convertToModelMessages,
+ createUIMessageStream,
+ createUIMessageStreamResponse,
hasToolCall,
isToolUIPart,
stepCountIs,
@@ -148,25 +150,29 @@ export const POST = async (req: NextRequest) => {
const controller = new AbortController();
const timer = setTimeout(() => controller.abort(), APP_CONSTANTS.AGENT_CHAT_TIMEOUT_MS);
- const result = streamText({
- model,
- system: AGENT_CHAT_SYSTEM_PROMPT,
- messages: modelMessages,
- tools: buildAgentTools({ userId, pastedText, pageContext }),
- // Stop after the write: the result card renders deterministically from
- // structured fields, so a second generation just to narrate it is 10-30s
- // of local inference for a sentence that could be wrong.
- stopWhen: [stepCountIs(APP_CONSTANTS.AGENT_CHAT_MAX_STEPS), hasToolCall("add_job")],
- // Argument extraction wants determinism.
- temperature: TEMPERATURES.ANALYSIS,
- abortSignal: controller.signal,
- providerOptions: {
- ollama: { options: { num_ctx: APP_CONSTANTS.AGENT_CHAT_NUM_CTX } },
- },
- });
-
- return result.toUIMessageStreamResponse({
+ const stream = createUIMessageStream({
originalMessages: messages,
+ execute: ({ writer }) => {
+ const result = streamText({
+ model,
+ system: AGENT_CHAT_SYSTEM_PROMPT,
+ messages: modelMessages,
+ tools: buildAgentTools({ userId, pastedText, pageContext }),
+ // Stop after the write: the result card renders deterministically from
+ // structured fields, so a second generation just to narrate it is 10-30s
+ // of local inference for a sentence that could be wrong.
+ stopWhen: [stepCountIs(APP_CONSTANTS.AGENT_CHAT_MAX_STEPS), hasToolCall("add_job")],
+ // Argument extraction wants determinism.
+ temperature: TEMPERATURES.ANALYSIS,
+ abortSignal: controller.signal,
+ providerOptions: {
+ ollama: { options: { num_ctx: APP_CONSTANTS.AGENT_CHAT_NUM_CTX } },
+ },
+ });
+ // createUIMessageStream emits no start/finish of its own — the merged
+ // stream carries them, so this is byte-identical to the old response.
+ writer.merge(result.toUIMessageStream());
+ },
onFinish: async ({ messages: finalMessages, responseMessage, isAborted }) => {
clearTimeout(timer);
const { tool, state, outcome } = outcomeOf(responseMessage);
@@ -190,4 +196,6 @@ export const POST = async (req: NextRequest) => {
return mapAgentError(error, errorContext);
},
});
+
+ return createUIMessageStreamResponse({ stream });
};