@lobu/runtime-device 21.2.1 → 22.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 (196) 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/commands/_lib/apply/apply-cmd.js +5 -6
  6. package/vendor/cli/dist/commands/_lib/apply/client.d.ts +1 -0
  7. package/vendor/cli/dist/commands/_lib/apply/client.js +26 -7
  8. package/vendor/cli/dist/commands/_lib/apply/desired-state.d.ts +9 -1
  9. package/vendor/cli/dist/commands/_lib/apply/diff.d.ts +1 -0
  10. package/vendor/cli/dist/commands/_lib/apply/diff.js +46 -4
  11. package/vendor/cli/dist/commands/_lib/apply/map-config.js +18 -38
  12. package/vendor/cli/dist/commands/_lib/init-from-org/bootstrap.js +5 -17
  13. package/vendor/cli/dist/commands/init.d.ts +0 -2
  14. package/vendor/cli/dist/commands/init.js +0 -27
  15. package/vendor/cli/dist/commands/login.d.ts +17 -0
  16. package/vendor/cli/dist/commands/login.js +43 -17
  17. package/vendor/cli/dist/commands/memory/_lib/errors.d.ts +0 -8
  18. package/vendor/cli/dist/commands/memory/_lib/errors.js +0 -12
  19. package/vendor/cli/dist/commands/memory/_lib/mcp.js +6 -6
  20. package/vendor/cli/dist/commands/memory/_lib/seed-cmd.js +4 -10
  21. package/vendor/cli/dist/config/define.d.ts +21 -15
  22. package/vendor/cli/dist/config/define.js +2 -2
  23. package/vendor/cli/dist/connectors/README.md +7 -5
  24. package/vendor/cli/dist/connectors/_google/actions.ts +270 -0
  25. package/vendor/cli/dist/connectors/_google/discovery/calendar_v3.json +1 -0
  26. package/vendor/cli/dist/connectors/_google/discovery/chat_v1.json +1 -0
  27. package/vendor/cli/dist/connectors/_google/discovery/docs_v1.json +1 -0
  28. package/vendor/cli/dist/connectors/_google/discovery/drive_v3.json +1 -0
  29. package/vendor/cli/dist/connectors/_google/discovery/gmail_v1.json +1 -0
  30. package/vendor/cli/dist/connectors/_google/discovery/people_v1.json +1 -0
  31. package/vendor/cli/dist/connectors/_google/discovery/sheets_v4.json +1 -0
  32. package/vendor/cli/dist/connectors/_google/discovery/tasks_v1.json +1 -0
  33. package/vendor/cli/dist/connectors/_google/discovery/youtube_v3.json +1 -0
  34. package/vendor/cli/dist/connectors/_google/discovery.ts +442 -0
  35. package/vendor/cli/dist/connectors/_google/errors.ts +91 -0
  36. package/vendor/cli/dist/connectors/_google/list-feed.ts +223 -0
  37. package/vendor/cli/dist/connectors/_google/policies.ts +127 -0
  38. package/vendor/cli/dist/connectors/agentid.ts +43 -0
  39. package/vendor/cli/dist/connectors/chat-user-identity.ts +2 -1
  40. package/vendor/cli/dist/connectors/github-automation-events.ts +9 -0
  41. package/vendor/cli/dist/connectors/github-identity.ts +28 -3
  42. package/vendor/cli/dist/connectors/github.ts +101 -59
  43. package/vendor/cli/dist/connectors/google_calendar.ts +207 -719
  44. package/vendor/cli/dist/connectors/google_drive.ts +16 -2
  45. package/vendor/cli/dist/connectors/google_gmail.ts +51 -33
  46. package/vendor/cli/dist/connectors/herdr.ts +406 -0
  47. package/vendor/cli/dist/connectors/jira.ts +6 -6
  48. package/vendor/cli/dist/connectors/linear.ts +7 -4
  49. package/vendor/cli/dist/connectors/postgres.ts +27 -2
  50. package/vendor/cli/dist/connectors/tiktok-inbox-page.js +230 -0
  51. package/vendor/cli/dist/connectors/tiktok-web-page.js +211 -0
  52. package/vendor/cli/dist/connectors/tiktok_web.ts +286 -0
  53. package/vendor/cli/dist/connectors/whatsapp-web-adapter.js +228 -10
  54. package/vendor/cli/dist/connectors/whatsapp-web-helpers.ts +47 -14
  55. package/vendor/cli/dist/connectors/whatsapp-web-identity.ts +121 -0
  56. package/vendor/cli/dist/connectors/whatsapp_web.ts +170 -29
  57. package/vendor/cli/dist/connectors/x.ts +276 -136
  58. package/vendor/cli/dist/index.js +1 -37
  59. package/vendor/cli/dist/internal/context.d.ts +10 -0
  60. package/vendor/cli/dist/internal/context.js +26 -16
  61. package/vendor/cli/dist/internal/node-version.d.ts +1 -1
  62. package/vendor/cli/dist/internal/node-version.js +1 -1
  63. package/vendor/cli/dist/internal/oauth.d.ts +3 -1
  64. package/vendor/cli/dist/internal/oauth.js +4 -3
  65. package/vendor/cli/dist/internal/runtime-components.d.ts +9 -0
  66. package/vendor/cli/dist/internal/runtime-components.js +67 -2
  67. package/vendor/cli/dist/mac-auth.js +17 -1
  68. package/vendor/cli/dist/providers.json +1 -1
  69. package/vendor/cli/dist/runtime-components.json +4 -4
  70. package/vendor/cli/dist/templates/AGENTS.md.tmpl +11 -4
  71. package/vendor/cli/dist/templates/README.md.tmpl +1 -1
  72. package/vendor/cli/dist/vendor/ora/.bun-tag-fde20e9a6df212fa +0 -0
  73. package/vendor/cli/dist/vendor/ora/index.d.ts +332 -0
  74. package/vendor/cli/dist/vendor/ora/index.js +424 -0
  75. package/vendor/cli/dist/vendor/ora/license +9 -0
  76. package/vendor/cli/dist/vendor/ora/package.json +64 -0
  77. package/vendor/cli/dist/vendor/ora/readme.md +329 -0
  78. package/vendor/cli/package.json +2 -2
  79. package/vendor/connector-sdk/dist/browser-requirement.d.ts +36 -0
  80. package/vendor/connector-sdk/dist/browser-requirement.js +60 -0
  81. package/vendor/connector-sdk/dist/connector-runtime.d.ts +3 -1
  82. package/vendor/connector-sdk/dist/connector-runtime.js +10 -0
  83. package/vendor/connector-sdk/dist/connector-types.d.ts +159 -45
  84. package/vendor/connector-sdk/dist/define-connector.d.ts +4 -2
  85. package/vendor/connector-sdk/dist/define-connector.js +3 -0
  86. package/vendor/connector-sdk/dist/index.d.ts +5 -2
  87. package/vendor/connector-sdk/dist/index.js +1 -0
  88. package/vendor/connector-sdk/dist/reaction-client-types.d.ts +16 -21
  89. package/vendor/connector-sdk/dist/reaction-client-types.typecheck.d.ts +5 -0
  90. package/vendor/connector-sdk/package.json +5 -1
  91. package/vendor/connector-worker/dist/agent-turn/guest-entry.js +31 -27
  92. package/vendor/connector-worker/dist/agent-turn/guest.bundle.js +131 -185
  93. package/vendor/connector-worker/dist/agent-turn/memory.d.ts +3 -2
  94. package/vendor/connector-worker/dist/agent-turn/memory.js +3 -20
  95. package/vendor/connector-worker/dist/agent-turn/types.d.ts +3 -15
  96. package/vendor/connector-worker/dist/agent-turn/workspace.d.ts +2 -2
  97. package/vendor/connector-worker/dist/agent-turn/workspace.js +2 -4
  98. package/vendor/connector-worker/dist/daemon/action-monitor.d.ts +12 -0
  99. package/vendor/connector-worker/dist/daemon/action-monitor.js +25 -0
  100. package/vendor/connector-worker/dist/daemon/agent-turn.js +0 -9
  101. package/vendor/connector-worker/dist/daemon/builtins/index.d.ts +12 -0
  102. package/vendor/connector-worker/dist/daemon/builtins/index.js +61 -1
  103. package/vendor/connector-worker/dist/daemon/builtins/os-shell.d.ts +2 -1
  104. package/vendor/connector-worker/dist/daemon/builtins/os-shell.js +40 -27
  105. package/vendor/connector-worker/dist/daemon/client.d.ts +5 -1
  106. package/vendor/connector-worker/dist/daemon/client.js +3 -0
  107. package/vendor/connector-worker/dist/daemon/device-concurrency.d.ts +8 -0
  108. package/vendor/connector-worker/dist/daemon/device-concurrency.js +13 -0
  109. package/vendor/connector-worker/dist/daemon/executor.d.ts +1 -1
  110. package/vendor/connector-worker/dist/daemon/executor.js +48 -117
  111. package/vendor/connector-worker/dist/daemon/log.d.ts +2 -0
  112. package/vendor/connector-worker/dist/daemon/log.js +23 -2
  113. package/vendor/connector-worker/dist/daemon/mac-device-daemon.d.ts +20 -4
  114. package/vendor/connector-worker/dist/daemon/mac-device-daemon.js +53 -13
  115. package/vendor/connector-worker/dist/daemon/native-bridge/client.d.ts +6 -0
  116. package/vendor/connector-worker/dist/daemon/native-bridge/client.js +15 -0
  117. package/vendor/connector-worker/dist/daemon/native-bridge/executor.js +2 -0
  118. package/vendor/connector-worker/dist/daemon/native-bridge/protocol.d.ts +1 -1
  119. package/vendor/connector-worker/dist/daemon/native-bridge/protocol.js +1 -0
  120. package/vendor/connector-worker/dist/daemon/poll-loop.d.ts +3 -0
  121. package/vendor/connector-worker/dist/daemon/poll-loop.js +5 -1
  122. package/vendor/connector-worker/dist/daemon/start.d.ts +1 -0
  123. package/vendor/connector-worker/dist/daemon/start.js +12 -5
  124. package/vendor/connector-worker/dist/executor/interface.d.ts +34 -2
  125. package/vendor/connector-worker/dist/executor/isolate.js +66 -15
  126. package/vendor/connector-worker/dist/executor/redact.d.ts +1 -0
  127. package/vendor/connector-worker/dist/executor/redact.js +97 -26
  128. package/vendor/connector-worker/dist/executor/runtime.d.ts +7 -0
  129. package/vendor/connector-worker/dist/executor/runtime.js +26 -0
  130. package/vendor/connector-worker/dist/isolate/bridge.js +3 -1
  131. package/vendor/connector-worker/dist/mac-device-daemon.js +10 -3
  132. package/vendor/connector-worker/package.json +2 -2
  133. package/vendor/core/dist/agent-policy.js +1 -1
  134. package/vendor/core/dist/agent-store.d.ts +2 -0
  135. package/vendor/core/dist/agent-tooling.d.ts +1 -1
  136. package/vendor/core/dist/agent-tooling.js +1 -1
  137. package/vendor/core/dist/capabilities.js +2 -0
  138. package/vendor/core/dist/contracts/agent-settings.d.ts +0 -11
  139. package/vendor/core/dist/contracts/agent-settings.js +1 -7
  140. package/vendor/core/dist/contracts/tools/collection-presentation.d.ts +93 -0
  141. package/vendor/core/dist/contracts/tools/collection-presentation.js +88 -0
  142. package/vendor/core/dist/contracts/tools/collection-selection.d.ts +47 -0
  143. package/vendor/core/dist/contracts/tools/collection-selection.js +74 -0
  144. package/vendor/core/dist/contracts/tools/manage-agents.d.ts +10 -4
  145. package/vendor/core/dist/contracts/tools/manage-auth-profiles.d.ts +1 -1
  146. package/vendor/core/dist/contracts/tools/manage-auth-profiles.js +5 -1
  147. package/vendor/core/dist/contracts/tools/manage-automations.d.ts +19 -4
  148. package/vendor/core/dist/contracts/tools/manage-automations.js +25 -8
  149. package/vendor/core/dist/contracts/tools/manage-connections.d.ts +1 -0
  150. package/vendor/core/dist/contracts/tools/manage-connections.js +3 -0
  151. package/vendor/core/dist/contracts/tools/manage-entity-schema.d.ts +11 -0
  152. package/vendor/core/dist/contracts/tools/manage-entity-schema.js +42 -3
  153. package/vendor/core/dist/contracts/tools/manage-entity.d.ts +181 -152
  154. package/vendor/core/dist/contracts/tools/manage-entity.js +118 -162
  155. package/vendor/core/dist/contracts/tools/manage-operations.d.ts +41 -3
  156. package/vendor/core/dist/contracts/tools/manage-operations.js +40 -2
  157. package/vendor/core/dist/contracts/tools/manage-views.d.ts +182 -7
  158. package/vendor/core/dist/contracts/tools/manage-views.js +49 -13
  159. package/vendor/core/dist/contracts/tools/read-knowledge.d.ts +11 -2
  160. package/vendor/core/dist/contracts/tools/read-knowledge.js +23 -3
  161. package/vendor/core/dist/contracts/tools/save-memory.d.ts +3 -6
  162. package/vendor/core/dist/contracts/tools/save-memory.js +2 -6
  163. package/vendor/core/dist/contracts/tools/view-attach.d.ts +2 -1
  164. package/vendor/core/dist/contracts/tools/view-attach.js +7 -2
  165. package/vendor/core/dist/contracts/worker/protocol.d.ts +122 -28
  166. package/vendor/core/dist/contracts/worker/protocol.js +81 -17
  167. package/vendor/core/dist/index.d.ts +3 -4
  168. package/vendor/core/dist/index.js +3 -6
  169. package/vendor/core/dist/json-template.d.ts +6 -10
  170. package/vendor/core/dist/json-template.js +5 -9
  171. package/vendor/core/dist/logger.js +11 -5
  172. package/vendor/core/dist/shell-safety.d.ts +9 -0
  173. package/vendor/core/dist/shell-safety.js +154 -0
  174. package/vendor/core/dist/suggestions.d.ts +1 -1
  175. package/vendor/core/dist/suggestions.js +1 -1
  176. package/vendor/core/dist/tool-trace-summary.d.ts +1 -1
  177. package/vendor/core/dist/tool-trace-summary.js +1 -1
  178. package/vendor/core/dist/types.d.ts +1 -3
  179. package/vendor/core/dist/utils/diagnostic-scrubber.d.ts +7 -0
  180. package/vendor/core/dist/utils/{sentry-scrubber.js → diagnostic-scrubber.js} +25 -53
  181. package/vendor/core/dist/worker/auth.d.ts +44 -6
  182. package/vendor/core/dist/worker/auth.js +44 -11
  183. package/vendor/core/package.json +15 -6
  184. package/vendor/ora/.bun-tag-fde20e9a6df212fa +0 -0
  185. package/vendor/ora/index.d.ts +332 -0
  186. package/vendor/ora/index.js +424 -0
  187. package/vendor/ora/license +9 -0
  188. package/vendor/ora/package.json +53 -0
  189. package/vendor/ora/readme.md +329 -0
  190. package/vendor/cli/dist/commands/telemetry.d.ts +0 -10
  191. package/vendor/cli/dist/commands/telemetry.js +0 -66
  192. package/vendor/core/dist/sentry.d.ts +0 -26
  193. package/vendor/core/dist/sentry.js +0 -133
  194. package/vendor/core/dist/tool-policy.d.ts +0 -74
  195. package/vendor/core/dist/tool-policy.js +0 -305
  196. package/vendor/core/dist/utils/sentry-scrubber.d.ts +0 -29
