Skip to content

Commit

Permalink
add createdAt property to chat items and implement error handling for…
Browse files Browse the repository at this point in the history
… loading state
  • Loading branch information
nwaughachukwuma committed Dec 5, 2024
1 parent 5f2db7a commit c7d7710
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 7 deletions.
27 changes: 25 additions & 2 deletions app/src/lib/components/ChatListItem.svelte
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
<script context="module">
const TWO_MINUTES_MS = 120000;
</script>

<script lang="ts">
import { UserIcon, BotIcon } from 'lucide-svelte';
import { UserIcon, BotIcon, RotateCw } from 'lucide-svelte';
import cs from 'clsx';
import { parse } from 'marked';
import { Button } from './ui/button';
export let type: 'user' | 'assistant';
export let content: string;
export let loading = false;
export let createdAt: number | undefined = undefined;
$: likelyErrored = loading && (!createdAt || Date.now() - createdAt > TWO_MINUTES_MS);
</script>

<div
Expand All @@ -28,7 +36,12 @@
</div>

<div class="max-w-full justify-center break-words text-gray-200 flex flex-col text-base">
{#if loading}
{#if likelyErrored}
<span>
Failed to generate response.
<span class="text-red-300">Likely errored</span>
</span>
{:else if loading}
<slot name="loading">
<span class="animate-pulse">Generating response...</span>
</slot>
Expand All @@ -41,3 +54,13 @@
{/if}
</div>
</div>

{#if likelyErrored}
<Button
variant="ghost"
class="w-fit bg-gray-800 flex gap-x-2 text-gray-400 items-center hover:bg-gray-700 transition-all px-4 py-0.5"
>
<span>Regenerate</span>
<RotateCw class="inline w-4" />
</Button>
{/if}
2 changes: 1 addition & 1 deletion app/src/lib/components/ExampleCard.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
async function handleClick() {
startSession(category);
addChatItem({ id: uuid(), content, role: 'user', loading: false });
addChatItem({ id: uuid(), content, role: 'user', loading: false, createdAt: Date.now() });
return goto(href, { invalidateAll: true, replaceState: true });
}
</script>
Expand Down
1 change: 1 addition & 0 deletions app/src/lib/stores/sessionContext.svelte.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export type ChatItem = {
content: string;
role: 'user' | 'assistant';
loading?: boolean;
createdAt?: number;
};

export type Session = {
Expand Down
2 changes: 1 addition & 1 deletion app/src/routes/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
startSession(category);
const content = `${selectContent}\nCategory: ${category} `;
addChatItem({ id: uuid(), content, role: 'user', loading: false });
addChatItem({ id: uuid(), content, role: 'user', loading: false, createdAt: Date.now() });
const href = `/chat/${sessionId}?category=${category}&chat`;
return goto(href, { invalidateAll: true, replaceState: true });
Expand Down
18 changes: 15 additions & 3 deletions app/src/routes/chat/[sessionId=sessionId]/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@
id: uuid(),
content: searchTerm,
role: 'user',
loading: false
loading: false,
createdAt: Date.now()
};
addChatItem(chatItem);
searchTerm = '';
Expand All @@ -57,7 +58,13 @@
}
async function chatRequest(uItem: ChatItem) {
const aItem = addChatItem({ id: uuid(), content: '', role: 'assistant', loading: true });
const aItem = addChatItem({
id: uuid(),
content: '',
role: 'assistant',
loading: true,
createdAt: Date.now()
});
return fetch(`${env.API_BASE_URL}/chat/${sessionId}`, {
method: 'POST',
Expand Down Expand Up @@ -92,7 +99,12 @@
<div class="flex flex-col gap-y-3 h-full">
{#each sessionChats as item (item.id)}
{@const finalResponse = isfinalResponse(item)}
<ChatListItem type={item.role} content={item.content} loading={item.loading} />
<ChatListItem
type={item.role}
content={item.content}
loading={item.loading}
createdAt={item.createdAt}
/>

{#if finalResponse}
<ChatListActionItems
Expand Down

0 comments on commit c7d7710

Please sign in to comment.