Version: 0.9.26.dev.260417

后端:
1. Prompt 层从 execute 专属骨架重构为全节点统一四段式 buildUnifiedStageMessages
  - 新增 unified_context.go:定义 StageMessagesConfig + buildUnifiedStageMessages 统一骨架,所有节点(Chat/Plan/Execute/Deliver/DeepAnswer)共用同一套 msg0~msg3 拼装逻辑
  - 新增 conversation_view.go:通用对话历史渲染 buildConversationHistoryMessage,各节点复用,不再各自维护提取逻辑
  - 新增 chat_context.go / plan_context.go / deliver_context.go:各节点自行渲染 msg1(对话视图)和 msg2(工作区),统一层只负责"怎么拼",不再替节点决定"放什么"
  - Chat/Plan/Deliver/Execute 的 BuildXXXMessages 全部从 buildStageMessages 切到 buildUnifiedStageMessages,移除旧路径
  - 删除 execute_pinned.go:execute 记忆渲染合并到统一层 renderUnifiedMemoryContext
  - Plan prompt 不再在 user prompt 中拼装任务类 ID 列表和 renderStateSummary,改为依赖 msg2 规划工作区;Chat 粗排判断从"上下文有任务类 ID"改为"批量调度需求"
  - Deliver prompt 新增 IsAborted/IsExhaustedTerminal 区分,支持粗排收口和主动终止场景
2. Execute ReAct 上下文简化——移除归档搬运、窗口裁剪和重复工具压缩
  - 移除 splitExecuteLoopRecordsByBoundary、findLatestExecuteBoundaryMarker、tailExecuteLoops、compressExecuteLoopObservationsByTool、buildEarlyExecuteReactSummary、trimExecuteMessage1ByBudget 等六个函数
  - 移除 executeLoopWindowLimit / executeConversationTurnLimit / executeMessage1MaxRunes 等预算常量
  - msg1 不再从历史中归档上一轮 ReAct 结果,只保留真实对话流(user + assistant speak),全量注入
  - msg2 不再按 loop_closed / step_advanced 边界切分"归档/活跃",直接全量注入全部 ReAct Loop 记录
  - token 预算由统一压缩层兜底,prompt 层不再做提前裁剪
3. 压缩层从 Execute 专属提升为全节点通用 UnifiedCompact
  - 删除 execute_compact.go(Execute 专属压缩文件)
  - 新增 unified_compact.go:UnifiedCompactInput 参数化,各节点(Plan/Chat/Deliver/Execute)构造时从自己的 NodeInput 提取公共字段,消除对 Execute 的直接依赖
  - CompactionStore 接口扩展 LoadStageCompaction / SaveStageCompaction,各节点按 stageKey 独立维护压缩状态互不覆盖
  - 非 4 段式消息时退化成按角色汇总统计,确保 context_token_stats 仍然刷新
4. Retry 重试机制全面下线
  - dao/agent.go:saveChatHistoryCore / SaveChatHistory / SaveChatHistoryInTx 移除 retry_group_id / retry_index /
  retry_from_user_message_id / retry_from_assistant_message_id 四个参数,修复乱码注释
  - dao/agent-cache.go:移除 ApplyRetrySeed 和 extractMessageHistoryID 两个方法
  - conv/agent.go:ToEinoMessages 不再回灌 retry_* 字段到运行期上下文
  - service/agentsvc/agent.go:移除 chatRetryMeta 及 resolveRetryGroupID / buildRetrySeed 等全部重试逻辑
  - service/agentsvc/agent_quick_note.go:整个文件删除(retry 快速补写路径已无用)
  - service/events/chat_history_persist.go:移除 retry 参数传递
5. 节点层瘦身 + 可见消息逐条持久化
  - agent_nodes.go 大幅简化:Chat/Plan/Execute/Deliver 节点方法移除 ToolSchema 注入、状态摘要渲染等逻辑,只做参数转发和状态落盘
  - 新增 visible_message.go:persistVisibleAssistantMessage 统一处理可见 assistant speak 的实时持久化,失败仅记日志不中断主流程
  - 新增 llm_debug.go:logNodeLLMContext 统一打印 LLM 上下文调试日志
  - graph_run_state.go 新增 PersistVisibleMessageFunc 类型 + AgentGraphDeps.PersistVisibleMessage 字段
  - service/agentsvc/agent_newagent.go 精简主循环,注入 PersistVisibleMessage 回调;agent_history.go 精简历史构建
  - token_budget.go 移除 Execute 专属预算检查,统一到通用预算

前端:
1. 移除 retry 相关 UI 和类型
  - agent.ts 移除 retry_group_id / retry_index / retry_total 字段及 normalize 逻辑
  - AssistantPanel.vue 移除 retry 相关 UI 和交互代码(约 700 行精简)
  - dashboard.ts 移除 retry 相关类型定义
  - AssistantView.vue 微调
2. ContextWindowMeter 压缩次数展示和数值格式优化
  - 新增 formatCompactCount 工具函数,千位以上用 k 单位压缩(如 80k)
  - 新增压缩次数显示
3.修复了新对话发消息时,user和assistant消息被自动调换的bug

仓库:无
This commit is contained in:
Losita
2026-04-17 22:19:38 +08:00
parent d47a8bcabd
commit d8280cc647
39 changed files with 2095 additions and 2386 deletions

View File

