fix: tool call custom UI

This commit is contained in:
Nate 2025-07-13 17:03:17 -07:00
parent 6749e10daa
commit fa4b98f455
3 changed files with 106 additions and 7 deletions

View file

@ -53,8 +53,8 @@ type TODO = any;
export interface StreamCallbacks {
onContent?: (content: string) => void;
onToolStart?: (toolName: string) => void;
onToolResult?: (result: string) => void;
onToolError?: (error: string) => void;
onToolResult?: (result: string, toolName: string) => void;
onToolError?: (error: string, toolName?: string) => void;
}
// Define a function to handle streaming responses with tool calling
@ -220,7 +220,7 @@ export async function streamChatResponse(
});
if (callbacks?.onToolResult) {
callbacks.onToolResult(toolResult);
callbacks.onToolResult(toolResult, toolCall.name);
} else {
console.info(chalk.green(toolResult) + "\n");
}
@ -234,7 +234,7 @@ export async function streamChatResponse(
content: errorMessage,
});
if (callbacks?.onToolError) {
callbacks.onToolError(errorMessage);
callbacks.onToolError(errorMessage, toolCall.name);
} else {
console.info(
`${chalk.red("[Tool error:")} ${chalk.red(errorMessage)}${chalk.red(

View file

@ -5,6 +5,7 @@ import React, { useEffect, useState } from "react";
import { handleSlashCommands } from "../slashCommands.js";
import { StreamCallbacks, streamChatResponse } from "../streamChatResponse.js";
import UserInput from "./UserInput.js";
import ToolResultSummary from "./ToolResultSummary.js";
interface TUIChatProps {
assistant: ContinueClient["assistant"];
@ -126,23 +127,25 @@ const TUIChat: React.FC<TUIChatProps> = ({
},
]);
},
onToolResult: (result: string) => {
onToolResult: (result: string, toolName: string) => {
setMessages((prev) => [
...prev,
{
role: "system",
content: result,
messageType: "tool-result",
toolName,
},
]);
},
onToolError: (error: string) => {
onToolError: (error: string, toolName?: string) => {
setMessages((prev) => [
...prev,
{
role: "system",
content: error,
messageType: "tool-error",
toolName,
},
]);
},
@ -231,7 +234,10 @@ const TUIChat: React.FC<TUIChatProps> = ({
<Text color="green">Tool completed</Text>
</Box>
<Box marginLeft={2} paddingTop={1}>
<Text color="gray">{message.content}</Text>
<ToolResultSummary
toolName={message.toolName}
content={message.content}
/>
</Box>
</Box>
);

View file

@ -0,0 +1,93 @@
import { Text, Box } from "ink";
import React from "react";
interface ToolResultSummaryProps {
toolName?: string;
content: string;
}
const ToolResultSummary: React.FC<ToolResultSummaryProps> = ({
toolName,
content,
}) => {
const getSummary = () => {
if (!content) return "No output";
const lines = content.split('\n').length;
const chars = content.length;
// Use exact tool name matching
switch (toolName) {
case "Read":
// Try to extract file path from content if it contains line numbers
if (content.includes("→")) {
const pathMatch = content.match(/^(.+?):/);
const filePath = pathMatch ? pathMatch[1] : "file";
return `Read ${filePath} (${lines} lines)`;
}
return `Read tool output (${lines} lines)`;
case "Bash":
return `Command output (${lines} lines)`;
case "Glob":
const fileCount = lines;
return `Found ${fileCount} ${fileCount === 1 ? 'file' : 'files'}`;
case "Grep":
return `Found ${lines} ${lines === 1 ? 'match' : 'matches'}`;
case "LS":
return `Listed ${lines} ${lines === 1 ? 'item' : 'items'}`;
case "Write":
return "File written successfully";
case "Edit":
case "MultiEdit":
return "File edited successfully";
case "NotebookRead":
return `Read notebook (${lines} lines)`;
case "NotebookEdit":
return "Notebook edited successfully";
case "WebFetch":
return `Fetched web content (${lines} lines)`;
case "WebSearch":
return `Search results (${lines} lines)`;
case "TodoWrite":
return "Todo list updated";
case "Task":
return `Task completed (${lines} lines)`;
default:
// Handle MCP tools or unknown tools
if (toolName?.startsWith("mcp__")) {
const mcpToolName = toolName.replace("mcp__", "");
return `${mcpToolName} tool output (${lines} lines)`;
}
// Fallback for unknown tools
if (chars > 1000) {
return `${toolName || 'Tool'} output: ${lines} lines, ${chars} characters`;
} else if (lines > 10) {
return `${toolName || 'Tool'} output: ${lines} lines`;
} else {
return content.slice(0, 100) + (content.length > 100 ? "..." : "");
}
}
};
return (
<Box>
<Text color="gray">{getSummary()}</Text>
</Box>
);
};
export default ToolResultSummary;