@vanzxy/baileys 1.4.1 → 1.4.2

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.

Potentially problematic release.


This version of @vanzxy/baileys might be problematic. Click here for more details.

Files changed (111) hide show
  1. package/cjs/WAProto/index.js +106053 -0
  2. package/cjs/lib/Defaults/index.js +168 -0
  3. package/cjs/lib/Signal/Group/ciphertext-message.js +16 -0
  4. package/cjs/lib/Signal/Group/group-session-builder.js +67 -0
  5. package/cjs/lib/Signal/Group/group_cipher.js +86 -0
  6. package/cjs/lib/Signal/Group/index.js +58 -0
  7. package/cjs/lib/Signal/Group/keyhelper.js +56 -0
  8. package/cjs/lib/Signal/Group/sender-chain-key.js +30 -0
  9. package/cjs/lib/Signal/Group/sender-key-distribution-message.js +67 -0
  10. package/cjs/lib/Signal/Group/sender-key-message.js +70 -0
  11. package/cjs/lib/Signal/Group/sender-key-name.js +52 -0
  12. package/cjs/lib/Signal/Group/sender-key-record.js +45 -0
  13. package/cjs/lib/Signal/Group/sender-key-state.js +88 -0
  14. package/cjs/lib/Signal/Group/sender-message-key.js +30 -0
  15. package/cjs/lib/Signal/libsignal.js +467 -0
  16. package/cjs/lib/Signal/lid-mapping.js +281 -0
  17. package/cjs/lib/Socket/Client/index.js +19 -0
  18. package/cjs/lib/Socket/Client/types.js +15 -0
  19. package/cjs/lib/Socket/Client/websocket.js +61 -0
  20. package/cjs/lib/Socket/business.js +384 -0
  21. package/cjs/lib/Socket/chats.js +1257 -0
  22. package/cjs/lib/Socket/communities.js +442 -0
  23. package/cjs/lib/Socket/dugong.js +753 -0
  24. package/cjs/lib/Socket/groups.js +360 -0
  25. package/cjs/lib/Socket/index.js +19 -0
  26. package/cjs/lib/Socket/messages-recv.js +1790 -0
  27. package/cjs/lib/Socket/messages-send.js +1390 -0
  28. package/cjs/lib/Socket/mex.js +46 -0
  29. package/cjs/lib/Socket/newsletter.js +340 -0
  30. package/cjs/lib/Socket/socket.js +1007 -0
  31. package/cjs/lib/Store/index.js +20 -0
  32. package/cjs/lib/Store/make-in-memory-store.js +438 -0
  33. package/cjs/lib/Store/make-ordered-dictionary.js +82 -0
  34. package/cjs/lib/Store/object-repository.js +28 -0
  35. package/cjs/lib/Types/Auth.js +3 -0
  36. package/cjs/lib/Types/Bussines.js +3 -0
  37. package/cjs/lib/Types/Call.js +3 -0
  38. package/cjs/lib/Types/Chat.js +11 -0
  39. package/cjs/lib/Types/Contact.js +3 -0
  40. package/cjs/lib/Types/Events.js +4 -0
  41. package/cjs/lib/Types/GroupMetadata.js +3 -0
  42. package/cjs/lib/Types/Label.js +26 -0
  43. package/cjs/lib/Types/LabelAssociation.js +8 -0
  44. package/cjs/lib/Types/Message.js +19 -0
  45. package/cjs/lib/Types/Mex.js +40 -0
  46. package/cjs/lib/Types/Product.js +3 -0
  47. package/cjs/lib/Types/RichType.js +24 -0
  48. package/cjs/lib/Types/Signal.js +4 -0
  49. package/cjs/lib/Types/Socket.js +4 -0
  50. package/cjs/lib/Types/State.js +54 -0
  51. package/cjs/lib/Types/USync.js +4 -0
  52. package/cjs/lib/Types/index.js +42 -0
  53. package/cjs/lib/Utils/MessageBuilder.js +2164 -0
  54. package/cjs/lib/Utils/auth-utils.js +312 -0
  55. package/cjs/lib/Utils/browser-utils.js +32 -0
  56. package/cjs/lib/Utils/business.js +245 -0
  57. package/cjs/lib/Utils/chat-utils.js +937 -0
  58. package/cjs/lib/Utils/companion-reg-client-utils.js +45 -0
  59. package/cjs/lib/Utils/crypto.js +196 -0
  60. package/cjs/lib/Utils/decode-wa-message.js +324 -0
  61. package/cjs/lib/Utils/event-buffer.js +643 -0
  62. package/cjs/lib/Utils/generics.js +429 -0
  63. package/cjs/lib/Utils/history.js +151 -0
  64. package/cjs/lib/Utils/identity-change-handler.js +56 -0
  65. package/cjs/lib/Utils/index.js +42 -0
  66. package/cjs/lib/Utils/link-preview.js +122 -0
  67. package/cjs/lib/Utils/logger.js +8 -0
  68. package/cjs/lib/Utils/lt-hash.js +62 -0
  69. package/cjs/lib/Utils/make-mutex.js +38 -0
  70. package/cjs/lib/Utils/message-retry-manager.js +267 -0
  71. package/cjs/lib/Utils/messages-media.js +933 -0
  72. package/cjs/lib/Utils/messages.js +2175 -0
  73. package/cjs/lib/Utils/noise-handler.js +205 -0
  74. package/cjs/lib/Utils/offline-node-processor.js +43 -0
  75. package/cjs/lib/Utils/pre-key-manager.js +113 -0
  76. package/cjs/lib/Utils/process-message.js +772 -0
  77. package/cjs/lib/Utils/reporting-utils.js +263 -0
  78. package/cjs/lib/Utils/rich-message-utils.js +439 -0
  79. package/cjs/lib/Utils/signal.js +214 -0
  80. package/cjs/lib/Utils/stanza-ack.js +41 -0
  81. package/cjs/lib/Utils/sync-action-utils.js +54 -0
  82. package/cjs/lib/Utils/tc-token-utils.js +174 -0
  83. package/cjs/lib/Utils/use-multi-file-auth-state.js +125 -0
  84. package/cjs/lib/Utils/use-single-file-auth-state.js +121 -0
  85. package/cjs/lib/Utils/use-sqlite-auth-state.js +182 -0
  86. package/cjs/lib/Utils/validate-connection.js +210 -0
  87. package/cjs/lib/WABinary/constants.js +1470 -0
  88. package/cjs/lib/WABinary/decode.js +301 -0
  89. package/cjs/lib/WABinary/encode.js +257 -0
  90. package/cjs/lib/WABinary/generic-utils.js +274 -0
  91. package/cjs/lib/WABinary/index.js +22 -0
  92. package/cjs/lib/WABinary/jid-utils.js +114 -0
  93. package/cjs/lib/WABinary/types.js +37 -0
  94. package/cjs/lib/WAM/BinaryInfo.js +14 -0
  95. package/cjs/lib/WAM/constants.js +22856 -0
  96. package/cjs/lib/WAM/encode.js +154 -0
  97. package/cjs/lib/WAM/index.js +20 -0
  98. package/cjs/lib/WAUSync/Protocols/USyncContactProtocol.js +56 -0
  99. package/cjs/lib/WAUSync/Protocols/USyncDeviceProtocol.js +67 -0
  100. package/cjs/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +31 -0
  101. package/cjs/lib/WAUSync/Protocols/USyncStatusProtocol.js +42 -0
  102. package/cjs/lib/WAUSync/Protocols/USyncUsernameProtocol.js +29 -0
  103. package/cjs/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +55 -0
  104. package/cjs/lib/WAUSync/Protocols/UsyncLIDProtocol.js +33 -0
  105. package/cjs/lib/WAUSync/Protocols/index.js +24 -0
  106. package/cjs/lib/WAUSync/USyncQuery.js +126 -0
  107. package/cjs/lib/WAUSync/USyncUser.js +35 -0
  108. package/cjs/lib/WAUSync/index.js +20 -0
  109. package/cjs/lib/index.js +34 -0
  110. package/cjs/package.json +1 -0
  111. package/package.json +16 -5
