ClaireDocs

Ask Claire

Search the project, not the web.

Answers are grounded in Claire’s published documentation and include the source pages used.

⌘/Ctrl + J opens Ask Claire anywhere in docs.

Matrix bridge integration plan

Original implementation plan for routing Claire messaging platforms through Matrix bridges.

ArchivedReviewed 2026-08-17View source ↗

Overview#

Add Matrix bridge support alongside existing direct adapters. A PLATFORM_MODE environment variable switches between:

  • `direct` (current) - Native platform libraries (whatsapp-web.js, telegraf, etc.)
  • `matrix` (new) - Matrix bridges via Synapse homeserver (mautrix-*)

Both modes use the same IPlatformAdapter interface - the client/API doesn't know the difference.

Architecture#

text
┌─────────────────────────────────────────────────────────────────────┐
│                         Claire Backend                               │
│                      PlatformManager (existing)                      │
│                              │                                       │
│              ┌───────────────┴───────────────┐                      │
│              ▼                               ▼                       │
│     PLATFORM_MODE=direct            PLATFORM_MODE=matrix             │
│              │                               │                       │
│    ┌─────────┴─────────┐            ┌───────┴───────┐               │
│    │  Direct Adapters  │            │ MatrixAdapter │               │
│    │  (existing code)  │            │   (new code)  │               │
│    └─────────┬─────────┘            └───────┬───────┘               │
│              │                               │                       │
│    Platform APIs                    Matrix Homeserver                │
│    (whatsapp-web.js,               (Synapse + mautrix               │
│     telegraf, etc.)                  bridges in Docker)              │
└─────────────────────────────────────────────────────────────────────┘

Why Matrix Bridges?#

AspectDirect AdaptersMatrix Bridges
ReliabilityUntestedBattle-tested by Beeper
MaintenanceWe maintainCommunity maintained
Edge casesWe handleAlready handled
Setupnpm installDocker + config
Resource usageLight~2-4GB RAM

Files to Create#

text
server/src/adapters/matrix/
├── index.ts                    # MatrixBridgeAdapter class
├── types.ts                    # Matrix-specific types
├── client.ts                   # matrix-js-sdk wrapper
├── room-mapper.ts              # Matrix rooms ↔ platform chats
├── user-mapper.ts              # Ghost users ↔ platform contacts
├── event-converter.ts          # Matrix events → UnifiedMessage
└── bridge-auth/
    ├── index.ts                # Auth flow coordinator
    ├── whatsapp.ts             # QR code flow via bridge bot
    ├── telegram.ts             # Phone login flow
    └── instagram.ts            # Cookie auth flow

docker/matrix/
├── docker-compose.matrix.yml   # Synapse + all bridges
├── synapse/
│   └── homeserver.yaml.template
└── bridges/
    ├── whatsapp/config.yaml.template
    ├── telegram/config.yaml.template
    └── instagram/config.yaml.template

supabase/migrations/
└── 20260206_add_matrix_mappings.sql

Files to Modify#

FileChanges
server/src/config/index.tsAdd PLATFORM_MODE, Matrix env vars
server/src/adapters/index.tsAdd setMatrixMode() to PlatformManager
server/src/index.tsConditional adapter initialization
server/package.jsonAdd matrix-js-sdk dependency

New Dependencies#

json
{
  "matrix-js-sdk": "^34.0.0"
}

Implementation Phases#

Phase 1: Configuration & Types (~1 hour)#

  1. Add to server/src/config/index.ts: ``typescript PLATFORM_MODE: z.enum(['direct', 'matrix']).default('direct'), MATRIX_HOMESERVER_URL: z.string().url().optional(), MATRIX_SERVER_NAME: z.string().optional(), MATRIX_ADMIN_TOKEN: z.string().optional(), ``
  2. Create server/src/adapters/matrix/types.ts with Matrix-specific interfaces

Phase 2: MatrixBridgeAdapter Core (~3 hours)#

  1. Create server/src/adapters/matrix/index.ts:
  2. Extends BasePlatformAdapter
  3. Connects to Synapse via matrix-js-sdk
  4. Handles RoomEvent.Timeline for incoming messages
  5. Manages control rooms for bridge bot commands
  6. Key methods:
  7. initialize() - Connect to homeserver, start sync
  8. createSession() - Create control room with bridge bot, send login command
  9. sendMessage() - Find Matrix room, send via SDK
  10. getChats() - List rooms with bridge ghost users

