import { getSupportedThinkingLevels, stream, streamSimple } from "@earendil-works/pi-ai";
function normalizeSimpleReasoning(reasoning) {
    if (reasoning === undefined || reasoning === "none") {
        return undefined;
    }
    return reasoning;
}
export function resolveSimpleStreamOptions(options) {
    const { reasoning, ...baseOptions } = options;
    const normalizedReasoning = normalizeSimpleReasoning(reasoning);
    if (normalizedReasoning === undefined) {
        return baseOptions;
    }
    return {
        ...baseOptions,
        reasoning: normalizedReasoning,
    };
}
function isBedrockModel(model) {
    return model.api === "bedrock-converse-stream" || model.provider === "amazon-bedrock";
}
function isBedrockAnthropicModel(model) {
    return isBedrockModel(model) && model.id.includes("anthropic.");
}
function isOpenAIResponsesModel(model) {
    return model.api === "openai-responses" && model.provider === "openai";
}
function isOpenAICodexModel(model) {
    return model.api === "openai-codex-responses" && model.provider === "openai-codex";
}
function resolveBedrockOptions(model, options) {
    const { reasoning, ...baseOptions } = options;
    const normalizedReasoning = normalizeSimpleReasoning(reasoning);
    return {
        ...baseOptions,
        ...(normalizedReasoning !== undefined ? { reasoning: normalizedReasoning } : {}),
        maxTokens: options.maxTokens || Math.min(model.maxTokens, 32000),
        ...(isBedrockAnthropicModel(model) ? { interleavedThinking: true } : {}),
    };
}
export function resolveOpenAIReasoningEffort(model, reasoning) {
    if (reasoning === undefined) {
        return undefined;
    }
    if (reasoning === "none") {
        return reasoning;
    }
    if (getSupportedThinkingLevels(model).includes("xhigh") || reasoning !== "xhigh") {
        return reasoning;
    }
    return "high";
}
export function resolveOpenAIResponsesOptions(model, options) {
    const reasoningEffort = resolveOpenAIReasoningEffort(model, options.reasoning);
    const { reasoning: _reasoning, serviceTier: _serviceTier, ...baseOptions } = options;
    return {
        ...baseOptions,
        ...(isOpenAICodexModel(model) && options.transport === undefined
            ? { transport: "websocket-cached" }
            : {}),
        ...(reasoningEffort !== undefined ? { reasoningEffort } : {}),
        ...(options.serviceTier !== undefined ? { serviceTier: options.serviceTier } : {}),
    };
}
export function streamModel(model, context, options) {
    if (isBedrockModel(model)) {
        const providerOptions = resolveBedrockOptions(model, options);
        return stream(model, context, providerOptions);
    }
    if (isOpenAIResponsesModel(model)) {
        return stream(model, context, resolveOpenAIResponsesOptions(model, options));
    }
    if (isOpenAICodexModel(model)) {
        return stream(model, context, resolveOpenAIResponsesOptions(model, options));
    }
    return streamSimple(model, context, resolveSimpleStreamOptions(options));
}
//# sourceMappingURL=model_stream.js.map