import { isOpenAICodexContext, isResponsesContext } from "./prompt/codex-model.js";
import { applyCodexRequestParams } from "./activation/config.js";
import { isEffectiveOpenAICodexContext, shouldUseCodexAdapter, shouldUseGpt56CodeMode, shouldUseResponsesLiteForCodeMode, supportsProxiedGpt56CodeModeModel } from "./activation/activation.js";
import { injectPendingNativeWindowIntoPiCompactionRequest, rewriteCodexCompactedProviderRequest } from "./compaction/compaction.js";
import { applyResponsesLiteRequest, supportsResponsesLiteModel } from "../providers/openai-codex/responses-lite.js";
import { applyCodeModeFreeformContract } from "./code-mode-contract.js";
export async function rewriteCodexProviderRequest(payload, ctx, state) {
    if (!shouldUseCodexAdapter(ctx, state.config) || (!isEffectiveOpenAICodexContext(ctx, state.config) && !isResponsesContext(ctx))) {
        return undefined;
    }
    const isEffectiveOpenAICodex = isEffectiveOpenAICodexContext(ctx, state.config);
    const configuredPayload = applyCodexRequestParams(payload, state.config, {
        serviceTier: isEffectiveOpenAICodex,
        verbosity: true,
    });
    const piCompactionPayload = await injectPendingNativeWindowIntoPiCompactionRequest(configuredPayload, ctx, state);
    const rewrittenPayload = piCompactionPayload ?? (await rewriteCodexCompactedProviderRequest(configuredPayload, ctx, state)) ?? configuredPayload;
    if (shouldUseGpt56CodeMode(ctx, state.config)
        && isCodeModeCompatibleBody(rewrittenPayload)
        && (isOpenAICodexContext(ctx)
            ? supportsResponsesLiteModel(rewrittenPayload.model)
            : supportsProxiedGpt56CodeModeModel(rewrittenPayload.model))) {
        const codeModePayload = applyCodeModeFreeformContract(rewrittenPayload);
        return shouldUseResponsesLiteForCodeMode(ctx, state.config)
            ? applyResponsesLiteRequest(codeModePayload)
            : codeModePayload;
    }
    return rewrittenPayload;
}
function isCodeModeCompatibleBody(value) {
    return typeof value === "object" && value !== null
        && typeof value.model === "string"
        && Array.isArray(value.input);
}
