后端:
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 个
78 lines
2.1 KiB
Go
78 lines
2.1 KiB
Go
package utils
|
||
|
||
import (
|
||
"encoding/json"
|
||
"strings"
|
||
|
||
"github.com/LoveLosita/smartflow/backend/services/runtime/model"
|
||
)
|
||
|
||
const (
|
||
// AuditOperationCreate 表示系统新建一条记忆。
|
||
AuditOperationCreate = "create"
|
||
// AuditOperationUpdate 表示决策层更新已有记忆的内容。
|
||
AuditOperationUpdate = "update"
|
||
// AuditOperationArchive 表示治理层把重复记忆归档。
|
||
AuditOperationArchive = "archive"
|
||
// AuditOperationDelete 表示对已有记忆做软删除。
|
||
AuditOperationDelete = "delete"
|
||
// AuditOperationRestore 表示把已删除/归档记忆恢复为 active。
|
||
AuditOperationRestore = "restore"
|
||
)
|
||
|
||
// BuildItemAuditLog 构造记忆变更审计日志。
|
||
//
|
||
// 职责边界:
|
||
// 1. 负责把 before/after 快照统一序列化为审计日志结构;
|
||
// 2. 不负责决定“是否应该写审计”,该决策由上层 service/worker 控制;
|
||
// 3. 不负责落库,调用方仍需显式调用 AuditRepo。
|
||
func BuildItemAuditLog(
|
||
memoryID int64,
|
||
userID int,
|
||
operation string,
|
||
operatorType string,
|
||
reason string,
|
||
before *model.MemoryItem,
|
||
after *model.MemoryItem,
|
||
) model.MemoryAuditLog {
|
||
return model.MemoryAuditLog{
|
||
MemoryID: memoryID,
|
||
UserID: userID,
|
||
Operation: strings.TrimSpace(operation),
|
||
OperatorType: NormalizeOperatorType(operatorType),
|
||
Reason: strings.TrimSpace(reason),
|
||
BeforeJSON: marshalMemoryItemSnapshot(before),
|
||
AfterJSON: marshalMemoryItemSnapshot(after),
|
||
}
|
||
}
|
||
|
||
// NormalizeOperatorType 统一规整审计操作者类型。
|
||
//
|
||
// 规则说明:
|
||
// 1. 目前只接受 user/system 两类固定值;
|
||
// 2. 空值或未知值统一回退为 user,避免把脏值直接写进审计表;
|
||
// 3. 若后续扩展 admin/tool 等类型,再在这里集中放开即可。
|
||
func NormalizeOperatorType(raw string) string {
|
||
switch strings.ToLower(strings.TrimSpace(raw)) {
|
||
case "system":
|
||
return "system"
|
||
default:
|
||
return "user"
|
||
}
|
||
}
|
||
|
||
func marshalMemoryItemSnapshot(item *model.MemoryItem) *string {
|
||
if item == nil {
|
||
return nil
|
||
}
|
||
|
||
raw, err := json.Marshal(item)
|
||
if err != nil {
|
||
empty := "{}"
|
||
return &empty
|
||
}
|
||
|
||
value := string(raw)
|
||
return &value
|
||
}
|