@lobu/runtime-device 21.2.1 → 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.
- package/dist/component.json +1 -1
- package/dist/dependencies.bun.lock +91 -83
- package/npm-shrinkwrap.json +492 -438
- package/package.json +12 -2
- package/vendor/cli/dist/bundled-skills/lobu/SKILL.md +1 -1
- package/vendor/cli/dist/commands/_lib/apply/apply-cmd.js +5 -6
- package/vendor/cli/dist/commands/_lib/apply/client.d.ts +1 -0
- package/vendor/cli/dist/commands/_lib/apply/client.js +26 -7
- package/vendor/cli/dist/commands/_lib/apply/desired-state.d.ts +9 -1
- package/vendor/cli/dist/commands/_lib/apply/diff.d.ts +1 -0
- package/vendor/cli/dist/commands/_lib/apply/diff.js +46 -4
- package/vendor/cli/dist/commands/_lib/apply/map-config.js +18 -38
- package/vendor/cli/dist/commands/_lib/init-from-org/bootstrap.js +5 -17
- package/vendor/cli/dist/commands/init.d.ts +0 -2
- package/vendor/cli/dist/commands/init.js +0 -27
- package/vendor/cli/dist/commands/login.d.ts +17 -0
- package/vendor/cli/dist/commands/login.js +43 -17
- package/vendor/cli/dist/commands/memory/_lib/errors.d.ts +0 -8
- package/vendor/cli/dist/commands/memory/_lib/errors.js +0 -12
- package/vendor/cli/dist/commands/memory/_lib/mcp.js +6 -6
- package/vendor/cli/dist/commands/memory/_lib/seed-cmd.js +4 -10
- package/vendor/cli/dist/config/define.d.ts +21 -15
- package/vendor/cli/dist/config/define.js +2 -2
- package/vendor/cli/dist/connectors/README.md +21 -6
- package/vendor/cli/dist/connectors/_google/actions.ts +270 -0
- package/vendor/cli/dist/connectors/_google/discovery/calendar_v3.json +1 -0
- package/vendor/cli/dist/connectors/_google/discovery/chat_v1.json +1 -0
- package/vendor/cli/dist/connectors/_google/discovery/docs_v1.json +1 -0
- package/vendor/cli/dist/connectors/_google/discovery/drive_v3.json +1 -0
- package/vendor/cli/dist/connectors/_google/discovery/gmail_v1.json +1 -0
- package/vendor/cli/dist/connectors/_google/discovery/people_v1.json +1 -0
- package/vendor/cli/dist/connectors/_google/discovery/sheets_v4.json +1 -0
- package/vendor/cli/dist/connectors/_google/discovery/tasks_v1.json +1 -0
- package/vendor/cli/dist/connectors/_google/discovery/youtube_v3.json +1 -0
- package/vendor/cli/dist/connectors/_google/discovery.ts +442 -0
- package/vendor/cli/dist/connectors/_google/errors.ts +91 -0
- package/vendor/cli/dist/connectors/_google/list-feed.ts +223 -0
- package/vendor/cli/dist/connectors/_google/policies.ts +127 -0
- package/vendor/cli/dist/connectors/agentid.ts +43 -0
- package/vendor/cli/dist/connectors/chat-user-identity.ts +2 -1
- package/vendor/cli/dist/connectors/github-automation-events.ts +9 -0
- package/vendor/cli/dist/connectors/github-identity.ts +28 -3
- package/vendor/cli/dist/connectors/github.ts +101 -59
- package/vendor/cli/dist/connectors/google_calendar.ts +207 -719
- package/vendor/cli/dist/connectors/google_drive.ts +16 -2
- package/vendor/cli/dist/connectors/google_gmail.ts +52 -33
- package/vendor/cli/dist/connectors/herdr.ts +406 -0
- package/vendor/cli/dist/connectors/jira-helpers.ts +313 -0
- package/vendor/cli/dist/connectors/jira.ts +35 -227
- package/vendor/cli/dist/connectors/linear.ts +7 -4
- 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 +1185 -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 +3998 -0
- package/vendor/cli/dist/connectors/postgres.ts +27 -2
- package/vendor/cli/dist/connectors/takeout-utils.ts +240 -0
- package/vendor/cli/dist/connectors/tiktok-inbox-page.js +230 -0
- package/vendor/cli/dist/connectors/tiktok-web-page.js +217 -0
- package/vendor/cli/dist/connectors/tiktok_web.ts +319 -0
- package/vendor/cli/dist/connectors/whatsapp-web-adapter.js +285 -567
- package/vendor/cli/dist/connectors/whatsapp-web-helpers.ts +74 -455
- package/vendor/cli/dist/connectors/whatsapp-web-identity.ts +135 -0
- package/vendor/cli/dist/connectors/whatsapp_web.ts +159 -618
- package/vendor/cli/dist/connectors/x-dm-adapter.js +556 -0
- package/vendor/cli/dist/connectors/x-identity.ts +31 -2
- package/vendor/cli/dist/connectors/x.ts +539 -253
- package/vendor/cli/dist/index.js +1 -37
- package/vendor/cli/dist/internal/context.d.ts +10 -0
- package/vendor/cli/dist/internal/context.js +26 -16
- package/vendor/cli/dist/internal/node-version.d.ts +1 -1
- package/vendor/cli/dist/internal/node-version.js +1 -1
- package/vendor/cli/dist/internal/oauth.d.ts +3 -1
- package/vendor/cli/dist/internal/oauth.js +4 -3
- package/vendor/cli/dist/internal/runtime-components.d.ts +9 -0
- package/vendor/cli/dist/internal/runtime-components.js +67 -2
- package/vendor/cli/dist/mac-auth.js +17 -1
- package/vendor/cli/dist/providers.json +1 -1
- package/vendor/cli/dist/runtime-components.json +4 -4
- package/vendor/cli/dist/templates/AGENTS.md.tmpl +12 -5
- package/vendor/cli/dist/templates/README.md.tmpl +1 -1
- package/vendor/cli/dist/vendor/ora/.bun-tag-fde20e9a6df212fa +0 -0
- package/vendor/cli/dist/vendor/ora/index.d.ts +332 -0
- package/vendor/cli/dist/vendor/ora/index.js +424 -0
- package/vendor/cli/dist/vendor/ora/license +9 -0
- package/vendor/cli/dist/vendor/ora/package.json +64 -0
- package/vendor/cli/dist/vendor/ora/readme.md +329 -0
- package/vendor/cli/package.json +2 -2
- 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-requirement.d.ts +36 -0
- package/vendor/connector-sdk/dist/browser-requirement.js +60 -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-runtime.d.ts +3 -1
- package/vendor/connector-sdk/dist/connector-runtime.js +10 -0
- package/vendor/connector-sdk/dist/connector-types.d.ts +169 -45
- package/vendor/connector-sdk/dist/define-connector.d.ts +4 -2
- package/vendor/connector-sdk/dist/define-connector.js +3 -0
- package/vendor/connector-sdk/dist/index.d.ts +8 -2
- package/vendor/connector-sdk/dist/index.js +3 -0
- package/vendor/connector-sdk/dist/reaction-client-types.d.ts +16 -21
- package/vendor/connector-sdk/dist/reaction-client-types.typecheck.d.ts +5 -0
- package/vendor/connector-sdk/package.json +13 -1
- package/vendor/connector-worker/dist/agent-turn/guest-entry.js +43 -30
- package/vendor/connector-worker/dist/agent-turn/guest.bundle.js +416 -330
- package/vendor/connector-worker/dist/agent-turn/memory.d.ts +3 -2
- package/vendor/connector-worker/dist/agent-turn/memory.js +3 -20
- package/vendor/connector-worker/dist/agent-turn/types.d.ts +3 -15
- package/vendor/connector-worker/dist/agent-turn/workspace.d.ts +2 -2
- package/vendor/connector-worker/dist/agent-turn/workspace.js +2 -4
- package/vendor/connector-worker/dist/daemon/action-monitor.d.ts +12 -0
- package/vendor/connector-worker/dist/daemon/action-monitor.js +25 -0
- package/vendor/connector-worker/dist/daemon/agent-turn.js +0 -9
- package/vendor/connector-worker/dist/daemon/automation.d.ts +4 -11
- package/vendor/connector-worker/dist/daemon/automation.js +6 -6
- package/vendor/connector-worker/dist/daemon/builtins/index.d.ts +12 -0
- package/vendor/connector-worker/dist/daemon/builtins/index.js +61 -1
- package/vendor/connector-worker/dist/daemon/builtins/os-shell.d.ts +2 -1
- package/vendor/connector-worker/dist/daemon/builtins/os-shell.js +40 -27
- package/vendor/connector-worker/dist/daemon/client.d.ts +6 -4
- package/vendor/connector-worker/dist/daemon/client.js +3 -3
- package/vendor/connector-worker/dist/daemon/device-concurrency.d.ts +8 -0
- package/vendor/connector-worker/dist/daemon/device-concurrency.js +13 -0
- package/vendor/connector-worker/dist/daemon/executor.d.ts +1 -1
- package/vendor/connector-worker/dist/daemon/executor.js +48 -120
- package/vendor/connector-worker/dist/daemon/log.d.ts +2 -0
- package/vendor/connector-worker/dist/daemon/log.js +23 -2
- package/vendor/connector-worker/dist/daemon/mac-device-daemon.d.ts +23 -6
- package/vendor/connector-worker/dist/daemon/mac-device-daemon.js +57 -23
- package/vendor/connector-worker/dist/daemon/native-bridge/client.d.ts +6 -0
- package/vendor/connector-worker/dist/daemon/native-bridge/client.js +15 -0
- package/vendor/connector-worker/dist/daemon/native-bridge/executor.js +29 -25
- package/vendor/connector-worker/dist/daemon/native-bridge/protocol.d.ts +1 -1
- package/vendor/connector-worker/dist/daemon/native-bridge/protocol.js +1 -0
- package/vendor/connector-worker/dist/daemon/poll-loop.d.ts +3 -0
- package/vendor/connector-worker/dist/daemon/poll-loop.js +5 -1
- package/vendor/connector-worker/dist/daemon/start.d.ts +1 -0
- package/vendor/connector-worker/dist/daemon/start.js +15 -20
- 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/executor/interface.d.ts +34 -2
- package/vendor/connector-worker/dist/executor/isolate.js +66 -15
- package/vendor/connector-worker/dist/executor/redact.d.ts +1 -0
- package/vendor/connector-worker/dist/executor/redact.js +97 -26
- package/vendor/connector-worker/dist/executor/runtime.d.ts +7 -0
- package/vendor/connector-worker/dist/executor/runtime.js +26 -0
- package/vendor/connector-worker/dist/isolate/bridge.js +3 -1
- package/vendor/connector-worker/dist/mac-device-daemon.js +24 -29
- package/vendor/connector-worker/package.json +2 -2
- package/vendor/core/dist/agent-policy.js +1 -1
- package/vendor/core/dist/agent-store.d.ts +2 -0
- package/vendor/core/dist/agent-tooling.d.ts +1 -1
- package/vendor/core/dist/agent-tooling.js +1 -1
- package/vendor/core/dist/capabilities.d.ts +1 -1
- package/vendor/core/dist/capabilities.js +2 -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 +0 -11
- package/vendor/core/dist/contracts/agent-settings.js +1 -7
- package/vendor/core/dist/contracts/tools/collection-presentation.d.ts +93 -0
- package/vendor/core/dist/contracts/tools/collection-presentation.js +88 -0
- package/vendor/core/dist/contracts/tools/collection-selection.d.ts +47 -0
- package/vendor/core/dist/contracts/tools/collection-selection.js +74 -0
- package/vendor/core/dist/contracts/tools/manage-agents.d.ts +10 -4
- package/vendor/core/dist/contracts/tools/manage-auth-profiles.d.ts +1 -1
- package/vendor/core/dist/contracts/tools/manage-auth-profiles.js +5 -1
- package/vendor/core/dist/contracts/tools/manage-automations.d.ts +19 -4
- package/vendor/core/dist/contracts/tools/manage-automations.js +25 -8
- package/vendor/core/dist/contracts/tools/manage-connections.d.ts +1 -0
- package/vendor/core/dist/contracts/tools/manage-connections.js +3 -0
- package/vendor/core/dist/contracts/tools/manage-conversations.js +2 -2
- package/vendor/core/dist/contracts/tools/manage-entity-schema.d.ts +11 -0
- package/vendor/core/dist/contracts/tools/manage-entity-schema.js +42 -3
- package/vendor/core/dist/contracts/tools/manage-entity.d.ts +181 -152
- package/vendor/core/dist/contracts/tools/manage-entity.js +118 -162
- 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-operations.d.ts +41 -3
- package/vendor/core/dist/contracts/tools/manage-operations.js +40 -2
- package/vendor/core/dist/contracts/tools/manage-views.d.ts +182 -7
- package/vendor/core/dist/contracts/tools/manage-views.js +49 -13
- package/vendor/core/dist/contracts/tools/read-knowledge.d.ts +11 -2
- package/vendor/core/dist/contracts/tools/read-knowledge.js +23 -3
- package/vendor/core/dist/contracts/tools/save-memory.d.ts +3 -6
- package/vendor/core/dist/contracts/tools/save-memory.js +2 -6
- package/vendor/core/dist/contracts/tools/view-attach.d.ts +2 -1
- package/vendor/core/dist/contracts/tools/view-attach.js +7 -2
- package/vendor/core/dist/contracts/worker/protocol.d.ts +130 -159
- package/vendor/core/dist/contracts/worker/protocol.js +88 -76
- 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 +5 -5
- package/vendor/core/dist/index.js +4 -6
- package/vendor/core/dist/json-template.d.ts +6 -10
- package/vendor/core/dist/json-template.js +5 -9
- package/vendor/core/dist/logger.js +17 -26
- package/vendor/core/dist/shell-safety.d.ts +9 -0
- package/vendor/core/dist/shell-safety.js +154 -0
- package/vendor/core/dist/suggestions.d.ts +1 -1
- package/vendor/core/dist/suggestions.js +1 -1
- package/vendor/core/dist/tool-trace-summary.d.ts +1 -1
- package/vendor/core/dist/tool-trace-summary.js +1 -1
- package/vendor/core/dist/types.d.ts +1 -3
- package/vendor/core/dist/utils/diagnostic-scrubber.d.ts +7 -0
- package/vendor/core/dist/utils/diagnostic-scrubber.js +437 -0
- package/vendor/core/dist/worker/auth.d.ts +44 -6
- package/vendor/core/dist/worker/auth.js +44 -11
- package/vendor/core/dist/worker/wire.d.ts +0 -10
- package/vendor/core/package.json +20 -11
- package/vendor/ora/.bun-tag-fde20e9a6df212fa +0 -0
- package/vendor/ora/index.d.ts +332 -0
- package/vendor/ora/index.js +424 -0
- package/vendor/ora/license +9 -0
- package/vendor/ora/package.json +53 -0
- package/vendor/ora/readme.md +329 -0
- package/vendor/cli/dist/commands/telemetry.d.ts +0 -10
- package/vendor/cli/dist/commands/telemetry.js +0 -66
- 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/core/dist/sentry.d.ts +0 -26
- package/vendor/core/dist/sentry.js +0 -133
- package/vendor/core/dist/tool-policy.d.ts +0 -74
- package/vendor/core/dist/tool-policy.js +0 -305
- package/vendor/core/dist/utils/sentry-scrubber.d.ts +0 -29
- package/vendor/core/dist/utils/sentry-scrubber.js +0 -228
|
@@ -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 = 25;
|
|
37
37
|
const SOURCE_ERROR_ID = "whatsapp-web:source-observation-error";
|
|
38
|
+
const MAX_RECORD_BYTES = 128 * 1024;
|
|
39
|
+
const MAX_BUFFER_BYTES = 16 * 1024 * 1024;
|
|
40
|
+
const MAX_BUFFER_ENTRIES = 10_000;
|
|
38
41
|
const SYSTEM_TYPES = new Set([
|
|
39
42
|
"gp2",
|
|
40
43
|
"notification_template",
|
|
@@ -87,11 +90,13 @@ export function whatsAppWebAdapterProgram() {
|
|
|
87
90
|
},
|
|
88
91
|
});
|
|
89
92
|
};
|
|
90
|
-
const
|
|
93
|
+
const resident = listeners.get(request.bridge_id);
|
|
94
|
+
const previous = resident?.request.chat_filter === request.chat_filter
|
|
95
|
+
&& resident?.request.minimum_timestamp === request.minimum_timestamp ? resident : null;
|
|
91
96
|
detach(request.bridge_id);
|
|
92
97
|
// A new listener identity fences pending normalization from the detached
|
|
93
|
-
// handlers. Keep unaccepted records
|
|
94
|
-
// by attaching fresh handlers to the
|
|
98
|
+
// handlers. Keep unaccepted records only within the same source scope, and
|
|
99
|
+
// retry transient failures by attaching fresh handlers to the collection.
|
|
95
100
|
const state = {
|
|
96
101
|
...(previous ?? {
|
|
97
102
|
id: request.bridge_id, collection, sequence: 0,
|
|
@@ -102,25 +107,30 @@ export function whatsAppWebAdapterProgram() {
|
|
|
102
107
|
};
|
|
103
108
|
state.token = request.token;
|
|
104
109
|
state.request = request;
|
|
105
|
-
|
|
110
|
+
state.post = (entry) => post(state, entry);
|
|
111
|
+
const observe = (model) => {
|
|
106
112
|
if (state.error) return;
|
|
107
113
|
const sequence = ++state.sequence;
|
|
108
|
-
|
|
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) => {
|
|
109
117
|
if (listeners.get(state.id) !== state || !record || record.timestamp <= 0) return;
|
|
110
118
|
if (state.request.chat_filter === "group" && !record.is_group) return;
|
|
111
119
|
if (state.request.chat_filter === "individual" && record.is_group) return;
|
|
112
120
|
if (state.request.minimum_timestamp != null && record.timestamp < state.request.minimum_timestamp) return;
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
121
|
+
record = {
|
|
122
|
+
id: record.id,
|
|
123
|
+
events: [{ id: record.id, event_type: "message.created", resource_ref: record.id,
|
|
124
|
+
resource_type: "message", occurred_at: new Date(record.timestamp * 1000).toISOString() }],
|
|
125
|
+
};
|
|
116
126
|
const previous = state.pending.get(record.id);
|
|
117
127
|
const seen = state.fingerprints.get(record.id);
|
|
118
128
|
if (seen && seen.sequence >= sequence) return;
|
|
119
129
|
const fingerprint = JSON.stringify(record);
|
|
120
130
|
if (seen?.fingerprint === fingerprint) { seen.sequence = sequence; return; }
|
|
121
131
|
const bytes = new TextEncoder().encode(fingerprint).length;
|
|
122
|
-
if (bytes >
|
|
123
|
-
(state.pending.size >=
|
|
132
|
+
if (bytes > MAX_RECORD_BYTES || state.pendingBytes - (previous?.bytes ?? 0) + bytes > MAX_BUFFER_BYTES ||
|
|
133
|
+
(state.pending.size >= MAX_BUFFER_ENTRIES && !previous)) {
|
|
124
134
|
reportError(state, "WhatsApp page observation buffer overflowed; recovery is required", sequence);
|
|
125
135
|
return;
|
|
126
136
|
}
|
|
@@ -130,7 +140,7 @@ export function whatsAppWebAdapterProgram() {
|
|
|
130
140
|
state.fingerprintBytes += bytes - (seen?.bytes ?? 0);
|
|
131
141
|
state.fingerprints.delete(record.id);
|
|
132
142
|
state.fingerprints.set(record.id, { fingerprint, sequence, bytes });
|
|
133
|
-
while (state.fingerprints.size >
|
|
143
|
+
while (state.fingerprints.size > MAX_BUFFER_ENTRIES || state.fingerprintBytes > MAX_BUFFER_BYTES) {
|
|
134
144
|
const oldest = state.fingerprints.keys().next().value;
|
|
135
145
|
state.fingerprintBytes -= state.fingerprints.get(oldest).bytes;
|
|
136
146
|
state.fingerprints.delete(oldest);
|
|
@@ -141,14 +151,18 @@ export function whatsAppWebAdapterProgram() {
|
|
|
141
151
|
reportError(state, "WhatsApp source observation failed; recovery is required", sequence);
|
|
142
152
|
});
|
|
143
153
|
};
|
|
144
|
-
state.onAdd = (model) => observe(model
|
|
145
|
-
state.onChange = (model) => observe(model
|
|
154
|
+
state.onAdd = (model) => observe(model);
|
|
155
|
+
state.onChange = (model) => observe(model);
|
|
146
156
|
state.onReply = (event) => {
|
|
147
157
|
if (event.source !== window || event.origin !== location.origin || event.data?.token !== state.token) return;
|
|
148
158
|
if (event.data.type === "lobu-feed-record:stop") { detach(state.id); return; }
|
|
149
|
-
if (event.data.type !== "lobu-feed-record:ack"
|
|
159
|
+
if (event.data.type !== "lobu-feed-record:ack") return;
|
|
150
160
|
for (const [id, entry] of state.pending) {
|
|
151
|
-
if (entry.sequence
|
|
161
|
+
if (entry.sequence !== event.data.sequence) continue;
|
|
162
|
+
if (event.data.ok) {
|
|
163
|
+
state.pendingBytes -= entry.bytes; state.pending.delete(id); entry.resolve?.();
|
|
164
|
+
} else entry.reject?.(new Error("Browser rejected the source reference batch"));
|
|
165
|
+
break;
|
|
152
166
|
}
|
|
153
167
|
};
|
|
154
168
|
listeners.set(state.id, state);
|
|
@@ -170,6 +184,28 @@ export function whatsAppWebAdapterProgram() {
|
|
|
170
184
|
return { ok: true, listening: true };
|
|
171
185
|
}
|
|
172
186
|
|
|
187
|
+
// Replay shares the live listener's durable browser buffer. Await its receipt,
|
|
188
|
+
// not server delivery: the server advances the cursor only with its own commit.
|
|
189
|
+
async function publishChanges(request) {
|
|
190
|
+
const state = listeners.get(request.bridge_id);
|
|
191
|
+
if (!state || state.token !== request.token) throw new Error("Source listener is unavailable");
|
|
192
|
+
const record = request.batch;
|
|
193
|
+
const bytes = new TextEncoder().encode(JSON.stringify(record)).length;
|
|
194
|
+
const previous = state.pending.get(record.id);
|
|
195
|
+
if (bytes > MAX_RECORD_BYTES || state.pendingBytes - (previous?.bytes ?? 0) + bytes > MAX_BUFFER_BYTES ||
|
|
196
|
+
(state.pending.size >= MAX_BUFFER_ENTRIES && !previous)) throw new Error("Source replay buffer is full");
|
|
197
|
+
await new Promise((resolve, reject) => {
|
|
198
|
+
const timer = setTimeout(() => reject(new Error("Source replay was not accepted by the browser")), 10_000);
|
|
199
|
+
const entry = { record, bytes, sequence: ++state.sequence,
|
|
200
|
+
resolve: () => { clearTimeout(timer); resolve(); },
|
|
201
|
+
reject: (error) => { clearTimeout(timer); reject(error); } };
|
|
202
|
+
state.pendingBytes += bytes - (previous?.bytes ?? 0);
|
|
203
|
+
state.pending.set(record.id, entry);
|
|
204
|
+
state.post(entry);
|
|
205
|
+
});
|
|
206
|
+
return { ok: true };
|
|
207
|
+
}
|
|
208
|
+
|
|
173
209
|
function requireFirst(names) {
|
|
174
210
|
for (const name of names) {
|
|
175
211
|
try {
|
|
@@ -227,6 +263,50 @@ export function whatsAppWebAdapterProgram() {
|
|
|
227
263
|
return null;
|
|
228
264
|
}
|
|
229
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
|
+
|
|
230
310
|
/**
|
|
231
311
|
* Module names for each write op, most-current first.
|
|
232
312
|
*
|
|
@@ -268,6 +348,70 @@ export function whatsAppWebAdapterProgram() {
|
|
|
268
348
|
return model?.attributes ?? model?._data ?? model ?? {};
|
|
269
349
|
}
|
|
270
350
|
|
|
351
|
+
function messageReference(model) {
|
|
352
|
+
const row = modelData(model);
|
|
353
|
+
const key = row.id ?? model?.id;
|
|
354
|
+
const id = rawId(key);
|
|
355
|
+
const remote = widString(key?.remote ?? row.chatId ?? (key?.fromMe ? row.to : row.from));
|
|
356
|
+
if (!id || !remote || remote === "status@broadcast" || remote.endsWith("@newsletter") || SYSTEM_TYPES.has(row.type)) return null;
|
|
357
|
+
return { id, timestamp: unixSeconds(row), is_group: remote.endsWith("@g.us") };
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
/** Resume insertion order after reconnect without exporting any message body. */
|
|
361
|
+
async function observeMessages(input = {}) {
|
|
362
|
+
const after = input.after ?? null;
|
|
363
|
+
if (after !== null && (!Number.isSafeInteger(after) || after < 0)) throw new Error("Invalid WhatsApp observation cursor");
|
|
364
|
+
const database = await new Promise((resolve, reject) => {
|
|
365
|
+
const request = indexedDB.open("model-storage");
|
|
366
|
+
request.onupgradeneeded = () => { request.transaction.abort(); reject(new Error("WhatsApp source history database is unavailable")); };
|
|
367
|
+
request.onerror = () => reject(request.error);
|
|
368
|
+
request.onblocked = () => reject(new Error("WhatsApp source history database is blocked"));
|
|
369
|
+
request.onsuccess = () => resolve(request.result);
|
|
370
|
+
});
|
|
371
|
+
let rows;
|
|
372
|
+
try {
|
|
373
|
+
rows = await new Promise((resolve, reject) => {
|
|
374
|
+
const transaction = database.transaction("message", "readonly");
|
|
375
|
+
transaction.onabort = () => reject(transaction.error ?? new Error("WhatsApp source observation aborted"));
|
|
376
|
+
const index = transaction.objectStore("message").index("rowId");
|
|
377
|
+
const request = index.openCursor(after === null ? null : IDBKeyRange.lowerBound(after, true), after === null ? "prev" : "next");
|
|
378
|
+
const page = [];
|
|
379
|
+
request.onerror = () => reject(request.error);
|
|
380
|
+
request.onsuccess = () => {
|
|
381
|
+
const cursor = request.result;
|
|
382
|
+
if (!cursor) { resolve(page); return; }
|
|
383
|
+
if (!Number.isSafeInteger(cursor.key) || cursor.key <= 0 || typeof cursor.value.id !== "string") {
|
|
384
|
+
reject(new Error("WhatsApp source history has an unsupported key layout")); return;
|
|
385
|
+
}
|
|
386
|
+
page.push({ position: cursor.key, id: cursor.value.id, timestamp: unixSeconds(cursor.value), type: cursor.value.type });
|
|
387
|
+
if (after === null || page.length > 500) resolve(page);
|
|
388
|
+
else cursor.continue();
|
|
389
|
+
};
|
|
390
|
+
});
|
|
391
|
+
} finally { database.close(); }
|
|
392
|
+
if (after === null) return { after: rows[0]?.position ?? 0, references: [], hasMore: false };
|
|
393
|
+
const page = rows.slice(0, 500);
|
|
394
|
+
const references = [];
|
|
395
|
+
if (page.length) {
|
|
396
|
+
const keys = requireFirst(["WAWebMsgKey"]);
|
|
397
|
+
if (typeof keys?.fromString !== "function") throw new Error("WhatsApp source message keys are unavailable");
|
|
398
|
+
for (const row of page) {
|
|
399
|
+
// IndexedDB already contains routing and time metadata. Loading message
|
|
400
|
+
// models would hydrate private content and fails for status entries.
|
|
401
|
+
const reference = messageReference({ id: keys.fromString(row.id), t: row.timestamp, type: row.type });
|
|
402
|
+
if (!reference) continue;
|
|
403
|
+
// Timestamp-zero rows are source placeholders, not messages. A later
|
|
404
|
+
// hydration change is also observed by the reference listener.
|
|
405
|
+
if (reference.timestamp <= 0) continue;
|
|
406
|
+
if (reference.timestamp < input.started_at) continue;
|
|
407
|
+
if (input.chat_filter === "group" && !reference.is_group) continue;
|
|
408
|
+
if (input.chat_filter === "individual" && reference.is_group) continue;
|
|
409
|
+
references.push(reference);
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
return { after: page[page.length - 1]?.position ?? after, references, hasMore: rows.length > 500 };
|
|
413
|
+
}
|
|
414
|
+
|
|
271
415
|
function models(collection) {
|
|
272
416
|
if (Array.isArray(collection?._models)) return collection._models;
|
|
273
417
|
if (Array.isArray(collection?.models)) return collection.models;
|
|
@@ -381,20 +525,8 @@ export function whatsAppWebAdapterProgram() {
|
|
|
381
525
|
}
|
|
382
526
|
|
|
383
527
|
/**
|
|
384
|
-
*
|
|
385
|
-
*
|
|
386
|
-
* `normalizeMessage` runs per message and used to walk a whole collection on
|
|
387
|
-
* each call -- the chat name over 947 chats, the sender name over 5323
|
|
388
|
-
* contacts. That is O(messages x collection) of SYNCHRONOUS work: measured on
|
|
389
|
-
* a live prod tab the contact scan alone projects to ~6.8s, a real collect
|
|
390
|
-
* never yielded the main thread, and the extension's 90s run fence killed the
|
|
391
|
-
* run. Every run was slower than the last as backfill grew the store.
|
|
392
|
-
*
|
|
393
|
-
* Built once per collect and thrown away with it -- no cache to invalidate,
|
|
394
|
-
* and nothing here outlives the call. Only names are indexed: they read
|
|
395
|
-
* static fields, whereas `collectionByRawId` serves the reaction paths, which
|
|
396
|
-
* mutate a model in place without changing the collection, so that one keeps
|
|
397
|
-
* 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.
|
|
398
530
|
*/
|
|
399
531
|
function nameIndex(collections) {
|
|
400
532
|
const byId = (collection) => {
|
|
@@ -442,8 +574,7 @@ export function whatsAppWebAdapterProgram() {
|
|
|
442
574
|
const collections =
|
|
443
575
|
collectionsOverride ?? requireFirst(["WAWebCollections"]);
|
|
444
576
|
if (!collections) return null;
|
|
445
|
-
//
|
|
446
|
-
// in so the whole run shares it.
|
|
577
|
+
// Reads and searches share one name index across their result batch.
|
|
447
578
|
const nameLookup = names ?? nameIndex(collections);
|
|
448
579
|
let row = modelData(model);
|
|
449
580
|
let key = row.id ?? model?.id;
|
|
@@ -520,6 +651,7 @@ export function whatsAppWebAdapterProgram() {
|
|
|
520
651
|
: null,
|
|
521
652
|
media_filename: row.filename ?? row.fileName ?? row.title ?? null,
|
|
522
653
|
quoted_id: quotedId,
|
|
654
|
+
mentions_me: await mentionsOwnAccount(row),
|
|
523
655
|
is_forwarded: Boolean(row.isForwarded || Number(row.forwardingScore) > 0),
|
|
524
656
|
is_starred: Boolean(row.star ?? row.isStarred),
|
|
525
657
|
is_system_event: SYSTEM_TYPES.has(type),
|
|
@@ -568,7 +700,7 @@ export function whatsAppWebAdapterProgram() {
|
|
|
568
700
|
// Socket.stream was DISCONNECTED, then both were CONNECTED after reload.
|
|
569
701
|
// Current WhatsApp exposes account and transport state separately. An
|
|
570
702
|
// authenticated account can retain hydrated stores while its stream is
|
|
571
|
-
// disconnected; those cached messages are not a successful live
|
|
703
|
+
// disconnected; those cached messages are not a successful live read.
|
|
572
704
|
const socket = requireFirst(["WAWebSocketModel"])?.Socket;
|
|
573
705
|
const connection =
|
|
574
706
|
socket ??
|
|
@@ -673,7 +805,6 @@ export function whatsAppWebAdapterProgram() {
|
|
|
673
805
|
}
|
|
674
806
|
|
|
675
807
|
function operationCapabilities() {
|
|
676
|
-
const history = requireFirst(["WAWebChatLoadMessages"]);
|
|
677
808
|
const search = requireFirst([
|
|
678
809
|
"WAWebChatMessageSearch",
|
|
679
810
|
"WAWebMessageSearch",
|
|
@@ -684,19 +815,13 @@ export function whatsAppWebAdapterProgram() {
|
|
|
684
815
|
const edit = requireFirst(EDIT_MODULES);
|
|
685
816
|
const reaction = requireFirst(REACTION_MODULES);
|
|
686
817
|
const revoke = requireFirst(REVOKE_MODULES);
|
|
687
|
-
// Same nesting as the download path: the module root only exports
|
|
688
|
-
// `{ enforceKaleidoscopeScore, downloadManager }`.
|
|
689
|
-
const mediaModule = requireFirst(["WAWebDownloadManager"]);
|
|
690
|
-
const media = mediaModule?.downloadManager ?? mediaModule;
|
|
691
818
|
const hasFunction = (module, names) =>
|
|
692
819
|
names.some((name) => typeof module?.[name] === "function");
|
|
693
820
|
return {
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
(model) => typeof model?.downloadMedia === "function"
|
|
699
|
-
),
|
|
821
|
+
read_messages:
|
|
822
|
+
typeof indexedDB !== "undefined" &&
|
|
823
|
+
typeof requireFirst(["WAWebMsgKey"])?.fromString === "function" &&
|
|
824
|
+
typeof requireFirst(["WAWebCollections"])?.Msg?.getMessagesById === "function",
|
|
700
825
|
search_messages:
|
|
701
826
|
hasFunction(search, ["searchMessages", "searchMsgs", "search"]) ||
|
|
702
827
|
Boolean(requireFirst(["WAWebCollections"])?.Msg),
|
|
@@ -722,130 +847,6 @@ export function whatsAppWebAdapterProgram() {
|
|
|
722
847
|
};
|
|
723
848
|
}
|
|
724
849
|
|
|
725
|
-
function oldestMessage(chat) {
|
|
726
|
-
let oldest = null;
|
|
727
|
-
for (const message of models(modelData(chat).msgs ?? chat?.msgs)) {
|
|
728
|
-
const row = modelData(message);
|
|
729
|
-
const timestamp = unixSeconds(row);
|
|
730
|
-
const id = rawId(row.id ?? message?.id);
|
|
731
|
-
if (!id) continue;
|
|
732
|
-
if (
|
|
733
|
-
!oldest ||
|
|
734
|
-
timestamp < oldest.timestamp ||
|
|
735
|
-
(timestamp === oldest.timestamp && id < oldest.id)
|
|
736
|
-
) {
|
|
737
|
-
oldest = { timestamp, id };
|
|
738
|
-
}
|
|
739
|
-
}
|
|
740
|
-
return oldest;
|
|
741
|
-
}
|
|
742
|
-
|
|
743
|
-
function chatHasEarlier(chat) {
|
|
744
|
-
const row = modelData(chat);
|
|
745
|
-
const state =
|
|
746
|
-
row.msgs?.msgLoadState ??
|
|
747
|
-
chat?.msgs?.msgLoadState ??
|
|
748
|
-
row.msgLoadState ??
|
|
749
|
-
{};
|
|
750
|
-
if (state.noEarlierMsgs === true || state.hasEarlierMsgs === false)
|
|
751
|
-
return false;
|
|
752
|
-
if (row.hasEarlierMsgs === false) return false;
|
|
753
|
-
return true;
|
|
754
|
-
}
|
|
755
|
-
|
|
756
|
-
async function loadEarlier(chat, limit, deadline) {
|
|
757
|
-
const loader = requireFirst(["WAWebChatLoadMessages"]);
|
|
758
|
-
if (typeof loader?.loadEarlierMsgs !== "function") {
|
|
759
|
-
return {
|
|
760
|
-
available: false,
|
|
761
|
-
reason: "WAWebChatLoadMessages.loadEarlierMsgs unavailable",
|
|
762
|
-
loads: 0,
|
|
763
|
-
};
|
|
764
|
-
}
|
|
765
|
-
let loads = 0;
|
|
766
|
-
const beforeIds = new Set(
|
|
767
|
-
models(modelData(chat).msgs ?? chat?.msgs)
|
|
768
|
-
.map((message) => rawId(modelData(message).id ?? message?.id))
|
|
769
|
-
.filter(Boolean)
|
|
770
|
-
);
|
|
771
|
-
let previous = oldestMessage(chat);
|
|
772
|
-
let madeProgress = false;
|
|
773
|
-
while (loads < limit && chatHasEarlier(chat)) {
|
|
774
|
-
if (deadline && Date.now() >= deadline)
|
|
775
|
-
throw new Error("history deadline budget exhausted");
|
|
776
|
-
await loader.loadEarlierMsgs({ chat, msgCollection: chat.msgs });
|
|
777
|
-
loads += 1;
|
|
778
|
-
const next = oldestMessage(chat);
|
|
779
|
-
if (
|
|
780
|
-
!next ||
|
|
781
|
-
(previous &&
|
|
782
|
-
next.id === previous.id &&
|
|
783
|
-
next.timestamp === previous.timestamp)
|
|
784
|
-
)
|
|
785
|
-
break;
|
|
786
|
-
madeProgress = true;
|
|
787
|
-
previous = next;
|
|
788
|
-
}
|
|
789
|
-
let hasMore = chatHasEarlier(chat);
|
|
790
|
-
const loadedIds = () =>
|
|
791
|
-
models(modelData(chat).msgs ?? chat?.msgs)
|
|
792
|
-
.map((message) => rawId(modelData(message).id ?? message?.id))
|
|
793
|
-
.filter((id) => id && !beforeIds.has(id));
|
|
794
|
-
if (hasMore && !madeProgress) {
|
|
795
|
-
return {
|
|
796
|
-
available: true,
|
|
797
|
-
loads,
|
|
798
|
-
has_more: true,
|
|
799
|
-
loaded_ids: loadedIds(),
|
|
800
|
-
frontier_advanced: false,
|
|
801
|
-
error: "WhatsApp history loader made no progress",
|
|
802
|
-
};
|
|
803
|
-
}
|
|
804
|
-
if (hasMore) {
|
|
805
|
-
const phone =
|
|
806
|
-
loader.loadEarlierMsgsFromPhone ??
|
|
807
|
-
loader.requestEarlierMsgsFromPhone ??
|
|
808
|
-
loader.loadEarlierMsgsFromServer;
|
|
809
|
-
const state =
|
|
810
|
-
modelData(chat).msgs?.msgLoadState ?? chat?.msgs?.msgLoadState ?? {};
|
|
811
|
-
const wantsPhone =
|
|
812
|
-
state.canRequestFromPhone === true || state.hasMoreFromPhone === true;
|
|
813
|
-
if (wantsPhone && typeof phone !== "function") {
|
|
814
|
-
return {
|
|
815
|
-
available: true,
|
|
816
|
-
loads,
|
|
817
|
-
has_more: true,
|
|
818
|
-
loaded_ids: loadedIds(),
|
|
819
|
-
frontier_advanced: false,
|
|
820
|
-
error:
|
|
821
|
-
"phone-assisted WhatsApp history is advertised but its loader is unavailable",
|
|
822
|
-
};
|
|
823
|
-
}
|
|
824
|
-
if (typeof phone === "function" && wantsPhone) {
|
|
825
|
-
try {
|
|
826
|
-
await phone.call(loader, { chat, msgCollection: chat.msgs });
|
|
827
|
-
hasMore = chatHasEarlier(chat);
|
|
828
|
-
} catch (error) {
|
|
829
|
-
return {
|
|
830
|
-
available: true,
|
|
831
|
-
loads,
|
|
832
|
-
has_more: true,
|
|
833
|
-
loaded_ids: loadedIds(),
|
|
834
|
-
frontier_advanced: false,
|
|
835
|
-
error: `phone-assisted history failed: ${String(error)}`,
|
|
836
|
-
};
|
|
837
|
-
}
|
|
838
|
-
}
|
|
839
|
-
}
|
|
840
|
-
return {
|
|
841
|
-
available: true,
|
|
842
|
-
loads,
|
|
843
|
-
has_more: hasMore,
|
|
844
|
-
loaded_ids: loadedIds(),
|
|
845
|
-
frontier_advanced: true,
|
|
846
|
-
};
|
|
847
|
-
}
|
|
848
|
-
|
|
849
850
|
function uniqueMessageModels(collections) {
|
|
850
851
|
const byId = new Map();
|
|
851
852
|
for (const message of models(collections.Msg)) {
|
|
@@ -861,208 +862,6 @@ export function whatsAppWebAdapterProgram() {
|
|
|
861
862
|
return [...byId.values()];
|
|
862
863
|
}
|
|
863
864
|
|
|
864
|
-
async function collect(request) {
|
|
865
|
-
const status = readiness();
|
|
866
|
-
if (!status.ready) return { ok: false, error: status };
|
|
867
|
-
const collections = requireFirst(["WAWebCollections"]);
|
|
868
|
-
const chatFilter = ["all", "individual", "group"].includes(
|
|
869
|
-
request.chat_filter
|
|
870
|
-
)
|
|
871
|
-
? request.chat_filter
|
|
872
|
-
: "all";
|
|
873
|
-
const chatRows = [];
|
|
874
|
-
for (const chat of models(collections.Chat)) {
|
|
875
|
-
const raw = widString(modelData(chat).id ?? chat?.id);
|
|
876
|
-
const jid = (await canonicalJid(raw)).jid;
|
|
877
|
-
if (
|
|
878
|
-
!jid ||
|
|
879
|
-
jid === "status@broadcast" ||
|
|
880
|
-
jid.endsWith("@newsletter") ||
|
|
881
|
-
(chatFilter === "group" && !jid.endsWith("@g.us")) ||
|
|
882
|
-
(chatFilter === "individual" && jid.endsWith("@g.us"))
|
|
883
|
-
)
|
|
884
|
-
continue;
|
|
885
|
-
chatRows.push({ chat, jid });
|
|
886
|
-
}
|
|
887
|
-
chatRows.sort((a, b) => a.jid.localeCompare(b.jid));
|
|
888
|
-
const backfill = request.backfill ?? { complete: false, chats: {} };
|
|
889
|
-
const updates = {};
|
|
890
|
-
let cursor = backfill.cursor_chat_jid ?? null;
|
|
891
|
-
const loadedByChat = new Map();
|
|
892
|
-
const backfillMessageIds = new Set();
|
|
893
|
-
const eligible = request.backfill_disabled
|
|
894
|
-
? []
|
|
895
|
-
: chatRows.filter(
|
|
896
|
-
(entry) => backfill.chats?.[entry.jid]?.has_more !== false
|
|
897
|
-
);
|
|
898
|
-
if (eligible.length > 0) {
|
|
899
|
-
let start = cursor
|
|
900
|
-
? eligible.findIndex((entry) => entry.jid > cursor)
|
|
901
|
-
: 0;
|
|
902
|
-
if (start < 0) start = 0;
|
|
903
|
-
const rotated = [...eligible.slice(start), ...eligible.slice(0, start)];
|
|
904
|
-
const selected = rotated.slice(
|
|
905
|
-
0,
|
|
906
|
-
Math.max(1, Number(request.max_chats) || 1)
|
|
907
|
-
);
|
|
908
|
-
// The connector sends a relative budget, not an absolute instant: it runs
|
|
909
|
-
// on a different machine than this page, so any shared epoch would be off
|
|
910
|
-
// by the clock skew between them. Resolving it here against this page's
|
|
911
|
-
// own clock keeps the budget exact.
|
|
912
|
-
const budgetMs = Number(request.budget_ms);
|
|
913
|
-
const historyDeadline =
|
|
914
|
-
Number.isFinite(budgetMs) && budgetMs > 0
|
|
915
|
-
? Date.now() + budgetMs
|
|
916
|
-
: null;
|
|
917
|
-
for (const entry of selected) {
|
|
918
|
-
if (historyDeadline && Date.now() >= historyDeadline) break;
|
|
919
|
-
try {
|
|
920
|
-
const loaded = await loadEarlier(
|
|
921
|
-
entry.chat,
|
|
922
|
-
Math.max(1, Number(request.max_loads_per_chat) || 1),
|
|
923
|
-
historyDeadline
|
|
924
|
-
);
|
|
925
|
-
if (!loaded.available) {
|
|
926
|
-
return {
|
|
927
|
-
ok: false,
|
|
928
|
-
error: {
|
|
929
|
-
state: "capability_unavailable",
|
|
930
|
-
reason: loaded.reason,
|
|
931
|
-
},
|
|
932
|
-
};
|
|
933
|
-
}
|
|
934
|
-
const oldest = oldestMessage(entry.chat);
|
|
935
|
-
for (const id of loaded.loaded_ids) backfillMessageIds.add(id);
|
|
936
|
-
loadedByChat.set(entry.jid, loaded.loaded_ids);
|
|
937
|
-
if (loaded.frontier_advanced === false) {
|
|
938
|
-
updates[entry.jid] = {
|
|
939
|
-
...(backfill.chats?.[entry.jid] ?? {}),
|
|
940
|
-
error: loaded.error,
|
|
941
|
-
has_more: true,
|
|
942
|
-
loads: loaded.loads,
|
|
943
|
-
};
|
|
944
|
-
continue;
|
|
945
|
-
}
|
|
946
|
-
updates[entry.jid] = {
|
|
947
|
-
oldest_timestamp: oldest?.timestamp ?? null,
|
|
948
|
-
oldest_id: oldest?.id ?? null,
|
|
949
|
-
has_more: loaded.has_more,
|
|
950
|
-
loads: loaded.loads,
|
|
951
|
-
};
|
|
952
|
-
cursor = entry.jid;
|
|
953
|
-
} catch (error) {
|
|
954
|
-
if (/unavailable/i.test(String(error))) {
|
|
955
|
-
return {
|
|
956
|
-
ok: false,
|
|
957
|
-
error: {
|
|
958
|
-
state: "capability_unavailable",
|
|
959
|
-
reason: String(error),
|
|
960
|
-
},
|
|
961
|
-
};
|
|
962
|
-
}
|
|
963
|
-
updates[entry.jid] = {
|
|
964
|
-
...(backfill.chats?.[entry.jid] ?? {}),
|
|
965
|
-
error: String(error),
|
|
966
|
-
has_more: true,
|
|
967
|
-
};
|
|
968
|
-
}
|
|
969
|
-
}
|
|
970
|
-
}
|
|
971
|
-
|
|
972
|
-
const minimum = Number.isFinite(Number(request.minimum_timestamp))
|
|
973
|
-
? Number(request.minimum_timestamp)
|
|
974
|
-
: null;
|
|
975
|
-
const recent = Number.isFinite(Number(request.recent_since))
|
|
976
|
-
? Number(request.recent_since)
|
|
977
|
-
: null;
|
|
978
|
-
const dirtyRanges = Array.isArray(request.dirty_ranges)
|
|
979
|
-
? request.dirty_ranges
|
|
980
|
-
: [];
|
|
981
|
-
const dirtyByMessageId = new Map(
|
|
982
|
-
dirtyRanges
|
|
983
|
-
.filter((marker) => marker?.message_id)
|
|
984
|
-
.map((marker) => [marker.message_id, marker])
|
|
985
|
-
);
|
|
986
|
-
const normalized = [];
|
|
987
|
-
const quarantined = [];
|
|
988
|
-
const names = nameIndex(collections);
|
|
989
|
-
for (const model of uniqueMessageModels(collections)) {
|
|
990
|
-
const message = await normalizeMessage(
|
|
991
|
-
model,
|
|
992
|
-
"reconcile",
|
|
993
|
-
collections,
|
|
994
|
-
names
|
|
995
|
-
);
|
|
996
|
-
if (!message) continue;
|
|
997
|
-
if (message.timestamp <= 0) {
|
|
998
|
-
quarantined.push({
|
|
999
|
-
key: `${message.chat_jid}:${message.id}`,
|
|
1000
|
-
message_id: message.id,
|
|
1001
|
-
chat_jid: message.chat_jid,
|
|
1002
|
-
reason: "missing_stable_timestamp",
|
|
1003
|
-
});
|
|
1004
|
-
continue;
|
|
1005
|
-
}
|
|
1006
|
-
if (chatFilter === "group" && !message.is_group) continue;
|
|
1007
|
-
if (chatFilter === "individual" && message.is_group) continue;
|
|
1008
|
-
if (minimum != null && message.timestamp < minimum) continue;
|
|
1009
|
-
if (
|
|
1010
|
-
recent != null &&
|
|
1011
|
-
message.timestamp < recent &&
|
|
1012
|
-
!backfillMessageIds.has(message.id) &&
|
|
1013
|
-
!dirtyByMessageId.has(message.id)
|
|
1014
|
-
)
|
|
1015
|
-
continue;
|
|
1016
|
-
normalized.push(message);
|
|
1017
|
-
}
|
|
1018
|
-
normalized.sort(
|
|
1019
|
-
(a, b) => a.timestamp - b.timestamp || a.id.localeCompare(b.id)
|
|
1020
|
-
);
|
|
1021
|
-
const maxMessages = Math.max(
|
|
1022
|
-
1,
|
|
1023
|
-
Math.min(Number(request.max_messages) || 1_000, 2_000)
|
|
1024
|
-
);
|
|
1025
|
-
const byId = new Map(normalized.map((message) => [message.id, message]));
|
|
1026
|
-
const historyPages = [];
|
|
1027
|
-
for (const [chatJid, ids] of loadedByChat) {
|
|
1028
|
-
historyPages.push({
|
|
1029
|
-
chat_jid: chatJid,
|
|
1030
|
-
...(updates[chatJid] ?? {}),
|
|
1031
|
-
messages: ids.map((id) => byId.get(id)).filter(Boolean),
|
|
1032
|
-
});
|
|
1033
|
-
}
|
|
1034
|
-
const messages = normalized
|
|
1035
|
-
.filter((message) => !backfillMessageIds.has(message.id))
|
|
1036
|
-
.slice(-maxMessages)
|
|
1037
|
-
.sort((a, b) => a.timestamp - b.timestamp || a.id.localeCompare(b.id));
|
|
1038
|
-
const mergedChats = { ...(backfill.chats ?? {}), ...updates };
|
|
1039
|
-
return {
|
|
1040
|
-
ok: true,
|
|
1041
|
-
status,
|
|
1042
|
-
messages,
|
|
1043
|
-
history_pages: historyPages,
|
|
1044
|
-
quarantined,
|
|
1045
|
-
dirty_reconciled: normalized
|
|
1046
|
-
.filter((message) => dirtyByMessageId.has(message.id))
|
|
1047
|
-
.map((message) => ({
|
|
1048
|
-
key: dirtyByMessageId.get(message.id).key,
|
|
1049
|
-
message_id: message.id,
|
|
1050
|
-
})),
|
|
1051
|
-
backfill: {
|
|
1052
|
-
// `chatRows` is empty when the tab answered `probe` but its Chat
|
|
1053
|
-
// collection has not populated yet. A bare `.every()` is vacuously true
|
|
1054
|
-
// there, which would record a finished backfill that never ran.
|
|
1055
|
-
complete:
|
|
1056
|
-
request.backfill_disabled === true ||
|
|
1057
|
-
(chatRows.length > 0 &&
|
|
1058
|
-
chatRows.every((entry) => mergedChats[entry.jid]?.has_more === false)),
|
|
1059
|
-
cursor_chat_jid: cursor,
|
|
1060
|
-
inventory: chatRows.map((entry) => entry.jid),
|
|
1061
|
-
chats: updates,
|
|
1062
|
-
},
|
|
1063
|
-
};
|
|
1064
|
-
}
|
|
1065
|
-
|
|
1066
865
|
async function resolveChat(collections, input) {
|
|
1067
866
|
if (input?.self_chat === true) {
|
|
1068
867
|
const me = meUser();
|
|
@@ -1123,6 +922,109 @@ export function whatsAppWebAdapterProgram() {
|
|
|
1123
922
|
throw new Error(`WhatsApp did not confirm ${label}`);
|
|
1124
923
|
}
|
|
1125
924
|
|
|
925
|
+
/**
|
|
926
|
+
* Enumerate WhatsApp's own local history, not just the models loaded in the
|
|
927
|
+
* page. Opaque message bodies stay with WhatsApp: its native loader hydrates
|
|
928
|
+
* the selected keys. Each read scans at most one source page, so callers must
|
|
929
|
+
* follow cursors even when filtering leaves an empty page. An exact
|
|
930
|
+
* `id:<message ID>` read is the exception: ids are unique, so it walks
|
|
931
|
+
* history until that message and answers in one read.
|
|
932
|
+
*/
|
|
933
|
+
async function readMessages(input = {}) {
|
|
934
|
+
const query = String(input.query ?? "").trim();
|
|
935
|
+
const chatFilter = input.chat_filter ?? "all";
|
|
936
|
+
const limit = Math.min(100, Math.max(1, Math.trunc(Number(input.limit) || 50)));
|
|
937
|
+
let before = null;
|
|
938
|
+
if (input.cursor) {
|
|
939
|
+
const cursor = JSON.parse(input.cursor);
|
|
940
|
+
if (cursor.v !== 1 || !Number.isSafeInteger(cursor.before) || cursor.before <= 0 ||
|
|
941
|
+
cursor.query !== query || cursor.chat_filter !== chatFilter) {
|
|
942
|
+
throw new Error("Invalid WhatsApp source cursor or changed query scope");
|
|
943
|
+
}
|
|
944
|
+
before = cursor.before;
|
|
945
|
+
}
|
|
946
|
+
const keyModule = requireFirst(["WAWebMsgKey"]);
|
|
947
|
+
const exactId = query.startsWith("id:") ? query.slice(3) : null;
|
|
948
|
+
const database = await new Promise((resolve, reject) => {
|
|
949
|
+
const request = indexedDB.open("model-storage");
|
|
950
|
+
// Never create an empty replacement database when WhatsApp changes its
|
|
951
|
+
// storage layout. That would turn unavailable history into false success.
|
|
952
|
+
request.onupgradeneeded = () => {
|
|
953
|
+
request.transaction.abort();
|
|
954
|
+
reject(new Error("WhatsApp source history database is unavailable"));
|
|
955
|
+
};
|
|
956
|
+
request.onerror = () => reject(request.error);
|
|
957
|
+
request.onblocked = () => reject(new Error("WhatsApp source history database is blocked"));
|
|
958
|
+
request.onsuccess = () => resolve(request.result);
|
|
959
|
+
});
|
|
960
|
+
let sourceRows;
|
|
961
|
+
try {
|
|
962
|
+
sourceRows = await new Promise((resolve, reject) => {
|
|
963
|
+
const transaction = database.transaction("message", "readonly");
|
|
964
|
+
transaction.onabort = () => reject(transaction.error ?? new Error("WhatsApp source read aborted"));
|
|
965
|
+
const index = transaction.objectStore("message").index("rowId");
|
|
966
|
+
const range = before === null ? null : IDBKeyRange.upperBound(before, true);
|
|
967
|
+
const request = index.openCursor(range, "prev");
|
|
968
|
+
const rows = [];
|
|
969
|
+
request.onerror = () => reject(request.error);
|
|
970
|
+
request.onsuccess = () => {
|
|
971
|
+
const cursor = request.result;
|
|
972
|
+
if (!cursor) { resolve(rows); return; }
|
|
973
|
+
if (!Number.isSafeInteger(cursor.key) || cursor.key <= 0 || typeof cursor.value.id !== "string") {
|
|
974
|
+
reject(new Error("WhatsApp source history has an unsupported key layout"));
|
|
975
|
+
return;
|
|
976
|
+
}
|
|
977
|
+
if (exactId !== null) {
|
|
978
|
+
if (keyModule.fromString(cursor.value.id).id !== exactId) { cursor.continue(); return; }
|
|
979
|
+
resolve([{ position: cursor.key, id: cursor.value.id }]);
|
|
980
|
+
return;
|
|
981
|
+
}
|
|
982
|
+
rows.push({ position: cursor.key, id: cursor.value.id });
|
|
983
|
+
if (rows.length > limit) resolve(rows);
|
|
984
|
+
else cursor.continue();
|
|
985
|
+
};
|
|
986
|
+
});
|
|
987
|
+
} finally {
|
|
988
|
+
database.close();
|
|
989
|
+
}
|
|
990
|
+
const hasMore = sourceRows.length > limit;
|
|
991
|
+
const page = sourceRows.slice(0, limit);
|
|
992
|
+
const collections = requireFirst(["WAWebCollections"]);
|
|
993
|
+
const selected = page.filter((row) => {
|
|
994
|
+
const key = keyModule.fromString(row.id);
|
|
995
|
+
if (exactId !== null && key.id !== exactId) return false;
|
|
996
|
+
const remote = widString(key.remote);
|
|
997
|
+
if (remote === "status@broadcast" || remote.endsWith("@newsletter")) return false;
|
|
998
|
+
const isGroup = remote.endsWith("@g.us");
|
|
999
|
+
return !(chatFilter === "group" && !isGroup) && !(chatFilter === "individual" && isGroup);
|
|
1000
|
+
});
|
|
1001
|
+
const results = [];
|
|
1002
|
+
if (selected.length) {
|
|
1003
|
+
const loaded = await collections.Msg.getMessagesById(selected.map((row) => row.id));
|
|
1004
|
+
if (loaded?.canceled || !Array.isArray(loaded?.messages)) {
|
|
1005
|
+
throw new Error("Source message loading was canceled or unavailable");
|
|
1006
|
+
}
|
|
1007
|
+
const byKey = new Map(loaded.messages.map((model) => [String(model.id ?? modelData(model).id), model]));
|
|
1008
|
+
const names = nameIndex(collections);
|
|
1009
|
+
for (const row of selected) {
|
|
1010
|
+
const model = byKey.get(row.id);
|
|
1011
|
+
if (!model) throw new Error("Source message could not be loaded; retry the read");
|
|
1012
|
+
const message = await normalizeMessage(model, "snapshot", collections, names);
|
|
1013
|
+
// The relay boundary drops placeholder rows without a stable timestamp.
|
|
1014
|
+
// Skip them like search does; throwing would pin every later read to
|
|
1015
|
+
// the same cursor.
|
|
1016
|
+
if (!message || message.timestamp <= 0) continue;
|
|
1017
|
+
if (exactId === null && query && !`${message.body}\n${message.caption}`.toLowerCase().includes(query.toLowerCase())) continue;
|
|
1018
|
+
results.push(message);
|
|
1019
|
+
}
|
|
1020
|
+
}
|
|
1021
|
+
return {
|
|
1022
|
+
results,
|
|
1023
|
+
hasMore,
|
|
1024
|
+
...(hasMore ? { nextCursor: JSON.stringify({ v: 1, before: page[page.length - 1].position, query, chat_filter: chatFilter }) } : {}),
|
|
1025
|
+
};
|
|
1026
|
+
}
|
|
1027
|
+
|
|
1126
1028
|
async function searchMessages(input) {
|
|
1127
1029
|
const collections = requireFirst(["WAWebCollections"]);
|
|
1128
1030
|
const query = String(input?.query ?? "").trim();
|
|
@@ -1371,7 +1273,7 @@ export function whatsAppWebAdapterProgram() {
|
|
|
1371
1273
|
const message = collectionByRawId(collections.Msg, raw);
|
|
1372
1274
|
if (!message)
|
|
1373
1275
|
throw new Error(
|
|
1374
|
-
`WhatsApp message ${raw} is not loaded;
|
|
1276
|
+
`WhatsApp message ${raw} is not loaded; read it with id:${raw} from the live messages feed or search it first`
|
|
1375
1277
|
);
|
|
1376
1278
|
return message;
|
|
1377
1279
|
}
|
|
@@ -1521,195 +1423,6 @@ export function whatsAppWebAdapterProgram() {
|
|
|
1521
1423
|
};
|
|
1522
1424
|
}
|
|
1523
1425
|
|
|
1524
|
-
async function blobFromMedia(model) {
|
|
1525
|
-
const row = modelData(model);
|
|
1526
|
-
let result = null;
|
|
1527
|
-
if (typeof model?.downloadMedia === "function") {
|
|
1528
|
-
result = await model.downloadMedia({
|
|
1529
|
-
downloadEvenIfExpensive: true,
|
|
1530
|
-
rmrReason: 1,
|
|
1531
|
-
});
|
|
1532
|
-
}
|
|
1533
|
-
result =
|
|
1534
|
-
result ??
|
|
1535
|
-
modelData(model).mediaData?.mediaBlob ??
|
|
1536
|
-
model?.mediaData?.mediaBlob ??
|
|
1537
|
-
row.mediaBlob ??
|
|
1538
|
-
null;
|
|
1539
|
-
if (!result) {
|
|
1540
|
-
// The module exports `{ enforceKaleidoscopeScore, downloadManager }` —
|
|
1541
|
-
// the method sits on the nested object, so probing the module root only
|
|
1542
|
-
// ever found `undefined` and this fallback never ran.
|
|
1543
|
-
const managerModule = requireFirst(["WAWebDownloadManager"]);
|
|
1544
|
-
const manager = managerModule?.downloadManager ?? managerModule;
|
|
1545
|
-
if (typeof manager?.downloadAndMaybeDecrypt === "function") {
|
|
1546
|
-
// `downloadQpl` is NOT optional: the very first thing the function
|
|
1547
|
-
// does is call `downloadQpl.addAnnotations(...)`, so omitting it threw
|
|
1548
|
-
// "Cannot read properties of undefined" before a byte was fetched.
|
|
1549
|
-
// It is only WhatsApp's performance logger, so a stub that answers
|
|
1550
|
-
// every method with a no-op satisfies it — and a permissive proxy
|
|
1551
|
-
// keeps working when a future build calls a method we have not seen
|
|
1552
|
-
// (this already happened once: `addAnnotations` alone was not enough,
|
|
1553
|
-
// the build also calls `addPoint`).
|
|
1554
|
-
result = await manager.downloadAndMaybeDecrypt({
|
|
1555
|
-
directPath: row.directPath,
|
|
1556
|
-
encFilehash: row.encFilehash,
|
|
1557
|
-
filehash: row.filehash,
|
|
1558
|
-
mediaKey: row.mediaKey,
|
|
1559
|
-
mediaKeyTimestamp: row.mediaKeyTimestamp,
|
|
1560
|
-
type: row.type,
|
|
1561
|
-
mimetype: row.mimetype,
|
|
1562
|
-
downloadOrigin: null,
|
|
1563
|
-
partialVideoOpts: null,
|
|
1564
|
-
downloadQpl: new Proxy(
|
|
1565
|
-
{},
|
|
1566
|
-
{
|
|
1567
|
-
get: () => () => {
|
|
1568
|
-
/* WhatsApp's QPL telemetry; nothing to record here. */
|
|
1569
|
-
},
|
|
1570
|
-
}
|
|
1571
|
-
),
|
|
1572
|
-
signal: new AbortController().signal,
|
|
1573
|
-
});
|
|
1574
|
-
}
|
|
1575
|
-
}
|
|
1576
|
-
if (result === "NEED_POKE" || result?.status === "NEED_POKE")
|
|
1577
|
-
return { status: "NEED_POKE" };
|
|
1578
|
-
const candidate = result?.blob ?? result?.mediaBlob ?? result;
|
|
1579
|
-
if (candidate instanceof Blob)
|
|
1580
|
-
return { status: "downloaded", blob: candidate };
|
|
1581
|
-
// `downloadAndMaybeDecrypt` resolves the decrypted bytes as a raw
|
|
1582
|
-
// ArrayBuffer, not a Blob. Falling through to the Blob checks below
|
|
1583
|
-
// reported a perfectly good download as `unavailable`.
|
|
1584
|
-
if (candidate instanceof ArrayBuffer || ArrayBuffer.isView(candidate))
|
|
1585
|
-
return {
|
|
1586
|
-
status: "downloaded",
|
|
1587
|
-
blob: new Blob([candidate], {
|
|
1588
|
-
type: row.mimetype || "application/octet-stream",
|
|
1589
|
-
}),
|
|
1590
|
-
};
|
|
1591
|
-
if (candidate?.getData) {
|
|
1592
|
-
const data = await candidate.getData();
|
|
1593
|
-
if (data instanceof Blob) return { status: "downloaded", blob: data };
|
|
1594
|
-
if (data instanceof ArrayBuffer || ArrayBuffer.isView(data))
|
|
1595
|
-
return {
|
|
1596
|
-
status: "downloaded",
|
|
1597
|
-
blob: new Blob([data], { type: row.mimetype }),
|
|
1598
|
-
};
|
|
1599
|
-
}
|
|
1600
|
-
return { status: row.directPath ? "unavailable" : "expired" };
|
|
1601
|
-
}
|
|
1602
|
-
|
|
1603
|
-
function bytesToBase64(bytes) {
|
|
1604
|
-
let binary = "";
|
|
1605
|
-
const chunk = 0x8000;
|
|
1606
|
-
for (let i = 0; i < bytes.length; i += chunk) {
|
|
1607
|
-
binary += String.fromCharCode(...bytes.subarray(i, i + chunk));
|
|
1608
|
-
}
|
|
1609
|
-
return btoa(binary);
|
|
1610
|
-
}
|
|
1611
|
-
|
|
1612
|
-
async function downloadMedia(input) {
|
|
1613
|
-
const collections = requireFirst(["WAWebCollections"]);
|
|
1614
|
-
const model = resolveMessage(collections, input?.message_id);
|
|
1615
|
-
const row = modelData(model);
|
|
1616
|
-
if (row.isViewOnce || row.viewOnce || row.type === "view_once")
|
|
1617
|
-
return { status: "view_once" };
|
|
1618
|
-
const limit = Math.max(1, Number(input?.max_bytes) || 1);
|
|
1619
|
-
const size = Number(row.size ?? row.fileSize ?? 0);
|
|
1620
|
-
if (size > limit) return { status: "too_large", size_bytes: size };
|
|
1621
|
-
const isVideo =
|
|
1622
|
-
row.type === "video" || String(row.mimetype ?? "").startsWith("video/");
|
|
1623
|
-
if (isVideo && size <= 0) {
|
|
1624
|
-
return { status: "metadata_only", retryable: true, size_bytes: null };
|
|
1625
|
-
}
|
|
1626
|
-
// Bound the fetch HERE, where the work happens, so the dispatch always
|
|
1627
|
-
// answers. A caller-side timer cannot cancel an in-flight dispatch: it only
|
|
1628
|
-
// stops the caller waiting, and the reply then arrives with nobody left to
|
|
1629
|
-
// receive it. Racing inside the page is safe — there is no IPC child to
|
|
1630
|
-
// outlive — and it turns "too slow" into an ordinary retryable answer.
|
|
1631
|
-
const budgetMs = Math.max(0, Number(input?.timeout_ms) || 0);
|
|
1632
|
-
let downloaded;
|
|
1633
|
-
// Held so a download that wins the race can cancel its own timer. A sync
|
|
1634
|
-
// collects several items, and a stray rejecting timer per item would keep
|
|
1635
|
-
// firing into the page long after its answer shipped.
|
|
1636
|
-
let budgetTimer = null;
|
|
1637
|
-
try {
|
|
1638
|
-
downloaded = budgetMs
|
|
1639
|
-
? await Promise.race([
|
|
1640
|
-
blobFromMedia(model),
|
|
1641
|
-
new Promise((_, reject) => {
|
|
1642
|
-
budgetTimer = setTimeout(
|
|
1643
|
-
() =>
|
|
1644
|
-
reject(
|
|
1645
|
-
Object.assign(
|
|
1646
|
-
new Error("media download exceeded its budget"),
|
|
1647
|
-
{ state: "timeout_retryable" }
|
|
1648
|
-
)
|
|
1649
|
-
),
|
|
1650
|
-
budgetMs
|
|
1651
|
-
);
|
|
1652
|
-
}),
|
|
1653
|
-
])
|
|
1654
|
-
: await blobFromMedia(model);
|
|
1655
|
-
} catch (error) {
|
|
1656
|
-
const detail = String(error);
|
|
1657
|
-
const reported = String(error?.state ?? error?.status ?? "");
|
|
1658
|
-
const explicit = [
|
|
1659
|
-
"NEED_POKE",
|
|
1660
|
-
"awaiting_primary_device",
|
|
1661
|
-
"view_once",
|
|
1662
|
-
"expired",
|
|
1663
|
-
"too_large",
|
|
1664
|
-
"timeout_retryable",
|
|
1665
|
-
"metadata_only",
|
|
1666
|
-
"unavailable",
|
|
1667
|
-
].find(
|
|
1668
|
-
(state) =>
|
|
1669
|
-
reported === state ||
|
|
1670
|
-
detail.toLowerCase().includes(state.toLowerCase())
|
|
1671
|
-
);
|
|
1672
|
-
return {
|
|
1673
|
-
status: explicit ?? "unavailable",
|
|
1674
|
-
retryable: [
|
|
1675
|
-
"NEED_POKE",
|
|
1676
|
-
"awaiting_primary_device",
|
|
1677
|
-
"timeout_retryable",
|
|
1678
|
-
"metadata_only",
|
|
1679
|
-
"unavailable",
|
|
1680
|
-
].includes(explicit ?? "unavailable"),
|
|
1681
|
-
detail: detail.slice(0, 200),
|
|
1682
|
-
};
|
|
1683
|
-
} finally {
|
|
1684
|
-
if (budgetTimer !== null) clearTimeout(budgetTimer);
|
|
1685
|
-
}
|
|
1686
|
-
if (!downloaded.blob) return downloaded;
|
|
1687
|
-
if (downloaded.blob.size > limit)
|
|
1688
|
-
return { status: "too_large", size_bytes: downloaded.blob.size };
|
|
1689
|
-
const bytes = new Uint8Array(await downloaded.blob.arrayBuffer());
|
|
1690
|
-
if (bytes.length === 0) return { status: "unavailable", detail: "empty" };
|
|
1691
|
-
const mime =
|
|
1692
|
-
downloaded.blob.type || row.mimetype || "application/octet-stream";
|
|
1693
|
-
const kind = mime.startsWith("audio/")
|
|
1694
|
-
? "audio"
|
|
1695
|
-
: mime.startsWith("image/")
|
|
1696
|
-
? "image"
|
|
1697
|
-
: "file";
|
|
1698
|
-
return {
|
|
1699
|
-
status: "downloaded",
|
|
1700
|
-
attachment: {
|
|
1701
|
-
kind,
|
|
1702
|
-
filename:
|
|
1703
|
-
row.filename ??
|
|
1704
|
-
row.fileName ??
|
|
1705
|
-
`whatsapp-${rawId(row.id ?? model?.id)}`,
|
|
1706
|
-
mime_type: mime,
|
|
1707
|
-
data: bytesToBase64(bytes),
|
|
1708
|
-
size_bytes: bytes.length,
|
|
1709
|
-
},
|
|
1710
|
-
};
|
|
1711
|
-
}
|
|
1712
|
-
|
|
1713
1426
|
async function invoke(request) {
|
|
1714
1427
|
try {
|
|
1715
1428
|
if (Number(request?.adapter_version) !== ADAPTER_VERSION) {
|
|
@@ -1721,6 +1434,10 @@ export function whatsAppWebAdapterProgram() {
|
|
|
1721
1434
|
},
|
|
1722
1435
|
};
|
|
1723
1436
|
}
|
|
1437
|
+
if (request.op === "account_identity") {
|
|
1438
|
+
const accountId = widString(meUser());
|
|
1439
|
+
return accountId ? { accountId } : null;
|
|
1440
|
+
}
|
|
1724
1441
|
if (request.op === "probe") {
|
|
1725
1442
|
const status = readiness();
|
|
1726
1443
|
return status.ready
|
|
@@ -1728,6 +1445,8 @@ export function whatsAppWebAdapterProgram() {
|
|
|
1728
1445
|
: { ok: false, error: status };
|
|
1729
1446
|
}
|
|
1730
1447
|
if (request.op === "listen") return listen(request);
|
|
1448
|
+
if (request.op === "publish_changes") return await publishChanges(request);
|
|
1449
|
+
if (request.op === "observe_messages") return { ok: true, ...(await observeMessages(request.input)) };
|
|
1731
1450
|
const capabilities = operationCapabilities();
|
|
1732
1451
|
if (capabilities[request.op] !== true) {
|
|
1733
1452
|
return {
|
|
@@ -1738,9 +1457,8 @@ export function whatsAppWebAdapterProgram() {
|
|
|
1738
1457
|
},
|
|
1739
1458
|
};
|
|
1740
1459
|
}
|
|
1741
|
-
if (request.op === "
|
|
1742
|
-
|
|
1743
|
-
return { ok: true, ...(await downloadMedia(request)) };
|
|
1460
|
+
if (request.op === "read_messages")
|
|
1461
|
+
return { ok: true, ...(await readMessages(request.input)) };
|
|
1744
1462
|
if (request.op === "search_messages")
|
|
1745
1463
|
return { ok: true, ...(await searchMessages(request.input)) };
|
|
1746
1464
|
if (request.op === "draft_message")
|