UNPKG

@mastra/core

Version:

Mastra is a framework for building AI-powered applications and agents with a modern TypeScript stack.

142 lines 3.94 kB
import type { MastraServerCache } from '../cache/base.js'; /** * Options for creating a CachingTransformStream */ export interface CachingTransformStreamOptions<T> { /** * Cache instance for storing stream chunks */ cache: MastraServerCache; /** * Unique key for this stream's cache entries */ cacheKey: string; /** * Optional serializer for chunks before caching. * Defaults to identity (chunks stored as-is). */ serialize?: (chunk: T) => unknown; /** * Optional deserializer for chunks from cache. * Defaults to identity (chunks returned as-is). */ deserialize?: (cached: unknown) => T; } /** * Creates a TransformStream that caches all chunks passing through it. * * Use this for workflow streaming where you need resumable streams * without changing to a PubSub-based architecture. * * @example * ```typescript * const cache = mastra.getServerCache(); * const { transform, getHistory } = createCachingTransformStream({ * cache, * cacheKey: runId, * }); * * // Use the transform stream * const cachedStream = sourceStream.pipeThrough(transform); * * // Later, get cached history for replay * const history = await getHistory(); * ``` */ export declare function createCachingTransformStream<T>(options: CachingTransformStreamOptions<T>): { /** * TransformStream that caches chunks as they pass through */ transform: TransformStream<T, T>; /** * Get all cached chunks for this stream */ getHistory: (offset?: number) => Promise<T[]>; /** * Clear cached chunks for this stream */ clearCache: () => Promise<void>; }; /** * Creates a ReadableStream that first emits cached history, then pipes from a live source. * * Use this when a client reconnects and needs to receive missed chunks * before continuing with the live stream. * * @example * ```typescript * const cache = mastra.getServerCache(); * * // Get cached history * const history = await cache.listFromTo(runId, 0); * * // Create combined stream * const stream = createReplayStream({ * history: history as ChunkType[], * liveSource: workflow.stream(), * cache, * cacheKey: runId, * }); * ``` */ export declare function createReplayStream<T>(options: { /** * Cached chunks to emit first */ history: T[]; /** * Live stream to continue from after history */ liveSource: ReadableStream<T>; /** * Optional cache for continued caching of live chunks */ cache?: MastraServerCache; /** * Cache key for continued caching */ cacheKey?: string; /** * Optional serializer for caching */ serialize?: (chunk: T) => unknown; }): ReadableStream<T>; /** * Helper to create a caching transform and get history in one call. * * This is the recommended way to add caching to workflow streams. * * @example * ```typescript * const { pipeThrough, getHistory, clearCache } = withStreamCaching({ * cache: mastra.getServerCache(), * cacheKey: runId, * }); * * // Apply caching to a stream * const cachedStream = workflow.fullStream.pipeThrough(pipeThrough()); * * // On reconnect, get history and create replay stream * const history = await getHistory(); * const replayStream = createReplayStream({ * history, * liveSource: workflow.resumeStream(), * }); * ``` */ export declare function withStreamCaching<T>(options: CachingTransformStreamOptions<T>): { /** * Creates a new TransformStream that caches chunks. * Call this each time you need a new caching transform. */ pipeThrough: () => TransformStream<T, T>; /** * Get cached history for this stream */ getHistory: (offset?: number) => Promise<T[]>; /** * Clear the cache for this stream */ clearCache: () => Promise<void>; }; //# sourceMappingURL=caching-transform-stream.d.ts.map