import { createLogger } from "../utils/logger/index.js";
import { init_logger } from "../utils/logger.js";
import { runCapability } from "./runner.js";
//#region src/media-understanding/audio-transcription-runner.ts
init_logger();
const log = createLogger("AudioTranscriptionRunner");
async function runAudioTranscription(options) {
	if (options.providers.length === 0) log.warn({ attachmentCount: options.attachments.length }, "No audio providers configured; transcription disabled");
	const configById = /* @__PURE__ */ new Map();
	for (const cfg of options.providers) configById.set(cfg.id.toLowerCase(), cfg);
	const result = await runCapability({
		capability: "audio",
		attachments: options.attachments,
		providerOrder: options.providers.map((cfg) => cfg.id),
		signal: options.signal,
		buildRequest: ({ provider, attachment }) => buildAudioRequest({
			provider,
			attachment,
			config: configById.get(provider.id.toLowerCase()),
			timeoutMs: options.timeoutMs
		})
	});
	const outputs = result.outputs.map((entry) => ({
		attachmentIndex: entry.attachmentIndex,
		text: entry.text,
		provider: entry.provider,
		...entry.model ? { model: entry.model } : {}
	}));
	return {
		transcript: outputs.length === 0 ? void 0 : outputs.slice().sort((a, b) => a.attachmentIndex - b.attachmentIndex).map((o) => o.text).join("\n").trim() || void 0,
		outputs,
		decision: result.decision
	};
}
function buildAudioRequest(params) {
	const { provider, attachment, config, timeoutMs } = params;
	if (!config) {
		log.debug({
			providerId: provider.id,
			attachmentIndex: attachment.attachmentIndex
		}, `No resolved config for provider "${provider.id}"; skipping`);
		return;
	}
	if (provider.requiresApiKey !== false && !config.apiKey) {
		log.debug({
			providerId: provider.id,
			attachmentIndex: attachment.attachmentIndex
		}, `Provider "${provider.id}" has no apiKey; skipping`);
		return;
	}
	return {
		buffer: attachment.buffer,
		fileName: attachment.fileName,
		...attachment.mime ? { mime: attachment.mime } : {},
		...config.apiKey ? { apiKey: config.apiKey } : {},
		...config.baseUrl ? { baseUrl: config.baseUrl } : {},
		...config.headers ? { headers: config.headers } : {},
		...config.model ?? provider.defaultModels?.audio ? { model: config.model ?? provider.defaultModels?.audio } : {},
		...config.language ? { language: config.language } : {},
		...config.prompt ? { prompt: config.prompt } : {},
		...config.query ? { query: config.query } : {},
		timeoutMs
	};
}
//#endregion
export { runAudioTranscription };

//# sourceMappingURL=audio-transcription-runner.js.map