Skip to main content

Architecture and Core contract

Boundaries​

server/index.mjs activates one service, registers legacy and connection-scoped operator routes and returns an asynchronous cleanup function. server/connections.mjs owns the persisted registry, connection lifecycle and scoped stores. server/telegram.mjs contains Telegram API transport, settings-backed state, routing/admission, polling and reply reconciliation. server/discord.mjs contains Discord Gateway/REST, DM/channel/thread admissions, durable deliveries and bounded guild-channel context. server/room-history.mjs owns the shared, bounded group/topic context store and reply attribution. ui/settings.js implements the three host Settings workspaces (Configuration, Pending admissions and Chat mappings); ui/archive.js implements the historical work/outcome list/detail reader through the existing mod Archive contribution.

Telegram message update
→ chat/topic admission
→ record approved group human text in shared room history
→ transport recipient selection and bounded room/quoted-reply context
→ for supported images, getFile + bounded Telegram download, then freeze private submission
→ Core agents.turns.submit (agent, session, message, idempotency key, optional attachments)
→ saved inbound delivery and turn ID
→ later Core agents.turns.read
→ completed answerText + byte-free attachment descriptors
→ bounded owned attachment reads and all-artifact preflight
→ saved outbound sending record
→ Telegram sendMessage/sendPhoto/sendAnimation/sendDocument (Discord messages + multipart files)
→ record Telegram-confirmed reply chunks in shared room history
→ delivered or unknown

Switchboard does not directly call a model provider or run a parallel tool loop. It submits a real agent turn to Core. Telegram and Discord are implemented behind separate workers with a shared connection registry; ordinary message routing uses the existing external-turn contract and neither transport requires Tutor integration. Telegram and Discord commands additionally require the Core APIs listed below.

Host capabilities​

The service requires context.settings, context.secrets and context.agents. Public state lists agents; approval and mapping edits require an existing enabled agent. External execution requires:

const accepted = await context.agents.turns.submit({
agentId: mapping.agentId,
sessionId: `telegram:${chatId}:${topicId ?? 'root'}`,
displayMessage: text,
message: '[Telegram message from … in …]\n\n' + text,
idempotencyKey: `telegram-update-${updateId}`,
// For an inbound image, optional; never a token-bearing URL:
attachments: [{ name: 'telegram-image.png', type: 'image/png', size: imageBytes.length, encoding: 'data-url', content: `data:image/png;base64,${imageBytes.toString('base64')}` }],
});
const turn = await context.agents.turns.read(accepted.turn.id);

The example shows the migrated Telegram default. New Telegram connections prefix connection ID; Discord uses discord:<connectionId>:<channelId> (Discord IDs are strings) and discord-<connectionId>-message-<messageId> as its Core idempotency key. The legacy Telegram session string is truncated to 128 characters; new connections include their UUID in the session identity. submit returns an accepted turn record; it is not an immediate answer. Reconciliation recognizes succeeded, failed and interrupted as terminal. Successful reply text is taken from turn.result.answerText.

Both transports submit displayMessage separately from the attributed, context-enriched model-facing message; compatible Core must accept that field. It keeps the human-visible session text distinct from transport context.

