import { a as normalizeLowercaseStringOrEmpty, s as normalizeOptionalLowercaseString } from "./string-coerce-Bje8XVt9.js";
import { l as normalizeResolvedSecretInputString } from "./types.secrets-BHp0Y_k0.js";
import "./text-runtime-BXDoRNVl.js";
import "./secret-input-CpghZJXz.js";
import { a as resolveOpenAIProviderConfigRecord, n as asObjectRecord, o as trimToUndefined, t as asFiniteNumber } from "./realtime-provider-shared-Cdp4BlC0.js";
import { a as isValidOpenAIVoice, i as isValidOpenAIModel, n as OPENAI_TTS_MODELS, o as normalizeOpenAITtsBaseUrl, r as OPENAI_TTS_VOICES, s as openaiTTS } from "./tts-D3E4yUI1.js";
//#region extensions/openai/speech-provider.ts
const OPENAI_SPEECH_RESPONSE_FORMATS = [
	"mp3",
	"opus",
	"wav"
];
function normalizeOpenAISpeechResponseFormat(value) {
	const next = normalizeOptionalLowercaseString(value);
	if (!next) return;
	if (OPENAI_SPEECH_RESPONSE_FORMATS.includes(next)) return next;
	throw new Error(`Invalid OpenAI speech responseFormat: ${next}`);
}
function isGroqSpeechBaseUrl(baseUrl) {
	try {
		const hostname = normalizeLowercaseStringOrEmpty(new URL(baseUrl).hostname);
		return hostname === "groq.com" || hostname.endsWith(".groq.com");
	} catch {
		return false;
	}
}
function resolveSpeechResponseFormat(baseUrl, target, configuredFormat) {
	if (configuredFormat) return configuredFormat;
	if (isGroqSpeechBaseUrl(baseUrl)) return "wav";
	return target === "voice-note" ? "opus" : "mp3";
}
function responseFormatToFileExtension(format) {
	switch (format) {
		case "opus": return ".opus";
		case "wav": return ".wav";
		default: return ".mp3";
	}
}
function normalizeOpenAIProviderConfig(rawConfig) {
	const raw = resolveOpenAIProviderConfigRecord(rawConfig);
	return {
		apiKey: normalizeResolvedSecretInputString({
			value: raw?.apiKey,
			path: "messages.tts.providers.openai.apiKey"
		}),
		baseUrl: normalizeOpenAITtsBaseUrl(trimToUndefined(raw?.baseUrl) ?? trimToUndefined(process.env.OPENAI_TTS_BASE_URL) ?? "https://api.openai.com/v1"),
		model: trimToUndefined(raw?.model) ?? "gpt-4o-mini-tts",
		voice: trimToUndefined(raw?.voice) ?? "coral",
		speed: asFiniteNumber(raw?.speed),
		instructions: trimToUndefined(raw?.instructions),
		responseFormat: normalizeOpenAISpeechResponseFormat(raw?.responseFormat)
	};
}
function readOpenAIProviderConfig(config) {
	const normalized = normalizeOpenAIProviderConfig({});
	return {
		apiKey: trimToUndefined(config.apiKey) ?? normalized.apiKey,
		baseUrl: trimToUndefined(config.baseUrl) ?? normalized.baseUrl,
		model: trimToUndefined(config.model) ?? normalized.model,
		voice: trimToUndefined(config.voice) ?? normalized.voice,
		speed: asFiniteNumber(config.speed) ?? normalized.speed,
		instructions: trimToUndefined(config.instructions) ?? normalized.instructions,
		responseFormat: normalizeOpenAISpeechResponseFormat(config.responseFormat) ?? normalized.responseFormat
	};
}
function readOpenAIOverrides(overrides) {
	if (!overrides) return {};
	return {
		model: trimToUndefined(overrides.model),
		voice: trimToUndefined(overrides.voice),
		speed: asFiniteNumber(overrides.speed)
	};
}
function renderOpenAITtsPersonaInstructions(req) {
	const prompt = req.prompt;
	if (!prompt) return;
	const lines = [
		req.label ? `Persona: ${req.label}` : void 0,
		prompt.profile ? `Profile: ${prompt.profile}` : void 0,
		prompt.scene ? `Scene: ${prompt.scene}` : void 0,
		prompt.style ? `Style: ${prompt.style}` : void 0,
		prompt.accent ? `Accent: ${prompt.accent}` : void 0,
		prompt.pacing ? `Pacing: ${prompt.pacing}` : void 0,
		prompt.sampleContext ? `Sample context: ${prompt.sampleContext}` : void 0,
		...(prompt.constraints ?? []).map((constraint) => `Constraint: ${constraint}`)
	].map((line) => trimToUndefined(line)).filter((line) => Boolean(line));
	return lines.length > 0 ? lines.join("\n") : void 0;
}
function parseDirectiveToken(ctx) {
	const baseUrl = trimToUndefined(asObjectRecord(ctx.providerConfig)?.baseUrl);
	switch (ctx.key) {
		case "voice":
		case "openai_voice":
		case "openaivoice":
			if (!ctx.policy.allowVoice) return { handled: true };
			if (!isValidOpenAIVoice(ctx.value, baseUrl)) return {
				handled: true,
				warnings: [`invalid OpenAI voice "${ctx.value}"`]
			};
			return {
				handled: true,
				overrides: { voice: ctx.value }
			};
		case "model":
		case "openai_model":
		case "openaimodel":
			if (!ctx.policy.allowModelId) return { handled: true };
			if (!isValidOpenAIModel(ctx.value, baseUrl)) return { handled: false };
			return {
				handled: true,
				overrides: { model: ctx.value }
			};
		default: return { handled: false };
	}
}
function buildOpenAISpeechProvider() {
	return {
		id: "openai",
		label: "OpenAI",
		autoSelectOrder: 10,
		models: OPENAI_TTS_MODELS,
		voices: OPENAI_TTS_VOICES,
		resolveConfig: ({ rawConfig }) => normalizeOpenAIProviderConfig(rawConfig),
		parseDirectiveToken,
		resolveTalkConfig: ({ baseTtsConfig, talkProviderConfig }) => {
			const base = normalizeOpenAIProviderConfig(baseTtsConfig);
			const responseFormat = normalizeOpenAISpeechResponseFormat(talkProviderConfig.responseFormat);
			return {
				...base,
				...talkProviderConfig.apiKey === void 0 ? {} : { apiKey: normalizeResolvedSecretInputString({
					value: talkProviderConfig.apiKey,
					path: "talk.providers.openai.apiKey"
				}) },
				...trimToUndefined(talkProviderConfig.baseUrl) == null ? {} : { baseUrl: trimToUndefined(talkProviderConfig.baseUrl) },
				...trimToUndefined(talkProviderConfig.modelId) == null ? {} : { model: trimToUndefined(talkProviderConfig.modelId) },
				...trimToUndefined(talkProviderConfig.voiceId) == null ? {} : { voice: trimToUndefined(talkProviderConfig.voiceId) },
				...asFiniteNumber(talkProviderConfig.speed) == null ? {} : { speed: asFiniteNumber(talkProviderConfig.speed) },
				...trimToUndefined(talkProviderConfig.instructions) == null ? {} : { instructions: trimToUndefined(talkProviderConfig.instructions) },
				...responseFormat == null ? {} : { responseFormat }
			};
		},
		resolveTalkOverrides: ({ params }) => ({
			...trimToUndefined(params.voiceId) == null ? {} : { voice: trimToUndefined(params.voiceId) },
			...trimToUndefined(params.modelId) == null ? {} : { model: trimToUndefined(params.modelId) },
			...asFiniteNumber(params.speed) == null ? {} : { speed: asFiniteNumber(params.speed) }
		}),
		listVoices: async () => OPENAI_TTS_VOICES.map((voice) => ({
			id: voice,
			name: voice
		})),
		isConfigured: ({ providerConfig }) => Boolean(readOpenAIProviderConfig(providerConfig).apiKey || process.env.OPENAI_API_KEY),
		prepareSynthesis: (ctx) => {
			if (readOpenAIProviderConfig(ctx.providerConfig).instructions) return;
			const instructions = renderOpenAITtsPersonaInstructions({
				label: ctx.persona?.label ?? ctx.persona?.id,
				prompt: ctx.persona?.prompt
			});
			return instructions ? { providerConfig: { instructions } } : void 0;
		},
		synthesize: async (req) => {
			const config = readOpenAIProviderConfig(req.providerConfig);
			const overrides = readOpenAIOverrides(req.providerOverrides);
			const apiKey = config.apiKey || process.env.OPENAI_API_KEY;
			if (!apiKey) throw new Error("OpenAI API key missing");
			const responseFormat = resolveSpeechResponseFormat(config.baseUrl, req.target, config.responseFormat);
			return {
				audioBuffer: await openaiTTS({
					text: req.text,
					apiKey,
					baseUrl: config.baseUrl,
					model: overrides.model ?? config.model,
					voice: overrides.voice ?? config.voice,
					speed: overrides.speed ?? config.speed,
					instructions: config.instructions,
					responseFormat,
					timeoutMs: req.timeoutMs
				}),
				outputFormat: responseFormat,
				fileExtension: responseFormatToFileExtension(responseFormat),
				voiceCompatible: req.target === "voice-note" && responseFormat === "opus"
			};
		},
		synthesizeTelephony: async (req) => {
			const config = readOpenAIProviderConfig(req.providerConfig);
			const apiKey = config.apiKey || process.env.OPENAI_API_KEY;
			if (!apiKey) throw new Error("OpenAI API key missing");
			const outputFormat = "pcm";
			return {
				audioBuffer: await openaiTTS({
					text: req.text,
					apiKey,
					baseUrl: config.baseUrl,
					model: config.model,
					voice: config.voice,
					speed: config.speed,
					instructions: config.instructions,
					responseFormat: outputFormat,
					timeoutMs: req.timeoutMs
				}),
				outputFormat,
				sampleRate: 24e3
			};
		}
	};
}
//#endregion
export { buildOpenAISpeechProvider as t };
