后端:
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
前端:无
仓库:无
131 lines
4.4 KiB
Go
131 lines
4.4 KiB
Go
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
|
||
}
|
||
}
|
||
}
|