# openai/openai-fm architecture

> Architecture diagram of the GitHub repository openai/openai-fm, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-06-12). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/openai/openai-fm
- Repository: https://github.com/openai/openai-fm

## Overview

##### Project type and purpose
- This is a full-stack demo application built with Next.js (v13 App Router).
- It showcases OpenAI’s text-to-speech API in a web UI, with optional sharing via a Postgres database.

##### File structure
- Top-level config: next.config.ts, tsconfig.json, eslint.config.mjs, tailwind.config.mjs
- public/: static assets (audio files, worker script, screenshot)
- src/app/: Next.js App Router entrypoints
- src/app/page.tsx & layout.tsx: server-rendered UI shell and landing page
- src/app/api/generate/route.ts: serverless API endpoint that calls OpenAI TTS
- src/app/api/share/route.ts: serverless API endpoint that stores/fetches shareable clips (Postgres)
- src/app/tmp.middleware.ts: custom middleware (e.g., feature flags, request throttling)
- src/components/: React client & server components for UI (buttons, dialogs, pages)
- src/hooks/: custom React hooks (audio playback, service worker, clipboard)
- src/lib/: shared utilities (type definitions, client-side store, copy/text helpers)

##### README for additional insights
- Describes use of Next.js and OpenAI Speech API
- Notes optional Postgres database for the share feature

a. Main components:
- Client Browser (React components + Web Worker)
- Next.js Server / Edge Functions (App Router + API routes)
- External Service: OpenAI Text-to-Speech API
- (Optional) Postgres Database for share feature
- CDN / Static Assets Server for public/

b. Relationships & interactions:
1. User interacts with UI in Browser
2. UI triggers client hook or fetch to /api/generate → Next.js serverless function
3. Server-side route handler calls OpenAI Speech API → returns audio (or URL)
4. Response streamed back to Browser (hook plays audio or worker caches it)
5. If user “shares,” UI calls /api/share → serverless function reads/writes Postgres → returns share link
6. Browser may retrieve share data from Postgres via share route

c. Architectural patterns & design principles:
- Serverless micro-endpoints: each Next.js API route is a discrete function
- React Server Components (layout, page) for SSR and streaming UI
- Client Components (buttons, dialogs, hooks) for interactivity
- Separation of concerns: UI, API, external service, and data store clearly demarcated
- Progressive enhancement: service worker for offline / caching, audio playback hook

