2026-09-08 22:41:57 +03:00
|
|
|
/**
|
|
|
|
|
* One-time backfill: wipe the legacy ledgers and re-extract every chat with
|
|
|
|
|
* the redesigned pipeline (vision §1, commit b07c907). Run from the master
|
|
|
|
|
* tree only — data never crosses trees:
|
|
|
|
|
*
|
|
|
|
|
* npx tsx --env-file=.env.local scripts/backfill-graph.ts
|
|
|
|
|
*/
|
|
|
|
|
import { chatStore } from "../src/store/chats.ts";
|
|
|
|
|
import { graphStore } from "../src/store/graph.ts";
|
|
|
|
|
import { createExtractionPipeline } from "../src/agent/subagents/extraction.ts";
|
|
|
|
|
import { getGraph } from "../src/core/graph.ts";
|
2026-09-24 15:12:24 +03:00
|
|
|
import { effectiveSettings } from "../src/server/stores.ts";
|
2026-09-08 22:41:57 +03:00
|
|
|
|
|
|
|
|
async function main() {
|
|
|
|
|
const only = process.argv[2]; // optional: extract just one chat id
|
|
|
|
|
const chats = only ? chatStore.listChats().filter((c) => c.id === only) : chatStore.listChats();
|
|
|
|
|
console.log(`[backfill] ${chats.length} chats to extract`);
|
2026-09-24 15:12:24 +03:00
|
|
|
const pipeline = createExtractionPipeline({
|
|
|
|
|
chatStore,
|
|
|
|
|
graphStore,
|
|
|
|
|
// Live budget: the graph section's setting, same as the app's trigger
|
|
|
|
|
getChunkTokens: () => effectiveSettings().graph.extractionChunkTokens,
|
|
|
|
|
});
|
2026-09-08 22:41:57 +03:00
|
|
|
for (const chat of chats) {
|
|
|
|
|
const t0 = Date.now();
|
|
|
|
|
try {
|
|
|
|
|
const stats = await pipeline.runExtraction(chat.id);
|
|
|
|
|
console.log(
|
|
|
|
|
`[backfill] ${chat.id} (${chat.title.slice(0, 40)}): ` +
|
2026-09-28 16:54:44 +03:00
|
|
|
`${stats.nodes} nodes, ${stats.edges} edges in ${((Date.now() - t0) / 1000).toFixed(1)}s`,
|
2026-09-08 22:41:57 +03:00
|
|
|
);
|
|
|
|
|
} catch (err) {
|
|
|
|
|
console.error(`[backfill] ${chat.id} FAILED:`, err);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
const graph = getGraph(graphStore);
|
|
|
|
|
console.log(
|
2026-09-28 16:54:44 +03:00
|
|
|
`[backfill] done: ${graph.nodes.length} nodes, ${graph.edges.length} edges`,
|
2026-09-08 22:41:57 +03:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
main();
|