Open MercatoDocsSearch docsAPI reference

Framework

Communications Hub

An inbound email and a message sent from a business chat service can reach the same internal inbox. Follow an inbound message through normalization and then follow a reply out through the connected account that should send it.

The Communications Hub (communication_channels) bridges external chat and email channels (Slack, WhatsApp, Gmail, IMAP+SMTP) into the Messages module, and routes mobile push through the same ChannelAdapter contract. Each provider ships as a separate workspace package; the hub itself stays adapter-agnostic.

Three message providers ship today, all channelScope: 'user':

Three push providers ship today, all channelType: 'push' and channelScope: 'tenant' – one credential serves every device in the tenant, so they are connected through the admin-gated tenant connect route rather than the per-user one:

Push delivery itself is owned by the push_notifications module; these packages hold only the provider SDKs and their message builders. Push credentials are tenant-scoped (channelScope: 'tenant'), so they are connected through POST /api/communication_channels/channels/connect/tenant-credentials and the per-user connect route refuses them. See Push Notifications and the getting-started tutorial.

For the end-user perspective see the Communication Channels user guide.

Overview

Architecture

              ┌──────────────────────────────────────────────┐
              │  Communications Hub (communication_channels) │
              │                                              │
   ┌──────────┤  ChannelAdapter registry (process-wide)      │──────────┐
   │          │  CommunicationChannel + ExternalConversation │          │
   │          │  ExternalMessage + MessageChannelLink        │          │
   │          │  ChannelThreadMapping + MessageReaction      │          │
   │          └──────────────────────────────────────────────┘          │
   ▼                                                                    ▼
┌──────────┐  ┌──────────┐                                         ┌────────┐
│ channel- │  │ channel- │   …  (future: channel-slack,            │ Messages │
│  imap    │  │  gmail   │       channel-whatsapp, etc.)           │ module   │
│  (basic  │  │  (OAuth) │                                         └────────┘
│   auth)  │  │          │
└──────────┘  └──────────┘

Each provider exports a ChannelAdapter from @open-mercato/core/modules/communication_channels/lib/adapter. The hub's registry validates capability/method consistency at registration time (so an adapter that declares capabilities.deleteMessage: true without implementing deleteMessage() throws on boot).

Per-user channels

Phase 0 of the email integration spec added additive columns to CommunicationChannel:

ColumnPurpose
user_id (uuid, nullable)Owns this channel (NULL = tenant-shared)
is_primary (boolean)True for the user's default outbound channel; enforced by a partial unique index
poll_interval_seconds (int, nullable)Per-channel override of the platform default poll cadence
last_polled_at (timestamptz, nullable)Stamped by the poll worker on every tick
status (text)connected / requires_reauth / disconnected / error
last_error (text, nullable)Classified error string for diagnostics

Plus an additive column on integration_credentials:

ColumnPurpose
user_id (uuid, nullable)Identifies per-user credential rows so tenant-shared and per-user blobs can coexist

Cross-module references use EntityExtension declarations in data/extensions.ts (no direct ORM relationships across module boundaries).

Send path

Compose (Messages)
     │
     ▼
messages.message.sent event
     │
     ▼
subscribers/outbound-bridge.ts  (re-fetches the Message by ID — no payload coupling)
     │
     ▼
deliver-outbound-message command
     │
     ▼
ChannelAdapter.sendMessage   ── refreshCredentials() if access token near expiry
     │
     ▼
ExternalMessage + MessageChannelLink rows written

Receive path

Version availability. The polling mechanism below is present in v0.10.0. The “Automatic sync is off” profile warning and the CRM preset scheduling change described next are development-branch additions after v0.10.0.

Polling needs the scheduler module. The hub registers its poll tick and the Gmail watch renewal from seedDefaults and onOrganizationCreated, and skips both when schedulerService is not available – mail then arrives only on a manual "Poll now", and the profile page "My communication channels" shows an "Automatic sync is off" warning while any connected channel relies on polling and the organization's poll-tick schedule is missing, disabled, or deleted. The crm starter preset enables scheduler; an app that enables it later must run yarn mercato seed:defaults --module communication_channels once so existing organizations get their schedules, and yarn mercato auth sync-role-acls so existing roles get the scheduler features.

