# badrinathantv/openvoice-ai architecture

> Architecture diagram of the GitHub repository badrinathantv/openvoice-ai, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-15). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/badrinathantv/openvoice-ai
- Repository: https://github.com/badrinathantv/openvoice-ai

## Overview

#### Purpose and Entry Points
OpenVoice AI is a browser-based, real-time voice/text assistant with a FastAPI backend and React/Vite client. Backend startup exposes HTTP, WebRTC, and WebSocket interfaces through `backend/src/api/server.py`; the browser boots from `frontend/src/main.jsx` into `frontend/src/App.jsx`. Container/process launch is supported by `Dockerfile`, `entrypoint.sh`, and `start.sh`.

#### Client Orchestration and UI
The frontend presents agent identity, transcript, mic/text controls, and audio-state visualizations. `frontend/src/hooks/useVoicePipeline.js` coordinates transport, capture, playback, and UI state; it composes WebRTC-first connectivity and WebSocket fallback from `frontend/src/hooks/useWebRTC.js` and `frontend/src/hooks/useWebSocket.js`. Agent metadata is a client-side boundary in `frontend/src/config/agents.js`.

#### API and Transport Boundary
`backend/src/api/server.py` owns FastAPI REST health/agent endpoints plus `/api/webrtc/offer`, `/ws/voice`, and `/ws/chat`. Voice traffic carries PCM and control events over WebRTC data channels primarily, with WebSocket fallback; text chat is JSON WebSocket traffic. The API delegates per-connection processing to `backend/src/api/web_pipeline.py`, keeping transport lifecycle separate from inference orchestration.

#### Voice Processing Pipeline
`backend/src/api/web_pipeline.py` processes inbound utterances through optional denoising, speech detection/buffering, transcription, agent response streaming, sentence chunking, and synthesized PCM output. Model adapters are separated by interfaces in `backend/src/core/interfaces.py`:
- VAD: `backend/src/vad/silero.py`
- ASR: `backend/src/asr/qwen.py`
- Noise reduction: `backend/src/audio/denoiser.py`
- TTS: `backend/src/tts/piper.py` (with `backend/src/tts/kitten.py` available)
Streaming text is segmented before TTS by `backend/src/utils/chunker.py`; language filtering occurs at `backend/src/utils/language_guard.py`.

#### Agent Swarm and Session Memory
`backend/src/agents/session.py` builds the LangGraph Swarm conversation graph and retains session/checkpoint state. `backend/src/agents/state.py` defines shared voice/conversation state. Specialized agents provide domain prompts, tools, and transfer behavior:
`backend/src/agents/specialized/customer_care.py`,
`backend/src/agents/specialized/shopper.py`, and
`backend/src/agents/specialized/order_ops.py`.
The LLM-facing wrapper in `backend/src/llm/client.py` connects streaming generation to this session graph. Agent handoffs are surfaced back through pipeline status/agent events for client orb updates.

#### Runtime, Models, and External Dependencies
Backend requires Python 3.12 and runs under Uvicorn/FastAPI; frontend requires Node 18+ with React 19/Vite. CUDA is optional but intended for Silero, Qwen ASR, and ONNX TTS/denoising. OpenAI GPT-4o-mini is the primary LLM service, with Groq configured as an alternative. Qwen ASR can run through Transformers or optional vLLM streaming. LangGraph `MemorySaver` provides in-process session persistence; `DATABASE_URL` supports MongoDB configuration but the documented conversation checkpoint is in memory.

#### End-to-End Control Flow
Browser microphone PCM enters WebRTC or fallback WebSocket, then `web_pipeline` performs VAD and utterance assembly. Accepted audio is transcribed, English-gated, and submitted to the LangGraph session. The active agent may transfer control; generated tokens stream through sentence chunking to TTS, and synthesized PCM plus transcript/status/agent events return to the browser. Text mode bypasses microphone/VAD/ASR but shares the agent-session and streamed response path.

