Spaces:
Sleeping
Sleeping
File size: 10,542 Bytes
4c41b3d | 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 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 | import React, { useState, useRef, useEffect } from "react";
import { trpc } from "@/lib/trpc";
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
import { Button } from "@/components/ui/button";
import { Textarea } from "@/components/ui/textarea";
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select";
import { Badge } from "@/components/ui/badge";
import { Copy, Download, Send, Bot, User, Loader2 } from "lucide-react";
import { toast } from "sonner";
interface Message {
id: string;
role: "user" | "assistant";
content: string;
timestamp: string;
mode?: string;
}
export default function ChatInterface() {
const [messages, setMessages] = useState<Message[]>([]);
const [input, setInput] = useState("");
const [mode, setMode] = useState("auto");
const [contentType, setContentType] = useState("code");
const [isLoading, setIsLoading] = useState(false);
const messagesEndRef = useRef<HTMLDivElement>(null);
const sendMutation = trpc.chat.sendMessage.useMutation({
onSuccess: (data) => {
setMessages((prev) => [
...prev,
{
id: Date.now().toString(),
role: "assistant",
content: data.content,
timestamp: new Date().toLocaleTimeString(),
mode: data.mode,
},
]);
setIsLoading(false);
},
onError: (error) => {
toast.error("Failed to send message: " + error.message);
setIsLoading(false);
},
});
const scrollToBottom = () => {
messagesEndRef.current?.scrollIntoView({ behavior: "smooth" });
};
useEffect(() => {
scrollToBottom();
}, [messages]);
const handleSend = async () => {
if (!input.trim()) return;
const userMessage: Message = {
id: Date.now().toString(),
role: "user",
content: input,
timestamp: new Date().toLocaleTimeString(),
};
setMessages((prev) => [...prev, userMessage]);
setInput("");
setIsLoading(true);
await sendMutation.mutateAsync({
message: input,
mode: mode as any,
contentType: contentType as any,
});
};
const copyToClipboard = (text: string) => {
navigator.clipboard.writeText(text);
toast.success("Copied to clipboard!");
};
return (
<div className="min-h-screen bg-black text-white">
{/* Header */}
<header className="border-b border-cyan-500/20 bg-black/50 backdrop-blur-sm sticky top-0 z-40">
<div className="container mx-auto px-4 py-4">
<h1 className="text-2xl font-bold bg-gradient-to-r from-cyan-400 to-blue-500 bg-clip-text text-transparent">
AI Code Generator & Evaluator
</h1>
</div>
</header>
<main className="container mx-auto px-4 py-6">
<div className="grid grid-cols-1 lg:grid-cols-4 gap-6">
{/* Chat Area */}
<div className="lg:col-span-3 space-y-4">
{/* Messages */}
<Card className="bg-gray-900/50 border-cyan-500/20 flex flex-col h-96">
<CardContent className="flex-1 overflow-y-auto p-4 space-y-4">
{messages.length === 0 ? (
<div className="h-full flex items-center justify-center text-gray-500">
<div className="text-center">
<Bot size={48} className="mx-auto mb-4 opacity-50" />
<p>Start a conversation with the AI</p>
</div>
</div>
) : (
messages.map((msg) => (
<div
key={msg.id}
className={`flex gap-3 ${msg.role === "user" ? "justify-end" : "justify-start"}`}
>
{msg.role === "assistant" && (
<div className="w-8 h-8 rounded-lg bg-gradient-to-br from-cyan-400 to-blue-500 flex items-center justify-center flex-shrink-0">
<Bot size={16} className="text-black" />
</div>
)}
<div
className={`max-w-xs lg:max-w-md px-4 py-2 rounded-lg ${
msg.role === "user"
? "bg-green-500/20 border border-green-500/50 text-green-100"
: "bg-cyan-500/20 border border-cyan-500/50 text-cyan-100"
}`}
>
<p className="text-sm break-words">{msg.content}</p>
{msg.role === "assistant" && (
<div className="flex gap-2 mt-2">
<Button
size="sm"
variant="ghost"
className="h-6 px-2 text-xs"
onClick={() => copyToClipboard(msg.content)}
>
<Copy size={12} />
</Button>
<Button size="sm" variant="ghost" className="h-6 px-2 text-xs">
<Download size={12} />
</Button>
</div>
)}
</div>
{msg.role === "user" && (
<div className="w-8 h-8 rounded-lg bg-gradient-to-br from-green-400 to-emerald-500 flex items-center justify-center flex-shrink-0">
<User size={16} className="text-black" />
</div>
)}
</div>
))
)}
{isLoading && (
<div className="flex gap-3">
<div className="w-8 h-8 rounded-lg bg-gradient-to-br from-cyan-400 to-blue-500 flex items-center justify-center">
<Loader2 size={16} className="text-black animate-spin" />
</div>
<div className="bg-cyan-500/20 border border-cyan-500/50 px-4 py-2 rounded-lg">
<p className="text-sm text-cyan-100">Processing...</p>
</div>
</div>
)}
<div ref={messagesEndRef} />
</CardContent>
</Card>
{/* Input Area */}
<Card className="bg-gray-900/50 border-cyan-500/20">
<CardContent className="p-4 space-y-4">
<Textarea
placeholder="Enter your code generation or evaluation request..."
value={input}
onChange={(e) => setInput(e.target.value)}
onKeyDown={(e) => {
if (e.key === "Enter" && e.ctrlKey) {
handleSend();
}
}}
className="bg-black/50 border-green-500/20 text-white placeholder-gray-500 focus:border-green-500/50 min-h-24"
/>
<div className="flex gap-2">
<Button
onClick={handleSend}
disabled={isLoading || !input.trim()}
className="bg-gradient-to-r from-cyan-500 to-blue-500 text-black hover:from-cyan-600 hover:to-blue-600 flex-1"
>
<Send size={18} className="mr-2" />
Send
</Button>
</div>
</CardContent>
</Card>
</div>
{/* Sidebar */}
<div className="space-y-4">
{/* Mode Selection */}
<Card className="bg-gray-900/50 border-green-500/20">
<CardHeader>
<CardTitle className="text-green-400 text-sm">Mode</CardTitle>
</CardHeader>
<CardContent className="space-y-2">
<Select value={mode} onValueChange={setMode}>
<SelectTrigger className="bg-black/50 border-green-500/20 text-white">
<SelectValue />
</SelectTrigger>
<SelectContent className="bg-gray-900 border-green-500/20">
<SelectItem value="auto">Auto</SelectItem>
<SelectItem value="qwen">Qwen (Generate)</SelectItem>
<SelectItem value="deepseek">DeepSeek (Evaluate)</SelectItem>
<SelectItem value="loop">Loop (Iterative)</SelectItem>
</SelectContent>
</Select>
</CardContent>
</Card>
{/* Content Type */}
<Card className="bg-gray-900/50 border-purple-500/20">
<CardHeader>
<CardTitle className="text-purple-400 text-sm">Content Type</CardTitle>
</CardHeader>
<CardContent className="space-y-2">
<Select value={contentType} onValueChange={setContentType}>
<SelectTrigger className="bg-black/50 border-purple-500/20 text-white">
<SelectValue />
</SelectTrigger>
<SelectContent className="bg-gray-900 border-purple-500/20">
<SelectItem value="code">Code</SelectItem>
<SelectItem value="exploit">Exploit</SelectItem>
<SelectItem value="payload">Payload</SelectItem>
<SelectItem value="information">Information</SelectItem>
<SelectItem value="strategy">Strategy</SelectItem>
</SelectContent>
</Select>
</CardContent>
</Card>
{/* Stats */}
<Card className="bg-gray-900/50 border-orange-500/20">
<CardHeader>
<CardTitle className="text-orange-400 text-sm">Session Stats</CardTitle>
</CardHeader>
<CardContent className="space-y-2 text-sm">
<div className="flex justify-between">
<span className="text-gray-400">Messages:</span>
<span className="text-orange-400 font-bold">{messages.length}</span>
</div>
<div className="flex justify-between">
<span className="text-gray-400">Mode:</span>
<Badge className="bg-green-500/20 text-green-400">{mode}</Badge>
</div>
</CardContent>
</Card>
</div>
</div>
</main>
</div>
);
}
|