Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- The part of my chat-area.tsx component that handles sending messages:
- function handleSendMessage() {
- if (!input.trim() || isLoading) return;
- setIsLoading(true);
- const currentMessage = input;
- setInput("");
- // Add user message immediately for better UX
- setMessages(prev => [...prev, { role: "user", content: currentMessage }]);
- const processMessage = async () => {
- try {
- const response = await fetch('/api/chat/send', {
- method: 'POST',
- headers: {
- 'Content-Type': 'application/json',
- },
- body: JSON.stringify({
- model: selectedModel,
- message: currentMessage,
- chatId
- }),
- });
- if (!response.ok) {
- const error = await response.json();
- throw new Error(error.error || 'Failed to send message');
- }
- const data = await response.json();
- if (data.content) {
- setMessages(prev => [...prev, {
- role: "assistant",
- content: data.content
- }]);
- }
- } catch (error) {
- console.error("Error in chat:", error);
- toast({
- title: "Error",
- description: error instanceof Error ? error.message : "Failed to send message",
- variant: "destructive",
- });
- // Remove the user message if there was an error
- // setMessages(prev => prev.slice(0, -1));
- setMessages(prev => [...prev, { role: "assistant", content: "An error occurred. The page will refresh." }]);
- setTimeout(() => {
- window.location.reload();
- }, 3000); // Refresh after 3 seconds
- } finally {
- setIsLoading(false);
- if (inputRef.current) {
- inputRef.current.focus();
- }
- }
- };
- processMessage();
- }
- My /api/chat/send:
- import { verifyThreadOwnership } from "@/lib/dbFunctions";
- import { sendChatMessage } from "@/lib/llm";
- import { NextRequest, NextResponse } from "next/server";
- export async function POST(req: NextRequest) {
- try {
- const { model, message, chatId } = await req.json();
- // Verify thread ownership
- const isVerified = await verifyThreadOwnership(chatId);
- if (!isVerified) {
- return NextResponse.json(
- { error: "Unauthorized access to this chat thread" },
- { status: 401 }
- );
- }
- // Send message to OpenAI
- const response = await sendChatMessage(model, message, chatId);
- return NextResponse.json(response);
- } catch (error) {
- console.error("Error in chat API:", error);
- return NextResponse.json(
- { error: error, content: error },
- { status: 500 }
- );
- }
- }
- The functions it's calling from the dbFunctions and llm
- VerifyThreadOwnership:
- export async function verifyThreadOwnership(threadId: string): Promise<boolean> {
- const { userId } = await auth();
- await connectToDb();
- const user = await User.findOne({ userId: userId });
- if (!user) {
- throw new Error('User not found');
- }
- const hasThread = user.chats.some((chat: chat) => chat.id === threadId);
- if (!hasThread) {
- throw new Error('Unauthorized - Thread not found');
- }
- return true;
- }
- export async function sendChatMessage(llmModel: string, messageContent: string, threadId: string): Promise<{ status: string, runId?: string, content?: string }> {
- if (!(await hasEnoughTokens())) return { status: "error", content: "You don't have enough tokens" }
- try {
- // Create message and start run
- await client.beta.threads.messages.create(
- threadId,
- {
- role: "user",
- content: messageContent,
- }
- );
- const run = await client.beta.threads.runs.create(threadId, {
- model: llmModel,
- assistant_id: "asst_a2QluIujyYkVWXOJAYCTuKWh"
- });
- // Wait for run to complete with timeout and polling
- let runStatus;
- do {
- await new Promise(resolve => setTimeout(resolve, 1000)); // Wait 1 second between checks
- runStatus = await client.beta.threads.runs.retrieve(threadId, run.id);
- } while (runStatus.status === 'in_progress' || runStatus.status === 'queued');
- if (runStatus.status === 'completed') {
- const messages = await client.beta.threads.messages.list(threadId);
- const lastMessage = messages.data[0];
- await deductTokens(llmModel, runStatus.usage?.prompt_tokens ?? 0, runStatus.usage?.completion_tokens ?? 0);
- if (lastMessage.content[0].type === 'text') {
- return { status: "completed", content: lastMessage.content[0].text.value };
- }
- }
- return { status: "error", content: "Run completed but no valid response found" };
- } catch (error) {
- console.error("Chat message error:", error);
- return { status: "error", content: error instanceof Error ? error.message : 'An unexpected error occurred' };
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment