UNPKG

@mastra/core

Version:
93 lines • 4.81 kB
import type { IMastraLogger } from '../../logger/index.js'; import type { MemoryConfigInternal } from '../../memory/index.js'; import type { MastraMemory } from '../../memory/memory.js'; import type { MastraDBMessage, MessageList } from '../message-list/index.js'; export interface FlushMessagesOptions { /** * Runs inside the thread's save queue, immediately before unsaved messages * are drained. A rejection skips the save, leaves the messages unsaved, and * rejects the flush with that error. Unless throwOnError is enabled, storage * errors are logged, except a run fence conflict, which rejects like the guard. */ beforePersist?: () => Promise<void>; /** Reject this flush on storage failure without rejecting the shared queue. */ throwOnError?: boolean; } export declare class SaveQueueManager { private logger?; private debounceMs; private memory?; private onSaved?; private static MAX_STALENESS_MS; constructor({ logger, debounceMs, memory, onSaved, }: { logger?: IMastraLogger; debounceMs?: number; memory?: MastraMemory; /** Called with the rows memory stored after each successful save. */ onSaved?: (messages: MastraDBMessage[]) => void; }); private saveQueues; private saveDebounceTimers; /** * Debounces save operations for a thread, ensuring that consecutive save requests * are batched and only the latest is executed after a short delay. * @param threadId - The ID of the thread to debounce saves for. * @param messageList - The MessageList instance containing unsaved messages. * @param memoryConfig - Optional memory configuration to use for saving. * @returns A promise that resolves when the debounced save completes. */ private debounceSave; /** * Enqueues a save operation for a thread, ensuring that saves are executed in order and * only one save runs at a time per thread. If a save is already in progress for the thread, * the new save is queued to run after the previous completes. * * @param threadId - The ID of the thread whose messages should be saved. * @param messageList - The MessageList instance containing unsaved messages. * @param memoryConfig - Optional memory configuration to use for saving. * @param beforePersist - Optional guard; see {@link FlushMessagesOptions}. */ private enqueueSave; /** * Clears any pending debounced save for a thread, preventing the scheduled save * from executing if it hasn't already fired. * * @param threadId - The ID of the thread whose debounced save should be cleared. */ clearDebounce(threadId: string): void; /** * Persists any unsaved messages from the MessageList to memory storage. * Drains the list of unsaved messages and writes them using the memory backend. * @param messageList - The MessageList instance for the current thread. * @param memoryConfig - The memory configuration for saving. */ private persistUnsavedMessages; /** * Batches a save of unsaved messages for a thread, using debouncing to batch rapid updates. * If the oldest unsaved message is stale (older than MAX_STALENESS_MS), the save is performed immediately. * Otherwise, the save is delayed to batch multiple updates and reduce redundant writes. * * @param messageList - The MessageList instance containing unsaved messages. * @param threadId - The ID of the thread whose messages are being saved. * @param memoryConfig - Optional memory configuration for saving. */ batchMessages(messageList: MessageList, threadId?: string, memoryConfig?: MemoryConfigInternal): Promise<void>; /** * Forces an immediate save of unsaved messages for a thread, bypassing any debounce delay. * This is used when a flush to persistent storage is required (e.g., on shutdown or critical transitions). * * @param messageList - The MessageList instance containing unsaved messages. * @param threadId - The ID of the thread whose messages are being saved. * @param memoryConfig - Optional memory configuration for saving. * @param options - Optional {@link FlushMessagesOptions}. */ flushMessages(messageList: MessageList, threadId?: string, memoryConfig?: MemoryConfigInternal, options?: FlushMessagesOptions): Promise<void>; /** * Cancels any debounced save for a thread and waits for saves already in flight to finish, * so a caller can change stored messages without a queued save landing afterwards. * * @param threadId - The ID of the thread whose pending saves should settle. */ flushPending(threadId: string): Promise<void>; } //# sourceMappingURL=index.d.ts.map