Skip to content

Commit

Permalink
ai/svelte: use derived state for isLoading (#417)
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxLeiter authored Aug 4, 2023
1 parent c9e8a4b commit 5f91427
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/new-papayas-work.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'ai': patch
---

ai/svelte: fix isLoading return value
11 changes: 9 additions & 2 deletions packages/core/svelte/use-chat.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Readable, get, writable, Writable } from 'svelte/store'
import { Readable, get, writable, Writable, derived } from 'svelte/store'
import { useSWR } from 'sswr'
import { nanoid, createChunkDecoder } from '../shared/utils'

Expand Down Expand Up @@ -386,6 +386,13 @@ export function useChat({
input.set('')
}

const isLoading = derived(
[isSWRLoading, loading],
([$isSWRLoading, $loading]) => {
return $isSWRLoading || $loading
}
)

return {
messages,
error,
Expand All @@ -395,6 +402,6 @@ export function useChat({
setMessages,
input,
handleSubmit,
isLoading: isSWRLoading || loading
isLoading: isLoading
}
}
11 changes: 9 additions & 2 deletions packages/core/svelte/use-completion.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useSWR } from 'sswr'
import { Readable, get, writable } from 'svelte/store'
import { Readable, derived, get, writable } from 'svelte/store'

import { Writable } from 'svelte/store'

Expand Down Expand Up @@ -200,6 +200,13 @@ export function useCompletion({
return complete(inputValue)
}

const isLoading = derived(
[isSWRLoading, loading],
([$isSWRLoading, $loading]) => {
return $isSWRLoading || $loading
}
)

return {
completion,
complete,
Expand All @@ -208,6 +215,6 @@ export function useCompletion({
setCompletion,
input,
handleSubmit,
isLoading: isSWRLoading || loading
isLoading
}
}

0 comments on commit 5f91427

Please sign in to comment.