@theokit/gateway 0.6.1 → 0.7.1

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 CHANGED
@@ -1,5 +1,67 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.7.1
4
+
5
+ ### Patch Changes
6
+
7
+ - 29e2e28: The seam between the three repositories is documented, and the reason given for one contract was corrected.
8
+
9
+ `@theokit/gateway`'s README — the npm page — now says which repository owns which half: TheoKit the
10
+ HTTP route and the signature check, these packages the translation of the payload it hands over, the
11
+ SDK one redaction helper. A `quality:integration-story` gate fails when that disappears and passes
12
+ when the prose is rewritten around the same facts.
13
+
14
+ Both adapters' published docblocks stated that a TheoKit app's `onMessage` runs AFTER the 200 is
15
+ answered, so a throw there had no status left to change. Measured against `theokit@0.48.14`,
16
+ `handleChannelWebhook` awaits `onMessage` BEFORE building the response and catches nothing around
17
+ it: a throw means the 200 is never built, and mounted in a TheoKit route the rejection reaches that
18
+ route's error boundary and is answered 500 where the platform expected an acknowledgement. `parseInbound` returning `null`
19
+ is unchanged and still the right contract — only the stated reason was wrong, and it was reaching
20
+ users through the `.d.ts` on hover.
21
+
22
+ `@theokit/gateway-sms`'s docblocks also named the wrong seam. Its `parseInbound` is
23
+ `(options, ctx)`, and the `SignatureContext` it needs carries the raw body, the headers and the URL
24
+ — none of which TheoKit's `ChannelMessage` provides, so the signature check cannot be performed
25
+ from inside `onMessage` for any provider. SMS goes through its own `createWebhookServer`, and the
26
+ published `.d.ts` says so now instead of claiming to be the gateway half of the channel seam.
27
+
28
+ ## 0.7.0
29
+
30
+ ### Minor Changes
31
+
32
+ - 74a79b5: A gateway can now be authored, published and consumed outside this repository.
33
+
34
+ `PlatformName` and `MessageEvent` derive from a new exported `PlatformEventRegistry`, an interface
35
+ other packages extend through TypeScript declaration merging:
36
+
37
+ ```ts
38
+ declare module "@theokit/gateway" {
39
+ interface PlatformEventRegistry {
40
+ signal: SignalMessageEvent;
41
+ }
42
+ }
43
+ ```
44
+
45
+ Exhaustive narrowing survives, including over platforms the core has never heard of: forget a
46
+ `case` for a third-party platform and the build breaks, exactly as it does for the ten shipped
47
+ here. A misspelled platform name is still a compile error — the union is opened to extension, not
48
+ to arbitrary strings.
49
+
50
+ Both sides of an augmentation are gated. An entry typed `any`, registered under an index signature,
51
+ carrying a non-literal discriminator, or disagreeing with its own key is excluded rather than
52
+ admitted. Each of those was measured to break narrowing for **every** consumer before the guards
53
+ existed — an index signature made the whole union resolve to `never`, and an optional entry injected
54
+ `undefined` into it. Twelve augmentation shapes are compiled in CI against the published
55
+ declaration to keep it that way.
56
+
57
+ **Behaviour change:** `BaseMessageEvent.platform` is now `string` rather than `PlatformName`. Every
58
+ concrete variant still narrows it to its own literal, so code holding a `MessageEvent` is
59
+ unaffected. Code that assigns `.platform` off a bare `BaseMessageEvent` into a `PlatformName` slot
60
+ will need a narrowing step. This is required by the derivation: `PlatformName` now comes from the
61
+ guarded union, and naming it on the base would make the two reference each other.
62
+
63
+ See `docs/adr/0002-platform-event-registry.md`, which supersedes `docs/adr/0001-message-event-closed-union.md`.
64
+
3
65
  ## 0.6.1
4
66
 
5
67
  ### Patch Changes
