kage-baileys 3.0.6 → 3.2.3
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/Example/connection.cjs +4 -0
- package/README.MD +4726 -48
- package/WAProto/WAProto.proto +3395 -415
- package/WAProto/compat.cjs +12 -0
- package/WAProto/index.d.ts +47104 -19888
- package/WAProto/index.js +131979 -57361
- package/lib/Addon/http-error.js +43 -0
- package/lib/Addon/index.js +32 -0
- package/lib/Addon/keyed-db.js +250 -0
- package/lib/Addon/link-preview.js +82 -0
- package/lib/Defaults/baileys-version.json +3 -0
- package/lib/Defaults/connection.js +110 -0
- package/lib/Defaults/constants.js +156 -0
- package/lib/Defaults/history.js +46 -0
- package/lib/Defaults/index.js +230 -0
- package/lib/Defaults/media.js +104 -0
- package/lib/Defaults/phonenumber-mcc.json +223 -0
- package/lib/Defaults/prefix.js +44 -0
- package/lib/Extensions/business-advanced.js +382 -0
- package/lib/Extensions/business.js +159 -0
- package/lib/Extensions/chats-advanced.js +1106 -0
- package/lib/Extensions/chats.js +349 -0
- package/lib/Extensions/communities-advanced.js +430 -0
- package/lib/Extensions/community.js +94 -0
- package/lib/Extensions/groups-advanced.js +343 -0
- package/lib/Extensions/index.js +30 -0
- package/lib/Extensions/messages-recv-advanced.js +1690 -0
- package/lib/Extensions/messages-send-advanced.js +622 -0
- package/lib/Extensions/messages-send.js +1380 -0
- package/lib/Extensions/newsletter-advanced.js +228 -0
- package/lib/Extensions/newsletter.js +72 -0
- package/lib/Extensions/socket.js +389 -0
- package/lib/Modded/message_builder.js +2084 -4
- package/lib/Signal/Group/ciphertext-message.js +54 -0
- package/lib/Signal/Group/group-session-builder.js +124 -0
- package/lib/Signal/Group/group_cipher.js +216 -0
- package/lib/Signal/Group/index.js +124 -0
- package/lib/Signal/Group/keyhelper.js +108 -0
- package/lib/Signal/Group/sender-chain-key.js +83 -0
- package/lib/Signal/Group/sender-key-distribution-message.js +157 -0
- package/lib/Signal/Group/sender-key-message.js +166 -0
- package/lib/Signal/Group/sender-key-name.js +130 -0
- package/lib/Signal/Group/sender-key-record.js +113 -0
- package/lib/Signal/Group/sender-key-state.js +208 -0
- package/lib/Signal/Group/sender-message-key.js +82 -0
- package/lib/Signal/libsignal.js +935 -0
- package/lib/Signal/lid-mapping.js +545 -1
- package/lib/Socket/Client/index.js +60 -0
- package/lib/Socket/Client/types.js +51 -0
- package/lib/Socket/Client/websocket.js +152 -0
- package/lib/Socket/business.js +831 -0
- package/lib/Socket/chats.js +2535 -49
- package/lib/Socket/communities.js +450 -1
- package/lib/Socket/community.js +1022 -0
- package/lib/Socket/dugong.js +1 -1
- package/lib/Socket/groups.js +747 -0
- package/lib/Socket/index.js +60 -2
- package/lib/Socket/messages-recv.js +3450 -5
- package/lib/Socket/messages-send.js +3123 -0
- package/lib/Socket/mex.js +126 -1
- package/lib/Socket/newsletter.js +465 -0
- package/lib/Socket/socket.js +2217 -1
- package/lib/Store/index.js +79 -0
- package/lib/Store/make-cache-manager-store.js +186 -0
- package/lib/Store/make-in-memory-store.js +878 -0
- package/lib/Store/make-ordered-dictionary.js +187 -0
- package/lib/Store/object-repository.js +78 -0
- package/lib/Types/Auth.js +25 -0
- package/lib/Types/Bussines.js +25 -1
- package/lib/Types/Call.js +25 -0
- package/lib/Types/Chat.js +39 -0
- package/lib/Types/Contact.js +25 -0
- package/lib/Types/Events.js +25 -0
- package/lib/Types/GroupMetadata.js +25 -0
- package/lib/Types/Label.js +76 -0
- package/lib/Types/LabelAssociation.js +40 -0
- package/lib/Types/Message.js +57 -0
- package/lib/Types/Mex.js +56 -1
- package/lib/Types/MexUpdates.js +46 -0
- package/lib/Types/Newsletter.js +56 -0
- package/lib/Types/Product.js +25 -0
- package/lib/Types/RichType.js +23 -0
- package/lib/Types/Signal.js +25 -0
- package/lib/Types/Socket.js +25 -0
- package/lib/Types/State.js +92 -0
- package/lib/Types/USync.js +25 -0
- package/lib/Types/index.js +133 -0
- package/lib/Utils/auth-utils.js +703 -0
- package/lib/Utils/automation-tools.js +51 -16
- package/lib/Utils/baileys-event-stream.js +146 -0
- package/lib/Utils/browser-utils.js +95 -0
- package/lib/Utils/business.js +525 -0
- package/lib/Utils/chat-utils.js +1861 -0
- package/lib/Utils/companion-reg-client-utils.js +55 -1
- package/lib/Utils/crypto.js +355 -0
- package/lib/Utils/decode-wa-message.js +709 -0
- package/lib/Utils/event-buffer.js +1323 -8
- package/lib/Utils/generics.js +945 -1
- package/lib/Utils/history.js +266 -0
- package/lib/Utils/identity-change-handler.js +62 -1
- package/lib/Utils/index.js +114 -0
- package/lib/Utils/link-preview.js +179 -0
- package/lib/Utils/logger.js +42 -0
- package/lib/Utils/lt-hash.js +73 -0
- package/lib/Utils/make-mutex.js +104 -0
- package/lib/Utils/message-inspector.js +110 -0
- package/lib/Utils/message-retry-manager.js +507 -2
- package/lib/Utils/messages-media.js +1931 -1
- package/lib/Utils/messages.js +3748 -0
- package/lib/Utils/noise-handler.js +423 -0
- package/lib/Utils/offline-node-processor.js +53 -1
- package/lib/Utils/pre-key-manager.js +255 -1
- package/lib/Utils/process-message.js +1592 -0
- package/lib/Utils/reporting-utils.js +272 -1
- package/lib/Utils/rich-message-utils.js +377 -0
- package/lib/Utils/rich-messages.js +1 -1
- package/lib/Utils/signal.js +437 -0
- package/lib/Utils/stanza-ack.js +51 -1
- package/lib/Utils/status-refresh.js +37 -0
- package/lib/Utils/status-tools.js +4 -2
- package/lib/Utils/sync-action-utils.js +63 -1
- package/lib/Utils/tc-token-utils.js +185 -1
- package/lib/Utils/terminal-banner.js +31 -0
- package/lib/Utils/use-multi-file-auth-state.js +271 -0
- package/lib/Utils/use-single-file-auth-state.js +108 -0
- package/lib/Utils/use-sqlite-auth-state.js +142 -0
- package/lib/Utils/validate-connection.js +497 -1
- package/lib/Utils/view-once-compat.js +13 -5
- package/lib/VoIP/audio-feeder.js +150 -0
- package/lib/VoIP/index.js +289 -0
- package/lib/VoIP/relay-transport.js +608 -0
- package/lib/VoIP/signaling.js +583 -0
- package/lib/VoIP/types.js +30 -0
- package/lib/VoIP/wasm-engine.js +1261 -2
- package/lib/VoIP/worker-bootstrap.js +1090 -2
- package/lib/WABinary/constants.js +2805 -0
- package/lib/WABinary/decode.js +649 -2
- package/lib/WABinary/encode.js +564 -2
- package/lib/WABinary/generic-utils.js +401 -0
- package/lib/WABinary/index.js +82 -0
- package/lib/WABinary/jid-utils.js +290 -0
- package/lib/WABinary/types.js +25 -0
- package/lib/WAM/BinaryInfo.js +50 -0
- package/lib/WAM/constants.js +45747 -0
- package/lib/WAM/encode.js +393 -0
- package/lib/WAM/index.js +78 -0
- package/lib/WAUSync/Protocols/USyncBotProfileProtocol.js +135 -0
- package/lib/WAUSync/Protocols/USyncContactProtocol.js +110 -0
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +142 -0
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +85 -0
- package/lib/WAUSync/Protocols/USyncLIDProtocol.js +69 -0
- package/lib/WAUSync/Protocols/USyncStatusProtocol.js +107 -0
- package/lib/WAUSync/Protocols/USyncUsernameProtocol.js +38 -1
- package/lib/WAUSync/Protocols/index.js +83 -0
- package/lib/WAUSync/USyncQuery.js +241 -2
- package/lib/WAUSync/USyncUser.js +86 -0
- package/lib/WAUSync/index.js +78 -0
- package/lib/assets/wasm/loader.js +1875 -1
- package/lib/assets/wasm/worker-modules.js +11047 -1
- package/lib/index.d.ts +126 -0
- package/lib/index.js +53 -21
- package/lib/index.mjs +437 -0
- package/licenses/upstream-MIT.txt +22 -0
- package/maintenance/api-coverage.json +1642 -0
- package/maintenance/archivos-sha256.json +207 -0
- package/maintenance/consumer-validation.log +3 -0
- package/maintenance/dependency-audit.json +4 -4
- package/maintenance/extension-inventory.json +57 -0
- package/maintenance/protocol-update.json +1101 -0
- package/maintenance/protocol-wire-audit.json +4 -0
- package/maintenance/reference-socket-surface.json +209 -0
- package/maintenance/resumen-validacion.json +50 -0
- package/maintenance/validation.log +2725 -0
- package/maintenance/verification.json +566 -0
- package/package.json +107 -9
- package/scripts/check-imports.cjs +10 -0
- package/scripts/check-package.cjs +40 -0
- package/scripts/check-syntax.cjs +1 -1
- package/scripts/generate-protobuf.cjs +6 -1
- package/scripts/generate-public-types.cjs +9 -0
- package/scripts/kage-base64-fix.cjs +45 -0
- package/scripts/maintenance.cjs +10 -0
- package/test/advanced.test.cjs +24 -0
- package/test/base64-fix.test.cjs +49 -0
- package/test/maintenance.test.cjs +1 -1
- package/test/npm-publication.test.cjs +141 -0
- package/test/recepcion-reparada.test.cjs +98 -0
- package/test/resilience-binary.test.cjs +69 -0
- package/test/resilience-transport.test.cjs +34 -0
- package/test/types.test.ts +5 -0
- package/test/upgrade.test.cjs +104 -0
- package/tsconfig.json +20 -0
- package/WAProto/p.html +0 -1
- package/lib/KageExtra/Modded/message_builder.js +0 -2330
- package/lib/KageExtra/VoIP/audio-feeder.js +0 -136
- package/lib/KageExtra/VoIP/index.js +0 -282
- package/lib/KageExtra/VoIP/relay-transport.js +0 -559
- package/lib/KageExtra/VoIP/signaling.js +0 -603
- package/lib/KageExtra/VoIP/types.js +0 -17
- package/lib/KageExtra/VoIP/wasm-engine.js +0 -1214
- package/lib/KageExtra/VoIP/worker-bootstrap.js +0 -1042
- package/lib/KageExtra/assets/wasm/loader.js +0 -5
- package/lib/KageExtra/assets/wasm/whatsapp.wasm +0 -0
- package/lib/KageExtra/assets/wasm/worker-modules.js +0 -273
- package/lib/KageExtra/index.js +0 -3
- package/lib/KageExtra/package.json +0 -1
- package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +0 -53
- package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +0 -24
- package/maintenance/revision-full-check.txt +0 -1097
- package/maintenance/revision-library-tests.tap +0 -1088
- package/maintenance/revision-protobuf.txt +0 -5
- package/maintenance/revision-syntax.txt +0 -5
- package/maintenance/schema-coverage.json +0 -23
- package/maintenance/schema-merge.json +0 -30
- package/maintenance/validation-node20.txt +0 -191
- package/maintenance/validation-node22.txt +0 -237
|
@@ -37,15 +37,15 @@ var _stream = require('stream');
|
|
|
37
37
|
|
|
38
38
|
let sharp, ffmpeg;
|
|
39
39
|
try {
|
|
40
|
-
sharp =
|
|
40
|
+
sharp = require('sharp');
|
|
41
41
|
} catch (e2) {
|
|
42
|
-
console.warn('⚠️ [kage-baileys]
|
|
42
|
+
console.warn('⚠️ [kage-baileys] El módulo opcional sharp no está instalado. Para procesar imágenes instale sharp.');
|
|
43
43
|
}
|
|
44
44
|
|
|
45
45
|
try {
|
|
46
|
-
ffmpeg =
|
|
46
|
+
ffmpeg = require('fluent-ffmpeg');
|
|
47
47
|
} catch (e3) {
|
|
48
|
-
console.warn('⚠️ [kage-baileys]
|
|
48
|
+
console.warn('⚠️ [kage-baileys] El módulo opcional fluent-ffmpeg no está instalado. Algunas vistas previas de vídeo no estarán disponibles.');
|
|
49
49
|
}
|
|
50
50
|
|
|
51
51
|
function extractIE(text, { extract = true, hyperlink = true, citation = true, latex = true } = {}) {
|
|
@@ -2328,3 +2328,2083 @@ class ORich extends AIRich { }
|
|
|
2328
2328
|
|
|
2329
2329
|
|
|
2330
2330
|
exports.VERSION = VERSION; exports.Button = Button; exports.ButtonV2 = ButtonV2; exports.Carousel = Carousel; exports.AIRich = AIRich; exports.ORich = ORich; exports.Toolkit = Toolkit;
|
|
2331
|
+
|
|
2332
|
+
// Kage: herramientas de Kage.
|
|
2333
|
+
const __kageModule102 = (() => {
|
|
2334
|
+
const module = { exports: {} }; const exports = module.exports;
|
|
2335
|
+
/**
|
|
2336
|
+
* Do not remove this watermark.
|
|
2337
|
+
*
|
|
2338
|
+
* NIXCODE - Advanced WhatsApp Interactive Message Builder
|
|
2339
|
+
* Built for creating buttons, carousels, native flows,
|
|
2340
|
+
* and AI rich response payloads using Baileys with
|
|
2341
|
+
* fluent chaining, flexible payload customization,
|
|
2342
|
+
* and scalable architecture for modern bot development.
|
|
2343
|
+
*
|
|
2344
|
+
* Runtime:
|
|
2345
|
+
* - Baileys: @whiskeysockets/baileys (latest)
|
|
2346
|
+
*
|
|
2347
|
+
* Created by Nixel
|
|
2348
|
+
* Contributors: ~ Ahmad tumbuh kembang
|
|
2349
|
+
*
|
|
2350
|
+
* WhatsApp: wa.me/6285188349341
|
|
2351
|
+
* Channel: https://whatsapp.com/channel/0029VbCV1ck8fewpdNb2TY2k
|
|
2352
|
+
*
|
|
2353
|
+
* Copyright (c) 2026 Nixel
|
|
2354
|
+
*
|
|
2355
|
+
* Permission is granted to use and modify this library
|
|
2356
|
+
* for personal or commercial projects.
|
|
2357
|
+
*
|
|
2358
|
+
* Reuploading, reselling, relicensing, or redistributing
|
|
2359
|
+
* this library as a standalone product is prohibited.
|
|
2360
|
+
*
|
|
2361
|
+
* Do not claim this project as your own original work.
|
|
2362
|
+
*/
|
|
2363
|
+
'use strict';
|
|
2364
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
2365
|
+
if (k2 === undefined) k2 = k;
|
|
2366
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
2367
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
2368
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
2369
|
+
}
|
|
2370
|
+
Object.defineProperty(o, k2, desc);
|
|
2371
|
+
}) : (function(o, m, k, k2) {
|
|
2372
|
+
if (k2 === undefined) k2 = k;
|
|
2373
|
+
o[k2] = m[k];
|
|
2374
|
+
}));
|
|
2375
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
2376
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
2377
|
+
}) : function(o, v) {
|
|
2378
|
+
o["default"] = v;
|
|
2379
|
+
});
|
|
2380
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
2381
|
+
var ownKeys = function(o) {
|
|
2382
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
2383
|
+
var ar = [];
|
|
2384
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
2385
|
+
return ar;
|
|
2386
|
+
};
|
|
2387
|
+
return ownKeys(o);
|
|
2388
|
+
};
|
|
2389
|
+
return function (mod) {
|
|
2390
|
+
if (mod && mod.__esModule) return mod;
|
|
2391
|
+
var result = {};
|
|
2392
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
2393
|
+
__setModuleDefault(result, mod);
|
|
2394
|
+
return result;
|
|
2395
|
+
};
|
|
2396
|
+
})();
|
|
2397
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
2398
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
2399
|
+
};
|
|
2400
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
2401
|
+
exports.Toolkit = exports.ORich = exports.AIRich = exports.Carousel = exports.ButtonV2 = exports.Button = exports.VERSION = void 0;
|
|
2402
|
+
const VERSION = '4.6';
|
|
2403
|
+
exports.VERSION = VERSION;
|
|
2404
|
+
const index_js_1 = require("../index.js");
|
|
2405
|
+
const crypto_1 = __importDefault(require("crypto"));
|
|
2406
|
+
const stream_1 = require("stream");
|
|
2407
|
+
let sharp, ffmpeg;
|
|
2408
|
+
try {
|
|
2409
|
+
sharp = require('sharp');
|
|
2410
|
+
}
|
|
2411
|
+
catch {
|
|
2412
|
+
console.warn('⚠️ [kage-baileys] El módulo opcional sharp no está instalado. Para procesar imágenes instale sharp.');
|
|
2413
|
+
}
|
|
2414
|
+
try {
|
|
2415
|
+
ffmpeg = require('fluent-ffmpeg');
|
|
2416
|
+
}
|
|
2417
|
+
catch {
|
|
2418
|
+
console.warn('⚠️ [kage-baileys] El módulo opcional fluent-ffmpeg no está instalado. Algunas vistas previas de vídeo no estarán disponibles.');
|
|
2419
|
+
}
|
|
2420
|
+
function extractIE(text, { extract = true, hyperlink = true, citation = true, latex = true } = {}) {
|
|
2421
|
+
if (!extract) {
|
|
2422
|
+
return {
|
|
2423
|
+
text,
|
|
2424
|
+
ie: [],
|
|
2425
|
+
inline_entities: [],
|
|
2426
|
+
};
|
|
2427
|
+
}
|
|
2428
|
+
const createIE = (type, ie) => {
|
|
2429
|
+
if (type == 'hyperlink') {
|
|
2430
|
+
return {
|
|
2431
|
+
key: ie.key,
|
|
2432
|
+
metadata: {
|
|
2433
|
+
display_name: ie.text,
|
|
2434
|
+
is_trusted: ie.is_trusted,
|
|
2435
|
+
url: ie.url,
|
|
2436
|
+
__typename: 'GenAIInlineLinkItem',
|
|
2437
|
+
},
|
|
2438
|
+
};
|
|
2439
|
+
}
|
|
2440
|
+
if (type == 'citation') {
|
|
2441
|
+
return {
|
|
2442
|
+
key: ie.key,
|
|
2443
|
+
metadata: {
|
|
2444
|
+
reference_id: ie.reference_id,
|
|
2445
|
+
reference_url: ie.url,
|
|
2446
|
+
reference_title: ie.url,
|
|
2447
|
+
reference_display_name: ie.url,
|
|
2448
|
+
sources: [],
|
|
2449
|
+
__typename: 'GenAISearchCitationItem',
|
|
2450
|
+
},
|
|
2451
|
+
};
|
|
2452
|
+
}
|
|
2453
|
+
if (type == 'latex') {
|
|
2454
|
+
return {
|
|
2455
|
+
key: ie.key,
|
|
2456
|
+
metadata: {
|
|
2457
|
+
latex_expression: ie.text,
|
|
2458
|
+
latex_image: {
|
|
2459
|
+
url: ie.url,
|
|
2460
|
+
width: Number(ie.width) || 100,
|
|
2461
|
+
height: Number(ie.height) || 100,
|
|
2462
|
+
},
|
|
2463
|
+
font_height: Number(ie.font_height) || 83.333333333333,
|
|
2464
|
+
padding: Number(ie.padding) || 15,
|
|
2465
|
+
__typename: 'GenAILatexItem',
|
|
2466
|
+
},
|
|
2467
|
+
};
|
|
2468
|
+
}
|
|
2469
|
+
};
|
|
2470
|
+
let ie = [];
|
|
2471
|
+
let inline_entities = [];
|
|
2472
|
+
let result = '';
|
|
2473
|
+
let last = 0;
|
|
2474
|
+
let citation_index = 1;
|
|
2475
|
+
let hyperlink_index = 0;
|
|
2476
|
+
let latex_index = 0;
|
|
2477
|
+
let stack = [];
|
|
2478
|
+
for (let i = 0; i < text.length; i++) {
|
|
2479
|
+
if (text[i] == '[' && text[i - 1] != '\\') {
|
|
2480
|
+
stack.push(i);
|
|
2481
|
+
}
|
|
2482
|
+
else if (text[i] == ']' && (text[i + 1] == '(' || text[i + 1] == '<')) {
|
|
2483
|
+
let start = stack.pop();
|
|
2484
|
+
if (start == null)
|
|
2485
|
+
continue;
|
|
2486
|
+
let open = text[i + 1];
|
|
2487
|
+
let close = open == '(' ? ')' : '>';
|
|
2488
|
+
let type = open == '(' ? 'link' : 'latex';
|
|
2489
|
+
let end = i + 2;
|
|
2490
|
+
let depth = 1;
|
|
2491
|
+
while (end < text.length && depth) {
|
|
2492
|
+
if (text[end] == open && text[end - 1] != '\\')
|
|
2493
|
+
depth++;
|
|
2494
|
+
else if (text[end] == close && text[end - 1] != '\\')
|
|
2495
|
+
depth--;
|
|
2496
|
+
end++;
|
|
2497
|
+
}
|
|
2498
|
+
if (depth)
|
|
2499
|
+
continue;
|
|
2500
|
+
let raw = text.slice(start + 1, i).trim();
|
|
2501
|
+
let url = text.slice(i + 2, end - 1).trim();
|
|
2502
|
+
let key;
|
|
2503
|
+
let tag;
|
|
2504
|
+
let data;
|
|
2505
|
+
if (type == 'latex') {
|
|
2506
|
+
if (!latex)
|
|
2507
|
+
continue;
|
|
2508
|
+
let [txt = '', width = null, height = null, font_height = null, padding = null] = raw.split('|');
|
|
2509
|
+
key = `\u004E\u0049\u0058\u0045\u004C_LATEX_${latex_index++}`;
|
|
2510
|
+
tag = `{{${key}}}${txt || 'image'}{{/${key}}}`;
|
|
2511
|
+
data = {
|
|
2512
|
+
type: 'latex',
|
|
2513
|
+
ie: {
|
|
2514
|
+
key,
|
|
2515
|
+
text: txt,
|
|
2516
|
+
url,
|
|
2517
|
+
width,
|
|
2518
|
+
height,
|
|
2519
|
+
font_height,
|
|
2520
|
+
padding,
|
|
2521
|
+
},
|
|
2522
|
+
};
|
|
2523
|
+
}
|
|
2524
|
+
else if (raw) {
|
|
2525
|
+
if (!hyperlink)
|
|
2526
|
+
continue;
|
|
2527
|
+
const trusted = !url.startsWith('!');
|
|
2528
|
+
if (!trusted) {
|
|
2529
|
+
url = url.slice(1);
|
|
2530
|
+
}
|
|
2531
|
+
key = `\u004E\u0049\u0058\u0045\u004C_HYPERLINK_${hyperlink_index++}`;
|
|
2532
|
+
tag = `{{${key}}}${url}{{/${key}}}`;
|
|
2533
|
+
data = {
|
|
2534
|
+
type: 'hyperlink',
|
|
2535
|
+
ie: {
|
|
2536
|
+
key,
|
|
2537
|
+
text: raw,
|
|
2538
|
+
url,
|
|
2539
|
+
is_trusted: trusted,
|
|
2540
|
+
},
|
|
2541
|
+
};
|
|
2542
|
+
}
|
|
2543
|
+
else {
|
|
2544
|
+
if (!citation)
|
|
2545
|
+
continue;
|
|
2546
|
+
key = `\u004E\u0049\u0058\u0045\u004C_CITATION_${citation_index - 1}`;
|
|
2547
|
+
tag = `{{${key}}}${url}{{/${key}}}`;
|
|
2548
|
+
data = {
|
|
2549
|
+
type: 'citation',
|
|
2550
|
+
ie: {
|
|
2551
|
+
reference_id: citation_index++,
|
|
2552
|
+
key,
|
|
2553
|
+
text: '',
|
|
2554
|
+
url,
|
|
2555
|
+
},
|
|
2556
|
+
};
|
|
2557
|
+
}
|
|
2558
|
+
result += text.slice(last, start) + tag;
|
|
2559
|
+
last = end;
|
|
2560
|
+
ie.push(data);
|
|
2561
|
+
const entity = createIE(data.type, data.ie);
|
|
2562
|
+
if (entity) {
|
|
2563
|
+
inline_entities.push(entity);
|
|
2564
|
+
}
|
|
2565
|
+
i = end - 1;
|
|
2566
|
+
}
|
|
2567
|
+
}
|
|
2568
|
+
result += text.slice(last);
|
|
2569
|
+
return {
|
|
2570
|
+
text: result,
|
|
2571
|
+
ie,
|
|
2572
|
+
inline_entities,
|
|
2573
|
+
};
|
|
2574
|
+
}
|
|
2575
|
+
async function waitAllPromises(input) {
|
|
2576
|
+
const isPromise = (v) => v && typeof v.then === 'function';
|
|
2577
|
+
const isObject = (v) => v && typeof v === 'object';
|
|
2578
|
+
const deep = async (v) => {
|
|
2579
|
+
if (isPromise(v))
|
|
2580
|
+
return deep(await v);
|
|
2581
|
+
if (Array.isArray(v))
|
|
2582
|
+
return Promise.all(v.map(deep));
|
|
2583
|
+
if (isObject(v)) {
|
|
2584
|
+
const entries = await Promise.all(Object.entries(v).map(async ([k, val]) => [k, await deep(val)]));
|
|
2585
|
+
return Object.fromEntries(entries);
|
|
2586
|
+
}
|
|
2587
|
+
return v;
|
|
2588
|
+
};
|
|
2589
|
+
return deep(await input);
|
|
2590
|
+
}
|
|
2591
|
+
class Toolkit {
|
|
2592
|
+
constructor() { }
|
|
2593
|
+
static extractIE(text, { extract = true, hyperlink = true, citation = true, latex = true } = {}) {
|
|
2594
|
+
return extractIE(text, { extract, hyperlink, citation, latex });
|
|
2595
|
+
}
|
|
2596
|
+
static async resize(buffer, x, y, fit = 'cover') {
|
|
2597
|
+
return await sharp(buffer)
|
|
2598
|
+
.resize(x, y, {
|
|
2599
|
+
fit,
|
|
2600
|
+
position: 'center',
|
|
2601
|
+
background: { r: 0, g: 0, b: 0, alpha: 0 },
|
|
2602
|
+
})
|
|
2603
|
+
.png()
|
|
2604
|
+
.toBuffer();
|
|
2605
|
+
}
|
|
2606
|
+
static async waitAllPromises(input) {
|
|
2607
|
+
return await waitAllPromises(input);
|
|
2608
|
+
}
|
|
2609
|
+
static async fetchBuffer(url, options = {}, { silent = true } = {}) {
|
|
2610
|
+
try {
|
|
2611
|
+
let response = await fetch(url, options);
|
|
2612
|
+
if (!response.ok)
|
|
2613
|
+
throw Error(`HTTP ${response.status}`);
|
|
2614
|
+
return Buffer.from(await response.arrayBuffer());
|
|
2615
|
+
}
|
|
2616
|
+
catch (error) {
|
|
2617
|
+
if (silent)
|
|
2618
|
+
return Buffer.alloc(0);
|
|
2619
|
+
throw error;
|
|
2620
|
+
}
|
|
2621
|
+
}
|
|
2622
|
+
static async toUrl(_client, path, mediaType = 'document') {
|
|
2623
|
+
if (!path)
|
|
2624
|
+
throw new Error('Url or buffer needed');
|
|
2625
|
+
const media = await (0, index_js_1.prepareWAMessageMedia)({
|
|
2626
|
+
[mediaType]: Buffer.isBuffer(path) ? path : { url: path },
|
|
2627
|
+
}, {
|
|
2628
|
+
upload: _client.waUploadToServer,
|
|
2629
|
+
jid: '\u0040\u006e\u0065\u0077\u0073\u006c\u0065\u0074\u0074\u0065\u0072',
|
|
2630
|
+
});
|
|
2631
|
+
return Object.values(media)[0]?.url;
|
|
2632
|
+
}
|
|
2633
|
+
static async resolveMedia(_client, media, mediaType = 'image', { resolveUrl = false, resolveWAUrl = false, result = 'url', resize = false, width = 300, height = 300 } = {}) {
|
|
2634
|
+
const isUrl = (str) => /^https?:\/\/.+/i.test(str);
|
|
2635
|
+
const isWAUrl = (str) => /^https?:\/\/[^/]*\.whatsapp\.net\//i.test(str);
|
|
2636
|
+
if (Array.isArray(media)) {
|
|
2637
|
+
return Promise.all(media.map((item) => Toolkit.resolveMedia(_client, item, mediaType, {
|
|
2638
|
+
resolveUrl,
|
|
2639
|
+
resolveWAUrl,
|
|
2640
|
+
result,
|
|
2641
|
+
resize,
|
|
2642
|
+
width,
|
|
2643
|
+
height,
|
|
2644
|
+
})));
|
|
2645
|
+
}
|
|
2646
|
+
const originalIsBuffer = Buffer.isBuffer(media);
|
|
2647
|
+
if (typeof media === 'string' && isUrl(media)) {
|
|
2648
|
+
if (isWAUrl(media)) {
|
|
2649
|
+
if (resolveWAUrl) {
|
|
2650
|
+
media = await Toolkit.fetchBuffer(media, {}, { silent: true });
|
|
2651
|
+
}
|
|
2652
|
+
else if (!resolveUrl) {
|
|
2653
|
+
if (result === 'url')
|
|
2654
|
+
return media;
|
|
2655
|
+
media = await Toolkit.fetchBuffer(media, {}, { silent: true });
|
|
2656
|
+
}
|
|
2657
|
+
}
|
|
2658
|
+
else {
|
|
2659
|
+
if (!resolveUrl) {
|
|
2660
|
+
if (result === 'url')
|
|
2661
|
+
return media;
|
|
2662
|
+
media = await Toolkit.fetchBuffer(media, {}, { silent: true });
|
|
2663
|
+
}
|
|
2664
|
+
else {
|
|
2665
|
+
media = await Toolkit.fetchBuffer(media, {}, { silent: true });
|
|
2666
|
+
}
|
|
2667
|
+
}
|
|
2668
|
+
}
|
|
2669
|
+
if (typeof media === 'string' && !isUrl(media)) {
|
|
2670
|
+
media = Buffer.from(media, 'base64');
|
|
2671
|
+
}
|
|
2672
|
+
if (!Buffer.isBuffer(media) || !media.length) {
|
|
2673
|
+
return;
|
|
2674
|
+
}
|
|
2675
|
+
if (resize && Buffer.isBuffer(media)) {
|
|
2676
|
+
media = await Toolkit.resize(media, width, height);
|
|
2677
|
+
}
|
|
2678
|
+
if (result === 'buffer') {
|
|
2679
|
+
return media;
|
|
2680
|
+
}
|
|
2681
|
+
if (result === 'base64') {
|
|
2682
|
+
return media.toString('base64');
|
|
2683
|
+
}
|
|
2684
|
+
if (originalIsBuffer) {
|
|
2685
|
+
return Toolkit.toUrl(_client, media, mediaType);
|
|
2686
|
+
}
|
|
2687
|
+
return Toolkit.toUrl(_client, media, mediaType);
|
|
2688
|
+
}
|
|
2689
|
+
static getMp4Duration(buffer, { silent = true } = {}) {
|
|
2690
|
+
try {
|
|
2691
|
+
if (!Buffer.isBuffer(buffer) || buffer.length < 8) {
|
|
2692
|
+
if (silent)
|
|
2693
|
+
return 0;
|
|
2694
|
+
throw new Error('Invalid buffer');
|
|
2695
|
+
}
|
|
2696
|
+
let offset = 0;
|
|
2697
|
+
while (offset < buffer.length - 8) {
|
|
2698
|
+
const size = buffer.readUInt32BE(offset);
|
|
2699
|
+
if (size < 8 || offset + size > buffer.length) {
|
|
2700
|
+
if (silent)
|
|
2701
|
+
return 0;
|
|
2702
|
+
throw new Error('Invalid atom size');
|
|
2703
|
+
}
|
|
2704
|
+
const type = buffer.toString('ascii', offset + 4, offset + 8);
|
|
2705
|
+
if (type === 'moov') {
|
|
2706
|
+
let moovOffset = offset + 8;
|
|
2707
|
+
const moovEnd = offset + size;
|
|
2708
|
+
while (moovOffset < moovEnd - 8) {
|
|
2709
|
+
const childSize = buffer.readUInt32BE(moovOffset);
|
|
2710
|
+
if (childSize < 8 || moovOffset + childSize > moovEnd) {
|
|
2711
|
+
if (silent)
|
|
2712
|
+
return 0;
|
|
2713
|
+
throw new Error('Invalid child atom size');
|
|
2714
|
+
}
|
|
2715
|
+
const childType = buffer.toString('ascii', moovOffset + 4, moovOffset + 8);
|
|
2716
|
+
if (childType === 'mvhd') {
|
|
2717
|
+
const version = buffer.readUInt8(moovOffset + 8);
|
|
2718
|
+
if (version === 0) {
|
|
2719
|
+
const timescale = buffer.readUInt32BE(moovOffset + 20);
|
|
2720
|
+
const duration = buffer.readUInt32BE(moovOffset + 24);
|
|
2721
|
+
if (!timescale) {
|
|
2722
|
+
if (silent)
|
|
2723
|
+
return 0;
|
|
2724
|
+
throw new Error('Invalid timescale');
|
|
2725
|
+
}
|
|
2726
|
+
return duration / timescale;
|
|
2727
|
+
}
|
|
2728
|
+
if (version === 1) {
|
|
2729
|
+
const timescale = buffer.readUInt32BE(moovOffset + 32);
|
|
2730
|
+
const duration = Number(buffer.readBigUInt64BE(moovOffset + 36));
|
|
2731
|
+
if (!timescale) {
|
|
2732
|
+
if (silent)
|
|
2733
|
+
return 0;
|
|
2734
|
+
throw new Error('Invalid timescale');
|
|
2735
|
+
}
|
|
2736
|
+
return duration / timescale;
|
|
2737
|
+
}
|
|
2738
|
+
}
|
|
2739
|
+
moovOffset += childSize;
|
|
2740
|
+
}
|
|
2741
|
+
}
|
|
2742
|
+
offset += size;
|
|
2743
|
+
}
|
|
2744
|
+
if (silent)
|
|
2745
|
+
return 0;
|
|
2746
|
+
throw new Error('No mvhd found!');
|
|
2747
|
+
}
|
|
2748
|
+
catch (err) {
|
|
2749
|
+
if (silent)
|
|
2750
|
+
return 0;
|
|
2751
|
+
throw err;
|
|
2752
|
+
}
|
|
2753
|
+
}
|
|
2754
|
+
static getMp4Preview(videoBuffer, { time, result = 'buffer', resize = true, width = 300, height = 300, silent = true } = {}) {
|
|
2755
|
+
return new Promise((resolve, reject) => {
|
|
2756
|
+
const fail = (err) => {
|
|
2757
|
+
if (silent) {
|
|
2758
|
+
return resolve(result === 'base64' ? '' : Buffer.alloc(0));
|
|
2759
|
+
}
|
|
2760
|
+
return reject(err);
|
|
2761
|
+
};
|
|
2762
|
+
try {
|
|
2763
|
+
if (!Buffer.isBuffer(videoBuffer) || !videoBuffer.length) {
|
|
2764
|
+
return fail(new Error('videoBuffer tidak valid atau kosong'));
|
|
2765
|
+
}
|
|
2766
|
+
const inputStream = new stream_1.Readable({ read() { } });
|
|
2767
|
+
inputStream.push(videoBuffer);
|
|
2768
|
+
inputStream.push(null);
|
|
2769
|
+
const outputStream = new stream_1.PassThrough();
|
|
2770
|
+
const chunks = [];
|
|
2771
|
+
outputStream.on('data', (chunk) => chunks.push(chunk));
|
|
2772
|
+
outputStream.on('end', async () => {
|
|
2773
|
+
try {
|
|
2774
|
+
let output = Buffer.concat(chunks);
|
|
2775
|
+
if (!output.length) {
|
|
2776
|
+
return fail(new Error('Output kosong — cek format atau timestamp video'));
|
|
2777
|
+
}
|
|
2778
|
+
if (resize) {
|
|
2779
|
+
output = await Toolkit.resize(output, width, height);
|
|
2780
|
+
}
|
|
2781
|
+
return resolve(result === 'base64' ? output.toString('base64') : output);
|
|
2782
|
+
}
|
|
2783
|
+
catch (err) {
|
|
2784
|
+
return fail(err);
|
|
2785
|
+
}
|
|
2786
|
+
});
|
|
2787
|
+
outputStream.on('error', fail);
|
|
2788
|
+
time ??= Math.min(Toolkit.getMp4Duration(videoBuffer) * 0.2, 10);
|
|
2789
|
+
ffmpeg(inputStream)
|
|
2790
|
+
.outputOptions([`-ss ${time}`, '-vframes 1', '-vcodec png', '-f image2pipe'])
|
|
2791
|
+
.on('error', (err) => fail(new Error(`ffmpeg error: ${err.message}`)))
|
|
2792
|
+
.pipe(outputStream, { end: true });
|
|
2793
|
+
}
|
|
2794
|
+
catch (err) {
|
|
2795
|
+
return fail(err);
|
|
2796
|
+
}
|
|
2797
|
+
});
|
|
2798
|
+
}
|
|
2799
|
+
}
|
|
2800
|
+
exports.Toolkit = Toolkit;
|
|
2801
|
+
class RowBuilder {
|
|
2802
|
+
constructor() { this.buttons = []; }
|
|
2803
|
+
button(displayText, buttonId) {
|
|
2804
|
+
this.buttons.push({ buttonId, buttonText: { displayText }, type: 1 });
|
|
2805
|
+
return this;
|
|
2806
|
+
}
|
|
2807
|
+
}
|
|
2808
|
+
class CardBuilder {
|
|
2809
|
+
constructor(client) { this._card = new Button(client); }
|
|
2810
|
+
image(url) { this._card.setImage(url); return this; }
|
|
2811
|
+
title(t) { this._card.setTitle(t); return this; }
|
|
2812
|
+
text(t) { this._card.setBody(t); return this; }
|
|
2813
|
+
button(displayText, id) { this._card.addReply(displayText, id); return this; }
|
|
2814
|
+
}
|
|
2815
|
+
class BaseBuilder {
|
|
2816
|
+
constructor() {
|
|
2817
|
+
this._title = '';
|
|
2818
|
+
this._subtitle = '';
|
|
2819
|
+
this._body = '';
|
|
2820
|
+
this._footer = '';
|
|
2821
|
+
this._contextInfo = {};
|
|
2822
|
+
this._extraPayload = {};
|
|
2823
|
+
}
|
|
2824
|
+
setTitle(title) {
|
|
2825
|
+
if (typeof title !== 'string') {
|
|
2826
|
+
throw new TypeError('Title must be a string');
|
|
2827
|
+
}
|
|
2828
|
+
this._title = title;
|
|
2829
|
+
return this;
|
|
2830
|
+
}
|
|
2831
|
+
setSubtitle(subtitle) {
|
|
2832
|
+
if (typeof subtitle !== 'string') {
|
|
2833
|
+
throw new TypeError('Subtitle must be a string');
|
|
2834
|
+
}
|
|
2835
|
+
this._subtitle = subtitle;
|
|
2836
|
+
return this;
|
|
2837
|
+
}
|
|
2838
|
+
setBody(body) {
|
|
2839
|
+
if (typeof body !== 'string') {
|
|
2840
|
+
throw new TypeError('Body must be a string');
|
|
2841
|
+
}
|
|
2842
|
+
this._body = body;
|
|
2843
|
+
return this;
|
|
2844
|
+
}
|
|
2845
|
+
setFooter(footer) {
|
|
2846
|
+
if (typeof footer !== 'string') {
|
|
2847
|
+
throw new TypeError('Footer must be a string');
|
|
2848
|
+
}
|
|
2849
|
+
this._footer = footer;
|
|
2850
|
+
return this;
|
|
2851
|
+
}
|
|
2852
|
+
setContextInfo(obj) {
|
|
2853
|
+
if (typeof obj !== 'object' || obj === null || Array.isArray(obj)) {
|
|
2854
|
+
throw new TypeError('ContextInfo must be a plain object');
|
|
2855
|
+
}
|
|
2856
|
+
this._contextInfo = obj;
|
|
2857
|
+
return this;
|
|
2858
|
+
}
|
|
2859
|
+
addPayload(obj) {
|
|
2860
|
+
if (typeof obj !== 'object' || obj === null || Array.isArray(obj)) {
|
|
2861
|
+
throw new TypeError('Payload must be a plain object');
|
|
2862
|
+
}
|
|
2863
|
+
Object.assign(this._extraPayload, obj);
|
|
2864
|
+
return this;
|
|
2865
|
+
}
|
|
2866
|
+
text(t) { return this.setBody(t); }
|
|
2867
|
+
footer(f) { return this.setFooter(f); }
|
|
2868
|
+
title(t) { return this.setTitle(t); }
|
|
2869
|
+
subtitle(s) { return this.setSubtitle(s); }
|
|
2870
|
+
image(url, opts = {}) {
|
|
2871
|
+
if (this.setImage)
|
|
2872
|
+
return this.setImage(url, opts);
|
|
2873
|
+
if (this.setThumbnail)
|
|
2874
|
+
return this.setThumbnail(url);
|
|
2875
|
+
if (this.addImage)
|
|
2876
|
+
return this.addImage(url, opts);
|
|
2877
|
+
return this;
|
|
2878
|
+
}
|
|
2879
|
+
}
|
|
2880
|
+
class Button extends BaseBuilder {
|
|
2881
|
+
#client;
|
|
2882
|
+
constructor(client) {
|
|
2883
|
+
super();
|
|
2884
|
+
if (!client) {
|
|
2885
|
+
throw new Error('Socket is required');
|
|
2886
|
+
}
|
|
2887
|
+
this.#client = client;
|
|
2888
|
+
this._buttons = [];
|
|
2889
|
+
this._data;
|
|
2890
|
+
this._currentSelectionIndex = -1;
|
|
2891
|
+
this._currentSectionIndex = -1;
|
|
2892
|
+
this._params = {};
|
|
2893
|
+
}
|
|
2894
|
+
setVideo(path, options = {}) {
|
|
2895
|
+
if (!path)
|
|
2896
|
+
throw new Error('Url or buffer needed');
|
|
2897
|
+
Buffer.isBuffer(path) ? (this._data = { video: path, ...options }) : (this._data = { video: { url: path }, ...options });
|
|
2898
|
+
return this;
|
|
2899
|
+
}
|
|
2900
|
+
setImage(path, options = {}) {
|
|
2901
|
+
if (!path)
|
|
2902
|
+
throw new Error('Url or buffer needed');
|
|
2903
|
+
Buffer.isBuffer(path) ? (this._data = { image: path, ...options }) : (this._data = { image: { url: path }, ...options });
|
|
2904
|
+
return this;
|
|
2905
|
+
}
|
|
2906
|
+
setDocument(path, options = {}) {
|
|
2907
|
+
if (!path)
|
|
2908
|
+
throw new Error('Url or buffer needed');
|
|
2909
|
+
Buffer.isBuffer(path) ? (this._data = { document: path, ...options }) : (this._data = { document: { url: path }, ...options });
|
|
2910
|
+
return this;
|
|
2911
|
+
}
|
|
2912
|
+
setMedia(obj) {
|
|
2913
|
+
if (typeof obj !== 'object' || obj === null || Array.isArray(obj)) {
|
|
2914
|
+
throw new TypeError('Media must be a plain object');
|
|
2915
|
+
}
|
|
2916
|
+
this._data = obj;
|
|
2917
|
+
return this;
|
|
2918
|
+
}
|
|
2919
|
+
clearButtons() {
|
|
2920
|
+
this._buttons = [];
|
|
2921
|
+
return this;
|
|
2922
|
+
}
|
|
2923
|
+
setParams(obj) {
|
|
2924
|
+
this._params = obj;
|
|
2925
|
+
return this;
|
|
2926
|
+
}
|
|
2927
|
+
addButton(name, params) {
|
|
2928
|
+
this._buttons.push({
|
|
2929
|
+
name,
|
|
2930
|
+
buttonParamsJson: typeof params === 'string' ? params : JSON.stringify(params),
|
|
2931
|
+
});
|
|
2932
|
+
return this;
|
|
2933
|
+
}
|
|
2934
|
+
makeRow(header = '', title = '', description = '', id = '') {
|
|
2935
|
+
if (this._currentSelectionIndex === -1 || this._currentSectionIndex === -1) {
|
|
2936
|
+
throw new Error('You need to create a selection and a section first');
|
|
2937
|
+
}
|
|
2938
|
+
const buttonParams = JSON.parse(this._buttons[this._currentSelectionIndex].buttonParamsJson);
|
|
2939
|
+
buttonParams.sections[this._currentSectionIndex].rows.push({ header, title, description, id });
|
|
2940
|
+
this._buttons[this._currentSelectionIndex].buttonParamsJson = JSON.stringify(buttonParams);
|
|
2941
|
+
return this;
|
|
2942
|
+
}
|
|
2943
|
+
makeSection(title = '', highlight_label = '') {
|
|
2944
|
+
if (this._currentSelectionIndex === -1) {
|
|
2945
|
+
throw new Error('You need to create a selection first');
|
|
2946
|
+
}
|
|
2947
|
+
const buttonParams = JSON.parse(this._buttons[this._currentSelectionIndex].buttonParamsJson);
|
|
2948
|
+
buttonParams.sections.push({ title, highlight_label, rows: [] });
|
|
2949
|
+
this._currentSectionIndex = buttonParams.sections.length - 1;
|
|
2950
|
+
this._buttons[this._currentSelectionIndex].buttonParamsJson = JSON.stringify(buttonParams);
|
|
2951
|
+
return this;
|
|
2952
|
+
}
|
|
2953
|
+
addSelection(title, options = {}) {
|
|
2954
|
+
this._buttons.push({ ...options, name: 'single_select', buttonParamsJson: JSON.stringify({ title, sections: [] }) });
|
|
2955
|
+
this._currentSelectionIndex = this._buttons.length - 1;
|
|
2956
|
+
this._currentSectionIndex = -1;
|
|
2957
|
+
return this;
|
|
2958
|
+
}
|
|
2959
|
+
addReply(display_text = '', id = '', options = {}) {
|
|
2960
|
+
this._buttons.push({
|
|
2961
|
+
name: 'quick_reply',
|
|
2962
|
+
buttonParamsJson: JSON.stringify({
|
|
2963
|
+
display_text,
|
|
2964
|
+
id,
|
|
2965
|
+
...options,
|
|
2966
|
+
}),
|
|
2967
|
+
});
|
|
2968
|
+
return this;
|
|
2969
|
+
}
|
|
2970
|
+
addCall(display_text = '', id = '', options = {}) {
|
|
2971
|
+
this._buttons.push({
|
|
2972
|
+
name: 'cta_call',
|
|
2973
|
+
buttonParamsJson: JSON.stringify({
|
|
2974
|
+
display_text,
|
|
2975
|
+
id,
|
|
2976
|
+
...options,
|
|
2977
|
+
}),
|
|
2978
|
+
});
|
|
2979
|
+
return this;
|
|
2980
|
+
}
|
|
2981
|
+
addReminder(display_text = '', id = '', options = {}) {
|
|
2982
|
+
this._buttons.push({
|
|
2983
|
+
name: 'cta_reminder',
|
|
2984
|
+
buttonParamsJson: JSON.stringify({
|
|
2985
|
+
display_text,
|
|
2986
|
+
id,
|
|
2987
|
+
...options,
|
|
2988
|
+
}),
|
|
2989
|
+
});
|
|
2990
|
+
return this;
|
|
2991
|
+
}
|
|
2992
|
+
addCancelReminder(display_text = '', id = '', options = {}) {
|
|
2993
|
+
this._buttons.push({
|
|
2994
|
+
name: 'cta_cancel_reminder',
|
|
2995
|
+
buttonParamsJson: JSON.stringify({
|
|
2996
|
+
display_text,
|
|
2997
|
+
id,
|
|
2998
|
+
...options,
|
|
2999
|
+
}),
|
|
3000
|
+
});
|
|
3001
|
+
return this;
|
|
3002
|
+
}
|
|
3003
|
+
addAddress(display_text = '', id = '', options = {}) {
|
|
3004
|
+
this._buttons.push({
|
|
3005
|
+
name: 'address_message',
|
|
3006
|
+
buttonParamsJson: JSON.stringify({
|
|
3007
|
+
display_text,
|
|
3008
|
+
id,
|
|
3009
|
+
...options,
|
|
3010
|
+
}),
|
|
3011
|
+
});
|
|
3012
|
+
return this;
|
|
3013
|
+
}
|
|
3014
|
+
addLocation(options = {}) {
|
|
3015
|
+
this._buttons.push({
|
|
3016
|
+
name: 'send_location',
|
|
3017
|
+
buttonParamsJson: JSON.stringify(options),
|
|
3018
|
+
});
|
|
3019
|
+
return this;
|
|
3020
|
+
}
|
|
3021
|
+
addUrl(display_text = '', url = '', webview_interaction = false, options = {}) {
|
|
3022
|
+
this._buttons.push({
|
|
3023
|
+
...options,
|
|
3024
|
+
name: 'cta_url',
|
|
3025
|
+
buttonParamsJson: JSON.stringify({
|
|
3026
|
+
display_text,
|
|
3027
|
+
url,
|
|
3028
|
+
webview_interaction,
|
|
3029
|
+
...options,
|
|
3030
|
+
}),
|
|
3031
|
+
});
|
|
3032
|
+
return this;
|
|
3033
|
+
}
|
|
3034
|
+
addCopy(display_text = '', copy_code = '', options = {}) {
|
|
3035
|
+
this._buttons.push({
|
|
3036
|
+
name: 'cta_copy',
|
|
3037
|
+
buttonParamsJson: JSON.stringify({
|
|
3038
|
+
display_text,
|
|
3039
|
+
copy_code,
|
|
3040
|
+
...options,
|
|
3041
|
+
}),
|
|
3042
|
+
});
|
|
3043
|
+
return this;
|
|
3044
|
+
}
|
|
3045
|
+
static paramsList = {
|
|
3046
|
+
limited_time_offer: {
|
|
3047
|
+
text: 'string',
|
|
3048
|
+
url: 'string',
|
|
3049
|
+
copy_code: 'string',
|
|
3050
|
+
expiration_time: 'number',
|
|
3051
|
+
},
|
|
3052
|
+
bottom_sheet: {
|
|
3053
|
+
in_thread_buttons_limit: 'number',
|
|
3054
|
+
divider_indices: ['number'],
|
|
3055
|
+
list_title: 'string',
|
|
3056
|
+
button_title: 'string',
|
|
3057
|
+
},
|
|
3058
|
+
tap_target_configuration: {
|
|
3059
|
+
title: 'string',
|
|
3060
|
+
description: 'string',
|
|
3061
|
+
canonical_url: 'string',
|
|
3062
|
+
domain: 'string',
|
|
3063
|
+
buttonIndex: 'number',
|
|
3064
|
+
},
|
|
3065
|
+
};
|
|
3066
|
+
async toCard() {
|
|
3067
|
+
return {
|
|
3068
|
+
body: {
|
|
3069
|
+
text: this._body,
|
|
3070
|
+
},
|
|
3071
|
+
footer: {
|
|
3072
|
+
text: this._footer,
|
|
3073
|
+
},
|
|
3074
|
+
header: {
|
|
3075
|
+
title: this._title,
|
|
3076
|
+
subtitle: this._subtitle,
|
|
3077
|
+
hasMediaAttachment: !!this._data,
|
|
3078
|
+
...(this._data
|
|
3079
|
+
? await (0, index_js_1.prepareWAMessageMedia)(this._data, { upload: this.#client.waUploadToServer }).catch((e) => {
|
|
3080
|
+
if (String(e).includes('Invalid media type'))
|
|
3081
|
+
return this._data;
|
|
3082
|
+
throw e;
|
|
3083
|
+
})
|
|
3084
|
+
: {}),
|
|
3085
|
+
},
|
|
3086
|
+
nativeFlowMessage: {
|
|
3087
|
+
messageParamsJson: JSON.stringify(this._params),
|
|
3088
|
+
buttons: this._buttons,
|
|
3089
|
+
},
|
|
3090
|
+
};
|
|
3091
|
+
}
|
|
3092
|
+
async build(jid, { ...options } = {}) {
|
|
3093
|
+
if (this._cardBuilders && this._cardBuilders.length > 0) {
|
|
3094
|
+
for (const builder of this._cardBuilders) {
|
|
3095
|
+
this.addCard(await builder._card.toCard());
|
|
3096
|
+
}
|
|
3097
|
+
this._cardBuilders = [];
|
|
3098
|
+
}
|
|
3099
|
+
return (0, index_js_1.generateWAMessageFromContent)(jid, {
|
|
3100
|
+
...this._extraPayload,
|
|
3101
|
+
interactiveMessage: {
|
|
3102
|
+
...message,
|
|
3103
|
+
contextInfo: this._contextInfo,
|
|
3104
|
+
},
|
|
3105
|
+
}, { ...options });
|
|
3106
|
+
}
|
|
3107
|
+
async send(jid, { ...options } = {}) {
|
|
3108
|
+
const msg = await this.build(jid, options);
|
|
3109
|
+
await this.#client.relayMessage(msg.key.remoteJid, msg.message, {
|
|
3110
|
+
messageId: msg.key.id,
|
|
3111
|
+
additionalNodes: [
|
|
3112
|
+
{
|
|
3113
|
+
tag: 'biz',
|
|
3114
|
+
attrs: {},
|
|
3115
|
+
content: [
|
|
3116
|
+
{
|
|
3117
|
+
tag: 'interactive',
|
|
3118
|
+
attrs: { type: 'native_flow', v: '1' },
|
|
3119
|
+
content: [{ tag: 'native_flow', attrs: { v: '9', name: 'mixed' } }],
|
|
3120
|
+
},
|
|
3121
|
+
],
|
|
3122
|
+
},
|
|
3123
|
+
],
|
|
3124
|
+
...options,
|
|
3125
|
+
});
|
|
3126
|
+
return msg;
|
|
3127
|
+
}
|
|
3128
|
+
button(displayText, id) { return this.addReply(displayText, id); }
|
|
3129
|
+
}
|
|
3130
|
+
exports.Button = Button;
|
|
3131
|
+
class ButtonV2 extends BaseBuilder {
|
|
3132
|
+
#client;
|
|
3133
|
+
constructor(client) {
|
|
3134
|
+
super();
|
|
3135
|
+
if (!client) {
|
|
3136
|
+
throw new Error('Socket is required');
|
|
3137
|
+
}
|
|
3138
|
+
this.#client = client;
|
|
3139
|
+
this._image;
|
|
3140
|
+
this._data;
|
|
3141
|
+
this._buttons = [];
|
|
3142
|
+
}
|
|
3143
|
+
addButton(displayText = '', buttonId = crypto_1.default.randomUUID()) {
|
|
3144
|
+
this._buttons.push({
|
|
3145
|
+
buttonId,
|
|
3146
|
+
buttonText: { displayText },
|
|
3147
|
+
type: 1,
|
|
3148
|
+
});
|
|
3149
|
+
return this;
|
|
3150
|
+
}
|
|
3151
|
+
addRawButton(obj) {
|
|
3152
|
+
if (typeof obj !== 'object' || obj === null || Array.isArray(obj)) {
|
|
3153
|
+
throw new TypeError('Buttons must be a plain object');
|
|
3154
|
+
}
|
|
3155
|
+
this._buttons.push(obj);
|
|
3156
|
+
return this;
|
|
3157
|
+
}
|
|
3158
|
+
setThumbnail(path) {
|
|
3159
|
+
if (!path)
|
|
3160
|
+
throw new Error('Url or buffer needed');
|
|
3161
|
+
this._image = path;
|
|
3162
|
+
return this;
|
|
3163
|
+
}
|
|
3164
|
+
setMedia(obj) {
|
|
3165
|
+
if (typeof obj !== 'object' || obj === null || Array.isArray(obj)) {
|
|
3166
|
+
throw new TypeError('Media must be a plain object');
|
|
3167
|
+
}
|
|
3168
|
+
this._data = obj;
|
|
3169
|
+
return this;
|
|
3170
|
+
}
|
|
3171
|
+
async build(jid, { ...options } = {}) {
|
|
3172
|
+
let _thumbnail = this._image ? await Toolkit.resize(Buffer.isBuffer(this._image) ? this._image : await Toolkit.fetchBuffer(this._image, {}, { silent: true }), 300, 300) : null;
|
|
3173
|
+
const msg = (0, index_js_1.generateWAMessageFromContent)(jid, {
|
|
3174
|
+
...this._extraPayload,
|
|
3175
|
+
buttonsMessage: {
|
|
3176
|
+
contentText: this._body,
|
|
3177
|
+
footerText: this._footer,
|
|
3178
|
+
...(this._data
|
|
3179
|
+
? this._data
|
|
3180
|
+
: {
|
|
3181
|
+
headerType: 6,
|
|
3182
|
+
locationMessage: {
|
|
3183
|
+
degreesLatitude: 0,
|
|
3184
|
+
degreesLongitude: 0,
|
|
3185
|
+
name: this._title,
|
|
3186
|
+
address: this._subtitle,
|
|
3187
|
+
jpegThumbnail: _thumbnail,
|
|
3188
|
+
},
|
|
3189
|
+
}),
|
|
3190
|
+
viewOnce: true,
|
|
3191
|
+
contextInfo: this._contextInfo,
|
|
3192
|
+
buttons: [...this._buttons],
|
|
3193
|
+
},
|
|
3194
|
+
}, { ...options });
|
|
3195
|
+
return msg;
|
|
3196
|
+
}
|
|
3197
|
+
async send(jid, { ...options } = {}) {
|
|
3198
|
+
if (this._buttons.length < 1)
|
|
3199
|
+
throw new Error('ButtonV2 requires at least one button');
|
|
3200
|
+
const msg = await this.build(jid, options);
|
|
3201
|
+
await this.#client.relayMessage(msg.key.remoteJid, msg.message, {
|
|
3202
|
+
messageId: msg.key.id,
|
|
3203
|
+
additionalNodes: [
|
|
3204
|
+
{
|
|
3205
|
+
tag: 'biz',
|
|
3206
|
+
attrs: {},
|
|
3207
|
+
content: [
|
|
3208
|
+
{
|
|
3209
|
+
tag: 'interactive',
|
|
3210
|
+
attrs: { type: 'native_flow', v: '1' },
|
|
3211
|
+
content: [{ tag: 'native_flow', attrs: { v: '9', name: 'mixed' } }],
|
|
3212
|
+
},
|
|
3213
|
+
],
|
|
3214
|
+
},
|
|
3215
|
+
],
|
|
3216
|
+
...options,
|
|
3217
|
+
});
|
|
3218
|
+
return msg;
|
|
3219
|
+
}
|
|
3220
|
+
button(displayText, id) { return this.addButton(displayText, id); }
|
|
3221
|
+
row(cb) {
|
|
3222
|
+
const r = new RowBuilder();
|
|
3223
|
+
cb(r);
|
|
3224
|
+
r.buttons.forEach(b => this._buttons.push(b));
|
|
3225
|
+
return this;
|
|
3226
|
+
}
|
|
3227
|
+
}
|
|
3228
|
+
exports.ButtonV2 = ButtonV2;
|
|
3229
|
+
class Carousel extends BaseBuilder {
|
|
3230
|
+
#client;
|
|
3231
|
+
constructor(client) {
|
|
3232
|
+
super();
|
|
3233
|
+
if (!client) {
|
|
3234
|
+
throw new Error('Socket is required');
|
|
3235
|
+
}
|
|
3236
|
+
this.#client = client;
|
|
3237
|
+
this._cards = [];
|
|
3238
|
+
}
|
|
3239
|
+
addCard(card) {
|
|
3240
|
+
const cards = Array.isArray(card) ? card : [card];
|
|
3241
|
+
const baseIndex = this._cards.length;
|
|
3242
|
+
for (const [index, c] of cards.entries()) {
|
|
3243
|
+
if (!c?.header?.hasMediaAttachment) {
|
|
3244
|
+
throw new Error(`Card [${baseIndex + index}] must include an image or video in header`);
|
|
3245
|
+
}
|
|
3246
|
+
}
|
|
3247
|
+
this._cards.push(...cards);
|
|
3248
|
+
return this;
|
|
3249
|
+
}
|
|
3250
|
+
build(jid, { ...options } = {}) {
|
|
3251
|
+
return (0, index_js_1.generateWAMessageFromContent)(jid, {
|
|
3252
|
+
...this._extraPayload,
|
|
3253
|
+
interactiveMessage: {
|
|
3254
|
+
header: {
|
|
3255
|
+
hasMediaAttachment: false,
|
|
3256
|
+
},
|
|
3257
|
+
body: { text: this._body },
|
|
3258
|
+
footer: { text: this._footer },
|
|
3259
|
+
contextInfo: this._contextInfo,
|
|
3260
|
+
carouselMessage: {
|
|
3261
|
+
cards: this._cards,
|
|
3262
|
+
},
|
|
3263
|
+
},
|
|
3264
|
+
}, { ...options });
|
|
3265
|
+
}
|
|
3266
|
+
async send(jid, { ...options } = {}) {
|
|
3267
|
+
const msg = await this.build(jid, options);
|
|
3268
|
+
await this.#client.relayMessage(msg.key.remoteJid, msg.message, {
|
|
3269
|
+
messageId: msg.key.id,
|
|
3270
|
+
additionalNodes: [
|
|
3271
|
+
{
|
|
3272
|
+
tag: 'biz',
|
|
3273
|
+
attrs: {},
|
|
3274
|
+
content: [
|
|
3275
|
+
{
|
|
3276
|
+
tag: 'interactive',
|
|
3277
|
+
attrs: { type: 'native_flow', v: '1' },
|
|
3278
|
+
content: [{ tag: 'native_flow', attrs: { v: '9', name: 'mixed' } }],
|
|
3279
|
+
},
|
|
3280
|
+
],
|
|
3281
|
+
},
|
|
3282
|
+
],
|
|
3283
|
+
...options,
|
|
3284
|
+
});
|
|
3285
|
+
return msg;
|
|
3286
|
+
}
|
|
3287
|
+
card(cb) {
|
|
3288
|
+
if (!this._cardBuilders)
|
|
3289
|
+
this._cardBuilders = [];
|
|
3290
|
+
const cbObj = new CardBuilder(this.#client);
|
|
3291
|
+
cb(cbObj);
|
|
3292
|
+
this._cardBuilders.push(cbObj);
|
|
3293
|
+
return this;
|
|
3294
|
+
}
|
|
3295
|
+
}
|
|
3296
|
+
exports.Carousel = Carousel;
|
|
3297
|
+
class AIRich extends BaseBuilder {
|
|
3298
|
+
#client;
|
|
3299
|
+
constructor(client) {
|
|
3300
|
+
if (!client) {
|
|
3301
|
+
throw new Error('Socket is required');
|
|
3302
|
+
}
|
|
3303
|
+
super();
|
|
3304
|
+
this.#client = client;
|
|
3305
|
+
this._contextInfo = {};
|
|
3306
|
+
this._submessages = [];
|
|
3307
|
+
this._sections = [];
|
|
3308
|
+
this._richResponseSources = [];
|
|
3309
|
+
}
|
|
3310
|
+
addSubmessage(submessage) {
|
|
3311
|
+
const items = Array.isArray(submessage) ? submessage : [submessage];
|
|
3312
|
+
for (const item of items) {
|
|
3313
|
+
if (typeof item !== 'object' || item === null || Array.isArray(item)) {
|
|
3314
|
+
throw new TypeError('Submessage must be a plain object or array of plain objects');
|
|
3315
|
+
}
|
|
3316
|
+
this._submessages.push(item);
|
|
3317
|
+
}
|
|
3318
|
+
return this;
|
|
3319
|
+
}
|
|
3320
|
+
addSection(section) {
|
|
3321
|
+
const items = Array.isArray(section) ? section : [section];
|
|
3322
|
+
for (const item of items) {
|
|
3323
|
+
if (typeof item !== 'object' || item === null || Array.isArray(item)) {
|
|
3324
|
+
throw new TypeError('Section must be a plain object or array of plain objects');
|
|
3325
|
+
}
|
|
3326
|
+
this._sections.push(item);
|
|
3327
|
+
}
|
|
3328
|
+
return this;
|
|
3329
|
+
}
|
|
3330
|
+
addText(text, { hyperlink = true, citation = true, latex = true } = {}) {
|
|
3331
|
+
if (typeof text != 'string') {
|
|
3332
|
+
throw new TypeError('Text must be a string');
|
|
3333
|
+
}
|
|
3334
|
+
const { text: extractedText, inline_entities } = extractIE(text, {
|
|
3335
|
+
hyperlink,
|
|
3336
|
+
citation,
|
|
3337
|
+
latex,
|
|
3338
|
+
});
|
|
3339
|
+
this._submessages.push({
|
|
3340
|
+
messageType: 2,
|
|
3341
|
+
messageText: extractedText,
|
|
3342
|
+
});
|
|
3343
|
+
this._sections.push(AIRich.newLayout('Single', {
|
|
3344
|
+
text: extractedText,
|
|
3345
|
+
...(inline_entities.length && {
|
|
3346
|
+
inline_entities,
|
|
3347
|
+
}),
|
|
3348
|
+
__typename: 'GenAIMarkdownTextUXPrimitive',
|
|
3349
|
+
}));
|
|
3350
|
+
return this;
|
|
3351
|
+
}
|
|
3352
|
+
addCode(language, code) {
|
|
3353
|
+
if (typeof language !== 'string' || typeof code !== 'string') {
|
|
3354
|
+
throw new TypeError('Language and code must be a string');
|
|
3355
|
+
}
|
|
3356
|
+
const meta = AIRich.tokenizer(code, language);
|
|
3357
|
+
this._submessages.push({
|
|
3358
|
+
messageType: 5,
|
|
3359
|
+
codeMetadata: {
|
|
3360
|
+
codeLanguage: language,
|
|
3361
|
+
codeBlocks: meta.codeBlock,
|
|
3362
|
+
},
|
|
3363
|
+
});
|
|
3364
|
+
this._sections.push(AIRich.newLayout('Single', {
|
|
3365
|
+
language,
|
|
3366
|
+
code_blocks: meta.unified_codeBlock,
|
|
3367
|
+
__typename: 'GenAICodeUXPrimitive',
|
|
3368
|
+
}));
|
|
3369
|
+
return this;
|
|
3370
|
+
}
|
|
3371
|
+
addTable(table, { hyperlink = true, citation = true, latex = true } = {}) {
|
|
3372
|
+
if (!Array.isArray(table)) {
|
|
3373
|
+
throw new TypeError('Table must be an array');
|
|
3374
|
+
}
|
|
3375
|
+
const meta = AIRich.toTableMetadata(table, { hyperlink, citation, latex });
|
|
3376
|
+
this._submessages.push({
|
|
3377
|
+
messageType: 4,
|
|
3378
|
+
tableMetadata: {
|
|
3379
|
+
title: meta.title,
|
|
3380
|
+
rows: meta.rows,
|
|
3381
|
+
},
|
|
3382
|
+
});
|
|
3383
|
+
this._sections.push(AIRich.newLayout('Single', {
|
|
3384
|
+
rows: meta.unified_rows,
|
|
3385
|
+
__typename: 'GenATableUXPrimitive',
|
|
3386
|
+
}));
|
|
3387
|
+
return this;
|
|
3388
|
+
}
|
|
3389
|
+
addSource(sources = []) {
|
|
3390
|
+
if (!(Array.isArray(sources) && (sources.every((item) => typeof item === 'string') || sources.every((item) => Array.isArray(item) && item.every((v) => typeof v === 'string'))))) {
|
|
3391
|
+
throw new TypeError('Sources must be a string array or an array of string arrays');
|
|
3392
|
+
}
|
|
3393
|
+
if (sources.every((item) => typeof item === 'string')) {
|
|
3394
|
+
sources = [sources];
|
|
3395
|
+
}
|
|
3396
|
+
const source = sources.map(([icon, url, text]) => ({
|
|
3397
|
+
source_type: 'THIRD_PARTY',
|
|
3398
|
+
source_display_name: text ?? '',
|
|
3399
|
+
source_subtitle: 'AI',
|
|
3400
|
+
source_url: url ?? '',
|
|
3401
|
+
favicon: {
|
|
3402
|
+
url: Toolkit.resolveMedia(this.#client, icon ?? '', 'image'),
|
|
3403
|
+
mime_type: 'image/jpeg',
|
|
3404
|
+
width: 16,
|
|
3405
|
+
height: 16,
|
|
3406
|
+
},
|
|
3407
|
+
}));
|
|
3408
|
+
this._sections.push(AIRich.newLayout('Single', {
|
|
3409
|
+
sources: source,
|
|
3410
|
+
__typename: 'GenAISearchResultPrimitive',
|
|
3411
|
+
}));
|
|
3412
|
+
return this;
|
|
3413
|
+
}
|
|
3414
|
+
addReels(reelsItems = []) {
|
|
3415
|
+
if (!((reelsItems && typeof reelsItems === 'object' && !Array.isArray(reelsItems)) ||
|
|
3416
|
+
(Array.isArray(reelsItems) && reelsItems.every((item) => item && typeof item === 'object' && !Array.isArray(item))))) {
|
|
3417
|
+
throw new TypeError('Reels items must be an object or an array of objects');
|
|
3418
|
+
}
|
|
3419
|
+
if (!Array.isArray(reelsItems)) {
|
|
3420
|
+
reelsItems = [reelsItems];
|
|
3421
|
+
}
|
|
3422
|
+
const reels = reelsItems.map((item) => ({
|
|
3423
|
+
...item,
|
|
3424
|
+
_avatar: Toolkit.resolveMedia(this.#client, item.profileIconUrl ?? item.profile_url ?? item.profile ?? '', 'image'),
|
|
3425
|
+
_thumbnail: Toolkit.resolveMedia(this.#client, item.thumbnailUrl ?? item.thumbnail ?? '', 'image'),
|
|
3426
|
+
}));
|
|
3427
|
+
this._submessages.push({
|
|
3428
|
+
messageType: 9,
|
|
3429
|
+
contentItemsMetadata: {
|
|
3430
|
+
contentType: 1,
|
|
3431
|
+
itemsMetadata: reels.map((item) => ({
|
|
3432
|
+
reelItem: {
|
|
3433
|
+
title: item.username ?? '',
|
|
3434
|
+
profileIconUrl: item._avatar,
|
|
3435
|
+
thumbnailUrl: item._thumbnail,
|
|
3436
|
+
videoUrl: item.videoUrl ?? item.url ?? '',
|
|
3437
|
+
},
|
|
3438
|
+
})),
|
|
3439
|
+
},
|
|
3440
|
+
});
|
|
3441
|
+
reels.forEach((item, idx) => {
|
|
3442
|
+
this._richResponseSources.push({
|
|
3443
|
+
provider: '\u004E\u0049\u0058\u0045\u004C',
|
|
3444
|
+
thumbnailCDNURL: item._thumbnail,
|
|
3445
|
+
sourceProviderURL: item.videoUrl ?? item.url ?? '',
|
|
3446
|
+
sourceQuery: '',
|
|
3447
|
+
faviconCDNURL: item._avatar,
|
|
3448
|
+
citationNumber: idx + 1,
|
|
3449
|
+
sourceTitle: item.username ?? '',
|
|
3450
|
+
});
|
|
3451
|
+
});
|
|
3452
|
+
this._sections.push(AIRich.newLayout('HScroll', reels.map((item) => ({
|
|
3453
|
+
reels_url: item.videoUrl ?? item.url ?? '',
|
|
3454
|
+
thumbnail_url: item._thumbnail,
|
|
3455
|
+
creator: item.username ?? item.title ?? '',
|
|
3456
|
+
avatar_url: item._avatar,
|
|
3457
|
+
reels_title: item.reels_title ?? item.title ?? '',
|
|
3458
|
+
likes_count: item.likes_count ?? item.like ?? 0,
|
|
3459
|
+
shares_count: item.shares_count ?? item.share ?? 0,
|
|
3460
|
+
view_count: item.view_count ?? item.view ?? 0,
|
|
3461
|
+
reel_source: item.reel_source ?? item.source ?? 'IG',
|
|
3462
|
+
is_verified: !!(item.is_verified || item.verified),
|
|
3463
|
+
__typename: 'GenAIReelPrimitive',
|
|
3464
|
+
}))));
|
|
3465
|
+
return this;
|
|
3466
|
+
}
|
|
3467
|
+
addImage(imageUrl, { resolveUrl = false } = {}) {
|
|
3468
|
+
if (!(typeof imageUrl === 'string' || Buffer.isBuffer(imageUrl) || (Array.isArray(imageUrl) && imageUrl.every((v) => typeof v === 'string' || Buffer.isBuffer(v))))) {
|
|
3469
|
+
throw new TypeError('imageUrl must be string | buffer | array of string/buffer');
|
|
3470
|
+
}
|
|
3471
|
+
const list = Array.isArray(imageUrl)
|
|
3472
|
+
? imageUrl.map((v) => {
|
|
3473
|
+
const url = Toolkit.resolveMedia(this.#client, v, 'image', { resolveUrl });
|
|
3474
|
+
return {
|
|
3475
|
+
imagePreviewUrl: url,
|
|
3476
|
+
imageHighResUrl: url,
|
|
3477
|
+
sourceUrl: url,
|
|
3478
|
+
};
|
|
3479
|
+
})
|
|
3480
|
+
: (() => {
|
|
3481
|
+
const url = Toolkit.resolveMedia(this.#client, imageUrl, 'image', { resolveUrl });
|
|
3482
|
+
return [
|
|
3483
|
+
{
|
|
3484
|
+
imagePreviewUrl: url,
|
|
3485
|
+
imageHighResUrl: url,
|
|
3486
|
+
sourceUrl: url,
|
|
3487
|
+
},
|
|
3488
|
+
];
|
|
3489
|
+
})();
|
|
3490
|
+
this._submessages.push({
|
|
3491
|
+
messageType: 1,
|
|
3492
|
+
gridImageMetadata: {
|
|
3493
|
+
gridImageUrl: {
|
|
3494
|
+
imagePreviewUrl: list[0]?.imagePreviewUrl,
|
|
3495
|
+
},
|
|
3496
|
+
imageUrls: list,
|
|
3497
|
+
},
|
|
3498
|
+
});
|
|
3499
|
+
list.forEach(({ imagePreviewUrl }) => {
|
|
3500
|
+
this._sections.push(AIRich.newLayout('Single', {
|
|
3501
|
+
media: {
|
|
3502
|
+
url: imagePreviewUrl,
|
|
3503
|
+
mime_type: 'image/png',
|
|
3504
|
+
},
|
|
3505
|
+
imagine_type: 'IMAGE',
|
|
3506
|
+
status: { status: 'READY' },
|
|
3507
|
+
__typename: 'GenAIImaginePrimitive',
|
|
3508
|
+
}));
|
|
3509
|
+
});
|
|
3510
|
+
return this;
|
|
3511
|
+
}
|
|
3512
|
+
addVideo(videoUrl, { autoFill = true } = {}) {
|
|
3513
|
+
const isObjectVideo = (v) => v && typeof v === 'object' && v.url;
|
|
3514
|
+
const isValidPrimitive = typeof videoUrl === 'string' ||
|
|
3515
|
+
Buffer.isBuffer(videoUrl) ||
|
|
3516
|
+
isObjectVideo(videoUrl) ||
|
|
3517
|
+
(Array.isArray(videoUrl) && videoUrl.every((v) => typeof v === 'string' || Buffer.isBuffer(v) || isObjectVideo(v)));
|
|
3518
|
+
if (!isValidPrimitive) {
|
|
3519
|
+
throw new TypeError('videoUrl must be string | buffer | object | array');
|
|
3520
|
+
}
|
|
3521
|
+
const items = Array.isArray(videoUrl) ? videoUrl : [videoUrl];
|
|
3522
|
+
this._submessages.push({
|
|
3523
|
+
messageType: 2,
|
|
3524
|
+
messageText: '[ CANNOT_LOAD_VIDEO - \u004E\u0049\u0058\u0045\u004C ]',
|
|
3525
|
+
});
|
|
3526
|
+
items.forEach((item) => {
|
|
3527
|
+
const isObject = isObjectVideo(item);
|
|
3528
|
+
const url = isObject ? Toolkit.resolveMedia(this.#client, item.url ?? '', 'video') : Toolkit.resolveMedia(this.#client, item, 'video');
|
|
3529
|
+
const bufferPromise = autoFill ? Promise.resolve(url).then((u) => Toolkit.fetchBuffer(u)) : null;
|
|
3530
|
+
const file_length = isObject && item.file_length != null ? item.file_length : autoFill ? bufferPromise.then((b) => b?.length ?? 0) : 0;
|
|
3531
|
+
const duration = isObject && item.duration != null
|
|
3532
|
+
? item.duration
|
|
3533
|
+
: autoFill
|
|
3534
|
+
? bufferPromise.then((b) => Toolkit.getMp4Duration(b, {
|
|
3535
|
+
silent: true,
|
|
3536
|
+
}))
|
|
3537
|
+
: 0;
|
|
3538
|
+
const thumbnail = isObject && item.thumbnail
|
|
3539
|
+
? Toolkit.resolveMedia(this.#client, item.thumbnail, 'image', {
|
|
3540
|
+
result: 'base64',
|
|
3541
|
+
resize: true,
|
|
3542
|
+
width: 300,
|
|
3543
|
+
height: 300,
|
|
3544
|
+
})
|
|
3545
|
+
: autoFill
|
|
3546
|
+
? bufferPromise
|
|
3547
|
+
? bufferPromise.then((b) => Toolkit.getMp4Preview(b, {
|
|
3548
|
+
time: 0,
|
|
3549
|
+
result: 'base64',
|
|
3550
|
+
}))
|
|
3551
|
+
: null
|
|
3552
|
+
: null;
|
|
3553
|
+
this._sections.push(AIRich.newLayout('Single', {
|
|
3554
|
+
media: {
|
|
3555
|
+
url,
|
|
3556
|
+
mime_type: isObject ? (item.mime_type ?? 'video/mp4') : 'video/mp4',
|
|
3557
|
+
file_length,
|
|
3558
|
+
duration,
|
|
3559
|
+
},
|
|
3560
|
+
imagine_type: 'ANIMATE',
|
|
3561
|
+
status: { status: 'READY' },
|
|
3562
|
+
thumbnail: {
|
|
3563
|
+
raw_media: thumbnail,
|
|
3564
|
+
},
|
|
3565
|
+
__typename: 'GenAIImaginePrimitive',
|
|
3566
|
+
}));
|
|
3567
|
+
});
|
|
3568
|
+
return this;
|
|
3569
|
+
}
|
|
3570
|
+
addProduct(data = {}) {
|
|
3571
|
+
if (!((data && typeof data === 'object' && !Array.isArray(data)) || (Array.isArray(data) && data.every((item) => item && typeof item === 'object' && !Array.isArray(item))))) {
|
|
3572
|
+
throw new TypeError('Product items must be an object or an array of objects');
|
|
3573
|
+
}
|
|
3574
|
+
this._submessages.push({
|
|
3575
|
+
messageType: 2,
|
|
3576
|
+
messageText: '[ CANNOT_LOAD_PRODUCT - NIXEL ]',
|
|
3577
|
+
});
|
|
3578
|
+
const items = Array.isArray(data) ? data : [data];
|
|
3579
|
+
const product = items.map((item) => ({
|
|
3580
|
+
title: item.title,
|
|
3581
|
+
brand: item.brand,
|
|
3582
|
+
price: item.price,
|
|
3583
|
+
sale_price: item.sale_price,
|
|
3584
|
+
product_url: item.product_url ?? item.url,
|
|
3585
|
+
image: {
|
|
3586
|
+
url: Toolkit.resolveMedia(this.#client, item.image_url ?? item.image, 'image'),
|
|
3587
|
+
},
|
|
3588
|
+
additional_images: [
|
|
3589
|
+
{
|
|
3590
|
+
url: Toolkit.resolveMedia(this.#client, item.icon_url ?? item.icon, 'image'),
|
|
3591
|
+
},
|
|
3592
|
+
],
|
|
3593
|
+
__typename: 'GenAIProductItemCardPrimitive',
|
|
3594
|
+
}));
|
|
3595
|
+
this._sections.push(AIRich.newLayout(Array.isArray(data) ? 'HScroll' : 'Single', Array.isArray(data) ? product : product[0]));
|
|
3596
|
+
return this;
|
|
3597
|
+
}
|
|
3598
|
+
addPost(data = {}) {
|
|
3599
|
+
if (!((data && typeof data === 'object' && !Array.isArray(data)) || (Array.isArray(data) && data.every((item) => item && typeof item === 'object' && !Array.isArray(item))))) {
|
|
3600
|
+
throw new TypeError('Post items must be an object or an array of objects');
|
|
3601
|
+
}
|
|
3602
|
+
const posts = Array.isArray(data) ? data : [data];
|
|
3603
|
+
this._submessages.push({
|
|
3604
|
+
messageType: 2,
|
|
3605
|
+
messageText: '[ CANNOT_LOAD_POST - NIXEL ]',
|
|
3606
|
+
});
|
|
3607
|
+
const primitives = posts.map((p) => ({
|
|
3608
|
+
title: p.title ?? '',
|
|
3609
|
+
subtitle: p.subtitle ?? '',
|
|
3610
|
+
username: p.username ?? '',
|
|
3611
|
+
profile_picture_url: Toolkit.resolveMedia(this.#client, p.profile_picture_url ?? p.profile_url ?? p.profile ?? '', 'image'),
|
|
3612
|
+
is_verified: !!(p.is_verified || p.verified),
|
|
3613
|
+
thumbnail_url: Toolkit.resolveMedia(this.#client, p.thumbnail_url ?? p.thumbnail ?? '', 'image'),
|
|
3614
|
+
post_caption: p.post_caption ?? p.caption ?? '',
|
|
3615
|
+
likes_count: p.likes_count ?? p.like ?? 0,
|
|
3616
|
+
comments_count: p.comments_count ?? p.comment ?? 0,
|
|
3617
|
+
shares_count: p.shares_count ?? p.share ?? 0,
|
|
3618
|
+
post_url: p.post_url ?? p.url ?? '',
|
|
3619
|
+
post_deeplink: p.post_deeplink ?? p.deeplink ?? '',
|
|
3620
|
+
source_app: p.source_app || p.source || 'INSTAGRAM',
|
|
3621
|
+
footer_label: p.footer_label ?? p.footer ?? '',
|
|
3622
|
+
footer_icon: Toolkit.resolveMedia(this.#client, p.footer_icon ?? p.icon ?? '', 'image'),
|
|
3623
|
+
is_carousel: posts.length > 1,
|
|
3624
|
+
orientation: p.orientation ?? 'LANDSCAPE',
|
|
3625
|
+
post_type: p.post_type ?? 'VIDEO',
|
|
3626
|
+
__typename: 'GenAIPostPrimitive',
|
|
3627
|
+
}));
|
|
3628
|
+
this._sections.push(AIRich.newLayout('HScroll', primitives));
|
|
3629
|
+
return this;
|
|
3630
|
+
}
|
|
3631
|
+
addTip(text) {
|
|
3632
|
+
this._submessages.push({
|
|
3633
|
+
messageType: 2,
|
|
3634
|
+
messageText: text,
|
|
3635
|
+
});
|
|
3636
|
+
this._sections.push(AIRich.newLayout('Single', {
|
|
3637
|
+
text,
|
|
3638
|
+
__typename: 'GenAIMetadataTextPrimitive',
|
|
3639
|
+
}));
|
|
3640
|
+
return this;
|
|
3641
|
+
}
|
|
3642
|
+
addSuggest(suggestion, { scroll = true, layout } = {}) {
|
|
3643
|
+
if (!(typeof suggestion === 'string' || (Array.isArray(suggestion) && suggestion.every((v) => typeof v === 'string')))) {
|
|
3644
|
+
throw new TypeError('Suggestion must be a string or array of strings');
|
|
3645
|
+
}
|
|
3646
|
+
const suggest = Array.isArray(suggestion)
|
|
3647
|
+
? suggestion.map((text) => ({
|
|
3648
|
+
prompt_text: text,
|
|
3649
|
+
prompt_type: 'SUGGESTED_PROMPT',
|
|
3650
|
+
__typename: 'GenAIFollowUpSuggestionPillPrimitive',
|
|
3651
|
+
}))
|
|
3652
|
+
: [
|
|
3653
|
+
{
|
|
3654
|
+
prompt_text: suggestion,
|
|
3655
|
+
prompt_type: 'SUGGESTED_PROMPT',
|
|
3656
|
+
__typename: 'GenAIFollowUpSuggestionPillPrimitive',
|
|
3657
|
+
},
|
|
3658
|
+
];
|
|
3659
|
+
const type = layout ?? (suggest.length === 1 ? 'Single' : scroll ? 'HScroll' : 'ActionRow');
|
|
3660
|
+
this._sections.push(AIRich.newLayout(type, type === 'Single' ? suggest[0] : suggest, { __typename: 'GenAIUnifiedResponseSection' }));
|
|
3661
|
+
return this;
|
|
3662
|
+
}
|
|
3663
|
+
async build({ forwarded = true, notification = false, includesUnifiedResponse = true, includesSubmessages = true, quoted, quotedParticipant, ...options } = {}) {
|
|
3664
|
+
const forward = forwarded
|
|
3665
|
+
? {
|
|
3666
|
+
forwardingScore: 1,
|
|
3667
|
+
isForwarded: true,
|
|
3668
|
+
forwardedAiBotMessageInfo: { botJid: '0@bot' },
|
|
3669
|
+
forwardOrigin: 4,
|
|
3670
|
+
}
|
|
3671
|
+
: {};
|
|
3672
|
+
const notif = notification
|
|
3673
|
+
? {
|
|
3674
|
+
sessionTransparencyMetadata: {
|
|
3675
|
+
disclaimerText: '~ Ahmad tumbuh kembang',
|
|
3676
|
+
hcaId: `hca_${Date.now()}`,
|
|
3677
|
+
sessionTransparencyType: 1,
|
|
3678
|
+
},
|
|
3679
|
+
}
|
|
3680
|
+
: {};
|
|
3681
|
+
const qObj = quoted
|
|
3682
|
+
? {
|
|
3683
|
+
stanzaId: quoted?.key?.id || quoted?.id,
|
|
3684
|
+
participant: quotedParticipant || quoted?.key?.participant || quoted?.key?.remoteJid,
|
|
3685
|
+
quotedType: 0,
|
|
3686
|
+
quotedMessage: typeof quoted === 'object' && quoted !== null ? (quoted.message ?? quoted) : undefined,
|
|
3687
|
+
}
|
|
3688
|
+
: {};
|
|
3689
|
+
const sections = this._footer
|
|
3690
|
+
? [
|
|
3691
|
+
...(await waitAllPromises(this._sections)),
|
|
3692
|
+
AIRich.newLayout('Single', {
|
|
3693
|
+
text: this._footer,
|
|
3694
|
+
__typename: 'GenAIMetadataTextPrimitive',
|
|
3695
|
+
}),
|
|
3696
|
+
]
|
|
3697
|
+
: [...(await waitAllPromises(this._sections))];
|
|
3698
|
+
return {
|
|
3699
|
+
messageContextInfo: {
|
|
3700
|
+
deviceListMetadata: {},
|
|
3701
|
+
deviceListMetadataVersion: 2,
|
|
3702
|
+
botMetadata: {
|
|
3703
|
+
messageDisclaimerText: this._title,
|
|
3704
|
+
richResponseSourcesMetadata: { sources: this._richResponseSources },
|
|
3705
|
+
...notif,
|
|
3706
|
+
},
|
|
3707
|
+
},
|
|
3708
|
+
...this._extraPayload,
|
|
3709
|
+
botForwardedMessage: {
|
|
3710
|
+
message: {
|
|
3711
|
+
richResponseMessage: {
|
|
3712
|
+
messageType: 1,
|
|
3713
|
+
submessages: includesSubmessages ? await waitAllPromises(this._submessages) : [],
|
|
3714
|
+
unifiedResponse: {
|
|
3715
|
+
data: includesUnifiedResponse ? Buffer.from(JSON.stringify({ response_id: crypto_1.default.randomUUID(), sections })).toString('base64') : '',
|
|
3716
|
+
},
|
|
3717
|
+
contextInfo: {
|
|
3718
|
+
...forward,
|
|
3719
|
+
...qObj,
|
|
3720
|
+
...this._contextInfo,
|
|
3721
|
+
},
|
|
3722
|
+
},
|
|
3723
|
+
},
|
|
3724
|
+
},
|
|
3725
|
+
};
|
|
3726
|
+
}
|
|
3727
|
+
async send(jid, { forwarded, notification, includesUnifiedResponse, includesSubmessages, ...options } = {}) {
|
|
3728
|
+
const msg = await this.build({ forwarded, notification, includesUnifiedResponse, includesSubmessages, ...options });
|
|
3729
|
+
return await this.#client.relayMessage(jid, msg, { ...options });
|
|
3730
|
+
}
|
|
3731
|
+
static tokenizer(code, lang = 'javascript') {
|
|
3732
|
+
const keywordsMap = {
|
|
3733
|
+
javascript: new Set([
|
|
3734
|
+
'break',
|
|
3735
|
+
'case',
|
|
3736
|
+
'catch',
|
|
3737
|
+
'continue',
|
|
3738
|
+
'debugger',
|
|
3739
|
+
'delete',
|
|
3740
|
+
'do',
|
|
3741
|
+
'else',
|
|
3742
|
+
'finally',
|
|
3743
|
+
'for',
|
|
3744
|
+
'function',
|
|
3745
|
+
'if',
|
|
3746
|
+
'in',
|
|
3747
|
+
'instanceof',
|
|
3748
|
+
'new',
|
|
3749
|
+
'return',
|
|
3750
|
+
'switch',
|
|
3751
|
+
'this',
|
|
3752
|
+
'throw',
|
|
3753
|
+
'try',
|
|
3754
|
+
'typeof',
|
|
3755
|
+
'var',
|
|
3756
|
+
'void',
|
|
3757
|
+
'while',
|
|
3758
|
+
'with',
|
|
3759
|
+
'true',
|
|
3760
|
+
'false',
|
|
3761
|
+
'null',
|
|
3762
|
+
'undefined',
|
|
3763
|
+
'class',
|
|
3764
|
+
'const',
|
|
3765
|
+
'let',
|
|
3766
|
+
'super',
|
|
3767
|
+
'extends',
|
|
3768
|
+
'export',
|
|
3769
|
+
'import',
|
|
3770
|
+
'yield',
|
|
3771
|
+
'static',
|
|
3772
|
+
'constructor',
|
|
3773
|
+
'async',
|
|
3774
|
+
'await',
|
|
3775
|
+
'get',
|
|
3776
|
+
'set',
|
|
3777
|
+
]),
|
|
3778
|
+
typescript: new Set([
|
|
3779
|
+
'abstract',
|
|
3780
|
+
'any',
|
|
3781
|
+
'as',
|
|
3782
|
+
'asserts',
|
|
3783
|
+
'bigint',
|
|
3784
|
+
'boolean',
|
|
3785
|
+
'declare',
|
|
3786
|
+
'enum',
|
|
3787
|
+
'implements',
|
|
3788
|
+
'infer',
|
|
3789
|
+
'interface',
|
|
3790
|
+
'is',
|
|
3791
|
+
'keyof',
|
|
3792
|
+
'module',
|
|
3793
|
+
'namespace',
|
|
3794
|
+
'never',
|
|
3795
|
+
'readonly',
|
|
3796
|
+
'require',
|
|
3797
|
+
'number',
|
|
3798
|
+
'object',
|
|
3799
|
+
'override',
|
|
3800
|
+
'private',
|
|
3801
|
+
'protected',
|
|
3802
|
+
'public',
|
|
3803
|
+
'satisfies',
|
|
3804
|
+
'string',
|
|
3805
|
+
'symbol',
|
|
3806
|
+
'type',
|
|
3807
|
+
'unknown',
|
|
3808
|
+
'using',
|
|
3809
|
+
'from',
|
|
3810
|
+
'break',
|
|
3811
|
+
'case',
|
|
3812
|
+
'catch',
|
|
3813
|
+
'continue',
|
|
3814
|
+
'do',
|
|
3815
|
+
'else',
|
|
3816
|
+
'finally',
|
|
3817
|
+
'for',
|
|
3818
|
+
'function',
|
|
3819
|
+
'if',
|
|
3820
|
+
'new',
|
|
3821
|
+
'return',
|
|
3822
|
+
'switch',
|
|
3823
|
+
'this',
|
|
3824
|
+
'throw',
|
|
3825
|
+
'try',
|
|
3826
|
+
'var',
|
|
3827
|
+
'void',
|
|
3828
|
+
'while',
|
|
3829
|
+
'class',
|
|
3830
|
+
'const',
|
|
3831
|
+
'let',
|
|
3832
|
+
'extends',
|
|
3833
|
+
'import',
|
|
3834
|
+
'export',
|
|
3835
|
+
'async',
|
|
3836
|
+
'await',
|
|
3837
|
+
]),
|
|
3838
|
+
python: new Set([
|
|
3839
|
+
'False',
|
|
3840
|
+
'None',
|
|
3841
|
+
'True',
|
|
3842
|
+
'and',
|
|
3843
|
+
'as',
|
|
3844
|
+
'assert',
|
|
3845
|
+
'async',
|
|
3846
|
+
'await',
|
|
3847
|
+
'break',
|
|
3848
|
+
'class',
|
|
3849
|
+
'continue',
|
|
3850
|
+
'def',
|
|
3851
|
+
'del',
|
|
3852
|
+
'elif',
|
|
3853
|
+
'else',
|
|
3854
|
+
'except',
|
|
3855
|
+
'finally',
|
|
3856
|
+
'for',
|
|
3857
|
+
'from',
|
|
3858
|
+
'global',
|
|
3859
|
+
'if',
|
|
3860
|
+
'import',
|
|
3861
|
+
'in',
|
|
3862
|
+
'is',
|
|
3863
|
+
'lambda',
|
|
3864
|
+
'nonlocal',
|
|
3865
|
+
'not',
|
|
3866
|
+
'or',
|
|
3867
|
+
'pass',
|
|
3868
|
+
'raise',
|
|
3869
|
+
'return',
|
|
3870
|
+
'try',
|
|
3871
|
+
'while',
|
|
3872
|
+
'with',
|
|
3873
|
+
'yield',
|
|
3874
|
+
]),
|
|
3875
|
+
java: new Set([
|
|
3876
|
+
'abstract',
|
|
3877
|
+
'assert',
|
|
3878
|
+
'boolean',
|
|
3879
|
+
'break',
|
|
3880
|
+
'byte',
|
|
3881
|
+
'case',
|
|
3882
|
+
'catch',
|
|
3883
|
+
'char',
|
|
3884
|
+
'class',
|
|
3885
|
+
'const',
|
|
3886
|
+
'continue',
|
|
3887
|
+
'default',
|
|
3888
|
+
'do',
|
|
3889
|
+
'double',
|
|
3890
|
+
'else',
|
|
3891
|
+
'enum',
|
|
3892
|
+
'extends',
|
|
3893
|
+
'final',
|
|
3894
|
+
'finally',
|
|
3895
|
+
'float',
|
|
3896
|
+
'for',
|
|
3897
|
+
'goto',
|
|
3898
|
+
'if',
|
|
3899
|
+
'implements',
|
|
3900
|
+
'import',
|
|
3901
|
+
'instanceof',
|
|
3902
|
+
'int',
|
|
3903
|
+
'interface',
|
|
3904
|
+
'long',
|
|
3905
|
+
'native',
|
|
3906
|
+
'new',
|
|
3907
|
+
'package',
|
|
3908
|
+
'private',
|
|
3909
|
+
'protected',
|
|
3910
|
+
'public',
|
|
3911
|
+
'return',
|
|
3912
|
+
'short',
|
|
3913
|
+
'static',
|
|
3914
|
+
'strictfp',
|
|
3915
|
+
'super',
|
|
3916
|
+
'switch',
|
|
3917
|
+
'synchronized',
|
|
3918
|
+
'this',
|
|
3919
|
+
'throw',
|
|
3920
|
+
'throws',
|
|
3921
|
+
'transient',
|
|
3922
|
+
'try',
|
|
3923
|
+
'void',
|
|
3924
|
+
'volatile',
|
|
3925
|
+
'while',
|
|
3926
|
+
]),
|
|
3927
|
+
golang: new Set([
|
|
3928
|
+
'break',
|
|
3929
|
+
'case',
|
|
3930
|
+
'chan',
|
|
3931
|
+
'const',
|
|
3932
|
+
'continue',
|
|
3933
|
+
'default',
|
|
3934
|
+
'defer',
|
|
3935
|
+
'else',
|
|
3936
|
+
'fallthrough',
|
|
3937
|
+
'for',
|
|
3938
|
+
'func',
|
|
3939
|
+
'go',
|
|
3940
|
+
'goto',
|
|
3941
|
+
'if',
|
|
3942
|
+
'import',
|
|
3943
|
+
'interface',
|
|
3944
|
+
'map',
|
|
3945
|
+
'package',
|
|
3946
|
+
'range',
|
|
3947
|
+
'return',
|
|
3948
|
+
'select',
|
|
3949
|
+
'struct',
|
|
3950
|
+
'switch',
|
|
3951
|
+
'type',
|
|
3952
|
+
'var',
|
|
3953
|
+
]),
|
|
3954
|
+
c: new Set([
|
|
3955
|
+
'auto',
|
|
3956
|
+
'break',
|
|
3957
|
+
'case',
|
|
3958
|
+
'char',
|
|
3959
|
+
'const',
|
|
3960
|
+
'continue',
|
|
3961
|
+
'default',
|
|
3962
|
+
'do',
|
|
3963
|
+
'double',
|
|
3964
|
+
'else',
|
|
3965
|
+
'enum',
|
|
3966
|
+
'extern',
|
|
3967
|
+
'float',
|
|
3968
|
+
'for',
|
|
3969
|
+
'goto',
|
|
3970
|
+
'if',
|
|
3971
|
+
'int',
|
|
3972
|
+
'long',
|
|
3973
|
+
'register',
|
|
3974
|
+
'return',
|
|
3975
|
+
'short',
|
|
3976
|
+
'signed',
|
|
3977
|
+
'sizeof',
|
|
3978
|
+
'static',
|
|
3979
|
+
'struct',
|
|
3980
|
+
'switch',
|
|
3981
|
+
'typedef',
|
|
3982
|
+
'union',
|
|
3983
|
+
'unsigned',
|
|
3984
|
+
'void',
|
|
3985
|
+
'volatile',
|
|
3986
|
+
'while',
|
|
3987
|
+
]),
|
|
3988
|
+
cpp: new Set([
|
|
3989
|
+
'alignas',
|
|
3990
|
+
'alignof',
|
|
3991
|
+
'and',
|
|
3992
|
+
'auto',
|
|
3993
|
+
'bool',
|
|
3994
|
+
'break',
|
|
3995
|
+
'case',
|
|
3996
|
+
'catch',
|
|
3997
|
+
'class',
|
|
3998
|
+
'const',
|
|
3999
|
+
'constexpr',
|
|
4000
|
+
'continue',
|
|
4001
|
+
'delete',
|
|
4002
|
+
'do',
|
|
4003
|
+
'double',
|
|
4004
|
+
'else',
|
|
4005
|
+
'enum',
|
|
4006
|
+
'explicit',
|
|
4007
|
+
'export',
|
|
4008
|
+
'extern',
|
|
4009
|
+
'false',
|
|
4010
|
+
'float',
|
|
4011
|
+
'for',
|
|
4012
|
+
'friend',
|
|
4013
|
+
'if',
|
|
4014
|
+
'inline',
|
|
4015
|
+
'int',
|
|
4016
|
+
'long',
|
|
4017
|
+
'mutable',
|
|
4018
|
+
'namespace',
|
|
4019
|
+
'new',
|
|
4020
|
+
'noexcept',
|
|
4021
|
+
'nullptr',
|
|
4022
|
+
'operator',
|
|
4023
|
+
'private',
|
|
4024
|
+
'protected',
|
|
4025
|
+
'public',
|
|
4026
|
+
'return',
|
|
4027
|
+
'short',
|
|
4028
|
+
'signed',
|
|
4029
|
+
'sizeof',
|
|
4030
|
+
'static',
|
|
4031
|
+
'struct',
|
|
4032
|
+
'switch',
|
|
4033
|
+
'template',
|
|
4034
|
+
'this',
|
|
4035
|
+
'throw',
|
|
4036
|
+
'true',
|
|
4037
|
+
'try',
|
|
4038
|
+
'typedef',
|
|
4039
|
+
'typename',
|
|
4040
|
+
'union',
|
|
4041
|
+
'unsigned',
|
|
4042
|
+
'using',
|
|
4043
|
+
'virtual',
|
|
4044
|
+
'void',
|
|
4045
|
+
'while',
|
|
4046
|
+
]),
|
|
4047
|
+
php: new Set([
|
|
4048
|
+
'abstract',
|
|
4049
|
+
'and',
|
|
4050
|
+
'array',
|
|
4051
|
+
'as',
|
|
4052
|
+
'break',
|
|
4053
|
+
'callable',
|
|
4054
|
+
'case',
|
|
4055
|
+
'catch',
|
|
4056
|
+
'class',
|
|
4057
|
+
'clone',
|
|
4058
|
+
'const',
|
|
4059
|
+
'continue',
|
|
4060
|
+
'declare',
|
|
4061
|
+
'default',
|
|
4062
|
+
'do',
|
|
4063
|
+
'echo',
|
|
4064
|
+
'else',
|
|
4065
|
+
'elseif',
|
|
4066
|
+
'empty',
|
|
4067
|
+
'enddeclare',
|
|
4068
|
+
'endfor',
|
|
4069
|
+
'endforeach',
|
|
4070
|
+
'endif',
|
|
4071
|
+
'endswitch',
|
|
4072
|
+
'endwhile',
|
|
4073
|
+
'extends',
|
|
4074
|
+
'final',
|
|
4075
|
+
'finally',
|
|
4076
|
+
'fn',
|
|
4077
|
+
'for',
|
|
4078
|
+
'foreach',
|
|
4079
|
+
'function',
|
|
4080
|
+
'global',
|
|
4081
|
+
'goto',
|
|
4082
|
+
'if',
|
|
4083
|
+
'implements',
|
|
4084
|
+
'include',
|
|
4085
|
+
'include_once',
|
|
4086
|
+
'instanceof',
|
|
4087
|
+
'interface',
|
|
4088
|
+
'match',
|
|
4089
|
+
'namespace',
|
|
4090
|
+
'new',
|
|
4091
|
+
'null',
|
|
4092
|
+
'or',
|
|
4093
|
+
'private',
|
|
4094
|
+
'protected',
|
|
4095
|
+
'public',
|
|
4096
|
+
'require',
|
|
4097
|
+
'require_once',
|
|
4098
|
+
'return',
|
|
4099
|
+
'static',
|
|
4100
|
+
'switch',
|
|
4101
|
+
'throw',
|
|
4102
|
+
'trait',
|
|
4103
|
+
'try',
|
|
4104
|
+
'use',
|
|
4105
|
+
'var',
|
|
4106
|
+
'while',
|
|
4107
|
+
'yield',
|
|
4108
|
+
]),
|
|
4109
|
+
rust: new Set([
|
|
4110
|
+
'as',
|
|
4111
|
+
'break',
|
|
4112
|
+
'const',
|
|
4113
|
+
'continue',
|
|
4114
|
+
'crate',
|
|
4115
|
+
'else',
|
|
4116
|
+
'enum',
|
|
4117
|
+
'extern',
|
|
4118
|
+
'false',
|
|
4119
|
+
'fn',
|
|
4120
|
+
'for',
|
|
4121
|
+
'if',
|
|
4122
|
+
'impl',
|
|
4123
|
+
'in',
|
|
4124
|
+
'let',
|
|
4125
|
+
'loop',
|
|
4126
|
+
'match',
|
|
4127
|
+
'mod',
|
|
4128
|
+
'move',
|
|
4129
|
+
'mut',
|
|
4130
|
+
'pub',
|
|
4131
|
+
'ref',
|
|
4132
|
+
'return',
|
|
4133
|
+
'self',
|
|
4134
|
+
'Self',
|
|
4135
|
+
'static',
|
|
4136
|
+
'struct',
|
|
4137
|
+
'super',
|
|
4138
|
+
'trait',
|
|
4139
|
+
'true',
|
|
4140
|
+
'type',
|
|
4141
|
+
'unsafe',
|
|
4142
|
+
'use',
|
|
4143
|
+
'where',
|
|
4144
|
+
'while',
|
|
4145
|
+
]),
|
|
4146
|
+
html: new Set([
|
|
4147
|
+
'html',
|
|
4148
|
+
'head',
|
|
4149
|
+
'body',
|
|
4150
|
+
'div',
|
|
4151
|
+
'span',
|
|
4152
|
+
'p',
|
|
4153
|
+
'a',
|
|
4154
|
+
'img',
|
|
4155
|
+
'video',
|
|
4156
|
+
'audio',
|
|
4157
|
+
'script',
|
|
4158
|
+
'style',
|
|
4159
|
+
'link',
|
|
4160
|
+
'meta',
|
|
4161
|
+
'form',
|
|
4162
|
+
'input',
|
|
4163
|
+
'button',
|
|
4164
|
+
'table',
|
|
4165
|
+
'tr',
|
|
4166
|
+
'td',
|
|
4167
|
+
'th',
|
|
4168
|
+
'ul',
|
|
4169
|
+
'ol',
|
|
4170
|
+
'li',
|
|
4171
|
+
'section',
|
|
4172
|
+
'article',
|
|
4173
|
+
'header',
|
|
4174
|
+
'footer',
|
|
4175
|
+
'nav',
|
|
4176
|
+
'main',
|
|
4177
|
+
]),
|
|
4178
|
+
bash: new Set([
|
|
4179
|
+
'if',
|
|
4180
|
+
'then',
|
|
4181
|
+
'else',
|
|
4182
|
+
'elif',
|
|
4183
|
+
'fi',
|
|
4184
|
+
'for',
|
|
4185
|
+
'while',
|
|
4186
|
+
'do',
|
|
4187
|
+
'done',
|
|
4188
|
+
'case',
|
|
4189
|
+
'esac',
|
|
4190
|
+
'function',
|
|
4191
|
+
'in',
|
|
4192
|
+
'select',
|
|
4193
|
+
'until',
|
|
4194
|
+
'break',
|
|
4195
|
+
'continue',
|
|
4196
|
+
'return',
|
|
4197
|
+
'export',
|
|
4198
|
+
'readonly',
|
|
4199
|
+
'local',
|
|
4200
|
+
'declare',
|
|
4201
|
+
]),
|
|
4202
|
+
markdown: new Set(['#', '##', '###', '####', '#####', '######']),
|
|
4203
|
+
};
|
|
4204
|
+
if (!lang || lang === 'txt' || lang === 'text' || lang === 'plaintext') {
|
|
4205
|
+
return {
|
|
4206
|
+
codeBlock: [
|
|
4207
|
+
{
|
|
4208
|
+
codeContent: code,
|
|
4209
|
+
highlightType: 0,
|
|
4210
|
+
},
|
|
4211
|
+
],
|
|
4212
|
+
unified_codeBlock: [
|
|
4213
|
+
{
|
|
4214
|
+
content: code,
|
|
4215
|
+
type: 'DEFAULT',
|
|
4216
|
+
},
|
|
4217
|
+
],
|
|
4218
|
+
};
|
|
4219
|
+
}
|
|
4220
|
+
const TYPE_MAP = {
|
|
4221
|
+
0: 'DEFAULT',
|
|
4222
|
+
1: 'KEYWORD',
|
|
4223
|
+
2: 'METHOD',
|
|
4224
|
+
3: 'STR',
|
|
4225
|
+
4: 'NUMBER',
|
|
4226
|
+
5: 'COMMENT',
|
|
4227
|
+
};
|
|
4228
|
+
const keywords = keywordsMap[lang.toLowerCase()] || new Set();
|
|
4229
|
+
const tokens = [];
|
|
4230
|
+
let i = 0;
|
|
4231
|
+
const push = (content, type) => {
|
|
4232
|
+
if (!content)
|
|
4233
|
+
return;
|
|
4234
|
+
const last = tokens[tokens.length - 1];
|
|
4235
|
+
if (last && last.highlightType === type) {
|
|
4236
|
+
last.codeContent += content;
|
|
4237
|
+
}
|
|
4238
|
+
else {
|
|
4239
|
+
tokens.push({
|
|
4240
|
+
codeContent: content,
|
|
4241
|
+
highlightType: type,
|
|
4242
|
+
});
|
|
4243
|
+
}
|
|
4244
|
+
};
|
|
4245
|
+
const isIdentifier = (char) => {
|
|
4246
|
+
switch (lang.toLowerCase()) {
|
|
4247
|
+
case 'css':
|
|
4248
|
+
return /[a-zA-Z0-9_$-]/.test(char);
|
|
4249
|
+
case 'html':
|
|
4250
|
+
return /[a-zA-Z0-9_$:-]/.test(char);
|
|
4251
|
+
default:
|
|
4252
|
+
return /[a-zA-Z0-9_$]/.test(char);
|
|
4253
|
+
}
|
|
4254
|
+
};
|
|
4255
|
+
while (i < code.length) {
|
|
4256
|
+
const c = code[i];
|
|
4257
|
+
if (/\s/.test(c)) {
|
|
4258
|
+
let s = i;
|
|
4259
|
+
while (i < code.length && /\s/.test(code[i])) {
|
|
4260
|
+
i++;
|
|
4261
|
+
}
|
|
4262
|
+
push(code.slice(s, i), 0);
|
|
4263
|
+
continue;
|
|
4264
|
+
}
|
|
4265
|
+
if ((c === '/' && code[i + 1] === '/') || (c === '#' && ['python', 'bash'].includes(lang))) {
|
|
4266
|
+
let s = i;
|
|
4267
|
+
while (i < code.length && code[i] !== '\n') {
|
|
4268
|
+
i++;
|
|
4269
|
+
}
|
|
4270
|
+
push(code.slice(s, i), 5);
|
|
4271
|
+
continue;
|
|
4272
|
+
}
|
|
4273
|
+
if (c === '"' || c === "'" || c === '`') {
|
|
4274
|
+
let s = i;
|
|
4275
|
+
const q = c;
|
|
4276
|
+
i++;
|
|
4277
|
+
while (i < code.length) {
|
|
4278
|
+
if (code[i] === '\\' && i + 1 < code.length) {
|
|
4279
|
+
i += 2;
|
|
4280
|
+
}
|
|
4281
|
+
else if (code[i] === q) {
|
|
4282
|
+
i++;
|
|
4283
|
+
break;
|
|
4284
|
+
}
|
|
4285
|
+
else {
|
|
4286
|
+
i++;
|
|
4287
|
+
}
|
|
4288
|
+
}
|
|
4289
|
+
push(code.slice(s, i), 3);
|
|
4290
|
+
continue;
|
|
4291
|
+
}
|
|
4292
|
+
if (/[0-9]/.test(c)) {
|
|
4293
|
+
let s = i;
|
|
4294
|
+
while (i < code.length && /[0-9._]/.test(code[i])) {
|
|
4295
|
+
i++;
|
|
4296
|
+
}
|
|
4297
|
+
push(code.slice(s, i), 4);
|
|
4298
|
+
continue;
|
|
4299
|
+
}
|
|
4300
|
+
if (/[a-zA-Z_$]/.test(c)) {
|
|
4301
|
+
let s = i;
|
|
4302
|
+
while (i < code.length && isIdentifier(code[i])) {
|
|
4303
|
+
i++;
|
|
4304
|
+
}
|
|
4305
|
+
const word = code.slice(s, i);
|
|
4306
|
+
let type = 0;
|
|
4307
|
+
if (keywords.has(word)) {
|
|
4308
|
+
type = 1;
|
|
4309
|
+
}
|
|
4310
|
+
else if (lang === 'css') {
|
|
4311
|
+
let j = i;
|
|
4312
|
+
while (j < code.length && /\s/.test(code[j])) {
|
|
4313
|
+
j++;
|
|
4314
|
+
}
|
|
4315
|
+
if (code[j] === ':') {
|
|
4316
|
+
type = 1;
|
|
4317
|
+
}
|
|
4318
|
+
}
|
|
4319
|
+
else if (lang === 'html') {
|
|
4320
|
+
let p = s - 1;
|
|
4321
|
+
while (p >= 0 && /\s/.test(code[p])) {
|
|
4322
|
+
p--;
|
|
4323
|
+
}
|
|
4324
|
+
if (code[p] === '<' || (code[p] === '/' && code[p - 1] === '<')) {
|
|
4325
|
+
type = 1;
|
|
4326
|
+
}
|
|
4327
|
+
}
|
|
4328
|
+
if (type === 0) {
|
|
4329
|
+
let j = i;
|
|
4330
|
+
while (j < code.length && /\s/.test(code[j])) {
|
|
4331
|
+
j++;
|
|
4332
|
+
}
|
|
4333
|
+
if (code[j] === '(') {
|
|
4334
|
+
type = 2;
|
|
4335
|
+
}
|
|
4336
|
+
}
|
|
4337
|
+
push(word, type);
|
|
4338
|
+
continue;
|
|
4339
|
+
}
|
|
4340
|
+
push(c, 0);
|
|
4341
|
+
i++;
|
|
4342
|
+
}
|
|
4343
|
+
return {
|
|
4344
|
+
codeBlock: tokens,
|
|
4345
|
+
unified_codeBlock: tokens.map((t) => ({
|
|
4346
|
+
content: t.codeContent,
|
|
4347
|
+
type: TYPE_MAP[t.highlightType],
|
|
4348
|
+
})),
|
|
4349
|
+
};
|
|
4350
|
+
}
|
|
4351
|
+
static toTableMetadata(arr, { hyperlink = true, citation = true, latex = true } = {}) {
|
|
4352
|
+
if (!Array.isArray(arr) || !arr.every((row) => Array.isArray(row) && row.every((cell) => typeof cell === 'string'))) {
|
|
4353
|
+
throw new TypeError('Table must be a nested array of strings');
|
|
4354
|
+
}
|
|
4355
|
+
const [header, ...rows] = arr;
|
|
4356
|
+
const maxLen = Math.max(header.length, ...rows.map((r) => r.length));
|
|
4357
|
+
const normalize = (r) => [...r, ...Array(maxLen - r.length).fill('')];
|
|
4358
|
+
const unified_rows = [
|
|
4359
|
+
{
|
|
4360
|
+
is_header: true,
|
|
4361
|
+
cells: normalize(header),
|
|
4362
|
+
},
|
|
4363
|
+
...rows.map((r) => ({
|
|
4364
|
+
is_header: false,
|
|
4365
|
+
cells: normalize(r),
|
|
4366
|
+
})),
|
|
4367
|
+
].map((row) => {
|
|
4368
|
+
const markdown_cells = row.cells.map((cell) => {
|
|
4369
|
+
const extracted = extractIE(cell, { hyperlink, citation, latex });
|
|
4370
|
+
return {
|
|
4371
|
+
text: extracted.text,
|
|
4372
|
+
...(extracted.inline_entities.length ? { inline_entities: extracted.inline_entities } : {}),
|
|
4373
|
+
};
|
|
4374
|
+
});
|
|
4375
|
+
return {
|
|
4376
|
+
...row,
|
|
4377
|
+
...(markdown_cells.some((c) => c.inline_entities?.length) ? { markdown_cells } : {}),
|
|
4378
|
+
};
|
|
4379
|
+
});
|
|
4380
|
+
const rowsMeta = unified_rows.map((r) => ({
|
|
4381
|
+
items: r.cells,
|
|
4382
|
+
...(r.is_header ? { isHeading: true } : {}),
|
|
4383
|
+
}));
|
|
4384
|
+
return {
|
|
4385
|
+
title: '',
|
|
4386
|
+
rows: rowsMeta,
|
|
4387
|
+
unified_rows,
|
|
4388
|
+
};
|
|
4389
|
+
}
|
|
4390
|
+
static newLayout(name, data, extra = {}) {
|
|
4391
|
+
return {
|
|
4392
|
+
...extra,
|
|
4393
|
+
view_model: {
|
|
4394
|
+
[Array.isArray(data) ? 'primitives' : 'primitive']: data,
|
|
4395
|
+
__typename: `GenAI${name}LayoutViewModel`,
|
|
4396
|
+
},
|
|
4397
|
+
};
|
|
4398
|
+
}
|
|
4399
|
+
text(t) { return this.addText(t); }
|
|
4400
|
+
}
|
|
4401
|
+
exports.AIRich = AIRich;
|
|
4402
|
+
class ORich extends AIRich {
|
|
4403
|
+
}
|
|
4404
|
+
exports.ORich = ORich;
|
|
4405
|
+
|
|
4406
|
+
return module.exports;
|
|
4407
|
+
})();
|
|
4408
|
+
for (const key of Object.keys(__kageModule102)) {
|
|
4409
|
+
if (key !== '__esModule' && !Object.prototype.hasOwnProperty.call(exports,key)) Object.defineProperty(exports,key,{enumerable:true,configurable:true,get:()=>__kageModule102[key]});
|
|
4410
|
+
}
|