@kyyinfinite/zapo-message 0.0.0-stage → 1.0.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.
Files changed (191) hide show
  1. package/CHANGELOG.md +159 -0
  2. package/LICENSE +20 -0
  3. package/README.md +532 -2
  4. package/build.config.json +28 -0
  5. package/dist/airich/airich.d.ts +180 -0
  6. package/dist/airich/airich.js +548 -0
  7. package/dist/airich/context.d.ts +24 -0
  8. package/dist/airich/context.js +48 -0
  9. package/dist/airich/encode.d.ts +3 -0
  10. package/dist/airich/encode.js +16 -0
  11. package/dist/airich/index.d.ts +20 -0
  12. package/dist/airich/index.js +11 -0
  13. package/dist/airich/inline.d.ts +45 -0
  14. package/dist/airich/inline.js +152 -0
  15. package/dist/airich/nodes.d.ts +30 -0
  16. package/dist/airich/nodes.js +121 -0
  17. package/dist/airich/primitives.d.ts +144 -0
  18. package/dist/airich/primitives.js +230 -0
  19. package/dist/airich/protocol.d.ts +35 -0
  20. package/dist/airich/protocol.js +65 -0
  21. package/dist/airich/table.d.ts +22 -0
  22. package/dist/airich/table.js +35 -0
  23. package/dist/airich/tokenizer.d.ts +13 -0
  24. package/dist/airich/tokenizer.js +717 -0
  25. package/dist/airich/transport.d.ts +15 -0
  26. package/dist/airich/transport.js +56 -0
  27. package/dist/branding.d.ts +5 -0
  28. package/dist/branding.js +9 -0
  29. package/dist/btn/builders.d.ts +246 -0
  30. package/dist/btn/builders.js +710 -0
  31. package/dist/btn/media-slot.d.ts +28 -0
  32. package/dist/btn/media-slot.js +323 -0
  33. package/dist/btn/media-source.d.ts +38 -0
  34. package/dist/btn/media-source.js +116 -0
  35. package/dist/btn/sharp-loader.d.ts +5 -0
  36. package/dist/btn/sharp-loader.js +18 -0
  37. package/dist/btn/thumbnail.d.ts +21 -0
  38. package/dist/btn/thumbnail.js +124 -0
  39. package/dist/btn/validators.d.ts +40 -0
  40. package/dist/btn/validators.js +135 -0
  41. package/dist/btn.d.ts +1 -0
  42. package/dist/btn.js +1 -0
  43. package/dist/core/async-send.d.ts +20 -0
  44. package/dist/core/async-send.js +1 -0
  45. package/dist/core/base-builder.d.ts +20 -0
  46. package/dist/core/base-builder.js +67 -0
  47. package/dist/core/errors.d.ts +30 -0
  48. package/dist/core/errors.js +61 -0
  49. package/dist/download.d.ts +24 -0
  50. package/dist/download.js +123 -0
  51. package/dist/index.d.ts +38 -0
  52. package/dist/index.js +22 -0
  53. package/dist/integration/augment.d.ts +97 -0
  54. package/dist/integration/augment.js +1 -0
  55. package/dist/integration/auto.d.ts +8 -0
  56. package/dist/integration/auto.js +79 -0
  57. package/dist/integration/index.d.ts +4 -0
  58. package/dist/integration/index.js +2 -0
  59. package/dist/integration/methods.d.ts +25 -0
  60. package/dist/integration/methods.js +24 -0
  61. package/dist/interactive.d.ts +24 -0
  62. package/dist/interactive.js +121 -0
  63. package/dist/media-gate.d.ts +99 -0
  64. package/dist/media-gate.js +322 -0
  65. package/dist/message/album.d.ts +33 -0
  66. package/dist/message/album.js +107 -0
  67. package/dist/message/contact.d.ts +29 -0
  68. package/dist/message/contact.js +123 -0
  69. package/dist/message/forward.d.ts +23 -0
  70. package/dist/message/forward.js +77 -0
  71. package/dist/message/index.d.ts +26 -0
  72. package/dist/message/index.js +13 -0
  73. package/dist/message/link-preview.d.ts +28 -0
  74. package/dist/message/link-preview.js +98 -0
  75. package/dist/message/location.d.ts +24 -0
  76. package/dist/message/location.js +94 -0
  77. package/dist/message/media.d.ts +90 -0
  78. package/dist/message/media.js +123 -0
  79. package/dist/message/poll.d.ts +24 -0
  80. package/dist/message/poll.js +98 -0
  81. package/dist/message/reaction.d.ts +20 -0
  82. package/dist/message/reaction.js +71 -0
  83. package/dist/message/sticker.d.ts +29 -0
  84. package/dist/message/sticker.js +44 -0
  85. package/dist/message/target.d.ts +18 -0
  86. package/dist/message/target.js +24 -0
  87. package/dist/message/text.d.ts +17 -0
  88. package/dist/message/text.js +29 -0
  89. package/dist/message/vcard.d.ts +9 -0
  90. package/dist/message/vcard.js +23 -0
  91. package/dist/message/webp.d.ts +6 -0
  92. package/dist/message/webp.js +57 -0
  93. package/dist/payloads.d.ts +180 -0
  94. package/dist/payloads.js +1 -0
  95. package/dist/serialize.d.ts +41 -0
  96. package/dist/serialize.js +93 -0
  97. package/dist/status.d.ts +3 -0
  98. package/dist/status.js +21 -0
  99. package/dist/zapo-client.d.ts +44 -0
  100. package/dist/zapo-client.js +1 -0
  101. package/examples/airich.ts +59 -0
  102. package/examples/buttons.ts +48 -0
  103. package/examples/client-methods.ts +58 -0
  104. package/examples/hybrid.ts +133 -0
  105. package/examples/serialize.ts +61 -0
  106. package/package.json +271 -4
  107. package/scripts/check-no-comments.mjs +101 -0
  108. package/scripts/compare-golden.mjs +461 -0
  109. package/scripts/lib-config.mjs +9 -0
  110. package/scripts/smoke-auto.mjs +75 -0
  111. package/scripts/test-external-consumer.mjs +316 -0
  112. package/scripts/verify-package.mjs +78 -0
  113. package/src/airich/airich.ts +970 -0
  114. package/src/airich/context.ts +96 -0
  115. package/src/airich/encode.ts +25 -0
  116. package/src/airich/index.ts +98 -0
  117. package/src/airich/inline.ts +272 -0
  118. package/src/airich/nodes.ts +174 -0
  119. package/src/airich/primitives.ts +474 -0
  120. package/src/airich/protocol.ts +133 -0
  121. package/src/airich/table.ts +70 -0
  122. package/src/airich/tokenizer.ts +799 -0
  123. package/src/airich/transport.ts +106 -0
  124. package/src/branding.ts +23 -0
  125. package/src/btn/builders.ts +1073 -0
  126. package/src/btn/media-slot.ts +486 -0
  127. package/src/btn/media-source.ts +189 -0
  128. package/src/btn/sharp-loader.ts +23 -0
  129. package/src/btn/thumbnail.ts +182 -0
  130. package/src/btn/validators.ts +237 -0
  131. package/src/btn.ts +8 -0
  132. package/src/core/async-send.ts +28 -0
  133. package/src/core/base-builder.ts +104 -0
  134. package/src/core/errors.ts +82 -0
  135. package/src/download.ts +212 -0
  136. package/src/index.ts +229 -0
  137. package/src/integration/augment.ts +262 -0
  138. package/src/integration/auto.ts +103 -0
  139. package/src/integration/index.ts +8 -0
  140. package/src/integration/methods.ts +45 -0
  141. package/src/interactive.ts +188 -0
  142. package/src/media-gate.ts +499 -0
  143. package/src/message/album.ts +187 -0
  144. package/src/message/contact.ts +170 -0
  145. package/src/message/forward.ts +123 -0
  146. package/src/message/index.ts +83 -0
  147. package/src/message/link-preview.ts +148 -0
  148. package/src/message/location.ts +150 -0
  149. package/src/message/media.ts +323 -0
  150. package/src/message/poll.ts +158 -0
  151. package/src/message/reaction.ts +133 -0
  152. package/src/message/sticker.ts +100 -0
  153. package/src/message/target.ts +38 -0
  154. package/src/message/text.ts +61 -0
  155. package/src/message/vcard.ts +36 -0
  156. package/src/message/webp.ts +78 -0
  157. package/src/payloads.ts +204 -0
  158. package/src/serialize.ts +180 -0
  159. package/src/status.ts +29 -0
  160. package/src/zapo-client.ts +84 -0
  161. package/test/airich-inline.test.ts +200 -0
  162. package/test/airich-primitives.test.ts +226 -0
  163. package/test/airich-tokenizer.test.ts +145 -0
  164. package/test/airich.golden.test.ts +406 -0
  165. package/test/airich.test.ts +1006 -0
  166. package/test/btn-button.test.ts +416 -0
  167. package/test/btn-header.test.ts +646 -0
  168. package/test/btn-legacy.test.ts +342 -0
  169. package/test/btn-lifecycle.test.ts +295 -0
  170. package/test/btn-list.test.ts +261 -0
  171. package/test/btn-media-source.test.ts +150 -0
  172. package/test/btn-thumbnail.test.ts +160 -0
  173. package/test/builder-compat.test.ts +264 -0
  174. package/test/helpers.ts +27 -0
  175. package/test/hybrid-airich.test.ts +345 -0
  176. package/test/hybrid-client.test.ts +239 -0
  177. package/test/hybrid-errors.test.ts +557 -0
  178. package/test/hybrid-interactive.test.ts +455 -0
  179. package/test/hybrid.golden.test.ts +349 -0
  180. package/test/hybrid.test.ts +479 -0
  181. package/test/integration-auto.test.ts +159 -0
  182. package/test/integration.fakeserver.test.ts +440 -0
  183. package/test/media-gate.test.ts +215 -0
  184. package/test/message-album.test.ts +145 -0
  185. package/test/message-builders.test.ts +401 -0
  186. package/test/message-media.test.ts +303 -0
  187. package/test/message.fakeserver.test.ts +284 -0
  188. package/test/message.golden.test.ts +254 -0
  189. package/test/serializer.test.ts +448 -0
  190. package/tsconfig.json +26 -0
  191. package/tsconfig.test.json +10 -0
