@ai-sdk/vue 0.0.20 → 0.0.22

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.
@@ -1,5 +1,5 @@
1
1
 
2
- > @ai-sdk/vue@0.0.20 build /home/runner/work/ai/ai/packages/vue
2
+ > @ai-sdk/vue@0.0.22 build /home/runner/work/ai/ai/packages/vue
3
3
  > tsup
4
4
 
5
5
  CLI Building entry: src/index.ts
@@ -9,13 +9,13 @@
9
9
  CLI Target: es2018
10
10
  CJS Build start
11
11
  ESM Build start
12
- CJS dist/index.js 16.19 KB
13
- CJS dist/index.js.map 30.75 KB
14
- CJS ⚡️ Build success in 41ms
15
- ESM dist/index.mjs 14.07 KB
16
- ESM dist/index.mjs.map 30.53 KB
17
- ESM ⚡️ Build success in 41ms
12
+ ESM dist/index.mjs 13.89 KB
13
+ ESM dist/index.mjs.map 30.60 KB
14
+ ESM ⚡️ Build success in 47ms
15
+ CJS dist/index.js 16.02 KB
16
+ CJS dist/index.js.map 30.82 KB
17
+ CJS ⚡️ Build success in 48ms
18
18
  DTS Build start
19
- DTS ⚡️ Build success in 5457ms
20
- DTS dist/index.d.ts 5.86 KB
21
- DTS dist/index.d.mts 5.86 KB
19
+ DTS ⚡️ Build success in 5106ms
20
+ DTS dist/index.d.ts 5.89 KB
21
+ DTS dist/index.d.mts 5.89 KB
@@ -1,4 +1,4 @@
1
1
 
2
- > @ai-sdk/vue@0.0.20 clean /home/runner/work/ai/ai/packages/vue
2
+ > @ai-sdk/vue@0.0.22 clean /home/runner/work/ai/ai/packages/vue
3
3
  > rm -rf dist
4
4
 
package/CHANGELOG.md CHANGED
@@ -1,5 +1,21 @@
1
1
  # @ai-sdk/vue
2
2
 
3
+ ## 0.0.22
4
+
5
+ ### Patch Changes
6
+
7
+ - @ai-sdk/provider-utils@1.0.3
8
+ - @ai-sdk/ui-utils@0.0.18
9
+
10
+ ## 0.0.21
11
+
12
+ ### Patch Changes
13
+
14
+ - f63829fe: feat (ai/ui): add allowEmptySubmit flag to handleSubmit
15
+ - 4b2c09d9: feat (ai/ui): add mutator function support to useChat / setMessages
16
+ - Updated dependencies [f63829fe]
17
+ - @ai-sdk/ui-utils@0.0.17
18
+
3
19
  ## 0.0.20
4
20
 
5
21
  ### Patch Changes
