Skip to content

Commit

Permalink
ai/react: don't throw error if onError is passed (#370)
Browse files Browse the repository at this point in the history
The vue, solid, and svelte utils do not use an equivalent to `useSWRMutation` and always return the caught error in the `error` returned from our client hooks. This more closely aligns the react package by having SWR not throw the error if an `onError` is passed. 

x-ref: [slack](https://vercel.slack.com/archives/C050WU03V3N/p1689962301840149)
  • Loading branch information
MaxLeiter committed Jul 21, 2023
1 parent 7b2bb04 commit 877c16f
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/good-pumas-hang.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'ai': patch
---

ai/react: don't throw error if onError is passed
11 changes: 4 additions & 7 deletions packages/core/react/use-chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,9 @@ import type {
CreateMessage,
Message,
UseChatOptions,
RequestOptions,
ChatRequestOptions
} from '../shared/types'
import {
ChatCompletionRequestMessageFunctionCall,
CreateChatCompletionRequestFunctionCall
} from 'openai-edge'
import { ChatCompletionFunctions } from 'openai-edge/types/api'
import { ChatCompletionRequestMessageFunctionCall } from 'openai-edge'
export type { Message, CreateMessage, UseChatOptions }

export type UseChatHelpers = {
Expand Down Expand Up @@ -320,7 +315,9 @@ export function useChat({
},
{
populateCache: false,
revalidate: false
revalidate: false,
// @ts-expect-error - SWR tries to be clever with the throwOnError type
throwOnError: Boolean(!onError)
}
)

Expand Down
4 changes: 3 additions & 1 deletion packages/core/react/use-completion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,9 @@ export function useCompletion({
},
{
populateCache: false,
revalidate: false
revalidate: false,
// @ts-expect-error - SWR tries to be clever with the throwOnError type
throwOnError: Boolean(onError)
}
)

Expand Down

0 comments on commit 877c16f

Please sign in to comment.