summaryrefslogtreecommitdiffhomepage
path: root/packages/tool-edit-file/src/extension.ts
blob: 9dbebdad64b9f3b22fac3573df04d63a51e21403 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import { execBackendHandle } from "@dispatch/exec-backend";
import type { Extension } from "@dispatch/kernel";
import { type LspService, lspServiceHandle } from "@dispatch/lsp";
import { createEditFileTool, type DiagnosticsHook } from "./edit-file.js";

export const extension: Extension = {
	manifest: {
		id: "tool-edit-file",
		name: "Edit File Tool",
		version: "0.0.0",
		apiVersion: "^0.1.0",
		trust: "bundled",
		activation: "eager",
		capabilities: { fs: true },
		contributes: { tools: ["edit_file"] },
		// Host activates exec-backend first → host.getService at activation is safe.
		// LSP stays lazy (looked up at edit time, not activation): the LSP extension
		// activates AFTER us in the CORE_EXTENSIONS array, so resolving it here would
		// throw; the diagnostics hook below defers the lookup to execute().
		dependsOn: ["exec-backend"],
	},
	activate(host) {
		const resolveBackend = host.getService(execBackendHandle);

		// Lazy LSP lookup: the LSP extension activates AFTER us in the
		// CORE_EXTENSIONS array, so host.getService would throw at activation
		// time. Instead, defer the lookup to edit time — by then all extensions
		// have activated. If LSP isn't loaded, the try/catch returns a no-op
		// (graceful degradation: edits proceed without diagnostics).
		const diagnostics: DiagnosticsHook = async (opts) => {
			let lspService: LspService | undefined;
			try {
				lspService = host.getService(lspServiceHandle);
			} catch {
				return { formatted: "", slow: false, timedOut: false };
			}
			if (!lspService) {
				return { formatted: "", slow: false, timedOut: false };
			}
			return lspService.getDiagnostics({
				filePath: opts.filePath,
				text: opts.text,
				cwd: opts.cwd,
				// 10s matches the LSP service's per-server cap (see packages/lsp).
				// The service clamps this anyway; stated explicitly so the call
				// site is honest about the effective live-diagnostics budget.
				timeoutMs: 10_000,
				minSeverity: 2, // errors + warnings only
			});
		};

		host.defineTool(createEditFileTool({ resolveBackend, workdir: process.cwd(), diagnostics }));
	},
};