@@ -25,7 +25,7 @@ const chatRoutingSystemPrompt = `
- route=direct_reply 时,控制码后的可见内容应直接回应用户问题,而不是先讲能力边界。
- route=deep_answer 时,只输出控制码即可,不要补“让我想想”“这是个好问题”之类的占位话术。
粗排判断:当用户意图包含"批量安排/排课/把任务类排进日程",且上下文中有任务类 ID 时,设置 rough_build=true。
粗排判断:当用户意图包含"批量安排/排课/把任务类排进日程"等批量调度需求时,设置 rough_build=true;后端会结合真实请求范围决定是否真正进入粗排
二次粗排约束(强约束):
- 若上下文已出现 rough_build_done且用户未明确要求"重新粗排/从头重排",必须设置 rough_build=false。
- "移动/微调/优化/均匀化/调顺序"等请求默认视为 refine不得再次触发 rough build。
@@ -83,40 +83,25 @@ func BuildChatRoutingSystemPrompt() string {
// BuildChatRoutingMessages 组装路由阶段的 messages。
func BuildChatRoutingMessages(ctx *newagentmodel.ConversationContext, userInput string, state *newagentmodel.CommonState, nonce string) []*schema.Message {
return buildStageMessages(
BuildChatRoutingSystemPrompt(),
return buildUnifiedStageMessages(
ctx,
BuildChatRoutingUserPrompt(ctx, userInput, state, nonce),
StageMessagesConfig{
SystemPrompt: BuildChatRoutingSystemPrompt(),
Msg1Content: buildChatConversationMessage(ctx),
Msg2Content: buildChatRoutingWorkspace(ctx),
Msg3Suffix: BuildChatRoutingUserPrompt(userInput, nonce),
Msg3Role: schema.User,
},
)
}
// BuildChatRoutingUserPrompt 构造路由阶段的用户提示词。
func BuildChatRoutingUserPrompt(ctx *newagentmodel.ConversationContext, userInput string, state *newagentmodel.CommonState, nonce string) string {
func BuildChatRoutingUserPrompt(userInput string, nonce string) string {
var sb strings.Builder
sb.WriteString(fmt.Sprintf("nonce=%s\n", nonce))
sb.WriteString(fmt.Sprintf("当前时间=%s\n", time.Now().In(time.Local).Format("2006-01-02 15:04")))
sb.WriteString("\n请判断用户本轮意图的复杂度,选择最合适的路由,并输出控制码和对应内容。\n")
// 注入任务类上下文(供粗排判断参考)。
if state != nil && len(state.TaskClassIDs) > 0 {
parts := make([]string, len(state.TaskClassIDs))
for i, id := range state.TaskClassIDs {
parts[i] = fmt.Sprintf("%d", id)
}
sb.WriteString(fmt.Sprintf("\n本次请求涉及的任务类 ID[%s]\n", strings.Join(parts, ", ")))
}
if state != nil && len(state.TaskClasses) > 0 {
sb.WriteString("任务类约束:\n")
for _, tc := range state.TaskClasses {
line := fmt.Sprintf("- [ID=%d] %s策略=%s总时段预算=%d", tc.ID, tc.Name, tc.Strategy, tc.TotalSlots)
if tc.StartDate != "" || tc.EndDate != "" {
line += fmt.Sprintf(",日期范围=%s ~ %s", tc.StartDate, tc.EndDate)
}
sb.WriteString(line + "\n")
}
}
sb.WriteString("\n请基于最近真实对话和本轮输入选择最合适的路由,并严格按系统约定输出控制码。\n")
trimmedInput := strings.TrimSpace(userInput)
if trimmedInput != "" {
@@ -146,10 +131,23 @@ func BuildDeepAnswerSystemPrompt() string {
}
// BuildDeepAnswerMessages 组装深度回答阶段的 messages。
func BuildDeepAnswerMessages(ctx *newagentmodel.ConversationContext, userInput string) []*schema.Message {
return buildStageMessages(
BuildDeepAnswerSystemPrompt(),
func BuildDeepAnswerMessages(state *newagentmodel.CommonState, ctx *newagentmodel.ConversationContext, userInput string) []*schema.Message {
return buildUnifiedStageMessages(
ctx,
userInput,
StageMessagesConfig{
SystemPrompt: BuildDeepAnswerSystemPrompt(),
Msg1Content: buildChatConversationMessage(ctx),
Msg2Content: buildDeepAnswerWorkspace(),
Msg3Suffix: buildDeepAnswerUserPrompt(userInput),
Msg3Role: schema.User,
},
)
}
func buildDeepAnswerUserPrompt(userInput string) string {
trimmedInput := strings.TrimSpace(userInput)
if trimmedInput != "" {
return trimmedInput
}
return "请直接回答用户刚才的问题。"
}

View File

@@ -0,0 +1,33 @@
package newagentprompt
import (
"strings"
newagentmodel "github.com/LoveLosita/smartflow/backend/newAgent/model"
)
// buildChatConversationMessage 生成 chat / deep_answer 共用的真实对话视图。
func buildChatConversationMessage(ctx *newagentmodel.ConversationContext) string {
return buildConversationHistoryMessage(ctx, "真实对话记录")
}
// buildChatRoutingWorkspace 渲染 chat 路由节点的轻量补充区。
//
// 设计说明:
// 1. chat 只保留与路由判断直接相关的最小流程标记;
// 2. rough_build_done 仍需显式暴露,否则路由层会丢掉“不要重复粗排”的关键信号;
// 3. 不再展示轮次、阶段锚点、ReAct 摘要等 execute 专属信息。
func buildChatRoutingWorkspace(ctx *newagentmodel.ConversationContext) string {
lines := []string{"路由补充:"}
if hasExecuteRoughBuildDone(ctx) {
lines = append(lines, "- 已存在 rough_build_done除非用户明确要求重新粗排否则不要再次触发 rough_build。")
} else {
lines = append(lines, "- 暂无额外流程标记。")
}
return strings.Join(lines, "\n")
}
// buildDeepAnswerWorkspace 渲染 deep_answer 节点的轻量工作区。
func buildDeepAnswerWorkspace() string {
return "回答补充:请直接延续最近对话,聚焦回答用户本轮问题。"
}

View File

@@ -0,0 +1,37 @@
package newagentprompt
import (
"strings"
newagentmodel "github.com/LoveLosita/smartflow/backend/newAgent/model"
)
// buildConversationHistoryMessage 将“真实对话流”渲染成节点可直接复用的 msg1。
//
// 职责边界:
// 1. 只负责把 user + assistant speak 组织成稳定文本;
// 2. 不拼接 tool_call / tool observation这些不属于“真实对话”
// 3. 不做长度裁剪,长度预算交给统一压缩层处理。
func buildConversationHistoryMessage(ctx *newagentmodel.ConversationContext, title string) string {
title = strings.TrimSpace(title)
if title == "" {
title = "真实对话记录"
}
lines := []string{title + ""}
if ctx == nil {
lines = append(lines, "暂无。")
return strings.Join(lines, "\n")
}
turns := CollectConversationTurns(ctx.HistorySnapshot())
if len(turns) == 0 {
lines = append(lines, "暂无。")
return strings.Join(lines, "\n")
}
for _, turn := range turns {
lines = append(lines, turn.Role+": \""+turn.Content+"\"")
}
return strings.Join(lines, "\n")
}

View File

@@ -14,16 +14,16 @@ const deliverSystemPrompt = `
请遵守以下规则:
1. 只基于已有历史和计划状态生成总结,不要编造未执行的操作。
2. 如果所有步骤都已完成,简要总结每一步的成果。
3. 如果因轮次耗尽提前结束,如实告知用户当前进度未完成部分。
4. 使用自然、友好的语气,不要机械罗列步骤
5. 如果用户后续可能需要继续操作,给出简短建议。
6. 只输出总结文本,不要输出 JSON不要输出 markdown 标题。
2. 如果所有步骤都已完成,请自然概括每一步的主要成果。
3. 如果流程因轮次耗尽或主动终止而提前结束,如实说明当前进度未完成部分。
4. 使用自然、友好的语气,不要机械罗列工具过程
5. 如果用户后续需要继续操作,可以给出一句简短建议。
6. 只输出总结文本,不要输出 JSON不要输出 markdown 标题。
你会看到:
- 原始计划步骤及完成判定
- 当前执行进度
- 执行阶段的对话历史
- 原始计划步骤及完成进度
- 最近真实对话
- 当前流程的收口状态
`
// BuildDeliverSystemPrompt 返回交付阶段系统提示词。
@@ -31,37 +31,52 @@ func BuildDeliverSystemPrompt() string {
return strings.TrimSpace(deliverSystemPrompt)
}
// BuildDeliverMessages 组装交付阶段 messages。
// BuildDeliverMessages 组装交付阶段 messages。
func BuildDeliverMessages(state *newagentmodel.CommonState, ctx *newagentmodel.ConversationContext) []*schema.Message {
return buildStageMessages(
BuildDeliverSystemPrompt(),
roughBuildPrefix := buildDeliverRoughBuildPrefix(ctx, state)
return buildUnifiedStageMessages(
ctx,
BuildDeliverUserPrompt(state),
StageMessagesConfig{
SystemPrompt: BuildDeliverSystemPrompt(),
Msg1Content: buildDeliverConversationMessage(ctx),
Msg2Content: buildDeliverWorkspace(state),
Msg3Prefix: roughBuildPrefix,
Msg3Suffix: BuildDeliverUserPrompt(state, ctx),
Msg3Role: schema.User,
},
)
}
// BuildDeliverUserPrompt 构造交付阶段的用户提示词。
func BuildDeliverUserPrompt(state *newagentmodel.CommonState) string {
func BuildDeliverUserPrompt(state *newagentmodel.CommonState, ctx *newagentmodel.ConversationContext) string {
var sb strings.Builder
sb.WriteString("请为当前任务生成完成总结。\n")
sb.WriteString(renderStateSummary(state))
sb.WriteString("\n")
sb.WriteString("请基于最近对话和交付工作区,生成一段自然、诚实的完成总结。\n")
if state == nil || !state.HasPlan() {
sb.WriteString("当前没有正式计划,请基于对话历史简要总结本次交互。\n")
if hasExecuteRoughBuildDone(ctx) {
sb.WriteString("当前没有正式计划,但本轮已经完成粗排,请结合粗排补充和任务类详情总结粗排结果,不要把它说成正式完结。\n")
} else {
sb.WriteString("当前没有正式计划,请只概括本次互动,不要编造成果。\n")
}
return strings.TrimSpace(sb.String())
}
current, total := state.PlanProgress()
exhausted := state.Exhausted()
completed := countCompletedPlanSteps(state)
total := len(state.PlanSteps)
if exhausted {
sb.WriteString(fmt.Sprintf("注意:任务因轮次耗尽提前结束,当前进度 %d/%d。\n", current, total))
sb.WriteString("请如实说明已完成未完成的部分,并建议用户如何继续。\n")
} else {
sb.WriteString("所有计划步骤已执行完毕,请总结整体成果。\n")
if state.IsExhaustedTerminal() {
sb.WriteString(fmt.Sprintf("注意:任务因轮次耗尽提前结束,当前已完成 %d/%d。\n", completed, total))
sb.WriteString("请如实说明已完成未完成的部分,并给出一句继续建议。\n")
return strings.TrimSpace(sb.String())
}
if state.IsAborted() {
sb.WriteString(fmt.Sprintf("注意:流程已被主动终止,当前已完成 %d/%d 步。\n", completed, total))
sb.WriteString("请如实说明停在何处,以及用户若想继续应如何衔接。\n")
return strings.TrimSpace(sb.String())
}
sb.WriteString("若计划已正常完成,请概括整体成果;若仍有未完成步骤,也必须如实说明。\n")
return strings.TrimSpace(sb.String())
}

View File

@@ -0,0 +1,137 @@
package newagentprompt
import (
"fmt"
"strings"
newagentmodel "github.com/LoveLosita/smartflow/backend/newAgent/model"
)
// buildDeliverConversationMessage 生成 deliver 节点看到的真实对话视图。
func buildDeliverConversationMessage(ctx *newagentmodel.ConversationContext) string {
return buildConversationHistoryMessage(ctx, "执行对话记录")
}
// buildDeliverRoughBuildPrefix 构造 deliver 在“粗排已完成”场景下的专属前缀。
//
// 职责边界:
// 1. 这里只负责把粗排相关的任务类信息补进 msg3 前缀,不改写交付总结本身;
// 2. 只有在上下文里明确存在 rough_build_done 时才注入,避免普通交付场景被额外信息污染;
// 3. 这段前缀用于补齐第一次粗排没有正式计划时的任务类详情,优先让 deliver 看到 task_class_ids 和任务类约束。
func buildDeliverRoughBuildPrefix(ctx *newagentmodel.ConversationContext, state *newagentmodel.CommonState) string {
if !hasExecuteRoughBuildDone(ctx) {
return ""
}
lines := []string{
"粗排补充信息:",
"- 本轮已经完成粗排,相关任务类已进入 suggested/existing不要把它们说成正式计划。",
}
if taskClassIDs := renderPlanTaskClassIDs(state); taskClassIDs != "" {
lines = append(lines, "- "+taskClassIDs)
}
if taskClassMeta := renderPlanTaskClassMeta(state); taskClassMeta != "" {
lines = append(lines, "任务类详情:")
lines = append(lines, taskClassMeta)
}
if state == nil || !state.HasPlan() {
lines = append(lines, "- 当前没有正式计划,请把这批任务类的粗排结果作为总结重点。")
}
return strings.Join(lines, "\n")
}
// buildDeliverWorkspace 渲染 deliver 节点自己的结果视图。
//
// 设计说明:
// 1. deliver 只需要结果态信息:计划简表、完成进度、收口状态;
// 2. 不再注入工具目录、任务类约束、ReAct 摘要等过程噪声;
// 3. 没有正式计划时,明确退回“只基于对话做总结”。
func buildDeliverWorkspace(state *newagentmodel.CommonState) string {
lines := []string{"交付工作区:"}
if state == nil {
lines = append(lines, "- 当前缺少流程状态,请仅基于最近对话做诚实总结。")
return strings.Join(lines, "\n")
}
lines = append(lines, renderDeliverTerminalSummary(state))
if !state.HasPlan() {
lines = append(lines, "- 当前没有正式计划,请只概括本次互动。")
return strings.Join(lines, "\n")
}
total := len(state.PlanSteps)
completed := countCompletedPlanSteps(state)
lines = append(lines, fmt.Sprintf("- 计划进度:已完成 %d/%d 步。", completed, total))
lines = append(lines, "计划步骤:")
lines = append(lines, renderDeliverStepOutline(state, completed))
return strings.Join(lines, "\n")
}
// renderDeliverTerminalSummary 返回 deliver 节点需要知道的收口状态。
func renderDeliverTerminalSummary(state *newagentmodel.CommonState) string {
if state == nil || !state.HasTerminalOutcome() || state.TerminalOutcome == nil {
return "- 当前没有正式终止结果,请按最近对话和计划进度自然总结。"
}
outcome := state.TerminalOutcome
line := fmt.Sprintf("- 收口状态:%s", outcome.Status)
if stage := strings.TrimSpace(outcome.Stage); stage != "" {
line += fmt.Sprintf(";阶段:%s", stage)
}
if msg := strings.TrimSpace(outcome.UserMessage); msg != "" {
line += fmt.Sprintf(";用户提示:%s", msg)
}
return line
}
// renderDeliverStepOutline 生成 deliver 节点使用的步骤简表。
func renderDeliverStepOutline(state *newagentmodel.CommonState, completed int) string {
if state == nil || len(state.PlanSteps) == 0 {
return "- 暂无。"
}
lines := make([]string, 0, len(state.PlanSteps))
for i, step := range state.PlanSteps {
status := "未完成"
if i < completed {
status = "已完成"
}
content := strings.TrimSpace(step.Content)
if content == "" {
content = "(步骤正文为空)"
}
line := fmt.Sprintf("%d. [%s] %s", i+1, status, content)
if doneWhen := strings.TrimSpace(step.DoneWhen); doneWhen != "" {
line += fmt.Sprintf(" | 完成判定:%s", doneWhen)
}
lines = append(lines, line)
}
return strings.Join(lines, "\n")
}
// countCompletedPlanSteps 统计当前已经完成的计划步骤数。
func countCompletedPlanSteps(state *newagentmodel.CommonState) int {
if state == nil {
return 0
}
total := len(state.PlanSteps)
if total == 0 {
return 0
}
if state.CurrentStep <= 0 {
if state.IsCompleted() {
return total
}
return 0
}
if state.CurrentStep >= total {
return total
}
return state.CurrentStep
}

View File

@@ -12,20 +12,11 @@ import (
)
const (
// executeHistoryKindKey 用于在 history 中打运行态标记,供 prompt 分层识别。
// 说明loop_closed / step_advanced 等边界标记仍由节点层写入,但 prompt 层已不再消费它们——
// 因为 msg1/msg2 已经按"真实对话流 + 当前活跃 ReAct 记录"重构,不再做 msg2→msg1 的归档搬运。
executeHistoryKindKey = "newagent_history_kind"
executeHistoryKindCorrectionUser = "llm_correction_prompt"
executeHistoryKindLoopClosed = "execute_loop_closed"
executeHistoryKindStepAdvanced = "execute_step_advanced"
// executeLoopWindowLimit 控制当轮 ReAct Loop 窗口最多保留多少条记录。
executeLoopWindowLimit = 8
// executeTrimmedObservationText 是重复工具压缩后的 observation 占位文案。
executeTrimmedObservationText = "当前工具调用结果已经被使用过,当前无需使用,为节省上下文空间,已折叠"
// executeConversationTurnLimit 控制 msg1 注入的最大对话轮数user + assistant speak
// 超出时保留最近的条目,早期部分由 ReAct 摘要兜底。
executeConversationTurnLimit = 30
)
type executeToolSchemaDoc struct {
@@ -40,8 +31,6 @@ type executeLoopRecord struct {
Observation string
}
const executeMessage1MaxRunes = 1400
// buildExecuteStageMessages 组装 execute 阶段 4 条消息骨架。
//
// 消息结构(固定):
@@ -82,87 +71,24 @@ func buildExecuteMessage0(stageSystemPrompt string, ctx *newagentmodel.Conversat
return base + "\n\n" + toolCatalog
}
// splitExecuteLoopRecordsByBoundary 按已收口标记拆分归档/活跃 ReAct 记录
//
// 规则:
// 1. 标记之前的记录归档到 msg1
// 2. 标记之后的记录作为活跃 loop 进入 msg2
// 3. 若没有标记,则全部视为活跃记录(兼容旧会话快照)。
func splitExecuteLoopRecordsByBoundary(history []*schema.Message) (archived []executeLoopRecord, active []executeLoopRecord) {
if len(history) == 0 {
return nil, nil
}
boundary := findLatestExecuteBoundaryMarker(history)
if boundary < 0 {
return nil, collectExecuteLoopRecords(history)
}
if boundary > 0 {
archived = collectExecuteLoopRecords(history[:boundary])
}
if boundary+1 < len(history) {
active = collectExecuteLoopRecords(history[boundary+1:])
}
return archived, active
}
func findLatestExecuteBoundaryMarker(history []*schema.Message) int {
for i := len(history) - 1; i >= 0; i-- {
msg := history[i]
if msg == nil || msg.Extra == nil {
continue
}
kind, ok := msg.Extra[executeHistoryKindKey].(string)
if !ok {
continue
}
switch strings.TrimSpace(kind) {
case executeHistoryKindLoopClosed, executeHistoryKindStepAdvanced:
return i
}
}
return -1
}
func trimExecuteMessage1ByBudget(content string) string {
content = strings.TrimSpace(content)
if content == "" {
return ""
}
runes := []rune(content)
if len(runes) <= executeMessage1MaxRunes {
return content
}
if executeMessage1MaxRunes <= 3 {
return string(runes[:executeMessage1MaxRunes])
}
return string(runes[:executeMessage1MaxRunes-3]) + "..."
}
// buildExecuteMessage1V3 负责把真实对话流 + 上一轮 loop 归档并入 msg1并统一做长度裁剪。
// buildExecuteMessage1V3 只渲染"真实对话流 + 阶段锚点"
//
// 改造说明:
// 1. msg1 从人工提炼的摘要变为真实对话流,只注入 user + assistant speak
// 2. tool_call / observation 不在 msg1 中重复(已由 msg2 承载
// 3. 超出 executeConversationTurnLimit 的早期对话不注入,由 ReAct 摘要兜底。
// 1. msg1 只保留 user + assistant speak 组成的真实对话历史,全量注入
// 2. tool_call / observation 一律由 msg2 承载,这里不再重复
// 3. 不再从历史中"归档"上一轮 ReAct 结果到 msg1——归档搬运逻辑已随 splitExecuteLoopRecordsByBoundary 一并移除;
// 4. token 预算由统一压缩层兜底prompt 层不做提前裁剪。
func buildExecuteMessage1V3(ctx *newagentmodel.ConversationContext) string {
lines := []string{"历史上下文:"}
if ctx == nil {
lines = append(lines,
"- 对话历史:暂无。",
"- 阶段锚点:按当前工具事实推进执行。",
"- 历史归档 ReAct 摘要:暂无。",
"- 历史归档 ReAct 窗口:暂无。",
"- 当前循环早期摘要:暂无。",
)
return strings.Join(lines, "\n")
}
history := ctx.HistorySnapshot()
// 注入真实对话流user + assistant speak全量放入不再限制轮数和单条长度。
turns := collectExecuteConversationTurns(history)
turns := collectExecuteConversationTurns(ctx.HistorySnapshot())
if len(turns) == 0 {
lines = append(lines, "- 对话历史:暂无。")
} else {
@@ -180,16 +106,15 @@ func buildExecuteMessage1V3(ctx *newagentmodel.ConversationContext) string {
lines = append(lines, "- 阶段锚点:按当前工具事实推进,不做无依据操作。")
}
archivedLoops, activeLoops := splitExecuteLoopRecordsByBoundary(history)
lines = append(lines, "- 历史归档 ReAct 摘要:"+buildEarlyExecuteReactSummary(archivedLoops, executeLoopWindowLimit))
lines = append(lines, renderArchivedExecuteLoopWindowForMessage1V3(archivedLoops))
lines = append(lines, "- 当前循环早期摘要:"+buildEarlyExecuteReactSummary(activeLoops, executeLoopWindowLimit))
return strings.Join(lines, "\n")
}
// buildExecuteMessage2V3 承载当前活跃 loop 的全部记录。
// 若是新一轮刚开始(活跃 loop 为空),明确返回已清空状态。
// 不再限制窗口大小token 预算由 execute 层统一管理。
// buildExecuteMessage2V3 承载当前会话中全部 ReAct Loop 记录。
//
// 改造说明:
// 1. 不再按 execute_loop_closed / execute_step_advanced 边界切分"归档/活跃"两段;
// 2. 直接从 history 提取全部 assistant tool_call + 对应 observation 作为当前 Loop 视图;
// 3. 新一轮刚开始(尚未产生 tool_call时返回明确占位方便模型识别"干净起点"。
func buildExecuteMessage2V3(ctx *newagentmodel.ConversationContext) string {
lines := []string{"当轮 ReAct Loop 记录:"}
if ctx == nil {
@@ -197,31 +122,13 @@ func buildExecuteMessage2V3(ctx *newagentmodel.ConversationContext) string {
return strings.Join(lines, "\n")
}
_, activeLoops := splitExecuteLoopRecordsByBoundary(ctx.HistorySnapshot())
if len(activeLoops) == 0 {
loops := collectExecuteLoopRecords(ctx.HistorySnapshot())
if len(loops) == 0 {
lines = append(lines, "- 已清空(新一轮 loop 准备中)。")
return strings.Join(lines, "\n")
}
// 全量放入,不再限制窗口大小
for i, loop := range activeLoops {
lines = append(lines, fmt.Sprintf("%d) thought/reason%s", i+1, loop.Thought))
lines = append(lines, fmt.Sprintf(" tool_call%s", renderExecuteToolCallText(loop.ToolName, loop.ToolArgs)))
lines = append(lines, fmt.Sprintf(" observation%s", loop.Observation))
}
return strings.Join(lines, "\n")
}
func renderArchivedExecuteLoopWindowForMessage1V3(records []executeLoopRecord) string {
if len(records) == 0 {
return "- 历史归档 ReAct 窗口:暂无。"
}
windowLoops := tailExecuteLoops(records, executeLoopWindowLimit)
windowLoops = compressExecuteLoopObservationsByTool(windowLoops)
lines := []string{"历史归档 ReAct 窗口(由上一轮 msg2 并入):"}
for i, loop := range windowLoops {
for i, loop := range loops {
lines = append(lines, fmt.Sprintf("%d) thought/reason%s", i+1, loop.Thought))
lines = append(lines, fmt.Sprintf(" tool_call%s", renderExecuteToolCallText(loop.ToolName, loop.ToolArgs)))
lines = append(lines, fmt.Sprintf(" observation%s", loop.Observation))
@@ -525,51 +432,6 @@ func findExecuteThoughtBefore(history []*schema.Message, index int) string {
return "(未记录)"
}
func tailExecuteLoops(records []executeLoopRecord, limit int) []executeLoopRecord {
if len(records) == 0 {
return nil
}
if limit <= 0 || len(records) <= limit {
result := make([]executeLoopRecord, len(records))
copy(result, records)
return result
}
result := make([]executeLoopRecord, limit)
copy(result, records[len(records)-limit:])
return result
}
// compressExecuteLoopObservationsByTool 对窗口内重复工具做 observation 压缩。
func compressExecuteLoopObservationsByTool(records []executeLoopRecord) []executeLoopRecord {
if len(records) == 0 {
return records
}
latestIndexByTool := make(map[string]int, len(records))
for i := len(records) - 1; i >= 0; i-- {
key := strings.ToLower(strings.TrimSpace(records[i].ToolName))
if key == "" {
key = "unknown_tool"
}
if _, exists := latestIndexByTool[key]; !exists {
latestIndexByTool[key] = i
}
}
result := make([]executeLoopRecord, len(records))
copy(result, records)
for i := range result {
key := strings.ToLower(strings.TrimSpace(result[i].ToolName))
if key == "" {
key = "unknown_tool"
}
if latestIndexByTool[key] != i {
result[i].Observation = executeTrimmedObservationText
}
}
return result
}
func renderExecuteToolCallText(toolName, toolArgs string) string {
toolName = strings.TrimSpace(toolName)
if toolName == "" {
@@ -582,38 +444,6 @@ func renderExecuteToolCallText(toolName, toolArgs string) string {
return toolName + "(" + toolArgs + ")"
}
func buildEarlyExecuteReactSummary(records []executeLoopRecord, windowLimit int) string {
if len(records) == 0 {
return "暂无。"
}
if len(records) <= windowLimit {
return "无(当前窗口已覆盖全部 ReAct 记录)。"
}
early := records[:len(records)-windowLimit]
toolCounts := make(map[string]int, len(early))
for _, record := range early {
key := strings.TrimSpace(record.ToolName)
if key == "" {
key = "unknown_tool"
}
toolCounts[key]++
}
names := make([]string, 0, len(toolCounts))
for name := range toolCounts {
names = append(names, name)
}
sort.Strings(names)
parts := make([]string, 0, len(names))
for _, name := range names {
parts = append(parts, fmt.Sprintf("%s×%d", name, toolCounts[name]))
}
return fmt.Sprintf("已折叠 %d 条旧记录,涉及:%s。", len(early), strings.Join(parts, "、"))
}
func hasExecuteRoughBuildDone(ctx *newagentmodel.ConversationContext) bool {
if ctx == nil {
return false
@@ -725,3 +555,12 @@ func renderExecuteTaskClassIDs(state *newagentmodel.CommonState) string {
}
return fmt.Sprintf("task_class_ids=[%s]", strings.Join(parts, ","))
}
// renderExecuteMemoryContext 提取 execute 阶段要注入 msg3 的记忆文本。
//
// 1. 只读取统一的 memory_context避免把其他 pinned block 误塞进 prompt。
// 2. 为空时直接返回空串,保持 msg3 干净。
// 3. 复用统一记忆渲染逻辑,保证各阶段记忆入口一致。
func renderExecuteMemoryContext(ctx *newagentmodel.ConversationContext) string {
return renderUnifiedMemoryContext(ctx)
}

View File

@@ -1,31 +0,0 @@
package newagentprompt
import (
"strings"
newagentmodel "github.com/LoveLosita/smartflow/backend/newAgent/model"
)
const executeMemoryContextKey = "memory_context"
// renderExecuteMemoryContext 提取 Execute 阶段需要补充到 msg3 的记忆文本。
//
// 步骤化说明:
// 1. 只白名单消费 memory_context避免把 execution_context / current_step 等 Execute 自有块再次注入;
// 2. 若 block 不存在或正文为空,直接返回空串,不给 msg3 留空段;
// 3. 这里不重新渲染记忆,只消费 agentsvc 已经产出的最终文本,保证所有阶段口径一致。
func renderExecuteMemoryContext(ctx *newagentmodel.ConversationContext) string {
if ctx == nil {
return ""
}
block, ok := ctx.PinnedBlockByKey(executeMemoryContextKey)
if !ok {
return ""
}
content := strings.TrimSpace(block.Content)
if content == "" {
return ""
}
return content
}

View File

@@ -2,7 +2,6 @@ package newagentprompt
import (
"fmt"
"strconv"
"strings"
newagentmodel "github.com/LoveLosita/smartflow/backend/newAgent/model"
@@ -49,14 +48,19 @@ func BuildPlanSystemPrompt() string {
// BuildPlanMessages 组装规划阶段的 messages。
//
// 职责边界:
// 1. 负责把 state + context 收敛成规划阶段模型输入;
// 2. 负责把置顶上下文和工具摘要放在 history 前面,降低模型跑偏概率
// 3. 不负责解析模型输出,也不负责判断规划质量
// 1. 负责把 state + context 收敛成统一 4 段式规划阶段模型输入;
// 2. 负责解析模型输出,也不负责判断规划质量
// 3. msg3 中的状态文本由本函数显式传入,确保统一骨架下仍能看到完整计划与阶段信息
func BuildPlanMessages(state *newagentmodel.CommonState, ctx *newagentmodel.ConversationContext, userInput string) []*schema.Message {
return buildStageMessages(
BuildPlanSystemPrompt(),
return buildUnifiedStageMessages(
ctx,
BuildPlanUserPrompt(state, userInput),
StageMessagesConfig{
SystemPrompt: BuildPlanSystemPrompt(),
Msg1Content: buildPlanConversationMessage(ctx),
Msg2Content: buildPlanWorkspace(state),
Msg3Suffix: BuildPlanUserPrompt(state, userInput),
Msg3Role: schema.User,
},
)
}
@@ -64,21 +68,9 @@ func BuildPlanMessages(state *newagentmodel.CommonState, ctx *newagentmodel.Conv
func BuildPlanUserPrompt(state *newagentmodel.CommonState, userInput string) string {
var sb strings.Builder
sb.WriteString("请继续当前任务的规划阶段。\n")
sb.WriteString(renderStateSummary(state))
sb.WriteString("\n")
sb.WriteString("本轮目标:围绕当前任务继续规划,直到形成一份稳定、可执行的自然语言 plan或在信息不足时明确追问用户。\n\n")
sb.WriteString("请继续当前任务的规划阶段,严格输出 JSON。\n")
sb.WriteString("目标:围绕最近对话和规划工作区信息,产出一份稳定、可执行的自然语言计划;若关键信息不足,请明确 ask_user。\n\n")
sb.WriteString(BuildPlanDecisionContractText())
sb.WriteString("\n")
if state != nil && len(state.TaskClassIDs) > 0 {
parts := make([]string, len(state.TaskClassIDs))
for i, id := range state.TaskClassIDs {
parts[i] = strconv.Itoa(id)
}
sb.WriteString(fmt.Sprintf("\n本次排课请求涉及的任务类 ID前端传入[%s]\n", strings.Join(parts, ", ")))
sb.WriteString("规划时请结合上述任务类 ID 判断是否需要粗排needs_rough_build并在 plan_steps 中体现排课意图。\n")
}
trimmedInput := strings.TrimSpace(userInput)
if trimmedInput != "" {

View File

@@ -0,0 +1,133 @@
package newagentprompt
import (
"fmt"
"strconv"
"strings"
newagentmodel "github.com/LoveLosita/smartflow/backend/newAgent/model"
)
// buildPlanConversationMessage 生成 plan 节点看到的真实对话视图。
func buildPlanConversationMessage(ctx *newagentmodel.ConversationContext) string {
return buildConversationHistoryMessage(ctx, "规划参考对话")
}
// buildPlanWorkspace 渲染 plan 节点自己的工作区。
//
// 设计说明:
// 1. 这里只保留“规划真正需要知道的东西”已有计划、当前步骤、task_class_ids、任务类约束
// 2. 不再复用通用胖状态摘要,避免把 execute / deliver 无关状态一起塞给 plan
// 3. 若当前没有正式计划,则明确告诉模型“从零开始规划”,避免继续误沿用旧上下文。
func buildPlanWorkspace(state *newagentmodel.CommonState) string {
lines := []string{"规划工作区:"}
if state == nil {
lines = append(lines, "- 当前缺少流程状态,请主要依据最近对话与本轮输入继续规划。")
return strings.Join(lines, "\n")
}
if !state.HasPlan() {
lines = append(lines, "- 当前还没有正式计划。")
} else {
lines = append(lines, fmt.Sprintf("- 已有计划:共 %d 步。", len(state.PlanSteps)))
lines = append(lines, renderPlanCurrentStepSummary(state))
lines = append(lines, "计划简表:")
lines = append(lines, renderPlanStepOutline(state.PlanSteps))
}
if taskClassIDs := renderPlanTaskClassIDs(state); taskClassIDs != "" {
lines = append(lines, "- "+taskClassIDs)
}
if taskClassMeta := renderPlanTaskClassMeta(state); taskClassMeta != "" {
lines = append(lines, "任务类约束:")
lines = append(lines, taskClassMeta)
}
return strings.Join(lines, "\n")
}
// renderPlanCurrentStepSummary 返回 plan 节点需要知道的当前步骤进度。
func renderPlanCurrentStepSummary(state *newagentmodel.CommonState) string {
if state == nil || !state.HasPlan() {
return "- 当前步骤:暂无。"
}
current, total := state.PlanProgress()
step, ok := state.CurrentPlanStep()
if !ok {
return fmt.Sprintf("- 当前步骤:计划共 %d 步,当前没有可继续沿用的有效步骤。", total)
}
content := strings.TrimSpace(step.Content)
if content == "" {
content = "(当前步骤正文为空)"
}
summary := fmt.Sprintf("- 当前步骤:第 %d/%d 步,%s", current, total, content)
if doneWhen := strings.TrimSpace(step.DoneWhen); doneWhen != "" {
summary += fmt.Sprintf(";完成判定:%s", doneWhen)
}
return summary
}
// renderPlanStepOutline 将完整计划压成 plan 节点可读的简表。
func renderPlanStepOutline(steps []newagentmodel.PlanStep) string {
if len(steps) == 0 {
return "- 暂无。"
}
lines := make([]string, 0, len(steps))
for i, step := range steps {
content := strings.TrimSpace(step.Content)
if content == "" {
content = "(步骤正文为空)"
}
line := fmt.Sprintf("%d. %s", i+1, content)
if doneWhen := strings.TrimSpace(step.DoneWhen); doneWhen != "" {
line += fmt.Sprintf(" | 完成判定:%s", doneWhen)
}
lines = append(lines, line)
}
return strings.Join(lines, "\n")
}
// renderPlanTaskClassIDs 返回批量排课场景下的 task_class_ids 简表。
func renderPlanTaskClassIDs(state *newagentmodel.CommonState) string {
if state == nil || len(state.TaskClassIDs) == 0 {
return ""
}
parts := make([]string, len(state.TaskClassIDs))
for i, id := range state.TaskClassIDs {
parts[i] = strconv.Itoa(id)
}
return fmt.Sprintf("task_class_ids=[%s]", strings.Join(parts, ", "))
}
// renderPlanTaskClassMeta 返回 plan 节点真正需要看的任务类边界。
//
// 说明:
// 1. 这里只保留名称、策略、总时段、日期范围这类规划相关信息;
// 2. 不再把所有字段原样平铺,避免工作区过胖;
// 3. 若某项字段为空,则直接省略,不制造噪声。
func renderPlanTaskClassMeta(state *newagentmodel.CommonState) string {
if state == nil || len(state.TaskClasses) == 0 {
return ""
}
lines := make([]string, 0, len(state.TaskClasses))
for _, tc := range state.TaskClasses {
line := fmt.Sprintf("- [ID=%d] %s", tc.ID, strings.TrimSpace(tc.Name))
if strategy := strings.TrimSpace(tc.Strategy); strategy != "" {
line += fmt.Sprintf(";策略:%s", strategy)
}
if tc.TotalSlots > 0 {
line += fmt.Sprintf(";总时段预算:%d", tc.TotalSlots)
}
if tc.StartDate != "" || tc.EndDate != "" {
line += fmt.Sprintf(";日期范围:%s ~ %s", tc.StartDate, tc.EndDate)
}
lines = append(lines, line)
}
return strings.Join(lines, "\n")
}

View File

@@ -0,0 +1,212 @@
package newagentprompt
import (
"strings"
newagentmodel "github.com/LoveLosita/smartflow/backend/newAgent/model"
"github.com/cloudwego/eino/schema"
)
// ConversationTurn 表示对话历史中的一轮自然语言交互。
//
// 职责边界:
// 1. 这里只承载 user 与 assistant speak不承载 tool_call 和 tool observation
// 2. 供 chat / plan / deliver 等节点复用,避免各节点重复写一套提取逻辑;
// 3. 不负责裁剪长度,长度预算统一交给压缩层处理。
type ConversationTurn struct {
Role string
Content string
}
// StageMessagesConfig 描述统一四段式骨架下,各节点自行提供的内容块。
//
// 设计目标:
// 1. 统一层只负责“四条消息怎么拼”,不再替节点决定“每条消息里该放什么”;
// 2. Msg1 / Msg2 / Msg3Prefix / Msg3Suffix 都由节点自己渲染,避免 chat / plan / deliver 继续套 execute 的内容模板;
// 3. memory_context 仍由统一层单入口注入到 msg3避免多处重复注入。
type StageMessagesConfig struct {
// SystemPrompt 是节点自己的系统提示词。
SystemPrompt string
// Msg1Content 是第 2 条 assistant 消息,通常放“节点想看的历史视图”。
Msg1Content string
// Msg2Content 是第 3 条 assistant 消息,通常放“节点自己的工作区/补充约束”。
Msg2Content string
// Msg3Prefix 是第 4 条消息中位于 memory_context 之前的内容。
// 常见放法:阶段状态、规划工作区摘要、交付收口约束等。
Msg3Prefix string
// Msg3Suffix 是第 4 条消息中位于 memory_context 之后的内容。
// 对 user-role 节点来说,这里通常放最终用户指令,保证“用户输入收尾”。
Msg3Suffix string
// Msg3Role 指定第 4 条消息的角色。
// Execute 继续使用 system其余节点一般使用 user。
Msg3Role schema.RoleType
}
// buildUnifiedStageMessages 组装统一 4 段式消息骨架。
//
// 固定布局:
// 1. msg0(system):系统规则 + 阶段规则 + 工具简表;
// 2. msg1(assistant):节点自定义的历史视图;
// 3. msg2(assistant):节点自定义的工作区;
// 4. msg3(user/system):节点自定义前后缀 + 统一 memory_context。
func buildUnifiedStageMessages(
ctx *newagentmodel.ConversationContext,
config StageMessagesConfig,
) []*schema.Message {
msg0 := buildUnifiedMsg0(config.SystemPrompt, ctx)
msg1 := buildUnifiedMsg1(config.Msg1Content)
msg2 := buildUnifiedMsg2(config.Msg2Content)
msg3 := buildUnifiedMsg3(ctx, config)
return []*schema.Message{
schema.SystemMessage(msg0),
{Role: schema.Assistant, Content: msg1},
{Role: schema.Assistant, Content: msg2},
buildUnifiedMsg3Message(msg3, config.Msg3Role),
}
}
// buildUnifiedMsg3Message 根据配置决定第 4 条消息的角色。
func buildUnifiedMsg3Message(content string, role schema.RoleType) *schema.Message {
if role == schema.User {
return schema.UserMessage(content)
}
return schema.SystemMessage(content)
}
// buildUnifiedMsg0 合并系统提示 + 工具简表,生成 msg0。
//
// 步骤化说明:
// 1. 先合并基础系统提示与节点系统提示,保证模型身份稳定;
// 2. 若当前节点注入了工具 schema则附加紧凑工具目录
// 3. 若两部分都为空,则回退到最小兜底提示,避免出现空消息。
func buildUnifiedMsg0(stageSystemPrompt string, ctx *newagentmodel.ConversationContext) string {
base := strings.TrimSpace(mergeSystemPrompts(ctx, stageSystemPrompt))
if base == "" {
base = "你是 SmartMate 助手,请继续当前阶段。"
}
toolCatalog := renderExecuteToolCatalogCompact(ctx)
if toolCatalog == "" {
return base
}
return base + "\n\n" + toolCatalog
}
// buildUnifiedMsg1 返回节点自行提供的历史视图。
//
// 说明:
// 1. 统一层不再内置 execute 风格的 ReAct 摘要;
// 2. 节点若未传入内容,则回退到最小占位,保证四段结构稳定;
// 3. 压缩层仍会统一统计和压缩这条消息。
func buildUnifiedMsg1(content string) string {
content = strings.TrimSpace(content)
if content != "" {
return content
}
return "历史上下文:暂无。"
}
// buildUnifiedMsg2 返回节点自行提供的工作区。
//
// 说明:
// 1. 非 execute 节点也允许有自己的 msg2不再被统一层硬塞“暂无”语义
// 2. 若节点暂时没有额外工作区,则回退到最小占位,保证结构稳定。
func buildUnifiedMsg2(content string) string {
content = strings.TrimSpace(content)
if content != "" {
return content
}
return "阶段工作区:暂无。"
}
// buildUnifiedMsg3 统一拼装 msg3前缀 + memory_context + 后缀。
//
// 步骤化说明:
// 1. 前缀由节点决定,适合放轻量状态或阶段约束;
// 2. memory_context 只在这里注入一次,避免 pinned block 多入口重复出现;
// 3. 后缀由节点决定。对于 user-role 节点,通常把最终用户指令放在这里,保证消息末尾仍是用户输入。
func buildUnifiedMsg3(ctx *newagentmodel.ConversationContext, config StageMessagesConfig) string {
var sections []string
if prefix := strings.TrimSpace(config.Msg3Prefix); prefix != "" {
sections = append(sections, prefix)
}
if memoryText := renderUnifiedMemoryContext(ctx); memoryText != "" {
sections = append(sections, "相关记忆(仅在确有帮助时参考,不要机械复述):\n"+memoryText)
}
if suffix := strings.TrimSpace(config.Msg3Suffix); suffix != "" {
sections = append(sections, suffix)
}
if len(sections) == 0 {
return "请继续当前阶段。"
}
return strings.Join(sections, "\n\n")
}
// renderUnifiedMemoryContext 提取需要补充到 msg3 的记忆文本。
//
// 步骤化说明:
// 1. 只消费 memory_context避免把 execution_context / current_step 等阶段专属块混回 prompt
// 2. block 不存在或正文为空时直接返回空串;
// 3. 这里只读取 agentsvc 已经产出的最终文本,不在这里重新拼装记忆。
func renderUnifiedMemoryContext(ctx *newagentmodel.ConversationContext) string {
if ctx == nil {
return ""
}
block, ok := ctx.PinnedBlockByKey("memory_context")
if !ok {
return ""
}
content := strings.TrimSpace(block.Content)
if content == "" {
return ""
}
return content
}
// CollectConversationTurns 从历史消息中提取 user + assistant speak 对话流。
//
// 提取规则:
// 1. 只保留 user 消息(排除 correction prompt和 assistant 纯文本消息;
// 2. assistant tool_call 消息与 tool observation 消息不纳入“真实对话”;
// 3. 返回顺序保持与原始 history 一致。
func CollectConversationTurns(history []*schema.Message) []ConversationTurn {
if len(history) == 0 {
return nil
}
turns := make([]ConversationTurn, 0, len(history))
for _, msg := range history {
if msg == nil {
continue
}
text := strings.TrimSpace(msg.Content)
if text == "" {
continue
}
switch msg.Role {
case schema.User:
// 1. 跳过后端注入的 correction prompt避免把纠错文案误判为用户真实意图。
if isExecuteCorrectionPrompt(msg) {
continue
}
turns = append(turns, ConversationTurn{Role: "user", Content: text})
case schema.Assistant:
// 2. 跳过工具调用消息,只保留真正面向用户的 speak/答复。
if len(msg.ToolCalls) > 0 {
continue
}
turns = append(turns, ConversationTurn{Role: "assistant", Content: text})
}
}
return turns
}