fix: tool ui again
This commit is contained in:
parent
3716ebcdd7
commit
9c18d28cbe
2 changed files with 75 additions and 24 deletions
|
|
@ -52,7 +52,7 @@ type TODO = any;
|
||||||
|
|
||||||
export interface StreamCallbacks {
|
export interface StreamCallbacks {
|
||||||
onContent?: (content: string) => void;
|
onContent?: (content: string) => void;
|
||||||
onToolStart?: (toolName: string) => void;
|
onToolStart?: (toolName: string, toolArgs?: any) => void;
|
||||||
onToolResult?: (result: string, toolName: string) => void;
|
onToolResult?: (result: string, toolName: string) => void;
|
||||||
onToolError?: (error: string, toolName?: string) => void;
|
onToolError?: (error: string, toolName?: string) => void;
|
||||||
}
|
}
|
||||||
|
|
@ -128,6 +128,7 @@ export async function streamChatResponse(
|
||||||
id: toolCallDelta.id,
|
id: toolCallDelta.id,
|
||||||
name: "",
|
name: "",
|
||||||
arguments: {},
|
arguments: {},
|
||||||
|
startNotified: false,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
currentToolCallId = toolCallDelta.id;
|
currentToolCallId = toolCallDelta.id;
|
||||||
|
|
@ -141,15 +142,7 @@ export async function streamChatResponse(
|
||||||
if (toolCall) {
|
if (toolCall) {
|
||||||
if (!toolCall.name) {
|
if (!toolCall.name) {
|
||||||
toolCall.name = toolCallDelta.function.name;
|
toolCall.name = toolCallDelta.function.name;
|
||||||
if (callbacks?.onToolStart) {
|
toolCall.startNotified = false;
|
||||||
callbacks.onToolStart(toolCall.name);
|
|
||||||
} else {
|
|
||||||
process.stdout.write(
|
|
||||||
`\n${chalk.yellow("[Using tool:")} ${chalk.yellow.bold(
|
|
||||||
toolCall.name
|
|
||||||
)}${chalk.yellow("]")}`
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -167,6 +160,20 @@ export async function streamChatResponse(
|
||||||
// Try to parse complete JSON
|
// Try to parse complete JSON
|
||||||
const parsed = JSON.parse(toolArguments);
|
const parsed = JSON.parse(toolArguments);
|
||||||
toolCall.arguments = parsed;
|
toolCall.arguments = parsed;
|
||||||
|
|
||||||
|
// Notify start if we haven't already and have both name and args
|
||||||
|
if (toolCall.name && !toolCall.startNotified) {
|
||||||
|
toolCall.startNotified = true;
|
||||||
|
if (callbacks?.onToolStart) {
|
||||||
|
callbacks.onToolStart(toolCall.name, toolCall.arguments);
|
||||||
|
} else {
|
||||||
|
process.stdout.write(
|
||||||
|
`\n${chalk.yellow("[Using tool:")} ${chalk.yellow.bold(
|
||||||
|
toolCall.name
|
||||||
|
)}${chalk.yellow("]")}`
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
// Not complete JSON yet, continue collecting
|
// Not complete JSON yet, continue collecting
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,8 +4,8 @@ import { ChatCompletionMessageParam } from "openai/resources.mjs";
|
||||||
import React, { useEffect, useState } from "react";
|
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 ToolResultSummary from "./ToolResultSummary.js";
|
import ToolResultSummary from "./ToolResultSummary.js";
|
||||||
|
import UserInput from "./UserInput.js";
|
||||||
|
|
||||||
interface TUIChatProps {
|
interface TUIChatProps {
|
||||||
assistant: ContinueClient["assistant"];
|
assistant: ContinueClient["assistant"];
|
||||||
|
|
@ -19,6 +19,7 @@ interface DisplayMessage {
|
||||||
isStreaming?: boolean;
|
isStreaming?: boolean;
|
||||||
messageType?: "tool-start" | "tool-result" | "tool-error" | "system";
|
messageType?: "tool-start" | "tool-result" | "tool-error" | "system";
|
||||||
toolName?: string;
|
toolName?: string;
|
||||||
|
toolResult?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
const TUIChat: React.FC<TUIChatProps> = ({
|
const TUIChat: React.FC<TUIChatProps> = ({
|
||||||
|
|
@ -116,12 +117,56 @@ const TUIChat: React.FC<TUIChatProps> = ({
|
||||||
return newMessages;
|
return newMessages;
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
onToolStart: (toolName: string) => {
|
onToolStart: (toolName: string, toolArgs?: any) => {
|
||||||
|
const formatToolCall = (name: string, args: any) => {
|
||||||
|
if (!args) return name;
|
||||||
|
|
||||||
|
switch (name) {
|
||||||
|
case "Read":
|
||||||
|
return `Read(${args.file_path || args.path || ""})`;
|
||||||
|
case "Write":
|
||||||
|
return `Write(${args.file_path || ""})`;
|
||||||
|
case "Edit":
|
||||||
|
case "MultiEdit":
|
||||||
|
return `Edit(${args.file_path || ""})`;
|
||||||
|
case "Bash":
|
||||||
|
return `Bash(${args.command || ""})`;
|
||||||
|
case "Glob":
|
||||||
|
return `Glob(${args.pattern || ""})`;
|
||||||
|
case "Grep":
|
||||||
|
return `Grep(${args.pattern || ""})`;
|
||||||
|
case "LS":
|
||||||
|
return `LS(${args.path || ""})`;
|
||||||
|
case "NotebookRead":
|
||||||
|
return `NotebookRead(${args.notebook_path || ""})`;
|
||||||
|
case "NotebookEdit":
|
||||||
|
return `NotebookEdit(${args.notebook_path || ""})`;
|
||||||
|
case "WebFetch":
|
||||||
|
return `WebFetch(${args.url || ""})`;
|
||||||
|
case "WebSearch":
|
||||||
|
return `WebSearch(${args.query || ""})`;
|
||||||
|
case "TodoWrite":
|
||||||
|
return `TodoWrite(${args.todos?.length || 0} items)`;
|
||||||
|
case "Task":
|
||||||
|
return `Task(${args.description || ""})`;
|
||||||
|
default:
|
||||||
|
// Handle MCP tools or unknown tools
|
||||||
|
if (name.startsWith("mcp__")) {
|
||||||
|
const mcpToolName = name.replace("mcp__", "");
|
||||||
|
const firstParam = Object.values(args)[0];
|
||||||
|
return `${mcpToolName}(${firstParam || ""})`;
|
||||||
|
}
|
||||||
|
// For unknown tools, show first parameter value
|
||||||
|
const firstValue = Object.values(args)[0];
|
||||||
|
return `${name}(${firstValue || ""})`;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
setMessages((prev) => [
|
setMessages((prev) => [
|
||||||
...prev,
|
...prev,
|
||||||
{
|
{
|
||||||
role: "system",
|
role: "system",
|
||||||
content: `Using ${toolName}...`,
|
content: formatToolCall(toolName, toolArgs),
|
||||||
messageType: "tool-start",
|
messageType: "tool-start",
|
||||||
toolName,
|
toolName,
|
||||||
},
|
},
|
||||||
|
|
@ -132,11 +177,14 @@ const TUIChat: React.FC<TUIChatProps> = ({
|
||||||
const newMessages = [...prev];
|
const newMessages = [...prev];
|
||||||
// Find the last tool-start message for this tool and replace it
|
// Find the last tool-start message for this tool and replace it
|
||||||
for (let i = newMessages.length - 1; i >= 0; i--) {
|
for (let i = newMessages.length - 1; i >= 0; i--) {
|
||||||
if (newMessages[i].messageType === "tool-start" && newMessages[i].toolName === toolName) {
|
if (
|
||||||
|
newMessages[i].messageType === "tool-start" &&
|
||||||
|
newMessages[i].toolName === toolName
|
||||||
|
) {
|
||||||
newMessages[i] = {
|
newMessages[i] = {
|
||||||
...newMessages[i],
|
...newMessages[i],
|
||||||
content: result,
|
|
||||||
messageType: "tool-result",
|
messageType: "tool-result",
|
||||||
|
toolResult: result, // Store the actual result separately
|
||||||
};
|
};
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
@ -218,9 +266,7 @@ const TUIChat: React.FC<TUIChatProps> = ({
|
||||||
case "tool-start":
|
case "tool-start":
|
||||||
return (
|
return (
|
||||||
<Box key={index} marginBottom={1} paddingLeft={2}>
|
<Box key={index} marginBottom={1} paddingLeft={2}>
|
||||||
<Text color="white">
|
<Text color="white">○ {message.content}</Text>
|
||||||
○ {message.content}
|
|
||||||
</Text>
|
|
||||||
</Box>
|
</Box>
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
@ -233,14 +279,12 @@ const TUIChat: React.FC<TUIChatProps> = ({
|
||||||
flexDirection="column"
|
flexDirection="column"
|
||||||
>
|
>
|
||||||
<Box>
|
<Box>
|
||||||
<Text color="white">
|
<Text color="green">● {message.content}</Text>
|
||||||
✓ Completed {message.toolName}
|
|
||||||
</Text>
|
|
||||||
</Box>
|
</Box>
|
||||||
<Box marginLeft={2} paddingTop={1}>
|
<Box marginLeft={2} paddingTop={1}>
|
||||||
<ToolResultSummary
|
<ToolResultSummary
|
||||||
toolName={message.toolName}
|
toolName={message.toolName}
|
||||||
content={message.content}
|
content={message.toolResult || ""}
|
||||||
/>
|
/>
|
||||||
</Box>
|
</Box>
|
||||||
</Box>
|
</Box>
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue