pi-agent-ide 0.6.3 → 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +97 -0
- package/README.md +10 -3
- package/dist/{chunk-AWXVJ5AC.js → chunk-2RTCSZB3.js} +1 -1
- package/dist/chunk-3HYXQ4YA.js +28 -0
- package/dist/chunk-3JO7QFIL.js +1 -0
- package/dist/chunk-4NIFBFFR.js +11 -0
- package/dist/{chunk-VCTK4JSN.js → chunk-64M3SQPG.js} +1 -1
- package/dist/{chunk-5QQABP47.js → chunk-7LD2OQH3.js} +1 -1
- package/dist/chunk-7LZKOHH2.js +1 -0
- package/dist/chunk-AJRIASPQ.js +2 -0
- package/dist/chunk-EAZGN5ND.js +7 -0
- package/dist/{chunk-ZM4L5AGN.js → chunk-EDURPQAV.js} +3 -2
- package/dist/{chunk-GIRAEVUJ.js → chunk-F5YZG77W.js} +1 -1
- package/dist/chunk-FK42RSF4.js +1 -0
- package/dist/{chunk-O6LE42GK.js → chunk-FXNKMNSJ.js} +1 -1
- package/dist/chunk-GBLKIB6O.js +2 -0
- package/dist/chunk-KBP3QUDT.js +2 -0
- package/dist/chunk-KR36BT5E.js +1 -0
- package/dist/{chunk-NTYXZ3EL.js → chunk-KVHDMIZS.js} +1 -1
- package/dist/{chunk-QDST6VIU.js → chunk-KZC7TI5M.js} +1 -1
- package/dist/chunk-OE7QVAI2.js +5 -0
- package/dist/{chunk-GUTQ3PWL.js → chunk-PLKYHUNT.js} +1 -1
- package/dist/chunk-PNU3E7IO.js +3 -0
- package/dist/chunk-PS2B6THP.js +3 -0
- package/dist/chunk-PYPPV2F5.js +25 -0
- package/dist/{chunk-25CZ5ZIU.js → chunk-QFTBY67N.js} +3 -3
- package/dist/chunk-S5LXX32D.js +28 -0
- package/dist/chunk-SILJ3OL4.js +9 -0
- package/dist/chunk-SQKAXYIR.js +2 -0
- package/dist/chunk-VCGNVTXQ.js +1 -0
- package/dist/chunk-XYS4FSCC.js +12 -0
- package/dist/chunk-YGGKGZO2.js +1 -0
- package/dist/{chunk-L7JMDDSD.js → chunk-YRF7OATP.js} +1 -1
- package/dist/chunk-ZPT5D4VP.js +2 -0
- package/dist/entry-0.js +1 -1
- package/dist/entry-11.js +1 -1
- package/dist/entry-13.js +1 -1
- package/dist/entry-14.js +1 -1
- package/dist/entry-15.js +1 -1
- package/dist/entry-16.js +1 -1
- package/dist/entry-17.js +1 -1
- package/dist/entry-25.js +2 -2
- package/dist/entry-26.js +1 -1
- package/dist/entry-27.js +1 -1
- package/dist/entry-28.js +1 -1
- package/dist/entry-29.js +1 -1
- package/dist/entry-30.js +1 -1
- package/dist/entry-31.js +11 -9
- package/dist/entry-33.js +1 -1
- package/dist/entry-37.js +6 -6
- package/dist/entry-42.js +1 -1
- package/dist/entry-44.js +1 -1
- package/dist/entry-45.js +12 -11
- package/dist/entry-46.js +1 -1
- package/dist/entry-49.js +1 -1
- package/dist/entry-50.js +3 -6
- package/dist/entry-51.js +11 -9
- package/dist/entry-52.js +1 -1
- package/dist/entry-53.js +1 -1
- package/dist/entry-54.js +1 -1
- package/dist/entry-55.js +1 -1
- package/dist/entry-56.js +1 -1
- package/dist/entry-57.js +1 -1
- package/dist/entry-58.js +1 -1
- package/dist/entry-59.js +1 -1
- package/dist/entry-60.js +1 -1
- package/dist/entry-61.js +0 -1
- package/dist/entry-62.js +1 -0
- package/dist/entry-63.js +0 -1
- package/dist/entry-64.js +1 -0
- package/dist/entry-65.js +1 -1
- package/dist/entry-66.js +1 -1
- package/dist/entry-67.js +1 -1
- package/dist/entry-68.js +1 -1
- package/dist/entry-69.js +1 -1
- package/dist/entry-70.js +1 -1
- package/dist/entry-72.js +1 -1
- package/dist/entry-75.js +1 -1
- package/dist/entry-77.js +50 -307
- package/dist/entry-78.js +14 -26
- package/dist/entry-79.js +2 -2
- package/dist/entry-80.js +1 -1
- package/dist/entry-81.js +1 -1
- package/dist/entry-82.js +5 -3
- package/dist/entry-83.js +1 -1
- package/dist/entry-84.js +1 -1
- package/dist/entry-85.js +1 -1
- package/dist/entry-86.js +1 -1
- package/dist/pi-agent-ide.js +15 -3
- package/docs/agent-guides/editing.md +31 -3
- package/docs/agent-guides/git-changes.md +1 -1
- package/docs/agent-guides/json-reading.md +19 -3
- package/docs/agent-guides/read-resources.md +15 -1
- package/docs/agent-guides/search-code.md +43 -2
- package/docs/agent-guides/select-code.md +194 -0
- package/docs/agent-guides/terminal.md +1 -1
- package/docs/code-review.md +99 -0
- package/docs/configuration.md +36 -7
- package/docs/development.md +16 -19
- package/docs/nightly.md +7 -0
- package/docs/releases.md +42 -0
- package/docs/research/apply-native-capability-matrix.md +174 -0
- package/docs/research/apply-read-latency.md +2 -0
- package/docs/research/lpt-393-fuzzy-results/file.json +152 -0
- package/docs/research/lpt-393-fuzzy-results/repo.json +122 -0
- package/docs/research/lpt-393-search-fuzzy-fallback.md +97 -0
- package/docs/research/native-codemode-batch-decision.md +29 -0
- package/docs/research/native-codemode-batching.md +65 -0
- package/docs/structured-results.md +86 -0
- package/docs/tools.md +24 -4
- package/docs/user-hooks.md +1 -1
- package/node_modules/pi-agent-doctor/package.json +1 -1
- package/node_modules/pi-agent-documentation/package.json +1 -1
- package/node_modules/pi-agent-documentation/src/index.ts +1 -1
- package/node_modules/pi-agent-filesystem/index.ts +6 -1
- package/node_modules/pi-agent-filesystem/package.json +7 -6
- package/node_modules/pi-agent-filesystem/src/path-recovery.ts +109 -0
- package/node_modules/pi-agent-filesystem-image/package.json +3 -3
- package/node_modules/pi-agent-filesystem-jq/package.json +5 -5
- package/node_modules/pi-agent-filesystem-pdf/package.json +3 -3
- package/node_modules/pi-agent-filesystem-text/package.json +3 -3
- package/node_modules/pi-agent-ide-ast/package.json +15 -10
- package/node_modules/pi-agent-ide-ast/src/ast/constructs.ts +193 -0
- package/node_modules/pi-agent-ide-ast/src/ast/element-extent.ts +59 -0
- package/node_modules/pi-agent-ide-ast/src/ast/manager.ts +5 -1
- package/node_modules/pi-agent-ide-ast/src/ast/selection.ts +204 -0
- package/node_modules/pi-agent-ide-ast/src/ast/syntax-tree.ts +2 -0
- package/node_modules/pi-agent-ide-ast/src/extension.ts +4 -2
- package/node_modules/pi-agent-ide-ast/src/geometry-selection.ts +184 -0
- package/node_modules/pi-agent-ide-ast/src/overflow-handler.ts +6 -1
- package/node_modules/pi-agent-ide-ast/src/scope-handler.ts +6 -2
- package/node_modules/pi-agent-ide-ast/src/search-resolver.ts +110 -7
- package/node_modules/pi-agent-ide-ast/src/select-presentation.ts +104 -0
- package/node_modules/pi-agent-ide-ast/src/select-schema.ts +384 -0
- package/node_modules/pi-agent-ide-ast/src/selection-region.ts +41 -0
- package/node_modules/pi-agent-ide-ast/src/source-text.ts +92 -0
- package/node_modules/pi-agent-ide-ast/src/text-selection.ts +214 -0
- package/node_modules/pi-agent-ide-ast/src/tool-select.ts +251 -0
- package/node_modules/pi-agent-ide-changes/index.ts +2 -24
- package/node_modules/pi-agent-ide-changes/package.json +8 -7
- package/node_modules/pi-agent-ide-changes/src/current-change-presenter.ts +5 -0
- package/node_modules/pi-agent-ide-changes/src/tool-index-change.ts +126 -48
- package/node_modules/pi-agent-ide-changes/src/tool-text-undo.ts +11 -40
- package/node_modules/pi-agent-ide-formatter/package.json +3 -3
- package/node_modules/pi-agent-ide-languages/package.json +2 -2
- package/node_modules/pi-agent-ide-lint/package.json +7 -7
- package/node_modules/pi-agent-ide-lsp/index.ts +2 -2
- package/node_modules/pi-agent-ide-lsp/package.json +9 -9
- package/node_modules/pi-agent-ide-lsp/src/lsp/client.ts +5 -0
- package/node_modules/pi-agent-ide-lsp/src/lsp/manager.ts +8 -2
- package/node_modules/pi-agent-ide-lsp/src/lsp/project.ts +19 -0
- package/node_modules/pi-agent-ide-lsp/src/lsp/symbol-search.ts +204 -118
- package/node_modules/pi-agent-ide-lsp/src/rename-plan.ts +2 -17
- package/node_modules/pi-agent-ide-lsp/src/search-resolver.ts +92 -30
- package/node_modules/pi-agent-image/package.json +2 -2
- package/node_modules/pi-agent-invariant/package.json +1 -1
- package/node_modules/pi-agent-pdf/package.json +2 -2
- package/node_modules/pi-agent-read/package.json +7 -7
- package/node_modules/pi-agent-read/src/api/read-parameters.ts +2 -2
- package/node_modules/pi-agent-read/src/api/tools/read.ts +35 -2
- package/node_modules/pi-agent-read/src/core/extension.ts +11 -0
- package/node_modules/pi-agent-read/src/core/tools/read/raw-read.ts +9 -10
- package/node_modules/pi-agent-read/src/core/tools/read/read-result.ts +28 -15
- package/node_modules/pi-agent-read/src/core/tools/read/result-target.ts +93 -0
- package/node_modules/pi-agent-read/src/core/tools/read/structured-result.ts +218 -0
- package/node_modules/pi-agent-read/src/core/tools/tool-read.ts +127 -73
- package/node_modules/pi-agent-resource/package.json +1 -1
- package/node_modules/pi-agent-resource/src/index.ts +22 -0
- package/node_modules/pi-agent-resource/src/resource-access.ts +55 -0
- package/node_modules/pi-agent-resource/src/resource-scheduler.ts +204 -0
- package/node_modules/pi-agent-resource/src/result-targets.ts +233 -0
- package/node_modules/pi-agent-resource/src/tool-result.ts +125 -0
- package/node_modules/pi-agent-search/package.json +5 -4
- package/node_modules/pi-agent-search/src/api/fuzzy.ts +208 -0
- package/node_modules/pi-agent-search/src/api/presentation.ts +68 -0
- package/node_modules/pi-agent-search/src/api/search-parameters.ts +13 -6
- package/node_modules/pi-agent-search/src/api/search-scope.ts +22 -0
- package/node_modules/pi-agent-search/src/api/search.ts +39 -0
- package/node_modules/pi-agent-search/src/api/structured-result.ts +148 -0
- package/node_modules/pi-agent-search/src/core/extension.ts +90 -24
- package/node_modules/pi-agent-search/src/core/search-core.ts +145 -78
- package/node_modules/pi-agent-search-text/index.ts +6 -1
- package/node_modules/pi-agent-search-text/package.json +7 -6
- package/node_modules/pi-agent-search-text/src/fuzzy-search.ts +164 -0
- package/node_modules/pi-agent-search-text/src/resolvers.ts +174 -26
- package/node_modules/pi-agent-search-text/src/scoped-search.ts +103 -0
- package/node_modules/pi-agent-search-text/src/search-backend.ts +201 -18
- package/node_modules/pi-agent-search-text/src/search-query.ts +42 -34
- package/node_modules/pi-agent-search-text/src/search-recipe.ts +18 -4
- package/node_modules/pi-agent-search-text/src/search-renderer.ts +90 -259
- package/node_modules/pi-agent-search-text/src/search-result.ts +77 -0
- package/node_modules/pi-agent-search-text/src/search-session.ts +110 -17
- package/node_modules/pi-agent-text/package.json +2 -2
- package/node_modules/pi-agent-text/src/text-document.ts +15 -10
- package/node_modules/pi-agent-text-anchor-constant/package.json +3 -3
- package/node_modules/pi-agent-text-anchor-exact/package.json +3 -3
- package/node_modules/pi-agent-text-anchor-exact/src/anchor.ts +4 -3
- package/node_modules/pi-agent-text-anchor-line-hash/package.json +5 -5
- package/node_modules/pi-agent-text-editor/index.ts +4 -7
- package/node_modules/pi-agent-text-editor/package.json +20 -21
- package/node_modules/pi-agent-text-editor/src/api/mutation-preview.ts +2 -0
- package/node_modules/pi-agent-text-editor/src/api/mutation-result.ts +6 -0
- package/node_modules/pi-agent-text-editor/src/api/mutation-tool.ts +3 -1
- package/node_modules/pi-agent-text-editor/src/api/native-edit-batch-event.ts +12 -0
- package/node_modules/pi-agent-text-editor/src/api/plugin-protocol.ts +1 -20
- package/node_modules/pi-agent-text-editor/src/core/builtin-edit.ts +16 -0
- package/node_modules/pi-agent-text-editor/src/core/diff-tool.ts +78 -41
- package/node_modules/pi-agent-text-editor/src/core/editing-guidelines.ts +8 -0
- package/node_modules/pi-agent-text-editor/src/core/extension.ts +12 -2
- package/node_modules/pi-agent-text-editor/src/core/file-operation-tools.ts +35 -4
- package/node_modules/pi-agent-text-editor/src/core/file-operations.ts +1 -1
- package/node_modules/pi-agent-text-editor/src/core/file-result-targets.ts +76 -0
- package/node_modules/pi-agent-text-editor/src/core/mutation-result/file-mutation-result.ts +2 -0
- package/node_modules/pi-agent-text-editor/src/core/mutation-result-targets.ts +168 -0
- package/node_modules/pi-agent-text-editor/src/core/native-text-edit-batch.ts +820 -0
- package/node_modules/pi-agent-text-editor/src/core/post-edit-scope.ts +20 -6
- package/node_modules/pi-agent-text-editor/src/core/result-input.ts +22 -0
- package/node_modules/pi-agent-text-editor/src/core/result-target-anchors.ts +78 -0
- package/node_modules/pi-agent-text-editor/src/core/result-transfer.ts +175 -0
- package/node_modules/pi-agent-text-editor/src/core/structured-result.ts +412 -0
- package/node_modules/pi-agent-text-editor/src/core/text-edit-batch-registrar.ts +149 -139
- package/node_modules/pi-agent-text-editor/src/core/text-edit-batch.ts +2 -0
- package/node_modules/pi-agent-text-editor/src/core/text-editor-core.ts +236 -185
- package/node_modules/pi-agent-text-editor/src/core/text-mutation.ts +253 -203
- package/node_modules/pi-agent-text-editor/src/core/tool-call-interceptor/coordinator.ts +25 -5
- package/node_modules/pi-agent-text-editor/src/core/tool-call-interceptor/streaming-provider.ts +8 -4
- package/node_modules/pi-agent-text-editor/src/tools/text-tool-schema-properties.ts +9 -6
- package/node_modules/pi-agent-text-editor/src/tools/tool-text-copy.ts +5 -5
- package/node_modules/pi-agent-text-editor/src/tools/tool-text-delete.ts +5 -5
- package/node_modules/pi-agent-text-editor/src/tools/tool-text-move.ts +9 -7
- package/node_modules/pi-agent-text-editor/src/tools/tool-text-replace.ts +8 -8
- package/node_modules/pi-agent-text-editor/src/tools/tool-text-write.ts +8 -3
- package/node_modules/pi-agent-text-editor-renderer/index.ts +9 -73
- package/node_modules/pi-agent-text-editor-renderer/package.json +6 -5
- package/node_modules/pi-agent-text-editor-renderer/src/diff-model.ts +276 -129
- package/node_modules/pi-agent-text-editor-renderer/src/diff-renderer.ts +36 -2
- package/node_modules/pi-agent-text-editor-renderer/src/mutation-panel.ts +28 -2
- package/node_modules/pi-agent-text-editor-renderer/src/mutation-projection.ts +40 -10
- package/node_modules/pi-agent-text-editor-renderer/src/renderer.ts +19 -45
- package/node_modules/pi-agent-text-editor-stale-anchor/index.ts +16 -1
- package/node_modules/pi-agent-text-editor-stale-anchor/package.json +4 -4
- package/node_modules/pi-agent-tool-call-interception/package.json +2 -2
- package/node_modules/pi-agent-tool-ui/package.json +1 -1
- package/node_modules/pi-agent-tool-ui/src/index.ts +7 -0
- package/node_modules/pi-agent-tool-ui/src/result-panel.ts +298 -0
- package/node_modules/pi-agent-web/index.ts +23 -0
- package/node_modules/pi-agent-web/package.json +7 -6
- package/node_modules/pi-agent-web/src/search.ts +221 -0
- package/node_modules/pi-agent-web-html/package.json +2 -2
- package/node_modules/pi-agent-web-image/package.json +3 -3
- package/node_modules/pi-agent-web-pdf/package.json +3 -3
- package/node_modules/pi-agent-web-text/package.json +3 -3
- package/package.json +42 -37
- package/skills/capture-code-review-rule/SKILL.md +60 -0
- package/src/code-review/config.ts +68 -0
- package/src/code-review/extension.ts +105 -0
- package/src/code-review/fragments.ts +26 -0
- package/src/code-review/reviewer.ts +198 -0
- package/src/composite/host-version.ts +13 -0
- package/src/composite/module-labels.ts +1 -1
- package/src/composite/nested-tool-rendering.ts +298 -0
- package/src/composite/preferences.ts +0 -5
- package/src/composite/tool-availability.ts +90 -0
- package/src/core/extension.ts +2 -0
- package/src/documentation/extension.ts +81 -145
- package/src/extensions/pi-agent-read/extensions/pi-agent-filesystem/index.ts +6 -1
- package/src/extensions/pi-agent-read/extensions/pi-agent-filesystem/package.json +7 -6
- package/src/extensions/pi-agent-read/extensions/pi-agent-filesystem/plugins/pi-agent-filesystem-image/package.json +3 -3
- package/src/extensions/pi-agent-read/extensions/pi-agent-filesystem/plugins/pi-agent-filesystem-jq/package.json +5 -5
- package/src/extensions/pi-agent-read/extensions/pi-agent-filesystem/plugins/pi-agent-filesystem-pdf/package.json +3 -3
- package/src/extensions/pi-agent-read/extensions/pi-agent-filesystem/plugins/pi-agent-filesystem-text/package.json +3 -3
- package/src/extensions/pi-agent-read/extensions/pi-agent-filesystem/src/path-recovery.ts +109 -0
- package/src/extensions/pi-agent-read/extensions/pi-agent-web/index.ts +23 -0
- package/src/extensions/pi-agent-read/extensions/pi-agent-web/package.json +7 -6
- package/src/extensions/pi-agent-read/extensions/pi-agent-web/plugins/pi-agent-web-html/package.json +2 -2
- package/src/extensions/pi-agent-read/extensions/pi-agent-web/plugins/pi-agent-web-image/package.json +3 -3
- package/src/extensions/pi-agent-read/extensions/pi-agent-web/plugins/pi-agent-web-pdf/package.json +3 -3
- package/src/extensions/pi-agent-read/extensions/pi-agent-web/plugins/pi-agent-web-text/package.json +3 -3
- package/src/extensions/pi-agent-read/extensions/pi-agent-web/src/search.ts +221 -0
- package/src/extensions/pi-agent-read/package.json +7 -7
- package/src/extensions/pi-agent-read/src/api/read-parameters.ts +2 -2
- package/src/extensions/pi-agent-read/src/api/tools/read.ts +35 -2
- package/src/extensions/pi-agent-read/src/core/extension.ts +11 -0
- package/src/extensions/pi-agent-read/src/core/tools/read/raw-read.ts +9 -10
- package/src/extensions/pi-agent-read/src/core/tools/read/read-result.ts +28 -15
- package/src/extensions/pi-agent-read/src/core/tools/read/result-target.ts +93 -0
- package/src/extensions/pi-agent-read/src/core/tools/read/structured-result.ts +218 -0
- package/src/extensions/pi-agent-read/src/core/tools/tool-read.ts +127 -73
- package/src/extensions/pi-agent-search/package.json +5 -4
- package/src/extensions/pi-agent-search/plugins/pi-agent-search-text/index.ts +6 -1
- package/src/extensions/pi-agent-search/plugins/pi-agent-search-text/package.json +7 -6
- package/src/extensions/pi-agent-search/plugins/pi-agent-search-text/src/fuzzy-search.ts +164 -0
- package/src/extensions/pi-agent-search/plugins/pi-agent-search-text/src/resolvers.ts +174 -26
- package/src/extensions/pi-agent-search/plugins/pi-agent-search-text/src/scoped-search.ts +103 -0
- package/src/extensions/pi-agent-search/plugins/pi-agent-search-text/src/search-backend.ts +201 -18
- package/src/extensions/pi-agent-search/plugins/pi-agent-search-text/src/search-query.ts +42 -34
- package/src/extensions/pi-agent-search/plugins/pi-agent-search-text/src/search-recipe.ts +18 -4
- package/src/extensions/pi-agent-search/plugins/pi-agent-search-text/src/search-renderer.ts +90 -259
- package/src/extensions/pi-agent-search/plugins/pi-agent-search-text/src/search-result.ts +77 -0
- package/src/extensions/pi-agent-search/plugins/pi-agent-search-text/src/search-session.ts +110 -17
- package/src/extensions/pi-agent-search/src/api/fuzzy.ts +208 -0
- package/src/extensions/pi-agent-search/src/api/presentation.ts +68 -0
- package/src/extensions/pi-agent-search/src/api/search-parameters.ts +13 -6
- package/src/extensions/pi-agent-search/src/api/search-scope.ts +22 -0
- package/src/extensions/pi-agent-search/src/api/search.ts +39 -0
- package/src/extensions/pi-agent-search/src/api/structured-result.ts +148 -0
- package/src/extensions/pi-agent-search/src/core/extension.ts +90 -24
- package/src/extensions/pi-agent-search/src/core/search-core.ts +145 -78
- package/src/extensions/pi-agent-text-editor/index.ts +4 -7
- package/src/extensions/pi-agent-text-editor/package.json +20 -21
- package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-anchor-constant/package.json +3 -3
- package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-anchor-exact/package.json +3 -3
- package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-anchor-exact/src/anchor.ts +4 -3
- package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-anchor-line-hash/package.json +5 -5
- package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-renderer/index.ts +9 -73
- package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-renderer/package.json +6 -5
- package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-renderer/src/diff-model.ts +276 -129
- package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-renderer/src/diff-renderer.ts +36 -2
- package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-renderer/src/mutation-panel.ts +28 -2
- package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-renderer/src/mutation-projection.ts +40 -10
- package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-renderer/src/renderer.ts +19 -45
- package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-stale-anchor/index.ts +16 -1
- package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-stale-anchor/package.json +4 -4
- package/src/extensions/pi-agent-text-editor/src/api/mutation-preview.ts +2 -0
- package/src/extensions/pi-agent-text-editor/src/api/mutation-result.ts +6 -0
- package/src/extensions/pi-agent-text-editor/src/api/mutation-tool.ts +3 -1
- package/src/extensions/pi-agent-text-editor/src/api/native-edit-batch-event.ts +12 -0
- package/src/extensions/pi-agent-text-editor/src/api/plugin-protocol.ts +1 -20
- package/src/extensions/pi-agent-text-editor/src/core/builtin-edit.ts +16 -0
- package/src/extensions/pi-agent-text-editor/src/core/diff-tool.ts +78 -41
- package/src/extensions/pi-agent-text-editor/src/core/editing-guidelines.ts +8 -0
- package/src/extensions/pi-agent-text-editor/src/core/extension.ts +12 -2
- package/src/extensions/pi-agent-text-editor/src/core/file-operation-tools.ts +35 -4
- package/src/extensions/pi-agent-text-editor/src/core/file-operations.ts +1 -1
- package/src/extensions/pi-agent-text-editor/src/core/file-result-targets.ts +76 -0
- package/src/extensions/pi-agent-text-editor/src/core/mutation-result/file-mutation-result.ts +2 -0
- package/src/extensions/pi-agent-text-editor/src/core/mutation-result-targets.ts +168 -0
- package/src/extensions/pi-agent-text-editor/src/core/native-text-edit-batch.ts +820 -0
- package/src/extensions/pi-agent-text-editor/src/core/post-edit-scope.ts +20 -6
- package/src/extensions/pi-agent-text-editor/src/core/result-input.ts +22 -0
- package/src/extensions/pi-agent-text-editor/src/core/result-target-anchors.ts +78 -0
- package/src/extensions/pi-agent-text-editor/src/core/result-transfer.ts +175 -0
- package/src/extensions/pi-agent-text-editor/src/core/structured-result.ts +412 -0
- package/src/extensions/pi-agent-text-editor/src/core/text-edit-batch-registrar.ts +149 -139
- package/src/extensions/pi-agent-text-editor/src/core/text-edit-batch.ts +2 -0
- package/src/extensions/pi-agent-text-editor/src/core/text-editor-core.ts +236 -185
- package/src/extensions/pi-agent-text-editor/src/core/text-mutation.ts +253 -203
- package/src/extensions/pi-agent-text-editor/src/core/tool-call-interceptor/coordinator.ts +25 -5
- package/src/extensions/pi-agent-text-editor/src/core/tool-call-interceptor/streaming-provider.ts +8 -4
- package/src/extensions/pi-agent-text-editor/src/tools/text-tool-schema-properties.ts +9 -6
- package/src/extensions/pi-agent-text-editor/src/tools/tool-text-copy.ts +5 -5
- package/src/extensions/pi-agent-text-editor/src/tools/tool-text-delete.ts +5 -5
- package/src/extensions/pi-agent-text-editor/src/tools/tool-text-move.ts +9 -7
- package/src/extensions/pi-agent-text-editor/src/tools/tool-text-replace.ts +8 -8
- package/src/extensions/pi-agent-text-editor/src/tools/tool-text-write.ts +8 -3
- package/src/pi-agent-ide.ts +61 -8
- package/src/plugins/pi-agent-ide-ast/package.json +15 -10
- package/src/plugins/pi-agent-ide-ast/src/ast/constructs.ts +193 -0
- package/src/plugins/pi-agent-ide-ast/src/ast/element-extent.ts +59 -0
- package/src/plugins/pi-agent-ide-ast/src/ast/manager.ts +5 -1
- package/src/plugins/pi-agent-ide-ast/src/ast/selection.ts +204 -0
- package/src/plugins/pi-agent-ide-ast/src/ast/syntax-tree.ts +2 -0
- package/src/plugins/pi-agent-ide-ast/src/extension.ts +4 -2
- package/src/plugins/pi-agent-ide-ast/src/geometry-selection.ts +184 -0
- package/src/plugins/pi-agent-ide-ast/src/overflow-handler.ts +6 -1
- package/src/plugins/pi-agent-ide-ast/src/scope-handler.ts +6 -2
- package/src/plugins/pi-agent-ide-ast/src/search-resolver.ts +110 -7
- package/src/plugins/pi-agent-ide-ast/src/select-presentation.ts +104 -0
- package/src/plugins/pi-agent-ide-ast/src/select-schema.ts +384 -0
- package/src/plugins/pi-agent-ide-ast/src/selection-region.ts +41 -0
- package/src/plugins/pi-agent-ide-ast/src/source-text.ts +92 -0
- package/src/plugins/pi-agent-ide-ast/src/text-selection.ts +214 -0
- package/src/plugins/pi-agent-ide-ast/src/tool-select.ts +251 -0
- package/src/plugins/pi-agent-ide-changes/index.ts +2 -24
- package/src/plugins/pi-agent-ide-changes/package.json +8 -7
- package/src/plugins/pi-agent-ide-changes/src/current-change-presenter.ts +5 -0
- package/src/plugins/pi-agent-ide-changes/src/tool-index-change.ts +126 -48
- package/src/plugins/pi-agent-ide-changes/src/tool-text-undo.ts +11 -40
- package/src/plugins/pi-agent-ide-debugger/index.ts +63 -17
- package/src/plugins/pi-agent-ide-debugger/src/catalog.ts +18 -2
- package/src/plugins/pi-agent-ide-debugger/src/dap-client.ts +24 -4
- package/src/plugins/pi-agent-ide-debugger/src/doctor-plugin.ts +68 -2
- package/src/plugins/pi-agent-ide-debugger/src/java-runtime.ts +357 -0
- package/src/plugins/pi-agent-ide-debugger/src/session-manager.ts +55 -3
- package/src/plugins/pi-agent-ide-diagnostics/src/extension.ts +13 -13
- package/src/plugins/pi-agent-ide-formatter/package.json +3 -3
- package/src/plugins/pi-agent-ide-languages/package.json +2 -2
- package/src/plugins/pi-agent-ide-lint/package.json +7 -7
- package/src/plugins/pi-agent-ide-lsp/index.ts +2 -2
- package/src/plugins/pi-agent-ide-lsp/package.json +9 -9
- package/src/plugins/pi-agent-ide-lsp/src/lsp/client.ts +5 -0
- package/src/plugins/pi-agent-ide-lsp/src/lsp/manager.ts +8 -2
- package/src/plugins/pi-agent-ide-lsp/src/lsp/project.ts +19 -0
- package/src/plugins/pi-agent-ide-lsp/src/lsp/symbol-search.ts +204 -118
- package/src/plugins/pi-agent-ide-lsp/src/rename-plan.ts +2 -17
- package/src/plugins/pi-agent-ide-lsp/src/search-resolver.ts +92 -30
- package/src/plugins/pi-agent-ide-terminal/index.ts +12 -2
- package/src/plugins/pi-agent-ide-terminal/src/output-limits.ts +1 -1
- package/src/plugins/pi-agent-ide-terminal/src/resources.ts +65 -30
- package/src/plugins/pi-agent-ide-terminal/src/search.ts +16 -37
- package/src/plugins/pi-agent-ide-terminal/src/session-manager.ts +1 -0
- package/src/plugins/pi-agent-ide-terminal/src/shell-result.ts +100 -0
- package/src/plugins/pi-agent-ide-terminal/src/steering.ts +62 -0
- package/src/plugins/pi-agent-ide-terminal/src/tools.ts +98 -46
- package/src/plugins/pi-agent-ide-terminal/src/types.ts +3 -1
- package/src/plugins/pi-agent-ide-terminal/src/ui.ts +15 -6
- package/src/plugins/pi-agent-ide-vision/index.ts +35 -1
- package/dist/chunk-3E2NTWCN.js +0 -2
- package/dist/chunk-4LUW26BX.js +0 -10
- package/dist/chunk-4U2GUKNJ.js +0 -0
- package/dist/chunk-5VSE5X5R.js +0 -2
- package/dist/chunk-6EYWFBNG.js +0 -0
- package/dist/chunk-7WCZOO5I.js +0 -1
- package/dist/chunk-CEX3VOJ7.js +0 -2
- package/dist/chunk-FA6BKC5Y.js +0 -1
- package/dist/chunk-FEDVTZJG.js +0 -1
- package/dist/chunk-HMLH43ZS.js +0 -4
- package/dist/chunk-ITSNK5YG.js +0 -7
- package/dist/chunk-KII2JQC5.js +0 -1
- package/dist/chunk-LVWMWWWF.js +0 -28
- package/dist/chunk-P7XO5CIP.js +0 -2
- package/dist/chunk-PZFOOHY2.js +0 -15
- package/dist/chunk-ROXXK6TW.js +0 -0
- package/dist/chunk-S3ZPXWX7.js +0 -2
- package/dist/chunk-T7CZIACQ.js +0 -27
- package/dist/chunk-VXD6FZP5.js +0 -5
- package/dist/chunk-YHUTTZC6.js +0 -2
- package/dist/chunk-YT42LKFV.js +0 -16
- package/docs/agent-guides/apply.md +0 -333
- package/node_modules/pi-agent-text-editor/src/api/apply-renderer.ts +0 -3
- package/node_modules/pi-agent-text-editor/src/core/apply/apply-undo-store.ts +0 -155
- package/node_modules/pi-agent-text-editor/src/core/apply/execution.ts +0 -344
- package/node_modules/pi-agent-text-editor/src/core/apply/final-mutations.ts +0 -50
- package/node_modules/pi-agent-text-editor/src/core/apply/format-source.ts +0 -11
- package/node_modules/pi-agent-text-editor/src/core/apply/mixed-source.ts +0 -203
- package/node_modules/pi-agent-text-editor/src/core/apply/mutation-outcome.ts +0 -55
- package/node_modules/pi-agent-text-editor/src/core/apply/output.ts +0 -276
- package/node_modules/pi-agent-text-editor/src/core/apply/prompt.ts +0 -13
- package/node_modules/pi-agent-text-editor/src/core/apply/queue.ts +0 -25
- package/node_modules/pi-agent-text-editor/src/core/apply/renderer.ts +0 -405
- package/node_modules/pi-agent-text-editor/src/core/apply/results.ts +0 -133
- package/node_modules/pi-agent-text-editor/src/core/apply/runtime.ts +0 -369
- package/node_modules/pi-agent-text-editor/src/core/apply/tool.ts +0 -148
- package/node_modules/pi-agent-text-editor/src/core/apply/transaction.ts +0 -920
- package/node_modules/pi-agent-text-editor/src/core/builtin-edit-filter.ts +0 -52
- package/src/extensions/pi-agent-text-editor/src/api/apply-renderer.ts +0 -3
- package/src/extensions/pi-agent-text-editor/src/core/apply/apply-undo-store.ts +0 -155
- package/src/extensions/pi-agent-text-editor/src/core/apply/execution.ts +0 -344
- package/src/extensions/pi-agent-text-editor/src/core/apply/final-mutations.ts +0 -50
- package/src/extensions/pi-agent-text-editor/src/core/apply/format-source.ts +0 -11
- package/src/extensions/pi-agent-text-editor/src/core/apply/mixed-source.ts +0 -203
- package/src/extensions/pi-agent-text-editor/src/core/apply/mutation-outcome.ts +0 -55
- package/src/extensions/pi-agent-text-editor/src/core/apply/output.ts +0 -276
- package/src/extensions/pi-agent-text-editor/src/core/apply/prompt.ts +0 -13
- package/src/extensions/pi-agent-text-editor/src/core/apply/queue.ts +0 -25
- package/src/extensions/pi-agent-text-editor/src/core/apply/renderer.ts +0 -405
- package/src/extensions/pi-agent-text-editor/src/core/apply/results.ts +0 -133
- package/src/extensions/pi-agent-text-editor/src/core/apply/runtime.ts +0 -369
- package/src/extensions/pi-agent-text-editor/src/core/apply/tool.ts +0 -148
- package/src/extensions/pi-agent-text-editor/src/core/apply/transaction.ts +0 -920
- package/src/extensions/pi-agent-text-editor/src/core/builtin-edit-filter.ts +0 -52
|
@@ -0,0 +1,820 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
import { requiredValue } from "pi-agent-invariant";
|
|
3
|
+
import { fileURLToPath } from "node:url";
|
|
4
|
+
import type {
|
|
5
|
+
AgentToolResult,
|
|
6
|
+
ExtensionAPI,
|
|
7
|
+
ExtensionContext,
|
|
8
|
+
} from "@earendil-works/pi-coding-agent";
|
|
9
|
+
import type { AnyTextMutationToolRegistration } from "#src/api/mutation-tool.js";
|
|
10
|
+
import { getLastResolvedResource } from "#src/api/last-resolved-resource.js";
|
|
11
|
+
import { isMutationAnchorValue } from "#src/api/mutation-tool.js";
|
|
12
|
+
import type { FileMutationBatchResult, FileMutationResult } from "#src/api/mutation-result.js";
|
|
13
|
+
import type { TextBatchEntry } from "./text-edit-batch.js";
|
|
14
|
+
import type { TextEditCompletion } from "#src/api/edit-completion.js";
|
|
15
|
+
import type { TextEditorCore, TextResourceEditRequest } from "./text-editor-core.js";
|
|
16
|
+
import { BatchExecutionJournal } from "./text-edit-batch-execution.js";
|
|
17
|
+
import {
|
|
18
|
+
executeRegisteredTextBatch,
|
|
19
|
+
planRegisteredTextBatch,
|
|
20
|
+
type PlannedTextBatch,
|
|
21
|
+
} from "./text-edit-batch-registrar.js";
|
|
22
|
+
import { buildFailedTextMutationResult, mutationSources } from "./text-mutation.js";
|
|
23
|
+
import { isWholeFileInvocation } from "./file-operation-tools.js";
|
|
24
|
+
import { FileMutationAgentResult } from "./mutation-result/file-mutation-agent-result.js";
|
|
25
|
+
import { Type } from "typebox";
|
|
26
|
+
import {
|
|
27
|
+
ResourceScheduler,
|
|
28
|
+
resourceAccesses,
|
|
29
|
+
connectResultTargets,
|
|
30
|
+
resultError,
|
|
31
|
+
withStructuredResult,
|
|
32
|
+
type ResultTargetStore,
|
|
33
|
+
type StructuredResult,
|
|
34
|
+
} from "pi-agent-resource";
|
|
35
|
+
import { createPostEditScope } from "./post-edit-scope.js";
|
|
36
|
+
import { committedMutationTargets } from "./mutation-result-targets.js";
|
|
37
|
+
import { FileMutationResult as MutationPresentation } from "./mutation-result/file-mutation-result.js";
|
|
38
|
+
import { isFormattingContribution, isDiffStatusContribution } from "#src/api/post-edit.js";
|
|
39
|
+
import { captureScriptMutation } from "./text-mutation.js";
|
|
40
|
+
import {
|
|
41
|
+
NATIVE_EDIT_BATCH_EVENT,
|
|
42
|
+
type NativeEditBatchEvent,
|
|
43
|
+
} from "#src/api/native-edit-batch-event.js";
|
|
44
|
+
import {
|
|
45
|
+
mutationDataSchema,
|
|
46
|
+
mutationOutputSchema,
|
|
47
|
+
mutationOutcome,
|
|
48
|
+
type MutationData,
|
|
49
|
+
} from "./structured-result.js";
|
|
50
|
+
|
|
51
|
+
interface PendingBatch {
|
|
52
|
+
readonly entries: TextBatchEntry[];
|
|
53
|
+
readonly snapshots: Map<string, string>;
|
|
54
|
+
readonly existence: Map<string, boolean>;
|
|
55
|
+
readonly requests: Map<string, TextResourceEditRequest>;
|
|
56
|
+
plan: PlannedTextBatch;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
interface BatchSummary {
|
|
60
|
+
readonly calls: readonly { readonly id: string; readonly state: string }[];
|
|
61
|
+
readonly applied: boolean;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
interface ScriptBatch {
|
|
65
|
+
readonly reports: StructuredResult<MutationData>[];
|
|
66
|
+
reportCursor: number;
|
|
67
|
+
readonly id: string;
|
|
68
|
+
readonly context: ExtensionContext;
|
|
69
|
+
readonly summaries: BatchSummary[];
|
|
70
|
+
readonly results: FileMutationResult[];
|
|
71
|
+
readonly presentations: NativeEditBatchEvent[];
|
|
72
|
+
readonly errors: string[];
|
|
73
|
+
readonly cancellation: AbortController;
|
|
74
|
+
readonly targets: Map<string, string>;
|
|
75
|
+
readonly postEdits: ReturnType<typeof createPostEditScope>;
|
|
76
|
+
tail: Promise<void>;
|
|
77
|
+
readonly scheduler: ResourceScheduler;
|
|
78
|
+
readonly callOrder: Map<string, number>;
|
|
79
|
+
pending: PendingBatch;
|
|
80
|
+
closed: boolean;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function mergedFiles(files: MutationData["files"]): MutationData["files"] {
|
|
84
|
+
const merged = new Map<string, MutationData["files"][number]>();
|
|
85
|
+
for (const file of files) {
|
|
86
|
+
const previous = merged.get(file.source);
|
|
87
|
+
const effect =
|
|
88
|
+
previous?.effect === "unknown" || file.effect === "unknown"
|
|
89
|
+
? "unknown"
|
|
90
|
+
: previous?.effect === "applied" || file.effect === "applied"
|
|
91
|
+
? "applied"
|
|
92
|
+
: file.effect;
|
|
93
|
+
merged.set(file.source, { ...file, effect });
|
|
94
|
+
}
|
|
95
|
+
return [...merged.values()];
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
function receiptEffect(files: MutationData["files"]): MutationData["effect"] {
|
|
99
|
+
return files.some((file) => file.effect === "unknown")
|
|
100
|
+
? "unknown"
|
|
101
|
+
: files.some((file) => file.effect === "applied")
|
|
102
|
+
? "applied"
|
|
103
|
+
: "not-applied";
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
function receiptStatus(
|
|
107
|
+
errors: readonly unknown[],
|
|
108
|
+
operations: NonNullable<MutationData["operations"]>,
|
|
109
|
+
) {
|
|
110
|
+
return errors.length === 0
|
|
111
|
+
? ("success" as const)
|
|
112
|
+
: operations.some(
|
|
113
|
+
(operation) => operation.effect === "applied" && operation.errors.length === 0,
|
|
114
|
+
) &&
|
|
115
|
+
operations.some(
|
|
116
|
+
(operation) => operation.errors.length > 0 || operation.effect !== "applied",
|
|
117
|
+
)
|
|
118
|
+
? ("partial" as const)
|
|
119
|
+
: ("error" as const);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
function assertOpen(script: ScriptBatch): void {
|
|
123
|
+
if (script.closed) throw new Error("Codemode editor batch is closed.");
|
|
124
|
+
}
|
|
125
|
+
function pendingBatch(): PendingBatch {
|
|
126
|
+
return {
|
|
127
|
+
entries: [],
|
|
128
|
+
snapshots: new Map(),
|
|
129
|
+
existence: new Map(),
|
|
130
|
+
requests: new Map(),
|
|
131
|
+
plan: { changes: new Map(), mutations: [], failures: [] },
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
function isBatchable(
|
|
136
|
+
core: TextEditorCore,
|
|
137
|
+
registration: AnyTextMutationToolRegistration,
|
|
138
|
+
input: Readonly<Record<string, unknown>>,
|
|
139
|
+
): boolean {
|
|
140
|
+
if (
|
|
141
|
+
registration.direct?.matches(input) ||
|
|
142
|
+
registration.intent === "restore" ||
|
|
143
|
+
isWholeFileInvocation(registration.wholeFileOperation, input) ||
|
|
144
|
+
core.getSemanticMutationHandler(registration.name, input)
|
|
145
|
+
)
|
|
146
|
+
return false;
|
|
147
|
+
const fields = [
|
|
148
|
+
registration.source.field,
|
|
149
|
+
...(registration.source.targets ?? []).map((target) => target.field),
|
|
150
|
+
...(registration.anchors ?? []).map((anchor) => anchor.field),
|
|
151
|
+
];
|
|
152
|
+
return (
|
|
153
|
+
fields.every((field) => {
|
|
154
|
+
const value = input[field];
|
|
155
|
+
return (
|
|
156
|
+
typeof value !== "string" ||
|
|
157
|
+
!/^[a-z][a-z\d+.-]+:/iu.test(value) ||
|
|
158
|
+
value.startsWith("file://")
|
|
159
|
+
);
|
|
160
|
+
}) &&
|
|
161
|
+
typeof input[registration.source.field] === "string" &&
|
|
162
|
+
String(input[registration.source.field]).length > 0
|
|
163
|
+
);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
class NativeTextEditBatchCoordinator {
|
|
167
|
+
private readonly scripts = new Map<string, ScriptBatch>();
|
|
168
|
+
private readonly invocations = new Map<string, ScriptBatch>();
|
|
169
|
+
private readonly scopedInvocations = new Map<string, ScriptBatch>();
|
|
170
|
+
private readonly resultTargets: ResultTargetStore;
|
|
171
|
+
|
|
172
|
+
constructor(
|
|
173
|
+
private readonly core: TextEditorCore,
|
|
174
|
+
private readonly pi: ExtensionAPI,
|
|
175
|
+
) {
|
|
176
|
+
this.resultTargets = connectResultTargets(pi);
|
|
177
|
+
pi.registerTool({
|
|
178
|
+
name: "flush",
|
|
179
|
+
exposure: "codemode",
|
|
180
|
+
namespace: {
|
|
181
|
+
name: "ide_edit",
|
|
182
|
+
description: "Edit files and live IDE resources with guarded operations.",
|
|
183
|
+
},
|
|
184
|
+
annotations: {
|
|
185
|
+
readOnlyHint: false,
|
|
186
|
+
destructiveHint: true,
|
|
187
|
+
idempotentHint: false,
|
|
188
|
+
openWorldHint: false,
|
|
189
|
+
},
|
|
190
|
+
label: "Flush",
|
|
191
|
+
description:
|
|
192
|
+
"Use flush to commit pending native Codemode text edits and return their observed effects. Await tools.flush({}) before depending on a completed write. A failed flush retains applied effects and does not replay edits. Call it only inside native Codemode.",
|
|
193
|
+
promptSnippet: "Commit pending native editor changes and inspect their effects",
|
|
194
|
+
parameters: Type.Object({}, { additionalProperties: false }),
|
|
195
|
+
outputSchema: mutationOutputSchema,
|
|
196
|
+
execute: async (id) => {
|
|
197
|
+
const script = this.invocations.get(id);
|
|
198
|
+
if (!script) throw new Error("Flush requires an active native Codemode script");
|
|
199
|
+
const work = script.scheduler.run(undefined, async () => {
|
|
200
|
+
await this.commit(script);
|
|
201
|
+
const reports = script.reports.slice(script.reportCursor);
|
|
202
|
+
script.reportCursor = script.reports.length;
|
|
203
|
+
const files = mergedFiles(reports.flatMap((report) => report.data?.files ?? []));
|
|
204
|
+
const errors = reports.flatMap((report) => report.errors);
|
|
205
|
+
const operations = reports.flatMap((report) => report.data?.operations ?? []);
|
|
206
|
+
const effect = receiptEffect(files);
|
|
207
|
+
return withStructuredResult(
|
|
208
|
+
{
|
|
209
|
+
content: [
|
|
210
|
+
{
|
|
211
|
+
type: "text",
|
|
212
|
+
text:
|
|
213
|
+
errors.length === 0
|
|
214
|
+
? `Flushed ${operations.length} editor operations; ${effect}.`
|
|
215
|
+
: `Editor flush failed; ${effect}.\n${errors.map((error) => error.message).join("\n")}`,
|
|
216
|
+
},
|
|
217
|
+
],
|
|
218
|
+
details: { results: [] },
|
|
219
|
+
},
|
|
220
|
+
mutationDataSchema,
|
|
221
|
+
{
|
|
222
|
+
status: receiptStatus(errors, operations),
|
|
223
|
+
data: { operation: "flush", effect, files, operations },
|
|
224
|
+
errors,
|
|
225
|
+
},
|
|
226
|
+
);
|
|
227
|
+
});
|
|
228
|
+
script.tail = work.then(
|
|
229
|
+
() => undefined,
|
|
230
|
+
() => undefined,
|
|
231
|
+
);
|
|
232
|
+
return work;
|
|
233
|
+
},
|
|
234
|
+
});
|
|
235
|
+
pi.on("tool_call", async (event, context) => {
|
|
236
|
+
if (event.toolName === "codemode" && event.parentToolCallId === undefined) {
|
|
237
|
+
this.scripts.set(event.toolCallId, {
|
|
238
|
+
id: event.toolCallId,
|
|
239
|
+
context,
|
|
240
|
+
summaries: [],
|
|
241
|
+
reports: [],
|
|
242
|
+
reportCursor: 0,
|
|
243
|
+
presentations: [],
|
|
244
|
+
results: [],
|
|
245
|
+
errors: [],
|
|
246
|
+
cancellation: new AbortController(),
|
|
247
|
+
targets: new Map(),
|
|
248
|
+
postEdits: createPostEditScope(context.cwd),
|
|
249
|
+
tail: Promise.resolve(),
|
|
250
|
+
scheduler: new ResourceScheduler(),
|
|
251
|
+
callOrder: new Map(),
|
|
252
|
+
pending: pendingBatch(),
|
|
253
|
+
closed: false,
|
|
254
|
+
});
|
|
255
|
+
return;
|
|
256
|
+
}
|
|
257
|
+
const script =
|
|
258
|
+
event.parentToolCallId === undefined ? undefined : this.scripts.get(event.parentToolCallId);
|
|
259
|
+
if (!script) return;
|
|
260
|
+
if (event.toolName === "flush") {
|
|
261
|
+
this.invocations.set(event.toolCallId, script);
|
|
262
|
+
return;
|
|
263
|
+
}
|
|
264
|
+
const registration = this.core
|
|
265
|
+
.getMutationTools()
|
|
266
|
+
.find((tool) => tool.name === event.toolName);
|
|
267
|
+
if (registration) this.scopedInvocations.set(event.toolCallId, script);
|
|
268
|
+
const input: Record<string, unknown> = event.input;
|
|
269
|
+
let ownsSource = false;
|
|
270
|
+
if (registration && !isWholeFileInvocation(registration.wholeFileOperation, input)) {
|
|
271
|
+
const resolverContext = {
|
|
272
|
+
cwd: context.cwd,
|
|
273
|
+
...(context.signal !== undefined && { signal: context.signal }),
|
|
274
|
+
};
|
|
275
|
+
for (const descriptor of [registration.source, ...(registration.source.targets ?? [])]) {
|
|
276
|
+
const value = input[descriptor.field];
|
|
277
|
+
if (typeof value === "string" && value.length > 0) {
|
|
278
|
+
if (
|
|
279
|
+
["replace", "write", "copy", "move", "delete", "undo"].includes(registration.name) &&
|
|
280
|
+
value.startsWith("RESULT#")
|
|
281
|
+
) {
|
|
282
|
+
// The executor validates result handles and returns a structured rejection.
|
|
283
|
+
ownsSource = true;
|
|
284
|
+
continue;
|
|
285
|
+
}
|
|
286
|
+
const result = await this.core.textTargetResolver().tryResolve(value, resolverContext);
|
|
287
|
+
if (result.kind !== "not-handled") ownsSource = true;
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
for (const anchor of registration.anchors ?? []) {
|
|
291
|
+
const value = input[anchor.field];
|
|
292
|
+
if (
|
|
293
|
+
isMutationAnchorValue(anchor, value) &&
|
|
294
|
+
(await this.core.resolveTextAnchorResources(value, anchor.kinds, resolverContext)) !==
|
|
295
|
+
undefined
|
|
296
|
+
)
|
|
297
|
+
ownsSource = true;
|
|
298
|
+
}
|
|
299
|
+
if (registration.source.inherited && !input[registration.source.field] && !ownsSource) {
|
|
300
|
+
const source = script.pending.entries.at(-1)?.path ?? getLastResolvedResource(pi)?.source;
|
|
301
|
+
if (source !== undefined) input[registration.source.field] = source;
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
if (registration && !ownsSource && isBatchable(this.core, registration, input)) {
|
|
305
|
+
this.invocations.set(event.toolCallId, script);
|
|
306
|
+
return;
|
|
307
|
+
}
|
|
308
|
+
const work = script.scheduler.run(undefined, () => this.commit(script));
|
|
309
|
+
script.tail = work.then(
|
|
310
|
+
() => undefined,
|
|
311
|
+
() => undefined,
|
|
312
|
+
);
|
|
313
|
+
if (!(await work))
|
|
314
|
+
return {
|
|
315
|
+
block: true,
|
|
316
|
+
reason:
|
|
317
|
+
"Editor batch failed before this tool. See the Codemode result; do not replay accepted edits automatically.",
|
|
318
|
+
};
|
|
319
|
+
return;
|
|
320
|
+
});
|
|
321
|
+
pi.on("tool_result", async (event) => {
|
|
322
|
+
this.invocations.delete(event.toolCallId);
|
|
323
|
+
this.scopedInvocations.delete(event.toolCallId);
|
|
324
|
+
const script = this.scripts.get(event.toolCallId);
|
|
325
|
+
if (!script) return;
|
|
326
|
+
script.closed = true;
|
|
327
|
+
const interrupted =
|
|
328
|
+
script.context.signal?.aborted ||
|
|
329
|
+
(event.isError &&
|
|
330
|
+
event.content.some(
|
|
331
|
+
(block) =>
|
|
332
|
+
block.type === "text" &&
|
|
333
|
+
/^Script error:\nScript (?:aborted|timed out):/u.test(block.text),
|
|
334
|
+
));
|
|
335
|
+
if (interrupted) script.cancellation.abort();
|
|
336
|
+
await script.tail;
|
|
337
|
+
if (interrupted) {
|
|
338
|
+
if (script.pending.entries.length > 0) {
|
|
339
|
+
const error = resultError(
|
|
340
|
+
"Pending editor batch cancelled; no pending edits were written.",
|
|
341
|
+
"CANCELLED",
|
|
342
|
+
);
|
|
343
|
+
const operations = script.pending.entries.map((entry) => ({
|
|
344
|
+
id: entry.callId,
|
|
345
|
+
operation: entry.op,
|
|
346
|
+
effect: "not-applied" as const,
|
|
347
|
+
errors: [error],
|
|
348
|
+
}));
|
|
349
|
+
script.reports.push({
|
|
350
|
+
status: "error",
|
|
351
|
+
errors: [error],
|
|
352
|
+
data: {
|
|
353
|
+
operation: "flush",
|
|
354
|
+
effect: "not-applied",
|
|
355
|
+
operations,
|
|
356
|
+
files: [...script.pending.snapshots.keys()].map((source) => ({
|
|
357
|
+
source,
|
|
358
|
+
effect: "not-applied",
|
|
359
|
+
})),
|
|
360
|
+
},
|
|
361
|
+
});
|
|
362
|
+
script.errors.push(error.message);
|
|
363
|
+
for (const entry of script.pending.entries) {
|
|
364
|
+
const target = script.targets.get(entry.callId);
|
|
365
|
+
if (target) this.resultTargets.reject(target, error.message);
|
|
366
|
+
}
|
|
367
|
+
script.pending = pendingBatch();
|
|
368
|
+
}
|
|
369
|
+
} else await this.commit(script);
|
|
370
|
+
try {
|
|
371
|
+
await this.core.enqueueFileOperation(
|
|
372
|
+
() =>
|
|
373
|
+
script.postEdits.finish((outcome) => {
|
|
374
|
+
const index = script.results.findLastIndex(
|
|
375
|
+
(item) => item.data.path === outcome.after.source,
|
|
376
|
+
);
|
|
377
|
+
if (index < 0) return;
|
|
378
|
+
const previous = requiredValue(script.results[index]).data;
|
|
379
|
+
const updated = new MutationPresentation({
|
|
380
|
+
...previous,
|
|
381
|
+
afterContent: outcome.after.content,
|
|
382
|
+
afterDocument: outcome.after,
|
|
383
|
+
formatting: outcome.postEditContributions
|
|
384
|
+
.map((item) => item.data)
|
|
385
|
+
.findLast(isFormattingContribution)?.formatting ?? { status: "not-reported" },
|
|
386
|
+
diffStatuses: outcome.postEditContributions
|
|
387
|
+
.map((item) => item.data)
|
|
388
|
+
.filter(isDiffStatusContribution)
|
|
389
|
+
.flatMap((item) => item.diffStatuses),
|
|
390
|
+
});
|
|
391
|
+
const previousResult = requiredValue(script.results[index]);
|
|
392
|
+
script.results[index] = updated;
|
|
393
|
+
for (const [slot, presentation] of script.presentations.entries()) {
|
|
394
|
+
const results = presentation.result.details.results;
|
|
395
|
+
if (!results?.includes(previousResult)) continue;
|
|
396
|
+
script.presentations[slot] = {
|
|
397
|
+
...presentation,
|
|
398
|
+
result: {
|
|
399
|
+
...presentation.result,
|
|
400
|
+
details: {
|
|
401
|
+
...presentation.result.details,
|
|
402
|
+
results: results.map((result) =>
|
|
403
|
+
result === previousResult ? updated : result,
|
|
404
|
+
),
|
|
405
|
+
},
|
|
406
|
+
},
|
|
407
|
+
};
|
|
408
|
+
}
|
|
409
|
+
}),
|
|
410
|
+
undefined,
|
|
411
|
+
{ cwd: script.context.cwd, sources: script.postEdits.sources() },
|
|
412
|
+
);
|
|
413
|
+
} catch (error) {
|
|
414
|
+
script.errors.push(error instanceof Error ? error.message : String(error));
|
|
415
|
+
}
|
|
416
|
+
// Nested panels are frozen on message_end. Publish final snapshots before history is saved.
|
|
417
|
+
for (const presentation of script.presentations) {
|
|
418
|
+
const results = presentation.result.details.results ?? [];
|
|
419
|
+
this.pi.events.emit(NATIVE_EDIT_BATCH_EVENT, {
|
|
420
|
+
...presentation,
|
|
421
|
+
result: {
|
|
422
|
+
...presentation.result,
|
|
423
|
+
content: [new FileMutationAgentResult(results).toTextContent()],
|
|
424
|
+
},
|
|
425
|
+
} satisfies NativeEditBatchEvent);
|
|
426
|
+
}
|
|
427
|
+
this.scripts.delete(event.toolCallId);
|
|
428
|
+
if (
|
|
429
|
+
script.results.length === 0 &&
|
|
430
|
+
script.summaries.length === 0 &&
|
|
431
|
+
script.errors.length === 0
|
|
432
|
+
)
|
|
433
|
+
return;
|
|
434
|
+
const failed = script.errors.length > 0;
|
|
435
|
+
const content = event.content.map((block) =>
|
|
436
|
+
failed && block.type === "text"
|
|
437
|
+
? { ...block, text: block.text.replace(/^Script completed\n/u, "Script failed\n") }
|
|
438
|
+
: block,
|
|
439
|
+
);
|
|
440
|
+
content.push({
|
|
441
|
+
type: "text",
|
|
442
|
+
text: `Editor batches: ${script.summaries.filter((batch) => batch.applied).length} committed.${failed ? "\n" + script.errors.join("\n") : ""}`,
|
|
443
|
+
});
|
|
444
|
+
if (script.results.length > 0)
|
|
445
|
+
content.push(new FileMutationAgentResult(script.results).toTextContent());
|
|
446
|
+
const details =
|
|
447
|
+
typeof event.details === "object" && event.details !== null ? event.details : {};
|
|
448
|
+
return {
|
|
449
|
+
content,
|
|
450
|
+
details: {
|
|
451
|
+
...details,
|
|
452
|
+
editorBatches: script.summaries,
|
|
453
|
+
editorBatchResults: script.reports,
|
|
454
|
+
},
|
|
455
|
+
...(event.structuredContent === undefined
|
|
456
|
+
? {}
|
|
457
|
+
: { structuredContent: event.structuredContent }),
|
|
458
|
+
isError: event.isError || failed,
|
|
459
|
+
};
|
|
460
|
+
});
|
|
461
|
+
const clear = () => {
|
|
462
|
+
for (const script of this.scripts.values()) {
|
|
463
|
+
script.closed = true;
|
|
464
|
+
script.cancellation.abort();
|
|
465
|
+
}
|
|
466
|
+
this.scripts.clear();
|
|
467
|
+
this.invocations.clear();
|
|
468
|
+
this.scopedInvocations.clear();
|
|
469
|
+
};
|
|
470
|
+
pi.on("agent_end", clear);
|
|
471
|
+
pi.on("session_shutdown", clear);
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
/** Keep every mutation in the script's shared post-edit scope, including non-batched calls. */
|
|
475
|
+
runPostEdits<T>(id: string, work: () => T): T {
|
|
476
|
+
const script = this.scopedInvocations.get(id);
|
|
477
|
+
return script ? script.postEdits.run(work) : work();
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
/** Include non-batched mutations in the script's final post-edit presentation. */
|
|
481
|
+
recordMutation(id: string, result: AgentToolResult<FileMutationBatchResult>): void {
|
|
482
|
+
const script = this.scopedInvocations.get(id);
|
|
483
|
+
if (!script || !result.details.results?.some((item) => item.data.ok === true)) return;
|
|
484
|
+
script.results.push(...result.details.results);
|
|
485
|
+
script.presentations.push({ parentToolCallId: script.id, calls: [id], result });
|
|
486
|
+
}
|
|
487
|
+
execute(
|
|
488
|
+
id: string,
|
|
489
|
+
registration: AnyTextMutationToolRegistration,
|
|
490
|
+
input: Readonly<Record<string, unknown>>,
|
|
491
|
+
signal: AbortSignal | undefined,
|
|
492
|
+
context: ExtensionContext,
|
|
493
|
+
): Promise<AgentToolResult<FileMutationBatchResult>> | undefined {
|
|
494
|
+
const script = this.invocations.get(id);
|
|
495
|
+
if (!script) return undefined;
|
|
496
|
+
script.callOrder.set(id, script.callOrder.size);
|
|
497
|
+
const sources = [...mutationSources(registration, input).values()].map((source) =>
|
|
498
|
+
source.startsWith("file://")
|
|
499
|
+
? fileURLToPath(source)
|
|
500
|
+
: path.resolve(context.cwd, source.startsWith("@") ? source.slice(1) : source),
|
|
501
|
+
);
|
|
502
|
+
const effectiveSignal =
|
|
503
|
+
signal === undefined
|
|
504
|
+
? script.cancellation.signal
|
|
505
|
+
: AbortSignal.any([signal, script.cancellation.signal]);
|
|
506
|
+
const work = script.scheduler.run(
|
|
507
|
+
Promise.all(sources.map((source) => resourceAccesses(source, context.cwd, "write"))).then(
|
|
508
|
+
(sets) => sets.flat(),
|
|
509
|
+
),
|
|
510
|
+
() => this.accept(script, id, registration, input, effectiveSignal, context),
|
|
511
|
+
effectiveSignal,
|
|
512
|
+
);
|
|
513
|
+
script.tail = Promise.allSettled([script.tail, work]).then(() => undefined);
|
|
514
|
+
return work;
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
private async accept(
|
|
518
|
+
script: ScriptBatch,
|
|
519
|
+
id: string,
|
|
520
|
+
registration: AnyTextMutationToolRegistration,
|
|
521
|
+
input: Readonly<Record<string, unknown>>,
|
|
522
|
+
signal: AbortSignal | undefined,
|
|
523
|
+
context: ExtensionContext,
|
|
524
|
+
): Promise<AgentToolResult<FileMutationBatchResult>> {
|
|
525
|
+
signal?.throwIfAborted();
|
|
526
|
+
assertOpen(script);
|
|
527
|
+
const normalized = { ...input };
|
|
528
|
+
for (const descriptor of [registration.source, ...(registration.source.targets ?? [])]) {
|
|
529
|
+
const source = normalized[descriptor.field];
|
|
530
|
+
if (typeof source === "string" && source.length > 0)
|
|
531
|
+
normalized[descriptor.field] = source.startsWith("file://")
|
|
532
|
+
? fileURLToPath(source)
|
|
533
|
+
: path.resolve(context.cwd, source.startsWith("@") ? source.slice(1) : source);
|
|
534
|
+
}
|
|
535
|
+
const entry: TextBatchEntry = {
|
|
536
|
+
...normalized,
|
|
537
|
+
callId: id,
|
|
538
|
+
op: registration.name,
|
|
539
|
+
path: String(normalized[registration.source.field]),
|
|
540
|
+
};
|
|
541
|
+
const batch = script.pending;
|
|
542
|
+
const requests = new Map<string, TextResourceEditRequest>();
|
|
543
|
+
for (const source of mutationSources(registration, entry).values())
|
|
544
|
+
requests.set(source, {
|
|
545
|
+
source,
|
|
546
|
+
read: true,
|
|
547
|
+
...(registration.name === "write" && { allowReadFailure: true }),
|
|
548
|
+
...(batch.existence.has(source) && { expectedExistence: batch.existence.get(source) }),
|
|
549
|
+
});
|
|
550
|
+
const planning: { plan?: PlannedTextBatch; cause?: unknown } = {};
|
|
551
|
+
const preview = await this.core.previewTexts(
|
|
552
|
+
[...requests.values()],
|
|
553
|
+
{ cwd: context.cwd, ...(signal !== undefined && { signal }) },
|
|
554
|
+
async (texts, resolveAnchor) => {
|
|
555
|
+
try {
|
|
556
|
+
for (const [source, expected] of batch.snapshots)
|
|
557
|
+
if (requests.has(source) && texts.get(source) !== expected)
|
|
558
|
+
throw new Error(`Snapshot source ${source} changed before the edit batch.`);
|
|
559
|
+
planning.plan = await planRegisteredTextBatch(
|
|
560
|
+
new Map([[registration.name, registration]]),
|
|
561
|
+
{ edits: [entry], failureMode: "abort" },
|
|
562
|
+
texts,
|
|
563
|
+
resolveAnchor,
|
|
564
|
+
context,
|
|
565
|
+
signal,
|
|
566
|
+
() => {},
|
|
567
|
+
new Map([...batch.plan.changes].filter(([source]) => requests.has(source))),
|
|
568
|
+
);
|
|
569
|
+
return { changes: planning.plan.changes, result: planning.plan };
|
|
570
|
+
} catch (error) {
|
|
571
|
+
planning.cause = error;
|
|
572
|
+
throw error;
|
|
573
|
+
}
|
|
574
|
+
},
|
|
575
|
+
);
|
|
576
|
+
if (preview.kind === "failed") {
|
|
577
|
+
const result = await buildFailedTextMutationResult(
|
|
578
|
+
this.core,
|
|
579
|
+
{
|
|
580
|
+
code: "INVALID_REQUEST",
|
|
581
|
+
source: entry.path,
|
|
582
|
+
message: preview.reason,
|
|
583
|
+
cause: planning.cause,
|
|
584
|
+
},
|
|
585
|
+
context,
|
|
586
|
+
);
|
|
587
|
+
return { ...result, isError: true };
|
|
588
|
+
}
|
|
589
|
+
signal?.throwIfAborted();
|
|
590
|
+
assertOpen(script);
|
|
591
|
+
if (!planning.plan) throw new Error("Editor batch planning did not complete.");
|
|
592
|
+
for (const resource of preview.resources) {
|
|
593
|
+
batch.snapshots.set(resource.path, resource.beforeContent);
|
|
594
|
+
batch.existence.set(resource.path, resource.existed ?? true);
|
|
595
|
+
}
|
|
596
|
+
for (const [source, request] of requests) batch.requests.set(source, request);
|
|
597
|
+
batch.entries.push(entry);
|
|
598
|
+
const order = (left: { callId: string }, right: { callId: string }) =>
|
|
599
|
+
requiredValue(script.callOrder.get(left.callId)) -
|
|
600
|
+
requiredValue(script.callOrder.get(right.callId));
|
|
601
|
+
batch.entries.sort(order);
|
|
602
|
+
batch.plan = {
|
|
603
|
+
changes: new Map([...batch.plan.changes, ...planning.plan.changes]),
|
|
604
|
+
mutations: [...batch.plan.mutations, ...planning.plan.mutations].sort(order),
|
|
605
|
+
failures: [...batch.plan.failures, ...planning.plan.failures],
|
|
606
|
+
};
|
|
607
|
+
const target = ["replace", "insert", "write", "copy", "move"].includes(registration.name)
|
|
608
|
+
? this.resultTargets.reserve(context.cwd)
|
|
609
|
+
: undefined;
|
|
610
|
+
if (target) script.targets.set(id, target);
|
|
611
|
+
const details = {
|
|
612
|
+
results: [],
|
|
613
|
+
source: entry.path,
|
|
614
|
+
resolvedBy: preview.resources.find((resource) => resource.path === entry.path)?.resolvedBy,
|
|
615
|
+
nativeEditBatch: { parentToolCallId: script.id, state: "accepted" },
|
|
616
|
+
...(target ? { metadata: { resultTarget: target } } : {}),
|
|
617
|
+
};
|
|
618
|
+
return {
|
|
619
|
+
content: [
|
|
620
|
+
{
|
|
621
|
+
type: "text",
|
|
622
|
+
text: `Accepted ${registration.name} for ${entry.path}; not yet applied. The editor batch commits before another tool or when this script ends.`,
|
|
623
|
+
},
|
|
624
|
+
],
|
|
625
|
+
details,
|
|
626
|
+
};
|
|
627
|
+
}
|
|
628
|
+
|
|
629
|
+
private async settleTargets(
|
|
630
|
+
script: ScriptBatch,
|
|
631
|
+
batch: PendingBatch,
|
|
632
|
+
journal: BatchExecutionJournal,
|
|
633
|
+
completions: readonly TextEditCompletion[],
|
|
634
|
+
signal: AbortSignal,
|
|
635
|
+
): Promise<void> {
|
|
636
|
+
let mapped: ReturnType<typeof committedMutationTargets>;
|
|
637
|
+
try {
|
|
638
|
+
mapped = committedMutationTargets(
|
|
639
|
+
batch.plan.mutations.map(({ callId, mutation }) => ({ callId, edits: mutation.edits })),
|
|
640
|
+
completions,
|
|
641
|
+
);
|
|
642
|
+
} catch (error) {
|
|
643
|
+
for (const entry of batch.entries) {
|
|
644
|
+
const target = script.targets.get(entry.callId);
|
|
645
|
+
if (target)
|
|
646
|
+
this.resultTargets.reject(target, error instanceof Error ? error.message : String(error));
|
|
647
|
+
}
|
|
648
|
+
return;
|
|
649
|
+
}
|
|
650
|
+
const calls = journal.snapshot();
|
|
651
|
+
for (const entry of batch.entries) {
|
|
652
|
+
const reference = script.targets.get(entry.callId);
|
|
653
|
+
if (!reference) continue;
|
|
654
|
+
const targets = mapped.get(entry.callId);
|
|
655
|
+
if (calls.find((call) => call.callId === entry.callId)?.state !== "completed" || !targets) {
|
|
656
|
+
this.resultTargets.reject(
|
|
657
|
+
reference,
|
|
658
|
+
"This operation did not produce confirmed filesystem targets.",
|
|
659
|
+
);
|
|
660
|
+
continue;
|
|
661
|
+
}
|
|
662
|
+
try {
|
|
663
|
+
await this.resultTargets.verify({ targets, complete: true }, signal);
|
|
664
|
+
this.resultTargets.confirm(reference, targets, script.context.cwd);
|
|
665
|
+
} catch (error) {
|
|
666
|
+
this.resultTargets.reject(
|
|
667
|
+
reference,
|
|
668
|
+
error instanceof Error ? error.message : String(error),
|
|
669
|
+
);
|
|
670
|
+
}
|
|
671
|
+
}
|
|
672
|
+
}
|
|
673
|
+
|
|
674
|
+
private async commit(script: ScriptBatch): Promise<boolean> {
|
|
675
|
+
const batch = script.pending;
|
|
676
|
+
if (batch.entries.length === 0) return true;
|
|
677
|
+
script.pending = pendingBatch();
|
|
678
|
+
const journal = new BatchExecutionJournal(batch.entries.map((entry) => entry.callId));
|
|
679
|
+
const registrations = new Map(this.core.getMutationTools().map((tool) => [tool.name, tool]));
|
|
680
|
+
const signal =
|
|
681
|
+
script.context.signal === undefined
|
|
682
|
+
? script.cancellation.signal
|
|
683
|
+
: AbortSignal.any([script.context.signal, script.cancellation.signal]);
|
|
684
|
+
let observedSources: string[] = [];
|
|
685
|
+
const record = (outcome: StructuredResult<MutationData>): boolean => {
|
|
686
|
+
const calls = journal.snapshot();
|
|
687
|
+
const operations = calls.map((call) => ({
|
|
688
|
+
id: call.callId,
|
|
689
|
+
operation: requiredValue(batch.entries.find((entry) => entry.callId === call.callId)).op,
|
|
690
|
+
effect:
|
|
691
|
+
call.state === "completed" || call.state === "failed-applied"
|
|
692
|
+
? ("applied" as const)
|
|
693
|
+
: call.state === "failed-unknown" || call.state === "running"
|
|
694
|
+
? ("unknown" as const)
|
|
695
|
+
: ("not-applied" as const),
|
|
696
|
+
errors: call.failure === undefined ? [] : [resultError(call.failure.error)],
|
|
697
|
+
}));
|
|
698
|
+
const files = mergedFiles([
|
|
699
|
+
...operations.flatMap((operation) => {
|
|
700
|
+
const entry = requiredValue(batch.entries.find((entry) => entry.callId === operation.id));
|
|
701
|
+
const registration = requiredValue(registrations.get(entry.op));
|
|
702
|
+
const mutation = batch.plan.mutations.find(
|
|
703
|
+
(item) => item.callId === operation.id,
|
|
704
|
+
)?.mutation;
|
|
705
|
+
return [...mutationSources(registration, entry).values()].map((source) => ({
|
|
706
|
+
source,
|
|
707
|
+
effect:
|
|
708
|
+
mutation?.edits.has(source) === false ? ("not-applied" as const) : operation.effect,
|
|
709
|
+
}));
|
|
710
|
+
}),
|
|
711
|
+
...observedSources.map((source) => ({ source, effect: "applied" as const })),
|
|
712
|
+
]).map((file) =>
|
|
713
|
+
observedSources.includes(file.source) ? { ...file, effect: "applied" as const } : file,
|
|
714
|
+
);
|
|
715
|
+
const errors = [
|
|
716
|
+
...new Map(
|
|
717
|
+
[...outcome.errors, ...operations.flatMap((operation) => operation.errors)].map(
|
|
718
|
+
(error) => [JSON.stringify(error), error],
|
|
719
|
+
),
|
|
720
|
+
).values(),
|
|
721
|
+
];
|
|
722
|
+
const completed = calls.every((call) => call.state === "completed");
|
|
723
|
+
if (!completed && errors.length === 0)
|
|
724
|
+
errors.push(resultError("Editor batch did not complete", "BATCH_FAILED"));
|
|
725
|
+
script.reports.push({
|
|
726
|
+
status: receiptStatus(errors, operations),
|
|
727
|
+
errors,
|
|
728
|
+
data: {
|
|
729
|
+
...outcome.data,
|
|
730
|
+
operation: "flush",
|
|
731
|
+
effect: receiptEffect(files),
|
|
732
|
+
files,
|
|
733
|
+
operations,
|
|
734
|
+
},
|
|
735
|
+
});
|
|
736
|
+
script.summaries.push({
|
|
737
|
+
calls: calls.map((call) => ({ id: call.callId, state: call.state })),
|
|
738
|
+
applied:
|
|
739
|
+
operations.some((operation) => operation.effect === "applied") ||
|
|
740
|
+
observedSources.length > 0,
|
|
741
|
+
});
|
|
742
|
+
if (!completed)
|
|
743
|
+
script.errors.push(
|
|
744
|
+
"Editor batch failed; inspect its final file results. Accepted edits were not replayed.",
|
|
745
|
+
);
|
|
746
|
+
return completed;
|
|
747
|
+
};
|
|
748
|
+
try {
|
|
749
|
+
signal.throwIfAborted();
|
|
750
|
+
const captured = await script.postEdits.run(() =>
|
|
751
|
+
captureScriptMutation(this.core, () =>
|
|
752
|
+
executeRegisteredTextBatch(
|
|
753
|
+
this.core,
|
|
754
|
+
registrations,
|
|
755
|
+
{
|
|
756
|
+
edits: batch.entries,
|
|
757
|
+
expectedContent: batch.snapshots,
|
|
758
|
+
expectedExistence: batch.existence,
|
|
759
|
+
},
|
|
760
|
+
signal,
|
|
761
|
+
undefined,
|
|
762
|
+
script.context,
|
|
763
|
+
journal.reporter(),
|
|
764
|
+
() => {},
|
|
765
|
+
batch.plan,
|
|
766
|
+
),
|
|
767
|
+
),
|
|
768
|
+
);
|
|
769
|
+
observedSources = captured.completions.map((completion) => completion.resourceSource);
|
|
770
|
+
if (captured.kind === "failed") throw captured.error;
|
|
771
|
+
script.results.push(...(captured.value.details.results ?? []));
|
|
772
|
+
await this.settleTargets(script, batch, journal, captured.completions, signal);
|
|
773
|
+
const presentation = {
|
|
774
|
+
parentToolCallId: script.id,
|
|
775
|
+
calls: batch.entries.map((entry) => entry.callId),
|
|
776
|
+
result: captured.value,
|
|
777
|
+
} satisfies NativeEditBatchEvent;
|
|
778
|
+
script.presentations.push(presentation);
|
|
779
|
+
this.pi.events.emit(NATIVE_EDIT_BATCH_EVENT, presentation);
|
|
780
|
+
return record(mutationOutcome(captured.value, "flush", captured.completions));
|
|
781
|
+
} catch (error) {
|
|
782
|
+
journal.markRunningUnknown(error);
|
|
783
|
+
await this.settleTargets(script, batch, journal, [], signal);
|
|
784
|
+
return record({ status: "error", errors: [resultError(error)] });
|
|
785
|
+
}
|
|
786
|
+
}
|
|
787
|
+
}
|
|
788
|
+
|
|
789
|
+
const coordinators = new WeakMap<TextEditorCore, NativeTextEditBatchCoordinator>();
|
|
790
|
+
|
|
791
|
+
/** Run immediate mutation paths under the same deferred post-edit boundary as native batches. */
|
|
792
|
+
export function runNativePostEditScope<T>(core: TextEditorCore, id: string, work: () => T): T {
|
|
793
|
+
const coordinator = coordinators.get(core);
|
|
794
|
+
return coordinator ? coordinator.runPostEdits(id, work) : work();
|
|
795
|
+
}
|
|
796
|
+
|
|
797
|
+
/** Retain immediate child results so final formatting is visible on the parent script. */
|
|
798
|
+
export function recordNativeTextMutation(
|
|
799
|
+
core: TextEditorCore,
|
|
800
|
+
id: string,
|
|
801
|
+
result: AgentToolResult<FileMutationBatchResult>,
|
|
802
|
+
): void {
|
|
803
|
+
coordinators.get(core)?.recordMutation(id, result);
|
|
804
|
+
}
|
|
805
|
+
/** Attach accumulating editor batches to native Codemode's public parent/child lifecycle. */
|
|
806
|
+
export function registerNativeTextEditBatching(pi: ExtensionAPI, core: TextEditorCore): void {
|
|
807
|
+
coordinators.set(core, new NativeTextEditBatchCoordinator(core, pi));
|
|
808
|
+
}
|
|
809
|
+
|
|
810
|
+
/** Accept one nested text mutation, or leave a non-batched call on its normal execution path. */
|
|
811
|
+
export function executeNativeTextEditBatch(
|
|
812
|
+
core: TextEditorCore,
|
|
813
|
+
id: string,
|
|
814
|
+
registration: AnyTextMutationToolRegistration,
|
|
815
|
+
input: Readonly<Record<string, unknown>>,
|
|
816
|
+
signal: AbortSignal | undefined,
|
|
817
|
+
context: ExtensionContext,
|
|
818
|
+
): Promise<AgentToolResult<FileMutationBatchResult>> | undefined {
|
|
819
|
+
return coordinators.get(core)?.execute(id, registration, input, signal, context);
|
|
820
|
+
}
|