@theokit/gateway 0.20.0 → 0.21.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 CHANGED
@@ -1,5 +1,15 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.21.0
4
+
5
+ ### Minor Changes
6
+
7
+ - 6cad27f: Add `scopedStrategy` with four DM scopes (`main`, `per-peer`, `per-channel-peer`, `per-account-channel-peer`), peer links, an account label and a group scope.
8
+
9
+ ### Patch Changes
10
+
11
+ - 9b4ca9f: `createChannels` rejections for missing required config now name each field's environment variable; pass `envPrefix` on a `ChannelEntry` to match the prefix given to `configFromEnv`.
12
+
3
13
  ## 0.20.0
4
14
 
5
15
  ### Minor Changes
package/README.md CHANGED
@@ -325,6 +325,29 @@ const router = new SessionRouter(bindingStrategy(bindings, defaultStrategy));
325
325
 
326
326
  `accountOf(event)` says which bot account received the event when the app runs several. Creation throws `GatewayConfigurationError` with code `unknown_agent` for a default or rule naming an agent not in `agents`, and `empty_binding_rule` for a rule with no `platform`, `account` or `peer`. `bindingStrategy` prefixes the inner key with the escaped agent name, so two agents never share a session.
327
327
 
328
+ ## Session scopes
329
+
330
+ `scopedStrategy` builds a `SessionRouter` strategy with a DM scope. Every segment is escaped with the shared injective `sanitize` (`_` to `__`, `-` to `_d`) and joined with `-`, so distinct inputs never share a key. `dm` is always the second key segment, so a DM key never equals a group or thread key.
331
+
332
+ | `dmScope` | DM key |
333
+ | --- | --- |
334
+ | `main` | `_m-dm` (every DM is one conversation) |
335
+ | `per-peer` | `_p-dm-<peer>` for a sender in `peerLinks` (`"<platform>:<senderId>"` to a peer name, shared across platforms); an unlinked sender gets the `per-channel-peer` key |
336
+ | `per-channel-peer` | `<platform>-dm-<sender>` |
337
+ | `per-account-channel-peer` | `<platform>-dm-<accountLabel>-<sender>` (needs `accountLabel`) |
338
+
339
+ Groups ignore `dmScope`: `<platform>-grp-<channel>`, or `<platform>-grp-<channel>-<sender>` with `groupScope: "channel-sender"`. A thread with a topic is `<platform>-tpc-<channel>-<topic>`.
340
+
341
+ ```ts
342
+ import { scopedStrategy, SessionRouter } from "@theokit/gateway";
343
+
344
+ const router = new SessionRouter(
345
+ scopedStrategy({ dmScope: "per-peer", peerLinks: new Map([["telegram:42", "ana"], ["slack:U1", "ana"]]) }),
346
+ );
347
+ ```
348
+
349
+ Creation throws `GatewayConfigurationError` with code `invalid_session_scope` for `per-account-channel-peer` without a non-empty `accountLabel`, or for an empty peer name. It never throws per event.
350
+
328
351
  ## Architecture (5 pieces)
329
352
 
330
353
  | Module | Responsibility |
@@ -386,6 +409,8 @@ const { adapters, rejected } = createChannels([
386
409
  },
387
410
  ]);
388
411
  for (const { platform, error } of rejected) console.error(`${platform}: ${error.message}`);
412
+ // A rejection names each missing field's environment variable when the descriptor declares `env`.
413
+ // Pass `envPrefix` (the prefix you give `configFromEnv`) on an entry for the full variable name.
389
414
 
390
415
  const runner = new GatewayRunner({
391
416
  adapters,
package/dist/index.cjs CHANGED
@@ -631,7 +631,9 @@ function isAbsent(value) {
631
631
  return value === void 0 || value === null || value === "";
632
632
  }
633
633
  function missingRequiredKeys(entry, values) {
634
- return entry.descriptor.config.filter((field) => field.required && isAbsent(values[field.key])).map((field) => field.key);
634
+ return entry.descriptor.config.filter((field) => field.required && isAbsent(values[field.key])).map(
635
+ (field) => field.env === void 0 ? field.key : `${field.key} (env ${(entry.envPrefix ?? "") + field.env})`
636
+ );
635
637
  }
636
638
  function refused(platform, code, reason) {
637
639
  return {
@@ -1789,6 +1791,49 @@ function bindingStrategy(bindings, inner) {
1789
1791
  return (event) => `${sanitize(bindings.agentFor(event))}-${inner(event)}`;
1790
1792
  }
1791
1793
 
1794
+ // src/session/scoped-strategy.ts
1795
+ var join2 = (...segments) => segments.map(sanitize).join("-");
1796
+ function invalid(message, detail) {
1797
+ return new GatewayConfigurationError("gateway", {
1798
+ code: "invalid_session_scope",
1799
+ message: `gateway: invalid_session_scope: ${message}`,
1800
+ ...detail === void 0 ? {} : { detail }
1801
+ });
1802
+ }
1803
+ function scopedStrategy(options) {
1804
+ const { dmScope, peerLinks, accountLabel, groupScope = "channel" } = options;
1805
+ if (dmScope === "per-account-channel-peer" && (accountLabel === void 0 || accountLabel === "")) {
1806
+ throw invalid("per-account-channel-peer requires a non-empty accountLabel", "accountLabel");
1807
+ }
1808
+ if (peerLinks !== void 0) {
1809
+ for (const [from, peer] of peerLinks) {
1810
+ if (peer === "") throw invalid(`empty peer name for ${from}`, from);
1811
+ }
1812
+ }
1813
+ const dmKey = (platform, senderId) => {
1814
+ switch (dmScope) {
1815
+ case "main":
1816
+ return "_m-dm";
1817
+ case "per-peer": {
1818
+ const peer = peerLinks?.get(`${platform}:${senderId}`);
1819
+ return peer === void 0 ? join2(platform, "dm", senderId) : `_p-dm-${sanitize(peer)}`;
1820
+ }
1821
+ case "per-channel-peer":
1822
+ return join2(platform, "dm", senderId);
1823
+ case "per-account-channel-peer":
1824
+ return join2(platform, "dm", accountLabel ?? "", senderId);
1825
+ }
1826
+ };
1827
+ return (event) => {
1828
+ const { platform, channel, sender } = event;
1829
+ if (channel.type === "dm") return dmKey(platform, sender.id);
1830
+ if (channel.type === "thread" && channel.topicId !== void 0) {
1831
+ return join2(platform, "tpc", channel.id, channel.topicId);
1832
+ }
1833
+ return groupScope === "channel-sender" ? join2(platform, "grp", channel.id, sender.id) : join2(platform, "grp", channel.id);
1834
+ };
1835
+ }
1836
+
1792
1837
  // src/text/chunk.ts
1793
1838
  function searchBoundary(remaining, window, boundaries) {
1794
1839
  let cut = window;
@@ -2302,6 +2347,7 @@ exports.parseFlatXml = parseFlatXml;
2302
2347
  exports.rawBodyCapture = rawBodyCapture;
2303
2348
  exports.redactSecrets = redactSecrets;
2304
2349
  exports.scanFences = scanFences;
2350
+ exports.scopedStrategy = scopedStrategy;
2305
2351
  exports.selectVariant = selectVariant;
2306
2352
  exports.senderKey = senderKey;
2307
2353
  exports.splitKeepingRows = splitKeepingRows;