后端:
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 个
260 lines
6.4 KiB
Go
260 lines
6.4 KiB
Go
package api
|
||
|
||
import (
|
||
"context"
|
||
"errors"
|
||
"net/http"
|
||
"strconv"
|
||
"strings"
|
||
"time"
|
||
|
||
"github.com/LoveLosita/smartflow/backend/gateway/shared/respond"
|
||
memorycontracts "github.com/LoveLosita/smartflow/backend/shared/contracts/memory"
|
||
"github.com/LoveLosita/smartflow/backend/shared/ports"
|
||
"github.com/gin-gonic/gin"
|
||
)
|
||
|
||
type MemoryHandler struct {
|
||
client ports.MemoryCommandClient
|
||
}
|
||
|
||
var errMemoryHandlerNotReady = errors.New("memory handler is not initialized")
|
||
|
||
// NewMemoryHandler 创建 memory HTTP 门面。
|
||
//
|
||
// 职责边界:
|
||
// 1. gateway 只负责鉴权后的参数绑定、超时和响应透传;
|
||
// 2. 记忆管理业务、审计、向量同步和状态校验都交给 memory zrpc 服务;
|
||
// 3. agent 的 memory reader 已在 CP3 切到 memory zrpc,HTTP 管理面这里只保留管理职责。
|
||
func NewMemoryHandler(client ports.MemoryCommandClient) *MemoryHandler {
|
||
return &MemoryHandler{client: client}
|
||
}
|
||
|
||
func (h *MemoryHandler) ListItems(c *gin.Context) {
|
||
if h == nil || h.client == nil {
|
||
c.JSON(http.StatusInternalServerError, respond.InternalError(errMemoryHandlerNotReady))
|
||
return
|
||
}
|
||
|
||
limit, ok := parseOptionalInt(c.Query("limit"))
|
||
if !ok {
|
||
c.JSON(http.StatusBadRequest, respond.WrongParamType)
|
||
return
|
||
}
|
||
statusesRaw := c.Query("statuses")
|
||
if strings.TrimSpace(statusesRaw) == "" {
|
||
statusesRaw = c.Query("status")
|
||
}
|
||
memoryTypesRaw := c.Query("memory_types")
|
||
if strings.TrimSpace(memoryTypesRaw) == "" {
|
||
memoryTypesRaw = c.Query("memory_type")
|
||
}
|
||
|
||
ctx, cancel := context.WithTimeout(c.Request.Context(), 2*time.Second)
|
||
defer cancel()
|
||
|
||
resp, err := h.client.ListItems(ctx, memorycontracts.ListItemsRequest{
|
||
UserID: c.GetInt("user_id"),
|
||
ConversationID: strings.TrimSpace(c.Query("conversation_id")),
|
||
Statuses: splitCSV(statusesRaw),
|
||
MemoryTypes: splitCSV(memoryTypesRaw),
|
||
Limit: limit,
|
||
})
|
||
if err != nil {
|
||
respond.DealWithError(c, err)
|
||
return
|
||
}
|
||
|
||
c.JSON(http.StatusOK, respond.RespWithData(respond.Ok, resp))
|
||
}
|
||
|
||
func (h *MemoryHandler) GetItem(c *gin.Context) {
|
||
if h == nil || h.client == nil {
|
||
c.JSON(http.StatusInternalServerError, respond.InternalError(errMemoryHandlerNotReady))
|
||
return
|
||
}
|
||
|
||
memoryID, ok := parseMemoryIDParam(c)
|
||
if !ok {
|
||
c.JSON(http.StatusBadRequest, respond.WrongParamType)
|
||
return
|
||
}
|
||
|
||
ctx, cancel := context.WithTimeout(c.Request.Context(), 2*time.Second)
|
||
defer cancel()
|
||
|
||
resp, err := h.client.GetItem(ctx, memorycontracts.GetItemRequest{
|
||
UserID: c.GetInt("user_id"),
|
||
MemoryID: memoryID,
|
||
})
|
||
if err != nil {
|
||
respond.DealWithError(c, err)
|
||
return
|
||
}
|
||
c.JSON(http.StatusOK, respond.RespWithData(respond.Ok, resp))
|
||
}
|
||
|
||
func (h *MemoryHandler) CreateItem(c *gin.Context) {
|
||
if h == nil || h.client == nil {
|
||
c.JSON(http.StatusInternalServerError, respond.InternalError(errMemoryHandlerNotReady))
|
||
return
|
||
}
|
||
|
||
var req memorycontracts.CreateItemRequest
|
||
if err := c.ShouldBindJSON(&req); err != nil {
|
||
c.JSON(http.StatusBadRequest, respond.WrongParamType)
|
||
return
|
||
}
|
||
req.UserID = c.GetInt("user_id")
|
||
req.OperatorType = "user"
|
||
|
||
ctx, cancel := context.WithTimeout(c.Request.Context(), 2*time.Second)
|
||
defer cancel()
|
||
|
||
resp, err := h.client.CreateItem(ctx, req)
|
||
if err != nil {
|
||
respond.DealWithError(c, err)
|
||
return
|
||
}
|
||
c.JSON(http.StatusOK, respond.RespWithData(respond.Ok, resp))
|
||
}
|
||
|
||
func (h *MemoryHandler) UpdateItem(c *gin.Context) {
|
||
if h == nil || h.client == nil {
|
||
c.JSON(http.StatusInternalServerError, respond.InternalError(errMemoryHandlerNotReady))
|
||
return
|
||
}
|
||
|
||
memoryID, ok := parseMemoryIDParam(c)
|
||
if !ok {
|
||
c.JSON(http.StatusBadRequest, respond.WrongParamType)
|
||
return
|
||
}
|
||
|
||
var req memorycontracts.UpdateItemRequest
|
||
if err := c.ShouldBindJSON(&req); err != nil {
|
||
c.JSON(http.StatusBadRequest, respond.WrongParamType)
|
||
return
|
||
}
|
||
req.UserID = c.GetInt("user_id")
|
||
req.MemoryID = memoryID
|
||
req.OperatorType = "user"
|
||
|
||
ctx, cancel := context.WithTimeout(c.Request.Context(), 2*time.Second)
|
||
defer cancel()
|
||
|
||
resp, err := h.client.UpdateItem(ctx, req)
|
||
if err != nil {
|
||
respond.DealWithError(c, err)
|
||
return
|
||
}
|
||
c.JSON(http.StatusOK, respond.RespWithData(respond.Ok, resp))
|
||
}
|
||
|
||
func (h *MemoryHandler) DeleteItem(c *gin.Context) {
|
||
if h == nil || h.client == nil {
|
||
c.JSON(http.StatusInternalServerError, respond.InternalError(errMemoryHandlerNotReady))
|
||
return
|
||
}
|
||
|
||
memoryID, ok := parseMemoryIDParam(c)
|
||
if !ok {
|
||
c.JSON(http.StatusBadRequest, respond.WrongParamType)
|
||
return
|
||
}
|
||
|
||
var body struct {
|
||
Reason string `json:"reason"`
|
||
}
|
||
_ = c.ShouldBindJSON(&body)
|
||
|
||
ctx, cancel := context.WithTimeout(c.Request.Context(), 2*time.Second)
|
||
defer cancel()
|
||
|
||
resp, err := h.client.DeleteItem(ctx, memorycontracts.DeleteItemRequest{
|
||
UserID: c.GetInt("user_id"),
|
||
MemoryID: memoryID,
|
||
Reason: strings.TrimSpace(body.Reason),
|
||
OperatorType: "user",
|
||
})
|
||
if err != nil {
|
||
respond.DealWithError(c, err)
|
||
return
|
||
}
|
||
c.JSON(http.StatusOK, respond.RespWithData(respond.Ok, resp))
|
||
}
|
||
|
||
func (h *MemoryHandler) RestoreItem(c *gin.Context) {
|
||
if h == nil || h.client == nil {
|
||
c.JSON(http.StatusInternalServerError, respond.InternalError(errMemoryHandlerNotReady))
|
||
return
|
||
}
|
||
|
||
memoryID, ok := parseMemoryIDParam(c)
|
||
if !ok {
|
||
c.JSON(http.StatusBadRequest, respond.WrongParamType)
|
||
return
|
||
}
|
||
|
||
var body struct {
|
||
Reason string `json:"reason"`
|
||
}
|
||
_ = c.ShouldBindJSON(&body)
|
||
|
||
ctx, cancel := context.WithTimeout(c.Request.Context(), 2*time.Second)
|
||
defer cancel()
|
||
|
||
resp, err := h.client.RestoreItem(ctx, memorycontracts.RestoreItemRequest{
|
||
UserID: c.GetInt("user_id"),
|
||
MemoryID: memoryID,
|
||
Reason: strings.TrimSpace(body.Reason),
|
||
OperatorType: "user",
|
||
})
|
||
if err != nil {
|
||
respond.DealWithError(c, err)
|
||
return
|
||
}
|
||
c.JSON(http.StatusOK, respond.RespWithData(respond.Ok, resp))
|
||
}
|
||
|
||
func parseMemoryIDParam(c *gin.Context) (int64, bool) {
|
||
raw := strings.TrimSpace(c.Param("id"))
|
||
if raw == "" {
|
||
return 0, false
|
||
}
|
||
value, err := strconv.ParseInt(raw, 10, 64)
|
||
if err != nil || value <= 0 {
|
||
return 0, false
|
||
}
|
||
return value, true
|
||
}
|
||
|
||
func parseOptionalInt(raw string) (int, bool) {
|
||
raw = strings.TrimSpace(raw)
|
||
if raw == "" {
|
||
return 0, true
|
||
}
|
||
value, err := strconv.Atoi(raw)
|
||
if err != nil {
|
||
return 0, false
|
||
}
|
||
return value, true
|
||
}
|
||
|
||
func splitCSV(raw string) []string {
|
||
raw = strings.TrimSpace(raw)
|
||
if raw == "" {
|
||
return nil
|
||
}
|
||
parts := strings.Split(raw, ",")
|
||
result := make([]string, 0, len(parts))
|
||
for _, part := range parts {
|
||
part = strings.TrimSpace(part)
|
||
if part == "" {
|
||
continue
|
||
}
|
||
result = append(result, part)
|
||
}
|
||
return result
|
||
}
|