后端:
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 个
65 lines
1.7 KiB
Go
65 lines
1.7 KiB
Go
package repo
|
||
|
||
import (
|
||
"context"
|
||
"errors"
|
||
|
||
"github.com/LoveLosita/smartflow/backend/services/runtime/model"
|
||
"gorm.io/gorm"
|
||
"gorm.io/gorm/clause"
|
||
)
|
||
|
||
// SettingsRepo 封装 memory_user_settings 的读写。
|
||
type SettingsRepo struct {
|
||
db *gorm.DB
|
||
}
|
||
|
||
func NewSettingsRepo(db *gorm.DB) *SettingsRepo {
|
||
return &SettingsRepo{db: db}
|
||
}
|
||
|
||
func (r *SettingsRepo) WithTx(tx *gorm.DB) *SettingsRepo {
|
||
return &SettingsRepo{db: tx}
|
||
}
|
||
|
||
// GetByUserID 读取用户记忆设置。
|
||
//
|
||
// 返回语义:
|
||
// 1. 命中时返回真实记录;
|
||
// 2. 未命中时返回 nil,nil,由上层决定是否走默认开关;
|
||
// 3. 不在仓储层偷偷补默认值,避免写路径和读路径语义不一致。
|
||
func (r *SettingsRepo) GetByUserID(ctx context.Context, userID int) (*model.MemoryUserSetting, error) {
|
||
if r == nil || r.db == nil {
|
||
return nil, errors.New("memory settings repo is nil")
|
||
}
|
||
if userID <= 0 {
|
||
return nil, errors.New("memory settings user_id is invalid")
|
||
}
|
||
|
||
var setting model.MemoryUserSetting
|
||
query := r.db.WithContext(ctx).Where("user_id = ?", userID).Limit(1).Find(&setting)
|
||
if query.Error != nil {
|
||
return nil, query.Error
|
||
}
|
||
if query.RowsAffected == 0 {
|
||
return nil, nil
|
||
}
|
||
return &setting, nil
|
||
}
|
||
|
||
// Upsert 写入用户记忆设置。
|
||
func (r *SettingsRepo) Upsert(ctx context.Context, setting model.MemoryUserSetting) error {
|
||
if r == nil || r.db == nil {
|
||
return errors.New("memory settings repo is nil")
|
||
}
|
||
return r.db.WithContext(ctx).Clauses(clause.OnConflict{
|
||
Columns: []clause.Column{{Name: "user_id"}},
|
||
DoUpdates: clause.AssignmentColumns([]string{
|
||
"memory_enabled",
|
||
"implicit_memory_enabled",
|
||
"sensitive_memory_enabled",
|
||
"updated_at",
|
||
}),
|
||
}).Create(&setting).Error
|
||
}
|