add flags for markdown rendering and live updating to Tool to avoid special-casing shell tool by name, and open door for other tools to specify their rendering/updating (#629)

This commit is contained in:
Olcan 2025-05-30 13:59:05 -07:00 committed by GitHub
parent 0869fd168f
commit 1a5fd2ccb2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 57 additions and 54 deletions

View File

@ -288,41 +288,40 @@ export function useToolScheduler(
const callId = t.request.callId; const callId = t.request.callId;
setToolCalls(setStatus(t.request.callId, 'executing')); setToolCalls(setStatus(t.request.callId, 'executing'));
const updateOutput = const updateOutput = t.tool.canUpdateOutput
t.tool.name === 'execute_bash_command' ? (output: string) => {
? (output: string) => { setPendingHistoryItem(
setPendingHistoryItem( (prevItem: HistoryItemWithoutId | null) => {
(prevItem: HistoryItemWithoutId | null) => { if (prevItem?.type === 'tool_group') {
if (prevItem?.type === 'tool_group') { return {
return { ...prevItem,
...prevItem, tools: prevItem.tools.map(
tools: prevItem.tools.map( (toolDisplay: IndividualToolCallDisplay) =>
(toolDisplay: IndividualToolCallDisplay) => toolDisplay.callId === callId &&
toolDisplay.callId === callId && toolDisplay.status === ToolCallStatus.Executing
toolDisplay.status === ToolCallStatus.Executing ? {
? { ...toolDisplay,
...toolDisplay, resultDisplay: output,
resultDisplay: output, }
} : toolDisplay,
: toolDisplay, ),
), };
}; }
} return prevItem;
return prevItem; },
}, );
); // Also update the toolCall itself so that mapToDisplay
// Also update the toolCall itself so that mapToDisplay // can pick up the live output if the item is not pending
// can pick up the live output if the item is not pending // (e.g. if it's being re-rendered from history)
// (e.g. if it's being re-rendered from history) setToolCalls((prevToolCalls) =>
setToolCalls((prevToolCalls) => prevToolCalls.map((tc) =>
prevToolCalls.map((tc) => tc.request.callId === callId && tc.status === 'executing'
tc.request.callId === callId && tc.status === 'executing' ? { ...tc, liveOutput: output }
? { ...tc, liveOutput: output } : tc,
: tc, ),
), );
); }
} : undefined;
: undefined;
t.tool t.tool
.execute(t.request.args, signal, updateOutput) .execute(t.request.args, signal, updateOutput)
@ -541,18 +540,6 @@ export function mapToDisplay(
): HistoryItemToolGroup { ): HistoryItemToolGroup {
const tools = Array.isArray(tool) ? tool : [tool]; const tools = Array.isArray(tool) ? tool : [tool];
const toolsDisplays = tools.map((t): IndividualToolCallDisplay => { const toolsDisplays = tools.map((t): IndividualToolCallDisplay => {
// Determine if markdown rendering should be skipped for this tool
let renderOutputAsMarkdown = true; // Default to true
if (t.status === 'error') {
// For errors, the tool object might not be available, so check t.request.name
if (t.request.name === 'execute_bash_command') {
renderOutputAsMarkdown = false;
}
} else if ('tool' in t && t.tool?.name === 'execute_bash_command') {
// For other statuses, check t.tool.name if tool exists
renderOutputAsMarkdown = false;
}
switch (t.status) { switch (t.status) {
case 'success': case 'success':
return { return {
@ -562,7 +549,7 @@ export function mapToDisplay(
resultDisplay: t.response.resultDisplay, resultDisplay: t.response.resultDisplay,
status: mapStatus(t.status), status: mapStatus(t.status),
confirmationDetails: undefined, confirmationDetails: undefined,
renderOutputAsMarkdown, renderOutputAsMarkdown: t.tool.isOutputMarkdown,
}; };
case 'error': case 'error':
return { return {
@ -572,7 +559,7 @@ export function mapToDisplay(
resultDisplay: t.response.resultDisplay, resultDisplay: t.response.resultDisplay,
status: mapStatus(t.status), status: mapStatus(t.status),
confirmationDetails: undefined, confirmationDetails: undefined,
renderOutputAsMarkdown, renderOutputAsMarkdown: false,
}; };
case 'cancelled': case 'cancelled':
return { return {
@ -582,7 +569,7 @@ export function mapToDisplay(
resultDisplay: t.response.resultDisplay, resultDisplay: t.response.resultDisplay,
status: mapStatus(t.status), status: mapStatus(t.status),
confirmationDetails: undefined, confirmationDetails: undefined,
renderOutputAsMarkdown, renderOutputAsMarkdown: t.tool.isOutputMarkdown,
}; };
case 'awaiting_approval': case 'awaiting_approval':
return { return {
@ -592,7 +579,7 @@ export function mapToDisplay(
resultDisplay: undefined, resultDisplay: undefined,
status: mapStatus(t.status), status: mapStatus(t.status),
confirmationDetails: t.confirmationDetails, confirmationDetails: t.confirmationDetails,
renderOutputAsMarkdown, renderOutputAsMarkdown: t.tool.isOutputMarkdown,
}; };
case 'executing': case 'executing':
return { return {
@ -602,7 +589,7 @@ export function mapToDisplay(
resultDisplay: t.liveOutput ?? undefined, resultDisplay: t.liveOutput ?? undefined,
status: mapStatus(t.status), status: mapStatus(t.status),
confirmationDetails: undefined, confirmationDetails: undefined,
renderOutputAsMarkdown, renderOutputAsMarkdown: t.tool.isOutputMarkdown,
}; };
case 'validating': // Add this case case 'validating': // Add this case
return { return {
@ -612,7 +599,7 @@ export function mapToDisplay(
resultDisplay: undefined, resultDisplay: undefined,
status: mapStatus(t.status), status: mapStatus(t.status),
confirmationDetails: undefined, confirmationDetails: undefined,
renderOutputAsMarkdown, renderOutputAsMarkdown: t.tool.isOutputMarkdown,
}; };
case 'scheduled': case 'scheduled':
return { return {
@ -622,7 +609,7 @@ export function mapToDisplay(
resultDisplay: undefined, resultDisplay: undefined,
status: mapStatus(t.status), status: mapStatus(t.status),
confirmationDetails: undefined, confirmationDetails: undefined,
renderOutputAsMarkdown, renderOutputAsMarkdown: t.tool.isOutputMarkdown,
}; };
default: { default: {
// ensures every case is checked for above // ensures every case is checked for above

View File

@ -42,6 +42,8 @@ export class ShellTool extends BaseTool<ShellToolParams, ToolResult> {
toolDisplayName, toolDisplayName,
toolDescription, toolDescription,
toolParameterSchema, toolParameterSchema,
false, // output is not markdown
true, // output can be updated
); );
} }

View File

@ -33,6 +33,16 @@ export interface Tool<
*/ */
schema: FunctionDeclaration; schema: FunctionDeclaration;
/**
* Whether the tool's output should be rendered as markdown
*/
isOutputMarkdown: boolean;
/**
* Whether the tool supports live (streaming) output
*/
canUpdateOutput: boolean;
/** /**
* Validates the parameters for the tool * Validates the parameters for the tool
* Should be called from both `shouldConfirmExecute` and `execute` * Should be called from both `shouldConfirmExecute` and `execute`
@ -85,6 +95,8 @@ export abstract class BaseTool<
* @param name Internal name of the tool (used for API calls) * @param name Internal name of the tool (used for API calls)
* @param displayName User-friendly display name of the tool * @param displayName User-friendly display name of the tool
* @param description Description of what the tool does * @param description Description of what the tool does
* @param isOutputMarkdown Whether the tool's output should be rendered as markdown
* @param canUpdateOutput Whether the tool supports live (streaming) output
* @param parameterSchema JSON Schema defining the parameters * @param parameterSchema JSON Schema defining the parameters
*/ */
constructor( constructor(
@ -92,6 +104,8 @@ export abstract class BaseTool<
readonly displayName: string, readonly displayName: string,
readonly description: string, readonly description: string,
readonly parameterSchema: Record<string, unknown>, readonly parameterSchema: Record<string, unknown>,
readonly isOutputMarkdown: boolean = true,
readonly canUpdateOutput: boolean = false,
) {} ) {}
/** /**