2025-03-24 11:58:48 +00:00
|
|
|
"use client";
|
|
|
|
|
2025-03-24 16:27:56 +00:00
|
|
|
import { useCallback } from "react";
|
2025-03-24 11:58:48 +00:00
|
|
|
import { useChat } from "@ai-sdk/react";
|
2025-03-24 16:27:56 +00:00
|
|
|
import {
|
|
|
|
ChatBubble,
|
|
|
|
ChatBubbleAvatar,
|
|
|
|
ChatBubbleMessage,
|
|
|
|
} from "@/components/ui/chat/chat-bubble";
|
2025-03-24 11:58:48 +00:00
|
|
|
import { SendHorizonal } from "lucide-react";
|
|
|
|
import { Button } from "@/components/ui/button";
|
2025-03-24 13:24:20 +00:00
|
|
|
import { useProblem } from "@/hooks/use-problem";
|
2025-03-24 11:58:48 +00:00
|
|
|
import MdxPreview from "@/components/mdx-preview";
|
|
|
|
import { Textarea } from "@/components/ui/textarea";
|
|
|
|
import { ScrollArea, ScrollBar } from "@/components/ui/scroll-area";
|
|
|
|
import { ChatMessageList } from "@/components/ui/chat/chat-message-list";
|
|
|
|
|
|
|
|
export default function AiBotPage() {
|
2025-03-24 16:27:56 +00:00
|
|
|
const { problemId, problem, currentLang, currentValue } = useProblem();
|
|
|
|
const { messages, input, handleInputChange, setMessages, handleSubmit } = useChat({
|
2025-03-24 13:24:20 +00:00
|
|
|
initialMessages: [
|
|
|
|
{
|
|
|
|
id: problemId,
|
|
|
|
role: "system",
|
|
|
|
content: `Problem description:\n${problem.description}`,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
});
|
2025-03-24 11:58:48 +00:00
|
|
|
|
2025-03-24 16:27:56 +00:00
|
|
|
const handleFormSubmit = useCallback(
|
|
|
|
(e: React.FormEvent) => {
|
|
|
|
e.preventDefault();
|
|
|
|
if (!input.trim()) return;
|
|
|
|
|
|
|
|
const currentCodeMessage = {
|
|
|
|
id: problemId,
|
|
|
|
role: "system" as const,
|
|
|
|
content: `Current code:\n\`\`\`${currentLang}\n${currentValue}\n\`\`\``,
|
|
|
|
};
|
|
|
|
|
|
|
|
setMessages([...messages, currentCodeMessage]);
|
|
|
|
handleSubmit();
|
|
|
|
},
|
|
|
|
[currentLang, currentValue, handleSubmit, input, messages, problemId, setMessages]
|
|
|
|
);
|
|
|
|
|
2025-03-24 11:58:48 +00:00
|
|
|
return (
|
|
|
|
<div className="h-full flex flex-col">
|
|
|
|
<div className="flex-1">
|
|
|
|
<ScrollArea className="[&>[data-radix-scroll-area-viewport]]:max-h-[calc(100vh-238px)]">
|
|
|
|
<ChatMessageList>
|
2025-03-24 13:24:20 +00:00
|
|
|
{messages
|
|
|
|
.filter((message) => message.role === "user" || message.role === "assistant")
|
2025-03-24 16:27:56 +00:00
|
|
|
.map((message) => (
|
|
|
|
<ChatBubble key={message.id} layout="ai">
|
2025-03-24 13:24:20 +00:00
|
|
|
<ChatBubbleAvatar src="" fallback={message.role === "user" ? "US" : "AI"} />
|
|
|
|
<ChatBubbleMessage layout="ai">
|
|
|
|
<MdxPreview source={message.content} />
|
|
|
|
</ChatBubbleMessage>
|
|
|
|
</ChatBubble>
|
|
|
|
))}
|
2025-03-24 11:58:48 +00:00
|
|
|
</ChatMessageList>
|
|
|
|
<ScrollBar orientation="horizontal" />
|
|
|
|
</ScrollArea>
|
|
|
|
</div>
|
|
|
|
<footer className="h-36 flex flex-none">
|
2025-03-24 16:27:56 +00:00
|
|
|
<form onSubmit={handleFormSubmit} className="w-full p-4 pt-0 relative">
|
2025-03-24 11:58:48 +00:00
|
|
|
<Textarea
|
|
|
|
value={input}
|
|
|
|
onChange={handleInputChange}
|
|
|
|
className="h-full bg-muted border-transparent shadow-none rounded-lg"
|
|
|
|
placeholder="Bot will automatically get your current code"
|
|
|
|
/>
|
|
|
|
<Button type="submit" variant="ghost" className="absolute bottom-6 right-6 h-6 w-auto px-2">
|
|
|
|
<SendHorizonal className="size-4" />
|
|
|
|
</Button>
|
|
|
|
</form>
|
|
|
|
</footer>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|