Repository navigation
feat(script): evaluate scripts in dedicated worker contexts - #2352
gunjanjaswal wants to merge 7 commits into
Conversation
Add opt-in support for enumerating and evaluating JavaScript inside a page's dedicated Web Workers, so agents can inspect off-main-thread app logic (e.g. Neo.mjs). The feature is gated behind a new --experimentalWorkers flag and is off by default, so worker execution contexts are never enumerated unless explicitly requested and the baseline token usage is unchanged. - Add list_dedicated_workers tool to list a page's dedicated workers and assign each a stable worker id. - Add an optional workerId argument to evaluate_script that resolves the target through the existing Page | Frame | WebWorker union, mirroring the existing serviceWorkerId path. Refs ChromeDevTools#316
…worker-execution-context # Conflicts: # src/McpContext.ts
…worker-execution-context # Conflicts: # src/telemetry/tool_call_metrics.json
…worker-execution-context # Conflicts: # src/McpContext.ts
…worker-execution-context # Conflicts: # src/telemetry/flag_usage_metrics.json # tests/tools/script.test.ts
1f23d38 to
bb57f9f
Compare
…worker-execution-context
|
Thanks for the PR! I think it looks good but I will try to do a review next week. |
…worker-execution-context # Conflicts: # src/telemetry/tool_call_metrics.json
|
Thanks @OrKoN! I've just rebased it on main to clear the conflict (the tool-metrics file needed my |
| * Spawns a dedicated worker in the page and resolves once Puppeteer has | ||
| * attached to it. | ||
| */ | ||
| async function spawnDedicatedWorker(page: Page): Promise<void> { |
There was a problem hiding this comment.
Could we move it to test utils?
| }); | ||
|
|
||
| export const listDedicatedWorkers = defineTool({ | ||
| name: 'list_dedicated_workers', |
There was a problem hiding this comment.
I have been thinking if we need a dedicated tool to list workers or if we can just list the workers in the pages response similar to service workers are handled includeExtensionServiceWorkers? I think ideally we could land #2434 first to streamline the interactions with workers.
|
Thanks @OrKoN. On the test helper, yes, I'll move it to test utils. On the bigger question: listing workers in the pages response the way Happy to sequence it that way. Would you prefer I pick up #2434 first and then rework this on top of |
|
hi @gunjanjaswal, thanks, if you are still interested, let's work on #2434 first. I will close this PR for now to reduce the amount of open PRs but let's re-open it after refactoring is done. |
Closes ChromeDevTools#2434. Worker handling reached through Puppeteer targets directly: the context stored plain `ExtensionServiceWorker` objects, kept their ids in a side `WeakMap`, and callers such as the script evaluator resolved `target.worker()` themselves. This introduces an `McpWorker` class that mirrors `McpPage`, wrapping a worker target together with its stable id, `type`, `url`, and a lazy `worker()` accessor, so dedicated and service workers can share a single representation. ### What changed - New `src/McpWorker.ts` with an `McpWorker` class and a `WorkerType` (`service_worker` | `dedicated_worker` | `shared_worker`), plus a small per-type id prefix so ids never collide across types. - `McpContext` snapshots into `McpWorker` instances via `getWorkers()` / `createWorkersSnapshot()`. The id now lives on the worker, so the separate `getExtensionServiceWorkerId()` lookup is gone. - `ServiceWorkerConsoleCollector`, the `browser_evaluate_script` worker resolver, and the extension-service-worker response section consume the abstraction. The response filters by `type === 'service_worker'`, so the "Extension Service Workers" listing stays correct once other worker types start appearing. - `ExtensionServiceWorker` removed from `types.ts`. ### Scope No behavior change: the snapshot still tracks only extension service workers, and the `sw-N` id scheme is preserved (existing tests exercise the same ids). This is deliberately just the abstraction — surfacing dedicated workers is left to ChromeDevTools#2352, which should rebase cleanly on top. `tsc`, eslint, and prettier are clean, and the build passes.
Adds opt-in support for evaluating JavaScript inside a page's dedicated Web Workers, so agents can reach off-main-thread application logic (e.g. Neo.mjs).
Everything is gated behind a new
--experimentalWorkersflag and off by default. Worker contexts are never enumerated unless explicitly requested, so baseline token usage is unchanged.list_dedicated_workerslists the selected page's dedicated workers and assigns each a stable worker id.evaluate_scriptgains an optionalworkerIdargument that resolves the target through the existingPage | Frame | WebWorkerunion, mirroring the existingserviceWorkerIdpath (same mutual-exclusivity and no-element-args guards).Shared workers (#317) are intentionally left out of scope.
Tests cover both tools and the new evaluate path.
build,typecheck,eslint,prettier, andnpm run gen(docs + CLI reference) all run clean.Refs #316