ai-cli-mcp 2.13.0 → 2.14.1
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/CHANGELOG.md +14 -0
- package/README.ja.md +10 -0
- package/README.md +10 -0
- package/dist/__tests__/app-cli.test.js +26 -2
- package/dist/__tests__/cli-process-service.test.js +218 -5
- package/dist/__tests__/mcp-contract.test.js +138 -8
- package/dist/__tests__/process-management.test.js +2 -1
- package/dist/app/cli.js +6 -4
- package/dist/app/mcp.js +8 -4
- package/dist/cli-process-service.js +26 -26
- package/dist/process-result.js +51 -0
- package/dist/process-service.js +7 -21
- package/package.json +1 -1
- package/src/__tests__/app-cli.test.ts +35 -1
- package/src/__tests__/cli-process-service.test.ts +249 -5
- package/src/__tests__/mcp-contract.test.ts +152 -8
- package/src/__tests__/process-management.test.ts +2 -1
- package/src/app/cli.ts +7 -5
- package/src/app/mcp.ts +9 -4
- package/src/cli-process-service.ts +29 -26
- package/src/process-result.ts +79 -0
- package/src/process-service.ts +7 -22
|
@@ -122,6 +122,7 @@ describe('MCP Contract Tests', () => {
|
|
|
122
122
|
expect(Object.keys(waitTool.inputSchema.properties).sort()).toEqual([
|
|
123
123
|
'pids',
|
|
124
124
|
'timeout',
|
|
125
|
+
'verbose',
|
|
125
126
|
]);
|
|
126
127
|
});
|
|
127
128
|
|
|
@@ -155,22 +156,32 @@ describe('MCP Contract Tests', () => {
|
|
|
155
156
|
pid: runData.pid,
|
|
156
157
|
agent: 'claude',
|
|
157
158
|
status: expect.any(String),
|
|
158
|
-
startTime: expect.any(String),
|
|
159
|
-
workFolder: testDir,
|
|
160
|
-
prompt: 'create a file called contract.txt with content "hello"',
|
|
161
159
|
model: 'haiku',
|
|
162
160
|
stdout: expect.any(String),
|
|
163
161
|
stderr: expect.any(String),
|
|
164
162
|
});
|
|
163
|
+
expect(getResultData).toHaveProperty('exitCode');
|
|
164
|
+
expect(getResultData).not.toHaveProperty('startTime');
|
|
165
|
+
expect(getResultData).not.toHaveProperty('workFolder');
|
|
166
|
+
expect(getResultData).not.toHaveProperty('prompt');
|
|
165
167
|
|
|
166
168
|
const waitResponse = await client.callTool('wait', { pids: [runData.pid], timeout: 5 });
|
|
167
169
|
const waitData = parseToolJson(waitResponse);
|
|
168
170
|
|
|
169
171
|
expect(Array.isArray(waitData)).toBe(true);
|
|
170
172
|
expect(waitData).toHaveLength(1);
|
|
171
|
-
expect(waitData[0]
|
|
172
|
-
|
|
173
|
-
|
|
173
|
+
expect(waitData[0]).toMatchObject({
|
|
174
|
+
pid: runData.pid,
|
|
175
|
+
agent: 'claude',
|
|
176
|
+
status: 'completed',
|
|
177
|
+
exitCode: 0,
|
|
178
|
+
model: 'haiku',
|
|
179
|
+
stdout: expect.any(String),
|
|
180
|
+
stderr: expect.any(String),
|
|
181
|
+
});
|
|
182
|
+
expect(waitData[0]).not.toHaveProperty('startTime');
|
|
183
|
+
expect(waitData[0]).not.toHaveProperty('workFolder');
|
|
184
|
+
expect(waitData[0]).not.toHaveProperty('prompt');
|
|
174
185
|
|
|
175
186
|
const cleanupResponse = await client.callTool('cleanup_processes', {});
|
|
176
187
|
const cleanupData = parseToolJson(cleanupResponse);
|
|
@@ -183,13 +194,14 @@ describe('MCP Contract Tests', () => {
|
|
|
183
194
|
expect(cleanupData.removedPids).toContain(runData.pid);
|
|
184
195
|
});
|
|
185
196
|
|
|
186
|
-
it('
|
|
197
|
+
it('preserves successful prompt_file execution through the MCP process path', async () => {
|
|
187
198
|
const promptFile = join(testDir, 'prompt.txt');
|
|
188
|
-
writeFileSync(promptFile, '
|
|
199
|
+
writeFileSync(promptFile, 'Create a file from prompt_file');
|
|
189
200
|
|
|
190
201
|
const runResponse = await client.callTool('run', {
|
|
191
202
|
prompt_file: promptFile,
|
|
192
203
|
workFolder: testDir,
|
|
204
|
+
model: 'haiku',
|
|
193
205
|
});
|
|
194
206
|
const runData = parseToolJson(runResponse);
|
|
195
207
|
|
|
@@ -199,6 +211,138 @@ describe('MCP Contract Tests', () => {
|
|
|
199
211
|
agent: 'claude',
|
|
200
212
|
message: expect.any(String),
|
|
201
213
|
});
|
|
214
|
+
|
|
215
|
+
const waitResponse = await client.callTool('wait', { pids: [runData.pid], timeout: 5 });
|
|
216
|
+
const waitData = parseToolJson(waitResponse);
|
|
217
|
+
|
|
218
|
+
expect(waitData).toHaveLength(1);
|
|
219
|
+
expect(waitData[0]).toMatchObject({
|
|
220
|
+
pid: runData.pid,
|
|
221
|
+
agent: 'claude',
|
|
222
|
+
status: 'completed',
|
|
223
|
+
exitCode: 0,
|
|
224
|
+
model: 'haiku',
|
|
225
|
+
stdout: expect.stringContaining('Created file successfully'),
|
|
226
|
+
stderr: '',
|
|
227
|
+
});
|
|
228
|
+
expect(waitData[0]).not.toHaveProperty('prompt');
|
|
229
|
+
expect(waitData[0]).not.toHaveProperty('workFolder');
|
|
230
|
+
expect(waitData[0]).not.toHaveProperty('startTime');
|
|
231
|
+
});
|
|
232
|
+
|
|
233
|
+
it('returns compact results by default and full results when verbose is true for parsed output', async () => {
|
|
234
|
+
await client.disconnect();
|
|
235
|
+
|
|
236
|
+
const verboseMockPath = join(testDir, 'verbose-claude');
|
|
237
|
+
writeFileSync(
|
|
238
|
+
verboseMockPath,
|
|
239
|
+
`#!/bin/bash
|
|
240
|
+
printf '%s\n' '{"type":"assistant","message":{"content":[{"type":"tool_use","id":"tool-1","name":"Read","input":{"file_path":"/tmp/demo.txt"}}]}}'
|
|
241
|
+
printf '%s\n' '{"type":"user","message":{"content":[{"type":"tool_result","tool_use_id":"tool-1","content":[{"type":"text","text":"demo output"}]}]}}'
|
|
242
|
+
printf '%s\n' '{"type":"result","result":"Completed contract verbose test"}'
|
|
243
|
+
printf '%s\n' '{"type":"system","session_id":"session-verbose-1"}'
|
|
244
|
+
`
|
|
245
|
+
);
|
|
246
|
+
chmodSync(verboseMockPath, 0o755);
|
|
247
|
+
|
|
248
|
+
client = createTestClient({ claudeCliName: verboseMockPath, debug: false });
|
|
249
|
+
await client.connect();
|
|
250
|
+
|
|
251
|
+
const runResponse = await client.callTool('run', {
|
|
252
|
+
prompt: 'verbose-shape-test',
|
|
253
|
+
workFolder: testDir,
|
|
254
|
+
});
|
|
255
|
+
const runData = parseToolJson(runResponse);
|
|
256
|
+
|
|
257
|
+
const completedWait = parseToolJson(await client.callTool('wait', { pids: [runData.pid], timeout: 5 }));
|
|
258
|
+
expect(completedWait).toHaveLength(1);
|
|
259
|
+
expect(completedWait[0].status).toBe('completed');
|
|
260
|
+
|
|
261
|
+
const compactResult = parseToolJson(await client.callTool('get_result', { pid: runData.pid }));
|
|
262
|
+
expect(compactResult).toMatchObject({
|
|
263
|
+
pid: runData.pid,
|
|
264
|
+
agent: 'claude',
|
|
265
|
+
status: 'completed',
|
|
266
|
+
exitCode: 0,
|
|
267
|
+
model: null,
|
|
268
|
+
session_id: 'session-verbose-1',
|
|
269
|
+
agentOutput: {
|
|
270
|
+
message: 'Completed contract verbose test',
|
|
271
|
+
session_id: 'session-verbose-1',
|
|
272
|
+
},
|
|
273
|
+
});
|
|
274
|
+
expect(compactResult).not.toHaveProperty('startTime');
|
|
275
|
+
expect(compactResult).not.toHaveProperty('workFolder');
|
|
276
|
+
expect(compactResult).not.toHaveProperty('prompt');
|
|
277
|
+
expect(compactResult.agentOutput).not.toHaveProperty('tools');
|
|
278
|
+
|
|
279
|
+
const verboseResult = parseToolJson(await client.callTool('get_result', { pid: runData.pid, verbose: true }));
|
|
280
|
+
expect(verboseResult).toMatchObject({
|
|
281
|
+
pid: runData.pid,
|
|
282
|
+
agent: 'claude',
|
|
283
|
+
status: 'completed',
|
|
284
|
+
exitCode: 0,
|
|
285
|
+
model: null,
|
|
286
|
+
startTime: expect.any(String),
|
|
287
|
+
workFolder: testDir,
|
|
288
|
+
prompt: 'verbose-shape-test',
|
|
289
|
+
session_id: 'session-verbose-1',
|
|
290
|
+
agentOutput: {
|
|
291
|
+
message: 'Completed contract verbose test',
|
|
292
|
+
session_id: 'session-verbose-1',
|
|
293
|
+
tools: [
|
|
294
|
+
{
|
|
295
|
+
tool: 'Read',
|
|
296
|
+
input: { file_path: '/tmp/demo.txt' },
|
|
297
|
+
output: 'demo output',
|
|
298
|
+
},
|
|
299
|
+
],
|
|
300
|
+
},
|
|
301
|
+
});
|
|
302
|
+
|
|
303
|
+
const compactWait = parseToolJson(await client.callTool('wait', { pids: [runData.pid], timeout: 5 }));
|
|
304
|
+
expect(compactWait).toHaveLength(1);
|
|
305
|
+
expect(compactWait[0]).toMatchObject({
|
|
306
|
+
pid: runData.pid,
|
|
307
|
+
agent: 'claude',
|
|
308
|
+
status: 'completed',
|
|
309
|
+
exitCode: 0,
|
|
310
|
+
model: null,
|
|
311
|
+
session_id: 'session-verbose-1',
|
|
312
|
+
agentOutput: {
|
|
313
|
+
message: 'Completed contract verbose test',
|
|
314
|
+
session_id: 'session-verbose-1',
|
|
315
|
+
},
|
|
316
|
+
});
|
|
317
|
+
expect(compactWait[0]).not.toHaveProperty('startTime');
|
|
318
|
+
expect(compactWait[0]).not.toHaveProperty('workFolder');
|
|
319
|
+
expect(compactWait[0]).not.toHaveProperty('prompt');
|
|
320
|
+
expect(compactWait[0].agentOutput).not.toHaveProperty('tools');
|
|
321
|
+
|
|
322
|
+
const verboseWait = parseToolJson(await client.callTool('wait', { pids: [runData.pid], timeout: 5, verbose: true }));
|
|
323
|
+
expect(verboseWait).toHaveLength(1);
|
|
324
|
+
expect(verboseWait[0]).toMatchObject({
|
|
325
|
+
pid: runData.pid,
|
|
326
|
+
agent: 'claude',
|
|
327
|
+
status: 'completed',
|
|
328
|
+
exitCode: 0,
|
|
329
|
+
model: null,
|
|
330
|
+
startTime: expect.any(String),
|
|
331
|
+
workFolder: testDir,
|
|
332
|
+
prompt: 'verbose-shape-test',
|
|
333
|
+
session_id: 'session-verbose-1',
|
|
334
|
+
agentOutput: {
|
|
335
|
+
message: 'Completed contract verbose test',
|
|
336
|
+
session_id: 'session-verbose-1',
|
|
337
|
+
tools: [
|
|
338
|
+
{
|
|
339
|
+
tool: 'Read',
|
|
340
|
+
input: { file_path: '/tmp/demo.txt' },
|
|
341
|
+
output: 'demo output',
|
|
342
|
+
},
|
|
343
|
+
],
|
|
344
|
+
},
|
|
345
|
+
});
|
|
202
346
|
});
|
|
203
347
|
|
|
204
348
|
it('covers forge end-to-end through the MCP process path', async () => {
|
package/src/app/cli.ts
CHANGED
|
@@ -41,18 +41,20 @@ Compatibility aliases:
|
|
|
41
41
|
export const WAIT_HELP_TEXT = `Usage: ai-cli wait <pid...> [options]
|
|
42
42
|
|
|
43
43
|
Wait for one or more tracked processes to finish.
|
|
44
|
+
By default each result uses the compact shape; set --verbose to include full metadata and detailed parsed output.
|
|
44
45
|
|
|
45
46
|
Options:
|
|
46
47
|
--timeout <seconds> Maximum wait time in seconds
|
|
48
|
+
--verbose Return full metadata and detailed parsed output
|
|
47
49
|
--help, -h Show this help message
|
|
48
50
|
`;
|
|
49
51
|
|
|
50
52
|
export const RESULT_HELP_TEXT = `Usage: ai-cli result <pid> [options]
|
|
51
53
|
|
|
52
|
-
Get the current
|
|
54
|
+
Get the current output and status of a tracked process. By default this returns a compact result shape; set --verbose to include full metadata and detailed parsed output.
|
|
53
55
|
|
|
54
56
|
Options:
|
|
55
|
-
--verbose
|
|
57
|
+
--verbose Return full metadata and detailed parsed output
|
|
56
58
|
--help, -h Show this help message
|
|
57
59
|
`;
|
|
58
60
|
|
|
@@ -115,7 +117,7 @@ interface CliDeps {
|
|
|
115
117
|
}) => Promise<any>;
|
|
116
118
|
listProcesses: () => Promise<any>;
|
|
117
119
|
getProcessResult: (pid: number, verbose: boolean) => Promise<any>;
|
|
118
|
-
waitForProcesses: (pids: number[], timeoutSeconds?: number) => Promise<any>;
|
|
120
|
+
waitForProcesses: (pids: number[], timeoutSeconds?: number, verbose?: boolean) => Promise<any>;
|
|
119
121
|
killProcess: (pid: number) => Promise<any>;
|
|
120
122
|
cleanupProcesses: () => Promise<any>;
|
|
121
123
|
getDoctorStatus: () => any;
|
|
@@ -137,7 +139,7 @@ const defaultDeps: CliDeps = {
|
|
|
137
139
|
runProcess: (options) => getCliProcessService().startProcess(options),
|
|
138
140
|
listProcesses: () => getCliProcessService().listProcesses(),
|
|
139
141
|
getProcessResult: (pid, verbose) => getCliProcessService().getProcessResult(pid, verbose),
|
|
140
|
-
waitForProcesses: (pids, timeoutSeconds) => getCliProcessService().waitForProcesses(pids, timeoutSeconds),
|
|
142
|
+
waitForProcesses: (pids, timeoutSeconds, verbose) => getCliProcessService().waitForProcesses(pids, timeoutSeconds, verbose),
|
|
141
143
|
killProcess: (pid) => getCliProcessService().killProcess(pid),
|
|
142
144
|
cleanupProcesses: () => getCliProcessService().cleanupProcesses(),
|
|
143
145
|
getDoctorStatus: () => getCliDoctorStatus(),
|
|
@@ -317,7 +319,7 @@ export async function runCli(argv: string[], deps: Partial<CliDeps> = {}): Promi
|
|
|
317
319
|
return 1;
|
|
318
320
|
}
|
|
319
321
|
|
|
320
|
-
writeJson(stdout, await waitForProcesses(pids as number[], timeout));
|
|
322
|
+
writeJson(stdout, await waitForProcesses(pids as number[], timeout, 'verbose' in flags));
|
|
321
323
|
return 0;
|
|
322
324
|
}
|
|
323
325
|
|
package/src/app/mcp.ts
CHANGED
|
@@ -187,7 +187,7 @@ ${getSupportedModelsDescription()}
|
|
|
187
187
|
},
|
|
188
188
|
{
|
|
189
189
|
name: 'get_result',
|
|
190
|
-
description: 'Get the current output and status of an AI agent process by PID.
|
|
190
|
+
description: 'Get the current output and status of an AI agent process by PID. Defaults to a compact result shape; set verbose to true for full metadata and detailed parsed output.',
|
|
191
191
|
inputSchema: {
|
|
192
192
|
type: 'object',
|
|
193
193
|
properties: {
|
|
@@ -197,7 +197,7 @@ ${getSupportedModelsDescription()}
|
|
|
197
197
|
},
|
|
198
198
|
verbose: {
|
|
199
199
|
type: 'boolean',
|
|
200
|
-
description: 'Optional: If true, returns
|
|
200
|
+
description: 'Optional: If true, returns the full result shape including metadata fields and detailed parsed output such as tool usage history. Defaults to false.',
|
|
201
201
|
}
|
|
202
202
|
},
|
|
203
203
|
required: ['pid'],
|
|
@@ -205,7 +205,7 @@ ${getSupportedModelsDescription()}
|
|
|
205
205
|
},
|
|
206
206
|
{
|
|
207
207
|
name: 'wait',
|
|
208
|
-
description: 'Wait for multiple AI agent processes to complete and return their results.
|
|
208
|
+
description: 'Wait for multiple AI agent processes to complete and return their results. Defaults to compact result items; set verbose to true for full metadata and detailed parsed output.',
|
|
209
209
|
inputSchema: {
|
|
210
210
|
type: 'object',
|
|
211
211
|
properties: {
|
|
@@ -218,6 +218,10 @@ ${getSupportedModelsDescription()}
|
|
|
218
218
|
type: 'number',
|
|
219
219
|
description: 'Optional: Maximum time to wait in seconds. Defaults to 180 (3 minutes).',
|
|
220
220
|
},
|
|
221
|
+
verbose: {
|
|
222
|
+
type: 'boolean',
|
|
223
|
+
description: 'Optional: If true, each result item uses the full result shape including metadata fields and detailed parsed output. Defaults to false.',
|
|
224
|
+
},
|
|
221
225
|
},
|
|
222
226
|
required: ['pids'],
|
|
223
227
|
},
|
|
@@ -336,7 +340,8 @@ ${getSupportedModelsDescription()}
|
|
|
336
340
|
try {
|
|
337
341
|
const results = await this.processService.waitForProcesses(
|
|
338
342
|
toolArguments.pids,
|
|
339
|
-
typeof toolArguments.timeout === 'number' ? toolArguments.timeout : 180
|
|
343
|
+
typeof toolArguments.timeout === 'number' ? toolArguments.timeout : 180,
|
|
344
|
+
!!toolArguments.verbose
|
|
340
345
|
);
|
|
341
346
|
return {
|
|
342
347
|
content: [{
|
|
@@ -12,17 +12,19 @@ import {
|
|
|
12
12
|
unlinkSync,
|
|
13
13
|
writeFileSync,
|
|
14
14
|
} from 'node:fs';
|
|
15
|
-
import { join } from 'node:path';
|
|
15
|
+
import { join, basename, dirname } from 'node:path';
|
|
16
16
|
import { homedir } from 'node:os';
|
|
17
17
|
import { buildCliCommand, type BuildCliCommandOptions } from './cli-builder.js';
|
|
18
18
|
import { findClaudeCli, findCodexCli, findForgeCli, findGeminiCli } from './cli-utils.js';
|
|
19
19
|
import { parseClaudeOutput, parseCodexOutput, parseForgeOutput, parseGeminiOutput } from './parsers.js';
|
|
20
|
+
import { buildProcessResult } from './process-result.js';
|
|
20
21
|
import type { AgentType, ProcessListItem } from './process-service.js';
|
|
21
22
|
|
|
22
23
|
interface StoredProcess {
|
|
23
24
|
pid: number;
|
|
24
25
|
prompt: string;
|
|
25
26
|
workFolder: string;
|
|
27
|
+
cwdKey?: string;
|
|
26
28
|
model?: string;
|
|
27
29
|
toolType: AgentType;
|
|
28
30
|
startTime: string;
|
|
@@ -127,6 +129,7 @@ export class CliProcessService {
|
|
|
127
129
|
pid,
|
|
128
130
|
prompt: cmd.prompt,
|
|
129
131
|
workFolder: cmd.cwd,
|
|
132
|
+
cwdKey: this.resolveCwdKey(cmd.cwd),
|
|
130
133
|
model: options.model,
|
|
131
134
|
toolType: cmd.agent,
|
|
132
135
|
startTime: new Date().toISOString(),
|
|
@@ -183,7 +186,7 @@ export class CliProcessService {
|
|
|
183
186
|
}
|
|
184
187
|
}
|
|
185
188
|
|
|
186
|
-
|
|
189
|
+
return buildProcessResult({
|
|
187
190
|
pid,
|
|
188
191
|
agent: refreshed.toolType,
|
|
189
192
|
status: refreshed.status,
|
|
@@ -192,27 +195,12 @@ export class CliProcessService {
|
|
|
192
195
|
workFolder: refreshed.workFolder,
|
|
193
196
|
prompt: refreshed.prompt,
|
|
194
197
|
model: refreshed.model,
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
if (!verbose && agentOutput.tools) {
|
|
199
|
-
const { tools, ...rest } = agentOutput;
|
|
200
|
-
response.agentOutput = rest;
|
|
201
|
-
} else {
|
|
202
|
-
response.agentOutput = agentOutput;
|
|
203
|
-
}
|
|
204
|
-
if (agentOutput.session_id) {
|
|
205
|
-
response.session_id = agentOutput.session_id;
|
|
206
|
-
}
|
|
207
|
-
} else {
|
|
208
|
-
response.stdout = stdout;
|
|
209
|
-
response.stderr = stderr;
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
return response;
|
|
198
|
+
stdout,
|
|
199
|
+
stderr,
|
|
200
|
+
}, agentOutput, verbose);
|
|
213
201
|
}
|
|
214
202
|
|
|
215
|
-
async waitForProcesses(pids: number[], timeoutSeconds = 180): Promise<any[]> {
|
|
203
|
+
async waitForProcesses(pids: number[], timeoutSeconds = 180, verbose = false): Promise<any[]> {
|
|
216
204
|
const start = Date.now();
|
|
217
205
|
for (const pid of pids) {
|
|
218
206
|
this.readProcess(pid);
|
|
@@ -221,7 +209,7 @@ export class CliProcessService {
|
|
|
221
209
|
while (true) {
|
|
222
210
|
const statuses = pids.map((pid) => this.refreshStatus(this.readProcess(pid)).status);
|
|
223
211
|
if (statuses.every((status) => status !== 'running')) {
|
|
224
|
-
return Promise.all(pids.map((pid) => this.getProcessResult(pid,
|
|
212
|
+
return Promise.all(pids.map((pid) => this.getProcessResult(pid, verbose)));
|
|
225
213
|
}
|
|
226
214
|
|
|
227
215
|
if (Date.now() - start >= timeoutSeconds * 1000) {
|
|
@@ -274,7 +262,7 @@ export class CliProcessService {
|
|
|
274
262
|
continue;
|
|
275
263
|
}
|
|
276
264
|
|
|
277
|
-
const processDir = this.
|
|
265
|
+
const processDir = this.resolveStoredProcessDir(refreshed);
|
|
278
266
|
if (existsSync(processDir)) {
|
|
279
267
|
rmSync(processDir, { recursive: true, force: true });
|
|
280
268
|
removed++;
|
|
@@ -318,11 +306,15 @@ export class CliProcessService {
|
|
|
318
306
|
}
|
|
319
307
|
|
|
320
308
|
private parseProcessFile(metaPath: string): StoredProcess {
|
|
321
|
-
|
|
309
|
+
const process = JSON.parse(readFileSync(metaPath, 'utf-8')) as StoredProcess;
|
|
310
|
+
if (!process.cwdKey) {
|
|
311
|
+
process.cwdKey = basename(dirname(dirname(metaPath)));
|
|
312
|
+
}
|
|
313
|
+
return process;
|
|
322
314
|
}
|
|
323
315
|
|
|
324
316
|
private writeProcess(process: StoredProcess): void {
|
|
325
|
-
const processDir = this.
|
|
317
|
+
const processDir = this.resolveStoredProcessDir(process);
|
|
326
318
|
mkdirSync(processDir, { recursive: true });
|
|
327
319
|
writeFileSync(this.resolveMetaPath(processDir), JSON.stringify(process, null, 2));
|
|
328
320
|
}
|
|
@@ -347,7 +339,18 @@ export class CliProcessService {
|
|
|
347
339
|
}
|
|
348
340
|
|
|
349
341
|
private resolveProcessDir(cwd: string, pid: number): string {
|
|
350
|
-
return join(this.resolveCwdsDir(),
|
|
342
|
+
return join(this.resolveCwdsDir(), this.resolveCwdKey(cwd), String(pid));
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
private resolveStoredProcessDir(process: StoredProcess): string {
|
|
346
|
+
if (!process.cwdKey) {
|
|
347
|
+
process.cwdKey = this.resolveCwdKey(process.workFolder);
|
|
348
|
+
}
|
|
349
|
+
return join(this.resolveCwdsDir(), process.cwdKey, String(process.pid));
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
private resolveCwdKey(cwd: string): string {
|
|
353
|
+
return normalizeCwdForStorage(realpathSync(cwd));
|
|
351
354
|
}
|
|
352
355
|
|
|
353
356
|
private resolveMetaPath(processDir: string): string {
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import type { AgentType, ProcessStatus } from './process-service.js';
|
|
2
|
+
|
|
3
|
+
interface ProcessResultContext {
|
|
4
|
+
pid: number;
|
|
5
|
+
agent: AgentType;
|
|
6
|
+
status: ProcessStatus;
|
|
7
|
+
exitCode?: number;
|
|
8
|
+
startTime: string;
|
|
9
|
+
workFolder: string;
|
|
10
|
+
prompt: string;
|
|
11
|
+
model?: string;
|
|
12
|
+
stdout: string;
|
|
13
|
+
stderr: string;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function compactAgentOutput(agentOutput: any): any | null {
|
|
17
|
+
if (!agentOutput || typeof agentOutput !== 'object') {
|
|
18
|
+
return null;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const { tools: _tools, ...rest } = agentOutput;
|
|
22
|
+
const compact = Object.fromEntries(Object.entries(rest).filter(([, value]) => value !== undefined && value !== null));
|
|
23
|
+
return Object.keys(compact).length > 0 ? compact : null;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function hasMeaningfulParsedOutput(agentOutput: any): boolean {
|
|
27
|
+
if (!agentOutput || typeof agentOutput !== 'object') {
|
|
28
|
+
return false;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
return Object.entries(agentOutput).some(([key, value]) => {
|
|
32
|
+
if (value === undefined || value === null) {
|
|
33
|
+
return false;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
if (key === 'session_id') {
|
|
37
|
+
return false;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
if (key === 'tools') {
|
|
41
|
+
return Array.isArray(value) ? value.length > 0 : true;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
return true;
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export function buildProcessResult(context: ProcessResultContext, agentOutput: any, verbose = false): any {
|
|
49
|
+
const response: any = {
|
|
50
|
+
pid: context.pid,
|
|
51
|
+
agent: context.agent,
|
|
52
|
+
status: context.status,
|
|
53
|
+
exitCode: context.exitCode ?? null,
|
|
54
|
+
model: context.model ?? null,
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
if (verbose) {
|
|
58
|
+
response.startTime = context.startTime;
|
|
59
|
+
response.workFolder = context.workFolder;
|
|
60
|
+
response.prompt = context.prompt;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
if (agentOutput?.session_id) {
|
|
64
|
+
response.session_id = agentOutput.session_id;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const shapedAgentOutput = verbose ? agentOutput : compactAgentOutput(agentOutput);
|
|
68
|
+
|
|
69
|
+
if (hasMeaningfulParsedOutput(shapedAgentOutput)) {
|
|
70
|
+
response.agentOutput = shapedAgentOutput;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
if (!response.agentOutput) {
|
|
74
|
+
response.stdout = context.stdout;
|
|
75
|
+
response.stderr = context.stderr;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
return response;
|
|
79
|
+
}
|
package/src/process-service.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { spawn, type ChildProcess } from 'node:child_process';
|
|
2
2
|
import { buildCliCommand, type BuildCliCommandOptions } from './cli-builder.js';
|
|
3
3
|
import { parseClaudeOutput, parseCodexOutput, parseForgeOutput, parseGeminiOutput } from './parsers.js';
|
|
4
|
+
import { buildProcessResult } from './process-result.js';
|
|
4
5
|
|
|
5
6
|
export type AgentType = 'claude' | 'codex' | 'gemini' | 'forge';
|
|
6
7
|
export type ProcessStatus = 'running' | 'completed' | 'failed';
|
|
@@ -149,7 +150,7 @@ export class ProcessService {
|
|
|
149
150
|
}
|
|
150
151
|
}
|
|
151
152
|
|
|
152
|
-
|
|
153
|
+
return buildProcessResult({
|
|
153
154
|
pid,
|
|
154
155
|
agent: process.toolType,
|
|
155
156
|
status: process.status,
|
|
@@ -158,28 +159,12 @@ export class ProcessService {
|
|
|
158
159
|
workFolder: process.workFolder,
|
|
159
160
|
prompt: process.prompt,
|
|
160
161
|
model: process.model,
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
if (!verbose && agentOutput.tools) {
|
|
165
|
-
const { tools, ...rest } = agentOutput;
|
|
166
|
-
response.agentOutput = rest;
|
|
167
|
-
} else {
|
|
168
|
-
response.agentOutput = agentOutput;
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
if (agentOutput.session_id) {
|
|
172
|
-
response.session_id = agentOutput.session_id;
|
|
173
|
-
}
|
|
174
|
-
} else {
|
|
175
|
-
response.stdout = process.stdout;
|
|
176
|
-
response.stderr = process.stderr;
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
return response;
|
|
162
|
+
stdout: process.stdout,
|
|
163
|
+
stderr: process.stderr,
|
|
164
|
+
}, agentOutput, verbose);
|
|
180
165
|
}
|
|
181
166
|
|
|
182
|
-
async waitForProcesses(pids: number[], timeoutSeconds = 180): Promise<any[]> {
|
|
167
|
+
async waitForProcesses(pids: number[], timeoutSeconds = 180, verbose = false): Promise<any[]> {
|
|
183
168
|
for (const pid of pids) {
|
|
184
169
|
if (!this.processManager.has(pid)) {
|
|
185
170
|
throw new Error(`Process with PID ${pid} not found`);
|
|
@@ -211,7 +196,7 @@ export class ProcessService {
|
|
|
211
196
|
|
|
212
197
|
try {
|
|
213
198
|
await Promise.race([Promise.all(waitPromises), timeoutPromise]);
|
|
214
|
-
return pids.map((pid) => this.getProcessResult(pid,
|
|
199
|
+
return pids.map((pid) => this.getProcessResult(pid, verbose));
|
|
215
200
|
} finally {
|
|
216
201
|
if (timeoutHandle) {
|
|
217
202
|
clearTimeout(timeoutHandle);
|