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 ( +
{/* ml-11 to align with assistant message content, -mt-2 to make it snug */} + + + {isToolExpanded && ( +
+
+ {content} +
+
+ )} +
+ ); + } + + // Regular rendering for non-tool messages return (
@@ -81,12 +213,6 @@ export const AIMessageItem = memo(({ - ) : message.role === 'tool' ? ( - <> - - - - ) : ( <> @@ -125,21 +251,7 @@ export const AIMessageItem = memo(({
- {/* Display tool calls if present */} - {'tool_calls' in message && message.tool_calls && ( -
- {message.tool_calls.map((toolCall, index) => ( -
-
- Tool Call: {toolCall.type === 'function' ? toolCall.function.name : 'Unknown'} -
-
- {toolCall.type === 'function' ? toolCall.function.arguments : 'N/A'} -
-
- ))} -
- )} + {/* Tool calls are now hidden from assistant messages */} diff --git a/src/components/Shakespeare/ChatPane.tsx b/src/components/Shakespeare/ChatPane.tsx index ccc9c2c6..68597f0b 100644 --- a/src/components/Shakespeare/ChatPane.tsx +++ b/src/components/Shakespeare/ChatPane.tsx @@ -353,14 +353,30 @@ BASE_DOMAIN=nostrdeploy.com`);
- {messages.map((message, index) => ( - - ))} + {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 ( + + ); + })} {streamingMessage && (