@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,74 +1,44 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Transport-neutral helpers for the WhatsApp Web connector.
|
|
3
3
|
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
* in the connector-worker.
|
|
9
|
-
*
|
|
10
|
-
* Message normalization stays byte-comparable with the extension-native feed
|
|
11
|
-
* it replaces. The connector emits an `EventEnvelope` instead of a device feed
|
|
12
|
-
* item, and its checkpoint also carries acknowledgments for the generic source
|
|
13
|
-
* observation buffer.
|
|
4
|
+
* They are deliberately free of WhatsApp private APIs — the unstable
|
|
5
|
+
* MAIN-world surface lives in `whatsapp-web-adapter.js` and is injected into
|
|
6
|
+
* the page. Everything here runs in the connector-worker and shapes the rows
|
|
7
|
+
* that live reads and searches return. These helpers do not ingest events.
|
|
14
8
|
*/
|
|
15
9
|
|
|
16
|
-
import
|
|
10
|
+
import {
|
|
11
|
+
normalizeWhatsAppUserJid,
|
|
12
|
+
WHATSAPP_GROUP_SUFFIX,
|
|
13
|
+
WHATSAPP_LEGACY_PHONE_SERVER,
|
|
14
|
+
WHATSAPP_PHONE_SERVER,
|
|
15
|
+
} from "./whatsapp-web-identity.js";
|
|
17
16
|
|
|
18
|
-
export const WHATSAPP_ADAPTER_VERSION =
|
|
17
|
+
export const WHATSAPP_ADAPTER_VERSION = 25;
|
|
19
18
|
export const WHATSAPP_ORIGIN = "https://web.whatsapp.com";
|
|
20
|
-
const WHATSAPP_SOURCE = "whatsapp_web";
|
|
21
|
-
const RECENT_OVERLAP_SECONDS = 15 * 60;
|
|
22
|
-
const MAX_MESSAGES_PER_RUN = 1_000;
|
|
23
|
-
const MAX_CHATS_PER_RUN = 6;
|
|
24
|
-
const MAX_LOADS_PER_CHAT = 2;
|
|
25
|
-
export const MAX_MEDIA_BYTES = 2 * 1024 * 1024;
|
|
26
|
-
export const MAX_MEDIA_PER_RUN = 12;
|
|
27
|
-
/**
|
|
28
|
-
* Ceiling on the time the page may spend paging history in one run. The real
|
|
29
|
-
* bound is MAX_CHATS_PER_RUN * MAX_LOADS_PER_CHAT, so this only catches a
|
|
30
|
-
* `loadEarlierMsgs` that hangs: without it a stuck load burns the whole device
|
|
31
|
-
* action and the run returns nothing instead of the messages already collected.
|
|
32
|
-
*
|
|
33
|
-
* It MUST stay strictly under the extension's run fence. Every
|
|
34
|
-
* `dispatch("evaluate", ...)` is claimed as its own chrome action run, and
|
|
35
|
-
* `background.js` fences each one at RUN_TIMEOUT_MS (90s) from the moment it
|
|
36
|
-
* is claimed. At 90s this budget equalled that fence, so the adapter was
|
|
37
|
-
* entitled to spend the entire run paging history and still owed a
|
|
38
|
-
* serialize-and-return it could never afford. The run died as `run timed out
|
|
39
|
-
* after 90000ms`, and because a failed run discards its collected messages the
|
|
40
|
-
* checkpoint never advanced -- the next run repeated the same oversized work.
|
|
41
|
-
* That stalled a prod feed for three days at 17 consecutive failures.
|
|
42
|
-
*
|
|
43
|
-
* 60s leaves 30s of headroom for the dispatch round trip and the answer's
|
|
44
|
-
* serialization, matching the media phase's own budget.
|
|
45
|
-
*/
|
|
46
|
-
const COLLECT_BUDGET_MS = 60_000;
|
|
47
19
|
|
|
48
20
|
export type ChatFilter = "all" | "individual" | "group";
|
|
49
21
|
|
|
50
|
-
export type WhatsAppReaction = Record<string, unknown>;
|
|
51
|
-
|
|
52
22
|
/** A message after `normalizeRelayedMessage` — every field the port relies on. */
|
|
53
23
|
export interface WhatsAppMessage {
|
|
54
24
|
id: string;
|
|
55
25
|
chat_jid: string;
|
|
56
26
|
chat_name?: string | null;
|
|
57
27
|
sender_jid: string | null;
|
|
58
|
-
participant: string | null;
|
|
59
28
|
sender_phone: string | null;
|
|
60
29
|
push_name?: string | null;
|
|
61
30
|
is_group: boolean;
|
|
62
31
|
from_me: boolean;
|
|
63
32
|
timestamp: number;
|
|
64
33
|
occurred_at: string;
|
|
65
|
-
reactions: WhatsAppReaction[];
|
|
66
34
|
body?: string | null;
|
|
67
35
|
caption?: string | null;
|
|
68
36
|
message_type?: string | null;
|
|
69
37
|
media_kind?: string | null;
|
|
70
38
|
media_type?: string | null;
|
|
71
39
|
quoted_id?: string | null;
|
|
40
|
+
/** The message @mentions the account or quotes one of its messages. */
|
|
41
|
+
mentions_me?: boolean;
|
|
72
42
|
edited?: boolean;
|
|
73
43
|
edit_timestamp?: number | null;
|
|
74
44
|
revoked?: boolean;
|
|
@@ -78,121 +48,9 @@ export interface WhatsAppMessage {
|
|
|
78
48
|
[key: string]: unknown;
|
|
79
49
|
}
|
|
80
50
|
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
oldest_id?: string | null;
|
|
84
|
-
has_more?: boolean;
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
export interface BrowserCheckpoint {
|
|
88
|
-
schema: "owletto.whatsapp.browser.v1";
|
|
89
|
-
adapter_version: number;
|
|
90
|
-
/**
|
|
91
|
-
* Collect nothing at or before this instant, because another source already
|
|
92
|
-
* ingested it. Nothing in this repo assigns it: an operator writes it into an
|
|
93
|
-
* existing feed checkpoint when a connection changes source. Absent on a
|
|
94
|
-
* fresh connection, which backfills from scratch.
|
|
95
|
-
*/
|
|
96
|
-
cutover_unix_seconds?: number | null;
|
|
97
|
-
head: { timestamp?: number; id?: string | null };
|
|
98
|
-
backfill: {
|
|
99
|
-
complete: boolean;
|
|
100
|
-
cursor_chat_jid: string | null;
|
|
101
|
-
inventory: string[];
|
|
102
|
-
chats: Record<string, BackfillChatState>;
|
|
103
|
-
};
|
|
104
|
-
/** Media retries and dirty markers the extension kept in IndexedDB. */
|
|
105
|
-
media?: Record<string, MediaRecord>;
|
|
106
|
-
dirty?: DirtyMarker[];
|
|
107
|
-
diagnostics?: Record<string, unknown>;
|
|
108
|
-
last_run_at?: string;
|
|
109
|
-
/** Reference-only observation position in WhatsApp's own local message store. */
|
|
51
|
+
/** Reference-only observation position in WhatsApp's own local message store. */
|
|
52
|
+
export interface ObservationCheckpoint {
|
|
110
53
|
observation?: { after: number; started_at: number; chat_filter: ChatFilter };
|
|
111
|
-
/** Exact buffered revisions included in the last successful sync. */
|
|
112
|
-
source_ack?: {
|
|
113
|
-
binding_id: string;
|
|
114
|
-
epoch: string;
|
|
115
|
-
records: Array<{ id: string; revision: number }>;
|
|
116
|
-
};
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
export interface DirtyMarker {
|
|
120
|
-
key: string;
|
|
121
|
-
chat_jid?: string | null;
|
|
122
|
-
message_id?: string | null;
|
|
123
|
-
minimum_timestamp?: number | null;
|
|
124
|
-
reason?: string;
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
export type MediaStatus =
|
|
128
|
-
| "downloaded"
|
|
129
|
-
| "awaiting_primary_device"
|
|
130
|
-
| "view_once"
|
|
131
|
-
| "expired"
|
|
132
|
-
| "too_large"
|
|
133
|
-
| "timeout_retryable"
|
|
134
|
-
| "metadata_only"
|
|
135
|
-
| "unavailable";
|
|
136
|
-
|
|
137
|
-
export interface MediaAttachment {
|
|
138
|
-
kind: string;
|
|
139
|
-
filename: string;
|
|
140
|
-
mime_type: string;
|
|
141
|
-
data: string;
|
|
142
|
-
size_bytes: number;
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
export interface MediaRecord {
|
|
146
|
-
id: string;
|
|
147
|
-
revision: string;
|
|
148
|
-
status: MediaStatus;
|
|
149
|
-
raw_status?: string;
|
|
150
|
-
retryable: boolean;
|
|
151
|
-
attachment?: MediaAttachment;
|
|
152
|
-
mime_type?: string | null;
|
|
153
|
-
attempts: number;
|
|
154
|
-
next_attempt_at: number | null;
|
|
155
|
-
updated_at: number;
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
export interface CollectRequest {
|
|
159
|
-
op: "collect";
|
|
160
|
-
adapter_version: number;
|
|
161
|
-
max_messages: number;
|
|
162
|
-
max_chats: number;
|
|
163
|
-
max_loads_per_chat: number;
|
|
164
|
-
chat_filter: ChatFilter;
|
|
165
|
-
recent_since: number | null;
|
|
166
|
-
minimum_timestamp: number | null;
|
|
167
|
-
backfill_disabled: boolean;
|
|
168
|
-
backfill: BrowserCheckpoint["backfill"];
|
|
169
|
-
dirty_ranges?: Array<Omit<DirtyMarker, "reason">>;
|
|
170
|
-
/**
|
|
171
|
-
* How long the page may spend paging history, in milliseconds. Relative, not
|
|
172
|
-
* an absolute instant: the connector and the page run on different machines,
|
|
173
|
-
* so the page resolves this against its own clock.
|
|
174
|
-
*/
|
|
175
|
-
budget_ms?: number | null;
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
export interface CollectResponse {
|
|
179
|
-
ok: true;
|
|
180
|
-
status: Record<string, unknown>;
|
|
181
|
-
messages: unknown[];
|
|
182
|
-
history_pages?: Array<
|
|
183
|
-
{
|
|
184
|
-
chat_jid: string;
|
|
185
|
-
messages: unknown[];
|
|
186
|
-
} & BackfillChatState
|
|
187
|
-
>;
|
|
188
|
-
quarantined?: DirtyMarker[];
|
|
189
|
-
dirty_reconciled?: Array<{ key: string; message_id: string }>;
|
|
190
|
-
backfill: {
|
|
191
|
-
complete: boolean;
|
|
192
|
-
cursor_chat_jid: string | null;
|
|
193
|
-
inventory: string[];
|
|
194
|
-
chats: Record<string, BackfillChatState>;
|
|
195
|
-
};
|
|
196
54
|
}
|
|
197
55
|
|
|
198
56
|
export function rawMessageId(value: unknown): string | null {
|
|
@@ -211,17 +69,23 @@ export function rawMessageId(value: unknown): string | null {
|
|
|
211
69
|
|
|
212
70
|
export function canonicalizeJid(value: unknown): string | null {
|
|
213
71
|
if (typeof value !== "string") return null;
|
|
72
|
+
// A strict person JID normalizes through the identity module (legacy
|
|
73
|
+
// `@c.us` spelling, multi-device suffixes); chats, groups, broadcasts and
|
|
74
|
+
// newsletters are not user keys and pass through in canonical case.
|
|
75
|
+
const user = normalizeWhatsAppUserJid(value);
|
|
76
|
+
if (user) return user;
|
|
214
77
|
const jid = value.trim().toLowerCase();
|
|
215
78
|
if (!jid?.includes("@")) return null;
|
|
216
|
-
if (jid.endsWith(
|
|
79
|
+
if (jid.endsWith(`@${WHATSAPP_LEGACY_PHONE_SERVER}`))
|
|
80
|
+
return `${jid.slice(0, -WHATSAPP_LEGACY_PHONE_SERVER.length - 1)}@${WHATSAPP_PHONE_SERVER}`;
|
|
217
81
|
return jid;
|
|
218
82
|
}
|
|
219
83
|
|
|
220
84
|
function jidPhone(jid: unknown): string | null {
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
return
|
|
85
|
+
if (typeof jid !== "string") return null;
|
|
86
|
+
const user = normalizeWhatsAppUserJid(jid);
|
|
87
|
+
if (!user || !user.endsWith(`@${WHATSAPP_PHONE_SERVER}`)) return null;
|
|
88
|
+
return user.slice(0, -WHATSAPP_PHONE_SERVER.length - 1);
|
|
225
89
|
}
|
|
226
90
|
|
|
227
91
|
function finiteSeconds(value: unknown): number | null {
|
|
@@ -229,93 +93,6 @@ function finiteSeconds(value: unknown): number | null {
|
|
|
229
93
|
return Number.isFinite(seconds) && seconds >= 0 ? seconds : null;
|
|
230
94
|
}
|
|
231
95
|
|
|
232
|
-
export function initializeBrowserCheckpoint(
|
|
233
|
-
checkpoint: Record<string, unknown> | null | undefined
|
|
234
|
-
): BrowserCheckpoint {
|
|
235
|
-
const raw = (checkpoint ?? {}) as Record<string, any>;
|
|
236
|
-
if (raw.schema === "owletto.whatsapp.browser.v1") {
|
|
237
|
-
return {
|
|
238
|
-
...(raw as BrowserCheckpoint),
|
|
239
|
-
adapter_version: WHATSAPP_ADAPTER_VERSION,
|
|
240
|
-
head: raw.head && typeof raw.head === "object" ? raw.head : {},
|
|
241
|
-
backfill:
|
|
242
|
-
raw.backfill && typeof raw.backfill === "object"
|
|
243
|
-
? {
|
|
244
|
-
complete: Boolean(raw.backfill.complete),
|
|
245
|
-
cursor_chat_jid: raw.backfill.cursor_chat_jid ?? null,
|
|
246
|
-
inventory: Array.isArray(raw.backfill.inventory)
|
|
247
|
-
? raw.backfill.inventory
|
|
248
|
-
: [],
|
|
249
|
-
chats:
|
|
250
|
-
raw.backfill.chats && typeof raw.backfill.chats === "object"
|
|
251
|
-
? raw.backfill.chats
|
|
252
|
-
: {},
|
|
253
|
-
}
|
|
254
|
-
: {
|
|
255
|
-
complete: false,
|
|
256
|
-
cursor_chat_jid: null,
|
|
257
|
-
inventory: [],
|
|
258
|
-
chats: {},
|
|
259
|
-
},
|
|
260
|
-
};
|
|
261
|
-
}
|
|
262
|
-
|
|
263
|
-
return {
|
|
264
|
-
schema: "owletto.whatsapp.browser.v1",
|
|
265
|
-
adapter_version: WHATSAPP_ADAPTER_VERSION,
|
|
266
|
-
cutover_unix_seconds: null,
|
|
267
|
-
head: {},
|
|
268
|
-
backfill: {
|
|
269
|
-
complete: false,
|
|
270
|
-
cursor_chat_jid: null,
|
|
271
|
-
inventory: [],
|
|
272
|
-
chats: {},
|
|
273
|
-
},
|
|
274
|
-
};
|
|
275
|
-
}
|
|
276
|
-
|
|
277
|
-
export function buildCollectionPlan(run: {
|
|
278
|
-
checkpoint?: Record<string, unknown> | null;
|
|
279
|
-
config?: Record<string, unknown> | null;
|
|
280
|
-
}): { checkpoint: BrowserCheckpoint; request: CollectRequest } {
|
|
281
|
-
const checkpoint = initializeBrowserCheckpoint(run?.checkpoint);
|
|
282
|
-
const headTimestamp = finiteSeconds(checkpoint.head?.timestamp);
|
|
283
|
-
const cutover = finiteSeconds(checkpoint.cutover_unix_seconds);
|
|
284
|
-
const configuredMax = Number(run?.config?.max_messages_per_sync);
|
|
285
|
-
const maxMessages = Number.isFinite(configuredMax)
|
|
286
|
-
? Math.max(1, Math.min(Math.trunc(configuredMax), MAX_MESSAGES_PER_RUN))
|
|
287
|
-
: MAX_MESSAGES_PER_RUN;
|
|
288
|
-
const configuredFilter = run?.config?.chat_filter;
|
|
289
|
-
const chatFilter: ChatFilter = (
|
|
290
|
-
["all", "individual", "group"] as const
|
|
291
|
-
).includes(configuredFilter as ChatFilter)
|
|
292
|
-
? (configuredFilter as ChatFilter)
|
|
293
|
-
: "all";
|
|
294
|
-
return {
|
|
295
|
-
checkpoint,
|
|
296
|
-
request: {
|
|
297
|
-
op: "collect",
|
|
298
|
-
adapter_version: WHATSAPP_ADAPTER_VERSION,
|
|
299
|
-
max_messages: maxMessages,
|
|
300
|
-
max_chats: MAX_CHATS_PER_RUN,
|
|
301
|
-
max_loads_per_chat: MAX_LOADS_PER_CHAT,
|
|
302
|
-
chat_filter: chatFilter,
|
|
303
|
-
recent_since:
|
|
304
|
-
headTimestamp == null
|
|
305
|
-
? null
|
|
306
|
-
: Math.max(0, headTimestamp - RECENT_OVERLAP_SECONDS),
|
|
307
|
-
// The cutover is the last second already ingested by the connection's
|
|
308
|
-
// previous source, so collect strictly after it. An inclusive floor would
|
|
309
|
-
// re-read that second on every run, which is what used to justify a
|
|
310
|
-
// second event shape and a media-download skip for the overlap.
|
|
311
|
-
minimum_timestamp: cutover == null ? null : cutover + 1,
|
|
312
|
-
backfill_disabled: cutover != null,
|
|
313
|
-
backfill: checkpoint.backfill,
|
|
314
|
-
budget_ms: COLLECT_BUDGET_MS,
|
|
315
|
-
},
|
|
316
|
-
};
|
|
317
|
-
}
|
|
318
|
-
|
|
319
96
|
function timestampOf(message: Record<string, any> | null | undefined): number {
|
|
320
97
|
const direct = finiteSeconds(message?.timestamp);
|
|
321
98
|
if (direct != null && direct > 0) return direct;
|
|
@@ -323,42 +100,6 @@ function timestampOf(message: Record<string, any> | null | undefined): number {
|
|
|
323
100
|
return Number.isFinite(parsed) ? Math.floor(parsed / 1000) : 0;
|
|
324
101
|
}
|
|
325
102
|
|
|
326
|
-
export function messageRevision(
|
|
327
|
-
message: Record<string, any> | null | undefined
|
|
328
|
-
): string {
|
|
329
|
-
const state = JSON.stringify({
|
|
330
|
-
id: message?.id ?? null,
|
|
331
|
-
body: message?.body ?? null,
|
|
332
|
-
caption: message?.caption ?? null,
|
|
333
|
-
timestamp: timestampOf(message),
|
|
334
|
-
message_type: message?.message_type ?? null,
|
|
335
|
-
media_kind: message?.media_kind ?? null,
|
|
336
|
-
media_type: message?.media_type ?? null,
|
|
337
|
-
chat_jid: message?.chat_jid ?? null,
|
|
338
|
-
sender_jid: message?.sender_jid ?? null,
|
|
339
|
-
participant: message?.participant ?? null,
|
|
340
|
-
push_name: message?.push_name ?? null,
|
|
341
|
-
from_me: Boolean(message?.from_me),
|
|
342
|
-
is_group: Boolean(message?.is_group),
|
|
343
|
-
edited: Boolean(message?.edited),
|
|
344
|
-
edit_timestamp: message?.edit_timestamp ?? null,
|
|
345
|
-
revoked: Boolean(message?.revoked),
|
|
346
|
-
reactions: [...(message?.reactions ?? [])].sort((a: unknown, b: unknown) =>
|
|
347
|
-
JSON.stringify(a).localeCompare(JSON.stringify(b))
|
|
348
|
-
),
|
|
349
|
-
quoted_id: message?.quoted_id ?? null,
|
|
350
|
-
is_forwarded: Boolean(message?.is_forwarded),
|
|
351
|
-
is_starred: Boolean(message?.is_starred),
|
|
352
|
-
is_system_event: Boolean(message?.is_system_event),
|
|
353
|
-
});
|
|
354
|
-
let hash = 2166136261;
|
|
355
|
-
for (let i = 0; i < state.length; i += 1) {
|
|
356
|
-
hash ^= state.charCodeAt(i);
|
|
357
|
-
hash = Math.imul(hash, 16777619);
|
|
358
|
-
}
|
|
359
|
-
return `${timestampOf(message)}-${(hash >>> 0).toString(16).padStart(8, "0")}`;
|
|
360
|
-
}
|
|
361
|
-
|
|
362
103
|
export function normalizeRelayedMessage(
|
|
363
104
|
message: unknown
|
|
364
105
|
): WhatsAppMessage | null {
|
|
@@ -377,89 +118,20 @@ export function normalizeRelayedMessage(
|
|
|
377
118
|
return null;
|
|
378
119
|
}
|
|
379
120
|
const senderJid = canonicalizeJid(raw.sender_jid);
|
|
380
|
-
const participant = canonicalizeJid(raw.participant);
|
|
381
121
|
const fromMe = Boolean(raw.from_me);
|
|
122
|
+
// Retired history-sync fields must not survive through the raw-field spread.
|
|
123
|
+
const { participant: _droppedParticipant, reactions: _droppedReactions, ...rest } = raw;
|
|
382
124
|
return {
|
|
383
|
-
...
|
|
125
|
+
...rest,
|
|
384
126
|
id,
|
|
385
127
|
chat_jid: chatJid,
|
|
386
128
|
sender_jid: fromMe ? null : senderJid,
|
|
387
|
-
participant: fromMe ? participant : null,
|
|
388
129
|
sender_phone: fromMe ? null : (raw.sender_phone ?? jidPhone(senderJid)),
|
|
389
130
|
push_name: fromMe ? null : raw.push_name,
|
|
390
|
-
is_group: chatJid.endsWith(
|
|
131
|
+
is_group: chatJid.endsWith(WHATSAPP_GROUP_SUFFIX),
|
|
391
132
|
from_me: fromMe,
|
|
392
133
|
timestamp,
|
|
393
134
|
occurred_at: new Date(timestamp * 1000).toISOString(),
|
|
394
|
-
reactions: Array.isArray(raw.reactions)
|
|
395
|
-
? [...raw.reactions].sort((a, b) =>
|
|
396
|
-
JSON.stringify(a).localeCompare(JSON.stringify(b))
|
|
397
|
-
)
|
|
398
|
-
: [],
|
|
399
|
-
};
|
|
400
|
-
}
|
|
401
|
-
|
|
402
|
-
/**
|
|
403
|
-
* Merge the freshly collected model with durable source observations. The
|
|
404
|
-
* ordering contract is unchanged: the freshly reconciled model is authoritative
|
|
405
|
-
* and a durable row may never replace current state.
|
|
406
|
-
*/
|
|
407
|
-
export function mergeCollectedMessages(
|
|
408
|
-
messages: unknown[] | null | undefined,
|
|
409
|
-
durable: Array<{ message?: unknown } | unknown> | null | undefined,
|
|
410
|
-
minimumTimestamp: number | null = null
|
|
411
|
-
): WhatsAppMessage[] {
|
|
412
|
-
const byId = new Map<string, WhatsAppMessage>();
|
|
413
|
-
const accept = (raw: unknown) => {
|
|
414
|
-
const message = normalizeRelayedMessage(raw);
|
|
415
|
-
if (!message) return;
|
|
416
|
-
if (minimumTimestamp != null && message.timestamp < minimumTimestamp)
|
|
417
|
-
return;
|
|
418
|
-
byId.set(message.id, message);
|
|
419
|
-
};
|
|
420
|
-
for (const entry of durable ?? [])
|
|
421
|
-
accept((entry as { message?: unknown })?.message ?? entry);
|
|
422
|
-
for (const message of messages ?? []) accept(message);
|
|
423
|
-
return [...byId.values()].sort(
|
|
424
|
-
(a, b) => a.timestamp - b.timestamp || a.id.localeCompare(b.id)
|
|
425
|
-
);
|
|
426
|
-
}
|
|
427
|
-
|
|
428
|
-
export function mergeBrowserCheckpoint(
|
|
429
|
-
base: Record<string, unknown> | null | undefined,
|
|
430
|
-
result: Partial<CollectResponse> | null | undefined,
|
|
431
|
-
messages: WhatsAppMessage[]
|
|
432
|
-
): BrowserCheckpoint {
|
|
433
|
-
const checkpoint = initializeBrowserCheckpoint(base);
|
|
434
|
-
let headTimestamp = finiteSeconds(checkpoint.head?.timestamp) ?? 0;
|
|
435
|
-
let headId = checkpoint.head?.id ?? null;
|
|
436
|
-
for (const message of messages) {
|
|
437
|
-
if (
|
|
438
|
-
message.timestamp > headTimestamp ||
|
|
439
|
-
(message.timestamp === headTimestamp && message.id > (headId ?? ""))
|
|
440
|
-
) {
|
|
441
|
-
headTimestamp = message.timestamp;
|
|
442
|
-
headId = message.id;
|
|
443
|
-
}
|
|
444
|
-
}
|
|
445
|
-
const updates = result?.backfill?.chats ?? {};
|
|
446
|
-
return {
|
|
447
|
-
...checkpoint,
|
|
448
|
-
head: { timestamp: headTimestamp, id: headId },
|
|
449
|
-
backfill: {
|
|
450
|
-
complete: Boolean(
|
|
451
|
-
result?.backfill?.complete ?? checkpoint.backfill.complete
|
|
452
|
-
),
|
|
453
|
-
cursor_chat_jid:
|
|
454
|
-
result?.backfill?.cursor_chat_jid ??
|
|
455
|
-
checkpoint.backfill.cursor_chat_jid ??
|
|
456
|
-
null,
|
|
457
|
-
inventory: Array.isArray(result?.backfill?.inventory)
|
|
458
|
-
? result.backfill.inventory
|
|
459
|
-
: checkpoint.backfill.inventory,
|
|
460
|
-
chats: { ...checkpoint.backfill.chats, ...updates },
|
|
461
|
-
},
|
|
462
|
-
last_run_at: new Date().toISOString(),
|
|
463
135
|
};
|
|
464
136
|
}
|
|
465
137
|
|
|
@@ -543,89 +215,3 @@ export function messageTitle(message: WhatsAppMessage): string {
|
|
|
543
215
|
return `${chatLabel}: ${message.push_name}`;
|
|
544
216
|
return chatLabel;
|
|
545
217
|
}
|
|
546
|
-
|
|
547
|
-
/**
|
|
548
|
-
* The extension's `toFeedItem`, retargeted onto `EventEnvelope`. Field mapping:
|
|
549
|
-
* `id` → `origin_id`, `payload_text`/`title`/`semantic_type`/`metadata`/
|
|
550
|
-
* `attachments` carry over unchanged, and `occurred_at` becomes a `Date`.
|
|
551
|
-
*/
|
|
552
|
-
export function toEventEnvelope(
|
|
553
|
-
message: WhatsAppMessage,
|
|
554
|
-
mediaResult:
|
|
555
|
-
| MediaRecord
|
|
556
|
-
| { status: MediaStatus; retryable?: boolean }
|
|
557
|
-
| undefined
|
|
558
|
-
): EventEnvelope {
|
|
559
|
-
const metadata: Record<string, unknown> = {
|
|
560
|
-
source: WHATSAPP_SOURCE,
|
|
561
|
-
origin_id: message.id,
|
|
562
|
-
chat_jid: message.chat_jid,
|
|
563
|
-
is_group: Boolean(message.is_group),
|
|
564
|
-
from_me: Boolean(message.from_me),
|
|
565
|
-
};
|
|
566
|
-
for (const [key, value] of Object.entries({
|
|
567
|
-
participant: message.participant,
|
|
568
|
-
sender_jid: message.sender_jid,
|
|
569
|
-
sender_phone: message.sender_phone,
|
|
570
|
-
push_name: message.push_name,
|
|
571
|
-
media_type: message.media_kind ?? message.media_type,
|
|
572
|
-
quoted_id: message.quoted_id,
|
|
573
|
-
edit_timestamp: message.edit_timestamp,
|
|
574
|
-
reactions:
|
|
575
|
-
Array.isArray(message.reactions) && message.reactions.length > 0
|
|
576
|
-
? message.reactions
|
|
577
|
-
: undefined,
|
|
578
|
-
})) {
|
|
579
|
-
if (value !== undefined && value !== null && value !== "")
|
|
580
|
-
metadata[key] = value;
|
|
581
|
-
}
|
|
582
|
-
if (message.is_forwarded) metadata.is_forwarded = true;
|
|
583
|
-
if (message.is_starred) metadata.is_starred = true;
|
|
584
|
-
if (message.is_system_event) metadata.is_system_event = true;
|
|
585
|
-
if (message.edited) metadata.edited = true;
|
|
586
|
-
if (message.revoked) metadata.revoked = true;
|
|
587
|
-
if (!message.from_me && !message.is_group) {
|
|
588
|
-
metadata.is_direct_inbound = true;
|
|
589
|
-
}
|
|
590
|
-
if (mediaResult?.status) metadata.media_status = mediaResult.status;
|
|
591
|
-
const mediaKind = message.media_kind ?? message.message_type;
|
|
592
|
-
if (
|
|
593
|
-
(mediaKind === "audio" || mediaKind === "ptt") &&
|
|
594
|
-
mediaResult?.status &&
|
|
595
|
-
mediaResult.status !== "downloaded"
|
|
596
|
-
) {
|
|
597
|
-
metadata.voice_note_skipped =
|
|
598
|
-
mediaResult.status === "too_large" ? "too_large" : "not_downloaded";
|
|
599
|
-
}
|
|
600
|
-
|
|
601
|
-
const attachment = (mediaResult as MediaRecord | undefined)?.attachment;
|
|
602
|
-
return {
|
|
603
|
-
origin_id: message.id,
|
|
604
|
-
origin_type: "message",
|
|
605
|
-
title: messageTitle(message),
|
|
606
|
-
payload_text: messagePayloadText(message),
|
|
607
|
-
occurred_at: new Date(message.occurred_at),
|
|
608
|
-
semantic_type: "message",
|
|
609
|
-
metadata,
|
|
610
|
-
...(attachment ? { attachments: [attachment] } : {}),
|
|
611
|
-
};
|
|
612
|
-
}
|
|
613
|
-
|
|
614
|
-
export function isMediaEligible(
|
|
615
|
-
message: Record<string, any> | null | undefined
|
|
616
|
-
): boolean {
|
|
617
|
-
const kind = message?.media_kind ?? message?.message_type;
|
|
618
|
-
const mime: string = message?.media_type ?? "";
|
|
619
|
-
return (
|
|
620
|
-
kind === "image" ||
|
|
621
|
-
kind === "video" ||
|
|
622
|
-
kind === "document" ||
|
|
623
|
-
kind === "sticker" ||
|
|
624
|
-
kind === "ptt" ||
|
|
625
|
-
kind === "audio" ||
|
|
626
|
-
mime.startsWith("image/") ||
|
|
627
|
-
mime.startsWith("video/") ||
|
|
628
|
-
mime.startsWith("audio/") ||
|
|
629
|
-
mime === "application/pdf"
|
|
630
|
-
);
|
|
631
|
-
}
|
|
@@ -29,9 +29,20 @@ export const WHATSAPP_WEB_IDENTITY = {
|
|
|
29
29
|
USER_JID: "whatsapp_user_jid",
|
|
30
30
|
} as const;
|
|
31
31
|
|
|
32
|
-
const
|
|
33
|
-
const
|
|
34
|
-
const
|
|
32
|
+
export const WHATSAPP_PHONE_SERVER = "s.whatsapp.net";
|
|
33
|
+
export const WHATSAPP_LEGACY_PHONE_SERVER = "c.us";
|
|
34
|
+
export const WHATSAPP_LID_SERVER = "lid";
|
|
35
|
+
/** Chat JID suffix for WhatsApp groups (a conversation, not a person). */
|
|
36
|
+
export const WHATSAPP_GROUP_SUFFIX = "@g.us";
|
|
37
|
+
/**
|
|
38
|
+
* Chat JID suffixes for 1:1 chats, including the web client's legacy phone
|
|
39
|
+
* spelling. Anything else (group, broadcast, newsletter) is not a DM.
|
|
40
|
+
*/
|
|
41
|
+
export const WHATSAPP_DM_SUFFIXES = [
|
|
42
|
+
"@s.whatsapp.net",
|
|
43
|
+
"@c.us",
|
|
44
|
+
"@lid",
|
|
45
|
+
] as const;
|
|
35
46
|
|
|
36
47
|
/** True when `value` is only ASCII digits, with a length in [min, max]. */
|
|
37
48
|
function isDigits(value: string, min: number, max: number): boolean {
|
|
@@ -67,10 +78,13 @@ export function normalizeWhatsAppUserJid(
|
|
|
67
78
|
const user = colon === -1 ? userPart : userPart.slice(0, colon);
|
|
68
79
|
if (colon !== -1 && !isDigits(userPart.slice(colon + 1), 1, 4)) return null;
|
|
69
80
|
if (!isDigits(user, 5, 32)) return null;
|
|
70
|
-
if (
|
|
71
|
-
|
|
81
|
+
if (
|
|
82
|
+
server === WHATSAPP_PHONE_SERVER ||
|
|
83
|
+
server === WHATSAPP_LEGACY_PHONE_SERVER
|
|
84
|
+
) {
|
|
85
|
+
return `${user}@${WHATSAPP_PHONE_SERVER}`;
|
|
72
86
|
}
|
|
73
|
-
if (server ===
|
|
87
|
+
if (server === WHATSAPP_LID_SERVER) return `${user}@${WHATSAPP_LID_SERVER}`;
|
|
74
88
|
return null;
|
|
75
89
|
}
|
|
76
90
|
|