import type { EmissionEvent } from 'event-emission'; import { type TruncateOptions } from './context'; import type { RedactMessageOptions } from './conversation/index'; import { type ConversationEnvironment } from './environment'; import type { Conversation, ConversationHistorySnapshot, JSONValue, Message, MessageInput, TokenUsage } from './types'; /** * Event detail for conversation history changes. */ export interface ConversationHistoryEventDetail { type: ConversationHistoryActionType; conversation: Conversation; } export type ConversationHistoryEvent = EmissionEvent; type ConversationHistoryActionType = 'push' | 'undo' | 'redo' | 'switch'; type ConversationHistoryEventType = 'change' | ConversationHistoryActionType; /** * Manages a stack of conversation versions to support undo, redo, and branching. */ export declare class ConversationHistory extends EventTarget { private currentNode; private environment; private readonly events; constructor(initial?: Conversation, environment?: Partial); /** * Dispatches a change event. */ private notifyChange; private toAddListenerOptions; private toRemoveListenerOptions; /** * Overrides addEventListener to optionally return an unsubscribe function. * This is a convenience for modern frontend frameworks. */ addEventListener(type: string, callback: ((event: ConversationHistoryEvent) => void) | EventListenerOrEventListenerObject | null, options?: boolean | AddEventListenerOptions): void | (() => void); /** * Removes a listener registered with addEventListener. */ removeEventListener(type: string, callback: ((event: ConversationHistoryEvent) => void) | EventListenerOrEventListenerObject | null, options?: boolean | EventListenerOptions): void; /** * Dispatches a DOM-style event through the event-emission target. */ dispatchEvent(event: Event): boolean; /** * Subscribes to conversation changes. * This follows the Svelte store contract, making ConversationHistory a valid Svelte store. * @param run - Callback called with the current conversation whenever it changes. * @returns An unsubscribe function. */ subscribe(run: (value: Conversation) => void): () => void; /** * Returns the current conversation. * Useful for useSyncExternalStore in React. */ getSnapshot(): Conversation; /** * The current conversation state. */ get current(): Conversation; /** * Returns the message IDs for the current conversation. */ get ids(): string[]; /** * Whether an undo operation is possible. */ get canUndo(): boolean; /** * Whether a redo operation is possible. */ get canRedo(): boolean; /** * Returns the environment associated with this history. */ get env(): ConversationEnvironment; /** * Returns the number of branches available at the current level. */ get branchCount(): number; /** * Returns the index of the current branch at this level. */ get branchIndex(): number; /** * Returns the number of alternate paths available from the current state. */ get redoCount(): number; /** * Pushes a new conversation state onto the history. * If the current state is not a leaf, a new branch is created. */ push(next: Conversation): void; /** * Reverts to the previous conversation state. * @returns The conversation state after undo, or undefined if not possible. */ undo(): Conversation | undefined; /** * Advances to the next conversation state. * @param childIndex - The index of the branch to follow (default: 0). * @returns The conversation state after redo, or undefined if not possible. */ redo(childIndex?: number): Conversation | undefined; /** * Switches to a different branch at the current level. * @param index - The index of the sibling branch to switch to. * @returns The new conversation state, or undefined if not possible. */ switchToBranch(index: number): Conversation | undefined; /** * Returns the sequence of conversations from root to current. */ getPath(): Conversation[]; /** * Returns messages from the current conversation. */ getMessages(options?: { includeHidden?: boolean; }): ReadonlyArray; /** * Returns the message at the specified position. */ getMessageAtPosition(position: number): Message | undefined; /** * Returns all message IDs for the current conversation in order. */ getMessageIds(): string[]; /** * Returns the message with the specified ID, if present. */ getMessageById(id: string): Message | undefined; /** * Shorthand for getMessageById. */ get(id: string): Message | undefined; /** * Filters messages using a predicate. */ searchMessages(predicate: (m: Message) => boolean): Message[]; /** * Computes basic statistics for the current conversation. */ getStatistics(): { total: number; byRole: Record; hidden: number; withImages: number; }; /** * Returns true if any system message exists in the current conversation. */ hasSystemMessage(): boolean; /** * Returns the first system message in the current conversation, if any. */ getFirstSystemMessage(): Message | undefined; /** * Returns all system messages in the current conversation. */ getSystemMessages(): ReadonlyArray; /** * Converts the current conversation to external chat message format. */ toChatMessages(): import("@lasercat/homogenaize").Message[]; /** * Estimates tokens for the current conversation. */ estimateTokens(estimator?: (message: Message) => number): number; /** * Returns the most recent messages, with optional filtering. */ getRecentMessages(count: number, options?: { includeHidden?: boolean; includeSystem?: boolean; preserveToolPairs?: boolean; }): ReadonlyArray; /** * Returns the current streaming message, if any. */ getStreamingMessage(): Message | undefined; /** * Appends one or more messages to the history. */ appendMessages(...inputs: MessageInput[]): void; /** * Appends a user message to the history. */ appendUserMessage(content: MessageInput['content'], metadata?: Record): void; /** * Appends an assistant message to the history. */ appendAssistantMessage(content: MessageInput['content'], metadata?: Record): void; /** * Appends a system message to the history. */ appendSystemMessage(content: string, metadata?: Record): void; /** * Prepends a system message to the history. */ prependSystemMessage(content: string, metadata?: Record): void; /** * Replaces the first system message or prepends one if none exist. */ replaceSystemMessage(content: string, metadata?: Record): void; /** * Collapses multiple system messages into a single message. */ collapseSystemMessages(): void; /** * Redacts the message at the given position. */ redactMessageAtPosition(position: number, placeholderOrOptions?: string | RedactMessageOptions): void; /** * Truncates the conversation from a specific position. */ truncateFromPosition(position: number, options?: { preserveSystemMessages?: boolean; preserveToolPairs?: boolean; }): void; /** * Truncates the conversation to fit within a token limit. */ truncateToTokenLimit(maxTokens: number, options?: TruncateOptions): void; /** * Appends a streaming message placeholder and returns its ID. */ appendStreamingMessage(role: 'assistant' | 'user', metadata?: Record): string; /** * Updates a streaming message's content. */ updateStreamingMessage(messageId: string, content: string): void; /** * Finalizes a streaming message and optionally adds metadata or token usage. */ finalizeStreamingMessage(messageId: string, options?: { tokenUsage?: TokenUsage; metadata?: Record; }): void; /** * Cancels a streaming message by removing it from the conversation. */ cancelStreamingMessage(messageId: string): void; /** * Captures the entire history tree and current state in a plain snapshot. */ snapshot(): ConversationHistorySnapshot; /** * Reconstructs a ConversationHistory instance from JSON. */ static from(json: ConversationHistorySnapshot, environment?: Partial): ConversationHistory; /** * Binds a function to this history instance. * The first argument of the function must be a Conversation. * If the function returns a new Conversation, it is automatically pushed to the history. */ bind(fn: (conversation: Conversation, ...args: [...T, Partial?]) => R): (...args: T) => R; /** * Cleans up all listeners and resources. */ [Symbol.dispose](): void; } export {}; //# sourceMappingURL=history.d.ts.map