@codexverified/baileys 2.7.5

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