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

@@ -67,7 +67,7 @@ func Start() {
// outbox 通用事件总线接线(第二阶段): // outbox 通用事件总线接线(第二阶段):
// 1. 读取 Kafka 配置; // 1. 读取 Kafka 配置;
// 2. 创建 infra 级 EventBus // 2. 创建 infra 级 EventBus
// 3. 显式注册聊天持久化事件处理器; // 3. 显式注册"聊天持久化"事件处理器;
// 4. 启动总线后台 dispatch/consume 循环。 // 4. 启动总线后台 dispatch/consume 循环。
kafkaCfg := kafkabus.LoadConfig() kafkaCfg := kafkabus.LoadConfig()
eventBus, err := outboxinfra.NewEventBus(outboxRepo, kafkaCfg) eventBus, err := outboxinfra.NewEventBus(outboxRepo, kafkaCfg)
@@ -75,9 +75,9 @@ func Start() {
log.Fatalf("Failed to initialize outbox event bus: %v", err) log.Fatalf("Failed to initialize outbox event bus: %v", err)
} }
if eventBus != nil { if eventBus != nil {
// 3. 在启动前完成业务事件处理器注册。 // 3. 在启动前完成"业务事件处理器"注册。
// 3.1 这里显式调用 service/events保证 infra 层不承载业务语义。 // 3.1 这里显式调用 service/events保证 infra 层不承载业务语义。
// 3.2 若注册失败直接中止启动,避免消息已入队但无人消费的隐性故障。 // 3.2 若注册失败直接中止启动,避免"消息已入队但无人消费"的隐性故障。
if err = eventsvc.RegisterChatHistoryPersistHandler(eventBus, outboxRepo, manager); err != nil { if err = eventsvc.RegisterChatHistoryPersistHandler(eventBus, outboxRepo, manager); err != nil {
log.Fatalf("Failed to register chat history event handler: %v", err) log.Fatalf("Failed to register chat history event handler: %v", err)
} }
@@ -87,6 +87,9 @@ func Start() {
if err = eventsvc.RegisterChatTokenUsageAdjustHandler(eventBus, outboxRepo, manager); err != nil { if err = eventsvc.RegisterChatTokenUsageAdjustHandler(eventBus, outboxRepo, manager); err != nil {
log.Fatalf("Failed to register chat token usage adjust event handler: %v", err) log.Fatalf("Failed to register chat token usage adjust event handler: %v", err)
} }
if err = eventsvc.RegisterAgentStateSnapshotHandler(eventBus, outboxRepo, manager); err != nil {
log.Fatalf("Failed to register agent state snapshot event handler: %v", err)
}
eventBus.Start(context.Background()) eventBus.Start(context.Background())
defer eventBus.Close() defer eventBus.Close()
log.Println("Outbox event bus started") log.Println("Outbox event bus started")

View File

@@ -69,25 +69,28 @@ func applyScheduleChange(ctx context.Context, manager *dao.RepoManager, change S
// applyPlaceChange 应用放置变更。 // applyPlaceChange 应用放置变更。
func applyPlaceChange(ctx context.Context, manager *dao.RepoManager, change ScheduleChange, userID int) error { func applyPlaceChange(ctx context.Context, manager *dao.RepoManager, change ScheduleChange, userID int) error {
// Placepending → placed为现有 Event 创建 Schedule
// 前提Event 已经存在SourceID 是 ScheduleEvent.ID
// NewCoords 包含所有需要放置的位置(可能多天/多节)
if len(change.NewCoords) == 0 { if len(change.NewCoords) == 0 {
return fmt.Errorf("place 变更缺少目标位置") return fmt.Errorf("place 变更缺少目标位置")
} }
switch change.Source {
if change.Source != "event" || change.SourceID == 0 { case "event":
return fmt.Errorf("place 变更需要有效的 event source") return applyPlaceEventSource(ctx, manager, change, userID)
case "task_item":
return applyPlaceTaskItem(ctx, manager, change, userID)
default:
return fmt.Errorf("place 变更不支持的 source: %s", change.Source)
} }
}
// 按周天分组,压缩成 slot ranges // applyPlaceEventSource 处理 source=event 的放置(为已有 Event 创建 Schedule 记录)。
func applyPlaceEventSource(ctx context.Context, manager *dao.RepoManager, change ScheduleChange, userID int) error {
if change.SourceID == 0 {
return fmt.Errorf("place event 变更需要有效的 source_id")
}
groups := groupCoordsByWeekDay(change.NewCoords) groups := groupCoordsByWeekDay(change.NewCoords)
for week, dayGroups := range groups { for week, dayGroups := range groups {
for dayOfWeek, coords := range dayGroups { for dayOfWeek, coords := range dayGroups {
startSection, endSection := minMaxSection(coords) startSection, endSection := minMaxSection(coords)
// 创建 schedule 记录event 已存在,只创建 schedule
schedules := make([]model.Schedule, endSection-startSection+1) schedules := make([]model.Schedule, endSection-startSection+1)
for sec := startSection; sec <= endSection; sec++ { for sec := startSection; sec <= endSection; sec++ {
schedules[sec-startSection] = model.Schedule{ schedules[sec-startSection] = model.Schedule{
@@ -98,10 +101,7 @@ func applyPlaceChange(ctx context.Context, manager *dao.RepoManager, change Sche
EventID: change.SourceID, EventID: change.SourceID,
} }
} }
if _, err := manager.Schedule.AddSchedules(schedules); err != nil {
// 批量创建
_, err := manager.Schedule.AddSchedules(schedules)
if err != nil {
return fmt.Errorf("创建 schedule 失败: %w", err) return fmt.Errorf("创建 schedule 失败: %w", err)
} }
} }
@@ -109,29 +109,134 @@ func applyPlaceChange(ctx context.Context, manager *dao.RepoManager, change Sche
return nil return nil
} }
// applyMoveChange 应用移动变更 // applyPlaceTaskItem 处理 source=task_item 的放置
func applyMoveChange(ctx context.Context, manager *dao.RepoManager, change ScheduleChange, userID int) error { //
// Move已有 schedule只更新位置 // 两条路径:
// 需要删除旧位置的 schedule在新位置创建新 schedule // 1. 嵌入水课HostEventID != 0在宿主 Schedule 记录上设置 embedded_task_id。
// 2. 普通放置HostEventID == 0新建 ScheduleEvent(type=task) + Schedule 记录。
// 两条路径最终都更新 task_items.embedded_time。
func applyPlaceTaskItem(ctx context.Context, manager *dao.RepoManager, change ScheduleChange, userID int) error {
if change.SourceID == 0 {
return fmt.Errorf("place task_item 变更需要有效的 source_id")
}
// 1. 删除旧位置 // task_item 只占一段连续时段,取第一个 coord 的 week/dayOfWeek
if change.Source == "event" && change.SourceID != 0 { first := change.NewCoords[0]
if err := manager.Schedule.DeleteScheduleEventAndSchedule(ctx, change.SourceID, userID); err != nil { week, dayOfWeek := first.Week, first.DayOfWeek
return fmt.Errorf("删除旧位置失败: %w", err) startSection, endSection := minMaxSection(change.NewCoords)
targetTime := &model.TargetTime{
Week: week,
DayOfWeek: dayOfWeek,
SectionFrom: startSection,
SectionTo: endSection,
}
if change.HostEventID != 0 {
// 嵌入路径:更新宿主 Schedule 记录的 embedded_task_id
if err := manager.Schedule.EmbedTaskIntoSchedule(
startSection, endSection, dayOfWeek, week, userID, change.SourceID,
); err != nil {
return fmt.Errorf("嵌入水课失败: %w", err)
}
} else {
// 普通路径:新建 ScheduleEvent + Schedule 记录
startTime, endTime, err := RelativeTimeToRealTime(week, dayOfWeek, startSection, endSection)
if err != nil {
return fmt.Errorf("时间转换失败: %w", err)
}
relID := change.SourceID
event := model.ScheduleEvent{
UserID: userID,
Name: change.Name,
Type: "task",
RelID: &relID,
CanBeEmbedded: false,
StartTime: startTime,
EndTime: endTime,
}
eventID, err := manager.Schedule.AddScheduleEvent(&event)
if err != nil {
return fmt.Errorf("创建 schedule_event 失败: %w", err)
}
schedules := make([]model.Schedule, endSection-startSection+1)
for i, sec := 0, startSection; sec <= endSection; i, sec = i+1, sec+1 {
schedules[i] = model.Schedule{
UserID: userID,
Week: week,
DayOfWeek: dayOfWeek,
Section: sec,
EventID: eventID,
Status: "normal",
}
}
if _, err := manager.Schedule.AddSchedules(schedules); err != nil {
return fmt.Errorf("创建 schedule 记录失败: %w", err)
} }
} }
// 2. 创建新位置(复用 place 逻辑) if err := manager.TaskClass.UpdateTaskClassItemEmbeddedTime(ctx, change.SourceID, targetTime); err != nil {
return fmt.Errorf("更新 task_item embedded_time 失败: %w", err)
}
return nil
}
// applyMoveChange 应用移动变更。
func applyMoveChange(ctx context.Context, manager *dao.RepoManager, change ScheduleChange, userID int) error {
switch change.Source {
case "event":
if change.SourceID != 0 {
if err := manager.Schedule.DeleteScheduleEventAndSchedule(ctx, change.SourceID, userID); err != nil {
return fmt.Errorf("删除旧位置失败: %w", err)
}
}
case "task_item":
// 清理旧位置
if change.OldHostEventID != 0 {
// 旧位置是嵌入:清空宿主的 embedded_task_id
if _, err := manager.Schedule.SetScheduleEmbeddedTaskIDToNull(ctx, change.OldHostEventID); err != nil {
return fmt.Errorf("清除旧嵌入关系失败: %w", err)
}
} else {
// 旧位置是普通 task event按 task_item_id 删除
if err := manager.Schedule.DeleteScheduleEventByTaskItemID(ctx, change.SourceID); err != nil {
return fmt.Errorf("删除旧 task_item 日程失败: %w", err)
}
}
}
return applyPlaceChange(ctx, manager, change, userID) return applyPlaceChange(ctx, manager, change, userID)
} }
// applyUnplaceChange 应用移除变更。 // applyUnplaceChange 应用移除变更。
func applyUnplaceChange(ctx context.Context, manager *dao.RepoManager, change ScheduleChange, userID int) error { func applyUnplaceChange(ctx context.Context, manager *dao.RepoManager, change ScheduleChange, userID int) error {
// Unplace删除 schedule任务恢复为 pending switch change.Source {
if change.Source == "event" && change.SourceID != 0 { case "event":
if change.SourceID == 0 {
return fmt.Errorf("unplace event 变更需要有效的 source_id")
}
return manager.Schedule.DeleteScheduleEventAndSchedule(ctx, change.SourceID, userID) return manager.Schedule.DeleteScheduleEventAndSchedule(ctx, change.SourceID, userID)
case "task_item":
if change.SourceID == 0 {
return fmt.Errorf("unplace task_item 变更需要有效的 source_id")
}
if change.HostEventID != 0 {
// 是嵌入:清空宿主 Schedule 的 embedded_task_id
if _, err := manager.Schedule.SetScheduleEmbeddedTaskIDToNull(ctx, change.HostEventID); err != nil {
return fmt.Errorf("清除嵌入关系失败: %w", err)
}
} else {
// 普通 task event按 task_item_id 删除
if err := manager.Schedule.DeleteScheduleEventByTaskItemID(ctx, change.SourceID); err != nil {
return fmt.Errorf("删除 task_item 日程失败: %w", err)
}
}
if err := manager.TaskClass.DeleteTaskClassItemEmbeddedTime(ctx, change.SourceID); err != nil {
return fmt.Errorf("清除 task_item embedded_time 失败: %w", err)
}
return nil
default:
return fmt.Errorf("unplace 变更不支持的 source: %s", change.Source)
} }
return fmt.Errorf("unplace 变更的 source 不是 event: %s", change.Source)
} }
// ==================== 辅助函数 ==================== // ==================== 辅助函数 ====================

View File

@@ -0,0 +1,109 @@
package conv
import (
"fmt"
"github.com/LoveLosita/smartflow/backend/model"
newagenttools "github.com/LoveLosita/smartflow/backend/newAgent/tools"
)
// ScheduleStateToPreview 将 newAgent 的 ScheduleState 转换为前端预览缓存格式。
//
// 职责边界:
// 1. 只做数据格式转换,不做业务逻辑;
// 2. 将每个 ScheduleTask 的每个 TaskSlot 转为一条 HybridScheduleEntry
// 3. Day → (Week, DayOfWeek) 通过 ScheduleState.DayToWeekDay 转换;
// 4. 转换失败的 slotday_index 无效)静默跳过。
func ScheduleStateToPreview(
state *newagenttools.ScheduleState,
userID int,
conversationID string,
taskClassIDs []int,
summary string,
) *model.SchedulePlanPreviewCache {
if state == nil {
return nil
}
entries := make([]model.HybridScheduleEntry, 0, len(state.Tasks))
for i := range state.Tasks {
t := &state.Tasks[i]
// 待安排且无位置的任务不生成 entry。
if t.Status == "pending" && len(t.Slots) == 0 {
continue
}
for _, slot := range t.Slots {
week, dayOfWeek, ok := state.DayToWeekDay(slot.Day)
if !ok {
continue
}
entry := model.HybridScheduleEntry{
Week: week,
DayOfWeek: dayOfWeek,
SectionFrom: slot.SlotStart,
SectionTo: slot.SlotEnd,
Name: t.Name,
}
// Type 映射。
if t.Source == "event" {
if t.EventType != "" {
entry.Type = t.EventType
} else {
entry.Type = "course"
}
} else {
entry.Type = "task"
}
// Status 映射existing 不变pending有位置= suggested。
if t.Status == "pending" {
entry.Status = "suggested"
} else {
entry.Status = "existing"
}
// ID 映射。
if t.Source == "event" {
entry.EventID = t.SourceID
} else {
entry.TaskItemID = t.SourceID
}
// 嵌入与阻塞语义。
entry.CanBeEmbedded = t.CanEmbed
if t.Source == "event" && t.CanEmbed && t.EmbeddedBy == nil {
// 可嵌入且当前无嵌入任务 → 不阻塞 suggested 占位。
entry.BlockForSuggested = false
} else {
entry.BlockForSuggested = true
}
entries = append(entries, entry)
}
}
// 生成摘要(若调用方未提供)。
if summary == "" {
existingCount := 0
suggestedCount := 0
for _, e := range entries {
if e.Status == "existing" {
existingCount++
} else {
suggestedCount++
}
}
summary = fmt.Sprintf("共 %d 个日程条目,其中已确定 %d 个,新安排 %d 个。", len(entries), existingCount, suggestedCount)
}
return &model.SchedulePlanPreviewCache{
UserID: userID,
ConversationID: conversationID,
Summary: summary,
HybridEntries: entries,
TaskClassIDs: taskClassIDs,
}
}

View File

@@ -88,6 +88,51 @@ func (p *ScheduleProvider) loadCompleteTaskClasses(ctx context.Context, userID i
return complete, nil return complete, nil
} }
// LoadTaskClassMetas 加载指定任务类的约束元数据(不含 Items、不含日程供 Plan 阶段提前消费。
func (p *ScheduleProvider) LoadTaskClassMetas(ctx context.Context, userID int, taskClassIDs []int) ([]newagenttools.TaskClassMeta, error) {
if len(taskClassIDs) == 0 {
return nil, nil
}
complete, err := p.taskClassDAO.GetCompleteTaskClassesByIDs(ctx, userID, taskClassIDs)
if err != nil {
return nil, fmt.Errorf("加载任务类元数据失败: %w", err)
}
metas := make([]newagenttools.TaskClassMeta, 0, len(complete))
for _, tc := range complete {
meta := newagenttools.TaskClassMeta{
ID: tc.ID,
Name: derefString(tc.Name),
}
if tc.Strategy != nil {
meta.Strategy = *tc.Strategy
}
if tc.TotalSlots != nil {
meta.TotalSlots = *tc.TotalSlots
}
if tc.AllowFillerCourse != nil {
meta.AllowFillerCourse = *tc.AllowFillerCourse
}
if tc.ExcludedSlots != nil {
meta.ExcludedSlots = []int(tc.ExcludedSlots)
}
if tc.StartDate != nil {
meta.StartDate = tc.StartDate.Format("2006-01-02")
}
if tc.EndDate != nil {
meta.EndDate = tc.EndDate.Format("2006-01-02")
}
metas = append(metas, meta)
}
return metas, nil
}
func derefString(s *string) string {
if s == nil {
return ""
}
return *s
}
// buildExtraItemCategories 从已有日程中提取不属于给定 taskClasses 的 task event 的 category 映射。 // buildExtraItemCategories 从已有日程中提取不属于给定 taskClasses 的 task event 的 category 映射。
// 当加载全部 taskClass 时,通常返回空 map。 // 当加载全部 taskClass 时,通常返回空 map。
func buildExtraItemCategories(schedules []model.Schedule, taskClasses []model.TaskClass) map[int]string { func buildExtraItemCategories(schedules []model.Schedule, taskClasses []model.TaskClass) map[int]string {

View File

@@ -178,6 +178,7 @@ func LoadScheduleState(
} }
catID := tc.ID catID := tc.ID
pendingCount := 0
for _, item := range tc.Items { for _, item := range tc.Items {
if item.Status == nil || *item.Status != model.TaskItemStatusUnscheduled { if item.Status == nil || *item.Status != model.TaskItemStatusUnscheduled {
continue continue
@@ -197,17 +198,46 @@ func LoadScheduleState(
stateID := nextStateID stateID := nextStateID
state.Tasks = append(state.Tasks, newagenttools.ScheduleTask{ state.Tasks = append(state.Tasks, newagenttools.ScheduleTask{
StateID: stateID, StateID: stateID,
Source: "task_item", Source: "task_item",
SourceID: item.ID, SourceID: item.ID,
Name: name, Name: name,
Category: catName, Category: catName,
Status: "pending", Status: "pending",
Duration: duration, Duration: duration,
CategoryID: catID, CategoryID: catID,
TaskClassID: tc.ID,
}) })
itemStateIDs[item.ID] = stateID itemStateIDs[item.ID] = stateID
nextStateID++ nextStateID++
pendingCount++
}
// 有待安排 item 的任务类才暴露约束给 LLM。
if pendingCount > 0 {
meta := newagenttools.TaskClassMeta{
ID: tc.ID,
Name: catName,
}
if tc.Strategy != nil {
meta.Strategy = *tc.Strategy
}
if tc.TotalSlots != nil {
meta.TotalSlots = *tc.TotalSlots
}
if tc.AllowFillerCourse != nil {
meta.AllowFillerCourse = *tc.AllowFillerCourse
}
if tc.ExcludedSlots != nil {
meta.ExcludedSlots = []int(tc.ExcludedSlots)
}
if tc.StartDate != nil {
meta.StartDate = tc.StartDate.Format("2006-01-02")
}
if tc.EndDate != nil {
meta.EndDate = tc.EndDate.Format("2006-01-02")
}
state.TaskClasses = append(state.TaskClasses, meta)
} }
} }
@@ -286,6 +316,13 @@ type ScheduleChange struct {
NewCoords []SlotCoord NewCoords []SlotCoord
// For move/unplace: old slot positions // For move/unplace: old slot positions
OldCoords []SlotCoord OldCoords []SlotCoord
// HostEventID: source=task_item 嵌入路径时,宿主课程的 schedule_event.id。
// Place/Unplace当前操作位置的宿主 EventID0 表示非嵌入)。
// Move新位置的宿主 EventID。
HostEventID int
// OldHostEventID: Move 时旧位置的宿主 EventID0 表示旧位置非嵌入)。
OldHostEventID int
} }
// DiffScheduleState compares original and modified ScheduleState, // DiffScheduleState compares original and modified ScheduleState,
@@ -313,40 +350,44 @@ func DiffScheduleState(
// Place: pending → has slots // Place: pending → has slots
case wasPending && hasSlots: case wasPending && hasSlots:
changes = append(changes, ScheduleChange{ changes = append(changes, ScheduleChange{
Type: ChangePlace, Type: ChangePlace,
StateID: mod.StateID, StateID: mod.StateID,
Source: mod.Source, Source: mod.Source,
SourceID: mod.SourceID, SourceID: mod.SourceID,
EventType: mod.EventType, EventType: mod.EventType,
CategoryID: mod.CategoryID, CategoryID: mod.CategoryID,
Name: mod.Name, Name: mod.Name,
NewCoords: expandToCoords(mod.Slots, modified), NewCoords: expandToCoords(mod.Slots, modified),
HostEventID: resolveHostEventID(mod, modified),
}) })
// Move: had slots → different slots // Move: had slots → different slots
case hadSlots && hasSlots && !slotsEqual(orig.Slots, mod.Slots): case hadSlots && hasSlots && !slotsEqual(orig.Slots, mod.Slots):
changes = append(changes, ScheduleChange{ changes = append(changes, ScheduleChange{
Type: ChangeMove, Type: ChangeMove,
StateID: mod.StateID, StateID: mod.StateID,
Source: mod.Source, Source: mod.Source,
SourceID: mod.SourceID, SourceID: mod.SourceID,
EventType: mod.EventType, EventType: mod.EventType,
CategoryID: mod.CategoryID, CategoryID: mod.CategoryID,
Name: mod.Name, Name: mod.Name,
OldCoords: expandToCoords(orig.Slots, original), OldCoords: expandToCoords(orig.Slots, original),
NewCoords: expandToCoords(mod.Slots, modified), NewCoords: expandToCoords(mod.Slots, modified),
HostEventID: resolveHostEventID(mod, modified),
OldHostEventID: resolveHostEventID(orig, original),
}) })
// Unplace: had slots → no slots // Unplace: had slots → no slots
case hadSlots && !hasSlots: case hadSlots && !hasSlots:
changes = append(changes, ScheduleChange{ changes = append(changes, ScheduleChange{
Type: ChangeUnplace, Type: ChangeUnplace,
StateID: mod.StateID, StateID: mod.StateID,
Source: orig.Source, Source: orig.Source,
SourceID: orig.SourceID, SourceID: orig.SourceID,
EventType: orig.EventType, EventType: orig.EventType,
Name: orig.Name, Name: orig.Name,
OldCoords: expandToCoords(orig.Slots, original), OldCoords: expandToCoords(orig.Slots, original),
HostEventID: resolveHostEventID(orig, original),
}) })
} }
} }
@@ -376,6 +417,20 @@ func slotsEqual(a, b []newagenttools.TaskSlot) bool {
return true return true
} }
// resolveHostEventID 从任务的 EmbedHost 字段反查宿主的 ScheduleEvent.ID。
// 用于 DiffScheduleState 在生成 ScheduleChange 时记录嵌入路径的宿主 EventID。
// 若任务非嵌入EmbedHost == nil或宿主不存在返回 0。
func resolveHostEventID(task *newagenttools.ScheduleTask, state *newagenttools.ScheduleState) int {
if task == nil || task.EmbedHost == nil {
return 0
}
host := state.TaskByStateID(*task.EmbedHost)
if host == nil {
return 0
}
return host.SourceID
}
// expandToCoords converts compressed TaskSlots to individual SlotCoords. // expandToCoords converts compressed TaskSlots to individual SlotCoords.
func expandToCoords(slots []newagenttools.TaskSlot, state *newagenttools.ScheduleState) []SlotCoord { func expandToCoords(slots []newagenttools.TaskSlot, state *newagenttools.ScheduleState) []SlotCoord {
var coords []SlotCoord var coords []SlotCoord

View File

@@ -0,0 +1,279 @@
package conv
import (
"testing"
newagenttools "github.com/LoveLosita/smartflow/backend/newAgent/tools"
)
// buildTestState 构造最小可用的 ScheduleStateDayMapping 让 expandToCoords 能正常工作。
func buildTestState(days []newagenttools.DayMapping, tasks []newagenttools.ScheduleTask) *newagenttools.ScheduleState {
return &newagenttools.ScheduleState{
Window: newagenttools.ScheduleWindow{
TotalDays: len(days),
DayMapping: days,
},
Tasks: tasks,
}
}
// defaultDays 返回 3 天的 DayMappingday1=week3/dow1, day2=week3/dow2, day3=week3/dow3
func defaultDays() []newagenttools.DayMapping {
return []newagenttools.DayMapping{
{DayIndex: 1, Week: 3, DayOfWeek: 1},
{DayIndex: 2, Week: 3, DayOfWeek: 2},
{DayIndex: 3, Week: 3, DayOfWeek: 3},
}
}
// ==================== DiffScheduleState: task_item place ====================
// TestDiff_PlaceTaskItem_NonEmbed 验证:普通放置 task_item 时 HostEventID=0。
func TestDiff_PlaceTaskItem_NonEmbed(t *testing.T) {
days := defaultDays()
original := buildTestState(days, []newagenttools.ScheduleTask{
{StateID: 1, Source: "task_item", SourceID: 10, Name: "复习线代", Status: "pending", Duration: 2},
})
modified := buildTestState(days, []newagenttools.ScheduleTask{
{
StateID: 1,
Source: "task_item",
SourceID: 10,
Name: "复习线代",
Status: "existing",
Slots: []newagenttools.TaskSlot{{Day: 1, SlotStart: 1, SlotEnd: 2}},
},
})
changes := DiffScheduleState(original, modified)
if len(changes) != 1 {
t.Fatalf("期望 1 个变更,实际 %d 个", len(changes))
}
c := changes[0]
if c.Type != ChangePlace {
t.Errorf("期望 ChangePlace实际 %s", c.Type)
}
if c.Source != "task_item" || c.SourceID != 10 {
t.Errorf("source 或 sourceID 错误: %s/%d", c.Source, c.SourceID)
}
if c.HostEventID != 0 {
t.Errorf("非嵌入路径 HostEventID 应为 0实际 %d", c.HostEventID)
}
if len(c.NewCoords) != 2 {
t.Errorf("期望 2 个节次坐标,实际 %d", len(c.NewCoords))
}
}
// TestDiff_PlaceTaskItem_Embed 验证:嵌入放置时 HostEventID = 宿主的 SourceID。
func TestDiff_PlaceTaskItem_Embed(t *testing.T) {
days := defaultDays()
// 原始宿主水课已安排guest 待安排
original := buildTestState(days, []newagenttools.ScheduleTask{
{
StateID: 100,
Source: "event",
SourceID: 999, // ScheduleEvent.ID of the host course
Name: "高数",
Status: "existing",
CanEmbed: true,
Slots: []newagenttools.TaskSlot{{Day: 2, SlotStart: 3, SlotEnd: 4}},
},
{StateID: 1, Source: "task_item", SourceID: 10, Name: "复习线代", Status: "pending", Duration: 2},
})
hostID := 100
// 修改后guest 嵌入到宿主
modified := buildTestState(days, []newagenttools.ScheduleTask{
{
StateID: 100,
Source: "event",
SourceID: 999,
Name: "高数",
Status: "existing",
CanEmbed: true,
Slots: []newagenttools.TaskSlot{{Day: 2, SlotStart: 3, SlotEnd: 4}},
EmbeddedBy: &[]int{1}[0],
},
{
StateID: 1,
Source: "task_item",
SourceID: 10,
Name: "复习线代",
Status: "existing",
Slots: []newagenttools.TaskSlot{{Day: 2, SlotStart: 3, SlotEnd: 4}},
EmbedHost: &hostID,
},
})
changes := DiffScheduleState(original, modified)
// 宿主 slots 未变,只有 guest 产生 place 变更
var placeChange *ScheduleChange
for i := range changes {
if changes[i].SourceID == 10 {
placeChange = &changes[i]
}
}
if placeChange == nil {
t.Fatal("未找到 task_item 的 place 变更")
}
if placeChange.HostEventID != 999 {
t.Errorf("嵌入路径 HostEventID 应为 999宿主 SourceID实际 %d", placeChange.HostEventID)
}
}
// ==================== DiffScheduleState: task_item unplace ====================
// TestDiff_UnplaceTaskItem_NonEmbed 验证:从普通位置移除时 HostEventID=0。
func TestDiff_UnplaceTaskItem_NonEmbed(t *testing.T) {
days := defaultDays()
original := buildTestState(days, []newagenttools.ScheduleTask{
{
StateID: 1,
Source: "task_item",
SourceID: 10,
Name: "复习线代",
Status: "existing",
Slots: []newagenttools.TaskSlot{{Day: 1, SlotStart: 5, SlotEnd: 6}},
},
})
modified := buildTestState(days, []newagenttools.ScheduleTask{
{StateID: 1, Source: "task_item", SourceID: 10, Name: "复习线代", Status: "pending"},
})
changes := DiffScheduleState(original, modified)
if len(changes) != 1 {
t.Fatalf("期望 1 个变更,实际 %d", len(changes))
}
c := changes[0]
if c.Type != ChangeUnplace {
t.Errorf("期望 ChangeUnplace实际 %s", c.Type)
}
if c.HostEventID != 0 {
t.Errorf("普通移除 HostEventID 应为 0实际 %d", c.HostEventID)
}
if len(c.OldCoords) != 2 {
t.Errorf("期望 2 个旧坐标,实际 %d", len(c.OldCoords))
}
}
// TestDiff_UnplaceTaskItem_Embed 验证:从嵌入位置移除时 HostEventID = 宿主 SourceID。
func TestDiff_UnplaceTaskItem_Embed(t *testing.T) {
days := defaultDays()
hostStateID := 100
original := buildTestState(days, []newagenttools.ScheduleTask{
{
StateID: 100,
Source: "event",
SourceID: 999,
Name: "高数",
Status: "existing",
CanEmbed: true,
Slots: []newagenttools.TaskSlot{{Day: 2, SlotStart: 3, SlotEnd: 4}},
EmbeddedBy: &[]int{1}[0],
},
{
StateID: 1,
Source: "task_item",
SourceID: 10,
Name: "复习线代",
Status: "existing",
Slots: []newagenttools.TaskSlot{{Day: 2, SlotStart: 3, SlotEnd: 4}},
EmbedHost: &hostStateID,
},
})
modified := buildTestState(days, []newagenttools.ScheduleTask{
{
StateID: 100,
Source: "event",
SourceID: 999,
Name: "高数",
Status: "existing",
CanEmbed: true,
Slots: []newagenttools.TaskSlot{{Day: 2, SlotStart: 3, SlotEnd: 4}},
},
{StateID: 1, Source: "task_item", SourceID: 10, Name: "复习线代", Status: "pending"},
})
changes := DiffScheduleState(original, modified)
var unplaceChange *ScheduleChange
for i := range changes {
if changes[i].SourceID == 10 {
unplaceChange = &changes[i]
}
}
if unplaceChange == nil {
t.Fatal("未找到 task_item 的 unplace 变更")
}
if unplaceChange.HostEventID != 999 {
t.Errorf("嵌入移除 HostEventID 应为 999实际 %d", unplaceChange.HostEventID)
}
}
// ==================== DiffScheduleState: task_item move ====================
// TestDiff_MoveTaskItem 验证task_item 移动时 OldHostEventID 和 HostEventID 分别对应旧/新位置宿主。
func TestDiff_MoveTaskItem_NonEmbedToNonEmbed(t *testing.T) {
days := defaultDays()
original := buildTestState(days, []newagenttools.ScheduleTask{
{
StateID: 1,
Source: "task_item",
SourceID: 10,
Name: "复习线代",
Status: "existing",
Slots: []newagenttools.TaskSlot{{Day: 1, SlotStart: 1, SlotEnd: 2}},
},
})
modified := buildTestState(days, []newagenttools.ScheduleTask{
{
StateID: 1,
Source: "task_item",
SourceID: 10,
Name: "复习线代",
Status: "existing",
Slots: []newagenttools.TaskSlot{{Day: 2, SlotStart: 5, SlotEnd: 6}},
},
})
changes := DiffScheduleState(original, modified)
if len(changes) != 1 {
t.Fatalf("期望 1 个变更,实际 %d", len(changes))
}
c := changes[0]
if c.Type != ChangeMove {
t.Errorf("期望 ChangeMove实际 %s", c.Type)
}
if c.HostEventID != 0 || c.OldHostEventID != 0 {
t.Errorf("非嵌入移动两个 HostEventID 均应为 0实际 %d/%d", c.OldHostEventID, c.HostEventID)
}
if len(c.OldCoords) != 2 || len(c.NewCoords) != 2 {
t.Errorf("旧坐标 %d 个,新坐标 %d 个,均期望 2 个", len(c.OldCoords), len(c.NewCoords))
}
}
// ==================== resolveHostEventID ====================
func TestResolveHostEventID_NoEmbed(t *testing.T) {
task := &newagenttools.ScheduleTask{StateID: 1, EmbedHost: nil}
state := buildTestState(defaultDays(), nil)
if got := resolveHostEventID(task, state); got != 0 {
t.Errorf("无嵌入时应返回 0实际 %d", got)
}
}
func TestResolveHostEventID_WithEmbed(t *testing.T) {
hostID := 100
task := &newagenttools.ScheduleTask{StateID: 1, EmbedHost: &hostID}
state := buildTestState(defaultDays(), []newagenttools.ScheduleTask{
{StateID: 100, Source: "event", SourceID: 999},
})
if got := resolveHostEventID(task, state); got != 999 {
t.Errorf("期望宿主 SourceID=999实际 %d", got)
}
}

View File

@@ -539,7 +539,7 @@ func (d *CacheDAO) agentStateKey(conversationID string) string {
// //
// 职责边界: // 职责边界:
// 1. 只负责 JSON 序列化 + Redis SET不做业务校验 // 1. 只负责 JSON 序列化 + Redis SET不做业务校验
// 2. TTL 默认 24h过期自动清理避免已完成任务的快照堆积 // 2. TTL 默认 2h过期自动清理配合 MySQL outbox 异步持久化
// 3. snapshot 为 nil 时直接返回,避免写入无效数据。 // 3. snapshot 为 nil 时直接返回,避免写入无效数据。
func (d *CacheDAO) SaveAgentState(ctx context.Context, conversationID string, snapshot any) error { func (d *CacheDAO) SaveAgentState(ctx context.Context, conversationID string, snapshot any) error {
if d == nil || d.client == nil { if d == nil || d.client == nil {
@@ -557,7 +557,7 @@ func (d *CacheDAO) SaveAgentState(ctx context.Context, conversationID string, sn
if err != nil { if err != nil {
return fmt.Errorf("marshal agent state failed: %w", err) return fmt.Errorf("marshal agent state failed: %w", err)
} }
return d.client.Set(ctx, d.agentStateKey(normalizedID), data, 24*time.Hour).Err() return d.client.Set(ctx, d.agentStateKey(normalizedID), data, 2*time.Hour).Err()
} }
// LoadAgentState 从 Redis 读取并反序列化 agent 运行态快照。 // LoadAgentState 从 Redis 读取并反序列化 agent 运行态快照。

View File

@@ -22,6 +22,7 @@ func autoMigrateModels(db *gorm.DB) error {
&model.Schedule{}, &model.Schedule{},
&model.AgentOutboxMessage{}, &model.AgentOutboxMessage{},
&model.AgentScheduleState{}, &model.AgentScheduleState{},
&model.AgentStateSnapshotRecord{},
} }
for _, m := range models { for _, m := range models {

View File

@@ -0,0 +1,24 @@
package model
import "time"
// AgentStateSnapshotRecord 是 agent 运行态快照的 MySQL 持久化模型。
//
// 设计说明:
// 1. 通过 outbox 异步写入Redis 快照到期后仍可从此表恢复;
// 2. 按 conversation_id 索引,支持按会话查询最近快照;
// 3. phase 字段便于按阶段过滤和清理;
// 4. 不做历史版本管理(覆盖写),同一会话只保留最新快照。
type AgentStateSnapshotRecord struct {
ID int64 `gorm:"column:id;primaryKey;autoIncrement"`
ConversationID string `gorm:"column:conversation_id;type:varchar(128);not null;uniqueIndex:idx_conversation_snapshot"`
UserID int `gorm:"column:user_id;not null;index:idx_user_snapshot"`
Phase string `gorm:"column:phase;type:varchar(32);not null"`
SnapshotJSON string `gorm:"column:snapshot_json;type:longtext;not null"`
CreatedAt time.Time `gorm:"column:created_at;autoCreateTime"`
UpdatedAt time.Time `gorm:"column:updated_at;autoUpdateTime"`
}
func (AgentStateSnapshotRecord) TableName() string {
return "agent_state_snapshot_records"
}

View File

@@ -12,12 +12,13 @@ import (
const ( const (
GraphName = "agent_loop" GraphName = "agent_loop"
NodeChat = "chat" NodeChat = "chat"
NodePlan = "plan" NodePlan = "plan"
NodeConfirm = "confirm" NodeConfirm = "confirm"
NodeExecute = "execute" NodeRoughBuild = "rough_build"
NodeInterrupt = "interrupt" NodeExecute = "execute"
NodeDeliver = "deliver" NodeInterrupt = "interrupt"
NodeDeliver = "deliver"
) )
func RunAgentGraph(ctx context.Context, input newagentmodel.AgentGraphRunInput) (*newagentmodel.AgentGraphState, error) { 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 { if err := g.AddLambdaNode(NodeConfirm, compose.InvokableLambda(nodes.Confirm)); err != nil {
return nil, err 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 { if err := g.AddLambdaNode(NodeExecute, compose.InvokableLambda(nodes.Execute)); err != nil {
return nil, err return nil, err
} }
@@ -60,16 +64,17 @@ func RunAgentGraph(ctx context.Context, input newagentmodel.AgentGraphRunInput)
if err := g.AddEdge(compose.START, NodeChat); err != nil { if err := g.AddEdge(compose.START, NodeChat); err != nil {
return nil, err 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( if err := g.AddBranch(NodeChat, compose.NewGraphBranch(
branchAfterChat, branchAfterChat,
map[string]bool{ map[string]bool{
NodePlan: true, NodePlan: true,
NodeConfirm: true, NodeConfirm: true,
NodeExecute: true, NodeRoughBuild: true,
NodeDeliver: true, NodeExecute: true,
NodeInterrupt: true, NodeDeliver: true,
compose.END: true, NodeInterrupt: true,
compose.END: true,
}, },
)); err != nil { )); err != nil {
return nil, err return nil, err
@@ -85,17 +90,22 @@ func RunAgentGraph(ctx context.Context, input newagentmodel.AgentGraphRunInput)
)); err != nil { )); err != nil {
return nil, err return nil, err
} }
// Confirm -> Plan(用户拒绝或重规划) / Execute(确认后继续执行) / Interrupt(产出确认中断并等待外部回调) // Confirm -> Plan(用户拒绝或重规划) / RoughBuild(需粗排) / Execute(直接执行) / Interrupt(等待用户确认)
if err := g.AddBranch(NodeConfirm, compose.NewGraphBranch( if err := g.AddBranch(NodeConfirm, compose.NewGraphBranch(
branchAfterConfirm, branchAfterConfirm,
map[string]bool{ map[string]bool{
NodePlan: true, NodePlan: true,
NodeExecute: true, NodeRoughBuild: true,
NodeInterrupt: true, NodeExecute: true,
NodeInterrupt: true,
}, },
)); err != nil { )); err != nil {
return nil, err return nil, err
} }
// RoughBuild -> Execute粗排完成后直接进入执行阶段微调。
if err := g.AddEdge(NodeRoughBuild, NodeExecute); err != nil {
return nil, err
}
// Execute -> Execute(继续 ReAct) / Confirm(写操作待确认) / Deliver(完成) / Interrupt(需要追问用户) // Execute -> Execute(继续 ReAct) / Confirm(写操作待确认) / Deliver(完成) / Interrupt(需要追问用户)
if err := g.AddBranch(NodeExecute, compose.NewGraphBranch( if err := g.AddBranch(NodeExecute, compose.NewGraphBranch(
branchAfterExecute, branchAfterExecute,
@@ -145,16 +155,21 @@ func branchAfterChat(_ context.Context, st *newagentmodel.AgentGraphState) (stri
return compose.END, nil return compose.END, nil
} }
switch flowState.Phase { switch flowState.Phase {
case newagentmodel.PhaseChatting:
// 简单任务直接回复 / 深度回答完成,回复已在 Chat 节点生成。
return compose.END, nil
case newagentmodel.PhasePlanning: case newagentmodel.PhasePlanning:
return NodePlan, nil return NodePlan, nil
case newagentmodel.PhaseWaitingConfirm: case newagentmodel.PhaseWaitingConfirm:
return NodeConfirm, nil return NodeConfirm, nil
case newagentmodel.PhaseExecuting: case newagentmodel.PhaseExecuting:
if flowState.NeedsRoughBuild && st.Deps.RoughBuildFunc != nil {
return NodeRoughBuild, nil
}
return NodeExecute, nil return NodeExecute, nil
case newagentmodel.PhaseDone: case newagentmodel.PhaseDone:
return NodeDeliver, nil return NodeDeliver, nil
default: default:
// 普通聊天场景,回复已在 chatNode 生成,当前请求可直接结束。
return compose.END, nil return compose.END, nil
} }
} }
@@ -191,10 +206,14 @@ func branchAfterConfirm(_ context.Context, st *newagentmodel.AgentGraphState) (s
} }
switch flowState.Phase { switch flowState.Phase {
case newagentmodel.PhaseExecuting: case newagentmodel.PhaseExecuting:
// 若 Plan 节点标记了需要粗排且 RoughBuildFunc 已注入,走粗排节点。
if flowState.NeedsRoughBuild && st.Deps.RoughBuildFunc != nil {
return NodeRoughBuild, nil
}
return NodeExecute, nil return NodeExecute, nil
case newagentmodel.PhaseWaitingConfirm: case newagentmodel.PhaseWaitingConfirm:
// 1. confirm 节点产出确认请求后,当前连接必须进入 interrupt 收口。 // confirm 节点产出确认请求后,当前连接必须进入 interrupt 收口。
// 2. 真正的用户确认结果应由外部回调写回状态,再重新进入 graph。 // 真正的用户确认结果应由外部回调写回状态,再重新进入 graph。
return NodeInterrupt, nil return NodeInterrupt, nil
default: default:
return NodePlan, nil return NodePlan, nil

View 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
}

View File

@@ -1,5 +1,9 @@
package model package model
import (
newagenttools "github.com/LoveLosita/smartflow/backend/newAgent/tools"
)
// Phase 表示 agent 主循环当前所处的大阶段。 // Phase 表示 agent 主循环当前所处的大阶段。
type Phase string type Phase string
@@ -39,6 +43,17 @@ type CommonState struct {
// 连续修正计数LLM 连续输出不合法决策的次数,超过阈值后强制终止避免死循环。 // 连续修正计数LLM 连续输出不合法决策的次数,超过阈值后强制终止避免死循环。
ConsecutiveCorrections int `json:"consecutive_corrections"` 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 { func NewCommonState(traceID string, userID int, conversationID string) *CommonState {

View File

@@ -29,6 +29,20 @@ func (r *AgentGraphRequest) Normalize() {
r.ConfirmAction = strings.TrimSpace(r.ConfirmAction) 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 层运行时真正依赖的可插拔能力。 // AgentGraphDeps 描述 graph/node 层运行时真正依赖的可插拔能力。
// //
// 设计目的: // 设计目的:
@@ -45,6 +59,7 @@ type AgentGraphDeps struct {
ToolRegistry *newagenttools.ToolRegistry ToolRegistry *newagenttools.ToolRegistry
ScheduleProvider ScheduleStateProvider // 按 DAO 注入Execute 节点按需加载 ScheduleState ScheduleProvider ScheduleStateProvider // 按 DAO 注入Execute 节点按需加载 ScheduleState
SchedulePersistor SchedulePersistor // 按 DAO 注入,用于写工具执行后持久化变更 SchedulePersistor SchedulePersistor // 按 DAO 注入,用于写工具执行后持久化变更
RoughBuildFunc RoughBuildFunc // 按 Service 注入,粗排算法入口
} }
// EnsureChunkEmitter 保证 graph 运行时始终有一个可用的 chunk 发射器。 // EnsureChunkEmitter 保证 graph 运行时始终有一个可用的 chunk 发射器。

View File

@@ -44,14 +44,18 @@ const (
// 1. Speak 是本轮先对用户说的话;若 action=ask_user通常这里会承载要追问的问题 // 1. Speak 是本轮先对用户说的话;若 action=ask_user通常这里会承载要追问的问题
// 2. Action 是规划阶段的下一步动作类型; // 2. Action 是规划阶段的下一步动作类型;
// 3. Reason 是给后端和日志看的简短解释; // 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 { type PlanDecision struct {
Speak string `json:"speak,omitempty"` Speak string `json:"speak,omitempty"`
Action PlanAction `json:"action"` Action PlanAction `json:"action"`
Reason string `json:"reason,omitempty"` Reason string `json:"reason,omitempty"`
Complexity PlanComplexity `json:"complexity"` Complexity PlanComplexity `json:"complexity"`
NeedThinking bool `json:"need_thinking"` NeedThinking bool `json:"need_thinking"`
PlanSteps []PlanStep `json:"plan_steps,omitempty"` PlanSteps []PlanStep `json:"plan_steps,omitempty"`
NeedsRoughBuild bool `json:"needs_rough_build,omitempty"`
TaskClassIDs []int `json:"task_class_ids,omitempty"`
} }
// Normalize 统一清洗规划决策中的字符串字段。 // Normalize 统一清洗规划决策中的字符串字段。

View File

@@ -57,6 +57,8 @@ type AgentStateStore interface {
// 使用接口而非具体 DAO 类型,避免 model → dao 的循环依赖。 // 使用接口而非具体 DAO 类型,避免 model → dao 的循环依赖。
type ScheduleStateProvider interface { type ScheduleStateProvider interface {
LoadScheduleState(ctx context.Context, userID int) (*newagenttools.ScheduleState, error) LoadScheduleState(ctx context.Context, userID int) (*newagenttools.ScheduleState, error)
// LoadTaskClassMetas 只加载指定任务类的约束元数据,供 Plan 节点提前消费。
LoadTaskClassMetas(ctx context.Context, userID int, taskClassIDs []int) ([]newagenttools.TaskClassMeta, error)
} }
// SchedulePersistor 定义持久化 ScheduleState 变更的接口。 // SchedulePersistor 定义持久化 ScheduleState 变更的接口。

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") 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( if err := RunChatNode(
ctx, ctx,
ChatNodeInput{ ChatNodeInput{
@@ -105,6 +119,25 @@ func (n *AgentNodes) Plan(ctx context.Context, st *newagentmodel.AgentGraphState
return st, nil 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 是中断阶段的正式节点方法。 // Interrupt 是中断阶段的正式节点方法。
// //
// 职责边界: // 职责边界:
@@ -196,7 +229,7 @@ func (n *AgentNodes) Execute(ctx context.Context, st *newagentmodel.AgentGraphSt
// 1. 这里只做 graph -> node 的参数转接; // 1. 这里只做 graph -> node 的参数转接;
// 2. 真正的交付逻辑仍由 RunDeliverNode 负责; // 2. 真正的交付逻辑仍由 RunDeliverNode 负责;
// 3. 调 LLM 生成任务总结,失败时降级到机械格式化。 // 3. 调 LLM 生成任务总结,失败时降级到机械格式化。
// 4. 任务完成后删除 Redis 快照,清理持久化状态 // 4. 任务完成后保存最终状态到 Redis2h TTL支持断线恢复和 MySQL outbox 异步持久化。
func (n *AgentNodes) Deliver(ctx context.Context, st *newagentmodel.AgentGraphState) (*newagentmodel.AgentGraphState, error) { func (n *AgentNodes) Deliver(ctx context.Context, st *newagentmodel.AgentGraphState) (*newagentmodel.AgentGraphState, error) {
if st == nil { if st == nil {
return nil, errors.New("deliver node: state is 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 return nil, err
} }
deleteAgentState(ctx, st) saveAgentState(ctx, st)
return st, nil return st, nil
} }

View File

@@ -3,6 +3,7 @@ package newagentnode
import ( import (
"context" "context"
"fmt" "fmt"
"log"
"strings" "strings"
"time" "time"
@@ -36,89 +37,222 @@ type ChatNodeInput struct {
ChunkEmitter *newagentstream.ChunkEmitter 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 执行一轮聊天节点逻辑。 // RunChatNode 执行一轮聊天节点逻辑。
// //
// 核心职责: // 核心职责:
// 1. 恢复判定:有 pending interaction 则处理恢复,不生成 speak // 1. 恢复判定:有 pending interaction 则处理恢复;
// 2. 意图分流:无 pending 时,调 LLM 分类 chat / task // 2. 路由分流:无 pending 时,调 LLM 判断复杂度并路由
// 3. 闲聊回复:纯 chat 场景直接生成回复并流式推送phase → chatting → END // 3. direct_reply简单任务直接输出回复 → END
// 4. 任务路由task 场景 phase → planning交给后续 Plan 节点处理。 // 4. execute中等任务推 Execute ReAct
// // 5. deep_answer复杂问答原地开 thinking 深度回答 → END
// 保守原则:分类失败或意图不明时,一律走 task不丢失用户意图 // 6. plan复杂规划推 Plan 节点
func RunChatNode(ctx context.Context, input ChatNodeInput) error { func RunChatNode(ctx context.Context, input ChatNodeInput) error {
runtimeState, conversationContext, emitter, err := prepareChatNodeInput(input) runtimeState, conversationContext, emitter, err := prepareChatNodeInput(input)
if err != nil { if err != nil {
return err return err
} }
// 1. 有 pending interaction → 纯状态传递,不生成 speak // 1. 有 pending interaction → 纯状态传递,处理恢复
if runtimeState.HasPendingInteraction() { if runtimeState.HasPendingInteraction() {
return handleChatResume(input, runtimeState, conversationContext, emitter) return handleChatResume(input, runtimeState, conversationContext, emitter)
} }
// 2. 无 pending → 调 LLM 做意图分类 // 2. 无 pending → 路由决策(一次快速 LLM 调用,不开 thinking
messages := newagentprompt.BuildChatIntentMessages(conversationContext, input.UserInput) flowState := runtimeState.EnsureCommonState()
decision, _, err := newagentllm.GenerateJSON[chatIntentDecision]( messages := newagentprompt.BuildChatRoutingMessages(conversationContext, input.UserInput, flowState)
decision, rawResult, err := newagentllm.GenerateJSON[newagentmodel.ChatRoutingDecision](
ctx, ctx,
input.Client, input.Client,
messages, messages,
newagentllm.GenerateOptions{ newagentllm.GenerateOptions{
Temperature: 0.1, Temperature: 0.1,
MaxTokens: 300, MaxTokens: 500,
Thinking: newagentllm.ThinkingModeDisabled, Thinking: newagentllm.ThinkingModeDisabled,
Metadata: map[string]any{
"stage": chatStageName,
"phase": "routing",
},
}, },
) )
if err != nil || decision.Validate() != nil {
// 分类失败 → 保守:走 task。 rawText := ""
runtimeState.EnsureCommonState().Phase = newagentmodel.PhasePlanning 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 return nil
} }
// 3. 按意图分流。 if validateErr := decision.Validate(); validateErr != nil {
flowState := runtimeState.EnsureCommonState() log.Printf("[WARN] chat routing decision invalid chat=%s raw=%s err=%v",
switch decision.Intent { flowState.ConversationID, rawText, validateErr)
case "task":
flowState.Phase = newagentmodel.PhasePlanning flowState.Phase = newagentmodel.PhasePlanning
return nil 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: default:
flowState.Phase = newagentmodel.PhasePlanning flowState.Phase = newagentmodel.PhasePlanning
return nil 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 恢复。 // handleChatResume 处理 pending interaction 恢复。
// //
// 职责边界: // 职责边界:
@@ -216,31 +350,6 @@ func handleConfirmResume(
return nil 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 校验并准备聊天节点的运行态依赖。 // prepareChatNodeInput 校验并准备聊天节点的运行态依赖。
func prepareChatNodeInput(input ChatNodeInput) ( func prepareChatNodeInput(input ChatNodeInput) (
*newagentmodel.AgentRuntimeState, *newagentmodel.AgentRuntimeState,

View File

@@ -22,6 +22,11 @@ const (
executeStatusBlockID = "execute.status" executeStatusBlockID = "execute.status"
executeSpeakBlockID = "execute.speak" executeSpeakBlockID = "execute.speak"
executePinnedKey = "execution_context" executePinnedKey = "execution_context"
// maxConsecutiveCorrections 是 Execute 节点连续修正次数上限。
// 超过此阈值后终止执行,防止 LLM 陷入无限修正循环。
// 适用场景JSON 解析失败、决策不合法、goal_check 为空、工具名不存在。
maxConsecutiveCorrections = 3
) )
// ExecuteNodeInput 描述执行节点单轮运行所需的最小依赖。 // 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) return executePendingTool(ctx, runtimeState, conversationContext, input.ToolRegistry, input.ScheduleState, input.SchedulePersistor, input.OriginalScheduleState, emitter)
} }
// 2. 检查是否有可执行的 plan 步骤 // 2. 推送执行阶段状态,让前端知道当前进度
if !flowState.HasCurrentPlanStep() { if flowState.HasCurrentPlanStep() {
return fmt.Errorf("execute node: 当前无有效 plan 步骤,无法执行") // 有 plan显示步骤进度。
} current, total := flowState.PlanProgress()
currentStep, _ := flowState.CurrentPlanStep()
// 3. 推送执行阶段状态,让前端知道当前进度。 if err := emitter.EmitStatus(
current, total := flowState.PlanProgress() executeStatusBlockID,
currentStep, _ := flowState.CurrentPlanStep() executeStageName,
if err := emitter.EmitStatus( "executing",
executeStatusBlockID, fmt.Sprintf("正在执行第 %d/%d 步:%s", current, total, truncateText(currentStep.Content, 60)),
executeStageName, false,
"executing", ); err != nil {
fmt.Sprintf("正在执行第 %d/%d 步:%s", current, total, truncateText(currentStep.Content, 60)), return fmt.Errorf("执行阶段状态推送失败: %w", err)
false, }
); err != nil { } else {
return fmt.Errorf("执行阶段状态推送失败: %w", err) // 无 plan纯 ReAct 模式。
if err := emitter.EmitStatus(
executeStatusBlockID,
executeStageName,
"executing",
"正在处理你的请求...",
false,
); err != nil {
return fmt.Errorf("执行阶段状态推送失败: %w", err)
}
} }
// 4. 消耗一轮预算,并检查是否耗尽。 // 4. 消耗一轮预算,并检查是否耗尽。
@@ -129,7 +143,7 @@ func RunExecuteNode(ctx context.Context, input ExecuteNodeInput) error {
newagentllm.GenerateOptions{ newagentllm.GenerateOptions{
Temperature: 0.3, Temperature: 0.3,
MaxTokens: 1200, MaxTokens: 1200,
Thinking: newagentllm.ThinkingModeEnabled, Thinking: newagentllm.ThinkingModeDisabled,
Metadata: map[string]any{ Metadata: map[string]any{
"stage": executeStageName, "stage": executeStageName,
"step_index": flowState.CurrentStep, "step_index": flowState.CurrentStep,
@@ -137,8 +151,6 @@ func RunExecuteNode(ctx context.Context, input ExecuteNodeInput) error {
}, },
}, },
) )
const maxConsecutiveCorrections = 3
// 提前捕获原始文本,用于日志和 correction。 // 提前捕获原始文本,用于日志和 correction。
rawText := "" rawText := ""
if rawResult != nil { if rawResult != nil {
@@ -162,6 +174,25 @@ func RunExecuteNode(ctx context.Context, input ExecuteNodeInput) error {
) )
return nil 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) return fmt.Errorf("执行阶段模型调用失败: %w", err)
} }
@@ -210,8 +241,10 @@ func RunExecuteNode(ctx context.Context, input ExecuteNodeInput) error {
} }
} }
// 6. 若 LLM 先对用户说话,则伪流式推送并写回历史 // 6. 若 LLM 先对用户说话,且不是 ask_user / confirm二者交给下游节点收口则伪流式推送。
if strings.TrimSpace(decision.Speak) != "" { if strings.TrimSpace(decision.Speak) != "" &&
decision.Action != newagentmodel.ExecuteActionAskUser &&
decision.Action != newagentmodel.ExecuteActionConfirm {
if err := emitter.EmitPseudoAssistantText( if err := emitter.EmitPseudoAssistantText(
ctx, ctx,
executeSpeakBlockID, executeSpeakBlockID,
@@ -399,12 +432,34 @@ func executeToolCall(
return fmt.Errorf("日程状态未加载,无法执行工具") return fmt.Errorf("日程状态未加载,无法执行工具")
} }
if !registry.HasTool(toolName) { 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. 执行工具。 // 2. 执行工具。
result := registry.Execute(scheduleState, toolName, toolCall.Arguments) 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 消息对追加到对话历史。 // 3. 将工具调用和结果以合法的 assistant+tool 消息对追加到对话历史。
// //
// 修复说明: // 修复说明:

View File

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

View File

@@ -70,29 +70,46 @@ func renderStateSummary(state *newagentmodel.CommonState) string {
if !state.HasPlan() { if !state.HasPlan() {
sb.WriteString("当前完整 plan暂无。\n") 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 { } 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() return sb.String()

View File

@@ -1,63 +1,122 @@
package newagentprompt package newagentprompt
import ( import (
"fmt"
"strings" "strings"
newagentmodel "github.com/LoveLosita/smartflow/backend/newAgent/model" newagentmodel "github.com/LoveLosita/smartflow/backend/newAgent/model"
"github.com/cloudwego/eino/schema" "github.com/cloudwego/eino/schema"
) )
const chatIntentSystemPrompt = ` const chatRoutingSystemPrompt = `
你是 SmartFlow 的意图分类器 你是 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 返回意图分类系统提示词。 // BuildChatRoutingSystemPrompt 返回路由阶段的系统提示词。
func BuildChatIntentSystemPrompt() string { func BuildChatRoutingSystemPrompt() string {
return strings.TrimSpace(chatIntentSystemPrompt) return strings.TrimSpace(chatRoutingSystemPrompt)
} }
// BuildChatIntentMessages 组装意图分类的 messages。 // BuildChatRoutingMessages 组装路由阶段的 messages。
// func BuildChatRoutingMessages(ctx *newagentmodel.ConversationContext, userInput string, state *newagentmodel.CommonState) []*schema.Message {
// 职责边界: return buildStageMessages(
// 1. 只取最近 6 条历史,保证分类高效; BuildChatRoutingSystemPrompt(),
// 2. 不注入 pinned blocks / tool schemas分类不需要这些信息 ctx,
// 3. 不负责解析模型输出。 BuildChatRoutingUserPrompt(ctx, userInput, state),
func BuildChatIntentMessages(conversationContext *newagentmodel.ConversationContext, userInput string) []*schema.Message { )
messages := make([]*schema.Message, 0, 8) }
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 { sb.WriteString("请判断用户本轮意图的复杂度,并选择最合适的路由。\n")
history := conversationContext.HistorySnapshot()
if len(history) > 6 { // 注入任务类上下文(供粗排判断参考)。
history = history[len(history)-6:] 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 { sb.WriteString(fmt.Sprintf("\n本次请求涉及的任务类 ID[%s]\n", strings.Join(parts, ", ")))
messages = append(messages, history...) }
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) trimmedInput := strings.TrimSpace(userInput)
if trimmedInput != "" { if trimmedInput != "" {
alreadyLast := len(messages) > 0 && sb.WriteString("\n用户本轮输入\n")
messages[len(messages)-1].Role == schema.User && sb.WriteString(trimmedInput)
messages[len(messages)-1].Content == trimmedInput sb.WriteString("\n")
if !alreadyLast {
messages = append(messages, schema.UserMessage(trimmedInput))
}
} }
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,
)
} }

View File

@@ -8,9 +8,9 @@ import (
"github.com/cloudwego/eino/schema" "github.com/cloudwego/eino/schema"
) )
const executeSystemPrompt = ` const executeSystemPromptWithPlan = `
你是 SmartFlow NewAgent 的执行器。 你是 SmartFlow NewAgent 的执行器。
你的职责是在当前 plan 步骤的约束下,进行思考、执行、观察,再决定下一步动作。 你的职责是在"当前 plan 步骤"的约束下,进行思考、执行、观察,再决定下一步动作。
请遵守以下规则: 请遵守以下规则:
1. 只围绕当前步骤行动,不要擅自跳到其他 plan 步骤。 1. 只围绕当前步骤行动,不要擅自跳到其他 plan 步骤。
@@ -19,7 +19,7 @@ const executeSystemPrompt = `
4. 只有当你确认整个任务已经完成时,才输出 action=done且必须在 goal_check 中总结整体完成证据。 4. 只有当你确认整个任务已经完成时,才输出 action=done且必须在 goal_check 中总结整体完成证据。
5. 如果执行当前步骤缺少关键上下文,且无法通过已有历史或工具补齐,输出 action=ask_user。 5. 如果执行当前步骤缺少关键上下文,且无法通过已有历史或工具补齐,输出 action=ask_user。
6. 不要伪造工具结果;如果尚未真正拿到观察结果,就不要假装已经完成。 6. 不要伪造工具结果;如果尚未真正拿到观察结果,就不要假装已经完成。
7. goal_check 是你输出 next_plan / done 时的强制字段,禁止为空;必须显式地逐条对照 done_when说明哪些条件已满足、依据是什么 7. goal_check 是你输出 next_plan / done 时的强制字段,禁止为空;必须显式地逐条对照 done_when说明"哪些条件已满足、依据是什么"
你会看到: 你会看到:
- 当前完整 plan - 当前完整 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 返回执行阶段系统提示词。 // BuildExecuteSystemPrompt 返回执行阶段系统提示词。
func BuildExecuteSystemPrompt() string { 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 { func BuildExecuteDecisionContractText() string {
return strings.TrimSpace(fmt.Sprintf(` return strings.TrimSpace(fmt.Sprintf(`
输出协议(严格 JSON 输出协议(严格 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。 // BuildExecuteMessages 组装执行阶段的 messages。
func BuildExecuteMessages(state *newagentmodel.CommonState, ctx *newagentmodel.ConversationContext) []*schema.Message { 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( return buildStageMessages(
BuildExecuteSystemPrompt(), BuildExecuteReActSystemPrompt(),
ctx, ctx,
BuildExecuteUserPrompt(state), BuildExecuteReActUserPrompt(state),
) )
} }
// BuildExecuteUserPrompt 构造执行阶段的用户提示词。 // BuildExecuteUserPrompt 构造有 plan 模式的用户提示词。
func BuildExecuteUserPrompt(state *newagentmodel.CommonState) string { func BuildExecuteUserPrompt(state *newagentmodel.CommonState) string {
var sb strings.Builder var sb strings.Builder
@@ -132,3 +220,24 @@ func BuildExecuteUserPrompt(state *newagentmodel.CommonState) string {
return strings.TrimSpace(sb.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())
}

View File

@@ -2,6 +2,7 @@ package newagentprompt
import ( import (
"fmt" "fmt"
"strconv"
"strings" "strings"
newagentmodel "github.com/LoveLosita/smartflow/backend/newAgent/model" newagentmodel "github.com/LoveLosita/smartflow/backend/newAgent/model"
@@ -21,6 +22,14 @@ const planSystemPrompt = `
6. 只输出 JSON不要输出 markdown不要输出额外解释不要在 JSON 外再补文字。 6. 只输出 JSON不要输出 markdown不要输出额外解释不要在 JSON 外再补文字。
7. 每次输出前先评估任务复杂度simple简单明确无复杂依赖、moderate多步操作需要一定推理、complex需要深度推理、多方案比较或复杂依赖关系 7. 每次输出前先评估任务复杂度simple简单明确无复杂依赖、moderate多步操作需要一定推理、complex需要深度推理、多方案比较或复杂依赖关系
8. 根据复杂度判断 need_thinking你是否需要深度思考才能生成高质量计划当不确定时倾向于 false。 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(BuildPlanDecisionContractText())
sb.WriteString("\n") 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) trimmedInput := strings.TrimSpace(userInput)
if trimmedInput != "" { if trimmedInput != "" {
sb.WriteString("\n用户本轮输入\n") sb.WriteString("\n用户本轮输入\n")
@@ -84,39 +102,41 @@ func BuildPlanDecisionContractText() string {
- need_thinking是否需要深度思考才能生成高质量计划只能是 true / false - need_thinking是否需要深度思考才能生成高质量计划只能是 true / false
- plan_steps仅当 action=%s 时允许返回;返回时必须是完整计划,不是增量 - plan_steps仅当 action=%s 时允许返回;返回时必须是完整计划,不是增量
- plan_steps[].content步骤正文必填 - plan_steps[].content步骤正文必填
- plan_steps[].done_when可选建议写什么情况下算这一步做完 - plan_steps[].done_when可选建议写"什么情况下算这一步做完"
- needs_rough_build仅当满足粗排识别规则时为 true否则省略为 true 时后端自动运行粗排算法
- task_class_idsneeds_rough_build=true 时必填,从上下文"任务类 ID"字段读取
合法示例: 合法示例:
{ {
speak: 我先把计划再收束一下。, "speak": "我先把计划再收束一下。",
action: %s, "action": "%s",
reason: 当前信息已足够继续规划, "reason": "当前信息已足够继续规划",
complexity: moderate, "complexity": "moderate",
need_thinking: false "need_thinking": false
} }
{ {
speak: 你更希望我优先安排今天,还是按整周来规划?, "speak": "你更希望我优先安排今天,还是按整周来规划?",
action: %s, "action": "%s",
reason: 当前时间范围仍不明确, "reason": "当前时间范围仍不明确",
complexity: simple, "complexity": "simple",
need_thinking: false "need_thinking": false
} }
{ {
speak: 计划已经整理好了,我先给你确认一下。, "speak": "计划已经整理好了,我先给你确认一下。",
action: %s, "action": "%s",
reason: 当前计划已具备执行条件, "reason": "当前计划已具备执行条件",
complexity: simple, "complexity": "simple",
need_thinking: false, "need_thinking": false,
plan_steps: [ "plan_steps": [
{ {
content: 先确认本周可用时间范围, "content": "先确认本周可用时间范围",
done_when: 拿到明确的可用时间段列表 "done_when": "拿到明确的可用时间段列表"
}, },
{ {
content: 基于可用时间生成执行安排, "content": "基于可用时间生成执行安排",
done_when: 得到一份用户可确认的安排方案 "done_when": "得到一份用户可确认的安排方案"
} }
] ]
} }

View File

@@ -1,8 +1,6 @@
package newagentstream package newagentstream
import ( import "log"
"fmt"
)
// NewSSEPayloadEmitter 创建将 chunk 事件写入 outChan 的 emitter。 // NewSSEPayloadEmitter 创建将 chunk 事件写入 outChan 的 emitter。
// //
@@ -10,7 +8,7 @@ import (
// 1. 接收 outChanSSE 输出通道),返回 PayloadEmitter 函数; // 1. 接收 outChanSSE 输出通道),返回 PayloadEmitter 函数;
// 2. 只把原始 JSON payload 写入通道,不添加 "data: " 前缀和 "\n\n" 后缀; // 2. 只把原始 JSON payload 写入通道,不添加 "data: " 前缀和 "\n\n" 后缀;
// 3. SSE 格式化("data: " + payload + "\n\n")由 API 层的 writeSSEData 统一处理; // 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 { if outChan == nil {
return nil return nil
} }
if payload == "" { if payload == "" {
return nil return nil
} }
select { select {
case outChan <- payload: case outChan <- payload:
return nil return nil
default: default:
// 通道已满或已关闭:不阻塞,直接返回错误 // 通道已满:客户端可能已断开或消费过慢
return fmt.Errorf("outChan full or closed") // 静默丢弃此 chunk让图继续执行并完成状态持久化。
// 客户端重连后可从 Redis 快照恢复,不需要这条消息。
log.Printf("[WARN] SSE outChan full, dropping payload (len=%d)", len(payload))
return nil
} }
} }
} }

View File

@@ -83,9 +83,45 @@ func GetOverview(state *ScheduleState) string {
sb.WriteString(strings.Join(pendingParts, " ") + "\n") 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() 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 查看某天(或某天某段)的细粒度占用详情。 // QueryRange 查看某天(或某天某段)的细粒度占用详情。
// day 必填slotStart/slotEnd 选填nil 表示查整天)。 // day 必填slotStart/slotEnd 选填nil 表示查整天)。
// 整天模式按标准段1-2, 3-4, ..., 11-12分组输出。 // 整天模式按标准段1-2, 3-4, ..., 11-12分组输出。

View File

@@ -20,6 +20,19 @@ type TaskSlot struct {
SlotEnd int `json:"slot_end"` 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. // ScheduleTask is a unified task representation in the tool state.
// It merges existing schedules (from schedule_events) and pending tasks (from task_items) // It merges existing schedules (from schedule_events) and pending tasks (from task_items)
// into one flat list that the tool layer operates on. // into one flat list that the tool layer operates on.
@@ -36,7 +49,9 @@ type ScheduleTask struct {
Slots []TaskSlot `json:"slots,omitempty"` Slots []TaskSlot `json:"slots,omitempty"`
// Pending task: required consecutive slot count. // Pending task: required consecutive slot count.
Duration int `json:"duration,omitempty"` 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"` CategoryID int `json:"category_id,omitempty"`
// source=event only: whether this slot allows embedding other tasks. // source=event only: whether this slot allows embedding other tasks.
CanEmbed bool `json:"can_embed,omitempty"` CanEmbed bool `json:"can_embed,omitempty"`
@@ -51,8 +66,9 @@ type ScheduleTask struct {
// ScheduleState is the full tool operation state. // ScheduleState is the full tool operation state.
type ScheduleState struct { type ScheduleState struct {
Window ScheduleWindow `json:"window"` Window ScheduleWindow `json:"window"`
Tasks []ScheduleTask `json:"tasks"` Tasks []ScheduleTask `json:"tasks"`
TaskClasses []TaskClassMeta `json:"task_classes,omitempty"` // 任务类约束元数据,供 LLM 排课参考
} }
// DayToWeekDay converts day_index to (week, day_of_week). // DayToWeekDay converts day_index to (week, day_of_week).
@@ -95,9 +111,11 @@ func (s *ScheduleState) Clone() *ScheduleState {
TotalDays: s.Window.TotalDays, TotalDays: s.Window.TotalDays,
DayMapping: make([]DayMapping, len(s.Window.DayMapping)), 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.Window.DayMapping, s.Window.DayMapping)
copy(clone.TaskClasses, s.TaskClasses)
for i, t := range s.Tasks { for i, t := range s.Tasks {
clone.Tasks[i] = t clone.Tasks[i] = t
if t.Slots != nil { if t.Slots != nil {

View File

@@ -289,7 +289,30 @@ func readAgentExtraInt(extra map[string]any, key string) int {
return value return value
} }
// parseAgentLooseInt 负责把 extra 中的“弱类型数字”归一成 int。 // readAgentExtraIntSlice 从 extra 中提取 []int。
// 支持 JSON 数组格式([]any每个元素为 float64/int
func readAgentExtraIntSlice(extra map[string]any, key string) []int {
if len(extra) == 0 {
return nil
}
raw, ok := extra[key]
if !ok || raw == nil {
return nil
}
arr, ok := raw.([]any)
if !ok {
return nil
}
result := make([]int, 0, len(arr))
for _, item := range arr {
if v, ok := parseAgentLooseInt(item); ok && v > 0 {
result = append(result, v)
}
}
return result
}
// parseAgentLooseInt 负责把 extra 中的”弱类型数字”归一成 int。
// //
// 职责边界: // 职责边界:
// 1. 负责兼容前端 JSON 解码后的常见数值类型,以及字符串形式的数字。 // 1. 负责兼容前端 JSON 解码后的常见数值类型,以及字符串形式的数字。
@@ -530,7 +553,7 @@ func (s *AgentService) AgentChat(ctx context.Context, userMessage string, ifThin
requestStart := time.Now() requestStart := time.Now()
traceID := uuid.NewString() traceID := uuid.NewString()
outChan := make(chan string, 8) outChan := make(chan string, 256)
errChan := make(chan error, 1) errChan := make(chan error, 1)
go func() { go func() {
@@ -547,7 +570,7 @@ func (s *AgentService) agentChatOld(ctx context.Context, userMessage string, ifT
requestStart := time.Now() requestStart := time.Now()
traceID := uuid.NewString() traceID := uuid.NewString()
outChan := make(chan string, 8) outChan := make(chan string, 256)
errChan := make(chan error, 1) errChan := make(chan error, 1)
// 0. 初始化”请求级 token 统计器”,用于聚合本次请求所有模型开销。 // 0. 初始化”请求级 token 统计器”,用于聚合本次请求所有模型开销。

View File

@@ -18,6 +18,7 @@ import (
"github.com/LoveLosita/smartflow/backend/conv" "github.com/LoveLosita/smartflow/backend/conv"
"github.com/LoveLosita/smartflow/backend/model" "github.com/LoveLosita/smartflow/backend/model"
"github.com/LoveLosita/smartflow/backend/pkg" "github.com/LoveLosita/smartflow/backend/pkg"
eventsvc "github.com/LoveLosita/smartflow/backend/service/events"
) )
// runNewAgentGraph 运行 newAgent 通用 graph直接替换旧 agent 路由逻辑。 // runNewAgentGraph 运行 newAgent 通用 graph直接替换旧 agent 路由逻辑。
@@ -100,6 +101,21 @@ func (s *AgentService) runNewAgentGraph(
conversationContext = s.loadConversationContext(requestCtx, chatID, userMessage) conversationContext = s.loadConversationContext(requestCtx, chatID, userMessage)
} }
// 5.5 若 extra 携带 task_class_ids写入 CommonState仅首轮/尚未设置时生效,跨轮持久化)。
if taskClassIDs := readAgentExtraIntSlice(extra, "task_class_ids"); len(taskClassIDs) > 0 {
cs := runtimeState.EnsureCommonState()
if len(cs.TaskClassIDs) == 0 {
cs.TaskClassIDs = taskClassIDs
if s.scheduleProvider != nil {
if metas, metaErr := s.scheduleProvider.LoadTaskClassMetas(requestCtx, userID, taskClassIDs); metaErr != nil {
log.Printf("加载任务类约束元数据失败 chat=%s err=%v", chatID, metaErr)
} else {
cs.TaskClasses = metas
}
}
}
}
// 6. 构造 AgentGraphRequest。 // 6. 构造 AgentGraphRequest。
var confirmAction string var confirmAction string
if len(extra) > 0 { if len(extra) > 0 {
@@ -132,6 +148,7 @@ func (s *AgentService) runNewAgentGraph(
ToolRegistry: s.toolRegistry, ToolRegistry: s.toolRegistry,
ScheduleProvider: s.scheduleProvider, ScheduleProvider: s.scheduleProvider,
SchedulePersistor: s.schedulePersistor, SchedulePersistor: s.schedulePersistor,
RoughBuildFunc: s.makeRoughBuildFunc(),
} }
// 10. 构造 AgentGraphRunInput 并运行 graph。 // 10. 构造 AgentGraphRunInput 并运行 graph。
@@ -154,6 +171,33 @@ func (s *AgentService) runNewAgentGraph(
// 11. 持久化聊天历史(用户消息 + 助手回复)。 // 11. 持久化聊天历史(用户消息 + 助手回复)。
s.persistChatAfterGraph(requestCtx, userID, chatID, userMessage, finalState, retryMeta, requestStart, outChan, errChan) s.persistChatAfterGraph(requestCtx, userID, chatID, userMessage, finalState, retryMeta, requestStart, outChan, errChan)
// 11.5. 将最终状态快照异步写入 MySQL通过 outbox
// Deliver 节点已将快照保存到 Redis2h TTL此处通过 outbox 异步写入 MySQL 做永久存储。
if finalState != nil {
snapshot := &newagentmodel.AgentStateSnapshot{
RuntimeState: finalState.EnsureRuntimeState(),
ConversationContext: finalState.EnsureConversationContext(),
}
eventsvc.PublishAgentStateSnapshot(requestCtx, s.eventPublisher, snapshot, chatID, userID)
}
// 11.6. 将排程结果写入 Redis 预览缓存,复用旧 agent 的 SchedulePlanPreviewCache 格式。
// 前端通过 GET /agent/schedule-preview 获取,无需改动。
if finalState != nil && finalState.ScheduleState != nil {
flowState := finalState.EnsureFlowState()
preview := conv.ScheduleStateToPreview(
finalState.ScheduleState,
userID,
chatID,
flowState.TaskClassIDs,
"", // summary 由转换函数自动生成
)
if preview != nil && s.cacheDAO != nil {
if err := s.cacheDAO.SetSchedulePlanPreviewToCache(requestCtx, userID, chatID, preview); err != nil {
log.Printf("[WARN] 写入排程预览缓存失败 chat=%s: %v", chatID, err)
}
}
}
// 12. 发送 OpenAI 兼容的流式结束标记,告知客户端 stream 已完成。 // 12. 发送 OpenAI 兼容的流式结束标记,告知客户端 stream 已完成。
_ = chunkEmitter.EmitDone() _ = chunkEmitter.EmitDone()
@@ -203,6 +247,10 @@ func (s *AgentService) loadOrCreateRuntimeState(ctx context.Context, chatID stri
cs := snapshot.RuntimeState.EnsureCommonState() cs := snapshot.RuntimeState.EnsureCommonState()
cs.UserID = userID cs.UserID = userID
cs.ConversationID = chatID cs.ConversationID = chatID
// 不需要手动重置 Phase所有请求统一先过 Chat 节点Chat 会根据路由决策覆盖 Phase。
// 保留完整的 RuntimeStatePlanSteps、CurrentStep 等),支持连续对话调整日程。
return snapshot.RuntimeState, snapshot.ConversationContext return snapshot.RuntimeState, snapshot.ConversationContext
} }
return newRT() return newRT()
@@ -376,6 +424,35 @@ func (s *AgentService) persistChatAfterGraph(
} }
} }
// makeRoughBuildFunc 把 AgentService 上的 HybridScheduleWithPlanMultiFunc 封装成
// newAgent 层的 RoughBuildFunc完成外层 model.TaskClassItem → RoughBuildPlacement 的转换。
// HybridScheduleWithPlanMultiFunc 未注入时返回 nilRoughBuild 节点会静默跳过粗排。
func (s *AgentService) makeRoughBuildFunc() newagentmodel.RoughBuildFunc {
if s.HybridScheduleWithPlanMultiFunc == nil {
return nil
}
return func(ctx context.Context, userID int, taskClassIDs []int) ([]newagentmodel.RoughBuildPlacement, error) {
_, items, err := s.HybridScheduleWithPlanMultiFunc(ctx, userID, taskClassIDs)
if err != nil {
return nil, err
}
placements := make([]newagentmodel.RoughBuildPlacement, 0, len(items))
for _, item := range items {
if item.EmbeddedTime == nil {
continue
}
placements = append(placements, newagentmodel.RoughBuildPlacement{
TaskItemID: item.ID,
Week: item.EmbeddedTime.Week,
DayOfWeek: item.EmbeddedTime.DayOfWeek,
SectionFrom: item.EmbeddedTime.SectionFrom,
SectionTo: item.EmbeddedTime.SectionTo,
})
}
return placements, nil
}
}
// --- 依赖注入字段 --- // --- 依赖注入字段 ---
// toolRegistry 由 cmd/start.go 注入 // toolRegistry 由 cmd/start.go 注入

View File

@@ -0,0 +1,126 @@
package events
import (
"context"
"encoding/json"
"errors"
"log"
"github.com/LoveLosita/smartflow/backend/dao"
kafkabus "github.com/LoveLosita/smartflow/backend/infra/kafka"
outboxinfra "github.com/LoveLosita/smartflow/backend/infra/outbox"
"github.com/LoveLosita/smartflow/backend/model"
newagentmodel "github.com/LoveLosita/smartflow/backend/newAgent/model"
"gorm.io/gorm"
"gorm.io/gorm/clause"
)
const (
// EventTypeAgentStateSnapshotPersist 是"agent 状态快照持久化"的业务事件类型。
EventTypeAgentStateSnapshotPersist = "agent.state.snapshot.persist"
)
// AgentStateSnapshotPayload 是 outbox 事件的业务载荷。
type AgentStateSnapshotPayload struct {
ConversationID string `json:"conversation_id"`
UserID int `json:"user_id"`
Phase string `json:"phase"`
SnapshotJSON string `json:"snapshot_json"`
}
// RegisterAgentStateSnapshotHandler 注册"agent 状态快照持久化"消费者处理器。
//
// 职责边界:
// 1. 只负责快照写入 agent_state_snapshot_records 表;
// 2. 使用 upsert 语义,同一 conversation_id 只保留最新快照;
// 3. 通过 outbox 通用消费事务保证"业务写入 + consumed 推进"原子一致。
func RegisterAgentStateSnapshotHandler(
bus *outboxinfra.EventBus,
outboxRepo *outboxinfra.Repository,
repoManager *dao.RepoManager,
) error {
if bus == nil {
return errors.New("event bus is nil")
}
if outboxRepo == nil {
return errors.New("outbox repository is nil")
}
if repoManager == nil {
return errors.New("repo manager is nil")
}
handler := func(ctx context.Context, envelope kafkabus.Envelope) error {
var payload AgentStateSnapshotPayload
if unmarshalErr := json.Unmarshal(envelope.Payload, &payload); unmarshalErr != nil {
_ = outboxRepo.MarkDead(ctx, envelope.OutboxID, "解析快照载荷失败: "+unmarshalErr.Error())
return nil
}
return outboxRepo.ConsumeAndMarkConsumed(ctx, envelope.OutboxID, func(tx *gorm.DB) error {
record := model.AgentStateSnapshotRecord{
ConversationID: payload.ConversationID,
UserID: payload.UserID,
Phase: payload.Phase,
SnapshotJSON: payload.SnapshotJSON,
}
return tx.Clauses(clause.OnConflict{
Columns: []clause.Column{{Name: "conversation_id"}},
DoUpdates: clause.AssignmentColumns([]string{"user_id", "phase", "snapshot_json", "updated_at"}),
}).Create(&record).Error
})
}
return bus.RegisterEventHandler(EventTypeAgentStateSnapshotPersist, handler)
}
// PublishAgentStateSnapshot 发布"agent 状态快照持久化"事件到 outbox。
//
// 设计说明:
// 1. 将快照 JSON 序列化后通过 outbox 异步写入 MySQL
// 2. publisher 为 nil 时静默降级Kafka 未启用场景);
// 3. 发布失败只记日志,不中断主流程。
func PublishAgentStateSnapshot(
ctx context.Context,
publisher outboxinfra.EventPublisher,
snapshot *newagentmodel.AgentStateSnapshot,
conversationID string,
userID int,
) {
if publisher == nil {
return
}
if snapshot == nil {
return
}
snapshotJSON, err := json.Marshal(snapshot)
if err != nil {
log.Printf("[WARN] 序列化 agent 状态快照失败 chat=%s: %v", conversationID, err)
return
}
phase := ""
if snapshot.RuntimeState != nil {
cs := snapshot.RuntimeState.EnsureCommonState()
if cs != nil {
phase = string(cs.Phase)
}
}
payload := AgentStateSnapshotPayload{
ConversationID: conversationID,
UserID: userID,
Phase: phase,
SnapshotJSON: string(snapshotJSON),
}
if err := publisher.Publish(ctx, outboxinfra.PublishRequest{
EventType: EventTypeAgentStateSnapshotPersist,
EventVersion: outboxinfra.DefaultEventVersion,
MessageKey: conversationID,
AggregateID: conversationID,
Payload: payload,
}); err != nil {
log.Printf("[WARN] 发布 agent 状态快照事件失败 chat=%s: %v", conversationID, err)
}
}