@kyyinfinite/zapo-message 0.0.0-stage → 1.0.1
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 +159 -0
- package/LICENSE +20 -0
- package/README.md +1401 -2
- package/build.config.json +28 -0
- package/dist/airich/airich.d.ts +180 -0
- package/dist/airich/airich.js +548 -0
- package/dist/airich/context.d.ts +24 -0
- package/dist/airich/context.js +48 -0
- package/dist/airich/encode.d.ts +3 -0
- package/dist/airich/encode.js +16 -0
- package/dist/airich/index.d.ts +20 -0
- package/dist/airich/index.js +11 -0
- package/dist/airich/inline.d.ts +45 -0
- package/dist/airich/inline.js +152 -0
- package/dist/airich/nodes.d.ts +30 -0
- package/dist/airich/nodes.js +121 -0
- package/dist/airich/primitives.d.ts +144 -0
- package/dist/airich/primitives.js +230 -0
- package/dist/airich/protocol.d.ts +35 -0
- package/dist/airich/protocol.js +65 -0
- package/dist/airich/table.d.ts +22 -0
- package/dist/airich/table.js +35 -0
- package/dist/airich/tokenizer.d.ts +13 -0
- package/dist/airich/tokenizer.js +717 -0
- package/dist/airich/transport.d.ts +15 -0
- package/dist/airich/transport.js +56 -0
- package/dist/branding.d.ts +5 -0
- package/dist/branding.js +9 -0
- package/dist/btn/builders.d.ts +246 -0
- package/dist/btn/builders.js +710 -0
- package/dist/btn/media-slot.d.ts +28 -0
- package/dist/btn/media-slot.js +323 -0
- package/dist/btn/media-source.d.ts +36 -0
- package/dist/btn/media-source.js +116 -0
- package/dist/btn/sharp-loader.d.ts +5 -0
- package/dist/btn/sharp-loader.js +18 -0
- package/dist/btn/thumbnail.d.ts +21 -0
- package/dist/btn/thumbnail.js +124 -0
- package/dist/btn/validators.d.ts +40 -0
- package/dist/btn/validators.js +135 -0
- package/dist/btn.d.ts +1 -0
- package/dist/btn.js +1 -0
- package/dist/core/async-send.d.ts +20 -0
- package/dist/core/async-send.js +1 -0
- package/dist/core/base-builder.d.ts +20 -0
- package/dist/core/base-builder.js +67 -0
- package/dist/core/errors.d.ts +30 -0
- package/dist/core/errors.js +61 -0
- package/dist/download.d.ts +24 -0
- package/dist/download.js +123 -0
- package/dist/index.d.ts +38 -0
- package/dist/index.js +22 -0
- package/dist/integration/augment.d.ts +97 -0
- package/dist/integration/augment.js +1 -0
- package/dist/integration/auto.d.ts +8 -0
- package/dist/integration/auto.js +79 -0
- package/dist/integration/index.d.ts +4 -0
- package/dist/integration/index.js +2 -0
- package/dist/integration/methods.d.ts +25 -0
- package/dist/integration/methods.js +24 -0
- package/dist/interactive.d.ts +24 -0
- package/dist/interactive.js +121 -0
- package/dist/media-gate.d.ts +99 -0
- package/dist/media-gate.js +322 -0
- package/dist/message/album.d.ts +33 -0
- package/dist/message/album.js +107 -0
- package/dist/message/contact.d.ts +29 -0
- package/dist/message/contact.js +123 -0
- package/dist/message/forward.d.ts +23 -0
- package/dist/message/forward.js +77 -0
- package/dist/message/index.d.ts +26 -0
- package/dist/message/index.js +13 -0
- package/dist/message/link-preview.d.ts +28 -0
- package/dist/message/link-preview.js +98 -0
- package/dist/message/location.d.ts +24 -0
- package/dist/message/location.js +94 -0
- package/dist/message/media.d.ts +90 -0
- package/dist/message/media.js +123 -0
- package/dist/message/poll.d.ts +24 -0
- package/dist/message/poll.js +98 -0
- package/dist/message/reaction.d.ts +20 -0
- package/dist/message/reaction.js +71 -0
- package/dist/message/sticker.d.ts +29 -0
- package/dist/message/sticker.js +44 -0
- package/dist/message/target.d.ts +18 -0
- package/dist/message/target.js +24 -0
- package/dist/message/text.d.ts +17 -0
- package/dist/message/text.js +29 -0
- package/dist/message/vcard.d.ts +9 -0
- package/dist/message/vcard.js +23 -0
- package/dist/message/webp.d.ts +6 -0
- package/dist/message/webp.js +57 -0
- package/dist/payloads.d.ts +180 -0
- package/dist/payloads.js +1 -0
- package/dist/serialize.d.ts +41 -0
- package/dist/serialize.js +93 -0
- package/dist/status.d.ts +3 -0
- package/dist/status.js +21 -0
- package/dist/zapo-client.d.ts +44 -0
- package/dist/zapo-client.js +1 -0
- package/examples/airich.ts +59 -0
- package/examples/buttons.ts +48 -0
- package/examples/client-methods.ts +58 -0
- package/examples/hybrid.ts +133 -0
- package/examples/serialize.ts +61 -0
- package/package.json +271 -4
- package/scripts/check-no-comments.mjs +101 -0
- package/scripts/compare-golden.mjs +461 -0
- package/scripts/lib-config.mjs +9 -0
- package/scripts/smoke-auto.mjs +75 -0
- package/scripts/test-external-consumer.mjs +316 -0
- package/scripts/verify-package.mjs +78 -0
- package/src/airich/airich.ts +970 -0
- package/src/airich/context.ts +96 -0
- package/src/airich/encode.ts +25 -0
- package/src/airich/index.ts +98 -0
- package/src/airich/inline.ts +272 -0
- package/src/airich/nodes.ts +174 -0
- package/src/airich/primitives.ts +474 -0
- package/src/airich/protocol.ts +133 -0
- package/src/airich/table.ts +70 -0
- package/src/airich/tokenizer.ts +799 -0
- package/src/airich/transport.ts +106 -0
- package/src/branding.ts +23 -0
- package/src/btn/builders.ts +1073 -0
- package/src/btn/media-slot.ts +486 -0
- package/src/btn/media-source.ts +189 -0
- package/src/btn/sharp-loader.ts +23 -0
- package/src/btn/thumbnail.ts +182 -0
- package/src/btn/validators.ts +237 -0
- package/src/btn.ts +8 -0
- package/src/core/async-send.ts +28 -0
- package/src/core/base-builder.ts +104 -0
- package/src/core/errors.ts +82 -0
- package/src/download.ts +212 -0
- package/src/index.ts +229 -0
- package/src/integration/augment.ts +262 -0
- package/src/integration/auto.ts +103 -0
- package/src/integration/index.ts +8 -0
- package/src/integration/methods.ts +45 -0
- package/src/interactive.ts +188 -0
- package/src/media-gate.ts +499 -0
- package/src/message/album.ts +187 -0
- package/src/message/contact.ts +170 -0
- package/src/message/forward.ts +123 -0
- package/src/message/index.ts +83 -0
- package/src/message/link-preview.ts +148 -0
- package/src/message/location.ts +150 -0
- package/src/message/media.ts +323 -0
- package/src/message/poll.ts +158 -0
- package/src/message/reaction.ts +133 -0
- package/src/message/sticker.ts +100 -0
- package/src/message/target.ts +38 -0
- package/src/message/text.ts +61 -0
- package/src/message/vcard.ts +36 -0
- package/src/message/webp.ts +78 -0
- package/src/payloads.ts +204 -0
- package/src/serialize.ts +180 -0
- package/src/status.ts +29 -0
- package/src/zapo-client.ts +84 -0
- package/test/airich-inline.test.ts +200 -0
- package/test/airich-primitives.test.ts +226 -0
- package/test/airich-tokenizer.test.ts +145 -0
- package/test/airich.golden.test.ts +406 -0
- package/test/airich.test.ts +1006 -0
- package/test/btn-button.test.ts +416 -0
- package/test/btn-header.test.ts +646 -0
- package/test/btn-legacy.test.ts +342 -0
- package/test/btn-lifecycle.test.ts +295 -0
- package/test/btn-list.test.ts +261 -0
- package/test/btn-media-source.test.ts +150 -0
- package/test/btn-thumbnail.test.ts +160 -0
- package/test/builder-compat.test.ts +264 -0
- package/test/helpers.ts +27 -0
- package/test/hybrid-airich.test.ts +345 -0
- package/test/hybrid-client.test.ts +239 -0
- package/test/hybrid-errors.test.ts +557 -0
- package/test/hybrid-interactive.test.ts +455 -0
- package/test/hybrid.golden.test.ts +349 -0
- package/test/hybrid.test.ts +479 -0
- package/test/integration-auto.test.ts +159 -0
- package/test/integration.fakeserver.test.ts +440 -0
- package/test/media-gate.test.ts +215 -0
- package/test/message-album.test.ts +145 -0
- package/test/message-builders.test.ts +401 -0
- package/test/message-media.test.ts +303 -0
- package/test/message.fakeserver.test.ts +284 -0
- package/test/message.golden.test.ts +254 -0
- package/test/serializer.test.ts +448 -0
- package/tsconfig.json +26 -0
- package/tsconfig.test.json +10 -0
|
@@ -0,0 +1,970 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
import { proto } from "zapo-js";
|
|
3
|
+
import type { Proto } from "zapo-js";
|
|
4
|
+
import { BaseBuilder, type SendResolutionArg } from "../core/base-builder.js";
|
|
5
|
+
import { sendConfigured, type MessageSendOptions } from "../core/async-send.js";
|
|
6
|
+
import type { MessageSendResult } from "../zapo-client.js";
|
|
7
|
+
import {
|
|
8
|
+
ValidationError,
|
|
9
|
+
ItemNotFoundError,
|
|
10
|
+
InvalidTargetError,
|
|
11
|
+
} from "../core/errors.js";
|
|
12
|
+
import type {
|
|
13
|
+
SubmessagePayload,
|
|
14
|
+
WrapBotForwardedResult,
|
|
15
|
+
PlaceholderBuildResult,
|
|
16
|
+
EditBuildResult,
|
|
17
|
+
RichResponseBuildResult,
|
|
18
|
+
} from "../payloads.js";
|
|
19
|
+
import type { ZapoClient } from "../zapo-client.js";
|
|
20
|
+
import { parseInline, type ParseInlineOptions } from "./inline.js";
|
|
21
|
+
import { toTable } from "./table.js";
|
|
22
|
+
import { tokenizeCode } from "./tokenizer.js";
|
|
23
|
+
import { NodeStore, type RichNode } from "./nodes.js";
|
|
24
|
+
import { decodeUnifiedResponse } from "./encode.js";
|
|
25
|
+
import {
|
|
26
|
+
filterContextInfo,
|
|
27
|
+
buildContextInfo,
|
|
28
|
+
type AllowedContextInfo,
|
|
29
|
+
type QuoteInfo,
|
|
30
|
+
} from "./context.js";
|
|
31
|
+
import {
|
|
32
|
+
layout,
|
|
33
|
+
wrapBotForwarded,
|
|
34
|
+
buildRichResponse,
|
|
35
|
+
buildPlaceholderContent,
|
|
36
|
+
buildEditContent,
|
|
37
|
+
type UnifiedResponseSection,
|
|
38
|
+
} from "./protocol.js";
|
|
39
|
+
import { sendViaZapo, sendPlaceholderThenEdit } from "./transport.js";
|
|
40
|
+
import {
|
|
41
|
+
sourceSection,
|
|
42
|
+
imageSection,
|
|
43
|
+
videoSection,
|
|
44
|
+
footerActionSection,
|
|
45
|
+
widgetSection,
|
|
46
|
+
productSection,
|
|
47
|
+
postSection,
|
|
48
|
+
reelsSection,
|
|
49
|
+
htmlSection,
|
|
50
|
+
type SourceInput,
|
|
51
|
+
type ImageSectionOptions,
|
|
52
|
+
type VideoSectionOptions,
|
|
53
|
+
type FooterActionItem,
|
|
54
|
+
type WidgetItem,
|
|
55
|
+
type ProductItem,
|
|
56
|
+
type PostItem,
|
|
57
|
+
type ReelsItem,
|
|
58
|
+
type SectionLayoutOptions,
|
|
59
|
+
type HtmlSectionOptions,
|
|
60
|
+
} from "./primitives.js";
|
|
61
|
+
|
|
62
|
+
const assertString = (value: unknown, name: string): void => {
|
|
63
|
+
if (typeof value !== "string") throw new TypeError(`${name} harus string`);
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
const asRecord = (value: unknown): Record<string, unknown> | undefined =>
|
|
67
|
+
value && typeof value === "object" && !Array.isArray(value)
|
|
68
|
+
? (value as Record<string, unknown>)
|
|
69
|
+
: undefined;
|
|
70
|
+
|
|
71
|
+
export interface AIRichConstructorOptions {
|
|
72
|
+
botJid?: string | undefined;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export interface RawNodeOptions {
|
|
76
|
+
id?: string | null | undefined;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export interface InsertNodeOptions extends RawNodeOptions {
|
|
80
|
+
after?: boolean | undefined;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export interface QuotableMessage {
|
|
84
|
+
key?: { id?: string; participant?: string; remoteJid?: string };
|
|
85
|
+
id?: string;
|
|
86
|
+
message?: Proto.IMessage | Record<string, unknown>;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export type RichTarget = string | number;
|
|
90
|
+
|
|
91
|
+
export class AIRich extends BaseBuilder {
|
|
92
|
+
#nodes = new NodeStore();
|
|
93
|
+
#contextInfo: Record<string, unknown>;
|
|
94
|
+
#quote: QuoteInfo | null;
|
|
95
|
+
#footer: string;
|
|
96
|
+
#botJid: string | null;
|
|
97
|
+
#lastId: string | null;
|
|
98
|
+
#pendingEdits = new Map<string, string>();
|
|
99
|
+
#responseId: string = randomUUID();
|
|
100
|
+
|
|
101
|
+
constructor(client?: ZapoClient, { botJid }: AIRichConstructorOptions = {}) {
|
|
102
|
+
super(client);
|
|
103
|
+
if (
|
|
104
|
+
botJid !== undefined &&
|
|
105
|
+
(typeof botJid !== "string" || !botJid.trim())
|
|
106
|
+
) {
|
|
107
|
+
throw new TypeError("botJid harus string non-empty");
|
|
108
|
+
}
|
|
109
|
+
this.#botJid = botJid ?? null;
|
|
110
|
+
this.#contextInfo = {};
|
|
111
|
+
this.#quote = null;
|
|
112
|
+
this.#footer = "";
|
|
113
|
+
this.#lastId = null;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
get lastId(): string | null {
|
|
117
|
+
return this.#lastId;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
get size(): number {
|
|
121
|
+
return this.#nodes.size;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
ids(): string[] {
|
|
125
|
+
return this.#nodes.ids();
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
has(id: string): boolean {
|
|
129
|
+
return this.#nodes.has(id);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
pendingEdit(jid: string): string | null {
|
|
133
|
+
return this.#pendingEdits.get(jid) ?? null;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
raw(
|
|
137
|
+
section: UnifiedResponseSection,
|
|
138
|
+
submessage?: SubmessagePayload,
|
|
139
|
+
{ id }: RawNodeOptions = {},
|
|
140
|
+
): this {
|
|
141
|
+
if (!section || typeof section !== "object")
|
|
142
|
+
throw new ValidationError("section harus object");
|
|
143
|
+
const node = this.#nodes.makeNode({ id, section, submessage });
|
|
144
|
+
this.#nodes.push(node);
|
|
145
|
+
return this;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
replace(
|
|
149
|
+
target: RichTarget,
|
|
150
|
+
section: UnifiedResponseSection,
|
|
151
|
+
submessage?: SubmessagePayload,
|
|
152
|
+
{ id }: RawNodeOptions = {},
|
|
153
|
+
): this {
|
|
154
|
+
if (!section || typeof section !== "object")
|
|
155
|
+
throw new ValidationError("section harus object");
|
|
156
|
+
|
|
157
|
+
const node = this.#resolveTargetNode(target);
|
|
158
|
+
const replacement = this.#nodes.makeNode({ id, section, submessage });
|
|
159
|
+
this.#nodes.replaceAt(this.#nodes.indexOf(node), replacement);
|
|
160
|
+
return this;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
insert(
|
|
164
|
+
target: RichTarget,
|
|
165
|
+
section: UnifiedResponseSection,
|
|
166
|
+
submessage?: SubmessagePayload,
|
|
167
|
+
{ id, after = true }: InsertNodeOptions = {},
|
|
168
|
+
): this {
|
|
169
|
+
if (!section || typeof section !== "object")
|
|
170
|
+
throw new ValidationError("section harus object");
|
|
171
|
+
|
|
172
|
+
const node = this.#resolveTargetNode(target);
|
|
173
|
+
const index = this.#nodes.indexOf(node);
|
|
174
|
+
const insertion = this.#nodes.makeNode({ id, section, submessage });
|
|
175
|
+
this.#nodes.insertAt(after ? index + 1 : index, insertion);
|
|
176
|
+
return this;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
insertAt(
|
|
180
|
+
index: number,
|
|
181
|
+
section: UnifiedResponseSection,
|
|
182
|
+
submessage?: SubmessagePayload,
|
|
183
|
+
{ id }: RawNodeOptions = {},
|
|
184
|
+
): this {
|
|
185
|
+
if (!section || typeof section !== "object")
|
|
186
|
+
throw new ValidationError("section harus object");
|
|
187
|
+
|
|
188
|
+
const insertion = this.#nodes.makeNode({ id, section, submessage });
|
|
189
|
+
this.#nodes.insertAt(index, insertion);
|
|
190
|
+
return this;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
loadPayload(payload: unknown): this {
|
|
194
|
+
if (!payload || typeof payload !== "object")
|
|
195
|
+
throw new ValidationError("payload harus object");
|
|
196
|
+
|
|
197
|
+
const payloadRecord = payload as Record<string, unknown>;
|
|
198
|
+
const message = asRecord(payloadRecord.message) ?? payloadRecord;
|
|
199
|
+
const botForwarded = asRecord(message.botForwardedMessage);
|
|
200
|
+
const botForwardedInner = botForwarded
|
|
201
|
+
? asRecord(botForwarded.message)
|
|
202
|
+
: undefined;
|
|
203
|
+
const rich =
|
|
204
|
+
(botForwardedInner
|
|
205
|
+
? asRecord(botForwardedInner.richResponseMessage)
|
|
206
|
+
: undefined) ??
|
|
207
|
+
(botForwarded ? asRecord(botForwarded.richResponseMessage) : undefined) ??
|
|
208
|
+
asRecord(message.richResponseMessage);
|
|
209
|
+
|
|
210
|
+
if (!rich)
|
|
211
|
+
throw new ValidationError("payload tidak mengandung richResponseMessage");
|
|
212
|
+
|
|
213
|
+
let sections: unknown[] = [];
|
|
214
|
+
let responseId: string | null = null;
|
|
215
|
+
|
|
216
|
+
const unifiedResponse = asRecord(rich.unifiedResponse);
|
|
217
|
+
const unifiedData = unifiedResponse ? unifiedResponse.data : undefined;
|
|
218
|
+
if (unifiedData) {
|
|
219
|
+
const unified = asRecord(
|
|
220
|
+
decodeUnifiedResponse(unifiedData as string | Uint8Array),
|
|
221
|
+
);
|
|
222
|
+
if (unified && Array.isArray(unified.sections))
|
|
223
|
+
sections = structuredClone(unified.sections);
|
|
224
|
+
if (
|
|
225
|
+
unified &&
|
|
226
|
+
typeof unified.response_id === "string" &&
|
|
227
|
+
unified.response_id
|
|
228
|
+
)
|
|
229
|
+
responseId = unified.response_id;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
const submessages = Array.isArray(rich.submessages)
|
|
233
|
+
? (structuredClone(rich.submessages) as unknown[])
|
|
234
|
+
: [];
|
|
235
|
+
|
|
236
|
+
this.#nodes.clear();
|
|
237
|
+
const count = Math.max(sections.length, submessages.length);
|
|
238
|
+
for (let i = 0; i < count; i++) {
|
|
239
|
+
this.#nodes.push(
|
|
240
|
+
this.#nodes.makeNode({
|
|
241
|
+
section: (sections[i] ?? null) as UnifiedResponseSection | null,
|
|
242
|
+
submessage: (submessages[i] ?? null) as SubmessagePayload | null,
|
|
243
|
+
}),
|
|
244
|
+
);
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
const loaded = asRecord(rich.contextInfo);
|
|
248
|
+
if (loaded) {
|
|
249
|
+
const clone = structuredClone(loaded);
|
|
250
|
+
const {
|
|
251
|
+
forwardingScore: _fs,
|
|
252
|
+
isForwarded: _if,
|
|
253
|
+
forwardOrigin: _fo,
|
|
254
|
+
forwardedAiBotMessageInfo,
|
|
255
|
+
...rest
|
|
256
|
+
} = clone;
|
|
257
|
+
this.#contextInfo = rest;
|
|
258
|
+
const botInfo = asRecord(forwardedAiBotMessageInfo);
|
|
259
|
+
if (botInfo && typeof botInfo.botJid === "string" && botInfo.botJid) {
|
|
260
|
+
this.#botJid = botInfo.botJid;
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
if (responseId) this.#responseId = responseId;
|
|
265
|
+
|
|
266
|
+
return this;
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
text(
|
|
270
|
+
text: string,
|
|
271
|
+
{
|
|
272
|
+
id,
|
|
273
|
+
hyperlink = true,
|
|
274
|
+
citation = true,
|
|
275
|
+
latex = true,
|
|
276
|
+
}: RawNodeOptions & ParseInlineOptions = {},
|
|
277
|
+
): this {
|
|
278
|
+
assertString(text, "text");
|
|
279
|
+
const parsed = parseInline(text, { hyperlink, citation, latex });
|
|
280
|
+
|
|
281
|
+
return this.raw(
|
|
282
|
+
layout("Single", {
|
|
283
|
+
text: parsed.text,
|
|
284
|
+
...(parsed.entities.length && { inline_entities: parsed.entities }),
|
|
285
|
+
__typename: "GenAIMarkdownTextUXPrimitive",
|
|
286
|
+
}),
|
|
287
|
+
{
|
|
288
|
+
messageType: proto.AIRichResponseSubMessageType.AI_RICH_RESPONSE_TEXT,
|
|
289
|
+
messageText: text,
|
|
290
|
+
},
|
|
291
|
+
{ id },
|
|
292
|
+
);
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
code(language: string, code: string, { id }: RawNodeOptions = {}): this {
|
|
296
|
+
assertString(language, "language");
|
|
297
|
+
assertString(code, "code");
|
|
298
|
+
const tokens = tokenizeCode(code, language);
|
|
299
|
+
|
|
300
|
+
return this.raw(
|
|
301
|
+
layout("Single", {
|
|
302
|
+
language,
|
|
303
|
+
code_blocks: tokens.unified_codeBlock,
|
|
304
|
+
__typename: "GenAICodeUXPrimitive",
|
|
305
|
+
}),
|
|
306
|
+
{
|
|
307
|
+
messageType: proto.AIRichResponseSubMessageType.AI_RICH_RESPONSE_CODE,
|
|
308
|
+
codeMetadata: { codeLanguage: language, codeBlocks: tokens.codeBlock },
|
|
309
|
+
},
|
|
310
|
+
{ id },
|
|
311
|
+
);
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
table(
|
|
315
|
+
rows: string[][],
|
|
316
|
+
{
|
|
317
|
+
id,
|
|
318
|
+
hyperlink = true,
|
|
319
|
+
citation = true,
|
|
320
|
+
latex = true,
|
|
321
|
+
}: RawNodeOptions & ParseInlineOptions = {},
|
|
322
|
+
): this {
|
|
323
|
+
const { unified, legacy } = toTable(rows, { hyperlink, citation, latex });
|
|
324
|
+
|
|
325
|
+
return this.raw(
|
|
326
|
+
layout("Single", { rows: unified, __typename: "GenATableUXPrimitive" }),
|
|
327
|
+
{
|
|
328
|
+
messageType: proto.AIRichResponseSubMessageType.AI_RICH_RESPONSE_TABLE,
|
|
329
|
+
tableMetadata: { title: "", rows: legacy },
|
|
330
|
+
},
|
|
331
|
+
{ id },
|
|
332
|
+
);
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
metadata(text: string, { id }: RawNodeOptions = {}): this {
|
|
336
|
+
assertString(text, "text");
|
|
337
|
+
|
|
338
|
+
return this.raw(
|
|
339
|
+
layout("Single", { text, __typename: "GenAIMetadataTextPrimitive" }),
|
|
340
|
+
{
|
|
341
|
+
messageType: proto.AIRichResponseSubMessageType.AI_RICH_RESPONSE_TEXT,
|
|
342
|
+
messageText: text,
|
|
343
|
+
},
|
|
344
|
+
{ id },
|
|
345
|
+
);
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
tip(text: string, { id }: RawNodeOptions = {}): this {
|
|
349
|
+
assertString(text, "text");
|
|
350
|
+
|
|
351
|
+
return this.raw(
|
|
352
|
+
layout("Single", {
|
|
353
|
+
text: "ⓘ " + text,
|
|
354
|
+
__typename: "GenAIMetadataTextPrimitive",
|
|
355
|
+
}),
|
|
356
|
+
{
|
|
357
|
+
messageType: proto.AIRichResponseSubMessageType.AI_RICH_RESPONSE_TEXT,
|
|
358
|
+
messageText: text,
|
|
359
|
+
},
|
|
360
|
+
{ id },
|
|
361
|
+
);
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
suggest(
|
|
365
|
+
items: string | string[],
|
|
366
|
+
{
|
|
367
|
+
id,
|
|
368
|
+
scroll = true,
|
|
369
|
+
layout: layoutName,
|
|
370
|
+
}: {
|
|
371
|
+
id?: string | null | undefined;
|
|
372
|
+
scroll?: boolean | undefined;
|
|
373
|
+
layout?: string | undefined;
|
|
374
|
+
} = {},
|
|
375
|
+
): this {
|
|
376
|
+
const list = Array.isArray(items) ? items : [items];
|
|
377
|
+
if (!list.length || !list.every((item) => typeof item === "string")) {
|
|
378
|
+
throw new TypeError("suggest harus string atau array string");
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
const pills = list.map((prompt_text) => ({
|
|
382
|
+
prompt_text,
|
|
383
|
+
prompt_type: "SUGGESTED_PROMPT",
|
|
384
|
+
__typename: "GenAIFollowUpSuggestionPillPrimitive",
|
|
385
|
+
}));
|
|
386
|
+
|
|
387
|
+
const type =
|
|
388
|
+
layoutName ??
|
|
389
|
+
(pills.length === 1 ? "Single" : scroll ? "HScroll" : "ActionRow");
|
|
390
|
+
const first = pills[0];
|
|
391
|
+
|
|
392
|
+
return this.raw(
|
|
393
|
+
layout(type, type === "Single" && first !== undefined ? first : pills, {
|
|
394
|
+
__typename: "GenAIUnifiedResponseSection",
|
|
395
|
+
}),
|
|
396
|
+
undefined,
|
|
397
|
+
{ id },
|
|
398
|
+
);
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
source(
|
|
402
|
+
sources: SourceInput[],
|
|
403
|
+
{ id, layout: layoutName }: RawNodeOptions & SectionLayoutOptions = {},
|
|
404
|
+
): this {
|
|
405
|
+
return this.raw(sourceSection(sources, { layout: layoutName }), undefined, {
|
|
406
|
+
id,
|
|
407
|
+
});
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
image(
|
|
411
|
+
url: string,
|
|
412
|
+
{ id, ...options }: RawNodeOptions & ImageSectionOptions = {},
|
|
413
|
+
): this {
|
|
414
|
+
return this.raw(imageSection(url, options), undefined, { id });
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
video(
|
|
418
|
+
url: string,
|
|
419
|
+
{ id, ...options }: RawNodeOptions & VideoSectionOptions = {},
|
|
420
|
+
): this {
|
|
421
|
+
return this.raw(videoSection(url, options), undefined, { id });
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
footerAction(
|
|
425
|
+
data: FooterActionItem | FooterActionItem[],
|
|
426
|
+
{ id, layout: layoutName }: RawNodeOptions & SectionLayoutOptions = {},
|
|
427
|
+
): this {
|
|
428
|
+
return this.raw(
|
|
429
|
+
footerActionSection(data, { layout: layoutName }),
|
|
430
|
+
undefined,
|
|
431
|
+
{ id },
|
|
432
|
+
);
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
widget(
|
|
436
|
+
data: WidgetItem | WidgetItem[],
|
|
437
|
+
{ id, layout: layoutName }: RawNodeOptions & SectionLayoutOptions = {},
|
|
438
|
+
): this {
|
|
439
|
+
return this.raw(widgetSection(data, { layout: layoutName }), undefined, {
|
|
440
|
+
id,
|
|
441
|
+
});
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
product(
|
|
445
|
+
data: ProductItem | ProductItem[],
|
|
446
|
+
{ id, layout: layoutName }: RawNodeOptions & SectionLayoutOptions = {},
|
|
447
|
+
): this {
|
|
448
|
+
return this.raw(productSection(data, { layout: layoutName }), undefined, {
|
|
449
|
+
id,
|
|
450
|
+
});
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
post(
|
|
454
|
+
data: PostItem | PostItem[],
|
|
455
|
+
{ id, layout: layoutName }: RawNodeOptions & SectionLayoutOptions = {},
|
|
456
|
+
): this {
|
|
457
|
+
return this.raw(postSection(data, { layout: layoutName }), undefined, {
|
|
458
|
+
id,
|
|
459
|
+
});
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
reels(
|
|
463
|
+
items: ReelsItem | ReelsItem[],
|
|
464
|
+
{ id, layout: layoutName }: RawNodeOptions & SectionLayoutOptions = {},
|
|
465
|
+
): this {
|
|
466
|
+
return this.raw(reelsSection(items, { layout: layoutName }), undefined, {
|
|
467
|
+
id,
|
|
468
|
+
});
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
html(
|
|
472
|
+
html: string,
|
|
473
|
+
{ id, ...options }: RawNodeOptions & HtmlSectionOptions = {},
|
|
474
|
+
): this {
|
|
475
|
+
return this.raw(htmlSection(html, options), undefined, { id });
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
footer(text: string): this {
|
|
479
|
+
assertString(text, "footer");
|
|
480
|
+
this.#footer = text;
|
|
481
|
+
return this;
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
context(info: AllowedContextInfo | Record<string, unknown>): this {
|
|
485
|
+
this.#contextInfo = { ...this.#contextInfo, ...filterContextInfo(info) };
|
|
486
|
+
return this;
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
quote(message: QuotableMessage): this {
|
|
490
|
+
const id = message?.key?.id ?? message?.id;
|
|
491
|
+
if (!id) throw new ValidationError("quote butuh pesan yang punya key.id");
|
|
492
|
+
|
|
493
|
+
this.#quote = {
|
|
494
|
+
stanzaId: id,
|
|
495
|
+
participant: message.key?.participant ?? message.key?.remoteJid,
|
|
496
|
+
quotedType: proto.ContextInfo.QuotedType.EXPLICIT,
|
|
497
|
+
...(message.message && { quotedMessage: message.message }),
|
|
498
|
+
};
|
|
499
|
+
return this;
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
remove(target: RichTarget): this {
|
|
503
|
+
if (typeof target === "number") {
|
|
504
|
+
if (
|
|
505
|
+
!Number.isInteger(target) ||
|
|
506
|
+
target < 0 ||
|
|
507
|
+
target >= this.#nodes.size
|
|
508
|
+
) {
|
|
509
|
+
throw new ItemNotFoundError(target, this.ids());
|
|
510
|
+
}
|
|
511
|
+
this.#nodes.removeAt(target);
|
|
512
|
+
return this;
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
if (typeof target !== "string" || !this.#nodes.has(target)) {
|
|
516
|
+
throw new ItemNotFoundError(target, this.ids());
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
this.#nodes.removeNode(this.#nodes.get(target));
|
|
520
|
+
return this;
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
clear(): this {
|
|
524
|
+
this.#nodes.clear();
|
|
525
|
+
this.#footer = "";
|
|
526
|
+
this.#pendingEdits.clear();
|
|
527
|
+
this.#responseId = randomUUID();
|
|
528
|
+
this.#lastId = null;
|
|
529
|
+
return this;
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
#resolveTargetNode(target: RichTarget): RichNode {
|
|
533
|
+
if (typeof target === "number") {
|
|
534
|
+
if (
|
|
535
|
+
!Number.isInteger(target) ||
|
|
536
|
+
target < 0 ||
|
|
537
|
+
target >= this.#nodes.size
|
|
538
|
+
) {
|
|
539
|
+
throw new InvalidTargetError(
|
|
540
|
+
`index node ${target} di luar rentang (0-${this.#nodes.size - 1})`,
|
|
541
|
+
{ target },
|
|
542
|
+
);
|
|
543
|
+
}
|
|
544
|
+
return this.#nodes.nodes[target] as RichNode;
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
if (typeof target !== "string" || !target.trim()) {
|
|
548
|
+
throw new InvalidTargetError("target harus id string atau index number", {
|
|
549
|
+
target,
|
|
550
|
+
});
|
|
551
|
+
}
|
|
552
|
+
|
|
553
|
+
return this.#nodes.get(target);
|
|
554
|
+
}
|
|
555
|
+
|
|
556
|
+
#sections(): UnifiedResponseSection[] {
|
|
557
|
+
if (!this.#nodes.size)
|
|
558
|
+
throw new ValidationError("minimal satu konten sebelum build/send");
|
|
559
|
+
|
|
560
|
+
const sections = this.#nodes.nodes
|
|
561
|
+
.filter((node) => node.section !== null)
|
|
562
|
+
.map((node) => node.section as UnifiedResponseSection);
|
|
563
|
+
const footer = this.#footer;
|
|
564
|
+
if (footer) {
|
|
565
|
+
sections.push(
|
|
566
|
+
layout("Single", {
|
|
567
|
+
text: footer,
|
|
568
|
+
__typename: "GenAIMetadataTextPrimitive",
|
|
569
|
+
}),
|
|
570
|
+
);
|
|
571
|
+
}
|
|
572
|
+
return sections;
|
|
573
|
+
}
|
|
574
|
+
|
|
575
|
+
#submessages(enabled: boolean): SubmessagePayload[] {
|
|
576
|
+
if (!enabled) return [];
|
|
577
|
+
return this.#nodes.nodes
|
|
578
|
+
.map((node) => node.submessage)
|
|
579
|
+
.filter((sub): sub is SubmessagePayload => Boolean(sub));
|
|
580
|
+
}
|
|
581
|
+
|
|
582
|
+
#context(withContext: boolean): Record<string, unknown> {
|
|
583
|
+
return withContext
|
|
584
|
+
? buildContextInfo({
|
|
585
|
+
botJid: this.#botJid,
|
|
586
|
+
quote: this.#quote,
|
|
587
|
+
contextInfo: this.#contextInfo,
|
|
588
|
+
})
|
|
589
|
+
: buildContextInfo({ botJid: this.#botJid });
|
|
590
|
+
}
|
|
591
|
+
|
|
592
|
+
#rich({
|
|
593
|
+
sections,
|
|
594
|
+
submessages = [],
|
|
595
|
+
withContext = false,
|
|
596
|
+
}: {
|
|
597
|
+
sections: UnifiedResponseSection[];
|
|
598
|
+
submessages?: SubmessagePayload[];
|
|
599
|
+
withContext?: boolean;
|
|
600
|
+
}): RichResponseBuildResult {
|
|
601
|
+
return buildRichResponse({
|
|
602
|
+
responseId: this.#responseId,
|
|
603
|
+
sections,
|
|
604
|
+
submessages,
|
|
605
|
+
contextInfo: this.#context(withContext),
|
|
606
|
+
});
|
|
607
|
+
}
|
|
608
|
+
|
|
609
|
+
override build({
|
|
610
|
+
submessages = true,
|
|
611
|
+
}: { submessages?: boolean } = {}): WrapBotForwardedResult {
|
|
612
|
+
return wrapBotForwarded(
|
|
613
|
+
this.#rich({
|
|
614
|
+
sections: this.#sections(),
|
|
615
|
+
submessages: this.#submessages(submessages),
|
|
616
|
+
}),
|
|
617
|
+
);
|
|
618
|
+
}
|
|
619
|
+
|
|
620
|
+
buildPlaceholder(): PlaceholderBuildResult {
|
|
621
|
+
return buildPlaceholderContent({
|
|
622
|
+
responseId: this.#responseId,
|
|
623
|
+
contextInfo: this.#context(true),
|
|
624
|
+
});
|
|
625
|
+
}
|
|
626
|
+
|
|
627
|
+
buildEdit(
|
|
628
|
+
jid: string,
|
|
629
|
+
id: string | null,
|
|
630
|
+
{ submessages = true }: { submessages?: boolean } = {},
|
|
631
|
+
): EditBuildResult {
|
|
632
|
+
if (typeof id !== "string" || !id.trim()) {
|
|
633
|
+
throw new ValidationError(
|
|
634
|
+
"buildEdit butuh ID pesan berupa string yang valid",
|
|
635
|
+
);
|
|
636
|
+
}
|
|
637
|
+
|
|
638
|
+
return buildEditContent({ jid, id, rich: this.build({ submessages }) });
|
|
639
|
+
}
|
|
640
|
+
|
|
641
|
+
override async send(jid?: string, arg?: SendResolutionArg): Promise<unknown> {
|
|
642
|
+
const { client, options } = this._resolve(arg);
|
|
643
|
+
const { twoStep = true, submessages = true, ...sendOptions } = options;
|
|
644
|
+
|
|
645
|
+
if (!twoStep) {
|
|
646
|
+
const direct = wrapBotForwarded(
|
|
647
|
+
this.#rich({
|
|
648
|
+
sections: this.#sections(),
|
|
649
|
+
submessages: this.#submessages(Boolean(submessages)),
|
|
650
|
+
withContext: true,
|
|
651
|
+
}),
|
|
652
|
+
);
|
|
653
|
+
const result = await sendViaZapo(
|
|
654
|
+
client,
|
|
655
|
+
jid as string,
|
|
656
|
+
direct,
|
|
657
|
+
sendOptions,
|
|
658
|
+
);
|
|
659
|
+
this.#lastId = result?.id ?? null;
|
|
660
|
+
return result;
|
|
661
|
+
}
|
|
662
|
+
|
|
663
|
+
const sent = await sendPlaceholderThenEdit({
|
|
664
|
+
client,
|
|
665
|
+
jid: jid as string,
|
|
666
|
+
placeholder: this.buildPlaceholder(),
|
|
667
|
+
buildEdit: (messageId) =>
|
|
668
|
+
this.buildEdit(jid as string, messageId, {
|
|
669
|
+
submessages: Boolean(submessages),
|
|
670
|
+
}),
|
|
671
|
+
options: sendOptions,
|
|
672
|
+
onPlaceholderSent: (target, messageId) => {
|
|
673
|
+
this.#lastId = messageId;
|
|
674
|
+
this.#pendingEdits.set(target, messageId);
|
|
675
|
+
},
|
|
676
|
+
});
|
|
677
|
+
|
|
678
|
+
this.#pendingEdits.delete(jid as string);
|
|
679
|
+
return sent;
|
|
680
|
+
}
|
|
681
|
+
|
|
682
|
+
async edit(
|
|
683
|
+
jid: string,
|
|
684
|
+
id: string | null = this.#lastId,
|
|
685
|
+
arg?: SendResolutionArg,
|
|
686
|
+
): Promise<unknown> {
|
|
687
|
+
if (typeof id !== "string" || !id.trim()) {
|
|
688
|
+
throw new ValidationError("edit butuh ID pesan berupa string yang valid");
|
|
689
|
+
}
|
|
690
|
+
|
|
691
|
+
const { client, options } = this._resolve(arg);
|
|
692
|
+
const { submessages = false, ...sendOptions } = options;
|
|
693
|
+
|
|
694
|
+
const result = await sendViaZapo(
|
|
695
|
+
client,
|
|
696
|
+
jid,
|
|
697
|
+
this.buildEdit(jid, id, { submessages: Boolean(submessages) }),
|
|
698
|
+
sendOptions,
|
|
699
|
+
);
|
|
700
|
+
this.#pendingEdits.delete(jid);
|
|
701
|
+
return result;
|
|
702
|
+
}
|
|
703
|
+
}
|
|
704
|
+
|
|
705
|
+
export function airich(
|
|
706
|
+
client?: ZapoClient,
|
|
707
|
+
options?: AIRichConstructorOptions,
|
|
708
|
+
): AIRich;
|
|
709
|
+
export function airich(
|
|
710
|
+
client: ZapoClient | undefined,
|
|
711
|
+
jid: string,
|
|
712
|
+
config: AIRichConfig,
|
|
713
|
+
options?: AIRichSendOptions,
|
|
714
|
+
): Promise<MessageSendResult>;
|
|
715
|
+
export function airich(
|
|
716
|
+
client?: ZapoClient,
|
|
717
|
+
first?: string | AIRichConstructorOptions,
|
|
718
|
+
second?: AIRichConfig,
|
|
719
|
+
third?: AIRichSendOptions,
|
|
720
|
+
): AIRich | Promise<MessageSendResult> {
|
|
721
|
+
if (typeof first === "string") {
|
|
722
|
+
if (second === undefined) return new AIRich(client);
|
|
723
|
+
return sendAIRich(client, first, second, third);
|
|
724
|
+
}
|
|
725
|
+
return new AIRich(client, first);
|
|
726
|
+
}
|
|
727
|
+
|
|
728
|
+
export interface AIRichSendOptions extends MessageSendOptions {
|
|
729
|
+
twoStep?: boolean;
|
|
730
|
+
submessages?: boolean;
|
|
731
|
+
}
|
|
732
|
+
|
|
733
|
+
export type AIRichSectionConfig =
|
|
734
|
+
| {
|
|
735
|
+
type: "text";
|
|
736
|
+
text: string;
|
|
737
|
+
hyperlink?: boolean;
|
|
738
|
+
citation?: boolean;
|
|
739
|
+
latex?: boolean;
|
|
740
|
+
id?: string | null;
|
|
741
|
+
}
|
|
742
|
+
| { type: "code"; language: string; code: string; id?: string | null }
|
|
743
|
+
| {
|
|
744
|
+
type: "table";
|
|
745
|
+
rows: string[][];
|
|
746
|
+
hyperlink?: boolean;
|
|
747
|
+
citation?: boolean;
|
|
748
|
+
latex?: boolean;
|
|
749
|
+
id?: string | null;
|
|
750
|
+
}
|
|
751
|
+
| { type: "metadata"; text: string; id?: string | null }
|
|
752
|
+
| { type: "tip"; text: string; id?: string | null }
|
|
753
|
+
| {
|
|
754
|
+
type: "suggest";
|
|
755
|
+
items: string | readonly string[];
|
|
756
|
+
scroll?: boolean;
|
|
757
|
+
layout?: string;
|
|
758
|
+
id?: string | null;
|
|
759
|
+
}
|
|
760
|
+
| {
|
|
761
|
+
type: "source";
|
|
762
|
+
sources: readonly SourceInput[];
|
|
763
|
+
layout?: string;
|
|
764
|
+
id?: string | null;
|
|
765
|
+
}
|
|
766
|
+
| {
|
|
767
|
+
type: "image";
|
|
768
|
+
url: string;
|
|
769
|
+
width?: number;
|
|
770
|
+
height?: number;
|
|
771
|
+
status?: string;
|
|
772
|
+
updateText?: string;
|
|
773
|
+
id?: string | null;
|
|
774
|
+
}
|
|
775
|
+
| {
|
|
776
|
+
type: "video";
|
|
777
|
+
url: string;
|
|
778
|
+
mimeType?: string;
|
|
779
|
+
fileLength?: number;
|
|
780
|
+
duration?: number;
|
|
781
|
+
status?: string;
|
|
782
|
+
id?: string | null;
|
|
783
|
+
}
|
|
784
|
+
| {
|
|
785
|
+
type: "footerAction";
|
|
786
|
+
items: FooterActionItem | readonly FooterActionItem[];
|
|
787
|
+
layout?: string;
|
|
788
|
+
id?: string | null;
|
|
789
|
+
}
|
|
790
|
+
| {
|
|
791
|
+
type: "widget";
|
|
792
|
+
items: WidgetItem | readonly WidgetItem[];
|
|
793
|
+
layout?: string;
|
|
794
|
+
id?: string | null;
|
|
795
|
+
}
|
|
796
|
+
| {
|
|
797
|
+
type: "product";
|
|
798
|
+
items: ProductItem | readonly ProductItem[];
|
|
799
|
+
layout?: string;
|
|
800
|
+
id?: string | null;
|
|
801
|
+
}
|
|
802
|
+
| {
|
|
803
|
+
type: "post";
|
|
804
|
+
items: PostItem | readonly PostItem[];
|
|
805
|
+
layout?: string;
|
|
806
|
+
id?: string | null;
|
|
807
|
+
}
|
|
808
|
+
| {
|
|
809
|
+
type: "reels";
|
|
810
|
+
items: ReelsItem | readonly ReelsItem[];
|
|
811
|
+
layout?: string;
|
|
812
|
+
id?: string | null;
|
|
813
|
+
}
|
|
814
|
+
| {
|
|
815
|
+
type: "html";
|
|
816
|
+
html: string;
|
|
817
|
+
trustedSources?: readonly unknown[];
|
|
818
|
+
id?: string | null;
|
|
819
|
+
}
|
|
820
|
+
| {
|
|
821
|
+
type: "raw";
|
|
822
|
+
section: UnifiedResponseSection;
|
|
823
|
+
submessage?: SubmessagePayload;
|
|
824
|
+
id?: string | null;
|
|
825
|
+
};
|
|
826
|
+
|
|
827
|
+
export interface AIRichConfig {
|
|
828
|
+
sections: readonly AIRichSectionConfig[];
|
|
829
|
+
footer?: string;
|
|
830
|
+
context?: AllowedContextInfo | Record<string, unknown>;
|
|
831
|
+
quote?: QuotableMessage;
|
|
832
|
+
}
|
|
833
|
+
|
|
834
|
+
const isItemArray = <T>(
|
|
835
|
+
items: T | ReadonlyArray<T>,
|
|
836
|
+
): items is ReadonlyArray<T> => Array.isArray(items);
|
|
837
|
+
|
|
838
|
+
const mutableItems = <T>(items: T | ReadonlyArray<T>): T | T[] =>
|
|
839
|
+
isItemArray(items) ? [...items] : items;
|
|
840
|
+
|
|
841
|
+
const applyAIRichSection = (
|
|
842
|
+
builder: AIRich,
|
|
843
|
+
section: AIRichSectionConfig,
|
|
844
|
+
): void => {
|
|
845
|
+
if (section.type === "text") {
|
|
846
|
+
const { text, hyperlink, citation, latex, id } = section;
|
|
847
|
+
builder.text(text, {
|
|
848
|
+
...(id !== undefined ? { id } : {}),
|
|
849
|
+
...(hyperlink !== undefined ? { hyperlink } : {}),
|
|
850
|
+
...(citation !== undefined ? { citation } : {}),
|
|
851
|
+
...(latex !== undefined ? { latex } : {}),
|
|
852
|
+
});
|
|
853
|
+
} else if (section.type === "code") {
|
|
854
|
+
builder.code(section.language, section.code, {
|
|
855
|
+
...(section.id !== undefined ? { id: section.id } : {}),
|
|
856
|
+
});
|
|
857
|
+
} else if (section.type === "table") {
|
|
858
|
+
const { rows, hyperlink, citation, latex, id } = section;
|
|
859
|
+
builder.table(rows, {
|
|
860
|
+
...(id !== undefined ? { id } : {}),
|
|
861
|
+
...(hyperlink !== undefined ? { hyperlink } : {}),
|
|
862
|
+
...(citation !== undefined ? { citation } : {}),
|
|
863
|
+
...(latex !== undefined ? { latex } : {}),
|
|
864
|
+
});
|
|
865
|
+
} else if (section.type === "metadata") {
|
|
866
|
+
builder.metadata(section.text, {
|
|
867
|
+
...(section.id !== undefined ? { id: section.id } : {}),
|
|
868
|
+
});
|
|
869
|
+
} else if (section.type === "tip") {
|
|
870
|
+
builder.tip(section.text, {
|
|
871
|
+
...(section.id !== undefined ? { id: section.id } : {}),
|
|
872
|
+
});
|
|
873
|
+
} else if (section.type === "suggest") {
|
|
874
|
+
const { items, scroll, layout: layoutName, id } = section;
|
|
875
|
+
builder.suggest(mutableItems(items), {
|
|
876
|
+
...(id !== undefined ? { id } : {}),
|
|
877
|
+
...(scroll !== undefined ? { scroll } : {}),
|
|
878
|
+
...(layoutName !== undefined ? { layout: layoutName } : {}),
|
|
879
|
+
});
|
|
880
|
+
} else if (section.type === "source") {
|
|
881
|
+
const { sources, layout: layoutName, id } = section;
|
|
882
|
+
builder.source([...sources], {
|
|
883
|
+
...(id !== undefined ? { id } : {}),
|
|
884
|
+
...(layoutName !== undefined ? { layout: layoutName } : {}),
|
|
885
|
+
});
|
|
886
|
+
} else if (section.type === "image") {
|
|
887
|
+
const { url, width, height, status, updateText, id } = section;
|
|
888
|
+
builder.image(url, {
|
|
889
|
+
...(id !== undefined ? { id } : {}),
|
|
890
|
+
...(width !== undefined ? { width } : {}),
|
|
891
|
+
...(height !== undefined ? { height } : {}),
|
|
892
|
+
...(status !== undefined ? { status } : {}),
|
|
893
|
+
...(updateText !== undefined ? { update_text: updateText } : {}),
|
|
894
|
+
});
|
|
895
|
+
} else if (section.type === "video") {
|
|
896
|
+
const { url, mimeType, fileLength, duration, status, id } = section;
|
|
897
|
+
builder.video(url, {
|
|
898
|
+
...(id !== undefined ? { id } : {}),
|
|
899
|
+
...(mimeType !== undefined ? { mime_type: mimeType } : {}),
|
|
900
|
+
...(fileLength !== undefined ? { file_length: fileLength } : {}),
|
|
901
|
+
...(duration !== undefined ? { duration } : {}),
|
|
902
|
+
...(status !== undefined ? { status } : {}),
|
|
903
|
+
});
|
|
904
|
+
} else if (section.type === "footerAction") {
|
|
905
|
+
const { items, layout: layoutName, id } = section;
|
|
906
|
+
builder.footerAction(mutableItems(items), {
|
|
907
|
+
...(id !== undefined ? { id } : {}),
|
|
908
|
+
...(layoutName !== undefined ? { layout: layoutName } : {}),
|
|
909
|
+
});
|
|
910
|
+
} else if (section.type === "widget") {
|
|
911
|
+
const { items, layout: layoutName, id } = section;
|
|
912
|
+
builder.widget(mutableItems(items), {
|
|
913
|
+
...(id !== undefined ? { id } : {}),
|
|
914
|
+
...(layoutName !== undefined ? { layout: layoutName } : {}),
|
|
915
|
+
});
|
|
916
|
+
} else if (section.type === "product") {
|
|
917
|
+
const { items, layout: layoutName, id } = section;
|
|
918
|
+
builder.product(mutableItems(items), {
|
|
919
|
+
...(id !== undefined ? { id } : {}),
|
|
920
|
+
...(layoutName !== undefined ? { layout: layoutName } : {}),
|
|
921
|
+
});
|
|
922
|
+
} else if (section.type === "post") {
|
|
923
|
+
const { items, layout: layoutName, id } = section;
|
|
924
|
+
builder.post(mutableItems(items), {
|
|
925
|
+
...(id !== undefined ? { id } : {}),
|
|
926
|
+
...(layoutName !== undefined ? { layout: layoutName } : {}),
|
|
927
|
+
});
|
|
928
|
+
} else if (section.type === "reels") {
|
|
929
|
+
const { items, layout: layoutName, id } = section;
|
|
930
|
+
builder.reels(mutableItems(items), {
|
|
931
|
+
...(id !== undefined ? { id } : {}),
|
|
932
|
+
...(layoutName !== undefined ? { layout: layoutName } : {}),
|
|
933
|
+
});
|
|
934
|
+
} else if (section.type === "html") {
|
|
935
|
+
const { html, trustedSources, id } = section;
|
|
936
|
+
builder.html(html, {
|
|
937
|
+
...(id !== undefined ? { id } : {}),
|
|
938
|
+
...(trustedSources !== undefined
|
|
939
|
+
? { trusted_sources: [...trustedSources] }
|
|
940
|
+
: {}),
|
|
941
|
+
});
|
|
942
|
+
} else {
|
|
943
|
+
builder.raw(section.section, section.submessage, {
|
|
944
|
+
id: section.id,
|
|
945
|
+
});
|
|
946
|
+
}
|
|
947
|
+
};
|
|
948
|
+
|
|
949
|
+
export const applyAIRichConfig = (
|
|
950
|
+
builder: AIRich,
|
|
951
|
+
config: AIRichConfig,
|
|
952
|
+
): void => {
|
|
953
|
+
for (const section of config.sections) {
|
|
954
|
+
applyAIRichSection(builder, section);
|
|
955
|
+
}
|
|
956
|
+
if (config.footer !== undefined) builder.footer(config.footer);
|
|
957
|
+
if (config.context !== undefined) builder.context(config.context);
|
|
958
|
+
if (config.quote !== undefined) builder.quote(config.quote);
|
|
959
|
+
};
|
|
960
|
+
|
|
961
|
+
export const sendAIRich = async (
|
|
962
|
+
client: ZapoClient | undefined,
|
|
963
|
+
jid: string | undefined,
|
|
964
|
+
config: AIRichConfig,
|
|
965
|
+
options?: AIRichSendOptions,
|
|
966
|
+
): Promise<MessageSendResult> => {
|
|
967
|
+
const builder = new AIRich(client);
|
|
968
|
+
applyAIRichConfig(builder, config);
|
|
969
|
+
return sendConfigured(builder, jid, options);
|
|
970
|
+
};
|