import { aI as readStackEnv } from './src-CidJbygh.js';
import { g as getState } from './state-fxN45gBp.js';
import { d as getRequestId, r as requireAdmin, e as errorResponse, k as jsonResponse } from './helpers-kzwksNq1.js';
import './chunk-CLZ62Ad-.js';
import 'node:module';
import './provider-constants-DPrj3yBX.js';
import './dist-DOFcG0Zp.js';
import 'node:fs';
import 'node:path';
import 'buffer';
import 'fs';
import 'path';
import 'node:child_process';
import 'node:url';
import 'node:crypto';
import 'events';
import 'node:events';
import 'node:stream';
import 'node:string_decoder';
import 'assert';
import 'zlib';
import 'node:assert';
import 'node:fs/promises';
import 'node:os';
import './endpoints-UdQGgKsR.js';

//#region src/routes/api/transcribe/+server.ts
var DEFAULT_MODEL = "whisper-1";
var UPSTREAM_TIMEOUT_MS = 6e4;
function redactKey(s) {
	return s.replace(/(sk-[A-Za-z0-9_-]{4,})/g, "sk-***").replace(/(hf_[A-Za-z0-9_-]{4,})/gi, "hf_***").replace(/(Bearer\s+)[A-Za-z0-9._-]+/gi, "$1***");
}
var POST = async (event) => {
	const requestId = getRequestId(event);
	const authError = requireAdmin(event, requestId);
	if (authError) return authError;
	const stackEnv = readStackEnv(getState().stackDir);
	const sttBaseURL = (stackEnv.OP_STT_BASE_URL || process.env.OP_STT_BASE_URL || "").trim();
	const sttModel = (stackEnv.OP_STT_MODEL || process.env.OP_STT_MODEL || "").trim() || DEFAULT_MODEL;
	const sttLanguageEnv = (stackEnv.OP_STT_LANGUAGE || process.env.OP_STT_LANGUAGE || "").trim();
	const sttApiKey = (process.env.OP_STT_API_KEY ?? "").trim();
	if (!sttBaseURL) return errorResponse(503, "stt_not_configured", "Configure an STT engine in Admin → Voice settings.", {}, requestId);
	let inboundForm;
	try {
		inboundForm = await event.request.formData();
	} catch {
		return errorResponse(400, "bad_request", "Body must be multipart/form-data", {}, requestId);
	}
	const audio = inboundForm.get("audio");
	if (!(audio instanceof Blob)) return errorResponse(400, "bad_request", "Missing \"audio\" field (Blob)", {}, requestId);
	const languageReq = inboundForm.get("language");
	const promptReq = inboundForm.get("prompt");
	const language = typeof languageReq === "string" && languageReq.trim() ? languageReq.trim() : sttLanguageEnv;
	const outForm = new FormData();
	const filename = audio.name || "recording.webm";
	outForm.append("file", audio, filename);
	outForm.append("model", sttModel);
	outForm.append("response_format", "json");
	if (language) outForm.append("language", language);
	if (typeof promptReq === "string" && promptReq.trim()) outForm.append("prompt", promptReq.trim());
	const upstreamUrl = sttBaseURL.replace(/\/+$/, "") + "/v1/audio/transcriptions";
	const headers = {};
	if (sttApiKey) headers["authorization"] = `Bearer ${sttApiKey}`;
	let upstream;
	try {
		upstream = await fetch(upstreamUrl, {
			method: "POST",
			headers,
			body: outForm,
			signal: AbortSignal.timeout(UPSTREAM_TIMEOUT_MS)
		});
	} catch (err) {
		return errorResponse(502, "upstream_error", `Could not reach STT endpoint: ${redactKey(err instanceof Error ? err.message : String(err))}`, { upstream: upstreamUrl }, requestId);
	}
	if (!upstream.ok) {
		const snippet = redactKey(await upstream.text().catch(() => "")).slice(0, 500);
		return errorResponse(502, "upstream_error", `STT endpoint returned ${upstream.status}`, {
			upstreamStatus: upstream.status,
			body: snippet
		}, requestId);
	}
	let payload;
	try {
		payload = await upstream.json();
	} catch {
		return errorResponse(502, "upstream_error", "STT endpoint returned a non-JSON response", { upstreamStatus: upstream.status }, requestId);
	}
	return jsonResponse(200, { text: typeof payload?.text === "string" ? payload.text : "" }, requestId);
};

export { POST };
//# sourceMappingURL=_server.ts-C7C_JnLA.js.map
