后端: 1.阶段 6 agent / memory 服务化收口 - 新增 cmd/agent 独立进程入口,承载 agent zrpc server、agent outbox relay / consumer 和运行时依赖初始化 - 补齐 services/agent/rpc 的 Chat stream 与 conversation meta/list/timeline、schedule-preview、context-stats、schedule-state unary RPC - 新增 gateway/client/agent 与 shared/contracts/agent,将 /api/v1/agent chat 和非 chat 门面切到 agent zrpc - 收缩 gateway 本地 AgentService 装配,双 RPC 开关开启时不再初始化本地 agent 编排、LLM、RAG 和 memory reader fallback - 将 backend/memory 物理迁入 services/memory,私有实现收入 internal,保留 module/model/observe 作为 memory 服务门面 - 调整 memory outbox、memory reader 和 agent 记忆渲染链路的 import 与服务边界,cmd/memory 独占 memory worker / consumer - 关闭 gateway 侧 agent outbox worker 所有权,agent relay / consumer 由 cmd/agent 独占,gateway 仅保留 HTTP/SSE 门面与迁移期开关回退 - 更新阶段 6 文档,记录 agent / memory 当前切流点、smoke 结果,以及 backend/client 与 gateway/shared 的目录收口口径
95 lines
2.4 KiB
Go
95 lines
2.4 KiB
Go
package agent
|
||
|
||
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 负责把 agent 的 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 = "agent zrpc service internal error"
|
||
}
|
||
return wrapRPCError(errors.New(msg))
|
||
}
|
||
|
||
msg := strings.TrimSpace(st.Message())
|
||
if msg == "" {
|
||
msg = "agent 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.ConversationNotFound.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("调用 agent zrpc 服务失败: %w", err)
|
||
}
|