@nuiisweety/baileys 0.1.1 → 0.1.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.
@@ -0,0 +1,246 @@
1
+ import { getRandomValues, randomUUID, randomBytes } from 'crypto';
2
+ import { DONATE_URL, LEXER_REGEX } from '../Defaults/index.js';
3
+ import { LANGUAGE_KEYWORDS } from '../WABinary/constants.js';
4
+ import { CodeHighlightType, RichSubMessageType } from '../Types/RichType.js';
5
+ import { proto } from '../../WAProto/index.js';
6
+ import { unixTimestampSeconds } from './generics.js';
7
+ const NOOP = new Set([]);
8
+ export const tokenizeCode = (code, language = 'javascript') => {
9
+ const keywords = LANGUAGE_KEYWORDS[language] || NOOP;
10
+ const blocks = [];
11
+ LEXER_REGEX.lastIndex = 0;
12
+ let match;
13
+ while ((match = LEXER_REGEX.exec(code)) !== null) {
14
+ if (match[1]) {
15
+ blocks.push({ highlightType: CodeHighlightType.COMMENT, codeContent: match[1] });
16
+ }
17
+ else if (match[2]) {
18
+ blocks.push({ highlightType: CodeHighlightType.STRING, codeContent: match[2] });
19
+ }
20
+ else if (match[3]) {
21
+ blocks.push({
22
+ highlightType: keywords.has(match[3]) ? CodeHighlightType.KEYWORD : CodeHighlightType.METHOD,
23
+ codeContent: match[3],
24
+ });
25
+ }
26
+ else if (match[4]) {
27
+ blocks.push({
28
+ highlightType: keywords.has(match[4]) ? CodeHighlightType.KEYWORD : CodeHighlightType.DEFAULT,
29
+ codeContent: match[4],
30
+ });
31
+ }
32
+ else if (match[5]) {
33
+ blocks.push({ highlightType: CodeHighlightType.NUMBER, codeContent: match[5] });
34
+ }
35
+ else {
36
+ blocks.push({ highlightType: CodeHighlightType.DEFAULT, codeContent: match[6] });
37
+ }
38
+ }
39
+ return blocks;
40
+ };
41
+ export const toUnified = (submessages) => ({
42
+ response_id: randomUUID(),
43
+ sections: submessages.map((submessage, index) => {
44
+ switch (submessage.messageType) {
45
+ case RichSubMessageType.CODE:
46
+ const codeMetadata = submessage.codeMetadata;
47
+ return {
48
+ view_model: {
49
+ primitive: {
50
+ language: codeMetadata.codeLanguage,
51
+ code_blocks: codeMetadata.codeBlocks.map((block) => ({ content: block.codeContent, type: CodeHighlightType[block.highlightType] })),
52
+ __typename: 'GenAICodeUXPrimitive'
53
+ },
54
+ __typename: 'GenAISingleLayoutViewModel'
55
+ }
56
+ };
57
+ case RichSubMessageType.TABLE:
58
+ const tableMetadata = submessage.tableMetadata;
59
+ return {
60
+ view_model: {
61
+ primitive: {
62
+ title: tableMetadata.title,
63
+ rows: tableMetadata.rows.map((row) => ({
64
+ is_header: row.isHeading,
65
+ cells: row.items,
66
+ markdown_cells: row.items.map((item) => ({ text: item }))
67
+ })),
68
+ __typename: 'GenATableUXPrimitive'
69
+ },
70
+ __typename: 'GenAISingleLayoutViewModel'
71
+ }
72
+ };
73
+ case RichSubMessageType.TEXT:
74
+ return {
75
+ view_model: {
76
+ primitive: {
77
+ text: submessage.messageText,
78
+ inline_entities: submessage.inlineEntities || [],
79
+ __typename: 'GenAIMarkdownTextUXPrimitive'
80
+ },
81
+ __typename: 'GenAISingleLayoutViewModel'
82
+ }
83
+ };
84
+ }
85
+ return submessage;
86
+ })
87
+ });
88
+ export const prepareRichResponseMessage = (content) => {
89
+ const { code, contentText, disclaimerText, footerText, headerText, language, links, noHeading, richResponse, table, title } = content;
90
+ let submessages = [];
91
+ if (Array.isArray(richResponse)) {
92
+ submessages = richResponse.map((submessage) => {
93
+ if (submessage.text) {
94
+ return {
95
+ messageType: RichSubMessageType.TEXT,
96
+ messageText: submessage.text,
97
+ inlineEntities: submessage.inlineEntities
98
+ };
99
+ }
100
+ else if (submessage.code) {
101
+ return {
102
+ messageType: RichSubMessageType.CODE,
103
+ codeMetadata: {
104
+ codeLanguage: submessage.language,
105
+ codeBlocks: submessage.code
106
+ }
107
+ };
108
+ }
109
+ else if (submessage.table) {
110
+ return {
111
+ messageType: RichSubMessageType.TABLE,
112
+ tableMetadata: {
113
+ title: submessage.title,
114
+ rows: submessage.table
115
+ }
116
+ };
117
+ }
118
+ return submessage;
119
+ });
120
+ }
121
+ else {
122
+ if (headerText) {
123
+ submessages.push({
124
+ messageType: RichSubMessageType.TEXT,
125
+ messageText: headerText
126
+ });
127
+ }
128
+ if (contentText) {
129
+ submessages.push({
130
+ messageType: RichSubMessageType.TEXT,
131
+ messageText: contentText
132
+ });
133
+ }
134
+ if (code) {
135
+ language ||= 'javascript';
136
+ submessages.push({
137
+ messageType: RichSubMessageType.CODE,
138
+ codeMetadata: {
139
+ codeLanguage: language,
140
+ codeBlocks: tokenizeCode(code, language)
141
+ }
142
+ });
143
+ }
144
+ else if (links) {
145
+ links.forEach((linkField, index) => {
146
+ const prefix = 'SS_' + index;
147
+ const url = linkField.url || DONATE_URL;
148
+ const sources = linkField.sources?.map((sourceField) => ({
149
+ source_type: 'THIRD_PARTY',
150
+ source_display_name: sourceField.displayName || 'Donate',
151
+ source_subtitle: sourceField.subtitle || 'Saweria',
152
+ source_url: sourceField.url || url
153
+ }));
154
+ submessages.push({
155
+ messageType: RichSubMessageType.TEXT,
156
+ messageText: linkField.text + ` {{${prefix}}}¹{{/${prefix}}} `,
157
+ inlineEntities: [{
158
+ key: prefix,
159
+ metadata: {
160
+ reference_id: index + 1,
161
+ reference_url: url,
162
+ reference_title: linkField.title || 'For Donation via Saweria',
163
+ reference_display_name: linkField.displayName || 'Donation',
164
+ sources: sources || [],
165
+ __typename: 'GenAISearchCitationItem'
166
+ }
167
+ }]
168
+ });
169
+ });
170
+ }
171
+ else if (table) {
172
+ submessages.push({
173
+ messageType: RichSubMessageType.TABLE,
174
+ tableMetadata: {
175
+ title,
176
+ rows: table.map((items, index) => ({
177
+ isHeading: !noHeading && index == 0,
178
+ items
179
+ }))
180
+ }
181
+ });
182
+ }
183
+ if (footerText) {
184
+ submessages.push({
185
+ messageType: RichSubMessageType.TEXT,
186
+ messageText: footerText
187
+ });
188
+ }
189
+ }
190
+ const unified = toUnified(submessages);
191
+ const richResponseMessage = proto.AIRichResponseMessage.create({
192
+ submessages,
193
+ messageType: proto.AIRichResponseMessageType.AI_RICH_RESPONSE_TYPE_STANDARD,
194
+ unifiedResponse: {
195
+ data: Buffer.from(JSON.stringify(unified), 'utf-8')
196
+ },
197
+ contextInfo: {
198
+ isForwarded: true,
199
+ forwardingScore: 1,
200
+ forwardedAiBotMessageInfo: { botJid: '867051314767696@bot' },
201
+ forwardOrigin: 4
202
+ }
203
+ });
204
+ const message = wrapToBotForwardedMessage(richResponseMessage);
205
+ const botMetadata = message.messageContextInfo.botMetadata;
206
+ if (disclaimerText) {
207
+ botMetadata.messageDisclaimerText = disclaimerText;
208
+ }
209
+ botMetadata.botResponseId = unified.response_id;
210
+ return message;
211
+ };
212
+ export const botMetadataSignature = () => {
213
+ const signature = new Uint8Array(64);
214
+ getRandomValues(signature);
215
+ return signature;
216
+ };
217
+ export const botMetadataCertificate = (length = 685) => {
218
+ const certificate = new Uint8Array(length);
219
+ certificate[0] = 48;
220
+ certificate[1] = 130;
221
+ getRandomValues(certificate.subarray(2));
222
+ return certificate;
223
+ };
224
+ export const wrapToBotForwardedMessage = (richResponseMessage) => ({
225
+ messageContextInfo: {
226
+ botMetadata: {
227
+ verificationMetadata: {
228
+ proofs: [
229
+ {
230
+ certificateChain: [
231
+ botMetadataCertificate(),
232
+ botMetadataCertificate(892)
233
+ ],
234
+ version: 1,
235
+ useCase: 1,
236
+ signature: botMetadataSignature()
237
+ }
238
+ ]
239
+ }
240
+ }
241
+ },
242
+ botForwardedMessage: {
243
+ message: { richResponseMessage }
244
+ }
245
+ });
246
+ //# sourceMappingURL=rich-message-utils.js.map
@@ -1354,6 +1354,90 @@ export const RUST_KEYWORDS = new Set([
1354
1354
  'where', 'while', 'async', 'await', 'dyn', 'abstract', 'become', 'box', 'do', 'final',
1355
1355
  'macro', 'override', 'priv', 'typeof', 'unsized', 'virtual', 'yield'
1356
1356
  ]);
