@ai-sdk/vue
Version:
[Vue.js](https://vuejs.org/) UI components for the [AI SDK](https://ai-sdk.dev/docs):
143 lines (136 loc) • 5.67 kB
TypeScript
import { FetchFunction, FlexibleSchema as FlexibleSchema$1 } from '@ai-sdk/provider-utils';
import { FlexibleSchema, DeepPartial, InferSchema, CompletionRequestOptions, UseCompletionOptions, UIMessage, AbstractChat, ChatInit, ChatStatus } from 'ai';
export { UseCompletionOptions } from 'ai';
import { Ref, ComputedRef, ShallowRef, MaybeRefOrGetter } from 'vue';
type UseObjectOptions<SCHEMA extends FlexibleSchema, RESULT> = {
/** API endpoint that streams JSON chunks matching the schema */
api: string;
/** Schema that defines the final object shape */
schema: SCHEMA;
/** Shared state key. If omitted a random one is generated */
id?: string;
/** Initial partial value */
initialValue?: DeepPartial<RESULT>;
/** Optional custom fetch implementation */
fetch?: FetchFunction;
/** Called when stream ends */
onFinish?: (event: {
object: RESULT | undefined;
error: Error | undefined;
}) => Promise<void> | void;
/** Called on error */
onError?: (error: Error) => void;
/** Extra request headers */
headers?: Record<string, string> | Headers;
/** Request credentials mode. Defaults to 'same-origin' if omitted */
credentials?: RequestCredentials;
};
type UseObjectHelpers<RESULT, INPUT> = {
/** POST the input and start streaming */
submit: (input: INPUT) => void;
/** Current partial object, updated as chunks arrive */
object: Ref<DeepPartial<RESULT> | undefined>;
/** Latest error if any */
error: Ref<Error | undefined>;
/** Loading flag for the in-flight request */
isLoading: Ref<boolean | undefined>;
/** Abort the current request. Keeps current partial object. */
stop: () => void;
/** Abort and clear all state */
clear: () => void;
};
declare function useObject<SCHEMA extends FlexibleSchema, RESULT = InferSchema<SCHEMA>, INPUT = any>({ api, id, schema, initialValue, fetch, onError, onFinish, headers, credentials, }: UseObjectOptions<SCHEMA, RESULT>): UseObjectHelpers<RESULT, INPUT>;
type UseCompletionHelpers = {
/** The current completion result */
completion: Ref<string>;
/** The error object of the API request */
error: Ref<undefined | Error>;
/**
* Send a new prompt to the API endpoint and update the completion state.
*/
complete: (prompt: string, options?: CompletionRequestOptions) => Promise<string | null | undefined>;
/**
* Abort the current API request but keep the generated tokens.
*/
stop: () => void;
/**
* Update the `completion` state locally.
*/
setCompletion: (completion: string) => void;
/** The current value of the input */
input: Ref<string>;
/**
* Form submission handler to automatically reset input and append a user message
* @example
* ```jsx
* <form @submit="handleSubmit">
* <input @change="handleInputChange" v-model="input" />
* </form>
* ```
*/
handleSubmit: (event?: {
preventDefault?: () => void;
}) => void;
/** Whether the API request is in progress */
isLoading: Ref<boolean | undefined>;
};
declare function useCompletion({ api, id, initialCompletion, initialInput, credentials, headers, body, streamProtocol, onFinish, onError, fetch, }?: UseCompletionOptions): UseCompletionHelpers;
/**
* @deprecated Use the {@link useChat} composable instead. It exposes reactive
* refs and automatically recreates the chat when its init object changes.
*/
declare class Chat<UI_MESSAGE extends UIMessage> extends AbstractChat<UI_MESSAGE> {
constructor({ messages, ...init }: ChatInit<UI_MESSAGE>);
}
/**
* Return type of the {@link useChat} composable, which includes the chat
* instance methods and reactive properties for messages, status, and error.
*/
interface UseChatHelpers<UI_MESSAGE extends UIMessage> extends Pick<AbstractChat<UI_MESSAGE>, 'sendMessage' | 'regenerate' | 'stop' | 'resumeStream' | 'addToolOutput' | 'addToolApprovalResponse' | 'clearError'> {
/**
* The id of the chat.
*/
id: ComputedRef<string>;
/**
* The current error state of the chat, if any.
*/
error: ShallowRef<Error | undefined>;
/**
* The current status of the chat, which can be 'ready', 'generating', 'streaming', or 'error'.
*/
status: ShallowRef<ChatStatus>;
/**
* The list of messages in the chat, which can be updated by the chat instance methods or directly by setting this property.
*/
messages: ShallowRef<UI_MESSAGE[]>;
}
/**
* Composable to access messages, status, and other chat properties and
* methods. Accepts an optional reactive initial configuration object
*
* @example
*
* ```ts
* // passing a getter if any reactive properties are used within
* // the init object
* const { messages, sendMessage } = useChat(() => ({
* // ...
* })
* ```
*
* @see BaseChatInit
*/
declare function useChat<UI_MESSAGE extends UIMessage = UIMessage>(init?: MaybeRefOrGetter<ChatInit<UI_MESSAGE>>): UseChatHelpers<UI_MESSAGE>;
/**
* @deprecated Use `useObject` instead.
*/
declare const experimental_useObject: typeof useObject;
/**
* @deprecated Use `UseObjectOptions` instead.
*/
type Experimental_UseObjectOptions<SCHEMA extends FlexibleSchema$1, RESULT> = UseObjectOptions<SCHEMA, RESULT>;
/**
* @deprecated Use `UseObjectHelpers` instead.
*/
type Experimental_UseObjectHelpers<RESULT, INPUT> = UseObjectHelpers<RESULT, INPUT>;
export { Chat, Experimental_UseObjectHelpers, Experimental_UseObjectOptions, UseChatHelpers, UseCompletionHelpers, UseObjectHelpers, UseObjectOptions, experimental_useObject, useChat, useCompletion, useObject };