Phase 3: Room & User Mappers (~2 hours)#

  1. Create room-mapper.ts:
  2. Maps platform:chatId ↔ matrixRoomId
  3. Identifies rooms by ghost user presence (e.g., @_wa_12345:server.com)
  4. Create user-mapper.ts:
  5. Converts ghost users to platform contacts
  6. Ghost patterns: @_wa_*, @_telegram_*, @_instagram_*
  7. Create event-converter.ts:
  8. Converts Matrix m.room.message events to UnifiedMessage

Phase 4: Bridge Auth Flows (~2 hours)#

  1. Create bridge-auth/whatsapp.ts:
  2. Send login to bridge bot
  3. Parse QR code from m.image response
  4. Emit qr_code event for client
  5. Create bridge-auth/telegram.ts:
  6. Send login +phone to bridge bot
  7. Handle verification code prompt
  8. Create bridge-auth/instagram.ts:
  9. Send login-cookie <cookies> to bridge bot

Phase 5: Docker Infrastructure (~2 hours)#

  1. Create docker/matrix/docker-compose.matrix.yml: ``yaml services: synapse: image: matrixdotorg/synapse:latest postgres-synapse: image: postgres:15-alpine mautrix-whatsapp: image: dock.mau.dev/mautrix/whatsapp:latest mautrix-telegram: image: dock.mau.dev/mautrix/telegram:latest mautrix-instagram: image: dock.mau.dev/mautrix/meta:latest ``
  2. Create bridge config templates with environment variable substitution
  3. Create scripts/init-bridges.sh for registration file generation

Phase 6: Mode Switching & Testing (~2 hours)#

  1. Update server/src/index.ts: ``typescript if (matrixConfig.enabled) { const matrixAdapter = new MatrixBridgeAdapter(config); platformManager.setMatrixMode(matrixAdapter); } else { // existing direct adapter registration } ``
  2. Update PlatformManager.getAdapter() for matrix mode
  3. Write integration tests with mock Matrix server

Database Migration#

sql
-- Matrix room mappings for bridge mode
CREATE TABLE public.matrix_room_mappings (
    id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
    user_id UUID NOT NULL REFERENCES public.users(id),
    session_id TEXT NOT NULL,
    platform platform_type NOT NULL,
    matrix_room_id TEXT NOT NULL,
    platform_chat_id TEXT NOT NULL,
    is_control_room BOOLEAN DEFAULT FALSE,
    UNIQUE(matrix_room_id)
);

Environment Variables#

bash
# Platform Mode
PLATFORM_MODE=direct   # or 'matrix'

# Matrix Configuration (required when PLATFORM_MODE=matrix)
MATRIX_HOMESERVER_URL=http://localhost:8008
MATRIX_SERVER_NAME=claire.local
MATRIX_ADMIN_TOKEN=replace_with_admin_token

# Telegram API (required for mautrix-telegram)
TELEGRAM_API_ID=12345
TELEGRAM_API_HASH=replace_with_telegram_api_hash

Platform Auth Flows (Matrix Mode)#

PlatformBridge BotAuth CommandUser Action
WhatsApp@whatsappbotloginScan QR with phone
Telegram@telegrambotlogin +phoneEnter SMS code
Instagram@instagrambotlogin-cookie <c>Extract browser cookies
iMessageN/AN/ANot recommended via Matrix

iMessage Note#

iMessage via mautrix-imessage is NOT recommended for server deployment:

  • Requires local macOS machine with SIP disabled
  • Cannot run in Docker
  • Keep using direct iMessage adapter instead

Verification#

  1. Start Matrix stack: docker compose -f docker/matrix/docker-compose.matrix.yml up -d
  2. Set env: PLATFORM_MODE=matrix
  3. Start server: bun run dev
  4. Test WhatsApp:
  5. POST /platforms/whatsapp/connect → Get QR code
  6. Scan with phone
  7. Send message → Verify received in Claire
  8. Test Telegram:
  9. POST /platforms/telegram/connect with phone number
  10. Enter verification code
  11. Message bot → Verify received
  12. Run tests: bun test src/adapters/matrix

Critical Files#

  • server/src/adapters/types.ts - IPlatformAdapter interface to implement
  • server/src/adapters/base-adapter.ts - Base class to extend
  • server/src/adapters/index.ts - PlatformManager to modify
  • server/src/config/index.ts - Config schema to extend
  • server/src/index.ts - Server startup to modify

Estimated Time#

PhaseTime
Phase 1: Config & Types1 hour
Phase 2: MatrixBridgeAdapter3 hours
Phase 3: Mappers2 hours
Phase 4: Auth Flows2 hours
Phase 5: Docker2 hours
Phase 6: Testing2 hours
Total~12 hours

Rollback Plan#

If Matrix mode has issues:

  1. Set PLATFORM_MODE=direct
  2. Restart server
  3. Direct adapters resume immediately
  4. No data loss - sessions stored separately by mode