From 3d4684354bc7609ce140f5af744318f9782d8447 Mon Sep 17 00:00:00 2001 From: Tim Ermilov Date: Tue, 12 Nov 2024 12:27:47 +0100 Subject: [PATCH] Add loading indicator and error handling to chat UI --- app/components/agent/Chat.tsx | 38 +++++++++++++++++++++++++++-------- 1 file changed, 30 insertions(+), 8 deletions(-) diff --git a/app/components/agent/Chat.tsx b/app/components/agent/Chat.tsx index d89397c..2bba6eb 100644 --- a/app/components/agent/Chat.tsx +++ b/app/components/agent/Chat.tsx @@ -5,6 +5,7 @@ import { litlyticsAtom, pipelineAtom } from '~/store/store'; import { Button } from '../catalyst/button'; import { Input } from '../catalyst/input'; import { CustomMarkdown } from '../markdown/Markdown'; +import { Spinner } from '../Spinner'; import { askAgent } from './logic/askAgent'; import { type Message } from './logic/types'; @@ -45,6 +46,8 @@ export function Chat() { text: `Hi! I'm Lit. Ask me to do anything for you.`, }, ]); + const [loading, setLoading] = useState(false); + const [error, setError] = useState(); useEffect(() => { // scroll to bottom @@ -64,6 +67,8 @@ export function Chat() { } // reset input setInput(''); + // reset error + setError(undefined); // append user message to messages const messagesWithUser: Message[] = [ ...messages, @@ -75,15 +80,22 @@ export function Chat() { ]; setMessages(messagesWithUser); - // TODO: show loading state - + // show loading state + setLoading(true); // run new messages through agent - const newMessages = await askAgent({ - messages: messagesWithUser, - litlytics, - setPipeline, - }); - setMessages(newMessages); + try { + const newMessages = await askAgent({ + messages: messagesWithUser, + litlytics, + setPipeline, + }); + setMessages(newMessages); + } catch (err) { + // catch and display error + setError(err as Error); + } + // disable loading state + setLoading(false); }; return ( @@ -95,6 +107,16 @@ export function Chat() { {messages.map((m) => ( ))} + {loading && ( +
+ Thinking... +
+ )} + {error && ( +
+ Error while thinking: {error.message} +
+ )}