Version: 0.9.77.dev.260505
后端:
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 个
This commit is contained in:
181
backend/client/notification/client.go
Normal file
181
backend/client/notification/client.go
Normal file
@@ -0,0 +1,181 @@
|
||||
package notification
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
notificationpb "github.com/LoveLosita/smartflow/backend/services/notification/rpc/pb"
|
||||
contracts "github.com/LoveLosita/smartflow/backend/shared/contracts/notification"
|
||||
"github.com/zeromicro/go-zero/zrpc"
|
||||
)
|
||||
|
||||
const (
|
||||
defaultEndpoint = "127.0.0.1:9082"
|
||||
defaultTimeout = 6 * time.Second
|
||||
)
|
||||
|
||||
type ClientConfig struct {
|
||||
Endpoints []string
|
||||
Target string
|
||||
Timeout time.Duration
|
||||
}
|
||||
|
||||
// Client 是 gateway 侧 notification zrpc 的最小适配层。
|
||||
//
|
||||
// 职责边界:
|
||||
// 1. 只负责跨进程 gRPC 调用和响应转译,不碰 DB / provider / outbox 细节;
|
||||
// 2. 服务端业务错误先通过 gRPC status 传输,再在这里反解回 respond.Response 风格;
|
||||
// 3. 上层调用方仍然可以保持 `res, err :=` 的统一用法。
|
||||
type Client struct {
|
||||
rpc notificationpb.NotificationClient
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
return &Client{rpc: notificationpb.NewNotificationClient(zclient.Conn())}, nil
|
||||
}
|
||||
|
||||
func (c *Client) GetFeishuWebhook(ctx context.Context, req contracts.GetFeishuWebhookRequest) (*contracts.ChannelResponse, error) {
|
||||
if err := c.ensureReady(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
resp, err := c.rpc.GetFeishuWebhook(ctx, ¬ificationpb.GetFeishuWebhookRequest{
|
||||
UserId: int64(req.UserID),
|
||||
})
|
||||
if err != nil {
|
||||
return nil, responseFromRPCError(err)
|
||||
}
|
||||
return channelFromResponse(resp)
|
||||
}
|
||||
|
||||
func (c *Client) SaveFeishuWebhook(ctx context.Context, req contracts.SaveFeishuWebhookRequest) (*contracts.ChannelResponse, error) {
|
||||
if err := c.ensureReady(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
resp, err := c.rpc.SaveFeishuWebhook(ctx, ¬ificationpb.SaveFeishuWebhookRequest{
|
||||
UserId: int64(req.UserID),
|
||||
Enabled: req.Enabled,
|
||||
WebhookUrl: req.WebhookURL,
|
||||
AuthType: req.AuthType,
|
||||
BearerToken: req.BearerToken,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, responseFromRPCError(err)
|
||||
}
|
||||
return channelFromResponse(resp)
|
||||
}
|
||||
|
||||
func (c *Client) DeleteFeishuWebhook(ctx context.Context, req contracts.DeleteFeishuWebhookRequest) error {
|
||||
if err := c.ensureReady(); err != nil {
|
||||
return err
|
||||
}
|
||||
resp, err := c.rpc.DeleteFeishuWebhook(ctx, ¬ificationpb.DeleteFeishuWebhookRequest{
|
||||
UserId: int64(req.UserID),
|
||||
})
|
||||
if err != nil {
|
||||
return responseFromRPCError(err)
|
||||
}
|
||||
if resp == nil {
|
||||
return errors.New("notification zrpc service returned empty delete response")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Client) TestFeishuWebhook(ctx context.Context, req contracts.TestFeishuWebhookRequest) (*contracts.TestResult, error) {
|
||||
if err := c.ensureReady(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
resp, err := c.rpc.TestFeishuWebhook(ctx, ¬ificationpb.TestFeishuWebhookRequest{
|
||||
UserId: int64(req.UserID),
|
||||
})
|
||||
if err != nil {
|
||||
return nil, responseFromRPCError(err)
|
||||
}
|
||||
return testResultFromResponse(resp)
|
||||
}
|
||||
|
||||
func (c *Client) ensureReady() error {
|
||||
if c == nil || c.rpc == nil {
|
||||
return errors.New("notification zrpc client is not initialized")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func channelFromResponse(resp *notificationpb.ChannelResponse) (*contracts.ChannelResponse, error) {
|
||||
if resp == nil {
|
||||
return nil, errors.New("notification zrpc service returned empty channel response")
|
||||
}
|
||||
var lastTestAt *time.Time
|
||||
if value := timeFromUnixNano(resp.LastTestAtUnixNano); !value.IsZero() {
|
||||
lastTestAt = &value
|
||||
}
|
||||
return &contracts.ChannelResponse{
|
||||
Channel: resp.Channel,
|
||||
Enabled: resp.Enabled,
|
||||
Configured: resp.Configured,
|
||||
WebhookURLMask: resp.WebhookUrlMask,
|
||||
AuthType: resp.AuthType,
|
||||
HasBearerToken: resp.HasBearerToken,
|
||||
LastTestStatus: resp.LastTestStatus,
|
||||
LastTestError: resp.LastTestError,
|
||||
LastTestAt: lastTestAt,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func testResultFromResponse(resp *notificationpb.TestResult) (*contracts.TestResult, error) {
|
||||
if resp == nil {
|
||||
return nil, errors.New("notification zrpc service returned empty test response")
|
||||
}
|
||||
channel, err := channelFromResponse(resp.Channel)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &contracts.TestResult{
|
||||
Channel: *channel,
|
||||
Status: resp.Status,
|
||||
Outcome: resp.Outcome,
|
||||
Message: resp.Message,
|
||||
TraceID: resp.TraceId,
|
||||
SentAt: timeFromUnixNano(resp.SentAtUnixNano),
|
||||
Skipped: resp.Skipped,
|
||||
Provider: resp.Provider,
|
||||
}, 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
|
||||
}
|
||||
|
||||
func timeFromUnixNano(value int64) time.Time {
|
||||
if value <= 0 {
|
||||
return time.Time{}
|
||||
}
|
||||
return time.Unix(0, value)
|
||||
}
|
||||
151
backend/client/notification/errors.go
Normal file
151
backend/client/notification/errors.go
Normal file
@@ -0,0 +1,151 @@
|
||||
package notification
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/LoveLosita/smartflow/backend/shared/respond"
|
||||
"google.golang.org/genproto/googleapis/rpc/errdetails"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
)
|
||||
|
||||
// responseFromRPCError 负责把 notification 的 gRPC 错误反解回项目内的 respond.Response。
|
||||
//
|
||||
// 职责边界:
|
||||
// 1. 只在 gateway 边缘层使用,不下沉到服务实现里;
|
||||
// 2. 业务错误尽量恢复成 respond.Response,方便 API 层继续复用现有 DealWithError;
|
||||
// 3. 只要拿不到业务语义,就退化成普通 error,让上层按 500 处理。
|
||||
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 = "notification zrpc service internal error"
|
||||
}
|
||||
return wrapRPCError(errors.New(msg))
|
||||
}
|
||||
|
||||
msg := strings.TrimSpace(st.Message())
|
||||
if msg == "" {
|
||||
msg = "notification 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
|
||||
}
|
||||
|
||||
if resp, ok := responseFromStatusDetails(st); ok {
|
||||
return resp, true
|
||||
}
|
||||
if resp, ok := responseFromLegacyStatus(st.Code(), st.Message()); ok {
|
||||
return resp, true
|
||||
}
|
||||
return respond.Response{}, false
|
||||
}
|
||||
|
||||
func responseFromStatusDetails(st *status.Status) (respond.Response, bool) {
|
||||
for _, detail := range st.Details() {
|
||||
info, ok := detail.(*errdetails.ErrorInfo)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
|
||||
statusValue := strings.TrimSpace(info.Reason)
|
||||
if statusValue == "" {
|
||||
statusValue = grpcCodeToRespondStatus(st.Code())
|
||||
}
|
||||
if statusValue == "" {
|
||||
return respond.Response{}, false
|
||||
}
|
||||
|
||||
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 responseFromLegacyStatus(code codes.Code, message string) (respond.Response, bool) {
|
||||
trimmed := strings.TrimSpace(message)
|
||||
if resp, ok := respondResponseByMessage(trimmed); ok {
|
||||
return resp, true
|
||||
}
|
||||
|
||||
switch code {
|
||||
case codes.Unauthenticated:
|
||||
if trimmed == "" {
|
||||
trimmed = "unauthorized"
|
||||
}
|
||||
return respond.Response{Status: respond.ErrUnauthorized.Status, Info: trimmed}, true
|
||||
case codes.InvalidArgument:
|
||||
if trimmed == "" {
|
||||
trimmed = "invalid argument"
|
||||
}
|
||||
return respond.Response{Status: respond.MissingParam.Status, Info: trimmed}, true
|
||||
case codes.Internal, codes.Unknown, codes.DataLoss:
|
||||
if trimmed == "" {
|
||||
trimmed = "notification service internal error"
|
||||
}
|
||||
return respond.InternalError(errors.New(trimmed)), true
|
||||
}
|
||||
|
||||
return respond.Response{}, false
|
||||
}
|
||||
|
||||
func respondResponseByMessage(message string) (respond.Response, bool) {
|
||||
switch strings.TrimSpace(message) {
|
||||
case respond.MissingParam.Info:
|
||||
return respond.MissingParam, true
|
||||
case respond.WrongParamType.Info:
|
||||
return respond.WrongParamType, true
|
||||
case respond.ErrUnauthorized.Info:
|
||||
return respond.ErrUnauthorized, 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.Internal, codes.Unknown, codes.DataLoss:
|
||||
return "500"
|
||||
default:
|
||||
return "400"
|
||||
}
|
||||
}
|
||||
|
||||
func wrapRPCError(err error) error {
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf("调用 notification zrpc 服务失败: %w", err)
|
||||
}
|
||||
Reference in New Issue
Block a user