@lobu/runtime-device 22.0.0 → 23.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (77) hide show
  1. package/dist/component.json +1 -1
  2. package/dist/dependencies.bun.lock +3 -3
  3. package/npm-shrinkwrap.json +15 -15
  4. package/package.json +1 -1
  5. package/vendor/cli/dist/bundled-skills/lobu/SKILL.md +1 -1
  6. package/vendor/cli/dist/connectors/README.md +14 -1
  7. package/vendor/cli/dist/connectors/google_gmail.ts +1 -0
  8. package/vendor/cli/dist/connectors/jira-helpers.ts +313 -0
  9. package/vendor/cli/dist/connectors/jira.ts +29 -221
  10. package/vendor/cli/dist/connectors/linkedin-browser-script.ts +13 -0
  11. package/vendor/cli/dist/connectors/linkedin-draft-page.js +104 -0
  12. package/vendor/cli/dist/connectors/linkedin-drafts.ts +73 -0
  13. package/vendor/cli/dist/connectors/linkedin-identity.ts +32 -0
  14. package/vendor/cli/dist/connectors/linkedin-inbox-adapter.js +1185 -0
  15. package/vendor/cli/dist/connectors/linkedin-inbox.ts +156 -0
  16. package/vendor/cli/dist/connectors/linkedin-like.ts +114 -0
  17. package/vendor/cli/dist/connectors/linkedin-takeout.ts +577 -0
  18. package/vendor/cli/dist/connectors/linkedin.ts +3998 -0
  19. package/vendor/cli/dist/connectors/takeout-utils.ts +240 -0
  20. package/vendor/cli/dist/connectors/tiktok-inbox-page.js +9 -9
  21. package/vendor/cli/dist/connectors/tiktok-web-page.js +21 -15
  22. package/vendor/cli/dist/connectors/tiktok_web.ts +63 -30
  23. package/vendor/cli/dist/connectors/whatsapp-web-adapter.js +64 -564
  24. package/vendor/cli/dist/connectors/whatsapp-web-helpers.ts +30 -444
  25. package/vendor/cli/dist/connectors/whatsapp-web-identity.ts +20 -6
  26. package/vendor/cli/dist/connectors/whatsapp_web.ts +45 -645
  27. package/vendor/cli/dist/connectors/x-dm-adapter.js +556 -0
  28. package/vendor/cli/dist/connectors/x-identity.ts +31 -2
  29. package/vendor/cli/dist/connectors/x.ts +266 -120
  30. package/vendor/cli/dist/runtime-components.json +4 -4
  31. package/vendor/cli/dist/templates/AGENTS.md.tmpl +2 -2
  32. package/vendor/cli/package.json +1 -1
  33. package/vendor/connector-sdk/dist/browser-handoff-banner.d.ts +39 -0
  34. package/vendor/connector-sdk/dist/browser-handoff-banner.js +175 -0
  35. package/vendor/connector-sdk/dist/browser-source-read.d.ts +52 -0
  36. package/vendor/connector-sdk/dist/browser-source-read.js +107 -0
  37. package/vendor/connector-sdk/dist/connector-types.d.ts +10 -0
  38. package/vendor/connector-sdk/dist/index.d.ts +3 -0
  39. package/vendor/connector-sdk/dist/index.js +2 -0
  40. package/vendor/connector-sdk/package.json +9 -1
  41. package/vendor/connector-worker/dist/agent-turn/guest-entry.js +15 -6
  42. package/vendor/connector-worker/dist/agent-turn/guest.bundle.js +288 -148
  43. package/vendor/connector-worker/dist/daemon/automation.d.ts +4 -11
  44. package/vendor/connector-worker/dist/daemon/automation.js +6 -6
  45. package/vendor/connector-worker/dist/daemon/client.d.ts +2 -4
  46. package/vendor/connector-worker/dist/daemon/client.js +0 -3
  47. package/vendor/connector-worker/dist/daemon/executor.js +0 -3
  48. package/vendor/connector-worker/dist/daemon/mac-device-daemon.d.ts +4 -3
  49. package/vendor/connector-worker/dist/daemon/mac-device-daemon.js +4 -10
  50. package/vendor/connector-worker/dist/daemon/native-bridge/executor.js +27 -25
  51. package/vendor/connector-worker/dist/daemon/start.js +3 -15
  52. package/vendor/connector-worker/dist/daemon/terminal-failure.js +0 -11
  53. package/vendor/connector-worker/dist/daemon/worker-id.d.ts +17 -0
  54. package/vendor/connector-worker/dist/daemon/worker-id.js +22 -0
  55. package/vendor/connector-worker/dist/mac-device-daemon.js +14 -26
  56. package/vendor/connector-worker/package.json +1 -1
  57. package/vendor/core/dist/capabilities.d.ts +1 -1
  58. package/vendor/core/dist/capabilities.js +0 -1
  59. package/vendor/core/dist/connector-query-errors.d.ts +32 -0
  60. package/vendor/core/dist/connector-query-errors.js +41 -1
  61. package/vendor/core/dist/contracts/tools/manage-conversations.js +2 -2
  62. package/vendor/core/dist/contracts/tools/manage-feeds.d.ts +14 -0
  63. package/vendor/core/dist/contracts/tools/manage-feeds.js +17 -0
  64. package/vendor/core/dist/contracts/worker/protocol.d.ts +8 -131
  65. package/vendor/core/dist/contracts/worker/protocol.js +9 -61
  66. package/vendor/core/dist/device-pin-tombstones.d.ts +44 -0
  67. package/vendor/core/dist/device-pin-tombstones.js +57 -0
  68. package/vendor/core/dist/index.d.ts +2 -1
  69. package/vendor/core/dist/index.js +1 -0
  70. package/vendor/core/dist/logger.js +10 -25
  71. package/vendor/core/dist/utils/diagnostic-scrubber.js +258 -21
  72. package/vendor/core/dist/worker/wire.d.ts +0 -10
  73. package/vendor/core/package.json +6 -6
  74. package/vendor/connector-worker/dist/daemon/device-chat.d.ts +0 -9
  75. package/vendor/connector-worker/dist/daemon/device-chat.js +0 -61
  76. package/vendor/core/dist/contracts/worker/device-chat.d.ts +0 -4
  77. package/vendor/core/dist/contracts/worker/device-chat.js +0 -26