@open-mercato/scheduler "poll-tick" (every OM_HUB_POLL_SCHEDULER_TICK_SECONDS, 60 s by default)
     │   enqueues channels whose poll interval is due (OM_HUB_POLL_DEFAULT_SECONDS, 5 minutes by default)
     ▼
poll-channel worker (per channel)
     │
     ▼
ChannelAdapter.fetchHistory   ── reads previous cursor from channelState
     │
     ▼
ingest-inbound-message command
     │
     ▼
ExternalMessage written + Message materialised into the unified inbox
     │
     ▼
communication_channels.message.received event + notification

Mutation guards

Phase 4 of the email integration spec added two hub-side guards (packages/core/src/modules/communication_channels/lib/mutation-guards.ts):

Credential refresh contract

OAuth providers (Gmail) need their tenant-level OAuth client config – clientId and clientSecret – every time a per-user access token is refreshed. The hub resolves this once before delegating to the adapter via the RefreshCredentialsInput.oauthClient field:

export interface RefreshCredentialsInput {
  channelId: string
  /** Per-user credential blob (accessToken, refreshToken, expiresAt, …) */
  credentials: Record<string, unknown>
  scope: TenantScope
  /**
   * Tenant-level OAuth client config resolved by the hub from
   * `integration_credentials.scope = channel_<providerKey>`.
   *
   * - OAuth providers (Gmail): MUST read clientId / clientSecret from this field.
   * - Static-credential providers (IMAP, WhatsApp): ignore.
   */
  oauthClient?: OAuthClientConfig
}

export interface OAuthClientConfig {
  clientId: string
  clientSecret?: string
  /** OAuth directory/tenant id for providers that require one (provider-specific). */
  tenantId?: string
  /** Optional pre-resolved scopes list. */
  scopes?: string[]
}

The hub's refreshCredentialsIfNeeded helper resolves channel_<providerKey> via IntegrationCredentialsService.resolve(...) and passes the result through. Provider adapters that implement OAuth refresh MUST read clientId / clientSecret from input.oauthClient – see packages/channel-gmail/.../lib/adapter.ts:refreshCredentials for the reference implementation.

When the helper cannot resolve the OAuth client (missing channel_<provider> integration credential row, or credentialsService not registered), oauthClient is left undefined. The Gmail adapter then fails refresh with a clear error and the channel flips to status='requires_reauth' so the operator-visible notification fires.

Inbound reliability & threading (Spec B)

Shipped 2026-05-27. See .ai/specs/implemented/2026-05-27-email-integration-inbound-reliability-and-threading.md for the full design rationale.

Layered thread matching

Outbound delivery now injects a per-thread HMAC token (om_<base64url>_<base64url>) into both:

  1. The synthetic <[email protected]> Message-ID appended to the References header (RFC 6761 reserved TLD – guaranteed to round-trip through compliant MTAs).
  2. A hidden <span> (HTML emails) and a bracketed marker (plain-text) at the bottom of the body. Survives mail clients that strip References headers.

Inbound ingestion runs a five-strategy matcher in priority order before falling back to a new thread:

StrategyConfidenceWhen it fires
token-referenceshighReply preserves the om_* Message-ID in References / In-Reply-To. Most common.
token-bodyhighReply quoted our outbound body; the hidden footer survived.
jwz-headersmediumStandard JWZ algorithm against In-Reply-To / References pointing at our outbound Message-Id. Pre-token outbounds.
subject-participantslowNormalized subject match (Re:, Fwd:, [EXTERNAL] stripped) + participant set overlap. Last-ditch.
(none) – New thread.

MessageChannelLink.channelMetadata.threadMatchStrategy + threadMatchConfidence are persisted on every inbound for observability.

Zero-history IMAP bootstrap

A freshly-connected IMAP channel fetches zero historical messages – fetchHistory records UIDVALIDITY + UIDNEXT and returns immediately. From the next tick onward, the worker does UID FETCH previousUidNext:* capped at OM_CHANNEL_IMAP_HARD_CAP_PER_POLL (default 200). When more remain, hasMore: true triggers immediate re-enqueue without waiting for the next scheduler tick. UIDVALIDITY mismatch (mailbox rename / recreate) re-bootstraps rather than re-syncing the whole inbox.

This eliminates the "we can't scan a new user's 1M-message inbox on connect" failure mode by construction. Backlog import is opt-in via the operator-triggered flow below.

Per-message commit + dead-letter

poll-channel.ts advances the channel cursor (channel_state.uidNext) per successfully-ingested message. A transient ingest failure (DB blip, network reset) aborts the loop without advancing – the next tick re-fetches the same UIDs and idempotency on (channel_id, external_message_id) skips already-ingested messages. A permanent failure (malformed MIME, schema violation) writes the raw payload to channel_ingest_dead_letters (encrypted) and advances the cursor anyway so the bad blob never re-stalls the channel.

Auto-recovery sweep

poll-tick.ts enumerates two pools: (a) status='connected' channels due for normal polling, (b) status='error' channels whose lastFailureAt is older than OM_CHANNEL_AUTO_RECOVER_MINUTES. A successful poll in pool (b) flips status back to connected automatically – operators no longer have to manually reconnect after a transient outage.

Operator-triggered backlog import

The POST /api/communication_channels/channels/<id>/import-history route (gated by the communication_channels.channel.import_history ACL feature) creates a ProgressJob and enqueues a channel-import-history worker job (concurrency 1) that calls adapter.importHistory({ sinceDays, contactEmails?, maxMessages, cursor }) in a pagination loop:

FieldRangePurpose
sinceDays1..365Server-side SEARCH SINCE window.
contactEmailsoptional, up to 1000Server-side OR FROM … filter, chunked to ≤30 senders per SEARCH to keep the IMAP tag buffer bounded.
maxMessages1..5000Hard cap across all pages. Worker stops paging as soon as the cap is hit.

