@lobu/runtime-device 24.1.1 → 24.2.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 (158) hide show
  1. package/dist/component.json +1 -1
  2. package/dist/dependencies.bun.lock +26 -28
  3. package/npm-shrinkwrap.json +155 -172
  4. package/package.json +1 -1
  5. package/vendor/cli/dist/bundled-skills/lobu/SKILL.md +14 -5
  6. package/vendor/cli/dist/chrome-extension/accessibility-tree.js +834 -0
  7. package/vendor/cli/dist/chrome-extension/ambient-context.js +53 -0
  8. package/vendor/cli/dist/chrome-extension/auth-refresh.js +167 -0
  9. package/vendor/cli/dist/chrome-extension/automation-run.js +13 -0
  10. package/vendor/cli/dist/chrome-extension/background.js +1155 -0
  11. package/vendor/cli/dist/chrome-extension/bridge.js +140 -0
  12. package/vendor/cli/dist/chrome-extension/browser-origin-policy.js +62 -0
  13. package/vendor/cli/dist/chrome-extension/browser-recovery.js +260 -0
  14. package/vendor/cli/dist/chrome-extension/browser-url-sanitizer.js +284 -0
  15. package/vendor/cli/dist/chrome-extension/completion-response.js +12 -0
  16. package/vendor/cli/dist/chrome-extension/config.js +144 -0
  17. package/vendor/cli/dist/chrome-extension/connector-manifests/chrome.json +1516 -0
  18. package/vendor/cli/dist/chrome-extension/connector-manifests/chrome_bookmarks.json +84 -0
  19. package/vendor/cli/dist/chrome-extension/connector-manifests/chrome_downloads.json +86 -0
  20. package/vendor/cli/dist/chrome-extension/connector-manifests/chrome_history.json +78 -0
  21. package/vendor/cli/dist/chrome-extension/connector-manifests.js +1767 -0
  22. package/vendor/cli/dist/chrome-extension/console-capture.js +627 -0
  23. package/vendor/cli/dist/chrome-extension/control-activity.js +70 -0
  24. package/vendor/cli/dist/chrome-extension/debugger-lease.js +439 -0
  25. package/vendor/cli/dist/chrome-extension/feed-listener.js +488 -0
  26. package/vendor/cli/dist/chrome-extension/feed-transaction.js +48 -0
  27. package/vendor/cli/dist/chrome-extension/feeds.js +632 -0
  28. package/vendor/cli/dist/chrome-extension/focus-tab.js +25 -0
  29. package/vendor/cli/dist/chrome-extension/fonts/dm-mono-latin-500-normal.woff2 +0 -0
  30. package/vendor/cli/dist/chrome-extension/fonts/dm-sans-latin-400-normal.woff2 +0 -0
  31. package/vendor/cli/dist/chrome-extension/fonts/dm-sans-latin-600-normal.woff2 +0 -0
  32. package/vendor/cli/dist/chrome-extension/heartbeat.js +140 -0
  33. package/vendor/cli/dist/chrome-extension/icons/icon-128-rec.png +0 -0
  34. package/vendor/cli/dist/chrome-extension/icons/icon-128.png +0 -0
  35. package/vendor/cli/dist/chrome-extension/icons/icon-16-rec.png +0 -0
  36. package/vendor/cli/dist/chrome-extension/icons/icon-16.png +0 -0
  37. package/vendor/cli/dist/chrome-extension/icons/icon-32-rec.png +0 -0
  38. package/vendor/cli/dist/chrome-extension/icons/icon-32.png +0 -0
  39. package/vendor/cli/dist/chrome-extension/icons/icon-48-rec.png +0 -0
  40. package/vendor/cli/dist/chrome-extension/icons/icon-48.png +0 -0
  41. package/vendor/cli/dist/chrome-extension/lens-page.js +135 -0
  42. package/vendor/cli/dist/chrome-extension/lens-target.js +20 -0
  43. package/vendor/cli/dist/chrome-extension/manifest.json +76 -0
  44. package/vendor/cli/dist/chrome-extension/network-intercept.js +657 -0
  45. package/vendor/cli/dist/chrome-extension/page-activation.js +120 -0
  46. package/vendor/cli/dist/chrome-extension/page-dialogs.js +125 -0
  47. package/vendor/cli/dist/chrome-extension/page-url.js +11 -0
  48. package/vendor/cli/dist/chrome-extension/panel-session.js +184 -0
  49. package/vendor/cli/dist/chrome-extension/poll-auth.js +85 -0
  50. package/vendor/cli/dist/chrome-extension/poll-backoff.js +22 -0
  51. package/vendor/cli/dist/chrome-extension/run-fence.js +117 -0
  52. package/vendor/cli/dist/chrome-extension/setup-status.js +71 -0
  53. package/vendor/cli/dist/chrome-extension/side-panel-action-click.js +65 -0
  54. package/vendor/cli/dist/chrome-extension/sidepanel-context.js +50 -0
  55. package/vendor/cli/dist/chrome-extension/sidepanel.html +680 -0
  56. package/vendor/cli/dist/chrome-extension/sidepanel.js +1643 -0
  57. package/vendor/cli/dist/chrome-extension/tab-labels.js +233 -0
  58. package/vendor/cli/dist/chrome-extension/tools.js +3354 -0
  59. package/vendor/cli/dist/chrome-extension/watch-observer.js +354 -0
  60. package/vendor/cli/dist/chrome-extension/watch-redact.js +96 -0
  61. package/vendor/cli/dist/chrome-extension/watch.js +348 -0
  62. package/vendor/cli/dist/commands/_lib/apply/desired-state.d.ts +6 -4
  63. package/vendor/cli/dist/commands/_lib/apply/desired-state.js +5 -3
  64. package/vendor/cli/dist/commands/_lib/apply/diff.js +4 -3
  65. package/vendor/cli/dist/commands/_lib/chrome-for-testing.d.ts +13 -0
  66. package/vendor/cli/dist/commands/_lib/chrome-for-testing.js +226 -0
  67. package/vendor/cli/dist/commands/_lib/device-credential.d.ts +34 -0
  68. package/vendor/cli/dist/commands/_lib/device-credential.js +157 -0
  69. package/vendor/cli/dist/commands/_lib/device-wizard.d.ts +17 -0
  70. package/vendor/cli/dist/commands/_lib/device-wizard.js +11 -2
  71. package/vendor/cli/dist/commands/_lib/native-messaging.d.ts +7 -0
  72. package/vendor/cli/dist/commands/_lib/native-messaging.js +40 -0
  73. package/vendor/cli/dist/commands/browser.d.ts +23 -0
  74. package/vendor/cli/dist/commands/browser.js +492 -0
  75. package/vendor/cli/dist/commands/call.d.ts +12 -0
  76. package/vendor/cli/dist/commands/call.js +48 -26
  77. package/vendor/cli/dist/commands/context.d.ts +3 -1
  78. package/vendor/cli/dist/commands/context.js +13 -2
  79. package/vendor/cli/dist/commands/daemon.js +2 -152
  80. package/vendor/cli/dist/commands/memory/_lib/errors.d.ts +4 -1
  81. package/vendor/cli/dist/commands/memory/_lib/errors.js +9 -1
  82. package/vendor/cli/dist/commands/memory/_lib/mcp.d.ts +0 -9
  83. package/vendor/cli/dist/commands/memory/_lib/mcp.js +1 -23
  84. package/vendor/cli/dist/commands/memory/_lib/memory-auth.d.ts +20 -4
  85. package/vendor/cli/dist/commands/memory/_lib/memory-auth.js +21 -17
  86. package/vendor/cli/dist/commands/memory/_lib/seed-cmd.js +6 -17
  87. package/vendor/cli/dist/commands/memory/run.js +4 -25
  88. package/vendor/cli/dist/connectors/google_gmail.ts +70 -41
  89. package/vendor/cli/dist/connectors/google_gmail_references.ts +406 -0
  90. package/vendor/cli/dist/connectors/linkedin-inbox-adapter.js +107 -36
  91. package/vendor/cli/dist/connectors/linkedin-inbox.ts +13 -0
  92. package/vendor/cli/dist/connectors/linkedin.ts +1 -1
  93. package/vendor/cli/dist/connectors/postgres.ts +5 -3
  94. package/vendor/cli/dist/connectors/reddit.ts +171 -5
  95. package/vendor/cli/dist/connectors/whatsapp-web-adapter.js +147 -61
  96. package/vendor/cli/dist/connectors/whatsapp-web-helpers.ts +6 -2
  97. package/vendor/cli/dist/connectors/whatsapp_web.ts +48 -17
  98. package/vendor/cli/dist/connectors/x-dm-adapter.js +252 -35
  99. package/vendor/cli/dist/connectors/x.ts +189 -13
  100. package/vendor/cli/dist/index.js +46 -6
  101. package/vendor/cli/dist/internal/device-state.d.ts +2 -0
  102. package/vendor/cli/dist/internal/device-state.js +4 -0
  103. package/vendor/cli/dist/runtime-components.json +4 -4
  104. package/vendor/cli/dist/templates/AGENTS.md.tmpl +3 -1
  105. package/vendor/cli/dist/templates/TESTING.md.tmpl +8 -8
  106. package/vendor/cli/package.json +1 -1
  107. package/vendor/connector-sdk/dist/browser-requirement.js +1 -1
  108. package/vendor/connector-sdk/dist/connector-types.d.ts +5 -1
  109. package/vendor/connector-sdk/package.json +1 -1
  110. package/vendor/connector-worker/dist/agent-turn/gateway-tools.d.ts +9 -0
  111. package/vendor/connector-worker/dist/agent-turn/gateway-tools.js +23 -2
  112. package/vendor/connector-worker/dist/agent-turn/guest-entry.js +14 -6
  113. package/vendor/connector-worker/dist/agent-turn/guest.bundle.js +62 -25
  114. package/vendor/connector-worker/dist/agent-turn/media-tools.d.ts +1 -0
  115. package/vendor/connector-worker/dist/agent-turn/media-tools.js +5 -2
  116. package/vendor/connector-worker/dist/bin.js +2 -0
  117. package/vendor/connector-worker/dist/daemon/agent-turn.js +52 -17
  118. package/vendor/connector-worker/dist/daemon/automation.js +2 -12
  119. package/vendor/connector-worker/dist/daemon/client.d.ts +8 -2
  120. package/vendor/connector-worker/dist/daemon/client.js +8 -1
  121. package/vendor/connector-worker/dist/daemon/interactive-session.js +29 -6
  122. package/vendor/connector-worker/dist/daemon/poll-loop.js +23 -1
  123. package/vendor/connector-worker/dist/daemon/terminal-delivery.d.ts +5 -10
  124. package/vendor/connector-worker/dist/daemon/terminal-delivery.js +55 -23
  125. package/vendor/connector-worker/dist/daemon/terminal-failure.js +2 -2
  126. package/vendor/connector-worker/dist/executor/isolate.js +3 -0
  127. package/vendor/connector-worker/package.json +1 -1
  128. package/vendor/core/dist/agent-tooling.d.ts +8 -0
  129. package/vendor/core/dist/agent-tooling.js +9 -1
  130. package/vendor/core/dist/capabilities.d.ts +1 -1
  131. package/vendor/core/dist/capabilities.js +5 -0
  132. package/vendor/core/dist/contracts/entity-reference.d.ts +3 -0
  133. package/vendor/core/dist/contracts/entity-reference.js +31 -0
  134. package/vendor/core/dist/contracts/tools/manage-automations.d.ts +3 -0
  135. package/vendor/core/dist/contracts/tools/manage-automations.js +3 -0
  136. package/vendor/core/dist/contracts/tools/manage-connections.js +1 -1
  137. package/vendor/core/dist/contracts/tools/manage-entity-schema.js +1 -1
  138. package/vendor/core/dist/contracts/tools/manage-entity.d.ts +113 -34
  139. package/vendor/core/dist/contracts/tools/manage-entity.js +41 -14
  140. package/vendor/core/dist/contracts/tools/manage-operations.d.ts +13 -4
  141. package/vendor/core/dist/contracts/tools/manage-operations.js +19 -7
  142. package/vendor/core/dist/contracts/tools/read-knowledge.d.ts +6 -1
  143. package/vendor/core/dist/contracts/tools/read-knowledge.js +20 -1
  144. package/vendor/core/dist/contracts/tools/view-attach.d.ts +1 -1
  145. package/vendor/core/dist/contracts/tools/view-runtime.d.ts +2 -0
  146. package/vendor/core/dist/contracts/tools/view-runtime.js +3 -1
  147. package/vendor/core/dist/contracts/worker/device-automation.d.ts +4 -9
  148. package/vendor/core/dist/contracts/worker/device-automation.js +10 -15
  149. package/vendor/core/dist/contracts/worker/protocol.d.ts +20 -0
  150. package/vendor/core/dist/contracts/worker/protocol.js +20 -0
  151. package/vendor/core/dist/reserved.d.ts +6 -3
  152. package/vendor/core/dist/reserved.js +6 -3
  153. package/vendor/core/dist/worker/auth.d.ts +3 -2
  154. package/vendor/core/package.json +6 -1
  155. package/vendor/postgres/cf/src/query.js +3 -1
  156. package/vendor/postgres/cjs/src/query.js +3 -1
  157. package/vendor/postgres/src/query.js +3 -1
  158. /package/vendor/postgres/{.bun-tag-2f292c6554a8475e → .bun-tag-22d644c2873790db} +0 -0
