@@ -3,24 +3,13 @@ import React, { useState, useRef, useEffect } from "react";
33import { useSession } from "next-auth/react" ;
44import { Textarea } from "@/components/ui/textarea" ;
55import { Button } from "@/components/ui/button" ;
6- import { Badge } from "@/components/ui/badge" ;
76import {
87 ChatCircleDotsIcon ,
98 MicrophoneIcon ,
109 SpinnerGapIcon ,
1110} from "@phosphor-icons/react" ;
12- import {
13- Select ,
14- SelectContent ,
15- SelectGroup ,
16- SelectItem ,
17- SelectLabel ,
18- SelectTrigger ,
19- SelectValue ,
20- } from "@/components/ui/select" ;
2111import ReactMarkdown from "react-markdown" ;
2212import SyntaxHighlighter from "react-syntax-highlighter" ;
23- import { vs2015 } from "react-syntax-highlighter/dist/esm/styles/hljs" ;
2413import remarkGfm from "remark-gfm" ;
2514import { Geist_Mono } from "next/font/google" ;
2615import { cn } from "@/lib/utils" ;
@@ -61,7 +50,7 @@ const UIInput = () => {
6150 scrollToBottom ( ) ;
6251 } , [ messages ] ) ;
6352
64- const saveChat = api . chat . createChat . useMutation ( {
53+ const createChat = api . chat . createChat . useMutation ( {
6554 onError : ( error ) => {
6655 console . error ( "Error saving chat:" , error ) ;
6756 } ,
@@ -74,6 +63,8 @@ const UIInput = () => {
7463 return ;
7564 }
7665
66+ const tempMessageId = `ai-${ Date . now ( ) } ` ;
67+
7768 try {
7869 const reader = response . body ?. getReader ( ) ;
7970 if ( ! reader ) {
@@ -82,8 +73,6 @@ const UIInput = () => {
8273 return ;
8374 }
8475
85- const tempMessageId = `ai-${ Date . now ( ) } ` ;
86-
8776 setMessages ( ( prev ) => [
8877 ...prev ,
8978 { id : tempMessageId , role : "assistant" , content : "" } ,
@@ -111,6 +100,7 @@ const UIInput = () => {
111100 const { done, value } = await reader . read ( ) ;
112101
113102 if ( done ) {
103+ // Final update with complete content
114104 setMessages ( ( prev ) =>
115105 prev . map ( ( msg ) =>
116106 msg . id === tempMessageId
@@ -152,8 +142,22 @@ const UIInput = () => {
152142 content ?: string ;
153143 } ;
154144 } > ;
145+ error ?: string ;
155146 } ;
156147
148+ // Handle error responses
149+ if ( parsedData . error ) {
150+ console . error ( "Stream error:" , parsedData . error ) ;
151+ setMessages ( ( prev ) =>
152+ prev . map ( ( msg ) =>
153+ msg . id === tempMessageId
154+ ? { ...msg , content : `Error: ${ parsedData . error } ` }
155+ : msg ,
156+ ) ,
157+ ) ;
158+ break ;
159+ }
160+
157161 const content = parsedData . choices ?. [ 0 ] ?. delta ?. content ;
158162 if ( content ) {
159163 accumulatedContent += content ;
@@ -169,13 +173,16 @@ const UIInput = () => {
169173 updateMessage ( accumulatedContent ) ;
170174 }
171175 }
172-
173- saveChat . mutate ( {
174- message : userMessage ,
175- model : model ,
176- } ) ;
177176 } catch ( error ) {
178177 console . error ( "Error processing stream:" , error ) ;
178+ // Update message with error state
179+ setMessages ( ( prev ) =>
180+ prev . map ( ( msg ) =>
181+ msg . id === tempMessageId
182+ ? { ...msg , content : "Error: Failed to process response" }
183+ : msg ,
184+ ) ,
185+ ) ;
179186 } finally {
180187 setIsLoading ( false ) ;
181188 abortControllerRef . current = null ;
@@ -209,6 +216,7 @@ const UIInput = () => {
209216 abortControllerRef . current = new AbortController ( ) ;
210217
211218 try {
219+ const { chatId} = await createChat . mutateAsync ( ) ;
212220 setTimeout ( ( ) => {
213221 void ( async ( ) => {
214222 try {
@@ -220,6 +228,7 @@ const UIInput = () => {
220228 body : JSON . stringify ( {
221229 messages : [ { role : "user" , content : currentQuery } ] ,
222230 model : model ,
231+ chatId : chatId ,
223232 } ) ,
224233 signal : abortControllerRef . current ?. signal ,
225234 } ) ;
@@ -395,6 +404,12 @@ const UIInput = () => {
395404 < Textarea
396405 value = { query }
397406 onChange = { ( e ) => setQuery ( e . target . value ) }
407+ onKeyDown = { ( e ) => {
408+ if ( e . key === "Enter" && ! e . shiftKey ) {
409+ e . preventDefault ( ) ;
410+ void handleCreateChat ( e as any ) ;
411+ }
412+ } }
398413 placeholder = "Ask whatever you want to be"
399414 className = "h-[2rem] resize-none rounded-none border-none bg-transparent px-0 py-1 shadow-none ring-0 focus-visible:ring-0 dark:bg-transparent"
400415 disabled = { isLoading }
0 commit comments