@bastani/atomic 0.9.2-alpha.1 → 0.9.3-alpha.1
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 +70 -0
- package/README.md +2 -2
- package/dist/builtin/cursor/CHANGELOG.md +6 -0
- package/dist/builtin/cursor/package.json +2 -2
- package/dist/builtin/intercom/CHANGELOG.md +6 -0
- package/dist/builtin/intercom/package.json +1 -1
- package/dist/builtin/mcp/CHANGELOG.md +12 -0
- package/dist/builtin/mcp/direct-tools.ts +4 -2
- package/dist/builtin/mcp/package.json +1 -1
- package/dist/builtin/mcp/proxy-call.ts +3 -1
- package/dist/builtin/mcp/utils.ts +18 -7
- package/dist/builtin/subagents/CHANGELOG.md +17 -0
- package/dist/builtin/subagents/README.md +6 -6
- package/dist/builtin/subagents/agents/code-simplifier.md +7 -6
- package/dist/builtin/subagents/agents/codebase-analyzer.md +5 -4
- package/dist/builtin/subagents/agents/codebase-locator.md +3 -3
- package/dist/builtin/subagents/agents/codebase-online-researcher.md +10 -10
- package/dist/builtin/subagents/agents/codebase-pattern-finder.md +4 -4
- package/dist/builtin/subagents/agents/codebase-research-analyzer.md +3 -3
- package/dist/builtin/subagents/agents/codebase-research-locator.md +4 -4
- package/dist/builtin/subagents/agents/debugger.md +5 -5
- package/dist/builtin/subagents/agents/worker.md +56 -0
- package/dist/builtin/subagents/package.json +1 -1
- package/dist/builtin/subagents/skills/subagent/SKILL.md +11 -11
- package/dist/builtin/subagents/src/agents/agent-loaders.ts +3 -5
- package/dist/builtin/subagents/src/agents/agent-management-helpers.ts +3 -3
- package/dist/builtin/subagents/src/extension/schemas.ts +2 -2
- package/dist/builtin/subagents/src/intercom/result-intercom.ts +4 -3
- package/dist/builtin/subagents/src/runs/shared/mcp-direct-tool-allowlist.ts +1 -1
- package/dist/builtin/subagents/src/runs/shared/nested-render.ts +2 -2
- package/dist/builtin/subagents/src/runs/shared/pi-args.ts +2 -1
- package/dist/builtin/subagents/src/shared/types-depth.ts +5 -5
- package/dist/builtin/subagents/src/shared/types-runtime.ts +2 -1
- package/dist/builtin/subagents/src/tui/render-event-formatting.ts +2 -2
- package/dist/builtin/web-access/CHANGELOG.md +6 -0
- package/dist/builtin/web-access/package.json +1 -1
- package/dist/builtin/workflows/CHANGELOG.md +21 -0
- package/dist/builtin/workflows/README.md +2 -2
- package/dist/builtin/workflows/builtin/goal-artifacts.ts +11 -6
- package/dist/builtin/workflows/builtin/goal-ledger.ts +33 -1
- package/dist/builtin/workflows/builtin/goal-prompts.ts +23 -28
- package/dist/builtin/workflows/builtin/goal-reducer.ts +2 -2
- package/dist/builtin/workflows/builtin/goal-reports.ts +2 -5
- package/dist/builtin/workflows/builtin/goal-review.ts +1 -1
- package/dist/builtin/workflows/builtin/goal-runner.ts +10 -17
- package/dist/builtin/workflows/builtin/open-claude-design-feedback.ts +3 -3
- package/dist/builtin/workflows/builtin/open-claude-design-phases.ts +1 -3
- package/dist/builtin/workflows/builtin/open-claude-design-setup.ts +1 -1
- package/dist/builtin/workflows/builtin/ralph-core.ts +7 -17
- package/dist/builtin/workflows/builtin/ralph-runner.ts +11 -18
- package/dist/builtin/workflows/builtin/shared-prompts.ts +1 -1
- package/dist/builtin/workflows/package.json +1 -1
- package/dist/builtin/workflows/src/extension/config-loader.ts +35 -15
- package/dist/builtin/workflows/src/extension/discovery.ts +20 -8
- package/dist/builtin/workflows/src/extension/extension-runtime-state.ts +1 -2
- package/dist/builtin/workflows/src/extension/wiring.ts +1 -1
- package/dist/builtin/workflows/src/tui/dispatch-confirm.ts +11 -10
- package/dist/cli/args.d.ts.map +1 -1
- package/dist/cli/args.js +9 -9
- package/dist/cli/args.js.map +1 -1
- package/dist/config-self-update.d.ts.map +1 -1
- package/dist/config-self-update.js +3 -4
- package/dist/config-self-update.js.map +1 -1
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +4 -5
- package/dist/config.js.map +1 -1
- package/dist/core/agent-session-bash.d.ts +1 -0
- package/dist/core/agent-session-bash.d.ts.map +1 -1
- package/dist/core/agent-session-bash.js +1 -0
- package/dist/core/agent-session-bash.js.map +1 -1
- package/dist/core/agent-session-tool-registry.d.ts.map +1 -1
- package/dist/core/agent-session-tool-registry.js +23 -0
- package/dist/core/agent-session-tool-registry.js.map +1 -1
- package/dist/core/bash-executor.d.ts +2 -0
- package/dist/core/bash-executor.d.ts.map +1 -1
- package/dist/core/bash-executor.js +1 -0
- package/dist/core/bash-executor.js.map +1 -1
- package/dist/core/compaction/compaction.d.ts +29 -0
- package/dist/core/compaction/compaction.d.ts.map +1 -1
- package/dist/core/compaction/compaction.js +36 -1
- package/dist/core/compaction/compaction.js.map +1 -1
- package/dist/core/compaction/context-compaction-metrics.d.ts +14 -2
- package/dist/core/compaction/context-compaction-metrics.d.ts.map +1 -1
- package/dist/core/compaction/context-compaction-metrics.js +50 -1
- package/dist/core/compaction/context-compaction-metrics.js.map +1 -1
- package/dist/core/compaction/context-compaction-prompt.d.ts.map +1 -1
- package/dist/core/compaction/context-compaction-prompt.js +2 -0
- package/dist/core/compaction/context-compaction-prompt.js.map +1 -1
- package/dist/core/compaction/context-compaction-runner.d.ts.map +1 -1
- package/dist/core/compaction/context-compaction-runner.js +1 -1
- package/dist/core/compaction/context-compaction-runner.js.map +1 -1
- package/dist/core/compaction/context-deletion-application.d.ts.map +1 -1
- package/dist/core/compaction/context-deletion-application.js +5 -5
- package/dist/core/compaction/context-deletion-application.js.map +1 -1
- package/dist/core/compaction/context-deletion-targets.d.ts +2 -0
- package/dist/core/compaction/context-deletion-targets.d.ts.map +1 -1
- package/dist/core/compaction/context-deletion-targets.js +23 -3
- package/dist/core/compaction/context-deletion-targets.js.map +1 -1
- package/dist/core/compaction/context-deletion-tool-definitions.d.ts +6 -0
- package/dist/core/compaction/context-deletion-tool-definitions.d.ts.map +1 -1
- package/dist/core/compaction/context-deletion-tool-definitions.js.map +1 -1
- package/dist/core/compaction/context-deletion-tools.d.ts.map +1 -1
- package/dist/core/compaction/context-deletion-tools.js +18 -10
- package/dist/core/compaction/context-deletion-tools.js.map +1 -1
- package/dist/core/compaction/context-transcript-analysis.d.ts.map +1 -1
- package/dist/core/compaction/context-transcript-analysis.js +2 -4
- package/dist/core/compaction/context-transcript-analysis.js.map +1 -1
- package/dist/core/copilot-gemini-tool-arguments.d.ts.map +1 -1
- package/dist/core/copilot-gemini-tool-arguments.js +2 -60
- package/dist/core/copilot-gemini-tool-arguments.js.map +1 -1
- package/dist/core/extensions/context-types.d.ts +2 -0
- package/dist/core/extensions/context-types.d.ts.map +1 -1
- package/dist/core/extensions/context-types.js.map +1 -1
- package/dist/core/extensions/index.d.ts +2 -2
- package/dist/core/extensions/index.d.ts.map +1 -1
- package/dist/core/extensions/index.js +1 -1
- package/dist/core/extensions/index.js.map +1 -1
- package/dist/core/extensions/loader-virtual-modules.d.ts.map +1 -1
- package/dist/core/extensions/loader-virtual-modules.js +11 -3
- package/dist/core/extensions/loader-virtual-modules.js.map +1 -1
- package/dist/core/extensions/runner-context.d.ts.map +1 -1
- package/dist/core/extensions/runner-context.js +11 -0
- package/dist/core/extensions/runner-context.js.map +1 -1
- package/dist/core/extensions/tool-events.d.ts +13 -13
- package/dist/core/extensions/tool-events.d.ts.map +1 -1
- package/dist/core/extensions/tool-events.js +3 -3
- package/dist/core/extensions/tool-events.js.map +1 -1
- package/dist/core/extensions/types.d.ts +1 -1
- package/dist/core/extensions/types.d.ts.map +1 -1
- package/dist/core/extensions/types.js +1 -1
- package/dist/core/extensions/types.js.map +1 -1
- package/dist/core/flattened-tool-arguments.d.ts +18 -0
- package/dist/core/flattened-tool-arguments.d.ts.map +1 -1
- package/dist/core/flattened-tool-arguments.js +104 -0
- package/dist/core/flattened-tool-arguments.js.map +1 -1
- package/dist/core/sdk-exports.d.ts +1 -1
- package/dist/core/sdk-exports.d.ts.map +1 -1
- package/dist/core/sdk-exports.js +1 -1
- package/dist/core/sdk-exports.js.map +1 -1
- package/dist/core/sdk-types.d.ts +2 -2
- package/dist/core/sdk-types.d.ts.map +1 -1
- package/dist/core/sdk-types.js.map +1 -1
- package/dist/core/settings-manager-basic-accessors.d.ts +4 -0
- package/dist/core/settings-manager-basic-accessors.d.ts.map +1 -1
- package/dist/core/settings-manager-basic-accessors.js +18 -0
- package/dist/core/settings-manager-basic-accessors.js.map +1 -1
- package/dist/core/settings-manager-resource-accessors.d.ts +4 -0
- package/dist/core/settings-manager-resource-accessors.d.ts.map +1 -1
- package/dist/core/settings-manager-resource-accessors.js +15 -0
- package/dist/core/settings-manager-resource-accessors.js.map +1 -1
- package/dist/core/settings-types.d.ts +11 -0
- package/dist/core/settings-types.d.ts.map +1 -1
- package/dist/core/settings-types.js.map +1 -1
- package/dist/core/system-prompt.d.ts +1 -1
- package/dist/core/system-prompt.d.ts.map +1 -1
- package/dist/core/system-prompt.js +3 -2
- package/dist/core/system-prompt.js.map +1 -1
- package/dist/core/tools/artifact-protocol.d.ts +11 -0
- package/dist/core/tools/artifact-protocol.d.ts.map +1 -0
- package/dist/core/tools/artifact-protocol.js +76 -0
- package/dist/core/tools/artifact-protocol.js.map +1 -0
- package/dist/core/tools/artifacts.d.ts +18 -0
- package/dist/core/tools/artifacts.d.ts.map +1 -0
- package/dist/core/tools/artifacts.js +90 -0
- package/dist/core/tools/artifacts.js.map +1 -0
- package/dist/core/tools/bash-async-jobs.d.ts +20 -0
- package/dist/core/tools/bash-async-jobs.d.ts.map +1 -0
- package/dist/core/tools/bash-async-jobs.js +59 -0
- package/dist/core/tools/bash-async-jobs.js.map +1 -0
- package/dist/core/tools/bash-async-output.d.ts +10 -0
- package/dist/core/tools/bash-async-output.d.ts.map +1 -0
- package/dist/core/tools/bash-async-output.js +80 -0
- package/dist/core/tools/bash-async-output.js.map +1 -0
- package/dist/core/tools/bash-interceptor.d.ts +10 -0
- package/dist/core/tools/bash-interceptor.d.ts.map +1 -0
- package/dist/core/tools/bash-interceptor.js +39 -0
- package/dist/core/tools/bash-interceptor.js.map +1 -0
- package/dist/core/tools/bash-leading-cd.d.ts +7 -0
- package/dist/core/tools/bash-leading-cd.d.ts.map +1 -0
- package/dist/core/tools/bash-leading-cd.js +59 -0
- package/dist/core/tools/bash-leading-cd.js.map +1 -0
- package/dist/core/tools/bash-pty-native.d.ts +14 -0
- package/dist/core/tools/bash-pty-native.d.ts.map +1 -0
- package/dist/core/tools/bash-pty-native.js +71 -0
- package/dist/core/tools/bash-pty-native.js.map +1 -0
- package/dist/core/tools/bash.d.ts +28 -17
- package/dist/core/tools/bash.d.ts.map +1 -1
- package/dist/core/tools/bash.js +152 -35
- package/dist/core/tools/bash.js.map +1 -1
- package/dist/core/tools/block-resolver.d.ts +16 -0
- package/dist/core/tools/block-resolver.d.ts.map +1 -0
- package/dist/core/tools/block-resolver.js +74 -0
- package/dist/core/tools/block-resolver.js.map +1 -0
- package/dist/core/tools/conflict-registry.d.ts +16 -0
- package/dist/core/tools/conflict-registry.d.ts.map +1 -0
- package/dist/core/tools/conflict-registry.js +44 -0
- package/dist/core/tools/conflict-registry.js.map +1 -0
- package/dist/core/tools/directory-tree.d.ts +13 -0
- package/dist/core/tools/directory-tree.d.ts.map +1 -0
- package/dist/core/tools/directory-tree.js +81 -0
- package/dist/core/tools/directory-tree.js.map +1 -0
- package/dist/core/tools/edit.d.ts +4 -29
- package/dist/core/tools/edit.d.ts.map +1 -1
- package/dist/core/tools/edit.js +136 -228
- package/dist/core/tools/edit.js.map +1 -1
- package/dist/core/tools/fetch-url.d.ts +74 -0
- package/dist/core/tools/fetch-url.d.ts.map +1 -0
- package/dist/core/tools/fetch-url.js +518 -0
- package/dist/core/tools/fetch-url.js.map +1 -0
- package/dist/core/tools/find.d.ts +27 -9
- package/dist/core/tools/find.d.ts.map +1 -1
- package/dist/core/tools/find.js +400 -176
- package/dist/core/tools/find.js.map +1 -1
- package/dist/core/tools/glob-path-utils.d.ts +8 -0
- package/dist/core/tools/glob-path-utils.d.ts.map +1 -0
- package/dist/core/tools/glob-path-utils.js +26 -0
- package/dist/core/tools/glob-path-utils.js.map +1 -0
- package/dist/core/tools/grep.d.ts +12 -0
- package/dist/core/tools/grep.d.ts.map +1 -1
- package/dist/core/tools/grep.js +141 -17
- package/dist/core/tools/grep.js.map +1 -1
- package/dist/core/tools/hashline-engine/apply.d.ts +11 -0
- package/dist/core/tools/hashline-engine/apply.d.ts.map +1 -0
- package/dist/core/tools/hashline-engine/apply.js +752 -0
- package/dist/core/tools/hashline-engine/apply.js.map +1 -0
- package/dist/core/tools/hashline-engine/block.d.ts +40 -0
- package/dist/core/tools/hashline-engine/block.d.ts.map +1 -0
- package/dist/core/tools/hashline-engine/block.js +117 -0
- package/dist/core/tools/hashline-engine/block.js.map +1 -0
- package/dist/core/tools/hashline-engine/diff-preview.d.ts +15 -0
- package/dist/core/tools/hashline-engine/diff-preview.d.ts.map +1 -0
- package/dist/core/tools/hashline-engine/diff-preview.js +98 -0
- package/dist/core/tools/hashline-engine/diff-preview.js.map +1 -0
- package/dist/core/tools/hashline-engine/format.d.ts +71 -0
- package/dist/core/tools/hashline-engine/format.d.ts.map +1 -0
- package/dist/core/tools/hashline-engine/format.js +178 -0
- package/dist/core/tools/hashline-engine/format.js.map +1 -0
- package/dist/core/tools/hashline-engine/fs.d.ts +81 -0
- package/dist/core/tools/hashline-engine/fs.d.ts.map +1 -0
- package/dist/core/tools/hashline-engine/fs.js +143 -0
- package/dist/core/tools/hashline-engine/fs.js.map +1 -0
- package/dist/core/tools/hashline-engine/index.d.ts +18 -0
- package/dist/core/tools/hashline-engine/index.d.ts.map +1 -0
- package/dist/core/tools/hashline-engine/index.js +20 -0
- package/dist/core/tools/hashline-engine/index.js.map +1 -0
- package/dist/core/tools/hashline-engine/input.d.ts +101 -0
- package/dist/core/tools/hashline-engine/input.d.ts.map +1 -0
- package/dist/core/tools/hashline-engine/input.js +398 -0
- package/dist/core/tools/hashline-engine/input.js.map +1 -0
- package/dist/core/tools/hashline-engine/messages.d.ts +99 -0
- package/dist/core/tools/hashline-engine/messages.d.ts.map +1 -0
- package/dist/core/tools/hashline-engine/messages.js +144 -0
- package/dist/core/tools/hashline-engine/messages.js.map +1 -0
- package/dist/core/tools/hashline-engine/mismatch.d.ts +45 -0
- package/dist/core/tools/hashline-engine/mismatch.d.ts.map +1 -0
- package/dist/core/tools/hashline-engine/mismatch.js +90 -0
- package/dist/core/tools/hashline-engine/mismatch.js.map +1 -0
- package/dist/core/tools/hashline-engine/normalize.d.ts +21 -0
- package/dist/core/tools/hashline-engine/normalize.d.ts.map +1 -0
- package/dist/core/tools/hashline-engine/normalize.js +33 -0
- package/dist/core/tools/hashline-engine/normalize.js.map +1 -0
- package/dist/core/tools/hashline-engine/parser.d.ts +24 -0
- package/dist/core/tools/hashline-engine/parser.d.ts.map +1 -0
- package/dist/core/tools/hashline-engine/parser.js +381 -0
- package/dist/core/tools/hashline-engine/parser.js.map +1 -0
- package/dist/core/tools/hashline-engine/patcher.d.ts +118 -0
- package/dist/core/tools/hashline-engine/patcher.d.ts.map +1 -0
- package/dist/core/tools/hashline-engine/patcher.js +341 -0
- package/dist/core/tools/hashline-engine/patcher.js.map +1 -0
- package/dist/core/tools/hashline-engine/prefixes.d.ts +43 -0
- package/dist/core/tools/hashline-engine/prefixes.d.ts.map +1 -0
- package/dist/core/tools/hashline-engine/prefixes.js +135 -0
- package/dist/core/tools/hashline-engine/prefixes.js.map +1 -0
- package/dist/core/tools/hashline-engine/recovery.d.ts +41 -0
- package/dist/core/tools/hashline-engine/recovery.d.ts.map +1 -0
- package/dist/core/tools/hashline-engine/recovery.js +168 -0
- package/dist/core/tools/hashline-engine/recovery.js.map +1 -0
- package/dist/core/tools/hashline-engine/snapshots.d.ts +65 -0
- package/dist/core/tools/hashline-engine/snapshots.d.ts.map +1 -0
- package/dist/core/tools/hashline-engine/snapshots.js +108 -0
- package/dist/core/tools/hashline-engine/snapshots.js.map +1 -0
- package/dist/core/tools/hashline-engine/stream.d.ts +3 -0
- package/dist/core/tools/hashline-engine/stream.d.ts.map +1 -0
- package/dist/core/tools/hashline-engine/stream.js +111 -0
- package/dist/core/tools/hashline-engine/stream.js.map +1 -0
- package/dist/core/tools/hashline-engine/tokenizer.d.ts +69 -0
- package/dist/core/tools/hashline-engine/tokenizer.d.ts.map +1 -0
- package/dist/core/tools/hashline-engine/tokenizer.js +430 -0
- package/dist/core/tools/hashline-engine/tokenizer.js.map +1 -0
- package/dist/core/tools/hashline-engine/types.d.ts +166 -0
- package/dist/core/tools/hashline-engine/types.d.ts.map +1 -0
- package/dist/core/tools/hashline-engine/types.js +9 -0
- package/dist/core/tools/hashline-engine/types.js.map +1 -0
- package/dist/core/tools/hashline.d.ts +29 -0
- package/dist/core/tools/hashline.d.ts.map +1 -0
- package/dist/core/tools/hashline.js +110 -0
- package/dist/core/tools/hashline.js.map +1 -0
- package/dist/core/tools/index.d.ts +6 -4
- package/dist/core/tools/index.d.ts.map +1 -1
- package/dist/core/tools/index.js +52 -35
- package/dist/core/tools/index.js.map +1 -1
- package/dist/core/tools/notebook.d.ts +38 -0
- package/dist/core/tools/notebook.d.ts.map +1 -0
- package/dist/core/tools/notebook.js +125 -0
- package/dist/core/tools/notebook.js.map +1 -0
- package/dist/core/tools/read-document-extract.d.ts +9 -0
- package/dist/core/tools/read-document-extract.d.ts.map +1 -0
- package/dist/core/tools/read-document-extract.js +212 -0
- package/dist/core/tools/read-document-extract.js.map +1 -0
- package/dist/core/tools/read-selectors.d.ts +24 -0
- package/dist/core/tools/read-selectors.d.ts.map +1 -0
- package/dist/core/tools/read-selectors.js +277 -0
- package/dist/core/tools/read-selectors.js.map +1 -0
- package/dist/core/tools/read-url.d.ts +37 -0
- package/dist/core/tools/read-url.d.ts.map +1 -0
- package/dist/core/tools/read-url.js +39 -0
- package/dist/core/tools/read-url.js.map +1 -0
- package/dist/core/tools/read.d.ts +11 -11
- package/dist/core/tools/read.d.ts.map +1 -1
- package/dist/core/tools/read.js +224 -94
- package/dist/core/tools/read.js.map +1 -1
- package/dist/core/tools/resource-selectors.d.ts +44 -0
- package/dist/core/tools/resource-selectors.d.ts.map +1 -0
- package/dist/core/tools/resource-selectors.js +808 -0
- package/dist/core/tools/resource-selectors.js.map +1 -0
- package/dist/core/tools/search-details.d.ts +26 -0
- package/dist/core/tools/search-details.d.ts.map +1 -0
- package/dist/core/tools/search-details.js +24 -0
- package/dist/core/tools/search-details.js.map +1 -0
- package/dist/core/tools/search-line-ranges.d.ts +11 -0
- package/dist/core/tools/search-line-ranges.d.ts.map +1 -0
- package/dist/core/tools/search-line-ranges.js +65 -0
- package/dist/core/tools/search-line-ranges.js.map +1 -0
- package/dist/core/tools/search-native.d.ts +97 -0
- package/dist/core/tools/search-native.d.ts.map +1 -0
- package/dist/core/tools/search-native.js +27 -0
- package/dist/core/tools/search-native.js.map +1 -0
- package/dist/core/tools/search.d.ts +24 -0
- package/dist/core/tools/search.d.ts.map +1 -0
- package/dist/core/tools/search.js +573 -0
- package/dist/core/tools/search.js.map +1 -0
- package/dist/core/tools/truncate.d.ts +4 -4
- package/dist/core/tools/truncate.d.ts.map +1 -1
- package/dist/core/tools/truncate.js +3 -3
- package/dist/core/tools/truncate.js.map +1 -1
- package/dist/core/tools/url-ip-guards.d.ts +4 -0
- package/dist/core/tools/url-ip-guards.d.ts.map +1 -0
- package/dist/core/tools/url-ip-guards.js +126 -0
- package/dist/core/tools/url-ip-guards.js.map +1 -0
- package/dist/core/tools/write.d.ts +12 -2
- package/dist/core/tools/write.d.ts.map +1 -1
- package/dist/core/tools/write.js +166 -14
- package/dist/core/tools/write.js.map +1 -1
- package/dist/core/trust-manager.d.ts.map +1 -1
- package/dist/core/trust-manager.js +2 -3
- package/dist/core/trust-manager.js.map +1 -1
- package/dist/index-extensions.d.ts +2 -2
- package/dist/index-extensions.d.ts.map +1 -1
- package/dist/index-extensions.js +1 -1
- package/dist/index-extensions.js.map +1 -1
- package/dist/index.d.ts +3 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -3
- package/dist/index.js.map +1 -1
- package/dist/modes/interactive/components/custom-editor.d.ts +1 -0
- package/dist/modes/interactive/components/custom-editor.d.ts.map +1 -1
- package/dist/modes/interactive/components/custom-editor.js +9 -2
- package/dist/modes/interactive/components/custom-editor.js.map +1 -1
- package/dist/modes/interactive/components/settings-selector-handlers.d.ts.map +1 -1
- package/dist/modes/interactive/components/settings-selector-handlers.js +3 -0
- package/dist/modes/interactive/components/settings-selector-handlers.js.map +1 -1
- package/dist/modes/interactive/components/settings-selector-items.d.ts.map +1 -1
- package/dist/modes/interactive/components/settings-selector-items.js +7 -0
- package/dist/modes/interactive/components/settings-selector-items.js.map +1 -1
- package/dist/modes/interactive/components/settings-selector-types.d.ts +2 -0
- package/dist/modes/interactive/components/settings-selector-types.d.ts.map +1 -1
- package/dist/modes/interactive/components/settings-selector-types.js.map +1 -1
- package/dist/modes/interactive/components/tree-selector-content.d.ts.map +1 -1
- package/dist/modes/interactive/components/tree-selector-content.js +0 -5
- package/dist/modes/interactive/components/tree-selector-content.js.map +1 -1
- package/dist/modes/interactive/interactive-auth-login.d.ts.map +1 -1
- package/dist/modes/interactive/interactive-auth-login.js +1 -0
- package/dist/modes/interactive/interactive-auth-login.js.map +1 -1
- package/dist/modes/interactive/interactive-autocomplete.d.ts.map +1 -1
- package/dist/modes/interactive/interactive-autocomplete.js +80 -2
- package/dist/modes/interactive/interactive-autocomplete.js.map +1 -1
- package/dist/modes/interactive/interactive-hotkeys-debug.d.ts.map +1 -1
- package/dist/modes/interactive/interactive-hotkeys-debug.js +3 -0
- package/dist/modes/interactive/interactive-hotkeys-debug.js.map +1 -1
- package/dist/modes/interactive/interactive-input-handling.d.ts.map +1 -1
- package/dist/modes/interactive/interactive-input-handling.js +51 -0
- package/dist/modes/interactive/interactive-input-handling.js.map +1 -1
- package/dist/modes/interactive/interactive-mode-base.d.ts +5 -0
- package/dist/modes/interactive/interactive-mode-base.d.ts.map +1 -1
- package/dist/modes/interactive/interactive-mode-base.js +5 -0
- package/dist/modes/interactive/interactive-mode-base.js.map +1 -1
- package/dist/modes/interactive/interactive-mode-deps.d.ts +1 -1
- package/dist/modes/interactive/interactive-mode-deps.d.ts.map +1 -1
- package/dist/modes/interactive/interactive-mode-deps.js.map +1 -1
- package/dist/modes/interactive/interactive-mode-surface.d.ts +12 -0
- package/dist/modes/interactive/interactive-mode-surface.d.ts.map +1 -1
- package/dist/modes/interactive/interactive-mode-surface.js.map +1 -1
- package/dist/modes/interactive/interactive-mode.d.ts +1 -0
- package/dist/modes/interactive/interactive-mode.d.ts.map +1 -1
- package/dist/modes/interactive/interactive-mode.js +1 -0
- package/dist/modes/interactive/interactive-mode.js.map +1 -1
- package/dist/modes/interactive/interactive-model-routing.d.ts.map +1 -1
- package/dist/modes/interactive/interactive-model-routing.js +4 -1
- package/dist/modes/interactive/interactive-model-routing.js.map +1 -1
- package/dist/modes/interactive/interactive-onboarding.d.ts +11 -0
- package/dist/modes/interactive/interactive-onboarding.d.ts.map +1 -0
- package/dist/modes/interactive/interactive-onboarding.js +220 -0
- package/dist/modes/interactive/interactive-onboarding.js.map +1 -0
- package/dist/modes/interactive/interactive-selectors.d.ts.map +1 -1
- package/dist/modes/interactive/interactive-selectors.js +4 -0
- package/dist/modes/interactive/interactive-selectors.js.map +1 -1
- package/dist/modes/interactive/interactive-session-routing.d.ts.map +1 -1
- package/dist/modes/interactive/interactive-session-routing.js +6 -0
- package/dist/modes/interactive/interactive-session-routing.js.map +1 -1
- package/dist/modes/interactive/interactive-slash-commands.d.ts.map +1 -1
- package/dist/modes/interactive/interactive-slash-commands.js +9 -4
- package/dist/modes/interactive/interactive-slash-commands.js.map +1 -1
- package/dist/modes/interactive/interactive-startup.d.ts.map +1 -1
- package/dist/modes/interactive/interactive-startup.js +28 -0
- package/dist/modes/interactive/interactive-startup.js.map +1 -1
- package/dist/utils/child-process.d.ts.map +1 -1
- package/dist/utils/child-process.js +21 -1
- package/dist/utils/child-process.js.map +1 -1
- package/dist/utils/markit.d.ts +8 -0
- package/dist/utils/markit.d.ts.map +1 -0
- package/dist/utils/markit.js +53 -0
- package/dist/utils/markit.js.map +1 -0
- package/dist/utils/paths.d.ts +2 -1
- package/dist/utils/paths.d.ts.map +1 -1
- package/dist/utils/paths.js +14 -1
- package/dist/utils/paths.js.map +1 -1
- package/docs/compaction.md +16 -1
- package/docs/containerization.md +1 -1
- package/docs/docs.json +1 -0
- package/docs/extensions.md +25 -36
- package/docs/quickstart.md +11 -6
- package/docs/sdk.md +5 -5
- package/docs/settings.md +7 -0
- package/docs/subagents.md +3 -2
- package/docs/tools.md +49 -0
- package/docs/usage.md +3 -3
- package/docs/workflows.md +7 -5
- package/examples/extensions/subagent/README.md +5 -5
- package/examples/extensions/subagent/agents/planner.md +1 -1
- package/examples/extensions/subagent/agents/reviewer.md +1 -1
- package/examples/extensions/subagent/agents/scout.md +2 -2
- package/examples/extensions/subagent/display.ts +3 -3
- package/examples/sdk/05-tools.ts +3 -3
- package/examples/sdk/README.md +1 -1
- package/package.json +3 -2
package/dist/core/tools/grep.js
CHANGED
|
@@ -7,6 +7,7 @@ import { Type } from "typebox";
|
|
|
7
7
|
import { keyHint } from "../../modes/interactive/components/keybinding-hints.js";
|
|
8
8
|
import { ensureTool } from "../../utils/tools-manager.js";
|
|
9
9
|
import { resolveToCwd } from "./path-utils.js";
|
|
10
|
+
import { loadNativeSearchBinding } from "./search-native.js";
|
|
10
11
|
import { getTextOutput, invalidArgText, shortenPath, str } from "./render-utils.js";
|
|
11
12
|
import { wrapToolDefinition } from "./tool-definition-wrapper.js";
|
|
12
13
|
import { DEFAULT_MAX_BYTES, formatSize, GREP_MAX_LINE_LENGTH, truncateHead, truncateLine, } from "./truncate.js";
|
|
@@ -15,10 +16,20 @@ const grepSchema = Type.Object({
|
|
|
15
16
|
path: Type.Optional(Type.String({ description: "Directory or file to search (default: current directory)" })),
|
|
16
17
|
glob: Type.Optional(Type.String({ description: "Filter files by glob pattern, e.g. '*.ts' or '**/*.spec.ts'" })),
|
|
17
18
|
ignoreCase: Type.Optional(Type.Boolean({ description: "Case-insensitive search (default: false)" })),
|
|
19
|
+
type: Type.Optional(Type.String({ description: "File type filter for native grep." })),
|
|
18
20
|
literal: Type.Optional(Type.Boolean({ description: "Treat pattern as literal string instead of regex (default: false)" })),
|
|
19
21
|
context: Type.Optional(Type.Number({ description: "Number of lines to show before and after each match (default: 0)" })),
|
|
22
|
+
contextBefore: Type.Optional(Type.Number({ description: "Lines to show before each match." })),
|
|
23
|
+
contextAfter: Type.Optional(Type.Number({ description: "Lines to show after each match." })),
|
|
20
24
|
limit: Type.Optional(Type.Number({ description: "Maximum number of matches to return (default: 100)" })),
|
|
21
|
-
})
|
|
25
|
+
offset: Type.Optional(Type.Number({ description: "Skip first N matches." })),
|
|
26
|
+
mode: Type.Optional(Type.Union([Type.Literal("content"), Type.Literal("count"), Type.Literal("filesWithMatches")])),
|
|
27
|
+
maxCountPerFile: Type.Optional(Type.Number({ description: "Maximum matches per file." })),
|
|
28
|
+
hidden: Type.Optional(Type.Boolean({ description: "Search hidden files (default true)." })),
|
|
29
|
+
cache: Type.Optional(Type.Boolean({ description: "Use native cache." })),
|
|
30
|
+
timeoutMs: Type.Optional(Type.Number({ description: "Native grep timeout in milliseconds." })),
|
|
31
|
+
gitignore: Type.Optional(Type.Boolean({ description: "Respect .gitignore files (default: true)" })),
|
|
32
|
+
}, { additionalProperties: false });
|
|
22
33
|
const DEFAULT_LIMIT = 100;
|
|
23
34
|
function isRipgrepMatchEvent(event) {
|
|
24
35
|
return typeof event === "object" && event !== null && "type" in event && event.type === "match";
|
|
@@ -72,6 +83,17 @@ function formatGrepResult(result, options, theme, showImages) {
|
|
|
72
83
|
}
|
|
73
84
|
return text;
|
|
74
85
|
}
|
|
86
|
+
function formatNativeGrepMatch(match, displayPath) {
|
|
87
|
+
const lines = [];
|
|
88
|
+
for (const contextLine of match.contextBefore ?? []) {
|
|
89
|
+
lines.push(`${displayPath}-${contextLine.lineNumber}- ${contextLine.line}`);
|
|
90
|
+
}
|
|
91
|
+
lines.push(`${displayPath}:${match.lineNumber}: ${match.line}`);
|
|
92
|
+
for (const contextLine of match.contextAfter ?? []) {
|
|
93
|
+
lines.push(`${displayPath}-${contextLine.lineNumber}- ${contextLine.line}`);
|
|
94
|
+
}
|
|
95
|
+
return lines;
|
|
96
|
+
}
|
|
75
97
|
export function createGrepToolDefinition(cwd, options) {
|
|
76
98
|
const customOps = options?.operations;
|
|
77
99
|
return {
|
|
@@ -80,7 +102,7 @@ export function createGrepToolDefinition(cwd, options) {
|
|
|
80
102
|
description: `Search file contents for a pattern. Returns matching lines with file paths and line numbers. Respects .gitignore. Output is truncated to ${DEFAULT_LIMIT} matches or ${DEFAULT_MAX_BYTES / 1024}KB (whichever is hit first). Long lines are truncated to ${GREP_MAX_LINE_LENGTH} chars.`,
|
|
81
103
|
promptSnippet: "Search file contents for patterns (respects .gitignore)",
|
|
82
104
|
parameters: grepSchema,
|
|
83
|
-
async execute(_toolCallId, { pattern, path: searchDir, glob, ignoreCase, literal, context, limit, }, signal, _onUpdate, _ctx) {
|
|
105
|
+
async execute(_toolCallId, { pattern, path: searchDir, glob, ignoreCase, literal, context, limit, gitignore, type, contextBefore, contextAfter, offset, mode, maxCountPerFile, hidden, cache, timeoutMs, }, signal, _onUpdate, _ctx) {
|
|
84
106
|
return new Promise((resolve, reject) => {
|
|
85
107
|
if (signal?.aborted) {
|
|
86
108
|
reject(new Error("Operation aborted"));
|
|
@@ -95,11 +117,6 @@ export function createGrepToolDefinition(cwd, options) {
|
|
|
95
117
|
};
|
|
96
118
|
(async () => {
|
|
97
119
|
try {
|
|
98
|
-
const rgPath = await ensureTool("rg", true);
|
|
99
|
-
if (!rgPath) {
|
|
100
|
-
settle(() => reject(new Error("ripgrep (rg) is not available and could not be downloaded")));
|
|
101
|
-
return;
|
|
102
|
-
}
|
|
103
120
|
const searchPath = resolveToCwd(searchDir || ".", cwd);
|
|
104
121
|
const ops = customOps ?? defaultGrepOperations;
|
|
105
122
|
let isDirectory;
|
|
@@ -111,16 +128,77 @@ export function createGrepToolDefinition(cwd, options) {
|
|
|
111
128
|
return;
|
|
112
129
|
}
|
|
113
130
|
const contextValue = context && context > 0 ? context : 0;
|
|
131
|
+
const contextBeforeValue = contextBefore ?? contextValue;
|
|
132
|
+
const contextAfterValue = contextAfter ?? contextValue;
|
|
133
|
+
const nativeCache = options?.nativeCache === true;
|
|
114
134
|
const effectiveLimit = Math.max(1, limit ?? DEFAULT_LIMIT);
|
|
115
135
|
const formatPath = (filePath) => {
|
|
116
136
|
if (isDirectory) {
|
|
137
|
+
if (!path.isAbsolute(filePath))
|
|
138
|
+
return filePath.replace(/\\/g, "/");
|
|
117
139
|
const relative = path.relative(searchPath, filePath);
|
|
118
|
-
if (relative && !relative.startsWith(".."))
|
|
140
|
+
if (relative && !relative.startsWith(".."))
|
|
119
141
|
return relative.replace(/\\/g, "/");
|
|
120
|
-
}
|
|
121
142
|
}
|
|
122
143
|
return path.basename(filePath);
|
|
123
144
|
};
|
|
145
|
+
if (!customOps && !literal) {
|
|
146
|
+
const nativeBinding = loadNativeSearchBinding();
|
|
147
|
+
if (nativeBinding) {
|
|
148
|
+
const nativeResult = await nativeBinding.grep({
|
|
149
|
+
pattern,
|
|
150
|
+
path: searchPath,
|
|
151
|
+
cwd,
|
|
152
|
+
glob,
|
|
153
|
+
ignoreCase,
|
|
154
|
+
hidden: hidden ?? true,
|
|
155
|
+
gitignore: gitignore !== false,
|
|
156
|
+
cache: cache ?? nativeCache,
|
|
157
|
+
maxCount: mode === "count" ? undefined : effectiveLimit + 1,
|
|
158
|
+
offset,
|
|
159
|
+
context: contextBefore === undefined && contextAfter === undefined ? contextValue : undefined,
|
|
160
|
+
contextBefore,
|
|
161
|
+
contextAfter,
|
|
162
|
+
type,
|
|
163
|
+
mode,
|
|
164
|
+
maxCountPerFile,
|
|
165
|
+
maxColumns: GREP_MAX_LINE_LENGTH,
|
|
166
|
+
multiline: pattern.includes("\n") || pattern.includes("\\n"),
|
|
167
|
+
signal,
|
|
168
|
+
timeoutMs: timeoutMs ?? 30_000,
|
|
169
|
+
});
|
|
170
|
+
if (nativeResult.error)
|
|
171
|
+
throw new Error(nativeResult.error);
|
|
172
|
+
if (nativeResult.matches.length === 0) {
|
|
173
|
+
settle(() => resolve({ content: [{ type: "text", text: mode === "count" ? "0" : "No matches found" }], details: undefined }));
|
|
174
|
+
return;
|
|
175
|
+
}
|
|
176
|
+
const visibleMatches = nativeResult.matches.slice(0, effectiveLimit);
|
|
177
|
+
const filesOutput = mode === "filesWithMatches" && !isDirectory && (offset ?? 0) > 0 ? "" : visibleMatches.map((match) => formatPath(match.path)).join("\n");
|
|
178
|
+
const countOutput = String(Math.max(0, nativeResult.totalMatches - (offset ?? 0)));
|
|
179
|
+
const rawOutput = mode === "count" ? countOutput : mode === "filesWithMatches" ? (filesOutput || "No matches found") : visibleMatches.flatMap((match) => formatNativeGrepMatch(match, formatPath(match.path))).join("\n");
|
|
180
|
+
const truncation = truncateHead(rawOutput, { maxLines: Number.MAX_SAFE_INTEGER });
|
|
181
|
+
let output = truncation.content;
|
|
182
|
+
const details = {};
|
|
183
|
+
const notices = [];
|
|
184
|
+
if (nativeResult.matches.length > effectiveLimit || nativeResult.limitReached) {
|
|
185
|
+
notices.push(`${effectiveLimit} matches limit reached. Use limit=${effectiveLimit * 2} for more, or refine pattern`);
|
|
186
|
+
details.matchLimitReached = effectiveLimit;
|
|
187
|
+
}
|
|
188
|
+
if (truncation.truncated) {
|
|
189
|
+
notices.push(`${formatSize(DEFAULT_MAX_BYTES)} limit reached`);
|
|
190
|
+
details.truncation = truncation;
|
|
191
|
+
}
|
|
192
|
+
if (visibleMatches.some((match) => match.truncated)) {
|
|
193
|
+
notices.push(`Some lines truncated to ${GREP_MAX_LINE_LENGTH} chars. Use read tool to see full lines`);
|
|
194
|
+
details.linesTruncated = true;
|
|
195
|
+
}
|
|
196
|
+
if (notices.length > 0)
|
|
197
|
+
output += `\n\n[${notices.join(". ")}]`;
|
|
198
|
+
settle(() => resolve({ content: [{ type: "text", text: output }], details: Object.keys(details).length > 0 ? details : undefined }));
|
|
199
|
+
return;
|
|
200
|
+
}
|
|
201
|
+
}
|
|
124
202
|
const fileCache = new Map();
|
|
125
203
|
const getFileLines = async (filePath) => {
|
|
126
204
|
let lines = fileCache.get(filePath);
|
|
@@ -136,15 +214,31 @@ export function createGrepToolDefinition(cwd, options) {
|
|
|
136
214
|
}
|
|
137
215
|
return lines;
|
|
138
216
|
};
|
|
139
|
-
const
|
|
217
|
+
const rgPath = await ensureTool("rg", true);
|
|
218
|
+
if (!rgPath) {
|
|
219
|
+
settle(() => reject(new Error("ripgrep (rg) is not available and could not be downloaded")));
|
|
220
|
+
return;
|
|
221
|
+
}
|
|
222
|
+
const args = ["--json", "--line-number", "--color=never"];
|
|
223
|
+
if (hidden !== false)
|
|
224
|
+
args.push("--hidden");
|
|
225
|
+
if (gitignore === false)
|
|
226
|
+
args.push("--no-ignore");
|
|
140
227
|
if (ignoreCase)
|
|
141
228
|
args.push("--ignore-case");
|
|
142
229
|
if (literal)
|
|
143
230
|
args.push("--fixed-strings");
|
|
231
|
+
if (pattern.includes("\n") || pattern.includes("\\n"))
|
|
232
|
+
args.push("--multiline");
|
|
233
|
+
if (type)
|
|
234
|
+
args.push("--type", type);
|
|
235
|
+
if (maxCountPerFile !== undefined)
|
|
236
|
+
args.push("--max-count", String(maxCountPerFile));
|
|
144
237
|
if (glob)
|
|
145
238
|
args.push("--glob", glob);
|
|
146
239
|
args.push("--", pattern, searchPath);
|
|
147
240
|
const child = spawn(rgPath, args, { stdio: ["ignore", "pipe", "pipe"] });
|
|
241
|
+
const timeoutTimer = timeoutMs !== undefined ? setTimeout(() => stopChild(), timeoutMs) : undefined;
|
|
148
242
|
const rl = createInterface({ input: child.stdout });
|
|
149
243
|
let stderr = "";
|
|
150
244
|
let matchCount = 0;
|
|
@@ -154,6 +248,8 @@ export function createGrepToolDefinition(cwd, options) {
|
|
|
154
248
|
let killedDueToLimit = false;
|
|
155
249
|
const outputLines = [];
|
|
156
250
|
const cleanup = () => {
|
|
251
|
+
if (timeoutTimer)
|
|
252
|
+
clearTimeout(timeoutTimer);
|
|
157
253
|
rl.close();
|
|
158
254
|
signal?.removeEventListener("abort", onAbort);
|
|
159
255
|
};
|
|
@@ -177,8 +273,8 @@ export function createGrepToolDefinition(cwd, options) {
|
|
|
177
273
|
if (!lines.length)
|
|
178
274
|
return [`${relativePath}:${lineNumber}: (unable to read file)`];
|
|
179
275
|
const block = [];
|
|
180
|
-
const start =
|
|
181
|
-
const end =
|
|
276
|
+
const start = contextBeforeValue > 0 ? Math.max(1, lineNumber - contextBeforeValue) : lineNumber;
|
|
277
|
+
const end = contextAfterValue > 0 ? Math.min(lines.length, lineNumber + contextAfterValue) : lineNumber;
|
|
182
278
|
for (let current = start; current <= end; current++) {
|
|
183
279
|
const lineText = lines[current - 1] ?? "";
|
|
184
280
|
const sanitized = lineText.replace(/\r/g, "");
|
|
@@ -196,8 +292,10 @@ export function createGrepToolDefinition(cwd, options) {
|
|
|
196
292
|
};
|
|
197
293
|
// Collect matches during streaming, then format them after rg exits.
|
|
198
294
|
const matches = [];
|
|
295
|
+
let seenMatches = 0, seenFiles = 0;
|
|
296
|
+
const seenFilePaths = new Set(), filesWithMatches = new Set();
|
|
199
297
|
rl.on("line", (line) => {
|
|
200
|
-
if (!line.trim() || matchCount >= effectiveLimit)
|
|
298
|
+
if (!line.trim() || (mode !== "count" && mode !== "filesWithMatches" && matchCount >= effectiveLimit))
|
|
201
299
|
return;
|
|
202
300
|
let event;
|
|
203
301
|
try {
|
|
@@ -207,13 +305,35 @@ export function createGrepToolDefinition(cwd, options) {
|
|
|
207
305
|
return;
|
|
208
306
|
}
|
|
209
307
|
if (isRipgrepMatchEvent(event)) {
|
|
210
|
-
matchCount++;
|
|
211
308
|
const filePath = event.data?.path?.text;
|
|
309
|
+
if (mode === "filesWithMatches") {
|
|
310
|
+
if (typeof filePath === "string") {
|
|
311
|
+
const formatted = formatPath(filePath);
|
|
312
|
+
if (!seenFilePaths.has(formatted)) {
|
|
313
|
+
seenFilePaths.add(formatted);
|
|
314
|
+
seenFiles++;
|
|
315
|
+
if (offset === undefined || seenFiles > offset)
|
|
316
|
+
filesWithMatches.add(formatted);
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
matchCount = filesWithMatches.size;
|
|
320
|
+
if (matchCount >= effectiveLimit) {
|
|
321
|
+
matchLimitReached = true;
|
|
322
|
+
stopChild(true);
|
|
323
|
+
}
|
|
324
|
+
return;
|
|
325
|
+
}
|
|
326
|
+
seenMatches++;
|
|
327
|
+
if (mode !== "count" && offset !== undefined && seenMatches <= offset)
|
|
328
|
+
return;
|
|
329
|
+
matchCount++;
|
|
212
330
|
const lineNumber = event.data?.line_number;
|
|
213
331
|
const lineText = event.data?.lines?.text;
|
|
332
|
+
if (typeof filePath === "string")
|
|
333
|
+
filesWithMatches.add(formatPath(filePath));
|
|
214
334
|
if (typeof filePath === "string" && typeof lineNumber === "number")
|
|
215
335
|
matches.push({ filePath, lineNumber, lineText: typeof lineText === "string" ? lineText : undefined });
|
|
216
|
-
if (matchCount >= effectiveLimit) {
|
|
336
|
+
if (mode !== "count" && matchCount >= effectiveLimit) {
|
|
217
337
|
matchLimitReached = true;
|
|
218
338
|
stopChild(true);
|
|
219
339
|
}
|
|
@@ -235,12 +355,16 @@ export function createGrepToolDefinition(cwd, options) {
|
|
|
235
355
|
return;
|
|
236
356
|
}
|
|
237
357
|
if (matchCount === 0) {
|
|
238
|
-
settle(() => resolve({ content: [{ type: "text", text: "No matches found" }], details: undefined }));
|
|
358
|
+
settle(() => resolve({ content: [{ type: "text", text: mode === "count" ? "0" : "No matches found" }], details: undefined }));
|
|
359
|
+
return;
|
|
360
|
+
}
|
|
361
|
+
if (mode === "count" || mode === "filesWithMatches") {
|
|
362
|
+
settle(() => resolve({ content: [{ type: "text", text: mode === "count" ? String(Math.max(0, matchCount - (offset ?? 0))) : ([...filesWithMatches].join("\n") || "No matches found") }], details: matchLimitReached ? { matchLimitReached: effectiveLimit } : undefined }));
|
|
239
363
|
return;
|
|
240
364
|
}
|
|
241
365
|
// Format matches after streaming finishes so custom readFile() backends can be async.
|
|
242
366
|
for (const match of matches) {
|
|
243
|
-
if (
|
|
367
|
+
if (contextBeforeValue === 0 && contextAfterValue === 0 && match.lineText !== undefined) {
|
|
244
368
|
const relativePath = formatPath(match.filePath);
|
|
245
369
|
const sanitized = match.lineText
|
|
246
370
|
.replace(/\r\n/g, "\n")
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"grep.js","sourceRoot":"","sources":["../../../src/core/tools/grep.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,IAAI,UAAU,EAAE,IAAI,IAAI,MAAM,EAAE,MAAM,kBAAkB,CAAC;AAC1E,OAAO,EAAE,eAAe,EAAE,MAAM,eAAe,CAAC;AAEhD,OAAO,EAAE,IAAI,EAAE,MAAM,wBAAwB,CAAC;AAC9C,OAAO,EAAE,KAAK,EAAE,MAAM,eAAe,CAAC;AACtC,OAAO,IAAI,MAAM,MAAM,CAAC;AACxB,OAAO,EAAe,IAAI,EAAE,MAAM,SAAS,CAAC;AAC5C,OAAO,EAAE,OAAO,EAAE,MAAM,wDAAwD,CAAC;AACjF,OAAO,EAAE,UAAU,EAAE,MAAM,8BAA8B,CAAC;AAE1D,OAAO,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAC/C,OAAO,EAAE,aAAa,EAAE,cAAc,EAAE,WAAW,EAAE,GAAG,EAAE,MAAM,mBAAmB,CAAC;AACpF,OAAO,EAAE,kBAAkB,EAAE,MAAM,8BAA8B,CAAC;AAClE,OAAO,EACN,iBAAiB,EACjB,UAAU,EACV,oBAAoB,EAEpB,YAAY,EACZ,YAAY,GACZ,MAAM,eAAe,CAAC;AAEvB,MAAM,UAAU,GAAG,IAAI,CAAC,MAAM,CAAC;IAC9B,OAAO,EAAE,IAAI,CAAC,MAAM,CAAC,EAAE,WAAW,EAAE,0CAA0C,EAAE,CAAC;IACjF,I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AO,IAAI,CAAC;QACb,CAAC;KACD,CAAC;AACH,CAAC;AAED,MAAM,UAAU,cAAc,CAAC,GAAW,EAAE,OAAyB;IACpE,OAAO,kBAAkB,CAAC,wBAAwB,CAAC,GAAG,EAAE,OAAO,CAAC,CAAC,CAAC;AACnE,CAAC","sourcesContent":["import { readFile as fsReadFile, stat as fsStat } from \"node:fs/promises\";\nimport { createInterface } from \"node:readline\";\nimport type { AgentTool } from \"@earendil-works/pi-agent-core\";\nimport { Text } from \"@earendil-works/pi-tui\";\nimport { spawn } from \"child_process\";\nimport path from \"path\";\nimport { type Static, Type } from \"typebox\";\nimport { keyHint } from \"../../modes/interactive/components/keybinding-hints.ts\";\nimport { ensureTool } from \"../../utils/tools-manager.ts\";\nimport type { ToolDefinition, ToolRenderResultOptions } from \"../extensions/types.ts\";\nimport { resolveToCwd } from \"./path-utils.ts\";\nimport { getTextOutput, invalidArgText, shortenPath, str } from \"./render-utils.ts\";\nimport { wrapToolDefinition } from \"./tool-definition-wrapper.ts\";\nimport {\n\tDEFAULT_MAX_BYTES,\n\tformatSize,\n\tGREP_MAX_LINE_LENGTH,\n\ttype TruncationResult,\n\ttruncateHead,\n\ttruncateLine,\n} from \"./truncate.ts\";\n\nconst grepSchema = Type.Object({\n\tpattern: Type.String({ description: \"Search pattern (regex or literal string)\" }),\n\tpath: Type.Optional(Type.String({ description: \"Directory or file to search (default: current directory)\" })),\n\tglob: Type.Optional(Type.String({ description: \"Filter files by glob pattern, e.g. '*.ts' or '**/*.spec.ts'\" })),\n\tignoreCase: Type.Optional(Type.Boolean({ description: \"Case-insensitive search (default: false)\" })),\n\tliteral: Type.Optional(\n\t\tType.Boolean({ description: \"Treat pattern as literal string instead of regex (default: false)\" }),\n\t),\n\tcontext: Type.Optional(\n\t\tType.Number({ description: \"Number of lines to show before and after each match (default: 0)\" }),\n\t),\n\tlimit: Type.Optional(Type.Number({ description: \"Maximum number of matches to return (default: 100)\" })),\n});\n\nexport type GrepToolInput = Static<typeof grepSchema>;\nconst DEFAULT_LIMIT = 100;\n\nexport interface GrepToolDetails {\n\ttruncation?: TruncationResult;\n\tmatchLimitReached?: number;\n\tlinesTruncated?: boolean;\n}\n\ntype RipgrepMatchEvent = {\n\ttype: \"match\";\n\tdata?: {\n\t\tpath?: { text?: unknown };\n\t\tline_number?: unknown;\n\t\tlines?: { text?: unknown };\n\t};\n};\n\nfunction isRipgrepMatchEvent(event: unknown): event is RipgrepMatchEvent {\n\treturn typeof event === \"object\" && event !== null && \"type\" in event && event.type === \"match\";\n}\n\n/**\n * Pluggable operations for the grep tool.\n * Override these to delegate search to remote systems (for example SSH).\n */\nexport interface GrepOperations {\n\t/** Check if path is a directory. Throws if path does not exist. */\n\tisDirectory: (absolutePath: string) => Promise<boolean> | boolean;\n\t/** Read file contents for context lines */\n\treadFile: (absolutePath: string) => Promise<string> | string;\n}\n\nconst defaultGrepOperations: GrepOperations = {\n\tisDirectory: async (p) => (await fsStat(p)).isDirectory(),\n\treadFile: (p) => fsReadFile(p, \"utf-8\"),\n};\n\nexport interface GrepToolOptions {\n\t/** Custom operations for grep. Default: local filesystem plus ripgrep */\n\toperations?: GrepOperations;\n}\n\nfunction formatGrepCall(\n\targs: { pattern: string; path?: string; glob?: string; limit?: number } | undefined,\n\ttheme: typeof import(\"../../modes/interactive/theme/theme.ts\").theme,\n): string {\n\tconst pattern = str(args?.pattern);\n\tconst rawPath = str(args?.path);\n\tconst path = rawPath !== null ? shortenPath(rawPath || \".\") : null;\n\tconst glob = str(args?.glob);\n\tconst limit = args?.limit;\n\tconst invalidArg = invalidArgText(theme);\n\tlet text =\n\t\ttheme.fg(\"toolTitle\", theme.bold(\"grep\")) +\n\t\t\" \" +\n\t\t(pattern === null ? invalidArg : theme.fg(\"accent\", `/${pattern || \"\"}/`)) +\n\t\ttheme.fg(\"toolOutput\", ` in ${path === null ? invalidArg : path}`);\n\tif (glob) text += theme.fg(\"toolOutput\", ` (${glob})`);\n\tif (limit !== undefined) text += theme.fg(\"toolOutput\", ` limit ${limit}`);\n\treturn text;\n}\n\nfunction formatGrepResult(\n\tresult: {\n\t\tcontent: Array<{ type: string; text?: string; data?: string; mimeType?: string }>;\n\t\tdetails?: GrepToolDetails;\n\t},\n\toptions: ToolRenderResultOptions,\n\ttheme: typeof import(\"../../modes/interactive/theme/theme.ts\").theme,\n\tshowImages: boolean,\n): string {\n\tconst output = getTextOutput(result, showImages).trim();\n\tlet text = \"\";\n\tif (output) {\n\t\tconst lines = output.split(\"\\n\");\n\t\tconst maxLines = options.expanded ? lines.length : 15;\n\t\tconst displayLines = lines.slice(0, maxLines);\n\t\tconst remaining = lines.length - maxLines;\n\t\ttext += `\\n${displayLines.map((line) => theme.fg(\"toolOutput\", line)).join(\"\\n\")}`;\n\t\tif (remaining > 0) {\n\t\t\ttext += `${theme.fg(\"muted\", `\\n... (${remaining} more lines,`)} ${keyHint(\"app.tools.expand\", \"Expand\")}${theme.fg(\"muted\", \")\")}`;\n\t\t}\n\t}\n\n\tconst matchLimit = result.details?.matchLimitReached;\n\tconst truncation = result.details?.truncation;\n\tconst linesTruncated = result.details?.linesTruncated;\n\tif (matchLimit || truncation?.truncated || linesTruncated) {\n\t\tconst warnings: string[] = [];\n\t\tif (matchLimit) warnings.push(`${matchLimit} matches limit`);\n\t\tif (truncation?.truncated) warnings.push(`${formatSize(truncation.maxBytes ?? DEFAULT_MAX_BYTES)} limit`);\n\t\tif (linesTruncated) warnings.push(\"some lines truncated\");\n\t\ttext += `\\n${theme.fg(\"warning\", `[Truncated: ${warnings.join(\", \")}]`)}`;\n\t}\n\treturn text;\n}\n\nexport function createGrepToolDefinition(\n\tcwd: string,\n\toptions?: GrepToolOptions,\n): ToolDefinition<typeof grepSchema, GrepToolDetails | undefined> {\n\tconst customOps = options?.operations;\n\treturn {\n\t\tname: \"grep\",\n\t\tlabel: \"grep\",\n\t\tdescription: `Search file contents for a pattern. Returns matching lines with file paths and line numbers. Respects .gitignore. Output is truncated to ${DEFAULT_LIMIT} matches or ${DEFAULT_MAX_BYTES / 1024}KB (whichever is hit first). Long lines are truncated to ${GREP_MAX_LINE_LENGTH} chars.`,\n\t\tpromptSnippet: \"Search file contents for patterns (respects .gitignore)\",\n\t\tparameters: grepSchema,\n\t\tasync execute(\n\t\t\t_toolCallId,\n\t\t\t{\n\t\t\t\tpattern,\n\t\t\t\tpath: searchDir,\n\t\t\t\tglob,\n\t\t\t\tignoreCase,\n\t\t\t\tliteral,\n\t\t\t\tcontext,\n\t\t\t\tlimit,\n\t\t\t}: {\n\t\t\t\tpattern: string;\n\t\t\t\tpath?: string;\n\t\t\t\tglob?: string;\n\t\t\t\tignoreCase?: boolean;\n\t\t\t\tliteral?: boolean;\n\t\t\t\tcontext?: number;\n\t\t\t\tlimit?: number;\n\t\t\t},\n\t\t\tsignal?: AbortSignal,\n\t\t\t_onUpdate?,\n\t\t\t_ctx?,\n\t\t) {\n\t\t\treturn new Promise((resolve, reject) => {\n\t\t\t\tif (signal?.aborted) {\n\t\t\t\t\treject(new Error(\"Operation aborted\"));\n\t\t\t\t\treturn;\n\t\t\t\t}\n\t\t\t\tlet settled = false;\n\t\t\t\tconst settle = (fn: () => void) => {\n\t\t\t\t\tif (!settled) {\n\t\t\t\t\t\tsettled = true;\n\t\t\t\t\t\tfn();\n\t\t\t\t\t}\n\t\t\t\t};\n\n\t\t\t\t(async () => {\n\t\t\t\t\ttry {\n\t\t\t\t\t\tconst rgPath = await ensureTool(\"rg\", true);\n\t\t\t\t\t\tif (!rgPath) {\n\t\t\t\t\t\t\tsettle(() => reject(new Error(\"ripgrep (rg) is not available and could not be downloaded\")));\n\t\t\t\t\t\t\treturn;\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tconst searchPath = resolveToCwd(searchDir || \".\", cwd);\n\t\t\t\t\t\tconst ops = customOps ?? defaultGrepOperations;\n\t\t\t\t\t\tlet isDirectory: boolean;\n\t\t\t\t\t\ttry {\n\t\t\t\t\t\t\tisDirectory = await ops.isDirectory(searchPath);\n\t\t\t\t\t\t} catch {\n\t\t\t\t\t\t\tsettle(() => reject(new Error(`Path not found: ${searchPath}`)));\n\t\t\t\t\t\t\treturn;\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tconst contextValue = context && context > 0 ? context : 0;\n\t\t\t\t\t\tconst effectiveLimit = Math.max(1, limit ?? DEFAULT_LIMIT);\n\t\t\t\t\t\tconst formatPath = (filePath: string): string => {\n\t\t\t\t\t\t\tif (isDirectory) {\n\t\t\t\t\t\t\t\tconst relative = path.relative(searchPath, filePath);\n\t\t\t\t\t\t\t\tif (relative && !relative.startsWith(\"..\")) {\n\t\t\t\t\t\t\t\t\treturn relative.replace(/\\\\/g, \"/\");\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\treturn path.basename(filePath);\n\t\t\t\t\t\t};\n\n\t\t\t\t\t\tconst fileCache = new Map<string, string[]>();\n\t\t\t\t\t\tconst getFileLines = async (filePath: string): Promise<string[]> => {\n\t\t\t\t\t\t\tlet lines = fileCache.get(filePath);\n\t\t\t\t\t\t\tif (!lines) {\n\t\t\t\t\t\t\t\ttry {\n\t\t\t\t\t\t\t\t\tconst content = await ops.readFile(filePath);\n\t\t\t\t\t\t\t\t\tlines = content.replace(/\\r\\n/g, \"\\n\").replace(/\\r/g, \"\\n\").split(\"\\n\");\n\t\t\t\t\t\t\t\t} catch {\n\t\t\t\t\t\t\t\t\tlines = [];\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\tfileCache.set(filePath, lines);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\treturn lines;\n\t\t\t\t\t\t};\n\n\t\t\t\t\t\tconst args: string[] = [\"--json\", \"--line-number\", \"--color=never\", \"--hidden\"];\n\t\t\t\t\t\tif (ignoreCase) args.push(\"--ignore-case\");\n\t\t\t\t\t\tif (literal) args.push(\"--fixed-strings\");\n\t\t\t\t\t\tif (glob) args.push(\"--glob\", glob);\n\t\t\t\t\t\targs.push(\"--\", pattern, searchPath);\n\n\t\t\t\t\t\tconst child = spawn(rgPath, args, { stdio: [\"ignore\", \"pipe\", \"pipe\"] });\n\t\t\t\t\t\tconst rl = createInterface({ input: child.stdout });\n\t\t\t\t\t\tlet stderr = \"\";\n\t\t\t\t\t\tlet matchCount = 0;\n\t\t\t\t\t\tlet matchLimitReached = false;\n\t\t\t\t\t\tlet linesTruncated = false;\n\t\t\t\t\t\tlet aborted = false;\n\t\t\t\t\t\tlet killedDueToLimit = false;\n\t\t\t\t\t\tconst outputLines: string[] = [];\n\n\t\t\t\t\t\tconst cleanup = () => {\n\t\t\t\t\t\t\trl.close();\n\t\t\t\t\t\t\tsignal?.removeEventListener(\"abort\", onAbort);\n\t\t\t\t\t\t};\n\t\t\t\t\t\tconst stopChild = (dueToLimit = false) => {\n\t\t\t\t\t\t\tif (!child.killed) {\n\t\t\t\t\t\t\t\tkilledDueToLimit = dueToLimit;\n\t\t\t\t\t\t\t\tchild.kill();\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t};\n\t\t\t\t\t\tconst onAbort = () => {\n\t\t\t\t\t\t\taborted = true;\n\t\t\t\t\t\t\tstopChild();\n\t\t\t\t\t\t};\n\t\t\t\t\t\tsignal?.addEventListener(\"abort\", onAbort, { once: true });\n\t\t\t\t\t\tchild.stderr?.on(\"data\", (chunk) => {\n\t\t\t\t\t\t\tstderr += chunk.toString();\n\t\t\t\t\t\t});\n\n\t\t\t\t\t\tconst formatBlock = async (filePath: string, lineNumber: number): Promise<string[]> => {\n\t\t\t\t\t\t\tconst relativePath = formatPath(filePath);\n\t\t\t\t\t\t\tconst lines = await getFileLines(filePath);\n\t\t\t\t\t\t\tif (!lines.length) return [`${relativePath}:${lineNumber}: (unable to read file)`];\n\t\t\t\t\t\t\tconst block: string[] = [];\n\t\t\t\t\t\t\tconst start = contextValue > 0 ? Math.max(1, lineNumber - contextValue) : lineNumber;\n\t\t\t\t\t\t\tconst end = contextValue > 0 ? Math.min(lines.length, lineNumber + contextValue) : lineNumber;\n\t\t\t\t\t\t\tfor (let current = start; current <= end; current++) {\n\t\t\t\t\t\t\t\tconst lineText = lines[current - 1] ?? \"\";\n\t\t\t\t\t\t\t\tconst sanitized = lineText.replace(/\\r/g, \"\");\n\t\t\t\t\t\t\t\tconst isMatchLine = current === lineNumber;\n\t\t\t\t\t\t\t\t// Truncate long lines so grep output stays compact.\n\t\t\t\t\t\t\t\tconst { text: truncatedText, wasTruncated } = truncateLine(sanitized);\n\t\t\t\t\t\t\t\tif (wasTruncated) linesTruncated = true;\n\t\t\t\t\t\t\t\tif (isMatchLine) block.push(`${relativePath}:${current}: ${truncatedText}`);\n\t\t\t\t\t\t\t\telse block.push(`${relativePath}-${current}- ${truncatedText}`);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\treturn block;\n\t\t\t\t\t\t};\n\n\t\t\t\t\t\t// Collect matches during streaming, then format them after rg exits.\n\t\t\t\t\t\tconst matches: Array<{ filePath: string; lineNumber: number; lineText?: string }> = [];\n\t\t\t\t\t\trl.on(\"line\", (line) => {\n\t\t\t\t\t\t\tif (!line.trim() || matchCount >= effectiveLimit) return;\n\t\t\t\t\t\t\tlet event: unknown;\n\t\t\t\t\t\t\ttry {\n\t\t\t\t\t\t\t\tevent = JSON.parse(line) as unknown;\n\t\t\t\t\t\t\t} catch {\n\t\t\t\t\t\t\t\treturn;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tif (isRipgrepMatchEvent(event)) {\n\t\t\t\t\t\t\t\tmatchCount++;\n\t\t\t\t\t\t\t\tconst filePath = event.data?.path?.text;\n\t\t\t\t\t\t\t\tconst lineNumber = event.data?.line_number;\n\t\t\t\t\t\t\t\tconst lineText = event.data?.lines?.text;\n\t\t\t\t\t\t\t\tif (typeof filePath === \"string\" && typeof lineNumber === \"number\")\n\t\t\t\t\t\t\t\t\tmatches.push({ filePath, lineNumber, lineText: typeof lineText === \"string\" ? lineText : undefined });\n\t\t\t\t\t\t\t\tif (matchCount >= effectiveLimit) {\n\t\t\t\t\t\t\t\t\tmatchLimitReached = true;\n\t\t\t\t\t\t\t\t\tstopChild(true);\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t});\n\n\t\t\t\t\t\tchild.on(\"error\", (error) => {\n\t\t\t\t\t\t\tcleanup();\n\t\t\t\t\t\t\tsettle(() => reject(new Error(`Failed to run ripgrep: ${error.message}`)));\n\t\t\t\t\t\t});\n\t\t\t\t\t\tchild.on(\"close\", async (code) => {\n\t\t\t\t\t\t\tcleanup();\n\t\t\t\t\t\t\tif (aborted) {\n\t\t\t\t\t\t\t\tsettle(() => reject(new Error(\"Operation aborted\")));\n\t\t\t\t\t\t\t\treturn;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tif (!killedDueToLimit && code !== 0 && code !== 1) {\n\t\t\t\t\t\t\t\tconst errorMsg = stderr.trim() || `ripgrep exited with code ${code}`;\n\t\t\t\t\t\t\t\tsettle(() => reject(new Error(errorMsg)));\n\t\t\t\t\t\t\t\treturn;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tif (matchCount === 0) {\n\t\t\t\t\t\t\t\tsettle(() =>\n\t\t\t\t\t\t\t\t\tresolve({ content: [{ type: \"text\", text: \"No matches found\" }], details: undefined }),\n\t\t\t\t\t\t\t\t);\n\t\t\t\t\t\t\t\treturn;\n\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\t// Format matches after streaming finishes so custom readFile() backends can be async.\n\t\t\t\t\t\t\tfor (const match of matches) {\n\t\t\t\t\t\t\t\tif (contextValue === 0 && match.lineText !== undefined) {\n\t\t\t\t\t\t\t\t\tconst relativePath = formatPath(match.filePath);\n\t\t\t\t\t\t\t\t\tconst sanitized = match.lineText\n\t\t\t\t\t\t\t\t\t\t.replace(/\\r\\n/g, \"\\n\")\n\t\t\t\t\t\t\t\t\t\t.replace(/\\r/g, \"\")\n\t\t\t\t\t\t\t\t\t\t.replace(/\\n$/, \"\");\n\t\t\t\t\t\t\t\t\tconst { text: truncatedText, wasTruncated } = truncateLine(sanitized);\n\t\t\t\t\t\t\t\t\tif (wasTruncated) linesTruncated = true;\n\t\t\t\t\t\t\t\t\toutputLines.push(`${relativePath}:${match.lineNumber}: ${truncatedText}`);\n\t\t\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t\t\tconst block = await formatBlock(match.filePath, match.lineNumber);\n\t\t\t\t\t\t\t\t\toutputLines.push(...block);\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\tconst rawOutput = outputLines.join(\"\\n\");\n\t\t\t\t\t\t\t// Apply byte truncation. There is no line limit here because the match limit already capped rows.\n\t\t\t\t\t\t\tconst truncation = truncateHead(rawOutput, { maxLines: Number.MAX_SAFE_INTEGER });\n\t\t\t\t\t\t\tlet output = truncation.content;\n\t\t\t\t\t\t\tconst details: GrepToolDetails = {};\n\t\t\t\t\t\t\t// Build actionable notices for truncation and match limits.\n\t\t\t\t\t\t\tconst notices: string[] = [];\n\t\t\t\t\t\t\tif (matchLimitReached) {\n\t\t\t\t\t\t\t\tnotices.push(\n\t\t\t\t\t\t\t\t\t`${effectiveLimit} matches limit reached. Use limit=${effectiveLimit * 2} for more, or refine pattern`,\n\t\t\t\t\t\t\t\t);\n\t\t\t\t\t\t\t\tdetails.matchLimitReached = effectiveLimit;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tif (truncation.truncated) {\n\t\t\t\t\t\t\t\tnotices.push(`${formatSize(DEFAULT_MAX_BYTES)} limit reached`);\n\t\t\t\t\t\t\t\tdetails.truncation = truncation;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tif (linesTruncated) {\n\t\t\t\t\t\t\t\tnotices.push(\n\t\t\t\t\t\t\t\t\t`Some lines truncated to ${GREP_MAX_LINE_LENGTH} chars. Use read tool to see full lines`,\n\t\t\t\t\t\t\t\t);\n\t\t\t\t\t\t\t\tdetails.linesTruncated = true;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tif (notices.length > 0) output += `\\n\\n[${notices.join(\". \")}]`;\n\t\t\t\t\t\t\tsettle(() =>\n\t\t\t\t\t\t\t\tresolve({\n\t\t\t\t\t\t\t\t\tcontent: [{ type: \"text\", text: output }],\n\t\t\t\t\t\t\t\t\tdetails: Object.keys(details).length > 0 ? details : undefined,\n\t\t\t\t\t\t\t\t}),\n\t\t\t\t\t\t\t);\n\t\t\t\t\t\t});\n\t\t\t\t\t} catch (err) {\n\t\t\t\t\t\tsettle(() => reject(err as Error));\n\t\t\t\t\t}\n\t\t\t\t})();\n\t\t\t});\n\t\t},\n\t\trenderCall(args, theme, context) {\n\t\t\tconst text = (context.lastComponent as Text | undefined) ?? new Text(\"\", 0, 0);\n\t\t\ttext.setText(formatGrepCall(args, theme));\n\t\t\treturn text;\n\t\t},\n\t\trenderResult(result, options, theme, context) {\n\t\t\tconst text = (context.lastComponent as Text | undefined) ?? new Text(\"\", 0, 0);\n\t\t\ttext.setText(formatGrepResult(result, options, theme, context.showImages));\n\t\t\treturn text;\n\t\t},\n\t};\n}\n\nexport function createGrepTool(cwd: string, options?: GrepToolOptions): AgentTool<typeof grepSchema> {\n\treturn wrapToolDefinition(createGrepToolDefinition(cwd, options));\n}\n"]}
|
|
1
|
+
{"version":3,"file":"grep.js","sourceRoot":"","sources":["../../../src/core/tools/grep.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,IAAI,UAAU,EAAE,IAAI,IAAI,MAAM,EAAE,MAAM,kBAAkB,CAAC;AAC1E,OAAO,EAAE,eAAe,EAAE,MAAM,eAAe,CAAC;AAEhD,OAAO,EAAE,IAAI,EAAE,MAAM,wBAAwB,CAAC;AAC9C,OAAO,EAAE,KAAK,EAAE,MAAM,eAAe,CAAC;AACtC,OAAO,IAAI,MAAM,MAAM,CAAC;AACxB,OAAO,EAAe,IAAI,EAAE,MAAM,SAAS,CAAC;AAC5C,OAAO,EAAE,OAAO,EAAE,MAAM,wDAAwD,CAAC;AACjF,OAAO,EAAE,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IAAI,CAAC,OAAO,CAAC,gBAAgB,CAAC,MAAM,EAAE,OAAO,EAAE,KAAK,EAAE,OAAO,CAAC,UAAU,CAAC,CAAC,CAAC;YAC3E,OAAO,IAAI,CAAC;QACb,CAAC;KACD,CAAC;AACH,CAAC;AAED,MAAM,UAAU,cAAc,CAAC,GAAW,EAAE,OAAyB;IACpE,OAAO,kBAAkB,CAAC,wBAAwB,CAAC,GAAG,EAAE,OAAO,CAAC,CAAC,CAAC;AACnE,CAAC","sourcesContent":["import { readFile as fsReadFile, stat as fsStat } from \"node:fs/promises\";\nimport { createInterface } from \"node:readline\";\nimport type { AgentTool } from \"@earendil-works/pi-agent-core\";\nimport { Text } from \"@earendil-works/pi-tui\";\nimport { spawn } from \"child_process\";\nimport path from \"path\";\nimport { type Static, Type } from \"typebox\";\nimport { keyHint } from \"../../modes/interactive/components/keybinding-hints.ts\";\nimport { ensureTool } from \"../../utils/tools-manager.ts\";\nimport type { ToolDefinition, ToolRenderResultOptions } from \"../extensions/types.ts\";\nimport { resolveToCwd } from \"./path-utils.ts\";\nimport { loadNativeSearchBinding, type NativeGrepMatch } from \"./search-native.ts\";\nimport { getTextOutput, invalidArgText, shortenPath, str } from \"./render-utils.ts\";\nimport { wrapToolDefinition } from \"./tool-definition-wrapper.ts\";\nimport {\n\tDEFAULT_MAX_BYTES,\n\tformatSize,\n\tGREP_MAX_LINE_LENGTH,\n\ttype TruncationResult,\n\ttruncateHead,\n\ttruncateLine,\n} from \"./truncate.ts\";\n\nconst grepSchema = Type.Object({\n\tpattern: Type.String({ description: \"Search pattern (regex or literal string)\" }),\n\tpath: Type.Optional(Type.String({ description: \"Directory or file to search (default: current directory)\" })),\n\tglob: Type.Optional(Type.String({ description: \"Filter files by glob pattern, e.g. '*.ts' or '**/*.spec.ts'\" })),\n\tignoreCase: Type.Optional(Type.Boolean({ description: \"Case-insensitive search (default: false)\" })),\n\ttype: Type.Optional(Type.String({ description: \"File type filter for native grep.\" })),\n\tliteral: Type.Optional(\n\t\tType.Boolean({ description: \"Treat pattern as literal string instead of regex (default: false)\" }),\n\t),\n\tcontext: Type.Optional(\n\t\tType.Number({ description: \"Number of lines to show before and after each match (default: 0)\" }),\n\t),\n\tcontextBefore: Type.Optional(Type.Number({ description: \"Lines to show before each match.\" })),\n\tcontextAfter: Type.Optional(Type.Number({ description: \"Lines to show after each match.\" })),\n\tlimit: Type.Optional(Type.Number({ description: \"Maximum number of matches to return (default: 100)\" })),\n\toffset: Type.Optional(Type.Number({ description: \"Skip first N matches.\" })),\n\tmode: Type.Optional(Type.Union([Type.Literal(\"content\"), Type.Literal(\"count\"), Type.Literal(\"filesWithMatches\")])),\n\tmaxCountPerFile: Type.Optional(Type.Number({ description: \"Maximum matches per file.\" })),\n\thidden: Type.Optional(Type.Boolean({ description: \"Search hidden files (default true).\" })),\n\tcache: Type.Optional(Type.Boolean({ description: \"Use native cache.\" })),\n\ttimeoutMs: Type.Optional(Type.Number({ description: \"Native grep timeout in milliseconds.\" })),\n\tgitignore: Type.Optional(Type.Boolean({ description: \"Respect .gitignore files (default: true)\" })),\n}, { additionalProperties: false });\n\nexport type GrepToolInput = Static<typeof grepSchema>;\nconst DEFAULT_LIMIT = 100;\n\nexport interface GrepToolDetails {\n\ttruncation?: TruncationResult;\n\tmatchLimitReached?: number;\n\tlinesTruncated?: boolean;\n}\n\ntype RipgrepMatchEvent = {\n\ttype: \"match\";\n\tdata?: {\n\t\tpath?: { text?: unknown };\n\t\tline_number?: unknown;\n\t\tlines?: { text?: unknown };\n\t};\n};\n\nfunction isRipgrepMatchEvent(event: unknown): event is RipgrepMatchEvent {\n\treturn typeof event === \"object\" && event !== null && \"type\" in event && event.type === \"match\";\n}\n\n/**\n * Pluggable operations for the grep tool.\n * Override these to delegate search to remote systems (for example SSH).\n */\nexport interface GrepOperations {\n\t/** Check if path is a directory. Throws if path does not exist. */\n\tisDirectory: (absolutePath: string) => Promise<boolean> | boolean;\n\t/** Read file contents for context lines */\n\treadFile: (absolutePath: string) => Promise<string> | string;\n}\n\nconst defaultGrepOperations: GrepOperations = {\n\tisDirectory: async (p) => (await fsStat(p)).isDirectory(),\n\treadFile: (p) => fsReadFile(p, \"utf-8\"),\n};\n\nexport interface GrepToolOptions {\n\t/** Custom operations for grep. Default: local filesystem plus ripgrep */\n\toperations?: GrepOperations;\n\t/** Enable shared native filesystem scan cache. Defaults to false; search callers also disable it to stay fresh across out-of-band filesystem mutations. */\n\tnativeCache?: boolean;\n}\n\nfunction formatGrepCall(\n\targs: { pattern: string; path?: string; glob?: string; limit?: number } | undefined,\n\ttheme: typeof import(\"../../modes/interactive/theme/theme.ts\").theme,\n): string {\n\tconst pattern = str(args?.pattern);\n\tconst rawPath = str(args?.path);\n\tconst path = rawPath !== null ? shortenPath(rawPath || \".\") : null;\n\tconst glob = str(args?.glob);\n\tconst limit = args?.limit;\n\tconst invalidArg = invalidArgText(theme);\n\tlet text =\n\t\ttheme.fg(\"toolTitle\", theme.bold(\"grep\")) +\n\t\t\" \" +\n\t\t(pattern === null ? invalidArg : theme.fg(\"accent\", `/${pattern || \"\"}/`)) +\n\t\ttheme.fg(\"toolOutput\", ` in ${path === null ? invalidArg : path}`);\n\tif (glob) text += theme.fg(\"toolOutput\", ` (${glob})`);\n\tif (limit !== undefined) text += theme.fg(\"toolOutput\", ` limit ${limit}`);\n\treturn text;\n}\n\nfunction formatGrepResult(\n\tresult: {\n\t\tcontent: Array<{ type: string; text?: string; data?: string; mimeType?: string }>;\n\t\tdetails?: GrepToolDetails;\n\t},\n\toptions: ToolRenderResultOptions,\n\ttheme: typeof import(\"../../modes/interactive/theme/theme.ts\").theme,\n\tshowImages: boolean,\n): string {\n\tconst output = getTextOutput(result, showImages).trim();\n\tlet text = \"\";\n\tif (output) {\n\t\tconst lines = output.split(\"\\n\");\n\t\tconst maxLines = options.expanded ? lines.length : 15;\n\t\tconst displayLines = lines.slice(0, maxLines);\n\t\tconst remaining = lines.length - maxLines;\n\t\ttext += `\\n${displayLines.map((line) => theme.fg(\"toolOutput\", line)).join(\"\\n\")}`;\n\t\tif (remaining > 0) {\n\t\t\ttext += `${theme.fg(\"muted\", `\\n... (${remaining} more lines,`)} ${keyHint(\"app.tools.expand\", \"Expand\")}${theme.fg(\"muted\", \")\")}`;\n\t\t}\n\t}\n\n\tconst matchLimit = result.details?.matchLimitReached;\n\tconst truncation = result.details?.truncation;\n\tconst linesTruncated = result.details?.linesTruncated;\n\tif (matchLimit || truncation?.truncated || linesTruncated) {\n\t\tconst warnings: string[] = [];\n\t\tif (matchLimit) warnings.push(`${matchLimit} matches limit`);\n\t\tif (truncation?.truncated) warnings.push(`${formatSize(truncation.maxBytes ?? DEFAULT_MAX_BYTES)} limit`);\n\t\tif (linesTruncated) warnings.push(\"some lines truncated\");\n\t\ttext += `\\n${theme.fg(\"warning\", `[Truncated: ${warnings.join(\", \")}]`)}`;\n\t}\n\treturn text;\n}\n\nfunction formatNativeGrepMatch(match: NativeGrepMatch, displayPath: string): string[] {\n\tconst lines: string[] = [];\n\tfor (const contextLine of match.contextBefore ?? []) {\n\t\tlines.push(`${displayPath}-${contextLine.lineNumber}- ${contextLine.line}`);\n\t}\n\tlines.push(`${displayPath}:${match.lineNumber}: ${match.line}`);\n\tfor (const contextLine of match.contextAfter ?? []) {\n\t\tlines.push(`${displayPath}-${contextLine.lineNumber}- ${contextLine.line}`);\n\t}\n\treturn lines;\n}\n\nexport function createGrepToolDefinition(\n\tcwd: string,\n\toptions?: GrepToolOptions,\n): ToolDefinition<typeof grepSchema, GrepToolDetails | undefined> {\n\tconst customOps = options?.operations;\n\treturn {\n\t\tname: \"grep\",\n\t\tlabel: \"grep\",\n\t\tdescription: `Search file contents for a pattern. Returns matching lines with file paths and line numbers. Respects .gitignore. Output is truncated to ${DEFAULT_LIMIT} matches or ${DEFAULT_MAX_BYTES / 1024}KB (whichever is hit first). Long lines are truncated to ${GREP_MAX_LINE_LENGTH} chars.`,\n\t\tpromptSnippet: \"Search file contents for patterns (respects .gitignore)\",\n\t\tparameters: grepSchema,\n\t\tasync execute(\n\t\t\t_toolCallId,\n\t\t\t{\n\t\t\t\tpattern,\n\t\t\t\tpath: searchDir,\n\t\t\t\tglob,\n\t\t\t\tignoreCase,\n\t\t\t\tliteral,\n\t\t\t\tcontext,\n\t\t\t\tlimit,\n\t\t\t\tgitignore,\n\t\t\t\ttype,\n\t\t\t\tcontextBefore,\n\t\t\t\tcontextAfter,\n\t\t\t\toffset,\n\t\t\t\tmode,\n\t\t\t\tmaxCountPerFile,\n\t\t\t\thidden,\n\t\t\t\tcache,\n\t\t\t\ttimeoutMs,\n\t\t\t}: GrepToolInput,\n\t\t\tsignal?: AbortSignal,\n\t\t\t_onUpdate?,\n\t\t\t_ctx?,\n\t\t) {\n\t\t\treturn new Promise((resolve, reject) => {\n\t\t\t\tif (signal?.aborted) {\n\t\t\t\t\treject(new Error(\"Operation aborted\"));\n\t\t\t\t\treturn;\n\t\t\t\t}\n\t\t\t\tlet settled = false;\n\t\t\t\tconst settle = (fn: () => void) => {\n\t\t\t\t\tif (!settled) {\n\t\t\t\t\t\tsettled = true;\n\t\t\t\t\t\tfn();\n\t\t\t\t\t}\n\t\t\t\t};\n\n\t\t\t\t(async () => {\n\t\t\t\t\ttry {\n\t\t\t\t\t\tconst searchPath = resolveToCwd(searchDir || \".\", cwd);\n\t\t\t\t\t\tconst ops = customOps ?? defaultGrepOperations;\n\t\t\t\t\t\tlet isDirectory: boolean;\n\t\t\t\t\t\ttry {\n\t\t\t\t\t\t\tisDirectory = await ops.isDirectory(searchPath);\n\t\t\t\t\t\t} catch {\n\t\t\t\t\t\t\tsettle(() => reject(new Error(`Path not found: ${searchPath}`)));\n\t\t\t\t\t\t\treturn;\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tconst contextValue = context && context > 0 ? context : 0;\n\t\t\t\t\t\tconst contextBeforeValue = contextBefore ?? contextValue;\n\t\t\t\t\t\tconst contextAfterValue = contextAfter ?? contextValue;\n\t\t\t\t\t\tconst nativeCache = options?.nativeCache === true;\n\t\t\t\t\t\tconst effectiveLimit = Math.max(1, limit ?? DEFAULT_LIMIT);\n\t\t\t\t\t\tconst formatPath = (filePath: string): string => {\n\t\t\t\t\t\t\tif (isDirectory) {\n\t\t\t\t\t\t\t\tif (!path.isAbsolute(filePath)) return filePath.replace(/\\\\/g, \"/\");\n\t\t\t\t\t\t\t\tconst relative = path.relative(searchPath, filePath);\n\t\t\t\t\t\t\t\tif (relative && !relative.startsWith(\"..\")) return relative.replace(/\\\\/g, \"/\");\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\treturn path.basename(filePath);\n\t\t\t\t\t\t};\n\n\t\t\t\t\t\tif (!customOps && !literal) {\n\t\t\t\t\t\t\tconst nativeBinding = loadNativeSearchBinding();\n\t\t\t\t\t\t\tif (nativeBinding) {\n\t\t\t\t\t\t\t\tconst nativeResult = await nativeBinding.grep({\n\t\t\t\t\t\t\t\t\tpattern,\n\t\t\t\t\t\t\t\t\tpath: searchPath,\n\t\t\t\t\t\t\t\t\tcwd,\n\t\t\t\t\t\t\t\t\tglob,\n\t\t\t\t\t\t\t\t\tignoreCase,\n\t\t\t\t\t\t\t\thidden: hidden ?? true,\n\t\t\t\t\t\t\t\tgitignore: gitignore !== false,\n\t\t\t\t\t\t\t\tcache: cache ?? nativeCache,\n\t\t\t\t\t\t\t\tmaxCount: mode === \"count\" ? undefined : effectiveLimit + 1,\n\t\t\t\t\t\t\t\toffset,\n\t\t\t\t\t\t\t\tcontext: contextBefore === undefined && contextAfter === undefined ? contextValue : undefined,\n\t\t\t\t\t\t\t\tcontextBefore,\n\t\t\t\t\t\t\t\tcontextAfter,\n\t\t\t\t\t\t\t\ttype,\n\t\t\t\t\t\t\t\tmode,\n\t\t\t\t\t\t\t\tmaxCountPerFile,\n\t\t\t\t\t\t\t\tmaxColumns: GREP_MAX_LINE_LENGTH,\n\t\t\t\t\t\t\t\tmultiline: pattern.includes(\"\\n\") || pattern.includes(\"\\\\n\"),\n\t\t\t\t\t\t\t\tsignal,\n\t\t\t\t\t\t\t\ttimeoutMs: timeoutMs ?? 30_000,\n\t\t\t\t\t\t\t\t});\n\t\t\t\t\t\t\t\tif (nativeResult.error) throw new Error(nativeResult.error);\n\t\t\t\t\t\t\t\tif (nativeResult.matches.length === 0) {\n\t\t\t\t\t\t\t\t\tsettle(() => resolve({ content: [{ type: \"text\", text: mode === \"count\" ? \"0\" : \"No matches found\" }], details: undefined }));\n\t\t\t\t\t\t\t\t\treturn;\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\tconst visibleMatches = nativeResult.matches.slice(0, effectiveLimit);\n\t\t\t\t\t\t\t\tconst filesOutput = mode === \"filesWithMatches\" && !isDirectory && (offset ?? 0) > 0 ? \"\" : visibleMatches.map((match) => formatPath(match.path)).join(\"\\n\");\n\t\t\t\t\t\t\t\tconst countOutput = String(Math.max(0, nativeResult.totalMatches - (offset ?? 0)));\n\t\t\t\t\t\t\t\tconst rawOutput = mode === \"count\" ? countOutput : mode === \"filesWithMatches\" ? (filesOutput || \"No matches found\") : visibleMatches.flatMap((match) => formatNativeGrepMatch(match, formatPath(match.path))).join(\"\\n\");\n\t\t\t\t\t\t\t\tconst truncation = truncateHead(rawOutput, { maxLines: Number.MAX_SAFE_INTEGER });\n\t\t\t\t\t\t\t\tlet output = truncation.content;\n\t\t\t\t\t\t\t\tconst details: GrepToolDetails = {};\n\t\t\t\t\t\t\t\tconst notices: string[] = [];\n\t\t\t\t\t\t\t\tif (nativeResult.matches.length > effectiveLimit || nativeResult.limitReached) {\n\t\t\t\t\t\t\t\t\tnotices.push(`${effectiveLimit} matches limit reached. Use limit=${effectiveLimit * 2} for more, or refine pattern`);\n\t\t\t\t\t\t\t\t\tdetails.matchLimitReached = effectiveLimit;\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\tif (truncation.truncated) {\n\t\t\t\t\t\t\t\t\tnotices.push(`${formatSize(DEFAULT_MAX_BYTES)} limit reached`);\n\t\t\t\t\t\t\t\t\tdetails.truncation = truncation;\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\tif (visibleMatches.some((match) => match.truncated)) {\n\t\t\t\t\t\t\t\t\tnotices.push(`Some lines truncated to ${GREP_MAX_LINE_LENGTH} chars. Use read tool to see full lines`);\n\t\t\t\t\t\t\t\t\tdetails.linesTruncated = true;\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\tif (notices.length > 0) output += `\\n\\n[${notices.join(\". \")}]`;\n\t\t\t\t\t\t\t\tsettle(() => resolve({ content: [{ type: \"text\", text: output }], details: Object.keys(details).length > 0 ? details : undefined }));\n\t\t\t\t\t\t\t\treturn;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\n\n\n\t\t\t\t\t\tconst fileCache = new Map<string, string[]>();\n\t\t\t\t\t\tconst getFileLines = async (filePath: string): Promise<string[]> => {\n\t\t\t\t\t\t\tlet lines = fileCache.get(filePath);\n\t\t\t\t\t\t\tif (!lines) {\n\t\t\t\t\t\t\t\ttry {\n\t\t\t\t\t\t\t\t\tconst content = await ops.readFile(filePath);\n\t\t\t\t\t\t\t\t\tlines = content.replace(/\\r\\n/g, \"\\n\").replace(/\\r/g, \"\\n\").split(\"\\n\");\n\t\t\t\t\t\t\t\t} catch {\n\t\t\t\t\t\t\t\t\tlines = [];\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\tfileCache.set(filePath, lines);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\treturn lines;\n\t\t\t\t\t\t};\n\n\t\t\t\t\t\tconst rgPath = await ensureTool(\"rg\", true);\n\t\t\t\t\t\tif (!rgPath) {\n\t\t\t\t\t\t\tsettle(() => reject(new Error(\"ripgrep (rg) is not available and could not be downloaded\")));\n\t\t\t\t\t\t\treturn;\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tconst args: string[] = [\"--json\", \"--line-number\", \"--color=never\"];\n\t\t\t\t\t\tif (hidden !== false) args.push(\"--hidden\");\n\t\t\t\t\t\tif (gitignore === false) args.push(\"--no-ignore\");\n\t\t\t\t\t\tif (ignoreCase) args.push(\"--ignore-case\");\n\t\t\t\t\t\tif (literal) args.push(\"--fixed-strings\");\n\t\t\t\t\t\tif (pattern.includes(\"\\n\") || pattern.includes(\"\\\\n\")) args.push(\"--multiline\");\n\t\t\t\t\t\tif (type) args.push(\"--type\", type);\n\t\t\t\t\t\tif (maxCountPerFile !== undefined) args.push(\"--max-count\", String(maxCountPerFile));\n\t\t\t\t\t\tif (glob) args.push(\"--glob\", glob);\n\t\t\t\t\t\targs.push(\"--\", pattern, searchPath);\n\n\t\t\t\t\t\tconst child = spawn(rgPath, args, { stdio: [\"ignore\", \"pipe\", \"pipe\"] });\n\t\t\t\t\t\tconst timeoutTimer = timeoutMs !== undefined ? setTimeout(() => stopChild(), timeoutMs) : undefined;\n\t\t\t\t\t\tconst rl = createInterface({ input: child.stdout });\n\t\t\t\t\t\tlet stderr = \"\";\n\t\t\t\t\t\tlet matchCount = 0;\n\t\t\t\t\t\tlet matchLimitReached = false;\n\t\t\t\t\t\tlet linesTruncated = false;\n\t\t\t\t\t\tlet aborted = false;\n\t\t\t\t\t\tlet killedDueToLimit = false;\n\t\t\t\t\t\tconst outputLines: string[] = [];\n\n\t\t\t\t\t\tconst cleanup = () => {\n\t\t\t\t\t\t\tif (timeoutTimer) clearTimeout(timeoutTimer);\n\t\t\t\t\t\t\trl.close();\n\t\t\t\t\t\t\tsignal?.removeEventListener(\"abort\", onAbort);\n\t\t\t\t\t\t};\n\t\t\t\t\t\tconst stopChild = (dueToLimit = false) => {\n\t\t\t\t\t\t\tif (!child.killed) {\n\t\t\t\t\t\t\t\tkilledDueToLimit = dueToLimit;\n\t\t\t\t\t\t\t\tchild.kill();\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t};\n\t\t\t\t\t\tconst onAbort = () => {\n\t\t\t\t\t\t\taborted = true;\n\t\t\t\t\t\t\tstopChild();\n\t\t\t\t\t\t};\n\t\t\t\t\t\tsignal?.addEventListener(\"abort\", onAbort, { once: true });\n\t\t\t\t\t\tchild.stderr?.on(\"data\", (chunk) => {\n\t\t\t\t\t\t\tstderr += chunk.toString();\n\t\t\t\t\t\t});\n\n\t\t\t\t\t\tconst formatBlock = async (filePath: string, lineNumber: number): Promise<string[]> => {\n\t\t\t\t\t\t\tconst relativePath = formatPath(filePath);\n\t\t\t\t\t\t\tconst lines = await getFileLines(filePath);\n\t\t\t\t\t\t\tif (!lines.length) return [`${relativePath}:${lineNumber}: (unable to read file)`];\n\t\t\t\t\t\t\tconst block: string[] = [];\n\t\t\t\t\t\t\tconst start = contextBeforeValue > 0 ? Math.max(1, lineNumber - contextBeforeValue) : lineNumber;\n\t\t\t\t\t\t\tconst end = contextAfterValue > 0 ? Math.min(lines.length, lineNumber + contextAfterValue) : lineNumber;\n\t\t\t\t\t\t\tfor (let current = start; current <= end; current++) {\n\t\t\t\t\t\t\t\tconst lineText = lines[current - 1] ?? \"\";\n\t\t\t\t\t\t\t\tconst sanitized = lineText.replace(/\\r/g, \"\");\n\t\t\t\t\t\t\t\tconst isMatchLine = current === lineNumber;\n\t\t\t\t\t\t\t\t// Truncate long lines so grep output stays compact.\n\t\t\t\t\t\t\t\tconst { text: truncatedText, wasTruncated } = truncateLine(sanitized);\n\t\t\t\t\t\t\t\tif (wasTruncated) linesTruncated = true;\n\t\t\t\t\t\t\t\tif (isMatchLine) block.push(`${relativePath}:${current}: ${truncatedText}`);\n\t\t\t\t\t\t\t\telse block.push(`${relativePath}-${current}- ${truncatedText}`);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\treturn block;\n\t\t\t\t\t\t};\n\n\t\t\t\t\t\t// Collect matches during streaming, then format them after rg exits.\n\t\t\t\t\t\tconst matches: Array<{ filePath: string; lineNumber: number; lineText?: string }> = [];\n\t\t\t\t\t\tlet seenMatches = 0, seenFiles = 0; const seenFilePaths = new Set<string>(), filesWithMatches = new Set<string>();\n\t\t\t\t\t\trl.on(\"line\", (line) => {\n\t\t\t\t\t\t\tif (!line.trim() || (mode !== \"count\" && mode !== \"filesWithMatches\" && matchCount >= effectiveLimit)) return;\n\t\t\t\t\t\t\tlet event: unknown;\n\t\t\t\t\t\t\ttry {\n\t\t\t\t\t\t\t\tevent = JSON.parse(line) as unknown;\n\t\t\t\t\t\t\t} catch { return; }\n\t\t\t\t\t\t\tif (isRipgrepMatchEvent(event)) {\n\t\t\t\t\t\t\t\tconst filePath = event.data?.path?.text;\n\t\t\t\t\t\t\t\tif (mode === \"filesWithMatches\") { if (typeof filePath === \"string\") { const formatted = formatPath(filePath); if (!seenFilePaths.has(formatted)) { seenFilePaths.add(formatted); seenFiles++; if (offset === undefined || seenFiles > offset) filesWithMatches.add(formatted); } } matchCount = filesWithMatches.size; if (matchCount >= effectiveLimit) { matchLimitReached = true; stopChild(true); } return; }\n\t\t\t\t\t\t\t\tseenMatches++;\n\t\t\t\t\t\t\t\tif (mode !== \"count\" && offset !== undefined && seenMatches <= offset) return;\n\t\t\t\t\t\t\t\tmatchCount++;\n\t\t\t\t\t\t\t\tconst lineNumber = event.data?.line_number;\n\t\t\t\t\t\t\t\tconst lineText = event.data?.lines?.text;\n\t\t\t\t\t\t\t\tif (typeof filePath === \"string\") filesWithMatches.add(formatPath(filePath));\n\t\t\t\t\t\t\t\tif (typeof filePath === \"string\" && typeof lineNumber === \"number\")\n\t\t\t\t\t\t\t\t\tmatches.push({ filePath, lineNumber, lineText: typeof lineText === \"string\" ? lineText : undefined });\n\t\t\t\t\t\t\t\tif (mode !== \"count\" && matchCount >= effectiveLimit) { matchLimitReached = true; stopChild(true); }\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t});\n\n\t\t\t\t\t\tchild.on(\"error\", (error) => {\n\t\t\t\t\t\t\tcleanup();\n\t\t\t\t\t\t\tsettle(() => reject(new Error(`Failed to run ripgrep: ${error.message}`)));\n\t\t\t\t\t\t});\n\t\t\t\t\t\tchild.on(\"close\", async (code) => {\n\t\t\t\t\t\t\tcleanup();\n\t\t\t\t\t\t\tif (aborted) {\n\t\t\t\t\t\t\t\tsettle(() => reject(new Error(\"Operation aborted\")));\n\t\t\t\t\t\t\t\treturn;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tif (!killedDueToLimit && code !== 0 && code !== 1) {\n\t\t\t\t\t\t\t\tconst errorMsg = stderr.trim() || `ripgrep exited with code ${code}`;\n\t\t\t\t\t\t\t\tsettle(() => reject(new Error(errorMsg)));\n\t\t\t\t\t\t\t\treturn;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tif (matchCount === 0) {\n\t\t\t\t\t\t\t\tsettle(() => resolve({ content: [{ type: \"text\", text: mode === \"count\" ? \"0\" : \"No matches found\" }], details: undefined }));\n\t\t\t\t\t\t\t\treturn;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tif (mode === \"count\" || mode === \"filesWithMatches\") {\n\t\t\t\t\t\t\t\tsettle(() => resolve({ content: [{ type: \"text\", text: mode === \"count\" ? String(Math.max(0, matchCount - (offset ?? 0))) : ([...filesWithMatches].join(\"\\n\") || \"No matches found\") }], details: matchLimitReached ? { matchLimitReached: effectiveLimit } : undefined }));\n\t\t\t\t\t\t\t\treturn;\n\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\t// Format matches after streaming finishes so custom readFile() backends can be async.\n\t\t\t\t\t\t\tfor (const match of matches) {\n\t\t\t\t\t\t\t\tif (contextBeforeValue === 0 && contextAfterValue === 0 && match.lineText !== undefined) {\n\t\t\t\t\t\t\t\t\tconst relativePath = formatPath(match.filePath);\n\t\t\t\t\t\t\t\t\tconst sanitized = match.lineText\n\t\t\t\t\t\t\t\t\t\t.replace(/\\r\\n/g, \"\\n\")\n\t\t\t\t\t\t\t\t\t\t.replace(/\\r/g, \"\")\n\t\t\t\t\t\t\t\t\t\t.replace(/\\n$/, \"\");\n\t\t\t\t\t\t\t\t\tconst { text: truncatedText, wasTruncated } = truncateLine(sanitized);\n\t\t\t\t\t\t\t\t\tif (wasTruncated) linesTruncated = true;\n\t\t\t\t\t\t\t\t\toutputLines.push(`${relativePath}:${match.lineNumber}: ${truncatedText}`);\n\t\t\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t\t\tconst block = await formatBlock(match.filePath, match.lineNumber);\n\t\t\t\t\t\t\t\t\toutputLines.push(...block);\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\tconst rawOutput = outputLines.join(\"\\n\");\n\t\t\t\t\t\t\t// Apply byte truncation. There is no line limit here because the match limit already capped rows.\n\t\t\t\t\t\t\tconst truncation = truncateHead(rawOutput, { maxLines: Number.MAX_SAFE_INTEGER });\n\t\t\t\t\t\t\tlet output = truncation.content;\n\t\t\t\t\t\t\tconst details: GrepToolDetails = {};\n\t\t\t\t\t\t\t// Build actionable notices for truncation and match limits.\n\t\t\t\t\t\t\tconst notices: string[] = [];\n\t\t\t\t\t\t\tif (matchLimitReached) {\n\t\t\t\t\t\t\t\tnotices.push(\n\t\t\t\t\t\t\t\t\t`${effectiveLimit} matches limit reached. Use limit=${effectiveLimit * 2} for more, or refine pattern`,\n\t\t\t\t\t\t\t\t);\n\t\t\t\t\t\t\t\tdetails.matchLimitReached = effectiveLimit;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tif (truncation.truncated) {\n\t\t\t\t\t\t\t\tnotices.push(`${formatSize(DEFAULT_MAX_BYTES)} limit reached`);\n\t\t\t\t\t\t\t\tdetails.truncation = truncation;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tif (linesTruncated) {\n\t\t\t\t\t\t\t\tnotices.push(\n\t\t\t\t\t\t\t\t\t`Some lines truncated to ${GREP_MAX_LINE_LENGTH} chars. Use read tool to see full lines`,\n\t\t\t\t\t\t\t\t);\n\t\t\t\t\t\t\t\tdetails.linesTruncated = true;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tif (notices.length > 0) output += `\\n\\n[${notices.join(\". \")}]`;\n\t\t\t\t\t\t\tsettle(() =>\n\t\t\t\t\t\t\t\tresolve({\n\t\t\t\t\t\t\t\t\tcontent: [{ type: \"text\", text: output }],\n\t\t\t\t\t\t\t\t\tdetails: Object.keys(details).length > 0 ? details : undefined,\n\t\t\t\t\t\t\t\t}),\n\t\t\t\t\t\t\t);\n\t\t\t\t\t\t});\n\t\t\t\t\t} catch (err) {\n\t\t\t\t\t\tsettle(() => reject(err as Error));\n\t\t\t\t\t}\n\t\t\t\t})();\n\t\t\t});\n\t\t},\n\t\trenderCall(args, theme, context) {\n\t\t\tconst text = (context.lastComponent as Text | undefined) ?? new Text(\"\", 0, 0);\n\t\t\ttext.setText(formatGrepCall(args, theme));\n\t\t\treturn text;\n\t\t},\n\t\trenderResult(result, options, theme, context) {\n\t\t\tconst text = (context.lastComponent as Text | undefined) ?? new Text(\"\", 0, 0);\n\t\t\ttext.setText(formatGrepResult(result, options, theme, context.showImages));\n\t\t\treturn text;\n\t\t},\n\t};\n}\n\nexport function createGrepTool(cwd: string, options?: GrepToolOptions): AgentTool<typeof grepSchema> {\n\treturn wrapToolDefinition(createGrepToolDefinition(cwd, options));\n}\n"]}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { ApplyResult, Edit } from "./types.js";
|
|
2
|
+
/** A line that is nothing but closing delimiters: `}`, `)`, `];`, `})`, `},`. */
|
|
3
|
+
export declare const STRUCTURAL_CLOSER_RE: RegExp;
|
|
4
|
+
/**
|
|
5
|
+
* Apply a parsed list of edits to a text body. Pure function — no I/O.
|
|
6
|
+
*
|
|
7
|
+
* Returns the post-edit text and the first changed line number (1-indexed).
|
|
8
|
+
* Throws if an anchor is out of bounds.
|
|
9
|
+
*/
|
|
10
|
+
export declare function applyEdits(text: string, edits: readonly Edit[]): ApplyResult;
|
|
11
|
+
//# sourceMappingURL=apply.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"apply.d.ts","sourceRoot":"","sources":["../../../../src/core/tools/hashline-engine/apply.ts"],"names":[],"mappings":"AAaA,OAAO,KAAK,EAAU,WAAW,EAAU,IAAI,EAAE,MAAM,YAAY,CAAC;AAsHpE,iFAAiF;AACjF,eAAO,MAAM,oBAAoB,QAAyB,CAAC;AAklB3D;;;;;GAKG;AACH,wBAAgB,UAAU,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,SAAS,IAAI,EAAE,GAAG,WAAW,CAqG5E","sourcesContent":["// @generated vendored verbatim from oh-my-pi packages/hashline @ 15b5c1397fc -- DO NOT EDIT.\n// Parity source for the Atomic hashline edit engine (issue #1483); adapted only for Atomic's Node runtime (relative imports, Bun->Node host calls, erasable constructor syntax).\n/**\n * Apply a parsed list of {@link Edit}s to a text body and return the\n * post-edit lines plus any diagnostic warnings. Pure function: no FS, no\n * mutation of the input.\n *\n * Replacement groups are first normalized by {@link repairReplacementBoundaries},\n * which absorbs common model mistakes where a payload restates unchanged range\n * boundaries or duplicates/drops structural closers.\n */\nimport { afterInsertLandingShiftWarning, blockInsertLandingShiftWarning, UNRESOLVED_BLOCK_INTERNAL } from \"./messages.js\";\nimport { cloneCursor } from \"./tokenizer.js\";\nimport type { Anchor, ApplyResult, Cursor, Edit } from \"./types.js\";\n\ntype LineOrigin = \"original\" | \"insert\" | \"replacement\";\n\ntype InsertEdit = Extract<Edit, { kind: \"insert\" }>;\ntype DeleteEdit = Extract<Edit, { kind: \"delete\" }>;\ntype AppliedEdit = InsertEdit | DeleteEdit;\n\ninterface IndexedEdit {\n\tedit: AppliedEdit;\n\tidx: number;\n}\n\nfunction isReplacementInsert(edit: Edit): edit is InsertEdit & { mode: \"replacement\" } {\n\treturn edit.kind === \"insert\" && edit.mode === \"replacement\";\n}\n\nfunction getCursorAnchors(cursor: Cursor): Anchor[] {\n\treturn cursor.kind === \"before_anchor\" || cursor.kind === \"after_anchor\" ? [cursor.anchor] : [];\n}\n\nfunction getEditAnchors(edit: AppliedEdit): Anchor[] {\n\tif (edit.kind === \"delete\") return [edit.anchor];\n\treturn getCursorAnchors(edit.cursor);\n}\n\nfunction trailingPhantomLine(fileLines: readonly string[]): number {\n\t// `split(\"\\n\")` on a newline-terminated file yields a trailing \"\" sentinel.\n\t// It is addressable for inserts (append-past-end), but it is not real\n\t// content. Deleting it only strips the file's final newline, so ignore delete\n\t// edits that land there; inclusive ranges ending at EOF then do the intended\n\t// thing and delete through the last concrete line.\n\treturn fileLines.length > 1 && fileLines[fileLines.length - 1] === \"\" ? fileLines.length : 0;\n}\n\nfunction dropTrailingPhantomDeletes(edits: AppliedEdit[], fileLines: readonly string[]): AppliedEdit[] {\n\tconst phantomLine = trailingPhantomLine(fileLines);\n\tif (phantomLine === 0) return edits;\n\treturn edits.filter(edit => edit.kind !== \"delete\" || edit.anchor.line !== phantomLine);\n}\n\n/**\n * Verify every anchored edit points at an existing line. File-version binding is\n * checked once per section via the header hash before this function runs.\n */\nfunction validateLineBounds(edits: readonly AppliedEdit[], fileLines: readonly string[]): void {\n\tfor (const edit of edits) {\n\t\tfor (const anchor of getEditAnchors(edit)) {\n\t\t\tif (anchor.line < 1 || anchor.line > fileLines.length) {\n\t\t\t\tthrow new Error(`Line ${anchor.line} does not exist (file has ${fileLines.length} lines)`);\n\t\t\t}\n\t\t}\n\t}\n}\n\nfunction cloneAppliedEdit(edit: AppliedEdit, index: number): AppliedEdit {\n\tif (edit.kind === \"delete\") return { ...edit, anchor: { ...edit.anchor }, index };\n\treturn { ...edit, cursor: cloneCursor(edit.cursor), index };\n}\n\nfunction insertAtStart(fileLines: string[], lineOrigins: LineOrigin[], lines: string[]): void {\n\tif (lines.length === 0) return;\n\tconst origins = lines.map((): LineOrigin => \"insert\");\n\tif (fileLines.length === 1 && fileLines[0] === \"\") {\n\t\tfileLines.splice(0, 1, ...lines);\n\t\tlineOrigins.splice(0, 1, ...origins);\n\t\treturn;\n\t}\n\tfileLines.splice(0, 0, ...lines);\n\tlineOrigins.splice(0, 0, ...origins);\n}\n\nfunction insertAtEnd(fileLines: string[], lineOrigins: LineOrigin[], lines: string[]): number | undefined {\n\tif (lines.length === 0) return undefined;\n\tconst origins = lines.map((): LineOrigin => \"insert\");\n\tif (fileLines.length === 1 && fileLines[0] === \"\") {\n\t\tfileLines.splice(0, 1, ...lines);\n\t\tlineOrigins.splice(0, 1, ...origins);\n\t\treturn 1;\n\t}\n\tconst hasTrailingNewline = fileLines.length > 0 && fileLines[fileLines.length - 1] === \"\";\n\tconst insertIndex = hasTrailingNewline ? fileLines.length - 1 : fileLines.length;\n\tfileLines.splice(insertIndex, 0, ...lines);\n\tlineOrigins.splice(insertIndex, 0, ...origins);\n\treturn insertIndex + 1;\n}\n\nfunction bucketAnchorEditsByLine(edits: IndexedEdit[]): Map<number, IndexedEdit[]> {\n\tconst byLine = new Map<number, IndexedEdit[]>();\n\tfor (const entry of edits) {\n\t\tconst line =\n\t\t\tentry.edit.kind === \"delete\"\n\t\t\t\t? entry.edit.anchor.line\n\t\t\t\t: entry.edit.cursor.kind === \"before_anchor\" || entry.edit.cursor.kind === \"after_anchor\"\n\t\t\t\t\t? entry.edit.cursor.anchor.line\n\t\t\t\t\t: 0;\n\t\tconst bucket = byLine.get(line);\n\t\tif (bucket) bucket.push(entry);\n\t\telse byLine.set(line, [entry]);\n\t}\n\treturn byLine;\n}\n\n// ═══════════════════════════════════════════════════════════════════════════\n// Replacement-boundary repair\n//\n// Models routinely miscount a replacement range's edges. Sometimes the payload\n// re-states unchanged lines that still live on both sides of the range\n// (duplicating a function header and final statement); sometimes it only\n// re-states or omits a structural closer, which leaves delimiter balance broken.\n//\n// A balance-neutral boundary-echo repair fires only when both the leading and\n// trailing payload edges are exact copies of the surviving lines outside the\n// range. One-sided content echoes are left alone unless delimiter-balance repair\n// proves they are duplicated structural boundaries. This preserves intended\n// duplicate statements while absorbing the common \"body includes the unchanged\n// wrapper\" mistake.\n\n/** A line that is nothing but closing delimiters: `}`, `)`, `];`, `})`, `},`. */\nexport const STRUCTURAL_CLOSER_RE = /^\\s*[)\\]}]+[;,]?\\s*$/;\n\ninterface DelimiterBalance {\n\tparen: number;\n\tbracket: number;\n\tbrace: number;\n}\n\n/**\n * Net `()` / `[]` / `{}` delta across `lines`, skipping delimiters inside line\n * comments (`//`), block comments, and string/template literals. Block-comment\n * and backtick-template state carry across lines; `\"` / `'` reset at EOL since\n * they cannot span lines. Deliberately language-light: constructs it cannot\n * classify (e.g. regex literals) are counted naively, which can only suppress a\n * repair (the safe direction), never force one.\n */\nfunction computeDelimiterBalance(lines: readonly string[]): DelimiterBalance {\n\tconst balance: DelimiterBalance = { paren: 0, bracket: 0, brace: 0 };\n\tlet inBlockComment = false;\n\tlet quote = \"\";\n\tfor (const line of lines) {\n\t\tfor (let i = 0; i < line.length; i++) {\n\t\t\tconst ch = line[i];\n\t\t\tif (inBlockComment) {\n\t\t\t\tif (ch === \"*\" && line[i + 1] === \"/\") {\n\t\t\t\t\tinBlockComment = false;\n\t\t\t\t\ti++;\n\t\t\t\t}\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tif (quote) {\n\t\t\t\tif (ch === \"\\\\\") i++;\n\t\t\t\telse if (ch === quote) quote = \"\";\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tif (ch === '\"' || ch === \"'\" || ch === \"`\") {\n\t\t\t\tquote = ch;\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tif (ch === \"/\" && line[i + 1] === \"/\") break;\n\t\t\tif (ch === \"/\" && line[i + 1] === \"*\") {\n\t\t\t\tinBlockComment = true;\n\t\t\t\ti++;\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tswitch (ch) {\n\t\t\t\tcase \"(\":\n\t\t\t\t\tbalance.paren++;\n\t\t\t\t\tbreak;\n\t\t\t\tcase \")\":\n\t\t\t\t\tbalance.paren--;\n\t\t\t\t\tbreak;\n\t\t\t\tcase \"[\":\n\t\t\t\t\tbalance.bracket++;\n\t\t\t\t\tbreak;\n\t\t\t\tcase \"]\":\n\t\t\t\t\tbalance.bracket--;\n\t\t\t\t\tbreak;\n\t\t\t\tcase \"{\":\n\t\t\t\t\tbalance.brace++;\n\t\t\t\t\tbreak;\n\t\t\t\tcase \"}\":\n\t\t\t\t\tbalance.brace--;\n\t\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\t// `\"` / `'` cannot span lines; only backtick templates and block comments do.\n\t\tif (quote === '\"' || quote === \"'\") quote = \"\";\n\t}\n\treturn balance;\n}\n\nfunction balanceDelta(a: DelimiterBalance, b: DelimiterBalance): DelimiterBalance {\n\treturn { paren: a.paren - b.paren, bracket: a.bracket - b.bracket, brace: a.brace - b.brace };\n}\n\nfunction balanceNegate(a: DelimiterBalance): DelimiterBalance {\n\treturn { paren: -a.paren, bracket: -a.bracket, brace: -a.brace };\n}\n\nfunction balanceEqual(a: DelimiterBalance, b: DelimiterBalance): boolean {\n\treturn a.paren === b.paren && a.bracket === b.bracket && a.brace === b.brace;\n}\n\nfunction balanceIsZero(a: DelimiterBalance): boolean {\n\treturn a.paren === 0 && a.bracket === 0 && a.brace === 0;\n}\n\ninterface ReplacementGroup {\n\t/** Positions in the edit array of the payload inserts, in payload order. */\n\tinsertIndices: number[];\n\t/** Positions in the edit array of the range deletes, ascending by line. */\n\tdeleteIndices: number[];\n\tpayload: string[];\n\t/** First deleted line (1-indexed). */\n\tstartLine: number;\n\t/** Last deleted line (1-indexed). */\n\tendLine: number;\n}\n\n/**\n * Detect a replacement group starting at `start`: a run of `before_anchor`\n * replacement inserts sharing one source op line, immediately followed by the\n * contiguous range deletes for that same op. Mirrors how the parser lowers an\n * `replace N..M:` hunk with a body.\n */\nfunction findReplacementGroup(edits: readonly AppliedEdit[], start: number): ReplacementGroup | undefined {\n\tconst first = edits[start];\n\tif (first?.kind !== \"insert\" || first.mode !== \"replacement\" || first.cursor.kind !== \"before_anchor\") {\n\t\treturn undefined;\n\t}\n\tconst { lineNum } = first;\n\tconst anchorLine = first.cursor.anchor.line;\n\tconst insertIndices: number[] = [];\n\tconst payload: string[] = [];\n\tlet i = start;\n\tfor (; i < edits.length; i++) {\n\t\tconst edit = edits[i];\n\t\tif (edit.kind !== \"insert\" || edit.mode !== \"replacement\" || edit.lineNum !== lineNum) break;\n\t\tif (edit.cursor.kind !== \"before_anchor\" || edit.cursor.anchor.line !== anchorLine) break;\n\t\tinsertIndices.push(i);\n\t\tpayload.push(edit.text);\n\t}\n\tconst deleteIndices: number[] = [];\n\tlet expectedLine = anchorLine;\n\tfor (; i < edits.length; i++) {\n\t\tconst edit = edits[i];\n\t\tif (edit.kind !== \"delete\" || edit.lineNum !== lineNum || edit.anchor.line !== expectedLine) break;\n\t\tdeleteIndices.push(i);\n\t\texpectedLine++;\n\t}\n\tif (deleteIndices.length === 0) return undefined;\n\treturn {\n\t\tinsertIndices,\n\t\tdeleteIndices,\n\t\tpayload,\n\t\tstartLine: anchorLine,\n\t\tendLine: anchorLine + deleteIndices.length - 1,\n\t};\n}\n\n/**\n * Largest `k` such that the payload's last `k` lines exactly equal the `k`\n * surviving file lines just below the range AND dropping them zeroes `delta`.\n * Requires a non-zero `delta`: a zero-balance candidate can never account for\n * the imbalance, so intentional duplicates of ordinary statements stay intact,\n * while duplicated structural lines (closers like `});`, openers like `foo(`)\n * are dropped when they exactly explain the imbalance.\n */\nfunction findDuplicateSuffix(group: ReplacementGroup, fileLines: readonly string[], delta: DelimiterBalance): number {\n\tif (balanceIsZero(delta)) return 0;\n\tconst { payload, endLine } = group;\n\tconst maxK = Math.min(payload.length, fileLines.length - endLine);\n\tfor (let k = maxK; k >= 1; k--) {\n\t\tlet matches = true;\n\t\tfor (let t = 0; t < k; t++) {\n\t\t\tif (payload[payload.length - k + t] !== fileLines[endLine + t]) {\n\t\t\t\tmatches = false;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\tif (!matches) continue;\n\t\tif (balanceEqual(computeDelimiterBalance(payload.slice(payload.length - k)), delta)) return k;\n\t}\n\treturn 0;\n}\n\n/**\n * Largest `j` such that the payload's first `j` lines exactly equal the `j`\n * surviving file lines just above the range AND dropping them zeroes `delta`.\n * Requires a non-zero `delta`; see {@link findDuplicateSuffix}.\n */\nfunction findDuplicatePrefix(group: ReplacementGroup, fileLines: readonly string[], delta: DelimiterBalance): number {\n\tif (balanceIsZero(delta)) return 0;\n\tconst { payload, startLine } = group;\n\tconst maxJ = Math.min(payload.length, startLine - 1);\n\tfor (let j = maxJ; j >= 1; j--) {\n\t\tlet matches = true;\n\t\tfor (let t = 0; t < j; t++) {\n\t\t\tif (payload[t] !== fileLines[startLine - 1 - j + t]) {\n\t\t\t\tmatches = false;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\tif (!matches) continue;\n\t\tif (balanceEqual(computeDelimiterBalance(payload.slice(0, j)), delta)) return j;\n\t}\n\treturn 0;\n}\n\n/**\n * Smallest `m` such that the range's last `m` deleted lines are all pure\n * structural closers and sparing them (keeping instead of deleting) zeroes\n * `delta`. The mirror mistake: a range that swallows a closing delimiter the\n * payload never restates.\n */\nfunction findDroppedSuffixClosers(\n\tgroup: ReplacementGroup,\n\tfileLines: readonly string[],\n\tdelta: DelimiterBalance,\n): number {\n\tconst wanted = balanceNegate(delta);\n\tconst maxM = group.deleteIndices.length;\n\tfor (let m = 1; m <= maxM; m++) {\n\t\tif (!STRUCTURAL_CLOSER_RE.test(fileLines[group.endLine - m] ?? \"\")) break;\n\t\tif (balanceEqual(computeDelimiterBalance(fileLines.slice(group.endLine - m, group.endLine)), wanted)) return m;\n\t}\n\treturn 0;\n}\n\ninterface BoundaryEcho {\n\tleading: number;\n\ttrailing: number;\n}\n\nfunction hasNonWhitespace(text: string): boolean {\n\tfor (let i = 0; i < text.length; i++) {\n\t\tconst code = text.charCodeAt(i);\n\t\tif (code !== 9 && code !== 10 && code !== 11 && code !== 12 && code !== 13 && code !== 32) return true;\n\t}\n\treturn false;\n}\n\nfunction countDuplicateLeadingBoundaryLines(group: ReplacementGroup, fileLines: readonly string[]): number {\n\tconst { payload, startLine } = group;\n\tconst max = Math.min(payload.length, startLine - 1);\n\tfor (let count = max; count >= 1; count--) {\n\t\tlet matches = true;\n\t\tlet hasContent = false;\n\t\tfor (let offset = 0; offset < count; offset++) {\n\t\t\tconst line = payload[offset];\n\t\t\tif (line !== fileLines[startLine - 1 - count + offset]) {\n\t\t\t\tmatches = false;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\thasContent ||= hasNonWhitespace(line);\n\t\t}\n\t\tif (matches && hasContent) return count;\n\t}\n\treturn 0;\n}\n\nfunction countDuplicateTrailingBoundaryLines(group: ReplacementGroup, fileLines: readonly string[]): number {\n\tconst { payload, endLine } = group;\n\tconst max = Math.min(payload.length, fileLines.length - endLine);\n\tfor (let count = max; count >= 1; count--) {\n\t\tlet matches = true;\n\t\tlet hasContent = false;\n\t\tfor (let offset = 0; offset < count; offset++) {\n\t\t\tconst line = payload[payload.length - count + offset];\n\t\t\tif (line !== fileLines[endLine + offset]) {\n\t\t\t\tmatches = false;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\thasContent ||= hasNonWhitespace(line);\n\t\t}\n\t\tif (matches && hasContent) return count;\n\t}\n\treturn 0;\n}\n\nfunction findBoundaryEcho(group: ReplacementGroup, fileLines: readonly string[]): BoundaryEcho | undefined {\n\tconst leadingMax = countDuplicateLeadingBoundaryLines(group, fileLines);\n\tif (leadingMax === 0) return undefined;\n\tconst trailingMax = countDuplicateTrailingBoundaryLines(group, fileLines);\n\tif (trailingMax === 0) return undefined;\n\t// Bail when every payload line could be claimed by a boundary echo: any\n\t// repair would strip explicit replacement content with no signal that the\n\t// payload was a mistake rather than an intentional duplication.\n\tif (leadingMax + trailingMax >= group.payload.length) return undefined;\n\t// Balance-neutrality guard (see header comment): the dropped echo lines must\n\t// either be delimiter-neutral on their own or exactly cancel the payload/range\n\t// balance delta. In brace-heavy code where bare closer lines repeat, an\n\t// \"echo\" that shifts delimiter balance is structural content the payload\n\t// placed intentionally — stripping it would corrupt the result.\n\tconst leadingBalance = computeDelimiterBalance(group.payload.slice(0, leadingMax));\n\tconst trailingBalance = computeDelimiterBalance(group.payload.slice(group.payload.length - trailingMax));\n\tconst droppedBalance = balanceDelta(leadingBalance, balanceNegate(trailingBalance));\n\tif (!balanceIsZero(droppedBalance)) {\n\t\tconst delta = balanceDelta(\n\t\t\tcomputeDelimiterBalance(group.payload),\n\t\t\tcomputeDelimiterBalance(fileLines.slice(group.startLine - 1, group.endLine)),\n\t\t);\n\t\tif (!balanceEqual(droppedBalance, delta)) return undefined;\n\t}\n\treturn { leading: leadingMax, trailing: trailingMax };\n}\n\nfunction describeBoundaryEchoRepair(group: ReplacementGroup, echo: BoundaryEcho): string {\n\treturn (\n\t\t`Auto-repaired a replacement boundary echo at line ${group.startLine}: ` +\n\t\t`dropped ${echo.leading} leading and ${echo.trailing} trailing payload line(s) already present outside the range. ` +\n\t\t`Issue the payload as the final desired content for the selected range only — never restate unchanged lines bordering the range.`\n\t);\n}\n\nfunction describeBoundaryRepair(group: ReplacementGroup, action: string): string {\n\treturn (\n\t\t`Auto-repaired a delimiter-balance mismatch in the replacement at line ${group.startLine}: ${action}. ` +\n\t\t`Issue the payload as the final desired content only — never restate or omit a closing bracket bordering the range.`\n\t);\n}\n\n/**\n * Normalize replacement groups so common off-by-one boundaries do not duplicate\n * unchanged surrounding lines or structural closers. Returns the repaired edit\n * list plus one warning per repaired group.\n */\nfunction repairReplacementBoundaries(\n\tedits: readonly AppliedEdit[],\n\tfileLines: readonly string[],\n): {\n\tedits: AppliedEdit[];\n\twarnings: string[];\n} {\n\tconst out: AppliedEdit[] = [];\n\tconst warnings: string[] = [];\n\tlet i = 0;\n\twhile (i < edits.length) {\n\t\tconst group = findReplacementGroup(edits, i);\n\t\tif (!group) {\n\t\t\tout.push(edits[i]);\n\t\t\ti++;\n\t\t\tcontinue;\n\t\t}\n\t\tconst inserts = group.insertIndices.map(idx => edits[idx]);\n\t\tconst deletes = group.deleteIndices.map(idx => edits[idx]);\n\t\ti = group.deleteIndices[group.deleteIndices.length - 1] + 1;\n\n\t\tconst boundaryEcho = findBoundaryEcho(group, fileLines);\n\t\tif (boundaryEcho) {\n\t\t\twarnings.push(describeBoundaryEchoRepair(group, boundaryEcho));\n\t\t\tout.push(...inserts.slice(boundaryEcho.leading, inserts.length - boundaryEcho.trailing), ...deletes);\n\t\t\tcontinue;\n\t\t}\n\n\t\tconst delta = balanceDelta(\n\t\t\tcomputeDelimiterBalance(group.payload),\n\t\t\tcomputeDelimiterBalance(fileLines.slice(group.startLine - 1, group.endLine)),\n\t\t);\n\t\tif (balanceIsZero(delta)) {\n\t\t\tout.push(...inserts, ...deletes);\n\t\t\tcontinue;\n\t\t}\n\n\t\tconst dupSuffix = findDuplicateSuffix(group, fileLines, delta);\n\t\tif (dupSuffix > 0) {\n\t\t\twarnings.push(\n\t\t\t\tdescribeBoundaryRepair(\n\t\t\t\t\tgroup,\n\t\t\t\t\t`dropped ${dupSuffix} duplicated trailing payload line(s) already present below the range`,\n\t\t\t\t),\n\t\t\t);\n\t\t\tout.push(...inserts.slice(0, inserts.length - dupSuffix), ...deletes);\n\t\t\tcontinue;\n\t\t}\n\t\tconst dupPrefix = findDuplicatePrefix(group, fileLines, delta);\n\t\tif (dupPrefix > 0) {\n\t\t\twarnings.push(\n\t\t\t\tdescribeBoundaryRepair(\n\t\t\t\t\tgroup,\n\t\t\t\t\t`dropped ${dupPrefix} duplicated leading payload line(s) already present above the range`,\n\t\t\t\t),\n\t\t\t);\n\t\t\tout.push(...inserts.slice(dupPrefix), ...deletes);\n\t\t\tcontinue;\n\t\t}\n\t\tconst droppedClosers = findDroppedSuffixClosers(group, fileLines, delta);\n\t\tif (droppedClosers > 0) {\n\t\t\twarnings.push(\n\t\t\t\tdescribeBoundaryRepair(\n\t\t\t\t\tgroup,\n\t\t\t\t\t`kept ${droppedClosers} structural closing line(s) the range deleted without restating`,\n\t\t\t\t),\n\t\t\t);\n\t\t\tout.push(...inserts, ...deletes.slice(0, deletes.length - droppedClosers));\n\t\t\tcontinue;\n\t\t}\n\t\tout.push(...inserts, ...deletes);\n\t}\n\treturn { edits: out, warnings };\n}\n\n// ═══════════════════════════════════════════════════════════════════════════\n// After-insert landing correction\n//\n// The body rows of an `insert after N:` hunk carry an implicit depth claim:\n// their leading indentation says how deep the author expects the new lines\n// to sit. Two corrections share that claim, in opposite directions:\n//\n// Outward (any after-insert): when the depth is shallower than line N itself,\n// the hunk is inserting a sibling of some enclosing construct while anchored\n// inside it — the common shape is anchoring on the last statement of a block\n// and writing the body at the parent's depth. Sliding the landing point\n// forward across the structural closer lines that follow (and nothing else —\n// content lines are never crossed) places the body at the depth its\n// indentation names.\n//\n// Inward (block-lowered inserts only): `insert after block N:` anchors on the\n// resolved block's closing line, but a body indented deeper than that closer\n// claims a depth inside the block — the common misreading of the op as\n// \"append at the end of block N's body\". Sliding the landing point backward\n// across the block's trailing closer lines places the body inside, at its\n// claimed depth. Scoped to block-lowered inserts because there the author\n// named the opener and never saw the closer; a plain `insert after M:` on a\n// closer line stays literal (the escape hatch for genuinely-after content\n// such as method-chain continuations).\n//\n// Both shifts are deliberately conservative: they fire only when the body\n// and anchor indentation are comparable (one is a prefix of the other),\n// cross only pure closing-delimiter lines, stop as soon as depth matches the\n// body's claim, and are abandoned when any other edit in the patch targets a\n// crossed line. Every shift is reported as a warning so the author can\n// re-issue when the original landing was intended.\n\n/** Leading run of tabs and spaces. */\nfunction leadingIndent(line: string): string {\n\tlet end = 0;\n\twhile (end < line.length) {\n\t\tconst code = line.charCodeAt(end);\n\t\tif (code !== 9 && code !== 32) break;\n\t\tend++;\n\t}\n\treturn line.slice(0, end);\n}\n\n/** `deeper` strictly extends `shallower` (same indent style, more depth). */\nfunction isIndentDeeper(deeper: string, shallower: string): boolean {\n\treturn deeper.length > shallower.length && deeper.startsWith(shallower);\n}\n\ninterface AfterInsertGroup {\n\t/** Anchor line shared by every insert row of the hunk. */\n\tanchor: number;\n\t/** Indices into the edit list, in patch order. */\n\tmembers: number[];\n\t/** First line of the resolved block when lowered from `insert after block N:`. */\n\tblockStart?: number;\n}\n\n/**\n * Depth of an after-insert hunk's body: the shallowest indentation across its\n * non-blank rows. Returns `undefined` when no depth claim can be made — an\n * all-blank or all-closer body, or rows whose indentation styles are not\n * mutually comparable (tabs vs spaces).\n */\nfunction bodyTargetIndent(rows: readonly string[]): string | undefined {\n\tconst nonBlank = rows.filter(hasNonWhitespace);\n\tif (nonBlank.length === 0) return undefined;\n\t// A body of pure closers re-balances delimiters; it claims no depth.\n\tif (nonBlank.every(row => STRUCTURAL_CLOSER_RE.test(row))) return undefined;\n\tlet target = leadingIndent(nonBlank[0] ?? \"\");\n\tfor (const row of nonBlank) {\n\t\tconst indent = leadingIndent(row);\n\t\tif (indent.startsWith(target)) continue;\n\t\tif (target.startsWith(indent)) target = indent;\n\t\telse return undefined;\n\t}\n\treturn target;\n}\n\n/**\n * Resolve where an after-insert hunk anchored on `group.anchor` should land\n * given its body depth `target`: the last structural closer line in the run\n * directly below the anchor whose indentation still covers `target`. Returns\n * `undefined` when the landing stays put.\n */\nfunction resolveShiftedLanding(\n\tgroup: AfterInsertGroup,\n\ttarget: string,\n\tfileLines: readonly string[],\n\ttargetedLines: ReadonlySet<number>,\n): { line: number; crossed: number } | undefined {\n\tconst anchorText = fileLines[group.anchor - 1];\n\tif (anchorText === undefined || !hasNonWhitespace(anchorText)) return undefined;\n\tif (!isIndentDeeper(leadingIndent(anchorText), target)) return undefined;\n\n\tlet landing = group.anchor;\n\tlet crossed = 0;\n\tfor (let line = group.anchor + 1; line <= fileLines.length; line++) {\n\t\tconst text = fileLines[line - 1] ?? \"\";\n\t\tif (!hasNonWhitespace(text)) continue; // look past blanks, never land on them\n\t\tif (!STRUCTURAL_CLOSER_RE.test(text)) break; // content is never crossed\n\t\tconst indent = leadingIndent(text);\n\t\tif (!indent.startsWith(target)) break; // shallower than the body — crossing would over-escape\n\t\tif (targetedLines.has(line)) return undefined; // another hunk owns this closer\n\t\tlanding = line;\n\t\tcrossed++;\n\t\tif (indent.length === target.length) break; // depth returned to the body's level\n\t}\n\treturn landing === group.anchor ? undefined : { line: landing, crossed };\n}\n\n/**\n * Resolve where a block-lowered after-insert anchored on the block's closing\n * line should land given a body depth `target` deeper than that closer: just\n * above the block's trailing run of closer lines, bounded below by\n * `blockStart` (an empty block lands the body right after its opener).\n * Returns `undefined` when the landing stays put.\n */\nfunction resolveInwardLanding(\n\tgroup: AfterInsertGroup,\n\ttarget: string,\n\tblockStart: number,\n\tfileLines: readonly string[],\n\ttargetedLines: ReadonlySet<number>,\n): number | undefined {\n\tconst anchorText = fileLines[group.anchor - 1];\n\tif (anchorText === undefined || !hasNonWhitespace(anchorText)) return undefined;\n\t// Fires only when the block ends in a pure closer the body out-indents.\n\t// Blocks ending in content (indentation-only languages) already land the\n\t// body inside the block — nothing to correct.\n\tif (!STRUCTURAL_CLOSER_RE.test(anchorText)) return undefined;\n\tif (!isIndentDeeper(target, leadingIndent(anchorText))) return undefined;\n\n\tlet landing = group.anchor;\n\tfor (let line = group.anchor; line > blockStart; line--) {\n\t\tconst text = fileLines[line - 1] ?? \"\";\n\t\tif (!hasNonWhitespace(text)) {\n\t\t\tlanding = line - 1; // look past trailing blanks, never land after one\n\t\t\tcontinue;\n\t\t}\n\t\tif (!STRUCTURAL_CLOSER_RE.test(text)) break; // content reached — land right after it\n\t\tconst indent = leadingIndent(text);\n\t\tif (!isIndentDeeper(target, indent)) break; // closer at the body's depth — land after it\n\t\t// Another hunk owns this closer (the group's own rows put the anchor\n\t\t// itself in `targetedLines`; that one is ours to cross).\n\t\tif (line !== group.anchor && targetedLines.has(line)) return undefined;\n\t\tlanding = line - 1;\n\t}\n\treturn landing === group.anchor ? undefined : landing;\n}\n\n/**\n * Slide mis-anchored after-insert hunks to the depth their body indentation\n * claims: outward past the structural closer lines that follow the anchor\n * when the body is shallower, or — for `insert after block N:` lowerings —\n * inward across the block's trailing closers when the body is deeper than\n * the block's closing line. Returns the corrected edit list plus one warning\n * per shifted hunk.\n */\nfunction repairAfterInsertLandings(\n\tedits: readonly AppliedEdit[],\n\tfileLines: readonly string[],\n): { edits: readonly AppliedEdit[]; warnings: string[] } {\n\t// Group plain (non-replacement) after-anchor inserts per authored hunk:\n\t// rows of one hunk share the anchor line and the patch header line.\n\tconst groups = new Map<string, AfterInsertGroup>();\n\tedits.forEach((edit, idx) => {\n\t\tif (edit.kind !== \"insert\" || edit.mode === \"replacement\") return;\n\t\tif (edit.cursor.kind !== \"after_anchor\") return;\n\t\tconst key = `${edit.cursor.anchor.line}:${edit.lineNum}`;\n\t\tconst group = groups.get(key);\n\t\tif (group === undefined)\n\t\t\tgroups.set(key, { anchor: edit.cursor.anchor.line, members: [idx], blockStart: edit.blockStart });\n\t\telse group.members.push(idx);\n\t});\n\tif (groups.size === 0) return { edits, warnings: [] };\n\n\t// Lines explicitly targeted by any edit; a shift never crosses them.\n\tconst targetedLines = new Set<number>();\n\tfor (const edit of edits) {\n\t\tif (edit.kind === \"delete\") targetedLines.add(edit.anchor.line);\n\t\telse if (edit.cursor.kind === \"before_anchor\" || edit.cursor.kind === \"after_anchor\")\n\t\t\ttargetedLines.add(edit.cursor.anchor.line);\n\t}\n\n\tlet out: AppliedEdit[] | undefined;\n\tconst warnings: string[] = [];\n\tconst retarget = (group: AfterInsertGroup, line: number): void => {\n\t\tout ??= [...edits];\n\t\tfor (const idx of group.members) {\n\t\t\tconst edit = out[idx] as InsertEdit;\n\t\t\tout[idx] = { ...edit, cursor: { kind: \"after_anchor\", anchor: { line } } };\n\t\t}\n\t};\n\tfor (const group of groups.values()) {\n\t\tconst target = bodyTargetIndent(group.members.map(idx => (edits[idx] as InsertEdit).text));\n\t\tif (target === undefined) continue;\n\t\tconst outward = resolveShiftedLanding(group, target, fileLines, targetedLines);\n\t\tif (outward !== undefined) {\n\t\t\tretarget(group, outward.line);\n\t\t\twarnings.push(afterInsertLandingShiftWarning(group.anchor, outward.line, outward.crossed));\n\t\t\tcontinue;\n\t\t}\n\t\tif (group.blockStart === undefined) continue;\n\t\tconst inward = resolveInwardLanding(group, target, group.blockStart, fileLines, targetedLines);\n\t\tif (inward === undefined) continue;\n\t\tretarget(group, inward);\n\t\twarnings.push(blockInsertLandingShiftWarning(group.blockStart, group.anchor, inward));\n\t}\n\treturn { edits: out ?? edits, warnings };\n}\n\n/**\n * Apply a parsed list of edits to a text body. Pure function — no I/O.\n *\n * Returns the post-edit text and the first changed line number (1-indexed).\n * Throws if an anchor is out of bounds.\n */\nexport function applyEdits(text: string, edits: readonly Edit[]): ApplyResult {\n\tif (edits.length === 0) return { text, firstChangedLine: undefined };\n\n\t// Block edits are deferred until `resolveBlockEdits` expands them into\n\t// concrete inserts + deletes. Reaching the applier with one still present\n\t// is an internal wiring bug, not authored-input error.\n\tfor (const edit of edits) {\n\t\tif (edit.kind === \"block\") throw new Error(UNRESOLVED_BLOCK_INTERNAL);\n\t}\n\tconst appliedEdits = edits as readonly AppliedEdit[];\n\n\tconst fileLines = text.split(\"\\n\");\n\tconst lineOrigins: LineOrigin[] = fileLines.map(() => \"original\");\n\n\tlet firstChangedLine: number | undefined;\n\tconst trackFirstChanged = (line: number) => {\n\t\tif (firstChangedLine === undefined || line < firstChangedLine) firstChangedLine = line;\n\t};\n\n\tconst targetEdits = dropTrailingPhantomDeletes(\n\t\tappliedEdits.map((edit, index) => cloneAppliedEdit(edit, index)),\n\t\tfileLines,\n\t);\n\tvalidateLineBounds(targetEdits, fileLines);\n\tconst { edits: repaired, warnings: boundaryWarnings } = repairReplacementBoundaries(targetEdits, fileLines);\n\tconst { edits: landed, warnings: landingWarnings } = repairAfterInsertLandings(repaired, fileLines);\n\tconst warnings = [...boundaryWarnings, ...landingWarnings];\n\n\t// Partition edits into bof, eof, and anchor-targeted buckets.\n\tconst bofLines: string[] = [];\n\tconst eofLines: string[] = [];\n\tconst anchorEdits: IndexedEdit[] = [];\n\tlanded.forEach((edit, idx) => {\n\t\tif (edit.kind === \"insert\" && edit.cursor.kind === \"bof\") {\n\t\t\tbofLines.push(edit.text);\n\t\t} else if (edit.kind === \"insert\" && edit.cursor.kind === \"eof\") {\n\t\t\teofLines.push(edit.text);\n\t\t} else {\n\t\t\tanchorEdits.push({ edit, idx });\n\t\t}\n\t});\n\n\t// Apply per-line buckets bottom-up so earlier indices stay valid.\n\tconst byLine = bucketAnchorEditsByLine(anchorEdits);\n\tfor (const line of [...byLine.keys()].sort((a, b) => b - a)) {\n\t\tconst bucket = byLine.get(line);\n\t\tif (!bucket) continue;\n\t\tbucket.sort((a, b) => a.idx - b.idx);\n\n\t\tconst idx = line - 1;\n\t\tconst currentLine = fileLines[idx] ?? \"\";\n\t\tconst beforeInsertLines: string[] = [];\n\t\tconst afterInsertLines: string[] = [];\n\t\tconst replacementLines: string[] = [];\n\t\tlet deleteLine = false;\n\n\t\tfor (const { edit } of bucket) {\n\t\t\tif (isReplacementInsert(edit)) {\n\t\t\t\treplacementLines.push(edit.text);\n\t\t\t} else if (edit.kind === \"insert\" && edit.cursor.kind === \"after_anchor\") {\n\t\t\t\tafterInsertLines.push(edit.text);\n\t\t\t} else if (edit.kind === \"insert\") {\n\t\t\t\tbeforeInsertLines.push(edit.text);\n\t\t\t} else if (edit.kind === \"delete\") {\n\t\t\t\tdeleteLine = true;\n\t\t\t}\n\t\t}\n\t\tif (\n\t\t\tbeforeInsertLines.length === 0 &&\n\t\t\treplacementLines.length === 0 &&\n\t\t\tafterInsertLines.length === 0 &&\n\t\t\t!deleteLine\n\t\t)\n\t\t\tcontinue;\n\n\t\tconst replacement = deleteLine\n\t\t\t? [...beforeInsertLines, ...replacementLines, ...afterInsertLines]\n\t\t\t: [...beforeInsertLines, ...replacementLines, currentLine, ...afterInsertLines];\n\t\tconst origins: LineOrigin[] = [];\n\t\tfor (let i = 0; i < beforeInsertLines.length; i++) origins.push(\"insert\");\n\t\tfor (let i = 0; i < replacementLines.length; i++) origins.push(deleteLine ? \"replacement\" : \"insert\");\n\t\tif (!deleteLine) origins.push(lineOrigins[idx] ?? \"original\");\n\t\tfor (let i = 0; i < afterInsertLines.length; i++) origins.push(\"insert\");\n\n\t\tfileLines.splice(idx, 1, ...replacement);\n\t\tlineOrigins.splice(idx, 1, ...origins);\n\t\ttrackFirstChanged(line);\n\t}\n\n\tif (bofLines.length > 0) {\n\t\tinsertAtStart(fileLines, lineOrigins, bofLines);\n\t\ttrackFirstChanged(1);\n\t}\n\tconst eofChangedLine = insertAtEnd(fileLines, lineOrigins, eofLines);\n\tif (eofChangedLine !== undefined) trackFirstChanged(eofChangedLine);\n\n\treturn {\n\t\ttext: fileLines.join(\"\\n\"),\n\t\tfirstChangedLine,\n\t\t...(warnings.length > 0 ? { warnings } : {}),\n\t};\n}\n"]}
|