# samarth30/ai-voice-agent architecture

> Architecture diagram of the GitHub repository samarth30/ai-voice-agent, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-18). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/samarth30/ai-voice-agent
- Repository: https://github.com/samarth30/ai-voice-agent

## Overview

##### Project type and purpose
- This is a single‐service Node.js application that acts as a real‐time “voice assistant,” connecting incoming phone calls on a Twilio voice‐capable number to OpenAI’s Realtime API via bi‐directional audio streaming. Users call a Twilio number, Twilio forwards audio to your Node.js app, which then streams it to OpenAI, receives AI responses, and sends synthesized speech back through Twilio.

##### File structure
- Top level:
- index.js – the single Express‐style HTTP/WebSocket server entrypoint
- .env.example – outlines required environment variables (Twilio creds, OpenAI API key, port)
- package.json / package-lock.json – Node.js dependencies including twilio and openai SDKs
- README, LICENSE, CODE_OF_CONDUCT, .gitignore – documentation and metadata
- No subdirectories; all logic lives in index.js. Deployment and build are trivial (just “node index.js”).

##### README for additional insights
- Describes use of Ngrok to expose the local server to Twilio’s webhooks at `/incoming-call`
- Explains two WebSocket connections: one to Twilio Media Streams and one to OpenAI Realtime API
- Lists Twilio products (Programmable Voice, Media Streams) and OpenAI Realtime API as core technologies

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% External Caller
    Caller["Caller (Telephone)"]:::external

    %% External Services
    subgraph "Twilio Programmable Voice" 
        direction TB
        TwilioVoice["Twilio Voice\n(Webhook & Media Streams)"]:::external
    end

    Ngrok["Ngrok Tunneling"]:::infra

    subgraph "Your Node.js App" 
        direction TB
        NodeApp["Node.js App\n(index.js)\n• HTTP /incoming-call\n• WS /media\n• Audio Orchestrator"]:::app
        Env["Environment Variables\n(TWILIO_ACCOUNT_SID,\nOPENAI_API_KEY, PORT)"]:::config
    end

    OpenAI["OpenAI Realtime API"]:::external

    subgraph "Project Files" 
        direction TB
        PkgJson["package.json"]:::config
        PkgLock["package-lock.json"]:::config
        EnvFile[".env.example"]:::config
        Readme["Readme.md"]:::config
        CodeConduct["CODE_OF_CONDUCT.md"]:::config
        License["LICENSE"]:::config
        Gitignore[".gitignore"]:::config
    end

    %% Data Flows
    Caller -->|"Dial Phone" | TwilioVoice
    TwilioVoice -->|"HTTP POST /incoming-call"| Ngrok
    Ngrok -->|"HTTP POST /incoming-call"| NodeApp
    NodeApp -->|"TwiML Response"| TwilioVoice

    TwilioVoice -.->|"WS /media"| Ngrok
    Ngrok -.->|"WS /media"| NodeApp

    NodeApp -.->|"WS Audio Frames"| OpenAI
    OpenAI -.->|"WS Transcription & Audio"| NodeApp

    NodeApp -.->|"WS Synthesized Audio"| Ngrok
    Ngrok -.->|"WS Media"| TwilioVoice
    TwilioVoice -->|"Audio to Caller"| Caller

    %% Click Events
    click NodeApp "https://github.com/samarth30/ai-voice-agent/blob/main/index.js"
    click EnvFile "https://github.com/samarth30/ai-voice-agent/blob/main/.env.example"
    click PkgJson "https://github.com/samarth30/ai-voice-agent/blob/main/package.json"
    click PkgLock "https://github.com/samarth30/ai-voice-agent/blob/main/package-lock.json"
    click Readme "https://github.com/samarth30/ai-voice-agent/blob/main/Readme.md"
    click CodeConduct "https://github.com/samarth30/ai-voice-agent/blob/main/CODE_OF_CONDUCT.md"
    click License "https://github.com/samarth30/ai-voice-agent/tree/main/LICENSE"
    click Gitignore "https://github.com/samarth30/ai-voice-agent/blob/main/.gitignore"

    %% Styles
    classDef external fill:#D0E8FF,stroke:#0366D6,color:#0366D6;
    classDef app fill:#E6FFEA,stroke:#22863A,color:#22863A;
    classDef infra fill:#FFF5D1,stroke:#D29922,color:#D29922;
    classDef config fill:#F5F5F5,stroke:#CCCCCC,color:#333333;
```
