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:
@@ -1,8 +1,8 @@
|
||||
import { isToolUIPart, type LanguageModel, type UIMessage } from "ai";
|
||||
import type { DemoResult, DemoToolInput, JobKind, ResearchResult, ResearchToolInput, VisualResult, VisualToolInput } from "../../core/contracts.ts";
|
||||
import { JOB_TOOLS } from "../../core/pending.ts";
|
||||
import { getChat, saveMessages } from "../../store/chats.ts";
|
||||
import { createJob, findJobByToolCallId, getJob, updateJob } from "../../store/jobs.ts";
|
||||
import { chatStore } from "../../store/chats.ts";
|
||||
import { jobStore } from "../../store/jobs.ts";
|
||||
import { runDemoBuilder } from "./demo-builder.ts";
|
||||
import { createResearcherAgent } from "./researcher.ts";
|
||||
import { runVisualMaker } from "./visual-maker.ts";
|
||||
@@ -60,22 +60,22 @@ export function spawnPendingJobs(chatId: string, messages: UIMessage[]): void {
|
||||
if (!(JOB_TOOLS as readonly string[]).includes(tool)) continue;
|
||||
const runner = KIND_RUNNERS[tool as JobKind];
|
||||
if (!runner) continue;
|
||||
if (findJobByToolCallId(chatId, part.toolCallId)) continue; // already has a job
|
||||
const job = createJob({ kind: tool as JobKind, chatId, toolCallId: part.toolCallId });
|
||||
if (jobStore.findJobByToolCallId(chatId, part.toolCallId)) continue; // already has a job
|
||||
const job = jobStore.createJob({ kind: tool as JobKind, chatId, toolCallId: part.toolCallId });
|
||||
void runner(job.id).catch((err) => {
|
||||
console.error(`[job ${job.id}] ${tool} runner crashed:`, err);
|
||||
updateJob(job.id, { status: "error", error: String(err), activity: "failed" });
|
||||
jobStore.updateJob(job.id, { status: "error", error: String(err), activity: "failed" });
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
export async function runResearchJob(jobId: string, opts: RunResearchJobOptions = {}): Promise<void> {
|
||||
const job = getJob(jobId);
|
||||
const job = jobStore.getJob(jobId);
|
||||
if (!job || job.status !== "running") return;
|
||||
|
||||
const chat = getChat(job.chatId);
|
||||
const chat = chatStore.getChat(job.chatId);
|
||||
if (!chat) {
|
||||
updateJob(jobId, { status: "error", error: `Chat not found: ${job.chatId}`, activity: "failed" });
|
||||
jobStore.updateJob(jobId, { status: "error", error: `Chat not found: ${job.chatId}`, activity: "failed" });
|
||||
return;
|
||||
}
|
||||
const input = findToolPart(chat.messages, job.toolCallId)?.input as ResearchToolInput | undefined;
|
||||
@@ -94,7 +94,7 @@ export async function runResearchJob(jobId: string, opts: RunResearchJobOptions
|
||||
const agent = createResearcherAgent(opts.model);
|
||||
|
||||
try {
|
||||
updateJob(jobId, { activity: "researching" });
|
||||
jobStore.updateJob(jobId, { activity: "researching" });
|
||||
const result = await agent.generate({
|
||||
prompt,
|
||||
onToolExecutionStart: ({ toolCall }) => {
|
||||
@@ -102,7 +102,7 @@ export async function runResearchJob(jobId: string, opts: RunResearchJobOptions
|
||||
if (toolCall.toolName === "web_fetch" && typeof input?.url === "string") {
|
||||
fetchedUrls.push(input.url);
|
||||
}
|
||||
updateJob(jobId, { activity: activityLine(toolCall.toolName, input) });
|
||||
jobStore.updateJob(jobId, { activity: activityLine(toolCall.toolName, input) });
|
||||
},
|
||||
});
|
||||
|
||||
@@ -111,11 +111,11 @@ export async function runResearchJob(jobId: string, opts: RunResearchJobOptions
|
||||
const research: ResearchResult = { brief, sources: parseSources(brief, fetchedUrls) };
|
||||
|
||||
fulfillPart(job.chatId, job.toolCallId, research);
|
||||
updateJob(jobId, { status: "done", result: research }); // activity line kept: the last tool call is the record
|
||||
jobStore.updateJob(jobId, { status: "done", result: research }); // activity line kept: the last tool call is the record
|
||||
} catch (err) {
|
||||
const reason = err instanceof Error ? err.message : String(err);
|
||||
fulfillPart(job.chatId, job.toolCallId, { brief: `Research failed: ${reason}`, sources: [] });
|
||||
updateJob(jobId, { status: "error", activity: "failed", error: reason });
|
||||
jobStore.updateJob(jobId, { status: "error", activity: "failed", error: reason });
|
||||
}
|
||||
}
|
||||
|
||||
@@ -135,12 +135,12 @@ function isVisualFailure(result: VisualResult): boolean {
|
||||
}
|
||||
|
||||
export async function runDemoJob(jobId: string, opts: RunDemoJobOptions = {}): Promise<void> {
|
||||
const job = getJob(jobId);
|
||||
const job = jobStore.getJob(jobId);
|
||||
if (!job || job.status !== "running") return;
|
||||
|
||||
const chat = getChat(job.chatId);
|
||||
const chat = chatStore.getChat(job.chatId);
|
||||
if (!chat) {
|
||||
updateJob(jobId, { status: "error", error: `Chat not found: ${job.chatId}`, activity: "failed" });
|
||||
jobStore.updateJob(jobId, { status: "error", error: `Chat not found: ${job.chatId}`, activity: "failed" });
|
||||
return;
|
||||
}
|
||||
const input = findToolPart(chat.messages, job.toolCallId)?.input as DemoToolInput | undefined;
|
||||
@@ -148,25 +148,25 @@ export async function runDemoJob(jobId: string, opts: RunDemoJobOptions = {}): P
|
||||
const brief = typeof input?.brief === "string" ? input.brief : "";
|
||||
|
||||
try {
|
||||
updateJob(jobId, { activity: "building demo" });
|
||||
jobStore.updateJob(jobId, { activity: "building demo" });
|
||||
const result = await runDemoBuilder(
|
||||
{ title, brief },
|
||||
{
|
||||
model: opts.model,
|
||||
onActivity: (line) => updateJob(jobId, { activity: line }),
|
||||
onActivity: (line) => jobStore.updateJob(jobId, { activity: line }),
|
||||
},
|
||||
);
|
||||
fulfillPart(job.chatId, job.toolCallId, result);
|
||||
if (isDemoFailure(result)) {
|
||||
updateJob(jobId, { status: "error", error: result.notes ?? "demo build failed", activity: "failed" });
|
||||
jobStore.updateJob(jobId, { status: "error", error: result.notes ?? "demo build failed", activity: "failed" });
|
||||
} else {
|
||||
updateJob(jobId, { status: "done", result });
|
||||
jobStore.updateJob(jobId, { status: "done", result });
|
||||
}
|
||||
} catch (err) {
|
||||
const reason = err instanceof Error ? err.message : String(err);
|
||||
const failure: DemoResult = { title, html: "", tested: true, notes: `FAILED: ${reason}` };
|
||||
fulfillPart(job.chatId, job.toolCallId, failure);
|
||||
updateJob(jobId, { status: "error", error: reason, activity: "failed" });
|
||||
jobStore.updateJob(jobId, { status: "error", error: reason, activity: "failed" });
|
||||
}
|
||||
}
|
||||
|
||||
@@ -175,37 +175,37 @@ export interface RunVisualJobOptions {
|
||||
}
|
||||
|
||||
export async function runVisualJob(jobId: string, opts: RunVisualJobOptions = {}): Promise<void> {
|
||||
const job = getJob(jobId);
|
||||
const job = jobStore.getJob(jobId);
|
||||
if (!job || job.status !== "running") return;
|
||||
|
||||
const chat = getChat(job.chatId);
|
||||
const chat = chatStore.getChat(job.chatId);
|
||||
if (!chat) {
|
||||
updateJob(jobId, { status: "error", error: `Chat not found: ${job.chatId}`, activity: "failed" });
|
||||
jobStore.updateJob(jobId, { status: "error", error: `Chat not found: ${job.chatId}`, activity: "failed" });
|
||||
return;
|
||||
}
|
||||
const input = findToolPart(chat.messages, job.toolCallId)?.input as VisualToolInput | undefined;
|
||||
const brief = typeof input?.brief === "string" ? input.brief : "";
|
||||
|
||||
try {
|
||||
updateJob(jobId, { activity: "drawing figure" });
|
||||
jobStore.updateJob(jobId, { activity: "drawing figure" });
|
||||
const result = await runVisualMaker(
|
||||
{ brief },
|
||||
{
|
||||
model: opts.model,
|
||||
onActivity: (line) => updateJob(jobId, { activity: line }),
|
||||
onActivity: (line) => jobStore.updateJob(jobId, { activity: line }),
|
||||
},
|
||||
);
|
||||
fulfillPart(job.chatId, job.toolCallId, result);
|
||||
if (isVisualFailure(result)) {
|
||||
updateJob(jobId, { status: "error", error: result.notes ?? "visual build failed", activity: "failed" });
|
||||
jobStore.updateJob(jobId, { status: "error", error: result.notes ?? "visual build failed", activity: "failed" });
|
||||
} else {
|
||||
updateJob(jobId, { status: "done", result });
|
||||
jobStore.updateJob(jobId, { status: "done", result });
|
||||
}
|
||||
} catch (err) {
|
||||
const reason = err instanceof Error ? err.message : String(err);
|
||||
const failure: VisualResult = { svg: "", notes: `FAILED: ${reason}` };
|
||||
fulfillPart(job.chatId, job.toolCallId, failure);
|
||||
updateJob(jobId, { status: "error", error: reason, activity: "failed" });
|
||||
jobStore.updateJob(jobId, { status: "error", error: reason, activity: "failed" });
|
||||
}
|
||||
}
|
||||
|
||||
@@ -241,7 +241,7 @@ function hostOf(url: string | undefined): string {
|
||||
* Synchronous read-modify-write (no awaits) — atomic within the event loop.
|
||||
*/
|
||||
function fulfillPart(chatId: string, toolCallId: string, output: ResearchResult | DemoResult | VisualResult): void {
|
||||
const chat = getChat(chatId);
|
||||
const chat = chatStore.getChat(chatId);
|
||||
if (!chat) return;
|
||||
let found = false;
|
||||
const messages = chat.messages.map((m) =>
|
||||
@@ -260,7 +260,7 @@ function fulfillPart(chatId: string, toolCallId: string, output: ResearchResult
|
||||
);
|
||||
if (!found) return;
|
||||
try {
|
||||
saveMessages(chatId, messages);
|
||||
chatStore.saveMessages(chatId, messages);
|
||||
} catch (err) {
|
||||
console.error(`[chat ${chatId}] fulfilling job part ${toolCallId} failed:`, err);
|
||||
}
|
||||
|
||||
@@ -13,7 +13,7 @@ import {
|
||||
setCurrent,
|
||||
updateNode,
|
||||
} from "../../core/plan-tree.ts";
|
||||
import { getChat, saveTree } from "../../store/chats.ts";
|
||||
import { chatStore } from "../../store/chats.ts";
|
||||
import type { TeacherToolContext } from "../tools.ts";
|
||||
|
||||
/**
|
||||
@@ -52,12 +52,12 @@ async function runPlanOp(
|
||||
chatId: string,
|
||||
op: (tree: PlanTree) => PlanTree,
|
||||
): Promise<PlanToolResult> {
|
||||
const record = getChat(chatId);
|
||||
const record = chatStore.getChat(chatId);
|
||||
if (!record) return { ok: false, error: `Chat not found: ${chatId}`, tree: emptyTree() };
|
||||
const current = record.tree ?? emptyTree();
|
||||
try {
|
||||
const next = op(current);
|
||||
saveTree(chatId, next);
|
||||
chatStore.saveTree(chatId, next);
|
||||
return { ok: true, tree: next };
|
||||
} catch (err) {
|
||||
if (err instanceof PlanTreeError) return { ok: false, error: err.message, tree: current };
|
||||
@@ -81,7 +81,7 @@ export function planTools(ctx: TeacherToolContext): ToolSet {
|
||||
.describe("Initial `requires` edges between the nodes. Edge direction: from = prerequisite, to = dependent."),
|
||||
}),
|
||||
execute: async ({ nodes, edges }): Promise<PlanToolResult> => {
|
||||
const record = getChat(ctx.chatId);
|
||||
const record = chatStore.getChat(ctx.chatId);
|
||||
if (!record) return { ok: false, error: `Chat not found: ${ctx.chatId}`, tree: emptyTree() };
|
||||
const existing = record.tree;
|
||||
if (existing && existing.nodes.length > 0) {
|
||||
|
||||
+26
-2
@@ -1,7 +1,7 @@
|
||||
/**
|
||||
* Shared contracts — the single source of truth every component codes against.
|
||||
* Pure types + protocol docs; no runtime code, type-only imports (safe
|
||||
* everywhere: server, client, tests). See docs/vision.md (§ refs).
|
||||
* Pure types + protocol docs + tiny pure helpers; type-only imports of "ai"
|
||||
* (safe everywhere: server, client, tests). See docs/vision.md (§ refs).
|
||||
*/
|
||||
|
||||
import type { UIMessage } from "ai";
|
||||
@@ -83,6 +83,23 @@ export interface GraphEdge {
|
||||
provenance: string[];
|
||||
}
|
||||
|
||||
/** mappings.jsonl line — the naive-union identity map: a chat's tree node →
|
||||
* the graph node it folded into (the merge audit trail; v1 never merges two
|
||||
* sessions' tree nodes — W8 exact-title match aside). */
|
||||
export interface NodeMapping {
|
||||
chatId: string;
|
||||
treeNodeId: string;
|
||||
graphNodeId: string;
|
||||
}
|
||||
|
||||
/** sessions.jsonl line — the emission checkpoint for one chat (which chats
|
||||
* have been folded, and through which updatedAt — never fold twice). */
|
||||
export interface SessionCheckpoint {
|
||||
chatId: string;
|
||||
/** The chat record's `updatedAt` at the time of the fold. */
|
||||
foldedAt: string;
|
||||
}
|
||||
|
||||
export type LedgerEventKind =
|
||||
| "probe-correct"
|
||||
| "probe-miss"
|
||||
@@ -136,6 +153,13 @@ export interface GraphSnapshot {
|
||||
nodeSessions: Record<string, string[]>;
|
||||
}
|
||||
|
||||
/** Derive a short chat title from the learner's first message (pure).
|
||||
* Used on chat creation; deterministic so tests can pin it. */
|
||||
export function titleFromPrompt(prompt: string): string {
|
||||
const oneLine = prompt.replace(/\s+/g, " ").trim();
|
||||
return oneLine.length > 72 ? `${oneLine.slice(0, 72)}…` : oneLine;
|
||||
}
|
||||
|
||||
// ─── Chat records (the session store's on-disk format) ───────────────────────
|
||||
|
||||
/** One persisted session: data/chats/<id>.json. The stored messages are full
|
||||
|
||||
@@ -22,7 +22,7 @@
|
||||
*/
|
||||
|
||||
import type { GraphEdge, GraphNode } from "./contracts.ts";
|
||||
import { deriveNodeStates, getGraph, type DerivedGraph } from "./graph.ts";
|
||||
import { deriveNodeStates, type DerivedGraph } from "./graph.ts";
|
||||
import type { NodeStateClass } from "./contracts.ts";
|
||||
|
||||
/** The section header the teacher is taught to recognize (50-graph-context.md). */
|
||||
@@ -59,7 +59,7 @@ function isHotspot(node: GraphNode, state: NodeStateClass): boolean {
|
||||
* Render the context block, or null when there is nothing worth injecting
|
||||
* (no nodes; or a truncated graph whose selection came out empty).
|
||||
*
|
||||
* Pure: callers hand in exactly what getGraph() + deriveNodeStates() produce.
|
||||
* Pure: callers hand in exactly what getGraph(graphStore) + deriveNodeStates() produce.
|
||||
*/
|
||||
export function renderGraphContext(
|
||||
nodes: GraphNode[],
|
||||
@@ -162,11 +162,11 @@ function edgeChains(edges: GraphEdge[]): string[][] {
|
||||
// ─── the per-request entry point ─────────────────────────────────────────────
|
||||
|
||||
/**
|
||||
* Build the graph context block for one request. Stateless — re-reads the
|
||||
* ledgers every call, so it self-updates as the graph grows mid-session.
|
||||
* Accepts an injected derived graph (tests); defaults to the live one.
|
||||
* Build the graph context block for one request. Pure: the caller hands in
|
||||
* the derived graph (production: getGraph(graphStore) per request in the
|
||||
* chat-handler, so it self-updates as the graph grows mid-session).
|
||||
*/
|
||||
export function buildGraphContext(graph: DerivedGraph = getGraph()): string | null {
|
||||
export function buildGraphContext(graph: DerivedGraph): string | null {
|
||||
const states = deriveNodeStates(graph.nodes, graph.events);
|
||||
return renderGraphContext(graph.nodes, graph.edges, states);
|
||||
}
|
||||
|
||||
+32
-24
@@ -16,10 +16,12 @@
|
||||
* Idempotent + incremental: mappings dedupe nodes, (from,to,type) dedupes
|
||||
* edge assertions, chat-scoped refs dedupe events, and sessions.jsonl
|
||||
* checkpoints which chats have been folded. Re-emitting a chat appends
|
||||
* nothing. The store (src/store/graph.ts) holds the append-only ledgers;
|
||||
* everything here derives from them.
|
||||
* nothing. Every function takes the storage ports (core/ports.ts) as
|
||||
* explicit parameters — pure core, no adapter imports; the composition
|
||||
* root (src/server/stores.ts) wires the fs adapters in.
|
||||
*/
|
||||
|
||||
import { randomBytes } from "node:crypto";
|
||||
import { isToolUIPart, type UIMessage } from "ai";
|
||||
import type {
|
||||
GraphEdge,
|
||||
@@ -32,13 +34,18 @@ import type {
|
||||
PlanToolResult,
|
||||
} from "./contracts.ts";
|
||||
import type { QuestionAnswerData } from "./question-core.ts";
|
||||
import { getChat, listChats } from "../store/chats.ts";
|
||||
import { graphLedgers, newGraphNodeId } from "../store/graph.ts";
|
||||
import type { ChatStore, GraphStore } from "./ports.ts";
|
||||
|
||||
/** The ledger surface emitChat needs (the real one is src/store/graph.ts).
|
||||
* Injectable so tests can fold fixtures into an in-memory ledger instead of
|
||||
* the live data/graph files. */
|
||||
export type GraphLedgers = typeof graphLedgers;
|
||||
/** The storage ports every graph operation needs, passed explicitly. */
|
||||
export interface GraphDeps {
|
||||
chatStore: ChatStore;
|
||||
graphStore: GraphStore;
|
||||
}
|
||||
|
||||
/** "n_" + 12 hex chars (contracts.ts GraphNode.id — store-assigned). */
|
||||
function newGraphNodeId(): string {
|
||||
return `n_${randomBytes(6).toString("hex")}`;
|
||||
}
|
||||
|
||||
// ─── transcript scanning ─────────────────────────────────────────────────────
|
||||
|
||||
@@ -109,11 +116,12 @@ export interface EmissionStats {
|
||||
* turn (chat-handler onFinish) and again on catch-up (GET /api/graph). Sync
|
||||
* and fast: a handful of small-file reads plus one append per new record.
|
||||
*
|
||||
* `ledgers` is injectable for tests (in-memory ledger); production always
|
||||
* uses the live store.
|
||||
* The storage ports are explicit (tests pass in-memory ledgers; production
|
||||
* passes the fs adapters from src/server/stores.ts).
|
||||
*/
|
||||
export function emitChat(chatId: string, ledgers: GraphLedgers = graphLedgers): EmissionStats {
|
||||
const record = getChat(chatId);
|
||||
export function emitChat(chatId: string, deps: GraphDeps): EmissionStats {
|
||||
const { graphStore: ledgers } = deps;
|
||||
const record = deps.chatStore.getChat(chatId);
|
||||
if (!record) return { nodes: 0, edges: 0, events: 0 };
|
||||
const stats: EmissionStats = { nodes: 0, edges: 0, events: 0 };
|
||||
|
||||
@@ -297,11 +305,11 @@ export interface DerivedGraph {
|
||||
* edges are merged by (from,to,type) with unioned provenance, first-seen
|
||||
* order. `store events, derive state` — this is the only reader that matters.
|
||||
*/
|
||||
export function getGraph(): DerivedGraph {
|
||||
const nodes = graphLedgers.readNodes();
|
||||
export function getGraph(graphStore: GraphStore): DerivedGraph {
|
||||
const nodes = graphStore.readNodes();
|
||||
const merged: GraphEdge[] = [];
|
||||
const byKey = new Map<string, GraphEdge>();
|
||||
for (const line of graphLedgers.readEdges()) {
|
||||
for (const line of graphStore.readEdges()) {
|
||||
const key = `${line.from}\u0000${line.to}\u0000${line.type}`;
|
||||
const existing = byKey.get(key);
|
||||
if (existing) {
|
||||
@@ -314,7 +322,7 @@ export function getGraph(): DerivedGraph {
|
||||
merged.push(edge);
|
||||
}
|
||||
}
|
||||
return { nodes, edges: merged, events: graphLedgers.readEvents() };
|
||||
return { nodes, edges: merged, events: graphStore.readEvents() };
|
||||
}
|
||||
|
||||
const QUIZ_EVENT_KINDS: ReadonlySet<string> = new Set([
|
||||
@@ -366,8 +374,8 @@ export function deriveNodeStates(nodes: GraphNode[], events: LedgerEvent[]): Rec
|
||||
// ─── the /api/graph payload ──────────────────────────────────────────────────
|
||||
|
||||
/** Everything the home graph view needs, in one fetch (shape: contracts). */
|
||||
export function graphSnapshot(): GraphSnapshot {
|
||||
const { nodes, edges, events } = getGraph();
|
||||
export function graphSnapshot(deps: GraphDeps): GraphSnapshot {
|
||||
const { nodes, edges, events } = getGraph(deps.graphStore);
|
||||
const nodeStates = deriveNodeStates(nodes, events);
|
||||
|
||||
const nodeChats = new Map<string, Set<string>>();
|
||||
@@ -379,7 +387,7 @@ export function graphSnapshot(): GraphSnapshot {
|
||||
}
|
||||
chats.add(chatId);
|
||||
};
|
||||
for (const m of graphLedgers.readMappings()) touch(m.graphNodeId, m.chatId);
|
||||
for (const m of deps.graphStore.readMappings()) touch(m.graphNodeId, m.chatId);
|
||||
for (const edge of edges) {
|
||||
for (const chatId of edge.provenance) {
|
||||
touch(edge.from, chatId);
|
||||
@@ -397,7 +405,7 @@ export function graphSnapshot(): GraphSnapshot {
|
||||
}
|
||||
const sessions: Record<string, string> = {};
|
||||
for (const id of chatIds) {
|
||||
const chat = getChat(id);
|
||||
const chat = deps.chatStore.getChat(id);
|
||||
if (chat) sessions[id] = chat.title;
|
||||
}
|
||||
|
||||
@@ -417,9 +425,9 @@ export function graphSnapshot(): GraphSnapshot {
|
||||
* sessions deposit value — vision §1 "frequent updates"). Called at the top
|
||||
* of GET /api/graph; the chat-handler onFinish hook covers the live path.
|
||||
*/
|
||||
export function foldStaleChats(): number {
|
||||
export function foldStaleChats(deps: GraphDeps): number {
|
||||
const checkpoints = new Map<string, string>();
|
||||
for (const checkpoint of graphLedgers.readCheckpoints()) {
|
||||
for (const checkpoint of deps.graphStore.readCheckpoints()) {
|
||||
const previous = checkpoints.get(checkpoint.chatId);
|
||||
if (previous === undefined || checkpoint.foldedAt > previous) {
|
||||
checkpoints.set(checkpoint.chatId, checkpoint.foldedAt);
|
||||
@@ -427,11 +435,11 @@ export function foldStaleChats(): number {
|
||||
}
|
||||
|
||||
let folded = 0;
|
||||
for (const chat of listChats()) {
|
||||
for (const chat of deps.chatStore.listChats()) {
|
||||
const foldedAt = checkpoints.get(chat.id);
|
||||
if (foldedAt !== undefined && chat.updatedAt <= foldedAt) continue;
|
||||
try {
|
||||
emitChat(chat.id);
|
||||
emitChat(chat.id, deps);
|
||||
folded++;
|
||||
} catch (err) {
|
||||
console.error(`[graph] fold failed for chat ${chat.id}:`, err);
|
||||
|
||||
+7
-4
@@ -9,13 +9,16 @@
|
||||
* components/** → "@/src/core" (this barrel, ONLY)
|
||||
* app/** → "@/src/core" + "@/src/server"
|
||||
*
|
||||
* IO-bound modules (core/graph.ts, core/graph-context.ts read the ledgers
|
||||
* via the store) stay internal to the server side until the storage-port
|
||||
* stage inverts their dependency; they are deliberately NOT re-exported
|
||||
* here.
|
||||
* Server-only core modules (core/graph.ts emission/derivation,
|
||||
* core/graph-context.ts) are pure but need storage ports passed in by a
|
||||
* server caller, so they are deliberately NOT re-exported here — the
|
||||
* server imports them directly. The storage PORT TYPES are types-only and
|
||||
* browser-safe, hence re-exported.
|
||||
*/
|
||||
|
||||
export * from "./contracts.ts";
|
||||
export * from "./ports.ts";
|
||||
|
||||
export * from "./question-core.ts";
|
||||
export * from "./plan-tree.ts";
|
||||
export * from "./phases.ts";
|
||||
|
||||
@@ -0,0 +1,95 @@
|
||||
/**
|
||||
* Storage ports (the hexagonal seams — vision §1 "storage stays swappable").
|
||||
*
|
||||
* One port per ENTITY, with domain operations — never one generic
|
||||
* key/value interface. Core and the service layer that orbits it code
|
||||
* against these types only; concrete adapters live in src/store/ (fs
|
||||
* JSON/JSONL today, SQLite drop-in later) and are wired exactly once, in
|
||||
* the composition root (src/server/stores.ts). Dependencies point one way:
|
||||
* adapters may import core; core NEVER imports an adapter.
|
||||
*/
|
||||
|
||||
import type { UIMessage } from "ai";
|
||||
import type {
|
||||
ChatRecord,
|
||||
ChatSummary,
|
||||
GraphEdge,
|
||||
GraphNode,
|
||||
JobKind,
|
||||
JobRecord,
|
||||
LedgerEvent,
|
||||
NodeMapping,
|
||||
PlanTree,
|
||||
SessionCheckpoint,
|
||||
} from "./contracts.ts";
|
||||
|
||||
// ─── ChatStore — the session record's home (data/chats) ─────────────────────
|
||||
|
||||
export interface ChatStore {
|
||||
/** Create a session record (persisted immediately). */
|
||||
createChat(title: string): ChatRecord;
|
||||
/** The full record, or null if unknown id / corrupt file. */
|
||||
getChat(id: string): ChatRecord | null;
|
||||
/** All sessions, newest activity first (the sidebar's list view). */
|
||||
listChats(): ChatSummary[];
|
||||
/** Persist the full message list (client state + new response) after a turn. */
|
||||
saveMessages(id: string, messages: UIMessage[]): void;
|
||||
/** Persist the session plan tree (plan tools call this in execute). */
|
||||
saveTree(id: string, tree: PlanTree): void;
|
||||
}
|
||||
|
||||
// ─── GraphStore — the append-only evidence ledgers (data/graph) ──────────────
|
||||
// v1 surface = the five JSONL ledgers (nodes / edges / events / mappings /
|
||||
// sessions). Reads return the raw append-order lines; merging edge assertions
|
||||
// by (from, to, type) is DERIVED in core/graph.ts — the store never decides.
|
||||
// The extraction pipeline (next stage) becomes the sole writer; this read
|
||||
// surface stays.
|
||||
|
||||
export interface GraphStore {
|
||||
/** Append one GraphNode (ids are unique — no merging needed). */
|
||||
appendNode(node: GraphNode): void;
|
||||
/** Raw node lines in append order. */
|
||||
readNodes(): GraphNode[];
|
||||
|
||||
/** Append one edge ASSERTION (a second session asserting the same
|
||||
* (from, to, type) appends another line; readers merge). */
|
||||
appendEdge(assertion: GraphEdge): void;
|
||||
readEdges(): GraphEdge[];
|
||||
|
||||
/** Append one quiz outcome / taught event. */
|
||||
appendEvent(event: LedgerEvent): void;
|
||||
readEvents(): LedgerEvent[];
|
||||
|
||||
/** Append one identity-map line (chat's tree node → graph node). */
|
||||
appendMapping(mapping: NodeMapping): void;
|
||||
readMappings(): NodeMapping[];
|
||||
|
||||
/** Append one emission checkpoint. */
|
||||
appendCheckpoint(checkpoint: SessionCheckpoint): void;
|
||||
readCheckpoints(): SessionCheckpoint[];
|
||||
}
|
||||
|
||||
// ─── JobStore — background sub-agent jobs (data/jobs) ─────────────────────────
|
||||
|
||||
export interface CreateJobInit {
|
||||
kind: JobKind;
|
||||
chatId: string;
|
||||
toolCallId: string;
|
||||
/** First activity line (defaults to "starting"). */
|
||||
activity?: string;
|
||||
}
|
||||
|
||||
/** Mergeable fields — identity (id, kind, chatId, toolCallId, createdAt) is immutable. */
|
||||
export type JobPatch = Partial<Omit<JobRecord, "id" | "kind" | "chatId" | "toolCallId" | "createdAt">>;
|
||||
|
||||
export interface JobStore {
|
||||
/** Create a running job (keyed by toolCallId for spawn dedup). */
|
||||
createJob(init: CreateJobInit): JobRecord;
|
||||
getJob(id: string): JobRecord | null;
|
||||
/** Merge a patch into the record; null if unknown id. */
|
||||
updateJob(id: string, patch: JobPatch): JobRecord | null;
|
||||
/** All jobs for a chat, newest activity first. */
|
||||
listJobs(chatId: string): JobRecord[];
|
||||
/** The job fulfilling a given tool part, if any (spawn-hook dedup). */
|
||||
findJobByToolCallId(chatId: string, toolCallId: string): JobRecord | null;
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
+5
-3
@@ -1,9 +1,11 @@
|
||||
import { createChat, getChat, listChats, titleFromPrompt } from "../store/chats.ts";
|
||||
import { stores } from "./stores.ts";
|
||||
import { titleFromPrompt } from "../core/contracts.ts";
|
||||
|
||||
/**
|
||||
* Server-side chat entries for the transport layer (app/api/chats*,
|
||||
* app/chat/[id]). Today these delegate straight to the fs store; the
|
||||
* app/chat/[id]). Today these delegate straight to the ChatStore port; the
|
||||
* services stage carves the real orchestration behind this same surface.
|
||||
*/
|
||||
|
||||
export { createChat, getChat, listChats, titleFromPrompt };
|
||||
export const { createChat, getChat, listChats } = stores.chatStore;
|
||||
export { titleFromPrompt };
|
||||
|
||||
+3
-2
@@ -1,5 +1,6 @@
|
||||
import type { GraphSnapshot } from "../core/contracts.ts";
|
||||
import { foldStaleChats, graphSnapshot } from "../core/graph.ts";
|
||||
import { stores } from "./stores.ts";
|
||||
|
||||
/**
|
||||
* Server-side graph entry for the home view (GET /api/graph): fold any chat
|
||||
@@ -9,6 +10,6 @@ import { foldStaleChats, graphSnapshot } from "../core/graph.ts";
|
||||
* same read surface.
|
||||
*/
|
||||
export function graphView(): GraphSnapshot {
|
||||
foldStaleChats();
|
||||
return graphSnapshot();
|
||||
foldStaleChats(stores);
|
||||
return graphSnapshot(stores);
|
||||
}
|
||||
|
||||
@@ -8,3 +8,4 @@ export * from "./chat-handler.ts";
|
||||
export * from "./chats.ts";
|
||||
export * from "./graph.ts";
|
||||
export * from "./jobs.ts";
|
||||
export * from "./stores.ts";
|
||||
|
||||
+2
-2
@@ -1,4 +1,4 @@
|
||||
import { listJobs } from "../store/jobs.ts";
|
||||
import { stores } from "./stores.ts";
|
||||
|
||||
/**
|
||||
* Server-side job-status entry (GET /api/jobs?chatId=…, contracts.ts
|
||||
@@ -7,4 +7,4 @@ import { listJobs } from "../store/jobs.ts";
|
||||
* re-syncs the chat when the job flips to done/error.
|
||||
*/
|
||||
|
||||
export { listJobs };
|
||||
export const { listJobs } = stores.jobStore;
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
/**
|
||||
* THE COMPOSITION ROOT for storage — the single place concrete store
|
||||
* adapters are wired. Everything else (core, services, agents) codes against
|
||||
* the port types in core/ports.ts and receives instances through this
|
||||
* module. Swapping the fs adapter for SQLite later changes exactly this
|
||||
* file (and the adapter implementations in src/store/).
|
||||
*/
|
||||
|
||||
import { chatStore } from "../store/chats.ts";
|
||||
import { graphStore } from "../store/graph.ts";
|
||||
import { jobStore } from "../store/jobs.ts";
|
||||
/** The wired adapters. Satisfies core/graph.ts's GraphDeps structurally. */
|
||||
export const stores = {
|
||||
chatStore,
|
||||
graphStore,
|
||||
jobStore,
|
||||
};
|
||||
|
||||
export type Stores = typeof stores;
|
||||
+23
-17
@@ -1,21 +1,25 @@
|
||||
/**
|
||||
* ChatStore — the fs JSON adapter (one file per chat in data/chats/).
|
||||
*
|
||||
* Implements core/ports.ts ChatStore; the record shapes live in
|
||||
* core/contracts.ts (ChatRecord/ChatSummary). Idioms: sync fs, tiny
|
||||
* records, id-guarded filenames, JSON with tab indentation. Wired in the
|
||||
* composition root (src/server/stores.ts); nothing else constructs it.
|
||||
*/
|
||||
|
||||
import * as fs from "node:fs";
|
||||
import * as path from "node:path";
|
||||
import type { UIMessage } from "ai";
|
||||
import { PATHS } from "../../config.ts";
|
||||
import type { ChatStore } from "../core/ports.ts";
|
||||
import type { ChatRecord, ChatSummary, PlanTree } from "../core/contracts.ts";
|
||||
|
||||
/**
|
||||
* Chat store — one JSON file per chat in data/chats/. The record shape lives
|
||||
* in core/contracts (ChatRecord/ChatSummary); this module is the fs adapter's
|
||||
* implementation (the storage port formalizes this in a later stage).
|
||||
*/
|
||||
import type { UIMessage } from "ai";
|
||||
|
||||
function chatFile(id: string): string {
|
||||
if (!/^[A-Za-z0-9_-]+$/.test(id)) throw new Error(`Invalid chat id: ${id}`);
|
||||
return path.join(PATHS.chats, `${id}.json`);
|
||||
}
|
||||
|
||||
export function createChat(title: string): ChatRecord {
|
||||
function createChat(title: string): ChatRecord {
|
||||
const now = new Date().toISOString();
|
||||
const record: ChatRecord = {
|
||||
id: `chat_${Date.now().toString(36)}_${crypto.randomUUID().slice(0, 8)}`,
|
||||
@@ -29,7 +33,7 @@ export function createChat(title: string): ChatRecord {
|
||||
return record;
|
||||
}
|
||||
|
||||
export function getChat(id: string): ChatRecord | null {
|
||||
function getChat(id: string): ChatRecord | null {
|
||||
try {
|
||||
return JSON.parse(fs.readFileSync(chatFile(id), "utf-8")) as ChatRecord;
|
||||
} catch {
|
||||
@@ -37,7 +41,7 @@ export function getChat(id: string): ChatRecord | null {
|
||||
}
|
||||
}
|
||||
|
||||
export function listChats(): ChatSummary[] {
|
||||
function listChats(): ChatSummary[] {
|
||||
try {
|
||||
fs.mkdirSync(PATHS.chats, { recursive: true });
|
||||
return fs
|
||||
@@ -53,7 +57,7 @@ export function listChats(): ChatSummary[] {
|
||||
}
|
||||
|
||||
/** Persist the full message list (client state + new response) after a turn. */
|
||||
export function saveMessages(id: string, messages: UIMessage[]): void {
|
||||
function saveMessages(id: string, messages: UIMessage[]): void {
|
||||
const record = getChat(id);
|
||||
if (!record) throw new Error(`Chat not found: ${id}`);
|
||||
record.messages = messages;
|
||||
@@ -62,7 +66,7 @@ export function saveMessages(id: string, messages: UIMessage[]): void {
|
||||
}
|
||||
|
||||
/** Persist the session plan tree (plan tools call this in execute). */
|
||||
export function saveTree(id: string, tree: PlanTree): void {
|
||||
function saveTree(id: string, tree: PlanTree): void {
|
||||
const record = getChat(id);
|
||||
if (!record) throw new Error(`Chat not found: ${id}`);
|
||||
record.tree = tree;
|
||||
@@ -71,8 +75,10 @@ export function saveTree(id: string, tree: PlanTree): void {
|
||||
fs.writeFileSync(chatFile(id), JSON.stringify(record, null, "\t"), "utf-8");
|
||||
}
|
||||
|
||||
/** Derive a short title from the first user message. */
|
||||
export function titleFromPrompt(prompt: string): string {
|
||||
const oneLine = prompt.replace(/\s+/g, " ").trim();
|
||||
return oneLine.length > 72 ? `${oneLine.slice(0, 72)}…` : oneLine;
|
||||
}
|
||||
export const chatStore: ChatStore = {
|
||||
createChat,
|
||||
getChat,
|
||||
listChats,
|
||||
saveMessages,
|
||||
saveTree,
|
||||
};
|
||||
|
||||
+21
-35
@@ -1,9 +1,8 @@
|
||||
/**
|
||||
* Graph ledger store (W4; vision §1, contracts.ts "v1 emission").
|
||||
*
|
||||
* Five append-only JSONL ledgers at config.LEDGER_FILES:
|
||||
* GraphStore — the fs JSONL adapter: five append-only ledgers at
|
||||
* config.LEDGER_FILES (contracts.ts "v1 emission").
|
||||
* nodes.jsonl GraphNode — one line per graph node
|
||||
* edges.jsonl GraphEdge — one line per edge ASSERTION (a second
|
||||
* edges.jsonl GraphEdge — one line per edge ASSERTION (a second
|
||||
* session asserting the same (from,to,type)
|
||||
* appends another line; reads merge)
|
||||
* events.jsonl LedgerEvent — quiz outcomes + taught events
|
||||
@@ -11,38 +10,25 @@
|
||||
* (chatId, treeNodeId) → graphNodeId
|
||||
* sessions.jsonl SessionCheckpoint— emission checkpoints (foldedAt)
|
||||
*
|
||||
* data/graph/* is exclusively this store's (the legacy V0 scaffold lives in
|
||||
* data/legacy-graph). Reads are strict against the contracts.ts shapes:
|
||||
* corrupt lines are skipped, never guessed at — the ledgers are the source of
|
||||
* truth and everything else is derived (src/core/graph.ts).
|
||||
*
|
||||
* Same idioms as src/store/chats.ts: sync fs, tiny records, no framework.
|
||||
* Implements core/ports.ts GraphStore. data/graph/* is exclusively this
|
||||
* adapter's. Reads are strict against the contracts.ts shapes: corrupt lines
|
||||
* are skipped, never guessed at — the ledgers are the source of truth and
|
||||
* everything else is derived (core/graph.ts, which takes this port as a
|
||||
* parameter). Same idioms as src/store/chats.ts: sync fs, no framework.
|
||||
*/
|
||||
|
||||
import * as fs from "node:fs";
|
||||
import * as path from "node:path";
|
||||
import { randomBytes } from "node:crypto";
|
||||
import { LEDGER_FILES } from "../../config.ts";
|
||||
import type { GraphEdge, GraphNode, LedgerEvent, LedgerEventKind } from "../core/contracts.ts";
|
||||
|
||||
/** The naive-union identity map line: a chat's tree node → graph node. */
|
||||
export interface NodeMapping {
|
||||
chatId: string;
|
||||
treeNodeId: string;
|
||||
graphNodeId: string;
|
||||
}
|
||||
|
||||
/** sessions.jsonl line — the emission checkpoint for one chat. */
|
||||
export interface SessionCheckpoint {
|
||||
chatId: string;
|
||||
/** The chat record's `updatedAt` at the time of the fold. */
|
||||
foldedAt: string;
|
||||
}
|
||||
|
||||
/** "n_" + nanoid-like (contracts.ts GraphNode.id). */
|
||||
export function newGraphNodeId(): string {
|
||||
return `n_${randomBytes(6).toString("hex")}`;
|
||||
}
|
||||
import type {
|
||||
GraphEdge,
|
||||
GraphNode,
|
||||
LedgerEvent,
|
||||
LedgerEventKind,
|
||||
NodeMapping,
|
||||
SessionCheckpoint,
|
||||
} from "../core/contracts.ts";
|
||||
import type { GraphStore } from "../core/ports.ts";
|
||||
|
||||
// ─── JSONL primitives ────────────────────────────────────────────────────────
|
||||
|
||||
@@ -124,9 +110,9 @@ const isNodeMapping = (v: unknown): v is NodeMapping =>
|
||||
const isCheckpoint = (v: unknown): v is SessionCheckpoint =>
|
||||
isRecord(v) && isString(v.chatId) && isString(v.foldedAt);
|
||||
|
||||
// ─── typed ledger access ─────────────────────────────────────────────────────
|
||||
// ─── the port implementation ────────────────────────────────────────────────
|
||||
|
||||
export const graphLedgers = {
|
||||
export const graphStore: GraphStore = {
|
||||
appendNode(node: GraphNode): void {
|
||||
appendLine(LEDGER_FILES.nodes, node);
|
||||
},
|
||||
@@ -136,8 +122,8 @@ export const graphLedgers = {
|
||||
},
|
||||
|
||||
/** Append one edge ASSERTION; readers merge lines by (from,to,type). */
|
||||
appendEdge(edge: GraphEdge): void {
|
||||
appendLine(LEDGER_FILES.edges, edge);
|
||||
appendEdge(assertion: GraphEdge): void {
|
||||
appendLine(LEDGER_FILES.edges, assertion);
|
||||
},
|
||||
readEdges(): GraphEdge[] {
|
||||
return readValid(LEDGER_FILES.edges, isGraphEdge);
|
||||
|
||||
+26
-28
@@ -1,18 +1,19 @@
|
||||
/**
|
||||
* JobStore — the fs JSON adapter: one file per background sub-agent job in
|
||||
* data/jobs/ (contracts.ts "THE ASYNC PROTOCOL"). Keyed by toolCallId for
|
||||
* dedup: the chat-handler spawn hook never creates two jobs for the same
|
||||
* tool part. Implements core/ports.ts JobStore. Idioms: sync fs, id-guarded
|
||||
* filenames, JSON with tab indentation. Writes are small and rare (activity
|
||||
* line updates); the read-modify-write sections in the runner contain no
|
||||
* awaits, so they are atomic within the single-threaded event loop.
|
||||
*/
|
||||
|
||||
import * as fs from "node:fs";
|
||||
import * as path from "node:path";
|
||||
import { PATHS } from "../../config.ts";
|
||||
import type { JobKind, JobRecord } from "../core/contracts.ts";
|
||||
|
||||
/**
|
||||
* Job store — one JSON file per background sub-agent job in data/jobs/
|
||||
* (contracts.ts "THE ASYNC PROTOCOL"). Keyed by toolCallId for dedup: the
|
||||
* chat-handler spawn hook never creates two jobs for the same tool part.
|
||||
*
|
||||
* Follows src/store/chats.ts idioms: sync fs, id-guarded filenames, JSON
|
||||
* with tab indentation. Writes are small and rare (activity line updates);
|
||||
* the read-modify-write sections in the runner contain no awaits, so they
|
||||
* are atomic within the single-threaded event loop.
|
||||
*/
|
||||
import type { JobStore } from "../core/ports.ts";
|
||||
import type { CreateJobInit, JobPatch } from "../core/ports.ts";
|
||||
import type { JobRecord } from "../core/contracts.ts";
|
||||
|
||||
/** Background jobs (data/jobs). Not in config.ts PATHS (orchestrator-owned). */
|
||||
const JOBS_DIR = path.join(PATHS.data, "jobs");
|
||||
@@ -22,15 +23,7 @@ function jobFile(id: string): string {
|
||||
return path.join(JOBS_DIR, `${id}.json`);
|
||||
}
|
||||
|
||||
export interface CreateJobInit {
|
||||
kind: JobKind;
|
||||
chatId: string;
|
||||
toolCallId: string;
|
||||
/** First activity line (defaults to "starting"). */
|
||||
activity?: string;
|
||||
}
|
||||
|
||||
export function createJob(init: CreateJobInit): JobRecord {
|
||||
function createJob(init: CreateJobInit): JobRecord {
|
||||
const now = new Date().toISOString();
|
||||
const record: JobRecord = {
|
||||
id: `job_${Date.now().toString(36)}_${crypto.randomUUID().slice(0, 8)}`,
|
||||
@@ -47,7 +40,7 @@ export function createJob(init: CreateJobInit): JobRecord {
|
||||
return record;
|
||||
}
|
||||
|
||||
export function getJob(id: string): JobRecord | null {
|
||||
function getJob(id: string): JobRecord | null {
|
||||
try {
|
||||
return JSON.parse(fs.readFileSync(jobFile(id), "utf-8")) as JobRecord;
|
||||
} catch {
|
||||
@@ -56,10 +49,7 @@ export function getJob(id: string): JobRecord | null {
|
||||
}
|
||||
|
||||
/** Merge a partial patch into the record (identity fields are immutable). */
|
||||
export function updateJob(
|
||||
id: string,
|
||||
patch: Partial<Omit<JobRecord, "id" | "kind" | "chatId" | "toolCallId" | "createdAt">>,
|
||||
): JobRecord | null {
|
||||
function updateJob(id: string, patch: JobPatch): JobRecord | null {
|
||||
const record = getJob(id);
|
||||
if (!record) return null;
|
||||
const next: JobRecord = { ...record, ...patch, updatedAt: new Date().toISOString() };
|
||||
@@ -68,7 +58,7 @@ export function updateJob(
|
||||
}
|
||||
|
||||
/** All jobs for a chat, newest activity first. */
|
||||
export function listJobs(chatId: string): JobRecord[] {
|
||||
function listJobs(chatId: string): JobRecord[] {
|
||||
try {
|
||||
fs.mkdirSync(JOBS_DIR, { recursive: true });
|
||||
return fs
|
||||
@@ -83,6 +73,14 @@ export function listJobs(chatId: string): JobRecord[] {
|
||||
}
|
||||
|
||||
/** The job fulfilling a given tool part, if any (spawn-hook dedup). */
|
||||
export function findJobByToolCallId(chatId: string, toolCallId: string): JobRecord | null {
|
||||
function findJobByToolCallId(chatId: string, toolCallId: string): JobRecord | null {
|
||||
return listJobs(chatId).find((j) => j.toolCallId === toolCallId) ?? null;
|
||||
}
|
||||
|
||||
export const jobStore: JobStore = {
|
||||
createJob,
|
||||
getJob,
|
||||
updateJob,
|
||||
listJobs,
|
||||
findJobByToolCallId,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user