|
| 1 | +import React, { useRef, useEffect } from 'react' |
| 2 | +import { IconDocument, IconSearch, IconTerminal, IconBrain, IconCode } from '@posthog/icons' |
| 3 | +import { ConversationItem } from './types' |
| 4 | + |
| 5 | +const toolIcons: Record<string, React.ComponentType<{ className?: string }>> = { |
| 6 | + Read: IconDocument, |
| 7 | + Grep: IconSearch, |
| 8 | + Bash: IconTerminal, |
| 9 | + Edit: IconCode, |
| 10 | + PostHog: IconSearch, |
| 11 | +} |
| 12 | + |
| 13 | +function UserMessage({ content }: { content: React.ReactNode }) { |
| 14 | + return ( |
| 15 | + <div className="border-l-2 border-red dark:border-yellow bg-accent py-2 pl-3 pr-2"> |
| 16 | + <div className="font-medium text-sm [&>*:last-child]:mb-0">{content}</div> |
| 17 | + </div> |
| 18 | + ) |
| 19 | +} |
| 20 | + |
| 21 | +function ToolCall({ |
| 22 | + toolName, |
| 23 | + toolDetail, |
| 24 | + expanded, |
| 25 | +}: { |
| 26 | + toolName?: string |
| 27 | + toolDetail?: string |
| 28 | + expanded?: React.ReactNode |
| 29 | +}) { |
| 30 | + const Icon = toolName ? toolIcons[toolName] || IconTerminal : IconTerminal |
| 31 | + |
| 32 | + return ( |
| 33 | + <div className="pl-3 py-0.5"> |
| 34 | + <div className="flex items-center gap-2"> |
| 35 | + <Icon className="size-3 text-muted shrink-0" /> |
| 36 | + <span className="text-xs text-muted font-code truncate"> |
| 37 | + {toolName} |
| 38 | + {toolDetail && <span className="ml-1 opacity-75">{toolDetail}</span>} |
| 39 | + </span> |
| 40 | + </div> |
| 41 | + {expanded && ( |
| 42 | + <div className="mt-2 ml-5 max-w-4xl overflow-hidden rounded-sm border border-input">{expanded}</div> |
| 43 | + )} |
| 44 | + </div> |
| 45 | + ) |
| 46 | +} |
| 47 | + |
| 48 | +function ThinkBlock({ content }: { content: React.ReactNode }) { |
| 49 | + return ( |
| 50 | + <div className="my-2 max-w-4xl overflow-hidden rounded-sm border border-input bg-primary"> |
| 51 | + <div className="px-3 py-2 flex items-center gap-2"> |
| 52 | + <IconBrain className="size-3 text-muted shrink-0" /> |
| 53 | + <span className="text-xs text-muted font-code">Thinking...</span> |
| 54 | + </div> |
| 55 | + {content && ( |
| 56 | + <div className="px-3 py-2 border-t border-input"> |
| 57 | + <p className="text-xs text-muted font-code m-0 whitespace-pre-wrap">{content}</p> |
| 58 | + </div> |
| 59 | + )} |
| 60 | + </div> |
| 61 | + ) |
| 62 | +} |
| 63 | + |
| 64 | +function AgentMessage({ content }: { content: React.ReactNode }) { |
| 65 | + return ( |
| 66 | + <div className="py-1 pl-3 pr-2"> |
| 67 | + <div className="text-sm [&>*:last-child]:mb-0 [&_p]:mb-2 [&_ul]:mb-2 [&_ul]:pl-4 [&_ul]:list-disc [&_li]:mb-1 [&_strong]:font-semibold [&_code]:font-code [&_code]:text-xs [&_code]:bg-accent [&_code]:px-1 [&_code]:py-0.5 [&_code]:rounded-sm [&_code]:border [&_code]:border-input"> |
| 68 | + {content} |
| 69 | + </div> |
| 70 | + </div> |
| 71 | + ) |
| 72 | +} |
| 73 | + |
| 74 | +function LoadingIndicator() { |
| 75 | + return ( |
| 76 | + <div className="pl-3 py-1.5 flex items-center gap-2"> |
| 77 | + <span className="text-xs text-muted font-code animate-pulse">Thinking...</span> |
| 78 | + </div> |
| 79 | + ) |
| 80 | +} |
| 81 | + |
| 82 | +interface CodeConversationProps { |
| 83 | + conversation: ConversationItem[] |
| 84 | + activeSection: number |
| 85 | +} |
| 86 | + |
| 87 | +export default function CodeConversation({ conversation, activeSection }: CodeConversationProps) { |
| 88 | + const scrollRef = useRef<HTMLDivElement>(null) |
| 89 | + |
| 90 | + // Auto-scroll to bottom when conversation updates |
| 91 | + useEffect(() => { |
| 92 | + const el = scrollRef.current |
| 93 | + if (el) { |
| 94 | + el.scrollTop = el.scrollHeight |
| 95 | + } |
| 96 | + }, [conversation.length]) |
| 97 | + |
| 98 | + return ( |
| 99 | + <div ref={scrollRef} key={activeSection} className="flex-1 overflow-y-auto bg-primary"> |
| 100 | + <div className="pb-16"> |
| 101 | + {conversation.map((item, index) => ( |
| 102 | + <div key={index} className="mx-auto max-w-[750px] px-2 py-1.5"> |
| 103 | + {item.type === 'user' && <UserMessage content={item.content} />} |
| 104 | + {item.type === 'tool' && ( |
| 105 | + <ToolCall toolName={item.toolName} toolDetail={item.toolDetail} expanded={item.expanded} /> |
| 106 | + )} |
| 107 | + {item.type === 'think' && <ThinkBlock content={item.content} />} |
| 108 | + {item.type === 'agent' && <AgentMessage content={item.content} />} |
| 109 | + {item.type === 'loading' && <LoadingIndicator />} |
| 110 | + </div> |
| 111 | + ))} |
| 112 | + </div> |
| 113 | + </div> |
| 114 | + ) |
| 115 | +} |
0 commit comments