@theokit/gateway 0.10.0 → 0.12.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +200 -0
- package/README.md +178 -1
- package/dist/index.cjs +369 -76
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +434 -78
- package/dist/index.d.ts +434 -78
- package/dist/index.js +368 -77
- package/dist/index.js.map +1 -1
- package/package.json +2 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,205 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.12.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- 05e7fdd: Edit a message an adapter sent, so a reply can stream as a preview that the final text replaces.
|
|
8
|
+
|
|
9
|
+
`BasePlatformAdapter` gains `supportsEdit` (`false` by default) and
|
|
10
|
+
`editMessage(messageId, out)`, whose default refuses with `edit_unsupported`, makes no platform
|
|
11
|
+
call and never throws. A third-party subclass that already declares a member with either name and
|
|
12
|
+
a different type no longer compiles against this release, and under `noImplicitOverride` one
|
|
13
|
+
that declares it without `override` does not either.
|
|
14
|
+
|
|
15
|
+
Telegram (`editMessageText`), Discord (one REST `PATCH` of the message, without fetching the
|
|
16
|
+
channel), Slack (`chat.update`), Mattermost (`patchPost`), Matrix (an `m.replace` event) and Teams
|
|
17
|
+
(`updateActivity`, or `activities(conversationId).update` on `@microsoft/teams.api` below 2.0.14)
|
|
18
|
+
set `supportsEdit` to `true` and replace the text of a message they sent, honouring `format` as
|
|
19
|
+
`sendMessage` does (Slack's `chat.update` takes no `mrkdwn` argument, so its edit sends the text
|
|
20
|
+
as it is for every format), and return `{ ok: true, messageId }` with the same id. E-mail, SMS, LINE and
|
|
21
|
+
WhatsApp keep the default.
|
|
22
|
+
|
|
23
|
+
An edit is refused with no request as `edit_too_long` when the text would need more than one
|
|
24
|
+
platform message (Matrix has no length check), as `invalid_message_id` for an empty id (and on
|
|
25
|
+
Telegram one that is not a safe positive integer), and as `empty_text` and `not_connected` where
|
|
26
|
+
`sendMessage` answers those. Telegram answers its "message is not modified" refusal as a success.
|
|
27
|
+
On Discord an unknown channel is answered by the platform, as `discord_10003`. On Matrix a
|
|
28
|
+
`#alias` the adapter never resolved costs one lookup before the edit. Teams and Matrix do not
|
|
29
|
+
retry a refused markup as plain text on an edit, as they do on a send.
|
|
30
|
+
|
|
31
|
+
The six adapters keep `>=0.9.0` as their peer range on `@theokit/gateway` in this release. At run
|
|
32
|
+
time they work against any core in that range, since no adapter calls the base `editMessage`. Their
|
|
33
|
+
sources declare both members with `override`, so building them against a core older than this one
|
|
34
|
+
reports TS4113. The floor rises to this core in the next release, once it is on npm
|
|
35
|
+
(`docs/adr/0008-edit-capable-adapters-raise-their-core-floor-after-the-core-ships.md`). A
|
|
36
|
+
consumer's own typecheck reads the published declarations, which carry no `override`, and passes
|
|
37
|
+
on an older core.
|
|
38
|
+
|
|
39
|
+
- e646038: Make `google_chat` a first-party platform.
|
|
40
|
+
|
|
41
|
+
`@theokit/gateway` gains the `google_chat` key in `PlatformEventRegistry`, the
|
|
42
|
+
`GoogleChatMessageEvent` type, and a `channelRendering("google_chat")` row. `PlatformName` grows by
|
|
43
|
+
one member. Code that lists every platform in a `Record<PlatformName, …>` or in a switch with a
|
|
44
|
+
`never` default must add a `google_chat` entry, and a package that registered its own `google_chat`
|
|
45
|
+
key through module augmentation now collides with the first-party one (TS2717) and should drop it.
|
|
46
|
+
|
|
47
|
+
- e646038: Make `imessage` a first-party platform.
|
|
48
|
+
|
|
49
|
+
`@theokit/gateway` gains the `imessage` key in `PlatformEventRegistry`, the `IMessageMessageEvent`
|
|
50
|
+
type, and a `channelRendering("imessage")` row (`plain`, markers removed). `PlatformName` grows by
|
|
51
|
+
one member. Code that lists every platform in a `Record<PlatformName, …>` or in a switch with a
|
|
52
|
+
`never` default must add an `imessage` entry, and a package that registered its own `imessage` key
|
|
53
|
+
through module augmentation now collides with the first-party one (TS2717) and should drop it.
|
|
54
|
+
|
|
55
|
+
- e646038: Make `irc` a first-party platform.
|
|
56
|
+
|
|
57
|
+
`@theokit/gateway` gains the `irc` key in `PlatformEventRegistry`, the `IrcMessageEvent` type,
|
|
58
|
+
and a `channelRendering("irc")` row (`plain`, markers removed). `PlatformName` grows by one
|
|
59
|
+
member. Code that lists every platform in a `Record<PlatformName, …>` or in a switch with a
|
|
60
|
+
`never` default must add an `irc` entry, and a package that registered its own `irc` key through
|
|
61
|
+
module augmentation now collides with the first-party one (TS2717) and should drop it.
|
|
62
|
+
|
|
63
|
+
- e646038: Make `nextcloud_talk` a first-party platform.
|
|
64
|
+
|
|
65
|
+
`@theokit/gateway` gains the `nextcloud_talk` key in `PlatformEventRegistry`, the
|
|
66
|
+
`NextcloudTalkMessageEvent` type, and a `channelRendering("nextcloud_talk")` row (`markdown`,
|
|
67
|
+
passed through). `PlatformName` grows by one member. Code that lists every platform in a
|
|
68
|
+
`Record<PlatformName, …>` or in a switch with a `never` default must add a `nextcloud_talk`
|
|
69
|
+
entry, and a package that registered its own `nextcloud_talk` key through module augmentation now
|
|
70
|
+
collides with the first-party one (TS2717) and should drop it.
|
|
71
|
+
|
|
72
|
+
- e646038: Make `signal` a first-party platform.
|
|
73
|
+
|
|
74
|
+
`@theokit/gateway` gains the `signal` key in `PlatformEventRegistry`, the `SignalMessageEvent`
|
|
75
|
+
type, and a `channelRendering("signal")` row (`markdown`, text unchanged). `PlatformName` grows by
|
|
76
|
+
one member. Code that lists every platform in a `Record<PlatformName, …>` or in a switch with a
|
|
77
|
+
`never` default must add a `signal` entry, and a package that registered its own `signal` key
|
|
78
|
+
through module augmentation now collides with the first-party one (TS2717) and should drop it.
|
|
79
|
+
|
|
80
|
+
- b0b29d3: `rawBodyCapture(tag, { maxBytes })` refuses a body larger than `maxBytes` bytes before holding it.
|
|
81
|
+
A declared `content-length` over the limit is refused before any byte is kept; a streamed body is
|
|
82
|
+
refused once it passes the limit. Either way the rest of the stream is dropped, and when it ends
|
|
83
|
+
the middleware answers 413 with the JSON body `{ "error": "request body too large" }` and one
|
|
84
|
+
stderr line naming the router. `next` is not called, so no error reaches Express's final handler,
|
|
85
|
+
which would answer with an HTML page carrying the stack outside production. The middleware
|
|
86
|
+
answers once: data, the end of the stream or a stream error after a refusal do nothing. A
|
|
87
|
+
`maxBytes` that is not a finite number of zero or more throws a `RangeError` when the middleware is
|
|
88
|
+
built. Without the option the middleware keeps no limit, as before. `RawBodyCaptureOptions` is
|
|
89
|
+
exported.
|
|
90
|
+
|
|
91
|
+
### Patch Changes
|
|
92
|
+
|
|
93
|
+
- d3f359a: `createChannels` builds one adapter per platform. In 0.11.0 it built every entry, so two entries
|
|
94
|
+
for one platform gave a `GatewayRunner` two adapters of that platform: the runner sent the first
|
|
95
|
+
one's replies through the second and never disconnected the first. A later entry for a platform
|
|
96
|
+
it already built is now rejected with a `GatewayConfigurationError` coded `duplicate_channel`, and
|
|
97
|
+
its `create` is not called. If your app lists two entries for one platform, the second one now
|
|
98
|
+
lands in `rejected` instead of being built. The first entry built for a platform wins, and an
|
|
99
|
+
entry rejected for its configuration does not claim its platform.
|
|
100
|
+
|
|
101
|
+
An entry whose `create` returns an adapter reporting another platform than its descriptor declares
|
|
102
|
+
is rejected with the code `platform_mismatch`, and the adapter it built is dropped unconnected.
|
|
103
|
+
The runner keys adapters by the adapter's own platform, so such an adapter would otherwise reach
|
|
104
|
+
it as a second adapter for a platform the duplicate check never saw.
|
|
105
|
+
|
|
106
|
+
- 7bea3d4: Two overlapping `start()` calls on one `GatewayRunner` now share one connect. A call made while
|
|
107
|
+
the first is still connecting no longer connects every adapter again; both calls resolve the same
|
|
108
|
+
`StartResult`, or reject with the same error. A start that rejects is not remembered, so a later
|
|
109
|
+
`start()` connects again.
|
|
110
|
+
|
|
111
|
+
A `stop()` during an in-flight `start()` no longer leaves an adapter connected on a stopped runner.
|
|
112
|
+
The start disconnects what connected, wires no inbound dispatch, and resolves
|
|
113
|
+
`{ connected: [], failed }` instead of naming the platforms it connected. Under `"rollback"`, a
|
|
114
|
+
connect that fails still rejects it.
|
|
115
|
+
|
|
116
|
+
Under the default `connectFailure: "rollback"`, the rollback waits for every connect to settle, so
|
|
117
|
+
an adapter that connects after another refused is disconnected as well. The rejection therefore
|
|
118
|
+
comes only once the slowest connect settles. With one adapter refusing and another whose connect
|
|
119
|
+
hangs, `start()` used to reject at once and now stays pending, as a successful rollback start and
|
|
120
|
+
an `"isolate"` start already did.
|
|
121
|
+
|
|
122
|
+
When `stop()`, a rollback, or a start that `stop()` overtook disconnects an adapter and that
|
|
123
|
+
`disconnect()` fails, the runner now writes one line to stderr naming the platform, with any
|
|
124
|
+
credential in the error redacted. The failure used to be discarded, so an adapter that might still
|
|
125
|
+
hold its platform session left no trace. How `start()` and `stop()` resolve or reject is unchanged.
|
|
126
|
+
|
|
127
|
+
- 9a8b81b: `listenerLifecycle(...).start()` rejects with the bind error when the port is taken, instead of
|
|
128
|
+
resolving over no listener. Under Express 5 the error reached the `listen` callback, which ignored
|
|
129
|
+
it, so a second server on a port another one held reported itself started and answered nothing;
|
|
130
|
+
under Express 4 the error was emitted on the server with no listener attached. The
|
|
131
|
+
`createWebhookServer` of `@theokit/gateway-line` and `@theokit/gateway-sms` returns this lifecycle,
|
|
132
|
+
so their `start()` now rejects with `EADDRINUSE` in that case, and a later `start()` tries again.
|
|
133
|
+
`ListenableApp.listen` declares the error argument Express 5 passes, and `ListenerLike` the
|
|
134
|
+
optional `once("error")` Express 4's server has.
|
|
135
|
+
|
|
136
|
+
## 0.11.0
|
|
137
|
+
|
|
138
|
+
### Minor Changes
|
|
139
|
+
|
|
140
|
+
- 39d06f8: Wire channels from descriptors, and keep the channels that connect when another cannot.
|
|
141
|
+
|
|
142
|
+
`@theokit/gateway` exports `createChannels` and the types `ChannelDescriptor`,
|
|
143
|
+
`ChannelConfigField`, `ChannelWebhook`, `WebhookRequest`, `IngestResult`, `ChannelEntry`,
|
|
144
|
+
`ChannelRejection` and `CreateChannelsResult`. `createChannels(entries)` builds an adapter per
|
|
145
|
+
entry and returns `{ adapters, rejected }`: an entry whose required field is absent, `null` or
|
|
146
|
+
`""` is rejected with a `GatewayConfigurationError` coded `missing_required_config` naming the
|
|
147
|
+
keys, and a configuration error thrown by the descriptor's `create` is rejected the same way. Any
|
|
148
|
+
other error propagates. It connects nothing and makes no network call.
|
|
149
|
+
|
|
150
|
+
`GatewayRunner` accepts `connectFailure: "rollback" | "isolate"` (exported as `ConnectFailure`),
|
|
151
|
+
and `StartResult` is exported.
|
|
152
|
+
The default, `"rollback"`, is unchanged: one failed connect disconnects the adapters that did
|
|
153
|
+
connect and rejects `start()`. Under `"isolate"`, `start()` connects every adapter, wires inbound
|
|
154
|
+
only for the ones that connected, and reports the rest with a redacted reason.
|
|
155
|
+
|
|
156
|
+
`start()` now resolves a `StartResult` (`{ connected, failed }`) instead of `void`, under both
|
|
157
|
+
values. This is a type change: code that awaits `start()` and ignores the value is unaffected,
|
|
158
|
+
but a structural type declaring `start(): Promise<void>` that a `GatewayRunner` is assigned to
|
|
159
|
+
fails with TS2322, and a subclass overriding `start()` with that signature fails with TS2416.
|
|
160
|
+
|
|
161
|
+
`@theokit/gateway-telegram` exports `telegramChannel`, a socket channel whose config has a
|
|
162
|
+
required, secret `token` and an optional `allowedUsers`, and a `ConfigurationError` (a
|
|
163
|
+
`GatewayConfigurationError` with the `gateway-telegram` prefix). Called directly,
|
|
164
|
+
`telegramChannel.create` refuses a token that is not a string or is empty, and an `allowedUsers`
|
|
165
|
+
that is not an array of strings; through `createChannels`, an absent or empty token is rejected
|
|
166
|
+
first, as `missing_required_config`.
|
|
167
|
+
|
|
168
|
+
`@theokit/gateway-line` exports `lineChannel`, a webhook channel whose `webhook.verify` checks the
|
|
169
|
+
`x-line-signature` header against the raw body and whose `webhook.ingest` dispatches a verified
|
|
170
|
+
payload through `dispatchWebhookBody`, so a reply uses the cached reply token, or answers
|
|
171
|
+
`"malformed"` without dispatching. It answers `"no_handler"` (a member of `IngestResult`) when the
|
|
172
|
+
payload holds a text message for the handler and none is subscribed: before the runner started,
|
|
173
|
+
after it stopped, or when LINE's connect failed under `"isolate"`. Before, such a request answered
|
|
174
|
+
`"delivered"`, a route acknowledged it, and LINE did not redeliver the lost message.
|
|
175
|
+
`lineChannel.create` checks field types before the adapter's constructor.
|
|
176
|
+
|
|
177
|
+
`LineAdapter.dispatchWebhookBody` now resolves `"ok" | "no_handler"` instead of `void`. It answers
|
|
178
|
+
`"no_handler"`, dispatching nothing and caching no reply token, for a batch holding a text message
|
|
179
|
+
that passes the mention guard while no handler is subscribed; an empty or follow-only batch still
|
|
180
|
+
answers `"ok"`. It reads the handler once per batch, so a handler that unsubscribes while handling
|
|
181
|
+
one event still receives the rest of the batch. Code that ignores the value is unaffected; a
|
|
182
|
+
subclass overriding the method with `Promise<void>` fails with TS2416. `createWebhookServer` is
|
|
183
|
+
unchanged and still answers 200 before it dispatches.
|
|
184
|
+
|
|
185
|
+
Both descriptors are typed locally and import nothing `@theokit/gateway` 0.9.0 lacks, so the peer
|
|
186
|
+
range stays `>=0.9.0 <1.0.0`.
|
|
187
|
+
|
|
188
|
+
- e13abe8: Pair each platform with the rendering `ChatPresenter` needs.
|
|
189
|
+
|
|
190
|
+
`@theokit/gateway` exports `channelRendering(platform)` and the type `ChannelRendering`, a
|
|
191
|
+
`{ translate, format }` pair to spread into `@theokit/presenter`'s `ChatPresenter` options.
|
|
192
|
+
whatsapp and slack get single-character emphasis (`*bold*`, `_italic_`, `~strike~`), discord,
|
|
193
|
+
mattermost and teams keep markdown, and telegram, matrix, email, line and sms get the four inline
|
|
194
|
+
markers removed with `format: "plain"`. Before this, the presenter's defaults put `**bold**` on
|
|
195
|
+
the wire as characters on matrix and email, and handed telegram and slack a bold spelled for
|
|
196
|
+
another dialect.
|
|
197
|
+
|
|
198
|
+
Only bold, italic, strikethrough and inline code are translated. Headings, links, lists and block
|
|
199
|
+
quotes pass through, and fenced code blocks are not handled yet: the inline-code rule rewrites
|
|
200
|
+
their backticks. A platform the table does not know throws a `RangeError`. `toDialect` is
|
|
201
|
+
unchanged, and `@theokit/gateway` still does not depend on `@theokit/presenter`.
|
|
202
|
+
|
|
3
203
|
## 0.10.0
|
|
4
204
|
|
|
5
205
|
### Minor Changes
|
package/README.md
CHANGED
|
@@ -31,13 +31,123 @@ to read ten packages to learn which — so the classes are stated here.
|
|
|
31
31
|
|
|
32
32
|
**What none of them does is convert markdown to a platform's dialect.** `format` states the
|
|
33
33
|
caller's INTENT and lets the transport act on it; translating `**bold**` into WhatsApp's
|
|
34
|
-
`*bold*` is a presentation concern and is
|
|
34
|
+
`*bold*` is a presentation concern, and `channelRendering` (below) is where it happens.
|
|
35
35
|
|
|
36
36
|
Measured 2026-08-30: before this, two of ten read the field. An agent answered
|
|
37
37
|
`**Bom Sucesso (MG)**` and LINE and WhatsApp delivered literal asterisks, which is what a
|
|
38
38
|
declared-and-ignored field costs at the far end.
|
|
39
39
|
|
|
40
40
|
|
|
41
|
+
## Sending an agent's reply
|
|
42
|
+
|
|
43
|
+
`@theokit/presenter`'s `ChatPresenter` turns one agent turn into one chat message: it keeps the
|
|
44
|
+
answer's text, drops the model's reasoning and the tool events, and emits once, on `finish`.
|
|
45
|
+
`channelRendering(platform)` gives it the two things it needs to know about the destination, as
|
|
46
|
+
`{ translate, format }`.
|
|
47
|
+
|
|
48
|
+
```typescript
|
|
49
|
+
import { channelRendering, type MessageEvent } from "@theokit/gateway";
|
|
50
|
+
import { type AgentOutputEvent, ChatPresenter } from "@theokit/presenter";
|
|
51
|
+
import type { TelegramAdapter } from "@theokit/gateway-telegram";
|
|
52
|
+
|
|
53
|
+
async function reply(
|
|
54
|
+
adapter: TelegramAdapter,
|
|
55
|
+
event: MessageEvent,
|
|
56
|
+
turn: AsyncIterable<AgentOutputEvent>,
|
|
57
|
+
): Promise<void> {
|
|
58
|
+
const presenter = new ChatPresenter({
|
|
59
|
+
channel: event.channel,
|
|
60
|
+
...channelRendering(event.platform),
|
|
61
|
+
});
|
|
62
|
+
for await (const output of turn) {
|
|
63
|
+
for (const message of presenter.present(output)) await adapter.sendMessage(message);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
A hand-written loop that concatenates every event carrying `text` sends the reasoning too: one
|
|
69
|
+
reply reached a phone as `"TheOi! 👋"`, `"The"` being the start of the model's thinking. Build one
|
|
70
|
+
presenter per turn; it keeps state between `present()` calls.
|
|
71
|
+
|
|
72
|
+
| Platform | `translate` | `format` |
|
|
73
|
+
|---|---|---|
|
|
74
|
+
| whatsapp | `**b**` to `*b*`, `*i*` or `_i_` to `_i_`, `~~s~~` to `~s~`, `` `c` `` to ` ```c``` ` | `plain` |
|
|
75
|
+
| slack | `**b**` to `*b*`, `*i*` or `_i_` to `_i_`, `~~s~~` to `~s~`, `` `c` `` kept | `markdown` |
|
|
76
|
+
| discord, mattermost, teams | unchanged | `markdown` |
|
|
77
|
+
| telegram, matrix, email, line, sms | the markers removed, their content kept | `plain` |
|
|
78
|
+
|
|
79
|
+
- Only the four inline markers are translated: bold, italic, strikethrough and inline code.
|
|
80
|
+
Headings, links, lists and block quotes reach every platform unchanged.
|
|
81
|
+
- **Fenced code blocks are not handled yet, and they come out wrong.** The inline-code rule
|
|
82
|
+
rewrites a fence's backticks: a three-backtick fence becomes one backtick on the plain
|
|
83
|
+
platforms and seven on whatsapp. Do not send a fenced block to a platform that does not render
|
|
84
|
+
markdown.
|
|
85
|
+
- Telegram gets plain text on purpose. Its adapter maps `markdown` to legacy Markdown, which
|
|
86
|
+
refuses a reply with an unpaired `_` or `*`, and the send fails with no retry.
|
|
87
|
+
- These renderings are inferred from each platform's documented syntax and from what each adapter
|
|
88
|
+
does with `format`; none was observed on a real client.
|
|
89
|
+
- The presenter never splits. Each adapter's `sendMessage` splits a long message at its own
|
|
90
|
+
platform's limit, as it always has.
|
|
91
|
+
- `channelRendering` throws a `RangeError` for a platform it does not know, rather than letting
|
|
92
|
+
the presenter fall back to markdown.
|
|
93
|
+
- `@theokit/presenter` is your dependency, not this package's: `@theokit/gateway` does not depend
|
|
94
|
+
on it.
|
|
95
|
+
|
|
96
|
+
## Editing a sent message
|
|
97
|
+
|
|
98
|
+
Six adapters can replace the text of a message they sent, keeping its id: Telegram, Discord,
|
|
99
|
+
Slack, Mattermost, Matrix and Teams. E-mail, SMS, LINE and WhatsApp cannot, because their
|
|
100
|
+
platforms have no edit call. A caller that wants a reply to appear as a preview and then become
|
|
101
|
+
the final text reads `supportsEdit` before it starts, and falls back to `sendMessage` whenever an
|
|
102
|
+
edit is refused. `BasePlatformAdapter`, `OutboundMessage` and `SendResult` come from
|
|
103
|
+
`@theokit/gateway`.
|
|
104
|
+
|
|
105
|
+
```typescript
|
|
106
|
+
async function sendWithPreview(
|
|
107
|
+
adapter: BasePlatformAdapter,
|
|
108
|
+
out: OutboundMessage,
|
|
109
|
+
preview: string,
|
|
110
|
+
): Promise<SendResult> {
|
|
111
|
+
// No edit call on this platform: send the final text once.
|
|
112
|
+
if (!adapter.supportsEdit) return adapter.sendMessage(out);
|
|
113
|
+
const sent = await adapter.sendMessage({ ...out, text: preview });
|
|
114
|
+
if (!sent.ok || sent.messageId === undefined) return adapter.sendMessage(out);
|
|
115
|
+
// The preview must fit one message; edit_too_long and every other refusal fall back to a send.
|
|
116
|
+
const edited = await adapter.editMessage(sent.messageId, out);
|
|
117
|
+
return edited.ok ? edited : adapter.sendMessage(out);
|
|
118
|
+
}
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
`supportsEdit` is set at construction and does not change with `connect()`. `editMessage` never
|
|
122
|
+
throws on a platform error; it answers with a `SendResult`, and these refusals make no edit
|
|
123
|
+
request:
|
|
124
|
+
|
|
125
|
+
| Code | When |
|
|
126
|
+
|---|---|
|
|
127
|
+
| `edit_unsupported` | the adapter cannot edit (the four above), or a Matrix client injected without `sendMessage` |
|
|
128
|
+
| `edit_too_long` | the text would need more than one platform message, counted after a `plain` text is escaped as a send escapes it; an edit replaces one message and is never split. Matrix has no length check |
|
|
129
|
+
| `invalid_message_id` | the id is empty, or on Telegram not a positive integer |
|
|
130
|
+
| `empty_text` | the text is empty, or on Teams only whitespace |
|
|
131
|
+
| `not_connected` | the adapter is not connected, where its `sendMessage` checks that too (all but Telegram and Discord) |
|
|
132
|
+
| `invalid_channel` | Telegram only: the channel id is not numeric, as on a send |
|
|
133
|
+
| `encrypted_room_unsupported` | Matrix only: the room is encrypted, as on a send |
|
|
134
|
+
|
|
135
|
+
Any other refusal comes from the platform, mapped as `sendMessage` maps it. Keep the preview
|
|
136
|
+
within one message, and edit only a message this adapter sent: on Matrix an edit is an
|
|
137
|
+
`m.replace` event, and clients apply it only to an event from the same sender. An edit whose
|
|
138
|
+
markup the platform refuses is not retried as plain text. When an edit is refused, the sample
|
|
139
|
+
sends the final text as a new message, so the preview stays above it.
|
|
140
|
+
|
|
141
|
+
`supportsEdit` says that the adapter has an edit call, not that every edit is accepted. Treat
|
|
142
|
+
`edit_unsupported` from an adapter that reports `true` as "stop previewing", like any other
|
|
143
|
+
refusal. Where an adapter checks the connection, `not_connected` is answered before
|
|
144
|
+
`invalid_message_id`.
|
|
145
|
+
|
|
146
|
+
The adapters do not order edits. Two edits of the same message that are in flight at once can
|
|
147
|
+
reach the platform in either order, and the one that lands last stays on screen; on Matrix each
|
|
148
|
+
edit is a separate event and clients show the newest. Await each edit before sending the next,
|
|
149
|
+
or keep one in flight and send only the latest text when it settles.
|
|
150
|
+
|
|
41
151
|
## Architecture (5 pieces)
|
|
42
152
|
|
|
43
153
|
| Module | Responsibility |
|
|
@@ -75,6 +185,73 @@ const runner = new GatewayRunner({
|
|
|
75
185
|
await runner.start();
|
|
76
186
|
```
|
|
77
187
|
|
|
188
|
+
## Wiring channels from descriptors
|
|
189
|
+
|
|
190
|
+
Each platform package that has one exports a descriptor: a static object listing the channel's
|
|
191
|
+
config fields (`required`, `secret`), how to build its adapter (`create`), and, for a channel that
|
|
192
|
+
receives webhooks, how to verify and ingest a request (`webhook`). `createChannels` builds every
|
|
193
|
+
channel from its descriptor without connecting anything, and `connectFailure: "isolate"` keeps the
|
|
194
|
+
channels that connect when another one cannot.
|
|
195
|
+
|
|
196
|
+
```typescript
|
|
197
|
+
import { createChannels, GatewayRunner } from "@theokit/gateway";
|
|
198
|
+
import { lineChannel } from "@theokit/gateway-line";
|
|
199
|
+
import { telegramChannel } from "@theokit/gateway-telegram";
|
|
200
|
+
|
|
201
|
+
const { adapters, rejected } = createChannels([
|
|
202
|
+
{ descriptor: telegramChannel, config: { token: process.env.TELEGRAM_BOT_TOKEN } },
|
|
203
|
+
{
|
|
204
|
+
descriptor: lineChannel,
|
|
205
|
+
config: {
|
|
206
|
+
channelSecret: process.env.LINE_CHANNEL_SECRET,
|
|
207
|
+
channelAccessToken: process.env.LINE_CHANNEL_ACCESS_TOKEN,
|
|
208
|
+
},
|
|
209
|
+
},
|
|
210
|
+
]);
|
|
211
|
+
for (const { platform, error } of rejected) console.error(`${platform}: ${error.message}`);
|
|
212
|
+
|
|
213
|
+
const runner = new GatewayRunner({
|
|
214
|
+
adapters,
|
|
215
|
+
connectFailure: "isolate",
|
|
216
|
+
handler: async (event, ctx) => {
|
|
217
|
+
await ctx.reply(`got: ${event.text}`);
|
|
218
|
+
},
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
const { connected, failed } = await runner.start();
|
|
222
|
+
if (connected.length === 0) throw new Error("no channel connected");
|
|
223
|
+
for (const { platform, reason } of failed) console.error(`${platform} did not connect: ${reason}`);
|
|
224
|
+
```
|
|
225
|
+
|
|
226
|
+
- `createChannels` rejects an entry whose required field is absent, `null` or `""` with a
|
|
227
|
+
`GatewayConfigurationError` coded `missing_required_config` that names the keys, and reports a
|
|
228
|
+
configuration error thrown by `create` the same way. Any other error propagates. It performs no
|
|
229
|
+
network call.
|
|
230
|
+
- `connectFailure` defaults to `"rollback"`: one failed connect disconnects the adapters that did
|
|
231
|
+
connect and rejects `start()`, as before. Under `"isolate"`, `start()` resolves even when every
|
|
232
|
+
adapter failed, so check `connected` before you rely on the runner.
|
|
233
|
+
- `start()` resolves a `StartResult` (`{ connected, failed }`) under both values. Code that awaits
|
|
234
|
+
it and ignores the value is unaffected; a type or subclass that declares
|
|
235
|
+
`start(): Promise<void>` needs to change.
|
|
236
|
+
- A runner holds one channel per platform: it routes replies and `stop()` by platform.
|
|
237
|
+
`createChannels` enforces this. The first entry built for a platform wins, and a later entry for
|
|
238
|
+
it is rejected with a `GatewayConfigurationError` coded `duplicate_channel` and never built. An
|
|
239
|
+
entry whose `create` returns an adapter reporting another platform than its descriptor is
|
|
240
|
+
rejected with the code `platform_mismatch`, and that adapter is dropped unconnected. A
|
|
241
|
+
runner you build from adapters you constructed yourself is not checked, so give it one adapter
|
|
242
|
+
per platform: with two of one platform the later one sends the earlier one's replies, and the
|
|
243
|
+
earlier one is connected but never disconnected.
|
|
244
|
+
- Under `"isolate"`, and under `"rollback"` until the first failure, `start()` waits for every
|
|
245
|
+
connect, so a connect that never settles blocks it.
|
|
246
|
+
- A webhook channel's request reaches your HTTP route; route it by platform to the descriptor's
|
|
247
|
+
`webhook.verify(adapter, { rawBody, headers })`, then parse the body and pass it to
|
|
248
|
+
`webhook.ingest(adapter, payload)`, which answers `"delivered"`, `"malformed"` or
|
|
249
|
+
`"no_handler"`. Answer the platform with a success status only for `"delivered"`.
|
|
250
|
+
`"no_handler"` means the payload held a message and no handler was subscribed: a request that
|
|
251
|
+
arrives before `start()` resolves, after `stop()`, or for a channel `start()` reported in
|
|
252
|
+
`failed` under `"isolate"`. Acknowledging it loses the message, since the platform does not
|
|
253
|
+
redeliver an acknowledged request.
|
|
254
|
+
|
|
78
255
|
## Receiving from a TheoKit app
|
|
79
256
|
|
|
80
257
|
Three repositories share this path. `theokit` owns the HTTP route and the signature check —
|