上下文压缩:phase-bar 加压缩按钮,弹窗选「仅摘要/压缩总结」自动走流程

- 后端:config.yml 加 compress.prompt;engine.summarizeContext(sessionId, mode)
  把 prompt 落为 type='summarize_request' 的 user 消息;runLoopInner 在
  assistant_message SSE 上带 is_summary + mode 给前端;llm.buildMessages 跳过
  summarize_request / archived 行避免指令污染上下文;路由 POST /:id/summarize
  校验 mode ∈ {generate_only, compress_and_clear}。
- 前端:phase-bar「压缩」按钮(硬盘+下箭头 SVG 图标)取代原清空按钮;
  点击弹窗三按钮(取消 / 仅摘要蓝描边 / 压缩总结警示橙);选定后后端丢请求,
  SSE 收到总结时按模式自动调 DELETE 清空(仅 compress_and_clear)。
- 移除 phase-bar「清空」按钮;clearChat 保留为 no-op 防外部误调。
This commit is contained in:
2026-09-05 11:08:11 +08:00
parent 360169a489
commit eaf2cd68f5
6 changed files with 330 additions and 105 deletions
+7 -1
View File
@@ -44,6 +44,10 @@ function decodeBlocks(content) {
// 关联 tool 回执按 id 一并跳过(否则会留下无 tool_call 配对的孤儿 tool 消息,上游拒收);
// 改以一条 user 错误提示替代,让模型看到原因后重新生成工具调用 ——
// 坏样本留在上下文里只会被模型继续模仿,反复报同一个错。
// summarize_request 行(顶栏「压缩」按钮触发的总结指令,role=user, type='summarize_request'):
// 这条 user 是让 AI 做总结用的中间产物,AI 给出总结后就会被前端清空;为防用户在被清空前又发新消息
// 触发新一轮模型调用,这条指令会污染上下文(让 AI 误以为还要再总结),所以 buildMessages 直接跳过。
// 同时跳过 type='archived' 的中间态归档行(保留接口给将来需要时用,目前主流程走前端物理清空)。
function buildMessages(session, language) {
const messages = [{ role: 'system', content: systemPromptText(language) }];
const history = session.history;
@@ -57,7 +61,9 @@ function buildMessages(session, language) {
}
const dropToolCallIds = new Set();
history.forEach((row, i) => {
if (row.role === 'assistant') {
// summarize_request / archived:UI 可见,但不再进入模型上下文(避免污染 / 节省 token)
if (row.type === 'summarize_request' || row.type === 'archived') return;
if (row.role === 'assistant') {
if (row.type === 'errAck') {
let calls = [];
try { calls = JSON.parse(row.tool_calls || '[]') || []; } catch (_) {}