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,2348 @@
|
|
|
1
|
+
import {
|
|
2
|
+
isAgentContent,
|
|
3
|
+
isResourceResolutionAttempt,
|
|
4
|
+
type Resource,
|
|
5
|
+
type ResourceResolver,
|
|
6
|
+
type ResourceResolverContext,
|
|
7
|
+
type ResourceWrite,
|
|
8
|
+
} from "pi-agent-resource";
|
|
9
|
+
import {
|
|
10
|
+
createTextDocument,
|
|
11
|
+
isTextPresenterRegistration,
|
|
12
|
+
type TextAnchor,
|
|
13
|
+
type TextAnchorResolverContext,
|
|
14
|
+
type TextDocument,
|
|
15
|
+
type TextPresentationContext,
|
|
16
|
+
type TextPresenterRegistration,
|
|
17
|
+
} from "pi-agent-text";
|
|
18
|
+
|
|
19
|
+
import {
|
|
20
|
+
isTextEditHandlerRegistration,
|
|
21
|
+
type TextEditExecutionOutcome,
|
|
22
|
+
type TextEditHandlerRegistration,
|
|
23
|
+
type TextEditorToolPluginApi,
|
|
24
|
+
type TextEditPipelineFailure,
|
|
25
|
+
type TextEditPipelineStage,
|
|
26
|
+
type TextEditState,
|
|
27
|
+
type TextPreEditState,
|
|
28
|
+
} from "#src/api/edit-pipeline.js";
|
|
29
|
+
import {
|
|
30
|
+
type AnyTextMutationToolRegistration,
|
|
31
|
+
assertTextMutationToolRegistration,
|
|
32
|
+
type TextMutationToolListener,
|
|
33
|
+
} from "#src/api/mutation-tool.js";
|
|
34
|
+
import {
|
|
35
|
+
isResourceResolverRegistration,
|
|
36
|
+
isTextAnchorResolverRegistration,
|
|
37
|
+
isTextEditorToolId,
|
|
38
|
+
type PromptDescriptionSource,
|
|
39
|
+
type ResourceResolverRegistration,
|
|
40
|
+
TEXT_EDITOR_API_VERSION,
|
|
41
|
+
TEXT_EDITOR_PROTOCOL,
|
|
42
|
+
type TextAnchorResolverRegistration,
|
|
43
|
+
type TextAnchorResourceResolverContext,
|
|
44
|
+
type TextEditorPlugin,
|
|
45
|
+
type TextEditorPluginApi,
|
|
46
|
+
type TextEditorToolId,
|
|
47
|
+
} from "#src/api/plugin-protocol.js";
|
|
48
|
+
import {
|
|
49
|
+
isTextPostEditHandlerRegistration,
|
|
50
|
+
type TextPostEditContribution,
|
|
51
|
+
type TextPostEditHandlerRegistration,
|
|
52
|
+
type TextPostEditTransaction,
|
|
53
|
+
} from "#src/api/post-edit.js";
|
|
54
|
+
import {
|
|
55
|
+
isTextEditorToolRendererRegistration,
|
|
56
|
+
type TextEditorToolRendererRegistration,
|
|
57
|
+
} from "#src/api/tool-renderer.js";
|
|
58
|
+
import { TextAnchorRegistry, type TextAnchorRegistrySnapshot } from "#src/core/text-anchor-registry.js";
|
|
59
|
+
import { applyTextChanges, type TextChange, type TextChangeResult } from "#src/core/text-change-engine.js";
|
|
60
|
+
import { previewTextMutation } from "#src/core/text-mutation.js";
|
|
61
|
+
|
|
62
|
+
import type { TextAnchorInspectionOutcome, TextAnchorInspectionRequest } from "#src/api/anchor-inspection.js";
|
|
63
|
+
import type { TextEditCompletion, TextEditCompletionListener, TextEditIntent } from "#src/api/edit-completion.js";
|
|
64
|
+
import type {
|
|
65
|
+
TextMutationGuardContext,
|
|
66
|
+
TextMutationGuardRegistration,
|
|
67
|
+
TextMutationPlan,
|
|
68
|
+
} from "#src/api/mutation-guard.js";
|
|
69
|
+
import type { TextMutationPreviewOutcome, TextMutationPreviewRequest } from "#src/api/mutation-preview.js";
|
|
70
|
+
|
|
71
|
+
type PluginStatus = "active" | "pending";
|
|
72
|
+
|
|
73
|
+
interface PluginLifecycle
|
|
74
|
+
{
|
|
75
|
+
status: PluginStatus;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
interface RegisteredPlugin
|
|
79
|
+
{
|
|
80
|
+
readonly lifecycle: PluginLifecycle;
|
|
81
|
+
readonly plugin: TextEditorPlugin;
|
|
82
|
+
readonly ready: Promise<void>;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
interface RegisteredHandler
|
|
86
|
+
{
|
|
87
|
+
readonly pluginId: string;
|
|
88
|
+
readonly registration: TextEditHandlerRegistration;
|
|
89
|
+
readonly tool: TextEditorToolId;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
interface RegisteredResolver
|
|
93
|
+
{
|
|
94
|
+
readonly resolver: ResourceResolver;
|
|
95
|
+
readonly priority: number;
|
|
96
|
+
readonly order: number;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
interface ResolverContribution
|
|
100
|
+
{
|
|
101
|
+
readonly pluginId: string;
|
|
102
|
+
readonly registration: ResourceResolverRegistration;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
interface TextAnchorResolverContribution
|
|
106
|
+
{
|
|
107
|
+
readonly pluginId: string;
|
|
108
|
+
readonly registration: TextAnchorResolverRegistration;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
interface TextPresenterContribution
|
|
112
|
+
{
|
|
113
|
+
readonly pluginId: string;
|
|
114
|
+
readonly registration: TextPresenterRegistration;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
interface RegisteredPresenter extends TextPresenterContribution
|
|
118
|
+
{
|
|
119
|
+
readonly order: number;
|
|
120
|
+
}
|
|
121
|
+
interface PromptContribution
|
|
122
|
+
{
|
|
123
|
+
readonly description: string;
|
|
124
|
+
readonly pluginId: string;
|
|
125
|
+
readonly tool: TextEditorToolId;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
interface WritablePromptContribution
|
|
129
|
+
{
|
|
130
|
+
readonly description: PromptDescriptionSource;
|
|
131
|
+
readonly pluginId: string;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
interface PluginContributionDraft
|
|
135
|
+
{
|
|
136
|
+
readonly resolvers: ResolverContribution[];
|
|
137
|
+
readonly anchorResolvers: TextAnchorResolverContribution[];
|
|
138
|
+
readonly presenters?: TextPresenterContribution[];
|
|
139
|
+
readonly handlers: RegisteredHandler[];
|
|
140
|
+
readonly promptContributions: PromptContribution[];
|
|
141
|
+
readonly writablePromptContributions: WritablePromptContribution[];
|
|
142
|
+
readonly tools: TextEditorToolId[];
|
|
143
|
+
readonly mutationTools?: AnyTextMutationToolRegistration[];
|
|
144
|
+
readonly mutationToolListeners?: TextMutationToolListener[];
|
|
145
|
+
readonly editCompletionListeners?: TextEditCompletionListener[];
|
|
146
|
+
readonly mutationGuards?: TextMutationGuardRegistration[];
|
|
147
|
+
readonly toolRenderers?: TextEditorToolRendererRegistration[];
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
interface PluginContributionController
|
|
151
|
+
{
|
|
152
|
+
readonly api: TextEditorPluginApi;
|
|
153
|
+
close(): void;
|
|
154
|
+
commit(): void;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
interface StageRunCompleted<State>
|
|
158
|
+
{
|
|
159
|
+
readonly kind: "completed";
|
|
160
|
+
readonly state: State;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
type StageRunOutcome<State> = StageRunCompleted<State> | {
|
|
164
|
+
readonly kind: "failed";
|
|
165
|
+
readonly failure: TextEditPipelineFailure;
|
|
166
|
+
};
|
|
167
|
+
|
|
168
|
+
export interface TextMutationResult<Result>
|
|
169
|
+
{
|
|
170
|
+
readonly text: string;
|
|
171
|
+
readonly result: Result;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
export interface TextResourceEditFailure
|
|
175
|
+
{
|
|
176
|
+
readonly code:
|
|
177
|
+
| "INVALID_REQUEST"
|
|
178
|
+
| "INVALID_RESOLVER_RESULT"
|
|
179
|
+
| "INVALID_RESOURCE_CONTENT"
|
|
180
|
+
| "INVALID_WRITE_CONTENT"
|
|
181
|
+
| "PLUGIN_FAILED"
|
|
182
|
+
| "MUTATION_REJECTED"
|
|
183
|
+
| "NO_RESOLVER"
|
|
184
|
+
| "POST_WRITE_FAILED"
|
|
185
|
+
| "READ_FAILED"
|
|
186
|
+
| "RESOLVE_FAILED"
|
|
187
|
+
| "UNSUPPORTED_CAPABILITY"
|
|
188
|
+
| "UNSUPPORTED_CONTENT"
|
|
189
|
+
| "WRITE_FAILED";
|
|
190
|
+
readonly source: string;
|
|
191
|
+
readonly resolverId?: string;
|
|
192
|
+
readonly message: string;
|
|
193
|
+
readonly cause?: unknown;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
export type TextResourceEditOutcome<Result> =
|
|
197
|
+
| {
|
|
198
|
+
readonly kind: "completed";
|
|
199
|
+
readonly source: string;
|
|
200
|
+
readonly resolvedBy: string;
|
|
201
|
+
readonly before: TextDocument;
|
|
202
|
+
readonly after: TextDocument;
|
|
203
|
+
readonly result: Result;
|
|
204
|
+
readonly postEditContributions: readonly TextPostEditContribution[];
|
|
205
|
+
}
|
|
206
|
+
| { readonly kind: "failed"; readonly failure: TextResourceEditFailure; };
|
|
207
|
+
|
|
208
|
+
export interface TextResourceEditRequest
|
|
209
|
+
{
|
|
210
|
+
readonly source: string;
|
|
211
|
+
readonly read: boolean;
|
|
212
|
+
readonly allowReadFailure?: boolean;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
export interface TextResourcesMutationResult<Result>
|
|
216
|
+
{
|
|
217
|
+
readonly changes: ReadonlyMap<string, readonly TextChange[]>;
|
|
218
|
+
readonly result: Result;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
export type TextResourcesEditOutcome<Result> =
|
|
222
|
+
| {
|
|
223
|
+
readonly kind: "completed";
|
|
224
|
+
readonly resources: readonly Exclude<TextResourceEditOutcome<unknown>, { readonly kind: "failed"; }>[];
|
|
225
|
+
readonly result: Result;
|
|
226
|
+
}
|
|
227
|
+
| { readonly kind: "failed"; readonly failure: TextResourceEditFailure; readonly completed: readonly string[]; };
|
|
228
|
+
|
|
229
|
+
export type ResolveTextAnchor = (value: string, kinds?: readonly string[]) => Promise<TextAnchor>;
|
|
230
|
+
|
|
231
|
+
export type ResolveResourceTextAnchor = (
|
|
232
|
+
source: string,
|
|
233
|
+
value: string,
|
|
234
|
+
kinds?: readonly string[],
|
|
235
|
+
) => Promise<TextAnchor>;
|
|
236
|
+
|
|
237
|
+
export interface TextResourcesEditContext extends ResourceResolverContext, TextMutationGuardContext
|
|
238
|
+
{}
|
|
239
|
+
|
|
240
|
+
export interface TextEditorCore
|
|
241
|
+
{
|
|
242
|
+
inspectTextAnchors(request: TextAnchorInspectionRequest): Promise<TextAnchorInspectionOutcome>;
|
|
243
|
+
addAnchorResolver(registration: TextAnchorResolverRegistration): void;
|
|
244
|
+
resolveTextAnchorResources(
|
|
245
|
+
value: string,
|
|
246
|
+
kinds: readonly string[],
|
|
247
|
+
context: TextAnchorResourceResolverContext,
|
|
248
|
+
): Promise<readonly string[] | undefined>;
|
|
249
|
+
editText<Result>(
|
|
250
|
+
source: string,
|
|
251
|
+
context: ResourceResolverContext,
|
|
252
|
+
operation: (
|
|
253
|
+
text: string,
|
|
254
|
+
resolveAnchor: ResolveTextAnchor,
|
|
255
|
+
) => TextMutationResult<Result> | Promise<TextMutationResult<Result>>,
|
|
256
|
+
): Promise<TextResourceEditOutcome<Result>>;
|
|
257
|
+
editTexts<Result>(
|
|
258
|
+
sources: readonly TextResourceEditRequest[],
|
|
259
|
+
context: TextResourcesEditContext,
|
|
260
|
+
operation: (
|
|
261
|
+
texts: ReadonlyMap<string, string>,
|
|
262
|
+
resolveAnchor: ResolveResourceTextAnchor,
|
|
263
|
+
) => TextResourcesMutationResult<Result> | Promise<TextResourcesMutationResult<Result>>,
|
|
264
|
+
): Promise<TextResourcesEditOutcome<Result>>;
|
|
265
|
+
previewTexts(
|
|
266
|
+
sources: readonly TextResourceEditRequest[],
|
|
267
|
+
context: ResourceResolverContext,
|
|
268
|
+
operation: (
|
|
269
|
+
texts: ReadonlyMap<string, string>,
|
|
270
|
+
resolveAnchor: ResolveResourceTextAnchor,
|
|
271
|
+
) => TextResourcesMutationResult<unknown> | Promise<TextResourcesMutationResult<unknown>>,
|
|
272
|
+
): Promise<TextMutationPreviewOutcome>;
|
|
273
|
+
executeEdit<Input, Result>(
|
|
274
|
+
tool: TextEditorToolId,
|
|
275
|
+
initialState: TextPreEditState<Input>,
|
|
276
|
+
operation: (state: TextPreEditState<Input>) => Result | Promise<Result>,
|
|
277
|
+
): Promise<TextEditExecutionOutcome<Input, Result>>;
|
|
278
|
+
addMutationTool(registration: AnyTextMutationToolRegistration): void;
|
|
279
|
+
getMutationTools(): readonly AnyTextMutationToolRegistration[];
|
|
280
|
+
onMutationTool(listener: TextMutationToolListener): () => void;
|
|
281
|
+
onDidEdit(listener: TextEditCompletionListener): () => void;
|
|
282
|
+
getToolRenderer(tool: TextEditorToolId): TextEditorToolRendererRegistration | undefined;
|
|
283
|
+
registerPlugin(plugin: TextEditorPlugin): Promise<void>;
|
|
284
|
+
registerPostEditHandler(registration: TextPostEditHandlerRegistration): () => void;
|
|
285
|
+
registerTool(tool: TextEditorToolId): void;
|
|
286
|
+
waitForPendingPlugins(): Promise<void>;
|
|
287
|
+
renderGeneralPromptGuideline(): string | undefined;
|
|
288
|
+
renderToolPromptGuideline(tool: TextEditorToolId): string | undefined;
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
export function createTextEditorCore(
|
|
292
|
+
registerMutationTool?: (registration: AnyTextMutationToolRegistration, core: TextEditorCore) => void,
|
|
293
|
+
): TextEditorCore
|
|
294
|
+
{
|
|
295
|
+
const resolvers: RegisteredResolver[] = [];
|
|
296
|
+
const anchorRegistry = new TextAnchorRegistry();
|
|
297
|
+
const handlers: RegisteredHandler[] = [];
|
|
298
|
+
const presenters: RegisteredPresenter[] = [];
|
|
299
|
+
const postEditHandlers = new Map<string, TextPostEditHandlerRegistration>();
|
|
300
|
+
const pendingPlugins = new Set<Promise<void>>();
|
|
301
|
+
const plugins = new Map<string, RegisteredPlugin>();
|
|
302
|
+
const promptContributions: PromptContribution[] = [];
|
|
303
|
+
const writablePromptContributions: WritablePromptContribution[] = [];
|
|
304
|
+
const registeredTools = new Set<TextEditorToolId>();
|
|
305
|
+
const mutationTools = new Map<string, AnyTextMutationToolRegistration>();
|
|
306
|
+
const mutationListeners = new Set<TextMutationToolListener>();
|
|
307
|
+
const editCompletionListeners = new Set<TextEditCompletionListener>();
|
|
308
|
+
const mutationGuards: TextMutationGuardRegistration[] = [];
|
|
309
|
+
const toolRenderers = new Map<TextEditorToolId, TextEditorToolRendererRegistration>();
|
|
310
|
+
let registrationQueue = Promise.resolve();
|
|
311
|
+
const registerContributions = (draft: PluginContributionDraft): void =>
|
|
312
|
+
{
|
|
313
|
+
validateContributionDraft(
|
|
314
|
+
draft,
|
|
315
|
+
resolvers,
|
|
316
|
+
handlers,
|
|
317
|
+
promptContributions,
|
|
318
|
+
writablePromptContributions,
|
|
319
|
+
);
|
|
320
|
+
|
|
321
|
+
const incomingMutationNames = new Set<string>();
|
|
322
|
+
|
|
323
|
+
for (const registration of draft.mutationTools ?? [])
|
|
324
|
+
{
|
|
325
|
+
assertTextMutationToolRegistration(registration);
|
|
326
|
+
|
|
327
|
+
if (mutationTools.has(registration.name) || incomingMutationNames.has(registration.name))
|
|
328
|
+
{
|
|
329
|
+
throw new Error(`Mutation tool ${registration.name} is already registered`);
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
incomingMutationNames.add(registration.name);
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
for (const registration of draft.toolRenderers ?? [])
|
|
336
|
+
{
|
|
337
|
+
if (!isTextEditorToolRendererRegistration(registration))
|
|
338
|
+
{
|
|
339
|
+
throw new TypeError("Plugin provided an invalid text editor tool renderer");
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
anchorRegistry.assertCanAdd(draft.anchorResolvers.map(({ registration }) => registration));
|
|
344
|
+
const incomingPresenters = draft.presenters ?? [];
|
|
345
|
+
const presenterIds = new Set(presenters.map(({ registration }) => registration.presenter.id));
|
|
346
|
+
|
|
347
|
+
for (const contribution of incomingPresenters)
|
|
348
|
+
{
|
|
349
|
+
if (!isTextPresenterRegistration(contribution.registration))
|
|
350
|
+
{
|
|
351
|
+
throw new TypeError(`Plugin ${contribution.pluginId} provided an invalid text presenter`);
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
if (presenterIds.has(contribution.registration.presenter.id))
|
|
355
|
+
{
|
|
356
|
+
throw new Error(`Text presenter ${contribution.registration.presenter.id} is already registered`);
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
presenterIds.add(contribution.registration.presenter.id);
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
for (const contribution of draft.resolvers)
|
|
363
|
+
{
|
|
364
|
+
resolvers.push({
|
|
365
|
+
resolver: contribution.registration.resolver,
|
|
366
|
+
priority: contribution.registration.priority ?? 0,
|
|
367
|
+
order: resolvers.length,
|
|
368
|
+
});
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
for (const contribution of draft.anchorResolvers)
|
|
372
|
+
{
|
|
373
|
+
anchorRegistry.add(contribution.registration);
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
for (const contribution of incomingPresenters)
|
|
377
|
+
{
|
|
378
|
+
presenters.push({ ...contribution, order: presenters.length });
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
handlers.push(...draft.handlers);
|
|
382
|
+
mutationGuards.push(...(draft.mutationGuards ?? []));
|
|
383
|
+
promptContributions.push(...draft.promptContributions);
|
|
384
|
+
writablePromptContributions.push(...draft.writablePromptContributions);
|
|
385
|
+
|
|
386
|
+
for (const registration of draft.toolRenderers ?? [])
|
|
387
|
+
{
|
|
388
|
+
const current = toolRenderers.get(registration.tool);
|
|
389
|
+
toolRenderers.set(registration.tool, mergeToolRenderer(current, registration));
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
for (const tool of draft.tools)
|
|
393
|
+
{
|
|
394
|
+
registeredTools.add(tool);
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
for (const registration of draft.mutationTools ?? [])
|
|
398
|
+
{
|
|
399
|
+
addMutationRegistration(
|
|
400
|
+
registration,
|
|
401
|
+
mutationTools,
|
|
402
|
+
registeredTools,
|
|
403
|
+
mutationListeners,
|
|
404
|
+
registerMutationTool,
|
|
405
|
+
core,
|
|
406
|
+
);
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
for (const listener of draft.mutationToolListeners ?? [])
|
|
410
|
+
{
|
|
411
|
+
replayMutationTools(listener, mutationTools);
|
|
412
|
+
mutationListeners.add(listener);
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
for (const listener of draft.editCompletionListeners ?? [])
|
|
416
|
+
{
|
|
417
|
+
editCompletionListeners.add(listener);
|
|
418
|
+
}
|
|
419
|
+
};
|
|
420
|
+
|
|
421
|
+
const core: TextEditorCore = {
|
|
422
|
+
addAnchorResolver(registration): void
|
|
423
|
+
{
|
|
424
|
+
if (!isTextAnchorResolverRegistration(registration))
|
|
425
|
+
{
|
|
426
|
+
throw new TypeError("Invalid text anchor resolver");
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
anchorRegistry.add(registration);
|
|
430
|
+
},
|
|
431
|
+
addMutationTool(registration): void
|
|
432
|
+
{
|
|
433
|
+
addMutationRegistration(
|
|
434
|
+
registration,
|
|
435
|
+
mutationTools,
|
|
436
|
+
registeredTools,
|
|
437
|
+
mutationListeners,
|
|
438
|
+
registerMutationTool,
|
|
439
|
+
core,
|
|
440
|
+
);
|
|
441
|
+
},
|
|
442
|
+
getMutationTools(): readonly AnyTextMutationToolRegistration[]
|
|
443
|
+
{
|
|
444
|
+
return [...mutationTools.values()];
|
|
445
|
+
},
|
|
446
|
+
onMutationTool(listener): () => void
|
|
447
|
+
{
|
|
448
|
+
replayMutationTools(listener, mutationTools);
|
|
449
|
+
mutationListeners.add(listener);
|
|
450
|
+
return () => mutationListeners.delete(listener);
|
|
451
|
+
},
|
|
452
|
+
onDidEdit(listener): () => void
|
|
453
|
+
{
|
|
454
|
+
editCompletionListeners.add(listener);
|
|
455
|
+
return () => editCompletionListeners.delete(listener);
|
|
456
|
+
},
|
|
457
|
+
getToolRenderer(tool): TextEditorToolRendererRegistration | undefined
|
|
458
|
+
{
|
|
459
|
+
return toolRenderers.get(tool);
|
|
460
|
+
},
|
|
461
|
+
inspectTextAnchors(request): Promise<TextAnchorInspectionOutcome>
|
|
462
|
+
{
|
|
463
|
+
return inspectTextResource(request, [...resolvers], anchorRegistry.snapshot());
|
|
464
|
+
},
|
|
465
|
+
resolveTextAnchorResources(value, kinds, context): Promise<readonly string[] | undefined>
|
|
466
|
+
{
|
|
467
|
+
return anchorRegistry.snapshot().resolveResources(value, context, new Set(kinds));
|
|
468
|
+
},
|
|
469
|
+
editText<Result>(
|
|
470
|
+
source: string,
|
|
471
|
+
context: ResourceResolverContext,
|
|
472
|
+
operation: (
|
|
473
|
+
text: string,
|
|
474
|
+
resolveAnchor: ResolveTextAnchor,
|
|
475
|
+
) => TextMutationResult<Result> | Promise<TextMutationResult<Result>>,
|
|
476
|
+
): Promise<TextResourceEditOutcome<Result>>
|
|
477
|
+
{
|
|
478
|
+
const resolverSnapshot = [...resolvers].sort((left, right) =>
|
|
479
|
+
left.priority - right.priority || left.order - right.order
|
|
480
|
+
);
|
|
481
|
+
const presenterSnapshot = [...presenters].sort((left, right) =>
|
|
482
|
+
(left.registration.priority ?? 0) - (right.registration.priority ?? 0) || left.order - right.order
|
|
483
|
+
);
|
|
484
|
+
return editTextResource(
|
|
485
|
+
source,
|
|
486
|
+
context,
|
|
487
|
+
resolverSnapshot,
|
|
488
|
+
anchorRegistry.snapshot(),
|
|
489
|
+
presenterSnapshot,
|
|
490
|
+
[...postEditHandlers.values()],
|
|
491
|
+
[...editCompletionListeners],
|
|
492
|
+
operation,
|
|
493
|
+
);
|
|
494
|
+
},
|
|
495
|
+
editTexts<Result>(
|
|
496
|
+
sources: readonly TextResourceEditRequest[],
|
|
497
|
+
context: TextResourcesEditContext,
|
|
498
|
+
operation: (
|
|
499
|
+
texts: ReadonlyMap<string, string>,
|
|
500
|
+
resolveAnchor: ResolveResourceTextAnchor,
|
|
501
|
+
) => TextResourcesMutationResult<Result> | Promise<TextResourcesMutationResult<Result>>,
|
|
502
|
+
): Promise<TextResourcesEditOutcome<Result>>
|
|
503
|
+
{
|
|
504
|
+
const resolverSnapshot = [...resolvers].sort((left, right) =>
|
|
505
|
+
left.priority - right.priority || left.order - right.order
|
|
506
|
+
);
|
|
507
|
+
const presenterSnapshot = [...presenters].sort((left, right) =>
|
|
508
|
+
(left.registration.priority ?? 0) - (right.registration.priority ?? 0) || left.order - right.order
|
|
509
|
+
);
|
|
510
|
+
return editTextResources(
|
|
511
|
+
sources,
|
|
512
|
+
context,
|
|
513
|
+
resolverSnapshot,
|
|
514
|
+
anchorRegistry.snapshot(),
|
|
515
|
+
presenterSnapshot,
|
|
516
|
+
[...postEditHandlers.values()],
|
|
517
|
+
[...editCompletionListeners],
|
|
518
|
+
[...mutationGuards],
|
|
519
|
+
operation,
|
|
520
|
+
);
|
|
521
|
+
},
|
|
522
|
+
previewTexts(sources, context, operation): Promise<TextMutationPreviewOutcome>
|
|
523
|
+
{
|
|
524
|
+
const resolverSnapshot = [...resolvers].sort((left, right) =>
|
|
525
|
+
left.priority - right.priority || left.order - right.order
|
|
526
|
+
);
|
|
527
|
+
return previewTextResources(sources, context, resolverSnapshot, anchorRegistry.snapshot(), operation);
|
|
528
|
+
},
|
|
529
|
+
async executeEdit<Input, Result>(
|
|
530
|
+
tool: TextEditorToolId,
|
|
531
|
+
initialState: TextPreEditState<Input>,
|
|
532
|
+
operation: (state: TextPreEditState<Input>) => Result | Promise<Result>,
|
|
533
|
+
): Promise<TextEditExecutionOutcome<Input, Result>>
|
|
534
|
+
{
|
|
535
|
+
if (!isTextEditorToolId(tool))
|
|
536
|
+
{
|
|
537
|
+
throw new Error(`Unsupported text editor tool ${String(tool)}`);
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
const invocationHandlers = handlers.filter((registered) => registered.tool === tool);
|
|
541
|
+
const preEdit = await runPreEditHandlers(initialState, invocationHandlers, tool);
|
|
542
|
+
|
|
543
|
+
if (preEdit.kind === "failed")
|
|
544
|
+
{
|
|
545
|
+
return preEdit;
|
|
546
|
+
}
|
|
547
|
+
|
|
548
|
+
const result = await operation(preEdit.state);
|
|
549
|
+
const editState: TextEditState<Input, Result> = {
|
|
550
|
+
...preEdit.state,
|
|
551
|
+
result,
|
|
552
|
+
};
|
|
553
|
+
const edit = await runResultHandlers("text-edit", editState, invocationHandlers, tool);
|
|
554
|
+
|
|
555
|
+
if (edit.kind === "failed")
|
|
556
|
+
{
|
|
557
|
+
return edit;
|
|
558
|
+
}
|
|
559
|
+
|
|
560
|
+
const postEdit = await runResultHandlers("text-post-edit", edit.state, invocationHandlers, tool);
|
|
561
|
+
|
|
562
|
+
if (postEdit.kind === "failed")
|
|
563
|
+
{
|
|
564
|
+
return postEdit;
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
return { kind: "completed", state: postEdit.state };
|
|
568
|
+
},
|
|
569
|
+
registerPostEditHandler(registration): () => void
|
|
570
|
+
{
|
|
571
|
+
if (!isTextPostEditHandlerRegistration(registration))
|
|
572
|
+
{
|
|
573
|
+
throw new TypeError("Invalid text editor post-edit handler registration");
|
|
574
|
+
}
|
|
575
|
+
|
|
576
|
+
if (postEditHandlers.has(registration.id))
|
|
577
|
+
{
|
|
578
|
+
throw new Error(`Post-edit handler ${registration.id} is already registered`);
|
|
579
|
+
}
|
|
580
|
+
|
|
581
|
+
postEditHandlers.set(registration.id, registration);
|
|
582
|
+
return () =>
|
|
583
|
+
{
|
|
584
|
+
if (postEditHandlers.get(registration.id) === registration)
|
|
585
|
+
{
|
|
586
|
+
postEditHandlers.delete(registration.id);
|
|
587
|
+
}
|
|
588
|
+
};
|
|
589
|
+
},
|
|
590
|
+
registerTool(tool): void
|
|
591
|
+
{
|
|
592
|
+
if (!isTextEditorToolId(tool))
|
|
593
|
+
{
|
|
594
|
+
throw new TypeError("Invalid text editor tool ID");
|
|
595
|
+
}
|
|
596
|
+
|
|
597
|
+
registeredTools.add(tool);
|
|
598
|
+
},
|
|
599
|
+
registerPlugin(plugin): Promise<void>
|
|
600
|
+
{
|
|
601
|
+
const validationError = getPluginValidationError(plugin);
|
|
602
|
+
|
|
603
|
+
if (validationError !== undefined)
|
|
604
|
+
{
|
|
605
|
+
return Promise.reject(validationError);
|
|
606
|
+
}
|
|
607
|
+
|
|
608
|
+
const existing = plugins.get(plugin.id);
|
|
609
|
+
|
|
610
|
+
if (existing?.plugin === plugin)
|
|
611
|
+
{
|
|
612
|
+
return existing.ready;
|
|
613
|
+
}
|
|
614
|
+
|
|
615
|
+
if (existing !== undefined)
|
|
616
|
+
{
|
|
617
|
+
return Promise.reject(new Error(`Plugin ${plugin.id} is already registered`));
|
|
618
|
+
}
|
|
619
|
+
|
|
620
|
+
const lifecycle: PluginLifecycle = { status: "pending" };
|
|
621
|
+
const contributions = createPluginContributionController(
|
|
622
|
+
plugin.id,
|
|
623
|
+
registerContributions,
|
|
624
|
+
(request) => inspectTextResource(request, [...resolvers], anchorRegistry.snapshot()),
|
|
625
|
+
(listener) => core.onMutationTool(listener),
|
|
626
|
+
(listener) => core.onDidEdit(listener),
|
|
627
|
+
(request) => previewTextMutation(core, request),
|
|
628
|
+
);
|
|
629
|
+
const ready = registrationQueue.then(async () =>
|
|
630
|
+
{
|
|
631
|
+
try
|
|
632
|
+
{
|
|
633
|
+
await plugin.setup(contributions.api);
|
|
634
|
+
contributions.commit();
|
|
635
|
+
lifecycle.status = "active";
|
|
636
|
+
return;
|
|
637
|
+
}
|
|
638
|
+
catch (error)
|
|
639
|
+
{
|
|
640
|
+
contributions.close();
|
|
641
|
+
throw error;
|
|
642
|
+
}
|
|
643
|
+
});
|
|
644
|
+
const registeredPlugin: RegisteredPlugin = { lifecycle, plugin, ready };
|
|
645
|
+
|
|
646
|
+
plugins.set(plugin.id, registeredPlugin);
|
|
647
|
+
pendingPlugins.add(ready);
|
|
648
|
+
registrationQueue = ready.catch(() =>
|
|
649
|
+
{});
|
|
650
|
+
void ready.then(
|
|
651
|
+
() =>
|
|
652
|
+
{
|
|
653
|
+
pendingPlugins.delete(ready);
|
|
654
|
+
return;
|
|
655
|
+
},
|
|
656
|
+
() =>
|
|
657
|
+
{
|
|
658
|
+
pendingPlugins.delete(ready);
|
|
659
|
+
|
|
660
|
+
if (plugins.get(plugin.id) === registeredPlugin)
|
|
661
|
+
{
|
|
662
|
+
plugins.delete(plugin.id);
|
|
663
|
+
}
|
|
664
|
+
|
|
665
|
+
return;
|
|
666
|
+
},
|
|
667
|
+
);
|
|
668
|
+
|
|
669
|
+
return ready;
|
|
670
|
+
},
|
|
671
|
+
renderGeneralPromptGuideline(): string | undefined
|
|
672
|
+
{
|
|
673
|
+
const sections: string[] = [];
|
|
674
|
+
const writableEntries: string[] = [];
|
|
675
|
+
|
|
676
|
+
for (const registeredPlugin of plugins.values())
|
|
677
|
+
{
|
|
678
|
+
if (registeredPlugin.lifecycle.status !== "active")
|
|
679
|
+
{
|
|
680
|
+
continue;
|
|
681
|
+
}
|
|
682
|
+
|
|
683
|
+
const contribution = writablePromptContributions.find((candidate) =>
|
|
684
|
+
candidate.pluginId === registeredPlugin.plugin.id
|
|
685
|
+
);
|
|
686
|
+
|
|
687
|
+
if (contribution === undefined)
|
|
688
|
+
{
|
|
689
|
+
continue;
|
|
690
|
+
}
|
|
691
|
+
|
|
692
|
+
const description = renderDescriptionSource(contribution.description);
|
|
693
|
+
|
|
694
|
+
if (description !== undefined)
|
|
695
|
+
{
|
|
696
|
+
writableEntries.push(renderPromptEntry(registeredPlugin.plugin.id, description));
|
|
697
|
+
}
|
|
698
|
+
}
|
|
699
|
+
|
|
700
|
+
if (writableEntries.length > 0)
|
|
701
|
+
{
|
|
702
|
+
sections.push(["Text edits support these writable resources:", ...writableEntries].join("\n"));
|
|
703
|
+
}
|
|
704
|
+
|
|
705
|
+
const anchorSection = anchorRegistry.renderPromptSection();
|
|
706
|
+
|
|
707
|
+
if (anchorSection !== undefined)
|
|
708
|
+
{
|
|
709
|
+
sections.push(anchorSection);
|
|
710
|
+
}
|
|
711
|
+
|
|
712
|
+
return sections.length === 0 ? undefined : indentGuidelineContinuation(sections.join("\n\n"));
|
|
713
|
+
},
|
|
714
|
+
renderToolPromptGuideline(tool): string | undefined
|
|
715
|
+
{
|
|
716
|
+
const entries: string[] = [];
|
|
717
|
+
|
|
718
|
+
for (const registeredPlugin of plugins.values())
|
|
719
|
+
{
|
|
720
|
+
if (registeredPlugin.lifecycle.status !== "active")
|
|
721
|
+
{
|
|
722
|
+
continue;
|
|
723
|
+
}
|
|
724
|
+
|
|
725
|
+
const contribution = promptContributions.find((candidate) =>
|
|
726
|
+
candidate.pluginId === registeredPlugin.plugin.id && candidate.tool === tool
|
|
727
|
+
);
|
|
728
|
+
|
|
729
|
+
if (contribution !== undefined)
|
|
730
|
+
{
|
|
731
|
+
entries.push(renderPromptEntry(registeredPlugin.plugin.id, contribution.description));
|
|
732
|
+
}
|
|
733
|
+
}
|
|
734
|
+
|
|
735
|
+
return entries.length === 0
|
|
736
|
+
? undefined
|
|
737
|
+
: indentGuidelineContinuation([`${tool} supports these installed extensions:`, ...entries].join("\n"));
|
|
738
|
+
},
|
|
739
|
+
async waitForPendingPlugins(): Promise<void>
|
|
740
|
+
{
|
|
741
|
+
await Promise.all(pendingPlugins);
|
|
742
|
+
},
|
|
743
|
+
};
|
|
744
|
+
|
|
745
|
+
return core;
|
|
746
|
+
}
|
|
747
|
+
|
|
748
|
+
async function inspectTextResource(
|
|
749
|
+
request: TextAnchorInspectionRequest,
|
|
750
|
+
resolvers: readonly RegisteredResolver[],
|
|
751
|
+
anchors: TextAnchorRegistrySnapshot,
|
|
752
|
+
): Promise<TextAnchorInspectionOutcome>
|
|
753
|
+
{
|
|
754
|
+
const context: ResourceResolverContext = {
|
|
755
|
+
cwd: request.cwd,
|
|
756
|
+
...(request.signal === undefined ? {} : { signal: request.signal }),
|
|
757
|
+
};
|
|
758
|
+
|
|
759
|
+
for (
|
|
760
|
+
const { resolver } of [...resolvers].sort((left, right) =>
|
|
761
|
+
left.priority - right.priority || left.order - right.order
|
|
762
|
+
)
|
|
763
|
+
)
|
|
764
|
+
{
|
|
765
|
+
let attempt: unknown;
|
|
766
|
+
|
|
767
|
+
try
|
|
768
|
+
{
|
|
769
|
+
attempt = await resolver.tryResolve(request.source, context);
|
|
770
|
+
}
|
|
771
|
+
catch (error)
|
|
772
|
+
{
|
|
773
|
+
return { kind: "failed", reason: `Resolver ${resolver.id} failed`, cause: error };
|
|
774
|
+
}
|
|
775
|
+
|
|
776
|
+
if (!isResourceResolutionAttempt(attempt))
|
|
777
|
+
{
|
|
778
|
+
return { kind: "failed", reason: `Resolver ${resolver.id} returned an invalid result`, cause: attempt };
|
|
779
|
+
}
|
|
780
|
+
|
|
781
|
+
if (attempt.kind === "not-handled")
|
|
782
|
+
{
|
|
783
|
+
continue;
|
|
784
|
+
}
|
|
785
|
+
|
|
786
|
+
if (attempt.kind === "failed")
|
|
787
|
+
{
|
|
788
|
+
return { kind: "failed", reason: `Resolver ${resolver.id} failed`, cause: attempt.error };
|
|
789
|
+
}
|
|
790
|
+
|
|
791
|
+
const { resource } = attempt;
|
|
792
|
+
|
|
793
|
+
if (resource.read === undefined)
|
|
794
|
+
{
|
|
795
|
+
return { kind: "failed", reason: `Resource ${resource.source} is not readable text` };
|
|
796
|
+
}
|
|
797
|
+
|
|
798
|
+
let content: unknown;
|
|
799
|
+
|
|
800
|
+
try
|
|
801
|
+
{
|
|
802
|
+
content = await resource.read(request.signal === undefined ? {} : { signal: request.signal });
|
|
803
|
+
}
|
|
804
|
+
catch (error)
|
|
805
|
+
{
|
|
806
|
+
return { kind: "failed", reason: `Unable to read ${resource.source}`, cause: error };
|
|
807
|
+
}
|
|
808
|
+
|
|
809
|
+
if (!isAgentContent(content) || content.length !== 1 || content[0].type !== "text")
|
|
810
|
+
{
|
|
811
|
+
return { kind: "failed", reason: `Resource ${resource.source} is not readable text`, cause: content };
|
|
812
|
+
}
|
|
813
|
+
|
|
814
|
+
return anchors.inspect(request.anchors, request.kinds, {
|
|
815
|
+
source: resource.source,
|
|
816
|
+
content: content[0].text,
|
|
817
|
+
lines: content[0].text.length === 0 ? [] : content[0].text.split(/\r?\n/u),
|
|
818
|
+
cwd: request.cwd,
|
|
819
|
+
...(request.signal === undefined ? {} : { signal: request.signal }),
|
|
820
|
+
});
|
|
821
|
+
}
|
|
822
|
+
|
|
823
|
+
return { kind: "failed", reason: `No resolver handled ${request.source}` };
|
|
824
|
+
}
|
|
825
|
+
|
|
826
|
+
async function previewTextResources(
|
|
827
|
+
requests: readonly TextResourceEditRequest[],
|
|
828
|
+
context: ResourceResolverContext,
|
|
829
|
+
resolvers: readonly RegisteredResolver[],
|
|
830
|
+
anchorResolvers: TextAnchorRegistrySnapshot,
|
|
831
|
+
operation: (
|
|
832
|
+
texts: ReadonlyMap<string, string>,
|
|
833
|
+
resolveAnchor: ResolveResourceTextAnchor,
|
|
834
|
+
) => TextResourcesMutationResult<unknown> | Promise<TextResourcesMutationResult<unknown>>,
|
|
835
|
+
): Promise<TextMutationPreviewOutcome>
|
|
836
|
+
{
|
|
837
|
+
try
|
|
838
|
+
{
|
|
839
|
+
const requestBySource = new Map(requests.map((request) => [request.source, request]));
|
|
840
|
+
const sources = [...requestBySource.keys()];
|
|
841
|
+
|
|
842
|
+
if (sources.length === 0 || sources.some((source) => source.length === 0))
|
|
843
|
+
{
|
|
844
|
+
return { kind: "failed", reason: "No source was provided" };
|
|
845
|
+
}
|
|
846
|
+
|
|
847
|
+
const prepared = new Map<string, PreparedTextResource>();
|
|
848
|
+
|
|
849
|
+
for (const source of sources)
|
|
850
|
+
{
|
|
851
|
+
const request = requestBySource.get(source)!;
|
|
852
|
+
const outcome = await prepareTextResource(
|
|
853
|
+
source,
|
|
854
|
+
request.read,
|
|
855
|
+
request.allowReadFailure ?? false,
|
|
856
|
+
context,
|
|
857
|
+
resolvers,
|
|
858
|
+
);
|
|
859
|
+
|
|
860
|
+
if ("failure" in outcome)
|
|
861
|
+
{
|
|
862
|
+
return { kind: "failed", reason: outcome.failure.message };
|
|
863
|
+
}
|
|
864
|
+
|
|
865
|
+
prepared.set(source, outcome);
|
|
866
|
+
}
|
|
867
|
+
|
|
868
|
+
const texts = new Map([...prepared].map(([source, item]) => [source, item.before.content]));
|
|
869
|
+
const mutation = await operation(texts, (source, value, kinds) =>
|
|
870
|
+
{
|
|
871
|
+
const item = prepared.get(source);
|
|
872
|
+
|
|
873
|
+
if (item === undefined)
|
|
874
|
+
{
|
|
875
|
+
throw new Error(`Anchor refers to undeclared resource ${source}`);
|
|
876
|
+
}
|
|
877
|
+
|
|
878
|
+
return anchorResolvers.resolve(value, item.anchorContext, kinds === undefined ? undefined : new Set(kinds));
|
|
879
|
+
});
|
|
880
|
+
const resources = sources.map((source) =>
|
|
881
|
+
{
|
|
882
|
+
const item = prepared.get(source)!;
|
|
883
|
+
const changes = mutation.changes.get(source);
|
|
884
|
+
const applied = changes === undefined
|
|
885
|
+
? { content: item.before.content, changes: [] }
|
|
886
|
+
: applyTextChanges(item.before.content, changes);
|
|
887
|
+
return {
|
|
888
|
+
path: source,
|
|
889
|
+
existed: item.existed,
|
|
890
|
+
beforeRanges: applied.changes.map(({ fromBefore: from, toBefore: to }) => ({ from, to })),
|
|
891
|
+
...(item.resource.link === undefined ? {} : { link: item.resource.link }),
|
|
892
|
+
beforeContent: item.before.content,
|
|
893
|
+
afterContent: applied.content,
|
|
894
|
+
ranges: applied.changes.map(({ fromAfter: from, toAfter: to }) => ({ from, to })),
|
|
895
|
+
};
|
|
896
|
+
});
|
|
897
|
+
|
|
898
|
+
return { kind: "completed", resources };
|
|
899
|
+
}
|
|
900
|
+
catch (error)
|
|
901
|
+
{
|
|
902
|
+
return { kind: "failed", reason: error instanceof Error ? error.message : String(error) };
|
|
903
|
+
}
|
|
904
|
+
}
|
|
905
|
+
|
|
906
|
+
interface PreparedTextResource
|
|
907
|
+
{
|
|
908
|
+
readonly requestedSource: string;
|
|
909
|
+
readonly resource: Resource & { readonly write: ResourceWrite; };
|
|
910
|
+
readonly resolverId: string;
|
|
911
|
+
readonly existed: boolean;
|
|
912
|
+
readonly before: TextDocument;
|
|
913
|
+
readonly anchorContext: TextAnchorResolverContext;
|
|
914
|
+
}
|
|
915
|
+
|
|
916
|
+
async function editTextResources<Result>(
|
|
917
|
+
requests: readonly TextResourceEditRequest[],
|
|
918
|
+
context: TextResourcesEditContext,
|
|
919
|
+
resolvers: readonly RegisteredResolver[],
|
|
920
|
+
anchorResolvers: TextAnchorRegistrySnapshot,
|
|
921
|
+
presenters: readonly RegisteredPresenter[],
|
|
922
|
+
postEditHandlers: readonly TextPostEditHandlerRegistration[],
|
|
923
|
+
editCompletionListeners: readonly TextEditCompletionListener[],
|
|
924
|
+
mutationGuards: readonly TextMutationGuardRegistration[],
|
|
925
|
+
operation: (
|
|
926
|
+
texts: ReadonlyMap<string, string>,
|
|
927
|
+
resolveAnchor: ResolveResourceTextAnchor,
|
|
928
|
+
) => TextResourcesMutationResult<Result> | Promise<TextResourcesMutationResult<Result>>,
|
|
929
|
+
): Promise<TextResourcesEditOutcome<Result>>
|
|
930
|
+
{
|
|
931
|
+
const requestBySource = new Map(requests.map((request) => [request.source, request]));
|
|
932
|
+
const sources = [...requestBySource.keys()];
|
|
933
|
+
|
|
934
|
+
if (sources.length === 0 || sources.some((source) => source.length === 0))
|
|
935
|
+
{
|
|
936
|
+
const source = sources.find((candidate) => candidate.length === 0) ?? "";
|
|
937
|
+
return {
|
|
938
|
+
kind: "failed",
|
|
939
|
+
failure: { code: "INVALID_REQUEST", source, message: "No source was provided" },
|
|
940
|
+
completed: [],
|
|
941
|
+
};
|
|
942
|
+
}
|
|
943
|
+
|
|
944
|
+
const prepared = new Map<string, PreparedTextResource>();
|
|
945
|
+
|
|
946
|
+
for (const source of sources)
|
|
947
|
+
{
|
|
948
|
+
const request = requestBySource.get(source)!;
|
|
949
|
+
const outcome = await prepareTextResource(
|
|
950
|
+
source,
|
|
951
|
+
request.read,
|
|
952
|
+
request.allowReadFailure ?? false,
|
|
953
|
+
context,
|
|
954
|
+
resolvers,
|
|
955
|
+
);
|
|
956
|
+
|
|
957
|
+
if ("failure" in outcome)
|
|
958
|
+
{
|
|
959
|
+
return { kind: "failed", failure: outcome.failure, completed: [] };
|
|
960
|
+
}
|
|
961
|
+
|
|
962
|
+
prepared.set(source, outcome);
|
|
963
|
+
}
|
|
964
|
+
|
|
965
|
+
const texts = new Map([...prepared].map(([source, item]) => [source, item.before.content]));
|
|
966
|
+
const mutation = await operation(texts, (source, value, kinds) =>
|
|
967
|
+
{
|
|
968
|
+
const item = prepared.get(source);
|
|
969
|
+
|
|
970
|
+
if (item === undefined)
|
|
971
|
+
{
|
|
972
|
+
throw new Error(`Anchor refers to undeclared resource ${source}`);
|
|
973
|
+
}
|
|
974
|
+
|
|
975
|
+
return anchorResolvers.resolve(value, item.anchorContext, kinds === undefined ? undefined : new Set(kinds));
|
|
976
|
+
});
|
|
977
|
+
|
|
978
|
+
for (const source of mutation.changes.keys())
|
|
979
|
+
{
|
|
980
|
+
if (!prepared.has(source))
|
|
981
|
+
{
|
|
982
|
+
return {
|
|
983
|
+
kind: "failed",
|
|
984
|
+
failure: {
|
|
985
|
+
code: "INVALID_WRITE_CONTENT",
|
|
986
|
+
source,
|
|
987
|
+
message: `Text edit produced an undeclared resource ${source}`,
|
|
988
|
+
},
|
|
989
|
+
completed: [],
|
|
990
|
+
};
|
|
991
|
+
}
|
|
992
|
+
}
|
|
993
|
+
|
|
994
|
+
const applied = new Map<string, TextChangeResult>();
|
|
995
|
+
|
|
996
|
+
for (const [source, changes] of mutation.changes)
|
|
997
|
+
{
|
|
998
|
+
const item = prepared.get(source)!;
|
|
999
|
+
applied.set(source, applyTextChanges(item.before.content, changes));
|
|
1000
|
+
}
|
|
1001
|
+
|
|
1002
|
+
const plan: TextMutationPlan = {
|
|
1003
|
+
resources: sources.flatMap((source) =>
|
|
1004
|
+
{
|
|
1005
|
+
const item = prepared.get(source)!;
|
|
1006
|
+
const result = applied.get(source);
|
|
1007
|
+
|
|
1008
|
+
return result === undefined || result.content === item.before.content
|
|
1009
|
+
? []
|
|
1010
|
+
: [{
|
|
1011
|
+
source: item.resource.source,
|
|
1012
|
+
existed: item.existed,
|
|
1013
|
+
before: item.before,
|
|
1014
|
+
after: createTextDocument(item.resource.source, result.content),
|
|
1015
|
+
changes: result.changes,
|
|
1016
|
+
}];
|
|
1017
|
+
}),
|
|
1018
|
+
};
|
|
1019
|
+
|
|
1020
|
+
for (const registration of mutationGuards)
|
|
1021
|
+
{
|
|
1022
|
+
let outcome;
|
|
1023
|
+
|
|
1024
|
+
try
|
|
1025
|
+
{
|
|
1026
|
+
outcome = await registration.guard(plan, context);
|
|
1027
|
+
}
|
|
1028
|
+
catch (error)
|
|
1029
|
+
{
|
|
1030
|
+
return {
|
|
1031
|
+
kind: "failed",
|
|
1032
|
+
failure: {
|
|
1033
|
+
code: "PLUGIN_FAILED",
|
|
1034
|
+
source: sources[0] ?? "",
|
|
1035
|
+
message: `Mutation guard ${registration.id} failed`,
|
|
1036
|
+
cause: error,
|
|
1037
|
+
},
|
|
1038
|
+
completed: [],
|
|
1039
|
+
};
|
|
1040
|
+
}
|
|
1041
|
+
|
|
1042
|
+
if (outcome.kind === "rejected")
|
|
1043
|
+
{
|
|
1044
|
+
return {
|
|
1045
|
+
kind: "failed",
|
|
1046
|
+
failure: {
|
|
1047
|
+
code: "MUTATION_REJECTED",
|
|
1048
|
+
source: plan.resources[0]?.source ?? sources[0] ?? "",
|
|
1049
|
+
message: outcome.rejection.message,
|
|
1050
|
+
},
|
|
1051
|
+
completed: [],
|
|
1052
|
+
};
|
|
1053
|
+
}
|
|
1054
|
+
}
|
|
1055
|
+
|
|
1056
|
+
const completed: string[] = [];
|
|
1057
|
+
const outcomes: Exclude<TextResourceEditOutcome<unknown>, { readonly kind: "failed"; }>[] = [];
|
|
1058
|
+
|
|
1059
|
+
for (const source of sources)
|
|
1060
|
+
{
|
|
1061
|
+
const text = applied.get(source)?.content;
|
|
1062
|
+
const item = prepared.get(source)!;
|
|
1063
|
+
|
|
1064
|
+
if (text === undefined || text === item.before.content)
|
|
1065
|
+
{
|
|
1066
|
+
continue;
|
|
1067
|
+
}
|
|
1068
|
+
|
|
1069
|
+
const finalContent: unknown = [{ type: "text", text }];
|
|
1070
|
+
|
|
1071
|
+
if (!isAgentContent(finalContent))
|
|
1072
|
+
{
|
|
1073
|
+
return {
|
|
1074
|
+
kind: "failed",
|
|
1075
|
+
failure: {
|
|
1076
|
+
code: "INVALID_WRITE_CONTENT",
|
|
1077
|
+
source,
|
|
1078
|
+
message: `Text edit for ${source} produced invalid content`,
|
|
1079
|
+
},
|
|
1080
|
+
completed,
|
|
1081
|
+
};
|
|
1082
|
+
}
|
|
1083
|
+
|
|
1084
|
+
try
|
|
1085
|
+
{
|
|
1086
|
+
await item.resource.write(finalContent, context.signal === undefined ? {} : { signal: context.signal });
|
|
1087
|
+
}
|
|
1088
|
+
catch (error)
|
|
1089
|
+
{
|
|
1090
|
+
return {
|
|
1091
|
+
kind: "failed",
|
|
1092
|
+
failure: {
|
|
1093
|
+
code: "WRITE_FAILED",
|
|
1094
|
+
source,
|
|
1095
|
+
resolverId: item.resolverId,
|
|
1096
|
+
message: `Unable to write ${source}`,
|
|
1097
|
+
cause: error,
|
|
1098
|
+
},
|
|
1099
|
+
completed,
|
|
1100
|
+
};
|
|
1101
|
+
}
|
|
1102
|
+
|
|
1103
|
+
completed.push(source);
|
|
1104
|
+
outcomes.push(
|
|
1105
|
+
await finalizeTextResource({
|
|
1106
|
+
requestedSource: item.requestedSource,
|
|
1107
|
+
outcomeSource: item.requestedSource,
|
|
1108
|
+
resource: item.resource,
|
|
1109
|
+
resolvedBy: item.resolverId,
|
|
1110
|
+
existed: item.existed,
|
|
1111
|
+
before: item.before,
|
|
1112
|
+
requestedText: text,
|
|
1113
|
+
context,
|
|
1114
|
+
presenters,
|
|
1115
|
+
postEditHandlers,
|
|
1116
|
+
editCompletionListeners,
|
|
1117
|
+
result: mutation.result,
|
|
1118
|
+
}),
|
|
1119
|
+
);
|
|
1120
|
+
}
|
|
1121
|
+
|
|
1122
|
+
return { kind: "completed", resources: outcomes, result: mutation.result };
|
|
1123
|
+
}
|
|
1124
|
+
|
|
1125
|
+
async function prepareTextResource(
|
|
1126
|
+
source: string,
|
|
1127
|
+
read: boolean,
|
|
1128
|
+
allowReadFailure: boolean,
|
|
1129
|
+
context: ResourceResolverContext,
|
|
1130
|
+
resolvers: readonly RegisteredResolver[],
|
|
1131
|
+
): Promise<PreparedTextResource | { readonly failure: TextResourceEditFailure; }>
|
|
1132
|
+
{
|
|
1133
|
+
for (const { resolver } of resolvers)
|
|
1134
|
+
{
|
|
1135
|
+
let attempt: unknown;
|
|
1136
|
+
|
|
1137
|
+
try
|
|
1138
|
+
{
|
|
1139
|
+
attempt = await resolver.tryResolve(source, context);
|
|
1140
|
+
}
|
|
1141
|
+
catch (error)
|
|
1142
|
+
{
|
|
1143
|
+
return {
|
|
1144
|
+
failure: {
|
|
1145
|
+
code: "RESOLVE_FAILED",
|
|
1146
|
+
source,
|
|
1147
|
+
resolverId: resolver.id,
|
|
1148
|
+
message: `Resolver ${resolver.id} failed`,
|
|
1149
|
+
cause: error,
|
|
1150
|
+
},
|
|
1151
|
+
};
|
|
1152
|
+
}
|
|
1153
|
+
|
|
1154
|
+
if (!isResourceResolutionAttempt(attempt))
|
|
1155
|
+
{
|
|
1156
|
+
return {
|
|
1157
|
+
failure: {
|
|
1158
|
+
code: "INVALID_RESOLVER_RESULT",
|
|
1159
|
+
source,
|
|
1160
|
+
resolverId: resolver.id,
|
|
1161
|
+
message: `Resolver ${resolver.id} returned an invalid result`,
|
|
1162
|
+
cause: attempt,
|
|
1163
|
+
},
|
|
1164
|
+
};
|
|
1165
|
+
}
|
|
1166
|
+
|
|
1167
|
+
if (attempt.kind === "not-handled")
|
|
1168
|
+
{
|
|
1169
|
+
continue;
|
|
1170
|
+
}
|
|
1171
|
+
|
|
1172
|
+
if (attempt.kind === "failed")
|
|
1173
|
+
{
|
|
1174
|
+
return {
|
|
1175
|
+
failure: {
|
|
1176
|
+
code: "RESOLVE_FAILED",
|
|
1177
|
+
source,
|
|
1178
|
+
resolverId: resolver.id,
|
|
1179
|
+
message: `Resolver ${resolver.id} failed`,
|
|
1180
|
+
cause: attempt.error,
|
|
1181
|
+
},
|
|
1182
|
+
};
|
|
1183
|
+
}
|
|
1184
|
+
|
|
1185
|
+
if (attempt.resource.write === undefined || (read && attempt.resource.read === undefined))
|
|
1186
|
+
{
|
|
1187
|
+
return {
|
|
1188
|
+
failure: {
|
|
1189
|
+
code: "UNSUPPORTED_CAPABILITY",
|
|
1190
|
+
source: attempt.resource.source,
|
|
1191
|
+
resolverId: resolver.id,
|
|
1192
|
+
message: `Resource ${attempt.resource.source} does not support text editing`,
|
|
1193
|
+
},
|
|
1194
|
+
};
|
|
1195
|
+
}
|
|
1196
|
+
|
|
1197
|
+
let text = "";
|
|
1198
|
+
let existed = read;
|
|
1199
|
+
|
|
1200
|
+
if (read)
|
|
1201
|
+
{
|
|
1202
|
+
let content: unknown;
|
|
1203
|
+
|
|
1204
|
+
try
|
|
1205
|
+
{
|
|
1206
|
+
content = await attempt.resource.read!(context.signal === undefined ? {} : { signal: context.signal });
|
|
1207
|
+
}
|
|
1208
|
+
catch (error)
|
|
1209
|
+
{
|
|
1210
|
+
if (!allowReadFailure)
|
|
1211
|
+
{
|
|
1212
|
+
return {
|
|
1213
|
+
failure: {
|
|
1214
|
+
code: "READ_FAILED",
|
|
1215
|
+
source: attempt.resource.source,
|
|
1216
|
+
resolverId: resolver.id,
|
|
1217
|
+
message: `Unable to read ${attempt.resource.source}`,
|
|
1218
|
+
cause: error,
|
|
1219
|
+
},
|
|
1220
|
+
};
|
|
1221
|
+
}
|
|
1222
|
+
|
|
1223
|
+
existed = false;
|
|
1224
|
+
content = [{ type: "text", text: "" }];
|
|
1225
|
+
}
|
|
1226
|
+
|
|
1227
|
+
if (!isAgentContent(content) || content.length !== 1 || content[0].type !== "text")
|
|
1228
|
+
{
|
|
1229
|
+
return {
|
|
1230
|
+
failure: {
|
|
1231
|
+
code: "UNSUPPORTED_CONTENT",
|
|
1232
|
+
source: attempt.resource.source,
|
|
1233
|
+
resolverId: resolver.id,
|
|
1234
|
+
message: `Resource ${attempt.resource.source} is not editable text`,
|
|
1235
|
+
cause: content,
|
|
1236
|
+
},
|
|
1237
|
+
};
|
|
1238
|
+
}
|
|
1239
|
+
|
|
1240
|
+
text = content[0].text;
|
|
1241
|
+
}
|
|
1242
|
+
|
|
1243
|
+
const before = createTextDocument(attempt.resource.source, text);
|
|
1244
|
+
return {
|
|
1245
|
+
requestedSource: source,
|
|
1246
|
+
resource: attempt.resource,
|
|
1247
|
+
resolverId: resolver.id,
|
|
1248
|
+
existed,
|
|
1249
|
+
before,
|
|
1250
|
+
anchorContext: {
|
|
1251
|
+
source: attempt.resource.source,
|
|
1252
|
+
content: before.content,
|
|
1253
|
+
lines: before.lines.map((line) => line.content),
|
|
1254
|
+
cwd: context.cwd,
|
|
1255
|
+
...(context.signal === undefined ? {} : { signal: context.signal }),
|
|
1256
|
+
},
|
|
1257
|
+
};
|
|
1258
|
+
}
|
|
1259
|
+
|
|
1260
|
+
return { failure: { code: "NO_RESOLVER", source, message: `No resolver handled ${source}` } };
|
|
1261
|
+
}
|
|
1262
|
+
|
|
1263
|
+
interface FinalizeTextResourceRequest<Result>
|
|
1264
|
+
{
|
|
1265
|
+
readonly requestedSource: string;
|
|
1266
|
+
readonly outcomeSource: string;
|
|
1267
|
+
readonly resource: Resource;
|
|
1268
|
+
readonly resolvedBy: string;
|
|
1269
|
+
readonly existed: boolean;
|
|
1270
|
+
readonly before: TextDocument;
|
|
1271
|
+
readonly requestedText: string;
|
|
1272
|
+
readonly context: ResourceResolverContext & { readonly intent?: TextEditIntent; };
|
|
1273
|
+
readonly presenters: readonly RegisteredPresenter[];
|
|
1274
|
+
readonly postEditHandlers: readonly TextPostEditHandlerRegistration[];
|
|
1275
|
+
readonly editCompletionListeners: readonly TextEditCompletionListener[];
|
|
1276
|
+
readonly result: Result;
|
|
1277
|
+
}
|
|
1278
|
+
|
|
1279
|
+
async function finalizeTextResource<Result>(
|
|
1280
|
+
request: FinalizeTextResourceRequest<Result>,
|
|
1281
|
+
): Promise<Exclude<TextResourceEditOutcome<Result>, { readonly kind: "failed"; }>>
|
|
1282
|
+
{
|
|
1283
|
+
const requestedAfter = createTextDocument(request.resource.source, request.requestedText);
|
|
1284
|
+
const transaction: TextPostEditTransaction = {
|
|
1285
|
+
source: request.requestedSource,
|
|
1286
|
+
resourceSource: request.resource.source,
|
|
1287
|
+
resolvedBy: request.resolvedBy,
|
|
1288
|
+
cwd: request.context.cwd,
|
|
1289
|
+
before: request.before,
|
|
1290
|
+
requestedAfter,
|
|
1291
|
+
...(request.context.signal === undefined ? {} : { signal: request.context.signal }),
|
|
1292
|
+
};
|
|
1293
|
+
const postEditContributions: TextPostEditContribution[] = [];
|
|
1294
|
+
|
|
1295
|
+
for (const registration of request.postEditHandlers)
|
|
1296
|
+
{
|
|
1297
|
+
try
|
|
1298
|
+
{
|
|
1299
|
+
const data = await registration.handler(transaction);
|
|
1300
|
+
|
|
1301
|
+
if (data !== undefined)
|
|
1302
|
+
{
|
|
1303
|
+
postEditContributions.push({ id: registration.id, data });
|
|
1304
|
+
}
|
|
1305
|
+
}
|
|
1306
|
+
catch
|
|
1307
|
+
{
|
|
1308
|
+
// Post-edit integrations do not change a successful write into a failure.
|
|
1309
|
+
}
|
|
1310
|
+
}
|
|
1311
|
+
|
|
1312
|
+
let finalText = request.requestedText;
|
|
1313
|
+
|
|
1314
|
+
if (request.resource.read !== undefined)
|
|
1315
|
+
{
|
|
1316
|
+
try
|
|
1317
|
+
{
|
|
1318
|
+
const reread = await request.resource.read(
|
|
1319
|
+
request.context.signal === undefined ? {} : { signal: request.context.signal },
|
|
1320
|
+
);
|
|
1321
|
+
|
|
1322
|
+
if (isAgentContent(reread) && reread.length === 1 && reread[0].type === "text")
|
|
1323
|
+
{
|
|
1324
|
+
finalText = reread[0].text;
|
|
1325
|
+
}
|
|
1326
|
+
}
|
|
1327
|
+
catch
|
|
1328
|
+
{
|
|
1329
|
+
// The requested mutation remains the successful fallback when a final reread fails.
|
|
1330
|
+
}
|
|
1331
|
+
}
|
|
1332
|
+
|
|
1333
|
+
const finalAfter = createTextDocument(request.resource.source, finalText);
|
|
1334
|
+
const completion: TextEditCompletion = {
|
|
1335
|
+
source: request.requestedSource,
|
|
1336
|
+
resourceSource: request.resource.source,
|
|
1337
|
+
resolvedBy: request.resolvedBy,
|
|
1338
|
+
cwd: request.context.cwd,
|
|
1339
|
+
existed: request.existed,
|
|
1340
|
+
before: request.before,
|
|
1341
|
+
after: finalAfter,
|
|
1342
|
+
intent: request.context.intent ?? "edit",
|
|
1343
|
+
};
|
|
1344
|
+
|
|
1345
|
+
for (const listener of request.editCompletionListeners)
|
|
1346
|
+
{
|
|
1347
|
+
try
|
|
1348
|
+
{
|
|
1349
|
+
await listener(completion);
|
|
1350
|
+
}
|
|
1351
|
+
catch
|
|
1352
|
+
{
|
|
1353
|
+
// Completion observers cannot turn a successful write into a failure.
|
|
1354
|
+
}
|
|
1355
|
+
}
|
|
1356
|
+
|
|
1357
|
+
let after = finalAfter;
|
|
1358
|
+
const presentationContext: TextPresentationContext = {
|
|
1359
|
+
purpose: "edit-diff",
|
|
1360
|
+
source: request.resource.source,
|
|
1361
|
+
cwd: request.context.cwd,
|
|
1362
|
+
resolvedBy: request.resolvedBy,
|
|
1363
|
+
...(request.context.signal === undefined ? {} : { signal: request.context.signal }),
|
|
1364
|
+
};
|
|
1365
|
+
|
|
1366
|
+
for (const { registration } of request.presenters)
|
|
1367
|
+
{
|
|
1368
|
+
after = await registration.presenter.present(after, presentationContext);
|
|
1369
|
+
}
|
|
1370
|
+
|
|
1371
|
+
return {
|
|
1372
|
+
kind: "completed",
|
|
1373
|
+
source: request.outcomeSource,
|
|
1374
|
+
resolvedBy: request.resolvedBy,
|
|
1375
|
+
before: request.before,
|
|
1376
|
+
after,
|
|
1377
|
+
result: request.result,
|
|
1378
|
+
postEditContributions,
|
|
1379
|
+
};
|
|
1380
|
+
}
|
|
1381
|
+
|
|
1382
|
+
async function editTextResource<Result>(
|
|
1383
|
+
source: string,
|
|
1384
|
+
context: ResourceResolverContext,
|
|
1385
|
+
resolvers: readonly RegisteredResolver[],
|
|
1386
|
+
anchorResolvers: TextAnchorRegistrySnapshot,
|
|
1387
|
+
presenters: readonly RegisteredPresenter[],
|
|
1388
|
+
postEditHandlers: readonly TextPostEditHandlerRegistration[],
|
|
1389
|
+
editCompletionListeners: readonly TextEditCompletionListener[],
|
|
1390
|
+
operation: (
|
|
1391
|
+
text: string,
|
|
1392
|
+
resolveAnchor: ResolveTextAnchor,
|
|
1393
|
+
) => TextMutationResult<Result> | Promise<TextMutationResult<Result>>,
|
|
1394
|
+
): Promise<TextResourceEditOutcome<Result>>
|
|
1395
|
+
{
|
|
1396
|
+
if (source.length === 0)
|
|
1397
|
+
{
|
|
1398
|
+
return {
|
|
1399
|
+
kind: "failed",
|
|
1400
|
+
failure: {
|
|
1401
|
+
code: "INVALID_REQUEST",
|
|
1402
|
+
source,
|
|
1403
|
+
message: "No source was provided",
|
|
1404
|
+
},
|
|
1405
|
+
};
|
|
1406
|
+
}
|
|
1407
|
+
|
|
1408
|
+
for (const { resolver } of resolvers)
|
|
1409
|
+
{
|
|
1410
|
+
let attempt: unknown;
|
|
1411
|
+
|
|
1412
|
+
try
|
|
1413
|
+
{
|
|
1414
|
+
attempt = await resolver.tryResolve(source, context);
|
|
1415
|
+
}
|
|
1416
|
+
catch (error)
|
|
1417
|
+
{
|
|
1418
|
+
return {
|
|
1419
|
+
kind: "failed",
|
|
1420
|
+
failure: {
|
|
1421
|
+
code: "RESOLVE_FAILED",
|
|
1422
|
+
source,
|
|
1423
|
+
resolverId: resolver.id,
|
|
1424
|
+
message: `Resolver ${resolver.id} failed`,
|
|
1425
|
+
cause: error,
|
|
1426
|
+
},
|
|
1427
|
+
};
|
|
1428
|
+
}
|
|
1429
|
+
|
|
1430
|
+
if (!isResourceResolutionAttempt(attempt))
|
|
1431
|
+
{
|
|
1432
|
+
return {
|
|
1433
|
+
kind: "failed",
|
|
1434
|
+
failure: {
|
|
1435
|
+
code: "INVALID_RESOLVER_RESULT",
|
|
1436
|
+
source,
|
|
1437
|
+
resolverId: resolver.id,
|
|
1438
|
+
message: `Resolver ${resolver.id} returned an invalid result`,
|
|
1439
|
+
cause: attempt,
|
|
1440
|
+
},
|
|
1441
|
+
};
|
|
1442
|
+
}
|
|
1443
|
+
|
|
1444
|
+
if (attempt.kind === "not-handled")
|
|
1445
|
+
{
|
|
1446
|
+
continue;
|
|
1447
|
+
}
|
|
1448
|
+
|
|
1449
|
+
if (attempt.kind === "failed")
|
|
1450
|
+
{
|
|
1451
|
+
return {
|
|
1452
|
+
kind: "failed",
|
|
1453
|
+
failure: {
|
|
1454
|
+
code: "RESOLVE_FAILED",
|
|
1455
|
+
source,
|
|
1456
|
+
resolverId: resolver.id,
|
|
1457
|
+
message: `Resolver ${resolver.id} failed`,
|
|
1458
|
+
cause: attempt.error,
|
|
1459
|
+
},
|
|
1460
|
+
};
|
|
1461
|
+
}
|
|
1462
|
+
|
|
1463
|
+
const resource = attempt.resource;
|
|
1464
|
+
|
|
1465
|
+
if (resource.read === undefined || resource.write === undefined)
|
|
1466
|
+
{
|
|
1467
|
+
return {
|
|
1468
|
+
kind: "failed",
|
|
1469
|
+
failure: {
|
|
1470
|
+
code: "UNSUPPORTED_CAPABILITY",
|
|
1471
|
+
source: resource.source,
|
|
1472
|
+
resolverId: resolver.id,
|
|
1473
|
+
message: `Resource ${resource.source} does not support text editing`,
|
|
1474
|
+
},
|
|
1475
|
+
};
|
|
1476
|
+
}
|
|
1477
|
+
|
|
1478
|
+
let content: unknown;
|
|
1479
|
+
|
|
1480
|
+
try
|
|
1481
|
+
{
|
|
1482
|
+
content = await resource.read({
|
|
1483
|
+
...(context.signal === undefined ? {} : { signal: context.signal }),
|
|
1484
|
+
});
|
|
1485
|
+
}
|
|
1486
|
+
catch (error)
|
|
1487
|
+
{
|
|
1488
|
+
return {
|
|
1489
|
+
kind: "failed",
|
|
1490
|
+
failure: {
|
|
1491
|
+
code: "READ_FAILED",
|
|
1492
|
+
source: resource.source,
|
|
1493
|
+
resolverId: resolver.id,
|
|
1494
|
+
message: `Unable to read ${resource.source}`,
|
|
1495
|
+
cause: error,
|
|
1496
|
+
},
|
|
1497
|
+
};
|
|
1498
|
+
}
|
|
1499
|
+
|
|
1500
|
+
if (!isAgentContent(content))
|
|
1501
|
+
{
|
|
1502
|
+
return {
|
|
1503
|
+
kind: "failed",
|
|
1504
|
+
failure: {
|
|
1505
|
+
code: "INVALID_RESOURCE_CONTENT",
|
|
1506
|
+
source: resource.source,
|
|
1507
|
+
resolverId: resolver.id,
|
|
1508
|
+
message: `Resource ${resource.source} returned invalid content`,
|
|
1509
|
+
cause: content,
|
|
1510
|
+
},
|
|
1511
|
+
};
|
|
1512
|
+
}
|
|
1513
|
+
|
|
1514
|
+
const block = content[0];
|
|
1515
|
+
|
|
1516
|
+
if (content.length !== 1 || block.type !== "text")
|
|
1517
|
+
{
|
|
1518
|
+
return {
|
|
1519
|
+
kind: "failed",
|
|
1520
|
+
failure: {
|
|
1521
|
+
code: "UNSUPPORTED_CONTENT",
|
|
1522
|
+
source: resource.source,
|
|
1523
|
+
resolverId: resolver.id,
|
|
1524
|
+
message: `Resource ${resource.source} is not editable text`,
|
|
1525
|
+
},
|
|
1526
|
+
};
|
|
1527
|
+
}
|
|
1528
|
+
|
|
1529
|
+
const before = createTextDocument(resource.source, block.text);
|
|
1530
|
+
const anchorContext: TextAnchorResolverContext = {
|
|
1531
|
+
source: resource.source,
|
|
1532
|
+
content: before.content,
|
|
1533
|
+
lines: before.lines.map((line) => line.content),
|
|
1534
|
+
cwd: context.cwd,
|
|
1535
|
+
...(context.signal === undefined ? {} : { signal: context.signal }),
|
|
1536
|
+
};
|
|
1537
|
+
const mutation = await operation(
|
|
1538
|
+
block.text,
|
|
1539
|
+
(value, kinds) =>
|
|
1540
|
+
anchorResolvers.resolve(
|
|
1541
|
+
value,
|
|
1542
|
+
anchorContext,
|
|
1543
|
+
kinds === undefined ? undefined : new Set(kinds),
|
|
1544
|
+
),
|
|
1545
|
+
);
|
|
1546
|
+
const finalContent: unknown = [{ type: "text", text: mutation.text }];
|
|
1547
|
+
|
|
1548
|
+
if (!isAgentContent(finalContent))
|
|
1549
|
+
{
|
|
1550
|
+
return {
|
|
1551
|
+
kind: "failed",
|
|
1552
|
+
failure: {
|
|
1553
|
+
code: "INVALID_WRITE_CONTENT",
|
|
1554
|
+
source: resource.source,
|
|
1555
|
+
resolverId: resolver.id,
|
|
1556
|
+
message: `Text edit for ${resource.source} produced invalid content`,
|
|
1557
|
+
cause: mutation,
|
|
1558
|
+
},
|
|
1559
|
+
};
|
|
1560
|
+
}
|
|
1561
|
+
|
|
1562
|
+
try
|
|
1563
|
+
{
|
|
1564
|
+
await resource.write(finalContent, {
|
|
1565
|
+
...(context.signal === undefined ? {} : { signal: context.signal }),
|
|
1566
|
+
});
|
|
1567
|
+
}
|
|
1568
|
+
catch (error)
|
|
1569
|
+
{
|
|
1570
|
+
return {
|
|
1571
|
+
kind: "failed",
|
|
1572
|
+
failure: {
|
|
1573
|
+
code: "WRITE_FAILED",
|
|
1574
|
+
source: resource.source,
|
|
1575
|
+
resolverId: resolver.id,
|
|
1576
|
+
message: `Unable to write ${resource.source}`,
|
|
1577
|
+
cause: error,
|
|
1578
|
+
},
|
|
1579
|
+
};
|
|
1580
|
+
}
|
|
1581
|
+
|
|
1582
|
+
return finalizeTextResource({
|
|
1583
|
+
requestedSource: source,
|
|
1584
|
+
outcomeSource: resource.source,
|
|
1585
|
+
resource,
|
|
1586
|
+
resolvedBy: resolver.id,
|
|
1587
|
+
existed: true,
|
|
1588
|
+
before,
|
|
1589
|
+
requestedText: mutation.text,
|
|
1590
|
+
context,
|
|
1591
|
+
presenters,
|
|
1592
|
+
postEditHandlers,
|
|
1593
|
+
editCompletionListeners,
|
|
1594
|
+
result: mutation.result,
|
|
1595
|
+
});
|
|
1596
|
+
}
|
|
1597
|
+
|
|
1598
|
+
return {
|
|
1599
|
+
kind: "failed",
|
|
1600
|
+
failure: {
|
|
1601
|
+
code: "NO_RESOLVER",
|
|
1602
|
+
source,
|
|
1603
|
+
message: `No resolver handled ${source}`,
|
|
1604
|
+
},
|
|
1605
|
+
};
|
|
1606
|
+
}
|
|
1607
|
+
|
|
1608
|
+
async function runPreEditHandlers<Input>(
|
|
1609
|
+
initialState: TextPreEditState<Input>,
|
|
1610
|
+
handlers: readonly RegisteredHandler[],
|
|
1611
|
+
tool: TextEditorToolId,
|
|
1612
|
+
): Promise<StageRunOutcome<TextPreEditState<Input>>>
|
|
1613
|
+
{
|
|
1614
|
+
let state = initialState;
|
|
1615
|
+
|
|
1616
|
+
for (const registered of handlers)
|
|
1617
|
+
{
|
|
1618
|
+
if (registered.registration.stage !== "text-pre-edit")
|
|
1619
|
+
{
|
|
1620
|
+
continue;
|
|
1621
|
+
}
|
|
1622
|
+
|
|
1623
|
+
try
|
|
1624
|
+
{
|
|
1625
|
+
state = await registered.registration.handler(state) as TextPreEditState<Input>;
|
|
1626
|
+
}
|
|
1627
|
+
catch (error)
|
|
1628
|
+
{
|
|
1629
|
+
return {
|
|
1630
|
+
kind: "failed",
|
|
1631
|
+
failure: pluginFailure(registered.pluginId, tool, "text-pre-edit", error),
|
|
1632
|
+
};
|
|
1633
|
+
}
|
|
1634
|
+
}
|
|
1635
|
+
|
|
1636
|
+
return { kind: "completed", state };
|
|
1637
|
+
}
|
|
1638
|
+
|
|
1639
|
+
async function runResultHandlers<Input, Result>(
|
|
1640
|
+
stage: "text-edit" | "text-post-edit",
|
|
1641
|
+
initialState: TextEditState<Input, Result>,
|
|
1642
|
+
handlers: readonly RegisteredHandler[],
|
|
1643
|
+
tool: TextEditorToolId,
|
|
1644
|
+
): Promise<StageRunOutcome<TextEditState<Input, Result>>>
|
|
1645
|
+
{
|
|
1646
|
+
let state = initialState;
|
|
1647
|
+
|
|
1648
|
+
for (const registered of handlers)
|
|
1649
|
+
{
|
|
1650
|
+
if (registered.registration.stage !== stage)
|
|
1651
|
+
{
|
|
1652
|
+
continue;
|
|
1653
|
+
}
|
|
1654
|
+
|
|
1655
|
+
try
|
|
1656
|
+
{
|
|
1657
|
+
state = await registered.registration.handler(state) as TextEditState<Input, Result>;
|
|
1658
|
+
}
|
|
1659
|
+
catch (error)
|
|
1660
|
+
{
|
|
1661
|
+
return {
|
|
1662
|
+
kind: "failed",
|
|
1663
|
+
failure: pluginFailure(registered.pluginId, tool, stage, error),
|
|
1664
|
+
};
|
|
1665
|
+
}
|
|
1666
|
+
}
|
|
1667
|
+
|
|
1668
|
+
return { kind: "completed", state };
|
|
1669
|
+
}
|
|
1670
|
+
|
|
1671
|
+
function pluginFailure(
|
|
1672
|
+
pluginId: string,
|
|
1673
|
+
tool: TextEditorToolId,
|
|
1674
|
+
stage: TextEditPipelineStage,
|
|
1675
|
+
cause: unknown,
|
|
1676
|
+
): TextEditPipelineFailure
|
|
1677
|
+
{
|
|
1678
|
+
return {
|
|
1679
|
+
code: "PLUGIN_FAILED",
|
|
1680
|
+
pluginId,
|
|
1681
|
+
tool,
|
|
1682
|
+
stage,
|
|
1683
|
+
message: `Plugin ${pluginId} failed during ${stage}`,
|
|
1684
|
+
cause,
|
|
1685
|
+
};
|
|
1686
|
+
}
|
|
1687
|
+
|
|
1688
|
+
function getPluginValidationError(plugin: {
|
|
1689
|
+
readonly apiVersion: number;
|
|
1690
|
+
readonly id: string;
|
|
1691
|
+
readonly protocol: string;
|
|
1692
|
+
}): Error | undefined
|
|
1693
|
+
{
|
|
1694
|
+
if (plugin.protocol !== TEXT_EDITOR_PROTOCOL)
|
|
1695
|
+
{
|
|
1696
|
+
return new Error(`Plugin ${plugin.id} uses an unsupported protocol`);
|
|
1697
|
+
}
|
|
1698
|
+
|
|
1699
|
+
if (plugin.apiVersion !== TEXT_EDITOR_API_VERSION)
|
|
1700
|
+
{
|
|
1701
|
+
return new Error(`Plugin ${plugin.id} uses an unsupported API version`);
|
|
1702
|
+
}
|
|
1703
|
+
|
|
1704
|
+
if (plugin.id.trim().length === 0)
|
|
1705
|
+
{
|
|
1706
|
+
return new Error("Plugin ID must not be empty");
|
|
1707
|
+
}
|
|
1708
|
+
|
|
1709
|
+
return undefined;
|
|
1710
|
+
}
|
|
1711
|
+
|
|
1712
|
+
function createPluginContributionController(
|
|
1713
|
+
pluginId: string,
|
|
1714
|
+
registerContributions: (draft: PluginContributionDraft) => void,
|
|
1715
|
+
inspectTextAnchors: (request: TextAnchorInspectionRequest) => Promise<TextAnchorInspectionOutcome>,
|
|
1716
|
+
onMutationTool: (listener: TextMutationToolListener) => () => void,
|
|
1717
|
+
onDidEdit: (listener: TextEditCompletionListener) => () => void,
|
|
1718
|
+
previewMutation: (request: TextMutationPreviewRequest) => Promise<TextMutationPreviewOutcome>,
|
|
1719
|
+
): PluginContributionController
|
|
1720
|
+
{
|
|
1721
|
+
const setupDraft: PluginContributionDraft = {
|
|
1722
|
+
resolvers: [],
|
|
1723
|
+
anchorResolvers: [],
|
|
1724
|
+
presenters: [],
|
|
1725
|
+
handlers: [],
|
|
1726
|
+
promptContributions: [],
|
|
1727
|
+
writablePromptContributions: [],
|
|
1728
|
+
tools: [],
|
|
1729
|
+
mutationTools: [],
|
|
1730
|
+
mutationToolListeners: [],
|
|
1731
|
+
editCompletionListeners: [],
|
|
1732
|
+
mutationGuards: [],
|
|
1733
|
+
toolRenderers: [],
|
|
1734
|
+
};
|
|
1735
|
+
let state: "active" | "closed" | "setup" = "setup";
|
|
1736
|
+
const assertAvailable = (): void =>
|
|
1737
|
+
{
|
|
1738
|
+
if (state === "closed")
|
|
1739
|
+
{
|
|
1740
|
+
throw new Error(`Plugin ${pluginId} API is closed`);
|
|
1741
|
+
}
|
|
1742
|
+
};
|
|
1743
|
+
const createToolApi = (tool: TextEditorToolId): TextEditorToolPluginApi => ({
|
|
1744
|
+
addHandler(registration): void
|
|
1745
|
+
{
|
|
1746
|
+
assertAvailable();
|
|
1747
|
+
|
|
1748
|
+
if (!isTextEditHandlerRegistration(registration))
|
|
1749
|
+
{
|
|
1750
|
+
throw new TypeError(`Plugin ${pluginId} provided an invalid handler for ${tool}`);
|
|
1751
|
+
}
|
|
1752
|
+
|
|
1753
|
+
const contribution: RegisteredHandler = { pluginId, registration, tool };
|
|
1754
|
+
|
|
1755
|
+
if (state === "setup")
|
|
1756
|
+
{
|
|
1757
|
+
assertNoDraftHandler(setupDraft.handlers, contribution);
|
|
1758
|
+
setupDraft.handlers.push(contribution);
|
|
1759
|
+
return;
|
|
1760
|
+
}
|
|
1761
|
+
|
|
1762
|
+
registerContributions({
|
|
1763
|
+
resolvers: [],
|
|
1764
|
+
anchorResolvers: [],
|
|
1765
|
+
handlers: [contribution],
|
|
1766
|
+
promptContributions: [],
|
|
1767
|
+
writablePromptContributions: [],
|
|
1768
|
+
tools: [],
|
|
1769
|
+
});
|
|
1770
|
+
},
|
|
1771
|
+
describe(description): void
|
|
1772
|
+
{
|
|
1773
|
+
assertAvailable();
|
|
1774
|
+
const contribution: PromptContribution = {
|
|
1775
|
+
description: normalizeDescription(`Plugin prompt description for ${tool}`, description),
|
|
1776
|
+
pluginId,
|
|
1777
|
+
tool,
|
|
1778
|
+
};
|
|
1779
|
+
|
|
1780
|
+
if (state === "setup")
|
|
1781
|
+
{
|
|
1782
|
+
assertNoDraftDescription(setupDraft.promptContributions, contribution);
|
|
1783
|
+
setupDraft.promptContributions.push(contribution);
|
|
1784
|
+
return;
|
|
1785
|
+
}
|
|
1786
|
+
|
|
1787
|
+
registerContributions({
|
|
1788
|
+
resolvers: [],
|
|
1789
|
+
anchorResolvers: [],
|
|
1790
|
+
handlers: [],
|
|
1791
|
+
promptContributions: [contribution],
|
|
1792
|
+
writablePromptContributions: [],
|
|
1793
|
+
tools: [],
|
|
1794
|
+
});
|
|
1795
|
+
},
|
|
1796
|
+
});
|
|
1797
|
+
const api: TextEditorPluginApi = {
|
|
1798
|
+
addMutationTool(registration): void
|
|
1799
|
+
{
|
|
1800
|
+
assertAvailable();
|
|
1801
|
+
assertTextMutationToolRegistration(registration);
|
|
1802
|
+
|
|
1803
|
+
if (state === "setup")
|
|
1804
|
+
{
|
|
1805
|
+
setupDraft.mutationTools!.push(registration);
|
|
1806
|
+
return;
|
|
1807
|
+
}
|
|
1808
|
+
|
|
1809
|
+
registerContributions({
|
|
1810
|
+
resolvers: [],
|
|
1811
|
+
anchorResolvers: [],
|
|
1812
|
+
handlers: [],
|
|
1813
|
+
promptContributions: [],
|
|
1814
|
+
writablePromptContributions: [],
|
|
1815
|
+
tools: [],
|
|
1816
|
+
mutationTools: [registration],
|
|
1817
|
+
});
|
|
1818
|
+
},
|
|
1819
|
+
addToolRenderer(registration): void
|
|
1820
|
+
{
|
|
1821
|
+
assertAvailable();
|
|
1822
|
+
|
|
1823
|
+
if (!isTextEditorToolRendererRegistration(registration))
|
|
1824
|
+
{
|
|
1825
|
+
throw new TypeError(`Plugin ${pluginId} provided an invalid text editor tool renderer`);
|
|
1826
|
+
}
|
|
1827
|
+
|
|
1828
|
+
if (state === "setup")
|
|
1829
|
+
{
|
|
1830
|
+
setupDraft.toolRenderers!.push(registration);
|
|
1831
|
+
return;
|
|
1832
|
+
}
|
|
1833
|
+
|
|
1834
|
+
registerContributions({
|
|
1835
|
+
resolvers: [],
|
|
1836
|
+
anchorResolvers: [],
|
|
1837
|
+
handlers: [],
|
|
1838
|
+
promptContributions: [],
|
|
1839
|
+
writablePromptContributions: [],
|
|
1840
|
+
tools: [],
|
|
1841
|
+
toolRenderers: [registration],
|
|
1842
|
+
});
|
|
1843
|
+
},
|
|
1844
|
+
addMutationGuard(registration): void
|
|
1845
|
+
{
|
|
1846
|
+
assertAvailable();
|
|
1847
|
+
|
|
1848
|
+
if (registration.id.trim().length === 0 || typeof registration.guard !== "function")
|
|
1849
|
+
{
|
|
1850
|
+
throw new TypeError(`Plugin ${pluginId} provided an invalid mutation guard`);
|
|
1851
|
+
}
|
|
1852
|
+
|
|
1853
|
+
if (state === "setup")
|
|
1854
|
+
{
|
|
1855
|
+
setupDraft.mutationGuards!.push(registration);
|
|
1856
|
+
return;
|
|
1857
|
+
}
|
|
1858
|
+
|
|
1859
|
+
registerContributions({
|
|
1860
|
+
resolvers: [],
|
|
1861
|
+
anchorResolvers: [],
|
|
1862
|
+
handlers: [],
|
|
1863
|
+
promptContributions: [],
|
|
1864
|
+
writablePromptContributions: [],
|
|
1865
|
+
tools: [],
|
|
1866
|
+
mutationGuards: [registration],
|
|
1867
|
+
});
|
|
1868
|
+
},
|
|
1869
|
+
onMutationTool(listener): () => void
|
|
1870
|
+
{
|
|
1871
|
+
assertAvailable();
|
|
1872
|
+
|
|
1873
|
+
if (state === "setup")
|
|
1874
|
+
{
|
|
1875
|
+
setupDraft.mutationToolListeners!.push(listener);
|
|
1876
|
+
return () =>
|
|
1877
|
+
{
|
|
1878
|
+
const index = setupDraft.mutationToolListeners!.indexOf(listener);
|
|
1879
|
+
|
|
1880
|
+
if (index !== -1)
|
|
1881
|
+
{
|
|
1882
|
+
setupDraft.mutationToolListeners!.splice(index, 1);
|
|
1883
|
+
}
|
|
1884
|
+
};
|
|
1885
|
+
}
|
|
1886
|
+
|
|
1887
|
+
return onMutationTool(listener);
|
|
1888
|
+
},
|
|
1889
|
+
onDidEdit(listener): () => void
|
|
1890
|
+
{
|
|
1891
|
+
assertAvailable();
|
|
1892
|
+
|
|
1893
|
+
if (typeof listener !== "function")
|
|
1894
|
+
{
|
|
1895
|
+
throw new TypeError(`Plugin ${pluginId} provided an invalid edit completion listener`);
|
|
1896
|
+
}
|
|
1897
|
+
|
|
1898
|
+
if (state === "setup")
|
|
1899
|
+
{
|
|
1900
|
+
setupDraft.editCompletionListeners!.push(listener);
|
|
1901
|
+
return () =>
|
|
1902
|
+
{
|
|
1903
|
+
const index = setupDraft.editCompletionListeners!.indexOf(listener);
|
|
1904
|
+
|
|
1905
|
+
if (index !== -1)
|
|
1906
|
+
{
|
|
1907
|
+
setupDraft.editCompletionListeners!.splice(index, 1);
|
|
1908
|
+
}
|
|
1909
|
+
};
|
|
1910
|
+
}
|
|
1911
|
+
|
|
1912
|
+
return onDidEdit(listener);
|
|
1913
|
+
},
|
|
1914
|
+
inspectTextAnchors(request): Promise<TextAnchorInspectionOutcome>
|
|
1915
|
+
{
|
|
1916
|
+
assertAvailable();
|
|
1917
|
+
return inspectTextAnchors(request);
|
|
1918
|
+
},
|
|
1919
|
+
previewMutation(request): Promise<TextMutationPreviewOutcome>
|
|
1920
|
+
{
|
|
1921
|
+
assertAvailable();
|
|
1922
|
+
return previewMutation(request);
|
|
1923
|
+
},
|
|
1924
|
+
addResolver(registration): void
|
|
1925
|
+
{
|
|
1926
|
+
assertAvailable();
|
|
1927
|
+
|
|
1928
|
+
if (!isResourceResolverRegistration(registration))
|
|
1929
|
+
{
|
|
1930
|
+
throw new TypeError(`Plugin ${pluginId} provided an invalid resource resolver`);
|
|
1931
|
+
}
|
|
1932
|
+
|
|
1933
|
+
const contribution: ResolverContribution = { pluginId, registration };
|
|
1934
|
+
|
|
1935
|
+
if (state === "setup")
|
|
1936
|
+
{
|
|
1937
|
+
assertNoDraftResolver(setupDraft.resolvers, contribution);
|
|
1938
|
+
setupDraft.resolvers.push(contribution);
|
|
1939
|
+
return;
|
|
1940
|
+
}
|
|
1941
|
+
|
|
1942
|
+
registerContributions({
|
|
1943
|
+
resolvers: [contribution],
|
|
1944
|
+
anchorResolvers: [],
|
|
1945
|
+
handlers: [],
|
|
1946
|
+
promptContributions: [],
|
|
1947
|
+
writablePromptContributions: [],
|
|
1948
|
+
tools: [],
|
|
1949
|
+
});
|
|
1950
|
+
},
|
|
1951
|
+
addAnchorResolver(registration): void
|
|
1952
|
+
{
|
|
1953
|
+
assertAvailable();
|
|
1954
|
+
|
|
1955
|
+
if (!isTextAnchorResolverRegistration(registration))
|
|
1956
|
+
{
|
|
1957
|
+
throw new TypeError(`Plugin ${pluginId} provided an invalid text anchor resolver`);
|
|
1958
|
+
}
|
|
1959
|
+
|
|
1960
|
+
const contribution: TextAnchorResolverContribution = { pluginId, registration };
|
|
1961
|
+
|
|
1962
|
+
if (state === "setup")
|
|
1963
|
+
{
|
|
1964
|
+
setupDraft.anchorResolvers.push(contribution);
|
|
1965
|
+
return;
|
|
1966
|
+
}
|
|
1967
|
+
|
|
1968
|
+
registerContributions({
|
|
1969
|
+
resolvers: [],
|
|
1970
|
+
anchorResolvers: [contribution],
|
|
1971
|
+
handlers: [],
|
|
1972
|
+
promptContributions: [],
|
|
1973
|
+
writablePromptContributions: [],
|
|
1974
|
+
tools: [],
|
|
1975
|
+
});
|
|
1976
|
+
},
|
|
1977
|
+
addTextPresenter(registration): void
|
|
1978
|
+
{
|
|
1979
|
+
assertAvailable();
|
|
1980
|
+
|
|
1981
|
+
if (!isTextPresenterRegistration(registration))
|
|
1982
|
+
{
|
|
1983
|
+
throw new TypeError(`Plugin ${pluginId} provided an invalid text presenter`);
|
|
1984
|
+
}
|
|
1985
|
+
|
|
1986
|
+
const contribution: TextPresenterContribution = { pluginId, registration };
|
|
1987
|
+
|
|
1988
|
+
if (state === "setup")
|
|
1989
|
+
{
|
|
1990
|
+
setupDraft.presenters!.push(contribution);
|
|
1991
|
+
return;
|
|
1992
|
+
}
|
|
1993
|
+
|
|
1994
|
+
registerContributions({
|
|
1995
|
+
resolvers: [],
|
|
1996
|
+
anchorResolvers: [],
|
|
1997
|
+
presenters: [contribution],
|
|
1998
|
+
handlers: [],
|
|
1999
|
+
promptContributions: [],
|
|
2000
|
+
writablePromptContributions: [],
|
|
2001
|
+
tools: [],
|
|
2002
|
+
});
|
|
2003
|
+
},
|
|
2004
|
+
describe(description): void
|
|
2005
|
+
{
|
|
2006
|
+
assertAvailable();
|
|
2007
|
+
|
|
2008
|
+
if (setupDraft.writablePromptContributions.length > 0)
|
|
2009
|
+
{
|
|
2010
|
+
throw new Error(`Plugin ${pluginId} provides more than one writable resource description`);
|
|
2011
|
+
}
|
|
2012
|
+
|
|
2013
|
+
const contribution: WritablePromptContribution = {
|
|
2014
|
+
description: normalizeDescriptionSource(description),
|
|
2015
|
+
pluginId,
|
|
2016
|
+
};
|
|
2017
|
+
|
|
2018
|
+
if (state === "setup")
|
|
2019
|
+
{
|
|
2020
|
+
setupDraft.writablePromptContributions.push(contribution);
|
|
2021
|
+
return;
|
|
2022
|
+
}
|
|
2023
|
+
|
|
2024
|
+
registerContributions({
|
|
2025
|
+
resolvers: [],
|
|
2026
|
+
anchorResolvers: [],
|
|
2027
|
+
handlers: [],
|
|
2028
|
+
promptContributions: [],
|
|
2029
|
+
writablePromptContributions: [contribution],
|
|
2030
|
+
tools: [],
|
|
2031
|
+
});
|
|
2032
|
+
},
|
|
2033
|
+
tool(tool): TextEditorToolPluginApi
|
|
2034
|
+
{
|
|
2035
|
+
assertAvailable();
|
|
2036
|
+
|
|
2037
|
+
if (!isTextEditorToolId(tool))
|
|
2038
|
+
{
|
|
2039
|
+
throw new Error(`Plugin ${pluginId} provided an invalid tool ID`);
|
|
2040
|
+
}
|
|
2041
|
+
|
|
2042
|
+
if (state === "setup")
|
|
2043
|
+
{
|
|
2044
|
+
if (!setupDraft.tools.includes(tool))
|
|
2045
|
+
{
|
|
2046
|
+
setupDraft.tools.push(tool);
|
|
2047
|
+
}
|
|
2048
|
+
}
|
|
2049
|
+
else
|
|
2050
|
+
{
|
|
2051
|
+
registerContributions({
|
|
2052
|
+
resolvers: [],
|
|
2053
|
+
anchorResolvers: [],
|
|
2054
|
+
handlers: [],
|
|
2055
|
+
promptContributions: [],
|
|
2056
|
+
writablePromptContributions: [],
|
|
2057
|
+
tools: [tool],
|
|
2058
|
+
});
|
|
2059
|
+
}
|
|
2060
|
+
|
|
2061
|
+
return createToolApi(tool);
|
|
2062
|
+
},
|
|
2063
|
+
};
|
|
2064
|
+
|
|
2065
|
+
return {
|
|
2066
|
+
api,
|
|
2067
|
+
close(): void
|
|
2068
|
+
{
|
|
2069
|
+
state = "closed";
|
|
2070
|
+
},
|
|
2071
|
+
commit(): void
|
|
2072
|
+
{
|
|
2073
|
+
if (state !== "setup")
|
|
2074
|
+
{
|
|
2075
|
+
throw new Error(`Plugin ${pluginId} setup contributions cannot be committed`);
|
|
2076
|
+
}
|
|
2077
|
+
|
|
2078
|
+
registerContributions(setupDraft);
|
|
2079
|
+
state = "active";
|
|
2080
|
+
},
|
|
2081
|
+
};
|
|
2082
|
+
}
|
|
2083
|
+
|
|
2084
|
+
function validateContributionDraft(
|
|
2085
|
+
draft: PluginContributionDraft,
|
|
2086
|
+
registeredResolvers: readonly RegisteredResolver[],
|
|
2087
|
+
registeredHandlers: readonly RegisteredHandler[],
|
|
2088
|
+
registeredPromptContributions: readonly PromptContribution[],
|
|
2089
|
+
registeredWritablePromptContributions: readonly WritablePromptContribution[],
|
|
2090
|
+
): void
|
|
2091
|
+
{
|
|
2092
|
+
const resolverIds = new Set(registeredResolvers.map(({ resolver }) => resolver.id));
|
|
2093
|
+
|
|
2094
|
+
for (const contribution of draft.resolvers)
|
|
2095
|
+
{
|
|
2096
|
+
if (!isResourceResolverRegistration(contribution.registration))
|
|
2097
|
+
{
|
|
2098
|
+
throw new TypeError(`Plugin ${contribution.pluginId} provided an invalid resource resolver`);
|
|
2099
|
+
}
|
|
2100
|
+
|
|
2101
|
+
const resolverId = contribution.registration.resolver.id;
|
|
2102
|
+
|
|
2103
|
+
if (resolverIds.has(resolverId))
|
|
2104
|
+
{
|
|
2105
|
+
throw new Error(`Resource resolver ${resolverId} is already registered`);
|
|
2106
|
+
}
|
|
2107
|
+
|
|
2108
|
+
resolverIds.add(resolverId);
|
|
2109
|
+
}
|
|
2110
|
+
|
|
2111
|
+
const handlerKeys = new Set(registeredHandlers.map(handlerKey));
|
|
2112
|
+
|
|
2113
|
+
for (const handler of draft.handlers)
|
|
2114
|
+
{
|
|
2115
|
+
if (!isTextEditHandlerRegistration(handler.registration))
|
|
2116
|
+
{
|
|
2117
|
+
throw new TypeError(`Plugin ${handler.pluginId} provided an invalid handler for ${handler.tool}`);
|
|
2118
|
+
}
|
|
2119
|
+
|
|
2120
|
+
const key = handlerKey(handler);
|
|
2121
|
+
|
|
2122
|
+
if (handlerKeys.has(key))
|
|
2123
|
+
{
|
|
2124
|
+
throw new Error(
|
|
2125
|
+
`Plugin ${handler.pluginId} already handles ${handler.registration.stage} for ${handler.tool}`,
|
|
2126
|
+
);
|
|
2127
|
+
}
|
|
2128
|
+
|
|
2129
|
+
handlerKeys.add(key);
|
|
2130
|
+
}
|
|
2131
|
+
|
|
2132
|
+
const descriptionKeys = new Set(registeredPromptContributions.map(descriptionKey));
|
|
2133
|
+
|
|
2134
|
+
for (const contribution of draft.promptContributions)
|
|
2135
|
+
{
|
|
2136
|
+
const key = descriptionKey(contribution);
|
|
2137
|
+
|
|
2138
|
+
if (descriptionKeys.has(key))
|
|
2139
|
+
{
|
|
2140
|
+
throw new Error(`Plugin ${contribution.pluginId} describes ${contribution.tool} more than once`);
|
|
2141
|
+
}
|
|
2142
|
+
|
|
2143
|
+
descriptionKeys.add(key);
|
|
2144
|
+
}
|
|
2145
|
+
|
|
2146
|
+
const writablePluginIds = new Set(
|
|
2147
|
+
registeredWritablePromptContributions.map(({ pluginId }) => pluginId),
|
|
2148
|
+
);
|
|
2149
|
+
|
|
2150
|
+
for (const contribution of draft.writablePromptContributions)
|
|
2151
|
+
{
|
|
2152
|
+
if (writablePluginIds.has(contribution.pluginId))
|
|
2153
|
+
{
|
|
2154
|
+
throw new Error(
|
|
2155
|
+
`Plugin ${contribution.pluginId} provides more than one writable resource description`,
|
|
2156
|
+
);
|
|
2157
|
+
}
|
|
2158
|
+
|
|
2159
|
+
writablePluginIds.add(contribution.pluginId);
|
|
2160
|
+
}
|
|
2161
|
+
|
|
2162
|
+
for (const tool of draft.tools)
|
|
2163
|
+
{
|
|
2164
|
+
if (!isTextEditorToolId(tool))
|
|
2165
|
+
{
|
|
2166
|
+
throw new TypeError("Plugin provided an invalid tool ID");
|
|
2167
|
+
}
|
|
2168
|
+
}
|
|
2169
|
+
}
|
|
2170
|
+
|
|
2171
|
+
function assertNoDraftResolver(
|
|
2172
|
+
resolvers: readonly ResolverContribution[],
|
|
2173
|
+
incoming: ResolverContribution,
|
|
2174
|
+
): void
|
|
2175
|
+
{
|
|
2176
|
+
const resolverId = incoming.registration.resolver.id;
|
|
2177
|
+
|
|
2178
|
+
if (resolvers.some((resolver) => resolver.registration.resolver.id === resolverId))
|
|
2179
|
+
{
|
|
2180
|
+
throw new Error(`Resource resolver ${resolverId} is already registered`);
|
|
2181
|
+
}
|
|
2182
|
+
}
|
|
2183
|
+
|
|
2184
|
+
function assertNoDraftHandler(
|
|
2185
|
+
handlers: readonly RegisteredHandler[],
|
|
2186
|
+
incoming: RegisteredHandler,
|
|
2187
|
+
): void
|
|
2188
|
+
{
|
|
2189
|
+
if (handlers.some((handler) => handlerKey(handler) === handlerKey(incoming)))
|
|
2190
|
+
{
|
|
2191
|
+
throw new Error(
|
|
2192
|
+
`Plugin ${incoming.pluginId} already handles ${incoming.registration.stage} for ${incoming.tool}`,
|
|
2193
|
+
);
|
|
2194
|
+
}
|
|
2195
|
+
}
|
|
2196
|
+
|
|
2197
|
+
function assertNoDraftDescription(
|
|
2198
|
+
contributions: readonly PromptContribution[],
|
|
2199
|
+
incoming: PromptContribution,
|
|
2200
|
+
): void
|
|
2201
|
+
{
|
|
2202
|
+
if (contributions.some((contribution) => descriptionKey(contribution) === descriptionKey(incoming)))
|
|
2203
|
+
{
|
|
2204
|
+
throw new Error(`Plugin ${incoming.pluginId} describes ${incoming.tool} more than once`);
|
|
2205
|
+
}
|
|
2206
|
+
}
|
|
2207
|
+
|
|
2208
|
+
function handlerKey(handler: RegisteredHandler): string
|
|
2209
|
+
{
|
|
2210
|
+
return `${handler.pluginId}\0${handler.tool}\0${handler.registration.stage}`;
|
|
2211
|
+
}
|
|
2212
|
+
|
|
2213
|
+
function descriptionKey(contribution: PromptContribution): string
|
|
2214
|
+
{
|
|
2215
|
+
return `${contribution.pluginId}\0${contribution.tool}`;
|
|
2216
|
+
}
|
|
2217
|
+
|
|
2218
|
+
function normalizeDescriptionSource(value: unknown): PromptDescriptionSource
|
|
2219
|
+
{
|
|
2220
|
+
if (typeof value === "string")
|
|
2221
|
+
{
|
|
2222
|
+
return normalizeDescription("Writable resource prompt description", value);
|
|
2223
|
+
}
|
|
2224
|
+
|
|
2225
|
+
if (typeof value === "function")
|
|
2226
|
+
{
|
|
2227
|
+
return value as () => string | undefined;
|
|
2228
|
+
}
|
|
2229
|
+
|
|
2230
|
+
throw new TypeError("Writable resource prompt description must be a string or callback");
|
|
2231
|
+
}
|
|
2232
|
+
|
|
2233
|
+
function renderDescriptionSource(source: PromptDescriptionSource): string | undefined
|
|
2234
|
+
{
|
|
2235
|
+
if (typeof source === "string")
|
|
2236
|
+
{
|
|
2237
|
+
return source;
|
|
2238
|
+
}
|
|
2239
|
+
|
|
2240
|
+
const value: unknown = source();
|
|
2241
|
+
return value === undefined
|
|
2242
|
+
? undefined
|
|
2243
|
+
: normalizeDescription("Writable resource prompt description", value);
|
|
2244
|
+
}
|
|
2245
|
+
|
|
2246
|
+
function normalizeDescription(label: string, value: unknown): string
|
|
2247
|
+
{
|
|
2248
|
+
if (typeof value !== "string")
|
|
2249
|
+
{
|
|
2250
|
+
throw new TypeError(`${label} must be a string`);
|
|
2251
|
+
}
|
|
2252
|
+
|
|
2253
|
+
const lines = value
|
|
2254
|
+
.replaceAll("\r\n", "\n")
|
|
2255
|
+
.replaceAll("\r", "\n")
|
|
2256
|
+
.split("\n")
|
|
2257
|
+
.map((line) => line.trimEnd());
|
|
2258
|
+
|
|
2259
|
+
while (lines[0]?.trim().length === 0)
|
|
2260
|
+
{
|
|
2261
|
+
lines.shift();
|
|
2262
|
+
}
|
|
2263
|
+
|
|
2264
|
+
while (lines.at(-1)?.trim().length === 0)
|
|
2265
|
+
{
|
|
2266
|
+
lines.pop();
|
|
2267
|
+
}
|
|
2268
|
+
|
|
2269
|
+
const description = lines.join("\n");
|
|
2270
|
+
|
|
2271
|
+
if (description.trim().length === 0)
|
|
2272
|
+
{
|
|
2273
|
+
throw new Error(`${label} is empty`);
|
|
2274
|
+
}
|
|
2275
|
+
|
|
2276
|
+
return description;
|
|
2277
|
+
}
|
|
2278
|
+
|
|
2279
|
+
function renderPromptEntry(pluginId: string, description: string): string
|
|
2280
|
+
{
|
|
2281
|
+
const [firstLine, ...continuationLines] = description.split("\n");
|
|
2282
|
+
|
|
2283
|
+
return [
|
|
2284
|
+
`- \`${escapeInlineCode(pluginId)}\` — ${firstLine ?? ""}`,
|
|
2285
|
+
...continuationLines.map((line) => ` ${line}`),
|
|
2286
|
+
].join("\n");
|
|
2287
|
+
}
|
|
2288
|
+
|
|
2289
|
+
function indentGuidelineContinuation(guideline: string): string
|
|
2290
|
+
{
|
|
2291
|
+
const [firstLine, ...continuationLines] = guideline.split("\n");
|
|
2292
|
+
return [firstLine ?? "", ...continuationLines.map((line) => line.length === 0 ? line : ` ${line}`)].join("\n");
|
|
2293
|
+
}
|
|
2294
|
+
|
|
2295
|
+
function escapeInlineCode(value: string): string
|
|
2296
|
+
{
|
|
2297
|
+
return value.replaceAll("`", "\\`");
|
|
2298
|
+
}
|
|
2299
|
+
|
|
2300
|
+
function mergeToolRenderer(
|
|
2301
|
+
current: TextEditorToolRendererRegistration | undefined,
|
|
2302
|
+
registration: TextEditorToolRendererRegistration,
|
|
2303
|
+
): TextEditorToolRendererRegistration
|
|
2304
|
+
{
|
|
2305
|
+
const merged = registration.fallback === true
|
|
2306
|
+
? { ...registration, ...current, tool: registration.tool }
|
|
2307
|
+
: { ...current, ...registration, tool: registration.tool };
|
|
2308
|
+
const { fallback: _fallback, ...renderer } = merged;
|
|
2309
|
+
return renderer;
|
|
2310
|
+
}
|
|
2311
|
+
|
|
2312
|
+
function replayMutationTools(
|
|
2313
|
+
listener: TextMutationToolListener,
|
|
2314
|
+
mutationTools: ReadonlyMap<string, AnyTextMutationToolRegistration>,
|
|
2315
|
+
): void
|
|
2316
|
+
{
|
|
2317
|
+
for (const registration of mutationTools.values())
|
|
2318
|
+
{
|
|
2319
|
+
listener(registration);
|
|
2320
|
+
}
|
|
2321
|
+
}
|
|
2322
|
+
|
|
2323
|
+
function addMutationRegistration(
|
|
2324
|
+
registration: AnyTextMutationToolRegistration,
|
|
2325
|
+
mutationTools: Map<string, AnyTextMutationToolRegistration>,
|
|
2326
|
+
registeredTools: Set<TextEditorToolId>,
|
|
2327
|
+
listeners: Set<TextMutationToolListener>,
|
|
2328
|
+
registerMutationTool: ((registration: AnyTextMutationToolRegistration, core: TextEditorCore) => void) | undefined,
|
|
2329
|
+
core: TextEditorCore,
|
|
2330
|
+
): void
|
|
2331
|
+
{
|
|
2332
|
+
assertTextMutationToolRegistration(registration);
|
|
2333
|
+
|
|
2334
|
+
if (mutationTools.has(registration.name))
|
|
2335
|
+
{
|
|
2336
|
+
throw new Error(`Mutation tool ${registration.name} is already registered`);
|
|
2337
|
+
}
|
|
2338
|
+
|
|
2339
|
+
mutationTools.set(registration.name, registration);
|
|
2340
|
+
registeredTools.add(registration.name);
|
|
2341
|
+
|
|
2342
|
+
for (const listener of listeners)
|
|
2343
|
+
{
|
|
2344
|
+
listener(registration);
|
|
2345
|
+
}
|
|
2346
|
+
|
|
2347
|
+
registerMutationTool?.(registration, core);
|
|
2348
|
+
}
|