Version: 0.9.2.dev.260406

后端:
   1.Chat 四路由升级(二分类 chat/task → 四路由 direct_reply/execute/deep_answer/plan)
     - 新建model/chat_contract.go:路由决策模型,含 NeedsRoughBuild 粗排标记
     - 更新node/chat.go:四路由分流;新增 deep_answer 深度回答路径(二次 LLM 开 thinking)
     - 更新prompt/chat.go:意图分类 prompt 升级为四路由 prompt;新增 deep_answer prompt
   2.粗排节点(RoughBuild)全链路
     - 新建node/rough_build.go:粗排节点,调用注入的算法函数,结果写入 ScheduleState 后进 Execute 微调
     - 更新graph/common_graph.go:注册 RoughBuild 节点;Chat/Confirm 后可路由至粗排
     - 更新model/graph_run_state.go:新增 RoughBuildPlacement/RoughBuildFunc 类型;Deps 注入入口
     - 更新model/plan_contract.go:PlanDecision 新增 NeedsRoughBuild/TaskClassIDs 字段
     - 更新node/plan.go:plan_done 时写入粗排标记和 TaskClassIDs
   3.任务类约束元数据(TaskClassMeta)贯穿 prompt → tools → 持久化
     - 更新tools/state.go:新增 TaskClassMeta;ScheduleState.TaskClasses;ScheduleTask.TaskClassID;Clone 深拷贝
     - 更新conv/schedule_state.go:加载时构建 TaskClassMeta;Diff 支持 HostEventID 嵌入关系
     - 更新conv/schedule_provider.go:新增 LoadTaskClassMetas 按需加载
     - 更新model/state_store.go:ScheduleStateProvider 接口新增 LoadTaskClassMetas
     - 更新prompt/base.go:renderStateSummary 渲染任务类约束
     - 更新prompt/plan.go:注入任务类 ID 上下文和粗排识别规则
     - 更新tools/read_tools.go:GetOverview 展示任务类约束
     - 更新model/common_state.go:CommonState 新增 TaskClassIDs/TaskClasses/NeedsRoughBuild
   4.Execute 健壮性增强(correction 重试 + 纯 ReAct 模式)
     - 更新node/execute.go:未知工具名/空文本走 correction 重试而非 fatal;maxConsecutiveCorrections 提升为包级常量;新增无 plan 纯ReAct 模式;工具结果截断;speak 排除 ask_user/confirm
     - 更新prompt/execute.go:新增 ReAct 模式 system prompt 和 contract
   5.写入持久化完善(task_item source + 嵌入水课)
     - 更新conv/schedule_persist.go:place/move/unplace 支持 task_item source,含嵌入水课和普通 task event 两条路径
     - 新建conv/schedule_preview.go:ScheduleState → 排程预览缓存,复用旧格式,前端无需改动
   6.状态持久化体系(Redis → MySQL outbox 异步)
     - 更新dao/cache.go:Redis 快照 TTL 从 24h 改为 2h,配合 MySQL outbox
     - 新建model/agent_state_snapshot_record.go:快照 MySQL 记录模型
     - 新建service/events/agent_state_persist.go:outbox 异步持久化处理器
     - 更新cmd/start.go + inits/mysql.go:注册快照事件处理器 + AutoMigrate
     - 更新service/agentsvc/agent_newagent.go:注入 RoughBuildFunc;outbox 异步写快照;排程结果写 Redis 预览缓存
   7.基础设施与稳定性
     - 更新stream/sse_adapter.go:outChan 满时静默丢弃,保证持久化不被 SSE 阻断
     - 更新service/agentsvc/agent.go:新增 readAgentExtraIntSlice;outChan 容量 8→256
     - 更新node/agent_nodes.go:Chat 注入工具 schema;Deliver 改 saveAgentState 替代 deleteAgentState
前端:无
仓库:无
This commit is contained in:
Losita
2026-04-06 23:15:54 +08:00
parent b1eb6bedf9
commit 2038185730
30 changed files with 1866 additions and 298 deletions

View File

