@theokit/gateway 0.18.0 → 0.20.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 +22 -0
- package/README.md +144 -0
- package/dist/index.cjs +646 -36
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +330 -1
- package/dist/index.d.ts +330 -1
- package/dist/index.js +633 -37
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -830,6 +830,17 @@ interface SendResult {
|
|
|
830
830
|
readonly message: string;
|
|
831
831
|
};
|
|
832
832
|
}
|
|
833
|
+
/** The facts about an adapter that an app picks its rendering from. */
|
|
834
|
+
interface AdapterCapabilities {
|
|
835
|
+
readonly edit: boolean;
|
|
836
|
+
/** The attachment kinds the adapter sends; `[]` when none. */
|
|
837
|
+
readonly attachments: ReadonlyArray<OutboundAttachment["kind"]>;
|
|
838
|
+
readonly typing: boolean;
|
|
839
|
+
readonly reactions: boolean;
|
|
840
|
+
readonly threads: boolean;
|
|
841
|
+
/** Longest text one message holds; `undefined` when the adapter does not know. */
|
|
842
|
+
readonly maxMessageChars?: number;
|
|
843
|
+
}
|
|
833
844
|
/**
|
|
834
845
|
* Abstract base for all platform adapters.
|
|
835
846
|
*
|
|
@@ -957,6 +968,18 @@ declare abstract class BasePlatformAdapter {
|
|
|
957
968
|
* `boolean` so that an adapter that sends images can override it with `true`.
|
|
958
969
|
*/
|
|
959
970
|
readonly supportsAttachments: boolean;
|
|
971
|
+
/**
|
|
972
|
+
* The attachment kinds {@link sendMessage} can send. `undefined` means "not declared": the
|
|
973
|
+
* `capabilities` getter then reports `["image"]` when `supportsAttachments` is `true`, else `[]`.
|
|
974
|
+
*/
|
|
975
|
+
protected readonly attachmentKinds: ReadonlyArray<OutboundAttachment["kind"]> | undefined;
|
|
976
|
+
/**
|
|
977
|
+
* What this adapter can do, for an app that renders differently per channel.
|
|
978
|
+
*
|
|
979
|
+
* Derived from the members above and from which lifecycle methods the subclass overrides; an
|
|
980
|
+
* adapter overrides the getter only for what derivation cannot know. Read it at call time.
|
|
981
|
+
*/
|
|
982
|
+
get capabilities(): AdapterCapabilities;
|
|
960
983
|
/**
|
|
961
984
|
* Replace the text of a message this adapter sent, keeping its id.
|
|
962
985
|
*
|
|
@@ -1099,6 +1122,8 @@ interface ChannelConfigField {
|
|
|
1099
1122
|
readonly secret: boolean;
|
|
1100
1123
|
/** What the field is, for a human configuring the channel. */
|
|
1101
1124
|
readonly description: string;
|
|
1125
|
+
/** Variable name without the app prefix, e.g. `"TELEGRAM_TOKEN"`. Read by `configFromEnv`. */
|
|
1126
|
+
readonly env?: string;
|
|
1102
1127
|
}
|
|
1103
1128
|
/** A webhook request as the app's HTTP layer received it, before anything parsed the body. */
|
|
1104
1129
|
interface WebhookRequest {
|
|
@@ -1106,6 +1131,19 @@ interface WebhookRequest {
|
|
|
1106
1131
|
readonly rawBody: string;
|
|
1107
1132
|
/** Request headers. Descriptors look a header up without regard to case. */
|
|
1108
1133
|
readonly headers: Readonly<Record<string, string | undefined>>;
|
|
1134
|
+
/** The HTTP method. Always set by `createWebhookHandler`; optional so existing callers compile. */
|
|
1135
|
+
readonly method?: string;
|
|
1136
|
+
/** The full request URL, query included. Always set by `createWebhookHandler`. */
|
|
1137
|
+
readonly url?: string;
|
|
1138
|
+
}
|
|
1139
|
+
/** The answer a channel's `handle` gives for one webhook request. */
|
|
1140
|
+
interface WebhookAnswer {
|
|
1141
|
+
/** The HTTP status. */
|
|
1142
|
+
readonly status: number;
|
|
1143
|
+
/** The response body, when the platform expects one. */
|
|
1144
|
+
readonly body?: string;
|
|
1145
|
+
/** The response `content-type`. */
|
|
1146
|
+
readonly contentType?: string;
|
|
1109
1147
|
}
|
|
1110
1148
|
/**
|
|
1111
1149
|
* What `ingest` did with a verified payload: `"delivered"` dispatched it, `"malformed"` refused it
|
|
@@ -1128,7 +1166,22 @@ interface ChannelWebhook<TAdapter extends BasePlatformAdapter> {
|
|
|
1128
1166
|
verify(adapter: TAdapter, request: WebhookRequest): boolean;
|
|
1129
1167
|
/** Dispatch a parsed, verified payload, or report it malformed without dispatching. */
|
|
1130
1168
|
ingest(adapter: TAdapter, payload: unknown): Promise<IngestResult>;
|
|
1169
|
+
/** Methods the route takes. Default `["POST"]`. */
|
|
1170
|
+
readonly methods?: readonly string[];
|
|
1171
|
+
/** Full control of one request. When absent, the handler runs verify, JSON.parse and ingest. */
|
|
1172
|
+
handle?(adapter: TAdapter, request: WebhookRequest): Promise<WebhookAnswer>;
|
|
1131
1173
|
}
|
|
1174
|
+
/**
|
|
1175
|
+
* Whether a channel's `sender.id` is proven by the platform. An email From header or an SMS caller
|
|
1176
|
+
* id is written by the sender (CWE-290), so such a channel declares why it cannot be trusted.
|
|
1177
|
+
*
|
|
1178
|
+
* @public
|
|
1179
|
+
*/
|
|
1180
|
+
type SenderIdentity = "authenticated"
|
|
1181
|
+
/** Why the sender id can be forged, for error messages. */
|
|
1182
|
+
| {
|
|
1183
|
+
readonly unauthenticated: string;
|
|
1184
|
+
};
|
|
1132
1185
|
interface ChannelDescriptorBase<TAdapter extends BasePlatformAdapter> {
|
|
1133
1186
|
/** The platform the built adapter reports as its `platform`. */
|
|
1134
1187
|
readonly platform: PlatformName;
|
|
@@ -1139,6 +1192,16 @@ interface ChannelDescriptorBase<TAdapter extends BasePlatformAdapter> {
|
|
|
1139
1192
|
* which `createChannels` reports as a rejected channel; it performs no network call.
|
|
1140
1193
|
*/
|
|
1141
1194
|
create(config: Readonly<Record<string, unknown>>): TAdapter;
|
|
1195
|
+
/**
|
|
1196
|
+
* Whether `sender.id` is authenticated. Absent means unknown and is treated as not
|
|
1197
|
+
* authenticated (fail closed); it is optional only so descriptors in other packages keep compiling.
|
|
1198
|
+
*/
|
|
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>>;
|
|
1142
1205
|
}
|
|
1143
1206
|
/**
|
|
1144
1207
|
* A channel described as data. A `socket` channel opens its own connection to the platform; a
|
|
@@ -1153,6 +1216,42 @@ type ChannelDescriptor<TAdapter extends BasePlatformAdapter = BasePlatformAdapte
|
|
|
1153
1216
|
readonly transport: "webhook";
|
|
1154
1217
|
readonly webhook: ChannelWebhook<TAdapter>;
|
|
1155
1218
|
});
|
|
1219
|
+
/**
|
|
1220
|
+
* True only when the descriptor declares `senderIdentity: "authenticated"`. An absent or
|
|
1221
|
+
* unauthenticated declaration is false.
|
|
1222
|
+
*
|
|
1223
|
+
* @public
|
|
1224
|
+
*/
|
|
1225
|
+
declare function isAuthenticatedSender(descriptor: ChannelDescriptor): boolean;
|
|
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;
|
|
1156
1255
|
|
|
1157
1256
|
/**
|
|
1158
1257
|
* `GatewayConfigurationError` — shared base for per-adapter configuration
|
|
@@ -1325,6 +1424,35 @@ declare class GatewayLifecycleError extends Error {
|
|
|
1325
1424
|
constructor(opts: GatewayLifecycleErrorOptions);
|
|
1326
1425
|
}
|
|
1327
1426
|
|
|
1427
|
+
/**
|
|
1428
|
+
* File-backed pairing store: senders approved by an owner are kept in `pairing.json`.
|
|
1429
|
+
* Pending codes live in memory for one hour.
|
|
1430
|
+
*
|
|
1431
|
+
* @public
|
|
1432
|
+
*/
|
|
1433
|
+
/** Pairing store used by the access policy hook. */
|
|
1434
|
+
interface PairingStore {
|
|
1435
|
+
/** A 6-digit code for the sender; the same code while one is pending. */
|
|
1436
|
+
issue(platform: string, senderId: string): string;
|
|
1437
|
+
approve(code: string): Promise<{
|
|
1438
|
+
ok: true;
|
|
1439
|
+
platform: string;
|
|
1440
|
+
senderId: string;
|
|
1441
|
+
} | {
|
|
1442
|
+
ok: false;
|
|
1443
|
+
reason: "unknown_or_expired" | "write_failed";
|
|
1444
|
+
}>;
|
|
1445
|
+
isPaired(platform: string, senderId: string): boolean;
|
|
1446
|
+
}
|
|
1447
|
+
/** `<platform>:<senderId>`; email ids compare case-insensitively. */
|
|
1448
|
+
declare function senderKey(platform: string, senderId: string): string;
|
|
1449
|
+
/** Creates a store persisted in `<dir>/pairing.json` (mode 0600). */
|
|
1450
|
+
declare function createFilePairingStore(options: {
|
|
1451
|
+
dir: string;
|
|
1452
|
+
clock?: () => number;
|
|
1453
|
+
random?: (min: number, max: number) => number;
|
|
1454
|
+
}): PairingStore;
|
|
1455
|
+
|
|
1328
1456
|
/**
|
|
1329
1457
|
* Gateway hooks — own contract, not a `Plugin.kind` extension
|
|
1330
1458
|
* (T4.1, ADRs D176, D177).
|
|
@@ -1376,6 +1504,81 @@ interface GatewayHook {
|
|
|
1376
1504
|
on_error?(ctx: OnErrorContext): Promise<void> | void;
|
|
1377
1505
|
}
|
|
1378
1506
|
|
|
1507
|
+
/**
|
|
1508
|
+
* Access policy hook: who may talk to the agent, with sender pairing.
|
|
1509
|
+
*
|
|
1510
|
+
* @public
|
|
1511
|
+
*/
|
|
1512
|
+
|
|
1513
|
+
type DmPolicy = "pairing" | "allowlist" | "open" | "disabled";
|
|
1514
|
+
/** Options for {@link createAccessPolicyHook}. */
|
|
1515
|
+
interface AccessPolicyOptions {
|
|
1516
|
+
/** `<platform>:<senderId>` keys. */
|
|
1517
|
+
readonly owners: ReadonlySet<string>;
|
|
1518
|
+
readonly allowed?: ReadonlySet<string>;
|
|
1519
|
+
readonly dm: DmPolicy;
|
|
1520
|
+
/** Group messages from non-members: "silent" (default) or "open". */
|
|
1521
|
+
readonly groups?: "silent" | "open";
|
|
1522
|
+
/** Which platforms authenticate the sender. */
|
|
1523
|
+
readonly isAuthenticated: (platform: PlatformName) => boolean;
|
|
1524
|
+
/** Required when dm is "pairing". */
|
|
1525
|
+
readonly pairing?: PairingStore;
|
|
1526
|
+
readonly refusalText?: string;
|
|
1527
|
+
readonly refusalIntervalMs?: number;
|
|
1528
|
+
/** Let a message through untouched. */
|
|
1529
|
+
readonly passThrough?: (event: MessageEvent) => boolean;
|
|
1530
|
+
readonly clock?: () => number;
|
|
1531
|
+
readonly write?: (line: string) => void;
|
|
1532
|
+
}
|
|
1533
|
+
/** Builds the `pre_inbound` access policy hook. */
|
|
1534
|
+
declare function createAccessPolicyHook(options: AccessPolicyOptions): GatewayHook;
|
|
1535
|
+
|
|
1536
|
+
/**
|
|
1537
|
+
* Chat command parsing and a single `pre_inbound` hook for many commands.
|
|
1538
|
+
*
|
|
1539
|
+
* Recognises a command behind one leading mention (`<@U123> /help`,
|
|
1540
|
+
* `@bot /help`) and with Telegram's `@botname` suffix (`/help@my_bot`).
|
|
1541
|
+
* The name is matched exactly: `/new chat` is `/new` with arg `chat`,
|
|
1542
|
+
* `/newer` is never `/new`.
|
|
1543
|
+
*
|
|
1544
|
+
* @public
|
|
1545
|
+
*/
|
|
1546
|
+
|
|
1547
|
+
/** A parsed chat command. */
|
|
1548
|
+
interface ParsedChatCommand {
|
|
1549
|
+
/** Command name, without the prefix and without `@bot`. */
|
|
1550
|
+
readonly name: string;
|
|
1551
|
+
/** Trimmed rest of the text, `""` when none. */
|
|
1552
|
+
readonly arg: string;
|
|
1553
|
+
}
|
|
1554
|
+
/**
|
|
1555
|
+
* Strips one leading `<@U...>` or `@name` mention and a `@bot` suffix.
|
|
1556
|
+
* Returns `undefined` for text that is not a command.
|
|
1557
|
+
*/
|
|
1558
|
+
declare function parseChatCommand(text: string, options?: {
|
|
1559
|
+
prefixes?: ReadonlyArray<string>;
|
|
1560
|
+
}): ParsedChatCommand | undefined;
|
|
1561
|
+
/** One command handled by {@link commandHook}. */
|
|
1562
|
+
interface ChatCommand {
|
|
1563
|
+
readonly name: string;
|
|
1564
|
+
readonly description: string;
|
|
1565
|
+
/** Default `"optional"`. */
|
|
1566
|
+
readonly arg?: "none" | "optional" | "required";
|
|
1567
|
+
/** Usage line shown in help. Default `"/<name>"`. */
|
|
1568
|
+
readonly usage?: string;
|
|
1569
|
+
run(event: MessageEvent, arg: string): HookDecision | undefined | Promise<HookDecision | undefined>;
|
|
1570
|
+
}
|
|
1571
|
+
/** Help text listing every command's usage and description, in declaration order. */
|
|
1572
|
+
declare function helpText(commands: ReadonlyArray<ChatCommand>): string;
|
|
1573
|
+
/**
|
|
1574
|
+
* One `pre_inbound` hook for many commands. Also answers `/help`
|
|
1575
|
+
* (rename with `helpCommand`, disable with `false`).
|
|
1576
|
+
*/
|
|
1577
|
+
declare function commandHook(commands: ReadonlyArray<ChatCommand>, options?: {
|
|
1578
|
+
name?: string;
|
|
1579
|
+
helpCommand?: string | false;
|
|
1580
|
+
}): GatewayHook;
|
|
1581
|
+
|
|
1379
1582
|
/**
|
|
1380
1583
|
* `HookExecutor` — runs registered gateway hooks at their fire points.
|
|
1381
1584
|
*
|
|
@@ -1480,7 +1683,37 @@ interface GatewayRunnerOptions {
|
|
|
1480
1683
|
* @public
|
|
1481
1684
|
*/
|
|
1482
1685
|
readonly reconnect?: ReconnectPolicy;
|
|
1686
|
+
/**
|
|
1687
|
+
* Under `connectFailure: "isolate"`, give up on one adapter's `connect()` after this many ms. The
|
|
1688
|
+
* adapter is reported `timed_out`, counted in `StartResult.failed`, and never wired for inbound:
|
|
1689
|
+
* if its `connect()` resolves `true` later, the runner disconnects it. With `reconnect` set it is
|
|
1690
|
+
* retried like a transient failure. Ignored under `"rollback"`, as `reconnect` is.
|
|
1691
|
+
*
|
|
1692
|
+
* @public
|
|
1693
|
+
*/
|
|
1694
|
+
readonly connectTimeoutMs?: number;
|
|
1695
|
+
/**
|
|
1696
|
+
* Called as each adapter changes state, while `start()` may still be pending. `reason` is
|
|
1697
|
+
* redacted. A callback that throws is ignored. Ignored under `"rollback"`.
|
|
1698
|
+
*
|
|
1699
|
+
* @public
|
|
1700
|
+
*/
|
|
1701
|
+
readonly onAdapterState?: (platform: PlatformName, state: AdapterState, reason?: string) => void;
|
|
1702
|
+
/**
|
|
1703
|
+
* Exact credential values masked (longest first) in every reason and log line before
|
|
1704
|
+
* `redactSecrets` runs.
|
|
1705
|
+
*
|
|
1706
|
+
* @public
|
|
1707
|
+
*/
|
|
1708
|
+
readonly secrets?: ReadonlyArray<string>;
|
|
1483
1709
|
}
|
|
1710
|
+
/**
|
|
1711
|
+
* Where one adapter stands under `connectFailure: "isolate"`, as reported to
|
|
1712
|
+
* `GatewayRunnerOptions.onAdapterState`.
|
|
1713
|
+
*
|
|
1714
|
+
* @public
|
|
1715
|
+
*/
|
|
1716
|
+
type AdapterState = "connected" | "failed" | "retrying" | "timed_out";
|
|
1484
1717
|
/**
|
|
1485
1718
|
* Backoff for `GatewayRunnerOptions.reconnect`. The delay before attempt n is
|
|
1486
1719
|
* `min(initialDelayMs * factor^(n-1), maxDelayMs)`.
|
|
@@ -1537,7 +1770,14 @@ declare class GatewayRunner {
|
|
|
1537
1770
|
private startInFlight;
|
|
1538
1771
|
private stopped;
|
|
1539
1772
|
private readonly retryTimers;
|
|
1773
|
+
private readonly connectedPlatforms;
|
|
1774
|
+
private readonly redact;
|
|
1540
1775
|
constructor(opts: GatewayRunnerOptions);
|
|
1776
|
+
/** The registered adapter for a platform, connected or not. */
|
|
1777
|
+
adapter(platform: PlatformName): BasePlatformAdapter | undefined;
|
|
1778
|
+
/** Platforms whose adapter is connected now; updated by connects, retries and `stop()`. */
|
|
1779
|
+
get connected(): ReadonlySet<PlatformName>;
|
|
1780
|
+
private emitState;
|
|
1541
1781
|
/**
|
|
1542
1782
|
* Register a slash command. Match rule (EC-A): text === "/" + name
|
|
1543
1783
|
* OR text.startsWith("/" + name + " ") OR text.startsWith("/" + name + "@").
|
|
@@ -1587,6 +1827,11 @@ declare class GatewayRunner {
|
|
|
1587
1827
|
* `a => a.connect()` would let it escape `map` and reject `start()` itself.
|
|
1588
1828
|
*/
|
|
1589
1829
|
private connectIsolated;
|
|
1830
|
+
/** Connect one adapter, bounded by `connectTimeoutMs`, and report its state as it settles. */
|
|
1831
|
+
private connectOne;
|
|
1832
|
+
/** A connect that finished after the runner gave up on it must not stay connected unseen. */
|
|
1833
|
+
private releaseLateConnect;
|
|
1834
|
+
private classify;
|
|
1590
1835
|
private wireInbound;
|
|
1591
1836
|
/**
|
|
1592
1837
|
* Stop the runner. Drains in-flight handlers up to `drainTimeoutMs`,
|
|
@@ -1676,6 +1921,17 @@ declare function maskShapes(text: string): string;
|
|
|
1676
1921
|
* @returns the redacted string
|
|
1677
1922
|
*/
|
|
1678
1923
|
declare function redactSecrets(value: unknown): string;
|
|
1924
|
+
/**
|
|
1925
|
+
* Replace each exact value in `secrets` with `[redacted]`, longest value first so a value that
|
|
1926
|
+
* contains another is masked whole. Empty values are ignored. It does not run `redactSecrets`:
|
|
1927
|
+
* call that afterwards, because it shortens some shapes to a head and tail and the masked fragments
|
|
1928
|
+
* would otherwise survive.
|
|
1929
|
+
*
|
|
1930
|
+
* @param text the text to mask
|
|
1931
|
+
* @param secrets the exact credential values configured for this process
|
|
1932
|
+
* @returns the text with every occurrence of every value replaced
|
|
1933
|
+
*/
|
|
1934
|
+
declare function maskSecrets(text: string, secrets: ReadonlyArray<string>): string;
|
|
1679
1935
|
|
|
1680
1936
|
/**
|
|
1681
1937
|
* `SessionRouter` — pure routing logic: `MessageEvent → agentId`
|
|
@@ -1714,6 +1970,43 @@ declare class SessionRouter {
|
|
|
1714
1970
|
resolveAgentId(event: MessageEvent): string;
|
|
1715
1971
|
}
|
|
1716
1972
|
|
|
1973
|
+
/**
|
|
1974
|
+
* Rule-based agent bindings: pick the agent that answers a message by
|
|
1975
|
+
* platform, account and peer.
|
|
1976
|
+
*
|
|
1977
|
+
* @public
|
|
1978
|
+
*/
|
|
1979
|
+
|
|
1980
|
+
interface AgentBindingRule {
|
|
1981
|
+
readonly agent: string;
|
|
1982
|
+
readonly platform?: PlatformName;
|
|
1983
|
+
/** Matched against accountOf(event). */
|
|
1984
|
+
readonly account?: string;
|
|
1985
|
+
/** Matched against event.sender.id for a DM and event.channel.id otherwise. */
|
|
1986
|
+
readonly peer?: string;
|
|
1987
|
+
}
|
|
1988
|
+
/** Options for {@link createAgentBindings}. */
|
|
1989
|
+
interface AgentBindingsOptions {
|
|
1990
|
+
readonly defaultAgent: string;
|
|
1991
|
+
readonly rules: ReadonlyArray<AgentBindingRule>;
|
|
1992
|
+
/** Agents that exist; every rule and the default must name one. */
|
|
1993
|
+
readonly agents: ReadonlyArray<string>;
|
|
1994
|
+
/** Which bot account received the event, when the app runs several. Default: undefined. */
|
|
1995
|
+
readonly accountOf?: (event: MessageEvent) => string | undefined;
|
|
1996
|
+
}
|
|
1997
|
+
/** Chooses the agent that answers an event. */
|
|
1998
|
+
interface AgentBindings {
|
|
1999
|
+
agentFor(event: MessageEvent): string;
|
|
2000
|
+
}
|
|
2001
|
+
/**
|
|
2002
|
+
* Builds bindings from rules. The most specific matching rule wins (peer 4, account 2,
|
|
2003
|
+
* platform 1), ties go to the earlier rule, no match gives `defaultAgent`.
|
|
2004
|
+
* Throws `GatewayConfigurationError` for `unknown_agent` or `empty_binding_rule`.
|
|
2005
|
+
*/
|
|
2006
|
+
declare function createAgentBindings(options: AgentBindingsOptions): AgentBindings;
|
|
2007
|
+
/** Prefixes another strategy's key with the bound agent, so two agents never share a session. */
|
|
2008
|
+
declare function bindingStrategy(bindings: AgentBindings, inner: AgentIdStrategy): AgentIdStrategy;
|
|
2009
|
+
|
|
1717
2010
|
/**
|
|
1718
2011
|
* What `ChatPresenter` needs to know about one platform: how to rewrite the text, and which
|
|
1719
2012
|
* `format` to declare on the message (B-019 FR-001, FR-002).
|
|
@@ -1907,6 +2200,15 @@ declare function scanFences(text: string): readonly FenceRegion[];
|
|
|
1907
2200
|
*/
|
|
1908
2201
|
declare function splitKeepingRows(text: string, partLimit: number): string[];
|
|
1909
2202
|
|
|
2203
|
+
/** The outcome of delivering one event to the runner's handlers. */
|
|
2204
|
+
type DeliverOutcome = "ok" | "no_handler" | "handler_threw";
|
|
2205
|
+
/**
|
|
2206
|
+
* Map the outcomes of one delivered batch to an HTTP status, worst wins: `handler_threw` is 500,
|
|
2207
|
+
* `no_handler` is 503 so the provider retries, `ok` and an empty batch are 200. An unknown outcome
|
|
2208
|
+
* throws instead of answering 200.
|
|
2209
|
+
*/
|
|
2210
|
+
declare function statusForDeliver(outcomes: ReadonlyArray<DeliverOutcome>): 200 | 500 | 503;
|
|
2211
|
+
|
|
1910
2212
|
/**
|
|
1911
2213
|
* The half of an Express webhook server that is the same for every platform.
|
|
1912
2214
|
*
|
|
@@ -2017,4 +2319,31 @@ declare function listenerLifecycle(opts: {
|
|
|
2017
2319
|
stop(): Promise<void>;
|
|
2018
2320
|
};
|
|
2019
2321
|
|
|
2020
|
-
|
|
2322
|
+
/**
|
|
2323
|
+
* `createWebhookHandler` — one Fetch `Request` to `Response` handler for every webhook channel.
|
|
2324
|
+
*
|
|
2325
|
+
* It routes by platform, caps the body, and answers with the descriptor's `handle`, or runs verify,
|
|
2326
|
+
* JSON.parse and ingest. No secret, header value or body reaches `onError` or a response.
|
|
2327
|
+
*/
|
|
2328
|
+
|
|
2329
|
+
/** Options for `createWebhookHandler`. */
|
|
2330
|
+
interface WebhookHandlerOptions {
|
|
2331
|
+
/** The built channels; only those with `transport: "webhook"` are routed. */
|
|
2332
|
+
readonly channels: ReadonlyArray<BuiltChannel>;
|
|
2333
|
+
/** Whether the platform's runner has started; a platform that has not answers 503. */
|
|
2334
|
+
readonly isStarted: (platform: PlatformName) => boolean;
|
|
2335
|
+
/** Body cap in bytes. Default 1 MiB. */
|
|
2336
|
+
readonly maxBodyBytes?: number;
|
|
2337
|
+
/** Default: the last path segment after `/webhooks/`. */
|
|
2338
|
+
readonly platformFor?: (url: URL) => PlatformName | undefined;
|
|
2339
|
+
/** Called when a handle/ingest throws; the response is 500 with no body. */
|
|
2340
|
+
readonly onError?: (platform: PlatformName, error: unknown) => void;
|
|
2341
|
+
}
|
|
2342
|
+
/**
|
|
2343
|
+
* Build a Fetch handler for every `transport: "webhook"` channel in `channels`: 404 for an unknown
|
|
2344
|
+
* platform, 503 while it has not started, 405 for a method the route does not take, 413 past the
|
|
2345
|
+
* body cap, 400 for an unreadable body, and 500 with no body when a route throws.
|
|
2346
|
+
*/
|
|
2347
|
+
declare function createWebhookHandler(options: WebhookHandlerOptions): (request: Request) => Promise<Response>;
|
|
2348
|
+
|
|
2349
|
+
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 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, 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, selectVariant, senderKey, splitKeepingRows, statusForDeliver, toDialect, withMediaUnderstanding };
|