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.

fix(voice): let an embedded agent speak in the voice its owner bound

nirholas20eadb3712ac
diff --git a/pages/agent-embed.html b/pages/agent-embed.html
index 852998b2e..f80d7ae1f 100644
--- a/pages/agent-embed.html
+++ b/pages/agent-embed.html
@@ -120,7 +120,7 @@
import { AgentAvatar } from '/src/agent-avatar.js';
import { protocol } from '/src/agent-protocol.js';
import { EmbedActionBridge } from '/src/embed-action-bridge.js';
- import { createTTS } from '/src/runtime/speech.js';
+ import { createTTS, agentVoiceConfig } from '/src/runtime/speech.js';
const BRIDGE_VERSION = '1';
const CAPABILITIES = ['speak', 'gesture', 'look-at', 'emote', 'present-model'];
@@ -509,17 +509,20 @@
avatarCtl.attach();
// Bind real TTS so `speak` actions produce ElevenLabs audio + lip-sync.
- // Voice id comes from the agent record — no silent fallback to a default
- // voice; speak reports "agent has no voice configured" when unset.
+ // The whole config (voice id, model, saved settings, and the agent id
+ // that authorizes an owner-BYOK clone for anonymous listeners) comes
+ // from the agent record via agentVoiceConfig — no silent fallback to a
+ // default voice; speak reports "agent has no voice configured" when
+ // nothing is bound.
ttsCtx.viewer = viewer;
ttsCtx.avatar = avatarCtl;
try {
const ar = await fetch(`/api/agents/${agentId}`, { credentials: 'include' });
if (ar.ok) {
const { agent } = await ar.json();
- const voiceId = agent?.voice_id || null;
- if (voiceId) {
- ttsCtx.tts = createTTS({ provider: 'elevenlabs', voiceId, proxyURL: '/api/tts/eleven' });
+ const voiceCfg = agentVoiceConfig(agent, { agentId });
+ if (voiceCfg) {
+ ttsCtx.tts = createTTS(voiceCfg);
avatarCtl.setTTS(ttsCtx.tts);
}
}
diff --git a/src/runtime/index.js b/src/runtime/index.js
index f019b5c53..ad336e9f5 100644
--- a/src/runtime/index.js
+++ b/src/runtime/index.js
@@ -41,7 +41,15 @@ export class Runtime extends EventTarget {
...this.manifest.brain,
...providerConfig,
});
- this.tts = createTTS({ ...this.manifest.voice?.tts, ...voiceConfig.tts });
+ // agentId goes in first so an explicit manifest/voiceConfig value still wins,
+ // but a runtime that knows which agent it is never has to be told twice: the
+ // /api/tts/eleven proxy needs it to serve a voice cloned on the owner's own
+ // ElevenLabs key, which is the only lane that works without a platform key.
+ this.tts = createTTS({
+ ...(this.agentId ? { agentId: this.agentId } : {}),
+ ...this.manifest.voice?.tts,
+ ...voiceConfig.tts,
+ });
this.stt = createSTT({ ...this.manifest.voice?.stt, ...voiceConfig.stt });
this.messages = [];
diff --git a/src/runtime/speech.js b/src/runtime/speech.js
index 88c8a7ff9..c6bff511b 100644
--- a/src/runtime/speech.js
+++ b/src/runtime/speech.js
@@ -541,6 +541,52 @@ export function createTTS(config = {}) {
return factory(config);
}
+/**
+ * Turn a public agent record into the TTS config that plays its bound voice.
+ *
+ * Every surface that speaks as an agent (the hosted embed, the SDK runtime)
+ * needs the same three things from the agent row, and getting any of them
+ * wrong is silent: the agent just falls back to a generic voice or goes mute.
+ *
+ * voice_id — which voice to speak with.
+ * agentId — WHICH AGENT this voice belongs to. The /api/tts/eleven
+ * proxy only serves an owner-BYOK clone when the request
+ * names its agent, because that is what authorizes spending
+ * the owner's ElevenLabs credential. Omit it and a bound
+ * voice is unreachable to embed visitors and signed-out
+ * listeners, which is exactly who embeds are for.
+ * voice_model /
+ * voice_settings — the tuning the owner saved in the editor. Dropping these
+ * plays the right voice with the wrong delivery.
+ *
+ * Returns null when the agent has no ElevenLabs voice bound, so callers can
+ * render their own "no voice configured" state rather than guessing a default.
+ *
+ * @param {{id?: string, voice_provider?: string, voice_id?: string|null,
+ * voice_model?: string|null, voice_settings?: object|null}|null} agent
+ * @param {{ proxyURL?: string, agentId?: string }} [opts]
+ * @returns {object|null} a createTTS config, or null when nothing is bound.
+ */
+export function agentVoiceConfig(agent, { proxyURL = '/api/tts/eleven', agentId } = {}) {
+ const voiceId = agent?.voice_id || null;
+ if (!voiceId || (agent?.voice_provider && agent.voice_provider !== 'elevenlabs')) return null;
+
+ const id = agentId || agent?.id || null;
+ const s = agent?.voice_settings && typeof agent.voice_settings === 'object' ? agent.voice_settings : null;
+
+ return {
+ provider: 'elevenlabs',
+ voiceId,
+ proxyURL,
+ ...(id ? { agentId: id } : {}),
+ ...(agent?.voice_model ? { modelId: agent.voice_model } : {}),
+ ...(s?.stability != null ? { stability: Number(s.stability) } : {}),
+ ...(s?.similarity_boost != null ? { similarityBoost: Number(s.similarity_boost) } : {}),
+ ...(s?.style != null ? { style: Number(s.style) } : {}),
+ ...(s?.use_speaker_boost != null ? { useSpeakerBoost: !!s.use_speaker_boost } : {}),
+ };
+}
+
export function createSTT(config = {}) {
const provider = config.provider || 'browser';
const factory = STT_FACTORIES[provider];
diff --git a/tests/runtime/agent-voice-config.test.js b/tests/runtime/agent-voice-config.test.js
new file mode 100644
index 000000000..b8174bf92
--- /dev/null
+++ b/tests/runtime/agent-voice-config.test.js
@@ -0,0 +1,104 @@
+// agentVoiceConfig — the mapper every surface uses to play an agent's bound voice.
+//
+// The failure this guards against is silent, which is why it is worth a test: an
+// agent whose voice was cloned on its owner's own ElevenLabs key is only served
+// by /api/tts/eleven when the request names the agent. Drop `agentId` from the
+// TTS config and the embed still "works" — it just goes mute for every visitor
+// who is not the signed-in owner, which is everyone an embed exists for.
+
+import { describe, it, expect } from 'vitest';
+import { agentVoiceConfig, createTTS, ElevenLabsTTS } from '../../src/runtime/speech.js';
+
+const BOUND_AGENT = {
+ id: '11111111-2222-3333-4444-555555555555',
+ voice_provider: 'elevenlabs',
+ voice_id: 'voice_abc123',
+ voice_model: 'eleven_turbo_v2_5',
+ voice_settings: {
+ stability: 0.31,
+ similarity_boost: 0.82,
+ style: 0.44,
+ use_speaker_boost: false,
+ },
+};
+
+describe('agentVoiceConfig', () => {
+ it('carries the agent id so an owner-BYOK clone is reachable anonymously', () => {
+ const cfg = agentVoiceConfig(BOUND_AGENT);
+ expect(cfg.agentId).toBe(BOUND_AGENT.id);
+ expect(cfg.provider).toBe('elevenlabs');
+ expect(cfg.voiceId).toBe('voice_abc123');
+ expect(cfg.proxyURL).toBe('/api/tts/eleven');
+ });
+
+ it('prefers an explicitly supplied agentId over the record id', () => {
+ const cfg = agentVoiceConfig(BOUND_AGENT, { agentId: 'route-param-id' });
+ expect(cfg.agentId).toBe('route-param-id');
+ });
+
+ it('falls back to the record id when the caller supplies none', () => {
+ const cfg = agentVoiceConfig({ ...BOUND_AGENT }, {});
+ expect(cfg.agentId).toBe(BOUND_AGENT.id);
+ });
+
+ it('maps the saved snake_case settings onto the TTS constructor names', () => {
+ const cfg = agentVoiceConfig(BOUND_AGENT);
+ expect(cfg.modelId).toBe('eleven_turbo_v2_5');
+ expect(cfg.stability).toBe(0.31);
+ expect(cfg.similarityBoost).toBe(0.82);
+ expect(cfg.style).toBe(0.44);
+ expect(cfg.useSpeakerBoost).toBe(false);
+ });
+
+ it('omits tuning the owner never set, so ElevenLabs defaults survive', () => {
+ const cfg = agentVoiceConfig({ id: 'a1', voice_provider: 'elevenlabs', voice_id: 'v1' });
+ expect(cfg).not.toHaveProperty('modelId');
+ expect(cfg).not.toHaveProperty('stability');
+ expect(cfg).not.toHaveProperty('useSpeakerBoost');
+ const tts = createTTS(cfg);
+ expect(tts).toBeInstanceOf(ElevenLabsTTS);
+ expect(tts.modelId).toBe('eleven_flash_v2_5');
+ expect(tts.stability).toBe(0.5);
+ expect(tts.useSpeakerBoost).toBe(true);
+ });
+
+ it('honours use_speaker_boost:false rather than treating it as unset', () => {
+ const cfg = agentVoiceConfig({
+ id: 'a1',
+ voice_provider: 'elevenlabs',
+ voice_id: 'v1',
+ voice_settings: { use_speaker_boost: false },
+ });
+ expect(cfg.useSpeakerBoost).toBe(false);
+ expect(createTTS(cfg).useSpeakerBoost).toBe(false);
+ });
+
+ it('returns null when nothing is bound, so callers render their own empty state', () => {
+ expect(agentVoiceConfig(null)).toBeNull();
+ expect(agentVoiceConfig({})).toBeNull();
+ expect(agentVoiceConfig({ id: 'a1', voice_provider: 'browser', voice_id: null })).toBeNull();
+ });
+
+ it('returns null for a non-ElevenLabs provider even when a voice id lingers', () => {
+ expect(agentVoiceConfig({ id: 'a1', voice_provider: 'browser', voice_id: 'stale' })).toBeNull();
+ });
+
+ it('survives a malformed voice_settings blob instead of throwing', () => {
+ const cfg = agentVoiceConfig({
+ id: 'a1',
+ voice_provider: 'elevenlabs',
+ voice_id: 'v1',
+ voice_settings: 'not-an-object',
+ });
+ expect(cfg.voiceId).toBe('v1');
+ expect(cfg).not.toHaveProperty('stability');
+ });
+
+ it('builds a config createTTS accepts, with the agent id reaching the instance', () => {
+ const tts = createTTS(agentVoiceConfig(BOUND_AGENT));
+ expect(tts).toBeInstanceOf(ElevenLabsTTS);
+ expect(tts.agentId).toBe(BOUND_AGENT.id);
+ expect(tts.proxyURL).toBe('/api/tts/eleven');
+ expect(tts.modelId).toBe('eleven_turbo_v2_5');
+ });
+});