import { isOpenRouterProvider } from '../providers/openrouter.js';
/**
 * Build the `providerOptions` value for a streamText/generateText call.
 *
 * Currently handles two providers:
 *   - chatgpt-codex: requires `instructions`, `store: false`, and reasoning
 *     controls under the `openai` provider key (Responses API).
 *   - openrouter: forwards `provider`, `reasoning`, `plugins`, `models`,
 *     `service_tier`, `route`, and `user` into the request body via the
 *     `openrouter` provider key. The top-level `reasoningEffort` (from
 *     ModelParameters / `/tune`) is mapped to `reasoning.effort` when the
 *     user has not provided a more specific `openrouter.reasoning` block.
 *
 * OpenRouter options come from `providerConfig.openrouter` (always-on, set
 * in agents.config.json) — not from tune, so they aren't dropped when the
 * user toggles tune off.
 *
 * Returns `undefined` when no provider-specific options apply, so the SDK
 * call site can spread it without producing an empty `providerOptions: {}`.
 */
export function buildProviderOptions(providerConfig, systemContent, modelParameters) {
    if (providerConfig.sdkProvider === 'chatgpt-codex') {
        return {
            openai: {
                ...(systemContent ? { instructions: systemContent } : {}),
                store: false,
                reasoningEffort: modelParameters?.reasoningEffort ?? 'medium',
                reasoningSummary: modelParameters?.reasoningSummary ?? 'auto',
            },
        };
    }
    if (isOpenRouterProvider(providerConfig.name)) {
        const openrouter = providerConfig.openrouter;
        const reasoning = buildOpenRouterReasoning(openrouter, modelParameters);
        // Start from the generic escape-hatch so typed fields below can
        // override anything the user happened to spell the same way.
        const payload = {
            ...(openrouter?.extraBody ?? {}),
        };
        if (openrouter?.provider !== undefined) {
            payload['provider'] = openrouter.provider;
        }
        if (reasoning !== undefined) {
            payload['reasoning'] = reasoning;
        }
        if (openrouter?.plugins !== undefined) {
            payload['plugins'] = openrouter.plugins;
        }
        if (openrouter?.models !== undefined) {
            payload['models'] = openrouter.models;
        }
        if (openrouter?.service_tier !== undefined) {
            payload['service_tier'] = openrouter.service_tier;
        }
        if (openrouter?.route !== undefined) {
            payload['route'] = openrouter.route;
        }
        if (openrouter?.user !== undefined) {
            payload['user'] = openrouter.user;
        }
        if (Object.keys(payload).length === 0) {
            return undefined;
        }
        return { openrouter: payload };
    }
    return undefined;
}
/**
 * Resolve OpenRouter's `reasoning` body field by merging the top-level
 * `reasoningEffort` shortcut (from ModelParameters / `/tune`) with the more
 * granular `openrouter.reasoning` block on the provider config. The
 * provider-config block wins on key conflicts so always-on routing rules
 * aren't accidentally overridden by transient session state.
 */
function buildOpenRouterReasoning(openrouter, modelParameters) {
    const explicit = openrouter?.reasoning;
    const effortShortcut = modelParameters?.reasoningEffort;
    if (!explicit && !effortShortcut)
        return undefined;
    return {
        ...(effortShortcut ? { effort: effortShortcut } : {}),
        ...explicit,
    };
}
//# sourceMappingURL=provider-options.js.map