后端: 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 仓库:无
286 lines
10 KiB
Go
286 lines
10 KiB
Go
package newagentnode
|
||
|
||
import (
|
||
"context"
|
||
"fmt"
|
||
"strings"
|
||
"time"
|
||
|
||
"github.com/google/uuid"
|
||
|
||
infrallm "github.com/LoveLosita/smartflow/backend/infra/llm"
|
||
newagentmodel "github.com/LoveLosita/smartflow/backend/newAgent/model"
|
||
newagentprompt "github.com/LoveLosita/smartflow/backend/newAgent/prompt"
|
||
newagentstream "github.com/LoveLosita/smartflow/backend/newAgent/stream"
|
||
"github.com/cloudwego/eino/schema"
|
||
)
|
||
|
||
const (
|
||
planStageName = "plan"
|
||
planStatusBlockID = "plan.status"
|
||
planSpeakBlockID = "plan.speak"
|
||
planSummaryBlockID = "plan.summary"
|
||
planPinnedKey = "current_plan"
|
||
planCurrentStepKey = "current_step"
|
||
planCurrentStepTitle = "当前步骤"
|
||
planFullPlanTitle = "当前完整计划"
|
||
)
|
||
|
||
// PlanNodeInput 描述单轮规划节点执行所需的最小依赖。
|
||
type PlanNodeInput struct {
|
||
RuntimeState *newagentmodel.AgentRuntimeState
|
||
ConversationContext *newagentmodel.ConversationContext
|
||
UserInput string
|
||
Client *infrallm.Client
|
||
ChunkEmitter *newagentstream.ChunkEmitter
|
||
ResumeNode string
|
||
AlwaysExecute bool // true 时计划生成后自动确认,不进入 confirm 节点
|
||
ThinkingEnabled bool // 是否开启 thinking,由 config.yaml 的 agent.thinking.plan 注入
|
||
CompactionStore newagentmodel.CompactionStore // 上下文压缩持久化
|
||
PersistVisibleMessage newagentmodel.PersistVisibleMessageFunc
|
||
}
|
||
|
||
// RunPlanNode 执行一轮规划节点逻辑。
|
||
//
|
||
// 步骤说明:
|
||
// 1. 先校验最小依赖,并推送一条"正在规划"的状态,避免用户空等;
|
||
// 2. 单轮深度规划:开 thinking、无 token 上限,让 LLM 一步到位产出完整计划;
|
||
// 3. 若模型先对用户说了话,则先把 speak 伪流式推给前端,并写回 history;
|
||
// 4. 最后按 action 推进流程:
|
||
// 4.1 continue:继续停留在 planning;
|
||
// 4.2 ask_user:打开 pending interaction,后续交给 interrupt 收口;
|
||
// 4.3 plan_done:固化完整计划,刷新 pinned context,并进入 waiting_confirm。
|
||
func RunPlanNode(ctx context.Context, input PlanNodeInput) error {
|
||
runtimeState, conversationContext, emitter, err := preparePlanNodeInput(input)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
flowState := runtimeState.EnsureCommonState()
|
||
|
||
// 1. 先发一条阶段状态,让前端知道当前已经进入规划环节。
|
||
if err := emitter.EmitStatus(
|
||
planStatusBlockID,
|
||
planStageName,
|
||
"planning",
|
||
"正在梳理目标并补全执行计划。",
|
||
false,
|
||
); err != nil {
|
||
return fmt.Errorf("规划阶段状态推送失败: %w", err)
|
||
}
|
||
|
||
// 2. 构造本轮规划输入。
|
||
messages := newagentprompt.BuildPlanMessages(flowState, conversationContext, input.UserInput)
|
||
messages = compactUnifiedMessagesIfNeeded(ctx, messages, UnifiedCompactInput{
|
||
Client: input.Client,
|
||
CompactionStore: input.CompactionStore,
|
||
FlowState: flowState,
|
||
Emitter: emitter,
|
||
StageName: planStageName,
|
||
StatusBlockID: planStatusBlockID,
|
||
})
|
||
logNodeLLMContext(planStageName, "planning", flowState, messages)
|
||
|
||
// 3. 单轮深度规划:由配置决定是否开启 thinking,不做 token 上限约束。
|
||
decision, rawResult, err := infrallm.GenerateJSON[newagentmodel.PlanDecision](
|
||
ctx,
|
||
input.Client,
|
||
messages,
|
||
infrallm.GenerateOptions{
|
||
Temperature: 0.2,
|
||
Thinking: resolveThinkingMode(input.ThinkingEnabled),
|
||
Metadata: map[string]any{
|
||
"stage": planStageName,
|
||
"phase": "planning",
|
||
},
|
||
},
|
||
)
|
||
if err != nil {
|
||
if rawResult != nil && strings.TrimSpace(rawResult.Text) != "" {
|
||
return fmt.Errorf("规划解析失败,原始输出=%s,错误=%w", strings.TrimSpace(rawResult.Text), err)
|
||
}
|
||
return fmt.Errorf("规划阶段模型调用失败: %w", err)
|
||
}
|
||
if err := decision.Validate(); err != nil {
|
||
return fmt.Errorf("规划决策不合法: %w", err)
|
||
}
|
||
|
||
// 4. 若模型先对用户说了话,且不是 ask_user(ask_user 交给 interrupt 收口),则先以伪流式推送,再写回 history。
|
||
if strings.TrimSpace(decision.Speak) != "" && decision.Action != newagentmodel.PlanActionAskUser {
|
||
msg := schema.AssistantMessage(decision.Speak, nil)
|
||
if err := emitter.EmitPseudoAssistantText(
|
||
ctx,
|
||
planSpeakBlockID,
|
||
planStageName,
|
||
decision.Speak,
|
||
newagentstream.DefaultPseudoStreamOptions(),
|
||
); err != nil {
|
||
return fmt.Errorf("规划文案推送失败: %w", err)
|
||
}
|
||
conversationContext.AppendHistory(msg)
|
||
persistVisibleAssistantMessage(ctx, input.PersistVisibleMessage, flowState, msg)
|
||
}
|
||
|
||
// 5. 按规划动作推进流程状态。
|
||
switch decision.Action {
|
||
case newagentmodel.PlanActionContinue:
|
||
flowState.Phase = newagentmodel.PhasePlanning
|
||
return nil
|
||
case newagentmodel.PlanActionAskUser:
|
||
question := resolvePlanAskUserText(decision)
|
||
runtimeState.OpenAskUserInteraction(uuid.NewString(), question, strings.TrimSpace(input.ResumeNode))
|
||
return nil
|
||
case newagentmodel.PlanActionDone:
|
||
// 4.1 直接把结构化 PlanStep 固化到 CommonState,避免 state 层丢失 done_when。
|
||
// 4.2 再把完整自然语言计划写入 pinned context,保证后续 execute 优先看到。
|
||
// 4.3 若 LLM 识别到批量排课意图,把 NeedsRoughBuild 标记写入 CommonState,
|
||
// Confirm 节点后的路由会据此决定是否跳入 RoughBuild 节点。
|
||
// 4.4 最后进入 waiting_confirm,等待用户确认整体计划。
|
||
flowState.FinishPlan(decision.PlanSteps)
|
||
writePlanPinnedBlocks(conversationContext, decision.PlanSteps)
|
||
if decision.NeedsRoughBuild {
|
||
flowState.NeedsRoughBuild = true
|
||
// 以 LLM 决策中的 task_class_ids 为准(若非空则覆盖前端传入值)。
|
||
if len(decision.TaskClassIDs) > 0 {
|
||
flowState.TaskClassIDs = decision.TaskClassIDs
|
||
}
|
||
}
|
||
// always_execute 开启时,计划层跳过确认闸门,直接进入执行阶段。
|
||
// 这样可以与 Execute 节点的"写工具跳过确认"语义保持一致。
|
||
if input.AlwaysExecute {
|
||
// 1. 自动执行模式不会经过 Confirm 卡片,因此这里先把完整计划明确展示给用户。
|
||
// 2. 摘要格式复用 Confirm 节点,保证"手动确认"和"自动执行"两条链路文案一致。
|
||
// 3. 推流后同步写入历史,确保后续 Execute 阶段的上下文也能看到这份计划。
|
||
summary := strings.TrimSpace(buildPlanSummary(decision.PlanSteps))
|
||
if summary != "" {
|
||
msg := schema.AssistantMessage(summary, nil)
|
||
if err := emitter.EmitPseudoAssistantText(
|
||
ctx,
|
||
planSummaryBlockID,
|
||
planStageName,
|
||
summary,
|
||
newagentstream.DefaultPseudoStreamOptions(),
|
||
); err != nil {
|
||
return fmt.Errorf("自动执行前计划摘要推送失败: %w", err)
|
||
}
|
||
conversationContext.AppendHistory(msg)
|
||
persistVisibleAssistantMessage(ctx, input.PersistVisibleMessage, flowState, msg)
|
||
}
|
||
|
||
flowState.ConfirmPlan()
|
||
_ = emitter.EmitStatus(
|
||
planStatusBlockID,
|
||
planStageName,
|
||
"plan_auto_confirmed",
|
||
"计划已自动确认,开始执行。",
|
||
false,
|
||
)
|
||
}
|
||
return nil
|
||
default:
|
||
// 1. LLM 输出了不支持的 action,不应直接报错终止,而应给它修正机会。
|
||
// 2. 使用通用修正函数追加错误反馈,让 Graph 继续循环。
|
||
// 3. LLM 下一轮会看到错误反馈并修正自己的输出。
|
||
llmOutput := decision.Speak
|
||
if strings.TrimSpace(llmOutput) == "" {
|
||
llmOutput = decision.Reason
|
||
}
|
||
AppendLLMCorrectionWithHint(
|
||
conversationContext,
|
||
llmOutput,
|
||
fmt.Sprintf("你输出的 action \"%s\" 不是合法的执行动作。", decision.Action),
|
||
"合法的 action 包括:continue(继续当前步骤)、ask_user(追问用户)、next_plan(推进到下一步)、done(任务完成)。",
|
||
)
|
||
return nil
|
||
}
|
||
}
|
||
|
||
func preparePlanNodeInput(input PlanNodeInput) (*newagentmodel.AgentRuntimeState, *newagentmodel.ConversationContext, *newagentstream.ChunkEmitter, error) {
|
||
if input.RuntimeState == nil {
|
||
return nil, nil, nil, fmt.Errorf("plan node: runtime state 不能为空")
|
||
}
|
||
if input.Client == nil {
|
||
return nil, nil, nil, fmt.Errorf("plan node: plan client 未注入")
|
||
}
|
||
|
||
input.RuntimeState.EnsureCommonState()
|
||
if input.ConversationContext == nil {
|
||
input.ConversationContext = newagentmodel.NewConversationContext("")
|
||
}
|
||
if input.ChunkEmitter == nil {
|
||
input.ChunkEmitter = newagentstream.NewChunkEmitter(newagentstream.NoopPayloadEmitter(), "", "", time.Now().Unix())
|
||
}
|
||
return input.RuntimeState, input.ConversationContext, input.ChunkEmitter, nil
|
||
}
|
||
|
||
func resolvePlanAskUserText(decision *newagentmodel.PlanDecision) string {
|
||
if decision == nil {
|
||
return "我还缺一点关键信息,想先向你确认一下。"
|
||
}
|
||
if strings.TrimSpace(decision.Speak) != "" {
|
||
return strings.TrimSpace(decision.Speak)
|
||
}
|
||
if strings.TrimSpace(decision.Reason) != "" {
|
||
return strings.TrimSpace(decision.Reason)
|
||
}
|
||
return "我还缺一点关键信息,想先向你确认一下。"
|
||
}
|
||
|
||
func writePlanPinnedBlocks(ctx *newagentmodel.ConversationContext, steps []newagentmodel.PlanStep) {
|
||
if ctx == nil {
|
||
return
|
||
}
|
||
|
||
fullPlanText := buildPinnedPlanText(steps)
|
||
if strings.TrimSpace(fullPlanText) != "" {
|
||
ctx.UpsertPinnedBlock(newagentmodel.ContextBlock{
|
||
Key: planPinnedKey,
|
||
Title: planFullPlanTitle,
|
||
Content: fullPlanText,
|
||
})
|
||
}
|
||
|
||
if len(steps) == 0 {
|
||
return
|
||
}
|
||
|
||
firstStep := strings.TrimSpace(steps[0].Content)
|
||
if strings.TrimSpace(steps[0].DoneWhen) != "" {
|
||
firstStep = fmt.Sprintf("%s\n完成判定:%s", firstStep, strings.TrimSpace(steps[0].DoneWhen))
|
||
}
|
||
ctx.UpsertPinnedBlock(newagentmodel.ContextBlock{
|
||
Key: planCurrentStepKey,
|
||
Title: planCurrentStepTitle,
|
||
Content: firstStep,
|
||
})
|
||
}
|
||
|
||
func buildPinnedPlanText(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 == "" {
|
||
continue
|
||
}
|
||
|
||
line := fmt.Sprintf("%d. %s", i+1, content)
|
||
if strings.TrimSpace(step.DoneWhen) != "" {
|
||
line += fmt.Sprintf("\n完成判定:%s", strings.TrimSpace(step.DoneWhen))
|
||
}
|
||
lines = append(lines, line)
|
||
}
|
||
return strings.TrimSpace(strings.Join(lines, "\n\n"))
|
||
}
|
||
|
||
// resolveThinkingMode 根据配置布尔值返回对应的 ThinkingMode。
|
||
// 供 plan / execute / deliver 节点统一使用。
|
||
func resolveThinkingMode(enabled bool) infrallm.ThinkingMode {
|
||
if enabled {
|
||
return infrallm.ThinkingModeEnabled
|
||
}
|
||
return infrallm.ThinkingModeDisabled
|
||
}
|