@nordhavnhq/runner 0.0.0-stage → 0.95.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.
- package/bin/cliclick +0 -0
- package/dist/brand.d.ts +5 -0
- package/dist/brand.js +18 -0
- package/dist/cli/agent.d.ts +9 -0
- package/dist/cli/agent.js +80 -0
- package/dist/cli/folder.d.ts +2 -0
- package/dist/cli/folder.js +103 -0
- package/dist/cli/init.d.ts +2 -0
- package/dist/cli/init.js +67 -0
- package/dist/cli/lifecycle.d.ts +3 -0
- package/dist/cli/lifecycle.js +149 -0
- package/dist/cli/link.d.ts +42 -0
- package/dist/cli/link.js +314 -0
- package/dist/cli/logs.d.ts +2 -0
- package/dist/cli/logs.js +147 -0
- package/dist/cli/prompts.d.ts +7 -0
- package/dist/cli/prompts.js +13 -0
- package/dist/cli/ps.d.ts +2 -0
- package/dist/cli/ps.js +81 -0
- package/dist/cli/relay.d.ts +4 -0
- package/dist/cli/relay.js +29 -0
- package/dist/cli/update.d.ts +6 -0
- package/dist/cli/update.js +131 -0
- package/dist/config.d.ts +166 -0
- package/dist/config.js +383 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +145 -0
- package/dist/legacy-env.d.ts +4 -0
- package/dist/legacy-env.js +32 -0
- package/dist/paths.d.ts +37 -0
- package/dist/paths.js +92 -0
- package/dist/runtime/daemon.d.ts +57 -0
- package/dist/runtime/daemon.js +159 -0
- package/dist/runtime/dev-start.d.ts +31 -0
- package/dist/runtime/dev-start.js +110 -0
- package/dist/runtime/file-claims.d.ts +58 -0
- package/dist/runtime/file-claims.js +174 -0
- package/dist/runtime/fs-browse.d.ts +18 -0
- package/dist/runtime/fs-browse.js +104 -0
- package/dist/runtime/fs-checkout.d.ts +154 -0
- package/dist/runtime/fs-checkout.js +1049 -0
- package/dist/runtime/fs-git-index.d.ts +22 -0
- package/dist/runtime/fs-git-index.js +133 -0
- package/dist/runtime/fs-git.d.ts +16 -0
- package/dist/runtime/fs-git.js +424 -0
- package/dist/runtime/git-relay.d.ts +46 -0
- package/dist/runtime/git-relay.js +259 -0
- package/dist/runtime/git-unpushed.d.ts +36 -0
- package/dist/runtime/git-unpushed.js +93 -0
- package/dist/runtime/node-version.d.ts +12 -0
- package/dist/runtime/node-version.js +39 -0
- package/dist/runtime/preview-tunnel.d.ts +16 -0
- package/dist/runtime/preview-tunnel.js +135 -0
- package/dist/runtime/processes.d.ts +61 -0
- package/dist/runtime/processes.js +408 -0
- package/dist/runtime/project-node-download.d.ts +17 -0
- package/dist/runtime/project-node-download.js +137 -0
- package/dist/runtime/project-node.d.ts +65 -0
- package/dist/runtime/project-node.js +223 -0
- package/dist/runtime/reap-scratch.d.ts +22 -0
- package/dist/runtime/reap-scratch.js +112 -0
- package/dist/runtime/spill-tool-output.d.ts +14 -0
- package/dist/runtime/spill-tool-output.js +84 -0
- package/dist/runtime/up.d.ts +6 -0
- package/dist/runtime/up.js +1325 -0
- package/dist/tools/_coerce.d.ts +2 -0
- package/dist/tools/_coerce.js +26 -0
- package/dist/tools/_commit-scope.d.ts +14 -0
- package/dist/tools/_commit-scope.js +70 -0
- package/dist/tools/_jail.d.ts +68 -0
- package/dist/tools/_jail.js +209 -0
- package/dist/tools/_output.d.ts +24 -0
- package/dist/tools/_output.js +48 -0
- package/dist/tools/_ripgrep.d.ts +46 -0
- package/dist/tools/_ripgrep.js +148 -0
- package/dist/tools/_run-worktree.d.ts +10 -0
- package/dist/tools/_run-worktree.js +27 -0
- package/dist/tools/_shell-words.d.ts +15 -0
- package/dist/tools/_shell-words.js +43 -0
- package/dist/tools/_sonar/astro.d.ts +6 -0
- package/dist/tools/_sonar/astro.js +45 -0
- package/dist/tools/_sonar/blade.d.ts +3 -0
- package/dist/tools/_sonar/blade.js +53 -0
- package/dist/tools/_sonar/build.d.ts +44 -0
- package/dist/tools/_sonar/build.js +147 -0
- package/dist/tools/_sonar/changed.d.ts +21 -0
- package/dist/tools/_sonar/changed.js +230 -0
- package/dist/tools/_sonar/diagnose/delta.d.ts +27 -0
- package/dist/tools/_sonar/diagnose/delta.js +64 -0
- package/dist/tools/_sonar/diagnose/detect.d.ts +39 -0
- package/dist/tools/_sonar/diagnose/detect.js +117 -0
- package/dist/tools/_sonar/diagnose/index.d.ts +13 -0
- package/dist/tools/_sonar/diagnose/index.js +217 -0
- package/dist/tools/_sonar/diagnose/lint-worker.d.ts +1 -0
- package/dist/tools/_sonar/diagnose/lint-worker.js +48 -0
- package/dist/tools/_sonar/diagnose/pool.d.ts +22 -0
- package/dist/tools/_sonar/diagnose/pool.js +149 -0
- package/dist/tools/_sonar/diagnose/rules.d.ts +23 -0
- package/dist/tools/_sonar/diagnose/rules.js +131 -0
- package/dist/tools/_sonar/diagnose/types-worker.d.ts +1 -0
- package/dist/tools/_sonar/diagnose/types-worker.js +183 -0
- package/dist/tools/_sonar/files.d.ts +14 -0
- package/dist/tools/_sonar/files.js +121 -0
- package/dist/tools/_sonar/graph.d.ts +34 -0
- package/dist/tools/_sonar/graph.js +164 -0
- package/dist/tools/_sonar/hints.d.ts +116 -0
- package/dist/tools/_sonar/hints.js +545 -0
- package/dist/tools/_sonar/imports.d.ts +38 -0
- package/dist/tools/_sonar/imports.js +275 -0
- package/dist/tools/_sonar/index.d.ts +79 -0
- package/dist/tools/_sonar/index.js +462 -0
- package/dist/tools/_sonar/keys.d.ts +5 -0
- package/dist/tools/_sonar/keys.js +248 -0
- package/dist/tools/_sonar/laravel-routes.d.ts +15 -0
- package/dist/tools/_sonar/laravel-routes.js +263 -0
- package/dist/tools/_sonar/laravel.d.ts +64 -0
- package/dist/tools/_sonar/laravel.js +326 -0
- package/dist/tools/_sonar/location.d.ts +33 -0
- package/dist/tools/_sonar/location.js +137 -0
- package/dist/tools/_sonar/map.d.ts +43 -0
- package/dist/tools/_sonar/map.js +212 -0
- package/dist/tools/_sonar/parse.d.ts +3 -0
- package/dist/tools/_sonar/parse.js +76 -0
- package/dist/tools/_sonar/prewarm.d.ts +9 -0
- package/dist/tools/_sonar/prewarm.js +96 -0
- package/dist/tools/_sonar/prisma.d.ts +9 -0
- package/dist/tools/_sonar/prisma.js +51 -0
- package/dist/tools/_sonar/refs.d.ts +7 -0
- package/dist/tools/_sonar/refs.js +273 -0
- package/dist/tools/_sonar/render.d.ts +66 -0
- package/dist/tools/_sonar/render.js +498 -0
- package/dist/tools/_sonar/resolve.d.ts +63 -0
- package/dist/tools/_sonar/resolve.js +402 -0
- package/dist/tools/_sonar/roots.d.ts +4 -0
- package/dist/tools/_sonar/roots.js +42 -0
- package/dist/tools/_sonar/routes.d.ts +5 -0
- package/dist/tools/_sonar/routes.js +247 -0
- package/dist/tools/_sonar/serve.d.ts +12 -0
- package/dist/tools/_sonar/serve.js +104 -0
- package/dist/tools/_sonar/suggest.d.ts +23 -0
- package/dist/tools/_sonar/suggest.js +114 -0
- package/dist/tools/_sonar/tests.d.ts +21 -0
- package/dist/tools/_sonar/tests.js +134 -0
- package/dist/tools/_sonar/tsconfig.d.ts +34 -0
- package/dist/tools/_sonar/tsconfig.js +188 -0
- package/dist/tools/_sonar/types.d.ts +81 -0
- package/dist/tools/_sonar/types.js +6 -0
- package/dist/tools/_symbols.d.ts +27 -0
- package/dist/tools/_symbols.js +461 -0
- package/dist/tools/_test-report.d.ts +47 -0
- package/dist/tools/_test-report.js +203 -0
- package/dist/tools/_touched.d.ts +44 -0
- package/dist/tools/_touched.js +195 -0
- package/dist/tools/_upload.d.ts +26 -0
- package/dist/tools/_upload.js +132 -0
- package/dist/tools/_worktree-setup.d.ts +29 -0
- package/dist/tools/_worktree-setup.js +204 -0
- package/dist/tools/browser.d.ts +2 -0
- package/dist/tools/browser.js +8 -0
- package/dist/tools/commit-guard.d.ts +44 -0
- package/dist/tools/commit-guard.js +165 -0
- package/dist/tools/computer.d.ts +13 -0
- package/dist/tools/computer.js +952 -0
- package/dist/tools/console.d.ts +44 -0
- package/dist/tools/console.js +739 -0
- package/dist/tools/database.d.ts +28 -0
- package/dist/tools/database.js +152 -0
- package/dist/tools/filesystem.d.ts +36 -0
- package/dist/tools/filesystem.js +506 -0
- package/dist/tools/git-tag.d.ts +10 -0
- package/dist/tools/git-tag.js +164 -0
- package/dist/tools/git.d.ts +52 -0
- package/dist/tools/git.js +1272 -0
- package/dist/tools/http.d.ts +12 -0
- package/dist/tools/http.js +119 -0
- package/dist/tools/image.d.ts +8 -0
- package/dist/tools/image.js +551 -0
- package/dist/tools/index.d.ts +13 -0
- package/dist/tools/index.js +58 -0
- package/dist/tools/outline.d.ts +20 -0
- package/dist/tools/outline.js +98 -0
- package/dist/tools/search.d.ts +17 -0
- package/dist/tools/search.js +391 -0
- package/dist/tools/sonar.d.ts +17 -0
- package/dist/tools/sonar.js +231 -0
- package/dist/tools/testing.d.ts +36 -0
- package/dist/tools/testing.js +273 -0
- package/dist/tools/upload.d.ts +8 -0
- package/dist/tools/upload.js +106 -0
- package/dist/tools/webcam.d.ts +28 -0
- package/dist/tools/webcam.js +967 -0
- package/dist/update/install-kind.d.ts +35 -0
- package/dist/update/install-kind.js +109 -0
- package/dist/update/install.d.ts +32 -0
- package/dist/update/install.js +187 -0
- package/dist/update/manifest.d.ts +18 -0
- package/dist/update/manifest.js +73 -0
- package/dist/update/peers.d.ts +18 -0
- package/dist/update/peers.js +43 -0
- package/dist/update/policy.d.ts +20 -0
- package/dist/update/policy.js +49 -0
- package/dist/update/supervisor.d.ts +25 -0
- package/dist/update/supervisor.js +83 -0
- package/dist/update/updater.d.ts +44 -0
- package/dist/update/updater.js +116 -0
- package/dist/utils/format.d.ts +50 -0
- package/dist/utils/format.js +190 -0
- package/dist/version.d.ts +17 -0
- package/dist/version.js +63 -0
- package/dist/version.json +5 -0
- package/node_modules/@nordhavnhq/runner-browser/dist/act-tools.d.ts +15 -0
- package/node_modules/@nordhavnhq/runner-browser/dist/act-tools.js +541 -0
- package/node_modules/@nordhavnhq/runner-browser/dist/cast-hub.d.ts +25 -0
- package/node_modules/@nordhavnhq/runner-browser/dist/cast-hub.js +137 -0
- package/node_modules/@nordhavnhq/runner-browser/dist/guard.d.ts +21 -0
- package/node_modules/@nordhavnhq/runner-browser/dist/guard.js +104 -0
- package/node_modules/@nordhavnhq/runner-browser/dist/host-protocol.d.ts +47 -0
- package/node_modules/@nordhavnhq/runner-browser/dist/host-protocol.js +41 -0
- package/node_modules/@nordhavnhq/runner-browser/dist/host.d.ts +18 -0
- package/node_modules/@nordhavnhq/runner-browser/dist/host.js +15 -0
- package/node_modules/@nordhavnhq/runner-browser/dist/index.d.ts +13 -0
- package/node_modules/@nordhavnhq/runner-browser/dist/index.js +28 -0
- package/node_modules/@nordhavnhq/runner-browser/dist/live-input.d.ts +74 -0
- package/node_modules/@nordhavnhq/runner-browser/dist/live-input.js +93 -0
- package/node_modules/@nordhavnhq/runner-browser/dist/live-open.d.ts +4 -0
- package/node_modules/@nordhavnhq/runner-browser/dist/live-open.js +57 -0
- package/node_modules/@nordhavnhq/runner-browser/dist/live.d.ts +148 -0
- package/node_modules/@nordhavnhq/runner-browser/dist/live.js +179 -0
- package/node_modules/@nordhavnhq/runner-browser/dist/local-live.d.ts +31 -0
- package/node_modules/@nordhavnhq/runner-browser/dist/local-live.js +262 -0
- package/node_modules/@nordhavnhq/runner-browser/dist/marks.d.ts +36 -0
- package/node_modules/@nordhavnhq/runner-browser/dist/marks.js +173 -0
- package/node_modules/@nordhavnhq/runner-browser/dist/page-events.d.ts +27 -0
- package/node_modules/@nordhavnhq/runner-browser/dist/page-events.js +82 -0
- package/node_modules/@nordhavnhq/runner-browser/dist/page-tools.d.ts +13 -0
- package/node_modules/@nordhavnhq/runner-browser/dist/page-tools.js +526 -0
- package/node_modules/@nordhavnhq/runner-browser/dist/params.d.ts +12 -0
- package/node_modules/@nordhavnhq/runner-browser/dist/params.js +32 -0
- package/node_modules/@nordhavnhq/runner-browser/dist/pick.d.ts +24 -0
- package/node_modules/@nordhavnhq/runner-browser/dist/pick.js +76 -0
- package/node_modules/@nordhavnhq/runner-browser/dist/session.d.ts +225 -0
- package/node_modules/@nordhavnhq/runner-browser/dist/session.js +507 -0
- package/node_modules/@nordhavnhq/runner-browser/dist/shots.d.ts +11 -0
- package/node_modules/@nordhavnhq/runner-browser/dist/shots.js +103 -0
- package/node_modules/@nordhavnhq/runner-browser/dist/show.d.ts +10 -0
- package/node_modules/@nordhavnhq/runner-browser/dist/show.js +190 -0
- package/node_modules/@nordhavnhq/runner-browser/dist/target.d.ts +37 -0
- package/node_modules/@nordhavnhq/runner-browser/dist/target.js +82 -0
- package/node_modules/@nordhavnhq/runner-browser/dist/tour.d.ts +19 -0
- package/node_modules/@nordhavnhq/runner-browser/dist/tour.js +178 -0
- package/node_modules/@nordhavnhq/runner-browser/package.json +18 -0
- package/node_modules/@nordhavnhq/runner-runtime/dist/config.d.ts +96 -0
- package/node_modules/@nordhavnhq/runner-runtime/dist/config.js +25 -0
- package/node_modules/@nordhavnhq/runner-runtime/dist/connection.d.ts +276 -0
- package/node_modules/@nordhavnhq/runner-runtime/dist/connection.js +507 -0
- package/node_modules/@nordhavnhq/runner-runtime/dist/constants.d.ts +48 -0
- package/node_modules/@nordhavnhq/runner-runtime/dist/constants.js +95 -0
- package/node_modules/@nordhavnhq/runner-runtime/dist/contract/batch.d.ts +94 -0
- package/node_modules/@nordhavnhq/runner-runtime/dist/contract/batch.js +219 -0
- package/node_modules/@nordhavnhq/runner-runtime/dist/contract/calls.d.ts +136 -0
- package/node_modules/@nordhavnhq/runner-runtime/dist/contract/calls.js +214 -0
- package/node_modules/@nordhavnhq/runner-runtime/dist/contract/fs.d.ts +735 -0
- package/node_modules/@nordhavnhq/runner-runtime/dist/contract/fs.js +40 -0
- package/node_modules/@nordhavnhq/runner-runtime/dist/contract/loop.d.ts +362 -0
- package/node_modules/@nordhavnhq/runner-runtime/dist/contract/loop.js +703 -0
- package/node_modules/@nordhavnhq/runner-runtime/dist/contract/own-files.d.ts +105 -0
- package/node_modules/@nordhavnhq/runner-runtime/dist/contract/own-files.js +202 -0
- package/node_modules/@nordhavnhq/runner-runtime/dist/contract/phases.d.ts +204 -0
- package/node_modules/@nordhavnhq/runner-runtime/dist/contract/phases.js +497 -0
- package/node_modules/@nordhavnhq/runner-runtime/dist/contract/pins.d.ts +45 -0
- package/node_modules/@nordhavnhq/runner-runtime/dist/contract/pins.js +176 -0
- package/node_modules/@nordhavnhq/runner-runtime/dist/contract/processes.d.ts +74 -0
- package/node_modules/@nordhavnhq/runner-runtime/dist/contract/processes.js +72 -0
- package/node_modules/@nordhavnhq/runner-runtime/dist/contract/release-tag.d.ts +59 -0
- package/node_modules/@nordhavnhq/runner-runtime/dist/contract/release-tag.js +117 -0
- package/node_modules/@nordhavnhq/runner-runtime/dist/contract/replay.d.ts +62 -0
- package/node_modules/@nordhavnhq/runner-runtime/dist/contract/replay.js +84 -0
- package/node_modules/@nordhavnhq/runner-runtime/dist/contract/tool-names.d.ts +27 -0
- package/node_modules/@nordhavnhq/runner-runtime/dist/contract/tool-names.js +80 -0
- package/node_modules/@nordhavnhq/runner-runtime/dist/contract/version.d.ts +22 -0
- package/node_modules/@nordhavnhq/runner-runtime/dist/contract/version.js +22 -0
- package/node_modules/@nordhavnhq/runner-runtime/dist/contract/wire.d.ts +595 -0
- package/node_modules/@nordhavnhq/runner-runtime/dist/contract/wire.js +85 -0
- package/node_modules/@nordhavnhq/runner-runtime/dist/index.d.ts +43 -0
- package/node_modules/@nordhavnhq/runner-runtime/dist/index.js +50 -0
- package/node_modules/@nordhavnhq/runner-runtime/dist/inference.d.ts +135 -0
- package/node_modules/@nordhavnhq/runner-runtime/dist/inference.js +945 -0
- package/node_modules/@nordhavnhq/runner-runtime/dist/mcp.d.ts +67 -0
- package/node_modules/@nordhavnhq/runner-runtime/dist/mcp.js +47 -0
- package/node_modules/@nordhavnhq/runner-runtime/dist/platform.d.ts +21 -0
- package/node_modules/@nordhavnhq/runner-runtime/dist/platform.js +25 -0
- package/node_modules/@nordhavnhq/runner-runtime/dist/runtime/auto-load.d.ts +39 -0
- package/node_modules/@nordhavnhq/runner-runtime/dist/runtime/auto-load.js +67 -0
- package/node_modules/@nordhavnhq/runner-runtime/dist/runtime/background-commands.d.ts +61 -0
- package/node_modules/@nordhavnhq/runner-runtime/dist/runtime/background-commands.js +58 -0
- package/node_modules/@nordhavnhq/runner-runtime/dist/runtime/chat-run-ends.d.ts +23 -0
- package/node_modules/@nordhavnhq/runner-runtime/dist/runtime/chat-run-ends.js +42 -0
- package/node_modules/@nordhavnhq/runner-runtime/dist/runtime/duration.d.ts +6 -0
- package/node_modules/@nordhavnhq/runner-runtime/dist/runtime/duration.js +12 -0
- package/node_modules/@nordhavnhq/runner-runtime/dist/runtime/events.d.ts +150 -0
- package/node_modules/@nordhavnhq/runner-runtime/dist/runtime/events.js +36 -0
- package/node_modules/@nordhavnhq/runner-runtime/dist/runtime/extract-json.d.ts +14 -0
- package/node_modules/@nordhavnhq/runner-runtime/dist/runtime/extract-json.js +49 -0
- package/node_modules/@nordhavnhq/runner-runtime/dist/runtime/handle-chat.d.ts +109 -0
- package/node_modules/@nordhavnhq/runner-runtime/dist/runtime/handle-chat.js +894 -0
- package/node_modules/@nordhavnhq/runner-runtime/dist/runtime/handle-task.d.ts +73 -0
- package/node_modules/@nordhavnhq/runner-runtime/dist/runtime/handle-task.js +765 -0
- package/node_modules/@nordhavnhq/runner-runtime/dist/runtime/loop.d.ts +246 -0
- package/node_modules/@nordhavnhq/runner-runtime/dist/runtime/loop.js +621 -0
- package/node_modules/@nordhavnhq/runner-runtime/dist/runtime/messages.d.ts +88 -0
- package/node_modules/@nordhavnhq/runner-runtime/dist/runtime/messages.js +188 -0
- package/node_modules/@nordhavnhq/runner-runtime/dist/runtime/pending-steers.d.ts +15 -0
- package/node_modules/@nordhavnhq/runner-runtime/dist/runtime/pending-steers.js +10 -0
- package/node_modules/@nordhavnhq/runner-runtime/dist/runtime/phased-loop.d.ts +16 -0
- package/node_modules/@nordhavnhq/runner-runtime/dist/runtime/phased-loop.js +97 -0
- package/node_modules/@nordhavnhq/runner-runtime/dist/runtime/project-instructions.d.ts +22 -0
- package/node_modules/@nordhavnhq/runner-runtime/dist/runtime/project-instructions.js +47 -0
- package/node_modules/@nordhavnhq/runner-runtime/dist/runtime/run-tools.d.ts +26 -0
- package/node_modules/@nordhavnhq/runner-runtime/dist/runtime/run-tools.js +48 -0
- package/node_modules/@nordhavnhq/runner-runtime/dist/runtime/scratch-prompt.d.ts +8 -0
- package/node_modules/@nordhavnhq/runner-runtime/dist/runtime/scratch-prompt.js +19 -0
- package/node_modules/@nordhavnhq/runner-runtime/dist/runtime/side-question.d.ts +76 -0
- package/node_modules/@nordhavnhq/runner-runtime/dist/runtime/side-question.js +155 -0
- package/node_modules/@nordhavnhq/runner-runtime/dist/runtime/task-validation.d.ts +43 -0
- package/node_modules/@nordhavnhq/runner-runtime/dist/runtime/task-validation.js +37 -0
- package/node_modules/@nordhavnhq/runner-runtime/dist/runtime/unload-toolset.d.ts +49 -0
- package/node_modules/@nordhavnhq/runner-runtime/dist/runtime/unload-toolset.js +104 -0
- package/node_modules/@nordhavnhq/runner-runtime/dist/runtime/write-scope.d.ts +16 -0
- package/node_modules/@nordhavnhq/runner-runtime/dist/runtime/write-scope.js +6 -0
- package/node_modules/@nordhavnhq/runner-runtime/dist/tools/delegate.d.ts +86 -0
- package/node_modules/@nordhavnhq/runner-runtime/dist/tools/delegate.js +141 -0
- package/node_modules/@nordhavnhq/runner-runtime/dist/tools/index.d.ts +179 -0
- package/node_modules/@nordhavnhq/runner-runtime/dist/tools/index.js +35 -0
- package/node_modules/@nordhavnhq/runner-runtime/dist/tools/mcp.d.ts +21 -0
- package/node_modules/@nordhavnhq/runner-runtime/dist/tools/mcp.js +179 -0
- package/node_modules/@nordhavnhq/runner-runtime/package.json +18 -0
- package/package.json +33 -4
- package/README.md +0 -3
|
@@ -0,0 +1,967 @@
|
|
|
1
|
+
import { execSync, execFileSync, execFile } from 'node:child_process';
|
|
2
|
+
import { existsSync, readFileSync, writeFileSync, unlinkSync, mkdirSync } from 'node:fs';
|
|
3
|
+
import { tmpdir } from 'node:os';
|
|
4
|
+
import { join } from 'node:path';
|
|
5
|
+
import { promisify } from 'node:util';
|
|
6
|
+
const execFileAsync = promisify(execFile);
|
|
7
|
+
import ffmpegInstaller from '@ffmpeg-installer/ffmpeg';
|
|
8
|
+
import { uploadImage } from './_upload.js';
|
|
9
|
+
import { toNum, toObject } from './_coerce.js';
|
|
10
|
+
// ─── Screen Metadata ────────────────────────
|
|
11
|
+
const SCREEN_META_PATH = join(tmpdir(), 'nordhavn-screen-meta.json');
|
|
12
|
+
function writeScreenMeta(meta) {
|
|
13
|
+
try {
|
|
14
|
+
writeFileSync(SCREEN_META_PATH, JSON.stringify(meta));
|
|
15
|
+
}
|
|
16
|
+
catch { }
|
|
17
|
+
}
|
|
18
|
+
/** The most recent capture's mapping: this round's context first, then the
|
|
19
|
+
* file a previous round left behind. */
|
|
20
|
+
export function readScreenMeta(context) {
|
|
21
|
+
if (context?.lastScreenCapture)
|
|
22
|
+
return context.lastScreenCapture;
|
|
23
|
+
try {
|
|
24
|
+
return JSON.parse(readFileSync(SCREEN_META_PATH, 'utf-8'));
|
|
25
|
+
}
|
|
26
|
+
catch {
|
|
27
|
+
return null;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
// ─── Screenshot compression ─────────────────────────────────────────────────
|
|
31
|
+
//
|
|
32
|
+
// Screenshots get base64-encoded into the inference payload, so unbounded
|
|
33
|
+
// resolution / quality blows past the studio body limit fast. We cap to
|
|
34
|
+
// SCREENSHOT_MAX_DIM on the longest side and re-encode at SCREENSHOT_JPEG_QUALITY.
|
|
35
|
+
// The scaled-down imgWidth/imgHeight are written to the screen meta so
|
|
36
|
+
// `runner_mouse_control` can scale image_x/image_y back up to logical screen coords
|
|
37
|
+
// via xScale = logicalW / imgWidth (and similarly for y) - clicks land correctly.
|
|
38
|
+
const SCREENSHOT_MAX_DIM = 1280;
|
|
39
|
+
const SCREENSHOT_JPEG_QUALITY = 75;
|
|
40
|
+
function computeResizeTarget(logicalW, logicalH, maxDim = SCREENSHOT_MAX_DIM) {
|
|
41
|
+
if (logicalW <= 0 || logicalH <= 0)
|
|
42
|
+
return { w: logicalW, h: logicalH };
|
|
43
|
+
if (logicalW <= maxDim && logicalH <= maxDim)
|
|
44
|
+
return { w: logicalW, h: logicalH };
|
|
45
|
+
if (logicalW >= logicalH) {
|
|
46
|
+
return { w: maxDim, h: Math.max(1, Math.round((maxDim * logicalH) / logicalW)) };
|
|
47
|
+
}
|
|
48
|
+
return { w: Math.max(1, Math.round((maxDim * logicalW) / logicalH)), h: maxDim };
|
|
49
|
+
}
|
|
50
|
+
// JPEG quality for webpage captures. Slightly higher than screen captures
|
|
51
|
+
// (75) because webpages are text-heavy and JPEG can blur text edges; 82 keeps
|
|
52
|
+
// UI text crisp while still shrinking a 1440-wide PNG by ~5-10x.
|
|
53
|
+
const WEBPAGE_JPEG_QUALITY = 82;
|
|
54
|
+
// Re-encode a PNG file to a JPEG buffer at the given quality WITHOUT resizing,
|
|
55
|
+
// using ImageMagick. Returns null if no magick binary is available so callers
|
|
56
|
+
// can fall back to the original PNG. Absolute paths + homebrew fallback so this
|
|
57
|
+
// works even when the agent daemon doesn't have /opt/homebrew/bin in PATH.
|
|
58
|
+
function reencodePngToJpeg(pngPath, quality) {
|
|
59
|
+
const magickBin = [
|
|
60
|
+
'/opt/homebrew/bin/magick',
|
|
61
|
+
'/usr/local/bin/magick',
|
|
62
|
+
'magick',
|
|
63
|
+
'/opt/homebrew/bin/convert',
|
|
64
|
+
'/usr/local/bin/convert',
|
|
65
|
+
'convert',
|
|
66
|
+
].find(bin => {
|
|
67
|
+
try {
|
|
68
|
+
execSync(`"${bin}" --version`, { stdio: 'ignore' });
|
|
69
|
+
return true;
|
|
70
|
+
}
|
|
71
|
+
catch {
|
|
72
|
+
return false;
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
if (!magickBin)
|
|
76
|
+
return null;
|
|
77
|
+
const jpegPath = pngPath.replace(/\.png$/, '.jpg');
|
|
78
|
+
try {
|
|
79
|
+
execSync(`"${magickBin}" "${pngPath}" -quality ${quality} "${jpegPath}"`, {
|
|
80
|
+
stdio: 'pipe',
|
|
81
|
+
timeout: 15000,
|
|
82
|
+
});
|
|
83
|
+
if (!existsSync(jpegPath))
|
|
84
|
+
return null;
|
|
85
|
+
const data = readFileSync(jpegPath);
|
|
86
|
+
try {
|
|
87
|
+
unlinkSync(jpegPath);
|
|
88
|
+
}
|
|
89
|
+
catch { }
|
|
90
|
+
return data.length > 0 ? data : null;
|
|
91
|
+
}
|
|
92
|
+
catch {
|
|
93
|
+
try {
|
|
94
|
+
unlinkSync(jpegPath);
|
|
95
|
+
}
|
|
96
|
+
catch { }
|
|
97
|
+
return null;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
// ─── Webcam Tool ────────────────────────────
|
|
101
|
+
const captureWebcamDefinition = {
|
|
102
|
+
name: 'runner_capture_webcam',
|
|
103
|
+
description: 'Capture a photo using the webcam. The image is uploaded to chat history and a URL is returned.',
|
|
104
|
+
parameters: {
|
|
105
|
+
type: 'object',
|
|
106
|
+
properties: {
|
|
107
|
+
warmup: { type: 'number', description: 'Camera warmup seconds. Default: 1.' },
|
|
108
|
+
},
|
|
109
|
+
required: [],
|
|
110
|
+
},
|
|
111
|
+
};
|
|
112
|
+
function getFfmpegPath() {
|
|
113
|
+
if (!ffmpegInstaller?.path || !existsSync(ffmpegInstaller.path)) {
|
|
114
|
+
throw new Error(`Bundled ffmpeg not found for ${process.platform}/${process.arch}. Reinstall @nordhavnhq/runner.`);
|
|
115
|
+
}
|
|
116
|
+
return ffmpegInstaller.path;
|
|
117
|
+
}
|
|
118
|
+
let cachedWindowsVideoDevice = null;
|
|
119
|
+
function getWindowsVideoDevice(ffmpegPath) {
|
|
120
|
+
if (cachedWindowsVideoDevice)
|
|
121
|
+
return cachedWindowsVideoDevice;
|
|
122
|
+
// `ffmpeg -list_devices true -f dshow -i dummy` writes the list to stderr and exits non-zero.
|
|
123
|
+
let stderr = '';
|
|
124
|
+
try {
|
|
125
|
+
execFileSync(ffmpegPath, ['-hide_banner', '-list_devices', 'true', '-f', 'dshow', '-i', 'dummy'], {
|
|
126
|
+
stdio: ['ignore', 'ignore', 'pipe'],
|
|
127
|
+
timeout: 10000,
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
catch (err) {
|
|
131
|
+
stderr = err.stderr?.toString() ?? '';
|
|
132
|
+
}
|
|
133
|
+
const match = stderr.match(/"([^"]+)"\s*\(video\)/);
|
|
134
|
+
const device = match?.[1];
|
|
135
|
+
if (!device) {
|
|
136
|
+
throw new Error('No DirectShow video device found. Ensure a webcam is connected and accessible.');
|
|
137
|
+
}
|
|
138
|
+
cachedWindowsVideoDevice = device;
|
|
139
|
+
return device;
|
|
140
|
+
}
|
|
141
|
+
function captureFrame(outputPath, _warmup) {
|
|
142
|
+
const ffmpegPath = getFfmpegPath();
|
|
143
|
+
const platform = process.platform;
|
|
144
|
+
const args = [];
|
|
145
|
+
if (platform === 'darwin') {
|
|
146
|
+
args.push('-f', 'avfoundation', '-framerate', '30', '-i', '0:none');
|
|
147
|
+
}
|
|
148
|
+
else if (platform === 'win32') {
|
|
149
|
+
args.push('-f', 'dshow', '-i', `video=${getWindowsVideoDevice(ffmpegPath)}`);
|
|
150
|
+
}
|
|
151
|
+
else {
|
|
152
|
+
args.push('-f', 'v4l2', '-i', '/dev/video0');
|
|
153
|
+
}
|
|
154
|
+
args.push('-frames:v', '1', '-update', '1', '-y', outputPath);
|
|
155
|
+
execFileSync(ffmpegPath, args, {
|
|
156
|
+
stdio: 'pipe',
|
|
157
|
+
timeout: 15000,
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
async function executeCapture(params, context) {
|
|
161
|
+
const warmup = toNum(params.warmup) ?? 1;
|
|
162
|
+
if (!context?.studioUrl || !context?.agentToken) {
|
|
163
|
+
return 'Error: Upload configuration not available. Cannot capture webcam.';
|
|
164
|
+
}
|
|
165
|
+
const tmpDir = join(tmpdir(), 'nordhavn-webcam');
|
|
166
|
+
mkdirSync(tmpDir, { recursive: true });
|
|
167
|
+
const filename = `webcam-${Date.now()}.jpg`;
|
|
168
|
+
const outputPath = join(tmpDir, filename);
|
|
169
|
+
try {
|
|
170
|
+
captureFrame(outputPath, warmup);
|
|
171
|
+
}
|
|
172
|
+
catch (err) {
|
|
173
|
+
return `Error: Failed to capture webcam image: ${err.message}`;
|
|
174
|
+
}
|
|
175
|
+
if (!existsSync(outputPath)) {
|
|
176
|
+
return 'Error: Webcam capture failed - no image file was created.';
|
|
177
|
+
}
|
|
178
|
+
const imageData = readFileSync(outputPath);
|
|
179
|
+
try {
|
|
180
|
+
unlinkSync(outputPath);
|
|
181
|
+
}
|
|
182
|
+
catch { }
|
|
183
|
+
return uploadImage(imageData, filename, 'agent-webcam', context, params.folder);
|
|
184
|
+
}
|
|
185
|
+
// ─── Screen Capture Tool ────────────────────
|
|
186
|
+
const captureScreenDefinition = {
|
|
187
|
+
name: 'runner_capture_screen',
|
|
188
|
+
description: 'Take a full-resolution screenshot of an entire display.\n\n' +
|
|
189
|
+
'By default the image is inlined into your next tool result for analysis (no upload, no chat-history record) - analyze it directly. ' +
|
|
190
|
+
'Set keep:true ONLY when the user explicitly asks for the screenshot to be saved/shared/shown in the chat; then it is uploaded and the result also includes a URL.\n\n' +
|
|
191
|
+
'TIP: If you only need to see a specific app (e.g. Spotify, Chrome), use runner_capture_window instead - it captures just that window at full quality and gives you exact coordinates without cropping.\n' +
|
|
192
|
+
'TIP: To click a Dock icon, use runner_get_dock_items - it returns x/y coordinates directly, no screenshot needed.\n\n' +
|
|
193
|
+
'When clicking based on this image, pass image_x/image_y to runner_mouse_control - scale + multi-display origin are applied automatically.\n\n' +
|
|
194
|
+
'ZOOM: a full display is scaled down to 1280px, so small text (ids, error codes) can be unreadable. Pass region {x, y, width, height} in pixels of your MOST RECENT capture image to re-capture just that part at full sharpness. image_x/image_y on the zoomed image still click correctly.\n\n' +
|
|
195
|
+
'Always returns imageWidth/Height, displayLogicalWidth/Height, and displayOriginX/Y (non-zero on secondary displays). With keep:true also returns a URL.',
|
|
196
|
+
parameters: {
|
|
197
|
+
type: 'object',
|
|
198
|
+
properties: {
|
|
199
|
+
display: {
|
|
200
|
+
type: 'number',
|
|
201
|
+
description: 'Display number. 1 = primary/main display (default). 2, 3… = additional displays in CGGetActiveDisplayList order. Ignored when region is set.',
|
|
202
|
+
},
|
|
203
|
+
region: {
|
|
204
|
+
type: 'object',
|
|
205
|
+
description: 'Zoom: rectangle in pixel coordinates of the most recent runner_capture_screen/runner_capture_window image. macOS only.',
|
|
206
|
+
properties: {
|
|
207
|
+
x: { type: 'number' },
|
|
208
|
+
y: { type: 'number' },
|
|
209
|
+
width: { type: 'number' },
|
|
210
|
+
height: { type: 'number' },
|
|
211
|
+
},
|
|
212
|
+
required: ['x', 'y', 'width', 'height'],
|
|
213
|
+
},
|
|
214
|
+
keep: {
|
|
215
|
+
type: 'boolean',
|
|
216
|
+
description: 'Upload to chat history and return a URL. Default: false (inline, transient).',
|
|
217
|
+
},
|
|
218
|
+
},
|
|
219
|
+
required: [],
|
|
220
|
+
},
|
|
221
|
+
};
|
|
222
|
+
// Translate a region given in pixels of the most recent capture image into
|
|
223
|
+
// global logical coordinates, the space `screencapture -R` takes.
|
|
224
|
+
function regionToLogical(region, context) {
|
|
225
|
+
const r = toObject(region);
|
|
226
|
+
const nums = r && ['x', 'y', 'width', 'height'].map(k => toNum(r[k]));
|
|
227
|
+
if (!nums || !nums.every(n => n !== undefined)) {
|
|
228
|
+
return 'Error: region needs numeric x, y, width and height.';
|
|
229
|
+
}
|
|
230
|
+
const [x, y, w, h] = nums;
|
|
231
|
+
if (w <= 0 || h <= 0)
|
|
232
|
+
return 'Error: region width and height must be positive.';
|
|
233
|
+
const meta = readScreenMeta(context);
|
|
234
|
+
if (!meta)
|
|
235
|
+
return 'Error: region needs a previous capture to measure against. Call runner_capture_screen or runner_capture_window first.';
|
|
236
|
+
return {
|
|
237
|
+
x: Math.round(meta.originX + x * meta.xScale),
|
|
238
|
+
y: Math.round(meta.originY + y * meta.yScale),
|
|
239
|
+
width: Math.max(1, Math.round(w * meta.xScale)),
|
|
240
|
+
height: Math.max(1, Math.round(h * meta.yScale)),
|
|
241
|
+
};
|
|
242
|
+
}
|
|
243
|
+
function captureScreen(outputPath, display, region) {
|
|
244
|
+
const platform = process.platform;
|
|
245
|
+
if (platform === 'darwin') {
|
|
246
|
+
// -x = no sound, -C = capture cursor, -D = display number, -R = a rect in
|
|
247
|
+
// global logical coords (negative on a display left of the main one).
|
|
248
|
+
// screencapture exits 0 even on permission failure but produces no file or prints error.
|
|
249
|
+
const target = region
|
|
250
|
+
? `-R${region.x},${region.y},${region.width},${region.height}`
|
|
251
|
+
: `-D${display}`;
|
|
252
|
+
let result;
|
|
253
|
+
try {
|
|
254
|
+
result = execFileSync('screencapture', ['-x', '-C', target, outputPath], {
|
|
255
|
+
encoding: 'utf-8',
|
|
256
|
+
stdio: 'pipe',
|
|
257
|
+
timeout: 15000,
|
|
258
|
+
});
|
|
259
|
+
}
|
|
260
|
+
catch (err) {
|
|
261
|
+
result = `${err.stdout ?? ''}${err.stderr ?? ''}` || err.message;
|
|
262
|
+
// e.g. "rect (...) does not intersect any displays" for a region off-screen.
|
|
263
|
+
if (!result.includes('could not create image') && !existsSync(outputPath)) {
|
|
264
|
+
throw new Error(result.trim(), { cause: err });
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
if (result.includes('could not create image')) {
|
|
268
|
+
throw new Error('Screen recording permission not granted. Go to System Settings > Privacy & Security > Screen & System Audio Recording and allow Terminal (or the app running the agent).');
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
else if (platform === 'linux') {
|
|
272
|
+
try {
|
|
273
|
+
execSync('which gnome-screenshot', { stdio: 'ignore' });
|
|
274
|
+
execSync(`gnome-screenshot -f "${outputPath}"`, { stdio: 'pipe', timeout: 15000 });
|
|
275
|
+
}
|
|
276
|
+
catch {
|
|
277
|
+
execSync(`scrot "${outputPath}"`, { stdio: 'pipe', timeout: 15000 });
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
else {
|
|
281
|
+
throw new Error(`Screen capture not supported on ${platform}`);
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
async function executeScreenCapture(params, context) {
|
|
285
|
+
const display = toNum(params.display) ?? 1;
|
|
286
|
+
const keep = params.keep === true;
|
|
287
|
+
if (keep && (!context?.studioUrl || !context?.agentToken)) {
|
|
288
|
+
return 'Error: Upload configuration not available. Cannot keep screen capture.';
|
|
289
|
+
}
|
|
290
|
+
let region = null;
|
|
291
|
+
if (params.region !== undefined && params.region !== null) {
|
|
292
|
+
if (process.platform !== 'darwin')
|
|
293
|
+
return 'Error: region is only supported on macOS.';
|
|
294
|
+
const resolved = regionToLogical(params.region, context);
|
|
295
|
+
if (typeof resolved === 'string')
|
|
296
|
+
return resolved;
|
|
297
|
+
region = resolved;
|
|
298
|
+
}
|
|
299
|
+
const tmpDir = join(tmpdir(), 'nordhavn-screen');
|
|
300
|
+
mkdirSync(tmpDir, { recursive: true });
|
|
301
|
+
const filename = `screen-${Date.now()}.png`;
|
|
302
|
+
const outputPath = join(tmpDir, filename);
|
|
303
|
+
try {
|
|
304
|
+
captureScreen(outputPath, display, region);
|
|
305
|
+
}
|
|
306
|
+
catch (err) {
|
|
307
|
+
return `Error: Failed to capture screen: ${err.message}`;
|
|
308
|
+
}
|
|
309
|
+
if (!existsSync(outputPath)) {
|
|
310
|
+
return 'Error: Screen capture failed - no image file was created. Make sure screen recording permission is granted and the terminal was restarted after granting it.';
|
|
311
|
+
}
|
|
312
|
+
const imageData = readFileSync(outputPath);
|
|
313
|
+
if (imageData.length === 0) {
|
|
314
|
+
try {
|
|
315
|
+
unlinkSync(outputPath);
|
|
316
|
+
}
|
|
317
|
+
catch { }
|
|
318
|
+
return 'Error: Screen capture produced an empty file.';
|
|
319
|
+
}
|
|
320
|
+
// Detect logical display size via CGDisplayBounds (CG space: top-left origin,
|
|
321
|
+
// Y down, logical points - exactly what cliclick uses).
|
|
322
|
+
// CGGetActiveDisplayList index 0 = main display = screencapture -D 1.
|
|
323
|
+
// A region already IS a logical rect, so there is nothing to look up.
|
|
324
|
+
let originX = region?.x ?? 0;
|
|
325
|
+
let originY = region?.y ?? 0;
|
|
326
|
+
let logicalW = region?.width ?? 0;
|
|
327
|
+
let logicalH = region?.height ?? 0;
|
|
328
|
+
if (!region) {
|
|
329
|
+
try {
|
|
330
|
+
const pyScript = [
|
|
331
|
+
'import Quartz',
|
|
332
|
+
'_, displays, _ = Quartz.CGGetActiveDisplayList(10, None, None)',
|
|
333
|
+
`idx = ${display - 1}`,
|
|
334
|
+
'if idx < len(displays):',
|
|
335
|
+
' b = Quartz.CGDisplayBounds(displays[idx])',
|
|
336
|
+
' print(f"{int(b.origin.x)},{int(b.origin.y)},{int(b.size.width)},{int(b.size.height)}")',
|
|
337
|
+
].join('\n');
|
|
338
|
+
const out = execSync(`python3 -c '${pyScript}'`, {
|
|
339
|
+
encoding: 'utf-8',
|
|
340
|
+
stdio: 'pipe',
|
|
341
|
+
timeout: 5000,
|
|
342
|
+
}).trim();
|
|
343
|
+
const parts = out.split(',').map(Number);
|
|
344
|
+
if (parts.length === 4 && parts.every(n => !isNaN(n))) {
|
|
345
|
+
;
|
|
346
|
+
[originX, originY, logicalW, logicalH] = parts;
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
catch { }
|
|
350
|
+
}
|
|
351
|
+
// Resize screenshot from native physical pixels to a capped logical size
|
|
352
|
+
// (max SCREENSHOT_MAX_DIM on the long side) and re-encode at a lower JPEG
|
|
353
|
+
// quality so base64 bytes stay manageable. Clicks remain accurate because
|
|
354
|
+
// xScale / yScale below are computed against the *actual* output dimensions.
|
|
355
|
+
// Use absolute magick path + homebrew fallback so this works even when
|
|
356
|
+
// the agent daemon doesn't have /opt/homebrew/bin in PATH.
|
|
357
|
+
let finalData = imageData;
|
|
358
|
+
let resizeSucceeded = false;
|
|
359
|
+
const resizedPath = outputPath.replace('.png', '-resized.jpg');
|
|
360
|
+
// A zoomed region keeps its native (Retina) pixels up to the cap - that
|
|
361
|
+
// sharpness is the whole point of asking for one. A full display is sized
|
|
362
|
+
// from its logical dimensions as before.
|
|
363
|
+
let nativeW = 0;
|
|
364
|
+
let nativeH = 0;
|
|
365
|
+
try {
|
|
366
|
+
nativeW = imageData.readUInt32BE(16);
|
|
367
|
+
nativeH = imageData.readUInt32BE(20);
|
|
368
|
+
}
|
|
369
|
+
catch { }
|
|
370
|
+
const resizeTarget = region && nativeW > 0 && nativeH > 0
|
|
371
|
+
? computeResizeTarget(nativeW, nativeH)
|
|
372
|
+
: logicalW > 0 && logicalH > 0
|
|
373
|
+
? computeResizeTarget(logicalW, logicalH)
|
|
374
|
+
: { w: 0, h: 0 };
|
|
375
|
+
if (resizeTarget.w > 0 && resizeTarget.h > 0) {
|
|
376
|
+
const magickBin = [
|
|
377
|
+
'/opt/homebrew/bin/magick',
|
|
378
|
+
'/usr/local/bin/magick',
|
|
379
|
+
'magick',
|
|
380
|
+
'/opt/homebrew/bin/convert',
|
|
381
|
+
'/usr/local/bin/convert',
|
|
382
|
+
'convert',
|
|
383
|
+
].find(bin => {
|
|
384
|
+
try {
|
|
385
|
+
execSync(`"${bin}" --version`, { stdio: 'ignore' });
|
|
386
|
+
return true;
|
|
387
|
+
}
|
|
388
|
+
catch {
|
|
389
|
+
return false;
|
|
390
|
+
}
|
|
391
|
+
});
|
|
392
|
+
if (magickBin) {
|
|
393
|
+
try {
|
|
394
|
+
execSync(`"${magickBin}" "${outputPath}" -resize ${resizeTarget.w}x${resizeTarget.h}! -quality ${SCREENSHOT_JPEG_QUALITY} "${resizedPath}"`, {
|
|
395
|
+
stdio: 'pipe',
|
|
396
|
+
timeout: 15000,
|
|
397
|
+
});
|
|
398
|
+
if (existsSync(resizedPath)) {
|
|
399
|
+
finalData = readFileSync(resizedPath);
|
|
400
|
+
resizeSucceeded = true;
|
|
401
|
+
try {
|
|
402
|
+
unlinkSync(resizedPath);
|
|
403
|
+
}
|
|
404
|
+
catch { }
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
catch { }
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
try {
|
|
411
|
+
unlinkSync(outputPath);
|
|
412
|
+
}
|
|
413
|
+
catch { }
|
|
414
|
+
// Compute scale factors for image_x/image_y coordinate translation.
|
|
415
|
+
// After a successful resize the image is resizeTarget.w × resizeTarget.h -
|
|
416
|
+
// typically smaller than logicalW × logicalH, so xScale > 1 maps image
|
|
417
|
+
// pixels back to logical click coordinates. If resize failed we fall back
|
|
418
|
+
// to the native PNG dimensions.
|
|
419
|
+
let imgWidth = resizeSucceeded ? resizeTarget.w : 0;
|
|
420
|
+
let imgHeight = resizeSucceeded ? resizeTarget.h : 0;
|
|
421
|
+
if ((imgWidth === 0 || imgHeight === 0) && finalData.length > 0) {
|
|
422
|
+
// PNG: width at bytes 16-19, height at 20-23 (big-endian)
|
|
423
|
+
try {
|
|
424
|
+
imgWidth = finalData.readUInt32BE(16);
|
|
425
|
+
imgHeight = finalData.readUInt32BE(20);
|
|
426
|
+
}
|
|
427
|
+
catch { }
|
|
428
|
+
}
|
|
429
|
+
const xScale = imgWidth > 0 && logicalW > 0 ? logicalW / imgWidth : 1;
|
|
430
|
+
const yScale = imgHeight > 0 && logicalH > 0 ? logicalH / imgHeight : 1;
|
|
431
|
+
// Write screen meta so runner_mouse_control can auto-translate image_x/image_y coords.
|
|
432
|
+
writeScreenMeta({ xScale, yScale, originX, originY, imageWidth: imgWidth, imageHeight: imgHeight });
|
|
433
|
+
if (context)
|
|
434
|
+
context.lastScreenCapture = { xScale, yScale, originX, originY, imageWidth: imgWidth, imageHeight: imgHeight };
|
|
435
|
+
// The screen capture is always JPEG after the magick resize step. When
|
|
436
|
+
// resize is skipped we still have a PNG buffer in finalData but ext is
|
|
437
|
+
// updated on the keep:true path; for the inline path we report the
|
|
438
|
+
// actual media type so the LLM decodes it correctly.
|
|
439
|
+
const wasResized = finalData !== imageData;
|
|
440
|
+
const mediaType = wasResized ? 'image/jpeg' : 'image/png';
|
|
441
|
+
const baseMeta = {
|
|
442
|
+
displayOriginX: originX,
|
|
443
|
+
displayOriginY: originY,
|
|
444
|
+
displayLogicalWidth: logicalW,
|
|
445
|
+
displayLogicalHeight: logicalH,
|
|
446
|
+
imageWidth: imgWidth,
|
|
447
|
+
imageHeight: imgHeight,
|
|
448
|
+
note: imgWidth > 0
|
|
449
|
+
? `Image is ${imgWidth}x${imgHeight}px mapped to logical ${logicalW}x${logicalH}. Pass image_x/image_y to runner_mouse_control for auto-scaled clicks.`
|
|
450
|
+
: undefined,
|
|
451
|
+
};
|
|
452
|
+
if (region) {
|
|
453
|
+
baseMeta.region = region;
|
|
454
|
+
baseMeta.displayNote = `Zoomed capture of logical rect (${region.x}, ${region.y}, ${region.width}x${region.height}). image_x/image_y on THIS image are mapped back automatically; a further region is measured against this image too.`;
|
|
455
|
+
}
|
|
456
|
+
else if (originX !== 0 || originY !== 0) {
|
|
457
|
+
baseMeta.displayNote = `Display ${display} has global origin (${originX}, ${originY}) - already handled by image_x/image_y auto-scaling.`;
|
|
458
|
+
}
|
|
459
|
+
if (!keep) {
|
|
460
|
+
context?.pendingToolImages?.push({ data: finalData, mediaType });
|
|
461
|
+
return JSON.stringify({ success: true, inline: true, mediaType, size: finalData.length, ...baseMeta });
|
|
462
|
+
}
|
|
463
|
+
const uploadResult = await uploadImage(finalData, filename.replace('.png', '.jpg'), 'agent-screen', context, params.folder);
|
|
464
|
+
try {
|
|
465
|
+
const parsed = JSON.parse(uploadResult);
|
|
466
|
+
return JSON.stringify({ ...parsed, ...baseMeta });
|
|
467
|
+
}
|
|
468
|
+
catch {
|
|
469
|
+
return uploadResult;
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
// ─── Webpage Screenshot Tool ────────────────
|
|
473
|
+
const captureWebpageDefinition = {
|
|
474
|
+
name: 'runner_capture_webpage',
|
|
475
|
+
description: 'Screenshot a publicly reachable webpage by URL using headless browser.\n\n' +
|
|
476
|
+
'By default the image is inlined into your next tool result for analysis (no upload, no chat-history record) - analyze it directly. ' +
|
|
477
|
+
'Set keep:true ONLY when the user explicitly asks for the screenshot to be saved/shared/shown in the chat; then it is uploaded and a URL is returned.\n\n' +
|
|
478
|
+
'NOTE: cannot reliably reach agent-local dev servers (localhost / 127.0.0.1) - for local previews use `runner_capture_screen` against a visible browser window instead, or expose the dev server via a public tunnel first.',
|
|
479
|
+
parameters: {
|
|
480
|
+
type: 'object',
|
|
481
|
+
properties: {
|
|
482
|
+
url: { type: 'string', description: 'Webpage URL (http/https).' },
|
|
483
|
+
width: { type: 'number', description: 'Viewport width px. Default: 1440, clamped to 320-2560.' },
|
|
484
|
+
height: { type: 'number', description: 'Viewport height px. Default: 900, clamped to 320-2400. This is a viewport capture, not a full-page scroll: do not ask for a giant height to "see the whole page" - it just blurs. Scroll and re-capture instead.' },
|
|
485
|
+
delay: { type: 'number', description: 'Wait seconds after load. Default: 2.' },
|
|
486
|
+
keep: {
|
|
487
|
+
type: 'boolean',
|
|
488
|
+
description: 'Upload to chat history and return a URL. Default: false (inline, transient).',
|
|
489
|
+
},
|
|
490
|
+
name: {
|
|
491
|
+
type: 'string',
|
|
492
|
+
description: 'Optional descriptive file name for the saved capture, no extension, e.g. "stadsfeestzaal-homepage-mobile". Only used with keep:true. Slugified on save. Defaults to a name derived from the URL when omitted.',
|
|
493
|
+
},
|
|
494
|
+
},
|
|
495
|
+
required: ['url'],
|
|
496
|
+
},
|
|
497
|
+
};
|
|
498
|
+
function findChromeBinary() {
|
|
499
|
+
const candidates = process.platform === 'darwin'
|
|
500
|
+
? [
|
|
501
|
+
'/Applications/Google Chrome.app/Contents/MacOS/Google Chrome',
|
|
502
|
+
'/Applications/Chromium.app/Contents/MacOS/Chromium',
|
|
503
|
+
'/Applications/Brave Browser.app/Contents/MacOS/Brave Browser',
|
|
504
|
+
'/Applications/Microsoft Edge.app/Contents/MacOS/Microsoft Edge',
|
|
505
|
+
]
|
|
506
|
+
: [
|
|
507
|
+
'/usr/bin/google-chrome',
|
|
508
|
+
'/usr/bin/google-chrome-stable',
|
|
509
|
+
'/usr/bin/chromium',
|
|
510
|
+
'/usr/bin/chromium-browser',
|
|
511
|
+
];
|
|
512
|
+
for (const bin of candidates) {
|
|
513
|
+
if (existsSync(bin))
|
|
514
|
+
return bin;
|
|
515
|
+
}
|
|
516
|
+
try {
|
|
517
|
+
const result = execSync('which google-chrome || which chromium || which chromium-browser', {
|
|
518
|
+
encoding: 'utf-8',
|
|
519
|
+
stdio: ['pipe', 'pipe', 'pipe'],
|
|
520
|
+
}).trim();
|
|
521
|
+
if (result)
|
|
522
|
+
return result;
|
|
523
|
+
}
|
|
524
|
+
catch { }
|
|
525
|
+
return null;
|
|
526
|
+
}
|
|
527
|
+
// Slugify an arbitrary string into a safe file-name base. Drops any
|
|
528
|
+
// extension the agent tacked on, lowercases, collapses non-alphanumerics to
|
|
529
|
+
// single dashes, trims, and caps the length. Empty input -> 'webpage'.
|
|
530
|
+
function slugifyFilename(input) {
|
|
531
|
+
const noExt = input.replace(/\.(png|jpe?g|webp|gif)$/i, '');
|
|
532
|
+
const slug = noExt
|
|
533
|
+
.toLowerCase()
|
|
534
|
+
.replace(/[^a-z0-9]+/g, '-')
|
|
535
|
+
.replace(/^-+|-+$/g, '')
|
|
536
|
+
.slice(0, 80)
|
|
537
|
+
.replace(/-+$/, '');
|
|
538
|
+
return slug || 'webpage';
|
|
539
|
+
}
|
|
540
|
+
// Default capture file name when the agent doesn't pass one: hostname (sans
|
|
541
|
+
// `www.`) plus a slugged path, then a compact local timestamp for
|
|
542
|
+
// uniqueness. Beats the old `webpage-<epoch-ms>.png` which told a human
|
|
543
|
+
// browsing the file manager nothing about what they were looking at.
|
|
544
|
+
// e.g. `https://stadsfeestzaal-aarschot.be/agenda`
|
|
545
|
+
// -> `stadsfeestzaal-aarschot-agenda-20260608-1432`
|
|
546
|
+
function captureFilenameBase(url) {
|
|
547
|
+
let slug = 'webpage';
|
|
548
|
+
try {
|
|
549
|
+
const u = new URL(url);
|
|
550
|
+
const host = u.hostname.replace(/^www\./, '');
|
|
551
|
+
const path = u.pathname.replace(/^\/+|\/+$/g, '');
|
|
552
|
+
slug = slugifyFilename(path ? `${host}-${path}` : host);
|
|
553
|
+
}
|
|
554
|
+
catch { }
|
|
555
|
+
const d = new Date();
|
|
556
|
+
const p = (n) => String(n).padStart(2, '0');
|
|
557
|
+
const stamp = `${d.getFullYear()}${p(d.getMonth() + 1)}${p(d.getDate())}-${p(d.getHours())}${p(d.getMinutes())}`;
|
|
558
|
+
return `${slug}-${stamp}`;
|
|
559
|
+
}
|
|
560
|
+
async function executeCaptureWebpage(params, context) {
|
|
561
|
+
const url = params.url;
|
|
562
|
+
if (!url)
|
|
563
|
+
return 'Error: url is required.';
|
|
564
|
+
try {
|
|
565
|
+
new URL(url);
|
|
566
|
+
}
|
|
567
|
+
catch {
|
|
568
|
+
return `Error: Invalid URL "${url}"`;
|
|
569
|
+
}
|
|
570
|
+
const keep = params.keep === true;
|
|
571
|
+
if (keep && (!context?.studioUrl || !context?.agentToken)) {
|
|
572
|
+
return 'Error: Upload configuration not available. Cannot keep webpage capture.';
|
|
573
|
+
}
|
|
574
|
+
const chrome = findChromeBinary();
|
|
575
|
+
if (!chrome) {
|
|
576
|
+
return 'Error: No Chrome/Chromium browser found. Install Google Chrome or Chromium.';
|
|
577
|
+
}
|
|
578
|
+
// Clamp the viewport to sane bounds. An agent asking for a 5000-6000px-tall
|
|
579
|
+
// "full page" viewport produces a huge capture that the model's vision layer
|
|
580
|
+
// downsamples into an unreadable blur - which reads as "the site is broken"
|
|
581
|
+
// and drives pointless re-captures. A viewport-sized window stays sharp.
|
|
582
|
+
const clamp = (v, min, max) => Math.min(Math.max(Math.round(v), min), max);
|
|
583
|
+
const width = clamp(toNum(params.width) ?? 1440, 320, 2560);
|
|
584
|
+
const height = clamp(toNum(params.height) ?? 900, 320, 2400);
|
|
585
|
+
const delay = toNum(params.delay) ?? 2;
|
|
586
|
+
const tmpDir = join(tmpdir(), 'nordhavn-webpage');
|
|
587
|
+
mkdirSync(tmpDir, { recursive: true });
|
|
588
|
+
// The agent may pass a descriptive `name`; fall back to a URL-derived one.
|
|
589
|
+
const providedName = typeof params.name === 'string' ? params.name.trim() : '';
|
|
590
|
+
const filename = `${providedName ? slugifyFilename(providedName) : captureFilenameBase(url)}.png`;
|
|
591
|
+
const outputPath = join(tmpDir, filename);
|
|
592
|
+
try {
|
|
593
|
+
const args = [
|
|
594
|
+
'--headless=new',
|
|
595
|
+
`--screenshot=${outputPath}`,
|
|
596
|
+
`--window-size=${width},${height}`,
|
|
597
|
+
'--hide-scrollbars',
|
|
598
|
+
'--disable-gpu',
|
|
599
|
+
'--no-sandbox',
|
|
600
|
+
'--disable-dev-shm-usage',
|
|
601
|
+
'--disable-blink-features=AutomationControlled',
|
|
602
|
+
'--user-agent=Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36',
|
|
603
|
+
`--virtual-time-budget=${delay * 1000}`,
|
|
604
|
+
url,
|
|
605
|
+
];
|
|
606
|
+
// MUST be async: a synchronous spawn blocks the single Node event loop for
|
|
607
|
+
// the full duration, which stalls the agent's WebSocket heartbeat. On a
|
|
608
|
+
// slow or unreachable page that stall hits the 30s timeout, the studio
|
|
609
|
+
// sees a dead agent, recycles the socket and aborts the in-flight task.
|
|
610
|
+
// execFile (vs exec) avoids shell-escaping bugs with spaces in the path;
|
|
611
|
+
// maxBuffer is bumped because headless Chrome is noisy on stderr.
|
|
612
|
+
await execFileAsync(chrome, args, {
|
|
613
|
+
timeout: 30000,
|
|
614
|
+
maxBuffer: 16 * 1024 * 1024,
|
|
615
|
+
});
|
|
616
|
+
}
|
|
617
|
+
catch (err) {
|
|
618
|
+
// Chrome often exits with warnings but still produces the screenshot, so
|
|
619
|
+
// only treat it as a failure when no file landed.
|
|
620
|
+
if (!existsSync(outputPath)) {
|
|
621
|
+
if (err?.killed || err?.signal === 'SIGTERM' || err?.code === 'ETIMEDOUT') {
|
|
622
|
+
return `Error: Webpage capture timed out: "${url}" did not load within 30s. Check that the URL is correct and publicly reachable; a non-existent domain or unreachable host hangs until the timeout.`;
|
|
623
|
+
}
|
|
624
|
+
return `Error: Failed to capture webpage: ${err.message}`;
|
|
625
|
+
}
|
|
626
|
+
}
|
|
627
|
+
if (!existsSync(outputPath)) {
|
|
628
|
+
return 'Error: Webpage capture failed - no image file was created.';
|
|
629
|
+
}
|
|
630
|
+
const pngData = readFileSync(outputPath);
|
|
631
|
+
if (pngData.length === 0) {
|
|
632
|
+
try {
|
|
633
|
+
unlinkSync(outputPath);
|
|
634
|
+
}
|
|
635
|
+
catch { }
|
|
636
|
+
return 'Error: Webpage capture produced an empty file.';
|
|
637
|
+
}
|
|
638
|
+
// Compress the native PNG to JPEG (no resize) so base64 bytes stay small.
|
|
639
|
+
// Falls back to the raw PNG when ImageMagick isn't available.
|
|
640
|
+
const jpegData = reencodePngToJpeg(outputPath, WEBPAGE_JPEG_QUALITY);
|
|
641
|
+
try {
|
|
642
|
+
unlinkSync(outputPath);
|
|
643
|
+
}
|
|
644
|
+
catch { }
|
|
645
|
+
const finalData = jpegData ?? pngData;
|
|
646
|
+
const mediaType = jpegData ? 'image/jpeg' : 'image/png';
|
|
647
|
+
if (!keep) {
|
|
648
|
+
context?.pendingToolImages?.push({ data: finalData, mediaType });
|
|
649
|
+
return JSON.stringify({ success: true, inline: true, mediaType, size: finalData.length });
|
|
650
|
+
}
|
|
651
|
+
const uploadFilename = jpegData ? filename.replace(/\.png$/, '.jpg') : filename;
|
|
652
|
+
return uploadImage(finalData, uploadFilename, 'agent-webpage', context, params.folder);
|
|
653
|
+
}
|
|
654
|
+
// ─── List Windows Tool (macOS) ──────────────
|
|
655
|
+
const listWindowsDefinition = {
|
|
656
|
+
name: 'runner_list_windows',
|
|
657
|
+
description: 'List all visible application windows on screen with their app name, title, window ID, and bounds.\n\n' +
|
|
658
|
+
'Bounds (x, y, width, height) are in logical screen coordinates - the same space as runner_mouse_control/cliclick.\n\n' +
|
|
659
|
+
'CRITICAL - use x and y values EXACTLY as returned. Do NOT divide, multiply, add, or subtract ANY value (no scale factor, no display origin, no screen height). These coordinates are already correct for runner_mouse_control on any display configuration including multi-display setups.\n\n' +
|
|
660
|
+
'Use this before runner_capture_window to confirm the exact app name, or to get window positions for click calculations.\n\n' +
|
|
661
|
+
'macOS only. Requires pyobjc-framework-Quartz (`pip install pyobjc-framework-Quartz`).',
|
|
662
|
+
parameters: {
|
|
663
|
+
type: 'object',
|
|
664
|
+
properties: {
|
|
665
|
+
app: {
|
|
666
|
+
type: 'string',
|
|
667
|
+
description: 'Filter by app name or window title (case-insensitive substring). Omit to list all windows.',
|
|
668
|
+
},
|
|
669
|
+
},
|
|
670
|
+
required: [],
|
|
671
|
+
},
|
|
672
|
+
};
|
|
673
|
+
async function executeListWindows(params) {
|
|
674
|
+
if (process.platform !== 'darwin') {
|
|
675
|
+
return 'Error: runner_list_windows is only available on macOS.';
|
|
676
|
+
}
|
|
677
|
+
const appFilter = typeof params.app === 'string' ? params.app : '';
|
|
678
|
+
const pyScript = [
|
|
679
|
+
'import Quartz, json, sys',
|
|
680
|
+
'app_filter = sys.argv[1].lower() if len(sys.argv) > 1 else ""',
|
|
681
|
+
'windows = Quartz.CGWindowListCopyWindowInfo(',
|
|
682
|
+
' Quartz.kCGWindowListOptionOnScreenOnly | Quartz.kCGWindowListExcludeDesktopElements,',
|
|
683
|
+
' Quartz.kCGNullWindowID',
|
|
684
|
+
')',
|
|
685
|
+
'result = []',
|
|
686
|
+
'for w in windows:',
|
|
687
|
+
' if w.get("kCGWindowLayer", 0) > 0: continue',
|
|
688
|
+
' app = str(w.get("kCGWindowOwnerName", ""))',
|
|
689
|
+
' title = str(w.get("kCGWindowName", ""))',
|
|
690
|
+
' if app_filter and app_filter not in app.lower() and app_filter not in title.lower(): continue',
|
|
691
|
+
' wid = int(w.get("kCGWindowNumber", 0))',
|
|
692
|
+
' b = w.get("kCGWindowBounds", {})',
|
|
693
|
+
' result.append({"app": app, "title": title, "windowId": wid,',
|
|
694
|
+
' "x": int(b.get("X", 0)), "y": int(b.get("Y", 0)),',
|
|
695
|
+
' "width": int(b.get("Width", 0)), "height": int(b.get("Height", 0))})',
|
|
696
|
+
'print(json.dumps(result))',
|
|
697
|
+
].join('\n');
|
|
698
|
+
try {
|
|
699
|
+
const args = appFilter ? ['-c', pyScript, appFilter] : ['-c', pyScript];
|
|
700
|
+
const out = execFileSync('python3', args, {
|
|
701
|
+
encoding: 'utf-8',
|
|
702
|
+
stdio: 'pipe',
|
|
703
|
+
timeout: 5000,
|
|
704
|
+
}).trim();
|
|
705
|
+
const windows = JSON.parse(out);
|
|
706
|
+
if (windows.length === 0) {
|
|
707
|
+
return appFilter
|
|
708
|
+
? `No visible windows found matching "${appFilter}". Call runner_list_windows without a filter to see all windows.`
|
|
709
|
+
: 'No visible windows found.';
|
|
710
|
+
}
|
|
711
|
+
return JSON.stringify(windows, null, 2);
|
|
712
|
+
}
|
|
713
|
+
catch (err) {
|
|
714
|
+
return `Error: Failed to list windows: ${err.message}. Install pyobjc-framework-Quartz with: pip install pyobjc-framework-Quartz`;
|
|
715
|
+
}
|
|
716
|
+
}
|
|
717
|
+
// ─── Capture Window Tool (macOS) ────────────
|
|
718
|
+
const captureWindowDefinition = {
|
|
719
|
+
name: 'runner_capture_window',
|
|
720
|
+
description: 'Capture a screenshot of a specific application window by app name - without needing to crop from a full-screen image.\n\n' +
|
|
721
|
+
'By default the image is inlined into your next tool result for analysis (no upload, no chat-history record) - analyze it directly. ' +
|
|
722
|
+
'Set keep:true ONLY when the user explicitly asks for the screenshot to be saved/shared/shown in the chat; then it is uploaded and the result also includes a URL.\n\n' +
|
|
723
|
+
'Prefer this over runner_capture_screen + runner_edit_image crop when you want to inspect a particular app.\n\n' +
|
|
724
|
+
'When clicking based on this image, pass image_x/image_y to runner_mouse_control - the window origin is included automatically, so clicks land correctly even on secondary displays.\n\n' +
|
|
725
|
+
'Always returns imageWidth/Height, windowLogicalWidth/Height and windowX/Y (window top-left in absolute logical coords; can be negative on displays to the left). With keep:true also returns a URL.\n\n' +
|
|
726
|
+
'Pick the window with app, title, or both; or pass window_id from runner_list_windows to get exactly that one. Invisible windows and anything smaller than 100x60 (popups, tooltips, toolbars) are skipped unless you ask for them by window_id.\n\n' +
|
|
727
|
+
'macOS only. Requires pyobjc-framework-Quartz (`pip install pyobjc-framework-Quartz`).',
|
|
728
|
+
parameters: {
|
|
729
|
+
type: 'object',
|
|
730
|
+
properties: {
|
|
731
|
+
app: {
|
|
732
|
+
type: 'string',
|
|
733
|
+
description: 'App name (case-insensitive substring, e.g. "Spotify", "Chrome"). Use runner_list_windows to confirm the exact name.',
|
|
734
|
+
},
|
|
735
|
+
title: {
|
|
736
|
+
type: 'string',
|
|
737
|
+
description: 'Window title (case-insensitive substring), e.g. "Ads Manager". Combine with app to pick one window of a busy app.',
|
|
738
|
+
},
|
|
739
|
+
window_id: {
|
|
740
|
+
type: 'number',
|
|
741
|
+
description: 'Exact windowId from runner_list_windows. Overrides app/title/window_index.',
|
|
742
|
+
},
|
|
743
|
+
window_index: {
|
|
744
|
+
type: 'number',
|
|
745
|
+
description: 'Which matching window to capture when several match (0 = frontmost, default: 0).',
|
|
746
|
+
},
|
|
747
|
+
keep: {
|
|
748
|
+
type: 'boolean',
|
|
749
|
+
description: 'Upload to chat history and return a URL. Default: false (inline, transient).',
|
|
750
|
+
},
|
|
751
|
+
},
|
|
752
|
+
required: [],
|
|
753
|
+
},
|
|
754
|
+
};
|
|
755
|
+
// Below this a "window" is a popup, tooltip or strip of chrome (Chrome keeps
|
|
756
|
+
// 22px-high ones around), never the thing the agent meant to look at.
|
|
757
|
+
const MIN_CAPTURE_WINDOW_WIDTH = 100;
|
|
758
|
+
const MIN_CAPTURE_WINDOW_HEIGHT = 60;
|
|
759
|
+
async function executeCaptureWindow(params, context) {
|
|
760
|
+
if (process.platform !== 'darwin') {
|
|
761
|
+
return 'Error: runner_capture_window is only available on macOS.';
|
|
762
|
+
}
|
|
763
|
+
const keep = params.keep === true;
|
|
764
|
+
if (keep && (!context?.studioUrl || !context?.agentToken)) {
|
|
765
|
+
return 'Error: Upload configuration not available. Cannot keep window capture.';
|
|
766
|
+
}
|
|
767
|
+
const appName = typeof params.app === 'string' ? params.app.trim() : '';
|
|
768
|
+
const titleFilter = typeof params.title === 'string' ? params.title.trim() : '';
|
|
769
|
+
const wantedId = Math.floor(toNum(params.window_id) ?? 0);
|
|
770
|
+
if (!appName && !titleFilter && !wantedId)
|
|
771
|
+
return 'Error: pass "app", "title" or "window_id".';
|
|
772
|
+
const label = wantedId ? `window ${wantedId}` : [appName, titleFilter && `"${titleFilter}"`].filter(Boolean).join(' ');
|
|
773
|
+
const windowIndex = Math.max(0, Math.floor(toNum(params.window_index) ?? 0));
|
|
774
|
+
// 1. Look up window ID + bounds via CGWindowListCopyWindowInfo (front to back).
|
|
775
|
+
// `app` alone keeps its old meaning (app name OR title); with `title`
|
|
776
|
+
// beside it both must match.
|
|
777
|
+
const pyScript = [
|
|
778
|
+
'import Quartz, json, sys',
|
|
779
|
+
'app_filter, title_filter = sys.argv[1].lower(), sys.argv[2].lower()',
|
|
780
|
+
'wanted_id, idx = int(sys.argv[3]), int(sys.argv[4])',
|
|
781
|
+
'min_w, min_h = int(sys.argv[5]), int(sys.argv[6])',
|
|
782
|
+
'windows = Quartz.CGWindowListCopyWindowInfo(',
|
|
783
|
+
' Quartz.kCGWindowListOptionOnScreenOnly | Quartz.kCGWindowListExcludeDesktopElements,',
|
|
784
|
+
' Quartz.kCGNullWindowID',
|
|
785
|
+
')',
|
|
786
|
+
'matches = []',
|
|
787
|
+
'for w in windows:',
|
|
788
|
+
' wid = int(w.get("kCGWindowNumber", 0))',
|
|
789
|
+
' b = w.get("kCGWindowBounds", {})',
|
|
790
|
+
' info = {"windowId": wid, "x": int(b.get("X", 0)), "y": int(b.get("Y", 0)),',
|
|
791
|
+
' "width": int(b.get("Width", 0)), "height": int(b.get("Height", 0))}',
|
|
792
|
+
' if wanted_id:',
|
|
793
|
+
' if wid == wanted_id: matches.append(info)',
|
|
794
|
+
' continue',
|
|
795
|
+
' if w.get("kCGWindowLayer", 0) > 0: continue',
|
|
796
|
+
' if float(w.get("kCGWindowAlpha", 1)) <= 0: continue',
|
|
797
|
+
' if info["width"] < min_w or info["height"] < min_h: continue',
|
|
798
|
+
' app = str(w.get("kCGWindowOwnerName", "")).lower()',
|
|
799
|
+
' title = str(w.get("kCGWindowName", "")).lower()',
|
|
800
|
+
' if title_filter and title_filter not in title: continue',
|
|
801
|
+
' if app_filter and app_filter not in app and (title_filter or app_filter not in title): continue',
|
|
802
|
+
' matches.append(info)',
|
|
803
|
+
'i = 0 if wanted_id else idx',
|
|
804
|
+
'print(json.dumps(matches[i]) if i < len(matches) else "null")',
|
|
805
|
+
].join('\n');
|
|
806
|
+
let windowId;
|
|
807
|
+
let windowBounds;
|
|
808
|
+
try {
|
|
809
|
+
const out = execFileSync('python3', [
|
|
810
|
+
'-c', pyScript, appName, titleFilter, String(wantedId), String(windowIndex),
|
|
811
|
+
String(MIN_CAPTURE_WINDOW_WIDTH), String(MIN_CAPTURE_WINDOW_HEIGHT),
|
|
812
|
+
], {
|
|
813
|
+
encoding: 'utf-8',
|
|
814
|
+
stdio: 'pipe',
|
|
815
|
+
timeout: 5000,
|
|
816
|
+
}).trim();
|
|
817
|
+
if (!out || out === 'null') {
|
|
818
|
+
return `No visible window found for ${label} (index ${windowIndex}). Use runner_list_windows to see available windows.`;
|
|
819
|
+
}
|
|
820
|
+
const info = JSON.parse(out);
|
|
821
|
+
windowId = info.windowId;
|
|
822
|
+
windowBounds = { x: info.x, y: info.y, width: info.width, height: info.height };
|
|
823
|
+
}
|
|
824
|
+
catch (err) {
|
|
825
|
+
return `Error: Failed to find window for ${label}: ${err.message}. Install pyobjc-framework-Quartz with: pip install pyobjc-framework-Quartz`;
|
|
826
|
+
}
|
|
827
|
+
// 2. Capture the specific window.
|
|
828
|
+
// -l <id>: capture that window ID only
|
|
829
|
+
// -x: silent, -o: omit window shadow
|
|
830
|
+
const tmpDir = join(tmpdir(), 'nordhavn-window');
|
|
831
|
+
mkdirSync(tmpDir, { recursive: true });
|
|
832
|
+
const safeName = (appName || titleFilter || String(windowId)).replace(/[^a-zA-Z0-9]/g, '-').toLowerCase().slice(0, 32);
|
|
833
|
+
const filename = `window-${safeName}-${Date.now()}.png`;
|
|
834
|
+
const outputPath = join(tmpDir, filename);
|
|
835
|
+
try {
|
|
836
|
+
execFileSync('screencapture', ['-x', '-o', '-l', String(windowId), outputPath], {
|
|
837
|
+
stdio: 'pipe',
|
|
838
|
+
timeout: 15000,
|
|
839
|
+
});
|
|
840
|
+
}
|
|
841
|
+
catch (err) {
|
|
842
|
+
if (!existsSync(outputPath)) {
|
|
843
|
+
return `Error: Failed to capture window: ${err.stderr?.toString().trim() || err.message}`;
|
|
844
|
+
}
|
|
845
|
+
}
|
|
846
|
+
if (!existsSync(outputPath)) {
|
|
847
|
+
return 'Error: Window capture produced no file. Screen recording permission may not be granted.';
|
|
848
|
+
}
|
|
849
|
+
// Read native PNG dimensions as a fallback (used when resize is unavailable).
|
|
850
|
+
let physicalWidth = 0;
|
|
851
|
+
let physicalHeight = 0;
|
|
852
|
+
try {
|
|
853
|
+
const rawData = readFileSync(outputPath);
|
|
854
|
+
physicalWidth = rawData.readUInt32BE(16);
|
|
855
|
+
physicalHeight = rawData.readUInt32BE(20);
|
|
856
|
+
}
|
|
857
|
+
catch { }
|
|
858
|
+
// Resize to a capped logical size (max SCREENSHOT_MAX_DIM on the long side)
|
|
859
|
+
// and re-encode at a lower JPEG quality so base64 bytes stay manageable.
|
|
860
|
+
// xScale / yScale below are computed against the actual output dimensions
|
|
861
|
+
// so clicks remain accurate. Falls back to the native PNG if ImageMagick
|
|
862
|
+
// isn't installed.
|
|
863
|
+
const resizedPath = outputPath.replace('.png', '-resized.jpg');
|
|
864
|
+
let finalData;
|
|
865
|
+
let finalFilename = filename;
|
|
866
|
+
let imgWidth = physicalWidth || windowBounds.width;
|
|
867
|
+
let imgHeight = physicalHeight || windowBounds.height;
|
|
868
|
+
let resizeSucceeded = false;
|
|
869
|
+
const resizeTarget = computeResizeTarget(windowBounds.width, windowBounds.height);
|
|
870
|
+
if (resizeTarget.w > 0 && resizeTarget.h > 0) {
|
|
871
|
+
const magickBin = [
|
|
872
|
+
'/opt/homebrew/bin/magick',
|
|
873
|
+
'/usr/local/bin/magick',
|
|
874
|
+
'magick',
|
|
875
|
+
'/opt/homebrew/bin/convert',
|
|
876
|
+
'/usr/local/bin/convert',
|
|
877
|
+
'convert',
|
|
878
|
+
].find(bin => {
|
|
879
|
+
try {
|
|
880
|
+
execSync(`"${bin}" --version`, { stdio: 'ignore' });
|
|
881
|
+
return true;
|
|
882
|
+
}
|
|
883
|
+
catch {
|
|
884
|
+
return false;
|
|
885
|
+
}
|
|
886
|
+
});
|
|
887
|
+
if (magickBin) {
|
|
888
|
+
try {
|
|
889
|
+
execSync(`"${magickBin}" "${outputPath}" -resize ${resizeTarget.w}x${resizeTarget.h}! -quality ${SCREENSHOT_JPEG_QUALITY} "${resizedPath}"`, {
|
|
890
|
+
stdio: 'pipe',
|
|
891
|
+
timeout: 15000,
|
|
892
|
+
});
|
|
893
|
+
resizeSucceeded = existsSync(resizedPath);
|
|
894
|
+
}
|
|
895
|
+
catch { }
|
|
896
|
+
}
|
|
897
|
+
}
|
|
898
|
+
if (resizeSucceeded) {
|
|
899
|
+
finalData = readFileSync(resizedPath);
|
|
900
|
+
finalFilename = filename.replace('.png', '.jpg');
|
|
901
|
+
imgWidth = resizeTarget.w;
|
|
902
|
+
imgHeight = resizeTarget.h;
|
|
903
|
+
try {
|
|
904
|
+
unlinkSync(resizedPath);
|
|
905
|
+
}
|
|
906
|
+
catch { }
|
|
907
|
+
}
|
|
908
|
+
else {
|
|
909
|
+
finalData = readFileSync(outputPath);
|
|
910
|
+
}
|
|
911
|
+
try {
|
|
912
|
+
unlinkSync(outputPath);
|
|
913
|
+
}
|
|
914
|
+
catch { }
|
|
915
|
+
if (finalData.length === 0) {
|
|
916
|
+
return 'Error: Window capture produced an empty file.';
|
|
917
|
+
}
|
|
918
|
+
const xScale = imgWidth > 0 && windowBounds.width > 0 ? parseFloat((windowBounds.width / imgWidth).toFixed(4)) : 1;
|
|
919
|
+
const yScale = imgHeight > 0 && windowBounds.height > 0 ? parseFloat((windowBounds.height / imgHeight).toFixed(4)) : 1;
|
|
920
|
+
// Write screen meta so runner_mouse_control can auto-translate image_x/image_y
|
|
921
|
+
// - for window captures the origin is the window top-left (which can be
|
|
922
|
+
// negative on displays to the left of the main display).
|
|
923
|
+
const winMeta = {
|
|
924
|
+
xScale,
|
|
925
|
+
yScale,
|
|
926
|
+
originX: windowBounds.x,
|
|
927
|
+
originY: windowBounds.y,
|
|
928
|
+
imageWidth: imgWidth,
|
|
929
|
+
imageHeight: imgHeight,
|
|
930
|
+
windowX: windowBounds.x,
|
|
931
|
+
windowY: windowBounds.y,
|
|
932
|
+
};
|
|
933
|
+
writeScreenMeta(winMeta);
|
|
934
|
+
if (context)
|
|
935
|
+
context.lastScreenCapture = winMeta;
|
|
936
|
+
const mediaType = resizeSucceeded ? 'image/jpeg' : 'image/png';
|
|
937
|
+
const baseMeta = {
|
|
938
|
+
windowX: windowBounds.x,
|
|
939
|
+
windowY: windowBounds.y,
|
|
940
|
+
windowLogicalWidth: windowBounds.width,
|
|
941
|
+
windowLogicalHeight: windowBounds.height,
|
|
942
|
+
imageWidth: imgWidth,
|
|
943
|
+
imageHeight: imgHeight,
|
|
944
|
+
note: `Image is ${imgWidth}x${imgHeight}px. ` +
|
|
945
|
+
`Pass image_x/image_y to runner_mouse_control - the window origin (${windowBounds.x}, ${windowBounds.y}) is added automatically. ` +
|
|
946
|
+
`For runner_get_dock_items / runner_list_windows / osascript coordinates use x/y instead.`,
|
|
947
|
+
};
|
|
948
|
+
if (!keep) {
|
|
949
|
+
context?.pendingToolImages?.push({ data: finalData, mediaType });
|
|
950
|
+
return JSON.stringify({ success: true, inline: true, mediaType, size: finalData.length, ...baseMeta });
|
|
951
|
+
}
|
|
952
|
+
const uploadResult = await uploadImage(finalData, finalFilename, 'agent-window', context, params.folder);
|
|
953
|
+
try {
|
|
954
|
+
const parsed = JSON.parse(uploadResult);
|
|
955
|
+
return JSON.stringify({ ...parsed, ...baseMeta });
|
|
956
|
+
}
|
|
957
|
+
catch {
|
|
958
|
+
return uploadResult;
|
|
959
|
+
}
|
|
960
|
+
}
|
|
961
|
+
export const webcamTools = [
|
|
962
|
+
{ definition: captureWebcamDefinition, execute: executeCapture },
|
|
963
|
+
{ definition: captureScreenDefinition, execute: executeScreenCapture },
|
|
964
|
+
{ definition: captureWebpageDefinition, execute: executeCaptureWebpage },
|
|
965
|
+
{ definition: listWindowsDefinition, execute: executeListWindows },
|
|
966
|
+
{ definition: captureWindowDefinition, execute: executeCaptureWindow },
|
|
967
|
+
];
|