@wahack/pi-coding-agent 15.11.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +10031 -0
- package/README.md +36 -0
- package/examples/README.md +21 -0
- package/examples/custom-tools/README.md +104 -0
- package/examples/custom-tools/hello/index.ts +20 -0
- package/examples/extensions/README.md +142 -0
- package/examples/extensions/api-demo.ts +79 -0
- package/examples/extensions/chalk-logger.ts +25 -0
- package/examples/extensions/hello.ts +31 -0
- package/examples/extensions/pirate.ts +43 -0
- package/examples/extensions/plan-mode.ts +549 -0
- package/examples/extensions/reload-runtime.ts +38 -0
- package/examples/extensions/thinking-note.ts +13 -0
- package/examples/extensions/tools.ts +145 -0
- package/examples/extensions/with-deps/index.ts +36 -0
- package/examples/extensions/with-deps/package-lock.json +31 -0
- package/examples/extensions/with-deps/package.json +17 -0
- package/examples/hooks/README.md +56 -0
- package/examples/hooks/auto-commit-on-exit.ts +48 -0
- package/examples/hooks/confirm-destructive.ts +58 -0
- package/examples/hooks/custom-compaction.ts +115 -0
- package/examples/hooks/dirty-repo-guard.ts +51 -0
- package/examples/hooks/file-trigger.ts +40 -0
- package/examples/hooks/git-checkpoint.ts +52 -0
- package/examples/hooks/handoff.ts +149 -0
- package/examples/hooks/permission-gate.ts +33 -0
- package/examples/hooks/protected-paths.ts +29 -0
- package/examples/hooks/qna.ts +118 -0
- package/examples/hooks/status-line.ts +39 -0
- package/examples/sdk/01-minimal.ts +21 -0
- package/examples/sdk/02-custom-model.ts +49 -0
- package/examples/sdk/03-custom-prompt.ts +46 -0
- package/examples/sdk/04-skills.ts +43 -0
- package/examples/sdk/06-extensions.ts +82 -0
- package/examples/sdk/06-hooks.ts +61 -0
- package/examples/sdk/07-context-files.ts +35 -0
- package/examples/sdk/08-prompt-templates.ts +41 -0
- package/examples/sdk/08-slash-commands.ts +46 -0
- package/examples/sdk/09-api-keys-and-oauth.ts +54 -0
- package/examples/sdk/11-sessions.ts +47 -0
- package/examples/sdk/12-redis-sessions.ts +54 -0
- package/examples/sdk/13-sql-sessions.ts +61 -0
- package/examples/sdk/README.md +172 -0
- package/package.json +554 -0
- package/scripts/build-binary.ts +100 -0
- package/scripts/bundle-dist.ts +90 -0
- package/scripts/format-prompts.ts +68 -0
- package/scripts/generate-docs-index.ts +40 -0
- package/scripts/generate-template.ts +33 -0
- package/scripts/omp +42 -0
- package/scripts/omp.ts +19 -0
- package/src/async/index.ts +1 -0
- package/src/async/job-manager.ts +625 -0
- package/src/auto-thinking/classifier.ts +185 -0
- package/src/autoresearch/command-resume.md +14 -0
- package/src/autoresearch/dashboard.ts +436 -0
- package/src/autoresearch/git.ts +319 -0
- package/src/autoresearch/helpers.ts +218 -0
- package/src/autoresearch/index.ts +536 -0
- package/src/autoresearch/prompt-setup.md +43 -0
- package/src/autoresearch/prompt.md +103 -0
- package/src/autoresearch/resume-message.md +10 -0
- package/src/autoresearch/state.ts +273 -0
- package/src/autoresearch/storage.ts +699 -0
- package/src/autoresearch/tools/init-experiment.ts +272 -0
- package/src/autoresearch/tools/log-experiment.ts +524 -0
- package/src/autoresearch/tools/run-experiment.ts +407 -0
- package/src/autoresearch/tools/update-notes.ts +109 -0
- package/src/autoresearch/types.ts +168 -0
- package/src/bun-imports.d.ts +28 -0
- package/src/capability/context-file.ts +44 -0
- package/src/capability/extension-module.ts +34 -0
- package/src/capability/extension.ts +47 -0
- package/src/capability/fs.ts +117 -0
- package/src/capability/hook.ts +40 -0
- package/src/capability/index.ts +436 -0
- package/src/capability/instruction.ts +37 -0
- package/src/capability/mcp.ts +74 -0
- package/src/capability/prompt.ts +35 -0
- package/src/capability/rule-buckets.ts +66 -0
- package/src/capability/rule.ts +261 -0
- package/src/capability/settings.ts +34 -0
- package/src/capability/skill.ts +63 -0
- package/src/capability/slash-command.ts +40 -0
- package/src/capability/ssh.ts +41 -0
- package/src/capability/system-prompt.ts +34 -0
- package/src/capability/tool.ts +38 -0
- package/src/capability/types.ts +168 -0
- package/src/cli/agents-cli.ts +138 -0
- package/src/cli/args.ts +340 -0
- package/src/cli/auth-broker-cli.ts +895 -0
- package/src/cli/auth-gateway-cli.ts +611 -0
- package/src/cli/classify-install-target.ts +76 -0
- package/src/cli/claude-trace-cli.ts +795 -0
- package/src/cli/commands/init-xdg.ts +27 -0
- package/src/cli/completion-gen.ts +550 -0
- package/src/cli/config-cli.ts +418 -0
- package/src/cli/dry-balance-cli.ts +856 -0
- package/src/cli/extension-flags.ts +48 -0
- package/src/cli/file-processor.ts +133 -0
- package/src/cli/gallery-cli.ts +230 -0
- package/src/cli/gallery-fixtures/agentic.ts +407 -0
- package/src/cli/gallery-fixtures/codeintel.ts +187 -0
- package/src/cli/gallery-fixtures/edit.ts +194 -0
- package/src/cli/gallery-fixtures/fs.ts +220 -0
- package/src/cli/gallery-fixtures/index.ts +40 -0
- package/src/cli/gallery-fixtures/interaction.ts +49 -0
- package/src/cli/gallery-fixtures/memory.ts +81 -0
- package/src/cli/gallery-fixtures/misc.ts +250 -0
- package/src/cli/gallery-fixtures/search.ts +213 -0
- package/src/cli/gallery-fixtures/shell.ts +167 -0
- package/src/cli/gallery-fixtures/types.ts +57 -0
- package/src/cli/gallery-fixtures/web.ts +158 -0
- package/src/cli/gallery-screenshot.ts +279 -0
- package/src/cli/grep-cli.ts +160 -0
- package/src/cli/grievances-cli.ts +256 -0
- package/src/cli/initial-message.ts +58 -0
- package/src/cli/list-models.ts +194 -0
- package/src/cli/plugin-cli.ts +996 -0
- package/src/cli/read-cli.ts +57 -0
- package/src/cli/session-picker.ts +79 -0
- package/src/cli/setup-cli.ts +231 -0
- package/src/cli/shell-cli.ts +176 -0
- package/src/cli/ssh-cli.ts +179 -0
- package/src/cli/startup-cwd.ts +68 -0
- package/src/cli/stats-cli.ts +238 -0
- package/src/cli/tiny-models-cli.ts +127 -0
- package/src/cli/update-cli.ts +611 -0
- package/src/cli/usage-cli.ts +603 -0
- package/src/cli/web-search-cli.ts +132 -0
- package/src/cli/worktree-cli.ts +291 -0
- package/src/cli-commands.ts +79 -0
- package/src/cli.ts +200 -0
- package/src/commands/acp.ts +24 -0
- package/src/commands/agents.ts +57 -0
- package/src/commands/auth-broker.ts +99 -0
- package/src/commands/auth-gateway.ts +69 -0
- package/src/commands/commit.ts +46 -0
- package/src/commands/complete.ts +66 -0
- package/src/commands/completions.ts +60 -0
- package/src/commands/config.ts +51 -0
- package/src/commands/dry-balance.ts +43 -0
- package/src/commands/gallery.ts +52 -0
- package/src/commands/grep.ts +48 -0
- package/src/commands/grievances.ts +51 -0
- package/src/commands/install.ts +107 -0
- package/src/commands/launch.ts +169 -0
- package/src/commands/plugin.ts +78 -0
- package/src/commands/read.ts +38 -0
- package/src/commands/setup.ts +67 -0
- package/src/commands/shell.ts +29 -0
- package/src/commands/ssh.ts +60 -0
- package/src/commands/stats.ts +29 -0
- package/src/commands/tiny-models.ts +36 -0
- package/src/commands/update.ts +21 -0
- package/src/commands/usage.ts +35 -0
- package/src/commands/web-search.ts +42 -0
- package/src/commands/worktree.ts +56 -0
- package/src/commit/agentic/agent.ts +317 -0
- package/src/commit/agentic/fallback.ts +96 -0
- package/src/commit/agentic/index.ts +355 -0
- package/src/commit/agentic/prompts/analyze-file.md +22 -0
- package/src/commit/agentic/prompts/session-user.md +25 -0
- package/src/commit/agentic/prompts/split-confirm.md +1 -0
- package/src/commit/agentic/prompts/system.md +38 -0
- package/src/commit/agentic/state.ts +60 -0
- package/src/commit/agentic/tools/analyze-file.ts +146 -0
- package/src/commit/agentic/tools/git-file-diff.ts +191 -0
- package/src/commit/agentic/tools/git-hunk.ts +50 -0
- package/src/commit/agentic/tools/git-overview.ts +81 -0
- package/src/commit/agentic/tools/index.ts +54 -0
- package/src/commit/agentic/tools/propose-changelog.ts +144 -0
- package/src/commit/agentic/tools/propose-commit.ts +109 -0
- package/src/commit/agentic/tools/recent-commits.ts +81 -0
- package/src/commit/agentic/tools/schemas.ts +23 -0
- package/src/commit/agentic/tools/split-commit.ts +245 -0
- package/src/commit/agentic/topo-sort.ts +44 -0
- package/src/commit/agentic/trivial.ts +51 -0
- package/src/commit/agentic/validation.ts +183 -0
- package/src/commit/analysis/conventional.ts +64 -0
- package/src/commit/analysis/index.ts +4 -0
- package/src/commit/analysis/scope.ts +242 -0
- package/src/commit/analysis/summary.ts +105 -0
- package/src/commit/analysis/validation.ts +66 -0
- package/src/commit/changelog/detect.ts +40 -0
- package/src/commit/changelog/generate.ts +97 -0
- package/src/commit/changelog/index.ts +234 -0
- package/src/commit/changelog/parse.ts +44 -0
- package/src/commit/cli.ts +85 -0
- package/src/commit/git/diff.ts +148 -0
- package/src/commit/index.ts +5 -0
- package/src/commit/map-reduce/index.ts +69 -0
- package/src/commit/map-reduce/map-phase.ts +193 -0
- package/src/commit/map-reduce/reduce-phase.ts +49 -0
- package/src/commit/map-reduce/utils.ts +9 -0
- package/src/commit/message.ts +11 -0
- package/src/commit/model-selection.ts +92 -0
- package/src/commit/pipeline.ts +243 -0
- package/src/commit/prompts/analysis-system.md +148 -0
- package/src/commit/prompts/analysis-user.md +38 -0
- package/src/commit/prompts/changelog-system.md +50 -0
- package/src/commit/prompts/changelog-user.md +18 -0
- package/src/commit/prompts/file-observer-system.md +24 -0
- package/src/commit/prompts/file-observer-user.md +8 -0
- package/src/commit/prompts/reduce-system.md +50 -0
- package/src/commit/prompts/reduce-user.md +17 -0
- package/src/commit/prompts/summary-retry.md +3 -0
- package/src/commit/prompts/summary-system.md +38 -0
- package/src/commit/prompts/summary-user.md +13 -0
- package/src/commit/prompts/types-description.md +2 -0
- package/src/commit/shared-llm.ts +77 -0
- package/src/commit/types.ts +118 -0
- package/src/commit/utils/exclusions.ts +42 -0
- package/src/commit/utils.ts +58 -0
- package/src/config/api-key-resolver.ts +60 -0
- package/src/config/append-only-context-mode.ts +31 -0
- package/src/config/config-file.ts +317 -0
- package/src/config/file-lock.ts +164 -0
- package/src/config/keybindings.ts +628 -0
- package/src/config/mcp-schema.json +230 -0
- package/src/config/model-discovery.ts +554 -0
- package/src/config/model-registry.ts +2090 -0
- package/src/config/model-resolver.ts +1502 -0
- package/src/config/model-roles.ts +74 -0
- package/src/config/models-config-schema.ts +226 -0
- package/src/config/models-config.ts +129 -0
- package/src/config/prompt-templates.ts +185 -0
- package/src/config/resolve-config-value.ts +94 -0
- package/src/config/settings-schema.ts +3530 -0
- package/src/config/settings.ts +1178 -0
- package/src/config.ts +242 -0
- package/src/cursor.ts +340 -0
- package/src/dap/client.ts +760 -0
- package/src/dap/config.ts +189 -0
- package/src/dap/defaults.json +212 -0
- package/src/dap/index.ts +4 -0
- package/src/dap/session.ts +1441 -0
- package/src/dap/types.ts +610 -0
- package/src/debug/index.ts +515 -0
- package/src/debug/log-formatting.ts +58 -0
- package/src/debug/log-viewer.ts +908 -0
- package/src/debug/profiler.ts +162 -0
- package/src/debug/protocol-probe.ts +267 -0
- package/src/debug/raw-sse-buffer.ts +273 -0
- package/src/debug/raw-sse.ts +292 -0
- package/src/debug/report-bundle.ts +374 -0
- package/src/debug/system-info.ts +111 -0
- package/src/debug/terminal-info.ts +124 -0
- package/src/discovery/agents-md.ts +67 -0
- package/src/discovery/agents.ts +230 -0
- package/src/discovery/at-imports.ts +273 -0
- package/src/discovery/builtin-defaults.ts +39 -0
- package/src/discovery/builtin-rules/index.ts +54 -0
- package/src/discovery/builtin-rules/rs-box-leak.md +48 -0
- package/src/discovery/builtin-rules/rs-future-prelude.md +23 -0
- package/src/discovery/builtin-rules/rs-lazylock.md +51 -0
- package/src/discovery/builtin-rules/rs-match-ergonomics.md +67 -0
- package/src/discovery/builtin-rules/rs-parking-lot.md +44 -0
- package/src/discovery/builtin-rules/rs-result-type.md +19 -0
- package/src/discovery/builtin-rules/ts-bare-catch.md +38 -0
- package/src/discovery/builtin-rules/ts-import-type.md +42 -0
- package/src/discovery/builtin-rules/ts-no-any.md +56 -0
- package/src/discovery/builtin-rules/ts-no-deprecated-leftovers.md +44 -0
- package/src/discovery/builtin-rules/ts-no-dynamic-import.md +39 -0
- package/src/discovery/builtin-rules/ts-no-return-type.md +45 -0
- package/src/discovery/builtin-rules/ts-no-test-timers.md +55 -0
- package/src/discovery/builtin-rules/ts-no-tiny-functions.md +51 -0
- package/src/discovery/builtin-rules/ts-promise-with-resolvers.md +65 -0
- package/src/discovery/builtin-rules/ts-redundant-clear-guard.md +75 -0
- package/src/discovery/builtin-rules/ts-set-map.md +28 -0
- package/src/discovery/builtin.ts +906 -0
- package/src/discovery/claude-plugins.ts +386 -0
- package/src/discovery/claude.ts +584 -0
- package/src/discovery/cline.ts +83 -0
- package/src/discovery/codex.ts +522 -0
- package/src/discovery/cursor.ts +220 -0
- package/src/discovery/gemini.ts +383 -0
- package/src/discovery/github.ts +154 -0
- package/src/discovery/helpers.ts +1016 -0
- package/src/discovery/index.ts +81 -0
- package/src/discovery/mcp-json.ts +171 -0
- package/src/discovery/omp-extension-roots.ts +190 -0
- package/src/discovery/omp-plugins.ts +383 -0
- package/src/discovery/opencode.ts +398 -0
- package/src/discovery/plugin-dir-roots.ts +28 -0
- package/src/discovery/ssh.ts +153 -0
- package/src/discovery/substitute-plugin-root.ts +29 -0
- package/src/discovery/vscode.ts +105 -0
- package/src/discovery/windsurf.ts +147 -0
- package/src/edit/apply-patch/index.ts +87 -0
- package/src/edit/apply-patch/parser.ts +174 -0
- package/src/edit/diff.ts +999 -0
- package/src/edit/file-snapshot-store.ts +91 -0
- package/src/edit/hashline/block-resolver.ts +33 -0
- package/src/edit/hashline/diff.ts +290 -0
- package/src/edit/hashline/execute.ts +242 -0
- package/src/edit/hashline/filesystem.ts +130 -0
- package/src/edit/hashline/index.ts +5 -0
- package/src/edit/hashline/noop-loop-guard.ts +99 -0
- package/src/edit/hashline/params.ts +18 -0
- package/src/edit/index.ts +571 -0
- package/src/edit/modes/apply-patch.lark +19 -0
- package/src/edit/modes/apply-patch.ts +53 -0
- package/src/edit/modes/patch.ts +1891 -0
- package/src/edit/modes/replace.ts +1137 -0
- package/src/edit/normalize.ts +345 -0
- package/src/edit/notebook.ts +242 -0
- package/src/edit/read-file.ts +25 -0
- package/src/edit/renderer.ts +769 -0
- package/src/edit/streaming.ts +517 -0
- package/src/eval/__tests__/agent-bridge.test.ts +708 -0
- package/src/eval/__tests__/bridge-timeout.test.ts +64 -0
- package/src/eval/__tests__/budget-bridge.test.ts +69 -0
- package/src/eval/__tests__/completion-bridge.test.ts +412 -0
- package/src/eval/__tests__/helpers-local-roots.test.ts +58 -0
- package/src/eval/__tests__/idle-timeout.test.ts +80 -0
- package/src/eval/__tests__/js-context-manager.test.ts +241 -0
- package/src/eval/__tests__/kernel-spawn.test.ts +103 -0
- package/src/eval/agent-bridge.ts +319 -0
- package/src/eval/backend.ts +71 -0
- package/src/eval/bridge-timeout.ts +44 -0
- package/src/eval/budget-bridge.ts +48 -0
- package/src/eval/completion-bridge.ts +207 -0
- package/src/eval/concurrency-bridge.ts +34 -0
- package/src/eval/idle-timeout.ts +91 -0
- package/src/eval/index.ts +4 -0
- package/src/eval/js/context-manager.ts +502 -0
- package/src/eval/js/executor.ts +173 -0
- package/src/eval/js/index.ts +51 -0
- package/src/eval/js/shared/helpers.ts +283 -0
- package/src/eval/js/shared/indirect-eval.ts +30 -0
- package/src/eval/js/shared/local-module-loader.ts +342 -0
- package/src/eval/js/shared/prelude.ts +2 -0
- package/src/eval/js/shared/prelude.txt +246 -0
- package/src/eval/js/shared/rewrite-imports.ts +532 -0
- package/src/eval/js/shared/runtime.ts +352 -0
- package/src/eval/js/shared/types.ts +18 -0
- package/src/eval/js/tool-bridge.ts +162 -0
- package/src/eval/js/worker-core.ts +132 -0
- package/src/eval/js/worker-entry.ts +30 -0
- package/src/eval/js/worker-protocol.ts +47 -0
- package/src/eval/py/__tests__/prelude.test.ts +19 -0
- package/src/eval/py/display.ts +71 -0
- package/src/eval/py/executor.ts +742 -0
- package/src/eval/py/index.ts +68 -0
- package/src/eval/py/kernel.ts +748 -0
- package/src/eval/py/prelude.py +658 -0
- package/src/eval/py/prelude.ts +3 -0
- package/src/eval/py/runner.py +1133 -0
- package/src/eval/py/runtime.ts +276 -0
- package/src/eval/py/spawn-options.ts +126 -0
- package/src/eval/py/tool-bridge.ts +182 -0
- package/src/eval/session-id.ts +8 -0
- package/src/eval/types.ts +48 -0
- package/src/exa/index.ts +2 -0
- package/src/exa/mcp-client.ts +370 -0
- package/src/exa/types.ts +69 -0
- package/src/exec/bash-executor.ts +419 -0
- package/src/exec/exec.ts +53 -0
- package/src/exec/non-interactive-env.ts +48 -0
- package/src/export/custom-share.ts +65 -0
- package/src/export/html/index.ts +164 -0
- package/src/export/html/template.css +1051 -0
- package/src/export/html/template.generated.ts +2 -0
- package/src/export/html/template.html +46 -0
- package/src/export/html/template.js +2271 -0
- package/src/export/html/template.macro.ts +25 -0
- package/src/export/html/vendor/highlight.min.js +1213 -0
- package/src/export/html/vendor/marked.min.js +6 -0
- package/src/export/ttsr.ts +583 -0
- package/src/extensibility/custom-commands/bundled/ci-green/index.ts +54 -0
- package/src/extensibility/custom-commands/bundled/review/index.ts +489 -0
- package/src/extensibility/custom-commands/index.ts +2 -0
- package/src/extensibility/custom-commands/loader.ts +238 -0
- package/src/extensibility/custom-commands/types.ts +113 -0
- package/src/extensibility/custom-tools/index.ts +7 -0
- package/src/extensibility/custom-tools/loader.ts +269 -0
- package/src/extensibility/custom-tools/types.ts +270 -0
- package/src/extensibility/custom-tools/wrapper.ts +47 -0
- package/src/extensibility/extensions/compact-handler.ts +40 -0
- package/src/extensibility/extensions/get-commands-handler.ts +78 -0
- package/src/extensibility/extensions/index.ts +16 -0
- package/src/extensibility/extensions/loader.ts +572 -0
- package/src/extensibility/extensions/runner.ts +922 -0
- package/src/extensibility/extensions/types.ts +1322 -0
- package/src/extensibility/extensions/wrapper.ts +223 -0
- package/src/extensibility/hooks/index.ts +5 -0
- package/src/extensibility/hooks/loader.ts +257 -0
- package/src/extensibility/hooks/runner.ts +425 -0
- package/src/extensibility/hooks/tool-wrapper.ts +107 -0
- package/src/extensibility/hooks/types.ts +606 -0
- package/src/extensibility/legacy-pi-ai-shim.ts +24 -0
- package/src/extensibility/legacy-pi-coding-agent-shim.ts +15 -0
- package/src/extensibility/plugins/doctor.ts +65 -0
- package/src/extensibility/plugins/git-url.ts +367 -0
- package/src/extensibility/plugins/index.ts +9 -0
- package/src/extensibility/plugins/installer.ts +192 -0
- package/src/extensibility/plugins/legacy-pi-compat.ts +682 -0
- package/src/extensibility/plugins/loader.ts +313 -0
- package/src/extensibility/plugins/manager.ts +827 -0
- package/src/extensibility/plugins/marketplace/cache.ts +136 -0
- package/src/extensibility/plugins/marketplace/fetcher.ts +317 -0
- package/src/extensibility/plugins/marketplace/index.ts +6 -0
- package/src/extensibility/plugins/marketplace/manager.ts +770 -0
- package/src/extensibility/plugins/marketplace/registry.ts +196 -0
- package/src/extensibility/plugins/marketplace/source-resolver.ts +147 -0
- package/src/extensibility/plugins/marketplace/types.ts +191 -0
- package/src/extensibility/plugins/marketplace-auto-update.ts +49 -0
- package/src/extensibility/plugins/parser.ts +105 -0
- package/src/extensibility/plugins/types.ts +194 -0
- package/src/extensibility/shared-events.ts +343 -0
- package/src/extensibility/skills.ts +312 -0
- package/src/extensibility/slash-commands.ts +227 -0
- package/src/extensibility/tool-proxy.ts +25 -0
- package/src/extensibility/typebox.ts +418 -0
- package/src/extensibility/utils.ts +44 -0
- package/src/goals/index.ts +3 -0
- package/src/goals/runtime.ts +528 -0
- package/src/goals/state.ts +37 -0
- package/src/goals/tools/goal-tool.ts +251 -0
- package/src/hindsight/backend.ts +354 -0
- package/src/hindsight/bank.ts +156 -0
- package/src/hindsight/client.ts +598 -0
- package/src/hindsight/config.ts +175 -0
- package/src/hindsight/content.ts +210 -0
- package/src/hindsight/index.ts +8 -0
- package/src/hindsight/mental-models.ts +429 -0
- package/src/hindsight/seeds.json +32 -0
- package/src/hindsight/state.ts +488 -0
- package/src/hindsight/transcript.ts +71 -0
- package/src/index.ts +59 -0
- package/src/internal-urls/agent-protocol.ts +146 -0
- package/src/internal-urls/artifact-protocol.ts +107 -0
- package/src/internal-urls/docs-index.generated.ts +106 -0
- package/src/internal-urls/history-protocol.ts +113 -0
- package/src/internal-urls/index.ts +25 -0
- package/src/internal-urls/issue-pr-protocol.ts +584 -0
- package/src/internal-urls/json-query.ts +126 -0
- package/src/internal-urls/local-protocol.ts +287 -0
- package/src/internal-urls/mcp-protocol.ts +151 -0
- package/src/internal-urls/memory-protocol.ts +169 -0
- package/src/internal-urls/omp-protocol.ts +93 -0
- package/src/internal-urls/parse.ts +72 -0
- package/src/internal-urls/registry-helpers.ts +25 -0
- package/src/internal-urls/router.ts +105 -0
- package/src/internal-urls/rule-protocol.ts +45 -0
- package/src/internal-urls/skill-protocol.ts +96 -0
- package/src/internal-urls/types.ts +152 -0
- package/src/internal-urls/vault-protocol.ts +936 -0
- package/src/irc/bus.ts +292 -0
- package/src/lib/xai-http.ts +124 -0
- package/src/lsp/client.ts +1193 -0
- package/src/lsp/clients/biome-client.ts +264 -0
- package/src/lsp/clients/index.ts +50 -0
- package/src/lsp/clients/lsp-linter-client.ts +93 -0
- package/src/lsp/clients/swiftlint-client.ts +120 -0
- package/src/lsp/config.ts +502 -0
- package/src/lsp/defaults.json +493 -0
- package/src/lsp/diagnostics-ledger.ts +51 -0
- package/src/lsp/edits.ts +267 -0
- package/src/lsp/index.ts +2477 -0
- package/src/lsp/lspmux.ts +233 -0
- package/src/lsp/render.ts +694 -0
- package/src/lsp/startup-events.ts +13 -0
- package/src/lsp/types.ts +455 -0
- package/src/lsp/utils.ts +718 -0
- package/src/main.ts +1325 -0
- package/src/mcp/client.ts +484 -0
- package/src/mcp/config-writer.ts +225 -0
- package/src/mcp/config.ts +365 -0
- package/src/mcp/index.ts +29 -0
- package/src/mcp/json-rpc.ts +122 -0
- package/src/mcp/loader.ts +124 -0
- package/src/mcp/manager.ts +1275 -0
- package/src/mcp/oauth-discovery.ts +442 -0
- package/src/mcp/oauth-flow.ts +442 -0
- package/src/mcp/render.ts +132 -0
- package/src/mcp/smithery-auth.ts +104 -0
- package/src/mcp/smithery-connect.ts +145 -0
- package/src/mcp/smithery-registry.ts +477 -0
- package/src/mcp/timeout.ts +59 -0
- package/src/mcp/tool-bridge.ts +426 -0
- package/src/mcp/tool-cache.ts +117 -0
- package/src/mcp/transports/http.ts +519 -0
- package/src/mcp/transports/index.ts +6 -0
- package/src/mcp/transports/stdio.ts +528 -0
- package/src/mcp/types.ts +423 -0
- package/src/memories/index.ts +1150 -0
- package/src/memories/storage.ts +577 -0
- package/src/memory-backend/index.ts +18 -0
- package/src/memory-backend/local-backend.ts +39 -0
- package/src/memory-backend/off-backend.ts +25 -0
- package/src/memory-backend/resolve.ts +25 -0
- package/src/memory-backend/runtime.ts +66 -0
- package/src/memory-backend/types.ts +166 -0
- package/src/mnemopi/backend.ts +547 -0
- package/src/mnemopi/config.ts +160 -0
- package/src/mnemopi/index.ts +3 -0
- package/src/mnemopi/state.ts +584 -0
- package/src/modes/acp/acp-agent.ts +2407 -0
- package/src/modes/acp/acp-client-bridge.ts +154 -0
- package/src/modes/acp/acp-event-mapper.ts +929 -0
- package/src/modes/acp/acp-mode.ts +23 -0
- package/src/modes/acp/index.ts +2 -0
- package/src/modes/acp/terminal-auth.ts +37 -0
- package/src/modes/components/agent-dashboard.ts +1206 -0
- package/src/modes/components/agent-hub.ts +1071 -0
- package/src/modes/components/assistant-message.ts +307 -0
- package/src/modes/components/bash-execution.ts +220 -0
- package/src/modes/components/bordered-loader.ts +41 -0
- package/src/modes/components/branch-summary-message.ts +45 -0
- package/src/modes/components/btw-panel.ts +104 -0
- package/src/modes/components/chat-block.ts +111 -0
- package/src/modes/components/compaction-summary-message.ts +87 -0
- package/src/modes/components/copy-selector.ts +206 -0
- package/src/modes/components/countdown-timer.ts +75 -0
- package/src/modes/components/custom-editor.ts +398 -0
- package/src/modes/components/custom-message.ts +63 -0
- package/src/modes/components/diff.ts +277 -0
- package/src/modes/components/dynamic-border.ts +34 -0
- package/src/modes/components/error-banner.ts +33 -0
- package/src/modes/components/eval-execution.ts +158 -0
- package/src/modes/components/execution-shared.ts +101 -0
- package/src/modes/components/extensions/extension-dashboard.ts +399 -0
- package/src/modes/components/extensions/extension-list.ts +502 -0
- package/src/modes/components/extensions/index.ts +9 -0
- package/src/modes/components/extensions/inspector-panel.ts +317 -0
- package/src/modes/components/extensions/state-manager.ts +627 -0
- package/src/modes/components/extensions/types.ts +186 -0
- package/src/modes/components/footer.ts +274 -0
- package/src/modes/components/history-search.ts +280 -0
- package/src/modes/components/hook-editor.ts +167 -0
- package/src/modes/components/hook-input.ts +87 -0
- package/src/modes/components/hook-message.ts +66 -0
- package/src/modes/components/hook-selector.ts +660 -0
- package/src/modes/components/index.ts +38 -0
- package/src/modes/components/keybinding-hints.ts +65 -0
- package/src/modes/components/late-diagnostics-message.ts +60 -0
- package/src/modes/components/login-dialog.ts +164 -0
- package/src/modes/components/mcp-add-wizard.ts +1340 -0
- package/src/modes/components/message-frame.ts +88 -0
- package/src/modes/components/model-selector.ts +1271 -0
- package/src/modes/components/oauth-selector.ts +368 -0
- package/src/modes/components/omfg-panel.ts +141 -0
- package/src/modes/components/overlay-box.ts +108 -0
- package/src/modes/components/plan-review-overlay.ts +820 -0
- package/src/modes/components/plan-toc.ts +138 -0
- package/src/modes/components/plugin-selector.ts +95 -0
- package/src/modes/components/plugin-settings.ts +722 -0
- package/src/modes/components/queue-mode-selector.ts +56 -0
- package/src/modes/components/read-tool-group.ts +670 -0
- package/src/modes/components/segment-track.ts +52 -0
- package/src/modes/components/session-selector.ts +625 -0
- package/src/modes/components/settings-defs.ts +189 -0
- package/src/modes/components/settings-selector.ts +651 -0
- package/src/modes/components/show-images-selector.ts +45 -0
- package/src/modes/components/skill-message.ts +89 -0
- package/src/modes/components/status-line/component.ts +869 -0
- package/src/modes/components/status-line/context-thresholds.ts +79 -0
- package/src/modes/components/status-line/git-utils.ts +42 -0
- package/src/modes/components/status-line/index.ts +5 -0
- package/src/modes/components/status-line/presets.ts +106 -0
- package/src/modes/components/status-line/segments.ts +584 -0
- package/src/modes/components/status-line/separators.ts +55 -0
- package/src/modes/components/status-line/token-rate.ts +66 -0
- package/src/modes/components/status-line/types.ts +108 -0
- package/src/modes/components/theme-selector.ts +63 -0
- package/src/modes/components/thinking-selector.ts +52 -0
- package/src/modes/components/tiny-title-download-progress.ts +90 -0
- package/src/modes/components/tips.txt +19 -0
- package/src/modes/components/todo-reminder.ts +38 -0
- package/src/modes/components/tool-execution.ts +1024 -0
- package/src/modes/components/transcript-container.ts +608 -0
- package/src/modes/components/tree-selector.ts +978 -0
- package/src/modes/components/ttsr-notification.ts +122 -0
- package/src/modes/components/user-message-selector.ts +227 -0
- package/src/modes/components/user-message.ts +66 -0
- package/src/modes/components/visual-truncate.ts +63 -0
- package/src/modes/components/welcome.ts +493 -0
- package/src/modes/controllers/btw-controller.ts +105 -0
- package/src/modes/controllers/command-controller-shared.ts +109 -0
- package/src/modes/controllers/command-controller.ts +1566 -0
- package/src/modes/controllers/event-controller.ts +1054 -0
- package/src/modes/controllers/extension-ui-controller.ts +886 -0
- package/src/modes/controllers/input-controller.ts +1073 -0
- package/src/modes/controllers/mcp-command-controller.ts +2017 -0
- package/src/modes/controllers/omfg-controller.ts +283 -0
- package/src/modes/controllers/omfg-rule.ts +647 -0
- package/src/modes/controllers/selector-controller.ts +1108 -0
- package/src/modes/controllers/ssh-command-controller.ts +384 -0
- package/src/modes/controllers/streaming-reveal.ts +279 -0
- package/src/modes/controllers/tan-command-controller.ts +173 -0
- package/src/modes/controllers/todo-command-controller.ts +485 -0
- package/src/modes/data/emojis.json +1 -0
- package/src/modes/emoji-autocomplete.ts +285 -0
- package/src/modes/gradient-highlight.ts +87 -0
- package/src/modes/image-references.ts +117 -0
- package/src/modes/index.ts +17 -0
- package/src/modes/interactive-mode.ts +3370 -0
- package/src/modes/internal-url-autocomplete.ts +143 -0
- package/src/modes/loop-limit.ts +140 -0
- package/src/modes/magic-keywords.ts +20 -0
- package/src/modes/markdown-prose.ts +247 -0
- package/src/modes/oauth-manual-input.ts +69 -0
- package/src/modes/orchestrate.ts +42 -0
- package/src/modes/print-mode.ts +126 -0
- package/src/modes/prompt-action-autocomplete.ts +260 -0
- package/src/modes/rpc/host-tools.ts +186 -0
- package/src/modes/rpc/host-uris.ts +235 -0
- package/src/modes/rpc/rpc-client.ts +963 -0
- package/src/modes/rpc/rpc-mode.ts +947 -0
- package/src/modes/rpc/rpc-subagents.ts +265 -0
- package/src/modes/rpc/rpc-types.ts +458 -0
- package/src/modes/runtime-init.ts +116 -0
- package/src/modes/session-observer-registry.ts +146 -0
- package/src/modes/setup-version.ts +11 -0
- package/src/modes/setup-wizard/index.ts +99 -0
- package/src/modes/setup-wizard/lazy.ts +16 -0
- package/src/modes/setup-wizard/scenes/glyph.ts +96 -0
- package/src/modes/setup-wizard/scenes/outro.ts +35 -0
- package/src/modes/setup-wizard/scenes/providers.ts +69 -0
- package/src/modes/setup-wizard/scenes/sign-in.ts +205 -0
- package/src/modes/setup-wizard/scenes/splash.ts +201 -0
- package/src/modes/setup-wizard/scenes/theme.ts +299 -0
- package/src/modes/setup-wizard/scenes/types.ts +48 -0
- package/src/modes/setup-wizard/scenes/web-search.ts +129 -0
- package/src/modes/setup-wizard/wizard-overlay.ts +275 -0
- package/src/modes/shared.ts +47 -0
- package/src/modes/theme/dark.json +95 -0
- package/src/modes/theme/defaults/alabaster.json +93 -0
- package/src/modes/theme/defaults/amethyst.json +96 -0
- package/src/modes/theme/defaults/anthracite.json +93 -0
- package/src/modes/theme/defaults/basalt.json +91 -0
- package/src/modes/theme/defaults/birch.json +95 -0
- package/src/modes/theme/defaults/dark-abyss.json +91 -0
- package/src/modes/theme/defaults/dark-arctic.json +104 -0
- package/src/modes/theme/defaults/dark-aurora.json +95 -0
- package/src/modes/theme/defaults/dark-catppuccin.json +107 -0
- package/src/modes/theme/defaults/dark-cavern.json +91 -0
- package/src/modes/theme/defaults/dark-copper.json +95 -0
- package/src/modes/theme/defaults/dark-cosmos.json +90 -0
- package/src/modes/theme/defaults/dark-cyberpunk.json +102 -0
- package/src/modes/theme/defaults/dark-dracula.json +98 -0
- package/src/modes/theme/defaults/dark-eclipse.json +91 -0
- package/src/modes/theme/defaults/dark-ember.json +95 -0
- package/src/modes/theme/defaults/dark-equinox.json +90 -0
- package/src/modes/theme/defaults/dark-forest.json +96 -0
- package/src/modes/theme/defaults/dark-github.json +105 -0
- package/src/modes/theme/defaults/dark-gruvbox.json +112 -0
- package/src/modes/theme/defaults/dark-lavender.json +95 -0
- package/src/modes/theme/defaults/dark-lunar.json +89 -0
- package/src/modes/theme/defaults/dark-midnight.json +95 -0
- package/src/modes/theme/defaults/dark-monochrome.json +94 -0
- package/src/modes/theme/defaults/dark-monokai.json +98 -0
- package/src/modes/theme/defaults/dark-nebula.json +90 -0
- package/src/modes/theme/defaults/dark-nord.json +97 -0
- package/src/modes/theme/defaults/dark-ocean.json +101 -0
- package/src/modes/theme/defaults/dark-one.json +100 -0
- package/src/modes/theme/defaults/dark-poimandres.json +142 -0
- package/src/modes/theme/defaults/dark-rainforest.json +91 -0
- package/src/modes/theme/defaults/dark-reef.json +91 -0
- package/src/modes/theme/defaults/dark-retro.json +92 -0
- package/src/modes/theme/defaults/dark-rose-pine.json +96 -0
- package/src/modes/theme/defaults/dark-sakura.json +95 -0
- package/src/modes/theme/defaults/dark-slate.json +95 -0
- package/src/modes/theme/defaults/dark-solarized.json +97 -0
- package/src/modes/theme/defaults/dark-solstice.json +90 -0
- package/src/modes/theme/defaults/dark-starfall.json +91 -0
- package/src/modes/theme/defaults/dark-sunset.json +99 -0
- package/src/modes/theme/defaults/dark-swamp.json +90 -0
- package/src/modes/theme/defaults/dark-synthwave.json +103 -0
- package/src/modes/theme/defaults/dark-taiga.json +91 -0
- package/src/modes/theme/defaults/dark-terminal.json +95 -0
- package/src/modes/theme/defaults/dark-tokyo-night.json +101 -0
- package/src/modes/theme/defaults/dark-tundra.json +91 -0
- package/src/modes/theme/defaults/dark-twilight.json +91 -0
- package/src/modes/theme/defaults/dark-volcanic.json +91 -0
- package/src/modes/theme/defaults/graphite.json +92 -0
- package/src/modes/theme/defaults/index.ts +199 -0
- package/src/modes/theme/defaults/light-arctic.json +107 -0
- package/src/modes/theme/defaults/light-aurora-day.json +91 -0
- package/src/modes/theme/defaults/light-canyon.json +91 -0
- package/src/modes/theme/defaults/light-catppuccin.json +106 -0
- package/src/modes/theme/defaults/light-cirrus.json +90 -0
- package/src/modes/theme/defaults/light-coral.json +95 -0
- package/src/modes/theme/defaults/light-cyberpunk.json +96 -0
- package/src/modes/theme/defaults/light-dawn.json +90 -0
- package/src/modes/theme/defaults/light-dunes.json +91 -0
- package/src/modes/theme/defaults/light-eucalyptus.json +95 -0
- package/src/modes/theme/defaults/light-forest.json +100 -0
- package/src/modes/theme/defaults/light-frost.json +95 -0
- package/src/modes/theme/defaults/light-github.json +115 -0
- package/src/modes/theme/defaults/light-glacier.json +91 -0
- package/src/modes/theme/defaults/light-gruvbox.json +108 -0
- package/src/modes/theme/defaults/light-haze.json +90 -0
- package/src/modes/theme/defaults/light-honeycomb.json +95 -0
- package/src/modes/theme/defaults/light-lagoon.json +91 -0
- package/src/modes/theme/defaults/light-lavender.json +95 -0
- package/src/modes/theme/defaults/light-meadow.json +91 -0
- package/src/modes/theme/defaults/light-mint.json +95 -0
- package/src/modes/theme/defaults/light-monochrome.json +101 -0
- package/src/modes/theme/defaults/light-ocean.json +99 -0
- package/src/modes/theme/defaults/light-one.json +99 -0
- package/src/modes/theme/defaults/light-opal.json +91 -0
- package/src/modes/theme/defaults/light-orchard.json +91 -0
- package/src/modes/theme/defaults/light-paper.json +95 -0
- package/src/modes/theme/defaults/light-poimandres.json +142 -0
- package/src/modes/theme/defaults/light-prism.json +90 -0
- package/src/modes/theme/defaults/light-retro.json +98 -0
- package/src/modes/theme/defaults/light-sand.json +95 -0
- package/src/modes/theme/defaults/light-savanna.json +91 -0
- package/src/modes/theme/defaults/light-solarized.json +102 -0
- package/src/modes/theme/defaults/light-soleil.json +90 -0
- package/src/modes/theme/defaults/light-sunset.json +99 -0
- package/src/modes/theme/defaults/light-synthwave.json +98 -0
- package/src/modes/theme/defaults/light-tokyo-night.json +111 -0
- package/src/modes/theme/defaults/light-wetland.json +91 -0
- package/src/modes/theme/defaults/light-zenith.json +89 -0
- package/src/modes/theme/defaults/limestone.json +94 -0
- package/src/modes/theme/defaults/mahogany.json +97 -0
- package/src/modes/theme/defaults/marble.json +93 -0
- package/src/modes/theme/defaults/obsidian.json +91 -0
- package/src/modes/theme/defaults/onyx.json +91 -0
- package/src/modes/theme/defaults/pearl.json +93 -0
- package/src/modes/theme/defaults/porcelain.json +91 -0
- package/src/modes/theme/defaults/quartz.json +96 -0
- package/src/modes/theme/defaults/sandstone.json +95 -0
- package/src/modes/theme/defaults/titanium.json +90 -0
- package/src/modes/theme/light.json +93 -0
- package/src/modes/theme/mermaid-cache.ts +29 -0
- package/src/modes/theme/shimmer.ts +235 -0
- package/src/modes/theme/theme-schema.json +459 -0
- package/src/modes/theme/theme.ts +2676 -0
- package/src/modes/turn-budget.ts +31 -0
- package/src/modes/types.ts +359 -0
- package/src/modes/ultrathink.ts +41 -0
- package/src/modes/utils/context-usage.ts +339 -0
- package/src/modes/utils/copy-targets.ts +360 -0
- package/src/modes/utils/hotkeys-markdown.ts +61 -0
- package/src/modes/utils/keybinding-matchers.ts +51 -0
- package/src/modes/utils/tools-markdown.ts +27 -0
- package/src/modes/utils/ui-helpers.ts +801 -0
- package/src/modes/workflow.ts +42 -0
- package/src/plan-mode/approved-plan.ts +186 -0
- package/src/plan-mode/plan-handoff.ts +37 -0
- package/src/plan-mode/plan-protection.ts +31 -0
- package/src/plan-mode/state.ts +6 -0
- package/src/priority.json +41 -0
- package/src/prompts/agents/designer.md +66 -0
- package/src/prompts/agents/explore.md +58 -0
- package/src/prompts/agents/frontmatter.md +11 -0
- package/src/prompts/agents/init.md +33 -0
- package/src/prompts/agents/librarian.md +119 -0
- package/src/prompts/agents/oracle.md +55 -0
- package/src/prompts/agents/plan.md +48 -0
- package/src/prompts/agents/reviewer.md +140 -0
- package/src/prompts/agents/task.md +16 -0
- package/src/prompts/ci-green-request.md +36 -0
- package/src/prompts/dry-balance-bench.md +8 -0
- package/src/prompts/goals/goal-budget-limit.md +16 -0
- package/src/prompts/goals/goal-continuation.md +28 -0
- package/src/prompts/goals/goal-mode-active.md +23 -0
- package/src/prompts/memories/consolidation.md +30 -0
- package/src/prompts/memories/read-path.md +11 -0
- package/src/prompts/memories/stage_one_input.md +6 -0
- package/src/prompts/memories/stage_one_system.md +21 -0
- package/src/prompts/review-custom-request.md +22 -0
- package/src/prompts/review-headless-request.md +16 -0
- package/src/prompts/review-request.md +69 -0
- package/src/prompts/steering/user-interjection.md +10 -0
- package/src/prompts/system/agent-creation-architect.md +50 -0
- package/src/prompts/system/agent-creation-user.md +6 -0
- package/src/prompts/system/auto-continue.md +1 -0
- package/src/prompts/system/auto-thinking-difficulty-local.md +14 -0
- package/src/prompts/system/auto-thinking-difficulty.md +12 -0
- package/src/prompts/system/background-tan-dispatch.md +8 -0
- package/src/prompts/system/btw-user.md +8 -0
- package/src/prompts/system/commit-message-system.md +14 -0
- package/src/prompts/system/custom-system-prompt.md +64 -0
- package/src/prompts/system/eager-todo.md +13 -0
- package/src/prompts/system/empty-stop-retry.md +6 -0
- package/src/prompts/system/irc-incoming.md +7 -0
- package/src/prompts/system/manual-continue.md +7 -0
- package/src/prompts/system/memory-consolidation-system.md +8 -0
- package/src/prompts/system/memory-extraction-system.md +26 -0
- package/src/prompts/system/omfg-user.md +50 -0
- package/src/prompts/system/orchestrate-notice.md +40 -0
- package/src/prompts/system/plan-mode-active.md +109 -0
- package/src/prompts/system/plan-mode-approved.md +25 -0
- package/src/prompts/system/plan-mode-compact-instructions.md +16 -0
- package/src/prompts/system/plan-mode-reference.md +11 -0
- package/src/prompts/system/plan-mode-subagent.md +33 -0
- package/src/prompts/system/plan-mode-tool-decision-reminder.md +9 -0
- package/src/prompts/system/project-prompt.md +52 -0
- package/src/prompts/system/subagent-system-prompt.md +64 -0
- package/src/prompts/system/subagent-user-prompt.md +3 -0
- package/src/prompts/system/subagent-yield-reminder.md +12 -0
- package/src/prompts/system/system-prompt.md +258 -0
- package/src/prompts/system/tiny-title-system.md +8 -0
- package/src/prompts/system/title-system.md +16 -0
- package/src/prompts/system/ttsr-interrupt.md +7 -0
- package/src/prompts/system/ttsr-tool-reminder.md +5 -0
- package/src/prompts/system/ultrathink-notice.md +3 -0
- package/src/prompts/system/web-search.md +25 -0
- package/src/prompts/system/workflow-notice.md +70 -0
- package/src/prompts/tools/apply-patch.md +65 -0
- package/src/prompts/tools/ask.md +30 -0
- package/src/prompts/tools/ast-edit.md +39 -0
- package/src/prompts/tools/ast-grep.md +42 -0
- package/src/prompts/tools/async-result.md +8 -0
- package/src/prompts/tools/bash.md +46 -0
- package/src/prompts/tools/browser.md +73 -0
- package/src/prompts/tools/checkpoint.md +16 -0
- package/src/prompts/tools/debug.md +34 -0
- package/src/prompts/tools/eval.md +92 -0
- package/src/prompts/tools/find.md +36 -0
- package/src/prompts/tools/github.md +21 -0
- package/src/prompts/tools/goal.md +18 -0
- package/src/prompts/tools/image-gen.md +7 -0
- package/src/prompts/tools/inspect-image-system.md +20 -0
- package/src/prompts/tools/inspect-image.md +32 -0
- package/src/prompts/tools/irc.md +59 -0
- package/src/prompts/tools/job.md +19 -0
- package/src/prompts/tools/lsp-late-diagnostic.md +8 -0
- package/src/prompts/tools/lsp.md +42 -0
- package/src/prompts/tools/memory-edit.md +8 -0
- package/src/prompts/tools/patch.md +70 -0
- package/src/prompts/tools/read.md +84 -0
- package/src/prompts/tools/recall.md +5 -0
- package/src/prompts/tools/reflect.md +5 -0
- package/src/prompts/tools/render-mermaid.md +9 -0
- package/src/prompts/tools/replace.md +30 -0
- package/src/prompts/tools/resolve.md +9 -0
- package/src/prompts/tools/retain.md +6 -0
- package/src/prompts/tools/rewind.md +13 -0
- package/src/prompts/tools/search-tool-bm25.md +32 -0
- package/src/prompts/tools/search.md +24 -0
- package/src/prompts/tools/ssh.md +31 -0
- package/src/prompts/tools/task-summary.md +17 -0
- package/src/prompts/tools/task.md +88 -0
- package/src/prompts/tools/todo.md +62 -0
- package/src/prompts/tools/web-search.md +10 -0
- package/src/prompts/tools/write.md +14 -0
- package/src/registry/agent-lifecycle.ts +218 -0
- package/src/registry/agent-registry.ts +151 -0
- package/src/sdk.ts +2558 -0
- package/src/secrets/index.ts +123 -0
- package/src/secrets/obfuscator.ts +298 -0
- package/src/secrets/regex.ts +21 -0
- package/src/session/agent-session.ts +10121 -0
- package/src/session/agent-storage.ts +455 -0
- package/src/session/artifacts.ts +135 -0
- package/src/session/auth-broker-config.ts +131 -0
- package/src/session/auth-storage.ts +29 -0
- package/src/session/blob-store.ts +255 -0
- package/src/session/client-bridge.ts +85 -0
- package/src/session/history-storage.ts +348 -0
- package/src/session/indexed-session-storage.ts +430 -0
- package/src/session/messages.ts +541 -0
- package/src/session/redis-session-storage.ts +170 -0
- package/src/session/session-dump-format.ts +209 -0
- package/src/session/session-history-format.ts +246 -0
- package/src/session/session-manager.ts +3676 -0
- package/src/session/session-storage.ts +529 -0
- package/src/session/shake-types.ts +43 -0
- package/src/session/sql-session-storage.ts +314 -0
- package/src/session/streaming-output.ts +1330 -0
- package/src/session/tool-choice-queue.ts +213 -0
- package/src/session/yield-queue.ts +173 -0
- package/src/slash-commands/acp-builtins.ts +70 -0
- package/src/slash-commands/builtin-registry.ts +1798 -0
- package/src/slash-commands/helpers/context-report.ts +39 -0
- package/src/slash-commands/helpers/format.ts +46 -0
- package/src/slash-commands/helpers/marketplace-manager.ts +25 -0
- package/src/slash-commands/helpers/mcp.ts +532 -0
- package/src/slash-commands/helpers/parse.ts +85 -0
- package/src/slash-commands/helpers/ssh.ts +195 -0
- package/src/slash-commands/helpers/stats-dashboard.ts +85 -0
- package/src/slash-commands/helpers/todo.ts +279 -0
- package/src/slash-commands/helpers/usage-report.ts +95 -0
- package/src/slash-commands/marketplace-install-parser.ts +99 -0
- package/src/slash-commands/types.ts +135 -0
- package/src/ssh/config-writer.ts +183 -0
- package/src/ssh/connection-manager.ts +509 -0
- package/src/ssh/ssh-executor.ts +189 -0
- package/src/ssh/sshfs-mount.ts +140 -0
- package/src/ssh/utils.ts +8 -0
- package/src/stt/downloader.ts +71 -0
- package/src/stt/index.ts +3 -0
- package/src/stt/recorder.ts +351 -0
- package/src/stt/setup.ts +52 -0
- package/src/stt/stt-controller.ts +160 -0
- package/src/stt/transcribe.py +70 -0
- package/src/stt/transcriber.ts +91 -0
- package/src/stubs/natives/index.ts +814 -0
- package/src/stubs/natives/package.json +7 -0
- package/src/stubs/tui/index.ts +282 -0
- package/src/stubs/tui/package.json +7 -0
- package/src/system-prompt.ts +611 -0
- package/src/task/agents.ts +167 -0
- package/src/task/commands.ts +132 -0
- package/src/task/discovery.ts +122 -0
- package/src/task/executor.ts +2133 -0
- package/src/task/index.ts +1419 -0
- package/src/task/name-generator.ts +1577 -0
- package/src/task/omp-command.ts +26 -0
- package/src/task/output-manager.ts +88 -0
- package/src/task/parallel.ts +116 -0
- package/src/task/render.ts +1381 -0
- package/src/task/repair-args.ts +129 -0
- package/src/task/subprocess-tool-registry.ts +88 -0
- package/src/task/types.ts +336 -0
- package/src/task/worktree.ts +514 -0
- package/src/telemetry-export.ts +144 -0
- package/src/thinking.ts +167 -0
- package/src/tiny/compiled-runtime.ts +179 -0
- package/src/tiny/device.ts +111 -0
- package/src/tiny/dtype.ts +101 -0
- package/src/tiny/models.ts +242 -0
- package/src/tiny/text.ts +165 -0
- package/src/tiny/title-client.ts +543 -0
- package/src/tiny/title-protocol.ts +56 -0
- package/src/tiny/worker.ts +568 -0
- package/src/tool-discovery/mode.ts +24 -0
- package/src/tool-discovery/tool-index.ts +256 -0
- package/src/tools/approval.ts +189 -0
- package/src/tools/archive-reader.ts +721 -0
- package/src/tools/ask.ts +928 -0
- package/src/tools/ast-edit.ts +642 -0
- package/src/tools/ast-grep.ts +452 -0
- package/src/tools/auto-generated-guard.ts +322 -0
- package/src/tools/bash-command-fixup.ts +37 -0
- package/src/tools/bash-interactive.ts +408 -0
- package/src/tools/bash-interceptor.ts +67 -0
- package/src/tools/bash-pty-selection.ts +14 -0
- package/src/tools/bash-skill-urls.ts +248 -0
- package/src/tools/bash.ts +1386 -0
- package/src/tools/browser/attach.ts +175 -0
- package/src/tools/browser/launch.ts +660 -0
- package/src/tools/browser/readable.ts +112 -0
- package/src/tools/browser/registry.ts +197 -0
- package/src/tools/browser/render.ts +216 -0
- package/src/tools/browser/tab-protocol.ts +105 -0
- package/src/tools/browser/tab-supervisor.ts +628 -0
- package/src/tools/browser/tab-worker-entry.ts +21 -0
- package/src/tools/browser/tab-worker.ts +1226 -0
- package/src/tools/browser.ts +343 -0
- package/src/tools/checkpoint.ts +136 -0
- package/src/tools/conflict-detect.ts +718 -0
- package/src/tools/context.ts +39 -0
- package/src/tools/debug.ts +1067 -0
- package/src/tools/eval-backends.ts +27 -0
- package/src/tools/eval-render.ts +752 -0
- package/src/tools/eval.ts +577 -0
- package/src/tools/fetch.ts +1926 -0
- package/src/tools/file-recorder.ts +35 -0
- package/src/tools/find.ts +609 -0
- package/src/tools/fs-cache-invalidation.ts +28 -0
- package/src/tools/gh-cache-invalidation.ts +255 -0
- package/src/tools/gh-format.ts +12 -0
- package/src/tools/gh-renderer.ts +481 -0
- package/src/tools/gh.ts +3720 -0
- package/src/tools/github-cache.ts +637 -0
- package/src/tools/grouped-file-output.ts +210 -0
- package/src/tools/image-gen.ts +1517 -0
- package/src/tools/index.ts +599 -0
- package/src/tools/inspect-image-renderer.ts +132 -0
- package/src/tools/inspect-image.ts +174 -0
- package/src/tools/irc.ts +723 -0
- package/src/tools/job.ts +557 -0
- package/src/tools/json-tree.ts +243 -0
- package/src/tools/jtd-to-json-schema.ts +219 -0
- package/src/tools/jtd-to-typescript.ts +136 -0
- package/src/tools/jtd-utils.ts +102 -0
- package/src/tools/list-limit.ts +40 -0
- package/src/tools/match-line-format.ts +20 -0
- package/src/tools/memory-edit.ts +59 -0
- package/src/tools/memory-recall.ts +100 -0
- package/src/tools/memory-reflect.ts +88 -0
- package/src/tools/memory-render.ts +202 -0
- package/src/tools/memory-retain.ts +91 -0
- package/src/tools/output-meta.ts +754 -0
- package/src/tools/output-schema-validator.ts +132 -0
- package/src/tools/path-utils.ts +1054 -0
- package/src/tools/plan-mode-guard.ts +108 -0
- package/src/tools/puppeteer/00_stealth_tampering.txt +63 -0
- package/src/tools/puppeteer/01_stealth_activity.txt +20 -0
- package/src/tools/puppeteer/02_stealth_hairline.txt +11 -0
- package/src/tools/puppeteer/03_stealth_botd.txt +384 -0
- package/src/tools/puppeteer/04_stealth_iframe.txt +81 -0
- package/src/tools/puppeteer/05_stealth_webgl.txt +75 -0
- package/src/tools/puppeteer/06_stealth_screen.txt +72 -0
- package/src/tools/puppeteer/07_stealth_fonts.txt +97 -0
- package/src/tools/puppeteer/08_stealth_audio.txt +51 -0
- package/src/tools/puppeteer/09_stealth_locale.txt +46 -0
- package/src/tools/puppeteer/10_stealth_plugins.txt +206 -0
- package/src/tools/puppeteer/11_stealth_hardware.txt +8 -0
- package/src/tools/puppeteer/12_stealth_codecs.txt +40 -0
- package/src/tools/puppeteer/13_stealth_worker.txt +74 -0
- package/src/tools/read.ts +2929 -0
- package/src/tools/render-mermaid.ts +69 -0
- package/src/tools/render-utils.ts +838 -0
- package/src/tools/renderers.ts +77 -0
- package/src/tools/report-tool-issue.ts +534 -0
- package/src/tools/resolve.ts +276 -0
- package/src/tools/review.ts +253 -0
- package/src/tools/search-tool-bm25.ts +351 -0
- package/src/tools/search.ts +1580 -0
- package/src/tools/sqlite-reader.ts +828 -0
- package/src/tools/ssh.ts +349 -0
- package/src/tools/todo.ts +982 -0
- package/src/tools/tool-errors.ts +62 -0
- package/src/tools/tool-result.ts +94 -0
- package/src/tools/tool-timeouts.ts +30 -0
- package/src/tools/tts.ts +133 -0
- package/src/tools/write.ts +1217 -0
- package/src/tools/yield.ts +269 -0
- package/src/tui/code-cell.ts +216 -0
- package/src/tui/file-list.ts +55 -0
- package/src/tui/hyperlink.ts +175 -0
- package/src/tui/index.ts +12 -0
- package/src/tui/output-block.ts +240 -0
- package/src/tui/status-line.ts +54 -0
- package/src/tui/tree-list.ts +84 -0
- package/src/tui/types.ts +15 -0
- package/src/tui/utils.ts +103 -0
- package/src/utils/block-context.ts +312 -0
- package/src/utils/changelog.ts +132 -0
- package/src/utils/clipboard.ts +193 -0
- package/src/utils/command-args.ts +76 -0
- package/src/utils/commit-message-generator.ts +151 -0
- package/src/utils/edit-mode.ts +41 -0
- package/src/utils/enhanced-paste.ts +230 -0
- package/src/utils/event-bus.ts +33 -0
- package/src/utils/external-editor.ts +65 -0
- package/src/utils/file-display-mode.ts +45 -0
- package/src/utils/file-mentions.ts +281 -0
- package/src/utils/git.ts +1833 -0
- package/src/utils/image-loading.ts +132 -0
- package/src/utils/image-resize.ts +309 -0
- package/src/utils/jj.ts +248 -0
- package/src/utils/lang-from-path.ts +239 -0
- package/src/utils/markit.ts +89 -0
- package/src/utils/open.ts +55 -0
- package/src/utils/session-color.ts +68 -0
- package/src/utils/shell-snapshot.ts +187 -0
- package/src/utils/sixel.ts +69 -0
- package/src/utils/title-generator.ts +373 -0
- package/src/utils/tool-choice.ts +33 -0
- package/src/utils/tools-manager.ts +363 -0
- package/src/web/kagi.ts +305 -0
- package/src/web/parallel.ts +353 -0
- package/src/web/scrapers/artifacthub.ts +207 -0
- package/src/web/scrapers/arxiv.ts +83 -0
- package/src/web/scrapers/aur.ts +162 -0
- package/src/web/scrapers/biorxiv.ts +133 -0
- package/src/web/scrapers/bluesky.ts +262 -0
- package/src/web/scrapers/brew.ts +172 -0
- package/src/web/scrapers/cheatsh.ts +68 -0
- package/src/web/scrapers/chocolatey.ts +196 -0
- package/src/web/scrapers/choosealicense.ts +95 -0
- package/src/web/scrapers/cisa-kev.ts +87 -0
- package/src/web/scrapers/clojars.ts +154 -0
- package/src/web/scrapers/coingecko.ts +177 -0
- package/src/web/scrapers/crates-io.ts +97 -0
- package/src/web/scrapers/crossref.ts +136 -0
- package/src/web/scrapers/devto.ts +147 -0
- package/src/web/scrapers/discogs.ts +306 -0
- package/src/web/scrapers/discourse.ts +197 -0
- package/src/web/scrapers/dockerhub.ts +138 -0
- package/src/web/scrapers/docs-rs.ts +653 -0
- package/src/web/scrapers/fdroid.ts +134 -0
- package/src/web/scrapers/firefox-addons.ts +191 -0
- package/src/web/scrapers/flathub.ts +223 -0
- package/src/web/scrapers/github-gist.ts +58 -0
- package/src/web/scrapers/github.ts +704 -0
- package/src/web/scrapers/gitlab.ts +401 -0
- package/src/web/scrapers/go-pkg.ts +266 -0
- package/src/web/scrapers/hackage.ts +140 -0
- package/src/web/scrapers/hackernews.ts +189 -0
- package/src/web/scrapers/hex.ts +105 -0
- package/src/web/scrapers/huggingface.ts +321 -0
- package/src/web/scrapers/iacr.ts +89 -0
- package/src/web/scrapers/index.ts +252 -0
- package/src/web/scrapers/jetbrains-marketplace.ts +159 -0
- package/src/web/scrapers/lemmy.ts +203 -0
- package/src/web/scrapers/lobsters.ts +175 -0
- package/src/web/scrapers/mastodon.ts +292 -0
- package/src/web/scrapers/maven.ts +138 -0
- package/src/web/scrapers/mdn.ts +173 -0
- package/src/web/scrapers/metacpan.ts +222 -0
- package/src/web/scrapers/musicbrainz.ts +250 -0
- package/src/web/scrapers/npm.ts +98 -0
- package/src/web/scrapers/nuget.ts +183 -0
- package/src/web/scrapers/nvd.ts +222 -0
- package/src/web/scrapers/ollama.ts +239 -0
- package/src/web/scrapers/open-vsx.ts +106 -0
- package/src/web/scrapers/opencorporates.ts +292 -0
- package/src/web/scrapers/openlibrary.ts +336 -0
- package/src/web/scrapers/orcid.ts +286 -0
- package/src/web/scrapers/osv.ts +176 -0
- package/src/web/scrapers/packagist.ts +160 -0
- package/src/web/scrapers/pub-dev.ts +143 -0
- package/src/web/scrapers/pubmed.ts +211 -0
- package/src/web/scrapers/pypi.ts +112 -0
- package/src/web/scrapers/rawg.ts +110 -0
- package/src/web/scrapers/readthedocs.ts +120 -0
- package/src/web/scrapers/reddit.ts +95 -0
- package/src/web/scrapers/repology.ts +251 -0
- package/src/web/scrapers/rfc.ts +201 -0
- package/src/web/scrapers/rubygems.ts +103 -0
- package/src/web/scrapers/searchcode.ts +189 -0
- package/src/web/scrapers/sec-edgar.ts +261 -0
- package/src/web/scrapers/semantic-scholar.ts +171 -0
- package/src/web/scrapers/snapcraft.ts +187 -0
- package/src/web/scrapers/sourcegraph.ts +336 -0
- package/src/web/scrapers/spdx.ts +108 -0
- package/src/web/scrapers/spotify.ts +198 -0
- package/src/web/scrapers/stackoverflow.ts +120 -0
- package/src/web/scrapers/terraform.ts +277 -0
- package/src/web/scrapers/tldr.ts +47 -0
- package/src/web/scrapers/twitter.ts +94 -0
- package/src/web/scrapers/types.ts +397 -0
- package/src/web/scrapers/utils.ts +109 -0
- package/src/web/scrapers/vimeo.ts +133 -0
- package/src/web/scrapers/vscode-marketplace.ts +187 -0
- package/src/web/scrapers/w3c.ts +156 -0
- package/src/web/scrapers/wikidata.ts +344 -0
- package/src/web/scrapers/wikipedia.ts +84 -0
- package/src/web/scrapers/youtube.ts +325 -0
- package/src/web/search/index.ts +292 -0
- package/src/web/search/provider.ts +157 -0
- package/src/web/search/providers/anthropic.ts +318 -0
- package/src/web/search/providers/base.ts +89 -0
- package/src/web/search/providers/brave.ts +152 -0
- package/src/web/search/providers/codex.ts +591 -0
- package/src/web/search/providers/exa.ts +400 -0
- package/src/web/search/providers/gemini.ts +460 -0
- package/src/web/search/providers/jina.ts +111 -0
- package/src/web/search/providers/kagi.ts +86 -0
- package/src/web/search/providers/kimi.ts +196 -0
- package/src/web/search/providers/parallel.ts +225 -0
- package/src/web/search/providers/perplexity.ts +730 -0
- package/src/web/search/providers/searxng.ts +313 -0
- package/src/web/search/providers/synthetic.ts +114 -0
- package/src/web/search/providers/tavily.ts +176 -0
- package/src/web/search/providers/utils.ts +128 -0
- package/src/web/search/providers/zai.ts +333 -0
- package/src/web/search/render.ts +262 -0
- package/src/web/search/types.ts +482 -0
- package/src/web/search/utils.ts +17 -0
- package/src/workspace-tree.ts +286 -0
package/src/lsp/index.ts
ADDED
|
@@ -0,0 +1,2477 @@
|
|
|
1
|
+
import * as fs from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import type {
|
|
4
|
+
AgentTool,
|
|
5
|
+
AgentToolContext,
|
|
6
|
+
AgentToolResult,
|
|
7
|
+
AgentToolUpdateCallback,
|
|
8
|
+
ToolApprovalDecision,
|
|
9
|
+
} from "@oh-my-pi/pi-agent-core";
|
|
10
|
+
import { logger, once, prompt, untilAborted } from "@oh-my-pi/pi-utils";
|
|
11
|
+
import type { BunFile } from "bun";
|
|
12
|
+
import { type Theme, theme } from "../modes/theme/theme";
|
|
13
|
+
import lspDescription from "../prompts/tools/lsp.md" with { type: "text" };
|
|
14
|
+
import type { ToolSession } from "../tools";
|
|
15
|
+
import { truncateForPrompt } from "../tools/approval";
|
|
16
|
+
import { formatPathRelativeToCwd, resolveToCwd } from "../tools/path-utils";
|
|
17
|
+
import { ToolAbortError, ToolError, throwIfAborted } from "../tools/tool-errors";
|
|
18
|
+
import { clampTimeout } from "../tools/tool-timeouts";
|
|
19
|
+
import {
|
|
20
|
+
ensureFileOpen,
|
|
21
|
+
getActiveClients,
|
|
22
|
+
getOrCreateClient,
|
|
23
|
+
type LspServerStatus,
|
|
24
|
+
notifySaved,
|
|
25
|
+
refreshFile,
|
|
26
|
+
sendNotification,
|
|
27
|
+
sendRequest,
|
|
28
|
+
setIdleTimeout,
|
|
29
|
+
syncContent,
|
|
30
|
+
WARMUP_TIMEOUT_MS,
|
|
31
|
+
waitForProjectLoaded,
|
|
32
|
+
} from "./client";
|
|
33
|
+
import { getLinterClient } from "./clients";
|
|
34
|
+
import { getServersForFile, type LspConfig, loadConfig } from "./config";
|
|
35
|
+
import {
|
|
36
|
+
applyTextEdits,
|
|
37
|
+
applyTextEditsToString,
|
|
38
|
+
applyWorkspaceEdit,
|
|
39
|
+
flattenWorkspaceTextEdits,
|
|
40
|
+
rangesOverlap,
|
|
41
|
+
} from "./edits";
|
|
42
|
+
import { detectLspmux } from "./lspmux";
|
|
43
|
+
import {
|
|
44
|
+
type CodeAction,
|
|
45
|
+
type CodeActionContext,
|
|
46
|
+
type Command,
|
|
47
|
+
type Diagnostic,
|
|
48
|
+
type DocumentSymbol,
|
|
49
|
+
type Hover,
|
|
50
|
+
type Location,
|
|
51
|
+
type LocationLink,
|
|
52
|
+
type LspClient,
|
|
53
|
+
type LspParams,
|
|
54
|
+
type LspToolDetails,
|
|
55
|
+
lspSchema,
|
|
56
|
+
type Position,
|
|
57
|
+
type PublishedDiagnostics,
|
|
58
|
+
type ServerConfig,
|
|
59
|
+
type SymbolInformation,
|
|
60
|
+
type TextEdit,
|
|
61
|
+
type WorkspaceEdit,
|
|
62
|
+
} from "./types";
|
|
63
|
+
import {
|
|
64
|
+
applyCodeAction,
|
|
65
|
+
dedupeWorkspaceSymbols,
|
|
66
|
+
extractHoverText,
|
|
67
|
+
fileToUri,
|
|
68
|
+
filterWorkspaceSymbols,
|
|
69
|
+
formatCodeAction,
|
|
70
|
+
formatDiagnostic,
|
|
71
|
+
formatDiagnosticsSummary,
|
|
72
|
+
formatDocumentSymbol,
|
|
73
|
+
formatGroupedDiagnosticMessages,
|
|
74
|
+
formatLocation,
|
|
75
|
+
formatSymbolInformation,
|
|
76
|
+
formatWorkspaceEdit,
|
|
77
|
+
readLocationContext,
|
|
78
|
+
resolveDiagnosticTargets,
|
|
79
|
+
resolveSymbolColumn,
|
|
80
|
+
sortDiagnostics,
|
|
81
|
+
summarizeDiagnosticMessages,
|
|
82
|
+
symbolKindToIcon,
|
|
83
|
+
uriToFile,
|
|
84
|
+
} from "./utils";
|
|
85
|
+
|
|
86
|
+
export type { LspServerStatus } from "./client";
|
|
87
|
+
export type { LspToolDetails } from "./types";
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* LSP actions that do not mutate the workspace or language-server state.
|
|
91
|
+
* Anything not in this set (rename, code_actions with apply, rename_file,
|
|
92
|
+
* reload, raw request, etc.) is classified as write-tier.
|
|
93
|
+
*/
|
|
94
|
+
export const LSP_READONLY_ACTIONS: ReadonlySet<string> = new Set([
|
|
95
|
+
"diagnostics",
|
|
96
|
+
"definition",
|
|
97
|
+
"type_definition",
|
|
98
|
+
"implementation",
|
|
99
|
+
"references",
|
|
100
|
+
"hover",
|
|
101
|
+
"symbols",
|
|
102
|
+
"status",
|
|
103
|
+
"capabilities",
|
|
104
|
+
]);
|
|
105
|
+
|
|
106
|
+
export interface LspStartupServerInfo {
|
|
107
|
+
name: string;
|
|
108
|
+
status: "connecting" | "ready" | "error" | "available";
|
|
109
|
+
fileTypes: string[];
|
|
110
|
+
error?: string;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/** Result from warming up LSP servers */
|
|
114
|
+
export interface LspWarmupResult {
|
|
115
|
+
servers: Array<LspStartupServerInfo & { status: "ready" | "error" }>;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/** Options for warming up LSP servers */
|
|
119
|
+
export interface LspWarmupOptions {
|
|
120
|
+
/** Called when starting to connect to servers */
|
|
121
|
+
onConnecting?: (serverNames: string[]) => void;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
export function discoverStartupLspServers(
|
|
125
|
+
cwd: string,
|
|
126
|
+
status: LspStartupServerInfo["status"] = "connecting",
|
|
127
|
+
): LspStartupServerInfo[] {
|
|
128
|
+
const config = loadConfig(cwd);
|
|
129
|
+
return getLspServers(config).map(([name, serverConfig]) => ({
|
|
130
|
+
name,
|
|
131
|
+
status,
|
|
132
|
+
fileTypes: serverConfig.fileTypes,
|
|
133
|
+
}));
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* Warm up LSP servers for a directory by connecting to all detected servers.
|
|
138
|
+
* This should be called at startup to avoid cold-start delays.
|
|
139
|
+
*
|
|
140
|
+
* @param cwd - Working directory to detect and start servers for
|
|
141
|
+
* @param options - Optional callbacks for progress reporting
|
|
142
|
+
* @returns Status of each server that was started
|
|
143
|
+
*/
|
|
144
|
+
export async function warmupLspServers(cwd: string, options?: LspWarmupOptions): Promise<LspWarmupResult> {
|
|
145
|
+
const config = loadConfig(cwd);
|
|
146
|
+
setIdleTimeout(config.idleTimeoutMs);
|
|
147
|
+
const servers: LspWarmupResult["servers"] = [];
|
|
148
|
+
const lspServers = getLspServers(config);
|
|
149
|
+
|
|
150
|
+
// Notify caller which servers we're connecting to
|
|
151
|
+
if (lspServers.length > 0 && options?.onConnecting) {
|
|
152
|
+
options.onConnecting(lspServers.map(([name]) => name));
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
// Start all detected servers in parallel with a short timeout
|
|
156
|
+
// Servers that don't respond quickly will be initialized lazily on first use
|
|
157
|
+
const results = await Promise.allSettled(
|
|
158
|
+
lspServers.map(async ([name, serverConfig]) => {
|
|
159
|
+
const client = await getOrCreateClient(serverConfig, cwd, serverConfig.warmupTimeoutMs ?? WARMUP_TIMEOUT_MS);
|
|
160
|
+
return { name, client, fileTypes: serverConfig.fileTypes };
|
|
161
|
+
}),
|
|
162
|
+
);
|
|
163
|
+
|
|
164
|
+
for (let i = 0; i < results.length; i++) {
|
|
165
|
+
const result = results[i];
|
|
166
|
+
const [name, serverConfig] = lspServers[i];
|
|
167
|
+
if (result.status === "fulfilled") {
|
|
168
|
+
servers.push({
|
|
169
|
+
name: result.value.name,
|
|
170
|
+
status: "ready",
|
|
171
|
+
fileTypes: result.value.fileTypes,
|
|
172
|
+
});
|
|
173
|
+
} else {
|
|
174
|
+
const errorMsg = result.reason?.message ?? String(result.reason);
|
|
175
|
+
logger.warn("LSP server failed to start", { server: name, error: errorMsg });
|
|
176
|
+
servers.push({
|
|
177
|
+
name,
|
|
178
|
+
status: "error",
|
|
179
|
+
fileTypes: serverConfig.fileTypes,
|
|
180
|
+
error: errorMsg,
|
|
181
|
+
});
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
return { servers };
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* Get status of currently active LSP servers.
|
|
190
|
+
*/
|
|
191
|
+
export function getLspStatus(): LspServerStatus[] {
|
|
192
|
+
return getActiveClients();
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* Sync in-memory file content to all applicable LSP servers.
|
|
197
|
+
* Sends didOpen (if new) or didChange (if already open).
|
|
198
|
+
*
|
|
199
|
+
* @param absolutePath - Absolute path to the file
|
|
200
|
+
* @param content - The new file content
|
|
201
|
+
* @param cwd - Working directory for LSP config resolution
|
|
202
|
+
* @param servers - Servers to sync to
|
|
203
|
+
*/
|
|
204
|
+
async function syncFileContent(
|
|
205
|
+
absolutePath: string,
|
|
206
|
+
content: string,
|
|
207
|
+
cwd: string,
|
|
208
|
+
servers: Array<[string, ServerConfig]>,
|
|
209
|
+
signal?: AbortSignal,
|
|
210
|
+
): Promise<void> {
|
|
211
|
+
throwIfAborted(signal);
|
|
212
|
+
await Promise.allSettled(
|
|
213
|
+
servers.map(async ([_serverName, serverConfig]) => {
|
|
214
|
+
throwIfAborted(signal);
|
|
215
|
+
if (serverConfig.createClient) {
|
|
216
|
+
return;
|
|
217
|
+
}
|
|
218
|
+
const client = await getOrCreateClient(serverConfig, cwd);
|
|
219
|
+
throwIfAborted(signal);
|
|
220
|
+
await syncContent(client, absolutePath, content, signal);
|
|
221
|
+
}),
|
|
222
|
+
);
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
/**
|
|
226
|
+
* Notify all LSP servers that a file was saved.
|
|
227
|
+
* Assumes content was already synced via syncFileContent.
|
|
228
|
+
*
|
|
229
|
+
* @param absolutePath - Absolute path to the file
|
|
230
|
+
* @param cwd - Working directory for LSP config resolution
|
|
231
|
+
* @param servers - Servers to notify
|
|
232
|
+
*/
|
|
233
|
+
async function notifyFileSaved(
|
|
234
|
+
absolutePath: string,
|
|
235
|
+
cwd: string,
|
|
236
|
+
servers: Array<[string, ServerConfig]>,
|
|
237
|
+
signal?: AbortSignal,
|
|
238
|
+
): Promise<void> {
|
|
239
|
+
throwIfAborted(signal);
|
|
240
|
+
await Promise.allSettled(
|
|
241
|
+
servers.map(async ([_serverName, serverConfig]) => {
|
|
242
|
+
throwIfAborted(signal);
|
|
243
|
+
if (serverConfig.createClient) {
|
|
244
|
+
return;
|
|
245
|
+
}
|
|
246
|
+
const client = await getOrCreateClient(serverConfig, cwd);
|
|
247
|
+
await notifySaved(client, absolutePath, signal);
|
|
248
|
+
}),
|
|
249
|
+
);
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
// Cache config per cwd to avoid repeated file I/O
|
|
253
|
+
const configCache = new Map<string, LspConfig>();
|
|
254
|
+
|
|
255
|
+
function getConfig(cwd: string): LspConfig {
|
|
256
|
+
let config = configCache.get(cwd);
|
|
257
|
+
if (!config) {
|
|
258
|
+
config = loadConfig(cwd);
|
|
259
|
+
setIdleTimeout(config.idleTimeoutMs);
|
|
260
|
+
configCache.set(cwd, config);
|
|
261
|
+
}
|
|
262
|
+
return config;
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
function isCustomLinter(serverConfig: ServerConfig): boolean {
|
|
266
|
+
return Boolean(serverConfig.createClient);
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
function splitServers(servers: Array<[string, ServerConfig]>): {
|
|
270
|
+
lspServers: Array<[string, ServerConfig]>;
|
|
271
|
+
customLinterServers: Array<[string, ServerConfig]>;
|
|
272
|
+
} {
|
|
273
|
+
const lspServers: Array<[string, ServerConfig]> = [];
|
|
274
|
+
const customLinterServers: Array<[string, ServerConfig]> = [];
|
|
275
|
+
for (const entry of servers) {
|
|
276
|
+
if (isCustomLinter(entry[1])) {
|
|
277
|
+
customLinterServers.push(entry);
|
|
278
|
+
} else {
|
|
279
|
+
lspServers.push(entry);
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
return { lspServers, customLinterServers };
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
function getLspServers(config: LspConfig): Array<[string, ServerConfig]> {
|
|
286
|
+
return (Object.entries(config.servers) as Array<[string, ServerConfig]>).filter(
|
|
287
|
+
([, serverConfig]) => !isCustomLinter(serverConfig),
|
|
288
|
+
);
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
function getLspServersForFile(config: LspConfig, filePath: string): Array<[string, ServerConfig]> {
|
|
292
|
+
return getServersForFile(config, filePath).filter(([, serverConfig]) => !isCustomLinter(serverConfig));
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
function getLspServerForFile(config: LspConfig, filePath: string): [string, ServerConfig] | null {
|
|
296
|
+
const servers = getLspServersForFile(config, filePath);
|
|
297
|
+
return servers.length > 0 ? servers[0] : null;
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
function isProjectAwareLspServer(serverConfig: ServerConfig): boolean {
|
|
301
|
+
return !serverConfig.createClient && !serverConfig.isLinter;
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
const DIAGNOSTIC_MESSAGE_LIMIT = 50;
|
|
305
|
+
const SINGLE_DIAGNOSTICS_WAIT_TIMEOUT_MS = 3000;
|
|
306
|
+
const BATCH_DIAGNOSTICS_WAIT_TIMEOUT_MS = 400;
|
|
307
|
+
const DIAGNOSTICS_POLL_MS = 100;
|
|
308
|
+
const DIAGNOSTICS_SETTLE_MS = 250;
|
|
309
|
+
/**
|
|
310
|
+
* How long the edit/write writethrough blocks inline waiting for fresh
|
|
311
|
+
* diagnostics before handing slow servers off to the deferred late-injection
|
|
312
|
+
* channel. Keeps the common fast-server case inline while letting an edit
|
|
313
|
+
* return promptly when a server (e.g. a large-monorepo tsserver) is slow to
|
|
314
|
+
* publish fresh diagnostics.
|
|
315
|
+
*/
|
|
316
|
+
const INLINE_DIAGNOSTICS_WAIT_TIMEOUT_MS = 500;
|
|
317
|
+
/**
|
|
318
|
+
* Inner per-server diagnostics wait budget for the background/deferred fetch.
|
|
319
|
+
* Longer than the inline cap (and the old 3s default) so a slow server still
|
|
320
|
+
* delivers late instead of giving up before it ever publishes.
|
|
321
|
+
*/
|
|
322
|
+
const DEFERRED_DIAGNOSTICS_WAIT_TIMEOUT_MS = 12_000;
|
|
323
|
+
const MAX_GLOB_DIAGNOSTIC_TARGETS = 20;
|
|
324
|
+
const WORKSPACE_SYMBOL_LIMIT = 200;
|
|
325
|
+
const PROJECT_INDEXED_ACTIONS: ReadonlySet<string> = new Set([
|
|
326
|
+
"definition",
|
|
327
|
+
"type_definition",
|
|
328
|
+
"implementation",
|
|
329
|
+
"references",
|
|
330
|
+
"rename",
|
|
331
|
+
"hover",
|
|
332
|
+
]);
|
|
333
|
+
|
|
334
|
+
const RUST_WORKSPACE_MARKERS = ["Cargo.toml", "rust-analyzer.toml"] as const;
|
|
335
|
+
|
|
336
|
+
function hasRustWorkspaceAncestor(filePath: string): boolean {
|
|
337
|
+
let dir = path.dirname(filePath);
|
|
338
|
+
while (true) {
|
|
339
|
+
for (const marker of RUST_WORKSPACE_MARKERS) {
|
|
340
|
+
if (fs.existsSync(path.join(dir, marker))) {
|
|
341
|
+
return true;
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
const parent = path.dirname(dir);
|
|
345
|
+
if (parent === dir) {
|
|
346
|
+
return false;
|
|
347
|
+
}
|
|
348
|
+
dir = parent;
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
function limitDiagnosticMessages(messages: string[]): string[] {
|
|
353
|
+
if (messages.length <= DIAGNOSTIC_MESSAGE_LIMIT) {
|
|
354
|
+
return messages;
|
|
355
|
+
}
|
|
356
|
+
return messages.slice(0, DIAGNOSTIC_MESSAGE_LIMIT);
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
const LOCATION_CONTEXT_LINES = 1;
|
|
360
|
+
const REFERENCE_CONTEXT_LIMIT = 50;
|
|
361
|
+
|
|
362
|
+
const REFERENCES_RETRY_COUNT = 2;
|
|
363
|
+
const REFERENCES_RETRY_DELAY_MS = 250;
|
|
364
|
+
|
|
365
|
+
function comparePosition(a: Position, b: Position): number {
|
|
366
|
+
return a.line === b.line ? a.character - b.character : a.line - b.line;
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
function rangeContainsPosition(range: Location["range"], position: Position): boolean {
|
|
370
|
+
return comparePosition(range.start, position) <= 0 && comparePosition(position, range.end) <= 0;
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
function isOnlyQueriedDeclaration(locations: Location[], uri: string, position: Position): boolean {
|
|
374
|
+
return locations.length === 1 && locations[0]?.uri === uri && rangeContainsPosition(locations[0].range, position);
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
function normalizeLocationResult(result: Location | Location[] | LocationLink | LocationLink[] | null): Location[] {
|
|
378
|
+
if (!result) return [];
|
|
379
|
+
const raw = Array.isArray(result) ? result : [result];
|
|
380
|
+
return raw.flatMap(loc => {
|
|
381
|
+
if ("uri" in loc) {
|
|
382
|
+
return [loc as Location];
|
|
383
|
+
}
|
|
384
|
+
if ("targetUri" in loc) {
|
|
385
|
+
const link = loc as LocationLink;
|
|
386
|
+
return [{ uri: link.targetUri, range: link.targetSelectionRange ?? link.targetRange }];
|
|
387
|
+
}
|
|
388
|
+
return [];
|
|
389
|
+
});
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
async function formatLocationWithContext(location: Location, cwd: string): Promise<string> {
|
|
393
|
+
const header = ` ${formatLocation(location, cwd)}`;
|
|
394
|
+
const context = await readLocationContext(
|
|
395
|
+
uriToFile(location.uri),
|
|
396
|
+
location.range.start.line + 1,
|
|
397
|
+
LOCATION_CONTEXT_LINES,
|
|
398
|
+
);
|
|
399
|
+
if (context.length === 0) {
|
|
400
|
+
return header;
|
|
401
|
+
}
|
|
402
|
+
return `${header}\n${context.map(lineText => ` ${lineText}`).join("\n")}`;
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
const MAX_RENAME_PAIRS = 1000;
|
|
406
|
+
|
|
407
|
+
interface FileRenamePair {
|
|
408
|
+
oldUri: string;
|
|
409
|
+
newUri: string;
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
/**
|
|
413
|
+
* Enumerate the {oldUri, newUri} pairs needed for an LSP willRenameFiles/didRenameFiles request.
|
|
414
|
+
* For files this is a single pair. For directories this walks every regular file underneath
|
|
415
|
+
* and produces a parallel pair anchored at the new directory root.
|
|
416
|
+
*/
|
|
417
|
+
async function enumerateRenamePairs(
|
|
418
|
+
source: string,
|
|
419
|
+
dest: string,
|
|
420
|
+
): Promise<{ pairs: FileRenamePair[]; directory: boolean; exceeded: boolean }> {
|
|
421
|
+
const stat = await fs.promises.stat(source);
|
|
422
|
+
if (!stat.isDirectory()) {
|
|
423
|
+
return {
|
|
424
|
+
pairs: [{ oldUri: fileToUri(source), newUri: fileToUri(dest) }],
|
|
425
|
+
directory: false,
|
|
426
|
+
exceeded: false,
|
|
427
|
+
};
|
|
428
|
+
}
|
|
429
|
+
const entries = await fs.promises.readdir(source, { recursive: true, withFileTypes: true });
|
|
430
|
+
const pairs: FileRenamePair[] = [];
|
|
431
|
+
for (const entry of entries) {
|
|
432
|
+
if (!entry.isFile()) continue;
|
|
433
|
+
if (pairs.length >= MAX_RENAME_PAIRS) {
|
|
434
|
+
return { pairs, directory: true, exceeded: true };
|
|
435
|
+
}
|
|
436
|
+
const parent = entry.parentPath ?? source;
|
|
437
|
+
const absOld = path.join(parent, entry.name);
|
|
438
|
+
const rel = path.relative(source, absOld);
|
|
439
|
+
pairs.push({
|
|
440
|
+
oldUri: fileToUri(absOld),
|
|
441
|
+
newUri: fileToUri(path.join(dest, rel)),
|
|
442
|
+
});
|
|
443
|
+
}
|
|
444
|
+
return { pairs, directory: true, exceeded: false };
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
/** True when an LSP error indicates the server doesn't implement the requested method. */
|
|
448
|
+
function isMethodNotFoundError(err: unknown): boolean {
|
|
449
|
+
if (!(err instanceof Error)) return false;
|
|
450
|
+
const msg = err.message.toLowerCase();
|
|
451
|
+
return (
|
|
452
|
+
msg.includes("method not found") ||
|
|
453
|
+
msg.includes("unhandled method") ||
|
|
454
|
+
msg.includes("not supported") ||
|
|
455
|
+
msg.includes("-32601")
|
|
456
|
+
);
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
async function reloadServer(client: LspClient, serverName: string, signal?: AbortSignal): Promise<string> {
|
|
460
|
+
// rust-analyzer exposes a real reload request.
|
|
461
|
+
try {
|
|
462
|
+
await sendRequest(client, "rust-analyzer/reloadWorkspace", null, signal);
|
|
463
|
+
return `Reloaded ${serverName}`;
|
|
464
|
+
} catch {
|
|
465
|
+
// Method not supported — fall through.
|
|
466
|
+
}
|
|
467
|
+
// workspace/didChangeConfiguration is a notification per spec; sending it
|
|
468
|
+
// as a request hangs until the tool deadline on servers that route it to
|
|
469
|
+
// the notification handler and never respond.
|
|
470
|
+
try {
|
|
471
|
+
await sendNotification(client, "workspace/didChangeConfiguration", { settings: {} });
|
|
472
|
+
return `Reloaded ${serverName}`;
|
|
473
|
+
} catch {
|
|
474
|
+
client.proc.kill();
|
|
475
|
+
return `Restarted ${serverName}`;
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
interface WaitForDiagnosticsOptions {
|
|
480
|
+
timeoutMs?: number;
|
|
481
|
+
signal?: AbortSignal;
|
|
482
|
+
minVersion?: number;
|
|
483
|
+
expectedDocumentVersion?: number;
|
|
484
|
+
/**
|
|
485
|
+
* Quiescence window (ms). typescript-language-server never echoes the document
|
|
486
|
+
* version (issue #983) and emits diagnostics from several sources at different
|
|
487
|
+
* times, so there is no single "complete, version-matched" publish to gate on.
|
|
488
|
+
* When the server does not exact-version-match, accept the latest publish only
|
|
489
|
+
* after no newer one has arrived for this long, letting an in-flight pre-edit
|
|
490
|
+
* publish be superseded by the fresh one.
|
|
491
|
+
*/
|
|
492
|
+
settleMs?: number;
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
async function waitForDiagnostics(
|
|
496
|
+
client: LspClient,
|
|
497
|
+
uri: string,
|
|
498
|
+
options: WaitForDiagnosticsOptions = {},
|
|
499
|
+
): Promise<Diagnostic[]> {
|
|
500
|
+
const { timeoutMs = 3000, signal, minVersion, expectedDocumentVersion, settleMs = DIAGNOSTICS_SETTLE_MS } = options;
|
|
501
|
+
const start = Date.now();
|
|
502
|
+
let settledRef: PublishedDiagnostics | undefined;
|
|
503
|
+
let settledAt = 0;
|
|
504
|
+
while (Date.now() - start < timeoutMs) {
|
|
505
|
+
throwIfAborted(signal);
|
|
506
|
+
const versionOk = minVersion === undefined || client.diagnosticsVersion > minVersion;
|
|
507
|
+
const published = client.diagnostics.get(uri);
|
|
508
|
+
if (published && versionOk) {
|
|
509
|
+
// Server honored our exact document version → authoritative, accept now.
|
|
510
|
+
if (expectedDocumentVersion !== undefined && published.version === expectedDocumentVersion) {
|
|
511
|
+
return published.diagnostics;
|
|
512
|
+
}
|
|
513
|
+
// Unversioned/mismatched publish: wait for the stream to go quiet so an
|
|
514
|
+
// in-flight publish for the pre-edit content is superseded by the fresh one.
|
|
515
|
+
if (published !== settledRef) {
|
|
516
|
+
settledRef = published;
|
|
517
|
+
settledAt = Date.now();
|
|
518
|
+
} else if (Date.now() - settledAt >= settleMs) {
|
|
519
|
+
return published.diagnostics;
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
await Bun.sleep(DIAGNOSTICS_POLL_MS);
|
|
523
|
+
}
|
|
524
|
+
const versionOk = minVersion === undefined || client.diagnosticsVersion > minVersion;
|
|
525
|
+
if (!versionOk) {
|
|
526
|
+
return [];
|
|
527
|
+
}
|
|
528
|
+
return client.diagnostics.get(uri)?.diagnostics ?? [];
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
/** Project type detection result */
|
|
532
|
+
interface ProjectType {
|
|
533
|
+
type: "rust" | "typescript" | "go" | "python" | "unknown";
|
|
534
|
+
command?: string[];
|
|
535
|
+
description: string;
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
/** Detect project type from root markers */
|
|
539
|
+
function detectProjectType(cwd: string): ProjectType {
|
|
540
|
+
// Check for Rust (Cargo.toml)
|
|
541
|
+
if (fs.existsSync(path.join(cwd, "Cargo.toml"))) {
|
|
542
|
+
return { type: "rust", command: ["cargo", "check", "--message-format=short"], description: "Rust (cargo check)" };
|
|
543
|
+
}
|
|
544
|
+
|
|
545
|
+
// Check for TypeScript (tsconfig.json)
|
|
546
|
+
if (fs.existsSync(path.join(cwd, "tsconfig.json"))) {
|
|
547
|
+
return { type: "typescript", command: ["npx", "tsc", "--noEmit"], description: "TypeScript (tsc --noEmit)" };
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
// Check for Go (go.mod)
|
|
551
|
+
if (fs.existsSync(path.join(cwd, "go.mod"))) {
|
|
552
|
+
return { type: "go", command: ["go", "build", "./..."], description: "Go (go build)" };
|
|
553
|
+
}
|
|
554
|
+
|
|
555
|
+
// Check for Python (pyproject.toml or pyrightconfig.json)
|
|
556
|
+
if (fs.existsSync(path.join(cwd, "pyproject.toml")) || fs.existsSync(path.join(cwd, "pyrightconfig.json"))) {
|
|
557
|
+
return { type: "python", command: ["pyright"], description: "Python (pyright)" };
|
|
558
|
+
}
|
|
559
|
+
|
|
560
|
+
return { type: "unknown", description: "Unknown project type" };
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
/** Run workspace diagnostics command and parse output */
|
|
564
|
+
async function runWorkspaceDiagnostics(
|
|
565
|
+
cwd: string,
|
|
566
|
+
signal?: AbortSignal,
|
|
567
|
+
): Promise<{ output: string; projectType: ProjectType }> {
|
|
568
|
+
throwIfAborted(signal);
|
|
569
|
+
const projectType = detectProjectType(cwd);
|
|
570
|
+
if (!projectType.command) {
|
|
571
|
+
return {
|
|
572
|
+
output: `Cannot detect project type. Supported: Rust (Cargo.toml), TypeScript (tsconfig.json), Go (go.mod), Python (pyproject.toml)`,
|
|
573
|
+
projectType,
|
|
574
|
+
};
|
|
575
|
+
}
|
|
576
|
+
const proc = Bun.spawn(projectType.command, {
|
|
577
|
+
cwd,
|
|
578
|
+
stdout: "pipe",
|
|
579
|
+
stderr: "pipe",
|
|
580
|
+
windowsHide: true,
|
|
581
|
+
});
|
|
582
|
+
const abortHandler = () => {
|
|
583
|
+
proc.kill();
|
|
584
|
+
};
|
|
585
|
+
if (signal) {
|
|
586
|
+
signal.addEventListener("abort", abortHandler, { once: true });
|
|
587
|
+
}
|
|
588
|
+
|
|
589
|
+
try {
|
|
590
|
+
const [stdout, stderr] = await Promise.all([new Response(proc.stdout).text(), new Response(proc.stderr).text()]);
|
|
591
|
+
await proc.exited;
|
|
592
|
+
throwIfAborted(signal);
|
|
593
|
+
const combined = (stdout + stderr).trim();
|
|
594
|
+
if (!combined) {
|
|
595
|
+
return { output: "No issues found", projectType };
|
|
596
|
+
}
|
|
597
|
+
// Limit output length
|
|
598
|
+
const lines = combined.split("\n");
|
|
599
|
+
if (lines.length > 50) {
|
|
600
|
+
return { output: `${lines.slice(0, 50).join("\n")}\n... and ${lines.length - 50} more lines`, projectType };
|
|
601
|
+
}
|
|
602
|
+
return { output: combined, projectType };
|
|
603
|
+
} catch (e) {
|
|
604
|
+
if (signal?.aborted) {
|
|
605
|
+
throw new ToolAbortError();
|
|
606
|
+
}
|
|
607
|
+
return { output: `Failed to run ${projectType.command.join(" ")}: ${e}`, projectType };
|
|
608
|
+
} finally {
|
|
609
|
+
signal?.removeEventListener("abort", abortHandler);
|
|
610
|
+
}
|
|
611
|
+
}
|
|
612
|
+
|
|
613
|
+
/** Result from getDiagnosticsForFile */
|
|
614
|
+
export interface FileDiagnosticsResult {
|
|
615
|
+
/** Name of the LSP server used (if available) */
|
|
616
|
+
server?: string;
|
|
617
|
+
/** Formatted diagnostic messages */
|
|
618
|
+
messages: string[];
|
|
619
|
+
/** Summary string (e.g., "2 error(s), 1 warning(s)") */
|
|
620
|
+
summary: string;
|
|
621
|
+
/** Whether there are any errors (severity 1) */
|
|
622
|
+
errored: boolean;
|
|
623
|
+
/** Whether the file was formatted */
|
|
624
|
+
formatter?: FileFormatResult;
|
|
625
|
+
}
|
|
626
|
+
|
|
627
|
+
type ServerVersionMap = Map<string, number>;
|
|
628
|
+
|
|
629
|
+
interface GetDiagnosticsForFileOptions {
|
|
630
|
+
signal?: AbortSignal;
|
|
631
|
+
minVersions?: ServerVersionMap;
|
|
632
|
+
expectedDocumentVersions?: ServerVersionMap;
|
|
633
|
+
/** Per-server wait budget (ms). Defaults to {@link SINGLE_DIAGNOSTICS_WAIT_TIMEOUT_MS}. */
|
|
634
|
+
timeoutMs?: number;
|
|
635
|
+
}
|
|
636
|
+
|
|
637
|
+
/**
|
|
638
|
+
* Capture current diagnostic versions for all LSP servers.
|
|
639
|
+
* Call this BEFORE syncing content to detect stale diagnostics later.
|
|
640
|
+
*/
|
|
641
|
+
async function captureDiagnosticVersions(
|
|
642
|
+
cwd: string,
|
|
643
|
+
servers: Array<[string, ServerConfig]>,
|
|
644
|
+
initTimeoutMs?: number,
|
|
645
|
+
): Promise<ServerVersionMap> {
|
|
646
|
+
const versions = new Map<string, number>();
|
|
647
|
+
await Promise.allSettled(
|
|
648
|
+
servers.map(async ([serverName, serverConfig]) => {
|
|
649
|
+
if (serverConfig.createClient) return;
|
|
650
|
+
const client = await getOrCreateClient(serverConfig, cwd, initTimeoutMs);
|
|
651
|
+
versions.set(serverName, client.diagnosticsVersion);
|
|
652
|
+
}),
|
|
653
|
+
);
|
|
654
|
+
return versions;
|
|
655
|
+
}
|
|
656
|
+
|
|
657
|
+
async function captureOpenFileVersions(
|
|
658
|
+
absolutePath: string,
|
|
659
|
+
cwd: string,
|
|
660
|
+
servers: Array<[string, ServerConfig]>,
|
|
661
|
+
): Promise<ServerVersionMap> {
|
|
662
|
+
const uri = fileToUri(absolutePath);
|
|
663
|
+
const versions = new Map<string, number>();
|
|
664
|
+
await Promise.allSettled(
|
|
665
|
+
servers.map(async ([serverName, serverConfig]) => {
|
|
666
|
+
const client = await getOrCreateClient(serverConfig, cwd);
|
|
667
|
+
const version = client.openFiles.get(uri)?.version;
|
|
668
|
+
if (version !== undefined) {
|
|
669
|
+
versions.set(serverName, version);
|
|
670
|
+
}
|
|
671
|
+
}),
|
|
672
|
+
);
|
|
673
|
+
return versions;
|
|
674
|
+
}
|
|
675
|
+
|
|
676
|
+
/**
|
|
677
|
+
* Get diagnostics for a file using LSP or custom linter client.
|
|
678
|
+
*
|
|
679
|
+
* @param absolutePath - Absolute path to the file
|
|
680
|
+
* @param cwd - Working directory for LSP config resolution
|
|
681
|
+
* @param servers - Servers to query diagnostics for
|
|
682
|
+
* @param minVersions - Minimum diagnostic versions per server (to detect stale results)
|
|
683
|
+
* @returns Diagnostic results or undefined if no servers
|
|
684
|
+
*/
|
|
685
|
+
async function getDiagnosticsForFile(
|
|
686
|
+
absolutePath: string,
|
|
687
|
+
cwd: string,
|
|
688
|
+
servers: Array<[string, ServerConfig]>,
|
|
689
|
+
options: GetDiagnosticsForFileOptions = {},
|
|
690
|
+
): Promise<FileDiagnosticsResult | undefined> {
|
|
691
|
+
const { signal, minVersions, expectedDocumentVersions, timeoutMs } = options;
|
|
692
|
+
if (servers.length === 0) {
|
|
693
|
+
return undefined;
|
|
694
|
+
}
|
|
695
|
+
|
|
696
|
+
const uri = fileToUri(absolutePath);
|
|
697
|
+
const relPath = formatPathRelativeToCwd(absolutePath, cwd);
|
|
698
|
+
const allDiagnostics: Diagnostic[] = [];
|
|
699
|
+
const serverNames: string[] = [];
|
|
700
|
+
|
|
701
|
+
// Wait for diagnostics from all servers in parallel
|
|
702
|
+
const results = await Promise.allSettled(
|
|
703
|
+
servers.map(async ([serverName, serverConfig]) => {
|
|
704
|
+
throwIfAborted(signal);
|
|
705
|
+
// Use custom linter client if configured
|
|
706
|
+
if (serverConfig.createClient) {
|
|
707
|
+
const linterClient = getLinterClient(serverName, serverConfig, cwd);
|
|
708
|
+
const diagnostics = await linterClient.lint(absolutePath);
|
|
709
|
+
return { serverName, diagnostics };
|
|
710
|
+
}
|
|
711
|
+
|
|
712
|
+
// Default: use LSP
|
|
713
|
+
const client = await getOrCreateClient(serverConfig, cwd);
|
|
714
|
+
throwIfAborted(signal);
|
|
715
|
+
if (isProjectAwareLspServer(serverConfig)) {
|
|
716
|
+
await waitForProjectLoaded(client, signal);
|
|
717
|
+
throwIfAborted(signal);
|
|
718
|
+
}
|
|
719
|
+
// Content already synced + didSave sent, wait for fresh diagnostics
|
|
720
|
+
const minVersion = minVersions?.get(serverName);
|
|
721
|
+
const expectedDocumentVersion = expectedDocumentVersions?.get(serverName);
|
|
722
|
+
const diagnostics = await waitForDiagnostics(client, uri, {
|
|
723
|
+
timeoutMs: timeoutMs ?? SINGLE_DIAGNOSTICS_WAIT_TIMEOUT_MS,
|
|
724
|
+
signal,
|
|
725
|
+
minVersion,
|
|
726
|
+
expectedDocumentVersion,
|
|
727
|
+
});
|
|
728
|
+
return { serverName, diagnostics };
|
|
729
|
+
}),
|
|
730
|
+
);
|
|
731
|
+
|
|
732
|
+
for (const result of results) {
|
|
733
|
+
if (result.status === "fulfilled") {
|
|
734
|
+
serverNames.push(result.value.serverName);
|
|
735
|
+
allDiagnostics.push(...result.value.diagnostics);
|
|
736
|
+
}
|
|
737
|
+
}
|
|
738
|
+
|
|
739
|
+
if (serverNames.length === 0) {
|
|
740
|
+
return undefined;
|
|
741
|
+
}
|
|
742
|
+
|
|
743
|
+
if (allDiagnostics.length === 0) {
|
|
744
|
+
return {
|
|
745
|
+
server: serverNames.join(", "),
|
|
746
|
+
messages: [],
|
|
747
|
+
summary: "OK",
|
|
748
|
+
errored: false,
|
|
749
|
+
};
|
|
750
|
+
}
|
|
751
|
+
|
|
752
|
+
// Deduplicate diagnostics by range + message (different servers might report similar issues)
|
|
753
|
+
const seen = new Set<string>();
|
|
754
|
+
const uniqueDiagnostics: Diagnostic[] = [];
|
|
755
|
+
for (const d of allDiagnostics) {
|
|
756
|
+
const key = `${d.range.start.line}:${d.range.start.character}:${d.range.end.line}:${d.range.end.character}:${d.message}`;
|
|
757
|
+
if (!seen.has(key)) {
|
|
758
|
+
seen.add(key);
|
|
759
|
+
uniqueDiagnostics.push(d);
|
|
760
|
+
}
|
|
761
|
+
}
|
|
762
|
+
|
|
763
|
+
sortDiagnostics(uniqueDiagnostics);
|
|
764
|
+
const formatted = uniqueDiagnostics.map(d => formatDiagnostic(d, relPath));
|
|
765
|
+
const limited = limitDiagnosticMessages(formatted);
|
|
766
|
+
const summary = formatDiagnosticsSummary(uniqueDiagnostics);
|
|
767
|
+
const hasErrors = uniqueDiagnostics.some(d => d.severity === 1);
|
|
768
|
+
|
|
769
|
+
return {
|
|
770
|
+
server: serverNames.join(", "),
|
|
771
|
+
messages: limited,
|
|
772
|
+
summary,
|
|
773
|
+
errored: hasErrors,
|
|
774
|
+
};
|
|
775
|
+
}
|
|
776
|
+
|
|
777
|
+
export enum FileFormatResult {
|
|
778
|
+
UNCHANGED = "unchanged",
|
|
779
|
+
FORMATTED = "formatted",
|
|
780
|
+
}
|
|
781
|
+
|
|
782
|
+
/** Default formatting options for LSP */
|
|
783
|
+
const DEFAULT_FORMAT_OPTIONS = {
|
|
784
|
+
tabSize: 3,
|
|
785
|
+
insertSpaces: true,
|
|
786
|
+
trimTrailingWhitespace: true,
|
|
787
|
+
insertFinalNewline: true,
|
|
788
|
+
trimFinalNewlines: true,
|
|
789
|
+
};
|
|
790
|
+
|
|
791
|
+
/**
|
|
792
|
+
* Format content using LSP or custom linter client.
|
|
793
|
+
*
|
|
794
|
+
* @param absolutePath - Absolute path (for URI)
|
|
795
|
+
* @param content - Content to format
|
|
796
|
+
* @param cwd - Working directory for LSP config resolution
|
|
797
|
+
* @param servers - Servers to try formatting with
|
|
798
|
+
* @returns Formatted content, or original if no formatter available
|
|
799
|
+
*/
|
|
800
|
+
async function formatContent(
|
|
801
|
+
absolutePath: string,
|
|
802
|
+
content: string,
|
|
803
|
+
cwd: string,
|
|
804
|
+
servers: Array<[string, ServerConfig]>,
|
|
805
|
+
signal?: AbortSignal,
|
|
806
|
+
): Promise<string> {
|
|
807
|
+
if (servers.length === 0) {
|
|
808
|
+
return content;
|
|
809
|
+
}
|
|
810
|
+
|
|
811
|
+
const uri = fileToUri(absolutePath);
|
|
812
|
+
|
|
813
|
+
for (const [serverName, serverConfig] of servers) {
|
|
814
|
+
try {
|
|
815
|
+
throwIfAborted(signal);
|
|
816
|
+
// Use custom linter client if configured
|
|
817
|
+
if (serverConfig.createClient) {
|
|
818
|
+
const linterClient = getLinterClient(serverName, serverConfig, cwd);
|
|
819
|
+
return await linterClient.format(absolutePath, content);
|
|
820
|
+
}
|
|
821
|
+
|
|
822
|
+
// Default: use LSP
|
|
823
|
+
const client = await getOrCreateClient(serverConfig, cwd);
|
|
824
|
+
throwIfAborted(signal);
|
|
825
|
+
|
|
826
|
+
const caps = client.serverCapabilities;
|
|
827
|
+
if (!caps?.documentFormattingProvider) {
|
|
828
|
+
continue;
|
|
829
|
+
}
|
|
830
|
+
|
|
831
|
+
// Request formatting (content already synced)
|
|
832
|
+
const edits = (await sendRequest(
|
|
833
|
+
client,
|
|
834
|
+
"textDocument/formatting",
|
|
835
|
+
{
|
|
836
|
+
textDocument: { uri },
|
|
837
|
+
options: DEFAULT_FORMAT_OPTIONS,
|
|
838
|
+
},
|
|
839
|
+
signal,
|
|
840
|
+
)) as TextEdit[] | null;
|
|
841
|
+
|
|
842
|
+
if (!edits || edits.length === 0) {
|
|
843
|
+
return content;
|
|
844
|
+
}
|
|
845
|
+
|
|
846
|
+
// Apply edits in-memory and return
|
|
847
|
+
return applyTextEditsToString(content, edits);
|
|
848
|
+
} catch {}
|
|
849
|
+
}
|
|
850
|
+
|
|
851
|
+
return content;
|
|
852
|
+
}
|
|
853
|
+
|
|
854
|
+
/** Options for creating the LSP writethrough callback */
|
|
855
|
+
export interface WritethroughOptions {
|
|
856
|
+
/** Whether to format the file using LSP after writing */
|
|
857
|
+
enableFormat?: boolean;
|
|
858
|
+
/** Whether to get LSP diagnostics after writing */
|
|
859
|
+
enableDiagnostics?: boolean;
|
|
860
|
+
/** Called when diagnostics arrive after the main timeout. */
|
|
861
|
+
onDeferredDiagnostics?: (diagnostics: FileDiagnosticsResult) => void;
|
|
862
|
+
/** Signal to cancel a pending deferred diagnostics fetch. */
|
|
863
|
+
deferredSignal?: AbortSignal;
|
|
864
|
+
/** Transform diagnostics before surfacing them after a successful fetch. */
|
|
865
|
+
transformDiagnostics?: (absPath: string, result: FileDiagnosticsResult) => FileDiagnosticsResult;
|
|
866
|
+
}
|
|
867
|
+
|
|
868
|
+
/** Internal resolved form of {@link WritethroughOptions} that the writethrough machinery operates on. */
|
|
869
|
+
type ResolvedWritethroughOptions = {
|
|
870
|
+
enableFormat: boolean;
|
|
871
|
+
enableDiagnostics: boolean;
|
|
872
|
+
transformDiagnostics?: (absPath: string, result: FileDiagnosticsResult) => FileDiagnosticsResult;
|
|
873
|
+
};
|
|
874
|
+
|
|
875
|
+
/** Per-file deferred LSP diagnostics wiring for {@link WritethroughCallback}. */
|
|
876
|
+
export type WritethroughDeferredHandle = {
|
|
877
|
+
onDeferredDiagnostics: (diagnostics: FileDiagnosticsResult) => void;
|
|
878
|
+
signal: AbortSignal;
|
|
879
|
+
finalize: (diagnostics: FileDiagnosticsResult | undefined) => void;
|
|
880
|
+
};
|
|
881
|
+
|
|
882
|
+
/** Callback type for the LSP writethrough */
|
|
883
|
+
export type WritethroughCallback = (
|
|
884
|
+
dst: string,
|
|
885
|
+
content: string,
|
|
886
|
+
signal?: AbortSignal,
|
|
887
|
+
file?: BunFile,
|
|
888
|
+
batch?: LspWritethroughBatchRequest,
|
|
889
|
+
getDeferred?: (dst: string) => WritethroughDeferredHandle | undefined,
|
|
890
|
+
) => Promise<FileDiagnosticsResult | undefined>;
|
|
891
|
+
|
|
892
|
+
/** No-op writethrough callback */
|
|
893
|
+
export async function writethroughNoop(
|
|
894
|
+
dst: string,
|
|
895
|
+
content: string,
|
|
896
|
+
_signal?: AbortSignal,
|
|
897
|
+
file?: BunFile,
|
|
898
|
+
_batch?: LspWritethroughBatchRequest,
|
|
899
|
+
_getDeferred?: (dst: string) => WritethroughDeferredHandle | undefined,
|
|
900
|
+
): Promise<FileDiagnosticsResult | undefined> {
|
|
901
|
+
if (file) {
|
|
902
|
+
await file.write(content);
|
|
903
|
+
} else {
|
|
904
|
+
await Bun.write(dst, content);
|
|
905
|
+
}
|
|
906
|
+
return undefined;
|
|
907
|
+
}
|
|
908
|
+
|
|
909
|
+
interface PendingWritethrough {
|
|
910
|
+
dst: string;
|
|
911
|
+
content: string;
|
|
912
|
+
file?: BunFile;
|
|
913
|
+
}
|
|
914
|
+
|
|
915
|
+
interface LspWritethroughBatchRequest {
|
|
916
|
+
id: string;
|
|
917
|
+
flush: boolean;
|
|
918
|
+
}
|
|
919
|
+
|
|
920
|
+
interface LspWritethroughBatchState {
|
|
921
|
+
entries: Map<string, PendingWritethrough>;
|
|
922
|
+
options: ResolvedWritethroughOptions;
|
|
923
|
+
}
|
|
924
|
+
|
|
925
|
+
const writethroughBatches = new Map<string, LspWritethroughBatchState>();
|
|
926
|
+
|
|
927
|
+
function getOrCreateWritethroughBatch(id: string, options: ResolvedWritethroughOptions): LspWritethroughBatchState {
|
|
928
|
+
const existing = writethroughBatches.get(id);
|
|
929
|
+
if (existing) {
|
|
930
|
+
existing.options.enableFormat ||= options.enableFormat;
|
|
931
|
+
existing.options.enableDiagnostics ||= options.enableDiagnostics;
|
|
932
|
+
existing.options.transformDiagnostics ??= options.transformDiagnostics;
|
|
933
|
+
return existing;
|
|
934
|
+
}
|
|
935
|
+
const batch: LspWritethroughBatchState = {
|
|
936
|
+
entries: new Map<string, PendingWritethrough>(),
|
|
937
|
+
options: { ...options },
|
|
938
|
+
};
|
|
939
|
+
writethroughBatches.set(id, batch);
|
|
940
|
+
return batch;
|
|
941
|
+
}
|
|
942
|
+
|
|
943
|
+
export async function flushLspWritethroughBatch(
|
|
944
|
+
id: string,
|
|
945
|
+
cwd: string,
|
|
946
|
+
signal?: AbortSignal,
|
|
947
|
+
): Promise<FileDiagnosticsResult | undefined> {
|
|
948
|
+
const state = writethroughBatches.get(id);
|
|
949
|
+
if (!state) {
|
|
950
|
+
return undefined;
|
|
951
|
+
}
|
|
952
|
+
writethroughBatches.delete(id);
|
|
953
|
+
return flushWritethroughBatch(Array.from(state.entries.values()), cwd, state.options, signal);
|
|
954
|
+
}
|
|
955
|
+
|
|
956
|
+
function mergeDiagnostics(
|
|
957
|
+
results: Array<FileDiagnosticsResult | undefined>,
|
|
958
|
+
options: ResolvedWritethroughOptions,
|
|
959
|
+
): FileDiagnosticsResult | undefined {
|
|
960
|
+
const messages: string[] = [];
|
|
961
|
+
const servers = new Set<string>();
|
|
962
|
+
let hasResults = false;
|
|
963
|
+
let hasFormatter = false;
|
|
964
|
+
let formatted = false;
|
|
965
|
+
|
|
966
|
+
for (const result of results) {
|
|
967
|
+
if (!result) continue;
|
|
968
|
+
hasResults = true;
|
|
969
|
+
if (result.server) {
|
|
970
|
+
for (const server of result.server.split(",")) {
|
|
971
|
+
const trimmed = server.trim();
|
|
972
|
+
if (trimmed) {
|
|
973
|
+
servers.add(trimmed);
|
|
974
|
+
}
|
|
975
|
+
}
|
|
976
|
+
}
|
|
977
|
+
if (result.messages.length > 0) {
|
|
978
|
+
messages.push(...result.messages);
|
|
979
|
+
}
|
|
980
|
+
if (result.formatter !== undefined) {
|
|
981
|
+
hasFormatter = true;
|
|
982
|
+
if (result.formatter === FileFormatResult.FORMATTED) {
|
|
983
|
+
formatted = true;
|
|
984
|
+
}
|
|
985
|
+
}
|
|
986
|
+
}
|
|
987
|
+
|
|
988
|
+
if (!hasResults && !hasFormatter) {
|
|
989
|
+
return undefined;
|
|
990
|
+
}
|
|
991
|
+
|
|
992
|
+
let summary = options.enableDiagnostics ? "no issues" : "OK";
|
|
993
|
+
let errored = false;
|
|
994
|
+
let limitedMessages = messages;
|
|
995
|
+
if (messages.length > 0) {
|
|
996
|
+
const summaryInfo = summarizeDiagnosticMessages(messages);
|
|
997
|
+
summary = summaryInfo.summary;
|
|
998
|
+
errored = summaryInfo.errored;
|
|
999
|
+
limitedMessages = limitDiagnosticMessages(messages);
|
|
1000
|
+
}
|
|
1001
|
+
const formatter = hasFormatter ? (formatted ? FileFormatResult.FORMATTED : FileFormatResult.UNCHANGED) : undefined;
|
|
1002
|
+
|
|
1003
|
+
return {
|
|
1004
|
+
server: servers.size > 0 ? Array.from(servers).join(", ") : undefined,
|
|
1005
|
+
messages: limitedMessages,
|
|
1006
|
+
summary,
|
|
1007
|
+
errored,
|
|
1008
|
+
formatter,
|
|
1009
|
+
};
|
|
1010
|
+
}
|
|
1011
|
+
|
|
1012
|
+
async function scheduleDeferredDiagnosticsFetch(args: {
|
|
1013
|
+
dst: string;
|
|
1014
|
+
cwd: string;
|
|
1015
|
+
servers: Array<[string, ServerConfig]>;
|
|
1016
|
+
minVersions: ServerVersionMap | undefined;
|
|
1017
|
+
expectedDocumentVersions: ServerVersionMap | undefined;
|
|
1018
|
+
signal: AbortSignal;
|
|
1019
|
+
callback: (diagnostics: FileDiagnosticsResult) => void;
|
|
1020
|
+
}): Promise<void> {
|
|
1021
|
+
try {
|
|
1022
|
+
const deferredTimeout = AbortSignal.timeout(25_000);
|
|
1023
|
+
const combined = AbortSignal.any([args.signal, deferredTimeout]);
|
|
1024
|
+
const diagnostics = await getDiagnosticsForFile(args.dst, args.cwd, args.servers, {
|
|
1025
|
+
signal: combined,
|
|
1026
|
+
minVersions: args.minVersions,
|
|
1027
|
+
expectedDocumentVersions: args.expectedDocumentVersions,
|
|
1028
|
+
timeoutMs: DEFERRED_DIAGNOSTICS_WAIT_TIMEOUT_MS,
|
|
1029
|
+
});
|
|
1030
|
+
if (args.signal.aborted || diagnostics === undefined) return;
|
|
1031
|
+
args.callback(diagnostics);
|
|
1032
|
+
} catch {
|
|
1033
|
+
// Cancelled or LSP gave up; silently discard.
|
|
1034
|
+
}
|
|
1035
|
+
}
|
|
1036
|
+
|
|
1037
|
+
/**
|
|
1038
|
+
* Fetch post-write diagnostics without making the edit/write block on a slow
|
|
1039
|
+
* language server.
|
|
1040
|
+
*
|
|
1041
|
+
* Blocks inline only briefly ({@link INLINE_DIAGNOSTICS_WAIT_TIMEOUT_MS}) for a
|
|
1042
|
+
* fresh result. Freshness is enforced by the pre-edit `minVersions` baseline:
|
|
1043
|
+
* exact document-version matches return immediately, and unversioned/mismatched
|
|
1044
|
+
* publishes must settle with no newer publish before inline acceptance. If
|
|
1045
|
+
* nothing fresh arrives in the inline window and a deferred
|
|
1046
|
+
* channel is available, the in-flight fetch is handed off to deliver late via
|
|
1047
|
+
* `onDeferredDiagnostics`, and this returns `undefined` so the tool result
|
|
1048
|
+
* lands immediately. Without a deferred channel (direct/CI callers) it blocks
|
|
1049
|
+
* for the standard budget so the result is still returned inline.
|
|
1050
|
+
*/
|
|
1051
|
+
async function fetchDiagnosticsWithDeferral(args: {
|
|
1052
|
+
dst: string;
|
|
1053
|
+
cwd: string;
|
|
1054
|
+
servers: Array<[string, ServerConfig]>;
|
|
1055
|
+
minVersions: ServerVersionMap | undefined;
|
|
1056
|
+
expectedDocumentVersions: ServerVersionMap | undefined;
|
|
1057
|
+
transformDiagnostics?: ResolvedWritethroughOptions["transformDiagnostics"];
|
|
1058
|
+
deferred?: { onDeferredDiagnostics: (diagnostics: FileDiagnosticsResult) => void; signal: AbortSignal };
|
|
1059
|
+
signal?: AbortSignal;
|
|
1060
|
+
}): Promise<FileDiagnosticsResult | undefined> {
|
|
1061
|
+
const { dst, cwd, servers, minVersions, expectedDocumentVersions, transformDiagnostics, deferred, signal } = args;
|
|
1062
|
+
const apply = (d: FileDiagnosticsResult | undefined) =>
|
|
1063
|
+
d && transformDiagnostics ? transformDiagnostics(dst, d) : d;
|
|
1064
|
+
|
|
1065
|
+
if (!deferred) {
|
|
1066
|
+
// No late-injection channel: block for the standard budget and return inline.
|
|
1067
|
+
return apply(
|
|
1068
|
+
await getDiagnosticsForFile(dst, cwd, servers, {
|
|
1069
|
+
signal,
|
|
1070
|
+
minVersions,
|
|
1071
|
+
expectedDocumentVersions,
|
|
1072
|
+
}),
|
|
1073
|
+
);
|
|
1074
|
+
}
|
|
1075
|
+
|
|
1076
|
+
// One background fetch with a generous inner budget; await it only briefly inline.
|
|
1077
|
+
const fetchPromise = getDiagnosticsForFile(dst, cwd, servers, {
|
|
1078
|
+
signal: deferred.signal,
|
|
1079
|
+
minVersions,
|
|
1080
|
+
expectedDocumentVersions,
|
|
1081
|
+
timeoutMs: DEFERRED_DIAGNOSTICS_WAIT_TIMEOUT_MS,
|
|
1082
|
+
});
|
|
1083
|
+
const INLINE_TIMEOUT = Symbol("inline-diagnostics-timeout");
|
|
1084
|
+
const raced = await Promise.race([
|
|
1085
|
+
fetchPromise,
|
|
1086
|
+
Bun.sleep(INLINE_DIAGNOSTICS_WAIT_TIMEOUT_MS).then(() => INLINE_TIMEOUT),
|
|
1087
|
+
]);
|
|
1088
|
+
if (raced !== INLINE_TIMEOUT) {
|
|
1089
|
+
return apply(raced as FileDiagnosticsResult | undefined);
|
|
1090
|
+
}
|
|
1091
|
+
// Slow server: deliver late via the deferred channel; nothing inline. The
|
|
1092
|
+
// deferred sink (edit tool) applies its own dedup, so pass the raw result.
|
|
1093
|
+
void fetchPromise
|
|
1094
|
+
.then(diagnostics => {
|
|
1095
|
+
if (diagnostics && !deferred.signal.aborted) deferred.onDeferredDiagnostics(diagnostics);
|
|
1096
|
+
})
|
|
1097
|
+
.catch(() => {});
|
|
1098
|
+
return undefined;
|
|
1099
|
+
}
|
|
1100
|
+
|
|
1101
|
+
async function runLspWritethrough(
|
|
1102
|
+
dst: string,
|
|
1103
|
+
content: string,
|
|
1104
|
+
cwd: string,
|
|
1105
|
+
options: ResolvedWritethroughOptions,
|
|
1106
|
+
signal?: AbortSignal,
|
|
1107
|
+
file?: BunFile,
|
|
1108
|
+
deferred?: {
|
|
1109
|
+
onDeferredDiagnostics: (diagnostics: FileDiagnosticsResult) => void;
|
|
1110
|
+
signal: AbortSignal;
|
|
1111
|
+
},
|
|
1112
|
+
): Promise<FileDiagnosticsResult | undefined> {
|
|
1113
|
+
const { enableFormat, enableDiagnostics } = options;
|
|
1114
|
+
const config = getConfig(cwd);
|
|
1115
|
+
const servers = getServersForFile(config, dst);
|
|
1116
|
+
if (servers.length === 0) {
|
|
1117
|
+
return writethroughNoop(dst, content, signal, file);
|
|
1118
|
+
}
|
|
1119
|
+
const { lspServers, customLinterServers } = splitServers(servers);
|
|
1120
|
+
|
|
1121
|
+
let finalContent = content;
|
|
1122
|
+
const writeContent = async (value: string) => (file ? file.write(value) : Bun.write(dst, value));
|
|
1123
|
+
const getWritePromise = once(() => writeContent(finalContent));
|
|
1124
|
+
const useCustomFormatter = enableFormat && customLinterServers.length > 0;
|
|
1125
|
+
|
|
1126
|
+
// Capture diagnostic versions BEFORE syncing to detect stale diagnostics
|
|
1127
|
+
// Bound client creation by the writethrough budget: a hung/broken server
|
|
1128
|
+
// must not add its full init wait (30s default) to every edit.
|
|
1129
|
+
const minVersions = enableDiagnostics ? await captureDiagnosticVersions(cwd, servers, 5_000) : undefined;
|
|
1130
|
+
let expectedDocumentVersions: ServerVersionMap | undefined;
|
|
1131
|
+
|
|
1132
|
+
let formatter: FileFormatResult | undefined;
|
|
1133
|
+
let diagnostics: FileDiagnosticsResult | undefined;
|
|
1134
|
+
let timedOut = false;
|
|
1135
|
+
let synced = false;
|
|
1136
|
+
try {
|
|
1137
|
+
const timeoutSignal = AbortSignal.timeout(5_000);
|
|
1138
|
+
timeoutSignal.addEventListener(
|
|
1139
|
+
"abort",
|
|
1140
|
+
() => {
|
|
1141
|
+
timedOut = true;
|
|
1142
|
+
},
|
|
1143
|
+
{ once: true },
|
|
1144
|
+
);
|
|
1145
|
+
const operationSignal = signal ? AbortSignal.any([signal, timeoutSignal]) : timeoutSignal;
|
|
1146
|
+
await untilAborted(operationSignal, async () => {
|
|
1147
|
+
if (useCustomFormatter) {
|
|
1148
|
+
// Custom linters (e.g. Biome CLI) require on-disk input.
|
|
1149
|
+
await writeContent(content);
|
|
1150
|
+
finalContent = await formatContent(dst, content, cwd, customLinterServers, operationSignal);
|
|
1151
|
+
formatter = finalContent !== content ? FileFormatResult.FORMATTED : FileFormatResult.UNCHANGED;
|
|
1152
|
+
await writeContent(finalContent);
|
|
1153
|
+
await syncFileContent(dst, finalContent, cwd, lspServers, operationSignal);
|
|
1154
|
+
} else {
|
|
1155
|
+
// 1. Sync original content to LSP servers
|
|
1156
|
+
await syncFileContent(dst, content, cwd, lspServers, operationSignal);
|
|
1157
|
+
|
|
1158
|
+
// 2. Format in-memory via LSP
|
|
1159
|
+
if (enableFormat) {
|
|
1160
|
+
finalContent = await formatContent(dst, content, cwd, lspServers, operationSignal);
|
|
1161
|
+
formatter = finalContent !== content ? FileFormatResult.FORMATTED : FileFormatResult.UNCHANGED;
|
|
1162
|
+
}
|
|
1163
|
+
|
|
1164
|
+
// 3. If formatted, sync formatted content to LSP servers
|
|
1165
|
+
if (finalContent !== content) {
|
|
1166
|
+
await syncFileContent(dst, finalContent, cwd, lspServers, operationSignal);
|
|
1167
|
+
}
|
|
1168
|
+
|
|
1169
|
+
// 4. Write to disk
|
|
1170
|
+
await getWritePromise();
|
|
1171
|
+
}
|
|
1172
|
+
|
|
1173
|
+
if (enableDiagnostics) {
|
|
1174
|
+
expectedDocumentVersions = await captureOpenFileVersions(dst, cwd, lspServers);
|
|
1175
|
+
}
|
|
1176
|
+
|
|
1177
|
+
// 5. Notify saved to LSP servers
|
|
1178
|
+
await notifyFileSaved(dst, cwd, lspServers, operationSignal);
|
|
1179
|
+
});
|
|
1180
|
+
synced = true;
|
|
1181
|
+
} catch {
|
|
1182
|
+
if (timedOut) {
|
|
1183
|
+
formatter = undefined;
|
|
1184
|
+
diagnostics = undefined;
|
|
1185
|
+
// Schedule background diagnostic fetch if caller wants deferred results
|
|
1186
|
+
if (deferred && !deferred.signal.aborted && enableDiagnostics) {
|
|
1187
|
+
void scheduleDeferredDiagnosticsFetch({
|
|
1188
|
+
dst,
|
|
1189
|
+
cwd,
|
|
1190
|
+
servers,
|
|
1191
|
+
minVersions,
|
|
1192
|
+
expectedDocumentVersions,
|
|
1193
|
+
signal: deferred.signal,
|
|
1194
|
+
callback: deferred.onDeferredDiagnostics,
|
|
1195
|
+
});
|
|
1196
|
+
}
|
|
1197
|
+
}
|
|
1198
|
+
await getWritePromise();
|
|
1199
|
+
}
|
|
1200
|
+
|
|
1201
|
+
if (synced && enableDiagnostics) {
|
|
1202
|
+
diagnostics = await fetchDiagnosticsWithDeferral({
|
|
1203
|
+
dst,
|
|
1204
|
+
cwd,
|
|
1205
|
+
servers,
|
|
1206
|
+
minVersions,
|
|
1207
|
+
expectedDocumentVersions,
|
|
1208
|
+
transformDiagnostics: options.transformDiagnostics,
|
|
1209
|
+
deferred,
|
|
1210
|
+
signal,
|
|
1211
|
+
});
|
|
1212
|
+
}
|
|
1213
|
+
|
|
1214
|
+
if (formatter !== undefined) {
|
|
1215
|
+
diagnostics ??= {
|
|
1216
|
+
server: servers.map(([name]) => name).join(", "),
|
|
1217
|
+
messages: [],
|
|
1218
|
+
summary: "OK",
|
|
1219
|
+
errored: false,
|
|
1220
|
+
};
|
|
1221
|
+
diagnostics.formatter = formatter;
|
|
1222
|
+
}
|
|
1223
|
+
|
|
1224
|
+
return diagnostics;
|
|
1225
|
+
}
|
|
1226
|
+
|
|
1227
|
+
async function flushWritethroughBatch(
|
|
1228
|
+
batch: PendingWritethrough[],
|
|
1229
|
+
cwd: string,
|
|
1230
|
+
options: ResolvedWritethroughOptions,
|
|
1231
|
+
signal?: AbortSignal,
|
|
1232
|
+
getDeferred?: (dst: string) => WritethroughDeferredHandle | undefined,
|
|
1233
|
+
): Promise<FileDiagnosticsResult | undefined> {
|
|
1234
|
+
if (batch.length === 0) {
|
|
1235
|
+
return undefined;
|
|
1236
|
+
}
|
|
1237
|
+
const results: Array<FileDiagnosticsResult | undefined> = [];
|
|
1238
|
+
for (const entry of batch) {
|
|
1239
|
+
const bundle = getDeferred?.(entry.dst);
|
|
1240
|
+
const deferredInner =
|
|
1241
|
+
bundle &&
|
|
1242
|
+
({
|
|
1243
|
+
onDeferredDiagnostics: bundle.onDeferredDiagnostics,
|
|
1244
|
+
signal: bundle.signal,
|
|
1245
|
+
} as const);
|
|
1246
|
+
const diag = await runLspWritethrough(entry.dst, entry.content, cwd, options, signal, entry.file, deferredInner);
|
|
1247
|
+
bundle?.finalize(diag);
|
|
1248
|
+
results.push(diag);
|
|
1249
|
+
}
|
|
1250
|
+
return mergeDiagnostics(results, options);
|
|
1251
|
+
}
|
|
1252
|
+
|
|
1253
|
+
/** Create a writethrough callback for LSP aware write operations */
|
|
1254
|
+
export function createLspWritethrough(cwd: string, options?: WritethroughOptions): WritethroughCallback {
|
|
1255
|
+
const resolvedOptions: ResolvedWritethroughOptions = {
|
|
1256
|
+
enableFormat: options?.enableFormat ?? false,
|
|
1257
|
+
enableDiagnostics: options?.enableDiagnostics ?? false,
|
|
1258
|
+
transformDiagnostics: options?.transformDiagnostics,
|
|
1259
|
+
};
|
|
1260
|
+
if (!resolvedOptions.enableFormat && !resolvedOptions.enableDiagnostics) {
|
|
1261
|
+
return writethroughNoop;
|
|
1262
|
+
}
|
|
1263
|
+
return async (
|
|
1264
|
+
dst: string,
|
|
1265
|
+
content: string,
|
|
1266
|
+
signal?: AbortSignal,
|
|
1267
|
+
file?: BunFile,
|
|
1268
|
+
batch?: LspWritethroughBatchRequest,
|
|
1269
|
+
getDeferred?: (dst: string) => WritethroughDeferredHandle | undefined,
|
|
1270
|
+
) => {
|
|
1271
|
+
if (!batch) {
|
|
1272
|
+
const bundle = getDeferred?.(dst);
|
|
1273
|
+
const deferredInner =
|
|
1274
|
+
bundle &&
|
|
1275
|
+
({
|
|
1276
|
+
onDeferredDiagnostics: bundle.onDeferredDiagnostics,
|
|
1277
|
+
signal: bundle.signal,
|
|
1278
|
+
} as const);
|
|
1279
|
+
const diagnostics = await runLspWritethrough(dst, content, cwd, resolvedOptions, signal, file, deferredInner);
|
|
1280
|
+
bundle?.finalize(diagnostics);
|
|
1281
|
+
return diagnostics;
|
|
1282
|
+
}
|
|
1283
|
+
|
|
1284
|
+
const state = getOrCreateWritethroughBatch(batch.id, resolvedOptions);
|
|
1285
|
+
state.entries.set(dst, { dst, content, file });
|
|
1286
|
+
|
|
1287
|
+
if (!batch.flush) {
|
|
1288
|
+
await writethroughNoop(dst, content, signal, file);
|
|
1289
|
+
return undefined;
|
|
1290
|
+
}
|
|
1291
|
+
|
|
1292
|
+
writethroughBatches.delete(batch.id);
|
|
1293
|
+
return flushWritethroughBatch(Array.from(state.entries.values()), cwd, state.options, signal, getDeferred);
|
|
1294
|
+
};
|
|
1295
|
+
}
|
|
1296
|
+
|
|
1297
|
+
/**
|
|
1298
|
+
* LSP tool for language server protocol operations.
|
|
1299
|
+
*/
|
|
1300
|
+
export class LspTool implements AgentTool<typeof lspSchema, LspToolDetails, Theme> {
|
|
1301
|
+
readonly name = "lsp";
|
|
1302
|
+
readonly approval = (args: unknown): ToolApprovalDecision => {
|
|
1303
|
+
const rawAction = (args as Partial<LspParams>).action;
|
|
1304
|
+
const action = typeof rawAction === "string" ? rawAction.toLowerCase() : "";
|
|
1305
|
+
return LSP_READONLY_ACTIONS.has(action) ? "read" : "write";
|
|
1306
|
+
};
|
|
1307
|
+
readonly formatApprovalDetails = (args: unknown): string[] => {
|
|
1308
|
+
const params = args as Partial<LspParams>;
|
|
1309
|
+
const lines = [`Action: ${typeof params.action === "string" ? params.action : "(missing)"}`];
|
|
1310
|
+
if (typeof params.file === "string" && params.file.length > 0) {
|
|
1311
|
+
lines.push(`File: ${truncateForPrompt(params.file)}`);
|
|
1312
|
+
}
|
|
1313
|
+
return lines;
|
|
1314
|
+
};
|
|
1315
|
+
readonly label = "LSP";
|
|
1316
|
+
readonly loadMode = "discoverable";
|
|
1317
|
+
readonly summary = "Query LSP (language server) for diagnostics, hover info, and references";
|
|
1318
|
+
readonly description: string;
|
|
1319
|
+
readonly parameters = lspSchema;
|
|
1320
|
+
readonly strict = true;
|
|
1321
|
+
|
|
1322
|
+
constructor(private readonly session: ToolSession) {
|
|
1323
|
+
this.description = prompt.render(lspDescription);
|
|
1324
|
+
}
|
|
1325
|
+
|
|
1326
|
+
static createIf(session: ToolSession): LspTool | null {
|
|
1327
|
+
return session.enableLsp === false ? null : new LspTool(session);
|
|
1328
|
+
}
|
|
1329
|
+
|
|
1330
|
+
async execute(
|
|
1331
|
+
_toolCallId: string,
|
|
1332
|
+
params: LspParams,
|
|
1333
|
+
signal?: AbortSignal,
|
|
1334
|
+
_onUpdate?: AgentToolUpdateCallback<LspToolDetails>,
|
|
1335
|
+
_context?: AgentToolContext,
|
|
1336
|
+
): Promise<AgentToolResult<LspToolDetails>> {
|
|
1337
|
+
const { action, file, line, symbol, query, new_name, apply, timeout } = params;
|
|
1338
|
+
const timeoutSec = clampTimeout("lsp", timeout);
|
|
1339
|
+
const timeoutSignal = AbortSignal.timeout(timeoutSec * 1000);
|
|
1340
|
+
const callerSignal = signal;
|
|
1341
|
+
signal = callerSignal ? AbortSignal.any([callerSignal, timeoutSignal]) : timeoutSignal;
|
|
1342
|
+
throwIfAborted(signal);
|
|
1343
|
+
|
|
1344
|
+
const config = getConfig(this.session.cwd);
|
|
1345
|
+
|
|
1346
|
+
// Status action doesn't need a file
|
|
1347
|
+
if (action === "status") {
|
|
1348
|
+
const configuredNames = Object.keys(config.servers);
|
|
1349
|
+
const lspmuxState = await detectLspmux();
|
|
1350
|
+
const lspmuxStatus = lspmuxState.available
|
|
1351
|
+
? lspmuxState.running
|
|
1352
|
+
? "lspmux: active (multiplexing enabled)"
|
|
1353
|
+
: "lspmux: installed but server not running"
|
|
1354
|
+
: "";
|
|
1355
|
+
|
|
1356
|
+
// `Object.keys(config.servers)` reflects what is *configured & resolvable
|
|
1357
|
+
// on PATH* — it does NOT prove the server actually starts. A wrapper
|
|
1358
|
+
// binary that exits immediately (e.g. rustup without the rust-analyzer
|
|
1359
|
+
// component) still appears here. Distinguish "configured" from
|
|
1360
|
+
// "started" (have a live in-process client) so callers cannot mistake
|
|
1361
|
+
// presence-on-PATH for a working server.
|
|
1362
|
+
const startedClients = getActiveClients();
|
|
1363
|
+
const startedByConfigName = new Map<string, LspServerStatus>();
|
|
1364
|
+
// getActiveClients() reports `name = client.config.command` (the
|
|
1365
|
+
// unresolved binary name from defaults.json), so match against
|
|
1366
|
+
// `serverConfig.command`, not the resolved path.
|
|
1367
|
+
for (const [name, serverConfig] of Object.entries(config.servers)) {
|
|
1368
|
+
const matched = startedClients.find(c => c.name === serverConfig.command);
|
|
1369
|
+
if (matched) startedByConfigName.set(name, matched);
|
|
1370
|
+
}
|
|
1371
|
+
|
|
1372
|
+
const lines: string[] = [];
|
|
1373
|
+
if (configuredNames.length === 0) {
|
|
1374
|
+
lines.push("No language servers configured for this project");
|
|
1375
|
+
} else {
|
|
1376
|
+
const labelled = configuredNames.map(name => {
|
|
1377
|
+
const started = startedByConfigName.get(name);
|
|
1378
|
+
if (!started) return `${name} (configured, not started)`;
|
|
1379
|
+
return `${name} (${started.status})`;
|
|
1380
|
+
});
|
|
1381
|
+
lines.push(`Language servers: ${labelled.join(", ")}`);
|
|
1382
|
+
lines.push(
|
|
1383
|
+
" note: 'configured, not started' means the binary resolves on PATH but no request has spawned it yet; 'ready' means a client process is live for this cwd.",
|
|
1384
|
+
);
|
|
1385
|
+
}
|
|
1386
|
+
if (lspmuxStatus) lines.push(lspmuxStatus);
|
|
1387
|
+
|
|
1388
|
+
return {
|
|
1389
|
+
content: [{ type: "text", text: lines.join("\n") }],
|
|
1390
|
+
details: { action, success: true, request: params },
|
|
1391
|
+
};
|
|
1392
|
+
}
|
|
1393
|
+
|
|
1394
|
+
// Diagnostics can be batch or single-file - queries all applicable servers
|
|
1395
|
+
if (action === "diagnostics") {
|
|
1396
|
+
if (file === "*") {
|
|
1397
|
+
// `*` => run workspace diagnostics across all configured servers
|
|
1398
|
+
const result = await runWorkspaceDiagnostics(this.session.cwd, signal);
|
|
1399
|
+
return {
|
|
1400
|
+
content: [
|
|
1401
|
+
{
|
|
1402
|
+
type: "text",
|
|
1403
|
+
text: `Workspace diagnostics (${result.projectType.description}):\n${result.output}`,
|
|
1404
|
+
},
|
|
1405
|
+
],
|
|
1406
|
+
details: { action, success: true, request: params },
|
|
1407
|
+
};
|
|
1408
|
+
}
|
|
1409
|
+
|
|
1410
|
+
if (!file) {
|
|
1411
|
+
return {
|
|
1412
|
+
content: [
|
|
1413
|
+
{
|
|
1414
|
+
type: "text",
|
|
1415
|
+
text: "Error: file parameter required. Use `*` for workspace-wide diagnostics or a path/glob for specific files.",
|
|
1416
|
+
},
|
|
1417
|
+
],
|
|
1418
|
+
details: { action, success: false, request: params },
|
|
1419
|
+
};
|
|
1420
|
+
}
|
|
1421
|
+
|
|
1422
|
+
let targets: string[];
|
|
1423
|
+
let truncatedGlobTargets = false;
|
|
1424
|
+
const resolvedTargets = await resolveDiagnosticTargets(file, this.session.cwd, MAX_GLOB_DIAGNOSTIC_TARGETS);
|
|
1425
|
+
targets = resolvedTargets.matches;
|
|
1426
|
+
truncatedGlobTargets = resolvedTargets.truncated;
|
|
1427
|
+
|
|
1428
|
+
if (targets.length === 0) {
|
|
1429
|
+
return {
|
|
1430
|
+
content: [{ type: "text", text: `No files matched pattern: ${file}` }],
|
|
1431
|
+
details: { action, success: true, request: params },
|
|
1432
|
+
};
|
|
1433
|
+
}
|
|
1434
|
+
|
|
1435
|
+
const detailed = targets.length > 1 || truncatedGlobTargets;
|
|
1436
|
+
const diagnosticsWaitTimeoutMs = detailed
|
|
1437
|
+
? Math.min(BATCH_DIAGNOSTICS_WAIT_TIMEOUT_MS, timeoutSec * 1000)
|
|
1438
|
+
: Math.min(SINGLE_DIAGNOSTICS_WAIT_TIMEOUT_MS, timeoutSec * 1000);
|
|
1439
|
+
const results: string[] = [];
|
|
1440
|
+
const allServerNames = new Set<string>();
|
|
1441
|
+
if (truncatedGlobTargets) {
|
|
1442
|
+
results.push(
|
|
1443
|
+
`${theme.status.warning} Pattern matched more than ${MAX_GLOB_DIAGNOSTIC_TARGETS} files; showing first ${MAX_GLOB_DIAGNOSTIC_TARGETS}. Narrow the glob or use workspace diagnostics.`,
|
|
1444
|
+
);
|
|
1445
|
+
}
|
|
1446
|
+
|
|
1447
|
+
for (const target of targets) {
|
|
1448
|
+
throwIfAborted(signal);
|
|
1449
|
+
const resolved = resolveToCwd(target, this.session.cwd);
|
|
1450
|
+
const servers = getServersForFile(config, resolved);
|
|
1451
|
+
if (servers.length === 0) {
|
|
1452
|
+
results.push(`${theme.status.error} ${target}: No language server found`);
|
|
1453
|
+
continue;
|
|
1454
|
+
}
|
|
1455
|
+
|
|
1456
|
+
const uri = fileToUri(resolved);
|
|
1457
|
+
const relPath = formatPathRelativeToCwd(resolved, this.session.cwd);
|
|
1458
|
+
const allDiagnostics: Diagnostic[] = [];
|
|
1459
|
+
|
|
1460
|
+
// Query all applicable servers for this file
|
|
1461
|
+
for (const [serverName, serverConfig] of servers) {
|
|
1462
|
+
allServerNames.add(serverName);
|
|
1463
|
+
try {
|
|
1464
|
+
throwIfAborted(signal);
|
|
1465
|
+
if (serverConfig.createClient) {
|
|
1466
|
+
const linterClient = getLinterClient(serverName, serverConfig, this.session.cwd);
|
|
1467
|
+
const diagnostics = await linterClient.lint(resolved);
|
|
1468
|
+
allDiagnostics.push(...diagnostics);
|
|
1469
|
+
continue;
|
|
1470
|
+
}
|
|
1471
|
+
const client = await getOrCreateClient(serverConfig, this.session.cwd);
|
|
1472
|
+
if (isProjectAwareLspServer(serverConfig)) {
|
|
1473
|
+
await waitForProjectLoaded(client, signal);
|
|
1474
|
+
throwIfAborted(signal);
|
|
1475
|
+
}
|
|
1476
|
+
const minVersion = client.diagnosticsVersion;
|
|
1477
|
+
await refreshFile(client, resolved, signal);
|
|
1478
|
+
const expectedDocumentVersion = client.openFiles.get(uri)?.version;
|
|
1479
|
+
const diagnostics = await waitForDiagnostics(client, uri, {
|
|
1480
|
+
timeoutMs: diagnosticsWaitTimeoutMs,
|
|
1481
|
+
signal,
|
|
1482
|
+
minVersion,
|
|
1483
|
+
expectedDocumentVersion,
|
|
1484
|
+
});
|
|
1485
|
+
allDiagnostics.push(...diagnostics);
|
|
1486
|
+
} catch (err) {
|
|
1487
|
+
if (err instanceof ToolAbortError || signal?.aborted) {
|
|
1488
|
+
throw err;
|
|
1489
|
+
}
|
|
1490
|
+
// Server failed, continue with others
|
|
1491
|
+
}
|
|
1492
|
+
}
|
|
1493
|
+
|
|
1494
|
+
// Deduplicate diagnostics
|
|
1495
|
+
const seen = new Set<string>();
|
|
1496
|
+
const uniqueDiagnostics: Diagnostic[] = [];
|
|
1497
|
+
for (const d of allDiagnostics) {
|
|
1498
|
+
const key = `${d.range.start.line}:${d.range.start.character}:${d.range.end.line}:${d.range.end.character}:${d.message}`;
|
|
1499
|
+
if (!seen.has(key)) {
|
|
1500
|
+
seen.add(key);
|
|
1501
|
+
uniqueDiagnostics.push(d);
|
|
1502
|
+
}
|
|
1503
|
+
}
|
|
1504
|
+
|
|
1505
|
+
sortDiagnostics(uniqueDiagnostics);
|
|
1506
|
+
|
|
1507
|
+
if (!detailed && targets.length === 1) {
|
|
1508
|
+
if (uniqueDiagnostics.length === 0) {
|
|
1509
|
+
return {
|
|
1510
|
+
content: [{ type: "text", text: "OK" }],
|
|
1511
|
+
details: { action, serverName: Array.from(allServerNames).join(", "), success: true },
|
|
1512
|
+
};
|
|
1513
|
+
}
|
|
1514
|
+
|
|
1515
|
+
const summary = formatDiagnosticsSummary(uniqueDiagnostics);
|
|
1516
|
+
const formatted = uniqueDiagnostics.map(d => formatDiagnostic(d, relPath));
|
|
1517
|
+
const output = `${summary}:\n${formatGroupedDiagnosticMessages(formatted)}`;
|
|
1518
|
+
return {
|
|
1519
|
+
content: [{ type: "text", text: output }],
|
|
1520
|
+
details: { action, serverName: Array.from(allServerNames).join(", "), success: true },
|
|
1521
|
+
};
|
|
1522
|
+
}
|
|
1523
|
+
|
|
1524
|
+
if (uniqueDiagnostics.length === 0) {
|
|
1525
|
+
results.push(`${theme.status.success} ${relPath}: no issues`);
|
|
1526
|
+
} else {
|
|
1527
|
+
const summary = formatDiagnosticsSummary(uniqueDiagnostics);
|
|
1528
|
+
results.push(`${theme.status.error} ${relPath}: ${summary}`);
|
|
1529
|
+
const formatted = uniqueDiagnostics.map(d => formatDiagnostic(d, relPath));
|
|
1530
|
+
results.push(formatGroupedDiagnosticMessages(formatted));
|
|
1531
|
+
}
|
|
1532
|
+
}
|
|
1533
|
+
|
|
1534
|
+
return {
|
|
1535
|
+
content: [{ type: "text", text: results.join("\n") }],
|
|
1536
|
+
details: { action, serverName: Array.from(allServerNames).join(", "), success: true },
|
|
1537
|
+
};
|
|
1538
|
+
}
|
|
1539
|
+
|
|
1540
|
+
if (action === "rename_file") {
|
|
1541
|
+
if (!file || !new_name) {
|
|
1542
|
+
return {
|
|
1543
|
+
content: [
|
|
1544
|
+
{
|
|
1545
|
+
type: "text",
|
|
1546
|
+
text: "Error: rename_file requires both `file` (source path) and `new_name` (destination path)",
|
|
1547
|
+
},
|
|
1548
|
+
],
|
|
1549
|
+
details: { action, success: false, request: params },
|
|
1550
|
+
};
|
|
1551
|
+
}
|
|
1552
|
+
|
|
1553
|
+
const source = resolveToCwd(file, this.session.cwd);
|
|
1554
|
+
const dest = resolveToCwd(new_name, this.session.cwd);
|
|
1555
|
+
|
|
1556
|
+
if (source === dest) {
|
|
1557
|
+
return {
|
|
1558
|
+
content: [{ type: "text", text: "Error: source and destination paths are identical" }],
|
|
1559
|
+
details: { action, success: false, request: params },
|
|
1560
|
+
};
|
|
1561
|
+
}
|
|
1562
|
+
|
|
1563
|
+
let sourceStat: fs.Stats;
|
|
1564
|
+
try {
|
|
1565
|
+
sourceStat = await fs.promises.stat(source);
|
|
1566
|
+
} catch {
|
|
1567
|
+
return {
|
|
1568
|
+
content: [
|
|
1569
|
+
{
|
|
1570
|
+
type: "text",
|
|
1571
|
+
text: `Error: source path does not exist: ${formatPathRelativeToCwd(source, this.session.cwd)}`,
|
|
1572
|
+
},
|
|
1573
|
+
],
|
|
1574
|
+
details: { action, success: false, request: params },
|
|
1575
|
+
};
|
|
1576
|
+
}
|
|
1577
|
+
|
|
1578
|
+
let destExists = false;
|
|
1579
|
+
try {
|
|
1580
|
+
await fs.promises.stat(dest);
|
|
1581
|
+
destExists = true;
|
|
1582
|
+
} catch {
|
|
1583
|
+
// expected: destination must not exist
|
|
1584
|
+
}
|
|
1585
|
+
if (destExists) {
|
|
1586
|
+
return {
|
|
1587
|
+
content: [
|
|
1588
|
+
{
|
|
1589
|
+
type: "text",
|
|
1590
|
+
text: `Error: destination already exists: ${formatPathRelativeToCwd(dest, this.session.cwd)}`,
|
|
1591
|
+
},
|
|
1592
|
+
],
|
|
1593
|
+
details: { action, success: false, request: params },
|
|
1594
|
+
};
|
|
1595
|
+
}
|
|
1596
|
+
|
|
1597
|
+
const enumerated = await enumerateRenamePairs(source, dest);
|
|
1598
|
+
if (enumerated.exceeded) {
|
|
1599
|
+
return {
|
|
1600
|
+
content: [
|
|
1601
|
+
{
|
|
1602
|
+
type: "text",
|
|
1603
|
+
text: `Error: directory contains more than ${MAX_RENAME_PAIRS} files; rename in smaller batches to keep LSP edits accurate`,
|
|
1604
|
+
},
|
|
1605
|
+
],
|
|
1606
|
+
details: { action, success: false, request: params },
|
|
1607
|
+
};
|
|
1608
|
+
}
|
|
1609
|
+
const { pairs } = enumerated;
|
|
1610
|
+
if (pairs.length === 0) {
|
|
1611
|
+
return {
|
|
1612
|
+
content: [{ type: "text", text: "Error: no files to rename" }],
|
|
1613
|
+
details: { action, success: false, request: params },
|
|
1614
|
+
};
|
|
1615
|
+
}
|
|
1616
|
+
|
|
1617
|
+
const lspParams = { files: pairs };
|
|
1618
|
+
// Filter to servers whose fileTypes match either the source or any
|
|
1619
|
+
// destination path. Asking every configured server about a .md/.sql/.txt
|
|
1620
|
+
// rename used to stack up willRenameFiles requests against irrelevant
|
|
1621
|
+
// language servers and hit the wall-clock timeout. A server only has
|
|
1622
|
+
// something useful to say about a rename if it understands one of the
|
|
1623
|
+
// affected file extensions.
|
|
1624
|
+
const allLspServers = getLspServers(config);
|
|
1625
|
+
const relevantNames = new Set<string>();
|
|
1626
|
+
const collectRelevant = (filePath: string) => {
|
|
1627
|
+
for (const [name] of getLspServersForFile(config, filePath)) {
|
|
1628
|
+
relevantNames.add(name);
|
|
1629
|
+
}
|
|
1630
|
+
};
|
|
1631
|
+
collectRelevant(source);
|
|
1632
|
+
collectRelevant(dest);
|
|
1633
|
+
for (const pair of pairs) {
|
|
1634
|
+
collectRelevant(uriToFile(pair.oldUri));
|
|
1635
|
+
collectRelevant(uriToFile(pair.newUri));
|
|
1636
|
+
}
|
|
1637
|
+
const servers = allLspServers.filter(([name]) => relevantNames.has(name));
|
|
1638
|
+
const respondingServers = new Set<string>();
|
|
1639
|
+
const perServerEdits: Array<{ serverName: string; edit: WorkspaceEdit }> = [];
|
|
1640
|
+
const serverNotes: string[] = [];
|
|
1641
|
+
|
|
1642
|
+
for (const [serverName, serverConfig] of servers) {
|
|
1643
|
+
throwIfAborted(signal);
|
|
1644
|
+
try {
|
|
1645
|
+
const client = await getOrCreateClient(serverConfig, this.session.cwd);
|
|
1646
|
+
if (isProjectAwareLspServer(serverConfig)) {
|
|
1647
|
+
await waitForProjectLoaded(client, signal);
|
|
1648
|
+
}
|
|
1649
|
+
const result = (await sendRequest(
|
|
1650
|
+
client,
|
|
1651
|
+
"workspace/willRenameFiles",
|
|
1652
|
+
lspParams,
|
|
1653
|
+
signal,
|
|
1654
|
+
)) as WorkspaceEdit | null;
|
|
1655
|
+
respondingServers.add(serverName);
|
|
1656
|
+
if (result && (result.changes || result.documentChanges)) {
|
|
1657
|
+
perServerEdits.push({ serverName, edit: result });
|
|
1658
|
+
}
|
|
1659
|
+
} catch (err) {
|
|
1660
|
+
if (err instanceof ToolAbortError || signal?.aborted) {
|
|
1661
|
+
throw err;
|
|
1662
|
+
}
|
|
1663
|
+
if (!isMethodNotFoundError(err)) {
|
|
1664
|
+
const msg = err instanceof Error ? err.message : String(err);
|
|
1665
|
+
serverNotes.push(` ${serverName}: ${msg}`);
|
|
1666
|
+
}
|
|
1667
|
+
}
|
|
1668
|
+
}
|
|
1669
|
+
|
|
1670
|
+
const sourceLabel = formatPathRelativeToCwd(source, this.session.cwd);
|
|
1671
|
+
const destLabel = formatPathRelativeToCwd(dest, this.session.cwd);
|
|
1672
|
+
const fileCountLabel = sourceStat.isDirectory()
|
|
1673
|
+
? `${pairs.length} file${pairs.length !== 1 ? "s" : ""} under ${sourceLabel}`
|
|
1674
|
+
: sourceLabel;
|
|
1675
|
+
|
|
1676
|
+
const shouldApply = apply !== false;
|
|
1677
|
+
if (!shouldApply) {
|
|
1678
|
+
const lines: string[] = [];
|
|
1679
|
+
lines.push(`Rename preview: ${fileCountLabel} → ${destLabel}`);
|
|
1680
|
+
if (perServerEdits.length === 0) {
|
|
1681
|
+
lines.push(" No LSP edits would be applied");
|
|
1682
|
+
} else {
|
|
1683
|
+
for (const { serverName, edit } of perServerEdits) {
|
|
1684
|
+
const edits = formatWorkspaceEdit(edit, this.session.cwd);
|
|
1685
|
+
if (edits.length === 0) continue;
|
|
1686
|
+
lines.push(` ${serverName}:`);
|
|
1687
|
+
for (const e of edits) {
|
|
1688
|
+
lines.push(` ${e}`);
|
|
1689
|
+
}
|
|
1690
|
+
}
|
|
1691
|
+
}
|
|
1692
|
+
if (serverNotes.length > 0) {
|
|
1693
|
+
lines.push(" Server notes:");
|
|
1694
|
+
lines.push(...serverNotes);
|
|
1695
|
+
}
|
|
1696
|
+
return {
|
|
1697
|
+
content: [{ type: "text", text: lines.join("\n") }],
|
|
1698
|
+
details: {
|
|
1699
|
+
action,
|
|
1700
|
+
serverName: Array.from(respondingServers).join(", "),
|
|
1701
|
+
success: true,
|
|
1702
|
+
request: params,
|
|
1703
|
+
},
|
|
1704
|
+
};
|
|
1705
|
+
}
|
|
1706
|
+
|
|
1707
|
+
const summary: string[] = [];
|
|
1708
|
+
|
|
1709
|
+
// Coalesce per-URI edits across servers before applying. Each server
|
|
1710
|
+
// computed positions against the pre-edit file content, so applying
|
|
1711
|
+
// server A then re-reading for server B yields stale positions and
|
|
1712
|
+
// produces malformed imports. Group all text edits by URI, prefer the
|
|
1713
|
+
// project-primary (project-aware) server on overlap, and apply once
|
|
1714
|
+
// per URI from a single snapshot.
|
|
1715
|
+
const serverConfigByName = new Map(servers);
|
|
1716
|
+
interface AcceptedBucket {
|
|
1717
|
+
primaryServer: string;
|
|
1718
|
+
edits: TextEdit[];
|
|
1719
|
+
discarded: number;
|
|
1720
|
+
conflictServers: Set<string>;
|
|
1721
|
+
}
|
|
1722
|
+
const acceptedByUri = new Map<string, AcceptedBucket>();
|
|
1723
|
+
for (const { serverName, edit } of perServerEdits) {
|
|
1724
|
+
const cfg = serverConfigByName.get(serverName);
|
|
1725
|
+
const incomingPrimary = cfg ? isProjectAwareLspServer(cfg) : false;
|
|
1726
|
+
const flat = flattenWorkspaceTextEdits(edit);
|
|
1727
|
+
for (const [uri, edits] of flat) {
|
|
1728
|
+
const existing = acceptedByUri.get(uri);
|
|
1729
|
+
if (!existing) {
|
|
1730
|
+
acceptedByUri.set(uri, {
|
|
1731
|
+
primaryServer: serverName,
|
|
1732
|
+
edits: [...edits],
|
|
1733
|
+
discarded: 0,
|
|
1734
|
+
conflictServers: new Set(),
|
|
1735
|
+
});
|
|
1736
|
+
continue;
|
|
1737
|
+
}
|
|
1738
|
+
const existingCfg = serverConfigByName.get(existing.primaryServer);
|
|
1739
|
+
const existingIsPrimary = existingCfg ? isProjectAwareLspServer(existingCfg) : false;
|
|
1740
|
+
if (incomingPrimary && !existingIsPrimary) {
|
|
1741
|
+
// Promote incoming to primary; keep existing edits that don't overlap.
|
|
1742
|
+
const keptOld: TextEdit[] = [];
|
|
1743
|
+
let discardedOld = 0;
|
|
1744
|
+
for (const oe of existing.edits) {
|
|
1745
|
+
if (edits.some(ne => rangesOverlap(ne.range, oe.range))) discardedOld++;
|
|
1746
|
+
else keptOld.push(oe);
|
|
1747
|
+
}
|
|
1748
|
+
if (discardedOld > 0) existing.conflictServers.add(existing.primaryServer);
|
|
1749
|
+
existing.discarded += discardedOld;
|
|
1750
|
+
existing.primaryServer = serverName;
|
|
1751
|
+
existing.edits = [...edits, ...keptOld];
|
|
1752
|
+
} else {
|
|
1753
|
+
// Existing wins; discard incoming edits that overlap any accepted edit.
|
|
1754
|
+
let discardedNew = 0;
|
|
1755
|
+
for (const ne of edits) {
|
|
1756
|
+
if (existing.edits.some(ae => rangesOverlap(ae.range, ne.range))) {
|
|
1757
|
+
discardedNew++;
|
|
1758
|
+
} else {
|
|
1759
|
+
existing.edits.push(ne);
|
|
1760
|
+
}
|
|
1761
|
+
}
|
|
1762
|
+
if (discardedNew > 0) {
|
|
1763
|
+
existing.conflictServers.add(serverName);
|
|
1764
|
+
existing.discarded += discardedNew;
|
|
1765
|
+
}
|
|
1766
|
+
}
|
|
1767
|
+
}
|
|
1768
|
+
}
|
|
1769
|
+
|
|
1770
|
+
for (const [uri, bucket] of acceptedByUri) {
|
|
1771
|
+
const filePath = uriToFile(uri);
|
|
1772
|
+
await applyTextEdits(filePath, bucket.edits);
|
|
1773
|
+
const rel = formatPathRelativeToCwd(filePath, this.session.cwd);
|
|
1774
|
+
summary.push(` ${bucket.primaryServer}: applied ${bucket.edits.length} edit(s) to ${rel}`);
|
|
1775
|
+
if (bucket.discarded > 0) {
|
|
1776
|
+
const others = Array.from(bucket.conflictServers).join(", ");
|
|
1777
|
+
summary.push(
|
|
1778
|
+
` note: discarded ${bucket.discarded} overlapping edit(s) from ${others} (kept ${bucket.primaryServer})`,
|
|
1779
|
+
);
|
|
1780
|
+
logger.warn(
|
|
1781
|
+
`lsp rename_file: discarded ${bucket.discarded} overlapping edit(s) from ${others} on ${rel}; kept ${bucket.primaryServer}`,
|
|
1782
|
+
);
|
|
1783
|
+
}
|
|
1784
|
+
}
|
|
1785
|
+
|
|
1786
|
+
await fs.promises.mkdir(path.dirname(dest), { recursive: true });
|
|
1787
|
+
await fs.promises.rename(source, dest);
|
|
1788
|
+
summary.push(` Renamed ${sourceLabel} → ${destLabel}`);
|
|
1789
|
+
|
|
1790
|
+
for (const [serverName, serverConfig] of servers) {
|
|
1791
|
+
try {
|
|
1792
|
+
const client = await getOrCreateClient(serverConfig, this.session.cwd);
|
|
1793
|
+
for (const { oldUri } of pairs) {
|
|
1794
|
+
if (client.openFiles.has(oldUri)) {
|
|
1795
|
+
await sendNotification(client, "textDocument/didClose", {
|
|
1796
|
+
textDocument: { uri: oldUri },
|
|
1797
|
+
});
|
|
1798
|
+
client.openFiles.delete(oldUri);
|
|
1799
|
+
}
|
|
1800
|
+
}
|
|
1801
|
+
await sendNotification(client, "workspace/didRenameFiles", lspParams);
|
|
1802
|
+
} catch (err) {
|
|
1803
|
+
if (err instanceof ToolAbortError || signal?.aborted) {
|
|
1804
|
+
throw err;
|
|
1805
|
+
}
|
|
1806
|
+
const msg = err instanceof Error ? err.message : String(err);
|
|
1807
|
+
serverNotes.push(` ${serverName}: ${msg}`);
|
|
1808
|
+
}
|
|
1809
|
+
}
|
|
1810
|
+
|
|
1811
|
+
if (serverNotes.length > 0) {
|
|
1812
|
+
summary.push(" Server notes:");
|
|
1813
|
+
summary.push(...serverNotes);
|
|
1814
|
+
}
|
|
1815
|
+
|
|
1816
|
+
const header = `Renamed ${fileCountLabel} → ${destLabel}`;
|
|
1817
|
+
return {
|
|
1818
|
+
content: [{ type: "text", text: `${header}\n${summary.join("\n")}` }],
|
|
1819
|
+
details: {
|
|
1820
|
+
action,
|
|
1821
|
+
serverName: Array.from(respondingServers).join(", "),
|
|
1822
|
+
success: true,
|
|
1823
|
+
request: params,
|
|
1824
|
+
},
|
|
1825
|
+
};
|
|
1826
|
+
}
|
|
1827
|
+
|
|
1828
|
+
if (action === "capabilities") {
|
|
1829
|
+
let serverList: Array<[string, ServerConfig]>;
|
|
1830
|
+
if (file && file !== "*") {
|
|
1831
|
+
const resolved = resolveToCwd(file, this.session.cwd);
|
|
1832
|
+
serverList = getLspServersForFile(config, resolved);
|
|
1833
|
+
if (serverList.length === 0) {
|
|
1834
|
+
return {
|
|
1835
|
+
content: [{ type: "text", text: "No language server found for this file" }],
|
|
1836
|
+
details: { action, success: false, request: params },
|
|
1837
|
+
};
|
|
1838
|
+
}
|
|
1839
|
+
} else {
|
|
1840
|
+
serverList = getLspServers(config);
|
|
1841
|
+
}
|
|
1842
|
+
|
|
1843
|
+
if (serverList.length === 0) {
|
|
1844
|
+
return {
|
|
1845
|
+
content: [{ type: "text", text: "No language servers configured" }],
|
|
1846
|
+
details: { action, success: false, request: params },
|
|
1847
|
+
};
|
|
1848
|
+
}
|
|
1849
|
+
|
|
1850
|
+
const sections: string[] = [];
|
|
1851
|
+
const respondingServers = new Set<string>();
|
|
1852
|
+
for (const [serverName, serverConfig] of serverList) {
|
|
1853
|
+
throwIfAborted(signal);
|
|
1854
|
+
try {
|
|
1855
|
+
const client = await getOrCreateClient(serverConfig, this.session.cwd);
|
|
1856
|
+
respondingServers.add(serverName);
|
|
1857
|
+
const caps = client.serverCapabilities ?? {};
|
|
1858
|
+
sections.push(`${serverName}:`);
|
|
1859
|
+
sections.push(` capabilities: ${JSON.stringify(caps, null, 2).split("\n").join("\n ")}`);
|
|
1860
|
+
} catch (err) {
|
|
1861
|
+
if (err instanceof ToolAbortError || signal?.aborted) {
|
|
1862
|
+
throw err;
|
|
1863
|
+
}
|
|
1864
|
+
const msg = err instanceof Error ? err.message : String(err);
|
|
1865
|
+
sections.push(`${serverName}: failed to start (${msg})`);
|
|
1866
|
+
}
|
|
1867
|
+
}
|
|
1868
|
+
|
|
1869
|
+
return {
|
|
1870
|
+
content: [{ type: "text", text: sections.join("\n") }],
|
|
1871
|
+
details: {
|
|
1872
|
+
action,
|
|
1873
|
+
serverName: Array.from(respondingServers).join(", "),
|
|
1874
|
+
success: true,
|
|
1875
|
+
request: params,
|
|
1876
|
+
},
|
|
1877
|
+
};
|
|
1878
|
+
}
|
|
1879
|
+
|
|
1880
|
+
if (action === "request") {
|
|
1881
|
+
const method = query?.trim();
|
|
1882
|
+
if (!method) {
|
|
1883
|
+
return {
|
|
1884
|
+
content: [
|
|
1885
|
+
{
|
|
1886
|
+
type: "text",
|
|
1887
|
+
text: "Error: action=request requires `query` to specify the LSP method name (e.g., 'rust-analyzer/expandMacro')",
|
|
1888
|
+
},
|
|
1889
|
+
],
|
|
1890
|
+
details: { action, success: false, request: params },
|
|
1891
|
+
};
|
|
1892
|
+
}
|
|
1893
|
+
|
|
1894
|
+
let chosenServer: [string, ServerConfig] | null = null;
|
|
1895
|
+
let resolvedTarget: string | null = null;
|
|
1896
|
+
if (file && file !== "*") {
|
|
1897
|
+
resolvedTarget = resolveToCwd(file, this.session.cwd);
|
|
1898
|
+
chosenServer = getLspServerForFile(config, resolvedTarget);
|
|
1899
|
+
if (!chosenServer) {
|
|
1900
|
+
return {
|
|
1901
|
+
content: [{ type: "text", text: "No language server found for this file" }],
|
|
1902
|
+
details: { action, success: false, request: params },
|
|
1903
|
+
};
|
|
1904
|
+
}
|
|
1905
|
+
} else {
|
|
1906
|
+
const all = getLspServers(config);
|
|
1907
|
+
if (all.length === 0) {
|
|
1908
|
+
return {
|
|
1909
|
+
content: [{ type: "text", text: "No language servers configured" }],
|
|
1910
|
+
details: { action, success: false, request: params },
|
|
1911
|
+
};
|
|
1912
|
+
}
|
|
1913
|
+
chosenServer = all[0];
|
|
1914
|
+
}
|
|
1915
|
+
|
|
1916
|
+
const [chosenName, chosenConfig] = chosenServer;
|
|
1917
|
+
let requestParams: unknown;
|
|
1918
|
+
if (params.payload !== undefined) {
|
|
1919
|
+
try {
|
|
1920
|
+
requestParams = JSON.parse(params.payload);
|
|
1921
|
+
} catch (err) {
|
|
1922
|
+
const msg = err instanceof Error ? err.message : String(err);
|
|
1923
|
+
return {
|
|
1924
|
+
content: [{ type: "text", text: `Error: invalid JSON in payload: ${msg}` }],
|
|
1925
|
+
details: { action, serverName: chosenName, success: false, request: params },
|
|
1926
|
+
};
|
|
1927
|
+
}
|
|
1928
|
+
} else if (resolvedTarget) {
|
|
1929
|
+
const uri = fileToUri(resolvedTarget);
|
|
1930
|
+
if (line !== undefined) {
|
|
1931
|
+
const character = await resolveSymbolColumn(resolvedTarget, line, symbol);
|
|
1932
|
+
requestParams = { textDocument: { uri }, position: { line: line - 1, character } };
|
|
1933
|
+
} else {
|
|
1934
|
+
requestParams = { textDocument: { uri } };
|
|
1935
|
+
}
|
|
1936
|
+
} else {
|
|
1937
|
+
requestParams = {};
|
|
1938
|
+
}
|
|
1939
|
+
|
|
1940
|
+
try {
|
|
1941
|
+
const client = await getOrCreateClient(chosenConfig, this.session.cwd);
|
|
1942
|
+
if (resolvedTarget) {
|
|
1943
|
+
await ensureFileOpen(client, resolvedTarget, signal);
|
|
1944
|
+
}
|
|
1945
|
+
const result = await sendRequest(client, method, requestParams, signal);
|
|
1946
|
+
const formatted =
|
|
1947
|
+
result === null || result === undefined
|
|
1948
|
+
? "null"
|
|
1949
|
+
: typeof result === "string"
|
|
1950
|
+
? result
|
|
1951
|
+
: JSON.stringify(result, null, 2);
|
|
1952
|
+
return {
|
|
1953
|
+
content: [{ type: "text", text: `${chosenName} ← ${method}:\n${formatted}` }],
|
|
1954
|
+
details: { action, serverName: chosenName, success: true, request: params },
|
|
1955
|
+
};
|
|
1956
|
+
} catch (err) {
|
|
1957
|
+
if (err instanceof ToolAbortError || signal?.aborted) {
|
|
1958
|
+
throw new ToolAbortError();
|
|
1959
|
+
}
|
|
1960
|
+
const msg = err instanceof Error ? err.message : String(err);
|
|
1961
|
+
// Echo a (truncated) preview of the params we sent so the caller can
|
|
1962
|
+
// tell parse / shape errors (e.g. nested args dropped, missing field)
|
|
1963
|
+
// apart from genuine server errors without spinning up another debug call.
|
|
1964
|
+
const previewRaw = JSON.stringify(requestParams ?? null);
|
|
1965
|
+
const preview = previewRaw.length > 400 ? `${previewRaw.slice(0, 397)}...` : previewRaw;
|
|
1966
|
+
return {
|
|
1967
|
+
content: [
|
|
1968
|
+
{ type: "text", text: `LSP error from ${chosenName} on ${method}: ${msg}\n params: ${preview}` },
|
|
1969
|
+
],
|
|
1970
|
+
details: { action, serverName: chosenName, success: false, request: params },
|
|
1971
|
+
};
|
|
1972
|
+
}
|
|
1973
|
+
}
|
|
1974
|
+
|
|
1975
|
+
// `*` means workspace scope for symbols/reload; other actions need a concrete file.
|
|
1976
|
+
const isWorkspace = file === "*";
|
|
1977
|
+
const requiresFile = !file && action !== "reload";
|
|
1978
|
+
|
|
1979
|
+
if (requiresFile) {
|
|
1980
|
+
return {
|
|
1981
|
+
content: [
|
|
1982
|
+
{
|
|
1983
|
+
type: "text",
|
|
1984
|
+
text: "Error: file parameter required. Use `*` for workspace scope where supported.",
|
|
1985
|
+
},
|
|
1986
|
+
],
|
|
1987
|
+
details: { action, success: false },
|
|
1988
|
+
};
|
|
1989
|
+
}
|
|
1990
|
+
|
|
1991
|
+
const resolvedFile = file && !isWorkspace ? resolveToCwd(file, this.session.cwd) : null;
|
|
1992
|
+
if (action === "symbols" && (isWorkspace || !resolvedFile)) {
|
|
1993
|
+
const normalizedQuery = query?.trim();
|
|
1994
|
+
if (!normalizedQuery) {
|
|
1995
|
+
return {
|
|
1996
|
+
content: [{ type: "text", text: "Error: query parameter required for workspace symbol search" }],
|
|
1997
|
+
details: { action, success: false, request: params },
|
|
1998
|
+
};
|
|
1999
|
+
}
|
|
2000
|
+
const servers = getLspServers(config);
|
|
2001
|
+
if (servers.length === 0) {
|
|
2002
|
+
return {
|
|
2003
|
+
content: [{ type: "text", text: "No language server found for this action" }],
|
|
2004
|
+
details: { action, success: false, request: params },
|
|
2005
|
+
};
|
|
2006
|
+
}
|
|
2007
|
+
const aggregatedSymbols: SymbolInformation[] = [];
|
|
2008
|
+
const respondingServers = new Set<string>();
|
|
2009
|
+
for (const [workspaceServerName, workspaceServerConfig] of servers) {
|
|
2010
|
+
throwIfAborted(signal);
|
|
2011
|
+
try {
|
|
2012
|
+
const workspaceClient = await getOrCreateClient(workspaceServerConfig, this.session.cwd);
|
|
2013
|
+
const workspaceResult = (await sendRequest(
|
|
2014
|
+
workspaceClient,
|
|
2015
|
+
"workspace/symbol",
|
|
2016
|
+
{ query: normalizedQuery },
|
|
2017
|
+
signal,
|
|
2018
|
+
)) as SymbolInformation[] | null;
|
|
2019
|
+
if (!workspaceResult || workspaceResult.length === 0) {
|
|
2020
|
+
continue;
|
|
2021
|
+
}
|
|
2022
|
+
respondingServers.add(workspaceServerName);
|
|
2023
|
+
aggregatedSymbols.push(...filterWorkspaceSymbols(workspaceResult, normalizedQuery));
|
|
2024
|
+
} catch (err) {
|
|
2025
|
+
if (err instanceof ToolAbortError || signal?.aborted) {
|
|
2026
|
+
throw err;
|
|
2027
|
+
}
|
|
2028
|
+
}
|
|
2029
|
+
}
|
|
2030
|
+
const dedupedSymbols = dedupeWorkspaceSymbols(aggregatedSymbols);
|
|
2031
|
+
if (dedupedSymbols.length === 0) {
|
|
2032
|
+
return {
|
|
2033
|
+
content: [{ type: "text", text: `No symbols matching "${normalizedQuery}"` }],
|
|
2034
|
+
details: {
|
|
2035
|
+
action,
|
|
2036
|
+
serverName: Array.from(respondingServers).join(", "),
|
|
2037
|
+
success: true,
|
|
2038
|
+
request: params,
|
|
2039
|
+
},
|
|
2040
|
+
};
|
|
2041
|
+
}
|
|
2042
|
+
const limitedSymbols = dedupedSymbols.slice(0, WORKSPACE_SYMBOL_LIMIT);
|
|
2043
|
+
const lines = limitedSymbols.map(s => formatSymbolInformation(s, this.session.cwd));
|
|
2044
|
+
const truncationLine =
|
|
2045
|
+
dedupedSymbols.length > WORKSPACE_SYMBOL_LIMIT
|
|
2046
|
+
? `\n... ${dedupedSymbols.length - WORKSPACE_SYMBOL_LIMIT} additional symbol(s) omitted`
|
|
2047
|
+
: "";
|
|
2048
|
+
return {
|
|
2049
|
+
content: [
|
|
2050
|
+
{
|
|
2051
|
+
type: "text",
|
|
2052
|
+
text: `Found ${dedupedSymbols.length} symbol(s) matching "${normalizedQuery}":\n${lines.map(l => ` ${l}`).join("\n")}${truncationLine}`,
|
|
2053
|
+
},
|
|
2054
|
+
],
|
|
2055
|
+
details: {
|
|
2056
|
+
action,
|
|
2057
|
+
serverName: Array.from(respondingServers).join(", "),
|
|
2058
|
+
success: true,
|
|
2059
|
+
request: params,
|
|
2060
|
+
},
|
|
2061
|
+
};
|
|
2062
|
+
}
|
|
2063
|
+
|
|
2064
|
+
if (action === "reload" && (isWorkspace || !resolvedFile)) {
|
|
2065
|
+
const servers = getLspServers(config);
|
|
2066
|
+
if (servers.length === 0) {
|
|
2067
|
+
return {
|
|
2068
|
+
content: [{ type: "text", text: "No language server found for this action" }],
|
|
2069
|
+
details: { action, success: false, request: params },
|
|
2070
|
+
};
|
|
2071
|
+
}
|
|
2072
|
+
const outputs: string[] = [];
|
|
2073
|
+
for (const [workspaceServerName, workspaceServerConfig] of servers) {
|
|
2074
|
+
throwIfAborted(signal);
|
|
2075
|
+
try {
|
|
2076
|
+
const workspaceClient = await getOrCreateClient(workspaceServerConfig, this.session.cwd);
|
|
2077
|
+
outputs.push(await reloadServer(workspaceClient, workspaceServerName, signal));
|
|
2078
|
+
} catch (err) {
|
|
2079
|
+
if (err instanceof ToolAbortError || signal?.aborted) {
|
|
2080
|
+
throw err;
|
|
2081
|
+
}
|
|
2082
|
+
const errorMessage = err instanceof Error ? err.message : String(err);
|
|
2083
|
+
outputs.push(`Failed to reload ${workspaceServerName}: ${errorMessage}`);
|
|
2084
|
+
}
|
|
2085
|
+
}
|
|
2086
|
+
return {
|
|
2087
|
+
content: [{ type: "text", text: outputs.join("\n") }],
|
|
2088
|
+
details: { action, serverName: servers.map(([name]) => name).join(", "), success: true, request: params },
|
|
2089
|
+
};
|
|
2090
|
+
}
|
|
2091
|
+
|
|
2092
|
+
const serverInfo = resolvedFile ? getLspServerForFile(config, resolvedFile) : null;
|
|
2093
|
+
if (!serverInfo) {
|
|
2094
|
+
return {
|
|
2095
|
+
content: [{ type: "text", text: "No language server found for this action" }],
|
|
2096
|
+
details: { action, success: false },
|
|
2097
|
+
};
|
|
2098
|
+
}
|
|
2099
|
+
|
|
2100
|
+
const [serverName, serverConfig] = serverInfo;
|
|
2101
|
+
|
|
2102
|
+
try {
|
|
2103
|
+
const client = await getOrCreateClient(serverConfig, this.session.cwd);
|
|
2104
|
+
const targetFile = resolvedFile;
|
|
2105
|
+
const isRustAnalyzerServer =
|
|
2106
|
+
serverName === "rust-analyzer" ||
|
|
2107
|
+
path.basename(serverConfig.command) === "rust-analyzer" ||
|
|
2108
|
+
(serverConfig.resolvedCommand ? path.basename(serverConfig.resolvedCommand) === "rust-analyzer" : false);
|
|
2109
|
+
const needsProjectIndex =
|
|
2110
|
+
targetFile !== null && PROJECT_INDEXED_ACTIONS.has(action) && isProjectAwareLspServer(serverConfig);
|
|
2111
|
+
const rustWorkspaceWait =
|
|
2112
|
+
needsProjectIndex && isRustAnalyzerServer && targetFile !== null && hasRustWorkspaceAncestor(targetFile);
|
|
2113
|
+
|
|
2114
|
+
if (targetFile) {
|
|
2115
|
+
await ensureFileOpen(client, targetFile, signal);
|
|
2116
|
+
}
|
|
2117
|
+
if (rustWorkspaceWait) {
|
|
2118
|
+
await waitForProjectLoaded(client, signal);
|
|
2119
|
+
}
|
|
2120
|
+
|
|
2121
|
+
// For project-aware servers, references/rename/definition without a `symbol`
|
|
2122
|
+
// silently falls back to the first non-whitespace column on the line, which
|
|
2123
|
+
// frequently points at the wrong identifier (decorator, keyword, parameter)
|
|
2124
|
+
// and the server returns plausible-looking but unrelated results. Require
|
|
2125
|
+
// `symbol` explicitly so callers cannot accidentally trigger that fallback.
|
|
2126
|
+
if (
|
|
2127
|
+
targetFile &&
|
|
2128
|
+
line !== undefined &&
|
|
2129
|
+
!symbol &&
|
|
2130
|
+
(action === "references" || action === "rename" || action === "definition") &&
|
|
2131
|
+
isProjectAwareLspServer(serverConfig)
|
|
2132
|
+
) {
|
|
2133
|
+
throw new ToolError(
|
|
2134
|
+
`symbol is required for project-aware ${action}; pass symbol=<name>, optionally symbol#N for repeated occurrences`,
|
|
2135
|
+
);
|
|
2136
|
+
}
|
|
2137
|
+
const uri = targetFile ? fileToUri(targetFile) : "";
|
|
2138
|
+
const resolvedLine = line ?? 1;
|
|
2139
|
+
const resolvedCharacter = targetFile ? await resolveSymbolColumn(targetFile, resolvedLine, symbol) : 0;
|
|
2140
|
+
const position = { line: resolvedLine - 1, character: resolvedCharacter };
|
|
2141
|
+
|
|
2142
|
+
let output: string;
|
|
2143
|
+
|
|
2144
|
+
if (needsProjectIndex && !isRustAnalyzerServer) {
|
|
2145
|
+
await waitForProjectLoaded(client, signal);
|
|
2146
|
+
}
|
|
2147
|
+
|
|
2148
|
+
switch (action) {
|
|
2149
|
+
// =====================================================================
|
|
2150
|
+
// Standard LSP Operations
|
|
2151
|
+
// =====================================================================
|
|
2152
|
+
|
|
2153
|
+
case "definition": {
|
|
2154
|
+
const result = (await sendRequest(
|
|
2155
|
+
client,
|
|
2156
|
+
"textDocument/definition",
|
|
2157
|
+
{
|
|
2158
|
+
textDocument: { uri },
|
|
2159
|
+
position,
|
|
2160
|
+
},
|
|
2161
|
+
signal,
|
|
2162
|
+
)) as Location | Location[] | LocationLink | LocationLink[] | null;
|
|
2163
|
+
|
|
2164
|
+
const locations = normalizeLocationResult(result);
|
|
2165
|
+
|
|
2166
|
+
if (locations.length === 0) {
|
|
2167
|
+
output = "No definition found";
|
|
2168
|
+
} else {
|
|
2169
|
+
const lines = await Promise.all(
|
|
2170
|
+
locations.map(location => formatLocationWithContext(location, this.session.cwd)),
|
|
2171
|
+
);
|
|
2172
|
+
output = `Found ${locations.length} definition(s):\n${lines.join("\n")}`;
|
|
2173
|
+
}
|
|
2174
|
+
break;
|
|
2175
|
+
}
|
|
2176
|
+
|
|
2177
|
+
case "type_definition": {
|
|
2178
|
+
const result = (await sendRequest(
|
|
2179
|
+
client,
|
|
2180
|
+
"textDocument/typeDefinition",
|
|
2181
|
+
{
|
|
2182
|
+
textDocument: { uri },
|
|
2183
|
+
position,
|
|
2184
|
+
},
|
|
2185
|
+
signal,
|
|
2186
|
+
)) as Location | Location[] | LocationLink | LocationLink[] | null;
|
|
2187
|
+
|
|
2188
|
+
const locations = normalizeLocationResult(result);
|
|
2189
|
+
|
|
2190
|
+
if (locations.length === 0) {
|
|
2191
|
+
output = "No type definition found";
|
|
2192
|
+
} else {
|
|
2193
|
+
const lines = await Promise.all(
|
|
2194
|
+
locations.map(location => formatLocationWithContext(location, this.session.cwd)),
|
|
2195
|
+
);
|
|
2196
|
+
output = `Found ${locations.length} type definition(s):\n${lines.join("\n")}`;
|
|
2197
|
+
}
|
|
2198
|
+
break;
|
|
2199
|
+
}
|
|
2200
|
+
|
|
2201
|
+
case "implementation": {
|
|
2202
|
+
const result = (await sendRequest(
|
|
2203
|
+
client,
|
|
2204
|
+
"textDocument/implementation",
|
|
2205
|
+
{
|
|
2206
|
+
textDocument: { uri },
|
|
2207
|
+
position,
|
|
2208
|
+
},
|
|
2209
|
+
signal,
|
|
2210
|
+
)) as Location | Location[] | LocationLink | LocationLink[] | null;
|
|
2211
|
+
|
|
2212
|
+
const locations = normalizeLocationResult(result);
|
|
2213
|
+
|
|
2214
|
+
if (locations.length === 0) {
|
|
2215
|
+
output = "No implementation found";
|
|
2216
|
+
} else {
|
|
2217
|
+
const lines = await Promise.all(
|
|
2218
|
+
locations.map(location => formatLocationWithContext(location, this.session.cwd)),
|
|
2219
|
+
);
|
|
2220
|
+
output = `Found ${locations.length} implementation(s):\n${lines.join("\n")}`;
|
|
2221
|
+
}
|
|
2222
|
+
break;
|
|
2223
|
+
}
|
|
2224
|
+
case "references": {
|
|
2225
|
+
let result: Location[] | null = null;
|
|
2226
|
+
for (let attempt = 0; attempt <= REFERENCES_RETRY_COUNT; attempt++) {
|
|
2227
|
+
result = (await sendRequest(
|
|
2228
|
+
client,
|
|
2229
|
+
"textDocument/references",
|
|
2230
|
+
{
|
|
2231
|
+
textDocument: { uri },
|
|
2232
|
+
position,
|
|
2233
|
+
context: { includeDeclaration: true },
|
|
2234
|
+
},
|
|
2235
|
+
signal,
|
|
2236
|
+
)) as Location[] | null;
|
|
2237
|
+
|
|
2238
|
+
const locations = result ?? [];
|
|
2239
|
+
if (!isProjectAwareLspServer(serverConfig) || attempt === REFERENCES_RETRY_COUNT) {
|
|
2240
|
+
break;
|
|
2241
|
+
}
|
|
2242
|
+
if (locations.length > 0 && !isOnlyQueriedDeclaration(locations, uri, position)) {
|
|
2243
|
+
break;
|
|
2244
|
+
}
|
|
2245
|
+
|
|
2246
|
+
await waitForProjectLoaded(client, signal);
|
|
2247
|
+
throwIfAborted(signal);
|
|
2248
|
+
await untilAborted(signal, () => Bun.sleep(REFERENCES_RETRY_DELAY_MS));
|
|
2249
|
+
}
|
|
2250
|
+
|
|
2251
|
+
if (!result || result.length === 0) {
|
|
2252
|
+
output = "No references found";
|
|
2253
|
+
} else {
|
|
2254
|
+
const contextualReferences = result.slice(0, REFERENCE_CONTEXT_LIMIT);
|
|
2255
|
+
const plainReferences = result.slice(REFERENCE_CONTEXT_LIMIT);
|
|
2256
|
+
const contextualLines = await Promise.all(
|
|
2257
|
+
contextualReferences.map(location => formatLocationWithContext(location, this.session.cwd)),
|
|
2258
|
+
);
|
|
2259
|
+
const plainLines = plainReferences.map(location => ` ${formatLocation(location, this.session.cwd)}`);
|
|
2260
|
+
const lines = plainLines.length
|
|
2261
|
+
? [
|
|
2262
|
+
...contextualLines,
|
|
2263
|
+
` ... ${plainLines.length} additional reference(s) shown without context`,
|
|
2264
|
+
...plainLines,
|
|
2265
|
+
]
|
|
2266
|
+
: contextualLines;
|
|
2267
|
+
output = `Found ${result.length} reference(s):\n${lines.join("\n")}`;
|
|
2268
|
+
}
|
|
2269
|
+
break;
|
|
2270
|
+
}
|
|
2271
|
+
|
|
2272
|
+
case "hover": {
|
|
2273
|
+
const result = (await sendRequest(
|
|
2274
|
+
client,
|
|
2275
|
+
"textDocument/hover",
|
|
2276
|
+
{
|
|
2277
|
+
textDocument: { uri },
|
|
2278
|
+
position,
|
|
2279
|
+
},
|
|
2280
|
+
signal,
|
|
2281
|
+
)) as Hover | null;
|
|
2282
|
+
|
|
2283
|
+
if (!result?.contents) {
|
|
2284
|
+
output = "No hover information";
|
|
2285
|
+
} else {
|
|
2286
|
+
output = extractHoverText(result.contents);
|
|
2287
|
+
}
|
|
2288
|
+
break;
|
|
2289
|
+
}
|
|
2290
|
+
|
|
2291
|
+
case "code_actions": {
|
|
2292
|
+
const diagnostics = client.diagnostics.get(uri)?.diagnostics ?? [];
|
|
2293
|
+
const context: CodeActionContext = {
|
|
2294
|
+
diagnostics,
|
|
2295
|
+
only: !apply && query ? [query] : undefined,
|
|
2296
|
+
triggerKind: 1,
|
|
2297
|
+
};
|
|
2298
|
+
|
|
2299
|
+
const result = (await sendRequest(
|
|
2300
|
+
client,
|
|
2301
|
+
"textDocument/codeAction",
|
|
2302
|
+
{
|
|
2303
|
+
textDocument: { uri },
|
|
2304
|
+
range: { start: position, end: position },
|
|
2305
|
+
context,
|
|
2306
|
+
},
|
|
2307
|
+
signal,
|
|
2308
|
+
)) as (CodeAction | Command)[] | null;
|
|
2309
|
+
|
|
2310
|
+
if (!result || result.length === 0) {
|
|
2311
|
+
output = "No code actions available";
|
|
2312
|
+
break;
|
|
2313
|
+
}
|
|
2314
|
+
|
|
2315
|
+
if (apply === true && query) {
|
|
2316
|
+
const normalizedQuery = query.trim();
|
|
2317
|
+
if (normalizedQuery.length === 0) {
|
|
2318
|
+
output = "Error: query parameter required when apply=true for code_actions";
|
|
2319
|
+
break;
|
|
2320
|
+
}
|
|
2321
|
+
const parsedIndex = /^\d+$/.test(normalizedQuery) ? Number.parseInt(normalizedQuery, 10) : null;
|
|
2322
|
+
const selectedAction =
|
|
2323
|
+
parsedIndex !== null
|
|
2324
|
+
? result[parsedIndex]
|
|
2325
|
+
: result.find(actionItem =>
|
|
2326
|
+
actionItem.title.toLowerCase().includes(normalizedQuery.toLowerCase()),
|
|
2327
|
+
);
|
|
2328
|
+
|
|
2329
|
+
if (!selectedAction) {
|
|
2330
|
+
const actionLines = result.map((actionItem, index) => ` ${formatCodeAction(actionItem, index)}`);
|
|
2331
|
+
output = `No code action matches "${normalizedQuery}". Available actions:\n${actionLines.join("\n")}`;
|
|
2332
|
+
break;
|
|
2333
|
+
}
|
|
2334
|
+
|
|
2335
|
+
const appliedAction = await applyCodeAction(selectedAction, {
|
|
2336
|
+
resolveCodeAction: async actionItem =>
|
|
2337
|
+
(await sendRequest(client, "codeAction/resolve", actionItem, signal)) as CodeAction,
|
|
2338
|
+
applyWorkspaceEdit: async edit => applyWorkspaceEdit(edit, this.session.cwd),
|
|
2339
|
+
executeCommand: async commandItem => {
|
|
2340
|
+
await sendRequest(
|
|
2341
|
+
client,
|
|
2342
|
+
"workspace/executeCommand",
|
|
2343
|
+
{
|
|
2344
|
+
command: commandItem.command,
|
|
2345
|
+
arguments: commandItem.arguments ?? [],
|
|
2346
|
+
},
|
|
2347
|
+
signal,
|
|
2348
|
+
);
|
|
2349
|
+
},
|
|
2350
|
+
});
|
|
2351
|
+
|
|
2352
|
+
if (!appliedAction) {
|
|
2353
|
+
output = `Action "${selectedAction.title}" has no workspace edit or command to apply`;
|
|
2354
|
+
break;
|
|
2355
|
+
}
|
|
2356
|
+
|
|
2357
|
+
const summaryLines: string[] = [];
|
|
2358
|
+
if (appliedAction.edits.length > 0) {
|
|
2359
|
+
summaryLines.push(" Workspace edit:");
|
|
2360
|
+
summaryLines.push(...appliedAction.edits.map(item => ` ${item}`));
|
|
2361
|
+
}
|
|
2362
|
+
if (appliedAction.executedCommands.length > 0) {
|
|
2363
|
+
summaryLines.push(" Executed command(s):");
|
|
2364
|
+
summaryLines.push(...appliedAction.executedCommands.map(commandName => ` ${commandName}`));
|
|
2365
|
+
}
|
|
2366
|
+
|
|
2367
|
+
output = `Applied "${appliedAction.title}":\n${summaryLines.join("\n")}`;
|
|
2368
|
+
break;
|
|
2369
|
+
}
|
|
2370
|
+
|
|
2371
|
+
const actionLines = result.map((actionItem, index) => ` ${formatCodeAction(actionItem, index)}`);
|
|
2372
|
+
output = `${result.length} code action(s):\n${actionLines.join("\n")}`;
|
|
2373
|
+
break;
|
|
2374
|
+
}
|
|
2375
|
+
case "symbols": {
|
|
2376
|
+
if (!targetFile) {
|
|
2377
|
+
output = "Error: file parameter required for document symbols";
|
|
2378
|
+
break;
|
|
2379
|
+
}
|
|
2380
|
+
// File-based document symbols
|
|
2381
|
+
const result = (await sendRequest(
|
|
2382
|
+
client,
|
|
2383
|
+
"textDocument/documentSymbol",
|
|
2384
|
+
{
|
|
2385
|
+
textDocument: { uri },
|
|
2386
|
+
},
|
|
2387
|
+
signal,
|
|
2388
|
+
)) as (DocumentSymbol | SymbolInformation)[] | null;
|
|
2389
|
+
|
|
2390
|
+
if (!result || result.length === 0) {
|
|
2391
|
+
output = "No symbols found";
|
|
2392
|
+
} else {
|
|
2393
|
+
const relPath = formatPathRelativeToCwd(targetFile, this.session.cwd);
|
|
2394
|
+
if ("selectionRange" in result[0]) {
|
|
2395
|
+
const lines = (result as DocumentSymbol[]).flatMap(s => formatDocumentSymbol(s));
|
|
2396
|
+
output = `Symbols in ${relPath}:\n${lines.join("\n")}`;
|
|
2397
|
+
} else {
|
|
2398
|
+
const lines = (result as SymbolInformation[]).map(s => {
|
|
2399
|
+
const line = s.location.range.start.line + 1;
|
|
2400
|
+
const icon = symbolKindToIcon(s.kind);
|
|
2401
|
+
return `${icon} ${s.name} @ line ${line}`;
|
|
2402
|
+
});
|
|
2403
|
+
output = `Symbols in ${relPath}:\n${lines.join("\n")}`;
|
|
2404
|
+
}
|
|
2405
|
+
}
|
|
2406
|
+
break;
|
|
2407
|
+
}
|
|
2408
|
+
|
|
2409
|
+
case "rename": {
|
|
2410
|
+
if (!new_name) {
|
|
2411
|
+
return {
|
|
2412
|
+
content: [{ type: "text", text: "Error: new_name parameter required for rename" }],
|
|
2413
|
+
details: { action, serverName, success: false },
|
|
2414
|
+
};
|
|
2415
|
+
}
|
|
2416
|
+
|
|
2417
|
+
const result = (await sendRequest(
|
|
2418
|
+
client,
|
|
2419
|
+
"textDocument/rename",
|
|
2420
|
+
{
|
|
2421
|
+
textDocument: { uri },
|
|
2422
|
+
position,
|
|
2423
|
+
newName: new_name,
|
|
2424
|
+
},
|
|
2425
|
+
signal,
|
|
2426
|
+
)) as WorkspaceEdit | null;
|
|
2427
|
+
|
|
2428
|
+
if (!result) {
|
|
2429
|
+
output = "Rename returned no edits";
|
|
2430
|
+
} else {
|
|
2431
|
+
const shouldApply = apply !== false;
|
|
2432
|
+
if (shouldApply) {
|
|
2433
|
+
const applied = await applyWorkspaceEdit(result, this.session.cwd);
|
|
2434
|
+
output = `Applied rename:\n${applied.map(a => ` ${a}`).join("\n")}`;
|
|
2435
|
+
} else {
|
|
2436
|
+
const preview = formatWorkspaceEdit(result, this.session.cwd);
|
|
2437
|
+
output = `Rename preview:\n${preview.map(p => ` ${p}`).join("\n")}`;
|
|
2438
|
+
}
|
|
2439
|
+
}
|
|
2440
|
+
break;
|
|
2441
|
+
}
|
|
2442
|
+
|
|
2443
|
+
case "reload": {
|
|
2444
|
+
output = await reloadServer(client, serverName, signal);
|
|
2445
|
+
break;
|
|
2446
|
+
}
|
|
2447
|
+
|
|
2448
|
+
default:
|
|
2449
|
+
output = `Unknown action: ${action}`;
|
|
2450
|
+
}
|
|
2451
|
+
|
|
2452
|
+
return {
|
|
2453
|
+
content: [{ type: "text", text: output }],
|
|
2454
|
+
details: { serverName, action, success: true, request: params },
|
|
2455
|
+
};
|
|
2456
|
+
} catch (err) {
|
|
2457
|
+
if (err instanceof ToolError) throw err;
|
|
2458
|
+
if (err instanceof ToolAbortError || signal?.aborted) {
|
|
2459
|
+
// Distinguish a wall-clock timeout from a caller cancel:
|
|
2460
|
+
// callerSignal aborting → real cancel (re-throw ToolAbortError);
|
|
2461
|
+
// timeoutSignal aborting without callerSignal → emit a ToolError naming the
|
|
2462
|
+
// elapsed budget and server, instead of opaque "Operation aborted".
|
|
2463
|
+
if (timeoutSignal.aborted && !callerSignal?.aborted) {
|
|
2464
|
+
throw new ToolError(
|
|
2465
|
+
`LSP ${action} timed out after ${timeoutSec}s on ${serverName}. The server may still be indexing; try again or pass timeout=<larger>.`,
|
|
2466
|
+
);
|
|
2467
|
+
}
|
|
2468
|
+
throw new ToolAbortError();
|
|
2469
|
+
}
|
|
2470
|
+
const errorMessage = err instanceof Error ? err.message : String(err);
|
|
2471
|
+
return {
|
|
2472
|
+
content: [{ type: "text", text: `LSP error: ${errorMessage}` }],
|
|
2473
|
+
details: { serverName, action, success: false, request: params },
|
|
2474
|
+
};
|
|
2475
|
+
}
|
|
2476
|
+
}
|
|
2477
|
+
}
|