@lobu/runtime-device 22.0.0 → 23.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.
Files changed (77) hide show
  1. package/dist/component.json +1 -1
  2. package/dist/dependencies.bun.lock +3 -3
  3. package/npm-shrinkwrap.json +15 -15
  4. package/package.json +1 -1
  5. package/vendor/cli/dist/bundled-skills/lobu/SKILL.md +1 -1
  6. package/vendor/cli/dist/connectors/README.md +14 -1
  7. package/vendor/cli/dist/connectors/google_gmail.ts +1 -0
  8. package/vendor/cli/dist/connectors/jira-helpers.ts +313 -0
  9. package/vendor/cli/dist/connectors/jira.ts +29 -221
  10. package/vendor/cli/dist/connectors/linkedin-browser-script.ts +13 -0
  11. package/vendor/cli/dist/connectors/linkedin-draft-page.js +104 -0
  12. package/vendor/cli/dist/connectors/linkedin-drafts.ts +73 -0
  13. package/vendor/cli/dist/connectors/linkedin-identity.ts +32 -0
  14. package/vendor/cli/dist/connectors/linkedin-inbox-adapter.js +1185 -0
  15. package/vendor/cli/dist/connectors/linkedin-inbox.ts +156 -0
  16. package/vendor/cli/dist/connectors/linkedin-like.ts +114 -0
  17. package/vendor/cli/dist/connectors/linkedin-takeout.ts +577 -0
  18. package/vendor/cli/dist/connectors/linkedin.ts +3998 -0
  19. package/vendor/cli/dist/connectors/takeout-utils.ts +240 -0
  20. package/vendor/cli/dist/connectors/tiktok-inbox-page.js +9 -9
  21. package/vendor/cli/dist/connectors/tiktok-web-page.js +21 -15
  22. package/vendor/cli/dist/connectors/tiktok_web.ts +63 -30
  23. package/vendor/cli/dist/connectors/whatsapp-web-adapter.js +64 -564
  24. package/vendor/cli/dist/connectors/whatsapp-web-helpers.ts +30 -444
  25. package/vendor/cli/dist/connectors/whatsapp-web-identity.ts +20 -6
  26. package/vendor/cli/dist/connectors/whatsapp_web.ts +45 -645
  27. package/vendor/cli/dist/connectors/x-dm-adapter.js +556 -0
  28. package/vendor/cli/dist/connectors/x-identity.ts +31 -2
  29. package/vendor/cli/dist/connectors/x.ts +266 -120
  30. package/vendor/cli/dist/runtime-components.json +4 -4
  31. package/vendor/cli/dist/templates/AGENTS.md.tmpl +2 -2
  32. package/vendor/cli/package.json +1 -1
  33. package/vendor/connector-sdk/dist/browser-handoff-banner.d.ts +39 -0
  34. package/vendor/connector-sdk/dist/browser-handoff-banner.js +175 -0
  35. package/vendor/connector-sdk/dist/browser-source-read.d.ts +52 -0
  36. package/vendor/connector-sdk/dist/browser-source-read.js +107 -0
  37. package/vendor/connector-sdk/dist/connector-types.d.ts +10 -0
  38. package/vendor/connector-sdk/dist/index.d.ts +3 -0
  39. package/vendor/connector-sdk/dist/index.js +2 -0
  40. package/vendor/connector-sdk/package.json +9 -1
  41. package/vendor/connector-worker/dist/agent-turn/guest-entry.js +15 -6
  42. package/vendor/connector-worker/dist/agent-turn/guest.bundle.js +288 -148
  43. package/vendor/connector-worker/dist/daemon/automation.d.ts +4 -11
  44. package/vendor/connector-worker/dist/daemon/automation.js +6 -6
  45. package/vendor/connector-worker/dist/daemon/client.d.ts +2 -4
  46. package/vendor/connector-worker/dist/daemon/client.js +0 -3
  47. package/vendor/connector-worker/dist/daemon/executor.js +0 -3
  48. package/vendor/connector-worker/dist/daemon/mac-device-daemon.d.ts +4 -3
  49. package/vendor/connector-worker/dist/daemon/mac-device-daemon.js +4 -10
  50. package/vendor/connector-worker/dist/daemon/native-bridge/executor.js +27 -25
  51. package/vendor/connector-worker/dist/daemon/start.js +3 -15
  52. package/vendor/connector-worker/dist/daemon/terminal-failure.js +0 -11
  53. package/vendor/connector-worker/dist/daemon/worker-id.d.ts +17 -0
  54. package/vendor/connector-worker/dist/daemon/worker-id.js +22 -0
  55. package/vendor/connector-worker/dist/mac-device-daemon.js +14 -26
  56. package/vendor/connector-worker/package.json +1 -1
  57. package/vendor/core/dist/capabilities.d.ts +1 -1
  58. package/vendor/core/dist/capabilities.js +0 -1
  59. package/vendor/core/dist/connector-query-errors.d.ts +32 -0
  60. package/vendor/core/dist/connector-query-errors.js +41 -1
  61. package/vendor/core/dist/contracts/tools/manage-conversations.js +2 -2
  62. package/vendor/core/dist/contracts/tools/manage-feeds.d.ts +14 -0
  63. package/vendor/core/dist/contracts/tools/manage-feeds.js +17 -0
  64. package/vendor/core/dist/contracts/worker/protocol.d.ts +8 -131
  65. package/vendor/core/dist/contracts/worker/protocol.js +9 -61
  66. package/vendor/core/dist/device-pin-tombstones.d.ts +44 -0
  67. package/vendor/core/dist/device-pin-tombstones.js +57 -0
  68. package/vendor/core/dist/index.d.ts +2 -1
  69. package/vendor/core/dist/index.js +1 -0
  70. package/vendor/core/dist/logger.js +10 -25
  71. package/vendor/core/dist/utils/diagnostic-scrubber.js +258 -21
  72. package/vendor/core/dist/worker/wire.d.ts +0 -10
  73. package/vendor/core/package.json +6 -6
  74. package/vendor/connector-worker/dist/daemon/device-chat.d.ts +0 -9
  75. package/vendor/connector-worker/dist/daemon/device-chat.js +0 -61
  76. package/vendor/core/dist/contracts/worker/device-chat.d.ts +0 -4
  77. package/vendor/core/dist/contracts/worker/device-chat.js +0 -26