d. Key technologies / frameworks:
- Next.js 13 (App Router)
- React 18 (Server & Client Components)
- OpenAI Node HTTP client (server-side API calls)
- PostgreSQL (optional, via environment POSTGRES_URL)
- Tailwind CSS for styling + CSS Modules
- web worker (public/worker-*.js) for background TTS processing

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Layer: Browser (Client)
    subgraph "Browser (Client)"
        direction TB
        UI["Client: TTSPage Component"]:::frontend
        DYN["Client: ClientDynamicTTS Component"]:::frontend
        PLAY["PlayButton"]:::frontend
        DL["DownloadButton"]:::frontend
        SHAREBTN["ShareButton"]:::frontend
        SHAREDLG["ShareDialog"]:::frontend
        COPYBTN["CodeCopyButton"]:::frontend
        SWHOOK["Hook: useServiceWorker"]:::hook
        AUDIOHOOK["Hook: useAudioClip"]:::hook
        SCROLLHOOK["Hook: useBodyScrollable"]:::hook
        COPYDELAY["Hook: useCopiedDelay"]:::hook
        UI -->|loads| SWHOOK
        SWHOOK -->|registers| WORKER["Web Worker"]:::asset
        UI -->|plays audio via| AUDIOHOOK
        UI -->|toggles scroll| SCROLLHOOK
        UI -->|copy code| COPYBTN
        COPYBTN -->|uses| COPYDELAY
        UI --> DYN
        DYN --> PLAY
        DYN --> DL
        DYN --> SHAREBTN
        SHAREBTN --> SHAREDLG
    end

    %% Layer: Next.js Server
    subgraph "Next.js Server (App + API)" 
        direction TB
        LAYOUT["Server Component: layout.tsx"]:::server
        PAGE["Server Component: page.tsx"]:::server
        MIDDLE["Middleware: tmp.middleware.ts"]:::middleware
        GEN["API: /api/generate"]:::api
        SHARE["API: /api/share"]:::api
        LAYOUT --> PAGE
        PAGE -->|fetch| GEN
        PAGE -->|fetch| SHARE
        GEN -->|calls| OPENAI
        SHARE -->|reads/writes| DB
    end

    %% Layer: Shared Utilities & Config
    subgraph "Utilities & Config"
        direction TB
        TYPES["lib/types.ts"]:::lib
        STORE["lib/store.ts"]:::lib
        SNIPPET["lib/codeSnippet.ts"]:::lib
        COPYTXT["lib/copyText.ts"]:::lib
        LIBHELP["lib/library.ts"]:::lib
        CONFIG["Config Files"]:::config
    end

    %% Layer: External Services
    subgraph "External Resources"
        direction TB
        OPENAI["OpenAI Speech API"]:::external
        DB["Postgres Database"]:::db
        ASSETS["Static Assets (public/)"]:::asset
    end

    %% Data Flows
    UI -->|POST /api/generate| GEN
    GEN -->|"Audio Stream"| UI
    UI -->|POST /api/share| SHARE
    SHARE -->|"Share Link"| UI

    %% Click Events
    click LAYOUT "https://github.com/openai/openai-fm/blob/main/src/app/layout.tsx"
    click PAGE "https://github.com/openai/openai-fm/blob/main/src/app/page.tsx"
    click UI "https://github.com/openai/openai-fm/blob/main/src/components/TTSPage.tsx"
    click DYN "https://github.com/openai/openai-fm/blob/main/src/components/ClientDynamicTTS.tsx"
    click PLAY "https://github.com/openai/openai-fm/blob/main/src/components/PlayButton.tsx"
    click DL "https://github.com/openai/openai-fm/blob/main/src/components/DownloadButton.tsx"
    click SHAREBTN "https://github.com/openai/openai-fm/blob/main/src/components/ShareButton.tsx"
    click SHAREDLG "https://github.com/openai/openai-fm/blob/main/src/components/ShareDialog.tsx"
    click COPYBTN "https://github.com/openai/openai-fm/blob/main/src/components/CodeCopyButton.tsx"
    click SWHOOK "https://github.com/openai/openai-fm/blob/main/src/hooks/useServiceWorker.ts"
    click AUDIOHOOK "https://github.com/openai/openai-fm/blob/main/src/hooks/useAudioClip.tsx"
    click SCROLLHOOK "https://github.com/openai/openai-fm/blob/main/src/hooks/useBodyScrollable.tsx"
    click COPYDELAY "https://github.com/openai/openai-fm/blob/main/src/hooks/useCopiedDelay.tsx"
    click GEN "https://github.com/openai/openai-fm/blob/main/src/app/api/generate/route.ts"
    click SHARE "https://github.com/openai/openai-fm/blob/main/src/app/api/share/route.ts"
    click MIDDLE "https://github.com/openai/openai-fm/blob/main/src/app/tmp.middleware.ts"
    click TYPES "https://github.com/openai/openai-fm/blob/main/src/lib/types.ts"
    click STORE "https://github.com/openai/openai-fm/blob/main/src/lib/store.ts"
    click SNIPPET "https://github.com/openai/openai-fm/blob/main/src/lib/codeSnippet.ts"
    click COPYTXT "https://github.com/openai/openai-fm/blob/main/src/lib/copyText.ts"
    click LIBHELP "https://github.com/openai/openai-fm/blob/main/src/lib/library.ts"
    click WORKER "https://github.com/openai/openai-fm/blob/main/public/worker-444eae9e2e1bdd6edd8969f319655e70.js"

    %% Styles
    classDef frontend fill:#cce5ff,stroke:#3399ff,color:#000
    classDef server fill:#e6f7ff,stroke:#66b2ff,color:#000
    classDef api fill:#d4edda,stroke:#28a745,color:#000
    classDef middleware fill:#fdfd96,stroke:#ffd700,color:#000
    classDef hook fill:#fff3cd,stroke:#ffc107,color:#000
    classDef lib fill:#f0f0f0,stroke:#999,color:#000
    classDef config fill:#f5e6ff,stroke:#b266ff,color:#000
    classDef external fill:#ffe5cc,stroke:#ff8000,color:#000
    classDef db fill:#e0e0e0,stroke:#666,color:#000
    classDef asset fill:#d6d6d6,stroke:#888,color:#000
```
