Skip to content
Source
Sign inSign up
TanStack/ai

feat(ai): live speech-to-text sessions with seven provider adapters and a voice support agent example #1964

Opentombeckenhamwants to merge 30 commits intomain fromrefs/pull/1964/headopened View on GitHub

tombeckenhamcommented

transcriptionSession() turns live audio into text on the server. Send audio chunks as they arrive, for example from a Twilio call, and iterate interim, final, turn-end, and error events. Then give each turn to chat(). It ships with seven providers: Deepgram (in the new @tanstack/ai-deepgram package), ElevenLabs, OpenAI, xAI, Gemini, Mistral, and Cloudflare Workers AI.

This PR now also contains #2048: a voice support agent example that uses the session on a call, plus the Gemini 3.8 TTS models and a logger change that the example needed.

🎯 Changes

  • Core (@tanstack/ai).
    • The transcriptionSession() activity and the LiveTranscriptionAdapter contract: audioInputs plus connect({ emit, end }).
    • If a provider does not take the input format, the session converts it: G.711 μ-law/A-law decode, plus PCM resampling. createAudioConverter() is exported.
    • openLiveSocket() in @tanstack/ai/adapter-internals, used by all the adapters. It rejects connect when the provider sends an error before it is ready, when the signal is already aborted, or after connectTimeoutMs (15 s). It also takes a socket that is already open, such as one from the Workers AI binding. If the peer never answers a close, the session ends 1 s later.
    • send() after close() starts is dropped, so no audio goes out after the last commit.
    • closeGraceMs and closeHandshakeMs (default 1000 ms each) set how long close() waits for late text and for the provider to answer the close.
    • fakeWebSocket() in @tanstack/ai/testing.
    • The errors debug category does not log a cancelled request. This includes a custom Error that the caller gives to abort().
  • Adapters.
    • deepgramLiveTranscription (Nova-3 on /v1/listen, Flux on /v2/listen) and cloudflareLiveTranscription (Nova-3 and Flux over the env.AI binding).
    • elevenlabsLiveTranscription, openaiLiveTranscription, grokLiveTranscription, geminiLiveTranscription, mistralLiveTranscription.
    • All use the standard WebSocket, so they run on Node, Bun, and Workers. Auth goes through a subprotocol, a query param, or a minted token, never custom headers.
    • Gemini TTS: gemini-3.8-flash-tts and gemini-3.8-flash-lite-tts. On these models, systemInstruction goes to speechMetadata.style, and turns set speechMetadata.speaker. multiSpeakerVoiceConfig without turns throws before the request. With Vertex AI or Google auth, a style or turns throws, because @google/genai drops speechMetadata.
    • geminiLiveTranscription rejects mode: 'SMART', because its finals repeat earlier text.
  • Example (from #2048). examples/react/voice-support-agent: click Start call, ask about TanStack AI, and hear the answer. A Twilio number can point at the same server. Each hop (listen, think, speak) runs on OpenAI, Gemini, or Grok, with your own keys or env keys.
  • Docs.
    • New pages: docs/media/live-transcription.md, docs/media/live-transcription-advanced.md, docs/adapters/deepgram.md, and the tutorial docs/tutorials/voice-support-agent.md.
    • A "Live transcription" section on the ElevenLabs, OpenAI, Grok, and Gemini adapter pages. The media-generation skill is updated.
  • Not in this PR. Vertex, BytePlus Seed ASR, and fal (their sockets need an auth header), Cloudflare over REST outside a Worker, AssemblyAI, middleware and devtools events for sessions, and Deepgram KeepAlive.

Decisions on the open questions of the issue: events are an async iterator and a plain union (not AG-UI), conversion lives in @tanstack/ai, and there is no E2E test because aimock cannot mock live-audio WebSockets.

✅ Checklist

  • I have followed the steps in the Contributing guide.
  • I have tested code changes locally with pnpm run test:pr, or these tests do not apply to this pull request.
  • I fully understand the code in this pull request, including any code generated with AI assistance.
  • Docs: I updated docs/ for this change, or this change is not user-facing.
  • Changeset: I added a changeset (pnpm changeset), or this PR does not change a published package.

🚀 Release Impact

  • This change affects published code, and I have generated a changeset.
  • This change is docs/CI/dev-only (no release).

Testing

Commands run.

  • Earlier: vitest run on the live transcription tests of the eight packages, test:types and test:oxlint on those packages, and kiira check on the changed docs. In the example: tsc, oxlint, oxfmt --check, vite build, and pnpm test:docs.
  • For the review fixes in 3e02f1844, 800f0054b, 426efd222, 3bad32abc, 8d2c632db, and c18fcd35e: vitest run on the touched test files in ai, ai-openai, ai-elevenlabs, and ai-gemini. Each new test fails on the old code. Also test:types and test:oxlint on those packages, and test:types on the example.
  • Not run locally: pnpm test:pr and the E2E suite. CI runs them.

Live runs. A recorded sentence streamed in real time gave the correct text from OpenAI (gpt-transcribe, gpt-live-transcribe), ElevenLabs, xAI, Gemini, and Workers AI (Nova-3 and Flux). Deepgram direct and Mistral have unit tests only. The example had one live browser call on OpenAI. The review fixes in the example were not run live.

Manual test.

  1. Run pnpm install && pnpm build.
  2. Set OPENAI_API_KEY in examples/react/voice-support-agent/.env.local.
  3. Run pnpm --filter voice-support-agent dev, open http://localhost:3100, and click the green handset.
  4. Say "How do I add a server tool?" and pause. Expect your words in the captions, then a spoken answer.
  5. Talk over a long reply. Expect playback to stop at once.

How this PR makes testing easy.

  • Each adapter has a tests/live-transcription.test.ts. fakeWebSocket() tests an adapter with no network.
  • packages/ai/tests/transcription-session.test.ts and live-socket.test.ts cover the session and the socket.
  • The example app is the end-to-end path. Its README has the browser steps and the TwiML for a Twilio number.

Linked issues

Closes #1944

Risk / rollback

Low for existing users. The session, adapters, and example are new. The changed existing behavior is small: the logger skips cancelled requests, and Gemini 3.8 TTS throws for multiSpeakerVoiceConfig without turns. To undo, revert the PR.

The Twilio path of the example now needs TWILIO_STREAM_TOKEN, and /voice takes connections only from the page on localhost (same Origin). The README explains both.

Public API change

Before

// Only finished files could be transcribed.
await generateTranscription({ adapter: openaiTranscription('gpt-4o-transcribe'), audio: file, threadId })

After

import { transcriptionSession } from '@tanstack/ai'
import { deepgramLiveTranscription } from '@tanstack/ai-deepgram'

const session = await transcriptionSession({
  adapter: deepgramLiveTranscription('nova-3'),
  audio: { encoding: 'mulaw', sampleRate: 8000 },
})
twilio.addEventListener('message', (e) => session.send(payloadOf(e.data)))
for await (const event of session) if (event.type === 'final') console.log(event.text)

Sign in to join this conversation.