@theokit/gateway 0.19.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/dist/index.d.cts CHANGED
@@ -1122,6 +1122,8 @@ interface ChannelConfigField {
1122
1122
  readonly secret: boolean;
1123
1123
  /** What the field is, for a human configuring the channel. */
1124
1124
  readonly description: string;
1125
+ /** Variable name without the app prefix, e.g. `"TELEGRAM_TOKEN"`. Read by `configFromEnv`. */
1126
+ readonly env?: string;
1125
1127
  }
1126
1128
  /** A webhook request as the app's HTTP layer received it, before anything parsed the body. */
1127
1129
  interface WebhookRequest {
@@ -1195,6 +1197,11 @@ interface ChannelDescriptorBase<TAdapter extends BasePlatformAdapter> {
1195
1197
  * authenticated (fail closed); it is optional only so descriptors in other packages keep compiling.
1196
1198
  */
1197
1199
  readonly senderIdentity?: SenderIdentity;
1200
+ /**
1201
+ * Alternative config shapes for the same platform (for example WhatsApp cloud versus baileys),
1202
+ * chosen by name with `selectVariant`. Each variant reports the parent's `platform`.
1203
+ */
1204
+ readonly variants?: Readonly<Record<string, ChannelDescriptor>>;
1198
1205
  }
1199
1206
  /**
1200
1207
  * A channel described as data. A `socket` channel opens its own connection to the platform; a
@@ -1217,6 +1224,35 @@ type ChannelDescriptor<TAdapter extends BasePlatformAdapter = BasePlatformAdapte
1217
1224
  */
1218
1225
  declare function isAuthenticatedSender(descriptor: ChannelDescriptor): boolean;
1219
1226
 
1227
+ /**
1228
+ * `configFromEnv` and `selectVariant`: bind a channel descriptor's config fields to environment
1229
+ * variables, and pick one of a platform's config shapes.
1230
+ *
1231
+ * @public
1232
+ */
1233
+
1234
+ /** What `configFromEnv` read from the environment. Holds secret values: never log it whole. */
1235
+ interface EnvConfig {
1236
+ /** Trimmed, non-empty values by config key. */
1237
+ readonly config: Readonly<Record<string, string>>;
1238
+ /** Full variable names of required fields that are unset, empty or whitespace. */
1239
+ readonly missing: readonly string[];
1240
+ /** Values of present fields marked secret, for a log mask. Never logged. */
1241
+ readonly secrets: readonly string[];
1242
+ }
1243
+ /**
1244
+ * Read a descriptor's fields from `env`, using `prefix + field.env` as the variable name. Fields
1245
+ * without `env` are skipped.
1246
+ */
1247
+ declare function configFromEnv(descriptor: ChannelDescriptor, env: Readonly<Record<string, string | undefined>>, prefix?: string): EnvConfig;
1248
+ /**
1249
+ * The descriptor to use for `name`: the descriptor itself when `name` is undefined and it has no
1250
+ * variants, otherwise the named variant. Throws `unknown_channel_variant` for a name that is not a
1251
+ * variant (or a missing name when variants exist) and `platform_mismatch` for a variant whose
1252
+ * platform differs from its parent's.
1253
+ */
1254
+ declare function selectVariant(descriptor: ChannelDescriptor, name: string | undefined): ChannelDescriptor;
1255
+
1220
1256
  /**
1221
1257
  * `GatewayConfigurationError` — shared base for per-adapter configuration
1222
1258
  * errors (roadmap M0/M2).
@@ -1267,6 +1303,11 @@ interface ChannelEntry {
1267
1303
  readonly descriptor: ChannelDescriptor<BasePlatformAdapter>;
1268
1304
  /** Values keyed by the descriptor's field keys. Absent means every required field is missing. */
1269
1305
  readonly config?: Readonly<Record<string, unknown>>;
1306
+ /**
1307
+ * The prefix passed to `configFromEnv` for this entry, so a missing field's rejection names its
1308
+ * full environment variable. Defaults to `""`.
1309
+ */
1310
+ readonly envPrefix?: string;
1270
1311
  }
1271
1312
  /** A channel that was not built, and the configuration error that says why. */
