@lobu/runtime-device 22.0.0 → 24.0.0
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/dist/component.json +1 -1
- package/dist/dependencies.bun.lock +33 -30
- package/npm-shrinkwrap.json +140 -124
- package/package.json +2 -1
- package/vendor/cli/dist/bundled-skills/lobu/SKILL.md +74 -1
- package/vendor/cli/dist/commands/_lib/apply/apply-cmd.js +9 -11
- package/vendor/cli/dist/commands/_lib/apply/client.d.ts +3 -11
- package/vendor/cli/dist/commands/_lib/apply/client.js +3 -3
- package/vendor/cli/dist/commands/_lib/apply/desired-state.d.ts +0 -6
- package/vendor/cli/dist/commands/_lib/apply/desired-state.js +6 -15
- package/vendor/cli/dist/commands/_lib/apply/diff.js +1 -0
- package/vendor/cli/dist/commands/_lib/apply/map-config.js +8 -0
- package/vendor/cli/dist/commands/_lib/view-bundler.d.ts +2 -9
- package/vendor/cli/dist/commands/_lib/view-bundler.js +25 -97
- package/vendor/cli/dist/config/define.d.ts +8 -0
- package/vendor/cli/dist/connectors/README.md +15 -2
- package/vendor/cli/dist/connectors/google_gmail.ts +1 -0
- package/vendor/cli/dist/connectors/jira-helpers.ts +313 -0
- package/vendor/cli/dist/connectors/jira.ts +29 -221
- package/vendor/cli/dist/connectors/linkedin-browser-script.ts +13 -0
- package/vendor/cli/dist/connectors/linkedin-draft-page.js +104 -0
- package/vendor/cli/dist/connectors/linkedin-drafts.ts +73 -0
- package/vendor/cli/dist/connectors/linkedin-identity.ts +32 -0
- package/vendor/cli/dist/connectors/linkedin-inbox-adapter.js +1182 -0
- package/vendor/cli/dist/connectors/linkedin-inbox.ts +156 -0
- package/vendor/cli/dist/connectors/linkedin-like.ts +114 -0
- package/vendor/cli/dist/connectors/linkedin-takeout.ts +577 -0
- package/vendor/cli/dist/connectors/linkedin.ts +4017 -0
- package/vendor/cli/dist/connectors/reddit.ts +92 -20
- package/vendor/cli/dist/connectors/slack.ts +1 -0
- package/vendor/cli/dist/connectors/takeout-utils.ts +240 -0
- package/vendor/cli/dist/connectors/tiktok-inbox-page.js +9 -9
- package/vendor/cli/dist/connectors/tiktok-web-page.js +21 -15
- package/vendor/cli/dist/connectors/tiktok_web.ts +63 -30
- package/vendor/cli/dist/connectors/whatsapp-web-adapter.js +104 -574
- package/vendor/cli/dist/connectors/whatsapp-web-helpers.ts +30 -444
- package/vendor/cli/dist/connectors/whatsapp-web-identity.ts +20 -6
- package/vendor/cli/dist/connectors/whatsapp_web.ts +46 -646
- package/vendor/cli/dist/connectors/x-dm-adapter.js +556 -0
- package/vendor/cli/dist/connectors/x-identity.ts +33 -2
- package/vendor/cli/dist/connectors/x.ts +266 -120
- package/vendor/cli/dist/runtime-components.json +4 -4
- package/vendor/cli/dist/templates/AGENTS.md.tmpl +12 -5
- package/vendor/cli/package.json +1 -1
- package/vendor/connector-sdk/dist/browser-handoff-banner.d.ts +39 -0
- package/vendor/connector-sdk/dist/browser-handoff-banner.js +175 -0
- package/vendor/connector-sdk/dist/browser-source-read.d.ts +52 -0
- package/vendor/connector-sdk/dist/browser-source-read.js +107 -0
- package/vendor/connector-sdk/dist/connector-types.d.ts +10 -0
- package/vendor/connector-sdk/dist/index.d.ts +3 -0
- package/vendor/connector-sdk/dist/index.js +2 -0
- package/vendor/connector-sdk/package.json +9 -1
- package/vendor/connector-worker/dist/agent-turn/gateway-tools.d.ts +2 -2
- package/vendor/connector-worker/dist/agent-turn/gateway-tools.js +2 -2
- package/vendor/connector-worker/dist/agent-turn/guest-entry.js +49 -8
- package/vendor/connector-worker/dist/agent-turn/guest.bundle.js +650 -448
- package/vendor/connector-worker/dist/agent-turn/native-session.d.ts +12 -0
- package/vendor/connector-worker/dist/agent-turn/native-session.js +32 -6
- package/vendor/connector-worker/dist/agent-turn/types.d.ts +24 -3
- package/vendor/connector-worker/dist/daemon/agent-turn.js +5 -1
- package/vendor/connector-worker/dist/daemon/automation.d.ts +4 -11
- package/vendor/connector-worker/dist/daemon/automation.js +26 -21
- package/vendor/connector-worker/dist/daemon/client.d.ts +2 -4
- package/vendor/connector-worker/dist/daemon/client.js +0 -3
- package/vendor/connector-worker/dist/daemon/executor.js +0 -3
- package/vendor/connector-worker/dist/daemon/mac-device-daemon.d.ts +4 -3
- package/vendor/connector-worker/dist/daemon/mac-device-daemon.js +4 -10
- package/vendor/connector-worker/dist/daemon/native-bridge/executor.js +27 -25
- package/vendor/connector-worker/dist/daemon/start.js +3 -15
- package/vendor/connector-worker/dist/daemon/terminal-failure.js +0 -11
- package/vendor/connector-worker/dist/daemon/worker-id.d.ts +17 -0
- package/vendor/connector-worker/dist/daemon/worker-id.js +22 -0
- package/vendor/connector-worker/dist/isolate/prelude.js +93 -44
- package/vendor/connector-worker/dist/mac-device-daemon.js +14 -26
- package/vendor/connector-worker/package.json +1 -1
- package/vendor/core/dist/agent-policy.js +0 -3
- package/vendor/core/dist/agent-tooling.d.ts +1 -1
- package/vendor/core/dist/agent-tooling.js +3 -1
- package/vendor/core/dist/capabilities.d.ts +1 -1
- package/vendor/core/dist/capabilities.js +0 -1
- package/vendor/core/dist/connector-query-errors.d.ts +32 -0
- package/vendor/core/dist/connector-query-errors.js +41 -1
- package/vendor/core/dist/contracts/agent-settings.d.ts +6 -2
- package/vendor/core/dist/contracts/agent-settings.js +12 -1
- package/vendor/core/dist/contracts/tools/manage-conversations.js +2 -2
- package/vendor/core/dist/contracts/tools/manage-feeds.d.ts +14 -0
- package/vendor/core/dist/contracts/tools/manage-feeds.js +17 -0
- package/vendor/core/dist/contracts/tools/manage-views.d.ts +53 -234
- package/vendor/core/dist/contracts/tools/manage-views.js +50 -76
- package/vendor/core/dist/contracts/tools/view-attach.d.ts +5 -8
- package/vendor/core/dist/contracts/tools/view-attach.js +14 -31
- package/vendor/core/dist/contracts/tools/view-definition.d.ts +21 -0
- package/vendor/core/dist/contracts/tools/view-definition.js +165 -0
- package/vendor/core/dist/contracts/worker/protocol.d.ts +47 -133
- package/vendor/core/dist/contracts/worker/protocol.js +33 -62
- package/vendor/core/dist/device-pin-tombstones.d.ts +44 -0
- package/vendor/core/dist/device-pin-tombstones.js +57 -0
- package/vendor/core/dist/index.d.ts +3 -2
- package/vendor/core/dist/index.js +3 -1
- package/vendor/core/dist/logger.js +10 -25
- package/vendor/core/dist/suggestions.d.ts +15 -2
- package/vendor/core/dist/suggestions.js +48 -3
- package/vendor/core/dist/types.d.ts +26 -0
- package/vendor/core/dist/types.js +5 -0
- package/vendor/core/dist/utils/diagnostic-scrubber.js +258 -21
- package/vendor/core/dist/worker/wire.d.ts +0 -10
- package/vendor/core/package.json +16 -10
- package/vendor/postgres/cf/src/connection.js +18 -0
- package/vendor/postgres/cjs/src/connection.js +18 -0
- package/vendor/postgres/src/connection.js +18 -0
- package/vendor/connector-worker/dist/daemon/device-chat.d.ts +0 -9
- package/vendor/connector-worker/dist/daemon/device-chat.js +0 -61
- package/vendor/core/dist/contracts/worker/device-chat.d.ts +0 -4
- package/vendor/core/dist/contracts/worker/device-chat.js +0 -26
- /package/vendor/postgres/{.bun-tag-f39dafdb2305d504 → .bun-tag-2f292c6554a8475e} +0 -0
|
@@ -3,10 +3,10 @@
|
|
|
3
3
|
*
|
|
4
4
|
* Ported from the Owletto extension's whatsapp-web-main-v1.js. WhatsApp's
|
|
5
5
|
* normalization and action code retain the extension implementation; the
|
|
6
|
-
* connector-owned feed listener below
|
|
7
|
-
* through the generic browser transport.
|
|
8
|
-
* reaches into WhatsApp's own module registry via window.require
|
|
9
|
-
*
|
|
6
|
+
* connector-owned feed listener below observes new messages and emits
|
|
7
|
+
* references only (never content) through the generic browser transport. It
|
|
8
|
+
* reaches into WhatsApp's own module registry via window.require; those private
|
|
9
|
+
* module shapes are load-bearing.
|
|
10
10
|
*
|
|
11
11
|
* How it gets into the page: the connector serialises this function with
|
|
12
12
|
* Function.prototype.toString() and runs `(<source>)()` through the generic
|
|
@@ -33,8 +33,11 @@ export function whatsAppWebAdapterProgram() {
|
|
|
33
33
|
// when this number moves: shipping a fix under the old number leaves every
|
|
34
34
|
// already-open tab running the previous code with nothing to show for it.
|
|
35
35
|
// Keep in lockstep with WHATSAPP_ADAPTER_VERSION in whatsapp-web-helpers.ts.
|
|
36
|
-
const ADAPTER_VERSION =
|
|
36
|
+
const ADAPTER_VERSION = 26;
|
|
37
37
|
const SOURCE_ERROR_ID = "whatsapp-web:source-observation-error";
|
|
38
|
+
// Storage rows one `chat:` read examines; a whole personal history is
|
|
39
|
+
// usually under this, so one read answers.
|
|
40
|
+
const CHAT_SCAN_ROWS = 20_000;
|
|
38
41
|
const MAX_RECORD_BYTES = 128 * 1024;
|
|
39
42
|
const MAX_BUFFER_BYTES = 16 * 1024 * 1024;
|
|
40
43
|
const MAX_BUFFER_ENTRIES = 10_000;
|
|
@@ -91,8 +94,7 @@ export function whatsAppWebAdapterProgram() {
|
|
|
91
94
|
});
|
|
92
95
|
};
|
|
93
96
|
const resident = listeners.get(request.bridge_id);
|
|
94
|
-
const previous = resident?.request.
|
|
95
|
-
&& resident?.request.chat_filter === request.chat_filter
|
|
97
|
+
const previous = resident?.request.chat_filter === request.chat_filter
|
|
96
98
|
&& resident?.request.minimum_timestamp === request.minimum_timestamp ? resident : null;
|
|
97
99
|
detach(request.bridge_id);
|
|
98
100
|
// A new listener identity fences pending normalization from the detached
|
|
@@ -109,19 +111,17 @@ export function whatsAppWebAdapterProgram() {
|
|
|
109
111
|
state.token = request.token;
|
|
110
112
|
state.request = request;
|
|
111
113
|
state.post = (entry) => post(state, entry);
|
|
112
|
-
const observe = (model
|
|
114
|
+
const observe = (model) => {
|
|
113
115
|
if (state.error) return;
|
|
114
116
|
const sequence = ++state.sequence;
|
|
115
|
-
|
|
116
|
-
|
|
117
|
+
// A throwing model getter must reach the error record below, not escape
|
|
118
|
+
// the collection handler, so the reference is read inside the promise.
|
|
119
|
+
void new Promise((resolve) => resolve(messageReference(model))).then((record) => {
|
|
117
120
|
if (listeners.get(state.id) !== state || !record || record.timestamp <= 0) return;
|
|
118
121
|
if (state.request.chat_filter === "group" && !record.is_group) return;
|
|
119
122
|
if (state.request.chat_filter === "individual" && record.is_group) return;
|
|
120
123
|
if (state.request.minimum_timestamp != null && record.timestamp < state.request.minimum_timestamp) return;
|
|
121
|
-
|
|
122
|
-
// still pass; they are exactly what the recent collection window misses.
|
|
123
|
-
if (kind === "add" && record.timestamp < state.request.recent_since) return;
|
|
124
|
-
if (request.references_only) record = {
|
|
124
|
+
record = {
|
|
125
125
|
id: record.id,
|
|
126
126
|
events: [{ id: record.id, event_type: "message.created", resource_ref: record.id,
|
|
127
127
|
resource_type: "message", occurred_at: new Date(record.timestamp * 1000).toISOString() }],
|
|
@@ -154,8 +154,8 @@ export function whatsAppWebAdapterProgram() {
|
|
|
154
154
|
reportError(state, "WhatsApp source observation failed; recovery is required", sequence);
|
|
155
155
|
});
|
|
156
156
|
};
|
|
157
|
-
state.onAdd = (model) => observe(model
|
|
158
|
-
state.onChange = (model) => observe(model
|
|
157
|
+
state.onAdd = (model) => observe(model);
|
|
158
|
+
state.onChange = (model) => observe(model);
|
|
159
159
|
state.onReply = (event) => {
|
|
160
160
|
if (event.source !== window || event.origin !== location.origin || event.data?.token !== state.token) return;
|
|
161
161
|
if (event.data.type === "lobu-feed-record:stop") { detach(state.id); return; }
|
|
@@ -191,7 +191,7 @@ export function whatsAppWebAdapterProgram() {
|
|
|
191
191
|
// not server delivery: the server advances the cursor only with its own commit.
|
|
192
192
|
async function publishChanges(request) {
|
|
193
193
|
const state = listeners.get(request.bridge_id);
|
|
194
|
-
if (!state
|
|
194
|
+
if (!state || state.token !== request.token) throw new Error("Source listener is unavailable");
|
|
195
195
|
const record = request.batch;
|
|
196
196
|
const bytes = new TextEncoder().encode(JSON.stringify(record)).length;
|
|
197
197
|
const previous = state.pending.get(record.id);
|
|
@@ -266,6 +266,50 @@ export function whatsAppWebAdapterProgram() {
|
|
|
266
266
|
return null;
|
|
267
267
|
}
|
|
268
268
|
|
|
269
|
+
/**
|
|
270
|
+
* Every canonical form of the account's own identity. `meUser()` prefers
|
|
271
|
+
* the phone-number WID, but a group can address a member by its LID WID
|
|
272
|
+
* instead, so a mention or a quote of the account must match either form.
|
|
273
|
+
*/
|
|
274
|
+
async function ownJids() {
|
|
275
|
+
const module = requireFirst(["WAWebUserPrefsMeUser"]);
|
|
276
|
+
const candidates = [meUser()];
|
|
277
|
+
for (const accessor of ["getMaybeMePnUser", "getMaybeMeLidUser"]) {
|
|
278
|
+
try {
|
|
279
|
+
candidates.push(module?.[accessor]?.());
|
|
280
|
+
} catch {
|
|
281
|
+
/* WhatsApp's private module graph throws freely; a miss is not fatal. */
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
const forms = new Set();
|
|
285
|
+
for (const value of candidates) {
|
|
286
|
+
const own = await canonicalJid(value);
|
|
287
|
+
if (own.jid) forms.add(own.jid);
|
|
288
|
+
if (own.lid) forms.add(own.lid);
|
|
289
|
+
}
|
|
290
|
+
return forms;
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
/**
|
|
294
|
+
* Whether a message addresses the account: it @mentions the account, or it
|
|
295
|
+
* replies to (quotes) one of the account's messages.
|
|
296
|
+
*/
|
|
297
|
+
async function mentionsOwnAccount(row) {
|
|
298
|
+
const addressed = [
|
|
299
|
+
...(Array.isArray(row.mentionedJidList) ? row.mentionedJidList : []),
|
|
300
|
+
row.quotedParticipant,
|
|
301
|
+
].filter(Boolean);
|
|
302
|
+
if (addressed.length === 0) return false;
|
|
303
|
+
const own = await ownJids();
|
|
304
|
+
for (const value of addressed) {
|
|
305
|
+
const candidate = await canonicalJid(value);
|
|
306
|
+
if (own.has(candidate.jid) || (candidate.lid && own.has(candidate.lid))) {
|
|
307
|
+
return true;
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
return false;
|
|
311
|
+
}
|
|
312
|
+
|
|
269
313
|
/**
|
|
270
314
|
* Module names for each write op, most-current first.
|
|
271
315
|
*
|
|
@@ -484,20 +528,8 @@ export function whatsAppWebAdapterProgram() {
|
|
|
484
528
|
}
|
|
485
529
|
|
|
486
530
|
/**
|
|
487
|
-
*
|
|
488
|
-
*
|
|
489
|
-
* `normalizeMessage` runs per message and used to walk a whole collection on
|
|
490
|
-
* each call -- the chat name over 947 chats, the sender name over 5323
|
|
491
|
-
* contacts. That is O(messages x collection) of SYNCHRONOUS work: measured on
|
|
492
|
-
* a live prod tab the contact scan alone projects to ~6.8s, a real collect
|
|
493
|
-
* never yielded the main thread, and the extension's 90s run fence killed the
|
|
494
|
-
* run. Every run was slower than the last as backfill grew the store.
|
|
495
|
-
*
|
|
496
|
-
* Built once per collect and thrown away with it -- no cache to invalidate,
|
|
497
|
-
* and nothing here outlives the call. Only names are indexed: they read
|
|
498
|
-
* static fields, whereas `collectionByRawId` serves the reaction paths, which
|
|
499
|
-
* mutate a model in place without changing the collection, so that one keeps
|
|
500
|
-
* scanning.
|
|
531
|
+
* Build name lookups once per read or search so normalizing each message
|
|
532
|
+
* does not rescan every chat and contact. Nothing here outlives the call.
|
|
501
533
|
*/
|
|
502
534
|
function nameIndex(collections) {
|
|
503
535
|
const byId = (collection) => {
|
|
@@ -545,8 +577,7 @@ export function whatsAppWebAdapterProgram() {
|
|
|
545
577
|
const collections =
|
|
546
578
|
collectionsOverride ?? requireFirst(["WAWebCollections"]);
|
|
547
579
|
if (!collections) return null;
|
|
548
|
-
//
|
|
549
|
-
// in so the whole run shares it.
|
|
580
|
+
// Reads and searches share one name index across their result batch.
|
|
550
581
|
const nameLookup = names ?? nameIndex(collections);
|
|
551
582
|
let row = modelData(model);
|
|
552
583
|
let key = row.id ?? model?.id;
|
|
@@ -623,6 +654,7 @@ export function whatsAppWebAdapterProgram() {
|
|
|
623
654
|
: null,
|
|
624
655
|
media_filename: row.filename ?? row.fileName ?? row.title ?? null,
|
|
625
656
|
quoted_id: quotedId,
|
|
657
|
+
mentions_me: await mentionsOwnAccount(row),
|
|
626
658
|
is_forwarded: Boolean(row.isForwarded || Number(row.forwardingScore) > 0),
|
|
627
659
|
is_starred: Boolean(row.star ?? row.isStarred),
|
|
628
660
|
is_system_event: SYSTEM_TYPES.has(type),
|
|
@@ -671,7 +703,7 @@ export function whatsAppWebAdapterProgram() {
|
|
|
671
703
|
// Socket.stream was DISCONNECTED, then both were CONNECTED after reload.
|
|
672
704
|
// Current WhatsApp exposes account and transport state separately. An
|
|
673
705
|
// authenticated account can retain hydrated stores while its stream is
|
|
674
|
-
// disconnected; those cached messages are not a successful live
|
|
706
|
+
// disconnected; those cached messages are not a successful live read.
|
|
675
707
|
const socket = requireFirst(["WAWebSocketModel"])?.Socket;
|
|
676
708
|
const connection =
|
|
677
709
|
socket ??
|
|
@@ -776,7 +808,6 @@ export function whatsAppWebAdapterProgram() {
|
|
|
776
808
|
}
|
|
777
809
|
|
|
778
810
|
function operationCapabilities() {
|
|
779
|
-
const history = requireFirst(["WAWebChatLoadMessages"]);
|
|
780
811
|
const search = requireFirst([
|
|
781
812
|
"WAWebChatMessageSearch",
|
|
782
813
|
"WAWebMessageSearch",
|
|
@@ -787,23 +818,13 @@ export function whatsAppWebAdapterProgram() {
|
|
|
787
818
|
const edit = requireFirst(EDIT_MODULES);
|
|
788
819
|
const reaction = requireFirst(REACTION_MODULES);
|
|
789
820
|
const revoke = requireFirst(REVOKE_MODULES);
|
|
790
|
-
// Same nesting as the download path: the module root only exports
|
|
791
|
-
// `{ enforceKaleidoscopeScore, downloadManager }`.
|
|
792
|
-
const mediaModule = requireFirst(["WAWebDownloadManager"]);
|
|
793
|
-
const media = mediaModule?.downloadManager ?? mediaModule;
|
|
794
821
|
const hasFunction = (module, names) =>
|
|
795
822
|
names.some((name) => typeof module?.[name] === "function");
|
|
796
823
|
return {
|
|
797
|
-
collect: hasFunction(history, ["loadEarlierMsgs"]),
|
|
798
824
|
read_messages:
|
|
799
825
|
typeof indexedDB !== "undefined" &&
|
|
800
826
|
typeof requireFirst(["WAWebMsgKey"])?.fromString === "function" &&
|
|
801
827
|
typeof requireFirst(["WAWebCollections"])?.Msg?.getMessagesById === "function",
|
|
802
|
-
download_media:
|
|
803
|
-
hasFunction(media, ["downloadAndMaybeDecrypt"]) ||
|
|
804
|
-
models(requireFirst(["WAWebCollections"])?.Msg).some(
|
|
805
|
-
(model) => typeof model?.downloadMedia === "function"
|
|
806
|
-
),
|
|
807
828
|
search_messages:
|
|
808
829
|
hasFunction(search, ["searchMessages", "searchMsgs", "search"]) ||
|
|
809
830
|
Boolean(requireFirst(["WAWebCollections"])?.Msg),
|
|
@@ -829,130 +850,6 @@ export function whatsAppWebAdapterProgram() {
|
|
|
829
850
|
};
|
|
830
851
|
}
|
|
831
852
|
|
|
832
|
-
function oldestMessage(chat) {
|
|
833
|
-
let oldest = null;
|
|
834
|
-
for (const message of models(modelData(chat).msgs ?? chat?.msgs)) {
|
|
835
|
-
const row = modelData(message);
|
|
836
|
-
const timestamp = unixSeconds(row);
|
|
837
|
-
const id = rawId(row.id ?? message?.id);
|
|
838
|
-
if (!id) continue;
|
|
839
|
-
if (
|
|
840
|
-
!oldest ||
|
|
841
|
-
timestamp < oldest.timestamp ||
|
|
842
|
-
(timestamp === oldest.timestamp && id < oldest.id)
|
|
843
|
-
) {
|
|
844
|
-
oldest = { timestamp, id };
|
|
845
|
-
}
|
|
846
|
-
}
|
|
847
|
-
return oldest;
|
|
848
|
-
}
|
|
849
|
-
|
|
850
|
-
function chatHasEarlier(chat) {
|
|
851
|
-
const row = modelData(chat);
|
|
852
|
-
const state =
|
|
853
|
-
row.msgs?.msgLoadState ??
|
|
854
|
-
chat?.msgs?.msgLoadState ??
|
|
855
|
-
row.msgLoadState ??
|
|
856
|
-
{};
|
|
857
|
-
if (state.noEarlierMsgs === true || state.hasEarlierMsgs === false)
|
|
858
|
-
return false;
|
|
859
|
-
if (row.hasEarlierMsgs === false) return false;
|
|
860
|
-
return true;
|
|
861
|
-
}
|
|
862
|
-
|
|
863
|
-
async function loadEarlier(chat, limit, deadline) {
|
|
864
|
-
const loader = requireFirst(["WAWebChatLoadMessages"]);
|
|
865
|
-
if (typeof loader?.loadEarlierMsgs !== "function") {
|
|
866
|
-
return {
|
|
867
|
-
available: false,
|
|
868
|
-
reason: "WAWebChatLoadMessages.loadEarlierMsgs unavailable",
|
|
869
|
-
loads: 0,
|
|
870
|
-
};
|
|
871
|
-
}
|
|
872
|
-
let loads = 0;
|
|
873
|
-
const beforeIds = new Set(
|
|
874
|
-
models(modelData(chat).msgs ?? chat?.msgs)
|
|
875
|
-
.map((message) => rawId(modelData(message).id ?? message?.id))
|
|
876
|
-
.filter(Boolean)
|
|
877
|
-
);
|
|
878
|
-
let previous = oldestMessage(chat);
|
|
879
|
-
let madeProgress = false;
|
|
880
|
-
while (loads < limit && chatHasEarlier(chat)) {
|
|
881
|
-
if (deadline && Date.now() >= deadline)
|
|
882
|
-
throw new Error("history deadline budget exhausted");
|
|
883
|
-
await loader.loadEarlierMsgs({ chat, msgCollection: chat.msgs });
|
|
884
|
-
loads += 1;
|
|
885
|
-
const next = oldestMessage(chat);
|
|
886
|
-
if (
|
|
887
|
-
!next ||
|
|
888
|
-
(previous &&
|
|
889
|
-
next.id === previous.id &&
|
|
890
|
-
next.timestamp === previous.timestamp)
|
|
891
|
-
)
|
|
892
|
-
break;
|
|
893
|
-
madeProgress = true;
|
|
894
|
-
previous = next;
|
|
895
|
-
}
|
|
896
|
-
let hasMore = chatHasEarlier(chat);
|
|
897
|
-
const loadedIds = () =>
|
|
898
|
-
models(modelData(chat).msgs ?? chat?.msgs)
|
|
899
|
-
.map((message) => rawId(modelData(message).id ?? message?.id))
|
|
900
|
-
.filter((id) => id && !beforeIds.has(id));
|
|
901
|
-
if (hasMore && !madeProgress) {
|
|
902
|
-
return {
|
|
903
|
-
available: true,
|
|
904
|
-
loads,
|
|
905
|
-
has_more: true,
|
|
906
|
-
loaded_ids: loadedIds(),
|
|
907
|
-
frontier_advanced: false,
|
|
908
|
-
error: "WhatsApp history loader made no progress",
|
|
909
|
-
};
|
|
910
|
-
}
|
|
911
|
-
if (hasMore) {
|
|
912
|
-
const phone =
|
|
913
|
-
loader.loadEarlierMsgsFromPhone ??
|
|
914
|
-
loader.requestEarlierMsgsFromPhone ??
|
|
915
|
-
loader.loadEarlierMsgsFromServer;
|
|
916
|
-
const state =
|
|
917
|
-
modelData(chat).msgs?.msgLoadState ?? chat?.msgs?.msgLoadState ?? {};
|
|
918
|
-
const wantsPhone =
|
|
919
|
-
state.canRequestFromPhone === true || state.hasMoreFromPhone === true;
|
|
920
|
-
if (wantsPhone && typeof phone !== "function") {
|
|
921
|
-
return {
|
|
922
|
-
available: true,
|
|
923
|
-
loads,
|
|
924
|
-
has_more: true,
|
|
925
|
-
loaded_ids: loadedIds(),
|
|
926
|
-
frontier_advanced: false,
|
|
927
|
-
error:
|
|
928
|
-
"phone-assisted WhatsApp history is advertised but its loader is unavailable",
|
|
929
|
-
};
|
|
930
|
-
}
|
|
931
|
-
if (typeof phone === "function" && wantsPhone) {
|
|
932
|
-
try {
|
|
933
|
-
await phone.call(loader, { chat, msgCollection: chat.msgs });
|
|
934
|
-
hasMore = chatHasEarlier(chat);
|
|
935
|
-
} catch (error) {
|
|
936
|
-
return {
|
|
937
|
-
available: true,
|
|
938
|
-
loads,
|
|
939
|
-
has_more: true,
|
|
940
|
-
loaded_ids: loadedIds(),
|
|
941
|
-
frontier_advanced: false,
|
|
942
|
-
error: `phone-assisted history failed: ${String(error)}`,
|
|
943
|
-
};
|
|
944
|
-
}
|
|
945
|
-
}
|
|
946
|
-
}
|
|
947
|
-
return {
|
|
948
|
-
available: true,
|
|
949
|
-
loads,
|
|
950
|
-
has_more: hasMore,
|
|
951
|
-
loaded_ids: loadedIds(),
|
|
952
|
-
frontier_advanced: true,
|
|
953
|
-
};
|
|
954
|
-
}
|
|
955
|
-
|
|
956
853
|
function uniqueMessageModels(collections) {
|
|
957
854
|
const byId = new Map();
|
|
958
855
|
for (const message of models(collections.Msg)) {
|
|
@@ -968,208 +865,6 @@ export function whatsAppWebAdapterProgram() {
|
|
|
968
865
|
return [...byId.values()];
|
|
969
866
|
}
|
|
970
867
|
|
|
971
|
-
async function collect(request) {
|
|
972
|
-
const status = readiness();
|
|
973
|
-
if (!status.ready) return { ok: false, error: status };
|
|
974
|
-
const collections = requireFirst(["WAWebCollections"]);
|
|
975
|
-
const chatFilter = ["all", "individual", "group"].includes(
|
|
976
|
-
request.chat_filter
|
|
977
|
-
)
|
|
978
|
-
? request.chat_filter
|
|
979
|
-
: "all";
|
|
980
|
-
const chatRows = [];
|
|
981
|
-
for (const chat of models(collections.Chat)) {
|
|
982
|
-
const raw = widString(modelData(chat).id ?? chat?.id);
|
|
983
|
-
const jid = (await canonicalJid(raw)).jid;
|
|
984
|
-
if (
|
|
985
|
-
!jid ||
|
|
986
|
-
jid === "status@broadcast" ||
|
|
987
|
-
jid.endsWith("@newsletter") ||
|
|
988
|
-
(chatFilter === "group" && !jid.endsWith("@g.us")) ||
|
|
989
|
-
(chatFilter === "individual" && jid.endsWith("@g.us"))
|
|
990
|
-
)
|
|
991
|
-
continue;
|
|
992
|
-
chatRows.push({ chat, jid });
|
|
993
|
-
}
|
|
994
|
-
chatRows.sort((a, b) => a.jid.localeCompare(b.jid));
|
|
995
|
-
const backfill = request.backfill ?? { complete: false, chats: {} };
|
|
996
|
-
const updates = {};
|
|
997
|
-
let cursor = backfill.cursor_chat_jid ?? null;
|
|
998
|
-
const loadedByChat = new Map();
|
|
999
|
-
const backfillMessageIds = new Set();
|
|
1000
|
-
const eligible = request.backfill_disabled
|
|
1001
|
-
? []
|
|
1002
|
-
: chatRows.filter(
|
|
1003
|
-
(entry) => backfill.chats?.[entry.jid]?.has_more !== false
|
|
1004
|
-
);
|
|
1005
|
-
if (eligible.length > 0) {
|
|
1006
|
-
let start = cursor
|
|
1007
|
-
? eligible.findIndex((entry) => entry.jid > cursor)
|
|
1008
|
-
: 0;
|
|
1009
|
-
if (start < 0) start = 0;
|
|
1010
|
-
const rotated = [...eligible.slice(start), ...eligible.slice(0, start)];
|
|
1011
|
-
const selected = rotated.slice(
|
|
1012
|
-
0,
|
|
1013
|
-
Math.max(1, Number(request.max_chats) || 1)
|
|
1014
|
-
);
|
|
1015
|
-
// The connector sends a relative budget, not an absolute instant: it runs
|
|
1016
|
-
// on a different machine than this page, so any shared epoch would be off
|
|
1017
|
-
// by the clock skew between them. Resolving it here against this page's
|
|
1018
|
-
// own clock keeps the budget exact.
|
|
1019
|
-
const budgetMs = Number(request.budget_ms);
|
|
1020
|
-
const historyDeadline =
|
|
1021
|
-
Number.isFinite(budgetMs) && budgetMs > 0
|
|
1022
|
-
? Date.now() + budgetMs
|
|
1023
|
-
: null;
|
|
1024
|
-
for (const entry of selected) {
|
|
1025
|
-
if (historyDeadline && Date.now() >= historyDeadline) break;
|
|
1026
|
-
try {
|
|
1027
|
-
const loaded = await loadEarlier(
|
|
1028
|
-
entry.chat,
|
|
1029
|
-
Math.max(1, Number(request.max_loads_per_chat) || 1),
|
|
1030
|
-
historyDeadline
|
|
1031
|
-
);
|
|
1032
|
-
if (!loaded.available) {
|
|
1033
|
-
return {
|
|
1034
|
-
ok: false,
|
|
1035
|
-
error: {
|
|
1036
|
-
state: "capability_unavailable",
|
|
1037
|
-
reason: loaded.reason,
|
|
1038
|
-
},
|
|
1039
|
-
};
|
|
1040
|
-
}
|
|
1041
|
-
const oldest = oldestMessage(entry.chat);
|
|
1042
|
-
for (const id of loaded.loaded_ids) backfillMessageIds.add(id);
|
|
1043
|
-
loadedByChat.set(entry.jid, loaded.loaded_ids);
|
|
1044
|
-
if (loaded.frontier_advanced === false) {
|
|
1045
|
-
updates[entry.jid] = {
|
|
1046
|
-
...(backfill.chats?.[entry.jid] ?? {}),
|
|
1047
|
-
error: loaded.error,
|
|
1048
|
-
has_more: true,
|
|
1049
|
-
loads: loaded.loads,
|
|
1050
|
-
};
|
|
1051
|
-
continue;
|
|
1052
|
-
}
|
|
1053
|
-
updates[entry.jid] = {
|
|
1054
|
-
oldest_timestamp: oldest?.timestamp ?? null,
|
|
1055
|
-
oldest_id: oldest?.id ?? null,
|
|
1056
|
-
has_more: loaded.has_more,
|
|
1057
|
-
loads: loaded.loads,
|
|
1058
|
-
};
|
|
1059
|
-
cursor = entry.jid;
|
|
1060
|
-
} catch (error) {
|
|
1061
|
-
if (/unavailable/i.test(String(error))) {
|
|
1062
|
-
return {
|
|
1063
|
-
ok: false,
|
|
1064
|
-
error: {
|
|
1065
|
-
state: "capability_unavailable",
|
|
1066
|
-
reason: String(error),
|
|
1067
|
-
},
|
|
1068
|
-
};
|
|
1069
|
-
}
|
|
1070
|
-
updates[entry.jid] = {
|
|
1071
|
-
...(backfill.chats?.[entry.jid] ?? {}),
|
|
1072
|
-
error: String(error),
|
|
1073
|
-
has_more: true,
|
|
1074
|
-
};
|
|
1075
|
-
}
|
|
1076
|
-
}
|
|
1077
|
-
}
|
|
1078
|
-
|
|
1079
|
-
const minimum = Number.isFinite(Number(request.minimum_timestamp))
|
|
1080
|
-
? Number(request.minimum_timestamp)
|
|
1081
|
-
: null;
|
|
1082
|
-
const recent = Number.isFinite(Number(request.recent_since))
|
|
1083
|
-
? Number(request.recent_since)
|
|
1084
|
-
: null;
|
|
1085
|
-
const dirtyRanges = Array.isArray(request.dirty_ranges)
|
|
1086
|
-
? request.dirty_ranges
|
|
1087
|
-
: [];
|
|
1088
|
-
const dirtyByMessageId = new Map(
|
|
1089
|
-
dirtyRanges
|
|
1090
|
-
.filter((marker) => marker?.message_id)
|
|
1091
|
-
.map((marker) => [marker.message_id, marker])
|
|
1092
|
-
);
|
|
1093
|
-
const normalized = [];
|
|
1094
|
-
const quarantined = [];
|
|
1095
|
-
const names = nameIndex(collections);
|
|
1096
|
-
for (const model of uniqueMessageModels(collections)) {
|
|
1097
|
-
const message = await normalizeMessage(
|
|
1098
|
-
model,
|
|
1099
|
-
"reconcile",
|
|
1100
|
-
collections,
|
|
1101
|
-
names
|
|
1102
|
-
);
|
|
1103
|
-
if (!message) continue;
|
|
1104
|
-
if (message.timestamp <= 0) {
|
|
1105
|
-
quarantined.push({
|
|
1106
|
-
key: `${message.chat_jid}:${message.id}`,
|
|
1107
|
-
message_id: message.id,
|
|
1108
|
-
chat_jid: message.chat_jid,
|
|
1109
|
-
reason: "missing_stable_timestamp",
|
|
1110
|
-
});
|
|
1111
|
-
continue;
|
|
1112
|
-
}
|
|
1113
|
-
if (chatFilter === "group" && !message.is_group) continue;
|
|
1114
|
-
if (chatFilter === "individual" && message.is_group) continue;
|
|
1115
|
-
if (minimum != null && message.timestamp < minimum) continue;
|
|
1116
|
-
if (
|
|
1117
|
-
recent != null &&
|
|
1118
|
-
message.timestamp < recent &&
|
|
1119
|
-
!backfillMessageIds.has(message.id) &&
|
|
1120
|
-
!dirtyByMessageId.has(message.id)
|
|
1121
|
-
)
|
|
1122
|
-
continue;
|
|
1123
|
-
normalized.push(message);
|
|
1124
|
-
}
|
|
1125
|
-
normalized.sort(
|
|
1126
|
-
(a, b) => a.timestamp - b.timestamp || a.id.localeCompare(b.id)
|
|
1127
|
-
);
|
|
1128
|
-
const maxMessages = Math.max(
|
|
1129
|
-
1,
|
|
1130
|
-
Math.min(Number(request.max_messages) || 1_000, 2_000)
|
|
1131
|
-
);
|
|
1132
|
-
const byId = new Map(normalized.map((message) => [message.id, message]));
|
|
1133
|
-
const historyPages = [];
|
|
1134
|
-
for (const [chatJid, ids] of loadedByChat) {
|
|
1135
|
-
historyPages.push({
|
|
1136
|
-
chat_jid: chatJid,
|
|
1137
|
-
...(updates[chatJid] ?? {}),
|
|
1138
|
-
messages: ids.map((id) => byId.get(id)).filter(Boolean),
|
|
1139
|
-
});
|
|
1140
|
-
}
|
|
1141
|
-
const messages = normalized
|
|
1142
|
-
.filter((message) => !backfillMessageIds.has(message.id))
|
|
1143
|
-
.slice(-maxMessages)
|
|
1144
|
-
.sort((a, b) => a.timestamp - b.timestamp || a.id.localeCompare(b.id));
|
|
1145
|
-
const mergedChats = { ...(backfill.chats ?? {}), ...updates };
|
|
1146
|
-
return {
|
|
1147
|
-
ok: true,
|
|
1148
|
-
status,
|
|
1149
|
-
messages,
|
|
1150
|
-
history_pages: historyPages,
|
|
1151
|
-
quarantined,
|
|
1152
|
-
dirty_reconciled: normalized
|
|
1153
|
-
.filter((message) => dirtyByMessageId.has(message.id))
|
|
1154
|
-
.map((message) => ({
|
|
1155
|
-
key: dirtyByMessageId.get(message.id).key,
|
|
1156
|
-
message_id: message.id,
|
|
1157
|
-
})),
|
|
1158
|
-
backfill: {
|
|
1159
|
-
// `chatRows` is empty when the tab answered `probe` but its Chat
|
|
1160
|
-
// collection has not populated yet. A bare `.every()` is vacuously true
|
|
1161
|
-
// there, which would record a finished backfill that never ran.
|
|
1162
|
-
complete:
|
|
1163
|
-
request.backfill_disabled === true ||
|
|
1164
|
-
(chatRows.length > 0 &&
|
|
1165
|
-
chatRows.every((entry) => mergedChats[entry.jid]?.has_more === false)),
|
|
1166
|
-
cursor_chat_jid: cursor,
|
|
1167
|
-
inventory: chatRows.map((entry) => entry.jid),
|
|
1168
|
-
chats: updates,
|
|
1169
|
-
},
|
|
1170
|
-
};
|
|
1171
|
-
}
|
|
1172
|
-
|
|
1173
868
|
async function resolveChat(collections, input) {
|
|
1174
869
|
if (input?.self_chat === true) {
|
|
1175
870
|
const me = meUser();
|
|
@@ -1253,6 +948,11 @@ export function whatsAppWebAdapterProgram() {
|
|
|
1253
948
|
}
|
|
1254
949
|
const keyModule = requireFirst(["WAWebMsgKey"]);
|
|
1255
950
|
const exactId = query.startsWith("id:") ? query.slice(3) : null;
|
|
951
|
+
let chatJid = null;
|
|
952
|
+
if (query.startsWith("chat:")) {
|
|
953
|
+
const chat = await resolveChat(requireFirst(["WAWebCollections"]), { chat_jid: query.slice(5) });
|
|
954
|
+
chatJid = (await canonicalJid(modelData(chat).id ?? chat?.id)).jid;
|
|
955
|
+
}
|
|
1256
956
|
const database = await new Promise((resolve, reject) => {
|
|
1257
957
|
const request = indexedDB.open("model-storage");
|
|
1258
958
|
// Never create an empty replacement database when WhatsApp changes its
|
|
@@ -1288,24 +988,46 @@ export function whatsAppWebAdapterProgram() {
|
|
|
1288
988
|
return;
|
|
1289
989
|
}
|
|
1290
990
|
rows.push({ position: cursor.key, id: cursor.value.id });
|
|
1291
|
-
if (rows.length > limit) resolve(rows);
|
|
991
|
+
if (rows.length > (chatJid === null ? limit : CHAT_SCAN_ROWS)) resolve(rows);
|
|
1292
992
|
else cursor.continue();
|
|
1293
993
|
};
|
|
1294
994
|
});
|
|
1295
995
|
} finally {
|
|
1296
996
|
database.close();
|
|
1297
997
|
}
|
|
1298
|
-
|
|
1299
|
-
|
|
998
|
+
let hasMore = sourceRows.length > limit;
|
|
999
|
+
let page = sourceRows.slice(0, limit);
|
|
1000
|
+
let last = page[page.length - 1];
|
|
1001
|
+
if (chatJid !== null) {
|
|
1002
|
+
// A chat read scans one large storage page, then keeps that chat's rows.
|
|
1003
|
+
// Matching happens after the transaction closes because LID conversion
|
|
1004
|
+
// awaits, and history can key a chat by phone or by LID.
|
|
1005
|
+
const examined = sourceRows.slice(0, CHAT_SCAN_ROWS);
|
|
1006
|
+
const canonical = new Map();
|
|
1007
|
+
const matches = [];
|
|
1008
|
+
for (const row of examined) {
|
|
1009
|
+
const remote = keyModule.fromString(row.id).remote;
|
|
1010
|
+
const raw = widString(remote);
|
|
1011
|
+
if (!canonical.has(raw)) canonical.set(raw, (await canonicalJid(remote)).jid);
|
|
1012
|
+
if (canonical.get(raw) !== chatJid) continue;
|
|
1013
|
+
matches.push(row);
|
|
1014
|
+
if (matches.length > limit) break;
|
|
1015
|
+
}
|
|
1016
|
+
page = matches.slice(0, limit);
|
|
1017
|
+
hasMore = matches.length > limit || sourceRows.length > CHAT_SCAN_ROWS;
|
|
1018
|
+
last = matches.length > limit ? page[page.length - 1] : examined[examined.length - 1];
|
|
1019
|
+
}
|
|
1300
1020
|
const collections = requireFirst(["WAWebCollections"]);
|
|
1301
|
-
const selected =
|
|
1021
|
+
const selected = [];
|
|
1022
|
+
for (const row of page) {
|
|
1302
1023
|
const key = keyModule.fromString(row.id);
|
|
1303
|
-
if (exactId !== null && key.id !== exactId)
|
|
1024
|
+
if (exactId !== null && key.id !== exactId) continue;
|
|
1304
1025
|
const remote = widString(key.remote);
|
|
1305
|
-
if (remote === "status@broadcast" || remote.endsWith("@newsletter"))
|
|
1026
|
+
if (remote === "status@broadcast" || remote.endsWith("@newsletter")) continue;
|
|
1306
1027
|
const isGroup = remote.endsWith("@g.us");
|
|
1307
|
-
|
|
1308
|
-
|
|
1028
|
+
if ((chatFilter === "group" && !isGroup) || (chatFilter === "individual" && isGroup)) continue;
|
|
1029
|
+
selected.push(row);
|
|
1030
|
+
}
|
|
1309
1031
|
const results = [];
|
|
1310
1032
|
if (selected.length) {
|
|
1311
1033
|
const loaded = await collections.Msg.getMessagesById(selected.map((row) => row.id));
|
|
@@ -1322,14 +1044,14 @@ export function whatsAppWebAdapterProgram() {
|
|
|
1322
1044
|
// Skip them like search does; throwing would pin every later read to
|
|
1323
1045
|
// the same cursor.
|
|
1324
1046
|
if (!message || message.timestamp <= 0) continue;
|
|
1325
|
-
if (exactId === null && query && !`${message.body}\n${message.caption}`.toLowerCase().includes(query.toLowerCase())) continue;
|
|
1047
|
+
if (exactId === null && chatJid === null && query && !`${message.body}\n${message.caption}`.toLowerCase().includes(query.toLowerCase())) continue;
|
|
1326
1048
|
results.push(message);
|
|
1327
1049
|
}
|
|
1328
1050
|
}
|
|
1329
1051
|
return {
|
|
1330
1052
|
results,
|
|
1331
1053
|
hasMore,
|
|
1332
|
-
...(hasMore ? { nextCursor: JSON.stringify({ v: 1, before:
|
|
1054
|
+
...(hasMore ? { nextCursor: JSON.stringify({ v: 1, before: last.position, query, chat_filter: chatFilter }) } : {}),
|
|
1333
1055
|
};
|
|
1334
1056
|
}
|
|
1335
1057
|
|
|
@@ -1581,7 +1303,7 @@ export function whatsAppWebAdapterProgram() {
|
|
|
1581
1303
|
const message = collectionByRawId(collections.Msg, raw);
|
|
1582
1304
|
if (!message)
|
|
1583
1305
|
throw new Error(
|
|
1584
|
-
`WhatsApp message ${raw} is not loaded;
|
|
1306
|
+
`WhatsApp message ${raw} is not loaded; read it with id:${raw} from the live messages feed or search it first`
|
|
1585
1307
|
);
|
|
1586
1308
|
return message;
|
|
1587
1309
|
}
|
|
@@ -1731,195 +1453,6 @@ export function whatsAppWebAdapterProgram() {
|
|
|
1731
1453
|
};
|
|
1732
1454
|
}
|
|
1733
1455
|
|
|
1734
|
-
async function blobFromMedia(model) {
|
|
1735
|
-
const row = modelData(model);
|
|
1736
|
-
let result = null;
|
|
1737
|
-
if (typeof model?.downloadMedia === "function") {
|
|
1738
|
-
result = await model.downloadMedia({
|
|
1739
|
-
downloadEvenIfExpensive: true,
|
|
1740
|
-
rmrReason: 1,
|
|
1741
|
-
});
|
|
1742
|
-
}
|
|
1743
|
-
result =
|
|
1744
|
-
result ??
|
|
1745
|
-
modelData(model).mediaData?.mediaBlob ??
|
|
1746
|
-
model?.mediaData?.mediaBlob ??
|
|
1747
|
-
row.mediaBlob ??
|
|
1748
|
-
null;
|
|
1749
|
-
if (!result) {
|
|
1750
|
-
// The module exports `{ enforceKaleidoscopeScore, downloadManager }` —
|
|
1751
|
-
// the method sits on the nested object, so probing the module root only
|
|
1752
|
-
// ever found `undefined` and this fallback never ran.
|
|
1753
|
-
const managerModule = requireFirst(["WAWebDownloadManager"]);
|
|
1754
|
-
const manager = managerModule?.downloadManager ?? managerModule;
|
|
1755
|
-
if (typeof manager?.downloadAndMaybeDecrypt === "function") {
|
|
1756
|
-
// `downloadQpl` is NOT optional: the very first thing the function
|
|
1757
|
-
// does is call `downloadQpl.addAnnotations(...)`, so omitting it threw
|
|
1758
|
-
// "Cannot read properties of undefined" before a byte was fetched.
|
|
1759
|
-
// It is only WhatsApp's performance logger, so a stub that answers
|
|
1760
|
-
// every method with a no-op satisfies it — and a permissive proxy
|
|
1761
|
-
// keeps working when a future build calls a method we have not seen
|
|
1762
|
-
// (this already happened once: `addAnnotations` alone was not enough,
|
|
1763
|
-
// the build also calls `addPoint`).
|
|
1764
|
-
result = await manager.downloadAndMaybeDecrypt({
|
|
1765
|
-
directPath: row.directPath,
|
|
1766
|
-
encFilehash: row.encFilehash,
|
|
1767
|
-
filehash: row.filehash,
|
|
1768
|
-
mediaKey: row.mediaKey,
|
|
1769
|
-
mediaKeyTimestamp: row.mediaKeyTimestamp,
|
|
1770
|
-
type: row.type,
|
|
1771
|
-
mimetype: row.mimetype,
|
|
1772
|
-
downloadOrigin: null,
|
|
1773
|
-
partialVideoOpts: null,
|
|
1774
|
-
downloadQpl: new Proxy(
|
|
1775
|
-
{},
|
|
1776
|
-
{
|
|
1777
|
-
get: () => () => {
|
|
1778
|
-
/* WhatsApp's QPL telemetry; nothing to record here. */
|
|
1779
|
-
},
|
|
1780
|
-
}
|
|
1781
|
-
),
|
|
1782
|
-
signal: new AbortController().signal,
|
|
1783
|
-
});
|
|
1784
|
-
}
|
|
1785
|
-
}
|
|
1786
|
-
if (result === "NEED_POKE" || result?.status === "NEED_POKE")
|
|
1787
|
-
return { status: "NEED_POKE" };
|
|
1788
|
-
const candidate = result?.blob ?? result?.mediaBlob ?? result;
|
|
1789
|
-
if (candidate instanceof Blob)
|
|
1790
|
-
return { status: "downloaded", blob: candidate };
|
|
1791
|
-
// `downloadAndMaybeDecrypt` resolves the decrypted bytes as a raw
|
|
1792
|
-
// ArrayBuffer, not a Blob. Falling through to the Blob checks below
|
|
1793
|
-
// reported a perfectly good download as `unavailable`.
|
|
1794
|
-
if (candidate instanceof ArrayBuffer || ArrayBuffer.isView(candidate))
|
|
1795
|
-
return {
|
|
1796
|
-
status: "downloaded",
|
|
1797
|
-
blob: new Blob([candidate], {
|
|
1798
|
-
type: row.mimetype || "application/octet-stream",
|
|
1799
|
-
}),
|
|
1800
|
-
};
|
|
1801
|
-
if (candidate?.getData) {
|
|
1802
|
-
const data = await candidate.getData();
|
|
1803
|
-
if (data instanceof Blob) return { status: "downloaded", blob: data };
|
|
1804
|
-
if (data instanceof ArrayBuffer || ArrayBuffer.isView(data))
|
|
1805
|
-
return {
|
|
1806
|
-
status: "downloaded",
|
|
1807
|
-
blob: new Blob([data], { type: row.mimetype }),
|
|
1808
|
-
};
|
|
1809
|
-
}
|
|
1810
|
-
return { status: row.directPath ? "unavailable" : "expired" };
|
|
1811
|
-
}
|
|
1812
|
-
|
|
1813
|
-
function bytesToBase64(bytes) {
|
|
1814
|
-
let binary = "";
|
|
1815
|
-
const chunk = 0x8000;
|
|
1816
|
-
for (let i = 0; i < bytes.length; i += chunk) {
|
|
1817
|
-
binary += String.fromCharCode(...bytes.subarray(i, i + chunk));
|
|
1818
|
-
}
|
|
1819
|
-
return btoa(binary);
|
|
1820
|
-
}
|
|
1821
|
-
|
|
1822
|
-
async function downloadMedia(input) {
|
|
1823
|
-
const collections = requireFirst(["WAWebCollections"]);
|
|
1824
|
-
const model = resolveMessage(collections, input?.message_id);
|
|
1825
|
-
const row = modelData(model);
|
|
1826
|
-
if (row.isViewOnce || row.viewOnce || row.type === "view_once")
|
|
1827
|
-
return { status: "view_once" };
|
|
1828
|
-
const limit = Math.max(1, Number(input?.max_bytes) || 1);
|
|
1829
|
-
const size = Number(row.size ?? row.fileSize ?? 0);
|
|
1830
|
-
if (size > limit) return { status: "too_large", size_bytes: size };
|
|
1831
|
-
const isVideo =
|
|
1832
|
-
row.type === "video" || String(row.mimetype ?? "").startsWith("video/");
|
|
1833
|
-
if (isVideo && size <= 0) {
|
|
1834
|
-
return { status: "metadata_only", retryable: true, size_bytes: null };
|
|
1835
|
-
}
|
|
1836
|
-
// Bound the fetch HERE, where the work happens, so the dispatch always
|
|
1837
|
-
// answers. A caller-side timer cannot cancel an in-flight dispatch: it only
|
|
1838
|
-
// stops the caller waiting, and the reply then arrives with nobody left to
|
|
1839
|
-
// receive it. Racing inside the page is safe — there is no IPC child to
|
|
1840
|
-
// outlive — and it turns "too slow" into an ordinary retryable answer.
|
|
1841
|
-
const budgetMs = Math.max(0, Number(input?.timeout_ms) || 0);
|
|
1842
|
-
let downloaded;
|
|
1843
|
-
// Held so a download that wins the race can cancel its own timer. A sync
|
|
1844
|
-
// collects several items, and a stray rejecting timer per item would keep
|
|
1845
|
-
// firing into the page long after its answer shipped.
|
|
1846
|
-
let budgetTimer = null;
|
|
1847
|
-
try {
|
|
1848
|
-
downloaded = budgetMs
|
|
1849
|
-
? await Promise.race([
|
|
1850
|
-
blobFromMedia(model),
|
|
1851
|
-
new Promise((_, reject) => {
|
|
1852
|
-
budgetTimer = setTimeout(
|
|
1853
|
-
() =>
|
|
1854
|
-
reject(
|
|
1855
|
-
Object.assign(
|
|
1856
|
-
new Error("media download exceeded its budget"),
|
|
1857
|
-
{ state: "timeout_retryable" }
|
|
1858
|
-
)
|
|
1859
|
-
),
|
|
1860
|
-
budgetMs
|
|
1861
|
-
);
|
|
1862
|
-
}),
|
|
1863
|
-
])
|
|
1864
|
-
: await blobFromMedia(model);
|
|
1865
|
-
} catch (error) {
|
|
1866
|
-
const detail = String(error);
|
|
1867
|
-
const reported = String(error?.state ?? error?.status ?? "");
|
|
1868
|
-
const explicit = [
|
|
1869
|
-
"NEED_POKE",
|
|
1870
|
-
"awaiting_primary_device",
|
|
1871
|
-
"view_once",
|
|
1872
|
-
"expired",
|
|
1873
|
-
"too_large",
|
|
1874
|
-
"timeout_retryable",
|
|
1875
|
-
"metadata_only",
|
|
1876
|
-
"unavailable",
|
|
1877
|
-
].find(
|
|
1878
|
-
(state) =>
|
|
1879
|
-
reported === state ||
|
|
1880
|
-
detail.toLowerCase().includes(state.toLowerCase())
|
|
1881
|
-
);
|
|
1882
|
-
return {
|
|
1883
|
-
status: explicit ?? "unavailable",
|
|
1884
|
-
retryable: [
|
|
1885
|
-
"NEED_POKE",
|
|
1886
|
-
"awaiting_primary_device",
|
|
1887
|
-
"timeout_retryable",
|
|
1888
|
-
"metadata_only",
|
|
1889
|
-
"unavailable",
|
|
1890
|
-
].includes(explicit ?? "unavailable"),
|
|
1891
|
-
detail: detail.slice(0, 200),
|
|
1892
|
-
};
|
|
1893
|
-
} finally {
|
|
1894
|
-
if (budgetTimer !== null) clearTimeout(budgetTimer);
|
|
1895
|
-
}
|
|
1896
|
-
if (!downloaded.blob) return downloaded;
|
|
1897
|
-
if (downloaded.blob.size > limit)
|
|
1898
|
-
return { status: "too_large", size_bytes: downloaded.blob.size };
|
|
1899
|
-
const bytes = new Uint8Array(await downloaded.blob.arrayBuffer());
|
|
1900
|
-
if (bytes.length === 0) return { status: "unavailable", detail: "empty" };
|
|
1901
|
-
const mime =
|
|
1902
|
-
downloaded.blob.type || row.mimetype || "application/octet-stream";
|
|
1903
|
-
const kind = mime.startsWith("audio/")
|
|
1904
|
-
? "audio"
|
|
1905
|
-
: mime.startsWith("image/")
|
|
1906
|
-
? "image"
|
|
1907
|
-
: "file";
|
|
1908
|
-
return {
|
|
1909
|
-
status: "downloaded",
|
|
1910
|
-
attachment: {
|
|
1911
|
-
kind,
|
|
1912
|
-
filename:
|
|
1913
|
-
row.filename ??
|
|
1914
|
-
row.fileName ??
|
|
1915
|
-
`whatsapp-${rawId(row.id ?? model?.id)}`,
|
|
1916
|
-
mime_type: mime,
|
|
1917
|
-
data: bytesToBase64(bytes),
|
|
1918
|
-
size_bytes: bytes.length,
|
|
1919
|
-
},
|
|
1920
|
-
};
|
|
1921
|
-
}
|
|
1922
|
-
|
|
1923
1456
|
async function invoke(request) {
|
|
1924
1457
|
try {
|
|
1925
1458
|
if (Number(request?.adapter_version) !== ADAPTER_VERSION) {
|
|
@@ -1954,11 +1487,8 @@ export function whatsAppWebAdapterProgram() {
|
|
|
1954
1487
|
},
|
|
1955
1488
|
};
|
|
1956
1489
|
}
|
|
1957
|
-
if (request.op === "collect") return await collect(request);
|
|
1958
1490
|
if (request.op === "read_messages")
|
|
1959
1491
|
return { ok: true, ...(await readMessages(request.input)) };
|
|
1960
|
-
if (request.op === "download_media")
|
|
1961
|
-
return { ok: true, ...(await downloadMedia(request)) };
|
|
1962
1492
|
if (request.op === "search_messages")
|
|
1963
1493
|
return { ok: true, ...(await searchMessages(request.input)) };
|
|
1964
1494
|
if (request.op === "draft_message")
|