Main entry points: [`backend/src/agents/session.py`](https://github.com/badrinathantv/openvoice-ai/blob/master/backend/src/agents/session.py) (Swarm session graph), [`backend/src/api/web_pipeline.py`](https://github.com/badrinathantv/openvoice-ai/blob/master/backend/src/api/web_pipeline.py) (Connection pipeline), [`backend/src/api/server.py`](https://github.com/badrinathantv/openvoice-ai/blob/master/backend/src/api/server.py) (FastAPI server), [`frontend/src/hooks/useVoicePipeline.js`](https://github.com/badrinathantv/openvoice-ai/blob/master/frontend/src/hooks/useVoicePipeline.js) (Voice pipeline hook)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_client["Browser Client"]
  node_app["React app<br/>React/Vite UI<br/>[App.jsx]"]
  node_voice_hook["Voice pipeline hook<br/>client orchestrator"]
  node_webrtc_hook["WebRTC transport<br/>client transport<br/>[useWebRTC.js]"]
  node_websocket_hook["WebSocket transport<br/>client transport<br/>[useWebSocket.js]"]
  node_agent_config["Agent metadata<br/>client config boundary<br/>[agents.js]"]
end

subgraph group_transport["API &amp; Transport"]
  node_server["FastAPI server<br/>[server.py]"]
  node_web_pipeline["Connection pipeline<br/>inference orchestrator<br/>[web_pipeline.py]"]
end

subgraph group_voice["Voice Pipeline"]
  node_vad["VAD &amp; utterance buffer<br/>speech detection<br/>[silero.py]"]
  node_asr["Qwen transcription<br/>ASR adapter<br/>[qwen.py]"]
  node_language_guard["English gate<br/>language filter<br/>[language_guard.py]"]
  node_chunker["Sentence chunker<br/>stream segmenter<br/>[chunker.py]"]
  node_tts["Piper synthesis<br/>TTS adapter<br/>[piper.py]"]
end

subgraph group_agents["Agent Runtime"]
  node_session["Swarm session graph<br/>LangGraph orchestration<br/>[session.py]"]
  node_agent_state["Conversation state<br/>shared state<br/>[state.py]"]
  node_specialists["Specialized agents<br/>[customer_care.py]"]
  node_llm_client["LLM streaming client<br/>generation wrapper<br/>[client.py]"]
end

subgraph group_external["Models &amp; Services"]
  node_llm_service{{"LLM providers<br/>OpenAI / Groq"}}
  node_memory[("MemorySaver<br/>in-process checkpoints")]
end

node_app -->|"UI actions and state"| node_voice_hook
node_agent_config -->|"agent identity"| node_app
node_voice_hook -->|"primary transport"| node_webrtc_hook
node_voice_hook -->|"fallback / text mode"| node_websocket_hook
node_webrtc_hook -->|"offer, PCM, control events"| node_server
node_websocket_hook -.->|"voice fallback and chat JSON"| node_server
node_server -->|"per-connection processing"| node_web_pipeline
node_web_pipeline -->|"microphone audio"| node_vad
node_vad -->|"accepted utterance"| node_asr
node_asr -->|"transcript"| node_language_guard
node_language_guard -->|"validated text"| node_session
node_web_pipeline -->|"text mode"| node_session
node_session -->|"reads and updates"| node_agent_state
node_session -->|"routes and transfers"| node_specialists
node_session -->|"streams generation"| node_llm_client
node_llm_client -->|"LLM requests"| node_llm_service
node_session -->|"checkpoints"| node_memory
node_llm_client -->|"streamed tokens"| node_chunker
node_chunker -->|"sentences"| node_tts
node_tts -->|"synthesized PCM"| node_web_pipeline
node_web_pipeline -->|"PCM, transcript, status, agent events"| node_server
node_server -->|"response events"| node_voice_hook

click node_app "https://github.com/badrinathantv/openvoice-ai/blob/master/frontend/src/App.jsx"
click node_voice_hook "https://github.com/badrinathantv/openvoice-ai/blob/master/frontend/src/hooks/useVoicePipeline.js"
click node_webrtc_hook "https://github.com/badrinathantv/openvoice-ai/blob/master/frontend/src/hooks/useWebRTC.js"
click node_websocket_hook "https://github.com/badrinathantv/openvoice-ai/blob/master/frontend/src/hooks/useWebSocket.js"
click node_agent_config "https://github.com/badrinathantv/openvoice-ai/blob/master/frontend/src/config/agents.js"
click node_server "https://github.com/badrinathantv/openvoice-ai/blob/master/backend/src/api/server.py"
click node_web_pipeline "https://github.com/badrinathantv/openvoice-ai/blob/master/backend/src/api/web_pipeline.py"
click node_vad "https://github.com/badrinathantv/openvoice-ai/blob/master/backend/src/vad/silero.py"
click node_asr "https://github.com/badrinathantv/openvoice-ai/blob/master/backend/src/asr/qwen.py"
click node_language_guard "https://github.com/badrinathantv/openvoice-ai/blob/master/backend/src/utils/language_guard.py"
click node_chunker "https://github.com/badrinathantv/openvoice-ai/blob/master/backend/src/utils/chunker.py"
click node_tts "https://github.com/badrinathantv/openvoice-ai/blob/master/backend/src/tts/piper.py"
click node_session "https://github.com/badrinathantv/openvoice-ai/blob/master/backend/src/agents/session.py"
click node_agent_state "https://github.com/badrinathantv/openvoice-ai/blob/master/backend/src/agents/state.py"
click node_specialists "https://github.com/badrinathantv/openvoice-ai/blob/master/backend/src/agents/specialized/customer_care.py"
click node_llm_client "https://github.com/badrinathantv/openvoice-ai/blob/master/backend/src/llm/client.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_app,node_voice_hook,node_webrtc_hook,node_websocket_hook,node_agent_config toneBlue
class node_server,node_web_pipeline toneAmber
class node_vad,node_asr,node_language_guard,node_chunker,node_tts toneMint
class node_session,node_agent_state,node_specialists,node_llm_client toneRose
class node_llm_service,node_memory toneIndigo
```

## Components

### Browser Client

- [React app](https://github.com/badrinathantv/openvoice-ai/blob/master/frontend/src/App.jsx): React/Vite UI · Displays transcript, controls, and agent/audio state. · `frontend/src/App.jsx`
- [Voice pipeline hook](https://github.com/badrinathantv/openvoice-ai/blob/master/frontend/src/hooks/useVoicePipeline.js): client orchestrator · Coordinates capture, transport, playback, and UI state. · `frontend/src/hooks/useVoicePipeline.js`
- [WebRTC transport](https://github.com/badrinathantv/openvoice-ai/blob/master/frontend/src/hooks/useWebRTC.js): client transport · Primary PCM and control-event channel. · `frontend/src/hooks/useWebRTC.js`
- [WebSocket transport](https://github.com/badrinathantv/openvoice-ai/blob/master/frontend/src/hooks/useWebSocket.js): client transport · Fallback voice transport and text-chat channel. · `frontend/src/hooks/useWebSocket.js`
- [Agent metadata](https://github.com/badrinathantv/openvoice-ai/blob/master/frontend/src/config/agents.js): client config boundary · `frontend/src/config/agents.js`

### API & Transport

- [FastAPI server](https://github.com/badrinathantv/openvoice-ai/blob/master/backend/src/api/server.py): HTTP / WebRTC / WebSocket API · Serves health and agent APIs plus offer, voice, and chat endpoints. · `backend/src/api/server.py`
- [Connection pipeline](https://github.com/badrinathantv/openvoice-ai/blob/master/backend/src/api/web_pipeline.py): inference orchestrator · Separates connection lifecycle from voice and chat processing. · `backend/src/api/web_pipeline.py`

### Voice Pipeline

- [VAD & utterance buffer](https://github.com/badrinathantv/openvoice-ai/blob/master/backend/src/vad/silero.py): speech detection · `backend/src/vad/silero.py`
- [Qwen transcription](https://github.com/badrinathantv/openvoice-ai/blob/master/backend/src/asr/qwen.py): ASR adapter · `backend/src/asr/qwen.py`
- [English gate](https://github.com/badrinathantv/openvoice-ai/blob/master/backend/src/utils/language_guard.py): language filter · `backend/src/utils/language_guard.py`
- [Sentence chunker](https://github.com/badrinathantv/openvoice-ai/blob/master/backend/src/utils/chunker.py): stream segmenter · `backend/src/utils/chunker.py`
- [Piper synthesis](https://github.com/badrinathantv/openvoice-ai/blob/master/backend/src/tts/piper.py): TTS adapter · Produces streamed PCM response audio. · `backend/src/tts/piper.py`

### Agent Runtime

- [Swarm session graph](https://github.com/badrinathantv/openvoice-ai/blob/master/backend/src/agents/session.py): LangGraph orchestration · Builds conversations and retains in-process checkpoint state. · `backend/src/agents/session.py`
- [Conversation state](https://github.com/badrinathantv/openvoice-ai/blob/master/backend/src/agents/state.py): shared state · `backend/src/agents/state.py`
- [Specialized agents](https://github.com/badrinathantv/openvoice-ai/blob/master/backend/src/agents/specialized/customer_care.py): customer care / shopper / order ops · Provide prompts, tools, and transfer behavior. · `backend/src/agents/specialized/customer_care.py`
- [LLM streaming client](https://github.com/badrinathantv/openvoice-ai/blob/master/backend/src/llm/client.py): generation wrapper · Connects streaming generation to the session graph. · `backend/src/llm/client.py`

### Models & Services

- **LLM providers**: OpenAI / Groq · GPT-4o-mini is primary; Groq is an alternative.
- **MemorySaver**: in-process checkpoints

## Connections

- React app → Voice pipeline hook: UI actions and state
- Agent metadata → React app: agent identity
- Voice pipeline hook → WebRTC transport: primary transport
- Voice pipeline hook → WebSocket transport: fallback / text mode
- WebRTC transport → FastAPI server: offer, PCM, control events
- WebSocket transport → FastAPI server: voice fallback and chat JSON
- FastAPI server → Connection pipeline: per-connection processing
- Connection pipeline → VAD & utterance buffer: microphone audio
- VAD & utterance buffer → Qwen transcription: accepted utterance
- Qwen transcription → English gate: transcript
- English gate → Swarm session graph: validated text
- Connection pipeline → Swarm session graph: text mode · Text chat bypasses VAD and ASR.
- Swarm session graph → Conversation state: reads and updates
- Swarm session graph → Specialized agents: routes and transfers
- Swarm session graph → LLM streaming client: streams generation
- LLM streaming client → LLM providers: LLM requests
- Swarm session graph → MemorySaver: checkpoints
- LLM streaming client → Sentence chunker: streamed tokens
- Sentence chunker → Piper synthesis: sentences
- Piper synthesis → Connection pipeline: synthesized PCM
- Connection pipeline → FastAPI server: PCM, transcript, status, agent events
- FastAPI server → Voice pipeline hook: response events
