File size: 6,655 Bytes
6a50e97 d8e039b 6a50e97 d8e039b 6a50e97 d8e039b 6a50e97 d8e039b 6a50e97 d8e039b 6a50e97 d8e039b 6a50e97 d8e039b 6a50e97 d8e039b 6a50e97 d8e039b 6a50e97 d8e039b 6a50e97 d8e039b 6a50e97 d8e039b 6a50e97 d8e039b 6a50e97 d8e039b 6a50e97 d8e039b 6a50e97 d8e039b 6a50e97 d8e039b 6a50e97 d8e039b 6a50e97 d8e039b |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
import React from 'react'
import ReactMarkdown from 'react-markdown'
import { Button } from '@/components/ui/button'
import { Textarea } from '@/components/ui/textarea'
import { Card } from '@/components/ui/card'
import { Badge } from '@/components/ui/badge'
import { Message } from '@/types/chat'
import { Send, Square, Eye, EyeOff, Brain, User, Bot } from 'lucide-react'
interface ChatContainerProps {
messages: Message[]
input: string
setInput: (value: string) => void
onSubmit: () => void
onStop: () => void
isLoading: boolean
disabled?: boolean
placeholder?: string
}
export function ChatContainer({
messages,
input,
setInput,
onSubmit,
onStop,
isLoading,
disabled = false,
placeholder = "Type your message..."
}: ChatContainerProps) {
const [showThinking, setShowThinking] = React.useState<{ [key: string]: boolean }>({})
const handleKeyPress = (e: React.KeyboardEvent) => {
if (e.key === 'Enter' && !e.shiftKey) {
e.preventDefault()
if (!isLoading && !disabled) {
onSubmit()
}
}
}
const toggleThinking = (messageId: string) => {
setShowThinking(prev => ({
...prev,
[messageId]: !prev[messageId]
}))
}
return (
<div className="flex flex-col h-full">
{/* Messages */}
<div className="flex-1 overflow-y-auto p-4 space-y-4">
{messages.length === 0 ? (
<div className="flex items-center justify-center h-full">
<div className="text-center">
<Bot className="h-12 w-12 mx-auto text-muted-foreground mb-4" />
<h3 className="text-lg font-medium mb-2">Start a conversation</h3>
<p className="text-muted-foreground">
Ask me anything and I'll help you out!
</p>
</div>
</div>
) : (
messages.map((message) => (
<div key={message.id} className="space-y-3">
<div className={`flex items-start gap-3 ${
message.role === 'user' ? 'justify-end' : 'justify-start'
}`}>
{message.role !== 'user' && (
<div className="w-8 h-8 rounded-full bg-primary flex items-center justify-center flex-shrink-0">
<Bot className="h-4 w-4 text-primary-foreground" />
</div>
)}
<Card className={`max-w-[80%] ${
message.role === 'user'
? 'bg-primary text-primary-foreground'
: 'bg-muted'
}`}>
<div className="p-4">
<div className="flex items-center gap-2 mb-2">
{message.role === 'user' ? (
<User className="h-4 w-4" />
) : (
<Bot className="h-4 w-4" />
)}
<span className="text-sm font-medium capitalize">
{message.role === 'user' ? 'You' : 'Assistant'}
</span>
{message.model_used && (
<Badge variant="outline" className="text-xs">
{message.model_used}
</Badge>
)}
</div>
{/* Thinking content */}
{message.thinking_content && message.supports_thinking && (
<div className="mb-3">
<Button
variant="ghost"
size="sm"
onClick={() => toggleThinking(message.id)}
className="p-1 h-auto"
>
<Brain className="h-3 w-3 mr-1" />
{showThinking[message.id] ? (
<>
<EyeOff className="h-3 w-3 mr-1" />
Hide thinking
</>
) : (
<>
<Eye className="h-3 w-3 mr-1" />
Show thinking
</>
)}
</Button>
{showThinking[message.id] && (
<div className="mt-2 p-3 bg-background/50 rounded border-l-4 border-blue-500">
<div className="text-xs text-muted-foreground mb-1">Thinking process:</div>
<ReactMarkdown
components={{
p: ({ children }) => <p className="text-sm prose prose-sm max-w-none">{children}</p>
}}
>
{message.thinking_content}
</ReactMarkdown>
</div>
)}
</div>
)}
{/* Main content */}
<ReactMarkdown
components={{
p: ({ children }) => <p className="prose prose-sm max-w-none">{children}</p>
}}
>
{message.content}
</ReactMarkdown>
</div>
</Card>
{message.role === 'user' && (
<div className="w-8 h-8 rounded-full bg-muted flex items-center justify-center flex-shrink-0">
<User className="h-4 w-4" />
</div>
)}
</div>
</div>
))
)}
</div>
{/* Input area */}
<div className="border-t p-4">
<div className="flex gap-2">
<Textarea
value={input}
onChange={(e) => setInput(e.target.value)}
onKeyPress={handleKeyPress}
placeholder={placeholder}
disabled={disabled}
className="min-h-[60px] resize-none"
/>
{isLoading ? (
<Button onClick={onStop} variant="outline" size="icon">
<Square className="h-4 w-4" />
</Button>
) : (
<Button
onClick={onSubmit}
disabled={disabled || !input.trim()}
size="icon"
>
<Send className="h-4 w-4" />
</Button>
)}
</div>
</div>
</div>
)
}
|