@lobu/runtime-device 24.1.1 → 24.3.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 (194) hide show
  1. package/dist/component.json +1 -1
  2. package/dist/dependencies.bun.lock +46 -134
  3. package/npm-shrinkwrap.json +229 -730
  4. package/package.json +1 -1
  5. package/vendor/cli/dist/bundled-skills/lobu/SKILL.md +20 -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/chat.d.ts +0 -12
  78. package/vendor/cli/dist/commands/chat.js +1 -1
  79. package/vendor/cli/dist/commands/context.d.ts +3 -1
  80. package/vendor/cli/dist/commands/context.js +13 -2
  81. package/vendor/cli/dist/commands/daemon.js +38 -171
  82. package/vendor/cli/dist/commands/doctor.d.ts +0 -6
  83. package/vendor/cli/dist/commands/doctor.js +1 -1
  84. package/vendor/cli/dist/commands/memory/_lib/errors.d.ts +4 -1
  85. package/vendor/cli/dist/commands/memory/_lib/errors.js +9 -1
  86. package/vendor/cli/dist/commands/memory/_lib/mcp.d.ts +0 -9
  87. package/vendor/cli/dist/commands/memory/_lib/mcp.js +1 -23
  88. package/vendor/cli/dist/commands/memory/_lib/memory-auth.d.ts +20 -4
  89. package/vendor/cli/dist/commands/memory/_lib/memory-auth.js +21 -17
  90. package/vendor/cli/dist/commands/memory/_lib/seed-cmd.d.ts +0 -8
  91. package/vendor/cli/dist/commands/memory/_lib/seed-cmd.js +7 -18
  92. package/vendor/cli/dist/commands/memory/run.js +4 -25
  93. package/vendor/cli/dist/config/define.d.ts +1 -1
  94. package/vendor/cli/dist/connectors/gchat.ts +31 -1
  95. package/vendor/cli/dist/connectors/google_gmail.ts +70 -41
  96. package/vendor/cli/dist/connectors/google_gmail_references.ts +406 -0
  97. package/vendor/cli/dist/connectors/linkedin-inbox-adapter.js +107 -36
  98. package/vendor/cli/dist/connectors/linkedin-inbox.ts +13 -0
  99. package/vendor/cli/dist/connectors/linkedin.ts +1 -1
  100. package/vendor/cli/dist/connectors/postgres.ts +14 -8
  101. package/vendor/cli/dist/connectors/reddit.ts +171 -5
  102. package/vendor/cli/dist/connectors/whatsapp-web-adapter.js +147 -61
  103. package/vendor/cli/dist/connectors/whatsapp-web-helpers.ts +6 -2
  104. package/vendor/cli/dist/connectors/whatsapp_web.ts +48 -17
  105. package/vendor/cli/dist/connectors/x-dm-adapter.js +252 -35
  106. package/vendor/cli/dist/connectors/x.ts +189 -13
  107. package/vendor/cli/dist/index.js +46 -6
  108. package/vendor/cli/dist/internal/cli-update-check.d.ts +33 -0
  109. package/vendor/cli/dist/internal/cli-update-check.js +98 -0
  110. package/vendor/cli/dist/internal/credentials.js +10 -36
  111. package/vendor/cli/dist/internal/device-state.d.ts +2 -0
  112. package/vendor/cli/dist/internal/device-state.js +4 -0
  113. package/vendor/cli/dist/internal/gateway-url.d.ts +0 -8
  114. package/vendor/cli/dist/internal/gateway-url.js +1 -1
  115. package/vendor/cli/dist/runtime-components.json +4 -4
  116. package/vendor/cli/dist/templates/AGENTS.md.tmpl +3 -1
  117. package/vendor/cli/dist/templates/TESTING.md.tmpl +8 -8
  118. package/vendor/cli/package.json +1 -1
  119. package/vendor/connector-sdk/dist/browser-requirement.js +1 -1
  120. package/vendor/connector-sdk/dist/connector-types.d.ts +34 -1
  121. package/vendor/connector-sdk/dist/index.d.ts +3 -1
  122. package/vendor/connector-sdk/dist/index.js +4 -0
  123. package/vendor/connector-sdk/dist/redact.d.ts +10 -0
  124. package/vendor/{connector-worker/dist/executor → connector-sdk/dist}/redact.js +0 -49
  125. package/vendor/connector-sdk/dist/types.d.ts +2 -1
  126. package/vendor/connector-sdk/package.json +6 -1
  127. package/vendor/connector-worker/dist/agent-turn/gateway-tools.d.ts +9 -0
  128. package/vendor/connector-worker/dist/agent-turn/gateway-tools.js +23 -2
  129. package/vendor/connector-worker/dist/agent-turn/guest-entry.js +14 -6
  130. package/vendor/connector-worker/dist/agent-turn/guest.bundle.js +62 -25
  131. package/vendor/connector-worker/dist/agent-turn/media-tools.d.ts +1 -0
  132. package/vendor/connector-worker/dist/agent-turn/media-tools.js +5 -2
  133. package/vendor/connector-worker/dist/bin.js +2 -0
  134. package/vendor/connector-worker/dist/daemon/acp-adapters/THIRD_PARTY_LICENSES.txt +1 -1
  135. package/vendor/connector-worker/dist/daemon/acp-adapters/claude.js +7339 -2136
  136. package/vendor/connector-worker/dist/daemon/agent-turn.js +52 -17
  137. package/vendor/connector-worker/dist/daemon/automation.js +3 -13
  138. package/vendor/connector-worker/dist/daemon/client.d.ts +23 -3
  139. package/vendor/connector-worker/dist/daemon/client.js +59 -5
  140. package/vendor/connector-worker/dist/daemon/executor.d.ts +0 -28
  141. package/vendor/connector-worker/dist/daemon/executor.js +27 -12
  142. package/vendor/connector-worker/dist/daemon/index.d.ts +1 -1
  143. package/vendor/connector-worker/dist/daemon/index.js +1 -1
  144. package/vendor/connector-worker/dist/daemon/interactive-session.js +29 -6
  145. package/vendor/connector-worker/dist/daemon/poll-loop.js +29 -2
  146. package/vendor/connector-worker/dist/daemon/start.d.ts +0 -1
  147. package/vendor/connector-worker/dist/daemon/start.js +1 -1
  148. package/vendor/connector-worker/dist/daemon/terminal-delivery.d.ts +6 -10
  149. package/vendor/connector-worker/dist/daemon/terminal-delivery.js +59 -23
  150. package/vendor/connector-worker/dist/daemon/terminal-failure.js +2 -2
  151. package/vendor/connector-worker/dist/executor/isolate.js +4 -1
  152. package/vendor/connector-worker/dist/isolate/bridge.d.ts +6 -2
  153. package/vendor/connector-worker/dist/isolate/bridge.js +14 -8
  154. package/vendor/connector-worker/package.json +2 -2
  155. package/vendor/core/dist/agent-tooling.d.ts +8 -0
  156. package/vendor/core/dist/agent-tooling.js +9 -1
  157. package/vendor/core/dist/capabilities.d.ts +1 -1
  158. package/vendor/core/dist/capabilities.js +5 -0
  159. package/vendor/core/dist/connector-query-errors.d.ts +6 -0
  160. package/vendor/core/dist/connector-query-errors.js +4 -0
  161. package/vendor/core/dist/contracts/entity-reference.d.ts +3 -0
  162. package/vendor/core/dist/contracts/entity-reference.js +31 -0
  163. package/vendor/core/dist/contracts/tools/manage-automations.d.ts +3 -0
  164. package/vendor/core/dist/contracts/tools/manage-automations.js +4 -1
  165. package/vendor/core/dist/contracts/tools/manage-connections.js +1 -1
  166. package/vendor/core/dist/contracts/tools/manage-entity-schema.js +2 -2
  167. package/vendor/core/dist/contracts/tools/manage-entity.d.ts +117 -34
  168. package/vendor/core/dist/contracts/tools/manage-entity.js +47 -14
  169. package/vendor/core/dist/contracts/tools/manage-operations.d.ts +13 -4
  170. package/vendor/core/dist/contracts/tools/manage-operations.js +19 -7
  171. package/vendor/core/dist/contracts/tools/read-knowledge.d.ts +6 -1
  172. package/vendor/core/dist/contracts/tools/read-knowledge.js +20 -1
  173. package/vendor/core/dist/contracts/tools/view-attach.d.ts +1 -1
  174. package/vendor/core/dist/contracts/tools/view-runtime.d.ts +2 -0
  175. package/vendor/core/dist/contracts/tools/view-runtime.js +3 -1
  176. package/vendor/core/dist/contracts/worker/device-automation.d.ts +4 -9
  177. package/vendor/core/dist/contracts/worker/device-automation.js +10 -15
  178. package/vendor/core/dist/contracts/worker/protocol.d.ts +20 -0
  179. package/vendor/core/dist/contracts/worker/protocol.js +20 -0
  180. package/vendor/core/dist/errors.d.ts +0 -20
  181. package/vendor/core/dist/errors.js +1 -25
  182. package/vendor/core/dist/otel.js +38 -10
  183. package/vendor/core/dist/reserved.d.ts +6 -3
  184. package/vendor/core/dist/reserved.js +6 -3
  185. package/vendor/core/dist/utils/ajv.js +3 -1
  186. package/vendor/core/dist/worker/auth.d.ts +3 -2
  187. package/vendor/core/package.json +31 -5
  188. package/vendor/postgres/cf/src/query.js +3 -1
  189. package/vendor/postgres/cjs/src/query.js +3 -1
  190. package/vendor/postgres/src/query.js +3 -1
  191. package/vendor/connector-worker/dist/executor/http-status.d.ts +0 -16
  192. package/vendor/connector-worker/dist/executor/http-status.js +0 -21
  193. package/vendor/connector-worker/dist/executor/redact.d.ts +0 -29
  194. /package/vendor/postgres/{.bun-tag-2f292c6554a8475e → .bun-tag-22d644c2873790db} +0 -0
