后端: 1.阶段 6 memory 服务化 CP1-CP3 落地 - 新增 cmd/memory 独立进程入口,落地 services/memory dao/rpc/sv 与 memory zrpc pb - 将 memory.extract.requested outbox 消费与 memory worker 迁入 cmd/memory,单体 worker 不再消费 memory outbox - 新增 gateway/client/memory、shared/contracts/memory 和 shared/ports memory port - 将 /api/v1/memory/items* HTTP 管理面切到 memory zrpc,gateway 只保留鉴权、限流、幂等、参数绑定和响应透传 - 新增 memory Retrieve RPC,并将 agent 主链路 memory reader 切到 memory zrpc 读取 - 补充 agent memory RPC reader 适配器,保留注入侧 observer / metrics 观测能力 - 保留旧 backend/memory 核心实现作为迁移期复用与回退面,cmd/memory 内部继续复用既有 Module / ReadService 逻辑 - 补充 memory.rpc 示例配置,更新单体 outbox 发布边界与 memory handler 注释口径
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/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
|
||
}
|