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:
@@ -12,12 +12,13 @@ import (
|
||||
const (
|
||||
GraphName = "agent_loop"
|
||||
|
||||
NodeChat = "chat"
|
||||
NodePlan = "plan"
|
||||
NodeConfirm = "confirm"
|
||||
NodeExecute = "execute"
|
||||
NodeInterrupt = "interrupt"
|
||||
NodeDeliver = "deliver"
|
||||
NodeChat = "chat"
|
||||
NodePlan = "plan"
|
||||
NodeConfirm = "confirm"
|
||||
NodeRoughBuild = "rough_build"
|
||||
NodeExecute = "execute"
|
||||
NodeInterrupt = "interrupt"
|
||||
NodeDeliver = "deliver"
|
||||
)
|
||||
|
||||
func RunAgentGraph(ctx context.Context, input newagentmodel.AgentGraphRunInput) (*newagentmodel.AgentGraphState, error) {
|
||||
@@ -44,6 +45,9 @@ func RunAgentGraph(ctx context.Context, input newagentmodel.AgentGraphRunInput)
|
||||
if err := g.AddLambdaNode(NodeConfirm, compose.InvokableLambda(nodes.Confirm)); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := g.AddLambdaNode(NodeRoughBuild, compose.InvokableLambda(nodes.RoughBuild)); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := g.AddLambdaNode(NodeExecute, compose.InvokableLambda(nodes.Execute)); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -60,16 +64,17 @@ func RunAgentGraph(ctx context.Context, input newagentmodel.AgentGraphRunInput)
|
||||
if err := g.AddEdge(compose.START, NodeChat); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// Chat -> END(普通聊天) / Plan / Confirm / Execute / Deliver / Interrupt
|
||||
// Chat -> END / Plan / Confirm / RoughBuild / Execute / Deliver / Interrupt
|
||||
if err := g.AddBranch(NodeChat, compose.NewGraphBranch(
|
||||
branchAfterChat,
|
||||
map[string]bool{
|
||||
NodePlan: true,
|
||||
NodeConfirm: true,
|
||||
NodeExecute: true,
|
||||
NodeDeliver: true,
|
||||
NodeInterrupt: true,
|
||||
compose.END: true,
|
||||
NodePlan: true,
|
||||
NodeConfirm: true,
|
||||
NodeRoughBuild: true,
|
||||
NodeExecute: true,
|
||||
NodeDeliver: true,
|
||||
NodeInterrupt: true,
|
||||
compose.END: true,
|
||||
},
|
||||
)); err != nil {
|
||||
return nil, err
|
||||
@@ -85,17 +90,22 @@ func RunAgentGraph(ctx context.Context, input newagentmodel.AgentGraphRunInput)
|
||||
)); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// Confirm -> Plan(用户拒绝或重规划) / Execute(确认后继续执行) / Interrupt(产出确认中断并等待外部回调)
|
||||
// Confirm -> Plan(用户拒绝或重规划) / RoughBuild(需粗排) / Execute(直接执行) / Interrupt(等待用户确认)
|
||||
if err := g.AddBranch(NodeConfirm, compose.NewGraphBranch(
|
||||
branchAfterConfirm,
|
||||
map[string]bool{
|
||||
NodePlan: true,
|
||||
NodeExecute: true,
|
||||
NodeInterrupt: true,
|
||||
NodePlan: true,
|
||||
NodeRoughBuild: true,
|
||||
NodeExecute: true,
|
||||
NodeInterrupt: true,
|
||||
},
|
||||
)); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// RoughBuild -> Execute:粗排完成后直接进入执行阶段微调。
|
||||
if err := g.AddEdge(NodeRoughBuild, NodeExecute); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// Execute -> Execute(继续 ReAct) / Confirm(写操作待确认) / Deliver(完成) / Interrupt(需要追问用户)
|
||||
if err := g.AddBranch(NodeExecute, compose.NewGraphBranch(
|
||||
branchAfterExecute,
|
||||
@@ -145,16 +155,21 @@ func branchAfterChat(_ context.Context, st *newagentmodel.AgentGraphState) (stri
|
||||
return compose.END, nil
|
||||
}
|
||||
switch flowState.Phase {
|
||||
case newagentmodel.PhaseChatting:
|
||||
// 简单任务直接回复 / 深度回答完成,回复已在 Chat 节点生成。
|
||||
return compose.END, nil
|
||||
case newagentmodel.PhasePlanning:
|
||||
return NodePlan, nil
|
||||
case newagentmodel.PhaseWaitingConfirm:
|
||||
return NodeConfirm, nil
|
||||
case newagentmodel.PhaseExecuting:
|
||||
if flowState.NeedsRoughBuild && st.Deps.RoughBuildFunc != nil {
|
||||
return NodeRoughBuild, nil
|
||||
}
|
||||
return NodeExecute, nil
|
||||
case newagentmodel.PhaseDone:
|
||||
return NodeDeliver, nil
|
||||
default:
|
||||
// 普通聊天场景,回复已在 chatNode 生成,当前请求可直接结束。
|
||||
return compose.END, nil
|
||||
}
|
||||
}
|
||||
@@ -191,10 +206,14 @@ func branchAfterConfirm(_ context.Context, st *newagentmodel.AgentGraphState) (s
|
||||
}
|
||||
switch flowState.Phase {
|
||||
case newagentmodel.PhaseExecuting:
|
||||
// 若 Plan 节点标记了需要粗排且 RoughBuildFunc 已注入,走粗排节点。
|
||||
if flowState.NeedsRoughBuild && st.Deps.RoughBuildFunc != nil {
|
||||
return NodeRoughBuild, nil
|
||||
}
|
||||
return NodeExecute, nil
|
||||
case newagentmodel.PhaseWaitingConfirm:
|
||||
// 1. confirm 节点产出确认请求后,当前连接必须进入 interrupt 收口。
|
||||
// 2. 真正的用户确认结果应由外部回调写回状态,再重新进入 graph。
|
||||
// confirm 节点产出确认请求后,当前连接必须进入 interrupt 收口。
|
||||
// 真正的用户确认结果应由外部回调写回状态,再重新进入 graph。
|
||||
return NodeInterrupt, nil
|
||||
default:
|
||||
return NodePlan, nil
|
||||
|
||||
72
backend/newAgent/model/chat_contract.go
Normal file
72
backend/newAgent/model/chat_contract.go
Normal file
@@ -0,0 +1,72 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// ChatRoute 表示 Chat 节点路由决策的目标路径。
|
||||
type ChatRoute string
|
||||
|
||||
const (
|
||||
// ChatRouteDirectReply 简单任务:Chat 节点直接输出回复,不再调用下游节点。
|
||||
ChatRouteDirectReply ChatRoute = "direct_reply"
|
||||
|
||||
// ChatRouteExecute 中等任务:需要用工具处理,直接进 Execute ReAct 循环。
|
||||
ChatRouteExecute ChatRoute = "execute"
|
||||
|
||||
// ChatRouteDeepAnswer 复杂问答:需要深度思考但不需工具,Chat 节点原地开 thinking 回答。
|
||||
ChatRouteDeepAnswer ChatRoute = "deep_answer"
|
||||
|
||||
// ChatRoutePlan 复杂规划:需要先制定计划,进 Plan 节点。
|
||||
ChatRoutePlan ChatRoute = "plan"
|
||||
)
|
||||
|
||||
// ChatRoutingDecision 是 Chat 节点单次路由决策的结构化输出。
|
||||
//
|
||||
// 职责边界:
|
||||
// 1. Route 决定后续处理路径;
|
||||
// 2. Speak 始终填写:给用户看的话;
|
||||
// 3. NeedsRoughBuild 仅在 route=execute 且满足粗排条件时为 true;
|
||||
// 4. Reason 给后端和日志看。
|
||||
type ChatRoutingDecision struct {
|
||||
Route ChatRoute `json:"route"`
|
||||
Speak string `json:"speak,omitempty"`
|
||||
NeedsRoughBuild bool `json:"needs_rough_build,omitempty"`
|
||||
Reason string `json:"reason,omitempty"`
|
||||
}
|
||||
|
||||
// Normalize 统一清洗路由决策中的字符串字段。
|
||||
func (d *ChatRoutingDecision) Normalize() {
|
||||
if d == nil {
|
||||
return
|
||||
}
|
||||
d.Route = ChatRoute(strings.TrimSpace(string(d.Route)))
|
||||
d.Speak = strings.TrimSpace(d.Speak)
|
||||
d.Reason = strings.TrimSpace(d.Reason)
|
||||
}
|
||||
|
||||
// Validate 校验路由决策的最小合法性。
|
||||
func (d *ChatRoutingDecision) Validate() error {
|
||||
if d == nil {
|
||||
return fmt.Errorf("chat routing decision 不能为空")
|
||||
}
|
||||
|
||||
d.Normalize()
|
||||
|
||||
switch d.Route {
|
||||
case ChatRouteDirectReply, ChatRouteExecute, ChatRouteDeepAnswer, ChatRoutePlan:
|
||||
// ok
|
||||
case "":
|
||||
return fmt.Errorf("chat routing decision.route 不能为空")
|
||||
default:
|
||||
return fmt.Errorf("未知 route: %s", d.Route)
|
||||
}
|
||||
|
||||
// direct_reply 必须有 speak。
|
||||
if d.Route == ChatRouteDirectReply && d.Speak == "" {
|
||||
return fmt.Errorf("direct_reply 必须携带 speak")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -1,5 +1,9 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
newagenttools "github.com/LoveLosita/smartflow/backend/newAgent/tools"
|
||||
)
|
||||
|
||||
// Phase 表示 agent 主循环当前所处的大阶段。
|
||||
type Phase string
|
||||
|
||||
@@ -39,6 +43,17 @@ type CommonState struct {
|
||||
|
||||
// 连续修正计数:LLM 连续输出不合法决策的次数,超过阈值后强制终止避免死循环。
|
||||
ConsecutiveCorrections int `json:"consecutive_corrections"`
|
||||
|
||||
// TaskClassIDs 本次排课请求涉及的任务类 ID 列表,由前端 extra.task_class_ids 传入。
|
||||
// Plan 节点据此判断是否需要粗排;跨轮次持久化,不会因会话恢复而丢失。
|
||||
TaskClassIDs []int `json:"task_class_ids,omitempty"`
|
||||
// TaskClasses 本次排课涉及的任务类约束元数据(含日期、策略、时段预算等),
|
||||
// 在 Service 层从 DB 加载并注入,供 Plan prompt 直接消费,避免 LLM 因信息不足而追问用户。
|
||||
TaskClasses []newagenttools.TaskClassMeta `json:"task_classes,omitempty"`
|
||||
|
||||
// NeedsRoughBuild 由 Plan 节点在 plan_done 时写入,标记 Confirm 后是否需要走粗排节点。
|
||||
// 粗排节点执行完毕后会将此字段重置为 false。
|
||||
NeedsRoughBuild bool `json:"needs_rough_build,omitempty"`
|
||||
}
|
||||
|
||||
func NewCommonState(traceID string, userID int, conversationID string) *CommonState {
|
||||
|
||||
@@ -29,6 +29,20 @@ func (r *AgentGraphRequest) Normalize() {
|
||||
r.ConfirmAction = strings.TrimSpace(r.ConfirmAction)
|
||||
}
|
||||
|
||||
// RoughBuildPlacement 是粗排算法返回的单条放置结果。
|
||||
// 字段使用 DB 坐标系(week/dayOfWeek/section),由 RoughBuild 节点转换为 ScheduleState 的 day_index。
|
||||
type RoughBuildPlacement struct {
|
||||
TaskItemID int
|
||||
Week int
|
||||
DayOfWeek int
|
||||
SectionFrom int
|
||||
SectionTo int
|
||||
}
|
||||
|
||||
// RoughBuildFunc 是粗排算法的依赖注入签名。
|
||||
// 由 service 层封装 HybridScheduleWithPlanMulti 后注入,newAgent 层不直接依赖外层 model。
|
||||
type RoughBuildFunc func(ctx context.Context, userID int, taskClassIDs []int) ([]RoughBuildPlacement, error)
|
||||
|
||||
// AgentGraphDeps 描述 graph/node 层运行时真正依赖的可插拔能力。
|
||||
//
|
||||
// 设计目的:
|
||||
@@ -45,6 +59,7 @@ type AgentGraphDeps struct {
|
||||
ToolRegistry *newagenttools.ToolRegistry
|
||||
ScheduleProvider ScheduleStateProvider // 按 DAO 注入,Execute 节点按需加载 ScheduleState
|
||||
SchedulePersistor SchedulePersistor // 按 DAO 注入,用于写工具执行后持久化变更
|
||||
RoughBuildFunc RoughBuildFunc // 按 Service 注入,粗排算法入口
|
||||
}
|
||||
|
||||
// EnsureChunkEmitter 保证 graph 运行时始终有一个可用的 chunk 发射器。
|
||||
|
||||
@@ -44,14 +44,18 @@ const (
|
||||
// 1. Speak 是本轮先对用户说的话;若 action=ask_user,通常这里会承载要追问的问题;
|
||||
// 2. Action 是规划阶段的下一步动作类型;
|
||||
// 3. Reason 是给后端和日志看的简短解释;
|
||||
// 4. PlanSteps 只在 plan_done 时要求返回,表示本轮最终确认下来的完整自然语言计划。
|
||||
// 4. PlanSteps 只在 plan_done 时要求返回,表示本轮最终确认下来的完整自然语言计划;
|
||||
// 5. NeedsRoughBuild 为 true 时,Confirm 后自动触发粗排节点,不需要 LLM 在 plan_steps 里手动描述放置步骤;
|
||||
// 6. TaskClassIDs 是本次粗排涉及的任务类 ID 列表,与 CommonState.TaskClassIDs 保持一致。
|
||||
type PlanDecision struct {
|
||||
Speak string `json:"speak,omitempty"`
|
||||
Action PlanAction `json:"action"`
|
||||
Reason string `json:"reason,omitempty"`
|
||||
Complexity PlanComplexity `json:"complexity"`
|
||||
NeedThinking bool `json:"need_thinking"`
|
||||
PlanSteps []PlanStep `json:"plan_steps,omitempty"`
|
||||
Speak string `json:"speak,omitempty"`
|
||||
Action PlanAction `json:"action"`
|
||||
Reason string `json:"reason,omitempty"`
|
||||
Complexity PlanComplexity `json:"complexity"`
|
||||
NeedThinking bool `json:"need_thinking"`
|
||||
PlanSteps []PlanStep `json:"plan_steps,omitempty"`
|
||||
NeedsRoughBuild bool `json:"needs_rough_build,omitempty"`
|
||||
TaskClassIDs []int `json:"task_class_ids,omitempty"`
|
||||
}
|
||||
|
||||
// Normalize 统一清洗规划决策中的字符串字段。
|
||||
|
||||
@@ -57,6 +57,8 @@ type AgentStateStore interface {
|
||||
// 使用接口而非具体 DAO 类型,避免 model → dao 的循环依赖。
|
||||
type ScheduleStateProvider interface {
|
||||
LoadScheduleState(ctx context.Context, userID int) (*newagenttools.ScheduleState, error)
|
||||
// LoadTaskClassMetas 只加载指定任务类的约束元数据,供 Plan 节点提前消费。
|
||||
LoadTaskClassMetas(ctx context.Context, userID int, taskClassIDs []int) ([]newagenttools.TaskClassMeta, error)
|
||||
}
|
||||
|
||||
// SchedulePersistor 定义持久化 ScheduleState 变更的接口。
|
||||
|
||||
@@ -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. 任务完成后保存最终状态到 Redis(2h 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
|
||||
}
|
||||
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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 消息对追加到对话历史。
|
||||
//
|
||||
// 修复说明:
|
||||
|
||||
@@ -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_user(ask_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,不应直接报错终止,而应给它修正机会。
|
||||
|
||||
130
backend/newAgent/node/rough_build.go
Normal file
130
backend/newAgent/node/rough_build.go
Normal 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.Slots(pending 任务预填位置);
|
||||
// 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_id(SourceID)定位任务;
|
||||
// 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
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -70,29 +70,46 @@ func renderStateSummary(state *newagentmodel.CommonState) string {
|
||||
|
||||
if !state.HasPlan() {
|
||||
sb.WriteString("当前完整 plan:暂无。\n")
|
||||
return sb.String()
|
||||
}
|
||||
|
||||
sb.WriteString("当前完整 plan:\n")
|
||||
for i, step := range state.PlanSteps {
|
||||
sb.WriteString(fmt.Sprintf("%d. %s\n", i+1, strings.TrimSpace(step.Content)))
|
||||
if strings.TrimSpace(step.DoneWhen) != "" {
|
||||
sb.WriteString(fmt.Sprintf(" 完成判定:%s\n", strings.TrimSpace(step.DoneWhen)))
|
||||
}
|
||||
}
|
||||
|
||||
if step, ok := state.CurrentPlanStep(); ok {
|
||||
sb.WriteString(fmt.Sprintf("当前步骤进度:%d/%d\n", current, total))
|
||||
sb.WriteString("当前步骤内容:\n")
|
||||
sb.WriteString(strings.TrimSpace(step.Content))
|
||||
sb.WriteString("\n")
|
||||
if strings.TrimSpace(step.DoneWhen) != "" {
|
||||
sb.WriteString("当前步骤完成判定:\n")
|
||||
sb.WriteString(strings.TrimSpace(step.DoneWhen))
|
||||
sb.WriteString("\n")
|
||||
}
|
||||
} else {
|
||||
sb.WriteString("当前步骤进度:暂时无有效当前步骤。\n")
|
||||
sb.WriteString("当前完整 plan:\n")
|
||||
for i, step := range state.PlanSteps {
|
||||
sb.WriteString(fmt.Sprintf("%d. %s\n", i+1, strings.TrimSpace(step.Content)))
|
||||
if strings.TrimSpace(step.DoneWhen) != "" {
|
||||
sb.WriteString(fmt.Sprintf(" 完成判定:%s\n", strings.TrimSpace(step.DoneWhen)))
|
||||
}
|
||||
}
|
||||
|
||||
if step, ok := state.CurrentPlanStep(); ok {
|
||||
sb.WriteString(fmt.Sprintf("当前步骤进度:%d/%d\n", current, total))
|
||||
sb.WriteString("当前步骤内容:\n")
|
||||
sb.WriteString(strings.TrimSpace(step.Content))
|
||||
sb.WriteString("\n")
|
||||
if strings.TrimSpace(step.DoneWhen) != "" {
|
||||
sb.WriteString("当前步骤完成判定:\n")
|
||||
sb.WriteString(strings.TrimSpace(step.DoneWhen))
|
||||
sb.WriteString("\n")
|
||||
}
|
||||
} else {
|
||||
sb.WriteString("当前步骤进度:暂时无有效当前步骤。\n")
|
||||
}
|
||||
}
|
||||
|
||||
// 渲染任务类约束元数据(如有),帮助 LLM 了解排程范围和策略,避免追问已有信息。
|
||||
if len(state.TaskClasses) > 0 {
|
||||
sb.WriteString("\n本次排课涉及的任务类约束:\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)
|
||||
}
|
||||
if tc.AllowFillerCourse {
|
||||
line += ",允许嵌入水课"
|
||||
}
|
||||
if len(tc.ExcludedSlots) > 0 {
|
||||
line += fmt.Sprintf(",排除时段=%v", tc.ExcludedSlots)
|
||||
}
|
||||
sb.WriteString(line + "\n")
|
||||
}
|
||||
}
|
||||
|
||||
return sb.String()
|
||||
|
||||
@@ -1,63 +1,122 @@
|
||||
package newagentprompt
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
newagentmodel "github.com/LoveLosita/smartflow/backend/newAgent/model"
|
||||
"github.com/cloudwego/eino/schema"
|
||||
)
|
||||
|
||||
const chatIntentSystemPrompt = `
|
||||
你是 SmartFlow 的意图分类器。
|
||||
你的唯一任务是判断用户本轮输入是"纯闲聊"还是"包含任务意图"。
|
||||
const chatRoutingSystemPrompt = `
|
||||
你是 SmartFlow 的智能路由器。你的职责是判断用户意图的复杂度,并决定后续处理路径。
|
||||
|
||||
判断规则:
|
||||
1. chat:打招呼、感谢、简单问答、情感表达、闲聊,不涉及任何具体任务或操作请求。
|
||||
2. task:包含任何需要规划/执行/操作的意图,包括但不限于查询信息、创建内容、修改数据、安排日程、继续已有任务等。
|
||||
你会看到:
|
||||
- 历史对话
|
||||
- 用户本轮输入
|
||||
- 当前可用工具摘要(如有)
|
||||
- 本次排课涉及的任务类约束(如有)
|
||||
|
||||
保守原则:当不确定时,倾向于判断为 task,宁可多走一次规划也不要丢失用户意图。
|
||||
请遵守以下规则:
|
||||
1. 只输出严格 JSON,不要输出 markdown,不要输出额外解释。
|
||||
2. 根据用户意图判断复杂度并选择路由。
|
||||
3. speak 字段始终填写:给用户看的话。
|
||||
|
||||
严格输出以下 JSON(不要输出 markdown,不要在 JSON 外补文字):
|
||||
{"intent":"chat或task","reply":"仅当intent=chat时填写你的闲聊回复,task时留空","reason":"简短判断依据"}
|
||||
路由规则:
|
||||
- direct_reply:纯闲聊、简单问答、打招呼、感谢等。speak 直接写你的完整回复。
|
||||
- execute:需要用工具处理的请求(查询日程、移动课程、排课等),但不需要先制定计划。speak 写简短确认。
|
||||
- deep_answer:复杂问题但不需要工具(如分析建议、深度解释等),需要深度思考后直接回答。speak 写过渡语(如"让我想想")。
|
||||
- plan:用户明确要求先制定计划,或涉及多阶段复杂规划。speak 写确认语。
|
||||
|
||||
粗排判断:当用户意图包含"批量安排/排课/把任务类排进日程",且上下文中有任务类 ID 时,设置 needs_rough_build=true。
|
||||
|
||||
输出协议(严格 JSON):
|
||||
{"route":"direct_reply / execute / deep_answer / plan","speak":"给用户看的话","needs_rough_build":false,"reason":"简短判断依据"}
|
||||
|
||||
合法示例:
|
||||
|
||||
{"route":"direct_reply","speak":"你好!我是 SmartFlow 助手,有什么可以帮你的?","reason":"用户打招呼"}
|
||||
|
||||
{"route":"execute","speak":"好的,我来帮你看看今天的安排。","reason":"需要调用工具查询日程","needs_rough_build":false}
|
||||
|
||||
{"route":"execute","speak":"好的,我来帮你排课。","reason":"批量排课需求,有任务类 ID","needs_rough_build":true}
|
||||
|
||||
{"route":"deep_answer","speak":"这是个好问题,让我仔细想想。","reason":"需要深度分析但不需要工具"}
|
||||
|
||||
{"route":"plan","speak":"明白,我来帮你制定一个完整的学习计划。","reason":"用户明确要求制定计划"}
|
||||
`
|
||||
|
||||
// BuildChatIntentSystemPrompt 返回意图分类系统提示词。
|
||||
func BuildChatIntentSystemPrompt() string {
|
||||
return strings.TrimSpace(chatIntentSystemPrompt)
|
||||
// BuildChatRoutingSystemPrompt 返回路由阶段的系统提示词。
|
||||
func BuildChatRoutingSystemPrompt() string {
|
||||
return strings.TrimSpace(chatRoutingSystemPrompt)
|
||||
}
|
||||
|
||||
// BuildChatIntentMessages 组装意图分类的 messages。
|
||||
//
|
||||
// 职责边界:
|
||||
// 1. 只取最近 6 条历史,保证分类高效;
|
||||
// 2. 不注入 pinned blocks / tool schemas,分类不需要这些信息;
|
||||
// 3. 不负责解析模型输出。
|
||||
func BuildChatIntentMessages(conversationContext *newagentmodel.ConversationContext, userInput string) []*schema.Message {
|
||||
messages := make([]*schema.Message, 0, 8)
|
||||
// BuildChatRoutingMessages 组装路由阶段的 messages。
|
||||
func BuildChatRoutingMessages(ctx *newagentmodel.ConversationContext, userInput string, state *newagentmodel.CommonState) []*schema.Message {
|
||||
return buildStageMessages(
|
||||
BuildChatRoutingSystemPrompt(),
|
||||
ctx,
|
||||
BuildChatRoutingUserPrompt(ctx, userInput, state),
|
||||
)
|
||||
}
|
||||
|
||||
messages = append(messages, schema.SystemMessage(BuildChatIntentSystemPrompt()))
|
||||
// BuildChatRoutingUserPrompt 构造路由阶段的用户提示词。
|
||||
func BuildChatRoutingUserPrompt(ctx *newagentmodel.ConversationContext, userInput string, state *newagentmodel.CommonState) string {
|
||||
var sb strings.Builder
|
||||
|
||||
if conversationContext != nil {
|
||||
history := conversationContext.HistorySnapshot()
|
||||
if len(history) > 6 {
|
||||
history = history[len(history)-6:]
|
||||
sb.WriteString("请判断用户本轮意图的复杂度,并选择最合适的路由。\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)
|
||||
}
|
||||
if len(history) > 0 {
|
||||
messages = append(messages, history...)
|
||||
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")
|
||||
}
|
||||
}
|
||||
|
||||
// 只在 history 末尾还没有当前用户消息时才追加,
|
||||
// 避免与 loadConversationContext 的预追加产生重复。
|
||||
trimmedInput := strings.TrimSpace(userInput)
|
||||
if trimmedInput != "" {
|
||||
alreadyLast := len(messages) > 0 &&
|
||||
messages[len(messages)-1].Role == schema.User &&
|
||||
messages[len(messages)-1].Content == trimmedInput
|
||||
if !alreadyLast {
|
||||
messages = append(messages, schema.UserMessage(trimmedInput))
|
||||
}
|
||||
sb.WriteString("\n用户本轮输入:\n")
|
||||
sb.WriteString(trimmedInput)
|
||||
sb.WriteString("\n")
|
||||
}
|
||||
|
||||
return messages
|
||||
return strings.TrimSpace(sb.String())
|
||||
}
|
||||
|
||||
// --- 深度回答 prompt ---
|
||||
|
||||
const deepAnswerSystemPrompt = `
|
||||
你是 SmartFlow 的深度分析助手。用户提出了一个需要深入思考的问题,请认真分析后给出详细、有价值的回答。
|
||||
|
||||
请遵守以下规则:
|
||||
1. 充分利用上下文中已有的信息(任务类约束、日程数据、历史对话等)。
|
||||
2. 如果缺少关键信息,在回答中说明需要哪些额外信息。
|
||||
3. 直接输出你的回答,不要输出 JSON。
|
||||
`
|
||||
|
||||
// BuildDeepAnswerSystemPrompt 返回深度回答阶段的系统提示词。
|
||||
func BuildDeepAnswerSystemPrompt() string {
|
||||
return strings.TrimSpace(deepAnswerSystemPrompt)
|
||||
}
|
||||
|
||||
// BuildDeepAnswerMessages 组装深度回答阶段的 messages。
|
||||
func BuildDeepAnswerMessages(ctx *newagentmodel.ConversationContext, userInput string) []*schema.Message {
|
||||
return buildStageMessages(
|
||||
BuildDeepAnswerSystemPrompt(),
|
||||
ctx,
|
||||
userInput,
|
||||
)
|
||||
}
|
||||
|
||||
@@ -8,9 +8,9 @@ import (
|
||||
"github.com/cloudwego/eino/schema"
|
||||
)
|
||||
|
||||
const executeSystemPrompt = `
|
||||
const executeSystemPromptWithPlan = `
|
||||
你是 SmartFlow NewAgent 的执行器。
|
||||
你的职责是在“当前 plan 步骤”的约束下,进行思考、执行、观察,再决定下一步动作。
|
||||
你的职责是在"当前 plan 步骤"的约束下,进行思考、执行、观察,再决定下一步动作。
|
||||
|
||||
请遵守以下规则:
|
||||
1. 只围绕当前步骤行动,不要擅自跳到其他 plan 步骤。
|
||||
@@ -19,7 +19,7 @@ const executeSystemPrompt = `
|
||||
4. 只有当你确认整个任务已经完成时,才输出 action=done,且必须在 goal_check 中总结整体完成证据。
|
||||
5. 如果执行当前步骤缺少关键上下文,且无法通过已有历史或工具补齐,输出 action=ask_user。
|
||||
6. 不要伪造工具结果;如果尚未真正拿到观察结果,就不要假装已经完成。
|
||||
7. goal_check 是你输出 next_plan / done 时的强制字段,禁止为空;必须显式地逐条对照 done_when,说明”哪些条件已满足、依据是什么”。
|
||||
7. goal_check 是你输出 next_plan / done 时的强制字段,禁止为空;必须显式地逐条对照 done_when,说明"哪些条件已满足、依据是什么"。
|
||||
|
||||
你会看到:
|
||||
- 当前完整 plan
|
||||
@@ -28,15 +28,43 @@ const executeSystemPrompt = `
|
||||
- 工具摘要
|
||||
- 历史对话与历史观察
|
||||
|
||||
请把注意力聚焦在”当前步骤是否完成,以及下一步最合理的执行动作”上。
|
||||
请把注意力聚焦在"当前步骤是否完成,以及下一步最合理的执行动作"上。
|
||||
`
|
||||
|
||||
const executeSystemPromptReAct = `
|
||||
你是 SmartFlow NewAgent 的执行器,当前为自由执行模式(无预定义计划步骤)。
|
||||
你需要根据用户意图,自主决定使用哪些工具来完成任务。
|
||||
|
||||
请遵守以下规则:
|
||||
1. 每轮先分析当前情况,决定下一步动作。
|
||||
2. 只输出严格 JSON,不要输出 markdown,不要输出额外解释,不要在 JSON 外再补文字。
|
||||
3. 需要查询数据 → 输出 action=continue 并附带 tool_call。
|
||||
4. 需要修改数据(写操作)→ 输出 action=confirm 并附带 tool_call,等待用户确认。
|
||||
5. 缺少关键信息且无法通过工具补齐 → 输出 action=ask_user。
|
||||
6. 任务完成 → 输出 action=done,并在 goal_check 中总结完成证据。
|
||||
7. 不要伪造工具结果;如果尚未真正拿到观察结果,就不要假装已经完成。
|
||||
8. 尽量高效:能用一次工具调用完成的,不要分多轮。
|
||||
|
||||
你会看到:
|
||||
- 用户原始请求
|
||||
- 置顶上下文块(粗排结果等)
|
||||
- 工具摘要
|
||||
- 历史对话与历史观察
|
||||
|
||||
请直接行动,不要犹豫,不要重复已经做过的操作。
|
||||
`
|
||||
|
||||
// BuildExecuteSystemPrompt 返回执行阶段系统提示词。
|
||||
func BuildExecuteSystemPrompt() string {
|
||||
return strings.TrimSpace(executeSystemPrompt)
|
||||
return strings.TrimSpace(executeSystemPromptWithPlan)
|
||||
}
|
||||
|
||||
// BuildExecuteDecisionContractText 返回执行阶段的输出协议说明。
|
||||
// BuildExecuteReActSystemPrompt 返回纯 ReAct 模式的系统提示词。
|
||||
func BuildExecuteReActSystemPrompt() string {
|
||||
return strings.TrimSpace(executeSystemPromptReAct)
|
||||
}
|
||||
|
||||
// BuildExecuteDecisionContractText 返回执行阶段的输出协议说明(有 plan 模式)。
|
||||
func BuildExecuteDecisionContractText() string {
|
||||
return strings.TrimSpace(fmt.Sprintf(`
|
||||
输出协议(严格 JSON):
|
||||
@@ -86,16 +114,76 @@ func BuildExecuteDecisionContractText() string {
|
||||
))
|
||||
}
|
||||
|
||||
// BuildExecuteReActContractText 返回纯 ReAct 模式的输出协议说明。
|
||||
func BuildExecuteReActContractText() string {
|
||||
return strings.TrimSpace(fmt.Sprintf(`
|
||||
输出协议(严格 JSON):
|
||||
- speak:给用户看的话(可以是分析结果、中间进展、或最终回复)
|
||||
- action:只能是 %s / %s / %s / %s
|
||||
- reason:给后端和日志看的简短说明
|
||||
- goal_check:输出 %s 时必填,总结任务完成证据
|
||||
- tool_call:输出 %s 时可附带写工具意图(需 confirm),输出 %s 时可附带读工具调用
|
||||
- tool_call 格式:{"name": "工具名", "arguments": {...}}
|
||||
|
||||
合法示例:
|
||||
{
|
||||
"speak": "我来查一下今天的安排。",
|
||||
"action": "%s",
|
||||
"reason": "需要调用 get_overview 查询",
|
||||
"tool_call": {
|
||||
"name": "get_overview",
|
||||
"arguments": {}
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
"speak": "已将概率论移到周三第1-2节。",
|
||||
"action": "%s",
|
||||
"reason": "用户要求移动课程,写操作需确认",
|
||||
"tool_call": {
|
||||
"name": "move",
|
||||
"arguments": {"task_state_id": 5, "target_day": 3, "target_slot_start": 1, "target_slot_end": 2}
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
"speak": "今天共3节课,分别是...",
|
||||
"action": "%s",
|
||||
"reason": "查询完成,已回答用户",
|
||||
"goal_check": "已通过 get_overview 查到今天的课程并展示给用户"
|
||||
}
|
||||
`,
|
||||
newagentmodel.ExecuteActionContinue,
|
||||
newagentmodel.ExecuteActionAskUser,
|
||||
newagentmodel.ExecuteActionConfirm,
|
||||
newagentmodel.ExecuteActionDone,
|
||||
newagentmodel.ExecuteActionDone,
|
||||
newagentmodel.ExecuteActionConfirm,
|
||||
newagentmodel.ExecuteActionContinue,
|
||||
newagentmodel.ExecuteActionContinue,
|
||||
newagentmodel.ExecuteActionConfirm,
|
||||
newagentmodel.ExecuteActionDone,
|
||||
))
|
||||
}
|
||||
|
||||
// BuildExecuteMessages 组装执行阶段的 messages。
|
||||
func BuildExecuteMessages(state *newagentmodel.CommonState, ctx *newagentmodel.ConversationContext) []*schema.Message {
|
||||
if state != nil && state.HasPlan() {
|
||||
return buildStageMessages(
|
||||
BuildExecuteSystemPrompt(),
|
||||
ctx,
|
||||
BuildExecuteUserPrompt(state),
|
||||
)
|
||||
}
|
||||
// 无 plan:纯 ReAct 模式。
|
||||
return buildStageMessages(
|
||||
BuildExecuteSystemPrompt(),
|
||||
BuildExecuteReActSystemPrompt(),
|
||||
ctx,
|
||||
BuildExecuteUserPrompt(state),
|
||||
BuildExecuteReActUserPrompt(state),
|
||||
)
|
||||
}
|
||||
|
||||
// BuildExecuteUserPrompt 构造执行阶段的用户提示词。
|
||||
// BuildExecuteUserPrompt 构造有 plan 模式的用户提示词。
|
||||
func BuildExecuteUserPrompt(state *newagentmodel.CommonState) string {
|
||||
var sb strings.Builder
|
||||
|
||||
@@ -132,3 +220,24 @@ func BuildExecuteUserPrompt(state *newagentmodel.CommonState) string {
|
||||
|
||||
return strings.TrimSpace(sb.String())
|
||||
}
|
||||
|
||||
// BuildExecuteReActUserPrompt 构造纯 ReAct 模式的用户提示词。
|
||||
func BuildExecuteReActUserPrompt(state *newagentmodel.CommonState) string {
|
||||
var sb strings.Builder
|
||||
|
||||
sb.WriteString("当前为自由执行模式,无预定义计划步骤。\n")
|
||||
sb.WriteString("请根据用户意图直接使用工具完成请求。\n\n")
|
||||
|
||||
sb.WriteString(renderStateSummary(state))
|
||||
sb.WriteString("\n\n")
|
||||
|
||||
sb.WriteString("判断规则:\n")
|
||||
sb.WriteString("- 需要查询/读取数据 → action=continue + tool_call(读工具)\n")
|
||||
sb.WriteString("- 需要修改/写入数据 → action=confirm + tool_call(写工具,需用户确认)\n")
|
||||
sb.WriteString("- 缺少关键信息 → action=ask_user\n")
|
||||
sb.WriteString("- 任务完成 → action=done + goal_check\n\n")
|
||||
|
||||
sb.WriteString(BuildExecuteReActContractText())
|
||||
|
||||
return strings.TrimSpace(sb.String())
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package newagentprompt
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
newagentmodel "github.com/LoveLosita/smartflow/backend/newAgent/model"
|
||||
@@ -21,6 +22,14 @@ const planSystemPrompt = `
|
||||
6. 只输出 JSON,不要输出 markdown,不要输出额外解释,不要在 JSON 外再补文字。
|
||||
7. 每次输出前先评估任务复杂度:simple(简单明确,无复杂依赖)、moderate(多步操作,需要一定推理)、complex(需要深度推理、多方案比较或复杂依赖关系)。
|
||||
8. 根据复杂度判断 need_thinking:你是否需要深度思考才能生成高质量计划?当不确定时倾向于 false。
|
||||
9. 粗排识别规则:若满足以下两个条件,在 action=plan_done 时附加 needs_rough_build=true 和 task_class_ids:
|
||||
条件1:用户输入中存在"任务类 ID"字段(见上下文"任务类 ID"部分);
|
||||
条件2:用户意图明确是"批量安排/帮我排课/把任务类排进日程"等批量调度需求。
|
||||
满足时:后端会在用户确认计划后自动运行粗排算法(硬性约束已由算法保证,无需 LLM 校验)。
|
||||
你的 plan_steps 应聚焦于"用读写工具优化方案",建议两步:
|
||||
第1步:用 get_overview / find_free 等读工具审视粗排结果,找出可优化的点(时段分布不均、空位未利用等);
|
||||
第2步:用 move / batch_move 等写工具微调后,将最终方案展示给用户确认。
|
||||
禁止安排任何"校验/验证约束"步骤——硬性约束由算法兜底,LLM 不需要操心。
|
||||
|
||||
你会看到:
|
||||
- 当前阶段与轮次信息
|
||||
@@ -63,6 +72,15 @@ func BuildPlanUserPrompt(state *newagentmodel.CommonState, userInput string) str
|
||||
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 != "" {
|
||||
sb.WriteString("\n用户本轮输入:\n")
|
||||
@@ -84,39 +102,41 @@ func BuildPlanDecisionContractText() string {
|
||||
- need_thinking:是否需要深度思考才能生成高质量计划,只能是 true / false
|
||||
- plan_steps:仅当 action=%s 时允许返回;返回时必须是完整计划,不是增量
|
||||
- plan_steps[].content:步骤正文,必填
|
||||
- plan_steps[].done_when:可选,建议写”什么情况下算这一步做完”
|
||||
- plan_steps[].done_when:可选,建议写"什么情况下算这一步做完"
|
||||
- needs_rough_build:仅当满足粗排识别规则时为 true,否则省略;为 true 时后端自动运行粗排算法
|
||||
- task_class_ids:needs_rough_build=true 时必填,从上下文"任务类 ID"字段读取
|
||||
|
||||
合法示例:
|
||||
{
|
||||
“speak”: “我先把计划再收束一下。”,
|
||||
“action”: “%s”,
|
||||
“reason”: “当前信息已足够继续规划”,
|
||||
“complexity”: “moderate”,
|
||||
“need_thinking”: false
|
||||
"speak": "我先把计划再收束一下。",
|
||||
"action": "%s",
|
||||
"reason": "当前信息已足够继续规划",
|
||||
"complexity": "moderate",
|
||||
"need_thinking": false
|
||||
}
|
||||
|
||||
{
|
||||
“speak”: “你更希望我优先安排今天,还是按整周来规划?”,
|
||||
“action”: “%s”,
|
||||
“reason”: “当前时间范围仍不明确”,
|
||||
“complexity”: “simple”,
|
||||
“need_thinking”: false
|
||||
"speak": "你更希望我优先安排今天,还是按整周来规划?",
|
||||
"action": "%s",
|
||||
"reason": "当前时间范围仍不明确",
|
||||
"complexity": "simple",
|
||||
"need_thinking": false
|
||||
}
|
||||
|
||||
{
|
||||
“speak”: “计划已经整理好了,我先给你确认一下。”,
|
||||
“action”: “%s”,
|
||||
“reason”: “当前计划已具备执行条件”,
|
||||
“complexity”: “simple”,
|
||||
“need_thinking”: false,
|
||||
“plan_steps”: [
|
||||
"speak": "计划已经整理好了,我先给你确认一下。",
|
||||
"action": "%s",
|
||||
"reason": "当前计划已具备执行条件",
|
||||
"complexity": "simple",
|
||||
"need_thinking": false,
|
||||
"plan_steps": [
|
||||
{
|
||||
“content”: “先确认本周可用时间范围”,
|
||||
“done_when”: “拿到明确的可用时间段列表”
|
||||
"content": "先确认本周可用时间范围",
|
||||
"done_when": "拿到明确的可用时间段列表"
|
||||
},
|
||||
{
|
||||
“content”: “基于可用时间生成执行安排”,
|
||||
“done_when”: “得到一份用户可确认的安排方案”
|
||||
"content": "基于可用时间生成执行安排",
|
||||
"done_when": "得到一份用户可确认的安排方案"
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
package newagentstream
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
import "log"
|
||||
|
||||
// NewSSEPayloadEmitter 创建将 chunk 事件写入 outChan 的 emitter。
|
||||
//
|
||||
@@ -10,7 +8,7 @@ import (
|
||||
// 1. 接收 outChan(SSE 输出通道),返回 PayloadEmitter 函数;
|
||||
// 2. 只把原始 JSON payload 写入通道,不添加 "data: " 前缀和 "\n\n" 后缀;
|
||||
// 3. SSE 格式化("data: " + payload + "\n\n")由 API 层的 writeSSEData 统一处理;
|
||||
// 4. 发送失败时返回 error,但不关闭通道(通道由调用方管理)。
|
||||
// 4. 通道满时静默丢弃并返回 nil,让图继续完成状态持久化,避免因客户端超时而丢失快照。
|
||||
//
|
||||
// 使用示例:
|
||||
//
|
||||
@@ -22,17 +20,18 @@ func NewSSEPayloadEmitter(outChan chan<- string) PayloadEmitter {
|
||||
if outChan == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
if payload == "" {
|
||||
return nil
|
||||
}
|
||||
|
||||
select {
|
||||
case outChan <- payload:
|
||||
return nil
|
||||
default:
|
||||
// 通道已满或已关闭:不阻塞,直接返回错误。
|
||||
return fmt.Errorf("outChan full or closed")
|
||||
// 通道已满:客户端可能已断开或消费过慢。
|
||||
// 静默丢弃此 chunk,让图继续执行并完成状态持久化。
|
||||
// 客户端重连后可从 Redis 快照恢复,不需要这条消息。
|
||||
log.Printf("[WARN] SSE outChan full, dropping payload (len=%d)", len(payload))
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -83,9 +83,45 @@ func GetOverview(state *ScheduleState) string {
|
||||
sb.WriteString(strings.Join(pendingParts, " ") + "\n")
|
||||
}
|
||||
|
||||
// 6. 任务类约束(排课策略与限制)。
|
||||
if len(state.TaskClasses) > 0 {
|
||||
sb.WriteString("\n任务类约束(排课时请遵守):\n")
|
||||
for _, tc := range state.TaskClasses {
|
||||
strategy := formatStrategy(tc.Strategy)
|
||||
allow := "否"
|
||||
if tc.AllowFillerCourse {
|
||||
allow = "是"
|
||||
}
|
||||
line := fmt.Sprintf(" [%s] 策略=%s 总预算=%d节 允许嵌水课=%s", tc.Name, strategy, tc.TotalSlots, allow)
|
||||
if len(tc.ExcludedSlots) > 0 {
|
||||
parts := make([]string, len(tc.ExcludedSlots))
|
||||
for i, s := range tc.ExcludedSlots {
|
||||
parts[i] = fmt.Sprintf("%d", s)
|
||||
}
|
||||
line += fmt.Sprintf(" 排除时段=[%s]", strings.Join(parts, ","))
|
||||
}
|
||||
sb.WriteString(line + "\n")
|
||||
}
|
||||
}
|
||||
|
||||
return sb.String()
|
||||
}
|
||||
|
||||
// formatStrategy 将 strategy 字段值转为中文描述。
|
||||
func formatStrategy(strategy string) string {
|
||||
switch strategy {
|
||||
case "steady":
|
||||
return "均匀分布"
|
||||
case "rapid":
|
||||
return "集中突击"
|
||||
default:
|
||||
if strategy == "" {
|
||||
return "默认"
|
||||
}
|
||||
return strategy
|
||||
}
|
||||
}
|
||||
|
||||
// QueryRange 查看某天(或某天某段)的细粒度占用详情。
|
||||
// day 必填,slotStart/slotEnd 选填(nil 表示查整天)。
|
||||
// 整天模式按标准段(1-2, 3-4, ..., 11-12)分组输出。
|
||||
|
||||
@@ -20,6 +20,19 @@ type TaskSlot struct {
|
||||
SlotEnd int `json:"slot_end"`
|
||||
}
|
||||
|
||||
// TaskClassMeta 是任务类级别的调度约束,供 LLM 在排课时参考。
|
||||
// 只记录影响排课决策的字段,不暴露数据库内部细节。
|
||||
type TaskClassMeta struct {
|
||||
ID int `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Strategy string `json:"strategy"` // "steady"=均匀分布 | "rapid"=集中突击
|
||||
TotalSlots int `json:"total_slots"` // 该任务类总时段预算
|
||||
AllowFillerCourse bool `json:"allow_filler_course"` // 是否允许嵌入水课时段
|
||||
ExcludedSlots []int `json:"excluded_slots"` // 排除的半天时段索引(空=无限制)
|
||||
StartDate string `json:"start_date,omitempty"` // 排程起始日期(YYYY-MM-DD)
|
||||
EndDate string `json:"end_date,omitempty"` // 排程截止日期(YYYY-MM-DD)
|
||||
}
|
||||
|
||||
// ScheduleTask is a unified task representation in the tool state.
|
||||
// It merges existing schedules (from schedule_events) and pending tasks (from task_items)
|
||||
// into one flat list that the tool layer operates on.
|
||||
@@ -36,7 +49,9 @@ type ScheduleTask struct {
|
||||
Slots []TaskSlot `json:"slots,omitempty"`
|
||||
// Pending task: required consecutive slot count.
|
||||
Duration int `json:"duration,omitempty"`
|
||||
// source=task_item only: TaskClass.ID for category lookup.
|
||||
// source=task_item only: TaskClass.ID,用于反查任务类约束。
|
||||
TaskClassID int `json:"task_class_id,omitempty"`
|
||||
// source=task_item only: TaskClass.ID for category lookup (internal alias).
|
||||
CategoryID int `json:"category_id,omitempty"`
|
||||
// source=event only: whether this slot allows embedding other tasks.
|
||||
CanEmbed bool `json:"can_embed,omitempty"`
|
||||
@@ -51,8 +66,9 @@ type ScheduleTask struct {
|
||||
|
||||
// ScheduleState is the full tool operation state.
|
||||
type ScheduleState struct {
|
||||
Window ScheduleWindow `json:"window"`
|
||||
Tasks []ScheduleTask `json:"tasks"`
|
||||
Window ScheduleWindow `json:"window"`
|
||||
Tasks []ScheduleTask `json:"tasks"`
|
||||
TaskClasses []TaskClassMeta `json:"task_classes,omitempty"` // 任务类约束元数据,供 LLM 排课参考
|
||||
}
|
||||
|
||||
// DayToWeekDay converts day_index to (week, day_of_week).
|
||||
@@ -95,9 +111,11 @@ func (s *ScheduleState) Clone() *ScheduleState {
|
||||
TotalDays: s.Window.TotalDays,
|
||||
DayMapping: make([]DayMapping, len(s.Window.DayMapping)),
|
||||
},
|
||||
Tasks: make([]ScheduleTask, len(s.Tasks)),
|
||||
Tasks: make([]ScheduleTask, len(s.Tasks)),
|
||||
TaskClasses: make([]TaskClassMeta, len(s.TaskClasses)),
|
||||
}
|
||||
copy(clone.Window.DayMapping, s.Window.DayMapping)
|
||||
copy(clone.TaskClasses, s.TaskClasses)
|
||||
for i, t := range s.Tasks {
|
||||
clone.Tasks[i] = t
|
||||
if t.Slots != nil {
|
||||
|
||||
Reference in New Issue
Block a user