@@ -0,0 +1,657 @@
1
+ // Network interception primitive for the chrome connector.
2
+ //
3
+ // Exposes four action_keys:
4
+ //
5
+ // network_intercept_start({ tab_id?, patterns, max_buffer_responses?,
6
+ // max_body_bytes? })
7
+ // Attach the CDP Network domain to the tab and start buffering response
8
+ // bodies whose URL matches one of `patterns` (strings → globs; arrays
9
+ // of {regex} → RegExp). The first call on a tab adopts the run's
10
+ // scratch tab if `tab_id` is omitted. Returns
11
+ // `{ session_id, tab_id }`. Calling start() again with the same session
12
+ // id is idempotent — useful after a service-worker eviction.
13
+ //
14
+ // network_intercept_drain({ session_id })
15
+ // Return all buffered intercepted responses for `session_id`. Atomically
16
+ // clears the buffer so subsequent drain() calls see only fresh entries.
17
+ //
18
+ // network_intercept_replay({ session_id, url })
19
+ // Replay the latest matching GET with a replacement URL. Browser-owned
20
+ // authorization headers and cookies never leave the extension; only the
21
+ // resulting status is returned. The response is captured by the same
22
+ // session and becomes available through drain().
23
+ //
24
+ // network_intercept_stop({ session_id })
25
+ // Remove the CDP event listener for `session_id` and delete the buffer.
26
+ // Network domain stays enabled if other live sessions on the same tab
27
+ // are still listening; otherwise the next withDebugger() on that tab
28
+ // will detach naturally.
29
+ //
30
+ // Wire model: each session = one row in chrome.storage.session under
31
+ // `owletto.netint.sessions.<session_id>` with shape
32
+ // { tab_id, patterns, buffer: Array<{url, status, mime, body, ts}>,
33
+ // latest_request?: {url, method, headers} }.
34
+ // Persisting to chrome.storage.session is the same surface the tab-group
35
+ // store uses — it survives MV3 service-worker eviction,
36
+ // which is critical because a `start` → `drain` round-trip from the
37
+ // gateway may straddle multiple SW wakeups.
38
+ //
39
+ // Concurrency: multiple sessions can coexist on the same tab. The CDP
40
+ // Network listener is registered once per session; events are matched
41
+ // against each session's pattern set independently. Two connectors that
42
+ // care about the same URL each get an independent copy in their own
43
+ // buffer.
44
+ //
45
+ // Body size: bodies bigger than `max_body_bytes` (default 1 MiB) are
46
+ // truncated and flagged `truncated: true`. Each session caps total
47
+ // buffered responses at `max_buffer_responses` (default 100) — oldest
48
+ // entries drop first (FIFO).
49
+ //
50
+ // Security: respects the connector's `allowedOrigins` runCtx exactly the
51
+ // same way as tools.js — the start() call enforces against the tab's
52
+ // current URL, and every body URL checked at drain time is matched
53
+ // against the same allowlist (a session can't be coerced into capturing
54
+ // off-allowlist responses by a redirect).
55
+
56
+ const SESSION_KEY_PREFIX = 'owletto.netint.sessions.';
57
+ const ACTIVE_LISTENERS_KEY = 'owletto.netint.active_listeners';
58
+
59
+ const DEFAULT_MAX_BUFFER_RESPONSES = 100;
60
+ const DEFAULT_MAX_BODY_BYTES = 1 * 1024 * 1024;
61
+
62
+ // Shared debugger lease/refcount. Both withDebugger() (action tools) and
63
+ // tool_network_intercept_start hold leases on the same tabId;
64
+ // chrome.debugger.attach/detach happen only on 0→1 / 1→0 transitions so a
65
+ // long-lived intercept session coexists cleanly with per-call action tools
66
+ // on the same tab. Lives in its own module to avoid a tools.js ↔
67
+ // network-intercept.js import cycle.
68
+ import {
69
+ acquireDebuggerLease,
70
+ debuggerDetachGeneration,
71
+ onExternalDebuggerDetach,
72
+ prepareDiscardedTab,
73
+ releaseDebuggerLease,
74
+ } from "./debugger-lease.js";
75
+ import { browserUrlAllowed, assertBrowserSessionGrant } from "./browser-origin-policy.js";
76
+
77
+ // In-memory per-SW-instance: session_id → { tabId, patterns,
78
+ // matchesUrl, allowedOrigins, listener, requestIdToUrl Map }.
79
+ // On SW eviction this is empty on next wake; the persisted session row
80
+ // in chrome.storage.session lets start() re-attach idempotently.
81
+ const LIVE_SESSIONS = new Map();
82
+
83
+ // chrome.storage.session has no compare-and-swap. Serialize each session's
84
+ // read-modify-write operations so recording a replay template cannot clobber a
85
+ // response body (or vice versa), and stop cannot race a late write that would
86
+ // resurrect the deleted session row.
87
+ const SESSION_MUTATIONS = new Map();
88
+
89
+ function withSessionMutation(sessionId, fn) {
90
+ const previous = SESSION_MUTATIONS.get(sessionId) ?? Promise.resolve();
91
+ const next = previous.then(fn, fn);
92
+ const tail = next.then(
93
+ () => {},
94
+ () => {},
95
+ );
96
+ SESSION_MUTATIONS.set(sessionId, tail);
97
+ tail.then(() => {
98
+ if (SESSION_MUTATIONS.get(sessionId) === tail) SESSION_MUTATIONS.delete(sessionId);
99
+ });
100
+ return next;
101
+ }
102
+
103
+ // External detach (user cancels the debugging infobar, DevTools takes the
104
+ // target, tab closes) kills the CDP attachment out from under every live
105
+ // session on that tab. Drop the dead in-memory entries so a same-session
106
+ // start() performs a REAL reattach instead of returning resumed:true on a
107
+ // listener that will never fire again. The persisted row is intentionally
108
+ // kept — the resume revives its buffer. No lease release here: the lease
109
+ // module hard-resets the tab's refcount on this same event, and stop() gates
110
+ // its release on the live entry we just removed.
111
+ onExternalDebuggerDetach((chromeApi, tabId) => {
112
+ for (const [sessionId, live] of LIVE_SESSIONS) {
113
+ if (live.tabId !== tabId) continue;
114
+ chromeApi.debugger.onEvent.removeListener(live.listener);
115
+ LIVE_SESSIONS.delete(sessionId);
116
+ }
117
+ });
118
+
119
+ function dbgTarget(tabId) {
120
+ return { tabId };
121
+ }
122
+
123
+ function sessionKey(sessionId) {
124
+ return `${SESSION_KEY_PREFIX}${sessionId}`;
125
+ }
126
+
127
+ async function readSession(chromeApi, sessionId) {
128
+ const key = sessionKey(sessionId);
129
+ const stored = await chromeApi.storage.session.get(key);
130
+ return stored[key] ?? null;
131
+ }
132
+
133
+ async function writeSession(chromeApi, sessionId, row) {
134
+ await chromeApi.storage.session.set({ [sessionKey(sessionId)]: row });
135
+ }
136
+
137
+ async function deleteSession(chromeApi, sessionId) {
138
+ await chromeApi.storage.session.remove(sessionKey(sessionId));
139
+ }
140
+
141
+ function compileGlob(pattern) {
142
+ // Mirror connector-sdk/browser-network.ts compileGlob — keep the wire
143
+ // contract symmetric so a pattern that worked on the Playwright path
144
+ // works here unchanged.
145
+ const DOUBLE_STAR = '\x00DOUBLE_STAR\x00';
146
+ const escaped = pattern
147
+ .replace(/\*\*/g, DOUBLE_STAR)
148
+ .replace(/[.+^${}()|[\]\\]/g, '\\$&')
149
+ .replace(/\*/g, '[^/]*')
150
+ .split(DOUBLE_STAR)
151
+ .join('.*');
152
+ return new RegExp(escaped);
153
+ }
154
+
155
+ function buildMatchers(patterns) {
156
+ if (!Array.isArray(patterns) || patterns.length === 0) {
157
+ throw new Error('network_intercept: `patterns` must be a non-empty array');
158
+ }
159
+ const matchers = patterns.map((p) => {
160
+ if (typeof p === 'string') return compileGlob(p);
161
+ // Wire-format for regex: { regex: '<source>', flags?: '<i|g|m>' }.
162
+ // Server-side connectors can't ship a JS RegExp through JSON, so they
163
+ // serialize it as this object.
164
+ if (p && typeof p === 'object' && typeof p.regex === 'string') {
165
+ return new RegExp(p.regex, typeof p.flags === 'string' ? p.flags : '');
166
+ }
167
+ throw new Error(
168
+ 'network_intercept: each pattern must be a glob string or { regex, flags? } object',
169
+ );
170
+ });
171
+ return matchers;
172
+ }
173
+
174
+ function makeMatcher(matchers) {
175
+ return (url) => matchers.some((re) => re.test(url));
176
+ }
177
+
178
+ // Keep only the application headers X needs to reproduce an authenticated
179
+ // GraphQL GET. Cookie remains browser-owned through credentials: include, and
180
+ // browser-controlled/hop-by-hop headers are never copied into page fetch().
181
+ function replayHeaders(headers) {
182
+ const safe = {};
183
+ for (const [rawName, rawValue] of Object.entries(headers ?? {})) {
184
+ const name = rawName.toLowerCase();
185
+ if (
186
+ name === 'accept' ||
187
+ name === 'content-type' ||
188
+ name === 'authorization' ||
189
+ name === 'x-csrf-token' ||
190
+ name.startsWith('x-twitter-') ||
191
+ name === 'x-client-transaction-id'
192
+ ) {
193
+ safe[name] = String(rawValue);
194
+ }
195
+ }
196
+ return safe;
197
+ }
198
+
199
+ function generateSessionId() {
200
+ // crypto.randomUUID is available in MV3 SWs (Chrome 92+); minimum is 120
201
+ // in manifest.json so this is safe.
202
+ return `netint-${globalThis.crypto.randomUUID()}`;
203
+ }
204
+
205
+ // ── start ────────────────────────────────────────────────────────────────
206
+ //
207
+ // Lock-free: if `args.session_id` is supplied we reuse it (idempotent on
208
+ // SW wake); otherwise mint a fresh one. We never lock per tab here because
209
+ // the Network listener is additive — withDebugger / withTabLock in tools.js
210
+ // owns the debugger lifecycle for action tools; this primitive just enables
211
+ // the Network domain and stays attached for the session's lifetime.
212
+ export async function tool_network_intercept_start(chromeApi, args, runCtx) {
213
+ const tabId = args?.tab_id ?? runCtx.scratchTabId;
214
+ if (tabId == null) {
215
+ throw new Error('network_intercept_start: no tab (pass tab_id or use after navigate)');
216
+ }
217
+ const sessionId = args?.session_id ?? generateSessionId();
218
+
219
+ // Already-live in this SW instance → reuse after waking, UNLESS an external
220
+ // detach orphaned it (generation moved since the entry was published — see
221
+ // debugger-lease.js). A stale entry is dropped so we fall through to a real
222
+ // reattach instead of resuming a dead listener on a lease reset to 0.
223
+ assertBrowserSessionGrant((await readSession(chromeApi, sessionId)) ?? LIVE_SESSIONS.get(sessionId), runCtx);
224
+ const live = LIVE_SESSIONS.get(sessionId);
225
+ if (live) {
226
+ if (debuggerDetachGeneration(live.tabId) === live.detachGen) {
227
+ await prepareDiscardedTab(chromeApi, live.tabId, () =>
228
+ enforceTabUrlAgainstAllowedOrigins(chromeApi, live.tabId, { allowedOrigins: live.allowed_origins }),
229
+ );
230
+ if (debuggerDetachGeneration(live.tabId) === live.detachGen) {
231
+ return {
232
+ ok: true,
233
+ observation: { session_id: sessionId, tab_id: live.tabId, resumed: true },
234
+ };
235
+ }
236
+ }
237
+ chromeApi.debugger.onEvent.removeListener(live.listener);
238
+ LIVE_SESSIONS.delete(sessionId);
239
+ }
240
+
241
+ // An existing persisted row (SW-eviction resume, or the detach-reattach just
242
+ // above) is authoritative for the session's tab, policy, limits, and — most
243
+ // importantly — its buffered responses: rewriting with buffer:[] would drop
244
+ // everything captured before the resume. args/runCtx configure NEW sessions.
245
+ const existing = await readSession(chromeApi, sessionId);
246
+ const effectiveTabId = existing?.tab_id ?? tabId;
247
+
248
+ // Config comes from the stored row on resume, or from validated args on a
249
+ // new session. buildMatchers throws on empty/malformed patterns — only
250
+ // enforced for new sessions; a resume inherits the stored patterns and must
251
+ // not require the caller to re-send them.
252
+ let effectivePatterns;
253
+ let effectiveAllowedOrigins;
254
+ let effectiveMaxBufferResponses;
255
+ let effectiveMaxBodyBytes;
256
+ if (existing) {
257
+ effectivePatterns = existing.patterns;
258
+ effectiveAllowedOrigins = existing.allowed_origins ?? [];
259
+ effectiveMaxBufferResponses = existing.max_buffer_responses;
260
+ effectiveMaxBodyBytes = existing.max_body_bytes;
261
+ } else {
262
+ buildMatchers(args?.patterns); // validate; matchers rebuilt after attach
263
+ effectivePatterns = args.patterns;
264
+ effectiveAllowedOrigins = runCtx.allowedOrigins ?? [];
265
+ effectiveMaxBufferResponses = clampInt(
266
+ args?.max_buffer_responses,
267
+ 1,
268
+ 1000,
269
+ DEFAULT_MAX_BUFFER_RESPONSES,
270
+ );
271
+ effectiveMaxBodyBytes = clampInt(
272
+ args?.max_body_bytes,
273
+ 1024,
274
+ 10 * 1024 * 1024,
275
+ DEFAULT_MAX_BODY_BYTES,
276
+ );
277
+ }
278
+
279
+ // allowedOrigins enforcement on the *current* tab URL — keeps the primitive
280
+ // symmetric with click_ref / type_ref / evaluate. Validate the EFFECTIVE tab
281
+ // + policy (stored ones on resume) before committing any state.
282
+ await enforceTabUrlAgainstAllowedOrigins(chromeApi, effectiveTabId, {
283
+ allowedOrigins: effectiveAllowedOrigins,
284
+ });
285
+ // Before the row is written: a refusal must not touch a resumed session.
286
+ await prepareDiscardedTab(chromeApi, effectiveTabId);
287
+
288
+ // Persist first so a SW eviction between start() and the CDP attach can't
289
+ // leave a dangling listener / phantom buffer. Preserve the existing buffer.
290
+ await writeSession(chromeApi, sessionId, {
291
+ tab_id: effectiveTabId,
292
+ patterns: effectivePatterns,
293
+ allowed_origins: effectiveAllowedOrigins,
294
+ max_buffer_responses: effectiveMaxBufferResponses,
295
+ max_body_bytes: effectiveMaxBodyBytes,
296
+ buffer: Array.isArray(existing?.buffer) ? existing.buffer : [],
297
+ latest_request: existing?.latest_request,
298
+ created_at: existing?.created_at ?? Date.now(),
299
+ });
300
+
301
+ // Take a shared debugger lease (see tools.js). If no one else is
302
+ // attached on this tab, the lease attaches; if a withDebugger() action
303
+ // tool currently holds the tab, the lease just bumps the refcount and
304
+ // we ride the existing attach. We keep the lease for the session's
305
+ // lifetime (intentionally NOT under withDebugger which auto-detaches at
306
+ // end of fn), and release it in tool_network_intercept_stop when the
307
+ // last live session on this tab ends. This is what makes a later
308
+ // navigate / evaluate via withDebugger on the same tab safe: it sees a
309
+ // non-zero lease, skips the attach, runs, decrements back. The 1→0
310
+ // detach is deferred until intercept_stop.
311
+ let lease;
312
+ try {
313
+ lease = await acquireDebuggerLease(chromeApi, effectiveTabId);
314
+ } catch (err) {
315
+ await deleteSession(chromeApi, sessionId);
316
+ throw err;
317
+ }
318
+
319
+ // Rebuild matchers from the EFFECTIVE patterns (stored ones on resume), so a
320
+ // reattach keeps matching what the original session was configured for.
321
+ const matches = makeMatcher(buildMatchers(effectivePatterns));
322
+ const requestIdToUrl = new Map(); // CDP requestId → URL (filled by responseReceived)
323
+ const requestIdToMime = new Map();
324
+ const requestIdToStatus = new Map();
325
+
326
+ const listener = (source, method, params) => {
327
+ if (source.tabId !== effectiveTabId) return;
328
+ if (method === 'Network.requestWillBeSent') {
329
+ const request = params?.request;
330
+ const url = request?.url;
331
+ if (!url || !matches(url)) return;
332
+ if (!browserUrlAllowed(url, effectiveAllowedOrigins)) return;
333
+ void rememberLatestRequest(chromeApi, sessionId, {
334
+ url,
335
+ method: request?.method ?? 'GET',
336
+ headers: replayHeaders(request?.headers),
337
+ });
338
+ return;
339
+ }
340
+ if (method === 'Network.responseReceived') {
341
+ const url = params?.response?.url;
342
+ if (!url || !matches(url)) return;
343
+ if (!browserUrlAllowed(url, effectiveAllowedOrigins)) return;
344
+ requestIdToUrl.set(params.requestId, url);
345
+ requestIdToMime.set(params.requestId, params.response?.mimeType ?? '');
346
+ requestIdToStatus.set(params.requestId, params.response?.status ?? 0);
347
+ return;
348
+ }
349
+ if (method === 'Network.loadingFinished') {
350
+ const url = requestIdToUrl.get(params?.requestId);
351
+ if (!url) return;
352
+ const reqId = params.requestId;
353
+ // Fire-and-buffer. We don't await here — listener must return
354
+ // synchronously to keep CDP delivery in order.
355
+ void captureBody(chromeApi, effectiveTabId, sessionId, reqId, url, {
356
+ mime: requestIdToMime.get(reqId) ?? '',
357
+ status: requestIdToStatus.get(reqId) ?? 0,
358
+ maxBodyBytes: effectiveMaxBodyBytes,
359
+ maxBufferResponses: effectiveMaxBufferResponses,
360
+ });
361
+ requestIdToUrl.delete(reqId);
362
+ requestIdToMime.delete(reqId);
363
+ requestIdToStatus.delete(reqId);
364
+ }
365
+ };
366
+
367
+ // Install before enabling the domain: events can arrive before the
368
+ // Network.enable promise settles, and the page's first requests are often
369
+ // the ones a capture is for.
370
+ chromeApi.debugger.onEvent.addListener(listener);
371
+
372
+ try {
373
+ await chromeApi.debugger.sendCommand(dbgTarget(effectiveTabId), 'Network.enable', {});
374
+ } catch (err) {
375
+ // If Network.enable fails the session is unusable; drop the listener,
376
+ // release the lease we just acquired and clean up the persisted row.
377
+ chromeApi.debugger.onEvent.removeListener(listener);
378
+ await releaseDebuggerLease(chromeApi, lease);
379
+ await deleteSession(chromeApi, sessionId);
380
+ throw err;
381
+ }
382
+
383
+ // detachGen comes from the LEASE token (read atomically with acquisition),
384
+ // not a pre-acquire sample; store the lease so stop() releases with the same
385
+ // token.
386
+ LIVE_SESSIONS.set(sessionId, {
387
+ allowed_origins: effectiveAllowedOrigins,
388
+ tabId: effectiveTabId,
389
+ detachGen: lease.generation,
390
+ lease,
391
+ listener,
392
+ requestIdToUrl,
393
+ requestIdToMime,
394
+ requestIdToStatus,
395
+ });
396
+
397
+ return {
398
+ ok: true,
399
+ observation: { session_id: sessionId, tab_id: effectiveTabId, resumed: false },
400
+ };
401
+ }
402
+
403
+ async function rememberLatestRequest(chromeApi, sessionId, request) {
404
+ await withSessionMutation(sessionId, async () => {
405
+ const row = await readSession(chromeApi, sessionId);
406
+ if (!row) return;
407
+ row.latest_request = request;
408
+ await writeSession(chromeApi, sessionId, row);
409
+ });
410
+ }
411
+
412
+ async function captureBody(
413
+ chromeApi,
414
+ tabId,
415
+ sessionId,
416
+ requestId,
417
+ url,
418
+ { mime, status, maxBodyBytes, maxBufferResponses },
419
+ ) {
420
+ try {
421
+ const res = await chromeApi.debugger.sendCommand(dbgTarget(tabId), 'Network.getResponseBody', {
422
+ requestId,
423
+ });
424
+ let body = res?.body ?? '';
425
+ const base64Encoded = !!res?.base64Encoded;
426
+ let truncated = false;
427
+ if (body.length > maxBodyBytes) {
428
+ body = body.slice(0, maxBodyBytes);
429
+ truncated = true;
430
+ }
431
+ await withSessionMutation(sessionId, async () => {
432
+ const row = await readSession(chromeApi, sessionId);
433
+ if (!row) return; // session was stopped between event + getResponseBody
434
+ row.buffer.push({
435
+ url,
436
+ status,
437
+ mime,
438
+ body,
439
+ base64_encoded: base64Encoded,
440
+ truncated,
441
+ ts: Date.now(),
442
+ });
443
+ // FIFO cap.
444
+ while (row.buffer.length > maxBufferResponses) row.buffer.shift();
445
+ await writeSession(chromeApi, sessionId, row);
446
+ });
447
+ } catch {
448
+ // Common: Network.getResponseBody fails when the body has already
449
+ // been freed (long-running listener, low-memory page). Swallow —
450
+ // the connector will see one fewer intercepted response, which
451
+ // matches the Playwright path's best-effort semantics.
452
+ }
453
+ }
454
+
455
+ // ── drain ────────────────────────────────────────────────────────────────
456
+
457
+ export async function tool_network_intercept_drain(chromeApi, args, runCtx = {}) {
458
+ const sessionId = args?.session_id;
459
+ if (typeof sessionId !== 'string') {
460
+ throw new Error('network_intercept_drain: `session_id` is required');
461
+ }
462
+ const drained = await withSessionMutation(sessionId, async () => {
463
+ const row = await readSession(chromeApi, sessionId);
464
+ if (!row) return null;
465
+ assertBrowserSessionGrant(row, runCtx);
466
+ const responses = row.buffer ?? [];
467
+ row.buffer = [];
468
+ await writeSession(chromeApi, sessionId, row);
469
+ return responses;
470
+ });
471
+ if (!drained) {
472
+ return {
473
+ ok: true,
474
+ observation: { session_id: sessionId, responses: [], drained: 0, missing: true },
475
+ };
476
+ }
477
+ return {
478
+ ok: true,
479
+ observation: {
480
+ session_id: sessionId,
481
+ responses: drained,
482
+ drained: drained.length,
483
+ missing: false,
484
+ },
485
+ };
486
+ }
487
+
488
+ // ── replay ────────────────────────────────────────────────────────────────
489
+
490
+ export async function tool_network_intercept_replay(chromeApi, args, runCtx = {}) {
491
+ const sessionId = args?.session_id;
492
+ if (typeof sessionId !== 'string') {
493
+ throw new Error('network_intercept_replay: `session_id` is required');
494
+ }
495
+ if (typeof args?.url !== 'string') {
496
+ throw new Error('network_intercept_replay: `url` is required');
497
+ }
498
+
499
+ // Join the same queue as request capture so replay cannot read just before a
500
+ // pending requestWillBeSent write publishes the browser-owned template.
501
+ const row = await withSessionMutation(sessionId, () => readSession(chromeApi, sessionId));
502
+ if (!row) throw new Error(`network_intercept_replay: session '${sessionId}' is not active`);
503
+ assertBrowserSessionGrant(row, runCtx);
504
+ const live = LIVE_SESSIONS.get(sessionId);
505
+ if (!live) {
506
+ throw new Error(
507
+ `network_intercept_replay: session '${sessionId}' must be resumed before replay`,
508
+ );
509
+ }
510
+ const template = row.latest_request;
511
+ if (!template) {
512
+ throw new Error('network_intercept_replay: no matching browser request has been observed');
513
+ }
514
+ if (String(template.method).toUpperCase() !== 'GET') {
515
+ throw new Error('network_intercept_replay: only captured GET requests can be replayed');
516
+ }
517
+
518
+ let requested;
519
+ let original;
520
+ try {
521
+ requested = new URL(args.url);
522
+ original = new URL(template.url);
523
+ } catch {
524
+ throw new Error('network_intercept_replay: `url` must be an absolute URL');
525
+ }
526
+ if (requested.protocol !== 'https:') {
527
+ throw new Error('network_intercept_replay: only HTTPS URLs can be replayed');
528
+ }
529
+ if (requested.origin !== original.origin) {
530
+ throw new Error('network_intercept_replay: replacement URL must keep the captured origin');
531
+ }
532
+ if (!browserUrlAllowed(requested.href, row.allowed_origins ?? [])) {
533
+ throw new Error('network_intercept_replay: replacement URL is outside allowedOrigins');
534
+ }
535
+ if (!makeMatcher(buildMatchers(row.patterns))(requested.href)) {
536
+ throw new Error('network_intercept_replay: replacement URL does not match session patterns');
537
+ }
538
+
539
+ const expression = `(async () => {
540
+ const response = await fetch(${JSON.stringify(requested.href)}, {
541
+ method: "GET",
542
+ headers: ${JSON.stringify(template.headers ?? {})},
543
+ credentials: "include"
544
+ });
545
+ await response.arrayBuffer();
546
+ return { ok: response.ok, status: response.status };
547
+ })()`;
548
+ const evaluated = await chromeApi.debugger.sendCommand(
549
+ dbgTarget(live.tabId),
550
+ 'Runtime.evaluate',
551
+ { expression, awaitPromise: true, returnByValue: true },
552
+ );
553
+ if (evaluated?.exceptionDetails) {
554
+ throw new Error(
555
+ `network_intercept_replay: browser fetch failed: ${evaluated.exceptionDetails.text ?? 'unknown error'}`,
556
+ );
557
+ }
558
+ const value = evaluated?.result?.value;
559
+ return {
560
+ ok: true,
561
+ observation: {
562
+ session_id: sessionId,
563
+ requested: true,
564
+ ok: value?.ok === true,
565
+ status: typeof value?.status === 'number' ? value.status : 0,
566
+ },
567
+ };
568
+ }
569
+
570
+ // ── stop ─────────────────────────────────────────────────────────────────
571
+
572
+ export async function tool_network_intercept_stop(chromeApi, args, runCtx = {}) {
573
+ const sessionId = args?.session_id;
574
+ if (typeof sessionId !== 'string') {
575
+ throw new Error('network_intercept_stop: `session_id` is required');
576
+ }
577
+ assertBrowserSessionGrant((await readSession(chromeApi, sessionId)) ?? LIVE_SESSIONS.get(sessionId), runCtx);
578
+ const live = LIVE_SESSIONS.get(sessionId);
579
+ if (live) {
580
+ chromeApi.debugger.onEvent.removeListener(live.listener);
581
+ LIVE_SESSIONS.delete(sessionId);
582
+ }
583
+ await withSessionMutation(sessionId, () => deleteSession(chromeApi, sessionId));
584
+ // Release our debugger lease. The shared refcount in tools.js handles
585
+ // detach: this only physically detaches when no other session AND no
586
+ // action tool is currently holding the tab. Action tools that
587
+ // overlapped via withDebugger will have already released their lease at
588
+ // the end of their inner fn — so if WE were the last lease holder the
589
+ // detach happens here.
590
+ //
591
+ // Gate on `live` alone, not the storage row: the lease is acquired exactly
592
+ // once when the LIVE_SESSIONS entry is created (start path), so `live` is the
593
+ // authoritative "we hold a lease" signal. Requiring the row too leaked the
594
+ // lease (refcount stuck > 0, blocking future detach) whenever the row was
595
+ // deleted first — e.g. a racing stop or chrome.storage.session eviction.
596
+ if (live) {
597
+ // Release with the stored lease TOKEN so a lease superseded by an external
598
+ // detach (refcount already reset) is a no-op instead of decrementing a
599
+ // newer lease's count.
600
+ await releaseDebuggerLease(chromeApi, live.lease);
601
+ }
602
+ return {
603
+ ok: true,
604
+ observation: { session_id: sessionId, stopped: true },
605
+ };
606
+ }
607
+
608
+ // ── helpers ──────────────────────────────────────────────────────────────
609
+
610
+ async function enforceTabUrlAgainstAllowedOrigins(chromeApi, tabId, runCtx) {
611
+ if (!runCtx?.allowedOrigins || runCtx.allowedOrigins.length === 0) return;
612
+ const tab = await chromeApi.tabs.get(tabId);
613
+ if (!tab?.url) {
614
+ throw new Error(
615
+ `network_intercept: tab ${tabId} has no URL to validate against allowedOrigins`,
616
+ );
617
+ }
618
+ // about:blank is the scratch-tab setup URL used by extensionNetworkSync.
619
+ // The pattern is: open scratch tab (about:blank) → start the Network
620
+ // listener → navigate to the real allowed-origin URL. Rejecting
621
+ // about:blank here would block the only safe ordering for capturing the
622
+ // page's initial-render XHRs (responses landing before our listener
623
+ // attaches are lost). The buffered listener itself still enforces the
624
+ // allowlist on every captured response URL, so this carve-out is just
625
+ // for the bare-tab attach, not for what gets recorded.
626
+ if (tab.url === 'about:blank') return;
627
+ if (!browserUrlAllowed(tab.url, runCtx.allowedOrigins)) {
628
+ throw new Error(
629
+ `network_intercept: '${tab.url}' is not in allowedOrigins [${runCtx.allowedOrigins.join(', ')}]`,
630
+ );
631
+ }
632
+ }
633
+
634
+ function clampInt(value, min, max, fallback) {
635
+ if (typeof value !== 'number' || !Number.isFinite(value)) return fallback;
636
+ const v = Math.floor(value);
637
+ if (v < min) return min;
638
+ if (v > max) return max;
639
+ return v;
640
+ }
641
+
642
+ // Test-only: lets unit tests reset the in-memory listener map between cases.
643
+ // Production code never imports this — the SW lifecycle handles it.
644
+ export function __resetLiveSessionsForTests() {
645
+ LIVE_SESSIONS.clear();
646
+ SESSION_MUTATIONS.clear();
647
+ }
648
+
649
+ export const NETWORK_INTERCEPT_TOOLS = {
650
+ network_intercept_start: tool_network_intercept_start,
651
+ network_intercept_drain: tool_network_intercept_drain,
652
+ network_intercept_replay: tool_network_intercept_replay,
653
+ network_intercept_stop: tool_network_intercept_stop,
654
+ };
655
+
656
+ // Exported for testing/debug; the canonical buffer is in chrome.storage.session.
657
+ export const ACTIVE_LISTENERS_KEY_NAME = ACTIVE_LISTENERS_KEY;