package/README.md CHANGED
@@ -50,6 +50,42 @@ const runner = new GatewayRunner({
50
50
  await runner.start();
51
51
  ```
52
52
 
53
+ ## Receiving from a TheoKit app
54
+
55
+ Three repositories share this path. `theokit` owns the HTTP route and the signature check —
56
+ `handleChannelWebhook` verifies the signature, parses the body, and hands your app the parsed JSON
57
+ as `payload: unknown`. These packages own the translation. `theokit-sdk` is used here for one thing
58
+ today: redacting a throw before it reaches a log, at two call sites.
59
+
60
+ ```ts
61
+ import { parseInbound } from "@theokit/gateway-telegram";
62
+
63
+ // Inside onMessage. The signature and the JSON parse already happened.
64
+ const event = parseInbound(message.payload);
65
+ if (event !== null) {
66
+ console.log(`${event.platform} ${event.channel.id}: ${event.text}`);
67
+ }
68
+ ```
69
+
70
+ `parseInbound` returns `null` rather than throwing for a body it does not recognise. Measured
71
+ against `theokit@0.48.14`: `onMessage` is
72
+ awaited **before** the 200 is built, and nothing inside `handleChannelWebhook` catches — so a throw
73
+ on an unparseable payload means the 200 is never built. Mounted in a TheoKit route, the rejection
74
+ reaches that route's error boundary and is answered **500**: the platform sees a failed delivery
75
+ where it expected an acknowledgement.
76
+
77
+ `parseInbound` under that name exists on `@theokit/gateway-telegram` and `@theokit/gateway-sms`,
78
+ with different arguments: SMS takes `(options, ctx)` because the `SignatureContext` it needs carries
79
+ the raw body, the headers and the URL, and `ChannelMessage` provides none of the three — so it is wired through its
80
+ own `createWebhookServer` rather than this seam.
81
+ Every other adapter exports its translation under its own name — `gateway-line` has
82
+ `lineEventToMessageEvent`, `gateway-whatsapp` composes `parseWebhookPayload` with
83
+ `normalizeInboundMessages` — each with its own signature: read the one you are using rather than
84
+ assuming this one's.
85
+
86
+ Adapters whose transport is a long-lived connection rather than a webhook own that transport and do
87
+ not go through this seam.
88
+
53
89
  ## Design principles
54
90
 
55
91
  - **Compose, don't reimplement.** The gateway never owns session persistence (SDK does), never owns scheduling (Cron does), never owns prompt resolution (SystemPromptResolver does).
package/dist/index.d.cts CHANGED
@@ -12,20 +12,31 @@
12
12
  * }
13
13
  * ```
14
14
  *
15
- * This union is intentionally **closed** — adding a platform edits it here
16
- * rather than extending it externally. The trade-off (exhaustive narrowing over
17
- * OCP purity) is recorded in `wiki/decisions/adr-0001-message-event-closed-union.md`.
15
+ * This union is **open to extension and closed to corruption**. It is derived from
16
+ * {@link PlatformEventRegistry}, an interface a package outside this repository extends through
17
+ * declaration merging — so a gateway can be authored, published and consumed without editing this
18
+ * file, while narrowing still holds over platforms declared here and elsewhere alike. Entries that
19
+ * are not well-formed event shapes agreeing with their own key are excluded rather than admitted.
20
+ *
21
+ * It was closed until 2026-08-23. `docs/adr/0001-message-event-closed-union.md` records why, and
22
+ * `docs/adr/0002-platform-event-registry.md` records what changed and what the guard costs.
18
23
  *
19
24
  * @public
20
25
  */
21
- /** Closed enum of supported transport platforms. */
22
- type PlatformName = "telegram" | "discord" | "slack" | "whatsapp" | "teams" | "email" | "sms" | "mattermost" | "line" | "matrix";
23
26
  /** Fields common to every platform's inbound event. */
24
27
  interface BaseMessageEvent {
25
28
  /** Stable id used as a session-key segment and for dedup. */
26
29
  readonly id: string;
27
- /** Discriminator. */
28
- readonly platform: PlatformName;
30
+ /**
31
+ * Discriminator.
32
+ *
33
+ * Typed `string` here and narrowed to a literal by every variant. It cannot be `PlatformName`:
34
+ * that type is derived from the guarded union below, and the union is built out of shapes that
35
+ * extend this interface — naming it here would make the two reference each other (`TS2456`).
36
+ * Widening it is also what lets `PlatformName` be derived from the FILTERED union rather than
37
+ * from `keyof`, so a name and an event can never disagree about which platforms exist.
38
+ */
39
+ readonly platform: string;
29
40
  /** Sender identity (opaque, platform-namespaced). */
30
41
  readonly sender: {
31
42
  readonly id: string;
@@ -219,8 +230,131 @@ interface MatrixMessageEvent extends BaseMessageEvent {
219
230
  readonly raw: unknown;
220
231
  };
221
232
  }
222
- /** Discriminated union of all platform variants. */
223
- type MessageEvent = TelegramMessageEvent | DiscordMessageEvent | SlackMessageEvent | WhatsAppMessageEvent | TeamsMessageEvent | EmailMessageEvent | SMSMessageEvent | MattermostMessageEvent | LineMessageEvent | MatrixMessageEvent;
233
+ /**
234
+ * The open registry every platform's inbound event is filed under.
235
+ *
236
+ * **This is an `interface` on purpose, and it must stay one.** An interface can be extended from
237
+ * another package through declaration merging; a type alias cannot. That single property is what
238
+ * lets a gateway be authored, published and consumed without editing this file — the capability
239
+ * B-008 measured as impossible (`TS2416`, compiled against the published declaration) and ADR-0002
240
+ * decided to unlock. Collapsing it back into a type alias would silently remove that capability
241
+ * while every test in this package still passed.
242
+ *
243
+ * A third-party gateway registers itself from its own package:
244
+ *
245
+ * ```typescript
246
+ * declare module "@theokit/gateway" {
247
+ * interface PlatformEventRegistry {
248
+ * signal: SignalMessageEvent;
249
+ * }
250
+ * }
251
+ * ```
252
+ *
253
+ * @public
254
+ */
255
+ interface PlatformEventRegistry {
256
+ telegram: TelegramMessageEvent;
257
+ discord: DiscordMessageEvent;
258
+ slack: SlackMessageEvent;
259
+ whatsapp: WhatsAppMessageEvent;
260
+ teams: TeamsMessageEvent;
261
+ email: EmailMessageEvent;
262
+ sms: SMSMessageEvent;
263
+ mattermost: MattermostMessageEvent;
264
+ line: LineMessageEvent;
265
+ matrix: MatrixMessageEvent;
266
+ }
267
+ /**
268
+ * `true` only for `any` — the one type that would otherwise swallow the union.
269
+ *
270
+ * `any extends X` distributes to both branches of a conditional, so an ordinary
271
+ * `T extends BaseMessageEvent ? T : never` does NOT keep `any` out. This does: nothing but `any`
272
+ * makes `1 & T` accept `0`.
273
+ */
274
+ type IsAny<T> = 0 extends 1 & T ? true : false;
275
+ /**
276
+ * A registry key must be a literal, never the broad `string` an index signature produces.
277
+ *
278
+ * Measured in review: `interface PlatformEventRegistry { [key: string]: BaseMessageEvent }` is a
279
+ * LEGAL augmentation — every existing variant is assignable to the base — and without this it
280
+ * widens the whole platform name to `string`, so every typo becomes valid and every consumer's
281
+ * `switch` stops narrowing. That is the same collapse an `any` VALUE causes, arriving through the
282
+ * key side, and it is why both sides are gated.
283
+ */
284
+ type LiteralKey<K> = string extends K ? never : K;
285
+ /**
286
+ * The registry's LITERAL keys — index signatures dropped before `keyof` ever sees them.
287
+ *
288
+ * Filtering the value was not enough, measured twice. `keyof` over a registry carrying
289
+ * `{ [key: string]: BaseMessageEvent }` is `string | number`, and indexing a mapped type by that
290
+ * yields `never` for BOTH the union and the platform name: a hostile augmentation did not merely
291
+ * fail to join, it annihilated the union and broke every first-party narrowing site with
292
+ * `TS2339: Property 'telegram' does not exist on type 'never'`.
293
+ *
294
+ * It also fixes a second measured defect for free. Mapping over `keyof` directly is HOMOMORPHIC, so
295
+ * an OPTIONAL entry (`signal?: Event`, a shape an author writes by accident) kept its modifier and
296
+ * injected `undefined` into the union — `TS18048: 'e' is possibly 'undefined'` on a plain `switch`.
297
+ * Mapping over a computed key set is not homomorphic and drops the modifier. A `-?` was written
298
+ * here first as an explicit belt; the gate showed it could not be made to matter while this type is
299
+ * in place, so it was removed rather than kept as decoration nothing can fail without.
300
+ */
301
+ type LiteralKeysOf<R> = keyof {
302
+ [K in keyof R as string extends K ? never : number extends K ? never : symbol extends K ? never : K]: 0;
303
+ };
304
+ /**
305
+ * A registry entry joins {@link MessageEvent} only if it is a real event shape
306
+ * (`docs/adr/0002-platform-event-registry.md`).
307
+ *
308
+ * Measured, not anticipated: without this guard a single entry typed `any` collapses the whole
309
+ * union, and `tsc --strict` then accepts `event.completelyMadeUpField.nested.nonsense` on EVERY
310
+ * consumer — ours included — with no error in any package and nothing red in any suite. The closed
311
+ * union could not fail that way, so the guard is what keeps the new capability from costing a
312
+ * safety property the old design had.
313
+ *
314
+ * A malformed entry is EXCLUDED rather than admitted. It is NOT free for anybody else, and an
315
+ * earlier version of this docblock said it was — measured false twice over. Filtering the VALUE
316
+ * alone left two shapes that break every consumer rather than only their author:
317
+ * `{ [key: string]: … }` made `keyof` yield `string | number`, so the union and the platform name
318
+ * both collapsed to `never`; and `signal?: …` kept its optional modifier through a homomorphic
319
+ * mapping and injected `undefined` into the union. Both are handled above — the first by
320
+ * {@link LiteralKeysOf}, the second by the `-?` on the mapping. What remains true, and is the
321
+ * honest version of the original claim:
322
+ * a `Record<PlatformName, T>` written against the ten still fails to compile, because the rejected
323
+ * key is gone from `PlatformName` while the consumer's literal map is not. What the exclusion buys
324
+ * is that the failure is a missing-key error at the consumer's own map rather than a silent loss
325
+ * of narrowing everywhere.
326
+ *
327
+ * Exported for the contract test in `tests/types/registry-guard.test.ts`, which asserts against
328
+ * this type rather than a copy of it — a copy would stay green with the guard deleted. It is
329
+ * deliberately NOT re-exported from the package barrel; it is an implementation detail of the
330
+ * derivation, not part of the published surface.
331
+ */
332
+ type Registered<K, T> = IsAny<T> extends true ? never : [T] extends [BaseMessageEvent] ? [T] extends [{
333
+ readonly platform: LiteralKey<K>;
334
+ }] ? [LiteralKey<K>] extends [never] ? never : T : never : never;
335
+ /**
336
+ * The canonical inbound event: every guarded entry in {@link PlatformEventRegistry}.
337
+ *
338
+ * Derived rather than written, so third-party platforms are included automatically and exhaustive
339
+ * narrowing still holds over all of them.
340
+ *
341
+ * @public
342
+ */
343
+ type MessageEvent = {
344
+ [K in LiteralKeysOf<PlatformEventRegistry>]: Registered<K, PlatformEventRegistry[K]>;
345
+ }[LiteralKeysOf<PlatformEventRegistry>];
346
+ /**
347
+ * Every platform that actually has a well-formed event in {@link PlatformEventRegistry}.
348
+ *
349
+ * Derived from the GUARDED union rather than from `keyof`, so `PlatformName` and
350
+ * `MessageEvent["platform"]` are the same set by construction. Deriving from `keyof` let a rejected
351
+ * entry keep contributing its key, which produced names no event could ever carry — an adapter
352
+ * could register for a platform that cannot exist.
353
+ *
354
+ * Still a union of string literals, so `switch (event.platform)` narrows and a misspelled name is
355
+ * a compile error.
356
+ */
357
+ type PlatformName = MessageEvent["platform"];
224
358
 
225
359
  /**
226
360
  * `BasePlatformAdapter` — contract every transport adapter implements
@@ -723,4 +857,4 @@ interface ChunkByGraphemeOptions {
723
857
  */
724
858
  declare function chunkByGrapheme(text: string, options: ChunkByGraphemeOptions): string[];
725
859
 
726
- export { type AgentIdStrategy, type BaseMessageEvent, BasePlatformAdapter, type ChunkByGraphemeOptions, type ChunkTextOptions, type DeliveryRequest, DeliveryRouter, type DeliveryTarget, type DiscordMessageEvent, type EmailMessageEvent, GatewayConfigurationError, type GatewayConfigurationErrorOptions, type GatewayContext, type GatewayHandler, type GatewayHook, GatewayLifecycleError, type GatewayLifecycleErrorOptions, GatewayRunner, type GatewayRunnerOptions, type HookDecision, HookExecutor, type HookName, type LineMessageEvent, type MatrixMessageEvent, type MattermostMessageEvent, type MessageEvent, type OnErrorContext, type OutboundMessage, type PlatformName, type PostOutboundContext, type PreInboundContext, type SMSMessageEvent, type SendResult, SessionRouter, type SlackMessageEvent, type TeamsMessageEvent, type TelegramMessageEvent, type WhatsAppMessageEvent, chunkByGrapheme, chunkText, defaultStrategy };
860
+ export { type AgentIdStrategy, type BaseMessageEvent, BasePlatformAdapter, type ChunkByGraphemeOptions, type ChunkTextOptions, type DeliveryRequest, DeliveryRouter, type DeliveryTarget, type DiscordMessageEvent, type EmailMessageEvent, GatewayConfigurationError, type GatewayConfigurationErrorOptions, type GatewayContext, type GatewayHandler, type GatewayHook, GatewayLifecycleError, type GatewayLifecycleErrorOptions, GatewayRunner, type GatewayRunnerOptions, type HookDecision, HookExecutor, type HookName, type LineMessageEvent, type MatrixMessageEvent, type MattermostMessageEvent, type MessageEvent, type OnErrorContext, type OutboundMessage, type PlatformEventRegistry, type PlatformName, type PostOutboundContext, type PreInboundContext, type SMSMessageEvent, type SendResult, SessionRouter, type SlackMessageEvent, type TeamsMessageEvent, type TelegramMessageEvent, type WhatsAppMessageEvent, chunkByGrapheme, chunkText, defaultStrategy };
package/dist/index.d.ts CHANGED
@@ -12,20 +12,31 @@
12
12
  * }
13
13
  * ```
14
14
  *
15
- * This union is intentionally **closed** — adding a platform edits it here
16
- * rather than extending it externally. The trade-off (exhaustive narrowing over
17
- * OCP purity) is recorded in `wiki/decisions/adr-0001-message-event-closed-union.md`.
15
+ * This union is **open to extension and closed to corruption**. It is derived from
16
+ * {@link PlatformEventRegistry}, an interface a package outside this repository extends through
17
+ * declaration merging — so a gateway can be authored, published and consumed without editing this
18
+ * file, while narrowing still holds over platforms declared here and elsewhere alike. Entries that
19
+ * are not well-formed event shapes agreeing with their own key are excluded rather than admitted.
20
+ *
21
+ * It was closed until 2026-08-23. `docs/adr/0001-message-event-closed-union.md` records why, and
22
+ * `docs/adr/0002-platform-event-registry.md` records what changed and what the guard costs.
18
23
  *
19
24
  * @public
20
25
  */
21
- /** Closed enum of supported transport platforms. */
22
- type PlatformName = "telegram" | "discord" | "slack" | "whatsapp" | "teams" | "email" | "sms" | "mattermost" | "line" | "matrix";
23
26
  /** Fields common to every platform's inbound event. */
24
27
  interface BaseMessageEvent {
25
28
  /** Stable id used as a session-key segment and for dedup. */
26
29
  readonly id: string;
27
- /** Discriminator. */
28
- readonly platform: PlatformName;
30
+ /**
31
+ * Discriminator.
32
+ *
33
+ * Typed `string` here and narrowed to a literal by every variant. It cannot be `PlatformName`:
34
+ * that type is derived from the guarded union below, and the union is built out of shapes that
35
+ * extend this interface — naming it here would make the two reference each other (`TS2456`).
36
+ * Widening it is also what lets `PlatformName` be derived from the FILTERED union rather than
37
+ * from `keyof`, so a name and an event can never disagree about which platforms exist.
38
+ */
39
+ readonly platform: string;
29
40
  /** Sender identity (opaque, platform-namespaced). */
30
41
  readonly sender: {
31
42
  readonly id: string;
@@ -219,8 +230,131 @@ interface MatrixMessageEvent extends BaseMessageEvent {
219
230
  readonly raw: unknown;
220
231
  };
221
232
  }
222
- /** Discriminated union of all platform variants. */
223
- type MessageEvent = TelegramMessageEvent | DiscordMessageEvent | SlackMessageEvent | WhatsAppMessageEvent | TeamsMessageEvent | EmailMessageEvent | SMSMessageEvent | MattermostMessageEvent | LineMessageEvent | MatrixMessageEvent;
233
+ /**
234
+ * The open registry every platform's inbound event is filed under.
235
+ *
236
+ * **This is an `interface` on purpose, and it must stay one.** An interface can be extended from
237
+ * another package through declaration merging; a type alias cannot. That single property is what
238
+ * lets a gateway be authored, published and consumed without editing this file — the capability
239
+ * B-008 measured as impossible (`TS2416`, compiled against the published declaration) and ADR-0002
240
+ * decided to unlock. Collapsing it back into a type alias would silently remove that capability
241
+ * while every test in this package still passed.
242
+ *
243
+ * A third-party gateway registers itself from its own package:
244
+ *
245
+ * ```typescript
246
+ * declare module "@theokit/gateway" {
247
+ * interface PlatformEventRegistry {
248
+ * signal: SignalMessageEvent;
249
+ * }
250
+ * }
251
+ * ```
252
+ *
253
+ * @public
254
+ */
255
+ interface PlatformEventRegistry {
256
+ telegram: TelegramMessageEvent;
257
+ discord: DiscordMessageEvent;
258
+ slack: SlackMessageEvent;
259
+ whatsapp: WhatsAppMessageEvent;
260
+ teams: TeamsMessageEvent;
261
+ email: EmailMessageEvent;
262
+ sms: SMSMessageEvent;
263
+ mattermost: MattermostMessageEvent;
264
+ line: LineMessageEvent;
265
+ matrix: MatrixMessageEvent;
266
+ }
267
+ /**
268
+ * `true` only for `any` — the one type that would otherwise swallow the union.
269
+ *
270
+ * `any extends X` distributes to both branches of a conditional, so an ordinary
271
+ * `T extends BaseMessageEvent ? T : never` does NOT keep `any` out. This does: nothing but `any`
272
+ * makes `1 & T` accept `0`.
273
+ */
274
+ type IsAny<T> = 0 extends 1 & T ? true : false;
275
+ /**
276
+ * A registry key must be a literal, never the broad `string` an index signature produces.
277
+ *
278
+ * Measured in review: `interface PlatformEventRegistry { [key: string]: BaseMessageEvent }` is a
279
+ * LEGAL augmentation — every existing variant is assignable to the base — and without this it
280
+ * widens the whole platform name to `string`, so every typo becomes valid and every consumer's
281
+ * `switch` stops narrowing. That is the same collapse an `any` VALUE causes, arriving through the
282
+ * key side, and it is why both sides are gated.
283
+ */
284
+ type LiteralKey<K> = string extends K ? never : K;
285
+ /**
286
+ * The registry's LITERAL keys — index signatures dropped before `keyof` ever sees them.
287
+ *
288
+ * Filtering the value was not enough, measured twice. `keyof` over a registry carrying
289
+ * `{ [key: string]: BaseMessageEvent }` is `string | number`, and indexing a mapped type by that
290
+ * yields `never` for BOTH the union and the platform name: a hostile augmentation did not merely
291
+ * fail to join, it annihilated the union and broke every first-party narrowing site with
292
+ * `TS2339: Property 'telegram' does not exist on type 'never'`.
293
+ *
294
+ * It also fixes a second measured defect for free. Mapping over `keyof` directly is HOMOMORPHIC, so
295
+ * an OPTIONAL entry (`signal?: Event`, a shape an author writes by accident) kept its modifier and
296
+ * injected `undefined` into the union — `TS18048: 'e' is possibly 'undefined'` on a plain `switch`.
297
+ * Mapping over a computed key set is not homomorphic and drops the modifier. A `-?` was written
298
+ * here first as an explicit belt; the gate showed it could not be made to matter while this type is
299
+ * in place, so it was removed rather than kept as decoration nothing can fail without.
300
+ */
301
+ type LiteralKeysOf<R> = keyof {
302
+ [K in keyof R as string extends K ? never : number extends K ? never : symbol extends K ? never : K]: 0;
303
+ };
304
+ /**
305
+ * A registry entry joins {@link MessageEvent} only if it is a real event shape
306
+ * (`docs/adr/0002-platform-event-registry.md`).
307
+ *
308
+ * Measured, not anticipated: without this guard a single entry typed `any` collapses the whole
309
+ * union, and `tsc --strict` then accepts `event.completelyMadeUpField.nested.nonsense` on EVERY
310
+ * consumer — ours included — with no error in any package and nothing red in any suite. The closed
311
+ * union could not fail that way, so the guard is what keeps the new capability from costing a
312
+ * safety property the old design had.
313
+ *
314
+ * A malformed entry is EXCLUDED rather than admitted. It is NOT free for anybody else, and an
315
+ * earlier version of this docblock said it was — measured false twice over. Filtering the VALUE
316
+ * alone left two shapes that break every consumer rather than only their author:
317
+ * `{ [key: string]: … }` made `keyof` yield `string | number`, so the union and the platform name
318
+ * both collapsed to `never`; and `signal?: …` kept its optional modifier through a homomorphic
319
+ * mapping and injected `undefined` into the union. Both are handled above — the first by
320
+ * {@link LiteralKeysOf}, the second by the `-?` on the mapping. What remains true, and is the
321
+ * honest version of the original claim:
322
+ * a `Record<PlatformName, T>` written against the ten still fails to compile, because the rejected
323
+ * key is gone from `PlatformName` while the consumer's literal map is not. What the exclusion buys
324
+ * is that the failure is a missing-key error at the consumer's own map rather than a silent loss
325
+ * of narrowing everywhere.
326
+ *
327
+ * Exported for the contract test in `tests/types/registry-guard.test.ts`, which asserts against
328
+ * this type rather than a copy of it — a copy would stay green with the guard deleted. It is
329
+ * deliberately NOT re-exported from the package barrel; it is an implementation detail of the
330
+ * derivation, not part of the published surface.
331
+ */
332
+ type Registered<K, T> = IsAny<T> extends true ? never : [T] extends [BaseMessageEvent] ? [T] extends [{
333
+ readonly platform: LiteralKey<K>;
334
+ }] ? [LiteralKey<K>] extends [never] ? never : T : never : never;
335
+ /**
336
+ * The canonical inbound event: every guarded entry in {@link PlatformEventRegistry}.
337
+ *
338
+ * Derived rather than written, so third-party platforms are included automatically and exhaustive
339
+ * narrowing still holds over all of them.
340
+ *
341
+ * @public
342
+ */
343
+ type MessageEvent = {
344
+ [K in LiteralKeysOf<PlatformEventRegistry>]: Registered<K, PlatformEventRegistry[K]>;
345
+ }[LiteralKeysOf<PlatformEventRegistry>];
346
+ /**
347
+ * Every platform that actually has a well-formed event in {@link PlatformEventRegistry}.
348
+ *
349
+ * Derived from the GUARDED union rather than from `keyof`, so `PlatformName` and
350
+ * `MessageEvent["platform"]` are the same set by construction. Deriving from `keyof` let a rejected
351
+ * entry keep contributing its key, which produced names no event could ever carry — an adapter
352
+ * could register for a platform that cannot exist.
353
+ *
354
+ * Still a union of string literals, so `switch (event.platform)` narrows and a misspelled name is
355
+ * a compile error.
356
+ */
357
+ type PlatformName = MessageEvent["platform"];
224
358
 
225
359
  /**
226
360
  * `BasePlatformAdapter` — contract every transport adapter implements
@@ -723,4 +857,4 @@ interface ChunkByGraphemeOptions {
723
857
  */
724
858
  declare function chunkByGrapheme(text: string, options: ChunkByGraphemeOptions): string[];
725
859
 
726
- export { type AgentIdStrategy, type BaseMessageEvent, BasePlatformAdapter, type ChunkByGraphemeOptions, type ChunkTextOptions, type DeliveryRequest, DeliveryRouter, type DeliveryTarget, type DiscordMessageEvent, type EmailMessageEvent, GatewayConfigurationError, type GatewayConfigurationErrorOptions, type GatewayContext, type GatewayHandler, type GatewayHook, GatewayLifecycleError, type GatewayLifecycleErrorOptions, GatewayRunner, type GatewayRunnerOptions, type HookDecision, HookExecutor, type HookName, type LineMessageEvent, type MatrixMessageEvent, type MattermostMessageEvent, type MessageEvent, type OnErrorContext, type OutboundMessage, type PlatformName, type PostOutboundContext, type PreInboundContext, type SMSMessageEvent, type SendResult, SessionRouter, type SlackMessageEvent, type TeamsMessageEvent, type TelegramMessageEvent, type WhatsAppMessageEvent, chunkByGrapheme, chunkText, defaultStrategy };
860
+ export { type AgentIdStrategy, type BaseMessageEvent, BasePlatformAdapter, type ChunkByGraphemeOptions, type ChunkTextOptions, type DeliveryRequest, DeliveryRouter, type DeliveryTarget, type DiscordMessageEvent, type EmailMessageEvent, GatewayConfigurationError, type GatewayConfigurationErrorOptions, type GatewayContext, type GatewayHandler, type GatewayHook, GatewayLifecycleError, type GatewayLifecycleErrorOptions, GatewayRunner, type GatewayRunnerOptions, type HookDecision, HookExecutor, type HookName, type LineMessageEvent, type MatrixMessageEvent, type MattermostMessageEvent, type MessageEvent, type OnErrorContext, type OutboundMessage, type PlatformEventRegistry, type PlatformName, type PostOutboundContext, type PreInboundContext, type SMSMessageEvent, type SendResult, SessionRouter, type SlackMessageEvent, type TeamsMessageEvent, type TelegramMessageEvent, type WhatsAppMessageEvent, chunkByGrapheme, chunkText, defaultStrategy };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@theokit/gateway",
3
- "version": "0.6.1",
3
+ "version": "0.7.1",
4
4
  "description": "Multi-platform messaging gateway core for @theokit/sdk — transport-agnostic primitives (BasePlatformAdapter, MessageEvent, SessionRouter, DeliveryRouter, GatewayRunner, hooks). Inspired by hermes-agent/gateway. ADRs D170-D181.",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {
@@ -34,12 +34,12 @@
34
34
  "CHANGELOG.md"
35
35
  ],
36
36
  "peerDependencies": {
37
- "@theokit/sdk": "^2.18.0"
37
+ "@theokit/sdk": ">=2.18.0 <5"
38
38
  },
39
39
  "devDependencies": {
40
40
  "@stryker-mutator/core": "10",
41
41
  "@stryker-mutator/vitest-runner": "10",
42
- "@theokit/sdk": "^2.18.0",
42
+ "@theokit/sdk": "^4.53.1",
43
43
  "tsup": "^8.5.0",
44
44
  "typescript": "^5.8.0",
45
45
  "vitest": "^4.1.8",
@@ -52,7 +52,7 @@
52
52
  "scripts": {
53
53
  "build": "tsup",
54
54
  "typecheck": "tsc --noEmit",
55
- "test": "vitest run",
55
+ "test": "tsc --noEmit && vitest run",
56
56
  "test:mutation": "stryker run"
57
57
  }
58
58
  }