后端:
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
前端:无
仓库:无
136 lines
4.8 KiB
Go
136 lines
4.8 KiB
Go
package newagenttools
|
||
|
||
// DayMapping maps a day_index to a real (week, day_of_week) coordinate.
|
||
type DayMapping struct {
|
||
DayIndex int `json:"day_index"`
|
||
Week int `json:"week"`
|
||
DayOfWeek int `json:"day_of_week"`
|
||
}
|
||
|
||
// ScheduleWindow defines the planning window.
|
||
type ScheduleWindow struct {
|
||
TotalDays int `json:"total_days"`
|
||
DayMapping []DayMapping `json:"day_mapping"`
|
||
}
|
||
|
||
// TaskSlot is a compressed time slot using day_index and section range.
|
||
type TaskSlot struct {
|
||
Day int `json:"day"`
|
||
SlotStart int `json:"slot_start"`
|
||
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.
|
||
type ScheduleTask struct {
|
||
StateID int `json:"state_id"`
|
||
Source string `json:"source"` // "event" | "task_item"
|
||
SourceID int `json:"source_id"` // ScheduleEvent.ID or TaskClassItem.ID
|
||
Name string `json:"name"`
|
||
Category string `json:"category"` // e.g. "课程", "学习", "作业"
|
||
Status string `json:"status"` // "existing" | "pending"
|
||
Locked bool `json:"locked"`
|
||
|
||
// Existing task: compressed slot ranges. Pending task: nil until placed.
|
||
Slots []TaskSlot `json:"slots,omitempty"`
|
||
// Pending task: required consecutive slot count.
|
||
Duration int `json:"duration,omitempty"`
|
||
// 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"`
|
||
|
||
// Embed relationships (resolved after all tasks are loaded).
|
||
EmbeddedBy *int `json:"embedded_by,omitempty"` // host: which state_id is embedded into me
|
||
EmbedHost *int `json:"embed_host,omitempty"` // guest: which state_id's slot I'm embedded into
|
||
|
||
// Internal: not exposed to LLM, used for flush/diff logic.
|
||
EventType string `json:"event_type,omitempty"` // "course" | "task" (source=event only)
|
||
}
|
||
|
||
// ScheduleState is the full tool operation state.
|
||
type ScheduleState struct {
|
||
Window ScheduleWindow `json:"window"`
|
||
Tasks []ScheduleTask `json:"tasks"`
|
||
TaskClasses []TaskClassMeta `json:"task_classes,omitempty"` // 任务类约束元数据,供 LLM 排课参考
|
||
}
|
||
|
||
// DayToWeekDay converts day_index to (week, day_of_week).
|
||
func (s *ScheduleState) DayToWeekDay(day int) (week, dayOfWeek int, ok bool) {
|
||
for _, m := range s.Window.DayMapping {
|
||
if m.DayIndex == day {
|
||
return m.Week, m.DayOfWeek, true
|
||
}
|
||
}
|
||
return 0, 0, false
|
||
}
|
||
|
||
// WeekDayToDay converts (week, day_of_week) to day_index.
|
||
func (s *ScheduleState) WeekDayToDay(week, dayOfWeek int) (day int, ok bool) {
|
||
for _, m := range s.Window.DayMapping {
|
||
if m.Week == week && m.DayOfWeek == dayOfWeek {
|
||
return m.DayIndex, true
|
||
}
|
||
}
|
||
return 0, false
|
||
}
|
||
|
||
// TaskByStateID finds a task by state_id. Returns nil if not found.
|
||
func (s *ScheduleState) TaskByStateID(stateID int) *ScheduleTask {
|
||
for i := range s.Tasks {
|
||
if s.Tasks[i].StateID == stateID {
|
||
return &s.Tasks[i]
|
||
}
|
||
}
|
||
return nil
|
||
}
|
||
|
||
// Clone returns a deep copy of the ScheduleState.
|
||
func (s *ScheduleState) Clone() *ScheduleState {
|
||
if s == nil {
|
||
return nil
|
||
}
|
||
clone := &ScheduleState{
|
||
Window: ScheduleWindow{
|
||
TotalDays: s.Window.TotalDays,
|
||
DayMapping: make([]DayMapping, len(s.Window.DayMapping)),
|
||
},
|
||
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 {
|
||
clone.Tasks[i].Slots = make([]TaskSlot, len(t.Slots))
|
||
copy(clone.Tasks[i].Slots, t.Slots)
|
||
}
|
||
if t.EmbeddedBy != nil {
|
||
v := *t.EmbeddedBy
|
||
clone.Tasks[i].EmbeddedBy = &v
|
||
}
|
||
if t.EmbedHost != nil {
|
||
v := *t.EmbedHost
|
||
clone.Tasks[i].EmbedHost = &v
|
||
}
|
||
}
|
||
return clone
|
||
}
|