Compare commits
47 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 8dae4e5038 | |||
| b9b28edefe | |||
| 58120f435f | |||
| 027b8e52da | |||
| aad510a9d5 | |||
| 9852a805a1 | |||
| b2cabf0122 | |||
| 521ce15f86 | |||
| fb97c11140 | |||
| 1c5c62e311 | |||
| 77148f7f97 | |||
| a329d2f2bc | |||
| 39e9e4446b | |||
| b32de54944 | |||
| 071b874e1b | |||
| 9ba65d3323 | |||
| 890a851bbf | |||
| 5f6ca23da4 | |||
| 58df1c06ee | |||
| 95f8599dc2 | |||
| 8a11242d7f | |||
| 948513ef5f | |||
| c497a35d21 | |||
| e0a539bc64 | |||
| 44b8395ead | |||
| 1bc8878490 | |||
| ded2ac493d | |||
| 57b3319ac0 | |||
| eba7ba25b8 | |||
| df774892c8 | |||
| f3b4ce6b67 | |||
| bb8545b3e1 | |||
| 600149fc2b | |||
| f4de3c8748 | |||
| 35538e6f77 | |||
| ea924f3bbf | |||
| 7bc15a2fc9 | |||
| 2bf7db92ee | |||
| 95260f56ba | |||
| c5ace0376a | |||
| 7ee09388fa | |||
| a15b0ef060 | |||
| 57cfd9a315 | |||
| 5fb4149c32 | |||
| 03d97ba617 | |||
| 5205f5f4b4 | |||
| 6eda0f4d00 |
@@ -0,0 +1,39 @@
|
||||
---
|
||||
description: Deploy the latest OmniRoute code to the Akamai VPS (69.164.221.35)
|
||||
---
|
||||
|
||||
# Deploy to Akamai VPS Workflow
|
||||
|
||||
Deploy OmniRoute to the Akamai VPS using `npm pack + scp` + PM2.
|
||||
|
||||
**Akamai VPS:** `69.164.221.35`
|
||||
**Process manager:** PM2 (`omniroute`)
|
||||
**Port:** `20128`
|
||||
|
||||
## Steps
|
||||
|
||||
### 1. Build + pack locally
|
||||
|
||||
// turbo
|
||||
|
||||
```bash
|
||||
cd /home/diegosouzapw/dev/proxys/9router && npm run build:cli && npm pack --ignore-scripts
|
||||
```
|
||||
|
||||
### 2. Copy to Akamai VPS and install
|
||||
|
||||
// turbo-all
|
||||
|
||||
```bash
|
||||
scp omniroute-*.tgz root@69.164.221.35:/tmp/
|
||||
```
|
||||
|
||||
```bash
|
||||
ssh root@69.164.221.35 "npm install -g /tmp/omniroute-*.tgz --ignore-scripts && cd /usr/lib/node_modules/omniroute/app && npm rebuild better-sqlite3 && pm2 delete omniroute 2>/dev/null; pm2 start /root/.omniroute/ecosystem.config.cjs --update-env && pm2 save && echo '✅ Akamai done'"
|
||||
```
|
||||
|
||||
### 3. Verify the deployment
|
||||
|
||||
```bash
|
||||
curl -s -o /dev/null -w 'AKAMAI HTTP %{http_code}\n' http://69.164.221.35:20128/
|
||||
```
|
||||
@@ -0,0 +1,49 @@
|
||||
---
|
||||
description: Deploy the latest OmniRoute code to BOTH the Akamai VPS and the Local VPS
|
||||
---
|
||||
|
||||
# Deploy to VPS (Both) Workflow
|
||||
|
||||
Deploy OmniRoute to the production VPSs using `npm pack + scp` + PM2.
|
||||
|
||||
**Akamai VPS:** `69.164.221.35`
|
||||
**Local VPS:** `192.168.0.15`
|
||||
**Process manager:** PM2 (`omniroute`)
|
||||
**Port:** `20128`
|
||||
**PM2 entry:** `/usr/lib/node_modules/omniroute/app/server.js`
|
||||
|
||||
> [!IMPORTANT]
|
||||
> The npm registry rejects packages > 100MB, so deployment uses **npm pack + scp**.
|
||||
|
||||
## Steps
|
||||
|
||||
### 1. Build + pack locally
|
||||
|
||||
// turbo
|
||||
|
||||
```bash
|
||||
cd /home/diegosouzapw/dev/proxys/9router && npm run build:cli && npm pack --ignore-scripts
|
||||
```
|
||||
|
||||
### 2. Copy to both VPS and install
|
||||
|
||||
// turbo-all
|
||||
|
||||
```bash
|
||||
scp omniroute-*.tgz root@69.164.221.35:/tmp/ && scp omniroute-*.tgz root@192.168.0.15:/tmp/
|
||||
```
|
||||
|
||||
```bash
|
||||
ssh root@69.164.221.35 "npm install -g /tmp/omniroute-*.tgz --ignore-scripts && cd /usr/lib/node_modules/omniroute/app && npm rebuild better-sqlite3 && pm2 delete omniroute 2>/dev/null; pm2 start /root/.omniroute/ecosystem.config.cjs --update-env && pm2 save && echo '✅ Akamai done'"
|
||||
```
|
||||
|
||||
```bash
|
||||
ssh root@192.168.0.15 "npm install -g /tmp/omniroute-*.tgz --ignore-scripts && cd /usr/lib/node_modules/omniroute/app && npm rebuild better-sqlite3 && pm2 delete omniroute 2>/dev/null; pm2 start /root/.omniroute/ecosystem.config.cjs --update-env && pm2 save && echo '✅ Local done'"
|
||||
```
|
||||
|
||||
### 3. Verify the deployment
|
||||
|
||||
```bash
|
||||
curl -s -o /dev/null -w 'AKAMAI HTTP %{http_code}\n' http://69.164.221.35:20128/
|
||||
curl -s -o /dev/null -w 'LOCAL HTTP %{http_code}\n' http://192.168.0.15:20128/
|
||||
```
|
||||
@@ -0,0 +1,39 @@
|
||||
---
|
||||
description: Deploy the latest OmniRoute code to the Local VPS (192.168.0.15)
|
||||
---
|
||||
|
||||
# Deploy to Local VPS Workflow
|
||||
|
||||
Deploy OmniRoute to the Local VPS using `npm pack + scp` + PM2.
|
||||
|
||||
**Local VPS:** `192.168.0.15`
|
||||
**Process manager:** PM2 (`omniroute`)
|
||||
**Port:** `20128`
|
||||
|
||||
## Steps
|
||||
|
||||
### 1. Build + pack locally
|
||||
|
||||
// turbo
|
||||
|
||||
```bash
|
||||
cd /home/diegosouzapw/dev/proxys/9router && npm run build:cli && npm pack --ignore-scripts
|
||||
```
|
||||
|
||||
### 2. Copy to Local VPS and install
|
||||
|
||||
// turbo-all
|
||||
|
||||
```bash
|
||||
scp omniroute-*.tgz root@192.168.0.15:/tmp/
|
||||
```
|
||||
|
||||
```bash
|
||||
ssh root@192.168.0.15 "npm install -g /tmp/omniroute-*.tgz --ignore-scripts && cd /usr/lib/node_modules/omniroute/app && npm rebuild better-sqlite3 && pm2 delete omniroute 2>/dev/null; pm2 start /root/.omniroute/ecosystem.config.cjs --update-env && pm2 save && echo '✅ Local done'"
|
||||
```
|
||||
|
||||
### 3. Verify the deployment
|
||||
|
||||
```bash
|
||||
curl -s -o /dev/null -w 'LOCAL HTTP %{http_code}\n' http://192.168.0.15:20128/
|
||||
```
|
||||
@@ -1,102 +0,0 @@
|
||||
---
|
||||
description: Deploy the latest OmniRoute code to the Akamai VPS (69.164.221.35) via npm
|
||||
---
|
||||
|
||||
# Deploy to VPS Workflow
|
||||
|
||||
Deploy OmniRoute to the production VPS using `npm pack + scp` + PM2.
|
||||
|
||||
**VPS:** `69.164.221.35` (Akamai, Ubuntu 24.04, 1GB RAM + 2.5GB swap)
|
||||
**Local VPS:** `192.168.0.15` (same setup)
|
||||
**Process manager:** PM2 (`omniroute`)
|
||||
**Port:** `20128`
|
||||
**PM2 entry:** `/usr/lib/node_modules/omniroute/app/server.js`
|
||||
|
||||
> [!IMPORTANT]
|
||||
> PM2 runs from the global npm package at `/usr/lib/node_modules/omniroute`.
|
||||
> The Next.js standalone build is at `app/server.js` inside that directory.
|
||||
> The npm registry rejects packages > 100MB, so deployment uses **npm pack + scp**.
|
||||
|
||||
> [!CAUTION]
|
||||
> **NEVER** use `pm2 restart omniroute` after `npm install -g`. This drops env vars.
|
||||
> Always use `pm2 delete omniroute && pm2 start <ecosystem.config.cjs> --update-env`.
|
||||
> After `npm install -g`, always rebuild better-sqlite3: `cd .../app && npm rebuild better-sqlite3`
|
||||
|
||||
## Steps
|
||||
|
||||
### 1. Build + pack locally
|
||||
|
||||
Run the full build (includes hash-strip patch) and create the .tgz:
|
||||
|
||||
// turbo
|
||||
|
||||
```bash
|
||||
cd /home/diegosouzapw/dev/proxys/9router && npm run build:cli && npm pack --ignore-scripts
|
||||
```
|
||||
|
||||
### 2. Copy to both VPS and install
|
||||
|
||||
// turbo-all
|
||||
|
||||
```bash
|
||||
scp omniroute-*.tgz root@69.164.221.35:/tmp/ && scp omniroute-*.tgz root@192.168.0.15:/tmp/
|
||||
```
|
||||
|
||||
```bash
|
||||
ssh root@69.164.221.35 "npm install -g /tmp/omniroute-*.tgz --ignore-scripts && cd /usr/lib/node_modules/omniroute/app && npm rebuild better-sqlite3 && pm2 delete omniroute 2>/dev/null; pm2 start /root/.omniroute/ecosystem.config.cjs --update-env && pm2 save && echo '✅ Akamai done'"
|
||||
```
|
||||
|
||||
```bash
|
||||
ssh root@192.168.0.15 "npm install -g /tmp/omniroute-*.tgz --ignore-scripts && cd /usr/lib/node_modules/omniroute/app && npm rebuild better-sqlite3 && pm2 delete omniroute 2>/dev/null; pm2 start /root/.omniroute/ecosystem.config.cjs --update-env && pm2 save && echo '✅ Local done'"
|
||||
```
|
||||
|
||||
### 3. Verify the deployment
|
||||
|
||||
```bash
|
||||
ssh root@69.164.221.35 "pm2 list && cat \$(npm root -g)/omniroute/app/package.json | grep version | head -1 && curl -s -o /dev/null -w 'HTTP %{http_code}' http://localhost:20128/"
|
||||
```
|
||||
|
||||
```bash
|
||||
ssh root@192.168.0.15 "pm2 list && cat \$(npm root -g)/omniroute/app/package.json | grep version | head -1 && curl -s -X POST http://localhost:20128/api/auth/login -H 'Content-Type: application/json' -d '{\"password\":\"123456\"}'"
|
||||
```
|
||||
|
||||
Expected: PM2 shows `online`, version matches, login returns `{"success":true}`.
|
||||
|
||||
## How it works
|
||||
|
||||
1. `npm run build:cli` builds Next.js standalone → `app/` and strips Turbopack hashed require() calls from chunks
|
||||
2. `npm pack --ignore-scripts` packages without re-running the build
|
||||
3. `scp` transfers the .tgz to each VPS (~286MB)
|
||||
4. `npm install -g /tmp/omniroute-*.tgz --ignore-scripts` installs pre-built package
|
||||
5. `npm rebuild better-sqlite3` recompiles native bindings for the VPS Node.js version
|
||||
6. `pm2 delete` + `pm2 start ecosystem.config.cjs --update-env` restarts with env vars
|
||||
7. `pm2 save` persists the process list for reboot survival
|
||||
|
||||
## Ecosystem Config
|
||||
|
||||
Both VPSs have `ecosystem.config.cjs` at `/root/.omniroute/ecosystem.config.cjs`.
|
||||
This file defines env vars (PORT, DATA_DIR, INITIAL_PASSWORD, OAuth secrets, etc.)
|
||||
that `pm2 restart` does NOT inject — only `pm2 start --update-env` does.
|
||||
|
||||
## PM2 Setup (one-time — if reconfiguring from scratch)
|
||||
|
||||
```bash
|
||||
ssh root@<VPS> "
|
||||
pm2 delete omniroute 2>/dev/null;
|
||||
cd /usr/lib/node_modules/omniroute/app && npm rebuild better-sqlite3 &&
|
||||
pm2 start /root/.omniroute/ecosystem.config.cjs --update-env &&
|
||||
pm2 save && pm2 startup
|
||||
"
|
||||
```
|
||||
|
||||
> [!NOTE]
|
||||
> Ensure `/root/.omniroute/ecosystem.config.cjs` exists with all required env vars.
|
||||
> For fresh installs, copy from the existing VPS or create from the template in `.env`.
|
||||
|
||||
## Notes
|
||||
|
||||
- Env vars are in `/root/.omniroute/ecosystem.config.cjs` (NOT `.env` in app dir)
|
||||
- PM2 is configured with `pm2 startup` to auto-restart on reboot
|
||||
- Nginx proxies `omniroute.online` → `localhost:20128`
|
||||
- The VPS has only 1GB RAM — builds happen locally, never on the VPS
|
||||
- After `npm install -g`, `better-sqlite3` MUST be rebuilt in the `app/` subdir
|
||||
@@ -4,6 +4,82 @@
|
||||
|
||||
---
|
||||
|
||||
## [3.0.7] — 2026-03-25
|
||||
|
||||
### 🐛 Bug Fixes
|
||||
|
||||
- **Antigravity Token Refresh:** Fixed `client_secret is missing` error for npm-installed users — the `clientSecretDefault` was empty in providerRegistry, causing Google to reject token refresh requests (#588)
|
||||
- **OpenCode Zen Models:** Added `modelsUrl` to the OpenCode Zen registry entry so "Import from /models" works correctly (#612)
|
||||
- **Streaming Artifacts:** Fixed excessive newlines left in responses after thinking-tag signature stripping (#626)
|
||||
- **Proxy Fallback:** Added automatic retry without proxy when SOCKS5 relay fails
|
||||
- **Proxy Test:** Test endpoint now resolves real credentials from DB via proxyId
|
||||
|
||||
### ✨ New Features
|
||||
|
||||
- **Playground Account/Key Selector:** Persistent, always-visible dropdown to select specific provider accounts/keys for testing — fetches all connections at startup and filters by selected provider
|
||||
- **CLI Tools Dynamic Models:** Model selection now dynamically fetches from `/v1/models` API — providers like Kiro now show their full model catalog
|
||||
- **Antigravity Model List:** Updated with Claude Sonnet 4.5, Claude Sonnet 4, GPT 5, GPT 5 Mini; enabled `passthroughModels` for dynamic model access (#628)
|
||||
|
||||
### 🔧 Maintenance
|
||||
|
||||
- Merged PR #625 — Provider Limits light mode background fix
|
||||
|
||||
---
|
||||
|
||||
## [3.0.6] — 2026-03-25
|
||||
|
||||
### 🐛 Bug Fixes
|
||||
|
||||
- **Limits/Proxy:** Fixed Codex limit fetching for accounts behind SOCKS5 proxies — token refresh now runs inside proxy context
|
||||
- **CI:** Fixed integration test `v1/models` assertion failure in CI environments without provider connections
|
||||
- **Settings:** Proxy test button now shows success/failure results immediately (previously hidden behind health data)
|
||||
|
||||
### ✨ New Features
|
||||
|
||||
- **Playground:** Added Account selector dropdown — test specific connections individually when a provider has multiple accounts
|
||||
|
||||
### 🔧 Maintenance
|
||||
|
||||
- Merged PR #623 — LongCat API base URL path correction
|
||||
|
||||
---
|
||||
|
||||
## [3.0.5] — 2026-03-25
|
||||
|
||||
### ✨ New Features
|
||||
|
||||
- **Limits UI:** Added tag grouping feature to the connections dashboard to improve visual organization for accounts with custom tags.
|
||||
|
||||
---
|
||||
|
||||
## [3.0.4] — 2026-03-25
|
||||
|
||||
### 🐛 Bug Fixes
|
||||
|
||||
- **Streaming:** Fixed `TextDecoder` state corruption inside combo `sanitize` TransformStream which caused SSE garbled output matching multibyte characters (PR #614)
|
||||
- **Providers UI:** Safely render HTML tags inside provider connection error tooltips using `dangerouslySetInnerHTML`
|
||||
- **Proxy Settings:** Added missing `username` and `password` payload body properties allowing authenticated proxies to be successfully verified from the Dashboard.
|
||||
- **Provider API:** Bound soft exception returns to `getCodexUsage` preventing API HTTP 500 failures when token fetch fails
|
||||
|
||||
---
|
||||
|
||||
## [3.0.3] — 2026-03-25
|
||||
|
||||
### ✨ New Features
|
||||
|
||||
- **Auto-Sync Models:** Added a UI toggle and `sync-models` endpoint to automatically synchronise model lists per provider using a scheduled interval scheduler (PR #597)
|
||||
|
||||
### 🐛 Bug Fixes
|
||||
|
||||
- **Timeouts:** Elevated default proxies `FETCH_TIMEOUT_MS` and `STREAM_IDLE_TIMEOUT_MS` to 10 minutes to properly support deep reasoning models (like o1) without aborting requests (Fixes #609)
|
||||
- **CLI Tool Detection:** Improved cross-platform detection handling NVM paths, Windows `PATHEXT` (preventing `.cmd` wrappers issue), and custom NPM prefixes (PR #598)
|
||||
- **Streaming Logs:** Implemented `tool_calls` delta accumulation in streaming response logs so function calls are tracked and persisted accurately in DB (PR #603)
|
||||
- **Model Catalog:** Removed auth exemption, properly hiding `comfyui` and `sdwebui` models when no provider is explicitly configured (PR #599)
|
||||
|
||||
### 🌐 Translations
|
||||
|
||||
- **cs:** Improved Czech translation strings across the app (PR #601)
|
||||
|
||||
## [3.0.2] — 2026-03-25
|
||||
|
||||
### 🚀 Enhancements & Features
|
||||
|
||||
@@ -32,12 +32,12 @@ _Your universal API proxy — one endpoint, 67+ providers, zero downtime. Now wi
|
||||
|
||||
| Area | Change |
|
||||
| ---------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| 🔒 **CodeQL Security** | Fixed 10+ CodeQL alerts: polynomial-redos, insecure-randomness, shell-injection remediation |
|
||||
| ✅ **Route Validation** | All 176 API routes now validated with Zod schemas + `validateBody()` — CI `check:route-validation:t06` passes |
|
||||
| 🐛 **omniModel Tag Leak** | Internal `<omniModel>` tags no longer leak to clients in SSE streaming responses (#585) |
|
||||
| 🔒 **CodeQL Security** | Fixed 10+ CodeQL alerts: polynomial-redos, insecure-randomness, shell-injection remediation |
|
||||
| ✅ **Route Validation** | All 176 API routes now validated with Zod schemas + `validateBody()` — CI `check:route-validation:t06` passes |
|
||||
| 🐛 **omniModel Tag Leak** | Internal `<omniModel>` tags no longer leak to clients in SSE streaming responses (#585) |
|
||||
| 🔑 **Registered Keys API** | Auto-provision API keys via `POST /api/v1/registered-keys` with per-provider/account quota enforcement, idempotency, SHA-256 storage, and optional GitHub issue reporting |
|
||||
| 🎨 **Provider Icons** | 130+ provider logos via `@lobehub/icons` (SVG) with PNG → generic fallback chain |
|
||||
| 🔄 **Model Auto-Sync** | 24h scheduler refreshes model lists for 16 providers on startup — configurable via `MODEL_SYNC_INTERVAL_HOURS` |
|
||||
| 🔄 **Model Auto-Sync** | 24h scheduler and manual UI toggle to sync model lists for built-in and custom OpenAI-compatible providers |
|
||||
| 🌐 **OpenCode Zen/Go** | Two new providers from @kang-heewon via PR #530: free tier + subscription tier via `OpencodeExecutor` |
|
||||
| 🐛 **Gemini CLI OAuth** | Actionable error when `GEMINI_OAUTH_CLIENT_SECRET` is missing in Docker (was cryptic Google error) |
|
||||
| 🐛 **OpenCode config** | `saveOpenCodeConfig()` now correctly writes TOML to `XDG_CONFIG_HOME` |
|
||||
|
||||
+1
-1
@@ -1,7 +1,7 @@
|
||||
openapi: 3.1.0
|
||||
info:
|
||||
title: OmniRoute API
|
||||
version: 3.0.2
|
||||
version: 3.0.7
|
||||
description: |
|
||||
OmniRoute is a local-first AI API proxy router. It provides an OpenAI-compatible
|
||||
endpoint that routes requests to multiple AI providers with load balancing,
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
import { loadProviderCredentials } from "./credentialLoader.ts";
|
||||
|
||||
// Timeout for non-streaming fetch requests (ms). Prevents stalled connections.
|
||||
export const FETCH_TIMEOUT_MS = parseInt(process.env.FETCH_TIMEOUT_MS || "120000", 10);
|
||||
export const FETCH_TIMEOUT_MS = parseInt(process.env.FETCH_TIMEOUT_MS || "600000", 10);
|
||||
|
||||
// Idle timeout for SSE streams (ms). Closes stream if no data for this duration.
|
||||
// Default: 120s balances deep-reasoning pauses with fast zombie stream detection (#473).
|
||||
// Extended-thinking models rarely pause >90s between chunks. Override with STREAM_IDLE_TIMEOUT_MS env var.
|
||||
export const STREAM_IDLE_TIMEOUT_MS = parseInt(process.env.STREAM_IDLE_TIMEOUT_MS || "120000", 10);
|
||||
export const STREAM_IDLE_TIMEOUT_MS = parseInt(process.env.STREAM_IDLE_TIMEOUT_MS || "600000", 10);
|
||||
|
||||
// Provider configurations
|
||||
// OAuth credentials read from env vars with hardcoded fallbacks for backward compatibility.
|
||||
|
||||
@@ -386,16 +386,21 @@ export const REGISTRY: Record<string, RegistryEntry> = {
|
||||
clientIdEnv: "ANTIGRAVITY_OAUTH_CLIENT_ID",
|
||||
clientIdDefault: "1071006060591-tmhssin2h21lcre235vtolojh4g403ep.apps.googleusercontent.com",
|
||||
clientSecretEnv: "ANTIGRAVITY_OAUTH_CLIENT_SECRET",
|
||||
clientSecretDefault: "",
|
||||
clientSecretDefault: "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",
|
||||
},
|
||||
models: [
|
||||
{ id: "claude-opus-4-6-thinking", name: "Claude Opus 4.6 Thinking" },
|
||||
{ id: "claude-sonnet-4-6", name: "Claude Sonnet 4.6" },
|
||||
{ id: "claude-sonnet-4-5", name: "Claude Sonnet 4.5" },
|
||||
{ id: "claude-sonnet-4", name: "Claude Sonnet 4" },
|
||||
{ id: "gemini-2.5-pro", name: "Gemini 2.5 Pro" },
|
||||
{ id: "gemini-2.5-flash", name: "Gemini 2.5 Flash" },
|
||||
{ id: "gemini-2.0-flash", name: "Gemini 2.0 Flash" },
|
||||
{ id: "gpt-oss-120b-medium", name: "GPT OSS 120B Medium" },
|
||||
{ id: "gpt-5", name: "GPT 5" },
|
||||
{ id: "gpt-5-mini", name: "GPT 5 Mini" },
|
||||
],
|
||||
passthroughModels: true,
|
||||
},
|
||||
|
||||
github: {
|
||||
@@ -576,6 +581,7 @@ export const REGISTRY: Record<string, RegistryEntry> = {
|
||||
format: "openai",
|
||||
executor: "opencode",
|
||||
baseUrl: "https://opencode.ai/zen/v1",
|
||||
modelsUrl: "https://opencode.ai/zen/v1/models",
|
||||
authType: "apikey",
|
||||
authHeader: "Authorization",
|
||||
authPrefix: "Bearer",
|
||||
@@ -1275,10 +1281,7 @@ export const REGISTRY: Record<string, RegistryEntry> = {
|
||||
alias: "lc",
|
||||
format: "openai",
|
||||
executor: "default",
|
||||
// (#536) Correct OpenAI-compatible base URL — was longcat.chat/api/v1/chat/completions
|
||||
// which is the chat endpoint directly, not the base. Key validation and routing must
|
||||
// use https://api.longcat.chat/openai which resolves /v1/models and /v1/chat/completions
|
||||
baseUrl: "https://api.longcat.chat/openai",
|
||||
baseUrl: "https://api.longcat.chat/openai/v1/chat/completions",
|
||||
authType: "apikey",
|
||||
authHeader: "Authorization",
|
||||
authPrefix: "Bearer",
|
||||
|
||||
+24
-10
@@ -526,18 +526,32 @@ export async function handleComboChat({
|
||||
// visible content so they don't leak to the user. The tag is still
|
||||
// present in the full response for round-trip context pinning, but
|
||||
// we clean it from each SSE chunk's content field before delivery.
|
||||
//
|
||||
// IMPORTANT: Use a SEPARATE TextDecoder from the transform stream above.
|
||||
// The transform stream's decoder accumulates UTF-8 state; reusing it here
|
||||
// would corrupt multi-byte characters split across chunk boundaries.
|
||||
const sanitizeDecoder = new TextDecoder();
|
||||
const sanitize = new TransformStream({
|
||||
transform(chunk, controller) {
|
||||
const text = decoder.decode(chunk, { stream: true });
|
||||
// Only run replacement if the chunk actually contains the tag
|
||||
if (text.includes("<omniModel>")) {
|
||||
const cleaned = text.replace(
|
||||
/(?:\\\\n|\\n)?<omniModel>[^<]+<\/omniModel>(?:\\\\n|\\n)?/g,
|
||||
""
|
||||
);
|
||||
controller.enqueue(encoder.encode(cleaned));
|
||||
} else {
|
||||
controller.enqueue(chunk);
|
||||
const text = sanitizeDecoder.decode(chunk, { stream: true });
|
||||
if (text) {
|
||||
if (text.includes("<omniModel>")) {
|
||||
const cleaned = text.replace(/\n?<omniModel>[^<]+<\/omniModel>\n?/g, "");
|
||||
if (cleaned) controller.enqueue(encoder.encode(cleaned));
|
||||
} else {
|
||||
controller.enqueue(encoder.encode(text));
|
||||
}
|
||||
}
|
||||
},
|
||||
flush(controller) {
|
||||
const tail = sanitizeDecoder.decode();
|
||||
if (tail) {
|
||||
if (tail.includes("<omniModel>")) {
|
||||
const cleaned = tail.replace(/\n?<omniModel>[^<]+<\/omniModel>\n?/g, "");
|
||||
if (cleaned) controller.enqueue(encoder.encode(cleaned));
|
||||
} else {
|
||||
controller.enqueue(encoder.encode(tail));
|
||||
}
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
@@ -9,7 +9,7 @@ const DEFAULT_COMBO_CONFIG = {
|
||||
strategy: "priority",
|
||||
maxRetries: 1,
|
||||
retryDelayMs: 2000,
|
||||
timeoutMs: 120000,
|
||||
timeoutMs: 600000,
|
||||
concurrencyPerModel: 3, // max simultaneous requests per model (round-robin)
|
||||
queueTimeoutMs: 30000, // max wait time in semaphore queue (round-robin)
|
||||
healthCheckEnabled: true,
|
||||
|
||||
@@ -132,6 +132,11 @@ export function detectAndLearn(
|
||||
}
|
||||
}
|
||||
|
||||
// Collapse excessive consecutive newlines left after tag removal (fixes #626)
|
||||
if (found.length > 0) {
|
||||
cleaned = cleaned.replace(/\n{3,}/g, "\n\n");
|
||||
}
|
||||
|
||||
return { found, cleaned: cleaned.trim() || cleaned };
|
||||
}
|
||||
|
||||
|
||||
@@ -856,7 +856,7 @@ async function getCodexUsage(accessToken, providerSpecificData: Record<string, u
|
||||
quotas,
|
||||
};
|
||||
} catch (error) {
|
||||
throw new Error(`Failed to fetch Codex usage: ${error.message}`);
|
||||
return { message: `Failed to fetch Codex usage: ${(error as Error).message}` };
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -57,6 +57,13 @@ type TranslateState = ReturnType<typeof initState> & {
|
||||
accumulatedContent?: string;
|
||||
};
|
||||
|
||||
type ToolCall = {
|
||||
id: string | null;
|
||||
index: number;
|
||||
type: string;
|
||||
function: { name: string; arguments: string };
|
||||
};
|
||||
|
||||
type UsageTokenRecord = Record<string, number>;
|
||||
|
||||
function getOpenAIIntermediateChunks(value: unknown): unknown[] {
|
||||
@@ -113,6 +120,9 @@ export function createSSEStream(options: StreamOptions = {}) {
|
||||
let usage: UsageTokenRecord | null = null;
|
||||
/** Passthrough (OpenAI CC shape): saw tool_calls in stream before finish_reason */
|
||||
let passthroughHasToolCalls = false;
|
||||
/** Passthrough: accumulate tool_calls deltas for call log responseBody */
|
||||
const passthroughToolCalls = new Map<string, ToolCall>();
|
||||
let passthroughToolCallSeq = 0;
|
||||
|
||||
// State for translate mode (accumulatedContent for call log response body)
|
||||
const state: TranslateState | null =
|
||||
@@ -268,9 +278,39 @@ export function createSSEStream(options: StreamOptions = {}) {
|
||||
}
|
||||
}
|
||||
|
||||
// T18: Track if we saw tool calls
|
||||
// T18: Track if we saw tool calls & accumulate for call log
|
||||
if (delta?.tool_calls && delta.tool_calls.length > 0) {
|
||||
passthroughHasToolCalls = true;
|
||||
for (const tc of delta.tool_calls) {
|
||||
// Key by index first — id only appears on the first delta in OpenAI streaming
|
||||
let key: string;
|
||||
if (Number.isInteger(tc?.index)) {
|
||||
key = `idx:${tc.index}`;
|
||||
} else if (tc?.id) {
|
||||
key = `id:${tc.id}`;
|
||||
} else {
|
||||
key = `seq:${++passthroughToolCallSeq}`;
|
||||
}
|
||||
const existing = passthroughToolCalls.get(key);
|
||||
const deltaArgs =
|
||||
typeof tc?.function?.arguments === "string" ? tc.function.arguments : "";
|
||||
if (!existing) {
|
||||
passthroughToolCalls.set(key, {
|
||||
id: tc?.id ?? null,
|
||||
index: Number.isInteger(tc?.index) ? tc.index : passthroughToolCalls.size,
|
||||
type: tc?.type || "function",
|
||||
function: {
|
||||
name: tc?.function?.name || "",
|
||||
arguments: deltaArgs,
|
||||
},
|
||||
});
|
||||
} else {
|
||||
if (tc?.id) existing.id = existing.id || tc.id;
|
||||
if (tc?.function?.name && !existing.function.name)
|
||||
existing.function.name = tc.function.name;
|
||||
existing.function.arguments += deltaArgs;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const content = delta?.content || delta?.reasoning_content;
|
||||
@@ -516,13 +556,20 @@ export function createSSEStream(options: StreamOptions = {}) {
|
||||
const prompt = Number(u?.prompt_tokens ?? u?.input_tokens ?? 0);
|
||||
const completion = Number(u?.completion_tokens ?? u?.output_tokens ?? 0);
|
||||
const content = passthroughAccumulatedContent.trim() || "";
|
||||
const message: Record<string, unknown> = {
|
||||
role: "assistant",
|
||||
content: content || null,
|
||||
};
|
||||
if (passthroughToolCalls.size > 0) {
|
||||
message.tool_calls = [...passthroughToolCalls.values()].sort(
|
||||
(a, b) => a.index - b.index
|
||||
);
|
||||
}
|
||||
const responseBody = {
|
||||
choices: [
|
||||
{
|
||||
message: {
|
||||
role: "assistant",
|
||||
content,
|
||||
},
|
||||
message,
|
||||
finish_reason: passthroughHasToolCalls ? "tool_calls" : "stop",
|
||||
},
|
||||
],
|
||||
usage: {
|
||||
@@ -643,13 +690,32 @@ export function createSSEStream(options: StreamOptions = {}) {
|
||||
const prompt = Number(u?.prompt_tokens ?? u?.input_tokens ?? 0);
|
||||
const completion = Number(u?.completion_tokens ?? u?.output_tokens ?? 0);
|
||||
const content = (state?.accumulatedContent ?? "").trim() || "";
|
||||
const message: Record<string, unknown> = {
|
||||
role: "assistant",
|
||||
content: content || null,
|
||||
};
|
||||
const hasToolCalls = state?.toolCalls?.size > 0;
|
||||
if (hasToolCalls) {
|
||||
// Normalize shape — translators may store different structures
|
||||
message.tool_calls = [...state.toolCalls.values()]
|
||||
.map(
|
||||
(tc: Record<string, unknown>): ToolCall => ({
|
||||
id: (tc.id as string) ?? null,
|
||||
index: (tc.index as number) ?? (tc.blockIndex as number) ?? 0,
|
||||
type: (tc.type as string) ?? "function",
|
||||
function: (tc.function as ToolCall["function"]) ?? {
|
||||
name: (tc.name as string) ?? "",
|
||||
arguments: "",
|
||||
},
|
||||
})
|
||||
)
|
||||
.sort((a, b) => a.index - b.index);
|
||||
}
|
||||
const responseBody = {
|
||||
choices: [
|
||||
{
|
||||
message: {
|
||||
role: "assistant",
|
||||
content,
|
||||
},
|
||||
message,
|
||||
finish_reason: hasToolCalls ? "tool_calls" : "stop",
|
||||
},
|
||||
],
|
||||
usage: {
|
||||
|
||||
Generated
+2
-2
@@ -1,12 +1,12 @@
|
||||
{
|
||||
"name": "omniroute",
|
||||
"version": "3.0.2",
|
||||
"version": "3.0.7",
|
||||
"lockfileVersion": 3,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"name": "omniroute",
|
||||
"version": "3.0.2",
|
||||
"version": "3.0.7",
|
||||
"hasInstallScript": true,
|
||||
"license": "MIT",
|
||||
"workspaces": [
|
||||
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "omniroute",
|
||||
"version": "3.0.2",
|
||||
"version": "3.0.7",
|
||||
"description": "Smart AI Router with auto fallback — route to FREE & cheap models, zero downtime. Works with Cursor, Cline, Claude Desktop, Codex, and any OpenAI-compatible tool.",
|
||||
"type": "module",
|
||||
"bin": {
|
||||
|
||||
@@ -33,6 +33,7 @@ export default function CLIToolsPageClient({ machineId }) {
|
||||
const [apiKeys, setApiKeys] = useState([]);
|
||||
const [toolStatuses, setToolStatuses] = useState({});
|
||||
const [statusesLoaded, setStatusesLoaded] = useState(false);
|
||||
const [dynamicModels, setDynamicModels] = useState([]);
|
||||
const translateOrFallback = useCallback(
|
||||
(key, fallback, values = undefined) => {
|
||||
try {
|
||||
@@ -49,6 +50,7 @@ export default function CLIToolsPageClient({ machineId }) {
|
||||
loadCloudSettings();
|
||||
fetchApiKeys();
|
||||
fetchToolStatuses();
|
||||
fetchDynamicModels();
|
||||
}, []);
|
||||
|
||||
const loadCloudSettings = async () => {
|
||||
@@ -107,6 +109,18 @@ export default function CLIToolsPageClient({ machineId }) {
|
||||
}
|
||||
};
|
||||
|
||||
const fetchDynamicModels = async () => {
|
||||
try {
|
||||
const res = await fetch("/v1/models");
|
||||
if (res.ok) {
|
||||
const data = await res.json();
|
||||
setDynamicModels(data?.data || []);
|
||||
}
|
||||
} catch (error) {
|
||||
console.log("Error fetching dynamic models:", error);
|
||||
}
|
||||
};
|
||||
|
||||
const getActiveProviders = () => {
|
||||
return connections.filter((c) => c.isActive !== false);
|
||||
};
|
||||
@@ -116,6 +130,7 @@ export default function CLIToolsPageClient({ machineId }) {
|
||||
const models = [];
|
||||
const seenModels = new Set();
|
||||
|
||||
// First: add static models from the constants
|
||||
activeProviders.forEach((conn) => {
|
||||
const alias = PROVIDER_ID_TO_ALIAS[conn.provider] || conn.provider;
|
||||
const providerModels = getModelsByProviderId(conn.provider);
|
||||
@@ -135,6 +150,31 @@ export default function CLIToolsPageClient({ machineId }) {
|
||||
});
|
||||
});
|
||||
|
||||
// Second: add dynamic models from /v1/models (fills gaps for Kiro, OpenCode, custom providers)
|
||||
const activeProviderIds = new Set(activeProviders.map((c) => c.provider));
|
||||
const activeAliases = new Set(
|
||||
activeProviders.map((c) => PROVIDER_ID_TO_ALIAS[c.provider] || c.provider)
|
||||
);
|
||||
dynamicModels.forEach((dm) => {
|
||||
const modelId = dm.id || dm;
|
||||
if (seenModels.has(modelId)) return;
|
||||
// Parse alias/model format
|
||||
const slashIdx = modelId.indexOf("/");
|
||||
if (slashIdx === -1) return;
|
||||
const alias = modelId.substring(0, slashIdx);
|
||||
const bareModel = modelId.substring(slashIdx + 1);
|
||||
if (!activeAliases.has(alias) && !activeProviderIds.has(alias)) return;
|
||||
seenModels.add(modelId);
|
||||
models.push({
|
||||
value: modelId,
|
||||
label: modelId,
|
||||
provider: alias,
|
||||
alias: alias,
|
||||
connectionName: "",
|
||||
modelId: bareModel,
|
||||
});
|
||||
});
|
||||
|
||||
return models;
|
||||
};
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
import { useState, useEffect, useCallback, useRef } from "react";
|
||||
import { Card, Button, Select, Badge } from "@/shared/components";
|
||||
import { ALIAS_TO_ID } from "@/shared/constants/providers";
|
||||
import dynamic from "next/dynamic";
|
||||
|
||||
const Editor = dynamic(() => import("@monaco-editor/react"), { ssr: false });
|
||||
@@ -20,6 +21,13 @@ interface ProviderOption {
|
||||
label: string;
|
||||
}
|
||||
|
||||
interface ConnectionOption {
|
||||
id: string;
|
||||
name: string;
|
||||
provider: string;
|
||||
authType: string;
|
||||
}
|
||||
|
||||
const ENDPOINT_OPTIONS = [
|
||||
{ value: "chat", label: "Chat Completions" },
|
||||
{ value: "responses", label: "Responses" },
|
||||
@@ -182,8 +190,10 @@ function ImageResultsInline({ data }: { data: any }) {
|
||||
export default function PlaygroundPage() {
|
||||
const [models, setModels] = useState<ModelInfo[]>([]);
|
||||
const [providers, setProviders] = useState<ProviderOption[]>([]);
|
||||
const [allConnections, setAllConnections] = useState<ConnectionOption[]>([]);
|
||||
const [selectedProvider, setSelectedProvider] = useState("");
|
||||
const [selectedModel, setSelectedModel] = useState("");
|
||||
const [selectedConnection, setSelectedConnection] = useState("");
|
||||
const [selectedEndpoint, setSelectedEndpoint] = useState("chat");
|
||||
const [requestBody, setRequestBody] = useState("");
|
||||
const [responseBody, setResponseBody] = useState("");
|
||||
@@ -205,8 +215,16 @@ export default function PlaygroundPage() {
|
||||
const isImageEndpoint = selectedEndpoint === "images";
|
||||
const supportsVision = isChatEndpoint && isVisionModel(selectedModel);
|
||||
|
||||
// Fetch models
|
||||
// Load connections for a given provider — filtered from allConnections
|
||||
const providerConnections = allConnections.filter((c) => {
|
||||
if (!selectedProvider) return false;
|
||||
const resolvedProvider = ALIAS_TO_ID[selectedProvider] || selectedProvider;
|
||||
return c.provider === resolvedProvider || c.provider === selectedProvider;
|
||||
});
|
||||
|
||||
// Fetch models and ALL connections at startup
|
||||
useEffect(() => {
|
||||
// Fetch models
|
||||
fetch("/v1/models")
|
||||
.then((res) => res.json())
|
||||
.then((data) => {
|
||||
@@ -222,7 +240,26 @@ export default function PlaygroundPage() {
|
||||
.sort()
|
||||
.map((p) => ({ value: p, label: p }));
|
||||
setProviders(providerOpts);
|
||||
if (providerOpts.length > 0) setSelectedProvider(providerOpts[0].value);
|
||||
if (providerOpts.length > 0) {
|
||||
setSelectedProvider(providerOpts[0].value);
|
||||
}
|
||||
})
|
||||
.catch(() => {});
|
||||
|
||||
// Fetch ALL connections (once)
|
||||
fetch("/api/providers/client")
|
||||
.then((res) => res.json())
|
||||
.then((data) => {
|
||||
const conns: ConnectionOption[] = [];
|
||||
for (const conn of data?.connections || []) {
|
||||
conns.push({
|
||||
id: conn.id,
|
||||
name: conn.name || conn.email || conn.id,
|
||||
provider: conn.provider,
|
||||
authType: conn.authType || "apiKey",
|
||||
});
|
||||
}
|
||||
setAllConnections(conns);
|
||||
})
|
||||
.catch(() => {});
|
||||
}, []);
|
||||
@@ -241,6 +278,7 @@ export default function PlaygroundPage() {
|
||||
|
||||
const handleProviderChange = (newProvider: string) => {
|
||||
setSelectedProvider(newProvider);
|
||||
setSelectedConnection("");
|
||||
const providerModels = models
|
||||
.filter((m) => !newProvider || m.id.startsWith(newProvider + "/"))
|
||||
.map((m) => m.id);
|
||||
@@ -334,8 +372,13 @@ export default function PlaygroundPage() {
|
||||
} catch {
|
||||
/* ignore parse errors */
|
||||
}
|
||||
const fetchHeaders: Record<string, string> = {};
|
||||
if (selectedConnection) {
|
||||
fetchHeaders["X-OmniRoute-Connection"] = selectedConnection;
|
||||
}
|
||||
res = await fetch(`/api${path}`, {
|
||||
method: "POST",
|
||||
headers: fetchHeaders,
|
||||
body: form,
|
||||
signal: controller.signal,
|
||||
});
|
||||
@@ -345,9 +388,13 @@ export default function PlaygroundPage() {
|
||||
if (supportsVision && uploadedImages.length > 0) {
|
||||
parsed = buildChatBodyWithImages(parsed, uploadedImages);
|
||||
}
|
||||
const fetchHeaders: Record<string, string> = { "Content-Type": "application/json" };
|
||||
if (selectedConnection) {
|
||||
fetchHeaders["X-OmniRoute-Connection"] = selectedConnection;
|
||||
}
|
||||
res = await fetch(`/api${path}`, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
headers: fetchHeaders,
|
||||
body: JSON.stringify(parsed),
|
||||
signal: controller.signal,
|
||||
});
|
||||
@@ -473,6 +520,33 @@ export default function PlaygroundPage() {
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Account/Key — always shown when provider is selected */}
|
||||
{!isSearchEndpoint && (
|
||||
<div className="flex-1 w-full">
|
||||
<label className="block text-xs font-medium text-text-muted mb-1.5 uppercase tracking-wider">
|
||||
Account / Key
|
||||
</label>
|
||||
<Select
|
||||
value={selectedConnection}
|
||||
onChange={(e: any) => setSelectedConnection(e.target.value)}
|
||||
options={[
|
||||
{
|
||||
value: "",
|
||||
label:
|
||||
providerConnections.length > 0
|
||||
? `Auto (${providerConnections.length} accounts)`
|
||||
: "No accounts",
|
||||
},
|
||||
...providerConnections.map((c) => ({
|
||||
value: c.id,
|
||||
label: c.name,
|
||||
})),
|
||||
]}
|
||||
className="w-full"
|
||||
/>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Send Button — hidden in search mode (SearchPlayground has its own) */}
|
||||
{!isSearchEndpoint && (
|
||||
<div className="shrink-0">
|
||||
|
||||
@@ -1513,6 +1513,35 @@ export default function ProviderDetailPage() {
|
||||
|
||||
const canImportModels = connections.some((conn) => conn.isActive !== false);
|
||||
|
||||
// Auto-sync toggle state: read from first active connection's providerSpecificData
|
||||
const autoSyncConnection = connections.find((conn: any) => conn.isActive !== false);
|
||||
const isAutoSyncEnabled = !!(autoSyncConnection as any)?.providerSpecificData?.autoSync;
|
||||
const [togglingAutoSync, setTogglingAutoSync] = useState(false);
|
||||
|
||||
const handleToggleAutoSync = async () => {
|
||||
if (!autoSyncConnection || togglingAutoSync) return;
|
||||
setTogglingAutoSync(true);
|
||||
try {
|
||||
const newValue = !isAutoSyncEnabled;
|
||||
await fetch(`/api/providers/${(autoSyncConnection as any).id}`, {
|
||||
method: "PUT",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({
|
||||
providerSpecificData: { autoSync: newValue },
|
||||
}),
|
||||
});
|
||||
await fetchConnections();
|
||||
notify[newValue ? "success" : "info"](
|
||||
newValue ? t("autoSyncEnabled") : t("autoSyncDisabled")
|
||||
);
|
||||
} catch (error) {
|
||||
console.log("Error toggling auto-sync:", error);
|
||||
notify.error(t("autoSyncToggleFailed"));
|
||||
} finally {
|
||||
setTogglingAutoSync(false);
|
||||
}
|
||||
};
|
||||
|
||||
const customMap = useMemo(() => buildCompatMap(modelMeta.customModels), [modelMeta.customModels]);
|
||||
const overrideMap = useMemo(
|
||||
() => buildCompatMap(modelMeta.modelCompatOverrides),
|
||||
@@ -1604,29 +1633,50 @@ export default function ProviderDetailPage() {
|
||||
};
|
||||
|
||||
const renderModelsSection = () => {
|
||||
const autoSyncToggle = canImportModels && (
|
||||
<button
|
||||
onClick={handleToggleAutoSync}
|
||||
disabled={togglingAutoSync}
|
||||
className="flex items-center gap-1.5 px-2.5 py-1 rounded-lg border border-border bg-transparent cursor-pointer text-[12px] disabled:opacity-50 disabled:cursor-not-allowed"
|
||||
title={t("autoSyncTooltip")}
|
||||
>
|
||||
<span
|
||||
className="material-symbols-outlined text-[16px]"
|
||||
style={{ color: isAutoSyncEnabled ? "#22c55e" : "var(--color-text-muted)" }}
|
||||
>
|
||||
{isAutoSyncEnabled ? "toggle_on" : "toggle_off"}
|
||||
</span>
|
||||
<span className="text-text-main">{t("autoSync")}</span>
|
||||
</button>
|
||||
);
|
||||
|
||||
if (isCompatible) {
|
||||
return (
|
||||
<CompatibleModelsSection
|
||||
providerStorageAlias={providerStorageAlias}
|
||||
providerDisplayAlias={providerDisplayAlias}
|
||||
modelAliases={modelAliases}
|
||||
copied={copied}
|
||||
onCopy={copy}
|
||||
onSetAlias={handleSetAlias}
|
||||
onDeleteAlias={handleDeleteAlias}
|
||||
connections={connections}
|
||||
isAnthropic={isAnthropicCompatible}
|
||||
onImportWithProgress={handleCompatibleImportWithProgress}
|
||||
t={t}
|
||||
effectiveModelNormalize={effectiveModelNormalize}
|
||||
effectiveModelPreserveDeveloper={effectiveModelPreserveDeveloper}
|
||||
getUpstreamHeadersRecord={getUpstreamHeadersRecordForModel}
|
||||
saveModelCompatFlags={saveModelCompatFlags}
|
||||
compatSavingModelId={compatSavingModelId}
|
||||
onModelsChanged={fetchProviderModelMeta}
|
||||
/>
|
||||
<div>
|
||||
<div className="flex items-center gap-2 mb-4">{autoSyncToggle}</div>
|
||||
<CompatibleModelsSection
|
||||
providerStorageAlias={providerStorageAlias}
|
||||
providerDisplayAlias={providerDisplayAlias}
|
||||
modelAliases={modelAliases}
|
||||
copied={copied}
|
||||
onCopy={copy}
|
||||
onSetAlias={handleSetAlias}
|
||||
onDeleteAlias={handleDeleteAlias}
|
||||
connections={connections}
|
||||
isAnthropic={isAnthropicCompatible}
|
||||
onImportWithProgress={handleCompatibleImportWithProgress}
|
||||
t={t}
|
||||
effectiveModelNormalize={effectiveModelNormalize}
|
||||
effectiveModelPreserveDeveloper={effectiveModelPreserveDeveloper}
|
||||
getUpstreamHeadersRecord={getUpstreamHeadersRecordForModel}
|
||||
saveModelCompatFlags={saveModelCompatFlags}
|
||||
compatSavingModelId={compatSavingModelId}
|
||||
onModelsChanged={fetchProviderModelMeta}
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
if (providerInfo.passthroughModels) {
|
||||
return (
|
||||
<div>
|
||||
@@ -1640,6 +1690,7 @@ export default function ProviderDetailPage() {
|
||||
>
|
||||
{importingModels ? t("importingModels") : t("importFromModels")}
|
||||
</Button>
|
||||
{autoSyncToggle}
|
||||
{!canImportModels && (
|
||||
<span className="text-xs text-text-muted">{t("addConnectionToImport")}</span>
|
||||
)}
|
||||
@@ -1673,6 +1724,7 @@ export default function ProviderDetailPage() {
|
||||
>
|
||||
{importingModels ? t("importingModels") : t("importFromModels")}
|
||||
</Button>
|
||||
{autoSyncToggle}
|
||||
{!canImportModels && (
|
||||
<span className="text-xs text-text-muted">{t("addConnectionToImport")}</span>
|
||||
)}
|
||||
@@ -3795,10 +3847,9 @@ function ConnectionRow({
|
||||
{connection.lastError && connection.isActive !== false && (
|
||||
<span
|
||||
className={`text-xs truncate max-w-[300px] ${statusPresentation.errorTextClass}`}
|
||||
title={connection.lastError}
|
||||
>
|
||||
{connection.lastError}
|
||||
</span>
|
||||
title={connection.lastError.replace(/<[^>]*>?/gm, "")}
|
||||
dangerouslySetInnerHTML={{ __html: connection.lastError }}
|
||||
/>
|
||||
)}
|
||||
<span className="text-xs text-text-muted">#{connection.priority}</span>
|
||||
{connection.globalPriority && (
|
||||
|
||||
@@ -9,6 +9,8 @@ type ProxyItem = {
|
||||
type: string;
|
||||
host: string;
|
||||
port: number;
|
||||
username?: string | null;
|
||||
password?: string | null;
|
||||
region?: string | null;
|
||||
notes?: string | null;
|
||||
status?: string;
|
||||
@@ -207,6 +209,7 @@ export default function ProxyRegistryManager() {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({
|
||||
proxyId: item.id,
|
||||
proxy: {
|
||||
type: item.type || "http",
|
||||
host: item.host,
|
||||
@@ -463,12 +466,7 @@ export default function ProxyRegistryManager() {
|
||||
</td>
|
||||
<td className="py-2 pr-3 text-xs text-text-muted">
|
||||
<div className="flex flex-col gap-0.5">
|
||||
{health ? (
|
||||
<>
|
||||
<span>{health.successRate ?? 0}% success</span>
|
||||
<span>{health.avgLatencyMs ?? "-"} ms avg</span>
|
||||
</>
|
||||
) : testById[item.id] ? (
|
||||
{testById[item.id] ? (
|
||||
testById[item.id]!.success ? (
|
||||
<>
|
||||
<span className="text-emerald-400">
|
||||
@@ -480,9 +478,14 @@ export default function ProxyRegistryManager() {
|
||||
</>
|
||||
) : (
|
||||
<span className="text-red-400">
|
||||
{testById[item.id]!.error || "failed"}
|
||||
✗ {testById[item.id]!.error || "failed"}
|
||||
</span>
|
||||
)
|
||||
) : health ? (
|
||||
<>
|
||||
<span>{health.successRate ?? 0}% success</span>
|
||||
<span>{health.avgLatencyMs ?? "-"} ms avg</span>
|
||||
</>
|
||||
) : (
|
||||
<span>—</span>
|
||||
)}
|
||||
|
||||
@@ -334,11 +334,21 @@ export default function ProviderLimits() {
|
||||
if (groupBy !== "environment") return null;
|
||||
const groups = new Map();
|
||||
for (const conn of visibleConnections) {
|
||||
const key = conn.group || t("ungrouped");
|
||||
const key = (conn.providerSpecificData?.tag as string | undefined)?.trim() || t("ungrouped");
|
||||
if (!groups.has(key)) groups.set(key, []);
|
||||
groups.get(key).push(conn);
|
||||
}
|
||||
return groups;
|
||||
|
||||
// Convert to sorted array based on tag string (ungrouped at the end)
|
||||
const sortedGroups = new Map(
|
||||
[...groups.entries()].sort(([a], [b]) => {
|
||||
if (a === t("ungrouped")) return 1;
|
||||
if (b === t("ungrouped")) return -1;
|
||||
return a.localeCompare(b);
|
||||
})
|
||||
);
|
||||
|
||||
return sortedGroups;
|
||||
}, [groupBy, visibleConnections, t]);
|
||||
|
||||
const handleSetGroupBy = (value: "none" | "environment") => {
|
||||
@@ -359,7 +369,10 @@ export default function ProviderLimits() {
|
||||
useEffect(() => {
|
||||
if (typeof window === "undefined") return;
|
||||
const hasSaved = localStorage.getItem(LS_GROUP_BY) !== null;
|
||||
if (!hasSaved && connections.some((c) => c.group)) {
|
||||
if (
|
||||
!hasSaved &&
|
||||
connections.some((c) => (c.providerSpecificData?.tag as string | undefined)?.trim())
|
||||
) {
|
||||
setGroupBy("environment");
|
||||
}
|
||||
}, [connections]);
|
||||
@@ -498,7 +511,7 @@ export default function ProviderLimits() {
|
||||
</div>
|
||||
|
||||
{/* Account rows */}
|
||||
<div className="rounded-xl border border-border overflow-hidden bg-bg-subtle">
|
||||
<div className="rounded-xl border border-border overflow-hidden bg-surface">
|
||||
{/* Table header */}
|
||||
<div
|
||||
className="items-center px-4 py-2.5 border-b border-border text-[11px] font-semibold uppercase tracking-wider text-text-muted"
|
||||
|
||||
@@ -0,0 +1,125 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { getProviderConnectionById } from "@/models";
|
||||
import { replaceCustomModels } from "@/lib/db/models";
|
||||
import { saveCallLog } from "@/lib/usage/callLogs";
|
||||
import { isAuthenticated } from "@/shared/utils/apiAuth";
|
||||
|
||||
/**
|
||||
* POST /api/providers/[id]/sync-models
|
||||
*
|
||||
* Fetches the model list from a provider's /models endpoint and replaces the
|
||||
* full custom models list for that provider. Logs the operation to call_logs.
|
||||
*
|
||||
* Used by:
|
||||
* - modelSyncScheduler (auto-sync on interval)
|
||||
* - Manual trigger from UI
|
||||
*/
|
||||
export async function POST(request: Request, { params }: { params: Promise<{ id: string }> }) {
|
||||
const start = Date.now();
|
||||
const { id } = await params;
|
||||
|
||||
try {
|
||||
if (!(await isAuthenticated(request))) {
|
||||
return NextResponse.json(
|
||||
{ error: { message: "Authentication required", type: "invalid_api_key" } },
|
||||
{ status: 401 }
|
||||
);
|
||||
}
|
||||
|
||||
const connection = await getProviderConnectionById(id);
|
||||
if (!connection) {
|
||||
return NextResponse.json({ error: "Connection not found" }, { status: 404 });
|
||||
}
|
||||
|
||||
// Use a human-readable provider name for logs
|
||||
const providerLabel = connection.name || connection.provider || "unknown";
|
||||
|
||||
// Fetch models from the existing /api/providers/[id]/models endpoint
|
||||
const origin = new URL(request.url).origin;
|
||||
const modelsUrl = `${origin}/api/providers/${id}/models`;
|
||||
const modelsRes = await fetch(modelsUrl, {
|
||||
method: "GET",
|
||||
headers: {
|
||||
cookie: request.headers.get("cookie") || "",
|
||||
"x-internal": "model-sync",
|
||||
},
|
||||
});
|
||||
|
||||
const duration = Date.now() - start;
|
||||
const modelsData = await modelsRes.json();
|
||||
|
||||
if (!modelsRes.ok) {
|
||||
// Log the failed attempt
|
||||
await saveCallLog({
|
||||
method: "GET",
|
||||
path: `/api/providers/${id}/models`,
|
||||
status: modelsRes.status,
|
||||
model: "model-sync",
|
||||
provider: providerLabel,
|
||||
sourceFormat: "-",
|
||||
connectionId: id,
|
||||
duration,
|
||||
error: modelsData.error || `HTTP ${modelsRes.status}`,
|
||||
requestType: "model-sync",
|
||||
});
|
||||
|
||||
return NextResponse.json(
|
||||
{ error: modelsData.error || "Failed to fetch models" },
|
||||
{ status: modelsRes.status }
|
||||
);
|
||||
}
|
||||
|
||||
const fetchedModels = modelsData.models || [];
|
||||
|
||||
// Replace the full model list
|
||||
const models = fetchedModels
|
||||
.map((m: any) => ({
|
||||
id: m.id || m.name || m.model,
|
||||
name: m.name || m.displayName || m.id || m.model,
|
||||
source: "auto-sync",
|
||||
}))
|
||||
.filter((m: any) => m.id);
|
||||
|
||||
const replaced = await replaceCustomModels(connection.provider, models);
|
||||
|
||||
// Log the successful sync
|
||||
await saveCallLog({
|
||||
method: "GET",
|
||||
path: `/api/providers/${id}/models`,
|
||||
status: 200,
|
||||
model: "model-sync",
|
||||
provider: providerLabel,
|
||||
sourceFormat: "-",
|
||||
connectionId: id,
|
||||
duration: Date.now() - start,
|
||||
requestType: "model-sync",
|
||||
responseBody: {
|
||||
syncedModels: models.length,
|
||||
provider: connection.provider,
|
||||
},
|
||||
});
|
||||
|
||||
return NextResponse.json({
|
||||
ok: true,
|
||||
provider: connection.provider,
|
||||
syncedModels: replaced.length,
|
||||
models: replaced,
|
||||
});
|
||||
} catch (error: any) {
|
||||
// Log error
|
||||
await saveCallLog({
|
||||
method: "POST",
|
||||
path: `/api/providers/${id}/sync-models`,
|
||||
status: 500,
|
||||
model: "model-sync",
|
||||
provider: "unknown",
|
||||
sourceFormat: "-",
|
||||
connectionId: id,
|
||||
duration: Date.now() - start,
|
||||
error: error.message || "Sync failed",
|
||||
requestType: "model-sync",
|
||||
}).catch(() => {});
|
||||
|
||||
return NextResponse.json({ error: error.message || "Failed to sync models" }, { status: 500 });
|
||||
}
|
||||
}
|
||||
@@ -8,6 +8,7 @@ import {
|
||||
import { testProxySchema } from "@/shared/validation/schemas";
|
||||
import { isValidationFailure, validateBody } from "@/shared/validation/helpers";
|
||||
import { createErrorResponse, createErrorResponseFromUnknown } from "@/lib/api/errorResponse";
|
||||
import { getProxyById } from "@/lib/localDb";
|
||||
|
||||
const BASE_SUPPORTED_PROXY_TYPES = new Set(["http", "https"]);
|
||||
|
||||
@@ -56,7 +57,26 @@ export async function POST(request: Request) {
|
||||
type: "invalid_request",
|
||||
});
|
||||
}
|
||||
const { proxy } = validation.data;
|
||||
let { proxy } = validation.data;
|
||||
|
||||
// If a proxyId is provided, look up the real (non-redacted) credentials from DB.
|
||||
// The frontend sends redacted credentials (***) from listProxies(), so we need
|
||||
// the actual secrets for testing.
|
||||
const body = rawBody as Record<string, unknown>;
|
||||
const proxyId = typeof body.proxyId === "string" ? body.proxyId.trim() : null;
|
||||
if (proxyId) {
|
||||
const dbProxy = await getProxyById(proxyId, { includeSecrets: true });
|
||||
if (dbProxy) {
|
||||
proxy = {
|
||||
...proxy,
|
||||
host: proxy.host || dbProxy.host,
|
||||
port: proxy.port || String(dbProxy.port),
|
||||
type: proxy.type || dbProxy.type,
|
||||
username: dbProxy.username,
|
||||
password: dbProxy.password,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
const proxyType = String(proxy.type || "http").toLowerCase();
|
||||
if (proxyType === "socks5" && !isSocks5ProxyEnabled()) {
|
||||
|
||||
@@ -131,34 +131,101 @@ export async function GET(
|
||||
return Response.json({ message: "Usage not available for API key connections" });
|
||||
}
|
||||
|
||||
// Refresh credentials if needed using executor
|
||||
let refreshed = false;
|
||||
try {
|
||||
const result = await refreshAndUpdateCredentials(connection);
|
||||
connection = result.connection;
|
||||
refreshed = result.refreshed;
|
||||
|
||||
// Sync to cloud only if token was refreshed
|
||||
if (refreshed) {
|
||||
await syncToCloudIfEnabled();
|
||||
}
|
||||
} catch (refreshError) {
|
||||
console.error("[Usage API] Credential refresh failed:", refreshError);
|
||||
return Response.json(
|
||||
{
|
||||
error: `Credential refresh failed: ${(refreshError as any).message}`,
|
||||
},
|
||||
{ status: 401 }
|
||||
);
|
||||
}
|
||||
|
||||
// Resolve proxy for this connection (key → combo → provider → global → direct)
|
||||
// Resolve proxy for this connection FIRST (key → combo → provider → global → direct)
|
||||
// so that both credential refresh AND usage fetch go through the proxy.
|
||||
const proxyInfo = await resolveProxyForConnection(connectionId);
|
||||
|
||||
// Fetch usage from provider API, wrapped in proxy context
|
||||
const usage = await runWithProxyContext(proxyInfo?.proxy || null, () =>
|
||||
getUsageForProvider(connection)
|
||||
);
|
||||
// Helper: perform credential refresh + usage fetch
|
||||
const fetchUsageWithContext = async (proxyConfig: unknown) => {
|
||||
return runWithProxyContext(proxyConfig, async () => {
|
||||
let conn = connection;
|
||||
let wasRefreshed = false;
|
||||
|
||||
// Refresh credentials if needed using executor
|
||||
try {
|
||||
const result = await refreshAndUpdateCredentials(conn);
|
||||
conn = result.connection;
|
||||
wasRefreshed = result.refreshed;
|
||||
|
||||
// Sync to cloud only if token was refreshed
|
||||
if (wasRefreshed) {
|
||||
await syncToCloudIfEnabled();
|
||||
}
|
||||
} catch (refreshError) {
|
||||
console.error("[Usage API] Credential refresh failed:", refreshError);
|
||||
throw refreshError;
|
||||
}
|
||||
|
||||
// Fetch usage from provider API
|
||||
const usageData = await getUsageForProvider(conn);
|
||||
connection = conn; // propagate updated connection for status sync below
|
||||
return { usage: usageData, refreshed: wasRefreshed };
|
||||
});
|
||||
};
|
||||
|
||||
// Check if a usage result indicates a network-level error (proxy can't relay)
|
||||
const isNetworkFailure = (usageResult: any): boolean => {
|
||||
const msg = usageResult?.usage?.message;
|
||||
if (typeof msg !== "string") return false;
|
||||
return (
|
||||
msg.includes("fetch failed") ||
|
||||
msg.includes("ECONNREFUSED") ||
|
||||
msg.includes("ETIMEDOUT") ||
|
||||
msg.includes("Proxy unreachable") ||
|
||||
msg.includes("UND_ERR_CONNECT_TIMEOUT")
|
||||
);
|
||||
};
|
||||
|
||||
let result: any;
|
||||
const proxyConfig = proxyInfo?.proxy || null;
|
||||
try {
|
||||
result = await fetchUsageWithContext(proxyConfig);
|
||||
} catch (proxyError: any) {
|
||||
const isAuthError =
|
||||
proxyError?.message?.includes?.("refresh") || proxyError?.message?.includes?.("Credential");
|
||||
|
||||
if (isAuthError) {
|
||||
return Response.json(
|
||||
{ error: `Credential refresh failed: ${proxyError.message}` },
|
||||
{ status: 401 }
|
||||
);
|
||||
}
|
||||
|
||||
// If proxy was active and it's a network error (thrown), retry without proxy
|
||||
const isThrownNetworkError =
|
||||
proxyError?.message === "fetch failed" ||
|
||||
proxyError?.code === "PROXY_UNREACHABLE" ||
|
||||
proxyError?.code === "UND_ERR_CONNECT_TIMEOUT" ||
|
||||
proxyError?.cause?.code === "ECONNREFUSED";
|
||||
|
||||
if (proxyConfig && isThrownNetworkError) {
|
||||
console.warn(
|
||||
`[Usage API] Proxy fetch threw for ${connectionId}, retrying without proxy:`,
|
||||
proxyError?.message
|
||||
);
|
||||
result = await fetchUsageWithContext(null);
|
||||
} else {
|
||||
throw proxyError;
|
||||
}
|
||||
}
|
||||
|
||||
// If the usage result contains a network error AND a proxy was active,
|
||||
// retry without proxy. getCodexUsage() catches fetch errors internally
|
||||
// and returns {message: "Failed to fetch..."} instead of throwing.
|
||||
if (proxyConfig && isNetworkFailure(result)) {
|
||||
console.warn(
|
||||
`[Usage API] Proxy usage returned network error for ${connectionId}, retrying without proxy:`,
|
||||
result.usage?.message
|
||||
);
|
||||
try {
|
||||
result = await fetchUsageWithContext(null);
|
||||
} catch (directError: any) {
|
||||
console.error("[Usage API] Direct fetch also failed:", directError?.message);
|
||||
throw directError;
|
||||
}
|
||||
}
|
||||
|
||||
const { usage, refreshed } = result;
|
||||
|
||||
// Populate quota cache for quota-aware account selection
|
||||
if (isRecord(usage?.quotas)) {
|
||||
|
||||
@@ -14,8 +14,8 @@ import { getAllImageModels } from "@omniroute/open-sse/config/imageRegistry.ts";
|
||||
import { getAllRerankModels } from "@omniroute/open-sse/config/rerankRegistry.ts";
|
||||
import { getAllAudioModels } from "@omniroute/open-sse/config/audioRegistry.ts";
|
||||
import { getAllModerationModels } from "@omniroute/open-sse/config/moderationRegistry.ts";
|
||||
import { getAllVideoModels, getVideoProvider } from "@omniroute/open-sse/config/videoRegistry.ts";
|
||||
import { getAllMusicModels, getMusicProvider } from "@omniroute/open-sse/config/musicRegistry.ts";
|
||||
import { getAllVideoModels } from "@omniroute/open-sse/config/videoRegistry.ts";
|
||||
import { getAllMusicModels } from "@omniroute/open-sse/config/musicRegistry.ts";
|
||||
import { REGISTRY } from "@omniroute/open-sse/config/providerRegistry.ts";
|
||||
|
||||
const FALLBACK_ALIAS_TO_PROVIDER = {
|
||||
@@ -315,10 +315,9 @@ export async function getUnifiedModelsResponse(
|
||||
});
|
||||
}
|
||||
|
||||
// Add video models (local providers always listed, cloud filtered by active)
|
||||
// Add video models (filtered by active providers)
|
||||
for (const videoModel of getAllVideoModels()) {
|
||||
const vConfig = getVideoProvider(videoModel.provider);
|
||||
if (vConfig?.authType !== "none" && !isProviderActive(videoModel.provider)) continue;
|
||||
if (!isProviderActive(videoModel.provider)) continue;
|
||||
models.push({
|
||||
id: videoModel.id,
|
||||
object: "model",
|
||||
@@ -328,10 +327,9 @@ export async function getUnifiedModelsResponse(
|
||||
});
|
||||
}
|
||||
|
||||
// Add music models (local providers always listed, cloud filtered by active)
|
||||
// Add music models (filtered by active providers)
|
||||
for (const musicModel of getAllMusicModels()) {
|
||||
const mConfig = getMusicProvider(musicModel.provider);
|
||||
if (mConfig?.authType !== "none" && !isProviderActive(musicModel.provider)) continue;
|
||||
if (!isProviderActive(musicModel.provider)) continue;
|
||||
models.push({
|
||||
id: musicModel.id,
|
||||
object: "model",
|
||||
|
||||
+956
-954
File diff suppressed because it is too large
Load Diff
@@ -1378,6 +1378,11 @@
|
||||
"chatCompletions": "Chat Completions",
|
||||
"importingModels": "Importing...",
|
||||
"importFromModels": "Import from /models",
|
||||
"autoSync": "Auto-Sync",
|
||||
"autoSyncTooltip": "Automatically refresh model list every 24h (configurable via MODEL_SYNC_INTERVAL_HOURS)",
|
||||
"autoSyncEnabled": "Auto-sync enabled — models will refresh periodically",
|
||||
"autoSyncDisabled": "Auto-sync disabled",
|
||||
"autoSyncToggleFailed": "Failed to toggle auto-sync",
|
||||
"addConnectionToImport": "Add a connection to enable importing.",
|
||||
"noModelsConfigured": "No models configured",
|
||||
"connectionCount": "{count} connection(s)",
|
||||
|
||||
@@ -1378,6 +1378,11 @@
|
||||
"chatCompletions": "聊天完成",
|
||||
"importingModels": "正在导入...",
|
||||
"importFromModels": "从 /models 导入",
|
||||
"autoSync": "自动同步",
|
||||
"autoSyncTooltip": "每24小时自动刷新模型列表(可通过 MODEL_SYNC_INTERVAL_HOURS 配置)",
|
||||
"autoSyncEnabled": "已启用自动同步 — 模型列表将定期刷新",
|
||||
"autoSyncDisabled": "已禁用自动同步",
|
||||
"autoSyncToggleFailed": "切换自动同步失败",
|
||||
"addConnectionToImport": "添加连接以启用导入。",
|
||||
"noModelsConfigured": "尚未配置模型",
|
||||
"connectionCount": "{count} 连接",
|
||||
|
||||
@@ -360,6 +360,76 @@ export async function addCustomModel(
|
||||
return model;
|
||||
}
|
||||
|
||||
/**
|
||||
* Replace the entire custom models list for a provider (used by auto-sync).
|
||||
* Preserves per-model compatibility overrides for models that still exist.
|
||||
*/
|
||||
export async function replaceCustomModels(
|
||||
providerId: string,
|
||||
models: Array<{
|
||||
id: string;
|
||||
name?: string;
|
||||
source?: string;
|
||||
apiFormat?: string;
|
||||
supportedEndpoints?: string[];
|
||||
}>
|
||||
) {
|
||||
const db = getDbInstance();
|
||||
const existing = await getCustomModels(providerId);
|
||||
const existingMap = new Map<string, JsonRecord>();
|
||||
if (Array.isArray(existing)) {
|
||||
for (const m of existing) {
|
||||
if (m && typeof m === "object" && m.id) existingMap.set(m.id, m);
|
||||
}
|
||||
}
|
||||
|
||||
// Merge: keep existing per-model compat flags if model still exists
|
||||
const merged = models.map((m) => {
|
||||
const prev = existingMap.get(m.id);
|
||||
return {
|
||||
id: m.id,
|
||||
name: m.name || m.id,
|
||||
source: m.source || "auto-sync",
|
||||
apiFormat: m.apiFormat || (prev as any)?.apiFormat || "chat-completions",
|
||||
supportedEndpoints: m.supportedEndpoints || (prev as any)?.supportedEndpoints || ["chat"],
|
||||
// Preserve existing compat flags
|
||||
...(prev && (prev as any).normalizeToolCallId !== undefined
|
||||
? { normalizeToolCallId: (prev as any).normalizeToolCallId }
|
||||
: {}),
|
||||
...(prev && (prev as any).preserveOpenAIDeveloperRole !== undefined
|
||||
? { preserveOpenAIDeveloperRole: (prev as any).preserveOpenAIDeveloperRole }
|
||||
: {}),
|
||||
...(prev && (prev as any).compatByProtocol
|
||||
? { compatByProtocol: (prev as any).compatByProtocol }
|
||||
: {}),
|
||||
...(prev && (prev as any).upstreamHeaders
|
||||
? { upstreamHeaders: (prev as any).upstreamHeaders }
|
||||
: {}),
|
||||
};
|
||||
});
|
||||
|
||||
if (merged.length === 0) {
|
||||
db.prepare("DELETE FROM key_value WHERE namespace = 'customModels' AND key = ?").run(
|
||||
providerId
|
||||
);
|
||||
} else {
|
||||
db.prepare(
|
||||
"INSERT OR REPLACE INTO key_value (namespace, key, value) VALUES ('customModels', ?, ?)"
|
||||
).run(providerId, JSON.stringify(merged));
|
||||
}
|
||||
|
||||
// Remove compat overrides for models that no longer exist
|
||||
const newIds = new Set(models.map((m) => m.id));
|
||||
const compatList = readCompatList(providerId);
|
||||
const filteredCompat = compatList.filter((e) => newIds.has(e.id));
|
||||
if (filteredCompat.length !== compatList.length) {
|
||||
writeCompatList(providerId, filteredCompat);
|
||||
}
|
||||
|
||||
backupDbFile("pre-write");
|
||||
return merged;
|
||||
}
|
||||
|
||||
export async function removeCustomModel(providerId, modelId) {
|
||||
const db = getDbInstance();
|
||||
const row = db
|
||||
|
||||
@@ -48,6 +48,7 @@ export {
|
||||
getCustomModels,
|
||||
getAllCustomModels,
|
||||
addCustomModel,
|
||||
replaceCustomModels,
|
||||
removeCustomModel,
|
||||
updateCustomModel,
|
||||
getModelCompatOverrides,
|
||||
|
||||
@@ -655,7 +655,7 @@ export async function validateProviderApiKey({ provider, apiKey, providerSpecifi
|
||||
// LongCat AI — does not expose /v1/models; validate via chat completions directly (#592)
|
||||
longcat: async ({ apiKey }: any) => {
|
||||
try {
|
||||
const res = await fetch("https://longcat.chat/api/v1/chat/completions", {
|
||||
const res = await fetch("https://api.longcat.chat/openai/v1/chat/completions", {
|
||||
method: "POST",
|
||||
headers: buildBearerHeaders(apiKey),
|
||||
body: JSON.stringify({
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
import fs from "fs/promises";
|
||||
import fsSync from "fs";
|
||||
import os from "os";
|
||||
import path from "path";
|
||||
import { spawn } from "child_process";
|
||||
import { spawn, execFileSync } from "child_process";
|
||||
|
||||
const VALID_RUNTIME_MODES = new Set(["auto", "host", "container"]);
|
||||
const FALSE_VALUES = new Set(["0", "false", "no", "off"]);
|
||||
@@ -258,6 +259,42 @@ const validateEnvPath = (value: string | undefined, allowedParents: string[]): s
|
||||
return normalized;
|
||||
};
|
||||
|
||||
/**
|
||||
* Detect the npm global bin directory.
|
||||
* Cached on first call — `execFileSync` is expensive, only run once.
|
||||
*/
|
||||
let _npmGlobalPrefix: string | undefined;
|
||||
const getNpmGlobalPrefix = (): string => {
|
||||
if (_npmGlobalPrefix !== undefined) return _npmGlobalPrefix;
|
||||
|
||||
const envPrefix = String(process.env.npm_config_prefix || "").trim();
|
||||
if (envPrefix && path.isAbsolute(envPrefix)) {
|
||||
_npmGlobalPrefix = envPrefix;
|
||||
return _npmGlobalPrefix;
|
||||
}
|
||||
|
||||
try {
|
||||
const result = execFileSync("npm", ["config", "get", "prefix"], {
|
||||
timeout: 5000,
|
||||
encoding: "utf8",
|
||||
stdio: ["ignore", "pipe", "ignore"],
|
||||
...(isWindows() ? { shell: true } : {}),
|
||||
});
|
||||
const prefix = result.trim();
|
||||
if (
|
||||
prefix &&
|
||||
path.isAbsolute(prefix) &&
|
||||
!DANGEROUS_PATH_CHARS.some((c) => prefix.includes(c))
|
||||
) {
|
||||
_npmGlobalPrefix = prefix;
|
||||
return _npmGlobalPrefix;
|
||||
}
|
||||
} catch {}
|
||||
|
||||
_npmGlobalPrefix = "";
|
||||
return _npmGlobalPrefix;
|
||||
};
|
||||
|
||||
/**
|
||||
* Pre-compute expected parent directories at module startup for performance.
|
||||
* These are the allowed directories for CLI binary installation locations.
|
||||
@@ -281,6 +318,8 @@ const getExpectedParentPaths = (): string[] => {
|
||||
"C:\\Program Files (x86)",
|
||||
]);
|
||||
|
||||
const npmPrefix = getNpmGlobalPrefix();
|
||||
|
||||
return [
|
||||
home,
|
||||
userProfile,
|
||||
@@ -288,6 +327,7 @@ const getExpectedParentPaths = (): string[] => {
|
||||
validatedLocalAppData,
|
||||
validatedProgramFiles,
|
||||
validatedProgramFilesX86,
|
||||
npmPrefix,
|
||||
].filter(Boolean);
|
||||
};
|
||||
|
||||
@@ -310,86 +350,94 @@ const getExtraPaths = () =>
|
||||
});
|
||||
|
||||
/**
|
||||
* Get known installation paths for a specific CLI tool on Windows.
|
||||
* Returns ONLY verified, tool-specific paths - NOT generic user bin directories.
|
||||
* This is more secure than searching PATH as it checks known locations only.
|
||||
* Get known installation paths for a specific CLI tool.
|
||||
* Checks npm global prefix, NVM locations, standalone installer paths.
|
||||
* Works on all platforms — Windows checks .cmd wrappers, Linux/macOS checks bare names.
|
||||
*/
|
||||
const getKnownToolPaths = (toolId: string): string[] => {
|
||||
if (!isWindows()) return [];
|
||||
|
||||
const home = os.homedir();
|
||||
const userProfile = process.env.USERPROFILE || home;
|
||||
const paths: string[] = [];
|
||||
|
||||
// Validate environment paths against allowed parent directories
|
||||
const appData = validateEnvPath(process.env.APPDATA, [home, userProfile]);
|
||||
const localAppData = validateEnvPath(process.env.LOCALAPPDATA, [
|
||||
path.join(home, "AppData", "Local"),
|
||||
path.join(userProfile, "AppData", "Local"),
|
||||
userProfile,
|
||||
]);
|
||||
|
||||
// Cache nvm node path to avoid duplicate detection calls
|
||||
const npmPrefix = getNpmGlobalPrefix();
|
||||
const nvmNodePath = getNvmNodePath();
|
||||
|
||||
// Tool-specific known installation paths (verified locations only)
|
||||
const knownPaths: Record<string, string[]> = {
|
||||
const toolBins: Record<string, [string, string][]> = {
|
||||
claude: [
|
||||
// Official Claude Code standalone installer locations
|
||||
path.join(home, ".local", "bin", "claude.exe"),
|
||||
...(localAppData ? [path.join(localAppData, "Programs", "Claude", "claude.exe")] : []),
|
||||
...(localAppData ? [path.join(localAppData, "claude-code", "claude.exe")] : []),
|
||||
// npm global (only if nvm-windows is detected)
|
||||
...(nvmNodePath ? [path.join(nvmNodePath, "claude-code.cmd")] : []),
|
||||
],
|
||||
codex: [
|
||||
path.join(home, ".local", "bin", "codex"),
|
||||
// npm global (only if nvm-windows is detected)
|
||||
...(nvmNodePath ? [path.join(nvmNodePath, "codex.cmd")] : []),
|
||||
...(appData ? [path.join(appData, "npm", "codex.cmd")] : []),
|
||||
],
|
||||
droid: [
|
||||
path.join(home, ".local", "bin", "droid"),
|
||||
// npm global (only if nvm-windows is detected)
|
||||
...(nvmNodePath ? [path.join(nvmNodePath, "droid.cmd")] : []),
|
||||
...(appData ? [path.join(appData, "npm", "droid.cmd")] : []),
|
||||
],
|
||||
openclaw: [
|
||||
path.join(home, ".local", "bin", "openclaw"),
|
||||
// npm global (only if nvm-windows is detected)
|
||||
...(nvmNodePath ? [path.join(nvmNodePath, "openclaw.cmd")] : []),
|
||||
...(appData ? [path.join(appData, "npm", "openclaw.cmd")] : []),
|
||||
["claude.cmd", "claude"],
|
||||
["claude.exe", "claude"],
|
||||
],
|
||||
codex: [["codex.cmd", "codex"]],
|
||||
droid: [["droid.cmd", "droid"]],
|
||||
openclaw: [["openclaw.cmd", "openclaw"]],
|
||||
cursor: [
|
||||
path.join(home, ".local", "bin", "agent"),
|
||||
path.join(home, ".local", "bin", "cursor"),
|
||||
// npm global (only if nvm-windows is detected)
|
||||
...(nvmNodePath ? [path.join(nvmNodePath, "agent.cmd")] : []),
|
||||
...(nvmNodePath ? [path.join(nvmNodePath, "cursor.cmd")] : []),
|
||||
...(appData ? [path.join(appData, "npm", "agent.cmd")] : []),
|
||||
...(appData ? [path.join(appData, "npm", "cursor.cmd")] : []),
|
||||
["agent.cmd", "agent"],
|
||||
["cursor.cmd", "cursor"],
|
||||
],
|
||||
cline: [
|
||||
path.join(home, ".local", "bin", "cline"),
|
||||
// npm global (only if nvm-windows is detected)
|
||||
...(nvmNodePath ? [path.join(nvmNodePath, "cline.cmd")] : []),
|
||||
...(appData ? [path.join(appData, "npm", "cline.cmd")] : []),
|
||||
],
|
||||
kilo: [
|
||||
path.join(home, ".local", "bin", "kilocode"),
|
||||
// npm global (only if nvm-windows is detected)
|
||||
...(nvmNodePath ? [path.join(nvmNodePath, "kilocode.cmd")] : []),
|
||||
...(appData ? [path.join(appData, "npm", "kilocode.cmd")] : []),
|
||||
],
|
||||
opencode: [
|
||||
path.join(home, ".local", "bin", "opencode"),
|
||||
// npm global (only if nvm-windows is detected)
|
||||
...(nvmNodePath ? [path.join(nvmNodePath, "opencode.cmd")] : []),
|
||||
...(appData ? [path.join(appData, "npm", "opencode.cmd")] : []),
|
||||
],
|
||||
// Add other tools as needed with their specific known paths
|
||||
cline: [["cline.cmd", "cline"]],
|
||||
kilo: [["kilocode.cmd", "kilocode"]],
|
||||
opencode: [["opencode.cmd", "opencode"]],
|
||||
};
|
||||
|
||||
return knownPaths[toolId] || [];
|
||||
const bins = toolBins[toolId] || [];
|
||||
|
||||
if (isWindows()) {
|
||||
const userProfile = process.env.USERPROFILE || home;
|
||||
const appData = validateEnvPath(process.env.APPDATA, [home, userProfile]);
|
||||
const localAppData = validateEnvPath(process.env.LOCALAPPDATA, [
|
||||
path.join(home, "AppData", "Local"),
|
||||
path.join(userProfile, "AppData", "Local"),
|
||||
userProfile,
|
||||
]);
|
||||
|
||||
if (toolId === "claude") {
|
||||
paths.push(path.join(home, ".local", "bin", "claude.exe"));
|
||||
if (localAppData) {
|
||||
paths.push(path.join(localAppData, "Programs", "Claude", "claude.exe"));
|
||||
paths.push(path.join(localAppData, "claude-code", "claude.exe"));
|
||||
}
|
||||
}
|
||||
|
||||
for (const [winName] of bins) {
|
||||
if (npmPrefix) paths.push(path.join(npmPrefix, winName));
|
||||
if (appData) {
|
||||
const appDataPath = path.join(appData, "npm", winName);
|
||||
if (
|
||||
!npmPrefix ||
|
||||
path.normalize(appDataPath) !== path.normalize(path.join(npmPrefix, winName))
|
||||
) {
|
||||
paths.push(appDataPath);
|
||||
}
|
||||
}
|
||||
if (nvmNodePath) paths.push(path.join(nvmNodePath, winName));
|
||||
}
|
||||
} else {
|
||||
for (const [, posixName] of bins) {
|
||||
const nodeBinDir = path.dirname(process.execPath);
|
||||
paths.push(path.join(nodeBinDir, posixName));
|
||||
|
||||
if (npmPrefix) {
|
||||
paths.push(path.join(npmPrefix, "bin", posixName));
|
||||
}
|
||||
|
||||
paths.push(path.join(home, ".local", "bin", posixName));
|
||||
// Only add system paths if they exist (avoids unnecessary stat calls)
|
||||
if (fsSync.existsSync("/usr/local/bin")) {
|
||||
paths.push(path.join("/usr", "local", "bin", posixName));
|
||||
}
|
||||
if (fsSync.existsSync("/usr/bin")) {
|
||||
paths.push(path.join("/usr", "bin", posixName));
|
||||
}
|
||||
|
||||
if (toolId === "opencode") {
|
||||
paths.push(path.join(home, ".opencode", "bin", posixName));
|
||||
}
|
||||
if (toolId === "claude") {
|
||||
paths.push(path.join(home, ".claude", "bin", posixName));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return paths;
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -492,7 +540,7 @@ const locateCommand = async (command: string, env: Record<string, string | undef
|
||||
* Security hardening:
|
||||
* - Resolves symlinks and verifies target stays within expected directories
|
||||
* - Verifies file is a regular file (not directory, pipe, or device)
|
||||
* - Checks file size bounds (1KB - 100MB) to detect suspicious binaries
|
||||
* - Checks file size bounds (30B - 100MB) to detect suspicious binaries
|
||||
*/
|
||||
const checkKnownPath = async (commandPath: string) => {
|
||||
if (!path.isAbsolute(commandPath)) {
|
||||
@@ -521,9 +569,10 @@ const checkKnownPath = async (commandPath: string) => {
|
||||
return { installed: false, commandPath: null, reason: "not_file" };
|
||||
}
|
||||
|
||||
// CLI binaries should be > 1KB and < 100MB
|
||||
// This catches suspicious files while allowing for wrapper scripts
|
||||
if (stat.size < 1024 || stat.size > 100 * 1024 * 1024) {
|
||||
// CLI binaries should be > 30 bytes and < 100MB
|
||||
// npm .cmd wrappers on Windows are ~300-500 bytes, JS wrappers on Linux can be ~44 bytes
|
||||
// Minimum catches empty/suspicious files while allowing legitimate thin wrappers
|
||||
if (stat.size < 30 || stat.size > 100 * 1024 * 1024) {
|
||||
return { installed: false, commandPath: null, reason: "suspicious_size" };
|
||||
}
|
||||
} catch (error) {
|
||||
@@ -556,7 +605,7 @@ const locateCommandCandidate = async (
|
||||
|
||||
// SECURITY: First check known installation paths for this specific tool
|
||||
// This avoids searching PATH and reduces attack surface
|
||||
if (toolId && isWindows()) {
|
||||
if (toolId) {
|
||||
const knownPaths = getKnownToolPaths(toolId);
|
||||
for (const knownPath of knownPaths) {
|
||||
const result = await checkKnownPath(knownPath);
|
||||
@@ -592,6 +641,7 @@ const checkRunnable = async (
|
||||
PATH: env.PATH,
|
||||
HOME: env.HOME || env.USERPROFILE,
|
||||
SystemRoot: env.SystemRoot, // Windows needs this
|
||||
PATHEXT: env.PATHEXT, // Windows cmd.exe needs this to resolve .cmd/.bat/.exe extensions
|
||||
};
|
||||
|
||||
for (const args of [["--version"], ["-v"]]) {
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
/**
|
||||
* Model Auto-Sync Scheduler (#488)
|
||||
*
|
||||
* Automatically refreshes model lists for all providers with autoSync enabled
|
||||
* at a configurable interval (default: 24h).
|
||||
* Automatically refreshes model lists for provider connections that have
|
||||
* autoSync enabled in their providerSpecificData, at a configurable
|
||||
* interval (default: 24h).
|
||||
*
|
||||
* Pattern mirrors cloudSyncScheduler.ts for consistency.
|
||||
*/
|
||||
@@ -12,53 +13,67 @@ import { getSettings, updateSettings } from "@/lib/localDb";
|
||||
const DEFAULT_INTERVAL_MS = 24 * 60 * 60 * 1000; // 24 hours
|
||||
const MODEL_SYNC_SETTING_KEY = "model_sync_last_run";
|
||||
|
||||
/** Providers that support live model list fetching via /v1/models */
|
||||
const AUTO_SYNC_PROVIDERS = [
|
||||
"openai",
|
||||
"anthropic",
|
||||
"google",
|
||||
"gemini",
|
||||
"deepseek",
|
||||
"groq",
|
||||
"mistral",
|
||||
"cohere",
|
||||
"openrouter",
|
||||
"together",
|
||||
"fireworks",
|
||||
"perplexity",
|
||||
"xai",
|
||||
"cerebras",
|
||||
"ollama",
|
||||
"nvidia",
|
||||
];
|
||||
|
||||
let schedulerTimer: NodeJS.Timeout | null = null;
|
||||
let isRunning = false;
|
||||
|
||||
/**
|
||||
* Fetch and cache models for a single provider.
|
||||
* Calls the internal /api/providers/{id}/sync-models endpoint (if it exists)
|
||||
* or falls back to /v1/models from the provider registry.
|
||||
* Fetch all provider connections that have autoSync enabled.
|
||||
*/
|
||||
async function syncProviderModels(providerId: string, baseUrl: string): Promise<void> {
|
||||
async function getAutoSyncConnections(): Promise<
|
||||
Array<{ id: string; provider: string; name?: string }>
|
||||
> {
|
||||
try {
|
||||
const res = await fetch(`${baseUrl}/api/provider-nodes/sync-models`, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json", "x-internal": "model-sync-scheduler" },
|
||||
body: JSON.stringify({ provider: providerId }),
|
||||
const { getProviderConnections } = await import("@/lib/localDb");
|
||||
const connections = await getProviderConnections();
|
||||
return connections.filter((conn: any) => {
|
||||
if (!conn.isActive && conn.isActive !== undefined) return false;
|
||||
const psd =
|
||||
conn.providerSpecificData && typeof conn.providerSpecificData === "object"
|
||||
? conn.providerSpecificData
|
||||
: {};
|
||||
return psd.autoSync === true;
|
||||
});
|
||||
if (!res.ok) {
|
||||
console.warn(`[ModelSync] Provider ${providerId}: sync returned ${res.status}`);
|
||||
} else {
|
||||
console.log(`[ModelSync] Provider ${providerId}: ✓ updated`);
|
||||
}
|
||||
} catch (err) {
|
||||
console.warn(`[ModelSync] Provider ${providerId}: fetch failed —`, (err as Error).message);
|
||||
console.warn("[ModelSync] Failed to load connections:", (err as Error).message);
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Run one full model-sync cycle across all auto-sync providers.
|
||||
* Sync models for a single connection via the internal sync-models endpoint.
|
||||
*/
|
||||
async function syncConnectionModels(
|
||||
connectionId: string,
|
||||
provider: string,
|
||||
baseUrl: string
|
||||
): Promise<boolean> {
|
||||
try {
|
||||
const res = await fetch(`${baseUrl}/api/providers/${connectionId}/sync-models`, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json", "x-internal": "model-sync-scheduler" },
|
||||
});
|
||||
if (!res.ok) {
|
||||
console.warn(
|
||||
`[ModelSync] ${provider} (${connectionId.slice(0, 8)}): sync returned ${res.status}`
|
||||
);
|
||||
return false;
|
||||
}
|
||||
const data = await res.json();
|
||||
console.log(
|
||||
`[ModelSync] ${provider} (${connectionId.slice(0, 8)}): ✓ ${data.syncedModels || 0} models`
|
||||
);
|
||||
return true;
|
||||
} catch (err) {
|
||||
console.warn(
|
||||
`[ModelSync] ${provider} (${connectionId.slice(0, 8)}): fetch failed —`,
|
||||
(err as Error).message
|
||||
);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Run one full model-sync cycle across all auto-sync connections.
|
||||
*/
|
||||
async function runSyncCycle(apiBaseUrl: string): Promise<void> {
|
||||
if (isRunning) {
|
||||
@@ -67,26 +82,37 @@ async function runSyncCycle(apiBaseUrl: string): Promise<void> {
|
||||
}
|
||||
isRunning = true;
|
||||
const start = Date.now();
|
||||
console.log(
|
||||
`[ModelSync] Starting 24h model sync cycle — ${AUTO_SYNC_PROVIDERS.length} providers`
|
||||
);
|
||||
|
||||
const results = await Promise.allSettled(
|
||||
AUTO_SYNC_PROVIDERS.map((id) => syncProviderModels(id, apiBaseUrl))
|
||||
);
|
||||
|
||||
const succeeded = results.filter((r) => r.status === "fulfilled").length;
|
||||
console.log(
|
||||
`[ModelSync] Cycle complete: ${succeeded}/${AUTO_SYNC_PROVIDERS.length} providers synced in ${Date.now() - start}ms`
|
||||
);
|
||||
|
||||
// Record last sync time
|
||||
try {
|
||||
await updateSettings({ [MODEL_SYNC_SETTING_KEY]: new Date().toISOString() });
|
||||
} catch {
|
||||
// Non-critical
|
||||
const connections = await getAutoSyncConnections();
|
||||
|
||||
if (connections.length === 0) {
|
||||
console.log("[ModelSync] No connections with autoSync enabled — skipping cycle");
|
||||
return;
|
||||
}
|
||||
|
||||
console.log(`[ModelSync] Starting model sync cycle — ${connections.length} connection(s)`);
|
||||
|
||||
const results = await Promise.allSettled(
|
||||
connections.map((conn) =>
|
||||
syncConnectionModels(conn.id, conn.name || conn.provider, apiBaseUrl)
|
||||
)
|
||||
);
|
||||
|
||||
const succeeded = results.filter((r) => r.status === "fulfilled" && r.value === true).length;
|
||||
console.log(
|
||||
`[ModelSync] Cycle complete: ${succeeded}/${connections.length} synced in ${Date.now() - start}ms`
|
||||
);
|
||||
|
||||
// Record last sync time
|
||||
try {
|
||||
await updateSettings({ [MODEL_SYNC_SETTING_KEY]: new Date().toISOString() });
|
||||
} catch {
|
||||
// Non-critical
|
||||
}
|
||||
} finally {
|
||||
isRunning = false;
|
||||
}
|
||||
isRunning = false;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -108,9 +134,7 @@ export function startModelSyncScheduler(
|
||||
const effectiveIntervalMs =
|
||||
!isNaN(envHours) && envHours > 0 ? envHours * 60 * 60 * 1000 : intervalMs;
|
||||
|
||||
console.log(
|
||||
`[ModelSync] Scheduler started — interval: ${effectiveIntervalMs / 3_600_000}h, providers: ${AUTO_SYNC_PROVIDERS.length}`
|
||||
);
|
||||
console.log(`[ModelSync] Scheduler started — interval: ${effectiveIntervalMs / 3_600_000}h`);
|
||||
|
||||
// Run immediately on startup (staggered by 5s to avoid startup congestion)
|
||||
const startupDelay = setTimeout(() => runSyncCycle(apiBaseUrl), 5_000);
|
||||
|
||||
@@ -59,13 +59,15 @@ test("contract: /api/v1/models returns OpenAI-compatible model shape", async ()
|
||||
|
||||
assert.equal(body.object, "list");
|
||||
assert.ok(Array.isArray(body.data));
|
||||
assert.ok(body.data.length > 0, "models list should not be empty");
|
||||
|
||||
const first = body.data[0];
|
||||
assert.equal(typeof first.id, "string");
|
||||
assert.equal(first.object, "model");
|
||||
assert.equal(typeof first.created, "number");
|
||||
assert.equal(typeof first.owned_by, "string");
|
||||
// In CI environments without provider connections, models list may be empty — skip shape check
|
||||
if (body.data.length > 0) {
|
||||
const first = body.data[0];
|
||||
assert.equal(typeof first.id, "string");
|
||||
assert.equal(first.object, "model");
|
||||
assert.equal(typeof first.created, "number");
|
||||
assert.equal(typeof first.owned_by, "string");
|
||||
}
|
||||
});
|
||||
|
||||
test("contract: /api/v1/embeddings GET returns embedding model listing shape", async () => {
|
||||
|
||||
@@ -0,0 +1,202 @@
|
||||
/**
|
||||
* Tests for CLI tool detection: cross-platform known paths, size threshold,
|
||||
* npm prefix deduplication, and env var overrides.
|
||||
*/
|
||||
|
||||
import { describe, it, before, after } from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import fs from "node:fs";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
|
||||
const { getCliRuntimeStatus, CLI_TOOL_IDS } =
|
||||
await import("../../src/shared/services/cliRuntime.ts");
|
||||
|
||||
// ─── Helpers ──────────────────────────────────────────────────
|
||||
|
||||
function createTempDir() {
|
||||
return fs.mkdtempSync(path.join(os.tmpdir(), "cli-test-"));
|
||||
}
|
||||
|
||||
function createFile(dir, name, content) {
|
||||
const filePath = path.join(dir, name);
|
||||
fs.writeFileSync(filePath, content);
|
||||
if (process.platform !== "win32") {
|
||||
fs.chmodSync(filePath, 0o755);
|
||||
}
|
||||
return filePath;
|
||||
}
|
||||
|
||||
// ─── CLI_TOOL_IDS ─────────────────────────────────────────────
|
||||
|
||||
describe("CLI_TOOL_IDS", () => {
|
||||
it("should include all expected tools", () => {
|
||||
const expected = [
|
||||
"claude",
|
||||
"codex",
|
||||
"droid",
|
||||
"openclaw",
|
||||
"cursor",
|
||||
"cline",
|
||||
"kilo",
|
||||
"continue",
|
||||
"opencode",
|
||||
];
|
||||
for (const id of expected) {
|
||||
assert.ok(CLI_TOOL_IDS.includes(id), `Missing tool: ${id}`);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
// ─── Size Threshold (30 bytes) ────────────────────────────────
|
||||
|
||||
describe("Size threshold — checkKnownPath", () => {
|
||||
let tmpDir;
|
||||
|
||||
before(() => {
|
||||
tmpDir = createTempDir();
|
||||
});
|
||||
|
||||
after(() => {
|
||||
fs.rmSync(tmpDir, { recursive: true, force: true });
|
||||
});
|
||||
|
||||
it("should detect files >= 30 bytes via env var", async () => {
|
||||
const prev = process.env.CLI_DROID_BIN;
|
||||
// Create a valid 30-byte+ script
|
||||
const content =
|
||||
process.platform === "win32"
|
||||
? "@echo off\r\necho 1.0.0\r\nexit 0\r\n"
|
||||
: "#!/bin/sh\r\necho 1.0.0\r\nexit 0\r\n";
|
||||
const script = createFile(tmpDir, "droid-valid", content);
|
||||
// Verify it's at least 30 bytes
|
||||
const stat = fs.statSync(script);
|
||||
assert.ok(stat.size >= 30, `File should be >= 30 bytes, got ${stat.size}`);
|
||||
|
||||
process.env.CLI_DROID_BIN = script;
|
||||
try {
|
||||
const result = await getCliRuntimeStatus("droid");
|
||||
assert.ok(result.installed, `Expected installed=true, got reason=${result.reason}`);
|
||||
assert.ok(result.commandPath === script, `Expected commandPath=${script}`);
|
||||
} finally {
|
||||
if (prev !== undefined) process.env.CLI_DROID_BIN = prev;
|
||||
else delete process.env.CLI_DROID_BIN;
|
||||
}
|
||||
});
|
||||
|
||||
it("should detect a valid CLI script (>= 30 bytes) via env var", async () => {
|
||||
const prev = process.env.CLI_DROID_BIN;
|
||||
const script =
|
||||
process.platform === "win32"
|
||||
? createFile(tmpDir, "droid.cmd", "@echo off\necho 1.0.0\n")
|
||||
: createFile(tmpDir, "droid", "#!/bin/sh\necho 1.0.0\n");
|
||||
|
||||
process.env.CLI_DROID_BIN = script;
|
||||
try {
|
||||
const result = await getCliRuntimeStatus("droid");
|
||||
assert.ok(result.installed, `Expected installed=true, got reason=${result.reason}`);
|
||||
assert.ok(
|
||||
result.commandPath === script,
|
||||
`Expected commandPath=${script}, got ${result.commandPath}`
|
||||
);
|
||||
} finally {
|
||||
if (prev !== undefined) process.env.CLI_DROID_BIN = prev;
|
||||
else delete process.env.CLI_DROID_BIN;
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
// ─── Healthcheck with --version ───────────────────────────────
|
||||
|
||||
describe("Healthcheck — checkRunnable", () => {
|
||||
let tmpDir;
|
||||
|
||||
before(() => {
|
||||
tmpDir = createTempDir();
|
||||
});
|
||||
|
||||
after(() => {
|
||||
fs.rmSync(tmpDir, { recursive: true, force: true });
|
||||
});
|
||||
|
||||
it("should report runnable=true for a script that outputs version", async () => {
|
||||
const prev = process.env.CLI_CLINE_BIN;
|
||||
const script =
|
||||
process.platform === "win32"
|
||||
? createFile(tmpDir, "good.cmd", "@echo off\necho 1.0.0\n")
|
||||
: createFile(tmpDir, "good", "#!/bin/sh\necho 1.0.0\n");
|
||||
|
||||
process.env.CLI_CLINE_BIN = script;
|
||||
try {
|
||||
const result = await getCliRuntimeStatus("cline");
|
||||
assert.ok(result.installed, `Expected installed=true, got reason=${result.reason}`);
|
||||
if (result.runnable) {
|
||||
assert.ok(result.reason === null, `Expected no reason, got ${result.reason}`);
|
||||
}
|
||||
} finally {
|
||||
if (prev !== undefined) process.env.CLI_CLINE_BIN = prev;
|
||||
else delete process.env.CLI_CLINE_BIN;
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
// ─── Unknown tool ─────────────────────────────────────────────
|
||||
|
||||
describe("Unknown tool", () => {
|
||||
it("should return unknown_tool for non-existent tool", async () => {
|
||||
const result = await getCliRuntimeStatus("nonexistent-tool-xyz");
|
||||
assert.equal(result.installed, false);
|
||||
assert.equal(result.reason, "unknown_tool");
|
||||
});
|
||||
});
|
||||
|
||||
// ─── continue tool (requiresBinary: false) ────────────────────
|
||||
|
||||
describe("continue tool — no binary required", () => {
|
||||
it("should report installed=true without checking binary", async () => {
|
||||
const result = await getCliRuntimeStatus("continue");
|
||||
assert.equal(result.installed, true);
|
||||
assert.equal(result.reason, "not_required");
|
||||
});
|
||||
});
|
||||
|
||||
// ─── resolveOpencodeConfigPath — cross-platform ─────────────────
|
||||
|
||||
const { resolveOpencodeConfigPath: resolveOpencodeConfigPathFn } =
|
||||
await import("../../src/shared/services/cliRuntime.ts");
|
||||
|
||||
describe("resolveOpencodeConfigPath — cross-platform", () => {
|
||||
it("should resolve on Linux with XDG_CONFIG_HOME", () => {
|
||||
const result = resolveOpencodeConfigPathFn(
|
||||
"linux",
|
||||
{ XDG_CONFIG_HOME: "/tmp/xdg" },
|
||||
"/home/dev"
|
||||
);
|
||||
assert.equal(result, path.join("/tmp/xdg", "opencode", "opencode.json"));
|
||||
});
|
||||
|
||||
it("should resolve on Linux with default .config", () => {
|
||||
const result = resolveOpencodeConfigPathFn("linux", {}, "/home/dev");
|
||||
assert.equal(result, path.join("/home/dev", ".config", "opencode", "opencode.json"));
|
||||
});
|
||||
|
||||
it("should resolve on Windows with APPDATA", () => {
|
||||
const result = resolveOpencodeConfigPathFn(
|
||||
"win32",
|
||||
{ APPDATA: "C:\\Users\\dev\\AppData\\Roaming" },
|
||||
"C:\\Users\\dev"
|
||||
);
|
||||
assert.equal(
|
||||
result,
|
||||
path.join("C:\\Users\\dev\\AppData\\Roaming", "opencode", "opencode.json")
|
||||
);
|
||||
});
|
||||
|
||||
it("should fallback to home/AppData/Roaming on Windows without APPDATA", () => {
|
||||
const result = resolveOpencodeConfigPathFn("win32", {}, "C:\\Users\\dev");
|
||||
assert.equal(
|
||||
result,
|
||||
path.join("C:\\Users\\dev", "AppData", "Roaming", "opencode", "opencode.json")
|
||||
);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user