import { generateText, type LanguageModelUsage } from "ai";
import { BestAgentError } from "@best-agent/foundation";
import { type PhaseResultCandidateVerdict, type PhaseResultPhaseName, type PlanStatus, type PlanTodoKind, type PlanTodoStatus, type ProviderConfig, type ProviderDecisionTransport, type ProviderKind, type ProviderTurnActionPolicy, type ToolEffect, type ToolInputValue } from "@best-agent/protocol";
type AiSdkGenerateTextInput = Parameters<typeof generateText>[0];
type AiSdkModel = AiSdkGenerateTextInput["model"];
type AiSdkProviderOptions = NonNullable<AiSdkGenerateTextInput["providerOptions"]>;
type OpenAiCompatibleProviderOptions = {
    reasoningEffort?: ProviderConfig["reasoningEffort"];
};
type BestAgentProviderOptions = AiSdkProviderOptions & {
    openaiCompatible?: OpenAiCompatibleProviderOptions;
};
export type ToolUsePolicy = "answer-when-sufficient" | "tool-first-implementation" | "required-tool-only";
import type { ProviderCapabilities } from "./provider-metadata.js";
import type { ConversationMessage } from "./provider-conversation.js";
import { type ProviderToolName, type ProviderToolNameBinding, type RuntimeToolName } from "./provider-tool-names.js";
export type { ConversationMessage } from "./provider-conversation.js";
export { resolveOpenAiCompatibleMode } from "./provider-metadata.js";
export { DELEGATE_TASKS_TOOL_NAME, PHASE_RESULT_TOOL_NAME, PLAN_UPDATE_TOOL_NAME, isReservedProviderToolName, prepareProviderToolNameBindings, toRuntimeToolName, toProviderToolName, type ProviderToolName, type ProviderToolNameBinding, type RuntimeToolName, } from "./provider-tool-names.js";
export interface ProviderGenerateInput {
    cacheControl?: ProviderCacheControl;
    messages: ConversationMessage[];
    stream?: ProviderTextStreamCallbacks;
    systemPrompt?: string;
    toolNameBindings?: readonly ProviderToolNameBinding[];
}
export interface ProviderTextStreamCallbacks {
    noActionTimeoutMs?: number;
    onReasoningDelta?: (event: {
        delta: string;
        streamId: string;
    }) => Promise<void> | void;
    onReasoningEnd?: (event: {
        streamId: string;
    }) => Promise<void> | void;
    onReasoningStart?: (event: {
        streamId: string;
    }) => Promise<void> | void;
    onTextDelta?: (event: {
        delta: string;
        streamId: string;
    }) => Promise<void> | void;
    onToolCallDelta?: (event: {
        inputTextDelta: string;
        toolCallId: string;
        toolName: string;
    }) => Promise<void> | void;
    onToolCallStarted?: (event: {
        toolCallId: string;
        toolName: string;
    }) => Promise<void> | void;
}
export interface ProviderGenerateOutput {
    model: string;
    provider: ProviderKind;
    reasoningText?: string;
    text: string;
    usage?: ProviderUsageStats;
}
export interface ProviderWebSearchInput {
    domains?: string[];
    limit: number;
    query: string;
    recencyDays?: number;
}
export interface ProviderToolOption {
    description: string;
    effect: ToolEffect;
    inputSchema?: unknown;
    name: string;
}
export interface FunctionCallingToolOption extends ProviderToolOption {
    decodeFunctionArguments(input: unknown): ToolInputValue;
    functionCallingParameters: Record<string, unknown>;
    name: ProviderToolName;
    providerName: ProviderToolName;
    runtimeName: RuntimeToolName;
    strictFunctionCalling: boolean;
}
export interface ProviderCacheControl {
    stablePrefixText?: string;
    stablePrefixTokens?: number;
}
interface ResolveProviderOptionsInput {
    cacheControl?: ProviderCacheControl;
    includeReasoningSummary?: boolean;
    systemPrompt?: string;
}
export interface ProviderDecideInput extends ProviderGenerateInput {
    actionPolicy: ProviderTurnActionPolicy;
    decisionTransport: ProviderDecisionTransport;
    allowPhaseResult?: boolean;
    allowPlanUpdate?: boolean;
    phaseResultPhaseName?: PhaseResultPhaseName;
    toolUsePolicy?: ToolUsePolicy;
    tools: ProviderToolOption[];
}
export interface ProviderRespondDecision {
    kind: "respond";
    model: string;
    provider: ProviderKind;
    text: string;
    usage?: ProviderUsageStats;
}
export interface ProviderSelectedToolCall {
    input: ToolInputValue;
    providerToolCallId?: string;
    providerToolName?: string;
    toolName: string;
}
type ProviderToolDecisionBase = {
    kind: "tool";
    model: string;
    provider: ProviderKind;
    reasoning?: string;
    usage?: ProviderUsageStats;
};
type ProviderLegacyToolDecisionShape = {
    input: ToolInputValue;
    providerToolCallId?: string;
    toolCalls?: never;
    toolName: string;
};
type ProviderMultiToolDecisionShape = {
    input?: never;
    providerToolCallId?: never;
    toolCalls: ProviderSelectedToolCall[];
    toolName?: never;
};
export type ProviderToolDecision = ProviderToolDecisionBase & (ProviderLegacyToolDecisionShape | ProviderMultiToolDecisionShape);
export interface ProviderPlanUpdateTodo {
    content: string;
    kind?: PlanTodoKind;
    status?: PlanTodoStatus;
}
export interface ProviderPlanUpdatePayload {
    acceptanceCriteria: string[];
    affectedPaths: string[];
    evidenceRefs: string[];
    openQuestions: string[];
    risks: string[];
    status: Extract<PlanStatus, "draft" | "review_ready">;
    summary: string;
    title: string;
    todos: ProviderPlanUpdateTodo[];
}
export interface ProviderPlanUpdateDecision {
    kind: "plan_update";
    model: string;
    plan: ProviderPlanUpdatePayload;
    provider: ProviderKind;
    usage?: ProviderUsageStats;
}
export interface ProviderPhaseResultPayload {
    candidateVerdict?: PhaseResultCandidateVerdict;
    phaseName: PhaseResultPhaseName;
    summary: string;
}
export interface ProviderPhaseResultDecision {
    kind: "phase_result";
    model: string;
    phaseResult: ProviderPhaseResultPayload;
    provider: ProviderKind;
    usage?: ProviderUsageStats;
}
export type ProviderRepairableDecisionReasonCode = "invalid-structured-control-combination" | "invalid-structured-control-input" | "invalid-native-tool-input" | "invalid-native-tool-selection" | "structured-control-not-allowed" | "textual-control-payload" | "leaked-control-payload";
export interface ProviderRepairableInvalidDecision {
    kind: "repairable_invalid";
    message: string;
    model: string;
    provider: ProviderKind;
    rawText?: string;
    reasonCode: ProviderRepairableDecisionReasonCode;
    selectedToolNames?: string[];
    usage?: ProviderUsageStats;
}
export type ProviderDecisionOutput = ProviderPhaseResultDecision | ProviderPlanUpdateDecision | ProviderRepairableInvalidDecision | ProviderRespondDecision | ProviderToolDecision;
export interface ProviderDecisionParseOptions {
    actionPolicy?: ProviderTurnActionPolicy;
    allowPhaseResult?: boolean;
    allowPlanUpdate?: boolean;
    phaseResultPhaseName?: PhaseResultPhaseName;
    toolUsePolicy?: ToolUsePolicy;
}
export interface ProviderUsageStats {
    cacheReadTokens: number;
    cacheWriteTokens: number;
    inputTokens: number;
    outputTokens: number;
    reasoningTokens: number;
    totalTokens: number;
}
export interface ProviderPromptCacheDiagnostics {
    eligible: boolean;
    providerHintApplied: boolean;
}
export interface ModelProvider {
    readonly capabilities?: ProviderCapabilities;
    readonly config: ProviderConfig;
    decide(input: ProviderDecideInput): Promise<ProviderDecisionOutput>;
    generate(input: ProviderGenerateInput): Promise<ProviderGenerateOutput>;
    searchWeb?(input: ProviderWebSearchInput): Promise<string>;
}
export type ProviderOptions = AiSdkProviderOptions;
export interface ProviderTextResult {
    reasoningText?: string;
    sources?: unknown[];
    text: string;
    toolCalls?: Array<{
        args: unknown;
        toolCallId: string;
        toolName: string;
    }>;
    totalUsage?: LanguageModelUsage;
}
export declare const MAX_RETRYABLE_PROVIDER_ATTEMPTS = 3;
export declare function resolveProviderTimeoutMs(config: ProviderConfig): number;
export declare function isProviderNoActionError(error: unknown): boolean;
export declare function isProviderContextOverflowError(error: unknown): boolean;
export declare function prepareToolsForFunctionCalling(tools: readonly ProviderToolOption[]): FunctionCallingToolOption[];
export declare function decodeFunctionCallingToolInput(tools: readonly FunctionCallingToolOption[], toolName: string, input: unknown): ToolInputValue;
export declare function normalizeProviderToolDecision(decision: ProviderToolDecision): ProviderSelectedToolCall[];
export declare function decideWithAiSdk(config: ProviderConfig, input: ProviderDecideInput, getModel: (modelId: string) => AiSdkModel): Promise<ProviderDecisionOutput>;
export declare function decorateInvalidStructuredDecision(message: string, rawText?: string): BestAgentError;
export declare class ProviderRepairableDecisionError extends BestAgentError {
    readonly providerRepairableDecision = true;
    readonly rawText?: string;
    readonly reasonCode: ProviderRepairableDecisionReasonCode;
    readonly selectedToolNames?: string[];
    readonly usage?: ProviderUsageStats;
    constructor(input: {
        message: string;
        rawText?: string;
        reasonCode: ProviderRepairableDecisionReasonCode;
        selectedToolNames?: string[];
        usage?: ProviderUsageStats;
    });
}
export declare function isProviderRepairableDecisionError(error: unknown): error is ProviderRepairableDecisionError;
export declare function parsePlanUpdatePayload(plan: Record<string, unknown> | undefined): ProviderPlanUpdatePayload;
export declare function parsePhaseResultPayload(payload: Record<string, unknown> | undefined, expectedPhaseName?: PhaseResultPhaseName): ProviderPhaseResultPayload;
export declare function decisionLeakToolNames(tools: readonly ProviderToolOption[], allowPlanUpdate?: boolean, allowPhaseResult?: boolean): string[];
/** @deprecated Use containsControlPayloadFinal from shared/control-payload-classifier. */
export declare function containsDecisionControlLeak(text: string, toolNames?: readonly string[]): boolean;
export declare function mergeSystemPrompts(...parts: Array<string | undefined>): string | undefined;
export declare function resolveProviderOptions(config: ProviderConfig, input: ResolveProviderOptionsInput | undefined): BestAgentProviderOptions | undefined;
export declare function resolveAiSdkSystemPrompt(config: ProviderConfig, systemPrompt: string | undefined): string | undefined;
export declare function resolveProviderPromptCacheDiagnostics(config: ProviderConfig, cacheControl: ProviderCacheControl | undefined): ProviderPromptCacheDiagnostics;
export declare function attachDecisionUsage(decision: ProviderDecisionOutput, usage: ProviderUsageStats | undefined): ProviderDecisionOutput;
export declare function createProviderUsageStats(usage: LanguageModelUsage | undefined): ProviderUsageStats | undefined;
export declare function generateTextWithProviderResilience(config: ProviderConfig, input: Parameters<typeof generateText>[0], stream?: ProviderTextStreamCallbacks): Promise<ProviderTextResult>;
export declare function isRetryableProviderError(error: unknown): boolean;
export declare function resolveProviderRetryDelayMs(attempt: number, error: unknown): number;
export declare function decorateProviderError(error: unknown, config: ProviderConfig): Error;
//# sourceMappingURL=provider-core.d.ts.map