|
| 1 | +# Named Messaging Adapters via Bindings |
| 2 | + |
| 3 | +## Context |
| 4 | + |
| 5 | +Messaging adapters are currently singleton per platform per instance (one Discord bot token, one Telegram bot token, one Slack app pair). Bindings route traffic by platform + location filters (guild/workspace/chat/channel), but they do not select which credential instance handles that traffic. |
| 6 | + |
| 7 | +This blocks multi-bot scenarios on a single instance, such as: |
| 8 | + |
| 9 | +- Two Telegram bots routing to different agents |
| 10 | +- Two Discord bots with different server memberships and identities |
| 11 | +- A default shared bot plus one specialized bot for a specific agent |
| 12 | + |
| 13 | +The existing binding abstraction is still the right place to express routing. The missing piece is adapter instance selection. |
| 14 | + |
| 15 | +## Proposal |
| 16 | + |
| 17 | +Add support for multiple named adapter instances per platform, while keeping the existing nameless token fields as the default instance for backward compatibility. |
| 18 | + |
| 19 | +- Existing config shape remains valid |
| 20 | +- Named instances are optional and additive |
| 21 | +- Bindings gain an optional `adapter` field |
| 22 | +- Binding resolution chooses both agent and adapter instance |
| 23 | + |
| 24 | +This keeps the simple path simple (paste one token) and unlocks advanced routing when needed. |
| 25 | + |
| 26 | +## Goals |
| 27 | + |
| 28 | +- Support multiple credential instances per messaging platform |
| 29 | +- Keep old configs and API payloads working unchanged |
| 30 | +- Keep bindings as the single routing abstraction |
| 31 | +- Preserve first-match binding behavior |
| 32 | +- Avoid introducing a separate per-agent messaging config system |
| 33 | + |
| 34 | +## Non-Goals |
| 35 | + |
| 36 | +- No backward-compat shim for legacy behavior beyond config/API compatibility |
| 37 | +- No cross-platform global adapter namespace |
| 38 | +- No automatic migration that rewrites user config files |
| 39 | + |
| 40 | +## Config Shape |
| 41 | + |
| 42 | +### Telegram example |
| 43 | + |
| 44 | +```toml |
| 45 | +[messaging.telegram] |
| 46 | +enabled = true |
| 47 | +token = "env:TELEGRAM_BOT_TOKEN" # default instance (legacy path) |
| 48 | + |
| 49 | +[[messaging.telegram.instances]] |
| 50 | +name = "support" |
| 51 | +enabled = true |
| 52 | +token = "env:TELEGRAM_BOT_TOKEN_SUPPORT" |
| 53 | + |
| 54 | +[[messaging.telegram.instances]] |
| 55 | +name = "sales" |
| 56 | +enabled = true |
| 57 | +token = "env:TELEGRAM_BOT_TOKEN_SALES" |
| 58 | + |
| 59 | +[[bindings]] |
| 60 | +agent_id = "main" |
| 61 | +channel = "telegram" |
| 62 | +chat_id = "-100111111111" |
| 63 | +# adapter omitted => default instance |
| 64 | + |
| 65 | +[[bindings]] |
| 66 | +agent_id = "support-agent" |
| 67 | +channel = "telegram" |
| 68 | +chat_id = "-100222222222" |
| 69 | +adapter = "support" |
| 70 | +``` |
| 71 | + |
| 72 | +### Binding semantics |
| 73 | + |
| 74 | +- `adapter` is platform-scoped (same name can exist under Telegram and Discord) |
| 75 | +- `adapter` omitted means default instance for that platform |
| 76 | +- If no matching binding exists, message still routes to default agent (current behavior) |
| 77 | + |
| 78 | +## Data Model Changes |
| 79 | + |
| 80 | +### `Binding` |
| 81 | + |
| 82 | +Add: |
| 83 | + |
| 84 | +- `adapter: Option<String>` |
| 85 | + |
| 86 | +### Messaging config structs |
| 87 | + |
| 88 | +For each platform with token-based auth (Discord, Telegram, Slack, Twitch): |
| 89 | + |
| 90 | +- Keep existing singleton fields (`token`, `bot_token`, `app_token`, etc.) |
| 91 | +- Add optional `instances: Vec<...InstanceConfig>` with: |
| 92 | + - `name: String` |
| 93 | + - `enabled: bool` |
| 94 | + - platform credential fields |
| 95 | + - optional platform-specific extras if needed later |
| 96 | + |
| 97 | +## Runtime Model |
| 98 | + |
| 99 | +## Adapter identity |
| 100 | + |
| 101 | +Each running adapter gets a stable runtime key: |
| 102 | + |
| 103 | +- Default instance: `<platform>` (example: `telegram`) |
| 104 | +- Named instance: `<platform>:<name>` (example: `telegram:support`) |
| 105 | + |
| 106 | +`MessagingManager` stores adapters by runtime key instead of platform name alone. |
| 107 | + |
| 108 | +## Inbound routing |
| 109 | + |
| 110 | +When an adapter emits an inbound message: |
| 111 | + |
| 112 | +- Keep `message.source = <platform>` for existing platform semantics |
| 113 | +- Add `adapter` metadata (or equivalent field) carrying runtime adapter key |
| 114 | + |
| 115 | +Binding resolution matches on: |
| 116 | + |
| 117 | +1. platform (`channel`) |
| 118 | +2. adapter selector (`binding.adapter` vs inbound adapter identity) |
| 119 | +3. existing platform filters (`guild_id`, `workspace_id`, `chat_id`, `channel_ids`, DMs) |
| 120 | + |
| 121 | +Then first-match wins as before. |
| 122 | + |
| 123 | +## Outbound routing |
| 124 | + |
| 125 | +`respond`, `send_status`, and `fetch_history` use the adapter identity captured on inbound so replies stay on the same bot instance. |
| 126 | + |
| 127 | +For proactive sends (`broadcast` and tools), the caller can target runtime adapter key explicitly when required. |
| 128 | + |
| 129 | +## Permissions Model |
| 130 | + |
| 131 | +Permission maps currently aggregate per platform. They become per adapter instance: |
| 132 | + |
| 133 | +- Build permission set by filtering bindings on `(platform, adapter)` |
| 134 | +- Default adapter uses bindings where `adapter` is absent or explicitly set to default selector |
| 135 | +- Named adapter uses bindings with matching `adapter` |
| 136 | + |
| 137 | +This allows independent scope per token instance without changing binding filter fields. |
| 138 | + |
| 139 | +## API Changes |
| 140 | + |
| 141 | +## Bindings API |
| 142 | + |
| 143 | +`POST/PUT/DELETE /bindings` payloads add optional: |
| 144 | + |
| 145 | +- `adapter?: string` |
| 146 | + |
| 147 | +Old payloads remain valid. |
| 148 | + |
| 149 | +## Messaging API |
| 150 | + |
| 151 | +Status/toggle/disconnect move from platform-only targeting to adapter instance targeting: |
| 152 | + |
| 153 | +- Platform + optional adapter name |
| 154 | +- Platform-only continues to refer to default instance for compatibility |
| 155 | + |
| 156 | +Response payloads should include adapter instance identity so UI can render multiple cards per platform. |
| 157 | + |
| 158 | +## UI Changes |
| 159 | + |
| 160 | +- Keep current quick setup flow for default token |
| 161 | +- Add “Add another token” flow that requires a name |
| 162 | +- Settings display becomes list of adapter instances per platform |
| 163 | +- Binding editor adds optional adapter selector (default preselected) |
| 164 | + |
| 165 | +The common single-token path remains one step. |
| 166 | + |
| 167 | +## Validation Rules |
| 168 | + |
| 169 | +- No duplicate instance names within a platform |
| 170 | +- Binding `adapter` must reference an existing configured instance for that platform |
| 171 | +- Reserved names: reject empty names and `default` |
| 172 | +- Runtime key collisions are impossible under platform-scoped names but still validated |
| 173 | + |
| 174 | +Config load should fail fast with clear messages when these constraints are violated. |
| 175 | + |
| 176 | +## Backward Compatibility |
| 177 | + |
| 178 | +- Existing `[messaging.<platform>]` blocks continue to create the default adapter |
| 179 | +- Existing bindings with no `adapter` continue to work unchanged |
| 180 | +- Existing API clients can omit `adapter` |
| 181 | +- Existing docs/examples remain valid; new docs add advanced multi-instance examples |
| 182 | + |
| 183 | +No migration required for existing users. |
| 184 | + |
| 185 | +## Failure Modes and Handling |
| 186 | + |
| 187 | +- Binding references missing adapter: reject config/API mutation |
| 188 | +- Named adapter disabled/disconnected: bindings remain but produce clear routing/health errors |
| 189 | +- Duplicate adapter name: reject config load and UI mutation |
| 190 | +- Default adapter missing while bindings rely on default: validation error |
| 191 | + |
| 192 | +## Ordered Implementation Phases |
| 193 | + |
| 194 | +### Phase 1: Config and binding model |
| 195 | + |
| 196 | +1. Add `adapter` to binding structs and TOML parsing |
| 197 | +2. Add per-platform `instances` config parsing |
| 198 | +3. Add validation rules (names, existence, duplicates) |
| 199 | +4. Update docs for config reference |
| 200 | + |
| 201 | +### Phase 2: Runtime adapter identity |
| 202 | + |
| 203 | +1. Refactor `MessagingManager` keying from platform name to runtime adapter key |
| 204 | +2. Instantiate default + named adapters per platform |
| 205 | +3. Attach adapter identity to inbound messages |
| 206 | +4. Route outbound operations via inbound adapter identity |
| 207 | + |
| 208 | +### Phase 3: Binding resolution and permissions |
| 209 | + |
| 210 | +1. Extend binding match logic with adapter selector |
| 211 | +2. Build per-adapter permission sets from filtered bindings |
| 212 | +3. Ensure hot-reload updates per-adapter permissions correctly |
| 213 | + |
| 214 | +### Phase 4: API and UI |
| 215 | + |
| 216 | +1. Extend bindings API payloads with optional `adapter` |
| 217 | +2. Extend messaging status/toggle/disconnect APIs for adapter instances |
| 218 | +3. Update dashboard settings and binding editor for named instances |
| 219 | +4. Preserve platform-only behavior for default adapter paths |
| 220 | + |
| 221 | +### Phase 5: Test coverage and rollout docs |
| 222 | + |
| 223 | +1. Add config parsing/validation tests for named instances |
| 224 | +2. Add routing tests for adapter-specific bindings |
| 225 | +3. Add API tests for backward compatible payloads |
| 226 | +4. Add setup docs for multi-bot per platform scenarios |
| 227 | + |
| 228 | +## Open Questions |
| 229 | + |
| 230 | +- Should adapter identity be surfaced as a first-class field on `InboundMessage` instead of metadata? |
| 231 | +- For Slack, should named instances support independent app-level settings beyond tokens in this phase? |
| 232 | +- Should proactive broadcast endpoints require explicit adapter for platforms with multiple configured instances, or keep default fallback? |
0 commit comments