Skip to content

First-class multi-tenant runtime for per-connection bots #563

@mrloldev

Description

@mrloldev

Problem Statement

The Chat SDK assumes one bot identity and one credential set per process. In a SaaS product, each customer connects their own Telegram bot, SMS number, or WhatsApp sender. We need many isolated bot runtimes on shared webhook routes without running one process per connection.

Proposed Solution

Add a built-in multi-tenant mode (or official helper) that:

  1. Resolves a connection from platform + connectionKey on each webhook
  2. Lazily creates a Chat instance per connection with its own adapter credentials
  3. Prefixes shared state keys so thread/subscription state does not collide
  4. Exposes connection context in message handlers (e.g. via AsyncLocalStorage)
  5. Caches runtimes with LRU eviction

Handlers would receive (ctx, thread, message) where ctx.connection identifies the tenant.

Alternatives Considered

  • One global Chat and swap credentials per request: not safe for concurrent webhooks
  • One Chat singleton per connection: too much memory at scale
  • External wrapper package: works today, but belongs in the SDK long term

We ship option 3 as @delphi-ai/multi-tenant-chat until upstream support exists.

Use Case

Priority

Critical

Contribution

  • I am willing to help implement this feature

Additional Context

We already built and use this pattern. Reference implementation: https://github.com/delphi-ai/multi-tenant-chat.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions