'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 };