pi-agent-ide 0.1.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/LICENSE +21 -0
- package/README.md +53 -0
- package/assets/banner.png +0 -0
- package/docs/architecture.md +100 -0
- package/docs/configuration.md +104 -0
- package/docs/development.md +102 -0
- package/docs/evaluation.md +14 -0
- package/docs/extensions.md +298 -0
- package/docs/tools.md +104 -0
- package/node_modules/pi-agent-filesystem/index.ts +60 -0
- package/node_modules/pi-agent-filesystem/package.json +23 -0
- package/node_modules/pi-agent-filesystem/src/resolver.ts +175 -0
- package/node_modules/pi-agent-filesystem-image/index.ts +14 -0
- package/node_modules/pi-agent-filesystem-image/package.json +20 -0
- package/node_modules/pi-agent-filesystem-pdf/index.ts +14 -0
- package/node_modules/pi-agent-filesystem-pdf/package.json +17 -0
- package/node_modules/pi-agent-filesystem-text/index.ts +23 -0
- package/node_modules/pi-agent-filesystem-text/package.json +20 -0
- package/node_modules/pi-agent-ide-ast/index.ts +1 -0
- package/node_modules/pi-agent-ide-ast/package.json +39 -0
- package/node_modules/pi-agent-ide-ast/src/api/scope.ts +1 -0
- package/node_modules/pi-agent-ide-ast/src/ast/collect-files.ts +24 -0
- package/node_modules/pi-agent-ide-ast/src/ast/manager.ts +374 -0
- package/node_modules/pi-agent-ide-ast/src/ast/outline.ts +813 -0
- package/node_modules/pi-agent-ide-ast/src/ast/read-text.ts +30 -0
- package/node_modules/pi-agent-ide-ast/src/extension.ts +85 -0
- package/node_modules/pi-agent-ide-ast/src/outline-resolver.ts +69 -0
- package/node_modules/pi-agent-ide-ast/src/scope-handler.ts +221 -0
- package/node_modules/pi-agent-ide-ast/src/scope-resolver.ts +45 -0
- package/node_modules/pi-agent-ide-ast/src/search-resolver.ts +141 -0
- package/node_modules/pi-agent-ide-changes/index.ts +68 -0
- package/node_modules/pi-agent-ide-changes/package.json +29 -0
- package/node_modules/pi-agent-ide-changes/src/change-anchor.ts +103 -0
- package/node_modules/pi-agent-ide-changes/src/changes/change-groups.ts +309 -0
- package/node_modules/pi-agent-ide-changes/src/changes/change-service.ts +223 -0
- package/node_modules/pi-agent-ide-changes/src/changes/change-types.ts +105 -0
- package/node_modules/pi-agent-ide-changes/src/changes/git-changes-backend.ts +325 -0
- package/node_modules/pi-agent-ide-changes/src/current-change-presenter.ts +232 -0
- package/node_modules/pi-agent-ide-changes/src/index-mutation-queue.ts +11 -0
- package/node_modules/pi-agent-ide-changes/src/last-text-transaction-store.ts +88 -0
- package/node_modules/pi-agent-ide-changes/src/tool-index-change.ts +116 -0
- package/node_modules/pi-agent-ide-changes/src/tool-text-undo.ts +153 -0
- package/node_modules/pi-agent-ide-formatter/index.ts +33 -0
- package/node_modules/pi-agent-ide-formatter/package.json +14 -0
- package/node_modules/pi-agent-ide-formatter/src/formatter.ts +50 -0
- package/node_modules/pi-agent-ide-formatter/src/registry.ts +30 -0
- package/node_modules/pi-agent-ide-formatter/src/types.ts +10 -0
- package/node_modules/pi-agent-ide-lint/index.ts +237 -0
- package/node_modules/pi-agent-ide-lint/package.json +19 -0
- package/node_modules/pi-agent-ide-lint/src/command-linter.ts +160 -0
- package/node_modules/pi-agent-ide-lint/src/command-runner.ts +51 -0
- package/node_modules/pi-agent-ide-lint/src/registry.ts +30 -0
- package/node_modules/pi-agent-ide-lint/src/types.ts +10 -0
- package/node_modules/pi-agent-ide-lsp/index.ts +250 -0
- package/node_modules/pi-agent-ide-lsp/package.json +23 -0
- package/node_modules/pi-agent-ide-lsp/src/code-view-resolvers.ts +139 -0
- package/node_modules/pi-agent-ide-lsp/src/lsp/client.ts +463 -0
- package/node_modules/pi-agent-ide-lsp/src/lsp/code-views.ts +577 -0
- package/node_modules/pi-agent-ide-lsp/src/lsp/diagnostics.ts +179 -0
- package/node_modules/pi-agent-ide-lsp/src/lsp/index.ts +59 -0
- package/node_modules/pi-agent-ide-lsp/src/lsp/language-map.ts +57 -0
- package/node_modules/pi-agent-ide-lsp/src/lsp/lsp-compiler.ts +85 -0
- package/node_modules/pi-agent-ide-lsp/src/lsp/manager.ts +485 -0
- package/node_modules/pi-agent-ide-lsp/src/lsp/navigation.ts +85 -0
- package/node_modules/pi-agent-ide-lsp/src/lsp/registry.ts +97 -0
- package/node_modules/pi-agent-ide-lsp/src/lsp/rename.ts +50 -0
- package/node_modules/pi-agent-ide-lsp/src/lsp/symbol-search.ts +149 -0
- package/node_modules/pi-agent-ide-lsp/src/lsp/symbols.ts +151 -0
- package/node_modules/pi-agent-ide-lsp/src/lsp/types.ts +95 -0
- package/node_modules/pi-agent-ide-lsp/src/search-resolver.ts +42 -0
- package/node_modules/pi-agent-image/package.json +18 -0
- package/node_modules/pi-agent-image/src/image-converter.ts +318 -0
- package/node_modules/pi-agent-image/src/index.ts +6 -0
- package/node_modules/pi-agent-pdf/package.json +16 -0
- package/node_modules/pi-agent-pdf/src/index.ts +5 -0
- package/node_modules/pi-agent-pdf/src/pdf-converter.ts +197 -0
- package/node_modules/pi-agent-read/index.ts +1 -0
- package/node_modules/pi-agent-read/package.json +41 -0
- package/node_modules/pi-agent-read/src/api/connect-plugin.ts +67 -0
- package/node_modules/pi-agent-read/src/api/plugin-protocol.ts +67 -0
- package/node_modules/pi-agent-read/src/api/rendering.ts +11 -0
- package/node_modules/pi-agent-read/src/api/tools/read.ts +208 -0
- package/node_modules/pi-agent-read/src/core/extension.ts +40 -0
- package/node_modules/pi-agent-read/src/core/read-core.ts +390 -0
- package/node_modules/pi-agent-read/src/core/tools/read/output-truncation.ts +193 -0
- package/node_modules/pi-agent-read/src/core/tools/read/read-renderer.ts +400 -0
- package/node_modules/pi-agent-read/src/core/tools/read/read-result.ts +187 -0
- package/node_modules/pi-agent-read/src/core/tools/read/temp-resource-store.ts +218 -0
- package/node_modules/pi-agent-read/src/core/tools/tool-read.ts +828 -0
- package/node_modules/pi-agent-resource/package.json +17 -0
- package/node_modules/pi-agent-resource/src/content-conversion/connect-plugin.ts +84 -0
- package/node_modules/pi-agent-resource/src/content-conversion/content-converter.ts +49 -0
- package/node_modules/pi-agent-resource/src/content-conversion/content-description.ts +29 -0
- package/node_modules/pi-agent-resource/src/content-conversion/content-host.ts +73 -0
- package/node_modules/pi-agent-resource/src/content-conversion/content-runner.ts +152 -0
- package/node_modules/pi-agent-resource/src/content-conversion/plugin-protocol.ts +74 -0
- package/node_modules/pi-agent-resource/src/content-conversion/validation.ts +149 -0
- package/node_modules/pi-agent-resource/src/content.ts +17 -0
- package/node_modules/pi-agent-resource/src/index.ts +65 -0
- package/node_modules/pi-agent-resource/src/resolver.ts +23 -0
- package/node_modules/pi-agent-resource/src/resource.ts +41 -0
- package/node_modules/pi-agent-resource/src/validation.ts +139 -0
- package/node_modules/pi-agent-search/index.ts +1 -0
- package/node_modules/pi-agent-search/package.json +35 -0
- package/node_modules/pi-agent-search/src/api/connect-plugin.ts +61 -0
- package/node_modules/pi-agent-search/src/api/plugin-protocol.ts +60 -0
- package/node_modules/pi-agent-search/src/api/search.ts +91 -0
- package/node_modules/pi-agent-search/src/core/extension.ts +126 -0
- package/node_modules/pi-agent-search/src/core/search-core.ts +400 -0
- package/node_modules/pi-agent-search-semantic/index.ts +105 -0
- package/node_modules/pi-agent-search-semantic/package.json +17 -0
- package/node_modules/pi-agent-search-text/index.ts +54 -0
- package/node_modules/pi-agent-search-text/package.json +33 -0
- package/node_modules/pi-agent-search-text/src/file-search.ts +105 -0
- package/node_modules/pi-agent-search-text/src/lucene-query-parser.d.ts +22 -0
- package/node_modules/pi-agent-search-text/src/resolvers.ts +192 -0
- package/node_modules/pi-agent-search-text/src/search-backend.ts +295 -0
- package/node_modules/pi-agent-search-text/src/search-query.ts +139 -0
- package/node_modules/pi-agent-search-text/src/search-renderer.ts +420 -0
- package/node_modules/pi-agent-search-text/src/search-result.ts +193 -0
- package/node_modules/pi-agent-search-text/src/search-session.ts +338 -0
- package/node_modules/pi-agent-search-web/index.ts +74 -0
- package/node_modules/pi-agent-search-web/package.json +19 -0
- package/node_modules/pi-agent-search-web/src/config.ts +499 -0
- package/node_modules/pi-agent-search-web/src/providers/duckduckgo-html.ts +78 -0
- package/node_modules/pi-agent-search-web/src/providers/endpoints.ts +89 -0
- package/node_modules/pi-agent-search-web/src/providers/request.ts +430 -0
- package/node_modules/pi-agent-search-web/src/providers/response.ts +402 -0
- package/node_modules/pi-agent-search-web/src/search-format.ts +29 -0
- package/node_modules/pi-agent-search-web/src/search.ts +434 -0
- package/node_modules/pi-agent-search-web/src/types.ts +153 -0
- package/node_modules/pi-agent-text/package.json +18 -0
- package/node_modules/pi-agent-text/src/index.ts +31 -0
- package/node_modules/pi-agent-text/src/presented-text.ts +24 -0
- package/node_modules/pi-agent-text/src/source-line.ts +49 -0
- package/node_modules/pi-agent-text/src/text-anchor.ts +157 -0
- package/node_modules/pi-agent-text/src/text-converter.ts +109 -0
- package/node_modules/pi-agent-text/src/text-document.ts +149 -0
- package/node_modules/pi-agent-text-anchor-constant/index.ts +30 -0
- package/node_modules/pi-agent-text-anchor-constant/package.json +17 -0
- package/node_modules/pi-agent-text-anchor-constant/src/resolver.ts +47 -0
- package/node_modules/pi-agent-text-anchor-line-hash/index.ts +48 -0
- package/node_modules/pi-agent-text-anchor-line-hash/package.json +24 -0
- package/node_modules/pi-agent-text-anchor-line-hash/src/anchor.ts +110 -0
- package/node_modules/pi-agent-text-anchor-line-hash/src/read-handler.ts +47 -0
- package/node_modules/pi-agent-text-editor/index.ts +19 -0
- package/node_modules/pi-agent-text-editor/package.json +92 -0
- package/node_modules/pi-agent-text-editor/src/api/anchor-inspection.ts +24 -0
- package/node_modules/pi-agent-text-editor/src/api/connect-plugin.ts +67 -0
- package/node_modules/pi-agent-text-editor/src/api/edit-completion.ts +19 -0
- package/node_modules/pi-agent-text-editor/src/api/edit-pipeline.ts +64 -0
- package/node_modules/pi-agent-text-editor/src/api/last-resolved-resource.ts +46 -0
- package/node_modules/pi-agent-text-editor/src/api/mutation-guard.ts +46 -0
- package/node_modules/pi-agent-text-editor/src/api/mutation-preview.ts +30 -0
- package/node_modules/pi-agent-text-editor/src/api/mutation-result.ts +23 -0
- package/node_modules/pi-agent-text-editor/src/api/mutation-tool.ts +180 -0
- package/node_modules/pi-agent-text-editor/src/api/plugin-protocol.ts +217 -0
- package/node_modules/pi-agent-text-editor/src/api/post-edit.ts +147 -0
- package/node_modules/pi-agent-text-editor/src/api/stale-anchor-message.ts +17 -0
- package/node_modules/pi-agent-text-editor/src/api/text-selection-anchor.ts +121 -0
- package/node_modules/pi-agent-text-editor/src/api/tool-call-interceptor.ts +11 -0
- package/node_modules/pi-agent-text-editor/src/api/tool-renderer.ts +39 -0
- package/node_modules/pi-agent-text-editor/src/core/builtin-edit-filter.ts +59 -0
- package/node_modules/pi-agent-text-editor/src/core/extension.ts +150 -0
- package/node_modules/pi-agent-text-editor/src/core/mutation-result/agent-diff.ts +386 -0
- package/node_modules/pi-agent-text-editor/src/core/mutation-result/diff.ts +129 -0
- package/node_modules/pi-agent-text-editor/src/core/mutation-result/file-mutation-agent-result.ts +359 -0
- package/node_modules/pi-agent-text-editor/src/core/mutation-result/file-mutation-result.ts +648 -0
- package/node_modules/pi-agent-text-editor/src/core/mutation-result/inline-hints.ts +117 -0
- package/node_modules/pi-agent-text-editor/src/core/text-anchor-recovery.ts +24 -0
- package/node_modules/pi-agent-text-editor/src/core/text-anchor-registry.ts +381 -0
- package/node_modules/pi-agent-text-editor/src/core/text-change-engine.ts +273 -0
- package/node_modules/pi-agent-text-editor/src/core/text-edit-batch-coordinator.ts +607 -0
- package/node_modules/pi-agent-text-editor/src/core/text-edit-batch-execution.ts +127 -0
- package/node_modules/pi-agent-text-editor/src/core/text-edit-batch-recovery.ts +45 -0
- package/node_modules/pi-agent-text-editor/src/core/text-edit-batch-registrar.ts +432 -0
- package/node_modules/pi-agent-text-editor/src/core/text-edit-batch.ts +115 -0
- package/node_modules/pi-agent-text-editor/src/core/text-editor-core.ts +2348 -0
- package/node_modules/pi-agent-text-editor/src/core/text-mutation-anchor-error.ts +28 -0
- package/node_modules/pi-agent-text-editor/src/core/text-mutation.ts +756 -0
- package/node_modules/pi-agent-text-editor/src/core/tool-call-interceptor/coordinator.ts +1235 -0
- package/node_modules/pi-agent-text-editor/src/core/tool-call-interceptor/partial-json-resolver.ts +43 -0
- package/node_modules/pi-agent-text-editor/src/core/tool-call-interceptor/partial-tool-recovery.ts +308 -0
- package/node_modules/pi-agent-text-editor/src/core/tool-call-interceptor/rendering.ts +4 -0
- package/node_modules/pi-agent-text-editor/src/core/tool-call-interceptor/streaming-provider.ts +467 -0
- package/node_modules/pi-agent-text-editor/src/core/tool-description.ts +16 -0
- package/node_modules/pi-agent-text-editor/src/tools/extension.ts +19 -0
- package/node_modules/pi-agent-text-editor/src/tools/text-selection.ts +106 -0
- package/node_modules/pi-agent-text-editor/src/tools/tool-text-copy.ts +121 -0
- package/node_modules/pi-agent-text-editor/src/tools/tool-text-delete.ts +79 -0
- package/node_modules/pi-agent-text-editor/src/tools/tool-text-insert.ts +56 -0
- package/node_modules/pi-agent-text-editor/src/tools/tool-text-move.ts +142 -0
- package/node_modules/pi-agent-text-editor/src/tools/tool-text-replace.ts +72 -0
- package/node_modules/pi-agent-text-editor/src/tools/tool-text-write.ts +28 -0
- package/node_modules/pi-agent-text-editor-argument-order/index.ts +29 -0
- package/node_modules/pi-agent-text-editor-argument-order/package.json +16 -0
- package/node_modules/pi-agent-text-editor-argument-order/src/argument-order-guard.ts +112 -0
- package/node_modules/pi-agent-text-editor-overwrite/index.ts +391 -0
- package/node_modules/pi-agent-text-editor-overwrite/package.json +16 -0
- package/node_modules/pi-agent-text-editor-renderer/index.ts +22 -0
- package/node_modules/pi-agent-text-editor-renderer/package.json +21 -0
- package/node_modules/pi-agent-text-editor-renderer/src/diff-model.ts +452 -0
- package/node_modules/pi-agent-text-editor-renderer/src/diff-renderer.ts +271 -0
- package/node_modules/pi-agent-text-editor-renderer/src/diff-theme.ts +411 -0
- package/node_modules/pi-agent-text-editor-renderer/src/diff-viewport.ts +38 -0
- package/node_modules/pi-agent-text-editor-renderer/src/frozen-viewport.ts +205 -0
- package/node_modules/pi-agent-text-editor-renderer/src/mutation-panel.ts +229 -0
- package/node_modules/pi-agent-text-editor-renderer/src/mutation-projection.ts +407 -0
- package/node_modules/pi-agent-text-editor-renderer/src/mutation-result.ts +153 -0
- package/node_modules/pi-agent-text-editor-renderer/src/render-resource.ts +8 -0
- package/node_modules/pi-agent-text-editor-renderer/src/renderer.ts +728 -0
- package/node_modules/pi-agent-text-editor-renderer/src/typing-interpolation.ts +219 -0
- package/node_modules/pi-agent-text-editor-stale-anchor/index.ts +375 -0
- package/node_modules/pi-agent-text-editor-stale-anchor/package.json +17 -0
- package/node_modules/pi-agent-tool-call-interception/package.json +17 -0
- package/node_modules/pi-agent-tool-call-interception/src/index.ts +418 -0
- package/node_modules/pi-agent-web/index.ts +37 -0
- package/node_modules/pi-agent-web/package.json +24 -0
- package/node_modules/pi-agent-web/src/resolver.ts +182 -0
- package/node_modules/pi-agent-web-html/index.ts +15 -0
- package/node_modules/pi-agent-web-html/package.json +18 -0
- package/node_modules/pi-agent-web-html/src/extract-readable-html.ts +101 -0
- package/node_modules/pi-agent-web-html/src/html-converter.ts +91 -0
- package/node_modules/pi-agent-web-image/index.ts +14 -0
- package/node_modules/pi-agent-web-image/package.json +17 -0
- package/node_modules/pi-agent-web-pdf/index.ts +14 -0
- package/node_modules/pi-agent-web-pdf/package.json +17 -0
- package/node_modules/pi-agent-web-text/index.ts +14 -0
- package/node_modules/pi-agent-web-text/package.json +17 -0
- package/package.json +176 -0
- package/src/api/code-view.ts +25 -0
- package/src/api/connect-plugin.ts +64 -0
- package/src/api/plugin-protocol.ts +63 -0
- package/src/api/read.ts +5 -0
- package/src/api/resource.ts +2 -0
- package/src/api/search.ts +4 -0
- package/src/api/text-editor.ts +3 -0
- package/src/api/text.ts +2 -0
- package/src/api/toolchain.ts +24 -0
- package/src/code-view/diagnostic-view.ts +82 -0
- package/src/code-view/reference.ts +113 -0
- package/src/code-view/source-view.ts +229 -0
- package/src/composite/builtin-extensions.ts +68 -0
- package/src/composite/config.ts +94 -0
- package/src/composite/selection.ts +66 -0
- package/src/core/extension.ts +48 -0
- package/src/core/ide-core.ts +117 -0
- package/src/extensions/pi-agent-read/extensions/pi-agent-filesystem/index.ts +60 -0
- package/src/extensions/pi-agent-read/extensions/pi-agent-filesystem/package.json +23 -0
- package/src/extensions/pi-agent-read/extensions/pi-agent-filesystem/plugins/pi-agent-filesystem-image/index.ts +14 -0
- package/src/extensions/pi-agent-read/extensions/pi-agent-filesystem/plugins/pi-agent-filesystem-image/package.json +20 -0
- package/src/extensions/pi-agent-read/extensions/pi-agent-filesystem/plugins/pi-agent-filesystem-pdf/index.ts +14 -0
- package/src/extensions/pi-agent-read/extensions/pi-agent-filesystem/plugins/pi-agent-filesystem-pdf/package.json +17 -0
- package/src/extensions/pi-agent-read/extensions/pi-agent-filesystem/plugins/pi-agent-filesystem-text/index.ts +23 -0
- package/src/extensions/pi-agent-read/extensions/pi-agent-filesystem/plugins/pi-agent-filesystem-text/package.json +20 -0
- package/src/extensions/pi-agent-read/extensions/pi-agent-filesystem/src/resolver.ts +175 -0
- package/src/extensions/pi-agent-read/extensions/pi-agent-web/index.ts +37 -0
- package/src/extensions/pi-agent-read/extensions/pi-agent-web/package.json +24 -0
- package/src/extensions/pi-agent-read/extensions/pi-agent-web/plugins/pi-agent-web-html/index.ts +15 -0
- package/src/extensions/pi-agent-read/extensions/pi-agent-web/plugins/pi-agent-web-html/package.json +18 -0
- package/src/extensions/pi-agent-read/extensions/pi-agent-web/plugins/pi-agent-web-html/src/extract-readable-html.ts +101 -0
- package/src/extensions/pi-agent-read/extensions/pi-agent-web/plugins/pi-agent-web-html/src/html-converter.ts +91 -0
- package/src/extensions/pi-agent-read/extensions/pi-agent-web/plugins/pi-agent-web-image/index.ts +14 -0
- package/src/extensions/pi-agent-read/extensions/pi-agent-web/plugins/pi-agent-web-image/package.json +17 -0
- package/src/extensions/pi-agent-read/extensions/pi-agent-web/plugins/pi-agent-web-pdf/index.ts +14 -0
- package/src/extensions/pi-agent-read/extensions/pi-agent-web/plugins/pi-agent-web-pdf/package.json +17 -0
- package/src/extensions/pi-agent-read/extensions/pi-agent-web/plugins/pi-agent-web-text/index.ts +14 -0
- package/src/extensions/pi-agent-read/extensions/pi-agent-web/plugins/pi-agent-web-text/package.json +17 -0
- package/src/extensions/pi-agent-read/extensions/pi-agent-web/src/resolver.ts +182 -0
- package/src/extensions/pi-agent-read/index.ts +1 -0
- package/src/extensions/pi-agent-read/package.json +41 -0
- package/src/extensions/pi-agent-read/src/api/connect-plugin.ts +67 -0
- package/src/extensions/pi-agent-read/src/api/plugin-protocol.ts +67 -0
- package/src/extensions/pi-agent-read/src/api/rendering.ts +11 -0
- package/src/extensions/pi-agent-read/src/api/tools/read.ts +208 -0
- package/src/extensions/pi-agent-read/src/core/extension.ts +40 -0
- package/src/extensions/pi-agent-read/src/core/read-core.ts +390 -0
- package/src/extensions/pi-agent-read/src/core/tools/read/output-truncation.ts +193 -0
- package/src/extensions/pi-agent-read/src/core/tools/read/read-renderer.ts +400 -0
- package/src/extensions/pi-agent-read/src/core/tools/read/read-result.ts +187 -0
- package/src/extensions/pi-agent-read/src/core/tools/read/temp-resource-store.ts +218 -0
- package/src/extensions/pi-agent-read/src/core/tools/tool-read.ts +828 -0
- package/src/extensions/pi-agent-search/index.ts +1 -0
- package/src/extensions/pi-agent-search/package.json +35 -0
- package/src/extensions/pi-agent-search/plugins/pi-agent-search-semantic/index.ts +105 -0
- package/src/extensions/pi-agent-search/plugins/pi-agent-search-semantic/package.json +17 -0
- package/src/extensions/pi-agent-search/plugins/pi-agent-search-text/index.ts +54 -0
- package/src/extensions/pi-agent-search/plugins/pi-agent-search-text/package.json +33 -0
- package/src/extensions/pi-agent-search/plugins/pi-agent-search-text/src/file-search.ts +105 -0
- package/src/extensions/pi-agent-search/plugins/pi-agent-search-text/src/lucene-query-parser.d.ts +22 -0
- package/src/extensions/pi-agent-search/plugins/pi-agent-search-text/src/resolvers.ts +192 -0
- package/src/extensions/pi-agent-search/plugins/pi-agent-search-text/src/search-backend.ts +295 -0
- package/src/extensions/pi-agent-search/plugins/pi-agent-search-text/src/search-query.ts +139 -0
- package/src/extensions/pi-agent-search/plugins/pi-agent-search-text/src/search-renderer.ts +420 -0
- package/src/extensions/pi-agent-search/plugins/pi-agent-search-text/src/search-result.ts +193 -0
- package/src/extensions/pi-agent-search/plugins/pi-agent-search-text/src/search-session.ts +338 -0
- package/src/extensions/pi-agent-search/plugins/pi-agent-search-web/index.ts +74 -0
- package/src/extensions/pi-agent-search/plugins/pi-agent-search-web/package.json +19 -0
- package/src/extensions/pi-agent-search/plugins/pi-agent-search-web/src/config.ts +499 -0
- package/src/extensions/pi-agent-search/plugins/pi-agent-search-web/src/providers/duckduckgo-html.ts +78 -0
- package/src/extensions/pi-agent-search/plugins/pi-agent-search-web/src/providers/endpoints.ts +89 -0
- package/src/extensions/pi-agent-search/plugins/pi-agent-search-web/src/providers/request.ts +430 -0
- package/src/extensions/pi-agent-search/plugins/pi-agent-search-web/src/providers/response.ts +402 -0
- package/src/extensions/pi-agent-search/plugins/pi-agent-search-web/src/search-format.ts +29 -0
- package/src/extensions/pi-agent-search/plugins/pi-agent-search-web/src/search.ts +434 -0
- package/src/extensions/pi-agent-search/plugins/pi-agent-search-web/src/types.ts +153 -0
- package/src/extensions/pi-agent-search/src/api/connect-plugin.ts +61 -0
- package/src/extensions/pi-agent-search/src/api/plugin-protocol.ts +60 -0
- package/src/extensions/pi-agent-search/src/api/search.ts +91 -0
- package/src/extensions/pi-agent-search/src/core/extension.ts +126 -0
- package/src/extensions/pi-agent-search/src/core/search-core.ts +400 -0
- package/src/extensions/pi-agent-text-editor/index.ts +19 -0
- package/src/extensions/pi-agent-text-editor/package.json +92 -0
- package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-anchor-constant/index.ts +30 -0
- package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-anchor-constant/package.json +17 -0
- package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-anchor-constant/src/resolver.ts +47 -0
- package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-anchor-line-hash/index.ts +48 -0
- package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-anchor-line-hash/package.json +24 -0
- package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-anchor-line-hash/src/anchor.ts +110 -0
- package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-anchor-line-hash/src/read-handler.ts +47 -0
- package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-argument-order/index.ts +29 -0
- package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-argument-order/package.json +16 -0
- package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-argument-order/src/argument-order-guard.ts +112 -0
- package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-overwrite/index.ts +391 -0
- package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-overwrite/package.json +16 -0
- package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-renderer/index.ts +22 -0
- package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-renderer/package.json +21 -0
- package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-renderer/src/diff-model.ts +452 -0
- package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-renderer/src/diff-renderer.ts +271 -0
- package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-renderer/src/diff-theme.ts +411 -0
- package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-renderer/src/diff-viewport.ts +38 -0
- package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-renderer/src/frozen-viewport.ts +205 -0
- package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-renderer/src/mutation-panel.ts +229 -0
- package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-renderer/src/mutation-projection.ts +407 -0
- package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-renderer/src/mutation-result.ts +153 -0
- package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-renderer/src/render-resource.ts +8 -0
- package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-renderer/src/renderer.ts +728 -0
- package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-renderer/src/typing-interpolation.ts +219 -0
- package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-stale-anchor/index.ts +375 -0
- package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-stale-anchor/package.json +17 -0
- package/src/extensions/pi-agent-text-editor/src/api/anchor-inspection.ts +24 -0
- package/src/extensions/pi-agent-text-editor/src/api/connect-plugin.ts +67 -0
- package/src/extensions/pi-agent-text-editor/src/api/edit-completion.ts +19 -0
- package/src/extensions/pi-agent-text-editor/src/api/edit-pipeline.ts +64 -0
- package/src/extensions/pi-agent-text-editor/src/api/last-resolved-resource.ts +46 -0
- package/src/extensions/pi-agent-text-editor/src/api/mutation-guard.ts +46 -0
- package/src/extensions/pi-agent-text-editor/src/api/mutation-preview.ts +30 -0
- package/src/extensions/pi-agent-text-editor/src/api/mutation-result.ts +23 -0
- package/src/extensions/pi-agent-text-editor/src/api/mutation-tool.ts +180 -0
- package/src/extensions/pi-agent-text-editor/src/api/plugin-protocol.ts +217 -0
- package/src/extensions/pi-agent-text-editor/src/api/post-edit.ts +147 -0
- package/src/extensions/pi-agent-text-editor/src/api/stale-anchor-message.ts +17 -0
- package/src/extensions/pi-agent-text-editor/src/api/text-selection-anchor.ts +121 -0
- package/src/extensions/pi-agent-text-editor/src/api/tool-call-interceptor.ts +11 -0
- package/src/extensions/pi-agent-text-editor/src/api/tool-renderer.ts +39 -0
- package/src/extensions/pi-agent-text-editor/src/core/builtin-edit-filter.ts +59 -0
- package/src/extensions/pi-agent-text-editor/src/core/extension.ts +150 -0
- package/src/extensions/pi-agent-text-editor/src/core/mutation-result/agent-diff.ts +386 -0
- package/src/extensions/pi-agent-text-editor/src/core/mutation-result/diff.ts +129 -0
- package/src/extensions/pi-agent-text-editor/src/core/mutation-result/file-mutation-agent-result.ts +359 -0
- package/src/extensions/pi-agent-text-editor/src/core/mutation-result/file-mutation-result.ts +648 -0
- package/src/extensions/pi-agent-text-editor/src/core/mutation-result/inline-hints.ts +117 -0
- package/src/extensions/pi-agent-text-editor/src/core/text-anchor-recovery.ts +24 -0
- package/src/extensions/pi-agent-text-editor/src/core/text-anchor-registry.ts +381 -0
- package/src/extensions/pi-agent-text-editor/src/core/text-change-engine.ts +273 -0
- package/src/extensions/pi-agent-text-editor/src/core/text-edit-batch-coordinator.ts +607 -0
- package/src/extensions/pi-agent-text-editor/src/core/text-edit-batch-execution.ts +127 -0
- package/src/extensions/pi-agent-text-editor/src/core/text-edit-batch-recovery.ts +45 -0
- package/src/extensions/pi-agent-text-editor/src/core/text-edit-batch-registrar.ts +432 -0
- package/src/extensions/pi-agent-text-editor/src/core/text-edit-batch.ts +115 -0
- package/src/extensions/pi-agent-text-editor/src/core/text-editor-core.ts +2348 -0
- package/src/extensions/pi-agent-text-editor/src/core/text-mutation-anchor-error.ts +28 -0
- package/src/extensions/pi-agent-text-editor/src/core/text-mutation.ts +756 -0
- package/src/extensions/pi-agent-text-editor/src/core/tool-call-interceptor/coordinator.ts +1235 -0
- package/src/extensions/pi-agent-text-editor/src/core/tool-call-interceptor/partial-json-resolver.ts +43 -0
- package/src/extensions/pi-agent-text-editor/src/core/tool-call-interceptor/partial-tool-recovery.ts +308 -0
- package/src/extensions/pi-agent-text-editor/src/core/tool-call-interceptor/rendering.ts +4 -0
- package/src/extensions/pi-agent-text-editor/src/core/tool-call-interceptor/streaming-provider.ts +467 -0
- package/src/extensions/pi-agent-text-editor/src/core/tool-description.ts +16 -0
- package/src/extensions/pi-agent-text-editor/src/tools/extension.ts +19 -0
- package/src/extensions/pi-agent-text-editor/src/tools/text-selection.ts +106 -0
- package/src/extensions/pi-agent-text-editor/src/tools/tool-text-copy.ts +121 -0
- package/src/extensions/pi-agent-text-editor/src/tools/tool-text-delete.ts +79 -0
- package/src/extensions/pi-agent-text-editor/src/tools/tool-text-insert.ts +56 -0
- package/src/extensions/pi-agent-text-editor/src/tools/tool-text-move.ts +142 -0
- package/src/extensions/pi-agent-text-editor/src/tools/tool-text-replace.ts +72 -0
- package/src/extensions/pi-agent-text-editor/src/tools/tool-text-write.ts +28 -0
- package/src/index.ts +1 -0
- package/src/pi-agent-ide.ts +17 -0
- package/src/plugins/pi-agent-ide-ast/index.ts +1 -0
- package/src/plugins/pi-agent-ide-ast/package.json +39 -0
- package/src/plugins/pi-agent-ide-ast/src/api/scope.ts +1 -0
- package/src/plugins/pi-agent-ide-ast/src/ast/collect-files.ts +24 -0
- package/src/plugins/pi-agent-ide-ast/src/ast/manager.ts +374 -0
- package/src/plugins/pi-agent-ide-ast/src/ast/outline.ts +813 -0
- package/src/plugins/pi-agent-ide-ast/src/ast/read-text.ts +30 -0
- package/src/plugins/pi-agent-ide-ast/src/extension.ts +85 -0
- package/src/plugins/pi-agent-ide-ast/src/outline-resolver.ts +69 -0
- package/src/plugins/pi-agent-ide-ast/src/scope-handler.ts +221 -0
- package/src/plugins/pi-agent-ide-ast/src/scope-resolver.ts +45 -0
- package/src/plugins/pi-agent-ide-ast/src/search-resolver.ts +141 -0
- package/src/plugins/pi-agent-ide-changes/index.ts +68 -0
- package/src/plugins/pi-agent-ide-changes/package.json +29 -0
- package/src/plugins/pi-agent-ide-changes/src/change-anchor.ts +103 -0
- package/src/plugins/pi-agent-ide-changes/src/changes/change-groups.ts +309 -0
- package/src/plugins/pi-agent-ide-changes/src/changes/change-service.ts +223 -0
- package/src/plugins/pi-agent-ide-changes/src/changes/change-types.ts +105 -0
- package/src/plugins/pi-agent-ide-changes/src/changes/git-changes-backend.ts +325 -0
- package/src/plugins/pi-agent-ide-changes/src/current-change-presenter.ts +232 -0
- package/src/plugins/pi-agent-ide-changes/src/index-mutation-queue.ts +11 -0
- package/src/plugins/pi-agent-ide-changes/src/last-text-transaction-store.ts +88 -0
- package/src/plugins/pi-agent-ide-changes/src/tool-index-change.ts +116 -0
- package/src/plugins/pi-agent-ide-changes/src/tool-text-undo.ts +153 -0
- package/src/plugins/pi-agent-ide-formatter/index.ts +33 -0
- package/src/plugins/pi-agent-ide-formatter/package.json +14 -0
- package/src/plugins/pi-agent-ide-formatter/src/formatter.ts +50 -0
- package/src/plugins/pi-agent-ide-formatter/src/registry.ts +30 -0
- package/src/plugins/pi-agent-ide-formatter/src/types.ts +10 -0
- package/src/plugins/pi-agent-ide-lint/index.ts +237 -0
- package/src/plugins/pi-agent-ide-lint/package.json +19 -0
- package/src/plugins/pi-agent-ide-lint/src/command-linter.ts +160 -0
- package/src/plugins/pi-agent-ide-lint/src/command-runner.ts +51 -0
- package/src/plugins/pi-agent-ide-lint/src/registry.ts +30 -0
- package/src/plugins/pi-agent-ide-lint/src/types.ts +10 -0
- package/src/plugins/pi-agent-ide-lsp/index.ts +250 -0
- package/src/plugins/pi-agent-ide-lsp/package.json +23 -0
- package/src/plugins/pi-agent-ide-lsp/src/code-view-resolvers.ts +139 -0
- package/src/plugins/pi-agent-ide-lsp/src/lsp/client.ts +463 -0
- package/src/plugins/pi-agent-ide-lsp/src/lsp/code-views.ts +577 -0
- package/src/plugins/pi-agent-ide-lsp/src/lsp/diagnostics.ts +179 -0
- package/src/plugins/pi-agent-ide-lsp/src/lsp/index.ts +59 -0
- package/src/plugins/pi-agent-ide-lsp/src/lsp/language-map.ts +57 -0
- package/src/plugins/pi-agent-ide-lsp/src/lsp/lsp-compiler.ts +85 -0
- package/src/plugins/pi-agent-ide-lsp/src/lsp/manager.ts +485 -0
- package/src/plugins/pi-agent-ide-lsp/src/lsp/navigation.ts +85 -0
- package/src/plugins/pi-agent-ide-lsp/src/lsp/registry.ts +97 -0
- package/src/plugins/pi-agent-ide-lsp/src/lsp/rename.ts +50 -0
- package/src/plugins/pi-agent-ide-lsp/src/lsp/symbol-search.ts +149 -0
- package/src/plugins/pi-agent-ide-lsp/src/lsp/symbols.ts +151 -0
- package/src/plugins/pi-agent-ide-lsp/src/lsp/types.ts +95 -0
- package/src/plugins/pi-agent-ide-lsp/src/search-resolver.ts +42 -0
- package/src/post-edit/gate.ts +153 -0
- package/src/toolchain/diagnostic.ts +7 -0
- package/src/toolchain/gate.ts +322 -0
- package/src/toolchain/registry.ts +223 -0
- package/src/toolchain/types.ts +164 -0
|
@@ -0,0 +1,756 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
type AgentToolResult,
|
|
5
|
+
defineTool,
|
|
6
|
+
type ExtensionContext,
|
|
7
|
+
type ToolDefinition,
|
|
8
|
+
} from "@earendil-works/pi-coding-agent";
|
|
9
|
+
|
|
10
|
+
import { type FileMutationBatchResult, FileMutationResult } from "#src/api/mutation-result.js";
|
|
11
|
+
import {
|
|
12
|
+
mutationSource,
|
|
13
|
+
type TextMutation,
|
|
14
|
+
type TextMutationContext,
|
|
15
|
+
type TextMutationEdit,
|
|
16
|
+
type TextMutationToolRegistration,
|
|
17
|
+
} from "#src/api/mutation-tool.js";
|
|
18
|
+
import { isTextMutationResultContributionData } from "#src/api/post-edit.js";
|
|
19
|
+
import { formatStaleAnchorMessage, type StaleAnchorMessageDetails } from "#src/api/stale-anchor-message.js";
|
|
20
|
+
import { createUnifiedDiff } from "#src/core/mutation-result/diff.js";
|
|
21
|
+
import { FileMutationAgentResult } from "#src/core/mutation-result/file-mutation-agent-result.js";
|
|
22
|
+
import { readTextAnchorRecovery } from "#src/core/text-anchor-recovery.js";
|
|
23
|
+
import { applyTextChanges, TextChangeDocument } from "#src/core/text-change-engine.js";
|
|
24
|
+
import { executeTextToolWithBatch } from "#src/core/text-edit-batch-registrar.js";
|
|
25
|
+
import {
|
|
26
|
+
contextualizeTextMutationAnchorError,
|
|
27
|
+
TextMutationAnchorResolutionError,
|
|
28
|
+
} from "#src/core/text-mutation-anchor-error.js";
|
|
29
|
+
import {
|
|
30
|
+
type ToolCallInterceptionRenderStore,
|
|
31
|
+
withToolCallInterceptionRendering,
|
|
32
|
+
} from "#src/core/tool-call-interceptor/rendering.js";
|
|
33
|
+
import { appendSchemaFieldOrder } from "#src/core/tool-description.js";
|
|
34
|
+
|
|
35
|
+
import type { TextEditExecutionOutcome } from "#src/api/edit-pipeline.js";
|
|
36
|
+
import type { TextMutationPreviewOutcome, TextMutationPreviewRequest } from "#src/api/mutation-preview.js";
|
|
37
|
+
import type {
|
|
38
|
+
ResolveResourceTextAnchor,
|
|
39
|
+
TextEditorCore,
|
|
40
|
+
TextResourceEditFailure,
|
|
41
|
+
TextResourceEditOutcome,
|
|
42
|
+
TextResourcesEditOutcome,
|
|
43
|
+
} from "#src/core/text-editor-core.js";
|
|
44
|
+
import type { TextAnchor } from "pi-agent-text";
|
|
45
|
+
import type { Static, TSchema } from "typebox";
|
|
46
|
+
|
|
47
|
+
export function createTextTool<TParams extends TSchema>(
|
|
48
|
+
core: TextEditorCore,
|
|
49
|
+
definition: TextMutationToolRegistration<TParams>,
|
|
50
|
+
annotations: ToolCallInterceptionRenderStore,
|
|
51
|
+
pluginPromptGuideline: () => string | undefined,
|
|
52
|
+
getLastResolvedSource: () => string | undefined,
|
|
53
|
+
): ToolDefinition<TParams, FileMutationBatchResult>
|
|
54
|
+
{
|
|
55
|
+
const renderer = core.getToolRenderer(definition.name);
|
|
56
|
+
|
|
57
|
+
const tool = withToolCallInterceptionRendering<TParams, FileMutationBatchResult, unknown>(
|
|
58
|
+
defineTool<TParams, FileMutationBatchResult, unknown>({
|
|
59
|
+
name: definition.name,
|
|
60
|
+
label: definition.name,
|
|
61
|
+
description: appendSchemaFieldOrder(definition.description, definition.parameters),
|
|
62
|
+
parameters: definition.parameters,
|
|
63
|
+
prepareArguments: (args) =>
|
|
64
|
+
prepareGuardedArguments(
|
|
65
|
+
definition.parameters,
|
|
66
|
+
args,
|
|
67
|
+
definition.source.inherited ? definition.source.field : undefined,
|
|
68
|
+
getLastResolvedSource(),
|
|
69
|
+
),
|
|
70
|
+
...(renderer?.renderShell === undefined ? {} : { renderShell: renderer.renderShell }),
|
|
71
|
+
...(renderer?.renderCall === undefined ? {} : { renderCall: renderer.renderCall }),
|
|
72
|
+
...(renderer?.renderResult === undefined ? {} : { renderResult: renderer.renderResult }),
|
|
73
|
+
async execute(toolCallId, params, signal, onUpdate, context)
|
|
74
|
+
{
|
|
75
|
+
const directExecute = () =>
|
|
76
|
+
executeTextMutation(core, definition, params as unknown as Static<TParams>, signal, context);
|
|
77
|
+
return executeTextToolWithBatch(toolCallId, directExecute, signal, onUpdate, context);
|
|
78
|
+
},
|
|
79
|
+
}),
|
|
80
|
+
annotations,
|
|
81
|
+
);
|
|
82
|
+
Object.defineProperty(tool, "promptGuidelines", {
|
|
83
|
+
enumerable: true,
|
|
84
|
+
get(): string[] | undefined
|
|
85
|
+
{
|
|
86
|
+
const guideline = pluginPromptGuideline();
|
|
87
|
+
return guideline === undefined ? undefined : [guideline];
|
|
88
|
+
},
|
|
89
|
+
});
|
|
90
|
+
return tool;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function prepareGuardedArguments<TParams extends TSchema>(
|
|
94
|
+
schema: TParams,
|
|
95
|
+
args: unknown,
|
|
96
|
+
sourceField: string | undefined,
|
|
97
|
+
lastResolvedSource: string | undefined,
|
|
98
|
+
): ReturnType<NonNullable<ToolDefinition<TParams, FileMutationBatchResult>["prepareArguments"]>>
|
|
99
|
+
{
|
|
100
|
+
const prepared = args !== null && typeof args === "object" && !Array.isArray(args)
|
|
101
|
+
? { ...(args as Record<string, unknown>) }
|
|
102
|
+
: {};
|
|
103
|
+
const required = (schema as { required?: unknown; }).required;
|
|
104
|
+
const properties = (schema as { properties?: Record<string, { type?: unknown; }>; }).properties;
|
|
105
|
+
|
|
106
|
+
if (sourceField !== undefined && !(sourceField in prepared) && lastResolvedSource !== undefined)
|
|
107
|
+
{
|
|
108
|
+
prepared[sourceField] = lastResolvedSource;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
if (Array.isArray(required))
|
|
112
|
+
{
|
|
113
|
+
for (const field of required)
|
|
114
|
+
{
|
|
115
|
+
if (typeof field === "string" && !(field in prepared) && properties?.[field]?.type === "string")
|
|
116
|
+
{
|
|
117
|
+
prepared[field] = "";
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
return prepared as ReturnType<NonNullable<ToolDefinition<TParams, FileMutationBatchResult>["prepareArguments"]>>;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
interface ResolvedMutationSources
|
|
126
|
+
{
|
|
127
|
+
readonly fields: ReadonlyMap<string, string>;
|
|
128
|
+
readonly anchors: ReadonlyMap<string, readonly string[]>;
|
|
129
|
+
readonly resources: readonly string[];
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
async function resolveMutationSources(
|
|
133
|
+
core: TextEditorCore,
|
|
134
|
+
definition: TextMutationToolRegistration,
|
|
135
|
+
input: Readonly<Record<string, unknown>>,
|
|
136
|
+
context: { readonly cwd: string; readonly signal?: AbortSignal; },
|
|
137
|
+
): Promise<ResolvedMutationSources>
|
|
138
|
+
{
|
|
139
|
+
const expandedByAnchor = new Map<string, readonly string[]>();
|
|
140
|
+
const expandedBySourceField = new Map<string, readonly string[]>();
|
|
141
|
+
|
|
142
|
+
for (const descriptor of definition.anchors ?? [])
|
|
143
|
+
{
|
|
144
|
+
const value = input[descriptor.field];
|
|
145
|
+
|
|
146
|
+
if (typeof value !== "string" || value.length === 0)
|
|
147
|
+
{
|
|
148
|
+
continue;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
let expanded: readonly string[] | undefined;
|
|
152
|
+
|
|
153
|
+
try
|
|
154
|
+
{
|
|
155
|
+
expanded = await core.resolveTextAnchorResources(value, descriptor.kinds, context);
|
|
156
|
+
}
|
|
157
|
+
catch (error)
|
|
158
|
+
{
|
|
159
|
+
const source = input[descriptor.sourceField];
|
|
160
|
+
throw contextualizeTextMutationAnchorError(
|
|
161
|
+
error,
|
|
162
|
+
definition.name,
|
|
163
|
+
descriptor.field,
|
|
164
|
+
typeof source === "string" ? source : "",
|
|
165
|
+
value,
|
|
166
|
+
);
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
if (expanded === undefined)
|
|
170
|
+
{
|
|
171
|
+
continue;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
const previous = expandedBySourceField.get(descriptor.sourceField);
|
|
175
|
+
|
|
176
|
+
if (previous !== undefined && !sameResources(previous, expanded, context.cwd))
|
|
177
|
+
{
|
|
178
|
+
throw new Error(`Mutation anchors for ${descriptor.sourceField} refer to different resources.`);
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
expandedByAnchor.set(descriptor.field, expanded);
|
|
182
|
+
expandedBySourceField.set(descriptor.sourceField, expanded);
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
const fields = new Map<string, string>();
|
|
186
|
+
const sourceDescriptors = [definition.source, ...(definition.source.targets ?? [])];
|
|
187
|
+
|
|
188
|
+
for (const descriptor of sourceDescriptors)
|
|
189
|
+
{
|
|
190
|
+
const explicit = input[descriptor.field];
|
|
191
|
+
const explicitSource = typeof explicit === "string" && explicit.length > 0 ? explicit : undefined;
|
|
192
|
+
const fallback = "fallbackTo" in descriptor ? fields.get(descriptor.fallbackTo) : undefined;
|
|
193
|
+
const expanded = expandedBySourceField.get(descriptor.field);
|
|
194
|
+
|
|
195
|
+
if (expanded !== undefined)
|
|
196
|
+
{
|
|
197
|
+
if (explicitSource === undefined)
|
|
198
|
+
{
|
|
199
|
+
fields.set(descriptor.field, expanded[0]!);
|
|
200
|
+
}
|
|
201
|
+
else
|
|
202
|
+
{
|
|
203
|
+
if (expanded.length !== 1)
|
|
204
|
+
{
|
|
205
|
+
throw new Error(
|
|
206
|
+
`${descriptor.field} must be omitted when a search anchor selects multiple resources.`,
|
|
207
|
+
);
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
if (!sameResource(explicitSource, expanded[0]!, context.cwd))
|
|
211
|
+
{
|
|
212
|
+
throw new Error(`Anchor does not belong to ${explicitSource}.`);
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
fields.set(descriptor.field, explicitSource);
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
continue;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
const source = explicitSource ?? fallback;
|
|
222
|
+
|
|
223
|
+
if (source === undefined)
|
|
224
|
+
{
|
|
225
|
+
throw new Error(`${descriptor.field} is required`);
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
fields.set(descriptor.field, source);
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
const anchors = new Map<string, readonly string[]>();
|
|
232
|
+
|
|
233
|
+
for (const descriptor of definition.anchors ?? [])
|
|
234
|
+
{
|
|
235
|
+
const expanded = expandedByAnchor.get(descriptor.field);
|
|
236
|
+
const source = fields.get(descriptor.sourceField);
|
|
237
|
+
|
|
238
|
+
if (expanded !== undefined)
|
|
239
|
+
{
|
|
240
|
+
anchors.set(
|
|
241
|
+
descriptor.field,
|
|
242
|
+
source !== undefined && expanded.length === 1 ? [source] : expanded,
|
|
243
|
+
);
|
|
244
|
+
}
|
|
245
|
+
else if (source !== undefined)
|
|
246
|
+
{
|
|
247
|
+
anchors.set(descriptor.field, [source]);
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
return {
|
|
252
|
+
fields,
|
|
253
|
+
anchors,
|
|
254
|
+
resources: [...new Set([...fields.values(), ...[...anchors.values()].flat()])],
|
|
255
|
+
};
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
function createMutationContext(
|
|
259
|
+
definition: TextMutationToolRegistration,
|
|
260
|
+
input: Readonly<Record<string, unknown>>,
|
|
261
|
+
invocation: { readonly cwd: string; readonly signal?: AbortSignal; },
|
|
262
|
+
sources: ResolvedMutationSources,
|
|
263
|
+
texts: ReadonlyMap<string, string>,
|
|
264
|
+
resolveResourceAnchor: ResolveResourceTextAnchor,
|
|
265
|
+
): TextMutationContext
|
|
266
|
+
{
|
|
267
|
+
const documents = new Map([...texts].map(([source, text]) => [source, new TextChangeDocument(text)]));
|
|
268
|
+
const anchorCache = new Map<string, Promise<ReadonlyMap<string, TextAnchor>>>();
|
|
269
|
+
const sourceFor = (field: string): string =>
|
|
270
|
+
{
|
|
271
|
+
const source = sources.fields.get(field);
|
|
272
|
+
|
|
273
|
+
if (source === undefined)
|
|
274
|
+
{
|
|
275
|
+
throw new Error(`Unknown mutation source field ${field}`);
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
return source;
|
|
279
|
+
};
|
|
280
|
+
const documentFor = (source: string): TextChangeDocument =>
|
|
281
|
+
{
|
|
282
|
+
const document = documents.get(source);
|
|
283
|
+
|
|
284
|
+
if (document === undefined)
|
|
285
|
+
{
|
|
286
|
+
throw new Error(`Unknown mutation resource ${source}`);
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
return document;
|
|
290
|
+
};
|
|
291
|
+
const resolveAnchors = (field: string): Promise<ReadonlyMap<string, TextAnchor>> =>
|
|
292
|
+
{
|
|
293
|
+
const cached = anchorCache.get(field);
|
|
294
|
+
|
|
295
|
+
if (cached !== undefined)
|
|
296
|
+
{
|
|
297
|
+
return cached;
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
const pending = (async () =>
|
|
301
|
+
{
|
|
302
|
+
const descriptor = (definition.anchors ?? []).find((anchor) => anchor.field === field);
|
|
303
|
+
const value = input[field];
|
|
304
|
+
const anchorSources = sources.anchors.get(field);
|
|
305
|
+
|
|
306
|
+
if (descriptor === undefined || typeof value !== "string" || anchorSources === undefined)
|
|
307
|
+
{
|
|
308
|
+
throw new Error(`Mutation tool ${definition.name} tried to resolve undeclared anchor ${field}`);
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
return new Map(
|
|
312
|
+
await Promise.all(anchorSources.map(async (source) =>
|
|
313
|
+
{
|
|
314
|
+
try
|
|
315
|
+
{
|
|
316
|
+
return [source, await resolveResourceAnchor(source, value, descriptor.kinds)] as const;
|
|
317
|
+
}
|
|
318
|
+
catch (error)
|
|
319
|
+
{
|
|
320
|
+
throw contextualizeTextMutationAnchorError(error, definition.name, field, source, value);
|
|
321
|
+
}
|
|
322
|
+
})),
|
|
323
|
+
);
|
|
324
|
+
})();
|
|
325
|
+
anchorCache.set(field, pending);
|
|
326
|
+
return pending;
|
|
327
|
+
};
|
|
328
|
+
|
|
329
|
+
return {
|
|
330
|
+
cwd: invocation.cwd,
|
|
331
|
+
...(invocation.signal === undefined ? {} : { signal: invocation.signal }),
|
|
332
|
+
sourceDocument: documentFor(sourceFor(definition.source.field)),
|
|
333
|
+
sourceFor,
|
|
334
|
+
documentFor,
|
|
335
|
+
targetDocument(field): TextChangeDocument
|
|
336
|
+
{
|
|
337
|
+
if (!(definition.source.targets ?? []).some((target) => target.field === field))
|
|
338
|
+
{
|
|
339
|
+
throw new Error(`Mutation tool ${definition.name} tried to access undeclared target ${field}`);
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
return documentFor(sourceFor(field));
|
|
343
|
+
},
|
|
344
|
+
resolveAnchors,
|
|
345
|
+
async resolveAnchor(field): Promise<TextAnchor>
|
|
346
|
+
{
|
|
347
|
+
const resolved = await resolveAnchors(field);
|
|
348
|
+
|
|
349
|
+
if (resolved.size !== 1)
|
|
350
|
+
{
|
|
351
|
+
throw new Error(
|
|
352
|
+
`Anchor ${field} selects multiple resources; use an operation that supports a search set.`,
|
|
353
|
+
);
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
return resolved.values().next().value!;
|
|
357
|
+
},
|
|
358
|
+
};
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
function sameResources(left: readonly string[], right: readonly string[], cwd: string): boolean
|
|
362
|
+
{
|
|
363
|
+
const leftKeys = left.map((source) => resourceIdentity(source, cwd)).sort();
|
|
364
|
+
const rightKeys = right.map((source) => resourceIdentity(source, cwd)).sort();
|
|
365
|
+
return leftKeys.length === rightKeys.length && leftKeys.every((key, index) => key === rightKeys[index]);
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
function sameResource(left: string, right: string, cwd: string): boolean
|
|
369
|
+
{
|
|
370
|
+
return resourceIdentity(left, cwd) === resourceIdentity(right, cwd);
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
function resourceIdentity(source: string, cwd: string): string
|
|
374
|
+
{
|
|
375
|
+
if (/^[a-z][a-z\d+.-]*:\/\//iu.test(source))
|
|
376
|
+
{
|
|
377
|
+
return source;
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
const fileSource = source.startsWith("@") ? source.slice(1) : source;
|
|
381
|
+
return path.resolve(cwd, fileSource);
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
export async function previewTextMutation(
|
|
385
|
+
core: TextEditorCore,
|
|
386
|
+
request: TextMutationPreviewRequest,
|
|
387
|
+
): Promise<TextMutationPreviewOutcome>
|
|
388
|
+
{
|
|
389
|
+
const definition = core.getMutationTools().find((candidate) => candidate.name === request.tool);
|
|
390
|
+
|
|
391
|
+
if (definition === undefined)
|
|
392
|
+
{
|
|
393
|
+
return { kind: "failed", reason: `Unknown mutation tool ${request.tool}` };
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
try
|
|
397
|
+
{
|
|
398
|
+
const sources = await resolveMutationSources(core, definition, request.input, request);
|
|
399
|
+
return await core.previewTexts(
|
|
400
|
+
sources.resources.map((source) => ({
|
|
401
|
+
source,
|
|
402
|
+
read: true,
|
|
403
|
+
...(definition.name === "write" ? { allowReadFailure: true } : {}),
|
|
404
|
+
})),
|
|
405
|
+
{ cwd: request.cwd, ...(request.signal === undefined ? {} : { signal: request.signal }) },
|
|
406
|
+
async (texts, resolveAnchor) =>
|
|
407
|
+
{
|
|
408
|
+
const mutation = await definition.mutate(
|
|
409
|
+
createMutationContext(definition, request.input, request, sources, texts, resolveAnchor),
|
|
410
|
+
request.input,
|
|
411
|
+
);
|
|
412
|
+
|
|
413
|
+
return {
|
|
414
|
+
changes: new Map([...mutation.edits].map(([source, edit]) => [source, edit.changes])),
|
|
415
|
+
result: mutation,
|
|
416
|
+
};
|
|
417
|
+
},
|
|
418
|
+
);
|
|
419
|
+
}
|
|
420
|
+
catch (error)
|
|
421
|
+
{
|
|
422
|
+
return { kind: "failed", reason: error instanceof Error ? error.message : String(error) };
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
export async function executeTextMutation<TParams extends TSchema>(
|
|
427
|
+
core: TextEditorCore,
|
|
428
|
+
definition: TextMutationToolRegistration<TParams>,
|
|
429
|
+
params: Static<TParams>,
|
|
430
|
+
signal: AbortSignal | undefined,
|
|
431
|
+
context: ExtensionContext,
|
|
432
|
+
): Promise<AgentToolResult<FileMutationBatchResult>>
|
|
433
|
+
{
|
|
434
|
+
const source = mutationSource(definition, params);
|
|
435
|
+
const pipeline = await core.executeEdit(
|
|
436
|
+
definition.name,
|
|
437
|
+
{
|
|
438
|
+
cwd: context.cwd,
|
|
439
|
+
input: params,
|
|
440
|
+
...(signal === undefined ? {} : { signal }),
|
|
441
|
+
},
|
|
442
|
+
async (state) =>
|
|
443
|
+
{
|
|
444
|
+
try
|
|
445
|
+
{
|
|
446
|
+
const sources = await resolveMutationSources(core, definition, state.input, state);
|
|
447
|
+
const outcome = await core.editTexts(
|
|
448
|
+
sources.resources.map((resourceSource) => ({
|
|
449
|
+
source: resourceSource,
|
|
450
|
+
read: true,
|
|
451
|
+
...(definition.name === "write" ? { allowReadFailure: true } : {}),
|
|
452
|
+
})),
|
|
453
|
+
{
|
|
454
|
+
cwd: state.cwd,
|
|
455
|
+
intent: definition.intent ?? "edit",
|
|
456
|
+
...(state.signal === undefined ? {} : { signal: state.signal }),
|
|
457
|
+
},
|
|
458
|
+
async (texts, resolveAnchor) =>
|
|
459
|
+
{
|
|
460
|
+
const mutation = await definition.mutate(
|
|
461
|
+
createMutationContext(definition, state.input, state, sources, texts, resolveAnchor),
|
|
462
|
+
state.input,
|
|
463
|
+
);
|
|
464
|
+
return {
|
|
465
|
+
changes: new Map(
|
|
466
|
+
[...mutation.edits].map(([editSource, edit]) => [editSource, edit.changes]),
|
|
467
|
+
),
|
|
468
|
+
result: mutation,
|
|
469
|
+
};
|
|
470
|
+
},
|
|
471
|
+
);
|
|
472
|
+
return await completeTextMutation(outcome, mutationSource(definition, state.input));
|
|
473
|
+
}
|
|
474
|
+
catch (error)
|
|
475
|
+
{
|
|
476
|
+
return failedResourceEdit(
|
|
477
|
+
mutationSource(definition, state.input),
|
|
478
|
+
"INVALID_REQUEST",
|
|
479
|
+
errorMessage(error),
|
|
480
|
+
error,
|
|
481
|
+
);
|
|
482
|
+
}
|
|
483
|
+
},
|
|
484
|
+
);
|
|
485
|
+
|
|
486
|
+
return buildToolResult(core, pipeline, source, context);
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
export function mutationSources(
|
|
490
|
+
definition: TextMutationToolRegistration,
|
|
491
|
+
params: Readonly<Record<string, unknown>>,
|
|
492
|
+
): ReadonlyMap<string, string>
|
|
493
|
+
{
|
|
494
|
+
const sources = new Map<string, string>();
|
|
495
|
+
const primary = params[definition.source.field];
|
|
496
|
+
|
|
497
|
+
if (typeof primary !== "string" || primary.length === 0)
|
|
498
|
+
{
|
|
499
|
+
throw new Error(`${definition.source.field} is required`);
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
sources.set(definition.source.field, primary);
|
|
503
|
+
|
|
504
|
+
for (const target of definition.source.targets ?? [])
|
|
505
|
+
{
|
|
506
|
+
const explicit = params[target.field];
|
|
507
|
+
const fallback = sources.get(target.fallbackTo);
|
|
508
|
+
const source = typeof explicit === "string" && explicit.length > 0 ? explicit : fallback;
|
|
509
|
+
|
|
510
|
+
if (source === undefined)
|
|
511
|
+
{
|
|
512
|
+
throw new Error(`${target.field} is required`);
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
sources.set(target.field, source);
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
return sources;
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
type CompletedTextResource = Extract<TextResourceEditOutcome<unknown>, { readonly kind: "completed"; }>;
|
|
522
|
+
|
|
523
|
+
export function buildSuccessfulTextMutationResult(
|
|
524
|
+
resource: CompletedTextResource,
|
|
525
|
+
resultSource: string,
|
|
526
|
+
edit: TextMutationEdit,
|
|
527
|
+
diffAfterContent = resource.after.content,
|
|
528
|
+
): FileMutationResult
|
|
529
|
+
{
|
|
530
|
+
const before = resource.before.content;
|
|
531
|
+
const finalAfter = resource.after.content;
|
|
532
|
+
const applied = applyTextChanges(before, edit.changes);
|
|
533
|
+
const unified = createUnifiedDiff(resultSource, before, diffAfterContent);
|
|
534
|
+
const mutationResultData = resource.postEditContributions
|
|
535
|
+
.map((contribution) => contribution.data)
|
|
536
|
+
.find(isTextMutationResultContributionData);
|
|
537
|
+
const rawChanges = applied.changes.map((change, editIndex) => ({
|
|
538
|
+
editIndex,
|
|
539
|
+
fromA: change.fromBefore,
|
|
540
|
+
toA: change.toBefore,
|
|
541
|
+
fromB: change.fromAfter,
|
|
542
|
+
toB: change.toAfter,
|
|
543
|
+
removedText: change.removedText,
|
|
544
|
+
insertedText: change.insertedText,
|
|
545
|
+
}));
|
|
546
|
+
|
|
547
|
+
return new FileMutationResult({
|
|
548
|
+
...mutationResultData,
|
|
549
|
+
ok: true,
|
|
550
|
+
path: resultSource,
|
|
551
|
+
diffs: [unified.diff],
|
|
552
|
+
files: [{ path: resultSource, action: edit.action }],
|
|
553
|
+
editCount: 1,
|
|
554
|
+
addedLines: unified.stats.added,
|
|
555
|
+
removedLines: unified.stats.removed,
|
|
556
|
+
beforeContentMap: { [resultSource]: before },
|
|
557
|
+
afterContent: finalAfter,
|
|
558
|
+
rawChanges,
|
|
559
|
+
afterDocument: resource.after,
|
|
560
|
+
});
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
async function completeTextMutation(
|
|
564
|
+
outcome: TextResourcesEditOutcome<TextMutation>,
|
|
565
|
+
source: string,
|
|
566
|
+
): Promise<TextResourcesEditOutcome<TextMutation>>
|
|
567
|
+
{
|
|
568
|
+
if (outcome.kind === "failed" || outcome.result.afterWrite === undefined)
|
|
569
|
+
{
|
|
570
|
+
return outcome;
|
|
571
|
+
}
|
|
572
|
+
|
|
573
|
+
try
|
|
574
|
+
{
|
|
575
|
+
await outcome.result.afterWrite();
|
|
576
|
+
return outcome;
|
|
577
|
+
}
|
|
578
|
+
catch (error)
|
|
579
|
+
{
|
|
580
|
+
return {
|
|
581
|
+
kind: "failed",
|
|
582
|
+
failure: {
|
|
583
|
+
code: "POST_WRITE_FAILED",
|
|
584
|
+
source,
|
|
585
|
+
message: `Post-write action failed for ${source}`,
|
|
586
|
+
cause: error,
|
|
587
|
+
},
|
|
588
|
+
completed: outcome.resources.map((resource) => resource.source),
|
|
589
|
+
};
|
|
590
|
+
}
|
|
591
|
+
}
|
|
592
|
+
|
|
593
|
+
async function buildToolResult(
|
|
594
|
+
core: TextEditorCore,
|
|
595
|
+
pipeline: TextEditExecutionOutcome,
|
|
596
|
+
source: string,
|
|
597
|
+
context: ExtensionContext,
|
|
598
|
+
): Promise<AgentToolResult<FileMutationBatchResult>>
|
|
599
|
+
{
|
|
600
|
+
if (pipeline.kind === "failed")
|
|
601
|
+
{
|
|
602
|
+
return failureToolResult(source, pipeline.failure.code, pipeline.failure.message, "not-applied");
|
|
603
|
+
}
|
|
604
|
+
|
|
605
|
+
const outcome = pipeline.state.result;
|
|
606
|
+
|
|
607
|
+
if (!isResourcesEditOutcome(outcome))
|
|
608
|
+
{
|
|
609
|
+
return failureToolResult(source, "INVALID_RESULT", "Text editor returned an invalid edit result.", "unknown");
|
|
610
|
+
}
|
|
611
|
+
|
|
612
|
+
if (outcome.kind === "failed")
|
|
613
|
+
{
|
|
614
|
+
const recovery = await anchorFailureToolResult(core, outcome.failure, context);
|
|
615
|
+
|
|
616
|
+
if (recovery !== undefined)
|
|
617
|
+
{
|
|
618
|
+
return recovery;
|
|
619
|
+
}
|
|
620
|
+
|
|
621
|
+
const completed = outcome.completed.length === 0 ? "" : ` Completed writes: ${outcome.completed.join(", ")}.`;
|
|
622
|
+
const effect = outcome.completed.length === 0 ? "not-applied" : "applied";
|
|
623
|
+
const message = `${outcome.failure.message.replace(/[.!?]+$/u, "")}.${completed}`;
|
|
624
|
+
return failureToolResult(source, outcome.failure.code, message, effect);
|
|
625
|
+
}
|
|
626
|
+
|
|
627
|
+
const mutation = outcome.result as TextMutation;
|
|
628
|
+
const results = outcome.resources.flatMap((resource) =>
|
|
629
|
+
{
|
|
630
|
+
const resultSource = resource.source;
|
|
631
|
+
const edit = mutation.edits.get(resultSource);
|
|
632
|
+
return edit === undefined ? [] : [buildSuccessfulTextMutationResult(resource, resultSource, edit)];
|
|
633
|
+
});
|
|
634
|
+
|
|
635
|
+
return {
|
|
636
|
+
content: [new FileMutationAgentResult(results).toTextContent()],
|
|
637
|
+
details: { results },
|
|
638
|
+
};
|
|
639
|
+
}
|
|
640
|
+
|
|
641
|
+
async function anchorFailureToolResult(
|
|
642
|
+
core: TextEditorCore,
|
|
643
|
+
failure: TextResourceEditFailure,
|
|
644
|
+
context: ExtensionContext,
|
|
645
|
+
): Promise<AgentToolResult<FileMutationBatchResult> | undefined>
|
|
646
|
+
{
|
|
647
|
+
if (!(failure.cause instanceof TextMutationAnchorResolutionError))
|
|
648
|
+
{
|
|
649
|
+
return undefined;
|
|
650
|
+
}
|
|
651
|
+
|
|
652
|
+
const contextual = failure.cause;
|
|
653
|
+
const resolution = contextual.resolution;
|
|
654
|
+
const range = resolution.rejection?.contextRange;
|
|
655
|
+
|
|
656
|
+
if (range === undefined)
|
|
657
|
+
{
|
|
658
|
+
return undefined;
|
|
659
|
+
}
|
|
660
|
+
|
|
661
|
+
const read = await readTextAnchorRecovery(core, { path: contextual.source, ...range }, {
|
|
662
|
+
cwd: context.cwd,
|
|
663
|
+
...(context.signal === undefined ? {} : { signal: context.signal }),
|
|
664
|
+
});
|
|
665
|
+
|
|
666
|
+
if (read === undefined || read.isError === true)
|
|
667
|
+
{
|
|
668
|
+
return undefined;
|
|
669
|
+
}
|
|
670
|
+
|
|
671
|
+
const result = new FileMutationResult({
|
|
672
|
+
ok: false,
|
|
673
|
+
path: contextual.source,
|
|
674
|
+
errors: [{ path: contextual.source, code: failure.code, reason: failure.message }],
|
|
675
|
+
});
|
|
676
|
+
const recoveryContext = read.content
|
|
677
|
+
.filter((block) => block.type === "text")
|
|
678
|
+
.map((block) => block.text)
|
|
679
|
+
.join("\n");
|
|
680
|
+
const details: StaleAnchorMessageDetails = {
|
|
681
|
+
guardId: "stale-anchor",
|
|
682
|
+
effect: "not-applied",
|
|
683
|
+
toolName: contextual.toolName,
|
|
684
|
+
field: contextual.field,
|
|
685
|
+
path: contextual.source,
|
|
686
|
+
anchor: contextual.anchor,
|
|
687
|
+
...(recoveryContext.length === 0 ? {} : { context: recoveryContext }),
|
|
688
|
+
};
|
|
689
|
+
|
|
690
|
+
return {
|
|
691
|
+
content: [{ type: "text", text: formatStaleAnchorMessage(details, resolution.message) }],
|
|
692
|
+
details: {
|
|
693
|
+
results: [result],
|
|
694
|
+
effect: "not-applied",
|
|
695
|
+
anchorRecovery: true,
|
|
696
|
+
} as FileMutationBatchResult,
|
|
697
|
+
};
|
|
698
|
+
}
|
|
699
|
+
|
|
700
|
+
export async function buildFailedTextMutationResult(
|
|
701
|
+
core: TextEditorCore,
|
|
702
|
+
failure: TextResourceEditFailure,
|
|
703
|
+
context: ExtensionContext,
|
|
704
|
+
effect: "applied" | "not-applied" | "unknown" = "not-applied",
|
|
705
|
+
): Promise<AgentToolResult<FileMutationBatchResult>>
|
|
706
|
+
{
|
|
707
|
+
return effect === "not-applied"
|
|
708
|
+
? await anchorFailureToolResult(core, failure, context)
|
|
709
|
+
?? failureToolResult(failure.source, failure.code, failure.message, effect)
|
|
710
|
+
: failureToolResult(failure.source, failure.code, failure.message, effect);
|
|
711
|
+
}
|
|
712
|
+
|
|
713
|
+
function failureToolResult(
|
|
714
|
+
source: string,
|
|
715
|
+
code: string,
|
|
716
|
+
reason: string,
|
|
717
|
+
effect: "not-applied" | "applied" | "unknown",
|
|
718
|
+
): { content: [{ type: "text"; text: string; }]; details: FileMutationBatchResult; }
|
|
719
|
+
{
|
|
720
|
+
const result = new FileMutationResult({
|
|
721
|
+
ok: false,
|
|
722
|
+
path: source,
|
|
723
|
+
errors: [{ path: source, code, reason }],
|
|
724
|
+
});
|
|
725
|
+
|
|
726
|
+
return {
|
|
727
|
+
content: [new FileMutationAgentResult(result).toTextContent()],
|
|
728
|
+
details: { results: [result], effect } as FileMutationBatchResult,
|
|
729
|
+
};
|
|
730
|
+
}
|
|
731
|
+
|
|
732
|
+
function failedResourceEdit(
|
|
733
|
+
source: string,
|
|
734
|
+
code: TextResourceEditFailure["code"],
|
|
735
|
+
message: string,
|
|
736
|
+
cause?: unknown,
|
|
737
|
+
): TextResourcesEditOutcome<never>
|
|
738
|
+
{
|
|
739
|
+
return {
|
|
740
|
+
kind: "failed",
|
|
741
|
+
failure: { code, source, message, ...(cause === undefined ? {} : { cause }) },
|
|
742
|
+
completed: [],
|
|
743
|
+
};
|
|
744
|
+
}
|
|
745
|
+
|
|
746
|
+
function isResourcesEditOutcome(value: unknown): value is TextResourcesEditOutcome<unknown>
|
|
747
|
+
{
|
|
748
|
+
return typeof value === "object"
|
|
749
|
+
&& value !== null
|
|
750
|
+
&& ((value as { kind?: unknown; }).kind === "completed" || (value as { kind?: unknown; }).kind === "failed");
|
|
751
|
+
}
|
|
752
|
+
|
|
753
|
+
function errorMessage(error: unknown): string
|
|
754
|
+
{
|
|
755
|
+
return error instanceof Error ? error.message : String(error);
|
|
756
|
+
}
|