Storage ports: ChatStore/GraphStore/JobStore + adapters + core inversion (stage 3)
Per-entity semantic ports (src/core/ports.ts), one adapter family
(src/store/*, fs JSON/JSONL), exactly one composition point
(src/server/stores.ts). Dependency direction is now machine-checked:
adapters implement core interfaces; core never imports an adapter.
The inversion:
- core/graph.ts (emitChat/getGraph/graphSnapshot/foldStaleChats) is now
pure — it takes the storage ports as explicit parameters (GraphDeps).
No store imports; newGraphNodeId is local; node id format unchanged.
- core/graph-context.ts buildGraphContext() no longer defaults to the
live ledgers: the chat-handler hands in getGraph(graphStore) per turn.
- store adapters export exactly one object each implementing their port
(chatStore/graphStore/jobStore); bodies otherwise verbatim.
NodeMapping + SessionCheckpoint moved store/graph -> core/contracts
(they ARE contract shapes); CreateJobInit/JobPatch live in ports.
- titleFromPrompt moved store/chats -> core/contracts (pure helper,
planNodeStatus precedent). Store modules now contain persistence only.
- server/stores.ts is the composition root; server/{chats,graph,jobs}.ts
and chat-handler wire from it; agent (plan tools, sub-agent runner)
call the port objects. The shell surface (@/src/server exports) is
unchanged.
Lint law extended: the whole of src/core is now a purity zone (was a
file glob with a graph/graph-context exemption). Verified it fires on
core->store imports; tsc clean, 190/190 tests, eslint clean, prod build
clean (prerender 6/6). Tests pass deps explicitly — graph.test uses the
same fs-adapter pair stores.ts wires in production; graph-context.test
keeps its in-memory ledgers as a GraphStore implementation.
This commit is contained in:
@@ -2,9 +2,9 @@ import { createAgentUIStreamResponse, type UIMessage } from "ai";
|
||||
import { createTeacherAgent, type TeacherAgent } from "../agent/teacher-agent.ts";
|
||||
import { createTeacherTools } from "../agent/tools.ts";
|
||||
import { spawnPendingJobs } from "../agent/subagents/runner.ts";
|
||||
import { emitChat } from "../core/graph.ts";
|
||||
import { emitChat, getGraph } from "../core/graph.ts";
|
||||
import { buildGraphContext } from "../core/graph-context.ts";
|
||||
import { getChat, saveMessages } from "../store/chats.ts";
|
||||
import { stores } from "./stores.ts";
|
||||
|
||||
/**
|
||||
* One chat turn: run the teacher agent over the client's message list and
|
||||
@@ -23,7 +23,7 @@ export async function handleChatRequest(req: Request, agent?: TeacherAgent): Pro
|
||||
return Response.json({ error: "Invalid JSON body" }, { status: 400 });
|
||||
}
|
||||
const chatId = typeof body.chatId === "string" ? body.chatId : "";
|
||||
if (!chatId || !getChat(chatId)) {
|
||||
if (!chatId || !stores.chatStore.getChat(chatId)) {
|
||||
return Response.json({ error: "Unknown chat" }, { status: 404 });
|
||||
}
|
||||
if (!Array.isArray(body.messages)) {
|
||||
@@ -37,7 +37,7 @@ export async function handleChatRequest(req: Request, agent?: TeacherAgent): Pro
|
||||
// take the lesson down: the block is context, not a dependency.
|
||||
let graphContext: string | null = null;
|
||||
try {
|
||||
graphContext = buildGraphContext();
|
||||
graphContext = buildGraphContext(getGraph(stores.graphStore));
|
||||
} catch (err) {
|
||||
console.error(`[chat ${chatId}] graph context build failed:`, err);
|
||||
}
|
||||
@@ -49,7 +49,7 @@ export async function handleChatRequest(req: Request, agent?: TeacherAgent): Pro
|
||||
// Persist even when aborted: the partial transcript (including any
|
||||
// pending question part) is what makes resume work.
|
||||
try {
|
||||
saveMessages(chatId, messages as UIMessage[]);
|
||||
stores.chatStore.saveMessages(chatId, messages as UIMessage[]);
|
||||
} catch (err) {
|
||||
console.error(`[chat ${chatId}] persist failed:`, err, { isAborted });
|
||||
}
|
||||
@@ -67,7 +67,7 @@ export async function handleChatRequest(req: Request, agent?: TeacherAgent): Pro
|
||||
// Skipped under tests: scripted chats are fixtures, not knowledge.
|
||||
if (process.env.NODE_ENV !== "test") {
|
||||
try {
|
||||
emitChat(chatId);
|
||||
emitChat(chatId, stores);
|
||||
} catch (err) {
|
||||
console.error(`[chat ${chatId}] graph emission failed:`, err);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user