后端:
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 个
130 lines
3.6 KiB
Go
130 lines
3.6 KiB
Go
package agentconv
|
||
|
||
import (
|
||
"fmt"
|
||
"time"
|
||
|
||
schedule "github.com/LoveLosita/smartflow/backend/services/agent/tools/schedule"
|
||
"github.com/LoveLosita/smartflow/backend/services/runtime/model"
|
||
)
|
||
|
||
// ScheduleStateToPreview 将 agent 的 ScheduleState 转换为前端预览缓存格式。
|
||
//
|
||
// 职责边界:
|
||
// 1. 只做数据格式转换,不做业务逻辑;
|
||
// 2. 将每个 ScheduleTask 的每个 TaskSlot 转为一条 HybridScheduleEntry;
|
||
// 3. Day → (Week, DayOfWeek) 通过 ScheduleState.DayToWeekDay 转换;
|
||
// 4. 转换失败的 slot(day_index 无效)静默跳过。
|
||
func ScheduleStateToPreview(
|
||
state *schedule.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 schedule.IsPendingTask(*t) {
|
||
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 不变,suggested / 兼容建议态统一输出为 suggested。
|
||
if shouldMarkSuggestedInPreview(*t) {
|
||
entry.Status = "suggested"
|
||
} else {
|
||
entry.Status = "existing"
|
||
}
|
||
|
||
// ID 映射。
|
||
if t.Source == "event" {
|
||
entry.EventID = t.SourceID
|
||
} else {
|
||
entry.TaskItemID = t.SourceID
|
||
entry.TaskClassID = t.TaskClassID
|
||
// 嵌入任务:将宿主课程的 source_id(即 event_id)桥接到 EventID,
|
||
// 供前端作为 embed_course_event_id 传递给 BatchApplyPlans 做冲突豁免。
|
||
if t.EmbedHost != nil {
|
||
if host := state.TaskByStateID(*t.EmbedHost); host != nil {
|
||
entry.EventID = host.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,
|
||
GeneratedAt: time.Now(),
|
||
}
|
||
}
|
||
|
||
// shouldMarkSuggestedInPreview 判断某条 ScheduleTask 在预览层是否应标记为 suggested。
|
||
//
|
||
// 规则说明:
|
||
// 1. 新语义下,显式 suggested 直接输出为建议态;
|
||
// 2. 兼容旧快照:pending+Slots、existing+Duration>0 的 task_item 也继续按 suggested 输出;
|
||
// 3. 这样前端预览口径可以在迁移期保持稳定,不会因为状态枚举切换而抖动。
|
||
func shouldMarkSuggestedInPreview(t schedule.ScheduleTask) bool {
|
||
return schedule.IsSuggestedTask(t)
|
||
}
|