import { memo, useState } from 'react'; import { Streamdown } from 'streamdown'; import { Wrench, Eye, FileText, Edit, Package, PackageMinus, GitCommit, BookOpen, Download, Hash, Tag, Network, List, Plus, Terminal, Globe, CheckCircle } from 'lucide-react'; import type { AIMessage } from '@/lib/SessionManager'; import { cn } from '@/lib/utils'; import OpenAI from 'openai'; interface AIMessageItemProps { message: AIMessage; isCurrentlyLoading?: boolean; toolCall?: OpenAI.Chat.Completions.ChatCompletionMessageToolCall | undefined; // Tool call data passed from the assistant message } export const AIMessageItem = memo(({ message, isCurrentlyLoading = false, toolCall }: AIMessageItemProps) => { const [isToolExpanded, setIsToolExpanded] = useState(false); // Get content to display const getContent = () => { if (typeof message.content === 'string') { return message.content; } if (Array.isArray(message.content)) { // Handle array content (like images, text blocks) return message.content .map(item => { if (typeof item === 'string') return item; if (item.type === 'text') return item.text; if (item.type === 'image_url') return `[Image: ${item.image_url.url}]`; return '[Unknown content]'; }) .join('\n'); } return ''; }; // Get tool info including icon and title 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 icons and titles based on tool name and arguments switch (toolName) { case 'shell': return { icon: Terminal, title: args.command ? args.command : 'Shell Command' }; case 'text_editor_view': { const title = args.path ? (args.start_line || args.end_line ? `Viewed ${args.path} (lines ${args.start_line || 1}-${args.end_line || 'end'})` : `Viewed ${args.path}`) : 'Viewed File'; return { icon: Eye, title }; } case 'text_editor_write': return { icon: FileText, title: args.path ? `Wrote ${args.path}` : 'Wrote File' }; case 'text_editor_str_replace': return { icon: Edit, title: args.path ? `Edited ${args.path}` : 'Edited File' }; case 'npm_add_package': { const installTitle = args.name ? (args.dev ? `Installed ${args.name} (dev)` : `Installed ${args.name}`) : 'Installed Package'; return { icon: Package, title: installTitle }; } case 'npm_remove_package': return { icon: PackageMinus, title: args.name ? `Removed ${args.name}` : 'Removed Package' }; case 'git_commit': return { icon: GitCommit, title: args.message ? `Committed: ${args.message}` : 'Committed Changes' }; case 'build_project': return { icon: Package, title: 'Built Project' }; case 'nostr_read_nip': return { icon: BookOpen, title: args.nip ? `Read NIP-${args.nip}` : 'Read NIP' }; case 'nostr_fetch_event': return { icon: Download, title: args.identifier ? `Fetched ${args.identifier.slice(0, 16)}...` : 'Fetched Event' }; case 'nostr_read_kind': return { icon: Hash, title: args.kind !== undefined ? `Read Kind ${args.kind}` : 'Read Kind' }; case 'nostr_read_tag': return { icon: Tag, title: args.tag ? `Read Tag "${args.tag}"` : 'Read Tag' }; case 'nostr_read_protocol': return { icon: Network, title: args.doc ? `Read Protocol: ${args.doc}` : 'Read Protocol' }; case 'nostr_read_nips_index': return { icon: List, title: 'Read NIPs Index' }; case 'nostr_generate_kind': return { icon: Plus, title: args.range ? `Generated ${args.range} kind` : 'Generated Kind' }; case 'deploy_project': return { icon: Globe, title: args.deployServer ? `Deployed to ${args.deployServer}` : 'Deployed Project' }; case 'typecheck': return { icon: CheckCircle, title: 'TypeScript' }; default: return { icon: Wrench, title: toolName.replace(/_/g, ' ').replace(/\b\w/g, l => l.toUpperCase()) }; } } } // 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 { icon: Wrench, title: firstLine.replace(/^(Error |Tool |Executing |Result: )/i, '').trim() }; } return { icon: Wrench, title: 'Tool Result' }; } return { icon: Wrench, title: 'Tool' }; }; // Special rendering for tool messages if (message.role === 'tool') { const content = getContent(); const toolInfo = getToolInfo(); const IconComponent = toolInfo.icon; // Regular tool message rendering return (
{/* -mt-2 to make it snug with the previous assistant message */} {isToolExpanded && (
{content}
)}
); } // Regular rendering for non-tool messages if (message.role === 'user') { // User messages: right-aligned bubble without avatar/name return (
{getContent()}
); } // Assistant messages: left-aligned without avatar/name return (
{getContent()}
{/* Tool calls are now hidden from assistant messages */}
); }); AIMessageItem.displayName = 'AIMessageItem';