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,45 +1,48 @@
|
|
|
1
|
-
/**
|
|
1
|
+
/**
|
|
2
|
+
Runtime for intercepting streamed Pi tool calls.
|
|
3
|
+
*/
|
|
2
4
|
|
|
3
5
|
import { Allow } from "partial-json";
|
|
4
6
|
import {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
7
|
+
isToolCallAnnotation,
|
|
8
|
+
type ToolCallAnnotation,
|
|
9
|
+
withToolCallInterceptionDetails,
|
|
8
10
|
} from "pi-agent-tool-call-interception";
|
|
9
11
|
import { Guard } from "typebox-guard/guard";
|
|
10
12
|
|
|
11
13
|
import { resolvePartial } from "./partial-json-resolver.js";
|
|
12
14
|
import {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
15
|
+
type PartialToolRecoveryBatchEntry,
|
|
16
|
+
PartialToolRecoveryRegistry,
|
|
17
|
+
type PartialToolRecoveryResult,
|
|
16
18
|
} from "./partial-tool-recovery.js";
|
|
17
19
|
import { registerStreamingInterceptorProvider } from "./streaming-provider.js";
|
|
18
20
|
|
|
19
21
|
import type {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
22
|
+
AgentToolResult,
|
|
23
|
+
ExtensionAPI,
|
|
24
|
+
ExtensionContext,
|
|
25
|
+
MessageUpdateEvent,
|
|
24
26
|
} from "@earendil-works/pi-coding-agent";
|
|
25
27
|
|
|
26
28
|
export {
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
29
|
+
getToolCallInterception,
|
|
30
|
+
type ToolCallAnnotation,
|
|
31
|
+
type ToolCallInterceptionDetails,
|
|
30
32
|
} from "pi-agent-tool-call-interception";
|
|
31
33
|
|
|
32
34
|
type AssistantStreamEvent = MessageUpdateEvent["assistantMessageEvent"];
|
|
33
35
|
|
|
34
|
-
interface PendingRecoveryBlock
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
readonly ctx: InterceptorContext;
|
|
36
|
+
interface PendingRecoveryBlock {
|
|
37
|
+
readonly blocked: InterceptResult;
|
|
38
|
+
readonly ctx: InterceptorContext;
|
|
38
39
|
}
|
|
39
40
|
|
|
40
41
|
// ── Utility type ──────────────────────────────────────────────────────
|
|
41
42
|
|
|
42
|
-
/**
|
|
43
|
+
/**
|
|
44
|
+
Value or Promise of value — for sync/async interop.
|
|
45
|
+
*/
|
|
43
46
|
export type Awaitable<T> = T | Promise<T>;
|
|
44
47
|
|
|
45
48
|
export const PARTIAL_TOOL_RECOVERY_CUSTOM_TYPE = "agentic-ide-partial-tool-recovery";
|
|
@@ -51,1185 +54,1133 @@ export type ToolCallAnnotationSink = (toolCallId: string, annotation: ToolCallAn
|
|
|
51
54
|
const TOOL_CALL_ANNOTATION_EVENT = "pi-agent-text-editor/tool-call-annotation";
|
|
52
55
|
const toolCallAnnotationSinkUnsubscribers = new WeakMap<ExtensionAPI, () => void>();
|
|
53
56
|
|
|
54
|
-
interface ToolCallAnnotationEvent
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
readonly annotation: ToolCallAnnotation;
|
|
57
|
+
interface ToolCallAnnotationEvent {
|
|
58
|
+
readonly toolCallId: string;
|
|
59
|
+
readonly annotation: ToolCallAnnotation;
|
|
58
60
|
}
|
|
59
61
|
|
|
60
|
-
export interface BlockedToolCallFact
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
readonly nativeExecutionBlocked: boolean;
|
|
62
|
+
export interface BlockedToolCallFact {
|
|
63
|
+
readonly callId: string;
|
|
64
|
+
readonly toolName: string;
|
|
65
|
+
readonly arguments: Record<string, unknown>;
|
|
66
|
+
readonly contentIndex?: number;
|
|
67
|
+
readonly batchEntryIndex?: number;
|
|
68
|
+
readonly details?: unknown;
|
|
69
|
+
readonly result: AgentToolResult<unknown>;
|
|
70
|
+
readonly nativeExecutionBlocked: boolean;
|
|
70
71
|
}
|
|
71
72
|
|
|
72
73
|
export type BlockedToolCallSink = (blocked: BlockedToolCallFact) => void;
|
|
73
74
|
|
|
74
75
|
// ── Public types ──────────────────────────────────────────────────────────
|
|
75
76
|
|
|
76
|
-
export interface InterceptResult
|
|
77
|
-
{
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
annotation?: ToolCallAnnotation;
|
|
77
|
+
export interface InterceptResult {
|
|
78
|
+
message: {
|
|
79
|
+
customType: string;
|
|
80
|
+
content: string;
|
|
81
|
+
display: boolean;
|
|
82
|
+
details?: unknown;
|
|
83
|
+
};
|
|
84
|
+
annotation?: ToolCallAnnotation;
|
|
85
85
|
}
|
|
86
86
|
|
|
87
|
-
export interface GuardCallRecord
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
readonly blockExecution?: boolean;
|
|
87
|
+
export interface GuardCallRecord {
|
|
88
|
+
readonly originalArguments: Record<string, unknown>;
|
|
89
|
+
readonly guardResult?: AgentToolResult<unknown>;
|
|
90
|
+
readonly blockExecution?: boolean;
|
|
92
91
|
}
|
|
93
92
|
|
|
94
|
-
export interface GuardInterception
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
readonly result: AgentToolResult<unknown>;
|
|
93
|
+
export interface GuardInterception {
|
|
94
|
+
readonly toolCallId: string;
|
|
95
|
+
readonly arguments: Record<string, unknown>;
|
|
96
|
+
readonly result: AgentToolResult<unknown>;
|
|
99
97
|
}
|
|
100
98
|
|
|
101
|
-
export interface InterceptorContext
|
|
102
|
-
{
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
99
|
+
export interface InterceptorContext {
|
|
100
|
+
toolCall: {
|
|
101
|
+
id: string;
|
|
102
|
+
name: string;
|
|
103
|
+
arguments: Record<string, unknown>;
|
|
104
|
+
thoughtSignature?: string;
|
|
105
|
+
};
|
|
106
|
+
args: Record<string, unknown>;
|
|
107
|
+
/**
|
|
108
|
+
Stream content index, used to isolate interceptor state.
|
|
109
|
+
*/
|
|
110
|
+
/**
|
|
111
|
+
Position and calls within the current streamed tool-call batch.
|
|
112
|
+
*/
|
|
113
|
+
batchEntryIndex?: number;
|
|
114
|
+
batchEntries?: readonly PartialToolRecoveryBatchEntry[];
|
|
115
|
+
contentIndex?: number;
|
|
116
|
+
/**
|
|
117
|
+
Best-effort parsed arguments during streaming (set on toolcall_delta).
|
|
118
|
+
*/
|
|
119
|
+
partialArgs?: Record<string, unknown>;
|
|
120
|
+
cwd: string;
|
|
121
|
+
signal: AbortSignal | undefined;
|
|
122
|
+
/**
|
|
123
|
+
Extension context with UI methods (select, input, editor, confirm, etc.).
|
|
124
|
+
*/
|
|
125
|
+
ctx: ExtensionContext;
|
|
121
126
|
}
|
|
122
127
|
|
|
123
|
-
export interface ToolCallInterceptorHandler
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
128
|
+
export interface ToolCallInterceptorHandler {
|
|
129
|
+
/**
|
|
130
|
+
Unique handler name (for debug/removal).
|
|
131
|
+
*/
|
|
132
|
+
name: string;
|
|
133
|
+
toolNames: string[];
|
|
134
|
+
/**
|
|
135
|
+
Block Pi's native tool_call before execution. Existing cooperative executors may leave this disabled.
|
|
136
|
+
*/
|
|
137
|
+
blockExecution?: boolean;
|
|
138
|
+
/**
|
|
139
|
+
* Optional lifecycle hooks for handlers that keep per-content state.
|
|
140
|
+
*/
|
|
141
|
+
onContentEnd?(contentIndex: number): void;
|
|
142
|
+
onAgentEnd?(): void;
|
|
143
|
+
onAbort?(contentIndex: number): void;
|
|
144
|
+
/**
|
|
145
|
+
* Predicate. Called on each toolcall_start/delta/end for toolNames.
|
|
146
|
+
* Return InterceptResult — intercept (abort; return the result through tool_result).
|
|
147
|
+
* Return undefined — skip, next handler.
|
|
148
|
+
* Can be sync or async (returns Awaitable).
|
|
149
|
+
*/
|
|
150
|
+
intercept(context: InterceptorContext): Awaitable<InterceptResult | undefined>;
|
|
143
151
|
}
|
|
144
152
|
|
|
145
|
-
export interface ToolCallInterceptor
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
153
|
+
export interface ToolCallInterceptor {
|
|
154
|
+
register(handler: ToolCallInterceptorHandler): void;
|
|
155
|
+
unregister(name: string): void;
|
|
156
|
+
registerRecovery(registration: Parameters<PartialToolRecoveryRegistry["register"]>[0]): void;
|
|
157
|
+
unregisterRecovery(toolName: string): void;
|
|
158
|
+
registerBlockedCallSink(sink: BlockedToolCallSink | undefined): void;
|
|
159
|
+
processStreamEvent(
|
|
160
|
+
event: AssistantStreamEvent,
|
|
161
|
+
context: ExtensionContext,
|
|
162
|
+
): Promise<GuardInterception | undefined>;
|
|
163
|
+
markStreamEventProcessed(event: AssistantStreamEvent): void;
|
|
164
|
+
finalizeStreamInterception(contentIndex: number): void;
|
|
165
|
+
getToolCallRecord(toolCallId: string): GuardCallRecord | undefined;
|
|
156
166
|
}
|
|
157
167
|
|
|
158
168
|
// ── Implementation ────────────────────────────────────────────────────────
|
|
159
169
|
|
|
160
|
-
class InterceptorImpl implements ToolCallInterceptor
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
170
|
+
class InterceptorImpl implements ToolCallInterceptor {
|
|
171
|
+
/**
|
|
172
|
+
toolName → handler[]
|
|
173
|
+
*/
|
|
174
|
+
private readonly handlers = new Map<string, ToolCallInterceptorHandler[]>();
|
|
175
|
+
private readonly recoveryRegistry = new PartialToolRecoveryRegistry();
|
|
176
|
+
/**
|
|
177
|
+
All blocked calls collected during the current assistant batch.
|
|
178
|
+
*/
|
|
179
|
+
private readonly pendingRecoveryBlocks: PendingRecoveryBlock[] = [];
|
|
180
|
+
/**
|
|
181
|
+
Short reason for the guard that aborted the current assistant message.
|
|
182
|
+
*/
|
|
183
|
+
private pendingAbortReason: string | undefined;
|
|
184
|
+
/**
|
|
185
|
+
Source batch indexes that recovery actually applied.
|
|
186
|
+
*/
|
|
187
|
+
private readonly recoveryAppliedBatchIndexes = new Set<number>();
|
|
188
|
+
/**
|
|
189
|
+
Accumulated raw partial JSON arguments per contentIndex.
|
|
190
|
+
*/
|
|
191
|
+
private readonly partialBuffers = new Map<number, string>();
|
|
192
|
+
/**
|
|
193
|
+
Content items whose complete JSON arguments were already intercepted.
|
|
194
|
+
*/
|
|
195
|
+
private readonly completeArgumentContentIndices = new Set<number>();
|
|
196
|
+
/**
|
|
197
|
+
Content items whose complete JSON arguments were already intercepted.
|
|
198
|
+
*/
|
|
199
|
+
/**
|
|
200
|
+
Completed/partial calls in the current assistant batch.
|
|
201
|
+
*/
|
|
202
|
+
private readonly streamedBatchEntries = new Map<number, PartialToolRecoveryBatchEntry>();
|
|
203
|
+
/**
|
|
204
|
+
contentIndex → local position within the current edit batch.
|
|
205
|
+
*/
|
|
206
|
+
private readonly batchEntryIndexes = new Map<number, number>();
|
|
207
|
+
/**
|
|
208
|
+
toolCallId → local batch entry index, retained until tool_result arrives.
|
|
209
|
+
*/
|
|
210
|
+
private readonly toolCallBatchEntryIndexes = new Map<string, number>();
|
|
211
|
+
/**
|
|
212
|
+
Batch entries whose tool execution has already returned a result.
|
|
213
|
+
*/
|
|
214
|
+
private readonly completedBatchEntryIndexes = new Set<number>();
|
|
215
|
+
private nextBatchEntryIndex = 0;
|
|
216
|
+
/**
|
|
217
|
+
contentIndex → toolName (recorded on toolcall_start).
|
|
218
|
+
*/
|
|
219
|
+
private readonly contentNames = new Map<number, string>();
|
|
220
|
+
/**
|
|
221
|
+
contentIndex → renderer/tool call id.
|
|
222
|
+
*/
|
|
223
|
+
private readonly contentToolCallIds = new Map<number, string>();
|
|
224
|
+
/**
|
|
225
|
+
Annotations queued until a streamed content item exposes its id.
|
|
226
|
+
*/
|
|
227
|
+
private readonly pendingToolCallAnnotations = new Map<number, ToolCallAnnotation[]>();
|
|
228
|
+
private readonly emittedToolCallAnnotationKinds = new Map<
|
|
229
|
+
number,
|
|
230
|
+
Set<ToolCallAnnotation["kind"]>
|
|
231
|
+
>();
|
|
232
|
+
|
|
233
|
+
/**
|
|
234
|
+
contentIndex → tool was aborted in toolcall_delta (skip toolcall_end dispatch).
|
|
235
|
+
*/
|
|
236
|
+
private readonly abortedContentIndices = new Set<number>();
|
|
237
|
+
/**
|
|
238
|
+
Serialize asynchronous handler dispatch for each streamed content item.
|
|
239
|
+
*/
|
|
240
|
+
private readonly dispatchQueues = new Map<number, Promise<GuardInterception | undefined>>();
|
|
241
|
+
/**
|
|
242
|
+
Abort listeners installed for active content items.
|
|
243
|
+
*/
|
|
244
|
+
private readonly abortCleanups = new Map<number, () => void>();
|
|
245
|
+
/**
|
|
246
|
+
Stream events already dispatched by the provider wrapper.
|
|
247
|
+
*/
|
|
248
|
+
private readonly processedStreamEvents = new WeakSet<object>();
|
|
249
|
+
/**
|
|
250
|
+
Provider events may be cloned before message_update; track tool events by stable identity too.
|
|
251
|
+
*/
|
|
252
|
+
private readonly processedStreamEventKeys = new Map<string, number>();
|
|
253
|
+
/**
|
|
254
|
+
Original streamed arguments and the result returned by a guard.
|
|
255
|
+
*/
|
|
256
|
+
private readonly toolCallRecords = new Map<string, GuardCallRecord>();
|
|
257
|
+
private blockedCallSink: BlockedToolCallSink | undefined;
|
|
258
|
+
constructor(private readonly pi: ExtensionAPI) {
|
|
259
|
+
pi.on("tool_call", (event) => {
|
|
260
|
+
const record = this.toolCallRecords.get(event.toolCallId);
|
|
261
|
+
const guardResult = record?.blockExecution ? record.guardResult : undefined;
|
|
262
|
+
|
|
263
|
+
if (!guardResult) {
|
|
264
|
+
return;
|
|
265
|
+
}
|
|
254
266
|
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
267
|
+
const reason = guardResult.content
|
|
268
|
+
.filter((content) => content.type === "text")
|
|
269
|
+
.map((content) => content.text)
|
|
270
|
+
.join("\n");
|
|
259
271
|
|
|
260
|
-
|
|
261
|
-
|
|
272
|
+
return {
|
|
273
|
+
block: true,
|
|
274
|
+
reason: reason || `Tool call ${event.toolName} was blocked by a streaming interceptor.`,
|
|
275
|
+
};
|
|
276
|
+
});
|
|
277
|
+
pi.on("tool_result", (event) => {
|
|
278
|
+
const batchEntryIndex = this.toolCallBatchEntryIndexes.get(event.toolCallId);
|
|
262
279
|
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
this.annotateIncompleteCalls();
|
|
267
|
-
|
|
268
|
-
if (event.message.role !== "assistant")
|
|
269
|
-
{
|
|
270
|
-
return;
|
|
271
|
-
}
|
|
272
|
-
|
|
273
|
-
const reason = this.pendingAbortReason;
|
|
274
|
-
this.pendingAbortReason = undefined;
|
|
275
|
-
|
|
276
|
-
if (reason === undefined || event.message.stopReason !== "aborted")
|
|
277
|
-
{
|
|
278
|
-
return;
|
|
279
|
-
}
|
|
280
|
-
|
|
281
|
-
return {
|
|
282
|
-
message: {
|
|
283
|
-
...event.message,
|
|
284
|
-
errorMessage: reason,
|
|
285
|
-
},
|
|
286
|
-
};
|
|
287
|
-
});
|
|
280
|
+
if (batchEntryIndex !== undefined) {
|
|
281
|
+
this.completedBatchEntryIndexes.add(batchEntryIndex);
|
|
282
|
+
}
|
|
288
283
|
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
await this.finalizeRecovery();
|
|
292
|
-
|
|
293
|
-
for (const [contentIndex, entry] of this.streamedBatchEntries)
|
|
294
|
-
{
|
|
295
|
-
if (
|
|
296
|
-
!entry.complete
|
|
297
|
-
&& !this.recoveryAppliedBatchIndexes.has(entry.index)
|
|
298
|
-
&& !this.emittedToolCallAnnotationKinds.get(contentIndex)?.has("stale-anchor")
|
|
299
|
-
)
|
|
300
|
-
{
|
|
301
|
-
this.annotateContent(contentIndex, { kind: "aborted" });
|
|
302
|
-
}
|
|
303
|
-
}
|
|
304
|
-
|
|
305
|
-
this.cleanupAll();
|
|
306
|
-
});
|
|
307
|
-
}
|
|
284
|
+
const guardResult = this.toolCallRecords.get(event.toolCallId)?.guardResult;
|
|
285
|
+
this.toolCallRecords.delete(event.toolCallId);
|
|
308
286
|
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
{
|
|
317
|
-
const batchEntryIndex = this.getOrCreateBatchEntryIndex(event.contentIndex);
|
|
318
|
-
this.contentNames.set(event.contentIndex, block.name);
|
|
319
|
-
|
|
320
|
-
if (typeof block.id === "string")
|
|
321
|
-
{
|
|
322
|
-
this.bindToolCallId(event.contentIndex, block.id);
|
|
323
|
-
}
|
|
324
|
-
|
|
325
|
-
this.partialBuffers.set(event.contentIndex, "");
|
|
326
|
-
this.streamedBatchEntries.set(event.contentIndex, {
|
|
327
|
-
index: batchEntryIndex,
|
|
328
|
-
toolName: block.name,
|
|
329
|
-
args: {},
|
|
330
|
-
complete: false,
|
|
331
|
-
});
|
|
332
|
-
this.installAbortCleanup(event.contentIndex, ctx.signal);
|
|
333
|
-
}
|
|
334
|
-
|
|
335
|
-
return Promise.resolve(undefined as GuardInterception | undefined);
|
|
336
|
-
}
|
|
287
|
+
if (guardResult) {
|
|
288
|
+
return {
|
|
289
|
+
content: guardResult.content,
|
|
290
|
+
details: guardResult.details,
|
|
291
|
+
isError: true,
|
|
292
|
+
};
|
|
293
|
+
}
|
|
337
294
|
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
const toolName = this.contentNames.get(event.contentIndex);
|
|
343
|
-
const toolCallId = this.contentToolCallIds.get(event.contentIndex);
|
|
344
|
-
|
|
345
|
-
if (!toolName)
|
|
346
|
-
{
|
|
347
|
-
return;
|
|
348
|
-
}
|
|
349
|
-
|
|
350
|
-
const buffer = (this.partialBuffers.get(event.contentIndex) ?? "") + event.delta;
|
|
351
|
-
this.partialBuffers.set(event.contentIndex, buffer);
|
|
352
|
-
this.installAbortCleanup(event.contentIndex, ctx.signal);
|
|
353
|
-
const partialArgs = resolvePartial(buffer, Allow.OBJ | Allow.ARR) ?? {};
|
|
354
|
-
const completeArgs = parseCompleteArguments(buffer);
|
|
355
|
-
const batchEntryIndex = this.getOrCreateBatchEntryIndex(event.contentIndex);
|
|
356
|
-
this.streamedBatchEntries.set(event.contentIndex, {
|
|
357
|
-
index: batchEntryIndex,
|
|
358
|
-
toolName,
|
|
359
|
-
args: partialArgs,
|
|
360
|
-
complete: completeArgs !== undefined,
|
|
361
|
-
});
|
|
362
|
-
|
|
363
|
-
const handlers = this.handlers.get(toolName);
|
|
364
|
-
|
|
365
|
-
if (
|
|
366
|
-
!toolCallId || !handlers || handlers.length === 0
|
|
367
|
-
|| (!completeArgs && Object.keys(partialArgs).length === 0)
|
|
368
|
-
)
|
|
369
|
-
{
|
|
370
|
-
return;
|
|
371
|
-
}
|
|
372
|
-
|
|
373
|
-
const hasCompleteArgs = completeArgs !== undefined;
|
|
374
|
-
|
|
375
|
-
if (hasCompleteArgs && this.completeArgumentContentIndices.has(event.contentIndex))
|
|
376
|
-
{
|
|
377
|
-
return;
|
|
378
|
-
}
|
|
379
|
-
|
|
380
|
-
if (hasCompleteArgs)
|
|
381
|
-
{
|
|
382
|
-
this.completeArgumentContentIndices.add(event.contentIndex);
|
|
383
|
-
}
|
|
384
|
-
|
|
385
|
-
const args = completeArgs ?? partialArgs;
|
|
386
|
-
const interception = await this.dispatchHandlers(handlers, {
|
|
387
|
-
toolCall: { id: toolCallId, name: toolName, arguments: args },
|
|
388
|
-
args,
|
|
389
|
-
...(hasCompleteArgs ? {} : { partialArgs }),
|
|
390
|
-
batchEntryIndex,
|
|
391
|
-
batchEntries: this.getBatchEntries(batchEntryIndex),
|
|
392
|
-
contentIndex: event.contentIndex,
|
|
393
|
-
cwd: ctx.cwd,
|
|
394
|
-
signal: ctx.signal,
|
|
395
|
-
ctx,
|
|
396
|
-
});
|
|
397
|
-
|
|
398
|
-
if (interception)
|
|
399
|
-
{
|
|
400
|
-
this.rememberOriginalArguments(interception.toolCallId, interception.arguments);
|
|
401
|
-
}
|
|
402
|
-
|
|
403
|
-
return interception;
|
|
404
|
-
});
|
|
405
|
-
}
|
|
295
|
+
return;
|
|
296
|
+
});
|
|
297
|
+
pi.on("message_update", async (event, context) => {
|
|
298
|
+
const streamEvent = event.assistantMessageEvent;
|
|
406
299
|
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
{
|
|
411
|
-
const toolName = event.toolCall.name;
|
|
412
|
-
const handlers = this.handlers.get(toolName);
|
|
413
|
-
const batchEntryIndex = this.getOrCreateBatchEntryIndex(event.contentIndex);
|
|
414
|
-
this.bindToolCallId(event.contentIndex, event.toolCall.id);
|
|
415
|
-
const previous = this.toolCallRecords.get(event.toolCall.id);
|
|
416
|
-
this.toolCallRecords.set(event.toolCall.id, {
|
|
417
|
-
originalArguments: structuredClone(event.toolCall.arguments),
|
|
418
|
-
...(previous?.guardResult ? { guardResult: previous.guardResult } : {}),
|
|
419
|
-
...(previous?.blockExecution ? { blockExecution: true } : {}),
|
|
420
|
-
});
|
|
421
|
-
this.streamedBatchEntries.set(event.contentIndex, {
|
|
422
|
-
index: batchEntryIndex,
|
|
423
|
-
toolName,
|
|
424
|
-
args: event.toolCall.arguments,
|
|
425
|
-
complete: false,
|
|
426
|
-
});
|
|
427
|
-
|
|
428
|
-
const shouldIntercept = !this.completeArgumentContentIndices.has(event.contentIndex);
|
|
429
|
-
let interception: GuardInterception | undefined;
|
|
430
|
-
|
|
431
|
-
if (shouldIntercept)
|
|
432
|
-
{
|
|
433
|
-
this.completeArgumentContentIndices.add(event.contentIndex);
|
|
434
|
-
interception = handlers && handlers.length > 0
|
|
435
|
-
? await this.dispatchHandlers(handlers, {
|
|
436
|
-
toolCall: event.toolCall,
|
|
437
|
-
args: event.toolCall.arguments,
|
|
438
|
-
batchEntryIndex,
|
|
439
|
-
batchEntries: this.getBatchEntries(batchEntryIndex),
|
|
440
|
-
contentIndex: event.contentIndex,
|
|
441
|
-
cwd: ctx.cwd,
|
|
442
|
-
signal: ctx.signal,
|
|
443
|
-
ctx,
|
|
444
|
-
})
|
|
445
|
-
: undefined;
|
|
446
|
-
}
|
|
447
|
-
|
|
448
|
-
const batchEntry = this.streamedBatchEntries.get(event.contentIndex);
|
|
449
|
-
|
|
450
|
-
if (batchEntry)
|
|
451
|
-
{
|
|
452
|
-
this.streamedBatchEntries.set(event.contentIndex, { ...batchEntry, complete: true });
|
|
453
|
-
}
|
|
454
|
-
|
|
455
|
-
this.cleanupContent(event.contentIndex, toolName);
|
|
456
|
-
return interception;
|
|
457
|
-
});
|
|
458
|
-
}
|
|
300
|
+
if (this.consumeProcessedStreamEvent(streamEvent)) {
|
|
301
|
+
return;
|
|
302
|
+
}
|
|
459
303
|
|
|
460
|
-
|
|
461
|
-
}
|
|
462
|
-
markStreamEventProcessed(event: AssistantStreamEvent): void
|
|
463
|
-
{
|
|
464
|
-
this.processedStreamEvents.add(event);
|
|
465
|
-
const key = toolStreamEventKey(event);
|
|
466
|
-
|
|
467
|
-
if (key !== undefined)
|
|
468
|
-
{
|
|
469
|
-
this.processedStreamEventKeys.set(key, (this.processedStreamEventKeys.get(key) ?? 0) + 1);
|
|
470
|
-
}
|
|
471
|
-
}
|
|
304
|
+
await this.processStreamEvent(streamEvent, context);
|
|
305
|
+
});
|
|
472
306
|
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
const key = toolStreamEventKey(event);
|
|
307
|
+
pi.on("message_end", async (event) => {
|
|
308
|
+
await this.finalizeRecovery();
|
|
309
|
+
this.annotateIncompleteCalls();
|
|
477
310
|
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
}
|
|
311
|
+
if (event.message.role !== "assistant") {
|
|
312
|
+
return;
|
|
313
|
+
}
|
|
482
314
|
|
|
483
|
-
|
|
315
|
+
const reason = this.pendingAbortReason;
|
|
316
|
+
this.pendingAbortReason = undefined;
|
|
484
317
|
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
}
|
|
318
|
+
if (reason === undefined || event.message.stopReason !== "aborted") {
|
|
319
|
+
return;
|
|
320
|
+
}
|
|
489
321
|
|
|
490
|
-
|
|
491
|
-
{
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
322
|
+
return {
|
|
323
|
+
message: {
|
|
324
|
+
...event.message,
|
|
325
|
+
errorMessage: reason,
|
|
326
|
+
},
|
|
327
|
+
};
|
|
328
|
+
});
|
|
329
|
+
|
|
330
|
+
pi.on("agent_end", async () => {
|
|
331
|
+
await this.finalizeRecovery();
|
|
332
|
+
|
|
333
|
+
for (const [contentIndex, entry] of this.streamedBatchEntries) {
|
|
334
|
+
if (
|
|
335
|
+
!entry.complete &&
|
|
336
|
+
!this.recoveryAppliedBatchIndexes.has(entry.index) &&
|
|
337
|
+
!this.emittedToolCallAnnotationKinds.get(contentIndex)?.has("stale-anchor")
|
|
338
|
+
) {
|
|
339
|
+
this.annotateContent(contentIndex, { kind: "aborted" });
|
|
497
340
|
}
|
|
341
|
+
}
|
|
498
342
|
|
|
499
|
-
|
|
500
|
-
}
|
|
343
|
+
this.cleanupAll();
|
|
344
|
+
});
|
|
345
|
+
}
|
|
501
346
|
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
347
|
+
processStreamEvent(
|
|
348
|
+
event: AssistantStreamEvent,
|
|
349
|
+
context: ExtensionContext,
|
|
350
|
+
): Promise<GuardInterception | undefined> {
|
|
351
|
+
if (event.type === "toolcall_start") {
|
|
352
|
+
const block = event.partial.content[event.contentIndex];
|
|
506
353
|
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
}
|
|
354
|
+
if (block?.type === "toolCall") {
|
|
355
|
+
const batchEntryIndex = this.getOrCreateBatchEntryIndex(event.contentIndex);
|
|
356
|
+
this.contentNames.set(event.contentIndex, block.name);
|
|
511
357
|
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
const record = this.toolCallRecords.get(toolCallId);
|
|
515
|
-
|
|
516
|
-
if (record)
|
|
517
|
-
{
|
|
518
|
-
this.toolCallRecords.set(toolCallId, {
|
|
519
|
-
originalArguments: structuredClone(originalArguments),
|
|
520
|
-
...(record.guardResult ? { guardResult: record.guardResult } : {}),
|
|
521
|
-
...(record.blockExecution ? { blockExecution: true } : {}),
|
|
522
|
-
});
|
|
358
|
+
if (typeof block.id === "string") {
|
|
359
|
+
this.bindToolCallId(event.contentIndex, block.id);
|
|
523
360
|
}
|
|
524
|
-
}
|
|
525
361
|
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
if (result !== undefined)
|
|
536
|
-
{
|
|
537
|
-
this.pendingRecoveryBlocks.push({ blocked: result, ctx });
|
|
538
|
-
const guardResult: AgentToolResult<unknown> = {
|
|
539
|
-
content: [{ type: "text", text: result.message.content }],
|
|
540
|
-
details: result.message.details ?? null,
|
|
541
|
-
};
|
|
542
|
-
this.blockedCallSink?.({
|
|
543
|
-
callId: ctx.toolCall.id,
|
|
544
|
-
toolName: ctx.toolCall.name,
|
|
545
|
-
arguments: structuredClone(ctx.args),
|
|
546
|
-
...(ctx.contentIndex === undefined ? {} : { contentIndex: ctx.contentIndex }),
|
|
547
|
-
...(ctx.batchEntryIndex === undefined ? {} : { batchEntryIndex: ctx.batchEntryIndex }),
|
|
548
|
-
...(result.message.details === undefined ? {} : { details: result.message.details }),
|
|
549
|
-
result: guardResult,
|
|
550
|
-
nativeExecutionBlocked: handler.blockExecution === true,
|
|
551
|
-
});
|
|
552
|
-
return {
|
|
553
|
-
toolCallId: ctx.toolCall.id,
|
|
554
|
-
arguments: structuredClone(ctx.args),
|
|
555
|
-
result: this.abortToolCall(result, ctx, handler.name, handler.blockExecution === true),
|
|
556
|
-
};
|
|
557
|
-
}
|
|
558
|
-
}
|
|
362
|
+
this.partialBuffers.set(event.contentIndex, "");
|
|
363
|
+
this.streamedBatchEntries.set(event.contentIndex, {
|
|
364
|
+
index: batchEntryIndex,
|
|
365
|
+
toolName: block.name,
|
|
366
|
+
args: {},
|
|
367
|
+
complete: false,
|
|
368
|
+
});
|
|
369
|
+
this.installAbortCleanup(event.contentIndex, context.signal);
|
|
370
|
+
}
|
|
559
371
|
|
|
560
|
-
|
|
372
|
+
return Promise.resolve(undefined as GuardInterception | undefined);
|
|
561
373
|
}
|
|
562
|
-
|
|
563
|
-
{
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
)
|
|
571
|
-
{
|
|
572
|
-
this.annotateContent(contentIndex, { kind: "aborted" });
|
|
573
|
-
}
|
|
374
|
+
|
|
375
|
+
if (event.type === "toolcall_delta") {
|
|
376
|
+
return this.enqueue(event.contentIndex, async () => {
|
|
377
|
+
const toolName = this.contentNames.get(event.contentIndex);
|
|
378
|
+
const toolCallId = this.contentToolCallIds.get(event.contentIndex);
|
|
379
|
+
|
|
380
|
+
if (!toolName) {
|
|
381
|
+
return;
|
|
574
382
|
}
|
|
575
|
-
}
|
|
576
383
|
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
this.
|
|
580
|
-
const
|
|
384
|
+
const buffer = (this.partialBuffers.get(event.contentIndex) ?? "") + event.delta;
|
|
385
|
+
this.partialBuffers.set(event.contentIndex, buffer);
|
|
386
|
+
this.installAbortCleanup(event.contentIndex, context.signal);
|
|
387
|
+
const partialArguments = resolvePartial(buffer, Allow.OBJ | Allow.ARR) ?? {};
|
|
388
|
+
const completeArguments = parseCompleteArguments(buffer);
|
|
389
|
+
const batchEntryIndex = this.getOrCreateBatchEntryIndex(event.contentIndex);
|
|
390
|
+
this.streamedBatchEntries.set(event.contentIndex, {
|
|
391
|
+
index: batchEntryIndex,
|
|
392
|
+
toolName,
|
|
393
|
+
args: partialArguments,
|
|
394
|
+
complete: completeArguments !== undefined,
|
|
395
|
+
});
|
|
396
|
+
|
|
397
|
+
const handlers = this.handlers.get(toolName);
|
|
581
398
|
|
|
582
|
-
if (
|
|
583
|
-
|
|
584
|
-
|
|
399
|
+
if (
|
|
400
|
+
!toolCallId ||
|
|
401
|
+
!handlers ||
|
|
402
|
+
handlers.length === 0 ||
|
|
403
|
+
(!completeArguments && Object.keys(partialArguments).length === 0)
|
|
404
|
+
) {
|
|
405
|
+
return;
|
|
585
406
|
}
|
|
586
407
|
|
|
587
|
-
const
|
|
408
|
+
const hasCompleteArguments = completeArguments !== undefined;
|
|
409
|
+
|
|
410
|
+
if (hasCompleteArguments && this.completeArgumentContentIndices.has(event.contentIndex)) {
|
|
411
|
+
return;
|
|
412
|
+
}
|
|
588
413
|
|
|
589
|
-
if (
|
|
590
|
-
|
|
591
|
-
return;
|
|
414
|
+
if (hasCompleteArguments) {
|
|
415
|
+
this.completeArgumentContentIndices.add(event.contentIndex);
|
|
592
416
|
}
|
|
593
417
|
|
|
594
|
-
|
|
595
|
-
{
|
|
596
|
-
|
|
418
|
+
const arguments_ = completeArguments ?? partialArguments;
|
|
419
|
+
const interception = await this.dispatchHandlers(handlers, {
|
|
420
|
+
toolCall: { id: toolCallId, name: toolName, arguments: arguments_ },
|
|
421
|
+
args: arguments_,
|
|
422
|
+
...(!hasCompleteArguments && { partialArgs: partialArguments }),
|
|
423
|
+
batchEntryIndex,
|
|
424
|
+
batchEntries: this.getBatchEntries(batchEntryIndex),
|
|
425
|
+
contentIndex: event.contentIndex,
|
|
426
|
+
cwd: context.cwd,
|
|
427
|
+
signal: context.signal,
|
|
428
|
+
ctx: context,
|
|
429
|
+
});
|
|
430
|
+
|
|
431
|
+
if (interception) {
|
|
432
|
+
this.rememberOriginalArguments(interception.toolCallId, interception.arguments);
|
|
597
433
|
}
|
|
598
434
|
|
|
599
|
-
|
|
435
|
+
return interception;
|
|
436
|
+
});
|
|
600
437
|
}
|
|
601
438
|
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
const
|
|
439
|
+
if (event.type === "toolcall_end") {
|
|
440
|
+
return this.enqueue(event.contentIndex, async () => {
|
|
441
|
+
const toolName = event.toolCall.name;
|
|
442
|
+
const handlers = this.handlers.get(toolName);
|
|
443
|
+
const batchEntryIndex = this.getOrCreateBatchEntryIndex(event.contentIndex);
|
|
444
|
+
this.bindToolCallId(event.contentIndex, event.toolCall.id);
|
|
445
|
+
const previous = this.toolCallRecords.get(event.toolCall.id);
|
|
446
|
+
this.toolCallRecords.set(event.toolCall.id, {
|
|
447
|
+
originalArguments: structuredClone(event.toolCall.arguments),
|
|
448
|
+
...(previous?.guardResult && { guardResult: previous.guardResult }),
|
|
449
|
+
...(previous?.blockExecution && { blockExecution: true }),
|
|
450
|
+
});
|
|
451
|
+
this.streamedBatchEntries.set(event.contentIndex, {
|
|
452
|
+
index: batchEntryIndex,
|
|
453
|
+
toolName,
|
|
454
|
+
args: event.toolCall.arguments,
|
|
455
|
+
complete: false,
|
|
456
|
+
});
|
|
605
457
|
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
458
|
+
const shouldIntercept = !this.completeArgumentContentIndices.has(event.contentIndex);
|
|
459
|
+
let interception: GuardInterception | undefined;
|
|
460
|
+
|
|
461
|
+
if (shouldIntercept) {
|
|
462
|
+
this.completeArgumentContentIndices.add(event.contentIndex);
|
|
463
|
+
interception =
|
|
464
|
+
handlers && handlers.length > 0
|
|
465
|
+
? await this.dispatchHandlers(handlers, {
|
|
466
|
+
toolCall: event.toolCall,
|
|
467
|
+
args: event.toolCall.arguments,
|
|
468
|
+
batchEntryIndex,
|
|
469
|
+
batchEntries: this.getBatchEntries(batchEntryIndex),
|
|
470
|
+
contentIndex: event.contentIndex,
|
|
471
|
+
cwd: context.cwd,
|
|
472
|
+
signal: context.signal,
|
|
473
|
+
ctx: context,
|
|
474
|
+
})
|
|
475
|
+
: undefined;
|
|
609
476
|
}
|
|
610
477
|
|
|
611
|
-
|
|
612
|
-
this.emittedToolCallAnnotationKinds.set(contentIndex, emitted);
|
|
613
|
-
|
|
614
|
-
const toolCallId = this.contentToolCallIds.get(contentIndex);
|
|
478
|
+
const batchEntry = this.streamedBatchEntries.get(event.contentIndex);
|
|
615
479
|
|
|
616
|
-
if (
|
|
617
|
-
|
|
618
|
-
this.emitToolCallAnnotation(toolCallId, annotation);
|
|
619
|
-
return;
|
|
480
|
+
if (batchEntry) {
|
|
481
|
+
this.streamedBatchEntries.set(event.contentIndex, { ...batchEntry, complete: true });
|
|
620
482
|
}
|
|
621
483
|
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
484
|
+
this.cleanupContent(event.contentIndex, toolName);
|
|
485
|
+
return interception;
|
|
486
|
+
});
|
|
625
487
|
}
|
|
626
488
|
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
489
|
+
return Promise.resolve(undefined as GuardInterception | undefined);
|
|
490
|
+
}
|
|
491
|
+
markStreamEventProcessed(event: AssistantStreamEvent): void {
|
|
492
|
+
this.processedStreamEvents.add(event);
|
|
493
|
+
const key = toolStreamEventKey(event);
|
|
494
|
+
|
|
495
|
+
if (key !== undefined) {
|
|
496
|
+
this.processedStreamEventKeys.set(key, (this.processedStreamEventKeys.get(key) ?? 0) + 1);
|
|
631
497
|
}
|
|
498
|
+
}
|
|
632
499
|
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
500
|
+
private consumeProcessedStreamEvent(event: AssistantStreamEvent): boolean {
|
|
501
|
+
const isSameEvent = this.processedStreamEvents.delete(event);
|
|
502
|
+
const key = toolStreamEventKey(event);
|
|
636
503
|
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
504
|
+
if (key === undefined) {
|
|
505
|
+
return isSameEvent;
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
const count = this.processedStreamEventKeys.get(key) ?? 0;
|
|
641
509
|
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
this.batchEntryIndexes.set(contentIndex, created);
|
|
645
|
-
return created;
|
|
510
|
+
if (count === 0) {
|
|
511
|
+
return isSameEvent;
|
|
646
512
|
}
|
|
647
513
|
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
.sort((left, right) => left.index - right.index);
|
|
514
|
+
if (count === 1) {
|
|
515
|
+
this.processedStreamEventKeys.delete(key);
|
|
516
|
+
} else {
|
|
517
|
+
this.processedStreamEventKeys.set(key, count - 1);
|
|
653
518
|
}
|
|
654
519
|
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
520
|
+
return true;
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
finalizeStreamInterception(contentIndex: number): void {
|
|
524
|
+
this.cleanupTransientContent(contentIndex, undefined, true);
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
getToolCallRecord(toolCallId: string): GuardCallRecord | undefined {
|
|
528
|
+
return this.toolCallRecords.get(toolCallId);
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
private rememberOriginalArguments(
|
|
532
|
+
toolCallId: string,
|
|
533
|
+
originalArguments: Record<string, unknown>,
|
|
534
|
+
): void {
|
|
535
|
+
const record = this.toolCallRecords.get(toolCallId);
|
|
536
|
+
|
|
537
|
+
if (record) {
|
|
538
|
+
this.toolCallRecords.set(toolCallId, {
|
|
539
|
+
originalArguments: structuredClone(originalArguments),
|
|
540
|
+
...(record.guardResult && { guardResult: record.guardResult }),
|
|
541
|
+
...(record.blockExecution && { blockExecution: true }),
|
|
542
|
+
});
|
|
659
543
|
}
|
|
544
|
+
}
|
|
660
545
|
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
{
|
|
668
|
-
const reason = abortReason(result, handlerName);
|
|
669
|
-
const annotation: ToolCallAnnotation = result.annotation
|
|
670
|
-
? result.annotation.kind === "aborted" && result.annotation.label === undefined
|
|
671
|
-
? { ...result.annotation, label: reason }
|
|
672
|
-
: result.annotation
|
|
673
|
-
: (result.message.customType === "text-editor-stale-anchor-block"
|
|
674
|
-
? { kind: "stale-anchor", label: reason }
|
|
675
|
-
: { kind: "aborted", label: reason, color: "error" });
|
|
676
|
-
|
|
677
|
-
if (ctx.contentIndex !== undefined)
|
|
678
|
-
{
|
|
679
|
-
this.abortedContentIndices.add(ctx.contentIndex);
|
|
680
|
-
this.annotateContent(ctx.contentIndex, annotation);
|
|
681
|
-
}
|
|
546
|
+
private async dispatchHandlers(
|
|
547
|
+
handlers: readonly ToolCallInterceptorHandler[],
|
|
548
|
+
context: InterceptorContext,
|
|
549
|
+
): Promise<GuardInterception | undefined> {
|
|
550
|
+
for (const handler of handlers) {
|
|
551
|
+
const result = await handler.intercept(context);
|
|
682
552
|
|
|
553
|
+
if (result !== undefined) {
|
|
554
|
+
this.pendingRecoveryBlocks.push({ blocked: result, ctx: context });
|
|
683
555
|
const guardResult: AgentToolResult<unknown> = {
|
|
684
|
-
|
|
685
|
-
|
|
556
|
+
content: [{ type: "text", text: result.message.content }],
|
|
557
|
+
details: result.message.details ?? null,
|
|
686
558
|
};
|
|
687
|
-
this.
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
559
|
+
this.blockedCallSink?.({
|
|
560
|
+
callId: context.toolCall.id,
|
|
561
|
+
toolName: context.toolCall.name,
|
|
562
|
+
arguments: structuredClone(context.args),
|
|
563
|
+
...(context.contentIndex !== undefined && { contentIndex: context.contentIndex }),
|
|
564
|
+
...(context.batchEntryIndex !== undefined && {
|
|
565
|
+
batchEntryIndex: context.batchEntryIndex,
|
|
566
|
+
}),
|
|
567
|
+
...(result.message.details !== undefined && { details: result.message.details }),
|
|
568
|
+
result: guardResult,
|
|
569
|
+
nativeExecutionBlocked: handler.blockExecution === true,
|
|
691
570
|
});
|
|
692
|
-
return
|
|
571
|
+
return {
|
|
572
|
+
toolCallId: context.toolCall.id,
|
|
573
|
+
arguments: structuredClone(context.args),
|
|
574
|
+
result: this.abortToolCall(
|
|
575
|
+
result,
|
|
576
|
+
context,
|
|
577
|
+
handler.name,
|
|
578
|
+
handler.blockExecution === true,
|
|
579
|
+
),
|
|
580
|
+
};
|
|
581
|
+
}
|
|
693
582
|
}
|
|
694
583
|
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
584
|
+
return undefined;
|
|
585
|
+
}
|
|
586
|
+
private annotateIncompleteCalls(): void {
|
|
587
|
+
for (const [contentIndex, entry] of this.streamedBatchEntries) {
|
|
588
|
+
if (
|
|
589
|
+
!entry.complete &&
|
|
590
|
+
!this.recoveryAppliedBatchIndexes.has(entry.index) &&
|
|
591
|
+
!this.emittedToolCallAnnotationKinds.get(contentIndex)?.has("stale-anchor")
|
|
592
|
+
) {
|
|
593
|
+
this.annotateContent(contentIndex, { kind: "aborted" });
|
|
594
|
+
}
|
|
595
|
+
}
|
|
596
|
+
}
|
|
705
597
|
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
598
|
+
private bindToolCallId(contentIndex: number, toolCallId: string): void {
|
|
599
|
+
this.contentToolCallIds.set(contentIndex, toolCallId);
|
|
600
|
+
const batchEntryIndex = this.batchEntryIndexes.get(contentIndex);
|
|
709
601
|
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
}
|
|
602
|
+
if (batchEntryIndex !== undefined) {
|
|
603
|
+
this.toolCallBatchEntryIndexes.set(toolCallId, batchEntryIndex);
|
|
604
|
+
}
|
|
714
605
|
|
|
715
|
-
|
|
716
|
-
group.push(block);
|
|
717
|
-
groups.set(recoveryKey, group);
|
|
718
|
-
}
|
|
606
|
+
const pending = this.pendingToolCallAnnotations.get(contentIndex);
|
|
719
607
|
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
const first = group[0];
|
|
723
|
-
|
|
724
|
-
if (!first)
|
|
725
|
-
{
|
|
726
|
-
continue;
|
|
727
|
-
}
|
|
728
|
-
|
|
729
|
-
const finalResult = await this.recoverIfNeeded(
|
|
730
|
-
first.blocked,
|
|
731
|
-
first.ctx,
|
|
732
|
-
this.getAllBatchEntries(),
|
|
733
|
-
group,
|
|
734
|
-
);
|
|
735
|
-
|
|
736
|
-
if (finalResult === first.blocked)
|
|
737
|
-
{
|
|
738
|
-
continue;
|
|
739
|
-
}
|
|
740
|
-
|
|
741
|
-
const details = finalResult.message.details;
|
|
742
|
-
const recovery = Guard.IsObjectNotArray(details) && Guard.IsObjectNotArray(details.recovery)
|
|
743
|
-
? details.recovery
|
|
744
|
-
: undefined;
|
|
745
|
-
const appliedEntries = recovery?.appliedEntries;
|
|
746
|
-
|
|
747
|
-
if (Array.isArray(appliedEntries))
|
|
748
|
-
{
|
|
749
|
-
for (const index of appliedEntries)
|
|
750
|
-
{
|
|
751
|
-
if (typeof index === "number")
|
|
752
|
-
{
|
|
753
|
-
this.recoveryAppliedBatchIndexes.add(index);
|
|
754
|
-
}
|
|
755
|
-
}
|
|
756
|
-
}
|
|
757
|
-
}
|
|
608
|
+
if (!pending) {
|
|
609
|
+
return;
|
|
758
610
|
}
|
|
759
611
|
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
finalBatchEntries: readonly PartialToolRecoveryBatchEntry[],
|
|
764
|
-
blocks: readonly PendingRecoveryBlock[],
|
|
765
|
-
): Promise<InterceptResult>
|
|
766
|
-
{
|
|
767
|
-
if (ctx.contentIndex === undefined)
|
|
768
|
-
{
|
|
769
|
-
return result;
|
|
770
|
-
}
|
|
612
|
+
for (const annotation of pending) {
|
|
613
|
+
this.emitToolCallAnnotation(toolCallId, annotation);
|
|
614
|
+
}
|
|
771
615
|
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
? details.entryIndex
|
|
775
|
-
: undefined;
|
|
776
|
-
const blockedEntryIndexes = [
|
|
777
|
-
...new Set(
|
|
778
|
-
blocks
|
|
779
|
-
.map((block) => block.ctx.batchEntryIndex)
|
|
780
|
-
.filter((index): index is number => index !== undefined),
|
|
781
|
-
),
|
|
782
|
-
];
|
|
783
|
-
const blockedToolCallIds = [...new Set(blocks.map((block) => block.ctx.toolCall.id))];
|
|
784
|
-
const blockedDetails = blocks.map((block) => block.blocked.message.details);
|
|
785
|
-
const first = blocks[0];
|
|
786
|
-
const recoveryKey = first
|
|
787
|
-
? this.recoveryRegistry.recoveryKeyFor(first.ctx.toolCall.name)
|
|
788
|
-
: undefined;
|
|
789
|
-
|
|
790
|
-
if (!recoveryKey)
|
|
791
|
-
{
|
|
792
|
-
return result;
|
|
793
|
-
}
|
|
616
|
+
this.pendingToolCallAnnotations.delete(contentIndex);
|
|
617
|
+
}
|
|
794
618
|
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
...(staleEntryIndex === undefined ? {} : { staleEntryIndex }),
|
|
800
|
-
staleDetails: details,
|
|
801
|
-
...(ctx.partialArgs ? { partialArgs: ctx.partialArgs } : {}),
|
|
802
|
-
args: ctx.args,
|
|
803
|
-
batchEntries: finalBatchEntries,
|
|
804
|
-
...(ctx.batchEntryIndex === undefined ? {} : { batchEntryIndex: ctx.batchEntryIndex }),
|
|
805
|
-
recoverCompleteBatch: true,
|
|
806
|
-
completedEntryIndexes: [...this.completedBatchEntryIndexes],
|
|
807
|
-
blockedEntryIndexes,
|
|
808
|
-
blockedToolCallIds,
|
|
809
|
-
blockedDetails,
|
|
810
|
-
recoveryKey,
|
|
811
|
-
cwd: ctx.cwd,
|
|
812
|
-
ctx: ctx.ctx,
|
|
813
|
-
});
|
|
619
|
+
private annotateContent(contentIndex: number, annotation: ToolCallAnnotation): void {
|
|
620
|
+
const emitted =
|
|
621
|
+
this.emittedToolCallAnnotationKinds.get(contentIndex) ??
|
|
622
|
+
new Set<ToolCallAnnotation["kind"]>();
|
|
814
623
|
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
: result;
|
|
624
|
+
if (emitted.has(annotation.kind)) {
|
|
625
|
+
return;
|
|
818
626
|
}
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
try
|
|
830
|
-
{
|
|
831
|
-
return await current;
|
|
832
|
-
}
|
|
833
|
-
finally
|
|
834
|
-
{
|
|
835
|
-
if (this.dispatchQueues.get(contentIndex) === current)
|
|
836
|
-
{
|
|
837
|
-
this.dispatchQueues.delete(contentIndex);
|
|
838
|
-
}
|
|
839
|
-
}
|
|
627
|
+
|
|
628
|
+
emitted.add(annotation.kind);
|
|
629
|
+
this.emittedToolCallAnnotationKinds.set(contentIndex, emitted);
|
|
630
|
+
|
|
631
|
+
const toolCallId = this.contentToolCallIds.get(contentIndex);
|
|
632
|
+
|
|
633
|
+
if (toolCallId) {
|
|
634
|
+
this.emitToolCallAnnotation(toolCallId, annotation);
|
|
635
|
+
return;
|
|
840
636
|
}
|
|
841
637
|
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
return;
|
|
847
|
-
}
|
|
638
|
+
const pending = this.pendingToolCallAnnotations.get(contentIndex) ?? [];
|
|
639
|
+
pending.push(annotation);
|
|
640
|
+
this.pendingToolCallAnnotations.set(contentIndex, pending);
|
|
641
|
+
}
|
|
848
642
|
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
643
|
+
private emitToolCallAnnotation(toolCallId: string, annotation: ToolCallAnnotation): void {
|
|
644
|
+
const event = { toolCallId, annotation } satisfies ToolCallAnnotationEvent;
|
|
645
|
+
this.pi.events.emit(TOOL_CALL_ANNOTATION_EVENT, event);
|
|
646
|
+
}
|
|
853
647
|
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
handler.onAbort?.(contentIndex);
|
|
857
|
-
}
|
|
858
|
-
};
|
|
648
|
+
private getOrCreateBatchEntryIndex(contentIndex: number): number {
|
|
649
|
+
const existing = this.batchEntryIndexes.get(contentIndex);
|
|
859
650
|
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
{
|
|
863
|
-
signal.removeEventListener("abort", listener);
|
|
864
|
-
});
|
|
651
|
+
if (existing !== undefined) {
|
|
652
|
+
return existing;
|
|
865
653
|
}
|
|
866
654
|
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
655
|
+
const created = this.nextBatchEntryIndex;
|
|
656
|
+
this.nextBatchEntryIndex += 1;
|
|
657
|
+
this.batchEntryIndexes.set(contentIndex, created);
|
|
658
|
+
return created;
|
|
659
|
+
}
|
|
660
|
+
|
|
661
|
+
private getBatchEntries(batchEntryIndex: number): readonly PartialToolRecoveryBatchEntry[] {
|
|
662
|
+
return [...this.streamedBatchEntries.values()]
|
|
663
|
+
.filter((entry) => entry.index <= batchEntryIndex)
|
|
664
|
+
.sort((left, right) => left.index - right.index);
|
|
665
|
+
}
|
|
666
|
+
|
|
667
|
+
private getAllBatchEntries(): readonly PartialToolRecoveryBatchEntry[] {
|
|
668
|
+
return [...this.streamedBatchEntries.values()].sort((left, right) => left.index - right.index);
|
|
669
|
+
}
|
|
670
|
+
|
|
671
|
+
private abortToolCall(
|
|
672
|
+
result: InterceptResult,
|
|
673
|
+
context: InterceptorContext,
|
|
674
|
+
handlerName: string,
|
|
675
|
+
blockExecution: boolean,
|
|
676
|
+
): AgentToolResult<unknown> {
|
|
677
|
+
const reason = abortReason(result, handlerName);
|
|
678
|
+
const annotation: ToolCallAnnotation = result.annotation
|
|
679
|
+
? result.annotation.kind === "aborted" && result.annotation.label === undefined
|
|
680
|
+
? { ...result.annotation, label: reason }
|
|
681
|
+
: result.annotation
|
|
682
|
+
: result.message.customType === "text-editor-stale-anchor-block"
|
|
683
|
+
? { kind: "stale-anchor", label: reason }
|
|
684
|
+
: { kind: "aborted", label: reason, color: "error" };
|
|
685
|
+
|
|
686
|
+
if (context.contentIndex !== undefined) {
|
|
687
|
+
this.abortedContentIndices.add(context.contentIndex);
|
|
688
|
+
this.annotateContent(context.contentIndex, annotation);
|
|
871
689
|
}
|
|
872
690
|
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
691
|
+
const guardResult: AgentToolResult<unknown> = {
|
|
692
|
+
content: [{ type: "text", text: result.message.content }],
|
|
693
|
+
details: withToolCallInterceptionDetails(result.message.details, annotation, reason),
|
|
694
|
+
};
|
|
695
|
+
this.toolCallRecords.set(context.toolCall.id, {
|
|
696
|
+
originalArguments: structuredClone(context.args),
|
|
697
|
+
guardResult,
|
|
698
|
+
...(blockExecution && { blockExecution: true }),
|
|
699
|
+
});
|
|
700
|
+
return guardResult;
|
|
701
|
+
}
|
|
884
702
|
|
|
885
|
-
|
|
886
|
-
|
|
703
|
+
private async finalizeRecovery(): Promise<void> {
|
|
704
|
+
const pending = this.pendingRecoveryBlocks.splice(0);
|
|
887
705
|
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
this.contentNames.delete(contentIndex);
|
|
891
|
-
this.contentToolCallIds.delete(contentIndex);
|
|
892
|
-
this.pendingToolCallAnnotations.delete(contentIndex);
|
|
893
|
-
this.emittedToolCallAnnotationKinds.delete(contentIndex);
|
|
894
|
-
this.completeArgumentContentIndices.delete(contentIndex);
|
|
895
|
-
this.partialBuffers.delete(contentIndex);
|
|
896
|
-
this.abortedContentIndices.delete(contentIndex);
|
|
706
|
+
if (pending.length === 0) {
|
|
707
|
+
return;
|
|
897
708
|
}
|
|
898
709
|
|
|
899
|
-
|
|
900
|
-
{
|
|
901
|
-
for (const handlers of this.handlers.values())
|
|
902
|
-
{
|
|
903
|
-
for (const handler of handlers)
|
|
904
|
-
{
|
|
905
|
-
handler.onAgentEnd?.();
|
|
906
|
-
}
|
|
907
|
-
}
|
|
710
|
+
const groups = new Map<string, PendingRecoveryBlock[]>();
|
|
908
711
|
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
cleanup();
|
|
912
|
-
}
|
|
712
|
+
for (const block of pending) {
|
|
713
|
+
const recoveryKey = this.recoveryRegistry.recoveryKeyFor(block.ctx.toolCall.name);
|
|
913
714
|
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
this.pendingToolCallAnnotations.clear();
|
|
918
|
-
this.completeArgumentContentIndices.clear();
|
|
919
|
-
this.emittedToolCallAnnotationKinds.clear();
|
|
920
|
-
this.partialBuffers.clear();
|
|
921
|
-
this.streamedBatchEntries.clear();
|
|
922
|
-
this.batchEntryIndexes.clear();
|
|
923
|
-
this.toolCallBatchEntryIndexes.clear();
|
|
924
|
-
this.toolCallRecords.clear();
|
|
925
|
-
this.completedBatchEntryIndexes.clear();
|
|
926
|
-
this.nextBatchEntryIndex = 0;
|
|
927
|
-
this.abortedContentIndices.clear();
|
|
928
|
-
this.pendingAbortReason = undefined;
|
|
929
|
-
this.dispatchQueues.clear();
|
|
930
|
-
this.processedStreamEventKeys.clear();
|
|
931
|
-
this.recoveryRegistry.cleanupAll();
|
|
932
|
-
this.pendingRecoveryBlocks.length = 0;
|
|
933
|
-
this.recoveryAppliedBatchIndexes.clear();
|
|
934
|
-
}
|
|
715
|
+
if (recoveryKey === undefined) {
|
|
716
|
+
continue;
|
|
717
|
+
}
|
|
935
718
|
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
719
|
+
const group = groups.get(recoveryKey) ?? [];
|
|
720
|
+
group.push(block);
|
|
721
|
+
groups.set(recoveryKey, group);
|
|
939
722
|
}
|
|
940
723
|
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
724
|
+
for (const group of groups.values()) {
|
|
725
|
+
const first = group[0];
|
|
726
|
+
|
|
727
|
+
if (!first) {
|
|
728
|
+
continue;
|
|
729
|
+
}
|
|
730
|
+
|
|
731
|
+
const finalResult = await this.recoverIfNeeded(
|
|
732
|
+
first.blocked,
|
|
733
|
+
first.ctx,
|
|
734
|
+
this.getAllBatchEntries(),
|
|
735
|
+
group,
|
|
736
|
+
);
|
|
737
|
+
|
|
738
|
+
if (finalResult === first.blocked) {
|
|
739
|
+
continue;
|
|
740
|
+
}
|
|
741
|
+
|
|
742
|
+
const details = finalResult.message.details;
|
|
743
|
+
const recovery =
|
|
744
|
+
Guard.IsObjectNotArray(details) && Guard.IsObjectNotArray(details.recovery)
|
|
745
|
+
? details.recovery
|
|
746
|
+
: undefined;
|
|
747
|
+
const appliedEntries = recovery?.appliedEntries;
|
|
748
|
+
|
|
749
|
+
if (Array.isArray(appliedEntries)) {
|
|
750
|
+
for (const index of appliedEntries) {
|
|
751
|
+
if (typeof index === "number") {
|
|
752
|
+
this.recoveryAppliedBatchIndexes.add(index);
|
|
753
|
+
}
|
|
754
|
+
}
|
|
755
|
+
}
|
|
756
|
+
}
|
|
757
|
+
}
|
|
758
|
+
|
|
759
|
+
private async recoverIfNeeded(
|
|
760
|
+
result: InterceptResult,
|
|
761
|
+
context: InterceptorContext,
|
|
762
|
+
finalBatchEntries: readonly PartialToolRecoveryBatchEntry[],
|
|
763
|
+
blocks: readonly PendingRecoveryBlock[],
|
|
764
|
+
): Promise<InterceptResult> {
|
|
765
|
+
if (context.contentIndex === undefined) {
|
|
766
|
+
return result;
|
|
944
767
|
}
|
|
945
768
|
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
769
|
+
const details = result.message.details;
|
|
770
|
+
const staleEntryIndex =
|
|
771
|
+
Guard.IsObjectNotArray(details) && typeof details.entryIndex === "number"
|
|
772
|
+
? details.entryIndex
|
|
773
|
+
: undefined;
|
|
774
|
+
const blockedEntryIndexes = [
|
|
775
|
+
...new Set(
|
|
776
|
+
blocks
|
|
777
|
+
.map((block) => block.ctx.batchEntryIndex)
|
|
778
|
+
.filter((index): index is number => index !== undefined),
|
|
779
|
+
),
|
|
780
|
+
];
|
|
781
|
+
const blockedToolCallIds = [...new Set(blocks.map((block) => block.ctx.toolCall.id))];
|
|
782
|
+
const blockedDetails = blocks.map((block) => block.blocked.message.details);
|
|
783
|
+
const first = blocks[0];
|
|
784
|
+
const recoveryKey = first
|
|
785
|
+
? this.recoveryRegistry.recoveryKeyFor(first.ctx.toolCall.name)
|
|
786
|
+
: undefined;
|
|
787
|
+
|
|
788
|
+
if (!recoveryKey) {
|
|
789
|
+
return result;
|
|
949
790
|
}
|
|
950
791
|
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
{
|
|
971
|
-
handlers.splice(index, 1);
|
|
972
|
-
}
|
|
973
|
-
}
|
|
792
|
+
const recovery = await this.recoveryRegistry.recover({
|
|
793
|
+
contentIndex: context.contentIndex,
|
|
794
|
+
toolCallId: context.toolCall.id,
|
|
795
|
+
toolName: context.toolCall.name,
|
|
796
|
+
...(staleEntryIndex !== undefined && { staleEntryIndex }),
|
|
797
|
+
staleDetails: details,
|
|
798
|
+
...(context.partialArgs && { partialArgs: context.partialArgs }),
|
|
799
|
+
args: context.args,
|
|
800
|
+
batchEntries: finalBatchEntries,
|
|
801
|
+
...(context.batchEntryIndex !== undefined && { batchEntryIndex: context.batchEntryIndex }),
|
|
802
|
+
recoverCompleteBatch: true,
|
|
803
|
+
completedEntryIndexes: [...this.completedBatchEntryIndexes],
|
|
804
|
+
blockedEntryIndexes,
|
|
805
|
+
blockedToolCallIds,
|
|
806
|
+
blockedDetails,
|
|
807
|
+
recoveryKey,
|
|
808
|
+
cwd: context.cwd,
|
|
809
|
+
ctx: context.ctx,
|
|
810
|
+
});
|
|
974
811
|
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
812
|
+
return recovery && (recovery.appliedEntries.length > 0 || recovery.skippedEntries.length > 0)
|
|
813
|
+
? makeRecoveryResult(result, recovery)
|
|
814
|
+
: result;
|
|
815
|
+
}
|
|
816
|
+
private async enqueue(
|
|
817
|
+
contentIndex: number,
|
|
818
|
+
task: () => Promise<GuardInterception | undefined>,
|
|
819
|
+
): Promise<GuardInterception | undefined> {
|
|
820
|
+
const previous = this.dispatchQueues.get(contentIndex) ?? Promise.resolve();
|
|
821
|
+
const current = previous.catch(() => {}).then(task);
|
|
822
|
+
this.dispatchQueues.set(contentIndex, current);
|
|
823
|
+
|
|
824
|
+
try {
|
|
825
|
+
return await current;
|
|
826
|
+
} finally {
|
|
827
|
+
if (this.dispatchQueues.get(contentIndex) === current) {
|
|
828
|
+
this.dispatchQueues.delete(contentIndex);
|
|
829
|
+
}
|
|
981
830
|
}
|
|
831
|
+
}
|
|
982
832
|
|
|
983
|
-
|
|
984
|
-
{
|
|
985
|
-
|
|
986
|
-
{
|
|
987
|
-
this.handlers.set(toolName, handlers.filter((handler) => handler.name !== name));
|
|
988
|
-
}
|
|
833
|
+
private installAbortCleanup(contentIndex: number, signal: AbortSignal | undefined): void {
|
|
834
|
+
if (!signal || this.abortCleanups.has(contentIndex)) {
|
|
835
|
+
return;
|
|
989
836
|
}
|
|
990
|
-
}
|
|
991
837
|
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
{
|
|
996
|
-
return `${event.type}\u0000${event.contentIndex}\u0000${event.toolCall.id}`;
|
|
997
|
-
}
|
|
838
|
+
const listener = () => {
|
|
839
|
+
const toolName = this.contentNames.get(contentIndex);
|
|
840
|
+
const handlers = toolName ? this.handlers.get(toolName) : undefined;
|
|
998
841
|
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
}
|
|
842
|
+
for (const handler of handlers ?? []) {
|
|
843
|
+
handler.onAbort?.(contentIndex);
|
|
844
|
+
}
|
|
845
|
+
};
|
|
1003
846
|
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
}
|
|
847
|
+
signal.addEventListener("abort", listener, { once: true });
|
|
848
|
+
this.abortCleanups.set(contentIndex, () => {
|
|
849
|
+
signal.removeEventListener("abort", listener);
|
|
850
|
+
});
|
|
851
|
+
}
|
|
852
|
+
|
|
853
|
+
private cleanupContent(contentIndex: number, toolName?: string): void {
|
|
854
|
+
this.cleanupTransientContent(contentIndex, toolName);
|
|
855
|
+
this.recoveryRegistry.cleanup(contentIndex);
|
|
856
|
+
}
|
|
857
|
+
|
|
858
|
+
private cleanupTransientContent(contentIndex: number, toolName?: string, aborted = false): void {
|
|
859
|
+
const name = toolName ?? this.contentNames.get(contentIndex);
|
|
860
|
+
const handlers = name ? this.handlers.get(name) : undefined;
|
|
861
|
+
|
|
862
|
+
for (const handler of handlers ?? []) {
|
|
863
|
+
if (aborted) {
|
|
864
|
+
handler.onAbort?.(contentIndex);
|
|
865
|
+
}
|
|
1009
866
|
|
|
1010
|
-
|
|
1011
|
-
{
|
|
1012
|
-
if (result.annotation?.reason)
|
|
1013
|
-
{
|
|
1014
|
-
return result.annotation.reason;
|
|
867
|
+
handler.onContentEnd?.(contentIndex);
|
|
1015
868
|
}
|
|
1016
869
|
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
870
|
+
this.abortCleanups.get(contentIndex)?.();
|
|
871
|
+
this.abortCleanups.delete(contentIndex);
|
|
872
|
+
this.contentNames.delete(contentIndex);
|
|
873
|
+
this.contentToolCallIds.delete(contentIndex);
|
|
874
|
+
this.pendingToolCallAnnotations.delete(contentIndex);
|
|
875
|
+
this.emittedToolCallAnnotationKinds.delete(contentIndex);
|
|
876
|
+
this.completeArgumentContentIndices.delete(contentIndex);
|
|
877
|
+
this.partialBuffers.delete(contentIndex);
|
|
878
|
+
this.abortedContentIndices.delete(contentIndex);
|
|
879
|
+
}
|
|
880
|
+
|
|
881
|
+
private cleanupAll(): void {
|
|
882
|
+
for (const handlers of this.handlers.values()) {
|
|
883
|
+
for (const handler of handlers) {
|
|
884
|
+
handler.onAgentEnd?.();
|
|
885
|
+
}
|
|
1020
886
|
}
|
|
1021
887
|
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
return "Stale Anchor";
|
|
888
|
+
for (const cleanup of this.abortCleanups.values()) {
|
|
889
|
+
cleanup();
|
|
1025
890
|
}
|
|
1026
891
|
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
892
|
+
this.abortCleanups.clear();
|
|
893
|
+
this.contentNames.clear();
|
|
894
|
+
this.contentToolCallIds.clear();
|
|
895
|
+
this.pendingToolCallAnnotations.clear();
|
|
896
|
+
this.completeArgumentContentIndices.clear();
|
|
897
|
+
this.emittedToolCallAnnotationKinds.clear();
|
|
898
|
+
this.partialBuffers.clear();
|
|
899
|
+
this.streamedBatchEntries.clear();
|
|
900
|
+
this.batchEntryIndexes.clear();
|
|
901
|
+
this.toolCallBatchEntryIndexes.clear();
|
|
902
|
+
this.toolCallRecords.clear();
|
|
903
|
+
this.completedBatchEntryIndexes.clear();
|
|
904
|
+
this.nextBatchEntryIndex = 0;
|
|
905
|
+
this.abortedContentIndices.clear();
|
|
906
|
+
this.pendingAbortReason = undefined;
|
|
907
|
+
this.dispatchQueues.clear();
|
|
908
|
+
this.processedStreamEventKeys.clear();
|
|
909
|
+
this.recoveryRegistry.cleanupAll();
|
|
910
|
+
this.pendingRecoveryBlocks.length = 0;
|
|
911
|
+
this.recoveryAppliedBatchIndexes.clear();
|
|
912
|
+
}
|
|
913
|
+
|
|
914
|
+
registerRecovery(registration: Parameters<PartialToolRecoveryRegistry["register"]>[0]): void {
|
|
915
|
+
this.recoveryRegistry.register(registration);
|
|
916
|
+
}
|
|
917
|
+
|
|
918
|
+
unregisterRecovery(toolName: string): void {
|
|
919
|
+
this.recoveryRegistry.unregister(toolName);
|
|
920
|
+
}
|
|
921
|
+
|
|
922
|
+
registerBlockedCallSink(sink: BlockedToolCallSink | undefined): void {
|
|
923
|
+
this.blockedCallSink = sink;
|
|
924
|
+
}
|
|
925
|
+
|
|
926
|
+
register(handler: ToolCallInterceptorHandler): void {
|
|
927
|
+
const targetTools = new Set(handler.toolNames);
|
|
928
|
+
|
|
929
|
+
for (const [toolName, handlers] of this.handlers) {
|
|
930
|
+
const index = handlers.findIndex((current) => current.name === handler.name);
|
|
931
|
+
|
|
932
|
+
if (index === -1) {
|
|
933
|
+
continue;
|
|
934
|
+
}
|
|
935
|
+
|
|
936
|
+
if (targetTools.has(toolName)) {
|
|
937
|
+
handlers[index] = handler;
|
|
938
|
+
targetTools.delete(toolName);
|
|
939
|
+
} else {
|
|
940
|
+
handlers.splice(index, 1);
|
|
941
|
+
}
|
|
942
|
+
}
|
|
1034
943
|
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
const value: unknown = JSON.parse(json);
|
|
1040
|
-
return Guard.IsObjectNotArray(value) ? value : undefined;
|
|
944
|
+
for (const toolName of targetTools) {
|
|
945
|
+
const handlers = this.handlers.get(toolName) ?? [];
|
|
946
|
+
handlers.push(handler);
|
|
947
|
+
this.handlers.set(toolName, handlers);
|
|
1041
948
|
}
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
949
|
+
}
|
|
950
|
+
|
|
951
|
+
unregister(name: string): void {
|
|
952
|
+
for (const [toolName, handlers] of this.handlers) {
|
|
953
|
+
this.handlers.set(
|
|
954
|
+
toolName,
|
|
955
|
+
handlers.filter((handler) => handler.name !== name),
|
|
956
|
+
);
|
|
1045
957
|
}
|
|
958
|
+
}
|
|
1046
959
|
}
|
|
1047
960
|
|
|
1048
|
-
function
|
|
1049
|
-
{
|
|
1050
|
-
|
|
961
|
+
function toolStreamEventKey(event: AssistantStreamEvent): string | undefined {
|
|
962
|
+
if (event.type === "toolcall_end") {
|
|
963
|
+
return `${event.type}\u{0}${event.contentIndex}\u{0}${event.toolCall.id}`;
|
|
964
|
+
}
|
|
1051
965
|
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
}
|
|
966
|
+
if (event.type !== "toolcall_start" && event.type !== "toolcall_delta") {
|
|
967
|
+
return undefined;
|
|
968
|
+
}
|
|
1056
969
|
|
|
1057
|
-
|
|
970
|
+
const block = event.partial.content[event.contentIndex];
|
|
971
|
+
const toolCallId = block?.type === "toolCall" ? block.id : "";
|
|
972
|
+
const delta = event.type === "toolcall_delta" ? event.delta : "";
|
|
973
|
+
return `${event.type}\u{0}${event.contentIndex}\u{0}${toolCallId}\u{0}${delta}`;
|
|
974
|
+
}
|
|
1058
975
|
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
976
|
+
function abortReason(result: InterceptResult, handlerName: string): string {
|
|
977
|
+
if (result.annotation?.reason) {
|
|
978
|
+
return result.annotation.reason;
|
|
979
|
+
}
|
|
980
|
+
|
|
981
|
+
if (result.annotation?.label) {
|
|
982
|
+
return result.annotation.label;
|
|
983
|
+
}
|
|
984
|
+
|
|
985
|
+
if (result.message.customType === "text-editor-stale-anchor-block") {
|
|
986
|
+
return "Stale Anchor";
|
|
987
|
+
}
|
|
988
|
+
|
|
989
|
+
return (
|
|
990
|
+
handlerName
|
|
991
|
+
.replace(/[-_]+/g, " ")
|
|
992
|
+
.replace(/([a-z])([A-Z])/g, "$1 $2")
|
|
993
|
+
.replace(/\s+/g, " ")
|
|
994
|
+
.trim()
|
|
995
|
+
.replace(/\b\w/g, (letter) => letter.toUpperCase()) || "Blocked"
|
|
996
|
+
);
|
|
997
|
+
}
|
|
1063
998
|
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
: candidates.filter((result) => result.path === stalePath);
|
|
1073
|
-
const selected = matching.length > 0 ? matching : candidates;
|
|
1074
|
-
const anchors = selected.flatMap((result) =>
|
|
1075
|
-
{
|
|
1076
|
-
const freshAnchors = result.freshAnchors;
|
|
1077
|
-
return Array.isArray(freshAnchors)
|
|
1078
|
-
? freshAnchors.filter((anchor): anchor is string => typeof anchor === "string")
|
|
1079
|
-
: [];
|
|
1080
|
-
});
|
|
999
|
+
function parseCompleteArguments(json: string): Record<string, unknown> | undefined {
|
|
1000
|
+
try {
|
|
1001
|
+
const value: unknown = JSON.parse(json);
|
|
1002
|
+
return Guard.IsObjectNotArray(value) ? value : undefined;
|
|
1003
|
+
} catch {
|
|
1004
|
+
return undefined;
|
|
1005
|
+
}
|
|
1006
|
+
}
|
|
1081
1007
|
|
|
1082
|
-
|
|
1008
|
+
function freshAnchorsAfterRecovery(recovery: PartialToolRecoveryResult): string[] {
|
|
1009
|
+
const normalResult = recovery.normalResult;
|
|
1010
|
+
|
|
1011
|
+
if (!Guard.IsObjectNotArray(normalResult) || !Guard.IsObjectNotArray(normalResult.details)) {
|
|
1012
|
+
return [];
|
|
1013
|
+
}
|
|
1014
|
+
|
|
1015
|
+
const results = normalResult.details.results;
|
|
1016
|
+
|
|
1017
|
+
if (!Array.isArray(results)) {
|
|
1018
|
+
return [];
|
|
1019
|
+
}
|
|
1020
|
+
|
|
1021
|
+
const stalePath =
|
|
1022
|
+
Guard.IsObjectNotArray(recovery.staleDetails) && typeof recovery.staleDetails.path === "string"
|
|
1023
|
+
? recovery.staleDetails.path
|
|
1024
|
+
: undefined;
|
|
1025
|
+
const candidates = results
|
|
1026
|
+
.filter(Guard.IsObjectNotArray)
|
|
1027
|
+
.map((result) => (Guard.IsObjectNotArray(result.data) ? result.data : result));
|
|
1028
|
+
const matching =
|
|
1029
|
+
stalePath === undefined ? candidates : candidates.filter((result) => result.path === stalePath);
|
|
1030
|
+
const selected = matching.length > 0 ? matching : candidates;
|
|
1031
|
+
const anchors = selected.flatMap((result) => {
|
|
1032
|
+
const freshAnchors = result.freshAnchors;
|
|
1033
|
+
return Array.isArray(freshAnchors)
|
|
1034
|
+
? freshAnchors.filter((anchor): anchor is string => typeof anchor === "string")
|
|
1035
|
+
: [];
|
|
1036
|
+
});
|
|
1037
|
+
|
|
1038
|
+
return [...new Set(anchors)];
|
|
1083
1039
|
}
|
|
1084
1040
|
|
|
1085
|
-
function replaceCurrentAnchors(content: string, freshAnchors: readonly string[]): string
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
return `${base}\n\nCurrent nearby anchors:\n${freshAnchors.map((anchor) => ` ${anchor}`).join("\n")}`;
|
|
1041
|
+
function replaceCurrentAnchors(content: string, freshAnchors: readonly string[]): string {
|
|
1042
|
+
const marker = "\n\nCurrent nearby anchors:";
|
|
1043
|
+
const markerIndex = content.indexOf(marker);
|
|
1044
|
+
const base = markerIndex === -1 ? content : content.slice(0, markerIndex);
|
|
1045
|
+
return `${base}\n\nCurrent nearby anchors:\n${freshAnchors.map((anchor) => ` ${anchor}`).join("\n")}`;
|
|
1091
1046
|
}
|
|
1092
1047
|
|
|
1093
1048
|
function makeRecoveryResult(
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
): InterceptResult
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
}
|
|
1127
|
-
|
|
1049
|
+
blocked: InterceptResult,
|
|
1050
|
+
recovery: PartialToolRecoveryResult,
|
|
1051
|
+
): InterceptResult {
|
|
1052
|
+
const appliedCount = recovery.appliedEntries.length;
|
|
1053
|
+
const applied =
|
|
1054
|
+
appliedCount > 0 ? `${appliedCount} (indices: ${recovery.appliedEntries.join(", ")})` : "0";
|
|
1055
|
+
const skipped =
|
|
1056
|
+
recovery.skippedEntries.length > 0
|
|
1057
|
+
? recovery.skippedEntries.map((entry) => `${entry.index} (${entry.reason})`).join(", ")
|
|
1058
|
+
: "none";
|
|
1059
|
+
const execution = recovery.error
|
|
1060
|
+
? `Recovery execution failed: ${recovery.error}.`
|
|
1061
|
+
: "Recovery execution completed.";
|
|
1062
|
+
const freshAnchors = freshAnchorsAfterRecovery(recovery);
|
|
1063
|
+
const blockedDetails =
|
|
1064
|
+
freshAnchors.length > 0 && Guard.IsObjectNotArray(blocked.message.details)
|
|
1065
|
+
? { ...blocked.message.details, freshAnchors }
|
|
1066
|
+
: blocked.message.details;
|
|
1067
|
+
const blockedContent =
|
|
1068
|
+
freshAnchors.length > 0
|
|
1069
|
+
? replaceCurrentAnchors(blocked.message.content, freshAnchors)
|
|
1070
|
+
: blocked.message.content;
|
|
1071
|
+
const displayedRecovery =
|
|
1072
|
+
freshAnchors.length > 0 && Guard.IsObjectNotArray(recovery.staleDetails)
|
|
1073
|
+
? { ...recovery, staleDetails: { ...recovery.staleDetails, freshAnchors } }
|
|
1074
|
+
: recovery;
|
|
1075
|
+
|
|
1076
|
+
return {
|
|
1077
|
+
message: {
|
|
1078
|
+
customType: PARTIAL_TOOL_RECOVERY_CUSTOM_TYPE,
|
|
1079
|
+
content:
|
|
1080
|
+
`${blockedContent}\n\n[RECOVERY] Status: ${recovery.status}. Applied entries: ${applied}. ` +
|
|
1081
|
+
`Skipped entries: ${skipped}. ${execution}`,
|
|
1082
|
+
display: false,
|
|
1083
|
+
details: { blocked: blockedDetails, recovery: displayedRecovery },
|
|
1084
|
+
},
|
|
1085
|
+
};
|
|
1128
1086
|
}
|
|
1129
1087
|
|
|
1130
1088
|
const coordinators = new WeakMap<ExtensionAPI, ToolCallInterceptor>();
|
|
1131
1089
|
|
|
1132
|
-
function coordinatorFor(pi: ExtensionAPI): ToolCallInterceptor
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
|
|
1142
|
-
|
|
1143
|
-
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
coordinator.finalizeStreamInterception(contentIndex);
|
|
1153
|
-
},
|
|
1154
|
-
);
|
|
1155
|
-
return coordinator;
|
|
1090
|
+
function coordinatorFor(pi: ExtensionAPI): ToolCallInterceptor {
|
|
1091
|
+
const existing = coordinators.get(pi);
|
|
1092
|
+
|
|
1093
|
+
if (existing) {
|
|
1094
|
+
return existing;
|
|
1095
|
+
}
|
|
1096
|
+
|
|
1097
|
+
const coordinator = new InterceptorImpl(pi);
|
|
1098
|
+
coordinators.set(pi, coordinator);
|
|
1099
|
+
registerStreamingInterceptorProvider(
|
|
1100
|
+
pi,
|
|
1101
|
+
(event, context) => coordinator.processStreamEvent(event, context),
|
|
1102
|
+
(event) => {
|
|
1103
|
+
coordinator.markStreamEventProcessed(event);
|
|
1104
|
+
},
|
|
1105
|
+
(contentIndex) => {
|
|
1106
|
+
coordinator.finalizeStreamInterception(contentIndex);
|
|
1107
|
+
},
|
|
1108
|
+
);
|
|
1109
|
+
return coordinator;
|
|
1156
1110
|
}
|
|
1157
1111
|
|
|
1158
|
-
export function registerToolCallInterceptor(
|
|
1159
|
-
|
|
1160
|
-
|
|
1112
|
+
export function registerToolCallInterceptor(
|
|
1113
|
+
pi: ExtensionAPI,
|
|
1114
|
+
handler: ToolCallInterceptorHandler,
|
|
1115
|
+
): void {
|
|
1116
|
+
coordinatorFor(pi).register(handler);
|
|
1161
1117
|
}
|
|
1162
1118
|
|
|
1163
|
-
export function unregisterToolCallInterceptor(pi: ExtensionAPI, handlerName: string): void
|
|
1164
|
-
|
|
1165
|
-
coordinatorFor(pi).unregister(handlerName);
|
|
1119
|
+
export function unregisterToolCallInterceptor(pi: ExtensionAPI, handlerName: string): void {
|
|
1120
|
+
coordinatorFor(pi).unregister(handlerName);
|
|
1166
1121
|
}
|
|
1167
1122
|
|
|
1168
1123
|
export function registerToolCallRecovery(
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
): void
|
|
1172
|
-
|
|
1173
|
-
coordinatorFor(pi).registerRecovery(registration);
|
|
1124
|
+
pi: ExtensionAPI,
|
|
1125
|
+
registration: Parameters<PartialToolRecoveryRegistry["register"]>[0],
|
|
1126
|
+
): void {
|
|
1127
|
+
coordinatorFor(pi).registerRecovery(registration);
|
|
1174
1128
|
}
|
|
1175
1129
|
|
|
1176
|
-
export function unregisterToolCallRecovery(pi: ExtensionAPI, toolName: string): void
|
|
1177
|
-
|
|
1178
|
-
coordinatorFor(pi).unregisterRecovery(toolName);
|
|
1130
|
+
export function unregisterToolCallRecovery(pi: ExtensionAPI, toolName: string): void {
|
|
1131
|
+
coordinatorFor(pi).unregisterRecovery(toolName);
|
|
1179
1132
|
}
|
|
1180
1133
|
|
|
1181
1134
|
export function registerToolCallAnnotationSink(
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
): void
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
|
|
1135
|
+
pi: ExtensionAPI,
|
|
1136
|
+
sink?: ToolCallAnnotationSink,
|
|
1137
|
+
): void {
|
|
1138
|
+
toolCallAnnotationSinkUnsubscribers.get(pi)?.();
|
|
1139
|
+
toolCallAnnotationSinkUnsubscribers.delete(pi);
|
|
1140
|
+
|
|
1141
|
+
if (sink === undefined) {
|
|
1142
|
+
return;
|
|
1143
|
+
}
|
|
1144
|
+
|
|
1145
|
+
const unsubscribe = pi.events.on(TOOL_CALL_ANNOTATION_EVENT, (event) => {
|
|
1146
|
+
if (
|
|
1147
|
+
!Guard.IsObjectNotArray(event) ||
|
|
1148
|
+
typeof event.toolCallId !== "string" ||
|
|
1149
|
+
!isToolCallAnnotation(event.annotation)
|
|
1150
|
+
) {
|
|
1151
|
+
return;
|
|
1192
1152
|
}
|
|
1193
1153
|
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
!Guard.IsObjectNotArray(event)
|
|
1198
|
-
|| typeof event.toolCallId !== "string"
|
|
1199
|
-
|| !isToolCallAnnotation(event.annotation)
|
|
1200
|
-
)
|
|
1201
|
-
{
|
|
1202
|
-
return;
|
|
1203
|
-
}
|
|
1204
|
-
|
|
1205
|
-
sink(event.toolCallId, event.annotation);
|
|
1206
|
-
});
|
|
1207
|
-
toolCallAnnotationSinkUnsubscribers.set(pi, unsubscribe);
|
|
1154
|
+
sink(event.toolCallId, event.annotation);
|
|
1155
|
+
});
|
|
1156
|
+
toolCallAnnotationSinkUnsubscribers.set(pi, unsubscribe);
|
|
1208
1157
|
}
|
|
1209
1158
|
|
|
1210
1159
|
export function registerBlockedToolCallSink(
|
|
1211
|
-
|
|
1212
|
-
|
|
1213
|
-
): void
|
|
1214
|
-
|
|
1215
|
-
coordinatorFor(pi).registerBlockedCallSink(sink);
|
|
1160
|
+
pi: ExtensionAPI,
|
|
1161
|
+
sink: BlockedToolCallSink | undefined,
|
|
1162
|
+
): void {
|
|
1163
|
+
coordinatorFor(pi).registerBlockedCallSink(sink);
|
|
1216
1164
|
}
|
|
1217
1165
|
|
|
1218
1166
|
export function processToolCallStreamEvent(
|
|
1219
|
-
|
|
1220
|
-
|
|
1221
|
-
|
|
1222
|
-
): Promise<GuardInterception | undefined>
|
|
1223
|
-
|
|
1224
|
-
return coordinatorFor(pi).processStreamEvent(event, ctx);
|
|
1167
|
+
pi: ExtensionAPI,
|
|
1168
|
+
event: AssistantStreamEvent,
|
|
1169
|
+
context: ExtensionContext,
|
|
1170
|
+
): Promise<GuardInterception | undefined> {
|
|
1171
|
+
return coordinatorFor(pi).processStreamEvent(event, context);
|
|
1225
1172
|
}
|
|
1226
1173
|
|
|
1227
|
-
export function markToolCallStreamEventProcessed(
|
|
1228
|
-
|
|
1229
|
-
|
|
1174
|
+
export function markToolCallStreamEventProcessed(
|
|
1175
|
+
pi: ExtensionAPI,
|
|
1176
|
+
event: AssistantStreamEvent,
|
|
1177
|
+
): void {
|
|
1178
|
+
coordinatorFor(pi).markStreamEventProcessed(event);
|
|
1230
1179
|
}
|
|
1231
1180
|
|
|
1232
|
-
export function getToolCallRecord(
|
|
1233
|
-
|
|
1234
|
-
|
|
1181
|
+
export function getToolCallRecord(
|
|
1182
|
+
pi: ExtensionAPI,
|
|
1183
|
+
toolCallId: string,
|
|
1184
|
+
): GuardCallRecord | undefined {
|
|
1185
|
+
return coordinatorFor(pi).getToolCallRecord(toolCallId);
|
|
1235
1186
|
}
|