codemini-cli 0.5.10 → 0.5.11
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/OPERATIONS.md +242 -242
- package/README.md +588 -588
- package/codemini-web/dist/assets/{highlighted-body-OFNGDK62-7HL7yft8.js → highlighted-body-OFNGDK62-CANOG7Xg.js} +1 -1
- package/codemini-web/dist/assets/{index-BK75hMb2.js → index-B71xykPM.js} +108 -108
- package/codemini-web/dist/assets/index-Dkq1DdDX.css +2 -0
- package/codemini-web/dist/assets/mermaid-GHXKKRXX-Z_w7M93P.js +1 -0
- package/codemini-web/dist/index.html +23 -23
- package/codemini-web/lib/approval-manager.js +32 -32
- package/codemini-web/lib/runtime-bridge.js +17 -11
- package/codemini-web/server.js +534 -205
- package/deployment.md +212 -212
- package/package.json +1 -1
- package/skills/brainstorm/SKILL.md +77 -77
- package/skills/codemini.skills.json +40 -40
- package/skills/grill-me/SKILL.md +30 -30
- package/skills/superpowers-lite/SKILL.md +82 -82
- package/src/cli.js +74 -74
- package/src/commands/chat.js +210 -210
- package/src/commands/run.js +313 -313
- package/src/commands/skill.js +438 -304
- package/src/commands/web.js +57 -57
- package/src/core/agent-loop.js +980 -980
- package/src/core/ast.js +309 -307
- package/src/core/chat-runtime.js +6261 -6253
- package/src/core/command-evaluator.js +72 -72
- package/src/core/command-loader.js +311 -311
- package/src/core/command-policy.js +301 -301
- package/src/core/command-risk.js +156 -156
- package/src/core/config-store.js +289 -289
- package/src/core/constants.js +18 -1
- package/src/core/context-compact.js +365 -365
- package/src/core/default-system-prompt.js +114 -107
- package/src/core/dream-audit.js +105 -105
- package/src/core/dream-consolidate.js +229 -229
- package/src/core/dream-evaluator.js +185 -185
- package/src/core/fff-adapter.js +383 -383
- package/src/core/memory-store.js +543 -543
- package/src/core/project-index.js +737 -548
- package/src/core/project-instructions.js +98 -98
- package/src/core/provider/anthropic.js +514 -514
- package/src/core/provider/openai-compatible.js +501 -501
- package/src/core/reflect-skill.js +178 -178
- package/src/core/reply-language.js +40 -40
- package/src/core/session-store.js +474 -474
- package/src/core/shell-profile.js +237 -237
- package/src/core/shell.js +323 -323
- package/src/core/soul.js +69 -69
- package/src/core/system-prompt-composer.js +52 -52
- package/src/core/tool-args.js +199 -154
- package/src/core/tool-output.js +184 -184
- package/src/core/tool-result-store.js +206 -206
- package/src/core/tools.js +3024 -2893
- package/src/core/version.js +11 -11
- package/src/tui/chat-app.js +5171 -5171
- package/src/tui/tool-activity/presenters/misc.js +30 -30
- package/src/tui/tool-activity/presenters/system.js +20 -20
- package/templates/project-requirements/report-shell.html +582 -582
- package/codemini-web/dist/assets/index-BSdIdn3L.css +0 -2
- package/codemini-web/dist/assets/mermaid-GHXKKRXX-Dg9qh8mg.js +0 -1
package/src/core/shell.js
CHANGED
|
@@ -1,323 +1,323 @@
|
|
|
1
|
-
import { spawn, spawnSync } from 'node:child_process';
|
|
2
|
-
|
|
3
|
-
const LONG_RUNNING_COMMAND_RE =
|
|
4
|
-
/\b(npm\s+(?:run\s+)?(?:start|dev)\b|pnpm\s+(?:run\s+)?(?:start|dev)\b|yarn\s+(?:start|dev)\b|vite\b|next\s+dev\b|webpack\s+serve\b|python\s+-m\s+http\.server\b|serve\b|java\s+-jar\b|mvn(?:w)?\s+spring-boot:run\b|gradle(?:w)?\s+bootRun\b|gradle(?:w)?\s+run\b|java\b.*\bserver\b|dotnet\s+run\b|go\s+run\b.*\b(server|cmd\/server|main\.go)\b|air\b|cargo\s+run\b.*\b(server|api|web)\b|cargo\s+watch\s+-x\s+run\b)/i;
|
|
5
|
-
const GENERIC_LONG_RUNNING_HINT_RE = /\b(start|serve|server|dev|preview|watch)\b/i;
|
|
6
|
-
const READY_OUTPUT_PATTERNS = [
|
|
7
|
-
/\bready\b/i,
|
|
8
|
-
/\bcompiled successfully\b/i,
|
|
9
|
-
/\blocal:\s*https?:\/\//i,
|
|
10
|
-
/\blistening on\b/i,
|
|
11
|
-
/\bserver running\b/i,
|
|
12
|
-
/\brunning at\b/i,
|
|
13
|
-
/\bserving at\b/i,
|
|
14
|
-
/\bstarted\b.*\bin\b/i,
|
|
15
|
-
/\bstarted\s+[A-Za-z0-9_$.-]+\s+in\b/i,
|
|
16
|
-
/\btomcat started on port\(s\):/i,
|
|
17
|
-
/\bnetty started on port/i,
|
|
18
|
-
/\bnow listening on:\s*https?:\/\//i,
|
|
19
|
-
/\bapplication started\./i,
|
|
20
|
-
/\bserving http on\b/i,
|
|
21
|
-
/\bstarting development server at\b/i,
|
|
22
|
-
/\bactix web server running on\b/i,
|
|
23
|
-
/\bhttp:\/\/127\.0\.0\.1\b/i,
|
|
24
|
-
/\bhttp:\/\/localhost\b/i
|
|
25
|
-
];
|
|
26
|
-
const INSTALL_COMMAND_PATTERNS = [
|
|
27
|
-
/\b(?:npm|pnpm|yarn|bun)\s+install\b/i,
|
|
28
|
-
/\b(?:npm|pnpm|yarn|bun)\s+(?:ci|i|add)\b/i,
|
|
29
|
-
/\buv\s+pip\s+install\b/i,
|
|
30
|
-
/\bpip\s+install\b/i,
|
|
31
|
-
/\bcargo\s+install\b/i,
|
|
32
|
-
/\bbundle\s+install\b/i,
|
|
33
|
-
/\bcomposer\s+install\b/i
|
|
34
|
-
];
|
|
35
|
-
const BUILD_COMMAND_RE = /\b(?:build|compile|bundle|pack|transpile)\b/i;
|
|
36
|
-
const TEST_COMMAND_PATTERNS = [
|
|
37
|
-
/\b(?:npm|pnpm|yarn|bun)\s+(?:run\s+)?(?:test|lint|check|typecheck)\b/i,
|
|
38
|
-
/\b(?:jest|vitest|mocha|ava|pytest|go\s+test|cargo\s+test|dotnet\s+test)\b/i
|
|
39
|
-
];
|
|
40
|
-
const FRONTEND_SERVICE_PATTERNS = [
|
|
41
|
-
/\bvite\b/i,
|
|
42
|
-
/\bnext\s+dev\b/i,
|
|
43
|
-
/\bnuxt\s+dev\b/i,
|
|
44
|
-
/\bastro\s+dev\b/i,
|
|
45
|
-
/\bremix\s+dev\b/i,
|
|
46
|
-
/\bsvelte-kit\s+dev\b/i,
|
|
47
|
-
/\bwebpack\s+serve\b/i,
|
|
48
|
-
/\bvue-cli-service\s+serve\b/i,
|
|
49
|
-
/\breact-scripts\s+start\b/i,
|
|
50
|
-
/\bstorybook\b/i,
|
|
51
|
-
/\b(?:npm|pnpm|yarn|bun)\s+(?:run\s+)?(?:dev|start|serve|preview)\b.*\b(?:client|frontend|front-end|web|ui)\b/i,
|
|
52
|
-
/\b(?:client|frontend|front-end|web|ui)\b.*\b(?:dev|start|serve|preview)\b/i
|
|
53
|
-
];
|
|
54
|
-
const BACKEND_SERVICE_PATTERNS = [
|
|
55
|
-
/\bpython\s+-m\s+http\.server\b/i,
|
|
56
|
-
/\buvicorn\b/i,
|
|
57
|
-
/\bgunicorn\b/i,
|
|
58
|
-
/\bflask\s+run\b/i,
|
|
59
|
-
/\bdjango\s+runserver\b/i,
|
|
60
|
-
/\brails\s+(?:s|server)\b/i,
|
|
61
|
-
/\bmvn(?:w)?\s+spring-boot:run\b/i,
|
|
62
|
-
/\bgradle(?:w)?\s+bootRun\b/i,
|
|
63
|
-
/\bgradle(?:w)?\s+run\b/i,
|
|
64
|
-
/\bjava\b.*\bserver\b/i,
|
|
65
|
-
/\bdotnet\s+run\b/i,
|
|
66
|
-
/\bgo\s+run\b.*\b(server|cmd\/server|main\.go)\b/i,
|
|
67
|
-
/\bnest\s+start\b/i,
|
|
68
|
-
/\bnodemon\b/i,
|
|
69
|
-
/\bts-node-dev\b/i,
|
|
70
|
-
/\bair\b/i,
|
|
71
|
-
/\bphp\s+artisan\s+serve\b/i,
|
|
72
|
-
/\bsymfony\s+server:start\b/i,
|
|
73
|
-
/\b(?:npm|pnpm|yarn|bun)\s+(?:run\s+)?(?:dev|start|serve|preview)\b.*\b(?:server|api|backend)\b/i,
|
|
74
|
-
/\b(?:server|api|backend)\b.*\b(?:dev|start|serve|preview)\b/i
|
|
75
|
-
];
|
|
76
|
-
const DATABASE_SERVICE_PATTERNS = [
|
|
77
|
-
/\bpostgres(?:ql)?\b/i,
|
|
78
|
-
/\bmysql\b/i,
|
|
79
|
-
/\bmariadb\b/i,
|
|
80
|
-
/\bmongod\b/i,
|
|
81
|
-
/\bredis-server\b/i,
|
|
82
|
-
/\b(?:docker|docker-compose|docker compose)\s+.*\b(?:db|database|postgres|mysql|mongo|redis)\b/i,
|
|
83
|
-
/\b(?:db|database|postgres|mysql|mongo|redis)\b.*\b(?:start|up|serve|run)\b/i
|
|
84
|
-
];
|
|
85
|
-
const DOCKER_SERVICE_PATTERNS = [
|
|
86
|
-
/\bdocker\s+compose\s+up\b/i,
|
|
87
|
-
/\bdocker-compose\s+up\b/i,
|
|
88
|
-
/\bdocker\s+run\b/i,
|
|
89
|
-
/\bdocker\s+start\b/i
|
|
90
|
-
];
|
|
91
|
-
const PACKAGE_SERVICE_RE = /\b(?:npm|pnpm|yarn|bun)\s+(?:run\s+)?(?:dev|start|serve|preview|watch)\b/i;
|
|
92
|
-
const VITE_OR_SERVE_RE = /\b(?:vite|serve)\b/i;
|
|
93
|
-
const SERVICE_HINT_RE = /\b(?:watch|serve|server|dev|preview)\b/i;
|
|
94
|
-
const AUTO_STOP_GRACE_MS = 150;
|
|
95
|
-
const LONG_RUNNING_STARTUP_WINDOW_MS = 1500;
|
|
96
|
-
|
|
97
|
-
function normalizeCommand(command) {
|
|
98
|
-
return String(command || '').trim();
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
function matchesAny(value, patterns) {
|
|
102
|
-
return patterns.some((pattern) => pattern.test(value));
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
export function classifyCommandIntent(command) {
|
|
106
|
-
const value = normalizeCommand(command);
|
|
107
|
-
|
|
108
|
-
if (!value) {
|
|
109
|
-
return { kind: 'generic', longRunning: false };
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
if (matchesAny(value, INSTALL_COMMAND_PATTERNS)) {
|
|
113
|
-
return { kind: 'install', longRunning: false };
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
if (BUILD_COMMAND_RE.test(value)) {
|
|
117
|
-
return { kind: 'build', longRunning: false };
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
if (matchesAny(value, TEST_COMMAND_PATTERNS)) {
|
|
121
|
-
return { kind: 'test', longRunning: false };
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
if (matchesAny(value, FRONTEND_SERVICE_PATTERNS)) {
|
|
125
|
-
return { kind: 'frontend-service', longRunning: true };
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
if (matchesAny(value, BACKEND_SERVICE_PATTERNS)) {
|
|
129
|
-
return { kind: 'backend-service', longRunning: true };
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
if (matchesAny(value, DATABASE_SERVICE_PATTERNS)) {
|
|
133
|
-
return { kind: 'database-service', longRunning: true };
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
if (matchesAny(value, DOCKER_SERVICE_PATTERNS)) {
|
|
137
|
-
return { kind: 'docker-service', longRunning: true };
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
if (
|
|
141
|
-
PACKAGE_SERVICE_RE.test(value) ||
|
|
142
|
-
VITE_OR_SERVE_RE.test(value) ||
|
|
143
|
-
SERVICE_HINT_RE.test(value)
|
|
144
|
-
) {
|
|
145
|
-
return { kind: 'service', longRunning: true };
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
if (SERVICE_HINT_RE.test(value)) {
|
|
149
|
-
return { kind: 'service', longRunning: true };
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
return { kind: 'generic', longRunning: false };
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
export function isLikelyLongRunningCommand(command) {
|
|
156
|
-
const { longRunning } = classifyCommandIntent(command);
|
|
157
|
-
return longRunning || LONG_RUNNING_COMMAND_RE.test(normalizeCommand(command)) || GENERIC_LONG_RUNNING_HINT_RE.test(normalizeCommand(command));
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
export function hasReadyOutput(text) {
|
|
161
|
-
const value = String(text || '');
|
|
162
|
-
return READY_OUTPUT_PATTERNS.some((pattern) => pattern.test(value));
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
function collectDescendantPids(rootPid, seen = new Set()) {
|
|
166
|
-
const pid = Number(rootPid);
|
|
167
|
-
if (!Number.isInteger(pid) || pid <= 0 || seen.has(pid) || process.platform === 'win32') {
|
|
168
|
-
return [];
|
|
169
|
-
}
|
|
170
|
-
seen.add(pid);
|
|
171
|
-
const result = spawnSync('ps', ['-o', 'pid=', '--ppid', String(pid)], { encoding: 'utf8' });
|
|
172
|
-
if (result.status !== 0 || !result.stdout) return [];
|
|
173
|
-
const directChildren = result.stdout
|
|
174
|
-
.split('\n')
|
|
175
|
-
.map((value) => Number(String(value || '').trim()))
|
|
176
|
-
.filter((value) => Number.isInteger(value) && value > 0);
|
|
177
|
-
const descendants = [];
|
|
178
|
-
for (const childPid of directChildren) {
|
|
179
|
-
if (seen.has(childPid)) continue;
|
|
180
|
-
descendants.push(childPid);
|
|
181
|
-
descendants.push(...collectDescendantPids(childPid, seen));
|
|
182
|
-
}
|
|
183
|
-
return descendants;
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
export function terminateChild(child, signal = 'SIGTERM') {
|
|
187
|
-
if (!child) return;
|
|
188
|
-
const pid = Number(child.pid);
|
|
189
|
-
if (process.platform !== 'win32' && Number.isInteger(pid) && pid > 0) {
|
|
190
|
-
const descendants = collectDescendantPids(pid);
|
|
191
|
-
for (const targetPid of descendants.reverse()) {
|
|
192
|
-
try {
|
|
193
|
-
process.kill(targetPid, signal);
|
|
194
|
-
} catch {}
|
|
195
|
-
}
|
|
196
|
-
}
|
|
197
|
-
try {
|
|
198
|
-
child.kill(signal);
|
|
199
|
-
} catch {}
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
export function resolveShell(defaultShell) {
|
|
203
|
-
if (process.platform === 'win32') {
|
|
204
|
-
if (defaultShell === 'cmd') {
|
|
205
|
-
return { command: 'cmd.exe', args: ['/d', '/s', '/c'] };
|
|
206
|
-
}
|
|
207
|
-
if (defaultShell === 'bash') {
|
|
208
|
-
return { command: 'bash.exe', args: ['-lc'] };
|
|
209
|
-
}
|
|
210
|
-
return { command: 'powershell.exe', args: ['-NoLogo', '-NoProfile', '-Command'] };
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
if (defaultShell === 'powershell') {
|
|
214
|
-
return { command: 'pwsh', args: ['-NoLogo', '-NoProfile', '-Command'] };
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
return { command: '/bin/bash', args: ['-lc'] };
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
export function isDangerousCommand(command, blockedPatterns = []) {
|
|
221
|
-
const lowered = command.toLowerCase();
|
|
222
|
-
return blockedPatterns.some((pattern) => lowered.includes(String(pattern).toLowerCase()));
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
export function runShellCommand({
|
|
226
|
-
command,
|
|
227
|
-
cwd = process.cwd(),
|
|
228
|
-
shell = 'powershell',
|
|
229
|
-
timeoutMs = 1800000
|
|
230
|
-
}) {
|
|
231
|
-
const shellSpec = resolveShell(shell);
|
|
232
|
-
const shellCommand =
|
|
233
|
-
process.platform !== 'win32' && /(?:^|\/)bash(?:\.exe)?$/i.test(shellSpec.command)
|
|
234
|
-
? `exec ${command}`
|
|
235
|
-
: command;
|
|
236
|
-
|
|
237
|
-
return new Promise((resolve, reject) => {
|
|
238
|
-
const child = spawn(shellSpec.command, [...shellSpec.args, shellCommand], {
|
|
239
|
-
cwd,
|
|
240
|
-
stdio: ['ignore', 'pipe', 'pipe']
|
|
241
|
-
});
|
|
242
|
-
|
|
243
|
-
let stdout = '';
|
|
244
|
-
let stderr = '';
|
|
245
|
-
let timedOut = false;
|
|
246
|
-
let autoStopped = false;
|
|
247
|
-
let stopReason = '';
|
|
248
|
-
let finalized = false;
|
|
249
|
-
const longRunningCommand = isLikelyLongRunningCommand(command);
|
|
250
|
-
const autoStopWindowMs = longRunningCommand
|
|
251
|
-
? Math.min(LONG_RUNNING_STARTUP_WINDOW_MS, Math.max(350, Math.floor(timeoutMs * 0.6)))
|
|
252
|
-
: 0;
|
|
253
|
-
|
|
254
|
-
const finalizeResolve = (value) => {
|
|
255
|
-
if (finalized) return;
|
|
256
|
-
finalized = true;
|
|
257
|
-
clearTimeout(timer);
|
|
258
|
-
if (autoStopTimer) clearTimeout(autoStopTimer);
|
|
259
|
-
resolve(value);
|
|
260
|
-
};
|
|
261
|
-
|
|
262
|
-
const finalizeReject = (error) => {
|
|
263
|
-
if (finalized) return;
|
|
264
|
-
finalized = true;
|
|
265
|
-
clearTimeout(timer);
|
|
266
|
-
if (autoStopTimer) clearTimeout(autoStopTimer);
|
|
267
|
-
reject(error);
|
|
268
|
-
};
|
|
269
|
-
|
|
270
|
-
const timer = setTimeout(() => {
|
|
271
|
-
timedOut = true;
|
|
272
|
-
terminateChild(child, 'SIGTERM');
|
|
273
|
-
}, timeoutMs);
|
|
274
|
-
const autoStopTimer =
|
|
275
|
-
autoStopWindowMs > 0
|
|
276
|
-
? setTimeout(() => {
|
|
277
|
-
finalizeAutoStop('startup_window');
|
|
278
|
-
}, autoStopWindowMs)
|
|
279
|
-
: null;
|
|
280
|
-
|
|
281
|
-
const finalizeAutoStop = (reason) => {
|
|
282
|
-
if (timedOut || autoStopped || finalized) return;
|
|
283
|
-
autoStopped = true;
|
|
284
|
-
stopReason = reason;
|
|
285
|
-
terminateChild(child, 'SIGTERM');
|
|
286
|
-
setTimeout(() => {
|
|
287
|
-
terminateChild(child, 'SIGKILL');
|
|
288
|
-
}, AUTO_STOP_GRACE_MS);
|
|
289
|
-
finalizeResolve({ code: 0, stdout, stderr, auto_stopped: true, stop_reason: stopReason });
|
|
290
|
-
};
|
|
291
|
-
|
|
292
|
-
child.stdout.on('data', (chunk) => {
|
|
293
|
-
stdout += chunk.toString();
|
|
294
|
-
if (longRunningCommand && hasReadyOutput(stdout)) {
|
|
295
|
-
finalizeAutoStop('ready_output');
|
|
296
|
-
}
|
|
297
|
-
});
|
|
298
|
-
|
|
299
|
-
child.stderr.on('data', (chunk) => {
|
|
300
|
-
stderr += chunk.toString();
|
|
301
|
-
if (longRunningCommand && hasReadyOutput(stderr)) {
|
|
302
|
-
finalizeAutoStop('ready_output');
|
|
303
|
-
}
|
|
304
|
-
});
|
|
305
|
-
|
|
306
|
-
child.on('error', (err) => {
|
|
307
|
-
finalizeReject(err);
|
|
308
|
-
});
|
|
309
|
-
|
|
310
|
-
child.on('close', (code) => {
|
|
311
|
-
if (finalized) return;
|
|
312
|
-
if (timedOut) {
|
|
313
|
-
finalizeReject(new Error(`Command timed out after ${timeoutMs}ms`));
|
|
314
|
-
return;
|
|
315
|
-
}
|
|
316
|
-
if (autoStopped) {
|
|
317
|
-
finalizeResolve({ code: 0, stdout, stderr, auto_stopped: true, stop_reason: stopReason });
|
|
318
|
-
return;
|
|
319
|
-
}
|
|
320
|
-
finalizeResolve({ code, stdout, stderr });
|
|
321
|
-
});
|
|
322
|
-
});
|
|
323
|
-
}
|
|
1
|
+
import { spawn, spawnSync } from 'node:child_process';
|
|
2
|
+
|
|
3
|
+
const LONG_RUNNING_COMMAND_RE =
|
|
4
|
+
/\b(npm\s+(?:run\s+)?(?:start|dev)\b|pnpm\s+(?:run\s+)?(?:start|dev)\b|yarn\s+(?:start|dev)\b|vite\b|next\s+dev\b|webpack\s+serve\b|python\s+-m\s+http\.server\b|serve\b|java\s+-jar\b|mvn(?:w)?\s+spring-boot:run\b|gradle(?:w)?\s+bootRun\b|gradle(?:w)?\s+run\b|java\b.*\bserver\b|dotnet\s+run\b|go\s+run\b.*\b(server|cmd\/server|main\.go)\b|air\b|cargo\s+run\b.*\b(server|api|web)\b|cargo\s+watch\s+-x\s+run\b)/i;
|
|
5
|
+
const GENERIC_LONG_RUNNING_HINT_RE = /\b(start|serve|server|dev|preview|watch)\b/i;
|
|
6
|
+
const READY_OUTPUT_PATTERNS = [
|
|
7
|
+
/\bready\b/i,
|
|
8
|
+
/\bcompiled successfully\b/i,
|
|
9
|
+
/\blocal:\s*https?:\/\//i,
|
|
10
|
+
/\blistening on\b/i,
|
|
11
|
+
/\bserver running\b/i,
|
|
12
|
+
/\brunning at\b/i,
|
|
13
|
+
/\bserving at\b/i,
|
|
14
|
+
/\bstarted\b.*\bin\b/i,
|
|
15
|
+
/\bstarted\s+[A-Za-z0-9_$.-]+\s+in\b/i,
|
|
16
|
+
/\btomcat started on port\(s\):/i,
|
|
17
|
+
/\bnetty started on port/i,
|
|
18
|
+
/\bnow listening on:\s*https?:\/\//i,
|
|
19
|
+
/\bapplication started\./i,
|
|
20
|
+
/\bserving http on\b/i,
|
|
21
|
+
/\bstarting development server at\b/i,
|
|
22
|
+
/\bactix web server running on\b/i,
|
|
23
|
+
/\bhttp:\/\/127\.0\.0\.1\b/i,
|
|
24
|
+
/\bhttp:\/\/localhost\b/i
|
|
25
|
+
];
|
|
26
|
+
const INSTALL_COMMAND_PATTERNS = [
|
|
27
|
+
/\b(?:npm|pnpm|yarn|bun)\s+install\b/i,
|
|
28
|
+
/\b(?:npm|pnpm|yarn|bun)\s+(?:ci|i|add)\b/i,
|
|
29
|
+
/\buv\s+pip\s+install\b/i,
|
|
30
|
+
/\bpip\s+install\b/i,
|
|
31
|
+
/\bcargo\s+install\b/i,
|
|
32
|
+
/\bbundle\s+install\b/i,
|
|
33
|
+
/\bcomposer\s+install\b/i
|
|
34
|
+
];
|
|
35
|
+
const BUILD_COMMAND_RE = /\b(?:build|compile|bundle|pack|transpile)\b/i;
|
|
36
|
+
const TEST_COMMAND_PATTERNS = [
|
|
37
|
+
/\b(?:npm|pnpm|yarn|bun)\s+(?:run\s+)?(?:test|lint|check|typecheck)\b/i,
|
|
38
|
+
/\b(?:jest|vitest|mocha|ava|pytest|go\s+test|cargo\s+test|dotnet\s+test)\b/i
|
|
39
|
+
];
|
|
40
|
+
const FRONTEND_SERVICE_PATTERNS = [
|
|
41
|
+
/\bvite\b/i,
|
|
42
|
+
/\bnext\s+dev\b/i,
|
|
43
|
+
/\bnuxt\s+dev\b/i,
|
|
44
|
+
/\bastro\s+dev\b/i,
|
|
45
|
+
/\bremix\s+dev\b/i,
|
|
46
|
+
/\bsvelte-kit\s+dev\b/i,
|
|
47
|
+
/\bwebpack\s+serve\b/i,
|
|
48
|
+
/\bvue-cli-service\s+serve\b/i,
|
|
49
|
+
/\breact-scripts\s+start\b/i,
|
|
50
|
+
/\bstorybook\b/i,
|
|
51
|
+
/\b(?:npm|pnpm|yarn|bun)\s+(?:run\s+)?(?:dev|start|serve|preview)\b.*\b(?:client|frontend|front-end|web|ui)\b/i,
|
|
52
|
+
/\b(?:client|frontend|front-end|web|ui)\b.*\b(?:dev|start|serve|preview)\b/i
|
|
53
|
+
];
|
|
54
|
+
const BACKEND_SERVICE_PATTERNS = [
|
|
55
|
+
/\bpython\s+-m\s+http\.server\b/i,
|
|
56
|
+
/\buvicorn\b/i,
|
|
57
|
+
/\bgunicorn\b/i,
|
|
58
|
+
/\bflask\s+run\b/i,
|
|
59
|
+
/\bdjango\s+runserver\b/i,
|
|
60
|
+
/\brails\s+(?:s|server)\b/i,
|
|
61
|
+
/\bmvn(?:w)?\s+spring-boot:run\b/i,
|
|
62
|
+
/\bgradle(?:w)?\s+bootRun\b/i,
|
|
63
|
+
/\bgradle(?:w)?\s+run\b/i,
|
|
64
|
+
/\bjava\b.*\bserver\b/i,
|
|
65
|
+
/\bdotnet\s+run\b/i,
|
|
66
|
+
/\bgo\s+run\b.*\b(server|cmd\/server|main\.go)\b/i,
|
|
67
|
+
/\bnest\s+start\b/i,
|
|
68
|
+
/\bnodemon\b/i,
|
|
69
|
+
/\bts-node-dev\b/i,
|
|
70
|
+
/\bair\b/i,
|
|
71
|
+
/\bphp\s+artisan\s+serve\b/i,
|
|
72
|
+
/\bsymfony\s+server:start\b/i,
|
|
73
|
+
/\b(?:npm|pnpm|yarn|bun)\s+(?:run\s+)?(?:dev|start|serve|preview)\b.*\b(?:server|api|backend)\b/i,
|
|
74
|
+
/\b(?:server|api|backend)\b.*\b(?:dev|start|serve|preview)\b/i
|
|
75
|
+
];
|
|
76
|
+
const DATABASE_SERVICE_PATTERNS = [
|
|
77
|
+
/\bpostgres(?:ql)?\b/i,
|
|
78
|
+
/\bmysql\b/i,
|
|
79
|
+
/\bmariadb\b/i,
|
|
80
|
+
/\bmongod\b/i,
|
|
81
|
+
/\bredis-server\b/i,
|
|
82
|
+
/\b(?:docker|docker-compose|docker compose)\s+.*\b(?:db|database|postgres|mysql|mongo|redis)\b/i,
|
|
83
|
+
/\b(?:db|database|postgres|mysql|mongo|redis)\b.*\b(?:start|up|serve|run)\b/i
|
|
84
|
+
];
|
|
85
|
+
const DOCKER_SERVICE_PATTERNS = [
|
|
86
|
+
/\bdocker\s+compose\s+up\b/i,
|
|
87
|
+
/\bdocker-compose\s+up\b/i,
|
|
88
|
+
/\bdocker\s+run\b/i,
|
|
89
|
+
/\bdocker\s+start\b/i
|
|
90
|
+
];
|
|
91
|
+
const PACKAGE_SERVICE_RE = /\b(?:npm|pnpm|yarn|bun)\s+(?:run\s+)?(?:dev|start|serve|preview|watch)\b/i;
|
|
92
|
+
const VITE_OR_SERVE_RE = /\b(?:vite|serve)\b/i;
|
|
93
|
+
const SERVICE_HINT_RE = /\b(?:watch|serve|server|dev|preview)\b/i;
|
|
94
|
+
const AUTO_STOP_GRACE_MS = 150;
|
|
95
|
+
const LONG_RUNNING_STARTUP_WINDOW_MS = 1500;
|
|
96
|
+
|
|
97
|
+
function normalizeCommand(command) {
|
|
98
|
+
return String(command || '').trim();
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function matchesAny(value, patterns) {
|
|
102
|
+
return patterns.some((pattern) => pattern.test(value));
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
export function classifyCommandIntent(command) {
|
|
106
|
+
const value = normalizeCommand(command);
|
|
107
|
+
|
|
108
|
+
if (!value) {
|
|
109
|
+
return { kind: 'generic', longRunning: false };
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
if (matchesAny(value, INSTALL_COMMAND_PATTERNS)) {
|
|
113
|
+
return { kind: 'install', longRunning: false };
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
if (BUILD_COMMAND_RE.test(value)) {
|
|
117
|
+
return { kind: 'build', longRunning: false };
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
if (matchesAny(value, TEST_COMMAND_PATTERNS)) {
|
|
121
|
+
return { kind: 'test', longRunning: false };
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
if (matchesAny(value, FRONTEND_SERVICE_PATTERNS)) {
|
|
125
|
+
return { kind: 'frontend-service', longRunning: true };
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
if (matchesAny(value, BACKEND_SERVICE_PATTERNS)) {
|
|
129
|
+
return { kind: 'backend-service', longRunning: true };
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
if (matchesAny(value, DATABASE_SERVICE_PATTERNS)) {
|
|
133
|
+
return { kind: 'database-service', longRunning: true };
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
if (matchesAny(value, DOCKER_SERVICE_PATTERNS)) {
|
|
137
|
+
return { kind: 'docker-service', longRunning: true };
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
if (
|
|
141
|
+
PACKAGE_SERVICE_RE.test(value) ||
|
|
142
|
+
VITE_OR_SERVE_RE.test(value) ||
|
|
143
|
+
SERVICE_HINT_RE.test(value)
|
|
144
|
+
) {
|
|
145
|
+
return { kind: 'service', longRunning: true };
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
if (SERVICE_HINT_RE.test(value)) {
|
|
149
|
+
return { kind: 'service', longRunning: true };
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
return { kind: 'generic', longRunning: false };
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
export function isLikelyLongRunningCommand(command) {
|
|
156
|
+
const { longRunning } = classifyCommandIntent(command);
|
|
157
|
+
return longRunning || LONG_RUNNING_COMMAND_RE.test(normalizeCommand(command)) || GENERIC_LONG_RUNNING_HINT_RE.test(normalizeCommand(command));
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
export function hasReadyOutput(text) {
|
|
161
|
+
const value = String(text || '');
|
|
162
|
+
return READY_OUTPUT_PATTERNS.some((pattern) => pattern.test(value));
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
function collectDescendantPids(rootPid, seen = new Set()) {
|
|
166
|
+
const pid = Number(rootPid);
|
|
167
|
+
if (!Number.isInteger(pid) || pid <= 0 || seen.has(pid) || process.platform === 'win32') {
|
|
168
|
+
return [];
|
|
169
|
+
}
|
|
170
|
+
seen.add(pid);
|
|
171
|
+
const result = spawnSync('ps', ['-o', 'pid=', '--ppid', String(pid)], { encoding: 'utf8' });
|
|
172
|
+
if (result.status !== 0 || !result.stdout) return [];
|
|
173
|
+
const directChildren = result.stdout
|
|
174
|
+
.split('\n')
|
|
175
|
+
.map((value) => Number(String(value || '').trim()))
|
|
176
|
+
.filter((value) => Number.isInteger(value) && value > 0);
|
|
177
|
+
const descendants = [];
|
|
178
|
+
for (const childPid of directChildren) {
|
|
179
|
+
if (seen.has(childPid)) continue;
|
|
180
|
+
descendants.push(childPid);
|
|
181
|
+
descendants.push(...collectDescendantPids(childPid, seen));
|
|
182
|
+
}
|
|
183
|
+
return descendants;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
export function terminateChild(child, signal = 'SIGTERM') {
|
|
187
|
+
if (!child) return;
|
|
188
|
+
const pid = Number(child.pid);
|
|
189
|
+
if (process.platform !== 'win32' && Number.isInteger(pid) && pid > 0) {
|
|
190
|
+
const descendants = collectDescendantPids(pid);
|
|
191
|
+
for (const targetPid of descendants.reverse()) {
|
|
192
|
+
try {
|
|
193
|
+
process.kill(targetPid, signal);
|
|
194
|
+
} catch {}
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
try {
|
|
198
|
+
child.kill(signal);
|
|
199
|
+
} catch {}
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
export function resolveShell(defaultShell) {
|
|
203
|
+
if (process.platform === 'win32') {
|
|
204
|
+
if (defaultShell === 'cmd') {
|
|
205
|
+
return { command: 'cmd.exe', args: ['/d', '/s', '/c'] };
|
|
206
|
+
}
|
|
207
|
+
if (defaultShell === 'bash') {
|
|
208
|
+
return { command: 'bash.exe', args: ['-lc'] };
|
|
209
|
+
}
|
|
210
|
+
return { command: 'powershell.exe', args: ['-NoLogo', '-NoProfile', '-Command'] };
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
if (defaultShell === 'powershell') {
|
|
214
|
+
return { command: 'pwsh', args: ['-NoLogo', '-NoProfile', '-Command'] };
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
return { command: '/bin/bash', args: ['-lc'] };
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
export function isDangerousCommand(command, blockedPatterns = []) {
|
|
221
|
+
const lowered = command.toLowerCase();
|
|
222
|
+
return blockedPatterns.some((pattern) => lowered.includes(String(pattern).toLowerCase()));
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
export function runShellCommand({
|
|
226
|
+
command,
|
|
227
|
+
cwd = process.cwd(),
|
|
228
|
+
shell = 'powershell',
|
|
229
|
+
timeoutMs = 1800000
|
|
230
|
+
}) {
|
|
231
|
+
const shellSpec = resolveShell(shell);
|
|
232
|
+
const shellCommand =
|
|
233
|
+
process.platform !== 'win32' && /(?:^|\/)bash(?:\.exe)?$/i.test(shellSpec.command)
|
|
234
|
+
? `exec ${command}`
|
|
235
|
+
: command;
|
|
236
|
+
|
|
237
|
+
return new Promise((resolve, reject) => {
|
|
238
|
+
const child = spawn(shellSpec.command, [...shellSpec.args, shellCommand], {
|
|
239
|
+
cwd,
|
|
240
|
+
stdio: ['ignore', 'pipe', 'pipe']
|
|
241
|
+
});
|
|
242
|
+
|
|
243
|
+
let stdout = '';
|
|
244
|
+
let stderr = '';
|
|
245
|
+
let timedOut = false;
|
|
246
|
+
let autoStopped = false;
|
|
247
|
+
let stopReason = '';
|
|
248
|
+
let finalized = false;
|
|
249
|
+
const longRunningCommand = isLikelyLongRunningCommand(command);
|
|
250
|
+
const autoStopWindowMs = longRunningCommand
|
|
251
|
+
? Math.min(LONG_RUNNING_STARTUP_WINDOW_MS, Math.max(350, Math.floor(timeoutMs * 0.6)))
|
|
252
|
+
: 0;
|
|
253
|
+
|
|
254
|
+
const finalizeResolve = (value) => {
|
|
255
|
+
if (finalized) return;
|
|
256
|
+
finalized = true;
|
|
257
|
+
clearTimeout(timer);
|
|
258
|
+
if (autoStopTimer) clearTimeout(autoStopTimer);
|
|
259
|
+
resolve(value);
|
|
260
|
+
};
|
|
261
|
+
|
|
262
|
+
const finalizeReject = (error) => {
|
|
263
|
+
if (finalized) return;
|
|
264
|
+
finalized = true;
|
|
265
|
+
clearTimeout(timer);
|
|
266
|
+
if (autoStopTimer) clearTimeout(autoStopTimer);
|
|
267
|
+
reject(error);
|
|
268
|
+
};
|
|
269
|
+
|
|
270
|
+
const timer = setTimeout(() => {
|
|
271
|
+
timedOut = true;
|
|
272
|
+
terminateChild(child, 'SIGTERM');
|
|
273
|
+
}, timeoutMs);
|
|
274
|
+
const autoStopTimer =
|
|
275
|
+
autoStopWindowMs > 0
|
|
276
|
+
? setTimeout(() => {
|
|
277
|
+
finalizeAutoStop('startup_window');
|
|
278
|
+
}, autoStopWindowMs)
|
|
279
|
+
: null;
|
|
280
|
+
|
|
281
|
+
const finalizeAutoStop = (reason) => {
|
|
282
|
+
if (timedOut || autoStopped || finalized) return;
|
|
283
|
+
autoStopped = true;
|
|
284
|
+
stopReason = reason;
|
|
285
|
+
terminateChild(child, 'SIGTERM');
|
|
286
|
+
setTimeout(() => {
|
|
287
|
+
terminateChild(child, 'SIGKILL');
|
|
288
|
+
}, AUTO_STOP_GRACE_MS);
|
|
289
|
+
finalizeResolve({ code: 0, stdout, stderr, auto_stopped: true, stop_reason: stopReason });
|
|
290
|
+
};
|
|
291
|
+
|
|
292
|
+
child.stdout.on('data', (chunk) => {
|
|
293
|
+
stdout += chunk.toString();
|
|
294
|
+
if (longRunningCommand && hasReadyOutput(stdout)) {
|
|
295
|
+
finalizeAutoStop('ready_output');
|
|
296
|
+
}
|
|
297
|
+
});
|
|
298
|
+
|
|
299
|
+
child.stderr.on('data', (chunk) => {
|
|
300
|
+
stderr += chunk.toString();
|
|
301
|
+
if (longRunningCommand && hasReadyOutput(stderr)) {
|
|
302
|
+
finalizeAutoStop('ready_output');
|
|
303
|
+
}
|
|
304
|
+
});
|
|
305
|
+
|
|
306
|
+
child.on('error', (err) => {
|
|
307
|
+
finalizeReject(err);
|
|
308
|
+
});
|
|
309
|
+
|
|
310
|
+
child.on('close', (code) => {
|
|
311
|
+
if (finalized) return;
|
|
312
|
+
if (timedOut) {
|
|
313
|
+
finalizeReject(new Error(`Command timed out after ${timeoutMs}ms`));
|
|
314
|
+
return;
|
|
315
|
+
}
|
|
316
|
+
if (autoStopped) {
|
|
317
|
+
finalizeResolve({ code: 0, stdout, stderr, auto_stopped: true, stop_reason: stopReason });
|
|
318
|
+
return;
|
|
319
|
+
}
|
|
320
|
+
finalizeResolve({ code, stdout, stderr });
|
|
321
|
+
});
|
|
322
|
+
});
|
|
323
|
+
}
|