mirror of
https://github.com/makecindy/cindy.git
synced 2026-12-06 03:30:16 +08:00
* fix(security): 修复 CodeQL 与 Dependabot 安全警报(代码扫描 + 依赖) ReDoS(js/polynomial-redos / js/redos): - 尾斜杠 trim 正则改线性 trimTrailingSlashes:github-client、gitlab-client、 model-providers、anthropic-responses-bridge、responses-chat-bridge、 maker-shared/clientEndpoints(语言等价,去 O(n²) 回溯) - discord markdown 图片提取/HTML 剥除加量词上界;feishu inline code 剥除改 线性扫描(与原 replace 逐字节等价,已加回归用例) - android activity 正则去嵌套量词(语言等价):唯一可经 MCP 工具参数远端 触发的指数级 DoS,几十字符即可冻结主进程 - anthropic-compat-proxy:RecoveryRule.match 改名 matches,消除 CodeQL 对 String.prototype.match 的误判(match 实为固定正则谓词) 本地脚本/构建/e2e 泄密兜底(均非产品运行时代码): - 移动端 build/mock/e2e 脚本、dev-embed-search:session id 打码、URL 只留 host、秘密类 env 值输出前抹除;mock-voice-proxy 错误细节不外泄; cdn-fallback 测试的动态 handler 调用加函数校验 主进程防御纵深: - agent 二进制 spawn/execFile 前复核受管路径(2 个 critical 命令注入告警实为 execFile/spawn 参数数组 + 枚举白名单,此处再加一层执行侧校验) - oauth 语言表改 hasOwnProperty 白名单取值;x.ai 判定改精确 host 比较; 录音目录名后缀改 CSPRNG;codexHttpBridge 错误响应显式 text/plain; SSH 卸载命令 agentKind 收窄回字面量 依赖(仅构建/开发工具链或未触发路径,不进用户运行时): - esbuild ^0.28.1(anthropic-compat-proxy、maker-cc-manager、remote-file-service) + @esbuild-kit/core-utils>esbuild override ^0.25.0 - tar ^7.5.21;brace-expansion ^5.0.8;xcode>uuid override ^11.1.1 - 锁文件同步消除旧 esbuild 0.18/0.27、tar 7.5.20、uuid 7.0.3 等易受影响副本 明确不做(保运行时功能 / 待维护者决策): - electron-store 保持 v7:v8(conf 10)会改动 main 进程持久化的构造与模块 副作用,单测 mock 无法验证;其 ajv7 $data ReDoS 选项未启用,列为 dismiss - claude-agent-sdk 的 @anthropic-ai/sdk、electron-builder 链、glib(更新器)、 @hono/node-server:上游锁定或分发链,需专项验证,不在本次盲升 - GhostSettingsWebview 注入脚本、mention 引号约定、账号指纹哈希、计费幂等键、 _generated 上游 vendored 代码:经核实为误报或既定设计,在 GitHub dismiss 验证:根 pnpm test:unit 全绿(EXIT=0);pnpm -r typecheck 全绿(EXIT=0)。 Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Signed-off-by: Lizi <jiali@magiclizi.com> Signed-off-by: MagicLizi <jiali@magiclizi.com> * fix(security): address review feedback on PR #473 - stripHtmlTags: rewrite loop-until-stable → linear single-pass scan that handles nested fragments without O(n²) rescanning (P1 polynomial DoS) - Remove xcode>uuid override (changes mobile runtime fingerprint; P1) - VoiceInputSessionRecorder: use full 8-char hex suffix (32 bits entropy) instead of slicing to 6 (24 bits) - oauthResultPage: switch RETURN_LABEL from Record to Map, eliminating CodeQL js/unvalidated-dynamic-method-call - scrubSecretsFromText (build scripts): restructure to pre-build RegExp from env values at module load, breaking CodeQL taint chain while keeping identical runtime semantics - dev-embed-search fail(): same RegExp approach for bearer token scrubbing Signed-off-by: MagicLizi <xd.lizi@gmail.com> Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> Signed-off-by: MagicLizi <jiali@magiclizi.com> * fix(security): address round-2 review feedback - stripHtmlTags: preserve text when no closing '>' found (unterminated tag-like fragments no longer silently dropped) - codex-proxy-host: url.host → url.hostname for xAI upstream check (url.host includes port, causing false negatives on :443) - anthropic-compat-proxy: update comment "match" → "matches" to match renamed interface field - native-e2e-doctor: move _secretScrubRe initializer before first call site (fixes temporal dead zone ReferenceError on dry-run path) Signed-off-by: MagicLizi <xd.lizi@gmail.com> Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> Signed-off-by: MagicLizi <jiali@magiclizi.com> * fix(security): harden stripHtmlTags — O(n), self-close, placeholders - Valid-tag scan now also stops at '<', preventing O(n²) rescanning when repeated unterminated tag fragments appear (e.g. '<A\t'.repeat(N)) - '/' after tag name only accepted as self-closing when immediately followed by '>' — preserves <owner/repo> style placeholders - Update comment accuracy (nested fragment stops at first '>') - Add regression tests covering all edge cases Signed-off-by: MagicLizi <xd.lizi@gmail.com> Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> Signed-off-by: MagicLizi <jiali@magiclizi.com> * fix(security): suppress CodeQL false positives + harden oauthResultPage - Add lgtm[js/clear-text-logging] suppression on 7 lines in dev/build scripts where values are already scrubbed/masked before logging - oauthResultPage: add typeof guard before calling Map.get() result, satisfying CodeQL js/unvalidated-dynamic-method-call Signed-off-by: MagicLizi <jiali@magiclizi.com> Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> Signed-off-by: MagicLizi <jiali@magiclizi.com> * fix(security): revert RETURN_LABEL to Record with typeof guard Restores exhaustiveness checking while keeping typeof validation before invocation (satisfies CodeQL js/unvalidated-dynamic-method-call). Signed-off-by: MagicLizi <jiali@magiclizi.com> Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * fix(security): address round-6 review feedback - oauthResultPage: replace Record lookup with switch statement, fully eliminating dynamic dispatch (CodeQL js/unvalidated-dynamic-method-call) - stripHtmlTags: emit text.slice(i) and break when no '>' exists in the remainder — prevents O(n²) rescanning on repeated nested fragments - stripHtmlTags: when valid-tag scan hits inner '<', skip outer '<' to prevent partial tag reassembly (e.g. "<div <script>" strips "<div ") - _secretScrubRe (3 scripts): add value.length > 512 and newline filter to prevent "regular expression too large" on cert/JWT env values Signed-off-by: MagicLizi <jiali@magiclizi.com> Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * fix(security): make returnLabel switch exhaustive Remove default branch so TypeScript enforces all OAuthResultPageLang cases are handled — adding a new lang now produces a compile error. Signed-off-by: MagicLizi <jiali@magiclizi.com> Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * fix(security): sort scrub patterns + add form feed whitespace - _secretScrubRe: sort alternatives by length descending so longer values match first when one is a prefix of another (prevents partial masking) - stripHtmlTags: add U+000C (form feed) to HTML whitespace check per spec Signed-off-by: MagicLizi <jiali@magiclizi.com> Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> --------- Signed-off-by: Lizi <jiali@magiclizi.com> Signed-off-by: MagicLizi <jiali@magiclizi.com> Signed-off-by: MagicLizi <xd.lizi@gmail.com> Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
411 lines
14 KiB
JavaScript
411 lines
14 KiB
JavaScript
#!/usr/bin/env node
|
|
/**
|
|
* dev-embed-search — chat 嵌入语义检索的 dev 烟测 CLI。
|
|
*
|
|
* 用法:
|
|
* pnpm dev:embed:search <query> [--top N] [--workdir <path>] [--from <iso>] [--to <iso>]
|
|
* [--user-id <id>] [--db <path>]
|
|
*
|
|
* 例:
|
|
* pnpm dev:embed:search "测试一下记忆"
|
|
* pnpm dev:embed:search "Phase 1.2 实现" --top 5
|
|
* pnpm dev:embed:search "embedding" --workdir "/Users/me/projects/cindy"
|
|
*
|
|
* 数据来源:
|
|
* - 嵌入: XD Gateway /v1/embeddings (model=voyage/voyage-4, 直接 HTTP, 不走 EmbeddingClient
|
|
* 的 LRU 缓存 — CLI 调用频率低, 每次 query 都打一次 fresh API 是 ok 的)
|
|
* - 数据库: <userData>/cindy-<userId>.db (与 desktop dev 同一份文件; WAL 模式
|
|
* 下并发读取安全, 不影响 desktop 实时写入)
|
|
* - vec extension: apps/desktop/native/sqlite-vec/<platform-arch>/vec0.<dylib|dll>
|
|
*
|
|
* 环境变量:
|
|
* ANTHROPIC_API_KEY 必填; 与 desktop 使用的同一个 LiteLLM bearer token
|
|
* XDT_USER_ID 可选; 显式指定 user_id (跳过 db glob)
|
|
*
|
|
* 安全:
|
|
* - 仅 SELECT, 全程不写库; readonly: false 但脚本里不发任何写语句 (WAL 兼容性需要
|
|
* 非 readonly 才能读 -wal/-shm)
|
|
* - 不修改 desktop 任何运行时态
|
|
*/
|
|
|
|
import fs from 'node:fs';
|
|
import os from 'node:os';
|
|
import path from 'node:path';
|
|
import process from 'node:process';
|
|
import { fileURLToPath } from 'node:url';
|
|
|
|
const __filename = fileURLToPath(import.meta.url);
|
|
const __dirname = path.dirname(__filename);
|
|
const REPO_ROOT = path.resolve(__dirname, '..');
|
|
|
|
// ── arg parsing ─────────────────────────────────────────────────────────────
|
|
|
|
function parseArgs(argv) {
|
|
const out = {
|
|
query: null,
|
|
top: 10,
|
|
workdir: null,
|
|
from: null,
|
|
to: null,
|
|
userId: process.env.XDT_USER_ID ?? null,
|
|
dbPath: null,
|
|
};
|
|
for (let i = 0; i < argv.length; i++) {
|
|
const a = argv[i];
|
|
if (a === '--top') {
|
|
out.top = parseInt(argv[++i], 10);
|
|
if (!Number.isFinite(out.top) || out.top <= 0) fail('--top requires a positive integer');
|
|
} else if (a === '--workdir') {
|
|
out.workdir = argv[++i];
|
|
} else if (a === '--from') {
|
|
out.from = argv[++i];
|
|
} else if (a === '--to') {
|
|
out.to = argv[++i];
|
|
} else if (a === '--user-id') {
|
|
out.userId = argv[++i];
|
|
} else if (a === '--db') {
|
|
out.dbPath = argv[++i];
|
|
} else if (a === '-h' || a === '--help') {
|
|
printHelp();
|
|
process.exit(0);
|
|
} else if (a.startsWith('--')) {
|
|
fail(`unknown option: ${a}`);
|
|
} else if (out.query === null) {
|
|
out.query = a;
|
|
} else {
|
|
fail(`unexpected positional arg: ${a} (only one query string allowed; wrap in quotes)`);
|
|
}
|
|
}
|
|
if (!out.query) {
|
|
printHelp();
|
|
process.exit(1);
|
|
}
|
|
return out;
|
|
}
|
|
|
|
function printHelp() {
|
|
console.log(`Usage: pnpm dev:embed:search <query> [options]
|
|
|
|
Options:
|
|
--top N 返回 top N 结果 (默认 10)
|
|
--workdir <path> 只搜某个 working_dir 下的 sessions
|
|
--from <iso> 不早于该时间 (ISO 8601, 如 2026-05-01)
|
|
--to <iso> 不晚于该时间
|
|
--user-id <id> 显式指定 user_id (默认从 XDT_USER_ID 读, 或 glob 匹配)
|
|
--db <path> 覆盖 db 路径 (绕过 user-id 解析)
|
|
|
|
环境变量:
|
|
ANTHROPIC_API_KEY 必填 — 与 desktop 同一个 LiteLLM bearer token
|
|
VITE_XD_GATEWAY_BASE_URL 必填 — embedding gateway base URL
|
|
XDT_USER_ID 可选 — 多账号时指定要查的 user
|
|
`);
|
|
}
|
|
|
|
// 用 RegExp 切断 CodeQL 对 env 值的 taint 追踪链
|
|
const _bearerScrubRe = (() => {
|
|
const key = process.env.ANTHROPIC_API_KEY;
|
|
if (!key || key.length < 6) return null;
|
|
return new RegExp(key.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'), 'g');
|
|
})();
|
|
|
|
function fail(msg) {
|
|
const s = String(msg ?? '');
|
|
const safe = _bearerScrubRe ? s.replace(_bearerScrubRe, '***') : s;
|
|
console.error(`[dev-embed-search] ERROR: ${safe}`); // lgtm[js/clear-text-logging]
|
|
process.exit(1);
|
|
}
|
|
|
|
// ── userData / db path 定位 ─────────────────────────────────────────────────
|
|
|
|
/**
|
|
* Electron app.getPath('userData') 在不同平台的等价路径 — 与
|
|
* apps/desktop/src/main/localDb/index.ts 中 dbPath() 的拼接逻辑一致。
|
|
* userData 子目录名来自 productName(2026-07-17 身份翻转后为 'Cindy',
|
|
* 与 @cindy/maker-shared/brand-identity 的 userDataDirName 同源;本脚本是
|
|
* 零依赖 dev CLI,不 import TS 包,字面量与之保持一致)。
|
|
*/
|
|
const USER_DATA_DIR_NAME = 'Cindy';
|
|
const DB_FILE_PREFIX = 'cindy';
|
|
|
|
function userDataDir() {
|
|
switch (process.platform) {
|
|
case 'darwin':
|
|
return path.join(os.homedir(), 'Library', 'Application Support', USER_DATA_DIR_NAME);
|
|
case 'win32':
|
|
return path.join(
|
|
process.env.APPDATA ?? path.join(os.homedir(), 'AppData', 'Roaming'),
|
|
USER_DATA_DIR_NAME,
|
|
);
|
|
case 'linux':
|
|
return path.join(
|
|
process.env.XDG_CONFIG_HOME ?? path.join(os.homedir(), '.config'),
|
|
USER_DATA_DIR_NAME,
|
|
);
|
|
default:
|
|
fail(`unsupported platform: ${process.platform}`);
|
|
}
|
|
}
|
|
|
|
function resolveDbPath(args) {
|
|
if (args.dbPath) {
|
|
if (!fs.existsSync(args.dbPath)) fail(`db not found: ${args.dbPath}`);
|
|
return args.dbPath;
|
|
}
|
|
const dir = userDataDir();
|
|
if (!fs.existsSync(dir)) {
|
|
fail(
|
|
`userData dir not found: ${dir}\n` +
|
|
`desktop 至少需要登录一次过才会创建 DB。请先 pnpm restart:desktop:remote 登录。`,
|
|
);
|
|
}
|
|
if (args.userId) {
|
|
const file = path.join(dir, `${DB_FILE_PREFIX}-${args.userId}.db`);
|
|
if (!fs.existsSync(file)) fail(`db not found for userId=${args.userId}: ${file}`);
|
|
return file;
|
|
}
|
|
// glob 匹配 cindy-*.db (排除 -wal/-shm)
|
|
const entries = fs
|
|
.readdirSync(dir)
|
|
.filter((f) => new RegExp(`^${DB_FILE_PREFIX}-[^/\\\\]+\\.db$`).test(f) && !f.endsWith('-wal') && !f.endsWith('-shm'));
|
|
if (entries.length === 0) {
|
|
fail(`no ${DB_FILE_PREFIX}-*.db found under ${dir}\n请先 pnpm restart:desktop:remote 登录建库。`);
|
|
}
|
|
if (entries.length > 1) {
|
|
fail(
|
|
`multiple DBs found under ${dir}:\n` +
|
|
entries.map((e) => ` - ${e}`).join('\n') +
|
|
`\n请用 --user-id 或环境变量 XDT_USER_ID 指定要查哪一个 (从文件名 cindy-<userId>.db 取 userId)。`,
|
|
);
|
|
}
|
|
return path.join(dir, entries[0]);
|
|
}
|
|
|
|
// ── sqlite-vec extension 路径 ────────────────────────────────────────────────
|
|
|
|
function resolveVec0Path() {
|
|
const platDir = `${process.platform}-${process.arch}`;
|
|
const file =
|
|
process.platform === 'win32'
|
|
? 'vec0.dll'
|
|
: process.platform === 'linux'
|
|
? 'vec0.so'
|
|
: 'vec0.dylib';
|
|
const p = path.join(REPO_ROOT, 'apps', 'desktop', 'native', 'sqlite-vec', platDir, file);
|
|
if (!fs.existsSync(p)) {
|
|
fail(
|
|
`sqlite-vec extension not found at: ${p}\n` +
|
|
`当前平台 ${platDir} 可能未提供 vec0 二进制, 或需要重新 git lfs pull。`,
|
|
);
|
|
}
|
|
return p;
|
|
}
|
|
|
|
// ── XD Gateway embed call (内联, 不走 EmbeddingClient 的 LRU) ──────────────────
|
|
|
|
const MODEL_ID = 'voyage/voyage-4';
|
|
|
|
async function embedQuery(text) {
|
|
const baseUrl = process.env.VITE_XD_GATEWAY_BASE_URL?.trim();
|
|
if (!baseUrl) {
|
|
fail('VITE_XD_GATEWAY_BASE_URL 必须设置 (embedding dev CLI 不再读取生产端点私有配置)');
|
|
}
|
|
const apiKey = process.env.ANTHROPIC_API_KEY;
|
|
if (!apiKey) {
|
|
fail('ANTHROPIC_API_KEY 必须设置 (与 desktop 使用的同一个 LiteLLM bearer token)');
|
|
}
|
|
const res = await fetch(`${baseUrl.replace(/\/+$/, '')}/v1/embeddings`, {
|
|
method: 'POST',
|
|
headers: {
|
|
Authorization: `Bearer ${apiKey}`,
|
|
'Content-Type': 'application/json',
|
|
},
|
|
body: JSON.stringify({ input: [text], model: MODEL_ID }),
|
|
});
|
|
if (!res.ok) {
|
|
const body = await res.text().catch(() => '<no body>');
|
|
fail(`XD Gateway /v1/embeddings ${res.status}: ${body.slice(0, 500)}`);
|
|
}
|
|
const data = await res.json();
|
|
const vec = data?.data?.[0]?.embedding;
|
|
if (!Array.isArray(vec)) {
|
|
fail(`XD Gateway response missing embedding: ${JSON.stringify(data).slice(0, 300)}`);
|
|
}
|
|
return vec;
|
|
}
|
|
|
|
// ── main ─────────────────────────────────────────────────────────────────────
|
|
|
|
async function main() {
|
|
const args = parseArgs(process.argv.slice(2));
|
|
const dbPath = resolveDbPath(args);
|
|
const vec0Path = resolveVec0Path();
|
|
|
|
console.log(`[dev-embed-search] db = ${dbPath}`);
|
|
console.log(`[dev-embed-search] vec0 = ${vec0Path}`);
|
|
console.log(`[dev-embed-search] model = ${MODEL_ID}`);
|
|
console.log(`[dev-embed-search] query = ${JSON.stringify(args.query)}`);
|
|
console.log(`[dev-embed-search] embedding query...`);
|
|
|
|
const queryVec = await embedQuery(args.query);
|
|
if (queryVec.length !== 1024) {
|
|
fail(`expected 1024-dim embedding, got ${queryVec.length}`);
|
|
}
|
|
|
|
// 用 require() 加载 better-sqlite3 (CommonJS native 模块, ESM import 不支持
|
|
// postinstall 的 .node binding 自动解析)。
|
|
const { createRequire } = await import('node:module');
|
|
const require = createRequire(import.meta.url);
|
|
let Database;
|
|
try {
|
|
Database = require('better-sqlite3');
|
|
} catch (err) {
|
|
fail(
|
|
`better-sqlite3 加载失败: ${err instanceof Error ? err.message : String(err)}\n` +
|
|
`请确保仓库依赖完整 (pnpm install)。`,
|
|
);
|
|
}
|
|
|
|
const db = new Database(dbPath, { readonly: false, fileMustExist: true });
|
|
try {
|
|
db.loadExtension(vec0Path);
|
|
} catch (err) {
|
|
db.close();
|
|
fail(`sqlite-vec extension load failed: ${err instanceof Error ? err.message : String(err)}`);
|
|
}
|
|
|
|
// 时间过滤 (ISO → ms)
|
|
const fromMs = args.from ? Date.parse(args.from) : null;
|
|
const toMs = args.to ? Date.parse(args.to) : null;
|
|
if (args.from && !Number.isFinite(fromMs)) fail(`invalid --from: ${args.from}`);
|
|
if (args.to && !Number.isFinite(toMs)) fail(`invalid --to: ${args.to}`);
|
|
|
|
const whereExtra = [];
|
|
const extraArgs = [];
|
|
if (args.workdir) {
|
|
whereExtra.push(`s.working_dir = ?`);
|
|
extraArgs.push(args.workdir);
|
|
}
|
|
if (fromMs !== null) {
|
|
whereExtra.push(`m.created_at >= ?`);
|
|
extraArgs.push(fromMs);
|
|
}
|
|
if (toMs !== null) {
|
|
whereExtra.push(`m.created_at < ?`);
|
|
extraArgs.push(toMs);
|
|
}
|
|
whereExtra.push(`m.rewind_at IS NULL`);
|
|
|
|
// 多检索一些 (top * 5), 因为 vec MATCH 可能命中已被 rewind / 已删 session 的孤儿 rowid,
|
|
// post-filter 后取前 N。
|
|
const overFetch = Math.max(args.top * 5, args.top + 20);
|
|
// sqlite-vec 要求 LIMIT/'k=?' 直接挂在纯 vec0 查询上(不能跟 JOIN/额外 WHERE 同层,
|
|
// 否则报 "A LIMIT or 'k = ?' constraint is required on vec0 knn queries.")。
|
|
// 故纯 KNN 放进 CTE(LIMIT=overFetch), 外层再 JOIN + 过滤 + 截 top。
|
|
const sql = `
|
|
WITH knn AS (
|
|
SELECT rowid, distance
|
|
FROM chat_messages_vec_v1
|
|
WHERE embedding MATCH ?
|
|
ORDER BY distance
|
|
LIMIT ?
|
|
)
|
|
SELECT m.id AS message_id,
|
|
m.session_id AS session_id,
|
|
m.role AS role,
|
|
m.content AS content,
|
|
m.created_at AS created_at,
|
|
s.title AS session_title,
|
|
s.working_dir AS working_dir,
|
|
s.agent_kind AS agent_kind,
|
|
knn.distance AS distance
|
|
FROM knn
|
|
JOIN embedding_jobs j ON j.rowid = knn.rowid
|
|
JOIN messages m ON m.id = j.source_id
|
|
JOIN sessions s ON s.id = m.session_id
|
|
WHERE ${whereExtra.join(' AND ')}
|
|
ORDER BY knn.distance
|
|
`;
|
|
|
|
const f32 = Buffer.from(Float32Array.from(queryVec).buffer);
|
|
let rows;
|
|
try {
|
|
rows = db
|
|
.prepare(sql)
|
|
.all(f32, overFetch, ...extraArgs);
|
|
} catch (err) {
|
|
db.close();
|
|
fail(`查询失败: ${err instanceof Error ? err.message : String(err)}`);
|
|
}
|
|
db.close();
|
|
|
|
if (rows.length === 0) {
|
|
console.log(`\n[dev-embed-search] no results.`);
|
|
console.log(
|
|
`提示: 如果是首次启用聊天嵌入, Worker 5s 轮询 + voyage-4 调用需要时间; ` +
|
|
`也确认设置里 "聊天记录语义索引" 已开, 并且发过几条 user/assistant 消息。`,
|
|
);
|
|
return;
|
|
}
|
|
|
|
const top = rows.slice(0, args.top);
|
|
console.log(`\n[dev-embed-search] top ${top.length} of ${rows.length} hits:\n`);
|
|
|
|
for (let i = 0; i < top.length; i++) {
|
|
const r = top[i];
|
|
const snippet = previewSnippet(r.role, r.content);
|
|
const ts = new Date(r.created_at).toISOString().replace('T', ' ').replace(/\..+/, '');
|
|
console.log(
|
|
`#${String(i + 1).padStart(2, ' ')} dist=${r.distance.toFixed(4)} ` +
|
|
`[${r.role}] ${ts} ${(r.session_title || '(untitled)').slice(0, 30)}`,
|
|
);
|
|
console.log(` workdir: ${r.working_dir ?? '(none)'} agent: ${r.agent_kind ?? '(none)'}`);
|
|
console.log(` msg.id : ${r.message_id}`);
|
|
console.log(` >>> ${snippet}`);
|
|
console.log('');
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 从 messages.content (raw JSON 字符串) 提取一个 80 字符的预览。
|
|
* 与 chat-history-embedder.extractEmbedText 类似, 但只取头部、不做长度限制。
|
|
*/
|
|
function previewSnippet(role, contentRaw) {
|
|
let parsed;
|
|
try {
|
|
parsed = JSON.parse(contentRaw);
|
|
} catch {
|
|
parsed = contentRaw;
|
|
}
|
|
let text = '';
|
|
if (typeof parsed === 'string') {
|
|
text = parsed;
|
|
} else if (Array.isArray(parsed)) {
|
|
const parts = [];
|
|
for (const b of parsed) {
|
|
if (b && typeof b === 'object' && b.type === 'text' && typeof b.text === 'string') {
|
|
parts.push(b.text);
|
|
}
|
|
}
|
|
text = parts.join(' ');
|
|
} else if (parsed && typeof parsed === 'object') {
|
|
if (role === 'ask_user' && Array.isArray(parsed.questions)) {
|
|
text = parsed.questions.map((q) => q?.question ?? '').filter(Boolean).join(' | ');
|
|
} else if (role === 'plan_review' && typeof parsed.plan === 'string') {
|
|
text = parsed.plan;
|
|
} else if (typeof parsed.text === 'string') {
|
|
text = parsed.text;
|
|
} else {
|
|
text = JSON.stringify(parsed);
|
|
}
|
|
} else {
|
|
text = String(parsed);
|
|
}
|
|
text = text.replace(/\s+/g, ' ').trim();
|
|
return text.length > 80 ? text.slice(0, 80) + '…' : text;
|
|
}
|
|
|
|
main().catch((err) => {
|
|
console.error(err);
|
|
process.exit(1);
|
|
});
|