Skip to content

Commit

Permalink
Add loading indicator and error handling to chat UI
Browse files Browse the repository at this point in the history
  • Loading branch information
yamalight committed Nov 12, 2024
1 parent f7df4ef commit 3d46843
Showing 1 changed file with 30 additions and 8 deletions.
38 changes: 30 additions & 8 deletions app/components/agent/Chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -45,6 +46,8 @@ export function Chat() {
text: `Hi! I'm Lit. Ask me to do anything for you.`,
},
]);
const [loading, setLoading] = useState<boolean>(false);
const [error, setError] = useState<Error | undefined>();

useEffect(() => {
// scroll to bottom
Expand All @@ -64,6 +67,8 @@ export function Chat() {
}
// reset input
setInput('');
// reset error
setError(undefined);
// append user message to messages
const messagesWithUser: Message[] = [
...messages,
Expand All @@ -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 (
Expand All @@ -95,6 +107,16 @@ export function Chat() {
{messages.map((m) => (
<MessageRender key={m.id} message={m} />
))}
{loading && (
<div className="flex items-center justify-end gap-2">
<Spinner className="h-5 w-5" /> Thinking...
</div>
)}
{error && (
<div className="flex items-center justify-between bg-red-400 dark:bg-red-700 rounded-xl py-1 px-2 my-2">
Error while thinking: {error.message}
</div>
)}
</div>
<div className="flex items-center min-h-16 p-2">
<Input
Expand Down

0 comments on commit 3d46843

Please sign in to comment.