Compare commits

..
Author SHA1 Message Date
semantic-release-bot cf5b669133 chore(release): 0.15.5
## [0.15.5](https://github.com/evandrodevbr/GeminiNexus/compare/v0.15.4...v0.15.5) (2026-10-01)

### 🐛 Bug Fixes

* parse complete retry durations and preserve local IDE configuration ([b193bee](https://github.com/evandrodevbr/GeminiNexus/commit/b193bee03f1aaea5f7173e53ec6cdb787717067c))

### ✅ Tests

* mock OS exports consistently and verify LAN discovery ([fa39fb3](https://github.com/evandrodevbr/GeminiNexus/commit/fa39fb34fef662c11cd8780b562fa8008b982823))
2026-10-01 02:03:18 +00:00
Evandro Fonseca Junior 4b3f193bd0 Merge pull request #11 from evandrodevbr/codex/review-2026-09-30-049051
fix: correct retry timing and preserve local IDE configuration
2026-09-30 23:02:43 -03:00
Evandro Fonseca Junior fa39fb34fe test: mock OS exports consistently and verify LAN discovery 2026-09-30 17:43:09 -03:00
Evandro Fonseca Junior b193bee03f fix: parse complete retry durations and preserve local IDE configuration 2026-09-30 17:36:01 -03:00
semantic-release-bot e8eaa049d8 chore(release): 0.15.4
## [0.15.4](https://github.com/evandrodevbr/GeminiNexus/compare/v0.15.3...v0.15.4) (2026-09-14)

### 📝 Documentation

* establish dual license (CC-BY-NC-SA + MIT) and correct attribution ([42466c2](https://github.com/evandrodevbr/GeminiNexus/commit/42466c274b36b5dde2d7d33c9016b2fab05cf551))
* refine READMEs and highlight Windows SmartScreen warning ([0dbae81](https://github.com/evandrodevbr/GeminiNexus/commit/0dbae81353c162b0b95d566493164ea6c02e2fa2))
* rewrite the README with verified behavior and honest limitations ([acb8508](https://github.com/evandrodevbr/GeminiNexus/commit/acb8508bff2f6e32691f394d21d7c8193608cd5e))

### 💄 Styles

* clear the lint and prettier findings so the gates pass ([8ad3eef](https://github.com/evandrodevbr/GeminiNexus/commit/8ad3eef53d88d049a3a25cb54df590073a67b75b))

### 🔧 Continuous Integration

* upload release assets sequentially with retry ([b13f06c](https://github.com/evandrodevbr/GeminiNexus/commit/b13f06c0a3538ff962eae2f6fc07cfc81281bf02))
2026-09-14 11:52:31 +00:00
Evandro Fonseca Junior acb8508bff docs: rewrite the README with verified behavior and honest limitations
- README.md rebuilt from the code: account pool, proxy endpoints, settings paths, verified commands, build/make steps and limitations
- translations get a pointer to the canonical README
2026-09-14 08:51:55 -03:00
Evandro Fonseca Junior 43aa538932 chore(ci): drop trailing whitespace in the publish workflow 2026-09-14 08:51:55 -03:00
Evandro Fonseca Junior 8ad3eef53d style: clear the lint and prettier findings so the gates pass
- prettier formatting across routes, components and tests (no behavior change)
- unused imports and ternary-as-statement replaced with explicit branches
- eslint: scripts/**/*.js are CommonJS, allow require() there; prettier ignores pnpm-lock.yaml
2026-09-14 08:51:55 -03:00
30 changed files with 723 additions and 500 deletions

No files matched your search

+1 -1
View File
@@ -225,7 +225,7 @@ jobs:
fi fi
printf 'Uploading %s assets to %s\n' "${#files[@]}" "$RELEASE_TAG" printf 'Uploading %s assets to %s\n' "${#files[@]}" "$RELEASE_TAG"
# Upload files sequentially with retries to prevent GitHub API rate limits and 404/502 errors # Upload files sequentially with retries to prevent GitHub API rate limits and 404/502 errors
for file in "${files[@]}"; do for file in "${files[@]}"; do
echo "Uploading $file..." echo "Uploading $file..."
+1
View File
@@ -10,3 +10,4 @@ openspec/
.agent/ .agent/
.claude/ .claude/
.codex .codex
pnpm-lock.yaml
+26
View File
@@ -2,6 +2,32 @@
# Changelog # Changelog
## [0.15.5](https://github.com/evandrodevbr/GeminiNexus/compare/v0.15.4...v0.15.5) (2026-10-01)
### 🐛 Bug Fixes
* parse complete retry durations and preserve local IDE configuration ([b193bee](https://github.com/evandrodevbr/GeminiNexus/commit/b193bee03f1aaea5f7173e53ec6cdb787717067c))
### ✅ Tests
* mock OS exports consistently and verify LAN discovery ([fa39fb3](https://github.com/evandrodevbr/GeminiNexus/commit/fa39fb34fef662c11cd8780b562fa8008b982823))
## [0.15.4](https://github.com/evandrodevbr/GeminiNexus/compare/v0.15.3...v0.15.4) (2026-09-14)
### 📝 Documentation
* establish dual license (CC-BY-NC-SA + MIT) and correct attribution ([42466c2](https://github.com/evandrodevbr/GeminiNexus/commit/42466c274b36b5dde2d7d33c9016b2fab05cf551))
* refine READMEs and highlight Windows SmartScreen warning ([0dbae81](https://github.com/evandrodevbr/GeminiNexus/commit/0dbae81353c162b0b95d566493164ea6c02e2fa2))
* rewrite the README with verified behavior and honest limitations ([acb8508](https://github.com/evandrodevbr/GeminiNexus/commit/acb8508bff2f6e32691f394d21d7c8193608cd5e))
### 💄 Styles
* clear the lint and prettier findings so the gates pass ([8ad3eef](https://github.com/evandrodevbr/GeminiNexus/commit/8ad3eef53d88d049a3a25cb54df590073a67b75b))
### 🔧 Continuous Integration
* upload release assets sequentially with retry ([b13f06c](https://github.com/evandrodevbr/GeminiNexus/commit/b13f06c0a3538ff962eae2f6fc07cfc81281bf02))
## [0.15.3](https://github.com/evandrodevbr/GeminiNexus/compare/v0.15.2...v0.15.3) (2026-05-02) ## [0.15.3](https://github.com/evandrodevbr/GeminiNexus/compare/v0.15.2...v0.15.3) (2026-05-02)
### 👷 Build System ### 👷 Build System
+2 -1
View File
@@ -194,7 +194,7 @@ Al usar IDEs y herramientas de programación con IA, ¿te has encontrado con est
Puedes descargar los binarios precompilados más recientes para Windows, macOS y Linux desde nuestra [página de Releases](https://github.com/evandrodevbr/GeminiNexus/releases). Puedes descargar los binarios precompilados más recientes para Windows, macOS y Linux desde nuestra [página de Releases](https://github.com/evandrodevbr/GeminiNexus/releases).
*(Consulta la advertencia en la parte superior de esta página si usas Windows y te encuentras con el aviso de SmartScreen).* _(Consulta la advertencia en la parte superior de esta página si usas Windows y te encuentras con el aviso de SmartScreen)._
### 🔌 Uso con IDEs de IA ### 🔌 Uso con IDEs de IA
@@ -274,6 +274,7 @@ npm test
<summary><b>P: ¿Aviso de Windows SmartScreen ("Windows protegió su PC") durante la instalación?</b></summary> <summary><b>P: ¿Aviso de Windows SmartScreen ("Windows protegió su PC") durante la instalación?</b></summary>
Sí, este es un aviso común para nuevas aplicaciones no firmadas. Haz clic en **"Más información"** y luego en **"Ejecutar de todas formas"**. Consulta la advertencia en la parte superior del README para más detalles. Sí, este es un aviso común para nuevas aplicaciones no firmadas. Haz clic en **"Más información"** y luego en **"Ejecutar de todas formas"**. Consulta la advertencia en la parte superior del README para más detalles.
</details> </details>
<details> <details>
+210 -142
View File
@@ -1,190 +1,258 @@
<div align="center"> <p align="center">
<img src="docs/assets/logo.png" alt="Gemini Nexus" width="128" height="128" />
</p>
<img src="docs/assets/logo.png" width="110" alt="Gemini Nexus logo"> <h1 align="center">Gemini Nexus</h1>
<h1>Gemini Nexus</h1> <p align="center">
<strong>Desktop gateway that pools several Google Gemini and Claude accounts behind a single local OpenAI- and Anthropic-compatible API.</strong>
</p>
<p><strong>Desktop gateway that pools Google accounts behind a local OpenAI/Anthropic-compatible API, with encrypted credentials and auto-switching.</strong></p> <p align="center">
English | <a href="README.pt-BR.md">Português</a> | <a href="README.zh-CN.md">中文</a> | <a href="README.es.md">Español</a>
</p>
<a href="#-quickstart">Quickstart</a> · <p align="center">
<a href="#-architecture">Architecture</a> · <a href="https://github.com/evandrodevbr/GeminiNexus/releases"><img src="https://img.shields.io/github/v/release/evandrodevbr/GeminiNexus?style=flat-square" alt="Release" /></a>
<a href="#-security-model">Security</a> · <a href="https://github.com/evandrodevbr/GeminiNexus/actions/workflows/testing.yaml"><img src="https://github.com/evandrodevbr/GeminiNexus/actions/workflows/testing.yaml/badge.svg" alt="Tests" /></a>
<a href="https://git.evandro.dev.br/evandro/gemini-nexus/issues">Issues</a> <img src="https://img.shields.io/badge/platform-Windows%20%7C%20macOS%20%7C%20Linux-informational?style=flat-square" alt="Platform" />
<img src="https://img.shields.io/badge/electron-41-47848F?style=flat-square&logo=electron&logoColor=white" alt="Electron 41" />
<img src="https://img.shields.io/badge/react-19-61DAFB?style=flat-square&logo=react&logoColor=white" alt="React 19" />
<img src="https://img.shields.io/badge/node-%3E%3D20.19-5FA04E?style=flat-square&logo=nodedotjs&logoColor=white" alt="Node 20.19+" />
<img src="https://img.shields.io/badge/license-MIT%20%2B%20CC%20BY--NC--SA%204.0-blue?style=flat-square" alt="License" />
</p>
![Stack](https://img.shields.io/badge/Stack-Electron_41_%C2%B7_React_19-47848F?logo=electron&logoColor=white) > **Windows users:** the installers are not code-signed, so SmartScreen may show "Windows protected your PC". Choose "More info" and then "Run anyway"; this is the expected prompt for an unsigned application.
![Status](https://img.shields.io/badge/status-v0.15.3_active-brightgreen)
![License](https://img.shields.io/badge/license-CC_BY--NC--SA_4.0_%2B_MIT-lightgrey)
<sub>English · <a href="README.pt-BR.md">Português</a> · <a href="README.zh-CN.md">中文</a> · <a href="README.es.md">Español</a></sub> ## About
</div> AI coding tools assume a single account with a single quota. When that quota runs out, you have to switch accounts by hand, and you usually have no idea how many tokens you burned.
<hr /> Gemini Nexus is an Electron desktop application that runs a local gateway (an embedded NestJS server) in front of a pool of Google Gemini and Claude accounts. Your IDE points at the local gateway once; the gateway picks an account, translates the protocol, retries on quota or rate-limit errors, and records usage.
## 📌 The problem What it does:
Working with AI-powered IDEs and coding tools on a single Google account means quota that runs out, manual account switching, no idea how many tokens you spent, and no view of what the proxy actually sends upstream. Gemini Nexus keeps all accounts in one pool, switches between them automatically and shows the traffic. - Manages a pool of Google/Claude accounts with per-account status (active, rate limited, expired) and quota snapshots.
- Serves an OpenAI-compatible surface (`/v1/chat/completions`, `/v1/models`, `/v1/completions`, `/v1/responses`, images and audio routes), an Anthropic-compatible surface (`/v1/messages`) and the Gemini-native surface (`/v1beta/models`, `countTokens`).
- Streams responses over SSE and maps model names in both directions (for example `claude-sonnet-4-5` to the configured Gemini/Claude target).
- Stores tokens and keys encrypted at rest (AES-256-GCM) with an OS keychain wrapper, falling back to a local key file.
- Shows local analytics: token usage, cost estimates, model distribution, request traffic and request replay.
## ✨ What it does It is a personal desktop tool, not a hosted service: everything runs on your machine.
- **Pools** Google accounts added through OAuth, with status (Active, Rate Limited, Expired), last-used time and an optional per-account proxy URL. ## How it works
- **Switches** accounts automatically when quota drops below 5 % or an account is rate-limited; `CloudMonitorService` polls quota every 5 minutes.
- **Schedules** requests across the pool in `cache-first`, `balance` (default) or `performance-first` mode, with a per-account circuit breaker (default backoff 60 s → 5 min → 30 min → 2 h).
- **Serves** `POST /v1/chat/completions` (OpenAI), `POST /v1/messages` (Anthropic), `GET /v1/models` and `GET /v1/models/capabilities`, so Cursor, Windsurf, OpenCode, Claude Code and similar clients can connect.
- **Maps** requested model names to upstream models through the `custom_mapping` and `anthropic_mapping` settings; request timeout defaults to 120 s.
- **Tracks** token usage per account and model, charts prompt and completion tokens, and estimates cost with editable per-model pricing.
- **Inspects** traffic (latency, status, model), replays logged requests and generates cURL and Python snippets plus an IDE Quick Setup.
- **Protects** stored credentials with AES-256-GCM and an OS-keychain master key (see [Security model](#-security-model)).
## 🖼️ Demo
<details>
<summary>Screenshots</summary>
<p align="center"><img src="docs/assets/main.png" alt="Main dashboard" width="80%"></p>
<p align="center"><img src="docs/assets/2usage.png" alt="Usage analytics" width="80%"></p>
<p align="center"><img src="docs/assets/3proxy.png" alt="Proxy monitor" width="80%"></p>
<p align="center"><img src="docs/assets/4routing.png" alt="Routing config" width="80%"></p>
<p align="center"><img src="docs/assets/5docs.png" alt="Documentation" width="80%"></p>
<p align="center"><img src="docs/assets/6connections.png" alt="Active connections" width="80%"></p>
<p align="center"><img src="docs/assets/7config.png" alt="Settings" width="80%"></p>
</details>
## 🚀 Quickstart
**Use a release.** Download the build for your platform from [Releases](https://git.evandro.dev.br/evandro/gemini-nexus/releases), open the app, add at least one account, enable the proxy (it is disabled by default) and point your IDE at it:
```text ```text
API Base URL: http://localhost:8045/v1 (default, configurable on the Proxy page; development builds use 8046) IDE / CLI (Cursor, Windsurf, OpenCode, Claude Code, custom scripts)
API Key: (copy it from the Proxy page in the app) | OpenAI /v1/chat/completions Anthropic /v1/messages Gemini /v1beta
Model: gemini-3-flash (or any mapped model) v
Gemini Nexus (Electron app)
+-- Renderer: React 19 UI (accounts, usage, proxy, settings)
+-- Main process: IPC, SQLite storage, encryption, tray
+-- Gateway: NestJS + Fastify, bound to port 8045 (8046 in development)
| 1. authenticate the request (Bearer / x-api-key / x-goog-api-key)
| 2. choose an account from the pool (quota, rate limit, circuit breaker)
| 3. translate the protocol and call the upstream API
| 4. stream the answer back and persist usage metrics
v
Google Gemini API / Claude API (per-account OAuth credentials)
``` ```
> [!WARNING] - The gateway is started by the desktop app (Proxy page, or auto-start if enabled). It is a child process, so closing the app stops it.
> Builds are not code-signed. If Windows shows "Windows protected your PC", click **More info → Run anyway**. - Account selection honours a scheduling mode (`balance`, `cache-first`, `performance-first`), a preferred account, per-account upstream proxy URLs, and a circuit breaker with backoff steps.
- Requests are recorded locally (SQLite) and can be replayed from the Proxy page for debugging.
**Build from source.** ## Stack
| Tool | Minimum version | | Layer | Choice |
|---|---| |---|---|
| Node.js | 20+ (recommended in `AGENTS.md`; `.nvmrc` pins 22.17.1) | | Desktop shell | Electron 41, Electron Forge 7 (Vite plugin) |
| pnpm | 10.11.0 (`packageManager`; lockfile is `pnpm-lock.yaml`) | | UI | React 19, TypeScript 5.9, TanStack Router + Query, Tailwind CSS v4, Radix UI, Nivo charts |
| Native toolchain | needed by `better-sqlite3` and `keytar`: Visual Studio 2022 Build Tools (C++) on Windows, Xcode Command Line Tools on macOS, `build-essential` + `python3` + `libsecret-1-dev` on Ubuntu (`base-devel` + `python` + `libsecret` on Arch) | | Gateway | NestJS 11 on Fastify 5, RxJS, SSE streaming |
| Storage | better-sqlite3 + Drizzle ORM / raw SQL, JSON config in the app data dir |
| Secrets | Node crypto AES-256-GCM, `safeStorage` / `keytar` with file fallback |
| Validation | Zod schemas, class-validator |
| Internationalization | react-i18next (en, zh-CN, ru, vi) |
| Tests | Vitest (unit), Testing Library, Playwright (E2E, local only) |
| Package manager | pnpm 10.11.0 (`packageManager` field, Corepack) |
## Requirements
- Node.js `^20.19.0 || >=22.12.0` (Vite 8 engine range). The repository pins 22.17.1 in `.nvmrc`; this audit ran on Node 24.20.0.
- pnpm `10.11.0` (declared in `package.json`). With Corepack enabled, `pnpm` resolves to that version automatically.
- Native build toolchain for `better-sqlite3` and `keytar`: on Linux, `base-devel`/`build-essential`, `python3` and `libsecret`; on macOS, Xcode Command Line Tools; on Windows, Visual Studio Build Tools with the C++ workload. `pnpm install` runs `scripts/setup-dev.js --check-only`, which reports what is missing.
- A graphical session to run the desktop app itself (Electron).
- For installers: `dpkg`/`rpmbuild` on Linux, WiX Toolset on Windows.
## Quick start
```bash ```bash
git clone https://git.evandro.dev.br/evandro/gemini-nexus.git git clone https://github.com/evandrodevbr/GeminiNexus.git
cd gemini-nexus cd GeminiNexus
pnpm install # postinstall runs scripts/setup-dev.js --check-only to verify the toolchain
pnpm start # dev mode (Electron + Vite HMR) # 1. install dependencies (lockfile is pnpm; do not use npm install)
pnpm run make # installers/packages for the current platform corepack enable
pnpm run make:win # Windows: extracts WiX from wix314-binaries.zip, then runs make pnpm install --frozen-lockfile
# 2. run the desktop app (Electron + Vite HMR); the gateway listens on 8046 in dev
pnpm start
``` ```
There is no `.env.example` and no variable is required. Optional variables read by the code or described in the docs: Then, in the app:
| Variable | Required | Description | 1. Add at least one Google or Claude account on the Accounts page (OAuth flow).
|---|---|---| 2. Open the Proxy page, enable the gateway and copy the generated API key.
| `HTTP_PROXY` / `HTTPS_PROXY` / `NO_PROXY` | No | Route the app's outbound requests through a local proxy (see [TLS inspection](#-security-model)). | 3. Point your IDE or CLI at the local endpoint.
| `NODE_EXTRA_CA_CERTS` | No | Extra CA certificate for Node-side TLS when inspecting traffic. |
| `SENTRY_DSN` | No | Sentry DSN used when error reporting is enabled. |
## 🧱 Architecture ```text
API base URL: http://localhost:8045/v1
```mermaid API key: copied from the Proxy page
flowchart LR Model: gemini-3-flash (or any model id returned by GET /v1/models)
C["Client: Cursor, Windsurf, OpenCode, Claude Code"] -->|"/v1/chat/completions, /v1/messages"| P["NestJS proxy, port 8045"]
P --> S["Account scheduler and circuit breaker"]
S -->|"Google OAuth account"| G["Google upstream APIs"]
M["CloudMonitor and AutoSwitch, quota every 5 min"] -->|"quota polling"| G
S -->|"reads accounts"| DB[("SQLite: better-sqlite3 and Drizzle")]
M --> DB
K["AES-256-GCM, master key in OS keychain"] -->|"encrypts tokens"| DB
``` ```
1. A client sends OpenAI- or Anthropic-format requests to the NestJS proxy (Fastify, bound to `0.0.0.0`). ## Usage
2. The proxy guard checks the API key when one is configured; with an empty key the proxy runs in open mode.
3. The token manager picks an account according to the scheduling mode and each account's circuit-breaker state, then calls Google's upstream APIs with that account's OAuth token.
4. `CloudMonitorService` polls quota every 5 minutes and `AutoSwitchService` switches the active account when quota is below 5 % or the account is rate-limited.
5. The React 19 renderer (TanStack Router and Query) talks to the main process over oRPC on IPC; accounts, usage and settings live in SQLite (better-sqlite3 + Drizzle). Design notes: [`docs/cloud_features.md`](docs/cloud_features.md).
## 🔒 Security model Daily commands, all driven by `pnpm`:
Gemini Nexus holds OAuth tokens for several accounts and forwards traffic upstream, so credential handling is the core of the design. Everything below is implemented in this repository; limits are stated where they exist. ```bash
pnpm start # dev app + gateway (port 8046)
pnpm run package # unpack the app for the current platform (out/)
pnpm run make # build installers (needs the extra tooling listed above)
pnpm test # Vitest unit/integration suite
pnpm run type-check # tsc --noEmit
pnpm run lint # ESLint
pnpm run format # Prettier check (pnpm run format:write to fix)
pnpm run test:e2e # Playwright E2E, needs a graphical session
```
- **Credential storage and isolation.** `token_json` and `quota_json` are encrypted with AES-256-GCM (unique IV per operation, authenticated) before they reach SQLite. The master key lives in the OS credential store through `keytar` (Windows Credential Manager, macOS Keychain, Linux Secret Service/libsecret), never in a plain file. Legacy plaintext rows are migrated at startup. Accounts are kept in their own database (`cloud_accounts.db`), independent from the IDE's local database. Settings live in the app data directory (`%APPDATA%\Gemini Nexus` on Windows, `~/Library/Application Support/Gemini Nexus` on macOS, `~/.config/Gemini Nexus` on Linux; `${app}-dev` names in development). Account metadata is stored in `~/.geminiNexus-agent/geminiNexus_accounts.json`.
- **Log masking.** Keys such as `token`, `refresh_token`, `api_key`, `authorization`, `cookie` and `client_secret` are replaced by `[REDACTED]` before logging ([`src/utils/sensitiveDataMasking.ts`](src/utils/sensitiveDataMasking.ts)); the traffic logger also redacts the `authorization`, `x-api-key` and `cookie` headers.
- **Request logging, not an audit trail.** Winston writes API, traffic and app logs to rotating files (API log: 30 days, 50 MB per file). These are operational logs, not a tamper-evident audit trail; the `*-audit.json` files are rotation bookkeeping from `winston-daily-rotate-file`.
- **Proxy access.** The proxy is off by default. Once enabled it listens on `0.0.0.0`, and requests are authenticated only when `api_key` is non-empty (empty means open mode). Set a key and keep it private.
- **TLS inspection (debugging only).** [`docs/proxyman-install.md`](docs/proxyman-install.md) and [`docs/proxyman-debugging.md`](docs/proxyman-debugging.md) explain how to read the app's HTTPS traffic with Proxyman: trust its root CA, set the proxy variables and `NODE_EXTRA_CA_CERTS`, then start the app. `NODE_TLS_REJECT_UNAUTHORIZED=0` (and the script's `-AllowInsecureTls` flag) is a temporary override; never leave it on. Do not commit proxy addresses, certificate paths, cookies or OAuth codes, and redact logs before sharing them. `scripts/start-antigravity-with-proxyman.ps1` targets the installed official Antigravity client, not this app.
- **Error reporting.** Sentry reports (they need `SENTRY_DSN`) are gated by the `error_reporting_enabled` setting, which `DEFAULT_APP_CONFIG` ships as `true`; turn it off in Settings to opt out. The Windows user-name segment is masked in reported exception messages.
Supported versions and how to report a vulnerability: [`SECURITY.md`](SECURITY.md). ## Screenshots
## 🗂️ Structure | | |
|---|---|
| ![Main dashboard](docs/assets/main.png) | ![Usage analytics](docs/assets/2usage.png) |
| ![Proxy monitor](docs/assets/3proxy.png) | ![Routing config](docs/assets/4routing.png) |
| ![Documentation](docs/assets/5docs.png) | ![Active connections](docs/assets/6connections.png) |
| ![Settings](docs/assets/7config.png) | |
## API
Routes actually mapped by the gateway (read from the running server):
| Route | Purpose |
|---|---|
| `GET /v1/models` | OpenAI-style model list built from the built-in mapping plus models collected from accounts. |
| `GET /v1/models/capabilities` | Per-model flags: vision, streaming, JSON mode, audio, image generation. |
| `GET /v1/status` | Gateway health, uptime, account counters and request metrics. |
| `POST /v1/chat/completions` | OpenAI Chat Completions, streaming and non-streaming. |
| `POST /v1/completions` | Legacy completions compatibility endpoint. |
| `POST /v1/responses` | OpenAI Responses-style endpoint. |
| `POST /v1/messages` | Anthropic Messages API. |
| `POST /v1/messages/chat/completions` | Anthropic-shaped requests over the chat completions path. |
| `POST /v1/batch/completions` | Batched chat completion requests. |
| `POST /v1/images/generations`, `POST /v1/images/edits` | Image endpoints. |
| `POST /v1/audio/transcriptions` | Audio transcription endpoint. |
| `GET /v1/events` | Server-sent events stream of gateway activity. |
| `GET /v1/replay/requests`, `POST /v1/replay/:requestId` | List recorded requests and replay one of them. |
| `GET /v1beta/models`, `GET /v1beta/models/:model` | Gemini-native model listing. |
| `POST /v1beta/models/:modelAction`, `POST /v1beta/models/:model/countTokens` | Gemini-native generate and token counting. |
Authentication: the gateway accepts the API key as `Authorization: Bearer <key>`, `x-api-key: <key>` or `x-goog-api-key: <key>`. If the configured key is empty, the gateway runs open, which is only sensible while it is bound to localhost.
## Build and release
```bash
pnpm run package # app bundle in out/<product>-<platform>-<arch>
pnpm run make # installers (deb, rpm, AppImage, zip on Linux; dmg/zip on macOS; WiX installer on Windows)
pnpm run make:win # Windows helper script
```
The gateway is statically bundled into the Electron app; there is no separate server deployment.
Release automation in `.github/workflows/`:
- `format.yaml` and `lint.yaml` run Prettier and ESLint on pull requests to `main`.
- `testing.yaml` runs the unit suite and `tsc --noEmit` on pushes and pull requests. E2E is intentionally excluded because it needs a graphical environment.
- `release.yml` runs semantic-release on `main`, which tags the version, updates `CHANGELOG.md` and creates the GitHub release.
- `publish.yaml` builds `win32`, `darwin` and `linux` for x64 and arm64 when the Release workflow succeeds, and uploads the installers to the release.
## Project structure
```text ```text
src/ src/
├── components/ # UI (proxy/, usage/, ui/ primitives) ├── main.ts, preload.ts, renderer.ts Electron entry points
├── ipc/ # oRPC routers (account, cloud, config, gateway, usage...) ├── routes/ TanStack Router pages: index (accounts), usage, proxy, settings
├── server/ # NestJS proxy gateway (modules: proxy, usage) ├── components/ React UI (accounts, proxy/advanced, usage, ui primitives)
├── services/ # AutoSwitchService, CloudMonitorService, GoogleAPIService ├── server/ Embedded gateway
├── routes/ # index, usage, proxy, settings │ ├── main.ts NestJS bootstrap (port and process lifecycle)
├── utils/ # paths, logger, sensitiveDataMasking │ ├── modules/proxy/ controllers, proxy service, token manager, SSE, replay, metrics
└── tests/ # unit tests (Vitest) and E2E (Playwright) │ └── modules/usage/ token usage service
cli/ # separate Python CLI for account switching and quota checks ├── ipc/ Electron IPC handlers (accounts, config, database, proxy, tray, ...)
docs/ # design notes and Proxyman guides ├── lib/geminiNexus/ protocol mappers, model mapping, retry and streaming helpers
scripts/ # setup-dev, build-windows, generate-assets ├── localization/ i18n resources
├── utils/ paths, logging, encryption, traffic logger
└── tests/ unit, integration and e2e suites
docs/ OpenCode integration, cloud features, Proxyman guides, brand notes
scripts/ dev environment setup, Windows build helper, asset generation
``` ```
## 🧪 Scripts ## Verification
| Command | What it does | Previously recorded Linux x64 checks (Node 24.20.0, pnpm 10.11.0), before the review below:
| Command | Result |
|---|---| |---|---|
| `pnpm start` | Run the app in dev mode (`electron-forge start`, with `DEBUG=electron-forge:*`). | | `pnpm install --frozen-lockfile` | exit 0 |
| `pnpm run dev` | Same as `start`. | | `pnpm test` | 54 test files, 499 tests, all passing |
| `pnpm run package` | Package the app with Electron Forge, without building installers. | | `pnpm run type-check` | exit 0 |
| `pnpm run make` | Build installers/packages for the current platform. | | `pnpm run lint` | exit 0 (327 warnings, no errors) |
| `pnpm run make:win` | Windows build helper: extracts WiX from `wix314-binaries.zip`, adds it to `PATH`, runs `electron-forge make`. | | `pnpm run format` | all files formatted |
| `pnpm run publish` | Publish through Electron Forge. | | `pnpm run package` | exit 0, `out/Gemini Nexus-linux-x64` |
| `pnpm run setup` | Run `scripts/setup-dev.js`: detect the native toolchain and install or guide the setup. | | `pnpm run test:e2e` | not run: Playwright drives Electron and needs a graphical session |
| `pnpm run postinstall` | `scripts/setup-dev.js --check-only`; runs automatically after `pnpm install`. |
| `pnpm run lint` | ESLint over the repo. |
| `pnpm run format` | Prettier check. |
| `pnpm run format:write` | Prettier write. |
| `pnpm test` | Vitest unit tests (`vitest run`). |
| `pnpm run test:watch` | Vitest in watch mode. |
| `pnpm run test:unit` | Vitest (default mode). |
| `pnpm run test:e2e` | Playwright E2E tests. |
| `pnpm run test:all` | Unit tests, then Playwright. |
| `pnpm run type-check` | `tsc --noEmit`. |
## 📚 Documentation The E2E suite is also excluded from CI for the same reason.
- [`docs/cloud_features.md`](docs/cloud_features.md): account pool, quota monitoring and auto-switch design. ## Current state and limitations
- [`docs/proxyman-install.md`](docs/proxyman-install.md) and [`docs/proxyman-debugging.md`](docs/proxyman-debugging.md): TLS inspection setup (Windows).
- [`docs/OpencodeAPI.md`](docs/OpencodeAPI.md): OpenCode custom-endpoint integration (in Portuguese).
- [`cli/README.md`](cli/README.md): the Python command-line tool.
- [`CONTRIBUTING.md`](CONTRIBUTING.md), [`SECURITY.md`](SECURITY.md), [`CODE_OF_CONDUCT.md`](CODE_OF_CONDUCT.md), [`CHANGELOG.md`](CHANGELOG.md), [`LICENSE`](LICENSE), [`LICENSE-MIT`](LICENSE-MIT).
- Translations: [Português](README.pt-BR.md), [中文](README.zh-CN.md), [Español](README.es.md).
## 🛣️ Limitations and next steps - Upstream calls cannot be exercised without real Google/Claude accounts. With an empty pool the gateway starts, lists models and answers `429 No available accounts (all exhausted or rate limited)` on inference routes.
- The app is desktop-only (Windows, macOS, Linux). There is no headless or container mode, because the gateway is a child process of the Electron app.
- Accounts are added through the app UI and the OAuth flow; there is no CLI to import credentials.
- UI languages are en, zh-CN, ru and vi. The default language in `DEFAULT_APP_CONFIG` is `zh-CN`.
- Windows installers are unsigned (SmartScreen warning). macOS builds are unsigned and unnotarized.
- The E2E suite does not run in CI.
- The project is mid-migration to a single license: the code inherited from AntigravityManager is CC BY-NC-SA 4.0 (non-commercial), while the new code is MIT. See the license section.
- `pnpm install` reports `Ignored build scripts: sharp`. `sharp` is only used by `scripts/generate-assets.ts` for icon generation, and its prebuilt binaries are installed through optional dependencies.
- Upstream access goes through Google's `v1internal` Cloud Code endpoints and OAuth scopes ([`src/services/GoogleAPIService.ts`](src/services/GoogleAPIService.ts)). These are internal, undocumented APIs and may change without notice. The project is unofficial and not affiliated with Google or Anthropic. ## Documentation
- Accounts are added through Google OAuth; Claude models are reached through model mapping, not through separate Claude accounts.
- End-to-end tests are not run in CI (Electron needs a graphical environment); run `pnpm run test:e2e` locally.
- The Windows MSI is only produced when the WiX Toolset is available on the build machine; otherwise the Windows artifact is the ZIP.
- The workflows in `.github/workflows/` are written for GitHub (they call the `gh` CLI and the GitHub release publisher), so this README shows no CI badge.
- Next step stated by the project: migrate the remaining original code to MIT (see [License](#-license)).
## 🤝 Contributing | Document | Content |
|---|---|
| [`docs/OpencodeAPI.md`](docs/OpencodeAPI.md) | Wiring OpenCode to the local custom endpoint |
| [`docs/cloud_features.md`](docs/cloud_features.md) | Cloud account management and switching behaviour |
| [`docs/proxyman-debugging.md`](docs/proxyman-debugging.md) | Debugging upstream traffic with Proxyman |
| [`docs/proxyman-install.md`](docs/proxyman-install.md) | Proxyman setup for contributors |
| [`docs/brand-briefing.md`](docs/brand-briefing.md) | Naming, positioning and visual notes |
| [`CONTRIBUTING.md`](CONTRIBUTING.md), [`AGENTS.md`](AGENTS.md) | Contribution rules and repository conventions |
| [`CHANGELOG.md`](CHANGELOG.md) | Release history |
Read [`CONTRIBUTING.md`](CONTRIBUTING.md) and [`CODE_OF_CONDUCT.md`](CODE_OF_CONDUCT.md). Releases are versioned by semantic-release from Conventional Commits. Before opening a PR, these must pass: ## License
```bash Dual license:
pnpm run lint && pnpm run format:write && pnpm run type-check && pnpm test
```
## 📄 License - Original code from [AntigravityManager](https://github.com/Draculabo/AntigravityManager) by [Draculabo](https://github.com/Draculabo): [CC BY-NC-SA 4.0](LICENSE).
- All new code, features and architecture by [evandrodevbr](https://github.com/evandrodevbr): [MIT](LICENSE-MIT).
Mixed licensing: code inherited from [AntigravityManager](https://github.com/Draculabo/AntigravityManager) (by Draculabo) is [CC BY-NC-SA 4.0](LICENSE) (non-commercial, share-alike); all new code is [MIT](LICENSE-MIT). The project is migrating to MIT as the remaining original code is rewritten, so check both files before any commercial use. The project is being migrated to MIT as the remaining original code is rewritten.
## Credits
Originally forked from [AntigravityManager](https://github.com/Draculabo/AntigravityManager). Since the fork the codebase has been largely rewritten by [evandrodevbr](https://github.com/evandrodevbr): UI/UX, usage analytics, traffic monitor, environment isolation, CI/CD, test infrastructure and documentation.
## Review checks (2026-09-30)
Retry durations are parsed as complete values: `500ms` is milliseconds, and fractional components are rounded after summation. Invalid trailing text is rejected. Account and model lockouts honor the later expiration. IDE fallback configuration remains available at loopback when the OS denies network-interface enumeration.
Focused checks: `pnpm exec vitest run src/tests/unit/rate-limit-tracker.test.ts src/tests/unit/ipc/proxy-advanced-handler.test.ts`. Run `pnpm test`, `pnpm type-check`, `pnpm lint` and `pnpm format` for the full non-E2E gates. Native SQLite/keytar are mocked by the unit-test configuration; Electron packaging, real account switching and E2E still require separate validation.
This review passes all 509 tests across 54 files, type checking, lint (327 existing warnings, no errors) and formatting with Node 24.19.0 and pnpm 10.11.0. The original lockfile is retained: its audit still reports two critical findings, in `tar` and `seroval`, requiring a separately validated dependency update.
+2 -1
View File
@@ -194,7 +194,7 @@ O **Gemini Nexus** resolve tudo isso. É um aplicativo desktop profissional em E
Você pode baixar os binários pré-compilados mais recentes para Windows, macOS e Linux na nossa [página de Releases](https://github.com/evandrodevbr/GeminiNexus/releases). Você pode baixar os binários pré-compilados mais recentes para Windows, macOS e Linux na nossa [página de Releases](https://github.com/evandrodevbr/GeminiNexus/releases).
*(Consulte o aviso no topo desta página se você estiver usando o Windows e encontrar o prompt do SmartScreen).* _(Consulte o aviso no topo desta página se você estiver usando o Windows e encontrar o prompt do SmartScreen)._
### 🔌 Usando com IDEs de IA ### 🔌 Usando com IDEs de IA
@@ -295,6 +295,7 @@ src/
<summary><b>P: Aviso do Windows SmartScreen ("O Windows protegeu o seu computador") durante a instalação?</b></summary> <summary><b>P: Aviso do Windows SmartScreen ("O Windows protegeu o seu computador") durante a instalação?</b></summary>
Sim, este é um aviso comum para novos aplicativos não assinados. Clique em **"Mais informações"** e depois em **"Executar mesmo assim"**. Veja o aviso no topo do README para mais detalhes. Sim, este é um aviso comum para novos aplicativos não assinados. Clique em **"Mais informações"** e depois em **"Executar mesmo assim"**. Veja o aviso no topo do README para mais detalhes.
</details> </details>
<details> <details>
+2 -1
View File
@@ -194,7 +194,7 @@
您可以从我们的 [Releases 页面](https://github.com/evandrodevbr/GeminiNexus/releases) 下载适用于 Windows、macOS 和 Linux 的最新预编译版本。 您可以从我们的 [Releases 页面](https://github.com/evandrodevbr/GeminiNexus/releases) 下载适用于 Windows、macOS 和 Linux 的最新预编译版本。
*(如果您使用的是 Windows 并在安装时遇到了 SmartScreen 提示,请参阅页面顶部的警告)。* _(如果您使用的是 Windows 并在安装时遇到了 SmartScreen 提示,请参阅页面顶部的警告)。_
### 🔌 配合 AI IDE 使用 ### 🔌 配合 AI IDE 使用
@@ -274,6 +274,7 @@ npm test
<summary><b>问:安装时出现 Windows SmartScreen 警告("Windows 已保护你的电脑")?</b></summary> <summary><b>问:安装时出现 Windows SmartScreen 警告("Windows 已保护你的电脑")?</b></summary>
是的,对于未签名的新应用,这是一个常见的警告。请点击 **"更多信息"** (More info),然后点击 **"仍要运行"** (Run anyway)。详情请参阅 README 顶部的提示。 是的,对于未签名的新应用,这是一个常见的警告。请点击 **"更多信息"** (More info),然后点击 **"仍要运行"** (Run anyway)。详情请参阅 README 顶部的提示。
</details> </details>
<details> <details>
+7
View File
@@ -50,4 +50,11 @@ export default defineConfig([
'prefer-const': 'off', 'prefer-const': 'off',
}, },
}, },
{
// Node maintenance scripts are CommonJS; require() is the native API there.
files: ['scripts/**/*.js'],
rules: {
'@typescript-eslint/no-require-imports': 'off',
},
},
]); ]);
+1 -1
View File
@@ -1,7 +1,7 @@
{ {
"name": "gemini-nexus", "name": "gemini-nexus",
"productName": "Gemini Nexus", "productName": "Gemini Nexus",
"version": "0.15.3", "version": "0.15.5",
"description": "Gemini Nexus - Electron App", "description": "Gemini Nexus - Electron App",
"main": ".vite/build/main.js", "main": ".vite/build/main.js",
"private": true, "private": true,
+5 -1
View File
@@ -417,7 +417,11 @@ export function CloudAccountList() {
const setSelectionState = (id: string, selected: boolean) => const setSelectionState = (id: string, selected: boolean) =>
setSelectedIds((prev) => { setSelectedIds((prev) => {
const next = new Set(prev); const next = new Set(prev);
selected ? next.add(id) : next.delete(id); if (selected) {
next.add(id);
} else {
next.delete(id);
}
return next; return next;
}); });
const toggleSelectAllAccounts = () => const toggleSelectAllAccounts = () =>
-1
View File
@@ -1,6 +1,5 @@
import React from 'react'; import React from 'react';
import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card'; import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card';
import { calculateCost } from '@/constants/pricing';
export interface ModelCostEntry { export interface ModelCostEntry {
model: string; model: string;
+11 -4
View File
@@ -1,4 +1,4 @@
import React, { useState, useEffect } from 'react'; import React, { useState } from 'react';
import { import {
Dialog, Dialog,
DialogContent, DialogContent,
@@ -10,7 +10,6 @@ import {
import { Button } from '@/components/ui/button'; import { Button } from '@/components/ui/button';
import { Input } from '@/components/ui/input'; import { Input } from '@/components/ui/input';
import { Badge } from '@/components/ui/badge'; import { Badge } from '@/components/ui/badge';
import { Label } from '@/components/ui/label';
import { Loader2, RefreshCw, RotateCcw } from 'lucide-react'; import { Loader2, RefreshCw, RotateCcw } from 'lucide-react';
import { MODEL_PRICING } from '@/constants/pricing'; import { MODEL_PRICING } from '@/constants/pricing';
@@ -35,8 +34,16 @@ export const ModelPricingDialog: React.FC<ModelPricingDialogProps> = ({
Record<string, { input: number; output: number; source: string }> Record<string, { input: number; output: number; source: string }>
>({}); >({});
const [isFetching, setIsFetching] = useState(false); const [isFetching, setIsFetching] = useState(false);
const [pricingSource, setPricingSource] = useState<{
open: boolean;
customPricing: typeof customPricing;
}>({ open, customPricing });
useEffect(() => { // Re-seed the editable copy when the dialog opens or the source pricing changes.
// Adjusting state during render replaces the previous synchronous setState inside
// an effect (https://react.dev/learn/you-might-not-need-an-effect).
if (pricingSource.open !== open || pricingSource.customPricing !== customPricing) {
setPricingSource({ open, customPricing });
if (open) { if (open) {
// Initialize local state with custom pricing or empty if default // Initialize local state with custom pricing or empty if default
const initial: Record<string, { input: number; output: number; source: string }> = {}; const initial: Record<string, { input: number; output: number; source: string }> = {};
@@ -49,7 +56,7 @@ export const ModelPricingDialog: React.FC<ModelPricingDialogProps> = ({
}); });
setLocalPricing(initial); setLocalPricing(initial);
} }
}, [open, customPricing]); }
const handlePriceChange = (model: string, type: 'input' | 'output', value: string) => { const handlePriceChange = (model: string, type: 'input' | 'output', value: string) => {
const numValue = parseFloat(value); const numValue = parseFloat(value);
+2 -3
View File
@@ -58,9 +58,8 @@ export const MonitorTab: React.FC = () => {
tokensPrompt: log.metadata?.tokensPrompt as number | undefined, tokensPrompt: log.metadata?.tokensPrompt as number | undefined,
tokensCompletion: log.metadata?.tokensCompletion as number | undefined, tokensCompletion: log.metadata?.tokensCompletion as number | undefined,
tokensTotal: tokensTotal:
(((log.metadata?.tokensPrompt as number) || 0) + ((log.metadata?.tokensPrompt as number) || 0) +
((log.metadata?.tokensCompletion as number) || 0)) || ((log.metadata?.tokensCompletion as number) || 0) || undefined,
undefined,
})) }))
: []; : [];
const stats = statsData?.success ? statsData.data : undefined; const stats = statsData?.success ? statsData.data : undefined;
+46 -28
View File
@@ -26,7 +26,16 @@ interface TrafficLogTableProps {
isLoading: boolean; isLoading: boolean;
} }
type SortKey = 'timestamp' | 'direction' | 'endpoint' | 'model' | 'method' | 'status' | 'tokens' | 'cost' | 'duration'; type SortKey =
| 'timestamp'
| 'direction'
| 'endpoint'
| 'model'
| 'method'
| 'status'
| 'tokens'
| 'cost'
| 'duration';
type SortDir = 'asc' | 'desc'; type SortDir = 'asc' | 'desc';
function SortIcon({ function SortIcon({
@@ -79,13 +88,10 @@ function getBodyPreview(body: unknown): string | null {
// Request body: extract first user message // Request body: extract first user message
if (Array.isArray(obj.messages)) { if (Array.isArray(obj.messages)) {
const userMsg = obj.messages.find( const userMsg = obj.messages.find((m: Record<string, unknown>) => m.role === 'user');
(m: Record<string, unknown>) => m.role === 'user',
);
if (userMsg) { if (userMsg) {
const content = typeof userMsg.content === 'string' const content =
? userMsg.content typeof userMsg.content === 'string' ? userMsg.content : JSON.stringify(userMsg.content);
: JSON.stringify(userMsg.content);
return content.length > 120 ? content.slice(0, 120) + '…' : content; return content.length > 120 ? content.slice(0, 120) + '…' : content;
} }
} }
@@ -95,9 +101,7 @@ function getBodyPreview(body: unknown): string | null {
const firstChoice = obj.choices[0] as Record<string, unknown> | undefined; const firstChoice = obj.choices[0] as Record<string, unknown> | undefined;
const message = firstChoice?.message as Record<string, unknown> | undefined; const message = firstChoice?.message as Record<string, unknown> | undefined;
if (message?.content && typeof message.content === 'string') { if (message?.content && typeof message.content === 'string') {
return message.content.length > 120 return message.content.length > 120 ? message.content.slice(0, 120) + '…' : message.content;
? message.content.slice(0, 120) + '…'
: message.content;
} }
} }
@@ -254,18 +258,16 @@ export const TrafficLogTable: React.FC<TrafficLogTableProps> = ({ logs, isLoadin
<React.Fragment key={logKey}> <React.Fragment key={logKey}>
<tr <tr
className={`cursor-pointer border-b border-white/[0.04] transition-colors last:border-0 ${ className={`cursor-pointer border-b border-white/[0.04] transition-colors last:border-0 ${
isExpanded isExpanded ? 'bg-white/[0.03]' : 'hover:bg-white/[0.02]'
? 'bg-white/[0.03]'
: 'hover:bg-white/[0.02]'
}`} }`}
onClick={() => toggleExpand(logKey)} onClick={() => toggleExpand(logKey)}
> >
{/* Expand chevron */} {/* Expand chevron */}
<td className="py-2 pr-1"> <td className="py-2 pr-1">
{isExpanded ? ( {isExpanded ? (
<ChevronDown className="h-3.5 w-3.5 text-muted-foreground" /> <ChevronDown className="text-muted-foreground h-3.5 w-3.5" />
) : ( ) : (
<ChevronRight className="h-3.5 w-3.5 text-muted-foreground/50" /> <ChevronRight className="text-muted-foreground/50 h-3.5 w-3.5" />
)} )}
</td> </td>
<td className="text-muted-foreground py-2 pr-4 font-mono text-xs"> <td className="text-muted-foreground py-2 pr-4 font-mono text-xs">
@@ -300,11 +302,9 @@ export const TrafficLogTable: React.FC<TrafficLogTableProps> = ({ logs, isLoadin
)} )}
</td> </td>
<td className="py-2 pr-4 text-xs"> <td className="py-2 pr-4 text-xs">
{log.tokensTotal ? ( {log.tokensTotal
`${formatTokens(log.tokensPrompt)} / ${formatTokens(log.tokensCompletion)}` ? `${formatTokens(log.tokensPrompt)} / ${formatTokens(log.tokensCompletion)}`
) : ( : '—'}
'—'
)}
</td> </td>
<td className="py-2 pr-4 text-xs"> <td className="py-2 pr-4 text-xs">
{log.durationMs != null ? `${log.durationMs}ms` : '—'} {log.durationMs != null ? `${log.durationMs}ms` : '—'}
@@ -323,7 +323,7 @@ export const TrafficLogTable: React.FC<TrafficLogTableProps> = ({ logs, isLoadin
<div className="border-t border-white/[0.06] bg-white/[0.01] px-6 py-4"> <div className="border-t border-white/[0.06] bg-white/[0.01] px-6 py-4">
{/* Preview line */} {/* Preview line */}
{preview && ( {preview && (
<div className="mb-3 rounded-lg bg-white/[0.03] px-3 py-2 text-xs text-muted-foreground italic break-words"> <div className="text-muted-foreground mb-3 rounded-lg bg-white/[0.03] px-3 py-2 text-xs break-words italic">
"{preview}" "{preview}"
</div> </div>
)} )}
@@ -339,18 +339,32 @@ export const TrafficLogTable: React.FC<TrafficLogTableProps> = ({ logs, isLoadin
</Badge> </Badge>
)} )}
{log.tokensPrompt != null && ( {log.tokensPrompt != null && (
<Badge variant="outline" className="border-blue-500/20 text-blue-400 text-[10px]"> <Badge
variant="outline"
className="border-blue-500/20 text-[10px] text-blue-400"
>
Input: {log.tokensPrompt.toLocaleString()} tokens Input: {log.tokensPrompt.toLocaleString()} tokens
</Badge> </Badge>
)} )}
{log.tokensCompletion != null && ( {log.tokensCompletion != null && (
<Badge variant="outline" className="border-emerald-500/20 text-emerald-400 text-[10px]"> <Badge
variant="outline"
className="border-emerald-500/20 text-[10px] text-emerald-400"
>
Output: {log.tokensCompletion.toLocaleString()} tokens Output: {log.tokensCompletion.toLocaleString()} tokens
</Badge> </Badge>
)} )}
{log.tokensTotal != null && log.model && ( {log.tokensTotal != null && log.model && (
<Badge variant="outline" className="border-amber-500/20 text-amber-400 text-[10px]"> <Badge
Cost: ${calculateCost(log.model, log.tokensPrompt || 0, log.tokensCompletion || 0).toFixed(6)} variant="outline"
className="border-amber-500/20 text-[10px] text-amber-400"
>
Cost: $
{calculateCost(
log.model,
log.tokensPrompt || 0,
log.tokensCompletion || 0,
).toFixed(6)}
</Badge> </Badge>
)} )}
{log.durationMs != null && ( {log.durationMs != null && (
@@ -363,11 +377,15 @@ export const TrafficLogTable: React.FC<TrafficLogTableProps> = ({ logs, isLoadin
{/* Body content */} {/* Body content */}
<div className="space-y-2"> <div className="space-y-2">
<div className="flex items-center gap-2"> <div className="flex items-center gap-2">
<span className="text-muted-foreground text-[10px] font-medium uppercase tracking-wider"> <span className="text-muted-foreground text-[10px] font-medium tracking-wider uppercase">
{log.direction === 'inbound' ? 'Request Body' : log.direction === 'outbound' ? 'Response Body' : 'Payload'} {log.direction === 'inbound'
? 'Request Body'
: log.direction === 'outbound'
? 'Response Body'
: 'Payload'}
</span> </span>
</div> </div>
<pre className="custom-scrollbar max-h-[400px] overflow-auto rounded-lg bg-black/30 p-3 font-mono text-[11px] leading-relaxed text-foreground/80 whitespace-pre-wrap break-all"> <pre className="custom-scrollbar text-foreground/80 max-h-[400px] overflow-auto rounded-lg bg-black/30 p-3 font-mono text-[11px] leading-relaxed break-all whitespace-pre-wrap">
{formatBody(log.body)} {formatBody(log.body)}
</pre> </pre>
</div> </div>
+14 -14
View File
@@ -1,9 +1,6 @@
import { logger } from '../../utils/logger'; import { logger } from '../../utils/logger';
import { getServiceOrThrow, getServiceOptional } from './service-registry'; import { getServiceOrThrow, getServiceOptional } from './service-registry';
import type { import type { IdeConfigResult } from '../../server/modules/proxy/proxy-ide-config.service';
IdeConfigResult,
SupportedIde,
} from '../../server/modules/proxy/proxy-ide-config.service';
import { TrafficLogsRepo } from '../database/proxyMetricsHandler'; import { TrafficLogsRepo } from '../database/proxyMetricsHandler';
import { CloudAccountRepo } from '../database/cloudHandler'; import { CloudAccountRepo } from '../database/cloudHandler';
import { ConfigManager } from '../config/manager'; import { ConfigManager } from '../config/manager';
@@ -257,9 +254,7 @@ export async function getRecentRequests(): Promise<{
} }
} }
export async function replayRequest( export async function replayRequest(requestId: string): Promise<{
requestId: string,
): Promise<{
success: boolean; success: boolean;
data?: { original: unknown; newResponse: unknown }; data?: { original: unknown; newResponse: unknown };
error?: string; error?: string;
@@ -458,16 +453,21 @@ export async function generateIdeConfig(
const apiKey = config?.api_key || 'YOUR_API_KEY'; const apiKey = config?.api_key || 'YOUR_API_KEY';
let localIp = '127.0.0.1'; let localIp = '127.0.0.1';
const interfaces = networkInterfaces(); try {
for (const iface of Object.values(interfaces)) { const interfaces = networkInterfaces();
if (iface) { for (const iface of Object.values(interfaces)) {
for (const entry of iface) { if (iface) {
if (entry.family === 'IPv4' && !entry.internal) { for (const entry of iface) {
localIp = entry.address; if (entry.family === 'IPv4' && !entry.internal) {
break; localIp = entry.address;
break;
}
} }
} }
} }
} catch (error) {
// Local proxy configuration remains usable when LAN enumeration is restricted.
logger.warn('Could not enumerate network interfaces; using loopback', error);
} }
// Fallback generator mimicking the proxyIdeConfig.service // Fallback generator mimicking the proxyIdeConfig.service
+276 -274
View File
@@ -406,7 +406,7 @@ function UsagePage() {
</div> </div>
</div> </div>
<Tabs defaultValue="analytics" className="flex-1 flex flex-col"> <Tabs defaultValue="analytics" className="flex flex-1 flex-col">
<div className="px-6 pt-5"> <div className="px-6 pt-5">
<TabsList className="grid w-full max-w-md grid-cols-2"> <TabsList className="grid w-full max-w-md grid-cols-2">
<TabsTrigger value="analytics">Analytics</TabsTrigger> <TabsTrigger value="analytics">Analytics</TabsTrigger>
@@ -415,299 +415,301 @@ function UsagePage() {
</div> </div>
<TabsContent value="analytics" className="flex-1 space-y-4 px-6 py-5"> <TabsContent value="analytics" className="flex-1 space-y-4 px-6 py-5">
{hasQuotaWarning && ( {hasQuotaWarning && (
<Alert <Alert
variant="destructive" variant="destructive"
className="border-amber-500/50 bg-amber-500/10 text-amber-500" className="border-amber-500/50 bg-amber-500/10 text-amber-500"
> >
<AlertTriangle className="h-4 w-4" /> <AlertTriangle className="h-4 w-4" />
<AlertTitle>Quota Warning</AlertTitle> <AlertTitle>Quota Warning</AlertTitle>
<AlertDescription> <AlertDescription>
One or more models have exceeded 80% of their allocated usage limit. One or more models have exceeded 80% of their allocated usage limit.
</AlertDescription> </AlertDescription>
</Alert> </Alert>
)} )}
{/* ─── Stat cards ─── */} {/* ─── Stat cards ─── */}
<TooltipProvider delayDuration={300}> <TooltipProvider delayDuration={300}>
<div className="grid grid-cols-1 gap-3 sm:grid-cols-2 xl:grid-cols-5"> <div className="grid grid-cols-1 gap-3 sm:grid-cols-2 xl:grid-cols-5">
<StatCard
label="Total Tokens"
value={formatNumber(totals.totalTokens)}
icon={Zap}
isLoading={dailyLoading}
accent="amber"
trend={
prevDailyData ? calcTrend(totals.totalTokens, prevTotals.totalTokens) : undefined
}
sparkline={sparklineTotal}
/>
<div className="group relative">
<StatCard <StatCard
label={ label="Total Tokens"
<div className="flex cursor-help items-center gap-1.5"> value={formatNumber(totals.totalTokens)}
Input Tokens icon={Zap}
<Tooltip>
<TooltipTrigger asChild>
<Info className="text-muted-foreground/70 h-3.5 w-3.5" />
</TooltipTrigger>
<TooltipContent>
<p>Tokens sent to the model (prompt, context, files).</p>
</TooltipContent>
</Tooltip>
</div>
}
value={formatNumber(totals.promptTokens)}
icon={MessageSquare}
isLoading={dailyLoading} isLoading={dailyLoading}
accent="blue" accent="amber"
trend={ trend={
prevDailyData prevDailyData ? calcTrend(totals.totalTokens, prevTotals.totalTokens) : undefined
? calcTrend(totals.promptTokens, prevTotals.promptTokens)
: undefined
} }
sparkline={sparklinePrompt} sparkline={sparklineTotal}
/> />
</div> <div className="group relative">
<div className="group relative"> <StatCard
<StatCard label={
label={
<div className="flex cursor-help items-center gap-1.5">
Output Tokens
<Tooltip>
<TooltipTrigger asChild>
<Info className="text-muted-foreground/70 h-3.5 w-3.5" />
</TooltipTrigger>
<TooltipContent>
<p>Tokens generated by the model in response.</p>
</TooltipContent>
</Tooltip>
</div>
}
value={formatNumber(totals.completionTokens)}
icon={Activity}
isLoading={dailyLoading}
accent="green"
trend={
prevDailyData
? calcTrend(totals.completionTokens, prevTotals.completionTokens)
: undefined
}
sparkline={sparklineCompletion}
/>
</div>
<StatCard
label="Total Requests"
value={totals.requests.toLocaleString()}
icon={Layers}
isLoading={dailyLoading}
accent="slate"
trend={prevDailyData ? calcTrend(totals.requests, prevTotals.requests) : undefined}
sparkline={sparklineRequests}
/>
<div className="group relative">
<StatCard
label={
<div className="flex w-full items-center justify-between">
<div className="flex cursor-help items-center gap-1.5"> <div className="flex cursor-help items-center gap-1.5">
Estimated Cost Input Tokens
<Tooltip> <Tooltip>
<TooltipTrigger asChild> <TooltipTrigger asChild>
<Info className="text-muted-foreground/70 h-3.5 w-3.5" /> <Info className="text-muted-foreground/70 h-3.5 w-3.5" />
</TooltipTrigger> </TooltipTrigger>
<TooltipContent> <TooltipContent>
<p>Approximate cost based on configured pricing.</p> <p>Tokens sent to the model (prompt, context, files).</p>
</TooltipContent> </TooltipContent>
</Tooltip> </Tooltip>
</div> </div>
<Button }
variant="ghost" value={formatNumber(totals.promptTokens)}
size="icon" icon={MessageSquare}
className="ml-2 h-5 w-5 hover:bg-white/10" isLoading={dailyLoading}
onClick={() => setIsPricingDialogOpen(true)} accent="blue"
> trend={
<Settings className="text-muted-foreground h-3 w-3" /> prevDailyData
</Button> ? calcTrend(totals.promptTokens, prevTotals.promptTokens)
: undefined
}
sparkline={sparklinePrompt}
/>
</div>
<div className="group relative">
<StatCard
label={
<div className="flex cursor-help items-center gap-1.5">
Output Tokens
<Tooltip>
<TooltipTrigger asChild>
<Info className="text-muted-foreground/70 h-3.5 w-3.5" />
</TooltipTrigger>
<TooltipContent>
<p>Tokens generated by the model in response.</p>
</TooltipContent>
</Tooltip>
</div>
}
value={formatNumber(totals.completionTokens)}
icon={Activity}
isLoading={dailyLoading}
accent="green"
trend={
prevDailyData
? calcTrend(totals.completionTokens, prevTotals.completionTokens)
: undefined
}
sparkline={sparklineCompletion}
/>
</div>
<StatCard
label="Total Requests"
value={totals.requests.toLocaleString()}
icon={Layers}
isLoading={dailyLoading}
accent="slate"
trend={prevDailyData ? calcTrend(totals.requests, prevTotals.requests) : undefined}
sparkline={sparklineRequests}
/>
<div className="group relative">
<StatCard
label={
<div className="flex w-full items-center justify-between">
<div className="flex cursor-help items-center gap-1.5">
Estimated Cost
<Tooltip>
<TooltipTrigger asChild>
<Info className="text-muted-foreground/70 h-3.5 w-3.5" />
</TooltipTrigger>
<TooltipContent>
<p>Approximate cost based on configured pricing.</p>
</TooltipContent>
</Tooltip>
</div>
<Button
variant="ghost"
size="icon"
className="ml-2 h-5 w-5 hover:bg-white/10"
onClick={() => setIsPricingDialogOpen(true)}
>
<Settings className="text-muted-foreground h-3 w-3" />
</Button>
</div>
}
value={`$${totalCost.toFixed(2)}`}
icon={DollarSign}
isLoading={modelLoading}
accent="purple"
/>
</div>
</div>
</TooltipProvider>
{/* ─── Prompt/Completion ratio bar + top models ─── */}
{!dailyLoading && totals.totalTokens > 0 && (
<div className="grid grid-cols-1 gap-3 lg:grid-cols-3">
<div className="bg-card col-span-1 flex flex-col gap-3 rounded-xl border border-white/[0.06] p-4 lg:col-span-2">
<div className="flex items-center justify-between">
<span className="text-muted-foreground text-[11px] font-medium tracking-wider uppercase">
Input / Output
</span>
<span className="text-muted-foreground font-mono text-xs">
{formatNumber(totals.totalTokens)} total
</span>
</div>
<div className="flex h-2.5 w-full overflow-hidden rounded-full bg-white/[0.04]">
<div
className="h-full rounded-l-full transition-all duration-500"
style={{
width: `${promptRatio}%`,
backgroundColor: COLOR_PROMPT,
}}
/>
<div
className="h-full rounded-r-full transition-all duration-500"
style={{
width: `${completionRatio}%`,
backgroundColor: COLOR_COMPLETION,
}}
/>
</div>
<div className="flex items-center justify-between text-xs">
<div className="flex items-center gap-4">
<div className="flex items-center gap-1.5">
<span
className="h-2 w-2 rounded-full"
style={{ backgroundColor: COLOR_PROMPT }}
/>
<span className="text-muted-foreground">Input</span>
<span className="text-foreground font-mono font-semibold">
{promptRatio}%
</span>
</div>
<div className="flex items-center gap-1.5">
<span
className="h-2 w-2 rounded-full"
style={{ backgroundColor: COLOR_COMPLETION }}
/>
<span className="text-muted-foreground">Output</span>
<span className="text-foreground font-mono font-semibold">
{completionRatio}%
</span>
</div>
</div> </div>
} <span className="text-muted-foreground">
value={`$${totalCost.toFixed(2)}`} Avg/req:{' '}
icon={DollarSign} <span className="text-foreground font-mono font-medium">
{formatNumber(avgReqSize)}
</span>
</span>
</div>
</div>
<ForecastCard hourlyData={hourlyData} dailyData={dailyData} range={range} />
</div>
)}
{/* ─── Charts — Area + Model ranking ─── */}
<div className="grid grid-cols-1 gap-3 xl:grid-cols-5">
<div className="xl:col-span-3">
<ChartCard
title={range === '24h' ? 'Hourly Usage' : 'Daily Usage'}
isLoading={areaLoading}
error={areaError}
isEmpty={isAreaEmpty}
onRetry={() => (range === '24h' ? refetchHourly() : refetchDaily())}
emptyTitle="No usage data"
emptyDescription="Data will appear here once proxy requests are recorded."
errorMessage="Failed to load usage data"
retryLabel="Retry"
>
<div className="h-[320px]">
<ResponsiveLine
data={areaChartData}
theme={nivoTheme}
colors={[COLOR_PROMPT, COLOR_COMPLETION]}
margin={{ top: 10, right: 10, bottom: 40, left: 50 }}
xScale={{ type: 'point' }}
yScale={{ type: 'linear', min: 0, stacked: false }}
curve="monotoneX"
enableArea={true}
areaOpacity={0.08}
areaBlendMode="normal"
enablePoints={true}
pointSize={4}
pointColor={{ theme: 'background' }}
pointBorderWidth={2}
pointBorderColor={{ from: 'serieColor' }}
enableGridX={false}
enableGridY={true}
gridYValues={5}
axisBottom={{
tickSize: 0,
tickPadding: 8,
tickRotation: 0,
}}
axisLeft={{
tickSize: 0,
tickPadding: 8,
tickRotation: 0,
format: (v: number) => (v >= 1000 ? `${(v / 1000).toFixed(1)}k` : `${v}`),
}}
tooltip={LineTooltip}
legends={[
{
anchor: 'bottom',
direction: 'row',
justify: false,
translateX: 0,
translateY: 36,
itemsSpacing: 16,
itemWidth: 80,
itemHeight: 16,
itemDirection: 'left-to-right',
symbolSize: 8,
symbolShape: 'circle',
},
]}
animate={true}
motionConfig="gentle"
/>
</div>
</ChartCard>
</div>
<div className="xl:col-span-2">
<ChartCard
title="Model Distribution"
isLoading={modelLoading}
error={modelError}
isEmpty={isModelEmpty}
onRetry={() => refetchModel()}
emptyTitle="No model data"
emptyDescription="Model distribution will appear after proxy usage."
errorMessage="Failed to load model data"
retryLabel="Retry"
>
<div className="custom-scrollbar h-[320px] overflow-y-auto pr-2">
{topModelsData.map((m) => (
<ModelQuotaBar
key={m.model}
modelName={m.model}
usedTokens={m.tokens}
limitTokens={m.limit}
/>
))}
</div>
</ChartCard>
</div>
</div>
{/* ─── Cost Report ─── */}
{!dailyLoading && modelData && modelData.length > 0 && (
<div className="mt-6">
<CostReportCard
models={costReport}
totalInputCost={costReport.reduce((acc, r) => acc + r.inputCost, 0)}
totalOutputCost={costReport.reduce((acc, r) => acc + r.outputCost, 0)}
totalCost={totalCost}
isLoading={modelLoading} isLoading={modelLoading}
accent="purple"
/> />
</div> </div>
</div> )}
</TooltipProvider>
{/* ─── Prompt/Completion ratio bar + top models ─── */} {/* ─── Modals ─── */}
{!dailyLoading && totals.totalTokens > 0 && (
<div className="grid grid-cols-1 gap-3 lg:grid-cols-3">
<div className="bg-card col-span-1 flex flex-col gap-3 rounded-xl border border-white/[0.06] p-4 lg:col-span-2">
<div className="flex items-center justify-between">
<span className="text-muted-foreground text-[11px] font-medium tracking-wider uppercase">
Input / Output
</span>
<span className="text-muted-foreground font-mono text-xs">
{formatNumber(totals.totalTokens)} total
</span>
</div>
<div className="flex h-2.5 w-full overflow-hidden rounded-full bg-white/[0.04]">
<div
className="h-full rounded-l-full transition-all duration-500"
style={{
width: `${promptRatio}%`,
backgroundColor: COLOR_PROMPT,
}}
/>
<div
className="h-full rounded-r-full transition-all duration-500"
style={{
width: `${completionRatio}%`,
backgroundColor: COLOR_COMPLETION,
}}
/>
</div>
<div className="flex items-center justify-between text-xs">
<div className="flex items-center gap-4">
<div className="flex items-center gap-1.5">
<span
className="h-2 w-2 rounded-full"
style={{ backgroundColor: COLOR_PROMPT }}
/>
<span className="text-muted-foreground">Input</span>
<span className="text-foreground font-mono font-semibold">{promptRatio}%</span>
</div>
<div className="flex items-center gap-1.5">
<span
className="h-2 w-2 rounded-full"
style={{ backgroundColor: COLOR_COMPLETION }}
/>
<span className="text-muted-foreground">Output</span>
<span className="text-foreground font-mono font-semibold">
{completionRatio}%
</span>
</div>
</div>
<span className="text-muted-foreground">
Avg/req:{' '}
<span className="text-foreground font-mono font-medium">
{formatNumber(avgReqSize)}
</span>
</span>
</div>
</div>
<ForecastCard hourlyData={hourlyData} dailyData={dailyData} range={range} />
</div>
)}
{/* ─── Charts — Area + Model ranking ─── */}
<div className="grid grid-cols-1 gap-3 xl:grid-cols-5">
<div className="xl:col-span-3">
<ChartCard
title={range === '24h' ? 'Hourly Usage' : 'Daily Usage'}
isLoading={areaLoading}
error={areaError}
isEmpty={isAreaEmpty}
onRetry={() => (range === '24h' ? refetchHourly() : refetchDaily())}
emptyTitle="No usage data"
emptyDescription="Data will appear here once proxy requests are recorded."
errorMessage="Failed to load usage data"
retryLabel="Retry"
>
<div className="h-[320px]">
<ResponsiveLine
data={areaChartData}
theme={nivoTheme}
colors={[COLOR_PROMPT, COLOR_COMPLETION]}
margin={{ top: 10, right: 10, bottom: 40, left: 50 }}
xScale={{ type: 'point' }}
yScale={{ type: 'linear', min: 0, stacked: false }}
curve="monotoneX"
enableArea={true}
areaOpacity={0.08}
areaBlendMode="normal"
enablePoints={true}
pointSize={4}
pointColor={{ theme: 'background' }}
pointBorderWidth={2}
pointBorderColor={{ from: 'serieColor' }}
enableGridX={false}
enableGridY={true}
gridYValues={5}
axisBottom={{
tickSize: 0,
tickPadding: 8,
tickRotation: 0,
}}
axisLeft={{
tickSize: 0,
tickPadding: 8,
tickRotation: 0,
format: (v: number) => (v >= 1000 ? `${(v / 1000).toFixed(1)}k` : `${v}`),
}}
tooltip={LineTooltip}
legends={[
{
anchor: 'bottom',
direction: 'row',
justify: false,
translateX: 0,
translateY: 36,
itemsSpacing: 16,
itemWidth: 80,
itemHeight: 16,
itemDirection: 'left-to-right',
symbolSize: 8,
symbolShape: 'circle',
},
]}
animate={true}
motionConfig="gentle"
/>
</div>
</ChartCard>
</div>
<div className="xl:col-span-2">
<ChartCard
title="Model Distribution"
isLoading={modelLoading}
error={modelError}
isEmpty={isModelEmpty}
onRetry={() => refetchModel()}
emptyTitle="No model data"
emptyDescription="Model distribution will appear after proxy usage."
errorMessage="Failed to load model data"
retryLabel="Retry"
>
<div className="custom-scrollbar h-[320px] overflow-y-auto pr-2">
{topModelsData.map((m) => (
<ModelQuotaBar
key={m.model}
modelName={m.model}
usedTokens={m.tokens}
limitTokens={m.limit}
/>
))}
</div>
</ChartCard>
</div>
</div>
{/* ─── Cost Report ─── */}
{!dailyLoading && modelData && modelData.length > 0 && (
<div className="mt-6">
<CostReportCard
models={costReport}
totalInputCost={costReport.reduce((acc, r) => acc + r.inputCost, 0)}
totalOutputCost={costReport.reduce((acc, r) => acc + r.outputCost, 0)}
totalCost={totalCost}
isLoading={modelLoading}
/>
</div>
)}
{/* ─── Modals ─── */}
<ModelPricingDialog <ModelPricingDialog
open={isPricingDialogOpen} open={isPricingDialogOpen}
onOpenChange={setIsPricingDialogOpen} onOpenChange={setIsPricingDialogOpen}
+9 -11
View File
@@ -44,7 +44,9 @@ function toLowerText(value: string | undefined): string {
} }
function parseDurationToSeconds(text: string): number | null { function parseDurationToSeconds(text: string): number | null {
const match = text.match(/(?:(\d+)h)?(?:(\d+)m)?(?:(\d+(?:\.\d+)?)s)?(?:(\d+(?:\.\d+)?)ms)?/i); const match = text
.trim()
.match(/^(?:(\d+)h)?(?:(\d+)m)?(?:(\d+(?:\.\d+)?)s)?(?:(\d+(?:\.\d+)?)ms)?$/i);
if (!match) { if (!match) {
return null; return null;
} }
@@ -54,9 +56,8 @@ function parseDurationToSeconds(text: string): number | null {
const seconds = Number(match[3] ?? 0); const seconds = Number(match[3] ?? 0);
const milliseconds = Number(match[4] ?? 0); const milliseconds = Number(match[4] ?? 0);
const totalSeconds = const totalSeconds = Math.ceil(hours * 3600 + minutes * 60 + seconds + milliseconds / 1000);
hours * 3600 + minutes * 60 + Math.ceil(seconds) + Math.ceil(milliseconds / 1000); if (!Number.isFinite(totalSeconds) || totalSeconds <= 0) {
if (totalSeconds <= 0) {
return null; return null;
} }
return totalSeconds; return totalSeconds;
@@ -108,19 +109,16 @@ export class RateLimitTracker {
const now = Date.now(); const now = Date.now();
const globalLock = this.lockoutByKey.get(accountId); const globalLock = this.lockoutByKey.get(accountId);
if (globalLock && globalLock.resetTimeMs > now) { let resetTimeMs = globalLock?.resetTimeMs ?? now;
return Math.max(0, Math.ceil((globalLock.resetTimeMs - now) / 1000));
}
if (model) { if (model) {
const modelKey = this.buildLockoutKey(accountId, model); const modelKey = this.buildLockoutKey(accountId, model);
const modelLock = this.lockoutByKey.get(modelKey); const modelLock = this.lockoutByKey.get(modelKey);
if (modelLock && modelLock.resetTimeMs > now) { resetTimeMs = Math.max(resetTimeMs, modelLock?.resetTimeMs ?? now);
return Math.max(0, Math.ceil((modelLock.resetTimeMs - now) / 1000));
}
} }
return 0; // Both scopes apply; retrying before either lock expires still fails upstream.
return Math.max(0, Math.ceil((resetTimeMs - now) / 1000));
} }
getRemainingWaitSec(accountId: string, model?: string): number { getRemainingWaitSec(accountId: string, model?: string): number {
+2 -2
View File
@@ -16,8 +16,8 @@ import { getSwitchGuardSnapshot } from '../../ipc/switchGuard';
vi.mock('../../utils/paths', async () => { vi.mock('../../utils/paths', async () => {
const path = await import('path'); const path = await import('path');
const agentDir = path.join(process.cwd(), 'temp_test_agent'); const agentDir = path.join(process.cwd(), 'temp_test_agent');
return { return {
IS_DEV_ENVIRONMENT: false, IS_DEV_ENVIRONMENT: false,
getAgentDir: vi.fn(() => agentDir), getAgentDir: vi.fn(() => agentDir),
getAccountsFilePath: vi.fn(() => path.join(agentDir, 'accounts.json')), getAccountsFilePath: vi.fn(() => path.join(agentDir, 'accounts.json')),
getBackupsDir: vi.fn(() => path.join(agentDir, 'backups')), getBackupsDir: vi.fn(() => path.join(agentDir, 'backups')),
+1 -1
View File
@@ -57,7 +57,7 @@ vi.mock('../../ipc/database/dbConnection', () => ({
}), }),
})); }));
vi.mock('../../utils/paths', () => ({ vi.mock('../../utils/paths', () => ({
IS_DEV_ENVIRONMENT: false, IS_DEV_ENVIRONMENT: false,
getGeminiNexusDbPaths: () => ['mock-db'], getGeminiNexusDbPaths: () => ['mock-db'],
getCloudAccountsDbPath: () => 'mock-cloud-db', getCloudAccountsDbPath: () => 'mock-cloud-db',
+1 -1
View File
@@ -21,7 +21,7 @@ vi.mock('../../ipc/database/dbConnection', () => ({
}), }),
})); }));
vi.mock('../../utils/paths', () => ({ vi.mock('../../utils/paths', () => ({
IS_DEV_ENVIRONMENT: false, IS_DEV_ENVIRONMENT: false,
getGeminiNexusDbPaths: () => ['mock-db'], getGeminiNexusDbPaths: () => ['mock-db'],
})); }));
+1 -1
View File
@@ -47,7 +47,7 @@ vi.mock('../../utils/logger', () => ({
}, },
})); }));
vi.mock('../../utils/paths', () => ({ vi.mock('../../utils/paths', () => ({
IS_DEV_ENVIRONMENT: false, IS_DEV_ENVIRONMENT: false,
getGeminiNexusDbPaths: vi.fn().mockReturnValue(['/tmp/test.vscdb']), getGeminiNexusDbPaths: vi.fn().mockReturnValue(['/tmp/test.vscdb']),
})); }));
+1 -1
View File
@@ -64,7 +64,7 @@ vi.mock('../../../ipc/database/dbConnection', () => ({
configureDatabase: vi.fn(), configureDatabase: vi.fn(),
})); }));
vi.mock('../../../utils/paths', () => ({ vi.mock('../../../utils/paths', () => ({
IS_DEV_ENVIRONMENT: false, IS_DEV_ENVIRONMENT: false,
getCloudAccountsDbPath: vi.fn(() => '/mock/db/path.sqlite'), getCloudAccountsDbPath: vi.fn(() => '/mock/db/path.sqlite'),
})); }));
+1 -1
View File
@@ -48,7 +48,7 @@ vi.mock('../../../utils/logger', () => ({
}, },
})); }));
vi.mock('../../../utils/paths', () => ({ vi.mock('../../../utils/paths', () => ({
IS_DEV_ENVIRONMENT: false, IS_DEV_ENVIRONMENT: false,
getGeminiNexusDbPaths: vi.fn().mockReturnValue(['/tmp/test.vscdb']), getGeminiNexusDbPaths: vi.fn().mockReturnValue(['/tmp/test.vscdb']),
})); }));
+1 -1
View File
@@ -52,7 +52,7 @@ vi.mock('../../../utils/logger', () => ({
}, },
})); }));
vi.mock('../../../utils/paths', () => ({ vi.mock('../../../utils/paths', () => ({
IS_DEV_ENVIRONMENT: false, IS_DEV_ENVIRONMENT: false,
getGeminiNexusStoragePaths: vi.fn().mockReturnValue(['/tmp/storage.json']), getGeminiNexusStoragePaths: vi.fn().mockReturnValue(['/tmp/storage.json']),
getGeminiNexusDbPaths: vi.fn().mockReturnValue(['/tmp/state.vscdb']), getGeminiNexusDbPaths: vi.fn().mockReturnValue(['/tmp/state.vscdb']),
@@ -1,4 +1,5 @@
import { describe, it, expect, vi, beforeEach } from 'vitest'; import { describe, it, expect, vi, beforeEach } from 'vitest';
import { networkInterfaces } from 'os';
import { import {
getRecentTrafficLogs, getRecentTrafficLogs,
getTrafficLogStats, getTrafficLogStats,
@@ -14,6 +15,16 @@ import {
import { TrafficLogsRepo } from '../../../ipc/database/proxyMetricsHandler'; import { TrafficLogsRepo } from '../../../ipc/database/proxyMetricsHandler';
import { proxyAdvancedRegistry } from '../../../ipc/proxy-advanced/service-registry'; import { proxyAdvancedRegistry } from '../../../ipc/proxy-advanced/service-registry';
vi.mock('os', async (importOriginal) => {
const actual = await importOriginal<typeof import('os')>();
const mockedNetworkInterfaces = vi.fn().mockReturnValue({});
return {
...actual,
default: { ...actual, networkInterfaces: mockedNetworkInterfaces },
networkInterfaces: mockedNetworkInterfaces,
};
});
vi.mock('../../../utils/logger', () => ({ vi.mock('../../../utils/logger', () => ({
logger: { logger: {
info: vi.fn(), info: vi.fn(),
@@ -67,6 +78,7 @@ vi.mock('../../../ipc/proxy-advanced/service-registry', () => {
describe('Proxy Advanced Handler', () => { describe('Proxy Advanced Handler', () => {
beforeEach(() => { beforeEach(() => {
vi.clearAllMocks(); vi.clearAllMocks();
vi.mocked(networkInterfaces).mockReset().mockReturnValue({});
}); });
describe('getRecentTrafficLogs', () => { describe('getRecentTrafficLogs', () => {
@@ -415,9 +427,38 @@ describe('Proxy Advanced Handler', () => {
}); });
describe('generateIdeConfig', () => { describe('generateIdeConfig', () => {
it('uses loopback when network enumeration fails', async () => {
vi.mocked(networkInterfaces).mockImplementationOnce(() => {
throw new Error('network enumeration denied');
});
const result = await generateIdeConfig('vscode');
expect(networkInterfaces).toHaveBeenCalledOnce();
expect(result.success, result.error).toBe(true);
expect(result.data?.content).toMatchObject({ 'openai.url': 'http://127.0.0.1:8045/v1' });
});
it('uses the discovered LAN address when enumeration succeeds', async () => {
vi.mocked(networkInterfaces).mockReturnValueOnce({
eth0: [
{
address: '192.0.2.10',
netmask: '255.255.255.0',
family: 'IPv4',
mac: '00:11:22:33:44:55',
internal: false,
cidr: '192.0.2.10/24',
},
],
});
const result = await generateIdeConfig('vscode');
expect(networkInterfaces).toHaveBeenCalledOnce();
expect(result.success, result.error).toBe(true);
expect(result.data?.content).toMatchObject({ 'openai.url': 'http://192.0.2.10:8045/v1' });
});
it('should generate fallback config when proxyIdeConfig is null', async () => { it('should generate fallback config when proxyIdeConfig is null', async () => {
const result = await generateIdeConfig('vscode'); const result = await generateIdeConfig('vscode');
expect(result.success).toBe(true); expect(result.success, result.error).toBe(true);
expect(result.data?.name).toBe('VS Code'); expect(result.data?.name).toBe('VS Code');
}); });
@@ -460,5 +501,5 @@ describe('Proxy Advanced Handler', () => {
expect(result.data?.proxySetting).toBe('OPENAI_BASE_URL'); expect(result.data?.proxySetting).toBe('OPENAI_BASE_URL');
vi.mocked(proxyAdvancedRegistry).proxyIdeConfig = null; vi.mocked(proxyAdvancedRegistry).proxyIdeConfig = null;
}); });
}); });
}); });
@@ -24,7 +24,7 @@ vi.mock('../../../ipc/database/cloudHandler', () => ({
ensureDatabaseInitialized: vi.fn(), ensureDatabaseInitialized: vi.fn(),
})); }));
vi.mock('../../../utils/paths', () => ({ vi.mock('../../../utils/paths', () => ({
IS_DEV_ENVIRONMENT: false, IS_DEV_ENVIRONMENT: false,
getCloudAccountsDbPath: vi.fn(() => '/mock/db/path.sqlite'), getCloudAccountsDbPath: vi.fn(() => '/mock/db/path.sqlite'),
})); }));
+2 -2
View File
@@ -4,8 +4,8 @@ import path from 'path';
vi.mock('../../utils/paths', async () => { vi.mock('../../utils/paths', async () => {
const path = await import('path'); const path = await import('path');
return { return {
IS_DEV_ENVIRONMENT: false, IS_DEV_ENVIRONMENT: false,
getAgentDir: vi.fn(() => path.join(process.cwd(), 'temp_test_logs')), getAgentDir: vi.fn(() => path.join(process.cwd(), 'temp_test_logs')),
}; };
}); });
+51 -1
View File
@@ -1,7 +1,57 @@
import { describe, expect, it } from 'vitest'; import { describe, expect, it, vi } from 'vitest';
import { RateLimitReason, RateLimitTracker } from '../../server/modules/proxy/rate-limit-tracker'; import { RateLimitReason, RateLimitTracker } from '../../server/modules/proxy/rate-limit-tracker';
describe('RateLimitTracker parity replay', () => { describe('RateLimitTracker parity replay', () => {
it.each([
['500ms', 2],
['2500ms', 3],
['2.2s100ms', 3],
['1h2m3s', 3723],
[' 30s ', 30],
['30seconds', 5],
['invalid30s', 5],
])(
'parses complete retry duration %s without confusing minutes and milliseconds',
(duration, expected) => {
const tracker = new RateLimitTracker();
const info = tracker.parseAndMarkFromError({
accountId: 'acc-duration',
status: 503,
body: JSON.stringify({
error: { details: [{ reason: 'MODEL_CAPACITY_EXHAUSTED', retryDelay: duration }] },
}),
backoffSteps: [60],
});
expect(info?.retryAfterSec).toBe(expected);
},
);
it('waits for the longer of the account and model lockouts', () => {
const now = Date.parse('2026-01-01T00:00:00Z');
const clock = vi.spyOn(Date, 'now').mockReturnValue(now);
try {
const tracker = new RateLimitTracker();
tracker.setLockoutUntilIso(
'acc-overlap',
new Date(now + 5000).toISOString(),
RateLimitReason.RateLimitExceeded,
);
tracker.setLockoutUntilIso(
'acc-overlap',
new Date(now + 60000).toISOString(),
RateLimitReason.QuotaExhausted,
'model-a',
);
expect(tracker.getRemainingWaitSeconds('acc-overlap', 'model-a')).toBe(60);
expect(tracker.getRemainingWaitSeconds('acc-overlap', 'model-b')).toBe(5);
clock.mockReturnValue(now + 10000);
expect(tracker.getRemainingWaitSeconds('acc-overlap', 'model-a')).toBe(50);
expect(tracker.getRemainingWaitSeconds('acc-overlap', 'model-b')).toBe(0);
} finally {
clock.mockRestore();
}
});
it('uses Retry-After header before body/default', () => { it('uses Retry-After header before body/default', () => {
const tracker = new RateLimitTracker(); const tracker = new RateLimitTracker();
const info = tracker.parseAndMarkFromError({ const info = tracker.parseAndMarkFromError({
+2 -2
View File
@@ -104,8 +104,8 @@ vi.mock('electron', () => {
session: { session: {
webRequest: { webRequest: {
onBeforeSendHeaders: vi.fn(), onBeforeSendHeaders: vi.fn(),
} },
} },
}, },
on: vi.fn(), on: vi.fn(),
})), })),
+1 -1
View File
@@ -25,7 +25,7 @@ vi.mock('../../ipc/database/cloudHandler', () => ({
ensureDatabaseInitialized: vi.fn(), ensureDatabaseInitialized: vi.fn(),
})); }));
vi.mock('../../utils/paths', () => ({ vi.mock('../../utils/paths', () => ({
IS_DEV_ENVIRONMENT: false, IS_DEV_ENVIRONMENT: false,
getCloudAccountsDbPath: vi.fn(() => '/mock/db/path.sqlite'), getCloudAccountsDbPath: vi.fn(() => '/mock/db/path.sqlite'),
})); }));