2026-08-31 19:51:17 +03:00
|
|
|
import type { GraphSnapshot } from "../core/contracts.ts";
|
2026-09-19 20:55:35 +03:00
|
|
|
import type { GraphCostSummary } from "../core/cost-summary.ts";
|
|
|
|
|
import { graphCostSummary } from "../core/cost-summary.ts";
|
2026-08-31 22:36:41 +03:00
|
|
|
import { graphSnapshot } from "../core/graph.ts";
|
2026-08-31 20:00:12 +03:00
|
|
|
import { stores } from "./stores.ts";
|
2026-08-31 19:51:17 +03:00
|
|
|
|
|
|
|
|
/**
|
2026-08-31 22:36:41 +03:00
|
|
|
* Server-side graph entry for the home view (GET /api/graph). A PURE READ:
|
|
|
|
|
* the extraction pipeline (the sole graph writer) is triggered by quiz
|
|
|
|
|
* answers in sessions (turn-service), never from here — the home view
|
|
|
|
|
* reflects whatever the pipeline has merged so far.
|
2026-08-31 19:51:17 +03:00
|
|
|
*/
|
|
|
|
|
export function graphView(): GraphSnapshot {
|
2026-08-31 20:00:12 +03:00
|
|
|
return graphSnapshot(stores);
|
2026-08-31 19:51:17 +03:00
|
|
|
}
|
2026-09-19 20:55:35 +03:00
|
|
|
|
|
|
|
|
/** The graph view's cost surface (GET /api/graph/costs — the 2026-09 cost
|
|
|
|
|
* pill): fold every usage channel into stage totals + recent runs. A pure
|
|
|
|
|
* read over the chat records (chat-front-door runs), the notes sources
|
|
|
|
|
* (notes-front-door runs), and the naming ledger (graph-global runs);
|
|
|
|
|
* dollars are inferred by the pure core fold (core/cost-summary.ts +
|
|
|
|
|
* prices.ts) — nothing here stores or caches money. */
|
|
|
|
|
export function graphCosts(): GraphCostSummary {
|
|
|
|
|
const chats = stores.chatStore
|
|
|
|
|
.listChats()
|
|
|
|
|
.flatMap((summary) => {
|
|
|
|
|
const record = stores.chatStore.getChat(summary.id);
|
|
|
|
|
return record?.extractions?.length
|
|
|
|
|
? [{ title: record.title ?? summary.id, extractions: record.extractions }]
|
|
|
|
|
: [];
|
|
|
|
|
});
|
|
|
|
|
const sources = stores.graphStore
|
|
|
|
|
.readSources()
|
|
|
|
|
.filter((s) => s.extractions?.length)
|
|
|
|
|
.map((s) => ({ filePath: s.filePath, extractions: s.extractions! }));
|
|
|
|
|
return graphCostSummary({ chats, sources, naming: stores.graphStore.readNamingRuns() });
|
|
|
|
|
}
|