@weave-os/router 0.2.14
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/LICENSE +90 -0
- package/README.md +172 -0
- package/bin.js +68 -0
- package/cc-statusline.sh +951 -0
- package/codex-skills/disable-routing/SKILL.md +22 -0
- package/codex-skills/fm/SKILL.md +15 -0
- package/codex-skills/fm/scripts/emit.sh +8 -0
- package/codex-skills/force-model/SKILL.md +15 -0
- package/codex-skills/force-model/scripts/emit.sh +9 -0
- package/codex-skills/rf/SKILL.md +15 -0
- package/codex-skills/rf/scripts/emit.sh +7 -0
- package/codex-skills/router-feedback/SKILL.md +15 -0
- package/codex-skills/router-feedback/scripts/emit.sh +9 -0
- package/codex-skills/router-models/SKILL.md +51 -0
- package/codex-skills/router-off/SKILL.md +22 -0
- package/codex-skills/router-on/SKILL.md +22 -0
- package/codex-skills/router-status/SKILL.md +19 -0
- package/codex-skills/ufm/SKILL.md +14 -0
- package/codex-skills/ufm/scripts/emit.sh +3 -0
- package/codex-skills/unforce-model/SKILL.md +14 -0
- package/codex-skills/unforce-model/scripts/emit.sh +4 -0
- package/codex-status.sh +378 -0
- package/commands/beta.md +5 -0
- package/commands/fm.md +6 -0
- package/commands/force-model.md +6 -0
- package/commands/models.md +46 -0
- package/commands/rf.md +6 -0
- package/commands/router-feedback.md +6 -0
- package/commands/router-models.md +46 -0
- package/commands/router-off.md +10 -0
- package/commands/router-on.md +10 -0
- package/commands/router-session.md +15 -0
- package/commands/router-status.md +10 -0
- package/commands/ufm.md +5 -0
- package/commands/unforce-model.md +5 -0
- package/directives.tsv +13 -0
- package/install.sh +5092 -0
- package/opencode-weave/README.md +78 -0
- package/opencode-weave/package.json +18 -0
- package/opencode-weave/src/index.ts +655 -0
- package/package.json +79 -0
- package/pi-router/README.md +123 -0
- package/pi-router/package.json +25 -0
- package/pi-router/skills/install-lsps/SKILL.md +75 -0
- package/pi-router/skills/lsp-guide/SKILL.md +63 -0
- package/pi-router/src/beta.ts +21 -0
- package/pi-router/src/compaction.ts +188 -0
- package/pi-router/src/config.ts +293 -0
- package/pi-router/src/context-window.ts +12 -0
- package/pi-router/src/dispatch.ts +377 -0
- package/pi-router/src/force-model.ts +126 -0
- package/pi-router/src/index.ts +62 -0
- package/pi-router/src/lsp-broker.ts +255 -0
- package/pi-router/src/lsp-client.ts +435 -0
- package/pi-router/src/lsp-format.ts +230 -0
- package/pi-router/src/lsp-install.ts +215 -0
- package/pi-router/src/lsp-protocol.ts +128 -0
- package/pi-router/src/lsp-servers.ts +361 -0
- package/pi-router/src/lsp.ts +529 -0
- package/pi-router/src/metadata.ts +32 -0
- package/pi-router/src/pricing.generated.ts +91 -0
- package/pi-router/src/provider.ts +64 -0
- package/pi-router/src/routed-model.ts +182 -0
- package/pi-router/src/safety.ts +66 -0
- package/pi-router/src/savings.ts +191 -0
- package/pi-router/src/ui.ts +91 -0
- package/pi-router/src/wooly.ts +408 -0
- package/registry.sh +102 -0
- package/uninstall.sh +788 -0
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Opt-in language-server provisioning.
|
|
3
|
+
*
|
|
4
|
+
* Nothing here runs uninvited: the assistant is told (via a system-prompt
|
|
5
|
+
* addendum) which detected languages lack a server, offers ONCE in
|
|
6
|
+
* conversation ("I can enable Go LSP support if you'd like — just say the
|
|
7
|
+
* word!"), and only a user "yes" relayed through the lsp_enable tool triggers
|
|
8
|
+
* an install. A "no, stop asking" is persisted per language and respected
|
|
9
|
+
* across sessions.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import { spawn as nodeSpawn } from "node:child_process";
|
|
13
|
+
import * as fs from "node:fs";
|
|
14
|
+
import type { SpawnFn } from "./lsp-client.js";
|
|
15
|
+
import {
|
|
16
|
+
defaultWhich,
|
|
17
|
+
detectMarkers,
|
|
18
|
+
installCommandText,
|
|
19
|
+
LSP_SERVERS,
|
|
20
|
+
resolveBinary,
|
|
21
|
+
type LanguageServerSpec,
|
|
22
|
+
type WhichFn,
|
|
23
|
+
} from "./lsp-servers.js";
|
|
24
|
+
|
|
25
|
+
const INSTALL_TIMEOUT_MS = 300_000;
|
|
26
|
+
const INSTALL_KILL_GRACE_MS = 5000;
|
|
27
|
+
const OUTPUT_TAIL_BYTES = 2048;
|
|
28
|
+
const MAX_SCAN_ENTRIES = 50;
|
|
29
|
+
const SKIPPED_SCAN_DIRS = new Set(["node_modules", "vendor", "dist", "build", "target"]);
|
|
30
|
+
|
|
31
|
+
export interface DetectDeps {
|
|
32
|
+
exists?(target: string): boolean;
|
|
33
|
+
readDir?(dir: string): string[];
|
|
34
|
+
isDir?(target: string): boolean;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Which registry languages are present in a workspace: positive markers at the
|
|
39
|
+
* root or one level down (enough for common monorepo layouts, cheap enough to
|
|
40
|
+
* run at session start).
|
|
41
|
+
*/
|
|
42
|
+
export function detectWorkspaceServers(cwd: string, deps: DetectDeps = {}): LanguageServerSpec[] {
|
|
43
|
+
const exists = deps.exists ?? fs.existsSync;
|
|
44
|
+
const isDir = deps.isDir ?? ((target: string) => {
|
|
45
|
+
try {
|
|
46
|
+
return fs.statSync(target).isDirectory();
|
|
47
|
+
} catch {
|
|
48
|
+
return false;
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
const readDir = deps.readDir ?? ((dir: string) => {
|
|
52
|
+
try {
|
|
53
|
+
return fs.readdirSync(dir);
|
|
54
|
+
} catch {
|
|
55
|
+
return [];
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
const roots = [cwd];
|
|
60
|
+
for (const entry of readDir(cwd).slice(0, MAX_SCAN_ENTRIES)) {
|
|
61
|
+
if (entry.startsWith(".") || SKIPPED_SCAN_DIRS.has(entry)) continue;
|
|
62
|
+
const child = `${cwd}/${entry}`;
|
|
63
|
+
if (isDir(child)) roots.push(child);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
return LSP_SERVERS.filter((spec) =>
|
|
67
|
+
roots.some((root) => detectMarkers(spec).some((marker) => exists(`${root}/${marker}`))),
|
|
68
|
+
);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
interface LspPrefs {
|
|
72
|
+
dismissed: string[];
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** Tolerant read: a missing or corrupt prefs file means "nothing dismissed", never an error. */
|
|
76
|
+
export function loadDismissedLanguages(prefsPath: string, readFile: (p: string) => string = (p) => fs.readFileSync(p, "utf8")): Set<string> {
|
|
77
|
+
try {
|
|
78
|
+
const parsed = JSON.parse(readFile(prefsPath)) as Partial<LspPrefs>;
|
|
79
|
+
return new Set(Array.isArray(parsed.dismissed) ? parsed.dismissed.filter((entry) => typeof entry === "string") : []);
|
|
80
|
+
} catch {
|
|
81
|
+
return new Set();
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export function saveDismissedLanguages(
|
|
86
|
+
prefsPath: string,
|
|
87
|
+
dismissed: Set<string>,
|
|
88
|
+
writeFile: (p: string, data: string) => void = (p, data) => fs.writeFileSync(p, data),
|
|
89
|
+
): void {
|
|
90
|
+
writeFile(prefsPath, `${JSON.stringify({ dismissed: [...dismissed].sort() }, null, 2)}\n`);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* The system-prompt addendum that turns detection into a conversational offer.
|
|
95
|
+
* Returns undefined when there is nothing to offer (nothing detected missing,
|
|
96
|
+
* or everything relevant was dismissed).
|
|
97
|
+
*/
|
|
98
|
+
export function buildLspOffer(missing: LanguageServerSpec[], dismissed: Set<string>): string | undefined {
|
|
99
|
+
const offerable = missing.filter((spec) => !dismissed.has(spec.language));
|
|
100
|
+
if (offerable.length === 0) return undefined;
|
|
101
|
+
const languages = offerable.map((spec) => spec.language).join(", ");
|
|
102
|
+
return [
|
|
103
|
+
"## Code intelligence (LSP)",
|
|
104
|
+
`This workspace contains ${languages} code, but the matching language server(s) are not installed, so the \`lsp\` tool cannot serve those files yet.`,
|
|
105
|
+
`At a natural moment, offer ONCE — briefly, in your own words (e.g. "I can enable ${offerable[0].language} LSP support if you'd like — just say the word!") — to enable it.`,
|
|
106
|
+
'If the user agrees, call lsp_enable with {"language": "<language>"} for each language they want.',
|
|
107
|
+
'If they decline or ask not to be asked again, call lsp_enable with {"language": "<language>", "action": "dismiss"} and drop the subject.',
|
|
108
|
+
"Never call lsp_enable without the user's explicit go-ahead, and do not repeat the offer in this session.",
|
|
109
|
+
].join("\n");
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
export interface InstallResult {
|
|
113
|
+
ok: boolean;
|
|
114
|
+
text: string;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
export interface InstallDeps {
|
|
118
|
+
which?: WhichFn;
|
|
119
|
+
spawnFn?: SpawnFn;
|
|
120
|
+
timeoutMs?: number;
|
|
121
|
+
killGraceMs?: number;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/** Run the spec's install command. Only ever called with the user's explicit consent. */
|
|
125
|
+
export async function installServer(spec: LanguageServerSpec, deps: InstallDeps = {}, signal?: AbortSignal): Promise<InstallResult> {
|
|
126
|
+
const which = deps.which ?? defaultWhich;
|
|
127
|
+
const commandText = installCommandText(spec);
|
|
128
|
+
|
|
129
|
+
if (resolveBinary(spec, which)) {
|
|
130
|
+
return { ok: true, text: `The ${spec.language} language server is already installed — the lsp tool is ready for ${spec.language} files.` };
|
|
131
|
+
}
|
|
132
|
+
if (!which(spec.install.requires)) {
|
|
133
|
+
return {
|
|
134
|
+
ok: false,
|
|
135
|
+
text: `Cannot install the ${spec.language} language server: it needs \`${spec.install.requires}\` on PATH (the install command is \`${commandText}\`). The install-lsps skill covers installing the ${spec.install.requires} toolchain with the user's consent.`,
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
const [command, ...args] = spec.install.command;
|
|
140
|
+
const spawnFn = deps.spawnFn ?? nodeSpawn;
|
|
141
|
+
const timeoutMs = deps.timeoutMs ?? INSTALL_TIMEOUT_MS;
|
|
142
|
+
const killGraceMs = deps.killGraceMs ?? INSTALL_KILL_GRACE_MS;
|
|
143
|
+
|
|
144
|
+
const outcome = await new Promise<{ code: number | null; output: string; cancelled?: "aborted" | "timed out" }>((resolve) => {
|
|
145
|
+
const child = spawnFn(command, args, { shell: false, stdio: ["ignore", "pipe", "pipe"] });
|
|
146
|
+
let tail = "";
|
|
147
|
+
let settled = false;
|
|
148
|
+
let cancelled: "aborted" | "timed out" | undefined;
|
|
149
|
+
let killTimer: NodeJS.Timeout | undefined;
|
|
150
|
+
const settle = (code: number | null): void => {
|
|
151
|
+
if (settled) return;
|
|
152
|
+
settled = true;
|
|
153
|
+
clearTimeout(timer);
|
|
154
|
+
if (killTimer) clearTimeout(killTimer);
|
|
155
|
+
signal?.removeEventListener("abort", onAbort);
|
|
156
|
+
resolve({ code, output: tail, cancelled });
|
|
157
|
+
};
|
|
158
|
+
const append = (chunk: Buffer): void => {
|
|
159
|
+
tail = (tail + chunk.toString("utf8")).slice(-OUTPUT_TAIL_BYTES);
|
|
160
|
+
};
|
|
161
|
+
// Cancellation must not settle before the child actually exits — an
|
|
162
|
+
// installer that shrugs off SIGTERM would keep mutating the system after
|
|
163
|
+
// we reported failure. Settle on close, escalating to SIGKILL if needed.
|
|
164
|
+
const cancel = (reason: "aborted" | "timed out"): void => {
|
|
165
|
+
if (settled || cancelled) return;
|
|
166
|
+
cancelled = reason;
|
|
167
|
+
child.kill("SIGTERM");
|
|
168
|
+
killTimer = setTimeout(() => {
|
|
169
|
+
if (!settled) child.kill("SIGKILL");
|
|
170
|
+
}, killGraceMs);
|
|
171
|
+
killTimer.unref?.();
|
|
172
|
+
};
|
|
173
|
+
const onAbort = (): void => cancel("aborted");
|
|
174
|
+
const timer = setTimeout(() => cancel("timed out"), timeoutMs);
|
|
175
|
+
timer.unref?.();
|
|
176
|
+
|
|
177
|
+
child.stdout?.on("data", append);
|
|
178
|
+
child.stderr?.on("data", append);
|
|
179
|
+
child.on("close", (code) => settle(code));
|
|
180
|
+
child.on("error", (error: Error) => {
|
|
181
|
+
append(Buffer.from(error.message));
|
|
182
|
+
settle(null);
|
|
183
|
+
});
|
|
184
|
+
if (signal) {
|
|
185
|
+
if (signal.aborted) onAbort();
|
|
186
|
+
else signal.addEventListener("abort", onAbort, { once: true });
|
|
187
|
+
}
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
const lastOutputLine =
|
|
191
|
+
outcome.output
|
|
192
|
+
.split("\n")
|
|
193
|
+
.map((line) => line.trim())
|
|
194
|
+
.filter(Boolean)
|
|
195
|
+
.pop() ?? "";
|
|
196
|
+
if (outcome.cancelled) {
|
|
197
|
+
return {
|
|
198
|
+
ok: false,
|
|
199
|
+
text: `Installing the ${spec.language} language server ${outcome.cancelled} (\`${commandText}\`); the installer process was stopped. The install may be partial — re-run \`${commandText}\` to complete it.`,
|
|
200
|
+
};
|
|
201
|
+
}
|
|
202
|
+
if (outcome.code !== 0) {
|
|
203
|
+
return {
|
|
204
|
+
ok: false,
|
|
205
|
+
text: `Installing the ${spec.language} language server failed (\`${commandText}\`${outcome.code === null ? "" : `, exit ${outcome.code}`})${lastOutputLine ? `: ${lastOutputLine}` : ""}`,
|
|
206
|
+
};
|
|
207
|
+
}
|
|
208
|
+
if (!resolveBinary(spec, which)) {
|
|
209
|
+
return {
|
|
210
|
+
ok: false,
|
|
211
|
+
text: `\`${commandText}\` succeeded but its binary is still not resolvable — the install location is probably not on PATH. Ask the user to add it and restart pi.`,
|
|
212
|
+
};
|
|
213
|
+
}
|
|
214
|
+
return { ok: true, text: `Installed the ${spec.language} language server — the lsp tool now works for ${spec.language} files in this session.` };
|
|
215
|
+
}
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Content-Length framing and coordinate conversion.
|
|
3
|
+
*
|
|
4
|
+
* Both transports in this subsystem speak the same wire format: LSP servers
|
|
5
|
+
* over stdio, and the subagent broker over a local socket. Framing lives here
|
|
6
|
+
* so neither owns it.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { fileURLToPath, pathToFileURL } from "node:url";
|
|
10
|
+
|
|
11
|
+
export interface JsonRpcMessage {
|
|
12
|
+
jsonrpc?: string;
|
|
13
|
+
id?: number | string | null;
|
|
14
|
+
method?: string;
|
|
15
|
+
params?: unknown;
|
|
16
|
+
result?: unknown;
|
|
17
|
+
error?: { code: number; message: string; data?: unknown };
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const HEADER_SEPARATOR = "\r\n\r\n";
|
|
21
|
+
|
|
22
|
+
/** Buffered-bytes ceiling. A peer that blows through it is malfunctioning; the owner kills it. */
|
|
23
|
+
export const MAX_FRAME_BYTES = 16 * 1024 * 1024;
|
|
24
|
+
|
|
25
|
+
export function encodeFrame(message: unknown): Buffer {
|
|
26
|
+
const body = Buffer.from(JSON.stringify(message), "utf8");
|
|
27
|
+
// Content-Length counts BYTES, not characters — a UTF-8 body with any
|
|
28
|
+
// non-ASCII content would otherwise desynchronize the peer's parser.
|
|
29
|
+
return Buffer.concat([Buffer.from(`Content-Length: ${body.length}${HEADER_SEPARATOR}`, "ascii"), body]);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export interface FrameParser {
|
|
33
|
+
push(chunk: Buffer): void;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Incremental parser: handles several frames per chunk, one frame split across
|
|
38
|
+
* chunks, and extra headers (`Content-Type`). A body that is not JSON costs
|
|
39
|
+
* that one message; `onOverflow` fires once and the parser then stays quiet so
|
|
40
|
+
* the owner can tear the peer down.
|
|
41
|
+
*/
|
|
42
|
+
export function createFrameParser(onMessage: (message: JsonRpcMessage) => void, onOverflow?: (error: Error) => void): FrameParser {
|
|
43
|
+
let buffer: Buffer = Buffer.alloc(0);
|
|
44
|
+
let overflowed = false;
|
|
45
|
+
|
|
46
|
+
return {
|
|
47
|
+
push(chunk: Buffer): void {
|
|
48
|
+
if (overflowed) return;
|
|
49
|
+
buffer = buffer.length === 0 ? chunk : Buffer.concat([buffer, chunk]);
|
|
50
|
+
if (buffer.length > MAX_FRAME_BYTES) {
|
|
51
|
+
overflowed = true;
|
|
52
|
+
buffer = Buffer.alloc(0);
|
|
53
|
+
onOverflow?.(new Error(`framed message exceeded ${MAX_FRAME_BYTES} bytes`));
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
while (true) {
|
|
58
|
+
const headerEnd = buffer.indexOf(HEADER_SEPARATOR);
|
|
59
|
+
if (headerEnd === -1) return;
|
|
60
|
+
const header = buffer.subarray(0, headerEnd).toString("ascii");
|
|
61
|
+
const match = /content-length:\s*(\d+)/i.exec(header);
|
|
62
|
+
if (!match) {
|
|
63
|
+
// No length means no way to find the next boundary; drop the header
|
|
64
|
+
// block and resynchronize on whatever follows it.
|
|
65
|
+
buffer = buffer.subarray(headerEnd + HEADER_SEPARATOR.length);
|
|
66
|
+
continue;
|
|
67
|
+
}
|
|
68
|
+
const bodyStart = headerEnd + HEADER_SEPARATOR.length;
|
|
69
|
+
const bodyLength = Number(match[1]);
|
|
70
|
+
if (buffer.length < bodyStart + bodyLength) return;
|
|
71
|
+
const body = buffer.subarray(bodyStart, bodyStart + bodyLength).toString("utf8");
|
|
72
|
+
buffer = buffer.subarray(bodyStart + bodyLength);
|
|
73
|
+
try {
|
|
74
|
+
onMessage(JSON.parse(body) as JsonRpcMessage);
|
|
75
|
+
} catch {
|
|
76
|
+
/* malformed body — skip this message, keep the stream */
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
},
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export function pathToUri(filePath: string): string {
|
|
84
|
+
return pathToFileURL(filePath).toString();
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export function uriToPath(uri: string): string {
|
|
88
|
+
if (!uri.startsWith("file:")) return uri;
|
|
89
|
+
try {
|
|
90
|
+
return fileURLToPath(uri);
|
|
91
|
+
} catch {
|
|
92
|
+
// Some servers emit non-canonical file URIs (unencoded spaces).
|
|
93
|
+
return decodeURIComponent(uri.replace(/^file:\/\//, ""));
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* The operation vocabulary. It lives beside the framing rather than with the pi
|
|
99
|
+
* tool because it is literally the broker's wire payload — keeping it here lets
|
|
100
|
+
* the broker stay below the tool adapter instead of importing back into it.
|
|
101
|
+
*/
|
|
102
|
+
export const LSP_OPERATIONS = ["definition", "references", "hover", "documentSymbol", "diagnostics"] as const;
|
|
103
|
+
|
|
104
|
+
export type LspOperation = (typeof LSP_OPERATIONS)[number];
|
|
105
|
+
|
|
106
|
+
/** Operations that resolve a point in a file, and so require line + column. */
|
|
107
|
+
export const POSITION_OPERATIONS = new Set<LspOperation>(["definition", "references", "hover"]);
|
|
108
|
+
|
|
109
|
+
export interface LspOperationParams {
|
|
110
|
+
operation: LspOperation;
|
|
111
|
+
path: string;
|
|
112
|
+
line?: number;
|
|
113
|
+
column?: number;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
export interface LspPosition {
|
|
117
|
+
line: number;
|
|
118
|
+
character: number;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/** Model-facing 1-based line/column to the 0-based position LSP uses. */
|
|
122
|
+
export function toLspPosition(line: number, column: number): LspPosition {
|
|
123
|
+
return { line: Math.max(0, Math.trunc(line) - 1), character: Math.max(0, Math.trunc(column) - 1) };
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
export function fromLspPosition(position: LspPosition | undefined): { line: number; column: number } {
|
|
127
|
+
return { line: (position?.line ?? 0) + 1, column: (position?.character ?? 0) + 1 };
|
|
128
|
+
}
|
|
@@ -0,0 +1,361 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Which language server serves which file, and the pool of live ones.
|
|
3
|
+
*
|
|
4
|
+
* The registry is a data table rather than a class hierarchy: adding a language
|
|
5
|
+
* is one row, and every behavioral difference between servers is already
|
|
6
|
+
* expressible as data (binary, root markers, install hint).
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import * as fs from "node:fs";
|
|
10
|
+
import * as os from "node:os";
|
|
11
|
+
import * as path from "node:path";
|
|
12
|
+
import { abortable, LspClient, spawnTransport, type LspClientOptions } from "./lsp-client.js";
|
|
13
|
+
|
|
14
|
+
export interface ServerBinary {
|
|
15
|
+
command: string;
|
|
16
|
+
args: string[];
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export interface ServerInstall {
|
|
20
|
+
/** Toolchain binary that must already exist for the install to be possible. */
|
|
21
|
+
requires: string;
|
|
22
|
+
command: string[];
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export interface LanguageServerSpec {
|
|
26
|
+
id: string;
|
|
27
|
+
/** Model/user-facing language name ("go"), distinct from the server id ("gopls"). */
|
|
28
|
+
language: string;
|
|
29
|
+
/** Lowercased file extension (with dot) to LSP languageId. */
|
|
30
|
+
languages: Record<string, string>;
|
|
31
|
+
/** Tried in order; the first one on PATH wins. */
|
|
32
|
+
binaries: ServerBinary[];
|
|
33
|
+
rootMarkers: string[];
|
|
34
|
+
install: ServerInstall;
|
|
35
|
+
/**
|
|
36
|
+
* Where the install command drops its binary when that directory is not on
|
|
37
|
+
* PATH ("~" expands to the home dir). A function of the environment because
|
|
38
|
+
* the toolchains honor overrides (GOBIN / GOPATH / CARGO_HOME) — hardcoding
|
|
39
|
+
* the defaults would lose a successful install to a non-default location.
|
|
40
|
+
*/
|
|
41
|
+
fallbackDirs(env: NodeJS.ProcessEnv): string[];
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** `go install` target: $GOBIN, else <first GOPATH element>/bin, else ~/go/bin. */
|
|
45
|
+
export function goBinDirs(env: NodeJS.ProcessEnv): string[] {
|
|
46
|
+
const dirs: string[] = [];
|
|
47
|
+
if (env.GOBIN?.trim()) dirs.push(env.GOBIN.trim());
|
|
48
|
+
const gopathFirst = env.GOPATH?.split(path.delimiter)[0]?.trim();
|
|
49
|
+
dirs.push(gopathFirst ? path.join(gopathFirst, "bin") : "~/go/bin");
|
|
50
|
+
return dirs;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** rustup/cargo bin dir: $CARGO_HOME/bin, else ~/.cargo/bin. */
|
|
54
|
+
export function cargoBinDirs(env: NodeJS.ProcessEnv): string[] {
|
|
55
|
+
const cargoHome = env.CARGO_HOME?.trim();
|
|
56
|
+
return [cargoHome ? path.join(cargoHome, "bin") : "~/.cargo/bin"];
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export const LSP_SERVERS: LanguageServerSpec[] = [
|
|
60
|
+
{
|
|
61
|
+
id: "gopls",
|
|
62
|
+
language: "go",
|
|
63
|
+
languages: { ".go": "go" },
|
|
64
|
+
binaries: [{ command: "gopls", args: ["serve"] }],
|
|
65
|
+
rootMarkers: ["go.work", "go.mod"],
|
|
66
|
+
install: { requires: "go", command: ["go", "install", "golang.org/x/tools/gopls@latest"] },
|
|
67
|
+
fallbackDirs: goBinDirs,
|
|
68
|
+
},
|
|
69
|
+
{
|
|
70
|
+
id: "typescript",
|
|
71
|
+
language: "typescript",
|
|
72
|
+
languages: {
|
|
73
|
+
".ts": "typescript",
|
|
74
|
+
".tsx": "typescriptreact",
|
|
75
|
+
".mts": "typescript",
|
|
76
|
+
".cts": "typescript",
|
|
77
|
+
".js": "javascript",
|
|
78
|
+
".jsx": "javascriptreact",
|
|
79
|
+
".mjs": "javascript",
|
|
80
|
+
".cjs": "javascript",
|
|
81
|
+
},
|
|
82
|
+
binaries: [{ command: "typescript-language-server", args: ["--stdio"] }],
|
|
83
|
+
rootMarkers: ["tsconfig.json", "jsconfig.json", "package.json", ".git"],
|
|
84
|
+
install: { requires: "npm", command: ["npm", "i", "-g", "typescript-language-server", "typescript"] },
|
|
85
|
+
fallbackDirs: () => [],
|
|
86
|
+
},
|
|
87
|
+
{
|
|
88
|
+
id: "pyright",
|
|
89
|
+
language: "python",
|
|
90
|
+
languages: { ".py": "python", ".pyi": "python" },
|
|
91
|
+
binaries: [
|
|
92
|
+
{ command: "pyright-langserver", args: ["--stdio"] },
|
|
93
|
+
{ command: "basedpyright-langserver", args: ["--stdio"] },
|
|
94
|
+
],
|
|
95
|
+
rootMarkers: ["pyrightconfig.json", "pyproject.toml", "setup.py", "requirements.txt", ".git"],
|
|
96
|
+
install: { requires: "npm", command: ["npm", "i", "-g", "pyright"] },
|
|
97
|
+
fallbackDirs: () => [],
|
|
98
|
+
},
|
|
99
|
+
{
|
|
100
|
+
id: "rust-analyzer",
|
|
101
|
+
language: "rust",
|
|
102
|
+
languages: { ".rs": "rust" },
|
|
103
|
+
binaries: [{ command: "rust-analyzer", args: [] }],
|
|
104
|
+
rootMarkers: ["Cargo.toml"],
|
|
105
|
+
install: { requires: "rustup", command: ["rustup", "component", "add", "rust-analyzer"] },
|
|
106
|
+
fallbackDirs: cargoBinDirs,
|
|
107
|
+
},
|
|
108
|
+
];
|
|
109
|
+
|
|
110
|
+
export function specForLanguage(language: string): LanguageServerSpec | undefined {
|
|
111
|
+
return LSP_SERVERS.find((spec) => spec.language === language.toLowerCase());
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
export function installCommandText(spec: LanguageServerSpec): string {
|
|
115
|
+
return spec.install.command.join(" ");
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Markers that positively indicate the language is present in a directory.
|
|
120
|
+
* `.git` is a root-walk fallback, not evidence of any language.
|
|
121
|
+
*/
|
|
122
|
+
export function detectMarkers(spec: LanguageServerSpec): string[] {
|
|
123
|
+
return spec.rootMarkers.filter((marker) => marker !== ".git");
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
export function specForFile(filePath: string): LanguageServerSpec | undefined {
|
|
127
|
+
const extension = path.extname(filePath).toLowerCase();
|
|
128
|
+
if (!extension) return undefined;
|
|
129
|
+
return LSP_SERVERS.find((spec) => extension in spec.languages);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
export function languageIdFor(spec: LanguageServerSpec, filePath: string): string {
|
|
133
|
+
return spec.languages[path.extname(filePath).toLowerCase()] ?? "plaintext";
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
export function supportedExtensions(): string[] {
|
|
137
|
+
return LSP_SERVERS.flatMap((spec) => Object.keys(spec.languages)).sort();
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
export type ExistsFn = (target: string) => boolean;
|
|
141
|
+
|
|
142
|
+
/** Nearest ancestor holding a marker wins; `fallbackCwd` when the file sits outside any project. */
|
|
143
|
+
export function findWorkspaceRoot(filePath: string, markers: string[], fallbackCwd: string, exists: ExistsFn = fs.existsSync): string {
|
|
144
|
+
let current = path.dirname(path.resolve(filePath));
|
|
145
|
+
while (true) {
|
|
146
|
+
for (const marker of markers) {
|
|
147
|
+
if (exists(path.join(current, marker))) return current;
|
|
148
|
+
}
|
|
149
|
+
const parent = path.dirname(current);
|
|
150
|
+
if (parent === current) return fallbackCwd;
|
|
151
|
+
current = parent;
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
export type WhichFn = (command: string) => string | undefined;
|
|
156
|
+
|
|
157
|
+
function isExecutable(candidate: string): boolean {
|
|
158
|
+
try {
|
|
159
|
+
if (!fs.statSync(candidate).isFile()) return false;
|
|
160
|
+
// Windows has no X_OK bit; presence on PATH with a known extension is the test.
|
|
161
|
+
if (process.platform !== "win32") fs.accessSync(candidate, fs.constants.X_OK);
|
|
162
|
+
return true;
|
|
163
|
+
} catch {
|
|
164
|
+
return false;
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/** Hand-rolled PATH scan — the package ships raw TS with peer deps only, so no `which` dependency. */
|
|
169
|
+
export function defaultWhich(command: string): string | undefined {
|
|
170
|
+
if (command.includes("/") || command.includes(path.sep)) return isExecutable(command) ? command : undefined;
|
|
171
|
+
const extensions = process.platform === "win32" ? ["", ".cmd", ".exe", ".bat"] : [""];
|
|
172
|
+
for (const dir of (process.env.PATH ?? "").split(path.delimiter)) {
|
|
173
|
+
if (!dir) continue;
|
|
174
|
+
for (const extension of extensions) {
|
|
175
|
+
const candidate = path.join(dir, command + extension);
|
|
176
|
+
if (isExecutable(candidate)) return candidate;
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
return undefined;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
function expandHome(dir: string): string {
|
|
183
|
+
return dir.startsWith("~/") ? path.join(os.homedir(), dir.slice(2)) : dir;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
export function resolveBinary(
|
|
187
|
+
spec: LanguageServerSpec,
|
|
188
|
+
which: WhichFn = defaultWhich,
|
|
189
|
+
env: NodeJS.ProcessEnv = process.env,
|
|
190
|
+
): ServerBinary | undefined {
|
|
191
|
+
for (const binary of spec.binaries) {
|
|
192
|
+
// Fallback candidates are absolute, which defaultWhich checks directly;
|
|
193
|
+
// routing them through `which` keeps a single injectable seam.
|
|
194
|
+
const candidates = [binary.command, ...spec.fallbackDirs(env).map((dir) => path.join(expandHome(dir), binary.command))];
|
|
195
|
+
for (const candidate of candidates) {
|
|
196
|
+
const resolved = which(candidate);
|
|
197
|
+
if (resolved) return { command: resolved, args: binary.args };
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
return undefined;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
export function missingServerText(spec: LanguageServerSpec): string {
|
|
204
|
+
const names = spec.binaries.map((binary) => binary.command).join(" or ");
|
|
205
|
+
return [
|
|
206
|
+
`No language server for this file type: ${names} is not on PATH.`,
|
|
207
|
+
`The user can enable it (install: ${installCommandText(spec)}), or ask the assistant to via the lsp_enable tool.`,
|
|
208
|
+
"Until then, use grep/read for this file instead.",
|
|
209
|
+
].join("\n");
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
export interface PoolOptions extends LspClientOptions {
|
|
213
|
+
maxServers: number;
|
|
214
|
+
idleMs: number;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
export interface PoolDeps {
|
|
218
|
+
createClient?(spec: LanguageServerSpec, binary: ServerBinary, root: string, options: LspClientOptions): LspClient;
|
|
219
|
+
now?(): number;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
interface PoolEntry {
|
|
223
|
+
client: LspClient;
|
|
224
|
+
ready: Promise<LspClient>;
|
|
225
|
+
lastUsed: number;
|
|
226
|
+
/** False until initialize succeeds — pending entries are exempt from idle and LRU eviction. */
|
|
227
|
+
initialized: boolean;
|
|
228
|
+
idleTimer?: NodeJS.Timeout;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
function defaultCreateClient(
|
|
232
|
+
_spec: LanguageServerSpec,
|
|
233
|
+
binary: ServerBinary,
|
|
234
|
+
root: string,
|
|
235
|
+
options: LspClientOptions,
|
|
236
|
+
): LspClient {
|
|
237
|
+
return new LspClient(root, spawnTransport(binary.command, binary.args, root), options);
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/**
|
|
241
|
+
* Live servers keyed by workspace root + language. Servers are expensive to
|
|
242
|
+
* start (gopls indexes a module) and cheap to keep, so they are spawned lazily,
|
|
243
|
+
* shared by every caller including broker-connected subagents, and reclaimed on
|
|
244
|
+
* idle or LRU pressure.
|
|
245
|
+
*/
|
|
246
|
+
export class LspServerPool {
|
|
247
|
+
private readonly entries = new Map<string, PoolEntry>();
|
|
248
|
+
private readonly createClient: NonNullable<PoolDeps["createClient"]>;
|
|
249
|
+
private readonly now: () => number;
|
|
250
|
+
|
|
251
|
+
constructor(
|
|
252
|
+
private readonly options: PoolOptions,
|
|
253
|
+
deps: PoolDeps = {},
|
|
254
|
+
) {
|
|
255
|
+
this.createClient = deps.createClient ?? defaultCreateClient;
|
|
256
|
+
this.now = deps.now ?? Date.now;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
get size(): number {
|
|
260
|
+
return this.entries.size;
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
async acquire(spec: LanguageServerSpec, binary: ServerBinary, root: string, signal?: AbortSignal): Promise<LspClient> {
|
|
264
|
+
const key = `${root} ${spec.id}`;
|
|
265
|
+
const existing = this.entries.get(key);
|
|
266
|
+
if (existing && existing.client.dead) {
|
|
267
|
+
this.forget(key, existing);
|
|
268
|
+
existing.client.killNow();
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
let entry = this.entries.get(key);
|
|
272
|
+
if (!entry) {
|
|
273
|
+
const client = this.createClient(spec, binary, root, this.options);
|
|
274
|
+
const created: PoolEntry = { client, ready: Promise.resolve(client), lastUsed: this.now(), initialized: false };
|
|
275
|
+
created.ready = client.initialize().then(() => {
|
|
276
|
+
// Only now does the entry become evictable: an idle timer or LRU pass
|
|
277
|
+
// during the (up to warmup-length) handshake would dispose a client
|
|
278
|
+
// its original caller is still awaiting.
|
|
279
|
+
created.initialized = true;
|
|
280
|
+
if (this.entries.get(key) === created) this.armIdleTimer(key, created);
|
|
281
|
+
return client;
|
|
282
|
+
});
|
|
283
|
+
this.entries.set(key, created);
|
|
284
|
+
// A failed handshake must not stay cached as a poisoned promise.
|
|
285
|
+
created.ready.catch(() => {
|
|
286
|
+
if (this.entries.get(key) === created) this.forget(key, created);
|
|
287
|
+
client.killNow();
|
|
288
|
+
});
|
|
289
|
+
entry = created;
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
entry.lastUsed = this.now();
|
|
293
|
+
if (entry.initialized) this.armIdleTimer(key, entry);
|
|
294
|
+
this.evictOverCap(key);
|
|
295
|
+
return abortable(entry.ready, signal);
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
async shutdownAll(): Promise<void> {
|
|
299
|
+
const entries = [...this.entries.entries()];
|
|
300
|
+
this.entries.clear();
|
|
301
|
+
await Promise.all(
|
|
302
|
+
entries.map(async ([, entry]) => {
|
|
303
|
+
if (entry.idleTimer) clearTimeout(entry.idleTimer);
|
|
304
|
+
try {
|
|
305
|
+
await entry.client.dispose();
|
|
306
|
+
} catch {
|
|
307
|
+
/* one stuck server must not block the rest of shutdown */
|
|
308
|
+
}
|
|
309
|
+
}),
|
|
310
|
+
);
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
/** Emergency sweep for `process.on("exit")`, where nothing async can run. */
|
|
314
|
+
killAllSync(): void {
|
|
315
|
+
for (const [, entry] of this.entries) {
|
|
316
|
+
if (entry.idleTimer) clearTimeout(entry.idleTimer);
|
|
317
|
+
entry.client.killNow();
|
|
318
|
+
}
|
|
319
|
+
this.entries.clear();
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
private forget(key: string, entry: PoolEntry): void {
|
|
323
|
+
if (entry.idleTimer) clearTimeout(entry.idleTimer);
|
|
324
|
+
if (this.entries.get(key) === entry) this.entries.delete(key);
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
private armIdleTimer(key: string, entry: PoolEntry): void {
|
|
328
|
+
if (entry.idleTimer) clearTimeout(entry.idleTimer);
|
|
329
|
+
entry.idleTimer = setTimeout(() => {
|
|
330
|
+
// A slow request (or diagnostics wait) can outlive a short idle window;
|
|
331
|
+
// "idle" means no in-flight work, not merely no recent acquire.
|
|
332
|
+
if (entry.client.busy && !entry.client.dead) {
|
|
333
|
+
this.armIdleTimer(key, entry);
|
|
334
|
+
return;
|
|
335
|
+
}
|
|
336
|
+
this.forget(key, entry);
|
|
337
|
+
void entry.client.dispose();
|
|
338
|
+
}, this.options.idleMs);
|
|
339
|
+
entry.idleTimer.unref?.();
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
private evictOverCap(keepKey: string): void {
|
|
343
|
+
while (this.entries.size > Math.max(1, this.options.maxServers)) {
|
|
344
|
+
let oldestKey: string | undefined;
|
|
345
|
+
let oldestEntry: PoolEntry | undefined;
|
|
346
|
+
for (const [key, entry] of this.entries) {
|
|
347
|
+
if (key === keepKey) continue;
|
|
348
|
+
// Still initializing = someone is awaiting it. The cap is soft during
|
|
349
|
+
// warmup; the overflow is reclaimed on the next settled acquire.
|
|
350
|
+
if (!entry.initialized) continue;
|
|
351
|
+
if (!oldestEntry || entry.lastUsed < oldestEntry.lastUsed) {
|
|
352
|
+
oldestKey = key;
|
|
353
|
+
oldestEntry = entry;
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
if (!oldestKey || !oldestEntry) return;
|
|
357
|
+
this.forget(oldestKey, oldestEntry);
|
|
358
|
+
void oldestEntry.client.dispose();
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
}
|