Inbound image turns require a Core version supporting external-turn attachments and a selected model configured to accept images; the submit/read availability indicator alone does not prove either. Core persists the attachment in its own agent artifact lifecycle and forwards image input to compatible models. Switchboard checks supported image signatures, limits the Telegram download to 20 MiB (within Core's 30,000,000-character data-URL limit), rejects redirects and never persists or sends the token-bearing download URL as model input. Captions and bytes are not public delivery diagnostics.

If submit/read are absent on a new input, valid routed input is recorded as failed with core_external_agent_turns_unavailable. An already frozen, uncertain submission instead remains pending under its original idempotency key until Core is available; its bytes are not discarded and the polling offset does not advance past it. Approval itself does not establish that Core turns are available.

Additional command capabilities​

/stop needs context.agents.turns.cancel. /new needs context.agents.sessions.create; /model needs context.agents.sessions.model, setModel, and context.models.list for choices. /new persists the returned owned session ID and a history boundary on the mapping. Ordinary messaging still uses submit/read; command availability must not be inferred from the Core turns indicator. See Telegram commands and Discord commands.

Multiple bot connections​

Settings connections stores {id, name, transport, botId} records (transport is telegram or discord; absent legacy values mean telegram), never credentials. The original default connection retains its original keys and Core identities in place; new UUID connections prefix every settings/secret key with connection-<id>-. Each runs its own abortable Telegram poller or Discord Gateway worker and serialized delivery queue. Transport is immutable after creation. Registry/configuration mutations are serialized separately; one long poll does not block other bots.

New connections use session telegram:<connectionId>:<chatId>:<topicId-or-root> and idempotency key telegram-<connectionId>-update-<updateId>. Default preserves the old forms shown above. Local route/delivery IDs can match across bots because their stores are isolated. Provider token validation binds a connection to a bot ID, rejects duplicate bots and prevents retained routes/replies moving to a different bot.

Configuration invalidates network work before draining the worker queue and blocks new polling during persistence. Interrupted sends remain ambiguous and are not blindly retried. Already-sent provider work cannot be recalled, and Core turns are not canceled by disabling/removing a connection.

Stored state​

Host store keyContents
Settings configurationTelegram polling or Discord gateway enabled flag and nullable default agent
Settings transport-stateHealth, polling state, last poll time, sanitized error, cached bot identity, and the Telegram polling cursor; Discord gateway resume identity/sequence are process-local
Settings mappingsUp to 250 approved chat/topic or channel/thread → agent mappings; may include an owned session ID and history boundary (per exact chat/topic within a Telegram grant)
Settings pending-admissionsUp to 100 admission requests
Settings archive-history-v1 (mod-wide)Retained terminal delivery metadata, up to 10,000 records / 8,000,000 bytes; separate from replay buffers, survives bot removal, excludes message bodies and artifacts
Settings deliveriesUp to 200 combined inbound/outbound records per connection; Telegram retains unresolved frozen submissions, and Discord also enforces a 32,000,000-byte bound and protects unresolved/unknown records from eviction
Settings servers / observed-guilds (Discord connection-scoped)Up to 250 authorized servers and 250 gateway-observed guilds; observations alone do not grant access
Settings telegram-command-updates (Telegram connection-scoped)Up to 200 completed command update IDs to avoid executing or replying again after an uncertain send
Settings command-interactions (Discord connection-scoped)Up to 200 recent command IDs/statuses; no interaction tokens
Settings telegram-room-history (mod-wide, not connection-prefixed)Up to 100 chat/topic rooms, 50 messages each, 512,000 UTF-8 JSON bytes total; see routing
Settings discord-room-history (mod-wide)Bounded guild/channel history shared across approved Discord bots; thread IDs are separate rooms; DMs connection-isolated
Secret telegram-bot-token or discord-bot-token (connection-scoped)Provider bot credential; never part of public state

Telegram mapping IDs are the first 24 hex characters of SHA-256 over telegram:<chatId>:<topicId-or-root>; Discord mapping IDs hash discord:<guildId-or-dm>:<channelId>. Inbound delivery IDs use in:<updateId> (Telegram) or in:<messageId> (Discord); outbound IDs are out:<turnId>.

Mutations and message/reconciliation operations share a serialized promise queue. Configuration changes increment a poll generation; stale batches are checked against generation, enabled state and current token before processing. An AbortController handles service shutdown. Discord additionally uses Gateway sequence/heartbeat/resume, periodic reconciliation and REST message write-ahead state. These mechanisms reduce races; they do not make either provider and Core one transactional system.

Security and reliability boundaries​

The host supplies operator authentication and secret persistence. API responses use Cache-Control: no-store; token presence and sanitized bot identity are exposed, not the token itself. Settings rendering uses text nodes rather than interpreting incoming chat metadata as HTML.

Admission controls the route, not each participant or every requested action. Core remains responsible for agent execution and its applicable tool/approval policies. Keep powerful agents out of broadly shared chats unless that access is intentional.

Persistent Telegram offsets or Discord Gateway reconnection plus Core idempotency and outbound state support restart recovery. Unknown sends are intentionally suppressed rather than retried. Bounded collections and absent replay controls mean this is not an exactly-once, lossless messaging service. See operations before extending delivery behavior.

Shared context boundary​

The registry shares a mod-wide Telegram room-history store between its Telegram workers and a separate mod-wide discord-room-history store between Discord workers. Serialized persistence prevents lost updates across workers. Only Telegram group/supergroup chat+topic identities share Telegram context; Discord shares per guild+channel across approved bots. A thread is a separate channel room and does not inherit its parent history; DMs are connection-isolated. Credentials, polling cursors or gateway sessions, admissions, Core execution sessions and outboxes stay scoped to each connection.

Successfully posted answer chunks enter shared history after provider confirmation, not merely Core success. Failed/unknown sends do not manufacture room text. A crash between provider acknowledgment and history persistence can leave a visible message absent from bounded context; no blind resend is attempted.

Shared room context needs no new Core capability or Tutor integration: existing settings persistence and external-turn submission carry the context. This does not remove the additional Core prerequisites for Discord session commands. Full submissions, including inline image bytes, are frozen in private settings before Core submission and reused if acceptance is uncertain. They are never exposed in public delivery diagnostics. Shared context never invokes a peer agent, so no automatic bot-to-bot wake loop is introduced.

Inherited access​

Telegram approvals can inherit connection assignment (inheritedAgent); group-root access covers topics without merging their sessions or context. Explicit topic mappings win. Discord servers and gateway-owned observed-guilds persist per connection; generated inherited routes remain server-grant-dependent and isolated per channel/thread. Responder inheritance is separate: inheritedAgent (falling back to legacy inherited) follows the connection default; a custom responder does not bypass a generated route's server fence. Independently authorized DMs/legacy rooms remain independent even when using the default responder. Revocation fences server-dependent replies, not independent room grants. Operator Settings separates bot identity/credential/default assignment (Configuration), pending requests (Pending admissions) and approved access (Chat mappings), with a shared bot selector; Archive uses the existing mod navigation and native list/detail layout for historical work/outcomes. A separate bounded ledger survives bot deletion; replay buffers remain unchanged. See historical Archive.

Outbound image contract​

Compatible Core snapshots turn.result.attachments as byte-free {id,name,mimeType,sizeBytes} descriptors for the exact completed run. Older text receipts may omit the array. Switchboard calls context.agents.turns.readAttachment(turnId, attachmentId) and expects {name,mimeType,sizeBytes,encoding:'base64',content:BASE64} (raw base64, not a data URL). Core owns receipt authorization, exact-run selection and artifact retention. Switchboard never fetches preview URLs or scans session files.

Before any text/image send, all descriptors and retained bytes are validated: at most ten images, 10 MiB each and 20 MiB aggregate, PNG/JPEG/GIF/WebP signatures matching MIME, canonical base64 and matching descriptor sizes/names. The read waiter has a 30-second total deadline; Core's capability itself has no cancellation parameter, so a late response cannot authorize a send. Missing capability/bytes, unsupported artifacts and invalid/oversize results are failed deliveries, not successful text-only fallbacks. Image bytes are held only for delivery, not copied into the outbound ledger or Archive.

Telegram sends text chunks followed by individual binary uploads: PNG/JPEG via sendPhoto, GIF via sendAnimation and WebP via sendDocument. Discord attaches binary files[n] plus payload_json to the first message, with allowed mentions disabled and stable nonce enforcement; later text chunks retain the same channel. Both preserve the originating route/topic and reference the originating message only on the first send. The existing write-ahead sending record covers the entire result. Any partial/ambiguous send remains unknown and is never automatically resent, including after restart.