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