#!/usr/bin/env node "use strict"; /** * cli.ts — OpenClaw → ZhiYi migration tool * * Usage: * node dist/cli.js migrate --lance-dir ~/.openclaw/memory-lancedb/memories.lance * node dist/cli.js status --lance-dir ~/.openclaw/memory-lancedb/memories.lance * node dist/cli.js test-zhiyi */ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); const path = __importStar(require("path")); const os = __importStar(require("os")); class ZhiYiClient { baseUrl; apiKey; constructor(baseUrl = "http://localhost:7821", apiKey = process.env.ZHIYI_API_KEY || "zhiyi-dev-key-2026") { this.baseUrl = baseUrl; this.apiKey = apiKey; } async commit(payload) { const res = await fetch(`${this.baseUrl}/api/v1/commit`, { method: "POST", headers: { "Content-Type": "application/json", "X-API-Key": this.apiKey, }, body: JSON.stringify(payload), }); if (!res.ok) { const body = await res.text(); throw new Error(`ZhiYi commit failed: ${res.status} ${body}`); } return res.json(); } async commitBatch(payloads) { const res = await fetch(`${this.baseUrl}/api/v1/commit/batch`, { method: "POST", headers: { "Content-Type": "application/json", "X-API-Key": this.apiKey, }, body: JSON.stringify(payloads), }); if (!res.ok) { const body = await res.text(); throw new Error(`ZhiYi batch commit failed: ${res.status} ${body}`); } return res.json(); } async getStats() { const res = await fetch(`${this.baseUrl}/api/v1/stats`, { headers: { "X-API-Key": this.apiKey }, }); if (!res.ok) throw new Error(`Stats failed: ${res.status}`); return res.json(); } async search(query, namespace = "openclaw-main", topK = 5) { const res = await fetch(`${this.baseUrl}/api/v1/recall?q=${encodeURIComponent(query)}&top_k=${topK}&namespace=${encodeURIComponent(namespace)}`, { headers: { "X-API-Key": this.apiKey } }); if (!res.ok) throw new Error(`Search failed: ${res.status}`); return res.json(); } } // ── LanceDB Reader ─────────────────────────────────────────────────────────── async function readMemoriesFromLanceDB(lanceDir) { // Dynamically load @lancedb/lancedb from memory-lancedb-pro's node_modules const memoryLancedbPath = path.join(os.homedir(), ".openclaw", "workspace", "plugins", "memory-lancedb", "node_modules", "@lancedb", "lancedb"); // eslint-disable-next-line @typescript-eslint/no-var-requires const lancedb = require(memoryLancedbPath); // LanceDB 0.26: use connect() factory, returns Connection const db = await lancedb.connect(lanceDir); const table = await db.openTable("memories"); const allEntries = []; const BATCH = 500; while (true) { // LanceDB 0.26 API: query().limit(n).toArray() returns Promise const batch = await table.query().limit(BATCH).toArray(); if (batch.length === 0) break; allEntries.push(...batch); if (batch.length < BATCH) break; console.error(` Read ${allEntries.length} entries...`); } return allEntries; } // ── Entity extraction ──────────────────────────────────────────────────────── function extractEntities(text) { const entities = []; const lower = text.toLowerCase(); // Capitalized multi-word phrases const capitalized = text.match(/[A-Z][a-z]+(?:\s+[A-Z][a-z]+)*/g) || []; entities.push(...capitalized.slice(0, 3)); // Quoted strings const quoted = text.match(/"([^"]+)"/g) || []; entities.push(...quoted.slice(0, 2).map((q) => q.replace(/"/g, ""))); // Known key topics const topics = [ "织忆", "MemoryWeave", "OpenClaw", "Hermes", "ComfyUI", "牧尘", "小唯", "小红书", "AI", "ChatGPT", "LLM", ]; for (const t of topics) { if (lower.includes(t.toLowerCase())) entities.push(t); } return [...new Set(entities)].slice(0, 5); } // ── Commands ───────────────────────────────────────────────────────────────── async function cmdMigrate(lanceDir, zhiyiUrl, apiKey) { const zhiyi = new ZhiYiClient(zhiyiUrl, apiKey); const resolved = lanceDir.replace("~", os.homedir()); console.error("Reading LanceDB..."); const entries = await readMemoriesFromLanceDB(resolved); console.error(`\nTotal entries found: ${entries.length}`); const valid = entries.filter((e) => e.id !== "__schema__" && e.text.trim().length > 0); console.error(`Valid entries: ${valid.length}`); const byCategory = {}; for (const e of valid) { byCategory[e.category] = (byCategory[e.category] || 0) + 1; } console.error("\nBy category:"); for (const [cat, count] of Object.entries(byCategory)) { console.error(` ${cat}: ${count}`); } const BATCH = 50; const committed = []; console.error(`\nCommitting to ZhiYi (openclaw-main)...`); for (let i = 0; i < valid.length; i += BATCH) { const batch = valid.slice(i, i + BATCH); const payloads = batch.map((entry) => ({ namespace: "openclaw-main", content: entry.text, entities: extractEntities(entry.text), source: `openclaw:${entry.category}`, commit_message: `migrated from openclaw:${entry.id} (scope=${entry.scope})`, })); try { const result = await zhiyi.commitBatch(payloads); committed.push(...result.ids); console.error(` Batch ${Math.floor(i / BATCH) + 1}: committed ${batch.length}`); } catch (err) { console.error(` Batch ${Math.floor(i / BATCH) + 1}: FAILED — ${err}`); console.error(" Retrying one by one..."); for (const p of payloads) { try { const r = await zhiyi.commit(p); committed.push(r.id); } catch (e) { console.error(` Failed: ${p.content.slice(0, 60)}... — ${e}`); } } } } console.error(`\nDone: ${committed.length}/${valid.length} committed`); return { total: valid.length, committed: committed.length }; } async function cmdStatus(lanceDir) { const resolved = lanceDir.replace("~", os.homedir()); console.error("Reading LanceDB..."); const entries = await readMemoriesFromLanceDB(resolved); const valid = entries.filter((e) => e.id !== "__schema__" && e.text.trim().length > 0); const byCategory = {}; const byScope = {}; const importanceBuckets = { high: 0, mid: 0, low: 0 }; for (const e of valid) { byCategory[e.category] = (byCategory[e.category] || 0) + 1; byScope[e.scope] = (byScope[e.scope] || 0) + 1; if (e.importance >= 7) importanceBuckets.high++; else if (e.importance >= 4) importanceBuckets.mid++; else importanceBuckets.low++; } console.log(JSON.stringify({ total_entries: entries.length, valid_entries: valid.length, by_category: byCategory, by_scope: byScope, importance_distribution: importanceBuckets, sample_entries: valid.slice(0, 3).map((e) => ({ id: e.id, category: e.category, scope: e.scope, text_preview: e.text.slice(0, 120) + (e.text.length > 120 ? "..." : ""), })), }, null, 2)); } async function cmdTestZhiyi(zhiyiUrl, apiKey) { const zhiyi = new ZhiYiClient(zhiyiUrl, apiKey); console.error("Testing ZhiYi connection..."); try { const stats = await zhiyi.getStats(); console.log(JSON.stringify({ ok: true, stats })); } catch (err) { console.error(`Connection failed: ${err}`); process.exit(1); } } // ── CLI ─────────────────────────────────────────────────────────────────────── const [cmd, ...args] = process.argv.slice(2); if (cmd === "migrate") { const lanceDir = args[0] || `${os.homedir()}/.openclaw/memory-lancedb/memories.lance`; cmdMigrate(lanceDir, process.env.ZHIYI_URL, process.env.ZHIYI_API_KEY).catch((err) => { console.error(`\nMigration failed: ${err}`); process.exit(1); }); } else if (cmd === "status") { const lanceDir = args[0] || `${os.homedir()}/.openclaw/memory-lancedb/memories.lance`; cmdStatus(lanceDir).catch((err) => { console.error(`Status failed: ${err}`); process.exit(1); }); } else if (cmd === "test-zhiyi") { cmdTestZhiyi(process.env.ZHIYI_URL, process.env.ZHIYI_API_KEY); } else { console.error(`Usage: node dist/cli.js migrate [--lance-dir PATH] # LanceDB → ZhiYi node dist/cli.js status [--lance-dir PATH] # LanceDB stats node dist/cli.js test-zhiyi # test ZhiYi connection Env: ZHIYI_URL, ZHIYI_API_KEY`); process.exit(1); }