@@ -0,0 +1,263 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getMessageReportingToken = exports.shouldIncludeReportingToken = void 0;
4
+ const crypto_1 = require("crypto");
5
+ const index_js_1 = require("../../WAProto/index.js");
6
+ const crypto_js_1 = require("./crypto.js");
7
+ const reportingFields = [
8
+ { f: 1 },
9
+ {
10
+ f: 3,
11
+ s: [{ f: 2 }, { f: 3 }, { f: 8 }, { f: 11 }, { f: 17, s: [{ f: 21 }, { f: 22 }] }, { f: 25 }]
12
+ },
13
+ { f: 4, s: [{ f: 1 }, { f: 16 }, { f: 17, s: [{ f: 21 }, { f: 22 }] }] },
14
+ { f: 5, s: [{ f: 3 }, { f: 4 }, { f: 5 }, { f: 16 }, { f: 17, s: [{ f: 21 }, { f: 22 }] }] },
15
+ { f: 6, s: [{ f: 1 }, { f: 17, s: [{ f: 21 }, { f: 22 }] }, { f: 30 }] },
16
+ { f: 7, s: [{ f: 2 }, { f: 7 }, { f: 10 }, { f: 17, s: [{ f: 21 }, { f: 22 }] }, { f: 20 }] },
17
+ { f: 8, s: [{ f: 2 }, { f: 7 }, { f: 9 }, { f: 17, s: [{ f: 21 }, { f: 22 }] }, { f: 21 }] },
18
+ { f: 9, s: [{ f: 2 }, { f: 6 }, { f: 7 }, { f: 13 }, { f: 17, s: [{ f: 21 }, { f: 22 }] }, { f: 20 }] },
19
+ { f: 12, s: [{ f: 1 }, { f: 2 }, { f: 14, m: true }, { f: 15 }] },
20
+ { f: 18, s: [{ f: 6 }, { f: 16 }, { f: 17, s: [{ f: 21 }, { f: 22 }] }] },
21
+ { f: 26, s: [{ f: 4 }, { f: 5 }, { f: 8 }, { f: 13 }, { f: 17, s: [{ f: 21 }, { f: 22 }] }] },
22
+ { f: 28, s: [{ f: 1 }, { f: 2 }, { f: 4 }, { f: 5 }, { f: 6 }, { f: 7, s: [{ f: 21 }, { f: 22 }] }] },
23
+ { f: 37, s: [{ f: 1, m: true }] },
24
+ {
25
+ f: 49,
26
+ s: [
27
+ { f: 2 },
28
+ { f: 3, s: [{ f: 1 }, { f: 2 }] },
29
+ { f: 5, s: [{ f: 21 }, { f: 22 }] },
30
+ { f: 8, s: [{ f: 1 }, { f: 2 }] }
31
+ ]
32
+ },
33
+ { f: 53, s: [{ f: 1, m: true }] },
34
+ { f: 55, s: [{ f: 1, m: true }] },
35
+ { f: 58, s: [{ f: 1, m: true }] },
36
+ { f: 59, s: [{ f: 1, m: true }] },
37
+ {
38
+ f: 60,
39
+ s: [
40
+ { f: 2 },
41
+ { f: 3, s: [{ f: 1 }, { f: 2 }] },
42
+ { f: 5, s: [{ f: 21 }, { f: 22 }] },
43
+ { f: 8, s: [{ f: 1 }, { f: 2 }] }
44
+ ]
45
+ },
46
+ {
47
+ f: 64,
48
+ s: [
49
+ { f: 2 },
50
+ { f: 3, s: [{ f: 1 }, { f: 2 }] },
51
+ { f: 5, s: [{ f: 21 }, { f: 22 }] },
52
+ { f: 8, s: [{ f: 1 }, { f: 2 }] }
53
+ ]
54
+ },
55
+ { f: 66, s: [{ f: 2 }, { f: 6 }, { f: 7 }, { f: 13 }, { f: 17, s: [{ f: 21 }, { f: 22 }] }, { f: 20 }] },
56
+ { f: 74, s: [{ f: 1, m: true }] },
57
+ { f: 87, s: [{ f: 1, m: true }] },
58
+ { f: 88, s: [{ f: 1 }, { f: 2, s: [{ f: 1 }] }, { f: 3, s: [{ f: 21 }, { f: 22 }] }] },
59
+ { f: 92, s: [{ f: 1, m: true }] },
60
+ { f: 93, s: [{ f: 1, m: true }] },
61
+ { f: 94, s: [{ f: 1, m: true }] }
62
+ ];
63
+ const compileReportingFields = (fields) => {
64
+ const map = new Map();
65
+ for (const f of fields) {
66
+ map.set(f.f, {
67
+ m: f.m,
68
+ children: f.s ? compileReportingFields(f.s) : undefined
69
+ });
70
+ }
71
+ return map;
72
+ };
73
+ const compiledReportingFields = compileReportingFields(reportingFields);
74
+ const EMPTY_MAP = new Map();
75
+ const ENC_SECRET_REPORT_TOKEN = 'Report Token';
76
+ const WIRE = {
77
+ VARINT: 0,
78
+ FIXED64: 1,
79
+ BYTES: 2,
80
+ FIXED32: 5
81
+ };
82
+ const shouldIncludeReportingToken = (message) => !message.reactionMessage &&
83
+ !message.encReactionMessage &&
84
+ !message.encEventResponseMessage &&
85
+ !message.pollUpdateMessage;
86
+ exports.shouldIncludeReportingToken = shouldIncludeReportingToken;
87
+ const generateMsgSecretKey = (modificationType, origMsgId, origMsgSender, modificationSender, origMsgSecret) => {
88
+ const useCaseSecret = Buffer.concat([
89
+ Buffer.from(origMsgId, 'utf8'),
90
+ Buffer.from(origMsgSender, 'utf8'),
91
+ Buffer.from(modificationSender, 'utf8'),
92
+ Buffer.from(modificationType, 'utf8')
93
+ ]);
94
+ return (0, crypto_js_1.hkdf)(origMsgSecret, 32, { info: useCaseSecret.toString('latin1') });
95
+ };
96
+ const extractReportingTokenContent = (data, cfg) => {
97
+ const out = [];
98
+ let i = 0;
99
+ while (i < data.length) {
100
+ const tag = decodeVarint(data, i);
101
+ if (!tag.ok) {
102
+ return null;
103
+ }
104
+ const fieldNum = tag.value >> 3;
105
+ const wireType = tag.value & 0x7;
106
+ const fieldStart = i;
107
+ i += tag.bytes;
108
+ const fieldCfg = cfg.get(fieldNum);
109
+ const pushSlice = (end) => {
110
+ if (end > data.length) {
111
+ return false;
112
+ }
113
+ out.push({ num: fieldNum, bytes: data.subarray(fieldStart, end) });
114
+ i = end;
115
+ return true;
116
+ };
117
+ const skip = (end) => {
118
+ if (end > data.length) {
119
+ return false;
120
+ }
121
+ i = end;
122
+ return true;
123
+ };
124
+ if (wireType === WIRE.VARINT) {
125
+ const v = decodeVarint(data, i);
126
+ if (!v.ok) {
127
+ return null;
128
+ }
129
+ const end = i + v.bytes;
130
+ if (!fieldCfg) {
131
+ if (!skip(end)) {
132
+ return null;
133
+ }
134
+ continue;
135
+ }
136
+ if (!pushSlice(end)) {
137
+ return null;
138
+ }
139
+ continue;
140
+ }
141
+ if (wireType === WIRE.FIXED64) {
142
+ const end = i + 8;
143
+ if (!fieldCfg) {
144
+ if (!skip(end)) {
145
+ return null;
146
+ }
147
+ continue;
148
+ }
149
+ if (!pushSlice(end)) {
150
+ return null;
151
+ }
152
+ continue;
153
+ }
154
+ if (wireType === WIRE.FIXED32) {
155
+ const end = i + 4;
156
+ if (!fieldCfg) {
157
+ if (!skip(end)) {
158
+ return null;
159
+ }
160
+ continue;
161
+ }
162
+ if (!pushSlice(end)) {
163
+ return null;
164
+ }
165
+ continue;
166
+ }
167
+ if (wireType === WIRE.BYTES) {
168
+ const len = decodeVarint(data, i);
169
+ if (!len.ok) {
170
+ return null;
171
+ }
172
+ const valStart = i + len.bytes;
173
+ const valEnd = valStart + len.value;
174
+ if (valEnd > data.length) {
175
+ return null;
176
+ }
177
+ if (!fieldCfg) {
178
+ i = valEnd;
179
+ continue;
180
+ }
181
+ if (fieldCfg.m || fieldCfg.children) {
182
+ const sub = extractReportingTokenContent(data.subarray(valStart, valEnd), fieldCfg.children ?? EMPTY_MAP);
183
+ if (sub === null) {
184
+ return null;
185
+ }
186
+ if (sub.length > 0) {
187
+ const newTag = encodeVarint(tag.value);
188
+ const newLen = encodeVarint(sub.length);
189
+ out.push({
190
+ num: fieldNum,
191
+ bytes: Buffer.concat([newTag, newLen, sub])
192
+ });
193
+ }
194
+ i = valEnd;
195
+ continue;
196
+ }
197
+ out.push({ num: fieldNum, bytes: data.subarray(fieldStart, valEnd) });
198
+ i = valEnd;
199
+ continue;
200
+ }
201
+ return null;
202
+ }
203
+ if (out.length === 0) {
204
+ return Buffer.alloc(0);
205
+ }
206
+ out.sort((a, b) => a.num - b.num);
207
+ return Buffer.concat(out.map(f => f.bytes));
208
+ };
209
+ const decodeVarint = (buffer, offset) => {
210
+ let value = 0;
211
+ let bytes = 0;
212
+ let shift = 0;
213
+ while (offset + bytes < buffer.length) {
214
+ const current = buffer[offset + bytes];
215
+ value |= (current & 0x7f) << shift;
216
+ bytes++;
217
+ if ((current & 0x80) === 0) {
218
+ return { value, bytes, ok: true };
219
+ }
220
+ shift += 7;
221
+ if (shift > 35) {
222
+ return { value: 0, bytes: 0, ok: false };
223
+ }
224
+ }
225
+ return { value: 0, bytes: 0, ok: false };
226
+ };
227
+ const encodeVarint = (value) => {
228
+ const parts = [];
229
+ let remaining = value >>> 0;
230
+ while (remaining > 0x7f) {
231
+ parts.push((remaining & 0x7f) | 0x80);
232
+ remaining >>>= 7;
233
+ }
234
+ parts.push(remaining);
235
+ return Buffer.from(parts);
236
+ };
237
+ const getMessageReportingToken = async (msgProtobuf, message, key) => {
238
+ const msgSecret = message.messageContextInfo?.messageSecret;
239
+ if (!msgSecret || !key.id) {
240
+ return null;
241
+ }
242
+ const from = key.fromMe ? key.remoteJid : key.participant || key.remoteJid;
243
+ const to = key.fromMe ? key.participant || key.remoteJid : key.remoteJid;
244
+ const reportingSecret = generateMsgSecretKey(ENC_SECRET_REPORT_TOKEN, key.id, from, to, msgSecret);
245
+ const content = extractReportingTokenContent(msgProtobuf, compiledReportingFields);
246
+ if (!content || content.length === 0) {
247
+ return null;
248
+ }
249
+ const reportingToken = (0, crypto_1.createHmac)('sha256', reportingSecret).update(content).digest().subarray(0, 16);
250
+ return {
251
+ tag: 'reporting',
252
+ attrs: {},
253
+ content: [
254
+ {
255
+ tag: 'reporting_token',
256
+ attrs: { v: '2' },
257
+ content: reportingToken
258
+ }
259
+ ]
260
+ };
261
+ };
262
+ exports.getMessageReportingToken = getMessageReportingToken;
263
+ //# sourceMappingURL=reporting-utils.js.map
@@ -0,0 +1,439 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.wrapToBotForwardedMessage = exports.botMetadataCertificate = exports.botMetadataSignature = exports.prepareRichResponseMessage = exports.toUnified = exports.tokenizeCode = void 0;
4
+ /**
5
+ * Lia@Changes 09-04-26 [WIP]
6
+ * Adds support for tables and code blocks with richResponseMessage (wrapped inside botForwardedMessage).
7
+ *
8
+ * If you use or copy this code, please credit my name or project. AND DO NOT CHANGE THIS NOTE
9
+ */
10
+ const crypto_1 = require("crypto");
11
+ const index_js_1 = require("../Defaults/index.js");
12
+ const constants_js_1 = require("../WABinary/constants.js");
13
+ const RichType_js_1 = require("../Types/RichType.js");
14
+ const index_js_2 = require("../../WAProto/index.js");
15
+ const generics_js_1 = require("./generics.js");
16
+ const NOOP = new Set([]);
17
+ // Vanz@Fix (bug 40 / inline image): AIRichResponseInlineImageMetadata.imageUrl is a nested
18
+ // AIRichResponseImageURL message (imagePreviewUrl/imageHighResUrl/sourceUrl), NOT a plain string.
19
+ // Passing a raw string made protobufjs silently encode an empty embedded message (no throw),
20
+ // so the image URL data was dropped on the wire and the image never rendered.
21
+ const toImageUrlObject = (url) => {
22
+ if (!url)
23
+ return undefined;
24
+ if (typeof url === 'object')
25
+ return { imagePreviewUrl: url.imagePreviewUrl || url.url, imageHighResUrl: url.imageHighResUrl || url.url, sourceUrl: url.sourceUrl || url.url };
26
+ return { imagePreviewUrl: url, imageHighResUrl: url, sourceUrl: url };
27
+ };
28
+ const IMAGE_ALIGNMENT = { leading: 0, trailing: 1, center: 2 };
29
+ const toImageAlignment = (alignment) => {
30
+ if (typeof alignment === 'number')
31
+ return alignment;
32
+ return IMAGE_ALIGNMENT[String(alignment || 'center').toLowerCase()] ?? IMAGE_ALIGNMENT.center;
33
+ };
34
+ const tokenizeCode = (code, language = 'javascript') => {
35
+ const keywords = constants_js_1.LANGUAGE_KEYWORDS[language] || NOOP;
36
+ const blocks = [];
37
+ index_js_1.LEXER_REGEX.lastIndex = 0;
38
+ let match;
39
+ while ((match = index_js_1.LEXER_REGEX.exec(code)) !== null) {
40
+ if (match[1]) {
41
+ blocks.push({ highlightType: RichType_js_1.CodeHighlightType.COMMENT, codeContent: match[1] });
42
+ }
43
+ else if (match[2]) {
44
+ blocks.push({ highlightType: RichType_js_1.CodeHighlightType.STRING, codeContent: match[2] });
45
+ }
46
+ else if (match[3]) {
47
+ blocks.push({
48
+ highlightType: keywords.has(match[3]) ? RichType_js_1.CodeHighlightType.KEYWORD : RichType_js_1.CodeHighlightType.METHOD,
49
+ codeContent: match[3],
50
+ });
51
+ }
52
+ else if (match[4]) {
53
+ blocks.push({
54
+ highlightType: keywords.has(match[4]) ? RichType_js_1.CodeHighlightType.KEYWORD : RichType_js_1.CodeHighlightType.DEFAULT,
55
+ codeContent: match[4],
56
+ });
57
+ }
58
+ else if (match[5]) {
59
+ blocks.push({ highlightType: RichType_js_1.CodeHighlightType.NUMBER, codeContent: match[5] });
60
+ }
61
+ else {
62
+ blocks.push({ highlightType: RichType_js_1.CodeHighlightType.DEFAULT, codeContent: match[6] });
63
+ }
64
+ }
65
+ return blocks;
66
+ };
67
+ exports.tokenizeCode = tokenizeCode;
68
+ // Lia@Changes 09-04-26 --- Inject buffer into unifiedResponse.data to support proper rendering of rich messages (ex: tables and code blocks)
69
+ const toUnified = (submessages, uuid) => ({
70
+ response_id: uuid || (0, crypto_1.randomUUID)(),
71
+ sections: submessages.map((submessage, index) => {
72
+ switch (submessage.messageType) {
73
+ case RichType_js_1.RichSubMessageType.CODE:
74
+ const codeMetadata = submessage.codeMetadata;
75
+ return {
76
+ view_model: {
77
+ primitive: {
78
+ language: codeMetadata.codeLanguage,
79
+ code_blocks: codeMetadata.codeBlocks.map((block) => ({ content: block.codeContent, type: RichType_js_1.CodeHighlightType[block.highlightType] })),
80
+ __typename: 'GenAICodeUXPrimitive'
81
+ },
82
+ __typename: 'GenAISingleLayoutViewModel'
83
+ }
84
+ };
85
+ case RichType_js_1.RichSubMessageType.CONTENT_ITEMS:
86
+ // Vanz@Fix (bug 40): was returning {} — now renders content items carousel
87
+ const contentItemsMeta = submessage.contentItemsMetadata;
88
+ return {
89
+ view_model: {
90
+ primitive: {
91
+ items: contentItemsMeta?.itemsMetadata || [],
92
+ content_type: contentItemsMeta?.contentType ?? 1,
93
+ __typename: 'GenAIContentItemsUXPrimitive'
94
+ },
95
+ __typename: 'GenAISingleLayoutViewModel'
96
+ }
97
+ };
98
+ case RichType_js_1.RichSubMessageType.INLINE_IMAGE:
99
+ // Vanz@Fix (bug 40, round 2): imageUrl is now a real AIRichResponseImageURL
100
+ // object (imagePreviewUrl/imageHighResUrl/sourceUrl) instead of a bare string
101
+ // that protobufjs silently dropped on encode. alignment is mapped back to its
102
+ // enum name (matches how the compiled proto's toObject() renders other enums).
103
+ const imgMeta = submessage.imageMetadata;
104
+ const ALIGNMENT_NAME = ['AI_RICH_RESPONSE_IMAGE_LAYOUT_LEADING_ALIGNED', 'AI_RICH_RESPONSE_IMAGE_LAYOUT_TRAILING_ALIGNED', 'AI_RICH_RESPONSE_IMAGE_LAYOUT_CENTER_ALIGNED'];
105
+ return {
106
+ view_model: {
107
+ primitive: {
108
+ image_url: {
109
+ image_preview_url: imgMeta?.imageUrl?.imagePreviewUrl || '',
110
+ image_high_res_url: imgMeta?.imageUrl?.imageHighResUrl || '',
111
+ source_url: imgMeta?.imageUrl?.sourceUrl || ''
112
+ },
113
+ image_text: imgMeta?.imageText || '',
114
+ alignment: ALIGNMENT_NAME[imgMeta?.alignment ?? 2],
115
+ tap_link_url: imgMeta?.tapLinkUrl || '',
116
+ __typename: 'GenAIInlineImageUXPrimitive'
117
+ },
118
+ __typename: 'GenAISingleLayoutViewModel'
119
+ }
120
+ };
121
+ case RichType_js_1.RichSubMessageType.LATEX:
122
+ // Vanz@Fix (bug 40): was returning {} — now renders LaTeX expressions
123
+ const latexMeta = submessage.latexMetadata;
124
+ return {
125
+ view_model: {
126
+ primitive: {
127
+ text: latexMeta?.text || '',
128
+ expressions: latexMeta?.expressions || [],
129
+ __typename: 'GenAILatexUXPrimitive'
130
+ },
131
+ __typename: 'GenAISingleLayoutViewModel'
132
+ }
133
+ };
134
+ case RichType_js_1.RichSubMessageType.TABLE:
135
+ const tableMetadata = submessage.tableMetadata;
136
+ return {
137
+ view_model: {
138
+ primitive: {
139
+ title: tableMetadata.title,
140
+ rows: tableMetadata.rows.map((row) => ({
141
+ is_header: row.isHeading,
142
+ cells: row.items,
143
+ markdown_cells: row.items.map((item) => ({ text: item }))
144
+ })),
145
+ __typename: 'GenATableUXPrimitive'
146
+ },
147
+ __typename: 'GenAISingleLayoutViewModel'
148
+ }
149
+ };
150
+ case RichType_js_1.RichSubMessageType.TEXT:
151
+ return {
152
+ view_model: {
153
+ primitive: {
154
+ text: submessage.messageText,
155
+ inline_entities: submessage.inlineEntities || [],
156
+ __typename: 'GenAIMarkdownTextUXPrimitive'
157
+ },
158
+ __typename: 'GenAISingleLayoutViewModel'
159
+ }
160
+ };
161
+ }
162
+ return submessage;
163
+ })
164
+ });
165
+ exports.toUnified = toUnified;
166
+ const prepareRichResponseMessage = (content) => {
167
+ const { alignment, code, contentText, disclaimerText, footerText, headerText, imageText, inlineImage, inlineVideo, items, language, latex, links, noHeading, posts, products, suggested, richResponse, table, tapLinkUrl, title } = content;
168
+ let submessages = [];
169
+ if (Array.isArray(richResponse)) {
170
+ submessages = richResponse.map((submessage) => {
171
+ if (submessage.text) {
172
+ return {
173
+ messageType: RichType_js_1.RichSubMessageType.TEXT,
174
+ messageText: submessage.text,
175
+ inlineEntities: submessage.inlineEntities
176
+ };
177
+ }
178
+ else if (submessage.code) {
179
+ return {
180
+ messageType: RichType_js_1.RichSubMessageType.CODE,
181
+ codeMetadata: {
182
+ codeLanguage: submessage.language,
183
+ codeBlocks: submessage.code
184
+ }
185
+ };
186
+ }
187
+ else if (submessage.items) {
188
+ return {
189
+ messageType: RichType_js_1.RichSubMessageType.CONTENT_ITEMS,
190
+ contentItemsMetadata: {
191
+ itemsMetadata: submessage.items,
192
+ contentType: index_js_2.proto.AIRichResponseContentItemsMetadata.ContentType.CAROUSEL
193
+ }
194
+ };
195
+ }
196
+ else if (submessage.inlineImage) {
197
+ return {
198
+ messageType: RichType_js_1.RichSubMessageType.INLINE_IMAGE,
199
+ imageMetadata: {
200
+ imageUrl: toImageUrlObject(submessage.inlineImage),
201
+ imageText: submessage.imageText,
202
+ alignment: toImageAlignment(submessage.alignment),
203
+ tapLinkUrl: submessage.tapLinkUrl
204
+ }
205
+ };
206
+ }
207
+ else if (submessage.inlineVideo) {
208
+ return {
209
+ messageType: RichType_js_1.RichSubMessageType.TEXT,
210
+ messageText: 'INLINE_VIDEO'
211
+ };
212
+ }
213
+ else if (submessage.latex) {
214
+ return {
215
+ messageType: RichType_js_1.RichSubMessageType.LATEX,
216
+ latexMetadata: {
217
+ text: submessage.text,
218
+ expressions: submessage.latex
219
+ }
220
+ };
221
+ }
222
+ else if (submessage.posts) {
223
+ return {
224
+ messageType: RichType_js_1.RichSubMessageType.TEXT,
225
+ messageText: 'POSTS'
226
+ };
227
+ }
228
+ else if (submessage.products) {
229
+ return {
230
+ messageType: RichType_js_1.RichSubMessageType.TEXT,
231
+ messageText: 'PRODUCTS'
232
+ };
233
+ }
234
+ else if (submessage.suggested) {
235
+ return {
236
+ messageType: RichType_js_1.RichSubMessageType.TEXT,
237
+ messageText: 'SUGGESTED_PROMPT'
238
+ };
239
+ }
240
+ else if (submessage.table) {
241
+ return {
242
+ messageType: RichType_js_1.RichSubMessageType.TABLE,
243
+ tableMetadata: {
244
+ title: submessage.title,
245
+ rows: submessage.table
246
+ }
247
+ };
248
+ }
249
+ return submessage;
250
+ });
251
+ }
252
+ else {
253
+ if (headerText) {
254
+ submessages.push({
255
+ messageType: RichType_js_1.RichSubMessageType.TEXT,
256
+ messageText: headerText
257
+ });
258
+ }
259
+ if (contentText) {
260
+ submessages.push({
261
+ messageType: RichType_js_1.RichSubMessageType.TEXT,
262
+ messageText: contentText
263
+ });
264
+ }
265
+ if (code) {
266
+ language ||= 'javascript';
267
+ submessages.push({
268
+ messageType: RichType_js_1.RichSubMessageType.CODE,
269
+ codeMetadata: {
270
+ codeLanguage: language,
271
+ codeBlocks: (0, exports.tokenizeCode)(code, language)
272
+ }
273
+ });
274
+ }
275
+ if (items) {
276
+ submessages.push({
277
+ messageType: RichType_js_1.RichSubMessageType.CONTENT_ITEMS,
278
+ contentItemsMetadata: {
279
+ itemsMetadata: items,
280
+ contentType: index_js_2.proto.AIRichResponseContentItemsMetadata.ContentType.CAROUSEL
281
+ }
282
+ });
283
+ }
284
+ if (inlineImage) {
285
+ submessages.push({
286
+ messageType: RichType_js_1.RichSubMessageType.INLINE_IMAGE,
287
+ imageMetadata: {
288
+ imageUrl: toImageUrlObject(inlineImage),
289
+ imageText,
290
+ alignment: toImageAlignment(alignment),
291
+ tapLinkUrl
292
+ }
293
+ });
294
+ }
295
+ if (inlineVideo) {
296
+ submessages.push({
297
+ messageType: RichType_js_1.RichSubMessageType.TEXT,
298
+ messageText: 'INLINE_VIDEO'
299
+ });
300
+ }
301
+ if (latex) {
302
+ submessages.push({
303
+ messageType: RichType_js_1.RichSubMessageType.LATEX,
304
+ latexMetadata: {
305
+ text,
306
+ expressions: latex
307
+ }
308
+ });
309
+ }
310
+ if (links) {
311
+ links.forEach((linkField, index) => {
312
+ const prefix = 'SS_' + index;
313
+ const url = linkField.url || index_js_1.DONATE_URL;
314
+ const sources = linkField.sources?.map((sourceField) => ({
315
+ source_type: 'THIRD_PARTY',
316
+ source_display_name: sourceField.displayName || 'Donate',
317
+ source_subtitle: sourceField.subtitle || 'Saweria',
318
+ source_url: sourceField.url || url
319
+ }));
320
+ submessages.push({
321
+ messageType: RichType_js_1.RichSubMessageType.TEXT,
322
+ messageText: linkField.text + ` {{${prefix}}}¹{{/${prefix}}} `,
323
+ inlineEntities: [{
324
+ key: prefix,
325
+ metadata: {
326
+ reference_id: index + 1,
327
+ reference_url: url,
328
+ reference_title: linkField.title || 'For Donation via Saweria',
329
+ reference_display_name: linkField.displayName || 'Donation',
330
+ sources: sources || [],
331
+ __typename: 'GenAISearchCitationItem'
332
+ }
333
+ }]
334
+ });
335
+ });
336
+ }
337
+ if (posts) {
338
+ submessages.push({
339
+ messageType: RichType_js_1.RichSubMessageType.TEXT,
340
+ messageText: 'POSTS'
341
+ });
342
+ }
343
+ if (products) {
344
+ submessages.push({
345
+ messageType: RichType_js_1.RichSubMessageType.TEXT,
346
+ messageText: 'PRODUCTS'
347
+ });
348
+ }
349
+ if (suggested) {
350
+ submessages.push({
351
+ messageType: RichType_js_1.RichSubMessageType.TEXT,
352
+ messageText: 'SUGGESTED_PROMPT'
353
+ });
354
+ }
355
+ if (table) {
356
+ submessages.push({
357
+ messageType: RichType_js_1.RichSubMessageType.TABLE,
358
+ tableMetadata: {
359
+ title,
360
+ rows: table.map((items, index) => ({
361
+ isHeading: !noHeading && index == 0,
362
+ items
363
+ }))
364
+ }
365
+ });
366
+ }
367
+ if (footerText) {
368
+ submessages.push({
369
+ messageType: RichType_js_1.RichSubMessageType.TEXT,
370
+ messageText: footerText
371
+ });
372
+ }
373
+ }
374
+ const uuid = (0, crypto_1.randomUUID)();
375
+ const unified = (0, exports.toUnified)(submessages, uuid);
376
+ const richResponseMessage = index_js_2.proto.AIRichResponseMessage.create({
377
+ submessages,
378
+ messageType: index_js_2.proto.AIRichResponseMessageType.AI_RICH_RESPONSE_TYPE_STANDARD,
379
+ unifiedResponse: {
380
+ data: Buffer.from(JSON.stringify(unified)) // Lia@Note 25-04-26 --- Expects "ArrayBufferLike"
381
+ },
382
+ contextInfo: {
383
+ isForwarded: true,
384
+ forwardingScore: 1,
385
+ forwardedAiBotMessageInfo: { botJid: '867051314767696@bot' },
386
+ forwardOrigin: 4
387
+ }
388
+ });
389
+ const message = (0, exports.wrapToBotForwardedMessage)(richResponseMessage);
390
+ const botMetadata = message.messageContextInfo.botMetadata;
391
+ // Lia@Note 13-05-26 --- Add disclaimer text on richResponseMessage
392
+ if (disclaimerText) {
393
+ botMetadata.messageDisclaimerText = disclaimerText;
394
+ }
395
+ // Lia@Note 15-05-26 --- Add responseId from unified directly to botMetadata
396
+ botMetadata.botResponseId = uuid;
397
+ return message;
398
+ };
399
+ exports.prepareRichResponseMessage = prepareRichResponseMessage;
400
+ // Lia@Note 17-04-26 --- signature and certificateChain for proofs[] field
401
+ const botMetadataSignature = () => {
402
+ const signature = new Uint8Array(64);
403
+ (0, crypto_1.getRandomValues)(signature);
404
+ return signature;
405
+ };
406
+ exports.botMetadataSignature = botMetadataSignature;
407
+ const botMetadataCertificate = (length = 685) => {
408
+ const certificate = new Uint8Array(length);
409
+ certificate[0] = 48;
410
+ certificate[1] = 130;
411
+ (0, crypto_1.getRandomValues)(certificate.subarray(2));
412
+ return certificate;
413
+ };
414
+ exports.botMetadataCertificate = botMetadataCertificate;
415
+ const wrapToBotForwardedMessage = (richResponseMessage) => ({
416
+ messageContextInfo: {
417
+ botMetadata: {
418
+ // Lia@Note 09-04-26 --- TODO: Fill verificationMetadata field
419
+ verificationMetadata: {
420
+ proofs: [
421
+ {
422
+ certificateChain: [
423
+ (0, exports.botMetadataCertificate)(),
424
+ (0, exports.botMetadataCertificate)(892)
425
+ ],
426
+ version: 1,
427
+ useCase: 1,
428
+ signature: (0, exports.botMetadataSignature)()
429
+ }
430
+ ]
431
+ }
432
+ }
433
+ },
434
+ botForwardedMessage: {
435
+ message: { richResponseMessage }
436
+ }
437
+ });
438
+ exports.wrapToBotForwardedMessage = wrapToBotForwardedMessage;
439
+ //# sourceMappingURL=rich-message-utils.js.map