@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.
Files changed (233) hide show
  1. package/dist/component.json +1 -1
  2. package/dist/dependencies.bun.lock +91 -83
  3. package/npm-shrinkwrap.json +492 -438
  4. package/package.json +12 -2
  5. package/vendor/cli/dist/bundled-skills/lobu/SKILL.md +1 -1
  6. package/vendor/cli/dist/commands/_lib/apply/apply-cmd.js +5 -6
  7. package/vendor/cli/dist/commands/_lib/apply/client.d.ts +1 -0
  8. package/vendor/cli/dist/commands/_lib/apply/client.js +26 -7
  9. package/vendor/cli/dist/commands/_lib/apply/desired-state.d.ts +9 -1
  10. package/vendor/cli/dist/commands/_lib/apply/diff.d.ts +1 -0
  11. package/vendor/cli/dist/commands/_lib/apply/diff.js +46 -4
  12. package/vendor/cli/dist/commands/_lib/apply/map-config.js +18 -38
  13. package/vendor/cli/dist/commands/_lib/init-from-org/bootstrap.js +5 -17
  14. package/vendor/cli/dist/commands/init.d.ts +0 -2
  15. package/vendor/cli/dist/commands/init.js +0 -27
  16. package/vendor/cli/dist/commands/login.d.ts +17 -0
  17. package/vendor/cli/dist/commands/login.js +43 -17
  18. package/vendor/cli/dist/commands/memory/_lib/errors.d.ts +0 -8
  19. package/vendor/cli/dist/commands/memory/_lib/errors.js +0 -12
  20. package/vendor/cli/dist/commands/memory/_lib/mcp.js +6 -6
  21. package/vendor/cli/dist/commands/memory/_lib/seed-cmd.js +4 -10
  22. package/vendor/cli/dist/config/define.d.ts +21 -15
  23. package/vendor/cli/dist/config/define.js +2 -2
  24. package/vendor/cli/dist/connectors/README.md +21 -6
  25. package/vendor/cli/dist/connectors/_google/actions.ts +270 -0
  26. package/vendor/cli/dist/connectors/_google/discovery/calendar_v3.json +1 -0
  27. package/vendor/cli/dist/connectors/_google/discovery/chat_v1.json +1 -0
  28. package/vendor/cli/dist/connectors/_google/discovery/docs_v1.json +1 -0
  29. package/vendor/cli/dist/connectors/_google/discovery/drive_v3.json +1 -0
  30. package/vendor/cli/dist/connectors/_google/discovery/gmail_v1.json +1 -0
  31. package/vendor/cli/dist/connectors/_google/discovery/people_v1.json +1 -0
  32. package/vendor/cli/dist/connectors/_google/discovery/sheets_v4.json +1 -0
  33. package/vendor/cli/dist/connectors/_google/discovery/tasks_v1.json +1 -0
  34. package/vendor/cli/dist/connectors/_google/discovery/youtube_v3.json +1 -0
  35. package/vendor/cli/dist/connectors/_google/discovery.ts +442 -0
  36. package/vendor/cli/dist/connectors/_google/errors.ts +91 -0
  37. package/vendor/cli/dist/connectors/_google/list-feed.ts +223 -0
  38. package/vendor/cli/dist/connectors/_google/policies.ts +127 -0
  39. package/vendor/cli/dist/connectors/agentid.ts +43 -0
  40. package/vendor/cli/dist/connectors/chat-user-identity.ts +2 -1
  41. package/vendor/cli/dist/connectors/github-automation-events.ts +9 -0
  42. package/vendor/cli/dist/connectors/github-identity.ts +28 -3
  43. package/vendor/cli/dist/connectors/github.ts +101 -59
  44. package/vendor/cli/dist/connectors/google_calendar.ts +207 -719
  45. package/vendor/cli/dist/connectors/google_drive.ts +16 -2
  46. package/vendor/cli/dist/connectors/google_gmail.ts +52 -33
  47. package/vendor/cli/dist/connectors/herdr.ts +406 -0
  48. package/vendor/cli/dist/connectors/jira-helpers.ts +313 -0
  49. package/vendor/cli/dist/connectors/jira.ts +35 -227
  50. package/vendor/cli/dist/connectors/linear.ts +7 -4
  51. package/vendor/cli/dist/connectors/linkedin-browser-script.ts +13 -0
  52. package/vendor/cli/dist/connectors/linkedin-draft-page.js +104 -0
  53. package/vendor/cli/dist/connectors/linkedin-drafts.ts +73 -0
  54. package/vendor/cli/dist/connectors/linkedin-identity.ts +32 -0
  55. package/vendor/cli/dist/connectors/linkedin-inbox-adapter.js +1185 -0
  56. package/vendor/cli/dist/connectors/linkedin-inbox.ts +156 -0
  57. package/vendor/cli/dist/connectors/linkedin-like.ts +114 -0
  58. package/vendor/cli/dist/connectors/linkedin-takeout.ts +577 -0
  59. package/vendor/cli/dist/connectors/linkedin.ts +3998 -0
  60. package/vendor/cli/dist/connectors/postgres.ts +27 -2
  61. package/vendor/cli/dist/connectors/takeout-utils.ts +240 -0
  62. package/vendor/cli/dist/connectors/tiktok-inbox-page.js +230 -0
  63. package/vendor/cli/dist/connectors/tiktok-web-page.js +217 -0
  64. package/vendor/cli/dist/connectors/tiktok_web.ts +319 -0
  65. package/vendor/cli/dist/connectors/whatsapp-web-adapter.js +285 -567
  66. package/vendor/cli/dist/connectors/whatsapp-web-helpers.ts +74 -455
  67. package/vendor/cli/dist/connectors/whatsapp-web-identity.ts +135 -0
  68. package/vendor/cli/dist/connectors/whatsapp_web.ts +159 -618
  69. package/vendor/cli/dist/connectors/x-dm-adapter.js +556 -0
  70. package/vendor/cli/dist/connectors/x-identity.ts +31 -2
  71. package/vendor/cli/dist/connectors/x.ts +539 -253
  72. package/vendor/cli/dist/index.js +1 -37
  73. package/vendor/cli/dist/internal/context.d.ts +10 -0
  74. package/vendor/cli/dist/internal/context.js +26 -16
  75. package/vendor/cli/dist/internal/node-version.d.ts +1 -1
  76. package/vendor/cli/dist/internal/node-version.js +1 -1
  77. package/vendor/cli/dist/internal/oauth.d.ts +3 -1
  78. package/vendor/cli/dist/internal/oauth.js +4 -3
  79. package/vendor/cli/dist/internal/runtime-components.d.ts +9 -0
  80. package/vendor/cli/dist/internal/runtime-components.js +67 -2
  81. package/vendor/cli/dist/mac-auth.js +17 -1
  82. package/vendor/cli/dist/providers.json +1 -1
  83. package/vendor/cli/dist/runtime-components.json +4 -4
  84. package/vendor/cli/dist/templates/AGENTS.md.tmpl +12 -5
  85. package/vendor/cli/dist/templates/README.md.tmpl +1 -1
  86. package/vendor/cli/dist/vendor/ora/.bun-tag-fde20e9a6df212fa +0 -0
  87. package/vendor/cli/dist/vendor/ora/index.d.ts +332 -0
  88. package/vendor/cli/dist/vendor/ora/index.js +424 -0
  89. package/vendor/cli/dist/vendor/ora/license +9 -0
  90. package/vendor/cli/dist/vendor/ora/package.json +64 -0
  91. package/vendor/cli/dist/vendor/ora/readme.md +329 -0
  92. package/vendor/cli/package.json +2 -2
  93. package/vendor/connector-sdk/dist/browser-handoff-banner.d.ts +39 -0
  94. package/vendor/connector-sdk/dist/browser-handoff-banner.js +175 -0
  95. package/vendor/connector-sdk/dist/browser-requirement.d.ts +36 -0
  96. package/vendor/connector-sdk/dist/browser-requirement.js +60 -0
  97. package/vendor/connector-sdk/dist/browser-source-read.d.ts +52 -0
  98. package/vendor/connector-sdk/dist/browser-source-read.js +107 -0
  99. package/vendor/connector-sdk/dist/connector-runtime.d.ts +3 -1
  100. package/vendor/connector-sdk/dist/connector-runtime.js +10 -0
  101. package/vendor/connector-sdk/dist/connector-types.d.ts +169 -45
  102. package/vendor/connector-sdk/dist/define-connector.d.ts +4 -2
  103. package/vendor/connector-sdk/dist/define-connector.js +3 -0
  104. package/vendor/connector-sdk/dist/index.d.ts +8 -2
  105. package/vendor/connector-sdk/dist/index.js +3 -0
  106. package/vendor/connector-sdk/dist/reaction-client-types.d.ts +16 -21
  107. package/vendor/connector-sdk/dist/reaction-client-types.typecheck.d.ts +5 -0
  108. package/vendor/connector-sdk/package.json +13 -1
  109. package/vendor/connector-worker/dist/agent-turn/guest-entry.js +43 -30
  110. package/vendor/connector-worker/dist/agent-turn/guest.bundle.js +416 -330
  111. package/vendor/connector-worker/dist/agent-turn/memory.d.ts +3 -2
  112. package/vendor/connector-worker/dist/agent-turn/memory.js +3 -20
  113. package/vendor/connector-worker/dist/agent-turn/types.d.ts +3 -15
  114. package/vendor/connector-worker/dist/agent-turn/workspace.d.ts +2 -2
  115. package/vendor/connector-worker/dist/agent-turn/workspace.js +2 -4
  116. package/vendor/connector-worker/dist/daemon/action-monitor.d.ts +12 -0
  117. package/vendor/connector-worker/dist/daemon/action-monitor.js +25 -0
  118. package/vendor/connector-worker/dist/daemon/agent-turn.js +0 -9
  119. package/vendor/connector-worker/dist/daemon/automation.d.ts +4 -11
  120. package/vendor/connector-worker/dist/daemon/automation.js +6 -6
  121. package/vendor/connector-worker/dist/daemon/builtins/index.d.ts +12 -0
  122. package/vendor/connector-worker/dist/daemon/builtins/index.js +61 -1
  123. package/vendor/connector-worker/dist/daemon/builtins/os-shell.d.ts +2 -1
  124. package/vendor/connector-worker/dist/daemon/builtins/os-shell.js +40 -27
  125. package/vendor/connector-worker/dist/daemon/client.d.ts +6 -4
  126. package/vendor/connector-worker/dist/daemon/client.js +3 -3
  127. package/vendor/connector-worker/dist/daemon/device-concurrency.d.ts +8 -0
  128. package/vendor/connector-worker/dist/daemon/device-concurrency.js +13 -0
  129. package/vendor/connector-worker/dist/daemon/executor.d.ts +1 -1
  130. package/vendor/connector-worker/dist/daemon/executor.js +48 -120
  131. package/vendor/connector-worker/dist/daemon/log.d.ts +2 -0
  132. package/vendor/connector-worker/dist/daemon/log.js +23 -2
  133. package/vendor/connector-worker/dist/daemon/mac-device-daemon.d.ts +23 -6
  134. package/vendor/connector-worker/dist/daemon/mac-device-daemon.js +57 -23
  135. package/vendor/connector-worker/dist/daemon/native-bridge/client.d.ts +6 -0
  136. package/vendor/connector-worker/dist/daemon/native-bridge/client.js +15 -0
  137. package/vendor/connector-worker/dist/daemon/native-bridge/executor.js +29 -25
  138. package/vendor/connector-worker/dist/daemon/native-bridge/protocol.d.ts +1 -1
  139. package/vendor/connector-worker/dist/daemon/native-bridge/protocol.js +1 -0
  140. package/vendor/connector-worker/dist/daemon/poll-loop.d.ts +3 -0
  141. package/vendor/connector-worker/dist/daemon/poll-loop.js +5 -1
  142. package/vendor/connector-worker/dist/daemon/start.d.ts +1 -0
  143. package/vendor/connector-worker/dist/daemon/start.js +15 -20
  144. package/vendor/connector-worker/dist/daemon/terminal-failure.js +0 -11
  145. package/vendor/connector-worker/dist/daemon/worker-id.d.ts +17 -0
  146. package/vendor/connector-worker/dist/daemon/worker-id.js +22 -0
  147. package/vendor/connector-worker/dist/executor/interface.d.ts +34 -2
  148. package/vendor/connector-worker/dist/executor/isolate.js +66 -15
  149. package/vendor/connector-worker/dist/executor/redact.d.ts +1 -0
  150. package/vendor/connector-worker/dist/executor/redact.js +97 -26
  151. package/vendor/connector-worker/dist/executor/runtime.d.ts +7 -0
  152. package/vendor/connector-worker/dist/executor/runtime.js +26 -0
  153. package/vendor/connector-worker/dist/isolate/bridge.js +3 -1
  154. package/vendor/connector-worker/dist/mac-device-daemon.js +24 -29
  155. package/vendor/connector-worker/package.json +2 -2
  156. package/vendor/core/dist/agent-policy.js +1 -1
  157. package/vendor/core/dist/agent-store.d.ts +2 -0
  158. package/vendor/core/dist/agent-tooling.d.ts +1 -1
  159. package/vendor/core/dist/agent-tooling.js +1 -1
  160. package/vendor/core/dist/capabilities.d.ts +1 -1
  161. package/vendor/core/dist/capabilities.js +2 -1
  162. package/vendor/core/dist/connector-query-errors.d.ts +32 -0
  163. package/vendor/core/dist/connector-query-errors.js +41 -1
  164. package/vendor/core/dist/contracts/agent-settings.d.ts +0 -11
  165. package/vendor/core/dist/contracts/agent-settings.js +1 -7
  166. package/vendor/core/dist/contracts/tools/collection-presentation.d.ts +93 -0
  167. package/vendor/core/dist/contracts/tools/collection-presentation.js +88 -0
  168. package/vendor/core/dist/contracts/tools/collection-selection.d.ts +47 -0
  169. package/vendor/core/dist/contracts/tools/collection-selection.js +74 -0
  170. package/vendor/core/dist/contracts/tools/manage-agents.d.ts +10 -4
  171. package/vendor/core/dist/contracts/tools/manage-auth-profiles.d.ts +1 -1
  172. package/vendor/core/dist/contracts/tools/manage-auth-profiles.js +5 -1
  173. package/vendor/core/dist/contracts/tools/manage-automations.d.ts +19 -4
  174. package/vendor/core/dist/contracts/tools/manage-automations.js +25 -8
  175. package/vendor/core/dist/contracts/tools/manage-connections.d.ts +1 -0
  176. package/vendor/core/dist/contracts/tools/manage-connections.js +3 -0
  177. package/vendor/core/dist/contracts/tools/manage-conversations.js +2 -2
  178. package/vendor/core/dist/contracts/tools/manage-entity-schema.d.ts +11 -0
  179. package/vendor/core/dist/contracts/tools/manage-entity-schema.js +42 -3
  180. package/vendor/core/dist/contracts/tools/manage-entity.d.ts +181 -152
  181. package/vendor/core/dist/contracts/tools/manage-entity.js +118 -162
  182. package/vendor/core/dist/contracts/tools/manage-feeds.d.ts +14 -0
  183. package/vendor/core/dist/contracts/tools/manage-feeds.js +17 -0
  184. package/vendor/core/dist/contracts/tools/manage-operations.d.ts +41 -3
  185. package/vendor/core/dist/contracts/tools/manage-operations.js +40 -2
  186. package/vendor/core/dist/contracts/tools/manage-views.d.ts +182 -7
  187. package/vendor/core/dist/contracts/tools/manage-views.js +49 -13
  188. package/vendor/core/dist/contracts/tools/read-knowledge.d.ts +11 -2
  189. package/vendor/core/dist/contracts/tools/read-knowledge.js +23 -3
  190. package/vendor/core/dist/contracts/tools/save-memory.d.ts +3 -6
  191. package/vendor/core/dist/contracts/tools/save-memory.js +2 -6
  192. package/vendor/core/dist/contracts/tools/view-attach.d.ts +2 -1
  193. package/vendor/core/dist/contracts/tools/view-attach.js +7 -2
  194. package/vendor/core/dist/contracts/worker/protocol.d.ts +130 -159
  195. package/vendor/core/dist/contracts/worker/protocol.js +88 -76
  196. package/vendor/core/dist/device-pin-tombstones.d.ts +44 -0
  197. package/vendor/core/dist/device-pin-tombstones.js +57 -0
  198. package/vendor/core/dist/index.d.ts +5 -5
  199. package/vendor/core/dist/index.js +4 -6
  200. package/vendor/core/dist/json-template.d.ts +6 -10
  201. package/vendor/core/dist/json-template.js +5 -9
  202. package/vendor/core/dist/logger.js +17 -26
  203. package/vendor/core/dist/shell-safety.d.ts +9 -0
  204. package/vendor/core/dist/shell-safety.js +154 -0
  205. package/vendor/core/dist/suggestions.d.ts +1 -1
  206. package/vendor/core/dist/suggestions.js +1 -1
  207. package/vendor/core/dist/tool-trace-summary.d.ts +1 -1
  208. package/vendor/core/dist/tool-trace-summary.js +1 -1
  209. package/vendor/core/dist/types.d.ts +1 -3
  210. package/vendor/core/dist/utils/diagnostic-scrubber.d.ts +7 -0
  211. package/vendor/core/dist/utils/diagnostic-scrubber.js +437 -0
  212. package/vendor/core/dist/worker/auth.d.ts +44 -6
  213. package/vendor/core/dist/worker/auth.js +44 -11
  214. package/vendor/core/dist/worker/wire.d.ts +0 -10
  215. package/vendor/core/package.json +20 -11
  216. package/vendor/ora/.bun-tag-fde20e9a6df212fa +0 -0
  217. package/vendor/ora/index.d.ts +332 -0
  218. package/vendor/ora/index.js +424 -0
  219. package/vendor/ora/license +9 -0
  220. package/vendor/ora/package.json +53 -0
  221. package/vendor/ora/readme.md +329 -0
  222. package/vendor/cli/dist/commands/telemetry.d.ts +0 -10
  223. package/vendor/cli/dist/commands/telemetry.js +0 -66
  224. package/vendor/connector-worker/dist/daemon/device-chat.d.ts +0 -9
  225. package/vendor/connector-worker/dist/daemon/device-chat.js +0 -61
  226. package/vendor/core/dist/contracts/worker/device-chat.d.ts +0 -4
  227. package/vendor/core/dist/contracts/worker/device-chat.js +0 -26
  228. package/vendor/core/dist/sentry.d.ts +0 -26
  229. package/vendor/core/dist/sentry.js +0 -133
  230. package/vendor/core/dist/tool-policy.d.ts +0 -74
  231. package/vendor/core/dist/tool-policy.js +0 -305
  232. package/vendor/core/dist/utils/sentry-scrubber.d.ts +0 -29
  233. package/vendor/core/dist/utils/sentry-scrubber.js +0 -228
