Skip to content

Open-source platform for 3D AI agents. Turn text or a photo into a rigged, animated GLB avatar, give it an LLM brain, memory and a wallet, and embed it anywhere with one web component. Remote MCP server, x402 payments, Three.js, browser-native.

4.9 KB
Raw Blame
/**
 * Voice-clone modal opened from inside Talk mode.
 *
 * Wraps the shared VoiceSetup panel in a modal shell so the avatar's owner can
 * clone their voice in-place without leaving talk mode. On close, the caller is
 * responsible for refreshing whichever cached voice lookup it has — typically
 * TalkController.refreshVoice().
 *
 * VoiceSetup POSTs to /api/agents/:id/voice/clone (real ElevenLabs cloning,
 * BYOK or platform key, rate-limited 3/day) and owns every capture, credential,
 * and failure state. This file is only the modal chrome.
 */

import { VoiceSetup } from './voice-setup.js';
import { log } from '../shared/log.js';

let active = null;

/**
 * @param {object} opts
 * @param {string} opts.agentId   Required — voice is cloned against an agent.
 * @param {string} [opts.agentName='Avatar']
 * @param {() => void} [opts.onClose]  Fires after the modal closes (clone or cancel).
 */
export function openVoiceCloneModal({ agentId, agentName = 'Avatar', onClose } = {}) {
	if (!agentId) {
		log.warn('[voice-clone] no agentId — owner has no linked agent yet');
		return null;
	}
	if (active) return active; // single instance

	injectStylesOnce();

	const root = document.createElement('div');
	root.className = 'tws-vcm-root';
	root.innerHTML = `
		<div class="tws-vcm-backdrop" data-ref="backdrop"></div>
		<div class="tws-vcm-card" role="dialog" aria-modal="true" aria-labelledby="tws-vcm-title">
			<div class="tws-vcm-head">
				<div>
					<div class="tws-vcm-eyebrow">three.ws</div>
					<h2 class="tws-vcm-title" id="tws-vcm-title">Clone your voice</h2>
				</div>
				<button class="tws-vcm-close" data-ref="close" aria-label="Close">✕</button>
			</div>
			<p class="tws-vcm-help">
				Read the script at a natural pace for 30 seconds or more. Once cloned,
				this avatar speaks in your voice everywhere on three.ws.
			</p>
			<div data-ref="recorder-host"></div>
		</div>
	`;
	document.body.appendChild(root);
	document.body.classList.add('tws-vcm-open');

	const host = root.querySelector('[data-ref="recorder-host"]');
	const recorder = new VoiceSetup(host, { bind: 'now', agentId, agentName });
	recorder.mount();

	const close = () => {
		if (!active) return;
		try {
			recorder.destroy();
		} catch {
			// Already torn down; closing the modal must never throw.
		}
		window.removeEventListener('keydown', onKey);
		document.body.classList.remove('tws-vcm-open');
		root.remove();
		active = null;
		try {
			onClose?.();
		} catch (err) {
			log.warn('[voice-clone] onClose threw', err?.message);
		}
	};

	const onKey = (e) => {
		if (e.key === 'Escape') {
			e.preventDefault();
			close();
		}
	};
	window.addEventListener('keydown', onKey);

	root.querySelector('[data-ref="backdrop"]').addEventListener('click', close);
	root.querySelector('[data-ref="close"]').addEventListener('click', close);

	active = { close };
	return active;
}

export function closeVoiceCloneModal() {
	active?.close();
}

// ── styles ─────────────────────────────────────────────────────────────────

function injectStylesOnce() {
	if (document.getElementById('tws-vcm-css')) return;
	const s = document.createElement('style');
	s.id = 'tws-vcm-css';
	s.textContent = `
.tws-vcm-open { overflow: hidden; }
.tws-vcm-root {
	position: fixed; inset: 0; z-index: 10001;
	display: grid; place-items: center;
	padding: 24px;
	animation: tws-vcm-in 180ms ease-out;
}
@keyframes tws-vcm-in { from { opacity: 0; } to { opacity: 1; } }
.tws-vcm-backdrop {
	position: absolute; inset: 0;
	background: rgba(5, 5, 5, 0.78);
	backdrop-filter: blur(6px);
}
.tws-vcm-card {
	position: relative;
	width: 100%;
	max-width: 520px;
	max-height: calc(100vh - 48px);
	overflow-y: auto;
	background: #0d0d0d;
	border: 1px solid rgba(255, 255, 255, 0.08);
	border-radius: 16px;
	padding: 22px 24px 20px;
	color: #fafafa;
	font-family: 'Inter', system-ui, sans-serif;
	box-shadow: 0 20px 60px rgba(0, 0, 0, 0.55);
}
.tws-vcm-head {
	display: flex;
	align-items: flex-start;
	justify-content: space-between;
	gap: 12px;
	margin-bottom: 10px;
}
.tws-vcm-eyebrow {
	font-family: 'Space Grotesk', sans-serif;
	font-size: 10px;
	letter-spacing: 0.18em;
	text-transform: uppercase;
	color: #ffffff;
	margin-bottom: 4px;
}
.tws-vcm-title {
	margin: 0;
	font-size: 22px;
	font-weight: 700;
	letter-spacing: -0.01em;
	font-family: 'Space Grotesk', sans-serif;
}
.tws-vcm-close {
	background: rgba(255, 255, 255, 0.06);
	border: 1px solid rgba(255, 255, 255, 0.12);
	color: #fafafa;
	width: 32px;
	height: 32px;
	border-radius: 999px;
	cursor: pointer;
	font-size: 13px;
	font-family: inherit;
	flex-shrink: 0;
}
.tws-vcm-close:hover {
	background: rgba(255, 255, 255, 0.12);
	border-color: rgba(255, 255, 255, 0.25);
}
.tws-vcm-help {
	margin: 0 0 14px;
	font-size: 13px;
	line-height: 1.5;
	color: #a1a1aa;
}
`;
	document.head.appendChild(s);
}