import type { LanguageModel, ModelMessage, ToolChoice } from 'ai';
import { streamText } from 'ai';
import { AgentRun } from './agent-run';
import type { Tool } from './define-tool';
import { AgentError } from './errors';
import { type ApprovalHook, type ApprovalRequest, type PostToolUseHook, type PreRequestHook, type PreToolUseHook } from './hooks';
import { ModelProvider } from './models';
import type { AgentState } from './state';
import type { StopResult, StopWhen } from './stop-conditions';
import { type TokenUsage } from './token-usage';
export type ProviderOptions = Parameters<typeof streamText>[0]['providerOptions'];
export type ProviderOptionsFactory = (ctx: {
    runId: string;
}) => ProviderOptions;
export interface AgentConfig<TTools extends Record<string, Tool<any, any>> = Record<string, Tool<any, any>>> {
    model: LanguageModel;
    system?: string | string[];
    tools: TTools;
    toolChoice?: ToolChoice<TTools>;
    providerOptions?: ProviderOptions | ProviderOptionsFactory;
    maxSteps?: number;
    stopWhen?: StopWhen;
    modelProvider?: ModelProvider;
    /** Called when the agent run finishes with an error. Observe-only — cannot prevent the error. */
    onError?: (error: AgentError, result: RunResult) => void | Promise<void>;
    /** Called when the agent run finishes for any reason. Always fires, including on errors. */
    onStop?: (result: RunResult) => void | Promise<void>;
    /** Explicit context window limit override. When not set, resolved from models.dev if available. */
    contextWindowLimit?: number;
    /** Called when an approval is requested. Fires before the event is pushed to the iterator. Observe-only, errors swallowed. */
    onApprovalRequested?: (approval: ApprovalRequest, toolCallId: string, toolName: string, input: Record<string, unknown>) => void | Promise<void>;
    hooks?: {
        approval?: ApprovalHook[];
        preToolUse?: PreToolUseHook[];
        postToolUse?: PostToolUseHook[];
        preRequest?: PreRequestHook[];
    };
}
export type FinishReason = 'complete' | 'maxSteps' | 'stopCondition' | 'interrupted' | 'approvalRequired' | 'error';
export interface RunResult {
    /** The full agent state after this run (messages + any pending tool calls + approval history). */
    state: AgentState;
    newMessages: ModelMessage[];
    finishReason: FinishReason;
    /** Present when finishReason is 'stopCondition'. Identifies which condition fired. */
    stopCondition?: StopResult;
    /** Present when finishReason is 'error'. */
    error?: AgentError;
    /** Per-model token usage aggregate for this run (ephemeral — not persisted in state). */
    tokenUsage: TokenUsage;
    /** Context window limit for the model (if known). */
    contextWindowLimit?: number;
}
export interface RunOptions {
    state: AgentState;
    signal?: AbortSignal;
    stream?: boolean;
}
export declare class Agent<TTools extends Record<string, Tool<any, any>> = Record<string, Tool<any, any>>> {
    private model;
    private system;
    private tools;
    private maxStepsLimit;
    private stopWhen;
    private aiSdkTools;
    private toolChoice;
    private providerOptions;
    private hooks;
    private onError;
    private onStop;
    private onApprovalRequested;
    private contextWindowLimit;
    private modelProvider;
    constructor(config: AgentConfig<TTools>);
    run(options: RunOptions): AgentRun;
    private resolveProviderOptions;
    private executeLoop;
    private executeModelStep;
    private translateStreamPart;
    private mapFinishReason;
    /** Finish the run and invoke onError/onStop callbacks. */
    private finishRun;
    /**
     * Run approval hooks (if configured and not skipped), then preToolUse hooks, then execute the tool call.
     * Returns a typed outcome describing what happened.
     *
     * @param skipApproval - When true, skip the approval hook chain (used when resuming with an approved decision).
     *   PreToolUse hooks still run regardless of this flag.
     */
    private resolveToolCall;
    /**
     * Execute a tool call and run postToolUse hooks on the result.
     */
    private executeWithPostHooks;
    /** Merge hook state updates from outcomes into the running toolState map. */
    private mergeHookStateUpdates;
    /** Merge tool state updates from executed outcomes into the running toolState map. */
    private mergeToolStateUpdates;
    /**
     * When preToolUse hooks mutate tool-call inputs via `ctx.next(updatedInput)` and
     * set `updateContextWindow: true`, patch the corresponding tool-call entries in
     * the assistant message so the model sees the mutated values.
     *
     * When `notifyModel: true`, prepend a system notification to the tool result output
     * so the model knows the inputs were modified by a hook.
     */
    private applyHookMutationEffects;
    /**
     * When the last message is an assistant message with pending tool calls,
     * execute them and append results. This handles resuming from an
     * interrupted run where tool calls were never executed.
     *
     * Behavior is determined by `state.pendingToolCalls`:
     * - If a dangling tool call has a matching `type: 'approval'` entry in `state.pendingToolCalls`,
     *   it is skipped (parked — caller must use `withApprovals()` to resolve it).
     * - If a dangling tool call has a matching `type: 'stopped'` entry, it is also skipped.
     * - If no match is found, the tool call auto-executes (handles `maxSteps` resume and
     *   cases where `withApprovals()` has already removed the entry).
     *
     * Returns a RunResult if the preamble itself causes a stop (remaining pending approvals),
     * or null if the loop should continue normally.
     *
     * This does NOT count as a loop step — stop conditions do not fire.
     */
    private executeDanglingToolCalls;
    /**
     * Evaluate stop conditions for the given timing phase.
     * Returns a RunResult if a condition fired, null otherwise.
     */
    private checkStopConditions;
}
//# sourceMappingURL=agent.d.ts.map