@namzu/sdk 68.0.1 → 69.0.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/CHANGELOG.md +35 -0
- package/dist/bridge/acp/server.d.ts +12 -0
- package/dist/bridge/acp/server.d.ts.map +1 -1
- package/dist/bridge/acp/server.js +20 -0
- package/dist/bridge/acp/server.js.map +1 -1
- package/dist/compaction/context-window.d.ts +8 -0
- package/dist/compaction/context-window.d.ts.map +1 -1
- package/dist/compaction/context-window.js +14 -0
- package/dist/compaction/context-window.js.map +1 -1
- package/dist/compaction/loaded-skills.d.ts +10 -0
- package/dist/compaction/loaded-skills.d.ts.map +1 -1
- package/dist/compaction/loaded-skills.js +45 -1
- package/dist/compaction/loaded-skills.js.map +1 -1
- package/dist/compaction/manager.d.ts +8 -0
- package/dist/compaction/manager.d.ts.map +1 -1
- package/dist/compaction/manager.js +31 -0
- package/dist/compaction/manager.js.map +1 -1
- package/dist/compaction/manual.d.ts.map +1 -1
- package/dist/compaction/manual.js +7 -1
- package/dist/compaction/manual.js.map +1 -1
- package/dist/compaction/model-summary.js +2 -2
- package/dist/compaction/model-summary.js.map +1 -1
- package/dist/compaction/restore.d.ts +8 -0
- package/dist/compaction/restore.d.ts.map +1 -1
- package/dist/compaction/restore.js +65 -0
- package/dist/compaction/restore.js.map +1 -1
- package/dist/compaction/salience/working-set.d.ts.map +1 -1
- package/dist/compaction/salience/working-set.js +2 -2
- package/dist/compaction/salience/working-set.js.map +1 -1
- package/dist/compaction/tool-result-editing.d.ts +6 -0
- package/dist/compaction/tool-result-editing.d.ts.map +1 -1
- package/dist/compaction/tool-result-editing.js +7 -1
- package/dist/compaction/tool-result-editing.js.map +1 -1
- package/dist/compaction/types.d.ts +2 -0
- package/dist/compaction/types.d.ts.map +1 -1
- package/dist/compaction/wire.d.ts.map +1 -1
- package/dist/compaction/wire.js +4 -0
- package/dist/compaction/wire.js.map +1 -1
- package/dist/manager/session/turn-recorder.d.ts.map +1 -1
- package/dist/manager/session/turn-recorder.js +23 -1
- package/dist/manager/session/turn-recorder.js.map +1 -1
- package/dist/network/join.d.ts +10 -0
- package/dist/network/join.d.ts.map +1 -1
- package/dist/network/join.js +15 -0
- package/dist/network/join.js.map +1 -1
- package/dist/network/service.d.ts.map +1 -1
- package/dist/network/service.js +166 -12
- package/dist/network/service.js.map +1 -1
- package/dist/network/store.d.ts +23 -1
- package/dist/network/store.d.ts.map +1 -1
- package/dist/network/store.js +23 -1
- package/dist/network/store.js.map +1 -1
- package/dist/network/transport.d.ts +1 -1
- package/dist/network/transport.js +1 -1
- package/dist/network/wire.d.ts +12 -0
- package/dist/network/wire.d.ts.map +1 -1
- package/dist/network/wire.js +12 -1
- package/dist/network/wire.js.map +1 -1
- package/dist/prompt/coding-agent-doctrine.d.ts +4 -4
- package/dist/prompt/coding-agent-doctrine.d.ts.map +1 -1
- package/dist/prompt/coding-agent-doctrine.js +7 -2
- package/dist/prompt/coding-agent-doctrine.js.map +1 -1
- package/dist/provider/fallback.d.ts.map +1 -1
- package/dist/provider/fallback.js +5 -0
- package/dist/provider/fallback.js.map +1 -1
- package/dist/provider/idle-timeout.d.ts.map +1 -1
- package/dist/provider/idle-timeout.js +5 -0
- package/dist/provider/idle-timeout.js.map +1 -1
- package/dist/provider/instrumentation.d.ts.map +1 -1
- package/dist/provider/instrumentation.js +10 -0
- package/dist/provider/instrumentation.js.map +1 -1
- package/dist/provider/model-window-suffix.d.ts.map +1 -1
- package/dist/provider/model-window-suffix.js +1 -0
- package/dist/provider/model-window-suffix.js.map +1 -1
- package/dist/provider/retry.d.ts.map +1 -1
- package/dist/provider/retry.js +5 -0
- package/dist/provider/retry.js.map +1 -1
- package/dist/public-runtime.d.ts +1 -1
- package/dist/public-runtime.d.ts.map +1 -1
- package/dist/public-runtime.js +1 -1
- package/dist/public-runtime.js.map +1 -1
- package/dist/public-tools.d.ts +2 -0
- package/dist/public-tools.d.ts.map +1 -1
- package/dist/public-tools.js +4 -0
- package/dist/public-tools.js.map +1 -1
- package/dist/public-types.d.ts +1 -0
- package/dist/public-types.d.ts.map +1 -1
- package/dist/registry/tool/presentation.d.ts +8 -1
- package/dist/registry/tool/presentation.d.ts.map +1 -1
- package/dist/registry/tool/presentation.js +14 -5
- package/dist/registry/tool/presentation.js.map +1 -1
- package/dist/runtime/jobs/registry.d.ts.map +1 -1
- package/dist/runtime/jobs/registry.js +2 -1
- package/dist/runtime/jobs/registry.js.map +1 -1
- package/dist/runtime/query/auto-review/classifier.d.ts.map +1 -1
- package/dist/runtime/query/auto-review/classifier.js +19 -3
- package/dist/runtime/query/auto-review/classifier.js.map +1 -1
- package/dist/runtime/query/auto-review/failure.d.ts +12 -0
- package/dist/runtime/query/auto-review/failure.d.ts.map +1 -0
- package/dist/runtime/query/auto-review/failure.js +46 -0
- package/dist/runtime/query/auto-review/failure.js.map +1 -0
- package/dist/runtime/query/auto-review/index.d.ts +1 -0
- package/dist/runtime/query/auto-review/index.d.ts.map +1 -1
- package/dist/runtime/query/auto-review/index.js +1 -0
- package/dist/runtime/query/auto-review/index.js.map +1 -1
- package/dist/runtime/query/file-evidence-replay.d.ts.map +1 -1
- package/dist/runtime/query/file-evidence-replay.js +6 -6
- package/dist/runtime/query/file-evidence-replay.js.map +1 -1
- package/dist/runtime/query/index.d.ts.map +1 -1
- package/dist/runtime/query/index.js +8 -1
- package/dist/runtime/query/index.js.map +1 -1
- package/dist/runtime/query/iteration/index.d.ts.map +1 -1
- package/dist/runtime/query/iteration/index.js +2 -0
- package/dist/runtime/query/iteration/index.js.map +1 -1
- package/dist/runtime/query/iteration/phases/compaction.d.ts.map +1 -1
- package/dist/runtime/query/iteration/phases/compaction.js +21 -3
- package/dist/runtime/query/iteration/phases/compaction.js.map +1 -1
- package/dist/runtime/query/iteration/phases/context.d.ts +4 -0
- package/dist/runtime/query/iteration/phases/context.d.ts.map +1 -1
- package/dist/runtime/query/iteration/phases/context.js.map +1 -1
- package/dist/runtime/query/iteration/phases/per-call-review.d.ts +6 -2
- package/dist/runtime/query/iteration/phases/per-call-review.d.ts.map +1 -1
- package/dist/runtime/query/iteration/phases/per-call-review.js +24 -9
- package/dist/runtime/query/iteration/phases/per-call-review.js.map +1 -1
- package/dist/runtime/query/iteration/step-shaping.d.ts.map +1 -1
- package/dist/runtime/query/iteration/step-shaping.js +4 -0
- package/dist/runtime/query/iteration/step-shaping.js.map +1 -1
- package/dist/runtime/query/prepare-turn.d.ts +5 -1
- package/dist/runtime/query/prepare-turn.d.ts.map +1 -1
- package/dist/runtime/query/prepare-turn.js +13 -3
- package/dist/runtime/query/prepare-turn.js.map +1 -1
- package/dist/runtime/query/review-policy.d.ts +13 -2
- package/dist/runtime/query/review-policy.d.ts.map +1 -1
- package/dist/runtime/query/review-policy.js +13 -2
- package/dist/runtime/query/review-policy.js.map +1 -1
- package/dist/runtime/query/turn/outcome-ledger.d.ts +4 -0
- package/dist/runtime/query/turn/outcome-ledger.d.ts.map +1 -1
- package/dist/runtime/query/turn/outcome-ledger.js +36 -1
- package/dist/runtime/query/turn/outcome-ledger.js.map +1 -1
- package/dist/sandbox/provider/local.d.ts.map +1 -1
- package/dist/sandbox/provider/local.js +3 -1
- package/dist/sandbox/provider/local.js.map +1 -1
- package/dist/tools/admit-facts.d.ts.map +1 -1
- package/dist/tools/admit-facts.js +20 -0
- package/dist/tools/admit-facts.js.map +1 -1
- package/dist/tools/builtins/bash.d.ts.map +1 -1
- package/dist/tools/builtins/bash.js +5 -2
- package/dist/tools/builtins/bash.js.map +1 -1
- package/dist/tools/builtins/browser.js +1 -1
- package/dist/tools/builtins/browser.js.map +1 -1
- package/dist/tools/builtins/computer-use.d.ts +4 -0
- package/dist/tools/builtins/computer-use.d.ts.map +1 -1
- package/dist/tools/builtins/computer-use.js +106 -5
- package/dist/tools/builtins/computer-use.js.map +1 -1
- package/dist/tools/builtins/content-fingerprint.d.ts.map +1 -1
- package/dist/tools/builtins/content-fingerprint.js +2 -1
- package/dist/tools/builtins/content-fingerprint.js.map +1 -1
- package/dist/tools/builtins/edit-apply.d.ts +1 -0
- package/dist/tools/builtins/edit-apply.d.ts.map +1 -1
- package/dist/tools/builtins/edit-apply.js +108 -21
- package/dist/tools/builtins/edit-apply.js.map +1 -1
- package/dist/tools/builtins/edit-match.d.ts +37 -0
- package/dist/tools/builtins/edit-match.d.ts.map +1 -1
- package/dist/tools/builtins/edit-match.js +193 -0
- package/dist/tools/builtins/edit-match.js.map +1 -1
- package/dist/tools/builtins/edit.d.ts.map +1 -1
- package/dist/tools/builtins/edit.js +14 -7
- package/dist/tools/builtins/edit.js.map +1 -1
- package/dist/tools/builtins/exit-semantics.d.ts +8 -0
- package/dist/tools/builtins/exit-semantics.d.ts.map +1 -1
- package/dist/tools/builtins/exit-semantics.js +19 -0
- package/dist/tools/builtins/exit-semantics.js.map +1 -1
- package/dist/tools/builtins/index.d.ts +2 -0
- package/dist/tools/builtins/index.d.ts.map +1 -1
- package/dist/tools/builtins/index.js +1 -0
- package/dist/tools/builtins/index.js.map +1 -1
- package/dist/tools/builtins/present-file.d.ts +24 -0
- package/dist/tools/builtins/present-file.d.ts.map +1 -0
- package/dist/tools/builtins/present-file.js +116 -0
- package/dist/tools/builtins/present-file.js.map +1 -0
- package/dist/tools/builtins/read-file.js +1 -1
- package/dist/tools/builtins/read-file.js.map +1 -1
- package/dist/tools/builtins/read-render.d.ts +12 -1
- package/dist/tools/builtins/read-render.d.ts.map +1 -1
- package/dist/tools/builtins/read-render.js +16 -3
- package/dist/tools/builtins/read-render.js.map +1 -1
- package/dist/tools/builtins/search-tools.d.ts +10 -0
- package/dist/tools/builtins/search-tools.d.ts.map +1 -1
- package/dist/tools/builtins/search-tools.js +36 -2
- package/dist/tools/builtins/search-tools.js.map +1 -1
- package/dist/tools/builtins/wait-for-job.d.ts.map +1 -1
- package/dist/tools/builtins/wait-for-job.js +5 -0
- package/dist/tools/builtins/wait-for-job.js.map +1 -1
- package/dist/tools/builtins/web.d.ts.map +1 -1
- package/dist/tools/builtins/web.js +13 -2
- package/dist/tools/builtins/web.js.map +1 -1
- package/dist/tools/task/create.js +2 -2
- package/dist/tools/task/create.js.map +1 -1
- package/dist/tools/task/update.js +1 -1
- package/dist/tools/task/update.js.map +1 -1
- package/dist/types/provider/chat.d.ts +8 -0
- package/dist/types/provider/chat.d.ts.map +1 -1
- package/dist/types/provider/interface.d.ts +10 -0
- package/dist/types/provider/interface.d.ts.map +1 -1
- package/dist/types/provider/model.d.ts +6 -0
- package/dist/types/provider/model.d.ts.map +1 -1
- package/dist/types/provider/stream.d.ts +2 -0
- package/dist/types/provider/stream.d.ts.map +1 -1
- package/dist/types/tool/presentation.d.ts +11 -0
- package/dist/types/tool/presentation.d.ts.map +1 -1
- package/dist/utils/node-on-path.d.ts +27 -0
- package/dist/utils/node-on-path.d.ts.map +1 -0
- package/dist/utils/node-on-path.js +70 -0
- package/dist/utils/node-on-path.js.map +1 -0
- package/package.json +1 -1
- package/src/bridge/acp/server.ts +25 -0
- package/src/compaction/context-window.ts +18 -0
- package/src/compaction/loaded-skills.ts +42 -1
- package/src/compaction/manager.ts +28 -0
- package/src/compaction/manual.ts +9 -1
- package/src/compaction/model-summary.ts +2 -2
- package/src/compaction/restore.ts +69 -0
- package/src/compaction/salience/working-set.ts +6 -2
- package/src/compaction/tool-result-editing.ts +8 -1
- package/src/compaction/types.ts +2 -0
- package/src/compaction/wire.ts +4 -0
- package/src/manager/session/turn-recorder.ts +24 -1
- package/src/network/join.ts +17 -0
- package/src/network/service.ts +185 -12
- package/src/network/store.ts +28 -1
- package/src/network/transport.ts +1 -1
- package/src/network/wire.ts +13 -1
- package/src/prompt/coding-agent-doctrine.ts +7 -2
- package/src/provider/fallback.ts +6 -0
- package/src/provider/idle-timeout.ts +6 -0
- package/src/provider/instrumentation.ts +12 -0
- package/src/provider/model-window-suffix.ts +1 -0
- package/src/provider/retry.ts +6 -0
- package/src/public-runtime.ts +1 -0
- package/src/public-tools.ts +9 -0
- package/src/public-types.ts +1 -0
- package/src/registry/tool/presentation.ts +14 -4
- package/src/runtime/jobs/registry.ts +2 -1
- package/src/runtime/query/auto-review/classifier.ts +19 -3
- package/src/runtime/query/auto-review/failure.ts +50 -0
- package/src/runtime/query/auto-review/index.ts +1 -0
- package/src/runtime/query/file-evidence-replay.ts +6 -5
- package/src/runtime/query/index.ts +19 -0
- package/src/runtime/query/iteration/index.ts +2 -0
- package/src/runtime/query/iteration/phases/compaction.ts +25 -2
- package/src/runtime/query/iteration/phases/context.ts +4 -0
- package/src/runtime/query/iteration/phases/per-call-review.ts +24 -7
- package/src/runtime/query/iteration/step-shaping.ts +4 -0
- package/src/runtime/query/prepare-turn.ts +20 -2
- package/src/runtime/query/review-policy.ts +24 -4
- package/src/runtime/query/turn/outcome-ledger.ts +45 -1
- package/src/sandbox/provider/local.ts +3 -1
- package/src/tools/admit-facts.ts +18 -0
- package/src/tools/builtins/bash.ts +5 -2
- package/src/tools/builtins/browser.ts +1 -1
- package/src/tools/builtins/computer-use.ts +123 -4
- package/src/tools/builtins/content-fingerprint.ts +2 -1
- package/src/tools/builtins/edit-apply.ts +124 -24
- package/src/tools/builtins/edit-match.ts +200 -0
- package/src/tools/builtins/edit.ts +16 -7
- package/src/tools/builtins/exit-semantics.ts +23 -0
- package/src/tools/builtins/index.ts +2 -0
- package/src/tools/builtins/present-file.ts +127 -0
- package/src/tools/builtins/read-file.ts +1 -1
- package/src/tools/builtins/read-render.ts +18 -3
- package/src/tools/builtins/search-tools.ts +36 -2
- package/src/tools/builtins/wait-for-job.ts +5 -0
- package/src/tools/builtins/web.ts +15 -2
- package/src/tools/task/create.ts +2 -2
- package/src/tools/task/update.ts +1 -1
- package/src/types/provider/chat.ts +9 -0
- package/src/types/provider/interface.ts +11 -0
- package/src/types/provider/model.ts +6 -0
- package/src/types/provider/stream.ts +2 -0
- package/src/types/tool/presentation.ts +12 -0
- package/src/utils/node-on-path.ts +81 -0
|
@@ -9,6 +9,7 @@ import { cancelCauseOf } from '../../types/session/cancel-cause.js'
|
|
|
9
9
|
import type { ToolFacts } from '../../types/tool/presentation.js'
|
|
10
10
|
import { subscribeToAbort } from '../../utils/abort.js'
|
|
11
11
|
import { readPositiveIntEnv } from '../../utils/env.js'
|
|
12
|
+
import { withNodeOnPath } from '../../utils/node-on-path.js'
|
|
12
13
|
import {
|
|
13
14
|
type CommandShell,
|
|
14
15
|
WINDOWS_BASH_GUIDANCE,
|
|
@@ -31,7 +32,7 @@ import {
|
|
|
31
32
|
recordDirectory,
|
|
32
33
|
withCwdCapture,
|
|
33
34
|
} from './bash-cwd.js'
|
|
34
|
-
import { judgeExit } from './exit-semantics.js'
|
|
35
|
+
import { commandNotFoundHint, judgeExit } from './exit-semantics.js'
|
|
35
36
|
import { jsonStringEscapes } from './json-string-hint.js'
|
|
36
37
|
import { creditShellReads } from './shell-read-credit.js'
|
|
37
38
|
import { leadingLongSleep, longSleepRefusal } from './sleep-guard.js'
|
|
@@ -812,6 +813,7 @@ const BashToolBase = defineTool({
|
|
|
812
813
|
? `[${clipped.join(' and ')} was truncated by the sandbox output cap. The omitted output is unavailable in this result. Use a saved artifact or a read-only observation; do not repeat a state-changing action to recover its output.]`
|
|
813
814
|
: '',
|
|
814
815
|
result.attribution ? `[${result.attribution.message}]` : '',
|
|
816
|
+
commandNotFoundHint(result.exitCode, result.stderr) ?? '',
|
|
815
817
|
]
|
|
816
818
|
.filter(Boolean)
|
|
817
819
|
.join('\n\n')
|
|
@@ -909,7 +911,7 @@ const BashToolBase = defineTool({
|
|
|
909
911
|
{
|
|
910
912
|
cwd: startDirectory,
|
|
911
913
|
timeout: input.timeout,
|
|
912
|
-
env: { ...inherited.env, ...context.env },
|
|
914
|
+
env: withNodeOnPath({ ...inherited.env, ...context.env }),
|
|
913
915
|
maxBuffer: DEFAULT_BASH_MAX_BUFFER_BYTES,
|
|
914
916
|
signal: context.abortSignal,
|
|
915
917
|
onOutput,
|
|
@@ -1024,6 +1026,7 @@ const BashToolBase = defineTool({
|
|
|
1024
1026
|
clipped.length > 0
|
|
1025
1027
|
? `[${clipped.join(' and ')} ${clipped.length === 1 ? 'was' : 'were'} truncated by the host output cap. The omitted output is unavailable in this result.]`
|
|
1026
1028
|
: '',
|
|
1029
|
+
commandNotFoundHint(exitCode, failure.stderr ?? '') ?? '',
|
|
1027
1030
|
]
|
|
1028
1031
|
.filter(Boolean)
|
|
1029
1032
|
.join('\n\n')
|
|
@@ -1039,7 +1039,7 @@ function unavailableLine(caps: BrowserCapabilities): string[] {
|
|
|
1039
1039
|
|
|
1040
1040
|
function observeDescription(caps: BrowserCapabilities): string {
|
|
1041
1041
|
return [
|
|
1042
|
-
`Drives a web browser${caps.headless ? ' with no visible window' : ''}: open pages and read them. Change pages with browser_act.`,
|
|
1042
|
+
`Drives a web browser${caps.headless ? ' with no visible window' : ''}: open pages that need a real browser (a sign-in, a page drawn by script, a page you must click through) and read them. To read what a public page says, such as an article or documentation, try web_fetch first when you have it. Change pages with browser_act.`,
|
|
1043
1043
|
'navigate, back, forward and reload return a snapshot of the page when it has something to read, so do not snapshot again straight after. Work from snapshots: "snapshot" returns the page as an accessibility tree, each element with [ref=eN] and, for a control, what it holds (a field\'s text, a select\'s chosen option), under a header "Page: <origin> — <title> (tab tN)". Refs belong to the latest snapshot only; take a new one after the page changes.',
|
|
1044
1044
|
'A full snapshot of a big page is costly, so read less when you can: snapshot with view "delta" (what changed since the last one), view "interactive" (only what can be clicked or typed into), view "reading" (the page text, for an article), or query and role to find an element by name or text (also reaches content a menu or long page hides). "value" with a ref reads one control\'s current value without a snapshot; "wait_for" with stable waits for a page that is still loading.',
|
|
1045
1045
|
'A page you wrote inside the work folder opens by its path (navigate url); to check how it looks, open it and take a screenshot.',
|
|
@@ -530,6 +530,50 @@ const SCREEN_OBSERVATIONS = new Set<string>([
|
|
|
530
530
|
const TERMINAL_APPS =
|
|
531
531
|
/^(WindowsTerminal|OpenConsole|conhost|cmd|powershell|pwsh|wsl|mintty|wezterm(-gui)?|alacritty|Hyper|Tabby|kitty|ConEmu(64)?|putty|Terminal|iTerm2?|gnome-terminal(-server)?|konsole|xterm|xfce4-terminal|tilix|terminator|foot|ghostty|Warp)$/i
|
|
532
532
|
|
|
533
|
+
/**
|
|
534
|
+
* Browser windows, by the process name a host reports in `WindowInfo.app` (on
|
|
535
|
+
* Windows without `.exe`). The tool only looks at these: a page is worked
|
|
536
|
+
* through the browser tool.
|
|
537
|
+
*/
|
|
538
|
+
const BROWSER_APPS =
|
|
539
|
+
/^(chrome|google-chrome(-stable|-beta|-unstable)?|chromium(-browser)?|msedge|microsoft-edge(-stable|-beta|-dev)?|firefox(-esr|-bin)?|brave(-browser)?|opera|vivaldi(-stable)?|arc-browser|safari|librewolf|waterfox|zen-browser|navigator|tor browser|iexplore|duckduckgo|yandex-?browser|thorium)$/i
|
|
540
|
+
|
|
541
|
+
/**
|
|
542
|
+
* Editors and development environments, by process name. Like a terminal,
|
|
543
|
+
* the tool may click in these but does not type or press keys in them: the
|
|
544
|
+
* shell and the edit tools do that work without a screen.
|
|
545
|
+
*/
|
|
546
|
+
const IDE_APPS =
|
|
547
|
+
/^(code(-insiders|-oss)?|cursor|windsurf|zed|devenv|idea(64)?|pycharm(64)?|webstorm(64)?|goland(64)?|clion(64)?|rider(64)?|phpstorm(64)?|rubymine(64)?|datagrip(64)?|studio64|android-studio|jetbrains-[a-z-]+|sublime_text|subl|atom-editor|eclipse|netbeans(64)?|xcode|nova-editor|vscodium|codium|kate|gedit|vim|gvim|nvim-qt|emacs|lapce|jetbrains-fleet)$/i
|
|
548
|
+
|
|
549
|
+
/** How far the tool works in an application: only looks, looks and clicks, or everything. */
|
|
550
|
+
export type AppTier = 'view' | 'click' | 'full'
|
|
551
|
+
|
|
552
|
+
/** The tier of an application by the name a host reports for its window. */
|
|
553
|
+
export function appTier(app: string): AppTier {
|
|
554
|
+
const name = app
|
|
555
|
+
.trim()
|
|
556
|
+
.replace(/\.exe$/i, '')
|
|
557
|
+
.replace(/^.*[\\/]/, '')
|
|
558
|
+
if (BROWSER_APPS.test(name)) return 'view'
|
|
559
|
+
if (TERMINAL_APPS.test(name) || IDE_APPS.test(name)) return 'click'
|
|
560
|
+
return 'full'
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
/** Keys that only move a page: allowed in a view-only application. */
|
|
564
|
+
const SCROLL_KEYS = /^(page_?up|page_?down|home|end|arrow_?(up|down|left|right)|up|down|space)$/i
|
|
565
|
+
|
|
566
|
+
function onlyScrollKeys(keys: unknown): boolean {
|
|
567
|
+
if (typeof keys !== 'string') return false
|
|
568
|
+
const parts = keys.split(/[\s+]+/).filter(Boolean)
|
|
569
|
+
return parts.length > 0 && parts.every((part) => SCROLL_KEYS.test(part))
|
|
570
|
+
}
|
|
571
|
+
|
|
572
|
+
/** Actions that type: refused in a click-only application. */
|
|
573
|
+
const TYPING_ACTIONS = new Set<string>(['type_text', 'key'])
|
|
574
|
+
/** Actions that press or drive a control: refused in a view-only application too. */
|
|
575
|
+
const POINTING_ACTIONS = new Set<string>(['mouse_click', 'mouse_drag', 'ui_act', 'ui_do'])
|
|
576
|
+
|
|
533
577
|
/** Actions a batch cannot carry: the ones that return an image or a tree, and batch itself. */
|
|
534
578
|
const OUTSIDE_BATCH = new Set<string>(['screenshot', 'zoom', 'ui_snapshot', 'wait_for', 'batch'])
|
|
535
579
|
|
|
@@ -724,6 +768,7 @@ function buildDescription(
|
|
|
724
768
|
lines.push(
|
|
725
769
|
'Coordinates: every x/y you send is a pixel of the most recent screenshot this tool returned — origin at its top-left, x to the right, y down — never a screen pixel. Each screenshot states its id and size (for example "s3: 1456x819"); stay inside it. Display screenshots map onto the display. To aim at an earlier display screenshot, pass its id as screenshot_id.',
|
|
726
770
|
'Take a screenshot before your first click on pixels.',
|
|
771
|
+
"Look at the screen before you say what state an application is in. Never click links with computer use: a page is the browser tool's. Prefer a connected tool or a command line to the screen when one does the job.",
|
|
727
772
|
)
|
|
728
773
|
if (settings.screenshotAfterActions)
|
|
729
774
|
lines.push(
|
|
@@ -755,7 +800,7 @@ function buildDescription(
|
|
|
755
800
|
)
|
|
756
801
|
if (windowCaptureAvailable(host, caps))
|
|
757
802
|
lines.push(
|
|
758
|
-
`When the user names an application or window, ${available.includes('list_windows') ? 'use list_windows then screenshot {window_id}' : 'use screenshot {window_id} with a host-provided window id'} to capture pixels for that window. Its x/y are pixels of that window image; clicks, drags, typing and keys target that window. Window pointer input is sent in the background first. If a click or drag is refused, inspect a fresh image before any other action; where the host can use the screen it may repeat a refused click once on the screen, and the result says so. ${caps.windowScroll === false ? `Window pixel scrolling is unavailable on this host;
|
|
803
|
+
`When the user names an application or window, ${available.includes('list_windows') ? 'use list_windows then screenshot {window_id}' : 'use screenshot {window_id} with a host-provided window id'} to capture pixels for that window. Its x/y are pixels of that window image; clicks, drags, typing and keys target that window. Window pointer input is sent in the background first. If a click or drag is refused, inspect a fresh image before any other action; where the host can use the screen it may repeat a refused click once on the screen, and the result says so. ${caps.windowScroll === false ? `Window pixel scrolling is unavailable on this host; ${hostActionAvailable(caps, 'screenshot') ? 'deliberately take a display screenshot for display scrolling' : 'scroll by another means'}. ` : ''}Foreground text may briefly bring the verified window to the front: the host waits until the user has been still for a moment, puts their window back afterwards, and reports it as not delivered (safe to try again later) when the screen stays busy. ${host.beginTurn ? 'A result that says the user pressed Escape means stop: end your turn and say what is left undone. ' : ''}A browser window is only looked at: act on a page with the browser tool. A stale or moved window is refused; take a fresh window screenshot. ${hostActionAvailable(caps, 'screenshot') ? 'Plain screenshot still shows the display. ' : 'screenshot requires window_id on this host. '}Browser permission bubbles outside the window may be absent. If the window image is blank or shows another window, do not click it; focus the target and capture it again.`,
|
|
759
804
|
)
|
|
760
805
|
if (available.includes('ui_snapshot'))
|
|
761
806
|
lines.push(
|
|
@@ -1858,7 +1903,7 @@ export function createComputerUseTool(
|
|
|
1858
1903
|
)
|
|
1859
1904
|
if (frame.window && caps.windowScroll === false)
|
|
1860
1905
|
throw new StepFailure(
|
|
1861
|
-
'window pixel scrolling is unavailable on this host;
|
|
1906
|
+
'window pixel scrolling is unavailable on this host; take a deliberate display screenshot before display scrolling, or use the browser tool for a page',
|
|
1862
1907
|
)
|
|
1863
1908
|
const at = mapPoint(frame, item.at, 'at')
|
|
1864
1909
|
return step(() =>
|
|
@@ -1883,7 +1928,9 @@ export function createComputerUseTool(
|
|
|
1883
1928
|
if (item.delivery_mode && !frame.window)
|
|
1884
1929
|
throw new StepFailure('type_text delivery_mode requires a window screenshot')
|
|
1885
1930
|
if (frame.window && TERMINAL_APPS.test(frame.window.window.app))
|
|
1886
|
-
throw new StepFailure(
|
|
1931
|
+
throw new StepFailure(
|
|
1932
|
+
'computer_use never types into a terminal window; use the shell tool for a command',
|
|
1933
|
+
)
|
|
1887
1934
|
return step(() =>
|
|
1888
1935
|
runHost(
|
|
1889
1936
|
{
|
|
@@ -1907,7 +1954,9 @@ export function createComputerUseTool(
|
|
|
1907
1954
|
'key delivery_mode requires a window screenshot; take one with screenshot {window_id}',
|
|
1908
1955
|
)
|
|
1909
1956
|
if (frame.window && TERMINAL_APPS.test(frame.window.window.app))
|
|
1910
|
-
throw new StepFailure(
|
|
1957
|
+
throw new StepFailure(
|
|
1958
|
+
'computer_use never sends keys to a terminal window; use the shell tool for a command',
|
|
1959
|
+
)
|
|
1911
1960
|
return step(() =>
|
|
1912
1961
|
runHost(
|
|
1913
1962
|
{
|
|
@@ -2390,6 +2439,14 @@ export function createComputerUseTool(
|
|
|
2390
2439
|
)
|
|
2391
2440
|
}
|
|
2392
2441
|
const front = windows.find((window) => window.focused)
|
|
2442
|
+
if (front && appTier(front.app) === 'view')
|
|
2443
|
+
throw new StepFailure(
|
|
2444
|
+
`a browser is in front (${front.app}, window ${front.id}), so nothing was typed: computer_use only looks at a browser. Use the browser tool to act on a page, or bring the window you mean to the front (focus_window, or click it) and try again`,
|
|
2445
|
+
)
|
|
2446
|
+
if (front && IDE_APPS.test(front.app.trim().replace(/\.exe$/i, '')))
|
|
2447
|
+
throw new StepFailure(
|
|
2448
|
+
`an editor is in front (${front.app}, window ${front.id}), so nothing was typed: computer_use clicks in an editor but does not type there. Use the edit tools for a file and the shell tool for a command, or bring the window you mean to the front (focus_window, or click it) and try again`,
|
|
2449
|
+
)
|
|
2393
2450
|
if (front && TERMINAL_APPS.test(front.app))
|
|
2394
2451
|
throw new StepFailure(
|
|
2395
2452
|
`a terminal is in front (${front.app}, window ${front.id}), so nothing was typed: computer_use never sends keys or text to a terminal. Bring the window you mean to the front (focus_window, or click it) and try again; for a command, use a shell tool instead`,
|
|
@@ -3106,6 +3163,66 @@ export function createComputerUseTool(
|
|
|
3106
3163
|
return `computer_use does not act in ${applicationName(app)}: the person put it off limits. Nothing was done. Use another application, or tell the person you need it and let them change the list.`
|
|
3107
3164
|
}
|
|
3108
3165
|
|
|
3166
|
+
/**
|
|
3167
|
+
* Why this call may not run because of the kind of application it would act in, or undefined:
|
|
3168
|
+
* a browser window is only looked at, a terminal or an editor is clicked but not typed into.
|
|
3169
|
+
* It places the call from what the tool already knows (a window screenshot, the last window list)
|
|
3170
|
+
* and never reads the host for it: acting must not cost a call it would not make. A call it
|
|
3171
|
+
* cannot place is left to the checks that follow, and a terminal's typing is refused by them.
|
|
3172
|
+
* The person's denied list is checked first and stands on top of this.
|
|
3173
|
+
*/
|
|
3174
|
+
const appTierRefusal = async (
|
|
3175
|
+
input: ActionInput,
|
|
3176
|
+
context: ToolContext,
|
|
3177
|
+
): Promise<string | undefined> => {
|
|
3178
|
+
if (isReadOnlyInput(input)) return undefined
|
|
3179
|
+
// A call with no window of its own is placed by the focused window or by bounds: read a fresh list.
|
|
3180
|
+
const loose0: unknown = input
|
|
3181
|
+
const shot0 =
|
|
3182
|
+
isRecord(loose0) && typeof loose0.screenshot_id === 'string'
|
|
3183
|
+
? loose0.screenshot_id
|
|
3184
|
+
: undefined
|
|
3185
|
+
const unbound = (batchActions(input) ?? [input]).some((item) => {
|
|
3186
|
+
if (!isRecord(item) || boundWindowId(item) !== undefined) return false
|
|
3187
|
+
if (!TYPING_ACTIONS.has(String(item.type)) && !POINTING_ACTIONS.has(String(item.type)))
|
|
3188
|
+
return false
|
|
3189
|
+
const id = typeof item.screenshot_id === 'string' ? item.screenshot_id : shot0
|
|
3190
|
+
const frame = id ? frames.get(id) : frames.latest()
|
|
3191
|
+
return !frame?.window
|
|
3192
|
+
})
|
|
3193
|
+
if (unbound && typeof host.listWindows === 'function') {
|
|
3194
|
+
try {
|
|
3195
|
+
rememberWindows(await host.listWindows(context.abortSignal))
|
|
3196
|
+
} catch {
|
|
3197
|
+
// The checks that follow read the front window before typing.
|
|
3198
|
+
}
|
|
3199
|
+
}
|
|
3200
|
+
const loose: unknown = input
|
|
3201
|
+
const shotId =
|
|
3202
|
+
isRecord(loose) && typeof loose.screenshot_id === 'string' ? loose.screenshot_id : undefined
|
|
3203
|
+
const withShot = (item: Record<string, unknown>): Record<string, unknown> =>
|
|
3204
|
+
shotId && item.screenshot_id === undefined ? { ...item, screenshot_id: shotId } : item
|
|
3205
|
+
for (const item of batchActions(input) ?? [input]) {
|
|
3206
|
+
if (!isRecord(item)) continue
|
|
3207
|
+
const type = String(item.type)
|
|
3208
|
+
const setsValue =
|
|
3209
|
+
(type === 'ui_act' || type === 'ui_do') &&
|
|
3210
|
+
(item.action === 'set_value' || typeof item.value === 'string')
|
|
3211
|
+
if (!TYPING_ACTIONS.has(type) && !POINTING_ACTIONS.has(type)) continue
|
|
3212
|
+
const placed = appsKnown(withShot(item))
|
|
3213
|
+
for (const app of placed) {
|
|
3214
|
+
const tier = appTier(app)
|
|
3215
|
+
const name = applicationName(app)
|
|
3216
|
+
if (tier === 'view' && type === 'key' && onlyScrollKeys(item.keys)) continue
|
|
3217
|
+
if (tier === 'view')
|
|
3218
|
+
return `computer_use only looks at ${name}, a browser: it does not click, type or press keys there. Use the browser tool to act on a page; take a screenshot to see what the window shows. Nothing was done.`
|
|
3219
|
+
if (tier === 'click' && !TERMINAL_APPS.test(app) && (TYPING_ACTIONS.has(type) || setsValue))
|
|
3220
|
+
return `computer_use clicks in ${name} but does not type or press keys there. Use the shell tool for a command and the edit tools for a file. Nothing was done.`
|
|
3221
|
+
}
|
|
3222
|
+
}
|
|
3223
|
+
return undefined
|
|
3224
|
+
}
|
|
3225
|
+
|
|
3109
3226
|
const runAction = async (input: ActionInput, context: ToolContext): Promise<ToolResult> => {
|
|
3110
3227
|
const denied = refusal(input.type)
|
|
3111
3228
|
if (denied) return { success: false, output: '', error: denied }
|
|
@@ -3190,6 +3307,8 @@ export function createComputerUseTool(
|
|
|
3190
3307
|
async execute(input, context): Promise<ToolResult> {
|
|
3191
3308
|
const keptOut = await deniedAppRefusal(input, context)
|
|
3192
3309
|
if (keptOut) return { success: false, output: '', error: keptOut }
|
|
3310
|
+
const tiered = await appTierRefusal(input, context)
|
|
3311
|
+
if (tiered) return { success: false, output: '', error: tiered }
|
|
3193
3312
|
const shotId = 'screenshot_id' in input ? input.screenshot_id : undefined
|
|
3194
3313
|
const before = shotId ? frames.get(shotId) : frames.latest()
|
|
3195
3314
|
// Only what is already known: acting must not cost the host a call it would not make.
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { createHash } from 'node:crypto'
|
|
2
|
+
import { linePrefix } from './read-render.js'
|
|
2
3
|
|
|
3
4
|
/**
|
|
4
5
|
* A short, stable fingerprint of a file body.
|
|
@@ -50,7 +51,7 @@ export function unreadEditShownError(path: string, content: string): string | un
|
|
|
50
51
|
const lines = content.split('\n')
|
|
51
52
|
if (lines.length > 1 && lines[lines.length - 1] === '') lines.pop()
|
|
52
53
|
if (lines.length > UNREAD_EDIT_SHOWN_MAX_LINES) return undefined
|
|
53
|
-
const numbered = lines.map((line, i) => `${i + 1}
|
|
54
|
+
const numbered = lines.map((line, i) => `${linePrefix(i + 1)}${line}`).join('\n')
|
|
54
55
|
return `${path} had not been read in this session, so this edit was not applied. It has now been read for you; its current contents follow. Repeat the edit against them, without reading the file again. Nothing was written.\n\n${numbered}`
|
|
55
56
|
}
|
|
56
57
|
|
|
@@ -1,4 +1,11 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
describeIndent,
|
|
3
|
+
indentedMatchStarts,
|
|
4
|
+
reconcileNewString,
|
|
5
|
+
resolveIndentedMatch,
|
|
6
|
+
resolveOldString,
|
|
7
|
+
showWhitespace,
|
|
8
|
+
} from './edit-match.js'
|
|
2
9
|
import type { EditInput } from './edit.js'
|
|
3
10
|
|
|
4
11
|
/**
|
|
@@ -156,30 +163,50 @@ export type EditApplyFailure = {
|
|
|
156
163
|
export function applyEdit(
|
|
157
164
|
content: string,
|
|
158
165
|
operations: readonly NormalizedEditInput[],
|
|
159
|
-
): { success: true; content: string; replacements: number } | EditApplyFailure {
|
|
166
|
+
): { success: true; content: string; replacements: number; notes: string[] } | EditApplyFailure {
|
|
160
167
|
let current = content
|
|
161
168
|
let replacements = 0
|
|
162
|
-
|
|
169
|
+
const notes: string[] = []
|
|
170
|
+
const failures: string[] = []
|
|
171
|
+
let reason: EditApplyFailure['reason']
|
|
172
|
+
|
|
173
|
+
// Every hunk is tried, so one reply names every one that does not apply
|
|
174
|
+
// rather than the first. A failed hunk leaves the content as it was, so
|
|
175
|
+
// the ones after it are judged against the file without it; the write
|
|
176
|
+
// stays all-or-nothing either way.
|
|
163
177
|
for (const [index, operation] of operations.entries()) {
|
|
164
|
-
const result = applyOne(current, operation)
|
|
178
|
+
const result = applyOne(current, operation, index < FULL_EXCERPT_HUNKS)
|
|
165
179
|
if (!result.success) {
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
...(result.reason ? { reason: result.reason } : {}),
|
|
170
|
-
}
|
|
180
|
+
failures.push(framedFailure(result.error, index, operations.length))
|
|
181
|
+
reason ??= result.reason
|
|
182
|
+
continue
|
|
171
183
|
}
|
|
172
184
|
current = result.content
|
|
173
185
|
replacements += result.replacements
|
|
186
|
+
if (result.note !== undefined) {
|
|
187
|
+
notes.push(operations.length === 1 ? result.note : `edits[${index}]: ${result.note}`)
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
if (failures.length > 0) {
|
|
192
|
+
return {
|
|
193
|
+
success: false,
|
|
194
|
+
error:
|
|
195
|
+
operations.length === 1
|
|
196
|
+
? (failures[0] as string)
|
|
197
|
+
: `${capBatchError(failures.join('\n'))}\nNothing was written — the whole batch is refused, so the file is exactly as it was.`,
|
|
198
|
+
...(reason ? { reason } : {}),
|
|
199
|
+
}
|
|
174
200
|
}
|
|
175
201
|
|
|
176
|
-
return { success: true, content: current, replacements }
|
|
202
|
+
return { success: true, content: current, replacements, notes }
|
|
177
203
|
}
|
|
178
204
|
|
|
179
205
|
function applyOne(
|
|
180
206
|
content: string,
|
|
181
207
|
input: NormalizedEditInput,
|
|
182
|
-
|
|
208
|
+
fullExcerpt = true,
|
|
209
|
+
): { success: true; content: string; replacements: number; note?: string } | EditApplyFailure {
|
|
183
210
|
if (input.operation === 'insert') {
|
|
184
211
|
return applyLineInsert(content, input)
|
|
185
212
|
}
|
|
@@ -194,22 +221,30 @@ function applyOne(
|
|
|
194
221
|
// replacement so it fits where it lands. See `edit-match.ts`.
|
|
195
222
|
let oldString = replacement.oldString
|
|
196
223
|
let newString = replacement.newString
|
|
224
|
+
let note: string | undefined
|
|
197
225
|
if (!content.includes(oldString)) {
|
|
198
226
|
const resolved = resolveOldString(content, oldString)
|
|
199
|
-
if (resolved
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
227
|
+
if (resolved !== null) {
|
|
228
|
+
newString = reconcileNewString(oldString, resolved, newString)
|
|
229
|
+
oldString = resolved
|
|
230
|
+
} else {
|
|
231
|
+
// Last resort, and only for one place: the same lines with different
|
|
232
|
+
// leading whitespace. `replace_all` names no single place, so it never
|
|
233
|
+
// takes this road.
|
|
234
|
+
const indented = replacement.replace_all
|
|
235
|
+
? null
|
|
236
|
+
: resolveIndentedMatch(content, oldString, newString)
|
|
237
|
+
if (indented === null) {
|
|
238
|
+
return {
|
|
239
|
+
success: false,
|
|
240
|
+
reason: 'text_not_found',
|
|
241
|
+
error: notFoundError(content, oldString, fullExcerpt),
|
|
242
|
+
}
|
|
209
243
|
}
|
|
244
|
+
oldString = indented.span
|
|
245
|
+
newString = indented.newString
|
|
246
|
+
note = INDENT_NOTE
|
|
210
247
|
}
|
|
211
|
-
newString = reconcileNewString(oldString, resolved, newString)
|
|
212
|
-
oldString = resolved
|
|
213
248
|
}
|
|
214
249
|
|
|
215
250
|
// A deletion takes the line's trailing newline with it, so removing a whole
|
|
@@ -248,9 +283,68 @@ function applyOne(
|
|
|
248
283
|
content:
|
|
249
284
|
content.slice(0, firstIndex) + newString + content.slice(firstIndex + oldString.length),
|
|
250
285
|
replacements: 1,
|
|
286
|
+
...(note === undefined ? {} : { note }),
|
|
251
287
|
}
|
|
252
288
|
}
|
|
253
289
|
|
|
290
|
+
/** Said once when an edit applied although the indentation of old_string was not the file's. */
|
|
291
|
+
const INDENT_NOTE =
|
|
292
|
+
'matched with adjusted indentation (old_string was indented differently from the file; new_string was re-indented to fit). Copy leading whitespace as it is after the arrow in read output.'
|
|
293
|
+
|
|
294
|
+
const CONTEXT_LINES = 12
|
|
295
|
+
/** Failing hunks of one batch that get the file's lines; the rest get index, reason and line number. */
|
|
296
|
+
const FULL_EXCERPT_HUNKS = 3
|
|
297
|
+
/** The most characters one batch's failure text may run to. */
|
|
298
|
+
const BATCH_ERROR_MAX_CHARS = 6000
|
|
299
|
+
|
|
300
|
+
function capBatchError(text: string): string {
|
|
301
|
+
if (text.length <= BATCH_ERROR_MAX_CHARS) return text
|
|
302
|
+
return `${text.slice(0, BATCH_ERROR_MAX_CHARS)}…\n(more failing hunks follow; fix these and run again)`
|
|
303
|
+
}
|
|
304
|
+
const CONTEXT_LINE_MAX_CHARS = 160
|
|
305
|
+
|
|
306
|
+
/**
|
|
307
|
+
* The not-found refusal. When a line of the file matches the first line of
|
|
308
|
+
* old_string apart from spacing, the file's own lines there are shown with
|
|
309
|
+
* their whitespace made visible, so the model can copy them without a read.
|
|
310
|
+
*/
|
|
311
|
+
function notFoundError(content: string, oldString: string, fullExcerpt = true): string {
|
|
312
|
+
const head =
|
|
313
|
+
'old_string/oldStr not found in file. Make sure the string matches exactly, including whitespace and indentation.'
|
|
314
|
+
const near = nearLine(content, oldString)
|
|
315
|
+
const starts = indentedMatchStarts(content, oldString)
|
|
316
|
+
if (starts.length > 1) {
|
|
317
|
+
const where = starts
|
|
318
|
+
.slice(0, 5)
|
|
319
|
+
.map((at) => at + 1)
|
|
320
|
+
.join(', ')
|
|
321
|
+
return `${head} Its lines appear in ${starts.length} places once indentation is ignored (starting at lines ${where}), so none was chosen; include more surrounding lines, or copy the indentation of the one you mean exactly.`
|
|
322
|
+
}
|
|
323
|
+
if (near === undefined) {
|
|
324
|
+
return `${head} Read the lines you mean to change again and copy them as they are.`
|
|
325
|
+
}
|
|
326
|
+
if (!fullExcerpt) {
|
|
327
|
+
return `${head} A line that matches its first line apart from spacing is at line ${near}; read around it and copy it as it is.`
|
|
328
|
+
}
|
|
329
|
+
const lines = content.split('\n')
|
|
330
|
+
const wanted = oldString.split('\n')
|
|
331
|
+
const firstWanted = wanted.find((line) => line.trim() !== '') ?? ''
|
|
332
|
+
const count = Math.min(Math.max(wanted.length, 3) + 1, CONTEXT_LINES)
|
|
333
|
+
const from = Math.max(1, near - 1)
|
|
334
|
+
const to = Math.min(lines.length, near - 1 + count)
|
|
335
|
+
const shown: string[] = []
|
|
336
|
+
for (let n = from; n <= to; n++) {
|
|
337
|
+
// The empty piece after a final newline is not a line of the file.
|
|
338
|
+
if (n === lines.length && lines[n - 1] === '') break
|
|
339
|
+
const raw = (lines[n - 1] ?? '').replace(/\r$/, '')
|
|
340
|
+
const visible = showWhitespace(raw)
|
|
341
|
+
shown.push(
|
|
342
|
+
`${String(n).padStart(6)}| ${visible.length > CONTEXT_LINE_MAX_CHARS ? `${visible.slice(0, CONTEXT_LINE_MAX_CHARS)}…` : visible}`,
|
|
343
|
+
)
|
|
344
|
+
}
|
|
345
|
+
return `${head} A line that matches its first line apart from spacing is at line ${near}: the file has ${describeIndent(lines[near - 1] ?? '')} there, your old_string has ${describeIndent(firstWanted)}. The file's lines, with → for a tab and · for a space (the numbers and | are not part of the file):\n${shown.join('\n')}\nCopy the text after the | exactly, turning → back into a tab and · into a space.`
|
|
346
|
+
}
|
|
347
|
+
|
|
254
348
|
const NEAR_LINE_MIN_CHARS = 8
|
|
255
349
|
|
|
256
350
|
/** The line of `content` that matches the first non-blank line of `needle` apart from spacing, 1-based. */
|
|
@@ -336,7 +430,13 @@ function normalizeLineEndings(
|
|
|
336
430
|
/** The batch framing an operation's own error is reported under. */
|
|
337
431
|
function framedError(error: string, index: number, total: number): string {
|
|
338
432
|
if (total === 1) return error
|
|
339
|
-
return
|
|
433
|
+
return `${framedFailure(error, index, total)} Nothing was written — the whole batch is refused, so the file is exactly as it was.`
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
/** One failing hunk of a batch, named by its index. */
|
|
437
|
+
function framedFailure(error: string, index: number, total: number): string {
|
|
438
|
+
if (total === 1) return error
|
|
439
|
+
return `edits[${index}] of ${total}: ${error}`
|
|
340
440
|
}
|
|
341
441
|
|
|
342
442
|
/**
|
|
@@ -271,3 +271,203 @@ export function reconcileNewString(oldString: string, resolved: string, newStrin
|
|
|
271
271
|
if (hasCurlySingle) reconciled = curlifySingleQuotes(reconciled)
|
|
272
272
|
return reconciled
|
|
273
273
|
}
|
|
274
|
+
|
|
275
|
+
/** Spaces a tab stands for when indentation of different styles is compared. */
|
|
276
|
+
const TAB_WIDTHS = [4, 2, 8] as const
|
|
277
|
+
|
|
278
|
+
/** The result of finding `old_string` again by its content alone, ignoring how it was indented. */
|
|
279
|
+
export interface IndentedMatch {
|
|
280
|
+
/** The real span of the file the lines occupy. */
|
|
281
|
+
readonly span: string
|
|
282
|
+
/** `new_string`, re-indented by the difference the match found. */
|
|
283
|
+
readonly newString: string
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
function leadingWhitespace(line: string): string {
|
|
287
|
+
let end = 0
|
|
288
|
+
while (end < line.length && (line[end] === ' ' || line[end] === '\t')) end++
|
|
289
|
+
return line.slice(0, end)
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
function indentWidth(indent: string, tab: number): number {
|
|
293
|
+
let width = 0
|
|
294
|
+
for (const ch of indent) width += ch === '\t' ? tab : 1
|
|
295
|
+
return width
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
/** The smallest space-only indent (2 or more) among the lines, or 0. */
|
|
299
|
+
function spaceIndentUnit(lines: readonly string[]): number {
|
|
300
|
+
let unit = 0
|
|
301
|
+
for (const line of lines) {
|
|
302
|
+
if (line.trim() === '') continue
|
|
303
|
+
const indent = leadingWhitespace(line)
|
|
304
|
+
if (indent.length < 2 || indent.includes('\t')) continue
|
|
305
|
+
if (unit === 0 || indent.length < unit) unit = indent.length
|
|
306
|
+
}
|
|
307
|
+
return unit
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
function isBlank(line: string): boolean {
|
|
311
|
+
return line.trim() === ''
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
/** The text of a line apart from its indentation and trailing whitespace. */
|
|
315
|
+
function lineKey(line: string): string {
|
|
316
|
+
return line.trim()
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
/**
|
|
320
|
+
* Where, 0-based, the lines of `oldString` appear in `content` once leading
|
|
321
|
+
* and trailing whitespace is ignored. More than one entry means the text
|
|
322
|
+
* names several places and needs more context.
|
|
323
|
+
*/
|
|
324
|
+
export function indentedMatchStarts(content: string, oldString: string): number[] {
|
|
325
|
+
const wanted = oldString.split('\n')
|
|
326
|
+
const endsWithNewline = wanted.length > 1 && wanted[wanted.length - 1] === ''
|
|
327
|
+
if (endsWithNewline) wanted.pop()
|
|
328
|
+
if (wanted.every(isBlank)) return []
|
|
329
|
+
const wantedKeys = wanted.map(lineKey)
|
|
330
|
+
const lines = content.split('\n')
|
|
331
|
+
const keys = lines.map(lineKey)
|
|
332
|
+
const first = wantedKeys.findIndex((key) => key !== '')
|
|
333
|
+
const starts: number[] = []
|
|
334
|
+
for (let at = 0; at + wanted.length <= lines.length; at++) {
|
|
335
|
+
// biome-ignore lint/style/noNonNullAssertion: index < length
|
|
336
|
+
if (keys[at + first]! !== wantedKeys[first]!) continue
|
|
337
|
+
let same = true
|
|
338
|
+
for (let i = 0; i < wanted.length; i++) {
|
|
339
|
+
if (keys[at + i] !== wantedKeys[i]) {
|
|
340
|
+
same = false
|
|
341
|
+
break
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
if (same) starts.push(at)
|
|
345
|
+
}
|
|
346
|
+
return starts
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
/**
|
|
350
|
+
* Find `oldString` in `content` by comparing lines with their leading and
|
|
351
|
+
* trailing whitespace ignored, for the case where the model copied the right
|
|
352
|
+
* lines with the wrong indentation (the usual cause: one level short, because
|
|
353
|
+
* the first tab of an indented line was taken for a separator).
|
|
354
|
+
*
|
|
355
|
+
* Applies only when ALL of these hold, and otherwise answers null:
|
|
356
|
+
*
|
|
357
|
+
* - exactly one run of lines in the file matches (a second place is
|
|
358
|
+
* ambiguous, and nothing is guessed);
|
|
359
|
+
* - the indentation differs by the same amount on every non-blank line
|
|
360
|
+
* (the model's text is the file's text shifted, not a different shape);
|
|
361
|
+
* - at least one line is not blank.
|
|
362
|
+
*
|
|
363
|
+
* The shift is measured in columns with a tab counted as 4, 2 or 8 spaces
|
|
364
|
+
* (the first that makes the shift constant), and `newString` is moved by the
|
|
365
|
+
* same number of columns in the file's own style, so a tab-indented file stays
|
|
366
|
+
* tab-indented. Only leading whitespace is ever changed.
|
|
367
|
+
*/
|
|
368
|
+
export function resolveIndentedMatch(
|
|
369
|
+
content: string,
|
|
370
|
+
oldString: string,
|
|
371
|
+
newString: string,
|
|
372
|
+
): IndentedMatch | null {
|
|
373
|
+
const starts = indentedMatchStarts(content, oldString)
|
|
374
|
+
if (starts.length !== 1) return null
|
|
375
|
+
const found = starts[0] as number
|
|
376
|
+
const wanted = oldString.split('\n')
|
|
377
|
+
const endsWithNewline = wanted.length > 1 && wanted[wanted.length - 1] === ''
|
|
378
|
+
if (endsWithNewline) wanted.pop()
|
|
379
|
+
const lines = content.split('\n')
|
|
380
|
+
|
|
381
|
+
// Trailing whitespace on a matched line is compared away but the replaced
|
|
382
|
+
// span keeps it nowhere: new_string has none, so applying would silently
|
|
383
|
+
// delete it (a Markdown hard break, say). Refuse; the not-found error
|
|
384
|
+
// shows the whitespace.
|
|
385
|
+
for (let i = 0; i < wanted.length; i++) {
|
|
386
|
+
// biome-ignore lint/style/noNonNullAssertion: index < length
|
|
387
|
+
const fileLine = lines[found + i]!.replace(/\r$/, '')
|
|
388
|
+
if (fileLine !== fileLine.trimEnd()) return null
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
const pairs: { file: string; model: string }[] = []
|
|
392
|
+
for (let i = 0; i < wanted.length; i++) {
|
|
393
|
+
// biome-ignore lint/style/noNonNullAssertion: index < length
|
|
394
|
+
const model = wanted[i]!
|
|
395
|
+
if (isBlank(model)) continue
|
|
396
|
+
// biome-ignore lint/style/noNonNullAssertion: index < length
|
|
397
|
+
pairs.push({ file: leadingWhitespace(lines[found + i]!), model: leadingWhitespace(model) })
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
let tab = 0
|
|
401
|
+
let delta = 0
|
|
402
|
+
const modelTabs = pairs.some((p) => p.model.includes('\t'))
|
|
403
|
+
const fileTabs = pairs.some((p) => p.file.includes('\t'))
|
|
404
|
+
// A model writing tabs into a space-indented file means the file's own
|
|
405
|
+
// indent unit per tab; guess 4 and a 2-space file gets the wrong nesting.
|
|
406
|
+
const unit = modelTabs && !fileTabs ? spaceIndentUnit(lines) : 0
|
|
407
|
+
const widths = unit > 0 ? [unit, ...TAB_WIDTHS.filter((w) => w !== unit)] : TAB_WIDTHS
|
|
408
|
+
for (const candidate of widths) {
|
|
409
|
+
const [head, ...rest] = pairs
|
|
410
|
+
if (head === undefined) return null
|
|
411
|
+
const shift = indentWidth(head.file, candidate) - indentWidth(head.model, candidate)
|
|
412
|
+
if (
|
|
413
|
+
rest.every((p) => indentWidth(p.file, candidate) - indentWidth(p.model, candidate) === shift)
|
|
414
|
+
) {
|
|
415
|
+
tab = candidate
|
|
416
|
+
delta = shift
|
|
417
|
+
break
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
if (tab === 0) return null
|
|
421
|
+
|
|
422
|
+
const fileUsesTabs = pairs.some((p) => p.file.includes('\t'))
|
|
423
|
+
const render = (width: number): string => {
|
|
424
|
+
if (!fileUsesTabs) return ' '.repeat(width)
|
|
425
|
+
return '\t'.repeat(Math.floor(width / tab)) + ' '.repeat(width % tab)
|
|
426
|
+
}
|
|
427
|
+
const modelUsesTabs = pairs.some((p) => p.model.includes('\t'))
|
|
428
|
+
const shifted =
|
|
429
|
+
delta === 0 && fileUsesTabs === modelUsesTabs
|
|
430
|
+
? newString
|
|
431
|
+
: newString
|
|
432
|
+
.split('\n')
|
|
433
|
+
.map((line) => {
|
|
434
|
+
if (isBlank(line)) return line
|
|
435
|
+
const indent = leadingWhitespace(line)
|
|
436
|
+
const width = Math.max(0, indentWidth(indent, tab) + delta)
|
|
437
|
+
return render(width) + line.slice(indent.length)
|
|
438
|
+
})
|
|
439
|
+
.join('\n')
|
|
440
|
+
|
|
441
|
+
let start = 0
|
|
442
|
+
for (let i = 0; i < found; i++) start += (lines[i] as string).length + 1
|
|
443
|
+
let end = start
|
|
444
|
+
for (let i = 0; i < wanted.length; i++)
|
|
445
|
+
end += (lines[found + i] as string).length + (i < wanted.length - 1 ? 1 : 0)
|
|
446
|
+
if (endsWithNewline) {
|
|
447
|
+
if (content[end] === '\n') end += 1
|
|
448
|
+
} else if (content[end - 1] === '\r') {
|
|
449
|
+
end -= 1
|
|
450
|
+
}
|
|
451
|
+
return { span: content.slice(start, end), newString: shifted }
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
/** How a line's indentation reads, for a message: "2 tabs", "4 spaces", "none". */
|
|
455
|
+
export function describeIndent(line: string): string {
|
|
456
|
+
const indent = leadingWhitespace(line)
|
|
457
|
+
if (indent === '') return 'no indentation'
|
|
458
|
+
const tabs = [...indent].filter((c) => c === '\t').length
|
|
459
|
+
const spaces = indent.length - tabs
|
|
460
|
+
const parts: string[] = []
|
|
461
|
+
if (tabs > 0) parts.push(`${tabs} ${tabs === 1 ? 'tab' : 'tabs'}`)
|
|
462
|
+
if (spaces > 0) parts.push(`${spaces} ${spaces === 1 ? 'space' : 'spaces'}`)
|
|
463
|
+
return parts.join(' and ')
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
/** A line with its indentation and trailing whitespace made visible: `→` is a tab, `·` a space. */
|
|
467
|
+
export function showWhitespace(line: string): string {
|
|
468
|
+
const indent = leadingWhitespace(line)
|
|
469
|
+
const body = line.slice(indent.length).replace(/[ \t\r]+$/, (tail) => {
|
|
470
|
+
return tail.replaceAll('\t', '→').replaceAll(' ', '·').replaceAll('\r', '⏎')
|
|
471
|
+
})
|
|
472
|
+
return indent.replaceAll('\t', '→').replaceAll(' ', '·') + body
|
|
473
|
+
}
|