@@ -33,6 +33,20 @@ func (n *AgentNodes) Chat(ctx context.Context, st *newagentmodel.AgentGraphState
return nil, errors.New("chat node: state is nil")
}
// 注入工具 schema 到 ConversationContext让路由决策更智能。
if st.Deps.ToolRegistry != nil {
schemas := st.Deps.ToolRegistry.Schemas()
toolSchemas := make([]newagentmodel.ToolSchemaContext, len(schemas))
for i, s := range schemas {
toolSchemas[i] = newagentmodel.ToolSchemaContext{
Name: s.Name,
Desc: s.Desc,
SchemaText: s.SchemaText,
}
}
st.EnsureConversationContext().SetToolSchemas(toolSchemas)
}
if err := RunChatNode(
ctx,
ChatNodeInput{
@@ -105,6 +119,25 @@ func (n *AgentNodes) Plan(ctx context.Context, st *newagentmodel.AgentGraphState
return st, nil
}
// RoughBuild 是粗排阶段的正式节点方法。
//
// 职责边界:
// 1. 调用注入的 RoughBuildFunc 执行粗排算法;
// 2. 把粗排结果写入 ScheduleState
// 3. 完成后保存状态,支持意外断线恢复。
func (n *AgentNodes) RoughBuild(ctx context.Context, st *newagentmodel.AgentGraphState) (*newagentmodel.AgentGraphState, error) {
if st == nil {
return nil, errors.New("rough_build node: state is nil")
}
if err := RunRoughBuildNode(ctx, st); err != nil {
return nil, err
}
saveAgentState(ctx, st)
return st, nil
}
// Interrupt 是中断阶段的正式节点方法。
//
// 职责边界:
@@ -196,7 +229,7 @@ func (n *AgentNodes) Execute(ctx context.Context, st *newagentmodel.AgentGraphSt
// 1. 这里只做 graph -> node 的参数转接;
// 2. 真正的交付逻辑仍由 RunDeliverNode 负责;
// 3. 调 LLM 生成任务总结,失败时降级到机械格式化。
// 4. 任务完成后删除 Redis 快照,清理持久化状态
// 4. 任务完成后保存最终状态到 Redis2h TTL支持断线恢复和 MySQL outbox 异步持久化。
func (n *AgentNodes) Deliver(ctx context.Context, st *newagentmodel.AgentGraphState) (*newagentmodel.AgentGraphState, error) {
if st == nil {
return nil, errors.New("deliver node: state is nil")
@@ -214,7 +247,7 @@ func (n *AgentNodes) Deliver(ctx context.Context, st *newagentmodel.AgentGraphSt
return nil, err
}
deleteAgentState(ctx, st)
saveAgentState(ctx, st)
return st, nil
}

View File

@@ -3,6 +3,7 @@ package newagentnode
import (
"context"
"fmt"
"log"
"strings"
"time"
@@ -36,89 +37,222 @@ type ChatNodeInput struct {
ChunkEmitter *newagentstream.ChunkEmitter
}
// chatIntentDecision 是意图分类的结构化输出。
type chatIntentDecision struct {
Intent string `json:"intent"`
Reply string `json:"reply,omitempty"`
Reason string `json:"reason,omitempty"`
}
// Normalize 清洗意图分类结果中的字符串字段。
func (d *chatIntentDecision) Normalize() {
if d == nil {
return
}
d.Intent = strings.TrimSpace(d.Intent)
d.Reply = strings.TrimSpace(d.Reply)
d.Reason = strings.TrimSpace(d.Reason)
}
// Validate 校验意图分类结果的最小合法性。
func (d *chatIntentDecision) Validate() error {
if d == nil {
return fmt.Errorf("chat intent decision 不能为空")
}
d.Normalize()
switch d.Intent {
case "chat", "task":
return nil
default:
return fmt.Errorf("未知 intent: %s", d.Intent)
}
}
// RunChatNode 执行一轮聊天节点逻辑。
//
// 核心职责:
// 1. 恢复判定:有 pending interaction 则处理恢复,不生成 speak
// 2. 意图分流:无 pending 时,调 LLM 分类 chat / task
// 3. 闲聊回复:纯 chat 场景直接生成回复并流式推送phase → chatting → END
// 4. 任务路由task 场景 phase → planning交给后续 Plan 节点处理。
//
// 保守原则:分类失败或意图不明时,一律走 task不丢失用户意图
// 1. 恢复判定:有 pending interaction 则处理恢复;
// 2. 路由分流:无 pending 时,调 LLM 判断复杂度并路由
// 3. direct_reply简单任务直接输出回复 → END
// 4. execute中等任务推 Execute ReAct
// 5. deep_answer复杂问答原地开 thinking 深度回答 → END
// 6. plan复杂规划推 Plan 节点
func RunChatNode(ctx context.Context, input ChatNodeInput) error {
runtimeState, conversationContext, emitter, err := prepareChatNodeInput(input)
if err != nil {
return err
}
// 1. 有 pending interaction → 纯状态传递,不生成 speak
// 1. 有 pending interaction → 纯状态传递,处理恢复
if runtimeState.HasPendingInteraction() {
return handleChatResume(input, runtimeState, conversationContext, emitter)
}
// 2. 无 pending → 调 LLM 做意图分类
messages := newagentprompt.BuildChatIntentMessages(conversationContext, input.UserInput)
decision, _, err := newagentllm.GenerateJSON[chatIntentDecision](
// 2. 无 pending → 路由决策(一次快速 LLM 调用,不开 thinking
flowState := runtimeState.EnsureCommonState()
messages := newagentprompt.BuildChatRoutingMessages(conversationContext, input.UserInput, flowState)
decision, rawResult, err := newagentllm.GenerateJSON[newagentmodel.ChatRoutingDecision](
ctx,
input.Client,
messages,
newagentllm.GenerateOptions{
Temperature: 0.1,
MaxTokens: 300,
MaxTokens: 500,
Thinking: newagentllm.ThinkingModeDisabled,
Metadata: map[string]any{
"stage": chatStageName,
"phase": "routing",
},
},
)
if err != nil || decision.Validate() != nil {
// 分类失败 → 保守:走 task。
runtimeState.EnsureCommonState().Phase = newagentmodel.PhasePlanning
rawText := ""
if rawResult != nil {
rawText = strings.TrimSpace(rawResult.Text)
}
if err != nil {
// 路由失败 → 保守:走 plan。
log.Printf("[WARN] chat routing LLM failed chat=%s raw=%s err=%v",
flowState.ConversationID, rawText, err)
flowState.Phase = newagentmodel.PhasePlanning
return nil
}
// 3. 按意图分流。
flowState := runtimeState.EnsureCommonState()
switch decision.Intent {
case "task":
if validateErr := decision.Validate(); validateErr != nil {
log.Printf("[WARN] chat routing decision invalid chat=%s raw=%s err=%v",
flowState.ConversationID, rawText, validateErr)
flowState.Phase = newagentmodel.PhasePlanning
return nil
case "chat":
return handleChatReply(ctx, decision, conversationContext, emitter, flowState)
}
log.Printf("[DEBUG] chat routing chat=%s route=%s reason=%s",
flowState.ConversationID, decision.Route, decision.Reason)
// 3. 按路由决策推进。
switch decision.Route {
case newagentmodel.ChatRouteDirectReply:
return handleDirectReply(ctx, decision, conversationContext, emitter, flowState)
case newagentmodel.ChatRouteExecute:
return handleRouteExecute(decision, emitter, flowState)
case newagentmodel.ChatRouteDeepAnswer:
return handleDeepAnswer(ctx, input, decision, conversationContext, emitter, flowState)
case newagentmodel.ChatRoutePlan:
return handleRoutePlan(decision, emitter, flowState)
default:
flowState.Phase = newagentmodel.PhasePlanning
return nil
}
}
// handleDirectReply 处理简单任务:直接输出回复。
func handleDirectReply(
ctx context.Context,
decision *newagentmodel.ChatRoutingDecision,
conversationContext *newagentmodel.ConversationContext,
emitter *newagentstream.ChunkEmitter,
flowState *newagentmodel.CommonState,
) error {
if strings.TrimSpace(decision.Speak) != "" {
if err := emitter.EmitPseudoAssistantText(
ctx, chatSpeakBlockID, chatStageName,
decision.Speak,
newagentstream.DefaultPseudoStreamOptions(),
); err != nil {
return fmt.Errorf("闲聊回复推送失败: %w", err)
}
conversationContext.AppendHistory(schema.AssistantMessage(decision.Speak, nil))
}
flowState.Phase = newagentmodel.PhaseChatting
return nil
}
// handleRouteExecute 处理中等任务:推送简短确认,设 PhaseExecuting。
//
// 不把 speak 写入 history因为真正的回复由 Execute 节点产出。
func handleRouteExecute(
decision *newagentmodel.ChatRoutingDecision,
emitter *newagentstream.ChunkEmitter,
flowState *newagentmodel.CommonState,
) error {
speak := strings.TrimSpace(decision.Speak)
if speak == "" {
speak = "好的,我来处理。"
}
// 推送轻量状态通知,让前端知道请求已接收。
_ = emitter.EmitStatus(chatStatusBlockID, chatStageName, "accepted", speak, false)
flowState.Phase = newagentmodel.PhaseExecuting
// 安全兜底:只有真正持有 task_class_ids 时才开粗排。
if decision.NeedsRoughBuild && len(flowState.TaskClassIDs) > 0 {
flowState.NeedsRoughBuild = true
}
return nil
}
// handleDeepAnswer 处理复杂问答:推送过渡语 → 原地开 thinking 再调一次 LLM → 输出深度回答。
func handleDeepAnswer(
ctx context.Context,
input ChatNodeInput,
decision *newagentmodel.ChatRoutingDecision,
conversationContext *newagentmodel.ConversationContext,
emitter *newagentstream.ChunkEmitter,
flowState *newagentmodel.CommonState,
) error {
// 1. 推送过渡语。
briefSpeak := strings.TrimSpace(decision.Speak)
if briefSpeak == "" {
briefSpeak = "让我想想。"
}
if err := emitter.EmitPseudoAssistantText(
ctx, chatSpeakBlockID, chatStageName,
briefSpeak,
newagentstream.DefaultPseudoStreamOptions(),
); err != nil {
return fmt.Errorf("过渡文案推送失败: %w", err)
}
// 2. 第二次 LLM 调用:开 thinking深度回答。
deepMessages := newagentprompt.BuildDeepAnswerMessages(conversationContext, input.UserInput)
deepResult, err := input.Client.GenerateText(ctx, deepMessages, newagentllm.GenerateOptions{
Temperature: 0.5,
MaxTokens: 2000,
Thinking: newagentllm.ThinkingModeEnabled,
Metadata: map[string]any{
"stage": chatStageName,
"phase": "deep_answer",
},
})
if err != nil || deepResult == nil {
// 深度回答失败 → 降级,只保留过渡语。
log.Printf("[WARN] deep answer LLM failed chat=%s err=%v", flowState.ConversationID, err)
conversationContext.AppendHistory(schema.AssistantMessage(briefSpeak, nil))
flowState.Phase = newagentmodel.PhaseChatting
return nil
}
// 3. 输出深度回答。
deepText := strings.TrimSpace(deepResult.Text)
if deepText == "" {
conversationContext.AppendHistory(schema.AssistantMessage(briefSpeak, nil))
flowState.Phase = newagentmodel.PhaseChatting
return nil
}
if err := emitter.EmitPseudoAssistantText(
ctx, chatSpeakBlockID, chatStageName,
deepText,
newagentstream.DefaultPseudoStreamOptions(),
); err != nil {
return fmt.Errorf("深度回答推送失败: %w", err)
}
// 将完整回复(过渡语 + 深度回答)写入 history。
fullReply := briefSpeak + "\n\n" + deepText
conversationContext.AppendHistory(schema.AssistantMessage(fullReply, nil))
flowState.Phase = newagentmodel.PhaseChatting
return nil
}
// handleRoutePlan 处理复杂规划:推送确认语,设 PhasePlanning。
func handleRoutePlan(
decision *newagentmodel.ChatRoutingDecision,
emitter *newagentstream.ChunkEmitter,
flowState *newagentmodel.CommonState,
) error {
speak := strings.TrimSpace(decision.Speak)
if speak == "" {
speak = "好的,让我来规划一下。"
}
_ = emitter.EmitStatus(chatStatusBlockID, chatStageName, "planning", speak, false)
flowState.Phase = newagentmodel.PhasePlanning
return nil
}
// ─── 恢复处理(保持原有逻辑不变)───
// handleChatResume 处理 pending interaction 恢复。
//
// 职责边界:
@@ -216,31 +350,6 @@ func handleConfirmResume(
return nil
}
// handleChatReply 处理纯闲聊意图 — 把分类时产出的 reply 流式推给前端。
func handleChatReply(
ctx context.Context,
decision *chatIntentDecision,
conversationContext *newagentmodel.ConversationContext,
emitter *newagentstream.ChunkEmitter,
flowState *newagentmodel.CommonState,
) error {
reply := strings.TrimSpace(decision.Reply)
if reply != "" {
if err := emitter.EmitPseudoAssistantText(
ctx, chatSpeakBlockID, chatStageName,
reply,
newagentstream.DefaultPseudoStreamOptions(),
); err != nil {
return fmt.Errorf("闲聊回复推送失败: %w", err)
}
conversationContext.AppendHistory(schema.AssistantMessage(reply, nil))
}
flowState.Phase = newagentmodel.PhaseChatting
return nil
}
// prepareChatNodeInput 校验并准备聊天节点的运行态依赖。
func prepareChatNodeInput(input ChatNodeInput) (
*newagentmodel.AgentRuntimeState,

View File

@@ -22,6 +22,11 @@ const (
executeStatusBlockID = "execute.status"
executeSpeakBlockID = "execute.speak"
executePinnedKey = "execution_context"
// maxConsecutiveCorrections 是 Execute 节点连续修正次数上限。
// 超过此阈值后终止执行,防止 LLM 陷入无限修正循环。
// 适用场景JSON 解析失败、决策不合法、goal_check 为空、工具名不存在。
maxConsecutiveCorrections = 3
)
// ExecuteNodeInput 描述执行节点单轮运行所需的最小依赖。
@@ -95,22 +100,31 @@ func RunExecuteNode(ctx context.Context, input ExecuteNodeInput) error {
return executePendingTool(ctx, runtimeState, conversationContext, input.ToolRegistry, input.ScheduleState, input.SchedulePersistor, input.OriginalScheduleState, emitter)
}
// 2. 检查是否有可执行的 plan 步骤
if !flowState.HasCurrentPlanStep() {
return fmt.Errorf("execute node: 当前无有效 plan 步骤,无法执行")
}
// 3. 推送执行阶段状态,让前端知道当前进度。
current, total := flowState.PlanProgress()
currentStep, _ := flowState.CurrentPlanStep()
if err := emitter.EmitStatus(
executeStatusBlockID,
executeStageName,
"executing",
fmt.Sprintf("正在执行第 %d/%d 步:%s", current, total, truncateText(currentStep.Content, 60)),
false,
); err != nil {
return fmt.Errorf("执行阶段状态推送失败: %w", err)
// 2. 推送执行阶段状态,让前端知道当前进度
if flowState.HasCurrentPlanStep() {
// 有 plan显示步骤进度。
current, total := flowState.PlanProgress()
currentStep, _ := flowState.CurrentPlanStep()
if err := emitter.EmitStatus(
executeStatusBlockID,
executeStageName,
"executing",
fmt.Sprintf("正在执行第 %d/%d 步:%s", current, total, truncateText(currentStep.Content, 60)),
false,
); err != nil {
return fmt.Errorf("执行阶段状态推送失败: %w", err)
}
} else {
// 无 plan纯 ReAct 模式。
if err := emitter.EmitStatus(
executeStatusBlockID,
executeStageName,
"executing",
"正在处理你的请求...",
false,
); err != nil {
return fmt.Errorf("执行阶段状态推送失败: %w", err)
}
}
// 4. 消耗一轮预算,并检查是否耗尽。
@@ -129,7 +143,7 @@ func RunExecuteNode(ctx context.Context, input ExecuteNodeInput) error {
newagentllm.GenerateOptions{
Temperature: 0.3,
MaxTokens: 1200,
Thinking: newagentllm.ThinkingModeEnabled,
Thinking: newagentllm.ThinkingModeDisabled,
Metadata: map[string]any{
"stage": executeStageName,
"step_index": flowState.CurrentStep,
@@ -137,8 +151,6 @@ func RunExecuteNode(ctx context.Context, input ExecuteNodeInput) error {
},
},
)
const maxConsecutiveCorrections = 3
// 提前捕获原始文本,用于日志和 correction。
rawText := ""
if rawResult != nil {
@@ -162,6 +174,25 @@ func RunExecuteNode(ctx context.Context, input ExecuteNodeInput) error {
)
return nil
}
// 模型返回空文本(常见原因:上下文过长、模型异常),走 correction 重试而非直接 fatal。
if strings.Contains(err.Error(), "empty text") {
log.Printf("[WARN] execute LLM 返回空文本 chat=%s round=%d consecutive=%d/%d",
flowState.ConversationID, flowState.RoundUsed,
flowState.ConsecutiveCorrections+1, maxConsecutiveCorrections)
flowState.ConsecutiveCorrections++
if flowState.ConsecutiveCorrections >= maxConsecutiveCorrections {
return fmt.Errorf("连续 %d 次模型返回空文本,终止执行", flowState.ConsecutiveCorrections)
}
AppendLLMCorrectionWithHint(
conversationContext,
"",
"模型没有返回任何内容。",
"请重新输出合法 JSON 格式的执行决策。",
)
return nil
}
return fmt.Errorf("执行阶段模型调用失败: %w", err)
}
@@ -210,8 +241,10 @@ func RunExecuteNode(ctx context.Context, input ExecuteNodeInput) error {
}
}
// 6. 若 LLM 先对用户说话,则伪流式推送并写回历史
if strings.TrimSpace(decision.Speak) != "" {
// 6. 若 LLM 先对用户说话,且不是 ask_user / confirm二者交给下游节点收口则伪流式推送。
if strings.TrimSpace(decision.Speak) != "" &&
decision.Action != newagentmodel.ExecuteActionAskUser &&
decision.Action != newagentmodel.ExecuteActionConfirm {
if err := emitter.EmitPseudoAssistantText(
ctx,
executeSpeakBlockID,
@@ -399,12 +432,34 @@ func executeToolCall(
return fmt.Errorf("日程状态未加载,无法执行工具")
}
if !registry.HasTool(toolName) {
return fmt.Errorf("未知工具: %s", toolName)
// LLM 拼错或编造了工具名,走 correction 机制给重试机会,而非直接 fatal。
// 与 action 不合法、决策校验失败等路径一致:追加错误反馈 → Graph 循环 → LLM 修正。
flowState.ConsecutiveCorrections++
if flowState.ConsecutiveCorrections >= maxConsecutiveCorrections {
return fmt.Errorf("连续 %d 次调用未知工具,终止执行: %s可用工具%s",
flowState.ConsecutiveCorrections, toolName, strings.Join(registry.ToolNames(), "、"))
}
log.Printf("[WARN] execute 工具名不合法 chat=%s round=%d tool=%s consecutive=%d/%d available=%v",
flowState.ConversationID, flowState.RoundUsed, toolName,
flowState.ConsecutiveCorrections, maxConsecutiveCorrections, registry.ToolNames())
AppendLLMCorrectionWithHint(
conversationContext,
"",
fmt.Sprintf("你调用的工具 \"%s\" 不存在。", toolName),
fmt.Sprintf("可用工具:%s。请检查拼写后重新输出。", strings.Join(registry.ToolNames(), "、")),
)
return nil
}
// 2. 执行工具。
result := registry.Execute(scheduleState, toolName, toolCall.Arguments)
// 2.5 截断过大的工具结果,防止上下文膨胀导致后续 LLM 调用返回空或超限。
const maxToolResultLen = 3000
if len(result) > maxToolResultLen {
result = result[:maxToolResultLen] + fmt.Sprintf("\n...(结果已截断,原始长度 %d 字符)", len(result))
}
// 3. 将工具调用和结果以合法的 assistant+tool 消息对追加到对话历史。
//
// 修复说明:

View File

@@ -67,7 +67,7 @@ func RunPlanNode(ctx context.Context, input PlanNodeInput) error {
// 2. 构造本轮规划输入。
messages := newagentprompt.BuildPlanMessages(flowState, conversationContext, input.UserInput)
// 3. Phase 1快速评估开 thinking让 LLM 同时产出复杂度评估和规划结果。
// 3. Phase 1快速评估开 thinking让 LLM 同时产出复杂度评估和规划结果。
decision, rawResult, err := newagentllm.GenerateJSON[newagentmodel.PlanDecision](
ctx,
input.Client,
@@ -75,7 +75,7 @@ func RunPlanNode(ctx context.Context, input PlanNodeInput) error {
newagentllm.GenerateOptions{
Temperature: 0.2,
MaxTokens: 1600,
Thinking: newagentllm.ThinkingModeDisabled,
Thinking: newagentllm.ThinkingModeEnabled,
Metadata: map[string]any{
"stage": planStageName,
"phase": "assessment",
@@ -128,8 +128,8 @@ func RunPlanNode(ctx context.Context, input PlanNodeInput) error {
// 深度规划失败时静默降级到 Phase 1 结果,不中断流程。
}
// 5. 若模型先对用户说了话,则先以伪流式推送,再写回 history,保证上下文连续
if strings.TrimSpace(decision.Speak) != "" {
// 5. 若模型先对用户说了话,且不是 ask_userask_user 交给 interrupt 收口),则先以伪流式推送,再写回 history。
if strings.TrimSpace(decision.Speak) != "" && decision.Action != newagentmodel.PlanActionAskUser {
if err := emitter.EmitPseudoAssistantText(
ctx,
planSpeakBlockID,
@@ -154,9 +154,18 @@ func RunPlanNode(ctx context.Context, input PlanNodeInput) error {
case newagentmodel.PlanActionDone:
// 4.1 直接把结构化 PlanStep 固化到 CommonState避免 state 层丢失 done_when。
// 4.2 再把完整自然语言计划写入 pinned context保证后续 execute 优先看到。
// 4.3 最后进入 waiting_confirm等待用户确认整体计划。
// 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
}
}
return nil
default:
// 1. LLM 输出了不支持的 action不应直接报错终止而应给它修正机会。

View File

@@ -0,0 +1,130 @@
package newagentnode
import (
"context"
"fmt"
newagentmodel "github.com/LoveLosita/smartflow/backend/newAgent/model"
newagenttools "github.com/LoveLosita/smartflow/backend/newAgent/tools"
)
const (
roughBuildStageName = "rough_build"
roughBuildStatusBlock = "rough_build.status"
)
// RunRoughBuildNode 执行粗排节点逻辑。
//
// 步骤说明:
// 1. 推送"正在粗排"状态给前端;
// 2. 从 CommonState 读取 TaskClassIDs确认有需要排课的任务类
// 3. 加载 ScheduleState含 DayMapping
// 4. 调用 RoughBuildFunc 拿到粗排结果([]RoughBuildPlacement
// 5. 把粗排结果写入 ScheduleState 的对应 task.Slotspending 任务预填位置);
// 6. 推送"粗排完成"状态,清除 NeedsRoughBuild 标记,进入执行阶段。
func RunRoughBuildNode(ctx context.Context, st *newagentmodel.AgentGraphState) error {
if st == nil {
return fmt.Errorf("rough build node: state is nil")
}
flowState := st.EnsureFlowState()
emitter := st.EnsureChunkEmitter()
// 1. 推送状态:告知前端进入粗排环节。
_ = emitter.EmitStatus(
roughBuildStatusBlock,
roughBuildStageName,
"rough_building",
"正在为你生成初始排课方案,请稍候。",
true,
)
// 2. 校验依赖。
if st.Deps.RoughBuildFunc == nil {
return fmt.Errorf("rough build node: RoughBuildFunc 未注入")
}
// 3. 读取任务类 IDs。
taskClassIDs := flowState.TaskClassIDs
if len(taskClassIDs) == 0 {
// 没有任务类 ID 时静默跳过粗排,直接进入执行阶段。
flowState.Phase = newagentmodel.PhaseExecuting
flowState.NeedsRoughBuild = false
return nil
}
// 4. 加载 ScheduleState含 DayMapping用于坐标转换
scheduleState, err := st.EnsureScheduleState(ctx)
if err != nil {
return fmt.Errorf("rough build node: 加载日程状态失败: %w", err)
}
if scheduleState == nil {
return fmt.Errorf("rough build node: ScheduleState 为空,无法执行粗排")
}
// 5. 调用粗排算法。
placements, err := st.Deps.RoughBuildFunc(ctx, flowState.UserID, taskClassIDs)
if err != nil {
return fmt.Errorf("rough build node: 粗排算法失败: %w", err)
}
// 6. 把粗排结果写入 ScheduleState。
applyRoughBuildPlacements(scheduleState, placements)
// 7. 推送完成状态。
_ = emitter.EmitStatus(
roughBuildStatusBlock,
roughBuildStageName,
"rough_build_done",
fmt.Sprintf("初始排课方案已生成,共 %d 个任务已预排,进入微调阶段。", len(placements)),
false,
)
// 8. 把粗排完成信息写入 pinned context让 Execute 阶段的 LLM 直接跳过"触发粗排"
// 进入验证和微调,避免 LLM 误以为需要自己运行算法而浪费一轮工具调用。
st.EnsureConversationContext().UpsertPinnedBlock(newagentmodel.ContextBlock{
Key: "rough_build_done",
Title: "粗排已完成",
Content: fmt.Sprintf(
"后端已自动运行粗排算法,初始排课方案已写入日程状态(共 %d 个任务已预排)。\n"+
"请直接调用 get_overview 查看预排结果,然后用 move/swap 微调不合理的位置。\n"+
"无需再次触发粗排,也不要在 plan_steps 里描述触发粗排相关的操作。",
len(placements),
),
})
// 9. 清除标记,进入执行阶段。
flowState.NeedsRoughBuild = false
flowState.Phase = newagentmodel.PhaseExecuting
return nil
}
// applyRoughBuildPlacements 把粗排结果写入 ScheduleState 对应任务的 Slots。
//
// 设计说明:
// 1. 通过 task_item_idSourceID定位任务
// 2. 用 DayMapping 把 (week, dayOfWeek) 转为 day_index
// 3. task.Status 保持 "pending",让 LLM 在 Execute 阶段看到"有建议位置的待安排任务"
// 可用 move/swap 微调,也可用 unplace 推翻粗排结果;
// 4. 转换失败的条目静默跳过,不中断整体流程。
func applyRoughBuildPlacements(state *newagenttools.ScheduleState, placements []newagentmodel.RoughBuildPlacement) {
if state == nil {
return
}
for _, p := range placements {
day, ok := state.WeekDayToDay(p.Week, p.DayOfWeek)
if !ok {
continue // DayMapping 里没有对应 day跳过
}
for i := range state.Tasks {
t := &state.Tasks[i]
if t.Source != "task_item" || t.SourceID != p.TaskItemID {
continue
}
t.Slots = []newagenttools.TaskSlot{
{Day: day, SlotStart: p.SectionFrom, SlotEnd: p.SectionTo},
}
break
}
}
}