Overview
We are building a Stateless Delivery Edge on Cloudflare Workers. It acts as a "Universal Adaptor" for sending Emails/SMS via tenant-configured providers.
Phase 1: Infrastructure & Skeleton (Foundation)
Goal: Get a "Hello World" Worker running with database connectivity.
- Initialize Worker:
- Generate
apps/communicationusingpnpm create cloudflare. - Configure
wrangler.toml(Name:logicspike-communication). - Install dependencies:
hono,zod,drizzle-orm,postgres.
- Generate
- Database Setup (D1/Postgres):
- Define Schema in
packages/db/schema/communication.ts. - Tables:
provider_configs,message_logs,message_events. - Run migrations.
- Define Schema in
- Internal Bindings:
- Configure Service Binding in
apps/manager/wrangler.toml. - Verify
managercan callcommunication.
- Configure Service Binding in
Phase 2: Core Domain Logic (The Brain)
Goal: Securely manage provider credentials and route messages.
- Encryption Utility:
- Implement
lib/crypto.ts(AES-256-GCM). - Add
ENCRYPTION_SECRETto Cloudflare Secrets.
- Implement
- Provider Factory:
- Create
domain/providers/factory.ts. - Define
Providerinterface. - Implement
SystemProvider(Console/Mock for dev). - Implement "Resolution Logic" (Tenant Config -> Decrypt -> Instantiate).
- Create
- Types & VALIDATION:
- Define Zod schemas for
Content,ChannelType,ProviderConfig.
- Define Zod schemas for
Phase 3: The Universal API (The Interface)
Goal: Expose the endpoints defined in API Spec.
- Hono Setup:
- Create
src/index.tswith Hono app. - Add Error Handling middleware.
- Create
- Provider Management:
POST /v1/providers: Validate & Encrypt config -> Save to DB.GET /v1/providers: List configs (Masked secrets).
- Sending Logic:
POST /v1/send:- Validate Payload (Zod).
- Check Idempotency (KV/DB).
- Resolve Provider.
- Execute
provider.send(). - Log result to
message_logs.
Phase 4: Provider Adapters (The Plugins)
Goal: Connect to real world services.
- Twilio Adapter:
- Implement
TwilioProviderclass. - Map
Content-> SMS Body.
- Implement
- Resend/SMTP Adapter:
- Implement
EmailProviderclass. - Handle HTML/Text parts.
- Implement
- Failover Logic:
- Implement retry mechanism if Primary fails.
Phase 5: Manager Integration (The Client)
Goal: Replace existing hardcoded logic in Manager Service.
- SDK/Client:
- Create
packages/communication-client(Optional) or just a helper in Manager.
- Create
- Refactor Auth:
- Update
auth/phone.tsto callcommunication.send({ channel: 'sms' }). - Update
auth/email.tsto callcommunication.send({ channel: 'email' }).
- Update
Phase 6: Seller Dashboard (The UI)
Goal: Let Tenants configure their providers.
- Settings Page:
- Create
/app/settings/communicationroute.
- Create
- Forms:
- "Add Provider" Modal (Type select, API Key inputs).
- "Test Connection" button.
- Visualization:
- List active providers.
- Show usage logs (optional MVP).