fix(desktop): show output for failed tool calls (#11940)

This commit is contained in:
Lifei Zhou
2026-09-09 20:54:00 +00:00
committed by GitHub
parent e74a4035b4
commit ce3b1e7e0e
4 changed files with 63 additions and 36 deletions
@@ -507,7 +507,7 @@ describe('createAcpSessionNotificationAdapter', () => {
});
});
it('maps failed tool responses to error results', () => {
it('preserves failed tool responses as results with error status', () => {
const adapter = createAcpSessionNotificationAdapter();
const failedToolStateChanges = adapter.apply(
@@ -527,8 +527,12 @@ describe('createAcpSessionNotificationAdapter', () => {
type: 'toolResponse',
id: 'tool-1',
toolResult: {
status: 'error',
error: 'permission denied',
status: 'success',
value: {
content: [{ type: 'text', text: 'permission denied' }],
structuredContent: 'permission denied',
isError: true,
},
},
metadata: {
title: 'Read file',
@@ -687,8 +691,11 @@ describe('createAcpSessionNotificationAdapter', () => {
content,
},
toolResult: {
status: 'error',
error: 'file not found',
status: 'success',
value: {
content: [{ type: 'text', text: 'file not found' }],
isError: true,
},
},
});
});
+21 -26
View File
@@ -80,13 +80,14 @@ export function applyToolCallUpdate(
message.content.push({
type: 'toolResponse',
id: update.toolCallId,
toolResult:
toolCallState.status === 'failed'
? { status: 'error', error: toolError(toolCallState) }
: {
status: 'success',
value: toolResultValue(toolCallState, mcpAppMetadata(update)),
},
toolResult: {
status: 'success',
value: toolResultValue(
toolCallState,
mcpAppMetadata(update),
toolCallState.status === 'failed'
),
},
...(metadata ? { metadata } : {}),
});
@@ -210,11 +211,21 @@ function baseToolMetadata(
function toolResultValue(
update: ToolCallUpdate,
mcpAppMeta: DesktopMcpAppMeta | undefined
mcpAppMeta: DesktopMcpAppMeta | undefined,
isError: boolean
): ToolResultValue {
const content = toolResultContent(update);
if (isError && content.length === 0) {
const errorText =
typeof update.rawOutput === 'string' && update.rawOutput.trim()
? update.rawOutput
: (update.title ?? 'Tool call failed');
content.push({ type: 'text', text: errorText });
}
const toolResult: ToolResultValue = {
content: toolResultContent(update),
isError: false,
content,
isError,
...(mcpAppMeta ? { _meta: mcpAppMeta } : {}),
};
@@ -315,22 +326,6 @@ function apiResourceContentsFromAcpResource(
};
}
function toolError(update: ToolCallUpdate): string {
if (typeof update.rawOutput === 'string' && update.rawOutput.trim()) {
return update.rawOutput;
}
const contentText = toolResultContent(update)
.flatMap((content) => (content.type === 'text' ? [content.text] : []))
.filter((text) => text.trim().length > 0)
.join('\n');
if (contentText) {
return contentText;
}
return update.title ?? 'Tool call failed';
}
interface DesktopMcpAppMeta extends Record<string, unknown> {
ui: {
resourceUri: string;
@@ -69,6 +69,23 @@ const toolResponse: ToolResponseMessageContent = {
},
};
const failedToolResponse: ToolResponseMessageContent = {
type: 'toolResponse',
id: 'tool-1',
toolResult: {
status: 'success',
value: {
content: [
{
type: 'text',
text: 'this output is important',
},
],
isError: true,
},
},
};
function renderToolCall(response?: ToolResponseMessageContent) {
return render(
<ToolCallWithResponse
@@ -109,6 +126,12 @@ describe('ToolCallWithResponse live output', () => {
expect(await screen.findByText('final result')).toBeInTheDocument();
});
it('renders output from a failed tool call', async () => {
renderToolCall(failedToolResponse);
expect(await screen.findByText('this output is important')).toBeInTheDocument();
});
it('passes the current ACP permission generation through inline approval', async () => {
const permissionMessage: Message = {
content: [
@@ -141,6 +141,9 @@ function getSubagentSessionId(
}
function getToolResultContent(toolResult: Record<string, unknown>): ContentBlock[] {
if (toolResult.status === 'error') {
return typeof toolResult.error === 'string' ? [{ type: 'text', text: toolResult.error }] : [];
}
if (toolResult.status !== 'success') {
return [];
}
@@ -534,12 +537,14 @@ function ToolCallView({
// This is a workaround for cases where the backend doesn't send tool responses
const isStreamingComplete = !isStreamingMessage;
const shouldShowAsComplete = isStreamingComplete && !toolResponse;
const toolResult = toolResponse?.toolResult as Record<string, unknown> | undefined;
const toolResultValue = toolResult?.value as ToolResultValue | undefined;
const loadingStatus: LoadingStatus = !toolResponse
? shouldShowAsComplete
? 'success'
: 'loading'
: (toolResponse.toolResult as Record<string, unknown>).status === 'error'
: toolResult?.status === 'error' || toolResultValue?.isError
? 'error'
: 'success';
@@ -553,10 +558,7 @@ function ToolCallView({
}
}, [toolResponse, startTime]);
const toolResults =
loadingStatus === 'success' && toolResponse?.toolResult
? getToolResultContent(toolResponse.toolResult)
: [];
const toolResults = toolResult ? getToolResultContent(toolResult) : [];
const liveOutput = toolResponse ? '' : liveOutputToString(notifications);
const logs = notifications