Files
smartmate/backend/services/agent/conv/schedule_state_apply.go
Losita 3b6fca44a6 Version: 0.9.77.dev.260505
后端:
1.阶段 6 CP4/CP5 目录收口与共享边界纯化
- 将 backend 根目录收口为 services、client、gateway、cmd、shared 五个一级目录
- 收拢 bootstrap、inits、infra/kafka、infra/outbox、conv、respond、pkg、middleware,移除根目录旧实现与空目录
- 将 utils 下沉到 services/userauth/internal/auth,将 logic 下沉到 services/schedule/core/planning
- 将迁移期 runtime 桥接实现统一收拢到 services/runtime/{conv,dao,eventsvc,model},删除 shared/legacy 与未再被 import 的旧 service 实现
- 将 gateway/shared/respond 收口为 HTTP/Gin 错误写回适配,shared/respond 仅保留共享错误语义与状态映射
- 将 HTTP IdempotencyMiddleware 与 RateLimitMiddleware 收口到 gateway/middleware
- 将 GormCachePlugin 下沉到 shared/infra/gormcache,将共享 RateLimiter 下沉到 shared/infra/ratelimit,将 agent token budget 下沉到 services/agent/shared
- 删除 InitEino 兼容壳,收缩 cmd/internal/coreinit 仅保留旧组合壳残留域初始化语义
- 更新微服务迁移计划与桌面 checklist,补齐 CP4/CP5 当前切流点、目录终态与验证结果
- 完成 go test ./...、git diff --check 与最终真实 smoke;health、register/login、task/create+get、schedule/today、task-class/list、memory/items、agent chat/meta/timeline/context-stats 全部 200,SSE 合并结果为 CP5_OK 且 [DONE] 只有 1 个
2026-05-05 23:25:07 +08:00

91 lines
2.6 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package agentconv
import (
schedule "github.com/LoveLosita/smartflow/backend/services/agent/tools/schedule"
"github.com/LoveLosita/smartflow/backend/services/runtime/model"
"github.com/LoveLosita/smartflow/backend/shared/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
}