@@ -0,0 +1,286 @@
1
+ import {
2
+ ConnectorRuntime,
3
+ requireBrowser,
4
+ type ActionContext,
5
+ type ActionResult,
6
+ type ChromeActionDispatcher,
7
+ type FeedReadContext,
8
+ type FeedReadResult,
9
+ type RuntimeConnectorDefinition,
10
+ } from "@lobu/connector-sdk";
11
+ import { tiktokPage } from "./tiktok-web-page.js";
12
+ import { tiktokInboxPage } from "./tiktok-inbox-page.js";
13
+
14
+ const ORIGIN = "https://www.tiktok.com";
15
+ const ORIGINS = [ORIGIN];
16
+ const HANDLE = /^[A-Za-z0-9_.]{1,64}$/;
17
+ type RecordValue = Record<string, unknown>;
18
+ const object = (value: unknown): RecordValue => value !== null && typeof value === "object" && !Array.isArray(value) ? value as RecordValue : {};
19
+
20
+ export function normalizeTikTokPostUrl(value: unknown): string {
21
+ if (typeof value !== "string") throw new Error("post_url is required");
22
+ const url = new URL(value);
23
+ if (url.origin !== ORIGIN || url.username || url.password || !/^\/@[A-Za-z0-9_.]{1,64}\/(video|photo)\/[0-9]+$/.test(url.pathname)) {
24
+ throw new Error("Expected a canonical https://www.tiktok.com/@creator/video/id or /photo/id URL");
25
+ }
26
+ return `${ORIGIN}${url.pathname}`;
27
+ }
28
+
29
+ export function normalizeTikTokPost(value: unknown): RecordValue {
30
+ const item = object(value);
31
+ const author = object(item.author);
32
+ if (typeof item.id !== "string" || !/^[0-9]+$/.test(item.id) || typeof author.uniqueId !== "string" || !HANDLE.test(author.uniqueId)) {
33
+ throw new Error("TikTok returned a post without a valid source identity");
34
+ }
35
+ const video = object(item.video);
36
+ const images = object(item.imagePost).images;
37
+ const photo = Array.isArray(images) && images.length > 0;
38
+ const stats = object(item.stats);
39
+ const created = Number(item.createTime);
40
+ return {
41
+ origin_id: item.id,
42
+ source_url: `${ORIGIN}/@${author.uniqueId}/${photo ? "photo" : "video"}/${item.id}`,
43
+ text: typeof item.desc === "string" ? item.desc : "",
44
+ author: { id: author.id, handle: author.uniqueId, name: author.nickname },
45
+ ...(Number.isFinite(created) && created > 0 && created < 8640000000000 ? { created_at: new Date(created * 1000).toISOString() } : {}),
46
+ ...(typeof item.digged === "boolean" ? { liked: item.digged } : {}),
47
+ media: photo ? { type: "photos", count: images.length } : { type: "video", duration_seconds: video.duration, width: video.width, height: video.height },
48
+ counts: { likes: stats.diggCount, comments: stats.commentCount, views: stats.playCount, shares: stats.shareCount },
49
+ };
50
+ }
51
+
52
+ function expression(command: string, args: RecordValue = {}): string {
53
+ return `(${tiktokPage.toString()})(${JSON.stringify(command)},${JSON.stringify(args)})`;
54
+ }
55
+
56
+ async function page(browser: ChromeActionDispatcher, tabId: number, command: string, args: RecordValue = {}): Promise<RecordValue> {
57
+ const output = await browser.dispatch("evaluate", { tab_id: tabId, expression: expression(command, args), allowed_origins: ORIGINS });
58
+ if (!output.value || typeof output.value !== "object") throw new Error("TikTok page returned no data");
59
+ return object(output.value);
60
+ }
61
+
62
+ async function scratch<T>(browser: ChromeActionDispatcher, run: (tabId: number) => Promise<T>): Promise<T> {
63
+ const nav = await browser.dispatch("navigate", { url: "about:blank", new_tab: true, allowed_origins: ORIGINS });
64
+ if (typeof nav.tab_id !== "number") throw new Error("TikTok scratch tab was not created");
65
+ try { return await run(nav.tab_id); }
66
+ finally { await browser.dispatch("close_tab", { tab_id: nav.tab_id, allowed_origins: ORIGINS }); }
67
+ }
68
+
69
+ async function navigate(browser: ChromeActionDispatcher, tabId: number, url: string, focus = false): Promise<void> {
70
+ await browser.dispatch("navigate", { tab_id: tabId, url, allowed_origins: ORIGINS });
71
+ // TikTok's video page can remain blank in a fresh background tab. Its own
72
+ // controls appeared immediately after focus in the live browser probe.
73
+ if (focus) await browser.dispatch("focus_tab", { tab_id: tabId, draw_attention: false, allowed_origins: ORIGINS });
74
+ await browser.dispatch("wait_for_selector", { tab_id: tabId, selector: '[data-e2e="nav-profile"]', timeout_ms: 15000, allowed_origins: ORIGINS });
75
+ }
76
+
77
+ async function sourcePost(browser: ChromeActionDispatcher, tabId: number, url: string): Promise<RecordValue> {
78
+ if (!url.includes("/photo/")) {
79
+ await navigate(browser, tabId, url, true);
80
+ return page(browser, tabId, "detail", { url });
81
+ }
82
+ const start = await browser.dispatch("network_intercept_start", { tab_id: tabId, patterns: [`${ORIGIN}/api/item/detail/**`], max_body_bytes: 2097152, max_buffer_responses: 5, allowed_origins: ORIGINS });
83
+ if (typeof start.session_id !== "string") throw new Error("TikTok photo capture did not start");
84
+ try {
85
+ await navigate(browser, tabId, url);
86
+ await browser.dispatch("wait_for_selector", { tab_id: tabId, selector: '[data-e2e="recommend-list-item-container"] .swiper', timeout_ms: 15000, allowed_origins: ORIGINS });
87
+ const data = await browser.dispatch("network_intercept_drain", { session_id: start.session_id, allowed_origins: ORIGINS });
88
+ for (const response of Array.isArray(data.responses) ? data.responses : []) {
89
+ const r = object(response);
90
+ if (r.status !== 200 || r.truncated || r.base64_encoded || typeof r.body !== "string") continue;
91
+ const json = object(JSON.parse(r.body));
92
+ const item = object(object(json.itemInfo).itemStruct);
93
+ if (json.statusCode === 0 && item.id === url.split("/").pop()) return page(browser, tabId, "detail", { url, source_item: item });
94
+ }
95
+ throw new Error("TikTok did not expose the requested photo post's source record");
96
+ } finally {
97
+ await browser.dispatch("network_intercept_stop", { session_id: start.session_id, allowed_origins: ORIGINS });
98
+ }
99
+ }
100
+
101
+ async function followingPosts(browser: ChromeActionDispatcher, tabId: number, sessionId: string, limit: number): Promise<RecordValue[]> {
102
+ const unique = new Map<unknown, RecordValue>();
103
+ let continuation: RecordValue | undefined;
104
+ for (let poll = 0; poll < 5; poll++) {
105
+ await page(browser, tabId, poll ? "following_wait" : "following_status");
106
+ const data = await browser.dispatch("network_intercept_drain", { session_id: sessionId, allowed_origins: ORIGINS });
107
+ if (data.missing || !Array.isArray(data.responses)) throw new Error("TikTok Following capture is unavailable");
108
+ for (const response of data.responses) {
109
+ const r = object(response);
110
+ const url = new URL(String(r.url));
111
+ if (url.origin !== ORIGIN || url.pathname !== "/api/following/item_list/" || url.username || url.password) throw new Error("TikTok Following capture has an unexpected URL");
112
+ if (r.status !== 200 || r.truncated || r.base64_encoded || typeof r.body !== "string") throw new Error("TikTok Following response is unavailable or truncated");
113
+ const json = object(JSON.parse(r.body));
114
+ if (json.status_code !== 0 || (json.statusCode !== undefined && json.statusCode !== 0) || !Array.isArray(json.itemList) || typeof json.hasMore !== "boolean") throw new Error("TikTok did not return a successful Following feed");
115
+ for (const item of json.itemList) {
116
+ const row = normalizeTikTokPost(item);
117
+ unique.set(row.origin_id, row);
118
+ }
119
+ continuation = json.hasMore ? { url: url.href, cursor: json.cursor, level: json.level } : undefined;
120
+ }
121
+ if (data.responses.length) break;
122
+ }
123
+ if (unique.size < limit && continuation) {
124
+ // Keep continuation in the signed-in page: separate remote operations per
125
+ // request exhausted the source-read deadline in the live browser probe.
126
+ const data = await page(browser, tabId, "following_posts", { ...continuation, limit, seen_ids: [...unique.keys()] });
127
+ if (!Array.isArray(data.items)) throw new Error("TikTok Following continuation returned no data");
128
+ for (const item of data.items) {
129
+ const row = normalizeTikTokPost(item);
130
+ unique.set(row.origin_id, row);
131
+ }
132
+ }
133
+ // Do not return a successful partial read when a late challenge appeared.
134
+ await page(browser, tabId, "following_status");
135
+ return [...unique.values()];
136
+ }
137
+
138
+ async function readTikTokSnapshot(ctx: FeedReadContext): Promise<FeedReadResult> {
139
+ if (ctx.cursor || ctx.offset || ctx.sort || ctx.window || ctx.match) throw new Error("TikTok snapshot feeds do not support pagination, sorting, time windows, or exact-match filters");
140
+ if (ctx.query && ctx.feedKey !== "search") throw new Error("Use the TikTok search feed for queries");
141
+ if (ctx.limit !== undefined && (!Number.isInteger(ctx.limit) || ctx.limit < 1)) throw new Error("limit must be a positive integer");
142
+ const limit = Math.min(ctx.limit ?? 10, 30);
143
+ let url = ORIGIN;
144
+ if (ctx.feedKey === "following") url += "/following";
145
+ else if (ctx.feedKey === "search") {
146
+ const query = ctx.query?.trim() || (typeof ctx.config.query === "string" ? ctx.config.query.trim() : "");
147
+ if (!query || query.length > 200) throw new Error("Search requires a query of 1 to 200 characters");
148
+ url += `/search?q=${encodeURIComponent(query)}`;
149
+ } else if (ctx.feedKey !== "for_you") throw new Error(`Unsupported TikTok feed: ${ctx.feedKey}`);
150
+ const browser = requireBrowser(ctx);
151
+ return scratch(browser, async tabId => {
152
+ let sessionId: string | undefined;
153
+ try {
154
+ if (ctx.feedKey === "following") {
155
+ const start = await browser.dispatch("network_intercept_start", { tab_id: tabId, patterns: [`${ORIGIN}/api/following/item_list/**`], max_body_bytes: 2097152, max_buffer_responses: 10, allowed_origins: ORIGINS });
156
+ if (typeof start.session_id !== "string") throw new Error("TikTok feed capture did not start");
157
+ sessionId = start.session_id;
158
+ }
159
+ // Snapshot/search results also lazy-load only in a visible tab.
160
+ await navigate(browser, tabId, url, true);
161
+ let rows: RecordValue[] = [];
162
+ if (ctx.feedKey === "following") {
163
+ await browser.dispatch("wait_for_selector", { tab_id: tabId, selector: '[data-e2e="recommend-list-item-container"]', timeout_ms: 15000, allowed_origins: ORIGINS });
164
+ rows = await followingPosts(browser, tabId, sessionId!, limit);
165
+ } else if (ctx.feedKey === "search") {
166
+ await browser.dispatch("wait_for_selector", { tab_id: tabId, selector: 'a[href*="/video/"],a[href*="/photo/"]', timeout_ms: 15000, allowed_origins: ORIGINS });
167
+ const data = await page(browser, tabId, "post_links");
168
+ rows = (Array.isArray(data.rows) ? data.rows : []).map(value => {
169
+ const r = object(value);
170
+ const sourceUrl = normalizeTikTokPostUrl(r.url);
171
+ return { origin_id: sourceUrl.split("/").pop(), source_url: sourceUrl, text: r.text, media: { type: sourceUrl.includes("/photo/") ? "photos" : "video" } };
172
+ });
173
+ } else {
174
+ const data = await page(browser, tabId, "snapshot");
175
+ rows = (Array.isArray(data.items) ? data.items : []).map(normalizeTikTokPost);
176
+ }
177
+ if (!rows.length) throw new Error("TikTok returned no verifiable posts; this is not proof of an empty feed");
178
+ const unique = [...new Map(rows.map(row => [row.origin_id, row])).values()];
179
+ // End of this bounded snapshot, never a claim of complete source history.
180
+ // Returning true would make the generic pager invent an unusable offset.
181
+ return { rows: unique.slice(0, limit), hasMore: false };
182
+ } finally {
183
+ if (sessionId) await browser.dispatch("network_intercept_stop", { session_id: sessionId, allowed_origins: ORIGINS });
184
+ }
185
+ });
186
+ }
187
+
188
+ async function readTikTokInbox(ctx: FeedReadContext): Promise<FeedReadResult> {
189
+ if (ctx.offset || ctx.sort || ctx.window || ctx.match || ctx.query) throw new Error("TikTok inbox reads use source order and the returned cursor; search, offsets, sorting, matching, and time windows are unsupported");
190
+ if (ctx.limit !== undefined && (!Number.isInteger(ctx.limit) || ctx.limit < 1)) throw new Error("limit must be a positive integer");
191
+ const browser = requireBrowser(ctx);
192
+ return scratch(browser, async tabId => {
193
+ await navigate(browser, tabId, ORIGIN);
194
+ const result = await browser.dispatch("evaluate", {
195
+ tab_id: tabId,
196
+ expression: `(${tiktokInboxPage.toString()})(${JSON.stringify({ feed: ctx.feedKey, limit: Math.min(ctx.limit ?? 20, 100), cursor: ctx.cursor })})`,
197
+ await_promise: true,
198
+ allowed_origins: ORIGINS,
199
+ });
200
+ const data = object(result.value);
201
+ if (!Array.isArray(data.rows) || typeof data.hasMore !== "boolean" || (data.hasMore && typeof data.nextCursor !== "string")) throw new Error("TikTok inbox returned no verifiable page");
202
+ return data as unknown as FeedReadResult;
203
+ });
204
+ }
205
+
206
+ export default class TikTokConnector extends ConnectorRuntime {
207
+ readonly definition: RuntimeConnectorDefinition = {
208
+ key: "tiktok.web", name: "TikTok", version: "1.1.5", faviconDomain: "tiktok.com",
209
+ description: "Read TikTok through your signed-in paired browser, inspect post visuals, set likes, and prepare comments for human submission. Automations decide what to read and act on.",
210
+ browser: { origins: ORIGINS, authMethods: ["browser"], accountProbe: { url: ORIGIN, expression: expression("identity") } },
211
+ authSchema: { methods: [{ type: "browser", mode: "live" }] },
212
+ feeds: {
213
+ messages: { key: "messages", name: "Messages", description: "Read a recent page per conversation before older history, with sender, direction, source IDs, evidence revisions and timestamps. Cursors cover the initial conversation set; a changed set reports incomplete coverage and needs a fresh scan. Unsupported media is identified, not transcribed. No message is sent.", read: readTikTokInbox },
214
+ activity: { key: "activity", name: "Activity", description: "Read loaded TikTok activity including likes, comments, mentions and followers, with source IDs and links. Follow the returned cursor for older entries. Opening the activity panel may clear its unread badge.", read: readTikTokInbox },
215
+ for_you: { key: "for_you", name: "For You", description: "Initial personalized timeline snapshot. No history coverage or continuation; repeat reads can overlap.", read: readTikTokSnapshot },
216
+ following: { key: "following", name: "Following", description: "Read up to 30 distinct Following posts using TikTok's source continuation. Returns fewer only at the source end; stalled or budget-limited reads report an error. No complete history coverage or caller continuation; repeat reads can overlap.", read: readTikTokSnapshot },
217
+ search: { key: "search", name: "Search", description: "Browser search results for videos and photo posts. Use query when reading. Initial snapshot without continuation.", read: readTikTokSnapshot, configSchema: { type: "object", properties: { query: { type: "string", maxLength: 200 } }, additionalProperties: false } },
218
+ },
219
+ actions: {
220
+ set_like: { key: "set_like", name: "Set like", kind: "write", description: "Set a post's liked state and verify it after reload. Already-matching posts are left unchanged. May focus a scratch tab to load the post. Selection and budgets belong in Automations.", annotations: { idempotentHint: true, destructiveHint: false, openWorldHint: true }, inputSchema: { type: "object", required: ["post_url", "liked"], additionalProperties: false, properties: { post_url: { type: "string", format: "uri" }, liked: { type: "boolean" } } } },
221
+ inspect_post: { key: "inspect_post", name: "Inspect post visuals", kind: "read", description: "Return post metadata and real screenshots at requested video times or photo indices. Images are visual samples, not full video or audio understanding. May focus a scratch tab to load media.", annotations: { readOnlyHint: true, openWorldHint: true }, inputSchema: { type: "object", required: ["post_url"], additionalProperties: false, properties: { post_url: { type: "string", format: "uri" }, frame_times: { type: "array", minItems: 1, maxItems: 6, items: { type: "number", minimum: 0 }, default: [0] }, photo_indices: { type: "array", minItems: 1, maxItems: 10, items: { type: "integer", minimum: 0 }, description: "Zero-based photos to capture; defaults to the first photo." } } } },
222
+ prepare_comment: { key: "prepare_comment", name: "Prepare comment", kind: "write", description: "Prepare a video comment for human submission. Call operations.execute with activation: { kind: 'page_visit', urls: [post_url] }. After the user opens that exact post, fills its empty composer. The user must click Post. Fails without page activation; never submits or overwrites a draft. Photo comments are not supported.", annotations: { destructiveHint: false, idempotentHint: false, openWorldHint: true }, inputSchema: { type: "object", required: ["post_url", "body"], additionalProperties: false, properties: { post_url: { type: "string", format: "uri" }, body: { type: "string", minLength: 1 } } } },
223
+ },
224
+ };
225
+
226
+ async execute(ctx: ActionContext): Promise<ActionResult> {
227
+ try {
228
+ const url = normalizeTikTokPostUrl(ctx.input.post_url);
229
+ const browser = requireBrowser(ctx);
230
+ if (ctx.actionKey === "prepare_comment") {
231
+ if (url.includes("/photo/")) throw new Error("Photo comment drafts are not supported yet");
232
+ const body = ctx.input.body;
233
+ if (typeof body !== "string" || !body.trim()) throw new Error("Comment body is required");
234
+ // Page activation preserves query parameters and fragments, unlike source permalinks.
235
+ const activatedUrl = new URL(String(ctx.input.post_url)).href;
236
+ const nav = await browser.dispatch("navigate", { url: activatedUrl, require_page_activation: true, allowed_origins: ORIGINS });
237
+ if (typeof nav.tab_id !== "number") throw new Error("User-owned post tab is not active");
238
+ await page(browser, nav.tab_id, "open_comment", { url });
239
+ const tree = await browser.dispatch("get_accessibility_tree", { tab_id: nav.tab_id, filter: "interactive", allowed_origins: ORIGINS });
240
+ const editors = (Array.isArray(tree.tree) ? tree.tree : []).map(object).filter(node => node.role === "textbox");
241
+ if (editors.length !== 1 || typeof tree.document_epoch !== "number" || typeof editors[0]?.ref_id !== "number") throw new Error("TikTok comment textbox is ambiguous");
242
+ await browser.dispatch("type_ref", { tab_id: nav.tab_id, ref: { document_epoch: tree.document_epoch, ref_id: editors[0].ref_id }, text: body, clear_first: false, allowed_origins: ORIGINS });
243
+ const staged = await page(browser, nav.tab_id, "comment_text", { url });
244
+ if (staged.text !== body) throw new Error("TikTok comment draft did not match the requested text; review it in the browser");
245
+ return { success: true, output: { prepared: true, submitted: false, post_url: activatedUrl, tab_id: nav.tab_id, body } };
246
+ }
247
+ if (ctx.actionKey !== "set_like" && ctx.actionKey !== "inspect_post") throw new Error("Unsupported TikTok action");
248
+ if (ctx.actionKey === "set_like" && typeof ctx.input.liked !== "boolean") throw new Error("liked must be a boolean");
249
+ if (ctx.actionKey === "inspect_post" && (url.includes("/photo/") ? ctx.input.frame_times !== undefined : ctx.input.photo_indices !== undefined)) throw new Error("Use photo_indices for photos and frame_times for videos");
250
+ const times = ctx.input.frame_times ?? [0];
251
+ const photoIndices = ctx.input.photo_indices ?? [0];
252
+ if (ctx.actionKey === "inspect_post" && (!Array.isArray(times) || !times.length || times.length > 6 || times.some(t => typeof t !== "number" || !Number.isFinite(t) || t < 0))) throw new Error("frame_times must contain 1 to 6 nonnegative seconds");
253
+ if (ctx.actionKey === "inspect_post" && (!Array.isArray(photoIndices) || !photoIndices.length || photoIndices.length > 10 || photoIndices.some(t => typeof t !== "number" || !Number.isInteger(t) || t < 0))) throw new Error("photo_indices must contain 1 to 10 nonnegative integers");
254
+ const output = await scratch(browser, async tabId => {
255
+ const detail = await sourcePost(browser, tabId, url);
256
+ const args = { url, source_item: detail.item };
257
+ const post = { ...normalizeTikTokPost(detail.item), liked: detail.liked };
258
+ if (ctx.actionKey === "set_like") {
259
+ const effect = await page(browser, tabId, "set_like", { ...args, liked: ctx.input.liked });
260
+ if (effect.liked !== ctx.input.liked) throw new Error("TikTok like state was not confirmed");
261
+ if (effect.changed === true) {
262
+ const persisted = await sourcePost(browser, tabId, url);
263
+ if (persisted.liked !== ctx.input.liked) throw new Error("TikTok like state did not persist after reload");
264
+ }
265
+ return { post_url: url, ...effect };
266
+ }
267
+ await page(browser, tabId, "mute", args);
268
+ await browser.dispatch("focus_tab", { tab_id: tabId, draw_attention: false, allowed_origins: ORIGINS });
269
+ const attachments: unknown[] = [];
270
+ const frames: RecordValue[] = [];
271
+ const photos = url.includes("/photo/");
272
+ for (const position of (photos ? photoIndices : times) as number[]) {
273
+ const frame = await page(browser, tabId, photos ? "photo" : "frame", { ...args, ...(photos ? { index: position } : { seconds: position }) });
274
+ const shot = await browser.dispatch("screenshot", { tab_id: tabId, allowed_origins: ORIGINS });
275
+ if (!Array.isArray(shot.attachments) || !shot.attachments.length) throw new Error("TikTok screenshot returned no image attachments");
276
+ frames.push({ ...frame, attachment_index: attachments.length });
277
+ attachments.push(...shot.attachments);
278
+ }
279
+ return { post, frames, attachments, audio_inspected: false };
280
+ });
281
+ return { success: true, output };
282
+ } catch (error) {
283
+ return { success: false, error: error instanceof Error ? error.message : String(error) };
284
+ }
285
+ }
286
+ }
@@ -33,8 +33,11 @@ export function whatsAppWebAdapterProgram() {
33
33
  // when this number moves: shipping a fix under the old number leaves every
34
34
  // already-open tab running the previous code with nothing to show for it.
35
35
  // Keep in lockstep with WHATSAPP_ADAPTER_VERSION in whatsapp-web-helpers.ts.
36
- const ADAPTER_VERSION = 14;
36
+ const ADAPTER_VERSION = 22;
37
37
  const SOURCE_ERROR_ID = "whatsapp-web:source-observation-error";
38
+ const MAX_RECORD_BYTES = 128 * 1024;
39
+ const MAX_BUFFER_BYTES = 16 * 1024 * 1024;
40
+ const MAX_BUFFER_ENTRIES = 10_000;
38
41
  const SYSTEM_TYPES = new Set([
39
42
  "gp2",
40
43
  "notification_template",
@@ -87,11 +90,14 @@ export function whatsAppWebAdapterProgram() {
87
90
  },
88
91
  });
89
92
  };
