@floydous/pi-agent-kernel 0.4.3 → 0.6.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 (65) hide show
  1. package/AGENT_KERNEL_SYS_PROMPT.md +15 -12
  2. package/CHANGELOG.md +87 -0
  3. package/README.md +6 -54
  4. package/config.example.toml +1 -8
  5. package/docs/README.md +1 -2
  6. package/docs/architecture.md +2 -3
  7. package/docs/configuration.md +11 -10
  8. package/docs/editing-and-verification.md +21 -8
  9. package/docs/podman-linux-checks.md +24 -0
  10. package/docs/release-discipline.md +10 -0
  11. package/docs/retrieval.md +21 -23
  12. package/docs/testing.md +3 -3
  13. package/grammars/tree-sitter-html.wasm +0 -0
  14. package/package.json +5 -3
  15. package/src/config/agent_kernel_command.ts +60 -625
  16. package/src/config/kernel_config.ts +53 -69
  17. package/src/config/toml.ts +6 -1
  18. package/src/editing/auto_heal.ts +1 -1
  19. package/src/editing/patch.ts +75 -37
  20. package/src/editing/post_edit_verification.ts +20 -14
  21. package/src/editing/smart_anchor.ts +9 -6
  22. package/src/editing/syntax-verify.ts +79 -222
  23. package/src/index.ts +36 -453
  24. package/src/retrieval/html_chunker.ts +526 -0
  25. package/src/retrieval/search_bm25.ts +39 -28
  26. package/src/retrieval/search_chunker.ts +69 -7
  27. package/src/retrieval/search_config.ts +21 -40
  28. package/src/retrieval/search_embedder.ts +14 -31
  29. package/src/retrieval/search_index.ts +338 -129
  30. package/src/retrieval/search_modal.ts +1208 -222
  31. package/src/retrieval/{repomap.ts → tag_extractor.ts} +6 -293
  32. package/src/retrieval/tree_sitter_engine.ts +171 -36
  33. package/src/retrieval/workspace_walker.ts +78 -2
  34. package/src/safety/atomic_write.ts +1 -1
  35. package/src/safety/epistemic_guard.ts +105 -31
  36. package/src/safety/host_environment.ts +180 -0
  37. package/src/safety/output_clamper.ts +97 -27
  38. package/src/tools/code_search_output.ts +11 -2
  39. package/src/tools/code_search_tool.ts +25 -13
  40. package/src/tools/context.ts +11 -10
  41. package/src/tools/edit_tool.ts +243 -154
  42. package/src/tools/read_tool.ts +285 -214
  43. package/src/tools/stream_reader.ts +206 -0
  44. package/src/ui/footer.ts +7 -7
  45. package/docs/lsp.md +0 -31
  46. package/src/lsp/adapters/base.ts +0 -42
  47. package/src/lsp/adapters/index.ts +0 -21
  48. package/src/lsp/adapters/python.ts +0 -37
  49. package/src/lsp/adapters/rust.ts +0 -35
  50. package/src/lsp/adapters/typescript.ts +0 -40
  51. package/src/lsp/index.ts +0 -13
  52. package/src/lsp/lsp_client.ts +0 -765
  53. package/src/lsp/lsp_detector.ts +0 -52
  54. package/src/lsp/lsp_formatter.ts +0 -323
  55. package/src/lsp/lsp_installer.ts +0 -320
  56. package/src/lsp/lsp_manager.ts +0 -202
  57. package/src/lsp/lsp_modal.ts +0 -675
  58. package/src/lsp/lsp_registry.ts +0 -600
  59. package/src/lsp/lsp_types.ts +0 -143
  60. package/src/retrieval/ast_search.ts +0 -999
  61. package/src/retrieval/symbol_reader.ts +0 -228
  62. package/src/tools/ast_search_formatter.ts +0 -161
  63. package/src/tools/ast_search_tool.ts +0 -146
  64. package/src/tools/lsp_tool.ts +0 -794
  65. package/src/tools/repo_map_tool.ts +0 -56