@@ -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 now observes changes and emits records
7
- * through the generic browser transport. Do not "clean it up" casually: it
8
- * reaches into WhatsApp's own module registry via window.require and its shape
9
- * is load-bearing.
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,7 +33,7 @@ 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 = 22;
36
+ const ADAPTER_VERSION = 25;
37
37
  const SOURCE_ERROR_ID = "whatsapp-web:source-observation-error";
38
38
  const MAX_RECORD_BYTES = 128 * 1024;
39
39
  const MAX_BUFFER_BYTES = 16 * 1024 * 1024;
@@ -91,8 +91,7 @@ export function whatsAppWebAdapterProgram() {
91
91
  });
92
92
  };
93
93
  const resident = listeners.get(request.bridge_id);
94
- const previous = resident?.request.references_only === request.references_only
95
- && resident?.request.chat_filter === request.chat_filter
94
+ const previous = resident?.request.chat_filter === request.chat_filter
96
95
  && resident?.request.minimum_timestamp === request.minimum_timestamp ? resident : null;
97
96
  detach(request.bridge_id);
98
97
  // A new listener identity fences pending normalization from the detached
@@ -109,19 +108,17 @@ export function whatsAppWebAdapterProgram() {
109
108
  state.token = request.token;
110
109
  state.request = request;
111
110
  state.post = (entry) => post(state, entry);
112
- const observe = (model, kind) => {
111
+ const observe = (model) => {
113
112
  if (state.error) return;
114
113
  const sequence = ++state.sequence;
115
- const normalized = request.references_only ? Promise.resolve(messageReference(model)) : normalizeMessage(model, "reconcile");
116
- void normalized.then((record) => {
114
+ // A throwing model getter must reach the error record below, not escape
115
+ // the collection handler, so the reference is read inside the promise.
116
+ void new Promise((resolve) => resolve(messageReference(model))).then((record) => {
117
117
  if (listeners.get(state.id) !== state || !record || record.timestamp <= 0) return;
118
118
  if (state.request.chat_filter === "group" && !record.is_group) return;
119
119
  if (state.request.chat_filter === "individual" && record.is_group) return;
120
120
  if (state.request.minimum_timestamp != null && record.timestamp < state.request.minimum_timestamp) return;
121
- // Loading historical models also emits add. Changes to old messages
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 = {
121
+ record = {
125
122
  id: record.id,
126
123
  events: [{ id: record.id, event_type: "message.created", resource_ref: record.id,
127
124
  resource_type: "message", occurred_at: new Date(record.timestamp * 1000).toISOString() }],
@@ -154,8 +151,8 @@ export function whatsAppWebAdapterProgram() {
154
151
  reportError(state, "WhatsApp source observation failed; recovery is required", sequence);
155
152
  });
156
153
  };
157
- state.onAdd = (model) => observe(model, "add");
158
- state.onChange = (model) => observe(model, "change");
154
+ state.onAdd = (model) => observe(model);
155
+ state.onChange = (model) => observe(model);
159
156
  state.onReply = (event) => {
160
157
  if (event.source !== window || event.origin !== location.origin || event.data?.token !== state.token) return;
161
158
  if (event.data.type === "lobu-feed-record:stop") { detach(state.id); return; }
@@ -191,7 +188,7 @@ export function whatsAppWebAdapterProgram() {
191
188
  // not server delivery: the server advances the cursor only with its own commit.
192
189
  async function publishChanges(request) {
193
190
  const state = listeners.get(request.bridge_id);
194
- if (!state?.request.references_only || state.token !== request.token) throw new Error("Source listener is unavailable");
191
+ if (!state || state.token !== request.token) throw new Error("Source listener is unavailable");
195
192
  const record = request.batch;
196
193
  const bytes = new TextEncoder().encode(JSON.stringify(record)).length;
197
194
  const previous = state.pending.get(record.id);
@@ -266,6 +263,50 @@ export function whatsAppWebAdapterProgram() {
266
263
  return null;
267
264
  }
268
265
 
266
+ /**
267
+ * Every canonical form of the account's own identity. `meUser()` prefers
268
+ * the phone-number WID, but a group can address a member by its LID WID
269
+ * instead, so a mention or a quote of the account must match either form.
270
+ */
271
+ async function ownJids() {
272
+ const module = requireFirst(["WAWebUserPrefsMeUser"]);
273
+ const candidates = [meUser()];
274
+ for (const accessor of ["getMaybeMePnUser", "getMaybeMeLidUser"]) {
275
+ try {
276
+ candidates.push(module?.[accessor]?.());
277
+ } catch {
278
+ /* WhatsApp's private module graph throws freely; a miss is not fatal. */
279
+ }
280
+ }
281
+ const forms = new Set();
282
+ for (const value of candidates) {
283
+ const own = await canonicalJid(value);
284
+ if (own.jid) forms.add(own.jid);
285
+ if (own.lid) forms.add(own.lid);
286
+ }
287
+ return forms;
288
+ }
289
+
290
+ /**
291
+ * Whether a message addresses the account: it @mentions the account, or it
292
+ * replies to (quotes) one of the account's messages.
293
+ */
294
+ async function mentionsOwnAccount(row) {
295
+ const addressed = [
296
+ ...(Array.isArray(row.mentionedJidList) ? row.mentionedJidList : []),
297
+ row.quotedParticipant,
298
+ ].filter(Boolean);
299
+ if (addressed.length === 0) return false;
300
+ const own = await ownJids();
301
+ for (const value of addressed) {
302
+ const candidate = await canonicalJid(value);
303
+ if (own.has(candidate.jid) || (candidate.lid && own.has(candidate.lid))) {
304
+ return true;
305
+ }
306
+ }
307
+ return false;
308
+ }
309
+
269
310
  /**
270
311
  * Module names for each write op, most-current first.
271
312
  *
@@ -484,20 +525,8 @@ export function whatsAppWebAdapterProgram() {
484
525
  }
485
526
 
486
527
  /**
487
- * Name lookups for one collect.
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.
528
+ * Build name lookups once per read or search so normalizing each message
529
+ * does not rescan every chat and contact. Nothing here outlives the call.
501
530
  */
502
531
  function nameIndex(collections) {
503
532
  const byId = (collection) => {
@@ -545,8 +574,7 @@ export function whatsAppWebAdapterProgram() {
545
574
  const collections =
546
575
  collectionsOverride ?? requireFirst(["WAWebCollections"]);
547
576
  if (!collections) return null;
548
- // A single normalise (the watch path) builds its own; a collect passes one
549
- // in so the whole run shares it.
577
+ // Reads and searches share one name index across their result batch.
550
578
  const nameLookup = names ?? nameIndex(collections);
551
579
  let row = modelData(model);
552
580
  let key = row.id ?? model?.id;
@@ -623,6 +651,7 @@ export function whatsAppWebAdapterProgram() {
623
651
  : null,
624
652
  media_filename: row.filename ?? row.fileName ?? row.title ?? null,
625
653
  quoted_id: quotedId,
654
+ mentions_me: await mentionsOwnAccount(row),
626
655
  is_forwarded: Boolean(row.isForwarded || Number(row.forwardingScore) > 0),
627
656
  is_starred: Boolean(row.star ?? row.isStarred),
628
657
  is_system_event: SYSTEM_TYPES.has(type),
@@ -671,7 +700,7 @@ export function whatsAppWebAdapterProgram() {
671
700
  // Socket.stream was DISCONNECTED, then both were CONNECTED after reload.
672
701
  // Current WhatsApp exposes account and transport state separately. An
673
702
  // authenticated account can retain hydrated stores while its stream is
674
- // disconnected; those cached messages are not a successful live sync.
703
+ // disconnected; those cached messages are not a successful live read.
675
704
  const socket = requireFirst(["WAWebSocketModel"])?.Socket;
676
705
  const connection =
677
706
  socket ??
@@ -776,7 +805,6 @@ export function whatsAppWebAdapterProgram() {
776
805
  }
777
806
 
778
807
  function operationCapabilities() {
779
- const history = requireFirst(["WAWebChatLoadMessages"]);
780
808
  const search = requireFirst([
781
809
  "WAWebChatMessageSearch",
782
810
  "WAWebMessageSearch",
@@ -787,23 +815,13 @@ export function whatsAppWebAdapterProgram() {
787
815
  const edit = requireFirst(EDIT_MODULES);
788
816
  const reaction = requireFirst(REACTION_MODULES);
789
817
  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
818
  const hasFunction = (module, names) =>
795
819
  names.some((name) => typeof module?.[name] === "function");
796
820
  return {
797
- collect: hasFunction(history, ["loadEarlierMsgs"]),
798
821
  read_messages:
799
822
  typeof indexedDB !== "undefined" &&
800
823
  typeof requireFirst(["WAWebMsgKey"])?.fromString === "function" &&
801
824
  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
825
  search_messages:
808
826
  hasFunction(search, ["searchMessages", "searchMsgs", "search"]) ||
809
827
  Boolean(requireFirst(["WAWebCollections"])?.Msg),
@@ -829,130 +847,6 @@ export function whatsAppWebAdapterProgram() {
829
847
  };
830
848
  }
831
849
 
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
850
  function uniqueMessageModels(collections) {
957
851
  const byId = new Map();
958
852
  for (const message of models(collections.Msg)) {
@@ -968,208 +862,6 @@ export function whatsAppWebAdapterProgram() {
968
862
  return [...byId.values()];
969
863
  }
970
864
 
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
865
  async function resolveChat(collections, input) {
1174
866
  if (input?.self_chat === true) {
1175
867
  const me = meUser();
@@ -1581,7 +1273,7 @@ export function whatsAppWebAdapterProgram() {
1581
1273
  const message = collectionByRawId(collections.Msg, raw);
1582
1274
  if (!message)
1583
1275
  throw new Error(
1584
- `WhatsApp message ${raw} is not loaded; sync or search it first`
1276
+ `WhatsApp message ${raw} is not loaded; read it with id:${raw} from the live messages feed or search it first`
1585
1277
  );
1586
1278
  return message;
1587
1279
  }
@@ -1731,195 +1423,6 @@ export function whatsAppWebAdapterProgram() {
1731
1423
  };
1732
1424
  }
1733
1425
 
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
1426
  async function invoke(request) {
1924
1427
  try {
1925
1428
  if (Number(request?.adapter_version) !== ADAPTER_VERSION) {
@@ -1954,11 +1457,8 @@ export function whatsAppWebAdapterProgram() {
1954
1457
  },
1955
1458
  };
1956
1459
  }
1957
- if (request.op === "collect") return await collect(request);
1958
1460
  if (request.op === "read_messages")
1959
1461
  return { ok: true, ...(await readMessages(request.input)) };
1960
- if (request.op === "download_media")
1961
- return { ok: true, ...(await downloadMedia(request)) };
1962
1462
  if (request.op === "search_messages")
1963
1463
  return { ok: true, ...(await searchMessages(request.input)) };
1964
1464
  if (request.op === "draft_message")