import { clampThinkingLevel, streamOpenAICodexResponses, streamOpenAIResponses, streamSimple, } from "@earendil-works/pi-ai/compat";
export const CODEX_FAST_MODE_SERVICE_TIER = "priority";
const DEFAULT_CODEX_FAST_MODE_STREAMERS = {
    streamSimple,
    streamOpenAIResponses,
    streamOpenAICodexResponses,
};
export function isCodexFastModeSupportedProvider(provider) {
    return provider === "openai" || provider === "openai-codex";
}
export function isCodexFastModeCandidateModelId(modelId) {
    const provider = modelId?.split("/", 1)[0];
    return provider !== undefined && isCodexFastModeSupportedProvider(provider);
}
export function isCodexFastModeSupportedModel(model) {
    return isCodexFastModeSupportedProvider(model.provider);
}
export function hasSupportedCodexFastModeModel(models) {
    return models.some(isCodexFastModeSupportedModel);
}
export function isWorkflowStageOrchestrationContext(context) {
    return context?.kind === "workflow-stage";
}
export function getCodexFastModeScope(context) {
    return isWorkflowStageOrchestrationContext(context) ? "workflow" : "chat";
}
export function isCodexFastModeEnabledForScope(settings, scope) {
    return settings[scope];
}
export function isCodexFastModeEnabledForSession(settings, context) {
    return isCodexFastModeEnabledForScope(settings, getCodexFastModeScope(context));
}
export function shouldApplyCodexFastModeForScope(model, settings, scope) {
    return isCodexFastModeSupportedModel(model) && isCodexFastModeEnabledForScope(settings, scope);
}
export function shouldApplyCodexFastMode(model, settings, context) {
    return shouldApplyCodexFastModeForScope(model, settings, getCodexFastModeScope(context));
}
export function withCodexFastModeStreamOptions(options, enabled) {
    if (!enabled) {
        return options;
    }
    return {
        ...(options ?? {}),
        serviceTier: CODEX_FAST_MODE_SERVICE_TIER,
    };
}
export function isCodexFastModeNativeApi(api) {
    return api === "openai-responses" || api === "openai-codex-responses";
}
export function shouldUseNativeCodexFastMode(model, options) {
    return (isCodexFastModeSupportedModel(model) &&
        isCodexFastModeNativeApi(model.api) &&
        options?.serviceTier === CODEX_FAST_MODE_SERVICE_TIER);
}
function buildCodexFastModeBaseProviderOptions(options) {
    return {
        temperature: options?.temperature,
        maxTokens: options?.maxTokens,
        signal: options?.signal,
        apiKey: options?.apiKey,
        transport: options?.transport,
        cacheRetention: options?.cacheRetention,
        sessionId: options?.sessionId,
        onPayload: options?.onPayload,
        onResponse: options?.onResponse,
        headers: options?.headers,
        timeoutMs: options?.timeoutMs,
        websocketConnectTimeoutMs: options?.websocketConnectTimeoutMs,
        maxRetries: options?.maxRetries,
        maxRetryDelayMs: options?.maxRetryDelayMs,
        metadata: options?.metadata,
    };
}
export function mapCodexFastModeReasoningEffort(model, reasoning) {
    const clampedReasoning = reasoning ? clampThinkingLevel(model, reasoning) : undefined;
    return clampedReasoning === "off" ? undefined : clampedReasoning;
}
export function buildOpenAIResponsesCodexFastModeOptions(model, options) {
    return {
        ...buildCodexFastModeBaseProviderOptions(options),
        reasoningEffort: mapCodexFastModeReasoningEffort(model, options?.reasoning),
        serviceTier: options?.serviceTier,
    };
}
export function buildOpenAICodexResponsesCodexFastModeOptions(model, options) {
    return {
        ...buildCodexFastModeBaseProviderOptions(options),
        reasoningEffort: mapCodexFastModeReasoningEffort(model, options?.reasoning),
        serviceTier: options?.serviceTier,
    };
}
export function streamWithCodexFastMode(model, context, options, streamers = DEFAULT_CODEX_FAST_MODE_STREAMERS) {
    if (shouldUseNativeCodexFastMode(model, options)) {
        if (model.api === "openai-responses") {
            return streamers.streamOpenAIResponses(model, context, buildOpenAIResponsesCodexFastModeOptions(model, options));
        }
        return streamers.streamOpenAICodexResponses(model, context, buildOpenAICodexResponsesCodexFastModeOptions(model, options));
    }
    return streamers.streamSimple(model, context, options);
}
function isObjectPayload(payload) {
    return typeof payload === "object" && payload !== null && !Array.isArray(payload);
}
export function withCodexFastModePayload(payload, enabled) {
    if (!enabled || !isObjectPayload(payload) || payload.service_tier !== undefined) {
        return payload;
    }
    return {
        ...payload,
        service_tier: CODEX_FAST_MODE_SERVICE_TIER,
    };
}
export function formatCodexFastModeModelLabel(modelName, enabled) {
    return enabled ? `${modelName} fast` : modelName;
}
//# sourceMappingURL=codex-fast-mode.js.map