import { z } from "zod";
export const DEFAULT_OPENAI_REALTIME_TRANSCRIPTION_MODEL = "gpt-4o-transcribe";
export const DEFAULT_OPENAI_TTS_MODEL = "tts-1";
const OpenAiTtsVoiceSchema = z.enum(["alloy", "echo", "fable", "onyx", "nova", "shimmer"]);
const OpenAiTtsModelSchema = z.enum(["tts-1", "tts-1-hd"]);
const NumberLikeSchema = z.union([z.number(), z.string().trim().min(1)]);
const OptionalFiniteNumberSchema = NumberLikeSchema.pipe(z.coerce.number().finite()).optional();
const OptionalTrimmedStringSchema = z
    .string()
    .trim()
    .optional()
    .transform((value) => (value && value.length > 0 ? value : undefined));
const OpenAiSpeechResolutionSchema = z.object({
    apiKey: OptionalTrimmedStringSchema,
    sttConfidenceThreshold: OptionalFiniteNumberSchema,
    sttModel: OptionalTrimmedStringSchema,
    ttsVoice: z.string().trim().toLowerCase().pipe(OpenAiTtsVoiceSchema).default("alloy"),
    ttsModel: z
        .string()
        .trim()
        .toLowerCase()
        .pipe(OpenAiTtsModelSchema)
        .default(DEFAULT_OPENAI_TTS_MODEL),
    realtimeTranscriptionModel: OptionalTrimmedStringSchema.default(DEFAULT_OPENAI_REALTIME_TRANSCRIPTION_MODEL),
});
function isOpenAiProviderActive(provider) {
    return provider.enabled !== false && provider.provider === "openai";
}
function pickIfOpenAi(provider, value) {
    return isOpenAiProviderActive(provider) ? value : undefined;
}
function firstDefined(values) {
    for (const value of values) {
        if (value !== undefined && value !== null) {
            return value;
        }
    }
    return undefined;
}
function buildOpenAiSttInput(params) {
    const { env, persisted, providers } = params;
    return {
        sttConfidenceThreshold: firstDefined([
            env.STT_CONFIDENCE_THRESHOLD,
            persisted.features?.dictation?.stt?.confidenceThreshold,
        ]),
        sttModel: firstDefined([
            env.STT_MODEL,
            pickIfOpenAi(providers.voiceStt, persisted.features?.voiceMode?.stt?.model),
            pickIfOpenAi(providers.dictationStt, persisted.features?.dictation?.stt?.model),
        ]),
        realtimeTranscriptionModel: firstDefined([
            env.OPENAI_REALTIME_TRANSCRIPTION_MODEL,
            pickIfOpenAi(providers.dictationStt, persisted.features?.dictation?.stt?.model),
            DEFAULT_OPENAI_REALTIME_TRANSCRIPTION_MODEL,
        ]),
    };
}
function buildOpenAiTtsInput(params) {
    const { env, persisted, providers } = params;
    return {
        ttsVoice: firstDefined([
            env.TTS_VOICE,
            pickIfOpenAi(providers.voiceTts, persisted.features?.voiceMode?.tts?.voice),
            "alloy",
        ]),
        ttsModel: firstDefined([
            env.TTS_MODEL,
            pickIfOpenAi(providers.voiceTts, persisted.features?.voiceMode?.tts?.model),
            DEFAULT_OPENAI_TTS_MODEL,
        ]),
    };
}
function buildOpenAiResolutionInput(params) {
    return {
        apiKey: firstDefined([
            params.env.OPENAI_API_KEY,
            params.persisted.providers?.openai?.apiKey,
        ]),
        ...buildOpenAiSttInput(params),
        ...buildOpenAiTtsInput(params),
    };
}
export function resolveOpenAiSpeechConfig(params) {
    const parsed = OpenAiSpeechResolutionSchema.parse(buildOpenAiResolutionInput(params));
    if (!parsed.apiKey) {
        return undefined;
    }
    return {
        apiKey: parsed.apiKey,
        stt: {
            apiKey: parsed.apiKey,
            ...(parsed.sttConfidenceThreshold !== undefined
                ? { confidenceThreshold: parsed.sttConfidenceThreshold }
                : {}),
            ...(parsed.sttModel ? { model: parsed.sttModel } : {}),
        },
        tts: {
            apiKey: parsed.apiKey,
            voice: parsed.ttsVoice,
            model: parsed.ttsModel,
            responseFormat: "pcm",
        },
        realtimeTranscriptionModel: parsed.realtimeTranscriptionModel,
    };
}
//# sourceMappingURL=config.js.map