@lobu/runtime-device 21.2.1 → 23.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (233) hide show
  1. package/dist/component.json +1 -1
  2. package/dist/dependencies.bun.lock +91 -83
  3. package/npm-shrinkwrap.json +492 -438
  4. package/package.json +12 -2
  5. package/vendor/cli/dist/bundled-skills/lobu/SKILL.md +1 -1
  6. package/vendor/cli/dist/commands/_lib/apply/apply-cmd.js +5 -6
  7. package/vendor/cli/dist/commands/_lib/apply/client.d.ts +1 -0
  8. package/vendor/cli/dist/commands/_lib/apply/client.js +26 -7
  9. package/vendor/cli/dist/commands/_lib/apply/desired-state.d.ts +9 -1
  10. package/vendor/cli/dist/commands/_lib/apply/diff.d.ts +1 -0
  11. package/vendor/cli/dist/commands/_lib/apply/diff.js +46 -4
  12. package/vendor/cli/dist/commands/_lib/apply/map-config.js +18 -38
  13. package/vendor/cli/dist/commands/_lib/init-from-org/bootstrap.js +5 -17
  14. package/vendor/cli/dist/commands/init.d.ts +0 -2
  15. package/vendor/cli/dist/commands/init.js +0 -27
  16. package/vendor/cli/dist/commands/login.d.ts +17 -0
  17. package/vendor/cli/dist/commands/login.js +43 -17
  18. package/vendor/cli/dist/commands/memory/_lib/errors.d.ts +0 -8
  19. package/vendor/cli/dist/commands/memory/_lib/errors.js +0 -12
  20. package/vendor/cli/dist/commands/memory/_lib/mcp.js +6 -6
  21. package/vendor/cli/dist/commands/memory/_lib/seed-cmd.js +4 -10
  22. package/vendor/cli/dist/config/define.d.ts +21 -15
  23. package/vendor/cli/dist/config/define.js +2 -2
  24. package/vendor/cli/dist/connectors/README.md +21 -6
  25. package/vendor/cli/dist/connectors/_google/actions.ts +270 -0
  26. package/vendor/cli/dist/connectors/_google/discovery/calendar_v3.json +1 -0
  27. package/vendor/cli/dist/connectors/_google/discovery/chat_v1.json +1 -0
  28. package/vendor/cli/dist/connectors/_google/discovery/docs_v1.json +1 -0
  29. package/vendor/cli/dist/connectors/_google/discovery/drive_v3.json +1 -0
  30. package/vendor/cli/dist/connectors/_google/discovery/gmail_v1.json +1 -0
  31. package/vendor/cli/dist/connectors/_google/discovery/people_v1.json +1 -0
  32. package/vendor/cli/dist/connectors/_google/discovery/sheets_v4.json +1 -0
  33. package/vendor/cli/dist/connectors/_google/discovery/tasks_v1.json +1 -0
  34. package/vendor/cli/dist/connectors/_google/discovery/youtube_v3.json +1 -0
  35. package/vendor/cli/dist/connectors/_google/discovery.ts +442 -0
  36. package/vendor/cli/dist/connectors/_google/errors.ts +91 -0
  37. package/vendor/cli/dist/connectors/_google/list-feed.ts +223 -0
  38. package/vendor/cli/dist/connectors/_google/policies.ts +127 -0
  39. package/vendor/cli/dist/connectors/agentid.ts +43 -0
  40. package/vendor/cli/dist/connectors/chat-user-identity.ts +2 -1
  41. package/vendor/cli/dist/connectors/github-automation-events.ts +9 -0
  42. package/vendor/cli/dist/connectors/github-identity.ts +28 -3
  43. package/vendor/cli/dist/connectors/github.ts +101 -59
  44. package/vendor/cli/dist/connectors/google_calendar.ts +207 -719
  45. package/vendor/cli/dist/connectors/google_drive.ts +16 -2
  46. package/vendor/cli/dist/connectors/google_gmail.ts +52 -33
  47. package/vendor/cli/dist/connectors/herdr.ts +406 -0
  48. package/vendor/cli/dist/connectors/jira-helpers.ts +313 -0
  49. package/vendor/cli/dist/connectors/jira.ts +35 -227
  50. package/vendor/cli/dist/connectors/linear.ts +7 -4
  51. package/vendor/cli/dist/connectors/linkedin-browser-script.ts +13 -0
  52. package/vendor/cli/dist/connectors/linkedin-draft-page.js +104 -0
  53. package/vendor/cli/dist/connectors/linkedin-drafts.ts +73 -0
  54. package/vendor/cli/dist/connectors/linkedin-identity.ts +32 -0
  55. package/vendor/cli/dist/connectors/linkedin-inbox-adapter.js +1185 -0
  56. package/vendor/cli/dist/connectors/linkedin-inbox.ts +156 -0
  57. package/vendor/cli/dist/connectors/linkedin-like.ts +114 -0
  58. package/vendor/cli/dist/connectors/linkedin-takeout.ts +577 -0
  59. package/vendor/cli/dist/connectors/linkedin.ts +3998 -0
  60. package/vendor/cli/dist/connectors/postgres.ts +27 -2
  61. package/vendor/cli/dist/connectors/takeout-utils.ts +240 -0
  62. package/vendor/cli/dist/connectors/tiktok-inbox-page.js +230 -0
  63. package/vendor/cli/dist/connectors/tiktok-web-page.js +217 -0
  64. package/vendor/cli/dist/connectors/tiktok_web.ts +319 -0
  65. package/vendor/cli/dist/connectors/whatsapp-web-adapter.js +285 -567
  66. package/vendor/cli/dist/connectors/whatsapp-web-helpers.ts +74 -455
  67. package/vendor/cli/dist/connectors/whatsapp-web-identity.ts +135 -0
  68. package/vendor/cli/dist/connectors/whatsapp_web.ts +159 -618
  69. package/vendor/cli/dist/connectors/x-dm-adapter.js +556 -0
  70. package/vendor/cli/dist/connectors/x-identity.ts +31 -2
  71. package/vendor/cli/dist/connectors/x.ts +539 -253
  72. package/vendor/cli/dist/index.js +1 -37
  73. package/vendor/cli/dist/internal/context.d.ts +10 -0
  74. package/vendor/cli/dist/internal/context.js +26 -16
  75. package/vendor/cli/dist/internal/node-version.d.ts +1 -1
  76. package/vendor/cli/dist/internal/node-version.js +1 -1
  77. package/vendor/cli/dist/internal/oauth.d.ts +3 -1
  78. package/vendor/cli/dist/internal/oauth.js +4 -3
  79. package/vendor/cli/dist/internal/runtime-components.d.ts +9 -0
  80. package/vendor/cli/dist/internal/runtime-components.js +67 -2
  81. package/vendor/cli/dist/mac-auth.js +17 -1
  82. package/vendor/cli/dist/providers.json +1 -1
  83. package/vendor/cli/dist/runtime-components.json +4 -4
  84. package/vendor/cli/dist/templates/AGENTS.md.tmpl +12 -5
  85. package/vendor/cli/dist/templates/README.md.tmpl +1 -1
  86. package/vendor/cli/dist/vendor/ora/.bun-tag-fde20e9a6df212fa +0 -0
  87. package/vendor/cli/dist/vendor/ora/index.d.ts +332 -0
  88. package/vendor/cli/dist/vendor/ora/index.js +424 -0
  89. package/vendor/cli/dist/vendor/ora/license +9 -0
  90. package/vendor/cli/dist/vendor/ora/package.json +64 -0
  91. package/vendor/cli/dist/vendor/ora/readme.md +329 -0
  92. package/vendor/cli/package.json +2 -2
  93. package/vendor/connector-sdk/dist/browser-handoff-banner.d.ts +39 -0
  94. package/vendor/connector-sdk/dist/browser-handoff-banner.js +175 -0
  95. package/vendor/connector-sdk/dist/browser-requirement.d.ts +36 -0
  96. package/vendor/connector-sdk/dist/browser-requirement.js +60 -0
  97. package/vendor/connector-sdk/dist/browser-source-read.d.ts +52 -0
  98. package/vendor/connector-sdk/dist/browser-source-read.js +107 -0
  99. package/vendor/connector-sdk/dist/connector-runtime.d.ts +3 -1
  100. package/vendor/connector-sdk/dist/connector-runtime.js +10 -0
  101. package/vendor/connector-sdk/dist/connector-types.d.ts +169 -45
  102. package/vendor/connector-sdk/dist/define-connector.d.ts +4 -2
  103. package/vendor/connector-sdk/dist/define-connector.js +3 -0
  104. package/vendor/connector-sdk/dist/index.d.ts +8 -2
  105. package/vendor/connector-sdk/dist/index.js +3 -0
  106. package/vendor/connector-sdk/dist/reaction-client-types.d.ts +16 -21
  107. package/vendor/connector-sdk/dist/reaction-client-types.typecheck.d.ts +5 -0
  108. package/vendor/connector-sdk/package.json +13 -1
  109. package/vendor/connector-worker/dist/agent-turn/guest-entry.js +43 -30
  110. package/vendor/connector-worker/dist/agent-turn/guest.bundle.js +416 -330
  111. package/vendor/connector-worker/dist/agent-turn/memory.d.ts +3 -2
  112. package/vendor/connector-worker/dist/agent-turn/memory.js +3 -20
  113. package/vendor/connector-worker/dist/agent-turn/types.d.ts +3 -15
  114. package/vendor/connector-worker/dist/agent-turn/workspace.d.ts +2 -2
  115. package/vendor/connector-worker/dist/agent-turn/workspace.js +2 -4
  116. package/vendor/connector-worker/dist/daemon/action-monitor.d.ts +12 -0
  117. package/vendor/connector-worker/dist/daemon/action-monitor.js +25 -0
  118. package/vendor/connector-worker/dist/daemon/agent-turn.js +0 -9
  119. package/vendor/connector-worker/dist/daemon/automation.d.ts +4 -11
  120. package/vendor/connector-worker/dist/daemon/automation.js +6 -6
  121. package/vendor/connector-worker/dist/daemon/builtins/index.d.ts +12 -0
  122. package/vendor/connector-worker/dist/daemon/builtins/index.js +61 -1
  123. package/vendor/connector-worker/dist/daemon/builtins/os-shell.d.ts +2 -1
  124. package/vendor/connector-worker/dist/daemon/builtins/os-shell.js +40 -27
  125. package/vendor/connector-worker/dist/daemon/client.d.ts +6 -4
  126. package/vendor/connector-worker/dist/daemon/client.js +3 -3
  127. package/vendor/connector-worker/dist/daemon/device-concurrency.d.ts +8 -0
  128. package/vendor/connector-worker/dist/daemon/device-concurrency.js +13 -0
  129. package/vendor/connector-worker/dist/daemon/executor.d.ts +1 -1
  130. package/vendor/connector-worker/dist/daemon/executor.js +48 -120
  131. package/vendor/connector-worker/dist/daemon/log.d.ts +2 -0
  132. package/vendor/connector-worker/dist/daemon/log.js +23 -2
  133. package/vendor/connector-worker/dist/daemon/mac-device-daemon.d.ts +23 -6
  134. package/vendor/connector-worker/dist/daemon/mac-device-daemon.js +57 -23
  135. package/vendor/connector-worker/dist/daemon/native-bridge/client.d.ts +6 -0
  136. package/vendor/connector-worker/dist/daemon/native-bridge/client.js +15 -0
  137. package/vendor/connector-worker/dist/daemon/native-bridge/executor.js +29 -25
  138. package/vendor/connector-worker/dist/daemon/native-bridge/protocol.d.ts +1 -1
  139. package/vendor/connector-worker/dist/daemon/native-bridge/protocol.js +1 -0
  140. package/vendor/connector-worker/dist/daemon/poll-loop.d.ts +3 -0
  141. package/vendor/connector-worker/dist/daemon/poll-loop.js +5 -1
  142. package/vendor/connector-worker/dist/daemon/start.d.ts +1 -0
  143. package/vendor/connector-worker/dist/daemon/start.js +15 -20
  144. package/vendor/connector-worker/dist/daemon/terminal-failure.js +0 -11
  145. package/vendor/connector-worker/dist/daemon/worker-id.d.ts +17 -0
  146. package/vendor/connector-worker/dist/daemon/worker-id.js +22 -0
  147. package/vendor/connector-worker/dist/executor/interface.d.ts +34 -2
  148. package/vendor/connector-worker/dist/executor/isolate.js +66 -15
  149. package/vendor/connector-worker/dist/executor/redact.d.ts +1 -0
  150. package/vendor/connector-worker/dist/executor/redact.js +97 -26
  151. package/vendor/connector-worker/dist/executor/runtime.d.ts +7 -0
  152. package/vendor/connector-worker/dist/executor/runtime.js +26 -0
  153. package/vendor/connector-worker/dist/isolate/bridge.js +3 -1
  154. package/vendor/connector-worker/dist/mac-device-daemon.js +24 -29
  155. package/vendor/connector-worker/package.json +2 -2
  156. package/vendor/core/dist/agent-policy.js +1 -1
  157. package/vendor/core/dist/agent-store.d.ts +2 -0
  158. package/vendor/core/dist/agent-tooling.d.ts +1 -1
  159. package/vendor/core/dist/agent-tooling.js +1 -1
  160. package/vendor/core/dist/capabilities.d.ts +1 -1
  161. package/vendor/core/dist/capabilities.js +2 -1
  162. package/vendor/core/dist/connector-query-errors.d.ts +32 -0
  163. package/vendor/core/dist/connector-query-errors.js +41 -1
  164. package/vendor/core/dist/contracts/agent-settings.d.ts +0 -11
  165. package/vendor/core/dist/contracts/agent-settings.js +1 -7
  166. package/vendor/core/dist/contracts/tools/collection-presentation.d.ts +93 -0
  167. package/vendor/core/dist/contracts/tools/collection-presentation.js +88 -0
  168. package/vendor/core/dist/contracts/tools/collection-selection.d.ts +47 -0
  169. package/vendor/core/dist/contracts/tools/collection-selection.js +74 -0
  170. package/vendor/core/dist/contracts/tools/manage-agents.d.ts +10 -4
  171. package/vendor/core/dist/contracts/tools/manage-auth-profiles.d.ts +1 -1
  172. package/vendor/core/dist/contracts/tools/manage-auth-profiles.js +5 -1
  173. package/vendor/core/dist/contracts/tools/manage-automations.d.ts +19 -4
  174. package/vendor/core/dist/contracts/tools/manage-automations.js +25 -8
  175. package/vendor/core/dist/contracts/tools/manage-connections.d.ts +1 -0
  176. package/vendor/core/dist/contracts/tools/manage-connections.js +3 -0
  177. package/vendor/core/dist/contracts/tools/manage-conversations.js +2 -2
  178. package/vendor/core/dist/contracts/tools/manage-entity-schema.d.ts +11 -0
  179. package/vendor/core/dist/contracts/tools/manage-entity-schema.js +42 -3
  180. package/vendor/core/dist/contracts/tools/manage-entity.d.ts +181 -152
  181. package/vendor/core/dist/contracts/tools/manage-entity.js +118 -162
  182. package/vendor/core/dist/contracts/tools/manage-feeds.d.ts +14 -0
  183. package/vendor/core/dist/contracts/tools/manage-feeds.js +17 -0
  184. package/vendor/core/dist/contracts/tools/manage-operations.d.ts +41 -3
  185. package/vendor/core/dist/contracts/tools/manage-operations.js +40 -2
  186. package/vendor/core/dist/contracts/tools/manage-views.d.ts +182 -7
  187. package/vendor/core/dist/contracts/tools/manage-views.js +49 -13
  188. package/vendor/core/dist/contracts/tools/read-knowledge.d.ts +11 -2
  189. package/vendor/core/dist/contracts/tools/read-knowledge.js +23 -3
  190. package/vendor/core/dist/contracts/tools/save-memory.d.ts +3 -6
  191. package/vendor/core/dist/contracts/tools/save-memory.js +2 -6
  192. package/vendor/core/dist/contracts/tools/view-attach.d.ts +2 -1
  193. package/vendor/core/dist/contracts/tools/view-attach.js +7 -2
  194. package/vendor/core/dist/contracts/worker/protocol.d.ts +130 -159
  195. package/vendor/core/dist/contracts/worker/protocol.js +88 -76
  196. package/vendor/core/dist/device-pin-tombstones.d.ts +44 -0
  197. package/vendor/core/dist/device-pin-tombstones.js +57 -0
  198. package/vendor/core/dist/index.d.ts +5 -5
  199. package/vendor/core/dist/index.js +4 -6
  200. package/vendor/core/dist/json-template.d.ts +6 -10
  201. package/vendor/core/dist/json-template.js +5 -9
  202. package/vendor/core/dist/logger.js +17 -26
  203. package/vendor/core/dist/shell-safety.d.ts +9 -0
  204. package/vendor/core/dist/shell-safety.js +154 -0
  205. package/vendor/core/dist/suggestions.d.ts +1 -1
  206. package/vendor/core/dist/suggestions.js +1 -1
  207. package/vendor/core/dist/tool-trace-summary.d.ts +1 -1
  208. package/vendor/core/dist/tool-trace-summary.js +1 -1
  209. package/vendor/core/dist/types.d.ts +1 -3
  210. package/vendor/core/dist/utils/diagnostic-scrubber.d.ts +7 -0
  211. package/vendor/core/dist/utils/diagnostic-scrubber.js +437 -0
  212. package/vendor/core/dist/worker/auth.d.ts +44 -6
  213. package/vendor/core/dist/worker/auth.js +44 -11
  214. package/vendor/core/dist/worker/wire.d.ts +0 -10
  215. package/vendor/core/package.json +20 -11
  216. package/vendor/ora/.bun-tag-fde20e9a6df212fa +0 -0
  217. package/vendor/ora/index.d.ts +332 -0
  218. package/vendor/ora/index.js +424 -0
  219. package/vendor/ora/license +9 -0
  220. package/vendor/ora/package.json +53 -0
  221. package/vendor/ora/readme.md +329 -0
  222. package/vendor/cli/dist/commands/telemetry.d.ts +0 -10
  223. package/vendor/cli/dist/commands/telemetry.js +0 -66
  224. package/vendor/connector-worker/dist/daemon/device-chat.d.ts +0 -9
  225. package/vendor/connector-worker/dist/daemon/device-chat.js +0 -61
  226. package/vendor/core/dist/contracts/worker/device-chat.d.ts +0 -4
  227. package/vendor/core/dist/contracts/worker/device-chat.js +0 -26
  228. package/vendor/core/dist/sentry.d.ts +0 -26
  229. package/vendor/core/dist/sentry.js +0 -133
  230. package/vendor/core/dist/tool-policy.d.ts +0 -74
  231. package/vendor/core/dist/tool-policy.js +0 -305
  232. package/vendor/core/dist/utils/sentry-scrubber.d.ts +0 -29
  233. package/vendor/core/dist/utils/sentry-scrubber.js +0 -228