@@ -2,9 +2,10 @@
2
2
  * Serialized into the signed-in x.com page. All bindings must remain inside
3
3
  * this function: the connector ships it with Function.prototype.toString().
4
4
  *
5
- * It reads and sends direct messages through the same-origin DM endpoints
6
- * the x.com web client calls, with the page's own session cookies. It never
7
- * opens /messages, which marks conversations read.
5
+ * It reads and sends direct messages, and reads the notifications timeline,
6
+ * through the same-origin endpoints the x.com web client calls, with the
7
+ * page's own session cookies. It never opens /messages or /notifications,
8
+ * which mark conversations and notifications read.
8
9
  */
9
10
  export function xDirectMessagesProgram() {
10
11
  const key = "__lobuXDirectMessagesV1";
@@ -49,14 +50,26 @@ export function xDirectMessagesProgram() {
49
50
  return raw ? decodeURIComponent(raw.slice(name.length + 1)) : "";
50
51
  }
51
52
  // `twid` carries the signed-in numeric user id; it is not a credential.
52
- function account() {
53
+ function account(expected) {
53
54
  const match = /^u=([0-9]+)$/.exec(cookie("twid"));
54
55
  if (!match) fail("Sign in to X in the paired browser");
56
+ if (expected !== undefined && match[1] !== expected)
57
+ fail(
58
+ "X account changed or belongs to another account; reconnect this source",
59
+ );
55
60
  return match[1];
56
61
  }
57
- async function request(path, init = {}) {
62
+ function savedAccount(input) {
63
+ if (
64
+ typeof input.account_id !== "string" ||
65
+ !/^[0-9]{1,20}$/.test(input.account_id)
66
+ )
67
+ fail("Saved X account identity is unavailable; reconnect this source");
68
+ return account(input.account_id);
69
+ }
70
+ async function request(path, init = {}, expected) {
58
71
  init.signal?.throwIfAborted();
59
- const self = account();
72
+ const self = account(expected);
60
73
  const csrf = cookie("ct0");
61
74
  if (!csrf) fail("Sign in to X in the paired browser");
62
75
  const response = await fetch(path, {
@@ -73,6 +86,7 @@ export function xDirectMessagesProgram() {
73
86
  ...init.headers,
74
87
  },
75
88
  });
89
+ account(self);
76
90
  if (!response.ok) {
77
91
  let code = null;
78
92
  try {
@@ -80,6 +94,7 @@ export function xDirectMessagesProgram() {
80
94
  } catch {
81
95
  code = null;
82
96
  }
97
+ account(self);
83
98
  fail(
84
99
  `request failed (HTTP ${response.status}${code === null ? "" : `, X error ${code}`})`,
85
100
  response.status,
@@ -89,23 +104,28 @@ export function xDirectMessagesProgram() {
89
104
  if (account() !== self) fail("X account changed during the request");
90
105
  return body;
91
106
  }
92
- async function inbox() {
107
+ async function inbox(self) {
93
108
  const body = await request(
94
109
  `${DM_API}/inbox_initial_state.json?${DM_PARAMS}`,
110
+ {},
111
+ self,
95
112
  );
96
113
  return body?.inbox_initial_state ?? fail("Inbox state is unavailable");
97
114
  }
98
- async function conversation(conversationId, maxId, signal) {
115
+ async function conversation(conversationId, maxId, signal, self) {
99
116
  const page = maxId ? `&max_id=${encodeURIComponent(maxId)}` : "";
100
117
  const body = await request(
101
118
  `${DM_API}/conversation/${conversationId}.json?context=FETCH_DM_CONVERSATION&${DM_PARAMS}${page}`,
102
119
  signal ? { signal } : {},
120
+ self,
103
121
  );
104
122
  return body?.conversation_timeline ?? fail("Conversation is unavailable");
105
123
  }
106
- async function updates(cursor) {
124
+ async function updates(cursor, self) {
107
125
  const body = await request(
108
126
  `${DM_API}/user_updates.json?cursor=${encodeURIComponent(cursor)}&${DM_PARAMS}`,
127
+ {},
128
+ self,
109
129
  );
110
130
  const events = body?.user_events ?? fail("Inbox updates are unavailable");
111
131
  // A quiet inbox answers with only its cursor and last-seen ids: no
@@ -210,7 +230,12 @@ export function xDirectMessagesProgram() {
210
230
  async function readMessage(self, conversationId, messageId) {
211
231
  let maxId;
212
232
  for (let page = 0; page < LOOKUP_PAGES; page++) {
213
- const timeline = await conversation(conversationId, maxId);
233
+ const timeline = await conversation(
234
+ conversationId,
235
+ maxId,
236
+ undefined,
237
+ self,
238
+ );
214
239
  const row = rowsOf(timeline, self).find(
215
240
  (candidate) => candidate.message_id === messageId,
216
241
  );
@@ -225,7 +250,7 @@ export function xDirectMessagesProgram() {
225
250
  fail("Message lookup is incomplete: conversation page limit reached");
226
251
  }
227
252
  async function read(input) {
228
- const self = account();
253
+ const self = savedAccount(input);
229
254
  const limit = Math.max(1, Math.min(100, Number(input.limit) || 20));
230
255
  const query = typeof input.query === "string" ? input.query.trim() : "";
231
256
  if (query) {
@@ -235,7 +260,7 @@ export function xDirectMessagesProgram() {
235
260
  return { rows: row ? [row] : [], hasMore: false };
236
261
  }
237
262
  // The inbox's recent state across conversations, not a history export.
238
- const all = rowsOf(await inbox(), self);
263
+ const all = rowsOf(await inbox(self), self);
239
264
  let start = 0;
240
265
  if (input.cursor) {
241
266
  if (typeof input.cursor !== "string" || input.cursor.length > 1000)
@@ -304,7 +329,7 @@ export function xDirectMessagesProgram() {
304
329
  );
305
330
  }
306
331
  async function send(input) {
307
- const self = account();
332
+ const self = savedAccount(input);
308
333
  const signal = AbortSignal.timeout(SEND_TIMEOUT_MS);
309
334
  const conversationId = String(input.conversation_id ?? "");
310
335
  if (!CONVERSATION_ID.test(conversationId))
@@ -330,7 +355,7 @@ export function xDirectMessagesProgram() {
330
355
  });
331
356
  const readConversation = () => {
332
357
  if (account() !== self) fail("X account changed before the send read");
333
- return conversation(conversationId, undefined, signal);
358
+ return conversation(conversationId, undefined, signal, self);
334
359
  };
335
360
  // Only an existing conversation the account can read is a send target.
336
361
  const before = await readConversation();
@@ -340,21 +365,25 @@ export function xDirectMessagesProgram() {
340
365
  for (const delay of [...UNAVAILABLE_SEND_RETRY_DELAYS_MS, null]) {
341
366
  if (account() !== self) fail("X account changed before the send");
342
367
  try {
343
- response = await request(`${DM_API}/new2.json`, {
344
- method: "POST",
345
- signal,
346
- headers: { "content-type": "application/json" },
347
- body: JSON.stringify({
348
- conversation_id: conversationId,
349
- recipient_ids: false,
350
- request_id: requestId,
351
- text,
352
- cards_platform: "Web-12",
353
- include_cards: 1,
354
- include_quote_count: true,
355
- dm_users: false,
356
- }),
357
- });
368
+ response = await request(
369
+ `${DM_API}/new2.json`,
370
+ {
371
+ method: "POST",
372
+ signal,
373
+ headers: { "content-type": "application/json" },
374
+ body: JSON.stringify({
375
+ conversation_id: conversationId,
376
+ recipient_ids: false,
377
+ request_id: requestId,
378
+ text,
379
+ cards_platform: "Web-12",
380
+ include_cards: 1,
381
+ include_quote_count: true,
382
+ dm_users: false,
383
+ }),
384
+ },
385
+ self,
386
+ );
358
387
  break;
359
388
  } catch (error) {
360
389
  if (error?.status !== 503) throw error;
@@ -398,7 +427,7 @@ export function xDirectMessagesProgram() {
398
427
  // inbox stands now, and each later pass reports what arrived after it.
399
428
  async function snapshot(previous, self) {
400
429
  if (!previous) {
401
- const state = await inbox();
430
+ const state = await inbox(self);
402
431
  messagesOf(state);
403
432
  if (typeof state.cursor !== "string" || !state.cursor)
404
433
  fail("Inbox cursor is unavailable");
@@ -417,7 +446,7 @@ export function xDirectMessagesProgram() {
417
446
  let cursor = previous.cursor;
418
447
  let more = false;
419
448
  for (let page = 1; ; page++) {
420
- const batch = await updates(cursor);
449
+ const batch = await updates(cursor, self);
421
450
  for (const row of rowsOf(batch, self).reverse()) {
422
451
  if (observed.has(row.id)) continue;
423
452
  observed.add(row.id);
@@ -515,13 +544,13 @@ export function xDirectMessagesProgram() {
515
544
  async function poll(listener) {
516
545
  if (!active(listener)) return;
517
546
  try {
547
+ account(listener.account);
518
548
  if (!listener.batches.length) {
519
- if (account() !== listener.account)
520
- fail("X account changed; reconnect this source");
521
549
  const { checkpoint } = listener;
522
550
  const result = await snapshot(checkpoint?.x_dm, listener.account);
523
551
  listener.batches = await batchesOf(checkpoint, result);
524
552
  }
553
+ account(listener.account);
525
554
  if (active(listener)) post(listener, listener.batches[0]);
526
555
  } catch {
527
556
  stop(
@@ -545,6 +574,11 @@ export function xDirectMessagesProgram() {
545
574
  event.data.sequence !== listener.sequence
546
575
  )
547
576
  return;
577
+ try {
578
+ account(listener.account);
579
+ } catch {
580
+ return stop(listener, "X account changed; reconnect this source");
581
+ }
548
582
  if (event.data.ok === false)
549
583
  return stop(
550
584
  listener,
@@ -561,7 +595,7 @@ export function xDirectMessagesProgram() {
561
595
  async function listen(input) {
562
596
  if (!input.bridge_id || !input.token)
563
597
  fail("Source subscription binding is required");
564
- const self = account();
598
+ const self = savedAccount(input);
565
599
  const previous = input.checkpoint?.x_dm;
566
600
  if (previous && previous.account !== self)
567
601
  fail("Source checkpoint belongs to another account");
@@ -587,5 +621,188 @@ export function xDirectMessagesProgram() {
587
621
  fail("Unable to attach source subscription");
588
622
  return { listening: true };
589
623
  }
590
- globalThis[key] = { version, read, listen, send, detach };
624
+ // The notification API is read from this same neutral page. In particular,
625
+ // never render /notifications or call its seen/read-state endpoints here.
626
+ async function readNotifications(input) {
627
+ const self = savedAccount(input);
628
+ const limit = Math.max(
629
+ 1,
630
+ Math.min(100, Math.trunc(Number(input.limit) || 25)),
631
+ );
632
+ let cursor = null;
633
+ if (input.cursor) {
634
+ if (typeof input.cursor !== "string" || input.cursor.length > 10000)
635
+ fail("Invalid notification cursor");
636
+ cursor = JSON.parse(input.cursor);
637
+ if (
638
+ cursor.v !== 1 ||
639
+ cursor.kind !== "notifications" ||
640
+ cursor.account !== self ||
641
+ !(cursor.source === null || typeof cursor.source === "string") ||
642
+ !(cursor.after === null || typeof cursor.after === "string")
643
+ )
644
+ fail("Notification cursor belongs to another account or source");
645
+ }
646
+ const path =
647
+ "/i/api/2/notifications/all.json?count=50" +
648
+ (cursor?.source ? `&cursor=${encodeURIComponent(cursor.source)}` : "");
649
+ let body;
650
+ try {
651
+ body = await request(path, {}, self);
652
+ } catch (error) {
653
+ account(self);
654
+ if (
655
+ input.restartExpired &&
656
+ cursor?.source &&
657
+ [400, 404].includes(error.status)
658
+ )
659
+ return { rows: [], account_id: self, expired: true };
660
+ throw error;
661
+ }
662
+ if (!Array.isArray(body?.timeline?.instructions) || !body.globalObjects)
663
+ fail("Notification timeline is unavailable");
664
+ const entries = [];
665
+ for (const instruction of body.timeline.instructions) {
666
+ for (const entry of instruction.addEntries?.entries ?? []) {
667
+ const index = entries.findIndex(
668
+ (prior) => prior.entryId === entry.entryId,
669
+ );
670
+ if (index < 0) entries.push(entry);
671
+ else entries[index] = entry;
672
+ }
673
+ const replacement = instruction.replaceEntry;
674
+ if (replacement?.entry) {
675
+ const index = entries.findIndex(
676
+ (prior) => prior.entryId === replacement.entryIdToReplace,
677
+ );
678
+ if (index < 0) entries.push(replacement.entry);
679
+ else entries[index] = replacement.entry;
680
+ }
681
+ }
682
+ const all = [];
683
+ const seen = new Set();
684
+ let next;
685
+ for (const entry of entries) {
686
+ const operation = entry.content?.operation;
687
+ if (operation?.cursor) {
688
+ if (operation.cursor.cursorType === "Bottom") {
689
+ if (typeof operation.cursor.value !== "string")
690
+ fail("Notification continuation is invalid");
691
+ next = operation.cursor.value || undefined;
692
+ }
693
+ continue;
694
+ }
695
+ if (typeof entry.entryId !== "string" || !entry.entryId)
696
+ fail("Notification entry has no stable identity");
697
+ const content = entry.content?.item?.content;
698
+ const ref = content?.notification;
699
+ const note = ref ? body.globalObjects.notifications?.[ref.id] : null;
700
+ const tweet = content?.tweet
701
+ ? body.globalObjects.tweets?.[content.tweet.id]
702
+ : null;
703
+ if (ref ? !note || note.id !== ref.id : !tweet)
704
+ fail("Notification item is unavailable or unsupported");
705
+ const id = `${self}:${entry.entryId}`;
706
+ if (seen.has(id)) continue;
707
+ seen.add(id);
708
+ if (
709
+ note &&
710
+ (typeof note.timestampMs !== "string" ||
711
+ !/^[0-9]+$/.test(note.timestampMs))
712
+ )
713
+ fail("Notification item has no verifiable source date");
714
+ const sourceTime = note
715
+ ? Number(note.timestampMs)
716
+ : Date.parse(tweet.created_at);
717
+ if (
718
+ !Number.isFinite(sourceTime) ||
719
+ !Number.isFinite(new Date(sourceTime).getTime())
720
+ )
721
+ fail("Notification item has no verifiable source date");
722
+ const actorIds = [...new Set(ref?.fromUsers ?? [tweet?.user_id_str])]
723
+ .filter((x) => typeof x === "string" && x)
724
+ .sort();
725
+ const tweetIds = [...new Set(ref?.targetTweets ?? [content?.tweet?.id])]
726
+ .filter((x) => typeof x === "string" && x)
727
+ .sort();
728
+ let url = "https://x.com/notifications";
729
+ if (tweet)
730
+ url = `https://x.com/i/status/${encodeURIComponent(content.tweet.id)}`;
731
+ else if (ref.url?.url) {
732
+ try {
733
+ const candidate = new URL(ref.url.url, "https://x.com");
734
+ if (candidate.origin === "https://x.com") url = candidate.href;
735
+ } catch {
736
+ /* Keep the source inbox link. */
737
+ }
738
+ }
739
+ const row = {
740
+ id,
741
+ account_id: self,
742
+ entry_id: entry.entryId,
743
+ notification_id: ref?.id ?? null,
744
+ kind: note?.icon?.id ?? "post",
745
+ actor_ids: actorIds,
746
+ tweet_ids: tweetIds,
747
+ text: note?.message?.text ?? tweet?.full_text ?? "",
748
+ url,
749
+ occurred_at: new Date(sourceTime).toISOString(),
750
+ timestamp_basis: note ? "notification" : "post_created_at",
751
+ };
752
+ const hash = await crypto.subtle.digest(
753
+ "SHA-256",
754
+ new TextEncoder().encode(JSON.stringify(row)),
755
+ );
756
+ row.revision = Array.from(new Uint8Array(hash), (x) =>
757
+ x.toString(16).padStart(2, "0"),
758
+ ).join("");
759
+ // Keep the content-sensitive revision, but never persist sync bodies in child action output.
760
+ if (input.metadataOnly) delete row.text;
761
+ all.push(row);
762
+ }
763
+ let start = 0;
764
+ if (cursor?.after) {
765
+ start = all.findIndex((row) => row.id === cursor.after) + 1;
766
+ if (!start) {
767
+ account(self);
768
+ if (input.restartExpired)
769
+ return { rows: [], account_id: self, expired: true };
770
+ fail(
771
+ "Notification page changed; restart the read (coverage incomplete)",
772
+ );
773
+ }
774
+ }
775
+ const rows = all.slice(start, start + limit);
776
+ const moreInPage = start + rows.length < all.length;
777
+ if (!moreInPage && cursor && next === cursor.source) {
778
+ if (all.length) fail("Notification cursor did not advance");
779
+ next = undefined;
780
+ }
781
+ const nextCursor = moreInPage
782
+ ? JSON.stringify({
783
+ v: 1,
784
+ kind: "notifications",
785
+ account: self,
786
+ source: cursor?.source ?? null,
787
+ after: rows.at(-1).id,
788
+ })
789
+ : next
790
+ ? JSON.stringify({
791
+ v: 1,
792
+ kind: "notifications",
793
+ account: self,
794
+ source: next,
795
+ after: null,
796
+ })
797
+ : undefined;
798
+ if (account() !== self)
799
+ fail("X account changed during the notification read");
800
+ return {
801
+ rows,
802
+ account_id: self,
803
+ hasMore: !!nextCursor,
804
+ ...(nextCursor ? { nextCursor } : {}),
805
+ };
806
+ }
807
+ globalThis[key] = { version, read, listen, send, detach, readNotifications };
591
808
  }