# allenmylath/rustvani architecture

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

- Interactive diagram: https://gitdiagram.com/allenmylath/rustvani
- Repository: https://github.com/allenmylath/rustvani

## Overview

1. **What this repo is**
   - `rustvani` is a Rust voice-agent runtime built around a streaming pipeline: audio in, speech detection, transcription, LLM reasoning, response generation, and audio out.
   - The architecture mirrors Pipecat’s frame/processor model, but the implementation is Rust/Tokio-centric and optimized for low-latency, low-memory deployment.

2. **The core execution model**
   - The heart of the system is `frames/`, `pipeline/`, and `agents/`.
   - Data moves as typed `Frame`s through a chain of `FrameProcessor`s.
   - The pipeline is not just a sequence of calls; it is a runtime with queues, lifecycle events, interruption handling, and task orchestration via `PipelineTask`.

3. **Frame boundaries and flow control**
   - Frames are the main integration boundary between subsystems.
   - They carry system events, control signals, and data payloads such as audio chunks, transcriptions, LLM tokens, tool calls, and playback audio.
   - The two-queue design in processors separates lifecycle/system frames from normal data frames so interruptions and shutdowns are not blocked by audio backlog.

4. **Transport layer**
   - `transport/` is the external I/O boundary.
   - WebSocket transport is the primary runtime path (`src/transport/websocket/`), implemented with `axum` and async Rust networking.
   - Transport adapters inject inbound audio and protocol messages into the pipeline, and emit outbound audio and signaling frames back to the client.
   - The `ravi/` module sits alongside transport as a real-time protocol layer for client readiness, speaking state, transcription events, and token streaming.

5. **Audio pre-processing and voice activity detection**
   - `vad/` handles speech/no-speech detection and turn-state logic.
   - It supports a native Silero VAD path and an ONNX-backed path; both are used as a gate before STT and for interruption/turn control.
   - `audio_process/` provides noise suppression and resampling so transport audio can be normalized before inference.
   - `turn/` adds end-of-turn heuristics, which helps decide when user speech is complete and when the assistant can respond.

6. **Speech-to-text and text-to-speech services**
   - `services/stt/` contains streaming STT integrations, notably Sarvam’s WebSocket STT.
   - `services/tts/` contains both remote TTS backends and a local Piper ONNX path.
   - This is a clear architectural boundary: remote services handle scalable cloud inference, while local ONNX services keep latency-sensitive deployments self-contained.

7. **LLM integration and tool/function calling**
   - `services/llm/` and `adapters/` define the LLM provider boundary.
   - Provider adapters normalize wire formats for OpenAI-compatible and Sarvam-compatible LLMs.
   - `FunctionRegistry` is the tool execution boundary: the model requests a tool, Rust executes it, and the result is fed back into the LLM context.
   - The repository distinguishes between simple tool results and richer data-returning tool calls, which affects both LLM context and downstream UI/logging.

8. **Conversation context and aggregation**
   - `context/` holds the shared conversation state: messages, tool definitions, and tool-selection state.
   - `processors/` contains aggregators that collect streaming user and assistant fragments into coherent conversation turns.
   - These aggregators sit between STT/LLM/TTS stages and ensure the context is updated consistently rather than frame-by-frame.

9. **Conversation flow engine**
   - `dhara/` is a higher-level orchestration layer for multi-node conversational flows.
   - It lets the application move between named nodes, each with its own prompt/tooling/context strategy.
   - Tool handlers can either stay in the current node or transition to another node, so flow control is separate from the low-level audio/LLM pipeline.

10. **Built-in tools and external data access**
    - `tools/postgres/` implements a managed Postgres tool, including schema caching, query execution, structured filtering, and vector search.
    - This is a controlled external-service boundary: the LLM does not directly emit arbitrary SQL; it calls constrained tool interfaces instead.
    - The tool lifecycle is explicit, with startup/shutdown/cancel hooks so it can be attached to long-running voice sessions safely.

11. **Supporting infrastructure**
    - `observer/`, `metrics/`, and `clock/` provide supporting runtime abstractions for timing, observation, and instrumentation.
    - `utils/` contains text cleanup and sentence splitting used by TTS and conversational buffering.
    - `src/bin/` shows how the library is deployed in practice: WebSocket servers, voice servers, pipeline tests, and provider demos.

