|
| 1 | +package syncer |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "crypto/sha256" |
| 6 | + "encoding/hex" |
| 7 | + "encoding/json" |
| 8 | + "fmt" |
| 9 | + |
| 10 | + "github.com/google/uuid" |
| 11 | + |
| 12 | + "github.com/chaitin/MonkeyCode/backend/biz/mcphub/repo" |
| 13 | +) |
| 14 | + |
| 15 | +type upstreamRepo interface { |
| 16 | + Get(ctx context.Context, id uuid.UUID) (*repo.UpstreamConfig, error) |
| 17 | + MarkSyncSuccess(ctx context.Context, id uuid.UUID) error |
| 18 | + MarkSyncFailed(ctx context.Context, id uuid.UUID) error |
| 19 | +} |
| 20 | + |
| 21 | +type toolRepo interface { |
| 22 | + ReplaceByUpstream(ctx context.Context, upstreamID uuid.UUID, rows []repo.UpsertToolInput) error |
| 23 | +} |
| 24 | + |
| 25 | +type registryPublisher interface { |
| 26 | + Publish(ctx context.Context) error |
| 27 | +} |
| 28 | + |
| 29 | +type upstreamClient interface { |
| 30 | + ListTools(ctx context.Context, upstream *repo.UpstreamConfig) ([]repo.UpstreamTool, error) |
| 31 | +} |
| 32 | + |
| 33 | +type Service struct { |
| 34 | + upstreams upstreamRepo |
| 35 | + tools toolRepo |
| 36 | + registry registryPublisher |
| 37 | + client upstreamClient |
| 38 | +} |
| 39 | + |
| 40 | +func NewService(upstreams upstreamRepo, tools toolRepo, registry registryPublisher, client upstreamClient) *Service { |
| 41 | + return &Service{ |
| 42 | + upstreams: upstreams, |
| 43 | + tools: tools, |
| 44 | + registry: registry, |
| 45 | + client: client, |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +func (s *Service) Sync(ctx context.Context, upstreamID uuid.UUID) error { |
| 50 | + upstream, err := s.upstreams.Get(ctx, upstreamID) |
| 51 | + if err != nil { |
| 52 | + return err |
| 53 | + } |
| 54 | + |
| 55 | + tools, err := s.client.ListTools(ctx, upstream) |
| 56 | + if err != nil { |
| 57 | + _ = s.upstreams.MarkSyncFailed(ctx, upstreamID) |
| 58 | + return err |
| 59 | + } |
| 60 | + |
| 61 | + rows := make([]repo.UpsertToolInput, 0, len(tools)) |
| 62 | + for _, tool := range tools { |
| 63 | + rows = append(rows, repo.UpsertToolInput{ |
| 64 | + UpstreamID: upstreamID, |
| 65 | + Name: tool.Name, |
| 66 | + NamespacedName: NamespacedName(upstream.Slug, tool.Name), |
| 67 | + Scope: upstream.Scope, |
| 68 | + UserID: upstream.UserID, |
| 69 | + TeamID: upstream.TeamID, |
| 70 | + Description: tool.Description, |
| 71 | + InputSchema: tool.InputSchema, |
| 72 | + VersionHash: hashSchema(tool.InputSchema), |
| 73 | + Price: 0, |
| 74 | + }) |
| 75 | + } |
| 76 | + |
| 77 | + if err := s.tools.ReplaceByUpstream(ctx, upstreamID, rows); err != nil { |
| 78 | + _ = s.upstreams.MarkSyncFailed(ctx, upstreamID) |
| 79 | + return err |
| 80 | + } |
| 81 | + if err := s.registry.Publish(ctx); err != nil { |
| 82 | + _ = s.upstreams.MarkSyncFailed(ctx, upstreamID) |
| 83 | + return err |
| 84 | + } |
| 85 | + if err := s.upstreams.MarkSyncSuccess(ctx, upstreamID); err != nil { |
| 86 | + return err |
| 87 | + } |
| 88 | + return nil |
| 89 | +} |
| 90 | + |
| 91 | +func hashSchema(raw json.RawMessage) string { |
| 92 | + if len(raw) == 0 { |
| 93 | + raw = json.RawMessage(`{}`) |
| 94 | + } |
| 95 | + sum := sha256.Sum256(raw) |
| 96 | + return hex.EncodeToString(sum[:]) |
| 97 | +} |
| 98 | + |
| 99 | +func NamespacedName(slug, tool string) string { |
| 100 | + return fmt.Sprintf("%s__%s", slug, tool) |
| 101 | +} |
0 commit comments