2026-08-31 20:00:12 +03:00
|
|
|
/**
|
|
|
|
|
* 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.
|
|
|
|
|
*/
|
|
|
|
|
|
2026-08-22 02:18:27 +03:00
|
|
|
import * as fs from "node:fs";
|
|
|
|
|
import * as path from "node:path";
|
|
|
|
|
import { PATHS } from "../../config.ts";
|
2026-08-31 20:00:12 +03:00
|
|
|
import type { ChatStore } from "../core/ports.ts";
|
2026-09-12 06:02:40 +03:00
|
|
|
import type { ChatRecord, ChatSettings, ChatSummary, ExtractionRunUsage, PlanTree, TurnUsage } from "../core/contracts.ts";
|
2026-08-31 20:00:12 +03:00
|
|
|
import type { UIMessage } from "ai";
|
2026-09-01 18:24:13 +03:00
|
|
|
import { findPendingToolCall } from "../core/pending.ts";
|
2026-09-11 06:44:20 +03:00
|
|
|
import { phaseAt3 } from "../core/phases.ts";
|
2026-08-22 02:18:27 +03:00
|
|
|
|
|
|
|
|
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`);
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-31 20:00:12 +03:00
|
|
|
function createChat(title: string): ChatRecord {
|
2026-08-22 02:18:27 +03:00
|
|
|
const now = new Date().toISOString();
|
|
|
|
|
const record: ChatRecord = {
|
|
|
|
|
id: `chat_${Date.now().toString(36)}_${crypto.randomUUID().slice(0, 8)}`,
|
|
|
|
|
title: title.trim() || "Untitled session",
|
|
|
|
|
createdAt: now,
|
|
|
|
|
updatedAt: now,
|
|
|
|
|
messages: [],
|
|
|
|
|
};
|
|
|
|
|
fs.mkdirSync(PATHS.chats, { recursive: true });
|
|
|
|
|
fs.writeFileSync(chatFile(record.id), JSON.stringify(record, null, "\t"), "utf-8");
|
|
|
|
|
return record;
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-31 20:00:12 +03:00
|
|
|
function getChat(id: string): ChatRecord | null {
|
2026-08-22 02:18:27 +03:00
|
|
|
try {
|
|
|
|
|
return JSON.parse(fs.readFileSync(chatFile(id), "utf-8")) as ChatRecord;
|
|
|
|
|
} catch {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-31 20:00:12 +03:00
|
|
|
function listChats(): ChatSummary[] {
|
2026-08-22 02:18:27 +03:00
|
|
|
try {
|
|
|
|
|
fs.mkdirSync(PATHS.chats, { recursive: true });
|
|
|
|
|
return fs
|
|
|
|
|
.readdirSync(PATHS.chats)
|
|
|
|
|
.filter((f) => f.endsWith(".json"))
|
|
|
|
|
.map((f) => getChat(f.slice(0, -5)))
|
|
|
|
|
.filter((r): r is ChatRecord => r !== null)
|
2026-09-11 03:25:25 +03:00
|
|
|
.map(({ messages, tree, turns, extractions, ...summary }) => ({
|
|
|
|
|
// turns and extractions stay out of the list payload (list =
|
|
|
|
|
// summaries; usage aggregates ride the full record, the picker
|
|
|
|
|
// never needs them).
|
2026-09-01 18:24:13 +03:00
|
|
|
...summary,
|
|
|
|
|
messageCount: messages.length,
|
|
|
|
|
// Record-derived status for the session picker: phase from the
|
2026-09-11 06:44:20 +03:00
|
|
|
// transcript's explicit markers (probe_done / plan_init — the
|
|
|
|
|
// event-driven phases, docs/probe-teach-split.md §4), pending
|
|
|
|
|
// question from the last unanswered quiz/ask part. Liveness (busy)
|
|
|
|
|
// is NOT record-derived — the API route merges it (ChatListItem).
|
|
|
|
|
phase: phaseAt3(messages),
|
2026-09-01 18:24:13 +03:00
|
|
|
pendingQuestion: findPendingToolCall(messages) !== null,
|
|
|
|
|
}))
|
2026-08-22 02:18:27 +03:00
|
|
|
.sort((a, b) => b.updatedAt.localeCompare(a.updatedAt));
|
|
|
|
|
} catch {
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Persist the full message list (client state + new response) after a turn. */
|
2026-08-31 20:00:12 +03:00
|
|
|
function saveMessages(id: string, messages: UIMessage[]): void {
|
2026-08-22 02:18:27 +03:00
|
|
|
const record = getChat(id);
|
|
|
|
|
if (!record) throw new Error(`Chat not found: ${id}`);
|
|
|
|
|
record.messages = messages;
|
|
|
|
|
record.updatedAt = new Date().toISOString();
|
|
|
|
|
fs.writeFileSync(chatFile(id), JSON.stringify(record, null, "\t"), "utf-8");
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-11 02:43:35 +03:00
|
|
|
/** Append one usage entry to the record's turn log — idempotent per
|
2026-09-11 04:16:05 +03:00
|
|
|
* turn id: each REQUEST carries its own id, so a replay of the same turn's
|
|
|
|
|
* persist appends once, while continuation turns (which share a response
|
|
|
|
|
* message id) each keep their usage. */
|
2026-09-11 02:43:35 +03:00
|
|
|
function appendTurnUsage(id: string, entry: TurnUsage): void {
|
|
|
|
|
const record = getChat(id);
|
|
|
|
|
if (!record) throw new Error(`Chat not found: ${id}`);
|
2026-09-11 04:16:05 +03:00
|
|
|
record.turns = record.turns?.some((t) => t.id === entry.id)
|
2026-09-11 02:43:35 +03:00
|
|
|
? record.turns
|
|
|
|
|
: [...(record.turns ?? []), entry];
|
|
|
|
|
fs.writeFileSync(chatFile(id), JSON.stringify(record, null, "\t"), "utf-8");
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-11 03:25:25 +03:00
|
|
|
/** Append one extraction-run usage entry to the record's extractions
|
|
|
|
|
* log — idempotent per startedAt: a retry that reaches the append twice
|
|
|
|
|
* (abort replays) does not double-count spend. */
|
|
|
|
|
function appendExtractionUsage(id: string, entry: ExtractionRunUsage): void {
|
|
|
|
|
const record = getChat(id);
|
|
|
|
|
if (!record) throw new Error(`Chat not found: ${id}`);
|
|
|
|
|
record.extractions = record.extractions?.some((r) => r.startedAt === entry.startedAt)
|
|
|
|
|
? record.extractions
|
|
|
|
|
: [...(record.extractions ?? []), entry];
|
|
|
|
|
fs.writeFileSync(chatFile(id), JSON.stringify(record, null, "\t"), "utf-8");
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-12 06:02:40 +03:00
|
|
|
/** Persist the per-chat model/thinking settings — replace-whole (the
|
|
|
|
|
* picker sends the complete object; absent = config defaults). */
|
|
|
|
|
function saveSettings(id: string, settings: ChatSettings): void {
|
|
|
|
|
const record = getChat(id);
|
|
|
|
|
if (!record) throw new Error(`Chat not found: ${id}`);
|
|
|
|
|
// Like saveTree: a settings tweak is not conversation activity, so it
|
|
|
|
|
// deliberately does NOT bump updatedAt (sidebar order must not churn).
|
|
|
|
|
record.settings = settings;
|
|
|
|
|
fs.writeFileSync(chatFile(id), JSON.stringify(record, null, "\t"), "utf-8");
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-17 14:28:20 +03:00
|
|
|
/** Rename a session (the header's click-to-edit title, 2026-09-17). Like
|
|
|
|
|
* saveSettings, a rename is NOT conversation activity: updatedAt stays put
|
|
|
|
|
* so sidebar order never churns. Same fallback as createChat — the empty
|
|
|
|
|
* string becomes "Untitled session" (the API gate rejects it first; this
|
|
|
|
|
* is the honest floor for any other caller). */
|
|
|
|
|
function renameChat(id: string, title: string): ChatRecord {
|
|
|
|
|
const record = getChat(id);
|
|
|
|
|
if (!record) throw new Error(`Chat not found: ${id}`);
|
|
|
|
|
record.title = title.trim() || "Untitled session";
|
|
|
|
|
fs.writeFileSync(chatFile(id), JSON.stringify(record, null, "\t"), "utf-8");
|
|
|
|
|
return record;
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-23 02:41:04 +03:00
|
|
|
/** Delete the session record file entirely (session-deletion wave 6,
|
|
|
|
|
* owner 2026-09-22). False when the id is unknown — the route turns that
|
|
|
|
|
* into a 404. The record's graph traces and jobs are the route's
|
|
|
|
|
* choreography (core/graph-deletion.ts), not the store's. */
|
|
|
|
|
function deleteChat(id: string): boolean {
|
|
|
|
|
try {
|
|
|
|
|
fs.unlinkSync(chatFile(id));
|
|
|
|
|
return true;
|
|
|
|
|
} catch {
|
|
|
|
|
return false; // never existed, already deleted, or unreadable — same answer
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-22 09:06:24 +03:00
|
|
|
/** Persist the session plan tree (plan tools call this in execute). */
|
2026-08-31 20:00:12 +03:00
|
|
|
function saveTree(id: string, tree: PlanTree): void {
|
2026-08-22 09:06:24 +03:00
|
|
|
const record = getChat(id);
|
|
|
|
|
if (!record) throw new Error(`Chat not found: ${id}`);
|
|
|
|
|
record.tree = tree;
|
|
|
|
|
// Deliberately does NOT bump updatedAt: tree mutations piggyback on the
|
|
|
|
|
// turn's saveMessages, and sidebar ordering shouldn't churn on tree edits.
|
|
|
|
|
fs.writeFileSync(chatFile(id), JSON.stringify(record, null, "\t"), "utf-8");
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-31 20:00:12 +03:00
|
|
|
export const chatStore: ChatStore = {
|
|
|
|
|
createChat,
|
|
|
|
|
getChat,
|
|
|
|
|
listChats,
|
|
|
|
|
saveMessages,
|
|
|
|
|
saveTree,
|
2026-09-12 06:02:40 +03:00
|
|
|
saveSettings,
|
2026-09-17 14:28:20 +03:00
|
|
|
renameChat,
|
2026-09-23 02:41:04 +03:00
|
|
|
deleteChat,
|
2026-09-11 02:43:35 +03:00
|
|
|
appendTurnUsage,
|
2026-09-11 03:25:25 +03:00
|
|
|
appendExtractionUsage,
|
2026-08-31 20:00:12 +03:00
|
|
|
};
|