12. **How to read the system diagram**
    - The main end-to-end path is: transport input → VAD/audio cleanup → STT → user aggregation/context → LLM/tool loop → assistant aggregation → TTS → transport output.
    - The major architectural seams are transport, audio inference, LLM/provider adapters, tool execution, and conversation-flow management.
    - Everything else in the repo supports one of those seams or the lifecycle of a pipeline task.

Main entry points: [`src/services/llm/mod.rs`](https://github.com/allenmylath/rustvani/blob/main/src/services/llm/mod.rs) (LLM), [`src/pipeline/pipeline.rs`](https://github.com/allenmylath/rustvani/blob/main/src/pipeline/pipeline.rs) (Pipeline), [`src/transport/websocket/axum_websocket_transport.rs`](https://github.com/allenmylath/rustvani/blob/main/src/transport/websocket/axum_websocket_transport.rs) (WebSocket), [`src/vad/mod.rs`](https://github.com/allenmylath/rustvani/blob/main/src/vad/mod.rs) (VAD)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_core["Core runtime"]
  node_frames["Frames<br/>typed event model<br/>[mod.rs]"]
  node_queue["Queues<br/>frame queue<br/>[queue.rs]"]
  node_processor["Processors<br/>processor trait<br/>[processor.rs]"]
  node_pipeline["Pipeline<br/>[pipeline.rs]"]
  node_task["Task<br/>[task.rs]"]
end

subgraph group_io["I/O boundary"]
  node_transport["Transport<br/>I/O boundary<br/>[mod.rs]"]
  node_ws["WebSocket<br/>axum transport"]
  node_ravi["RAVI<br/>realtime protocol<br/>[mod.rs]"]
  node_vad["VAD<br/>speech gate<br/>[mod.rs]"]
  node_audio["Audio Proc<br/>audio cleanup<br/>[mod.rs]"]
  node_turn["Turn Engine<br/>end-of-turn logic<br/>[mod.rs]"]
end

subgraph group_ai["Inference services"]
  node_stt["STT<br/>speech-to-text<br/>[mod.rs]"]
  node_llm["LLM<br/>language model<br/>[mod.rs]"]
  node_tts["TTS<br/>text-to-speech<br/>[mod.rs]"]
end

subgraph group_flow["Conversation flow"]
  node_context["Context<br/>conversation state<br/>[mod.rs]"]
  node_agg["Aggregators<br/>stream joiners<br/>[mod.rs]"]
  node_dhara["Dhara<br/>flow engine<br/>[mod.rs]"]
end

subgraph group_tools["Tools"]
  node_registry["Registry"]
  node_postgres[("Postgres Tool<br/>managed db tool<br/>[mod.rs]")]
end

subgraph group_support["Support"]
  node_observer(("Observer<br/>runtime hooks<br/>[mod.rs]"))
  node_metrics(("Metrics<br/>instrumentation<br/>[mod.rs]"))
  node_clock(("Clock<br/>timing<br/>[mod.rs]"))
end

node_transport -->|"websocket impl"| node_ws
node_transport -->|"signals"| node_ravi
node_ws -->|"audio in"| node_vad
node_ws -->|"normalize"| node_audio
node_audio -->|"preprocess"| node_vad
node_vad -->|"turn state"| node_turn
node_vad -->|"speech frames"| node_stt
node_stt -->|"transcripts"| node_agg
node_agg -->|"update"| node_context
node_context -->|"prompt"| node_llm
node_llm -->|"tool call"| node_registry
node_registry -->|"execute"| node_postgres
node_postgres -->|"result"| node_llm
node_llm -->|"assistant stream"| node_agg
node_llm -->|"speak"| node_tts
node_tts -->|"audio out"| node_ws
node_frames -->|"contract"| node_processor
node_processor -->|"buffering"| node_queue
node_pipeline -->|"orchestrate"| node_task
node_task -->|"run"| node_processor
node_pipeline -->|"connect"| node_transport
node_dhara -->|"state"| node_context
node_dhara -->|"tools"| node_registry
node_observer -->|"observe"| node_pipeline
node_metrics -->|"measure"| node_pipeline
node_clock -->|"timing"| node_task

click node_frames "https://github.com/allenmylath/rustvani/blob/main/src/frames/mod.rs"
click node_queue "https://github.com/allenmylath/rustvani/blob/main/src/frames/queue.rs"
click node_processor "https://github.com/allenmylath/rustvani/blob/main/src/frames/processor.rs"
click node_pipeline "https://github.com/allenmylath/rustvani/blob/main/src/pipeline/pipeline.rs"
click node_task "https://github.com/allenmylath/rustvani/blob/main/src/pipeline/task.rs"
click node_transport "https://github.com/allenmylath/rustvani/blob/main/src/transport/mod.rs"
click node_ws "https://github.com/allenmylath/rustvani/blob/main/src/transport/websocket/axum_websocket_transport.rs"
click node_ravi "https://github.com/allenmylath/rustvani/blob/main/src/ravi/mod.rs"
click node_vad "https://github.com/allenmylath/rustvani/blob/main/src/vad/mod.rs"
click node_audio "https://github.com/allenmylath/rustvani/blob/main/src/audio_process/mod.rs"
click node_turn "https://github.com/allenmylath/rustvani/blob/main/src/turn/mod.rs"
click node_stt "https://github.com/allenmylath/rustvani/blob/main/src/services/stt/mod.rs"
click node_llm "https://github.com/allenmylath/rustvani/blob/main/src/services/llm/mod.rs"
click node_tts "https://github.com/allenmylath/rustvani/blob/main/src/services/tts/mod.rs"
click node_context "https://github.com/allenmylath/rustvani/blob/main/src/context/mod.rs"
click node_agg "https://github.com/allenmylath/rustvani/blob/main/src/processors/mod.rs"
click node_dhara "https://github.com/allenmylath/rustvani/blob/main/src/dhara/mod.rs"
click node_registry "https://github.com/allenmylath/rustvani/blob/main/src/services/llm/function_registry.rs"
click node_postgres "https://github.com/allenmylath/rustvani/blob/main/src/tools/postgres/mod.rs"
click node_observer "https://github.com/allenmylath/rustvani/blob/main/src/observer/mod.rs"
click node_metrics "https://github.com/allenmylath/rustvani/blob/main/src/metrics/mod.rs"
click node_clock "https://github.com/allenmylath/rustvani/blob/main/src/clock/mod.rs"

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_frames,node_queue,node_processor,node_pipeline,node_task toneBlue
class node_transport,node_ws,node_ravi,node_vad,node_audio,node_turn toneAmber
class node_stt,node_llm,node_tts toneMint
class node_context,node_agg,node_dhara toneRose
class node_registry,node_postgres toneIndigo
class node_observer,node_metrics,node_clock toneTeal
```

## Components

### Core runtime

Frame-based execution and orchestration

- [Frames](https://github.com/allenmylath/rustvani/blob/main/src/frames/mod.rs): typed event model · Core frame types, directions, and processor contracts · `src/frames/mod.rs`
- [Queues](https://github.com/allenmylath/rustvani/blob/main/src/frames/queue.rs): frame queue · Two-queue processor buffering for lifecycle and data frames · `src/frames/queue.rs`
- [Processors](https://github.com/allenmylath/rustvani/blob/main/src/frames/processor.rs): processor trait · Frame processor interface and chaining primitives · `src/frames/processor.rs`
- [Pipeline](https://github.com/allenmylath/rustvani/blob/main/src/pipeline/pipeline.rs): runtime pipeline · Pipeline assembly and execution orchestration · `src/pipeline/pipeline.rs`
- [Task](https://github.com/allenmylath/rustvani/blob/main/src/pipeline/task.rs): pipeline task · Task lifecycle, orchestration, and interruption handling · `src/pipeline/task.rs`

### I/O boundary

Transport, audio, and realtime protocol

- [Transport](https://github.com/allenmylath/rustvani/blob/main/src/transport/mod.rs): I/O boundary · Inbound and outbound transport abstractions · `src/transport/mod.rs`
- [WebSocket](https://github.com/allenmylath/rustvani/blob/main/src/transport/websocket/axum_websocket_transport.rs): axum transport · Primary websocket transport implementation · `src/transport/websocket/axum_websocket_transport.rs`
- [RAVI](https://github.com/allenmylath/rustvani/blob/main/src/ravi/mod.rs): realtime protocol · Realtime client signaling for speaking, tokens, and readiness · `src/ravi/mod.rs`
- [VAD](https://github.com/allenmylath/rustvani/blob/main/src/vad/mod.rs): speech gate · Speech detection and turn-state control · `src/vad/mod.rs`
- [Audio Proc](https://github.com/allenmylath/rustvani/blob/main/src/audio_process/mod.rs): audio cleanup · Noise suppression and resampling before inference · `src/audio_process/mod.rs`
- [Turn Engine](https://github.com/allenmylath/rustvani/blob/main/src/turn/mod.rs): end-of-turn logic · Heuristics for detecting completed user turns · `src/turn/mod.rs`

### Inference services

STT, LLM, and TTS integrations

- [STT](https://github.com/allenmylath/rustvani/blob/main/src/services/stt/mod.rs): speech-to-text · Streaming speech recognition integrations · `src/services/stt/mod.rs`
- [LLM](https://github.com/allenmylath/rustvani/blob/main/src/services/llm/mod.rs): language model · Provider integrations and model-facing logic · `src/services/llm/mod.rs`
- [TTS](https://github.com/allenmylath/rustvani/blob/main/src/services/tts/mod.rs): text-to-speech · Remote and local speech synthesis backends · `src/services/tts/mod.rs`

### Conversation flow

Context, aggregation, and higher-level routing

- [Context](https://github.com/allenmylath/rustvani/blob/main/src/context/mod.rs): conversation state · Shared messages, tools, and selection state · `src/context/mod.rs`
- [Aggregators](https://github.com/allenmylath/rustvani/blob/main/src/processors/mod.rs): stream joiners · Collect streaming fragments into conversation turns · `src/processors/mod.rs`
- [Dhara](https://github.com/allenmylath/rustvani/blob/main/src/dhara/mod.rs): flow engine · Named conversational nodes and transitions · `src/dhara/mod.rs`

### Tools

External service access and tool execution

- [Registry](https://github.com/allenmylath/rustvani/blob/main/src/services/llm/function_registry.rs): tool registry · Function registry and tool-call dispatch boundary · `src/services/llm/function_registry.rs`
- [Postgres Tool](https://github.com/allenmylath/rustvani/blob/main/src/tools/postgres/mod.rs): managed db tool · Schema caching, query execution, filtering, and vector search · `src/tools/postgres/mod.rs`

### Support

Runtime utilities and observability

- [Observer](https://github.com/allenmylath/rustvani/blob/main/src/observer/mod.rs): runtime hooks · Observation and event hooks for pipeline execution · `src/observer/mod.rs`
- [Metrics](https://github.com/allenmylath/rustvani/blob/main/src/metrics/mod.rs): instrumentation · Performance and runtime instrumentation · `src/metrics/mod.rs`
- [Clock](https://github.com/allenmylath/rustvani/blob/main/src/clock/mod.rs): timing · Timing abstraction for scheduling and latency control · `src/clock/mod.rs`

## Connections

- Transport → WebSocket: websocket impl · Primary network transport runtime
- Transport → RAVI: signals · Realtime protocol messages alongside transport
- WebSocket → VAD: audio in · Inbound audio enters the speech gate
- WebSocket → Audio Proc: normalize · Transport audio is cleaned and resampled
- Audio Proc → VAD: preprocess · Normalized audio feeds speech detection
- VAD → Turn Engine: turn state · Speech/no-speech drives end-of-turn logic
- VAD → STT: speech frames · Detected speech is forwarded to transcription
- STT → Aggregators: transcripts · Streaming transcripts are aggregated into turns
- Aggregators → Context: update · Aggregated user and assistant content updates shared context
- Context → LLM: prompt · Conversation state and tools shape model requests
- LLM → Registry: tool call · Model tool requests are dispatched through the registry
- Registry → Postgres Tool: execute · Registered tools invoke constrained external actions
- Postgres Tool → LLM: result · Tool output returns to model context
- LLM → Aggregators: assistant stream · Assistant tokens are accumulated into a turn
- LLM → TTS: speak · Generated assistant text is synthesized to audio
- TTS → WebSocket: audio out · Synthesized audio is emitted back to the client
- Frames → Processors: contract · Processors consume and emit typed frames
- Processors → Queues: buffering · Lifecycle and data frames are separated in queues
- Pipeline → Task: orchestrate · Pipeline execution is managed by pipeline tasks
- Task → Processors: run · Tasks drive the processor chain
- Pipeline → Transport: connect · Runtime connects I/O boundaries to the frame pipeline
- Dhara → Context: state · Higher-level conversational nodes share context strategy
- Dhara → Registry: tools · Flow nodes can use tool handlers and transitions
- Observer → Pipeline: observe · Execution hooks trace pipeline activity
- Metrics → Pipeline: measure · Instrumentation captures runtime behavior
- Clock → Task: timing · Timing abstraction supports task scheduling and latency
