Improve UI for tool calls

This commit is contained in:
Alex Gleason committed 2025-08-24 20:53:47 -05:00
1 parent 6c0bab27c0
commit d0d85f26b2
2 files changed
+160 -32

No files matched your search

+136 -24
View File
@@ -1,9 +1,11 @@
import { memo } from 'react';
import { memo, useState } from 'react';
import { Streamdown } from 'streamdown';
import { Avatar, AvatarFallback, AvatarImage } from '@/components/ui/avatar';
import { Bot, User, Square, Wrench } from 'lucide-react';
import { Bot, User, Square, Wrench, ChevronDown, ChevronRight } from 'lucide-react';
import { Button } from '@/components/ui/button';
import { AIMessage } from '@/hooks/useAIChat';
import { cn } from '@/lib/utils';
import OpenAI from 'openai';
interface AIMessageItemProps {
message: AIMessage;
@@ -11,6 +13,7 @@ interface AIMessageItemProps {
userProfileImage?: string;
isCurrentlyLoading?: boolean;
onStopGeneration?: () => void;
toolCall?: OpenAI.Chat.Completions.ChatCompletionMessageToolCall | undefined; // Tool call data passed from the assistant message
}
export const AIMessageItem = memo(({
@@ -18,8 +21,11 @@ export const AIMessageItem = memo(({
userDisplayName = 'You',
userProfileImage,
isCurrentlyLoading = false,
onStopGeneration
onStopGeneration,
toolCall
}: AIMessageItemProps) => {
const [isToolExpanded, setIsToolExpanded] = useState(false);
// Get display name based on role
const getDisplayName = () => {
switch (message.role) {
@@ -71,6 +77,132 @@ export const AIMessageItem = memo(({
return '';
};
// Get tool name and description from tool call data
const getToolInfo = () => {
if (message.role === 'tool' && toolCall) {
if (toolCall.type === 'function') {
const toolName = toolCall.function.name;
let args;
try {
args = JSON.parse(toolCall.function.arguments);
} catch {
args = {};
}
// Generate nice titles based on tool name and arguments
switch (toolName) {
case 'text_editor_view':
return {
title: args.path ? `View ${args.path}` : 'View File',
subtitle: args.start_line || args.end_line ?
`Lines ${args.start_line || 1}-${args.end_line || 'end'}` :
'Full file'
};
case 'text_editor_write':
return {
title: args.path ? `Write ${args.path}` : 'Write File',
subtitle: 'Create or overwrite file'
};
case 'text_editor_str_replace':
return {
title: args.path ? `Edit ${args.path}` : 'Edit File',
subtitle: 'Replace text'
};
case 'npm_add_package':
return {
title: args.name ? `Install ${args.name}` : 'Install Package',
subtitle: args.dev ? 'Dev dependency' : 'Dependency'
};
case 'npm_remove_package':
return {
title: args.name ? `Remove ${args.name}` : 'Remove Package',
subtitle: 'Uninstall package'
};
case 'git_commit':
return {
title: 'Git Commit',
subtitle: args.message || 'Commit changes'
};
default:
return {
title: toolName.replace(/_/g, ' ').replace(/\b\w/g, l => l.toUpperCase()),
subtitle: 'Tool execution'
};
}
}
}
// Fallback to extracting from content
if (message.role === 'tool') {
const content = getContent();
const lines = content.split('\n');
const firstLine = lines[0];
if (firstLine && firstLine.length < 100) {
return {
title: firstLine.replace(/^(Error |Tool |Executing |Result: )/i, '').trim(),
subtitle: 'Tool result'
};
}
return {
title: 'Tool Result',
subtitle: 'Execution result'
};
}
return {
title: 'Tool',
subtitle: ''
};
};
// Special rendering for tool messages
if (message.role === 'tool') {
const content = getContent();
const toolInfo = getToolInfo();
return (
<div className="ml-11 -mt-2"> {/* ml-11 to align with assistant message content, -mt-2 to make it snug */}
<button
onClick={() => setIsToolExpanded(!isToolExpanded)}
className={cn(
"w-full flex items-center gap-2 px-3 py-1.5 text-xs",
"bg-muted/50 hover:bg-muted/70 rounded border",
"transition-colors duration-200"
)}
>
{isToolExpanded ? (
<ChevronDown className="h-3 w-3 text-muted-foreground" />
) : (
<ChevronRight className="h-3 w-3 text-muted-foreground" />
)}
<Wrench className="h-3 w-3 text-muted-foreground" />
<div className="flex flex-col items-start min-w-0 flex-1">
<span className="text-muted-foreground font-medium truncate">
{toolInfo.title}
</span>
{toolInfo.subtitle && (
<span className="text-muted-foreground/70 text-xs truncate">
{toolInfo.subtitle}
</span>
)}
</div>
</button>
{isToolExpanded && (
<div className="mt-1 p-3 bg-muted/30 rounded border text-xs">
<div className="whitespace-pre-wrap break-words font-mono">
{content}
</div>
</div>
)}
</div>
);
}
// Regular rendering for non-tool messages
return (
<div className="flex gap-3">
<Avatar className="h-8 w-8 flex-shrink-0">
@@ -81,12 +213,6 @@ export const AIMessageItem = memo(({
<User className="h-4 w-4" />
</AvatarFallback>
</>
) : message.role === 'tool' ? (
<>
<AvatarFallback>
<Wrench className="h-4 w-4" />
</AvatarFallback>
</>
) : (
<>
<AvatarFallback>
@@ -125,21 +251,7 @@ export const AIMessageItem = memo(({
</Streamdown>
</div>
{/* Display tool calls if present */}
{'tool_calls' in message && message.tool_calls && (
<div className="mt-2 space-y-2">
{message.tool_calls.map((toolCall, index) => (
<div key={index} className="bg-muted p-2 rounded text-xs">
<div className="font-medium">
Tool Call: {toolCall.type === 'function' ? toolCall.function.name : 'Unknown'}
</div>
<div className="text-muted-foreground">
{toolCall.type === 'function' ? toolCall.function.arguments : 'N/A'}
</div>
</div>
))}
</div>
)}
{/* Tool calls are now hidden from assistant messages */}
</div>
</div>
</div>
+24 -8
View File
@@ -353,14 +353,30 @@ BASE_DOMAIN=nostrdeploy.com`);
<div className="flex-1 overflow-y-scroll overflow-x-hidden" ref={scrollAreaRef}>
<div className="p-4 space-y-4">
{messages.map((message, index) => (
<AIMessageItem
key={`${index}-${message.role}-${typeof message.content === 'string' ? message.content.slice(0, 50) : 'content'}`}
message={message}
userDisplayName="You"
onStopGeneration={stopGeneration}
/>
))}
{messages.map((message, index) => {
// Find the corresponding tool call for tool messages
let toolCall: OpenAI.Chat.Completions.ChatCompletionMessageToolCall | undefined = undefined;
if (message.role === 'tool' && 'tool_call_id' in message) {
// Look backwards to find the assistant message with matching tool call
for (let i = index - 1; i >= 0; i--) {
const prevMessage = messages[i];
if (prevMessage.role === 'assistant' && 'tool_calls' in prevMessage && prevMessage.tool_calls) {
toolCall = prevMessage.tool_calls.find(tc => tc.id === message.tool_call_id);
if (toolCall) break;
}
}
}
return (
<AIMessageItem
key={`${index}-${message.role}-${typeof message.content === 'string' ? message.content.slice(0, 50) : 'content'}`}
message={message}
userDisplayName="You"
onStopGeneration={stopGeneration}
toolCall={toolCall}
/>
);
})}
{streamingMessage && (
<AIMessageItem
key="streaming-message"