import { SHORTCUT_MODEL_ID } from './model-ids.js';
// OpenAI Priority Processing excludes long-context requests, publicly defined as
// requests estimated above 128K prompt tokens.
// Source: https://openai.com/api-priority-processing/
export const GPT55_PRIORITY_MAX_ESTIMATED_PROMPT_TOKENS = 128_000;
function getUtf8ByteLength(value) {
    if (typeof TextEncoder !== 'undefined') {
        return new TextEncoder().encode(value).byteLength;
    }
    return unescape(encodeURIComponent(value)).length;
}
export function estimateShortcutPromptTokens(context) {
    try {
        const promptBytes = getUtf8ByteLength(JSON.stringify({
            systemPrompt: context.systemPrompt,
            messages: context.messages,
            tools: context.tools
        }));
        // Approximate OpenAI prompt tokens from serialized request bytes. This is a
        // pre-request eligibility guard only; provider-reported service_tier remains
        // the billing source of truth.
        return Math.ceil(promptBytes / 4);
    }
    catch {
        return null;
    }
}
export function shouldUseShortcutFastMode(modelId, speed, estimatedPromptTokens) {
    if (speed !== 'fast')
        return false;
    if (modelId !== SHORTCUT_MODEL_ID.Gpt55)
        return true;
    return (typeof estimatedPromptTokens === 'number' &&
        Number.isFinite(estimatedPromptTokens) &&
        estimatedPromptTokens <= GPT55_PRIORITY_MAX_ESTIMATED_PROMPT_TOKENS);
}
export function resolveShortcutSpeedForRequest(modelId, speed, estimatedPromptTokens) {
    if (speed === undefined)
        return undefined;
    if (speed === 'standard')
        return 'standard';
    return shouldUseShortcutFastMode(modelId, speed, estimatedPromptTokens) ? 'fast' : undefined;
}
export function resolveShortcutSpeedForContext(modelId, speed, context) {
    return resolveShortcutSpeedForRequest(modelId, speed, estimateShortcutPromptTokens(context));
}
//# sourceMappingURL=fast-mode.js.map