1272
1313
  interface ChannelRejection {
@@ -1388,6 +1429,35 @@ declare class GatewayLifecycleError extends Error {
1388
1429
  constructor(opts: GatewayLifecycleErrorOptions);
1389
1430
  }
1390
1431
 
1432
+ /**
1433
+ * File-backed pairing store: senders approved by an owner are kept in `pairing.json`.
1434
+ * Pending codes live in memory for one hour.
1435
+ *
1436
+ * @public
1437
+ */
1438
+ /** Pairing store used by the access policy hook. */
1439
+ interface PairingStore {
1440
+ /** A 6-digit code for the sender; the same code while one is pending. */
1441
+ issue(platform: string, senderId: string): string;
1442
+ approve(code: string): Promise<{
1443
+ ok: true;
1444
+ platform: string;
1445
+ senderId: string;
1446
+ } | {
1447
+ ok: false;
1448
+ reason: "unknown_or_expired" | "write_failed";
1449
+ }>;
1450
+ isPaired(platform: string, senderId: string): boolean;
1451
+ }
1452
+ /** `<platform>:<senderId>`; email ids compare case-insensitively. */
1453
+ declare function senderKey(platform: string, senderId: string): string;
1454
+ /** Creates a store persisted in `<dir>/pairing.json` (mode 0600). */
1455
+ declare function createFilePairingStore(options: {
1456
+ dir: string;
1457
+ clock?: () => number;
1458
+ random?: (min: number, max: number) => number;
1459
+ }): PairingStore;
1460
+
1391
1461
  /**
1392
1462
  * Gateway hooks — own contract, not a `Plugin.kind` extension
1393
1463
  * (T4.1, ADRs D176, D177).
@@ -1439,6 +1509,35 @@ interface GatewayHook {
1439
1509
  on_error?(ctx: OnErrorContext): Promise<void> | void;
1440
1510
  }
1441
1511
 
1512
+ /**
1513
+ * Access policy hook: who may talk to the agent, with sender pairing.
1514
+ *
1515
+ * @public
1516
+ */
1517
+
1518
+ type DmPolicy = "pairing" | "allowlist" | "open" | "disabled";
1519
+ /** Options for {@link createAccessPolicyHook}. */
1520
+ interface AccessPolicyOptions {
1521
+ /** `<platform>:<senderId>` keys. */
1522
+ readonly owners: ReadonlySet<string>;
1523
+ readonly allowed?: ReadonlySet<string>;
1524
+ readonly dm: DmPolicy;
1525
+ /** Group messages from non-members: "silent" (default) or "open". */
1526
+ readonly groups?: "silent" | "open";
1527
+ /** Which platforms authenticate the sender. */
1528
+ readonly isAuthenticated: (platform: PlatformName) => boolean;
1529
+ /** Required when dm is "pairing". */
1530
+ readonly pairing?: PairingStore;
1531
+ readonly refusalText?: string;
1532
+ readonly refusalIntervalMs?: number;
1533
+ /** Let a message through untouched. */
1534
+ readonly passThrough?: (event: MessageEvent) => boolean;
1535
+ readonly clock?: () => number;
1536
+ readonly write?: (line: string) => void;
1537
+ }
1538
+ /** Builds the `pre_inbound` access policy hook. */
1539
+ declare function createAccessPolicyHook(options: AccessPolicyOptions): GatewayHook;
1540
+
1442
1541
  /**
1443
1542
  * Chat command parsing and a single `pre_inbound` hook for many commands.
1444
1543
  *
@@ -1913,6 +2012,41 @@ declare function createAgentBindings(options: AgentBindingsOptions): AgentBindin
1913
2012
  /** Prefixes another strategy's key with the bound agent, so two agents never share a session. */
1914
2013
  declare function bindingStrategy(bindings: AgentBindings, inner: AgentIdStrategy): AgentIdStrategy;
1915
2014
 
2015
+ /**
2016
+ * `scopedStrategy` — session keys with DM scopes, peer links and account labels.
2017
+ *
2018
+ * `dm` is always the second key segment, so no label or peer name can make a
2019
+ * DM key equal a group (`grp`) or thread (`tpc`) key. Every segment is encoded
2020
+ * with `encodeURIComponent` and joined with `:`, which it never emits, so the
2021
+ * key mapping is injective. Linked peers and `main` use a leading `@` segment,
2022
+ * which no encoded platform can start with (`@` encodes to `%40`).
2023
+ *
2024
+ * @public
2025
+ */
2026
+
2027
+ /** How DMs are keyed: one conversation, per peer, per channel and peer, or per account too. */
2028
+ type DmScope = "main" | "per-peer" | "per-channel-peer" | "per-account-channel-peer";
2029
+ /** Options for {@link scopedStrategy}. */
2030
+ interface ScopedStrategyOptions {
2031
+ readonly dmScope: DmScope;
2032
+ /** `<platform>:<senderId>` to a peer name. Used by `per-peer`. */
2033
+ readonly peerLinks?: ReadonlyMap<string, string>;
2034
+ /** Required when `dmScope` is `per-account-channel-peer`. */
2035
+ readonly accountLabel?: string;
2036
+ /** Group key: one per channel (default) or one per channel and sender. */
2037
+ readonly groupScope?: "channel" | "channel-sender";
2038
+ }
2039
+ /**
2040
+ * Builds a strategy for the chosen DM scope. Keys:
2041
+ * `main` → `_m-dm`; `per-peer` linked → `_p-dm-<peer>`, unlinked → the
2042
+ * per-channel-peer key; `per-channel-peer` → `<platform>-dm-<sender>`;
2043
+ * `per-account-channel-peer` → `<platform>-dm-<account>-<sender>`.
2044
+ * Groups are `<platform>-grp-<channel>[-<sender>]`, topic threads
2045
+ * `<platform>-tpc-<channel>-<topic>`. Every segment is escaped with `sanitize`.
2046
+ * Throws `GatewayConfigurationError` (`invalid_session_scope`) on bad options, never per event.
2047
+ */
2048
+ declare function scopedStrategy(options: ScopedStrategyOptions): AgentIdStrategy;
2049
+
1916
2050
  /**
1917
2051
  * What `ChatPresenter` needs to know about one platform: how to rewrite the text, and which
1918
2052
  * `format` to declare on the message (B-019 FR-001, FR-002).
@@ -2252,4 +2386,4 @@ interface WebhookHandlerOptions {
2252
2386
  */
2253
2387
  declare function createWebhookHandler(options: WebhookHandlerOptions): (request: Request) => Promise<Response>;
2254
2388
 
2255
- export { type AdapterCapabilities, type AdapterState, type AgentBindingRule, type AgentBindings, type AgentBindingsOptions, type AgentIdStrategy, type BaseMessageEvent, BasePlatformAdapter, type BuiltChannel, type BuzzMessageEvent, CREDENTIAL_SHAPES, type ChannelConfigField, type ChannelDescriptor, type ChannelEntry, type ChannelRejection, type ChannelRendering, type ChannelWebhook, type ChatCommand, type ChunkByGraphemeOptions, type ChunkTextOptions, type ClickClackMessageEvent, type ConnectFailure, type CoveredShape, type CreateChannelsResult, DEFAULT_WEBHOOK_MAX_BYTES, type DeliveryRequest, DeliveryRouter, type DeliveryTarget, type DiscordMessageEvent, type EmailMessageEvent, type FeishuLarkMessageEvent, type FenceRegion, GatewayConfigurationError, type GatewayConfigurationErrorOptions, type GatewayContext, type GatewayHandler, type GatewayHook, GatewayLifecycleError, type GatewayLifecycleErrorOptions, GatewayRunner, type GatewayRunnerOptions, type GoogleChatMessageEvent, type HookDecision, HookExecutor, type HookName, type IMessageMessageEvent, type InboundAudio, type IngestResult, type IrcMessageEvent, type LineMessageEvent, type ListenableApp, type ListenerLike, type MatrixMessageEvent, type MattermostMessageEvent, type MediaUnderstandingOptions, type MessageEvent, type NextcloudTalkMessageEvent, type NostrMessageEvent, type OnErrorContext, type OutboundAttachment, type OutboundMessage, type ParsedChatCommand, type PlatformEventRegistry, type PlatformName, type PostOutboundContext, type PreInboundContext, type QQMessageEvent, type RaftMessageEvent, type RawBodyCaptureOptions, type RawBodyRequest, type ReconnectPolicy, type SMSMessageEvent, SeenIds, type SendResult, type SenderIdentity, SessionRouter, type SignalMessageEvent, type SlackMessageEvent, type StartResult, type SynologyChatMessageEvent, type TeamsMessageEvent, type TelegramMessageEvent, type TlonUrbitMessageEvent, type TwitchMessageEvent, type UncoveredShape, type WeChatMessageEvent, type WeComMessageEvent, type WebhookAnswer, type WebhookHandlerOptions, type WebhookRequest, WeixinAdapterBase, type WeixinAdapterConfig, type WeixinFetch, type WeixinSendRequest, type WeixinWebhookAnswer, type WhatsAppMessageEvent, type XMessageEvent, type XThreadTweet, type YuanbaoMessageEvent, type ZaloMessageEvent, type ZaloPersonalMessageEvent, bindingStrategy, channelRendering, chunkByGrapheme, chunkText, commandHook, createAgentBindings, createChannels, createWebhookHandler, defaultStrategy, helpText, isAuthenticatedSender, listenerLifecycle, loadPeer, maskSecrets, maskShapes, parseChatCommand, parseFlatXml, rawBodyCapture, redactSecrets, scanFences, splitKeepingRows, statusForDeliver, toDialect, withMediaUnderstanding };
2389
+ export { type AccessPolicyOptions, type AdapterCapabilities, type AdapterState, type AgentBindingRule, type AgentBindings, type AgentBindingsOptions, type AgentIdStrategy, type BaseMessageEvent, BasePlatformAdapter, type BuiltChannel, type BuzzMessageEvent, CREDENTIAL_SHAPES, type ChannelConfigField, type ChannelDescriptor, type ChannelEntry, type ChannelRejection, type ChannelRendering, type ChannelWebhook, type ChatCommand, type ChunkByGraphemeOptions, type ChunkTextOptions, type ClickClackMessageEvent, type ConnectFailure, type CoveredShape, type CreateChannelsResult, DEFAULT_WEBHOOK_MAX_BYTES, type DeliveryRequest, DeliveryRouter, type DeliveryTarget, type DiscordMessageEvent, type DmPolicy, type DmScope, type EmailMessageEvent, type EnvConfig, type FeishuLarkMessageEvent, type FenceRegion, GatewayConfigurationError, type GatewayConfigurationErrorOptions, type GatewayContext, type GatewayHandler, type GatewayHook, GatewayLifecycleError, type GatewayLifecycleErrorOptions, GatewayRunner, type GatewayRunnerOptions, type GoogleChatMessageEvent, type HookDecision, HookExecutor, type HookName, type IMessageMessageEvent, type InboundAudio, type IngestResult, type IrcMessageEvent, type LineMessageEvent, type ListenableApp, type ListenerLike, type MatrixMessageEvent, type MattermostMessageEvent, type MediaUnderstandingOptions, type MessageEvent, type NextcloudTalkMessageEvent, type NostrMessageEvent, type OnErrorContext, type OutboundAttachment, type OutboundMessage, type PairingStore, type ParsedChatCommand, type PlatformEventRegistry, type PlatformName, type PostOutboundContext, type PreInboundContext, type QQMessageEvent, type RaftMessageEvent, type RawBodyCaptureOptions, type RawBodyRequest, type ReconnectPolicy, type SMSMessageEvent, type ScopedStrategyOptions, SeenIds, type SendResult, type SenderIdentity, SessionRouter, type SignalMessageEvent, type SlackMessageEvent, type StartResult, type SynologyChatMessageEvent, type TeamsMessageEvent, type TelegramMessageEvent, type TlonUrbitMessageEvent, type TwitchMessageEvent, type UncoveredShape, type WeChatMessageEvent, type WeComMessageEvent, type WebhookAnswer, type WebhookHandlerOptions, type WebhookRequest, WeixinAdapterBase, type WeixinAdapterConfig, type WeixinFetch, type WeixinSendRequest, type WeixinWebhookAnswer, type WhatsAppMessageEvent, type XMessageEvent, type XThreadTweet, type YuanbaoMessageEvent, type ZaloMessageEvent, type ZaloPersonalMessageEvent, bindingStrategy, channelRendering, chunkByGrapheme, chunkText, commandHook, configFromEnv, createAccessPolicyHook, createAgentBindings, createChannels, createFilePairingStore, createWebhookHandler, defaultStrategy, helpText, isAuthenticatedSender, listenerLifecycle, loadPeer, maskSecrets, maskShapes, parseChatCommand, parseFlatXml, rawBodyCapture, redactSecrets, scanFences, scopedStrategy, selectVariant, senderKey, splitKeepingRows, statusForDeliver, toDialect, withMediaUnderstanding };
package/dist/index.d.ts CHANGED
@@ -1122,6 +1122,8 @@ interface ChannelConfigField {
1122
1122
  readonly secret: boolean;
1123
1123
  /** What the field is, for a human configuring the channel. */
1124
1124
  readonly description: string;
1125
+ /** Variable name without the app prefix, e.g. `"TELEGRAM_TOKEN"`. Read by `configFromEnv`. */
1126
+ readonly env?: string;
1125
1127
  }
1126
1128
  /** A webhook request as the app's HTTP layer received it, before anything parsed the body. */
1127
1129
  interface WebhookRequest {
@@ -1195,6 +1197,11 @@ interface ChannelDescriptorBase<TAdapter extends BasePlatformAdapter> {
1195
1197
  * authenticated (fail closed); it is optional only so descriptors in other packages keep compiling.
1196
1198
  */
1197
1199
  readonly senderIdentity?: SenderIdentity;
1200
+ /**
1201
+ * Alternative config shapes for the same platform (for example WhatsApp cloud versus baileys),
1202
+ * chosen by name with `selectVariant`. Each variant reports the parent's `platform`.
1203
+ */
1204
+ readonly variants?: Readonly<Record<string, ChannelDescriptor>>;
1198
1205
  }
1199
1206
  /**
1200
1207
  * A channel described as data. A `socket` channel opens its own connection to the platform; a
@@ -1217,6 +1224,35 @@ type ChannelDescriptor<TAdapter extends BasePlatformAdapter = BasePlatformAdapte
1217
1224
  */
1218
1225
  declare function isAuthenticatedSender(descriptor: ChannelDescriptor): boolean;
1219
1226
 
1227
+ /**
1228
+ * `configFromEnv` and `selectVariant`: bind a channel descriptor's config fields to environment
1229
+ * variables, and pick one of a platform's config shapes.
1230
+ *
1231
+ * @public
1232
+ */
1233
+
1234
+ /** What `configFromEnv` read from the environment. Holds secret values: never log it whole. */
1235
+ interface EnvConfig {
1236
+ /** Trimmed, non-empty values by config key. */
1237
+ readonly config: Readonly<Record<string, string>>;
1238
+ /** Full variable names of required fields that are unset, empty or whitespace. */
1239
+ readonly missing: readonly string[];
1240
+ /** Values of present fields marked secret, for a log mask. Never logged. */
1241
+ readonly secrets: readonly string[];
1242
+ }
1243
+ /**
1244
+ * Read a descriptor's fields from `env`, using `prefix + field.env` as the variable name. Fields
1245
+ * without `env` are skipped.
1246
+ */
1247
+ declare function configFromEnv(descriptor: ChannelDescriptor, env: Readonly<Record<string, string | undefined>>, prefix?: string): EnvConfig;
1248
+ /**
1249
+ * The descriptor to use for `name`: the descriptor itself when `name` is undefined and it has no
1250
+ * variants, otherwise the named variant. Throws `unknown_channel_variant` for a name that is not a
1251
+ * variant (or a missing name when variants exist) and `platform_mismatch` for a variant whose
1252
+ * platform differs from its parent's.
1253
+ */
1254
+ declare function selectVariant(descriptor: ChannelDescriptor, name: string | undefined): ChannelDescriptor;
1255
+
1220
1256
  /**
1221
1257
  * `GatewayConfigurationError` — shared base for per-adapter configuration
1222
1258
  * errors (roadmap M0/M2).
@@ -1267,6 +1303,11 @@ interface ChannelEntry {
1267
1303
  readonly descriptor: ChannelDescriptor<BasePlatformAdapter>;
1268
1304
  /** Values keyed by the descriptor's field keys. Absent means every required field is missing. */
1269
1305
  readonly config?: Readonly<Record<string, unknown>>;
1306
+ /**
1307
+ * The prefix passed to `configFromEnv` for this entry, so a missing field's rejection names its
1308
+ * full environment variable. Defaults to `""`.
1309
+ */
1310
+ readonly envPrefix?: string;
1270
1311
  }
1271
1312
  /** A channel that was not built, and the configuration error that says why. */
1272
1313
  interface ChannelRejection {
@@ -1388,6 +1429,35 @@ declare class GatewayLifecycleError extends Error {
1388
1429
  constructor(opts: GatewayLifecycleErrorOptions);
1389
1430
  }
1390
1431
 
1432
+ /**
1433
+ * File-backed pairing store: senders approved by an owner are kept in `pairing.json`.
1434
+ * Pending codes live in memory for one hour.
1435
+ *
1436
+ * @public
1437
+ */
1438
+ /** Pairing store used by the access policy hook. */
1439
+ interface PairingStore {
1440
+ /** A 6-digit code for the sender; the same code while one is pending. */
1441
+ issue(platform: string, senderId: string): string;
1442
+ approve(code: string): Promise<{
1443
+ ok: true;
1444
+ platform: string;
1445
+ senderId: string;
1446
+ } | {
1447
+ ok: false;
1448
+ reason: "unknown_or_expired" | "write_failed";
1449
+ }>;
1450
+ isPaired(platform: string, senderId: string): boolean;
1451
+ }
1452
+ /** `<platform>:<senderId>`; email ids compare case-insensitively. */
1453
+ declare function senderKey(platform: string, senderId: string): string;
1454
+ /** Creates a store persisted in `<dir>/pairing.json` (mode 0600). */
1455
+ declare function createFilePairingStore(options: {
1456
+ dir: string;
1457
+ clock?: () => number;
1458
+ random?: (min: number, max: number) => number;
1459
+ }): PairingStore;
1460
+
1391
1461
  /**
1392
1462
  * Gateway hooks — own contract, not a `Plugin.kind` extension
1393
1463
  * (T4.1, ADRs D176, D177).
@@ -1439,6 +1509,35 @@ interface GatewayHook {
1439
1509
  on_error?(ctx: OnErrorContext): Promise<void> | void;
1440
1510
  }
1441
1511
 
1512
+ /**
1513
+ * Access policy hook: who may talk to the agent, with sender pairing.
1514
+ *
1515
+ * @public
1516
+ */
1517
+
1518
+ type DmPolicy = "pairing" | "allowlist" | "open" | "disabled";
1519
+ /** Options for {@link createAccessPolicyHook}. */
1520
+ interface AccessPolicyOptions {
1521
+ /** `<platform>:<senderId>` keys. */
1522
+ readonly owners: ReadonlySet<string>;
1523
+ readonly allowed?: ReadonlySet<string>;
1524
+ readonly dm: DmPolicy;
1525
+ /** Group messages from non-members: "silent" (default) or "open". */
1526
+ readonly groups?: "silent" | "open";
1527
+ /** Which platforms authenticate the sender. */
1528
+ readonly isAuthenticated: (platform: PlatformName) => boolean;
1529
+ /** Required when dm is "pairing". */
1530
+ readonly pairing?: PairingStore;
1531
+ readonly refusalText?: string;
1532
+ readonly refusalIntervalMs?: number;
1533
+ /** Let a message through untouched. */
1534
+ readonly passThrough?: (event: MessageEvent) => boolean;
1535
+ readonly clock?: () => number;
1536
+ readonly write?: (line: string) => void;
1537
+ }
1538
+ /** Builds the `pre_inbound` access policy hook. */
1539
+ declare function createAccessPolicyHook(options: AccessPolicyOptions): GatewayHook;
1540
+
1442
1541
  /**
1443
1542
  * Chat command parsing and a single `pre_inbound` hook for many commands.
1444
1543
  *
@@ -1913,6 +2012,41 @@ declare function createAgentBindings(options: AgentBindingsOptions): AgentBindin
1913
2012
  /** Prefixes another strategy's key with the bound agent, so two agents never share a session. */
1914
2013
  declare function bindingStrategy(bindings: AgentBindings, inner: AgentIdStrategy): AgentIdStrategy;
1915
2014
 
2015
+ /**
2016
+ * `scopedStrategy` — session keys with DM scopes, peer links and account labels.
2017
+ *
2018
+ * `dm` is always the second key segment, so no label or peer name can make a
2019
+ * DM key equal a group (`grp`) or thread (`tpc`) key. Every segment is encoded
2020
+ * with `encodeURIComponent` and joined with `:`, which it never emits, so the
2021
+ * key mapping is injective. Linked peers and `main` use a leading `@` segment,
2022
+ * which no encoded platform can start with (`@` encodes to `%40`).
2023
+ *
2024
+ * @public
2025
+ */
2026
+
2027
+ /** How DMs are keyed: one conversation, per peer, per channel and peer, or per account too. */
2028
+ type DmScope = "main" | "per-peer" | "per-channel-peer" | "per-account-channel-peer";
2029
+ /** Options for {@link scopedStrategy}. */
2030
+ interface ScopedStrategyOptions {
2031
+ readonly dmScope: DmScope;
2032
+ /** `<platform>:<senderId>` to a peer name. Used by `per-peer`. */
2033
+ readonly peerLinks?: ReadonlyMap<string, string>;
2034
+ /** Required when `dmScope` is `per-account-channel-peer`. */
2035
+ readonly accountLabel?: string;
2036
+ /** Group key: one per channel (default) or one per channel and sender. */
2037
+ readonly groupScope?: "channel" | "channel-sender";
2038
+ }
2039
+ /**
2040
+ * Builds a strategy for the chosen DM scope. Keys:
2041
+ * `main` → `_m-dm`; `per-peer` linked → `_p-dm-<peer>`, unlinked → the
2042
+ * per-channel-peer key; `per-channel-peer` → `<platform>-dm-<sender>`;
2043
+ * `per-account-channel-peer` → `<platform>-dm-<account>-<sender>`.
2044
+ * Groups are `<platform>-grp-<channel>[-<sender>]`, topic threads
2045
+ * `<platform>-tpc-<channel>-<topic>`. Every segment is escaped with `sanitize`.
2046
+ * Throws `GatewayConfigurationError` (`invalid_session_scope`) on bad options, never per event.
2047
+ */
2048
+ declare function scopedStrategy(options: ScopedStrategyOptions): AgentIdStrategy;
2049
+
1916
2050
  /**
1917
2051
  * What `ChatPresenter` needs to know about one platform: how to rewrite the text, and which
1918
2052
  * `format` to declare on the message (B-019 FR-001, FR-002).
@@ -2252,4 +2386,4 @@ interface WebhookHandlerOptions {
2252
2386
  */
2253
2387
  declare function createWebhookHandler(options: WebhookHandlerOptions): (request: Request) => Promise<Response>;
2254
2388
 
2255
- export { type AdapterCapabilities, type AdapterState, type AgentBindingRule, type AgentBindings, type AgentBindingsOptions, type AgentIdStrategy, type BaseMessageEvent, BasePlatformAdapter, type BuiltChannel, type BuzzMessageEvent, CREDENTIAL_SHAPES, type ChannelConfigField, type ChannelDescriptor, type ChannelEntry, type ChannelRejection, type ChannelRendering, type ChannelWebhook, type ChatCommand, type ChunkByGraphemeOptions, type ChunkTextOptions, type ClickClackMessageEvent, type ConnectFailure, type CoveredShape, type CreateChannelsResult, DEFAULT_WEBHOOK_MAX_BYTES, type DeliveryRequest, DeliveryRouter, type DeliveryTarget, type DiscordMessageEvent, type EmailMessageEvent, type FeishuLarkMessageEvent, type FenceRegion, GatewayConfigurationError, type GatewayConfigurationErrorOptions, type GatewayContext, type GatewayHandler, type GatewayHook, GatewayLifecycleError, type GatewayLifecycleErrorOptions, GatewayRunner, type GatewayRunnerOptions, type GoogleChatMessageEvent, type HookDecision, HookExecutor, type HookName, type IMessageMessageEvent, type InboundAudio, type IngestResult, type IrcMessageEvent, type LineMessageEvent, type ListenableApp, type ListenerLike, type MatrixMessageEvent, type MattermostMessageEvent, type MediaUnderstandingOptions, type MessageEvent, type NextcloudTalkMessageEvent, type NostrMessageEvent, type OnErrorContext, type OutboundAttachment, type OutboundMessage, type ParsedChatCommand, type PlatformEventRegistry, type PlatformName, type PostOutboundContext, type PreInboundContext, type QQMessageEvent, type RaftMessageEvent, type RawBodyCaptureOptions, type RawBodyRequest, type ReconnectPolicy, type SMSMessageEvent, SeenIds, type SendResult, type SenderIdentity, SessionRouter, type SignalMessageEvent, type SlackMessageEvent, type StartResult, type SynologyChatMessageEvent, type TeamsMessageEvent, type TelegramMessageEvent, type TlonUrbitMessageEvent, type TwitchMessageEvent, type UncoveredShape, type WeChatMessageEvent, type WeComMessageEvent, type WebhookAnswer, type WebhookHandlerOptions, type WebhookRequest, WeixinAdapterBase, type WeixinAdapterConfig, type WeixinFetch, type WeixinSendRequest, type WeixinWebhookAnswer, type WhatsAppMessageEvent, type XMessageEvent, type XThreadTweet, type YuanbaoMessageEvent, type ZaloMessageEvent, type ZaloPersonalMessageEvent, bindingStrategy, channelRendering, chunkByGrapheme, chunkText, commandHook, createAgentBindings, createChannels, createWebhookHandler, defaultStrategy, helpText, isAuthenticatedSender, listenerLifecycle, loadPeer, maskSecrets, maskShapes, parseChatCommand, parseFlatXml, rawBodyCapture, redactSecrets, scanFences, splitKeepingRows, statusForDeliver, toDialect, withMediaUnderstanding };
2389
+ export { type AccessPolicyOptions, type AdapterCapabilities, type AdapterState, type AgentBindingRule, type AgentBindings, type AgentBindingsOptions, type AgentIdStrategy, type BaseMessageEvent, BasePlatformAdapter, type BuiltChannel, type BuzzMessageEvent, CREDENTIAL_SHAPES, type ChannelConfigField, type ChannelDescriptor, type ChannelEntry, type ChannelRejection, type ChannelRendering, type ChannelWebhook, type ChatCommand, type ChunkByGraphemeOptions, type ChunkTextOptions, type ClickClackMessageEvent, type ConnectFailure, type CoveredShape, type CreateChannelsResult, DEFAULT_WEBHOOK_MAX_BYTES, type DeliveryRequest, DeliveryRouter, type DeliveryTarget, type DiscordMessageEvent, type DmPolicy, type DmScope, type EmailMessageEvent, type EnvConfig, type FeishuLarkMessageEvent, type FenceRegion, GatewayConfigurationError, type GatewayConfigurationErrorOptions, type GatewayContext, type GatewayHandler, type GatewayHook, GatewayLifecycleError, type GatewayLifecycleErrorOptions, GatewayRunner, type GatewayRunnerOptions, type GoogleChatMessageEvent, type HookDecision, HookExecutor, type HookName, type IMessageMessageEvent, type InboundAudio, type IngestResult, type IrcMessageEvent, type LineMessageEvent, type ListenableApp, type ListenerLike, type MatrixMessageEvent, type MattermostMessageEvent, type MediaUnderstandingOptions, type MessageEvent, type NextcloudTalkMessageEvent, type NostrMessageEvent, type OnErrorContext, type OutboundAttachment, type OutboundMessage, type PairingStore, type ParsedChatCommand, type PlatformEventRegistry, type PlatformName, type PostOutboundContext, type PreInboundContext, type QQMessageEvent, type RaftMessageEvent, type RawBodyCaptureOptions, type RawBodyRequest, type ReconnectPolicy, type SMSMessageEvent, type ScopedStrategyOptions, SeenIds, type SendResult, type SenderIdentity, SessionRouter, type SignalMessageEvent, type SlackMessageEvent, type StartResult, type SynologyChatMessageEvent, type TeamsMessageEvent, type TelegramMessageEvent, type TlonUrbitMessageEvent, type TwitchMessageEvent, type UncoveredShape, type WeChatMessageEvent, type WeComMessageEvent, type WebhookAnswer, type WebhookHandlerOptions, type WebhookRequest, WeixinAdapterBase, type WeixinAdapterConfig, type WeixinFetch, type WeixinSendRequest, type WeixinWebhookAnswer, type WhatsAppMessageEvent, type XMessageEvent, type XThreadTweet, type YuanbaoMessageEvent, type ZaloMessageEvent, type ZaloPersonalMessageEvent, bindingStrategy, channelRendering, chunkByGrapheme, chunkText, commandHook, configFromEnv, createAccessPolicyHook, createAgentBindings, createChannels, createFilePairingStore, createWebhookHandler, defaultStrategy, helpText, isAuthenticatedSender, listenerLifecycle, loadPeer, maskSecrets, maskShapes, parseChatCommand, parseFlatXml, rawBodyCapture, redactSecrets, scanFences, scopedStrategy, selectVariant, senderKey, splitKeepingRows, statusForDeliver, toDialect, withMediaUnderstanding };