@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.
- package/dist/component.json +1 -1
- package/dist/dependencies.bun.lock +3 -3
- package/npm-shrinkwrap.json +15 -15
- package/package.json +1 -1
- package/vendor/cli/dist/bundled-skills/lobu/SKILL.md +1 -1
- package/vendor/cli/dist/connectors/README.md +14 -1
- package/vendor/cli/dist/connectors/google_gmail.ts +1 -0
- package/vendor/cli/dist/connectors/jira-helpers.ts +313 -0
- package/vendor/cli/dist/connectors/jira.ts +29 -221
- package/vendor/cli/dist/connectors/linkedin-browser-script.ts +13 -0
- package/vendor/cli/dist/connectors/linkedin-draft-page.js +104 -0
- package/vendor/cli/dist/connectors/linkedin-drafts.ts +73 -0
- package/vendor/cli/dist/connectors/linkedin-identity.ts +32 -0
- package/vendor/cli/dist/connectors/linkedin-inbox-adapter.js +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/takeout-utils.ts +240 -0
- package/vendor/cli/dist/connectors/tiktok-inbox-page.js +9 -9
- package/vendor/cli/dist/connectors/tiktok-web-page.js +21 -15
- package/vendor/cli/dist/connectors/tiktok_web.ts +63 -30
- package/vendor/cli/dist/connectors/whatsapp-web-adapter.js +64 -564
- package/vendor/cli/dist/connectors/whatsapp-web-helpers.ts +30 -444
- package/vendor/cli/dist/connectors/whatsapp-web-identity.ts +20 -6
- package/vendor/cli/dist/connectors/whatsapp_web.ts +45 -645
- 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 +266 -120
- package/vendor/cli/dist/runtime-components.json +4 -4
- package/vendor/cli/dist/templates/AGENTS.md.tmpl +2 -2
- package/vendor/cli/package.json +1 -1
- package/vendor/connector-sdk/dist/browser-handoff-banner.d.ts +39 -0
- package/vendor/connector-sdk/dist/browser-handoff-banner.js +175 -0
- package/vendor/connector-sdk/dist/browser-source-read.d.ts +52 -0
- package/vendor/connector-sdk/dist/browser-source-read.js +107 -0
- package/vendor/connector-sdk/dist/connector-types.d.ts +10 -0
- package/vendor/connector-sdk/dist/index.d.ts +3 -0
- package/vendor/connector-sdk/dist/index.js +2 -0
- package/vendor/connector-sdk/package.json +9 -1
- package/vendor/connector-worker/dist/agent-turn/guest-entry.js +15 -6
- package/vendor/connector-worker/dist/agent-turn/guest.bundle.js +288 -148
- 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/client.d.ts +2 -4
- package/vendor/connector-worker/dist/daemon/client.js +0 -3
- package/vendor/connector-worker/dist/daemon/executor.js +0 -3
- package/vendor/connector-worker/dist/daemon/mac-device-daemon.d.ts +4 -3
- package/vendor/connector-worker/dist/daemon/mac-device-daemon.js +4 -10
- package/vendor/connector-worker/dist/daemon/native-bridge/executor.js +27 -25
- package/vendor/connector-worker/dist/daemon/start.js +3 -15
- package/vendor/connector-worker/dist/daemon/terminal-failure.js +0 -11
- package/vendor/connector-worker/dist/daemon/worker-id.d.ts +17 -0
- package/vendor/connector-worker/dist/daemon/worker-id.js +22 -0
- package/vendor/connector-worker/dist/mac-device-daemon.js +14 -26
- package/vendor/connector-worker/package.json +1 -1
- package/vendor/core/dist/capabilities.d.ts +1 -1
- package/vendor/core/dist/capabilities.js +0 -1
- package/vendor/core/dist/connector-query-errors.d.ts +32 -0
- package/vendor/core/dist/connector-query-errors.js +41 -1
- package/vendor/core/dist/contracts/tools/manage-conversations.js +2 -2
- package/vendor/core/dist/contracts/tools/manage-feeds.d.ts +14 -0
- package/vendor/core/dist/contracts/tools/manage-feeds.js +17 -0
- package/vendor/core/dist/contracts/worker/protocol.d.ts +8 -131
- package/vendor/core/dist/contracts/worker/protocol.js +9 -61
- 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 +2 -1
- package/vendor/core/dist/index.js +1 -0
- package/vendor/core/dist/logger.js +10 -25
- package/vendor/core/dist/utils/diagnostic-scrubber.js +258 -21
- package/vendor/core/dist/worker/wire.d.ts +0 -10
- package/vendor/core/package.json +6 -6
- 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
|
@@ -1,57 +1,41 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* WhatsApp
|
|
3
|
-
* The
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
2
|
+
* WhatsApp's feed reads live without importing message history into events.
|
|
3
|
+
* The page adapter answers source reads and observes new messages; observation
|
|
4
|
+
* publishes references only, through the paired browser's generic evaluate
|
|
5
|
+
* and feed_listen operations. An Automation reads content live when it runs. The only
|
|
6
|
+
* checkpoint is the observation position used to replay missed references.
|
|
7
7
|
*
|
|
8
8
|
* Auth stays in the user's signed-in WhatsApp Web tab. Org policy governs
|
|
9
9
|
* message writes; there is no connector-side action ledger.
|
|
10
10
|
*/
|
|
11
11
|
|
|
12
12
|
import {
|
|
13
|
+
assertNoOffsetPaging,
|
|
14
|
+
assertNoSort,
|
|
15
|
+
assertNoWindow,
|
|
13
16
|
requireBrowser,
|
|
17
|
+
splitReferenceBatches,
|
|
14
18
|
type ActionContext,
|
|
15
19
|
type ActionResult,
|
|
16
20
|
type ChromeActionDispatcher,
|
|
17
21
|
ConnectorRuntime,
|
|
18
|
-
type EventEnvelope,
|
|
19
22
|
type FeedReadContext,
|
|
20
23
|
type FeedReadResult,
|
|
21
24
|
type FeedObserveContext,
|
|
22
25
|
type RuntimeConnectorDefinition,
|
|
23
|
-
type SyncContext,
|
|
24
|
-
type SyncResult,
|
|
25
26
|
} from "@lobu/connector-sdk";
|
|
26
27
|
import {
|
|
27
|
-
type BrowserCheckpoint,
|
|
28
|
-
buildCollectionPlan,
|
|
29
28
|
type ChatFilter,
|
|
30
|
-
type CollectResponse,
|
|
31
|
-
type DirtyMarker,
|
|
32
|
-
isMediaEligible,
|
|
33
|
-
MAX_MEDIA_BYTES,
|
|
34
|
-
MAX_MEDIA_PER_RUN,
|
|
35
|
-
type MediaRecord,
|
|
36
|
-
type MediaStatus,
|
|
37
|
-
mergeBrowserCheckpoint,
|
|
38
|
-
mergeCollectedMessages,
|
|
39
29
|
messageContentKind,
|
|
40
30
|
messagePayloadText,
|
|
41
|
-
messageRevision,
|
|
42
31
|
messageTitle,
|
|
43
32
|
normalizeRelayedMessage,
|
|
44
|
-
|
|
33
|
+
type ObservationCheckpoint,
|
|
45
34
|
WHATSAPP_ADAPTER_VERSION,
|
|
46
35
|
WHATSAPP_ORIGIN,
|
|
47
|
-
type WhatsAppMessage,
|
|
48
36
|
} from "./whatsapp-web-helpers.js";
|
|
49
37
|
import { whatsAppWebAdapterProgram } from "./whatsapp-web-adapter.js";
|
|
50
38
|
|
|
51
|
-
const SOURCE_OBSERVATION_ERROR_ID = "whatsapp-web:source-observation-error";
|
|
52
|
-
// Each durable browser record is limited to 128 KiB, including its checkpoint.
|
|
53
|
-
const SOURCE_RECORD_MAX_BYTES = 128 * 1024;
|
|
54
|
-
|
|
55
39
|
/**
|
|
56
40
|
* How long a run waits for WhatsApp Web to finish hydrating before giving up.
|
|
57
41
|
*
|
|
@@ -72,44 +56,9 @@ const SOURCE_RECORD_MAX_BYTES = 128 * 1024;
|
|
|
72
56
|
*/
|
|
73
57
|
const READY_TIMEOUT_MS = 120_000;
|
|
74
58
|
const READY_POLL_INTERVAL_MS = 500;
|
|
75
|
-
/**
|
|
76
|
-
* Per-item budget, enforced by the ADAPTER inside the page.
|
|
77
|
-
*
|
|
78
|
-
* A caller-side timer cannot bound a dispatch: it cannot cancel one, so racing
|
|
79
|
-
* it only stops us waiting while the request stays in flight in the parent
|
|
80
|
-
* worker. The child then finishes and exits, the device's answer arrives with
|
|
81
|
-
* nobody to receive it, and the parent's reply-send finds a dead IPC channel —
|
|
82
|
-
* which failed a prod sync run that had already written its events, and once
|
|
83
|
-
* killed the worker daemon outright. Measured `download_media` evaluates take
|
|
84
|
-
* 3.9-5.2s, so the old 4s caller-side cap orphaned nearly every media item.
|
|
85
|
-
*
|
|
86
|
-
* The extension's `evaluate` op takes no timeout and its schema is deliberately
|
|
87
|
-
* frozen, but the adapter request shape is ours: send the budget with the
|
|
88
|
-
* request and let the page enforce it, so a slow item comes back as an ordinary
|
|
89
|
-
* `timeout_retryable` answer instead of an abandoned dispatch. This mirrors how
|
|
90
|
-
* `x.ts` hands `timeout_ms` to `wait_for_selector` rather than racing it.
|
|
91
|
-
*/
|
|
92
|
-
const MEDIA_ITEM_TIMEOUT_MS = 20_000;
|
|
93
|
-
|
|
94
|
-
/**
|
|
95
|
-
* Outer bound on the whole media phase, checked between items. The per-item
|
|
96
|
-
* budget above caps any single download; this stops a long queue of merely
|
|
97
|
-
* slow ones from consuming a run. Items past it stay retryable.
|
|
98
|
-
*/
|
|
99
|
-
const MEDIA_PHASE_BUDGET_MS = 60_000;
|
|
100
|
-
const MEDIA_CONCURRENCY = 3;
|
|
101
|
-
const MAX_TOTAL_MEDIA_BYTES = 4 * 1024 * 1024;
|
|
102
|
-
const MAX_DIRTY_MARKERS_PERSISTED = 250;
|
|
103
|
-
/**
|
|
104
|
-
* Media retry rows the checkpoint carries between runs. Attachments are never
|
|
105
|
-
* persisted — only the status/backoff bookkeeping the extension kept in
|
|
106
|
-
* IndexedDB, which is what decides whether to re-attempt a download.
|
|
107
|
-
*/
|
|
108
|
-
const MAX_MEDIA_RECORDS_PERSISTED = 500;
|
|
109
59
|
|
|
110
60
|
interface WhatsAppWebConfig {
|
|
111
61
|
chat_filter?: ChatFilter;
|
|
112
|
-
max_messages_per_sync?: number;
|
|
113
62
|
}
|
|
114
63
|
|
|
115
64
|
/** Adapter failures the bridge can recover from by re-injecting the program. */
|
|
@@ -132,8 +81,8 @@ class WhatsAppAdapterError extends Error {
|
|
|
132
81
|
*
|
|
133
82
|
* The window is persistent on purpose: a cold tab costs a 20s load plus up to
|
|
134
83
|
* 25s of WhatsApp hydration, and every one of those seconds is spent inside a
|
|
135
|
-
* run's budget. Reusing the window keeps the message model hot for
|
|
136
|
-
*
|
|
84
|
+
* run's budget. Reusing the window keeps the message model hot for reads and
|
|
85
|
+
* keeps the page observer attached between runs.
|
|
137
86
|
*/
|
|
138
87
|
async function openWhatsAppTab(
|
|
139
88
|
dispatcher: ChromeActionDispatcher
|
|
@@ -278,11 +227,6 @@ const TRANSIENT_READINESS_PATTERN = /hydrating|stores_settling/i;
|
|
|
278
227
|
* its CDP timeout and the adapter never gets to name a state -- the bridge
|
|
279
228
|
* REJECTS the dispatch instead of returning an adapter-shaped error, so this
|
|
280
229
|
* message carries no connector prefix to classify on.
|
|
281
|
-
*
|
|
282
|
-
* Only the readiness phase may treat it as transient. A `collect` that times
|
|
283
|
-
* out is a real failure, and the server's dependency_unavailable branch skips
|
|
284
|
-
* `consecutive_failures` -- classifying it here would let a permanently broken
|
|
285
|
-
* collect retry forever without ever walking the feed toward its hard pause.
|
|
286
230
|
*/
|
|
287
231
|
const BARE_EVALUATE_TIMEOUT_PATTERN = /\btimed out\b/i;
|
|
288
232
|
const DEPENDENCY_UNAVAILABLE_PREFIX =
|
|
@@ -290,19 +234,15 @@ const DEPENDENCY_UNAVAILABLE_PREFIX =
|
|
|
290
234
|
|
|
291
235
|
/**
|
|
292
236
|
* Only readiness-phase WhatsApp failures are transient. Authentication, adapter
|
|
293
|
-
* corruption, unsupported operations
|
|
294
|
-
*
|
|
237
|
+
* corruption, and unsupported operations remain ordinary connector failures
|
|
238
|
+
* and still count toward source health.
|
|
295
239
|
*/
|
|
296
|
-
function classifyWhatsAppReadinessFailure(
|
|
297
|
-
error: unknown,
|
|
298
|
-
{ allowBareTimeout = false }: { allowBareTimeout?: boolean } = {}
|
|
299
|
-
): string | null {
|
|
240
|
+
function classifyWhatsAppReadinessFailure(error: unknown): string | null {
|
|
300
241
|
if (!(error instanceof Error)) return null;
|
|
301
242
|
// A dispatch the bridge abandoned never reaches the adapter, so it has no
|
|
302
243
|
// connector prefix to match. Classify it before the prefix gate below, or a
|
|
303
|
-
// hydration stall that timed out is booked as a hard failure.
|
|
304
|
-
|
|
305
|
-
if (allowBareTimeout && BARE_EVALUATE_TIMEOUT_PATTERN.test(error.message)) {
|
|
244
|
+
// hydration stall that timed out is booked as a hard failure.
|
|
245
|
+
if (BARE_EVALUATE_TIMEOUT_PATTERN.test(error.message)) {
|
|
306
246
|
return `${DEPENDENCY_UNAVAILABLE_PREFIX} ${error.message}`;
|
|
307
247
|
}
|
|
308
248
|
// MAIN-world adapter failures may cross the extension/worker boundary without
|
|
@@ -358,9 +298,7 @@ async function readyWhatsAppTab(
|
|
|
358
298
|
}
|
|
359
299
|
await new Promise((resolve) => setTimeout(resolve, READY_POLL_INTERVAL_MS));
|
|
360
300
|
} while (Date.now() < deadline);
|
|
361
|
-
const transient = classifyWhatsAppReadinessFailure(lastError
|
|
362
|
-
allowBareTimeout: true,
|
|
363
|
-
});
|
|
301
|
+
const transient = classifyWhatsAppReadinessFailure(lastError);
|
|
364
302
|
if (transient) throw new Error(transient);
|
|
365
303
|
throw (
|
|
366
304
|
lastError ??
|
|
@@ -368,204 +306,6 @@ async function readyWhatsAppTab(
|
|
|
368
306
|
);
|
|
369
307
|
}
|
|
370
308
|
|
|
371
|
-
const MEDIA_STATES: ReadonlySet<string> = new Set([
|
|
372
|
-
"downloaded",
|
|
373
|
-
"NEED_POKE",
|
|
374
|
-
"awaiting_primary_device",
|
|
375
|
-
"view_once",
|
|
376
|
-
"expired",
|
|
377
|
-
"too_large",
|
|
378
|
-
"timeout_retryable",
|
|
379
|
-
"metadata_only",
|
|
380
|
-
"unavailable",
|
|
381
|
-
]);
|
|
382
|
-
const TERMINAL_MEDIA_STATES: ReadonlySet<string> = new Set([
|
|
383
|
-
"downloaded",
|
|
384
|
-
"view_once",
|
|
385
|
-
"expired",
|
|
386
|
-
"too_large",
|
|
387
|
-
]);
|
|
388
|
-
const RETRYABLE_MEDIA_STATES = [
|
|
389
|
-
"awaiting_primary_device",
|
|
390
|
-
"timeout_retryable",
|
|
391
|
-
"unavailable",
|
|
392
|
-
"metadata_only",
|
|
393
|
-
];
|
|
394
|
-
|
|
395
|
-
function mediaBackoff(attempts: number): number {
|
|
396
|
-
return Date.now() + Math.min(60 * 60_000, 5_000 * 2 ** Math.min(attempts, 8));
|
|
397
|
-
}
|
|
398
|
-
|
|
399
|
-
/**
|
|
400
|
-
* Download the media the run is allowed to spend on, and carry the retry
|
|
401
|
-
* bookkeeping forward in the checkpoint.
|
|
402
|
-
*
|
|
403
|
-
* The extension cached blobs in IndexedDB; a checkpoint cannot hold megabytes
|
|
404
|
-
* of base64, so only the status/backoff row survives a run. A message whose
|
|
405
|
-
* media already downloaded is not re-attempted because the head cursor has
|
|
406
|
-
* moved past it — the cache only ever mattered for re-emitted rows.
|
|
407
|
-
*/
|
|
408
|
-
async function downloadEligibleMedia(
|
|
409
|
-
dispatcher: ChromeActionDispatcher,
|
|
410
|
-
tabId: number,
|
|
411
|
-
messages: WhatsAppMessage[],
|
|
412
|
-
priorMedia: Record<string, MediaRecord>
|
|
413
|
-
): Promise<{
|
|
414
|
-
results: Map<string, MediaRecord>;
|
|
415
|
-
nextMedia: Record<string, MediaRecord>;
|
|
416
|
-
}> {
|
|
417
|
-
const results = new Map<string, MediaRecord>();
|
|
418
|
-
const nextMedia: Record<string, MediaRecord> = {};
|
|
419
|
-
const queue: Array<{
|
|
420
|
-
message: WhatsAppMessage;
|
|
421
|
-
previous: MediaRecord | undefined;
|
|
422
|
-
revision: string;
|
|
423
|
-
}> = [];
|
|
424
|
-
let totalBytes = 0;
|
|
425
|
-
|
|
426
|
-
const remember = (record: MediaRecord) => {
|
|
427
|
-
results.set(record.id, record);
|
|
428
|
-
// Terminal rows need no further bookkeeping; only pending retries are
|
|
429
|
-
// worth a checkpoint slot. So a prior record is ALWAYS retryable — there
|
|
430
|
-
// is no terminal-prior case to short-circuit. Re-downloading inside the
|
|
431
|
-
// recent-overlap window costs one fetch and dedupes on content hash;
|
|
432
|
-
// persisting terminal rows to avoid it would grow the checkpoint without
|
|
433
|
-
// bound.
|
|
434
|
-
if (record.retryable)
|
|
435
|
-
nextMedia[record.id] = { ...record, attachment: undefined };
|
|
436
|
-
};
|
|
437
|
-
|
|
438
|
-
const defer = (message: WhatsAppMessage, previous?: MediaRecord) => {
|
|
439
|
-
remember({
|
|
440
|
-
id: message.id,
|
|
441
|
-
revision: messageRevision(message),
|
|
442
|
-
status: "metadata_only",
|
|
443
|
-
retryable: true,
|
|
444
|
-
attempts: previous?.attempts ?? 0,
|
|
445
|
-
next_attempt_at: Date.now() + 5_000,
|
|
446
|
-
updated_at: Date.now(),
|
|
447
|
-
});
|
|
448
|
-
};
|
|
449
|
-
|
|
450
|
-
for (const message of messages) {
|
|
451
|
-
if (!isMediaEligible(message)) continue;
|
|
452
|
-
const revision = messageRevision(message);
|
|
453
|
-
const previous = priorMedia[message.id];
|
|
454
|
-
if (
|
|
455
|
-
previous?.revision === revision &&
|
|
456
|
-
(previous.next_attempt_at ?? 0) > Date.now()
|
|
457
|
-
) {
|
|
458
|
-
remember(previous);
|
|
459
|
-
continue;
|
|
460
|
-
}
|
|
461
|
-
if (queue.length >= MAX_MEDIA_PER_RUN) {
|
|
462
|
-
defer(message, previous);
|
|
463
|
-
continue;
|
|
464
|
-
}
|
|
465
|
-
queue.push({ message, previous, revision });
|
|
466
|
-
}
|
|
467
|
-
|
|
468
|
-
const mediaDeadline = Date.now() + MEDIA_PHASE_BUDGET_MS;
|
|
469
|
-
let cursor = 0;
|
|
470
|
-
const worker = async (): Promise<void> => {
|
|
471
|
-
while (cursor < queue.length) {
|
|
472
|
-
const item = queue[cursor++];
|
|
473
|
-
if (!item) return;
|
|
474
|
-
const { message, previous, revision } = item;
|
|
475
|
-
if (Date.now() > mediaDeadline) {
|
|
476
|
-
// Out of phase budget: leave the rest retryable rather than starting a
|
|
477
|
-
// dispatch this run cannot wait for.
|
|
478
|
-
defer(message, previous);
|
|
479
|
-
continue;
|
|
480
|
-
}
|
|
481
|
-
try {
|
|
482
|
-
const response = await invokeAdapter<{
|
|
483
|
-
status?: string;
|
|
484
|
-
retryable?: boolean;
|
|
485
|
-
attachment?: MediaRecord["attachment"];
|
|
486
|
-
}>(dispatcher, tabId, {
|
|
487
|
-
op: "download_media",
|
|
488
|
-
message_id: message.id,
|
|
489
|
-
max_bytes: MAX_MEDIA_BYTES,
|
|
490
|
-
timeout_ms: MEDIA_ITEM_TIMEOUT_MS,
|
|
491
|
-
});
|
|
492
|
-
const rawStatus = MEDIA_STATES.has(response.status ?? "")
|
|
493
|
-
? (response.status as string)
|
|
494
|
-
: "unavailable";
|
|
495
|
-
const status = (
|
|
496
|
-
rawStatus === "NEED_POKE" ? "awaiting_primary_device" : rawStatus
|
|
497
|
-
) as MediaStatus;
|
|
498
|
-
const attempts = (previous?.attempts ?? 0) + 1;
|
|
499
|
-
const retryable = Boolean(
|
|
500
|
-
response.retryable ||
|
|
501
|
-
rawStatus === "NEED_POKE" ||
|
|
502
|
-
RETRYABLE_MEDIA_STATES.includes(status)
|
|
503
|
-
);
|
|
504
|
-
const record: MediaRecord = {
|
|
505
|
-
id: message.id,
|
|
506
|
-
revision,
|
|
507
|
-
status,
|
|
508
|
-
raw_status: rawStatus,
|
|
509
|
-
retryable,
|
|
510
|
-
attachment: response.attachment,
|
|
511
|
-
mime_type:
|
|
512
|
-
response.attachment?.mime_type ?? message.media_type ?? null,
|
|
513
|
-
attempts,
|
|
514
|
-
next_attempt_at: retryable ? mediaBackoff(attempts) : null,
|
|
515
|
-
updated_at: Date.now(),
|
|
516
|
-
};
|
|
517
|
-
const size = Number(record.attachment?.size_bytes) || 0;
|
|
518
|
-
if (size && totalBytes + size > MAX_TOTAL_MEDIA_BYTES) {
|
|
519
|
-
// Over the per-run byte budget: emit the metadata placeholder and
|
|
520
|
-
// leave the download to a later run.
|
|
521
|
-
remember({
|
|
522
|
-
...record,
|
|
523
|
-
attachment: undefined,
|
|
524
|
-
status: "metadata_only",
|
|
525
|
-
retryable: true,
|
|
526
|
-
});
|
|
527
|
-
} else {
|
|
528
|
-
totalBytes += size;
|
|
529
|
-
remember(record);
|
|
530
|
-
}
|
|
531
|
-
} catch (error) {
|
|
532
|
-
const attempts = (previous?.attempts ?? 0) + 1;
|
|
533
|
-
// Both the phase budget and the child-side dispatch backstop mean
|
|
534
|
-
// "try again later", not "this media is gone".
|
|
535
|
-
const text = String(error);
|
|
536
|
-
const timedOut =
|
|
537
|
-
text.includes("timed out") || text.includes("IPC may be wedged");
|
|
538
|
-
const reported =
|
|
539
|
-
error instanceof WhatsAppAdapterError ? error.state : null;
|
|
540
|
-
const explicitState =
|
|
541
|
-
reported && MEDIA_STATES.has(reported) ? reported : null;
|
|
542
|
-
const status = (
|
|
543
|
-
explicitState === "NEED_POKE"
|
|
544
|
-
? "awaiting_primary_device"
|
|
545
|
-
: (explicitState ??
|
|
546
|
-
(timedOut ? "timeout_retryable" : "unavailable"))
|
|
547
|
-
) as MediaStatus;
|
|
548
|
-
const retryable = !TERMINAL_MEDIA_STATES.has(status);
|
|
549
|
-
remember({
|
|
550
|
-
id: message.id,
|
|
551
|
-
revision,
|
|
552
|
-
status,
|
|
553
|
-
retryable,
|
|
554
|
-
attempts,
|
|
555
|
-
next_attempt_at: retryable ? mediaBackoff(attempts) : null,
|
|
556
|
-
updated_at: Date.now(),
|
|
557
|
-
});
|
|
558
|
-
}
|
|
559
|
-
}
|
|
560
|
-
};
|
|
561
|
-
await Promise.all(
|
|
562
|
-
Array.from({ length: Math.min(MEDIA_CONCURRENCY, queue.length) }, () =>
|
|
563
|
-
worker()
|
|
564
|
-
)
|
|
565
|
-
);
|
|
566
|
-
return { results, nextMedia };
|
|
567
|
-
}
|
|
568
|
-
|
|
569
309
|
/** Normalise adapter search/read hits into the row shape the tools return. */
|
|
570
310
|
function normalizedSearchResults(
|
|
571
311
|
results: unknown[] | null | undefined
|
|
@@ -588,6 +328,7 @@ function normalizedSearchResults(
|
|
|
588
328
|
push_name: message.push_name ?? null,
|
|
589
329
|
is_group: message.is_group,
|
|
590
330
|
quoted_id: message.quoted_id ?? null,
|
|
331
|
+
mentions_me: message.mentions_me === true,
|
|
591
332
|
});
|
|
592
333
|
}
|
|
593
334
|
return rows;
|
|
@@ -602,18 +343,18 @@ const WRITE_ACTIONS = new Set([
|
|
|
602
343
|
]);
|
|
603
344
|
|
|
604
345
|
export default class WhatsAppWebConnector extends ConnectorRuntime<
|
|
605
|
-
|
|
346
|
+
ObservationCheckpoint,
|
|
606
347
|
WhatsAppWebConfig
|
|
607
348
|
> {
|
|
608
349
|
readonly definition: RuntimeConnectorDefinition<
|
|
609
|
-
|
|
350
|
+
ObservationCheckpoint,
|
|
610
351
|
WhatsAppWebConfig
|
|
611
352
|
> = {
|
|
612
353
|
key: "whatsapp.web",
|
|
613
354
|
name: "WhatsApp",
|
|
614
355
|
description:
|
|
615
|
-
"Personal WhatsApp messages read from WhatsApp Web in the paired Owletto Chrome.
|
|
616
|
-
version: "1.0
|
|
356
|
+
"Personal WhatsApp messages read live from WhatsApp Web in the paired Owletto Chrome without importing message history. Automations react to new-message references and read content on demand. Search, draft, send, edit, react to, and revoke messages.",
|
|
357
|
+
version: "2.1.0",
|
|
617
358
|
faviconDomain: "whatsapp.com",
|
|
618
359
|
authSchema: { methods: [{ type: "browser", mode: "live" }] },
|
|
619
360
|
browser: {
|
|
@@ -640,7 +381,7 @@ export default class WhatsAppWebConnector extends ConnectorRuntime<
|
|
|
640
381
|
channel_id: {
|
|
641
382
|
type: "string",
|
|
642
383
|
title: "Chat",
|
|
643
|
-
description: "WhatsApp chat ID.
|
|
384
|
+
description: "WhatsApp chat ID. Scoping replies to a group allows replies without a mention, subject to the other trigger filters. Unscoped group replies require an @mention or a reply quoting the account.",
|
|
644
385
|
},
|
|
645
386
|
},
|
|
646
387
|
},
|
|
@@ -667,121 +408,6 @@ export default class WhatsAppWebConnector extends ConnectorRuntime<
|
|
|
667
408
|
},
|
|
668
409
|
},
|
|
669
410
|
},
|
|
670
|
-
messages: {
|
|
671
|
-
sync: (ctx) => this.syncMessages(ctx),
|
|
672
|
-
webhook: { events: ["message"], mode: "trigger" },
|
|
673
|
-
key: "messages",
|
|
674
|
-
name: "Messages",
|
|
675
|
-
description:
|
|
676
|
-
"Personal WhatsApp messages from one-to-one and group chats. Syncs live changes and progressively hydrates history available to WhatsApp Web.",
|
|
677
|
-
configSchema: {
|
|
678
|
-
type: "object",
|
|
679
|
-
properties: {
|
|
680
|
-
chat_filter: {
|
|
681
|
-
type: "string",
|
|
682
|
-
enum: ["all", "individual", "group"],
|
|
683
|
-
default: "all",
|
|
684
|
-
description: "Which chats to include.",
|
|
685
|
-
},
|
|
686
|
-
max_messages_per_sync: {
|
|
687
|
-
type: "integer",
|
|
688
|
-
minimum: 1,
|
|
689
|
-
maximum: 1000,
|
|
690
|
-
default: 1000,
|
|
691
|
-
description:
|
|
692
|
-
"Bounded maximum messages emitted per sync. Additional durable history resumes on later runs.",
|
|
693
|
-
},
|
|
694
|
-
},
|
|
695
|
-
},
|
|
696
|
-
eventKinds: {
|
|
697
|
-
message: {
|
|
698
|
-
description:
|
|
699
|
-
"A WhatsApp message, caption, media placeholder, edit, revoke, or reaction-updated message.",
|
|
700
|
-
metadataSchema: {
|
|
701
|
-
type: "object",
|
|
702
|
-
required: [
|
|
703
|
-
"source",
|
|
704
|
-
"origin_id",
|
|
705
|
-
"chat_jid",
|
|
706
|
-
"is_group",
|
|
707
|
-
"from_me",
|
|
708
|
-
],
|
|
709
|
-
properties: {
|
|
710
|
-
source: { type: "string", const: "whatsapp_web" },
|
|
711
|
-
origin_id: { type: "string" },
|
|
712
|
-
chat_jid: { type: "string" },
|
|
713
|
-
is_group: { type: "boolean" },
|
|
714
|
-
from_me: { type: "boolean" },
|
|
715
|
-
is_direct_inbound: { type: "boolean" },
|
|
716
|
-
participant: { type: "string" },
|
|
717
|
-
sender_jid: { type: "string" },
|
|
718
|
-
sender_phone: { type: "string" },
|
|
719
|
-
push_name: { type: "string" },
|
|
720
|
-
media_type: { type: "string" },
|
|
721
|
-
quoted_id: { type: "string" },
|
|
722
|
-
is_forwarded: { type: "boolean" },
|
|
723
|
-
is_starred: { type: "boolean" },
|
|
724
|
-
is_system_event: { type: "boolean" },
|
|
725
|
-
edited: { type: "boolean" },
|
|
726
|
-
edit_timestamp: { type: "number" },
|
|
727
|
-
revoked: { type: "boolean" },
|
|
728
|
-
reactions: { type: "array", items: { type: "object" } },
|
|
729
|
-
voice_note_skipped: {
|
|
730
|
-
type: "string",
|
|
731
|
-
enum: [
|
|
732
|
-
"not_downloaded",
|
|
733
|
-
"too_large",
|
|
734
|
-
"empty",
|
|
735
|
-
"read_error",
|
|
736
|
-
"invalid_path",
|
|
737
|
-
],
|
|
738
|
-
},
|
|
739
|
-
media_status: {
|
|
740
|
-
type: "string",
|
|
741
|
-
enum: [
|
|
742
|
-
"downloaded",
|
|
743
|
-
"awaiting_primary_device",
|
|
744
|
-
"view_once",
|
|
745
|
-
"expired",
|
|
746
|
-
"too_large",
|
|
747
|
-
"timeout_retryable",
|
|
748
|
-
"metadata_only",
|
|
749
|
-
"unavailable",
|
|
750
|
-
],
|
|
751
|
-
},
|
|
752
|
-
},
|
|
753
|
-
},
|
|
754
|
-
attributions: [
|
|
755
|
-
{
|
|
756
|
-
role: "authored_by",
|
|
757
|
-
autoCreate: true,
|
|
758
|
-
target: {
|
|
759
|
-
entityType: "person",
|
|
760
|
-
createWhen: {
|
|
761
|
-
path: "metadata.is_direct_inbound",
|
|
762
|
-
equals: true,
|
|
763
|
-
},
|
|
764
|
-
titlePath: "metadata.push_name",
|
|
765
|
-
identities: [
|
|
766
|
-
{ namespace: "wa_jid", eventPath: "metadata.sender_jid" },
|
|
767
|
-
{ namespace: "phone", eventPath: "metadata.sender_phone" },
|
|
768
|
-
],
|
|
769
|
-
},
|
|
770
|
-
traits: {
|
|
771
|
-
push_name: {
|
|
772
|
-
eventPath: "metadata.push_name",
|
|
773
|
-
mergeStrategy: "prefer_non_empty",
|
|
774
|
-
},
|
|
775
|
-
last_seen_at: {
|
|
776
|
-
eventPath: "occurred_at",
|
|
777
|
-
mergeStrategy: "overwrite",
|
|
778
|
-
},
|
|
779
|
-
},
|
|
780
|
-
},
|
|
781
|
-
],
|
|
782
|
-
},
|
|
783
|
-
},
|
|
784
|
-
},
|
|
785
411
|
},
|
|
786
412
|
actions: {
|
|
787
413
|
search_messages: {
|
|
@@ -825,6 +451,7 @@ export default class WhatsAppWebConnector extends ConnectorRuntime<
|
|
|
825
451
|
enum: ["whatsapp_fts", "loaded_model_fallback"],
|
|
826
452
|
},
|
|
827
453
|
results: { type: "array", items: { type: "object" } },
|
|
454
|
+
hasMore: { type: "boolean" },
|
|
828
455
|
},
|
|
829
456
|
},
|
|
830
457
|
},
|
|
@@ -965,7 +592,7 @@ export default class WhatsAppWebConnector extends ConnectorRuntime<
|
|
|
965
592
|
},
|
|
966
593
|
};
|
|
967
594
|
|
|
968
|
-
private async observeMessages(ctx: FeedObserveContext<
|
|
595
|
+
private async observeMessages(ctx: FeedObserveContext<ObservationCheckpoint, WhatsAppWebConfig>): Promise<void> {
|
|
969
596
|
const dispatcher = requireBrowser(ctx);
|
|
970
597
|
const tabId = await readyWhatsAppTab(dispatcher);
|
|
971
598
|
const chatFilter = ctx.config.chat_filter ?? "all";
|
|
@@ -981,45 +608,33 @@ export default class WhatsAppWebConnector extends ConnectorRuntime<
|
|
|
981
608
|
throw new Error("A browser supporting source subscriptions is required for WhatsApp");
|
|
982
609
|
}
|
|
983
610
|
await invokeAdapter(dispatcher, tabId, {
|
|
984
|
-
op: "listen", bridge_id: binding.bridge_id, token: binding.token,
|
|
985
|
-
chat_filter: chatFilter,
|
|
611
|
+
op: "listen", bridge_id: binding.bridge_id, token: binding.token,
|
|
612
|
+
chat_filter: chatFilter, minimum_timestamp: startedAt,
|
|
986
613
|
});
|
|
987
614
|
let cursor = ctx.checkpoint ?? null;
|
|
988
615
|
for (let page = 0; page < 10; page++) {
|
|
989
616
|
const replay = await invokeAdapter<{
|
|
990
617
|
after: number; references: Array<{ id: string; timestamp: number }>; hasMore: boolean;
|
|
991
618
|
}>(dispatcher, tabId, { op: "observe_messages", input: { after, started_at: startedAt, chat_filter: chatFilter } });
|
|
992
|
-
const next:
|
|
993
|
-
schema: "owletto.whatsapp.browser.v1", adapter_version: WHATSAPP_ADAPTER_VERSION,
|
|
994
|
-
head: {}, backfill: { complete: true, cursor_chat_jid: null, inventory: [], chats: {} },
|
|
619
|
+
const next: ObservationCheckpoint = {
|
|
995
620
|
observation: { after: replay.after, started_at: startedAt, chat_filter: chatFilter },
|
|
996
621
|
};
|
|
997
622
|
const events = replay.references.map(reference => ({
|
|
998
623
|
id: reference.id, event_type: "message.created", resource_type: "message",
|
|
999
624
|
resource_ref: reference.id, occurred_at: new Date(reference.timestamp * 1000).toISOString(),
|
|
1000
625
|
}));
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
let end = Math.min(offset + 500, events.length);
|
|
1011
|
-
let batch = batchThrough(end);
|
|
1012
|
-
const size = () => new TextEncoder().encode(JSON.stringify(batch)).length;
|
|
1013
|
-
while (size() > SOURCE_RECORD_MAX_BYTES && end - offset > 1) {
|
|
1014
|
-
end = offset + Math.floor((end - offset) / 2);
|
|
1015
|
-
batch = batchThrough(end);
|
|
1016
|
-
}
|
|
1017
|
-
if (size() > SOURCE_RECORD_MAX_BYTES) throw new Error("Source reference or checkpoint exceeds the browser record limit");
|
|
626
|
+
// Earlier chunks may be replayed after interruption; existing signal
|
|
627
|
+
// dedupe handles them. Advance only after the entire page is accepted.
|
|
628
|
+
const batches = splitReferenceBatches(events, (slice, isLast, offset) => ({
|
|
629
|
+
id: `replay:${chatFilter}:${startedAt}:${after}:${replay.after}:${offset}`,
|
|
630
|
+
events: slice,
|
|
631
|
+
...(isLast ? { checkpoint: { previous: cursor, next } } : {}),
|
|
632
|
+
more: isLast && replay.hasMore && page === 9,
|
|
633
|
+
}));
|
|
634
|
+
for (const batch of batches) {
|
|
1018
635
|
await invokeAdapter(dispatcher, tabId, {
|
|
1019
636
|
op: "publish_changes", bridge_id: binding.bridge_id, token: binding.token, batch,
|
|
1020
637
|
});
|
|
1021
|
-
if (end === events.length) break;
|
|
1022
|
-
offset = end;
|
|
1023
638
|
}
|
|
1024
639
|
if (!replay.hasMore) return;
|
|
1025
640
|
cursor = next;
|
|
@@ -1027,225 +642,12 @@ export default class WhatsAppWebConnector extends ConnectorRuntime<
|
|
|
1027
642
|
}
|
|
1028
643
|
}
|
|
1029
644
|
|
|
1030
|
-
private async syncMessages(
|
|
1031
|
-
ctx: SyncContext<BrowserCheckpoint, WhatsAppWebConfig>
|
|
1032
|
-
): Promise<SyncResult> {
|
|
1033
|
-
const dispatcher = requireBrowser(ctx);
|
|
1034
|
-
const tabId = await readyWhatsAppTab(dispatcher);
|
|
1035
|
-
return await this.collectMessages(ctx, dispatcher, tabId);
|
|
1036
|
-
}
|
|
1037
|
-
|
|
1038
|
-
/**
|
|
1039
|
-
* The collect phase re-checks readiness inside the page, so a message landing
|
|
1040
|
-
* between the passing probe and this dispatch surfaces the same transient
|
|
1041
|
-
* `stores_settling` here. `readyWhatsAppTab` classifies that as a dependency
|
|
1042
|
-
* failure; without the same treatment on this path an ordinary race counts
|
|
1043
|
-
* against the feed's consecutive-failure budget and walks it toward a pause.
|
|
1044
|
-
*/
|
|
1045
|
-
private async collectMessages(
|
|
1046
|
-
ctx: SyncContext<BrowserCheckpoint, WhatsAppWebConfig>,
|
|
1047
|
-
dispatcher: ChromeActionDispatcher,
|
|
1048
|
-
tabId: number
|
|
1049
|
-
): Promise<SyncResult> {
|
|
1050
|
-
try {
|
|
1051
|
-
return await this.collectMessagesInner(ctx, dispatcher, tabId);
|
|
1052
|
-
} catch (error) {
|
|
1053
|
-
const transient = classifyWhatsAppReadinessFailure(error);
|
|
1054
|
-
if (transient) throw new Error(transient);
|
|
1055
|
-
throw error;
|
|
1056
|
-
}
|
|
1057
|
-
}
|
|
1058
|
-
|
|
1059
|
-
private async collectMessagesInner(
|
|
1060
|
-
ctx: SyncContext<BrowserCheckpoint, WhatsAppWebConfig>,
|
|
1061
|
-
dispatcher: ChromeActionDispatcher,
|
|
1062
|
-
tabId: number
|
|
1063
|
-
): Promise<SyncResult> {
|
|
1064
|
-
const { checkpoint, request } = buildCollectionPlan({
|
|
1065
|
-
checkpoint: ctx.checkpoint as Record<string, unknown> | null,
|
|
1066
|
-
config: ctx.config as Record<string, unknown>,
|
|
1067
|
-
});
|
|
1068
|
-
|
|
1069
|
-
const observed = await dispatcher.dispatch<{
|
|
1070
|
-
bridge_id: string;
|
|
1071
|
-
binding_id: string;
|
|
1072
|
-
epoch: string;
|
|
1073
|
-
token: string;
|
|
1074
|
-
listening?: boolean;
|
|
1075
|
-
records: Array<{ revision: number; payload: Record<string, unknown> }>;
|
|
1076
|
-
}>("feed_listen", { tab_id: tabId }).catch((error: unknown) => {
|
|
1077
|
-
const message = error instanceof Error ? error.message : String(error);
|
|
1078
|
-
if (message.includes("Owletto for Chrome: unknown dispatch") && message.includes("action_key='feed_listen'")) {
|
|
1079
|
-
throw new Error("[lobu:dependency_unavailable:browser_extension_update_required] Update the paired Owletto extension to support connector feed listeners");
|
|
1080
|
-
}
|
|
1081
|
-
throw error;
|
|
1082
|
-
});
|
|
1083
|
-
if (!observed.bridge_id || !Array.isArray(observed.records)) {
|
|
1084
|
-
throw new Error("The paired browser must support connector feed listeners; update the Owletto extension");
|
|
1085
|
-
}
|
|
1086
|
-
const sourceErrors = observed.records.filter((row) =>
|
|
1087
|
-
row.payload.id === SOURCE_OBSERVATION_ERROR_ID &&
|
|
1088
|
-
typeof row.payload.source_error === "string"
|
|
1089
|
-
);
|
|
1090
|
-
// A real rebind proves a prior page failure recovered. A dry snapshot
|
|
1091
|
-
// cannot install that listener, so it must keep surfacing the marker.
|
|
1092
|
-
if (observed.listening !== false) await invokeAdapter(dispatcher, tabId, {
|
|
1093
|
-
op: "listen", bridge_id: observed.bridge_id, token: observed.token,
|
|
1094
|
-
chat_filter: request.chat_filter,
|
|
1095
|
-
minimum_timestamp: request.minimum_timestamp,
|
|
1096
|
-
recent_since: request.recent_since ?? Math.floor(Date.now() / 1000) - 15 * 60,
|
|
1097
|
-
});
|
|
1098
|
-
else if (sourceErrors.length > 0) {
|
|
1099
|
-
throw new Error(String(sourceErrors[0].payload.source_error));
|
|
1100
|
-
}
|
|
1101
|
-
|
|
1102
|
-
// Recovery ranges and media retries remain in the normal feed checkpoint.
|
|
1103
|
-
const dirtyBefore = checkpoint.dirty ?? [];
|
|
1104
|
-
const dirtyBatch = dirtyBefore.slice(0, MAX_DIRTY_MARKERS_PERSISTED);
|
|
1105
|
-
request.dirty_ranges = dirtyBatch.map((marker) => ({
|
|
1106
|
-
key: marker.key,
|
|
1107
|
-
chat_jid: marker.chat_jid ?? null,
|
|
1108
|
-
message_id: marker.message_id ?? null,
|
|
1109
|
-
minimum_timestamp: marker.minimum_timestamp ?? null,
|
|
1110
|
-
}));
|
|
1111
|
-
|
|
1112
|
-
const result = await invokeAdapter<CollectResponse>(
|
|
1113
|
-
dispatcher,
|
|
1114
|
-
tabId,
|
|
1115
|
-
request as unknown as Record<string, unknown>
|
|
1116
|
-
);
|
|
1117
|
-
|
|
1118
|
-
// History pages come back separately from the recent window so a backfill
|
|
1119
|
-
// page can advance its chat cursor without competing for the recent slot.
|
|
1120
|
-
const historyMessages = (result.history_pages ?? []).flatMap(
|
|
1121
|
-
(page) => page.messages ?? []
|
|
1122
|
-
);
|
|
1123
|
-
const inScope = (row: Record<string, unknown>) =>
|
|
1124
|
-
(request.chat_filter !== "group" || row.is_group === true) &&
|
|
1125
|
-
(request.chat_filter !== "individual" || row.is_group !== true) &&
|
|
1126
|
-
(request.minimum_timestamp == null || Number(row.timestamp) >= request.minimum_timestamp);
|
|
1127
|
-
const buffered = observed.records.filter((row) =>
|
|
1128
|
-
!sourceErrors.includes(row) && inScope(row.payload)
|
|
1129
|
-
);
|
|
1130
|
-
const bufferedIds = new Set(buffered.map((row) => row.payload.id));
|
|
1131
|
-
const messages = mergeCollectedMessages(
|
|
1132
|
-
[...(result.messages ?? []), ...historyMessages],
|
|
1133
|
-
buffered.map((row) => row.payload),
|
|
1134
|
-
request.minimum_timestamp
|
|
1135
|
-
).sort((a, b) => Number(bufferedIds.has(b.id)) - Number(bufferedIds.has(a.id)) || a.timestamp - b.timestamp || a.id.localeCompare(b.id))
|
|
1136
|
-
.slice(0, request.max_messages);
|
|
1137
|
-
|
|
1138
|
-
const emittedIds = new Set(messages.map((message) => message.id));
|
|
1139
|
-
const reconciledKeys = new Set(
|
|
1140
|
-
(result.dirty_reconciled ?? [])
|
|
1141
|
-
.filter(
|
|
1142
|
-
(marker) => marker.message_id && emittedIds.has(marker.message_id)
|
|
1143
|
-
)
|
|
1144
|
-
.map((marker) => marker.key)
|
|
1145
|
-
);
|
|
1146
|
-
|
|
1147
|
-
const { results: media, nextMedia } = await downloadEligibleMedia(
|
|
1148
|
-
dispatcher,
|
|
1149
|
-
tabId,
|
|
1150
|
-
messages,
|
|
1151
|
-
checkpoint.media ?? {}
|
|
1152
|
-
);
|
|
1153
|
-
|
|
1154
|
-
const events: EventEnvelope[] = messages.map((message) =>
|
|
1155
|
-
toEventEnvelope(message, media.get(message.id))
|
|
1156
|
-
);
|
|
1157
|
-
|
|
1158
|
-
const nextCheckpoint = mergeBrowserCheckpoint(
|
|
1159
|
-
checkpoint as unknown as Record<string, unknown>,
|
|
1160
|
-
result,
|
|
1161
|
-
messages
|
|
1162
|
-
);
|
|
1163
|
-
// A live burst can consume this run's budget. Retain collection cursors
|
|
1164
|
-
// when any collected row was deferred, so backfill cannot skip that page.
|
|
1165
|
-
const deferredCollection = mergeCollectedMessages(
|
|
1166
|
-
[...(result.messages ?? []), ...historyMessages], [], request.minimum_timestamp
|
|
1167
|
-
).some((row) => !emittedIds.has(row.id));
|
|
1168
|
-
if (deferredCollection) {
|
|
1169
|
-
nextCheckpoint.head = checkpoint.head;
|
|
1170
|
-
nextCheckpoint.backfill = { ...checkpoint.backfill, complete: false };
|
|
1171
|
-
}
|
|
1172
|
-
nextCheckpoint.source_ack = {
|
|
1173
|
-
binding_id: observed.binding_id,
|
|
1174
|
-
epoch: observed.epoch,
|
|
1175
|
-
records: observed.records
|
|
1176
|
-
.filter((row) => sourceErrors.includes(row) || emittedIds.has(String(row.payload.id)) || !inScope(row.payload))
|
|
1177
|
-
.map((row) => ({ id: String(row.payload.id), revision: row.revision })),
|
|
1178
|
-
};
|
|
1179
|
-
|
|
1180
|
-
// Carry forward the markers this run did not reconcile, plus anything the
|
|
1181
|
-
// adapter newly quarantined.
|
|
1182
|
-
const dirtyByKey = new Map<string, DirtyMarker>();
|
|
1183
|
-
for (const marker of dirtyBefore) {
|
|
1184
|
-
if (!reconciledKeys.has(marker.key)) dirtyByKey.set(marker.key, marker);
|
|
1185
|
-
}
|
|
1186
|
-
for (const marker of result.quarantined ?? []) {
|
|
1187
|
-
dirtyByKey.set(marker.key, {
|
|
1188
|
-
...marker,
|
|
1189
|
-
message_id: marker.message_id ?? null,
|
|
1190
|
-
minimum_timestamp: null,
|
|
1191
|
-
});
|
|
1192
|
-
}
|
|
1193
|
-
const dirty: DirtyMarker[] = [...dirtyByKey.values()].slice(
|
|
1194
|
-
0,
|
|
1195
|
-
MAX_DIRTY_MARKERS_PERSISTED
|
|
1196
|
-
);
|
|
1197
|
-
// `mergeBrowserCheckpoint` spreads the prior checkpoint forward, so every
|
|
1198
|
-
// one of these fields must be REPLACED, not conditionally set: a marker
|
|
1199
|
-
// the adapter just reconciled has to leave the checkpoint, or the next run
|
|
1200
|
-
// re-requests it and the list never empties.
|
|
1201
|
-
const diagnostics: Record<string, unknown> = {};
|
|
1202
|
-
if (dirty.length > 0) {
|
|
1203
|
-
nextCheckpoint.dirty = dirty;
|
|
1204
|
-
nextCheckpoint.backfill.complete = false;
|
|
1205
|
-
const reasons: Record<string, number> = {};
|
|
1206
|
-
for (const marker of dirty) {
|
|
1207
|
-
const reason = marker.reason ?? "unknown";
|
|
1208
|
-
reasons[reason] = (reasons[reason] ?? 0) + 1;
|
|
1209
|
-
}
|
|
1210
|
-
diagnostics.dirty_reconciliation = {
|
|
1211
|
-
pending_count: dirty.length,
|
|
1212
|
-
requested_count: dirtyBatch.length,
|
|
1213
|
-
reasons: Object.fromEntries(Object.entries(reasons).slice(0, 10)),
|
|
1214
|
-
};
|
|
1215
|
-
} else {
|
|
1216
|
-
nextCheckpoint.dirty = undefined;
|
|
1217
|
-
}
|
|
1218
|
-
|
|
1219
|
-
const persistedMedia = Object.entries(nextMedia).slice(
|
|
1220
|
-
0,
|
|
1221
|
-
MAX_MEDIA_RECORDS_PERSISTED
|
|
1222
|
-
);
|
|
1223
|
-
nextCheckpoint.media =
|
|
1224
|
-
persistedMedia.length > 0
|
|
1225
|
-
? Object.fromEntries(persistedMedia)
|
|
1226
|
-
: undefined;
|
|
1227
|
-
|
|
1228
|
-
const mediaStatusCounts: Record<string, number> = {};
|
|
1229
|
-
for (const record of media.values()) {
|
|
1230
|
-
mediaStatusCounts[record.status] =
|
|
1231
|
-
(mediaStatusCounts[record.status] ?? 0) + 1;
|
|
1232
|
-
}
|
|
1233
|
-
if (Object.keys(mediaStatusCounts).length > 0) {
|
|
1234
|
-
diagnostics.media_status_counts = mediaStatusCounts;
|
|
1235
|
-
}
|
|
1236
|
-
nextCheckpoint.diagnostics =
|
|
1237
|
-
Object.keys(diagnostics).length > 0 ? diagnostics : undefined;
|
|
1238
|
-
|
|
1239
|
-
await ctx.commit(events, nextCheckpoint as unknown as BrowserCheckpoint);
|
|
1240
|
-
return { status: "complete" };
|
|
1241
|
-
}
|
|
1242
|
-
|
|
1243
645
|
private async readMessages(ctx: FeedReadContext<WhatsAppWebConfig>): Promise<FeedReadResult> {
|
|
1244
|
-
|
|
1245
|
-
|
|
646
|
+
assertNoOffsetPaging(ctx, "WhatsApp live reads paginate with the returned cursor, not an offset.");
|
|
647
|
+
assertNoSort(ctx, "WhatsApp live reads use source storage order; explicit sorting is not supported.");
|
|
1246
648
|
// Source message time cannot represent edits, revokes, or delayed arrivals.
|
|
1247
|
-
//
|
|
1248
|
-
|
|
649
|
+
// Automations subscribe to new-message references and read content by ID.
|
|
650
|
+
assertNoWindow(ctx, "WhatsApp live reads do not support change-time windows; subscribe to message.created and read references by id:<message ID>.");
|
|
1249
651
|
const dispatcher = requireBrowser(ctx);
|
|
1250
652
|
const tabId = await readyWhatsAppTab(dispatcher);
|
|
1251
653
|
const response = await invokeAdapter<{
|
|
@@ -1277,6 +679,7 @@ export default class WhatsAppWebConnector extends ConnectorRuntime<
|
|
|
1277
679
|
{ name: "push_name", type: "string" },
|
|
1278
680
|
{ name: "is_group", type: "boolean" },
|
|
1279
681
|
{ name: "quoted_id", type: "string" },
|
|
682
|
+
{ name: "mentions_me", type: "boolean" },
|
|
1280
683
|
],
|
|
1281
684
|
nextCursor: response.nextCursor,
|
|
1282
685
|
hasMore: response.hasMore,
|
|
@@ -1304,9 +707,6 @@ export default class WhatsAppWebConnector extends ConnectorRuntime<
|
|
|
1304
707
|
results: normalizedSearchResults(
|
|
1305
708
|
response.results as unknown[] | undefined
|
|
1306
709
|
),
|
|
1307
|
-
...(typeof response.total === "number"
|
|
1308
|
-
? { total: response.total }
|
|
1309
|
-
: {}),
|
|
1310
710
|
...(typeof response.hasMore === "boolean"
|
|
1311
711
|
? { hasMore: response.hasMore }
|
|
1312
712
|
: {}),
|
|
@@ -1314,8 +714,8 @@ export default class WhatsAppWebConnector extends ConnectorRuntime<
|
|
|
1314
714
|
};
|
|
1315
715
|
}
|
|
1316
716
|
// A send that reports no raw WhatsApp key.id did not demonstrably land;
|
|
1317
|
-
// fail loudly rather than record a success
|
|
1318
|
-
// corroborate
|
|
717
|
+
// fail loudly rather than record a success no message reference will
|
|
718
|
+
// corroborate.
|
|
1319
719
|
if (action === "send_message" && !response.message_id) {
|
|
1320
720
|
return {
|
|
1321
721
|
success: false,
|