@@ -354,7 +354,7 @@ export default class PostgresConnector extends ConnectorRuntime {
354
354
  description:
355
355
  'Bring your own PostgreSQL database as memory, read a configured feed directly, or run governed connection-level SQL.',
356
356
  version: '1.1.0',
357
- queryCapabilities: { exactMatch: true },
357
+ queryCapabilities: { exactMatch: true, selection: true },
358
358
  faviconDomain: 'postgresql.org',
359
359
  authSchema: {
360
360
  methods: [
@@ -588,11 +588,36 @@ export default class PostgresConnector extends ConnectorRuntime {
588
588
  });
589
589
  where = `WHERE CASE ${cases.join(' ')} ELSE FALSE END`;
590
590
  }
591
+ if (ctx.selection?.search || ctx.selection?.filters?.length) {
592
+ const projection = await tx.unsafe(`SELECT * FROM (\n${baseSql}\n) q LIMIT 0`);
593
+ const names = new Set(projection.columns.map((column) => column.name));
594
+ const requireColumn = (field: string) => {
595
+ if (!names.has(field)) throw Object.assign(new Error(`Unknown selection attribute: ${field}`), { status: 400 });
596
+ };
597
+ const predicates: string[] = [];
598
+ if (ctx.selection.search) {
599
+ requireColumn('name');
600
+ const escaped = ctx.selection.search.replace(/[!%_]/g, '!$&');
601
+ predicates.push(`q."name"::text ILIKE ${bind(`%${escaped}%`)} ESCAPE '!'`);
602
+ }
603
+ for (const filter of ctx.selection.filters ?? []) {
604
+ requireColumn(filter.field);
605
+ const actual = `COALESCE(to_jsonb(q) -> ${bind(filter.field)}, 'null'::jsonb)`;
606
+ const expected = `${bind(JSON.stringify(filter.value))}::jsonb`;
607
+ if (filter.op === 'eq' || filter.op === 'neq') {
608
+ predicates.push(`${actual} ${filter.op === 'eq' ? '=' : '<>'} ${expected}`);
609
+ } else {
610
+ const operator = { lt: '<', lte: '<=', gt: '>', gte: '>=' }[filter.op];
611
+ predicates.push(`(jsonb_typeof(${actual}) = jsonb_typeof(${expected}) AND ${actual} ${operator} ${expected})`);
612
+ }
613
+ }
614
+ if (predicates.length) where += `${where ? ' AND' : 'WHERE'} ${predicates.join(' AND ')}`;
615
+ }
591
616
  const wrapped = `SELECT * FROM (\n${baseSql}\n) q\n${where}\n${orderBy}\nLIMIT ${limit} OFFSET ${offset}`;
592
617
  const rows = await tx.unsafe(wrapped, params);
593
618
  // Record lookup needs only its bounded result, never a full-source count.
594
619
  if (ctx.exactMatch) return { data: rows, total: undefined };
595
- const counted = (await tx.unsafe(countSql)) as unknown as Array<{ n: number }>;
620
+ const counted = (await tx.unsafe(`${countSql}\n${where}`, params)) as unknown as Array<{ n: number }>;
596
621
  return { data: rows, total: counted[0]?.n };
597
622
  })) as unknown as {
598
623
  data: Array<Record<string, unknown>> & { columns?: Array<{ name: string; type: number }> };
@@ -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
+ }
@@ -0,0 +1,230 @@
1
+ // Serialized into the paired browser. Read only the inbox's own loaded records
2
+ // and read loaders; never return provider clients, tickets, or arbitrary state.
3
+ export async function tiktokInboxPage(input) {
4
+ const fail = (message, reason) => { throw Object.assign(new Error(`TikTok inbox: ${message}`), reason ? { browser_interruption: reason } : {}); };
5
+ const visible = element => !!element?.getClientRects().length;
6
+ const account = () => {
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", "challenge_required");
9
+ const raw = document.querySelector("#__UNIVERSAL_DATA_FOR_REHYDRATION__")?.textContent;
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", "login_required");
12
+ return user.uid;
13
+ };
14
+ const owner = account();
15
+ const deadline = Date.now() + 20000;
16
+ const pause = () => new Promise(resolve => setTimeout(resolve, 100));
17
+ const wait = async read => {
18
+ while (Date.now() < deadline) {
19
+ if (account() !== owner) fail("Account changed during the read", "account_mismatch");
20
+ const value = read();
21
+ if (value) return value;
22
+ await pause();
23
+ }
24
+ fail("Inbox did not finish loading; restart this page read", "loading_failure");
25
+ };
26
+ const readWithinDeadline = async read => {
27
+ let outcome;
28
+ Promise.resolve().then(read).then(value => { outcome = { value }; }, error => { outcome = { error }; });
29
+ const settled = await wait(() => outcome);
30
+ if ("error" in settled) throw settled.error;
31
+ return settled.value;
32
+ };
33
+ const feed = input.feed;
34
+ if (!["messages", "activity"].includes(feed)) fail("Unsupported feed");
35
+ const limit = input.limit ?? 20;
36
+ if (!Number.isInteger(limit) || limit < 1 || limit > 100) fail("limit must be between 1 and 100");
37
+ let cursor = {};
38
+ if (input.cursor !== undefined) {
39
+ if (typeof input.cursor !== "string" || input.cursor.length > 100000) fail("Invalid cursor");
40
+ try { cursor = JSON.parse(input.cursor); } catch { fail("Invalid cursor"); }
41
+ if (!cursor || cursor.v !== (feed === "messages" ? 2 : 1) || cursor.feed !== feed || cursor.account !== owner) fail("Cursor belongs to a different source or account");
42
+ if (cursor.after !== undefined && (typeof cursor.after !== "string" || !/^\d+$/.test(cursor.after))) fail("Invalid message or activity cursor");
43
+ if (feed === "messages" && (typeof cursor.thread !== "string" || !/^[0-9:]{1,150}$/.test(cursor.thread))) fail("Invalid conversation cursor");
44
+ }
45
+ const result = async (rows, next) => {
46
+ const revised = await Promise.all(rows.map(async row => {
47
+ const bytes = await crypto.subtle.digest("SHA-256", new TextEncoder().encode(JSON.stringify(row)));
48
+ return { ...row, source_revision: [...new Uint8Array(bytes)].map(byte => byte.toString(16).padStart(2, "0")).join("") };
49
+ }));
50
+ if (account() !== owner) fail("Account changed during the read", "account_mismatch");
51
+ return { rows: revised, hasMore: !!next, ...(next ? { nextCursor: JSON.stringify({ v: feed === "messages" ? 2 : 1, feed, account: owner, ...next }) } : {}) };
52
+ };
53
+ // React attaches the source record and loading state to these exact inbox
54
+ // components. Match their shape, not minified component names or hook indexes.
55
+ function* fibers(element) {
56
+ let fiber = element?.[Object.keys(element).find(key => key.startsWith("__reactFiber$"))];
57
+ // DOM references can retain the alternate after an immutable React commit.
58
+ // Resolve membership in the committed tree, never prefer an alternate blindly.
59
+ const nodeBudget = 50000;
60
+ let root = fiber;
61
+ // TikTok's inbox can sit more than 100 ancestors below its host root.
62
+ for (let i = 0; root?.return && i < nodeBudget; i++) root = root.return;
63
+ if (root?.return) fail("Inbox component root exceeds this read's node budget");
64
+ const current = root?.stateNode?.current;
65
+ if (current) {
66
+ const pending = [{ node: current, parent: null }];
67
+ let found;
68
+ for (let i = 0; pending.length && i < nodeBudget; i++) {
69
+ const entry = pending.pop();
70
+ const node = entry.node;
71
+ if (node === fiber || node === fiber?.alternate) { found = entry; break; }
72
+ if (node.sibling) pending.push({ node: node.sibling, parent: entry.parent });
73
+ if (node.child) pending.push({ node: node.child, parent: entry });
74
+ }
75
+ if (!found) fail("Inbox component is no longer mounted");
76
+ // A reused child can still point back to an old ancestor via `return`.
77
+ // Keep the ancestor path discovered in the committed tree itself.
78
+ for (let i = 0; found && i < 16; i++, found = found.parent) yield found.node;
79
+ return;
80
+ }
81
+ if (fiber?.alternate) fail("Committed inbox component is unavailable");
82
+ for (let i = 0; fiber && i < 16; i++, fiber = fiber.return) yield fiber;
83
+ }
84
+ const props = (element, predicate) => {
85
+ for (const fiber of fibers(element)) {
86
+ if (predicate(fiber.memoizedProps)) return fiber.memoizedProps;
87
+ }
88
+ return null;
89
+ };
90
+ const show = async key => {
91
+ const button = await wait(() => [...document.querySelectorAll(`[data-e2e="nav-${key}"]`)].find(visible));
92
+ if (button.getAttribute("aria-pressed") !== "true") button.click();
93
+ };
94
+ if (feed === "activity") {
95
+ await show("activity");
96
+ const panel = await wait(() => [...document.querySelectorAll('[data-e2e="inbox-list"]')].find(visible));
97
+ const source = () => props(panel, p => Array.isArray(p?.item?.notice_list) && typeof p.onHitBottom === "function");
98
+ await wait(source);
99
+ const current = () => {
100
+ const value = source();
101
+ if (!value || ![0, 1, false, true].includes(value.item.has_more) || typeof value.item.loading !== "boolean") fail("Activity loading state is unavailable");
102
+ return value;
103
+ };
104
+ const load = async () => {
105
+ await wait(() => !current().item.loading);
106
+ const before = current().item;
107
+ const stamp = JSON.stringify([before.min_time, before.max_time, before.notice_list.map(n => n.nid_str)]);
108
+ await readWithinDeadline(() => current().onHitBottom());
109
+ await wait(() => {
110
+ const next = current().item;
111
+ return !next.loading && (!next.has_more || JSON.stringify([next.min_time, next.max_time, next.notice_list.map(n => n.nid_str)]) !== stamp);
112
+ });
113
+ };
114
+ // TikTok can display its empty placeholder before making the first request.
115
+ // Its initial has_more=1 must never be interpreted as a successful empty read.
116
+ await wait(() => !current().item.loading);
117
+ if (!current().item.notice_list.length && current().item.has_more) await load();
118
+ for (let page = 0; page < 20; page++) {
119
+ const state = current().item;
120
+ if (state.notice_list.some(n => typeof n.nid_str !== "string" || !/^\d+$/.test(n.nid_str))) fail("Activity source identity is unavailable");
121
+ const start = cursor.after ? state.notice_list.findIndex(n => n.nid_str === cursor.after) + 1 : 0;
122
+ if (cursor.after && !start) {
123
+ if (!state.has_more) fail("Activity cursor expired; restart the read");
124
+ await load();
125
+ continue;
126
+ }
127
+ const items = state.notice_list.slice(start, start + limit);
128
+ if (!items.length && state.has_more) { await load(); continue; }
129
+ const rendered = new Map();
130
+ for (const element of panel.querySelectorAll('[data-e2e="inbox-list-item"]')) {
131
+ const item = props(element, p => typeof p?.item?.nid_str === "string")?.item;
132
+ if (item) rendered.set(item.nid_str, element);
133
+ }
134
+ const rows = items.map(item => {
135
+ const element = rendered.get(item.nid_str);
136
+ const text = element?.innerText?.trim();
137
+ if (!text) fail("An activity record has no rendered content; coverage is incomplete");
138
+ const links = [...new Set([...element.querySelectorAll("a[href]")].map(a => {
139
+ const url = new URL(a.href);
140
+ return url.origin === location.origin ? `${url.origin}${url.pathname}` : null;
141
+ }).filter(Boolean))];
142
+ const seconds = Number(item.create_time);
143
+ if (!Number.isFinite(seconds) || seconds <= 0 || seconds > 8640000000000) fail("Activity timestamp is unavailable");
144
+ return { id: item.nid_str, origin_id: item.nid_str, type: item.type, text, occurred_at: new Date(seconds * 1000).toISOString(), read: item.has_read === true, source_url: links.find(url => /\/(video|photo)\/\d+$/.test(url)) ?? links[0] ?? location.origin, links };
145
+ });
146
+ const more = start + items.length < state.notice_list.length || !!state.has_more;
147
+ return result(rows, more ? { after: items.at(-1).nid_str } : null);
148
+ }
149
+ fail("Activity cursor exceeds this read's page budget; restart the read");
150
+ }
151
+ await show("messages");
152
+ const list = await wait(() => document.querySelector('[data-e2e="dm-new-conversation-list"]'));
153
+ const clientApi = () => {
154
+ const elements = [list, ...list.querySelectorAll("div")].slice(0,12);
155
+ for (const element of elements) for (const fiber of fibers(element)) {
156
+ let hook = fiber.memoizedState;
157
+ for (let i = 0; hook && i < 180; i++, hook = hook.next) {
158
+ const api = hook.memoizedState?.current?.value;
159
+ if (api?.stateStore && typeof api.stateStore.isInitSucceeded === "boolean" && typeof api.instance?.getConversationList === "function" && typeof api.instance?.getMessagesByConversation === "function") return api;
160
+ }
161
+ }
162
+ return null;
163
+ };
164
+ const api = await wait(clientApi);
165
+ if (api.instance.ctx?.option?.userId !== owner) fail("Message client belongs to a different account", "account_mismatch");
166
+ await wait(() => {
167
+ if (api.stateStore.isInitError || api.stateStore.isNotAvailable) fail("Message initialization failed");
168
+ return api.stateStore.isInitSucceeded && api.stateStore.isInitLoadDone;
169
+ });
170
+ const sourceThreads = api.instance.getConversationList();
171
+ if (!Array.isArray(sourceThreads)) fail("Conversation list is unavailable");
172
+ if (sourceThreads.some(thread => typeof thread.id !== "string" || !/^[0-9:]{1,150}$/.test(thread.id))) fail("Conversation source identity is unavailable");
173
+ if (sourceThreads.length > 200) fail("Conversation list exceeds this scan's budget; coverage is incomplete");
174
+ const ids = sourceThreads.map(thread => thread.id);
175
+ const snapshot = [...ids].sort();
176
+ const finish = async (rows, next) => {
177
+ const output = await result(rows, next);
178
+ if (api.instance.ctx?.option?.userId !== owner) fail("Message client changed accounts during the read", "account_mismatch");
179
+ const latest = api.instance.getConversationList();
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
+ return output;
182
+ };
183
+ const validIds = values => Array.isArray(values) && values.length <= 200 && values.every(id => typeof id === "string" && ids.includes(id));
184
+ if (input.cursor !== undefined) {
185
+ if (!Array.isArray(cursor.snapshot) || JSON.stringify(cursor.snapshot) !== JSON.stringify(snapshot)) fail("Conversation set changed; restart the read, previous coverage is incomplete");
186
+ if (!ids.includes(cursor.thread) || !["recent", "history"].includes(cursor.phase) || (cursor.phase === "history" ? cursor.after === undefined : cursor.after !== undefined) || !validIds(cursor.remaining) || !Array.isArray(cursor.history) || cursor.history.length > 200 || cursor.history.some(item => !ids.includes(item?.thread) || typeof item.after !== "string" || !/^\d+$/.test(item.after))) fail("Invalid conversation scan cursor");
187
+ }
188
+ // Take one recent page per conversation before draining old history. Preserve
189
+ // the initial order across recency changes; set changes require a fresh scan.
190
+ const phase = cursor.phase ?? "recent";
191
+ const remaining = cursor.remaining ?? ids.slice(1);
192
+ const history = cursor.history ?? [];
193
+ const thread = sourceThreads.find(thread => thread.id === (cursor.thread ?? ids[0]));
194
+ if (!thread) return finish([], null);
195
+ if (typeof thread.getMessageList !== "function" || typeof thread.hasMoreMessages !== "boolean") fail("Conversation loading state is unavailable");
196
+ const project = message => {
197
+ if (typeof message.serverId !== "string" || !/^\d+$/.test(message.serverId) || message.conversationId !== thread.id) fail("Message source identity is unavailable");
198
+ let content;
199
+ try { content = typeof message.content === "string" ? JSON.parse(message.content) : message.content; } catch { fail("Message content is unavailable"); }
200
+ const timestamp = message.createdAt?.getTime?.();
201
+ if (!Number.isFinite(timestamp) || typeof message.isFromMe !== "boolean") fail("Message timestamp or direction is unavailable");
202
+ const plainText = message.type === 7 && typeof content?.text === "string" && Object.keys(content).every(key => key === "text");
203
+ return { id: message.serverId, origin_id: message.serverId, conversation_id: thread.id, conversation_name: thread.isGroupChat ? thread.coreInfo?.name ?? null : null, source_url: location.origin, text: message.isRecalled ? "" : typeof content?.text === "string" ? content.text : typeof content?.tips === "string" ? content.tips : "", type: message.type, sender_id: typeof message.sender === "string" ? message.sender : null, from_me: message.isFromMe, occurred_at: new Date(timestamp).toISOString(), recalled: message.isRecalled === true, has_uninspected_content: !plainText };
204
+ };
205
+ for (let page = 0; page < 20; page++) {
206
+ await wait(() => !thread.isPullingMessages);
207
+ if (api.instance.ctx?.option?.userId !== owner) fail("Message client changed accounts during the read", "account_mismatch");
208
+ const messages = thread.getMessageList().map(project).sort((a,b) => b.occurred_at.localeCompare(a.occurred_at) || b.id.localeCompare(a.id));
209
+ const start = cursor.after ? messages.findIndex(message => message.id === cursor.after) + 1 : 0;
210
+ if ((cursor.after && !start) || start === messages.length && thread.hasMoreMessages) {
211
+ if (!thread.hasMoreMessages) fail("Message cursor expired; restart the read");
212
+ const response = await readWithinDeadline(() => api.instance.getMessagesByConversation({ conversation: thread, limit: 50 }));
213
+ if (typeof response?.hasMore !== "boolean" || !Array.isArray(response.messages)) fail("Message history returned no completion state");
214
+ if (response.hasMore && !response.messages.length) fail("Message history did not advance");
215
+ continue;
216
+ }
217
+ const rows = messages.slice(start, start + limit);
218
+ const more = start + rows.length < messages.length || thread.hasMoreMessages;
219
+ const queued = [...history];
220
+ if (more && phase === "recent") queued.push({ thread: thread.id, after: rows.at(-1).id });
221
+ const base = { snapshot, remaining, history: queued };
222
+ const next = phase === "recent" && remaining.length
223
+ ? { ...base, phase: "recent", thread: remaining[0], remaining: remaining.slice(1) }
224
+ : more && phase === "history"
225
+ ? { ...base, phase, thread: thread.id, after: rows.at(-1).id }
226
+ : queued.length ? { ...base, phase: "history", ...queued[0], history: queued.slice(1) } : null;
227
+ return finish(rows, next);
228
+ }
229
+ fail("Message cursor exceeds this read's page budget; restart the read");
230
+ }