@risacakep/risayap 0.0.0-stage → 7.0.0-rc25.7
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/CLARISSA-BAILEYS-RC25.md +168 -0
- package/LICENSE +21 -0
- package/MIGRASI-CLARISSA-BAILEYS-RC25.md +102 -0
- package/README.md +1399 -2
- package/THIRD_PARTY_NOTICES.md +49 -0
- package/WAProto/GenerateStatics.sh +3 -0
- package/WAProto/WAProto.proto +5526 -0
- package/WAProto/fix-imports.js +85 -0
- package/WAProto/index.d.ts +14130 -0
- package/WAProto/index.js +97990 -0
- package/engine-requirements.js +10 -0
- package/lib/Defaults/index.d.ts +75 -0
- package/lib/Defaults/index.d.ts.map +1 -0
- package/lib/Defaults/index.js +131 -0
- package/lib/Defaults/index.js.map +1 -0
- package/lib/Extensions/advanced.d.ts +247 -0
- package/lib/Extensions/advanced.d.ts.map +1 -0
- package/lib/Extensions/advanced.js +821 -0
- package/lib/Extensions/advanced.js.map +1 -0
- package/lib/Extensions/experimental.d.ts +378 -0
- package/lib/Extensions/experimental.d.ts.map +1 -0
- package/lib/Extensions/experimental.js +628 -0
- package/lib/Extensions/experimental.js.map +1 -0
- package/lib/MessageBuilder/index.d.ts +210 -0
- package/lib/MessageBuilder/index.d.ts.map +1 -0
- package/lib/MessageBuilder/index.js +676 -0
- package/lib/MessageBuilder/index.js.map +1 -0
- package/lib/Signal/Group/ciphertext-message.d.ts +10 -0
- package/lib/Signal/Group/ciphertext-message.d.ts.map +1 -0
- package/lib/Signal/Group/ciphertext-message.js +12 -0
- package/lib/Signal/Group/ciphertext-message.js.map +1 -0
- package/lib/Signal/Group/group-session-builder.d.ts +15 -0
- package/lib/Signal/Group/group-session-builder.d.ts.map +1 -0
- package/lib/Signal/Group/group-session-builder.js +30 -0
- package/lib/Signal/Group/group-session-builder.js.map +1 -0
- package/lib/Signal/Group/group_cipher.d.ts +17 -0
- package/lib/Signal/Group/group_cipher.d.ts.map +1 -0
- package/lib/Signal/Group/group_cipher.js +82 -0
- package/lib/Signal/Group/group_cipher.js.map +1 -0
- package/lib/Signal/Group/index.d.ts +12 -0
- package/lib/Signal/Group/index.d.ts.map +1 -0
- package/lib/Signal/Group/index.js +12 -0
- package/lib/Signal/Group/index.js.map +1 -0
- package/lib/Signal/Group/keyhelper.d.ts +11 -0
- package/lib/Signal/Group/keyhelper.d.ts.map +1 -0
- package/lib/Signal/Group/keyhelper.js +18 -0
- package/lib/Signal/Group/keyhelper.js.map +1 -0
- package/lib/Signal/Group/sender-chain-key.d.ts +14 -0
- package/lib/Signal/Group/sender-chain-key.d.ts.map +1 -0
- package/lib/Signal/Group/sender-chain-key.js +26 -0
- package/lib/Signal/Group/sender-chain-key.js.map +1 -0
- package/lib/Signal/Group/sender-key-distribution-message.d.ts +17 -0
- package/lib/Signal/Group/sender-key-distribution-message.d.ts.map +1 -0
- package/lib/Signal/Group/sender-key-distribution-message.js +63 -0
- package/lib/Signal/Group/sender-key-distribution-message.js.map +1 -0
- package/lib/Signal/Group/sender-key-message.d.ts +19 -0
- package/lib/Signal/Group/sender-key-message.d.ts.map +1 -0
- package/lib/Signal/Group/sender-key-message.js +66 -0
- package/lib/Signal/Group/sender-key-message.js.map +1 -0
- package/lib/Signal/Group/sender-key-name.d.ts +18 -0
- package/lib/Signal/Group/sender-key-name.d.ts.map +1 -0
- package/lib/Signal/Group/sender-key-name.js +48 -0
- package/lib/Signal/Group/sender-key-name.js.map +1 -0
- package/lib/Signal/Group/sender-key-record.d.ts +31 -0
- package/lib/Signal/Group/sender-key-record.d.ts.map +1 -0
- package/lib/Signal/Group/sender-key-record.js +41 -0
- package/lib/Signal/Group/sender-key-record.js.map +1 -0
- package/lib/Signal/Group/sender-key-state.d.ts +39 -0
- package/lib/Signal/Group/sender-key-state.d.ts.map +1 -0
- package/lib/Signal/Group/sender-key-state.js +84 -0
- package/lib/Signal/Group/sender-key-state.js.map +1 -0
- package/lib/Signal/Group/sender-message-key.d.ts +12 -0
- package/lib/Signal/Group/sender-message-key.d.ts.map +1 -0
- package/lib/Signal/Group/sender-message-key.js +26 -0
- package/lib/Signal/Group/sender-message-key.js.map +1 -0
- package/lib/Signal/libsignal.d.ts +5 -0
- package/lib/Signal/libsignal.d.ts.map +1 -0
- package/lib/Signal/libsignal.js +431 -0
- package/lib/Signal/libsignal.js.map +1 -0
- package/lib/Signal/lid-mapping.d.ts +23 -0
- package/lib/Signal/lid-mapping.d.ts.map +1 -0
- package/lib/Signal/lid-mapping.js +277 -0
- package/lib/Signal/lid-mapping.js.map +1 -0
- package/lib/Socket/Client/index.d.ts +3 -0
- package/lib/Socket/Client/index.d.ts.map +1 -0
- package/lib/Socket/Client/index.js +3 -0
- package/lib/Socket/Client/index.js.map +1 -0
- package/lib/Socket/Client/types.d.ts +16 -0
- package/lib/Socket/Client/types.d.ts.map +1 -0
- package/lib/Socket/Client/types.js +11 -0
- package/lib/Socket/Client/types.js.map +1 -0
- package/lib/Socket/Client/websocket.d.ts +13 -0
- package/lib/Socket/Client/websocket.d.ts.map +1 -0
- package/lib/Socket/Client/websocket.js +54 -0
- package/lib/Socket/Client/websocket.js.map +1 -0
- package/lib/Socket/business.d.ts +219 -0
- package/lib/Socket/business.d.ts.map +1 -0
- package/lib/Socket/business.js +379 -0
- package/lib/Socket/business.js.map +1 -0
- package/lib/Socket/chats.d.ts +127 -0
- package/lib/Socket/chats.d.ts.map +1 -0
- package/lib/Socket/chats.js +1198 -0
- package/lib/Socket/chats.js.map +1 -0
- package/lib/Socket/communities.d.ts +275 -0
- package/lib/Socket/communities.d.ts.map +1 -0
- package/lib/Socket/communities.js +431 -0
- package/lib/Socket/communities.js.map +1 -0
- package/lib/Socket/groups.d.ts +163 -0
- package/lib/Socket/groups.d.ts.map +1 -0
- package/lib/Socket/groups.js +347 -0
- package/lib/Socket/groups.js.map +1 -0
- package/lib/Socket/index.d.ts +262 -0
- package/lib/Socket/index.d.ts.map +1 -0
- package/lib/Socket/index.js +33 -0
- package/lib/Socket/index.js.map +1 -0
- package/lib/Socket/messages-recv.d.ts +205 -0
- package/lib/Socket/messages-recv.d.ts.map +1 -0
- package/lib/Socket/messages-recv.js +1772 -0
- package/lib/Socket/messages-recv.js.map +1 -0
- package/lib/Socket/messages-send.d.ts +201 -0
- package/lib/Socket/messages-send.d.ts.map +1 -0
- package/lib/Socket/messages-send.js +1205 -0
- package/lib/Socket/messages-send.js.map +1 -0
- package/lib/Socket/mex.d.ts +3 -0
- package/lib/Socket/mex.d.ts.map +1 -0
- package/lib/Socket/mex.js +42 -0
- package/lib/Socket/mex.js.map +1 -0
- package/lib/Socket/newsletter.d.ts +172 -0
- package/lib/Socket/newsletter.d.ts.map +1 -0
- package/lib/Socket/newsletter.js +181 -0
- package/lib/Socket/newsletter.js.map +1 -0
- package/lib/Socket/socket.d.ts +59 -0
- package/lib/Socket/socket.d.ts.map +1 -0
- package/lib/Socket/socket.js +1015 -0
- package/lib/Socket/socket.js.map +1 -0
- package/lib/Types/Auth.d.ts +117 -0
- package/lib/Types/Auth.d.ts.map +1 -0
- package/lib/Types/Auth.js +2 -0
- package/lib/Types/Auth.js.map +1 -0
- package/lib/Types/Bussines.d.ts +25 -0
- package/lib/Types/Bussines.d.ts.map +1 -0
- package/lib/Types/Bussines.js +2 -0
- package/lib/Types/Bussines.js.map +1 -0
- package/lib/Types/Call.d.ts +15 -0
- package/lib/Types/Call.d.ts.map +1 -0
- package/lib/Types/Call.js +2 -0
- package/lib/Types/Call.js.map +1 -0
- package/lib/Types/Chat.d.ts +124 -0
- package/lib/Types/Chat.d.ts.map +1 -0
- package/lib/Types/Chat.js +8 -0
- package/lib/Types/Chat.js.map +1 -0
- package/lib/Types/Contact.d.ts +26 -0
- package/lib/Types/Contact.d.ts.map +1 -0
- package/lib/Types/Contact.js +2 -0
- package/lib/Types/Contact.js.map +1 -0
- package/lib/Types/Events.d.ts +256 -0
- package/lib/Types/Events.d.ts.map +1 -0
- package/lib/Types/Events.js +2 -0
- package/lib/Types/Events.js.map +1 -0
- package/lib/Types/GroupMetadata.d.ts +71 -0
- package/lib/Types/GroupMetadata.d.ts.map +1 -0
- package/lib/Types/GroupMetadata.js +2 -0
- package/lib/Types/GroupMetadata.js.map +1 -0
- package/lib/Types/Label.d.ts +47 -0
- package/lib/Types/Label.d.ts.map +1 -0
- package/lib/Types/Label.js +25 -0
- package/lib/Types/Label.js.map +1 -0
- package/lib/Types/LabelAssociation.d.ts +30 -0
- package/lib/Types/LabelAssociation.d.ts.map +1 -0
- package/lib/Types/LabelAssociation.js +7 -0
- package/lib/Types/LabelAssociation.js.map +1 -0
- package/lib/Types/Message.d.ts +331 -0
- package/lib/Types/Message.d.ts.map +1 -0
- package/lib/Types/Message.js +11 -0
- package/lib/Types/Message.js.map +1 -0
- package/lib/Types/Mex.d.ts +141 -0
- package/lib/Types/Mex.d.ts.map +1 -0
- package/lib/Types/Mex.js +37 -0
- package/lib/Types/Mex.js.map +1 -0
- package/lib/Types/Product.d.ts +79 -0
- package/lib/Types/Product.d.ts.map +1 -0
- package/lib/Types/Product.js +2 -0
- package/lib/Types/Product.js.map +1 -0
- package/lib/Types/Signal.d.ts +87 -0
- package/lib/Types/Signal.d.ts.map +1 -0
- package/lib/Types/Signal.js +2 -0
- package/lib/Types/Signal.js.map +1 -0
- package/lib/Types/Socket.d.ts +140 -0
- package/lib/Types/Socket.d.ts.map +1 -0
- package/lib/Types/Socket.js +3 -0
- package/lib/Types/Socket.js.map +1 -0
- package/lib/Types/State.d.ts +97 -0
- package/lib/Types/State.d.ts.map +1 -0
- package/lib/Types/State.js +56 -0
- package/lib/Types/State.js.map +1 -0
- package/lib/Types/USync.d.ts +26 -0
- package/lib/Types/USync.d.ts.map +1 -0
- package/lib/Types/USync.js +2 -0
- package/lib/Types/USync.js.map +1 -0
- package/lib/Types/index.d.ts +66 -0
- package/lib/Types/index.d.ts.map +1 -0
- package/lib/Types/index.js +26 -0
- package/lib/Types/index.js.map +1 -0
- package/lib/Utils/auth-utils.d.ts +24 -0
- package/lib/Utils/auth-utils.d.ts.map +1 -0
- package/lib/Utils/auth-utils.js +302 -0
- package/lib/Utils/auth-utils.js.map +1 -0
- package/lib/Utils/browser-utils.d.ts +4 -0
- package/lib/Utils/browser-utils.d.ts.map +1 -0
- package/lib/Utils/browser-utils.js +29 -0
- package/lib/Utils/browser-utils.js.map +1 -0
- package/lib/Utils/business.d.ts +23 -0
- package/lib/Utils/business.d.ts.map +1 -0
- package/lib/Utils/business.js +231 -0
- package/lib/Utils/business.js.map +1 -0
- package/lib/Utils/chat-utils.d.ts +100 -0
- package/lib/Utils/chat-utils.d.ts.map +1 -0
- package/lib/Utils/chat-utils.js +872 -0
- package/lib/Utils/chat-utils.js.map +1 -0
- package/lib/Utils/companion-reg-client-utils.d.ts +50 -0
- package/lib/Utils/companion-reg-client-utils.d.ts.map +1 -0
- package/lib/Utils/companion-reg-client-utils.js +90 -0
- package/lib/Utils/companion-reg-client-utils.js.map +1 -0
- package/lib/Utils/crypto.d.ts +37 -0
- package/lib/Utils/crypto.d.ts.map +1 -0
- package/lib/Utils/crypto.js +118 -0
- package/lib/Utils/crypto.js.map +1 -0
- package/lib/Utils/decode-wa-message.d.ts +66 -0
- package/lib/Utils/decode-wa-message.d.ts.map +1 -0
- package/lib/Utils/decode-wa-message.js +317 -0
- package/lib/Utils/decode-wa-message.js.map +1 -0
- package/lib/Utils/event-buffer.d.ts +36 -0
- package/lib/Utils/event-buffer.d.ts.map +1 -0
- package/lib/Utils/event-buffer.js +622 -0
- package/lib/Utils/event-buffer.js.map +1 -0
- package/lib/Utils/generics.d.ts +92 -0
- package/lib/Utils/generics.d.ts.map +1 -0
- package/lib/Utils/generics.js +393 -0
- package/lib/Utils/generics.js.map +1 -0
- package/lib/Utils/history.d.ts +24 -0
- package/lib/Utils/history.d.ts.map +1 -0
- package/lib/Utils/history.js +134 -0
- package/lib/Utils/history.js.map +1 -0
- package/lib/Utils/identity-change-handler.d.ts +44 -0
- package/lib/Utils/identity-change-handler.d.ts.map +1 -0
- package/lib/Utils/identity-change-handler.js +50 -0
- package/lib/Utils/identity-change-handler.js.map +1 -0
- package/lib/Utils/index.d.ts +22 -0
- package/lib/Utils/index.d.ts.map +1 -0
- package/lib/Utils/index.js +22 -0
- package/lib/Utils/index.js.map +1 -0
- package/lib/Utils/link-preview.d.ts +21 -0
- package/lib/Utils/link-preview.d.ts.map +1 -0
- package/lib/Utils/link-preview.js +85 -0
- package/lib/Utils/link-preview.js.map +1 -0
- package/lib/Utils/logger.d.ts +12 -0
- package/lib/Utils/logger.d.ts.map +1 -0
- package/lib/Utils/logger.js +3 -0
- package/lib/Utils/logger.js.map +1 -0
- package/lib/Utils/lt-hash.d.ts +8 -0
- package/lib/Utils/lt-hash.d.ts.map +1 -0
- package/lib/Utils/lt-hash.js +8 -0
- package/lib/Utils/lt-hash.js.map +1 -0
- package/lib/Utils/make-mutex.d.ts +9 -0
- package/lib/Utils/make-mutex.d.ts.map +1 -0
- package/lib/Utils/make-mutex.js +33 -0
- package/lib/Utils/make-mutex.js.map +1 -0
- package/lib/Utils/message-retry-manager.d.ts +115 -0
- package/lib/Utils/message-retry-manager.d.ts.map +1 -0
- package/lib/Utils/message-retry-manager.js +265 -0
- package/lib/Utils/message-retry-manager.js.map +1 -0
- package/lib/Utils/messages-media.d.ts +135 -0
- package/lib/Utils/messages-media.d.ts.map +1 -0
- package/lib/Utils/messages-media.js +791 -0
- package/lib/Utils/messages-media.js.map +1 -0
- package/lib/Utils/messages.d.ts +91 -0
- package/lib/Utils/messages.d.ts.map +1 -0
- package/lib/Utils/messages.js +910 -0
- package/lib/Utils/messages.js.map +1 -0
- package/lib/Utils/noise-handler.d.ts +20 -0
- package/lib/Utils/noise-handler.d.ts.map +1 -0
- package/lib/Utils/noise-handler.js +201 -0
- package/lib/Utils/noise-handler.js.map +1 -0
- package/lib/Utils/offline-node-processor.d.ts +17 -0
- package/lib/Utils/offline-node-processor.d.ts.map +1 -0
- package/lib/Utils/offline-node-processor.js +40 -0
- package/lib/Utils/offline-node-processor.js.map +1 -0
- package/lib/Utils/pre-key-manager.d.ts +28 -0
- package/lib/Utils/pre-key-manager.d.ts.map +1 -0
- package/lib/Utils/pre-key-manager.js +106 -0
- package/lib/Utils/pre-key-manager.js.map +1 -0
- package/lib/Utils/process-message.d.ts +60 -0
- package/lib/Utils/process-message.d.ts.map +1 -0
- package/lib/Utils/process-message.js +630 -0
- package/lib/Utils/process-message.js.map +1 -0
- package/lib/Utils/reporting-utils.d.ts +11 -0
- package/lib/Utils/reporting-utils.d.ts.map +1 -0
- package/lib/Utils/reporting-utils.js +258 -0
- package/lib/Utils/reporting-utils.js.map +1 -0
- package/lib/Utils/signal.d.ts +47 -0
- package/lib/Utils/signal.d.ts.map +1 -0
- package/lib/Utils/signal.js +201 -0
- package/lib/Utils/signal.js.map +1 -0
- package/lib/Utils/stanza-ack.d.ts +11 -0
- package/lib/Utils/stanza-ack.d.ts.map +1 -0
- package/lib/Utils/stanza-ack.js +38 -0
- package/lib/Utils/stanza-ack.js.map +1 -0
- package/lib/Utils/sync-action-utils.d.ts +19 -0
- package/lib/Utils/sync-action-utils.d.ts.map +1 -0
- package/lib/Utils/sync-action-utils.js +49 -0
- package/lib/Utils/sync-action-utils.js.map +1 -0
- package/lib/Utils/tc-token-utils.d.ts +37 -0
- package/lib/Utils/tc-token-utils.d.ts.map +1 -0
- package/lib/Utils/tc-token-utils.js +164 -0
- package/lib/Utils/tc-token-utils.js.map +1 -0
- package/lib/Utils/terminal-welcome.d.ts +11 -0
- package/lib/Utils/terminal-welcome.d.ts.map +1 -0
- package/lib/Utils/terminal-welcome.js +215 -0
- package/lib/Utils/terminal-welcome.js.map +1 -0
- package/lib/Utils/use-multi-file-auth-state.d.ts +13 -0
- package/lib/Utils/use-multi-file-auth-state.d.ts.map +1 -0
- package/lib/Utils/use-multi-file-auth-state.js +121 -0
- package/lib/Utils/use-multi-file-auth-state.js.map +1 -0
- package/lib/Utils/validate-connection.d.ts +11 -0
- package/lib/Utils/validate-connection.d.ts.map +1 -0
- package/lib/Utils/validate-connection.js +210 -0
- package/lib/Utils/validate-connection.js.map +1 -0
- package/lib/WABinary/constants.d.ts +28 -0
- package/lib/WABinary/constants.d.ts.map +1 -0
- package/lib/WABinary/constants.js +1301 -0
- package/lib/WABinary/constants.js.map +1 -0
- package/lib/WABinary/decode.d.ts +7 -0
- package/lib/WABinary/decode.d.ts.map +1 -0
- package/lib/WABinary/decode.js +262 -0
- package/lib/WABinary/decode.js.map +1 -0
- package/lib/WABinary/encode.d.ts +3 -0
- package/lib/WABinary/encode.d.ts.map +1 -0
- package/lib/WABinary/encode.js +220 -0
- package/lib/WABinary/encode.js.map +1 -0
- package/lib/WABinary/generic-utils.d.ts +31 -0
- package/lib/WABinary/generic-utils.d.ts.map +1 -0
- package/lib/WABinary/generic-utils.js +224 -0
- package/lib/WABinary/generic-utils.js.map +1 -0
- package/lib/WABinary/index.d.ts +6 -0
- package/lib/WABinary/index.d.ts.map +1 -0
- package/lib/WABinary/index.js +6 -0
- package/lib/WABinary/index.js.map +1 -0
- package/lib/WABinary/jid-utils.d.ts +48 -0
- package/lib/WABinary/jid-utils.d.ts.map +1 -0
- package/lib/WABinary/jid-utils.js +96 -0
- package/lib/WABinary/jid-utils.js.map +1 -0
- package/lib/WABinary/types.d.ts +19 -0
- package/lib/WABinary/types.d.ts.map +1 -0
- package/lib/WABinary/types.js +2 -0
- package/lib/WABinary/types.js.map +1 -0
- package/lib/WAM/BinaryInfo.d.ts +9 -0
- package/lib/WAM/BinaryInfo.d.ts.map +1 -0
- package/lib/WAM/BinaryInfo.js +10 -0
- package/lib/WAM/BinaryInfo.js.map +1 -0
- package/lib/WAM/constants.d.ts +40 -0
- package/lib/WAM/constants.d.ts.map +1 -0
- package/lib/WAM/constants.js +22853 -0
- package/lib/WAM/constants.js.map +1 -0
- package/lib/WAM/encode.d.ts +3 -0
- package/lib/WAM/encode.d.ts.map +1 -0
- package/lib/WAM/encode.js +150 -0
- package/lib/WAM/encode.js.map +1 -0
- package/lib/WAM/index.d.ts +4 -0
- package/lib/WAM/index.d.ts.map +1 -0
- package/lib/WAM/index.js +4 -0
- package/lib/WAM/index.js.map +1 -0
- package/lib/WAUSync/Protocols/USyncContactProtocol.d.ts +10 -0
- package/lib/WAUSync/Protocols/USyncContactProtocol.d.ts.map +1 -0
- package/lib/WAUSync/Protocols/USyncContactProtocol.js +52 -0
- package/lib/WAUSync/Protocols/USyncContactProtocol.js.map +1 -0
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.d.ts +23 -0
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.d.ts.map +1 -0
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +54 -0
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.js.map +1 -0
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.d.ts +13 -0
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.d.ts.map +1 -0
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +27 -0
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js.map +1 -0
- package/lib/WAUSync/Protocols/USyncStatusProtocol.d.ts +13 -0
- package/lib/WAUSync/Protocols/USyncStatusProtocol.d.ts.map +1 -0
- package/lib/WAUSync/Protocols/USyncStatusProtocol.js +38 -0
- package/lib/WAUSync/Protocols/USyncStatusProtocol.js.map +1 -0
- package/lib/WAUSync/Protocols/USyncUsernameProtocol.d.ts +10 -0
- package/lib/WAUSync/Protocols/USyncUsernameProtocol.d.ts.map +1 -0
- package/lib/WAUSync/Protocols/USyncUsernameProtocol.js +25 -0
- package/lib/WAUSync/Protocols/USyncUsernameProtocol.js.map +1 -0
- package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.d.ts +26 -0
- package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.d.ts.map +1 -0
- package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +51 -0
- package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js.map +1 -0
- package/lib/WAUSync/Protocols/UsyncLIDProtocol.d.ts +10 -0
- package/lib/WAUSync/Protocols/UsyncLIDProtocol.d.ts.map +1 -0
- package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +29 -0
- package/lib/WAUSync/Protocols/UsyncLIDProtocol.js.map +1 -0
- package/lib/WAUSync/Protocols/index.d.ts +6 -0
- package/lib/WAUSync/Protocols/index.d.ts.map +1 -0
- package/lib/WAUSync/Protocols/index.js +6 -0
- package/lib/WAUSync/Protocols/index.js.map +1 -0
- package/lib/WAUSync/USyncQuery.d.ts +30 -0
- package/lib/WAUSync/USyncQuery.d.ts.map +1 -0
- package/lib/WAUSync/USyncQuery.js +98 -0
- package/lib/WAUSync/USyncQuery.js.map +1 -0
- package/lib/WAUSync/USyncUser.d.ts +17 -0
- package/lib/WAUSync/USyncUser.d.ts.map +1 -0
- package/lib/WAUSync/USyncUser.js +31 -0
- package/lib/WAUSync/USyncUser.js.map +1 -0
- package/lib/WAUSync/index.d.ts +4 -0
- package/lib/WAUSync/index.d.ts.map +1 -0
- package/lib/WAUSync/index.js +4 -0
- package/lib/WAUSync/index.js.map +1 -0
- package/lib/extensions/clarissa-MessageBuilder.js +3257 -0
- package/lib/extensions/clarissa-ai-rich.js +658 -0
- package/lib/extensions/clarissa-carousel.js +275 -0
- package/lib/index.d.ts +15 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +16 -0
- package/lib/index.js.map +1 -0
- package/package.json +147 -4
|
@@ -0,0 +1,3257 @@
|
|
|
1
|
+
/*
|
|
2
|
+
⌬═════════════════════⌬
|
|
3
|
+
┇
|
|
4
|
+
┇ ▬▬▬▬▬▬▬▬ ஐஐ ▬▬▬▬▬▬▬▬
|
|
5
|
+
┇ ----- 🥳 𝗪𝗘𝗟𝗖𝗢𝗠𝗘 𝗕𝗥𝗢𝗞 🙌 -----
|
|
6
|
+
┇ ▬▬▬▬▬▬▬▬ ஐஐ ▬▬▬▬▬▬▬▬
|
|
7
|
+
┇
|
|
8
|
+
┇
|
|
9
|
+
┇ █▀█ █ █▀█ █▀█ ▄▀█ █
|
|
10
|
+
┇ █ █ █ █▀▄ █▄█ █▀█ █
|
|
11
|
+
┇ ▀█ --𝗤--𝗜--𝗥--𝗢------𝗔--𝗜----
|
|
12
|
+
┇
|
|
13
|
+
┇ ❏ ───────────────── ❏
|
|
14
|
+
┇ ✧・゚ 💬 𝗜𝗡𝗙𝗢𝗥𝗠𝗔𝗧𝗜𝗢𝗡 💬 ゚・✧
|
|
15
|
+
┇ ❏ ───────────────── ❏
|
|
16
|
+
┇ › ୨୧ Developer : Clarissa
|
|
17
|
+
┇ › ୨୧ Website : clarissa.my.id
|
|
18
|
+
┇ › ୨୧ WhatsApp : wa.me/556184127506
|
|
19
|
+
┇
|
|
20
|
+
┇ ✦ ───────────────── ✦
|
|
21
|
+
┇ ✧・゚ ⛔ 𝗗𝗜𝗟𝗔𝗥𝗔𝗡𝗚 𝗞𝗘𝗥𝗔𝗦 ⛔ ゚・✧
|
|
22
|
+
┇ ✦ ───────────────── ✦
|
|
23
|
+
┇ › ✎ Dilarang Hapus Creadit SC
|
|
24
|
+
┇ › ✎ Dilarang Jual-Beli Script Ini
|
|
25
|
+
┇ › ✎ Dilarang Ngaku Sebagai Dev Clarissa
|
|
26
|
+
┇ › ✎ Jangan Ganti Baileys ya Brok!!
|
|
27
|
+
┇
|
|
28
|
+
┇ ♡ ───────────────── ♡
|
|
29
|
+
┇ ✧・゚ 🌸 𝗜𝗭𝗜𝗡 & 𝗦𝗬𝗔𝗥𝗔𝗧 🌸 ゚・✧
|
|
30
|
+
┇ ♡ ───────────────── ♡
|
|
31
|
+
┇ › ○ Bebas Kembangin dan Recode SC
|
|
32
|
+
┇ › ○ Bebas Modif dan Tambah Fitur
|
|
33
|
+
┇ › ○ Bebas Share Ke Teman / Group
|
|
34
|
+
┇ › ○ Wajib Izin & Cantumin Creadit
|
|
35
|
+
┇ › ○ Hargai Karya Orang Anjing!!!!
|
|
36
|
+
┇
|
|
37
|
+
┇ ⚠ ───────────────── ⚠
|
|
38
|
+
┇ ✧・゚ 📢 𝗖𝗔𝗧𝗔𝗧𝗔𝗡 & 𝗟𝗔𝗣𝗢𝗥𝗔𝗡 📢 ゚・✧
|
|
39
|
+
┇ ⚠ ───────────────── ⚠
|
|
40
|
+
┇ › Jika menemukan yang melanggar
|
|
41
|
+
┇ › aturan di atas, segera laporkan
|
|
42
|
+
┇ › via WhatsApp yang tertera
|
|
43
|
+
┇
|
|
44
|
+
┇ › sertakan bukti Screenshot / Link
|
|
45
|
+
┇ › agar cepat kami tindak lanjuti!!
|
|
46
|
+
┇ ╺╺╺╺╺╺╺╺╺╺╺╺╺╺╺✧˖°.
|
|
47
|
+
┇
|
|
48
|
+
┇ ║▌│█║▌│ █║▌│█│║▌
|
|
49
|
+
┇ ♡ 𝗖𝗟𝗔𝗥𝗜𝗦𝗦𝗔 - 𝗔𝗜 | 𝟮𝟬𝟮𝟲 ♡
|
|
50
|
+
┇
|
|
51
|
+
⌬═════════════════════⌬
|
|
52
|
+
*/
|
|
53
|
+
|
|
54
|
+
'use strict';
|
|
55
|
+
|
|
56
|
+
const VERSION = '4.7';
|
|
57
|
+
|
|
58
|
+
import { generateWAMessageFromContent, prepareWAMessageMedia, generateMessageIDV2 } from '@risacakep/risayap';
|
|
59
|
+
import crypto from 'crypto';
|
|
60
|
+
import sharp from 'sharp';
|
|
61
|
+
import ffmpeg from 'fluent-ffmpeg';
|
|
62
|
+
import { PassThrough, Readable } from 'stream';
|
|
63
|
+
|
|
64
|
+
function extractIE(text, { extract = true, hyperlink = true, citation = true, latex = true } = {}) {
|
|
65
|
+
if (!extract) {
|
|
66
|
+
return {
|
|
67
|
+
text,
|
|
68
|
+
ie: [],
|
|
69
|
+
inline_entities: [],
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
const createIE = (type, ie) => {
|
|
74
|
+
if (type == 'hyperlink') {
|
|
75
|
+
return {
|
|
76
|
+
key: ie.key,
|
|
77
|
+
metadata: {
|
|
78
|
+
display_name: ie.text,
|
|
79
|
+
is_trusted: ie.is_trusted,
|
|
80
|
+
url: ie.url,
|
|
81
|
+
__typename: 'GenAIInlineLinkItem',
|
|
82
|
+
},
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
if (type == 'citation') {
|
|
87
|
+
return {
|
|
88
|
+
key: ie.key,
|
|
89
|
+
metadata: {
|
|
90
|
+
reference_id: ie.reference_id,
|
|
91
|
+
reference_url: ie.url,
|
|
92
|
+
reference_title: ie.url,
|
|
93
|
+
reference_display_name: ie.url,
|
|
94
|
+
sources: [],
|
|
95
|
+
__typename: 'GenAISearchCitationItem',
|
|
96
|
+
},
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
if (type == 'latex') {
|
|
101
|
+
return {
|
|
102
|
+
key: ie.key,
|
|
103
|
+
metadata: {
|
|
104
|
+
latex_expression: ie.text,
|
|
105
|
+
latex_image: {
|
|
106
|
+
url: ie.url,
|
|
107
|
+
width: Number(ie.width) || 100,
|
|
108
|
+
height: Number(ie.height) || 100,
|
|
109
|
+
},
|
|
110
|
+
font_height: Number(ie.font_height) || 83.333333333333,
|
|
111
|
+
padding: Number(ie.padding) || 15,
|
|
112
|
+
__typename: 'GenAILatexItem',
|
|
113
|
+
},
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
let ie = [];
|
|
119
|
+
let inline_entities = [];
|
|
120
|
+
let result = '';
|
|
121
|
+
let last = 0;
|
|
122
|
+
let citation_index = 1;
|
|
123
|
+
let hyperlink_index = 0;
|
|
124
|
+
let latex_index = 0;
|
|
125
|
+
let stack = [];
|
|
126
|
+
|
|
127
|
+
for (let i = 0; i < text.length; i++) {
|
|
128
|
+
if (text[i] == '[' && text[i - 1] != '\\') {
|
|
129
|
+
stack.push(i);
|
|
130
|
+
} else if (text[i] == ']' && (text[i + 1] == '(' || text[i + 1] == '<')) {
|
|
131
|
+
let start = stack.pop();
|
|
132
|
+
|
|
133
|
+
if (start == null) continue;
|
|
134
|
+
|
|
135
|
+
let open = text[i + 1];
|
|
136
|
+
let close = open == '(' ? ')' : '>';
|
|
137
|
+
let type = open == '(' ? 'link' : 'latex';
|
|
138
|
+
let end = i + 2;
|
|
139
|
+
let depth = 1;
|
|
140
|
+
|
|
141
|
+
while (end < text.length && depth) {
|
|
142
|
+
if (text[end] == open && text[end - 1] != '\\') depth++;
|
|
143
|
+
else if (text[end] == close && text[end - 1] != '\\') depth--;
|
|
144
|
+
end++;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
if (depth) continue;
|
|
148
|
+
|
|
149
|
+
let raw = text.slice(start + 1, i).trim();
|
|
150
|
+
let url = text.slice(i + 2, end - 1).trim();
|
|
151
|
+
|
|
152
|
+
let key;
|
|
153
|
+
let tag;
|
|
154
|
+
let data;
|
|
155
|
+
|
|
156
|
+
if (type == 'latex') {
|
|
157
|
+
if (!latex) continue;
|
|
158
|
+
|
|
159
|
+
let [txt = '', width = null, height = null, font_height = null, padding = null] = raw.split('|');
|
|
160
|
+
|
|
161
|
+
key = `\u004E\u0049\u0058\u0045\u004C_LATEX_${latex_index++}`;
|
|
162
|
+
tag = `{{${key}}}${txt || 'image'}{{/${key}}}`;
|
|
163
|
+
|
|
164
|
+
data = {
|
|
165
|
+
type: 'latex',
|
|
166
|
+
ie: {
|
|
167
|
+
key,
|
|
168
|
+
text: txt,
|
|
169
|
+
url,
|
|
170
|
+
width,
|
|
171
|
+
height,
|
|
172
|
+
font_height,
|
|
173
|
+
padding,
|
|
174
|
+
},
|
|
175
|
+
};
|
|
176
|
+
} else if (raw) {
|
|
177
|
+
if (!hyperlink) continue;
|
|
178
|
+
|
|
179
|
+
const trusted = !url.startsWith('!');
|
|
180
|
+
|
|
181
|
+
if (!trusted) {
|
|
182
|
+
url = url.slice(1);
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
key = `\u004E\u0049\u0058\u0045\u004C_HYPERLINK_${hyperlink_index++}`;
|
|
186
|
+
tag = `{{${key}}}${url}{{/${key}}}`;
|
|
187
|
+
|
|
188
|
+
data = {
|
|
189
|
+
type: 'hyperlink',
|
|
190
|
+
ie: {
|
|
191
|
+
key,
|
|
192
|
+
text: raw,
|
|
193
|
+
url,
|
|
194
|
+
is_trusted: trusted,
|
|
195
|
+
},
|
|
196
|
+
};
|
|
197
|
+
} else {
|
|
198
|
+
if (!citation) continue;
|
|
199
|
+
|
|
200
|
+
key = `\u004E\u0049\u0058\u0045\u004C_CITATION_${citation_index - 1}`;
|
|
201
|
+
tag = `{{${key}}}${url}{{/${key}}}`;
|
|
202
|
+
|
|
203
|
+
data = {
|
|
204
|
+
type: 'citation',
|
|
205
|
+
ie: {
|
|
206
|
+
reference_id: citation_index++,
|
|
207
|
+
key,
|
|
208
|
+
text: '',
|
|
209
|
+
url,
|
|
210
|
+
},
|
|
211
|
+
};
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
result += text.slice(last, start) + tag;
|
|
215
|
+
last = end;
|
|
216
|
+
|
|
217
|
+
ie.push(data);
|
|
218
|
+
|
|
219
|
+
const entity = createIE(data.type, data.ie);
|
|
220
|
+
|
|
221
|
+
if (entity) {
|
|
222
|
+
inline_entities.push(entity);
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
i = end - 1;
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
result += text.slice(last);
|
|
230
|
+
|
|
231
|
+
return {
|
|
232
|
+
text: result,
|
|
233
|
+
ie,
|
|
234
|
+
inline_entities,
|
|
235
|
+
};
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
async function waitAllPromises(input) {
|
|
239
|
+
const isPromise = (v) => v && typeof v.then === 'function';
|
|
240
|
+
const isObject = (v) => v && typeof v === 'object';
|
|
241
|
+
|
|
242
|
+
const deep = async (v) => {
|
|
243
|
+
if (isPromise(v)) return deep(await v);
|
|
244
|
+
if (Array.isArray(v)) return Promise.all(v.map(deep));
|
|
245
|
+
if (isObject(v)) {
|
|
246
|
+
const entries = await Promise.all(Object.entries(v).map(async ([k, val]) => [k, await deep(val)]));
|
|
247
|
+
return Object.fromEntries(entries);
|
|
248
|
+
}
|
|
249
|
+
return v;
|
|
250
|
+
};
|
|
251
|
+
|
|
252
|
+
return deep(await input);
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
class AIRichError extends Error {
|
|
256
|
+
constructor(message, code, meta = {}) {
|
|
257
|
+
super(message);
|
|
258
|
+
this.name = 'AIRichError';
|
|
259
|
+
this.code = code;
|
|
260
|
+
Object.assign(this, meta);
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
class ItemNotFoundError extends AIRichError {
|
|
265
|
+
constructor(id, availableIds = []) {
|
|
266
|
+
super(`Item id "${id}" not found${availableIds.length ? ` (available: ${availableIds.join(', ')})` : ' (no items have an id yet)'}`, 'ITEM_NOT_FOUND', { id, availableIds });
|
|
267
|
+
this.name = 'ItemNotFoundError';
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
class DuplicateIdError extends AIRichError {
|
|
272
|
+
constructor(id) {
|
|
273
|
+
super(`Item id "${id}" already exists`, 'DUPLICATE_ID', { id });
|
|
274
|
+
this.name = 'DuplicateIdError';
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
class InvalidTargetError extends AIRichError {
|
|
279
|
+
constructor(message, meta = {}) {
|
|
280
|
+
super(message, 'INVALID_TARGET', meta);
|
|
281
|
+
this.name = 'InvalidTargetError';
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
class ContentValidationError extends AIRichError {
|
|
286
|
+
constructor(message, meta = {}) {
|
|
287
|
+
super(message, 'CONTENT_VALIDATION', meta);
|
|
288
|
+
this.name = 'ContentValidationError';
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
class Toolkit {
|
|
293
|
+
constructor() {}
|
|
294
|
+
|
|
295
|
+
static extractIE(text, { extract = true, hyperlink = true, citation = true, latex = true } = {}) {
|
|
296
|
+
return extractIE(text, { extract, hyperlink, citation, latex });
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
static async resize(buffer, x, y, fit = 'cover') {
|
|
300
|
+
return await sharp(buffer)
|
|
301
|
+
.resize(x, y, {
|
|
302
|
+
fit,
|
|
303
|
+
position: 'center',
|
|
304
|
+
background: { r: 0, g: 0, b: 0, alpha: 0 },
|
|
305
|
+
})
|
|
306
|
+
.png()
|
|
307
|
+
.toBuffer();
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
static async waitAllPromises(input) {
|
|
311
|
+
return await waitAllPromises(input);
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
static async fetchBuffer(url, options = {}, { silent = true } = {}) {
|
|
315
|
+
try {
|
|
316
|
+
let response = await fetch(url, options);
|
|
317
|
+
if (!response.ok) throw Error(`HTTP ${response.status}`);
|
|
318
|
+
return Buffer.from(await response.arrayBuffer());
|
|
319
|
+
} catch (error) {
|
|
320
|
+
if (silent) return Buffer.alloc(0);
|
|
321
|
+
throw error;
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
static async toUrl(_client, path, mediaType = 'document') {
|
|
326
|
+
if (!path) throw new Error('Url or buffer needed');
|
|
327
|
+
|
|
328
|
+
const media = await prepareWAMessageMedia(
|
|
329
|
+
{
|
|
330
|
+
[mediaType]: Buffer.isBuffer(path) ? path : { url: path },
|
|
331
|
+
},
|
|
332
|
+
{
|
|
333
|
+
upload: _client.waUploadToServer,
|
|
334
|
+
jid: '\u0040\u006e\u0065\u0077\u0073\u006c\u0065\u0074\u0074\u0065\u0072',
|
|
335
|
+
}
|
|
336
|
+
);
|
|
337
|
+
|
|
338
|
+
return Object.values(media)[0]?.url;
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
static async resolveMedia(_client, media, mediaType = 'image', { resolveUrl = false, resolveWAUrl = false, result = 'url', resize = false, width = 300, height = 300 } = {}) {
|
|
342
|
+
const isUrl = (str) => /^https?:\/\/.+/i.test(str);
|
|
343
|
+
|
|
344
|
+
const isWAUrl = (str) => /^https?:\/\/[^/]*\.whatsapp\.net\//i.test(str);
|
|
345
|
+
|
|
346
|
+
if (Array.isArray(media)) {
|
|
347
|
+
return Promise.all(
|
|
348
|
+
media.map((item) =>
|
|
349
|
+
Toolkit.resolveMedia(_client, item, mediaType, {
|
|
350
|
+
resolveUrl,
|
|
351
|
+
resolveWAUrl,
|
|
352
|
+
result,
|
|
353
|
+
resize,
|
|
354
|
+
width,
|
|
355
|
+
height,
|
|
356
|
+
})
|
|
357
|
+
)
|
|
358
|
+
);
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
const originalIsBuffer = Buffer.isBuffer(media);
|
|
362
|
+
|
|
363
|
+
if (typeof media === 'string' && isUrl(media)) {
|
|
364
|
+
if (isWAUrl(media)) {
|
|
365
|
+
if (resolveWAUrl) {
|
|
366
|
+
media = await Toolkit.fetchBuffer(media, {}, { silent: true });
|
|
367
|
+
} else if (!resolveUrl) {
|
|
368
|
+
if (result === 'url') return media;
|
|
369
|
+
|
|
370
|
+
media = await Toolkit.fetchBuffer(media, {}, { silent: true });
|
|
371
|
+
}
|
|
372
|
+
} else {
|
|
373
|
+
if (!resolveUrl) {
|
|
374
|
+
if (result === 'url') return media;
|
|
375
|
+
|
|
376
|
+
media = await Toolkit.fetchBuffer(media, {}, { silent: true });
|
|
377
|
+
} else {
|
|
378
|
+
media = await Toolkit.fetchBuffer(media, {}, { silent: true });
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
if (typeof media === 'string' && !isUrl(media)) {
|
|
384
|
+
media = Buffer.from(media, 'base64');
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
if (!Buffer.isBuffer(media) || !media.length) {
|
|
388
|
+
return;
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
if (resize && Buffer.isBuffer(media)) {
|
|
392
|
+
media = await Toolkit.resize(media, width, height);
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
if (result === 'buffer') {
|
|
396
|
+
return media;
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
if (result === 'base64') {
|
|
400
|
+
return media.toString('base64');
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
if (originalIsBuffer) {
|
|
404
|
+
return Toolkit.toUrl(_client, media, mediaType);
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
return Toolkit.toUrl(_client, media, mediaType);
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
static getMp4Duration(buffer, { silent = true } = {}) {
|
|
411
|
+
try {
|
|
412
|
+
if (!Buffer.isBuffer(buffer) || buffer.length < 8) {
|
|
413
|
+
if (silent) return 0;
|
|
414
|
+
throw new Error('Invalid buffer');
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
let offset = 0;
|
|
418
|
+
|
|
419
|
+
while (offset < buffer.length - 8) {
|
|
420
|
+
const size = buffer.readUInt32BE(offset);
|
|
421
|
+
|
|
422
|
+
if (size < 8 || offset + size > buffer.length) {
|
|
423
|
+
if (silent) return 0;
|
|
424
|
+
throw new Error('Invalid atom size');
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
const type = buffer.toString('ascii', offset + 4, offset + 8);
|
|
428
|
+
|
|
429
|
+
if (type === 'moov') {
|
|
430
|
+
let moovOffset = offset + 8;
|
|
431
|
+
const moovEnd = offset + size;
|
|
432
|
+
|
|
433
|
+
while (moovOffset < moovEnd - 8) {
|
|
434
|
+
const childSize = buffer.readUInt32BE(moovOffset);
|
|
435
|
+
|
|
436
|
+
if (childSize < 8 || moovOffset + childSize > moovEnd) {
|
|
437
|
+
if (silent) return 0;
|
|
438
|
+
throw new Error('Invalid child atom size');
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
const childType = buffer.toString('ascii', moovOffset + 4, moovOffset + 8);
|
|
442
|
+
|
|
443
|
+
if (childType === 'mvhd') {
|
|
444
|
+
const version = buffer.readUInt8(moovOffset + 8);
|
|
445
|
+
|
|
446
|
+
if (version === 0) {
|
|
447
|
+
const timescale = buffer.readUInt32BE(moovOffset + 20);
|
|
448
|
+
const duration = buffer.readUInt32BE(moovOffset + 24);
|
|
449
|
+
|
|
450
|
+
if (!timescale) {
|
|
451
|
+
if (silent) return 0;
|
|
452
|
+
throw new Error('Invalid timescale');
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
return duration / timescale;
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
if (version === 1) {
|
|
459
|
+
const timescale = buffer.readUInt32BE(moovOffset + 32);
|
|
460
|
+
const duration = Number(buffer.readBigUInt64BE(moovOffset + 36));
|
|
461
|
+
|
|
462
|
+
if (!timescale) {
|
|
463
|
+
if (silent) return 0;
|
|
464
|
+
throw new Error('Invalid timescale');
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
return duration / timescale;
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
moovOffset += childSize;
|
|
472
|
+
}
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
offset += size;
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
if (silent) return 0;
|
|
479
|
+
|
|
480
|
+
throw new Error('No mvhd found!');
|
|
481
|
+
} catch (err) {
|
|
482
|
+
if (silent) return 0;
|
|
483
|
+
throw err;
|
|
484
|
+
}
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
static getMp4Preview(videoBuffer, { time, result = 'buffer', resize = true, width = 300, height = 300, silent = true } = {}) {
|
|
488
|
+
return new Promise((resolve, reject) => {
|
|
489
|
+
const fail = (err) => {
|
|
490
|
+
if (silent) {
|
|
491
|
+
return resolve(result === 'base64' ? '' : Buffer.alloc(0));
|
|
492
|
+
}
|
|
493
|
+
return reject(err);
|
|
494
|
+
};
|
|
495
|
+
|
|
496
|
+
try {
|
|
497
|
+
if (!Buffer.isBuffer(videoBuffer) || !videoBuffer.length) {
|
|
498
|
+
return fail(new Error('videoBuffer tidak valid atau kosong'));
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
const inputStream = new Readable({ read() {} });
|
|
502
|
+
inputStream.push(videoBuffer);
|
|
503
|
+
inputStream.push(null);
|
|
504
|
+
|
|
505
|
+
const outputStream = new PassThrough();
|
|
506
|
+
const chunks = [];
|
|
507
|
+
|
|
508
|
+
outputStream.on('data', (chunk) => chunks.push(chunk));
|
|
509
|
+
|
|
510
|
+
outputStream.on('end', async () => {
|
|
511
|
+
try {
|
|
512
|
+
let output = Buffer.concat(chunks);
|
|
513
|
+
|
|
514
|
+
if (!output.length) {
|
|
515
|
+
return fail(new Error('Output kosong — cek format atau timestamp video'));
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
if (resize) {
|
|
519
|
+
output = await Toolkit.resize(output, width, height);
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
return resolve(result === 'base64' ? output.toString('base64') : output);
|
|
523
|
+
} catch (err) {
|
|
524
|
+
return fail(err);
|
|
525
|
+
}
|
|
526
|
+
});
|
|
527
|
+
|
|
528
|
+
outputStream.on('error', fail);
|
|
529
|
+
|
|
530
|
+
time = time ?? Math.min(Toolkit.getMp4Duration(videoBuffer) * 0.2, 10);
|
|
531
|
+
|
|
532
|
+
ffmpeg(inputStream)
|
|
533
|
+
.outputOptions([`-ss ${time}`, '-vframes 1', '-vcodec png', '-f image2pipe'])
|
|
534
|
+
.on('error', (err) => fail(new Error(`ffmpeg error: ${err.message}`)))
|
|
535
|
+
.pipe(outputStream, { end: true });
|
|
536
|
+
} catch (err) {
|
|
537
|
+
return fail(err);
|
|
538
|
+
}
|
|
539
|
+
});
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
static stringifyEscaped(obj) {
|
|
543
|
+
return JSON.stringify(obj).replace(/[\u007f-\uffff]/g, (c) => '\\u' + c.charCodeAt(0).toString(16).padStart(4, '0'));
|
|
544
|
+
}
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
class BaseBuilder {
|
|
548
|
+
constructor() {
|
|
549
|
+
this._title = '';
|
|
550
|
+
this._subtitle = '';
|
|
551
|
+
this._body = '';
|
|
552
|
+
this._footer = '';
|
|
553
|
+
this._contextInfo = {};
|
|
554
|
+
this._extraPayload = {};
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
setTitle(title) {
|
|
558
|
+
if (typeof title !== 'string') {
|
|
559
|
+
throw new TypeError('Title must be a string');
|
|
560
|
+
}
|
|
561
|
+
this._title = title;
|
|
562
|
+
return this;
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
setSubtitle(subtitle) {
|
|
566
|
+
if (typeof subtitle !== 'string') {
|
|
567
|
+
throw new TypeError('Subtitle must be a string');
|
|
568
|
+
}
|
|
569
|
+
this._subtitle = subtitle;
|
|
570
|
+
return this;
|
|
571
|
+
}
|
|
572
|
+
|
|
573
|
+
setBody(body) {
|
|
574
|
+
if (typeof body !== 'string') {
|
|
575
|
+
throw new TypeError('Body must be a string');
|
|
576
|
+
}
|
|
577
|
+
this._body = body;
|
|
578
|
+
return this;
|
|
579
|
+
}
|
|
580
|
+
|
|
581
|
+
setFooter(footer) {
|
|
582
|
+
if (typeof footer !== 'string') {
|
|
583
|
+
throw new TypeError('Footer must be a string');
|
|
584
|
+
}
|
|
585
|
+
this._footer = footer;
|
|
586
|
+
return this;
|
|
587
|
+
}
|
|
588
|
+
|
|
589
|
+
setContextInfo(obj) {
|
|
590
|
+
if (typeof obj !== 'object' || obj === null || Array.isArray(obj)) {
|
|
591
|
+
throw new TypeError('ContextInfo must be a plain object');
|
|
592
|
+
}
|
|
593
|
+
|
|
594
|
+
this._contextInfo = obj;
|
|
595
|
+
return this;
|
|
596
|
+
}
|
|
597
|
+
|
|
598
|
+
addPayload(obj) {
|
|
599
|
+
if (typeof obj !== 'object' || obj === null || Array.isArray(obj)) {
|
|
600
|
+
throw new TypeError('Payload must be a plain object');
|
|
601
|
+
}
|
|
602
|
+
|
|
603
|
+
Object.assign(this._extraPayload, obj);
|
|
604
|
+
|
|
605
|
+
return this;
|
|
606
|
+
}
|
|
607
|
+
}
|
|
608
|
+
|
|
609
|
+
class Button extends BaseBuilder {
|
|
610
|
+
#client;
|
|
611
|
+
|
|
612
|
+
constructor(client) {
|
|
613
|
+
super();
|
|
614
|
+
if (!client) {
|
|
615
|
+
throw new Error('Socket is required');
|
|
616
|
+
}
|
|
617
|
+
this.#client = client;
|
|
618
|
+
|
|
619
|
+
this._buttons = [];
|
|
620
|
+
this._data;
|
|
621
|
+
this._currentSelectionIndex = -1;
|
|
622
|
+
this._currentSectionIndex = -1;
|
|
623
|
+
this._params = {};
|
|
624
|
+
}
|
|
625
|
+
|
|
626
|
+
loadFrom(msg) {
|
|
627
|
+
if (!msg) throw new Error('interactiveMessage needed');
|
|
628
|
+
if (!msg.interactiveMessage) throw new Error('interactiveMessage not found');
|
|
629
|
+
|
|
630
|
+
const { interactiveMessage, ...extraPayload } = msg;
|
|
631
|
+
const iM = interactiveMessage;
|
|
632
|
+
const header = iM.header || {};
|
|
633
|
+
const nativeFlow = iM.nativeFlowMessage || {};
|
|
634
|
+
|
|
635
|
+
this._title = header.title || '';
|
|
636
|
+
this._subtitle = header.subtitle || '';
|
|
637
|
+
this._body = iM.body?.text || '';
|
|
638
|
+
this._footer = iM.footer?.text || '';
|
|
639
|
+
this._contextInfo = iM.contextInfo || {};
|
|
640
|
+
this._extraPayload = extraPayload;
|
|
641
|
+
|
|
642
|
+
this._buttons = Array.isArray(nativeFlow.buttons)
|
|
643
|
+
? nativeFlow.buttons.map((button) => ({
|
|
644
|
+
...button,
|
|
645
|
+
buttonParamsJson: typeof button.buttonParamsJson === 'string' ? button.buttonParamsJson : JSON.stringify(button.buttonParamsJson || {}),
|
|
646
|
+
}))
|
|
647
|
+
: [];
|
|
648
|
+
|
|
649
|
+
this._data = header.imageMessage
|
|
650
|
+
? { imageMessage: header.imageMessage }
|
|
651
|
+
: header.videoMessage
|
|
652
|
+
? { videoMessage: header.videoMessage }
|
|
653
|
+
: header.documentMessage
|
|
654
|
+
? { documentMessage: header.documentMessage }
|
|
655
|
+
: header.productMessage
|
|
656
|
+
? { productMessage: header.productMessage }
|
|
657
|
+
: undefined;
|
|
658
|
+
|
|
659
|
+
this._params = {};
|
|
660
|
+
|
|
661
|
+
if (typeof nativeFlow.messageParamsJson === 'string') {
|
|
662
|
+
try {
|
|
663
|
+
this._params = JSON.parse(nativeFlow.messageParamsJson || '{}');
|
|
664
|
+
} catch {
|
|
665
|
+
this._params = {};
|
|
666
|
+
}
|
|
667
|
+
} else if (nativeFlow.messageParamsJson && typeof nativeFlow.messageParamsJson === 'object') {
|
|
668
|
+
this._params = { ...nativeFlow.messageParamsJson };
|
|
669
|
+
}
|
|
670
|
+
|
|
671
|
+
this._currentSelectionIndex = this._buttons.findLastIndex((button) => button.name === 'single_select');
|
|
672
|
+
|
|
673
|
+
this._currentSectionIndex = -1;
|
|
674
|
+
|
|
675
|
+
if (this._currentSelectionIndex !== -1) {
|
|
676
|
+
try {
|
|
677
|
+
const button = this._buttons[this._currentSelectionIndex];
|
|
678
|
+
const params = JSON.parse(button.buttonParamsJson || '{}');
|
|
679
|
+
|
|
680
|
+
if (Array.isArray(params.sections) && params.sections.length) {
|
|
681
|
+
this._currentSectionIndex = params.sections.length - 1;
|
|
682
|
+
}
|
|
683
|
+
} catch {
|
|
684
|
+
this._currentSelectionIndex = -1;
|
|
685
|
+
this._currentSectionIndex = -1;
|
|
686
|
+
}
|
|
687
|
+
}
|
|
688
|
+
|
|
689
|
+
return this;
|
|
690
|
+
}
|
|
691
|
+
|
|
692
|
+
setImage(path, options = {}) {
|
|
693
|
+
if (!path) throw new Error('Url or buffer needed');
|
|
694
|
+
Buffer.isBuffer(path) ? (this._data = { image: path, ...options }) : (this._data = { image: { url: path }, ...options });
|
|
695
|
+
return this;
|
|
696
|
+
}
|
|
697
|
+
|
|
698
|
+
setDocument(path, options = {}) {
|
|
699
|
+
if (!path) throw new Error('Url or buffer needed');
|
|
700
|
+
Buffer.isBuffer(path) ? (this._data = { document: path, ...options }) : (this._data = { document: { url: path }, ...options });
|
|
701
|
+
return this;
|
|
702
|
+
}
|
|
703
|
+
|
|
704
|
+
setMedia(obj) {
|
|
705
|
+
if (typeof obj !== 'object' || obj === null || Array.isArray(obj)) {
|
|
706
|
+
throw new TypeError('Media must be a plain object');
|
|
707
|
+
}
|
|
708
|
+
|
|
709
|
+
this._data = obj;
|
|
710
|
+
return this;
|
|
711
|
+
}
|
|
712
|
+
|
|
713
|
+
clearButtons() {
|
|
714
|
+
this._buttons = [];
|
|
715
|
+
return this;
|
|
716
|
+
}
|
|
717
|
+
|
|
718
|
+
setParams(obj) {
|
|
719
|
+
this._params = obj;
|
|
720
|
+
return this;
|
|
721
|
+
}
|
|
722
|
+
|
|
723
|
+
addButton(name, params) {
|
|
724
|
+
this._buttons.push({
|
|
725
|
+
name,
|
|
726
|
+
buttonParamsJson: typeof params === 'string' ? params : JSON.stringify(params),
|
|
727
|
+
});
|
|
728
|
+
|
|
729
|
+
return this;
|
|
730
|
+
}
|
|
731
|
+
|
|
732
|
+
makeRow(header = '', title = '', description = '', id = '') {
|
|
733
|
+
if (this._currentSelectionIndex === -1 || this._currentSectionIndex === -1) {
|
|
734
|
+
throw new Error('You need to create a selection and a section first');
|
|
735
|
+
}
|
|
736
|
+
const buttonParams = JSON.parse(this._buttons[this._currentSelectionIndex].buttonParamsJson);
|
|
737
|
+
buttonParams.sections[this._currentSectionIndex].rows.push({ header, title, description, id });
|
|
738
|
+
this._buttons[this._currentSelectionIndex].buttonParamsJson = JSON.stringify(buttonParams);
|
|
739
|
+
return this;
|
|
740
|
+
}
|
|
741
|
+
|
|
742
|
+
makeSection(title = '', highlight_label = '') {
|
|
743
|
+
if (this._currentSelectionIndex === -1) {
|
|
744
|
+
throw new Error('You need to create a selection first');
|
|
745
|
+
}
|
|
746
|
+
const buttonParams = JSON.parse(this._buttons[this._currentSelectionIndex].buttonParamsJson);
|
|
747
|
+
buttonParams.sections.push({ title, highlight_label, rows: [] });
|
|
748
|
+
this._currentSectionIndex = buttonParams.sections.length - 1;
|
|
749
|
+
this._buttons[this._currentSelectionIndex].buttonParamsJson = JSON.stringify(buttonParams);
|
|
750
|
+
return this;
|
|
751
|
+
}
|
|
752
|
+
|
|
753
|
+
addSelection(title, options = {}) {
|
|
754
|
+
this._buttons.push({ name: 'single_select', buttonParamsJson: JSON.stringify({ title, sections: [], ...options }) });
|
|
755
|
+
this._currentSelectionIndex = this._buttons.length - 1;
|
|
756
|
+
this._currentSectionIndex = -1;
|
|
757
|
+
return this;
|
|
758
|
+
}
|
|
759
|
+
|
|
760
|
+
addReply(display_text = '', id = '', options = {}) {
|
|
761
|
+
this._buttons.push({
|
|
762
|
+
name: 'quick_reply',
|
|
763
|
+
buttonParamsJson: JSON.stringify({
|
|
764
|
+
display_text,
|
|
765
|
+
id,
|
|
766
|
+
...options,
|
|
767
|
+
}),
|
|
768
|
+
});
|
|
769
|
+
return this;
|
|
770
|
+
}
|
|
771
|
+
|
|
772
|
+
addCall(display_text = '', id = '', options = {}) {
|
|
773
|
+
this._buttons.push({
|
|
774
|
+
name: 'cta_call',
|
|
775
|
+
buttonParamsJson: JSON.stringify({
|
|
776
|
+
display_text,
|
|
777
|
+
id,
|
|
778
|
+
...options,
|
|
779
|
+
}),
|
|
780
|
+
});
|
|
781
|
+
return this;
|
|
782
|
+
}
|
|
783
|
+
|
|
784
|
+
addReminder(display_text = '', id = '', options = {}) {
|
|
785
|
+
this._buttons.push({
|
|
786
|
+
name: 'cta_reminder',
|
|
787
|
+
buttonParamsJson: JSON.stringify({
|
|
788
|
+
display_text,
|
|
789
|
+
id,
|
|
790
|
+
...options,
|
|
791
|
+
}),
|
|
792
|
+
});
|
|
793
|
+
return this;
|
|
794
|
+
}
|
|
795
|
+
|
|
796
|
+
addCancelReminder(display_text = '', id = '', options = {}) {
|
|
797
|
+
this._buttons.push({
|
|
798
|
+
name: 'cta_cancel_reminder',
|
|
799
|
+
buttonParamsJson: JSON.stringify({
|
|
800
|
+
display_text,
|
|
801
|
+
id,
|
|
802
|
+
...options,
|
|
803
|
+
}),
|
|
804
|
+
});
|
|
805
|
+
return this;
|
|
806
|
+
}
|
|
807
|
+
|
|
808
|
+
addAddress(display_text = '', id = '', options = {}) {
|
|
809
|
+
this._buttons.push({
|
|
810
|
+
name: 'address_message',
|
|
811
|
+
buttonParamsJson: JSON.stringify({
|
|
812
|
+
display_text,
|
|
813
|
+
id,
|
|
814
|
+
...options,
|
|
815
|
+
}),
|
|
816
|
+
});
|
|
817
|
+
return this;
|
|
818
|
+
}
|
|
819
|
+
|
|
820
|
+
addLocation(options = {}) {
|
|
821
|
+
this._buttons.push({
|
|
822
|
+
name: 'send_location',
|
|
823
|
+
buttonParamsJson: JSON.stringify(options),
|
|
824
|
+
});
|
|
825
|
+
return this;
|
|
826
|
+
}
|
|
827
|
+
|
|
828
|
+
addUrl(display_text = '', url = '', webview_interaction = false, options = {}) {
|
|
829
|
+
this._buttons.push({
|
|
830
|
+
...options,
|
|
831
|
+
name: 'cta_url',
|
|
832
|
+
buttonParamsJson: JSON.stringify({
|
|
833
|
+
display_text,
|
|
834
|
+
url,
|
|
835
|
+
webview_interaction,
|
|
836
|
+
...options,
|
|
837
|
+
}),
|
|
838
|
+
});
|
|
839
|
+
return this;
|
|
840
|
+
}
|
|
841
|
+
|
|
842
|
+
addCopy(display_text = '', copy_code = '', options = {}) {
|
|
843
|
+
this._buttons.push({
|
|
844
|
+
name: 'cta_copy',
|
|
845
|
+
buttonParamsJson: JSON.stringify({
|
|
846
|
+
display_text,
|
|
847
|
+
copy_code,
|
|
848
|
+
...options,
|
|
849
|
+
}),
|
|
850
|
+
});
|
|
851
|
+
return this;
|
|
852
|
+
}
|
|
853
|
+
|
|
854
|
+
static paramsList = {
|
|
855
|
+
limited_time_offer: {
|
|
856
|
+
text: 'string',
|
|
857
|
+
url: 'string',
|
|
858
|
+
copy_code: 'string',
|
|
859
|
+
expiration_time: 'number',
|
|
860
|
+
},
|
|
861
|
+
bottom_sheet: {
|
|
862
|
+
in_thread_buttons_limit: 'number',
|
|
863
|
+
divider_indices: ['number'],
|
|
864
|
+
list_title: 'string',
|
|
865
|
+
button_title: 'string',
|
|
866
|
+
},
|
|
867
|
+
tap_target_configuration: {
|
|
868
|
+
title: 'string',
|
|
869
|
+
description: 'string',
|
|
870
|
+
canonical_url: 'string',
|
|
871
|
+
domain: 'string',
|
|
872
|
+
buttonIndex: 'number',
|
|
873
|
+
},
|
|
874
|
+
};
|
|
875
|
+
|
|
876
|
+
async toCard() {
|
|
877
|
+
return {
|
|
878
|
+
body: {
|
|
879
|
+
text: this._body,
|
|
880
|
+
},
|
|
881
|
+
footer: {
|
|
882
|
+
text: this._footer,
|
|
883
|
+
},
|
|
884
|
+
header: {
|
|
885
|
+
title: this._title,
|
|
886
|
+
subtitle: this._subtitle,
|
|
887
|
+
hasMediaAttachment: !!this._data,
|
|
888
|
+
...(this._data
|
|
889
|
+
? await prepareWAMessageMedia(this._data, { upload: this.#client.waUploadToServer }).catch((e) => {
|
|
890
|
+
if (String(e).includes('Invalid media type')) return this._data;
|
|
891
|
+
throw e;
|
|
892
|
+
})
|
|
893
|
+
: {}),
|
|
894
|
+
},
|
|
895
|
+
nativeFlowMessage: {
|
|
896
|
+
messageParamsJson: JSON.stringify(this._params),
|
|
897
|
+
buttons: this._buttons,
|
|
898
|
+
},
|
|
899
|
+
};
|
|
900
|
+
}
|
|
901
|
+
|
|
902
|
+
async build(jid, { messageId, ...options } = {}) {
|
|
903
|
+
const message = await this.toCard();
|
|
904
|
+
|
|
905
|
+
return generateWAMessageFromContent(
|
|
906
|
+
jid,
|
|
907
|
+
{
|
|
908
|
+
...this._extraPayload,
|
|
909
|
+
interactiveMessage: {
|
|
910
|
+
...message,
|
|
911
|
+
contextInfo: this._contextInfo,
|
|
912
|
+
},
|
|
913
|
+
},
|
|
914
|
+
{ messageId: messageId || generateMessageIDV2(), ...options }
|
|
915
|
+
);
|
|
916
|
+
}
|
|
917
|
+
|
|
918
|
+
async send(jid, { messageId, additionalNodes = [], ...options } = {}) {
|
|
919
|
+
const msg = await this.build(jid, { messageId, ...options });
|
|
920
|
+
|
|
921
|
+
await this.#client.relayMessage(msg.key.remoteJid, msg.message, {
|
|
922
|
+
messageId: msg.key.id,
|
|
923
|
+
additionalNodes: [
|
|
924
|
+
{
|
|
925
|
+
tag: 'biz',
|
|
926
|
+
attrs: {},
|
|
927
|
+
content: [
|
|
928
|
+
{
|
|
929
|
+
tag: 'interactive',
|
|
930
|
+
attrs: { type: 'native_flow', v: '1' },
|
|
931
|
+
content: [{ tag: 'native_flow', attrs: { v: '9', name: 'mixed' } }],
|
|
932
|
+
},
|
|
933
|
+
],
|
|
934
|
+
},
|
|
935
|
+
...additionalNodes,
|
|
936
|
+
],
|
|
937
|
+
...options,
|
|
938
|
+
});
|
|
939
|
+
return msg;
|
|
940
|
+
}
|
|
941
|
+
}
|
|
942
|
+
|
|
943
|
+
class ButtonV2 extends BaseBuilder {
|
|
944
|
+
#client;
|
|
945
|
+
|
|
946
|
+
constructor(client) {
|
|
947
|
+
super();
|
|
948
|
+
if (!client) {
|
|
949
|
+
throw new Error('Socket is required');
|
|
950
|
+
}
|
|
951
|
+
|
|
952
|
+
this.#client = client;
|
|
953
|
+
this._image;
|
|
954
|
+
this._data;
|
|
955
|
+
this._buttons = [];
|
|
956
|
+
}
|
|
957
|
+
|
|
958
|
+
loadFrom(msg) {
|
|
959
|
+
if (!msg) throw new Error('buttonsMessage needed');
|
|
960
|
+
if (!msg.buttonsMessage) throw new Error('buttonsMessage not found');
|
|
961
|
+
|
|
962
|
+
const { buttonsMessage, ...extraPayload } = msg;
|
|
963
|
+
const bM = buttonsMessage;
|
|
964
|
+
const location = bM.locationMessage || {};
|
|
965
|
+
|
|
966
|
+
this._title = location.name || '';
|
|
967
|
+
this._subtitle = location.address || '';
|
|
968
|
+
this._body = bM.contentText || '';
|
|
969
|
+
this._footer = bM.footerText || '';
|
|
970
|
+
this._contextInfo = bM.contextInfo || {};
|
|
971
|
+
this._extraPayload = extraPayload;
|
|
972
|
+
|
|
973
|
+
this._buttons = Array.isArray(bM.buttons)
|
|
974
|
+
? bM.buttons.map((button) => ({
|
|
975
|
+
...button,
|
|
976
|
+
...(button.nativeFlowInfo
|
|
977
|
+
? {
|
|
978
|
+
nativeFlowInfo: {
|
|
979
|
+
...button.nativeFlowInfo,
|
|
980
|
+
paramsJson: typeof button.nativeFlowInfo.paramsJson === 'string' ? button.nativeFlowInfo.paramsJson : JSON.stringify(button.nativeFlowInfo.paramsJson || {}),
|
|
981
|
+
},
|
|
982
|
+
}
|
|
983
|
+
: {}),
|
|
984
|
+
}))
|
|
985
|
+
: [];
|
|
986
|
+
|
|
987
|
+
this._image = location.jpegThumbnail || undefined;
|
|
988
|
+
|
|
989
|
+
if (!this._image && bM.locationMessage) {
|
|
990
|
+
this._image = undefined;
|
|
991
|
+
}
|
|
992
|
+
|
|
993
|
+
if (!bM.locationMessage && bM.headerType === 6) {
|
|
994
|
+
this._image = undefined;
|
|
995
|
+
}
|
|
996
|
+
|
|
997
|
+
this._data = Object.keys(bM).reduce((data, key) => {
|
|
998
|
+
if (!['contentText', 'footerText', 'contextInfo', 'buttons', 'headerType', 'locationMessage', 'viewOnce'].includes(key)) {
|
|
999
|
+
data[key] = bM[key];
|
|
1000
|
+
}
|
|
1001
|
+
return data;
|
|
1002
|
+
}, {});
|
|
1003
|
+
|
|
1004
|
+
if (!Object.keys(this._data).length) {
|
|
1005
|
+
this._data = undefined;
|
|
1006
|
+
}
|
|
1007
|
+
|
|
1008
|
+
return this;
|
|
1009
|
+
}
|
|
1010
|
+
|
|
1011
|
+
addButton(displayText = '', buttonId = crypto.randomUUID()) {
|
|
1012
|
+
this._buttons.push({
|
|
1013
|
+
buttonId,
|
|
1014
|
+
buttonText: { displayText },
|
|
1015
|
+
type: 1,
|
|
1016
|
+
});
|
|
1017
|
+
return this;
|
|
1018
|
+
}
|
|
1019
|
+
|
|
1020
|
+
addRawButton(obj) {
|
|
1021
|
+
if (typeof obj !== 'object' || obj === null || Array.isArray(obj)) {
|
|
1022
|
+
throw new TypeError('Buttons must be a plain object');
|
|
1023
|
+
}
|
|
1024
|
+
|
|
1025
|
+
this._buttons.push(obj);
|
|
1026
|
+
return this;
|
|
1027
|
+
}
|
|
1028
|
+
|
|
1029
|
+
setRawThumbnail(thumbnail) {
|
|
1030
|
+
if (!thumbnail) throw new Error('Thumbnail needed');
|
|
1031
|
+
this._image = { base64: thumbnail, is_raw: true };
|
|
1032
|
+
return this;
|
|
1033
|
+
}
|
|
1034
|
+
|
|
1035
|
+
setThumbnail(path) {
|
|
1036
|
+
if (!path) throw new Error('Url or buffer needed');
|
|
1037
|
+
this._image = path;
|
|
1038
|
+
return this;
|
|
1039
|
+
}
|
|
1040
|
+
|
|
1041
|
+
setMedia(obj) {
|
|
1042
|
+
if (typeof obj !== 'object' || obj === null || Array.isArray(obj)) {
|
|
1043
|
+
throw new TypeError('Media must be a plain object');
|
|
1044
|
+
}
|
|
1045
|
+
|
|
1046
|
+
this._data = obj;
|
|
1047
|
+
return this;
|
|
1048
|
+
}
|
|
1049
|
+
|
|
1050
|
+
async build(jid, { messageId, ...options } = {}) {
|
|
1051
|
+
const _thumbnail = this._image?.is_raw
|
|
1052
|
+
? this._image.base64
|
|
1053
|
+
: this._image
|
|
1054
|
+
? await Toolkit.resize(Buffer.isBuffer(this._image) ? this._image : await Toolkit.fetchBuffer(this._image, {}, { silent: true }), 300, 300)
|
|
1055
|
+
: null;
|
|
1056
|
+
const msg = generateWAMessageFromContent(
|
|
1057
|
+
jid,
|
|
1058
|
+
{
|
|
1059
|
+
...this._extraPayload,
|
|
1060
|
+
buttonsMessage: {
|
|
1061
|
+
contentText: this._body,
|
|
1062
|
+
footerText: this._footer,
|
|
1063
|
+
...(this._data
|
|
1064
|
+
? this._data
|
|
1065
|
+
: {
|
|
1066
|
+
headerType: 6,
|
|
1067
|
+
locationMessage: {
|
|
1068
|
+
degreesLatitude: 0,
|
|
1069
|
+
degreesLongitude: 0,
|
|
1070
|
+
name: this._title,
|
|
1071
|
+
address: this._subtitle,
|
|
1072
|
+
jpegThumbnail: _thumbnail,
|
|
1073
|
+
},
|
|
1074
|
+
}),
|
|
1075
|
+
viewOnce: true,
|
|
1076
|
+
contextInfo: this._contextInfo,
|
|
1077
|
+
buttons: [...this._buttons],
|
|
1078
|
+
},
|
|
1079
|
+
},
|
|
1080
|
+
{ messageId: messageId || generateMessageIDV2(), ...options }
|
|
1081
|
+
);
|
|
1082
|
+
return msg;
|
|
1083
|
+
}
|
|
1084
|
+
|
|
1085
|
+
async send(jid, { messageId, additionalNodes = [], ...options } = {}) {
|
|
1086
|
+
if (this._buttons.length < 1) throw new Error('ButtonV2 requires at least one button');
|
|
1087
|
+
const msg = await this.build(jid, { messageId, ...options });
|
|
1088
|
+
|
|
1089
|
+
await this.#client.relayMessage(msg.key.remoteJid, msg.message, {
|
|
1090
|
+
messageId: msg.key.id,
|
|
1091
|
+
additionalNodes: [
|
|
1092
|
+
{
|
|
1093
|
+
tag: 'biz',
|
|
1094
|
+
attrs: {},
|
|
1095
|
+
content: [
|
|
1096
|
+
{
|
|
1097
|
+
tag: 'interactive',
|
|
1098
|
+
attrs: { type: 'native_flow', v: '1' },
|
|
1099
|
+
content: [{ tag: 'native_flow', attrs: { v: '9', name: 'mixed' } }],
|
|
1100
|
+
},
|
|
1101
|
+
],
|
|
1102
|
+
},
|
|
1103
|
+
...additionalNodes,
|
|
1104
|
+
],
|
|
1105
|
+
...options,
|
|
1106
|
+
});
|
|
1107
|
+
return msg;
|
|
1108
|
+
}
|
|
1109
|
+
}
|
|
1110
|
+
|
|
1111
|
+
class Carousel extends BaseBuilder {
|
|
1112
|
+
#client;
|
|
1113
|
+
|
|
1114
|
+
constructor(client) {
|
|
1115
|
+
super();
|
|
1116
|
+
if (!client) {
|
|
1117
|
+
throw new Error('Socket is required');
|
|
1118
|
+
}
|
|
1119
|
+
|
|
1120
|
+
this.#client = client;
|
|
1121
|
+
this._cards = [];
|
|
1122
|
+
}
|
|
1123
|
+
|
|
1124
|
+
loadFrom(msg) {
|
|
1125
|
+
if (!msg) throw new Error('interactiveMessage needed');
|
|
1126
|
+
if (!msg.interactiveMessage) throw new Error('interactiveMessage not found');
|
|
1127
|
+
|
|
1128
|
+
const { interactiveMessage, ...extraPayload } = msg;
|
|
1129
|
+
const iM = interactiveMessage;
|
|
1130
|
+
const carousel = iM.carouselMessage || {};
|
|
1131
|
+
|
|
1132
|
+
this._body = iM.body?.text || '';
|
|
1133
|
+
this._footer = iM.footer?.text || '';
|
|
1134
|
+
this._contextInfo = iM.contextInfo || {};
|
|
1135
|
+
this._extraPayload = extraPayload;
|
|
1136
|
+
|
|
1137
|
+
this._cards = Array.isArray(carousel.cards)
|
|
1138
|
+
? carousel.cards.map((card) => ({
|
|
1139
|
+
...card,
|
|
1140
|
+
header: {
|
|
1141
|
+
...(card.header || {}),
|
|
1142
|
+
hasMediaAttachment: !!card.header?.hasMediaAttachment,
|
|
1143
|
+
...(card.header?.imageMessage ? { imageMessage: card.header.imageMessage } : {}),
|
|
1144
|
+
...(card.header?.videoMessage ? { videoMessage: card.header.videoMessage } : {}),
|
|
1145
|
+
},
|
|
1146
|
+
body: {
|
|
1147
|
+
text: card.body?.text || '',
|
|
1148
|
+
},
|
|
1149
|
+
footer: {
|
|
1150
|
+
text: card.footer?.text || '',
|
|
1151
|
+
},
|
|
1152
|
+
nativeFlowMessage: {
|
|
1153
|
+
...(card.nativeFlowMessage || {}),
|
|
1154
|
+
buttons: Array.isArray(card.nativeFlowMessage?.buttons)
|
|
1155
|
+
? card.nativeFlowMessage.buttons.map((button) => ({
|
|
1156
|
+
...button,
|
|
1157
|
+
buttonParamsJson: typeof button.buttonParamsJson === 'string' ? button.buttonParamsJson : JSON.stringify(button.buttonParamsJson || {}),
|
|
1158
|
+
}))
|
|
1159
|
+
: [],
|
|
1160
|
+
messageParamsJson:
|
|
1161
|
+
typeof card.nativeFlowMessage?.messageParamsJson === 'string' ? card.nativeFlowMessage.messageParamsJson : JSON.stringify(card.nativeFlowMessage?.messageParamsJson || {}),
|
|
1162
|
+
},
|
|
1163
|
+
}))
|
|
1164
|
+
: [];
|
|
1165
|
+
|
|
1166
|
+
return this;
|
|
1167
|
+
}
|
|
1168
|
+
|
|
1169
|
+
addCard(card) {
|
|
1170
|
+
const cards = Array.isArray(card) ? card : [card];
|
|
1171
|
+
const baseIndex = this._cards.length;
|
|
1172
|
+
|
|
1173
|
+
for (const [index, c] of cards.entries()) {
|
|
1174
|
+
if (!c?.header?.hasMediaAttachment) {
|
|
1175
|
+
throw new Error(`Card [${baseIndex + index}] must include an image or video in header`);
|
|
1176
|
+
}
|
|
1177
|
+
}
|
|
1178
|
+
|
|
1179
|
+
this._cards.push(...cards);
|
|
1180
|
+
return this;
|
|
1181
|
+
}
|
|
1182
|
+
|
|
1183
|
+
build(jid, { messageId, ...options } = {}) {
|
|
1184
|
+
return generateWAMessageFromContent(
|
|
1185
|
+
jid,
|
|
1186
|
+
{
|
|
1187
|
+
...this._extraPayload,
|
|
1188
|
+
interactiveMessage: {
|
|
1189
|
+
header: {
|
|
1190
|
+
hasMediaAttachment: false,
|
|
1191
|
+
},
|
|
1192
|
+
body: { text: this._body },
|
|
1193
|
+
footer: { text: this._footer },
|
|
1194
|
+
contextInfo: this._contextInfo,
|
|
1195
|
+
carouselMessage: {
|
|
1196
|
+
cards: this._cards,
|
|
1197
|
+
},
|
|
1198
|
+
},
|
|
1199
|
+
},
|
|
1200
|
+
{ messageId: messageId || generateMessageIDV2(), ...options }
|
|
1201
|
+
);
|
|
1202
|
+
}
|
|
1203
|
+
|
|
1204
|
+
async send(jid, { messageId, additionalNodes = [], ...options } = {}) {
|
|
1205
|
+
const msg = this.build(jid, { messageId, ...options });
|
|
1206
|
+
|
|
1207
|
+
await this.#client.relayMessage(msg.key.remoteJid, msg.message, {
|
|
1208
|
+
messageId: msg.key.id,
|
|
1209
|
+
additionalNodes: [
|
|
1210
|
+
{
|
|
1211
|
+
tag: 'biz',
|
|
1212
|
+
attrs: {},
|
|
1213
|
+
content: [
|
|
1214
|
+
{
|
|
1215
|
+
tag: 'interactive',
|
|
1216
|
+
attrs: { type: 'native_flow', v: '1' },
|
|
1217
|
+
content: [{ tag: 'native_flow', attrs: { v: '9', name: 'mixed' } }],
|
|
1218
|
+
},
|
|
1219
|
+
],
|
|
1220
|
+
},
|
|
1221
|
+
...additionalNodes,
|
|
1222
|
+
],
|
|
1223
|
+
...options,
|
|
1224
|
+
});
|
|
1225
|
+
return msg;
|
|
1226
|
+
}
|
|
1227
|
+
}
|
|
1228
|
+
|
|
1229
|
+
class AIRich extends BaseBuilder {
|
|
1230
|
+
#client;
|
|
1231
|
+
|
|
1232
|
+
constructor(client, { dynamic = true, unsupportedTypeAlert = true } = {}) {
|
|
1233
|
+
if (!client) {
|
|
1234
|
+
throw new Error('Socket is required');
|
|
1235
|
+
}
|
|
1236
|
+
|
|
1237
|
+
super();
|
|
1238
|
+
this.#client = client;
|
|
1239
|
+
this._contextInfo = {};
|
|
1240
|
+
this._nodes = [];
|
|
1241
|
+
this._idIndex = new Map();
|
|
1242
|
+
this._unsupportedTypeAlert = !!unsupportedTypeAlert;
|
|
1243
|
+
this._dynamic = !!dynamic;
|
|
1244
|
+
this._responseId = crypto.randomUUID();
|
|
1245
|
+
this._botResponseId = crypto.randomUUID();
|
|
1246
|
+
this._lastMessageKey = null;
|
|
1247
|
+
}
|
|
1248
|
+
|
|
1249
|
+
loadFrom(msg) {
|
|
1250
|
+
if (!msg) throw new Error('AI Rich message needed');
|
|
1251
|
+
|
|
1252
|
+
const message = msg.message ?? msg;
|
|
1253
|
+
|
|
1254
|
+
let richResponseMessage = message?.botForwardedMessage?.message?.richResponseMessage;
|
|
1255
|
+
|
|
1256
|
+
if (!richResponseMessage) {
|
|
1257
|
+
richResponseMessage = message?.botForwardedMessage?.richResponseMessage;
|
|
1258
|
+
}
|
|
1259
|
+
|
|
1260
|
+
if (!richResponseMessage) {
|
|
1261
|
+
richResponseMessage = message?.richResponseMessage;
|
|
1262
|
+
}
|
|
1263
|
+
|
|
1264
|
+
if (!richResponseMessage) {
|
|
1265
|
+
throw new Error('richResponseMessage not found');
|
|
1266
|
+
}
|
|
1267
|
+
|
|
1268
|
+
const messageContextInfo = message?.messageContextInfo ?? {};
|
|
1269
|
+
const botMetadata = messageContextInfo?.botMetadata ?? {};
|
|
1270
|
+
|
|
1271
|
+
this._title = botMetadata?.messageDisclaimerText ?? '';
|
|
1272
|
+
|
|
1273
|
+
this._contextInfo = structuredClone(richResponseMessage?.contextInfo ?? {});
|
|
1274
|
+
|
|
1275
|
+
const loadedSubmessages = Array.isArray(richResponseMessage?.submessages) ? structuredClone(richResponseMessage.submessages) : [];
|
|
1276
|
+
|
|
1277
|
+
let loadedSections = [];
|
|
1278
|
+
|
|
1279
|
+
const unifiedData = richResponseMessage?.unifiedResponse?.data;
|
|
1280
|
+
|
|
1281
|
+
if (unifiedData) {
|
|
1282
|
+
try {
|
|
1283
|
+
const decoded = Buffer.from(unifiedData, 'base64').toString('utf8');
|
|
1284
|
+
const unifiedResponse = JSON.parse(decoded);
|
|
1285
|
+
|
|
1286
|
+
if (Array.isArray(unifiedResponse?.sections)) {
|
|
1287
|
+
loadedSections = structuredClone(unifiedResponse.sections);
|
|
1288
|
+
}
|
|
1289
|
+
} catch {}
|
|
1290
|
+
}
|
|
1291
|
+
|
|
1292
|
+
this._nodes = [];
|
|
1293
|
+
this._idIndex = new Map();
|
|
1294
|
+
|
|
1295
|
+
const maxLength = Math.max(loadedSections.length, loadedSubmessages.length);
|
|
1296
|
+
|
|
1297
|
+
for (let i = 0; i < maxLength; i++) {
|
|
1298
|
+
this._nodes.push({
|
|
1299
|
+
id: null,
|
|
1300
|
+
section: loadedSections[i] ?? null,
|
|
1301
|
+
submessage: loadedSubmessages[i] ?? null,
|
|
1302
|
+
});
|
|
1303
|
+
}
|
|
1304
|
+
|
|
1305
|
+
this._extraPayload = {};
|
|
1306
|
+
|
|
1307
|
+
for (const [key, value] of Object.entries(message)) {
|
|
1308
|
+
if (key !== 'messageContextInfo' && key !== 'botForwardedMessage' && key !== 'richResponseMessage') {
|
|
1309
|
+
this._extraPayload[key] = structuredClone(value);
|
|
1310
|
+
}
|
|
1311
|
+
}
|
|
1312
|
+
|
|
1313
|
+
return this;
|
|
1314
|
+
}
|
|
1315
|
+
|
|
1316
|
+
setResponseId(id) {
|
|
1317
|
+
if (typeof id !== 'string') {
|
|
1318
|
+
throw new TypeError('ID must be a string');
|
|
1319
|
+
}
|
|
1320
|
+
this._responseId = id;
|
|
1321
|
+
|
|
1322
|
+
return this;
|
|
1323
|
+
}
|
|
1324
|
+
|
|
1325
|
+
refreshResponseId() {
|
|
1326
|
+
this._responseId = crypto.randomUUID();
|
|
1327
|
+
|
|
1328
|
+
return this;
|
|
1329
|
+
}
|
|
1330
|
+
|
|
1331
|
+
setBotResponseId(id) {
|
|
1332
|
+
if (typeof id !== 'string') {
|
|
1333
|
+
throw new TypeError('ID must be a string');
|
|
1334
|
+
}
|
|
1335
|
+
this._botResponseId = id;
|
|
1336
|
+
|
|
1337
|
+
return this;
|
|
1338
|
+
}
|
|
1339
|
+
|
|
1340
|
+
refreshBotResponseId() {
|
|
1341
|
+
this._botResponseId = crypto.randomUUID();
|
|
1342
|
+
|
|
1343
|
+
return this;
|
|
1344
|
+
}
|
|
1345
|
+
|
|
1346
|
+
createAlert(type) {
|
|
1347
|
+
if (this._unsupportedTypeAlert) {
|
|
1348
|
+
return {
|
|
1349
|
+
messageType: 2,
|
|
1350
|
+
messageText: `[ UNSUPPORTED_TYPE - ${type}]`,
|
|
1351
|
+
};
|
|
1352
|
+
}
|
|
1353
|
+
|
|
1354
|
+
return undefined;
|
|
1355
|
+
}
|
|
1356
|
+
|
|
1357
|
+
addText(text, { hyperlink = true, citation = true, latex = true, id, replace, insertAt } = {}) {
|
|
1358
|
+
if (typeof text !== 'string') {
|
|
1359
|
+
throw new TypeError('Text must be a string');
|
|
1360
|
+
}
|
|
1361
|
+
|
|
1362
|
+
const { text: extractedText, inline_entities } = extractIE(text, {
|
|
1363
|
+
hyperlink,
|
|
1364
|
+
citation,
|
|
1365
|
+
latex,
|
|
1366
|
+
});
|
|
1367
|
+
|
|
1368
|
+
const section = AIRich.newLayout('Single', {
|
|
1369
|
+
text: extractedText,
|
|
1370
|
+
...(inline_entities.length && { inline_entities }),
|
|
1371
|
+
__typename: 'GenAIMarkdownTextUXPrimitive',
|
|
1372
|
+
});
|
|
1373
|
+
|
|
1374
|
+
const submessages = [
|
|
1375
|
+
{
|
|
1376
|
+
messageType: 2,
|
|
1377
|
+
messageText: text,
|
|
1378
|
+
},
|
|
1379
|
+
].filter(Boolean);
|
|
1380
|
+
|
|
1381
|
+
return this._addContent(section, submessages, {
|
|
1382
|
+
id,
|
|
1383
|
+
replace,
|
|
1384
|
+
insertAt,
|
|
1385
|
+
});
|
|
1386
|
+
}
|
|
1387
|
+
|
|
1388
|
+
addFOAText(text, { id, replace, insertAt } = {}) {
|
|
1389
|
+
if (typeof text !== 'string') {
|
|
1390
|
+
throw new TypeError('Text must be a string');
|
|
1391
|
+
}
|
|
1392
|
+
|
|
1393
|
+
const section = AIRich.newLayout('Single', {
|
|
1394
|
+
text,
|
|
1395
|
+
__typename: 'FOATextPrimitive',
|
|
1396
|
+
});
|
|
1397
|
+
|
|
1398
|
+
const submessages = [
|
|
1399
|
+
{
|
|
1400
|
+
messageType: 2,
|
|
1401
|
+
messageText: text,
|
|
1402
|
+
},
|
|
1403
|
+
];
|
|
1404
|
+
|
|
1405
|
+
return this._addContent(section, submessages, {
|
|
1406
|
+
id,
|
|
1407
|
+
replace,
|
|
1408
|
+
insertAt,
|
|
1409
|
+
});
|
|
1410
|
+
}
|
|
1411
|
+
|
|
1412
|
+
addCode(language, code, { id, replace, insertAt } = {}) {
|
|
1413
|
+
if (typeof language !== 'string' || typeof code !== 'string') {
|
|
1414
|
+
throw new TypeError('Language and code must be a string');
|
|
1415
|
+
}
|
|
1416
|
+
|
|
1417
|
+
const meta = AIRich.tokenizer(code, language);
|
|
1418
|
+
|
|
1419
|
+
const section = AIRich.newLayout('Single', {
|
|
1420
|
+
language,
|
|
1421
|
+
code_blocks: meta.unified_codeBlock,
|
|
1422
|
+
__typename: 'GenAICodeUXPrimitive',
|
|
1423
|
+
});
|
|
1424
|
+
|
|
1425
|
+
const submessages = [
|
|
1426
|
+
{
|
|
1427
|
+
messageType: 5,
|
|
1428
|
+
codeMetadata: {
|
|
1429
|
+
codeLanguage: language,
|
|
1430
|
+
codeBlocks: meta.codeBlock,
|
|
1431
|
+
},
|
|
1432
|
+
},
|
|
1433
|
+
];
|
|
1434
|
+
|
|
1435
|
+
return this._addContent(section, submessages, {
|
|
1436
|
+
id,
|
|
1437
|
+
replace,
|
|
1438
|
+
insertAt,
|
|
1439
|
+
});
|
|
1440
|
+
}
|
|
1441
|
+
|
|
1442
|
+
addTable(table, { hyperlink = true, citation = true, latex = true, id, replace, insertAt } = {}) {
|
|
1443
|
+
if (!Array.isArray(table)) {
|
|
1444
|
+
throw new TypeError('Table must be an array');
|
|
1445
|
+
}
|
|
1446
|
+
|
|
1447
|
+
const meta = AIRich.toTableMetadata(table, {
|
|
1448
|
+
hyperlink,
|
|
1449
|
+
citation,
|
|
1450
|
+
latex,
|
|
1451
|
+
});
|
|
1452
|
+
|
|
1453
|
+
const section = AIRich.newLayout('Single', {
|
|
1454
|
+
rows: meta.unified_rows,
|
|
1455
|
+
__typename: 'GenATableUXPrimitive',
|
|
1456
|
+
});
|
|
1457
|
+
|
|
1458
|
+
const submessages = [
|
|
1459
|
+
{
|
|
1460
|
+
messageType: 4,
|
|
1461
|
+
tableMetadata: {
|
|
1462
|
+
title: meta.title,
|
|
1463
|
+
rows: meta.rows,
|
|
1464
|
+
},
|
|
1465
|
+
},
|
|
1466
|
+
];
|
|
1467
|
+
|
|
1468
|
+
return this._addContent(section, submessages, {
|
|
1469
|
+
id,
|
|
1470
|
+
replace,
|
|
1471
|
+
insertAt,
|
|
1472
|
+
});
|
|
1473
|
+
}
|
|
1474
|
+
|
|
1475
|
+
addSource(sources = [], { id, replace, insertAt } = {}) {
|
|
1476
|
+
if (!Array.isArray(sources)) {
|
|
1477
|
+
throw new TypeError('Sources must be an array of strings, arrays, or objects');
|
|
1478
|
+
}
|
|
1479
|
+
|
|
1480
|
+
const isStringArray = sources.every((item) => typeof item === 'string');
|
|
1481
|
+
|
|
1482
|
+
const isArrayFormat = sources.every((item) => Array.isArray(item) && item.every((value) => typeof value === 'string'));
|
|
1483
|
+
|
|
1484
|
+
const isObjectFormat = sources.every((item) => item && typeof item === 'object' && !Array.isArray(item));
|
|
1485
|
+
|
|
1486
|
+
if (!isStringArray && !isArrayFormat && !isObjectFormat) {
|
|
1487
|
+
throw new TypeError('Sources must be a string array, array of string arrays, or array of objects');
|
|
1488
|
+
}
|
|
1489
|
+
|
|
1490
|
+
if (isStringArray) {
|
|
1491
|
+
sources = [sources];
|
|
1492
|
+
}
|
|
1493
|
+
|
|
1494
|
+
const normalizedSources = sources.map((source) => {
|
|
1495
|
+
if (Array.isArray(source)) {
|
|
1496
|
+
const [icon, url, title, subtitle] = source;
|
|
1497
|
+
|
|
1498
|
+
return {
|
|
1499
|
+
icon,
|
|
1500
|
+
url,
|
|
1501
|
+
title,
|
|
1502
|
+
subtitle,
|
|
1503
|
+
};
|
|
1504
|
+
}
|
|
1505
|
+
|
|
1506
|
+
return {
|
|
1507
|
+
icon: source.favicon ?? source.icon ?? '',
|
|
1508
|
+
url: source.url ?? '',
|
|
1509
|
+
title: source.title ?? '',
|
|
1510
|
+
subtitle: source.subtitle ?? '',
|
|
1511
|
+
};
|
|
1512
|
+
});
|
|
1513
|
+
|
|
1514
|
+
const source = normalizedSources.map(({ icon, url, title, subtitle }) => ({
|
|
1515
|
+
source_type: 'THIRD_PARTY',
|
|
1516
|
+
source_display_name: title,
|
|
1517
|
+
source_subtitle: subtitle,
|
|
1518
|
+
source_url: url,
|
|
1519
|
+
favicon: {
|
|
1520
|
+
url: Toolkit.resolveMedia(this.#client, icon, 'image'),
|
|
1521
|
+
mime_type: 'image/jpeg',
|
|
1522
|
+
width: 16,
|
|
1523
|
+
height: 16,
|
|
1524
|
+
},
|
|
1525
|
+
}));
|
|
1526
|
+
|
|
1527
|
+
const submessage = this.createAlert('GenAISearchResultPrimitive');
|
|
1528
|
+
|
|
1529
|
+
const section = AIRich.newLayout('Single', {
|
|
1530
|
+
sources: source,
|
|
1531
|
+
__typename: 'GenAISearchResultPrimitive',
|
|
1532
|
+
});
|
|
1533
|
+
|
|
1534
|
+
return this._addContent(section, submessage, {
|
|
1535
|
+
id,
|
|
1536
|
+
replace,
|
|
1537
|
+
insertAt,
|
|
1538
|
+
});
|
|
1539
|
+
}
|
|
1540
|
+
|
|
1541
|
+
addReels(reelsItems = [], { id, replace, insertAt } = {}) {
|
|
1542
|
+
if (
|
|
1543
|
+
!(
|
|
1544
|
+
(reelsItems && typeof reelsItems === 'object' && !Array.isArray(reelsItems)) ||
|
|
1545
|
+
(Array.isArray(reelsItems) && reelsItems.every((item) => item && typeof item === 'object' && !Array.isArray(item)))
|
|
1546
|
+
)
|
|
1547
|
+
) {
|
|
1548
|
+
throw new TypeError('Reels items must be an object or an array of objects');
|
|
1549
|
+
}
|
|
1550
|
+
|
|
1551
|
+
const items = Array.isArray(reelsItems) ? reelsItems : [reelsItems];
|
|
1552
|
+
|
|
1553
|
+
const reels = items.map((item) => ({
|
|
1554
|
+
...item,
|
|
1555
|
+
_avatar: Toolkit.resolveMedia(this.#client, item.profileIconUrl ?? item.profile_url ?? item.profile ?? '', 'image'),
|
|
1556
|
+
_thumbnail: Toolkit.resolveMedia(this.#client, item.thumbnailUrl ?? item.thumbnail ?? '', 'image'),
|
|
1557
|
+
}));
|
|
1558
|
+
|
|
1559
|
+
const section = AIRich.newLayout(
|
|
1560
|
+
'HScroll',
|
|
1561
|
+
reels.map((item) => ({
|
|
1562
|
+
reels_url: item.videoUrl ?? item.url ?? '',
|
|
1563
|
+
thumbnail_url: item._thumbnail,
|
|
1564
|
+
creator: item.username ?? item.title ?? '',
|
|
1565
|
+
avatar_url: item._avatar,
|
|
1566
|
+
reels_title: item.reels_title ?? item.title ?? '',
|
|
1567
|
+
likes_count: item.likes_count ?? item.like ?? 0,
|
|
1568
|
+
shares_count: item.shares_count ?? item.share ?? 0,
|
|
1569
|
+
view_count: item.view_count ?? item.view ?? 0,
|
|
1570
|
+
reel_source: item.reel_source ?? item.source ?? 'IG',
|
|
1571
|
+
is_verified: !!(item.is_verified || item.verified),
|
|
1572
|
+
__typename: 'GenAIReelPrimitive',
|
|
1573
|
+
}))
|
|
1574
|
+
);
|
|
1575
|
+
|
|
1576
|
+
const submessages = [
|
|
1577
|
+
{
|
|
1578
|
+
messageType: 9,
|
|
1579
|
+
contentItemsMetadata: {
|
|
1580
|
+
contentType: 1,
|
|
1581
|
+
itemsMetadata: reels.map((item) => ({
|
|
1582
|
+
reelItem: {
|
|
1583
|
+
title: item.username ?? '',
|
|
1584
|
+
profileIconUrl: item._avatar,
|
|
1585
|
+
thumbnailUrl: item._thumbnail,
|
|
1586
|
+
videoUrl: item.videoUrl ?? item.url ?? '',
|
|
1587
|
+
},
|
|
1588
|
+
})),
|
|
1589
|
+
},
|
|
1590
|
+
},
|
|
1591
|
+
];
|
|
1592
|
+
|
|
1593
|
+
return this._addContent(section, submessages, {
|
|
1594
|
+
id,
|
|
1595
|
+
replace,
|
|
1596
|
+
insertAt,
|
|
1597
|
+
});
|
|
1598
|
+
}
|
|
1599
|
+
|
|
1600
|
+
addImage(imageUrl, { width, height, status = 'READY', update_text, resolveUrl = false, id, replace, insertAt } = {}) {
|
|
1601
|
+
if (!(typeof imageUrl === 'string' || Buffer.isBuffer(imageUrl) || (Array.isArray(imageUrl) && imageUrl.every((v) => typeof v === 'string' || Buffer.isBuffer(v))))) {
|
|
1602
|
+
throw new TypeError('imageUrl must be string | buffer | array of string/buffer');
|
|
1603
|
+
}
|
|
1604
|
+
|
|
1605
|
+
const list = Array.isArray(imageUrl)
|
|
1606
|
+
? imageUrl.map((v) => {
|
|
1607
|
+
const url = Toolkit.resolveMedia(this.#client, v, 'image', { resolveUrl });
|
|
1608
|
+
|
|
1609
|
+
return {
|
|
1610
|
+
imagePreviewUrl: url,
|
|
1611
|
+
imageHighResUrl: url,
|
|
1612
|
+
sourceUrl: url,
|
|
1613
|
+
};
|
|
1614
|
+
})
|
|
1615
|
+
: (() => {
|
|
1616
|
+
const url = Toolkit.resolveMedia(this.#client, imageUrl, 'image', { resolveUrl });
|
|
1617
|
+
|
|
1618
|
+
return [
|
|
1619
|
+
{
|
|
1620
|
+
imagePreviewUrl: url,
|
|
1621
|
+
imageHighResUrl: url,
|
|
1622
|
+
sourceUrl: url,
|
|
1623
|
+
},
|
|
1624
|
+
];
|
|
1625
|
+
})();
|
|
1626
|
+
|
|
1627
|
+
const sections = list.map(({ imagePreviewUrl }) =>
|
|
1628
|
+
AIRich.newLayout('Single', {
|
|
1629
|
+
media: {
|
|
1630
|
+
url: imagePreviewUrl,
|
|
1631
|
+
mime_type: 'image/png',
|
|
1632
|
+
width,
|
|
1633
|
+
height,
|
|
1634
|
+
},
|
|
1635
|
+
imagine_type: 'IMAGE',
|
|
1636
|
+
status: {
|
|
1637
|
+
status,
|
|
1638
|
+
update_text,
|
|
1639
|
+
},
|
|
1640
|
+
__typename: 'GenAIImaginePrimitive',
|
|
1641
|
+
})
|
|
1642
|
+
);
|
|
1643
|
+
|
|
1644
|
+
const submessage = {
|
|
1645
|
+
messageType: 1,
|
|
1646
|
+
gridImageMetadata: {
|
|
1647
|
+
gridImageUrl: {
|
|
1648
|
+
imagePreviewUrl: list[0]?.imagePreviewUrl,
|
|
1649
|
+
},
|
|
1650
|
+
imageUrls: list,
|
|
1651
|
+
},
|
|
1652
|
+
};
|
|
1653
|
+
|
|
1654
|
+
if (id && sections.length !== 1) {
|
|
1655
|
+
throw new Error('Cannot assign one id to multiple image sections');
|
|
1656
|
+
}
|
|
1657
|
+
|
|
1658
|
+
return this._addContent(sections, submessage, {
|
|
1659
|
+
id,
|
|
1660
|
+
replace,
|
|
1661
|
+
insertAt,
|
|
1662
|
+
});
|
|
1663
|
+
}
|
|
1664
|
+
|
|
1665
|
+
addVideo(videoUrl, { autoFill = true, status = 'READY', estimatedTime, id, replace, insertAt } = {}) {
|
|
1666
|
+
const isObjectVideo = (v) => v && typeof v === 'object' && !Array.isArray(v) && v.url;
|
|
1667
|
+
|
|
1668
|
+
const isValidPrimitive =
|
|
1669
|
+
typeof videoUrl === 'string' ||
|
|
1670
|
+
Buffer.isBuffer(videoUrl) ||
|
|
1671
|
+
isObjectVideo(videoUrl) ||
|
|
1672
|
+
(Array.isArray(videoUrl) && videoUrl.every((v) => typeof v === 'string' || Buffer.isBuffer(v) || isObjectVideo(v)));
|
|
1673
|
+
|
|
1674
|
+
if (!isValidPrimitive) {
|
|
1675
|
+
throw new TypeError('videoUrl must be string | buffer | object | array');
|
|
1676
|
+
}
|
|
1677
|
+
|
|
1678
|
+
const items = Array.isArray(videoUrl) ? videoUrl : [videoUrl];
|
|
1679
|
+
|
|
1680
|
+
const alert = this.createAlert('GenAIImaginePrimitive (ANIMATE)');
|
|
1681
|
+
|
|
1682
|
+
const sections = [];
|
|
1683
|
+
const submessages = [];
|
|
1684
|
+
|
|
1685
|
+
for (const item of items) {
|
|
1686
|
+
const isObject = isObjectVideo(item);
|
|
1687
|
+
|
|
1688
|
+
const url = isObject ? Toolkit.resolveMedia(this.#client, item.url ?? '', 'video') : Toolkit.resolveMedia(this.#client, item, 'video');
|
|
1689
|
+
|
|
1690
|
+
const bufferPromise = autoFill ? Promise.resolve(url).then((u) => Toolkit.fetchBuffer(u)) : null;
|
|
1691
|
+
|
|
1692
|
+
const file_length = isObject && item.file_length != null ? item.file_length : autoFill ? bufferPromise.then((b) => b?.length ?? 0) : 0;
|
|
1693
|
+
|
|
1694
|
+
const duration =
|
|
1695
|
+
isObject && item.duration != null
|
|
1696
|
+
? item.duration
|
|
1697
|
+
: autoFill
|
|
1698
|
+
? bufferPromise.then((b) =>
|
|
1699
|
+
Toolkit.getMp4Duration(b, {
|
|
1700
|
+
silent: true,
|
|
1701
|
+
})
|
|
1702
|
+
)
|
|
1703
|
+
: 0;
|
|
1704
|
+
|
|
1705
|
+
const thumbnail =
|
|
1706
|
+
isObject && item.thumbnail
|
|
1707
|
+
? Toolkit.resolveMedia(this.#client, item.thumbnail, 'image', {
|
|
1708
|
+
result: 'base64',
|
|
1709
|
+
resize: true,
|
|
1710
|
+
width: 300,
|
|
1711
|
+
height: 300,
|
|
1712
|
+
})
|
|
1713
|
+
: autoFill
|
|
1714
|
+
? bufferPromise?.then((b) =>
|
|
1715
|
+
Toolkit.getMp4Preview(b, {
|
|
1716
|
+
time: 0,
|
|
1717
|
+
result: 'base64',
|
|
1718
|
+
})
|
|
1719
|
+
)
|
|
1720
|
+
: null;
|
|
1721
|
+
|
|
1722
|
+
sections.push(
|
|
1723
|
+
AIRich.newLayout('Single', {
|
|
1724
|
+
media: {
|
|
1725
|
+
url,
|
|
1726
|
+
mime_type: isObject ? (item.mime_type ?? 'video/mp4') : 'video/mp4',
|
|
1727
|
+
file_length,
|
|
1728
|
+
duration,
|
|
1729
|
+
},
|
|
1730
|
+
imagine_type: 'ANIMATE',
|
|
1731
|
+
status: {
|
|
1732
|
+
status,
|
|
1733
|
+
estimated_completion_time: estimatedTime != null ? Math.floor((Date.now() + estimatedTime) / 1000) : undefined,
|
|
1734
|
+
},
|
|
1735
|
+
thumbnail: {
|
|
1736
|
+
raw_media: thumbnail,
|
|
1737
|
+
},
|
|
1738
|
+
__typename: 'GenAIImaginePrimitive',
|
|
1739
|
+
})
|
|
1740
|
+
);
|
|
1741
|
+
}
|
|
1742
|
+
|
|
1743
|
+
if (alert !== undefined) {
|
|
1744
|
+
submessages.push(alert);
|
|
1745
|
+
}
|
|
1746
|
+
|
|
1747
|
+
if (submessages.length > 1) {
|
|
1748
|
+
throw new Error('Video content can only have one submessage');
|
|
1749
|
+
}
|
|
1750
|
+
|
|
1751
|
+
return this._addContent(sections, submessages[0], {
|
|
1752
|
+
id,
|
|
1753
|
+
replace,
|
|
1754
|
+
insertAt,
|
|
1755
|
+
});
|
|
1756
|
+
}
|
|
1757
|
+
|
|
1758
|
+
addProduct(data = {}, { id, replace, insertAt } = {}) {
|
|
1759
|
+
if (!((data && typeof data === 'object' && !Array.isArray(data)) || (Array.isArray(data) && data.every((item) => item && typeof item === 'object' && !Array.isArray(item))))) {
|
|
1760
|
+
throw new TypeError('Product items must be an object or an array of objects');
|
|
1761
|
+
}
|
|
1762
|
+
|
|
1763
|
+
const items = Array.isArray(data) ? data : [data];
|
|
1764
|
+
|
|
1765
|
+
const product = items.map((item) => ({
|
|
1766
|
+
title: item.title,
|
|
1767
|
+
brand: item.brand,
|
|
1768
|
+
price: item.price,
|
|
1769
|
+
sale_price: item.sale_price,
|
|
1770
|
+
product_url: item.product_url ?? item.url,
|
|
1771
|
+
image: {
|
|
1772
|
+
url: Toolkit.resolveMedia(this.#client, item.image_url ?? item.image, 'image'),
|
|
1773
|
+
},
|
|
1774
|
+
additional_images: [
|
|
1775
|
+
{
|
|
1776
|
+
url: Toolkit.resolveMedia(this.#client, item.icon_url ?? item.icon, 'image'),
|
|
1777
|
+
},
|
|
1778
|
+
],
|
|
1779
|
+
__typename: 'GenAIProductItemCardPrimitive',
|
|
1780
|
+
}));
|
|
1781
|
+
|
|
1782
|
+
const section = AIRich.newLayout(Array.isArray(data) ? 'HScroll' : 'Single', Array.isArray(data) ? product : product[0]);
|
|
1783
|
+
|
|
1784
|
+
const submessage = this.createAlert('GenAIProductItemCardPrimitive');
|
|
1785
|
+
|
|
1786
|
+
return this._addContent(section, submessage, {
|
|
1787
|
+
id,
|
|
1788
|
+
replace,
|
|
1789
|
+
insertAt,
|
|
1790
|
+
});
|
|
1791
|
+
}
|
|
1792
|
+
|
|
1793
|
+
addPost(data = {}, { id, replace, insertAt } = {}) {
|
|
1794
|
+
if (!((data && typeof data === 'object' && !Array.isArray(data)) || (Array.isArray(data) && data.every((item) => item && typeof item === 'object' && !Array.isArray(item))))) {
|
|
1795
|
+
throw new TypeError('Post items must be an object or an array of objects');
|
|
1796
|
+
}
|
|
1797
|
+
|
|
1798
|
+
const posts = Array.isArray(data) ? data : [data];
|
|
1799
|
+
|
|
1800
|
+
const primitives = posts.map((p) => ({
|
|
1801
|
+
title: p.title ?? '',
|
|
1802
|
+
subtitle: p.subtitle ?? '',
|
|
1803
|
+
username: p.username ?? '',
|
|
1804
|
+
profile_picture_url: Toolkit.resolveMedia(this.#client, p.profile_picture_url ?? p.profile_url ?? p.profile ?? '', 'image'),
|
|
1805
|
+
is_verified: !!(p.is_verified || p.verified),
|
|
1806
|
+
thumbnail_url: Toolkit.resolveMedia(this.#client, p.thumbnail_url ?? p.thumbnail ?? '', 'image'),
|
|
1807
|
+
post_caption: p.post_caption ?? p.caption ?? '',
|
|
1808
|
+
likes_count: p.likes_count ?? p.like ?? 0,
|
|
1809
|
+
comments_count: p.comments_count ?? p.comment ?? 0,
|
|
1810
|
+
shares_count: p.shares_count ?? p.share ?? 0,
|
|
1811
|
+
post_url: p.post_url ?? p.url ?? '',
|
|
1812
|
+
post_deeplink: p.post_deeplink ?? p.deeplink ?? '',
|
|
1813
|
+
source_app: p.source_app || p.source || 'INSTAGRAM',
|
|
1814
|
+
footer_label: p.footer_label ?? p.footer ?? '',
|
|
1815
|
+
footer_icon: Toolkit.resolveMedia(this.#client, p.footer_icon ?? p.icon ?? '', 'image'),
|
|
1816
|
+
is_carousel: posts.length > 1,
|
|
1817
|
+
orientation: p.orientation ?? 'LANDSCAPE',
|
|
1818
|
+
post_type: p.post_type ?? 'VIDEO',
|
|
1819
|
+
__typename: 'GenAIPostPrimitive',
|
|
1820
|
+
}));
|
|
1821
|
+
|
|
1822
|
+
const section = AIRich.newLayout('HScroll', primitives);
|
|
1823
|
+
|
|
1824
|
+
const submessage = this.createAlert('GenAIPostPrimitive');
|
|
1825
|
+
|
|
1826
|
+
return this._addContent(section, submessage, {
|
|
1827
|
+
id,
|
|
1828
|
+
replace,
|
|
1829
|
+
insertAt,
|
|
1830
|
+
});
|
|
1831
|
+
}
|
|
1832
|
+
|
|
1833
|
+
addMetadata(text, { id, replace, insertAt } = {}) {
|
|
1834
|
+
if (typeof text !== 'string') {
|
|
1835
|
+
throw new TypeError('Text must be a string');
|
|
1836
|
+
}
|
|
1837
|
+
|
|
1838
|
+
const section = AIRich.newLayout('Single', {
|
|
1839
|
+
text,
|
|
1840
|
+
__typename: 'GenAIMetadataTextPrimitive',
|
|
1841
|
+
});
|
|
1842
|
+
|
|
1843
|
+
const submessage = {
|
|
1844
|
+
messageType: 2,
|
|
1845
|
+
messageText: text,
|
|
1846
|
+
};
|
|
1847
|
+
|
|
1848
|
+
return this._addContent(section, submessage, {
|
|
1849
|
+
id,
|
|
1850
|
+
replace,
|
|
1851
|
+
insertAt,
|
|
1852
|
+
});
|
|
1853
|
+
}
|
|
1854
|
+
|
|
1855
|
+
addTip(text, { id, replace, insertAt } = {}) {
|
|
1856
|
+
if (typeof text !== 'string') {
|
|
1857
|
+
throw new TypeError('Text must be a string');
|
|
1858
|
+
}
|
|
1859
|
+
|
|
1860
|
+
const section = AIRich.newLayout('Single', {
|
|
1861
|
+
text: 'ⓘ ' + text,
|
|
1862
|
+
__typename: 'GenAIMetadataTextPrimitive',
|
|
1863
|
+
});
|
|
1864
|
+
|
|
1865
|
+
const submessage = {
|
|
1866
|
+
messageType: 2,
|
|
1867
|
+
messageText: text,
|
|
1868
|
+
};
|
|
1869
|
+
|
|
1870
|
+
return this._addContent(section, submessage, {
|
|
1871
|
+
id,
|
|
1872
|
+
replace,
|
|
1873
|
+
insertAt,
|
|
1874
|
+
});
|
|
1875
|
+
}
|
|
1876
|
+
|
|
1877
|
+
addWidget(data, { layout, id, replace, insertAt, ...options } = {}) {
|
|
1878
|
+
if (!((data && typeof data === 'object' && !Array.isArray(data)) || (Array.isArray(data) && data.every((item) => item && typeof item === 'object' && !Array.isArray(item))))) {
|
|
1879
|
+
throw new TypeError('Widget must be an object or an array of objects');
|
|
1880
|
+
}
|
|
1881
|
+
|
|
1882
|
+
const isArray = Array.isArray(data);
|
|
1883
|
+
|
|
1884
|
+
const items = isArray ? data : [data];
|
|
1885
|
+
|
|
1886
|
+
const widgets = items.map((item) => ({
|
|
1887
|
+
__typename: 'GenAI3PExtWidgetPrimitive',
|
|
1888
|
+
|
|
1889
|
+
header: {
|
|
1890
|
+
__typename: 'GenAI3PExtWidgetStandardHeader',
|
|
1891
|
+
title: item.title ?? '',
|
|
1892
|
+
...(item.header ?? {}),
|
|
1893
|
+
},
|
|
1894
|
+
|
|
1895
|
+
body: {
|
|
1896
|
+
__typename: 'GenAI3PExtCalendarEventList',
|
|
1897
|
+
sections: item.sections ?? [],
|
|
1898
|
+
|
|
1899
|
+
ctas: (item.actions ?? []).map((action) => ({
|
|
1900
|
+
__typename: 'GenAI3PExtWidgetCTA',
|
|
1901
|
+
label: action.label ?? '',
|
|
1902
|
+
state: action.state ?? 'PENDING',
|
|
1903
|
+
kind: action.kind ?? 'OTHER',
|
|
1904
|
+
tool_call_id: action.tool_call_id ?? action.id ?? '',
|
|
1905
|
+
|
|
1906
|
+
...(action.toast && {
|
|
1907
|
+
toast: {
|
|
1908
|
+
__typename: 'GenAI3PExtWidgetToast',
|
|
1909
|
+
label: action.toast.label ?? action.label ?? '',
|
|
1910
|
+
},
|
|
1911
|
+
}),
|
|
1912
|
+
})),
|
|
1913
|
+
|
|
1914
|
+
...(item.body ?? {}),
|
|
1915
|
+
},
|
|
1916
|
+
}));
|
|
1917
|
+
|
|
1918
|
+
const section = AIRich.newLayout(layout ?? (isArray ? 'HScroll' : 'Single'), isArray ? widgets : widgets[0], options);
|
|
1919
|
+
|
|
1920
|
+
const submessage = this.createAlert('GenAI3PExtWidgetStandardHeader');
|
|
1921
|
+
|
|
1922
|
+
return this._addContent(section, submessage, {
|
|
1923
|
+
id,
|
|
1924
|
+
replace,
|
|
1925
|
+
insertAt,
|
|
1926
|
+
});
|
|
1927
|
+
}
|
|
1928
|
+
|
|
1929
|
+
addFooterAction(data, { layout, id, replace, insertAt, ...options } = {}) {
|
|
1930
|
+
if (!((data && typeof data === 'object' && !Array.isArray(data)) || (Array.isArray(data) && data.every((item) => item && typeof item === 'object' && !Array.isArray(item))))) {
|
|
1931
|
+
throw new TypeError('Footer action must be an object or an array of objects');
|
|
1932
|
+
}
|
|
1933
|
+
|
|
1934
|
+
const isArray = Array.isArray(data);
|
|
1935
|
+
|
|
1936
|
+
const items = isArray ? data : [data];
|
|
1937
|
+
|
|
1938
|
+
const actions = items.map((item) => ({
|
|
1939
|
+
__typename: 'GenAIFooterActionPrimitive',
|
|
1940
|
+
|
|
1941
|
+
cta_text: item.text ?? item.cta_text ?? '',
|
|
1942
|
+
|
|
1943
|
+
cta_type: item.type ?? item.cta_type ?? 'OPEN_URL',
|
|
1944
|
+
|
|
1945
|
+
cta_url: item.url ?? item.cta_url ?? '',
|
|
1946
|
+
}));
|
|
1947
|
+
|
|
1948
|
+
const section = AIRich.newLayout(layout ?? (isArray ? 'HScroll' : 'Single'), isArray ? actions : actions[0], options);
|
|
1949
|
+
|
|
1950
|
+
const submessage = this.createAlert('GenAIFooterActionPrimitive');
|
|
1951
|
+
|
|
1952
|
+
return this._addContent(section, submessage, {
|
|
1953
|
+
id,
|
|
1954
|
+
replace,
|
|
1955
|
+
insertAt,
|
|
1956
|
+
});
|
|
1957
|
+
}
|
|
1958
|
+
|
|
1959
|
+
addSuggest(suggestion, { scroll = true, layout, id, replace, insertAt } = {}) {
|
|
1960
|
+
if (!(typeof suggestion === 'string' || (Array.isArray(suggestion) && suggestion.every((v) => typeof v === 'string')))) {
|
|
1961
|
+
throw new TypeError('Suggestion must be a string or array of strings');
|
|
1962
|
+
}
|
|
1963
|
+
|
|
1964
|
+
const suggest = Array.isArray(suggestion)
|
|
1965
|
+
? suggestion.map((text) => ({
|
|
1966
|
+
prompt_text: text,
|
|
1967
|
+
prompt_type: 'SUGGESTED_PROMPT',
|
|
1968
|
+
__typename: 'GenAIFollowUpSuggestionPillPrimitive',
|
|
1969
|
+
}))
|
|
1970
|
+
: [
|
|
1971
|
+
{
|
|
1972
|
+
prompt_text: suggestion,
|
|
1973
|
+
prompt_type: 'SUGGESTED_PROMPT',
|
|
1974
|
+
__typename: 'GenAIFollowUpSuggestionPillPrimitive',
|
|
1975
|
+
},
|
|
1976
|
+
];
|
|
1977
|
+
|
|
1978
|
+
const type = layout ?? (suggest.length === 1 ? 'Single' : scroll ? 'HScroll' : 'ActionRow');
|
|
1979
|
+
|
|
1980
|
+
const section = AIRich.newLayout(type, type === 'Single' ? suggest[0] : suggest, {
|
|
1981
|
+
__typename: 'GenAIUnifiedResponseSection',
|
|
1982
|
+
});
|
|
1983
|
+
|
|
1984
|
+
const submessage = this.createAlert('GenAIFollowUpSuggestionPillPrimitive');
|
|
1985
|
+
|
|
1986
|
+
return this._addContent(section, submessage, {
|
|
1987
|
+
id,
|
|
1988
|
+
replace,
|
|
1989
|
+
insertAt,
|
|
1990
|
+
});
|
|
1991
|
+
}
|
|
1992
|
+
|
|
1993
|
+
async build(
|
|
1994
|
+
jid,
|
|
1995
|
+
{ bypassDownload = true, forwarded = true, notification = false, includesUnifiedResponse = true, includesSubmessages = true, quoted, quotedParticipant, messageId, ...options } = {}
|
|
1996
|
+
) {
|
|
1997
|
+
const forward = forwarded
|
|
1998
|
+
? {
|
|
1999
|
+
forwardingScore: 1,
|
|
2000
|
+
isForwarded: true,
|
|
2001
|
+
forwardedAiBotMessageInfo: { botJid: '867051314767696@bot' },
|
|
2002
|
+
forwardOrigin: 4,
|
|
2003
|
+
}
|
|
2004
|
+
: {};
|
|
2005
|
+
|
|
2006
|
+
const notif = notification
|
|
2007
|
+
? {
|
|
2008
|
+
sessionTransparencyMetadata: {
|
|
2009
|
+
disclaimerText: '~ Ahmad tumbuh kembang',
|
|
2010
|
+
hcaId: `hca_${Date.now()}`,
|
|
2011
|
+
sessionTransparencyType: 1,
|
|
2012
|
+
},
|
|
2013
|
+
}
|
|
2014
|
+
: {};
|
|
2015
|
+
|
|
2016
|
+
const qObj = quoted
|
|
2017
|
+
? {
|
|
2018
|
+
stanzaId: quoted?.key?.id || quoted?.id,
|
|
2019
|
+
participant: quotedParticipant || quoted?.key?.participant || quoted?.participant || quoted?.key?.remoteJid,
|
|
2020
|
+
quotedType: 0,
|
|
2021
|
+
quotedMessage: typeof quoted === 'object' && quoted !== null ? (quoted.message ?? quoted) : undefined,
|
|
2022
|
+
}
|
|
2023
|
+
: {};
|
|
2024
|
+
|
|
2025
|
+
const sections = this._footer
|
|
2026
|
+
? [
|
|
2027
|
+
...(await waitAllPromises(this._sections)),
|
|
2028
|
+
AIRich.newLayout('Single', {
|
|
2029
|
+
text: this._footer,
|
|
2030
|
+
__typename: 'GenAIMetadataTextPrimitive',
|
|
2031
|
+
}),
|
|
2032
|
+
]
|
|
2033
|
+
: [...(await waitAllPromises(this._sections))];
|
|
2034
|
+
|
|
2035
|
+
if (this._dynamic) {
|
|
2036
|
+
this.refreshResponseId();
|
|
2037
|
+
this.refreshBotResponseId();
|
|
2038
|
+
}
|
|
2039
|
+
|
|
2040
|
+
return generateWAMessageFromContent(
|
|
2041
|
+
jid,
|
|
2042
|
+
{
|
|
2043
|
+
messageContextInfo: {
|
|
2044
|
+
deviceListMetadata: {},
|
|
2045
|
+
deviceListMetadataVersion: 2,
|
|
2046
|
+
botMetadata: {
|
|
2047
|
+
messageDisclaimerText: this._title,
|
|
2048
|
+
...notif,
|
|
2049
|
+
verificationMetadata: AIRich.generateVerificationMetadata(),
|
|
2050
|
+
botResponseId: this._botResponseId,
|
|
2051
|
+
},
|
|
2052
|
+
},
|
|
2053
|
+
...this._extraPayload,
|
|
2054
|
+
botForwardedMessage: {
|
|
2055
|
+
message: {
|
|
2056
|
+
richResponseMessage: {
|
|
2057
|
+
messageType: 1,
|
|
2058
|
+
submessages: includesSubmessages ? await waitAllPromises(this._submessages) : [],
|
|
2059
|
+
unifiedResponse: {
|
|
2060
|
+
data: includesUnifiedResponse ? Buffer.from(Toolkit.stringifyEscaped({ response_id: this._responseId, sections })).toString('base64') : '',
|
|
2061
|
+
},
|
|
2062
|
+
contextInfo: {
|
|
2063
|
+
...forward,
|
|
2064
|
+
...qObj,
|
|
2065
|
+
...this._contextInfo,
|
|
2066
|
+
},
|
|
2067
|
+
},
|
|
2068
|
+
},
|
|
2069
|
+
},
|
|
2070
|
+
},
|
|
2071
|
+
{ messageId: messageId || generateMessageIDV2(), ...options }
|
|
2072
|
+
);
|
|
2073
|
+
}
|
|
2074
|
+
|
|
2075
|
+
async buildEdit(targetJid, targetId, { msg, messageId, ...options } = {}) {
|
|
2076
|
+
if (!msg) {
|
|
2077
|
+
msg = (await this.build(targetJid, options)).message;
|
|
2078
|
+
}
|
|
2079
|
+
|
|
2080
|
+
const editedMessage = msg;
|
|
2081
|
+
|
|
2082
|
+
if (!editedMessage) {
|
|
2083
|
+
throw new Error('buildEdit: msg does not contain botForwardedMessage');
|
|
2084
|
+
}
|
|
2085
|
+
|
|
2086
|
+
return generateWAMessageFromContent(
|
|
2087
|
+
targetJid,
|
|
2088
|
+
{
|
|
2089
|
+
botForwardedMessage: {
|
|
2090
|
+
message: {
|
|
2091
|
+
protocolMessage: {
|
|
2092
|
+
key: {
|
|
2093
|
+
remoteJid: targetJid,
|
|
2094
|
+
fromMe: true,
|
|
2095
|
+
id: targetId,
|
|
2096
|
+
},
|
|
2097
|
+
type: 14,
|
|
2098
|
+
editedMessage,
|
|
2099
|
+
},
|
|
2100
|
+
},
|
|
2101
|
+
},
|
|
2102
|
+
},
|
|
2103
|
+
{ messageId: messageId || generateMessageIDV2(), ...options }
|
|
2104
|
+
);
|
|
2105
|
+
}
|
|
2106
|
+
|
|
2107
|
+
async sendEdit(jid, id, { msg, messageId, additionalNodes = [], ...options } = {}) {
|
|
2108
|
+
jid = jid ?? this._lastMessageKey?.remoteJid;
|
|
2109
|
+
id = id ?? this._lastMessageKey?.id;
|
|
2110
|
+
|
|
2111
|
+
if (!jid) {
|
|
2112
|
+
throw new Error('JID is required');
|
|
2113
|
+
}
|
|
2114
|
+
|
|
2115
|
+
if (!id) {
|
|
2116
|
+
throw new Error('Message id is required');
|
|
2117
|
+
}
|
|
2118
|
+
|
|
2119
|
+
const msgEdit = await this.buildEdit(jid, id, {
|
|
2120
|
+
msg,
|
|
2121
|
+
messageId: messageId || generateMessageIDV2(),
|
|
2122
|
+
...options,
|
|
2123
|
+
});
|
|
2124
|
+
|
|
2125
|
+
await this.#client.relayMessage(jid, msgEdit.message, {
|
|
2126
|
+
messageId: msgEdit.key.id,
|
|
2127
|
+
additionalNodes,
|
|
2128
|
+
});
|
|
2129
|
+
|
|
2130
|
+
return msgEdit;
|
|
2131
|
+
}
|
|
2132
|
+
|
|
2133
|
+
async send(jid, { bypassDownload = true, forwarded = true, notification = false, includesUnifiedResponse = true, includesSubmessages = true, messageId, additionalNodes = [], ...options } = {}) {
|
|
2134
|
+
const msg = await this.build(jid, {
|
|
2135
|
+
forwarded,
|
|
2136
|
+
notification,
|
|
2137
|
+
includesUnifiedResponse,
|
|
2138
|
+
includesSubmessages,
|
|
2139
|
+
messageId,
|
|
2140
|
+
...options,
|
|
2141
|
+
});
|
|
2142
|
+
|
|
2143
|
+
await this.#client.relayMessage(msg.key.remoteJid, msg.message, {
|
|
2144
|
+
messageId: msg.key.id,
|
|
2145
|
+
additionalNodes,
|
|
2146
|
+
...options,
|
|
2147
|
+
});
|
|
2148
|
+
|
|
2149
|
+
if (includesUnifiedResponse && bypassDownload) {
|
|
2150
|
+
await this.sendEdit(jid, msg.key.id, {
|
|
2151
|
+
msg: msg.message,
|
|
2152
|
+
});
|
|
2153
|
+
}
|
|
2154
|
+
|
|
2155
|
+
this._lastMessageKey = msg.key;
|
|
2156
|
+
|
|
2157
|
+
return msg;
|
|
2158
|
+
}
|
|
2159
|
+
|
|
2160
|
+
static tokenizer(code, lang = 'javascript') {
|
|
2161
|
+
const keywordsMap = {
|
|
2162
|
+
javascript: new Set([
|
|
2163
|
+
'break',
|
|
2164
|
+
'case',
|
|
2165
|
+
'catch',
|
|
2166
|
+
'continue',
|
|
2167
|
+
'debugger',
|
|
2168
|
+
'delete',
|
|
2169
|
+
'do',
|
|
2170
|
+
'else',
|
|
2171
|
+
'finally',
|
|
2172
|
+
'for',
|
|
2173
|
+
'function',
|
|
2174
|
+
'if',
|
|
2175
|
+
'in',
|
|
2176
|
+
'instanceof',
|
|
2177
|
+
'new',
|
|
2178
|
+
'return',
|
|
2179
|
+
'switch',
|
|
2180
|
+
'this',
|
|
2181
|
+
'throw',
|
|
2182
|
+
'try',
|
|
2183
|
+
'typeof',
|
|
2184
|
+
'var',
|
|
2185
|
+
'void',
|
|
2186
|
+
'while',
|
|
2187
|
+
'with',
|
|
2188
|
+
'true',
|
|
2189
|
+
'false',
|
|
2190
|
+
'null',
|
|
2191
|
+
'undefined',
|
|
2192
|
+
'class',
|
|
2193
|
+
'const',
|
|
2194
|
+
'let',
|
|
2195
|
+
'super',
|
|
2196
|
+
'extends',
|
|
2197
|
+
'export',
|
|
2198
|
+
'import',
|
|
2199
|
+
'yield',
|
|
2200
|
+
'static',
|
|
2201
|
+
'constructor',
|
|
2202
|
+
'async',
|
|
2203
|
+
'await',
|
|
2204
|
+
'get',
|
|
2205
|
+
'set',
|
|
2206
|
+
]),
|
|
2207
|
+
|
|
2208
|
+
typescript: new Set([
|
|
2209
|
+
'abstract',
|
|
2210
|
+
'any',
|
|
2211
|
+
'as',
|
|
2212
|
+
'asserts',
|
|
2213
|
+
'bigint',
|
|
2214
|
+
'boolean',
|
|
2215
|
+
'declare',
|
|
2216
|
+
'enum',
|
|
2217
|
+
'implements',
|
|
2218
|
+
'infer',
|
|
2219
|
+
'interface',
|
|
2220
|
+
'is',
|
|
2221
|
+
'keyof',
|
|
2222
|
+
'module',
|
|
2223
|
+
'namespace',
|
|
2224
|
+
'never',
|
|
2225
|
+
'readonly',
|
|
2226
|
+
'require',
|
|
2227
|
+
'number',
|
|
2228
|
+
'object',
|
|
2229
|
+
'override',
|
|
2230
|
+
'private',
|
|
2231
|
+
'protected',
|
|
2232
|
+
'public',
|
|
2233
|
+
'satisfies',
|
|
2234
|
+
'string',
|
|
2235
|
+
'symbol',
|
|
2236
|
+
'type',
|
|
2237
|
+
'unknown',
|
|
2238
|
+
'using',
|
|
2239
|
+
'from',
|
|
2240
|
+
'break',
|
|
2241
|
+
'case',
|
|
2242
|
+
'catch',
|
|
2243
|
+
'continue',
|
|
2244
|
+
'do',
|
|
2245
|
+
'else',
|
|
2246
|
+
'finally',
|
|
2247
|
+
'for',
|
|
2248
|
+
'function',
|
|
2249
|
+
'if',
|
|
2250
|
+
'new',
|
|
2251
|
+
'return',
|
|
2252
|
+
'switch',
|
|
2253
|
+
'this',
|
|
2254
|
+
'throw',
|
|
2255
|
+
'try',
|
|
2256
|
+
'var',
|
|
2257
|
+
'void',
|
|
2258
|
+
'while',
|
|
2259
|
+
'class',
|
|
2260
|
+
'const',
|
|
2261
|
+
'let',
|
|
2262
|
+
'extends',
|
|
2263
|
+
'import',
|
|
2264
|
+
'export',
|
|
2265
|
+
'async',
|
|
2266
|
+
'await',
|
|
2267
|
+
]),
|
|
2268
|
+
|
|
2269
|
+
python: new Set([
|
|
2270
|
+
'False',
|
|
2271
|
+
'None',
|
|
2272
|
+
'True',
|
|
2273
|
+
'and',
|
|
2274
|
+
'as',
|
|
2275
|
+
'assert',
|
|
2276
|
+
'async',
|
|
2277
|
+
'await',
|
|
2278
|
+
'break',
|
|
2279
|
+
'class',
|
|
2280
|
+
'continue',
|
|
2281
|
+
'def',
|
|
2282
|
+
'del',
|
|
2283
|
+
'elif',
|
|
2284
|
+
'else',
|
|
2285
|
+
'except',
|
|
2286
|
+
'finally',
|
|
2287
|
+
'for',
|
|
2288
|
+
'from',
|
|
2289
|
+
'global',
|
|
2290
|
+
'if',
|
|
2291
|
+
'import',
|
|
2292
|
+
'in',
|
|
2293
|
+
'is',
|
|
2294
|
+
'lambda',
|
|
2295
|
+
'nonlocal',
|
|
2296
|
+
'not',
|
|
2297
|
+
'or',
|
|
2298
|
+
'pass',
|
|
2299
|
+
'raise',
|
|
2300
|
+
'return',
|
|
2301
|
+
'try',
|
|
2302
|
+
'while',
|
|
2303
|
+
'with',
|
|
2304
|
+
'yield',
|
|
2305
|
+
]),
|
|
2306
|
+
|
|
2307
|
+
java: new Set([
|
|
2308
|
+
'abstract',
|
|
2309
|
+
'assert',
|
|
2310
|
+
'boolean',
|
|
2311
|
+
'break',
|
|
2312
|
+
'byte',
|
|
2313
|
+
'case',
|
|
2314
|
+
'catch',
|
|
2315
|
+
'char',
|
|
2316
|
+
'class',
|
|
2317
|
+
'const',
|
|
2318
|
+
'continue',
|
|
2319
|
+
'default',
|
|
2320
|
+
'do',
|
|
2321
|
+
'double',
|
|
2322
|
+
'else',
|
|
2323
|
+
'enum',
|
|
2324
|
+
'extends',
|
|
2325
|
+
'final',
|
|
2326
|
+
'finally',
|
|
2327
|
+
'float',
|
|
2328
|
+
'for',
|
|
2329
|
+
'goto',
|
|
2330
|
+
'if',
|
|
2331
|
+
'implements',
|
|
2332
|
+
'import',
|
|
2333
|
+
'instanceof',
|
|
2334
|
+
'int',
|
|
2335
|
+
'interface',
|
|
2336
|
+
'long',
|
|
2337
|
+
'native',
|
|
2338
|
+
'new',
|
|
2339
|
+
'package',
|
|
2340
|
+
'private',
|
|
2341
|
+
'protected',
|
|
2342
|
+
'public',
|
|
2343
|
+
'return',
|
|
2344
|
+
'short',
|
|
2345
|
+
'static',
|
|
2346
|
+
'strictfp',
|
|
2347
|
+
'super',
|
|
2348
|
+
'switch',
|
|
2349
|
+
'synchronized',
|
|
2350
|
+
'this',
|
|
2351
|
+
'throw',
|
|
2352
|
+
'throws',
|
|
2353
|
+
'transient',
|
|
2354
|
+
'try',
|
|
2355
|
+
'void',
|
|
2356
|
+
'volatile',
|
|
2357
|
+
'while',
|
|
2358
|
+
]),
|
|
2359
|
+
|
|
2360
|
+
golang: new Set([
|
|
2361
|
+
'break',
|
|
2362
|
+
'case',
|
|
2363
|
+
'chan',
|
|
2364
|
+
'const',
|
|
2365
|
+
'continue',
|
|
2366
|
+
'default',
|
|
2367
|
+
'defer',
|
|
2368
|
+
'else',
|
|
2369
|
+
'fallthrough',
|
|
2370
|
+
'for',
|
|
2371
|
+
'func',
|
|
2372
|
+
'go',
|
|
2373
|
+
'goto',
|
|
2374
|
+
'if',
|
|
2375
|
+
'import',
|
|
2376
|
+
'interface',
|
|
2377
|
+
'map',
|
|
2378
|
+
'package',
|
|
2379
|
+
'range',
|
|
2380
|
+
'return',
|
|
2381
|
+
'select',
|
|
2382
|
+
'struct',
|
|
2383
|
+
'switch',
|
|
2384
|
+
'type',
|
|
2385
|
+
'var',
|
|
2386
|
+
]),
|
|
2387
|
+
|
|
2388
|
+
c: new Set([
|
|
2389
|
+
'auto',
|
|
2390
|
+
'break',
|
|
2391
|
+
'case',
|
|
2392
|
+
'char',
|
|
2393
|
+
'const',
|
|
2394
|
+
'continue',
|
|
2395
|
+
'default',
|
|
2396
|
+
'do',
|
|
2397
|
+
'double',
|
|
2398
|
+
'else',
|
|
2399
|
+
'enum',
|
|
2400
|
+
'extern',
|
|
2401
|
+
'float',
|
|
2402
|
+
'for',
|
|
2403
|
+
'goto',
|
|
2404
|
+
'if',
|
|
2405
|
+
'int',
|
|
2406
|
+
'long',
|
|
2407
|
+
'register',
|
|
2408
|
+
'return',
|
|
2409
|
+
'short',
|
|
2410
|
+
'signed',
|
|
2411
|
+
'sizeof',
|
|
2412
|
+
'static',
|
|
2413
|
+
'struct',
|
|
2414
|
+
'switch',
|
|
2415
|
+
'typedef',
|
|
2416
|
+
'union',
|
|
2417
|
+
'unsigned',
|
|
2418
|
+
'void',
|
|
2419
|
+
'volatile',
|
|
2420
|
+
'while',
|
|
2421
|
+
]),
|
|
2422
|
+
|
|
2423
|
+
cpp: new Set([
|
|
2424
|
+
'alignas',
|
|
2425
|
+
'alignof',
|
|
2426
|
+
'and',
|
|
2427
|
+
'auto',
|
|
2428
|
+
'bool',
|
|
2429
|
+
'break',
|
|
2430
|
+
'case',
|
|
2431
|
+
'catch',
|
|
2432
|
+
'class',
|
|
2433
|
+
'const',
|
|
2434
|
+
'constexpr',
|
|
2435
|
+
'continue',
|
|
2436
|
+
'delete',
|
|
2437
|
+
'do',
|
|
2438
|
+
'double',
|
|
2439
|
+
'else',
|
|
2440
|
+
'enum',
|
|
2441
|
+
'explicit',
|
|
2442
|
+
'export',
|
|
2443
|
+
'extern',
|
|
2444
|
+
'false',
|
|
2445
|
+
'float',
|
|
2446
|
+
'for',
|
|
2447
|
+
'friend',
|
|
2448
|
+
'if',
|
|
2449
|
+
'inline',
|
|
2450
|
+
'int',
|
|
2451
|
+
'long',
|
|
2452
|
+
'mutable',
|
|
2453
|
+
'namespace',
|
|
2454
|
+
'new',
|
|
2455
|
+
'noexcept',
|
|
2456
|
+
'nullptr',
|
|
2457
|
+
'operator',
|
|
2458
|
+
'private',
|
|
2459
|
+
'protected',
|
|
2460
|
+
'public',
|
|
2461
|
+
'return',
|
|
2462
|
+
'short',
|
|
2463
|
+
'signed',
|
|
2464
|
+
'sizeof',
|
|
2465
|
+
'static',
|
|
2466
|
+
'struct',
|
|
2467
|
+
'switch',
|
|
2468
|
+
'template',
|
|
2469
|
+
'this',
|
|
2470
|
+
'throw',
|
|
2471
|
+
'true',
|
|
2472
|
+
'try',
|
|
2473
|
+
'typedef',
|
|
2474
|
+
'typename',
|
|
2475
|
+
'union',
|
|
2476
|
+
'unsigned',
|
|
2477
|
+
'using',
|
|
2478
|
+
'virtual',
|
|
2479
|
+
'void',
|
|
2480
|
+
'while',
|
|
2481
|
+
]),
|
|
2482
|
+
|
|
2483
|
+
php: new Set([
|
|
2484
|
+
'abstract',
|
|
2485
|
+
'and',
|
|
2486
|
+
'array',
|
|
2487
|
+
'as',
|
|
2488
|
+
'break',
|
|
2489
|
+
'callable',
|
|
2490
|
+
'case',
|
|
2491
|
+
'catch',
|
|
2492
|
+
'class',
|
|
2493
|
+
'clone',
|
|
2494
|
+
'const',
|
|
2495
|
+
'continue',
|
|
2496
|
+
'declare',
|
|
2497
|
+
'default',
|
|
2498
|
+
'do',
|
|
2499
|
+
'echo',
|
|
2500
|
+
'else',
|
|
2501
|
+
'elseif',
|
|
2502
|
+
'empty',
|
|
2503
|
+
'enddeclare',
|
|
2504
|
+
'endfor',
|
|
2505
|
+
'endforeach',
|
|
2506
|
+
'endif',
|
|
2507
|
+
'endswitch',
|
|
2508
|
+
'endwhile',
|
|
2509
|
+
'extends',
|
|
2510
|
+
'final',
|
|
2511
|
+
'finally',
|
|
2512
|
+
'fn',
|
|
2513
|
+
'for',
|
|
2514
|
+
'foreach',
|
|
2515
|
+
'function',
|
|
2516
|
+
'global',
|
|
2517
|
+
'goto',
|
|
2518
|
+
'if',
|
|
2519
|
+
'implements',
|
|
2520
|
+
'include',
|
|
2521
|
+
'include_once',
|
|
2522
|
+
'instanceof',
|
|
2523
|
+
'interface',
|
|
2524
|
+
'match',
|
|
2525
|
+
'namespace',
|
|
2526
|
+
'new',
|
|
2527
|
+
'null',
|
|
2528
|
+
'or',
|
|
2529
|
+
'private',
|
|
2530
|
+
'protected',
|
|
2531
|
+
'public',
|
|
2532
|
+
'require',
|
|
2533
|
+
'require_once',
|
|
2534
|
+
'return',
|
|
2535
|
+
'static',
|
|
2536
|
+
'switch',
|
|
2537
|
+
'throw',
|
|
2538
|
+
'trait',
|
|
2539
|
+
'try',
|
|
2540
|
+
'use',
|
|
2541
|
+
'var',
|
|
2542
|
+
'while',
|
|
2543
|
+
'yield',
|
|
2544
|
+
]),
|
|
2545
|
+
|
|
2546
|
+
rust: new Set([
|
|
2547
|
+
'as',
|
|
2548
|
+
'break',
|
|
2549
|
+
'const',
|
|
2550
|
+
'continue',
|
|
2551
|
+
'crate',
|
|
2552
|
+
'else',
|
|
2553
|
+
'enum',
|
|
2554
|
+
'extern',
|
|
2555
|
+
'false',
|
|
2556
|
+
'fn',
|
|
2557
|
+
'for',
|
|
2558
|
+
'if',
|
|
2559
|
+
'impl',
|
|
2560
|
+
'in',
|
|
2561
|
+
'let',
|
|
2562
|
+
'loop',
|
|
2563
|
+
'match',
|
|
2564
|
+
'mod',
|
|
2565
|
+
'move',
|
|
2566
|
+
'mut',
|
|
2567
|
+
'pub',
|
|
2568
|
+
'ref',
|
|
2569
|
+
'return',
|
|
2570
|
+
'self',
|
|
2571
|
+
'Self',
|
|
2572
|
+
'static',
|
|
2573
|
+
'struct',
|
|
2574
|
+
'super',
|
|
2575
|
+
'trait',
|
|
2576
|
+
'true',
|
|
2577
|
+
'type',
|
|
2578
|
+
'unsafe',
|
|
2579
|
+
'use',
|
|
2580
|
+
'where',
|
|
2581
|
+
'while',
|
|
2582
|
+
]),
|
|
2583
|
+
|
|
2584
|
+
html: new Set([
|
|
2585
|
+
'html',
|
|
2586
|
+
'head',
|
|
2587
|
+
'body',
|
|
2588
|
+
'div',
|
|
2589
|
+
'span',
|
|
2590
|
+
'p',
|
|
2591
|
+
'a',
|
|
2592
|
+
'img',
|
|
2593
|
+
'video',
|
|
2594
|
+
'audio',
|
|
2595
|
+
'script',
|
|
2596
|
+
'style',
|
|
2597
|
+
'link',
|
|
2598
|
+
'meta',
|
|
2599
|
+
'form',
|
|
2600
|
+
'input',
|
|
2601
|
+
'button',
|
|
2602
|
+
'table',
|
|
2603
|
+
'tr',
|
|
2604
|
+
'td',
|
|
2605
|
+
'th',
|
|
2606
|
+
'ul',
|
|
2607
|
+
'ol',
|
|
2608
|
+
'li',
|
|
2609
|
+
'section',
|
|
2610
|
+
'article',
|
|
2611
|
+
'header',
|
|
2612
|
+
'footer',
|
|
2613
|
+
'nav',
|
|
2614
|
+
'main',
|
|
2615
|
+
]),
|
|
2616
|
+
|
|
2617
|
+
bash: new Set([
|
|
2618
|
+
'if',
|
|
2619
|
+
'then',
|
|
2620
|
+
'else',
|
|
2621
|
+
'elif',
|
|
2622
|
+
'fi',
|
|
2623
|
+
'for',
|
|
2624
|
+
'while',
|
|
2625
|
+
'do',
|
|
2626
|
+
'done',
|
|
2627
|
+
'case',
|
|
2628
|
+
'esac',
|
|
2629
|
+
'function',
|
|
2630
|
+
'in',
|
|
2631
|
+
'select',
|
|
2632
|
+
'until',
|
|
2633
|
+
'break',
|
|
2634
|
+
'continue',
|
|
2635
|
+
'return',
|
|
2636
|
+
'export',
|
|
2637
|
+
'readonly',
|
|
2638
|
+
'local',
|
|
2639
|
+
'declare',
|
|
2640
|
+
]),
|
|
2641
|
+
|
|
2642
|
+
markdown: new Set(['#', '##', '###', '####', '#####', '######']),
|
|
2643
|
+
};
|
|
2644
|
+
|
|
2645
|
+
if (!lang || lang === 'txt' || lang === 'text' || lang === 'plaintext') {
|
|
2646
|
+
return {
|
|
2647
|
+
codeBlock: [
|
|
2648
|
+
{
|
|
2649
|
+
codeContent: code,
|
|
2650
|
+
highlightType: 0,
|
|
2651
|
+
},
|
|
2652
|
+
],
|
|
2653
|
+
unified_codeBlock: [
|
|
2654
|
+
{
|
|
2655
|
+
content: code,
|
|
2656
|
+
type: 'DEFAULT',
|
|
2657
|
+
},
|
|
2658
|
+
],
|
|
2659
|
+
};
|
|
2660
|
+
}
|
|
2661
|
+
|
|
2662
|
+
const TYPE_MAP = {
|
|
2663
|
+
0: 'DEFAULT',
|
|
2664
|
+
1: 'KEYWORD',
|
|
2665
|
+
2: 'METHOD',
|
|
2666
|
+
3: 'STR',
|
|
2667
|
+
4: 'NUMBER',
|
|
2668
|
+
5: 'COMMENT',
|
|
2669
|
+
};
|
|
2670
|
+
|
|
2671
|
+
const keywords = keywordsMap[lang.toLowerCase()] || new Set();
|
|
2672
|
+
const tokens = [];
|
|
2673
|
+
|
|
2674
|
+
let i = 0;
|
|
2675
|
+
|
|
2676
|
+
const push = (content, type) => {
|
|
2677
|
+
if (!content) return;
|
|
2678
|
+
|
|
2679
|
+
const last = tokens[tokens.length - 1];
|
|
2680
|
+
|
|
2681
|
+
if (last && last.highlightType === type) {
|
|
2682
|
+
last.codeContent += content;
|
|
2683
|
+
} else {
|
|
2684
|
+
tokens.push({
|
|
2685
|
+
codeContent: content,
|
|
2686
|
+
highlightType: type,
|
|
2687
|
+
});
|
|
2688
|
+
}
|
|
2689
|
+
};
|
|
2690
|
+
|
|
2691
|
+
const isIdentifier = (char) => {
|
|
2692
|
+
switch (lang.toLowerCase()) {
|
|
2693
|
+
case 'css':
|
|
2694
|
+
return /[a-zA-Z0-9_$-]/.test(char);
|
|
2695
|
+
|
|
2696
|
+
case 'html':
|
|
2697
|
+
return /[a-zA-Z0-9_$:-]/.test(char);
|
|
2698
|
+
|
|
2699
|
+
default:
|
|
2700
|
+
return /[a-zA-Z0-9_$]/.test(char);
|
|
2701
|
+
}
|
|
2702
|
+
};
|
|
2703
|
+
|
|
2704
|
+
while (i < code.length) {
|
|
2705
|
+
const c = code[i];
|
|
2706
|
+
|
|
2707
|
+
if (/\s/.test(c)) {
|
|
2708
|
+
let s = i;
|
|
2709
|
+
|
|
2710
|
+
while (i < code.length && /\s/.test(code[i])) {
|
|
2711
|
+
i++;
|
|
2712
|
+
}
|
|
2713
|
+
|
|
2714
|
+
push(code.slice(s, i), 0);
|
|
2715
|
+
continue;
|
|
2716
|
+
}
|
|
2717
|
+
|
|
2718
|
+
if ((c === '/' && code[i + 1] === '/') || (c === '#' && ['python', 'bash'].includes(lang))) {
|
|
2719
|
+
let s = i;
|
|
2720
|
+
|
|
2721
|
+
while (i < code.length && code[i] !== '\n') {
|
|
2722
|
+
i++;
|
|
2723
|
+
}
|
|
2724
|
+
|
|
2725
|
+
push(code.slice(s, i), 5);
|
|
2726
|
+
continue;
|
|
2727
|
+
}
|
|
2728
|
+
|
|
2729
|
+
if (c === '"' || c === "'" || c === '`') {
|
|
2730
|
+
let s = i;
|
|
2731
|
+
const q = c;
|
|
2732
|
+
|
|
2733
|
+
i++;
|
|
2734
|
+
|
|
2735
|
+
while (i < code.length) {
|
|
2736
|
+
if (code[i] === '\\' && i + 1 < code.length) {
|
|
2737
|
+
i += 2;
|
|
2738
|
+
} else if (code[i] === q) {
|
|
2739
|
+
i++;
|
|
2740
|
+
break;
|
|
2741
|
+
} else {
|
|
2742
|
+
i++;
|
|
2743
|
+
}
|
|
2744
|
+
}
|
|
2745
|
+
|
|
2746
|
+
push(code.slice(s, i), 3);
|
|
2747
|
+
continue;
|
|
2748
|
+
}
|
|
2749
|
+
|
|
2750
|
+
if (/[0-9]/.test(c)) {
|
|
2751
|
+
let s = i;
|
|
2752
|
+
|
|
2753
|
+
while (i < code.length && /[0-9._]/.test(code[i])) {
|
|
2754
|
+
i++;
|
|
2755
|
+
}
|
|
2756
|
+
|
|
2757
|
+
push(code.slice(s, i), 4);
|
|
2758
|
+
continue;
|
|
2759
|
+
}
|
|
2760
|
+
|
|
2761
|
+
if (/[a-zA-Z_$]/.test(c)) {
|
|
2762
|
+
let s = i;
|
|
2763
|
+
|
|
2764
|
+
while (i < code.length && isIdentifier(code[i])) {
|
|
2765
|
+
i++;
|
|
2766
|
+
}
|
|
2767
|
+
|
|
2768
|
+
const word = code.slice(s, i);
|
|
2769
|
+
|
|
2770
|
+
let type = 0;
|
|
2771
|
+
|
|
2772
|
+
if (keywords.has(word)) {
|
|
2773
|
+
type = 1;
|
|
2774
|
+
} else if (lang === 'css') {
|
|
2775
|
+
let j = i;
|
|
2776
|
+
|
|
2777
|
+
while (j < code.length && /\s/.test(code[j])) {
|
|
2778
|
+
j++;
|
|
2779
|
+
}
|
|
2780
|
+
|
|
2781
|
+
if (code[j] === ':') {
|
|
2782
|
+
type = 1;
|
|
2783
|
+
}
|
|
2784
|
+
} else if (lang === 'html') {
|
|
2785
|
+
let p = s - 1;
|
|
2786
|
+
|
|
2787
|
+
while (p >= 0 && /\s/.test(code[p])) {
|
|
2788
|
+
p--;
|
|
2789
|
+
}
|
|
2790
|
+
|
|
2791
|
+
if (code[p] === '<' || (code[p] === '/' && code[p - 1] === '<')) {
|
|
2792
|
+
type = 1;
|
|
2793
|
+
}
|
|
2794
|
+
}
|
|
2795
|
+
|
|
2796
|
+
if (type === 0) {
|
|
2797
|
+
let j = i;
|
|
2798
|
+
|
|
2799
|
+
while (j < code.length && /\s/.test(code[j])) {
|
|
2800
|
+
j++;
|
|
2801
|
+
}
|
|
2802
|
+
|
|
2803
|
+
if (code[j] === '(') {
|
|
2804
|
+
type = 2;
|
|
2805
|
+
}
|
|
2806
|
+
}
|
|
2807
|
+
|
|
2808
|
+
push(word, type);
|
|
2809
|
+
continue;
|
|
2810
|
+
}
|
|
2811
|
+
|
|
2812
|
+
push(c, 0);
|
|
2813
|
+
i++;
|
|
2814
|
+
}
|
|
2815
|
+
|
|
2816
|
+
return {
|
|
2817
|
+
codeBlock: tokens,
|
|
2818
|
+
unified_codeBlock: tokens.map((t) => ({
|
|
2819
|
+
content: t.codeContent,
|
|
2820
|
+
type: TYPE_MAP[t.highlightType],
|
|
2821
|
+
})),
|
|
2822
|
+
};
|
|
2823
|
+
}
|
|
2824
|
+
|
|
2825
|
+
static toTableMetadata(arr, { hyperlink = true, citation = true, latex = true } = {}) {
|
|
2826
|
+
if (!Array.isArray(arr) || !arr.every((row) => Array.isArray(row) && row.every((cell) => typeof cell === 'string'))) {
|
|
2827
|
+
throw new TypeError('Table must be a nested array of strings');
|
|
2828
|
+
}
|
|
2829
|
+
|
|
2830
|
+
const [header, ...rows] = arr;
|
|
2831
|
+
|
|
2832
|
+
const maxLen = Math.max(header.length, ...rows.map((r) => r.length));
|
|
2833
|
+
|
|
2834
|
+
const normalize = (r) => [...r, ...Array(maxLen - r.length).fill('')];
|
|
2835
|
+
|
|
2836
|
+
const unified_rows = [
|
|
2837
|
+
{
|
|
2838
|
+
is_header: true,
|
|
2839
|
+
cells: normalize(header),
|
|
2840
|
+
},
|
|
2841
|
+
...rows.map((r) => ({
|
|
2842
|
+
is_header: false,
|
|
2843
|
+
cells: normalize(r),
|
|
2844
|
+
})),
|
|
2845
|
+
].map((row) => {
|
|
2846
|
+
const markdown_cells = row.cells.map((cell) => {
|
|
2847
|
+
const extracted = extractIE(cell, { hyperlink, citation, latex });
|
|
2848
|
+
|
|
2849
|
+
return {
|
|
2850
|
+
text: extracted.text,
|
|
2851
|
+
...(extracted.inline_entities.length ? { inline_entities: extracted.inline_entities } : {}),
|
|
2852
|
+
};
|
|
2853
|
+
});
|
|
2854
|
+
|
|
2855
|
+
return {
|
|
2856
|
+
...row,
|
|
2857
|
+
...(markdown_cells.some((c) => c.inline_entities?.length) ? { markdown_cells } : {}),
|
|
2858
|
+
};
|
|
2859
|
+
});
|
|
2860
|
+
|
|
2861
|
+
const rowsMeta = unified_rows.map((r) => ({
|
|
2862
|
+
items: r.cells,
|
|
2863
|
+
...(r.is_header ? { isHeading: true } : {}),
|
|
2864
|
+
}));
|
|
2865
|
+
|
|
2866
|
+
return {
|
|
2867
|
+
title: '',
|
|
2868
|
+
rows: rowsMeta,
|
|
2869
|
+
unified_rows,
|
|
2870
|
+
};
|
|
2871
|
+
}
|
|
2872
|
+
|
|
2873
|
+
static generateVerificationMetadata() {
|
|
2874
|
+
const signatureMaterial = Buffer.from(
|
|
2875
|
+
`\u004E\u0049\u0058\u0045\u004C\u002E\u004D\u0065\u0073\u0073\u0061\u0067\u0065\u0042\u0075\u0069\u006C\u0064\u0065\u0072\u0056${VERSION}\u002D\u0056\u0065\u0072\u0069\u0066\u0069\u0063\u0061\u0074\u0069\u006F\u006E\u0053\u0069\u0067\u006E\u0061\u0074\u0075\u0072\u0065\u002E\u004D\u0065\u0074\u0061\u0064\u0061\u0074\u0061`
|
|
2876
|
+
);
|
|
2877
|
+
|
|
2878
|
+
const certificateMaterial = Buffer.from(
|
|
2879
|
+
`\u004E\u0049\u0058\u0045\u004C\u002E\u004D\u0065\u0073\u0073\u0061\u0067\u0065\u0042\u0075\u0069\u006C\u0064\u0065\u0072\u0056${VERSION}\u002D\u0043\u0065\u0072\u0074\u0069\u0066\u0069\u0063\u0061\u0074\u0065\u0043\u0068\u0061\u0069\u006E\u002E\u004D\u0065\u0074\u0061\u0064\u0061\u0074\u0061`
|
|
2880
|
+
);
|
|
2881
|
+
|
|
2882
|
+
const signature = Buffer.concat([signatureMaterial, crypto.randomBytes(64 - signatureMaterial.length)]).toString('base64');
|
|
2883
|
+
|
|
2884
|
+
const certificateChain = [
|
|
2885
|
+
Buffer.concat([certificateMaterial, crypto.randomBytes(684 - certificateMaterial.length)]).toString('base64'),
|
|
2886
|
+
|
|
2887
|
+
Buffer.concat([certificateMaterial, crypto.randomBytes(892 - certificateMaterial.length)]).toString('base64'),
|
|
2888
|
+
];
|
|
2889
|
+
|
|
2890
|
+
return {
|
|
2891
|
+
proofs: [
|
|
2892
|
+
{
|
|
2893
|
+
version: 1,
|
|
2894
|
+
useCase: 1,
|
|
2895
|
+
signature,
|
|
2896
|
+
certificateChain,
|
|
2897
|
+
},
|
|
2898
|
+
],
|
|
2899
|
+
};
|
|
2900
|
+
}
|
|
2901
|
+
|
|
2902
|
+
static newLayout(name, data, extra = {}) {
|
|
2903
|
+
return {
|
|
2904
|
+
...extra,
|
|
2905
|
+
view_model: {
|
|
2906
|
+
[Array.isArray(data) ? 'primitives' : 'primitive']: data,
|
|
2907
|
+
__typename: `GenAI${name}LayoutViewModel`,
|
|
2908
|
+
},
|
|
2909
|
+
};
|
|
2910
|
+
}
|
|
2911
|
+
|
|
2912
|
+
_makeNode(id, section, submessage) {
|
|
2913
|
+
return { id: id ?? null, section: section ?? null, submessage: submessage ?? null };
|
|
2914
|
+
}
|
|
2915
|
+
|
|
2916
|
+
_registerId(node, id) {
|
|
2917
|
+
if (id === undefined || id === null || id === '') return;
|
|
2918
|
+
|
|
2919
|
+
if (typeof id !== 'string') {
|
|
2920
|
+
throw new ContentValidationError('Item id must be a string', { id });
|
|
2921
|
+
}
|
|
2922
|
+
|
|
2923
|
+
if (this._idIndex.has(id)) {
|
|
2924
|
+
throw new DuplicateIdError(id);
|
|
2925
|
+
}
|
|
2926
|
+
|
|
2927
|
+
node.id = id;
|
|
2928
|
+
this._idIndex.set(id, node);
|
|
2929
|
+
}
|
|
2930
|
+
|
|
2931
|
+
_unregisterId(node) {
|
|
2932
|
+
if (node.id && this._idIndex.get(node.id) === node) {
|
|
2933
|
+
this._idIndex.delete(node.id);
|
|
2934
|
+
}
|
|
2935
|
+
}
|
|
2936
|
+
|
|
2937
|
+
hasId(id) {
|
|
2938
|
+
return typeof id === 'string' && this._idIndex.has(id);
|
|
2939
|
+
}
|
|
2940
|
+
|
|
2941
|
+
getIds() {
|
|
2942
|
+
return [...this._idIndex.keys()];
|
|
2943
|
+
}
|
|
2944
|
+
|
|
2945
|
+
peek(id) {
|
|
2946
|
+
const node = this._idIndex.get(id);
|
|
2947
|
+
|
|
2948
|
+
if (!node) return null;
|
|
2949
|
+
|
|
2950
|
+
return {
|
|
2951
|
+
id: node.id,
|
|
2952
|
+
section: node.section,
|
|
2953
|
+
submessage: node.submessage,
|
|
2954
|
+
};
|
|
2955
|
+
}
|
|
2956
|
+
|
|
2957
|
+
assignId(index, id) {
|
|
2958
|
+
if (!Number.isInteger(index) || index < 0 || index >= this._nodes.length) {
|
|
2959
|
+
throw new InvalidTargetError(`Node index ${index} is out of range (0-${this._nodes.length - 1})`, { index });
|
|
2960
|
+
}
|
|
2961
|
+
|
|
2962
|
+
const node = this._nodes[index];
|
|
2963
|
+
|
|
2964
|
+
if (node.id) {
|
|
2965
|
+
throw new AIRichError(`Node at index ${index} already has id "${node.id}"`, 'ALREADY_HAS_ID', { index, id: node.id });
|
|
2966
|
+
}
|
|
2967
|
+
|
|
2968
|
+
this._registerId(node, id);
|
|
2969
|
+
|
|
2970
|
+
return this;
|
|
2971
|
+
}
|
|
2972
|
+
|
|
2973
|
+
_getNode(id) {
|
|
2974
|
+
if (typeof id !== 'string' || !id) {
|
|
2975
|
+
throw new ContentValidationError('Item id must be a non-empty string', { id });
|
|
2976
|
+
}
|
|
2977
|
+
|
|
2978
|
+
const node = this._idIndex.get(id);
|
|
2979
|
+
|
|
2980
|
+
if (!node) {
|
|
2981
|
+
throw new ItemNotFoundError(id, this.getIds());
|
|
2982
|
+
}
|
|
2983
|
+
|
|
2984
|
+
return node;
|
|
2985
|
+
}
|
|
2986
|
+
|
|
2987
|
+
_resolveTarget(target) {
|
|
2988
|
+
if (Array.isArray(target)) {
|
|
2989
|
+
if (target.length < 1 || target.length > 2) {
|
|
2990
|
+
throw new ContentValidationError('Target must be id or [id, offset]', { target });
|
|
2991
|
+
}
|
|
2992
|
+
|
|
2993
|
+
const [id, offset = 0] = target;
|
|
2994
|
+
|
|
2995
|
+
if (typeof id !== 'string' || !id) {
|
|
2996
|
+
throw new ContentValidationError('Target id must be a non-empty string', { target });
|
|
2997
|
+
}
|
|
2998
|
+
|
|
2999
|
+
if (!Number.isInteger(offset)) {
|
|
3000
|
+
throw new ContentValidationError('Offset must be an integer', { target });
|
|
3001
|
+
}
|
|
3002
|
+
|
|
3003
|
+
return { id, offset };
|
|
3004
|
+
}
|
|
3005
|
+
|
|
3006
|
+
if (typeof target !== 'string' || !target) {
|
|
3007
|
+
throw new ContentValidationError('Target must be a non-empty id or [id, offset]', { target });
|
|
3008
|
+
}
|
|
3009
|
+
|
|
3010
|
+
return { id: target, offset: 0 };
|
|
3011
|
+
}
|
|
3012
|
+
|
|
3013
|
+
_resolveNodeIndex(target) {
|
|
3014
|
+
const { id, offset } = this._resolveTarget(target);
|
|
3015
|
+
const node = this._getNode(id);
|
|
3016
|
+
const baseIndex = this._nodes.indexOf(node);
|
|
3017
|
+
|
|
3018
|
+
if (baseIndex === -1) {
|
|
3019
|
+
throw new InvalidTargetError(`Item id "${id}" is registered but not present in the node list (internal desync)`, { id });
|
|
3020
|
+
}
|
|
3021
|
+
|
|
3022
|
+
const index = baseIndex + offset;
|
|
3023
|
+
|
|
3024
|
+
if (index < 0 || index >= this._nodes.length) {
|
|
3025
|
+
throw new InvalidTargetError(`Target "${id}" with offset ${offset} resolves to index ${index}, which is out of range (0-${this._nodes.length - 1})`, { id, offset, index });
|
|
3026
|
+
}
|
|
3027
|
+
|
|
3028
|
+
return { id, offset, baseIndex, index };
|
|
3029
|
+
}
|
|
3030
|
+
|
|
3031
|
+
_validateSections(section) {
|
|
3032
|
+
const items = Array.isArray(section) ? section : [section];
|
|
3033
|
+
|
|
3034
|
+
if (!items.length) {
|
|
3035
|
+
throw new ContentValidationError('At least one section is required');
|
|
3036
|
+
}
|
|
3037
|
+
|
|
3038
|
+
for (const item of items) {
|
|
3039
|
+
if (!item || typeof item !== 'object' || Array.isArray(item)) {
|
|
3040
|
+
throw new ContentValidationError('Sections must be plain objects');
|
|
3041
|
+
}
|
|
3042
|
+
}
|
|
3043
|
+
|
|
3044
|
+
return items;
|
|
3045
|
+
}
|
|
3046
|
+
|
|
3047
|
+
_validateSubmessages(submessage) {
|
|
3048
|
+
if (submessage === undefined || submessage === null) {
|
|
3049
|
+
return [];
|
|
3050
|
+
}
|
|
3051
|
+
|
|
3052
|
+
const items = Array.isArray(submessage) ? submessage : [submessage];
|
|
3053
|
+
|
|
3054
|
+
for (const item of items) {
|
|
3055
|
+
if (!item || typeof item !== 'object' || Array.isArray(item)) {
|
|
3056
|
+
throw new ContentValidationError('Submessages must be plain objects');
|
|
3057
|
+
}
|
|
3058
|
+
}
|
|
3059
|
+
|
|
3060
|
+
return items;
|
|
3061
|
+
}
|
|
3062
|
+
|
|
3063
|
+
_pairSubmessages(sections, submessages) {
|
|
3064
|
+
const n = sections.length;
|
|
3065
|
+
const m = submessages.length;
|
|
3066
|
+
|
|
3067
|
+
if (m === 0) return sections.map(() => null);
|
|
3068
|
+
if (m === 1) return sections.map((_, i) => (i === 0 ? submessages[0] : null));
|
|
3069
|
+
if (m === n) return submessages;
|
|
3070
|
+
|
|
3071
|
+
throw new ContentValidationError(`Cannot pair ${m} submessage(s) with ${n} section(s): expected 0, 1, or ${n}`, { sectionCount: n, submessageCount: m });
|
|
3072
|
+
}
|
|
3073
|
+
|
|
3074
|
+
_addContent(section, submessage, { id, replace, insertAt } = {}) {
|
|
3075
|
+
const hasReplace = replace !== undefined && replace !== null && replace !== '';
|
|
3076
|
+
|
|
3077
|
+
const hasInsertAt = insertAt !== undefined && insertAt !== null && insertAt !== '';
|
|
3078
|
+
|
|
3079
|
+
if (hasReplace && hasInsertAt) {
|
|
3080
|
+
throw new ContentValidationError('replace and insertAt cannot be used together');
|
|
3081
|
+
}
|
|
3082
|
+
|
|
3083
|
+
const sections = this._validateSections(section);
|
|
3084
|
+
const submessages = this._validateSubmessages(submessage);
|
|
3085
|
+
|
|
3086
|
+
if (!sections.length) {
|
|
3087
|
+
throw new ContentValidationError('At least one section is required');
|
|
3088
|
+
}
|
|
3089
|
+
|
|
3090
|
+
if (id !== undefined && id !== null && id !== '' && sections.length !== 1) {
|
|
3091
|
+
throw new ContentValidationError('One id can only be assigned to one node', {
|
|
3092
|
+
id,
|
|
3093
|
+
sectionCount: sections.length,
|
|
3094
|
+
});
|
|
3095
|
+
}
|
|
3096
|
+
|
|
3097
|
+
if (submessages.length && submessages.length !== sections.length && submessages.length !== 1) {
|
|
3098
|
+
throw new ContentValidationError('Section and submessage count must match');
|
|
3099
|
+
}
|
|
3100
|
+
|
|
3101
|
+
const pairedSubmessages = sections.map((_, index) => {
|
|
3102
|
+
if (!submessages.length) return undefined;
|
|
3103
|
+
|
|
3104
|
+
return submessages.length === 1 ? submessages[0] : submessages[index];
|
|
3105
|
+
});
|
|
3106
|
+
|
|
3107
|
+
if (id && this._idIndex.has(id) && !(hasReplace && this._resolveTarget(replace)?.id === id)) {
|
|
3108
|
+
throw new DuplicateIdError(id);
|
|
3109
|
+
}
|
|
3110
|
+
|
|
3111
|
+
const newNodes = sections.map((currentSection, index) => {
|
|
3112
|
+
return this._makeNode(index === 0 ? id : null, currentSection, pairedSubmessages[index]);
|
|
3113
|
+
});
|
|
3114
|
+
|
|
3115
|
+
if (hasReplace) {
|
|
3116
|
+
if (newNodes.length !== 1) {
|
|
3117
|
+
throw new ContentValidationError('replace only supports adding exactly one node');
|
|
3118
|
+
}
|
|
3119
|
+
|
|
3120
|
+
const target = this._resolveNodeIndex(replace);
|
|
3121
|
+
|
|
3122
|
+
if (!target) {
|
|
3123
|
+
throw new ContentValidationError('Target node could not be resolved');
|
|
3124
|
+
}
|
|
3125
|
+
|
|
3126
|
+
const oldNode = this._nodes[target.index];
|
|
3127
|
+
const newNode = newNodes[0];
|
|
3128
|
+
|
|
3129
|
+
if (!newNode.id && oldNode?.id) {
|
|
3130
|
+
newNode.id = oldNode.id;
|
|
3131
|
+
}
|
|
3132
|
+
|
|
3133
|
+
this._unregisterId(oldNode);
|
|
3134
|
+
|
|
3135
|
+
this._nodes.splice(target.index, 1, newNode);
|
|
3136
|
+
|
|
3137
|
+
if (newNode.id) {
|
|
3138
|
+
this._idIndex.set(newNode.id, newNode);
|
|
3139
|
+
}
|
|
3140
|
+
|
|
3141
|
+
return this;
|
|
3142
|
+
}
|
|
3143
|
+
|
|
3144
|
+
if (hasInsertAt) {
|
|
3145
|
+
const target = this._resolveNodeIndex(insertAt);
|
|
3146
|
+
|
|
3147
|
+
if (!target) {
|
|
3148
|
+
throw new ContentValidationError('Target node could not be resolved');
|
|
3149
|
+
}
|
|
3150
|
+
|
|
3151
|
+
const insertIndex = target.offset < 0 ? target.index : target.index + 1;
|
|
3152
|
+
|
|
3153
|
+
this._nodes.splice(insertIndex, 0, ...newNodes);
|
|
3154
|
+
|
|
3155
|
+
for (const node of newNodes) {
|
|
3156
|
+
if (node.id) {
|
|
3157
|
+
this._idIndex.set(node.id, node);
|
|
3158
|
+
}
|
|
3159
|
+
}
|
|
3160
|
+
|
|
3161
|
+
return this;
|
|
3162
|
+
}
|
|
3163
|
+
|
|
3164
|
+
this._nodes.push(...newNodes);
|
|
3165
|
+
|
|
3166
|
+
for (const node of newNodes) {
|
|
3167
|
+
if (node.id) {
|
|
3168
|
+
this._idIndex.set(node.id, node);
|
|
3169
|
+
}
|
|
3170
|
+
}
|
|
3171
|
+
|
|
3172
|
+
return this;
|
|
3173
|
+
}
|
|
3174
|
+
|
|
3175
|
+
addCompact({ title = '', subtitle = '', secondary_subtitle = '', image, entity_id = '', entity_url = '', entity_type = 'WEBSITE', action_type = 'OPEN_URL' } = {}) {
|
|
3176
|
+
const key = `NIXEL_COMPACT_ENTITY_${this._nodes.length + 1}`;
|
|
3177
|
+
const entityName = String(entity_id || title || '').trim();
|
|
3178
|
+
const heading = [title, subtitle, secondary_subtitle]
|
|
3179
|
+
.filter((value) => typeof value === 'string' && value.trim())
|
|
3180
|
+
.join('\n');
|
|
3181
|
+
const text = [heading, entityName ? `{{${key}}}${entityName}{{/${key}}}` : '']
|
|
3182
|
+
.filter(Boolean)
|
|
3183
|
+
.join('\n\n');
|
|
3184
|
+
const section = AIRich.newLayout('VStack', [{
|
|
3185
|
+
text,
|
|
3186
|
+
...(entityName && entity_url ? {
|
|
3187
|
+
inline_entities: [{
|
|
3188
|
+
key,
|
|
3189
|
+
metadata: {
|
|
3190
|
+
entity_id: String(entity_id || entityName),
|
|
3191
|
+
entity_name: entityName,
|
|
3192
|
+
...(image ? { entity_picture_url: image } : {}),
|
|
3193
|
+
entity_url,
|
|
3194
|
+
entity_type,
|
|
3195
|
+
action_type,
|
|
3196
|
+
__typename: 'GenAISocialEntityItem',
|
|
3197
|
+
},
|
|
3198
|
+
}],
|
|
3199
|
+
} : {}),
|
|
3200
|
+
__typename: 'GenAIMarkdownTextUXPrimitive',
|
|
3201
|
+
}]);
|
|
3202
|
+
return this.addSection(section);
|
|
3203
|
+
}
|
|
3204
|
+
|
|
3205
|
+
addSection(section, options = {}) {
|
|
3206
|
+
return this._addContent(section, undefined, options);
|
|
3207
|
+
}
|
|
3208
|
+
|
|
3209
|
+
addSubmessage(submessage, options = {}) {
|
|
3210
|
+
const items = this._validateSubmessages(submessage);
|
|
3211
|
+
|
|
3212
|
+
if (!items.length) {
|
|
3213
|
+
throw new ContentValidationError('At least one submessage is required');
|
|
3214
|
+
}
|
|
3215
|
+
|
|
3216
|
+
return this._addContent(undefined, items, options);
|
|
3217
|
+
}
|
|
3218
|
+
|
|
3219
|
+
delete(target) {
|
|
3220
|
+
const { index } = this._resolveNodeIndex(target);
|
|
3221
|
+
const [oldNode] = this._nodes.splice(index, 1);
|
|
3222
|
+
|
|
3223
|
+
this._unregisterId(oldNode);
|
|
3224
|
+
|
|
3225
|
+
return this;
|
|
3226
|
+
}
|
|
3227
|
+
|
|
3228
|
+
get _sections() {
|
|
3229
|
+
return this._nodes.filter((n) => n.section !== null).map((n) => n.section);
|
|
3230
|
+
}
|
|
3231
|
+
|
|
3232
|
+
get _submessages() {
|
|
3233
|
+
return this._nodes.filter((n) => n.submessage !== null).map((n) => n.submessage);
|
|
3234
|
+
}
|
|
3235
|
+
|
|
3236
|
+
get sections() {
|
|
3237
|
+
return this._sections;
|
|
3238
|
+
}
|
|
3239
|
+
|
|
3240
|
+
get items() {
|
|
3241
|
+
return this._sections.flatMap((section) => {
|
|
3242
|
+
const vm = section?.view_model;
|
|
3243
|
+
|
|
3244
|
+
if (Array.isArray(vm?.primitives)) {
|
|
3245
|
+
return vm.primitives;
|
|
3246
|
+
}
|
|
3247
|
+
|
|
3248
|
+
if (vm?.primitive) {
|
|
3249
|
+
return [vm.primitive];
|
|
3250
|
+
}
|
|
3251
|
+
|
|
3252
|
+
return [];
|
|
3253
|
+
});
|
|
3254
|
+
}
|
|
3255
|
+
}
|
|
3256
|
+
|
|
3257
|
+
export { VERSION, Button, ButtonV2, Carousel, AIRich, Toolkit };
|