@theokit/gateway-whatsapp 0.6.0 → 0.8.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
@@ -1,5 +1,5 @@
1
1
  import type { ChildProcess } from 'node:child_process';
2
- import { GatewayConfigurationError, GatewayConfigurationErrorOptions, BasePlatformAdapter, OutboundMessage, SendResult, MessageEvent, WhatsAppMessageEvent } from '@theokit/gateway';
2
+ import { GatewayConfigurationError, GatewayConfigurationErrorOptions, BasePlatformAdapter, AdapterCapabilities, OutboundMessage, SendResult, MessageEvent, WhatsAppMessageEvent } from '@theokit/gateway';
3
3
  export { WhatsAppMessageEvent } from '@theokit/gateway';
4
4
 
5
5
  /** Inbound webhook payload — the full envelope Meta POSTs. */
@@ -401,6 +401,7 @@ interface WhatsAppAdapterCommonOptions {
401
401
  * constructor stays available for tests and for a backend of your own.
402
402
  */
403
403
  declare class WhatsAppAdapter extends BasePlatformAdapter {
404
+ get capabilities(): AdapterCapabilities;
404
405
  readonly platform: "whatsapp";
405
406
  private readonly backendImpl;
406
407
  /**
@@ -1288,6 +1289,62 @@ declare class LineBuffer {
1288
1289
  get pending(): string;
1289
1290
  }
1290
1291
 
1292
+ /**
1293
+ * `whatsappChannel` — the WhatsApp channel described as data.
1294
+ *
1295
+ * Typed locally on purpose: this package declares `@theokit/gateway` as a peer, and a published
1296
+ * `.d.ts` naming a core type an older release lacks would break a consumer on that floor. The object
1297
+ * is structurally a `ChannelDescriptor`; the integration package assigns it to one.
1298
+ *
1299
+ * Two variants pick the backend: `cloud` (Meta Cloud API) and `baileys` (WhatsApp Web multi-device).
1300
+ * The parent descriptor itself describes the cloud shape; `selectVariant(whatsappChannel, "baileys")`
1301
+ * picks the other.
1302
+ */
1303
+
1304
+ type Config = Readonly<Record<string, unknown>>;
1305
+ /** One configuration field, shaped like the core's `ChannelConfigField` (typed locally). */
1306
+ interface ConfigField {
1307
+ readonly key: string;
1308
+ readonly required: boolean;
1309
+ readonly secret: boolean;
1310
+ readonly description: string;
1311
+ readonly env?: string;
1312
+ }
1313
+ declare function createCloud(config: Config): WhatsAppAdapter;
1314
+ declare function createBaileys(config: Config): WhatsAppAdapter;
1315
+ /**
1316
+ * The WhatsApp channel. The descriptor itself is the `cloud` shape; `variants.cloud` and
1317
+ * `variants.baileys` are the two backends, chosen with `selectVariant`.
1318
+ *
1319
+ * `create` refuses a value of the wrong type with this package's `ConfigurationError`
1320
+ * (`invalid_config`) and performs no network call.
1321
+ *
1322
+ * @public
1323
+ */
1324
+ declare const whatsappChannel: {
1325
+ variants: {
1326
+ cloud: {
1327
+ platform: "whatsapp";
1328
+ transport: "socket";
1329
+ senderIdentity: "authenticated";
1330
+ config: readonly ConfigField[];
1331
+ create: typeof createCloud;
1332
+ };
1333
+ baileys: {
1334
+ platform: "whatsapp";
1335
+ transport: "socket";
1336
+ senderIdentity: "authenticated";
1337
+ config: readonly ConfigField[];
1338
+ create: typeof createBaileys;
1339
+ };
1340
+ };
1341
+ platform: "whatsapp";
1342
+ transport: "socket";
1343
+ senderIdentity: "authenticated";
1344
+ config: readonly ConfigField[];
1345
+ create: typeof createCloud;
1346
+ };
1347
+
1291
1348
  /**
1292
1349
  * Per-backend HTTP/IPC error → canonical `WhatsAppSendResult.error` shape.
1293
1350
  *
@@ -1361,18 +1418,7 @@ declare function mapWhatsAppCloudError(status: number, body: unknown): ErrorPayl
1361
1418
  */
1362
1419
  declare function mapWhatsAppWebError(ipcError: string | undefined): ErrorPayload;
1363
1420
 
1364
- /**
1365
- * Split a long string into ≤4096-char chunks for WhatsApp messages (ADR D310).
1366
- *
1367
- * Thin wrapper over core `chunkText` (roadmap M1). WhatsApp family: fixed 4096
1368
- * window, `\n\n` → `\n` → ` ` boundary preference, UTF-16 surrogate-pair guard,
1369
- * leading-whitespace strip. `trimParts` reproduces EC-8 (empty parts filtered
1370
- * so consecutive newlines don't yield empty `sendMessage` calls Meta rejects).
1371
- * Output is byte-identical to the previous implementation (pinned by
1372
- * `tests/split.test.ts`).
1373
- *
1374
- * @internal
1375
- */
1421
+ /** Split `text` into chunks of at most `WHATSAPP_MAX_TEXT` characters. */
1376
1422
  declare function splitForWhatsApp(text: string): string[];
1377
1423
 
1378
- export { type BaileysSocketFactory, type BaileysSocketLike, type BaileysSocketOptions, ConfigurationError, type ConfigurationErrorOptions, type IpcCommand, type IpcEvent, LineBuffer, type MetaTemplateComponent, type MetaWebhookEnvelope, WhatsAppAdapter, type WhatsAppAdapterCommonOptions, type WhatsAppAdapterOptions, type WhatsAppBackend, WhatsAppBaileysBackend, type WhatsAppBaileysBackendOptions, type WhatsAppBaileysConfig, WhatsAppBridgeError, WhatsAppCloudBackend, type WhatsAppCloudBackendOptions, type WhatsAppCloudConfig, WhatsAppConnectTimeoutError, type WhatsAppInboundEvent, type WhatsAppOutboundMessage, type WhatsAppPairingState, type WhatsAppSendResult, type WhatsAppStatusReceipt, WhatsAppWebBackend, type WhatsAppWebBackendOptions, type WhatsAppWebConfig, createBaileysSocket, defaultBridgeScriptPath, digitsOnly, formatCommand, mapWhatsAppCloudError, mapWhatsAppWebError, normalizeInboundMessages, normalizeStatusReceipts, parseEvent, parseWebhookPayload, splitForWhatsApp, verifyWebhookSignature, verifyWebhookSubscription };
1424
+ export { type BaileysSocketFactory, type BaileysSocketLike, type BaileysSocketOptions, ConfigurationError, type ConfigurationErrorOptions, type IpcCommand, type IpcEvent, LineBuffer, type MetaTemplateComponent, type MetaWebhookEnvelope, WhatsAppAdapter, type WhatsAppAdapterCommonOptions, type WhatsAppAdapterOptions, type WhatsAppBackend, WhatsAppBaileysBackend, type WhatsAppBaileysBackendOptions, type WhatsAppBaileysConfig, WhatsAppBridgeError, WhatsAppCloudBackend, type WhatsAppCloudBackendOptions, type WhatsAppCloudConfig, WhatsAppConnectTimeoutError, type WhatsAppInboundEvent, type WhatsAppOutboundMessage, type WhatsAppPairingState, type WhatsAppSendResult, type WhatsAppStatusReceipt, WhatsAppWebBackend, type WhatsAppWebBackendOptions, type WhatsAppWebConfig, createBaileysSocket, defaultBridgeScriptPath, digitsOnly, formatCommand, mapWhatsAppCloudError, mapWhatsAppWebError, normalizeInboundMessages, normalizeStatusReceipts, parseEvent, parseWebhookPayload, splitForWhatsApp, verifyWebhookSignature, verifyWebhookSubscription, whatsappChannel };
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import type { ChildProcess } from 'node:child_process';
2
- import { GatewayConfigurationError, GatewayConfigurationErrorOptions, BasePlatformAdapter, OutboundMessage, SendResult, MessageEvent, WhatsAppMessageEvent } from '@theokit/gateway';
2
+ import { GatewayConfigurationError, GatewayConfigurationErrorOptions, BasePlatformAdapter, AdapterCapabilities, OutboundMessage, SendResult, MessageEvent, WhatsAppMessageEvent } from '@theokit/gateway';
3
3
  export { WhatsAppMessageEvent } from '@theokit/gateway';
4
4
 
5
5
  /** Inbound webhook payload — the full envelope Meta POSTs. */
@@ -401,6 +401,7 @@ interface WhatsAppAdapterCommonOptions {
401
401
  * constructor stays available for tests and for a backend of your own.
402
402
  */
403
403
  declare class WhatsAppAdapter extends BasePlatformAdapter {
404
+ get capabilities(): AdapterCapabilities;
404
405
  readonly platform: "whatsapp";
405
406
  private readonly backendImpl;
406
407
  /**
@@ -1288,6 +1289,62 @@ declare class LineBuffer {
1288
1289
  get pending(): string;
1289
1290
  }
1290
1291
 
1292
+ /**
1293
+ * `whatsappChannel` — the WhatsApp channel described as data.
1294
+ *
1295
+ * Typed locally on purpose: this package declares `@theokit/gateway` as a peer, and a published
1296
+ * `.d.ts` naming a core type an older release lacks would break a consumer on that floor. The object
1297
+ * is structurally a `ChannelDescriptor`; the integration package assigns it to one.
1298
+ *
1299
+ * Two variants pick the backend: `cloud` (Meta Cloud API) and `baileys` (WhatsApp Web multi-device).
1300
+ * The parent descriptor itself describes the cloud shape; `selectVariant(whatsappChannel, "baileys")`
1301
+ * picks the other.
1302
+ */
1303
+
1304
+ type Config = Readonly<Record<string, unknown>>;
1305
+ /** One configuration field, shaped like the core's `ChannelConfigField` (typed locally). */
1306
+ interface ConfigField {
1307
+ readonly key: string;
1308
+ readonly required: boolean;
1309
+ readonly secret: boolean;
1310
+ readonly description: string;
1311
+ readonly env?: string;
1312
+ }
1313
+ declare function createCloud(config: Config): WhatsAppAdapter;
1314
+ declare function createBaileys(config: Config): WhatsAppAdapter;
1315
+ /**
1316
+ * The WhatsApp channel. The descriptor itself is the `cloud` shape; `variants.cloud` and
1317
+ * `variants.baileys` are the two backends, chosen with `selectVariant`.
1318
+ *
1319
+ * `create` refuses a value of the wrong type with this package's `ConfigurationError`
1320
+ * (`invalid_config`) and performs no network call.
1321
+ *
1322
+ * @public
1323
+ */
1324
+ declare const whatsappChannel: {
1325
+ variants: {
1326
+ cloud: {
1327
+ platform: "whatsapp";
1328
+ transport: "socket";
1329
+ senderIdentity: "authenticated";
1330
+ config: readonly ConfigField[];
1331
+ create: typeof createCloud;
1332
+ };
1333
+ baileys: {
1334
+ platform: "whatsapp";
1335
+ transport: "socket";
1336
+ senderIdentity: "authenticated";
1337
+ config: readonly ConfigField[];
1338
+ create: typeof createBaileys;
1339
+ };
1340
+ };
1341
+ platform: "whatsapp";
1342
+ transport: "socket";
1343
+ senderIdentity: "authenticated";
1344
+ config: readonly ConfigField[];
1345
+ create: typeof createCloud;
1346
+ };
1347
+
1291
1348
  /**
1292
1349
  * Per-backend HTTP/IPC error → canonical `WhatsAppSendResult.error` shape.
1293
1350
  *
@@ -1361,18 +1418,7 @@ declare function mapWhatsAppCloudError(status: number, body: unknown): ErrorPayl
1361
1418
  */
1362
1419
  declare function mapWhatsAppWebError(ipcError: string | undefined): ErrorPayload;
1363
1420
 
1364
- /**
1365
- * Split a long string into ≤4096-char chunks for WhatsApp messages (ADR D310).
1366
- *
1367
- * Thin wrapper over core `chunkText` (roadmap M1). WhatsApp family: fixed 4096
1368
- * window, `\n\n` → `\n` → ` ` boundary preference, UTF-16 surrogate-pair guard,
1369
- * leading-whitespace strip. `trimParts` reproduces EC-8 (empty parts filtered
1370
- * so consecutive newlines don't yield empty `sendMessage` calls Meta rejects).
1371
- * Output is byte-identical to the previous implementation (pinned by
1372
- * `tests/split.test.ts`).
1373
- *
1374
- * @internal
1375
- */
1421
+ /** Split `text` into chunks of at most `WHATSAPP_MAX_TEXT` characters. */
1376
1422
  declare function splitForWhatsApp(text: string): string[];
1377
1423
 
1378
- export { type BaileysSocketFactory, type BaileysSocketLike, type BaileysSocketOptions, ConfigurationError, type ConfigurationErrorOptions, type IpcCommand, type IpcEvent, LineBuffer, type MetaTemplateComponent, type MetaWebhookEnvelope, WhatsAppAdapter, type WhatsAppAdapterCommonOptions, type WhatsAppAdapterOptions, type WhatsAppBackend, WhatsAppBaileysBackend, type WhatsAppBaileysBackendOptions, type WhatsAppBaileysConfig, WhatsAppBridgeError, WhatsAppCloudBackend, type WhatsAppCloudBackendOptions, type WhatsAppCloudConfig, WhatsAppConnectTimeoutError, type WhatsAppInboundEvent, type WhatsAppOutboundMessage, type WhatsAppPairingState, type WhatsAppSendResult, type WhatsAppStatusReceipt, WhatsAppWebBackend, type WhatsAppWebBackendOptions, type WhatsAppWebConfig, createBaileysSocket, defaultBridgeScriptPath, digitsOnly, formatCommand, mapWhatsAppCloudError, mapWhatsAppWebError, normalizeInboundMessages, normalizeStatusReceipts, parseEvent, parseWebhookPayload, splitForWhatsApp, verifyWebhookSignature, verifyWebhookSubscription };
1424
+ export { type BaileysSocketFactory, type BaileysSocketLike, type BaileysSocketOptions, ConfigurationError, type ConfigurationErrorOptions, type IpcCommand, type IpcEvent, LineBuffer, type MetaTemplateComponent, type MetaWebhookEnvelope, WhatsAppAdapter, type WhatsAppAdapterCommonOptions, type WhatsAppAdapterOptions, type WhatsAppBackend, WhatsAppBaileysBackend, type WhatsAppBaileysBackendOptions, type WhatsAppBaileysConfig, WhatsAppBridgeError, WhatsAppCloudBackend, type WhatsAppCloudBackendOptions, type WhatsAppCloudConfig, WhatsAppConnectTimeoutError, type WhatsAppInboundEvent, type WhatsAppOutboundMessage, type WhatsAppPairingState, type WhatsAppSendResult, type WhatsAppStatusReceipt, WhatsAppWebBackend, type WhatsAppWebBackendOptions, type WhatsAppWebConfig, createBaileysSocket, defaultBridgeScriptPath, digitsOnly, formatCommand, mapWhatsAppCloudError, mapWhatsAppWebError, normalizeInboundMessages, normalizeStatusReceipts, parseEvent, parseWebhookPayload, splitForWhatsApp, verifyWebhookSignature, verifyWebhookSubscription, whatsappChannel };
package/dist/index.js CHANGED
@@ -1844,6 +1844,9 @@ function requireNonEmpty(entries) {
1844
1844
  }
1845
1845
  }
1846
1846
  var WhatsAppAdapter = class _WhatsAppAdapter extends BasePlatformAdapter {
1847
+ get capabilities() {
1848
+ return { ...super.capabilities, maxMessageChars: WHATSAPP_MAX_TEXT, threads: false };
1849
+ }
1847
1850
  platform = "whatsapp";
1848
1851
  backendImpl;
1849
1852
  /**
@@ -1912,14 +1915,14 @@ var WhatsAppAdapter = class _WhatsAppAdapter extends BasePlatformAdapter {
1912
1915
  * @throws {ConfigurationError} when a required option is missing or empty.
1913
1916
  * @public
1914
1917
  */
1915
- static from(options, common = {}) {
1918
+ static from(options, common2 = {}) {
1916
1919
  switch (options.backend) {
1917
1920
  case "cloud":
1918
- return _WhatsAppAdapter.fromCloud(options.cloud, common);
1921
+ return _WhatsAppAdapter.fromCloud(options.cloud, common2);
1919
1922
  case "web":
1920
- return _WhatsAppAdapter.fromWeb(options.web, common);
1923
+ return _WhatsAppAdapter.fromWeb(options.web, common2);
1921
1924
  case "baileys":
1922
- return _WhatsAppAdapter.fromBaileys(options.baileys, common);
1925
+ return _WhatsAppAdapter.fromBaileys(options.baileys, common2);
1923
1926
  }
1924
1927
  }
1925
1928
  /**
@@ -2168,6 +2171,125 @@ var WhatsAppAdapter = class _WhatsAppAdapter extends BasePlatformAdapter {
2168
2171
  }
2169
2172
  };
2170
2173
 
2171
- export { ConfigurationError, LineBuffer, WhatsAppAdapter, WhatsAppBaileysBackend, WhatsAppBridgeError, WhatsAppCloudBackend, WhatsAppConnectTimeoutError, WhatsAppWebBackend, createBaileysSocket, defaultBridgeScriptPath, digitsOnly, formatCommand, mapWhatsAppCloudError, mapWhatsAppWebError, normalizeInboundMessages, normalizeStatusReceipts, parseEvent, parseWebhookPayload, splitForWhatsApp, verifyWebhookSignature, verifyWebhookSubscription };
2174
+ // src/channel.ts
2175
+ function invalid(key, expected) {
2176
+ return new ConfigurationError({
2177
+ code: "invalid_config",
2178
+ message: `gateway-whatsapp: config.${key} must be ${expected}`,
2179
+ detail: key
2180
+ });
2181
+ }
2182
+ function read(config, key, kind) {
2183
+ const value = config[key];
2184
+ if (value === void 0 || value === null || value === "") return void 0;
2185
+ if (kind === "string" && typeof value === "string") return value;
2186
+ if (kind === "boolean" && typeof value === "boolean") return value;
2187
+ if (kind === "boolean" && (value === "true" || value === "false")) return value === "true";
2188
+ throw invalid(key, kind === "string" ? "a string" : 'a boolean or the string "true" or "false"');
2189
+ }
2190
+ function requiredString(config, key) {
2191
+ const value = config[key] === "" ? "" : read(config, key, "string");
2192
+ if (typeof value !== "string") throw invalid(key, "a string");
2193
+ return value;
2194
+ }
2195
+ var REQUIRE_MENTION = {
2196
+ key: "requireMention",
2197
+ required: false,
2198
+ secret: false,
2199
+ env: "WHATSAPP_REQUIRE_MENTION",
2200
+ description: "Whether group messages must mention the bot. Default true."
2201
+ };
2202
+ var CLOUD_CONFIG = [
2203
+ {
2204
+ key: "accessToken",
2205
+ required: true,
2206
+ secret: true,
2207
+ env: "WHATSAPP_ACCESS_TOKEN",
2208
+ description: "The Meta Cloud API access token."
2209
+ },
2210
+ {
2211
+ key: "phoneNumberId",
2212
+ required: true,
2213
+ secret: false,
2214
+ env: "WHATSAPP_PHONE_NUMBER_ID",
2215
+ description: "The Cloud API phone number id the adapter answers for."
2216
+ },
2217
+ {
2218
+ key: "appSecret",
2219
+ required: true,
2220
+ secret: true,
2221
+ env: "WHATSAPP_APP_SECRET",
2222
+ description: "The Meta app secret that signs inbound webhooks."
2223
+ },
2224
+ {
2225
+ key: "verifyToken",
2226
+ required: false,
2227
+ secret: true,
2228
+ env: "WHATSAPP_VERIFY_TOKEN",
2229
+ description: "The token Meta's webhook subscription check sends; not an adapter option, kept so the webhook half can answer it."
2230
+ },
2231
+ REQUIRE_MENTION
2232
+ ];
2233
+ var BAILEYS_CONFIG = [
2234
+ {
2235
+ key: "sessionDir",
2236
+ required: true,
2237
+ secret: false,
2238
+ env: "WHATSAPP_SESSION_DIR",
2239
+ description: "Directory where the Baileys session (credentials) is stored."
2240
+ },
2241
+ {
2242
+ key: "botPhoneId",
2243
+ required: false,
2244
+ secret: false,
2245
+ env: "WHATSAPP_BOT_PHONE_ID",
2246
+ description: "The bot's own number; with requireMention (default), a group message must mention it or is dropped."
2247
+ },
2248
+ REQUIRE_MENTION
2249
+ ];
2250
+ function common(config) {
2251
+ const requireMention = read(config, "requireMention", "boolean");
2252
+ const botPhoneId = read(config, "botPhoneId", "string");
2253
+ return {
2254
+ ...requireMention === void 0 ? {} : { requireMention },
2255
+ ...botPhoneId === void 0 ? {} : { botPhoneId }
2256
+ };
2257
+ }
2258
+ function createCloud(config) {
2259
+ const accessToken = requiredString(config, "accessToken");
2260
+ const phoneNumberId = requiredString(config, "phoneNumberId");
2261
+ const appSecret = requiredString(config, "appSecret");
2262
+ read(config, "verifyToken", "string");
2263
+ const opts = common(config);
2264
+ return WhatsAppAdapter.from(
2265
+ { backend: "cloud", cloud: { accessToken, phoneNumberId, appSecret } },
2266
+ opts
2267
+ );
2268
+ }
2269
+ function createBaileys(config) {
2270
+ const sessionDir = requiredString(config, "sessionDir");
2271
+ const opts = common(config);
2272
+ return WhatsAppAdapter.from({ backend: "baileys", baileys: { sessionDir } }, opts);
2273
+ }
2274
+ var cloudChannel = {
2275
+ platform: "whatsapp",
2276
+ transport: "socket",
2277
+ senderIdentity: "authenticated",
2278
+ config: CLOUD_CONFIG,
2279
+ create: createCloud
2280
+ };
2281
+ var baileysChannel = {
2282
+ platform: "whatsapp",
2283
+ transport: "socket",
2284
+ senderIdentity: "authenticated",
2285
+ config: BAILEYS_CONFIG,
2286
+ create: createBaileys
2287
+ };
2288
+ var whatsappChannel = {
2289
+ ...cloudChannel,
2290
+ variants: { cloud: cloudChannel, baileys: baileysChannel }
2291
+ };
2292
+
2293
+ export { ConfigurationError, LineBuffer, WhatsAppAdapter, WhatsAppBaileysBackend, WhatsAppBridgeError, WhatsAppCloudBackend, WhatsAppConnectTimeoutError, WhatsAppWebBackend, createBaileysSocket, defaultBridgeScriptPath, digitsOnly, formatCommand, mapWhatsAppCloudError, mapWhatsAppWebError, normalizeInboundMessages, normalizeStatusReceipts, parseEvent, parseWebhookPayload, splitForWhatsApp, verifyWebhookSignature, verifyWebhookSubscription, whatsappChannel };
2172
2294
  //# sourceMappingURL=index.js.map
2173
2295
  //# sourceMappingURL=index.js.map