调试上下文时经常要把某条历史改掉再看模型怎么反应。用户消息的编辑是「覆盖式重发」 (删掉该条之后的全部历史再跑一轮),AI 回复和工具回执需要的只是改字面、直接入库、 不触发重跑。 - engine.patchMessageContent:只 UPDATE content,不删后续历史、不重跑;运行中 409。 改的是历史中间一行,末尾行 id 没变、锚点查不出差异,所以主动 invalidateContext, 下一轮发 X-Context-Reset 全量重建,否则 relay 缓存里还是改之前的文本。 - PATCH /api/sessions/:id/messages/:mid 暴露给前端,不限 role(assistant / tool 都走它)。 - 前端 AI 气泡和工具卡各加一个铅笔(title 都写明「直接入库,不重跑」),点击回填 底部输入框,editingRole 区分 assistant / tool,保存后只重绘那一个气泡或那张卡。 工具卡编辑前要求已展开加载全文,否则只有「执行中…」占位。 - 修流式收尾漏传 aiEdit:刚答完那条没有编辑按钮、刷新一下才有。 - 同一轮的多个工具调用包进 .tool-group(左侧竖线连起来):一条 assistant 消息可以带 2~6 个 tool_calls,以前几张卡看不出是同一轮还是两轮。单调用不套壳。
164 lines
8.3 KiB
JavaScript
164 lines
8.3 KiB
JavaScript
// 消息与交互 API:发消息 / SSE 流 / 开始执行 / 终止
|
||
const express = require('express');
|
||
const engine = require('../engine');
|
||
const log = require('../log');
|
||
|
||
const router = express.Router();
|
||
|
||
function wrap(fn) {
|
||
return function (req, res) {
|
||
try {
|
||
fn(req, res);
|
||
} catch (err) {
|
||
res.status(err && err.status_code || 500).json({ error: String(err && err.message || err) });
|
||
}
|
||
};
|
||
}
|
||
|
||
// 附件上传只写任务目录文件(见 routes/files.js),消息正文原样入库,不再拼接任何通知行。
|
||
// delivery:
|
||
// live(默认,兼容旧调用方/微信)→ 立即入库;回合在跑时下一轮迭代自动带上
|
||
// queue → 回合在跑时进待投递队列(不落库),回合收尾后自动发出;不在跑则等价于 live
|
||
// 「干预 / 立即发送」不走这里,而是对已在队列里的条目做升级(POST /pending/:qid)。
|
||
router.post('/:id/messages', wrap((req, res) => {
|
||
const content = String((req.body && req.body.content) || '').trim();
|
||
if (!content) return res.status(400).json({ error: 'content 不能为空' });
|
||
// deepThink:是否启用模型该「深度思考」参数(thinking_params 合并进上游请求体)
|
||
const deepThink = !!(req.body && req.body.deepThink);
|
||
const delivery = String((req.body && req.body.delivery) || 'live');
|
||
if (delivery === 'queue') {
|
||
const r = engine.enqueueMessage(req.params.id, content, { deepThink });
|
||
log.info('msg', (req.user || '-') + ' ' + req.params.id + ' 用户消息(' +
|
||
(r.queued ? '排队 ' + r.pending_id : r.delivered || '?') + '):' +
|
||
content.slice(0, 60).replace(/\s+/g, ' '));
|
||
return res.json(r);
|
||
}
|
||
const r = engine.postUserMessage(req.params.id, content, { deepThink });
|
||
log.info('msg', (req.user || '-') + ' ' + req.params.id + ' 用户消息(' + (r && r.delivered || '?') + '):' +
|
||
content.slice(0, 60).replace(/\s+/g, ' '));
|
||
res.json(r);
|
||
}));
|
||
|
||
// 待投递队列:升级队首(干预 / 立即发送)。
|
||
// 干预 = 等当前工具跑完/本条回复落库后插话;立即发送 = 杀进程 + abort 流,本回合收尾后立刻发。
|
||
router.post('/:id/pending/:qid', wrap((req, res) => {
|
||
const action = String((req.body && req.body.action) || 'interject');
|
||
if (action !== 'interject' && action !== 'now') {
|
||
return res.status(400).json({ error: 'action 只能是 interject 或 now' });
|
||
}
|
||
const r = engine.promotePending(req.params.id, req.params.qid, action);
|
||
log.info('msg', (req.user || '-') + ' ' + req.params.id + ' 待投递消息 ' + req.params.qid + ' → ' + action);
|
||
res.json(r);
|
||
}));
|
||
|
||
// 取消一条待投递消息(条幅上的 ×)。
|
||
router.delete('/:id/pending/:qid', wrap((req, res) => {
|
||
res.json(engine.cancelPending(req.params.id, req.params.qid));
|
||
}));
|
||
|
||
|
||
// 覆盖式编辑:删除该条及其后的历史,再以新内容重开一轮(运行中 409)。
|
||
router.post('/:id/messages/:mid/edit', wrap((req, res) => {
|
||
const content = String((req.body && req.body.content) || '').trim();
|
||
if (!content) return res.status(400).json({ error: 'content 不能为空' });
|
||
const deepThink = !!(req.body && req.body.deepThink);
|
||
const r = engine.editUserMessage(req.params.id, Number(req.params.mid), content, { deepThink });
|
||
log.info('msg', (req.user || '-') + ' ' + req.params.id + ' 编辑消息 ' + req.params.mid +
|
||
'(丢弃其后 ' + Math.max(0, (r.removed || 1) - 1) + ' 条)');
|
||
res.json(r);
|
||
}));
|
||
|
||
// 原地改写一条消息的正文(调试上下文用):只覆盖 content,不删后续历史、不重跑。
|
||
// 与上面的 /edit 区别:那个是「覆盖式重发」(用户消息专用),这个只改字面。
|
||
router.patch('/:id/messages/:mid', wrap((req, res) => {
|
||
const content = String((req.body && req.body.content) || '').trim();
|
||
if (!content) return res.status(400).json({ error: 'content 不能为空' });
|
||
const r = engine.patchMessageContent(req.params.id, Number(req.params.mid), content);
|
||
log.info('msg', (req.user || '-') + ' ' + req.params.id + ' 修改消息正文 ' + req.params.mid +
|
||
'(直接入库,不重跑)');
|
||
res.json(r);
|
||
}));
|
||
|
||
router.post('/:id/stop', wrap((req, res) => {
|
||
const r = engine.stop(req.params.id);
|
||
log.info('msg', (req.user || '-') + ' ' + req.params.id + ' 用户终止');
|
||
res.json(r);
|
||
}));
|
||
|
||
// 断开:held 会话收起「保持中」态,结版为已完成(纯状态动作:key 非独占,无模型可释放,绑定仍在可继续用)
|
||
router.post('/:id/release', wrap((req, res) => {
|
||
res.json(engine.releaseHold(req.params.id));
|
||
}));
|
||
|
||
// 清空会话内容:删除全部消息记录,保留会话本身与任务目录文件(不再随下条消息重发旧历史)。
|
||
// 状态收尾为已完成(绑定保持,下次发言沿用原模型);正在执行的回合会被拒绝(需先「终止」)。
|
||
router.delete('/:id/messages', wrap((req, res) => {
|
||
const r = engine.clearSessionMessages(req.params.id);
|
||
log.info('msg', (req.user || '-') + ' ' + req.params.id + ' 清空会话内容(文件保留)');
|
||
res.json(r);
|
||
}));
|
||
|
||
// 存档会话内容:把全部消息标成 archived(不再发给模型),但一行不删,界面仍可查看。
|
||
// 效果等同清空,区别只在留档;任务文件、标题、记忆碎片都不动,会话状态也不变。
|
||
router.post('/:id/messages/archive', wrap((req, res) => {
|
||
const r = engine.archiveSessionMessages(req.params.id);
|
||
log.info('msg', (req.user || '-') + ' ' + req.params.id + ' 存档会话内容(留档,不重跑)');
|
||
res.json(r);
|
||
}));
|
||
|
||
// 上下文压缩:phase-bar「压缩」按钮弹窗里点「摘要」触发。
|
||
// AI 总结;摘要之前的历史存档(type='archived',UI 保留展示、不再发给模型)。
|
||
// 摘要行本身标成 type='summary'(见 engine.runLoopInner),作为后续轮次的上下文起点。
|
||
// 后端直接开启一个压缩回合(不往会话插入任何消息):该回合沿用默认 system prompt +
|
||
// 正常历史,末尾追加一条压缩指令 user,且不带 tools(见 engine.summarizeContext);
|
||
// 响应仅回显一句短提示 prompt 供前端渲染指令气泡(不入库、不含模板正文),
|
||
// 并在总结回合的最终 SSE assistant_message 上带 is_summary 给前端走收尾。
|
||
// 状态非 active 时才允许(避免与正在跑的回合抢上下文)。本接口只是「丢一条总结请求进队列」,
|
||
// 不等待 AI 完成——前端通过现有 SSE 流听 assistant_message 事件拿结果。
|
||
router.post('/:id/summarize', wrap((req, res) => {
|
||
const r = engine.summarizeContext(req.params.id);
|
||
log.info('msg', (req.user || '-') + ' ' + req.params.id + ' 上下文压缩(' + (r.delivered || '?') + ')');
|
||
res.json(r);
|
||
}));
|
||
|
||
// 删除单条消息(含工具/任务消息):删 assistant 轮连带其 tool 回执;单删 tool 回执会把该调用
|
||
// 从父 assistant 的 tool_calls 里同步摘除(见 engine.deleteMessage),保证调用↔回执成对。
|
||
// 库中没了,后续发给模型的消息里自然也没有;执行中删除会在下一轮组装历史时生效。
|
||
router.delete('/:id/messages/:messageId', wrap((req, res) => {
|
||
const r = engine.deleteMessage(req.params.id, Number(req.params.messageId));
|
||
log.info('msg', (req.user || '-') + ' ' + req.params.id + ' 删除消息 ' + req.params.messageId +
|
||
(r.removed > 1 ? '(连带 ' + (r.removed - 1) + ' 条工具回执)' : ''));
|
||
res.json(r);
|
||
}));
|
||
|
||
// SSE 事件流
|
||
router.get('/:id/stream', (req, res) => {
|
||
const session = engine.getSession(req.params.id);
|
||
if (!session) return res.status(404).json({ error: '会话不存在' });
|
||
const bus = engine.getBus();
|
||
res.set({
|
||
'Content-Type': 'text/event-stream',
|
||
'Cache-Control': 'no-cache',
|
||
Connection: 'keep-alive',
|
||
'X-Accel-Buffering': 'no',
|
||
});
|
||
res.flushHeaders && res.flushHeaders();
|
||
res.write('retry: 3000\n\n');
|
||
bus.addClient(session.id, res);
|
||
const heartbeat = setInterval(() => {
|
||
// 可见心跳:具名事件(注释帧 `: ping` 不触发前端 JS 事件,无法做静默看门狗)
|
||
try { res.write('event: ping\ndata: {}\n\n'); } catch (_) {}
|
||
}, 15000);
|
||
req.on('close', () => {
|
||
clearInterval(heartbeat);
|
||
bus.removeClient(session.id, res);
|
||
});
|
||
bus.emit(session.id, 'status', {
|
||
session_id: session.id,
|
||
status: session.status,
|
||
model_id: session.model_id,
|
||
});
|
||
});
|
||
|
||
module.exports = router;
|