import { DEFAULT_THINKING_LEVEL, isValidThinkingLevel } from '../core/defaults.js';
function resolveSavedThinkingLevel(sessionContext, override) {
    if (override !== undefined) {
        return override;
    }
    const savedThinkingLevel = sessionContext.thinkingLevel;
    if (isValidThinkingLevel(savedThinkingLevel)) {
        return savedThinkingLevel;
    }
    return DEFAULT_THINKING_LEVEL;
}
async function resolveSavedModel(sessionContext, modelRegistry) {
    if (sessionContext.model == null) {
        return undefined;
    }
    const restoredModel = modelRegistry.find(sessionContext.model.provider, sessionContext.model.modelId);
    if (!restoredModel) {
        return undefined;
    }
    // Drop the saved model if no api key is available — better to fall through
    // than resume a session pinned to a model the runtime cannot authenticate.
    const apiKey = await modelRegistry.getApiKey(restoredModel);
    return apiKey ? restoredModel : undefined;
}
/** Pick the model and thinking level a fresh embedded runtime should boot with. */
export async function selectInitialRuntimeModel(options) {
    let model = options.model;
    const thinkingLevel = resolveSavedThinkingLevel(options.sessionContext, options.thinkingLevel);
    if (!model && options.sessionContext.messages.length > 0) {
        model = await resolveSavedModel(options.sessionContext, options.modelRegistry);
    }
    if (!model) {
        const availableModels = await options.modelRegistry.getAvailable();
        model = availableModels[0];
    }
    // Force reasoning off when the chosen model can't reason, so callers don't
    // accidentally request thinking on a non-reasoning model.
    if (!model || !model.reasoning) {
        return { model, thinkingLevel: 'off' };
    }
    return { model, thinkingLevel };
}
//# sourceMappingURL=model-selection.js.map