Files
persistent-chat-plugin/lib/bridge.js
卡若AI ba786b3700 feat: Persistent Chat v1.1 CO-Chat 整合上传 Gitea
同步 enhance/bridge/smoke/retire 模块、vendor 3.3.34、增强面板与 PRD 文档,
Hub 接入 hub-routes-prd,含安装/经验/模块说明与 co-integration 运维脚本。

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-06-29 17:42:16 +08:00

61 lines
1.9 KiB
JavaScript

'use strict';
const fs = require('fs');
const path = require('path');
const BRIDGE_FILE = path.join(require('os').homedir(), '.persistent-chat-local', 'bridge-log.json');
function readLog() {
try { return JSON.parse(fs.readFileSync(BRIDGE_FILE, 'utf8')); } catch { return { entries: [] }; }
}
function writeLog(log) {
fs.mkdirSync(path.dirname(BRIDGE_FILE), { recursive: true });
fs.writeFileSync(BRIDGE_FILE, JSON.stringify(log, null, 2));
}
/**
* FR-BRIDGE: 将 Composer/增强结论注入 ct_ 会话
* @param {object} opts
* @param {Map} sessions
* @param {Map} pendingWaits
* @param {function} nowMs
* @param {function} saveSessions
*/
function syncToSession(opts, sessions, pendingWaits, nowMs, saveSessions) {
const { token, content, source = 'composer' } = opts;
if (!token || !sessions.has(token)) {
return { ok: false, error: 'UNKNOWN_CONVERSATION_TOKEN' };
}
if (!content || typeof content !== 'string' || !content.trim()) {
return { ok: false, error: 'content 不能为空' };
}
const s = sessions.get(token);
const injected = `[BRIDGE:${source}]\n${content.trim()}\n\n请基于以上内容做卡若复盘(🎯📌💡📝▶)并继续 wait。`;
s.mode = s.bridged ? 'A+B' : (s.mode || 'A');
s.bridged = true;
s.lastBridgeAt = nowMs();
s.lastActiveAt = nowMs();
(s.messages = s.messages || []).push({ role: 'user', content: injected, at: nowMs(), bridge: true });
const wait = pendingWaits.get(token);
let wakeWaiting = false;
if (wait) {
pendingWaits.delete(token);
s.status = 'idle';
wait.resolve(injected);
wakeWaiting = true;
}
saveSessions();
const log = readLog();
log.entries = (log.entries || []).slice(-99);
log.entries.push({ token, at: nowMs(), source, wakeWaiting, len: content.length });
writeLog(log);
return { ok: true, token, wakeWaiting, mode: s.mode, injectedLen: injected.length };
}
module.exports = { syncToSession, readLog, BRIDGE_FILE };