@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
@@ -0,0 +1,632 @@
1
+ // Ambient-feed implementations for the chrome / chrome.history /
2
+ // chrome.bookmarks / chrome.downloads connectors.
3
+ //
4
+ // Three patterns here:
5
+ // - SNAPSHOT feeds (open_tabs, bookmarks, downloads):
6
+ // feed run = call the relevant chrome.* read API now, emit items.
7
+ // bookmarks and downloads fingerprint the snapshot and re-emit it
8
+ // only when the fingerprint moves; open_tabs emits every run.
9
+ // - EVENT-BUFFER feeds (tab_events, bookmarks, downloads, history):
10
+ // background listeners append to a chrome.storage.session ring
11
+ // buffer as events fire. feed runs flush + emit the buffered
12
+ // items. Capped at MAX_BUFFER per feed to bound storage.
13
+ // - ROLLING-WINDOW (history):
14
+ // each run pages chrome.history.search() over [lastSync - overlap,
15
+ // now] (deep backfill on the first run) AND flushes the live
16
+ // onVisited buffer. The window start rides the SERVER's feed
17
+ // checkpoint (in on the run payload, out on the success completion),
18
+ // never local storage; upserts on origin_id dedupe the overlap.
19
+ //
20
+ // Durable per-feed state -- the snapshot fingerprint, the window start -- lives
21
+ // in the SERVER's feed checkpoint, never in chrome.storage.local: a local write
22
+ // can only happen after the completion POST, and losing it is unrecoverable.
23
+
24
+ import { sanitizeBrowserRecord } from "./browser-url-sanitizer.js";
25
+
26
+ const MAX_BUFFER = 1000;
27
+ const BUFFER_PREFIX = "owletto.feedbuf.";
28
+
29
+ // ── buffered storage helpers ────────────────────────────────────────────
30
+
31
+ async function pushBuffered(key, event) {
32
+ // Use chrome.storage.session — survives SW eviction within a browser
33
+ // session, doesn't sync to disk, no cross-device leak.
34
+ const bucket = `${BUFFER_PREFIX}${key}`;
35
+ const stored = await chrome.storage.session.get(bucket);
36
+ const buf = Array.isArray(stored[bucket])
37
+ ? sanitizeBrowserRecord(stored[bucket])
38
+ : [];
39
+ buf.push(sanitizeBrowserRecord(event));
40
+ // Bound the buffer to MAX_BUFFER newest entries.
41
+ const trimmed = buf.length > MAX_BUFFER ? buf.slice(-MAX_BUFFER) : buf;
42
+ await chrome.storage.session.set({ [bucket]: trimmed });
43
+ }
44
+
45
+ // PEEK buffered events without consuming them. Legacy entries are sanitized
46
+ // back into session storage before they are returned. Callers should call
47
+ // clearBuffered(key, peeked.length) only after the gateway POST
48
+ // succeeded, so a transient network failure doesn't lose data.
49
+ async function peekBuffered(key) {
50
+ const bucket = `${BUFFER_PREFIX}${key}`;
51
+ const stored = await chrome.storage.session.get(bucket);
52
+ const buf = Array.isArray(stored[bucket]) ? stored[bucket] : [];
53
+ const sanitized = sanitizeBrowserRecord(buf);
54
+ if (bucket in stored)
55
+ await chrome.storage.session.set({ [bucket]: sanitized });
56
+ return sanitized;
57
+ }
58
+
59
+ // CONSUME the first `count` events from the buffer. Any events that
60
+ // landed in the buffer between peekBuffered() and clearBuffered() are
61
+ // preserved (we only drop the prefix we successfully streamed).
62
+ async function clearBuffered(key, count) {
63
+ const bucket = `${BUFFER_PREFIX}${key}`;
64
+ const stored = await chrome.storage.session.get(bucket);
65
+ const buf = Array.isArray(stored[bucket]) ? stored[bucket] : [];
66
+ const remaining = buf.slice(count);
67
+ if (remaining.length === 0) {
68
+ await chrome.storage.session.remove(bucket);
69
+ } else {
70
+ await chrome.storage.session.set({ [bucket]: remaining });
71
+ }
72
+ }
73
+
74
+ // Set once at pairing and only read afterwards, so unlike a per-run write it
75
+ // cannot be lost mid-run. Mirrors STORAGE_KEYS.workerId in background.js.
76
+ const WORKER_ID_KEY = "owletto.workerId";
77
+
78
+ async function currentWorkerId() {
79
+ const stored = await chrome.storage.local.get(WORKER_ID_KEY);
80
+ const id = stored[WORKER_ID_KEY];
81
+ return typeof id === "string" && id.length > 0 ? id : null;
82
+ }
83
+
84
+ // A feed checkpoint speaks only for the browser profile that wrote it. "How far
85
+ // has this feed synced" and "has THIS profile ever backfilled" are different
86
+ // questions, and a checkpoint inherited by a re-paired or wiped profile answers
87
+ // the second one wrongly: the profile would resume a window it never earned and
88
+ // silently skip its own backfill. So every checkpoint carries the worker_id that
89
+ // set it, and a mismatch, a missing worker_id (a checkpoint predating this
90
+ // field) or an unpaired profile all fall back to the deep backfill — redundant
91
+ // re-upload is idempotent on origin_id, where a skipped window is unrecoverable
92
+ // data loss.
93
+ function checkpointForThisProfile(checkpoint, workerId) {
94
+ if (!workerId || !checkpoint || typeof checkpoint !== "object") return null;
95
+ return checkpoint.worker_id === workerId ? checkpoint : null;
96
+ }
97
+
98
+ // chrome.bookmarks and chrome.downloads are SNAPSHOT sources: the whole state
99
+ // is readable at any moment. They used to be modelled as event streams — one
100
+ // backfill, then trust the live listener forever — which makes correctness
101
+ // depend on never missing an event. MV3 cannot promise that, and the live
102
+ // buffer lives in chrome.storage.session, which is wiped on browser restart,
103
+ // so misses are guaranteed. (chrome.history is genuinely append-only, which is
104
+ // why its overlap re-scan works there and is not the model to copy here.)
105
+ //
106
+ // Worse, the two faults were cancelling out. The "already backfilled" flag was
107
+ // a chrome.storage.local sentinel written by commit(), which runs AFTER the
108
+ // completion POST, so an eviction in that gap lost it while the server had
109
+ // already recorded success — prod re-posted the identical 136-item bookmark
110
+ // tree and the identical 6 downloads on 583 of 583 syncs, every ~5 minutes.
111
+ // Since origin_id is stable per item, that wasteful re-post was quietly
112
+ // upserting over the rows and healing every missed live event. Fixing only the
113
+ // churn would have removed the repair.
114
+ //
115
+ // So the state itself is the checkpoint: fingerprint the snapshot each run and
116
+ // compare. Unchanged means emit nothing; changed or absent means emit the whole
117
+ // snapshot, which origin_id upserts make idempotent. No interval to tune and no
118
+ // dependence on catching events — and it needs no "have I backfilled" flag at
119
+ // all, because an absent fingerprint IS the first run.
120
+ //
121
+ // Deletes are out of scope either way: a removed item just vanishes from the
122
+ // snapshot, and the item stream is an upsert rather than a set-replace, so a
123
+ // removal only ever surfaces as the live onRemoved event.
124
+ function fingerprint(parts) {
125
+ // FNV-1a over the joined identity fields. Not cryptographic — this only has
126
+ // to change when the snapshot changes, and it rides in a jsonb column.
127
+ let h = 0x811c9dc5;
128
+ const text = parts.join("\u0001");
129
+ for (let i = 0; i < text.length; i++) {
130
+ h ^= text.charCodeAt(i);
131
+ h = Math.imul(h, 0x01000193);
132
+ }
133
+ return (h >>> 0).toString(16);
134
+ }
135
+
136
+ // A snapshot run: emit `rows` only when the fingerprint moved. Returns the
137
+ // checkpoint to send, or undefined to leave the stored one alone (COALESCE),
138
+ // so a settled feed stops rewriting its row every five minutes.
139
+ function snapshotDelta(run, workerId, rows, digest) {
140
+ const mine = checkpointForThisProfile(run.checkpoint, workerId);
141
+ if (mine?.snapshot_digest === digest) {
142
+ return { rows: [], checkpoint: undefined };
143
+ }
144
+ // An empty snapshot is indistinguishable from a read that silently returned
145
+ // nothing, so never let one pin the digest: that would retire the first
146
+ // emission permanently, which is the wedge this whole class keeps hitting.
147
+ if (rows.length === 0) return { rows: [], checkpoint: undefined };
148
+ return {
149
+ rows,
150
+ checkpoint: { snapshot_digest: digest, worker_id: workerId },
151
+ };
152
+ }
153
+
154
+ // One stored version per stored identity per batch, as feed_history_visits
155
+ // does. A live "created" event and the snapshot row for the same item collide,
156
+ // and emitting both makes the second supersede the first for no information
157
+ // gain. The snapshot row wins: it is the item's state right now.
158
+ //
159
+ // Keyed on item.id, because that is what the server stores as events.origin_id
160
+ // (run-lifecycle.ts: `origin_id: item.id`). metadata.origin_id is connector
161
+ // bookkeeping the server does not read for identity, and for bookmarks the two
162
+ // differ -- item.id also carries date_added -- so keying on the metadata field
163
+ // would collapse rows the server would have kept apart.
164
+ function lastWinsByStoredId(items) {
165
+ const deduped = new Map();
166
+ for (const item of items) deduped.set(item.id, item);
167
+ return [...deduped.values()];
168
+ }
169
+
170
+ // ── tab_events (no permission delta) ───────────────────────────────────
171
+
172
+ // Module-stable listener references. We attach with hasListener() guards
173
+ // so that re-installing on permissions.onAdded (or anywhere else) is a
174
+ // no-op for the listeners we already wired, preventing duplicate events.
175
+
176
+ function onTabCreated(tab) {
177
+ if (!tab.url) return;
178
+ void pushBuffered("tab_events", makeTabEvent("created", tab));
179
+ }
180
+ function onTabRemoved(tabId, removeInfo) {
181
+ void pushBuffered("tab_events", {
182
+ event_type: "removed",
183
+ tab_id: tabId,
184
+ window_id: removeInfo?.windowId,
185
+ occurred_at: new Date().toISOString(),
186
+ });
187
+ }
188
+ function onTabUpdated(tabId, change, tab) {
189
+ if (!change.url) return;
190
+ void pushBuffered("tab_events", {
191
+ event_type: "updated",
192
+ tab_id: tabId,
193
+ url: change.url,
194
+ title: tab?.title,
195
+ window_id: tab?.windowId,
196
+ occurred_at: new Date().toISOString(),
197
+ });
198
+ }
199
+ function onTabActivated(info) {
200
+ void pushBuffered("tab_events", {
201
+ event_type: "activated",
202
+ tab_id: info.tabId,
203
+ window_id: info.windowId,
204
+ occurred_at: new Date().toISOString(),
205
+ });
206
+ }
207
+
208
+ function maybeAdd(event, fn) {
209
+ if (!event) return;
210
+ if (event.hasListener?.(fn)) return;
211
+ event.addListener(fn);
212
+ }
213
+
214
+ export function installTabEventListeners(chromeApi) {
215
+ maybeAdd(chromeApi.tabs.onCreated, onTabCreated);
216
+ maybeAdd(chromeApi.tabs.onRemoved, onTabRemoved);
217
+ maybeAdd(chromeApi.tabs.onUpdated, onTabUpdated);
218
+ maybeAdd(chromeApi.tabs.onActivated, onTabActivated);
219
+ }
220
+
221
+ function makeTabEvent(eventType, tab) {
222
+ return {
223
+ event_type: eventType,
224
+ tab_id: tab.id,
225
+ url: tab.url,
226
+ title: tab.title,
227
+ window_id: tab.windowId,
228
+ occurred_at: new Date().toISOString(),
229
+ };
230
+ }
231
+
232
+ // Feed run contract (peek-then-commit):
233
+ // tx.items — the events to stream
234
+ // tx.checkpoint — optional; sent on the success completion and persisted
235
+ // by the server (COALESCE keeps the stored value when a
236
+ // run sends none). Anything that must survive a lost
237
+ // commit() lives here.
238
+ // tx.commit — async; called by the dispatcher ONLY after the success
239
+ // completion is confirmed. Clears consumed buffer entries.
240
+ // If the run fails before commit, the buffer survives
241
+ // intact and the next run retries.
242
+
243
+ export async function feed_tab_events(_chromeApi) {
244
+ const buf = await peekBuffered("tab_events");
245
+ const items = buf
246
+ .map((e) => ({
247
+ id: `tabevt-${e.tab_id}-${e.occurred_at}-${e.event_type}`,
248
+ title: `${e.event_type}: ${e.title ?? e.url ?? "tab " + e.tab_id}`,
249
+ payload_type: "text",
250
+ payload_text: e.url
251
+ ? `${e.event_type} ${e.url}`
252
+ : `${e.event_type} tab ${e.tab_id}`,
253
+ occurred_at: e.occurred_at,
254
+ source_url: e.url ?? undefined,
255
+ origin_type: "tab_event",
256
+ semantic_type: "tab_event",
257
+ metadata: {
258
+ source: "chrome_tab_events",
259
+ origin_id: `tabevt-${e.tab_id}-${e.occurred_at}-${e.event_type}`,
260
+ event_type: e.event_type,
261
+ tab_id: e.tab_id,
262
+ url: e.url,
263
+ title: e.title,
264
+ window_id: e.window_id,
265
+ },
266
+ }))
267
+ .map(sanitizeBrowserRecord);
268
+ return {
269
+ items,
270
+ commit: async () => {
271
+ await clearBuffered("tab_events", buf.length);
272
+ },
273
+ };
274
+ }
275
+
276
+ // ── chrome.history (opt-in: requires `history` permission) ─────────────
277
+
278
+ function onHistoryVisited(item) {
279
+ void pushBuffered("history.visits", normaliseVisit(item));
280
+ }
281
+ export function installHistoryListener(chromeApi) {
282
+ if (!chromeApi.history?.onVisited) return; // permission not yet granted
283
+ maybeAdd(chromeApi.history.onVisited, onHistoryVisited);
284
+ }
285
+
286
+ function normaliseVisit(item) {
287
+ const ts = item.lastVisitTime
288
+ ? new Date(item.lastVisitTime).toISOString()
289
+ : new Date().toISOString();
290
+ return {
291
+ origin_id: `visit-${item.id}-${ts}`,
292
+ url: item.url,
293
+ title: item.title,
294
+ visit_time: ts,
295
+ visit_count: item.visitCount,
296
+ typed_count: item.typedCount,
297
+ };
298
+ }
299
+
300
+ // History backfill window + per-run overlap. We re-query a rolling window
301
+ // on EVERY run rather than backfilling once and trusting the live
302
+ // onVisited buffer: chrome.storage.session (where the buffer lives) is
303
+ // wiped on browser restart, and the SW only buffers while awake, so a
304
+ // one-shot design silently drops visits — and wedged at zero forever if
305
+ // the single first-sync backfill ever returned empty. chrome.history.search
306
+ // dedupes by URL, so we keep the live buffer too for per-visit granularity;
307
+ // origin_id (URL + visit time) makes both paths idempotent on upsert.
308
+ const HISTORY_BACKFILL_MS = 90 * 24 * 60 * 60 * 1000;
309
+ const HISTORY_OVERLAP_MS = 10 * 60 * 1000;
310
+
311
+ // The window start comes from the SERVER's feed checkpoint, handed to us on
312
+ // the run payload, and the new one goes back on the success completion.
313
+ // It used to live in chrome.storage.local, written by commit() -- which runs
314
+ // AFTER the completion POST, so any interruption in that gap (an MV3 service
315
+ // worker is evictable at any await) left the checkpoint unwritten while the
316
+ // server had already recorded success. Prod ran 568 of 568 chrome.history
317
+ // syncs with feeds.checkpoint NULL, re-collecting the full 90-day window
318
+ // (1957-2781 items) every ~5 minutes: the local write is unrecoverable once
319
+ // lost, where a server round-trip self-heals on the very next poll.
320
+ //
321
+ // The window is only trusted when checkpointForThisProfile says it is ours: a
322
+ // re-paired or wiped profile would otherwise scan 10 minutes and silently skip
323
+ // its own 90 days of history.
324
+ export async function feed_history_visits(chromeApi, run = {}) {
325
+ if (!chromeApi.history) {
326
+ throw new Error("chrome.history: permission not granted");
327
+ }
328
+ // Rewrite any legacy raw entries before a Chrome API failure can abort
329
+ // this feed run.
330
+ const live = await peekBuffered("history.visits");
331
+ const rows = [];
332
+ const now = Date.now();
333
+ const workerId = await currentWorkerId();
334
+ const mine = checkpointForThisProfile(run.checkpoint, workerId);
335
+ const prev =
336
+ typeof mine?.last_sync_ms === "number" ? mine.last_sync_ms : null;
337
+ // First run (no checkpoint) reaches back HISTORY_BACKFILL_MS; later runs
338
+ // re-scan from the last sync minus an overlap to catch boundary visits.
339
+ const startTime =
340
+ prev != null
341
+ ? Math.max(0, prev - HISTORY_OVERLAP_MS)
342
+ : now - HISTORY_BACKFILL_MS;
343
+ const results = await chromeApi.history.search({
344
+ text: "",
345
+ startTime,
346
+ maxResults: 5000,
347
+ });
348
+ for (const item of results) rows.push(normaliseVisit(item));
349
+ const searchCount = rows.length;
350
+ rows.push(...live);
351
+ // One stored version per visit. history.search and the live onVisited buffer
352
+ // can both observe the SAME visit — same history item id and lastVisitTime,
353
+ // so the same origin_id — while disagreeing on `title`, because an SPA that
354
+ // rewrites its title mid-visit reports one value to each. Both landed in one
355
+ // batch, so the second upserted over the first and the pair then alternated
356
+ // on every run forever: prod held 710 stored versions of a single ChatGPT
357
+ // URL flapping between "Scheduled Tasks" and "Lobu Voice Profile | Scheduled
358
+ // Task", ~900 supersedes/hour across just 63 distinct visits. Last-wins keeps
359
+ // the live observation, which is the per-visit truth; the search row is a
360
+ // per-URL summary that only carries whatever title Chrome holds right now.
361
+ const deduped = new Map();
362
+ for (const v of rows) deduped.set(v.origin_id, v);
363
+ const items = [...deduped.values()]
364
+ .map((v) => ({
365
+ id: v.origin_id,
366
+ title: v.title ?? v.url,
367
+ payload_type: "text",
368
+ payload_text: v.title ? `${v.title}\n${v.url}` : v.url,
369
+ occurred_at: v.visit_time,
370
+ source_url: v.url,
371
+ origin_type: "page_visit",
372
+ semantic_type: "page_visit",
373
+ metadata: {
374
+ source: "chrome_history",
375
+ origin_id: v.origin_id,
376
+ url: v.url,
377
+ title: v.title,
378
+ visit_time: v.visit_time,
379
+ visit_count: v.visit_count,
380
+ },
381
+ }))
382
+ .map(sanitizeBrowserRecord);
383
+ return {
384
+ items,
385
+ // Advance the checkpoint once we're established. On the very first run,
386
+ // only advance if the deep backfill actually returned visits — a
387
+ // transient empty result must NOT pin the window, or we'd skip the
388
+ // 90-day backfill forever (the original wedge). An idle incremental run
389
+ // (0 new) still advances, since prev is already set. Left undefined, the
390
+ // server's COALESCE keeps whatever checkpoint it already holds.
391
+ checkpoint:
392
+ prev != null || searchCount > 0
393
+ ? { last_sync_ms: now, worker_id: workerId }
394
+ : undefined,
395
+ commit: async () => {
396
+ await clearBuffered("history.visits", live.length);
397
+ },
398
+ };
399
+ }
400
+
401
+ // ── chrome.bookmarks (opt-in) ──────────────────────────────────────────
402
+
403
+ function onBookmarkCreated(id, node) {
404
+ void pushBuffered("bookmarks", {
405
+ event_type: "created",
406
+ id,
407
+ node,
408
+ ts: Date.now(),
409
+ });
410
+ }
411
+ function onBookmarkRemoved(id, info) {
412
+ void pushBuffered("bookmarks", {
413
+ event_type: "removed",
414
+ id,
415
+ info,
416
+ ts: Date.now(),
417
+ });
418
+ }
419
+ function onBookmarkChanged(id, info) {
420
+ void pushBuffered("bookmarks", {
421
+ event_type: "changed",
422
+ id,
423
+ info,
424
+ ts: Date.now(),
425
+ });
426
+ }
427
+ function onBookmarkMoved(id, info) {
428
+ void pushBuffered("bookmarks", {
429
+ event_type: "moved",
430
+ id,
431
+ info,
432
+ ts: Date.now(),
433
+ });
434
+ }
435
+ export function installBookmarksListener(chromeApi) {
436
+ if (!chromeApi.bookmarks?.onCreated) return;
437
+ maybeAdd(chromeApi.bookmarks.onCreated, onBookmarkCreated);
438
+ maybeAdd(chromeApi.bookmarks.onRemoved, onBookmarkRemoved);
439
+ maybeAdd(chromeApi.bookmarks.onChanged, onBookmarkChanged);
440
+ maybeAdd(chromeApi.bookmarks.onMoved, onBookmarkMoved);
441
+ }
442
+
443
+ async function flattenBookmarkTree(nodes, path = "") {
444
+ const out = [];
445
+ for (const node of nodes) {
446
+ const nodePath = path ? `${path}/${node.title}` : node.title;
447
+ if (node.url) {
448
+ out.push({
449
+ bookmark_id: node.id,
450
+ title: node.title,
451
+ url: node.url,
452
+ parent_folder_id: node.parentId,
453
+ parent_folder_path: path,
454
+ date_added: node.dateAdded
455
+ ? new Date(node.dateAdded).toISOString()
456
+ : undefined,
457
+ });
458
+ }
459
+ if (node.children) {
460
+ out.push(...(await flattenBookmarkTree(node.children, nodePath)));
461
+ }
462
+ }
463
+ return out;
464
+ }
465
+
466
+ export async function feed_bookmarks(chromeApi, run = {}) {
467
+ if (!chromeApi.bookmarks) {
468
+ throw new Error("chrome.bookmarks: permission not granted");
469
+ }
470
+ // Clean the retry buffer before a checkpoint read or tree read can fail.
471
+ const live = await peekBuffered("bookmarks");
472
+ const workerId = await currentWorkerId();
473
+ const tree = await chromeApi.bookmarks.getTree();
474
+ const flat = await flattenBookmarkTree(tree);
475
+ const snapshot = flat.map((b) => ({ event_type: "created", ...b }));
476
+ const digest = fingerprint(
477
+ flat.map(
478
+ (b) =>
479
+ `${b.bookmark_id}|${b.title ?? ""}|${b.url ?? ""}|${b.parent_folder_id ?? ""}`,
480
+ ),
481
+ );
482
+ const delta = snapshotDelta(run, workerId, snapshot, digest);
483
+ const rows = [];
484
+ for (const ev of live) {
485
+ rows.push({
486
+ event_type: ev.event_type,
487
+ bookmark_id: ev.id,
488
+ title: ev.node?.title ?? ev.info?.title,
489
+ url: ev.node?.url ?? ev.info?.url,
490
+ parent_folder_id: ev.node?.parentId ?? ev.info?.parentId,
491
+ date_added: ev.node?.dateAdded
492
+ ? new Date(ev.node.dateAdded).toISOString()
493
+ : new Date(ev.ts).toISOString(),
494
+ });
495
+ }
496
+ rows.push(...delta.rows);
497
+ const items = rows
498
+ .map((b) => ({
499
+ id: `bookmark-${b.bookmark_id}-${b.event_type}-${b.date_added ?? Date.now()}`,
500
+ title: b.title ?? b.url ?? "(bookmark)",
501
+ payload_type: "text",
502
+ payload_text: b.title ? `${b.title}\n${b.url ?? ""}` : (b.url ?? ""),
503
+ occurred_at: b.date_added ?? new Date().toISOString(),
504
+ source_url: b.url,
505
+ origin_type: "bookmark",
506
+ semantic_type: "bookmark",
507
+ metadata: {
508
+ source: "chrome_bookmarks",
509
+ origin_id: `bookmark-${b.bookmark_id}-${b.event_type}`,
510
+ event_type: b.event_type,
511
+ bookmark_id: b.bookmark_id,
512
+ title: b.title,
513
+ url: b.url,
514
+ parent_folder_id: b.parent_folder_id,
515
+ parent_folder_path: b.parent_folder_path,
516
+ date_added: b.date_added,
517
+ },
518
+ }))
519
+ .map(sanitizeBrowserRecord);
520
+ return {
521
+ items: lastWinsByStoredId(items),
522
+ checkpoint: delta.checkpoint,
523
+ commit: async () => {
524
+ await clearBuffered("bookmarks", live.length);
525
+ },
526
+ };
527
+ }
528
+
529
+ // ── chrome.downloads (opt-in) ──────────────────────────────────────────
530
+
531
+ function onDownloadCreated(item) {
532
+ void pushBuffered("downloads", normaliseDownload(item));
533
+ }
534
+ // Keep a reference to the chromeApi the listener was installed against
535
+ // so we can look up the final item when it completes.
536
+ let _downloadsChromeApi = null;
537
+ function onDownloadChanged(delta) {
538
+ if (delta?.state?.current !== "complete") return;
539
+ if (!_downloadsChromeApi?.downloads?.search) return;
540
+ void (async () => {
541
+ try {
542
+ const [item] = await _downloadsChromeApi.downloads.search({
543
+ id: delta.id,
544
+ });
545
+ if (item) await pushBuffered("downloads", normaliseDownload(item));
546
+ } catch {}
547
+ })();
548
+ }
549
+ export function installDownloadsListener(chromeApi) {
550
+ if (!chromeApi.downloads?.onCreated) return;
551
+ _downloadsChromeApi = chromeApi;
552
+ maybeAdd(chromeApi.downloads.onCreated, onDownloadCreated);
553
+ maybeAdd(chromeApi.downloads.onChanged, onDownloadChanged);
554
+ }
555
+
556
+ function normaliseDownload(item) {
557
+ return {
558
+ origin_id: `download-${item.id}-${item.state ?? "started"}`,
559
+ download_id: item.id,
560
+ filename: item.filename,
561
+ source_url: item.url,
562
+ referrer: item.referrer,
563
+ mime: item.mime,
564
+ bytes: item.totalBytes,
565
+ started_at: item.startTime,
566
+ finished_at: item.endTime,
567
+ state: item.state,
568
+ };
569
+ }
570
+
571
+ export async function feed_downloads(chromeApi, run = {}) {
572
+ if (!chromeApi.downloads) {
573
+ throw new Error("chrome.downloads: permission not granted");
574
+ }
575
+ // Clean the retry buffer before a checkpoint read or snapshot read can fail.
576
+ const live = await peekBuffered("downloads");
577
+ const workerId = await currentWorkerId();
578
+ // Capped at the most recent 1000 to bound the read; the fingerprint is over
579
+ // the same window, so it moves whenever that window's contents move.
580
+ const results = await chromeApi.downloads.search({ limit: 1000 });
581
+ const snapshot = results.map(normaliseDownload);
582
+ const digest = fingerprint(
583
+ snapshot.map((d) => `${d.origin_id}|${d.filename ?? ""}|${d.state ?? ""}`),
584
+ );
585
+ const delta = snapshotDelta(run, workerId, snapshot, digest);
586
+ const rows = [...live, ...delta.rows];
587
+ const items = rows
588
+ .map((d) => ({
589
+ id: d.origin_id,
590
+ title: d.filename ?? d.source_url ?? "(download)",
591
+ payload_type: "text",
592
+ payload_text: `${d.filename ?? ""}\n${d.source_url ?? ""}`,
593
+ occurred_at: d.finished_at ?? d.started_at ?? new Date().toISOString(),
594
+ source_url: d.source_url,
595
+ origin_type: "download",
596
+ semantic_type: "download",
597
+ metadata: {
598
+ source: "chrome_downloads",
599
+ origin_id: d.origin_id,
600
+ download_id: d.download_id,
601
+ filename: d.filename,
602
+ source_url: d.source_url,
603
+ referrer: d.referrer,
604
+ mime: d.mime,
605
+ bytes: d.bytes,
606
+ started_at: d.started_at,
607
+ finished_at: d.finished_at,
608
+ state: d.state,
609
+ },
610
+ }))
611
+ .map(sanitizeBrowserRecord);
612
+ return {
613
+ items: lastWinsByStoredId(items),
614
+ checkpoint: delta.checkpoint,
615
+ commit: async () => {
616
+ await clearBuffered("downloads", live.length);
617
+ },
618
+ };
619
+ }
620
+
621
+ // ── listener-install dispatcher ────────────────────────────────────────
622
+
623
+ // Called from background.js on startup AND whenever permissions change
624
+ // (chrome.permissions.onAdded). Each install* is idempotent — Chrome's
625
+ // addListener silently dedupes the same function reference, and our
626
+ // functions are module-scoped so they're identity-stable across calls.
627
+ export function installFeedListeners(chromeApi) {
628
+ installTabEventListeners(chromeApi);
629
+ installHistoryListener(chromeApi);
630
+ installBookmarksListener(chromeApi);
631
+ installDownloadsListener(chromeApi);
632
+ }
@@ -0,0 +1,25 @@
1
+ export async function focusChromeTab(chromeApi, tabId, options = {}) {
2
+ if (typeof tabId !== "number" || !Number.isFinite(tabId)) {
3
+ throw new Error("focus_tab: 'tab_id' is required");
4
+ }
5
+
6
+ const tab = await chromeApi.tabs.get(tabId);
7
+ if (!tab?.id) {
8
+ throw new Error(`focus_tab: tab ${tabId} not found`);
9
+ }
10
+
11
+ const windowId = tab.windowId ?? null;
12
+ if (windowId != null) {
13
+ try {
14
+ await chromeApi.windows.update(windowId, {
15
+ focused: true,
16
+ ...(options.drawAttention === false ? {} : { drawAttention: true }),
17
+ });
18
+ } catch {
19
+ // Activating the tab is still useful if the window focus call fails.
20
+ }
21
+ }
22
+
23
+ await chromeApi.tabs.update(tabId, { active: true });
24
+ return { focused: true, tab_id: tabId, window_id: windowId };
25
+ }