A 429 is returned if another import is in-flight for the same channel (active-jobs scan, plus the worker's concurrency: 1 is the second line of defence). Progress streams to the existing ProgressTopBar via ProgressService.updateProgress(...). Adapter contract is additive optional – adapters that don't implement importHistory() get a 400 envelope back from the route (backlog import is IMAP-only; the Gmail adapter does not implement importHistory).

The UI lives on /backend/profile/communication-channels as a per-row "Import history" button → modal dialog (Cmd/Ctrl+Enter submits, Escape cancels). On success the dialog calls router.refresh() instead of window.location.reload() – preserves scroll position and avoids the flash that a full reload causes.

Provider push delivery (Spec C)

Shipped 2026-05-27. See .ai/specs/implemented/2026-05-27-email-integration-inbound-reliability-and-threading.md for the full design rationale.

Spec B established the polling foundation: 60-second cadence for IMAP and Gmail alike. Spec C replaces the 60s polling cadence for Gmail with native push delivery, dropping inbound latency from ≤60s to 5–15s typical while leaving 30-minute polling as a belt-and-suspenders fallback.

Adapter contract (additive)

Three optional methods on ChannelAdapter:

Adapters that don't implement these methods (IMAP, chat providers) stay on Spec B's polling.

Gmail Pub/Sub flow

  1. Operator one-time setup: create a Pub/Sub topic, grant [email protected] publisher, attach a push subscription to the topic pointing at https://<your-host>/api/communication_channels/webhooks/gmail with a service-account identity. Set OM_GMAIL_PUBSUB_TOPIC, OM_GMAIL_PUBSUB_AUDIENCE, OM_GMAIL_PUBSUB_SERVICE_ACCOUNT_EMAIL.
  2. On channel connect (or via the operator "Re-register push" route), the hub calls adapter.registerPush(...), which invokes gmail.users.watch. The returned historyId + watchExpirationMs are persisted to channelState.
  3. Gmail publishes { emailAddress, historyId } to the topic on every mailbox change (rate-capped at 1/s/user).
  4. Pub/Sub POSTs the envelope to the webhook with a Google-signed JWT. lib/gmail-pubsub-jwt.ts verifies the RS256 signature, aud claim, and email claim.
  5. The webhook enqueues a communication-channels-gmail-history-sync job per matching channel (we look up across all tenants by emailAddress and dispatch per-tenant – same emailAddress connected to multiple tenants is supported).
  6. gmail-history-sync worker calls adapter.applyPushNotification(...) (which walks history.list from the stored cursor) and feeds each new message through ingest-inbound-message. Cursor advances per-message commit (Spec B contract).

Renewal cron

Polling fallback

pollIntervalSeconds automatically flips from 60 to 1800 (30 min) when pushStatus='active' is persisted by registerPush. If push fails (e.g. the Gmail Pub/Sub topic is misconfigured) the channel keeps polling at 60s, so no message is ever lost. The pushStatus='failed' channel surfaces in the operator UI with a "Re-register push" button calling POST /api/communication_channels/channels/<id>/push/register.

OAuth setup

Gmail (@open-mercato/channel-gmail)

1. Create a Google Cloud project

  1. Go to Google Cloud Console, create or select a project for your tenant.
  2. Enable the Gmail API under APIs & Services → Library.
  3. Configure the OAuth consent screen:
    • User type: Internal if your tenant is a Workspace org; External if you ship to consumers.
    • Scopes: add https://www.googleapis.com/auth/gmail.modify (or the narrower gmail.send + gmail.readonly combo if you prefer), plus userinfo.email and userinfo.profile.
    • Test users: while in Testing status add every user you intend to onboard.

2. Create the OAuth Client ID

  1. APIs & Services → Credentials → Create credentials → OAuth client ID.
  2. Application type: Web application.
  3. Authorized redirect URIs: add https://<your-host>/api/communication_channels/oauth/gmail/callback for every environment you run (production, staging, local dev).
  4. Copy the Client ID and Client Secret.

3. Register the client in Open Mercato

In Backend → Integrations → Gmail add the client ID and secret. Scopes leave blank for defaults, or override with a comma-separated list matching what you configured in the consent screen.

The credentials are stored encrypted on IntegrationCredentials and resolved at runtime when a user clicks Connect Gmail. Users never see the client secret.

4. Move to In production

When you're ready to onboard more than 100 users, submit the OAuth app for verification. The hub keeps working without verification while you're under that limit and inside Workspace test-user lists.

IMAP + SMTP (@open-mercato/channel-imap)

No tenant setup required. The integrations admin page lets you toggle the provider on or off; individual users provide their own host/port/credentials when they connect.

Environment variables

The hub reads a small set of env vars; most have sensible defaults, but a few must be set explicitly – production-required ones are called out in the rows below.

VariableDefaultPurpose
OM_HUB_OAUTH_STATE_KEYfalls back to KMS_MASTER_KEY, then to JWT_SECRET (non-production only)AES-256-GCM key used to encrypt the OAuth state cookie. Required in production – set this or KMS_MASTER_KEY, or the per-user OAuth connect flow fails closed. Rotate this on a schedule – see runbook below.
OM_HUB_OAUTH_STATE_TTL_SECONDS300 (5 minutes)TTL on the state cookie. OAuth callbacks older than this are rejected.
OM_HUB_POLL_DEFAULT_SECONDS300 (5 minutes)Default poll cadence applied to channels that don't set poll_interval_seconds.
OM_HUB_POLL_CONCURRENCY10Max parallel poll-channel worker jobs per process. Bound by your DB connection pool.
OM_HUB_OUTBOUND_RETRY_MAX5Outbound delivery retries before marking the Message delivery_failed.
OM_HUB_POLL_SCHEDULER_TICK_SECONDS60Interval between poll-tick scheduler runs that enumerate due channels and fan out to per-channel poll jobs. Lower-bounded at 10 s.
OM_CHANNEL_IMAP_HARD_CAP_PER_POLL200Maximum number of IMAP UIDs fetched per fetchHistory page. When more remain, the adapter sets hasMore: true and the hub re-enqueues immediately. Tune lower if a single page's wall-clock budget is too tight; never set above ~500 (DB transaction size).
OM_CHANNEL_AUTO_RECOVER_MINUTES30Spec B § B5 auto-recovery sweep window. poll-tick re-enqueues status='error' channels whose lastFailureAt is older than this. Set 0 in tests to force immediate retry.
OM_THREAD_TOKEN_SECRETderived from KMS_MASTER_KEY via HKDFHMAC key used by Spec B's lib/thread-token.ts to mint per-thread om_* tokens injected into outbound References headers + a hidden body footer. Rotating invalidates all existing tokens (older threads fall back to JWZ + subject-participants strategies – no data loss).
OM_GMAIL_PUBSUB_TOPIC(none – required for Gmail push)Fully-qualified Pub/Sub topic, e.g. projects/openmercato-prod/topics/gmail-inbound. Passed to gmail.users.watch.
OM_GMAIL_PUBSUB_AUDIENCE(none – required for Gmail push)Expected aud claim in the Pub/Sub-signed JWT. Set this to the webhook URL or a stable identifier configured on the Pub/Sub push subscription.
OM_GMAIL_PUBSUB_SERVICE_ACCOUNT_EMAIL(none – required for Gmail push)Expected email claim in the Pub/Sub JWT. Typically the service account configured on the push subscription (NOT [email protected] – that's the publisher into the topic).
OM_PUSH_RENEWAL_GMAIL_LEAD_HOURS24Renewal lead time for gmail.users.watch. Lower means we renew earlier; higher means we cut closer to the 7-day expiration.
OM_IMPORT_HISTORY_MAX_SINCE_DAYS3650 (10 years)Deployment-wide ceiling on the operator-triggered "Import History" sinceDays field. Can only raise the ceiling, never below the 30-day default. Per-provider ceilings (e.g. IMAP's 365) still apply on top of this.
OM_IMPORT_HISTORY_MAX_MESSAGES50000Deployment-wide ceiling on the operator-triggered "Import History" maxMessages field. Can only raise the ceiling, never below the 1000-message default. Per-provider ceilings (e.g. IMAP's 5000) still apply on top of this.

Deploy runbook

Initial deploy

  1. Provision the Postgres schema – yarn db:migrate runs the hub's migration plus the per-user-columns migration (Migration20260526154135_communication_channels).
  2. Sync the ACL grants – yarn mercato auth sync-role-acls so the new communication_channels.* features (including communication_channels.connect_user_channel) are granted to the appropriate roles.
  3. Run the structural cache purge – yarn mercato configs cache structural --all-tenants. Mandatory after any modules.ts change that adds a provider (slices 3e/3f enable channel_imap and channel_gmail).
  4. Configure the OAuth apps – follow the per-provider setup sections above and register the client IDs/secrets via the Integrations admin UI.
  5. Set OM_HUB_OAUTH_STATE_KEY – required in production unless KMS_MASTER_KEY is set; set it explicitly even when KMS_MASTER_KEY is present if you want to rotate the state-cookie key independently of it.

Routine ops

TaskWhenHow
Rotate the OAuth state-cookie keyEvery 90 days (recommended), or after any suspected compromiseSet OM_HUB_OAUTH_STATE_KEY to a new random 32-byte base64 value and roll the deploy. In-flight OAuth flows fail with state expired and the user retries – that's expected and safe.
Bump a tenant's OAuth client secretAfter a provider rotates its credentialsUpdate the secret in Backend → Integrations → <Provider> for the tenant. No restart required; the next refresh-token call picks it up.
Force a poll outside the 5-minute cadenceUser reports "I sent mail and don't see it"Click Refresh on the channel row in the user's profile page, or call POST /api/communication_channels/channels/<id>/test-send for end-to-end.
Purge a disconnected user's channelsWhen a user leaves the orgThe auth.user.deleted cascade subscriber handles it automatically (see Phase 4 implementation status in the email integration spec). Manually: hit the disconnect command for each channel.

Adding a new provider

Tip · Full walkthrough

Building on the Communications Hub covers this end-to-end – the adapter contract, the capability rules, fail-closed webhook verification, the worker pattern for transports that are not webhooks, and tenant-scoped provider state. The steps below are the short version.

The hub deliberately stays adapter-agnostic. To add another email provider (e.g. Yahoo, ProtonMail Bridge variant), follow the same shape as the shipping packages:

  1. mkdir packages/channel-<provider> and copy the build/watch/jest/tsconfig templates from packages/channel-imap.
  2. Implement ChannelAdapter in src/modules/channel_<provider>/lib/adapter.ts.
  3. Register the adapter in src/modules/channel_<provider>/setup.ts via registerChannelAdapter(getAdapter()).
  4. Add { id: 'channel_<provider>', from: '@open-mercato/channel-<provider>' } to apps/mercato/src/modules.ts.
  5. Run yarn generate && yarn build:packages && yarn test, then yarn mercato configs cache structural --all-tenants.

The hub's webhook router, OAuth router, send-as-user route, and admin UI auto-discover the new provider by providerKey; no hub changes required.

Security posture

Integration tests

Each slice ships its own module-local integration specs under __integration__/:

TestLocationWhat it covers
TC-045D-001..006hubHub schema + adapter contract + entity extensions
TC-CHANNEL-EMAIL-HUB-001..003hubPer-user channel API contract + profile page
TC-CHANNEL-EMAIL-001..003channel-imapIMAP provider registration + webhook no-op + profile page
TC-CHANNEL-EMAIL-006..008channel-gmailGmail OAuth router + webhook no-op + profile page
TC-CHANNEL-EMAIL-014..020hubPhase 4 cross-provider scenarios: primary swap, admin RBAC, tenant OAuth override, disconnect, send-as-user guard, user-deleted cascade, rich-content widget
TC-CHANNEL-EMAIL-HUB-007..008hubsend-as-user attachments: bytes delivered to the adapter, caller-written refs dropped, refusals (no file sharing, wrong owner, other organization, type not allowed)

Run with:

yarn test:integration                        # full suite
OM_INTEGRATION_MODULES=communication_channels,channel-imap,channel-gmail \
  yarn test:integration                      # email-foundation slice only

Provider package contract

A ChannelAdapter is the only thing a provider package must expose. The complete contract lives at packages/core/src/modules/communication_channels/lib/adapter.ts. Methods are split into required core, optional extensions, and OAuth helpers – providers only implement the methods that match their declared capabilities (the registry validates consistency on registration).

Required (every adapter)

Recipient shape

capabilities.recipientFormat tells the hub what an outbound recipient looks like for this provider:

ValueHub validationUse for
'email' (default when omitted)Full email-address validationEmail providers – Gmail, IMAP
'provider-native'Length plus transport safety only – the recipient must match the allowlist ^[A-Za-z0-9._:@+-]+$ and must not contain ..Providers addressed by a provider-issued id – e.g. a Discord channel snowflake

A provider that declares 'provider-native' owns the format check itself and MUST treat the recipient as untrusted input – the hub deliberately does not know what a valid id looks like for your transport.

The transport-safety check is an allowlist, not a denylist, because the recipient reaches adapters that interpolate it into a REST path (Discord posts to /channels/{recipient}/messages). Anything outside the permitted set – path separators, query and fragment markers, whitespace, percent-encoded separators such as %2F, and control bytes – is rejected before your adapter sees it. If your transport's identifiers need a character outside that set, widen the allowlist in lib/outbound-recipient.ts deliberately rather than routing around the check.

Optional extensions

See the shipping providers for end-to-end examples:

Previous: Checkout · Next: configs module