import { ChatCompletionRequestMessageFunctionCall, CreateChatCompletionRequestFunctionCall } from 'openai-edge'; import { ChatCompletionFunctions } from 'openai-edge/types/api'; interface FunctionCallPayload { name: string; arguments: Record; } /** * Helper callback methods for AIStream stream lifecycle events * @interface */ interface AIStreamCallbacks { onStart?: () => Promise; onCompletion?: (completion: string) => Promise; onToken?: (token: string) => Promise; } /** * Custom parser for AIStream data. * @interface */ interface AIStreamParser { (data: string): string | void; } /** * Creates a TransformStream that parses events from an EventSource stream using a custom parser. * @param {AIStreamParser} customParser - Function to handle event data. * @returns {TransformStream} TransformStream parsing events. */ declare function createEventStreamTransformer(customParser: AIStreamParser): TransformStream; /** * Creates a transform stream that encodes input messages and invokes optional callback functions. * The transform stream uses the provided callbacks to execute custom logic at different stages of the stream's lifecycle. * - `onStart`: Called once when the stream is initialized. * - `onToken`: Called for each tokenized message. * - `onCompletion`: Called once when the stream is flushed, with the aggregated messages. * * This function is useful when you want to process a stream of messages and perform specific actions during the stream's lifecycle. * * @param {AIStreamCallbacks} [callbacks] - An object containing the callback functions. * @return {TransformStream} A transform stream that encodes input messages as Uint8Array and allows the execution of custom logic through callbacks. * * @example * const callbacks = { * onStart: async () => console.log('Stream started'), * onToken: async (token) => console.log(`Token: ${token}`), * onCompletion: async (completion) => console.log(`Completion: ${completion}`) * }; * const transformer = createCallbacksTransformer(callbacks); */ declare function createCallbacksTransformer(callbacks: AIStreamCallbacks | undefined): TransformStream; /** * Returns a stateful function that, when invoked, trims leading whitespace * from the input text. The trimming only occurs on the first invocation, ensuring that * subsequent calls do not alter the input text. This is particularly useful in scenarios * where a text stream is being processed and only the initial whitespace should be removed. * * @return {function(string): string} A function that takes a string as input and returns a string * with leading whitespace removed if it is the first invocation; otherwise, it returns the input unchanged. * * @example * const trimStart = trimStartOfStreamHelper(); * const output1 = trimStart(" text"); // "text" * const output2 = trimStart(" text"); // " text" * */ declare function trimStartOfStreamHelper(): (text: string) => string; /** * Returns a ReadableStream created from the response, parsed and handled with custom logic. * The stream goes through two transformation stages, first parsing the events and then * invoking the provided callbacks. * * For 2xx HTTP responses: * - The function continues with standard stream processing. * * For non-2xx HTTP responses: * - If the response body is defined, it asynchronously extracts and decodes the response body. * - It then creates a custom ReadableStream to propagate a detailed error message. * * @param {Response} response - The response. * @param {AIStreamParser} customParser - The custom parser function. * @param {AIStreamCallbacks} callbacks - The callbacks. * @return {ReadableStream} The AIStream. * @throws Will throw an error if the response is not OK. */ declare function AIStream(response: Response, customParser: AIStreamParser, callbacks?: AIStreamCallbacks): ReadableStream; /** * Shared types between the API and UI packages. */ type Message = { id: string; createdAt?: Date; content: string; role: 'system' | 'user' | 'assistant' | 'function'; /** * If the message has a role of `function`, the `name` field is the name of the function. * Otherwise, the name field should not be set. */ name?: string; /** * If the assistant role makes a function call, the `function_call` field * contains the function call name and arguments. Otherwise, the field should * not be set. */ function_call?: string | ChatCompletionRequestMessageFunctionCall; }; type CreateMessage = Omit & { id?: Message['id']; }; type ChatRequest = { messages: Message[]; options?: RequestOptions; functions?: Array; function_call?: CreateChatCompletionRequestFunctionCall; }; type FunctionCallHandler = (chatMessages: Message[], functionCall: ChatCompletionRequestMessageFunctionCall) => Promise; type RequestOptions = { headers?: Record | Headers; body?: object; }; type ChatRequestOptions = { options?: RequestOptions; functions?: Array; function_call?: CreateChatCompletionRequestFunctionCall; }; type UseChatOptions = { /** * The API endpoint that accepts a `{ messages: Message[] }` object and returns * a stream of tokens of the AI chat response. Defaults to `/api/chat`. */ api?: string; /** * A unique identifier for the chat. If not provided, a random one will be * generated. When provided, the `useChat` hook with the same `id` will * have shared states across components. */ id?: string; /** * Initial messages of the chat. Useful to load an existing chat history. */ initialMessages?: Message[]; /** * Initial input of the chat. */ initialInput?: string; /** * Callback function to be called when a function call is received. * If the function returns a `ChatRequest` object, the request will be sent * automatically to the API and will be used to update the chat. */ experimental_onFunctionCall?: FunctionCallHandler; /** * Callback function to be called when the API response is received. */ onResponse?: (response: Response) => void | Promise; /** * Callback function to be called when the chat is finished streaming. */ onFinish?: (message: Message) => void; /** * Callback function to be called when an error is encountered. */ onError?: (error: Error) => void; /** * The credentials mode to be used for the fetch request. * Possible values are: 'omit', 'same-origin', 'include'. * Defaults to 'same-origin'. */ credentials?: RequestCredentials; /** * HTTP headers to be sent with the API request. */ headers?: Record | Headers; /** * Extra body object to be sent with the API request. * @example * Send a `sessionId` to the API along with the messages. * ```js * useChat({ * body: { * sessionId: '123', * } * }) * ``` */ body?: object; /** * Whether to send extra message fields such as `message.id` and `message.createdAt` to the API. * Defaults to `false`. When set to `true`, the API endpoint might need to * handle the extra fields before forwarding the request to the AI service. */ sendExtraMessageFields?: boolean; }; type UseCompletionOptions = { /** * The API endpoint that accepts a `{ prompt: string }` object and returns * a stream of tokens of the AI completion response. Defaults to `/api/completion`. */ api?: string; /** * An unique identifier for the chat. If not provided, a random one will be * generated. When provided, the `useChat` hook with the same `id` will * have shared states across components. */ id?: string; /** * Initial prompt input of the completion. */ initialInput?: string; /** * Initial completion result. Useful to load an existing history. */ initialCompletion?: string; /** * Callback function to be called when the API response is received. */ onResponse?: (response: Response) => void | Promise; /** * Callback function to be called when the completion is finished streaming. */ onFinish?: (prompt: string, completion: string) => void; /** * Callback function to be called when an error is encountered. */ onError?: (error: Error) => void; /** * The credentials mode to be used for the fetch request. * Possible values are: 'omit', 'same-origin', 'include'. * Defaults to 'same-origin'. */ credentials?: RequestCredentials; /** * HTTP headers to be sent with the API request. */ headers?: Record | Headers; /** * Extra body object to be sent with the API request. * @example * Send a `sessionId` to the API along with the prompt. * ```js * useChat({ * body: { * sessionId: '123', * } * }) * ``` */ body?: object; }; type JSONValue = null | string | number | boolean | { [x: string]: JSONValue; } | Array; type OpenAIStreamCallbacks = AIStreamCallbacks & { /** * @example * ```js * const response = await openai.createChatCompletion({ * model: 'gpt-3.5-turbo-0613', * stream: true, * messages, * functions, * }) * * const stream = OpenAIStream(response, { * experimental_onFunctionCall: async (functionCallPayload, createFunctionCallMessages) => { * // ... run your custom logic here * const result = await myFunction(functionCallPayload) * * // Ask for another completion, or return a string to send to the client as an assistant message. * return await openai.createChatCompletion({ * model: 'gpt-3.5-turbo-0613', * stream: true, * // Append the relevant "assistant" and "function" call messages * messages: [...messages, ...createFunctionCallMessages(result)], * functions, * }) * } * }) * ``` */ experimental_onFunctionCall?: (functionCallPayload: FunctionCallPayload, createFunctionCallMessages: (functionCallResult: JSONValue) => CreateMessage[]) => Promise; }; declare function OpenAIStream(res: Response, callbacks?: OpenAIStreamCallbacks): ReadableStream; export { AIStreamCallbacks as A, CreateMessage as C, FunctionCallPayload as F, Message as M, OpenAIStreamCallbacks as O, RequestOptions as R, UseChatOptions as U, AIStreamParser as a, createCallbacksTransformer as b, createEventStreamTransformer as c, AIStream as d, OpenAIStream as e, ChatRequest as f, FunctionCallHandler as g, ChatRequestOptions as h, UseCompletionOptions as i, trimStartOfStreamHelper as t };