//#region \0rolldown/runtime.js
var __defProp = Object.defineProperty;
var __exportAll = (all, no_symbols) => {
	let target = {};
	for (var name in all) {
		__defProp(target, name, {
			get: all[name],
			enumerable: true
		});
	}
	if (!no_symbols) {
		__defProp(target, Symbol.toStringTag, { value: "Module" });
	}
	return target;
};

//#endregion
//#region extensions/voice-call/src/realtime/webrtc-session-manager.ts
var webrtc_session_manager_exports = /* @__PURE__ */ __exportAll({ WebRTCSessionManager: () => WebRTCSessionManager });
const SESSION_ENDPOINT = "https://api.openai.com/v1/realtime/sessions";
const SESSION_CLEANUP_INTERVAL_MS = 6e4;
/**
* Creates and manages ephemeral tokens for WebRTC clients.
*/
var WebRTCSessionManager = class {
	constructor(config) {
		this.activeSessions = /* @__PURE__ */ new Map();
		this.cleanupTimer = null;
		this.config = config;
		this.cleanupTimer = setInterval(() => this.cleanup(), SESSION_CLEANUP_INTERVAL_MS);
	}
	/**
	* Create an ephemeral token for a WebRTC client.
	*
	* The token is short-lived and allows the browser to establish a
	* WebRTC connection directly with OpenAI's Realtime API.
	*
	* @param overrides Optional per-request overrides for model, voice, instructions, tools
	* @returns Ephemeral token with expiry info
	*/
	async createEphemeralToken(overrides) {
		const sessionConfig = {
			model: overrides?.model ?? this.config.model ?? "gpt-4o-realtime-preview",
			voice: overrides?.voice ?? this.config.voice ?? "verse",
			modalities: ["audio", "text"]
		};
		if (overrides?.instructions ?? this.config.instructions) sessionConfig.instructions = overrides?.instructions ?? this.config.instructions;
		if (overrides?.tools ?? this.config.tools) sessionConfig.tools = overrides?.tools ?? this.config.tools;
		const response = await fetch(SESSION_ENDPOINT, {
			method: "POST",
			headers: {
				Authorization: `Bearer ${this.config.apiKey}`,
				"Content-Type": "application/json"
			},
			body: JSON.stringify(sessionConfig)
		});
		if (!response.ok) {
			const body = await response.text();
			throw new Error(`Failed to create ephemeral token: ${response.status} ${response.statusText} — ${body}`);
		}
		const data = await response.json();
		const token = {
			token: data.client_secret.value,
			expiresAt: data.client_secret.expires_at,
			sessionId: data.id
		};
		this.activeSessions.set(data.id, {
			sessionId: data.id,
			createdAt: Date.now(),
			expiresAt: data.client_secret.expires_at * 1e3,
			metadata: {}
		});
		return token;
	}
	/**
	* Track metadata for an active session.
	*/
	trackSession(sessionId, metadata) {
		const session = this.activeSessions.get(sessionId);
		if (session) session.metadata = {
			...session.metadata,
			...metadata
		};
	}
	/**
	* Get the number of active (non-expired) sessions.
	*/
	getActiveSessionCount() {
		this.cleanup();
		return this.activeSessions.size;
	}
	/**
	* Remove expired sessions.
	*/
	cleanup() {
		const now = Date.now();
		for (const [id, session] of this.activeSessions) if (session.expiresAt < now) this.activeSessions.delete(id);
	}
	/**
	* Stop the cleanup timer and clear all sessions.
	*/
	destroy() {
		if (this.cleanupTimer) {
			clearInterval(this.cleanupTimer);
			this.cleanupTimer = null;
		}
		this.activeSessions.clear();
	}
};

//#endregion
export { webrtc_session_manager_exports as n, WebRTCSessionManager as t };