/**
 * Stream lifecycle management utilities.
 *
 * Provides utilities for wrapping streams with lifecycle hooks, particularly
 * `onFinally` which runs after stream consumption completes.
 *
 * Key challenge: The `streamText()` function returns immediately, but the
 * streams are consumed asynchronously later. We need to track when consumption
 * actually completes to run cleanup logic.
 *
 * Solution: Wrap streams with `TransformStream` that tracks consumption via
 * the `flush()` method, which runs when the input stream closes.
 */
import type { Tool } from "../../agent/tools2/types.js";
import type { StreamTextResult } from "./provider.js";
/**
 * Wraps a StreamTextResult to add onFinally lifecycle hook.
 *
 * The onFinally callback runs after stream consumption completes, handling:
 * - Normal completion (stream exhausted)
 * - Errors during consumption
 * - Stream cancellation/abandonment
 *
 * Guarantees:
 * - Runs exactly once, even if multiple stream accessors are used
 * - Idempotent (safe to call multiple times)
 * - Works with all consumption patterns (iteration, promises, piping)
 *
 * @param result - The stream result to wrap
 * @param onFinally - Callback to run after consumption
 * @returns Wrapped result with lifecycle tracking
 *
 * @example
 * ```typescript
 * const result = await streamText({ model, messages });
 * const wrapped = withStreamLifecycle(result, async () => {
 *   console.log('Stream consumed!');
 *   await cleanup();
 * });
 *
 * for await (const text of wrapped.textStream) {
 *   console.log(text);
 * }
 * // onFinally runs here ^
 * ```
 */
export declare function withStreamLifecycle<TOOLS extends Record<string, Tool> = Record<string, Tool>>(result: StreamTextResult<TOOLS>, onFinally: () => Promise<void> | void): StreamTextResult<TOOLS>;
//# sourceMappingURL=stream-lifecycle.d.ts.map