@nordhavnhq/runner 0.0.0-stage → 0.96.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 +39 -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 +20 -0
- package/dist/cli/link.js +312 -0
- package/dist/cli/logs.d.ts +2 -0
- package/dist/cli/logs.js +144 -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 +80 -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 +135 -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 +1324 -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,507 @@
|
|
|
1
|
+
import { execSync } from 'node:child_process';
|
|
2
|
+
import { existsSync, mkdirSync } from 'node:fs';
|
|
3
|
+
import { join } from 'node:path';
|
|
4
|
+
import { randomBytes } from 'node:crypto';
|
|
5
|
+
import { chromium } from 'playwright-core';
|
|
6
|
+
import { errorMessage } from './params.js';
|
|
7
|
+
import { wirePageEvents } from './page-events.js';
|
|
8
|
+
// ─── Browser sessions ───────────────────────────────────────────────────────
|
|
9
|
+
//
|
|
10
|
+
// One session per dispatch (keyed by the queue/conversation id), created
|
|
11
|
+
// lazily by the first browser tool and reused by every later one, so a login
|
|
12
|
+
// survives across calls. A session holds one browser context with one or more
|
|
13
|
+
// tabs, a ring buffer of console messages and failed requests, and the owner
|
|
14
|
+
// of the dispatch (for the live view).
|
|
15
|
+
//
|
|
16
|
+
// A session with a `profile` runs in a persistent context whose user data dir
|
|
17
|
+
// lives in the agent home (`browser-profiles/<name>`), so cookies and logins
|
|
18
|
+
// outlive the dispatch. Chrome locks that dir: one session per profile at once.
|
|
19
|
+
//
|
|
20
|
+
// Sessions close when the dispatch aborts, on runner_browser_close, or after
|
|
21
|
+
// an idle TTL. Nothing leaks between dispatches because the key is per dispatch.
|
|
22
|
+
export const NAV_TIMEOUT_MS = 30_000;
|
|
23
|
+
const ACTION_TIMEOUT_MS = 10_000;
|
|
24
|
+
const IDLE_TTL_MS = 10 * 60_000;
|
|
25
|
+
/** A session the agent showed a person stays open this long after the show, watched or not. */
|
|
26
|
+
const SHOWN_TTL_MS = 30 * 60_000;
|
|
27
|
+
const LOG_LIMIT = 300;
|
|
28
|
+
export const VIEWPORTS = {
|
|
29
|
+
desktop: { width: 1440, height: 900 },
|
|
30
|
+
laptop: { width: 1280, height: 800 },
|
|
31
|
+
tablet: { width: 820, height: 1180 },
|
|
32
|
+
mobile: { width: 390, height: 844 },
|
|
33
|
+
};
|
|
34
|
+
const DEFAULT_UA = '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';
|
|
35
|
+
const DEVICES = {
|
|
36
|
+
mobile: {
|
|
37
|
+
userAgent: 'Mozilla/5.0 (iPhone; CPU iPhone OS 17_5 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.5 Mobile/15E148 Safari/604.1',
|
|
38
|
+
deviceScaleFactor: 3,
|
|
39
|
+
},
|
|
40
|
+
tablet: {
|
|
41
|
+
userAgent: 'Mozilla/5.0 (iPad; CPU OS 17_5 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.5 Mobile/15E148 Safari/604.1',
|
|
42
|
+
deviceScaleFactor: 2,
|
|
43
|
+
},
|
|
44
|
+
};
|
|
45
|
+
const sessions = new Map();
|
|
46
|
+
const listeners = new Set();
|
|
47
|
+
/** Follow session lifecycle (the live-view stream hooks in here). */
|
|
48
|
+
export function onSessionEvent(fn) {
|
|
49
|
+
listeners.add(fn);
|
|
50
|
+
return () => { listeners.delete(fn); };
|
|
51
|
+
}
|
|
52
|
+
export function emit(event) {
|
|
53
|
+
for (const fn of listeners) {
|
|
54
|
+
try {
|
|
55
|
+
fn(event);
|
|
56
|
+
}
|
|
57
|
+
catch { }
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
export function listSessions() {
|
|
61
|
+
return [...sessions.values()];
|
|
62
|
+
}
|
|
63
|
+
export function sessionById(id) {
|
|
64
|
+
for (const s of sessions.values())
|
|
65
|
+
if (s.id === id)
|
|
66
|
+
return s;
|
|
67
|
+
return undefined;
|
|
68
|
+
}
|
|
69
|
+
/** Stable per-dispatch key: every browser tool of one dispatch shares a
|
|
70
|
+
* session, other dispatches (and agents) never see it. A chat with a person
|
|
71
|
+
* keys on its conversation: every turn is a dispatch (queue row) of its own,
|
|
72
|
+
* and the browser has to stay open between turns. Without a dispatch the
|
|
73
|
+
* agent is the key, with its organisation: the browser host serves agents of
|
|
74
|
+
* several, and a slug is unique only within one. */
|
|
75
|
+
export function sessionKey(context) {
|
|
76
|
+
const agent = context?.agentSlug && (context.organisationSlug ? `${context.organisationSlug}/${context.agentSlug}` : context.agentSlug);
|
|
77
|
+
if (context?.userPresent === true && context.conversationId)
|
|
78
|
+
return `chat:${context.conversationId}`;
|
|
79
|
+
return context?.queueUid || context?.conversationId || agent || 'default';
|
|
80
|
+
}
|
|
81
|
+
export function findChromeBinary() {
|
|
82
|
+
const candidates = process.platform === 'darwin'
|
|
83
|
+
? [
|
|
84
|
+
'/Applications/Google Chrome.app/Contents/MacOS/Google Chrome',
|
|
85
|
+
'/Applications/Chromium.app/Contents/MacOS/Chromium',
|
|
86
|
+
'/Applications/Brave Browser.app/Contents/MacOS/Brave Browser',
|
|
87
|
+
'/Applications/Microsoft Edge.app/Contents/MacOS/Microsoft Edge',
|
|
88
|
+
]
|
|
89
|
+
: [
|
|
90
|
+
'/usr/bin/google-chrome',
|
|
91
|
+
'/usr/bin/google-chrome-stable',
|
|
92
|
+
'/usr/bin/chromium',
|
|
93
|
+
'/usr/bin/chromium-browser',
|
|
94
|
+
];
|
|
95
|
+
for (const bin of candidates) {
|
|
96
|
+
if (existsSync(bin))
|
|
97
|
+
return bin;
|
|
98
|
+
}
|
|
99
|
+
try {
|
|
100
|
+
const result = execSync('which google-chrome || which chromium || which chromium-browser', {
|
|
101
|
+
encoding: 'utf-8',
|
|
102
|
+
stdio: ['pipe', 'pipe', 'pipe'],
|
|
103
|
+
}).trim();
|
|
104
|
+
if (result)
|
|
105
|
+
return result;
|
|
106
|
+
}
|
|
107
|
+
catch { }
|
|
108
|
+
return null;
|
|
109
|
+
}
|
|
110
|
+
/** Profile names are folder names in the agent home: keep them boring. */
|
|
111
|
+
export function normaliseProfile(name) {
|
|
112
|
+
if (name === undefined || name === null || name === '')
|
|
113
|
+
return null;
|
|
114
|
+
if (typeof name !== 'string')
|
|
115
|
+
return { error: 'profile must be a string' };
|
|
116
|
+
const clean = name.trim().toLowerCase();
|
|
117
|
+
if (!/^[a-z0-9][a-z0-9_-]{0,47}$/.test(clean)) {
|
|
118
|
+
return { error: `Invalid profile "${name}": use 1-48 chars of a-z, 0-9, - and _.` };
|
|
119
|
+
}
|
|
120
|
+
return clean;
|
|
121
|
+
}
|
|
122
|
+
export function profilesRoot(context) {
|
|
123
|
+
return context?.agentHome ? join(context.agentHome, 'browser-profiles') : null;
|
|
124
|
+
}
|
|
125
|
+
export function resolveViewport(input) {
|
|
126
|
+
if (input === undefined || input === null || input === '')
|
|
127
|
+
return { ...VIEWPORTS.desktop };
|
|
128
|
+
if (typeof input === 'string') {
|
|
129
|
+
if (input in VIEWPORTS)
|
|
130
|
+
return { ...VIEWPORTS[input] };
|
|
131
|
+
const m = /^(\d{3,4})x(\d{3,4})$/.exec(input.trim());
|
|
132
|
+
if (m)
|
|
133
|
+
return { width: Number(m[1]), height: Number(m[2]) };
|
|
134
|
+
}
|
|
135
|
+
return { error: `Invalid viewport "${String(input)}": use ${Object.keys(VIEWPORTS).join(', ')} or WIDTHxHEIGHT.` };
|
|
136
|
+
}
|
|
137
|
+
/** The device a viewport input emulates: the mobile and tablet presets, else desktop. */
|
|
138
|
+
export function resolveDevice(input) {
|
|
139
|
+
return input === 'mobile' || input === 'tablet' ? input : 'desktop';
|
|
140
|
+
}
|
|
141
|
+
/** The context options of a device class. A desktop profile keeps the real
|
|
142
|
+
* user agent: sites that pin a session to it (Google, Meta) would log it out. */
|
|
143
|
+
export function deviceContextOptions(device, profile) {
|
|
144
|
+
if (device === 'desktop')
|
|
145
|
+
return profile ? {} : { userAgent: DEFAULT_UA };
|
|
146
|
+
const { userAgent, deviceScaleFactor } = DEVICES[device];
|
|
147
|
+
return { userAgent, isMobile: true, hasTouch: true, deviceScaleFactor };
|
|
148
|
+
}
|
|
149
|
+
/** The dispatch's open session, or null. Touches lastUsed. */
|
|
150
|
+
export function currentSession(context) {
|
|
151
|
+
const s = sessions.get(sessionKey(context));
|
|
152
|
+
if (!s)
|
|
153
|
+
return null;
|
|
154
|
+
s.lastUsed = Date.now();
|
|
155
|
+
return s;
|
|
156
|
+
}
|
|
157
|
+
export function activePage(session) {
|
|
158
|
+
return session.pages[session.active] ?? session.pages[0];
|
|
159
|
+
}
|
|
160
|
+
export const NO_SESSION = 'Error: No open browser page. Call runner_browser_navigate first.';
|
|
161
|
+
function pushLog(list, entry) {
|
|
162
|
+
list.push(entry);
|
|
163
|
+
if (list.length > LOG_LIMIT)
|
|
164
|
+
list.splice(0, list.length - LOG_LIMIT);
|
|
165
|
+
}
|
|
166
|
+
/** Give a tab the session's viewport: a new tab opens at the context's
|
|
167
|
+
* launch size, which a resize of the open session does not change. */
|
|
168
|
+
export async function fitPage(session, page) {
|
|
169
|
+
const size = page.viewportSize();
|
|
170
|
+
if (size && size.width === session.viewport.width && size.height === session.viewport.height)
|
|
171
|
+
return;
|
|
172
|
+
await page.setViewportSize(session.viewport).catch(() => { });
|
|
173
|
+
}
|
|
174
|
+
/** Wire a tab into the session: logs, and drop it from the list when it closes. */
|
|
175
|
+
export function trackPage(session, page) {
|
|
176
|
+
if (session.pages.includes(page))
|
|
177
|
+
return;
|
|
178
|
+
session.pages.push(page);
|
|
179
|
+
void fitPage(session, page);
|
|
180
|
+
const tab = () => session.pages.indexOf(page);
|
|
181
|
+
wirePageEvents(session, page, tab);
|
|
182
|
+
page.on('console', (msg) => {
|
|
183
|
+
const loc = msg.location();
|
|
184
|
+
// Chrome asks every site for /favicon.ico: a missing one is noise, not an error of the page.
|
|
185
|
+
if (msg.type() === 'error' && /\/favicon\.ico(\?|$)/.test(loc?.url ?? ''))
|
|
186
|
+
return;
|
|
187
|
+
pushLog(session.console, {
|
|
188
|
+
seq: ++session.seq,
|
|
189
|
+
at: Date.now(),
|
|
190
|
+
tab: tab(),
|
|
191
|
+
level: msg.type(),
|
|
192
|
+
text: msg.text().slice(0, 2_000),
|
|
193
|
+
location: loc?.url ? `${loc.url}:${loc.lineNumber}` : undefined,
|
|
194
|
+
});
|
|
195
|
+
});
|
|
196
|
+
page.on('pageerror', (err) => {
|
|
197
|
+
pushLog(session.console, { seq: ++session.seq, at: Date.now(), tab: tab(), level: 'pageerror', text: String(err?.stack || err).slice(0, 2_000) });
|
|
198
|
+
});
|
|
199
|
+
page.on('requestfailed', (req) => {
|
|
200
|
+
pushLog(session.network, {
|
|
201
|
+
seq: ++session.seq,
|
|
202
|
+
at: Date.now(),
|
|
203
|
+
tab: tab(),
|
|
204
|
+
method: req.method(),
|
|
205
|
+
url: req.url().slice(0, 500),
|
|
206
|
+
status: null,
|
|
207
|
+
failure: req.failure()?.errorText ?? 'failed',
|
|
208
|
+
resourceType: req.resourceType(),
|
|
209
|
+
});
|
|
210
|
+
});
|
|
211
|
+
page.on('response', (resp) => {
|
|
212
|
+
if (resp.status() < 400)
|
|
213
|
+
return;
|
|
214
|
+
const req = resp.request();
|
|
215
|
+
pushLog(session.network, {
|
|
216
|
+
seq: ++session.seq,
|
|
217
|
+
at: Date.now(),
|
|
218
|
+
tab: tab(),
|
|
219
|
+
method: req.method(),
|
|
220
|
+
url: resp.url().slice(0, 500),
|
|
221
|
+
status: resp.status(),
|
|
222
|
+
resourceType: req.resourceType(),
|
|
223
|
+
});
|
|
224
|
+
});
|
|
225
|
+
page.on('framenavigated', (frame) => {
|
|
226
|
+
if (frame === page.mainFrame())
|
|
227
|
+
emit({ type: 'changed', session });
|
|
228
|
+
});
|
|
229
|
+
page.on('close', () => {
|
|
230
|
+
const i = session.pages.indexOf(page);
|
|
231
|
+
if (i === -1)
|
|
232
|
+
return;
|
|
233
|
+
session.pages.splice(i, 1);
|
|
234
|
+
// A tab left of the active one shifts it; the active one closing picks its left neighbour.
|
|
235
|
+
if (i < session.active || session.active >= session.pages.length)
|
|
236
|
+
session.active = Math.max(0, session.active - 1);
|
|
237
|
+
emit({ type: 'changed', session });
|
|
238
|
+
});
|
|
239
|
+
}
|
|
240
|
+
export async function closeSession(key) {
|
|
241
|
+
const session = sessions.get(key);
|
|
242
|
+
if (!session)
|
|
243
|
+
return;
|
|
244
|
+
sessions.delete(key);
|
|
245
|
+
if (session.onAbort && session.signal) {
|
|
246
|
+
try {
|
|
247
|
+
session.signal.removeEventListener('abort', session.onAbort);
|
|
248
|
+
}
|
|
249
|
+
catch { }
|
|
250
|
+
}
|
|
251
|
+
try {
|
|
252
|
+
await session.context.close();
|
|
253
|
+
}
|
|
254
|
+
catch { }
|
|
255
|
+
try {
|
|
256
|
+
await session.browser?.close();
|
|
257
|
+
}
|
|
258
|
+
catch { }
|
|
259
|
+
emit({ type: 'closed', session });
|
|
260
|
+
}
|
|
261
|
+
export async function closeAllSessions() {
|
|
262
|
+
await Promise.all([...sessions.keys()].map(closeSession));
|
|
263
|
+
}
|
|
264
|
+
/** Close the sessions nobody used for a while (also on every open; the browser host runs it on a timer). */
|
|
265
|
+
export function reapIdle() {
|
|
266
|
+
const now = Date.now();
|
|
267
|
+
for (const [key, session] of sessions) {
|
|
268
|
+
// A person holding the wheel keeps the session alive.
|
|
269
|
+
if (session.handoff)
|
|
270
|
+
continue;
|
|
271
|
+
if (session.shown && now - session.shown.at < SHOWN_TTL_MS)
|
|
272
|
+
continue;
|
|
273
|
+
if (now - session.lastUsed > IDLE_TTL_MS)
|
|
274
|
+
void closeSession(key);
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
function profileInUse(profile, exceptKey) {
|
|
278
|
+
for (const [key, s] of sessions)
|
|
279
|
+
if (key !== exceptKey && s.profile === profile)
|
|
280
|
+
return true;
|
|
281
|
+
return false;
|
|
282
|
+
}
|
|
283
|
+
/**
|
|
284
|
+
* The dispatch's session, launched on first use. When the caller asks for a
|
|
285
|
+
* different profile, headed mode, device or viewport than the open session has,
|
|
286
|
+
* the session is relaunched (profile/headed/device) or resized (viewport) and
|
|
287
|
+
* `relaunched` says so. Uses the system Chrome (playwright-core ships no browser).
|
|
288
|
+
*/
|
|
289
|
+
export async function ensureSession(context, opts = {}, person) {
|
|
290
|
+
reapIdle();
|
|
291
|
+
const key = person ? `person:${randomBytes(4).toString('hex')}` : sessionKey(context);
|
|
292
|
+
const existing = sessions.get(key);
|
|
293
|
+
let relaunched;
|
|
294
|
+
if (existing) {
|
|
295
|
+
const plan = await reuseOrPlanRelaunch(existing, opts);
|
|
296
|
+
if ('session' in plan)
|
|
297
|
+
return plan;
|
|
298
|
+
relaunched = plan.relaunched;
|
|
299
|
+
await closeSession(key);
|
|
300
|
+
opts = plan.opts;
|
|
301
|
+
}
|
|
302
|
+
const launched = await launchBrowser(context, key, opts);
|
|
303
|
+
if ('error' in launched)
|
|
304
|
+
return launched;
|
|
305
|
+
const session = newSession(context, key, launched);
|
|
306
|
+
if (person) {
|
|
307
|
+
session.owner.openedByUserId = person.userId;
|
|
308
|
+
// The person drives a window they opened themselves.
|
|
309
|
+
session.handoff = { reason: `Opened by ${person.name}`, at: Date.now() };
|
|
310
|
+
}
|
|
311
|
+
await wireSession(session, context);
|
|
312
|
+
sessions.set(key, session);
|
|
313
|
+
emit({ type: 'opened', session });
|
|
314
|
+
return relaunched ? { session, relaunched } : { session };
|
|
315
|
+
}
|
|
316
|
+
/** Keep the open session when profile, headed and device match (resizing it
|
|
317
|
+
* when the viewport differs), or say how to relaunch it: user agent, touch and
|
|
318
|
+
* isMobile are context options a live context cannot change. Exported for tests. */
|
|
319
|
+
export async function reuseOrPlanRelaunch(existing, opts) {
|
|
320
|
+
existing.lastUsed = Date.now();
|
|
321
|
+
const wantProfile = opts.profile === undefined ? existing.profile : opts.profile;
|
|
322
|
+
const wantHeaded = opts.headed === undefined ? existing.headed : opts.headed;
|
|
323
|
+
const wantDevice = opts.device ?? existing.device;
|
|
324
|
+
if (wantProfile === existing.profile && wantHeaded === existing.headed && wantDevice === existing.device) {
|
|
325
|
+
await resizeSession(existing, opts.viewport);
|
|
326
|
+
return { session: existing };
|
|
327
|
+
}
|
|
328
|
+
const changes = [];
|
|
329
|
+
if (wantProfile !== existing.profile)
|
|
330
|
+
changes.push(`profile ${existing.profile ?? 'none'} -> ${wantProfile ?? 'none'}`);
|
|
331
|
+
if (wantHeaded !== existing.headed)
|
|
332
|
+
changes.push(`headed ${existing.headed} -> ${wantHeaded}`);
|
|
333
|
+
if (wantDevice !== existing.device)
|
|
334
|
+
changes.push(`device ${existing.device} -> ${wantDevice}`);
|
|
335
|
+
return {
|
|
336
|
+
relaunched: `Relaunched the browser (${changes.join(', ')}); open tabs were closed.`,
|
|
337
|
+
opts: { ...opts, profile: wantProfile, headed: wantHeaded, device: wantDevice, viewport: opts.viewport ?? existing.viewport },
|
|
338
|
+
};
|
|
339
|
+
}
|
|
340
|
+
async function resizeSession(session, viewport) {
|
|
341
|
+
if (!viewport)
|
|
342
|
+
return;
|
|
343
|
+
if (viewport.width === session.viewport.width && viewport.height === session.viewport.height)
|
|
344
|
+
return;
|
|
345
|
+
session.viewport = viewport;
|
|
346
|
+
for (const p of session.pages)
|
|
347
|
+
await p.setViewportSize(viewport).catch(() => { });
|
|
348
|
+
emit({ type: 'changed', session });
|
|
349
|
+
}
|
|
350
|
+
/** The user data dir of a profile, created; an error when it cannot be used. */
|
|
351
|
+
function profileDir(context, key, profile) {
|
|
352
|
+
const root = profilesRoot(context);
|
|
353
|
+
if (!root)
|
|
354
|
+
return { error: 'Error: Browser profiles need an agent home on this runner.' };
|
|
355
|
+
if (profileInUse(profile, key)) {
|
|
356
|
+
return { error: `Error: Profile "${profile}" is open in another session. Close that one first, or use another profile.` };
|
|
357
|
+
}
|
|
358
|
+
const dir = join(root, profile);
|
|
359
|
+
mkdirSync(dir, { recursive: true });
|
|
360
|
+
return dir;
|
|
361
|
+
}
|
|
362
|
+
function launchError(err, profile) {
|
|
363
|
+
const msg = errorMessage(err);
|
|
364
|
+
if (/ProcessSingleton|SingletonLock|profile.*in use/i.test(msg)) {
|
|
365
|
+
return { error: `Error: Profile "${profile}" is locked by another Chrome. Close it and try again.` };
|
|
366
|
+
}
|
|
367
|
+
return { error: `Error: Failed to launch browser: ${msg}` };
|
|
368
|
+
}
|
|
369
|
+
/** Launch the system Chrome: a persistent context for a profile, else a fresh
|
|
370
|
+
* browser with one clean context. */
|
|
371
|
+
async function launchBrowser(context, key, opts) {
|
|
372
|
+
const executablePath = findChromeBinary();
|
|
373
|
+
if (!executablePath) {
|
|
374
|
+
return { error: 'Error: No Chrome/Chromium browser found. Install Google Chrome or Chromium on this machine.' };
|
|
375
|
+
}
|
|
376
|
+
const profile = opts.profile ?? null;
|
|
377
|
+
const headed = opts.headed === true;
|
|
378
|
+
const viewport = opts.viewport ?? { ...VIEWPORTS.desktop };
|
|
379
|
+
const device = opts.device ?? 'desktop';
|
|
380
|
+
const emulation = deviceContextOptions(device, profile);
|
|
381
|
+
const args = ['--disable-dev-shm-usage', '--disable-blink-features=AutomationControlled'];
|
|
382
|
+
// A headed browser on a desktop must not run without its sandbox. The browser
|
|
383
|
+
// server image (a container, the container is the sandbox) turns it off for both.
|
|
384
|
+
if (!headed || (process.env.NORDHAVN_BROWSER_NO_SANDBOX || process.env.JUSTMADE_BROWSER_NO_SANDBOX) === '1')
|
|
385
|
+
args.push('--no-sandbox');
|
|
386
|
+
let browser = null;
|
|
387
|
+
let browserContext;
|
|
388
|
+
try {
|
|
389
|
+
if (profile) {
|
|
390
|
+
const dir = profileDir(context, key, profile);
|
|
391
|
+
if (typeof dir !== 'string')
|
|
392
|
+
return dir;
|
|
393
|
+
browserContext = await chromium.launchPersistentContext(dir, {
|
|
394
|
+
executablePath,
|
|
395
|
+
headless: !headed,
|
|
396
|
+
args,
|
|
397
|
+
viewport,
|
|
398
|
+
...emulation,
|
|
399
|
+
});
|
|
400
|
+
}
|
|
401
|
+
else {
|
|
402
|
+
browser = await chromium.launch({ executablePath, headless: !headed, args });
|
|
403
|
+
browserContext = await browser.newContext({ viewport, ...emulation });
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
catch (err) {
|
|
407
|
+
try {
|
|
408
|
+
await browser?.close();
|
|
409
|
+
}
|
|
410
|
+
catch { }
|
|
411
|
+
return launchError(err, profile);
|
|
412
|
+
}
|
|
413
|
+
browserContext.setDefaultTimeout(ACTION_TIMEOUT_MS);
|
|
414
|
+
browserContext.setDefaultNavigationTimeout(NAV_TIMEOUT_MS);
|
|
415
|
+
return { browser, browserContext, profile, headed, viewport, device };
|
|
416
|
+
}
|
|
417
|
+
function ownerOf(context) {
|
|
418
|
+
return {
|
|
419
|
+
agentSlug: context?.agentSlug ?? 'unknown',
|
|
420
|
+
organisationSlug: context?.organisationSlug ?? null,
|
|
421
|
+
conversationId: context?.conversationId ?? null,
|
|
422
|
+
taskUid: context?.taskUid ?? null,
|
|
423
|
+
projectSlug: context?.projectSlug ?? null,
|
|
424
|
+
openedByUserId: null,
|
|
425
|
+
};
|
|
426
|
+
}
|
|
427
|
+
function newSession(context, key, launched) {
|
|
428
|
+
return {
|
|
429
|
+
id: `BR${randomBytes(4).toString('hex').toUpperCase()}`,
|
|
430
|
+
key,
|
|
431
|
+
owner: ownerOf(context),
|
|
432
|
+
browser: launched.browser,
|
|
433
|
+
context: launched.browserContext,
|
|
434
|
+
pages: [],
|
|
435
|
+
active: 0,
|
|
436
|
+
profile: launched.profile,
|
|
437
|
+
headed: launched.headed,
|
|
438
|
+
viewport: launched.viewport,
|
|
439
|
+
device: launched.device,
|
|
440
|
+
console: [],
|
|
441
|
+
network: [],
|
|
442
|
+
seq: 0,
|
|
443
|
+
events: [],
|
|
444
|
+
readUpTo: { console: 0, network: 0, events: 0 },
|
|
445
|
+
downloadDir: context?.scratchRoot ? join(context.scratchRoot, 'browser-downloads') : null,
|
|
446
|
+
pendingDownloads: new Set(),
|
|
447
|
+
handoff: null,
|
|
448
|
+
shown: null,
|
|
449
|
+
tour: null,
|
|
450
|
+
startedAt: Date.now(),
|
|
451
|
+
lastUsed: Date.now(),
|
|
452
|
+
};
|
|
453
|
+
}
|
|
454
|
+
/** Track the session's tabs and close it with its window or its dispatch. */
|
|
455
|
+
async function wireSession(session, context) {
|
|
456
|
+
const { key, context: browserContext } = session;
|
|
457
|
+
// A persistent context opens with a blank tab of its own.
|
|
458
|
+
for (const p of browserContext.pages())
|
|
459
|
+
trackPage(session, p);
|
|
460
|
+
if (session.pages.length === 0)
|
|
461
|
+
trackPage(session, await browserContext.newPage());
|
|
462
|
+
// Popups and target=_blank links become tabs; the newest one gets focus,
|
|
463
|
+
// like in a real browser.
|
|
464
|
+
browserContext.on('page', (p) => {
|
|
465
|
+
trackPage(session, p);
|
|
466
|
+
session.active = session.pages.indexOf(p);
|
|
467
|
+
emit({ type: 'changed', session });
|
|
468
|
+
});
|
|
469
|
+
// The person closed the headed window: the session is gone.
|
|
470
|
+
browserContext.on('close', () => { if (sessions.get(key) === session)
|
|
471
|
+
void closeSession(key); });
|
|
472
|
+
// A task run that stops takes its browser with it. A chat keeps it until it
|
|
473
|
+
// idles out: a new message from the person stops the running turn, and the
|
|
474
|
+
// next turn (and the live view the person watches) needs the same browser.
|
|
475
|
+
if (context?.signal && context.userPresent !== true) {
|
|
476
|
+
session.signal = context.signal;
|
|
477
|
+
session.onAbort = () => { void closeSession(key); };
|
|
478
|
+
context.signal.addEventListener('abort', session.onAbort, { once: true });
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
/** Short public summary of a session (tool results, live view, Browser app). */
|
|
482
|
+
export async function describeSession(session) {
|
|
483
|
+
const tabs = await Promise.all(session.pages.map(async (p, i) => ({
|
|
484
|
+
index: i,
|
|
485
|
+
active: i === session.active,
|
|
486
|
+
url: p.url(),
|
|
487
|
+
title: await p.title().catch(() => ''),
|
|
488
|
+
})));
|
|
489
|
+
return {
|
|
490
|
+
id: session.id,
|
|
491
|
+
profile: session.profile,
|
|
492
|
+
headed: session.headed,
|
|
493
|
+
viewport: `${session.viewport.width}x${session.viewport.height}`,
|
|
494
|
+
device: session.device,
|
|
495
|
+
tabs,
|
|
496
|
+
handoff: session.handoff,
|
|
497
|
+
};
|
|
498
|
+
}
|
|
499
|
+
/** Exported for tests: drop every session without touching a browser. */
|
|
500
|
+
export function _resetSessionsForTests() {
|
|
501
|
+
sessions.clear();
|
|
502
|
+
listeners.clear();
|
|
503
|
+
}
|
|
504
|
+
/** Exported for tests: register a fake session. */
|
|
505
|
+
export function _putSessionForTests(session) {
|
|
506
|
+
sessions.set(session.key, session);
|
|
507
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { Locator, Page } from 'playwright-core';
|
|
2
|
+
import type { ToolContext } from '@nordhavnhq/runner-runtime';
|
|
3
|
+
/**
|
|
4
|
+
* Wait (at most PAINT_WAIT_MS) for the fonts and for the images in the
|
|
5
|
+
* viewport that are still loading, then two frames so they are painted.
|
|
6
|
+
* DOMContentLoaded alone gave shots with empty thumbnails. Never throws.
|
|
7
|
+
*/
|
|
8
|
+
export declare function waitForPaint(page: Page): Promise<void>;
|
|
9
|
+
/** Screenshot the page (or one element) as JPEG and either inline it into the
|
|
10
|
+
* next tool result (default) or upload it as a permanent project file. */
|
|
11
|
+
export declare function attachScreenshot(page: Page, params: Record<string, unknown>, context: ToolContext | undefined, base: Record<string, unknown>, nameHint: string, element?: Locator): Promise<Record<string, unknown>>;
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import sharp from 'sharp';
|
|
2
|
+
import { uploadImage } from './host.js';
|
|
3
|
+
import { errorLine, readTrimmed } from './params.js';
|
|
4
|
+
const JPEG_QUALITY = 75;
|
|
5
|
+
// The inference API rejects an image with a side over 8000px (it errors the
|
|
6
|
+
// whole message), and a full-page shot of a long page passes that easily.
|
|
7
|
+
// Inline shots are scaled under it; kept (uploaded) shots are left as they are.
|
|
8
|
+
const MAX_INLINE_IMAGE_EDGE = 7600;
|
|
9
|
+
/** Downscale a JPEG so neither side exceeds {@link MAX_INLINE_IMAGE_EDGE}.
|
|
10
|
+
* Returns the input on any failure: better to try than to drop the shot. */
|
|
11
|
+
async function clampInlineImage(shot) {
|
|
12
|
+
try {
|
|
13
|
+
const meta = await sharp(shot).metadata();
|
|
14
|
+
const w = meta.width ?? 0;
|
|
15
|
+
const h = meta.height ?? 0;
|
|
16
|
+
if (w <= MAX_INLINE_IMAGE_EDGE && h <= MAX_INLINE_IMAGE_EDGE)
|
|
17
|
+
return shot;
|
|
18
|
+
return await sharp(shot)
|
|
19
|
+
.resize({
|
|
20
|
+
width: w >= h ? MAX_INLINE_IMAGE_EDGE : undefined,
|
|
21
|
+
height: h > w ? MAX_INLINE_IMAGE_EDGE : undefined,
|
|
22
|
+
fit: 'inside',
|
|
23
|
+
withoutEnlargement: true,
|
|
24
|
+
})
|
|
25
|
+
.jpeg({ quality: JPEG_QUALITY })
|
|
26
|
+
.toBuffer();
|
|
27
|
+
}
|
|
28
|
+
catch {
|
|
29
|
+
return shot;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
/** How long a screenshot waits at most for the page to look done. */
|
|
33
|
+
const PAINT_WAIT_MS = 3_000;
|
|
34
|
+
/**
|
|
35
|
+
* Wait (at most PAINT_WAIT_MS) for the fonts and for the images in the
|
|
36
|
+
* viewport that are still loading, then two frames so they are painted.
|
|
37
|
+
* DOMContentLoaded alone gave shots with empty thumbnails. Never throws.
|
|
38
|
+
*/
|
|
39
|
+
export async function waitForPaint(page) {
|
|
40
|
+
// A dialog that is still open blocks evaluate itself: cap it here too.
|
|
41
|
+
let timer;
|
|
42
|
+
const cap = new Promise((resolve) => { timer = setTimeout(resolve, PAINT_WAIT_MS + 500); });
|
|
43
|
+
const wait = page.evaluate(async (ms) => {
|
|
44
|
+
const done = (async () => {
|
|
45
|
+
await document.fonts?.ready;
|
|
46
|
+
const loading = [...document.images].filter((img) => {
|
|
47
|
+
if (img.complete)
|
|
48
|
+
return false;
|
|
49
|
+
const r = img.getBoundingClientRect();
|
|
50
|
+
return r.width > 0 && r.bottom > 0 && r.top < window.innerHeight;
|
|
51
|
+
});
|
|
52
|
+
await Promise.all(loading.map(img => new Promise((resolve) => {
|
|
53
|
+
img.addEventListener('load', resolve, { once: true });
|
|
54
|
+
img.addEventListener('error', resolve, { once: true });
|
|
55
|
+
})));
|
|
56
|
+
await new Promise(resolve => requestAnimationFrame(() => requestAnimationFrame(resolve)));
|
|
57
|
+
})();
|
|
58
|
+
await Promise.race([done, new Promise(resolve => setTimeout(resolve, ms))]);
|
|
59
|
+
}, PAINT_WAIT_MS).catch(() => { });
|
|
60
|
+
await Promise.race([wait, cap]);
|
|
61
|
+
clearTimeout(timer);
|
|
62
|
+
}
|
|
63
|
+
/** Screenshot the page (or one element) as JPEG and either inline it into the
|
|
64
|
+
* next tool result (default) or upload it as a permanent project file. */
|
|
65
|
+
export async function attachScreenshot(page, params, context, base, nameHint, element) {
|
|
66
|
+
let shot;
|
|
67
|
+
await waitForPaint(page);
|
|
68
|
+
try {
|
|
69
|
+
shot = element
|
|
70
|
+
? await element.screenshot({ type: 'jpeg', quality: JPEG_QUALITY })
|
|
71
|
+
: await page.screenshot({ type: 'jpeg', quality: JPEG_QUALITY, fullPage: params.full_page === true });
|
|
72
|
+
}
|
|
73
|
+
catch (err) {
|
|
74
|
+
return { ...base, screenshotError: errorLine(err) };
|
|
75
|
+
}
|
|
76
|
+
if (params.keep === true)
|
|
77
|
+
return keepScreenshot(shot, params, context, base, nameHint);
|
|
78
|
+
const inlineShot = await clampInlineImage(shot);
|
|
79
|
+
context?.pendingToolImages?.push({ data: inlineShot, mediaType: 'image/jpeg' });
|
|
80
|
+
return { ...base, screenshot: 'inline', mediaType: 'image/jpeg', size: inlineShot.length };
|
|
81
|
+
}
|
|
82
|
+
/** A file-name slug for a kept screenshot. */
|
|
83
|
+
function shotSlug(providedName, nameHint) {
|
|
84
|
+
return (providedName || nameHint || 'browser')
|
|
85
|
+
.toLowerCase()
|
|
86
|
+
.replace(/[^a-z0-9]+/g, '-')
|
|
87
|
+
.replace(/^-+|-+$/g, '')
|
|
88
|
+
.slice(0, 80) || 'browser';
|
|
89
|
+
}
|
|
90
|
+
/** Upload the screenshot as a permanent project file. */
|
|
91
|
+
async function keepScreenshot(shot, params, context, base, nameHint) {
|
|
92
|
+
if (!context?.studioUrl || !context?.agentToken) {
|
|
93
|
+
return { ...base, screenshotError: 'Upload configuration not available; screenshot not kept.' };
|
|
94
|
+
}
|
|
95
|
+
const slug = shotSlug(readTrimmed(params.name), nameHint);
|
|
96
|
+
const uploadResult = await uploadImage(shot, `${slug}.jpg`, 'agent-browser', context, params.folder);
|
|
97
|
+
try {
|
|
98
|
+
return { ...base, kept: JSON.parse(uploadResult) };
|
|
99
|
+
}
|
|
100
|
+
catch {
|
|
101
|
+
return { ...base, screenshotError: uploadResult };
|
|
102
|
+
}
|
|
103
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { ToolContext, ToolDefinition } from '@nordhavnhq/runner-runtime';
|
|
2
|
+
import { type BrowserSession } from './session.js';
|
|
3
|
+
type Exec = (params: Record<string, unknown>, context?: ToolContext) => Promise<string>;
|
|
4
|
+
/** The live view of a session, bare (`/browser/view?session=<agent>:<id>`), or null without a studio URL. */
|
|
5
|
+
export declare function liveUrlOf(session: BrowserSession, context: ToolContext | undefined): string | null;
|
|
6
|
+
export declare const showTools: {
|
|
7
|
+
definition: ToolDefinition;
|
|
8
|
+
execute: Exec;
|
|
9
|
+
}[];
|
|
10
|
+
export {};
|