UNPKG

@mastra/core

Version:
73 lines • 3.76 kB
/** * Error thrown when a Mastra execution budget (`modelSettings.timeout`) is exceeded. * * This is distinct from a caller-supplied abort so that timeouts can be reported as * failures rather than treated as clean cancellations. */ export type MastraTimeoutType = 'step' | 'total' | 'firstChunk'; export declare class MastraTimeoutError extends Error { readonly timeoutType: MastraTimeoutType; readonly timeoutMs: number; constructor({ timeoutType, timeoutMs }: { timeoutType: MastraTimeoutType; timeoutMs: number; }); } /** * How long a total deadline's memory save may hold back the deadline error. A save that takes longer * (a hung store) finishes in the background; the caller still sees the deadline on time. */ export declare const DEADLINE_SAVE_WAIT_MS = 1000; /** How long a durable run keeps its claim waiting for that save, so a hung store cannot block a later run with the same ID. */ export declare const DEADLINE_SAVE_CLAIM_HOLD_MS = 30000; /** Resolves when `promise` settles or `ms` passes, whichever is first. Never rejects. */ export declare function settledOrElapsed(promise: Promise<unknown>, ms: number): Promise<void>; export declare function isMastraTimeoutError(error: unknown): error is MastraTimeoutError; /** * Returns the abort reason of a signal, if it has been aborted. */ export declare function getAbortReason(signal?: AbortSignal): unknown; export type TimeoutAbortSignal = { /** The signal to pass downstream. Identical to `parentSignal` when no timeout is configured. */ signal: AbortSignal | undefined; /** Rejects with a `MastraTimeoutError` when the budget elapses. Undefined when no timeout is configured. */ timeoutPromise?: Promise<never>; /** Clears the timer and, unless `detachParentOnCleanup` is false, detaches the parent listener. Safe to call more than once. */ cleanup: () => void; }; /** * Composes a caller-supplied abort signal with a time budget. * * When `timeoutMs` is undefined this is a pass-through: the parent signal is returned * unchanged and no timer is created. * * Set `detachParentOnCleanup: false` when the parent signal lives no longer than the * derived signal and `cleanup` may run while consumers still hold the derived signal: * cleanup then only clears the timer, so a later parent abort still propagates. */ export declare function createTimeoutAbortSignal({ parentSignal, timeoutMs, timeoutType, detachParentOnCleanup, }: { parentSignal?: AbortSignal; timeoutMs?: number; timeoutType: MastraTimeoutType; detachParentOnCleanup?: boolean; }): TimeoutAbortSignal; /** * Rejects as soon as `signal` aborts, otherwise resolves with `promise`. * * Guards against providers that ignore `abortSignal` and would otherwise hang past * their budget. */ /** * Keeps a budget in force while a stream is consumed. * * Establishing a stream is rarely the slow part — a provider can open one and then stall * mid-emission — so the budget has to stay armed until the stream ends. Errors the stream * with the abort reason if the signal fires before it completes, and cancels the upstream * so the provider connection is not left dangling. * * `onSettled` runs exactly once, whether the stream completes, errors or aborts. */ export declare function guardStreamWithAbort<T>(stream: ReadableStream<T>, signal?: AbortSignal, onSettled?: () => void): ReadableStream<T>; export declare function guardStreamUntilMatch<T>(stream: ReadableStream<T>, signal: AbortSignal | undefined, matches: (chunk: T) => boolean, onMatchedOrSettled: () => void): ReadableStream<T>; export declare function raceAgainstAbort<T>(promise: Promise<T>, signal?: AbortSignal): Promise<T>; //# sourceMappingURL=timeout.d.ts.map