march-cli 0.1.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.
Files changed (217) hide show
  1. package/bin/march.mjs +13 -0
  2. package/package.json +36 -0
  3. package/src/agent/command-exec-tool.mjs +91 -0
  4. package/src/agent/context-stats-tool.mjs +57 -0
  5. package/src/agent/editing/diff-apply.mjs +28 -0
  6. package/src/agent/editing/diff-format.mjs +57 -0
  7. package/src/agent/file-edit-tool.mjs +276 -0
  8. package/src/agent/find-tool.mjs +112 -0
  9. package/src/agent/model-payload-dumper.mjs +201 -0
  10. package/src/agent/pi-session/pi-session-sidecar-failure.mjs +10 -0
  11. package/src/agent/provider/payload-messages.mjs +138 -0
  12. package/src/agent/read-file-tool.mjs +112 -0
  13. package/src/agent/runner/fast-model.mjs +36 -0
  14. package/src/agent/runner/runner-cleanup.mjs +12 -0
  15. package/src/agent/runner/runner-init.mjs +15 -0
  16. package/src/agent/runner/runner-session-state.mjs +40 -0
  17. package/src/agent/runner.mjs +266 -0
  18. package/src/agent/runtime/runner-runtime-host.mjs +73 -0
  19. package/src/agent/runtime/runtime-factory.mjs +42 -0
  20. package/src/agent/runtime/runtime-host.mjs +34 -0
  21. package/src/agent/session/session-auto-name.mjs +41 -0
  22. package/src/agent/session/session-binding.mjs +12 -0
  23. package/src/agent/session/session-options.mjs +46 -0
  24. package/src/agent/tool-names.mjs +1 -0
  25. package/src/agent/tool-result.mjs +3 -0
  26. package/src/agent/tools.mjs +54 -0
  27. package/src/agent/turn/turn-events.mjs +64 -0
  28. package/src/agent/turn/turn-runner.mjs +103 -0
  29. package/src/auth/login-command.mjs +90 -0
  30. package/src/auth/storage.mjs +33 -0
  31. package/src/cli/args.mjs +71 -0
  32. package/src/cli/commands/copy-command.mjs +73 -0
  33. package/src/cli/commands/export-command.mjs +206 -0
  34. package/src/cli/commands/extensions-command.mjs +53 -0
  35. package/src/cli/commands/help-command.mjs +7 -0
  36. package/src/cli/commands/model-command.mjs +110 -0
  37. package/src/cli/commands/paste-image-command.mjs +43 -0
  38. package/src/cli/commands/provider-command.mjs +55 -0
  39. package/src/cli/commands/status-command.mjs +157 -0
  40. package/src/cli/commands/thinking-command.mjs +80 -0
  41. package/src/cli/fallback-ui.mjs +156 -0
  42. package/src/cli/input/attachment-tokens.mjs +20 -0
  43. package/src/cli/input/autocomplete.mjs +106 -0
  44. package/src/cli/input/external-editor.mjs +39 -0
  45. package/src/cli/input/history-store.mjs +35 -0
  46. package/src/cli/input/image-clipboard.mjs +55 -0
  47. package/src/cli/input/keybinding-dispatch.mjs +76 -0
  48. package/src/cli/input/keybindings.mjs +96 -0
  49. package/src/cli/input/mode-state.mjs +43 -0
  50. package/src/cli/input/prompt-templates.mjs +84 -0
  51. package/src/cli/input/select-with-keyboard.mjs +67 -0
  52. package/src/cli/permissions.mjs +103 -0
  53. package/src/cli/repl-commands.mjs +86 -0
  54. package/src/cli/repl-loop.mjs +157 -0
  55. package/src/cli/selector-list.mjs +21 -0
  56. package/src/cli/session/pi-session-switch-command.mjs +41 -0
  57. package/src/cli/session/session-command.mjs +23 -0
  58. package/src/cli/session/session-list-command.mjs +68 -0
  59. package/src/cli/session/session-name-command.mjs +26 -0
  60. package/src/cli/session/session-source-command.mjs +89 -0
  61. package/src/cli/session/session-switch-command.mjs +1 -0
  62. package/src/cli/shell/shell-command.mjs +55 -0
  63. package/src/cli/shell/shell-drawer-controls.mjs +33 -0
  64. package/src/cli/shell/shell-drawer.mjs +192 -0
  65. package/src/cli/shell/shell-split-layout.mjs +70 -0
  66. package/src/cli/slash-commands.mjs +176 -0
  67. package/src/cli/startup/startup-banner.mjs +17 -0
  68. package/src/cli/startup/startup-session.mjs +51 -0
  69. package/src/cli/status-line-updater.mjs +74 -0
  70. package/src/cli/tool-output.mjs +9 -0
  71. package/src/cli/tui/editor/external-editor-runner.mjs +24 -0
  72. package/src/cli/tui/input/mouse-selection-controller.mjs +89 -0
  73. package/src/cli/tui/input/mouse-tracking.mjs +20 -0
  74. package/src/cli/tui/layout/main-pane-layout.mjs +38 -0
  75. package/src/cli/tui/layout/safe-render-boundary.mjs +46 -0
  76. package/src/cli/tui/markdown-renderer.mjs +279 -0
  77. package/src/cli/tui/output/scroll-state.mjs +79 -0
  78. package/src/cli/tui/output/tool-card-renderer.mjs +59 -0
  79. package/src/cli/tui/output-buffer.mjs +297 -0
  80. package/src/cli/tui/permission-request-ui.mjs +18 -0
  81. package/src/cli/tui/recall-rendering.mjs +25 -0
  82. package/src/cli/tui/select/editor-select-list.mjs +111 -0
  83. package/src/cli/tui/selection-screen.mjs +212 -0
  84. package/src/cli/tui/status/retry-status.mjs +72 -0
  85. package/src/cli/tui/status/spinner-status.mjs +42 -0
  86. package/src/cli/tui/status/status-bar.mjs +88 -0
  87. package/src/cli/tui/syntax/highlighting.mjs +277 -0
  88. package/src/cli/tui/syntax/languages.mjs +91 -0
  89. package/src/cli/tui/syntax/tree-sitter/bash.highlights.scm +261 -0
  90. package/src/cli/tui/syntax/tree-sitter/c.highlights.scm +341 -0
  91. package/src/cli/tui/syntax/tree-sitter/cpp.highlights.scm +268 -0
  92. package/src/cli/tui/syntax/tree-sitter/csharp.highlights.scm +577 -0
  93. package/src/cli/tui/syntax/tree-sitter/css.highlights.scm +109 -0
  94. package/src/cli/tui/syntax/tree-sitter/diff.highlights.scm +49 -0
  95. package/src/cli/tui/syntax/tree-sitter/go.highlights.scm +254 -0
  96. package/src/cli/tui/syntax/tree-sitter/html.highlights.scm +13 -0
  97. package/src/cli/tui/syntax/tree-sitter/java.highlights.scm +330 -0
  98. package/src/cli/tui/syntax/tree-sitter/json.highlights.scm +38 -0
  99. package/src/cli/tui/syntax/tree-sitter/php.highlights.scm +203 -0
  100. package/src/cli/tui/syntax/tree-sitter/python.highlights.scm +137 -0
  101. package/src/cli/tui/syntax/tree-sitter/ruby.highlights.scm +309 -0
  102. package/src/cli/tui/syntax/tree-sitter/rust.highlights.scm +531 -0
  103. package/src/cli/tui/syntax/tree-sitter/toml.highlights.scm +39 -0
  104. package/src/cli/tui/syntax/tree-sitter/tree-sitter-bash.wasm +0 -0
  105. package/src/cli/tui/syntax/tree-sitter/tree-sitter-c-sharp.wasm +0 -0
  106. package/src/cli/tui/syntax/tree-sitter/tree-sitter-c.wasm +0 -0
  107. package/src/cli/tui/syntax/tree-sitter/tree-sitter-cpp.wasm +0 -0
  108. package/src/cli/tui/syntax/tree-sitter/tree-sitter-css.wasm +0 -0
  109. package/src/cli/tui/syntax/tree-sitter/tree-sitter-diff.wasm +0 -0
  110. package/src/cli/tui/syntax/tree-sitter/tree-sitter-go.wasm +0 -0
  111. package/src/cli/tui/syntax/tree-sitter/tree-sitter-html.wasm +0 -0
  112. package/src/cli/tui/syntax/tree-sitter/tree-sitter-java.wasm +0 -0
  113. package/src/cli/tui/syntax/tree-sitter/tree-sitter-json.wasm +0 -0
  114. package/src/cli/tui/syntax/tree-sitter/tree-sitter-php.wasm +0 -0
  115. package/src/cli/tui/syntax/tree-sitter/tree-sitter-python.wasm +0 -0
  116. package/src/cli/tui/syntax/tree-sitter/tree-sitter-ruby.wasm +0 -0
  117. package/src/cli/tui/syntax/tree-sitter/tree-sitter-rust.wasm +0 -0
  118. package/src/cli/tui/syntax/tree-sitter/tree-sitter-toml.wasm +0 -0
  119. package/src/cli/tui/syntax/tree-sitter/tree-sitter-tsx.wasm +0 -0
  120. package/src/cli/tui/syntax/tree-sitter/tree-sitter-typescript.wasm +0 -0
  121. package/src/cli/tui/syntax/tree-sitter/tree-sitter-yaml.wasm +0 -0
  122. package/src/cli/tui/syntax/tree-sitter/tsx.highlights.scm +35 -0
  123. package/src/cli/tui/syntax/tree-sitter/typescript.highlights.scm +35 -0
  124. package/src/cli/tui/syntax/tree-sitter/yaml.highlights.scm +99 -0
  125. package/src/cli/tui/tool-rendering.mjs +194 -0
  126. package/src/cli/tui/tui-diff-rendering.mjs +157 -0
  127. package/src/cli/tui/tui-handlers.mjs +110 -0
  128. package/src/cli/tui/tui-input-controller.mjs +61 -0
  129. package/src/cli/tui/ui-theme.mjs +148 -0
  130. package/src/cli/ui.mjs +299 -0
  131. package/src/config/config-json.mjs +73 -0
  132. package/src/config/dotenv.mjs +20 -0
  133. package/src/config/features.mjs +75 -0
  134. package/src/config/loader.mjs +109 -0
  135. package/src/config/settings-command.mjs +97 -0
  136. package/src/context/diagnostics.mjs +70 -0
  137. package/src/context/engine.mjs +148 -0
  138. package/src/context/injections.mjs +26 -0
  139. package/src/context/project-context.mjs +20 -0
  140. package/src/context/session-status.mjs +15 -0
  141. package/src/context/shell-layers.mjs +23 -0
  142. package/src/context/system-core/base.md +60 -0
  143. package/src/context/system-core/prompts/deepseek-v4-pro.md +3 -0
  144. package/src/context/system-core/prompts/default.md +3 -0
  145. package/src/context/system-core.mjs +35 -0
  146. package/src/debug/model-context-dumper.mjs +52 -0
  147. package/src/extensions/discovery.mjs +40 -0
  148. package/src/extensions/lifecycle-adapter.mjs +210 -0
  149. package/src/extensions/lifecycle-manifest.mjs +69 -0
  150. package/src/image-gen/index.mjs +7 -0
  151. package/src/image-gen/provider.mjs +231 -0
  152. package/src/image-gen/tool.mjs +84 -0
  153. package/src/lsp/client.mjs +204 -0
  154. package/src/lsp/diagnostic-store.mjs +39 -0
  155. package/src/lsp/servers.mjs +212 -0
  156. package/src/lsp/service.mjs +65 -0
  157. package/src/main.mjs +294 -0
  158. package/src/mcp/client.mjs +195 -0
  159. package/src/mcp/config.mjs +130 -0
  160. package/src/mcp/index.mjs +48 -0
  161. package/src/mcp/tools.mjs +98 -0
  162. package/src/memory/database.mjs +219 -0
  163. package/src/memory/glossary.mjs +124 -0
  164. package/src/memory/graph/graph-cascades.mjs +109 -0
  165. package/src/memory/graph/graph-diagnostics.mjs +73 -0
  166. package/src/memory/graph/graph-path-removal.mjs +50 -0
  167. package/src/memory/graph/graph-path-utils.mjs +17 -0
  168. package/src/memory/graph/graph-primitives.mjs +103 -0
  169. package/src/memory/graph/graph-read.mjs +159 -0
  170. package/src/memory/graph.mjs +282 -0
  171. package/src/memory/markdown/markdown-delete.mjs +23 -0
  172. package/src/memory/markdown/markdown-format.mjs +128 -0
  173. package/src/memory/markdown/markdown-recall.mjs +28 -0
  174. package/src/memory/markdown/ripgrep.mjs +16 -0
  175. package/src/memory/markdown/sqlite-index.mjs +87 -0
  176. package/src/memory/markdown-store.mjs +286 -0
  177. package/src/memory/markdown-tools.mjs +103 -0
  178. package/src/memory/search.mjs +142 -0
  179. package/src/memory/snapshot.mjs +86 -0
  180. package/src/memory/system-views.mjs +120 -0
  181. package/src/memory/tools.mjs +282 -0
  182. package/src/notification/desktop-notifier.mjs +85 -0
  183. package/src/platform/open-file.mjs +28 -0
  184. package/src/provider/config-command.mjs +129 -0
  185. package/src/provider/presets.mjs +72 -0
  186. package/src/session/attachment-display.mjs +16 -0
  187. package/src/session/attachment-references.mjs +65 -0
  188. package/src/session/attachments.mjs +140 -0
  189. package/src/session/persist.mjs +1 -0
  190. package/src/session/pi-manager.mjs +34 -0
  191. package/src/session/session-utils.mjs +16 -0
  192. package/src/session/sidecar-sync.mjs +19 -0
  193. package/src/session/sidecar.mjs +68 -0
  194. package/src/session/transcript.mjs +83 -0
  195. package/src/session/tree.mjs +42 -0
  196. package/src/shell/cli-runtime.mjs +11 -0
  197. package/src/shell/hints.mjs +12 -0
  198. package/src/shell/node-pty-adapter.mjs +81 -0
  199. package/src/shell/runtime-state.mjs +126 -0
  200. package/src/shell/runtime.mjs +244 -0
  201. package/src/shell/screen-buffer.mjs +136 -0
  202. package/src/shell/tool-read.mjs +74 -0
  203. package/src/shell/tools.mjs +299 -0
  204. package/src/supergrok/actions/image-generate.mjs +60 -0
  205. package/src/supergrok/actions/search.mjs +78 -0
  206. package/src/supergrok/auth.mjs +36 -0
  207. package/src/supergrok/constants.mjs +18 -0
  208. package/src/supergrok/oauth-provider.mjs +278 -0
  209. package/src/supergrok/provider.mjs +36 -0
  210. package/src/supergrok/response.mjs +76 -0
  211. package/src/supergrok/tool.mjs +61 -0
  212. package/src/text/ansi.mjs +3 -0
  213. package/src/web/config-command.mjs +43 -0
  214. package/src/web/fetch.mjs +78 -0
  215. package/src/web/presets.mjs +16 -0
  216. package/src/web/search.mjs +83 -0
  217. package/src/web/tools.mjs +107 -0
