Version: 0.9.20.dev.260415

后端:
1. 修复 query_available_slots section_from/section_to 错误覆盖 duration 并使用精确匹配而非范围包含
- 更新backend/newAgent/tools/schedule/read_filter_tools.go:移除 span = exactTo - exactFrom + 1 对 duration 的覆盖;matchSectionRange
  从精确匹配改为范围包含语义(slotStart < exactFrom || slotEnd > exactTo)
2. Execute 上下文窗口从硬编码裁剪改造为 80k token 动态预算 + LLM滚动压缩
- 基础设施层:AgentChat 新增 compaction 三个持久化字段,dao 新增 CRUD,Redis 新增缓存;pkg 新增 ExecuteTokenBudget常量、ExecuteTokenBreakdown 结构体、CheckExecuteTokenBudget 预算检查函数
- prompt 层:新建 compact_msg1.go / compact_msg2.go 分别实现msg1(历史对话)和 msg2(ReAct Loop)的 LLM 压缩;execute_context.go 移除 msg1 的 1400 字符/30 轮/120 字符三重裁剪和 msg2 的 8 条窗口限制,改为全量加载
- node 层:新建 execute_compact.go(compactExecuteMessagesIfNeeded:预算检查 → msg1 优先压缩 → msg2 兜底 → SSE 通知 → token 分布持久化);execute.go ReAct 循环插入 compact 调用 - 服务/API 层:AgentGraphDeps / AgentService 新增 CompactionStore 注入链路;新增 GET /api/v1/agent/context-stats 查询接口
- 启动层:cmd/start.go 注入 agentRepo 为 CompactionStore
3. 新增 Execute Context Compaction 决策报告
- 新建docs/功能决策记录/Execute_Context_Compaction_决策记录.md

前端:无 仓库:无
This commit is contained in:
LoveLosita
2026-04-15 22:01:37 +08:00
parent e77d42fce5
commit 8bde981592
23 changed files with 1921 additions and 8532 deletions

View File

@@ -0,0 +1,62 @@
package newagentprompt
import (
"context"
"fmt"
infrallm "github.com/LoveLosita/smartflow/backend/infra/llm"
"github.com/cloudwego/eino/schema"
)
const compactMsg1SystemPrompt = `你是一个对话压缩助手。你的任务是将以下多轮对话历史压缩为一段简洁的结构化摘要。
要求:
1. 保留用户的核心诉求和意图(原文关键词不要丢失)
2. 保留所有已确认的约束条件和规则
3. 保留关键操作决策和结果(比如排程相关的调整结果)
4. 保留用户偏好的重要信息
5. 去除冗余和重复信息
6. 按要点列出,每条一行
直接输出压缩后的摘要,不要输出其他内容。`
// CompactMsg1 将 msg1历史对话压缩为摘要。
// existingSummary 不为空时表示已有旧摘要,需要合并压缩。
func CompactMsg1(
ctx context.Context,
client *infrallm.Client,
historyText string,
existingSummary string,
) (string, error) {
var userContent string
if existingSummary != "" {
userContent = fmt.Sprintf(`已有压缩摘要:
%s
新增的对话记录:
%s
请将以上两部分合并为一份更紧凑的摘要。`, existingSummary, historyText)
} else {
userContent = fmt.Sprintf(`对话历史:
%s
请压缩以上对话历史。`, historyText)
}
messages := []*schema.Message{
schema.SystemMessage(compactMsg1SystemPrompt),
schema.UserMessage(userContent),
}
result, err := client.GenerateText(ctx, messages, infrallm.GenerateOptions{
MaxTokens: 4000,
})
if err != nil {
return "", fmt.Errorf("compact msg1 LLM call failed: %w", err)
}
if result == nil || result.Text == "" {
return "", fmt.Errorf("compact msg1 LLM returned empty result")
}
return result.Text, nil
}