90
- const previous = listeners.get(request.bridge_id);
93
+ const resident = listeners.get(request.bridge_id);
94
+ const previous = resident?.request.references_only === request.references_only
95
+ && resident?.request.chat_filter === request.chat_filter
96
+ && resident?.request.minimum_timestamp === request.minimum_timestamp ? resident : null;
91
97
  detach(request.bridge_id);
92
98
  // A new listener identity fences pending normalization from the detached
93
- // handlers. Keep unaccepted records, but retry a transient source failure
94
- // by attaching fresh handlers to the current collection.
99
+ // handlers. Keep unaccepted records only within the same source scope, and
100
+ // retry transient failures by attaching fresh handlers to the collection.
95
101
  const state = {
96
102
  ...(previous ?? {
97
103
  id: request.bridge_id, collection, sequence: 0,
@@ -102,10 +108,12 @@ export function whatsAppWebAdapterProgram() {
102
108
  };
103
109
  state.token = request.token;
104
110
  state.request = request;
111
+ state.post = (entry) => post(state, entry);
105
112
  const observe = (model, kind) => {
106
113
  if (state.error) return;
107
114
  const sequence = ++state.sequence;
108
- void normalizeMessage(model, "reconcile").then((record) => {
115
+ const normalized = request.references_only ? Promise.resolve(messageReference(model)) : normalizeMessage(model, "reconcile");
116
+ void normalized.then((record) => {
109
117
  if (listeners.get(state.id) !== state || !record || record.timestamp <= 0) return;
110
118
  if (state.request.chat_filter === "group" && !record.is_group) return;
111
119
  if (state.request.chat_filter === "individual" && record.is_group) return;
@@ -113,14 +121,19 @@ export function whatsAppWebAdapterProgram() {
113
121
  // Loading historical models also emits add. Changes to old messages
114
122
  // still pass; they are exactly what the recent collection window misses.
115
123
  if (kind === "add" && record.timestamp < state.request.recent_since) return;
124
+ if (request.references_only) record = {
125
+ id: record.id,
126
+ events: [{ id: record.id, event_type: "message.created", resource_ref: record.id,
127
+ resource_type: "message", occurred_at: new Date(record.timestamp * 1000).toISOString() }],
128
+ };
116
129
  const previous = state.pending.get(record.id);
117
130
  const seen = state.fingerprints.get(record.id);
118
131
  if (seen && seen.sequence >= sequence) return;
119
132
  const fingerprint = JSON.stringify(record);
120
133
  if (seen?.fingerprint === fingerprint) { seen.sequence = sequence; return; }
121
134
  const bytes = new TextEncoder().encode(fingerprint).length;
122
- if (bytes > 128 * 1024 || state.pendingBytes - (previous?.bytes ?? 0) + bytes > 16 * 1024 * 1024 ||
123
- (state.pending.size >= 10_000 && !previous)) {
135
+ if (bytes > MAX_RECORD_BYTES || state.pendingBytes - (previous?.bytes ?? 0) + bytes > MAX_BUFFER_BYTES ||
136
+ (state.pending.size >= MAX_BUFFER_ENTRIES && !previous)) {
124
137
  reportError(state, "WhatsApp page observation buffer overflowed; recovery is required", sequence);
125
138
  return;
126
139
  }
@@ -130,7 +143,7 @@ export function whatsAppWebAdapterProgram() {
130
143
  state.fingerprintBytes += bytes - (seen?.bytes ?? 0);
131
144
  state.fingerprints.delete(record.id);
132
145
  state.fingerprints.set(record.id, { fingerprint, sequence, bytes });
133
- while (state.fingerprints.size > 10_000 || state.fingerprintBytes > 16 * 1024 * 1024) {
146
+ while (state.fingerprints.size > MAX_BUFFER_ENTRIES || state.fingerprintBytes > MAX_BUFFER_BYTES) {
134
147
  const oldest = state.fingerprints.keys().next().value;
135
148
  state.fingerprintBytes -= state.fingerprints.get(oldest).bytes;
136
149
  state.fingerprints.delete(oldest);
@@ -146,9 +159,13 @@ export function whatsAppWebAdapterProgram() {
146
159
  state.onReply = (event) => {
147
160
  if (event.source !== window || event.origin !== location.origin || event.data?.token !== state.token) return;
148
161
  if (event.data.type === "lobu-feed-record:stop") { detach(state.id); return; }
149
- if (event.data.type !== "lobu-feed-record:ack" || !event.data.ok) return;
162
+ if (event.data.type !== "lobu-feed-record:ack") return;
150
163
  for (const [id, entry] of state.pending) {
151
- if (entry.sequence === event.data.sequence) { state.pendingBytes -= entry.bytes; state.pending.delete(id); break; }
164
+ if (entry.sequence !== event.data.sequence) continue;
165
+ if (event.data.ok) {
166
+ state.pendingBytes -= entry.bytes; state.pending.delete(id); entry.resolve?.();
167
+ } else entry.reject?.(new Error("Browser rejected the source reference batch"));
168
+ break;
152
169
  }
153
170
  };
154
171
  listeners.set(state.id, state);
@@ -170,6 +187,28 @@ export function whatsAppWebAdapterProgram() {
170
187
  return { ok: true, listening: true };
171
188
  }
172
189
 
190
+ // Replay shares the live listener's durable browser buffer. Await its receipt,
191
+ // not server delivery: the server advances the cursor only with its own commit.
192
+ async function publishChanges(request) {
193
+ const state = listeners.get(request.bridge_id);
194
+ if (!state?.request.references_only || state.token !== request.token) throw new Error("Source listener is unavailable");
195
+ const record = request.batch;
196
+ const bytes = new TextEncoder().encode(JSON.stringify(record)).length;
197
+ const previous = state.pending.get(record.id);
198
+ if (bytes > MAX_RECORD_BYTES || state.pendingBytes - (previous?.bytes ?? 0) + bytes > MAX_BUFFER_BYTES ||
199
+ (state.pending.size >= MAX_BUFFER_ENTRIES && !previous)) throw new Error("Source replay buffer is full");
200
+ await new Promise((resolve, reject) => {
201
+ const timer = setTimeout(() => reject(new Error("Source replay was not accepted by the browser")), 10_000);
202
+ const entry = { record, bytes, sequence: ++state.sequence,
203
+ resolve: () => { clearTimeout(timer); resolve(); },
204
+ reject: (error) => { clearTimeout(timer); reject(error); } };
205
+ state.pendingBytes += bytes - (previous?.bytes ?? 0);
206
+ state.pending.set(record.id, entry);
207
+ state.post(entry);
208
+ });
209
+ return { ok: true };
210
+ }
211
+
173
212
  function requireFirst(names) {
174
213
  for (const name of names) {
175
214
  try {
@@ -268,6 +307,70 @@ export function whatsAppWebAdapterProgram() {
268
307
  return model?.attributes ?? model?._data ?? model ?? {};
269
308
  }
270
309
 
310
+ function messageReference(model) {
311
+ const row = modelData(model);
312
+ const key = row.id ?? model?.id;
313
+ const id = rawId(key);
314
+ const remote = widString(key?.remote ?? row.chatId ?? (key?.fromMe ? row.to : row.from));
315
+ if (!id || !remote || remote === "status@broadcast" || remote.endsWith("@newsletter") || SYSTEM_TYPES.has(row.type)) return null;
316
+ return { id, timestamp: unixSeconds(row), is_group: remote.endsWith("@g.us") };
317
+ }
318
+
319
+ /** Resume insertion order after reconnect without exporting any message body. */
320
+ async function observeMessages(input = {}) {
321
+ const after = input.after ?? null;
322
+ if (after !== null && (!Number.isSafeInteger(after) || after < 0)) throw new Error("Invalid WhatsApp observation cursor");
323
+ const database = await new Promise((resolve, reject) => {
324
+ const request = indexedDB.open("model-storage");
325
+ request.onupgradeneeded = () => { request.transaction.abort(); reject(new Error("WhatsApp source history database is unavailable")); };
326
+ request.onerror = () => reject(request.error);
327
+ request.onblocked = () => reject(new Error("WhatsApp source history database is blocked"));
328
+ request.onsuccess = () => resolve(request.result);
329
+ });
330
+ let rows;
331
+ try {
332
+ rows = await new Promise((resolve, reject) => {
333
+ const transaction = database.transaction("message", "readonly");
334
+ transaction.onabort = () => reject(transaction.error ?? new Error("WhatsApp source observation aborted"));
335
+ const index = transaction.objectStore("message").index("rowId");
336
+ const request = index.openCursor(after === null ? null : IDBKeyRange.lowerBound(after, true), after === null ? "prev" : "next");
337
+ const page = [];
338
+ request.onerror = () => reject(request.error);
339
+ request.onsuccess = () => {
340
+ const cursor = request.result;
341
+ if (!cursor) { resolve(page); return; }
342
+ if (!Number.isSafeInteger(cursor.key) || cursor.key <= 0 || typeof cursor.value.id !== "string") {
343
+ reject(new Error("WhatsApp source history has an unsupported key layout")); return;
344
+ }
345
+ page.push({ position: cursor.key, id: cursor.value.id, timestamp: unixSeconds(cursor.value), type: cursor.value.type });
346
+ if (after === null || page.length > 500) resolve(page);
347
+ else cursor.continue();
348
+ };
349
+ });
350
+ } finally { database.close(); }
351
+ if (after === null) return { after: rows[0]?.position ?? 0, references: [], hasMore: false };
352
+ const page = rows.slice(0, 500);
353
+ const references = [];
354
+ if (page.length) {
355
+ const keys = requireFirst(["WAWebMsgKey"]);
356
+ if (typeof keys?.fromString !== "function") throw new Error("WhatsApp source message keys are unavailable");
357
+ for (const row of page) {
358
+ // IndexedDB already contains routing and time metadata. Loading message
359
+ // models would hydrate private content and fails for status entries.
360
+ const reference = messageReference({ id: keys.fromString(row.id), t: row.timestamp, type: row.type });
361
+ if (!reference) continue;
362
+ // Timestamp-zero rows are source placeholders, not messages. A later
363
+ // hydration change is also observed by the reference listener.
364
+ if (reference.timestamp <= 0) continue;
365
+ if (reference.timestamp < input.started_at) continue;
366
+ if (input.chat_filter === "group" && !reference.is_group) continue;
367
+ if (input.chat_filter === "individual" && reference.is_group) continue;
368
+ references.push(reference);
369
+ }
370
+ }
371
+ return { after: page[page.length - 1]?.position ?? after, references, hasMore: rows.length > 500 };
372
+ }
373
+
271
374
  function models(collection) {
272
375
  if (Array.isArray(collection?._models)) return collection._models;
273
376
  if (Array.isArray(collection?.models)) return collection.models;
@@ -692,6 +795,10 @@ export function whatsAppWebAdapterProgram() {
692
795
  names.some((name) => typeof module?.[name] === "function");
693
796
  return {
694
797
  collect: hasFunction(history, ["loadEarlierMsgs"]),
798
+ read_messages:
799
+ typeof indexedDB !== "undefined" &&
800
+ typeof requireFirst(["WAWebMsgKey"])?.fromString === "function" &&
801
+ typeof requireFirst(["WAWebCollections"])?.Msg?.getMessagesById === "function",
695
802
  download_media:
696
803
  hasFunction(media, ["downloadAndMaybeDecrypt"]) ||
697
804
  models(requireFirst(["WAWebCollections"])?.Msg).some(
@@ -1123,6 +1230,109 @@ export function whatsAppWebAdapterProgram() {
1123
1230
  throw new Error(`WhatsApp did not confirm ${label}`);
1124
1231
  }
1125
1232
 
1233
+ /**
1234
+ * Enumerate WhatsApp's own local history, not just the models loaded in the
1235
+ * page. Opaque message bodies stay with WhatsApp: its native loader hydrates
1236
+ * the selected keys. Each read scans at most one source page, so callers must
1237
+ * follow cursors even when filtering leaves an empty page. An exact
1238
+ * `id:<message ID>` read is the exception: ids are unique, so it walks
1239
+ * history until that message and answers in one read.
1240
+ */
1241
+ async function readMessages(input = {}) {
1242
+ const query = String(input.query ?? "").trim();
1243
+ const chatFilter = input.chat_filter ?? "all";
1244
+ const limit = Math.min(100, Math.max(1, Math.trunc(Number(input.limit) || 50)));
1245
+ let before = null;
1246
+ if (input.cursor) {
1247
+ const cursor = JSON.parse(input.cursor);
1248
+ if (cursor.v !== 1 || !Number.isSafeInteger(cursor.before) || cursor.before <= 0 ||
1249
+ cursor.query !== query || cursor.chat_filter !== chatFilter) {
1250
+ throw new Error("Invalid WhatsApp source cursor or changed query scope");
1251
+ }
1252
+ before = cursor.before;
1253
+ }
1254
+ const keyModule = requireFirst(["WAWebMsgKey"]);
1255
+ const exactId = query.startsWith("id:") ? query.slice(3) : null;
1256
+ const database = await new Promise((resolve, reject) => {
1257
+ const request = indexedDB.open("model-storage");
1258
+ // Never create an empty replacement database when WhatsApp changes its
1259
+ // storage layout. That would turn unavailable history into false success.
1260
+ request.onupgradeneeded = () => {
1261
+ request.transaction.abort();
1262
+ reject(new Error("WhatsApp source history database is unavailable"));
1263
+ };
1264
+ request.onerror = () => reject(request.error);
1265
+ request.onblocked = () => reject(new Error("WhatsApp source history database is blocked"));
1266
+ request.onsuccess = () => resolve(request.result);
1267
+ });
1268
+ let sourceRows;
1269
+ try {
1270
+ sourceRows = await new Promise((resolve, reject) => {
1271
+ const transaction = database.transaction("message", "readonly");
1272
+ transaction.onabort = () => reject(transaction.error ?? new Error("WhatsApp source read aborted"));
1273
+ const index = transaction.objectStore("message").index("rowId");
1274
+ const range = before === null ? null : IDBKeyRange.upperBound(before, true);
1275
+ const request = index.openCursor(range, "prev");
1276
+ const rows = [];
1277
+ request.onerror = () => reject(request.error);
1278
+ request.onsuccess = () => {
1279
+ const cursor = request.result;
1280
+ if (!cursor) { resolve(rows); return; }
1281
+ if (!Number.isSafeInteger(cursor.key) || cursor.key <= 0 || typeof cursor.value.id !== "string") {
1282
+ reject(new Error("WhatsApp source history has an unsupported key layout"));
1283
+ return;
1284
+ }
1285
+ if (exactId !== null) {
1286
+ if (keyModule.fromString(cursor.value.id).id !== exactId) { cursor.continue(); return; }
1287
+ resolve([{ position: cursor.key, id: cursor.value.id }]);
1288
+ return;
1289
+ }
1290
+ rows.push({ position: cursor.key, id: cursor.value.id });
1291
+ if (rows.length > limit) resolve(rows);
1292
+ else cursor.continue();
1293
+ };
1294
+ });
1295
+ } finally {
1296
+ database.close();
1297
+ }
1298
+ const hasMore = sourceRows.length > limit;
1299
+ const page = sourceRows.slice(0, limit);
1300
+ const collections = requireFirst(["WAWebCollections"]);
1301
+ const selected = page.filter((row) => {
1302
+ const key = keyModule.fromString(row.id);
1303
+ if (exactId !== null && key.id !== exactId) return false;
1304
+ const remote = widString(key.remote);
1305
+ if (remote === "status@broadcast" || remote.endsWith("@newsletter")) return false;
1306
+ const isGroup = remote.endsWith("@g.us");
1307
+ return !(chatFilter === "group" && !isGroup) && !(chatFilter === "individual" && isGroup);
1308
+ });
1309
+ const results = [];
1310
+ if (selected.length) {
1311
+ const loaded = await collections.Msg.getMessagesById(selected.map((row) => row.id));
1312
+ if (loaded?.canceled || !Array.isArray(loaded?.messages)) {
1313
+ throw new Error("Source message loading was canceled or unavailable");
1314
+ }
1315
+ const byKey = new Map(loaded.messages.map((model) => [String(model.id ?? modelData(model).id), model]));
1316
+ const names = nameIndex(collections);
1317
+ for (const row of selected) {
1318
+ const model = byKey.get(row.id);
1319
+ if (!model) throw new Error("Source message could not be loaded; retry the read");
1320
+ const message = await normalizeMessage(model, "snapshot", collections, names);
1321
+ // The relay boundary drops placeholder rows without a stable timestamp.
1322
+ // Skip them like search does; throwing would pin every later read to
1323
+ // the same cursor.
1324
+ if (!message || message.timestamp <= 0) continue;
1325
+ if (exactId === null && query && !`${message.body}\n${message.caption}`.toLowerCase().includes(query.toLowerCase())) continue;
1326
+ results.push(message);
1327
+ }
1328
+ }
1329
+ return {
1330
+ results,
1331
+ hasMore,
1332
+ ...(hasMore ? { nextCursor: JSON.stringify({ v: 1, before: page[page.length - 1].position, query, chat_filter: chatFilter }) } : {}),
1333
+ };
1334
+ }
1335
+
1126
1336
  async function searchMessages(input) {
1127
1337
  const collections = requireFirst(["WAWebCollections"]);
1128
1338
  const query = String(input?.query ?? "").trim();
@@ -1721,6 +1931,10 @@ export function whatsAppWebAdapterProgram() {
1721
1931
  },
1722
1932
  };
1723
1933
  }
1934
+ if (request.op === "account_identity") {
1935
+ const accountId = widString(meUser());
1936
+ return accountId ? { accountId } : null;
1937
+ }
1724
1938
  if (request.op === "probe") {
1725
1939
  const status = readiness();
1726
1940
  return status.ready
@@ -1728,6 +1942,8 @@ export function whatsAppWebAdapterProgram() {
1728
1942
  : { ok: false, error: status };
1729
1943
  }
1730
1944
  if (request.op === "listen") return listen(request);
1945
+ if (request.op === "publish_changes") return await publishChanges(request);
1946
+ if (request.op === "observe_messages") return { ok: true, ...(await observeMessages(request.input)) };
1731
1947
  const capabilities = operationCapabilities();
1732
1948
  if (capabilities[request.op] !== true) {
1733
1949
  return {
@@ -1739,6 +1955,8 @@ export function whatsAppWebAdapterProgram() {
1739
1955
  };
1740
1956
  }
1741
1957
  if (request.op === "collect") return await collect(request);
1958
+ if (request.op === "read_messages")
1959
+ return { ok: true, ...(await readMessages(request.input)) };
1742
1960
  if (request.op === "download_media")
1743
1961
  return { ok: true, ...(await downloadMedia(request)) };
1744
1962
  if (request.op === "search_messages")