1357
+ export const JAVA_KEYWORDS = new Set([
1358
+ 'abstract', 'assert', 'boolean', 'break', 'byte', 'case', 'catch', 'char', 'class', 'const',
1359
+ 'continue', 'default', 'do', 'double', 'else', 'enum', 'extends', 'final', 'finally', 'float',
1360
+ 'for', 'goto', 'if', 'implements', 'import', 'instanceof', 'int', 'interface', 'long', 'native',
1361
+ 'new', 'package', 'private', 'protected', 'public', 'return', 'short', 'static', 'strictfp',
1362
+ 'super', 'switch', 'synchronized', 'this', 'throw', 'throws', 'transient', 'try', 'void',
1363
+ 'volatile', 'while', 'true', 'false', 'null', 'var', 'record', 'sealed', 'permits', 'yield'
1364
+ ]);
1365
+ export const PHP_KEYWORDS = new Set([
1366
+ 'abstract', 'and', 'array', 'as', 'break', 'callable', 'case', 'catch', 'class', 'clone',
1367
+ 'const', 'continue', 'declare', 'default', 'do', 'echo', 'else', 'elseif', 'empty',
1368
+ 'enddeclare', 'endfor', 'endforeach', 'endif', 'endswitch', 'endwhile', 'enum', 'extends',
1369
+ 'final', 'finally', 'fn', 'for', 'foreach', 'function', 'global', 'goto', 'if', 'implements',
1370
+ 'include', 'include_once', 'instanceof', 'insteadof', 'interface', 'isset', 'list', 'match',
1371
+ 'namespace', 'new', 'or', 'print', 'private', 'protected', 'public', 'readonly', 'require',
1372
+ 'require_once', 'return', 'static', 'switch', 'throw', 'trait', 'try', 'unset', 'use',
1373
+ 'var', 'while', 'xor', 'yield', 'true', 'false', 'null'
1374
+ ]);
1375
+ export const RUBY_KEYWORDS = new Set([
1376
+ 'alias', 'and', 'begin', 'break', 'case', 'class', 'def', 'defined?', 'do', 'else', 'elsif',
1377
+ 'end', 'ensure', 'false', 'for', 'if', 'in', 'module', 'next', 'nil', 'not', 'or', 'redo',
1378
+ 'rescue', 'retry', 'return', 'self', 'super', 'then', 'true', 'undef', 'unless', 'until',
1379
+ 'when', 'while', 'yield', 'puts', 'print', 'require', 'require_relative', 'attr_accessor',
1380
+ 'attr_reader', 'attr_writer', 'private', 'protected', 'public', 'include', 'extend'
1381
+ ]);
1382
+ export const KOTLIN_KEYWORDS = new Set([
1383
+ 'as', 'as?', 'break', 'class', 'continue', 'do', 'else', 'false', 'for', 'fun', 'if', 'in',
1384
+ '!in', 'interface', 'is', '!is', 'null', 'object', 'package', 'return', 'super', 'this',
1385
+ 'throw', 'true', 'try', 'typealias', 'typeof', 'val', 'var', 'when', 'while', 'by', 'catch',
1386
+ 'constructor', 'delegate', 'dynamic', 'field', 'file', 'finally', 'get', 'import', 'init',
1387
+ 'param', 'property', 'receiver', 'set', 'setparam', 'value', 'where', 'abstract', 'actual',
1388
+ 'annotation', 'companion', 'crossinline', 'data', 'enum', 'expect', 'external', 'final',
1389
+ 'infix', 'inline', 'inner', 'internal', 'lateinit', 'noinline', 'open', 'operator', 'out',
1390
+ 'override', 'private', 'protected', 'public', 'reified', 'sealed', 'suspend', 'tailrec',
1391
+ 'vararg', 'it', 'Unit', 'Nothing', 'Any', 'Int', 'Long', 'Double', 'Float', 'Boolean',
1392
+ 'String', 'Char', 'Byte', 'Short', 'List', 'Map', 'Set', 'MutableList', 'MutableMap'
1393
+ ]);
1394
+ export const SWIFT_KEYWORDS = new Set([
1395
+ 'associatedtype', 'class', 'deinit', 'enum', 'extension', 'fileprivate', 'func', 'import',
1396
+ 'init', 'inout', 'internal', 'let', 'open', 'operator', 'private', 'precedencegroup',
1397
+ 'protocol', 'public', 'rethrows', 'static', 'struct', 'subscript', 'typealias', 'var',
1398
+ 'break', 'case', 'catch', 'continue', 'default', 'defer', 'do', 'else', 'fallthrough',
1399
+ 'for', 'guard', 'if', 'in', 'repeat', 'return', 'throw', 'switch', 'where', 'while',
1400
+ 'any', 'as', 'await', 'false', 'is', 'nil', 'self', 'Self', 'super', 'throws', 'true', 'try',
1401
+ 'async', 'convenience', 'dynamic', 'didSet', 'final', 'get', 'indirect', 'lazy', 'mutating',
1402
+ 'nonmutating', 'optional', 'override', 'postfix', 'prefix', 'required', 'set', 'some',
1403
+ 'unowned', 'weak', 'willSet', 'String', 'Int', 'Double', 'Float', 'Bool', 'Array', 'Dictionary'
1404
+ ]);
1405
+ export const SQL_KEYWORDS = new Set([
1406
+ 'SELECT', 'FROM', 'WHERE', 'INSERT', 'INTO', 'VALUES', 'UPDATE', 'SET', 'DELETE', 'CREATE',
1407
+ 'TABLE', 'DROP', 'ALTER', 'ADD', 'COLUMN', 'INDEX', 'VIEW', 'DATABASE', 'SCHEMA', 'TRUNCATE',
1408
+ 'JOIN', 'INNER', 'LEFT', 'RIGHT', 'FULL', 'OUTER', 'CROSS', 'ON', 'AS', 'UNION', 'ALL',
1409
+ 'DISTINCT', 'ORDER', 'BY', 'GROUP', 'HAVING', 'LIMIT', 'OFFSET', 'ASC', 'DESC',
1410
+ 'AND', 'OR', 'NOT', 'IN', 'LIKE', 'BETWEEN', 'IS', 'NULL', 'EXISTS', 'CASE', 'WHEN',
1411
+ 'THEN', 'ELSE', 'END', 'IF', 'COALESCE', 'NULLIF', 'CAST', 'CONVERT',
1412
+ 'COUNT', 'SUM', 'AVG', 'MAX', 'MIN', 'ROUND', 'LENGTH', 'UPPER', 'LOWER', 'TRIM',
1413
+ 'PRIMARY', 'KEY', 'FOREIGN', 'REFERENCES', 'UNIQUE', 'DEFAULT', 'NOT', 'CONSTRAINT',
1414
+ 'BEGIN', 'COMMIT', 'ROLLBACK', 'TRANSACTION', 'AUTO_INCREMENT', 'SERIAL', 'RETURNING',
1415
+ 'select', 'from', 'where', 'insert', 'into', 'values', 'update', 'delete', 'create',
1416
+ 'table', 'drop', 'alter', 'join', 'left', 'right', 'inner', 'outer', 'on', 'as',
1417
+ 'and', 'or', 'not', 'in', 'like', 'is', 'null', 'order', 'group', 'by', 'having', 'limit'
1418
+ ]);
1419
+ export const C_KEYWORDS = new Set([
1420
+ 'auto', 'break', 'case', 'char', 'const', 'continue', 'default', 'do', 'double', 'else',
1421
+ 'enum', 'extern', 'float', 'for', 'goto', 'if', 'inline', 'int', 'long', 'register',
1422
+ 'restrict', 'return', 'short', 'signed', 'sizeof', 'static', 'struct', 'switch', 'typedef',
1423
+ 'union', 'unsigned', 'void', 'volatile', 'while', '_Alignas', '_Alignof', '_Atomic',
1424
+ '_Bool', '_Complex', '_Generic', '_Imaginary', '_Noreturn', '_Static_assert', '_Thread_local',
1425
+ 'true', 'false', 'NULL', 'include', 'define', 'ifdef', 'ifndef', 'endif', 'pragma'
1426
+ ]);
1427
+ export const BASH_KEYWORDS = new Set([
1428
+ 'if', 'then', 'else', 'elif', 'fi', 'case', 'esac', 'for', 'select', 'while', 'until',
1429
+ 'do', 'done', 'in', 'function', 'time', 'coproc', 'echo', 'exit', 'return', 'break',
1430
+ 'continue', 'source', 'export', 'local', 'declare', 'readonly', 'unset', 'shift', 'trap',
1431
+ 'exec', 'eval', 'alias', 'unalias', 'set', 'unset', 'read', 'printf', 'test', 'let',
1432
+ 'true', 'false', 'null', 'cd', 'pwd', 'ls', 'grep', 'sed', 'awk', 'cat', 'mkdir',
1433
+ 'rm', 'cp', 'mv', 'chmod', 'chown', 'curl', 'wget', 'tar', 'zip', 'sudo', 'apt', 'npm'
1434
+ ]);
1435
+ export const JSON_KEYWORDS = new Set([
1436
+ 'true', 'false', 'null'
1437
+ ]);
1438
+ export const YAML_KEYWORDS = new Set([
1439
+ 'true', 'false', 'null', 'yes', 'no', 'on', 'off', 'include', 'extends', 'anchor'
1440
+ ]);
1357
1441
  export const LANGUAGE_KEYWORDS = {
1358
1442
  css: CSS_KEYWORDS,
1359
1443
  html: HTML_KEYWORDS,
@@ -1369,5 +1453,23 @@ export const LANGUAGE_KEYWORDS = {
1369
1453
  'c++': CPP_KEYWORDS,
1370
1454
  rust: RUST_KEYWORDS,
1371
1455
  rs: RUST_KEYWORDS,
1456
+ java: JAVA_KEYWORDS,
1457
+ php: PHP_KEYWORDS,
1458
+ ruby: RUBY_KEYWORDS,
1459
+ rb: RUBY_KEYWORDS,
1460
+ kotlin: KOTLIN_KEYWORDS,
1461
+ kt: KOTLIN_KEYWORDS,
1462
+ swift: SWIFT_KEYWORDS,
1463
+ sql: SQL_KEYWORDS,
1464
+ mysql: SQL_KEYWORDS,
1465
+ postgresql: SQL_KEYWORDS,
1466
+ sqlite: SQL_KEYWORDS,
1467
+ c: C_KEYWORDS,
1468
+ bash: BASH_KEYWORDS,
1469
+ sh: BASH_KEYWORDS,
1470
+ shell: BASH_KEYWORDS,
1471
+ json: JSON_KEYWORDS,
1472
+ yaml: YAML_KEYWORDS,
1473
+ yml: YAML_KEYWORDS,
1372
1474
  };
1373
1475
  //# sourceMappingURL=constants.js.map