package/dist/index.d.mts CHANGED
@@ -27,7 +27,7 @@ type UseChatHelpers = {
27
27
  * edit the messages on the client, and then trigger the `reload` method
28
28
  * manually to regenerate the AI response.
29
29
  */
30
- setMessages: (messages: Message[]) => void;
30
+ setMessages: (messages: Message[] | ((messages: Message[]) => Message[])) => void;
31
31
  /** The current value of the input */
32
32
  input: Ref<string>;
33
33
  /** Form submission handler to automatically reset input and append a user message */
package/dist/index.d.ts CHANGED
@@ -27,7 +27,7 @@ type UseChatHelpers = {
27
27
  * edit the messages on the client, and then trigger the `reload` method
28
28
  * manually to regenerate the AI response.
29
29
  */
30
- setMessages: (messages: Message[]) => void;
30
+ setMessages: (messages: Message[] | ((messages: Message[]) => Message[])) => void;
31
31
  /** The current value of the input */
32
32
  input: Ref<string>;
33
33
  /** Form submission handler to automatically reset input and append a user message */
package/dist/index.js CHANGED
@@ -180,14 +180,9 @@ function useChat({
180
180
  }
181
181
  }
182
182
  const append = async (message, options) => {
183
- var _a2, _b2, _c, _d;
184
183
  if (!message.id) {
185
184
  message.id = generateId2();
186
185
  }
187
- const requestOptions = {
188
- headers: (_b2 = options == null ? void 0 : options.headers) != null ? _b2 : (_a2 = options == null ? void 0 : options.options) == null ? void 0 : _a2.headers,
189
- body: (_d = options == null ? void 0 : options.body) != null ? _d : (_c = options == null ? void 0 : options.options) == null ? void 0 : _c.body
190
- };
191
186
  return triggerRequest(messages.value.concat(message), options);
192
187
  };
193
188
  const reload = async (options) => {
@@ -206,21 +201,26 @@ function useChat({
206
201
  abortController = null;
207
202
  }
208
203
  };
209
- const setMessages = (messages2) => {
210
- mutate(messages2);
204
+ const setMessages = (messagesArg) => {
205
+ if (typeof messagesArg === "function") {
206
+ messagesArg = messagesArg(messages.value);
207
+ }
208
+ mutate(messagesArg);
211
209
  };
212
210
  const input = (0, import_vue.ref)(initialInput);
213
211
  const handleSubmit = (event, options = {}) => {
214
212
  var _a2;
215
213
  (_a2 = event == null ? void 0 : event.preventDefault) == null ? void 0 : _a2.call(event);
216
214
  const inputValue = input.value;
215
+ if (!inputValue && !options.allowEmptySubmit)
216
+ return;
217
217
  triggerRequest(
218
- inputValue ? messages.value.concat({
218
+ !inputValue && options.allowEmptySubmit ? messages.value : messages.value.concat({
219
219
  id: generateId2(),
220
220
  createdAt: /* @__PURE__ */ new Date(),
221
221
  content: inputValue,
222
222
  role: "user"
223
- }) : messages.value,
223
+ }),
224
224
  options
225
225
  );
226
226
  input.value = "";
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/index.ts","../src/use-chat.ts","../src/use-completion.ts","../src/use-assistant.ts"],"sourcesContent":["export * from './use-chat';\nexport * from './use-completion';\nexport * from './use-assistant';\n","import type {\n ChatRequest,\n ChatRequestOptions,\n CreateMessage,\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 swrv from 'swrv';\nimport type { Ref } from 'vue';\nimport { ref, unref } from 'vue';\n\nexport type { CreateMessage, Message, UseChatOptions };\n\nexport type UseChatHelpers = {\n /** Current messages in the chat */\n messages: Ref<Message[]>;\n /** The error object of the API request */\n error: Ref<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 */\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: (messages: Message[]) => void;\n /** The current value of the input */\n input: Ref<string>;\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 /** Whether the API request is in progress */\n isLoading: Ref<boolean | undefined>;\n\n /** Additional data added on the server via StreamData */\n data: Ref<JSONValue[] | undefined>;\n};\n\nlet uniqueId = 0;\n\n// @ts-expect-error - some issues with the default export of useSWRV\nconst useSWRV = (swrv.default as typeof import('swrv')['default']) || swrv;\nconst store: Record<string, Message[] | undefined> = {};\n\nexport function useChat({\n api = '/api/chat',\n id,\n initialMessages = [],\n initialInput = '',\n sendExtraMessageFields,\n experimental_onFunctionCall,\n streamMode,\n onResponse,\n onFinish,\n onError,\n credentials,\n headers,\n body,\n generateId = generateIdFunc,\n fetch,\n keepLastMessageOnError = false,\n}: UseChatOptions = {}): UseChatHelpers {\n // Generate a unique ID for the chat if not provided.\n const chatId = id || `chat-${uniqueId++}`;\n\n const key = `${api}|${chatId}`;\n const { data: messagesData, mutate: originalMutate } = useSWRV<Message[]>(\n key,\n () => store[key] || initialMessages,\n );\n\n const { data: isLoading, mutate: mutateLoading } = useSWRV<boolean>(\n `${chatId}-loading`,\n null,\n );\n\n isLoading.value ??= false;\n\n // Force the `data` to be `initialMessages` if it's `undefined`.\n messagesData.value ??= initialMessages;\n\n const mutate = (data?: Message[]) => {\n store[key] = data;\n return originalMutate();\n };\n\n // Because of the `initialData` option, the `data` will never be `undefined`.\n const messages = messagesData as Ref<Message[]>;\n\n const error = ref<undefined | Error>(undefined);\n // cannot use JSONValue[] in ref because of infinite Typescript recursion:\n const streamData = ref<undefined | unknown[]>(undefined);\n\n let abortController: AbortController | null = null;\n async function triggerRequest(\n messagesSnapshot: Message[],\n { options, data, headers, body }: ChatRequestOptions = {},\n ) {\n try {\n error.value = undefined;\n mutateLoading(() => true);\n\n abortController = new AbortController();\n\n // Do an optimistic update to the chat state to show the updated messages\n // immediately.\n const previousMessages = messagesSnapshot;\n mutate(messagesSnapshot);\n\n const requestOptions = {\n headers: headers ?? options?.headers,\n body: body ?? options?.body,\n };\n\n let chatRequest: ChatRequest = {\n messages: messagesSnapshot,\n options: requestOptions,\n body: requestOptions.body,\n headers: requestOptions.headers,\n data,\n };\n\n await processChatStream({\n getStreamedResponse: async () => {\n const existingData = (streamData.value ?? []) as JSONValue[];\n\n const constructedMessagesPayload = sendExtraMessageFields\n ? chatRequest.messages\n : chatRequest.messages.map(\n ({\n role,\n content,\n name,\n data,\n annotations,\n function_call,\n }) => ({\n role,\n content,\n ...(name !== undefined && { name }),\n ...(data !== undefined && { data }),\n ...(annotations !== undefined && { annotations }),\n // outdated function/tool call handling (TODO deprecate):\n ...(function_call !== undefined && { function_call }),\n }),\n );\n\n return await callChatApi({\n api,\n body: {\n messages: constructedMessagesPayload,\n data: chatRequest.data,\n ...unref(body), // Use unref to unwrap the ref value\n ...requestOptions.body,\n },\n streamMode,\n headers: {\n ...headers,\n ...requestOptions.headers,\n },\n abortController: () => abortController,\n credentials,\n onResponse,\n onUpdate(merged, data) {\n mutate([...chatRequest.messages, ...merged]);\n streamData.value = [...existingData, ...(data ?? [])];\n },\n onFinish(message) {\n // workaround: sometimes the last chunk is not shown in the UI.\n // push it twice to make sure it's displayed.\n mutate([...chatRequest.messages, message]);\n onFinish?.(message);\n },\n restoreMessagesOnFailure() {\n // Restore the previous messages if the request fails.\n if (!keepLastMessageOnError) {\n mutate(previousMessages);\n }\n },\n generateId,\n onToolCall: undefined, // not implemented yet\n fetch,\n });\n },\n experimental_onFunctionCall,\n updateChatRequest(newChatRequest) {\n chatRequest = newChatRequest;\n },\n getCurrentMessages: () => messages.value,\n });\n\n abortController = null;\n } catch (err) {\n // Ignore abort errors as they are expected.\n if ((err as any).name === 'AbortError') {\n abortController = null;\n return null;\n }\n\n if (onError && err instanceof Error) {\n onError(err);\n }\n\n error.value = err as Error;\n } finally {\n mutateLoading(() => false);\n }\n }\n\n const append: UseChatHelpers['append'] = async (message, options) => {\n if (!message.id) {\n message.id = generateId();\n }\n\n const requestOptions = {\n headers: options?.headers ?? options?.options?.headers,\n body: options?.body ?? options?.options?.body,\n };\n\n return triggerRequest(messages.value.concat(message as Message), options);\n };\n\n const reload: UseChatHelpers['reload'] = async options => {\n const messagesSnapshot = messages.value;\n if (messagesSnapshot.length === 0) return null;\n\n const lastMessage = messagesSnapshot[messagesSnapshot.length - 1];\n if (lastMessage.role === 'assistant') {\n return triggerRequest(messagesSnapshot.slice(0, -1), options);\n }\n return triggerRequest(messagesSnapshot, options);\n };\n\n const stop = () => {\n if (abortController) {\n abortController.abort();\n abortController = null;\n }\n };\n\n const setMessages = (messages: Message[]) => {\n mutate(messages);\n };\n\n const input = ref(initialInput);\n\n const handleSubmit = (\n event?: { preventDefault?: () => void },\n options: ChatRequestOptions = {},\n ) => {\n event?.preventDefault?.();\n\n const inputValue = input.value;\n\n triggerRequest(\n inputValue\n ? messages.value.concat({\n id: generateId(),\n createdAt: new Date(),\n content: inputValue,\n role: 'user',\n })\n : messages.value,\n options,\n );\n\n input.value = '';\n };\n\n return {\n messages,\n append,\n error,\n reload,\n stop,\n setMessages,\n input,\n handleSubmit,\n isLoading,\n data: streamData as Ref<undefined | JSONValue[]>,\n };\n}\n","import type {\n JSONValue,\n RequestOptions,\n UseCompletionOptions,\n} from '@ai-sdk/ui-utils';\nimport { callCompletionApi } from '@ai-sdk/ui-utils';\nimport swrv from 'swrv';\nimport type { Ref } from 'vue';\nimport { ref, unref } from 'vue';\n\nexport type { UseCompletionOptions };\n\nexport type UseCompletionHelpers = {\n /** The current completion result */\n completion: Ref<string>;\n /** The error object of the API request */\n error: Ref<undefined | Error>;\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 /**\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: Ref<string>;\n /**\n * Form submission handler to automatically reset input and append a user message\n * @example\n * ```jsx\n * <form @submit=\"handleSubmit\">\n * <input @change=\"handleInputChange\" v-model=\"input\" />\n * </form>\n * ```\n */\n handleSubmit: (event?: { preventDefault?: () => void }) => void;\n /** Whether the API request is in progress */\n isLoading: Ref<boolean | undefined>;\n\n /** Additional data added on the server via StreamData */\n data: Ref<JSONValue[] | undefined>;\n};\n\nlet uniqueId = 0;\n\n// @ts-expect-error - some issues with the default export of useSWRV\nconst useSWRV = (swrv.default as typeof import('swrv')['default']) || swrv;\nconst store: Record<string, any> = {};\n\nexport function useCompletion({\n api = '/api/completion',\n id,\n initialCompletion = '',\n initialInput = '',\n credentials,\n headers,\n body,\n streamMode,\n onResponse,\n onFinish,\n onError,\n fetch,\n}: UseCompletionOptions = {}): UseCompletionHelpers {\n // Generate an unique id for the completion if not provided.\n const completionId = id || `completion-${uniqueId++}`;\n\n const key = `${api}|${completionId}`;\n const { data, mutate: originalMutate } = useSWRV<string>(\n key,\n () => store[key] || initialCompletion,\n );\n\n const { data: isLoading, mutate: mutateLoading } = useSWRV<boolean>(\n `${completionId}-loading`,\n null,\n );\n\n isLoading.value ??= false;\n\n const { data: streamData, mutate: mutateStreamData } = useSWRV<\n JSONValue[] | undefined\n >(`${completionId}-data`, null);\n\n // Force the `data` to be `initialCompletion` if it's `undefined`.\n data.value ||= initialCompletion;\n\n const mutate = (data: string) => {\n store[key] = data;\n return originalMutate();\n };\n\n // Because of the `initialData` option, the `data` will never be `undefined`.\n const completion = data as Ref<string>;\n\n const error = ref<undefined | Error>(undefined);\n\n let abortController: AbortController | null = null;\n\n async function triggerRequest(prompt: string, options?: RequestOptions) {\n const existingData = (streamData.value ?? []) as JSONValue[];\n return callCompletionApi({\n api,\n prompt,\n credentials,\n headers: {\n ...headers,\n ...options?.headers,\n },\n body: {\n ...unref(body),\n ...options?.body,\n },\n streamMode,\n setCompletion: mutate,\n setLoading: loading => mutateLoading(() => loading),\n setError: err => {\n error.value = err;\n },\n setAbortController: controller => {\n abortController = controller;\n },\n onResponse,\n onFinish,\n onError,\n onData: data => {\n mutateStreamData(() => [...existingData, ...(data ?? [])]);\n },\n fetch,\n });\n }\n\n const complete: UseCompletionHelpers['complete'] = async (\n prompt,\n options,\n ) => {\n return triggerRequest(prompt, options);\n };\n\n const stop = () => {\n if (abortController) {\n abortController.abort();\n abortController = null;\n }\n };\n\n const setCompletion = (completion: string) => {\n mutate(completion);\n };\n\n const input = ref(initialInput);\n\n const handleSubmit = (event?: { preventDefault?: () => void }) => {\n event?.preventDefault?.();\n const inputValue = input.value;\n return inputValue ? complete(inputValue) : undefined;\n };\n\n return {\n completion,\n complete,\n error,\n stop,\n setCompletion,\n input,\n handleSubmit,\n isLoading,\n data: streamData,\n };\n}\n","/**\n * A vue.js composable function to interact with the assistant API.\n */\n\nimport { isAbortError } from '@ai-sdk/provider-utils';\nimport { readDataStream, generateId } from '@ai-sdk/ui-utils';\nimport type {\n AssistantStatus,\n CreateMessage,\n Message,\n UseAssistantOptions,\n} from '@ai-sdk/ui-utils';\nimport { computed, readonly, ref } from 'vue';\nimport type { ComputedRef, Ref } from 'vue';\n\nexport type UseAssistantHelpers = {\n /**\n * The current array of chat messages.\n */\n messages: Ref<Message[]>;\n\n /**\n * Update the message store with a new array of messages.\n */\n setMessages: (messagesProcessor: (messages: Message[]) => Message[]) => void;\n\n /**\n * The current thread ID.\n */\n threadId: Ref<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 * The current value of the input field.\n */\n input: Ref<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 /**\n * Abort the current request immediately, keep the generated tokens if any.\n */\n stop: ComputedRef<() => void>;\n\n /**\n * Handler for the `onChange` event of the input field to control the input's value.\n */\n handleInputChange: (e: Event & { target: HTMLInputElement }) => void;\n\n /**\n * Handler for the `onSubmit` event of the form to append a user message and reset the input.\n */\n handleSubmit: (e: Event & { target: HTMLFormElement }) => void;\n\n /**\n * Whether the assistant is currently sending a message.\n */\n isSending: ComputedRef<boolean>;\n\n /**\n * The current status of the assistant.\n */\n status: Ref<AssistantStatus>;\n\n /**\n * The current error, if any.\n */\n error: Ref<Error | undefined>;\n};\n\nexport function useAssistant({\n api,\n threadId: threadIdParam,\n credentials,\n headers,\n body,\n onError,\n}: UseAssistantOptions): UseAssistantHelpers {\n const messages: Ref<Message[]> = ref([]);\n const input: Ref<string> = ref('');\n const currentThreadId: Ref<string | undefined> = ref(undefined);\n const status: Ref<AssistantStatus> = ref('awaiting_message');\n const error: Ref<undefined | Error> = ref(undefined);\n\n const setMessages = (messageFactory: (messages: Message[]) => Message[]) => {\n messages.value = messageFactory(messages.value);\n };\n\n const setCurrentThreadId = (newThreadId: string | undefined) => {\n currentThreadId.value = newThreadId;\n messages.value = [];\n };\n\n const handleInputChange = (event: Event & { target: HTMLInputElement }) => {\n input.value = event?.target?.value;\n };\n\n const isSending = computed(() => status.value === 'in_progress');\n\n // Abort controller to cancel the current API call when required\n const abortController = ref<AbortController | null>(null);\n\n // memoized function to stop the current request when required\n const stop = computed(() => {\n return () => {\n if (abortController.value) {\n abortController.value.abort();\n abortController.value = null;\n }\n };\n });\n\n const append = async (\n message: Message | CreateMessage,\n requestOptions?: {\n data?: Record<string, string>;\n },\n ) => {\n status.value = 'in_progress';\n\n // Append the new message to the current list of messages\n const newMessage: Message = {\n ...message,\n id: message.id ?? generateId(),\n };\n\n // Update the messages list with the new message\n setMessages(messages => [...messages, newMessage]);\n\n input.value = '';\n\n const controller = new AbortController();\n\n try {\n // Assign the new controller to the abortController ref\n abortController.value = controller;\n\n const response = await fetch(api, {\n method: 'POST',\n headers: {\n 'Content-Type': 'application/json',\n ...headers,\n },\n body: JSON.stringify({\n ...body,\n // Message Content\n message: message.content,\n\n // Always Use User Provided Thread ID When Available\n threadId: threadIdParam ?? currentThreadId.value ?? null,\n\n // Optional Request Data\n ...(requestOptions?.data && { data: requestOptions?.data }),\n }),\n signal: controller.signal,\n credentials,\n });\n\n if (!response.ok) {\n throw new Error(\n response.statusText ?? 'An error occurred while sending the message',\n );\n }\n\n if (!response.body) {\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 messages.value = [\n ...messages.value,\n {\n id: value.id,\n content: value.content[0].text.value,\n role: value.role,\n },\n ];\n break;\n }\n case 'assistant_control_data': {\n if (value.threadId) {\n currentThreadId.value = value.threadId;\n }\n\n setMessages(messages => {\n const lastMessage = messages[messages.length - 1];\n lastMessage.id = value.messageId;\n\n return [...messages.slice(0, -1), lastMessage];\n });\n\n break;\n }\n\n case 'text': {\n setMessages(messages => {\n const lastMessage = messages[messages.length - 1];\n lastMessage.content += value;\n\n return [...messages.slice(0, -1), lastMessage];\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 'error': {\n error.value = new Error(value);\n }\n\n default: {\n console.error('Unknown message type:', type);\n break;\n }\n }\n }\n } catch (err) {\n // If the error is an AbortError and the signal is aborted, reset the abortController and do nothing.\n if (isAbortError(err) && abortController.value?.signal.aborted) {\n abortController.value = null;\n return;\n }\n\n // If an error handler is provided, call it with the error\n if (onError && err instanceof Error) {\n onError(err);\n }\n\n error.value = err as Error;\n } finally {\n // Reset the status to 'awaiting_message' after the request is complete\n abortController.value = null;\n status.value = 'awaiting_message';\n }\n };\n\n const submitMessage = async (\n event: Event & { target: HTMLFormElement },\n requestOptions?: {\n data?: Record<string, string>;\n },\n ) => {\n event?.preventDefault?.();\n\n if (!input.value) return;\n\n append(\n {\n role: 'user',\n content: input.value,\n },\n requestOptions,\n );\n };\n\n return {\n append,\n messages,\n setMessages,\n threadId: readonly(currentThreadId),\n setThreadId: setCurrentThreadId,\n input,\n handleInputChange,\n handleSubmit: submitMessage,\n isSending,\n status,\n error,\n stop,\n };\n}\n\n/**\n * @deprecated Use `useAssistant` instead.\n */\nexport const experimental_useAssistant = useAssistant;\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACQA,sBAIO;AACP,kBAAiB;AAEjB,iBAA2B;AAiD3B,IAAI,WAAW;AAGf,IAAM,UAAW,YAAAA,QAAK,WAAgD,YAAAA;AACtE,IAAM,QAA+C,CAAC;AAE/C,SAAS,QAAQ;AAAA,EACtB,MAAM;AAAA,EACN;AAAA,EACA,kBAAkB,CAAC;AAAA,EACnB,eAAe;AAAA,EACf;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA,YAAAC,cAAa,gBAAAC;AAAA,EACb,OAAAC;AAAA,EACA,yBAAyB;AAC3B,IAAoB,CAAC,GAAmB;AAvFxC;AAyFE,QAAM,SAAS,MAAM,QAAQ,UAAU;AAEvC,QAAM,MAAM,GAAG,GAAG,IAAI,MAAM;AAC5B,QAAM,EAAE,MAAM,cAAc,QAAQ,eAAe,IAAI;AAAA,IACrD;AAAA,IACA,MAAM,MAAM,GAAG,KAAK;AAAA,EACtB;AAEA,QAAM,EAAE,MAAM,WAAW,QAAQ,cAAc,IAAI;AAAA,IACjD,GAAG,MAAM;AAAA,IACT;AAAA,EACF;AAEA,kBAAU,UAAV,sBAAU,QAAU;AAGpB,qBAA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ACH;AAAA,kBACE,KAAIC,MAAA,MAAM,OAAN,OAAAA,UAAY,6BAAW;AAAA,kBAC3B,MAAM;AAAA,kBACN,SAAS;AAAA,kBACT,MAAM,MAAM;AAAA,gBACd;AAAA,cACF;AAAA,aAAC;AACD;AAAA,UACF;AAAA,UAEA,KAAK,SAAS;AACZ,kBAAM,QAAQ,IAAI,MAAM,KAAK;AAAA,UAC/B;AAAA,UAEA,SAAS;AACP,oBAAQ,MAAM,yBAAyB,IAAI;AAC3C;AAAA,UACF;AAAA,QACF;AAAA,MACF;AAAA,IACF,SAAS,KAAK;AAEZ,cAAI,oCAAa,GAAG,OAAK,qBAAgB,UAAhB,mBAAuB,OAAO,UAAS;AAC9D,wBAAgB,QAAQ;AACxB;AAAA,MACF;AAGA,UAAI,WAAW,eAAe,OAAO;AACnC,gBAAQ,GAAG;AAAA,MACb;AAEA,YAAM,QAAQ;AAAA,IAChB,UAAE;AAEA,sBAAgB,QAAQ;AACxB,aAAO,QAAQ;AAAA,IACjB;AAAA,EACF;AAEA,QAAM,gBAAgB,OACpB,OACA,mBAGG;AA/QP;AAgRI,yCAAO,mBAAP;AAEA,QAAI,CAAC,MAAM;AAAO;AAElB;AAAA,MACE;AAAA,QACE,MAAM;AAAA,QACN,SAAS,MAAM;AAAA,MACjB;AAAA,MACA;AAAA,IACF;AAAA,EACF;AAEA,SAAO;AAAA,IACL;AAAA,IACA;AAAA,IACA;AAAA,IACA,cAAU,sBAAS,eAAe;AAAA,IAClC,aAAa;AAAA,IACb;AAAA,IACA;AAAA,IACA,cAAc;AAAA,IACd;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF;AACF;AAKO,IAAM,4BAA4B;","names":["swrv","generateId","generateIdFunc","fetch","headers","body","_a","data","_b","messages","import_ui_utils","import_swrv","import_vue","uniqueId","useSWRV","swrv","store","fetch","data","_a","completion","import_ui_utils","import_vue","messages","_a"]}
1
+ {"version":3,"sources":["../src/index.ts","../src/use-chat.ts","../src/use-completion.ts","../src/use-assistant.ts"],"sourcesContent":["export * from './use-chat';\nexport * from './use-completion';\nexport * from './use-assistant';\n","import type {\n ChatRequest,\n ChatRequestOptions,\n CreateMessage,\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 swrv from 'swrv';\nimport type { Ref } from 'vue';\nimport { ref, unref } from 'vue';\n\nexport type { CreateMessage, Message, UseChatOptions };\n\nexport type UseChatHelpers = {\n /** Current messages in the chat */\n messages: Ref<Message[]>;\n /** The error object of the API request */\n error: Ref<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 */\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: Ref<string>;\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 /** Whether the API request is in progress */\n isLoading: Ref<boolean | undefined>;\n\n /** Additional data added on the server via StreamData */\n data: Ref<JSONValue[] | undefined>;\n};\n\nlet uniqueId = 0;\n\n// @ts-expect-error - some issues with the default export of useSWRV\nconst useSWRV = (swrv.default as typeof import('swrv')['default']) || swrv;\nconst store: Record<string, Message[] | undefined> = {};\n\nexport function useChat({\n api = '/api/chat',\n id,\n initialMessages = [],\n initialInput = '',\n sendExtraMessageFields,\n experimental_onFunctionCall,\n streamMode,\n onResponse,\n onFinish,\n onError,\n credentials,\n headers,\n body,\n generateId = generateIdFunc,\n fetch,\n keepLastMessageOnError = false,\n}: UseChatOptions = {}): UseChatHelpers {\n // Generate a unique ID for the chat if not provided.\n const chatId = id || `chat-${uniqueId++}`;\n\n const key = `${api}|${chatId}`;\n const { data: messagesData, mutate: originalMutate } = useSWRV<Message[]>(\n key,\n () => store[key] || initialMessages,\n );\n\n const { data: isLoading, mutate: mutateLoading } = useSWRV<boolean>(\n `${chatId}-loading`,\n null,\n );\n\n isLoading.value ??= false;\n\n // Force the `data` to be `initialMessages` if it's `undefined`.\n messagesData.value ??= initialMessages;\n\n const mutate = (data?: Message[]) => {\n store[key] = data;\n return originalMutate();\n };\n\n // Because of the `initialData` option, the `data` will never be `undefined`.\n const messages = messagesData as Ref<Message[]>;\n\n const error = ref<undefined | Error>(undefined);\n // cannot use JSONValue[] in ref because of infinite Typescript recursion:\n const streamData = ref<undefined | unknown[]>(undefined);\n\n let abortController: AbortController | null = null;\n async function triggerRequest(\n messagesSnapshot: Message[],\n { options, data, headers, body }: ChatRequestOptions = {},\n ) {\n try {\n error.value = undefined;\n mutateLoading(() => true);\n\n abortController = new AbortController();\n\n // Do an optimistic update to the chat state to show the updated messages\n // immediately.\n const previousMessages = messagesSnapshot;\n mutate(messagesSnapshot);\n\n const requestOptions = {\n headers: headers ?? options?.headers,\n body: body ?? options?.body,\n };\n\n let chatRequest: ChatRequest = {\n messages: messagesSnapshot,\n options: requestOptions,\n body: requestOptions.body,\n headers: requestOptions.headers,\n data,\n };\n\n await processChatStream({\n getStreamedResponse: async () => {\n const existingData = (streamData.value ?? []) as JSONValue[];\n\n const constructedMessagesPayload = sendExtraMessageFields\n ? chatRequest.messages\n : chatRequest.messages.map(\n ({\n role,\n content,\n name,\n data,\n annotations,\n function_call,\n }) => ({\n role,\n content,\n ...(name !== undefined && { name }),\n ...(data !== undefined && { data }),\n ...(annotations !== undefined && { annotations }),\n // outdated function/tool call handling (TODO deprecate):\n ...(function_call !== undefined && { function_call }),\n }),\n );\n\n return await callChatApi({\n api,\n body: {\n messages: constructedMessagesPayload,\n data: chatRequest.data,\n ...unref(body), // Use unref to unwrap the ref value\n ...requestOptions.body,\n },\n streamMode,\n headers: {\n ...headers,\n ...requestOptions.headers,\n },\n abortController: () => abortController,\n credentials,\n onResponse,\n onUpdate(merged, data) {\n mutate([...chatRequest.messages, ...merged]);\n streamData.value = [...existingData, ...(data ?? [])];\n },\n onFinish(message) {\n // workaround: sometimes the last chunk is not shown in the UI.\n // push it twice to make sure it's displayed.\n mutate([...chatRequest.messages, message]);\n onFinish?.(message);\n },\n restoreMessagesOnFailure() {\n // Restore the previous messages if the request fails.\n if (!keepLastMessageOnError) {\n mutate(previousMessages);\n }\n },\n generateId,\n onToolCall: undefined, // not implemented yet\n fetch,\n });\n },\n experimental_onFunctionCall,\n updateChatRequest(newChatRequest) {\n chatRequest = newChatRequest;\n },\n getCurrentMessages: () => messages.value,\n });\n\n abortController = null;\n } catch (err) {\n // Ignore abort errors as they are expected.\n if ((err as any).name === 'AbortError') {\n abortController = null;\n return null;\n }\n\n if (onError && err instanceof Error) {\n onError(err);\n }\n\n error.value = err as Error;\n } finally {\n mutateLoading(() => false);\n }\n }\n\n const append: UseChatHelpers['append'] = async (message, options) => {\n if (!message.id) {\n message.id = generateId();\n }\n\n return triggerRequest(messages.value.concat(message as Message), options);\n };\n\n const reload: UseChatHelpers['reload'] = async options => {\n const messagesSnapshot = messages.value;\n if (messagesSnapshot.length === 0) return null;\n\n const lastMessage = messagesSnapshot[messagesSnapshot.length - 1];\n if (lastMessage.role === 'assistant') {\n return triggerRequest(messagesSnapshot.slice(0, -1), options);\n }\n return triggerRequest(messagesSnapshot, options);\n };\n\n const stop = () => {\n if (abortController) {\n abortController.abort();\n abortController = null;\n }\n };\n\n const setMessages = (\n messagesArg: Message[] | ((messages: Message[]) => Message[]),\n ) => {\n if (typeof messagesArg === 'function') {\n messagesArg = messagesArg(messages.value);\n }\n\n mutate(messagesArg);\n };\n\n const input = ref(initialInput);\n\n const handleSubmit = (\n event?: { preventDefault?: () => void },\n options: ChatRequestOptions = {},\n ) => {\n event?.preventDefault?.();\n\n const inputValue = input.value;\n\n if (!inputValue && !options.allowEmptySubmit) return;\n\n triggerRequest(\n !inputValue && options.allowEmptySubmit\n ? messages.value\n : messages.value.concat({\n id: generateId(),\n createdAt: new Date(),\n content: inputValue,\n role: 'user',\n }),\n options,\n );\n\n input.value = '';\n };\n\n return {\n messages,\n append,\n error,\n reload,\n stop,\n setMessages,\n input,\n handleSubmit,\n isLoading,\n data: streamData as Ref<undefined | JSONValue[]>,\n };\n}\n","import type {\n JSONValue,\n RequestOptions,\n UseCompletionOptions,\n} from '@ai-sdk/ui-utils';\nimport { callCompletionApi } from '@ai-sdk/ui-utils';\nimport swrv from 'swrv';\nimport type { Ref } from 'vue';\nimport { ref, unref } from 'vue';\n\nexport type { UseCompletionOptions };\n\nexport type UseCompletionHelpers = {\n /** The current completion result */\n completion: Ref<string>;\n /** The error object of the API request */\n error: Ref<undefined | Error>;\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 /**\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: Ref<string>;\n /**\n * Form submission handler to automatically reset input and append a user message\n * @example\n * ```jsx\n * <form @submit=\"handleSubmit\">\n * <input @change=\"handleInputChange\" v-model=\"input\" />\n * </form>\n * ```\n */\n handleSubmit: (event?: { preventDefault?: () => void }) => void;\n /** Whether the API request is in progress */\n isLoading: Ref<boolean | undefined>;\n\n /** Additional data added on the server via StreamData */\n data: Ref<JSONValue[] | undefined>;\n};\n\nlet uniqueId = 0;\n\n// @ts-expect-error - some issues with the default export of useSWRV\nconst useSWRV = (swrv.default as typeof import('swrv')['default']) || swrv;\nconst store: Record<string, any> = {};\n\nexport function useCompletion({\n api = '/api/completion',\n id,\n initialCompletion = '',\n initialInput = '',\n credentials,\n headers,\n body,\n streamMode,\n onResponse,\n onFinish,\n onError,\n fetch,\n}: UseCompletionOptions = {}): UseCompletionHelpers {\n // Generate an unique id for the completion if not provided.\n const completionId = id || `completion-${uniqueId++}`;\n\n const key = `${api}|${completionId}`;\n const { data, mutate: originalMutate } = useSWRV<string>(\n key,\n () => store[key] || initialCompletion,\n );\n\n const { data: isLoading, mutate: mutateLoading } = useSWRV<boolean>(\n `${completionId}-loading`,\n null,\n );\n\n isLoading.value ??= false;\n\n const { data: streamData, mutate: mutateStreamData } = useSWRV<\n JSONValue[] | undefined\n >(`${completionId}-data`, null);\n\n // Force the `data` to be `initialCompletion` if it's `undefined`.\n data.value ||= initialCompletion;\n\n const mutate = (data: string) => {\n store[key] = data;\n return originalMutate();\n };\n\n // Because of the `initialData` option, the `data` will never be `undefined`.\n const completion = data as Ref<string>;\n\n const error = ref<undefined | Error>(undefined);\n\n let abortController: AbortController | null = null;\n\n async function triggerRequest(prompt: string, options?: RequestOptions) {\n const existingData = (streamData.value ?? []) as JSONValue[];\n return callCompletionApi({\n api,\n prompt,\n credentials,\n headers: {\n ...headers,\n ...options?.headers,\n },\n body: {\n ...unref(body),\n ...options?.body,\n },\n streamMode,\n setCompletion: mutate,\n setLoading: loading => mutateLoading(() => loading),\n setError: err => {\n error.value = err;\n },\n setAbortController: controller => {\n abortController = controller;\n },\n onResponse,\n onFinish,\n onError,\n onData: data => {\n mutateStreamData(() => [...existingData, ...(data ?? [])]);\n },\n fetch,\n });\n }\n\n const complete: UseCompletionHelpers['complete'] = async (\n prompt,\n options,\n ) => {\n return triggerRequest(prompt, options);\n };\n\n const stop = () => {\n if (abortController) {\n abortController.abort();\n abortController = null;\n }\n };\n\n const setCompletion = (completion: string) => {\n mutate(completion);\n };\n\n const input = ref(initialInput);\n\n const handleSubmit = (event?: { preventDefault?: () => void }) => {\n event?.preventDefault?.();\n const inputValue = input.value;\n return inputValue ? complete(inputValue) : undefined;\n };\n\n return {\n completion,\n complete,\n error,\n stop,\n setCompletion,\n input,\n handleSubmit,\n isLoading,\n data: streamData,\n };\n}\n","/**\n * A vue.js composable function to interact with the assistant API.\n */\n\nimport { isAbortError } from '@ai-sdk/provider-utils';\nimport { readDataStream, generateId } from '@ai-sdk/ui-utils';\nimport type {\n AssistantStatus,\n CreateMessage,\n Message,\n UseAssistantOptions,\n} from '@ai-sdk/ui-utils';\nimport { computed, readonly, ref } from 'vue';\nimport type { ComputedRef, Ref } from 'vue';\n\nexport type UseAssistantHelpers = {\n /**\n * The current array of chat messages.\n */\n messages: Ref<Message[]>;\n\n /**\n * Update the message store with a new array of messages.\n */\n setMessages: (messagesProcessor: (messages: Message[]) => Message[]) => void;\n\n /**\n * The current thread ID.\n */\n threadId: Ref<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 * The current value of the input field.\n */\n input: Ref<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 /**\n * Abort the current request immediately, keep the generated tokens if any.\n */\n stop: ComputedRef<() => void>;\n\n /**\n * Handler for the `onChange` event of the input field to control the input's value.\n */\n handleInputChange: (e: Event & { target: HTMLInputElement }) => void;\n\n /**\n * Handler for the `onSubmit` event of the form to append a user message and reset the input.\n */\n handleSubmit: (e: Event & { target: HTMLFormElement }) => void;\n\n /**\n * Whether the assistant is currently sending a message.\n */\n isSending: ComputedRef<boolean>;\n\n /**\n * The current status of the assistant.\n */\n status: Ref<AssistantStatus>;\n\n /**\n * The current error, if any.\n */\n error: Ref<Error | undefined>;\n};\n\nexport function useAssistant({\n api,\n threadId: threadIdParam,\n credentials,\n headers,\n body,\n onError,\n}: UseAssistantOptions): UseAssistantHelpers {\n const messages: Ref<Message[]> = ref([]);\n const input: Ref<string> = ref('');\n const currentThreadId: Ref<string | undefined> = ref(undefined);\n const status: Ref<AssistantStatus> = ref('awaiting_message');\n const error: Ref<undefined | Error> = ref(undefined);\n\n const setMessages = (messageFactory: (messages: Message[]) => Message[]) => {\n messages.value = messageFactory(messages.value);\n };\n\n const setCurrentThreadId = (newThreadId: string | undefined) => {\n currentThreadId.value = newThreadId;\n messages.value = [];\n };\n\n const handleInputChange = (event: Event & { target: HTMLInputElement }) => {\n input.value = event?.target?.value;\n };\n\n const isSending = computed(() => status.value === 'in_progress');\n\n // Abort controller to cancel the current API call when required\n const abortController = ref<AbortController | null>(null);\n\n // memoized function to stop the current request when required\n const stop = computed(() => {\n return () => {\n if (abortController.value) {\n abortController.value.abort();\n abortController.value = null;\n }\n };\n });\n\n const append = async (\n message: Message | CreateMessage,\n requestOptions?: {\n data?: Record<string, string>;\n },\n ) => {\n status.value = 'in_progress';\n\n // Append the new message to the current list of messages\n const newMessage: Message = {\n ...message,\n id: message.id ?? generateId(),\n };\n\n // Update the messages list with the new message\n setMessages(messages => [...messages, newMessage]);\n\n input.value = '';\n\n const controller = new AbortController();\n\n try {\n // Assign the new controller to the abortController ref\n abortController.value = controller;\n\n const response = await fetch(api, {\n method: 'POST',\n headers: {\n 'Content-Type': 'application/json',\n ...headers,\n },\n body: JSON.stringify({\n ...body,\n // Message Content\n message: message.content,\n\n // Always Use User Provided Thread ID When Available\n threadId: threadIdParam ?? currentThreadId.value ?? null,\n\n // Optional Request Data\n ...(requestOptions?.data && { data: requestOptions?.data }),\n }),\n signal: controller.signal,\n credentials,\n });\n\n if (!response.ok) {\n throw new Error(\n response.statusText ?? 'An error occurred while sending the message',\n );\n }\n\n if (!response.body) {\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 messages.value = [\n ...messages.value,\n {\n id: value.id,\n content: value.content[0].text.value,\n role: value.role,\n },\n ];\n break;\n }\n case 'assistant_control_data': {\n if (value.threadId) {\n currentThreadId.value = value.threadId;\n }\n\n setMessages(messages => {\n const lastMessage = messages[messages.length - 1];\n lastMessage.id = value.messageId;\n\n return [...messages.slice(0, -1), lastMessage];\n });\n\n break;\n }\n\n case 'text': {\n setMessages(messages => {\n const lastMessage = messages[messages.length - 1];\n lastMessage.content += value;\n\n return [...messages.slice(0, -1), lastMessage];\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 'error': {\n error.value = new Error(value);\n }\n\n default: {\n console.error('Unknown message type:', type);\n break;\n }\n }\n }\n } catch (err) {\n // If the error is an AbortError and the signal is aborted, reset the abortController and do nothing.\n if (isAbortError(err) && abortController.value?.signal.aborted) {\n abortController.value = null;\n return;\n }\n\n // If an error handler is provided, call it with the error\n if (onError && err instanceof Error) {\n onError(err);\n }\n\n error.value = err as Error;\n } finally {\n // Reset the status to 'awaiting_message' after the request is complete\n abortController.value = null;\n status.value = 'awaiting_message';\n }\n };\n\n const submitMessage = async (\n event: Event & { target: HTMLFormElement },\n requestOptions?: {\n data?: Record<string, string>;\n },\n ) => {\n event?.preventDefault?.();\n\n if (!input.value) return;\n\n append(\n {\n role: 'user',\n content: input.value,\n },\n requestOptions,\n );\n };\n\n return {\n append,\n messages,\n setMessages,\n threadId: readonly(currentThreadId),\n setThreadId: setCurrentThreadId,\n input,\n handleInputChange,\n handleSubmit: submitMessage,\n isSending,\n status,\n error,\n stop,\n };\n}\n\n/**\n * @deprecated Use `useAssistant` instead.\n */\nexport const experimental_useAssistant = useAssistant;\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA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MACb;AAEA,YAAM,QAAQ;AAAA,IAChB,UAAE;AAEA,sBAAgB,QAAQ;AACxB,aAAO,QAAQ;AAAA,IACjB;AAAA,EACF;AAEA,QAAM,gBAAgB,OACpB,OACA,mBAGG;AA/QP;AAgRI,yCAAO,mBAAP;AAEA,QAAI,CAAC,MAAM;AAAO;AAElB;AAAA,MACE;AAAA,QACE,MAAM;AAAA,QACN,SAAS,MAAM;AAAA,MACjB;AAAA,MACA;AAAA,IACF;AAAA,EACF;AAEA,SAAO;AAAA,IACL;AAAA,IACA;AAAA,IACA;AAAA,IACA,cAAU,sBAAS,eAAe;AAAA,IAClC,aAAa;AAAA,IACb;AAAA,IACA;AAAA,IACA,cAAc;AAAA,IACd;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF;AACF;AAKO,IAAM,4BAA4B;","names":["swrv","generateId","generateIdFunc","fetch","headers","body","_a","data","import_ui_utils","import_swrv","import_vue","uniqueId","useSWRV","swrv","store","fetch","data","_a","completion","import_ui_utils","import_vue","messages","_a"]}
package/dist/index.mjs CHANGED
@@ -145,14 +145,9 @@ function useChat({
145
145
  }
146
146
  }
147
147
  const append = async (message, options) => {
148
- var _a2, _b2, _c, _d;
149
148
  if (!message.id) {
150
149
  message.id = generateId2();
151
150
  }
152
- const requestOptions = {
153
- headers: (_b2 = options == null ? void 0 : options.headers) != null ? _b2 : (_a2 = options == null ? void 0 : options.options) == null ? void 0 : _a2.headers,
154
- body: (_d = options == null ? void 0 : options.body) != null ? _d : (_c = options == null ? void 0 : options.options) == null ? void 0 : _c.body
155
- };
156
151
  return triggerRequest(messages.value.concat(message), options);
157
152
  };
158
153
  const reload = async (options) => {
@@ -171,21 +166,26 @@ function useChat({
171
166
  abortController = null;
172
167
  }
173
168
  };
174
- const setMessages = (messages2) => {
175
- mutate(messages2);
169
+ const setMessages = (messagesArg) => {
170
+ if (typeof messagesArg === "function") {
171
+ messagesArg = messagesArg(messages.value);
172
+ }
173
+ mutate(messagesArg);
176
174
  };
177
175
  const input = ref(initialInput);
178
176
  const handleSubmit = (event, options = {}) => {
179
177
  var _a2;
180
178
  (_a2 = event == null ? void 0 : event.preventDefault) == null ? void 0 : _a2.call(event);
181
179
  const inputValue = input.value;
180
+ if (!inputValue && !options.allowEmptySubmit)
181
+ return;
182
182
  triggerRequest(
183
- inputValue ? messages.value.concat({
183
+ !inputValue && options.allowEmptySubmit ? messages.value : messages.value.concat({
184
184
  id: generateId2(),
185
185
  createdAt: /* @__PURE__ */ new Date(),
186
186
  content: inputValue,
187
187
  role: "user"
188
- }) : messages.value,
188
+ }),
189
189
  options
190
190
  );
191
191
  input.value = "";
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/use-chat.ts","../src/use-completion.ts","../src/use-assistant.ts"],"sourcesContent":["import type {\n ChatRequest,\n ChatRequestOptions,\n CreateMessage,\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 swrv from 'swrv';\nimport type { Ref } from 'vue';\nimport { ref, unref } from 'vue';\n\nexport type { CreateMessage, Message, UseChatOptions };\n\nexport type UseChatHelpers = {\n /** Current messages in the chat */\n messages: Ref<Message[]>;\n /** The error object of the API request */\n error: Ref<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 */\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: (messages: Message[]) => void;\n /** The current value of the input */\n input: Ref<string>;\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 /** Whether the API request is in progress */\n isLoading: Ref<boolean | undefined>;\n\n /** Additional data added on the server via StreamData */\n data: Ref<JSONValue[] | undefined>;\n};\n\nlet uniqueId = 0;\n\n// @ts-expect-error - some issues with the default export of useSWRV\nconst useSWRV = (swrv.default as typeof import('swrv')['default']) || swrv;\nconst store: Record<string, Message[] | undefined> = {};\n\nexport function useChat({\n api = '/api/chat',\n id,\n initialMessages = [],\n initialInput = '',\n sendExtraMessageFields,\n experimental_onFunctionCall,\n streamMode,\n onResponse,\n onFinish,\n onError,\n credentials,\n headers,\n body,\n generateId = generateIdFunc,\n fetch,\n keepLastMessageOnError = false,\n}: UseChatOptions = {}): UseChatHelpers {\n // Generate a unique ID for the chat if not provided.\n const chatId = id || `chat-${uniqueId++}`;\n\n const key = `${api}|${chatId}`;\n const { data: messagesData, mutate: originalMutate } = useSWRV<Message[]>(\n key,\n () => store[key] || initialMessages,\n );\n\n const { data: isLoading, mutate: mutateLoading } = useSWRV<boolean>(\n `${chatId}-loading`,\n null,\n );\n\n isLoading.value ??= false;\n\n // Force the `data` to be `initialMessages` if it's `undefined`.\n messagesData.value ??= initialMessages;\n\n const mutate = (data?: Message[]) => {\n store[key] = data;\n return originalMutate();\n };\n\n // Because of the `initialData` option, the `data` will never be `undefined`.\n const messages = messagesData as Ref<Message[]>;\n\n const error = ref<undefined | Error>(undefined);\n // cannot use JSONValue[] in ref because of infinite Typescript recursion:\n const streamData = ref<undefined | unknown[]>(undefined);\n\n let abortController: AbortController | null = null;\n async function triggerRequest(\n messagesSnapshot: Message[],\n { options, data, headers, body }: ChatRequestOptions = {},\n ) {\n try {\n error.value = undefined;\n mutateLoading(() => true);\n\n abortController = new AbortController();\n\n // Do an optimistic update to the chat state to show the updated messages\n // immediately.\n const previousMessages = messagesSnapshot;\n mutate(messagesSnapshot);\n\n const requestOptions = {\n headers: headers ?? options?.headers,\n body: body ?? options?.body,\n };\n\n let chatRequest: ChatRequest = {\n messages: messagesSnapshot,\n options: requestOptions,\n body: requestOptions.body,\n headers: requestOptions.headers,\n data,\n };\n\n await processChatStream({\n getStreamedResponse: async () => {\n const existingData = (streamData.value ?? []) as JSONValue[];\n\n const constructedMessagesPayload = sendExtraMessageFields\n ? chatRequest.messages\n : chatRequest.messages.map(\n ({\n role,\n content,\n name,\n data,\n annotations,\n function_call,\n }) => ({\n role,\n content,\n ...(name !== undefined && { name }),\n ...(data !== undefined && { data }),\n ...(annotations !== undefined && { annotations }),\n // outdated function/tool call handling (TODO deprecate):\n ...(function_call !== undefined && { function_call }),\n }),\n );\n\n return await callChatApi({\n api,\n body: {\n messages: constructedMessagesPayload,\n data: chatRequest.data,\n ...unref(body), // Use unref to unwrap the ref value\n ...requestOptions.body,\n },\n streamMode,\n headers: {\n ...headers,\n ...requestOptions.headers,\n },\n abortController: () => abortController,\n credentials,\n onResponse,\n onUpdate(merged, data) {\n mutate([...chatRequest.messages, ...merged]);\n streamData.value = [...existingData, ...(data ?? [])];\n },\n onFinish(message) {\n // workaround: sometimes the last chunk is not shown in the UI.\n // push it twice to make sure it's displayed.\n mutate([...chatRequest.messages, message]);\n onFinish?.(message);\n },\n restoreMessagesOnFailure() {\n // Restore the previous messages if the request fails.\n if (!keepLastMessageOnError) {\n mutate(previousMessages);\n }\n },\n generateId,\n onToolCall: undefined, // not implemented yet\n fetch,\n });\n },\n experimental_onFunctionCall,\n updateChatRequest(newChatRequest) {\n chatRequest = newChatRequest;\n },\n getCurrentMessages: () => messages.value,\n });\n\n abortController = null;\n } catch (err) {\n // Ignore abort errors as they are expected.\n if ((err as any).name === 'AbortError') {\n abortController = null;\n return null;\n }\n\n if (onError && err instanceof Error) {\n onError(err);\n }\n\n error.value = err as Error;\n } finally {\n mutateLoading(() => false);\n }\n }\n\n const append: UseChatHelpers['append'] = async (message, options) => {\n if (!message.id) {\n message.id = generateId();\n }\n\n const requestOptions = {\n headers: options?.headers ?? options?.options?.headers,\n body: options?.body ?? options?.options?.body,\n };\n\n return triggerRequest(messages.value.concat(message as Message), options);\n };\n\n const reload: UseChatHelpers['reload'] = async options => {\n const messagesSnapshot = messages.value;\n if (messagesSnapshot.length === 0) return null;\n\n const lastMessage = messagesSnapshot[messagesSnapshot.length - 1];\n if (lastMessage.role === 'assistant') {\n return triggerRequest(messagesSnapshot.slice(0, -1), options);\n }\n return triggerRequest(messagesSnapshot, options);\n };\n\n const stop = () => {\n if (abortController) {\n abortController.abort();\n abortController = null;\n }\n };\n\n const setMessages = (messages: Message[]) => {\n mutate(messages);\n };\n\n const input = ref(initialInput);\n\n const handleSubmit = (\n event?: { preventDefault?: () => void },\n options: ChatRequestOptions = {},\n ) => {\n event?.preventDefault?.();\n\n const inputValue = input.value;\n\n triggerRequest(\n inputValue\n ? messages.value.concat({\n id: generateId(),\n createdAt: new Date(),\n content: inputValue,\n role: 'user',\n })\n : messages.value,\n options,\n );\n\n input.value = '';\n };\n\n return {\n messages,\n append,\n error,\n reload,\n stop,\n setMessages,\n input,\n handleSubmit,\n isLoading,\n data: streamData as Ref<undefined | JSONValue[]>,\n };\n}\n","import type {\n JSONValue,\n RequestOptions,\n UseCompletionOptions,\n} from '@ai-sdk/ui-utils';\nimport { callCompletionApi } from '@ai-sdk/ui-utils';\nimport swrv from 'swrv';\nimport type { Ref } from 'vue';\nimport { ref, unref } from 'vue';\n\nexport type { UseCompletionOptions };\n\nexport type UseCompletionHelpers = {\n /** The current completion result */\n completion: Ref<string>;\n /** The error object of the API request */\n error: Ref<undefined | Error>;\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 /**\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: Ref<string>;\n /**\n * Form submission handler to automatically reset input and append a user message\n * @example\n * ```jsx\n * <form @submit=\"handleSubmit\">\n * <input @change=\"handleInputChange\" v-model=\"input\" />\n * </form>\n * ```\n */\n handleSubmit: (event?: { preventDefault?: () => void }) => void;\n /** Whether the API request is in progress */\n isLoading: Ref<boolean | undefined>;\n\n /** Additional data added on the server via StreamData */\n data: Ref<JSONValue[] | undefined>;\n};\n\nlet uniqueId = 0;\n\n// @ts-expect-error - some issues with the default export of useSWRV\nconst useSWRV = (swrv.default as typeof import('swrv')['default']) || swrv;\nconst store: Record<string, any> = {};\n\nexport function useCompletion({\n api = '/api/completion',\n id,\n initialCompletion = '',\n initialInput = '',\n credentials,\n headers,\n body,\n streamMode,\n onResponse,\n onFinish,\n onError,\n fetch,\n}: UseCompletionOptions = {}): UseCompletionHelpers {\n // Generate an unique id for the completion if not provided.\n const completionId = id || `completion-${uniqueId++}`;\n\n const key = `${api}|${completionId}`;\n const { data, mutate: originalMutate } = useSWRV<string>(\n key,\n () => store[key] || initialCompletion,\n );\n\n const { data: isLoading, mutate: mutateLoading } = useSWRV<boolean>(\n `${completionId}-loading`,\n null,\n );\n\n isLoading.value ??= false;\n\n const { data: streamData, mutate: mutateStreamData } = useSWRV<\n JSONValue[] | undefined\n >(`${completionId}-data`, null);\n\n // Force the `data` to be `initialCompletion` if it's `undefined`.\n data.value ||= initialCompletion;\n\n const mutate = (data: string) => {\n store[key] = data;\n return originalMutate();\n };\n\n // Because of the `initialData` option, the `data` will never be `undefined`.\n const completion = data as Ref<string>;\n\n const error = ref<undefined | Error>(undefined);\n\n let abortController: AbortController | null = null;\n\n async function triggerRequest(prompt: string, options?: RequestOptions) {\n const existingData = (streamData.value ?? []) as JSONValue[];\n return callCompletionApi({\n api,\n prompt,\n credentials,\n headers: {\n ...headers,\n ...options?.headers,\n },\n body: {\n ...unref(body),\n ...options?.body,\n },\n streamMode,\n setCompletion: mutate,\n setLoading: loading => mutateLoading(() => loading),\n setError: err => {\n error.value = err;\n },\n setAbortController: controller => {\n abortController = controller;\n },\n onResponse,\n onFinish,\n onError,\n onData: data => {\n mutateStreamData(() => [...existingData, ...(data ?? [])]);\n },\n fetch,\n });\n }\n\n const complete: UseCompletionHelpers['complete'] = async (\n prompt,\n options,\n ) => {\n return triggerRequest(prompt, options);\n };\n\n const stop = () => {\n if (abortController) {\n abortController.abort();\n abortController = null;\n }\n };\n\n const setCompletion = (completion: string) => {\n mutate(completion);\n };\n\n const input = ref(initialInput);\n\n const handleSubmit = (event?: { preventDefault?: () => void }) => {\n event?.preventDefault?.();\n const inputValue = input.value;\n return inputValue ? complete(inputValue) : undefined;\n };\n\n return {\n completion,\n complete,\n error,\n stop,\n setCompletion,\n input,\n handleSubmit,\n isLoading,\n data: streamData,\n };\n}\n","/**\n * A vue.js composable function to interact with the assistant API.\n */\n\nimport { isAbortError } from '@ai-sdk/provider-utils';\nimport { readDataStream, generateId } from '@ai-sdk/ui-utils';\nimport type {\n AssistantStatus,\n CreateMessage,\n Message,\n UseAssistantOptions,\n} from '@ai-sdk/ui-utils';\nimport { computed, readonly, ref } from 'vue';\nimport type { ComputedRef, Ref } from 'vue';\n\nexport type UseAssistantHelpers = {\n /**\n * The current array of chat messages.\n */\n messages: Ref<Message[]>;\n\n /**\n * Update the message store with a new array of messages.\n */\n setMessages: (messagesProcessor: (messages: Message[]) => Message[]) => void;\n\n /**\n * The current thread ID.\n */\n threadId: Ref<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 * The current value of the input field.\n */\n input: Ref<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 /**\n * Abort the current request immediately, keep the generated tokens if any.\n */\n stop: ComputedRef<() => void>;\n\n /**\n * Handler for the `onChange` event of the input field to control the input's value.\n */\n handleInputChange: (e: Event & { target: HTMLInputElement }) => void;\n\n /**\n * Handler for the `onSubmit` event of the form to append a user message and reset the input.\n */\n handleSubmit: (e: Event & { target: HTMLFormElement }) => void;\n\n /**\n * Whether the assistant is currently sending a message.\n */\n isSending: ComputedRef<boolean>;\n\n /**\n * The current status of the assistant.\n */\n status: Ref<AssistantStatus>;\n\n /**\n * The current error, if any.\n */\n error: Ref<Error | undefined>;\n};\n\nexport function useAssistant({\n api,\n threadId: threadIdParam,\n credentials,\n headers,\n body,\n onError,\n}: UseAssistantOptions): UseAssistantHelpers {\n const messages: Ref<Message[]> = ref([]);\n const input: Ref<string> = ref('');\n const currentThreadId: Ref<string | undefined> = ref(undefined);\n const status: Ref<AssistantStatus> = ref('awaiting_message');\n const error: Ref<undefined | Error> = ref(undefined);\n\n const setMessages = (messageFactory: (messages: Message[]) => Message[]) => {\n messages.value = messageFactory(messages.value);\n };\n\n const setCurrentThreadId = (newThreadId: string | undefined) => {\n currentThreadId.value = newThreadId;\n messages.value = [];\n };\n\n const handleInputChange = (event: Event & { target: HTMLInputElement }) => {\n input.value = event?.target?.value;\n };\n\n const isSending = computed(() => status.value === 'in_progress');\n\n // Abort controller to cancel the current API call when required\n const abortController = ref<AbortController | null>(null);\n\n // memoized function to stop the current request when required\n const stop = computed(() => {\n return () => {\n if (abortController.value) {\n abortController.value.abort();\n abortController.value = null;\n }\n };\n });\n\n const append = async (\n message: Message | CreateMessage,\n requestOptions?: {\n data?: Record<string, string>;\n },\n ) => {\n status.value = 'in_progress';\n\n // Append the new message to the current list of messages\n const newMessage: Message = {\n ...message,\n id: message.id ?? generateId(),\n };\n\n // Update the messages list with the new message\n setMessages(messages => [...messages, newMessage]);\n\n input.value = '';\n\n const controller = new AbortController();\n\n try {\n // Assign the new controller to the abortController ref\n abortController.value = controller;\n\n const response = await fetch(api, {\n method: 'POST',\n headers: {\n 'Content-Type': 'application/json',\n ...headers,\n },\n body: JSON.stringify({\n ...body,\n // Message Content\n message: message.content,\n\n // Always Use User Provided Thread ID When Available\n threadId: threadIdParam ?? currentThreadId.value ?? null,\n\n // Optional Request Data\n ...(requestOptions?.data && { data: requestOptions?.data }),\n }),\n signal: controller.signal,\n credentials,\n });\n\n if (!response.ok) {\n throw new Error(\n response.statusText ?? 'An error occurred while sending the message',\n );\n }\n\n if (!response.body) {\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 messages.value = [\n ...messages.value,\n {\n id: value.id,\n content: value.content[0].text.value,\n role: value.role,\n },\n ];\n break;\n }\n case 'assistant_control_data': {\n if (value.threadId) {\n currentThreadId.value = value.threadId;\n }\n\n setMessages(messages => {\n const lastMessage = messages[messages.length - 1];\n lastMessage.id = value.messageId;\n\n return [...messages.slice(0, -1), lastMessage];\n });\n\n break;\n }\n\n case 'text': {\n setMessages(messages => {\n const lastMessage = messages[messages.length - 1];\n lastMessage.content += value;\n\n return [...messages.slice(0, -1), lastMessage];\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 'error': {\n error.value = new Error(value);\n }\n\n default: {\n console.error('Unknown message type:', type);\n break;\n }\n }\n }\n } catch (err) {\n // If the error is an AbortError and the signal is aborted, reset the abortController and do nothing.\n if (isAbortError(err) && abortController.value?.signal.aborted) {\n abortController.value = null;\n return;\n }\n\n // If an error handler is provided, call it with the error\n if (onError && err instanceof Error) {\n onError(err);\n }\n\n error.value = err as Error;\n } finally {\n // Reset the status to 'awaiting_message' after the request is complete\n abortController.value = null;\n status.value = 'awaiting_message';\n }\n };\n\n const submitMessage = async (\n event: Event & { target: HTMLFormElement },\n requestOptions?: {\n data?: Record<string, string>;\n },\n ) => {\n event?.preventDefault?.();\n\n if (!input.value) return;\n\n append(\n {\n role: 'user',\n content: input.value,\n },\n requestOptions,\n );\n };\n\n return {\n append,\n messages,\n setMessages,\n threadId: readonly(currentThreadId),\n setThreadId: setCurrentThreadId,\n input,\n handleInputChange,\n handleSubmit: submitMessage,\n isSending,\n status,\n error,\n stop,\n };\n}\n\n/**\n * @deprecated Use `useAssistant` instead.\n */\nexport const experimental_useAssistant = useAssistant;\n"],"mappings":";AAQA;AAAA,EACE;AAAA,EACA,cAAc;AAAA,EACd;AAAA,OACK;AACP,OAAO,UAAU;AAEjB,SAAS,KAAK,aAAa;AAiD3B,IAAI,WAAW;AAGf,IAAM,UAAW,KAAK,WAAgD;AACtE,IAAM,QAA+C,CAAC;AAE/C,SAAS,QAAQ;AAAA,EACtB,MAAM;AAAA,EACN;AAAA,EACA,kBAAkB,CAAC;AAAA,EACnB,eAAe;AAAA,EACf;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA,YAAAA,cAAa;AAAA,EACb,OAAAC;AAAA,EACA,yBAAyB;AAC3B,IAAoB,CAAC,GAAmB;AAvFxC;AAyFE,QAAM,SAAS,MAAM,QAAQ,UAAU;AAEvC,QAAM,MAAM,GAAG,GAAG,IAAI,MAAM;AAC5B,QAAM,EAAE,MAAM,cAAc,QAAQ,eAAe,IAAI;AAAA,IACrD;AAAA,IACA,MAAM,MAAM,GAAG,KAAK;AAAA,EACtB;AAEA,QAAM,EAAE,MAAM,WAAW,QAAQ,cAAc,IAAI;AAAA,IACjD,GAAG,MAAM;AAAA,IACT;AAAA,EACF;AAEA,kBAAU,UAAV,sBAAU,QAAU;AAGpB,qBAAa,UAAb,yBAAa,QAAU;AAEvB,QAAM,SAAS,CAAC,SAAqB;AACnC,UAAM,GAAG,IAAI;AACb,WAAO,eAAe;AAAA,EACxB;AAGA,QAAM,WAAW;AAEjB,QAAM,QAAQ,IAAuB,MAAS;AAE9C,QAAM,aAAa,IAA2B,MAAS;AA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AAA,UAEA,KAAK,SAAS;AACZ,kBAAM,QAAQ,IAAI,MAAM,KAAK;AAAA,UAC/B;AAAA,UAEA,SAAS;AACP,oBAAQ,MAAM,yBAAyB,IAAI;AAC3C;AAAA,UACF;AAAA,QACF;AAAA,MACF;AAAA,IACF,SAAS,KAAK;AAEZ,UAAI,aAAa,GAAG,OAAK,qBAAgB,UAAhB,mBAAuB,OAAO,UAAS;AAC9D,wBAAgB,QAAQ;AACxB;AAAA,MACF;AAGA,UAAI,WAAW,eAAe,OAAO;AACnC,gBAAQ,GAAG;AAAA,MACb;AAEA,YAAM,QAAQ;AAAA,IAChB,UAAE;AAEA,sBAAgB,QAAQ;AACxB,aAAO,QAAQ;AAAA,IACjB;AAAA,EACF;AAEA,QAAM,gBAAgB,OACpB,OACA,mBAGG;AA/QP;AAgRI,yCAAO,mBAAP;AAEA,QAAI,CAAC,MAAM;AAAO;AAElB;AAAA,MACE;AAAA,QACE,MAAM;AAAA,QACN,SAAS,MAAM;AAAA,MACjB;AAAA,MACA;AAAA,IACF;AAAA,EACF;AAEA,SAAO;AAAA,IACL;AAAA,IACA;AAAA,IACA;AAAA,IACA,UAAU,SAAS,eAAe;AAAA,IAClC,aAAa;AAAA,IACb;AAAA,IACA;AAAA,IACA,cAAc;AAAA,IACd;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF;AACF;AAKO,IAAM,4BAA4B;","names":["generateId","fetch","headers","body","_a","data","_b","messages","swrv","ref","unref","uniqueId","useSWRV","store","fetch","data","_a","completion","ref","messages","_a"]}
1
+ {"version":3,"sources":["../src/use-chat.ts","../src/use-completion.ts","../src/use-assistant.ts"],"sourcesContent":["import type {\n ChatRequest,\n ChatRequestOptions,\n CreateMessage,\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 swrv from 'swrv';\nimport type { Ref } from 'vue';\nimport { ref, unref } from 'vue';\n\nexport type { CreateMessage, Message, UseChatOptions };\n\nexport type UseChatHelpers = {\n /** Current messages in the chat */\n messages: Ref<Message[]>;\n /** The error object of the API request */\n error: Ref<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 */\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: Ref<string>;\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 /** Whether the API request is in progress */\n isLoading: Ref<boolean | undefined>;\n\n /** Additional data added on the server via StreamData */\n data: Ref<JSONValue[] | undefined>;\n};\n\nlet uniqueId = 0;\n\n// @ts-expect-error - some issues with the default export of useSWRV\nconst useSWRV = (swrv.default as typeof import('swrv')['default']) || swrv;\nconst store: Record<string, Message[] | undefined> = {};\n\nexport function useChat({\n api = '/api/chat',\n id,\n initialMessages = [],\n initialInput = '',\n sendExtraMessageFields,\n experimental_onFunctionCall,\n streamMode,\n onResponse,\n onFinish,\n onError,\n credentials,\n headers,\n body,\n generateId = generateIdFunc,\n fetch,\n keepLastMessageOnError = false,\n}: UseChatOptions = {}): UseChatHelpers {\n // Generate a unique ID for the chat if not provided.\n const chatId = id || `chat-${uniqueId++}`;\n\n const key = `${api}|${chatId}`;\n const { data: messagesData, mutate: originalMutate } = useSWRV<Message[]>(\n key,\n () => store[key] || initialMessages,\n );\n\n const { data: isLoading, mutate: mutateLoading } = useSWRV<boolean>(\n `${chatId}-loading`,\n null,\n );\n\n isLoading.value ??= false;\n\n // Force the `data` to be `initialMessages` if it's `undefined`.\n messagesData.value ??= initialMessages;\n\n const mutate = (data?: Message[]) => {\n store[key] = data;\n return originalMutate();\n };\n\n // Because of the `initialData` option, the `data` will never be `undefined`.\n const messages = messagesData as Ref<Message[]>;\n\n const error = ref<undefined | Error>(undefined);\n // cannot use JSONValue[] in ref because of infinite Typescript recursion:\n const streamData = ref<undefined | unknown[]>(undefined);\n\n let abortController: AbortController | null = null;\n async function triggerRequest(\n messagesSnapshot: Message[],\n { options, data, headers, body }: ChatRequestOptions = {},\n ) {\n try {\n error.value = undefined;\n mutateLoading(() => true);\n\n abortController = new AbortController();\n\n // Do an optimistic update to the chat state to show the updated messages\n // immediately.\n const previousMessages = messagesSnapshot;\n mutate(messagesSnapshot);\n\n const requestOptions = {\n headers: headers ?? options?.headers,\n body: body ?? options?.body,\n };\n\n let chatRequest: ChatRequest = {\n messages: messagesSnapshot,\n options: requestOptions,\n body: requestOptions.body,\n headers: requestOptions.headers,\n data,\n };\n\n await processChatStream({\n getStreamedResponse: async () => {\n const existingData = (streamData.value ?? []) as JSONValue[];\n\n const constructedMessagesPayload = sendExtraMessageFields\n ? chatRequest.messages\n : chatRequest.messages.map(\n ({\n role,\n content,\n name,\n data,\n annotations,\n function_call,\n }) => ({\n role,\n content,\n ...(name !== undefined && { name }),\n ...(data !== undefined && { data }),\n ...(annotations !== undefined && { annotations }),\n // outdated function/tool call handling (TODO deprecate):\n ...(function_call !== undefined && { function_call }),\n }),\n );\n\n return await callChatApi({\n api,\n body: {\n messages: constructedMessagesPayload,\n data: chatRequest.data,\n ...unref(body), // Use unref to unwrap the ref value\n ...requestOptions.body,\n },\n streamMode,\n headers: {\n ...headers,\n ...requestOptions.headers,\n },\n abortController: () => abortController,\n credentials,\n onResponse,\n onUpdate(merged, data) {\n mutate([...chatRequest.messages, ...merged]);\n streamData.value = [...existingData, ...(data ?? [])];\n },\n onFinish(message) {\n // workaround: sometimes the last chunk is not shown in the UI.\n // push it twice to make sure it's displayed.\n mutate([...chatRequest.messages, message]);\n onFinish?.(message);\n },\n restoreMessagesOnFailure() {\n // Restore the previous messages if the request fails.\n if (!keepLastMessageOnError) {\n mutate(previousMessages);\n }\n },\n generateId,\n onToolCall: undefined, // not implemented yet\n fetch,\n });\n },\n experimental_onFunctionCall,\n updateChatRequest(newChatRequest) {\n chatRequest = newChatRequest;\n },\n getCurrentMessages: () => messages.value,\n });\n\n abortController = null;\n } catch (err) {\n // Ignore abort errors as they are expected.\n if ((err as any).name === 'AbortError') {\n abortController = null;\n return null;\n }\n\n if (onError && err instanceof Error) {\n onError(err);\n }\n\n error.value = err as Error;\n } finally {\n mutateLoading(() => false);\n }\n }\n\n const append: UseChatHelpers['append'] = async (message, options) => {\n if (!message.id) {\n message.id = generateId();\n }\n\n return triggerRequest(messages.value.concat(message as Message), options);\n };\n\n const reload: UseChatHelpers['reload'] = async options => {\n const messagesSnapshot = messages.value;\n if (messagesSnapshot.length === 0) return null;\n\n const lastMessage = messagesSnapshot[messagesSnapshot.length - 1];\n if (lastMessage.role === 'assistant') {\n return triggerRequest(messagesSnapshot.slice(0, -1), options);\n }\n return triggerRequest(messagesSnapshot, options);\n };\n\n const stop = () => {\n if (abortController) {\n abortController.abort();\n abortController = null;\n }\n };\n\n const setMessages = (\n messagesArg: Message[] | ((messages: Message[]) => Message[]),\n ) => {\n if (typeof messagesArg === 'function') {\n messagesArg = messagesArg(messages.value);\n }\n\n mutate(messagesArg);\n };\n\n const input = ref(initialInput);\n\n const handleSubmit = (\n event?: { preventDefault?: () => void },\n options: ChatRequestOptions = {},\n ) => {\n event?.preventDefault?.();\n\n const inputValue = input.value;\n\n if (!inputValue && !options.allowEmptySubmit) return;\n\n triggerRequest(\n !inputValue && options.allowEmptySubmit\n ? messages.value\n : messages.value.concat({\n id: generateId(),\n createdAt: new Date(),\n content: inputValue,\n role: 'user',\n }),\n options,\n );\n\n input.value = '';\n };\n\n return {\n messages,\n append,\n error,\n reload,\n stop,\n setMessages,\n input,\n handleSubmit,\n isLoading,\n data: streamData as Ref<undefined | JSONValue[]>,\n };\n}\n","import type {\n JSONValue,\n RequestOptions,\n UseCompletionOptions,\n} from '@ai-sdk/ui-utils';\nimport { callCompletionApi } from '@ai-sdk/ui-utils';\nimport swrv from 'swrv';\nimport type { Ref } from 'vue';\nimport { ref, unref } from 'vue';\n\nexport type { UseCompletionOptions };\n\nexport type UseCompletionHelpers = {\n /** The current completion result */\n completion: Ref<string>;\n /** The error object of the API request */\n error: Ref<undefined | Error>;\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 /**\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: Ref<string>;\n /**\n * Form submission handler to automatically reset input and append a user message\n * @example\n * ```jsx\n * <form @submit=\"handleSubmit\">\n * <input @change=\"handleInputChange\" v-model=\"input\" />\n * </form>\n * ```\n */\n handleSubmit: (event?: { preventDefault?: () => void }) => void;\n /** Whether the API request is in progress */\n isLoading: Ref<boolean | undefined>;\n\n /** Additional data added on the server via StreamData */\n data: Ref<JSONValue[] | undefined>;\n};\n\nlet uniqueId = 0;\n\n// @ts-expect-error - some issues with the default export of useSWRV\nconst useSWRV = (swrv.default as typeof import('swrv')['default']) || swrv;\nconst store: Record<string, any> = {};\n\nexport function useCompletion({\n api = '/api/completion',\n id,\n initialCompletion = '',\n initialInput = '',\n credentials,\n headers,\n body,\n streamMode,\n onResponse,\n onFinish,\n onError,\n fetch,\n}: UseCompletionOptions = {}): UseCompletionHelpers {\n // Generate an unique id for the completion if not provided.\n const completionId = id || `completion-${uniqueId++}`;\n\n const key = `${api}|${completionId}`;\n const { data, mutate: originalMutate } = useSWRV<string>(\n key,\n () => store[key] || initialCompletion,\n );\n\n const { data: isLoading, mutate: mutateLoading } = useSWRV<boolean>(\n `${completionId}-loading`,\n null,\n );\n\n isLoading.value ??= false;\n\n const { data: streamData, mutate: mutateStreamData } = useSWRV<\n JSONValue[] | undefined\n >(`${completionId}-data`, null);\n\n // Force the `data` to be `initialCompletion` if it's `undefined`.\n data.value ||= initialCompletion;\n\n const mutate = (data: string) => {\n store[key] = data;\n return originalMutate();\n };\n\n // Because of the `initialData` option, the `data` will never be `undefined`.\n const completion = data as Ref<string>;\n\n const error = ref<undefined | Error>(undefined);\n\n let abortController: AbortController | null = null;\n\n async function triggerRequest(prompt: string, options?: RequestOptions) {\n const existingData = (streamData.value ?? []) as JSONValue[];\n return callCompletionApi({\n api,\n prompt,\n credentials,\n headers: {\n ...headers,\n ...options?.headers,\n },\n body: {\n ...unref(body),\n ...options?.body,\n },\n streamMode,\n setCompletion: mutate,\n setLoading: loading => mutateLoading(() => loading),\n setError: err => {\n error.value = err;\n },\n setAbortController: controller => {\n abortController = controller;\n },\n onResponse,\n onFinish,\n onError,\n onData: data => {\n mutateStreamData(() => [...existingData, ...(data ?? [])]);\n },\n fetch,\n });\n }\n\n const complete: UseCompletionHelpers['complete'] = async (\n prompt,\n options,\n ) => {\n return triggerRequest(prompt, options);\n };\n\n const stop = () => {\n if (abortController) {\n abortController.abort();\n abortController = null;\n }\n };\n\n const setCompletion = (completion: string) => {\n mutate(completion);\n };\n\n const input = ref(initialInput);\n\n const handleSubmit = (event?: { preventDefault?: () => void }) => {\n event?.preventDefault?.();\n const inputValue = input.value;\n return inputValue ? complete(inputValue) : undefined;\n };\n\n return {\n completion,\n complete,\n error,\n stop,\n setCompletion,\n input,\n handleSubmit,\n isLoading,\n data: streamData,\n };\n}\n","/**\n * A vue.js composable function to interact with the assistant API.\n */\n\nimport { isAbortError } from '@ai-sdk/provider-utils';\nimport { readDataStream, generateId } from '@ai-sdk/ui-utils';\nimport type {\n AssistantStatus,\n CreateMessage,\n Message,\n UseAssistantOptions,\n} from '@ai-sdk/ui-utils';\nimport { computed, readonly, ref } from 'vue';\nimport type { ComputedRef, Ref } from 'vue';\n\nexport type UseAssistantHelpers = {\n /**\n * The current array of chat messages.\n */\n messages: Ref<Message[]>;\n\n /**\n * Update the message store with a new array of messages.\n */\n setMessages: (messagesProcessor: (messages: Message[]) => Message[]) => void;\n\n /**\n * The current thread ID.\n */\n threadId: Ref<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 * The current value of the input field.\n */\n input: Ref<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 /**\n * Abort the current request immediately, keep the generated tokens if any.\n */\n stop: ComputedRef<() => void>;\n\n /**\n * Handler for the `onChange` event of the input field to control the input's value.\n */\n handleInputChange: (e: Event & { target: HTMLInputElement }) => void;\n\n /**\n * Handler for the `onSubmit` event of the form to append a user message and reset the input.\n */\n handleSubmit: (e: Event & { target: HTMLFormElement }) => void;\n\n /**\n * Whether the assistant is currently sending a message.\n */\n isSending: ComputedRef<boolean>;\n\n /**\n * The current status of the assistant.\n */\n status: Ref<AssistantStatus>;\n\n /**\n * The current error, if any.\n */\n error: Ref<Error | undefined>;\n};\n\nexport function useAssistant({\n api,\n threadId: threadIdParam,\n credentials,\n headers,\n body,\n onError,\n}: UseAssistantOptions): UseAssistantHelpers {\n const messages: Ref<Message[]> = ref([]);\n const input: Ref<string> = ref('');\n const currentThreadId: Ref<string | undefined> = ref(undefined);\n const status: Ref<AssistantStatus> = ref('awaiting_message');\n const error: Ref<undefined | Error> = ref(undefined);\n\n const setMessages = (messageFactory: (messages: Message[]) => Message[]) => {\n messages.value = messageFactory(messages.value);\n };\n\n const setCurrentThreadId = (newThreadId: string | undefined) => {\n currentThreadId.value = newThreadId;\n messages.value = [];\n };\n\n const handleInputChange = (event: Event & { target: HTMLInputElement }) => {\n input.value = event?.target?.value;\n };\n\n const isSending = computed(() => status.value === 'in_progress');\n\n // Abort controller to cancel the current API call when required\n const abortController = ref<AbortController | null>(null);\n\n // memoized function to stop the current request when required\n const stop = computed(() => {\n return () => {\n if (abortController.value) {\n abortController.value.abort();\n abortController.value = null;\n }\n };\n });\n\n const append = async (\n message: Message | CreateMessage,\n requestOptions?: {\n data?: Record<string, string>;\n },\n ) => {\n status.value = 'in_progress';\n\n // Append the new message to the current list of messages\n const newMessage: Message = {\n ...message,\n id: message.id ?? generateId(),\n };\n\n // Update the messages list with the new message\n setMessages(messages => [...messages, newMessage]);\n\n input.value = '';\n\n const controller = new AbortController();\n\n try {\n // Assign the new controller to the abortController ref\n abortController.value = controller;\n\n const response = await fetch(api, {\n method: 'POST',\n headers: {\n 'Content-Type': 'application/json',\n ...headers,\n },\n body: JSON.stringify({\n ...body,\n // Message Content\n message: message.content,\n\n // Always Use User Provided Thread ID When Available\n threadId: threadIdParam ?? currentThreadId.value ?? null,\n\n // Optional Request Data\n ...(requestOptions?.data && { data: requestOptions?.data }),\n }),\n signal: controller.signal,\n credentials,\n });\n\n if (!response.ok) {\n throw new Error(\n response.statusText ?? 'An error occurred while sending the message',\n );\n }\n\n if (!response.body) {\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 messages.value = [\n ...messages.value,\n {\n id: value.id,\n content: value.content[0].text.value,\n role: value.role,\n },\n ];\n break;\n }\n case 'assistant_control_data': {\n if (value.threadId) {\n currentThreadId.value = value.threadId;\n }\n\n setMessages(messages => {\n const lastMessage = messages[messages.length - 1];\n lastMessage.id = value.messageId;\n\n return [...messages.slice(0, -1), lastMessage];\n });\n\n break;\n }\n\n case 'text': {\n setMessages(messages => {\n const lastMessage = messages[messages.length - 1];\n lastMessage.content += value;\n\n return [...messages.slice(0, -1), lastMessage];\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 'error': {\n error.value = new Error(value);\n }\n\n default: {\n console.error('Unknown message type:', type);\n break;\n }\n }\n }\n } catch (err) {\n // If the error is an AbortError and the signal is aborted, reset the abortController and do nothing.\n if (isAbortError(err) && abortController.value?.signal.aborted) {\n abortController.value = null;\n return;\n }\n\n // If an error handler is provided, call it with the error\n if (onError && err instanceof Error) {\n onError(err);\n }\n\n error.value = err as Error;\n } finally {\n // Reset the status to 'awaiting_message' after the request is complete\n abortController.value = null;\n status.value = 'awaiting_message';\n }\n };\n\n const submitMessage = async (\n event: Event & { target: HTMLFormElement },\n requestOptions?: {\n data?: Record<string, string>;\n },\n ) => {\n event?.preventDefault?.();\n\n if (!input.value) return;\n\n append(\n {\n role: 'user',\n content: input.value,\n },\n requestOptions,\n );\n };\n\n return {\n append,\n messages,\n setMessages,\n threadId: readonly(currentThreadId),\n setThreadId: setCurrentThreadId,\n input,\n handleInputChange,\n handleSubmit: submitMessage,\n isSending,\n status,\n error,\n stop,\n };\n}\n\n/**\n * @deprecated Use `useAssistant` instead.\n */\nexport const experimental_useAssistant = useAssistant;\n"],"mappings":";AAQA;AAAA,EACE;AAAA,EACA,cAAc;AAAA,EACd;AAAA,OACK;AACP,OAAO,UAAU;AAEjB,SAAS,KAAK,aAAa;AAmD3B,IAAI,WAAW;AAGf,IAAM,UAAW,KAAK,WAAgD;AACtE,IAAM,QAA+C,CAAC;AAE/C,SAAS,QAAQ;AAAA,EACtB,MAAM;AAAA,EACN;AAAA,EACA,kBAAkB,CAAC;AAAA,EACnB,eAAe;AAAA,EACf;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;A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AAO;AAAA,IACL;AAAA,IACA;AAAA,IACA;AAAA,IACA,UAAU,SAAS,eAAe;AAAA,IAClC,aAAa;AAAA,IACb;AAAA,IACA;AAAA,IACA,cAAc;AAAA,IACd;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF;AACF;AAKO,IAAM,4BAA4B;","names":["generateId","fetch","headers","body","_a","data","swrv","ref","unref","uniqueId","useSWRV","store","fetch","data","_a","completion","ref","messages","_a"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ai-sdk/vue",
3
- "version": "0.0.20",
3
+ "version": "0.0.22",
4
4
  "license": "Apache-2.0",
5
5
  "sideEffects": false,
6
6
  "main": "./dist/index.js",
@@ -15,8 +15,8 @@
15
15
  }
16
16
  },
17
17
  "dependencies": {
18
- "@ai-sdk/provider-utils": "0.0.14",
19
- "@ai-sdk/ui-utils": "0.0.16",
18
+ "@ai-sdk/provider-utils": "1.0.3",
19
+ "@ai-sdk/ui-utils": "0.0.18",
20
20
  "swrv": "1.0.4"
21
21
  },
22
22
  "devDependencies": {
@@ -0,0 +1,30 @@
1
+ <script setup lang="ts">
2
+ import { useChat } from './use-chat';
3
+
4
+ const { messages, handleSubmit, input } = useChat();
5
+ </script>
6
+
7
+ <template>
8
+ <div class="flex flex-col w-full max-w-md py-24 mx-auto stretch">
9
+ <div
10
+ v-for="(m, idx) in messages"
11
+ key="m.id"
12
+ :data-testid="`message-${idx}`"
13
+ >
14
+ {{ m.role === 'user' ? 'User: ' : 'AI: ' }}
15
+ {{ m.content }}
16
+ </div>
17
+
18
+ <form @submit.prevent="(event) => handleSubmit(event, {
19
+ allowEmptySubmit: true,
20
+ })">
21
+ <input
22
+ :data-testid="`do-input`"
23
+ v-model="input"
24
+ type="text"
25
+ placeholder="Type a message..."
26
+ />
27
+ </form>
28
+
29
+ </div>
30
+ </template>
package/src/use-chat.ts CHANGED
@@ -47,7 +47,9 @@ export type UseChatHelpers = {
47
47
  * edit the messages on the client, and then trigger the `reload` method
48
48
  * manually to regenerate the AI response.
49
49
  */
50
- setMessages: (messages: Message[]) => void;
50
+ setMessages: (
51
+ messages: Message[] | ((messages: Message[]) => Message[]),
52
+ ) => void;
51
53
  /** The current value of the input */
52
54
  input: Ref<string>;
53
55
  /** Form submission handler to automatically reset input and append a user message */
@@ -238,11 +240,6 @@ export function useChat({
238
240
  message.id = generateId();
239
241
  }
240
242
 
241
- const requestOptions = {
242
- headers: options?.headers ?? options?.options?.headers,
243
- body: options?.body ?? options?.options?.body,
244
- };
245
-
246
243
  return triggerRequest(messages.value.concat(message as Message), options);
247
244
  };
248
245
 
@@ -264,8 +261,14 @@ export function useChat({
264
261
  }
265
262
  };
266
263
 
267
- const setMessages = (messages: Message[]) => {
268
- mutate(messages);
264
+ const setMessages = (
265
+ messagesArg: Message[] | ((messages: Message[]) => Message[]),
266
+ ) => {
267
+ if (typeof messagesArg === 'function') {
268
+ messagesArg = messagesArg(messages.value);
269
+ }
270
+
271
+ mutate(messagesArg);
269
272
  };
270
273
 
271
274
  const input = ref(initialInput);
@@ -278,15 +281,17 @@ export function useChat({
278
281
 
279
282
  const inputValue = input.value;
280
283
 
284
+ if (!inputValue && !options.allowEmptySubmit) return;
285
+
281
286
  triggerRequest(
282
- inputValue
283
- ? messages.value.concat({
287
+ !inputValue && options.allowEmptySubmit
288
+ ? messages.value
289
+ : messages.value.concat({
284
290
  id: generateId(),
285
291
  createdAt: new Date(),
286
292
  content: inputValue,
287
293
  role: 'user',
288
- })
289
- : messages.value,
294
+ }),
290
295
  options,
291
296
  );
292
297
 
@@ -10,6 +10,7 @@ import TestChatComponent from './TestChatComponent.vue';
10
10
  import TestChatTextStreamComponent from './TestChatTextStreamComponent.vue';
11
11
  import TestChatFormComponent from './TestChatFormComponent.vue';
12
12
  import { formatStreamPart } from '@ai-sdk/ui-utils';
13
+ import TestChatFormComponentOptions from './TestChatFormComponentOptions.vue';
13
14
 
14
15
  describe('stream data stream', () => {
15
16
  beforeEach(() => {
@@ -143,6 +144,50 @@ describe('form actions', () => {
143
144
  cleanup();
144
145
  });
145
146
 
147
+ it('should show streamed response using handleSubmit', async () => {
148
+ mockFetchDataStream({
149
+ url: 'https://example.com/api/chat',
150
+ chunks: ['Hello', ',', ' world', '.'].map(token =>
151
+ formatStreamPart('text', token),
152
+ ),
153
+ });
154
+
155
+ const firstInput = screen.getByTestId('do-input');
156
+ await userEvent.type(firstInput, 'hi');
157
+ await userEvent.keyboard('{Enter}');
158
+
159
+ await screen.findByTestId('message-0');
160
+ expect(screen.getByTestId('message-0')).toHaveTextContent('User: hi');
161
+
162
+ await screen.findByTestId('message-1');
163
+ expect(screen.getByTestId('message-1')).toHaveTextContent(
164
+ 'AI: Hello, world.',
165
+ );
166
+
167
+ mockFetchDataStream({
168
+ url: 'https://example.com/api/chat',
169
+ chunks: ['How', ' can', ' I', ' help', ' you', '?'].map(token =>
170
+ formatStreamPart('text', token),
171
+ ),
172
+ });
173
+
174
+ const secondInput = screen.getByTestId('do-input');
175
+ await userEvent.type(secondInput, '{Enter}');
176
+
177
+ expect(screen.queryByTestId('message-2')).not.toBeInTheDocument();
178
+ });
179
+ });
180
+
181
+ describe('form actions (with options)', () => {
182
+ beforeEach(() => {
183
+ render(TestChatFormComponentOptions);
184
+ });
185
+
186
+ afterEach(() => {
187
+ vi.restoreAllMocks();
188
+ cleanup();
189
+ });
190
+
146
191
  it('should show streamed response using handleSubmit', async () => {
147
192
  mockFetchDataStream({
148
193
  url: 'https://example.com/api/chat',
@@ -177,5 +222,26 @@ describe('form actions', () => {
177
222
  expect(screen.getByTestId('message-2')).toHaveTextContent(
178
223
  'AI: How can I help you?',
179
224
  );
225
+
226
+ mockFetchDataStream({
227
+ url: 'https://example.com/api/chat',
228
+ chunks: ['The', ' sky', ' is', ' blue.'].map(token =>
229
+ formatStreamPart('text', token),
230
+ ),
231
+ });
232
+
233
+ const thirdInput = screen.getByTestId('do-input');
234
+ await userEvent.type(thirdInput, 'what color is the sky?');
235
+ await userEvent.keyboard('{Enter}');
236
+
237
+ await screen.findByTestId('message-3');
238
+ expect(screen.getByTestId('message-3')).toHaveTextContent(
239
+ 'User: what color is the sky?',
240
+ );
241
+
242
+ await screen.findByTestId('message-4');
243
+ expect(screen.getByTestId('message-4')).toHaveTextContent(
244
+ 'AI: The sky is blue.',
245
+ );
180
246
  });
181
247
  });