Version: 0.9.74.dev.260505

后端:
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 注释口径
This commit is contained in:
Losita
2026-05-05 13:52:49 +08:00
parent fd327f845b
commit e1819c5653
19 changed files with 1688 additions and 110 deletions

View File

@@ -8,25 +8,30 @@ import (
"strings"
"time"
memorypkg "github.com/LoveLosita/smartflow/backend/memory"
memorymodel "github.com/LoveLosita/smartflow/backend/memory/model"
"github.com/LoveLosita/smartflow/backend/model"
"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 {
module *memorypkg.Module
client ports.MemoryCommandClient
}
var errMemoryHandlerNotReady = errors.New("memory handler is not initialized")
func NewMemoryHandler(module *memorypkg.Module) *MemoryHandler {
return &MemoryHandler{module: module}
// NewMemoryHandler 创建 memory HTTP 门面。
//
// 职责边界:
// 1. gateway 只负责鉴权后的参数绑定、超时和响应透传;
// 2. 记忆管理业务、审计、向量同步和状态校验都交给 memory zrpc 服务;
// 3. agent 的 memory reader 已在 CP3 切到 memory zrpcHTTP 管理面这里只保留管理职责。
func NewMemoryHandler(client ports.MemoryCommandClient) *MemoryHandler {
return &MemoryHandler{client: client}
}
func (h *MemoryHandler) ListItems(c *gin.Context) {
if h == nil || h.module == nil {
if h == nil || h.client == nil {
c.JSON(http.StatusInternalServerError, respond.InternalError(errMemoryHandlerNotReady))
return
}
@@ -48,7 +53,7 @@ func (h *MemoryHandler) ListItems(c *gin.Context) {
ctx, cancel := context.WithTimeout(c.Request.Context(), 2*time.Second)
defer cancel()
items, err := h.module.ListItems(ctx, memorymodel.ListItemsRequest{
resp, err := h.client.ListItems(ctx, memorycontracts.ListItemsRequest{
UserID: c.GetInt("user_id"),
ConversationID: strings.TrimSpace(c.Query("conversation_id")),
Statuses: splitCSV(statusesRaw),
@@ -60,11 +65,11 @@ func (h *MemoryHandler) ListItems(c *gin.Context) {
return
}
c.JSON(http.StatusOK, respond.RespWithData(respond.Ok, toMemoryItemViews(items)))
c.JSON(http.StatusOK, respond.RespWithData(respond.Ok, resp))
}
func (h *MemoryHandler) GetItem(c *gin.Context) {
if h == nil || h.module == nil {
if h == nil || h.client == nil {
c.JSON(http.StatusInternalServerError, respond.InternalError(errMemoryHandlerNotReady))
return
}
@@ -78,7 +83,7 @@ func (h *MemoryHandler) GetItem(c *gin.Context) {
ctx, cancel := context.WithTimeout(c.Request.Context(), 2*time.Second)
defer cancel()
item, err := h.module.GetItem(ctx, model.MemoryGetItemRequest{
resp, err := h.client.GetItem(ctx, memorycontracts.GetItemRequest{
UserID: c.GetInt("user_id"),
MemoryID: memoryID,
})
@@ -86,16 +91,16 @@ func (h *MemoryHandler) GetItem(c *gin.Context) {
respond.DealWithError(c, err)
return
}
c.JSON(http.StatusOK, respond.RespWithData(respond.Ok, toMemoryItemView(item)))
c.JSON(http.StatusOK, respond.RespWithData(respond.Ok, resp))
}
func (h *MemoryHandler) CreateItem(c *gin.Context) {
if h == nil || h.module == nil {
if h == nil || h.client == nil {
c.JSON(http.StatusInternalServerError, respond.InternalError(errMemoryHandlerNotReady))
return
}
var req model.MemoryCreateItemRequest
var req memorycontracts.CreateItemRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, respond.WrongParamType)
return
@@ -106,16 +111,16 @@ func (h *MemoryHandler) CreateItem(c *gin.Context) {
ctx, cancel := context.WithTimeout(c.Request.Context(), 2*time.Second)
defer cancel()
item, err := h.module.CreateItem(ctx, req)
resp, err := h.client.CreateItem(ctx, req)
if err != nil {
respond.DealWithError(c, err)
return
}
c.JSON(http.StatusOK, respond.RespWithData(respond.Ok, toMemoryItemView(item)))
c.JSON(http.StatusOK, respond.RespWithData(respond.Ok, resp))
}
func (h *MemoryHandler) UpdateItem(c *gin.Context) {
if h == nil || h.module == nil {
if h == nil || h.client == nil {
c.JSON(http.StatusInternalServerError, respond.InternalError(errMemoryHandlerNotReady))
return
}
@@ -126,7 +131,7 @@ func (h *MemoryHandler) UpdateItem(c *gin.Context) {
return
}
var req model.MemoryUpdateItemRequest
var req memorycontracts.UpdateItemRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, respond.WrongParamType)
return
@@ -138,16 +143,16 @@ func (h *MemoryHandler) UpdateItem(c *gin.Context) {
ctx, cancel := context.WithTimeout(c.Request.Context(), 2*time.Second)
defer cancel()
item, err := h.module.UpdateItem(ctx, req)
resp, err := h.client.UpdateItem(ctx, req)
if err != nil {
respond.DealWithError(c, err)
return
}
c.JSON(http.StatusOK, respond.RespWithData(respond.Ok, toMemoryItemView(item)))
c.JSON(http.StatusOK, respond.RespWithData(respond.Ok, resp))
}
func (h *MemoryHandler) DeleteItem(c *gin.Context) {
if h == nil || h.module == nil {
if h == nil || h.client == nil {
c.JSON(http.StatusInternalServerError, respond.InternalError(errMemoryHandlerNotReady))
return
}
@@ -166,7 +171,7 @@ func (h *MemoryHandler) DeleteItem(c *gin.Context) {
ctx, cancel := context.WithTimeout(c.Request.Context(), 2*time.Second)
defer cancel()
item, err := h.module.DeleteItem(ctx, model.MemoryDeleteItemRequest{
resp, err := h.client.DeleteItem(ctx, memorycontracts.DeleteItemRequest{
UserID: c.GetInt("user_id"),
MemoryID: memoryID,
Reason: strings.TrimSpace(body.Reason),
@@ -176,11 +181,11 @@ func (h *MemoryHandler) DeleteItem(c *gin.Context) {
respond.DealWithError(c, err)
return
}
c.JSON(http.StatusOK, respond.RespWithData(respond.Ok, toMemoryItemView(item)))
c.JSON(http.StatusOK, respond.RespWithData(respond.Ok, resp))
}
func (h *MemoryHandler) RestoreItem(c *gin.Context) {
if h == nil || h.module == nil {
if h == nil || h.client == nil {
c.JSON(http.StatusInternalServerError, respond.InternalError(errMemoryHandlerNotReady))
return
}
@@ -199,7 +204,7 @@ func (h *MemoryHandler) RestoreItem(c *gin.Context) {
ctx, cancel := context.WithTimeout(c.Request.Context(), 2*time.Second)
defer cancel()
item, err := h.module.RestoreItem(ctx, model.MemoryRestoreItemRequest{
resp, err := h.client.RestoreItem(ctx, memorycontracts.RestoreItemRequest{
UserID: c.GetInt("user_id"),
MemoryID: memoryID,
Reason: strings.TrimSpace(body.Reason),
@@ -209,7 +214,7 @@ func (h *MemoryHandler) RestoreItem(c *gin.Context) {
respond.DealWithError(c, err)
return
}
c.JSON(http.StatusOK, respond.RespWithData(respond.Ok, toMemoryItemView(item)))
c.JSON(http.StatusOK, respond.RespWithData(respond.Ok, resp))
}
func parseMemoryIDParam(c *gin.Context) (int64, bool) {
@@ -252,39 +257,3 @@ func splitCSV(raw string) []string {
}
return result
}
func toMemoryItemViews(items []memorymodel.ItemDTO) []model.MemoryItemView {
if len(items) == 0 {
return nil
}
result := make([]model.MemoryItemView, 0, len(items))
for _, item := range items {
result = append(result, toMemoryItemView(&item))
}
return result
}
func toMemoryItemView(item *memorymodel.ItemDTO) model.MemoryItemView {
if item == nil {
return model.MemoryItemView{}
}
return model.MemoryItemView{
ID: item.ID,
UserID: item.UserID,
ConversationID: item.ConversationID,
AssistantID: item.AssistantID,
RunID: item.RunID,
MemoryType: item.MemoryType,
Title: item.Title,
Content: item.Content,
ContentHash: item.ContentHash,
Confidence: item.Confidence,
Importance: item.Importance,
SensitivityLevel: item.SensitivityLevel,
IsExplicit: item.IsExplicit,
Status: item.Status,
TTLAt: item.TTLAt,
CreatedAt: item.CreatedAt,
UpdatedAt: item.UpdatedAt,
}
}

View File

@@ -0,0 +1,155 @@
package memory
import (
"context"
"encoding/json"
"errors"
"strings"
"time"
memorypb "github.com/LoveLosita/smartflow/backend/services/memory/rpc/pb"
memorycontracts "github.com/LoveLosita/smartflow/backend/shared/contracts/memory"
"github.com/zeromicro/go-zero/zrpc"
"google.golang.org/grpc"
)
const (
defaultEndpoint = "127.0.0.1:9088"
defaultTimeout = 6 * time.Second
)
type ClientConfig struct {
Endpoints []string
Target string
Timeout time.Duration
}
// Client 是 gateway 访问 memory zrpc 的最小适配层。
//
// 职责边界:
// 1. 只负责跨进程 gRPC 调用和 JSON 透传,不触碰 memory repo、worker 或 outbox
// 2. HTTP 入参仍由 gateway/api 做基础绑定,业务校验交给 memory 服务;
// 3. 复杂响应不在 gateway 重建模型,避免 DTO 复制扩散。
type Client struct {
rpc memorypb.MemoryClient
}
func NewClient(cfg ClientConfig) (*Client, error) {
timeout := cfg.Timeout
if timeout <= 0 {
timeout = defaultTimeout
}
endpoints := normalizeEndpoints(cfg.Endpoints)
target := strings.TrimSpace(cfg.Target)
if len(endpoints) == 0 && target == "" {
endpoints = []string{defaultEndpoint}
}
zclient, err := zrpc.NewClient(zrpc.RpcClientConf{
Endpoints: endpoints,
Target: target,
NonBlock: true,
Timeout: int64(timeout / time.Millisecond),
})
if err != nil {
return nil, err
}
// 1. 这里不在构造期 Ping memory 服务,避免 cmd/memory 短暂不可用时拖垮整个 gateway/worker 启动。
// 2. 真正的可用性检查延迟到各个 RPC 调用,由 `/api/v1/memory/*` 自己返回局部错误。
client := &Client{rpc: memorypb.NewMemoryClient(zclient.Conn())}
return client, nil
}
// Retrieve 调用 memory 服务完成 agent 记忆读取。
//
// 职责边界:
// 1. 只负责跨进程 JSON 编解码和 gRPC 错误还原;
// 2. 不在 gateway 侧重做召回、过滤或 prompt 渲染;
// 3. 返回 ItemDTO 给 agent 适配器继续转换为内部模型。
func (c *Client) Retrieve(ctx context.Context, req memorycontracts.RetrieveRequest) ([]memorycontracts.ItemDTO, error) {
resp, err := c.callJSON(ctx, c.rpc.Retrieve, req)
raw, err := jsonFromResponse(resp, err)
if err != nil {
return nil, err
}
if len(raw) == 0 || string(raw) == "null" {
return nil, nil
}
var items []memorycontracts.ItemDTO
if err := json.Unmarshal(raw, &items); err != nil {
return nil, err
}
return items, nil
}
func (c *Client) ListItems(ctx context.Context, req memorycontracts.ListItemsRequest) (json.RawMessage, error) {
resp, err := c.callJSON(ctx, c.rpc.ListItems, req)
return jsonFromResponse(resp, err)
}
func (c *Client) GetItem(ctx context.Context, req memorycontracts.GetItemRequest) (json.RawMessage, error) {
resp, err := c.callJSON(ctx, c.rpc.GetItem, req)
return jsonFromResponse(resp, err)
}
func (c *Client) CreateItem(ctx context.Context, req memorycontracts.CreateItemRequest) (json.RawMessage, error) {
resp, err := c.callJSON(ctx, c.rpc.CreateItem, req)
return jsonFromResponse(resp, err)
}
func (c *Client) UpdateItem(ctx context.Context, req memorycontracts.UpdateItemRequest) (json.RawMessage, error) {
resp, err := c.callJSON(ctx, c.rpc.UpdateItem, req)
return jsonFromResponse(resp, err)
}
func (c *Client) DeleteItem(ctx context.Context, req memorycontracts.DeleteItemRequest) (json.RawMessage, error) {
resp, err := c.callJSON(ctx, c.rpc.DeleteItem, req)
return jsonFromResponse(resp, err)
}
func (c *Client) RestoreItem(ctx context.Context, req memorycontracts.RestoreItemRequest) (json.RawMessage, error) {
resp, err := c.callJSON(ctx, c.rpc.RestoreItem, req)
return jsonFromResponse(resp, err)
}
func (c *Client) ensureReady() error {
if c == nil || c.rpc == nil {
return errors.New("memory zrpc client is not initialized")
}
return nil
}
func (c *Client) callJSON(ctx context.Context, fn func(context.Context, *memorypb.JSONRequest, ...grpc.CallOption) (*memorypb.JSONResponse, error), payload any) (*memorypb.JSONResponse, error) {
if err := c.ensureReady(); err != nil {
return nil, err
}
raw, err := json.Marshal(payload)
if err != nil {
return nil, err
}
return fn(ctx, &memorypb.JSONRequest{PayloadJson: raw})
}
func jsonFromResponse(resp *memorypb.JSONResponse, rpcErr error) (json.RawMessage, error) {
if rpcErr != nil {
return nil, responseFromRPCError(rpcErr)
}
if resp == nil {
return nil, errors.New("memory zrpc service returned empty JSON response")
}
if len(resp.DataJson) == 0 {
return json.RawMessage("null"), nil
}
return json.RawMessage(resp.DataJson), nil
}
func normalizeEndpoints(values []string) []string {
endpoints := make([]string, 0, len(values))
for _, value := range values {
trimmed := strings.TrimSpace(value)
if trimmed != "" {
endpoints = append(endpoints, trimmed)
}
}
return endpoints
}

View File

@@ -0,0 +1,94 @@
package memory
import (
"errors"
"fmt"
"strings"
"github.com/LoveLosita/smartflow/backend/respond"
"google.golang.org/genproto/googleapis/rpc/errdetails"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
// responseFromRPCError 负责把 memory 的 gRPC 错误反解回项目内错误。
//
// 职责边界:
// 1. 只在 gateway 边缘层使用;
// 2. 业务错误尽量恢复成 respond.Response方便 API 层复用 DealWithError
// 3. 服务不可用或未知内部错误包装成普通 error避免误报成用户可修正的参数问题。
func responseFromRPCError(err error) error {
if err == nil {
return nil
}
st, ok := status.FromError(err)
if !ok {
return wrapRPCError(err)
}
if resp, ok := responseFromStatus(st); ok {
return resp
}
switch st.Code() {
case codes.Internal, codes.Unknown, codes.Unavailable, codes.DeadlineExceeded, codes.DataLoss, codes.Unimplemented:
msg := strings.TrimSpace(st.Message())
if msg == "" {
msg = "memory zrpc service internal error"
}
return wrapRPCError(errors.New(msg))
}
msg := strings.TrimSpace(st.Message())
if msg == "" {
msg = "memory zrpc service rejected request"
}
return respond.Response{Status: grpcCodeToRespondStatus(st.Code()), Info: msg}
}
func responseFromStatus(st *status.Status) (respond.Response, bool) {
if st == nil {
return respond.Response{}, false
}
for _, detail := range st.Details() {
info, ok := detail.(*errdetails.ErrorInfo)
if !ok {
continue
}
statusValue := strings.TrimSpace(info.Reason)
if statusValue == "" {
statusValue = grpcCodeToRespondStatus(st.Code())
}
message := strings.TrimSpace(st.Message())
if message == "" && info.Metadata != nil {
message = strings.TrimSpace(info.Metadata["info"])
}
if message == "" {
message = statusValue
}
return respond.Response{Status: statusValue, Info: message}, true
}
return respond.Response{}, false
}
func grpcCodeToRespondStatus(code codes.Code) string {
switch code {
case codes.Unauthenticated:
return respond.ErrUnauthorized.Status
case codes.InvalidArgument:
return respond.MissingParam.Status
case codes.NotFound:
return respond.MemoryItemNotFound.Status
case codes.Internal, codes.Unknown, codes.DataLoss:
return "500"
default:
return "400"
}
}
func wrapRPCError(err error) error {
if err == nil {
return nil
}
return fmt.Errorf("调用 memory zrpc 服务失败: %w", err)
}