后端:
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 个
175 lines
4.4 KiB
Go
175 lines
4.4 KiB
Go
package sv
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"sort"
|
|
"strings"
|
|
"time"
|
|
|
|
agentmodel "github.com/LoveLosita/smartflow/backend/services/agent/model"
|
|
"github.com/LoveLosita/smartflow/backend/services/runtime/model"
|
|
"github.com/LoveLosita/smartflow/backend/shared/respond"
|
|
)
|
|
|
|
func (s *AgentService) QueryTasksForTool(ctx context.Context, req agentmodel.TaskQueryRequest) ([]agentmodel.TaskQueryTaskRecord, error) {
|
|
if req.UserID <= 0 {
|
|
return nil, errors.New("invalid user_id in task query")
|
|
}
|
|
|
|
var tasks []model.Task
|
|
var err error
|
|
|
|
// 优先使用统一提升链路(含缓存读取 + 读时派生 + outbox 异步落库)。
|
|
if s.GetTasksWithUrgencyPromotionFunc != nil {
|
|
tasks, err = s.GetTasksWithUrgencyPromotionFunc(ctx, req.UserID)
|
|
if err != nil {
|
|
if errors.Is(err, respond.UserTasksEmpty) {
|
|
return make([]agentmodel.TaskQueryTaskRecord, 0), nil
|
|
}
|
|
return nil, err
|
|
}
|
|
} else {
|
|
// 回退:未注入时走旧的 taskRepo 直接读取(无缓存、无持久化)。
|
|
if s.taskRepo == nil {
|
|
return nil, errors.New("task repository is nil")
|
|
}
|
|
tasks, err = s.taskRepo.GetTasksByUserID(req.UserID)
|
|
if err != nil {
|
|
if errors.Is(err, respond.UserTasksEmpty) {
|
|
return make([]agentmodel.TaskQueryTaskRecord, 0), nil
|
|
}
|
|
return nil, err
|
|
}
|
|
now := time.Now()
|
|
for i := range tasks {
|
|
applyReadTimeUrgencyPromotion(&tasks[i], now)
|
|
}
|
|
}
|
|
|
|
// 过滤、排序、截断。
|
|
filtered := make([]model.Task, 0, len(tasks))
|
|
for _, task := range tasks {
|
|
if !taskMatchesQueryFilter(task, req) {
|
|
continue
|
|
}
|
|
filtered = append(filtered, task)
|
|
}
|
|
|
|
sortTasksForQuery(filtered, req)
|
|
if req.Limit > 0 && len(filtered) > req.Limit {
|
|
filtered = filtered[:req.Limit]
|
|
}
|
|
|
|
records := make([]agentmodel.TaskQueryTaskRecord, 0, len(filtered))
|
|
for _, task := range filtered {
|
|
records = append(records, agentmodel.TaskQueryTaskRecord{
|
|
ID: task.ID,
|
|
Title: task.Title,
|
|
PriorityGroup: task.Priority,
|
|
EstimatedSections: model.NormalizeEstimatedSections(&task.EstimatedSections),
|
|
IsCompleted: task.IsCompleted,
|
|
DeadlineAt: task.DeadlineAt,
|
|
UrgencyThresholdAt: task.UrgencyThresholdAt,
|
|
})
|
|
}
|
|
return records, nil
|
|
}
|
|
|
|
func applyReadTimeUrgencyPromotion(task *model.Task, now time.Time) {
|
|
if task == nil || task.IsCompleted || task.UrgencyThresholdAt == nil {
|
|
return
|
|
}
|
|
if task.UrgencyThresholdAt.After(now) {
|
|
return
|
|
}
|
|
switch task.Priority {
|
|
case 2:
|
|
task.Priority = 1
|
|
case 4:
|
|
task.Priority = 3
|
|
}
|
|
}
|
|
|
|
func taskMatchesQueryFilter(task model.Task, req agentmodel.TaskQueryRequest) bool {
|
|
if !req.IncludeCompleted && task.IsCompleted {
|
|
return false
|
|
}
|
|
if req.Quadrant != nil && task.Priority != *req.Quadrant {
|
|
return false
|
|
}
|
|
keyword := strings.TrimSpace(req.Keyword)
|
|
if keyword != "" && !strings.Contains(strings.ToLower(task.Title), strings.ToLower(keyword)) {
|
|
return false
|
|
}
|
|
if req.DeadlineAfter != nil {
|
|
if task.DeadlineAt == nil || task.DeadlineAt.Before(*req.DeadlineAfter) {
|
|
return false
|
|
}
|
|
}
|
|
if req.DeadlineBefore != nil {
|
|
if task.DeadlineAt == nil || task.DeadlineAt.After(*req.DeadlineBefore) {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
|
|
func sortTasksForQuery(tasks []model.Task, req agentmodel.TaskQueryRequest) {
|
|
if len(tasks) <= 1 {
|
|
return
|
|
}
|
|
order := strings.ToLower(strings.TrimSpace(req.Order))
|
|
if order != "desc" {
|
|
order = "asc"
|
|
}
|
|
sortBy := strings.ToLower(strings.TrimSpace(req.SortBy))
|
|
if sortBy == "" {
|
|
sortBy = "deadline"
|
|
}
|
|
|
|
sort.SliceStable(tasks, func(i, j int) bool {
|
|
left := tasks[i]
|
|
right := tasks[j]
|
|
switch sortBy {
|
|
case "priority":
|
|
if left.Priority != right.Priority {
|
|
if order == "desc" {
|
|
return left.Priority > right.Priority
|
|
}
|
|
return left.Priority < right.Priority
|
|
}
|
|
return left.ID > right.ID
|
|
case "id":
|
|
if order == "desc" {
|
|
return left.ID > right.ID
|
|
}
|
|
return left.ID < right.ID
|
|
default:
|
|
if less, decided := compareDeadline(left.DeadlineAt, right.DeadlineAt, order); decided {
|
|
return less
|
|
}
|
|
return left.ID > right.ID
|
|
}
|
|
})
|
|
}
|
|
|
|
func compareDeadline(left, right *time.Time, order string) (less bool, decided bool) {
|
|
if left == nil && right == nil {
|
|
return false, false
|
|
}
|
|
if left == nil && right != nil {
|
|
return false, true
|
|
}
|
|
if left != nil && right == nil {
|
|
return true, true
|
|
}
|
|
if left.Equal(*right) {
|
|
return false, false
|
|
}
|
|
if order == "desc" {
|
|
return left.After(*right), true
|
|
}
|
|
return left.Before(*right), true
|
|
}
|