@@ -0,0 +1,240 @@
1
+ /**
2
+ * Pure takeout parsing/formatting helpers for the LinkedIn data-export feeds
3
+ * (`linkedin.ts`, `linkedin-takeout.ts`).
4
+ *
5
+ * ISOLATE-SAFE BY CONTRACT: this module must never import `node:fs`,
6
+ * `node:path`, or any other builtin the isolate prelude does not provide.
7
+ * Connector bundles run in a V8 isolate and `assertIsolateEligible` rejects the
8
+ * WHOLE bundle over one surviving builtin `require`, so an `fs` import here
9
+ * would take down the live browser feeds of every connector that shares these
10
+ * helpers — the LinkedIn regression in Lobu#3392.
11
+ *
12
+ * `node:crypto` is fine: the guest prelude serves it from `global.crypto`
13
+ * (see ISOLATE_PRELUDE_PROVIDED_BUILTINS).
14
+ *
15
+ * The CSV reader is injected into the connector (see its constructor), never
16
+ * imported, so no filesystem code reaches the bundle.
17
+ */
18
+
19
+ import { createHash } from "node:crypto";
20
+ import type { EventEnvelope } from "@lobu/connector-sdk";
21
+
22
+ const DEFAULT_WATERMARK = "1970-01-01T00:00:00.000Z";
23
+ const DEFAULT_BATCH_SIZE = 1000;
24
+
25
+ export interface LocalTakeoutConfig {
26
+ takeout_dir?: string;
27
+ batch_size?: number;
28
+ }
29
+
30
+ export function batchSize(config: LocalTakeoutConfig): number {
31
+ return Math.max(1, Math.min(config.batch_size ?? DEFAULT_BATCH_SIZE, 5000));
32
+ }
33
+
34
+ export function takeBatch<T extends EventEnvelope>(
35
+ events: T[],
36
+ watermark: string | undefined,
37
+ max: number
38
+ ): T[] {
39
+ const after = watermark ?? DEFAULT_WATERMARK;
40
+ return events
41
+ .filter((event) => eventCursor(event) > after)
42
+ .sort((a, b) => eventCursor(a).localeCompare(eventCursor(b)))
43
+ .slice(0, max);
44
+ }
45
+
46
+ export function maxEventCursor(
47
+ events: EventEnvelope[],
48
+ fallback?: string
49
+ ): string {
50
+ return events.reduce(
51
+ (max, event) => (eventCursor(event) > max ? eventCursor(event) : max),
52
+ fallback ?? DEFAULT_WATERMARK
53
+ );
54
+ }
55
+
56
+ // Composite watermark: ISO timestamp + origin_id so events sharing a second
57
+ // still order deterministically. The delimiter is persisted inside the jsonb
58
+ // checkpoint column, so it must NOT be NUL: Postgres rejects NUL in jsonb
59
+ // (unsupported Unicode escape sequence). \u0001 (SOH) sorts before every
60
+ // printable char (preserving timestamp-first ordering) and is jsonb-safe.
61
+ const CURSOR_DELIMITER = "\u0001";
62
+
63
+ function eventCursor(event: EventEnvelope): string {
64
+ return `${event.occurred_at.toISOString()}${CURSOR_DELIMITER}${event.origin_id}`;
65
+ }
66
+
67
+ export function stableId(
68
+ prefix: string,
69
+ parts: Array<string | number | undefined>
70
+ ): string {
71
+ const hash = createHash("sha256")
72
+ .update(parts.map((part) => String(part ?? "")).join("\0"))
73
+ .digest("hex")
74
+ .slice(0, 32);
75
+ return `${prefix}_${hash}`;
76
+ }
77
+
78
+ export function stripHtml(html: string): string {
79
+ return decodeHtml(htmlToPlainText(html))
80
+ .replace(/\n{3,}/g, "\n\n")
81
+ .replace(/[ \t]{2,}/g, " ")
82
+ .trim();
83
+ }
84
+
85
+ function htmlToPlainText(html: string): string {
86
+ let output = "";
87
+ let index = 0;
88
+ const lowerHtml = html.toLowerCase();
89
+
90
+ while (index < html.length) {
91
+ const char = html[index];
92
+ if (char !== "<") {
93
+ output += char;
94
+ index += 1;
95
+ continue;
96
+ }
97
+
98
+ const tagEnd = html.indexOf(">", index + 1);
99
+ if (tagEnd === -1) {
100
+ output += char;
101
+ index += 1;
102
+ continue;
103
+ }
104
+
105
+ const tagName = readTagName(html.slice(index + 1, tagEnd));
106
+ if (tagName === "script" || tagName === "style") {
107
+ const closeTagStart = lowerHtml.indexOf(`</${tagName}`, tagEnd + 1);
108
+ if (closeTagStart === -1) {
109
+ break;
110
+ }
111
+ const closeTagEnd = html.indexOf(">", closeTagStart + tagName.length + 2);
112
+ index = closeTagEnd === -1 ? html.length : closeTagEnd + 1;
113
+ continue;
114
+ }
115
+
116
+ if (tagName === "br" || isBlockBreakTag(tagName)) {
117
+ output += "\n";
118
+ }
119
+ index = tagEnd + 1;
120
+ }
121
+
122
+ return output;
123
+ }
124
+
125
+ function readTagName(tag: string): string {
126
+ let index = tag[0] === "/" ? 1 : 0;
127
+ while (tag[index] === " " || tag[index] === "\t" || tag[index] === "\n") {
128
+ index += 1;
129
+ }
130
+
131
+ let name = "";
132
+ while (index < tag.length) {
133
+ const char = tag[index]?.toLowerCase();
134
+ if (!char || !isAsciiNameChar(char)) {
135
+ break;
136
+ }
137
+ name += char;
138
+ index += 1;
139
+ }
140
+ return name;
141
+ }
142
+
143
+ function isAsciiNameChar(char: string): boolean {
144
+ return (
145
+ (char >= "a" && char <= "z") || (char >= "0" && char <= "9") || char === "-"
146
+ );
147
+ }
148
+
149
+ function isBlockBreakTag(tagName: string): boolean {
150
+ return (
151
+ tagName === "p" ||
152
+ tagName === "div" ||
153
+ tagName === "li" ||
154
+ tagName === "h1" ||
155
+ tagName === "h2" ||
156
+ tagName === "h3" ||
157
+ tagName === "h4" ||
158
+ tagName === "h5" ||
159
+ tagName === "h6"
160
+ );
161
+ }
162
+
163
+ export function decodeHtml(text: string): string {
164
+ return text.replace(
165
+ /&(nbsp|amp|quot|#039|apos|#\d+|#x[0-9a-f]+);/gi,
166
+ (entity, code) => {
167
+ const normalized = String(code).toLowerCase();
168
+ if (normalized === "nbsp") return " ";
169
+ if (normalized === "amp") return "&";
170
+ if (normalized === "quot") return '"';
171
+ if (normalized === "#039" || normalized === "apos") return "'";
172
+ if (normalized.startsWith("#x")) {
173
+ const codePoint = Number.parseInt(normalized.slice(2), 16);
174
+ return decodeHtmlCodePoint(codePoint, entity);
175
+ }
176
+ if (normalized.startsWith("#")) {
177
+ const codePoint = Number(normalized.slice(1));
178
+ return decodeHtmlCodePoint(codePoint, entity);
179
+ }
180
+ return entity;
181
+ }
182
+ );
183
+ }
184
+
185
+ function decodeHtmlCodePoint(codePoint: number, fallback: string): string {
186
+ if (!Number.isInteger(codePoint) || codePoint < 0 || codePoint > 0x10ffff) {
187
+ return fallback;
188
+ }
189
+ if (codePoint === 60 || codePoint === 62) {
190
+ return fallback;
191
+ }
192
+ return String.fromCodePoint(codePoint);
193
+ }
194
+
195
+ export function parseCsv(text: string): Record<string, string>[] {
196
+ const rows: string[][] = [];
197
+ let row: string[] = [];
198
+ let cell = "";
199
+ let inQuotes = false;
200
+
201
+ for (let i = 0; i < text.length; i++) {
202
+ const char = text[i];
203
+ const next = text[i + 1];
204
+ if (char === '"') {
205
+ if (inQuotes && next === '"') {
206
+ cell += '"';
207
+ i++;
208
+ } else {
209
+ inQuotes = !inQuotes;
210
+ }
211
+ } else if (char === "," && !inQuotes) {
212
+ row.push(cell);
213
+ cell = "";
214
+ } else if ((char === "\n" || char === "\r") && !inQuotes) {
215
+ if (char === "\r" && next === "\n") i++;
216
+ row.push(cell);
217
+ if (row.some((value) => value.trim().length > 0)) rows.push(row);
218
+ row = [];
219
+ cell = "";
220
+ } else {
221
+ cell += char;
222
+ }
223
+ }
224
+ row.push(cell);
225
+ if (row.some((value) => value.trim().length > 0)) rows.push(row);
226
+
227
+ const headerIndex = rows.findIndex((candidate) => {
228
+ const values = candidate.map((value) => value.trim()).filter(Boolean);
229
+ return values.length > 1 && !values[0]?.startsWith("Notes:");
230
+ });
231
+ if (headerIndex < 0) return [];
232
+ const headers = rows[headerIndex].map((value) => value.trim());
233
+ return rows.slice(headerIndex + 1).map((values) => {
234
+ const record: Record<string, string> = {};
235
+ headers.forEach((header, index) => {
236
+ record[header] = values[index]?.trim() ?? "";
237
+ });
238
+ return record;
239
+ });
240
+ }
@@ -1,14 +1,14 @@
1
1
  // Serialized into the paired browser. Read only the inbox's own loaded records
2
2
  // and read loaders; never return provider clients, tickets, or arbitrary state.
3
3
  export async function tiktokInboxPage(input) {
4
- const fail = message => { throw new Error(`TikTok inbox: ${message}`); };
4
+ const fail = (message, reason) => { throw Object.assign(new Error(`TikTok inbox: ${message}`), reason ? { browser_interruption: reason } : {}); };
5
5
  const visible = element => !!element?.getClientRects().length;
6
6
  const account = () => {
7
7
  if (location.origin !== "https://www.tiktok.com") fail("Wrong page origin");
8
- if (visible(document.querySelector("#captcha-verify-container-main-page"))) fail("Verification required in the connected browser");
8
+ if (visible(document.querySelector("#captcha-verify-container-main-page"))) fail("Verification required in the connected browser", "challenge_required");
9
9
  const raw = document.querySelector("#__UNIVERSAL_DATA_FOR_REHYDRATION__")?.textContent;
10
10
  const user = raw && JSON.parse(raw).__DEFAULT_SCOPE__?.["webapp.app-context"]?.user;
11
- if (typeof user?.uid !== "string" || !/^\d+$/.test(user.uid)) fail("Sign-in is required");
11
+ if (typeof user?.uid !== "string" || !/^\d+$/.test(user.uid)) fail("Sign-in is required", "login_required");
12
12
  return user.uid;
13
13
  };
14
14
  const owner = account();
@@ -16,12 +16,12 @@ export async function tiktokInboxPage(input) {
16
16
  const pause = () => new Promise(resolve => setTimeout(resolve, 100));
17
17
  const wait = async read => {
18
18
  while (Date.now() < deadline) {
19
- if (account() !== owner) fail("Account changed during the read");
19
+ if (account() !== owner) fail("Account changed during the read", "account_mismatch");
20
20
  const value = read();
21
21
  if (value) return value;
22
22
  await pause();
23
23
  }
24
- fail("Inbox did not finish loading; restart this page read");
24
+ fail("Inbox did not finish loading; restart this page read", "loading_failure");
25
25
  };
26
26
  const readWithinDeadline = async read => {
27
27
  let outcome;
@@ -47,7 +47,7 @@ export async function tiktokInboxPage(input) {
47
47
  const bytes = await crypto.subtle.digest("SHA-256", new TextEncoder().encode(JSON.stringify(row)));
48
48
  return { ...row, source_revision: [...new Uint8Array(bytes)].map(byte => byte.toString(16).padStart(2, "0")).join("") };
49
49
  }));
50
- if (account() !== owner) fail("Account changed during the read");
50
+ if (account() !== owner) fail("Account changed during the read", "account_mismatch");
51
51
  return { rows: revised, hasMore: !!next, ...(next ? { nextCursor: JSON.stringify({ v: feed === "messages" ? 2 : 1, feed, account: owner, ...next }) } : {}) };
52
52
  };
53
53
  // React attaches the source record and loading state to these exact inbox
@@ -162,7 +162,7 @@ export async function tiktokInboxPage(input) {
162
162
  return null;
163
163
  };
164
164
  const api = await wait(clientApi);
165
- if (api.instance.ctx?.option?.userId !== owner) fail("Message client belongs to a different account");
165
+ if (api.instance.ctx?.option?.userId !== owner) fail("Message client belongs to a different account", "account_mismatch");
166
166
  await wait(() => {
167
167
  if (api.stateStore.isInitError || api.stateStore.isNotAvailable) fail("Message initialization failed");
168
168
  return api.stateStore.isInitSucceeded && api.stateStore.isInitLoadDone;
@@ -175,7 +175,7 @@ export async function tiktokInboxPage(input) {
175
175
  const snapshot = [...ids].sort();
176
176
  const finish = async (rows, next) => {
177
177
  const output = await result(rows, next);
178
- if (api.instance.ctx?.option?.userId !== owner) fail("Message client changed accounts during the read");
178
+ if (api.instance.ctx?.option?.userId !== owner) fail("Message client changed accounts during the read", "account_mismatch");
179
179
  const latest = api.instance.getConversationList();
180
180
  if (!Array.isArray(latest) || JSON.stringify(latest.map(thread => thread.id).sort()) !== JSON.stringify(snapshot)) fail("Conversation set changed; restart the read, previous coverage is incomplete");
181
181
  return output;
@@ -204,7 +204,7 @@ export async function tiktokInboxPage(input) {
204
204
  };
205
205
  for (let page = 0; page < 20; page++) {
206
206
  await wait(() => !thread.isPullingMessages);
207
- if (api.instance.ctx?.option?.userId !== owner) fail("Message client changed accounts during the read");
207
+ if (api.instance.ctx?.option?.userId !== owner) fail("Message client changed accounts during the read", "account_mismatch");
208
208
  const messages = thread.getMessageList().map(project).sort((a,b) => b.occurred_at.localeCompare(a.occurred_at) || b.id.localeCompare(a.id));
209
209
  const start = cursor.after ? messages.findIndex(message => message.id === cursor.after) + 1 : 0;
210
210
  if ((cursor.after && !start) || start === messages.length && thread.hasMoreMessages) {
@@ -1,10 +1,11 @@
1
1
  // Self-contained browser adapter. Serialized into the user's paired browser;
2
2
  // keep every dependency inside this function, as in the WhatsApp page adapter.
3
3
  export async function tiktokPage(command, args = {}) {
4
+ const interrupt = (reason, message) => { throw Object.assign(new Error(message), { browser_interruption: reason }); };
4
5
  const visible = (e) => !!e && e.getClientRects().length > 0;
5
6
  const assertNoChallenge = () => {
6
7
  if (visible(document.querySelector("#captcha-verify-container-main-page"))) {
7
- throw new Error("TikTok verification required; complete the challenge in the connected browser");
8
+ interrupt("challenge_required", "TikTok verification required; complete the challenge in the connected browser");
8
9
  }
9
10
  };
10
11
  assertNoChallenge();
@@ -19,27 +20,36 @@ export async function tiktokPage(command, args = {}) {
19
20
  };
20
21
  const wait = async (read) => {
21
22
  for (let i = 0; i < 40; i++) {
23
+ assertNoChallenge();
22
24
  const value = read();
23
25
  if (value) return value;
24
26
  await new Promise(resolve => setTimeout(resolve, 250));
25
27
  }
26
- throw new Error("TikTok page did not expose the requested content; it may be loading, signed out, or unavailable");
28
+ interrupt("loading_failure", "TikTok page did not expose the requested content");
27
29
  };
28
30
  if (command === "identity") {
29
- try { return await wait(identity); } catch { return null; }
31
+ try { return await wait(identity); } catch (error) {
32
+ if (error.browser_interruption !== "loading_failure") throw error;
33
+ return null;
34
+ }
30
35
  }
31
- if (!identity()) throw new Error("TikTok sign-in is required");
32
- const assertFollowing = () => {
33
- if (location.origin !== "https://www.tiktok.com" || location.pathname !== "/following") throw new Error("TikTok Following page changed");
36
+ const owner = identity()?.accountId;
37
+ if (!owner) interrupt("login_required", "TikTok sign-in is required");
38
+ if (command === "read_failure") interrupt("loading_failure", "TikTok page did not finish loading the requested content");
39
+ const assertTimeline = (path) => {
40
+ if (location.origin !== "https://www.tiktok.com" || location.pathname !== path) throw new Error("TikTok timeline page changed");
34
41
  assertNoChallenge();
35
- if (!identity()) throw new Error("TikTok sign-in is required");
42
+ if (!identity()) interrupt("login_required", "TikTok sign-in is required");
43
+ if (identity().accountId !== owner) interrupt("account_mismatch", "TikTok account changed during the read");
36
44
  };
37
- if (command === "following_status" || command === "following_wait") {
38
- assertFollowing();
45
+ const assertFollowing = () => assertTimeline("/following");
46
+ if (["following_status", "following_wait", "for_you_status", "for_you_wait"].includes(command)) {
47
+ const path = command.startsWith("following_") ? "/following" : "/";
48
+ assertTimeline(path);
39
49
  document.querySelectorAll("video,audio").forEach(media => { media.muted = true; });
40
- if (command === "following_wait") {
50
+ if (command.endsWith("_wait")) {
41
51
  await new Promise(resolve => setTimeout(resolve, 250));
42
- assertFollowing();
52
+ assertTimeline(path);
43
53
  }
44
54
  return { ready: true };
45
55
  }
@@ -94,10 +104,6 @@ export async function tiktokPage(command, args = {}) {
94
104
  if (seen.size < args.limit && hasMore) throw new Error(`TikTok Following read incomplete: collected ${seen.size} of ${args.limit} requested posts; retry the read`);
95
105
  return { items: [...items.values()] };
96
106
  }
97
- if (command === "snapshot") {
98
- const d = scope();
99
- return { items: (d["webapp.updated-items"] || []).map(project) };
100
- }
101
107
  if (command === "post_links") {
102
108
  // Hidden preload links appear before search results render. Their presence
103
109
  // satisfies wait_for_selector but cannot establish readable coverage.
@@ -1,4 +1,7 @@
1
1
  import {
2
+ assertInboxSourceRead,
3
+ assertPositiveReadLimit,
4
+ assertSnapshotSourceRead,
2
5
  ConnectorRuntime,
3
6
  requireBrowser,
4
7
  type ActionContext,
@@ -26,10 +29,15 @@ export function normalizeTikTokPostUrl(value: unknown): string {
26
29
  return `${ORIGIN}${url.pathname}`;
27
30
  }
28
31
 
32
+ function hasPostIdentity(item: RecordValue): boolean {
33
+ const author = object(item.author);
34
+ return typeof item.id === "string" && /^[0-9]+$/.test(item.id) && typeof author.uniqueId === "string" && HANDLE.test(author.uniqueId);
35
+ }
36
+
29
37
  export function normalizeTikTokPost(value: unknown): RecordValue {
30
38
  const item = object(value);
31
39
  const author = object(item.author);
32
- if (typeof item.id !== "string" || !/^[0-9]+$/.test(item.id) || typeof author.uniqueId !== "string" || !HANDLE.test(author.uniqueId)) {
40
+ if (!hasPostIdentity(item)) {
33
41
  throw new Error("TikTok returned a post without a valid source identity");
34
42
  }
35
43
  const video = object(item.video);
@@ -49,29 +57,49 @@ export function normalizeTikTokPost(value: unknown): RecordValue {
49
57
  };
50
58
  }
51
59
 
60
+ // Keep structured page failures in a successful CDP value. CDP exceptions only
61
+ // preserve prose, which cannot safely distinguish a challenge from a timeout.
62
+ function pageExpression(source: string): string {
63
+ return `Promise.resolve().then(() => ${source}).catch(error => { if (!error.browser_interruption) throw error; return { __lobu_browser_interruption: { reason: error.browser_interruption, message: error.message } }; })`;
64
+ }
65
+
52
66
  function expression(command: string, args: RecordValue = {}): string {
53
- return `(${tiktokPage.toString()})(${JSON.stringify(command)},${JSON.stringify(args)})`;
67
+ return pageExpression(`(${tiktokPage.toString()})(${JSON.stringify(command)},${JSON.stringify(args)})`);
54
68
  }
55
69
 
56
70
  async function page(browser: ChromeActionDispatcher, tabId: number, command: string, args: RecordValue = {}): Promise<RecordValue> {
57
71
  const output = await browser.dispatch("evaluate", { tab_id: tabId, expression: expression(command, args), allowed_origins: ORIGINS });
72
+ // Only the source-read bridge throws a typed interruption; operation and
73
+ // worker dispatchers return the marker as a completed value.
74
+ const interruption = object(object(output.value).__lobu_browser_interruption);
75
+ if (interruption.reason) throw new Error(String(interruption.message));
58
76
  if (!output.value || typeof output.value !== "object") throw new Error("TikTok page returned no data");
59
77
  return object(output.value);
60
78
  }
61
79
 
62
80
  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 });
81
+ const nav = await browser.dispatch("navigate", { url: "about:blank", new_tab: true, retain_on_interruption: true, allowed_origins: ORIGINS });
64
82
  if (typeof nav.tab_id !== "number") throw new Error("TikTok scratch tab was not created");
65
83
  try { return await run(nav.tab_id); }
66
84
  finally { await browser.dispatch("close_tab", { tab_id: nav.tab_id, allowed_origins: ORIGINS }); }
67
85
  }
68
86
 
87
+ async function waitFor(browser: ChromeActionDispatcher, tabId: number, selector: string): Promise<void> {
88
+ try { await browser.dispatch("wait_for_selector", { tab_id: tabId, selector, timeout_ms: 15000, allowed_origins: ORIGINS }); }
89
+ catch (error) {
90
+ // Inspect the page while it is still owned. A selector timeout alone is
91
+ // neither verification evidence nor permission for a recovery handoff.
92
+ await page(browser, tabId, "read_failure");
93
+ throw error;
94
+ }
95
+ }
96
+
69
97
  async function navigate(browser: ChromeActionDispatcher, tabId: number, url: string, focus = false): Promise<void> {
70
98
  await browser.dispatch("navigate", { tab_id: tabId, url, allowed_origins: ORIGINS });
71
99
  // TikTok's video page can remain blank in a fresh background tab. Its own
72
100
  // controls appeared immediately after focus in the live browser probe.
73
101
  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 });
102
+ await waitFor(browser, tabId, '[data-e2e="nav-profile"]');
75
103
  }
76
104
 
77
105
  async function sourcePost(browser: ChromeActionDispatcher, tabId: number, url: string): Promise<RecordValue> {
@@ -83,7 +111,7 @@ async function sourcePost(browser: ChromeActionDispatcher, tabId: number, url: s
83
111
  if (typeof start.session_id !== "string") throw new Error("TikTok photo capture did not start");
84
112
  try {
85
113
  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 });
114
+ await waitFor(browser, tabId, '[data-e2e="recommend-list-item-container"] .swiper');
87
115
  const data = await browser.dispatch("network_intercept_drain", { session_id: start.session_id, allowed_origins: ORIGINS });
88
116
  for (const response of Array.isArray(data.responses) ? data.responses : []) {
89
117
  const r = object(response);
@@ -98,25 +126,31 @@ async function sourcePost(browser: ChromeActionDispatcher, tabId: number, url: s
98
126
  }
99
127
  }
100
128
 
101
- async function followingPosts(browser: ChromeActionDispatcher, tabId: number, sessionId: string, limit: number): Promise<RecordValue[]> {
129
+ async function timelinePosts(browser: ChromeActionDispatcher, tabId: number, sessionId: string, limit: number, feedKey: "for_you" | "following"): Promise<RecordValue[]> {
130
+ const endpoint = feedKey === "following" ? "following" : "recommend";
131
+ const label = feedKey === "following" ? "Following" : "For You";
102
132
  const unique = new Map<unknown, RecordValue>();
103
133
  let continuation: RecordValue | undefined;
104
134
  for (let poll = 0; poll < 5; poll++) {
105
- await page(browser, tabId, poll ? "following_wait" : "following_status");
135
+ await page(browser, tabId, `${feedKey}_${poll ? "wait" : "status"}`);
106
136
  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");
137
+ if (data.missing || !Array.isArray(data.responses)) throw new Error(`TikTok ${label} capture is unavailable`);
108
138
  for (const response of data.responses) {
109
139
  const r = object(response);
110
140
  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");
141
+ if (url.origin !== ORIGIN || url.pathname !== `/api/${endpoint}/item_list/` || url.username || url.password) throw new Error(`TikTok ${label} capture has an unexpected URL`);
142
+ if (r.status !== 200 || r.truncated || r.base64_encoded || typeof r.body !== "string") throw new Error(`TikTok ${label} response is unavailable or truncated`);
113
143
  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");
144
+ 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 ${label} feed`);
115
145
  for (const item of json.itemList) {
146
+ const record = object(item);
147
+ // Some recommendation ads use an advertiser label in uniqueId,
148
+ // not a creator handle. They cannot produce a canonical post URL.
149
+ if (feedKey === "for_you" && record.isAd === true && !hasPostIdentity(record)) continue;
116
150
  const row = normalizeTikTokPost(item);
117
151
  unique.set(row.origin_id, row);
118
152
  }
119
- continuation = json.hasMore ? { url: url.href, cursor: json.cursor, level: json.level } : undefined;
153
+ continuation = feedKey === "following" && json.hasMore ? { url: url.href, cursor: json.cursor, level: json.level } : undefined;
120
154
  }
121
155
  if (data.responses.length) break;
122
156
  }
@@ -131,14 +165,14 @@ async function followingPosts(browser: ChromeActionDispatcher, tabId: number, se
131
165
  }
132
166
  }
133
167
  // Do not return a successful partial read when a late challenge appeared.
134
- await page(browser, tabId, "following_status");
168
+ await page(browser, tabId, `${feedKey}_status`);
135
169
  return [...unique.values()];
136
170
  }
137
171
 
138
172
  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");
173
+ assertSnapshotSourceRead(ctx, "TikTok");
140
174
  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");
175
+ assertPositiveReadLimit(ctx);
142
176
  const limit = Math.min(ctx.limit ?? 10, 30);
143
177
  let url = ORIGIN;
144
178
  if (ctx.feedKey === "following") url += "/following";
@@ -151,28 +185,26 @@ async function readTikTokSnapshot(ctx: FeedReadContext): Promise<FeedReadResult>
151
185
  return scratch(browser, async tabId => {
152
186
  let sessionId: string | undefined;
153
187
  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 });
188
+ if (ctx.feedKey !== "search") {
189
+ const endpoint = ctx.feedKey === "following" ? "following" : "recommend";
190
+ const start = await browser.dispatch("network_intercept_start", { tab_id: tabId, patterns: [`${ORIGIN}/api/${endpoint}/item_list/**`], max_body_bytes: 2097152, max_buffer_responses: 10, allowed_origins: ORIGINS });
156
191
  if (typeof start.session_id !== "string") throw new Error("TikTok feed capture did not start");
157
192
  sessionId = start.session_id;
158
193
  }
159
194
  // Snapshot/search results also lazy-load only in a visible tab.
160
195
  await navigate(browser, tabId, url, true);
161
196
  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 });
197
+ if (ctx.feedKey === "following" || ctx.feedKey === "for_you") {
198
+ await waitFor(browser, tabId, '[data-e2e="recommend-list-item-container"]');
199
+ rows = await timelinePosts(browser, tabId, sessionId!, limit, ctx.feedKey);
200
+ } else {
201
+ await waitFor(browser, tabId, 'a[href*="/video/"],a[href*="/photo/"]');
167
202
  const data = await page(browser, tabId, "post_links");
168
203
  rows = (Array.isArray(data.rows) ? data.rows : []).map(value => {
169
204
  const r = object(value);
170
205
  const sourceUrl = normalizeTikTokPostUrl(r.url);
171
206
  return { origin_id: sourceUrl.split("/").pop(), source_url: sourceUrl, text: r.text, media: { type: sourceUrl.includes("/photo/") ? "photos" : "video" } };
172
207
  });
173
- } else {
174
- const data = await page(browser, tabId, "snapshot");
175
- rows = (Array.isArray(data.items) ? data.items : []).map(normalizeTikTokPost);
176
208
  }
177
209
  if (!rows.length) throw new Error("TikTok returned no verifiable posts; this is not proof of an empty feed");
178
210
  const unique = [...new Map(rows.map(row => [row.origin_id, row])).values()];
@@ -186,18 +218,19 @@ async function readTikTokSnapshot(ctx: FeedReadContext): Promise<FeedReadResult>
186
218
  }
187
219
 
188
220
  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");
221
+ assertInboxSourceRead(ctx, "TikTok");
222
+ assertPositiveReadLimit(ctx);
191
223
  const browser = requireBrowser(ctx);
192
224
  return scratch(browser, async tabId => {
193
225
  await navigate(browser, tabId, ORIGIN);
194
226
  const result = await browser.dispatch("evaluate", {
195
227
  tab_id: tabId,
196
- expression: `(${tiktokInboxPage.toString()})(${JSON.stringify({ feed: ctx.feedKey, limit: Math.min(ctx.limit ?? 20, 100), cursor: ctx.cursor })})`,
228
+ expression: pageExpression(`(${tiktokInboxPage.toString()})(${JSON.stringify({ feed: ctx.feedKey, limit: Math.min(ctx.limit ?? 20, 100), cursor: ctx.cursor })})`),
197
229
  await_promise: true,
198
230
  allowed_origins: ORIGINS,
199
231
  });
200
232
  const data = object(result.value);
233
+ if (data.__lobu_browser_interruption) throw new Error(String(object(data.__lobu_browser_interruption).message));
201
234
  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
235
  return data as unknown as FeedReadResult;
203
236
  });
@@ -205,14 +238,14 @@ async function readTikTokInbox(ctx: FeedReadContext): Promise<FeedReadResult> {
205
238
 
206
239
  export default class TikTokConnector extends ConnectorRuntime {
207
240
  readonly definition: RuntimeConnectorDefinition = {
208
- key: "tiktok.web", name: "TikTok", version: "1.1.5", faviconDomain: "tiktok.com",
241
+ key: "tiktok.web", name: "TikTok", version: "1.1.9", faviconDomain: "tiktok.com",
209
242
  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
243
  browser: { origins: ORIGINS, authMethods: ["browser"], accountProbe: { url: ORIGIN, expression: expression("identity") } },
211
244
  authSchema: { methods: [{ type: "browser", mode: "live" }] },
212
245
  feeds: {
213
246
  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
247
  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 },
248
+ for_you: { key: "for_you", name: "For You", description: "Initial personalized timeline snapshot. Ad units without canonical post identity are excluded. No history coverage or continuation; repeat reads can overlap.", read: readTikTokSnapshot },
216
249
  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
250
  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
251
  },