import { LanguageModelV3, LanguageModelV3CallOptions, LanguageModelV3GenerateResult, LanguageModelV3StreamResult, EmbeddingModelV3, EmbeddingModelV3CallOptions, EmbeddingModelV3Result, ProviderV3 } from '@ai-sdk/provider'; import { AppConfig, InitProgressReport, MLCEngineConfig, MLCEngineInterface } from '@mlc-ai/web-llm'; export { InitProgressReport as WebLLMProgress, WebWorkerMLCEngineHandler } from '@mlc-ai/web-llm'; import { UIMessage } from 'ai'; /** * UI message type for Browser AI features with custom data parts. * * Extends base UIMessage to include specific data part schemas * such as model download progress * * @example * // Import and use with useChat hook from @ai-sdk/react * ```typescript * import { useChat } from "@ai-sdk/react"; * import { WebLLMUIMessage } from "@browser-ai/web-llm"; * * const { messages, sendMessage } = useChat({ * onData: (dataPart) => { * if (dataPart.type === 'data-modelDownloadProgress') { * console.log(`Download: ${dataPart.data.progress}%`); * } * if (dataPart.type === 'data-notification') { * console.log(`${dataPart.data.level}: ${dataPart.data.message}`); * } * } * }); * ``` * * @see {@link https://v5.ai-sdk.dev/docs/reference/ai-sdk-ui/use-chat | useChat hook documentation} */ type WebLLMUIMessage = UIMessage< never, // No custom metadata type { /** * Model download progress data part for tracking browser AI model download status. * Used to display download progress bars and status messages to users. */ modelDownloadProgress: { /** Current download/initialization status */ status: "downloading" | "complete" | "error"; /** Download progress percentage (0-100), undefined for non-downloading states */ progress?: number; /** Human-readable status message to display to users */ message: string; }; /** * User notification data part for displaying temporary messages and alerts. * These are typically transient and not persisted in message history. */ notification: { /** The notification message text */ message: string; /** Notification severity level for styling and priority */ level: "info" | "warning" | "error"; }; } >; type Availability = | "unavailable" | "downloadable" | "downloading" | "available"; /** * Callback type for receiving model download/initialization progress updates. * @param progress - A value between 0 and 1 indicating completion. */ type DownloadProgressCallback = (progress: number) => void; declare global { interface Navigator { gpu?: GPU; } } /** * Check if the browser supports WebLLM (WebGPU) */ declare function doesBrowserSupportWebLLM(): boolean; type WebLLMModelId = string; interface WebLLMSettings { /** * Custom app configuration for WebLLM */ appConfig?: AppConfig; /** * Progress callback for model initialization */ initProgressCallback?: (progress: InitProgressReport) => void; /** * Engine configuration options */ engineConfig?: MLCEngineConfig; /** * A web worker instance to run the model in. * When provided, the model will run in a separate thread. * * @default undefined */ worker?: Worker; } declare class WebLLMLanguageModel implements LanguageModelV3 { readonly specificationVersion = "v3"; readonly modelId: WebLLMModelId; readonly provider = "web-llm"; private readonly config; private engine?; private isInitialized; private initializationPromise?; constructor(modelId: WebLLMModelId, options?: WebLLMSettings); readonly supportedUrls: Record; /** * Check if the model is initialized and ready to use * @returns true if the model is initialized, false otherwise */ get isModelInitialized(): boolean; private getEngine; private _initializeEngine; private getArgs; /** * Generates a complete text response using WebLLM * @param options * @returns Promise resolving to the generated content with finish reason, usage stats, and any warnings * @throws {LoadSettingError} When WebLLM is not available or model needs to be downloaded * @throws {UnsupportedFunctionalityError} When unsupported features like file input are used */ doGenerate(options: LanguageModelV3CallOptions): Promise; /** * Check the availability of the WebLLM model. * Note: On mobile devices with a worker, WebGPU detection is skipped since it * can't be done reliably. The actual availability will be determined at init. * @returns Promise resolving to "unavailable", "available", or "downloadable" */ availability(): Promise; /** * Creates an engine session with download progress monitoring. * * @example * ```typescript * const model = await model.createSessionWithProgress( * (progress) => { * console.log(`Download progress: ${Math.round(progress * 100)}%`); * } * ); * ``` * * @param onDownloadProgress Optional callback receiving progress values from 0 to 1 * @returns Promise resolving to the model instance * @throws {LoadSettingError} When WebLLM is not available or model is unavailable */ createSessionWithProgress(onDownloadProgress?: DownloadProgressCallback): Promise; /** * Generates a streaming text response using WebLLM * @param options * @returns Promise resolving to a readable stream of text chunks and request metadata * @throws {LoadSettingError} When WebLLM is not available or model needs to be downloaded * @throws {UnsupportedFunctionalityError} When unsupported features like file input are used */ doStream(options: LanguageModelV3CallOptions): Promise; } type WebLLMEmbeddingModelId = string; interface WebLLMEmbeddingSettings { /** * Custom app configuration for WebLLM */ appConfig?: AppConfig; /** * Progress callback for model initialization */ initProgressCallback?: (progress: InitProgressReport) => void; /** * Engine configuration options */ engineConfig?: MLCEngineConfig; /** * A web worker instance to run the model in. * When provided, the model will run in a separate thread. * * @default undefined */ worker?: Worker; /** * Maximum number of texts to embed in a single call. * @default 100 */ maxEmbeddingsPerCall?: number; } declare class WebLLMEmbeddingModel implements EmbeddingModelV3 { readonly specificationVersion = "v3"; readonly provider = "web-llm"; readonly modelId: WebLLMEmbeddingModelId; readonly maxEmbeddingsPerCall: number; readonly supportsParallelCalls = false; private readonly config; private engine?; private isInitialized; private initializationPromise?; constructor(modelId: WebLLMEmbeddingModelId, options?: WebLLMEmbeddingSettings); /** * Check if the model is initialized and ready to use */ get isModelInitialized(): boolean; private getEngine; private _initializeEngine; /** * Check the availability of the WebLLM embedding model * @returns Promise resolving to "unavailable", "available", or "downloadable" */ availability(): Promise; /** * Creates an engine session with download progress monitoring. * * @example * ```typescript * const engine = await model.createSessionWithProgress( * (progress) => { * console.log(`Download progress: ${Math.round(progress.progress * 100)}%`); * } * ); * ``` * * @param onInitProgress Optional callback receiving progress reports during model download * @returns Promise resolving to a configured WebLLM engine * @throws {LoadSettingError} When WebLLM isn't available or model is unavailable */ createSessionWithProgress(onInitProgress?: (progress: InitProgressReport) => void): Promise; /** * Embed texts using the WebLLM embedding model */ doEmbed(options: EmbeddingModelV3CallOptions): Promise; } interface WebLLMProvider extends ProviderV3 { (modelId: WebLLMModelId, settings?: WebLLMSettings): WebLLMLanguageModel; /** * Creates a model for text generation. */ languageModel(modelId: WebLLMModelId, settings?: WebLLMSettings): WebLLMLanguageModel; /** * Creates a model for text generation. */ chat(modelId: WebLLMModelId, settings?: WebLLMSettings): WebLLMLanguageModel; /** * Creates a model for text embeddings. */ embedding(modelId: WebLLMEmbeddingModelId, settings?: WebLLMEmbeddingSettings): EmbeddingModelV3; /** * Creates a model for text embeddings. */ embeddingModel: (modelId: WebLLMEmbeddingModelId, settings?: WebLLMEmbeddingSettings) => EmbeddingModelV3; } /** * Create a WebLLM provider instance. */ declare function createWebLLM(): WebLLMProvider; /** * Default WebLLM provider instance * * @example * ```typescript * import { webLLM } from "@browser-ai/web-llm"; * * // Language model * const chat = webLLM("Llama-3.2-3B-Instruct-q4f16_1-MLC"); * * // Embedding model * const embed = webLLM.embeddingModel("snowflake-arctic-embed-m-q0f32-MLC-b32"); * ``` */ declare const webLLM: WebLLMProvider; export { WebLLMEmbeddingModel, type WebLLMEmbeddingModelId, type WebLLMEmbeddingSettings, WebLLMLanguageModel, type WebLLMModelId, type WebLLMProvider, type WebLLMSettings, type WebLLMUIMessage, createWebLLM, doesBrowserSupportWebLLM, webLLM };