core/plugin/wasmguest/caps/subscriptions.go
Alex Dunmow bec3a43f55 feat(wasmguest): guest capability stubs over host calls (WO-WZ-003)
Every CoreServices interface (core/plugin/deps.go) now has a guest-side stub
that marshals to the WO-WZ-001 capability messages and dispatches through the
generic host_call transport, so plugin service code compiles and runs
unchanged against content.Content, settings.Settings, plugin.PluginBridge, etc.

- core/plugin/wasmguest/caps/: one file per family (17 families, 38 methods),
  a var _ <iface> = (*stub)(nil) compile proof each, and NewCoreServices(call)
  assembling them. The transport is injected (CallFunc) so marshaling is
  natively testable; the wasm shim binds it to CallHost, DESCRIBE probes pass
  nil (capability calls fail cleanly instead of nil-panicking).
- Error mapping wraps AbiError with <family>.<method> context and maps
  DEADLINE_EXCEEDED onto context.DeadlineExceeded.
- RAGService.RegisterContentFetcher stays guest-side (RAGStub) for
  HOOK_RAG_FETCH dispatch; Query/OnContentChanged marshal out. dispatch.go and
  describe.go now source fetchers from the caps RAG stub.
- caps_roundtrip_test.go: fake transport + 76 deterministic golden payloads
  (family_method_{req,resp}.pb) covering 100% of families, plus error-mapping,
  deadline, nil-transport, and guest-side-fetcher tests. WO-WZ-006 replays the
  same goldens to prevent host/guest drift.
- Acceptance: testdata/fixture Load hook calls deps.Content/Settings/Bridge
  unchanged (compiles for wasip1); caps_wasmhost_test.go drives it end-to-end
  through a real wazero module + fake host_call table.
- Disposition table in docs/wasm-abi.md: every member stub | host-side
  (Pool, Interceptors, AppURL/MediaPath, CoreServiceBindings host-side).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-03 14:23:58 +08:00

92 lines
2.8 KiB
Go

package caps
import (
"context"
abiv1 "git.dev.alexdunmow.com/block/core/abi/v1"
"git.dev.alexdunmow.com/block/core/subscriptions"
"github.com/google/uuid"
)
// subscriptionsStub implements subscriptions.Subscriptions over
// subscriptions.* capability calls.
type subscriptionsStub struct{ base }
var _ subscriptions.Subscriptions = (*subscriptionsStub)(nil)
func (s *subscriptionsStub) GetUserTierLevel(ctx context.Context, userID uuid.UUID) (*subscriptions.TierLevel, error) {
resp := &abiv1.SubscriptionsGetUserTierLevelResponse{}
req := &abiv1.SubscriptionsGetUserTierLevelRequest{UserId: userID.String()}
if err := s.invoke(ctx, "get_user_tier_level", req, resp); err != nil {
return nil, err
}
tl := resp.GetTierLevel()
if tl == nil {
return nil, nil
}
return &subscriptions.TierLevel{Level: int(tl.GetLevel()), Features: tl.GetFeatures()}, nil
}
func (s *subscriptionsStub) GetTierBySlug(ctx context.Context, slug string) (*subscriptions.Tier, error) {
resp := &abiv1.SubscriptionsGetTierBySlugResponse{}
req := &abiv1.SubscriptionsGetTierBySlugRequest{Slug: slug}
if err := s.invoke(ctx, "get_tier_by_slug", req, resp); err != nil {
return nil, err
}
t := resp.GetTier()
if t == nil {
return nil, nil
}
tier := tierFromProto(t)
return &tier, nil
}
func (s *subscriptionsStub) ListTiers(ctx context.Context) ([]subscriptions.Tier, error) {
resp := &abiv1.SubscriptionsListTiersResponse{}
if err := s.invoke(ctx, "list_tiers", &abiv1.SubscriptionsListTiersRequest{}, resp); err != nil {
return nil, err
}
tiers := make([]subscriptions.Tier, 0, len(resp.GetTiers()))
for _, t := range resp.GetTiers() {
tiers = append(tiers, tierFromProto(t))
}
return tiers, nil
}
func (s *subscriptionsStub) ListActivePlans(ctx context.Context, tierID uuid.UUID) ([]subscriptions.Plan, error) {
resp := &abiv1.SubscriptionsListActivePlansResponse{}
req := &abiv1.SubscriptionsListActivePlansRequest{TierId: tierID.String()}
if err := s.invoke(ctx, "list_active_plans", req, resp); err != nil {
return nil, err
}
plans := make([]subscriptions.Plan, 0, len(resp.GetPlans()))
for _, p := range resp.GetPlans() {
plan := subscriptions.Plan{
ID: parseUUID(p.GetId()),
TierID: parseUUID(p.GetTierId()),
BillingInterval: p.GetBillingInterval(),
Amount: p.GetAmount(),
Currency: p.GetCurrency(),
IsActive: p.GetIsActive(),
}
if ts := p.GetCreatedAt(); ts != nil {
plan.CreatedAt = ts.AsTime()
}
plans = append(plans, plan)
}
return plans, nil
}
func tierFromProto(t *abiv1.Tier) subscriptions.Tier {
return subscriptions.Tier{
ID: parseUUID(t.GetId()),
Name: t.GetName(),
Slug: t.GetSlug(),
Level: int(t.GetLevel()),
Description: t.GetDescription(),
Features: t.GetFeatures(),
IsDefault: t.GetIsDefault(),
Position: int(t.GetPosition()),
}
}