fix: tool call custom UI
This commit is contained in:
parent
6749e10daa
commit
fa4b98f455
3 changed files with 106 additions and 7 deletions
|
|
@ -53,8 +53,8 @@ type TODO = any;
|
||||||
export interface StreamCallbacks {
|
export interface StreamCallbacks {
|
||||||
onContent?: (content: string) => void;
|
onContent?: (content: string) => void;
|
||||||
onToolStart?: (toolName: string) => void;
|
onToolStart?: (toolName: string) => void;
|
||||||
onToolResult?: (result: string) => void;
|
onToolResult?: (result: string, toolName: string) => void;
|
||||||
onToolError?: (error: string) => void;
|
onToolError?: (error: string, toolName?: string) => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Define a function to handle streaming responses with tool calling
|
// Define a function to handle streaming responses with tool calling
|
||||||
|
|
@ -220,7 +220,7 @@ export async function streamChatResponse(
|
||||||
});
|
});
|
||||||
|
|
||||||
if (callbacks?.onToolResult) {
|
if (callbacks?.onToolResult) {
|
||||||
callbacks.onToolResult(toolResult);
|
callbacks.onToolResult(toolResult, toolCall.name);
|
||||||
} else {
|
} else {
|
||||||
console.info(chalk.green(toolResult) + "\n");
|
console.info(chalk.green(toolResult) + "\n");
|
||||||
}
|
}
|
||||||
|
|
@ -234,7 +234,7 @@ export async function streamChatResponse(
|
||||||
content: errorMessage,
|
content: errorMessage,
|
||||||
});
|
});
|
||||||
if (callbacks?.onToolError) {
|
if (callbacks?.onToolError) {
|
||||||
callbacks.onToolError(errorMessage);
|
callbacks.onToolError(errorMessage, toolCall.name);
|
||||||
} else {
|
} else {
|
||||||
console.info(
|
console.info(
|
||||||
`${chalk.red("[Tool error:")} ${chalk.red(errorMessage)}${chalk.red(
|
`${chalk.red("[Tool error:")} ${chalk.red(errorMessage)}${chalk.red(
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@ import React, { useEffect, useState } from "react";
|
||||||
import { handleSlashCommands } from "../slashCommands.js";
|
import { handleSlashCommands } from "../slashCommands.js";
|
||||||
import { StreamCallbacks, streamChatResponse } from "../streamChatResponse.js";
|
import { StreamCallbacks, streamChatResponse } from "../streamChatResponse.js";
|
||||||
import UserInput from "./UserInput.js";
|
import UserInput from "./UserInput.js";
|
||||||
|
import ToolResultSummary from "./ToolResultSummary.js";
|
||||||
|
|
||||||
interface TUIChatProps {
|
interface TUIChatProps {
|
||||||
assistant: ContinueClient["assistant"];
|
assistant: ContinueClient["assistant"];
|
||||||
|
|
@ -126,23 +127,25 @@ const TUIChat: React.FC<TUIChatProps> = ({
|
||||||
},
|
},
|
||||||
]);
|
]);
|
||||||
},
|
},
|
||||||
onToolResult: (result: string) => {
|
onToolResult: (result: string, toolName: string) => {
|
||||||
setMessages((prev) => [
|
setMessages((prev) => [
|
||||||
...prev,
|
...prev,
|
||||||
{
|
{
|
||||||
role: "system",
|
role: "system",
|
||||||
content: result,
|
content: result,
|
||||||
messageType: "tool-result",
|
messageType: "tool-result",
|
||||||
|
toolName,
|
||||||
},
|
},
|
||||||
]);
|
]);
|
||||||
},
|
},
|
||||||
onToolError: (error: string) => {
|
onToolError: (error: string, toolName?: string) => {
|
||||||
setMessages((prev) => [
|
setMessages((prev) => [
|
||||||
...prev,
|
...prev,
|
||||||
{
|
{
|
||||||
role: "system",
|
role: "system",
|
||||||
content: error,
|
content: error,
|
||||||
messageType: "tool-error",
|
messageType: "tool-error",
|
||||||
|
toolName,
|
||||||
},
|
},
|
||||||
]);
|
]);
|
||||||
},
|
},
|
||||||
|
|
@ -231,7 +234,10 @@ const TUIChat: React.FC<TUIChatProps> = ({
|
||||||
<Text color="green">Tool completed</Text>
|
<Text color="green">Tool completed</Text>
|
||||||
</Box>
|
</Box>
|
||||||
<Box marginLeft={2} paddingTop={1}>
|
<Box marginLeft={2} paddingTop={1}>
|
||||||
<Text color="gray">{message.content}</Text>
|
<ToolResultSummary
|
||||||
|
toolName={message.toolName}
|
||||||
|
content={message.content}
|
||||||
|
/>
|
||||||
</Box>
|
</Box>
|
||||||
</Box>
|
</Box>
|
||||||
);
|
);
|
||||||
|
|
|
||||||
93
src/ui/ToolResultSummary.tsx
Normal file
93
src/ui/ToolResultSummary.tsx
Normal 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;
|
||||||
Loading…
Add table
Reference in a new issue