pi-agent-ide 0.7.1 → 0.7.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +66 -0
- package/README.md +3 -1
- package/dist/{chunk-PKKDDIWW.js → chunk-2ZMMRINH.js} +1 -1
- package/dist/chunk-6U5OU77O.js +3 -0
- package/dist/chunk-BYZSBXUK.js +2 -0
- package/dist/{chunk-LEEQIQ7D.js → chunk-CEX73RI5.js} +6 -6
- package/dist/chunk-DZWTKF2K.js +1 -0
- package/dist/{chunk-WMO6RNE2.js → chunk-EOKEQ3YH.js} +6 -6
- package/dist/{chunk-HZEY4GT4.js → chunk-H445SHSJ.js} +2 -2
- package/dist/{chunk-4GCWGE2C.js → chunk-HZ5BNF3A.js} +1 -1
- package/dist/chunk-IG56UGSO.js +2 -0
- package/dist/chunk-IICJXZBU.js +6 -0
- package/dist/{chunk-XYS4FSCC.js → chunk-IM24DB3K.js} +1 -1
- package/dist/chunk-MXLSGMRZ.js +3 -0
- package/dist/{chunk-76ZK5ODR.js → chunk-NHKPP2AI.js} +13 -13
- package/dist/chunk-OB46OPMZ.js +1 -0
- package/dist/{chunk-PNU3E7IO.js → chunk-Q6CZ6GBR.js} +2 -2
- package/dist/chunk-QUYXINGZ.js +7 -0
- package/dist/{chunk-OMVRLWMX.js → chunk-SBP3ABH4.js} +1 -1
- package/dist/{chunk-YP46DVVP.js → chunk-STBJO3Z3.js} +1 -1
- package/dist/{chunk-2TRHFMCZ.js → chunk-TXOTNK6B.js} +1 -1
- package/dist/chunk-WLZIUMJL.js +1 -0
- package/dist/chunk-ZIWPKIBZ.js +1 -0
- package/dist/entry-11.js +1 -1
- package/dist/entry-13.js +1 -1
- package/dist/entry-14.js +1 -1
- package/dist/entry-15.js +1 -1
- package/dist/entry-17.js +1 -1
- package/dist/entry-25.js +1 -1
- package/dist/entry-26.js +1 -1
- package/dist/entry-27.js +1 -1
- package/dist/entry-28.js +1 -1
- package/dist/entry-29.js +1 -1
- package/dist/entry-31.js +12 -11
- package/dist/entry-33.js +3 -1
- package/dist/entry-37.js +6 -6
- package/dist/entry-42.js +1 -1
- package/dist/entry-44.js +1 -1
- package/dist/entry-45.js +20 -12
- package/dist/entry-46.js +1 -1
- package/dist/entry-49.js +1 -1
- package/dist/entry-50.js +1 -1
- package/dist/entry-51.js +7 -6
- package/dist/entry-52.js +1 -1
- package/dist/entry-53.js +1 -1
- package/dist/entry-54.js +1 -1
- package/dist/entry-56.js +1 -1
- package/dist/entry-58.js +1 -1
- package/dist/entry-60.js +1 -1
- package/dist/entry-62.js +1 -1
- package/dist/entry-65.js +1 -1
- package/dist/entry-66.js +1 -1
- package/dist/entry-68.js +1 -1
- package/dist/entry-70.js +1 -1
- package/dist/entry-75.js +1 -1
- package/dist/entry-77.js +63 -49
- package/dist/entry-78.js +15 -14
- package/dist/entry-79.js +1 -1
- package/dist/entry-82.js +5 -5
- package/dist/entry-83.js +1 -1
- package/dist/entry-84.js +1 -1
- package/dist/entry-85.js +1 -1
- package/dist/entry-86.js +1 -1
- package/dist/pi-agent-ide.js +34 -11
- package/docs/agent-guides/editing.md +14 -7
- package/docs/agent-guides/git-changes.md +10 -0
- package/docs/agent-guides/read-resources.md +9 -46
- package/docs/agent-guides/search-code.md +7 -1
- package/docs/agent-guides/select-code.md +1 -0
- package/docs/agent-guides/terminal.md +9 -1
- package/docs/configuration.md +22 -1
- package/docs/development.md +4 -0
- package/docs/directory-operations.md +45 -0
- package/docs/releases.md +12 -0
- package/docs/research/codemode-write-context.md +56 -0
- package/docs/research/native-codemode-batch-decision.md +2 -0
- package/docs/structured-results.md +2 -2
- package/docs/tools.md +10 -2
- package/docs/user-hooks.md +26 -0
- package/node_modules/pi-agent-doctor/package.json +1 -1
- package/node_modules/pi-agent-documentation/package.json +1 -1
- package/node_modules/pi-agent-filesystem/package.json +7 -7
- package/node_modules/pi-agent-filesystem-image/package.json +3 -3
- package/node_modules/pi-agent-filesystem-jq/package.json +5 -5
- package/node_modules/pi-agent-filesystem-jq/src/jq.ts +4 -1
- package/node_modules/pi-agent-filesystem-pdf/package.json +3 -3
- package/node_modules/pi-agent-filesystem-text/package.json +3 -3
- package/node_modules/pi-agent-ide-ast/package.json +13 -13
- package/node_modules/pi-agent-ide-ast/src/extension.ts +3 -3
- package/node_modules/pi-agent-ide-ast/src/overflow-handler.ts +9 -4
- package/node_modules/pi-agent-ide-ast/src/search-resolver.ts +0 -3
- package/node_modules/pi-agent-ide-ast/src/select-schema.ts +8 -6
- package/node_modules/pi-agent-ide-ast/src/tool-select.ts +13 -5
- package/node_modules/pi-agent-ide-changes/package.json +9 -8
- package/node_modules/pi-agent-ide-changes/src/tool-index-change.ts +82 -4
- package/node_modules/pi-agent-ide-changes/src/tool-text-undo.ts +8 -11
- package/node_modules/pi-agent-ide-formatter/package.json +3 -3
- package/node_modules/pi-agent-ide-languages/package.json +2 -2
- package/node_modules/pi-agent-ide-lint/package.json +7 -7
- package/node_modules/pi-agent-ide-lsp/index.ts +1 -1
- package/node_modules/pi-agent-ide-lsp/package.json +9 -9
- package/node_modules/pi-agent-ide-lsp/src/declaration-targets.ts +2 -1
- package/node_modules/pi-agent-ide-lsp/src/search-resolver.ts +0 -1
- package/node_modules/pi-agent-image/package.json +2 -2
- package/node_modules/pi-agent-invariant/package.json +1 -1
- package/node_modules/pi-agent-pdf/package.json +2 -2
- package/node_modules/pi-agent-read/package.json +7 -7
- package/node_modules/pi-agent-read/src/api/read-parameters.ts +3 -3
- package/node_modules/pi-agent-read/src/api/tools/read.ts +11 -0
- package/node_modules/pi-agent-read/src/core/extension.ts +1 -1
- package/node_modules/pi-agent-read/src/core/read-core.ts +5 -1
- package/node_modules/pi-agent-read/src/core/tools/read/output-truncation.ts +91 -18
- package/node_modules/pi-agent-read/src/core/tools/read/raw-read.ts +8 -2
- package/node_modules/pi-agent-read/src/core/tools/read/read-renderer.ts +38 -1
- package/node_modules/pi-agent-read/src/core/tools/read/read-result.ts +57 -2
- package/node_modules/pi-agent-read/src/core/tools/tool-read.ts +99 -44
- package/node_modules/pi-agent-resource/package.json +1 -1
- package/node_modules/pi-agent-resource/src/index.ts +1 -0
- package/node_modules/pi-agent-resource/src/result-targets.ts +6 -3
- package/node_modules/{pi-agent-read/src/core/tools/read → pi-agent-resource/src}/temp-resource-store.ts +17 -7
- package/node_modules/pi-agent-search/package.json +5 -5
- package/node_modules/pi-agent-search/src/api/fuzzy.ts +5 -1
- package/node_modules/pi-agent-search/src/api/search-parameters.ts +2 -2
- package/node_modules/pi-agent-search/src/core/extension.ts +2 -1
- package/node_modules/pi-agent-search/src/core/search-core.ts +13 -4
- package/node_modules/pi-agent-search-text/package.json +7 -7
- package/node_modules/pi-agent-search-text/src/resolvers.ts +11 -1
- package/node_modules/pi-agent-search-text/src/scoped-search.ts +1 -1
- package/node_modules/pi-agent-search-text/src/search-presentation.ts +42 -32
- package/node_modules/pi-agent-search-text/src/search-recipe.ts +1 -1
- package/node_modules/pi-agent-search-text/src/search-renderer.ts +6 -0
- package/node_modules/pi-agent-search-text/src/search-result.ts +17 -3
- package/node_modules/pi-agent-text/package.json +2 -2
- package/node_modules/pi-agent-text-anchor-constant/package.json +3 -3
- package/node_modules/pi-agent-text-anchor-exact/package.json +3 -3
- package/node_modules/pi-agent-text-anchor-line-hash/package.json +5 -5
- package/node_modules/pi-agent-text-editor/package.json +9 -9
- package/node_modules/pi-agent-text-editor/src/api/delete-guard.ts +24 -0
- package/node_modules/pi-agent-text-editor/src/api/edit-completion.ts +2 -2
- package/node_modules/pi-agent-text-editor/src/api/mutation-guard.ts +1 -0
- package/node_modules/pi-agent-text-editor/src/api/mutation-result.ts +1 -2
- package/node_modules/pi-agent-text-editor/src/api/native-edit-batch-event.ts +4 -0
- package/node_modules/pi-agent-text-editor/src/api/plugin-protocol.ts +4 -1
- package/node_modules/pi-agent-text-editor/src/api/stale-anchor-message.ts +1 -1
- package/node_modules/pi-agent-text-editor/src/core/delete-policy.ts +284 -0
- package/node_modules/pi-agent-text-editor/src/core/diff-tool.ts +54 -12
- package/node_modules/pi-agent-text-editor/src/core/editing-guidelines.ts +1 -1
- package/node_modules/pi-agent-text-editor/src/core/file-operation-tools.ts +32 -3
- package/node_modules/pi-agent-text-editor/src/core/file-operations.ts +30 -40
- package/node_modules/pi-agent-text-editor/src/core/file-result-targets.ts +1 -1
- package/node_modules/pi-agent-text-editor/src/core/file-transfers.ts +164 -0
- package/node_modules/pi-agent-text-editor/src/core/mutation-result/file-mutation-agent-result.ts +36 -12
- package/node_modules/pi-agent-text-editor/src/core/mutation-result/file-mutation-result.ts +25 -1
- package/node_modules/pi-agent-text-editor/src/core/mutation-result-targets.ts +132 -11
- package/node_modules/pi-agent-text-editor/src/core/native-text-edit-batch.ts +286 -162
- package/node_modules/pi-agent-text-editor/src/core/post-edit-scope.ts +34 -10
- package/node_modules/pi-agent-text-editor/src/core/result-input.ts +24 -4
- package/node_modules/pi-agent-text-editor/src/core/result-transfer.ts +46 -11
- package/node_modules/pi-agent-text-editor/src/core/structured-result.ts +21 -14
- package/node_modules/pi-agent-text-editor/src/core/temporary-directories.ts +96 -0
- package/node_modules/pi-agent-text-editor/src/core/text-change-engine.ts +5 -1
- package/node_modules/pi-agent-text-editor/src/core/text-edit-batch-registrar.ts +155 -22
- package/node_modules/pi-agent-text-editor/src/core/text-edit-batch.ts +4 -0
- package/node_modules/pi-agent-text-editor/src/core/text-editor-core.ts +126 -32
- package/node_modules/pi-agent-text-editor/src/core/text-mutation.ts +428 -189
- package/node_modules/pi-agent-text-editor/src/core/write-agent-result.ts +66 -0
- package/node_modules/pi-agent-text-editor/src/tools/text-tool-schema-properties.ts +6 -4
- package/node_modules/pi-agent-text-editor/src/tools/tool-text-copy.ts +15 -6
- package/node_modules/pi-agent-text-editor/src/tools/tool-text-delete.ts +9 -4
- package/node_modules/pi-agent-text-editor/src/tools/tool-text-insert.ts +7 -8
- package/node_modules/pi-agent-text-editor/src/tools/tool-text-move.ts +6 -4
- package/node_modules/pi-agent-text-editor/src/tools/tool-text-replace.ts +2 -3
- package/node_modules/pi-agent-text-editor/src/tools/tool-text-write.ts +10 -4
- package/node_modules/pi-agent-text-editor-renderer/package.json +5 -5
- package/node_modules/pi-agent-text-editor-renderer/src/mutation-panel.ts +11 -0
- package/node_modules/pi-agent-text-editor-renderer/src/renderer.ts +75 -11
- package/node_modules/pi-agent-text-editor-renderer/src/status-summary.ts +18 -3
- package/node_modules/pi-agent-text-editor-stale-anchor/package.json +4 -4
- package/node_modules/pi-agent-tool-call-interception/package.json +2 -2
- package/node_modules/pi-agent-tool-ui/package.json +1 -1
- package/node_modules/pi-agent-web/package.json +7 -7
- package/node_modules/pi-agent-web/src/resolver.ts +11 -13
- package/node_modules/pi-agent-web-html/package.json +2 -2
- package/node_modules/pi-agent-web-image/package.json +3 -3
- package/node_modules/pi-agent-web-pdf/package.json +3 -3
- package/node_modules/pi-agent-web-text/package.json +3 -3
- package/package.json +36 -35
- package/src/api/hooks.ts +24 -0
- package/src/composite/nested-tool-rendering.ts +184 -77
- package/src/composite/output-limits.ts +125 -0
- package/src/composite/responses-tools.ts +25 -0
- package/src/composite/text-results.ts +158 -7
- package/src/extensions/pi-agent-read/extensions/pi-agent-filesystem/package.json +7 -7
- package/src/extensions/pi-agent-read/extensions/pi-agent-filesystem/plugins/pi-agent-filesystem-image/package.json +3 -3
- package/src/extensions/pi-agent-read/extensions/pi-agent-filesystem/plugins/pi-agent-filesystem-jq/package.json +5 -5
- package/src/extensions/pi-agent-read/extensions/pi-agent-filesystem/plugins/pi-agent-filesystem-jq/src/jq.ts +4 -1
- package/src/extensions/pi-agent-read/extensions/pi-agent-filesystem/plugins/pi-agent-filesystem-pdf/package.json +3 -3
- package/src/extensions/pi-agent-read/extensions/pi-agent-filesystem/plugins/pi-agent-filesystem-text/package.json +3 -3
- package/src/extensions/pi-agent-read/extensions/pi-agent-web/package.json +7 -7
- package/src/extensions/pi-agent-read/extensions/pi-agent-web/plugins/pi-agent-web-html/package.json +2 -2
- package/src/extensions/pi-agent-read/extensions/pi-agent-web/plugins/pi-agent-web-image/package.json +3 -3
- package/src/extensions/pi-agent-read/extensions/pi-agent-web/plugins/pi-agent-web-pdf/package.json +3 -3
- package/src/extensions/pi-agent-read/extensions/pi-agent-web/plugins/pi-agent-web-text/package.json +3 -3
- package/src/extensions/pi-agent-read/extensions/pi-agent-web/src/resolver.ts +11 -13
- package/src/extensions/pi-agent-read/package.json +7 -7
- package/src/extensions/pi-agent-read/src/api/read-parameters.ts +3 -3
- package/src/extensions/pi-agent-read/src/api/tools/read.ts +11 -0
- package/src/extensions/pi-agent-read/src/core/extension.ts +1 -1
- package/src/extensions/pi-agent-read/src/core/read-core.ts +5 -1
- package/src/extensions/pi-agent-read/src/core/tools/read/output-truncation.ts +91 -18
- package/src/extensions/pi-agent-read/src/core/tools/read/raw-read.ts +8 -2
- package/src/extensions/pi-agent-read/src/core/tools/read/read-renderer.ts +38 -1
- package/src/extensions/pi-agent-read/src/core/tools/read/read-result.ts +57 -2
- package/src/extensions/pi-agent-read/src/core/tools/tool-read.ts +99 -44
- package/src/extensions/pi-agent-search/package.json +5 -5
- package/src/extensions/pi-agent-search/plugins/pi-agent-search-text/package.json +7 -7
- package/src/extensions/pi-agent-search/plugins/pi-agent-search-text/src/resolvers.ts +11 -1
- package/src/extensions/pi-agent-search/plugins/pi-agent-search-text/src/scoped-search.ts +1 -1
- package/src/extensions/pi-agent-search/plugins/pi-agent-search-text/src/search-presentation.ts +42 -32
- package/src/extensions/pi-agent-search/plugins/pi-agent-search-text/src/search-recipe.ts +1 -1
- package/src/extensions/pi-agent-search/plugins/pi-agent-search-text/src/search-renderer.ts +6 -0
- package/src/extensions/pi-agent-search/plugins/pi-agent-search-text/src/search-result.ts +17 -3
- package/src/extensions/pi-agent-search/src/api/fuzzy.ts +5 -1
- package/src/extensions/pi-agent-search/src/api/search-parameters.ts +2 -2
- package/src/extensions/pi-agent-search/src/core/extension.ts +2 -1
- package/src/extensions/pi-agent-search/src/core/search-core.ts +13 -4
- package/src/extensions/pi-agent-text-editor/package.json +9 -9
- package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-anchor-constant/package.json +3 -3
- package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-anchor-exact/package.json +3 -3
- package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-anchor-line-hash/package.json +5 -5
- package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-renderer/package.json +5 -5
- package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-renderer/src/mutation-panel.ts +11 -0
- package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-renderer/src/renderer.ts +75 -11
- package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-renderer/src/status-summary.ts +18 -3
- package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-stale-anchor/package.json +4 -4
- package/src/extensions/pi-agent-text-editor/src/api/delete-guard.ts +24 -0
- package/src/extensions/pi-agent-text-editor/src/api/edit-completion.ts +2 -2
- package/src/extensions/pi-agent-text-editor/src/api/mutation-guard.ts +1 -0
- package/src/extensions/pi-agent-text-editor/src/api/mutation-result.ts +1 -2
- package/src/extensions/pi-agent-text-editor/src/api/native-edit-batch-event.ts +4 -0
- package/src/extensions/pi-agent-text-editor/src/api/plugin-protocol.ts +4 -1
- package/src/extensions/pi-agent-text-editor/src/api/stale-anchor-message.ts +1 -1
- package/src/extensions/pi-agent-text-editor/src/core/delete-policy.ts +284 -0
- package/src/extensions/pi-agent-text-editor/src/core/diff-tool.ts +54 -12
- package/src/extensions/pi-agent-text-editor/src/core/editing-guidelines.ts +1 -1
- package/src/extensions/pi-agent-text-editor/src/core/file-operation-tools.ts +32 -3
- package/src/extensions/pi-agent-text-editor/src/core/file-operations.ts +30 -40
- package/src/extensions/pi-agent-text-editor/src/core/file-result-targets.ts +1 -1
- package/src/extensions/pi-agent-text-editor/src/core/file-transfers.ts +164 -0
- package/src/extensions/pi-agent-text-editor/src/core/mutation-result/file-mutation-agent-result.ts +36 -12
- package/src/extensions/pi-agent-text-editor/src/core/mutation-result/file-mutation-result.ts +25 -1
- package/src/extensions/pi-agent-text-editor/src/core/mutation-result-targets.ts +132 -11
- package/src/extensions/pi-agent-text-editor/src/core/native-text-edit-batch.ts +286 -162
- package/src/extensions/pi-agent-text-editor/src/core/post-edit-scope.ts +34 -10
- package/src/extensions/pi-agent-text-editor/src/core/result-input.ts +24 -4
- package/src/extensions/pi-agent-text-editor/src/core/result-transfer.ts +46 -11
- package/src/extensions/pi-agent-text-editor/src/core/structured-result.ts +21 -14
- package/src/extensions/pi-agent-text-editor/src/core/temporary-directories.ts +96 -0
- package/src/extensions/pi-agent-text-editor/src/core/text-change-engine.ts +5 -1
- package/src/extensions/pi-agent-text-editor/src/core/text-edit-batch-registrar.ts +155 -22
- package/src/extensions/pi-agent-text-editor/src/core/text-edit-batch.ts +4 -0
- package/src/extensions/pi-agent-text-editor/src/core/text-editor-core.ts +126 -32
- package/src/extensions/pi-agent-text-editor/src/core/text-mutation.ts +428 -189
- package/src/extensions/pi-agent-text-editor/src/core/write-agent-result.ts +66 -0
- package/src/extensions/pi-agent-text-editor/src/tools/text-tool-schema-properties.ts +6 -4
- package/src/extensions/pi-agent-text-editor/src/tools/tool-text-copy.ts +15 -6
- package/src/extensions/pi-agent-text-editor/src/tools/tool-text-delete.ts +9 -4
- package/src/extensions/pi-agent-text-editor/src/tools/tool-text-insert.ts +7 -8
- package/src/extensions/pi-agent-text-editor/src/tools/tool-text-move.ts +6 -4
- package/src/extensions/pi-agent-text-editor/src/tools/tool-text-replace.ts +2 -3
- package/src/extensions/pi-agent-text-editor/src/tools/tool-text-write.ts +10 -4
- package/src/pi-agent-ide.ts +11 -0
- package/src/plugins/pi-agent-ide-ast/package.json +13 -13
- package/src/plugins/pi-agent-ide-ast/src/extension.ts +3 -3
- package/src/plugins/pi-agent-ide-ast/src/overflow-handler.ts +9 -4
- package/src/plugins/pi-agent-ide-ast/src/search-resolver.ts +0 -3
- package/src/plugins/pi-agent-ide-ast/src/select-schema.ts +8 -6
- package/src/plugins/pi-agent-ide-ast/src/tool-select.ts +13 -5
- package/src/plugins/pi-agent-ide-changes/package.json +9 -8
- package/src/plugins/pi-agent-ide-changes/src/tool-index-change.ts +82 -4
- package/src/plugins/pi-agent-ide-changes/src/tool-text-undo.ts +8 -11
- package/src/plugins/pi-agent-ide-debugger/index.ts +20 -8
- package/src/plugins/pi-agent-ide-debugger/src/dap-client.ts +5 -0
- package/src/plugins/pi-agent-ide-debugger/src/session-manager.ts +93 -38
- package/src/plugins/pi-agent-ide-diagnostics/src/extension.ts +3 -2
- package/src/plugins/pi-agent-ide-formatter/package.json +3 -3
- package/src/plugins/pi-agent-ide-languages/package.json +2 -2
- package/src/plugins/pi-agent-ide-lint/package.json +7 -7
- package/src/plugins/pi-agent-ide-lsp/index.ts +1 -1
- package/src/plugins/pi-agent-ide-lsp/package.json +9 -9
- package/src/plugins/pi-agent-ide-lsp/src/declaration-targets.ts +2 -1
- package/src/plugins/pi-agent-ide-lsp/src/search-resolver.ts +0 -1
- package/src/plugins/pi-agent-ide-terminal/src/output-limits.ts +3 -3
- package/src/plugins/pi-agent-ide-terminal/src/tools.ts +3 -3
- package/src/plugins/pi-agent-ide-vision/index.ts +1 -0
- package/src/post-edit/gate.ts +13 -2
- package/dist/chunk-4XMY4H4V.js +0 -1
- package/dist/chunk-64M3SQPG.js +0 -3
- package/dist/chunk-CWD4O5PM.js +0 -3
- package/dist/chunk-D3WTDA6V.js +0 -2
- package/dist/chunk-EAZGN5ND.js +0 -7
- package/dist/chunk-GXEGY7GV.js +0 -1
- package/dist/chunk-MBUXFKIW.js +0 -3
- package/dist/chunk-PGIVHICI.js +0 -1
- package/dist/chunk-VMTR74FF.js +0 -2
- package/dist/chunk-WU6ZK6YD.js +0 -1
- package/src/extensions/pi-agent-read/src/core/tools/read/temp-resource-store.ts +0 -105
|
@@ -0,0 +1,284 @@
|
|
|
1
|
+
import { execFile } from "node:child_process";
|
|
2
|
+
import { lstat, readFile, realpath } from "node:fs/promises";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { promisify } from "node:util";
|
|
5
|
+
import { loadTemporaryDirectories } from "./temporary-directories.js";
|
|
6
|
+
import { requiredValue } from "pi-agent-invariant";
|
|
7
|
+
import type { BeforeDeleteEvent } from "#src/api/delete-guard.js";
|
|
8
|
+
|
|
9
|
+
const exec = promisify(execFile);
|
|
10
|
+
|
|
11
|
+
/** Host-only deletion dependencies; these are not agent parameters. */
|
|
12
|
+
export interface DeletePolicyContext {
|
|
13
|
+
readonly beforeDelete?: (event: BeforeDeleteEvent) => Promise<void>;
|
|
14
|
+
readonly confirm?: (event: BeforeDeleteEvent, reason: string) => Promise<boolean>;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
interface Project {
|
|
18
|
+
readonly root?: string;
|
|
19
|
+
readonly controls: readonly string[];
|
|
20
|
+
readonly gitAvailable: boolean;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** A host approval that must be checked again immediately before removal. */
|
|
24
|
+
export interface DeletionGuard {
|
|
25
|
+
readonly event: BeforeDeleteEvent;
|
|
26
|
+
readonly verify: () => Promise<void>;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** Check policy and hooks, obtain any required approval, then verify the same object again. */
|
|
30
|
+
export async function prepareDeletion(
|
|
31
|
+
source: string,
|
|
32
|
+
cwd: string,
|
|
33
|
+
context: DeletePolicyContext,
|
|
34
|
+
signal?: AbortSignal,
|
|
35
|
+
): Promise<BeforeDeleteEvent> {
|
|
36
|
+
const guard = await prepareDeletionGuard(source, cwd, context, signal, true);
|
|
37
|
+
await guard.verify();
|
|
38
|
+
return guard.event;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** Keep approval checks for multi-object removals; only Delete opts into temporary cleanup. */
|
|
42
|
+
export async function prepareDeletionGuard(
|
|
43
|
+
source: string,
|
|
44
|
+
cwd: string,
|
|
45
|
+
context: DeletePolicyContext,
|
|
46
|
+
signal?: AbortSignal,
|
|
47
|
+
allowTemporary = false,
|
|
48
|
+
): Promise<DeletionGuard> {
|
|
49
|
+
const before = await inspectDeletion(source, cwd, signal, allowTemporary);
|
|
50
|
+
await context.beforeDelete?.(before.event);
|
|
51
|
+
signal?.throwIfAborted();
|
|
52
|
+
if (before.reason !== undefined) {
|
|
53
|
+
if (context.confirm === undefined)
|
|
54
|
+
fail(
|
|
55
|
+
"DELETE_CONFIRMATION_REQUIRED",
|
|
56
|
+
`Deletion blocked: no user dialog is available. ${before.reason}`,
|
|
57
|
+
);
|
|
58
|
+
if (!(await context.confirm(before.event, before.reason)))
|
|
59
|
+
fail("DELETE_NOT_APPROVED", "Deletion was not approved by the user.");
|
|
60
|
+
}
|
|
61
|
+
signal?.throwIfAborted();
|
|
62
|
+
return {
|
|
63
|
+
event: before.event,
|
|
64
|
+
verify: async () => {
|
|
65
|
+
signal?.throwIfAborted();
|
|
66
|
+
let after;
|
|
67
|
+
try {
|
|
68
|
+
after = await inspectDeletion(source, cwd, signal, allowTemporary);
|
|
69
|
+
} catch (error) {
|
|
70
|
+
signal?.throwIfAborted();
|
|
71
|
+
fail(
|
|
72
|
+
"DELETE_TARGET_CHANGED",
|
|
73
|
+
`Deletion target cannot be verified again: ${error instanceof Error ? error.message : String(error)}`,
|
|
74
|
+
);
|
|
75
|
+
}
|
|
76
|
+
if (before.snapshot !== after.snapshot)
|
|
77
|
+
fail(
|
|
78
|
+
"DELETE_TARGET_CHANGED",
|
|
79
|
+
"Deletion target or its safety classification changed. Nothing was removed; make a new request.",
|
|
80
|
+
);
|
|
81
|
+
signal?.throwIfAborted();
|
|
82
|
+
},
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
async function inspectDeletion(
|
|
87
|
+
source: string,
|
|
88
|
+
cwd: string,
|
|
89
|
+
signal: AbortSignal | undefined,
|
|
90
|
+
allowTemporary: boolean,
|
|
91
|
+
) {
|
|
92
|
+
signal?.throwIfAborted();
|
|
93
|
+
// Resolve ancestors without dereferencing the link that will be unlinked.
|
|
94
|
+
const resolvedPath = path.join(await realpath(path.dirname(source)), path.basename(source));
|
|
95
|
+
const stat = await lstat(resolvedPath);
|
|
96
|
+
const kind = stat.isSymbolicLink()
|
|
97
|
+
? "symlink"
|
|
98
|
+
: stat.isDirectory()
|
|
99
|
+
? "directory"
|
|
100
|
+
: stat.isFile()
|
|
101
|
+
? "file"
|
|
102
|
+
: undefined;
|
|
103
|
+
if (kind === undefined)
|
|
104
|
+
fail("INVALID_FILE_TYPE", "Delete supports regular files, directories, and symlinks.");
|
|
105
|
+
const project = await findProject(cwd, signal);
|
|
106
|
+
const boundary = project.root ?? (await realpath(cwd));
|
|
107
|
+
assertUnprotected(resolvedPath, boundary, project.controls);
|
|
108
|
+
|
|
109
|
+
const temporaryRoots =
|
|
110
|
+
allowTemporary && kind !== "file" ? await loadTemporaryDirectories(boundary) : [];
|
|
111
|
+
const temporary =
|
|
112
|
+
!temporaryRoots.some((root) => path.relative(root, resolvedPath) === "") &&
|
|
113
|
+
temporaryRoots.some((root) => contains(root, resolvedPath));
|
|
114
|
+
let reason: string | undefined;
|
|
115
|
+
let tracked: string[] = [];
|
|
116
|
+
if (kind !== "file") {
|
|
117
|
+
if (!project.gitAvailable || project.root === undefined) {
|
|
118
|
+
// Broken Git metadata must not bypass the tracking check inside the current project.
|
|
119
|
+
if (!temporary || (project.root !== undefined && contains(project.root, resolvedPath)))
|
|
120
|
+
reason = "No Git worktree or reliable Git check is available.";
|
|
121
|
+
} else if (!contains(project.root, resolvedPath)) {
|
|
122
|
+
if (!temporary) reason = "Target is outside the current Git worktree.";
|
|
123
|
+
} else {
|
|
124
|
+
try {
|
|
125
|
+
const entries = (
|
|
126
|
+
await git(project.root, ["ls-files", "--cached", "--full-name", "-z"], signal)
|
|
127
|
+
)
|
|
128
|
+
.split("\0")
|
|
129
|
+
.filter(Boolean);
|
|
130
|
+
tracked = entries
|
|
131
|
+
.filter((entry) =>
|
|
132
|
+
contains(resolvedPath, path.resolve(requiredValue(project.root), entry)),
|
|
133
|
+
)
|
|
134
|
+
.sort();
|
|
135
|
+
if (tracked.length > 0)
|
|
136
|
+
reason = "Target is tracked by Git or contains tracked or staged entries.";
|
|
137
|
+
} catch {
|
|
138
|
+
signal?.throwIfAborted();
|
|
139
|
+
reason = "Git tracking check failed.";
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
const event: BeforeDeleteEvent = {
|
|
144
|
+
path: source,
|
|
145
|
+
resolvedPath,
|
|
146
|
+
cwd,
|
|
147
|
+
kind,
|
|
148
|
+
recursive: kind === "directory",
|
|
149
|
+
...(signal === undefined ? {} : { signal }),
|
|
150
|
+
};
|
|
151
|
+
return {
|
|
152
|
+
event,
|
|
153
|
+
reason,
|
|
154
|
+
snapshot: JSON.stringify({
|
|
155
|
+
resolvedPath,
|
|
156
|
+
kind,
|
|
157
|
+
dev: stat.dev,
|
|
158
|
+
ino: stat.ino,
|
|
159
|
+
birthtime: stat.birthtimeMs,
|
|
160
|
+
ctime: stat.ctimeMs,
|
|
161
|
+
mtime: stat.mtimeMs,
|
|
162
|
+
size: stat.size,
|
|
163
|
+
boundary,
|
|
164
|
+
controls: project.controls,
|
|
165
|
+
temporaryRoots,
|
|
166
|
+
reason,
|
|
167
|
+
tracked,
|
|
168
|
+
}),
|
|
169
|
+
};
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/** Protect recursive transfer destinations, including paths that do not exist yet. */
|
|
173
|
+
export async function assertUnprotectedTransferPath(
|
|
174
|
+
resolvedPath: string,
|
|
175
|
+
cwd: string,
|
|
176
|
+
signal?: AbortSignal,
|
|
177
|
+
): Promise<void> {
|
|
178
|
+
const project = await findProject(cwd, signal);
|
|
179
|
+
assertUnprotected(resolvedPath, project.root ?? (await realpath(cwd)), project.controls);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
function assertUnprotected(resolvedPath: string, boundary: string, controls: readonly string[]) {
|
|
183
|
+
if (
|
|
184
|
+
contains(resolvedPath, boundary) ||
|
|
185
|
+
resolvedPath === path.parse(resolvedPath).root ||
|
|
186
|
+
controls.some((control) => contains(control, resolvedPath) || contains(resolvedPath, control))
|
|
187
|
+
)
|
|
188
|
+
fail(
|
|
189
|
+
"DELETE_PROTECTED_TARGET",
|
|
190
|
+
`Cannot remove or overwrite protected project, ancestor, filesystem root, or Git control path: ${resolvedPath}`,
|
|
191
|
+
);
|
|
192
|
+
}
|
|
193
|
+
async function findProject(cwd: string, signal?: AbortSignal): Promise<Project> {
|
|
194
|
+
try {
|
|
195
|
+
const output = (
|
|
196
|
+
await git(
|
|
197
|
+
cwd,
|
|
198
|
+
[
|
|
199
|
+
"rev-parse",
|
|
200
|
+
"--path-format=absolute",
|
|
201
|
+
"--show-toplevel",
|
|
202
|
+
"--absolute-git-dir",
|
|
203
|
+
"--git-common-dir",
|
|
204
|
+
],
|
|
205
|
+
signal,
|
|
206
|
+
)
|
|
207
|
+
)
|
|
208
|
+
.trimEnd()
|
|
209
|
+
.split("\n");
|
|
210
|
+
if (output.length !== 3) throw new Error("Unexpected Git worktree paths");
|
|
211
|
+
const [root, gitDir, commonDir] = await Promise.all(output.map((entry) => realpath(entry)));
|
|
212
|
+
return {
|
|
213
|
+
root,
|
|
214
|
+
controls: [
|
|
215
|
+
path.join(requiredValue(root), ".git"),
|
|
216
|
+
requiredValue(gitDir),
|
|
217
|
+
requiredValue(commonDir),
|
|
218
|
+
].sort(),
|
|
219
|
+
gitAvailable: true,
|
|
220
|
+
};
|
|
221
|
+
} catch {
|
|
222
|
+
signal?.throwIfAborted();
|
|
223
|
+
// Preserve root/control protection even when Git is unavailable or its metadata is broken.
|
|
224
|
+
let directory = await realpath(cwd);
|
|
225
|
+
for (;;) {
|
|
226
|
+
const marker = path.join(directory, ".git");
|
|
227
|
+
const stat = await lstat(marker).catch((error: NodeJS.ErrnoException) => {
|
|
228
|
+
if (error.code === "ENOENT") return undefined;
|
|
229
|
+
throw error;
|
|
230
|
+
});
|
|
231
|
+
if (stat !== undefined) {
|
|
232
|
+
const controls = [marker];
|
|
233
|
+
if (stat.isDirectory() || stat.isSymbolicLink()) controls.push(await realpath(marker));
|
|
234
|
+
else if (stat.isFile()) {
|
|
235
|
+
const content = await readFile(marker, "utf8");
|
|
236
|
+
const reference = /^gitdir: (.+)\r?\n?$/u.exec(content)?.[1];
|
|
237
|
+
if (reference === undefined)
|
|
238
|
+
fail("DELETE_GIT_PROTECTION_FAILED", "Cannot identify current Git control data.");
|
|
239
|
+
const gitDir = await realpath(path.resolve(directory, reference));
|
|
240
|
+
controls.push(gitDir);
|
|
241
|
+
const common = await readFile(path.join(gitDir, "commondir"), "utf8").catch(
|
|
242
|
+
(error: NodeJS.ErrnoException) => {
|
|
243
|
+
if (error.code === "ENOENT") return undefined;
|
|
244
|
+
throw error;
|
|
245
|
+
},
|
|
246
|
+
);
|
|
247
|
+
if (common !== undefined)
|
|
248
|
+
controls.push(await realpath(path.resolve(gitDir, common.trimEnd())));
|
|
249
|
+
} else fail("DELETE_GIT_PROTECTION_FAILED", "Cannot identify current Git control data.");
|
|
250
|
+
return { root: directory, controls: controls.sort(), gitAvailable: false };
|
|
251
|
+
}
|
|
252
|
+
const parent = path.dirname(directory);
|
|
253
|
+
if (parent === directory) return { controls: [], gitAvailable: false };
|
|
254
|
+
directory = parent;
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
async function git(cwd: string, args: string[], signal?: AbortSignal): Promise<string> {
|
|
260
|
+
const env = Object.fromEntries(
|
|
261
|
+
Object.entries(process.env).filter(([key]) => !key.startsWith("GIT_")),
|
|
262
|
+
);
|
|
263
|
+
const { stdout } = await exec("git", ["-C", cwd, ...args], {
|
|
264
|
+
env: { ...env, GIT_OPTIONAL_LOCKS: "0" },
|
|
265
|
+
signal,
|
|
266
|
+
timeout: 5000,
|
|
267
|
+
maxBuffer: 16 * 1024 * 1024,
|
|
268
|
+
});
|
|
269
|
+
return stdout;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
function contains(parent: string, child: string): boolean {
|
|
273
|
+
const relative = path.relative(parent, child);
|
|
274
|
+
return (
|
|
275
|
+
relative === "" ||
|
|
276
|
+
// This checks containment; it does not construct a parent-relative path.
|
|
277
|
+
// eslint-disable-next-line repo/no-parent-paths
|
|
278
|
+
(!path.isAbsolute(relative) && relative !== ".." && !relative.startsWith(`..${path.sep}`))
|
|
279
|
+
);
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
function fail(code: string, message: string): never {
|
|
283
|
+
throw Object.assign(new Error(message), { code });
|
|
284
|
+
}
|
|
@@ -33,10 +33,42 @@ export const diffDataSchema = Type.Object(
|
|
|
33
33
|
);
|
|
34
34
|
export const diffOutputSchema = structuredResultSchema(diffDataSchema);
|
|
35
35
|
|
|
36
|
-
const
|
|
36
|
+
const comparisonReadParameters = {
|
|
37
|
+
...readParameters,
|
|
38
|
+
properties: {
|
|
39
|
+
...readParameters.properties,
|
|
40
|
+
path: {
|
|
41
|
+
...readParameters.properties.path,
|
|
42
|
+
description:
|
|
43
|
+
"Text-readable source accepted by Read, such as a file path, URL, temp: reference, SEARCH# or RESULT# reference, or registered resource. Supply a path.",
|
|
44
|
+
},
|
|
45
|
+
offset: {
|
|
46
|
+
...readParameters.properties.offset,
|
|
47
|
+
description:
|
|
48
|
+
"First text line to compare, numbered from 1. Omit or use 0 to start at line 1; negative offsets count from the end. For path#anchor, SEARCH# or RESULT# selections, count from the containing line: omitted, 0 or 1 starts there, 2 starts one line later, -1 one line earlier.",
|
|
49
|
+
},
|
|
50
|
+
limit: {
|
|
51
|
+
...readParameters.properties.limit,
|
|
52
|
+
description:
|
|
53
|
+
"Maximum text lines to compare from the selected starting position. Omit to compare all resolved text in the selected range. Read's display limits do not shorten the comparison inputs.",
|
|
54
|
+
},
|
|
55
|
+
},
|
|
56
|
+
};
|
|
57
|
+
const source = Type.Union([Type.String({ minLength: 1 }), comparisonReadParameters]);
|
|
37
58
|
/** Each side accepts a source string or the same request object as read. */
|
|
38
59
|
export const diffParameters = Type.Object(
|
|
39
|
-
{
|
|
60
|
+
{
|
|
61
|
+
before: {
|
|
62
|
+
...source,
|
|
63
|
+
description:
|
|
64
|
+
"Source for the removed (-) side. Supply a non-empty source reference string or a Read request object. Strings identify sources, not literal text.",
|
|
65
|
+
},
|
|
66
|
+
after: {
|
|
67
|
+
...source,
|
|
68
|
+
description:
|
|
69
|
+
"Source for the added (+) side. Supply a non-empty source reference string or a Read request object. Strings identify sources, not literal text.",
|
|
70
|
+
},
|
|
71
|
+
},
|
|
40
72
|
{ additionalProperties: false },
|
|
41
73
|
);
|
|
42
74
|
export type DiffRequest = Static<typeof diffParameters>;
|
|
@@ -107,13 +139,21 @@ export function comparisonText(data: ReadScriptData, requested: string): Compari
|
|
|
107
139
|
};
|
|
108
140
|
}
|
|
109
141
|
if (data.kind === "bytes")
|
|
110
|
-
throw Object.assign(
|
|
111
|
-
|
|
112
|
-
|
|
142
|
+
throw Object.assign(
|
|
143
|
+
new Error(
|
|
144
|
+
`Byte sources are not text: ${data.source}\nIf this is a text file, use its path without raw:.`,
|
|
145
|
+
),
|
|
146
|
+
{
|
|
147
|
+
code: "UNSUPPORTED_CONTENT",
|
|
148
|
+
},
|
|
149
|
+
);
|
|
113
150
|
if (data.blocks.length === 0 || data.blocks.some((block) => block.type !== "text"))
|
|
114
|
-
throw Object.assign(
|
|
115
|
-
|
|
116
|
-
|
|
151
|
+
throw Object.assign(
|
|
152
|
+
new Error(`Source is not text: ${data.source}\nUse a source that Read resolves to text.`),
|
|
153
|
+
{
|
|
154
|
+
code: "UNSUPPORTED_CONTENT",
|
|
155
|
+
},
|
|
156
|
+
);
|
|
117
157
|
return {
|
|
118
158
|
source: data.source,
|
|
119
159
|
sources: [data.source],
|
|
@@ -166,8 +206,7 @@ export function registerDiff(
|
|
|
166
206
|
parameters: diffParameters,
|
|
167
207
|
outputSchema: diffOutputSchema,
|
|
168
208
|
promptSnippet: "Compare two text-readable sources",
|
|
169
|
-
description:
|
|
170
|
-
"Use diff to compare two sources. before and after accept a source string or a read request {path, offset?, limit?, views?}. Use any source that read can resolve as text. Omit limit to compare complete resolved text; read presentation limits do not clip comparison inputs. Multiple resolved resources are joined with one newline separator in resolver order. Diff line numbers are relative to each selected text, not the original file when a window is selected. Native non-text content is rejected. Output uses the existing text diff and a shared output budget; oversized output has a full temporary reference.",
|
|
209
|
+
description: "Use diff to compare two text-readable sources without changing them.",
|
|
171
210
|
renderCall(_args, theme) {
|
|
172
211
|
return new Text(theme.fg("toolTitle", theme.bold("Diff")), 0, 0);
|
|
173
212
|
},
|
|
@@ -195,7 +234,10 @@ export function registerDiff(
|
|
|
195
234
|
const full = diffText(comparison);
|
|
196
235
|
const bounded = truncateHead(full);
|
|
197
236
|
const temporarySource = bounded.truncated ? await read.saveTemporary(full) : undefined;
|
|
198
|
-
const footer =
|
|
237
|
+
const footer =
|
|
238
|
+
temporarySource === undefined
|
|
239
|
+
? ""
|
|
240
|
+
: `\nDiff output is truncated; the comparison used the complete resolved inputs.\nUse read with path "${temporarySource}" for the full diff.`;
|
|
199
241
|
return withStructuredResult(
|
|
200
242
|
{
|
|
201
243
|
content: [
|
|
@@ -205,7 +247,7 @@ export function registerDiff(
|
|
|
205
247
|
temporarySource === undefined
|
|
206
248
|
? full
|
|
207
249
|
: truncateHead(full, {
|
|
208
|
-
maxLines: DEFAULT_MAX_LINES -
|
|
250
|
+
maxLines: DEFAULT_MAX_LINES - footer.split("\n").length,
|
|
209
251
|
maxBytes: DEFAULT_MAX_BYTES - Buffer.byteLength(footer),
|
|
210
252
|
}).content + footer,
|
|
211
253
|
},
|
|
@@ -4,5 +4,5 @@ export const EDITING_GUIDELINES = [
|
|
|
4
4
|
"Use an available anchor when it selects exactly the intended text; otherwise use minimal unique exact text. Keep the edit limited to the intended content.",
|
|
5
5
|
"When search broadens to separate words, treat its results as location hints. Refine the query before using those matches for replacement.",
|
|
6
6
|
"Use standalone mutation tools in one assistant-response batch when each change is known in advance and does not depend on another change. This is the default for several independent edits in one file or across files. Every call is evaluated against the original file snapshots. Combine overlapping changes into one mutation. Use native Codemode for computed or dependent tool composition; it keeps the same tool contracts and does not add grouped rollback. Check each batch result and retry only unapplied changes.",
|
|
7
|
-
"Inside
|
|
7
|
+
"Inside Codemode, run independent edits on different resources concurrently; run edits on the same resource in order. Check the final Codemode result and retry only unapplied changes. Read docs:editing when you need a write report inside the script.",
|
|
8
8
|
] as const;
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import path from "node:path";
|
|
1
2
|
import type { TextEditorCore } from "#src/core/text-editor-core.js";
|
|
2
3
|
import type { FileOperation, FileOperationResult } from "#src/core/file-operations.js";
|
|
3
4
|
import { executeFileOperation } from "#src/core/file-operations.js";
|
|
@@ -16,6 +17,7 @@ export function isWholeFileInvocation(
|
|
|
16
17
|
input.path.length === 0 ||
|
|
17
18
|
input.path.startsWith("SEARCH#") ||
|
|
18
19
|
input.path.startsWith("RESULT#") ||
|
|
20
|
+
(operation === "delete" && input.path.startsWith("symbol:")) ||
|
|
19
21
|
(typeof input.target === "string" && input.target.startsWith("RESULT#"))
|
|
20
22
|
)
|
|
21
23
|
return false;
|
|
@@ -29,9 +31,13 @@ export async function executeWholeFileTool(
|
|
|
29
31
|
operation: FileOperation,
|
|
30
32
|
input: Readonly<Record<string, unknown>>,
|
|
31
33
|
signal: AbortSignal | undefined,
|
|
32
|
-
context: Pick<ExtensionContext, "cwd"
|
|
34
|
+
context: Pick<ExtensionContext, "cwd"> & {
|
|
35
|
+
readonly hasUI?: boolean;
|
|
36
|
+
readonly ui?: Pick<ExtensionContext["ui"], "confirm">;
|
|
37
|
+
},
|
|
33
38
|
verifySource?: () => Promise<void>,
|
|
34
39
|
): Promise<AgentToolResult<FileMutationBatchResult>> {
|
|
40
|
+
const ui = context.hasUI ? context.ui : undefined;
|
|
35
41
|
const outcome = await core.enqueueFileOperation(
|
|
36
42
|
async (): Promise<FileOperationResult> => {
|
|
37
43
|
try {
|
|
@@ -51,7 +57,30 @@ export async function executeWholeFileTool(
|
|
|
51
57
|
},
|
|
52
58
|
};
|
|
53
59
|
}
|
|
54
|
-
return executeFileOperation(operation, input, context.cwd, signal
|
|
60
|
+
return executeFileOperation(operation, input, context.cwd, signal, {
|
|
61
|
+
beforeDelete: (event) => core.beforeDelete(event),
|
|
62
|
+
...(ui !== undefined && {
|
|
63
|
+
confirm: async (event, reason) =>
|
|
64
|
+
ui.confirm(
|
|
65
|
+
operation === "move" ? "Move filesystem object?" : "Delete permanently?",
|
|
66
|
+
[
|
|
67
|
+
event.path,
|
|
68
|
+
event.resolvedPath === event.path
|
|
69
|
+
? undefined
|
|
70
|
+
: `Resolved path: ${event.resolvedPath}`,
|
|
71
|
+
operation === "move" && event.path === path.resolve(context.cwd, String(input.path))
|
|
72
|
+
? `Move source to ${String(input.target)}; preserve link objects without following their targets.`
|
|
73
|
+
: event.recursive
|
|
74
|
+
? "Remove directory and all contents recursively."
|
|
75
|
+
: "Unlink symlink only; leave its target untouched.",
|
|
76
|
+
reason,
|
|
77
|
+
]
|
|
78
|
+
.filter((line) => line !== undefined)
|
|
79
|
+
.join("\n"),
|
|
80
|
+
{ signal },
|
|
81
|
+
),
|
|
82
|
+
}),
|
|
83
|
+
});
|
|
55
84
|
},
|
|
56
85
|
signal,
|
|
57
86
|
{
|
|
@@ -64,7 +93,7 @@ export async function executeWholeFileTool(
|
|
|
64
93
|
if (outcome.ok && operation !== "copy" && outcome.path !== undefined)
|
|
65
94
|
forgetDeferredPostEdit(outcome.path);
|
|
66
95
|
let postProcessingError: string | undefined;
|
|
67
|
-
if (outcome.ok && outcome.target !== undefined) {
|
|
96
|
+
if (outcome.ok && outcome.target !== undefined && outcome.sourceKind === "file") {
|
|
68
97
|
try {
|
|
69
98
|
await core.postProcessFile(outcome.target, { cwd: context.cwd, signal });
|
|
70
99
|
} catch (error) {
|
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { rm, unlink } from "node:fs/promises";
|
|
2
|
+
import { prepareFileTransfer, type TransferKind } from "./file-transfers.js";
|
|
3
|
+
import { prepareDeletion, type DeletePolicyContext } from "./delete-policy.js";
|
|
2
4
|
import path from "node:path";
|
|
3
5
|
import fs from "fs-extra";
|
|
4
6
|
import { Type } from "typebox";
|
|
@@ -9,7 +11,7 @@ export const fileOperations = ["delete", "move", "copy"] as const;
|
|
|
9
11
|
export type FileOperation = (typeof fileOperations)[number];
|
|
10
12
|
const filePath = Type.String({
|
|
11
13
|
minLength: 1,
|
|
12
|
-
description: "Local
|
|
14
|
+
description: "Local file, directory, or symlink path, relative to cwd or absolute.",
|
|
13
15
|
});
|
|
14
16
|
export const deleteFileParameters = Type.Object(
|
|
15
17
|
{ path: filePath },
|
|
@@ -23,6 +25,10 @@ export const transferFileParameters = Type.Object(
|
|
|
23
25
|
{ additionalProperties: false },
|
|
24
26
|
);
|
|
25
27
|
|
|
28
|
+
/** Host dependencies for deletion; no tool argument can replace the filesystem primitive. */
|
|
29
|
+
export interface FileOperationContext extends DeletePolicyContext {
|
|
30
|
+
readonly removeDirectory?: (source: string) => Promise<void>;
|
|
31
|
+
}
|
|
26
32
|
/** Plain receipt; unknown means the filesystem call failed after execution began. */
|
|
27
33
|
export interface FileOperationResult {
|
|
28
34
|
readonly kind: "file-operation";
|
|
@@ -31,6 +37,7 @@ export interface FileOperationResult {
|
|
|
31
37
|
readonly effect: "applied" | "not-applied" | "unknown";
|
|
32
38
|
readonly path?: string;
|
|
33
39
|
readonly target?: string;
|
|
40
|
+
readonly sourceKind?: TransferKind;
|
|
34
41
|
readonly error?: { readonly code: string; readonly message: string };
|
|
35
42
|
}
|
|
36
43
|
|
|
@@ -55,16 +62,18 @@ export function formatFileOperation(value: FileOperationResult): string {
|
|
|
55
62
|
.filter((line) => line !== undefined)
|
|
56
63
|
.join("\n");
|
|
57
64
|
}
|
|
58
|
-
/** Uses
|
|
65
|
+
/** Uses filesystem primitives without decoding contents; Delete applies hooks and safety policy first. */
|
|
59
66
|
export async function executeFileOperation(
|
|
60
67
|
operation: FileOperation,
|
|
61
68
|
input: unknown,
|
|
62
69
|
cwd: string,
|
|
63
70
|
signal?: AbortSignal,
|
|
71
|
+
deletion: FileOperationContext = {},
|
|
64
72
|
): Promise<FileOperationResult> {
|
|
65
73
|
let started = false;
|
|
66
74
|
let source: string | undefined;
|
|
67
75
|
let target: string | undefined;
|
|
76
|
+
let sourceKind: TransferKind | undefined;
|
|
68
77
|
try {
|
|
69
78
|
const schema = operation === "delete" ? deleteFileParameters : transferFileParameters;
|
|
70
79
|
if (!Value.Check(schema, input))
|
|
@@ -75,29 +84,25 @@ export async function executeFileOperation(
|
|
|
75
84
|
source = path.resolve(cwd, args.path);
|
|
76
85
|
target = args.target === undefined ? undefined : path.resolve(cwd, args.target);
|
|
77
86
|
signal?.throwIfAborted();
|
|
78
|
-
const
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
});
|
|
86
|
-
if (
|
|
87
|
-
source === target ||
|
|
88
|
-
(sourceStat.ino === targetStat.ino && sourceStat.dev === targetStat.dev)
|
|
89
|
-
)
|
|
90
|
-
throw Object.assign(new Error("Source and target are the same file"), {
|
|
91
|
-
code: "SAME_FILE",
|
|
92
|
-
});
|
|
93
|
-
}
|
|
94
|
-
}
|
|
87
|
+
const deleteEvent =
|
|
88
|
+
operation === "delete" ? await prepareDeletion(source, cwd, deletion, signal) : undefined;
|
|
89
|
+
const transfer =
|
|
90
|
+
operation !== "delete" && target !== undefined
|
|
91
|
+
? await prepareFileTransfer(operation, source, target, cwd, deletion, signal)
|
|
92
|
+
: undefined;
|
|
93
|
+
sourceKind = transfer?.sourceKind;
|
|
95
94
|
signal?.throwIfAborted();
|
|
96
95
|
started = true;
|
|
97
|
-
if (
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
96
|
+
if (deleteEvent !== undefined) {
|
|
97
|
+
if (deleteEvent.recursive) {
|
|
98
|
+
if (deletion.removeDirectory !== undefined)
|
|
99
|
+
await deletion.removeDirectory(deleteEvent.resolvedPath);
|
|
100
|
+
else await rm(deleteEvent.resolvedPath, { recursive: true, force: false });
|
|
101
|
+
} else await unlink(deleteEvent.resolvedPath);
|
|
102
|
+
} else if (transfer !== undefined) {
|
|
103
|
+
if (operation === "copy")
|
|
104
|
+
await fs.copy(transfer.source, transfer.target, { overwrite: true, dereference: false });
|
|
105
|
+
else await fs.move(transfer.source, transfer.target, { overwrite: true });
|
|
101
106
|
}
|
|
102
107
|
return {
|
|
103
108
|
kind: "file-operation",
|
|
@@ -106,6 +111,7 @@ export async function executeFileOperation(
|
|
|
106
111
|
effect: "applied",
|
|
107
112
|
path: source,
|
|
108
113
|
...(target === undefined ? {} : { target }),
|
|
114
|
+
...(sourceKind === undefined ? {} : { sourceKind }),
|
|
109
115
|
};
|
|
110
116
|
} catch (error) {
|
|
111
117
|
return {
|
|
@@ -125,19 +131,3 @@ export async function executeFileOperation(
|
|
|
125
131
|
};
|
|
126
132
|
}
|
|
127
133
|
}
|
|
128
|
-
|
|
129
|
-
async function regularFile(file: string) {
|
|
130
|
-
const stat = await lstat(file);
|
|
131
|
-
if (!stat.isFile() || stat.isSymbolicLink())
|
|
132
|
-
throw Object.assign(new Error("Source must be a regular file"), { code: "INVALID_FILE_TYPE" });
|
|
133
|
-
return stat;
|
|
134
|
-
}
|
|
135
|
-
async function optionalStat(file: string) {
|
|
136
|
-
try {
|
|
137
|
-
return await lstat(file);
|
|
138
|
-
} catch (error) {
|
|
139
|
-
if (error !== null && typeof error === "object" && "code" in error && error.code === "ENOENT")
|
|
140
|
-
return undefined;
|
|
141
|
-
throw error;
|
|
142
|
-
}
|
|
143
|
-
}
|
|
@@ -30,7 +30,7 @@ export async function attachFileMutationTargets(
|
|
|
30
30
|
const stat = await lstat(file);
|
|
31
31
|
states.push({ source: file, state: "present" });
|
|
32
32
|
if (!stat.isFile() || stat.isSymbolicLink())
|
|
33
|
-
throw new Error("
|
|
33
|
+
throw new Error("Target is not a regular text file; no text selection is available.");
|
|
34
34
|
const bytes = await readFile(file, { signal });
|
|
35
35
|
const content = new TextDecoder("utf-8", { fatal: true, ignoreBOM: true }).decode(bytes);
|
|
36
36
|
if (content.includes("\0")) throw new Error("Binary file has no supported text target.");
|