import type { GenerationStats, LLMContextConfig, LLMGenerationConfig, ToolDefinition } from './specs/LLM.nitro'; /** Role of a chat message. */ export type ChatRole = 'system' | 'user' | 'assistant' | 'tool'; /** Lifecycle status of a tool call produced by the model. */ export type ChatToolCallStatus = 'pending' | 'executing' | 'completed' | 'failed'; /** Structured record describing a tool call made by the model. */ export interface ChatToolCall { id: string; name: string; arguments: Record; status: ChatToolCallStatus; result?: unknown; error?: string; startedAt: number; completedAt?: number; } interface BaseChatMessageFields { id: string; createdAt: number; } export interface SystemChatMessage extends BaseChatMessageFields { role: 'system'; content: string; } export interface UserChatMessage extends BaseChatMessageFields { role: 'user'; content: string; } export interface AssistantChatMessage extends BaseChatMessageFields { role: 'assistant'; content: string; thinking?: string; toolCalls?: ChatToolCall[]; stats?: GenerationStats; /** True while the message is still being streamed. */ isStreaming?: boolean; error?: string; } export interface ToolChatMessage extends BaseChatMessageFields { role: 'tool'; toolCallId: string; name: string; content: string; } /** Discriminated union over all chat message roles. */ export type ChatMessage = SystemChatMessage | UserChatMessage | AssistantChatMessage | ToolChatMessage; /** Message shape accepted when seeding or replacing history. `id` and `createdAt` are auto-filled. */ export type ChatMessageInit = { id?: string; createdAt?: number; } & (Omit | Omit | Omit | Omit); /** High-level state machine status surfaced to the UI. */ export type ChatSessionStatus = 'idle' | 'loading' | 'streaming' | 'tool_calling' | 'done' | 'error'; export interface ChatSessionState { status: ChatSessionStatus; isGenerating: boolean; isLoaded: boolean; modelId: string; /** Partial assistant content accumulated during the current stream. */ partialAssistantContent: string; /** Partial assistant thinking content accumulated during the current thinking block. */ partialAssistantThinking: string; /** Tool calls that are currently in-flight for the active turn. */ activeToolCalls: ChatToolCall[]; lastError: Error | null; lastStats: GenerationStats | null; } export interface ChatSessionOptions { modelId: string; systemPrompt?: string; initialMessages?: ChatMessageInit[]; tools?: ToolDefinition[]; generationConfig?: LLMGenerationConfig; contextConfig?: LLMContextConfig; tokenBatchSize?: number; /** Called on every state transition with the latest session snapshot. */ onUpdate?: (state: ChatSessionState) => void; /** Called when a new message is appended to history (user, assistant, or tool). */ onMessage?: (message: ChatMessage) => void; /** Called for each streamed token of assistant content. */ onToken?: (token: string) => void; /** Called on every tool-call lifecycle update (pending/executing/completed/failed). */ onToolCall?: (toolCall: ChatToolCall) => void; /** Called when generation or loading fails. */ onError?: (error: Error) => void; } export interface ChatLoadOptions { onProgress?: (progress: number) => void; } export interface SendMessageOptions { /** Per-call token callback, invoked in addition to the session-level onToken. */ onToken?: (token: string) => void; /** Per-call tool-call callback, invoked in addition to the session-level onToolCall. */ onToolCall?: (toolCall: ChatToolCall) => void; } export type ChatSessionListener = (state: ChatSessionState) => void; /** * High-level chat session built on top of the low-level `LLM` singleton. * * Maintains its own structured message history so the UI has a stable source * of truth, while delegating actual generation to the native MLX runtime. * * @remarks * The underlying `LLM` module is a singleton; only one session can actively * generate at a time. Creating multiple sessions against the same runtime is * allowed but callers must coordinate `load()` / `unload()` themselves. */ export declare class ChatSession { private readonly _options; private readonly _listeners; private _messages; private _state; private _systemPrompt; private _isLoaded; private _idCounter; constructor(options: ChatSessionOptions); /** Current messages in the session (copy — safe to mutate). */ get messages(): ChatMessage[]; /** Current session state snapshot. */ get state(): ChatSessionState; get status(): ChatSessionStatus; get isGenerating(): boolean; get isLoaded(): boolean; get modelId(): string; get systemPrompt(): string | undefined; /** Subscribe to state updates. Returns an unsubscribe function. */ subscribe(listener: ChatSessionListener): () => void; /** * Load the underlying MLX model and apply the session's system prompt, * tools, and any seeded initial messages as additional context. */ load(loadOptions?: ChatLoadOptions): Promise; /** Unload the underlying model. Safe to call repeatedly. */ unload(): void; /** * Update the system prompt. Takes effect immediately on the native side, * but behavior for already-generated turns is undefined — prefer setting * before `load()` or after `reset()`. */ setSystemPrompt(prompt: string): void; /** * Replace the JS-side message history. Does not alter native history — * call `reset()` first (or reload the session) when strict alignment is * required. */ setMessages(messages: ChatMessageInit[]): void; /** * Clear user/assistant/tool messages from both the JS and native history. * System messages seeded via `initialMessages` are preserved. */ clearHistory(): void; /** Full reset: clears history, errors, and transient stream state. */ reset(): void; /** Remove a message by id. Returns `true` if a message was removed. */ deleteMessage(id: string): boolean; /** Patch a message by id. Returns `true` if a message was updated. */ updateMessage(id: string, patch: Partial): boolean; /** Stop the current generation. No-op when idle. */ stop(): void; /** * Append a user message, stream a generation, and resolve with the final * assistant message. Throws if a generation is already in progress. */ sendMessage(content: string, options?: SendMessageOptions): Promise; private _buildAdditionalContext; private _pushToolMessage; private _createInitialState; private _normalizeMessage; private _nextId; private _setState; private _emitUpdate; private _handleError; } /** Factory helper that returns a new {@link ChatSession}. */ export declare function createChatSession(options: ChatSessionOptions): ChatSession; export {}; //# sourceMappingURL=chat.d.ts.map