@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,474 @@
1
+ import { ValidationError } from "../core/errors.js";
2
+ import { layout, type UnifiedResponseSection } from "./protocol.js";
3
+
4
+ export const EXPERIMENTAL_PRIMITIVES = {
5
+ source: "GenAISearchResultPrimitive",
6
+ image: "GenAIImaginePrimitive",
7
+ video: "GenAIImaginePrimitive",
8
+ footerAction: "GenAIFooterActionPrimitive",
9
+ widget: "GenAI3PExtWidgetPrimitive",
10
+ product: "GenAIProductItemCardPrimitive",
11
+ post: "GenAIPostPrimitive",
12
+ reels: "GenAIReelPrimitive",
13
+ html: "GenAIaeacdsnwHtmlPrimitive",
14
+ } as const;
15
+
16
+ const assertObject = (
17
+ value: unknown,
18
+ name: string,
19
+ ): Record<string, unknown> => {
20
+ if (!value || typeof value !== "object" || Array.isArray(value)) {
21
+ throw new ValidationError(`${name} harus object`);
22
+ }
23
+ return value as Record<string, unknown>;
24
+ };
25
+
26
+ const assertString = (value: unknown, name: string): string => {
27
+ if (typeof value !== "string")
28
+ throw new ValidationError(`${name} harus string`);
29
+ return value;
30
+ };
31
+
32
+ const assertNonEmptyString = (value: unknown, name: string): string => {
33
+ const text = assertString(value, name);
34
+ if (!text.trim()) throw new ValidationError(`${name} harus string non-empty`);
35
+ return text;
36
+ };
37
+
38
+ export interface SourceObjectItem {
39
+ favicon?: string;
40
+ icon?: string;
41
+ url?: string;
42
+ title?: string;
43
+ subtitle?: string;
44
+ }
45
+
46
+ export type SourceInput = string | (string | undefined)[] | SourceObjectItem;
47
+
48
+ export interface SourceSectionOptions {
49
+ layout?: string | undefined;
50
+ }
51
+
52
+ export const sourceSection = (
53
+ sources: SourceInput[],
54
+ { layout: layoutName = "Single" }: SourceSectionOptions = {},
55
+ ): UnifiedResponseSection => {
56
+ if (!Array.isArray(sources) || !sources.length) {
57
+ throw new ValidationError(
58
+ "source butuh array berisi string url, [icon, url, title, subtitle], atau object",
59
+ );
60
+ }
61
+
62
+ const normalized = sources.map(
63
+ (
64
+ source,
65
+ ): { icon: string; url: string; title: string; subtitle: string } => {
66
+ if (typeof source === "string") {
67
+ return { icon: "", url: source, title: "", subtitle: "" };
68
+ }
69
+
70
+ if (Array.isArray(source)) {
71
+ const [icon, url, title, subtitle] = source;
72
+ return {
73
+ icon: icon ?? "",
74
+ url: assertNonEmptyString(url, "source url"),
75
+ title: title ?? "",
76
+ subtitle: subtitle ?? "",
77
+ };
78
+ }
79
+
80
+ const item = assertObject(source, "source");
81
+ return {
82
+ icon: (item.favicon ?? item.icon ?? "") as string,
83
+ url: assertNonEmptyString(item.url ?? "", "source url"),
84
+ title: (item.title ?? "") as string,
85
+ subtitle: (item.subtitle ?? "") as string,
86
+ };
87
+ },
88
+ );
89
+
90
+ return layout(layoutName, {
91
+ sources: normalized.map(({ icon, url, title, subtitle }) => ({
92
+ source_type: "THIRD_PARTY",
93
+ source_display_name: title,
94
+ source_subtitle: subtitle,
95
+ source_url: url,
96
+ favicon: { url: icon, mime_type: "image/jpeg", width: 16, height: 16 },
97
+ })),
98
+ __typename: EXPERIMENTAL_PRIMITIVES.source,
99
+ });
100
+ };
101
+
102
+ export interface ImageSectionOptions {
103
+ width?: number | undefined;
104
+ height?: number | undefined;
105
+ status?: string | undefined;
106
+ update_text?: string | undefined;
107
+ }
108
+
109
+ export const imageSection = (
110
+ url: string,
111
+ {
112
+ width,
113
+ height,
114
+ status = "READY",
115
+ update_text: updateText,
116
+ }: ImageSectionOptions = {},
117
+ ): UnifiedResponseSection => {
118
+ assertString(url, "image url");
119
+
120
+ return layout("Single", {
121
+ media: {
122
+ url,
123
+ mime_type: "image/png",
124
+ ...(width !== undefined && { width }),
125
+ ...(height !== undefined && { height }),
126
+ },
127
+ imagine_type: "IMAGE",
128
+ status: {
129
+ status,
130
+ ...(updateText !== undefined && { update_text: updateText }),
131
+ },
132
+ __typename: EXPERIMENTAL_PRIMITIVES.image,
133
+ });
134
+ };
135
+
136
+ export interface VideoSectionOptions {
137
+ mime_type?: string | undefined;
138
+ file_length?: number | undefined;
139
+ duration?: number | undefined;
140
+ status?: string | undefined;
141
+ }
142
+
143
+ export const videoSection = (
144
+ url: string,
145
+ {
146
+ mime_type: mimeType = "video/mp4",
147
+ file_length: fileLength,
148
+ duration,
149
+ status = "READY",
150
+ }: VideoSectionOptions = {},
151
+ ): UnifiedResponseSection => {
152
+ assertString(url, "video url");
153
+
154
+ return layout("Single", {
155
+ media: {
156
+ url,
157
+ mime_type: mimeType,
158
+ ...(fileLength !== undefined && { file_length: fileLength }),
159
+ ...(duration !== undefined && { duration }),
160
+ },
161
+ imagine_type: "ANIMATE",
162
+ status: { status },
163
+ __typename: EXPERIMENTAL_PRIMITIVES.video,
164
+ });
165
+ };
166
+
167
+ export interface FooterActionItem {
168
+ text?: string;
169
+ cta_text?: string;
170
+ type?: string;
171
+ cta_type?: string;
172
+ url?: string;
173
+ cta_url?: string;
174
+ }
175
+
176
+ export interface SectionLayoutOptions {
177
+ layout?: string | undefined;
178
+ }
179
+
180
+ export const footerActionSection = (
181
+ data: FooterActionItem | FooterActionItem[],
182
+ { layout: layoutName }: SectionLayoutOptions = {},
183
+ ): UnifiedResponseSection => {
184
+ const items = Array.isArray(data) ? data : [data];
185
+ if (!items.length)
186
+ throw new ValidationError("footerAction butuh minimal satu action");
187
+ if (
188
+ !items.every(
189
+ (item) => item && typeof item === "object" && !Array.isArray(item),
190
+ )
191
+ ) {
192
+ throw new ValidationError("footerAction harus object atau array object");
193
+ }
194
+
195
+ const actions = items.map((item) => ({
196
+ cta_text: item.text ?? item.cta_text ?? "",
197
+ cta_type: item.type ?? item.cta_type ?? "OPEN_URL",
198
+ cta_url: item.url ?? item.cta_url ?? "",
199
+ __typename: EXPERIMENTAL_PRIMITIVES.footerAction,
200
+ }));
201
+
202
+ const name = layoutName ?? (Array.isArray(data) ? "HScroll" : "Single");
203
+ return layout(
204
+ name,
205
+ Array.isArray(data) ? actions : (actions[0] as (typeof actions)[number]),
206
+ );
207
+ };
208
+
209
+ export interface WidgetActionToast {
210
+ label?: string;
211
+ }
212
+
213
+ export interface WidgetAction {
214
+ label?: string;
215
+ state?: string;
216
+ kind?: string;
217
+ tool_call_id?: string;
218
+ id?: string;
219
+ toast?: WidgetActionToast;
220
+ }
221
+
222
+ export interface WidgetItem {
223
+ title?: string;
224
+ header?: Record<string, unknown>;
225
+ sections?: unknown[];
226
+ actions?: WidgetAction[];
227
+ body?: Record<string, unknown>;
228
+ }
229
+
230
+ export const widgetSection = (
231
+ data: WidgetItem | WidgetItem[],
232
+ { layout: layoutName }: SectionLayoutOptions = {},
233
+ ): UnifiedResponseSection => {
234
+ const items = Array.isArray(data) ? data : [data];
235
+ if (!items.length)
236
+ throw new ValidationError("widget butuh minimal satu widget");
237
+ if (
238
+ !items.every(
239
+ (item) => item && typeof item === "object" && !Array.isArray(item),
240
+ )
241
+ ) {
242
+ throw new ValidationError("widget harus object atau array object");
243
+ }
244
+
245
+ const widgets = items.map((item) => ({
246
+ __typename: EXPERIMENTAL_PRIMITIVES.widget,
247
+ header: {
248
+ __typename: "GenAI3PExtWidgetStandardHeader",
249
+ title: item.title ?? "",
250
+ ...(item.header ?? {}),
251
+ },
252
+ body: {
253
+ __typename: "GenAI3PExtCalendarEventList",
254
+ sections: item.sections ?? [],
255
+ ctas: (item.actions ?? []).map((action) => ({
256
+ __typename: "GenAI3PExtWidgetCTA",
257
+ label: action.label ?? "",
258
+ state: action.state ?? "PENDING",
259
+ kind: action.kind ?? "OTHER",
260
+ tool_call_id: action.tool_call_id ?? action.id ?? "",
261
+ ...(action.toast && {
262
+ toast: {
263
+ __typename: "GenAI3PExtWidgetToast",
264
+ label: action.toast.label ?? action.label ?? "",
265
+ },
266
+ }),
267
+ })),
268
+ ...(item.body ?? {}),
269
+ },
270
+ }));
271
+
272
+ const name = layoutName ?? (Array.isArray(data) ? "HScroll" : "Single");
273
+ return layout(
274
+ name,
275
+ Array.isArray(data) ? widgets : (widgets[0] as (typeof widgets)[number]),
276
+ );
277
+ };
278
+
279
+ export interface ProductItem {
280
+ title?: string;
281
+ brand?: string;
282
+ price?: string;
283
+ sale_price?: string;
284
+ product_url?: string;
285
+ url?: string;
286
+ image_url?: string;
287
+ image?: string;
288
+ icon_url?: string;
289
+ icon?: string;
290
+ }
291
+
292
+ export const productSection = (
293
+ data: ProductItem | ProductItem[],
294
+ { layout: layoutName }: SectionLayoutOptions = {},
295
+ ): UnifiedResponseSection => {
296
+ const items = Array.isArray(data) ? data : [data];
297
+ if (!items.length)
298
+ throw new ValidationError("product butuh minimal satu product");
299
+ if (
300
+ !items.every(
301
+ (item) => item && typeof item === "object" && !Array.isArray(item),
302
+ )
303
+ ) {
304
+ throw new ValidationError("product harus object atau array object");
305
+ }
306
+
307
+ const products = items.map((item) => ({
308
+ title: item.title ?? "",
309
+ brand: item.brand ?? "",
310
+ price: item.price ?? "",
311
+ sale_price: item.sale_price ?? "",
312
+ product_url: item.product_url ?? item.url ?? "",
313
+ image: { url: item.image_url ?? item.image ?? "" },
314
+ additional_images: [{ url: item.icon_url ?? item.icon ?? "" }],
315
+ __typename: EXPERIMENTAL_PRIMITIVES.product,
316
+ }));
317
+
318
+ const name = layoutName ?? (Array.isArray(data) ? "HScroll" : "Single");
319
+ return layout(
320
+ name,
321
+ Array.isArray(data) ? products : (products[0] as (typeof products)[number]),
322
+ );
323
+ };
324
+
325
+ export interface PostItem {
326
+ title?: string;
327
+ subtitle?: string;
328
+ username?: string;
329
+ profile_picture_url?: string;
330
+ profile_url?: string;
331
+ profile?: string;
332
+ is_verified?: boolean;
333
+ verified?: boolean;
334
+ thumbnail_url?: string;
335
+ thumbnail?: string;
336
+ post_caption?: string;
337
+ caption?: string;
338
+ likes_count?: number;
339
+ like?: number;
340
+ comments_count?: number;
341
+ comment?: number;
342
+ shares_count?: number;
343
+ share?: number;
344
+ post_url?: string;
345
+ url?: string;
346
+ post_deeplink?: string;
347
+ deeplink?: string;
348
+ source_app?: string;
349
+ source?: string;
350
+ footer_label?: string;
351
+ footer?: string;
352
+ footer_icon?: string;
353
+ icon?: string;
354
+ orientation?: string;
355
+ post_type?: string;
356
+ }
357
+
358
+ export const postSection = (
359
+ data: PostItem | PostItem[],
360
+ { layout: layoutName }: SectionLayoutOptions = {},
361
+ ): UnifiedResponseSection => {
362
+ const items = Array.isArray(data) ? data : [data];
363
+ if (!items.length) throw new ValidationError("post butuh minimal satu post");
364
+ if (
365
+ !items.every(
366
+ (item) => item && typeof item === "object" && !Array.isArray(item),
367
+ )
368
+ ) {
369
+ throw new ValidationError("post harus object atau array object");
370
+ }
371
+
372
+ const posts = items.map((item) => ({
373
+ title: item.title ?? "",
374
+ subtitle: item.subtitle ?? "",
375
+ username: item.username ?? "",
376
+ profile_picture_url:
377
+ item.profile_picture_url ?? item.profile_url ?? item.profile ?? "",
378
+ is_verified: !!(item.is_verified || item.verified),
379
+ thumbnail_url: item.thumbnail_url ?? item.thumbnail ?? "",
380
+ post_caption: item.post_caption ?? item.caption ?? "",
381
+ likes_count: item.likes_count ?? item.like ?? 0,
382
+ comments_count: item.comments_count ?? item.comment ?? 0,
383
+ shares_count: item.shares_count ?? item.share ?? 0,
384
+ post_url: item.post_url ?? item.url ?? "",
385
+ post_deeplink: item.post_deeplink ?? item.deeplink ?? "",
386
+ source_app: item.source_app ?? item.source ?? "INSTAGRAM",
387
+ footer_label: item.footer_label ?? item.footer ?? "",
388
+ footer_icon: item.footer_icon ?? item.icon ?? "",
389
+ is_carousel: items.length > 1,
390
+ orientation: item.orientation ?? "LANDSCAPE",
391
+ post_type: item.post_type ?? "VIDEO",
392
+ __typename: EXPERIMENTAL_PRIMITIVES.post,
393
+ }));
394
+
395
+ return layout(layoutName ?? "HScroll", posts);
396
+ };
397
+
398
+ export interface ReelsItem {
399
+ videoUrl?: string;
400
+ url?: string;
401
+ thumbnailUrl?: string;
402
+ thumbnail?: string;
403
+ username?: string;
404
+ profileIconUrl?: string;
405
+ profile?: string;
406
+ reels_title?: string;
407
+ title?: string;
408
+ likes_count?: number;
409
+ like?: number;
410
+ shares_count?: number;
411
+ share?: number;
412
+ view_count?: number;
413
+ view?: number;
414
+ reel_source?: string;
415
+ source?: string;
416
+ is_verified?: boolean;
417
+ verified?: boolean;
418
+ }
419
+
420
+ export interface ReelsSectionOptions {
421
+ layout?: string | undefined;
422
+ }
423
+
424
+ export const reelsSection = (
425
+ data: ReelsItem | ReelsItem[],
426
+ { layout: layoutName = "HScroll" }: ReelsSectionOptions = {},
427
+ ): UnifiedResponseSection => {
428
+ const items = Array.isArray(data) ? data : [data];
429
+ if (!items.length) throw new ValidationError("reels butuh minimal satu item");
430
+ if (
431
+ !items.every(
432
+ (item) => item && typeof item === "object" && !Array.isArray(item),
433
+ )
434
+ ) {
435
+ throw new ValidationError("reels harus object atau array object");
436
+ }
437
+
438
+ return layout(
439
+ layoutName,
440
+ items.map((item) => ({
441
+ reels_url: item.videoUrl ?? item.url ?? "",
442
+ thumbnail_url: item.thumbnailUrl ?? item.thumbnail ?? "",
443
+ creator: item.username ?? "",
444
+ avatar_url: item.profileIconUrl ?? item.profile ?? "",
445
+ reels_title: item.reels_title ?? item.title ?? "",
446
+ likes_count: item.likes_count ?? item.like ?? 0,
447
+ shares_count: item.shares_count ?? item.share ?? 0,
448
+ view_count: item.view_count ?? item.view ?? 0,
449
+ reel_source: item.reel_source ?? item.source ?? "IG",
450
+ is_verified: !!(item.is_verified || item.verified),
451
+ __typename: EXPERIMENTAL_PRIMITIVES.reels,
452
+ })),
453
+ );
454
+ };
455
+
456
+ export interface HtmlSectionOptions {
457
+ trusted_sources?: unknown[] | undefined;
458
+ }
459
+
460
+ export const htmlSection = (
461
+ html: string,
462
+ { trusted_sources: trustedSources = [] }: HtmlSectionOptions = {},
463
+ ): UnifiedResponseSection => {
464
+ assertString(html, "html");
465
+
466
+ if (!Array.isArray(trustedSources))
467
+ throw new ValidationError("trusted_sources harus array");
468
+
469
+ return layout("Single", {
470
+ __typename: EXPERIMENTAL_PRIMITIVES.html,
471
+ payload: html,
472
+ trusted_sources: trustedSources,
473
+ });
474
+ };
@@ -0,0 +1,133 @@
1
+ import { randomBytes } from "node:crypto";
2
+ import { proto } from "zapo-js";
3
+ import type {
4
+ EditBuildResult,
5
+ PlaceholderBuildResult,
6
+ RichResponseBuildResult,
7
+ SubmessagePayload,
8
+ WrapBotForwardedResult,
9
+ } from "../payloads.js";
10
+ import { encodeUnifiedResponse } from "./encode.js";
11
+
12
+ export const ZWSP = "​";
13
+
14
+ export interface UnifiedViewModel {
15
+ __typename: string;
16
+ primitive?: Record<string, unknown>;
17
+ primitives?: Record<string, unknown>[];
18
+ }
19
+
20
+ export interface UnifiedResponseSection {
21
+ view_model: UnifiedViewModel;
22
+ __typename?: "GenAIUnifiedResponseSection";
23
+ }
24
+
25
+ export interface UnifiedResponsePayload {
26
+ response_id: string | null;
27
+ sections: UnifiedResponseSection[];
28
+ }
29
+
30
+ export const layout = (
31
+ name: string,
32
+ data: Record<string, unknown> | Record<string, unknown>[],
33
+ extra: Record<string, unknown> = {},
34
+ ): UnifiedResponseSection =>
35
+ Array.isArray(data)
36
+ ? {
37
+ ...extra,
38
+ view_model: {
39
+ primitives: data,
40
+ __typename: `GenAI${name}LayoutViewModel`,
41
+ },
42
+ }
43
+ : {
44
+ ...extra,
45
+ view_model: {
46
+ primitive: data,
47
+ __typename: `GenAI${name}LayoutViewModel`,
48
+ },
49
+ };
50
+
51
+ export interface RichResponseInput {
52
+ responseId: string;
53
+ sections: UnifiedResponseSection[];
54
+ submessages?: SubmessagePayload[] | undefined;
55
+ contextInfo?: Record<string, unknown> | undefined;
56
+ }
57
+
58
+ export const buildRichResponse = ({
59
+ responseId,
60
+ sections,
61
+ submessages = [],
62
+ contextInfo = {},
63
+ }: RichResponseInput): RichResponseBuildResult => ({
64
+ richResponseMessage: {
65
+ messageType: proto.AIRichResponseMessageType.AI_RICH_RESPONSE_TYPE_STANDARD,
66
+ submessages,
67
+ unifiedResponse: {
68
+ data: encodeUnifiedResponse({ response_id: responseId, sections }),
69
+ },
70
+ contextInfo,
71
+ },
72
+ });
73
+
74
+ export const wrapBotForwarded = (
75
+ message: RichResponseBuildResult,
76
+ ): WrapBotForwardedResult => ({
77
+ botForwardedMessage: { message },
78
+ });
79
+
80
+ export interface PlaceholderInput {
81
+ responseId: string;
82
+ contextInfo: Record<string, unknown>;
83
+ }
84
+
85
+ export const buildPlaceholderContent = ({
86
+ responseId,
87
+ contextInfo,
88
+ }: PlaceholderInput): PlaceholderBuildResult => ({
89
+ messageContextInfo: {
90
+ deviceListMetadataVersion: 2,
91
+ messageSecret: randomBytes(32),
92
+ },
93
+ ...wrapBotForwarded(
94
+ buildRichResponse({
95
+ responseId,
96
+ sections: [
97
+ layout("Single", {
98
+ text: ZWSP,
99
+ __typename: "GenAIMarkdownTextUXPrimitive",
100
+ }),
101
+ ],
102
+ submessages: [
103
+ {
104
+ messageType: proto.AIRichResponseSubMessageType.AI_RICH_RESPONSE_TEXT,
105
+ messageText: ZWSP,
106
+ },
107
+ ],
108
+ contextInfo,
109
+ }),
110
+ ),
111
+ });
112
+
113
+ export interface EditInput {
114
+ jid: string;
115
+ id: string;
116
+ rich: WrapBotForwardedResult;
117
+ }
118
+
119
+ export const buildEditContent = ({
120
+ jid,
121
+ id,
122
+ rich,
123
+ }: EditInput): EditBuildResult => ({
124
+ botForwardedMessage: {
125
+ message: {
126
+ protocolMessage: {
127
+ key: { remoteJid: jid, fromMe: true, id },
128
+ type: proto.Message.ProtocolMessage.Type.MESSAGE_EDIT,
129
+ editedMessage: rich,
130
+ },
131
+ },
132
+ },
133
+ });
@@ -0,0 +1,70 @@
1
+ import { ValidationError } from "../core/errors.js";
2
+ import { parseInline, type ParseInlineOptions } from "./inline.js";
3
+ import type { TableMetadataPayload } from "../payloads.js";
4
+
5
+ export interface UnifiedTableRow {
6
+ is_header: boolean;
7
+ cells: string[];
8
+ markdown_cells: { text: string; inline_entities?: unknown[] }[];
9
+ }
10
+
11
+ export interface ToTableResult {
12
+ unified: {
13
+ is_header: boolean;
14
+ cells: string[];
15
+ markdown_cells: { text: string; inline_entities?: unknown[] }[];
16
+ }[];
17
+ legacy: TableMetadataPayload["rows"];
18
+ }
19
+
20
+ export const toTable = (
21
+ rows: string[][],
22
+ inlineOptions: ParseInlineOptions = {},
23
+ ): ToTableResult => {
24
+ const valid =
25
+ Array.isArray(rows) &&
26
+ rows.length > 0 &&
27
+ rows.every(
28
+ (row) =>
29
+ Array.isArray(row) && row.every((cell) => typeof cell === "string"),
30
+ );
31
+ if (!valid)
32
+ throw new ValidationError(
33
+ "table harus array 2 dimensi berisi string, baris pertama jadi header",
34
+ );
35
+
36
+ const [header, ...body] = rows;
37
+ const width = Math.max(
38
+ (header as string[]).length,
39
+ ...body.map((row) => row.length),
40
+ );
41
+ const pad = (row: string[]): string[] => [
42
+ ...row,
43
+ ...Array<string>(width - row.length).fill(""),
44
+ ];
45
+
46
+ const unified = (
47
+ [
48
+ { is_header: true, cells: pad(header as string[]) },
49
+ ...body.map((row) => ({ is_header: false, cells: pad(row) })),
50
+ ] as UnifiedTableRow[]
51
+ ).map((row) => {
52
+ const markdown = row.cells.map((cell) => {
53
+ const { text, entities } = parseInline(cell, inlineOptions);
54
+ return {
55
+ text,
56
+ ...(entities.length ? { inline_entities: entities } : {}),
57
+ };
58
+ });
59
+
60
+ return { ...row, markdown_cells: markdown };
61
+ });
62
+
63
+ return {
64
+ unified,
65
+ legacy: unified.map((row) => ({
66
+ items: row.cells,
67
+ ...(row.is_header ? { isHeading: true } : {}),
68
+ })),
69
+ };
70
+ };