The chat turn is now composed, not implemented, by chat-handler.ts:
- src/server/turn-service.ts: one named, error-contained seam per turn
responsibility — buildTeacherContext (W8 read channel), persistTurn
(canonical state write), spawnTurnJobs (async protocol kick-off),
foldTurnIntoGraph (session→graph consolidation; the extraction-pipeline
stage swaps this body behind the same seam). Each takes its deps
explicitly; failures log one [chat <id>] line and never touch the
response.
- chat-handler.ts wires: storage ports (stores), the job runner, the
teacher tools (ctx = chatId + injected ChatStore), and the services.
It no longer contains any logic beyond request validation.
Agent layer now receives dependencies instead of importing them — a new
lint zone enforces it (src/agent never imports src/server or src/store;
verified to fire both ways):
- runner.ts: createJobRunner({ chatStore, jobStore }) factory returning
the spawn hook + the three job runners; the model-override test seam
stays module-level. Bodies otherwise verbatim.
- plan tools: persist through the ctx-injected ChatStore
(TeacherToolContext = { chatId, chatStore }).
- teacher-agent's dependency-free default registry is now the true
client-side set: questionTools + jobTools (jobTools was ctx-parameter-
shaped but never used its ctx — now honestly context-free). Production
always passes createTeacherTools({ chatId, chatStore }); tests that
drive a bare mock model get the client-side set unchanged.
Zero behavior change: tsc clean, 190/190 tests, eslint clean, production
build clean. Tests inject the same fs ports the chat-handler does.
53 lines
2.1 KiB
TypeScript
53 lines
2.1 KiB
TypeScript
import { createAgentUIStreamResponse, type UIMessage } from "ai";
|
|
import { createTeacherAgent, type TeacherAgent } from "../agent/teacher-agent.ts";
|
|
import { createJobRunner } from "../agent/subagents/runner.ts";
|
|
import { createTeacherTools } from "../agent/tools.ts";
|
|
import { stores } from "./stores.ts";
|
|
import { buildTeacherContext, foldTurnIntoGraph, persistTurn, spawnTurnJobs } from "./turn-service.ts";
|
|
|
|
/**
|
|
* One chat turn: run the teacher agent over the client's message list and
|
|
* stream the UI message stream back. Framework-free (plain Request/Response)
|
|
* so tests can drive it directly with a scripted mock model — the Next route
|
|
* handler in app/api/chat is a one-line wrapper.
|
|
*
|
|
* This module is the TURN COMPOSITION ROOT: it wires the storage ports, the
|
|
* job runner, the teacher tools, and the turn services (turn-service.ts)
|
|
* together — all actual work lives behind those seams. The background job
|
|
* runner closes over the same port objects the services see.
|
|
*/
|
|
|
|
const jobRunner = createJobRunner(stores);
|
|
|
|
export async function handleChatRequest(req: Request, agent?: TeacherAgent): Promise<Response> {
|
|
let body: { chatId?: unknown; messages?: unknown };
|
|
try {
|
|
body = await req.json();
|
|
} catch {
|
|
return Response.json({ error: "Invalid JSON body" }, { status: 400 });
|
|
}
|
|
const chatId = typeof body.chatId === "string" ? body.chatId : "";
|
|
if (!chatId || !stores.chatStore.getChat(chatId)) {
|
|
return Response.json({ error: "Unknown chat" }, { status: 404 });
|
|
}
|
|
if (!Array.isArray(body.messages)) {
|
|
return Response.json({ error: "messages must be an array" }, { status: 400 });
|
|
}
|
|
|
|
return createAgentUIStreamResponse({
|
|
agent:
|
|
agent ??
|
|
createTeacherAgent(
|
|
undefined,
|
|
createTeacherTools({ chatId, chatStore: stores.chatStore }),
|
|
buildTeacherContext(stores.graphStore, chatId),
|
|
),
|
|
uiMessages: body.messages as UIMessage[],
|
|
onFinish: ({ messages, isAborted }) => {
|
|
persistTurn(stores.chatStore, chatId, messages as UIMessage[], isAborted);
|
|
spawnTurnJobs(jobRunner, chatId, messages as UIMessage[]);
|
|
foldTurnIntoGraph(stores, chatId);
|
|
},
|
|
});
|
|
}
|