index.ts
4.74 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
import * as path from 'node:path';
import { tools } from 'evoltagent';
import type { StartDevServerResult, StopDevServerResult } from './types';
import { DevServerManager } from './utils/dev-server-manager';
/**
* Generate a stable lookup key for a dev server instance (same appPath => same key).
* Port and pid are NOT included because:
* - They become stale after restart (port may change, pid always changes)
* - DevServerManager tracks current port/pid in its internal state
* - serverKey is only used for Map lookup, never parsed
* Stable key ensures one manager per appPath and avoids concurrent start() / duplicate port logs.
*/
function makeServerKey(appPath: string): string {
const safeApp = appPath.split(path.sep).join('_');
return `launch:${safeApp}`;
}
@tools({
startDevServer: {
description: 'Start a dev server in-process and wait for it to become reachable.',
params: [
{ name: 'appPath', type: 'string', description: 'Absolute path to the app directory' },
{ name: 'runCommand', type: 'string', description: 'Run command (e.g. "pnpm dev")' },
{ name: 'timeoutMs', type: 'number', description: 'Timeout in milliseconds', optional: true },
],
returns: { type: 'string', description: 'JSON string containing StartDevServerResult with url/port/pid/serverKey.' },
},
stopDevServer: {
description: 'Stop a previously started dev server by serverKey.',
params: [{ name: 'serverKey', type: 'string', description: 'Server key returned by startDevServer()' }],
returns: { type: 'string', description: 'JSON string containing StopDevServerResult' },
},
})
export class LaunchTool {
private static readonly serverManagers = new Map<string, { manager: DevServerManager; appPath: string }>();
/** Mutex per appPath so only one startDevServer runs at a time for a given app (avoids duplicate port logs). */
private static readonly inFlightStarts = new Map<string, Promise<void>>();
async startDevServer(appPath: string, runCommand: string, timeoutMs: number = 60_000): Promise<string> {
const serverKey = makeServerKey(appPath);
const prev = LaunchTool.inFlightStarts.get(appPath);
let resolveThis: () => void;
const thisRun = new Promise<void>(r => {
resolveThis = r;
});
LaunchTool.inFlightStarts.set(appPath, thisRun);
await prev;
try {
const entry = LaunchTool.serverManagers.get(serverKey);
if (entry) {
const handle = await entry.manager.restart({ timeoutMs });
const result: StartDevServerResult = {
success: true,
serverKey,
url: handle.url,
port: handle.port,
pid: handle.child.pid ?? undefined,
outputTail: handle.outputTail(),
};
return JSON.stringify(result, null, 2);
}
const manager = new DevServerManager({ appPath, runCommand: runCommand.trim() });
const handle = await manager.start({ timeoutMs });
LaunchTool.serverManagers.set(serverKey, { manager, appPath });
const result: StartDevServerResult = {
success: true,
serverKey,
url: handle.url,
port: handle.port,
pid: handle.child.pid ?? undefined,
outputTail: handle.outputTail(),
};
return JSON.stringify(result, null, 2);
} catch (error) {
const result: StartDevServerResult = {
success: false,
error: error instanceof Error ? error.message : String(error),
};
return JSON.stringify(result, null, 2);
} finally {
resolveThis!();
LaunchTool.inFlightStarts.delete(appPath);
}
}
async stopDevServer(serverKey: string): Promise<string> {
const entry = LaunchTool.serverManagers.get(serverKey);
if (!entry) {
const result: StopDevServerResult = { success: false, serverKey, error: `Unknown serverKey: ${serverKey}` };
return JSON.stringify(result, null, 2);
}
try {
await entry.manager.stop();
LaunchTool.serverManagers.delete(serverKey);
const result: StopDevServerResult = { success: true, serverKey };
return JSON.stringify(result, null, 2);
} catch (error) {
const result: StopDevServerResult = {
success: false,
serverKey,
error: error instanceof Error ? error.message : String(error),
};
return JSON.stringify(result, null, 2);
}
}
}