@@ -0,0 +1,204 @@
1
+ import { spawn } from "node:child_process";
2
+ import { pathToFileURL } from "node:url";
3
+ import { extname } from "node:path";
4
+ import { readFileSync } from "node:fs";
5
+
6
+ const INITIALIZE_TIMEOUT_MS = 15000;
7
+ const TEXT_DOCUMENT_SYNC_INCREMENTAL = 2;
8
+
9
+ const LANGUAGE_IDS = {
10
+ ".js": "javascript",
11
+ ".jsx": "javascriptreact",
12
+ ".ts": "typescript",
13
+ ".tsx": "typescriptreact",
14
+ ".mjs": "javascript",
15
+ ".cjs": "javascript",
16
+ ".mts": "typescript",
17
+ ".cts": "typescript",
18
+ ".vue": "vue",
19
+ };
20
+
21
+ export class LspClient {
22
+ constructor({ serverId, command, args = [], cwd, initialization = {}, store }) {
23
+ this.serverId = serverId;
24
+ this.command = command;
25
+ this.args = args;
26
+ this.cwd = cwd;
27
+ this.initialization = initialization;
28
+ this.store = store;
29
+ this.status = "starting";
30
+ this.process = null;
31
+ this.buffer = Buffer.alloc(0);
32
+ this.nextId = 1;
33
+ this.pending = new Map();
34
+ this.documents = new Map();
35
+ this.syncKind = null;
36
+ }
37
+
38
+ async start() {
39
+ this.process = spawn(this.command, this.args, {
40
+ cwd: this.cwd,
41
+ env: process.env,
42
+ stdio: ["pipe", "pipe", "pipe"],
43
+ windowsHide: true,
44
+ });
45
+ this.process.stdout.on("data", (chunk) => this.#onData(chunk));
46
+ this.process.on("exit", () => {
47
+ this.status = "failed";
48
+ for (const pending of this.pending.values()) pending.reject(new Error("LSP exited"));
49
+ this.pending.clear();
50
+ });
51
+
52
+ const initialized = await withTimeout(this.#request("initialize", {
53
+ processId: process.pid,
54
+ rootUri: pathToFileURL(this.cwd).href,
55
+ workspaceFolders: [{ name: "workspace", uri: pathToFileURL(this.cwd).href }],
56
+ initializationOptions: this.initialization,
57
+ capabilities: {
58
+ workspace: {
59
+ configuration: true,
60
+ workspaceFolders: true,
61
+ },
62
+ textDocument: {
63
+ synchronization: {
64
+ didOpen: true,
65
+ didChange: true,
66
+ },
67
+ publishDiagnostics: {},
68
+ },
69
+ },
70
+ }), INITIALIZE_TIMEOUT_MS);
71
+ this.syncKind = getSyncKind(initialized?.capabilities);
72
+ this.#notify("initialized", {});
73
+ this.status = "ready";
74
+ }
75
+
76
+ touchFile(path) {
77
+ if (this.status === "failed") return;
78
+ const text = readFileSync(path, "utf8");
79
+ const uri = pathToFileURL(path).href;
80
+ const existing = this.documents.get(path);
81
+ this.status = "busy";
82
+ if (existing) {
83
+ const version = existing.version + 1;
84
+ this.documents.set(path, { version, text });
85
+ this.#notify("textDocument/didChange", {
86
+ textDocument: { uri, version },
87
+ contentChanges: this.syncKind === TEXT_DOCUMENT_SYNC_INCREMENTAL
88
+ ? [{ range: { start: { line: 0, character: 0 }, end: endPosition(existing.text) }, text }]
89
+ : [{ text }],
90
+ });
91
+ return;
92
+ }
93
+ this.documents.set(path, { version: 0, text });
94
+ this.#notify("textDocument/didOpen", {
95
+ textDocument: {
96
+ uri,
97
+ languageId: LANGUAGE_IDS[extname(path).toLowerCase()] ?? "plaintext",
98
+ version: 0,
99
+ text,
100
+ },
101
+ });
102
+ }
103
+
104
+ async shutdown() {
105
+ if (!this.process) return;
106
+ try { this.#notify("shutdown", null); } catch {}
107
+ this.process.kill();
108
+ }
109
+
110
+ #onData(chunk) {
111
+ this.buffer = Buffer.concat([this.buffer, chunk]);
112
+ for (;;) {
113
+ const headerEnd = this.buffer.indexOf("\r\n\r\n");
114
+ if (headerEnd === -1) return;
115
+ const header = this.buffer.slice(0, headerEnd).toString("utf8");
116
+ const match = header.match(/Content-Length: (\d+)/i);
117
+ if (!match) {
118
+ this.buffer = this.buffer.slice(headerEnd + 4);
119
+ continue;
120
+ }
121
+ const length = Number(match[1]);
122
+ const messageStart = headerEnd + 4;
123
+ const messageEnd = messageStart + length;
124
+ if (this.buffer.length < messageEnd) return;
125
+ const raw = this.buffer.slice(messageStart, messageEnd).toString("utf8");
126
+ this.buffer = this.buffer.slice(messageEnd);
127
+ try {
128
+ this.#handleMessage(JSON.parse(raw));
129
+ } catch {}
130
+ }
131
+ }
132
+
133
+ #handleMessage(message) {
134
+ if (message.id !== undefined && this.pending.has(message.id)) {
135
+ const pending = this.pending.get(message.id);
136
+ this.pending.delete(message.id);
137
+ if (message.error) pending.reject(new Error(message.error.message ?? "LSP request failed"));
138
+ else pending.resolve(message.result);
139
+ return;
140
+ }
141
+
142
+ if (message.method === "textDocument/publishDiagnostics") {
143
+ this.store.replaceFile({
144
+ serverId: this.serverId,
145
+ uri: message.params?.uri,
146
+ diagnostics: message.params?.diagnostics ?? [],
147
+ });
148
+ this.status = "idle";
149
+ return;
150
+ }
151
+
152
+ if (message.id !== undefined && message.method) {
153
+ this.#send({ jsonrpc: "2.0", id: message.id, result: this.#requestResult(message.method) });
154
+ }
155
+ }
156
+
157
+ #requestResult(method) {
158
+ if (method === "workspace/configuration") return [];
159
+ if (method === "workspace/workspaceFolders") return [{ name: "workspace", uri: pathToFileURL(this.cwd).href }];
160
+ if (method === "window/workDoneProgress/create") return null;
161
+ if (method === "client/registerCapability") return null;
162
+ if (method === "client/unregisterCapability") return null;
163
+ return null;
164
+ }
165
+
166
+ #request(method, params) {
167
+ const id = this.nextId++;
168
+ const promise = new Promise((resolve, reject) => {
169
+ this.pending.set(id, { resolve, reject });
170
+ });
171
+ this.#send({ jsonrpc: "2.0", id, method, params });
172
+ return promise;
173
+ }
174
+
175
+ #notify(method, params) {
176
+ this.#send({ jsonrpc: "2.0", method, params });
177
+ }
178
+
179
+ #send(message) {
180
+ const body = JSON.stringify(message);
181
+ this.process.stdin.write(`Content-Length: ${Buffer.byteLength(body, "utf8")}\r\n\r\n${body}`);
182
+ }
183
+ }
184
+
185
+ function withTimeout(promise, ms) {
186
+ let timer;
187
+ return Promise.race([
188
+ promise,
189
+ new Promise((_, reject) => {
190
+ timer = setTimeout(() => reject(new Error("LSP initialize timed out")), ms);
191
+ }),
192
+ ]).finally(() => clearTimeout(timer));
193
+ }
194
+
195
+ function getSyncKind(capabilities) {
196
+ const sync = capabilities?.textDocumentSync;
197
+ if (typeof sync === "number") return sync;
198
+ return sync?.change ?? null;
199
+ }
200
+
201
+ function endPosition(text) {
202
+ const lines = text.split(/\r\n|\r|\n/);
203
+ return { line: lines.length - 1, character: lines.at(-1)?.length ?? 0 };
204
+ }
@@ -0,0 +1,39 @@
1
+ import { fileURLToPath } from "node:url";
2
+
3
+ export class LspDiagnosticStore {
4
+ constructor() {
5
+ this.byPath = new Map();
6
+ }
7
+
8
+ replaceFile({ serverId, uri, diagnostics = [] }) {
9
+ const path = uriToPath(uri);
10
+ if (!path) return;
11
+ const normalized = diagnostics.map((diagnostic) => ({
12
+ ...diagnostic,
13
+ serverId,
14
+ path,
15
+ }));
16
+ this.byPath.set(path, {
17
+ path,
18
+ updatedAt: Date.now(),
19
+ diagnostics: normalized,
20
+ });
21
+ }
22
+
23
+ snapshot() {
24
+ const diagnostics = [];
25
+ for (const entry of this.byPath.values()) {
26
+ diagnostics.push(...entry.diagnostics);
27
+ }
28
+ return diagnostics;
29
+ }
30
+ }
31
+
32
+ function uriToPath(uri) {
33
+ if (typeof uri !== "string" || !uri.startsWith("file://")) return null;
34
+ try {
35
+ return fileURLToPath(uri);
36
+ } catch {
37
+ return null;
38
+ }
39
+ }
@@ -0,0 +1,212 @@
1
+ import { existsSync } from "node:fs";
2
+ import { dirname, join, resolve } from "node:path";
3
+ import { createRequire } from "node:module";
4
+
5
+ const NODE_ROOT_MARKERS = ["package-lock.json", "bun.lockb", "bun.lock", "pnpm-lock.yaml", "yarn.lock", "package.json"];
6
+
7
+ const LSP_SERVERS = [
8
+ {
9
+ id: "vue",
10
+ extensions: [".vue"],
11
+ rootMarkers: NODE_ROOT_MARKERS,
12
+ command: ["vue-language-server"],
13
+ args: ["--stdio"],
14
+ initialization: () => ({}),
15
+ },
16
+ {
17
+ id: "typescript",
18
+ extensions: [".ts", ".tsx", ".js", ".jsx", ".mjs", ".cjs", ".mts", ".cts"],
19
+ rootMarkers: NODE_ROOT_MARKERS,
20
+ command: ["typescript-language-server"],
21
+ args: ["--stdio"],
22
+ initialization: ({ root, workspaceRoot }) => {
23
+ const tsserver = resolveModule("typescript/lib/tsserver.js", workspaceRoot) ?? resolveModule("typescript/lib/tsserver.js", root);
24
+ if (!tsserver) return null;
25
+ return { tsserver: { path: tsserver } };
26
+ },
27
+ },
28
+ {
29
+ id: "python",
30
+ extensions: [".py", ".pyi"],
31
+ rootMarkers: ["pyproject.toml", "setup.py", "setup.cfg", "requirements.txt", "Pipfile", "pyrightconfig.json"],
32
+ command: ["pyright-langserver"],
33
+ args: ["--stdio"],
34
+ },
35
+ {
36
+ id: "go",
37
+ extensions: [".go"],
38
+ rootMarkers: ["go.work", "go.mod", "go.sum"],
39
+ command: ["gopls"],
40
+ args: [],
41
+ },
42
+ {
43
+ id: "rust",
44
+ extensions: [".rs"],
45
+ rootMarkers: ["Cargo.toml", "Cargo.lock"],
46
+ command: ["rust-analyzer"],
47
+ args: [],
48
+ },
49
+ {
50
+ id: "clangd",
51
+ extensions: [".c", ".cpp", ".cc", ".cxx", ".c++", ".h", ".hpp", ".hh", ".hxx", ".h++"],
52
+ rootMarkers: ["compile_commands.json", "compile_flags.txt", ".clangd"],
53
+ command: ["clangd"],
54
+ args: ["--background-index", "--clang-tidy"],
55
+ },
56
+ {
57
+ id: "svelte",
58
+ extensions: [".svelte"],
59
+ rootMarkers: NODE_ROOT_MARKERS,
60
+ command: ["svelteserver", "svelte-language-server"],
61
+ args: ["--stdio"],
62
+ initialization: () => ({}),
63
+ },
64
+ {
65
+ id: "astro",
66
+ extensions: [".astro"],
67
+ rootMarkers: NODE_ROOT_MARKERS,
68
+ command: ["astro-ls", "@astrojs/language-server"],
69
+ args: ["--stdio"],
70
+ initialization: ({ root, workspaceRoot }) => {
71
+ const tsserver = resolveModule("typescript/lib/tsserver.js", workspaceRoot) ?? resolveModule("typescript/lib/tsserver.js", root);
72
+ if (!tsserver) return null;
73
+ return { typescript: { tsdk: dirname(tsserver) } };
74
+ },
75
+ },
76
+ {
77
+ id: "yaml",
78
+ extensions: [".yaml", ".yml"],
79
+ rootMarkers: NODE_ROOT_MARKERS,
80
+ command: ["yaml-language-server"],
81
+ args: ["--stdio"],
82
+ },
83
+ {
84
+ id: "bash",
85
+ extensions: [".sh", ".bash", ".zsh", ".ksh"],
86
+ rootMarkers: [],
87
+ command: ["bash-language-server"],
88
+ args: ["start"],
89
+ },
90
+ {
91
+ id: "lua",
92
+ extensions: [".lua"],
93
+ rootMarkers: [".luarc.json", ".luarc.jsonc", ".luacheckrc", ".stylua.toml", "stylua.toml", "selene.toml", "selene.yml"],
94
+ command: ["lua-language-server"],
95
+ args: [],
96
+ },
97
+ {
98
+ id: "zig",
99
+ extensions: [".zig", ".zon"],
100
+ rootMarkers: ["build.zig"],
101
+ command: ["zls"],
102
+ args: [],
103
+ },
104
+ {
105
+ id: "dart",
106
+ extensions: [".dart"],
107
+ rootMarkers: ["pubspec.yaml", "analysis_options.yaml"],
108
+ command: ["dart"],
109
+ args: ["language-server", "--lsp"],
110
+ },
111
+ {
112
+ id: "php",
113
+ extensions: [".php"],
114
+ rootMarkers: ["composer.json", "composer.lock", ".php-version"],
115
+ command: ["intelephense"],
116
+ args: ["--stdio"],
117
+ initialization: () => ({ telemetry: { enabled: false } }),
118
+ },
119
+ {
120
+ id: "prisma",
121
+ extensions: [".prisma"],
122
+ rootMarkers: ["schema.prisma", "prisma/schema.prisma", "prisma"],
123
+ command: ["prisma"],
124
+ args: ["language-server"],
125
+ },
126
+ ];
127
+
128
+ export function resolveLspServer({ filePath, workspaceRoot }) {
129
+ const ext = extensionOf(filePath);
130
+ const def = LSP_SERVERS.find((server) => server.extensions.includes(ext));
131
+ if (!def) return null;
132
+
133
+ const root = def.rootMarkers.length > 0
134
+ ? findNearestRoot(dirname(filePath), workspaceRoot, def.rootMarkers) ?? workspaceRoot
135
+ : workspaceRoot;
136
+ const command = findCommand(def.command, { root, workspaceRoot });
137
+ if (!command) return null;
138
+ const initialization = def.initialization?.({ root, workspaceRoot }) ?? {};
139
+ if (initialization === null) return null;
140
+ return {
141
+ id: def.id,
142
+ command,
143
+ args: def.args,
144
+ root,
145
+ initialization,
146
+ };
147
+ }
148
+
149
+ export function listLspServerDefinitions() {
150
+ return LSP_SERVERS.map(({ id, extensions, rootMarkers, command, args }) => ({ id, extensions, rootMarkers, command, args }));
151
+ }
152
+
153
+ function findCommand(names, { root, workspaceRoot }) {
154
+ for (const name of names) {
155
+ const hit = findBin(name, { root, workspaceRoot });
156
+ if (hit) return hit;
157
+ }
158
+ return null;
159
+ }
160
+
161
+ function findBin(name, { root, workspaceRoot }) {
162
+ const names = platformCommandNames(name);
163
+ for (const base of [root, workspaceRoot]) {
164
+ for (const bin of names) {
165
+ const candidate = join(base, "node_modules", ".bin", bin);
166
+ if (existsSync(candidate)) return candidate;
167
+ }
168
+ }
169
+ return findOnPath(names);
170
+ }
171
+
172
+ function platformCommandNames(name) {
173
+ if (process.platform !== "win32") return [name];
174
+ return name.includes("/") ? [name] : [`${name}.cmd`, `${name}.exe`, name];
175
+ }
176
+
177
+ function findOnPath(names) {
178
+ const dirs = (process.env.PATH ?? "").split(process.platform === "win32" ? ";" : ":").filter(Boolean);
179
+ for (const dir of dirs) {
180
+ for (const name of names) {
181
+ const candidate = join(dir, name);
182
+ if (existsSync(candidate)) return candidate;
183
+ }
184
+ }
185
+ return null;
186
+ }
187
+
188
+ function resolveModule(id, base) {
189
+ try {
190
+ return createRequire(join(base, "package.json")).resolve(id);
191
+ } catch {
192
+ return null;
193
+ }
194
+ }
195
+
196
+ function findNearestRoot(start, stop, markers) {
197
+ let dir = resolve(start);
198
+ const boundary = resolve(stop);
199
+ for (;;) {
200
+ for (const marker of markers) {
201
+ if (existsSync(join(dir, marker))) return dir;
202
+ }
203
+ if (dir === boundary || dirname(dir) === dir) return null;
204
+ dir = dirname(dir);
205
+ }
206
+ }
207
+
208
+ function extensionOf(path) {
209
+ const lower = path.toLowerCase();
210
+ const dot = lower.lastIndexOf(".");
211
+ return dot === -1 ? "" : lower.slice(dot);
212
+ }
@@ -0,0 +1,65 @@
1
+ import { LspClient } from "./client.mjs";
2
+ import { LspDiagnosticStore } from "./diagnostic-store.mjs";
3
+ import { resolveLspServer } from "./servers.mjs";
4
+
5
+ export class LspService {
6
+ constructor({ cwd }) {
7
+ this.cwd = cwd;
8
+ this.store = new LspDiagnosticStore();
9
+ this.clients = new Map();
10
+ this.spawning = new Map();
11
+ }
12
+
13
+ touchFile(path) {
14
+ const server = resolveLspServer({ filePath: path, workspaceRoot: this.cwd });
15
+ if (!server) return;
16
+ const key = `${server.id}:${server.root}`;
17
+ const existing = this.clients.get(key);
18
+ if (existing) {
19
+ existing.touchFile(path);
20
+ return;
21
+ }
22
+ if (this.spawning.has(key)) {
23
+ this.spawning.get(key).then((client) => client?.touchFile(path)).catch(() => {});
24
+ return;
25
+ }
26
+ const task = this.#startClient(server, key).then((client) => {
27
+ client?.touchFile(path);
28
+ return client;
29
+ });
30
+ this.spawning.set(key, task);
31
+ task.finally(() => {
32
+ if (this.spawning.get(key) === task) this.spawning.delete(key);
33
+ }).catch(() => {});
34
+ }
35
+
36
+ snapshot() {
37
+ const diagnostics = this.store.snapshot();
38
+ const statuses = [...this.clients.values()].map((client) => client.status);
39
+ const status = statuses.includes("busy") ? "busy" : statuses.includes("starting") ? "starting" : statuses.includes("failed") ? "failed" : statuses.length > 0 ? "idle" : "";
40
+ return { status, diagnostics };
41
+ }
42
+
43
+ async dispose() {
44
+ await Promise.all([...this.clients.values()].map((client) => client.shutdown().catch(() => {})));
45
+ }
46
+
47
+ async #startClient(server, key) {
48
+ const client = new LspClient({
49
+ serverId: server.id,
50
+ command: server.command,
51
+ args: server.args,
52
+ cwd: server.root,
53
+ initialization: server.initialization,
54
+ store: this.store,
55
+ });
56
+ try {
57
+ await client.start();
58
+ this.clients.set(key, client);
59
+ return client;
60
+ } catch {
61
+ client.status = "failed";
62
+ return null;
63
+ }
64
+ }
65
+ }