@@ -1,74 +1,44 @@
1
1
  /**
2
2
  * Transport-neutral helpers for the WhatsApp Web connector.
3
3
  *
4
- * Ported from the Owletto extension's `whatsapp-web-adapter-v1.js`, which ran
5
- * these same functions in the extension service worker. They are deliberately
6
- * free of WhatsApp private APIs — the unstable MAIN-world surface lives in
7
- * `whatsapp-web-adapter.js` and is injected into the page. Everything here runs
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 type { EventEnvelope } from "@lobu/connector-sdk";
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 = 14;
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,119 +48,9 @@ export interface WhatsAppMessage {
78
48
  [key: string]: unknown;
79
49
  }
80
50
 
81
- export interface BackfillChatState {
82
- oldest_timestamp?: number | null;
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
- /** Exact buffered revisions included in the last successful sync. */
110
- source_ack?: {
111
- binding_id: string;
112
- epoch: string;
113
- records: Array<{ id: string; revision: number }>;
114
- };
115
- }
116
-
117
- export interface DirtyMarker {
118
- key: string;
119
- chat_jid?: string | null;
120
- message_id?: string | null;
121
- minimum_timestamp?: number | null;
122
- reason?: string;
123
- }
124
-
125
- export type MediaStatus =
126
- | "downloaded"
127
- | "awaiting_primary_device"
128
- | "view_once"
129
- | "expired"
130
- | "too_large"
131
- | "timeout_retryable"
132
- | "metadata_only"
133
- | "unavailable";
134
-
135
- export interface MediaAttachment {
136
- kind: string;
137
- filename: string;
138
- mime_type: string;
139
- data: string;
140
- size_bytes: number;
141
- }
142
-
143
- export interface MediaRecord {
144
- id: string;
145
- revision: string;
146
- status: MediaStatus;
147
- raw_status?: string;
148
- retryable: boolean;
149
- attachment?: MediaAttachment;
150
- mime_type?: string | null;
151
- attempts: number;
152
- next_attempt_at: number | null;
153
- updated_at: number;
154
- }
155
-
156
- export interface CollectRequest {
157
- op: "collect";
158
- adapter_version: number;
159
- max_messages: number;
160
- max_chats: number;
161
- max_loads_per_chat: number;
162
- chat_filter: ChatFilter;
163
- recent_since: number | null;
164
- minimum_timestamp: number | null;
165
- backfill_disabled: boolean;
166
- backfill: BrowserCheckpoint["backfill"];
167
- dirty_ranges?: Array<Omit<DirtyMarker, "reason">>;
168
- /**
169
- * How long the page may spend paging history, in milliseconds. Relative, not
170
- * an absolute instant: the connector and the page run on different machines,
171
- * so the page resolves this against its own clock.
172
- */
173
- budget_ms?: number | null;
174
- }
175
-
176
- export interface CollectResponse {
177
- ok: true;
178
- status: Record<string, unknown>;
179
- messages: unknown[];
180
- history_pages?: Array<
181
- {
182
- chat_jid: string;
183
- messages: unknown[];
184
- } & BackfillChatState
185
- >;
186
- quarantined?: DirtyMarker[];
187
- dirty_reconciled?: Array<{ key: string; message_id: string }>;
188
- backfill: {
189
- complete: boolean;
190
- cursor_chat_jid: string | null;
191
- inventory: string[];
192
- chats: Record<string, BackfillChatState>;
193
- };
51
+ /** Reference-only observation position in WhatsApp's own local message store. */
52
+ export interface ObservationCheckpoint {
53
+ observation?: { after: number; started_at: number; chat_filter: ChatFilter };
194
54
  }
195
55
 
196
56
  export function rawMessageId(value: unknown): string | null {
@@ -209,17 +69,23 @@ export function rawMessageId(value: unknown): string | null {
209
69
 
210
70
  export function canonicalizeJid(value: unknown): string | null {
211
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;
212
77
  const jid = value.trim().toLowerCase();
213
78
  if (!jid?.includes("@")) return null;
214
- if (jid.endsWith("@c.us")) return `${jid.slice(0, -5)}@s.whatsapp.net`;
79
+ if (jid.endsWith(`@${WHATSAPP_LEGACY_PHONE_SERVER}`))
80
+ return `${jid.slice(0, -WHATSAPP_LEGACY_PHONE_SERVER.length - 1)}@${WHATSAPP_PHONE_SERVER}`;
215
81
  return jid;
216
82
  }
217
83
 
218
84
  function jidPhone(jid: unknown): string | null {
219
- const canonical = canonicalizeJid(jid);
220
- if (!canonical?.endsWith("@s.whatsapp.net")) return null;
221
- const digits = canonical.slice(0, -"@s.whatsapp.net".length);
222
- return /^\d+$/.test(digits) ? digits : null;
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);
223
89
  }
224
90
 
225
91
  function finiteSeconds(value: unknown): number | null {
@@ -227,93 +93,6 @@ function finiteSeconds(value: unknown): number | null {
227
93
  return Number.isFinite(seconds) && seconds >= 0 ? seconds : null;
228
94
  }
229
95
 
230
- export function initializeBrowserCheckpoint(
231
- checkpoint: Record<string, unknown> | null | undefined
232
- ): BrowserCheckpoint {
233
- const raw = (checkpoint ?? {}) as Record<string, any>;
234
- if (raw.schema === "owletto.whatsapp.browser.v1") {
235
- return {
236
- ...(raw as BrowserCheckpoint),
237
- adapter_version: WHATSAPP_ADAPTER_VERSION,
238
- head: raw.head && typeof raw.head === "object" ? raw.head : {},
239
- backfill:
240
- raw.backfill && typeof raw.backfill === "object"
241
- ? {
242
- complete: Boolean(raw.backfill.complete),
243
- cursor_chat_jid: raw.backfill.cursor_chat_jid ?? null,
244
- inventory: Array.isArray(raw.backfill.inventory)
245
- ? raw.backfill.inventory
246
- : [],
247
- chats:
248
- raw.backfill.chats && typeof raw.backfill.chats === "object"
249
- ? raw.backfill.chats
250
- : {},
251
- }
252
- : {
253
- complete: false,
254
- cursor_chat_jid: null,
255
- inventory: [],
256
- chats: {},
257
- },
258
- };
259
- }
260
-
261
- return {
262
- schema: "owletto.whatsapp.browser.v1",
263
- adapter_version: WHATSAPP_ADAPTER_VERSION,
264
- cutover_unix_seconds: null,
265
- head: {},
266
- backfill: {
267
- complete: false,
268
- cursor_chat_jid: null,
269
- inventory: [],
270
- chats: {},
271
- },
272
- };
273
- }
274
-
275
- export function buildCollectionPlan(run: {
276
- checkpoint?: Record<string, unknown> | null;
277
- config?: Record<string, unknown> | null;
278
- }): { checkpoint: BrowserCheckpoint; request: CollectRequest } {
279
- const checkpoint = initializeBrowserCheckpoint(run?.checkpoint);
280
- const headTimestamp = finiteSeconds(checkpoint.head?.timestamp);
281
- const cutover = finiteSeconds(checkpoint.cutover_unix_seconds);
282
- const configuredMax = Number(run?.config?.max_messages_per_sync);
283
- const maxMessages = Number.isFinite(configuredMax)
284
- ? Math.max(1, Math.min(Math.trunc(configuredMax), MAX_MESSAGES_PER_RUN))
285
- : MAX_MESSAGES_PER_RUN;
286
- const configuredFilter = run?.config?.chat_filter;
287
- const chatFilter: ChatFilter = (
288
- ["all", "individual", "group"] as const
289
- ).includes(configuredFilter as ChatFilter)
290
- ? (configuredFilter as ChatFilter)
291
- : "all";
292
- return {
293
- checkpoint,
294
- request: {
295
- op: "collect",
296
- adapter_version: WHATSAPP_ADAPTER_VERSION,
297
- max_messages: maxMessages,
298
- max_chats: MAX_CHATS_PER_RUN,
299
- max_loads_per_chat: MAX_LOADS_PER_CHAT,
300
- chat_filter: chatFilter,
301
- recent_since:
302
- headTimestamp == null
303
- ? null
304
- : Math.max(0, headTimestamp - RECENT_OVERLAP_SECONDS),
305
- // The cutover is the last second already ingested by the connection's
306
- // previous source, so collect strictly after it. An inclusive floor would
307
- // re-read that second on every run, which is what used to justify a
308
- // second event shape and a media-download skip for the overlap.
309
- minimum_timestamp: cutover == null ? null : cutover + 1,
310
- backfill_disabled: cutover != null,
311
- backfill: checkpoint.backfill,
312
- budget_ms: COLLECT_BUDGET_MS,
313
- },
314
- };
315
- }
316
-
317
96
  function timestampOf(message: Record<string, any> | null | undefined): number {
318
97
  const direct = finiteSeconds(message?.timestamp);
319
98
  if (direct != null && direct > 0) return direct;
@@ -321,42 +100,6 @@ function timestampOf(message: Record<string, any> | null | undefined): number {
321
100
  return Number.isFinite(parsed) ? Math.floor(parsed / 1000) : 0;
322
101
  }
323
102
 
324
- export function messageRevision(
325
- message: Record<string, any> | null | undefined
326
- ): string {
327
- const state = JSON.stringify({
328
- id: message?.id ?? null,
329
- body: message?.body ?? null,
330
- caption: message?.caption ?? null,
331
- timestamp: timestampOf(message),
332
- message_type: message?.message_type ?? null,
333
- media_kind: message?.media_kind ?? null,
334
- media_type: message?.media_type ?? null,
335
- chat_jid: message?.chat_jid ?? null,
336
- sender_jid: message?.sender_jid ?? null,
337
- participant: message?.participant ?? null,
338
- push_name: message?.push_name ?? null,
339
- from_me: Boolean(message?.from_me),
340
- is_group: Boolean(message?.is_group),
341
- edited: Boolean(message?.edited),
342
- edit_timestamp: message?.edit_timestamp ?? null,
343
- revoked: Boolean(message?.revoked),
344
- reactions: [...(message?.reactions ?? [])].sort((a: unknown, b: unknown) =>
345
- JSON.stringify(a).localeCompare(JSON.stringify(b))
346
- ),
347
- quoted_id: message?.quoted_id ?? null,
348
- is_forwarded: Boolean(message?.is_forwarded),
349
- is_starred: Boolean(message?.is_starred),
350
- is_system_event: Boolean(message?.is_system_event),
351
- });
352
- let hash = 2166136261;
353
- for (let i = 0; i < state.length; i += 1) {
354
- hash ^= state.charCodeAt(i);
355
- hash = Math.imul(hash, 16777619);
356
- }
357
- return `${timestampOf(message)}-${(hash >>> 0).toString(16).padStart(8, "0")}`;
358
- }
359
-
360
103
  export function normalizeRelayedMessage(
361
104
  message: unknown
362
105
  ): WhatsAppMessage | null {
@@ -375,89 +118,20 @@ export function normalizeRelayedMessage(
375
118
  return null;
376
119
  }
377
120
  const senderJid = canonicalizeJid(raw.sender_jid);
378
- const participant = canonicalizeJid(raw.participant);
379
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;
380
124
  return {
381
- ...raw,
125
+ ...rest,
382
126
  id,
383
127
  chat_jid: chatJid,
384
128
  sender_jid: fromMe ? null : senderJid,
385
- participant: fromMe ? participant : null,
386
129
  sender_phone: fromMe ? null : (raw.sender_phone ?? jidPhone(senderJid)),
387
130
  push_name: fromMe ? null : raw.push_name,
388
- is_group: chatJid.endsWith("@g.us"),
131
+ is_group: chatJid.endsWith(WHATSAPP_GROUP_SUFFIX),
389
132
  from_me: fromMe,
390
133
  timestamp,
391
134
  occurred_at: new Date(timestamp * 1000).toISOString(),
392
- reactions: Array.isArray(raw.reactions)
393
- ? [...raw.reactions].sort((a, b) =>
394
- JSON.stringify(a).localeCompare(JSON.stringify(b))
395
- )
396
- : [],
397
- };
398
- }
399
-
400
- /**
401
- * Merge the freshly collected model with durable source observations. The
402
- * ordering contract is unchanged: the freshly reconciled model is authoritative
403
- * and a durable row may never replace current state.
404
- */
405
- export function mergeCollectedMessages(
406
- messages: unknown[] | null | undefined,
407
- durable: Array<{ message?: unknown } | unknown> | null | undefined,
408
- minimumTimestamp: number | null = null
409
- ): WhatsAppMessage[] {
410
- const byId = new Map<string, WhatsAppMessage>();
411
- const accept = (raw: unknown) => {
412
- const message = normalizeRelayedMessage(raw);
413
- if (!message) return;
414
- if (minimumTimestamp != null && message.timestamp < minimumTimestamp)
415
- return;
416
- byId.set(message.id, message);
417
- };
418
- for (const entry of durable ?? [])
419
- accept((entry as { message?: unknown })?.message ?? entry);
420
- for (const message of messages ?? []) accept(message);
421
- return [...byId.values()].sort(
422
- (a, b) => a.timestamp - b.timestamp || a.id.localeCompare(b.id)
423
- );
424
- }
425
-
426
- export function mergeBrowserCheckpoint(
427
- base: Record<string, unknown> | null | undefined,
428
- result: Partial<CollectResponse> | null | undefined,
429
- messages: WhatsAppMessage[]
430
- ): BrowserCheckpoint {
431
- const checkpoint = initializeBrowserCheckpoint(base);
432
- let headTimestamp = finiteSeconds(checkpoint.head?.timestamp) ?? 0;
433
- let headId = checkpoint.head?.id ?? null;
434
- for (const message of messages) {
435
- if (
436
- message.timestamp > headTimestamp ||
437
- (message.timestamp === headTimestamp && message.id > (headId ?? ""))
438
- ) {
439
- headTimestamp = message.timestamp;
440
- headId = message.id;
441
- }
442
- }
443
- const updates = result?.backfill?.chats ?? {};
444
- return {
445
- ...checkpoint,
446
- head: { timestamp: headTimestamp, id: headId },
447
- backfill: {
448
- complete: Boolean(
449
- result?.backfill?.complete ?? checkpoint.backfill.complete
450
- ),
451
- cursor_chat_jid:
452
- result?.backfill?.cursor_chat_jid ??
453
- checkpoint.backfill.cursor_chat_jid ??
454
- null,
455
- inventory: Array.isArray(result?.backfill?.inventory)
456
- ? result.backfill.inventory
457
- : checkpoint.backfill.inventory,
458
- chats: { ...checkpoint.backfill.chats, ...updates },
459
- },
460
- last_run_at: new Date().toISOString(),
461
135
  };
462
136
  }
463
137
 
@@ -479,120 +153,65 @@ function isInlineThumbnail(body: string): boolean {
479
153
  );
480
154
  }
481
155
 
482
- export function messagePayloadText(message: WhatsAppMessage): string {
156
+ const MEDIA_PLACEHOLDERS: Record<string, string> = {
157
+ ptt: "[voice note]",
158
+ audio: "[voice note]",
159
+ image: "[image]",
160
+ video: "[video]",
161
+ document: "[document]",
162
+ sticker: "[sticker]",
163
+ };
164
+
165
+ function messageBodyText(message: WhatsAppMessage): string {
483
166
  const body =
484
167
  typeof message.body === "string" &&
485
168
  message.body.length > 0 &&
486
169
  !isInlineThumbnail(message.body)
487
170
  ? message.body
488
171
  : "";
489
- const text =
172
+ return (
490
173
  body ||
491
174
  (typeof message.caption === "string" && message.caption.length > 0
492
175
  ? message.caption
493
- : "");
494
- if (message.revoked) return "[revoked message]";
495
- if (text) return text;
496
- if (message.is_system_event) return "[system event]";
497
- const kind = message.media_kind ?? message.media_type ?? message.message_type;
498
- if (kind === "ptt" || kind === "audio") return "[voice note]";
499
- if (kind === "image") return "[image]";
500
- if (kind === "video") return "[video]";
501
- if (kind === "document") return "[document]";
502
- if (kind === "sticker") return "[sticker]";
503
- return "[system event]";
176
+ : "")
177
+ );
504
178
  }
505
179
 
506
- export function messageTitle(message: WhatsAppMessage): string {
507
- const chatLabel = message.chat_name || message.chat_jid || "WhatsApp";
508
- if (message.from_me) return `→ ${chatLabel}`;
509
- if (message.is_group && message.push_name)
510
- return `${chatLabel}: ${message.push_name}`;
511
- return chatLabel;
180
+ function mediaKind(message: WhatsAppMessage): string {
181
+ return String(message.media_kind ?? message.media_type ?? message.message_type ?? "");
512
182
  }
513
183
 
514
184
  /**
515
- * The extension's `toFeedItem`, retargeted onto `EventEnvelope`. Field mapping:
516
- * `id` → `origin_id`, `payload_text`/`title`/`semantic_type`/`metadata`/
517
- * `attachments` carry over unchanged, and `occurred_at` becomes a `Date`.
185
+ * What a message carries, decided from its fields rather than from the
186
+ * placeholder text `messagePayloadText` renders for it: readers that must
187
+ * not answer a revocation or a bare image key on this.
518
188
  */
519
- export function toEventEnvelope(
520
- message: WhatsAppMessage,
521
- mediaResult:
522
- | MediaRecord
523
- | { status: MediaStatus; retryable?: boolean }
524
- | undefined
525
- ): EventEnvelope {
526
- const metadata: Record<string, unknown> = {
527
- source: WHATSAPP_SOURCE,
528
- origin_id: message.id,
529
- chat_jid: message.chat_jid,
530
- is_group: Boolean(message.is_group),
531
- from_me: Boolean(message.from_me),
532
- };
533
- for (const [key, value] of Object.entries({
534
- participant: message.participant,
535
- sender_jid: message.sender_jid,
536
- sender_phone: message.sender_phone,
537
- push_name: message.push_name,
538
- media_type: message.media_kind ?? message.media_type,
539
- quoted_id: message.quoted_id,
540
- edit_timestamp: message.edit_timestamp,
541
- reactions:
542
- Array.isArray(message.reactions) && message.reactions.length > 0
543
- ? message.reactions
544
- : undefined,
545
- })) {
546
- if (value !== undefined && value !== null && value !== "")
547
- metadata[key] = value;
548
- }
549
- if (message.is_forwarded) metadata.is_forwarded = true;
550
- if (message.is_starred) metadata.is_starred = true;
551
- if (message.is_system_event) metadata.is_system_event = true;
552
- if (message.edited) metadata.edited = true;
553
- if (message.revoked) metadata.revoked = true;
554
- if (!message.from_me && !message.is_group) {
555
- metadata.is_direct_inbound = true;
556
- }
557
- if (mediaResult?.status) metadata.media_status = mediaResult.status;
558
- const mediaKind = message.media_kind ?? message.message_type;
559
- if (
560
- (mediaKind === "audio" || mediaKind === "ptt") &&
561
- mediaResult?.status &&
562
- mediaResult.status !== "downloaded"
563
- ) {
564
- metadata.voice_note_skipped =
565
- mediaResult.status === "too_large" ? "too_large" : "not_downloaded";
566
- }
189
+ export type MessageContentKind = "text" | "media" | "revoked" | "system";
567
190
 
568
- const attachment = (mediaResult as MediaRecord | undefined)?.attachment;
569
- return {
570
- origin_id: message.id,
571
- origin_type: "message",
572
- title: messageTitle(message),
573
- payload_text: messagePayloadText(message),
574
- occurred_at: new Date(message.occurred_at),
575
- semantic_type: "message",
576
- metadata,
577
- ...(attachment ? { attachments: [attachment] } : {}),
578
- };
191
+ export function messageContentKind(message: WhatsAppMessage): MessageContentKind {
192
+ if (message.revoked) return "revoked";
193
+ if (message.is_system_event) return "system";
194
+ if (messageBodyText(message)) return "text";
195
+ return Object.hasOwn(MEDIA_PLACEHOLDERS, mediaKind(message)) ? "media" : "system";
579
196
  }
580
197
 
581
- export function isMediaEligible(
582
- message: Record<string, any> | null | undefined
583
- ): boolean {
584
- const kind = message?.media_kind ?? message?.message_type;
585
- const mime: string = message?.media_type ?? "";
586
- return (
587
- kind === "image" ||
588
- kind === "video" ||
589
- kind === "document" ||
590
- kind === "sticker" ||
591
- kind === "ptt" ||
592
- kind === "audio" ||
593
- mime.startsWith("image/") ||
594
- mime.startsWith("video/") ||
595
- mime.startsWith("audio/") ||
596
- mime === "application/pdf"
597
- );
198
+ export function messagePayloadText(message: WhatsAppMessage): string {
199
+ switch (messageContentKind(message)) {
200
+ case "revoked":
201
+ return "[revoked message]";
202
+ case "text":
203
+ return messageBodyText(message);
204
+ case "media":
205
+ return MEDIA_PLACEHOLDERS[mediaKind(message)];
206
+ default:
207
+ return messageBodyText(message) || "[system event]";
208
+ }
209
+ }
210
+
211
+ export function messageTitle(message: WhatsAppMessage): string {
212
+ const chatLabel = message.chat_name || message.chat_jid || "WhatsApp";
213
+ if (message.from_me) return `→ ${chatLabel}`;
214
+ if (message.is_group && message.push_name)
215
+ return `${chatLabel}: ${message.push_name}`;
216
+ return chatLabel;
598
217
  }