@oh-my-pi/pi-coding-agent 17.2.4 → 17.2.6
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/CHANGELOG.md +69 -0
- package/dist/{CHANGELOG-bpmhv26t.md → CHANGELOG-gs76k6wc.md} +69 -0
- package/dist/cli.js +4438 -3962
- package/dist/{template-8vdv6xb4.js → template-c2hyaytt.js} +8 -26
- package/dist/types/cli/args.d.ts +4 -1
- package/dist/types/cli/browser-relay-cli.d.ts +15 -0
- package/dist/types/cli/command-help.d.ts +99 -0
- package/dist/types/cli/gc-cli.d.ts +1 -0
- package/dist/types/cli/help-extra.d.ts +2 -0
- package/dist/types/cli/thinking-levels.d.ts +5 -0
- package/dist/types/cli/update-cli.d.ts +13 -0
- package/dist/types/commands/browser-relay.d.ts +38 -0
- package/dist/types/commands/commit.d.ts +3 -0
- package/dist/types/commands/complete.d.ts +1 -1
- package/dist/types/commands/completions.d.ts +6 -0
- package/dist/types/commands/grep.d.ts +3 -0
- package/dist/types/commands/join.d.ts +4 -0
- package/dist/types/commands/launch-help.d.ts +171 -0
- package/dist/types/commands/launch.d.ts +5 -1
- package/dist/types/commands/models.d.ts +3 -0
- package/dist/types/commands/say.d.ts +10 -0
- package/dist/types/commands/worktree.d.ts +3 -0
- package/dist/types/config/keybindings.d.ts +5 -0
- package/dist/types/config/model-discovery.d.ts +2 -2
- package/dist/types/config/models-config-schema-bundle.d.ts +3 -0
- package/dist/types/config/models-config-schema.d.ts +2 -0
- package/dist/types/config/models-config.d.ts +1 -0
- package/dist/types/config/service-tier.d.ts +2 -0
- package/dist/types/config/settings-schema.d.ts +69 -21
- package/dist/types/cursor-bridge-tools.d.ts +1 -1
- package/dist/types/cursor.d.ts +7 -4
- package/dist/types/edit/index.d.ts +3 -3
- package/dist/types/edit/modes/replace.d.ts +22 -15
- package/dist/types/edit/renderer.d.ts +1 -1
- package/dist/types/eval/py/kernel.d.ts +3 -1
- package/dist/types/eval/py/spawn-options.d.ts +12 -16
- package/dist/types/extensibility/legacy-pi-ai-shim.d.ts +8 -1
- package/dist/types/extensibility/legacy-pi-coding-agent-shim.d.ts +1 -1
- package/dist/types/extensibility/plugins/legacy-pi-compat.d.ts +2 -0
- package/dist/types/launch/broker.d.ts +1 -1
- package/dist/types/launch/client.d.ts +15 -4
- package/dist/types/launch/ensure.d.ts +8 -0
- package/dist/types/launch/paths.d.ts +1 -1
- package/dist/types/launch/protocol.d.ts +19 -2
- package/dist/types/lsp/client.d.ts +2 -0
- package/dist/types/lsp/mux/daemon.d.ts +15 -0
- package/dist/types/lsp/mux/protocol.d.ts +57 -0
- package/dist/types/lsp/mux/server.d.ts +18 -0
- package/dist/types/lsp/types.d.ts +22 -2
- package/dist/types/main.d.ts +2 -0
- package/dist/types/mnemopi/embed-client.d.ts +1 -1
- package/dist/types/mnemopi/state.d.ts +0 -23
- package/dist/types/modes/components/assistant-message.d.ts +2 -0
- package/dist/types/modes/components/btw-panel.d.ts +3 -0
- package/dist/types/modes/components/custom-editor.d.ts +2 -1
- package/dist/types/modes/components/read-tool-group.d.ts +2 -0
- package/dist/types/modes/components/stripped-tool-calls-placeholder.d.ts +12 -0
- package/dist/types/modes/components/tool-execution.d.ts +2 -0
- package/dist/types/modes/controllers/btw-controller.d.ts +2 -0
- package/dist/types/modes/controllers/command-controller.d.ts +1 -0
- package/dist/types/modes/controllers/input-controller.d.ts +1 -0
- package/dist/types/modes/interactive-mode.d.ts +5 -1
- package/dist/types/modes/types.d.ts +4 -1
- package/dist/types/modes/workflow.d.ts +2 -1
- package/dist/types/sdk.d.ts +3 -1
- package/dist/types/secrets/index.d.ts +1 -1
- package/dist/types/security/contracts/schemas.d.ts +405 -403
- package/dist/types/session/agent-session-types.d.ts +8 -1
- package/dist/types/session/agent-session.d.ts +26 -2
- package/dist/types/session/indexed-session-storage.d.ts +1 -0
- package/dist/types/session/launch-completion.d.ts +10 -0
- package/dist/types/session/session-advisors.d.ts +1 -1
- package/dist/types/session/session-context.d.ts +2 -1
- package/dist/types/session/session-entries.d.ts +15 -1
- package/dist/types/session/session-maintenance.d.ts +1 -0
- package/dist/types/session/session-manager.d.ts +23 -11
- package/dist/types/session/session-stats.d.ts +10 -0
- package/dist/types/session/session-storage.d.ts +15 -3
- package/dist/types/session/yield-queue.d.ts +6 -1
- package/dist/types/system-prompt.d.ts +4 -1
- package/dist/types/task/index.d.ts +2 -1
- package/dist/types/task/spawn-policy.d.ts +5 -0
- package/dist/types/thinking.d.ts +1 -7
- package/dist/types/tools/ast-grep.d.ts +1 -1
- package/dist/types/tools/browser/registry.d.ts +2 -1
- package/dist/types/tools/browser/relay/bridge.d.ts +35 -0
- package/dist/types/tools/browser/relay/daemon.d.ts +16 -0
- package/dist/types/tools/browser/relay/kind.d.ts +19 -0
- package/dist/types/tools/browser/relay/protocol.d.ts +97 -0
- package/dist/types/tools/browser/relay/server.d.ts +35 -0
- package/dist/types/tools/browser/render.d.ts +1 -0
- package/dist/types/tools/browser.d.ts +3 -0
- package/dist/types/tools/computer/exposure.d.ts +9 -7
- package/dist/types/tools/computer/protocol.d.ts +70 -17
- package/dist/types/tools/computer/supervisor.d.ts +23 -10
- package/dist/types/tools/computer/worker-entry.d.ts +1 -0
- package/dist/types/tools/computer/worker.d.ts +42 -7
- package/dist/types/tools/computer-renderer.d.ts +7 -5
- package/dist/types/tools/computer.d.ts +25 -44
- package/dist/types/tools/context.d.ts +3 -0
- package/dist/types/tools/glob.d.ts +1 -1
- package/dist/types/tools/grep.d.ts +1 -1
- package/dist/types/tools/index.d.ts +9 -0
- package/dist/types/tools/render-utils.d.ts +2 -0
- package/dist/types/tools/{browser/run-cancellation.d.ts → run-scope.d.ts} +8 -8
- package/dist/types/tools/tool-timeouts.d.ts +5 -0
- package/dist/types/tools/xdev.d.ts +8 -0
- package/dist/types/utils/clipboard.d.ts +2 -2
- package/dist/types/web/kagi.d.ts +1 -0
- package/dist/types/web/search/index.d.ts +1 -1
- package/dist/types/web/search/providers/anthropic.d.ts +1 -0
- package/dist/types/web/search/providers/base.d.ts +2 -0
- package/dist/types/web/search/providers/brave.d.ts +1 -0
- package/dist/types/web/search/providers/browser-page.d.ts +1 -0
- package/dist/types/web/search/providers/codex.d.ts +1 -0
- package/dist/types/web/search/providers/exa.d.ts +1 -0
- package/dist/types/web/search/providers/firecrawl.d.ts +1 -0
- package/dist/types/web/search/providers/gemini.d.ts +1 -0
- package/dist/types/web/search/providers/jina.d.ts +1 -0
- package/dist/types/web/search/providers/kagi.d.ts +1 -0
- package/dist/types/web/search/providers/kimi.d.ts +1 -0
- package/dist/types/web/search/providers/parallel.d.ts +1 -0
- package/dist/types/web/search/providers/perplexity.d.ts +1 -0
- package/dist/types/web/search/providers/searxng.d.ts +1 -0
- package/dist/types/web/search/providers/tavily.d.ts +1 -0
- package/dist/types/web/search/providers/tinyfish.d.ts +1 -0
- package/dist/types/web/search/providers/utils.d.ts +6 -9
- package/dist/types/web/search/providers/zai.d.ts +1 -0
- package/dist/types/web/search/types.d.ts +4 -0
- package/package.json +12 -12
- package/src/cli/args.ts +7 -87
- package/src/cli/browser-relay-cli.ts +119 -0
- package/src/cli/command-help.ts +103 -0
- package/src/cli/completion-gen.ts +5 -5
- package/src/cli/flag-tables.ts +9 -0
- package/src/cli/gc-cli.ts +641 -21
- package/src/cli/help-extra.ts +89 -0
- package/src/cli/setup-cli.ts +13 -21
- package/src/cli/thinking-levels.ts +7 -0
- package/src/cli/update-cli.ts +26 -7
- package/src/cli-commands.ts +170 -33
- package/src/cli.ts +17 -5
- package/src/commands/acp.ts +3 -1
- package/src/commands/agents.ts +3 -2
- package/src/commands/auth-broker.ts +3 -2
- package/src/commands/auth-gateway.ts +3 -2
- package/src/commands/bench.ts +2 -3
- package/src/commands/browser-relay.ts +53 -0
- package/src/commands/cleanse.ts +2 -2
- package/src/commands/commit.ts +3 -2
- package/src/commands/complete.ts +2 -1
- package/src/commands/completions.ts +3 -2
- package/src/commands/config.ts +3 -2
- package/src/commands/dry-balance.ts +2 -2
- package/src/commands/gallery.ts +3 -2
- package/src/commands/gc.ts +3 -2
- package/src/commands/grep.ts +3 -2
- package/src/commands/grievances.ts +3 -2
- package/src/commands/install.ts +2 -2
- package/src/commands/join.ts +3 -2
- package/src/commands/launch-help.ts +116 -0
- package/src/commands/launch.ts +7 -191
- package/src/commands/models.ts +3 -2
- package/src/commands/plugin.ts +3 -2
- package/src/commands/read.ts +3 -2
- package/src/commands/say.ts +3 -2
- package/src/commands/setup.ts +3 -2
- package/src/commands/shell.ts +3 -2
- package/src/commands/ssh.ts +3 -2
- package/src/commands/stats.ts +3 -2
- package/src/commands/tiny-models.ts +2 -2
- package/src/commands/token.ts +2 -2
- package/src/commands/ttsr.ts +2 -2
- package/src/commands/update.ts +3 -2
- package/src/commands/usage.ts +3 -2
- package/src/commands/web-search.ts +3 -2
- package/src/commands/worktree.ts +3 -2
- package/src/config/keybindings.ts +5 -0
- package/src/config/model-discovery.ts +44 -27
- package/src/config/model-registry.ts +19 -13
- package/src/config/models-config-schema-bundle.ts +9 -0
- package/src/config/resolve-config-value.ts +2 -1
- package/src/config/service-tier.ts +5 -0
- package/src/config/settings-schema.ts +72 -20
- package/src/config/settings.ts +1 -1
- package/src/cursor-bridge-tools.ts +1 -1
- package/src/cursor.ts +10 -14
- package/src/edit/diff.ts +17 -8
- package/src/edit/index.ts +20 -7
- package/src/edit/modes/replace.ts +93 -40
- package/src/edit/renderer.ts +1 -1
- package/src/edit/streaming.ts +14 -17
- package/src/eval/py/kernel.ts +2 -1
- package/src/eval/py/spawn-options.ts +24 -29
- package/src/export/html/index.ts +10 -3
- package/src/export/html/template.js +8 -26
- package/src/export/share.ts +4 -0
- package/src/extensibility/extensions/wrapper.ts +1 -0
- package/src/extensibility/legacy-pi-ai-shim.ts +22 -1
- package/src/extensibility/legacy-pi-coding-agent-shim.ts +7 -6
- package/src/extensibility/plugins/legacy-pi-compat.ts +124 -57
- package/src/extensibility/skills.ts +11 -0
- package/src/extensibility/utils.ts +10 -2
- package/src/launch/broker.ts +223 -7
- package/src/launch/client.ts +192 -21
- package/src/launch/ensure.ts +73 -0
- package/src/launch/paths.ts +1 -1
- package/src/launch/protocol.ts +54 -2
- package/src/lsp/client.ts +23 -2
- package/src/lsp/index.ts +8 -0
- package/src/lsp/mux/daemon.ts +348 -0
- package/src/lsp/mux/protocol.ts +96 -0
- package/src/lsp/mux/server.ts +797 -0
- package/src/lsp/types.ts +28 -2
- package/src/main.ts +20 -8
- package/src/mcp/config-writer.ts +1 -1
- package/src/mcp/oauth-flow.ts +11 -1
- package/src/mnemopi/embed-client.ts +49 -2
- package/src/mnemopi/state.ts +57 -23
- package/src/modes/components/assistant-message.ts +11 -0
- package/src/modes/components/btw-panel.ts +41 -4
- package/src/modes/components/chat-transcript-builder.ts +4 -0
- package/src/modes/components/custom-editor.ts +9 -0
- package/src/modes/components/read-tool-group.ts +10 -0
- package/src/modes/components/status-line/component.ts +1 -1
- package/src/modes/components/stripped-tool-calls-placeholder.ts +35 -0
- package/src/modes/components/tool-execution.ts +8 -0
- package/src/modes/components/tree-selector.ts +46 -48
- package/src/modes/controllers/btw-controller.ts +55 -7
- package/src/modes/controllers/command-controller.ts +31 -0
- package/src/modes/controllers/event-controller.ts +17 -0
- package/src/modes/controllers/input-controller.ts +63 -8
- package/src/modes/controllers/mcp-command-controller.ts +11 -5
- package/src/modes/controllers/selector-controller.ts +20 -0
- package/src/modes/interactive-mode.ts +18 -2
- package/src/modes/types.ts +9 -1
- package/src/modes/utils/hotkeys-markdown.ts +1 -0
- package/src/modes/utils/interactive-context-helpers.ts +1 -0
- package/src/modes/utils/ui-helpers.ts +6 -10
- package/src/modes/workflow.ts +8 -2
- package/src/prompts/agents/init.md +1 -1
- package/src/prompts/session/launch-completion.md +1 -0
- package/src/prompts/system/plan-mode-active.md +2 -2
- package/src/prompts/system/system-prompt.md +26 -52
- package/src/prompts/system/workflow-notice.md +3 -3
- package/src/prompts/tools/ast-grep.md +1 -1
- package/src/prompts/tools/bash.md +1 -1
- package/src/prompts/tools/browser.md +1 -0
- package/src/prompts/tools/computer.md +21 -21
- package/src/prompts/tools/glob.md +1 -1
- package/src/prompts/tools/grep.md +1 -1
- package/src/prompts/tools/replace.md +5 -4
- package/src/prompts/tools/task.md +4 -4
- package/src/sdk.ts +49 -6
- package/src/secrets/index.ts +1 -1
- package/src/security/contracts/schemas.ts +205 -183
- package/src/security/contracts/validation.ts +5 -1
- package/src/security/store.ts +2 -2
- package/src/session/agent-session-types.ts +9 -1
- package/src/session/agent-session.ts +204 -15
- package/src/session/indexed-session-storage.ts +9 -0
- package/src/session/launch-completion.ts +37 -0
- package/src/session/prewalk.ts +24 -2
- package/src/session/session-advisors.ts +2 -2
- package/src/session/session-context.ts +42 -12
- package/src/session/session-dump-format.ts +1 -1
- package/src/session/session-entries.ts +17 -1
- package/src/session/session-maintenance.ts +38 -4
- package/src/session/session-manager.ts +123 -67
- package/src/session/session-paths.ts +118 -55
- package/src/session/session-stats.ts +48 -3
- package/src/session/session-storage.ts +30 -22
- package/src/session/stream-guards.ts +1 -1
- package/src/session/yield-queue.ts +121 -16
- package/src/slash-commands/builtin-registry.ts +37 -24
- package/src/system-prompt.ts +8 -3
- package/src/task/index.ts +23 -8
- package/src/task/spawn-policy.ts +14 -0
- package/src/thinking.ts +2 -8
- package/src/tools/ast-grep.ts +10 -4
- package/src/tools/browser/cmux/cmux-tab.ts +97 -14
- package/src/tools/browser/cmux/rpc.ts +2 -8
- package/src/tools/browser/registry.ts +53 -2
- package/src/tools/browser/relay/bridge.ts +945 -0
- package/src/tools/browser/relay/daemon.ts +117 -0
- package/src/tools/browser/relay/extension-assets/background.js.txt +242 -0
- package/src/tools/browser/relay/extension-assets/manifest.json.txt +14 -0
- package/src/tools/browser/relay/extension-assets/options.html.txt +53 -0
- package/src/tools/browser/relay/extension-assets/options.js.txt +23 -0
- package/src/tools/browser/relay/kind.ts +41 -0
- package/src/tools/browser/relay/protocol.ts +54 -0
- package/src/tools/browser/relay/server.ts +141 -0
- package/src/tools/browser/render.ts +4 -1
- package/src/tools/browser/shared-daemon.ts +7 -58
- package/src/tools/browser/tab-supervisor.ts +5 -4
- package/src/tools/browser/tab-worker.ts +33 -12
- package/src/tools/browser.ts +26 -1
- package/src/tools/computer/exposure.ts +11 -35
- package/src/tools/computer/protocol.ts +51 -10
- package/src/tools/computer/supervisor.ts +227 -124
- package/src/tools/computer/worker-entry.ts +11 -6
- package/src/tools/computer/worker.ts +695 -81
- package/src/tools/computer-renderer.ts +104 -65
- package/src/tools/computer.ts +134 -428
- package/src/tools/context.ts +3 -0
- package/src/tools/glob.ts +9 -2
- package/src/tools/grep.ts +12 -6
- package/src/tools/hub/launch.ts +122 -6
- package/src/tools/index.ts +9 -0
- package/src/tools/inspect-image.ts +32 -6
- package/src/tools/render-utils.ts +2 -0
- package/src/tools/{browser/run-cancellation.ts → run-scope.ts} +11 -11
- package/src/tools/tool-timeouts.ts +1 -0
- package/src/tools/xdev.ts +29 -2
- package/src/utils/clipboard.ts +40 -14
- package/src/utils/command-args.ts +26 -28
- package/src/utils/tool-choice.ts +0 -13
- package/src/web/kagi.ts +2 -1
- package/src/web/search/index.ts +18 -2
- package/src/web/search/providers/anthropic.ts +4 -1
- package/src/web/search/providers/base.ts +2 -0
- package/src/web/search/providers/brave.ts +3 -1
- package/src/web/search/providers/browser-page.ts +6 -4
- package/src/web/search/providers/codex.ts +4 -1
- package/src/web/search/providers/duckduckgo.ts +2 -1
- package/src/web/search/providers/ecosia.ts +2 -1
- package/src/web/search/providers/exa.ts +4 -2
- package/src/web/search/providers/firecrawl.ts +3 -1
- package/src/web/search/providers/gemini.ts +8 -2
- package/src/web/search/providers/google.ts +2 -1
- package/src/web/search/providers/jina.ts +12 -2
- package/src/web/search/providers/kagi.ts +3 -0
- package/src/web/search/providers/kimi.ts +5 -1
- package/src/web/search/providers/mojeek.ts +2 -1
- package/src/web/search/providers/parallel.ts +5 -1
- package/src/web/search/providers/perplexity.ts +6 -3
- package/src/web/search/providers/public.ts +1 -1
- package/src/web/search/providers/searxng.ts +11 -5
- package/src/web/search/providers/startpage.ts +10 -4
- package/src/web/search/providers/synthetic.ts +11 -5
- package/src/web/search/providers/tavily.ts +3 -1
- package/src/web/search/providers/tinyfish.ts +3 -1
- package/src/web/search/providers/utils.ts +12 -10
- package/src/web/search/providers/xai.ts +2 -2
- package/src/web/search/providers/zai.ts +18 -3
- package/src/web/search/types.ts +6 -0
- package/dist/types/config/file-lock.d.ts +0 -29
- package/src/config/file-lock.ts +0 -164
|
@@ -1,131 +1,745 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { AsyncLocalStorage } from "node:async_hooks";
|
|
2
|
+
import * as os from "node:os";
|
|
3
|
+
import * as path from "node:path";
|
|
4
|
+
|
|
5
|
+
import type {
|
|
6
|
+
AxNode,
|
|
7
|
+
AxQuery,
|
|
8
|
+
AxSnapshotOptions,
|
|
9
|
+
DesktopCapabilities,
|
|
10
|
+
DesktopDisplay,
|
|
11
|
+
DesktopPoint,
|
|
12
|
+
DesktopSessionOptions,
|
|
13
|
+
DesktopWindow,
|
|
14
|
+
PointerOptions,
|
|
15
|
+
} from "@oh-my-pi/pi-natives";
|
|
2
16
|
import { createDesktopSession } from "@oh-my-pi/pi-natives/desktop";
|
|
3
|
-
import
|
|
17
|
+
import * as postmortem from "@oh-my-pi/pi-utils/postmortem";
|
|
18
|
+
import { Snowflake } from "@oh-my-pi/pi-utils/snowflake";
|
|
19
|
+
import { JsRuntime, type RuntimeHooks } from "../../eval/js/shared/runtime";
|
|
20
|
+
import { copyToClipboard, readTextFromClipboard } from "../../utils/clipboard";
|
|
21
|
+
import { cloneSafe, RunOutput } from "../browser/run-output";
|
|
22
|
+
import {
|
|
23
|
+
bindRunFacade,
|
|
24
|
+
markHandled,
|
|
25
|
+
resolvePredicateTimeout,
|
|
26
|
+
type WaitPredicateOptions,
|
|
27
|
+
waitForRun,
|
|
28
|
+
} from "../run-scope";
|
|
29
|
+
import { ToolAbortError, ToolError, throwIfAborted } from "../tool-errors";
|
|
30
|
+
import type {
|
|
31
|
+
ComputerScreenshot,
|
|
32
|
+
ComputerSessionSnapshot,
|
|
33
|
+
ComputerWorkerInbound,
|
|
34
|
+
ComputerWorkerTransport,
|
|
35
|
+
RunErrorPayload,
|
|
36
|
+
ToolReply,
|
|
37
|
+
} from "./protocol";
|
|
4
38
|
|
|
39
|
+
/** Native desktop operations consumed by the script runtime. */
|
|
5
40
|
export interface NativeDesktopSession {
|
|
6
|
-
readonly capabilities:
|
|
7
|
-
|
|
8
|
-
|
|
41
|
+
readonly capabilities: DesktopCapabilities;
|
|
42
|
+
listDisplays(): Promise<DesktopDisplay[]>;
|
|
43
|
+
listWindows(): Promise<DesktopWindow[]>;
|
|
44
|
+
capture(
|
|
45
|
+
target: string,
|
|
46
|
+
caps?: { maxWidth?: number; maxHeight?: number } | null,
|
|
47
|
+
): Promise<{
|
|
48
|
+
data: Uint8Array;
|
|
49
|
+
width: number;
|
|
50
|
+
height: number;
|
|
51
|
+
sourceWidth: number;
|
|
52
|
+
sourceHeight: number;
|
|
53
|
+
target: string;
|
|
54
|
+
}>;
|
|
55
|
+
click(target: string, x: number, y: number, opts?: PointerOptions | null): Promise<void>;
|
|
56
|
+
moveMouse(target: string, x: number, y: number, opts?: PointerOptions | null): Promise<void>;
|
|
57
|
+
drag(target: string, points: DesktopPoint[], opts?: PointerOptions | null): Promise<void>;
|
|
58
|
+
scroll(target: string, x: number, y: number, dx: number, dy: number, opts?: PointerOptions | null): Promise<void>;
|
|
59
|
+
typeText(target: string, text: string, opts?: PointerOptions | null): Promise<void>;
|
|
60
|
+
keyChord(target: string, keys: string[], opts?: PointerOptions | null): Promise<void>;
|
|
61
|
+
raiseWindow(windowId: string): Promise<void>;
|
|
62
|
+
axSnapshot(target: string, opts?: AxSnapshotOptions | null): Promise<{ text: string }>;
|
|
63
|
+
axQuery(target: string, query: AxQuery): Promise<AxNode[]>;
|
|
64
|
+
axElementAt(target: string, x: number, y: number): Promise<AxNode | null | undefined>;
|
|
65
|
+
axFocused(): Promise<AxNode | null | undefined>;
|
|
66
|
+
axNode(ref: string): Promise<AxNode>;
|
|
67
|
+
axAttributes(ref: string): Promise<Array<[string, string]>>;
|
|
68
|
+
axChildren(ref: string): Promise<AxNode[]>;
|
|
69
|
+
axParent(ref: string): Promise<AxNode | null | undefined>;
|
|
70
|
+
axPerform(ref: string, action: string): Promise<void>;
|
|
71
|
+
axSetValue(ref: string, value: string): Promise<void>;
|
|
72
|
+
axFocus(ref: string): Promise<void>;
|
|
73
|
+
axClick(ref: string, opts?: PointerOptions | null): Promise<void>;
|
|
9
74
|
close(): Promise<void>;
|
|
10
75
|
}
|
|
11
76
|
|
|
77
|
+
/** Creates the native session co-located with the computer worker runtime. */
|
|
12
78
|
export type NativeDesktopSessionFactory = (options: DesktopSessionOptions) => NativeDesktopSession;
|
|
13
79
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
80
|
+
type WindowFilter = { app?: string; title?: string };
|
|
81
|
+
type DeliveryOptions = { delivery?: string };
|
|
82
|
+
type ScreenshotOptions = { silent?: boolean };
|
|
83
|
+
type ClickOptions = DeliveryOptions & { button?: string; count?: number; modifiers?: string[] };
|
|
84
|
+
type DragOptions = DeliveryOptions & { modifiers?: string[] };
|
|
85
|
+
type ScrollOptions = DeliveryOptions & { dx?: number; dy?: number };
|
|
86
|
+
type AxOptions = Pick<AxSnapshotOptions, "all" | "maxDepth">;
|
|
87
|
+
|
|
88
|
+
type PendingTool = { resolve(value: unknown): void; reject(reason?: unknown): void };
|
|
89
|
+
interface ActiveRun {
|
|
90
|
+
id: string;
|
|
91
|
+
ac: AbortController;
|
|
92
|
+
signal: AbortSignal;
|
|
93
|
+
pendingTools: Map<string, PendingTool>;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
interface ComputerRunContext {
|
|
97
|
+
signal: AbortSignal;
|
|
98
|
+
readOnly: boolean;
|
|
99
|
+
snapshot: ComputerSessionSnapshot;
|
|
100
|
+
output: RunOutput;
|
|
101
|
+
screenshots: ComputerScreenshot[];
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
type RunContextAccessor = () => ComputerRunContext;
|
|
21
105
|
|
|
22
|
-
function
|
|
106
|
+
function errorPayload(error: unknown): RunErrorPayload {
|
|
107
|
+
if (error instanceof ToolAbortError) {
|
|
108
|
+
return { name: error.name, message: error.message, stack: error.stack, isToolError: false, isAbort: true };
|
|
109
|
+
}
|
|
110
|
+
if (error instanceof ToolError) {
|
|
111
|
+
return { name: error.name, message: error.message, stack: error.stack, isToolError: true, isAbort: false };
|
|
112
|
+
}
|
|
23
113
|
if (error instanceof Error) {
|
|
24
|
-
return { name: error.name, message: error.message,
|
|
114
|
+
return { name: error.name, message: error.message, stack: error.stack, isToolError: false, isAbort: false };
|
|
115
|
+
}
|
|
116
|
+
return { name: "Error", message: String(error), isToolError: false, isAbort: false };
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
function replyError(payload: RunErrorPayload): Error {
|
|
120
|
+
if (payload.isAbort) {
|
|
121
|
+
const error = new ToolAbortError(payload.message || "Tool call aborted");
|
|
122
|
+
if (payload.stack) error.stack = payload.stack;
|
|
123
|
+
return error;
|
|
124
|
+
}
|
|
125
|
+
const ErrorType = payload.isToolError ? ToolError : Error;
|
|
126
|
+
const error = new ErrorType(payload.message);
|
|
127
|
+
if (payload.name) error.name = payload.name;
|
|
128
|
+
if (payload.stack) error.stack = payload.stack;
|
|
129
|
+
return error;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
function nativeError(error: unknown): ToolError {
|
|
133
|
+
return new ToolError(error instanceof Error ? error.message : String(error));
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
async function nativeCall<T>(signal: AbortSignal, call: () => Promise<T>): Promise<T> {
|
|
137
|
+
throwIfAborted(signal);
|
|
138
|
+
try {
|
|
139
|
+
const value = await call();
|
|
140
|
+
throwIfAborted(signal);
|
|
141
|
+
return value;
|
|
142
|
+
} catch (error) {
|
|
143
|
+
if (error instanceof ToolAbortError) throw error;
|
|
144
|
+
throw nativeError(error);
|
|
25
145
|
}
|
|
26
|
-
return { name: "Error", message: String(error) };
|
|
27
146
|
}
|
|
28
147
|
|
|
29
|
-
function
|
|
30
|
-
|
|
31
|
-
|
|
148
|
+
function pointerOptions(options?: ClickOptions | DragOptions | DeliveryOptions): PointerOptions | undefined {
|
|
149
|
+
if (!options) return undefined;
|
|
150
|
+
const mapped: PointerOptions = {};
|
|
151
|
+
if ("button" in options && options.button !== undefined) mapped.button = options.button;
|
|
152
|
+
if ("count" in options && options.count !== undefined) mapped.count = options.count;
|
|
153
|
+
if ("modifiers" in options && options.modifiers !== undefined) mapped.modifiers = options.modifiers;
|
|
154
|
+
if (options.delivery !== undefined) mapped.deliveryMode = options.delivery;
|
|
155
|
+
return mapped;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
function chordKeys(chord: string | string[]): string[] {
|
|
159
|
+
return typeof chord === "string"
|
|
160
|
+
? chord
|
|
161
|
+
.split("+")
|
|
162
|
+
.map(key => key.trim())
|
|
163
|
+
.filter(Boolean)
|
|
164
|
+
: chord;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
function matchesFilter(window: DesktopWindow, filter?: WindowFilter): boolean {
|
|
168
|
+
if (!filter) return true;
|
|
169
|
+
const app = filter.app?.toLocaleLowerCase();
|
|
170
|
+
const title = filter.title?.toLocaleLowerCase();
|
|
171
|
+
return (
|
|
172
|
+
(!app || window.app.toLocaleLowerCase().includes(app)) &&
|
|
173
|
+
(!title || window.title.toLocaleLowerCase().includes(title))
|
|
174
|
+
);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
function guardRun(context: ComputerRunContext, method: string): void {
|
|
178
|
+
if (context.readOnly) throw new ToolError(`read-only run: '${method}' requires read_only: false`);
|
|
179
|
+
throwIfAborted(context.signal);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
async function captureScreenshot(
|
|
183
|
+
session: NativeDesktopSession,
|
|
184
|
+
getContext: RunContextAccessor,
|
|
185
|
+
target: string,
|
|
186
|
+
options?: ScreenshotOptions,
|
|
187
|
+
): Promise<{ path: string; width: number; height: number }> {
|
|
188
|
+
const context = getContext();
|
|
189
|
+
const frame = await nativeCall(context.signal, () =>
|
|
190
|
+
session.capture(target, {
|
|
191
|
+
maxWidth: context.snapshot.captureMaxWidth,
|
|
192
|
+
maxHeight: context.snapshot.captureMaxHeight,
|
|
193
|
+
}),
|
|
194
|
+
);
|
|
195
|
+
const destination = path.join(os.tmpdir(), `omp-computer-${Snowflake.next()}.png`);
|
|
196
|
+
await Bun.write(destination, frame.data);
|
|
197
|
+
const scaled = frame.width !== frame.sourceWidth || frame.height !== frame.sourceHeight;
|
|
198
|
+
context.screenshots.push({
|
|
199
|
+
path: destination,
|
|
200
|
+
width: frame.width,
|
|
201
|
+
height: frame.height,
|
|
202
|
+
sourceWidth: frame.sourceWidth,
|
|
203
|
+
sourceHeight: frame.sourceHeight,
|
|
204
|
+
target: frame.target,
|
|
205
|
+
});
|
|
206
|
+
if (!options?.silent) {
|
|
207
|
+
context.output.push({
|
|
208
|
+
type: "text",
|
|
209
|
+
text: scaled
|
|
210
|
+
? `screenshot ${frame.target} ${frame.width}×${frame.height} (scaled from ${frame.sourceWidth}×${frame.sourceHeight}) → ${destination}`
|
|
211
|
+
: `screenshot ${frame.target} ${frame.width}×${frame.height} → ${destination}`,
|
|
212
|
+
});
|
|
213
|
+
context.output.push({
|
|
214
|
+
type: "image",
|
|
215
|
+
data: Buffer.from(frame.data.buffer, frame.data.byteOffset, frame.data.byteLength).toString("base64"),
|
|
216
|
+
mimeType: "image/png",
|
|
217
|
+
});
|
|
218
|
+
}
|
|
219
|
+
return { path: destination, width: frame.width, height: frame.height };
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
class El {
|
|
223
|
+
readonly ref: string;
|
|
224
|
+
readonly role: string;
|
|
225
|
+
readonly nativeRole: string;
|
|
226
|
+
readonly title?: string;
|
|
227
|
+
readonly description?: string;
|
|
228
|
+
readonly enabled: boolean;
|
|
229
|
+
readonly focused: boolean;
|
|
230
|
+
readonly childCount: number;
|
|
231
|
+
readonly #session: NativeDesktopSession;
|
|
232
|
+
readonly #getContext: RunContextAccessor;
|
|
233
|
+
|
|
234
|
+
constructor(session: NativeDesktopSession, getContext: RunContextAccessor, node: AxNode) {
|
|
235
|
+
this.#session = session;
|
|
236
|
+
this.#getContext = getContext;
|
|
237
|
+
this.ref = node.ref;
|
|
238
|
+
this.role = node.role;
|
|
239
|
+
this.nativeRole = node.nativeRole;
|
|
240
|
+
this.title = node.title;
|
|
241
|
+
this.description = node.description;
|
|
242
|
+
this.enabled = node.enabled;
|
|
243
|
+
this.focused = node.focused;
|
|
244
|
+
this.childCount = node.childCount;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
async value(): Promise<string | undefined> {
|
|
248
|
+
const { signal } = this.#getContext();
|
|
249
|
+
return (await nativeCall(signal, () => this.#session.axNode(this.ref))).value;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
async setValue(value: string): Promise<void> {
|
|
253
|
+
const context = this.#getContext();
|
|
254
|
+
guardRun(context, "setValue");
|
|
255
|
+
await nativeCall(context.signal, () => this.#session.axSetValue(this.ref, value));
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
async bounds(): Promise<{ x: number; y: number; width: number; height: number } | null> {
|
|
259
|
+
const { signal } = this.#getContext();
|
|
260
|
+
const node = await nativeCall(signal, () => this.#session.axNode(this.ref));
|
|
261
|
+
if (node.x === undefined || node.y === undefined || node.width === undefined || node.height === undefined)
|
|
262
|
+
return null;
|
|
263
|
+
return { x: node.x, y: node.y, width: node.width, height: node.height };
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
async attributes(): Promise<Record<string, string>> {
|
|
267
|
+
const { signal } = this.#getContext();
|
|
268
|
+
return Object.fromEntries(await nativeCall(signal, () => this.#session.axAttributes(this.ref)));
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
async actions(): Promise<string[]> {
|
|
272
|
+
const { signal } = this.#getContext();
|
|
273
|
+
return (await nativeCall(signal, () => this.#session.axNode(this.ref))).actions ?? [];
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
async perform(action: string): Promise<void> {
|
|
277
|
+
const context = this.#getContext();
|
|
278
|
+
guardRun(context, "perform");
|
|
279
|
+
await nativeCall(context.signal, () => this.#session.axPerform(this.ref, action));
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
async press(): Promise<void> {
|
|
283
|
+
const context = this.#getContext();
|
|
284
|
+
guardRun(context, "press");
|
|
285
|
+
await nativeCall(context.signal, () => this.#session.axPerform(this.ref, "press"));
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
async click(options?: DeliveryOptions): Promise<void> {
|
|
289
|
+
const context = this.#getContext();
|
|
290
|
+
guardRun(context, "click");
|
|
291
|
+
await nativeCall(context.signal, () => this.#session.axClick(this.ref, pointerOptions(options)));
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
async focus(): Promise<void> {
|
|
295
|
+
const context = this.#getContext();
|
|
296
|
+
guardRun(context, "focus");
|
|
297
|
+
await nativeCall(context.signal, () => this.#session.axFocus(this.ref));
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
async parent(): Promise<El | null> {
|
|
301
|
+
const { signal } = this.#getContext();
|
|
302
|
+
const node = await nativeCall(signal, () => this.#session.axParent(this.ref));
|
|
303
|
+
return node ? new El(this.#session, this.#getContext, node) : null;
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
async children(): Promise<El[]> {
|
|
307
|
+
const { signal } = this.#getContext();
|
|
308
|
+
return (await nativeCall(signal, () => this.#session.axChildren(this.ref))).map(
|
|
309
|
+
node => new El(this.#session, this.#getContext, node),
|
|
310
|
+
);
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
class Win {
|
|
315
|
+
readonly id: string;
|
|
316
|
+
readonly app: string;
|
|
317
|
+
readonly title: string;
|
|
318
|
+
readonly pid?: number;
|
|
319
|
+
readonly bounds: { x: number; y: number; width: number; height: number };
|
|
320
|
+
readonly focused: boolean;
|
|
321
|
+
readonly #session: NativeDesktopSession;
|
|
322
|
+
readonly #getContext: RunContextAccessor;
|
|
323
|
+
|
|
324
|
+
constructor(session: NativeDesktopSession, getContext: RunContextAccessor, window: DesktopWindow) {
|
|
325
|
+
this.#session = session;
|
|
326
|
+
this.#getContext = getContext;
|
|
327
|
+
this.id = window.id;
|
|
328
|
+
this.app = window.app;
|
|
329
|
+
this.title = window.title;
|
|
330
|
+
this.pid = window.pid;
|
|
331
|
+
this.bounds = { x: window.x, y: window.y, width: window.width, height: window.height };
|
|
332
|
+
this.focused = window.focused;
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
screenshot(options?: ScreenshotOptions): Promise<{ path: string; width: number; height: number }> {
|
|
336
|
+
return captureScreenshot(this.#session, this.#getContext, this.id, options);
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
async click(x: number, y: number, options?: ClickOptions): Promise<void> {
|
|
340
|
+
const context = this.#getContext();
|
|
341
|
+
guardRun(context, "click");
|
|
342
|
+
await nativeCall(context.signal, () => this.#session.click(this.id, x, y, pointerOptions(options)));
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
async doubleClick(x: number, y: number, options?: Omit<ClickOptions, "count">): Promise<void> {
|
|
346
|
+
const context = this.#getContext();
|
|
347
|
+
guardRun(context, "doubleClick");
|
|
348
|
+
await nativeCall(context.signal, () =>
|
|
349
|
+
this.#session.click(this.id, x, y, pointerOptions({ ...options, count: 2 })),
|
|
350
|
+
);
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
async move(x: number, y: number): Promise<void> {
|
|
354
|
+
const context = this.#getContext();
|
|
355
|
+
guardRun(context, "move");
|
|
356
|
+
await nativeCall(context.signal, () => this.#session.moveMouse(this.id, x, y));
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
async drag(points: Array<[number, number]>, options?: DragOptions): Promise<void> {
|
|
360
|
+
const context = this.#getContext();
|
|
361
|
+
guardRun(context, "drag");
|
|
362
|
+
await nativeCall(context.signal, () =>
|
|
363
|
+
this.#session.drag(
|
|
364
|
+
this.id,
|
|
365
|
+
points.map(([x, y]) => ({ x, y })),
|
|
366
|
+
pointerOptions(options),
|
|
367
|
+
),
|
|
368
|
+
);
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
async scroll(x: number, y: number, options: ScrollOptions = {}): Promise<void> {
|
|
372
|
+
const context = this.#getContext();
|
|
373
|
+
guardRun(context, "scroll");
|
|
374
|
+
await nativeCall(context.signal, () =>
|
|
375
|
+
this.#session.scroll(this.id, x, y, options.dx ?? 0, options.dy ?? 0, pointerOptions(options)),
|
|
376
|
+
);
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
async type(text: string, options?: DeliveryOptions): Promise<void> {
|
|
380
|
+
const context = this.#getContext();
|
|
381
|
+
guardRun(context, "type");
|
|
382
|
+
await nativeCall(context.signal, () => this.#session.typeText(this.id, text, pointerOptions(options)));
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
async press(chord: string | string[], options?: DeliveryOptions): Promise<void> {
|
|
386
|
+
const context = this.#getContext();
|
|
387
|
+
guardRun(context, "press");
|
|
388
|
+
await nativeCall(context.signal, () =>
|
|
389
|
+
this.#session.keyChord(this.id, chordKeys(chord), pointerOptions(options)),
|
|
390
|
+
);
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
async raise(): Promise<void> {
|
|
394
|
+
const context = this.#getContext();
|
|
395
|
+
guardRun(context, "raise");
|
|
396
|
+
await nativeCall(context.signal, () => this.#session.raiseWindow(this.id));
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
async ax(options?: AxOptions): Promise<string> {
|
|
400
|
+
const { signal } = this.#getContext();
|
|
401
|
+
return (await nativeCall(signal, () => this.#session.axSnapshot(this.id, options))).text;
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
async find(query: AxQuery): Promise<El[]> {
|
|
405
|
+
const { signal } = this.#getContext();
|
|
406
|
+
return (await nativeCall(signal, () => this.#session.axQuery(this.id, query))).map(
|
|
407
|
+
node => new El(this.#session, this.#getContext, node),
|
|
408
|
+
);
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
async ref(ref: string): Promise<El> {
|
|
412
|
+
const { signal } = this.#getContext();
|
|
413
|
+
return new El(this.#session, this.#getContext, await nativeCall(signal, () => this.#session.axNode(ref)));
|
|
414
|
+
}
|
|
32
415
|
}
|
|
33
416
|
|
|
417
|
+
/** Hosts the persistent JavaScript runtime and native desktop session. */
|
|
34
418
|
export class ComputerWorkerCore {
|
|
419
|
+
readonly #transport: ComputerWorkerTransport;
|
|
420
|
+
readonly #createSession: NativeDesktopSessionFactory;
|
|
421
|
+
readonly #unsubscribe: () => void;
|
|
35
422
|
#session?: NativeDesktopSession;
|
|
36
|
-
#
|
|
423
|
+
#runtime?: JsRuntime;
|
|
424
|
+
#active: ActiveRun | null = null;
|
|
425
|
+
/**
|
|
426
|
+
* Per-run context, carried through AsyncLocalStorage so async work leaked
|
|
427
|
+
* from an ended run (timers, dangling promises) keeps that run's aborted
|
|
428
|
+
* context instead of borrowing the next run's signal and read-only policy.
|
|
429
|
+
*/
|
|
430
|
+
readonly #runContexts = new AsyncLocalStorage<ComputerRunContext>();
|
|
37
431
|
#closed = false;
|
|
38
|
-
#tail: Promise<void> = Promise.resolve();
|
|
39
|
-
readonly #unsubscribe: () => void;
|
|
40
432
|
|
|
41
|
-
constructor(
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
this.#
|
|
433
|
+
constructor(transport: ComputerWorkerTransport, createSession: NativeDesktopSessionFactory = createDesktopSession) {
|
|
434
|
+
this.#transport = transport;
|
|
435
|
+
this.#createSession = createSession;
|
|
436
|
+
this.#unsubscribe = transport.onMessage(message => this.handle(message));
|
|
437
|
+
this.#transport.send({ type: "ready" });
|
|
46
438
|
}
|
|
47
439
|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
440
|
+
/** Routes one supervisor command into the persistent worker state. */
|
|
441
|
+
handle(message: ComputerWorkerInbound): void {
|
|
442
|
+
switch (message.type) {
|
|
443
|
+
case "ping":
|
|
444
|
+
this.#transport.send({ type: "pong", id: message.id });
|
|
445
|
+
return;
|
|
446
|
+
case "run":
|
|
447
|
+
void this.#run(message);
|
|
448
|
+
return;
|
|
449
|
+
case "abort":
|
|
450
|
+
if (this.#active?.id === message.id) this.#active.ac.abort(new ToolAbortError());
|
|
451
|
+
return;
|
|
452
|
+
case "tool-reply":
|
|
453
|
+
this.#deliverToolReply(message.id, message.reply);
|
|
454
|
+
return;
|
|
455
|
+
case "close":
|
|
456
|
+
void this.#close();
|
|
60
457
|
}
|
|
61
|
-
this.#tail = this.#tail.then(() => this.#execute(message.id, message.actions));
|
|
62
458
|
}
|
|
63
459
|
|
|
64
|
-
|
|
65
|
-
if (this.#
|
|
66
|
-
if (this.#session) {
|
|
67
|
-
this.transport.send({
|
|
68
|
-
type: "error",
|
|
69
|
-
error: { name: "Error", message: "Computer worker already initialized" },
|
|
70
|
-
});
|
|
71
|
-
return;
|
|
72
|
-
}
|
|
460
|
+
#ensureSession(snapshot: ComputerSessionSnapshot): NativeDesktopSession {
|
|
461
|
+
if (this.#session) return this.#session;
|
|
73
462
|
try {
|
|
74
|
-
this.#session = this
|
|
75
|
-
this.#
|
|
76
|
-
this.transport.send({ type: "ready", capabilities: this.#session.capabilities });
|
|
463
|
+
this.#session = this.#createSession({ display: snapshot.display });
|
|
464
|
+
return this.#session;
|
|
77
465
|
} catch (error) {
|
|
78
|
-
|
|
466
|
+
throw nativeError(error);
|
|
79
467
|
}
|
|
80
468
|
}
|
|
81
469
|
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
470
|
+
#ensureRuntime(snapshot: ComputerSessionSnapshot): JsRuntime {
|
|
471
|
+
if (this.#runtime) return this.#runtime;
|
|
472
|
+
this.#runtime = new JsRuntime({ initialCwd: snapshot.cwd, sessionId: snapshot.sessionId });
|
|
473
|
+
return this.#runtime;
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
async #run(message: Extract<ComputerWorkerInbound, { type: "run" }>): Promise<void> {
|
|
477
|
+
if (this.#closed) {
|
|
478
|
+
this.#transport.send({
|
|
479
|
+
type: "result",
|
|
480
|
+
id: message.id,
|
|
481
|
+
ok: false,
|
|
482
|
+
error: errorPayload(new ToolError("Computer worker is closed")),
|
|
89
483
|
});
|
|
90
484
|
return;
|
|
91
485
|
}
|
|
486
|
+
if (this.#active) {
|
|
487
|
+
this.#transport.send({
|
|
488
|
+
type: "result",
|
|
489
|
+
id: message.id,
|
|
490
|
+
ok: false,
|
|
491
|
+
error: errorPayload(new ToolError("Computer worker is busy")),
|
|
492
|
+
});
|
|
493
|
+
return;
|
|
494
|
+
}
|
|
495
|
+
const timeoutSignal = AbortSignal.timeout(message.timeoutMs);
|
|
496
|
+
const ac = new AbortController();
|
|
497
|
+
const runAc = new AbortController();
|
|
498
|
+
const signal = AbortSignal.any([timeoutSignal, ac.signal, runAc.signal]);
|
|
499
|
+
const active: ActiveRun = { id: message.id, ac, signal, pendingTools: new Map() };
|
|
500
|
+
this.#active = active;
|
|
501
|
+
const output = new RunOutput();
|
|
502
|
+
const screenshots: ComputerScreenshot[] = [];
|
|
503
|
+
const runContext: ComputerRunContext = {
|
|
504
|
+
signal,
|
|
505
|
+
readOnly: message.session.readOnly,
|
|
506
|
+
snapshot: message.session,
|
|
507
|
+
output,
|
|
508
|
+
screenshots,
|
|
509
|
+
};
|
|
510
|
+
let returnValue: unknown;
|
|
511
|
+
let failure: { error: unknown } | undefined;
|
|
512
|
+
let completed = false;
|
|
92
513
|
try {
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
514
|
+
throwIfAborted(signal);
|
|
515
|
+
const session = this.#ensureSession(message.session);
|
|
516
|
+
const runtime = this.#ensureRuntime(message.session);
|
|
517
|
+
runtime.setCwd(message.session.cwd);
|
|
518
|
+
const desktop = this.#createDesktopScope(session);
|
|
519
|
+
runtime.setRunScope({
|
|
520
|
+
desktop: bindRunFacade(desktop, signal),
|
|
521
|
+
assert: (condition: unknown, text?: string): void => {
|
|
522
|
+
if (!condition) throw new ToolError(text ?? "Assertion failed");
|
|
523
|
+
},
|
|
524
|
+
wait: (msOrPredicate: number | (() => unknown), options?: WaitPredicateOptions): Promise<unknown> => {
|
|
525
|
+
const resolved =
|
|
526
|
+
typeof msOrPredicate === "number"
|
|
527
|
+
? undefined
|
|
528
|
+
: {
|
|
529
|
+
timeout: resolvePredicateTimeout(message.timeoutMs, options?.timeout),
|
|
530
|
+
interval: options?.interval,
|
|
531
|
+
};
|
|
532
|
+
return markHandled(waitForRun(msOrPredicate, signal, resolved));
|
|
533
|
+
},
|
|
534
|
+
});
|
|
535
|
+
const { promise: cancelRejection, reject: rejectCancel } = Promise.withResolvers<never>();
|
|
536
|
+
const onCancel = (): void => {
|
|
537
|
+
const abortError =
|
|
538
|
+
signal.reason instanceof ToolAbortError
|
|
539
|
+
? signal.reason
|
|
540
|
+
: new ToolAbortError(undefined, { cause: signal.reason });
|
|
541
|
+
rejectCancel(
|
|
542
|
+
timeoutSignal.aborted
|
|
543
|
+
? new ToolError(`Computer code execution timed out after ${message.timeoutMs}ms`)
|
|
544
|
+
: abortError,
|
|
545
|
+
);
|
|
546
|
+
const toolAbort = timeoutSignal.aborted
|
|
547
|
+
? postmortem.markExpectedCleanupError(new ToolAbortError(undefined, { cause: timeoutSignal.reason }))
|
|
548
|
+
: abortError;
|
|
549
|
+
for (const pending of active.pendingTools.values()) pending.reject(toolAbort);
|
|
550
|
+
active.pendingTools.clear();
|
|
551
|
+
};
|
|
552
|
+
if (signal.aborted) onCancel();
|
|
553
|
+
else signal.addEventListener("abort", onCancel, { once: true });
|
|
554
|
+
try {
|
|
555
|
+
returnValue = await Promise.race([
|
|
556
|
+
this.#runContexts.run(runContext, () =>
|
|
557
|
+
runtime.run(message.code, `computer-run-${message.id}.js`, this.#runtimeHooks(active, output), {
|
|
558
|
+
runId: message.id,
|
|
559
|
+
cwd: message.session.cwd,
|
|
560
|
+
}),
|
|
561
|
+
),
|
|
562
|
+
cancelRejection,
|
|
563
|
+
]);
|
|
564
|
+
completed = true;
|
|
565
|
+
} finally {
|
|
566
|
+
signal.removeEventListener("abort", onCancel);
|
|
567
|
+
}
|
|
568
|
+
} catch (error) {
|
|
569
|
+
failure = { error };
|
|
570
|
+
} finally {
|
|
571
|
+
runAc.abort(postmortem.markExpectedCleanupError(new ToolAbortError("Computer run ended")));
|
|
572
|
+
if (this.#active?.id === message.id) this.#active = null;
|
|
573
|
+
}
|
|
574
|
+
if (failure !== undefined) {
|
|
575
|
+
this.#transport.send({ type: "result", id: message.id, ok: false, error: errorPayload(failure.error) });
|
|
576
|
+
return;
|
|
577
|
+
}
|
|
578
|
+
if (completed) {
|
|
579
|
+
let capabilities: DesktopCapabilities;
|
|
580
|
+
try {
|
|
581
|
+
capabilities = this.#ensureSession(message.session).capabilities;
|
|
582
|
+
} catch (error) {
|
|
583
|
+
this.#transport.send({
|
|
584
|
+
type: "result",
|
|
585
|
+
id: message.id,
|
|
586
|
+
ok: false,
|
|
587
|
+
error: errorPayload(nativeError(error)),
|
|
102
588
|
});
|
|
103
589
|
return;
|
|
104
590
|
}
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
} catch (error) {
|
|
112
|
-
this.transport.send({ type: "error", id, error: serializeError(error) });
|
|
591
|
+
this.#transport.send({
|
|
592
|
+
type: "result",
|
|
593
|
+
id: message.id,
|
|
594
|
+
ok: true,
|
|
595
|
+
payload: { displays: output.finish(), returnValue: cloneSafe(returnValue), screenshots, capabilities },
|
|
596
|
+
});
|
|
113
597
|
}
|
|
114
598
|
}
|
|
115
599
|
|
|
600
|
+
#runtimeHooks(active: ActiveRun, output: RunOutput): RuntimeHooks {
|
|
601
|
+
return {
|
|
602
|
+
onText: chunk => {
|
|
603
|
+
throwIfAborted(active.signal);
|
|
604
|
+
output.pushText(chunk);
|
|
605
|
+
},
|
|
606
|
+
onDisplay: display => {
|
|
607
|
+
throwIfAborted(active.signal);
|
|
608
|
+
output.pushDisplay(display);
|
|
609
|
+
},
|
|
610
|
+
callTool: (name, args) => {
|
|
611
|
+
throwIfAborted(active.signal);
|
|
612
|
+
return this.#callTool(active, name, args);
|
|
613
|
+
},
|
|
614
|
+
};
|
|
615
|
+
}
|
|
616
|
+
|
|
617
|
+
async #callTool(active: ActiveRun, name: string, args: unknown): Promise<unknown> {
|
|
618
|
+
const id = `computer-tc-${active.id}-${crypto.randomUUID()}`;
|
|
619
|
+
const { promise, resolve, reject } = Promise.withResolvers<unknown>();
|
|
620
|
+
active.pendingTools.set(id, { resolve, reject });
|
|
621
|
+
this.#transport.send({ type: "tool-call", id, runId: active.id, name, args });
|
|
622
|
+
return await promise;
|
|
623
|
+
}
|
|
624
|
+
|
|
625
|
+
#deliverToolReply(id: string, reply: ToolReply): void {
|
|
626
|
+
const pending = this.#active?.pendingTools.get(id);
|
|
627
|
+
if (!pending) return;
|
|
628
|
+
this.#active?.pendingTools.delete(id);
|
|
629
|
+
if (reply.ok) pending.resolve(reply.value);
|
|
630
|
+
else pending.reject(replyError(reply.error));
|
|
631
|
+
}
|
|
632
|
+
|
|
633
|
+
#currentRunContext = (): ComputerRunContext => {
|
|
634
|
+
const context = this.#runContexts.getStore();
|
|
635
|
+
if (!context) throw new ToolError("no active computer run");
|
|
636
|
+
return context;
|
|
637
|
+
};
|
|
638
|
+
|
|
639
|
+
#createDesktopScope(session: NativeDesktopSession): object {
|
|
640
|
+
const getContext = this.#currentRunContext;
|
|
641
|
+
const makeWin = (window: DesktopWindow): Win => new Win(session, getContext, window);
|
|
642
|
+
const el = (node: AxNode): El => new El(session, getContext, node);
|
|
643
|
+
const desktopTarget = new Win(session, getContext, {
|
|
644
|
+
id: "desktop",
|
|
645
|
+
app: "desktop",
|
|
646
|
+
title: "desktop",
|
|
647
|
+
x: 0,
|
|
648
|
+
y: 0,
|
|
649
|
+
width: 0,
|
|
650
|
+
height: 0,
|
|
651
|
+
focused: false,
|
|
652
|
+
});
|
|
653
|
+
return {
|
|
654
|
+
capabilities: (): DesktopCapabilities => {
|
|
655
|
+
const { signal } = getContext();
|
|
656
|
+
throwIfAborted(signal);
|
|
657
|
+
try {
|
|
658
|
+
return session.capabilities;
|
|
659
|
+
} catch (error) {
|
|
660
|
+
throw nativeError(error);
|
|
661
|
+
}
|
|
662
|
+
},
|
|
663
|
+
displays: async (): Promise<DesktopDisplay[]> => {
|
|
664
|
+
const { signal } = getContext();
|
|
665
|
+
return await nativeCall(signal, () => session.listDisplays());
|
|
666
|
+
},
|
|
667
|
+
windows: async (filter?: WindowFilter): Promise<DesktopWindow[]> => {
|
|
668
|
+
const { signal } = getContext();
|
|
669
|
+
return (await nativeCall(signal, () => session.listWindows())).filter(window =>
|
|
670
|
+
matchesFilter(window, filter),
|
|
671
|
+
);
|
|
672
|
+
},
|
|
673
|
+
window: async (selector: string | WindowFilter): Promise<Win> => {
|
|
674
|
+
const { signal } = getContext();
|
|
675
|
+
const windows = await nativeCall(signal, () => session.listWindows());
|
|
676
|
+
const matches =
|
|
677
|
+
typeof selector === "string"
|
|
678
|
+
? windows.filter(window => window.id === selector)
|
|
679
|
+
: windows.filter(window => matchesFilter(window, selector));
|
|
680
|
+
if (matches.length === 0) throw new ToolError(`no window matches ${JSON.stringify(selector)}`);
|
|
681
|
+
if (matches.length > 1) {
|
|
682
|
+
const candidates = matches
|
|
683
|
+
.map(window => `${window.id} ${window.app} ${JSON.stringify(window.title)}`)
|
|
684
|
+
.join("\n");
|
|
685
|
+
throw new ToolError(`multiple windows match ${JSON.stringify(selector)}:\n${candidates}`);
|
|
686
|
+
}
|
|
687
|
+
return makeWin(matches[0]!);
|
|
688
|
+
},
|
|
689
|
+
focusedWindow: async (): Promise<Win | null> => {
|
|
690
|
+
const { signal } = getContext();
|
|
691
|
+
const window = (await nativeCall(signal, () => session.listWindows())).find(candidate => candidate.focused);
|
|
692
|
+
return window ? makeWin(window) : null;
|
|
693
|
+
},
|
|
694
|
+
screenshot: (options?: ScreenshotOptions) => captureScreenshot(session, getContext, "desktop", options),
|
|
695
|
+
click: desktopTarget.click.bind(desktopTarget),
|
|
696
|
+
doubleClick: desktopTarget.doubleClick.bind(desktopTarget),
|
|
697
|
+
move: desktopTarget.move.bind(desktopTarget),
|
|
698
|
+
drag: desktopTarget.drag.bind(desktopTarget),
|
|
699
|
+
scroll: desktopTarget.scroll.bind(desktopTarget),
|
|
700
|
+
type: desktopTarget.type.bind(desktopTarget),
|
|
701
|
+
press: desktopTarget.press.bind(desktopTarget),
|
|
702
|
+
elementAt: async (x: number, y: number): Promise<El | null> => {
|
|
703
|
+
const { signal } = getContext();
|
|
704
|
+
const node = await nativeCall(signal, () => session.axElementAt("desktop", x, y));
|
|
705
|
+
return node ? el(node) : null;
|
|
706
|
+
},
|
|
707
|
+
focusedElement: async (): Promise<El | null> => {
|
|
708
|
+
const { signal } = getContext();
|
|
709
|
+
const node = await nativeCall(signal, () => session.axFocused());
|
|
710
|
+
return node ? el(node) : null;
|
|
711
|
+
},
|
|
712
|
+
clipboard: {
|
|
713
|
+
read: async (): Promise<string> => {
|
|
714
|
+
const { signal } = getContext();
|
|
715
|
+
throwIfAborted(signal);
|
|
716
|
+
const text = await readTextFromClipboard();
|
|
717
|
+
throwIfAborted(signal);
|
|
718
|
+
return text;
|
|
719
|
+
},
|
|
720
|
+
write: async (text: string): Promise<void> => {
|
|
721
|
+
const context = getContext();
|
|
722
|
+
guardRun(context, "clipboard.write");
|
|
723
|
+
await copyToClipboard(text);
|
|
724
|
+
throwIfAborted(context.signal);
|
|
725
|
+
},
|
|
726
|
+
},
|
|
727
|
+
};
|
|
728
|
+
}
|
|
729
|
+
|
|
116
730
|
async #close(): Promise<void> {
|
|
117
731
|
if (this.#closed) return;
|
|
118
732
|
this.#closed = true;
|
|
733
|
+
this.#active?.ac.abort(new ToolAbortError());
|
|
119
734
|
try {
|
|
120
735
|
await this.#session?.close();
|
|
121
|
-
} catch
|
|
122
|
-
|
|
736
|
+
} catch {
|
|
737
|
+
// Closing is best-effort; the worker is exiting and has no request to report this against.
|
|
123
738
|
} finally {
|
|
124
739
|
this.#session = undefined;
|
|
125
|
-
this.#hasReturnedFrame = false;
|
|
126
740
|
this.#unsubscribe();
|
|
127
|
-
this
|
|
128
|
-
this
|
|
741
|
+
this.#transport.send({ type: "closed" });
|
|
742
|
+
this.#transport.close();
|
|
129
743
|
}
|
|
130
744
|
}
|
|
131
745
|
}
|