@ai-sdk/react 0.0.30 → 0.0.31
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.turbo/turbo-build.log +10 -10
- package/.turbo/turbo-clean.log +1 -1
- package/CHANGELOG.md +10 -0
- package/dist/index.d.mts +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +14 -6
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +14 -6
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -2
- package/src/use-chat.ts +11 -5
- package/src/use-chat.ui.test.tsx +126 -5
- package/src/use-completion.ts +8 -2
- package/src/use-completion.ui.test.tsx +1 -3
package/.turbo/turbo-build.log
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
|
|
2
|
-
> @ai-sdk/react@0.0.
|
|
2
|
+
> @ai-sdk/react@0.0.31 build /home/runner/work/ai/ai/packages/react
|
|
3
3
|
> tsup
|
|
4
4
|
|
|
5
5
|
[34mCLI[39m Building entry: src/index.ts
|
|
@@ -9,13 +9,13 @@
|
|
|
9
9
|
[34mCLI[39m Target: es2018
|
|
10
10
|
[34mCJS[39m Build start
|
|
11
11
|
[34mESM[39m Build start
|
|
12
|
-
[
|
|
13
|
-
[
|
|
14
|
-
[
|
|
15
|
-
[
|
|
16
|
-
[
|
|
17
|
-
[
|
|
12
|
+
[32mESM[39m [1mdist/index.mjs [22m[32m25.33 KB[39m
|
|
13
|
+
[32mESM[39m [1mdist/index.mjs.map [22m[32m54.88 KB[39m
|
|
14
|
+
[32mESM[39m ⚡️ Build success in 62ms
|
|
15
|
+
[32mCJS[39m [1mdist/index.js [22m[32m27.81 KB[39m
|
|
16
|
+
[32mCJS[39m [1mdist/index.js.map [22m[32m54.95 KB[39m
|
|
17
|
+
[32mCJS[39m ⚡️ Build success in 62ms
|
|
18
18
|
[34mDTS[39m Build start
|
|
19
|
-
[32mDTS[39m ⚡️ Build success in
|
|
20
|
-
[32mDTS[39m [1mdist/index.d.ts [22m[32m10.
|
|
21
|
-
[32mDTS[39m [1mdist/index.d.mts [22m[32m10.
|
|
19
|
+
[32mDTS[39m ⚡️ Build success in 5431ms
|
|
20
|
+
[32mDTS[39m [1mdist/index.d.ts [22m[32m10.49 KB[39m
|
|
21
|
+
[32mDTS[39m [1mdist/index.d.mts [22m[32m10.49 KB[39m
|
package/.turbo/turbo-clean.log
CHANGED
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,15 @@
|
|
|
1
1
|
# @ai-sdk/react
|
|
2
2
|
|
|
3
|
+
## 0.0.31
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- c450fcf7: feat (ui): invoke useChat onFinish with finishReason and tokens
|
|
8
|
+
- e4a1719f: chore (ai/ui): rename streamMode to streamProtocol
|
|
9
|
+
- Updated dependencies [c450fcf7]
|
|
10
|
+
- Updated dependencies [e4a1719f]
|
|
11
|
+
- @ai-sdk/ui-utils@0.0.21
|
|
12
|
+
|
|
3
13
|
## 0.0.30
|
|
4
14
|
|
|
5
15
|
### Patch Changes
|
package/dist/index.d.mts
CHANGED
|
@@ -109,7 +109,7 @@ type UseChatHelpers = {
|
|
|
109
109
|
/** Additional data added on the server via StreamData */
|
|
110
110
|
data?: JSONValue[];
|
|
111
111
|
};
|
|
112
|
-
declare function useChat({ api, id, initialMessages, initialInput, sendExtraMessageFields, experimental_onFunctionCall, experimental_onToolCall, onToolCall, experimental_prepareRequestBody, experimental_maxAutomaticRoundtrips, maxAutomaticRoundtrips, maxToolRoundtrips, streamMode, onResponse, onFinish, onError, credentials, headers, body, generateId, fetch, keepLastMessageOnError, }?: UseChatOptions & {
|
|
112
|
+
declare function useChat({ api, id, initialMessages, initialInput, sendExtraMessageFields, experimental_onFunctionCall, experimental_onToolCall, onToolCall, experimental_prepareRequestBody, experimental_maxAutomaticRoundtrips, maxAutomaticRoundtrips, maxToolRoundtrips, streamMode, streamProtocol, onResponse, onFinish, onError, credentials, headers, body, generateId, fetch, keepLastMessageOnError, }?: UseChatOptions & {
|
|
113
113
|
key?: string;
|
|
114
114
|
/**
|
|
115
115
|
@deprecated Use `maxToolRoundtrips` instead.
|
|
@@ -206,7 +206,7 @@ type UseCompletionHelpers = {
|
|
|
206
206
|
/** Additional data added on the server via StreamData */
|
|
207
207
|
data?: JSONValue[];
|
|
208
208
|
};
|
|
209
|
-
declare function useCompletion({ api, id, initialCompletion, initialInput, credentials, headers, body, streamMode, fetch, onResponse, onFinish, onError, }?: UseCompletionOptions): UseCompletionHelpers;
|
|
209
|
+
declare function useCompletion({ api, id, initialCompletion, initialInput, credentials, headers, body, streamMode, streamProtocol, fetch, onResponse, onFinish, onError, }?: UseCompletionOptions): UseCompletionHelpers;
|
|
210
210
|
|
|
211
211
|
type Experimental_UseObjectOptions<RESULT> = {
|
|
212
212
|
/**
|
package/dist/index.d.ts
CHANGED
|
@@ -109,7 +109,7 @@ type UseChatHelpers = {
|
|
|
109
109
|
/** Additional data added on the server via StreamData */
|
|
110
110
|
data?: JSONValue[];
|
|
111
111
|
};
|
|
112
|
-
declare function useChat({ api, id, initialMessages, initialInput, sendExtraMessageFields, experimental_onFunctionCall, experimental_onToolCall, onToolCall, experimental_prepareRequestBody, experimental_maxAutomaticRoundtrips, maxAutomaticRoundtrips, maxToolRoundtrips, streamMode, onResponse, onFinish, onError, credentials, headers, body, generateId, fetch, keepLastMessageOnError, }?: UseChatOptions & {
|
|
112
|
+
declare function useChat({ api, id, initialMessages, initialInput, sendExtraMessageFields, experimental_onFunctionCall, experimental_onToolCall, onToolCall, experimental_prepareRequestBody, experimental_maxAutomaticRoundtrips, maxAutomaticRoundtrips, maxToolRoundtrips, streamMode, streamProtocol, onResponse, onFinish, onError, credentials, headers, body, generateId, fetch, keepLastMessageOnError, }?: UseChatOptions & {
|
|
113
113
|
key?: string;
|
|
114
114
|
/**
|
|
115
115
|
@deprecated Use `maxToolRoundtrips` instead.
|
|
@@ -206,7 +206,7 @@ type UseCompletionHelpers = {
|
|
|
206
206
|
/** Additional data added on the server via StreamData */
|
|
207
207
|
data?: JSONValue[];
|
|
208
208
|
};
|
|
209
|
-
declare function useCompletion({ api, id, initialCompletion, initialInput, credentials, headers, body, streamMode, fetch, onResponse, onFinish, onError, }?: UseCompletionOptions): UseCompletionHelpers;
|
|
209
|
+
declare function useCompletion({ api, id, initialCompletion, initialInput, credentials, headers, body, streamMode, streamProtocol, fetch, onResponse, onFinish, onError, }?: UseCompletionOptions): UseCompletionHelpers;
|
|
210
210
|
|
|
211
211
|
type Experimental_UseObjectOptions<RESULT> = {
|
|
212
212
|
/**
|
package/dist/index.js
CHANGED
|
@@ -215,7 +215,7 @@ var experimental_useAssistant = useAssistant;
|
|
|
215
215
|
var import_ui_utils2 = require("@ai-sdk/ui-utils");
|
|
216
216
|
var import_react2 = require("react");
|
|
217
217
|
var import_swr = __toESM(require("swr"));
|
|
218
|
-
var getStreamedResponse = async (api, chatRequest, mutate, mutateStreamData, existingData, extraMetadataRef, messagesRef, abortControllerRef, generateId2,
|
|
218
|
+
var getStreamedResponse = async (api, chatRequest, mutate, mutateStreamData, existingData, extraMetadataRef, messagesRef, abortControllerRef, generateId2, streamProtocol, onFinish, onResponse, onToolCall, sendExtraMessageFields, experimental_prepareRequestBody, fetch2, keepLastMessageOnError) => {
|
|
219
219
|
var _a;
|
|
220
220
|
const previousMessages = messagesRef.current;
|
|
221
221
|
mutate(chatRequest.messages, false);
|
|
@@ -271,7 +271,7 @@ var getStreamedResponse = async (api, chatRequest, mutate, mutateStreamData, exi
|
|
|
271
271
|
tool_choice: chatRequest.tool_choice
|
|
272
272
|
}
|
|
273
273
|
},
|
|
274
|
-
|
|
274
|
+
streamProtocol,
|
|
275
275
|
credentials: extraMetadataRef.current.credentials,
|
|
276
276
|
headers: {
|
|
277
277
|
...extraMetadataRef.current.headers,
|
|
@@ -308,6 +308,7 @@ function useChat({
|
|
|
308
308
|
maxAutomaticRoundtrips = experimental_maxAutomaticRoundtrips,
|
|
309
309
|
maxToolRoundtrips = maxAutomaticRoundtrips,
|
|
310
310
|
streamMode,
|
|
311
|
+
streamProtocol,
|
|
311
312
|
onResponse,
|
|
312
313
|
onFinish,
|
|
313
314
|
onError,
|
|
@@ -318,6 +319,9 @@ function useChat({
|
|
|
318
319
|
fetch: fetch2,
|
|
319
320
|
keepLastMessageOnError = false
|
|
320
321
|
} = {}) {
|
|
322
|
+
if (streamMode) {
|
|
323
|
+
streamProtocol != null ? streamProtocol : streamProtocol = streamMode === "text" ? "text" : void 0;
|
|
324
|
+
}
|
|
321
325
|
const hookId = (0, import_react2.useId)();
|
|
322
326
|
const idKey = id != null ? id : hookId;
|
|
323
327
|
const chatKey = typeof api === "string" ? [api, idKey] : idKey;
|
|
@@ -369,7 +373,7 @@ function useChat({
|
|
|
369
373
|
messagesRef,
|
|
370
374
|
abortControllerRef,
|
|
371
375
|
generateId2,
|
|
372
|
-
|
|
376
|
+
streamProtocol,
|
|
373
377
|
onFinish,
|
|
374
378
|
onResponse,
|
|
375
379
|
onToolCall,
|
|
@@ -422,7 +426,7 @@ function useChat({
|
|
|
422
426
|
setError,
|
|
423
427
|
mutateStreamData,
|
|
424
428
|
streamData,
|
|
425
|
-
|
|
429
|
+
streamProtocol,
|
|
426
430
|
sendExtraMessageFields,
|
|
427
431
|
experimental_onFunctionCall,
|
|
428
432
|
experimental_onToolCall,
|
|
@@ -671,11 +675,15 @@ function useCompletion({
|
|
|
671
675
|
headers,
|
|
672
676
|
body,
|
|
673
677
|
streamMode,
|
|
678
|
+
streamProtocol,
|
|
674
679
|
fetch: fetch2,
|
|
675
680
|
onResponse,
|
|
676
681
|
onFinish,
|
|
677
682
|
onError
|
|
678
683
|
} = {}) {
|
|
684
|
+
if (streamMode) {
|
|
685
|
+
streamProtocol != null ? streamProtocol : streamProtocol = streamMode === "text" ? "text" : void 0;
|
|
686
|
+
}
|
|
679
687
|
const hookId = (0, import_react3.useId)();
|
|
680
688
|
const completionId = id || hookId;
|
|
681
689
|
const { data, mutate } = (0, import_swr2.default)([api, completionId], null, {
|
|
@@ -711,7 +719,7 @@ function useCompletion({
|
|
|
711
719
|
...extraMetadataRef.current.body,
|
|
712
720
|
...options == null ? void 0 : options.body
|
|
713
721
|
},
|
|
714
|
-
|
|
722
|
+
streamProtocol,
|
|
715
723
|
fetch: fetch2,
|
|
716
724
|
setCompletion: (completion2) => mutate(completion2, false),
|
|
717
725
|
setLoading: mutateLoading,
|
|
@@ -735,7 +743,7 @@ function useCompletion({
|
|
|
735
743
|
onError,
|
|
736
744
|
setError,
|
|
737
745
|
streamData,
|
|
738
|
-
|
|
746
|
+
streamProtocol,
|
|
739
747
|
fetch2,
|
|
740
748
|
mutateStreamData
|
|
741
749
|
]
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts","../src/use-assistant.ts","../src/use-chat.ts","../src/use-completion.ts","../src/use-object.ts"],"sourcesContent":["export * from './use-assistant';\nexport * from './use-chat';\nexport * from './use-completion';\nexport * from './use-object';\n","import { isAbortError } from '@ai-sdk/provider-utils';\nimport {\n AssistantStatus,\n CreateMessage,\n Message,\n UseAssistantOptions,\n generateId,\n readDataStream,\n} from '@ai-sdk/ui-utils';\nimport { useCallback, useRef, useState } from 'react';\n\n// use function to allow for mocking in tests:\nconst getOriginalFetch = () => fetch;\n\nexport type UseAssistantHelpers = {\n /**\n * The current array of chat messages.\n */\n messages: Message[];\n\n /**\n * Update the message store with a new array of messages.\n */\n setMessages: React.Dispatch<React.SetStateAction<Message[]>>;\n\n /**\n * The current thread ID.\n */\n threadId: string | undefined;\n\n /**\n * Set the current thread ID. Specifying a thread ID will switch to that thread, if it exists. If set to 'undefined', a new thread will be created. For both cases, `threadId` will be updated with the new value and `messages` will be cleared.\n */\n setThreadId: (threadId: string | undefined) => void;\n\n /**\n * The current value of the input field.\n */\n input: string;\n\n /**\n * Append a user message to the chat list. This triggers the API call to fetch\n * the assistant's response.\n * @param message The message to append\n * @param requestOptions Additional options to pass to the API call\n */\n append: (\n message: Message | CreateMessage,\n requestOptions?: {\n data?: Record<string, string>;\n },\n ) => Promise<void>;\n\n /**\nAbort the current request immediately, keep the generated tokens if any.\n */\n stop: () => void;\n\n /**\n * setState-powered method to update the input value.\n */\n setInput: React.Dispatch<React.SetStateAction<string>>;\n\n /**\n * Handler for the `onChange` event of the input field to control the input's value.\n */\n handleInputChange: (\n event:\n | React.ChangeEvent<HTMLInputElement>\n | React.ChangeEvent<HTMLTextAreaElement>,\n ) => void;\n\n /**\n * Form submission handler that automatically resets the input field and appends a user message.\n */\n submitMessage: (\n event?: React.FormEvent<HTMLFormElement>,\n requestOptions?: {\n data?: Record<string, string>;\n },\n ) => Promise<void>;\n\n /**\n * The current status of the assistant. This can be used to show a loading indicator.\n */\n status: AssistantStatus;\n\n /**\n * The error thrown during the assistant message processing, if any.\n */\n error: undefined | Error;\n};\n\nexport function useAssistant({\n api,\n threadId: threadIdParam,\n credentials,\n headers,\n body,\n onError,\n fetch,\n}: UseAssistantOptions): UseAssistantHelpers {\n const [messages, setMessages] = useState<Message[]>([]);\n const [input, setInput] = useState('');\n const [currentThreadId, setCurrentThreadId] = useState<string | undefined>(\n undefined,\n );\n const [status, setStatus] = useState<AssistantStatus>('awaiting_message');\n const [error, setError] = useState<undefined | Error>(undefined);\n\n const handleInputChange = (\n event:\n | React.ChangeEvent<HTMLInputElement>\n | React.ChangeEvent<HTMLTextAreaElement>,\n ) => {\n setInput(event.target.value);\n };\n\n // Abort controller to cancel the current API call.\n const abortControllerRef = useRef<AbortController | null>(null);\n\n const stop = useCallback(() => {\n if (abortControllerRef.current) {\n abortControllerRef.current.abort();\n abortControllerRef.current = null;\n }\n }, []);\n\n const append = async (\n message: Message | CreateMessage,\n requestOptions?: {\n data?: Record<string, string>;\n },\n ) => {\n setStatus('in_progress');\n\n setMessages(messages => [\n ...messages,\n {\n ...message,\n id: message.id ?? generateId(),\n },\n ]);\n\n setInput('');\n\n const abortController = new AbortController();\n\n try {\n abortControllerRef.current = abortController;\n\n const actualFetch = fetch ?? getOriginalFetch();\n const response = await actualFetch(api, {\n method: 'POST',\n credentials,\n signal: abortController.signal,\n headers: { 'Content-Type': 'application/json', ...headers },\n body: JSON.stringify({\n ...body,\n // always use user-provided threadId when available:\n threadId: threadIdParam ?? currentThreadId ?? null,\n message: message.content,\n\n // optional request data:\n data: requestOptions?.data,\n }),\n });\n\n if (!response.ok) {\n throw new Error(\n (await response.text()) ?? 'Failed to fetch the assistant response.',\n );\n }\n\n if (response.body == null) {\n throw new Error('The response body is empty.');\n }\n\n for await (const { type, value } of readDataStream(\n response.body.getReader(),\n )) {\n switch (type) {\n case 'assistant_message': {\n setMessages(messages => [\n ...messages,\n {\n id: value.id,\n role: value.role,\n content: value.content[0].text.value,\n },\n ]);\n break;\n }\n\n case 'text': {\n // text delta - add to last message:\n setMessages(messages => {\n const lastMessage = messages[messages.length - 1];\n return [\n ...messages.slice(0, messages.length - 1),\n {\n id: lastMessage.id,\n role: lastMessage.role,\n content: lastMessage.content + value,\n },\n ];\n });\n\n break;\n }\n\n case 'data_message': {\n setMessages(messages => [\n ...messages,\n {\n id: value.id ?? generateId(),\n role: 'data',\n content: '',\n data: value.data,\n },\n ]);\n break;\n }\n\n case 'assistant_control_data': {\n setCurrentThreadId(value.threadId);\n\n // set id of last message:\n setMessages(messages => {\n const lastMessage = messages[messages.length - 1];\n lastMessage.id = value.messageId;\n return [...messages.slice(0, messages.length - 1), lastMessage];\n });\n\n break;\n }\n\n case 'error': {\n setError(new Error(value));\n break;\n }\n }\n }\n } catch (error) {\n // Ignore abort errors as they are expected when the user cancels the request:\n if (isAbortError(error) && abortController.signal.aborted) {\n abortControllerRef.current = null;\n return;\n }\n\n if (onError && error instanceof Error) {\n onError(error);\n }\n\n setError(error as Error);\n } finally {\n abortControllerRef.current = null;\n setStatus('awaiting_message');\n }\n };\n\n const submitMessage = async (\n event?: React.FormEvent<HTMLFormElement>,\n requestOptions?: {\n data?: Record<string, string>;\n },\n ) => {\n event?.preventDefault?.();\n\n if (input === '') {\n return;\n }\n\n append({ role: 'user', content: input }, requestOptions);\n };\n\n const setThreadId = (threadId: string | undefined) => {\n setCurrentThreadId(threadId);\n setMessages([]);\n };\n\n return {\n append,\n messages,\n setMessages,\n threadId: currentThreadId,\n setThreadId,\n input,\n setInput,\n handleInputChange,\n submitMessage,\n status,\n error,\n stop,\n };\n}\n\n/**\n@deprecated Use `useAssistant` instead.\n */\nexport const experimental_useAssistant = useAssistant;\n","import type {\n ChatRequest,\n ChatRequestOptions,\n Attachment,\n CreateMessage,\n FetchFunction,\n IdGenerator,\n JSONValue,\n Message,\n UseChatOptions,\n} from '@ai-sdk/ui-utils';\nimport {\n callChatApi,\n generateId as generateIdFunc,\n processChatStream,\n} from '@ai-sdk/ui-utils';\nimport { useCallback, useEffect, useId, useRef, useState } from 'react';\nimport useSWR, { KeyedMutator } from 'swr';\n\nexport type { CreateMessage, Message, UseChatOptions };\n\nexport type UseChatHelpers = {\n /** Current messages in the chat */\n messages: Message[];\n /** The error object of the API request */\n error: undefined | Error;\n /**\n * Append a user message to the chat list. This triggers the API call to fetch\n * the assistant's response.\n * @param message The message to append\n * @param options Additional options to pass to the API call\n */\n append: (\n message: Message | CreateMessage,\n chatRequestOptions?: ChatRequestOptions,\n ) => Promise<string | null | undefined>;\n /**\n * Reload the last AI chat response for the given chat history. If the last\n * message isn't from the assistant, it will request the API to generate a\n * new response.\n */\n reload: (\n chatRequestOptions?: ChatRequestOptions,\n ) => Promise<string | null | undefined>;\n /**\n * Abort the current request immediately, keep the generated tokens if any.\n */\n stop: () => void;\n /**\n * Update the `messages` state locally. This is useful when you want to\n * edit the messages on the client, and then trigger the `reload` method\n * manually to regenerate the AI response.\n */\n setMessages: (\n messages: Message[] | ((messages: Message[]) => Message[]),\n ) => void;\n /** The current value of the input */\n input: string;\n /** setState-powered method to update the input value */\n setInput: React.Dispatch<React.SetStateAction<string>>;\n /** An input/textarea-ready onChange handler to control the value of the input */\n handleInputChange: (\n e:\n | React.ChangeEvent<HTMLInputElement>\n | React.ChangeEvent<HTMLTextAreaElement>,\n ) => void;\n /** Form submission handler to automatically reset input and append a user message */\n handleSubmit: (\n event?: { preventDefault?: () => void },\n chatRequestOptions?: ChatRequestOptions,\n ) => void;\n metadata?: Object;\n /** Whether the API request is in progress */\n isLoading: boolean;\n /** Additional data added on the server via StreamData */\n data?: JSONValue[];\n};\n\nconst getStreamedResponse = async (\n api: string,\n chatRequest: ChatRequest,\n mutate: KeyedMutator<Message[]>,\n mutateStreamData: KeyedMutator<JSONValue[] | undefined>,\n existingData: JSONValue[] | undefined,\n extraMetadataRef: React.MutableRefObject<any>,\n messagesRef: React.MutableRefObject<Message[]>,\n abortControllerRef: React.MutableRefObject<AbortController | null>,\n generateId: IdGenerator,\n streamMode: 'stream-data' | 'text' | undefined,\n onFinish: ((message: Message) => void) | undefined,\n onResponse: ((response: Response) => void | Promise<void>) | undefined,\n onToolCall: UseChatOptions['onToolCall'] | undefined,\n sendExtraMessageFields: boolean | undefined,\n experimental_prepareRequestBody:\n | ((options: {\n messages: Message[];\n requestData?: JSONValue;\n requestBody?: object;\n }) => JSONValue)\n | undefined,\n fetch: FetchFunction | undefined,\n keepLastMessageOnError: boolean,\n) => {\n // Do an optimistic update to the chat state to show the updated messages immediately:\n const previousMessages = messagesRef.current;\n mutate(chatRequest.messages, false);\n\n const constructedMessagesPayload = sendExtraMessageFields\n ? chatRequest.messages\n : chatRequest.messages.map(\n ({\n role,\n content,\n experimental_attachments,\n name,\n data,\n annotations,\n toolInvocations,\n function_call,\n tool_calls,\n tool_call_id,\n }) => ({\n role,\n content,\n ...(experimental_attachments !== undefined && {\n experimental_attachments,\n }),\n ...(name !== undefined && { name }),\n ...(data !== undefined && { data }),\n ...(annotations !== undefined && { annotations }),\n ...(toolInvocations !== undefined && { toolInvocations }),\n // outdated function/tool call handling (TODO deprecate):\n tool_call_id,\n ...(function_call !== undefined && { function_call }),\n ...(tool_calls !== undefined && { tool_calls }),\n }),\n );\n\n return await callChatApi({\n api,\n body: experimental_prepareRequestBody?.({\n messages: chatRequest.messages,\n requestData: chatRequest.data,\n requestBody: chatRequest.body,\n }) ?? {\n messages: constructedMessagesPayload,\n data: chatRequest.data,\n ...extraMetadataRef.current.body,\n ...chatRequest.body,\n ...(chatRequest.functions !== undefined && {\n functions: chatRequest.functions,\n }),\n ...(chatRequest.function_call !== undefined && {\n function_call: chatRequest.function_call,\n }),\n ...(chatRequest.tools !== undefined && {\n tools: chatRequest.tools,\n }),\n ...(chatRequest.tool_choice !== undefined && {\n tool_choice: chatRequest.tool_choice,\n }),\n },\n streamMode,\n credentials: extraMetadataRef.current.credentials,\n headers: {\n ...extraMetadataRef.current.headers,\n ...chatRequest.headers,\n },\n abortController: () => abortControllerRef.current,\n restoreMessagesOnFailure() {\n if (!keepLastMessageOnError) {\n mutate(previousMessages, false);\n }\n },\n onResponse,\n onUpdate(merged, data) {\n mutate([...chatRequest.messages, ...merged], false);\n mutateStreamData([...(existingData || []), ...(data || [])], false);\n },\n onToolCall,\n onFinish,\n generateId,\n fetch,\n });\n};\n\nexport function useChat({\n api = '/api/chat',\n id,\n initialMessages,\n initialInput = '',\n sendExtraMessageFields,\n experimental_onFunctionCall,\n experimental_onToolCall,\n onToolCall,\n experimental_prepareRequestBody,\n experimental_maxAutomaticRoundtrips = 0,\n maxAutomaticRoundtrips = experimental_maxAutomaticRoundtrips,\n maxToolRoundtrips = maxAutomaticRoundtrips,\n streamMode,\n onResponse,\n onFinish,\n onError,\n credentials,\n headers,\n body,\n generateId = generateIdFunc,\n fetch,\n keepLastMessageOnError = false,\n}: UseChatOptions & {\n key?: string;\n\n /**\n@deprecated Use `maxToolRoundtrips` instead.\n */\n experimental_maxAutomaticRoundtrips?: number;\n\n /**\n@deprecated Use `maxToolRoundtrips` instead.\n */\n maxAutomaticRoundtrips?: number;\n\n /**\n * Experimental (React only). When a function is provided, it will be used\n * to prepare the request body for the chat API. This can be useful for\n * customizing the request body based on the messages and data in the chat.\n *\n * @param messages The current messages in the chat.\n * @param requestData The data object passed in the chat request.\n * @param requestBody The request body object passed in the chat request.\n */\n experimental_prepareRequestBody?: (options: {\n messages: Message[];\n requestData?: JSONValue;\n requestBody?: object;\n }) => JSONValue;\n\n /**\nMaximal number of automatic roundtrips for tool calls.\n\nAn automatic tool call roundtrip is a call to the server with the\ntool call results when all tool calls in the last assistant\nmessage have results.\n\nA maximum number is required to prevent infinite loops in the\ncase of misconfigured tools.\n\nBy default, it's set to 0, which will disable the feature.\n */\n maxToolRoundtrips?: number;\n} = {}): UseChatHelpers & {\n /**\n * @deprecated Use `addToolResult` instead.\n */\n experimental_addToolResult: ({\n toolCallId,\n result,\n }: {\n toolCallId: string;\n result: any;\n }) => void;\n addToolResult: ({\n toolCallId,\n result,\n }: {\n toolCallId: string;\n result: any;\n }) => void;\n} {\n // Generate a unique id for the chat if not provided.\n const hookId = useId();\n const idKey = id ?? hookId;\n const chatKey = typeof api === 'string' ? [api, idKey] : idKey;\n\n // Store a empty array as the initial messages\n // (instead of using a default parameter value that gets re-created each time)\n // to avoid re-renders:\n const [initialMessagesFallback] = useState([]);\n\n // Store the chat state in SWR, using the chatId as the key to share states.\n const { data: messages, mutate } = useSWR<Message[]>(\n [chatKey, 'messages'],\n null,\n { fallbackData: initialMessages ?? initialMessagesFallback },\n );\n\n // We store loading state in another hook to sync loading states across hook invocations\n const { data: isLoading = false, mutate: mutateLoading } = useSWR<boolean>(\n [chatKey, 'loading'],\n null,\n );\n\n const { data: streamData, mutate: mutateStreamData } = useSWR<\n JSONValue[] | undefined\n >([chatKey, 'streamData'], null);\n\n const { data: error = undefined, mutate: setError } = useSWR<\n undefined | Error\n >([chatKey, 'error'], null);\n\n // Keep the latest messages in a ref.\n const messagesRef = useRef<Message[]>(messages || []);\n useEffect(() => {\n messagesRef.current = messages || [];\n }, [messages]);\n\n // Abort controller to cancel the current API call.\n const abortControllerRef = useRef<AbortController | null>(null);\n\n const extraMetadataRef = useRef({\n credentials,\n headers,\n body,\n });\n\n useEffect(() => {\n extraMetadataRef.current = {\n credentials,\n headers,\n body,\n };\n }, [credentials, headers, body]);\n\n const triggerRequest = useCallback(\n async (chatRequest: ChatRequest) => {\n const messageCount = messagesRef.current.length;\n\n try {\n mutateLoading(true);\n setError(undefined);\n\n const abortController = new AbortController();\n abortControllerRef.current = abortController;\n\n await processChatStream({\n getStreamedResponse: () =>\n getStreamedResponse(\n api,\n chatRequest,\n mutate,\n mutateStreamData,\n streamData!,\n extraMetadataRef,\n messagesRef,\n abortControllerRef,\n generateId,\n streamMode,\n onFinish,\n onResponse,\n onToolCall,\n sendExtraMessageFields,\n experimental_prepareRequestBody,\n fetch,\n keepLastMessageOnError,\n ),\n experimental_onFunctionCall,\n experimental_onToolCall,\n updateChatRequest: chatRequestParam => {\n chatRequest = chatRequestParam;\n },\n getCurrentMessages: () => messagesRef.current,\n });\n\n abortControllerRef.current = null;\n } catch (err) {\n // Ignore abort errors as they are expected.\n if ((err as any).name === 'AbortError') {\n abortControllerRef.current = null;\n return null;\n }\n\n if (onError && err instanceof Error) {\n onError(err);\n }\n\n setError(err as Error);\n } finally {\n mutateLoading(false);\n }\n\n // auto-submit when all tool calls in the last assistant message have results:\n const messages = messagesRef.current;\n const lastMessage = messages[messages.length - 1];\n if (\n // ensure we actually have new messages (to prevent infinite loops in case of errors):\n messages.length > messageCount &&\n // ensure there is a last message:\n lastMessage != null &&\n // check if the feature is enabled:\n maxToolRoundtrips > 0 &&\n // check that roundtrip is possible:\n isAssistantMessageWithCompletedToolCalls(lastMessage) &&\n // limit the number of automatic roundtrips:\n countTrailingAssistantMessages(messages) <= maxToolRoundtrips\n ) {\n await triggerRequest({ messages });\n }\n },\n [\n mutate,\n mutateLoading,\n api,\n extraMetadataRef,\n onResponse,\n onFinish,\n onError,\n setError,\n mutateStreamData,\n streamData,\n streamMode,\n sendExtraMessageFields,\n experimental_onFunctionCall,\n experimental_onToolCall,\n experimental_prepareRequestBody,\n onToolCall,\n maxToolRoundtrips,\n messagesRef,\n abortControllerRef,\n generateId,\n fetch,\n keepLastMessageOnError,\n ],\n );\n\n const append = useCallback(\n async (\n message: Message | CreateMessage,\n {\n options,\n functions,\n function_call,\n tools,\n tool_choice,\n data,\n headers,\n body,\n }: ChatRequestOptions = {},\n ) => {\n if (!message.id) {\n message.id = generateId();\n }\n\n const requestOptions = {\n headers: headers ?? options?.headers,\n body: body ?? options?.body,\n };\n\n const chatRequest: ChatRequest = {\n messages: messagesRef.current.concat(message as Message),\n options: requestOptions,\n headers: requestOptions.headers,\n body: requestOptions.body,\n data,\n ...(functions !== undefined && { functions }),\n ...(function_call !== undefined && { function_call }),\n ...(tools !== undefined && { tools }),\n ...(tool_choice !== undefined && { tool_choice }),\n };\n\n return triggerRequest(chatRequest);\n },\n [triggerRequest, generateId],\n );\n\n const reload = useCallback(\n async ({\n options,\n functions,\n function_call,\n tools,\n tool_choice,\n data,\n headers,\n body,\n }: ChatRequestOptions = {}) => {\n if (messagesRef.current.length === 0) return null;\n\n const requestOptions = {\n headers: headers ?? options?.headers,\n body: body ?? options?.body,\n };\n\n // Remove last assistant message and retry last user message.\n const lastMessage = messagesRef.current[messagesRef.current.length - 1];\n if (lastMessage.role === 'assistant') {\n const chatRequest: ChatRequest = {\n messages: messagesRef.current.slice(0, -1),\n options: requestOptions,\n headers: requestOptions.headers,\n body: requestOptions.body,\n data,\n ...(functions !== undefined && { functions }),\n ...(function_call !== undefined && { function_call }),\n ...(tools !== undefined && { tools }),\n ...(tool_choice !== undefined && { tool_choice }),\n };\n\n return triggerRequest(chatRequest);\n }\n\n const chatRequest: ChatRequest = {\n messages: messagesRef.current,\n options: requestOptions,\n headers: requestOptions.headers,\n body: requestOptions.body,\n data,\n ...(functions !== undefined && { functions }),\n ...(function_call !== undefined && { function_call }),\n ...(tools !== undefined && { tools }),\n ...(tool_choice !== undefined && { tool_choice }),\n };\n\n return triggerRequest(chatRequest);\n },\n [triggerRequest],\n );\n\n const stop = useCallback(() => {\n if (abortControllerRef.current) {\n abortControllerRef.current.abort();\n abortControllerRef.current = null;\n }\n }, []);\n\n const setMessages = useCallback(\n (messages: Message[] | ((messages: Message[]) => Message[])) => {\n if (typeof messages === 'function') {\n messages = messages(messagesRef.current);\n }\n\n mutate(messages, false);\n messagesRef.current = messages;\n },\n [mutate],\n );\n\n // Input state and handlers.\n const [input, setInput] = useState(initialInput);\n\n const handleSubmit = useCallback(\n async (\n event?: { preventDefault?: () => void },\n options: ChatRequestOptions = {},\n metadata?: Object,\n ) => {\n event?.preventDefault?.();\n\n if (!input && !options.allowEmptySubmit) return;\n\n if (metadata) {\n extraMetadataRef.current = {\n ...extraMetadataRef.current,\n ...metadata,\n };\n }\n\n const attachmentsForRequest: Attachment[] = [];\n const attachmentsFromOptions = options.experimental_attachments;\n\n if (attachmentsFromOptions) {\n if (attachmentsFromOptions instanceof FileList) {\n for (const attachment of Array.from(attachmentsFromOptions)) {\n const { name, type } = attachment;\n\n const dataUrl = await new Promise<string>((resolve, reject) => {\n const reader = new FileReader();\n reader.onload = readerEvent => {\n resolve(readerEvent.target?.result as string);\n };\n reader.onerror = error => reject(error);\n reader.readAsDataURL(attachment);\n });\n\n attachmentsForRequest.push({\n name,\n contentType: type,\n url: dataUrl,\n });\n }\n } else if (Array.isArray(attachmentsFromOptions)) {\n for (const file of attachmentsFromOptions) {\n const { name, url, contentType } = file;\n\n attachmentsForRequest.push({\n name,\n contentType,\n url,\n });\n }\n } else {\n throw new Error('Invalid attachments type');\n }\n }\n\n const requestOptions = {\n headers: options.headers ?? options.options?.headers,\n body: options.body ?? options.options?.body,\n };\n\n const messages =\n !input && options.allowEmptySubmit\n ? messagesRef.current\n : messagesRef.current.concat({\n id: generateId(),\n createdAt: new Date(),\n role: 'user',\n content: input,\n experimental_attachments:\n attachmentsForRequest.length > 0\n ? attachmentsForRequest\n : undefined,\n });\n\n const chatRequest: ChatRequest = {\n messages,\n options: requestOptions,\n headers: requestOptions.headers,\n body: requestOptions.body,\n data: options.data,\n };\n\n triggerRequest(chatRequest);\n\n setInput('');\n },\n [input, generateId, triggerRequest],\n );\n\n const handleInputChange = (e: any) => {\n setInput(e.target.value);\n };\n\n const addToolResult = ({\n toolCallId,\n result,\n }: {\n toolCallId: string;\n result: any;\n }) => {\n const updatedMessages = messagesRef.current.map((message, index, arr) =>\n // update the tool calls in the last assistant message:\n index === arr.length - 1 &&\n message.role === 'assistant' &&\n message.toolInvocations\n ? {\n ...message,\n toolInvocations: message.toolInvocations.map(toolInvocation =>\n toolInvocation.toolCallId === toolCallId\n ? { ...toolInvocation, result }\n : toolInvocation,\n ),\n }\n : message,\n );\n\n mutate(updatedMessages, false);\n\n // auto-submit when all tool calls in the last assistant message have results:\n const lastMessage = updatedMessages[updatedMessages.length - 1];\n if (isAssistantMessageWithCompletedToolCalls(lastMessage)) {\n triggerRequest({ messages: updatedMessages });\n }\n };\n\n return {\n messages: messages || [],\n error,\n append,\n reload,\n stop,\n setMessages,\n input,\n setInput,\n handleInputChange,\n handleSubmit,\n isLoading,\n data: streamData,\n addToolResult,\n experimental_addToolResult: addToolResult,\n };\n}\n\n/**\nCheck if the message is an assistant message with completed tool calls.\nThe message must have at least one tool invocation and all tool invocations\nmust have a result.\n */\nfunction isAssistantMessageWithCompletedToolCalls(message: Message) {\n return (\n message.role === 'assistant' &&\n message.toolInvocations &&\n message.toolInvocations.length > 0 &&\n message.toolInvocations.every(toolInvocation => 'result' in toolInvocation)\n );\n}\n\n/**\nReturns the number of trailing assistant messages in the array.\n */\nfunction countTrailingAssistantMessages(messages: Message[]) {\n let count = 0;\n for (let i = messages.length - 1; i >= 0; i--) {\n if (messages[i].role === 'assistant') {\n count++;\n } else {\n break;\n }\n }\n return count;\n}\n","import {\n JSONValue,\n RequestOptions,\n UseCompletionOptions,\n callCompletionApi,\n} from '@ai-sdk/ui-utils';\nimport { useCallback, useEffect, useId, useRef, useState } from 'react';\nimport useSWR from 'swr';\n\nexport type { UseCompletionOptions };\n\nexport type UseCompletionHelpers = {\n /** The current completion result */\n completion: string;\n /**\n * Send a new prompt to the API endpoint and update the completion state.\n */\n complete: (\n prompt: string,\n options?: RequestOptions,\n ) => Promise<string | null | undefined>;\n /** The error object of the API request */\n error: undefined | Error;\n /**\n * Abort the current API request but keep the generated tokens.\n */\n stop: () => void;\n /**\n * Update the `completion` state locally.\n */\n setCompletion: (completion: string) => void;\n /** The current value of the input */\n input: string;\n /** setState-powered method to update the input value */\n setInput: React.Dispatch<React.SetStateAction<string>>;\n /**\n * An input/textarea-ready onChange handler to control the value of the input\n * @example\n * ```jsx\n * <input onChange={handleInputChange} value={input} />\n * ```\n */\n handleInputChange: (\n event:\n | React.ChangeEvent<HTMLInputElement>\n | React.ChangeEvent<HTMLTextAreaElement>,\n ) => void;\n\n /**\n * Form submission handler to automatically reset input and append a user message\n * @example\n * ```jsx\n * <form onSubmit={handleSubmit}>\n * <input onChange={handleInputChange} value={input} />\n * </form>\n * ```\n */\n handleSubmit: (event?: { preventDefault?: () => void }) => void;\n\n /** Whether the API request is in progress */\n isLoading: boolean;\n /** Additional data added on the server via StreamData */\n data?: JSONValue[];\n};\n\nexport function useCompletion({\n api = '/api/completion',\n id,\n initialCompletion = '',\n initialInput = '',\n credentials,\n headers,\n body,\n streamMode,\n fetch,\n onResponse,\n onFinish,\n onError,\n}: UseCompletionOptions = {}): UseCompletionHelpers {\n // Generate an unique id for the completion if not provided.\n const hookId = useId();\n const completionId = id || hookId;\n\n // Store the completion state in SWR, using the completionId as the key to share states.\n const { data, mutate } = useSWR<string>([api, completionId], null, {\n fallbackData: initialCompletion,\n });\n\n const { data: isLoading = false, mutate: mutateLoading } = useSWR<boolean>(\n [completionId, 'loading'],\n null,\n );\n\n const { data: streamData, mutate: mutateStreamData } = useSWR<\n JSONValue[] | undefined\n >([completionId, 'streamData'], null);\n\n const [error, setError] = useState<undefined | Error>(undefined);\n const completion = data!;\n\n // Abort controller to cancel the current API call.\n const [abortController, setAbortController] =\n useState<AbortController | null>(null);\n\n const extraMetadataRef = useRef({\n credentials,\n headers,\n body,\n });\n useEffect(() => {\n extraMetadataRef.current = {\n credentials,\n headers,\n body,\n };\n }, [credentials, headers, body]);\n\n const triggerRequest = useCallback(\n async (prompt: string, options?: RequestOptions) =>\n callCompletionApi({\n api,\n prompt,\n credentials: extraMetadataRef.current.credentials,\n headers: { ...extraMetadataRef.current.headers, ...options?.headers },\n body: {\n ...extraMetadataRef.current.body,\n ...options?.body,\n },\n streamMode,\n fetch,\n setCompletion: completion => mutate(completion, false),\n setLoading: mutateLoading,\n setError,\n setAbortController,\n onResponse,\n onFinish,\n onError,\n onData: data => {\n mutateStreamData([...(streamData || []), ...(data || [])], false);\n },\n }),\n [\n mutate,\n mutateLoading,\n api,\n extraMetadataRef,\n setAbortController,\n onResponse,\n onFinish,\n onError,\n setError,\n streamData,\n streamMode,\n fetch,\n mutateStreamData,\n ],\n );\n\n const stop = useCallback(() => {\n if (abortController) {\n abortController.abort();\n setAbortController(null);\n }\n }, [abortController]);\n\n const setCompletion = useCallback(\n (completion: string) => {\n mutate(completion, false);\n },\n [mutate],\n );\n\n const complete = useCallback<UseCompletionHelpers['complete']>(\n async (prompt, options) => {\n return triggerRequest(prompt, options);\n },\n [triggerRequest],\n );\n\n const [input, setInput] = useState(initialInput);\n\n const handleSubmit = useCallback(\n (event?: { preventDefault?: () => void }) => {\n event?.preventDefault?.();\n return input ? complete(input) : undefined;\n },\n [input, complete],\n );\n\n const handleInputChange = (e: any) => {\n setInput(e.target.value);\n };\n\n return {\n completion,\n complete,\n error,\n setCompletion,\n stop,\n input,\n setInput,\n handleInputChange,\n handleSubmit,\n isLoading,\n data: streamData,\n };\n}\n","import { isAbortError } from '@ai-sdk/provider-utils';\nimport {\n DeepPartial,\n FetchFunction,\n isDeepEqualData,\n parsePartialJson,\n} from '@ai-sdk/ui-utils';\nimport { useCallback, useId, useRef, useState } from 'react';\nimport useSWR from 'swr';\nimport z from 'zod';\n\n// use function to allow for mocking in tests:\nconst getOriginalFetch = () => fetch;\n\nexport type Experimental_UseObjectOptions<RESULT> = {\n /**\n * The API endpoint. It should stream JSON that matches the schema as chunked text.\n */\n api: string;\n\n /**\n * A Zod schema that defines the shape of the complete object.\n */\n schema: z.Schema<RESULT>;\n\n /**\n * An unique identifier. If not provided, a random one will be\n * generated. When provided, the `useObject` hook with the same `id` will\n * have shared states across components.\n */\n id?: string;\n\n /**\n * An optional value for the initial object.\n */\n initialValue?: DeepPartial<RESULT>;\n\n /**\nCustom fetch implementation. You can use it as a middleware to intercept requests,\nor to provide a custom fetch implementation for e.g. testing.\n */\n fetch?: FetchFunction;\n\n /**\n * Callback function to be called when an error is encountered.\n */\n onError?: (error: Error) => void;\n};\n\nexport type Experimental_UseObjectHelpers<RESULT, INPUT> = {\n /**\n * @deprecated Use `submit` instead.\n */\n setInput: (input: INPUT) => void;\n\n /**\n * Calls the API with the provided input as JSON body.\n */\n submit: (input: INPUT) => void;\n\n /**\n * The current value for the generated object. Updated as the API streams JSON chunks.\n */\n object: DeepPartial<RESULT> | undefined;\n\n /**\n * The error object of the API request if any.\n */\n error: undefined | unknown;\n\n /**\n * Flag that indicates whether an API request is in progress.\n */\n isLoading: boolean;\n\n /**\n * Abort the current request immediately, keep the current partial object if any.\n */\n stop: () => void;\n};\n\nfunction useObject<RESULT, INPUT = any>({\n api,\n id,\n schema, // required, in the future we will use it for validation\n initialValue,\n fetch,\n onError,\n}: Experimental_UseObjectOptions<RESULT>): Experimental_UseObjectHelpers<\n RESULT,\n INPUT\n> {\n // Generate an unique id if not provided.\n const hookId = useId();\n const completionId = id ?? hookId;\n\n // Store the completion state in SWR, using the completionId as the key to share states.\n const { data, mutate } = useSWR<DeepPartial<RESULT>>(\n [api, completionId],\n null,\n { fallbackData: initialValue },\n );\n\n const [error, setError] = useState<undefined | unknown>(undefined);\n const [isLoading, setIsLoading] = useState(false);\n\n // Abort controller to cancel the current API call.\n const abortControllerRef = useRef<AbortController | null>(null);\n\n const stop = useCallback(() => {\n try {\n abortControllerRef.current?.abort();\n } catch (ignored) {\n } finally {\n setIsLoading(false);\n abortControllerRef.current = null;\n }\n }, []);\n\n const submit = async (input: INPUT) => {\n try {\n setIsLoading(true);\n setError(undefined);\n\n const abortController = new AbortController();\n abortControllerRef.current = abortController;\n\n const actualFetch = fetch ?? getOriginalFetch();\n const response = await actualFetch(api, {\n method: 'POST',\n headers: { 'Content-Type': 'application/json' },\n signal: abortController.signal,\n body: JSON.stringify(input),\n });\n\n if (!response.ok) {\n throw new Error(\n (await response.text()) ?? 'Failed to fetch the response.',\n );\n }\n\n if (response.body == null) {\n throw new Error('The response body is empty.');\n }\n\n let accumulatedText = '';\n let latestObject: DeepPartial<RESULT> | undefined = undefined;\n\n await response.body.pipeThrough(new TextDecoderStream()).pipeTo(\n new WritableStream<string>({\n write(chunk) {\n accumulatedText += chunk;\n\n const currentObject = parsePartialJson(\n accumulatedText,\n ) as DeepPartial<RESULT>;\n\n if (!isDeepEqualData(latestObject, currentObject)) {\n latestObject = currentObject;\n\n mutate(currentObject);\n }\n },\n\n close() {\n setIsLoading(false);\n abortControllerRef.current = null;\n },\n }),\n );\n } catch (error) {\n if (isAbortError(error)) {\n return;\n }\n\n if (onError && error instanceof Error) {\n onError(error);\n }\n\n setError(error);\n }\n };\n\n return {\n setInput: submit, // Deprecated\n submit,\n object: data,\n error,\n isLoading,\n stop,\n };\n}\n\nexport const experimental_useObject = useObject;\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,4BAA6B;AAC7B,sBAOO;AACP,mBAA8C;AAG9C,IAAM,mBAAmB,MAAM;AAiFxB,SAAS,aAAa;AAAA,EAC3B;AAAA,EACA,UAAU;AAAA,EACV;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA,OAAAA;AACF,GAA6C;AAC3C,QAAM,CAAC,UAAU,WAAW,QAAI,uBAAoB,CAAC,CAAC;AACtD,QAAM,CAAC,OAAO,QAA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ACtB,UAAI,eAAgD;AAEpD,YAAM,SAAS,KAAK,YAAY,IAAI,kBAAkB,CAAC,EAAE;AAAA,QACvD,IAAI,eAAuB;AAAA,UACzB,MAAM,OAAO;AACX,+BAAmB;AAEnB,kBAAM,oBAAgB;AAAA,cACpB;AAAA,YACF;AAEA,gBAAI,KAAC,kCAAgB,cAAc,aAAa,GAAG;AACjD,6BAAe;AAEf,qBAAO,aAAa;AAAA,YACtB;AAAA,UACF;AAAA,UAEA,QAAQ;AACN,yBAAa,KAAK;AAClB,+BAAmB,UAAU;AAAA,UAC/B;AAAA,QACF,CAAC;AAAA,MACH;AAAA,IACF,SAASG,QAAO;AACd,cAAI,qCAAaA,MAAK,GAAG;AACvB;AAAA,MACF;AAEA,UAAI,WAAWA,kBAAiB,OAAO;AACrC,gBAAQA,MAAK;AAAA,MACf;AAEA,eAASA,MAAK;AAAA,IAChB;AAAA,EACF;AAEA,SAAO;AAAA,IACL,UAAU;AAAA;AAAA,IACV;AAAA,IACA,QAAQ;AAAA,IACR;AAAA,IACA;AAAA,IACA;AAAA,EACF;AACF;AAEO,IAAM,yBAAyB;","names":["fetch","messages","_a","error","import_ui_utils","import_react","generateId","fetch","generateIdFunc","useSWR","messages","headers","body","chatRequest","_a","error","import_ui_utils","import_react","import_swr","fetch","useSWR","completion","data","import_provider_utils","import_ui_utils","import_react","import_swr","getOriginalFetch","fetch","useSWR","error"]}
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/use-assistant.ts","../src/use-chat.ts","../src/use-completion.ts","../src/use-object.ts"],"sourcesContent":["export * from './use-assistant';\nexport * from './use-chat';\nexport * from './use-completion';\nexport * from './use-object';\n","import { isAbortError } from '@ai-sdk/provider-utils';\nimport {\n AssistantStatus,\n CreateMessage,\n Message,\n UseAssistantOptions,\n generateId,\n readDataStream,\n} from '@ai-sdk/ui-utils';\nimport { useCallback, useRef, useState } from 'react';\n\n// use function to allow for mocking in tests:\nconst getOriginalFetch = () => fetch;\n\nexport type UseAssistantHelpers = {\n /**\n * The current array of chat messages.\n */\n messages: Message[];\n\n /**\n * Update the message store with a new array of messages.\n */\n setMessages: React.Dispatch<React.SetStateAction<Message[]>>;\n\n /**\n * The current thread ID.\n */\n threadId: string | undefined;\n\n /**\n * Set the current thread ID. Specifying a thread ID will switch to that thread, if it exists. If set to 'undefined', a new thread will be created. For both cases, `threadId` will be updated with the new value and `messages` will be cleared.\n */\n setThreadId: (threadId: string | undefined) => void;\n\n /**\n * The current value of the input field.\n */\n input: string;\n\n /**\n * Append a user message to the chat list. This triggers the API call to fetch\n * the assistant's response.\n * @param message The message to append\n * @param requestOptions Additional options to pass to the API call\n */\n append: (\n message: Message | CreateMessage,\n requestOptions?: {\n data?: Record<string, string>;\n },\n ) => Promise<void>;\n\n /**\nAbort the current request immediately, keep the generated tokens if any.\n */\n stop: () => void;\n\n /**\n * setState-powered method to update the input value.\n */\n setInput: React.Dispatch<React.SetStateAction<string>>;\n\n /**\n * Handler for the `onChange` event of the input field to control the input's value.\n */\n handleInputChange: (\n event:\n | React.ChangeEvent<HTMLInputElement>\n | React.ChangeEvent<HTMLTextAreaElement>,\n ) => void;\n\n /**\n * Form submission handler that automatically resets the input field and appends a user message.\n */\n submitMessage: (\n event?: React.FormEvent<HTMLFormElement>,\n requestOptions?: {\n data?: Record<string, string>;\n },\n ) => Promise<void>;\n\n /**\n * The current status of the assistant. This can be used to show a loading indicator.\n */\n status: AssistantStatus;\n\n /**\n * The error thrown during the assistant message processing, if any.\n */\n error: undefined | Error;\n};\n\nexport function useAssistant({\n api,\n threadId: threadIdParam,\n credentials,\n headers,\n body,\n onError,\n fetch,\n}: UseAssistantOptions): UseAssistantHelpers {\n const [messages, setMessages] = useState<Message[]>([]);\n const [input, setInput] = useState('');\n const [currentThreadId, setCurrentThreadId] = useState<string | undefined>(\n undefined,\n );\n const [status, setStatus] = useState<AssistantStatus>('awaiting_message');\n const [error, setError] = useState<undefined | Error>(undefined);\n\n const handleInputChange = (\n event:\n | React.ChangeEvent<HTMLInputElement>\n | React.ChangeEvent<HTMLTextAreaElement>,\n ) => {\n setInput(event.target.value);\n };\n\n // Abort controller to cancel the current API call.\n const abortControllerRef = useRef<AbortController | null>(null);\n\n const stop = useCallback(() => {\n if (abortControllerRef.current) {\n abortControllerRef.current.abort();\n abortControllerRef.current = null;\n }\n }, []);\n\n const append = async (\n message: Message | CreateMessage,\n requestOptions?: {\n data?: Record<string, string>;\n },\n ) => {\n setStatus('in_progress');\n\n setMessages(messages => [\n ...messages,\n {\n ...message,\n id: message.id ?? generateId(),\n },\n ]);\n\n setInput('');\n\n const abortController = new AbortController();\n\n try {\n abortControllerRef.current = abortController;\n\n const actualFetch = fetch ?? getOriginalFetch();\n const response = await actualFetch(api, {\n method: 'POST',\n credentials,\n signal: abortController.signal,\n headers: { 'Content-Type': 'application/json', ...headers },\n body: JSON.stringify({\n ...body,\n // always use user-provided threadId when available:\n threadId: threadIdParam ?? currentThreadId ?? null,\n message: message.content,\n\n // optional request data:\n data: requestOptions?.data,\n }),\n });\n\n if (!response.ok) {\n throw new Error(\n (await response.text()) ?? 'Failed to fetch the assistant response.',\n );\n }\n\n if (response.body == null) {\n throw new Error('The response body is empty.');\n }\n\n for await (const { type, value } of readDataStream(\n response.body.getReader(),\n )) {\n switch (type) {\n case 'assistant_message': {\n setMessages(messages => [\n ...messages,\n {\n id: value.id,\n role: value.role,\n content: value.content[0].text.value,\n },\n ]);\n break;\n }\n\n case 'text': {\n // text delta - add to last message:\n setMessages(messages => {\n const lastMessage = messages[messages.length - 1];\n return [\n ...messages.slice(0, messages.length - 1),\n {\n id: lastMessage.id,\n role: lastMessage.role,\n content: lastMessage.content + value,\n },\n ];\n });\n\n break;\n }\n\n case 'data_message': {\n setMessages(messages => [\n ...messages,\n {\n id: value.id ?? generateId(),\n role: 'data',\n content: '',\n data: value.data,\n },\n ]);\n break;\n }\n\n case 'assistant_control_data': {\n setCurrentThreadId(value.threadId);\n\n // set id of last message:\n setMessages(messages => {\n const lastMessage = messages[messages.length - 1];\n lastMessage.id = value.messageId;\n return [...messages.slice(0, messages.length - 1), lastMessage];\n });\n\n break;\n }\n\n case 'error': {\n setError(new Error(value));\n break;\n }\n }\n }\n } catch (error) {\n // Ignore abort errors as they are expected when the user cancels the request:\n if (isAbortError(error) && abortController.signal.aborted) {\n abortControllerRef.current = null;\n return;\n }\n\n if (onError && error instanceof Error) {\n onError(error);\n }\n\n setError(error as Error);\n } finally {\n abortControllerRef.current = null;\n setStatus('awaiting_message');\n }\n };\n\n const submitMessage = async (\n event?: React.FormEvent<HTMLFormElement>,\n requestOptions?: {\n data?: Record<string, string>;\n },\n ) => {\n event?.preventDefault?.();\n\n if (input === '') {\n return;\n }\n\n append({ role: 'user', content: input }, requestOptions);\n };\n\n const setThreadId = (threadId: string | undefined) => {\n setCurrentThreadId(threadId);\n setMessages([]);\n };\n\n return {\n append,\n messages,\n setMessages,\n threadId: currentThreadId,\n setThreadId,\n input,\n setInput,\n handleInputChange,\n submitMessage,\n status,\n error,\n stop,\n };\n}\n\n/**\n@deprecated Use `useAssistant` instead.\n */\nexport const experimental_useAssistant = useAssistant;\n","import type {\n ChatRequest,\n ChatRequestOptions,\n Attachment,\n CreateMessage,\n FetchFunction,\n IdGenerator,\n JSONValue,\n Message,\n UseChatOptions,\n} from '@ai-sdk/ui-utils';\nimport {\n callChatApi,\n generateId as generateIdFunc,\n processChatStream,\n} from '@ai-sdk/ui-utils';\nimport { useCallback, useEffect, useId, useRef, useState } from 'react';\nimport useSWR, { KeyedMutator } from 'swr';\n\nexport type { CreateMessage, Message, UseChatOptions };\n\nexport type UseChatHelpers = {\n /** Current messages in the chat */\n messages: Message[];\n /** The error object of the API request */\n error: undefined | Error;\n /**\n * Append a user message to the chat list. This triggers the API call to fetch\n * the assistant's response.\n * @param message The message to append\n * @param options Additional options to pass to the API call\n */\n append: (\n message: Message | CreateMessage,\n chatRequestOptions?: ChatRequestOptions,\n ) => Promise<string | null | undefined>;\n /**\n * Reload the last AI chat response for the given chat history. If the last\n * message isn't from the assistant, it will request the API to generate a\n * new response.\n */\n reload: (\n chatRequestOptions?: ChatRequestOptions,\n ) => Promise<string | null | undefined>;\n /**\n * Abort the current request immediately, keep the generated tokens if any.\n */\n stop: () => void;\n /**\n * Update the `messages` state locally. This is useful when you want to\n * edit the messages on the client, and then trigger the `reload` method\n * manually to regenerate the AI response.\n */\n setMessages: (\n messages: Message[] | ((messages: Message[]) => Message[]),\n ) => void;\n /** The current value of the input */\n input: string;\n /** setState-powered method to update the input value */\n setInput: React.Dispatch<React.SetStateAction<string>>;\n /** An input/textarea-ready onChange handler to control the value of the input */\n handleInputChange: (\n e:\n | React.ChangeEvent<HTMLInputElement>\n | React.ChangeEvent<HTMLTextAreaElement>,\n ) => void;\n /** Form submission handler to automatically reset input and append a user message */\n handleSubmit: (\n event?: { preventDefault?: () => void },\n chatRequestOptions?: ChatRequestOptions,\n ) => void;\n metadata?: Object;\n /** Whether the API request is in progress */\n isLoading: boolean;\n /** Additional data added on the server via StreamData */\n data?: JSONValue[];\n};\n\nconst getStreamedResponse = async (\n api: string,\n chatRequest: ChatRequest,\n mutate: KeyedMutator<Message[]>,\n mutateStreamData: KeyedMutator<JSONValue[] | undefined>,\n existingData: JSONValue[] | undefined,\n extraMetadataRef: React.MutableRefObject<any>,\n messagesRef: React.MutableRefObject<Message[]>,\n abortControllerRef: React.MutableRefObject<AbortController | null>,\n generateId: IdGenerator,\n streamProtocol: UseChatOptions['streamProtocol'],\n onFinish: UseChatOptions['onFinish'],\n onResponse: ((response: Response) => void | Promise<void>) | undefined,\n onToolCall: UseChatOptions['onToolCall'] | undefined,\n sendExtraMessageFields: boolean | undefined,\n experimental_prepareRequestBody:\n | ((options: {\n messages: Message[];\n requestData?: JSONValue;\n requestBody?: object;\n }) => JSONValue)\n | undefined,\n fetch: FetchFunction | undefined,\n keepLastMessageOnError: boolean,\n) => {\n // Do an optimistic update to the chat state to show the updated messages immediately:\n const previousMessages = messagesRef.current;\n mutate(chatRequest.messages, false);\n\n const constructedMessagesPayload = sendExtraMessageFields\n ? chatRequest.messages\n : chatRequest.messages.map(\n ({\n role,\n content,\n experimental_attachments,\n name,\n data,\n annotations,\n toolInvocations,\n function_call,\n tool_calls,\n tool_call_id,\n }) => ({\n role,\n content,\n ...(experimental_attachments !== undefined && {\n experimental_attachments,\n }),\n ...(name !== undefined && { name }),\n ...(data !== undefined && { data }),\n ...(annotations !== undefined && { annotations }),\n ...(toolInvocations !== undefined && { toolInvocations }),\n // outdated function/tool call handling (TODO deprecate):\n tool_call_id,\n ...(function_call !== undefined && { function_call }),\n ...(tool_calls !== undefined && { tool_calls }),\n }),\n );\n\n return await callChatApi({\n api,\n body: experimental_prepareRequestBody?.({\n messages: chatRequest.messages,\n requestData: chatRequest.data,\n requestBody: chatRequest.body,\n }) ?? {\n messages: constructedMessagesPayload,\n data: chatRequest.data,\n ...extraMetadataRef.current.body,\n ...chatRequest.body,\n ...(chatRequest.functions !== undefined && {\n functions: chatRequest.functions,\n }),\n ...(chatRequest.function_call !== undefined && {\n function_call: chatRequest.function_call,\n }),\n ...(chatRequest.tools !== undefined && {\n tools: chatRequest.tools,\n }),\n ...(chatRequest.tool_choice !== undefined && {\n tool_choice: chatRequest.tool_choice,\n }),\n },\n streamProtocol,\n credentials: extraMetadataRef.current.credentials,\n headers: {\n ...extraMetadataRef.current.headers,\n ...chatRequest.headers,\n },\n abortController: () => abortControllerRef.current,\n restoreMessagesOnFailure() {\n if (!keepLastMessageOnError) {\n mutate(previousMessages, false);\n }\n },\n onResponse,\n onUpdate(merged, data) {\n mutate([...chatRequest.messages, ...merged], false);\n mutateStreamData([...(existingData || []), ...(data || [])], false);\n },\n onToolCall,\n onFinish,\n generateId,\n fetch,\n });\n};\n\nexport function useChat({\n api = '/api/chat',\n id,\n initialMessages,\n initialInput = '',\n sendExtraMessageFields,\n experimental_onFunctionCall,\n experimental_onToolCall,\n onToolCall,\n experimental_prepareRequestBody,\n experimental_maxAutomaticRoundtrips = 0,\n maxAutomaticRoundtrips = experimental_maxAutomaticRoundtrips,\n maxToolRoundtrips = maxAutomaticRoundtrips,\n streamMode,\n streamProtocol,\n onResponse,\n onFinish,\n onError,\n credentials,\n headers,\n body,\n generateId = generateIdFunc,\n fetch,\n keepLastMessageOnError = false,\n}: UseChatOptions & {\n key?: string;\n\n /**\n@deprecated Use `maxToolRoundtrips` instead.\n */\n experimental_maxAutomaticRoundtrips?: number;\n\n /**\n@deprecated Use `maxToolRoundtrips` instead.\n */\n maxAutomaticRoundtrips?: number;\n\n /**\n * Experimental (React only). When a function is provided, it will be used\n * to prepare the request body for the chat API. This can be useful for\n * customizing the request body based on the messages and data in the chat.\n *\n * @param messages The current messages in the chat.\n * @param requestData The data object passed in the chat request.\n * @param requestBody The request body object passed in the chat request.\n */\n experimental_prepareRequestBody?: (options: {\n messages: Message[];\n requestData?: JSONValue;\n requestBody?: object;\n }) => JSONValue;\n\n /**\nMaximal number of automatic roundtrips for tool calls.\n\nAn automatic tool call roundtrip is a call to the server with the\ntool call results when all tool calls in the last assistant\nmessage have results.\n\nA maximum number is required to prevent infinite loops in the\ncase of misconfigured tools.\n\nBy default, it's set to 0, which will disable the feature.\n */\n maxToolRoundtrips?: number;\n} = {}): UseChatHelpers & {\n /**\n * @deprecated Use `addToolResult` instead.\n */\n experimental_addToolResult: ({\n toolCallId,\n result,\n }: {\n toolCallId: string;\n result: any;\n }) => void;\n addToolResult: ({\n toolCallId,\n result,\n }: {\n toolCallId: string;\n result: any;\n }) => void;\n} {\n // streamMode is deprecated, use streamProtocol instead.\n if (streamMode) {\n streamProtocol ??= streamMode === 'text' ? 'text' : undefined;\n }\n\n // Generate a unique id for the chat if not provided.\n const hookId = useId();\n const idKey = id ?? hookId;\n const chatKey = typeof api === 'string' ? [api, idKey] : idKey;\n\n // Store a empty array as the initial messages\n // (instead of using a default parameter value that gets re-created each time)\n // to avoid re-renders:\n const [initialMessagesFallback] = useState([]);\n\n // Store the chat state in SWR, using the chatId as the key to share states.\n const { data: messages, mutate } = useSWR<Message[]>(\n [chatKey, 'messages'],\n null,\n { fallbackData: initialMessages ?? initialMessagesFallback },\n );\n\n // We store loading state in another hook to sync loading states across hook invocations\n const { data: isLoading = false, mutate: mutateLoading } = useSWR<boolean>(\n [chatKey, 'loading'],\n null,\n );\n\n const { data: streamData, mutate: mutateStreamData } = useSWR<\n JSONValue[] | undefined\n >([chatKey, 'streamData'], null);\n\n const { data: error = undefined, mutate: setError } = useSWR<\n undefined | Error\n >([chatKey, 'error'], null);\n\n // Keep the latest messages in a ref.\n const messagesRef = useRef<Message[]>(messages || []);\n useEffect(() => {\n messagesRef.current = messages || [];\n }, [messages]);\n\n // Abort controller to cancel the current API call.\n const abortControllerRef = useRef<AbortController | null>(null);\n\n const extraMetadataRef = useRef({\n credentials,\n headers,\n body,\n });\n\n useEffect(() => {\n extraMetadataRef.current = {\n credentials,\n headers,\n body,\n };\n }, [credentials, headers, body]);\n\n const triggerRequest = useCallback(\n async (chatRequest: ChatRequest) => {\n const messageCount = messagesRef.current.length;\n\n try {\n mutateLoading(true);\n setError(undefined);\n\n const abortController = new AbortController();\n abortControllerRef.current = abortController;\n\n await processChatStream({\n getStreamedResponse: () =>\n getStreamedResponse(\n api,\n chatRequest,\n mutate,\n mutateStreamData,\n streamData!,\n extraMetadataRef,\n messagesRef,\n abortControllerRef,\n generateId,\n streamProtocol,\n onFinish,\n onResponse,\n onToolCall,\n sendExtraMessageFields,\n experimental_prepareRequestBody,\n fetch,\n keepLastMessageOnError,\n ),\n experimental_onFunctionCall,\n experimental_onToolCall,\n updateChatRequest: chatRequestParam => {\n chatRequest = chatRequestParam;\n },\n getCurrentMessages: () => messagesRef.current,\n });\n\n abortControllerRef.current = null;\n } catch (err) {\n // Ignore abort errors as they are expected.\n if ((err as any).name === 'AbortError') {\n abortControllerRef.current = null;\n return null;\n }\n\n if (onError && err instanceof Error) {\n onError(err);\n }\n\n setError(err as Error);\n } finally {\n mutateLoading(false);\n }\n\n // auto-submit when all tool calls in the last assistant message have results:\n const messages = messagesRef.current;\n const lastMessage = messages[messages.length - 1];\n if (\n // ensure we actually have new messages (to prevent infinite loops in case of errors):\n messages.length > messageCount &&\n // ensure there is a last message:\n lastMessage != null &&\n // check if the feature is enabled:\n maxToolRoundtrips > 0 &&\n // check that roundtrip is possible:\n isAssistantMessageWithCompletedToolCalls(lastMessage) &&\n // limit the number of automatic roundtrips:\n countTrailingAssistantMessages(messages) <= maxToolRoundtrips\n ) {\n await triggerRequest({ messages });\n }\n },\n [\n mutate,\n mutateLoading,\n api,\n extraMetadataRef,\n onResponse,\n onFinish,\n onError,\n setError,\n mutateStreamData,\n streamData,\n streamProtocol,\n sendExtraMessageFields,\n experimental_onFunctionCall,\n experimental_onToolCall,\n experimental_prepareRequestBody,\n onToolCall,\n maxToolRoundtrips,\n messagesRef,\n abortControllerRef,\n generateId,\n fetch,\n keepLastMessageOnError,\n ],\n );\n\n const append = useCallback(\n async (\n message: Message | CreateMessage,\n {\n options,\n functions,\n function_call,\n tools,\n tool_choice,\n data,\n headers,\n body,\n }: ChatRequestOptions = {},\n ) => {\n if (!message.id) {\n message.id = generateId();\n }\n\n const requestOptions = {\n headers: headers ?? options?.headers,\n body: body ?? options?.body,\n };\n\n const chatRequest: ChatRequest = {\n messages: messagesRef.current.concat(message as Message),\n options: requestOptions,\n headers: requestOptions.headers,\n body: requestOptions.body,\n data,\n ...(functions !== undefined && { functions }),\n ...(function_call !== undefined && { function_call }),\n ...(tools !== undefined && { tools }),\n ...(tool_choice !== undefined && { tool_choice }),\n };\n\n return triggerRequest(chatRequest);\n },\n [triggerRequest, generateId],\n );\n\n const reload = useCallback(\n async ({\n options,\n functions,\n function_call,\n tools,\n tool_choice,\n data,\n headers,\n body,\n }: ChatRequestOptions = {}) => {\n if (messagesRef.current.length === 0) return null;\n\n const requestOptions = {\n headers: headers ?? options?.headers,\n body: body ?? options?.body,\n };\n\n // Remove last assistant message and retry last user message.\n const lastMessage = messagesRef.current[messagesRef.current.length - 1];\n if (lastMessage.role === 'assistant') {\n const chatRequest: ChatRequest = {\n messages: messagesRef.current.slice(0, -1),\n options: requestOptions,\n headers: requestOptions.headers,\n body: requestOptions.body,\n data,\n ...(functions !== undefined && { functions }),\n ...(function_call !== undefined && { function_call }),\n ...(tools !== undefined && { tools }),\n ...(tool_choice !== undefined && { tool_choice }),\n };\n\n return triggerRequest(chatRequest);\n }\n\n const chatRequest: ChatRequest = {\n messages: messagesRef.current,\n options: requestOptions,\n headers: requestOptions.headers,\n body: requestOptions.body,\n data,\n ...(functions !== undefined && { functions }),\n ...(function_call !== undefined && { function_call }),\n ...(tools !== undefined && { tools }),\n ...(tool_choice !== undefined && { tool_choice }),\n };\n\n return triggerRequest(chatRequest);\n },\n [triggerRequest],\n );\n\n const stop = useCallback(() => {\n if (abortControllerRef.current) {\n abortControllerRef.current.abort();\n abortControllerRef.current = null;\n }\n }, []);\n\n const setMessages = useCallback(\n (messages: Message[] | ((messages: Message[]) => Message[])) => {\n if (typeof messages === 'function') {\n messages = messages(messagesRef.current);\n }\n\n mutate(messages, false);\n messagesRef.current = messages;\n },\n [mutate],\n );\n\n // Input state and handlers.\n const [input, setInput] = useState(initialInput);\n\n const handleSubmit = useCallback(\n async (\n event?: { preventDefault?: () => void },\n options: ChatRequestOptions = {},\n metadata?: Object,\n ) => {\n event?.preventDefault?.();\n\n if (!input && !options.allowEmptySubmit) return;\n\n if (metadata) {\n extraMetadataRef.current = {\n ...extraMetadataRef.current,\n ...metadata,\n };\n }\n\n const attachmentsForRequest: Attachment[] = [];\n const attachmentsFromOptions = options.experimental_attachments;\n\n if (attachmentsFromOptions) {\n if (attachmentsFromOptions instanceof FileList) {\n for (const attachment of Array.from(attachmentsFromOptions)) {\n const { name, type } = attachment;\n\n const dataUrl = await new Promise<string>((resolve, reject) => {\n const reader = new FileReader();\n reader.onload = readerEvent => {\n resolve(readerEvent.target?.result as string);\n };\n reader.onerror = error => reject(error);\n reader.readAsDataURL(attachment);\n });\n\n attachmentsForRequest.push({\n name,\n contentType: type,\n url: dataUrl,\n });\n }\n } else if (Array.isArray(attachmentsFromOptions)) {\n for (const file of attachmentsFromOptions) {\n const { name, url, contentType } = file;\n\n attachmentsForRequest.push({\n name,\n contentType,\n url,\n });\n }\n } else {\n throw new Error('Invalid attachments type');\n }\n }\n\n const requestOptions = {\n headers: options.headers ?? options.options?.headers,\n body: options.body ?? options.options?.body,\n };\n\n const messages =\n !input && options.allowEmptySubmit\n ? messagesRef.current\n : messagesRef.current.concat({\n id: generateId(),\n createdAt: new Date(),\n role: 'user',\n content: input,\n experimental_attachments:\n attachmentsForRequest.length > 0\n ? attachmentsForRequest\n : undefined,\n });\n\n const chatRequest: ChatRequest = {\n messages,\n options: requestOptions,\n headers: requestOptions.headers,\n body: requestOptions.body,\n data: options.data,\n };\n\n triggerRequest(chatRequest);\n\n setInput('');\n },\n [input, generateId, triggerRequest],\n );\n\n const handleInputChange = (e: any) => {\n setInput(e.target.value);\n };\n\n const addToolResult = ({\n toolCallId,\n result,\n }: {\n toolCallId: string;\n result: any;\n }) => {\n const updatedMessages = messagesRef.current.map((message, index, arr) =>\n // update the tool calls in the last assistant message:\n index === arr.length - 1 &&\n message.role === 'assistant' &&\n message.toolInvocations\n ? {\n ...message,\n toolInvocations: message.toolInvocations.map(toolInvocation =>\n toolInvocation.toolCallId === toolCallId\n ? { ...toolInvocation, result }\n : toolInvocation,\n ),\n }\n : message,\n );\n\n mutate(updatedMessages, false);\n\n // auto-submit when all tool calls in the last assistant message have results:\n const lastMessage = updatedMessages[updatedMessages.length - 1];\n if (isAssistantMessageWithCompletedToolCalls(lastMessage)) {\n triggerRequest({ messages: updatedMessages });\n }\n };\n\n return {\n messages: messages || [],\n error,\n append,\n reload,\n stop,\n setMessages,\n input,\n setInput,\n handleInputChange,\n handleSubmit,\n isLoading,\n data: streamData,\n addToolResult,\n experimental_addToolResult: addToolResult,\n };\n}\n\n/**\nCheck if the message is an assistant message with completed tool calls.\nThe message must have at least one tool invocation and all tool invocations\nmust have a result.\n */\nfunction isAssistantMessageWithCompletedToolCalls(message: Message) {\n return (\n message.role === 'assistant' &&\n message.toolInvocations &&\n message.toolInvocations.length > 0 &&\n message.toolInvocations.every(toolInvocation => 'result' in toolInvocation)\n );\n}\n\n/**\nReturns the number of trailing assistant messages in the array.\n */\nfunction countTrailingAssistantMessages(messages: Message[]) {\n let count = 0;\n for (let i = messages.length - 1; i >= 0; i--) {\n if (messages[i].role === 'assistant') {\n count++;\n } else {\n break;\n }\n }\n return count;\n}\n","import {\n JSONValue,\n RequestOptions,\n UseCompletionOptions,\n callCompletionApi,\n} from '@ai-sdk/ui-utils';\nimport { useCallback, useEffect, useId, useRef, useState } from 'react';\nimport useSWR from 'swr';\n\nexport type { UseCompletionOptions };\n\nexport type UseCompletionHelpers = {\n /** The current completion result */\n completion: string;\n /**\n * Send a new prompt to the API endpoint and update the completion state.\n */\n complete: (\n prompt: string,\n options?: RequestOptions,\n ) => Promise<string | null | undefined>;\n /** The error object of the API request */\n error: undefined | Error;\n /**\n * Abort the current API request but keep the generated tokens.\n */\n stop: () => void;\n /**\n * Update the `completion` state locally.\n */\n setCompletion: (completion: string) => void;\n /** The current value of the input */\n input: string;\n /** setState-powered method to update the input value */\n setInput: React.Dispatch<React.SetStateAction<string>>;\n /**\n * An input/textarea-ready onChange handler to control the value of the input\n * @example\n * ```jsx\n * <input onChange={handleInputChange} value={input} />\n * ```\n */\n handleInputChange: (\n event:\n | React.ChangeEvent<HTMLInputElement>\n | React.ChangeEvent<HTMLTextAreaElement>,\n ) => void;\n\n /**\n * Form submission handler to automatically reset input and append a user message\n * @example\n * ```jsx\n * <form onSubmit={handleSubmit}>\n * <input onChange={handleInputChange} value={input} />\n * </form>\n * ```\n */\n handleSubmit: (event?: { preventDefault?: () => void }) => void;\n\n /** Whether the API request is in progress */\n isLoading: boolean;\n /** Additional data added on the server via StreamData */\n data?: JSONValue[];\n};\n\nexport function useCompletion({\n api = '/api/completion',\n id,\n initialCompletion = '',\n initialInput = '',\n credentials,\n headers,\n body,\n streamMode,\n streamProtocol,\n fetch,\n onResponse,\n onFinish,\n onError,\n}: UseCompletionOptions = {}): UseCompletionHelpers {\n // streamMode is deprecated, use streamProtocol instead.\n if (streamMode) {\n streamProtocol ??= streamMode === 'text' ? 'text' : undefined;\n }\n\n // Generate an unique id for the completion if not provided.\n const hookId = useId();\n const completionId = id || hookId;\n\n // Store the completion state in SWR, using the completionId as the key to share states.\n const { data, mutate } = useSWR<string>([api, completionId], null, {\n fallbackData: initialCompletion,\n });\n\n const { data: isLoading = false, mutate: mutateLoading } = useSWR<boolean>(\n [completionId, 'loading'],\n null,\n );\n\n const { data: streamData, mutate: mutateStreamData } = useSWR<\n JSONValue[] | undefined\n >([completionId, 'streamData'], null);\n\n const [error, setError] = useState<undefined | Error>(undefined);\n const completion = data!;\n\n // Abort controller to cancel the current API call.\n const [abortController, setAbortController] =\n useState<AbortController | null>(null);\n\n const extraMetadataRef = useRef({\n credentials,\n headers,\n body,\n });\n useEffect(() => {\n extraMetadataRef.current = {\n credentials,\n headers,\n body,\n };\n }, [credentials, headers, body]);\n\n const triggerRequest = useCallback(\n async (prompt: string, options?: RequestOptions) =>\n callCompletionApi({\n api,\n prompt,\n credentials: extraMetadataRef.current.credentials,\n headers: { ...extraMetadataRef.current.headers, ...options?.headers },\n body: {\n ...extraMetadataRef.current.body,\n ...options?.body,\n },\n streamProtocol,\n fetch,\n setCompletion: completion => mutate(completion, false),\n setLoading: mutateLoading,\n setError,\n setAbortController,\n onResponse,\n onFinish,\n onError,\n onData: data => {\n mutateStreamData([...(streamData || []), ...(data || [])], false);\n },\n }),\n [\n mutate,\n mutateLoading,\n api,\n extraMetadataRef,\n setAbortController,\n onResponse,\n onFinish,\n onError,\n setError,\n streamData,\n streamProtocol,\n fetch,\n mutateStreamData,\n ],\n );\n\n const stop = useCallback(() => {\n if (abortController) {\n abortController.abort();\n setAbortController(null);\n }\n }, [abortController]);\n\n const setCompletion = useCallback(\n (completion: string) => {\n mutate(completion, false);\n },\n [mutate],\n );\n\n const complete = useCallback<UseCompletionHelpers['complete']>(\n async (prompt, options) => {\n return triggerRequest(prompt, options);\n },\n [triggerRequest],\n );\n\n const [input, setInput] = useState(initialInput);\n\n const handleSubmit = useCallback(\n (event?: { preventDefault?: () => void }) => {\n event?.preventDefault?.();\n return input ? complete(input) : undefined;\n },\n [input, complete],\n );\n\n const handleInputChange = (e: any) => {\n setInput(e.target.value);\n };\n\n return {\n completion,\n complete,\n error,\n setCompletion,\n stop,\n input,\n setInput,\n handleInputChange,\n handleSubmit,\n isLoading,\n data: streamData,\n };\n}\n","import { isAbortError } from '@ai-sdk/provider-utils';\nimport {\n DeepPartial,\n FetchFunction,\n isDeepEqualData,\n parsePartialJson,\n} from '@ai-sdk/ui-utils';\nimport { useCallback, useId, useRef, useState } from 'react';\nimport useSWR from 'swr';\nimport z from 'zod';\n\n// use function to allow for mocking in tests:\nconst getOriginalFetch = () => fetch;\n\nexport type Experimental_UseObjectOptions<RESULT> = {\n /**\n * The API endpoint. It should stream JSON that matches the schema as chunked text.\n */\n api: string;\n\n /**\n * A Zod schema that defines the shape of the complete object.\n */\n schema: z.Schema<RESULT>;\n\n /**\n * An unique identifier. If not provided, a random one will be\n * generated. When provided, the `useObject` hook with the same `id` will\n * have shared states across components.\n */\n id?: string;\n\n /**\n * An optional value for the initial object.\n */\n initialValue?: DeepPartial<RESULT>;\n\n /**\nCustom fetch implementation. You can use it as a middleware to intercept requests,\nor to provide a custom fetch implementation for e.g. testing.\n */\n fetch?: FetchFunction;\n\n /**\n * Callback function to be called when an error is encountered.\n */\n onError?: (error: Error) => void;\n};\n\nexport type Experimental_UseObjectHelpers<RESULT, INPUT> = {\n /**\n * @deprecated Use `submit` instead.\n */\n setInput: (input: INPUT) => void;\n\n /**\n * Calls the API with the provided input as JSON body.\n */\n submit: (input: INPUT) => void;\n\n /**\n * The current value for the generated object. Updated as the API streams JSON chunks.\n */\n object: DeepPartial<RESULT> | undefined;\n\n /**\n * The error object of the API request if any.\n */\n error: undefined | unknown;\n\n /**\n * Flag that indicates whether an API request is in progress.\n */\n isLoading: boolean;\n\n /**\n * Abort the current request immediately, keep the current partial object if any.\n */\n stop: () => void;\n};\n\nfunction useObject<RESULT, INPUT = any>({\n api,\n id,\n schema, // required, in the future we will use it for validation\n initialValue,\n fetch,\n onError,\n}: Experimental_UseObjectOptions<RESULT>): Experimental_UseObjectHelpers<\n RESULT,\n INPUT\n> {\n // Generate an unique id if not provided.\n const hookId = useId();\n const completionId = id ?? hookId;\n\n // Store the completion state in SWR, using the completionId as the key to share states.\n const { data, mutate } = useSWR<DeepPartial<RESULT>>(\n [api, completionId],\n null,\n { fallbackData: initialValue },\n );\n\n const [error, setError] = useState<undefined | unknown>(undefined);\n const [isLoading, setIsLoading] = useState(false);\n\n // Abort controller to cancel the current API call.\n const abortControllerRef = useRef<AbortController | null>(null);\n\n const stop = useCallback(() => {\n try {\n abortControllerRef.current?.abort();\n } catch (ignored) {\n } finally {\n setIsLoading(false);\n abortControllerRef.current = null;\n }\n }, []);\n\n const submit = async (input: INPUT) => {\n try {\n setIsLoading(true);\n setError(undefined);\n\n const abortController = new AbortController();\n abortControllerRef.current = abortController;\n\n const actualFetch = fetch ?? getOriginalFetch();\n const response = await actualFetch(api, {\n method: 'POST',\n headers: { 'Content-Type': 'application/json' },\n signal: abortController.signal,\n body: JSON.stringify(input),\n });\n\n if (!response.ok) {\n throw new Error(\n (await response.text()) ?? 'Failed to fetch the response.',\n );\n }\n\n if (response.body == null) {\n throw new Error('The response body is empty.');\n }\n\n let accumulatedText = '';\n let latestObject: DeepPartial<RESULT> | undefined = undefined;\n\n await response.body.pipeThrough(new TextDecoderStream()).pipeTo(\n new WritableStream<string>({\n write(chunk) {\n accumulatedText += chunk;\n\n const currentObject = parsePartialJson(\n accumulatedText,\n ) as DeepPartial<RESULT>;\n\n if (!isDeepEqualData(latestObject, currentObject)) {\n latestObject = currentObject;\n\n mutate(currentObject);\n }\n },\n\n close() {\n setIsLoading(false);\n abortControllerRef.current = null;\n },\n }),\n );\n } catch (error) {\n if (isAbortError(error)) {\n return;\n }\n\n if (onError && error instanceof Error) {\n onError(error);\n }\n\n setError(error);\n }\n };\n\n return {\n setInput: submit, // Deprecated\n submit,\n object: data,\n error,\n isLoading,\n stop,\n };\n}\n\nexport const experimental_useObject = useObject;\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,4BAA6B;AAC7B,sBAOO;AACP,mBAA8C;AAG9C,IAAM,mBAAmB,MAAM;AAiFxB,SAAS,aAAa;AAAA,EAC3B;AAAA,EACA,UAAU;AAAA,EACV;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA,OAAAA;AACF,GAA6C;AAC3C,QAAM,CAAC,UAAU,WAAW,QAAI,uBAAoB,CAAC,CAAC;AACtD,QAAM,CAAC,OAAO,QAAQ,QAAI,uBAAS,EAAE;AACrC,QAAM,CAAC,iBAAiB,kBAAkB,QAAI;AAAA,IAC5C;AAAA,EACF;AACA,QAAM,CAAC,QAAQ,SAAS,QAAI,uBAA0B,kBAAkB;AACxE,QAAM,CAAC,OAAO,QAAQ,QAAI,uBAA4B,MAAS;AAE/D,QAAM,oBAAoB,CACxB,UAGG;AACH,aAAS,MAAM,OAAO,KAAK;AAAA,EAC7B;AAGA,QAAM,yBAAqB,qBAA+B,IAAI;AAE9D,QAAM,WAAO,0BAAY,MAAM;AAC7B,QAAI,mBAAmB,SAAS;AAC9B,yBAAmB,QAAQ,MAAM;AACjC,yBAAmB,UAAU;AAAA,IAC/B;AAAA,EACF,GAAG,CAAC,CAAC;AAEL,QAAM,SAAS,OACb,SACA,mBAGG;AArIP;AAsII,cAAU,aAAa;AAEvB,gBAAY,CAAAC,cAAS;AAxIzB,UAAAC;AAwI4B;AAAA,QACtB,GAAGD;AAAA,QACH;AAAA,UACE,GAAG;AAAA,UACH,KAAIC,MAAA,QAAQ,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AAQA,MAAK;AAAA,MACf;AAEA,eAASA,MAAK;AAAA,IAChB;AAAA,EACF;AAEA,SAAO;AAAA,IACL,UAAU;AAAA;AAAA,IACV;AAAA,IACA,QAAQ;AAAA,IACR;AAAA,IACA;AAAA,IACA;AAAA,EACF;AACF;AAEO,IAAM,yBAAyB;","names":["fetch","messages","_a","error","import_ui_utils","import_react","generateId","fetch","generateIdFunc","useSWR","messages","headers","body","chatRequest","_a","error","import_ui_utils","import_react","import_swr","fetch","useSWR","completion","data","import_provider_utils","import_ui_utils","import_react","import_swr","getOriginalFetch","fetch","useSWR","error"]}
|
package/dist/index.mjs
CHANGED
|
@@ -182,7 +182,7 @@ import {
|
|
|
182
182
|
} from "@ai-sdk/ui-utils";
|
|
183
183
|
import { useCallback as useCallback2, useEffect, useId, useRef as useRef2, useState as useState2 } from "react";
|
|
184
184
|
import useSWR from "swr";
|
|
185
|
-
var getStreamedResponse = async (api, chatRequest, mutate, mutateStreamData, existingData, extraMetadataRef, messagesRef, abortControllerRef, generateId2,
|
|
185
|
+
var getStreamedResponse = async (api, chatRequest, mutate, mutateStreamData, existingData, extraMetadataRef, messagesRef, abortControllerRef, generateId2, streamProtocol, onFinish, onResponse, onToolCall, sendExtraMessageFields, experimental_prepareRequestBody, fetch2, keepLastMessageOnError) => {
|
|
186
186
|
var _a;
|
|
187
187
|
const previousMessages = messagesRef.current;
|
|
188
188
|
mutate(chatRequest.messages, false);
|
|
@@ -238,7 +238,7 @@ var getStreamedResponse = async (api, chatRequest, mutate, mutateStreamData, exi
|
|
|
238
238
|
tool_choice: chatRequest.tool_choice
|
|
239
239
|
}
|
|
240
240
|
},
|
|
241
|
-
|
|
241
|
+
streamProtocol,
|
|
242
242
|
credentials: extraMetadataRef.current.credentials,
|
|
243
243
|
headers: {
|
|
244
244
|
...extraMetadataRef.current.headers,
|
|
@@ -275,6 +275,7 @@ function useChat({
|
|
|
275
275
|
maxAutomaticRoundtrips = experimental_maxAutomaticRoundtrips,
|
|
276
276
|
maxToolRoundtrips = maxAutomaticRoundtrips,
|
|
277
277
|
streamMode,
|
|
278
|
+
streamProtocol,
|
|
278
279
|
onResponse,
|
|
279
280
|
onFinish,
|
|
280
281
|
onError,
|
|
@@ -285,6 +286,9 @@ function useChat({
|
|
|
285
286
|
fetch: fetch2,
|
|
286
287
|
keepLastMessageOnError = false
|
|
287
288
|
} = {}) {
|
|
289
|
+
if (streamMode) {
|
|
290
|
+
streamProtocol != null ? streamProtocol : streamProtocol = streamMode === "text" ? "text" : void 0;
|
|
291
|
+
}
|
|
288
292
|
const hookId = useId();
|
|
289
293
|
const idKey = id != null ? id : hookId;
|
|
290
294
|
const chatKey = typeof api === "string" ? [api, idKey] : idKey;
|
|
@@ -336,7 +340,7 @@ function useChat({
|
|
|
336
340
|
messagesRef,
|
|
337
341
|
abortControllerRef,
|
|
338
342
|
generateId2,
|
|
339
|
-
|
|
343
|
+
streamProtocol,
|
|
340
344
|
onFinish,
|
|
341
345
|
onResponse,
|
|
342
346
|
onToolCall,
|
|
@@ -389,7 +393,7 @@ function useChat({
|
|
|
389
393
|
setError,
|
|
390
394
|
mutateStreamData,
|
|
391
395
|
streamData,
|
|
392
|
-
|
|
396
|
+
streamProtocol,
|
|
393
397
|
sendExtraMessageFields,
|
|
394
398
|
experimental_onFunctionCall,
|
|
395
399
|
experimental_onToolCall,
|
|
@@ -640,11 +644,15 @@ function useCompletion({
|
|
|
640
644
|
headers,
|
|
641
645
|
body,
|
|
642
646
|
streamMode,
|
|
647
|
+
streamProtocol,
|
|
643
648
|
fetch: fetch2,
|
|
644
649
|
onResponse,
|
|
645
650
|
onFinish,
|
|
646
651
|
onError
|
|
647
652
|
} = {}) {
|
|
653
|
+
if (streamMode) {
|
|
654
|
+
streamProtocol != null ? streamProtocol : streamProtocol = streamMode === "text" ? "text" : void 0;
|
|
655
|
+
}
|
|
648
656
|
const hookId = useId2();
|
|
649
657
|
const completionId = id || hookId;
|
|
650
658
|
const { data, mutate } = useSWR2([api, completionId], null, {
|
|
@@ -680,7 +688,7 @@ function useCompletion({
|
|
|
680
688
|
...extraMetadataRef.current.body,
|
|
681
689
|
...options == null ? void 0 : options.body
|
|
682
690
|
},
|
|
683
|
-
|
|
691
|
+
streamProtocol,
|
|
684
692
|
fetch: fetch2,
|
|
685
693
|
setCompletion: (completion2) => mutate(completion2, false),
|
|
686
694
|
setLoading: mutateLoading,
|
|
@@ -704,7 +712,7 @@ function useCompletion({
|
|
|
704
712
|
onError,
|
|
705
713
|
setError,
|
|
706
714
|
streamData,
|
|
707
|
-
|
|
715
|
+
streamProtocol,
|
|
708
716
|
fetch2,
|
|
709
717
|
mutateStreamData
|
|
710
718
|
]
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/use-assistant.ts","../src/use-chat.ts","../src/use-completion.ts","../src/use-object.ts"],"sourcesContent":["import { isAbortError } from '@ai-sdk/provider-utils';\nimport {\n AssistantStatus,\n CreateMessage,\n Message,\n UseAssistantOptions,\n generateId,\n readDataStream,\n} from '@ai-sdk/ui-utils';\nimport { useCallback, useRef, useState } from 'react';\n\n// use function to allow for mocking in tests:\nconst getOriginalFetch = () => fetch;\n\nexport type UseAssistantHelpers = {\n /**\n * The current array of chat messages.\n */\n messages: Message[];\n\n /**\n * Update the message store with a new array of messages.\n */\n setMessages: React.Dispatch<React.SetStateAction<Message[]>>;\n\n /**\n * The current thread ID.\n */\n threadId: string | undefined;\n\n /**\n * Set the current thread ID. Specifying a thread ID will switch to that thread, if it exists. If set to 'undefined', a new thread will be created. For both cases, `threadId` will be updated with the new value and `messages` will be cleared.\n */\n setThreadId: (threadId: string | undefined) => void;\n\n /**\n * The current value of the input field.\n */\n input: string;\n\n /**\n * Append a user message to the chat list. This triggers the API call to fetch\n * the assistant's response.\n * @param message The message to append\n * @param requestOptions Additional options to pass to the API call\n */\n append: (\n message: Message | CreateMessage,\n requestOptions?: {\n data?: Record<string, string>;\n },\n ) => Promise<void>;\n\n /**\nAbort the current request immediately, keep the generated tokens if any.\n */\n stop: () => void;\n\n /**\n * setState-powered method to update the input value.\n */\n setInput: React.Dispatch<React.SetStateAction<string>>;\n\n /**\n * Handler for the `onChange` event of the input field to control the input's value.\n */\n handleInputChange: (\n event:\n | React.ChangeEvent<HTMLInputElement>\n | React.ChangeEvent<HTMLTextAreaElement>,\n ) => void;\n\n /**\n * Form submission handler that automatically resets the input field and appends a user message.\n */\n submitMessage: (\n event?: React.FormEvent<HTMLFormElement>,\n requestOptions?: {\n data?: Record<string, string>;\n },\n ) => Promise<void>;\n\n /**\n * The current status of the assistant. This can be used to show a loading indicator.\n */\n status: AssistantStatus;\n\n /**\n * The error thrown during the assistant message processing, if any.\n */\n error: undefined | Error;\n};\n\nexport function useAssistant({\n api,\n threadId: threadIdParam,\n credentials,\n headers,\n body,\n onError,\n fetch,\n}: UseAssistantOptions): UseAssistantHelpers {\n const [messages, setMessages] = useState<Message[]>([]);\n const [input, setInput] = useState('');\n const [currentThreadId, setCurrentThreadId] = useState<string | undefined>(\n undefined,\n );\n const [status, setStatus] = useState<AssistantStatus>('awaiting_message');\n const [error, setError] = useState<undefined | Error>(undefined);\n\n const handleInputChange = (\n event:\n | React.ChangeEvent<HTMLInputElement>\n | React.ChangeEvent<HTMLTextAreaElement>,\n ) => {\n setInput(event.target.value);\n };\n\n // Abort controller to cancel the current API call.\n const abortControllerRef = useRef<AbortController | null>(null);\n\n const stop = useCallback(() => {\n if (abortControllerRef.current) {\n abortControllerRef.current.abort();\n abortControllerRef.current = null;\n }\n }, []);\n\n const append = async (\n message: Message | CreateMessage,\n requestOptions?: {\n data?: Record<string, string>;\n },\n ) => {\n setStatus('in_progress');\n\n setMessages(messages => [\n ...messages,\n {\n ...message,\n id: message.id ?? generateId(),\n },\n ]);\n\n setInput('');\n\n const abortController = new AbortController();\n\n try {\n abortControllerRef.current = abortController;\n\n const actualFetch = fetch ?? getOriginalFetch();\n const response = await actualFetch(api, {\n method: 'POST',\n credentials,\n signal: abortController.signal,\n headers: { 'Content-Type': 'application/json', ...headers },\n body: JSON.stringify({\n ...body,\n // always use user-provided threadId when available:\n threadId: threadIdParam ?? currentThreadId ?? null,\n message: message.content,\n\n // optional request data:\n data: requestOptions?.data,\n }),\n });\n\n if (!response.ok) {\n throw new Error(\n (await response.text()) ?? 'Failed to fetch the assistant response.',\n );\n }\n\n if (response.body == null) {\n throw new Error('The response body is empty.');\n }\n\n for await (const { type, value } of readDataStream(\n response.body.getReader(),\n )) {\n switch (type) {\n case 'assistant_message': {\n setMessages(messages => [\n ...messages,\n {\n id: value.id,\n role: value.role,\n content: value.content[0].text.value,\n },\n ]);\n break;\n }\n\n case 'text': {\n // text delta - add to last message:\n setMessages(messages => {\n const lastMessage = messages[messages.length - 1];\n return [\n ...messages.slice(0, messages.length - 1),\n {\n id: lastMessage.id,\n role: lastMessage.role,\n content: lastMessage.content + value,\n },\n ];\n });\n\n break;\n }\n\n case 'data_message': {\n setMessages(messages => [\n ...messages,\n {\n id: value.id ?? generateId(),\n role: 'data',\n content: '',\n data: value.data,\n },\n ]);\n break;\n }\n\n case 'assistant_control_data': {\n setCurrentThreadId(value.threadId);\n\n // set id of last message:\n setMessages(messages => {\n const lastMessage = messages[messages.length - 1];\n lastMessage.id = value.messageId;\n return [...messages.slice(0, messages.length - 1), lastMessage];\n });\n\n break;\n }\n\n case 'error': {\n setError(new Error(value));\n break;\n }\n }\n }\n } catch (error) {\n // Ignore abort errors as they are expected when the user cancels the request:\n if (isAbortError(error) && abortController.signal.aborted) {\n abortControllerRef.current = null;\n return;\n }\n\n if (onError && error instanceof Error) {\n onError(error);\n }\n\n setError(error as Error);\n } finally {\n abortControllerRef.current = null;\n setStatus('awaiting_message');\n }\n };\n\n const submitMessage = async (\n event?: React.FormEvent<HTMLFormElement>,\n requestOptions?: {\n data?: Record<string, string>;\n },\n ) => {\n event?.preventDefault?.();\n\n if (input === '') {\n return;\n }\n\n append({ role: 'user', content: input }, requestOptions);\n };\n\n const setThreadId = (threadId: string | undefined) => {\n setCurrentThreadId(threadId);\n setMessages([]);\n };\n\n return {\n append,\n messages,\n setMessages,\n threadId: currentThreadId,\n setThreadId,\n input,\n setInput,\n handleInputChange,\n submitMessage,\n status,\n error,\n stop,\n };\n}\n\n/**\n@deprecated Use `useAssistant` instead.\n */\nexport const experimental_useAssistant = useAssistant;\n","import type {\n ChatRequest,\n ChatRequestOptions,\n Attachment,\n CreateMessage,\n FetchFunction,\n IdGenerator,\n JSONValue,\n Message,\n UseChatOptions,\n} from '@ai-sdk/ui-utils';\nimport {\n callChatApi,\n generateId as generateIdFunc,\n processChatStream,\n} from '@ai-sdk/ui-utils';\nimport { useCallback, useEffect, useId, useRef, useState } from 'react';\nimport useSWR, { KeyedMutator } from 'swr';\n\nexport type { CreateMessage, Message, UseChatOptions };\n\nexport type UseChatHelpers = {\n /** Current messages in the chat */\n messages: Message[];\n /** The error object of the API request */\n error: undefined | Error;\n /**\n * Append a user message to the chat list. This triggers the API call to fetch\n * the assistant's response.\n * @param message The message to append\n * @param options Additional options to pass to the API call\n */\n append: (\n message: Message | CreateMessage,\n chatRequestOptions?: ChatRequestOptions,\n ) => Promise<string | null | undefined>;\n /**\n * Reload the last AI chat response for the given chat history. If the last\n * message isn't from the assistant, it will request the API to generate a\n * new response.\n */\n reload: (\n chatRequestOptions?: ChatRequestOptions,\n ) => Promise<string | null | undefined>;\n /**\n * Abort the current request immediately, keep the generated tokens if any.\n */\n stop: () => void;\n /**\n * Update the `messages` state locally. This is useful when you want to\n * edit the messages on the client, and then trigger the `reload` method\n * manually to regenerate the AI response.\n */\n setMessages: (\n messages: Message[] | ((messages: Message[]) => Message[]),\n ) => void;\n /** The current value of the input */\n input: string;\n /** setState-powered method to update the input value */\n setInput: React.Dispatch<React.SetStateAction<string>>;\n /** An input/textarea-ready onChange handler to control the value of the input */\n handleInputChange: (\n e:\n | React.ChangeEvent<HTMLInputElement>\n | React.ChangeEvent<HTMLTextAreaElement>,\n ) => void;\n /** Form submission handler to automatically reset input and append a user message */\n handleSubmit: (\n event?: { preventDefault?: () => void },\n chatRequestOptions?: ChatRequestOptions,\n ) => void;\n metadata?: Object;\n /** Whether the API request is in progress */\n isLoading: boolean;\n /** Additional data added on the server via StreamData */\n data?: JSONValue[];\n};\n\nconst getStreamedResponse = async (\n api: string,\n chatRequest: ChatRequest,\n mutate: KeyedMutator<Message[]>,\n mutateStreamData: KeyedMutator<JSONValue[] | undefined>,\n existingData: JSONValue[] | undefined,\n extraMetadataRef: React.MutableRefObject<any>,\n messagesRef: React.MutableRefObject<Message[]>,\n abortControllerRef: React.MutableRefObject<AbortController | null>,\n generateId: IdGenerator,\n streamMode: 'stream-data' | 'text' | undefined,\n onFinish: ((message: Message) => void) | undefined,\n onResponse: ((response: Response) => void | Promise<void>) | undefined,\n onToolCall: UseChatOptions['onToolCall'] | undefined,\n sendExtraMessageFields: boolean | undefined,\n experimental_prepareRequestBody:\n | ((options: {\n messages: Message[];\n requestData?: JSONValue;\n requestBody?: object;\n }) => JSONValue)\n | undefined,\n fetch: FetchFunction | undefined,\n keepLastMessageOnError: boolean,\n) => {\n // Do an optimistic update to the chat state to show the updated messages immediately:\n const previousMessages = messagesRef.current;\n mutate(chatRequest.messages, false);\n\n const constructedMessagesPayload = sendExtraMessageFields\n ? chatRequest.messages\n : chatRequest.messages.map(\n ({\n role,\n content,\n experimental_attachments,\n name,\n data,\n annotations,\n toolInvocations,\n function_call,\n tool_calls,\n tool_call_id,\n }) => ({\n role,\n content,\n ...(experimental_attachments !== undefined && {\n experimental_attachments,\n }),\n ...(name !== undefined && { name }),\n ...(data !== undefined && { data }),\n ...(annotations !== undefined && { annotations }),\n ...(toolInvocations !== undefined && { toolInvocations }),\n // outdated function/tool call handling (TODO deprecate):\n tool_call_id,\n ...(function_call !== undefined && { function_call }),\n ...(tool_calls !== undefined && { tool_calls }),\n }),\n );\n\n return await callChatApi({\n api,\n body: experimental_prepareRequestBody?.({\n messages: chatRequest.messages,\n requestData: chatRequest.data,\n requestBody: chatRequest.body,\n }) ?? {\n messages: constructedMessagesPayload,\n data: chatRequest.data,\n ...extraMetadataRef.current.body,\n ...chatRequest.body,\n ...(chatRequest.functions !== undefined && {\n functions: chatRequest.functions,\n }),\n ...(chatRequest.function_call !== undefined && {\n function_call: chatRequest.function_call,\n }),\n ...(chatRequest.tools !== undefined && {\n tools: chatRequest.tools,\n }),\n ...(chatRequest.tool_choice !== undefined && {\n tool_choice: chatRequest.tool_choice,\n }),\n },\n streamMode,\n credentials: extraMetadataRef.current.credentials,\n headers: {\n ...extraMetadataRef.current.headers,\n ...chatRequest.headers,\n },\n abortController: () => abortControllerRef.current,\n restoreMessagesOnFailure() {\n if (!keepLastMessageOnError) {\n mutate(previousMessages, false);\n }\n },\n onResponse,\n onUpdate(merged, data) {\n mutate([...chatRequest.messages, ...merged], false);\n mutateStreamData([...(existingData || []), ...(data || [])], false);\n },\n onToolCall,\n onFinish,\n generateId,\n fetch,\n });\n};\n\nexport function useChat({\n api = '/api/chat',\n id,\n initialMessages,\n initialInput = '',\n sendExtraMessageFields,\n experimental_onFunctionCall,\n experimental_onToolCall,\n onToolCall,\n experimental_prepareRequestBody,\n experimental_maxAutomaticRoundtrips = 0,\n maxAutomaticRoundtrips = experimental_maxAutomaticRoundtrips,\n maxToolRoundtrips = maxAutomaticRoundtrips,\n streamMode,\n onResponse,\n onFinish,\n onError,\n credentials,\n headers,\n body,\n generateId = generateIdFunc,\n fetch,\n keepLastMessageOnError = false,\n}: UseChatOptions & {\n key?: string;\n\n /**\n@deprecated Use `maxToolRoundtrips` instead.\n */\n experimental_maxAutomaticRoundtrips?: number;\n\n /**\n@deprecated Use `maxToolRoundtrips` instead.\n */\n maxAutomaticRoundtrips?: number;\n\n /**\n * Experimental (React only). When a function is provided, it will be used\n * to prepare the request body for the chat API. This can be useful for\n * customizing the request body based on the messages and data in the chat.\n *\n * @param messages The current messages in the chat.\n * @param requestData The data object passed in the chat request.\n * @param requestBody The request body object passed in the chat request.\n */\n experimental_prepareRequestBody?: (options: {\n messages: Message[];\n requestData?: JSONValue;\n requestBody?: object;\n }) => JSONValue;\n\n /**\nMaximal number of automatic roundtrips for tool calls.\n\nAn automatic tool call roundtrip is a call to the server with the\ntool call results when all tool calls in the last assistant\nmessage have results.\n\nA maximum number is required to prevent infinite loops in the\ncase of misconfigured tools.\n\nBy default, it's set to 0, which will disable the feature.\n */\n maxToolRoundtrips?: number;\n} = {}): UseChatHelpers & {\n /**\n * @deprecated Use `addToolResult` instead.\n */\n experimental_addToolResult: ({\n toolCallId,\n result,\n }: {\n toolCallId: string;\n result: any;\n }) => void;\n addToolResult: ({\n toolCallId,\n result,\n }: {\n toolCallId: string;\n result: any;\n }) => void;\n} {\n // Generate a unique id for the chat if not provided.\n const hookId = useId();\n const idKey = id ?? hookId;\n const chatKey = typeof api === 'string' ? [api, idKey] : idKey;\n\n // Store a empty array as the initial messages\n // (instead of using a default parameter value that gets re-created each time)\n // to avoid re-renders:\n const [initialMessagesFallback] = useState([]);\n\n // Store the chat state in SWR, using the chatId as the key to share states.\n const { data: messages, mutate } = useSWR<Message[]>(\n [chatKey, 'messages'],\n null,\n { fallbackData: initialMessages ?? initialMessagesFallback },\n );\n\n // We store loading state in another hook to sync loading states across hook invocations\n const { data: isLoading = false, mutate: mutateLoading } = useSWR<boolean>(\n [chatKey, 'loading'],\n null,\n );\n\n const { data: streamData, mutate: mutateStreamData } = useSWR<\n JSONValue[] | undefined\n >([chatKey, 'streamData'], null);\n\n const { data: error = undefined, mutate: setError } = useSWR<\n undefined | Error\n >([chatKey, 'error'], null);\n\n // Keep the latest messages in a ref.\n const messagesRef = useRef<Message[]>(messages || []);\n useEffect(() => {\n messagesRef.current = messages || [];\n }, [messages]);\n\n // Abort controller to cancel the current API call.\n const abortControllerRef = useRef<AbortController | null>(null);\n\n const extraMetadataRef = useRef({\n credentials,\n headers,\n body,\n });\n\n useEffect(() => {\n extraMetadataRef.current = {\n credentials,\n headers,\n body,\n };\n }, [credentials, headers, body]);\n\n const triggerRequest = useCallback(\n async (chatRequest: ChatRequest) => {\n const messageCount = messagesRef.current.length;\n\n try {\n mutateLoading(true);\n setError(undefined);\n\n const abortController = new AbortController();\n abortControllerRef.current = abortController;\n\n await processChatStream({\n getStreamedResponse: () =>\n getStreamedResponse(\n api,\n chatRequest,\n mutate,\n mutateStreamData,\n streamData!,\n extraMetadataRef,\n messagesRef,\n abortControllerRef,\n generateId,\n streamMode,\n onFinish,\n onResponse,\n onToolCall,\n sendExtraMessageFields,\n experimental_prepareRequestBody,\n fetch,\n keepLastMessageOnError,\n ),\n experimental_onFunctionCall,\n experimental_onToolCall,\n updateChatRequest: chatRequestParam => {\n chatRequest = chatRequestParam;\n },\n getCurrentMessages: () => messagesRef.current,\n });\n\n abortControllerRef.current = null;\n } catch (err) {\n // Ignore abort errors as they are expected.\n if ((err as any).name === 'AbortError') {\n abortControllerRef.current = null;\n return null;\n }\n\n if (onError && err instanceof Error) {\n onError(err);\n }\n\n setError(err as Error);\n } finally {\n mutateLoading(false);\n }\n\n // auto-submit when all tool calls in the last assistant message have results:\n const messages = messagesRef.current;\n const lastMessage = messages[messages.length - 1];\n if (\n // ensure we actually have new messages (to prevent infinite loops in case of errors):\n messages.length > messageCount &&\n // ensure there is a last message:\n lastMessage != null &&\n // check if the feature is enabled:\n maxToolRoundtrips > 0 &&\n // check that roundtrip is possible:\n isAssistantMessageWithCompletedToolCalls(lastMessage) &&\n // limit the number of automatic roundtrips:\n countTrailingAssistantMessages(messages) <= maxToolRoundtrips\n ) {\n await triggerRequest({ messages });\n }\n },\n [\n mutate,\n mutateLoading,\n api,\n extraMetadataRef,\n onResponse,\n onFinish,\n onError,\n setError,\n mutateStreamData,\n streamData,\n streamMode,\n sendExtraMessageFields,\n experimental_onFunctionCall,\n experimental_onToolCall,\n experimental_prepareRequestBody,\n onToolCall,\n maxToolRoundtrips,\n messagesRef,\n abortControllerRef,\n generateId,\n fetch,\n keepLastMessageOnError,\n ],\n );\n\n const append = useCallback(\n async (\n message: Message | CreateMessage,\n {\n options,\n functions,\n function_call,\n tools,\n tool_choice,\n data,\n headers,\n body,\n }: ChatRequestOptions = {},\n ) => {\n if (!message.id) {\n message.id = generateId();\n }\n\n const requestOptions = {\n headers: headers ?? options?.headers,\n body: body ?? options?.body,\n };\n\n const chatRequest: ChatRequest = {\n messages: messagesRef.current.concat(message as Message),\n options: requestOptions,\n headers: requestOptions.headers,\n body: requestOptions.body,\n data,\n ...(functions !== undefined && { functions }),\n ...(function_call !== undefined && { function_call }),\n ...(tools !== undefined && { tools }),\n ...(tool_choice !== undefined && { tool_choice }),\n };\n\n return triggerRequest(chatRequest);\n },\n [triggerRequest, generateId],\n );\n\n const reload = useCallback(\n async ({\n options,\n functions,\n function_call,\n tools,\n tool_choice,\n data,\n headers,\n body,\n }: ChatRequestOptions = {}) => {\n if (messagesRef.current.length === 0) return null;\n\n const requestOptions = {\n headers: headers ?? options?.headers,\n body: body ?? options?.body,\n };\n\n // Remove last assistant message and retry last user message.\n const lastMessage = messagesRef.current[messagesRef.current.length - 1];\n if (lastMessage.role === 'assistant') {\n const chatRequest: ChatRequest = {\n messages: messagesRef.current.slice(0, -1),\n options: requestOptions,\n headers: requestOptions.headers,\n body: requestOptions.body,\n data,\n ...(functions !== undefined && { functions }),\n ...(function_call !== undefined && { function_call }),\n ...(tools !== undefined && { tools }),\n ...(tool_choice !== undefined && { tool_choice }),\n };\n\n return triggerRequest(chatRequest);\n }\n\n const chatRequest: ChatRequest = {\n messages: messagesRef.current,\n options: requestOptions,\n headers: requestOptions.headers,\n body: requestOptions.body,\n data,\n ...(functions !== undefined && { functions }),\n ...(function_call !== undefined && { function_call }),\n ...(tools !== undefined && { tools }),\n ...(tool_choice !== undefined && { tool_choice }),\n };\n\n return triggerRequest(chatRequest);\n },\n [triggerRequest],\n );\n\n const stop = useCallback(() => {\n if (abortControllerRef.current) {\n abortControllerRef.current.abort();\n abortControllerRef.current = null;\n }\n }, []);\n\n const setMessages = useCallback(\n (messages: Message[] | ((messages: Message[]) => Message[])) => {\n if (typeof messages === 'function') {\n messages = messages(messagesRef.current);\n }\n\n mutate(messages, false);\n messagesRef.current = messages;\n },\n [mutate],\n );\n\n // Input state and handlers.\n const [input, setInput] = useState(initialInput);\n\n const handleSubmit = useCallback(\n async (\n event?: { preventDefault?: () => void },\n options: ChatRequestOptions = {},\n metadata?: Object,\n ) => {\n event?.preventDefault?.();\n\n if (!input && !options.allowEmptySubmit) return;\n\n if (metadata) {\n extraMetadataRef.current = {\n ...extraMetadataRef.current,\n ...metadata,\n };\n }\n\n const attachmentsForRequest: Attachment[] = [];\n const attachmentsFromOptions = options.experimental_attachments;\n\n if (attachmentsFromOptions) {\n if (attachmentsFromOptions instanceof FileList) {\n for (const attachment of Array.from(attachmentsFromOptions)) {\n const { name, type } = attachment;\n\n const dataUrl = await new Promise<string>((resolve, reject) => {\n const reader = new FileReader();\n reader.onload = readerEvent => {\n resolve(readerEvent.target?.result as string);\n };\n reader.onerror = error => reject(error);\n reader.readAsDataURL(attachment);\n });\n\n attachmentsForRequest.push({\n name,\n contentType: type,\n url: dataUrl,\n });\n }\n } else if (Array.isArray(attachmentsFromOptions)) {\n for (const file of attachmentsFromOptions) {\n const { name, url, contentType } = file;\n\n attachmentsForRequest.push({\n name,\n contentType,\n url,\n });\n }\n } else {\n throw new Error('Invalid attachments type');\n }\n }\n\n const requestOptions = {\n headers: options.headers ?? options.options?.headers,\n body: options.body ?? options.options?.body,\n };\n\n const messages =\n !input && options.allowEmptySubmit\n ? messagesRef.current\n : messagesRef.current.concat({\n id: generateId(),\n createdAt: new Date(),\n role: 'user',\n content: input,\n experimental_attachments:\n attachmentsForRequest.length > 0\n ? attachmentsForRequest\n : undefined,\n });\n\n const chatRequest: ChatRequest = {\n messages,\n options: requestOptions,\n headers: requestOptions.headers,\n body: requestOptions.body,\n data: options.data,\n };\n\n triggerRequest(chatRequest);\n\n setInput('');\n },\n [input, generateId, triggerRequest],\n );\n\n const handleInputChange = (e: any) => {\n setInput(e.target.value);\n };\n\n const addToolResult = ({\n toolCallId,\n result,\n }: {\n toolCallId: string;\n result: any;\n }) => {\n const updatedMessages = messagesRef.current.map((message, index, arr) =>\n // update the tool calls in the last assistant message:\n index === arr.length - 1 &&\n message.role === 'assistant' &&\n message.toolInvocations\n ? {\n ...message,\n toolInvocations: message.toolInvocations.map(toolInvocation =>\n toolInvocation.toolCallId === toolCallId\n ? { ...toolInvocation, result }\n : toolInvocation,\n ),\n }\n : message,\n );\n\n mutate(updatedMessages, false);\n\n // auto-submit when all tool calls in the last assistant message have results:\n const lastMessage = updatedMessages[updatedMessages.length - 1];\n if (isAssistantMessageWithCompletedToolCalls(lastMessage)) {\n triggerRequest({ messages: updatedMessages });\n }\n };\n\n return {\n messages: messages || [],\n error,\n append,\n reload,\n stop,\n setMessages,\n input,\n setInput,\n handleInputChange,\n handleSubmit,\n isLoading,\n data: streamData,\n addToolResult,\n experimental_addToolResult: addToolResult,\n };\n}\n\n/**\nCheck if the message is an assistant message with completed tool calls.\nThe message must have at least one tool invocation and all tool invocations\nmust have a result.\n */\nfunction isAssistantMessageWithCompletedToolCalls(message: Message) {\n return (\n message.role === 'assistant' &&\n message.toolInvocations &&\n message.toolInvocations.length > 0 &&\n message.toolInvocations.every(toolInvocation => 'result' in toolInvocation)\n );\n}\n\n/**\nReturns the number of trailing assistant messages in the array.\n */\nfunction countTrailingAssistantMessages(messages: Message[]) {\n let count = 0;\n for (let i = messages.length - 1; i >= 0; i--) {\n if (messages[i].role === 'assistant') {\n count++;\n } else {\n break;\n }\n }\n return count;\n}\n","import {\n JSONValue,\n RequestOptions,\n UseCompletionOptions,\n callCompletionApi,\n} from '@ai-sdk/ui-utils';\nimport { useCallback, useEffect, useId, useRef, useState } from 'react';\nimport useSWR from 'swr';\n\nexport type { UseCompletionOptions };\n\nexport type UseCompletionHelpers = {\n /** The current completion result */\n completion: string;\n /**\n * Send a new prompt to the API endpoint and update the completion state.\n */\n complete: (\n prompt: string,\n options?: RequestOptions,\n ) => Promise<string | null | undefined>;\n /** The error object of the API request */\n error: undefined | Error;\n /**\n * Abort the current API request but keep the generated tokens.\n */\n stop: () => void;\n /**\n * Update the `completion` state locally.\n */\n setCompletion: (completion: string) => void;\n /** The current value of the input */\n input: string;\n /** setState-powered method to update the input value */\n setInput: React.Dispatch<React.SetStateAction<string>>;\n /**\n * An input/textarea-ready onChange handler to control the value of the input\n * @example\n * ```jsx\n * <input onChange={handleInputChange} value={input} />\n * ```\n */\n handleInputChange: (\n event:\n | React.ChangeEvent<HTMLInputElement>\n | React.ChangeEvent<HTMLTextAreaElement>,\n ) => void;\n\n /**\n * Form submission handler to automatically reset input and append a user message\n * @example\n * ```jsx\n * <form onSubmit={handleSubmit}>\n * <input onChange={handleInputChange} value={input} />\n * </form>\n * ```\n */\n handleSubmit: (event?: { preventDefault?: () => void }) => void;\n\n /** Whether the API request is in progress */\n isLoading: boolean;\n /** Additional data added on the server via StreamData */\n data?: JSONValue[];\n};\n\nexport function useCompletion({\n api = '/api/completion',\n id,\n initialCompletion = '',\n initialInput = '',\n credentials,\n headers,\n body,\n streamMode,\n fetch,\n onResponse,\n onFinish,\n onError,\n}: UseCompletionOptions = {}): UseCompletionHelpers {\n // Generate an unique id for the completion if not provided.\n const hookId = useId();\n const completionId = id || hookId;\n\n // Store the completion state in SWR, using the completionId as the key to share states.\n const { data, mutate } = useSWR<string>([api, completionId], null, {\n fallbackData: initialCompletion,\n });\n\n const { data: isLoading = false, mutate: mutateLoading } = useSWR<boolean>(\n [completionId, 'loading'],\n null,\n );\n\n const { data: streamData, mutate: mutateStreamData } = useSWR<\n JSONValue[] | undefined\n >([completionId, 'streamData'], null);\n\n const [error, setError] = useState<undefined | Error>(undefined);\n const completion = data!;\n\n // Abort controller to cancel the current API call.\n const [abortController, setAbortController] =\n useState<AbortController | null>(null);\n\n const extraMetadataRef = useRef({\n credentials,\n headers,\n body,\n });\n useEffect(() => {\n extraMetadataRef.current = {\n credentials,\n headers,\n body,\n };\n }, [credentials, headers, body]);\n\n const triggerRequest = useCallback(\n async (prompt: string, options?: RequestOptions) =>\n callCompletionApi({\n api,\n prompt,\n credentials: extraMetadataRef.current.credentials,\n headers: { ...extraMetadataRef.current.headers, ...options?.headers },\n body: {\n ...extraMetadataRef.current.body,\n ...options?.body,\n },\n streamMode,\n fetch,\n setCompletion: completion => mutate(completion, false),\n setLoading: mutateLoading,\n setError,\n setAbortController,\n onResponse,\n onFinish,\n onError,\n onData: data => {\n mutateStreamData([...(streamData || []), ...(data || [])], false);\n },\n }),\n [\n mutate,\n mutateLoading,\n api,\n extraMetadataRef,\n setAbortController,\n onResponse,\n onFinish,\n onError,\n setError,\n streamData,\n streamMode,\n fetch,\n mutateStreamData,\n ],\n );\n\n const stop = useCallback(() => {\n if (abortController) {\n abortController.abort();\n setAbortController(null);\n }\n }, [abortController]);\n\n const setCompletion = useCallback(\n (completion: string) => {\n mutate(completion, false);\n },\n [mutate],\n );\n\n const complete = useCallback<UseCompletionHelpers['complete']>(\n async (prompt, options) => {\n return triggerRequest(prompt, options);\n },\n [triggerRequest],\n );\n\n const [input, setInput] = useState(initialInput);\n\n const handleSubmit = useCallback(\n (event?: { preventDefault?: () => void }) => {\n event?.preventDefault?.();\n return input ? complete(input) : undefined;\n },\n [input, complete],\n );\n\n const handleInputChange = (e: any) => {\n setInput(e.target.value);\n };\n\n return {\n completion,\n complete,\n error,\n setCompletion,\n stop,\n input,\n setInput,\n handleInputChange,\n handleSubmit,\n isLoading,\n data: streamData,\n };\n}\n","import { isAbortError } from '@ai-sdk/provider-utils';\nimport {\n DeepPartial,\n FetchFunction,\n isDeepEqualData,\n parsePartialJson,\n} from '@ai-sdk/ui-utils';\nimport { useCallback, useId, useRef, useState } from 'react';\nimport useSWR from 'swr';\nimport z from 'zod';\n\n// use function to allow for mocking in tests:\nconst getOriginalFetch = () => fetch;\n\nexport type Experimental_UseObjectOptions<RESULT> = {\n /**\n * The API endpoint. It should stream JSON that matches the schema as chunked text.\n */\n api: string;\n\n /**\n * A Zod schema that defines the shape of the complete object.\n */\n schema: z.Schema<RESULT>;\n\n /**\n * An unique identifier. If not provided, a random one will be\n * generated. When provided, the `useObject` hook with the same `id` will\n * have shared states across components.\n */\n id?: string;\n\n /**\n * An optional value for the initial object.\n */\n initialValue?: DeepPartial<RESULT>;\n\n /**\nCustom fetch implementation. You can use it as a middleware to intercept requests,\nor to provide a custom fetch implementation for e.g. testing.\n */\n fetch?: FetchFunction;\n\n /**\n * Callback function to be called when an error is encountered.\n */\n onError?: (error: Error) => void;\n};\n\nexport type Experimental_UseObjectHelpers<RESULT, INPUT> = {\n /**\n * @deprecated Use `submit` instead.\n */\n setInput: (input: INPUT) => void;\n\n /**\n * Calls the API with the provided input as JSON body.\n */\n submit: (input: INPUT) => void;\n\n /**\n * The current value for the generated object. Updated as the API streams JSON chunks.\n */\n object: DeepPartial<RESULT> | undefined;\n\n /**\n * The error object of the API request if any.\n */\n error: undefined | unknown;\n\n /**\n * Flag that indicates whether an API request is in progress.\n */\n isLoading: boolean;\n\n /**\n * Abort the current request immediately, keep the current partial object if any.\n */\n stop: () => void;\n};\n\nfunction useObject<RESULT, INPUT = any>({\n api,\n id,\n schema, // required, in the future we will use it for validation\n initialValue,\n fetch,\n onError,\n}: Experimental_UseObjectOptions<RESULT>): Experimental_UseObjectHelpers<\n RESULT,\n INPUT\n> {\n // Generate an unique id if not provided.\n const hookId = useId();\n const completionId = id ?? hookId;\n\n // Store the completion state in SWR, using the completionId as the key to share states.\n const { data, mutate } = useSWR<DeepPartial<RESULT>>(\n [api, completionId],\n null,\n { fallbackData: initialValue },\n );\n\n const [error, setError] = useState<undefined | unknown>(undefined);\n const [isLoading, setIsLoading] = useState(false);\n\n // Abort controller to cancel the current API call.\n const abortControllerRef = useRef<AbortController | null>(null);\n\n const stop = useCallback(() => {\n try {\n abortControllerRef.current?.abort();\n } catch (ignored) {\n } finally {\n setIsLoading(false);\n abortControllerRef.current = null;\n }\n }, []);\n\n const submit = async (input: INPUT) => {\n try {\n setIsLoading(true);\n setError(undefined);\n\n const abortController = new AbortController();\n abortControllerRef.current = abortController;\n\n const actualFetch = fetch ?? getOriginalFetch();\n const response = await actualFetch(api, {\n method: 'POST',\n headers: { 'Content-Type': 'application/json' },\n signal: abortController.signal,\n body: JSON.stringify(input),\n });\n\n if (!response.ok) {\n throw new Error(\n (await response.text()) ?? 'Failed to fetch the response.',\n );\n }\n\n if (response.body == null) {\n throw new Error('The response body is empty.');\n }\n\n let accumulatedText = '';\n let latestObject: DeepPartial<RESULT> | undefined = undefined;\n\n await response.body.pipeThrough(new TextDecoderStream()).pipeTo(\n new WritableStream<string>({\n write(chunk) {\n accumulatedText += chunk;\n\n const currentObject = parsePartialJson(\n accumulatedText,\n ) as DeepPartial<RESULT>;\n\n if (!isDeepEqualData(latestObject, currentObject)) {\n latestObject = currentObject;\n\n mutate(currentObject);\n }\n },\n\n close() {\n setIsLoading(false);\n abortControllerRef.current = null;\n },\n }),\n );\n } catch (error) {\n if (isAbortError(error)) {\n return;\n }\n\n if (onError && error instanceof Error) {\n onError(error);\n }\n\n setError(error);\n }\n };\n\n return {\n setInput: submit, // Deprecated\n submit,\n object: data,\n error,\n isLoading,\n stop,\n };\n}\n\nexport const experimental_useObject = useObject;\n"],"mappings":";AAAA,SAAS,oBAAoB;AAC7B;AAAA,EAKE;AAAA,EACA;AAAA,OACK;AACP,SAAS,aAAa,QAAQ,gBAAgB;AAG9C,IAAM,mBAAmB,MAAM;AAiFxB,SAAS,aAAa;AAAA,EAC3B;AAAA,EACA,UAAU;AAAA,EACV;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA,OAAAA;AACF,GAA6C;AAC3C,QAAM,CAAC,UAAU,WAAW,IAAI,SAAoB,CAAC,CAAC;AACtD,QAAM,CAAC,OAAO,QAAQ,IAAI,SAAS,EAAE;AACrC,QAAM,CAAC,iBAAiB,kBAAkB,IAAI;AAAA,IAC5C;AAAA,EACF;AACA,QAAM,CAAC,QAAQ,SAAS,IAAI,SAA0B,k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AAEA,UAAI,kBAAkB;AACtB,UAAI,eAAgD;AAEpD,YAAM,SAAS,KAAK,YAAY,IAAI,kBAAkB,CAAC,EAAE;AAAA,QACvD,IAAI,eAAuB;AAAA,UACzB,MAAM,OAAO;AACX,+BAAmB;AAEnB,kBAAM,gBAAgB;AAAA,cACpB;AAAA,YACF;AAEA,gBAAI,CAAC,gBAAgB,cAAc,aAAa,GAAG;AACjD,6BAAe;AAEf,qBAAO,aAAa;AAAA,YACtB;AAAA,UACF;AAAA,UAEA,QAAQ;AACN,yBAAa,KAAK;AAClB,+BAAmB,UAAU;AAAA,UAC/B;AAAA,QACF,CAAC;AAAA,MACH;AAAA,IACF,SAASE,QAAO;AACd,UAAIR,cAAaQ,MAAK,GAAG;AACvB;AAAA,MACF;AAEA,UAAI,WAAWA,kBAAiB,OAAO;AACrC,gBAAQA,MAAK;AAAA,MACf;AAEA,eAASA,MAAK;AAAA,IAChB;AAAA,EACF;AAEA,SAAO;AAAA,IACL,UAAU;AAAA;AAAA,IACV;AAAA,IACA,QAAQ;AAAA,IACR;AAAA,IACA;AAAA,IACA;AAAA,EACF;AACF;AAEO,IAAM,yBAAyB;","names":["fetch","messages","_a","error","useCallback","useRef","useState","generateId","fetch","messages","headers","body","chatRequest","_a","error","useCallback","useEffect","useId","useRef","useState","useSWR","fetch","completion","data","isAbortError","useCallback","useId","useRef","useState","useSWR","getOriginalFetch","fetch","error"]}
|
|
1
|
+
{"version":3,"sources":["../src/use-assistant.ts","../src/use-chat.ts","../src/use-completion.ts","../src/use-object.ts"],"sourcesContent":["import { isAbortError } from '@ai-sdk/provider-utils';\nimport {\n AssistantStatus,\n CreateMessage,\n Message,\n UseAssistantOptions,\n generateId,\n readDataStream,\n} from '@ai-sdk/ui-utils';\nimport { useCallback, useRef, useState } from 'react';\n\n// use function to allow for mocking in tests:\nconst getOriginalFetch = () => fetch;\n\nexport type UseAssistantHelpers = {\n /**\n * The current array of chat messages.\n */\n messages: Message[];\n\n /**\n * Update the message store with a new array of messages.\n */\n setMessages: React.Dispatch<React.SetStateAction<Message[]>>;\n\n /**\n * The current thread ID.\n */\n threadId: string | undefined;\n\n /**\n * Set the current thread ID. Specifying a thread ID will switch to that thread, if it exists. If set to 'undefined', a new thread will be created. For both cases, `threadId` will be updated with the new value and `messages` will be cleared.\n */\n setThreadId: (threadId: string | undefined) => void;\n\n /**\n * The current value of the input field.\n */\n input: string;\n\n /**\n * Append a user message to the chat list. This triggers the API call to fetch\n * the assistant's response.\n * @param message The message to append\n * @param requestOptions Additional options to pass to the API call\n */\n append: (\n message: Message | CreateMessage,\n requestOptions?: {\n data?: Record<string, string>;\n },\n ) => Promise<void>;\n\n /**\nAbort the current request immediately, keep the generated tokens if any.\n */\n stop: () => void;\n\n /**\n * setState-powered method to update the input value.\n */\n setInput: React.Dispatch<React.SetStateAction<string>>;\n\n /**\n * Handler for the `onChange` event of the input field to control the input's value.\n */\n handleInputChange: (\n event:\n | React.ChangeEvent<HTMLInputElement>\n | React.ChangeEvent<HTMLTextAreaElement>,\n ) => void;\n\n /**\n * Form submission handler that automatically resets the input field and appends a user message.\n */\n submitMessage: (\n event?: React.FormEvent<HTMLFormElement>,\n requestOptions?: {\n data?: Record<string, string>;\n },\n ) => Promise<void>;\n\n /**\n * The current status of the assistant. This can be used to show a loading indicator.\n */\n status: AssistantStatus;\n\n /**\n * The error thrown during the assistant message processing, if any.\n */\n error: undefined | Error;\n};\n\nexport function useAssistant({\n api,\n threadId: threadIdParam,\n credentials,\n headers,\n body,\n onError,\n fetch,\n}: UseAssistantOptions): UseAssistantHelpers {\n const [messages, setMessages] = useState<Message[]>([]);\n const [input, setInput] = useState('');\n const [currentThreadId, setCurrentThreadId] = useState<string | undefined>(\n undefined,\n );\n const [status, setStatus] = useState<AssistantStatus>('awaiting_message');\n const [error, setError] = useState<undefined | Error>(undefined);\n\n const handleInputChange = (\n event:\n | React.ChangeEvent<HTMLInputElement>\n | React.ChangeEvent<HTMLTextAreaElement>,\n ) => {\n setInput(event.target.value);\n };\n\n // Abort controller to cancel the current API call.\n const abortControllerRef = useRef<AbortController | null>(null);\n\n const stop = useCallback(() => {\n if (abortControllerRef.current) {\n abortControllerRef.current.abort();\n abortControllerRef.current = null;\n }\n }, []);\n\n const append = async (\n message: Message | CreateMessage,\n requestOptions?: {\n data?: Record<string, string>;\n },\n ) => {\n setStatus('in_progress');\n\n setMessages(messages => [\n ...messages,\n {\n ...message,\n id: message.id ?? generateId(),\n },\n ]);\n\n setInput('');\n\n const abortController = new AbortController();\n\n try {\n abortControllerRef.current = abortController;\n\n const actualFetch = fetch ?? getOriginalFetch();\n const response = await actualFetch(api, {\n method: 'POST',\n credentials,\n signal: abortController.signal,\n headers: { 'Content-Type': 'application/json', ...headers },\n body: JSON.stringify({\n ...body,\n // always use user-provided threadId when available:\n threadId: threadIdParam ?? currentThreadId ?? null,\n message: message.content,\n\n // optional request data:\n data: requestOptions?.data,\n }),\n });\n\n if (!response.ok) {\n throw new Error(\n (await response.text()) ?? 'Failed to fetch the assistant response.',\n );\n }\n\n if (response.body == null) {\n throw new Error('The response body is empty.');\n }\n\n for await (const { type, value } of readDataStream(\n response.body.getReader(),\n )) {\n switch (type) {\n case 'assistant_message': {\n setMessages(messages => [\n ...messages,\n {\n id: value.id,\n role: value.role,\n content: value.content[0].text.value,\n },\n ]);\n break;\n }\n\n case 'text': {\n // text delta - add to last message:\n setMessages(messages => {\n const lastMessage = messages[messages.length - 1];\n return [\n ...messages.slice(0, messages.length - 1),\n {\n id: lastMessage.id,\n role: lastMessage.role,\n content: lastMessage.content + value,\n },\n ];\n });\n\n break;\n }\n\n case 'data_message': {\n setMessages(messages => [\n ...messages,\n {\n id: value.id ?? generateId(),\n role: 'data',\n content: '',\n data: value.data,\n },\n ]);\n break;\n }\n\n case 'assistant_control_data': {\n setCurrentThreadId(value.threadId);\n\n // set id of last message:\n setMessages(messages => {\n const lastMessage = messages[messages.length - 1];\n lastMessage.id = value.messageId;\n return [...messages.slice(0, messages.length - 1), lastMessage];\n });\n\n break;\n }\n\n case 'error': {\n setError(new Error(value));\n break;\n }\n }\n }\n } catch (error) {\n // Ignore abort errors as they are expected when the user cancels the request:\n if (isAbortError(error) && abortController.signal.aborted) {\n abortControllerRef.current = null;\n return;\n }\n\n if (onError && error instanceof Error) {\n onError(error);\n }\n\n setError(error as Error);\n } finally {\n abortControllerRef.current = null;\n setStatus('awaiting_message');\n }\n };\n\n const submitMessage = async (\n event?: React.FormEvent<HTMLFormElement>,\n requestOptions?: {\n data?: Record<string, string>;\n },\n ) => {\n event?.preventDefault?.();\n\n if (input === '') {\n return;\n }\n\n append({ role: 'user', content: input }, requestOptions);\n };\n\n const setThreadId = (threadId: string | undefined) => {\n setCurrentThreadId(threadId);\n setMessages([]);\n };\n\n return {\n append,\n messages,\n setMessages,\n threadId: currentThreadId,\n setThreadId,\n input,\n setInput,\n handleInputChange,\n submitMessage,\n status,\n error,\n stop,\n };\n}\n\n/**\n@deprecated Use `useAssistant` instead.\n */\nexport const experimental_useAssistant = useAssistant;\n","import type {\n ChatRequest,\n ChatRequestOptions,\n Attachment,\n CreateMessage,\n FetchFunction,\n IdGenerator,\n JSONValue,\n Message,\n UseChatOptions,\n} from '@ai-sdk/ui-utils';\nimport {\n callChatApi,\n generateId as generateIdFunc,\n processChatStream,\n} from '@ai-sdk/ui-utils';\nimport { useCallback, useEffect, useId, useRef, useState } from 'react';\nimport useSWR, { KeyedMutator } from 'swr';\n\nexport type { CreateMessage, Message, UseChatOptions };\n\nexport type UseChatHelpers = {\n /** Current messages in the chat */\n messages: Message[];\n /** The error object of the API request */\n error: undefined | Error;\n /**\n * Append a user message to the chat list. This triggers the API call to fetch\n * the assistant's response.\n * @param message The message to append\n * @param options Additional options to pass to the API call\n */\n append: (\n message: Message | CreateMessage,\n chatRequestOptions?: ChatRequestOptions,\n ) => Promise<string | null | undefined>;\n /**\n * Reload the last AI chat response for the given chat history. If the last\n * message isn't from the assistant, it will request the API to generate a\n * new response.\n */\n reload: (\n chatRequestOptions?: ChatRequestOptions,\n ) => Promise<string | null | undefined>;\n /**\n * Abort the current request immediately, keep the generated tokens if any.\n */\n stop: () => void;\n /**\n * Update the `messages` state locally. This is useful when you want to\n * edit the messages on the client, and then trigger the `reload` method\n * manually to regenerate the AI response.\n */\n setMessages: (\n messages: Message[] | ((messages: Message[]) => Message[]),\n ) => void;\n /** The current value of the input */\n input: string;\n /** setState-powered method to update the input value */\n setInput: React.Dispatch<React.SetStateAction<string>>;\n /** An input/textarea-ready onChange handler to control the value of the input */\n handleInputChange: (\n e:\n | React.ChangeEvent<HTMLInputElement>\n | React.ChangeEvent<HTMLTextAreaElement>,\n ) => void;\n /** Form submission handler to automatically reset input and append a user message */\n handleSubmit: (\n event?: { preventDefault?: () => void },\n chatRequestOptions?: ChatRequestOptions,\n ) => void;\n metadata?: Object;\n /** Whether the API request is in progress */\n isLoading: boolean;\n /** Additional data added on the server via StreamData */\n data?: JSONValue[];\n};\n\nconst getStreamedResponse = async (\n api: string,\n chatRequest: ChatRequest,\n mutate: KeyedMutator<Message[]>,\n mutateStreamData: KeyedMutator<JSONValue[] | undefined>,\n existingData: JSONValue[] | undefined,\n extraMetadataRef: React.MutableRefObject<any>,\n messagesRef: React.MutableRefObject<Message[]>,\n abortControllerRef: React.MutableRefObject<AbortController | null>,\n generateId: IdGenerator,\n streamProtocol: UseChatOptions['streamProtocol'],\n onFinish: UseChatOptions['onFinish'],\n onResponse: ((response: Response) => void | Promise<void>) | undefined,\n onToolCall: UseChatOptions['onToolCall'] | undefined,\n sendExtraMessageFields: boolean | undefined,\n experimental_prepareRequestBody:\n | ((options: {\n messages: Message[];\n requestData?: JSONValue;\n requestBody?: object;\n }) => JSONValue)\n | undefined,\n fetch: FetchFunction | undefined,\n keepLastMessageOnError: boolean,\n) => {\n // Do an optimistic update to the chat state to show the updated messages immediately:\n const previousMessages = messagesRef.current;\n mutate(chatRequest.messages, false);\n\n const constructedMessagesPayload = sendExtraMessageFields\n ? chatRequest.messages\n : chatRequest.messages.map(\n ({\n role,\n content,\n experimental_attachments,\n name,\n data,\n annotations,\n toolInvocations,\n function_call,\n tool_calls,\n tool_call_id,\n }) => ({\n role,\n content,\n ...(experimental_attachments !== undefined && {\n experimental_attachments,\n }),\n ...(name !== undefined && { name }),\n ...(data !== undefined && { data }),\n ...(annotations !== undefined && { annotations }),\n ...(toolInvocations !== undefined && { toolInvocations }),\n // outdated function/tool call handling (TODO deprecate):\n tool_call_id,\n ...(function_call !== undefined && { function_call }),\n ...(tool_calls !== undefined && { tool_calls }),\n }),\n );\n\n return await callChatApi({\n api,\n body: experimental_prepareRequestBody?.({\n messages: chatRequest.messages,\n requestData: chatRequest.data,\n requestBody: chatRequest.body,\n }) ?? {\n messages: constructedMessagesPayload,\n data: chatRequest.data,\n ...extraMetadataRef.current.body,\n ...chatRequest.body,\n ...(chatRequest.functions !== undefined && {\n functions: chatRequest.functions,\n }),\n ...(chatRequest.function_call !== undefined && {\n function_call: chatRequest.function_call,\n }),\n ...(chatRequest.tools !== undefined && {\n tools: chatRequest.tools,\n }),\n ...(chatRequest.tool_choice !== undefined && {\n tool_choice: chatRequest.tool_choice,\n }),\n },\n streamProtocol,\n credentials: extraMetadataRef.current.credentials,\n headers: {\n ...extraMetadataRef.current.headers,\n ...chatRequest.headers,\n },\n abortController: () => abortControllerRef.current,\n restoreMessagesOnFailure() {\n if (!keepLastMessageOnError) {\n mutate(previousMessages, false);\n }\n },\n onResponse,\n onUpdate(merged, data) {\n mutate([...chatRequest.messages, ...merged], false);\n mutateStreamData([...(existingData || []), ...(data || [])], false);\n },\n onToolCall,\n onFinish,\n generateId,\n fetch,\n });\n};\n\nexport function useChat({\n api = '/api/chat',\n id,\n initialMessages,\n initialInput = '',\n sendExtraMessageFields,\n experimental_onFunctionCall,\n experimental_onToolCall,\n onToolCall,\n experimental_prepareRequestBody,\n experimental_maxAutomaticRoundtrips = 0,\n maxAutomaticRoundtrips = experimental_maxAutomaticRoundtrips,\n maxToolRoundtrips = maxAutomaticRoundtrips,\n streamMode,\n streamProtocol,\n onResponse,\n onFinish,\n onError,\n credentials,\n headers,\n body,\n generateId = generateIdFunc,\n fetch,\n keepLastMessageOnError = false,\n}: UseChatOptions & {\n key?: string;\n\n /**\n@deprecated Use `maxToolRoundtrips` instead.\n */\n experimental_maxAutomaticRoundtrips?: number;\n\n /**\n@deprecated Use `maxToolRoundtrips` instead.\n */\n maxAutomaticRoundtrips?: number;\n\n /**\n * Experimental (React only). When a function is provided, it will be used\n * to prepare the request body for the chat API. This can be useful for\n * customizing the request body based on the messages and data in the chat.\n *\n * @param messages The current messages in the chat.\n * @param requestData The data object passed in the chat request.\n * @param requestBody The request body object passed in the chat request.\n */\n experimental_prepareRequestBody?: (options: {\n messages: Message[];\n requestData?: JSONValue;\n requestBody?: object;\n }) => JSONValue;\n\n /**\nMaximal number of automatic roundtrips for tool calls.\n\nAn automatic tool call roundtrip is a call to the server with the\ntool call results when all tool calls in the last assistant\nmessage have results.\n\nA maximum number is required to prevent infinite loops in the\ncase of misconfigured tools.\n\nBy default, it's set to 0, which will disable the feature.\n */\n maxToolRoundtrips?: number;\n} = {}): UseChatHelpers & {\n /**\n * @deprecated Use `addToolResult` instead.\n */\n experimental_addToolResult: ({\n toolCallId,\n result,\n }: {\n toolCallId: string;\n result: any;\n }) => void;\n addToolResult: ({\n toolCallId,\n result,\n }: {\n toolCallId: string;\n result: any;\n }) => void;\n} {\n // streamMode is deprecated, use streamProtocol instead.\n if (streamMode) {\n streamProtocol ??= streamMode === 'text' ? 'text' : undefined;\n }\n\n // Generate a unique id for the chat if not provided.\n const hookId = useId();\n const idKey = id ?? hookId;\n const chatKey = typeof api === 'string' ? [api, idKey] : idKey;\n\n // Store a empty array as the initial messages\n // (instead of using a default parameter value that gets re-created each time)\n // to avoid re-renders:\n const [initialMessagesFallback] = useState([]);\n\n // Store the chat state in SWR, using the chatId as the key to share states.\n const { data: messages, mutate } = useSWR<Message[]>(\n [chatKey, 'messages'],\n null,\n { fallbackData: initialMessages ?? initialMessagesFallback },\n );\n\n // We store loading state in another hook to sync loading states across hook invocations\n const { data: isLoading = false, mutate: mutateLoading } = useSWR<boolean>(\n [chatKey, 'loading'],\n null,\n );\n\n const { data: streamData, mutate: mutateStreamData } = useSWR<\n JSONValue[] | undefined\n >([chatKey, 'streamData'], null);\n\n const { data: error = undefined, mutate: setError } = useSWR<\n undefined | Error\n >([chatKey, 'error'], null);\n\n // Keep the latest messages in a ref.\n const messagesRef = useRef<Message[]>(messages || []);\n useEffect(() => {\n messagesRef.current = messages || [];\n }, [messages]);\n\n // Abort controller to cancel the current API call.\n const abortControllerRef = useRef<AbortController | null>(null);\n\n const extraMetadataRef = useRef({\n credentials,\n headers,\n body,\n });\n\n useEffect(() => {\n extraMetadataRef.current = {\n credentials,\n headers,\n body,\n };\n }, [credentials, headers, body]);\n\n const triggerRequest = useCallback(\n async (chatRequest: ChatRequest) => {\n const messageCount = messagesRef.current.length;\n\n try {\n mutateLoading(true);\n setError(undefined);\n\n const abortController = new AbortController();\n abortControllerRef.current = abortController;\n\n await processChatStream({\n getStreamedResponse: () =>\n getStreamedResponse(\n api,\n chatRequest,\n mutate,\n mutateStreamData,\n streamData!,\n extraMetadataRef,\n messagesRef,\n abortControllerRef,\n generateId,\n streamProtocol,\n onFinish,\n onResponse,\n onToolCall,\n sendExtraMessageFields,\n experimental_prepareRequestBody,\n fetch,\n keepLastMessageOnError,\n ),\n experimental_onFunctionCall,\n experimental_onToolCall,\n updateChatRequest: chatRequestParam => {\n chatRequest = chatRequestParam;\n },\n getCurrentMessages: () => messagesRef.current,\n });\n\n abortControllerRef.current = null;\n } catch (err) {\n // Ignore abort errors as they are expected.\n if ((err as any).name === 'AbortError') {\n abortControllerRef.current = null;\n return null;\n }\n\n if (onError && err instanceof Error) {\n onError(err);\n }\n\n setError(err as Error);\n } finally {\n mutateLoading(false);\n }\n\n // auto-submit when all tool calls in the last assistant message have results:\n const messages = messagesRef.current;\n const lastMessage = messages[messages.length - 1];\n if (\n // ensure we actually have new messages (to prevent infinite loops in case of errors):\n messages.length > messageCount &&\n // ensure there is a last message:\n lastMessage != null &&\n // check if the feature is enabled:\n maxToolRoundtrips > 0 &&\n // check that roundtrip is possible:\n isAssistantMessageWithCompletedToolCalls(lastMessage) &&\n // limit the number of automatic roundtrips:\n countTrailingAssistantMessages(messages) <= maxToolRoundtrips\n ) {\n await triggerRequest({ messages });\n }\n },\n [\n mutate,\n mutateLoading,\n api,\n extraMetadataRef,\n onResponse,\n onFinish,\n onError,\n setError,\n mutateStreamData,\n streamData,\n streamProtocol,\n sendExtraMessageFields,\n experimental_onFunctionCall,\n experimental_onToolCall,\n experimental_prepareRequestBody,\n onToolCall,\n maxToolRoundtrips,\n messagesRef,\n abortControllerRef,\n generateId,\n fetch,\n keepLastMessageOnError,\n ],\n );\n\n const append = useCallback(\n async (\n message: Message | CreateMessage,\n {\n options,\n functions,\n function_call,\n tools,\n tool_choice,\n data,\n headers,\n body,\n }: ChatRequestOptions = {},\n ) => {\n if (!message.id) {\n message.id = generateId();\n }\n\n const requestOptions = {\n headers: headers ?? options?.headers,\n body: body ?? options?.body,\n };\n\n const chatRequest: ChatRequest = {\n messages: messagesRef.current.concat(message as Message),\n options: requestOptions,\n headers: requestOptions.headers,\n body: requestOptions.body,\n data,\n ...(functions !== undefined && { functions }),\n ...(function_call !== undefined && { function_call }),\n ...(tools !== undefined && { tools }),\n ...(tool_choice !== undefined && { tool_choice }),\n };\n\n return triggerRequest(chatRequest);\n },\n [triggerRequest, generateId],\n );\n\n const reload = useCallback(\n async ({\n options,\n functions,\n function_call,\n tools,\n tool_choice,\n data,\n headers,\n body,\n }: ChatRequestOptions = {}) => {\n if (messagesRef.current.length === 0) return null;\n\n const requestOptions = {\n headers: headers ?? options?.headers,\n body: body ?? options?.body,\n };\n\n // Remove last assistant message and retry last user message.\n const lastMessage = messagesRef.current[messagesRef.current.length - 1];\n if (lastMessage.role === 'assistant') {\n const chatRequest: ChatRequest = {\n messages: messagesRef.current.slice(0, -1),\n options: requestOptions,\n headers: requestOptions.headers,\n body: requestOptions.body,\n data,\n ...(functions !== undefined && { functions }),\n ...(function_call !== undefined && { function_call }),\n ...(tools !== undefined && { tools }),\n ...(tool_choice !== undefined && { tool_choice }),\n };\n\n return triggerRequest(chatRequest);\n }\n\n const chatRequest: ChatRequest = {\n messages: messagesRef.current,\n options: requestOptions,\n headers: requestOptions.headers,\n body: requestOptions.body,\n data,\n ...(functions !== undefined && { functions }),\n ...(function_call !== undefined && { function_call }),\n ...(tools !== undefined && { tools }),\n ...(tool_choice !== undefined && { tool_choice }),\n };\n\n return triggerRequest(chatRequest);\n },\n [triggerRequest],\n );\n\n const stop = useCallback(() => {\n if (abortControllerRef.current) {\n abortControllerRef.current.abort();\n abortControllerRef.current = null;\n }\n }, []);\n\n const setMessages = useCallback(\n (messages: Message[] | ((messages: Message[]) => Message[])) => {\n if (typeof messages === 'function') {\n messages = messages(messagesRef.current);\n }\n\n mutate(messages, false);\n messagesRef.current = messages;\n },\n [mutate],\n );\n\n // Input state and handlers.\n const [input, setInput] = useState(initialInput);\n\n const handleSubmit = useCallback(\n async (\n event?: { preventDefault?: () => void },\n options: ChatRequestOptions = {},\n metadata?: Object,\n ) => {\n event?.preventDefault?.();\n\n if (!input && !options.allowEmptySubmit) return;\n\n if (metadata) {\n extraMetadataRef.current = {\n ...extraMetadataRef.current,\n ...metadata,\n };\n }\n\n const attachmentsForRequest: Attachment[] = [];\n const attachmentsFromOptions = options.experimental_attachments;\n\n if (attachmentsFromOptions) {\n if (attachmentsFromOptions instanceof FileList) {\n for (const attachment of Array.from(attachmentsFromOptions)) {\n const { name, type } = attachment;\n\n const dataUrl = await new Promise<string>((resolve, reject) => {\n const reader = new FileReader();\n reader.onload = readerEvent => {\n resolve(readerEvent.target?.result as string);\n };\n reader.onerror = error => reject(error);\n reader.readAsDataURL(attachment);\n });\n\n attachmentsForRequest.push({\n name,\n contentType: type,\n url: dataUrl,\n });\n }\n } else if (Array.isArray(attachmentsFromOptions)) {\n for (const file of attachmentsFromOptions) {\n const { name, url, contentType } = file;\n\n attachmentsForRequest.push({\n name,\n contentType,\n url,\n });\n }\n } else {\n throw new Error('Invalid attachments type');\n }\n }\n\n const requestOptions = {\n headers: options.headers ?? options.options?.headers,\n body: options.body ?? options.options?.body,\n };\n\n const messages =\n !input && options.allowEmptySubmit\n ? messagesRef.current\n : messagesRef.current.concat({\n id: generateId(),\n createdAt: new Date(),\n role: 'user',\n content: input,\n experimental_attachments:\n attachmentsForRequest.length > 0\n ? attachmentsForRequest\n : undefined,\n });\n\n const chatRequest: ChatRequest = {\n messages,\n options: requestOptions,\n headers: requestOptions.headers,\n body: requestOptions.body,\n data: options.data,\n };\n\n triggerRequest(chatRequest);\n\n setInput('');\n },\n [input, generateId, triggerRequest],\n );\n\n const handleInputChange = (e: any) => {\n setInput(e.target.value);\n };\n\n const addToolResult = ({\n toolCallId,\n result,\n }: {\n toolCallId: string;\n result: any;\n }) => {\n const updatedMessages = messagesRef.current.map((message, index, arr) =>\n // update the tool calls in the last assistant message:\n index === arr.length - 1 &&\n message.role === 'assistant' &&\n message.toolInvocations\n ? {\n ...message,\n toolInvocations: message.toolInvocations.map(toolInvocation =>\n toolInvocation.toolCallId === toolCallId\n ? { ...toolInvocation, result }\n : toolInvocation,\n ),\n }\n : message,\n );\n\n mutate(updatedMessages, false);\n\n // auto-submit when all tool calls in the last assistant message have results:\n const lastMessage = updatedMessages[updatedMessages.length - 1];\n if (isAssistantMessageWithCompletedToolCalls(lastMessage)) {\n triggerRequest({ messages: updatedMessages });\n }\n };\n\n return {\n messages: messages || [],\n error,\n append,\n reload,\n stop,\n setMessages,\n input,\n setInput,\n handleInputChange,\n handleSubmit,\n isLoading,\n data: streamData,\n addToolResult,\n experimental_addToolResult: addToolResult,\n };\n}\n\n/**\nCheck if the message is an assistant message with completed tool calls.\nThe message must have at least one tool invocation and all tool invocations\nmust have a result.\n */\nfunction isAssistantMessageWithCompletedToolCalls(message: Message) {\n return (\n message.role === 'assistant' &&\n message.toolInvocations &&\n message.toolInvocations.length > 0 &&\n message.toolInvocations.every(toolInvocation => 'result' in toolInvocation)\n );\n}\n\n/**\nReturns the number of trailing assistant messages in the array.\n */\nfunction countTrailingAssistantMessages(messages: Message[]) {\n let count = 0;\n for (let i = messages.length - 1; i >= 0; i--) {\n if (messages[i].role === 'assistant') {\n count++;\n } else {\n break;\n }\n }\n return count;\n}\n","import {\n JSONValue,\n RequestOptions,\n UseCompletionOptions,\n callCompletionApi,\n} from '@ai-sdk/ui-utils';\nimport { useCallback, useEffect, useId, useRef, useState } from 'react';\nimport useSWR from 'swr';\n\nexport type { UseCompletionOptions };\n\nexport type UseCompletionHelpers = {\n /** The current completion result */\n completion: string;\n /**\n * Send a new prompt to the API endpoint and update the completion state.\n */\n complete: (\n prompt: string,\n options?: RequestOptions,\n ) => Promise<string | null | undefined>;\n /** The error object of the API request */\n error: undefined | Error;\n /**\n * Abort the current API request but keep the generated tokens.\n */\n stop: () => void;\n /**\n * Update the `completion` state locally.\n */\n setCompletion: (completion: string) => void;\n /** The current value of the input */\n input: string;\n /** setState-powered method to update the input value */\n setInput: React.Dispatch<React.SetStateAction<string>>;\n /**\n * An input/textarea-ready onChange handler to control the value of the input\n * @example\n * ```jsx\n * <input onChange={handleInputChange} value={input} />\n * ```\n */\n handleInputChange: (\n event:\n | React.ChangeEvent<HTMLInputElement>\n | React.ChangeEvent<HTMLTextAreaElement>,\n ) => void;\n\n /**\n * Form submission handler to automatically reset input and append a user message\n * @example\n * ```jsx\n * <form onSubmit={handleSubmit}>\n * <input onChange={handleInputChange} value={input} />\n * </form>\n * ```\n */\n handleSubmit: (event?: { preventDefault?: () => void }) => void;\n\n /** Whether the API request is in progress */\n isLoading: boolean;\n /** Additional data added on the server via StreamData */\n data?: JSONValue[];\n};\n\nexport function useCompletion({\n api = '/api/completion',\n id,\n initialCompletion = '',\n initialInput = '',\n credentials,\n headers,\n body,\n streamMode,\n streamProtocol,\n fetch,\n onResponse,\n onFinish,\n onError,\n}: UseCompletionOptions = {}): UseCompletionHelpers {\n // streamMode is deprecated, use streamProtocol instead.\n if (streamMode) {\n streamProtocol ??= streamMode === 'text' ? 'text' : undefined;\n }\n\n // Generate an unique id for the completion if not provided.\n const hookId = useId();\n const completionId = id || hookId;\n\n // Store the completion state in SWR, using the completionId as the key to share states.\n const { data, mutate } = useSWR<string>([api, completionId], null, {\n fallbackData: initialCompletion,\n });\n\n const { data: isLoading = false, mutate: mutateLoading } = useSWR<boolean>(\n [completionId, 'loading'],\n null,\n );\n\n const { data: streamData, mutate: mutateStreamData } = useSWR<\n JSONValue[] | undefined\n >([completionId, 'streamData'], null);\n\n const [error, setError] = useState<undefined | Error>(undefined);\n const completion = data!;\n\n // Abort controller to cancel the current API call.\n const [abortController, setAbortController] =\n useState<AbortController | null>(null);\n\n const extraMetadataRef = useRef({\n credentials,\n headers,\n body,\n });\n useEffect(() => {\n extraMetadataRef.current = {\n credentials,\n headers,\n body,\n };\n }, [credentials, headers, body]);\n\n const triggerRequest = useCallback(\n async (prompt: string, options?: RequestOptions) =>\n callCompletionApi({\n api,\n prompt,\n credentials: extraMetadataRef.current.credentials,\n headers: { ...extraMetadataRef.current.headers, ...options?.headers },\n body: {\n ...extraMetadataRef.current.body,\n ...options?.body,\n },\n streamProtocol,\n fetch,\n setCompletion: completion => mutate(completion, false),\n setLoading: mutateLoading,\n setError,\n setAbortController,\n onResponse,\n onFinish,\n onError,\n onData: data => {\n mutateStreamData([...(streamData || []), ...(data || [])], false);\n },\n }),\n [\n mutate,\n mutateLoading,\n api,\n extraMetadataRef,\n setAbortController,\n onResponse,\n onFinish,\n onError,\n setError,\n streamData,\n streamProtocol,\n fetch,\n mutateStreamData,\n ],\n );\n\n const stop = useCallback(() => {\n if (abortController) {\n abortController.abort();\n setAbortController(null);\n }\n }, [abortController]);\n\n const setCompletion = useCallback(\n (completion: string) => {\n mutate(completion, false);\n },\n [mutate],\n );\n\n const complete = useCallback<UseCompletionHelpers['complete']>(\n async (prompt, options) => {\n return triggerRequest(prompt, options);\n },\n [triggerRequest],\n );\n\n const [input, setInput] = useState(initialInput);\n\n const handleSubmit = useCallback(\n (event?: { preventDefault?: () => void }) => {\n event?.preventDefault?.();\n return input ? complete(input) : undefined;\n },\n [input, complete],\n );\n\n const handleInputChange = (e: any) => {\n setInput(e.target.value);\n };\n\n return {\n completion,\n complete,\n error,\n setCompletion,\n stop,\n input,\n setInput,\n handleInputChange,\n handleSubmit,\n isLoading,\n data: streamData,\n };\n}\n","import { isAbortError } from '@ai-sdk/provider-utils';\nimport {\n DeepPartial,\n FetchFunction,\n isDeepEqualData,\n parsePartialJson,\n} from '@ai-sdk/ui-utils';\nimport { useCallback, useId, useRef, useState } from 'react';\nimport useSWR from 'swr';\nimport z from 'zod';\n\n// use function to allow for mocking in tests:\nconst getOriginalFetch = () => fetch;\n\nexport type Experimental_UseObjectOptions<RESULT> = {\n /**\n * The API endpoint. It should stream JSON that matches the schema as chunked text.\n */\n api: string;\n\n /**\n * A Zod schema that defines the shape of the complete object.\n */\n schema: z.Schema<RESULT>;\n\n /**\n * An unique identifier. If not provided, a random one will be\n * generated. When provided, the `useObject` hook with the same `id` will\n * have shared states across components.\n */\n id?: string;\n\n /**\n * An optional value for the initial object.\n */\n initialValue?: DeepPartial<RESULT>;\n\n /**\nCustom fetch implementation. You can use it as a middleware to intercept requests,\nor to provide a custom fetch implementation for e.g. testing.\n */\n fetch?: FetchFunction;\n\n /**\n * Callback function to be called when an error is encountered.\n */\n onError?: (error: Error) => void;\n};\n\nexport type Experimental_UseObjectHelpers<RESULT, INPUT> = {\n /**\n * @deprecated Use `submit` instead.\n */\n setInput: (input: INPUT) => void;\n\n /**\n * Calls the API with the provided input as JSON body.\n */\n submit: (input: INPUT) => void;\n\n /**\n * The current value for the generated object. Updated as the API streams JSON chunks.\n */\n object: DeepPartial<RESULT> | undefined;\n\n /**\n * The error object of the API request if any.\n */\n error: undefined | unknown;\n\n /**\n * Flag that indicates whether an API request is in progress.\n */\n isLoading: boolean;\n\n /**\n * Abort the current request immediately, keep the current partial object if any.\n */\n stop: () => void;\n};\n\nfunction useObject<RESULT, INPUT = any>({\n api,\n id,\n schema, // required, in the future we will use it for validation\n initialValue,\n fetch,\n onError,\n}: Experimental_UseObjectOptions<RESULT>): Experimental_UseObjectHelpers<\n RESULT,\n INPUT\n> {\n // Generate an unique id if not provided.\n const hookId = useId();\n const completionId = id ?? hookId;\n\n // Store the completion state in SWR, using the completionId as the key to share states.\n const { data, mutate } = useSWR<DeepPartial<RESULT>>(\n [api, completionId],\n null,\n { fallbackData: initialValue },\n );\n\n const [error, setError] = useState<undefined | unknown>(undefined);\n const [isLoading, setIsLoading] = useState(false);\n\n // Abort controller to cancel the current API call.\n const abortControllerRef = useRef<AbortController | null>(null);\n\n const stop = useCallback(() => {\n try {\n abortControllerRef.current?.abort();\n } catch (ignored) {\n } finally {\n setIsLoading(false);\n abortControllerRef.current = null;\n }\n }, []);\n\n const submit = async (input: INPUT) => {\n try {\n setIsLoading(true);\n setError(undefined);\n\n const abortController = new AbortController();\n abortControllerRef.current = abortController;\n\n const actualFetch = fetch ?? getOriginalFetch();\n const response = await actualFetch(api, {\n method: 'POST',\n headers: { 'Content-Type': 'application/json' },\n signal: abortController.signal,\n body: JSON.stringify(input),\n });\n\n if (!response.ok) {\n throw new Error(\n (await response.text()) ?? 'Failed to fetch the response.',\n );\n }\n\n if (response.body == null) {\n throw new Error('The response body is empty.');\n }\n\n let accumulatedText = '';\n let latestObject: DeepPartial<RESULT> | undefined = undefined;\n\n await response.body.pipeThrough(new TextDecoderStream()).pipeTo(\n new WritableStream<string>({\n write(chunk) {\n accumulatedText += chunk;\n\n const currentObject = parsePartialJson(\n accumulatedText,\n ) as DeepPartial<RESULT>;\n\n if (!isDeepEqualData(latestObject, currentObject)) {\n latestObject = currentObject;\n\n mutate(currentObject);\n }\n },\n\n close() {\n setIsLoading(false);\n abortControllerRef.current = null;\n },\n }),\n );\n } catch (error) {\n if (isAbortError(error)) {\n return;\n }\n\n if (onError && error instanceof Error) {\n onError(error);\n }\n\n setError(error);\n }\n };\n\n return {\n setInput: submit, // Deprecated\n submit,\n object: data,\n error,\n isLoading,\n stop,\n };\n}\n\nexport const experimental_useObject = useObject;\n"],"mappings":";AAAA,SAAS,oBAAoB;AAC7B;AAAA,EAKE;AAAA,EACA;AAAA,OACK;AACP,SAAS,aAAa,QAAQ,gBAAgB;AAG9C,IAAM,mBAAmB,MAAM;AAiFxB,SAAS,aAAa;AAAA,EAC3B;AAAA,EACA,UAAU;AAAA,EACV;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA,OAAAA;AACF,GAA6C;AAC3C,QAAM,CAAC,UAAU,WAAW,IAAI,SAAoB,CAAC,CAAC;AACtD,QAAM,CAAC,OAAO,QAAQ,IAAI,SAAS,EAAE;AACrC,QAAM,CAAC,iBAAiB,kBAAkB,IAAI;AAAA,IAC5C;AAAA,EACF;AACA,QAAM,CAAC,QAAQ,SAAS,IAAI,SAA0B,kBAAkB;AACxE,QAAM,CAAC,OAAO,QAAQ,IAAI,SAA4B,MAAS;AAE/D,QAAM,oBAAoB,CACxB,UAGG;AACH,aAAS,MAAM,OAAO,KAAK;AAAA,EAC7B;AAGA,QAAM,qBAAqB,OAA+B,IAAI;AAE9D,QAAM,OAAO,YAAY,MAAM;AAC7B,QAAI,mBAAmB,SAAS;AAC9B,yBAAmB,QAAQ,MAAM;AACjC,yBAAmB,UAAU;AAAA,IAC/B;AAAA,EACF,GAAG,CAAC,CAAC;AAEL,QAAM,SAAS,OACb,SACA,mBAGG;AArIP;AAsII,cAAU,aAAa;AAEvB,gBAAY,CAAAC,cAAS;AAxIzB,UAAAC;AAwI4B;AAAA,QACtB,GAAGD;AAAA,QACH;AAAA,UACE,GAAG;AAAA,UACH,KAAIC,MAAA,QAAQ,OAAR,OAAAA,MAAc,WAAW;AAAA,QAC/B;AAAA,MACF;AAAA,KAAC;AAED,aAAS,EAAE;AAEX,UAAM,kBAAkB,IAAI,gBA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AAA,MACF;AAEA,UAAI,WAAWA,kBAAiB,OAAO;AACrC,gBAAQA,MAAK;AAAA,MACf;AAEA,eAASA,MAAK;AAAA,IAChB;AAAA,EACF;AAEA,SAAO;AAAA,IACL,UAAU;AAAA;AAAA,IACV;AAAA,IACA,QAAQ;AAAA,IACR;AAAA,IACA;AAAA,IACA;AAAA,EACF;AACF;AAEO,IAAM,yBAAyB;","names":["fetch","messages","_a","error","useCallback","useRef","useState","generateId","fetch","messages","headers","body","chatRequest","_a","error","useCallback","useEffect","useId","useRef","useState","useSWR","fetch","completion","data","isAbortError","useCallback","useId","useRef","useState","useSWR","getOriginalFetch","fetch","error"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ai-sdk/react",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.31",
|
|
4
4
|
"license": "Apache-2.0",
|
|
5
5
|
"sideEffects": false,
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
},
|
|
17
17
|
"dependencies": {
|
|
18
18
|
"@ai-sdk/provider-utils": "1.0.5",
|
|
19
|
-
"@ai-sdk/ui-utils": "0.0.
|
|
19
|
+
"@ai-sdk/ui-utils": "0.0.21",
|
|
20
20
|
"swr": "2.2.5"
|
|
21
21
|
},
|
|
22
22
|
"devDependencies": {
|
package/src/use-chat.ts
CHANGED
|
@@ -86,8 +86,8 @@ const getStreamedResponse = async (
|
|
|
86
86
|
messagesRef: React.MutableRefObject<Message[]>,
|
|
87
87
|
abortControllerRef: React.MutableRefObject<AbortController | null>,
|
|
88
88
|
generateId: IdGenerator,
|
|
89
|
-
|
|
90
|
-
onFinish:
|
|
89
|
+
streamProtocol: UseChatOptions['streamProtocol'],
|
|
90
|
+
onFinish: UseChatOptions['onFinish'],
|
|
91
91
|
onResponse: ((response: Response) => void | Promise<void>) | undefined,
|
|
92
92
|
onToolCall: UseChatOptions['onToolCall'] | undefined,
|
|
93
93
|
sendExtraMessageFields: boolean | undefined,
|
|
@@ -160,7 +160,7 @@ const getStreamedResponse = async (
|
|
|
160
160
|
tool_choice: chatRequest.tool_choice,
|
|
161
161
|
}),
|
|
162
162
|
},
|
|
163
|
-
|
|
163
|
+
streamProtocol,
|
|
164
164
|
credentials: extraMetadataRef.current.credentials,
|
|
165
165
|
headers: {
|
|
166
166
|
...extraMetadataRef.current.headers,
|
|
@@ -198,6 +198,7 @@ export function useChat({
|
|
|
198
198
|
maxAutomaticRoundtrips = experimental_maxAutomaticRoundtrips,
|
|
199
199
|
maxToolRoundtrips = maxAutomaticRoundtrips,
|
|
200
200
|
streamMode,
|
|
201
|
+
streamProtocol,
|
|
201
202
|
onResponse,
|
|
202
203
|
onFinish,
|
|
203
204
|
onError,
|
|
@@ -267,6 +268,11 @@ By default, it's set to 0, which will disable the feature.
|
|
|
267
268
|
result: any;
|
|
268
269
|
}) => void;
|
|
269
270
|
} {
|
|
271
|
+
// streamMode is deprecated, use streamProtocol instead.
|
|
272
|
+
if (streamMode) {
|
|
273
|
+
streamProtocol ??= streamMode === 'text' ? 'text' : undefined;
|
|
274
|
+
}
|
|
275
|
+
|
|
270
276
|
// Generate a unique id for the chat if not provided.
|
|
271
277
|
const hookId = useId();
|
|
272
278
|
const idKey = id ?? hookId;
|
|
@@ -344,7 +350,7 @@ By default, it's set to 0, which will disable the feature.
|
|
|
344
350
|
messagesRef,
|
|
345
351
|
abortControllerRef,
|
|
346
352
|
generateId,
|
|
347
|
-
|
|
353
|
+
streamProtocol,
|
|
348
354
|
onFinish,
|
|
349
355
|
onResponse,
|
|
350
356
|
onToolCall,
|
|
@@ -407,7 +413,7 @@ By default, it's set to 0, which will disable the feature.
|
|
|
407
413
|
setError,
|
|
408
414
|
mutateStreamData,
|
|
409
415
|
streamData,
|
|
410
|
-
|
|
416
|
+
streamProtocol,
|
|
411
417
|
sendExtraMessageFields,
|
|
412
418
|
experimental_onFunctionCall,
|
|
413
419
|
experimental_onToolCall,
|
package/src/use-chat.ui.test.tsx
CHANGED
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
/* eslint-disable @next/next/no-img-element */
|
|
2
2
|
import { withTestServer } from '@ai-sdk/provider-utils/test';
|
|
3
|
-
import {
|
|
3
|
+
import {
|
|
4
|
+
formatStreamPart,
|
|
5
|
+
getTextFromDataUrl,
|
|
6
|
+
Message,
|
|
7
|
+
} from '@ai-sdk/ui-utils';
|
|
4
8
|
import '@testing-library/jest-dom/vitest';
|
|
5
9
|
import {
|
|
6
10
|
cleanup,
|
|
@@ -14,9 +18,26 @@ import React, { useRef, useState } from 'react';
|
|
|
14
18
|
import { useChat } from './use-chat';
|
|
15
19
|
|
|
16
20
|
describe('stream data stream', () => {
|
|
21
|
+
let onFinishCalls: Array<{
|
|
22
|
+
message: Message;
|
|
23
|
+
options: {
|
|
24
|
+
finishReason: string;
|
|
25
|
+
usage: {
|
|
26
|
+
completionTokens: number;
|
|
27
|
+
promptTokens: number;
|
|
28
|
+
totalTokens: number;
|
|
29
|
+
};
|
|
30
|
+
};
|
|
31
|
+
}> = [];
|
|
32
|
+
|
|
17
33
|
const TestComponent = () => {
|
|
18
34
|
const [id, setId] = React.useState<string>('first-id');
|
|
19
|
-
const { messages, append, error, data, isLoading } = useChat({
|
|
35
|
+
const { messages, append, error, data, isLoading } = useChat({
|
|
36
|
+
id,
|
|
37
|
+
onFinish: (message, options) => {
|
|
38
|
+
onFinishCalls.push({ message, options });
|
|
39
|
+
},
|
|
40
|
+
});
|
|
20
41
|
|
|
21
42
|
return (
|
|
22
43
|
<div>
|
|
@@ -48,11 +69,13 @@ describe('stream data stream', () => {
|
|
|
48
69
|
|
|
49
70
|
beforeEach(() => {
|
|
50
71
|
render(<TestComponent />);
|
|
72
|
+
onFinishCalls = [];
|
|
51
73
|
});
|
|
52
74
|
|
|
53
75
|
afterEach(() => {
|
|
54
76
|
vi.restoreAllMocks();
|
|
55
77
|
cleanup();
|
|
78
|
+
onFinishCalls = [];
|
|
56
79
|
});
|
|
57
80
|
|
|
58
81
|
it(
|
|
@@ -147,6 +170,50 @@ describe('stream data stream', () => {
|
|
|
147
170
|
);
|
|
148
171
|
});
|
|
149
172
|
|
|
173
|
+
it(
|
|
174
|
+
'should invoke onFinish when the stream finishes',
|
|
175
|
+
withTestServer(
|
|
176
|
+
{
|
|
177
|
+
url: '/api/chat',
|
|
178
|
+
type: 'stream-values',
|
|
179
|
+
content: [
|
|
180
|
+
formatStreamPart('text', 'Hello'),
|
|
181
|
+
formatStreamPart('text', ','),
|
|
182
|
+
formatStreamPart('text', ' world'),
|
|
183
|
+
formatStreamPart('text', '.'),
|
|
184
|
+
formatStreamPart('finish_message', {
|
|
185
|
+
finishReason: 'stop',
|
|
186
|
+
usage: { completionTokens: 1, promptTokens: 3 },
|
|
187
|
+
}),
|
|
188
|
+
],
|
|
189
|
+
},
|
|
190
|
+
async () => {
|
|
191
|
+
await userEvent.click(screen.getByTestId('do-append'));
|
|
192
|
+
|
|
193
|
+
await screen.findByTestId('message-1');
|
|
194
|
+
|
|
195
|
+
expect(onFinishCalls).toStrictEqual([
|
|
196
|
+
{
|
|
197
|
+
message: {
|
|
198
|
+
id: expect.any(String),
|
|
199
|
+
createdAt: expect.any(Date),
|
|
200
|
+
role: 'assistant',
|
|
201
|
+
content: 'Hello, world.',
|
|
202
|
+
},
|
|
203
|
+
options: {
|
|
204
|
+
finishReason: 'stop',
|
|
205
|
+
usage: {
|
|
206
|
+
completionTokens: 1,
|
|
207
|
+
promptTokens: 3,
|
|
208
|
+
totalTokens: 4,
|
|
209
|
+
},
|
|
210
|
+
},
|
|
211
|
+
},
|
|
212
|
+
]);
|
|
213
|
+
},
|
|
214
|
+
),
|
|
215
|
+
);
|
|
216
|
+
|
|
150
217
|
describe('id', () => {
|
|
151
218
|
it(
|
|
152
219
|
'should clear out messages when the id changes',
|
|
@@ -174,8 +241,25 @@ describe('stream data stream', () => {
|
|
|
174
241
|
});
|
|
175
242
|
|
|
176
243
|
describe('text stream', () => {
|
|
244
|
+
let onFinishCalls: Array<{
|
|
245
|
+
message: Message;
|
|
246
|
+
options: {
|
|
247
|
+
finishReason: string;
|
|
248
|
+
usage: {
|
|
249
|
+
completionTokens: number;
|
|
250
|
+
promptTokens: number;
|
|
251
|
+
totalTokens: number;
|
|
252
|
+
};
|
|
253
|
+
};
|
|
254
|
+
}> = [];
|
|
255
|
+
|
|
177
256
|
const TestComponent = () => {
|
|
178
|
-
const { messages, append } = useChat({
|
|
257
|
+
const { messages, append } = useChat({
|
|
258
|
+
streamProtocol: 'text',
|
|
259
|
+
onFinish: (message, options) => {
|
|
260
|
+
onFinishCalls.push({ message, options });
|
|
261
|
+
},
|
|
262
|
+
});
|
|
179
263
|
|
|
180
264
|
return (
|
|
181
265
|
<div>
|
|
@@ -198,11 +282,13 @@ describe('text stream', () => {
|
|
|
198
282
|
|
|
199
283
|
beforeEach(() => {
|
|
200
284
|
render(<TestComponent />);
|
|
285
|
+
onFinishCalls = [];
|
|
201
286
|
});
|
|
202
287
|
|
|
203
288
|
afterEach(() => {
|
|
204
289
|
vi.restoreAllMocks();
|
|
205
290
|
cleanup();
|
|
291
|
+
onFinishCalls = [];
|
|
206
292
|
});
|
|
207
293
|
|
|
208
294
|
it(
|
|
@@ -228,12 +314,47 @@ describe('text stream', () => {
|
|
|
228
314
|
},
|
|
229
315
|
),
|
|
230
316
|
);
|
|
317
|
+
|
|
318
|
+
it(
|
|
319
|
+
'should invoke onFinish when the stream finishes',
|
|
320
|
+
withTestServer(
|
|
321
|
+
{
|
|
322
|
+
url: '/api/chat',
|
|
323
|
+
type: 'stream-values',
|
|
324
|
+
content: ['Hello', ',', ' world', '.'],
|
|
325
|
+
},
|
|
326
|
+
async () => {
|
|
327
|
+
await userEvent.click(screen.getByTestId('do-append-text-stream'));
|
|
328
|
+
|
|
329
|
+
await screen.findByTestId('message-1-text-stream');
|
|
330
|
+
|
|
331
|
+
expect(onFinishCalls).toStrictEqual([
|
|
332
|
+
{
|
|
333
|
+
message: {
|
|
334
|
+
id: expect.any(String),
|
|
335
|
+
createdAt: expect.any(Date),
|
|
336
|
+
role: 'assistant',
|
|
337
|
+
content: 'Hello, world.',
|
|
338
|
+
},
|
|
339
|
+
options: {
|
|
340
|
+
finishReason: 'unknown',
|
|
341
|
+
usage: {
|
|
342
|
+
completionTokens: NaN,
|
|
343
|
+
promptTokens: NaN,
|
|
344
|
+
totalTokens: NaN,
|
|
345
|
+
},
|
|
346
|
+
},
|
|
347
|
+
},
|
|
348
|
+
]);
|
|
349
|
+
},
|
|
350
|
+
),
|
|
351
|
+
);
|
|
231
352
|
});
|
|
232
353
|
|
|
233
354
|
describe('form actions', () => {
|
|
234
355
|
const TestComponent = () => {
|
|
235
356
|
const { messages, handleSubmit, handleInputChange, isLoading, input } =
|
|
236
|
-
useChat({
|
|
357
|
+
useChat({ streamProtocol: 'text' });
|
|
237
358
|
|
|
238
359
|
return (
|
|
239
360
|
<div>
|
|
@@ -306,7 +427,7 @@ describe('form actions', () => {
|
|
|
306
427
|
describe('form actions (with options)', () => {
|
|
307
428
|
const TestComponent = () => {
|
|
308
429
|
const { messages, handleSubmit, handleInputChange, isLoading, input } =
|
|
309
|
-
useChat({
|
|
430
|
+
useChat({ streamProtocol: 'text' });
|
|
310
431
|
|
|
311
432
|
return (
|
|
312
433
|
<div>
|
package/src/use-completion.ts
CHANGED
|
@@ -72,11 +72,17 @@ export function useCompletion({
|
|
|
72
72
|
headers,
|
|
73
73
|
body,
|
|
74
74
|
streamMode,
|
|
75
|
+
streamProtocol,
|
|
75
76
|
fetch,
|
|
76
77
|
onResponse,
|
|
77
78
|
onFinish,
|
|
78
79
|
onError,
|
|
79
80
|
}: UseCompletionOptions = {}): UseCompletionHelpers {
|
|
81
|
+
// streamMode is deprecated, use streamProtocol instead.
|
|
82
|
+
if (streamMode) {
|
|
83
|
+
streamProtocol ??= streamMode === 'text' ? 'text' : undefined;
|
|
84
|
+
}
|
|
85
|
+
|
|
80
86
|
// Generate an unique id for the completion if not provided.
|
|
81
87
|
const hookId = useId();
|
|
82
88
|
const completionId = id || hookId;
|
|
@@ -126,7 +132,7 @@ export function useCompletion({
|
|
|
126
132
|
...extraMetadataRef.current.body,
|
|
127
133
|
...options?.body,
|
|
128
134
|
},
|
|
129
|
-
|
|
135
|
+
streamProtocol,
|
|
130
136
|
fetch,
|
|
131
137
|
setCompletion: completion => mutate(completion, false),
|
|
132
138
|
setLoading: mutateLoading,
|
|
@@ -150,7 +156,7 @@ export function useCompletion({
|
|
|
150
156
|
onError,
|
|
151
157
|
setError,
|
|
152
158
|
streamData,
|
|
153
|
-
|
|
159
|
+
streamProtocol,
|
|
154
160
|
fetch,
|
|
155
161
|
mutateStreamData,
|
|
156
162
|
],
|
|
@@ -124,9 +124,7 @@ describe('stream data stream', () => {
|
|
|
124
124
|
describe('text stream', () => {
|
|
125
125
|
const TestComponent = () => {
|
|
126
126
|
const { completion, handleSubmit, handleInputChange, input } =
|
|
127
|
-
useCompletion({
|
|
128
|
-
streamMode: 'text',
|
|
129
|
-
});
|
|
127
|
+
useCompletion({ streamProtocol: 'text' });
|
|
130
128
|
|
|
131
129
|
return (
|
|
132
130
|
<div>
|