#!/usr/bin/env node 'use strict'; // KB 版本管理:查看备份 / 回滚到 kb_docs_v。 // 用法: // node scripts/kb-version.js list // node scripts/kb-version.js rollback kb_docs_v20261023032922 // 需要 RAG_PG_URL 指向 KB 所在库(宿主机通常是 127.0.0.1:5440/)。 const fs = require('fs'); const path = require('path'); const { Pool } = require('../bridge/node_modules/pg'); const { listVersions, rollback } = require('../bridge/src/kb-version'); const ROOT = path.resolve(__dirname, '..'); const env = {}; for (const line of fs.readFileSync(path.join(ROOT, '.env'), 'utf8').split('\n')) { if (!line.includes('=') || line.trim().startsWith('#')) continue; const i = line.indexOf('='); env[line.slice(0, i).trim()] = line.slice(i + 1).trim(); } const url = process.env.RAG_PG_URL || env.RAG_PG_URL; if (!url) throw new Error('RAG_PG_URL is required'); (async () => { const pool = new Pool({ connectionString: url, max: 1 }); try { const cmd = process.argv[2]; if (cmd === 'list') { const out = await listVersions(pool); console.log(JSON.stringify(out, null, 2)); } else if (cmd === 'rollback') { const target = process.argv[3]; if (!target) throw new Error('usage: node scripts/kb-version.js rollback '); console.log(JSON.stringify(await rollback(pool, target), null, 2)); } else { console.log('usage: node scripts/kb-version.js list | rollback '); } } finally { await pool.end(); } })().catch((err) => { console.error('[kb-version]', err.message); process.exit(1); });