/**
 * High-level provider interface for LLM streaming interactions.
 *
 * This interface defines a complete, SDK-agnostic contract for multi-step
 * LLM streaming, including all lifecycle hooks. Concrete implementations
 * (adapters) bridge this interface to specific SDKs (Vercel AI, Anthropic, etc.).
 *
 * Key features:
 * - Complete lifecycle: prepareStep → onChunk → onStepFinish → onFinish → onFinally
 * - onFinally: Guaranteed to run after stream consumption (our addition!)
 * - SDK-agnostic: Could be implemented with any LLM SDK
 * - Testable: Easy to mock for testing
 */
import type { Tool } from "../../agent/tools2/types.js";
import type { StopCondition } from "../../util/stop-condition.js";
import type { LanguageModelV2 } from "@ai-sdk/provider";
import type { ProviderOptions } from "@ai-sdk/provider-utils";
import type { TextStreamPart, LanguageModelUsage, ModelMessage, UserModelMessage, ToolChoice } from "ai";
/**
 * Provider interface for LLM streaming interactions.
 *
 * Implementations of this interface wrap specific SDKs and provide
 * a consistent, high-level API with complete lifecycle management.
 */
export interface StreamTextProvider {
    /**
     * Streams text from an LLM with multi-step tool calling support.
     *
     * @param options - Configuration and lifecycle hooks
     * @returns Promise resolving to stream result with text/full streams
     */
    streamText<TOOLS extends Record<string, Tool> = Record<string, Tool>>(options: StreamTextOptions<TOOLS>): Promise<StreamTextResult<TOOLS>>;
}
/**
 * Options for a streamText call.
 *
 * Includes model configuration, messages, tools, and comprehensive
 * lifecycle hooks including onFinally (runs after stream consumption).
 */
export interface StreamTextOptions<TOOLS extends Record<string, Tool>> {
    /** The language model to use */
    model: LanguageModelV2;
    /**
     * Conversation messages.
     * Note: When using context middleware, this will be populated automatically
     * from context history. You typically provide user instead.
     */
    messages?: ModelMessage[];
    /**
     * User message for this interaction.
     * When using context middleware, this is added to the conversation history
     * and messages are populated automatically from context.
     */
    user?: UserModelMessage;
    /** Available tools the model can call */
    tools?: TOOLS;
    /**
     * System prompt.
     * When using context middleware, this is set on the context.
     */
    system?: string;
    /** Maximum number of automatic tool call steps */
    maxSteps?: number;
    /** Maximum number of tokens to generate */
    maxOutputTokens?: number;
    /** Abort signal for cancellation */
    abortSignal?: AbortSignal;
    /** Custom headers for the request */
    headers?: Record<string, string | undefined>;
    /** Provider-specific options */
    providerOptions?: ProviderOptions;
    /**
     * Vercel AI SDK specific: Condition to stop generation early.
     * Pass through to the underlying SDK.
     */
    stopWhen?: StopCondition<any> | StopCondition<any>[];
    /**
     * Vercel AI SDK specific: Transformation stream for the output.
     * Pass through to the underlying SDK.
     */
    experimental_transform?: any;
    /**
     * Called before each LLM step.
     * Can transform the step parameters before execution.
     */
    prepareStep?: (step: StepParameters<TOOLS>) => Promise<StepParameters<TOOLS>> | StepParameters<TOOLS>;
    /**
     * Called for each chunk in the stream during generation.
     */
    onChunk?: (event: {
        chunk: StreamChunk;
    }) => Promise<void> | void;
    /**
     * Called after each step completes (after tool execution).
     */
    onStepFinish?: (step: StepResult<TOOLS>) => Promise<void> | void;
    /**
     * Called when the entire multi-step interaction finishes.
     * This fires after generation completes, before stream consumption.
     */
    onFinish?: (result: FinalResult<TOOLS>) => Promise<void> | void;
    /**
     * Called after stream consumption completes.
     *
     * This is the key lifecycle hook for cleanup operations:
     * - Releasing locks (context handles)
     * - Finishing tracing spans
     * - Closing database connections
     * - Saving final artifacts
     *
     * Guarantees:
     * - Runs exactly once, even if multiple stream accessors are used
     * - Runs after the stream is fully consumed OR abandoned
     * - Runs in error cases as well (usage may be partial)
     * - Idempotent and safe to call multiple times
     *
     */
    onFinally?: () => Promise<void> | void;
    /**
     * Called if an error occurs during generation or consumption.
     */
    onError?: (error: Error) => Promise<void> | void;
    /**
     * Called if the operation is aborted via the abortSignal.
     */
    onAbort?: () => Promise<void> | void;
}
/**
 * Parameters for a single LLM step.
 */
export interface StepParameters<TOOLS extends Record<string, Tool>> {
    messages: ModelMessage[];
    stepNumber: number;
    toolChoice?: ToolChoice<TOOLS>;
    activeTools?: string[];
}
/**
 * A chunk from the streaming response.
 * This is a direct re-export of Vercel AI SDK's TextStreamPart.
 */
export type StreamChunk = TextStreamPart<Record<string, Tool>>;
import type { StepResult as VercelStepResult } from "ai";
/**
 * Result of a single LLM step.
 * Re-exported from Vercel AI SDK to avoid type incompatibilities.
 */
export type StepResult<TOOLS extends Record<string, Tool>> = VercelStepResult<TOOLS>;
/**
 * Final result of the entire multi-step interaction.
 * This is the result passed to onFinish callback.
 * Re-exported from Vercel AI SDK's internal type.
 */
export type FinalResult<TOOLS extends Record<string, Tool>> = StepResult<TOOLS> & {
    readonly steps: StepResult<TOOLS>[];
    readonly totalUsage: LanguageModelUsage;
};
import type { AsyncIterableStream } from "ai";
/**
 * Result of a streamText call.
 *
 * This type defines only the properties we actually use from the Vercel AI SDK's
 * StreamTextResult. Adapters must use `createStreamResult()` to build this type
 * from raw SDK results, ensuring all properties are plain enumerable values.
 */
export type StreamTextResult<TOOLS extends Record<string, Tool> = Record<string, Tool>> = {
    /** Stream of text deltas */
    textStream: AsyncIterableStream<string>;
    /** Stream of all events (text, tool calls, etc.) */
    fullStream: AsyncIterableStream<TextStreamPart<TOOLS>>;
    /** Promise resolving to step results after stream consumption */
    steps: Promise<StepResult<TOOLS>[]>;
    /** Promise resolving to the final result after stream consumption */
    response?: Promise<FinalResult<TOOLS>>;
};
//# sourceMappingURL=provider.d.ts.map