import { Input } from "@/components/ui/input"; import { Button } from "@/components/ui/button"; import { v4 as uuidv4 } from "uuid"; import { useChatInteract, useChatMessages, IStep, } from "@chainlit/react-client"; import { useState } from "react"; export function Playground() { const [inputValue, setInputValue] = useState(""); const { sendMessage } = useChatInteract(); const { messages } = useChatMessages(); const handleSendMessage = () => { const content = inputValue.trim(); if (content) { const message: IStep = { id: uuidv4(), name: "user", type: "user_message", output: content, createdAt: new Date().toISOString(), }; sendMessage(message, []); setInputValue(""); } }; const renderMessage = (message: IStep) => { const dateOptions: Intl.DateTimeFormatOptions = { hour: "2-digit", minute: "2-digit", }; const date = new Date(message.createdAt).toLocaleTimeString( undefined, dateOptions ); return (
{message.name}

{message.output}

{date}
); }; return (
{messages.map((message) => renderMessage(message))}
setInputValue(e.target.value)} onKeyUp={(e) => { if (e.key === "Enter") { handleSendMessage(); } }} />
); }