#!/usr/bin/env node 'use strict'; /** * Convert a local copy of the DragonKind bot's data into the public, anonymised format. * * node packages/ingest/bin/import-bot.js --bot /path/to/DragonKind [--out data/bot] [--tag 2026-11-01] * * Reads /data/knowledge.json, /data/runs.jsonl and /logs/encounters.jsonl. * Writes /scenes.json (merged with any existing catalogue) and /trajectories-.jsonl. * Set DK_ACCOUNT_SALT in the environment to hash account ids; never commit the salt. */ const fs = require('node:fs'); const path = require('node:path'); const { importBot, assertPublicSafe } = require('../src/bot'); const args = Object.fromEntries(process.argv.slice(2).reduce((acc, a, i, all) => { if (a.startsWith('--')) acc.push([a.slice(2), all[i + 1]]); return acc; }, [])); if (!args.bot) { console.error('usage: import-bot.js --bot /path/to/DragonKind [--out data/bot] [--tag YYYY-MM-DD]'); process.exit(2); } const out = path.resolve(args.out || 'data/bot'); const tag = args.tag || new Date().toISOString().slice(0, 10); const { scenes, trajectories, unlabeled } = importBot({ dataDir: path.join(args.bot, 'data'), logsDir: path.join(args.bot, 'logs') }); assertPublicSafe(scenes); assertPublicSafe(trajectories); fs.mkdirSync(out, { recursive: true }); const sceneFile = path.join(out, 'scenes.json'); const existing = fs.existsSync(sceneFile) ? JSON.parse(fs.readFileSync(sceneFile, 'utf8')) : {}; for (const [id, s] of Object.entries(scenes)) { const old = existing[id]; if (!old) { existing[id] = s; continue; } const ids = new Set(old.options.map((o) => o.id)); for (const o of s.options) if (!ids.has(o.id)) old.options.push(o); old.tint = old.tint || s.tint; old.backdrop = old.backdrop || s.backdrop; if (old.label.includes('·') && !s.label.includes('·')) old.label = s.label; } const sorted = Object.fromEntries(Object.entries(existing).sort(([a], [b]) => a.localeCompare(b))); fs.writeFileSync(sceneFile, `${JSON.stringify(sorted, null, 1)}\n`); const trajFile = path.join(out, `trajectories-${tag}.jsonl`); fs.writeFileSync(trajFile, trajectories.map((t) => JSON.stringify(t)).join('\n') + '\n'); const outcomes = trajectories.reduce((m, t) => ((m[t.outcome.type] = (m[t.outcome.type] || 0) + 1), m), {}); console.log(`${Object.keys(scenes).length} scenes, ${trajectories.length} attempts ${JSON.stringify(outcomes)}`); console.log(`wrote ${path.relative(process.cwd(), sceneFile)} and ${path.relative(process.cwd(), trajFile)}`); if (unlabeled.length) console.log(`scenes without a short label (add them to LABELS in packages/ingest/src/bot.js):\n ${unlabeled.join('\n ')}`);