// Build data/public/graph.json from every source: community form runs + DragonKind bot trajectories. // node analysis/network/build-graph.js const fs = require("fs"); const path = require("path"); const G = require("../../core/graph.js"); const root = path.resolve(__dirname, "../.."); const rel = (p) => path.join(root, p); /** The coded runs written by the Python coding rules (python -m atlas.coding -> data/derived/runs_clean.csv). */ function readCSV(file) { const text = fs.readFileSync(file, "utf8"), rows = []; let row = [], cell = "", q = false; for (let i = 0; i < text.length; i++) { const ch = text[i]; if (q) { if (ch === '"' && text[i + 1] === '"') { cell += '"'; i++; } else if (ch === '"') q = false; else cell += ch; } else if (ch === '"') q = true; else if (ch === ",") { row.push(cell); cell = ""; } else if (ch === "\n") { row.push(cell); rows.push(row); row = []; cell = ""; } else if (ch !== "\r") cell += ch; } if (cell || row.length) { row.push(cell); rows.push(row); } const [head, ...body] = rows; return body.map((r) => Object.fromEntries(head.map((h, i) => [h, r[i] === "" || r[i] === undefined ? null : r[i]]))); } const runs = readCSV(rel("data/derived/runs_clean.csv")); const records = runs.map((r) => ({ source: r.source === "Form" ? "form" : "form-bulk", vars: G.formRunVariables(r) })); const botDir = rel("data/bot"); const scenes = fs.existsSync(path.join(botDir, "scenes.json")) ? JSON.parse(fs.readFileSync(path.join(botDir, "scenes.json"), "utf8")) : {}; const trajs = []; for (const f of fs.readdirSync(botDir).filter((f) => /^trajectories-.*\.jsonl$/.test(f)).sort()) { for (const line of fs.readFileSync(path.join(botDir, f), "utf8").split("\n")) if (line.trim()) trajs.push(JSON.parse(line)); } const unmapped = {}; for (const t of trajs) { const { vars, unmapped: u } = G.trajectoryVariables(t); records.push({ source: "bot", vars }); for (const x of u) unmapped[x] = (unmapped[x] || 0) + 1; } const graph = { sources: records.reduce((m, r) => ((m[r.source] = (m[r.source] || 0) + 1), m), {}), variables: G.buildVariableGraph(records), scenes: G.buildSceneGraph(scenes, trajs) }; fs.writeFileSync(rel("data/public/graph.json"), JSON.stringify(graph)); console.log(`graph.json: ${JSON.stringify(graph.sources)}, ${Object.keys(graph.variables.nodes).length} variable nodes, ${Object.keys(graph.scenes).length} scenes`); const um = Object.keys(unmapped).filter((t) => !/^(head deeper|investigate the scream|lower your|show your bravery|hide behind|run and hope|reds are too|jump|daggers or not|engage the smaller)/i.test(t)); if (um.length) console.log("bot choices with no form equivalent:", um);