Version: 0.9.31.dev.260419
后端: 1. 日程暂存接口——前端拖拽调整后保存到 Redis 快照 - api/agent.go:新增 SaveScheduleState handler,解析绝对时间格式请求体,3 秒超时保护 - routers/routers.go:注册 POST /schedule-state - model/agent.go:新增 SaveScheduleStatePlacedItem / SaveScheduleStateRequest 结构体 - respond/respond.go:新增 5 个排程状态错误码(40058~40062) - 新增 service/agentsvc/agent_schedule_state.go:Load 快照 → ApplyPlacedItems → Save 回 Redis,校验归属 - 新增 newAgent/conv/schedule_state_apply.go:ApplyPlacedItems 绝对坐标→相对 day_index 转换,去重/坐标/嵌入关系校验 2. SchedulePersistor 持久化层全面下线 - 删除 newAgent/conv/schedule_persist.go(280 行,DiffScheduleState → applyChange → 事务写库整条链路) - model/state_store.go:移除 SchedulePersistor 接口 - model/graph_run_state.go / node/execute.go / node/agent_nodes.go / service/agent.go / service/agent_newagent.go / cmd/start.go:移除 SchedulePersistor 字段、参数、注入六处 3. schedule_completed 事件推送——deliver 节点排程完毕信号 - model/common_state.go:新增 HasScheduleChanges 标记,ResetForNextRun 清理 - node/execute.go / node/rough_build.go:写工具和粗排成功后置 HasScheduleChanges=true - node/deliver.go:IsCompleted && HasScheduleChanges 时调用 EmitScheduleCompleted - stream/emitter.go:新增 EmitScheduleCompleted 方法 - stream/openai.go:新增 StreamExtraKindScheduleCompleted + NewScheduleCompletedExtra 4. 预览接口补全 task_class_id - model/agent.go:GetSchedulePlanPreviewResponse 新增 TaskClassIDs - model/schedule.go:HybridScheduleEntry 新增 TaskClassID - conv/schedule_preview.go / service/agent_schedule_preview.go / service/schedule.go:三处透传填充 前端: 5. 排程完毕卡片 + 精排弹窗集成 - 新增 api/schedule_agent.ts:getSchedulePreview / saveScheduleState / applyBatchIntoSchedule - types/dashboard.ts:新增 HybridScheduleEntry / SchedulePreviewData / PlacedItem 类型 - components/dashboard/AssistantPanel.vue:监听 schedule_completed 事件异步拉取排程渲染卡片,集成 ScheduleResultCard + ScheduleFineTuneModal;confirm 交互从文本消息改为 resume 协议(approve/reject/cancel) 6. ToolTracePrototypeView 原型页新增日程小卡片 + 拖拽编排弹窗演示 7. DashboardView import 区域尺寸微调
This commit is contained in:
@@ -1,280 +0,0 @@
|
||||
package newagentconv
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
baseconv "github.com/LoveLosita/smartflow/backend/conv"
|
||||
"github.com/LoveLosita/smartflow/backend/dao"
|
||||
"github.com/LoveLosita/smartflow/backend/model"
|
||||
schedule "github.com/LoveLosita/smartflow/backend/newAgent/tools/schedule"
|
||||
)
|
||||
|
||||
// SchedulePersistorAdapter 实现 model.SchedulePersistor 接口。
|
||||
// 组合 RepoManager,调用 PersistScheduleChanges 持久化变更。
|
||||
type SchedulePersistorAdapter struct {
|
||||
manager *dao.RepoManager
|
||||
}
|
||||
|
||||
// NewSchedulePersistorAdapter 创建持久化适配器。
|
||||
func NewSchedulePersistorAdapter(manager *dao.RepoManager) *SchedulePersistorAdapter {
|
||||
return &SchedulePersistorAdapter{manager: manager}
|
||||
}
|
||||
|
||||
// PersistScheduleChanges 实现 model.SchedulePersistor 接口。
|
||||
func (a *SchedulePersistorAdapter) PersistScheduleChanges(ctx context.Context, original, modified *schedule.ScheduleState, userID int) error {
|
||||
return PersistScheduleChanges(ctx, a.manager, original, modified, userID)
|
||||
}
|
||||
|
||||
// PersistScheduleChanges 将内存中的 ScheduleState 变更持久化到数据库。
|
||||
//
|
||||
// 职责边界:
|
||||
// 1. 调用 DiffScheduleState 计算变更;
|
||||
// 2. 在事务中逐个应用变更到数据库;
|
||||
// 3. 全部成功或全部回滚,保证原子性。
|
||||
func PersistScheduleChanges(
|
||||
ctx context.Context,
|
||||
manager *dao.RepoManager,
|
||||
original *schedule.ScheduleState,
|
||||
modified *schedule.ScheduleState,
|
||||
userID int,
|
||||
) error {
|
||||
changes := DiffScheduleState(original, modified)
|
||||
if len(changes) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
return manager.Transaction(ctx, func(txM *dao.RepoManager) error {
|
||||
for _, change := range changes {
|
||||
if err := applyScheduleChange(ctx, txM, change, userID); err != nil {
|
||||
return fmt.Errorf("应用变更失败 [%s %s]: %w", change.Type, change.Name, err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
// applyScheduleChange 应用单个变更到数据库。
|
||||
func applyScheduleChange(ctx context.Context, manager *dao.RepoManager, change ScheduleChange, userID int) error {
|
||||
switch change.Type {
|
||||
case ChangePlace:
|
||||
return applyPlaceChange(ctx, manager, change, userID)
|
||||
case ChangeMove:
|
||||
return applyMoveChange(ctx, manager, change, userID)
|
||||
case ChangeUnplace:
|
||||
return applyUnplaceChange(ctx, manager, change, userID)
|
||||
default:
|
||||
return fmt.Errorf("未知变更类型: %s", change.Type)
|
||||
}
|
||||
}
|
||||
|
||||
// applyPlaceChange 应用放置变更。
|
||||
func applyPlaceChange(ctx context.Context, manager *dao.RepoManager, change ScheduleChange, userID int) error {
|
||||
if len(change.NewCoords) == 0 {
|
||||
return fmt.Errorf("place 变更缺少目标位置")
|
||||
}
|
||||
switch change.Source {
|
||||
case "event":
|
||||
return applyPlaceEventSource(ctx, manager, change, userID)
|
||||
case "task_item":
|
||||
return applyPlaceTaskItem(ctx, manager, change, userID)
|
||||
default:
|
||||
return fmt.Errorf("place 变更不支持的 source: %s", change.Source)
|
||||
}
|
||||
}
|
||||
|
||||
// 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)
|
||||
for week, dayGroups := range groups {
|
||||
for dayOfWeek, coords := range dayGroups {
|
||||
startSection, endSection := minMaxSection(coords)
|
||||
schedules := make([]model.Schedule, endSection-startSection+1)
|
||||
for sec := startSection; sec <= endSection; sec++ {
|
||||
schedules[sec-startSection] = model.Schedule{
|
||||
UserID: userID,
|
||||
Week: week,
|
||||
DayOfWeek: dayOfWeek,
|
||||
Section: sec,
|
||||
EventID: change.SourceID,
|
||||
}
|
||||
}
|
||||
if _, err := manager.Schedule.AddSchedules(schedules); err != nil {
|
||||
return fmt.Errorf("创建 schedule 失败: %w", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// applyPlaceTaskItem 处理 source=task_item 的放置。
|
||||
//
|
||||
// 两条路径:
|
||||
// 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")
|
||||
}
|
||||
|
||||
// task_item 只占一段连续时段,取第一个 coord 的 week/dayOfWeek
|
||||
first := change.NewCoords[0]
|
||||
week, dayOfWeek := first.Week, first.DayOfWeek
|
||||
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 := baseconv.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)
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
// applyUnplaceChange 应用移除变更。
|
||||
func applyUnplaceChange(ctx context.Context, manager *dao.RepoManager, change ScheduleChange, userID int) error {
|
||||
switch change.Source {
|
||||
case "event":
|
||||
if change.SourceID == 0 {
|
||||
return fmt.Errorf("unplace event 变更需要有效的 source_id")
|
||||
}
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
// ==================== 辅助函数 ====================
|
||||
|
||||
// intPtr 返回 int 指针,零值返回 nil。
|
||||
func intPtr(v int) *int {
|
||||
if v == 0 {
|
||||
return nil
|
||||
}
|
||||
return &v
|
||||
}
|
||||
|
||||
// groupCoordsByWeekDay 按周天分组坐标。
|
||||
func groupCoordsByWeekDay(coords []SlotCoord) map[int]map[int][]SlotCoord {
|
||||
result := make(map[int]map[int][]SlotCoord)
|
||||
for _, coord := range coords {
|
||||
if result[coord.Week] == nil {
|
||||
result[coord.Week] = make(map[int][]SlotCoord)
|
||||
}
|
||||
result[coord.Week][coord.DayOfWeek] = append(result[coord.Week][coord.DayOfWeek], coord)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// minMaxSection 返回坐标列表中的最小和最大节次。
|
||||
func minMaxSection(coords []SlotCoord) (min, max int) {
|
||||
if len(coords) == 0 {
|
||||
return 0, 0
|
||||
}
|
||||
min, max = coords[0].Section, coords[0].Section
|
||||
for _, c := range coords[1:] {
|
||||
if c.Section < min {
|
||||
min = c.Section
|
||||
}
|
||||
if c.Section > max {
|
||||
max = c.Section
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
@@ -71,6 +71,7 @@ func ScheduleStateToPreview(
|
||||
entry.EventID = t.SourceID
|
||||
} else {
|
||||
entry.TaskItemID = t.SourceID
|
||||
entry.TaskClassID = t.TaskClassID
|
||||
}
|
||||
|
||||
// 嵌入与阻塞语义。
|
||||
|
||||
90
backend/newAgent/conv/schedule_state_apply.go
Normal file
90
backend/newAgent/conv/schedule_state_apply.go
Normal file
@@ -0,0 +1,90 @@
|
||||
package newagentconv
|
||||
|
||||
import (
|
||||
"github.com/LoveLosita/smartflow/backend/model"
|
||||
schedule "github.com/LoveLosita/smartflow/backend/newAgent/tools/schedule"
|
||||
"github.com/LoveLosita/smartflow/backend/respond"
|
||||
)
|
||||
|
||||
// ApplyPlacedItems 将前端提交的绝对时间放置项应用到 ScheduleState。
|
||||
//
|
||||
// 职责边界:
|
||||
// 1. 只修改 source=task_item 的任务,source=event 的课程不受影响;
|
||||
// 2. 不在请求中的任务保持原样(slots/status/embed 不变);
|
||||
// 3. 不校验 Slots 的业务合法性(冲突等由 execute 节点兜底);
|
||||
// 4. 返回 respond.XXX 错误,调用方可直接透传给 DealWithError。
|
||||
func ApplyPlacedItems(
|
||||
state *schedule.ScheduleState,
|
||||
items []model.SaveScheduleStatePlacedItem,
|
||||
) error {
|
||||
// 1. 构建索引。
|
||||
sourceIDToTask := make(map[int]*schedule.ScheduleTask, len(state.Tasks))
|
||||
eventSourceIDToTask := make(map[int]*schedule.ScheduleTask)
|
||||
for i := range state.Tasks {
|
||||
t := &state.Tasks[i]
|
||||
if t.Source == "task_item" {
|
||||
sourceIDToTask[t.SourceID] = t
|
||||
} else if t.Source == "event" {
|
||||
eventSourceIDToTask[t.SourceID] = t
|
||||
}
|
||||
}
|
||||
|
||||
// 2. 去重检查。
|
||||
seen := make(map[int]struct{}, len(items))
|
||||
for _, item := range items {
|
||||
if _, dup := seen[item.TaskItemID]; dup {
|
||||
return respond.ScheduleStateDuplicateTaskItem
|
||||
}
|
||||
seen[item.TaskItemID] = struct{}{}
|
||||
}
|
||||
|
||||
// 3. 逐个处理 item。
|
||||
for _, item := range items {
|
||||
// 3.1 绝对坐标 → 相对 day_index。
|
||||
dayIndex, ok := state.WeekDayToDay(item.Week, item.DayOfWeek)
|
||||
if !ok {
|
||||
return respond.ScheduleStateInvalidCoordinates
|
||||
}
|
||||
|
||||
// 3.2 在快照中查找对应的 task_item。
|
||||
task, found := sourceIDToTask[item.TaskItemID]
|
||||
if !found {
|
||||
return respond.ScheduleStateTaskItemNotFound
|
||||
}
|
||||
|
||||
// 3.3 清除旧嵌入关系。
|
||||
if task.EmbedHost != nil {
|
||||
oldHost := state.TaskByStateID(*task.EmbedHost)
|
||||
if oldHost != nil {
|
||||
oldHost.EmbeddedBy = nil
|
||||
}
|
||||
task.EmbedHost = nil
|
||||
}
|
||||
|
||||
// 3.4 设置新嵌入关系。
|
||||
if item.EmbedCourseEventID != 0 {
|
||||
hostEvent := eventSourceIDToTask[item.EmbedCourseEventID]
|
||||
if hostEvent == nil {
|
||||
return respond.ScheduleStateEventNotFound
|
||||
}
|
||||
hostStateID := hostEvent.StateID
|
||||
guestStateID := task.StateID
|
||||
task.EmbedHost = &hostStateID
|
||||
hostEvent.EmbeddedBy = &guestStateID
|
||||
}
|
||||
|
||||
// 3.5 更新 Slots。
|
||||
task.Slots = []schedule.TaskSlot{{
|
||||
Day: dayIndex,
|
||||
SlotStart: item.StartSection,
|
||||
SlotEnd: item.EndSection,
|
||||
}}
|
||||
|
||||
// 3.6 pending → suggested。
|
||||
if task.Status == schedule.TaskStatusPending {
|
||||
task.Status = schedule.TaskStatusSuggested
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user