Skip to content

Commit 2e9d7c4

Browse files
committed
fix: ensure JSON output for all invoke CLI error paths
handleInvokeCLI now prints JSON for validation failures. The telemetry wrapper callback also catches throws from handleInvokeCLI and prints JSON before returning a failure Result.
1 parent 8a5b9a2 commit 2e9d7c4

1 file changed

Lines changed: 26 additions & 33 deletions

File tree

src/cli/commands/invoke/command.tsx

Lines changed: 26 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,13 @@ function resolveProtocol(options: InvokeOptions, projectProtocol?: string): stri
3939
async function handleInvokeCLI(options: InvokeOptions, preloadedContext?: InvokeContext): Promise<InvokeResult> {
4040
const validation = validateInvokeOptions(options);
4141
if (!validation.valid) {
42-
return { success: false, error: validation.error ?? 'Validation failed' };
42+
const result: InvokeResult = { success: false, error: validation.error ?? 'Validation failed' };
43+
if (options.json) {
44+
console.log(JSON.stringify(result));
45+
} else {
46+
console.error(result.error);
47+
}
48+
return result;
4349
}
4450

4551
let spinner: NodeJS.Timeout | undefined;
@@ -118,35 +124,6 @@ async function handleInvokeCLI(options: InvokeOptions, preloadedContext?: Invoke
118124
}
119125
}
120126

121-
function printInvokeResult(result: InvokeResult, options: InvokeOptions): void {
122-
if (options.json) {
123-
console.log(JSON.stringify(result));
124-
} else if (options.stream) {
125-
// Streaming already wrote to stdout, just show session and log path
126-
if (result.sessionId) {
127-
console.error(`\nSession: ${result.sessionId}`);
128-
console.error(`To resume: agentcore invoke --session-id ${result.sessionId}`);
129-
}
130-
if (result.logFilePath) {
131-
console.error(`Log: ${result.logFilePath}`);
132-
}
133-
} else {
134-
// Non-streaming, non-json: print provider info and response or error
135-
if (result.success && result.response) {
136-
console.log(result.response);
137-
} else if (!result.success && result.error) {
138-
console.error(result.error);
139-
}
140-
if (result.sessionId) {
141-
console.error(`\nSession: ${result.sessionId}`);
142-
console.error(`To resume: agentcore invoke --session-id ${result.sessionId}`);
143-
}
144-
if (result.logFilePath) {
145-
console.error(`Log: ${result.logFilePath}`);
146-
}
147-
}
148-
}
149-
150127
export const registerInvoke = (program: Command) => {
151128
program
152129
.command('invoke')
@@ -281,7 +258,13 @@ export const registerInvoke = (program: Command) => {
281258
},
282259
async (): Promise<Result> => {
283260
if (!resolved.success) {
284-
return { success: false, error: new ValidationError(resolved.error ?? 'Prompt resolution failed') };
261+
const error = resolved.error ?? 'Prompt resolution failed';
262+
if (cliOptions.json) {
263+
console.log(JSON.stringify({ success: false, error }));
264+
} else {
265+
console.error(error);
266+
}
267+
return { success: false, error: new ValidationError(error) };
285268
}
286269

287270
// Parse custom headers
@@ -321,8 +304,18 @@ export const registerInvoke = (program: Command) => {
321304
actorId: cliOptions.actorId,
322305
};
323306

324-
const invokeResult = await handleInvokeCLI(options, invokeContext);
325-
printInvokeResult(invokeResult, options);
307+
let invokeResult: InvokeResult;
308+
try {
309+
invokeResult = await handleInvokeCLI(options, invokeContext);
310+
} catch (err) {
311+
const msg = getErrorMessage(err);
312+
if (cliOptions.json) {
313+
console.log(JSON.stringify({ success: false, error: msg }));
314+
} else {
315+
console.error(msg);
316+
}
317+
return { success: false, error: err instanceof Error ? err : new Error(msg) };
318+
}
326319
if (invokeResult.success) {
327320
return { success: true };
328321
}

0 commit comments

Comments
 (0)