Hermes Agent commited on
Commit
4f1b59a
Β·
1 Parent(s): 55be8db

docs: add comprehensive system architecture, deployment guide, and OmniRoute feature specification

Browse files
Files changed (3) hide show
  1. ARCHITECTURE.md +109 -118
  2. DEPLOYMENT_GUIDE.md +122 -0
  3. OMNIROUTE_INTEGRATION_PLAN.md +93 -0
ARCHITECTURE.md CHANGED
@@ -1,143 +1,134 @@
1
- # Architecture Specification: Opencode-Cli
2
 
3
- This document details the software architecture, network routing, multi-service orchestration, and data persistence models for **Opencode-Cli** deployed on [Hugging Face Spaces (`Jishnupg/Opencode-Cli`)](https://huggingface.co/spaces/Jishnupg/Opencode-Cli).
4
 
5
  ---
6
 
7
- ## 1. System Overview
8
 
9
- **Opencode-Cli** is an enterprise-grade multi-service web container running on Debian Bookworm Slim. It orchestrates six integrated services into a unified web application exposed via a single public ingress port (`4096`):
10
 
11
- 1. **FastAPI Reverse Proxy & Gateway Router**: Ingress path-routing, CORS, WebSocket proxying, and service isolation.
12
- 2. **Open WebUI**: Primary ChatGPT-style frontend pre-configured to use OmniRoute's dedicated API server (`127.0.0.1:20129/v1`).
13
- 3. **OmniRoute v3.8.50 Multi-Port AI Gateway**:
14
- - **Dashboard**: Port `20128` (Web UI & settings at `/omniroute`)
15
- - **Dedicated API Server**: Port `20129` (OpenAI `/v1` & Gemini `/v1beta` compatibility)
16
- - **Live WebSocket Server**: Port `20132` (Real-time monitoring)
17
- 4. **Redis Cache & Rate Limiter**: Port `6379` (In-memory cache and distributed rate limiting for OmniRoute).
18
- 5. **Jellyfin Media Server**: Port `8096` (Media streaming server at `/jellyfin`).
19
- 6. **Telegram 5G Stream Proxy**: Port `8080` (High-speed MTProto streaming daemon at `/tg-stream`).
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
 
21
  ---
22
 
23
- ## 2. Production Architecture Diagram
24
-
25
- ```mermaid
26
- flowchart TD
27
- User([🌐 Public Web Client]) -->|HTTPS Port 443| Ingress[FastAPI Gateway Proxy - Port 4096]
28
-
29
- subgraph Container ["🐳 Docker Container (Debian Bookworm Slim)"]
30
- Ingress -->|Path: / , /api/config, /ws/socket.io | OWUI[Open WebUI - Port 8098]
31
- Ingress -->|Path: /dashboard/* , /login, /callback | OmniDash[OmniRoute Dashboard - Port 20128]
32
- Ingress -->|Path: /v1/* , /v1beta/* | OmniAPI[OmniRoute Dedicated API - Port 20129]
33
- Ingress -->|Path: /live-ws/* | OmniWS[OmniRoute Live WS - Port 20132]
34
- Ingress -->|Path: /jellyfin/* | JF[Jellyfin Media Server - Port 8096]
35
- Ingress -->|Path: /tg-stream/* | TG[Telegram 5G Streamer - Port 8080]
36
-
37
- OWUI -->|Internal OpenAI API /v1| OmniAPI
38
- OmniDash -->|Distributed Cache & Limits| Redis[(Redis Server - Port 6379)]
39
- OmniAPI -->|Distributed Cache & Limits| Redis
40
- TG -->|Auto-Generate .strm Files| JF
41
- end
42
-
43
- subgraph Persistence ["πŸ’Ύ Persistence & Storage Model"]
44
- Ext4["Local Container Ext4 (/root/.omniroute/storage.sqlite)"] <-->|SQLite .backup API (Every 120s)| HFData["HF FUSE Persistent Mount (/data/omniroute/storage.sqlite)"]
45
- OmniDash -->|Fast POSIX SQLite Locks| Ext4
46
- OWUI -->|User Accounts & DB| HFData
47
- JF -->|Media & Metadata| HFData
48
- end
49
  ```
50
 
 
 
 
 
 
51
  ---
52
 
53
- ## 3. Microservice Specifications
54
-
55
- ### 3.1 FastAPI Ingress Gateway (`proxy.py`, `gateway/`)
56
- - **Port**: `4096` (Exposed as container entrypoint)
57
- - **Role**:
58
- - Single-port ingress on Hugging Face Spaces.
59
- - Option A route classification with exact prefix boundaries.
60
- - Proxies HTTP requests and bidirectional WebSockets (`/ws/socket.io` & `/live-ws`).
61
-
62
- ### 3.2 Open WebUI (`open-webui`)
63
- - **Port**: `8098`
64
- - **Path**: `/` (Root Native Application)
65
- - **Environment**:
66
- - `OPENAI_API_BASE_URL="http://127.0.0.1:20129/v1"`
67
- - `ENABLE_OPENAI_API="true"`
68
- - `RAG_AUTO_UPDATE_INDEX="false"`
69
- - **Optimization**: SentenceTransformer embeddings (`all-MiniLM-L6-v2`) pre-cached in Docker image.
70
-
71
- ### 3.3 OmniRoute AI Gateway v3.8.50
72
- - **Ports**:
73
- - **Dashboard**: `20128` (Path `/dashboard`, `/login`, `/callback`, `/api/providers`)
74
- - **Dedicated API**: `20129` (Endpoints `/v1`, `/v1beta`)
75
- - **Live WebSocket**: `20132` (Path `/live-ws`)
76
- - **Environment**:
77
- - `NEXT_PUBLIC_BASE_URL="https://jishnupg-opencode-cli.hf.space"`
78
- - `AUTH_COOKIE_SECURE="true"`
79
- - `REDIS_URL="redis://127.0.0.1:6379"`
80
- - `DATA_DIR="/root/.omniroute"`
81
- - **Auto-Fix**: `fix_omniroute.py` auto-resolves migration version collisions at build time and container boot.
82
-
83
- ### 3.4 Redis Server (`redis-server`)
84
- - **Port**: `6379`
85
- - **Role**: In-memory rate limiting and distributed caching for OmniRoute.
86
-
87
- ### 3.5 Jellyfin Media Server (`jellyfin`)
88
- - **Port**: `8096`
89
- - **Path**: `/jellyfin`
90
- - **Engine**: Jellyfin 10.11 + FFmpeg 5.1.
91
-
92
- ### 3.6 Telegram Direct Stream Proxy (`tg_streamer.py`)
93
- - **Port**: `8080`
94
- - **Path**: `/tg-stream`
95
- - **Engine**: Python Pyrogram MTProto Client.
96
 
97
- ---
 
 
 
 
 
 
 
 
 
98
 
99
- ## 4. Port & Path Routing Matrix
100
-
101
- | Public Request Path | Target Upstream Port | Target Service | Function |
102
- | :--- | :--- | :--- | :--- |
103
- | `/` | `8098` | Open WebUI | Main Root Web UI |
104
- | `/dashboard/*` | `20128` | OmniRoute Dashboard | Management Panel |
105
- | `/v1/*` | `20129` | OmniRoute API Server | Dedicated OpenAI API |
106
- | `/v1beta/*` | `20129` | OmniRoute API Server | Dedicated Gemini API |
107
- | `/api/providers/*` | `20128` | OmniRoute Dashboard | Provider API |
108
- | `/api/oauth/*` | `20128` | OmniRoute Dashboard | OAuth API |
109
- | `/callback` | `20128` | OmniRoute Dashboard | OAuth Callback Handler |
110
- | `/live-ws/*` | `20132` | OmniRoute WebSocket | Live Monitoring WS |
111
- | `/jellyfin/*` | `8096` | Jellyfin Media Server | Media Streaming |
112
- | `/tg-stream/*` | `8080` | TG Stream Proxy | Telegram Direct Streamer |
113
 
114
  ---
115
 
116
- ## 5. Storage & Persistence Model
117
 
118
- - **Active Execution (`/root/.omniroute/storage.sqlite`)**: Runs on local Ext4 container disk to support `better-sqlite3` POSIX file locking.
119
- - **Safe SQLite Backup (`/data/omniroute/storage.sqlite`)**: Background daemon uses `sqlite3 .backup` API every 5 minutes and on shutdown to write consistent snapshots to the HF FUSE mount.
 
 
 
 
 
120
 
121
  ---
122
 
123
- ## 6. Repository File Structure
 
 
124
 
125
  ```
126
- .
127
- β”œβ”€β”€ Dockerfile # Multi-stage Docker image with Redis & pre-cached models
128
- β”œβ”€β”€ ARCHITECTURE.md # Architecture specification (this document)
129
- β”œβ”€β”€ README.md # User quickstart guide
130
- β”œβ”€β”€ entrypoint.sh # Concurrent service startup, Redis daemon & SQLite backup
131
- β”œβ”€β”€ fix_omniroute.py # Migration version collision auto-repair script
132
- β”œβ”€β”€ nginx.conf # Nginx configuration reference
133
- β”œβ”€β”€ proxy.py # FastAPI gateway entrypoint script
134
- β”œβ”€β”€ tg_streamer.py # Pyrogram Telegram 5G stream proxy
135
- └── gateway/ # FastAPI Modular Router Package
136
- β”œβ”€β”€ __init__.py
137
- β”œβ”€β”€ main.py # App router aggregator
138
- β”œβ”€β”€ utils.py # Async HTTP & WebSocket proxy engine
139
- β”œβ”€β”€ openwebui.py # Open WebUI path routing
140
- β”œβ”€β”€ omniroute.py # OmniRoute API & asset routing
141
- β”œβ”€β”€ jellyfin.py # Jellyfin path routing
142
- └── tg_stream.py # Telegram proxy routing
143
  ```
 
 
 
 
1
+ # OpenCode Space β€” Production System Architecture & Design Specification
2
 
3
+ This document provides a comprehensive technical overview of the **`Jishnupg/Opencode-Cli`** multi-service gateway architecture, service discovery, database persistence model, security boundaries, and protocol proxies.
4
 
5
  ---
6
 
7
+ ## 1. System Ingress Topology & Port Matrix
8
 
9
+ All client traffic terminates at **Port 4096** (the exposed public port on Hugging Face Spaces). The system operates a dual-layer reverse proxy architecture to guarantee sub-second platform readiness and unified routing.
10
 
11
+ ```
12
+ [🌐 Public Internet Client]
13
+ β”‚
14
+ [Nginx Edge Proxy (Port 4096)]
15
+ β”‚
16
+ [FastAPI Master Gateway (:8000)]
17
+ β”‚
18
+ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
19
+ β”‚ β”‚ β”‚ β”‚ β”‚
20
+ [Open WebUI] [OmniRoute Dashboard] [OmniRoute Dedicated API] [Jellyfin Media] [TG 5G Streamer]
21
+ (Port 8098) (Port 20128) (Port 20129) (Port 8096) (Port 8080)
22
+ Path: / Path: /dashboard Path: /v1, /v1beta Path: /jellyfin Path: /tg-stream
23
+ ```
24
+
25
+ ### Complete Ingress Routing Matrix
26
+
27
+ | Ingress Path / Pattern | Upstream Host:Port | Microservice | Protocol / Handler | Header Sanitization |
28
+ | :--- | :--- | :--- | :--- | :--- |
29
+ | `GET /health/live` | Internal Gateway | FastAPI Gateway | Native ASGI JSON | Instant 200 OK (<1ms) |
30
+ | `/` (Default Fallback) | `127.0.0.1:8098` | Open WebUI | HTTP / Socket.IO WS | HTML base rewrite (`/`) |
31
+ | `/omniroute`, `/omniroute/`| `127.0.0.1:8000` | FastAPI Gateway | HTTP 307 Redirect | Redirects to `/dashboard` |
32
+ | `/dashboard/*` | `127.0.0.1:20128` | OmniRoute Dashboard | HTTP / Next.js SSR | Strip X-Frame-Options |
33
+ | `/v1/*`, `/api/v1/*` | `127.0.0.1:20129` | Dedicated OpenAI API | OpenAI SSE Stream | CORS `*`, `Host` forward |
34
+ | `/v1beta/*`, `/api/v1beta/*`| `127.0.0.1:20129` | Dedicated Gemini API | Gemini JSON / Stream | CORS `*`, `Host` forward |
35
+ | `/api/providers/*` | `127.0.0.1:20128` | OmniRoute Management | REST JSON API | Encrypted DB read |
36
+ | `/api/custom-models/*` | `127.0.0.1:20128` | OmniRoute Management | REST JSON API | Referer-based proxy |
37
+ | `/api/connections/*` | `127.0.0.1:20128` | OmniRoute Management | REST JSON API | Referer-based proxy |
38
+ | `/api/oauth/*` | `127.0.0.1:20128` | OmniRoute OAuth | Remote OAuth Flow | `ALLOW_REMOTE_OAUTH` |
39
+ | `/live-ws/*` | `127.0.0.1:20132` | Live Telemetry WS | WebSocket Stream | `Host`, `X-Forwarded-*` |
40
+ | `/jellyfin/*` | `127.0.0.1:8096` | Jellyfin Media Server | Range Video / HTTP | `X-Forwarded-Prefix` |
41
+ | `/tg-stream/*`, `/tg_stream/*`| `127.0.0.1:8080` | Pyrogram 5G Streamer | 5G Chunk Streamer | Direct Pyrogram Stream |
42
 
43
  ---
44
 
45
+ ## 2. Gateway Core Engine Architecture (`gateway/`)
46
+
47
+ The master gateway (`gateway/main.py`, `gateway/utils.py`, `gateway/omniroute.py`) provides intelligent request classification and routing.
48
+
49
+ ```
50
+ [Incoming HTTP Request]
51
+ β”‚
52
+ [Security Path Traversal Check]
53
+ β”‚
54
+ [Lightweight Probe /health/live?] ──(Yes)──> 200 OK
55
+ β”‚ (No)
56
+ [Referer Header Inspection]
57
+ / \
58
+ (Referer has /dashboard) (No OmniRoute Referer)
59
+ β”‚ β”‚
60
+ [Route to OmniRoute :20128] [Match Path Prefix Matrix]
61
+ / β”‚ \
62
+ (OmniRoute) (Jellyfin) (TG Stream)
63
+ β”‚ β”‚ β”‚
64
+ [:20128] [:8096] [:8080]
65
+ β”‚
66
+ [Default Fallback]
67
+ β”‚
68
+ [Open WebUI :8098]
 
 
69
  ```
70
 
71
+ ### Key Gateway Design Patterns:
72
+ 1. **Async Connection Pooling (`httpx.AsyncClient`)**: Managed via FastAPI `@asynccontextmanager` lifespan. Connection keep-alive prevents TCP handshake overhead across internal microservice proxies.
73
+ 2. **Referer-Aware Routing**: Requests originating from OmniRoute UI pages (`Referer` containing `/dashboard` or `/omniroute`) are routed directly to OmniRoute port `20128`. This prevents auxiliary requests (`/api/custom-models`, `/api/connections`, `/providers/*.svg`) from falling through to Open WebUI.
74
+ 3. **CORS & iFrame Security Normalization**: Automatically strips restrictive `X-Frame-Options` headers and injects `Content-Security-Policy: frame-ancestors 'self' https://huggingface.co https://*.hf.space;` to enable embedding on Hugging Face Spaces.
75
+
76
  ---
77
 
78
+ ## 3. Storage Persistence & SQLite Synchronization Model
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79
 
80
+ OmniRoute uses `better-sqlite3`. To bypass FUSE network drive lock limitations while maintaining persistence, a dual-layer storage architecture is implemented:
81
+
82
+ ```
83
+ β”Œβ”€β”€οΏ½οΏ½β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” 120s Cron Daemon / β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
84
+ β”‚ High-Speed Ext4 Container Disk β”‚ Shutdown Backup Loop β”‚ Persistent Volume Mount β”‚
85
+ β”‚ (/root/.omniroute/storage.sqlite) β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€>β”‚ (/data/omniroute/storage.sqlite) β”‚
86
+ β”‚ - Active runtime POSIX file locks (fcntl) β”‚ β”‚ - Persistent across Space rebuilds & sleep β”‚
87
+ β”‚ - Active WAL (-wal) & SHM (-shm) sidecars β”‚<───────────────────────────── - Rotating backups (/data/omniroute/backups) β”‚
88
+ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ Boot Restoration Sync β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
89
+ ```
90
 
91
+ ### Database Lifecycle & Safety Guarantees:
92
+ 1. **Boot Initialization**:
93
+ - `entrypoint.sh` checks for persistent snapshot at `/data/omniroute/storage.sqlite`.
94
+ - Validates integrity using `sqlite3 "PRAGMA quick_check;"`.
95
+ - Restores clean snapshot into `/root/.omniroute/storage.sqlite`.
96
+ 2. **Runtime Backup Loop**:
97
+ - Every 120 seconds, a background daemon executes `sync_omniroute_db()`.
98
+ - Issues `PRAGMA wal_checkpoint(PASSIVE);` and `sqlite3 .backup`.
99
+ - Syncs `-wal` and `-shm` sidecars to preserve uncommitted transactions.
100
+ - Rotates up to 5 rolling backups in `/data/omniroute/backups/storage-YYYYMMDD-HHMM.sqlite`.
101
+ 3. **Shutdown Hook**:
102
+ - Shell traps (`trap sync_omniroute_db EXIT INT TERM`) flush runtime state before container termination.
 
 
103
 
104
  ---
105
 
106
+ ## 4. Encryption & Security Model
107
 
108
+ - **AES-256-GCM Encryption**: All provider credentials (API keys, OAuth access tokens, refresh tokens) are encrypted at rest with `STORAGE_ENCRYPTION_KEY`.
109
+ - **Secrets Fallback Engine**: If secrets are not passed via Hugging Face Space Secrets during local development, safe default fallbacks prevent container crash:
110
+ - `STORAGE_ENCRYPTION_KEY`
111
+ - `JWT_SECRET`
112
+ - `API_KEY_SECRET`
113
+ - `INITIAL_PASSWORD`
114
+ - **Path Traversal Gate**: The gateway inspects all requested paths and rejects directory traversal patterns (`..`, `.env`, `.sqlite`, `.git`) with HTTP 403.
115
 
116
  ---
117
 
118
+ ## 5. Process Lifecycle & Self-Healing Supervisor
119
+
120
+ The container supervisor (`entrypoint.sh`) maintains 24/7 uptime for all background daemons:
121
 
122
  ```
123
+ [PID 1 Process Supervisor Loop]
124
+ β”‚ (every 5s)
125
+ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
126
+ β–Ό β–Ό β–Ό
127
+ [Kill -0 $FASTAPI_PID] [Kill -0 $NGINX_PID] [Kill -0 $OMNIROUTE_PID]
128
+ β”‚ β”‚ β”‚
129
+ (Dead? Restart) (Dead? Restart) (Dead? Restart)
130
+ python3 -m uvicorn proxy:app nginx -g 'daemon off;' cd /omniroute && node server.js
 
 
 
 
 
 
 
 
 
131
  ```
132
+
133
+ - **Fast Gateway Readiness**: FastAPI (`:8000`) and Nginx (`:4096`) start within **300ms**, allowing Hugging Face to report `RUNNING` status instantly.
134
+ - **Asynchronous Heavy Boot**: OmniRoute Next.js server, Open WebUI, Jellyfin, and TG Streamer initialize in parallel background tasks without blocking public ingress.
DEPLOYMENT_GUIDE.md ADDED
@@ -0,0 +1,122 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # OpenCode Space β€” Production Deployment & Operations Manual
2
+
3
+ This guide provides step-by-step instructions for deploying, updating, scaling, and maintaining the **`Jishnupg/Opencode-Cli`** multi-service environment on Hugging Face Spaces or standard Docker / Kubernetes environments.
4
+
5
+ ---
6
+
7
+ ## 1. Prerequisites & Repository Remotes Setup
8
+
9
+ ### Primary Repository Endpoints:
10
+ - **GitHub Source Repo**: `https://github.com/JishnuPG-tech/Project.git`
11
+ - **Hugging Face Space**: `https://huggingface.co/spaces/Jishnupg/Opencode-Cli`
12
+
13
+ ### Git Remote Configuration:
14
+ To ensure updates are pushed cleanly to both GitHub and Hugging Face Space:
15
+
16
+ ```bash
17
+ # Add Hugging Face Space remote
18
+ git remote add hf https://huggingface.co/spaces/Jishnupg/Opencode-Cli
19
+
20
+ # Verify remotes list
21
+ git remote -v
22
+ # Output should show:
23
+ # hf https://huggingface.co/spaces/Jishnupg/Opencode-Cli (fetch)
24
+ # hf https://huggingface.co/spaces/Jishnupg/Opencode-Cli (push)
25
+ # origin https://github.com/JishnuPG-tech/Project.git (fetch)
26
+ # origin https://github.com/JishnuPG-tech/Project.git (push)
27
+ ```
28
+
29
+ ---
30
+
31
+ ## 2. Hugging Face Space Configuration & Secrets
32
+
33
+ In your Hugging Face Space settings (**Settings > Secrets**), configure the following environment secrets:
34
+
35
+ | Secret Key | Description | Example / Recommended Value |
36
+ | :--- | :--- | :--- |
37
+ | `STORAGE_ENCRYPTION_KEY` | Master AES-256-GCM key for encrypting provider credentials | 32-character random string |
38
+ | `JWT_SECRET` | Secret key for JWT token signing across services | 32-character random string |
39
+ | `API_KEY_SECRET` | Secret key for OmniRoute API key authorization | 32-character random string |
40
+ | `INITIAL_PASSWORD` | Default admin password for OmniRoute UI login | User-defined password |
41
+
42
+ > [!NOTE]
43
+ > If any secret is omitted during local Docker testing, `entrypoint.sh` automatically supplies safe default fallback values to prevent container crashes.
44
+
45
+ ---
46
+
47
+ ## 3. Build & Deployment Commands
48
+
49
+ ### Standard Deployment Workflow:
50
+
51
+ ```bash
52
+ # 1. Stage all modified code changes
53
+ git add .
54
+
55
+ # 2. Commit with conventional commit message
56
+ git commit -m "feat(omniroute): update gateway routing and provider persistence"
57
+
58
+ # 3. Push to GitHub repository
59
+ git push origin main
60
+
61
+ # 4. Push to Hugging Face Space repository (triggers automatic Docker build)
62
+ git push hf main
63
+ ```
64
+
65
+ ---
66
+
67
+ ## 4. Local Testing with Docker
68
+
69
+ To build and run the entire multi-service container locally:
70
+
71
+ ```bash
72
+ # Build the Docker image
73
+ docker build -t opencode-cli:latest .
74
+
75
+ # Run the container mapping public ingress port 4096
76
+ docker run -d \
77
+ -p 4096:4096 \
78
+ -e STORAGE_ENCRYPTION_KEY="my_secure_encryption_key_32bytes" \
79
+ -e JWT_SECRET="my_secure_jwt_secret_key_32bytes" \
80
+ -e API_KEY_SECRET="my_secure_api_key_secret_32bytes" \
81
+ -e INITIAL_PASSWORD="admin_password_123" \
82
+ --name opencode-space \
83
+ opencode-cli:latest
84
+
85
+ # Check container logs
86
+ docker logs -f opencode-space
87
+ ```
88
+
89
+ ### Access Local Endpoints:
90
+ - **Unified Gateway**: `http://localhost:4096/`
91
+ - **OmniRoute Dashboard**: `http://localhost:4096/dashboard`
92
+ - **OpenAI API Base URL**: `http://localhost:4096/v1`
93
+ - **Jellyfin Media**: `http://localhost:4096/jellyfin`
94
+ - **TG Streamer**: `http://localhost:4096/tg-stream`
95
+
96
+ ---
97
+
98
+ ## 5. Maintenance & Troubleshooting
99
+
100
+ ### Issue 1: `SyntaxError: Unexpected token '<'` in Frontend
101
+ - **Symptom**: Dashboard page shows "0 connections" or console logs `<JSON.parse error>`.
102
+ - **Cause**: Gateway routed an `/api/` request to Open WebUI instead of OmniRoute.
103
+ - **Fix**: Verify `gateway/main.py` has Referer-based routing enabled:
104
+ ```python
105
+ is_omniroute_referer = "/dashboard" in referer or "/omniroute" in referer or "/providers" in referer
106
+ ```
107
+
108
+ ### Issue 2: Duplicate Process Restarts (`FastAPI Gateway process died!`)
109
+ - **Symptom**: Container logs show repeated restart messages every 5s.
110
+ - **Cause**: Variable name mismatch between start command (`FASTAPI_PID=$!`) and supervisor check (`kill -0 $FASTAPI_PID`).
111
+ - **Fix**: Verify `FASTAPI_PID` is correctly set and checked in `entrypoint.sh`.
112
+
113
+ ### Issue 3: Database Snapshot Restoration Check
114
+ - **Symptom**: Provider keys disappear after space rebuild.
115
+ - **Verification Command**:
116
+ ```bash
117
+ # Check if persistent volume snapshot exists
118
+ ls -lh /data/omniroute/storage.sqlite
119
+
120
+ # Manually trigger snapshot sync
121
+ python3 -c "import sqlite3; conn = sqlite3.connect('/data/omniroute/storage.sqlite'); print(conn.execute('PRAGMA quick_check;').fetchall())"
122
+ ```
OMNIROUTE_INTEGRATION_PLAN.md ADDED
@@ -0,0 +1,93 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # OmniRoute AI Gateway β€” Feature Specification & Integration Roadmap
2
+
3
+ This document outlines the detailed feature architecture of **OmniRoute** integrated within the `Jishnupg/Opencode-Cli` project, detailing the token compression pipeline, 290+ AI providers, CLI compatibility modes, and API endpoints.
4
+
5
+ ---
6
+
7
+ ## 1. Core Engine Subsystems
8
+
9
+ ### A. Dual Token Compression Pipeline (RTK + Caveman)
10
+ OmniRoute features an inline token optimization engine that reduces prompt length by **15% to 95%** prior to forwarding requests to upstream LLM providers:
11
+
12
+ ```
13
+ [Client Prompt Payload]
14
+ β”‚
15
+ [RTK (Recursive Token Killer)] ──> Strips system prompt boilerplate, repeated context, & tool output noise
16
+ β”‚
17
+ [Caveman Compression Engine] ──> Compresses JSON schemas & system instructions into dense structural format
18
+ β”‚
19
+ [Compressed API Request] ──> Sent to Target Provider (e.g. Anthropic, OpenAI, DeepSeek, Kimi)
20
+ ```
21
+
22
+ ### B. Quota-Share Auto-Fallback Cascade
23
+ OmniRoute maintains real-time rate limit and quota tracking across 290+ providers and 500+ models.
24
+
25
+ ```
26
+ [Client LLM Request]
27
+ β”‚
28
+ [Provider Choice: Primary]
29
+ β”‚
30
+ (Rate Limit 429 / 402?)
31
+ / \
32
+ (No: 200 OK) (Yes: 429 Error)
33
+ β”‚ β”‚
34
+ [Return Stream Response] [Auto-Fallback Cascade]
35
+ β”‚
36
+ [Select Next Key / Provider]
37
+ β”‚
38
+ [Execute Request Seamlessly]
39
+ ```
40
+
41
+ ---
42
+
43
+ ## 2. Supported AI Providers (290+ Integrations)
44
+
45
+ OmniRoute supports both direct API keys and OAuth single sign-on across top AI providers:
46
+
47
+ ### Major Supported OAuth & API Providers:
48
+ - **Anthropic / Claude Code**: Claude 3.7 Sonnet, Claude 3.5 Haiku, Opus.
49
+ - **OpenAI**: GPT-4o, GPT-4o-mini, o1, o3-mini.
50
+ - **Google Antigravity & Gemini**: Gemini 2.5 Pro, Gemini 2.0 Flash, Flash-Lite.
51
+ - **Moonshot / Kimi Coding**: Kimi K1.5, Kimi CLI.
52
+ - **DeepSeek**: DeepSeek R1, DeepSeek V3.
53
+ - **Amazon Q / Bedrock**: Claude 3.5 Sonnet Bedrock endpoints.
54
+ - **GitHub Copilot / Copilot Enterprise**: Copilot Chat & Editor completions.
55
+ - **Cline / ClinePass**: Open-source coding agent integrations.
56
+ - **Cerebras & Groq**: Ultra-low latency Llama-3 70B inference.
57
+ - **Mistral, Cohere, Together, Replicate, Fireworks, OpenRouter**.
58
+
59
+ ---
60
+
61
+ ## 3. CLI Compatibility Layer (`CLI_COMPAT_*`)
62
+
63
+ To allow terminal coding assistants and IDE extensions to connect directly to OmniRoute without changing client code, OmniRoute emulates provider-specific CLI endpoints when environment flags are active:
64
+
65
+ | Compatibility Flag | Emulated Client Tool | Endpoint Behavior |
66
+ | :--- | :--- | :--- |
67
+ | `CLI_COMPAT_CLAUDE=1` | Anthropic Claude Code CLI | Translates `/v1/messages` requests |
68
+ | `CLI_COMPAT_ANTIGRAVITY=1` | Google Antigravity / Gemini CLI | Emulates Antigravity authentication & streaming |
69
+ | `CLI_COMPAT_CURSOR=1` | Cursor IDE | Translates Cursor autocomplete & chat headers |
70
+ | `CLI_COMPAT_GITHUB=1` | GitHub Copilot CLI / VSCode | Emulates Copilot token exchange |
71
+ | `CLI_COMPAT_KIMI_CODING=1` | Kimi Coding Assistant | Emulates Kimi OAuth handshake |
72
+ | `CLI_COMPAT_CODEX=1` | OpenAI Codex / Aider | Translates legacy code completion endpoints |
73
+
74
+ ---
75
+
76
+ ## 4. API Endpoints Reference
77
+
78
+ ### OpenAI Compatible API (`/v1`)
79
+ - **Base URL**: `https://jishnupg-opencode-cli.hf.space/v1` or `/api/v1`
80
+ - **Endpoints**:
81
+ - `POST /v1/chat/completions`: Streaming and non-streaming chat completions.
82
+ - `GET /v1/models`: List all available and active models across connected providers.
83
+ - `POST /v1/embeddings`: Vector embedding generation.
84
+
85
+ ### Gemini Compatible API (`/v1beta`)
86
+ - **Base URL**: `https://jishnupg-opencode-cli.hf.space/v1beta` or `/api/v1beta`
87
+ - **Endpoints**:
88
+ - `POST /v1beta/models/{model}:generateContent`: Gemini content generation.
89
+ - `POST /v1beta/models/{model}:streamGenerateContent`: Gemini streaming content generation.
90
+
91
+ ### Live Telemetry WebSocket (`/live-ws`)
92
+ - **Base URL**: `wss://jishnupg-opencode-cli.hf.space/live-ws`
93
+ - **Function**: Real-time token throughput metrics, active request visualization, and provider fallback telemetry.