Version: 0.7.1.dev.260321
feat(agent): ✨ 重构智能排程分流与双通道交付,补齐周级预算并接入连续微调复用 - 🔀 通用路由升级为 action 分流(chat/quick_note_create/task_query/schedule_plan),路由失败直接返回内部错误,不再回落聊天 - 🧭 智能排程链路重构:统一图编排与节点职责,完善日级/周级调优协作与提示词约束 - 📊 周级预算改为“有效周保底 + 负载加权分配”,避免有效周零预算并提升资源利用率 - ⚙️ 日级并发优化细化:按天拆分 DayGroup 并发执行,低收益天(suggested<=2)跳过,单天失败仅回退该天结果并继续全局 - 🧵 周级并发优化细化:按周并发 worker 执行,单周“单步动作”循环(每轮仅 1 个 Move/Swap 或 done),失败周保留原方案不影响其它周 - 🛰️ 新增排程预览双通道:聊天主链路输出终审文本,结构化 candidate_plans 通过 /api/v1/agent/schedule-preview 拉取 - 🗃️ 增补 Redis 预览缓存读写与清理逻辑,新增对应 API、路由、模型与错误码支持 - ♻️ 接入连续对话微调复用:命中同会话历史预览时复用上轮 HybridEntries,避免每轮重跑粗排 - 🛡️ 增加复用保护:仅当本轮与上轮 task_class_ids 集合一致才复用;不一致回退全量粗排 - 🧰 扩展预览缓存字段(task_class_ids/hybrid_entries/allocated_items),支撑微调承接链路 - 🗺️ 更新 README 5.4 Mermaid(总分流图 + 智能排程流转图)并补充决策文档 - ⚠️ 新增“连续微调复用”链路我尚未完成测试,且文档状态目前较为混乱,待连续对话微调功能真正测试完成后再统一更新
This commit is contained in:
@@ -7,6 +7,7 @@ import (
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/LoveLosita/smartflow/backend/model"
|
||||
"github.com/cloudwego/eino/schema"
|
||||
"github.com/go-redis/redis/v8"
|
||||
)
|
||||
@@ -40,6 +41,10 @@ func (m *AgentCache) historyWindowKey(sessionID string) string {
|
||||
return fmt.Sprintf("smartflow:history_window:%s", sessionID)
|
||||
}
|
||||
|
||||
func (m *AgentCache) schedulePreviewKey(userID int, sessionID string) string {
|
||||
return fmt.Sprintf("smartflow:schedule_preview:u:%d:c:%s", userID, sessionID)
|
||||
}
|
||||
|
||||
func (m *AgentCache) normalizeWindowSize(size int) int {
|
||||
if size < minHistoryWindowSize {
|
||||
return minHistoryWindowSize
|
||||
@@ -94,17 +99,15 @@ func (m *AgentCache) PushMessage(ctx context.Context, sessionID string, msg *sch
|
||||
return err
|
||||
}
|
||||
|
||||
// 1. 序列化 Eino 消息
|
||||
// 1. 序列化 Eino 消息。
|
||||
data, err := json.Marshal(msg)
|
||||
if err != nil {
|
||||
return fmt.Errorf("marshal message failed: %w", err)
|
||||
}
|
||||
|
||||
// 2. 利用 Pipeline 保证原子操作
|
||||
// 2. 使用 Pipeline 保证“写入+裁剪+续期”原子执行。
|
||||
pipe := m.client.Pipeline()
|
||||
// 往左侧推入最新消息(LIFO)
|
||||
pipe.LPush(ctx, key, data)
|
||||
// 只保留最新 size 条
|
||||
pipe.LTrim(ctx, key, 0, int64(size-1))
|
||||
pipe.Expire(ctx, key, m.expiration)
|
||||
|
||||
@@ -129,10 +132,9 @@ func (m *AgentCache) GetHistory(ctx context.Context, sessionID string) ([]*schem
|
||||
if err := json.Unmarshal([]byte(val), &msg); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// LRANGE 返回 [最新..最旧],这里反转成 [最旧..最新]
|
||||
// LRANGE 返回 [最新...最旧],这里反转成 [最旧...最新]
|
||||
messages[len(vals)-1-i] = &msg
|
||||
}
|
||||
|
||||
return messages, nil
|
||||
}
|
||||
|
||||
@@ -159,11 +161,9 @@ func (m *AgentCache) BackfillHistory(ctx context.Context, sessionID string, mess
|
||||
|
||||
pipe := m.client.Pipeline()
|
||||
pipe.Del(ctx, key)
|
||||
// 输入是 [最旧..最新],LPUSH 后变成 [最新..最旧]
|
||||
pipe.LPush(ctx, key, values...)
|
||||
pipe.LTrim(ctx, key, 0, int64(size-1))
|
||||
pipe.Expire(ctx, key, m.expiration)
|
||||
|
||||
_, err = pipe.Exec(ctx)
|
||||
return err
|
||||
}
|
||||
@@ -185,7 +185,7 @@ func (m *AgentCache) GetConversationStatus(ctx context.Context, sessionID string
|
||||
|
||||
func (m *AgentCache) SetConversationStatus(ctx context.Context, sessionID string) error {
|
||||
key := fmt.Sprintf("smartflow:conversation_status:%s", sessionID)
|
||||
// 仅用于“存在性”标记:只有不存在时才写入,避免重复写
|
||||
// 仅用于“存在性”标记:只有不存在时才写入,避免重复写。
|
||||
return m.client.SetNX(ctx, key, 1, m.expiration).Err()
|
||||
}
|
||||
|
||||
@@ -193,3 +193,55 @@ func (m *AgentCache) DeleteConversationStatus(ctx context.Context, sessionID str
|
||||
key := fmt.Sprintf("smartflow:conversation_status:%s", sessionID)
|
||||
return m.client.Del(ctx, key).Err()
|
||||
}
|
||||
|
||||
// SetSchedulePlanPreview 写入“排程预览”缓存。
|
||||
//
|
||||
// 步骤化说明:
|
||||
// 1. 先把结构化预览序列化成 JSON,避免缓存层结构漂移。
|
||||
// 2. 再按 user_id + conversation_id 写入,确保用户间数据隔离。
|
||||
// 3. 最后带 TTL 写入,保证预览是短期临时态而非长期状态。
|
||||
//
|
||||
// 失败处理:
|
||||
// 1. preview 为空时直接返回错误,避免写入无意义空值。
|
||||
// 2. 序列化失败或 Redis 写入失败都返回 error,由上层决定是否降级。
|
||||
func (m *AgentCache) SetSchedulePlanPreview(ctx context.Context, userID int, sessionID string, preview *model.SchedulePlanPreviewCache) error {
|
||||
if preview == nil {
|
||||
return fmt.Errorf("schedule preview is nil")
|
||||
}
|
||||
data, err := json.Marshal(preview)
|
||||
if err != nil {
|
||||
return fmt.Errorf("marshal schedule preview failed: %w", err)
|
||||
}
|
||||
return m.client.Set(ctx, m.schedulePreviewKey(userID, sessionID), data, m.expiration).Err()
|
||||
}
|
||||
|
||||
// GetSchedulePlanPreview 读取“排程预览”缓存。
|
||||
//
|
||||
// 语义约定:
|
||||
// 1. 未命中返回 (nil, nil),上层可区分“未生成”与“已过期”。
|
||||
// 2. 反序列化失败返回 error,避免把脏缓存当成正常结果。
|
||||
// 3. 不做 DB 回源,预览缓存失效后由业务侧重新生成。
|
||||
func (m *AgentCache) GetSchedulePlanPreview(ctx context.Context, userID int, sessionID string) (*model.SchedulePlanPreviewCache, error) {
|
||||
raw, err := m.client.Get(ctx, m.schedulePreviewKey(userID, sessionID)).Result()
|
||||
if err == redis.Nil {
|
||||
return nil, nil
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var preview model.SchedulePlanPreviewCache
|
||||
if err = json.Unmarshal([]byte(raw), &preview); err != nil {
|
||||
return nil, fmt.Errorf("unmarshal schedule preview failed: %w", err)
|
||||
}
|
||||
return &preview, nil
|
||||
}
|
||||
|
||||
// DeleteSchedulePlanPreview 删除“排程预览”缓存。
|
||||
//
|
||||
// 说明:
|
||||
// 1. 删除是幂等操作,key 不存在也视为成功。
|
||||
// 2. 用于新一轮排程前清理旧快照,避免前端读到过期结果。
|
||||
func (m *AgentCache) DeleteSchedulePlanPreview(ctx context.Context, userID int, sessionID string) error {
|
||||
return m.client.Del(ctx, m.schedulePreviewKey(userID, sessionID)).Err()
|
||||
}
|
||||
|
||||
@@ -146,6 +146,52 @@ func (dao *TaskClassDAO) GetCompleteTaskClassByID(ctx context.Context, id int, u
|
||||
return &taskClass, nil
|
||||
}
|
||||
|
||||
// GetCompleteTaskClassesByIDs 批量获取“完整任务类”(含 Items)。
|
||||
//
|
||||
// 职责边界:
|
||||
// 1. 负责按 user_id + ids 过滤,保证数据归属安全;
|
||||
// 2. 负责预加载 Items,供智能粗排直接使用;
|
||||
// 3. 不负责排序策略,返回结果顺序由 service 层决定;
|
||||
// 4. 若存在任一 id 不存在或不属于该用户,返回 WrongTaskClassID。
|
||||
func (dao *TaskClassDAO) GetCompleteTaskClassesByIDs(ctx context.Context, userID int, ids []int) ([]model.TaskClass, error) {
|
||||
if len(ids) == 0 {
|
||||
return []model.TaskClass{}, nil
|
||||
}
|
||||
|
||||
// 1. 先做去重与合法值过滤,避免无效 ID 放大数据库压力。
|
||||
uniqueIDs := make([]int, 0, len(ids))
|
||||
seen := make(map[int]struct{}, len(ids))
|
||||
for _, id := range ids {
|
||||
if id <= 0 {
|
||||
continue
|
||||
}
|
||||
if _, exists := seen[id]; exists {
|
||||
continue
|
||||
}
|
||||
seen[id] = struct{}{}
|
||||
uniqueIDs = append(uniqueIDs, id)
|
||||
}
|
||||
if len(uniqueIDs) == 0 {
|
||||
return nil, respond.WrongTaskClassID
|
||||
}
|
||||
|
||||
// 2. 批量查询并预加载任务项。
|
||||
var taskClasses []model.TaskClass
|
||||
err := dao.db.WithContext(ctx).
|
||||
Preload("Items").
|
||||
Where("user_id = ? AND id IN ?", userID, uniqueIDs).
|
||||
Find(&taskClasses).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 3. 数量校验:少一条都视为“存在非法/越权 ID”,统一按业务错误返回。
|
||||
if len(taskClasses) != len(uniqueIDs) {
|
||||
return nil, respond.WrongTaskClassID
|
||||
}
|
||||
return taskClasses, nil
|
||||
}
|
||||
|
||||
func (dao *TaskClassDAO) GetTaskClassItemByID(ctx context.Context, id int) (*model.TaskClassItem, error) {
|
||||
var item model.TaskClassItem
|
||||
err := dao.db.WithContext(ctx).
|
||||
|
||||
Reference in New Issue
Block a user