diff --git a/src/components/AIMessageItem.tsx b/src/components/AIMessageItem.tsx index 7097de52..0ffaa82c 100644 --- a/src/components/AIMessageItem.tsx +++ b/src/components/AIMessageItem.tsx @@ -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 ( +