forked from Acode-Foundation/Acode
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathserverLauncher.ts
More file actions
825 lines (722 loc) · 21 KB
/
serverLauncher.ts
File metadata and controls
825 lines (722 loc) · 21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
import lspStatusBar from "components/lspStatusBar";
import toast from "components/toast";
import alert from "dialogs/alert";
import confirm from "dialogs/confirm";
import loader from "dialogs/loader";
import type {
BridgeConfig,
InstallStatus,
LauncherConfig,
LspServerDefinition,
LspServerStats,
LspServerStatsFormatted,
ManagedServerEntry,
PortInfo,
WaitOptions,
} from "./types";
const managedServers = new Map<string, ManagedServerEntry>();
const checkedCommands = new Map<string, InstallStatus>();
const pendingInstallChecks = new Map<string, Promise<boolean>>();
const announcedServers = new Set<string>();
const STATUS_PRESENT: InstallStatus = "present";
const STATUS_DECLINED: InstallStatus = "declined";
const STATUS_FAILED: InstallStatus = "failed";
const AXS_BINARY = "$PREFIX/axs";
const TERMINAL_REQUIRED_MESSAGE = strings.terminal_required_message_for_lsp;
interface LspError extends Error {
code?: string;
}
function getExecutor(): Executor {
const executor = (globalThis as unknown as { Executor?: Executor }).Executor;
if (!executor) {
throw new Error("Executor plugin is not available");
}
return executor;
}
/**
* Get the background executor
*/
function getBackgroundExecutor(): Executor {
const executor = getExecutor();
return executor.BackgroundExecutor ?? executor;
}
function joinCommand(command: string, args: string[] = []): string {
if (!Array.isArray(args)) return command;
return [command, ...args].join(" ");
}
function wrapShellCommand(command: string): string {
const script = command.trim();
const escaped = script.replace(/"/g, '\\"');
return `sh -lc "set -e; ${escaped}"`;
}
/**
* Run a quick shell command using the background executor.
*/
async function runQuickCommand(command: string): Promise<string> {
const wrapped = wrapShellCommand(command);
return getBackgroundExecutor().execute(wrapped, true);
}
/**
* Run a shell command using the foreground executor
*/
async function runForegroundCommand(command: string): Promise<string> {
const wrapped = wrapShellCommand(command);
return getExecutor().execute(wrapped, true);
}
function quoteArg(value: unknown): string {
const str = String(value ?? "");
if (!str.length) return "''";
if (/^[A-Za-z0-9_@%+=:,./-]+$/.test(str)) return str;
return `'${str.replace(/'/g, "'\\''")}'`;
}
// ============================================================================
// Auto-Port Discovery
// ============================================================================
// Cache for the filesDir path
let cachedFilesDir: string | null = null;
/**
* Get the terminal home directory from system.getFilesDir().
* This is where axs stores port files.
*/
async function getTerminalHomeDir(): Promise<string> {
if (cachedFilesDir) {
return `${cachedFilesDir}/alpine/home`;
}
const system = (
globalThis as unknown as {
system?: {
getFilesDir: (
success: (filesDir: string) => void,
error: (error: string) => void,
) => void;
};
}
).system;
if (!system?.getFilesDir) {
throw new Error("System plugin is not available");
}
return new Promise((resolve, reject) => {
system.getFilesDir(
(filesDir: string) => {
cachedFilesDir = filesDir;
resolve(`${filesDir}/alpine/home`);
},
(error: string) => reject(new Error(error)),
);
});
}
/**
* Get the port file path for a given server and session.
* Port file format: ~/.axs/lsp_ports/{serverName}_{session}
*/
async function getPortFilePath(
serverName: string,
session: string,
): Promise<string> {
const homeDir = await getTerminalHomeDir();
// Use just the binary name (not full path), mirroring axs behavior
const baseName = serverName.split("/").pop() || serverName;
return `file://${homeDir}/.axs/lsp_ports/${baseName}_${session}`;
}
/**
* Read the port from a port file using the filesystem API.
* Returns null if the file doesn't exist or contains invalid data.
*/
async function readPortFromFile(filePath: string): Promise<number | null> {
try {
// Dynamic import to get fsOperation
const { default: fsOperation } = await import("fileSystem");
const fs = fsOperation(filePath);
// Check if file exists first
const exists = await fs.exists();
if (!exists) {
return null;
}
// Read the file content as text
const content = (await fs.readFile("utf-8")) as string;
const port = Number.parseInt(content.trim(), 10);
if (!Number.isFinite(port) || port <= 0 || port > 65535) {
return null;
}
return port;
} catch {
// File doesn't exist or couldn't be read
return null;
}
}
/**
* Get the port for a running LSP server from the axs port file.
* @param serverName - The LSP server binary name (e.g., "typescript-language-server")
* @param session - Session ID for port file naming
*/
export async function getLspPort(
serverName: string,
session: string,
): Promise<PortInfo | null> {
try {
const filePath = await getPortFilePath(serverName, session);
const port = await readPortFromFile(filePath);
if (port === null) {
return null;
}
return { port, filePath, session };
} catch {
return null;
}
}
/**
* Wait for the server ready signal (when axs prints "listening on").
* The axs proxy writes the port file immediately after binding, then prints the message.
* So once the signal is received, the port file should be available.
*/
async function waitForServerReady(
serverId: string,
timeout = 10000,
): Promise<boolean> {
const deadline = Date.now() + timeout;
const pollInterval = 50;
while (Date.now() < deadline) {
if (serverReadySignals.has(serverId)) {
serverReadySignals.delete(serverId);
return true;
}
await sleep(pollInterval);
}
return false;
}
/**
* Wait for the port file to be available after server signals ready.
* This is the most efficient approach: wait for ready signal, then read port.
*/
async function waitForPort(
serverId: string,
serverName: string,
session: string,
timeout = 10000,
): Promise<PortInfo | null> {
// First, wait for the server to signal it's ready
const ready = await waitForServerReady(serverId, timeout);
if (!ready) {
console.warn(
`[LSP:${serverId}] Server did not signal ready within timeout`,
);
}
// The port file should be available now (axs writes it before printing "listening on")
// Read it directly
const portInfo = await getLspPort(serverName, session);
if (!portInfo && ready) {
// Server signaled ready but port file not found - retry a few times
for (let i = 0; i < 5; i++) {
await sleep(100);
const retryPortInfo = await getLspPort(serverName, session);
if (retryPortInfo) {
return retryPortInfo;
}
}
}
return portInfo;
}
/**
* Quick check if a server is running and connectable.
* Attempts a fast WebSocket connection test.
*/
async function checkServerAlive(url: string, timeout = 1000): Promise<boolean> {
return new Promise((resolve) => {
try {
const ws = new WebSocket(url);
const timer = setTimeout(() => {
try {
ws.close();
} catch {}
resolve(false);
}, timeout);
ws.onopen = () => {
clearTimeout(timer);
try {
ws.close();
} catch {}
resolve(true);
};
ws.onerror = () => {
clearTimeout(timer);
resolve(false);
};
ws.onclose = () => {
clearTimeout(timer);
resolve(false);
};
} catch {
resolve(false);
}
});
}
/**
* Check if we can reuse an existing server by testing the port.
* Returns the port number if the server is alive, null otherwise.
*/
export async function canReuseExistingServer(
server: LspServerDefinition,
session: string,
): Promise<number | null> {
const bridge = server.launcher?.bridge;
const serverName = bridge?.command || server.launcher?.command || server.id;
const portInfo = await getLspPort(serverName, session);
if (!portInfo) {
return null;
}
const url = `ws://127.0.0.1:${portInfo.port}/`;
const alive = await checkServerAlive(url, 1000);
if (alive) {
console.info(
`[LSP:${server.id}] Reusing existing server on port ${portInfo.port}`,
);
return portInfo.port;
}
console.info(
`[LSP:${server.id}] Found stale port file, will start new server`,
);
return null;
}
function buildAxsBridgeCommand(
bridge: BridgeConfig | undefined,
session?: string,
): string | null {
if (!bridge || bridge.kind !== "axs") return null;
const binary = bridge.command
? String(bridge.command)
: (() => {
throw new Error("Bridge requires a command to execute");
})();
const args: string[] = Array.isArray(bridge.args)
? bridge.args.map((arg) => String(arg))
: [];
// Use session ID or bridge session or server command as fallback session
const effectiveSession = session || bridge.session || binary;
const parts = [AXS_BINARY, "lsp"];
// Add --session flag for port file naming
parts.push("--session", quoteArg(effectiveSession));
// Only add --port if explicitly specified
if (
typeof bridge.port === "number" &&
bridge.port > 0 &&
bridge.port <= 65535
) {
parts.push("--port", String(bridge.port));
}
parts.push(quoteArg(binary));
if (args.length) {
parts.push("--");
args.forEach((arg) => parts.push(quoteArg(arg)));
}
return parts.join(" ");
}
function resolveStartCommand(
server: LspServerDefinition,
session?: string,
): string | null {
const launcher = server.launcher;
if (!launcher) return null;
if (launcher.startCommand) {
return Array.isArray(launcher.startCommand)
? launcher.startCommand.join(" ")
: String(launcher.startCommand);
}
if (launcher.command) {
return joinCommand(launcher.command, launcher.args);
}
if (launcher.bridge) {
return buildAxsBridgeCommand(launcher.bridge, session);
}
return null;
}
async function ensureInstalled(server: LspServerDefinition): Promise<boolean> {
const launcher = server.launcher;
if (!launcher?.checkCommand) return true;
const cacheKey = `${server.id}:${launcher.checkCommand}`;
// Return cached result if already checked
if (checkedCommands.has(cacheKey)) {
return checkedCommands.get(cacheKey) === STATUS_PRESENT;
}
// If there's already a pending check for this server, wait for it
if (pendingInstallChecks.has(cacheKey)) {
const pending = pendingInstallChecks.get(cacheKey);
if (pending) return pending;
}
// Create and track the pending promise
const checkPromise = performInstallCheck(server, launcher, cacheKey);
pendingInstallChecks.set(cacheKey, checkPromise);
try {
return await checkPromise;
} finally {
pendingInstallChecks.delete(cacheKey);
}
}
interface LoaderDialog {
show: () => void;
destroy: () => void;
}
async function performInstallCheck(
server: LspServerDefinition,
launcher: LauncherConfig,
cacheKey: string,
): Promise<boolean> {
try {
if (launcher.checkCommand) {
await runQuickCommand(launcher.checkCommand);
}
checkedCommands.set(cacheKey, STATUS_PRESENT);
return true;
} catch (error) {
if (!launcher.install) {
checkedCommands.set(cacheKey, STATUS_FAILED);
console.warn(
`LSP server ${server.id} is missing check command result and has no installer.`,
error,
);
throw error;
}
const install = launcher.install;
const displayLabel = (
server.label ||
server.id ||
"Language server"
).trim();
const promptMessage = `Install ${displayLabel} language server?`;
const shouldInstall = await confirm(
server.label || displayLabel,
promptMessage,
);
if (!shouldInstall) {
checkedCommands.set(cacheKey, STATUS_DECLINED);
return false;
}
let loadingDialog: LoaderDialog | null = null;
try {
loadingDialog = loader.create(
server.label,
`Installing ${server.label}...`,
);
loadingDialog.show();
await runForegroundCommand(install.command);
toast(`${server.label} installed`);
checkedCommands.set(cacheKey, STATUS_PRESENT);
return true;
} catch (installError) {
console.error(`Failed to install ${server.id}`, installError);
toast(strings?.error ?? "Error");
checkedCommands.set(cacheKey, STATUS_FAILED);
throw installError;
} finally {
loadingDialog?.destroy?.();
}
}
}
async function startInteractiveServer(
command: string,
serverId: string,
): Promise<string> {
const executor = getExecutor();
const callback: ExecutorCallback = (type, data) => {
if (type === "stderr") {
if (/proot warning/i.test(data)) return;
console.warn(`[LSP:${serverId}] ${data}`);
} else if (type === "stdout" && data && data.trim()) {
console.info(`[LSP:${serverId}] ${data}`);
// Detect when the axs proxy signals it's listening
if (/listening on/i.test(data)) {
signalServerReady(serverId);
}
}
};
const uuid = await executor.start(command, callback, true);
managedServers.set(serverId, {
uuid,
command,
startedAt: Date.now(),
});
return uuid;
}
function sleep(ms: number): Promise<void> {
return new Promise((resolve) => setTimeout(resolve, ms));
}
/**
* Tracks servers that have signaled they're ready (listening)
* Key: serverId, Value: timestamp when ready
*/
const serverReadySignals = new Map<string, number>();
/**
* Called when stdout contains a "listening" message from the axs proxy.
* This signals that the server is ready to accept connections.
*/
export function signalServerReady(serverId: string): void {
serverReadySignals.set(serverId, Date.now());
}
/**
* Wait for the LSP server to be ready.
*
* This function polls for a ready signal (set when stdout contains "listening")
*/
async function waitForWebSocket(
url: string,
options: WaitOptions = {},
): Promise<void> {
const {
delay = 100, // Poll interval
probeTimeout = 5000, // Max wait time
} = options;
// Extract server ID from URL (e.g., "ws://127.0.0.1:2090" -> check by port)
const portMatch = url.match(/:(\d+)/);
const port = portMatch ? portMatch[1] : null;
// Find the server ID that's starting on this port
let targetServerId: string | null = null;
const entries = Array.from(managedServers.entries());
for (const [serverId, entry] of entries) {
if (
entry.command.includes(`--port ${port}`) ||
entry.command.includes(`:${port}`)
) {
targetServerId = serverId;
break;
}
}
const deadline = Date.now() + probeTimeout;
while (Date.now() < deadline) {
// Check if we got a ready signal
if (targetServerId && serverReadySignals.has(targetServerId)) {
// Server is ready, clear the signal and return
serverReadySignals.delete(targetServerId);
return;
}
await sleep(delay);
}
// Timeout reached, proceed anyway (transport will retry if needed)
console.debug(
`[LSP] waitForWebSocket timed out for ${url}, proceeding anyway`,
);
}
export interface EnsureServerResult {
uuid: string | null;
/** Port discovered from port file (for auto-port discovery) */
discoveredPort?: number;
}
export async function ensureServerRunning(
server: LspServerDefinition,
session?: string,
): Promise<EnsureServerResult> {
const launcher = server.launcher;
if (!launcher) return { uuid: null };
// Derive session from server ID if not provided
const effectiveSession = session || server.id;
// Check if server is already running via port file (dead client detection)
const bridge = launcher.bridge;
const serverName = bridge?.command || launcher.command || server.id;
try {
const existingPort = await canReuseExistingServer(server, effectiveSession);
if (existingPort !== null) {
// Server is already running and responsive, no need to start
return { uuid: null, discoveredPort: existingPort };
}
} catch {
// Failed to check, proceed with normal startup
}
const terminal = (
globalThis as unknown as {
Terminal?: { isInstalled?: () => Promise<boolean> | boolean };
}
).Terminal;
let isTerminalInstalled = false;
try {
isTerminalInstalled = Boolean(await terminal?.isInstalled?.());
} catch {}
if (!isTerminalInstalled) {
alert(strings.error, TERMINAL_REQUIRED_MESSAGE);
const unavailable: LspError = new Error(TERMINAL_REQUIRED_MESSAGE);
unavailable.code = "LSP_SERVER_UNAVAILABLE";
throw unavailable;
}
const installed = await ensureInstalled(server);
if (!installed) {
const unavailable: LspError = new Error(
`Language server ${server.id} is not available.`,
);
unavailable.code = "LSP_SERVER_UNAVAILABLE";
throw unavailable;
}
const key = server.id;
if (managedServers.has(key)) {
const existing = managedServers.get(key);
return { uuid: existing?.uuid ?? null };
}
const command = resolveStartCommand(server, effectiveSession);
if (!command) {
return { uuid: null };
}
try {
const uuid = await startInteractiveServer(command, key);
// For auto-port discovery, wait for server ready signal then read port
let discoveredPort: number | undefined;
if (bridge && !bridge.port) {
// Auto-port mode - wait for server ready signal and then read port file
const portInfo = await waitForPort(
key,
serverName,
effectiveSession,
10000,
);
if (portInfo) {
discoveredPort = portInfo.port;
console.info(
`[LSP:${server.id}] Auto-discovered port ${discoveredPort}`,
);
// Update managed server entry with the port
const entry = managedServers.get(key);
if (entry) {
entry.port = discoveredPort;
}
}
} else if (
server.transport?.url &&
(server.transport.kind === "websocket" ||
server.transport.kind === "stdio")
) {
// Fixed port mode - wait for the server to signal ready
await waitForWebSocket(server.transport.url);
}
if (!announcedServers.has(key)) {
console.info(`[LSP:${server.id}] ${server.label} connected`);
announcedServers.add(key);
}
return { uuid, discoveredPort };
} catch (error) {
console.error(`Failed to start language server ${server.id}`, error);
const errorMessage = error instanceof Error ? error.message : String(error);
lspStatusBar.show({
message: errorMessage || "Connection failed",
title: `${server.label} failed`,
type: "error",
icon: "error",
duration: false,
});
const entry = managedServers.get(key);
if (entry) {
getExecutor()
.stop(entry.uuid)
.catch((err: Error) => {
console.warn(
`Failed to stop language server shell ${server.id}`,
err,
);
});
managedServers.delete(key);
}
const unavailable: LspError = new Error(
`Language server ${server.id} failed to start (${errorMessage})`,
);
unavailable.code = "LSP_SERVER_UNAVAILABLE";
throw unavailable;
}
}
export function stopManagedServer(serverId: string): void {
const entry = managedServers.get(serverId);
if (!entry) return;
const executor = getExecutor();
executor.stop(entry.uuid).catch((error: Error) => {
console.warn(`Failed to stop language server ${serverId}`, error);
});
managedServers.delete(serverId);
announcedServers.delete(serverId);
// Stop foreground service when all servers are stopped
if (managedServers.size === 0) {
executor.stopService().catch(() => {});
}
}
export function resetManagedServers(): void {
for (const id of Array.from(managedServers.keys())) {
stopManagedServer(id);
}
managedServers.clear();
// Ensure foreground service is stopped
getExecutor()
.stopService()
.catch(() => {});
}
/**
* Get managed server info by server ID
*/
export function getManagedServerInfo(
serverId: string,
): ManagedServerEntry | null {
return managedServers.get(serverId) ?? null;
}
/**
* Get all managed servers
*/
export function getAllManagedServers(): Map<string, ManagedServerEntry> {
return new Map(managedServers);
}
function formatMemory(bytes: number): string {
if (!bytes || bytes <= 0) return "—";
const mb = bytes / (1024 * 1024);
if (mb >= 1) return `${mb.toFixed(1)} MB`;
const kb = bytes / 1024;
return `${kb.toFixed(0)} KB`;
}
function formatUptime(seconds: number): string {
if (!seconds || seconds <= 0) return "—";
if (seconds < 60) return `${seconds}s`;
const mins = Math.floor(seconds / 60);
const secs = seconds % 60;
if (mins < 60) return `${mins}m ${secs}s`;
const hours = Math.floor(mins / 60);
const remainingMins = mins % 60;
return `${hours}h ${remainingMins}m`;
}
/**
* Fetch server stats from the axs proxy /status endpoint
* @param serverId - The server ID to fetch stats for
* @param timeout - Timeout in milliseconds (default: 2000)
*/
export async function getServerStats(
serverId: string,
timeout = 2000,
): Promise<LspServerStatsFormatted | null> {
const entry = managedServers.get(serverId);
if (!entry?.port) {
return null;
}
try {
const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(), timeout);
const response = await fetch(`http://127.0.0.1:${entry.port}/status`, {
signal: controller.signal,
});
clearTimeout(timeoutId);
if (!response.ok) {
return null;
}
const data = (await response.json()) as LspServerStats;
// Aggregate stats from all processes
let totalMemory = 0;
let maxUptime = 0;
let firstPid: number | null = null;
for (const proc of data.processes || []) {
totalMemory += proc.memory_bytes || 0;
if (proc.uptime_secs > maxUptime) {
maxUptime = proc.uptime_secs;
}
if (firstPid === null && proc.pid) {
firstPid = proc.pid;
}
}
return {
memoryBytes: totalMemory,
memoryFormatted: formatMemory(totalMemory),
uptimeSeconds: maxUptime,
uptimeFormatted: formatUptime(maxUptime),
pid: firstPid,
processCount: data.processes?.length ?? 0,
};
} catch {
return null;
}
}