@@ -0,0 +1,97 @@
1
+ import type { Button, LegacyButton, ListMessage } from "../btn/builders.js";
2
+ import type { ButtonConfig, LegacyButtonConfig, ListMessageConfig } from "../btn/builders.js";
3
+ import type { AIRich, AIRichConfig, AIRichConstructorOptions, AIRichSendOptions } from "../airich/airich.js";
4
+ import type { MessageSendOptions } from "../core/async-send.js";
5
+ import type { MessageSendResult } from "../zapo-client.js";
6
+ import type { TextBuilder, TextConfig } from "../message/text.js";
7
+ import type { LinkPreviewBuilder, LinkPreviewConfig } from "../message/link-preview.js";
8
+ import type { ReactionBuilder, ReactionConfig } from "../message/reaction.js";
9
+ import type { ForwardBuilder, ForwardConfig } from "../message/forward.js";
10
+ import type { ContactBuilder, ContactConfig } from "../message/contact.js";
11
+ import type { PollBuilder, PollConfig } from "../message/poll.js";
12
+ import type { MediaBuilder, MediaConfig, ImageMessageConfig, VideoMessageConfig, AudioMessageConfig, DocumentMessageConfig } from "../message/media.js";
13
+ import type { StickerBuilder, StickerConfig } from "../message/sticker.js";
14
+ import type { AlbumBuilder, AlbumConfig } from "../message/album.js";
15
+ import type { LocationBuilder, LocationConfig } from "../message/location.js";
16
+ type SendPromise = Promise<MessageSendResult>;
17
+ declare module "zapo-js" {
18
+ interface WaClient {
19
+ button(): Button;
20
+ button(jid: string, config: ButtonConfig, options?: MessageSendOptions): SendPromise;
21
+ legacyButton(): LegacyButton;
22
+ legacyButton(jid: string, config: LegacyButtonConfig, options?: MessageSendOptions): SendPromise;
23
+ listMessage(): ListMessage;
24
+ listMessage(jid: string, config: ListMessageConfig, options?: MessageSendOptions): SendPromise;
25
+ airich(options?: AIRichConstructorOptions): AIRich;
26
+ airich(jid: string, config: AIRichConfig, options?: AIRichSendOptions): SendPromise;
27
+ text(): TextBuilder;
28
+ text(jid: string, config: TextConfig, options?: MessageSendOptions): SendPromise;
29
+ reaction(): ReactionBuilder;
30
+ reaction(config: ReactionConfig, options?: MessageSendOptions): SendPromise;
31
+ reaction(jid: string, config: ReactionConfig, options?: MessageSendOptions): SendPromise;
32
+ media(): MediaBuilder;
33
+ media(jid: string, config: MediaConfig, options?: MessageSendOptions): SendPromise;
34
+ image(): MediaBuilder;
35
+ image(jid: string, config: ImageMessageConfig, options?: MessageSendOptions): SendPromise;
36
+ video(): MediaBuilder;
37
+ video(jid: string, config: VideoMessageConfig, options?: MessageSendOptions): SendPromise;
38
+ audio(): MediaBuilder;
39
+ audio(jid: string, config: AudioMessageConfig, options?: MessageSendOptions): SendPromise;
40
+ document(): MediaBuilder;
41
+ document(jid: string, config: DocumentMessageConfig, options?: MessageSendOptions): SendPromise;
42
+ sticker(): StickerBuilder;
43
+ sticker(jid: string, config: StickerConfig, options?: MessageSendOptions): SendPromise;
44
+ contact(): ContactBuilder;
45
+ contact(jid: string, config: ContactConfig, options?: MessageSendOptions): SendPromise;
46
+ poll(): PollBuilder;
47
+ poll(jid: string, config: PollConfig, options?: MessageSendOptions): SendPromise;
48
+ album(): AlbumBuilder;
49
+ album(jid: string, config: AlbumConfig, options?: MessageSendOptions): SendPromise;
50
+ linkPreview(): LinkPreviewBuilder;
51
+ linkPreview(jid: string, config: LinkPreviewConfig, options?: MessageSendOptions): SendPromise;
52
+ forward(): ForwardBuilder;
53
+ forward(jid: string, config: ForwardConfig, options?: MessageSendOptions): SendPromise;
54
+ location(): LocationBuilder;
55
+ location(jid: string, config: LocationConfig, options?: MessageSendOptions): SendPromise;
56
+ }
57
+ }
58
+ export interface ZapoMessageClientMethods {
59
+ button(): Button;
60
+ button(jid: string, config: ButtonConfig, options?: MessageSendOptions): SendPromise;
61
+ legacyButton(): LegacyButton;
62
+ legacyButton(jid: string, config: LegacyButtonConfig, options?: MessageSendOptions): SendPromise;
63
+ listMessage(): ListMessage;
64
+ listMessage(jid: string, config: ListMessageConfig, options?: MessageSendOptions): SendPromise;
65
+ airich(options?: AIRichConstructorOptions): AIRich;
66
+ airich(jid: string, config: AIRichConfig, options?: AIRichSendOptions): SendPromise;
67
+ text(): TextBuilder;
68
+ text(jid: string, config: TextConfig, options?: MessageSendOptions): SendPromise;
69
+ reaction(): ReactionBuilder;
70
+ reaction(config: ReactionConfig, options?: MessageSendOptions): SendPromise;
71
+ reaction(jid: string, config: ReactionConfig, options?: MessageSendOptions): SendPromise;
72
+ media(): MediaBuilder;
73
+ media(jid: string, config: MediaConfig, options?: MessageSendOptions): SendPromise;
74
+ image(): MediaBuilder;
75
+ image(jid: string, config: ImageMessageConfig, options?: MessageSendOptions): SendPromise;
76
+ video(): MediaBuilder;
77
+ video(jid: string, config: VideoMessageConfig, options?: MessageSendOptions): SendPromise;
78
+ audio(): MediaBuilder;
79
+ audio(jid: string, config: AudioMessageConfig, options?: MessageSendOptions): SendPromise;
80
+ document(): MediaBuilder;
81
+ document(jid: string, config: DocumentMessageConfig, options?: MessageSendOptions): SendPromise;
82
+ sticker(): StickerBuilder;
83
+ sticker(jid: string, config: StickerConfig, options?: MessageSendOptions): SendPromise;
84
+ contact(): ContactBuilder;
85
+ contact(jid: string, config: ContactConfig, options?: MessageSendOptions): SendPromise;
86
+ poll(): PollBuilder;
87
+ poll(jid: string, config: PollConfig, options?: MessageSendOptions): SendPromise;
88
+ album(): AlbumBuilder;
89
+ album(jid: string, config: AlbumConfig, options?: MessageSendOptions): SendPromise;
90
+ linkPreview(): LinkPreviewBuilder;
91
+ linkPreview(jid: string, config: LinkPreviewConfig, options?: MessageSendOptions): SendPromise;
92
+ forward(): ForwardBuilder;
93
+ forward(jid: string, config: ForwardConfig, options?: MessageSendOptions): SendPromise;
94
+ location(): LocationBuilder;
95
+ location(jid: string, config: LocationConfig, options?: MessageSendOptions): SendPromise;
96
+ }
97
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,8 @@
1
+ import type { ZapoClient } from "../zapo-client.js";
2
+ export declare const installOnPrototype: (prototype: object) => boolean;
3
+ export declare const installZapoMessage: (client: ZapoClient) => ZapoClient;
4
+ export declare const autoInstallStatus: () => {
5
+ esm: boolean;
6
+ cjs: boolean;
7
+ marker: symbol;
8
+ };
@@ -0,0 +1,79 @@
1
+ import { createRequire } from "node:module";
2
+ import { WaClient } from "zapo-js";
3
+ import { ZAPO_MESSAGE_METHOD_NAMES, zapoMessageMethods } from "./methods.js";
4
+ import { isClient } from "../core/base-builder.js";
5
+ const INSTALLED_MARKER = Symbol.for("@kyyinfinite/zapo-message.methods-installed");
6
+ const defineMarker = (target) => {
7
+ Object.defineProperty(target, INSTALLED_MARKER, {
8
+ value: true,
9
+ enumerable: false,
10
+ writable: false,
11
+ configurable: false,
12
+ });
13
+ };
14
+ const hasMarker = (target) => INSTALLED_MARKER in target ||
15
+ Object.prototype.hasOwnProperty.call(target, INSTALLED_MARKER);
16
+ export const installOnPrototype = (prototype) => {
17
+ if (!prototype || typeof prototype !== "object")
18
+ return false;
19
+ if (hasMarker(prototype))
20
+ return false;
21
+ if (!Object.isExtensible(prototype))
22
+ return false;
23
+ const target = prototype;
24
+ defineMarker(target);
25
+ for (const name of ZAPO_MESSAGE_METHOD_NAMES) {
26
+ if (name in prototype)
27
+ continue;
28
+ const factory = zapoMessageMethods[name];
29
+ Object.defineProperty(prototype, name, {
30
+ value(...args) {
31
+ return factory(this, ...args);
32
+ },
33
+ enumerable: false,
34
+ writable: true,
35
+ configurable: true,
36
+ });
37
+ }
38
+ return true;
39
+ };
40
+ export const installZapoMessage = (client) => {
41
+ if (!isClient(client)) {
42
+ throw new TypeError("installZapoMessage() butuh WaClient zapo-js (punya client.message.send)");
43
+ }
44
+ const target = client;
45
+ if (hasMarker(client))
46
+ return client;
47
+ defineMarker(target);
48
+ for (const name of ZAPO_MESSAGE_METHOD_NAMES) {
49
+ if (name in client)
50
+ continue;
51
+ const factory = zapoMessageMethods[name];
52
+ Object.defineProperty(client, name, {
53
+ value(...args) {
54
+ return factory(client, ...args);
55
+ },
56
+ enumerable: false,
57
+ writable: true,
58
+ configurable: true,
59
+ });
60
+ }
61
+ return client;
62
+ };
63
+ const esmPrototype = WaClient.prototype;
64
+ const esmInstalled = esmPrototype ? installOnPrototype(esmPrototype) : false;
65
+ let cjsInstalled = false;
66
+ try {
67
+ const require = createRequire(import.meta.url);
68
+ const cjs = require("zapo-js");
69
+ const cjsPrototype = cjs?.WaClient?.prototype;
70
+ if (cjsPrototype &&
71
+ cjsPrototype !== esmPrototype &&
72
+ installOnPrototype(cjsPrototype)) {
73
+ cjsInstalled = true;
74
+ }
75
+ }
76
+ catch {
77
+ cjsInstalled = false;
78
+ }
79
+ export const autoInstallStatus = () => ({ esm: esmInstalled, cjs: cjsInstalled, marker: INSTALLED_MARKER });
@@ -0,0 +1,4 @@
1
+ export { installZapoMessage, installOnPrototype, autoInstallStatus, } from "./auto.js";
2
+ export { zapoMessageMethods, ZAPO_MESSAGE_METHOD_NAMES } from "./methods.js";
3
+ export type { ZapoMessageMethodName } from "./methods.js";
4
+ export type { ZapoMessageClientMethods } from "./augment.js";
@@ -0,0 +1,2 @@
1
+ export { installZapoMessage, installOnPrototype, autoInstallStatus, } from "./auto.js";
2
+ export { zapoMessageMethods, ZAPO_MESSAGE_METHOD_NAMES } from "./methods.js";
@@ -0,0 +1,25 @@
1
+ import { button, legacyButton, listMessage } from "../btn/builders.js";
2
+ import { airich } from "../airich/airich.js";
3
+ import { text, linkPreview, reaction, forward, contact, poll, media, image, video, audio, document, sticker, album, location } from "../message/index.js";
4
+ export declare const zapoMessageMethods: {
5
+ readonly button: typeof button;
6
+ readonly legacyButton: typeof legacyButton;
7
+ readonly listMessage: typeof listMessage;
8
+ readonly airich: typeof airich;
9
+ readonly text: typeof text;
10
+ readonly reaction: typeof reaction;
11
+ readonly media: typeof media;
12
+ readonly image: typeof image;
13
+ readonly video: typeof video;
14
+ readonly audio: typeof audio;
15
+ readonly document: typeof document;
16
+ readonly sticker: typeof sticker;
17
+ readonly contact: typeof contact;
18
+ readonly poll: typeof poll;
19
+ readonly album: typeof album;
20
+ readonly linkPreview: typeof linkPreview;
21
+ readonly forward: typeof forward;
22
+ readonly location: typeof location;
23
+ };
24
+ export type ZapoMessageMethodName = keyof typeof zapoMessageMethods;
25
+ export declare const ZAPO_MESSAGE_METHOD_NAMES: ZapoMessageMethodName[];
@@ -0,0 +1,24 @@
1
+ import { button, legacyButton, listMessage } from "../btn/builders.js";
2
+ import { airich } from "../airich/airich.js";
3
+ import { text, linkPreview, reaction, forward, contact, poll, media, image, video, audio, document, sticker, album, location, } from "../message/index.js";
4
+ export const zapoMessageMethods = {
5
+ button,
6
+ legacyButton,
7
+ listMessage,
8
+ airich,
9
+ text,
10
+ reaction,
11
+ media,
12
+ image,
13
+ video,
14
+ audio,
15
+ document,
16
+ sticker,
17
+ contact,
18
+ poll,
19
+ album,
20
+ linkPreview,
21
+ forward,
22
+ location,
23
+ };
24
+ export const ZAPO_MESSAGE_METHOD_NAMES = Object.keys(zapoMessageMethods);
@@ -0,0 +1,24 @@
1
+ import type { Proto } from "zapo-js";
2
+ export interface ExtractedInteractiveResponse {
3
+ id: string;
4
+ displayText: string | undefined;
5
+ flowName: string | undefined;
6
+ content: "buttonsResponseMessage" | "listResponseMessage" | "templateButtonReplyMessage" | "interactiveResponseMessage";
7
+ contextInfo: Proto.ContextInfo.$Properties | undefined;
8
+ }
9
+ export declare const extractInteractiveResponse: (message: unknown) => ExtractedInteractiveResponse | undefined;
10
+ export interface InteractiveContextMeta {
11
+ hasContext: boolean;
12
+ quotedStanzaId: string | undefined;
13
+ quotedParticipant: string | undefined;
14
+ quotedRemoteJid: string | undefined;
15
+ senderMatches: boolean;
16
+ }
17
+ export interface InspectInteractiveContextOptions {
18
+ botJids?: string[];
19
+ }
20
+ export declare const inspectInteractiveContext: (event: {
21
+ key?: Record<string, unknown>;
22
+ } | null | undefined, response: {
23
+ contextInfo?: Record<string, unknown>;
24
+ } | null | undefined, { botJids }?: InspectInteractiveContextOptions) => InteractiveContextMeta;
@@ -0,0 +1,121 @@
1
+ import { unwrapMessage } from "zapo-js";
2
+ const NATIVE_FLOW_ID_KEYS = [
3
+ "id",
4
+ "selectedId",
5
+ "selected_id",
6
+ "rowId",
7
+ "row_id",
8
+ ];
9
+ const safeParseJsonParams = (paramsJson) => {
10
+ if (typeof paramsJson !== "string" || !paramsJson)
11
+ return undefined;
12
+ try {
13
+ const parsed = JSON.parse(paramsJson);
14
+ if (!parsed || typeof parsed !== "object" || Array.isArray(parsed))
15
+ return undefined;
16
+ return parsed;
17
+ }
18
+ catch {
19
+ return undefined;
20
+ }
21
+ };
22
+ const nonEmptyString = (value) => {
23
+ if (typeof value !== "string")
24
+ return undefined;
25
+ const trimmed = value.trim();
26
+ return trimmed ? trimmed : undefined;
27
+ };
28
+ export const extractInteractiveResponse = (message) => {
29
+ if (!message || typeof message !== "object")
30
+ return undefined;
31
+ const msg = unwrapMessage(message);
32
+ const buttonsResponse = msg.buttonsResponseMessage;
33
+ if (buttonsResponse) {
34
+ const id = nonEmptyString(buttonsResponse.selectedButtonId);
35
+ if (id) {
36
+ return {
37
+ id,
38
+ displayText: nonEmptyString(buttonsResponse.selectedDisplayText),
39
+ flowName: undefined,
40
+ content: "buttonsResponseMessage",
41
+ contextInfo: buttonsResponse.contextInfo,
42
+ };
43
+ }
44
+ return undefined;
45
+ }
46
+ const listResponse = msg.listResponseMessage;
47
+ if (listResponse) {
48
+ const singleSelectReply = listResponse.singleSelectReply;
49
+ const id = nonEmptyString(singleSelectReply?.selectedRowId);
50
+ if (id) {
51
+ return {
52
+ id,
53
+ displayText: nonEmptyString(listResponse.title),
54
+ flowName: undefined,
55
+ content: "listResponseMessage",
56
+ contextInfo: listResponse.contextInfo,
57
+ };
58
+ }
59
+ return undefined;
60
+ }
61
+ const templateReply = msg.templateButtonReplyMessage;
62
+ if (templateReply) {
63
+ const id = nonEmptyString(templateReply.selectedId);
64
+ if (id) {
65
+ return {
66
+ id,
67
+ displayText: nonEmptyString(templateReply.selectedDisplayText),
68
+ flowName: undefined,
69
+ content: "templateButtonReplyMessage",
70
+ contextInfo: templateReply.contextInfo,
71
+ };
72
+ }
73
+ return undefined;
74
+ }
75
+ const interactiveResponse = msg.interactiveResponseMessage;
76
+ if (interactiveResponse) {
77
+ const native = interactiveResponse.nativeFlowResponseMessage;
78
+ if (native) {
79
+ const params = safeParseJsonParams(native.paramsJson);
80
+ if (params) {
81
+ for (const key of NATIVE_FLOW_ID_KEYS) {
82
+ const id = nonEmptyString(params[key]);
83
+ if (id) {
84
+ return {
85
+ id,
86
+ displayText: nonEmptyString(params.display_text ?? params.displayText),
87
+ flowName: nonEmptyString(native.name),
88
+ content: "interactiveResponseMessage",
89
+ contextInfo: interactiveResponse.contextInfo,
90
+ };
91
+ }
92
+ }
93
+ }
94
+ return undefined;
95
+ }
96
+ return undefined;
97
+ }
98
+ return undefined;
99
+ };
100
+ export const inspectInteractiveContext = (event, response, { botJids = [] } = {}) => {
101
+ const contextInfo = response?.contextInfo;
102
+ const key = event?.key;
103
+ const senderCandidates = [
104
+ key?.participantAlt,
105
+ key?.participant,
106
+ key?.remoteJidAlt,
107
+ key?.remoteJid,
108
+ ].filter(Boolean);
109
+ const meta = {
110
+ hasContext: !!contextInfo,
111
+ quotedStanzaId: nonEmptyString(contextInfo?.stanzaId),
112
+ quotedParticipant: nonEmptyString(contextInfo?.participant),
113
+ quotedRemoteJid: nonEmptyString(contextInfo?.remoteJid),
114
+ senderMatches: true,
115
+ };
116
+ if (meta.quotedParticipant) {
117
+ const knownJids = [...botJids, key?.remoteJid, key?.remoteJidAlt].filter(Boolean);
118
+ meta.senderMatches = knownJids.includes(meta.quotedParticipant);
119
+ }
120
+ return meta;
121
+ };
@@ -0,0 +1,99 @@
1
+ import { type Readable } from "node:stream";
2
+ export declare class QueueOverflowError extends Error {
3
+ code: string;
4
+ detail: Record<string, unknown>;
5
+ constructor(message: string, detail?: Record<string, unknown>);
6
+ }
7
+ export declare class QueueClosedError extends Error {
8
+ code: string;
9
+ detail: Record<string, unknown>;
10
+ constructor(message?: string, detail?: Record<string, unknown>);
11
+ }
12
+ export declare class QueueTimeoutError extends Error {
13
+ code: string;
14
+ detail: Record<string, unknown>;
15
+ constructor(message: string, detail?: Record<string, unknown>);
16
+ }
17
+ export declare class MediaAdmissionPausedError extends Error {
18
+ code: string;
19
+ detail: Record<string, unknown>;
20
+ constructor(message?: string, detail?: Record<string, unknown>);
21
+ }
22
+ export interface ConcurrencyController {
23
+ setFactor(next: number): number;
24
+ getFactor(): number;
25
+ effectiveLimit(baseConcurrency: number): number;
26
+ snapshot(): {
27
+ factor: number;
28
+ minConcurrency: number;
29
+ };
30
+ }
31
+ export declare const createConcurrencyController: ({ minConcurrency, }?: {
32
+ minConcurrency?: number;
33
+ }) => ConcurrencyController;
34
+ export interface MediaQueueMetrics {
35
+ inc(name: string): void;
36
+ }
37
+ export interface MediaQueueOptions {
38
+ concurrency?: number | undefined;
39
+ capacity?: number | undefined;
40
+ timeoutMs?: number | undefined;
41
+ maxBytes?: number | undefined;
42
+ controller?: ConcurrencyController | undefined;
43
+ metrics?: MediaQueueMetrics | undefined;
44
+ }
45
+ export interface MediaQueueJobContext {
46
+ signal: AbortSignal;
47
+ maxBytes: number;
48
+ noteBytes: (bytes: number) => void;
49
+ }
50
+ export interface MediaQueueRunOptions {
51
+ label?: string;
52
+ timeoutMs?: number;
53
+ signal?: AbortSignal;
54
+ }
55
+ export interface MediaQueueSnapshot {
56
+ active: number;
57
+ depth: number;
58
+ capacity: number;
59
+ concurrency: number;
60
+ paused: boolean;
61
+ closed: boolean;
62
+ processed: number;
63
+ failed: number;
64
+ timeouts: number;
65
+ oversized: number;
66
+ bytesLastJob: number;
67
+ maxBytes: number;
68
+ }
69
+ export interface MediaQueueStats {
70
+ processed: number;
71
+ failed: number;
72
+ timeouts: number;
73
+ oversized: number;
74
+ }
75
+ export interface MediaQueue {
76
+ run<T>(fn: (context: MediaQueueJobContext) => Promise<T> | T, options?: MediaQueueRunOptions): Promise<T>;
77
+ setAdmissionPaused(paused: boolean): boolean;
78
+ shutdown(options?: {
79
+ timeoutMs?: number;
80
+ }): Promise<{
81
+ rejectedPending: number;
82
+ stillRunning: number;
83
+ }>;
84
+ snapshot(): MediaQueueSnapshot;
85
+ isUnderPressure(): boolean;
86
+ readonly pendingCount: number;
87
+ readonly runningCount: number;
88
+ readonly maxBytes: number;
89
+ readonly stats: MediaQueueStats;
90
+ markOversized(): void;
91
+ }
92
+ export declare const createMediaQueue: (options?: MediaQueueOptions) => MediaQueue;
93
+ export interface LimitedStream extends Readable {
94
+ bytesRead(): number;
95
+ }
96
+ export declare const limitStreamBytes: (source: Readable, maxBytes: number, onLimit?: (bytesRead: number) => void) => LimitedStream;
97
+ export declare const configureSharedMediaQueue: (options?: MediaQueueOptions) => MediaQueue;
98
+ export declare const getSharedMediaQueue: () => MediaQueue;
99
+ export declare const resetSharedMediaQueue: () => void;