@@ -1,675 +0,0 @@
1
- import type { Theme } from "@earendil-works/pi-coding-agent";
2
- import { type Focusable, matchesKey, truncateToWidth, visibleWidth } from "../ui/tui_utils";
3
- import { LspManager } from "./lsp_manager";
4
- import {
5
- LSP_SERVERS,
6
- findExecutable,
7
- isServerDisabled,
8
- toggleServerEnabled,
9
- clearExecutableCache,
10
- } from "./lsp_registry";
11
- import { installLanguageServer } from "./lsp_installer";
12
- import { loadKernelConfig } from "../config";
13
-
14
- export interface LspModalResult {
15
- action: "install" | "stop" | "close";
16
- target?: string;
17
- installTargets?: string[];
18
- }
19
-
20
- interface ServerRowItem {
21
- type: "server";
22
- id: string;
23
- label: string;
24
- binDesc: string;
25
- }
26
-
27
- interface ActionRowItem {
28
- type: "action";
29
- id: "stop-all" | "close";
30
- label: string;
31
- desc: string;
32
- }
33
-
34
- type ModalItem = ServerRowItem | ActionRowItem;
35
-
36
- const SERVER_ITEMS: ServerRowItem[] = [
37
- { type: "server", id: "typescript", label: "TypeScript / JS", binDesc: "vtsls, biome, oxc" },
38
- { type: "server", id: "python", label: "Python", binDesc: "ty, ruff, pyright" },
39
- { type: "server", id: "rust", label: "Rust", binDesc: "rust-analyzer" },
40
- { type: "server", id: "go", label: "Go", binDesc: "gopls" },
41
- { type: "server", id: "cpp", label: "C / C++", binDesc: "clangd" },
42
- { type: "server", id: "zig", label: "Zig", binDesc: "zls" },
43
- { type: "server", id: "html", label: "HTML / Web", binDesc: "superhtml (Zig), vscode" },
44
- { type: "server", id: "json", label: "JSON / Schema", binDesc: "biome (Rust), vscode" },
45
- { type: "server", id: "markdown", label: "Markdown", binDesc: "marksman, oxide" },
46
- { type: "server", id: "toml", label: "TOML", binDesc: "taplo (Rust)" },
47
- { type: "server", id: "typst", label: "Typst", binDesc: "tinymist (Rust)" },
48
- { type: "server", id: "sql", label: "SQL", binDesc: "sqruff (Rust), sqls" },
49
- { type: "server", id: "lua", label: "Lua", binDesc: "lua-language-server" },
50
- { type: "server", id: "shell", label: "Shell / Bash", binDesc: "bash-language-server" },
51
- { type: "server", id: "yaml", label: "YAML", binDesc: "yaml-language-server" },
52
- { type: "server", id: "csharp", label: "C# / .NET", binDesc: "csharp-ls, OmniSharp" },
53
- { type: "server", id: "ruby", label: "Ruby", binDesc: "ruby-lsp, solargraph" },
54
- { type: "server", id: "java", label: "Java", binDesc: "jdtls" },
55
- { type: "server", id: "kotlin", label: "Kotlin", binDesc: "kotlin-lsp, fwcd" },
56
- { type: "server", id: "swift", label: "Swift", binDesc: "sourcekit-lsp" },
57
- { type: "server", id: "php", label: "PHP", binDesc: "intelephense" },
58
- { type: "server", id: "docker", label: "Dockerfile", binDesc: "docker-langserver" },
59
- ];
60
-
61
- const ACTION_ITEMS: ActionRowItem[] = [
62
- { type: "action", id: "stop-all", label: "Stop All Active Daemons", desc: "Terminate all running subprocesses" },
63
- { type: "action", id: "close", label: "Close Panel", desc: "Dismiss this dialog and start queued downloads" },
64
- ];
65
-
66
- function checkServerInstalled(id: string): { installed: boolean; activeBin?: string; binPath?: string } {
67
- const cfg = LSP_SERVERS[id];
68
- if (!cfg) return { installed: false };
69
- for (const cmd of cfg.commands) {
70
- const binPath = findExecutable(cmd.bin);
71
- if (binPath) {
72
- return { installed: true, activeBin: cmd.bin, binPath };
73
- }
74
- }
75
- return { installed: false, activeBin: cfg.commands[0]?.bin };
76
- }
77
-
78
- export class LspControlModal implements Focusable {
79
- focused = false;
80
-
81
- private tui: any;
82
- private lspMgr: LspManager;
83
- private theme: Theme;
84
- private done: (result: LspModalResult | undefined) => void;
85
- private selectedIndex = 0;
86
- private allItems: ModalItem[];
87
- private queuedInstalls = new Set<string>();
88
-
89
- constructor(
90
- tui: any,
91
- lspMgr: LspManager,
92
- theme: Theme,
93
- done: (result: LspModalResult | undefined) => void
94
- ) {
95
- this.tui = tui;
96
- this.lspMgr = lspMgr;
97
- this.theme = theme;
98
- this.done = done;
99
- this.allItems = [...SERVER_ITEMS, ...ACTION_ITEMS];
100
- }
101
-
102
- private finish(action: "install" | "stop" | "close", target?: string) {
103
- const installTargets = Array.from(this.queuedInstalls);
104
- this.done({
105
- action,
106
- target,
107
- installTargets: installTargets.length > 0 ? installTargets : target ? [target] : undefined,
108
- });
109
- }
110
-
111
- async handleInput(data: string): Promise<void> {
112
- if (matchesKey(data, "escape") || matchesKey(data, "q") || matchesKey(data, "ctrl+c")) {
113
- this.finish("close");
114
- return;
115
- }
116
-
117
- if (matchesKey(data, "up") || matchesKey(data, "k")) {
118
- this.selectedIndex = (this.selectedIndex - 1 + this.allItems.length) % this.allItems.length;
119
- this.tui?.requestRender?.();
120
- return;
121
- }
122
-
123
- if (matchesKey(data, "down") || matchesKey(data, "j")) {
124
- this.selectedIndex = (this.selectedIndex + 1) % this.allItems.length;
125
- this.tui?.requestRender?.();
126
- return;
127
- }
128
-
129
- if (matchesKey(data, "s")) {
130
- await this.lspMgr.stopAll();
131
- this.tui?.requestRender?.();
132
- return;
133
- }
134
-
135
- if (matchesKey(data, "r")) {
136
- clearExecutableCache();
137
- this.tui?.requestRender?.();
138
- return;
139
- }
140
-
141
- // Space key: Toggle on/off if installed, or queue/unqueue download if not installed
142
- if (matchesKey(data, "space")) {
143
- const item = this.allItems[this.selectedIndex];
144
- if (!item) return;
145
-
146
- if (item.type === "server") {
147
- const status = checkServerInstalled(item.id);
148
- if (!status.installed) {
149
- // Toggle queued install
150
- if (this.queuedInstalls.has(item.id)) {
151
- this.queuedInstalls.delete(item.id);
152
- } else {
153
- this.queuedInstalls.add(item.id);
154
- }
155
- this.tui?.requestRender?.();
156
- return;
157
- } else {
158
- const nowEnabled = toggleServerEnabled(item.id);
159
- if (!nowEnabled) {
160
- await this.lspMgr.stopLanguage(item.id);
161
- }
162
- this.tui?.requestRender?.();
163
- return;
164
- }
165
- } else if (item.type === "action") {
166
- if (item.id === "stop-all") {
167
- await this.lspMgr.stopAll();
168
- this.tui?.requestRender?.();
169
- return;
170
- } else if (item.id === "close") {
171
- this.finish("close");
172
- return;
173
- }
174
- }
175
- }
176
-
177
- // Enter / Return key
178
- if (matchesKey(data, "return")) {
179
- const item = this.allItems[this.selectedIndex];
180
- if (!item) return;
181
-
182
- if (item.type === "server") {
183
- const status = checkServerInstalled(item.id);
184
- if (!status.installed) {
185
- if (this.queuedInstalls.has(item.id)) {
186
- this.queuedInstalls.delete(item.id);
187
- } else {
188
- this.queuedInstalls.add(item.id);
189
- }
190
- this.tui?.requestRender?.();
191
- } else {
192
- const nowEnabled = toggleServerEnabled(item.id);
193
- if (!nowEnabled) {
194
- await this.lspMgr.stopLanguage(item.id);
195
- }
196
- this.tui?.requestRender?.();
197
- }
198
- return;
199
- } else if (item.type === "action") {
200
- if (item.id === "stop-all") {
201
- await this.lspMgr.stopAll();
202
- this.finish("stop");
203
- } else if (item.id === "close") {
204
- this.finish("close");
205
- }
206
- return;
207
- }
208
- }
209
- }
210
-
211
- render(width: number): string[] {
212
- const cardWidth = Math.min(104, Math.max(20, width - 4));
213
- const innerW = Math.max(1, cardWidth - 2);
214
-
215
- const pad = (s: string, len: number) => {
216
- const safe = truncateToWidth(s, len, "");
217
- return safe + " ".repeat(Math.max(0, len - visibleWidth(safe)));
218
- };
219
-
220
- // Theme Palette (Pastel TrueColor & Dark Charcoal Matte)
221
- const reset = "\x1b[0m";
222
- const bgCard = "\x1b[48;5;234m"; // Dark charcoal matte card background
223
-
224
- const fgBorder = "\x1b[38;5;240m"; // Refined dark slate/gray border (#585858)
225
- const fgTitle = "\x1b[1;38;5;255m"; // Pure crisp white bold title
226
- const fgSubHeader = "\x1b[1;38;5;250m"; // Light silver bold for section headers (#bcbcbc)
227
- const fgActiveCursor = "\x1b[1;38;5;255m"; // Pure white highlight for active cursor
228
- const fgSelected = "\x1b[38;5;252m"; // Crisp light gray for labels
229
- const fgStatusReady = "\x1b[38;2;155;210;170m"; // Pastel Mint (#9bd2aa) for enabled / installed [x]
230
- const fgDownload = "\x1b[38;2;230;190;130m"; // Soft Pastel Amber/Sand (#e6be82) for (download) indicator
231
- const fgQueue = "\x1b[1;38;2;140;210;240m"; // Pastel Sky Cyan (#8cd2f0) for queued downloads
232
- const fgDisabled = "\x1b[38;5;242m"; // Dim slate gray for [ ] disabled
233
- const fgValue = "\x1b[38;5;252m"; // Crisp silver-gray for metrics
234
- const fgNotice = "\x1b[38;5;215m"; // Soft muted amber for alerts
235
- const fgMuted = "\x1b[38;5;245m"; // Mid slate gray for secondary labels
236
- const fgDim = "\x1b[38;5;239m"; // Dimmed charcoal gray for details
237
-
238
- const row = (content: string) =>
239
- bgCard + fgBorder + "│" + reset + bgCard + pad(content, innerW) + fgBorder + "│" + reset;
240
-
241
- const activeClients = this.lspMgr.getStatus();
242
- const cardLines: string[] = [];
243
-
244
- // Header
245
- cardLines.push(bgCard + fgBorder + `╭${"─".repeat(innerW)}╮` + reset);
246
- cardLines.push(row(` ${fgTitle}Language Server Protocol (LSP)${reset}${bgCard} ${fgDim}(Tree-sitter AST & Stdio Daemons)`));
247
- cardLines.push(bgCard + fgBorder + `├${"─".repeat(innerW)}┤` + reset);
248
-
249
- // Active Daemons Status Section
250
- cardLines.push(row(` ${fgSubHeader}Active LSP Daemons (${fgValue}${activeClients.length}${fgSubHeader}):`));
251
- if (activeClients.length === 0) {
252
- cardLines.push(row(` ${fgDim}None (LSP daemons spawn automatically on demand when queried)`));
253
- } else {
254
- for (const c of activeClients) {
255
- const stateColor = c.state === "ready" ? fgStatusReady : fgNotice;
256
- cardLines.push(
257
- row(
258
- ` ${fgTitle}• ${c.languageId.toUpperCase()}${fgMuted} (State: ${stateColor}${c.state}${fgMuted}, Idle: ${fgValue}${c.idleSeconds}s${fgMuted}) ${fgDim}${c.rootDir}`
259
- )
260
- );
261
- }
262
- }
263
-
264
- cardLines.push(bgCard + fgBorder + `├${"─".repeat(innerW)}┤` + reset);
265
-
266
- // Section title with queued counter if any
267
- if (this.queuedInstalls.size > 0) {
268
- cardLines.push(row(` ${fgSubHeader}Available Server Binaries ${fgQueue}[${this.queuedInstalls.size} queued for download upon exit]${reset}${bgCard}:`));
269
- } else {
270
- cardLines.push(row(` ${fgSubHeader}Available Server Binaries:`));
271
- }
272
-
273
- // Terminal height resolution for dynamic scrolling viewport
274
- const termHeight =
275
- this.tui?.terminal?.rows ||
276
- (process.stdout && process.stdout.rows ? process.stdout.rows : 24);
277
-
278
- const maxVisibleServers = Math.max(6, Math.min(10, termHeight - 16));
279
- const numServers = SERVER_ITEMS.length;
280
-
281
- let scrollOffset = 0;
282
- if (this.selectedIndex < numServers) {
283
- if (this.selectedIndex >= scrollOffset + maxVisibleServers) {
284
- scrollOffset = this.selectedIndex - maxVisibleServers + 1;
285
- }
286
- } else {
287
- scrollOffset = Math.max(0, numServers - maxVisibleServers);
288
- }
289
- scrollOffset = Math.max(0, Math.min(scrollOffset, numServers - maxVisibleServers));
290
-
291
- if (scrollOffset > 0) {
292
- cardLines.push(row(` ${fgDim}▲ (${scrollOffset} more servers above)`));
293
- }
294
-
295
- const visibleEnd = Math.min(numServers, scrollOffset + maxVisibleServers);
296
- for (let i = scrollOffset; i < visibleEnd; i++) {
297
- const item = SERVER_ITEMS[i];
298
- const isSelected = i === this.selectedIndex;
299
- const cursor = isSelected ? `${fgActiveCursor} > ` : " ";
300
-
301
- const status = checkServerInstalled(item.id);
302
- const isQueued = this.queuedInstalls.has(item.id);
303
- const disabled = isServerDisabled(item.id);
304
- const isEnabled = status.installed && !disabled;
305
- const isActive = activeClients.some(
306
- (c) => c.languageId.toLowerCase() === item.id.toLowerCase()
307
- );
308
-
309
- // Checkbox representation
310
- let checkbox = `${fgDisabled}[ ]${reset}${bgCard}`;
311
- if (isQueued) {
312
- checkbox = `${fgQueue}[+]${reset}${bgCard}`;
313
- } else if (isEnabled) {
314
- checkbox = `${fgStatusReady}[x]${reset}${bgCard}`;
315
- }
316
-
317
- // Server name label
318
- const nameColor = isSelected ? fgActiveCursor : isQueued ? fgQueue : isEnabled ? fgSelected : fgMuted;
319
- const nameCol = `${nameColor}${item.label.padEnd(18)}${reset}${bgCard}`;
320
-
321
- // Indicator column
322
- let indicatorCol = "";
323
- if (isQueued) {
324
- indicatorCol = `${fgQueue}(queued) ${reset}${bgCard}`;
325
- } else if (!status.installed) {
326
- indicatorCol = `${fgDownload}(download) ${reset}${bgCard}`;
327
- } else if (isActive) {
328
- indicatorCol = `${fgStatusReady}(active) ${reset}${bgCard}`;
329
- } else if (isEnabled) {
330
- indicatorCol = `${fgStatusReady}(ready) ${reset}${bgCard}`;
331
- } else {
332
- indicatorCol = `${fgDim}(off) ${reset}${bgCard}`;
333
- }
334
-
335
- // Details / binary column
336
- const binText = status.installed
337
- ? `${fgDim}${status.activeBin || item.binDesc}`
338
- : `${fgDim}${item.binDesc}`;
339
-
340
- cardLines.push(row(`${cursor}${checkbox} ${nameCol} ${indicatorCol} ${binText}`));
341
- }
342
-
343
- if (visibleEnd < numServers) {
344
- cardLines.push(row(` ${fgDim}▼ (${numServers - visibleEnd} more servers below)`));
345
- }
346
-
347
- // Utility actions section
348
- cardLines.push(bgCard + fgBorder + `├${"─".repeat(innerW)}┤` + reset);
349
- cardLines.push(row(` ${fgSubHeader}Actions & Control:`));
350
-
351
- for (let j = 0; j < ACTION_ITEMS.length; j++) {
352
- const actionIdx = numServers + j;
353
- const item = ACTION_ITEMS[j];
354
- const isSelected = actionIdx === this.selectedIndex;
355
- const cursor = isSelected ? `${fgActiveCursor} > ` : " ";
356
- const bullet = item.id === "stop-all" ? `${fgNotice}[!]` : `${fgDim}[x]`;
357
- const labelColor = isSelected ? fgActiveCursor : fgMuted;
358
-
359
- cardLines.push(
360
- row(`${cursor}${bullet} ${labelColor}${item.label.padEnd(28)}${reset}${bgCard} ${fgDim}- ${item.desc}`)
361
- );
362
- }
363
-
364
- // Footer Key Hints
365
- cardLines.push(bgCard + fgBorder + `├${"─".repeat(innerW)}┤` + reset);
366
- const footerHint = this.queuedInstalls.size > 0
367
- ? ` ${fgQueue}Space: Toggle | Esc: Start ${this.queuedInstalls.size} queued download(s) & Close`
368
- : ` ${fgDim}Space: Toggle/Queue | Enter: Select | s: Stop All | r: Rescan | Esc: Close`;
369
- cardLines.push(row(footerHint));
370
- cardLines.push(bgCard + fgBorder + `╰${"─".repeat(innerW)}╯` + reset);
371
-
372
- // Layout calculations for full-screen backdrop framing
373
- const leftPad = Math.max(0, Math.floor((width - cardWidth) / 2));
374
- const rightPad = Math.max(0, width - cardWidth - leftPad);
375
-
376
- const scrim = (len: number) =>
377
- len <= 0 ? "" : "\x1b[48;5;232m\x1b[38;5;234m" + "░".repeat(len) + reset;
378
-
379
- const shadowRight = (len: number) => {
380
- if (len <= 0) return "";
381
- return "\x1b[48;5;232m\x1b[38;5;233m▌" + reset + scrim(len - 1);
382
- };
383
-
384
- const shadowBottom = (left: number, card: number, right: number) => {
385
- return (
386
- scrim(left) +
387
- "\x1b[48;5;232m\x1b[38;5;233m" +
388
- "▀".repeat(card) +
389
- reset +
390
- scrim(right)
391
- );
392
- };
393
-
394
- const totalCardRows = cardLines.length + 1;
395
- const topPad = Math.max(0, Math.floor((termHeight - totalCardRows) / 2));
396
- const bottomPad = Math.max(0, termHeight - totalCardRows - topPad);
397
-
398
- const fullLines: string[] = [];
399
-
400
- for (let i = 0; i < topPad; i++) {
401
- fullLines.push(scrim(width));
402
- }
403
-
404
- for (let i = 0; i < cardLines.length; i++) {
405
- const line = scrim(leftPad) + cardLines[i] + shadowRight(rightPad);
406
- fullLines.push(line);
407
- }
408
-
409
- fullLines.push(shadowBottom(leftPad, cardWidth, rightPad));
410
-
411
- for (let i = 0; i < bottomPad; i++) {
412
- fullLines.push(scrim(width));
413
- }
414
-
415
- return fullLines;
416
- }
417
-
418
- invalidate(): void {}
419
- dispose(): void {}
420
- }
421
-
422
- /**
423
- * Animated ASCII Progress Modal for downloading multiple LSP servers in background
424
- */
425
- export class LspDownloadModal implements Focusable {
426
- focused = false;
427
-
428
- private tui: any;
429
- private targets: string[];
430
- private theme: Theme;
431
- private done: () => void;
432
- private spinnerIndex = 0;
433
- private spinnerTimer: NodeJS.Timeout | null = null;
434
- private isRunning = true;
435
- private isFinished = false;
436
-
437
- // Progress state per item
438
- private tasks: Array<{
439
- id: string;
440
- label: string;
441
- status: "queued" | "downloading" | "done" | "error";
442
- progressMsg: string;
443
- errorMsg?: string;
444
- binPath?: string;
445
- }> = [];
446
-
447
- private static readonly SPINNER_FRAMES = ["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"];
448
-
449
- constructor(tui: any, targets: string[], theme: Theme, done: () => void) {
450
- this.tui = tui;
451
- this.targets = targets;
452
- this.theme = theme;
453
- this.done = done;
454
-
455
- this.tasks = targets.map((id) => {
456
- const cfg = SERVER_ITEMS.find((s) => s.id.toLowerCase() === id.toLowerCase());
457
- return {
458
- id,
459
- label: cfg?.label || id,
460
- status: "queued",
461
- progressMsg: "Queued for download...",
462
- };
463
- });
464
-
465
- this.startAnimation();
466
- this.runDownloads();
467
- }
468
-
469
- private startAnimation() {
470
- if (this.spinnerTimer) return;
471
- const intervalMs = loadKernelConfig().lsp.spinner_interval_ms;
472
- this.spinnerTimer = setInterval(() => {
473
- this.spinnerIndex = (this.spinnerIndex + 1) % LspDownloadModal.SPINNER_FRAMES.length;
474
- this.tui?.requestRender?.();
475
- }, intervalMs);
476
- if (this.spinnerTimer.unref) {
477
- this.spinnerTimer.unref();
478
- }
479
- }
480
-
481
- private stopAnimation() {
482
- if (this.spinnerTimer) {
483
- clearInterval(this.spinnerTimer);
484
- this.spinnerTimer = null;
485
- }
486
- }
487
-
488
- private async runDownloads() {
489
- for (let i = 0; i < this.tasks.length; i++) {
490
- if (!this.isRunning) break;
491
- const task = this.tasks[i];
492
- task.status = "downloading";
493
- task.progressMsg = "Initializing installer...";
494
- this.tui?.requestRender?.();
495
-
496
- const res = await installLanguageServer(task.id, (msg) => {
497
- task.progressMsg = msg;
498
- this.tui?.requestRender?.();
499
- });
500
-
501
- if (res.success) {
502
- task.status = "done";
503
- task.progressMsg = res.message;
504
- task.binPath = res.binPath;
505
- } else {
506
- task.status = "error";
507
- task.progressMsg = "Failed";
508
- task.errorMsg = res.message;
509
- }
510
- this.tui?.requestRender?.();
511
- }
512
-
513
- this.isFinished = true;
514
- this.stopAnimation();
515
- this.tui?.requestRender?.();
516
-
517
- // Auto-dismiss after 2 seconds on success
518
- setTimeout(() => {
519
- if (this.isRunning) {
520
- this.dispose();
521
- this.done();
522
- }
523
- }, 2200);
524
- }
525
-
526
- async handleInput(data: string): Promise<void> {
527
- if (
528
- matchesKey(data, "escape") ||
529
- matchesKey(data, "q") ||
530
- matchesKey(data, "ctrl+c") ||
531
- matchesKey(data, "return") ||
532
- matchesKey(data, "space")
533
- ) {
534
- this.isRunning = false;
535
- this.dispose();
536
- this.done();
537
- }
538
- }
539
-
540
- render(width: number): string[] {
541
- const cardWidth = Math.min(96, Math.max(20, width - 4));
542
- const innerW = Math.max(1, cardWidth - 2);
543
-
544
- const pad = (s: string, len: number) => {
545
- const safe = truncateToWidth(s, len, "");
546
- return safe + " ".repeat(Math.max(0, len - visibleWidth(safe)));
547
- };
548
-
549
- const reset = "\x1b[0m";
550
- const bgCard = "\x1b[48;5;234m";
551
- const fgBorder = "\x1b[38;5;240m";
552
- const fgTitle = "\x1b[1;38;5;255m";
553
- const fgSubHeader = "\x1b[1;38;5;250m";
554
- const fgSpinner = "\x1b[1;38;2;140;210;240m"; // Pastel cyan
555
- const fgSuccess = "\x1b[1;38;2;155;210;170m"; // Pastel mint
556
- const fgError = "\x1b[1;38;5;203m"; // Soft coral red
557
- const fgMuted = "\x1b[38;5;245m";
558
- const fgDim = "\x1b[38;5;239m";
559
-
560
- const frame = LspDownloadModal.SPINNER_FRAMES[this.spinnerIndex];
561
- const row = (content: string) =>
562
- bgCard + fgBorder + "│" + reset + bgCard + pad(content, innerW) + fgBorder + "│" + reset;
563
-
564
- const cardLines: string[] = [];
565
-
566
- cardLines.push(bgCard + fgBorder + `╭${"─".repeat(innerW)}╮` + reset);
567
- cardLines.push(row(` ${fgTitle}LSP Package Manager & Downloader${reset}${bgCard} ${fgDim}(Zero-Dependency Install)`));
568
- cardLines.push(bgCard + fgBorder + `├${"─".repeat(innerW)}┤` + reset);
569
-
570
- const doneCount = this.tasks.filter((t) => t.status === "done").length;
571
-
572
- if (this.isFinished) {
573
- const allSuccess = this.tasks.every((t) => t.status === "done");
574
- const headerMsg = allSuccess
575
- ? `${fgSuccess}✓ All ${this.tasks.length} language server(s) successfully installed!`
576
- : `${fgSubHeader}Installation completed (${doneCount}/${this.tasks.length} succeeded):`;
577
- cardLines.push(row(` ${headerMsg}`));
578
- } else {
579
- cardLines.push(row(` ${fgSpinner}${frame} Downloading & installing ${this.tasks.length} language server(s)... (${doneCount}/${this.tasks.length} completed)`));
580
- }
581
-
582
- cardLines.push(row(``));
583
-
584
- // Render each task row
585
- for (const task of this.tasks) {
586
- let icon = `${fgDim}•`;
587
- let statusColor = fgMuted;
588
- let lineText = "";
589
-
590
- if (task.status === "queued") {
591
- icon = `${fgDim}•`;
592
- statusColor = fgDim;
593
- lineText = `${icon} ${fgSubHeader}${task.label.padEnd(18)}${statusColor}Queued`;
594
- } else if (task.status === "downloading") {
595
- icon = `${fgSpinner}${frame}`;
596
- statusColor = fgSpinner;
597
- lineText = `${icon} ${fgTitle}${task.label.padEnd(18)}${statusColor}${task.progressMsg}`;
598
- } else if (task.status === "done") {
599
- icon = `${fgSuccess}✓`;
600
- statusColor = fgSuccess;
601
- const details = task.binPath ? `(${task.binPath})` : "Installed";
602
- lineText = `${icon} ${fgTitle}${task.label.padEnd(18)}${statusColor}Ready ${fgDim}${details}`;
603
- } else if (task.status === "error") {
604
- icon = `${fgError}✗`;
605
- statusColor = fgError;
606
- lineText = `${icon} ${fgTitle}${task.label.padEnd(18)}${statusColor}${task.errorMsg || "Failed"}`;
607
- }
608
-
609
- cardLines.push(row(` ${lineText}`));
610
- }
611
-
612
- cardLines.push(row(``));
613
- cardLines.push(bgCard + fgBorder + `├${"─".repeat(innerW)}┤` + reset);
614
-
615
- const footerText = this.isFinished
616
- ? ` ${fgDim}Press any key or Enter to continue...`
617
- : ` ${fgDim}Esc: Run in Background / Cancel`;
618
- cardLines.push(row(footerText));
619
- cardLines.push(bgCard + fgBorder + `╰${"─".repeat(innerW)}╯` + reset);
620
-
621
- const termHeight =
622
- this.tui?.terminal?.rows ||
623
- (process.stdout && process.stdout.rows ? process.stdout.rows : 24);
624
-
625
- const leftPad = Math.max(0, Math.floor((width - cardWidth) / 2));
626
- const rightPad = Math.max(0, width - cardWidth - leftPad);
627
-
628
- const scrim = (len: number) =>
629
- len <= 0 ? "" : "\x1b[48;5;232m\x1b[38;5;234m" + "░".repeat(len) + reset;
630
-
631
- const shadowRight = (len: number) => {
632
- if (len <= 0) return "";
633
- return "\x1b[48;5;232m\x1b[38;5;233m▌" + reset + scrim(len - 1);
634
- };
635
-
636
- const shadowBottom = (left: number, card: number, right: number) => {
637
- return (
638
- scrim(left) +
639
- "\x1b[48;5;232m\x1b[38;5;233m" +
640
- "▀".repeat(card) +
641
- reset +
642
- scrim(right)
643
- );
644
- };
645
-
646
- const totalCardRows = cardLines.length + 1;
647
- const topPad = Math.max(0, Math.floor((termHeight - totalCardRows) / 2));
648
- const bottomPad = Math.max(0, termHeight - totalCardRows - topPad);
649
-
650
- const fullLines: string[] = [];
651
-
652
- for (let i = 0; i < topPad; i++) {
653
- fullLines.push(scrim(width));
654
- }
655
-
656
- for (let i = 0; i < cardLines.length; i++) {
657
- const line = scrim(leftPad) + cardLines[i] + shadowRight(rightPad);
658
- fullLines.push(line);
659
- }
660
-
661
- fullLines.push(shadowBottom(leftPad, cardWidth, rightPad));
662
-
663
- for (let i = 0; i < bottomPad; i++) {
664
- fullLines.push(scrim(width));
665
- }
666
-
667
- return fullLines;
668
- }
669
-
670
- invalidate(): void {}
671
- dispose(): void {
672
- this.isRunning = false;
673
- this.stopAnimation();
674
- }
675
- }