"use client"; import { toast } from 'react-toastify'; import 'react-toastify/dist/ReactToastify.css'; import { ChatWindowMessage } from '@/schema/ChatWindowMessage'; import { useState, type FormEvent } from "react"; import { Feedback } from 'langsmith'; export function ChatMessageBubble(props: { message: ChatWindowMessage; aiEmoji?: string; onRemovePressed?: () => void; }) { const { role, content, runId } = props.message; const colorClassName = role === "human" ? "bg-sky-600" : "bg-slate-50 text-black"; const alignmentClassName = role === "human" ? "ml-auto" : "mr-auto"; const prefix = role === "human" ? "🧑" : props.aiEmoji; const [isLoading, setIsLoading] = useState(false); const [feedback, setFeedback] = useState(null); const [comment, setComment] = useState(""); const [showCommentForm, setShowCommentForm] = useState(false); async function handleScoreButtonPress(e: React.MouseEvent, score: number) { e.preventDefault(); setComment(""); await sendFeedback(score); } async function handleCommentSubmission(e: FormEvent) { e.preventDefault(); const score = typeof feedback?.score === "number" ? feedback.score : 0; await sendFeedback(score); } async function sendFeedback(score: number) { if (isLoading) { return; } setIsLoading(true); const response = await fetch("api/feedback", { method: feedback?.id ? "PUT" : "POST", body: JSON.stringify({ id: feedback?.id, run_id: runId, score, comment, }) }); const json = await response.json(); if (json.error) { toast(json.error, { theme: "dark" }); return; } else if (feedback?.id && comment) { toast("Response recorded! Go to https://smith.langchain.com and check it out in under your run's \"Feedback\" pane.", { theme: "dark", autoClose: 3000, }); setComment(""); setShowCommentForm(false); } else { setShowCommentForm(true); } if (json.feedback) { setFeedback(json.feedback); } setIsLoading(false); } return (
{prefix}
{/* TODO: Remove. Hacky fix, stop sequences don't seem to work with WebLLM yet. */} {content.trim().split("\nInstruct:")[0].split("\nInstruction:")[0]}
✖️
setComment(e.target.value)} />
Loading...
); }