import { accountRequiredMessage } from '../lib/desktop-mode.js'; import { Command } from 'commander'; import { resolve } from 'path'; import { AuthStore, type AuthProfile } from '../lib/auth.js'; import { getProfileEventLedgerPath, getProfileInputJournalPath, getProfileRuntimeStateDir, getProfileStateHistoryPath, getWorkspaceDir, } from '../lib/init-command.js'; import { ensureUserDataLayout, getUserDataDir } from '../lib/user-data.js'; import { readGameplayConfig } from '../lib/gameplay-config.js'; import { readDiagnosticsConfig } from '../lib/diagnostics-config.js'; import { observabilityLogPath } from '../runtime/observability/index.js'; import { ownerRawWsLogPath } from '../runtime/raw-ws-log.js'; import { exportRuntimeData, type RuntimeDataExportOptions, type RuntimeDataExportResult, } from '../runtime/clawclaw/runtime-data-export.js'; export interface DataCommandDependencies { readonly activeProfile?: () => AuthProfile | null; readonly exportRuntimeData?: (options: RuntimeDataExportOptions) => Promise; readonly cwd?: () => string; readonly now?: () => Date; } function exportLabel(gameId: string | undefined, now: Date): string { const scope = gameId ? `game-${gameId.replace(/[^A-Za-z0-9._-]+/g, '-').slice(0, 80)}` : 'all'; const timestamp = now.toISOString().replace(/[:.]/g, '-'); return `clawclaw-runtime-export-${scope}-${timestamp}`; } export function createDataCommand(dependencies: DataCommandDependencies = {}): Command { const cmd = new Command('data') .description('Inspect or export local ClawClaw data'); cmd.command('path') .description('Print the user-data directory for manual migration') .action(() => { const workspaceDir = getWorkspaceDir(); ensureUserDataLayout(workspaceDir); readGameplayConfig(workspaceDir); readDiagnosticsConfig(workspaceDir); console.log(JSON.stringify({ path: getUserDataDir(workspaceDir), contains_secrets: true, message: 'Zip this folder to move ClawClaw user data to another terminal. It contains account keys, persona, memory, gameplay and diagnostics configuration, Behavior, Perception, and Trigger modules.', }, null, 2)); }); cmd.command('export') .description('Export Runtime databases and diagnostics as JSONL') .option('--game-id ', 'export one game segment by backend game_id') .option('--all', 'export all Runtime data for the active account') .option('-o, --output ', 'new output directory') .action(async (options: { gameId?: string; all?: boolean; output?: string }) => { const gameId = options.gameId?.trim(); if (Boolean(gameId) === Boolean(options.all)) { throw new Error('Choose exactly one export scope: --game-id or --all.'); } const profile = (dependencies.activeProfile ?? (() => new AuthStore().getActive()))(); if (!profile) throw new Error(accountRequiredMessage('Not logged in. Run: ccl account register')); const now = (dependencies.now ?? (() => new Date()))(); const output = resolve( (dependencies.cwd ?? (() => process.cwd()))(), options.output ?? exportLabel(gameId, now), ); const runtimeStateDir = getProfileRuntimeStateDir(profile); const result = await (dependencies.exportRuntimeData ?? exportRuntimeData)({ selection: gameId ? { kind: 'game', gameId } : { kind: 'all' }, outputDir: output, paths: { eventLedger: getProfileEventLedgerPath(profile), stateHistory: getProfileStateHistoryPath(profile), inputJournal: getProfileInputJournalPath(profile), observability: observabilityLogPath(runtimeStateDir), rawWs: ownerRawWsLogPath(runtimeStateDir), }, now: () => now, }); console.log(JSON.stringify({ path: result.path, selection: result.selection, session_ids: result.sessionIds, segment_ids: result.segmentIds, row_counts: result.rowCounts, diagnostic_count: result.diagnosticCount, detail_capture_count: result.detailCaptureCount ?? 0, raw_ws_frame_count: result.rawWsFrameCount, }, null, 2)); }); return cmd; }