@crysnovax/baileys 1.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +467 -0
- package/WAProto/V +1 -0
- package/WAProto/index.js +104236 -0
- package/engine-requirements.js +13 -0
- package/lib/Defaults/index.js +148 -0
- package/lib/Signal/Group/ciphertext-message.js +11 -0
- package/lib/Signal/Group/group-session-builder.js +29 -0
- package/lib/Signal/Group/group_cipher.js +81 -0
- package/lib/Signal/Group/index.js +11 -0
- package/lib/Signal/Group/keyhelper.js +17 -0
- package/lib/Signal/Group/sender-chain-key.js +25 -0
- package/lib/Signal/Group/sender-key-distribution-message.js +62 -0
- package/lib/Signal/Group/sender-key-message.js +65 -0
- package/lib/Signal/Group/sender-key-name.js +47 -0
- package/lib/Signal/Group/sender-key-record.js +40 -0
- package/lib/Signal/Group/sender-key-state.js +83 -0
- package/lib/Signal/Group/sender-message-key.js +25 -0
- package/lib/Signal/libsignal.js +406 -0
- package/lib/Signal/lid-mapping.js +276 -0
- package/lib/Socket/Client/index.js +2 -0
- package/lib/Socket/Client/types.js +10 -0
- package/lib/Socket/Client/websocket.js +53 -0
- package/lib/Socket/business.js +378 -0
- package/lib/Socket/chats.js +1059 -0
- package/lib/Socket/communities.js +430 -0
- package/lib/Socket/groups.js +328 -0
- package/lib/Socket/index.js +11 -0
- package/lib/Socket/messages-recv.js +1476 -0
- package/lib/Socket/messages-send.js +1268 -0
- package/lib/Socket/mex.js +41 -0
- package/lib/Socket/newsletter.js +251 -0
- package/lib/Socket/socket.js +949 -0
- package/lib/Store/index.js +3 -0
- package/lib/Store/make-in-memory-store.js +420 -0
- package/lib/Store/make-ordered-dictionary.js +78 -0
- package/lib/Store/object-repository.js +23 -0
- package/lib/Types/Auth.js +1 -0
- package/lib/Types/Bussines.js +1 -0
- package/lib/Types/Call.js +1 -0
- package/lib/Types/Chat.js +7 -0
- package/lib/Types/Contact.js +1 -0
- package/lib/Types/Events.js +1 -0
- package/lib/Types/GroupMetadata.js +1 -0
- package/lib/Types/Label.js +24 -0
- package/lib/Types/LabelAssociation.js +6 -0
- package/lib/Types/Message.js +17 -0
- package/lib/Types/Newsletter.js +33 -0
- package/lib/Types/Product.js +1 -0
- package/lib/Types/RichType.js +22 -0
- package/lib/Types/Signal.js +1 -0
- package/lib/Types/Socket.js +2 -0
- package/lib/Types/State.js +12 -0
- package/lib/Types/USync.js +1 -0
- package/lib/Types/index.js +25 -0
- package/lib/Utils/auth-utils.js +289 -0
- package/lib/Utils/bot-planning-replay.js +206 -0
- package/lib/Utils/browser-utils.js +28 -0
- package/lib/Utils/business.js +230 -0
- package/lib/Utils/chat-utils.js +811 -0
- package/lib/Utils/companion-reg-client-utils.js +32 -0
- package/lib/Utils/crypto.js +117 -0
- package/lib/Utils/decode-wa-message.js +282 -0
- package/lib/Utils/event-buffer.js +589 -0
- package/lib/Utils/generics.js +385 -0
- package/lib/Utils/history.js +130 -0
- package/lib/Utils/identity-change-handler.js +48 -0
- package/lib/Utils/index.js +26 -0
- package/lib/Utils/link-preview.js +84 -0
- package/lib/Utils/logger.js +2 -0
- package/lib/Utils/lt-hash.js +7 -0
- package/lib/Utils/make-mutex.js +32 -0
- package/lib/Utils/message-retry-manager.js +241 -0
- package/lib/Utils/messages-media.js +830 -0
- package/lib/Utils/messages.js +1891 -0
- package/lib/Utils/meta-compositing.js +208 -0
- package/lib/Utils/noise-handler.js +200 -0
- package/lib/Utils/offline-node-processor.js +39 -0
- package/lib/Utils/pre-key-manager.js +105 -0
- package/lib/Utils/process-message.js +527 -0
- package/lib/Utils/reporting-utils.js +257 -0
- package/lib/Utils/rich-message-utils.js +387 -0
- package/lib/Utils/signal.js +158 -0
- package/lib/Utils/stanza-ack.js +37 -0
- package/lib/Utils/sync-action-utils.js +47 -0
- package/lib/Utils/tc-token-utils.js +17 -0
- package/lib/Utils/use-multi-file-auth-state.js +120 -0
- package/lib/Utils/use-single-file-auth-state.js +96 -0
- package/lib/Utils/validate-connection.js +206 -0
- package/lib/Utils//360/237/224/226 +217 -0
- package/lib/WABinary/constants.js +1372 -0
- package/lib/WABinary/decode.js +261 -0
- package/lib/WABinary/encode.js +219 -0
- package/lib/WABinary/generic-utils.js +227 -0
- package/lib/WABinary/index.js +5 -0
- package/lib/WABinary/jid-utils.js +95 -0
- package/lib/WABinary/types.js +1 -0
- package/lib/WAM/BinaryInfo.js +9 -0
- package/lib/WAM/constants.js +22852 -0
- package/lib/WAM/encode.js +149 -0
- package/lib/WAM/index.js +3 -0
- package/lib/WAUSync/Protocols/USyncContactProtocol.js +28 -0
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +53 -0
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +26 -0
- package/lib/WAUSync/Protocols/USyncStatusProtocol.js +37 -0
- package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +50 -0
- package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +28 -0
- package/lib/WAUSync/Protocols/index.js +4 -0
- package/lib/WAUSync/USyncQuery.js +93 -0
- package/lib/WAUSync/USyncUser.js +22 -0
- package/lib/WAUSync/index.js +3 -0
- package/lib/index.js +11 -0
- package/package.json +83 -0
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { Boom } from '@hapi/boom';
|
|
2
|
+
export var SyncState;
|
|
3
|
+
(function (SyncState) {
|
|
4
|
+
/** The socket is connecting, but we haven't received pending notifications yet. */
|
|
5
|
+
SyncState[SyncState["Connecting"] = 0] = "Connecting";
|
|
6
|
+
/** Pending notifications received. Buffering events until we decide whether to sync or not. */
|
|
7
|
+
SyncState[SyncState["AwaitingInitialSync"] = 1] = "AwaitingInitialSync";
|
|
8
|
+
/** The initial app state sync (history, etc.) is in progress. Buffering continues. */
|
|
9
|
+
SyncState[SyncState["Syncing"] = 2] = "Syncing";
|
|
10
|
+
/** Initial sync is complete, or was skipped. The socket is fully operational and events are processed in real-time. */
|
|
11
|
+
SyncState[SyncState["Online"] = 3] = "Online";
|
|
12
|
+
})(SyncState || (SyncState = {}));
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import { USyncUser } from '../WAUSync/index.js';
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
export * from './Auth.js';
|
|
2
|
+
export * from './GroupMetadata.js';
|
|
3
|
+
export * from './Chat.js';
|
|
4
|
+
export * from './Contact.js';
|
|
5
|
+
export * from './State.js';
|
|
6
|
+
export * from './Message.js';
|
|
7
|
+
export * from './Socket.js';
|
|
8
|
+
export * from './Events.js';
|
|
9
|
+
export * from './Product.js';
|
|
10
|
+
export * from './Call.js';
|
|
11
|
+
export * from './Signal.js';
|
|
12
|
+
export * from './Newsletter.js';
|
|
13
|
+
export var DisconnectReason;
|
|
14
|
+
(function (DisconnectReason) {
|
|
15
|
+
DisconnectReason[DisconnectReason["connectionClosed"] = 428] = "connectionClosed";
|
|
16
|
+
DisconnectReason[DisconnectReason["connectionLost"] = 408] = "connectionLost";
|
|
17
|
+
DisconnectReason[DisconnectReason["connectionReplaced"] = 440] = "connectionReplaced";
|
|
18
|
+
DisconnectReason[DisconnectReason["timedOut"] = 408] = "timedOut";
|
|
19
|
+
DisconnectReason[DisconnectReason["loggedOut"] = 401] = "loggedOut";
|
|
20
|
+
DisconnectReason[DisconnectReason["badSession"] = 500] = "badSession";
|
|
21
|
+
DisconnectReason[DisconnectReason["restartRequired"] = 515] = "restartRequired";
|
|
22
|
+
DisconnectReason[DisconnectReason["multideviceMismatch"] = 411] = "multideviceMismatch";
|
|
23
|
+
DisconnectReason[DisconnectReason["forbidden"] = 403] = "forbidden";
|
|
24
|
+
DisconnectReason[DisconnectReason["unavailableService"] = 503] = "unavailableService";
|
|
25
|
+
})(DisconnectReason || (DisconnectReason = {}));
|
|
@@ -0,0 +1,289 @@
|
|
|
1
|
+
import NodeCache from '@cacheable/node-cache';
|
|
2
|
+
import { AsyncLocalStorage } from 'async_hooks';
|
|
3
|
+
import { Mutex } from 'async-mutex';
|
|
4
|
+
import { randomBytes } from 'crypto';
|
|
5
|
+
import PQueue from 'p-queue';
|
|
6
|
+
import { DEFAULT_CACHE_TTLS } from '../Defaults/index.js';
|
|
7
|
+
import { Curve, signedKeyPair } from './crypto.js';
|
|
8
|
+
import { delay, generateRegistrationId } from './generics.js';
|
|
9
|
+
import { PreKeyManager } from './pre-key-manager.js';
|
|
10
|
+
/**
|
|
11
|
+
* Adds caching capability to a SignalKeyStore
|
|
12
|
+
* @param store the store to add caching to
|
|
13
|
+
* @param logger to log trace events
|
|
14
|
+
* @param _cache cache store to use
|
|
15
|
+
*/
|
|
16
|
+
export function makeCacheableSignalKeyStore(store, logger, _cache) {
|
|
17
|
+
const cache = _cache ||
|
|
18
|
+
new NodeCache({
|
|
19
|
+
stdTTL: DEFAULT_CACHE_TTLS.SIGNAL_STORE, // 5 minutes
|
|
20
|
+
useClones: false,
|
|
21
|
+
deleteOnExpire: true
|
|
22
|
+
});
|
|
23
|
+
// Mutex for protecting cache operations
|
|
24
|
+
const cacheMutex = new Mutex();
|
|
25
|
+
function getUniqueId(type, id) {
|
|
26
|
+
return `${type}.${id}`;
|
|
27
|
+
}
|
|
28
|
+
return {
|
|
29
|
+
async get(type, ids) {
|
|
30
|
+
return cacheMutex.runExclusive(async () => {
|
|
31
|
+
const data = {};
|
|
32
|
+
const idsToFetch = [];
|
|
33
|
+
for (const id of ids) {
|
|
34
|
+
const item = (await cache.get(getUniqueId(type, id)));
|
|
35
|
+
if (typeof item !== 'undefined') {
|
|
36
|
+
data[id] = item;
|
|
37
|
+
}
|
|
38
|
+
else {
|
|
39
|
+
idsToFetch.push(id);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
if (idsToFetch.length) {
|
|
43
|
+
logger?.trace({ items: idsToFetch.length }, 'loading from store');
|
|
44
|
+
const fetched = await store.get(type, idsToFetch);
|
|
45
|
+
for (const id of idsToFetch) {
|
|
46
|
+
const item = fetched[id];
|
|
47
|
+
if (item) {
|
|
48
|
+
data[id] = item;
|
|
49
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
|
|
50
|
+
await cache.set(getUniqueId(type, id), item);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
return data;
|
|
55
|
+
});
|
|
56
|
+
},
|
|
57
|
+
async set(data) {
|
|
58
|
+
return cacheMutex.runExclusive(async () => {
|
|
59
|
+
let keys = 0;
|
|
60
|
+
for (const type in data) {
|
|
61
|
+
for (const id in data[type]) {
|
|
62
|
+
await cache.set(getUniqueId(type, id), data[type][id]);
|
|
63
|
+
keys += 1;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
logger?.trace({ keys }, 'updated cache');
|
|
67
|
+
await store.set(data);
|
|
68
|
+
});
|
|
69
|
+
},
|
|
70
|
+
async clear() {
|
|
71
|
+
await cache.flushAll();
|
|
72
|
+
await store.clear?.();
|
|
73
|
+
}
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Adds DB-like transaction capability to the SignalKeyStore
|
|
78
|
+
* Uses AsyncLocalStorage for automatic context management
|
|
79
|
+
* @param state the key store to apply this capability to
|
|
80
|
+
* @param logger logger to log events
|
|
81
|
+
* @returns SignalKeyStore with transaction capability
|
|
82
|
+
*/
|
|
83
|
+
export const addTransactionCapability = (state, logger, { maxCommitRetries, delayBetweenTriesMs }) => {
|
|
84
|
+
const txStorage = new AsyncLocalStorage();
|
|
85
|
+
// Queues for concurrency control (keyed by signal data type - bounded set)
|
|
86
|
+
const keyQueues = new Map();
|
|
87
|
+
// Transaction mutexes with reference counting for cleanup
|
|
88
|
+
const txMutexes = new Map();
|
|
89
|
+
const txMutexRefCounts = new Map();
|
|
90
|
+
// Pre-key manager for specialized operations
|
|
91
|
+
const preKeyManager = new PreKeyManager(state, logger);
|
|
92
|
+
/**
|
|
93
|
+
* Get or create a queue for a specific key type
|
|
94
|
+
*/
|
|
95
|
+
function getQueue(key) {
|
|
96
|
+
if (!keyQueues.has(key)) {
|
|
97
|
+
keyQueues.set(key, new PQueue({ concurrency: 1 }));
|
|
98
|
+
}
|
|
99
|
+
return keyQueues.get(key);
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Get or create a transaction mutex
|
|
103
|
+
*/
|
|
104
|
+
function getTxMutex(key) {
|
|
105
|
+
if (!txMutexes.has(key)) {
|
|
106
|
+
txMutexes.set(key, new Mutex());
|
|
107
|
+
txMutexRefCounts.set(key, 0);
|
|
108
|
+
}
|
|
109
|
+
return txMutexes.get(key);
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Acquire a reference to a transaction mutex
|
|
113
|
+
*/
|
|
114
|
+
function acquireTxMutexRef(key) {
|
|
115
|
+
const count = txMutexRefCounts.get(key) ?? 0;
|
|
116
|
+
txMutexRefCounts.set(key, count + 1);
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Release a reference to a transaction mutex and cleanup if no longer needed
|
|
120
|
+
*/
|
|
121
|
+
function releaseTxMutexRef(key) {
|
|
122
|
+
const count = (txMutexRefCounts.get(key) ?? 1) - 1;
|
|
123
|
+
txMutexRefCounts.set(key, count);
|
|
124
|
+
// Cleanup if no more references and mutex is not locked
|
|
125
|
+
if (count <= 0) {
|
|
126
|
+
const mutex = txMutexes.get(key);
|
|
127
|
+
if (mutex && !mutex.isLocked()) {
|
|
128
|
+
txMutexes.delete(key);
|
|
129
|
+
txMutexRefCounts.delete(key);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* Check if currently in a transaction
|
|
135
|
+
*/
|
|
136
|
+
function isInTransaction() {
|
|
137
|
+
return !!txStorage.getStore();
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* Commit transaction with retries
|
|
141
|
+
*/
|
|
142
|
+
async function commitWithRetry(mutations) {
|
|
143
|
+
if (Object.keys(mutations).length === 0) {
|
|
144
|
+
logger.trace('no mutations in transaction');
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
logger.trace('committing transaction');
|
|
148
|
+
for (let attempt = 0; attempt < maxCommitRetries; attempt++) {
|
|
149
|
+
try {
|
|
150
|
+
await state.set(mutations);
|
|
151
|
+
logger.trace({ mutationCount: Object.keys(mutations).length }, 'committed transaction');
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
catch (error) {
|
|
155
|
+
const retriesLeft = maxCommitRetries - attempt - 1;
|
|
156
|
+
logger.warn(`failed to commit mutations, retries left=${retriesLeft}`);
|
|
157
|
+
if (retriesLeft === 0) {
|
|
158
|
+
throw error;
|
|
159
|
+
}
|
|
160
|
+
await delay(delayBetweenTriesMs);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
return {
|
|
165
|
+
get: async (type, ids) => {
|
|
166
|
+
const ctx = txStorage.getStore();
|
|
167
|
+
if (!ctx) {
|
|
168
|
+
// No transaction - direct read without exclusive lock for concurrency
|
|
169
|
+
return state.get(type, ids);
|
|
170
|
+
}
|
|
171
|
+
// In transaction - check cache first
|
|
172
|
+
const cached = ctx.cache[type] || {};
|
|
173
|
+
const missing = ids.filter(id => !(id in cached));
|
|
174
|
+
if (missing.length > 0) {
|
|
175
|
+
ctx.dbQueries++;
|
|
176
|
+
logger.trace({ type, count: missing.length }, 'fetching missing keys in transaction');
|
|
177
|
+
const fetched = await getTxMutex(type).runExclusive(() => state.get(type, missing));
|
|
178
|
+
// Update cache
|
|
179
|
+
ctx.cache[type] = ctx.cache[type] || {};
|
|
180
|
+
Object.assign(ctx.cache[type], fetched);
|
|
181
|
+
}
|
|
182
|
+
// Return requested ids from cache
|
|
183
|
+
const result = {};
|
|
184
|
+
for (const id of ids) {
|
|
185
|
+
const value = ctx.cache[type]?.[id];
|
|
186
|
+
if (value !== undefined && value !== null) {
|
|
187
|
+
result[id] = value;
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
return result;
|
|
191
|
+
},
|
|
192
|
+
set: async (data) => {
|
|
193
|
+
const ctx = txStorage.getStore();
|
|
194
|
+
if (!ctx) {
|
|
195
|
+
// No transaction - direct write with queue protection
|
|
196
|
+
const types = Object.keys(data);
|
|
197
|
+
// Process pre-keys with validation
|
|
198
|
+
for (const type_ of types) {
|
|
199
|
+
const type = type_;
|
|
200
|
+
if (type === 'pre-key') {
|
|
201
|
+
await preKeyManager.validateDeletions(data, type);
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
// Write all data in parallel
|
|
205
|
+
await Promise.all(types.map(type => getQueue(type).add(async () => {
|
|
206
|
+
const typeData = { [type]: data[type] };
|
|
207
|
+
await state.set(typeData);
|
|
208
|
+
})));
|
|
209
|
+
return;
|
|
210
|
+
}
|
|
211
|
+
// In transaction - update cache and mutations
|
|
212
|
+
logger.trace({ types: Object.keys(data) }, 'caching in transaction');
|
|
213
|
+
for (const key_ in data) {
|
|
214
|
+
const key = key_;
|
|
215
|
+
// Ensure structures exist
|
|
216
|
+
ctx.cache[key] = ctx.cache[key] || {};
|
|
217
|
+
ctx.mutations[key] = ctx.mutations[key] || {};
|
|
218
|
+
// Special handling for pre-keys
|
|
219
|
+
if (key === 'pre-key') {
|
|
220
|
+
await preKeyManager.processOperations(data, key, ctx.cache, ctx.mutations, true);
|
|
221
|
+
}
|
|
222
|
+
else {
|
|
223
|
+
// Normal key types
|
|
224
|
+
Object.assign(ctx.cache[key], data[key]);
|
|
225
|
+
Object.assign(ctx.mutations[key], data[key]);
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
},
|
|
229
|
+
isInTransaction,
|
|
230
|
+
transaction: async (work, key) => {
|
|
231
|
+
const existing = txStorage.getStore();
|
|
232
|
+
// Nested transaction - reuse existing context
|
|
233
|
+
if (existing) {
|
|
234
|
+
logger.trace('reusing existing transaction context');
|
|
235
|
+
return work();
|
|
236
|
+
}
|
|
237
|
+
// New transaction - acquire mutex and create context
|
|
238
|
+
const mutex = getTxMutex(key);
|
|
239
|
+
acquireTxMutexRef(key);
|
|
240
|
+
try {
|
|
241
|
+
return await mutex.runExclusive(async () => {
|
|
242
|
+
const ctx = {
|
|
243
|
+
cache: {},
|
|
244
|
+
mutations: {},
|
|
245
|
+
dbQueries: 0
|
|
246
|
+
};
|
|
247
|
+
logger.trace('entering transaction');
|
|
248
|
+
try {
|
|
249
|
+
const result = await txStorage.run(ctx, work);
|
|
250
|
+
// Commit mutations
|
|
251
|
+
await commitWithRetry(ctx.mutations);
|
|
252
|
+
logger.trace({ dbQueries: ctx.dbQueries }, 'transaction completed');
|
|
253
|
+
return result;
|
|
254
|
+
}
|
|
255
|
+
catch (error) {
|
|
256
|
+
logger.error({ error }, 'transaction failed, rolling back');
|
|
257
|
+
throw error;
|
|
258
|
+
}
|
|
259
|
+
});
|
|
260
|
+
}
|
|
261
|
+
finally {
|
|
262
|
+
releaseTxMutexRef(key);
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
};
|
|
266
|
+
};
|
|
267
|
+
export const initAuthCreds = () => {
|
|
268
|
+
const identityKey = Curve.generateKeyPair();
|
|
269
|
+
return {
|
|
270
|
+
noiseKey: Curve.generateKeyPair(),
|
|
271
|
+
pairingEphemeralKeyPair: Curve.generateKeyPair(),
|
|
272
|
+
signedIdentityKey: identityKey,
|
|
273
|
+
signedPreKey: signedKeyPair(identityKey, 1),
|
|
274
|
+
registrationId: generateRegistrationId(),
|
|
275
|
+
advSecretKey: randomBytes(32).toString('base64'),
|
|
276
|
+
processedHistoryMessages: [],
|
|
277
|
+
nextPreKeyId: 1,
|
|
278
|
+
firstUnuploadedPreKeyId: 1,
|
|
279
|
+
accountSyncCounter: 0,
|
|
280
|
+
accountSettings: {
|
|
281
|
+
unarchiveChats: false
|
|
282
|
+
},
|
|
283
|
+
registered: false,
|
|
284
|
+
pairingCode: undefined,
|
|
285
|
+
lastPropHash: undefined,
|
|
286
|
+
routingInfo: undefined,
|
|
287
|
+
additionalData: undefined
|
|
288
|
+
};
|
|
289
|
+
};
|
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Lia@Changes [WIP]
|
|
3
|
+
* botPlanningReplay — Meta AI-style live reasoning feed.
|
|
4
|
+
*
|
|
5
|
+
* Sends a progress indicator with all steps IN_PROGRESS, then replays
|
|
6
|
+
* each step completing in real time via sequential edits — exactly how
|
|
7
|
+
* Meta AI's "Thinking…" bubble works. Final rich message lands clean
|
|
8
|
+
* with NO "edited" badge, using the same delete-resend flow from
|
|
9
|
+
* meta-compositing.
|
|
10
|
+
*
|
|
11
|
+
* replayPlanning(sock, jid, steps, finalContent, options)
|
|
12
|
+
*
|
|
13
|
+
* Flow:
|
|
14
|
+
* Send placeholder (all steps IN_PROGRESS)
|
|
15
|
+
* → edit: step[0] → DONE (stepDelayMs)
|
|
16
|
+
* → edit: step[1] → DONE (stepDelayMs)
|
|
17
|
+
* → ...
|
|
18
|
+
* → edit: step[n] → DONE (finalPauseMs)
|
|
19
|
+
* → delete placeholder
|
|
20
|
+
* → send final rich message fresh
|
|
21
|
+
*
|
|
22
|
+
* If you use or copy this code, please credit @crysnovax/bailey.
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
import { proto } from '../../WAProto/index.js';
|
|
26
|
+
import {
|
|
27
|
+
buildCompositingPlaceholder,
|
|
28
|
+
buildProgressIndicator,
|
|
29
|
+
botMetadataSignature,
|
|
30
|
+
botMetadataCertificate,
|
|
31
|
+
PlanningStepStatus
|
|
32
|
+
} from './meta-compositing.js';
|
|
33
|
+
import { BOT_RENDERING_CONFIG_METADATA } from '../Defaults/index.js';
|
|
34
|
+
import { delay } from './generics.js';
|
|
35
|
+
|
|
36
|
+
// ─── Internal: rebuild the full botForwardedMessage with updated step statuses ───────────────
|
|
37
|
+
const buildReplayFrame = (description, steps, placeholderText = '') => {
|
|
38
|
+
return buildCompositingPlaceholder({ description, steps, placeholderText });
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
// ─── Internal: send an in-place edit of the planning bubble with new step states ─────────────
|
|
42
|
+
const editPlanningBubble = async (sock, jid, key, description, steps, placeholderText) => {
|
|
43
|
+
const updated = buildReplayFrame(description, steps, placeholderText);
|
|
44
|
+
// Use { edit: key, raw: true } — routes through protocolMessage.editedMessage
|
|
45
|
+
await sock.sendMessage(jid, {
|
|
46
|
+
raw: true,
|
|
47
|
+
edit: key,
|
|
48
|
+
...updated
|
|
49
|
+
});
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* replayPlanning — full live planning animation.
|
|
54
|
+
*
|
|
55
|
+
* @param {object} sock - Baileys socket
|
|
56
|
+
* @param {string} jid - Destination JID
|
|
57
|
+
* @param {Array} steps - Array of step definitions:
|
|
58
|
+
* { title, body?, isReasoning?, isEnhancedSearch? }
|
|
59
|
+
* Status is managed automatically — do NOT pass status here.
|
|
60
|
+
* @param {object} finalContent - Rich content for the final message.
|
|
61
|
+
* Same as sendMessage: { code, table, text, richResponse… }
|
|
62
|
+
* @param {object} [options]
|
|
63
|
+
* @param {string} [options.description] - Top label while thinking. Default: 'Thinking…'
|
|
64
|
+
* @param {string} [options.placeholderText] - Body text shown in bubble while loading.
|
|
65
|
+
* @param {number} [options.stepDelayMs] - Ms between each step completing. Default: 900
|
|
66
|
+
* @param {number} [options.finalPauseMs] - Ms to hold after all steps done before cleanup. Default: 600
|
|
67
|
+
* @param {boolean} [options.abortOnDisconnect] - Stop replay loop if socket closes. Default: true
|
|
68
|
+
* @param {object} [options.sendOptions] - Extra options for final sendMessage call.
|
|
69
|
+
* @returns {Promise<object>} - The final sent WAMessage
|
|
70
|
+
*/
|
|
71
|
+
export const replayPlanning = async (sock, jid, steps, finalContent, {
|
|
72
|
+
description = 'Thinking…',
|
|
73
|
+
placeholderText = '',
|
|
74
|
+
stepDelayMs = 900,
|
|
75
|
+
finalPauseMs = 600,
|
|
76
|
+
abortOnDisconnect = true,
|
|
77
|
+
sendOptions = {}
|
|
78
|
+
} = {}) => {
|
|
79
|
+
if (!steps?.length) {
|
|
80
|
+
throw new Error('replayPlanning: steps array must have at least one entry');
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// ── Track abort state if socket disconnects mid-replay ───────────────────
|
|
84
|
+
let aborted = false;
|
|
85
|
+
const onClose = () => { aborted = true; };
|
|
86
|
+
if (abortOnDisconnect) {
|
|
87
|
+
sock.ev?.once?.('connection.update', ({ connection }) => {
|
|
88
|
+
if (connection === 'close') onClose();
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// ── 1. Build initial state — all steps IN_PROGRESS ───────────────────────
|
|
93
|
+
const initialSteps = steps.map(step => ({
|
|
94
|
+
...step,
|
|
95
|
+
status: PlanningStepStatus.IN_PROGRESS
|
|
96
|
+
}));
|
|
97
|
+
|
|
98
|
+
const placeholder = await sock.sendMessage(jid, {
|
|
99
|
+
raw: true,
|
|
100
|
+
...buildReplayFrame(description, initialSteps, placeholderText)
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
const key = placeholder?.key;
|
|
104
|
+
|
|
105
|
+
// ── 2. Replay loop — flip each step to DONE sequentially ─────────────────
|
|
106
|
+
try {
|
|
107
|
+
const currentSteps = [...initialSteps];
|
|
108
|
+
|
|
109
|
+
for (let i = 0; i < currentSteps.length; i++) {
|
|
110
|
+
if (aborted) break;
|
|
111
|
+
|
|
112
|
+
await delay(stepDelayMs);
|
|
113
|
+
if (aborted) break;
|
|
114
|
+
|
|
115
|
+
// Flip this step to DONE
|
|
116
|
+
currentSteps[i] = {
|
|
117
|
+
...currentSteps[i],
|
|
118
|
+
status: PlanningStepStatus.DONE
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
if (key) {
|
|
122
|
+
await editPlanningBubble(
|
|
123
|
+
sock, jid, key,
|
|
124
|
+
description, currentSteps, placeholderText
|
|
125
|
+
);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
// ── 3. Hold with all steps DONE so user sees completion ──────────────
|
|
130
|
+
if (!aborted && finalPauseMs > 0) {
|
|
131
|
+
await delay(finalPauseMs);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
// ── 4. Delete placeholder silently ───────────────────────────────────
|
|
135
|
+
if (key && !aborted) {
|
|
136
|
+
await sock.sendMessage(jid, { delete: key });
|
|
137
|
+
}
|
|
138
|
+
} catch (err) {
|
|
139
|
+
// Non-fatal — always attempt final send even if replay failed mid-way
|
|
140
|
+
try {
|
|
141
|
+
if (key) await sock.sendMessage(jid, { delete: key });
|
|
142
|
+
} catch (_) {}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
// ── 5. Send the final rich message as a brand-new message — no edited badge ──
|
|
146
|
+
return sock.sendMessage(jid, finalContent, sendOptions);
|
|
147
|
+
};
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* replayPlanningOnly — same animation but WITHOUT sending a final message.
|
|
151
|
+
* Use when you want to control the final send yourself.
|
|
152
|
+
*
|
|
153
|
+
* Returns { key } of the placeholder (already deleted on completion).
|
|
154
|
+
*
|
|
155
|
+
* @param {object} sock
|
|
156
|
+
* @param {string} jid
|
|
157
|
+
* @param {Array} steps
|
|
158
|
+
* @param {object} [options] - Same options as replayPlanning minus sendOptions
|
|
159
|
+
* @returns {Promise<void>}
|
|
160
|
+
*/
|
|
161
|
+
export const replayPlanningOnly = async (sock, jid, steps, options = {}) => {
|
|
162
|
+
return replayPlanning(sock, jid, steps, null, {
|
|
163
|
+
...options,
|
|
164
|
+
_skipFinalSend: true
|
|
165
|
+
});
|
|
166
|
+
};
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* buildReasoningSteps — convenience builder for steps with isReasoning: true.
|
|
170
|
+
* Renders with the "reasoning" visual treatment in the Meta bubble.
|
|
171
|
+
*
|
|
172
|
+
* @example
|
|
173
|
+
* buildReasoningSteps(['Analyzing the problem…', 'Checking edge cases…'])
|
|
174
|
+
*/
|
|
175
|
+
export const buildReasoningSteps = (titles) =>
|
|
176
|
+
titles.map(title => ({ title, isReasoning: true }));
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* buildSearchSteps — convenience builder for steps with isEnhancedSearch: true.
|
|
180
|
+
* Renders with the "enhanced search" visual treatment.
|
|
181
|
+
*
|
|
182
|
+
* @example
|
|
183
|
+
* buildSearchSteps(['Searching the web…', 'Reading top results…'])
|
|
184
|
+
*/
|
|
185
|
+
export const buildSearchSteps = (titles) =>
|
|
186
|
+
titles.map(title => ({ title, isEnhancedSearch: true }));
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* mixedSteps — build a steps array mixing reasoning + search + plain steps.
|
|
190
|
+
* Pass an array of { title, type? } where type is 'reasoning' | 'search' | undefined.
|
|
191
|
+
*
|
|
192
|
+
* @example
|
|
193
|
+
* mixedSteps([
|
|
194
|
+
* { title: 'Understanding your question…', type: 'reasoning' },
|
|
195
|
+
* { title: 'Searching for data…', type: 'search' },
|
|
196
|
+
* { title: 'Writing the answer…' }
|
|
197
|
+
* ])
|
|
198
|
+
*/
|
|
199
|
+
export const mixedSteps = (defs) =>
|
|
200
|
+
defs.map(({ title, body, type }) => ({
|
|
201
|
+
title,
|
|
202
|
+
...(body ? { body } : {}),
|
|
203
|
+
...(type === 'reasoning' ? { isReasoning: true } : {}),
|
|
204
|
+
...(type === 'search' ? { isEnhancedSearch: true } : {})
|
|
205
|
+
}));
|
|
206
|
+
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { platform, release } from 'node:os';
|
|
2
|
+
import { proto } from '../../WAProto/index.js';
|
|
3
|
+
const PLATFORM_MAP = {
|
|
4
|
+
aix: 'AIX',
|
|
5
|
+
darwin: 'Mac OS',
|
|
6
|
+
win32: 'Windows',
|
|
7
|
+
android: 'Android',
|
|
8
|
+
freebsd: 'FreeBSD',
|
|
9
|
+
openbsd: 'OpenBSD',
|
|
10
|
+
sunos: 'Solaris',
|
|
11
|
+
linux: undefined,
|
|
12
|
+
haiku: undefined,
|
|
13
|
+
cygwin: undefined,
|
|
14
|
+
netbsd: undefined
|
|
15
|
+
};
|
|
16
|
+
export const Browsers = {
|
|
17
|
+
ubuntu: browser => ['Ubuntu', browser, '22.04.4'],
|
|
18
|
+
macOS: browser => ['Mac OS', browser, '14.4.1'],
|
|
19
|
+
baileys: browser => ['Baileys', browser, '6.5.0'],
|
|
20
|
+
windows: browser => ['Windows', browser, '10.0.22631'],
|
|
21
|
+
android: browser => [browser, 'Android', ''],
|
|
22
|
+
/** The appropriate browser based on your OS & release */
|
|
23
|
+
appropriate: browser => [PLATFORM_MAP[platform()] || 'Ubuntu', browser, release()]
|
|
24
|
+
};
|
|
25
|
+
export const getPlatformId = (browser) => {
|
|
26
|
+
const platformType = proto.DeviceProps.PlatformType[browser.toUpperCase()];
|
|
27
|
+
return platformType ? platformType.toString() : '1'; //chrome
|
|
28
|
+
};
|