diff --git a/.dockerignore b/.dockerignore index 58610da53da142013553d685cab8f75a003d4360..3218ab90abb77c062aafc4ce7469a2188acc3b2c 100644 --- a/.dockerignore +++ b/.dockerignore @@ -1,21 +1,10 @@ .git .github node_modules -client/node_modules -server/node_modules -shared/node_modules -client/dist -server/dist -server/data -dist +*.log .env .env.* -*.db -*.db-wal -*.db-shm -*.bak -.encryption-key -.DS_Store -npm-debug.log* -repo-assets -free-ai-apis.pdf +*.sqlite* +*.db* +lost+found +__pycache__/ diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..c18dd8d83ceed1806b50b0aaa46beb7e335fff13 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +__pycache__/ diff --git a/Dockerfile b/Dockerfile index 5ef8153c569d3ac93b951981acb32cc40042d0b6..39c9670f5949f78245ed794348055c574518c82a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,65 +1,45 @@ # syntax=docker/dockerfile:1.7 - -ARG NODE_IMAGE=node:24.22.1-bookworm-slim - -FROM ${NODE_IMAGE} AS deps -WORKDIR /app - -# better-sqlite3 is a native module; compiles from source via node-gyp with python3 + C++ toolchain -RUN apt-get update \ - && apt-get install -y --no-install-recommends python3 make g++ \ - && rm -rf /var/lib/apt/lists/* - -COPY package.json package-lock.json ./ -COPY shared/package.json ./shared/ -COPY server/package.json ./server/ -COPY client/package.json ./client/ -COPY cli/package.json ./cli/ - -RUN npm ci - -FROM deps AS build -WORKDIR /app - -COPY . . - -RUN npm run build -RUN npm prune --omit=dev - -FROM ${NODE_IMAGE} AS runtime -WORKDIR /app - -ENV NODE_ENV=production -ENV PORT=7860 -ENV HOST=0.0.0.0 -ENV ALLOWED_HOSTS=* -ENV FREELLMAPI_INSTALL_METHOD=docker -ENV FREEAPI_DB_PATH=/data/freellmapi.db -ENV FREEAPI_DB_BACKUP_PATH=/data/backups/freellmapi.db.backup -ENV FREEAPI_DB_DIR_HARDENING=0 -ENV TRUST_PROXY=true - -COPY --from=build --chown=node:node /app/package.json /app/package-lock.json ./ -COPY --from=build --chown=node:node /app/node_modules ./node_modules -COPY --from=build --chown=node:node /app/server/node_modules ./server/node_modules -COPY --from=build --chown=node:node /app/shared ./shared -COPY --from=build --chown=node:node /app/server/package.json ./server/package.json -COPY --from=build --chown=node:node /app/desktop/package.json ./desktop/package.json -COPY --from=build --chown=node:node /app/server/dist ./server/dist -COPY --from=build --chown=node:node /app/client/dist ./client/dist - -RUN mkdir -p /app/server/data /data /data/logs /data/backups \ - && chown -R node:node /app /data - -COPY --chmod=755 docker-entrypoint.sh /usr/local/bin/docker-entrypoint.sh +FROM diegosouzapw/omniroute:latest USER root -ENTRYPOINT ["/usr/local/bin/docker-entrypoint.sh"] +# Install Python runtime, pip, sqlite3, and ca-certificates for dataset persistence sync +RUN apt-get update && apt-get install -y --no-install-recommends \ + python3 \ + python3-pip \ + sqlite3 \ + ca-certificates \ + curl \ + && rm -rf /var/lib/apt/lists/* \ + && pip3 install --no-cache-dir --break-system-packages "huggingface_hub[cli]>=0.23.0" + +# Prepare persistent data mount path and assign ownership to node user (UID 1000) +RUN mkdir -p /data /app/data \ + && chown -R node:node /data /app/data \ + && chmod -R 775 /data + +# Deploy scripts +COPY --chown=node:node --chmod=755 sync.py /usr/local/bin/sync.py +COPY --chown=node:node --chmod=755 docker-entrypoint.sh /usr/local/bin/docker-entrypoint.sh + +# Hugging Face Spaces port, routing and storage configuration +ENV PORT=7860 \ + DASHBOARD_PORT=7860 \ + API_PORT=7860 \ + HOSTNAME=0.0.0.0 \ + APP_BIND_HOST=0.0.0.0 \ + DATA_DIR=/data \ + NODE_ENV=production \ + HF_DATASET_REPO=userbymahadi/OmniRoute-storage + +# Switch to standard non-root user (UID 1000) required by Hugging Face Spaces +USER node EXPOSE 7860 -HEALTHCHECK --interval=30s --timeout=5s --start-period=15s --retries=3 \ - CMD node -e "fetch('http://127.0.0.1:' + (process.env.PORT || 7860) + '/api/ping').then((res) => { if (!res.ok) process.exit(1); }).catch(() => process.exit(1));" +ENTRYPOINT ["/usr/local/bin/docker-entrypoint.sh"] + +HEALTHCHECK --interval=30s --timeout=5s --start-period=20s --retries=3 \ + CMD node -e "fetch('http://127.0.0.1:' + (process.env.PORT || 7860) + '/healthz').then((res) => { if (!res.ok) process.exit(1); }).catch(() => process.exit(1));" -CMD ["node", "server/dist/index.js"] +CMD ["node", "dev/run-standalone.mjs"] diff --git a/README.md b/README.md index b59e4a7bd6bf02b828d0b1b4405b9699e8c4ec96..a219a28d53019fe284805ea05b43d50ba5ad16db 100644 --- a/README.md +++ b/README.md @@ -1,19 +1,24 @@ --- -title: FreeLLMAPI -emoji: ⚡ -colorFrom: indigo -colorTo: red +title: OmniRoute +emoji: 🔀 +colorFrom: blue +colorTo: indigo sdk: docker app_port: 7860 pinned: false --- -# FreeLLMAPI on Hugging Face Spaces +# OmniRoute on Hugging Face Spaces -A unified reverse-proxy and router for free & paid LLM provider APIs with automatic failover, health probing, load balancing, and persistent storage. +OmniRoute is an open-source, production-grade AI gateway and intelligent multi-provider router with unified OpenAI-compatible endpoints, latency optimization, and automated failover. -## Features -- **Persistent Storage**: SQLite database, tokens, encryption key, and logs persist in Hugging Face Bucket Storage (`userbymahadi/FreeLLMAPI-storage`) mounted at `/data`. -- **Automatic Fallback Chain**: Zero-downtime routing across multiple LLM providers. -- **Unified API Endpoint**: OpenAI-compatible API endpoint `/v1/chat/completions`. -- **Health Probing**: Real-time status checks and latency measurement. +## Deployment Specifications +- **App & API Port**: `7860` (standard Hugging Face Docker port) +- **Persistent Data Directory**: `/data` (SQLite database, configuration, and API keys) +- **Cloud State Synchronization**: Automatically linked to dataset repository `userbymahadi/OmniRoute-storage` +- **Upstream Repository**: [cheaper-inference/OmniRoute](https://github.com/cheaper-inference/OmniRoute) +- **Official Docker Guide**: [OmniRoute DOCKER_GUIDE.md](https://github.com/cheaper-inference/OmniRoute/blob/release/v3.8.53/docs/guides/DOCKER_GUIDE.md) + +## Persistence Architecture +1. **Hugging Face Hardware Persistent Storage**: If persistent storage is enabled in Space settings, `/data` is backed by dedicated volume storage. +2. **Dataset Synchronization Daemon**: When `HF_TOKEN` is configured as a Secret, the container synchronizes all state (`storage.sqlite`, `server.env`) to the private dataset `userbymahadi/OmniRoute-storage` at startup, every 5 minutes during runtime, and upon graceful shutdown. diff --git a/cli/LICENSE b/cli/LICENSE deleted file mode 100644 index 8eb2b075b37107bd0403ddacc47ddcb97d46843b..0000000000000000000000000000000000000000 --- a/cli/LICENSE +++ /dev/null @@ -1,21 +0,0 @@ -MIT License - -Copyright (c) 2026 Tashfeen Ahmed - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. diff --git a/cli/README.md b/cli/README.md deleted file mode 100644 index c5b8f5854cc4d6bbfb9c8631d5243734bb2b1023..0000000000000000000000000000000000000000 --- a/cli/README.md +++ /dev/null @@ -1,117 +0,0 @@ -# freellmapi - -Point your coding agent at a [FreeLLMAPI](https://github.com/tashfeenahmed/freellmapi) -gateway in one command. The generators read the models your server is actually -serving and write the config file each tool expects. - -```bash -npx freellmapi setup-claude --url http://localhost:3001 --api-key -``` - -No install step, no account. The unified API key comes from your FreeLLMAPI -dashboard (or the tray popover in the desktop app). - -## Commands - -| Command | Tool | -| --- | --- | -| `setup-claude` | Claude Code | -| `setup-codex` | Codex CLI | -| `setup-cline` | Cline | -| `setup-continue` | Continue | -| `setup-aider` | Aider | -| `setup-opencode` | OpenCode | -| `setup-goose` | Goose | -| `setup-qwen` | Qwen Code | -| `setup-roo` | Roo Code | -| `setup-kilo` | Kilo Code | -| `setup-crush` | Crush | -| `setup-dsh` | DeepSeek Harness (`dsh`) | -| `setup-mimo` | MiMo Code (`mimo`) | -| `setup-atomcode` | AtomCode (`atomcode`) | -| `setup-openclaw` | OpenClaw | -| `setup-hermes` | Hermes Agent (`hermes`) | -| `setup-pi` | Pi (`pi`) | -| `setup-reasonix` | Reasonix (`reasonix`) | -| `setup-zcode` | ZCode (`zcode`) | -| `setup-cursor` | Cursor | -| `setup-generic` | Any OpenAI-compatible client | -| `launch` | Run Claude Code with credentials injected into the child process | -| `launch-codex` | Run Codex the same way | -| `list` | Print the supported tools and their base URLs | -| `keys` | Add, list, remove, or test provider keys (`keys --help`) | - -## Options - -| Flag | Meaning | -| --- | --- | -| `--url URL` | Gateway base URL (default `http://localhost:3001`) | -| `--api-key KEY` | Unified API key | -| `--profile NAME` | Name the generated profile/provider entry | -| `--model ID` | Pin a specific model instead of the catalog default | -| `--dry-run` | Print the diff and write nothing | - -`FREELLMAPI_URL` and `FREELLMAPI_API_KEY` work in place of `--url` / `--api-key`. - -## Provider keys - -The `keys` commands use a **dashboard session token**. Set -`FREELLMAPI_DASHBOARD_TOKEN`, or pass `--token TOKEN` to override it. This is the -session returned by dashboard sign-in (stored in the signed-in browser's local -storage as `freellmapi_dashboard_token`). The unified `--api-key` / -`FREELLMAPI_API_KEY` used by coding agents cannot authenticate these commands. -The CLI does not log in or persist the dashboard token. - -```bash -export FREELLMAPI_URL=http://localhost:3001 -export FREELLMAPI_DASHBOARD_TOKEN='' - -npx freellmapi keys add groq # hidden provider-key prompt -npx freellmapi keys add groq --key '' # non-interactive alternative -npx freellmapi keys list -npx freellmapi keys test groq # check every stored Groq key -npx freellmapi keys test groq --id 7 # check one key -npx freellmapi keys remove groq --id 7 -``` - -`keys list` prints each key's ID, platform, enabled chat model count, -enabled/disabled state, and stored health status. Counts respect the key's model -scope; custom keys count models registered to their endpoint. Credentials and -masked keys are never printed. Use the IDs from this list when a platform has -multiple keys: `remove` requires `--id` in that case, and also checks that the ID -belongs to the requested platform. With one matching key, `--id` is optional. - -`add` saves the key and immediately validates it through the gateway. A failed or -inconclusive check exits with code 1 and **leaves the key saved** for later testing -or removal. `test` also exits with code 1 if any selected key is not healthy or the -gateway cannot confirm its validity (for example, after a provider network -failure). Successful commands exit with code 0; argument, authentication, and -request errors exit with code 1. Validation uses the gateway's usual health -checks, including its policy of disabling a key after repeated confirmed failures. - -Keyless providers skip the prompt. Custom endpoints need a URL and model -registration, so create them in the dashboard; existing custom keys can be -listed, tested, and removed here. Requests accept `--url` / `FREELLMAPI_URL` and -`--timeout MS` (30,000 ms per request by default). `--dry-run` is not supported for -`keys`; it is rejected before any request. - -## Safety - -Every generator is non-destructive: it merges into your existing configuration -rather than replacing it, and takes a timestamped backup before touching a file -that already exists. `--dry-run` shows the exact diff first. - -The two `launch` commands never write credentials to disk at all — they inject -them into the child process environment for that run only. - -## Requirements - -Node.js >= 20.18. A running FreeLLMAPI gateway -([install guide](https://github.com/tashfeenahmed/freellmapi/blob/main/docs/en/install/01-install.md)). - -## Links - -- [Clients & coding agents guide](https://github.com/tashfeenahmed/freellmapi/blob/main/docs/en/clients/01-agent-clients.md) -- [Issue tracker](https://github.com/tashfeenahmed/freellmapi/issues) - -MIT Š Tashfeen Ahmed diff --git a/cli/eslint.config.js b/cli/eslint.config.js deleted file mode 100644 index 8c97539c82f826c14d0e55561115dd4866793634..0000000000000000000000000000000000000000 --- a/cli/eslint.config.js +++ /dev/null @@ -1,28 +0,0 @@ -import js from '@eslint/js' -import globals from 'globals' -import tseslint from 'typescript-eslint' -import { defineConfig, globalIgnores } from 'eslint/config' - -export default defineConfig([ - globalIgnores(['dist']), - { - files: ['**/*.ts'], - extends: [ - js.configs.recommended, - tseslint.configs.recommended, - ], - languageOptions: { - ecmaVersion: 2022, - globals: globals.node, - }, - rules: { - '@typescript-eslint/no-explicit-any': 'off', - '@typescript-eslint/no-unused-vars': ['error', { - argsIgnorePattern: '^_', - varsIgnorePattern: '^_', - caughtErrorsIgnorePattern: '^_', - }], - 'no-empty': ['error', { allowEmptyCatch: true }], - }, - }, -]) diff --git a/cli/package.json b/cli/package.json deleted file mode 100644 index 4a4c0d8fa18d492789016d5ca7c16acfcd52867f..0000000000000000000000000000000000000000 --- a/cli/package.json +++ /dev/null @@ -1,77 +0,0 @@ -{ - "name": "freellmapi", - "version": "0.8.2", - "description": "Set up coding agents to use a FreeLLMAPI gateway", - "license": "MIT", - "repository": { - "type": "git", - "url": "git+https://github.com/tashfeenahmed/freellmapi.git", - "directory": "cli" - }, - "homepage": "https://github.com/tashfeenahmed/freellmapi/tree/main/cli#readme", - "bugs": { - "url": "https://github.com/tashfeenahmed/freellmapi/issues" - }, - "keywords": [ - "freellmapi", - "llm", - "openai-compatible", - "claude-code", - "codex", - "aider", - "cline", - "coding-agent", - "cli" - ], - "type": "module", - "bin": { - "freellmapi": "dist/index.js" - }, - "exports": { - ".": "./dist/index.js", - "./tools": "./dist/tools.js" - }, - "files": [ - "README.md", - "LICENSE", - "dist/index.js", - "dist/index.d.ts", - "dist/config-files.js", - "dist/config-files.d.ts", - "dist/doctor.js", - "dist/doctor.d.ts", - "dist/keys.js", - "dist/keys.d.ts", - "dist/models.js", - "dist/models.d.ts", - "dist/tools.js", - "dist/tools.d.ts", - "dist/types.js", - "dist/types.d.ts", - "tools.json" - ], - "scripts": { - "build": "tsc", - "test": "vitest run", - "lint": "eslint .", - "prepublishOnly": "npm run build && npm test" - }, - "engines": { - "node": ">=20.18.0 <25.0.0" - }, - "dependencies": { - "cross-spawn": "^7.0.6", - "jsonc-parser": "^3.3.1", - "yaml": "^2.9.0" - }, - "devDependencies": { - "@types/cross-spawn": "^6.0.6", - "@types/node": "^24.12.2", - "typescript": "~6.0.2", - "vitest": "^3.1.3", - "@eslint/js": "^9.39.4", - "eslint": "^9.39.4", - "globals": "^17.4.0", - "typescript-eslint": "^8.58.0" - } -} diff --git a/cli/src/__snapshots__/tools.test.ts.snap b/cli/src/__snapshots__/tools.test.ts.snap deleted file mode 100644 index 0914ea1250a9ddc3f7e811a73a221eb1529fdb80..0000000000000000000000000000000000000000 --- a/cli/src/__snapshots__/tools.test.ts.snap +++ /dev/null @@ -1,1009 +0,0 @@ -// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html - -exports[`tool generators > setup-aider has stable golden output 1`] = ` -{ - "files": [ - { - "content": "# freellmapi:start -openai-api-base: "http://localhost:3000/v1" -openai-api-key: "freellmapi-test-key" -model: "openai/fast-coder" -# freellmapi:end -", - "format": "yaml", - "path": "/home/tester/.aider.conf.yml", - "sensitive": true, - }, - ], - "notes": [ - "Environment-only alternative: OPENAI_API_BASE=http://localhost:3000/v1 OPENAI_API_KEY=â€Ļ aider --model openai/fast-coder", - ], -} -`; - -exports[`tool generators > setup-atomcode has stable golden output 1`] = ` -{ - "files": [ - { - "content": "default_provider = "freellmapi" - -[providers.freellmapi] -type = "openai" -base_url = "http://localhost:3000/v1" -api_key = "freellmapi-test-key" -model = "fast-coder" -context_window = 131072", - "format": "toml", - "path": "/home/tester/.atomcode/config.toml", - "sensitive": true, - }, - ], - "notes": [ - "AtomCode reads ~/.atomcode/config.toml; other [providers.*] tables in it are kept.", - "default_provider now points at [providers.freellmapi] with fast-coder as its model.", - "Point base_url at the unified /v1 endpoint; api_key is the unified key shown on the Agents page.", - "One-shot check: atomcode -p "Say hello" (telemetry is on by default: atomcode telemetry disable).", - ], -} -`; - -exports[`tool generators > setup-claude has stable golden output 1`] = ` -{ - "files": [ - { - "format": "json", - "path": "/home/tester/.claude/settings.json", - "sensitive": true, - "value": { - "env": { - "ANTHROPIC_AUTH_TOKEN": "freellmapi-test-key", - "ANTHROPIC_BASE_URL": "http://localhost:3000", - "ANTHROPIC_DEFAULT_HAIKU_MODEL": "fast-coder", - "ANTHROPIC_DEFAULT_OPUS_MODEL": "fast-coder", - "ANTHROPIC_DEFAULT_SONNET_MODEL": "fast-coder", - "ANTHROPIC_MODEL": "fast-coder", - "CLAUDE_CODE_AUTO_COMPACT_WINDOW": "131072", - "CLAUDE_CODE_ENABLE_GATEWAY_MODEL_DISCOVERY": "1", - }, - }, - }, - ], - "notes": [ - "Claude Code will read this configuration automatically.", - "For zero-persistence credentials, prefer: freellmapi launch", - ], -} -`; - -exports[`tool generators > setup-cline has stable golden output 1`] = ` -{ - "files": [ - { - "format": "json", - "path": "/home/tester/.cline/data/settings/providers.json", - "sensitive": true, - "value": { - "lastUsedProvider": "openai-compatible", - "providers": { - "openai-compatible": { - "settings": { - "apiKey": "freellmapi-test-key", - "baseUrl": "http://localhost:3000/v1", - "capabilities": [ - "streaming", - "tools", - ], - "client": "openai-compatible", - "contextWindow": 131072, - "model": "fast-coder", - "protocol": "openai-chat", - "provider": "openai-compatible", - }, - "tokenSource": "manual", - "updatedAt": "2026-07-27T00:00:00.000Z", - }, - }, - "version": 1, - }, - }, - ], - "notes": [ - "Cline will use the generated OpenAI Compatible provider immediately.", - ], -} -`; - -exports[`tool generators > setup-codex has stable golden output 1`] = ` -{ - "files": [ - { - "content": "# freellmapi:start -model = "fast-coder" -model_provider = "freellmapi" -model_context_window = 131072 -model_auto_compact_token_limit = 117964 -tool_output_token_limit = 20000 - -[model_providers.freellmapi] -name = "FreeLLMAPI" -base_url = "http://localhost:3000/v1" -wire_api = "responses" -env_key = "FREELLMAPI_API_KEY" -requires_openai_auth = false -# freellmapi:end -", - "format": "toml", - "path": "/home/tester/.codex/config.toml", - }, - ], - "notes": [ - "No Codex CLI detected on this machine.", - "Install the CLI with: npm install -g @openai/codex", - "Export FREELLMAPI_API_KEY before running codex; the key is not written to config.toml.", - "Selected fast-coder with a 131072 token context window.", - ], -} -`; - -exports[`tool generators > setup-codex with a named profile has stable golden output 1`] = ` -{ - "files": [ - { - "content": "# freellmapi:start -[model_providers.freellmapi] -name = "FreeLLMAPI" -base_url = "http://localhost:3000/v1" -wire_api = "responses" -env_key = "FREELLMAPI_API_KEY" -requires_openai_auth = false - -[profiles.work] -model = "fast-coder" -model_provider = "freellmapi" -# freellmapi:end -", - "format": "toml", - "path": "/home/tester/.codex/config.toml", - }, - ], - "notes": [ - "No Codex CLI detected on this machine.", - "Install the CLI with: npm install -g @openai/codex", - "Export FREELLMAPI_API_KEY before running codex; the key is not written to config.toml.", - "Activate this profile with: codex --profile work", - "Selected fast-coder with a 131072 token context window.", - ], -} -`; - -exports[`tool generators > setup-continue has stable golden output 1`] = ` -{ - "files": [ - { - "content": "# freellmapi:start -name: FreeLLMAPI -version: 1.0.0 -schema: v1 -models: - - name: FreeLLMAPI - provider: openai - model: "fast-coder" - apiBase: "http://localhost:3000/v1" - apiKey: \${{ secrets.FREELLMAPI_API_KEY }} - capabilities: - - tool_use - defaultCompletionOptions: - contextLength: 131072 -# freellmapi:end -", - "format": "yaml", - "path": "/home/tester/.continue/config.yaml", - }, - { - "content": "FREELLMAPI_API_KEY=freellmapi-test-key -", - "format": "env", - "path": "/home/tester/.continue/.env", - "sensitive": true, - }, - ], - "notes": [ - "The key is in ~/.continue/.env (read by the Continue CLI and the IDE extensions); config.yaml references it as a secret.", - "One-shot check with the CLI (npm install -g @continuedev/cli): cn -p "Say hello"", - ], -} -`; - -exports[`tool generators > setup-crush has stable golden output 1`] = ` -{ - "files": [ - { - "format": "json", - "path": "/home/tester/.config/crush/crush.json", - "value": { - "$schema": "https://charm.land/crush.json", - "models": { - "large": { - "model": "fast-coder", - "provider": "freellmapi", - }, - "small": { - "model": "fast-coder", - "provider": "freellmapi", - }, - }, - "providers": { - "freellmapi": { - "api_key": "$FREELLMAPI_API_KEY", - "base_url": "http://localhost:3000/v1", - "models": [ - { - "can_reason": false, - "context_window": 131072, - "cost_per_1m_in": 0, - "cost_per_1m_in_cached": 0, - "cost_per_1m_out": 0, - "cost_per_1m_out_cached": 0, - "default_max_tokens": 8192, - "id": "fast-coder", - "name": "Fast Coder", - "supports_attachments": false, - }, - { - "can_reason": false, - "context_window": 262144, - "cost_per_1m_in": 0, - "cost_per_1m_in_cached": 0, - "cost_per_1m_out": 0, - "cost_per_1m_out_cached": 0, - "default_max_tokens": 8192, - "id": "reasoning-model", - "name": "Reasoning Model", - "supports_attachments": false, - }, - ], - "name": "FreeLLMAPI", - "type": "openai-compat", - }, - }, - }, - }, - ], - "notes": [ - "Export FREELLMAPI_API_KEY before starting Crush.", - "freellmapi/fast-coder is now the large and small model.", - ], -} -`; - -exports[`tool generators > setup-cursor has stable golden output 1`] = ` -{ - "files": [], - "notes": [ - "Cursor sends model traffic through its cloud service, so localhost is not reachable from Cursor.", - "Expose the gateway through a trusted HTTPS tunnel, then open Cursor Settings → Models, enable Override OpenAI Base URL, and enter /v1.", - "Cursor only documents custom keys for standard chat models; custom base-URL overrides may not support Responses-based or built-in models.", - "The override affects Cursor globally while enabled, so disable it before switching back to built-in providers.", - "Use a separately revocable URL token if the client cannot set an Authorization header.", - ], -} -`; - -exports[`tool generators > setup-dsh has stable golden output 1`] = ` -{ - "files": [ - { - "format": "yaml", - "path": "/home/tester/.dsh/settings.yaml", - "value": { - "agent-default-model": { - "model": "fast-coder", - "provider": "freellmapi", - "reasoningEffort": undefined, - }, - "llm-pi-ai": { - "providers": { - "freellmapi": { - "api": "openai-completions", - "apiKeyEnv": "FREELLMAPI_API_KEY", - "baseURL": "http://localhost:3000/v1", - "displayName": "FreeLLMAPI", - "models": [ - { - "contextWindow": 131072, - "id": "fast-coder", - "maxTokens": 8192, - "name": "Fast Coder", - }, - { - "contextWindow": 262144, - "id": "reasoning-model", - "maxTokens": 8192, - "name": "Reasoning Model", - }, - ], - }, - }, - }, - }, - }, - { - "content": "FREELLMAPI_API_KEY=freellmapi-test-key -", - "format": "env", - "path": "/home/tester/.dsh/.env", - "sensitive": true, - }, - ], - "notes": [ - "Start DeepSeek Harness with: npx @deepseek-ai/dsh web — freellmapi/fast-coder is the default model.", - "One-shot check: npx @deepseek-ai/dsh --profile headless "Say hello"", - "Settings are hot-reloaded, so a running dsh picks this up on its next request.", - "Models are declared text-only; give a vision model \`input: [text, image]\` under freellmapi.models in settings.yaml to send it images.", - ], -} -`; - -exports[`tool generators > setup-generic has stable golden output 1`] = ` -{ - "files": [], - "notes": [ - "OPENAI_BASE_URL=http://localhost:3000/v1", - "OPENAI_API_KEY=freellmapi-test-key", - "OPENAI_MODEL=fast-coder", - "curl http://localhost:3000/v1/chat/completions -H "Authorization: Bearer $OPENAI_API_KEY" -H "Content-Type: application/json" -d '{"model":"fast-coder","messages":[{"role":"user","content":"Hello"}]}'", - ], -} -`; - -exports[`tool generators > setup-goose has stable golden output 1`] = ` -{ - "files": [ - { - "format": "json", - "path": "/home/tester/.config/goose/custom_providers/freellmapi.json", - "value": { - "api_key_env": "FREELLMAPI_API_KEY", - "base_url": "http://localhost:3000/v1", - "description": "FreeLLMAPI OpenAI-compatible gateway", - "display_name": "FreeLLMAPI", - "dynamic_models": false, - "engine": "openai", - "models": [ - { - "context_limit": 131072, - "name": "fast-coder", - }, - { - "context_limit": 262144, - "name": "reasoning-model", - }, - ], - "name": "freellmapi", - "preserves_thinking": true, - "requires_auth": true, - "supports_streaming": true, - }, - }, - { - "content": "# freellmapi:start -GOOSE_PROVIDER: freellmapi -GOOSE_MODEL: "fast-coder" -# freellmapi:end -", - "format": "yaml", - "path": "/home/tester/.config/goose/config.yaml", - }, - ], - "notes": [ - "Export FREELLMAPI_API_KEY before starting Goose.", - ], -} -`; - -exports[`tool generators > setup-hermes has stable golden output 1`] = ` -{ - "files": [ - { - "format": "yaml", - "path": "/home/tester/.hermes/config.yaml", - "value": { - "model": { - "api_key": "\${FREELLMAPI_API_KEY}", - "api_key_env": undefined, - "api_mode": "chat_completions", - "base_url": "http://localhost:3000/v1", - "context_length": 131072, - "default": "fast-coder", - "default_headers": { - "User-Agent": "hermes-agent", - }, - "key_env": undefined, - "provider": "custom", - }, - }, - }, - { - "content": "FREELLMAPI_API_KEY=freellmapi-test-key -", - "format": "env", - "path": "/home/tester/.hermes/.env", - "sensitive": true, - }, - ], - "notes": [ - "Install Hermes Agent with: curl -fsSL https://hermes-agent.nousresearch.com/install.sh | bash -s -- --skip-setup", - "Try it with: hermes -z "Say hello" — custom/fast-coder is the default model.", - "A running \`hermes gateway\` (Telegram, Discord, â€Ļ) needs a restart to pick the change up.", - ], -} -`; - -exports[`tool generators > setup-kilo has stable golden output 1`] = ` -{ - "files": [ - { - "format": "json", - "path": "/home/tester/.config/kilo/kilo.jsonc", - "value": { - "$schema": "https://app.kilo.ai/config.json", - "model": "openai-compatible/fast-coder", - "provider": { - "openai-compatible": { - "models": { - "fast-coder": { - "limit": { - "context": 131072, - "output": 8192, - }, - "name": "Fast Coder", - "tool_call": true, - }, - "reasoning-model": { - "limit": { - "context": 262144, - "output": 8192, - }, - "name": "Reasoning Model", - "tool_call": true, - }, - }, - "options": { - "apiKey": "{env:FREELLMAPI_API_KEY}", - "baseURL": "http://localhost:3000/v1", - }, - }, - }, - }, - }, - ], - "notes": [ - "Export FREELLMAPI_API_KEY before starting Kilo; global config is trusted for {env:â€Ļ} expansion.", - "One-shot check: kilo run "Say hello" — openai-compatible/fast-coder is the default model.", - ], -} -`; - -exports[`tool generators > setup-mimo has stable golden output 1`] = ` -{ - "files": [ - { - "format": "json", - "path": "/home/tester/.config/mimocode/config.json", - "value": { - "$schema": "https://mimo.xiaomi.com/mimocode/config.json", - "model": "freellmapi/fast-coder", - "provider": { - "freellmapi": { - "models": { - "fast-coder": { - "limit": { - "context": 131072, - "output": 8192, - }, - "name": "Fast Coder", - }, - "reasoning-model": { - "limit": { - "context": 262144, - "output": 8192, - }, - "name": "Reasoning Model", - }, - }, - "name": "FreeLLMAPI", - "npm": "@ai-sdk/openai-compatible", - "options": { - "apiKey": "{env:FREELLMAPI_API_KEY}", - "baseURL": "http://localhost:3000/v1", - }, - }, - }, - }, - }, - ], - "notes": [ - "Install MiMo Code with: curl -fsSL https://mimo.xiaomi.com/install | bash", - "Export FREELLMAPI_API_KEY before starting MiMo Code.", - "Start it with \`mimo\` — freellmapi/fast-coder is the default model.", - "One-shot check: mimo run "Say hello"", - ], -} -`; - -exports[`tool generators > setup-openclaw has stable golden output 1`] = ` -{ - "files": [ - { - "format": "json", - "path": "/home/tester/.openclaw/openclaw.json", - "value": { - "agents": { - "defaults": { - "model": { - "primary": "freellmapi/fast-coder", - }, - }, - }, - "models": { - "providers": { - "freellmapi": { - "api": "openai-completions", - "apiKey": "\${FREELLMAPI_API_KEY}", - "baseUrl": "http://localhost:3000/v1", - "headers": { - "User-Agent": "openclaw", - }, - "models": [ - { - "contextWindow": 131072, - "cost": { - "cacheRead": 0, - "cacheWrite": 0, - "input": 0, - "output": 0, - }, - "id": "fast-coder", - "input": [ - "text", - ], - "maxTokens": 8192, - "name": "Fast Coder", - "reasoning": false, - }, - { - "contextWindow": 262144, - "cost": { - "cacheRead": 0, - "cacheWrite": 0, - "input": 0, - "output": 0, - }, - "id": "reasoning-model", - "input": [ - "text", - ], - "maxTokens": 8192, - "name": "Reasoning Model", - "reasoning": false, - }, - ], - }, - }, - }, - }, - }, - { - "content": "FREELLMAPI_API_KEY=freellmapi-test-key -", - "format": "env", - "path": "/home/tester/.openclaw/.env", - "sensitive": true, - }, - ], - "notes": [ - "Install OpenClaw with: npm install -g openclaw@latest --allow-scripts=openclaw", - "Try it without the gateway daemon: openclaw agent exec --model freellmapi/fast-coder "Say hello"", - "freellmapi/fast-coder is the default model; a running \`openclaw gateway\` needs a restart to pick the change up.", - "Models are declared text-only; give a vision model \`input: ["text", "image"]\` under models.providers.freellmapi to send it images.", - ], -} -`; - -exports[`tool generators > setup-opencode has stable golden output 1`] = ` -{ - "files": [ - { - "format": "json", - "path": "/home/tester/.config/opencode/opencode.json", - "value": { - "$schema": "https://opencode.ai/config.json", - "model": "freellmapi/fast-coder", - "provider": { - "freellmapi": { - "models": { - "fast-coder": { - "limit": { - "context": 131072, - "output": 8192, - }, - "name": "Fast Coder", - }, - "reasoning-model": { - "limit": { - "context": 262144, - "output": 8192, - }, - "name": "Reasoning Model", - }, - }, - "name": "FreeLLMAPI", - "npm": "@ai-sdk/openai-compatible", - "options": { - "apiKey": "{env:FREELLMAPI_API_KEY}", - "baseURL": "http://localhost:3000/v1", - }, - }, - }, - }, - }, - ], - "notes": [ - "Export FREELLMAPI_API_KEY before starting OpenCode.", - "freellmapi/fast-coder is now the default model.", - ], -} -`; - -exports[`tool generators > setup-pi has stable golden output 1`] = ` -{ - "files": [ - { - "format": "json", - "path": "/home/tester/.pi/agent/models.json", - "sensitive": true, - "value": { - "providers": { - "freellmapi": { - "api": "openai-completions", - "apiKey": "freellmapi-test-key", - "baseUrl": "http://localhost:3000/v1", - "headers": { - "User-Agent": "pi-coding-agent", - }, - "models": [ - { - "contextWindow": 131072, - "cost": { - "cacheRead": 0, - "cacheWrite": 0, - "input": 0, - "output": 0, - }, - "id": "fast-coder", - "input": [ - "text", - ], - "maxTokens": 8192, - "name": "Fast Coder", - "reasoning": false, - }, - { - "contextWindow": 262144, - "cost": { - "cacheRead": 0, - "cacheWrite": 0, - "input": 0, - "output": 0, - }, - "id": "reasoning-model", - "input": [ - "text", - ], - "maxTokens": 8192, - "name": "Reasoning Model", - "reasoning": false, - }, - ], - "name": "FreeLLMAPI", - }, - }, - }, - }, - { - "format": "json", - "path": "/home/tester/.pi/agent/settings.json", - "value": { - "defaultModel": "fast-coder", - "defaultProvider": "freellmapi", - }, - }, - ], - "notes": [ - "Install Pi with: npm install -g @earendil-works/pi-coding-agent (or curl -fsSL https://pi.dev/install.sh | sh)", - "Try it with: pi -p "Say hello" — freellmapi/fast-coder is the default model.", - "Every catalog model is in /model; a running Pi session picks the change up when /model is opened.", - "Models are declared text-only; give a vision model \`input: ["text", "image"]\` under providers.freellmapi in models.json to send it images.", - ], -} -`; - -exports[`tool generators > setup-qwen has stable golden output 1`] = ` -{ - "files": [ - { - "format": "json", - "path": "/home/tester/.qwen/settings.json", - "value": { - "model": { - "name": "fast-coder", - }, - "modelProviders": { - "openai": { - "models": [ - { - "baseUrl": "http://localhost:3000/v1", - "envKey": "FREELLMAPI_API_KEY", - "generationConfig": { - "contextWindowSize": 131072, - }, - "id": "fast-coder", - "name": "Fast Coder", - }, - { - "baseUrl": "http://localhost:3000/v1", - "envKey": "FREELLMAPI_API_KEY", - "generationConfig": { - "contextWindowSize": 262144, - }, - "id": "reasoning-model", - "name": "Reasoning Model", - }, - ], - "protocol": "openai", - }, - }, - "security": { - "auth": { - "selectedType": "openai", - }, - }, - }, - }, - { - "content": "FREELLMAPI_API_KEY=freellmapi-test-key -", - "format": "env", - "path": "/home/tester/.qwen/.env", - "sensitive": true, - }, - ], - "notes": [ - "Native Gemini mode is also available at http://localhost:3000/v1beta with GEMINI_API_KEY.", - ], -} -`; - -exports[`tool generators > setup-reasonix has stable golden output 1`] = ` -{ - "files": [ - { - "content": "default_model = "freellmapi/fast-coder" - -[[providers]] -name = "freellmapi" -display_name = "FreeLLMAPI" -kind = "openai" -base_url = "http://localhost:3000/v1" -models = ["fast-coder", "reasoning-model"] -default = "fast-coder" -api_key_env = "FREELLMAPI_API_KEY" -context_window = 131072 -price = { cache_hit = 0, input = 0, output = 0, currency = "$" }", - "format": "toml", - "path": "/home/tester/.reasonix/config.toml", - }, - { - "content": "FREELLMAPI_API_KEY=freellmapi-test-key -", - "format": "env", - "path": "/home/tester/.reasonix/.env", - "sensitive": true, - }, - ], - "notes": [ - "Install Reasonix with: npm install -g reasonix (or brew install esengine/reasonix/reasonix)", - "Try it with: reasonix -p "Say hello" — freellmapi/fast-coder is the default model.", - "Every catalog model is in /model as freellmapi/; other [[providers]] entries in config.toml are kept.", - "Reasonix Studio lists only the providers in [desktop].provider_access when that list is set; add "freellmapi" there, or under Settings → Model → Access.", - ], -} -`; - -exports[`tool generators > setup-roo has stable golden output 1`] = ` -{ - "files": [ - { - "format": "json", - "path": "/home/tester/.roo/freellmapi.json", - "sensitive": true, - "value": { - "globalSettings": {}, - "providerProfiles": { - "apiConfigs": { - "freellmapi": { - "apiProvider": "openai", - "openAiApiKey": "freellmapi-test-key", - "openAiBaseUrl": "http://localhost:3000/v1", - "openAiCustomModelInfo": { - "contextWindow": 131072, - "maxTokens": 8192, - "supportsPromptCache": false, - }, - "openAiModelId": "fast-coder", - "openAiStreamingEnabled": true, - }, - "freellmapi-cli": { - "apiProvider": "openrouter", - "openRouterApiKey": "freellmapi-test-key", - "openRouterBaseUrl": "http://localhost:3000/v1", - "openRouterModelId": "fast-coder", - }, - }, - "currentApiConfigName": "freellmapi", - }, - }, - }, - { - "format": "json", - "path": "/home/tester/.roo/freellmapi-cli.json", - "sensitive": true, - "value": { - "globalSettings": {}, - "providerProfiles": { - "apiConfigs": { - "freellmapi-cli": { - "apiProvider": "openrouter", - "openRouterApiKey": "freellmapi-test-key", - "openRouterBaseUrl": "http://localhost:3000/v1", - "openRouterModelId": "fast-coder", - }, - }, - "currentApiConfigName": "freellmapi-cli", - }, - }, - }, - { - "format": "json", - "path": "/home/tester/.roo/cli-settings.json", - "value": { - "model": "fast-coder", - "provider": "openrouter", - }, - }, - ], - "notes": [ - "VS Code: add "roo-cline.autoImportSettingsPath": "/home/tester/.roo/freellmapi.json" to settings.json and reload (or Roo Settings → Import); the freellmapi profile becomes current.", - "Roo CLI: merge {"roo-cline.autoImportSettingsPath": "/home/tester/.roo/freellmapi-cli.json"} into ~/.vscode-mock/global-storage/global-state.json (create it if missing), then:", - " export OPENROUTER_API_KEY= # the CLI refuses to start without it, even with the key in the profile", - " roo -m fast-coder "Say hello" # -m is required: the CLI's built-in default model shadows cli-settings.json", - ], -} -`; - -exports[`tool generators > setup-zcode has stable golden output 1`] = ` -{ - "files": [ - { - "arrays": { - "config.modelConfigRules.manualProviderModelRules": { - "ownedBy": { - "providerId": "freellmapi", - }, - }, - "config.modelConfigRules.providerModelRules": { - "ownedBy": { - "providerId": "freellmapi", - }, - }, - "config.providerConfigRules.providerRules": { - "ownedBy": { - "providerId": "freellmapi", - }, - }, - "config.providerOrder": { - "union": true, - }, - }, - "format": "json", - "path": "/home/tester/.zcode/v2/provider_config.json", - "sensitive": true, - "value": { - "config": { - "defaultModelSelection": { - "modelId": "fast-coder", - "providerId": "freellmapi", - }, - "modelConfigRules": { - "manualProviderModelRules": [], - "providerModelRules": [ - { - "config": { - "enabled": true, - "optionSpecs": { - "maxOutputTokens": { - "map": "{"max_tokens": maxOutputTokens}", - "max": 8192, - }, - }, - "properties": { - "contextWindow": 131072, - }, - }, - "modelId": "fast-coder", - "providerId": "freellmapi", - }, - { - "config": { - "enabled": true, - "optionSpecs": { - "maxOutputTokens": { - "map": "{"max_tokens": maxOutputTokens}", - "max": 8192, - }, - }, - "properties": { - "contextWindow": 262144, - }, - }, - "modelId": "reasoning-model", - "providerId": "freellmapi", - }, - ], - }, - "providerConfigRules": { - "providerRules": [ - { - "config": { - "access": { - "apiKey": "freellmapi-test-key", - "type": "api-key", - }, - "api": { - "baseUrl": "http://localhost:3000/v1", - "type": "openai-chat-completions", - }, - "group": "standard-personal", - "modelOrder": [ - "fast-coder", - "reasoning-model", - ], - "personalModelIds": [ - "fast-coder", - "reasoning-model", - ], - "visibility": "visible", - }, - "enabled": true, - "providerId": "freellmapi", - "providerName": "FreeLLMAPI", - "templateId": null, - }, - ], - }, - "providerOrder": [ - "freellmapi", - ], - }, - "schemaVersion": 1, - }, - }, - ], - "notes": [ - "Install ZCode from https://zcode.z.ai (or the community terminal client: npm i -g zcode-app-cli).", - "Quit ZCode Desktop before running this so it does not save its in-memory registry over the file.", - "freellmapi/fast-coder is now the default model; pick another under freellmapi in the model picker (or /model in the CLI). Other providers are kept.", - "The API key lives in the provider file (written 0600): ZCode has no env lookup for custom providers.", - ], -} -`; diff --git a/cli/src/config-files.test.ts b/cli/src/config-files.test.ts deleted file mode 100644 index a9549125ad7c3ce250f2934d1c30d7e57248b7b5..0000000000000000000000000000000000000000 --- a/cli/src/config-files.test.ts +++ /dev/null @@ -1,582 +0,0 @@ -import fs from 'node:fs'; -import os from 'node:os'; -import path from 'node:path'; -import { parse } from 'jsonc-parser'; -import { afterEach, describe, expect, it } from 'vitest'; -import { applyGeneratedFile, applyGeneratedFiles, renderFile } from './config-files.js'; - -const temporary: string[] = []; - -afterEach(() => { - for (const directory of temporary.splice(0)) { - fs.rmSync(directory, { recursive: true, force: true }); - } -}); - -describe('safe config writes', () => { - it('writes a fresh env file without a leading blank line', () => { - const rendered = renderFile({ path: '/unused', format: 'env', content: 'FREELLMAPI_API_KEY=k\n' }, ''); - expect(rendered).toBe('FREELLMAPI_API_KEY=k\n'); - expect(renderFile({ path: '/unused', format: 'env', content: 'FREELLMAPI_API_KEY=k\n' }, rendered)).toBe(rendered); - }); - - it('appends a new env key without leaving a blank line before it', () => { - const merged = renderFile( - { path: '/unused/.env', format: 'env', content: 'FREELLMAPI_API_KEY=k\n' }, - 'DEEPSEEK_API_KEY=sk-keep\n', - ); - expect(merged).toBe('DEEPSEEK_API_KEY=sk-keep\nFREELLMAPI_API_KEY=k\n'); - }); - - it('merges keyed JSON arrays per owner instead of replacing them (ZCode)', () => { - const existing = JSON.stringify({ - schemaVersion: 1, - config: { - providerConfigRules: { providerRules: [ - { providerId: 'local', providerName: 'Mine' }, - { providerId: 'freellmapi', providerName: 'Old' }, - ] }, - modelConfigRules: { - providerModelRules: [{ providerId: 'local', modelId: 'm' }, { providerId: 'freellmapi', modelId: 'gone' }], - manualProviderModelRules: [{ providerId: 'freellmapi', modelId: 'fast' }, { providerId: 'local', modelId: 'x' }], - }, - providerOrder: ['local', 'freellmapi'], - }, - }, null, 2); - const file = { - path: '/unused/provider_config.json', - format: 'json' as const, - arrays: { - 'config.providerConfigRules.providerRules': { ownedBy: { providerId: 'freellmapi' } }, - 'config.modelConfigRules.providerModelRules': { ownedBy: { providerId: 'freellmapi' } }, - 'config.modelConfigRules.manualProviderModelRules': { ownedBy: { providerId: 'freellmapi' } }, - 'config.providerOrder': { union: true as const }, - }, - value: { - config: { - providerConfigRules: { providerRules: [{ providerId: 'freellmapi', providerName: 'FreeLLMAPI' }] }, - modelConfigRules: { - providerModelRules: [{ providerId: 'freellmapi', modelId: 'fast' }], - manualProviderModelRules: [], - }, - providerOrder: ['freellmapi'], - }, - }, - }; - const once = renderFile(file, existing); - const merged = parse(once).config; - expect(merged.providerConfigRules.providerRules).toEqual([ - { providerId: 'local', providerName: 'Mine' }, - { providerId: 'freellmapi', providerName: 'FreeLLMAPI' }, - ]); - expect(merged.modelConfigRules.providerModelRules).toEqual([ - { providerId: 'local', modelId: 'm' }, - { providerId: 'freellmapi', modelId: 'fast' }, - ]); - expect(merged.modelConfigRules.manualProviderModelRules).toEqual([{ providerId: 'local', modelId: 'x' }]); - expect(merged.providerOrder).toEqual(['local', 'freellmapi']); - expect(renderFile(file, once)).toBe(once); - }); - - it('renders a fresh Continue config idempotently', () => { - const generated = [ - '# freellmapi:start', - 'name: FreeLLMAPI', - 'version: 1.0.0', - 'schema: v1', - 'models:', - ' - name: FreeLLMAPI', - ' provider: openai', - ' model: coder', - '# freellmapi:end', - '', - ].join('\n'); - const first = renderFile({ - path: '/unused', - format: 'yaml', - content: generated, - }); - const second = renderFile({ - path: '/unused', - format: 'yaml', - content: generated, - }, first); - expect(second).toBe(first); - expect(first).not.toContain('freellmapi:start'); - expect(first.match(/^models:$/gm)).toHaveLength(1); - }); - - it('deep-merges JSONC and retains inline and leading comments', () => { - const existing = [ - '// user configuration', - '{', - ' // keep the chosen theme', - ' "theme": "dark",', - ' "provider": {', - ' /* keep the unrelated provider */', - ' "other": true,', - ' },', - '}', - '', - ].join('\n'); - const rendered = renderFile({ - path: '/unused', - format: 'json', - value: { provider: { freellmapi: true } }, - }, existing); - expect(rendered).toContain('// user configuration'); - expect(rendered).toContain('// keep the chosen theme'); - expect(rendered).toContain('/* keep the unrelated provider */'); - expect(parse(rendered)).toEqual({ - theme: 'dark', - provider: { other: true, freellmapi: true }, - }); - }); - - it('merges Continue into an existing config without replacing other models', () => { - const existing = [ - '# personal config', - 'name: Personal', - 'version: 2.0.0', - 'schema: v1', - 'models:', - ' - name: Existing', - ' provider: ollama', - ' model: qwen', - 'rules:', - ' - Keep this rule', - '', - ].join('\n'); - const generated = [ - '# freellmapi:start', - 'name: FreeLLMAPI', - 'version: 1.0.0', - 'schema: v1', - 'models:', - ' - name: FreeLLMAPI', - ' provider: openai', - ' model: coder', - '# freellmapi:end', - '', - ].join('\n'); - const first = renderFile({ - path: '/unused', - format: 'yaml', - content: generated, - }, existing); - const second = renderFile({ - path: '/unused', - format: 'yaml', - content: generated, - }, first); - expect(second).toBe(first); - expect(first).toContain('# personal config'); - expect(first).toContain('name: Personal'); - expect(first).toContain(' - name: Existing'); - expect(first).toContain(' - name: FreeLLMAPI'); - expect(first).toContain('rules:\n - Keep this rule'); - expect(first.match(/^models:$/gm)).toHaveLength(1); - }); - - it('upgrades a previously marked Continue block without leaving duplicate keys', () => { - const existing = [ - 'name: Personal', - 'version: 2.0.0', - 'schema: v1', - 'models:', - ' - name: Existing', - ' provider: ollama', - ' model: qwen', - '# freellmapi:start', - 'models:', - ' - name: FreeLLMAPI', - ' provider: openai', - ' model: old', - '# freellmapi:end', - '', - ].join('\n'); - const generated = [ - '# freellmapi:start', - 'name: FreeLLMAPI', - 'version: 1.0.0', - 'schema: v1', - 'models:', - ' - name: FreeLLMAPI', - ' provider: openai', - ' model: current', - '# freellmapi:end', - '', - ].join('\n'); - const rendered = renderFile({ - path: '/unused', - format: 'yaml', - content: generated, - }, existing); - expect(rendered.match(/^name:/gm)).toHaveLength(1); - expect(rendered.match(/^version:/gm)).toHaveLength(1); - expect(rendered.match(/^schema:/gm)).toHaveLength(1); - expect(rendered.match(/^models:/gm)).toHaveLength(1); - expect(rendered).toContain(' - name: Existing'); - expect(rendered).toContain(' model: current'); - expect(rendered).not.toContain(' model: old'); - }); - - it('replaces generated YAML scalar keys without creating duplicates', () => { - const existing = '# user config\nGOOSE_PROVIDER: old\nGOOSE_MODEL: old\nkeep: true\n'; - const generated = [ - '# freellmapi:start', - 'GOOSE_PROVIDER: freellmapi', - 'GOOSE_MODEL: coder', - '# freellmapi:end', - '', - ].join('\n'); - const first = renderFile({ - path: '/unused', - format: 'yaml', - content: generated, - }, existing); - const second = renderFile({ - path: '/unused', - format: 'yaml', - content: generated, - }, first); - expect(second).toBe(first); - expect(first).toContain('# user config'); - expect(first).toContain('keep: true'); - expect(first.match(/^GOOSE_PROVIDER:/gm)).toHaveLength(1); - expect(first.match(/^GOOSE_MODEL:/gm)).toHaveLength(1); - }); - - it('deep-merges structured YAML and retains sibling routes and comments', () => { - // DeepSeek Harness keeps every provider under one `llm-pi-ai.providers` - // dict. The marked-block YAML merge removes whole top-level keys it - // re-emits, which here would delete the user's other providers; the - // structured merge must touch only the path it sets. - const existing = [ - '# my settings', - 'llm-pi-ai:', - ' providers:', - ' anthropic:', - ' # keep me', - ' apiKeyEnv: ANTHROPIC_API_KEY', - ' freellmapi:', - ' baseURL: http://old:1/v1', - ' models:', - ' - id: stale', - 'agent-default-model:', - ' provider: anthropic', - ' model: claude-sonnet-4-5', - ' reasoningEffort: high', - 'other: { a: 1 }', - '', - ].join('\n'); - const file = { - path: '/tmp/settings.yaml', - format: 'yaml' as const, - value: { - 'llm-pi-ai': { - providers: { - freellmapi: { - baseURL: 'http://localhost:3001/v1', - models: [{ id: 'auto' }], - }, - }, - }, - 'agent-default-model': { - provider: 'freellmapi', - model: 'auto', - reasoningEffort: undefined, - }, - }, - }; - const once = renderFile(file, existing); - expect(once).toContain('# my settings'); - expect(once).toContain('# keep me'); - expect(once).toContain('apiKeyEnv: ANTHROPIC_API_KEY'); - expect(once).toContain('baseURL: http://localhost:3001/v1'); - expect(once).not.toContain('http://old:1/v1'); - expect(once).not.toContain('stale'); - expect(once).toContain('provider: freellmapi'); - expect(once).not.toContain('reasoningEffort'); - expect(once).toContain('other: { a: 1 }'); - expect(renderFile(file, once)).toBe(once); - }); - - it('renders structured YAML from an empty or comment-only document', () => { - const file = { - path: '/tmp/settings.yaml', - format: 'yaml' as const, - value: { a: { b: [1, 2] }, c: undefined }, - }; - expect(renderFile(file, '')).toBe('a:\n b:\n - 1\n - 2\n'); - expect(renderFile(file, '# only a comment\n')).toContain('a:\n b:\n - 1\n - 2\n'); - }); - - it('refuses to merge structured YAML into a document it cannot parse', () => { - const file = { - path: '/tmp/settings.yaml', - format: 'yaml' as const, - value: { a: 1 }, - }; - expect(() => renderFile(file, 'a: [1, 2\n')).toThrow(/cannot be parsed/); - expect(() => renderFile(file, '- just\n- a list\n')).toThrow(/not a mapping/); - }); - - it('replaces conflicting Codex TOML settings and retains unrelated tables', () => { - const existing = [ - '# personal config', - 'model = "old"', - 'model_provider = "old"', - '', - '[model_providers.freellmapi]', - 'name = "Old"', - 'base_url = "https://old.invalid/v1"', - '', - '[mcp_servers.personal]', - 'command = "keep-me"', - '', - ].join('\n'); - const generated = [ - '# freellmapi:start', - 'model = "coder"', - 'model_provider = "freellmapi"', - '', - '[model_providers.freellmapi]', - 'name = "FreeLLMAPI"', - 'base_url = "http://localhost:3000/v1"', - '# freellmapi:end', - '', - ].join('\n'); - const first = renderFile({ - path: '/unused', - format: 'toml', - content: generated, - }, existing); - const second = renderFile({ - path: '/unused', - format: 'toml', - content: generated, - }, first); - expect(second).toBe(first); - expect(first).toContain('# personal config'); - expect(first).toContain('[mcp_servers.personal]\ncommand = "keep-me"'); - expect(first.match(/^model =/gm)).toHaveLength(1); - expect(first.match(/^\[model_providers\.freellmapi\]$/gm)).toHaveLength(1); - }); - - it('keeps generated root TOML keys in the root region when the file ends in a table', () => { - const existing = [ - '# personal config', - 'sandbox_mode = "workspace-write"', - '', - '[mcp_servers.personal]', - 'command = "keep-me"', - 'args = ["--serve"]', - '', - ].join('\n'); - const generated = [ - '# freellmapi:start', - 'model = "coder"', - 'model_provider = "freellmapi"', - '', - '[model_providers.freellmapi]', - 'name = "FreeLLMAPI"', - 'base_url = "http://localhost:3000/v1"', - '# freellmapi:end', - '', - ].join('\n'); - const first = renderFile({ - path: '/unused', - format: 'toml', - content: generated, - }, existing); - const second = renderFile({ - path: '/unused', - format: 'toml', - content: generated, - }, first); - expect(second).toBe(first); - - // Root-level keys must appear BEFORE the first table header, otherwise - // TOML assigns them to that table. - const lines = first.split('\n'); - const firstHeader = lines.findIndex(line => /^\[[^\]]+\]$/.test(line)); - const modelLine = lines.findIndex(line => line === 'model = "coder"'); - const providerLine = lines.findIndex(line => line === 'model_provider = "freellmapi"'); - expect(firstHeader).toBeGreaterThan(-1); - expect(modelLine).toBeGreaterThan(-1); - expect(modelLine).toBeLessThan(firstHeader); - expect(providerLine).toBeGreaterThan(-1); - expect(providerLine).toBeLessThan(firstHeader); - expect(first).toContain('sandbox_mode = "workspace-write"'); - expect(first).toContain('[mcp_servers.personal]\ncommand = "keep-me"\nargs = ["--serve"]'); - expect(first).toContain('[model_providers.freellmapi]'); - }); - - it('only strips root TOML keys that the generated root region sets', () => { - const existing = [ - 'name = "my personal codex"', - 'model = "old"', - '', - '[mcp_servers.personal]', - 'command = "keep-me"', - '', - ].join('\n'); - const generated = [ - '# freellmapi:start', - 'model = "coder"', - '', - '[model_providers.freellmapi]', - 'name = "FreeLLMAPI"', - '# freellmapi:end', - '', - ].join('\n'); - const first = renderFile({ - path: '/unused', - format: 'toml', - content: generated, - }, existing); - // The generated TABLE sets `name`, but the user's ROOT-level `name` is a - // different key and must survive. - expect(first).toContain('name = "my personal codex"'); - expect(first.match(/^model =/gm)).toHaveLength(1); - expect(first).toContain('model = "coder"'); - }); - - it('does not write any file when a later file in the batch fails to merge', () => { - const directory = fs.mkdtempSync(path.join(os.tmpdir(), 'freellmapi-cli-')); - temporary.push(directory); - const good = path.join(directory, 'good.json'); - const bad = path.join(directory, 'bad.json'); - fs.writeFileSync(bad, '{ this is not json'); - expect(() => applyGeneratedFiles([ - { path: good, format: 'json', value: { enabled: true } }, - { path: bad, format: 'json', value: { enabled: true } }, - ], false)).toThrow('cannot be parsed safely'); - expect(fs.existsSync(good)).toBe(false); - expect(fs.readFileSync(bad, 'utf8')).toBe('{ this is not json'); - }); - - it('merges an AtomCode config: root key above existing tables, other providers kept', () => { - // A user with a DeepSeek provider already configured runs setup-atomcode: - // `default_provider` must land in the root region (a key appended after - // `[providers.deepseek]` would become that table's key), the deepseek - // table must survive, and a stale `[providers.freellmapi]` is replaced. - const existing = [ - 'default_provider = "deepseek"', - 'auto_update = true', - '', - '[providers.deepseek]', - 'type = "openai"', - 'api_key = "sk-keep"', - 'model = "deepseek-chat"', - '', - '[providers.freellmapi]', - 'type = "openai"', - 'model = "stale-model"', - '', - ].join('\n'); - const generated = [ - 'default_provider = "freellmapi"', - '', - '[providers.freellmapi]', - 'type = "openai"', - 'base_url = "http://localhost:3000/v1"', - 'api_key = "freellmapi-test-key"', - 'model = "fast-coder"', - 'context_window = 131072', - ].join('\n'); - const merged = renderFile({ path: '/unused', format: 'toml', content: generated }, existing); - const lines = merged.split('\n'); - const firstTable = lines.findIndex(line => line.startsWith('[')); - expect(lines.slice(0, firstTable)).toContain('default_provider = "freellmapi"'); - expect(lines.slice(0, firstTable)).toContain('auto_update = true'); - expect(merged.match(/^default_provider =/gm)).toHaveLength(1); - expect(merged).toContain('[providers.deepseek]\ntype = "openai"\napi_key = "sk-keep"'); - expect(merged.match(/^\[providers\.freellmapi\]$/gm)).toHaveLength(1); - expect(merged).not.toContain('stale-model'); - expect(merged).toContain('context_window = 131072'); - expect(renderFile({ path: '/unused', format: 'toml', content: generated }, merged)).toBe(merged); - }); - - it('merges a Reasonix config: replaces only the [[providers]] entry with the same name', () => { - // Reasonix keeps every endpoint as a `[[providers]]` array entry. A rerun - // must swap the stale freellmapi entry (and its sub-table) for the new one - // and leave the deepseek entry, the [desktop] table and root keys alone. - const existing = [ - 'config_version = 1', - 'default_model = "deepseek/deepseek-flash"', - '', - '[desktop]', - 'provider_access = ["deepseek"]', - '', - '[[providers]]', - 'name = "deepseek"', - 'kind = "openai"', - 'api_key_env = "DEEPSEEK_API_KEY"', - '', - '[[providers]]', - "name = 'freellmapi'", - 'base_url = "http://stale:9/v1"', - '', - '[providers.model_overrides]', - 'stale = { context_window = 1 }', - '', - '[[plugins]]', - 'name = "freellmapi"', - 'command = "keep-me"', - '', - ].join('\n'); - const generated = [ - 'default_model = "freellmapi/auto"', - '', - '[[providers]]', - 'name = "freellmapi"', - 'kind = "openai"', - 'base_url = "http://localhost:3001/v1"', - 'models = ["auto"]', - ].join('\n'); - const merged = renderFile({ path: '/unused', format: 'toml', content: generated }, existing); - const lines = merged.split('\n'); - const firstTable = lines.findIndex(line => line.startsWith('[')); - expect(lines.slice(0, firstTable)).toContain('default_model = "freellmapi/auto"'); - expect(lines.slice(0, firstTable)).toContain('config_version = 1'); - expect(merged.match(/^default_model =/gm)).toHaveLength(1); - expect(merged).toContain('[desktop]\nprovider_access = ["deepseek"]'); - expect(merged).toContain('name = "deepseek"\nkind = "openai"'); - expect(merged).toContain('[[plugins]]\nname = "freellmapi"\ncommand = "keep-me"'); - expect(merged.match(/^\[\[providers\]\]$/gm)).toHaveLength(2); - expect(merged).not.toContain('stale'); - expect(merged).toContain('base_url = "http://localhost:3001/v1"'); - expect(renderFile({ path: '/unused', format: 'toml', content: generated }, merged)).toBe(merged); - }); - - it('creates a timestamped backup before replacing a real file', () => { - const directory = fs.mkdtempSync(path.join(os.tmpdir(), 'freellmapi-cli-')); - temporary.push(directory); - const target = path.join(directory, 'config.toml'); - fs.writeFileSync(target, 'user_setting = true\n'); - const result = applyGeneratedFile({ - path: target, - format: 'toml', - content: '# freellmapi:start\nmodel = "auto"\n# freellmapi:end\n', - }, false); - expect(result.backupPath).toBeTruthy(); - expect(fs.readFileSync(result.backupPath!, 'utf8')).toBe('user_setting = true\n'); - expect(fs.readFileSync(target, 'utf8')).toContain('user_setting = true'); - expect(fs.readFileSync(target, 'utf8')).toContain('model = "auto"'); - }); - - it('does not write in dry-run mode', () => { - const directory = fs.mkdtempSync(path.join(os.tmpdir(), 'freellmapi-cli-')); - temporary.push(directory); - const target = path.join(directory, 'new.json'); - const result = applyGeneratedFile({ - path: target, - format: 'json', - value: { enabled: true }, - }, true); - expect(result.changed).toBe(true); - expect(fs.existsSync(target)).toBe(false); - }); -}); diff --git a/cli/src/config-files.ts b/cli/src/config-files.ts deleted file mode 100644 index 1c1884ae87f39a29d254d85081ecb9775042afca..0000000000000000000000000000000000000000 --- a/cli/src/config-files.ts +++ /dev/null @@ -1,449 +0,0 @@ -import fs from 'node:fs'; -import path from 'node:path'; -import { - applyEdits, - modify, - parse, - type ParseError, -} from 'jsonc-parser'; -import { Document, isMap, parseDocument } from 'yaml'; -import type { GeneratedFile, JsonArrayMerge } from './types.js'; - -function isRecord(value: unknown): value is Record { - return Boolean(value) - && typeof value === 'object' - && !Array.isArray(value); -} - -function mergeJsonArray(current: unknown[], next: unknown[], rule: JsonArrayMerge): unknown[] { - if ('union' in rule) { - return [...current, ...next.filter(item => !current.some(existing => JSON.stringify(existing) === JSON.stringify(item)))]; - } - const owned = (item: unknown) => isRecord(item) - && Object.entries(rule.ownedBy).every(([field, expected]) => item[field] === expected); - return [...current.filter(item => !owned(item)), ...next]; -} - -function mergeJson(existing: string, value: Record, arrays: Record = {}): string { - let rendered = existing.trim() ? existing : '{}\n'; - const errors: ParseError[] = []; - const parsed = parse(rendered, errors, { - allowTrailingComma: true, - disallowComments: false, - }); - if (errors.length || !isRecord(parsed)) { - throw new Error('Existing JSON/JSONC cannot be parsed safely'); - } - - const formattingOptions = { - insertSpaces: true, - tabSize: 2, - eol: '\n', - }; - const applyPatch = ( - base: Record, - patch: Record, - parentPath: (string | number)[] = [], - ) => { - for (const [key, next] of Object.entries(patch)) { - const current = base[key]; - const propertyPath = [...parentPath, key]; - if (isRecord(current) && isRecord(next)) { - applyPatch(current, next, propertyPath); - continue; - } - const arrayRule = arrays[propertyPath.join('.')]; - const merged = arrayRule && Array.isArray(current) && Array.isArray(next) - ? mergeJsonArray(current, next, arrayRule) - : next; - rendered = applyEdits(rendered, modify( - rendered, - propertyPath, - merged, - { formattingOptions }, - )); - } - }; - applyPatch(parsed, value); - return rendered.endsWith('\n') ? rendered : `${rendered}\n`; -} - -// Structural YAML merge, the `mergeJson` rule applied to a YAML document: -// nested objects recurse, everything else (scalars, arrays) is replaced at -// that path, and an explicit `undefined` deletes the key (a generator's way -// to retire a sibling the new value makes stale). Editing the parsed -// Document rather than re-rendering from a plain object keeps the user's -// comments, key order, and quoting for every node the patch does not touch — -// a settings.yaml shared with other providers (DeepSeek Harness keeps every -// route under one `llm-pi-ai` key) must not lose its siblings just because -// ours changed. -function mergeYamlDocument(existing: string, value: Record): string { - if (!existing.trim()) { - const fresh = new Document(JSON.parse(JSON.stringify(value))); - return fresh.toString({ lineWidth: 0 }); - } - const doc = parseDocument(existing); - if (doc.errors.length) { - throw new Error(`Existing YAML cannot be parsed safely: ${doc.errors[0].message}`); - } - if (doc.contents !== null && !isMap(doc.contents)) { - throw new Error('Existing YAML is not a mapping at the top level'); - } - const applyPatch = (patch: Record, parentPath: string[] = []) => { - for (const [key, next] of Object.entries(patch)) { - const propertyPath = [...parentPath, key]; - if (next === undefined) { - doc.deleteIn(propertyPath); - continue; - } - const current = doc.getIn(propertyPath, true); - if (isRecord(next) && isMap(current)) { - applyPatch(next, propertyPath); - continue; - } - doc.setIn(propertyPath, next); - } - }; - applyPatch(value); - const rendered = doc.toString({ lineWidth: 0 }); - return rendered.endsWith('\n') ? rendered : `${rendered}\n`; -} - -function markerPattern(): RegExp { - return new RegExp( - `^[ \\t]*(?:#|//) freellmapi:start[\\s\\S]*?^[ \\t]*(?:#|//) freellmapi:end\\s*`, - 'm', - ); -} - -function mergeMarked(existing: string, generated: string): string { - const start = generated.match(/^[ \t]*(?:#|\/\/) freellmapi:start$/m)?.[0]; - const end = generated.match(/^[ \t]*(?:#|\/\/) freellmapi:end$/m)?.[0]; - if (!start || !end) return generated; - const marked = markerPattern(); - if (marked.test(existing)) { - return existing.replace(marked, `${generated.trimEnd()}\n`); - } - const separator = existing.length && !existing.endsWith('\n') ? '\n' : ''; - return `${existing}${separator}${existing.trim() ? '\n' : ''}${generated}`; -} - -function topLevelYamlKey(line: string): string | undefined { - return line.match(/^([A-Za-z0-9_-]+):(?:\s|$)/)?.[1]; -} - -function generatedYamlKeys(generated: string): Set { - return new Set(generated.split(/\r?\n/) - .map(topLevelYamlKey) - .filter((key): key is string => Boolean(key))); -} - -function removeYamlKeys(existing: string, keys: Set): string { - const lines = existing.split(/\r?\n/); - const output: string[] = []; - for (let index = 0; index < lines.length;) { - const key = topLevelYamlKey(lines[index]); - if (!key || !keys.has(key)) { - output.push(lines[index]); - index += 1; - continue; - } - index += 1; - while (index < lines.length && !topLevelYamlKey(lines[index])) index += 1; - } - return output.join('\n').replace(/\n{3,}/g, '\n\n').trimEnd(); -} - -function continueModelLines(generated: string): string[] { - const lines = generated.split(/\r?\n/); - const modelsIndex = lines.findIndex(line => line === 'models:'); - const endIndex = lines.findIndex( - (line, index) => index > modelsIndex && line === '# freellmapi:end', - ); - return lines.slice(modelsIndex + 1, endIndex < 0 ? undefined : endIndex); -} - -function mergeContinueYaml(existing: string, generated: string): string { - const generatedLines = generated.split(/\r?\n/); - const missingHeader = ['name', 'version', 'schema'] - .filter(key => !existing.split(/\r?\n/).some(line => topLevelYamlKey(line) === key)) - .map(key => generatedLines.find(line => topLevelYamlKey(line) === key)!) - .filter(Boolean); - let lines = existing.split(/\r?\n/); - const modelsIndex = lines.findIndex(line => topLevelYamlKey(line) === 'models'); - const modelLines = continueModelLines(generated); - - if (modelsIndex < 0) { - const prefix = missingHeader.length ? `${missingHeader.join('\n')}\n` : ''; - const separator = existing.trim() ? '\n\n' : ''; - return `${prefix}${existing.trimEnd()}${separator}models:\n${modelLines.join('\n')}\n`; - } - - let modelsEnd = modelsIndex + 1; - while (modelsEnd < lines.length && !topLevelYamlKey(lines[modelsEnd])) modelsEnd += 1; - const entryStart = lines.findIndex( - (line, index) => index > modelsIndex - && index < modelsEnd - && /^\s{2}-\s+name:\s+["']?FreeLLMAPI["']?\s*$/.test(line), - ); - if (entryStart >= 0) { - let entryEnd = entryStart + 1; - while ( - entryEnd < modelsEnd - && !/^\s{2}-\s+/.test(lines[entryEnd]) - && !topLevelYamlKey(lines[entryEnd]) - ) { - entryEnd += 1; - } - lines.splice(entryStart, entryEnd - entryStart, ...modelLines); - } else { - lines.splice(modelsEnd, 0, ...modelLines); - } - if (missingHeader.length) lines = [...missingHeader, ...lines]; - return `${lines.join('\n').replace(/\n+$/, '')}\n`; -} - -function mergeYaml(existing: string, generated: string): string { - if (/^name: FreeLLMAPI$/m.test(generated) && /^models:$/m.test(generated)) { - const withoutOldBlock = existing.replace(markerPattern(), '').trimEnd(); - return mergeContinueYaml(withoutOldBlock, generated); - } - if (!existing.trim()) return generated; - if (/^# freellmapi:start$/m.test(existing)) return mergeMarked(existing, generated); - const withoutConflicts = removeYamlKeys(existing, generatedYamlKeys(generated)); - return mergeMarked(withoutConflicts, generated); -} - -function tomlTableHeader(line: string): string | undefined { - return line.match(/^\[([^\]]+)\]$/)?.[1]; -} - -function tomlArrayHeader(line: string): string | undefined { - return line.match(/^\[\[([^\]]+)\]\]$/)?.[1]; -} - -function isTomlHeader(line: string): boolean { - return tomlTableHeader(line) !== undefined || tomlArrayHeader(line) !== undefined; -} - -// An array-of-tables entry (`[[providers]]`) has no name of its own; the -// `name = "..."` key inside it is what tools use to tell entries apart. -function tomlEntryName(lines: string[]): string | undefined { - for (const line of lines.slice(1)) { - if (isTomlHeader(line)) break; - const value = line.match(/^\s*name\s*=\s*(?:"([^"]*)"|'([^']*)')/); - if (value) return value[1] ?? value[2]; - } - return undefined; -} - -// Splits a document into its root region and one block per header, each block -// running up to the next header. -function tomlBlocks(lines: string[]): string[][] { - const blocks: string[][] = [[]]; - for (const line of lines) { - if (isTomlHeader(line)) blocks.push([]); - blocks[blocks.length - 1].push(line); - } - return blocks; -} - -function tomlRootKey(line: string): string | undefined { - return line.match(/^([A-Za-z0-9_.-]+)\s*=/)?.[1]; -} - -function isTomlMarkerLine(line: string): boolean { - return /^[ \t]*# freellmapi:(?:start|end)$/.test(line); -} - -function trimBlankEdges(lines: string[]): string[] { - let start = 0; - let end = lines.length; - while (start < end && !lines[start].trim()) start += 1; - while (end > start && !lines[end - 1].trim()) end -= 1; - return lines.slice(start, end); -} - -interface TomlSections { - root: string[]; - tables: string[]; -} - -function splitTomlSections(generated: string): TomlSections { - const lines = generated.split(/\r?\n/).filter(line => !isTomlMarkerLine(line)); - const firstTable = lines.findIndex(isTomlHeader); - if (firstTable < 0) return { root: trimBlankEdges(lines), tables: [] }; - return { - root: trimBlankEdges(lines.slice(0, firstTable)), - tables: trimBlankEdges(lines.slice(firstTable)), - }; -} - -// TOML keys written after a `[table]` header belong to that table, so -// generated root-level keys must be inserted BEFORE the first table header of -// the existing file, never appended after it. Generated tables are appended at -// the end, and only root keys that the generated ROOT REGION sets are removed -// from the existing root region. An existing `[[array]]` entry is replaced only -// when a generated entry of the same array carries the same `name` — together -// with the `[array.sub]` tables that belong to it — so the user's other entries -// in that array stay put. -function mergeToml(existing: string, generated: string): string { - if (!existing.trim()) return generated; - const { root: generatedRoot, tables: generatedTables } = splitTomlSections(generated); - const rootKeys = new Set(generatedRoot - .map(tomlRootKey) - .filter((key): key is string => Boolean(key))); - const tableNames = new Set(generatedTables - .map(tomlTableHeader) - .filter((name): name is string => Boolean(name))); - const arrayEntries = new Set(tomlBlocks(generatedTables) - .filter(block => block.length && tomlArrayHeader(block[0])) - .map(block => `${tomlArrayHeader(block[0])}\0${tomlEntryName(block)}`)); - - const kept: string[] = []; - let droppedArray: string | undefined; - const [existingRoot, ...existingBlocks] = tomlBlocks( - existing.split(/\r?\n/).filter(line => !isTomlMarkerLine(line)), - ); - kept.push(...existingRoot.filter(line => !rootKeys.has(tomlRootKey(line) ?? ''))); - for (const block of existingBlocks) { - const table = tomlTableHeader(block[0]); - const array = tomlArrayHeader(block[0]); - const name = table ?? array ?? ''; - if (droppedArray && name.startsWith(`${droppedArray}.`)) continue; - droppedArray = undefined; - if (table && tableNames.has(table)) continue; - if (array && arrayEntries.has(`${array}\0${tomlEntryName(block)}`)) { - droppedArray = array; - continue; - } - kept.push(...block); - } - - const wrap = (lines: string[]): string[] => (lines.length - ? ['# freellmapi:start', ...lines, '# freellmapi:end'] - : []); - const firstTable = kept.findIndex(isTomlHeader); - const head = firstTable < 0 ? kept : kept.slice(0, firstTable); - const tail = firstTable < 0 ? [] : kept.slice(firstTable); - const sections = [head, wrap(generatedRoot), tail, wrap(generatedTables)] - .map(lines => trimBlankEdges(lines).join('\n').replace(/\n{3,}/g, '\n\n')) - .filter(Boolean); - return `${sections.join('\n\n')}\n`; -} - -function mergeEnv(existing: string, generated: string): string { - const replacements = new Map( - generated.split(/\r?\n/) - .filter(line => line && !line.startsWith('#') && line.includes('=')) - .map(line => [line.slice(0, line.indexOf('=')), line]), - ); - // An absent or empty file has no lines: splitting '' yields [''] and that - // empty line survived as a blank first line in every freshly written .env. - // Trailing blank lines go too, or an appended key lands after a gap. - const lines = existing.trim() ? existing.replace(/(?:\r?\n)+$/, '').split(/\r?\n/) : []; - const seen = new Set(); - const output = lines.map(line => { - const separator = line.indexOf('='); - if (separator < 1 || line.trimStart().startsWith('#')) return line; - const key = line.slice(0, separator); - const replacement = replacements.get(key); - if (!replacement) return line; - seen.add(key); - return replacement; - }); - for (const [key, line] of replacements) { - if (!seen.has(key)) output.push(line); - } - return `${output.filter((line, index) => line || index < output.length - 1).join('\n')}\n`; -} - -export function renderFile(file: GeneratedFile, existing = ''): string { - if (file.format === 'json') return mergeJson(existing, file.value ?? {}, file.arrays); - if (file.format === 'env') return mergeEnv(existing, file.content ?? ''); - if (file.format === 'yaml') { - return file.value - ? mergeYamlDocument(existing, file.value) - : mergeYaml(existing, file.content ?? ''); - } - return mergeToml(existing, file.content ?? ''); -} - -function timestamp(): string { - return new Date().toISOString().replace(/[:.]/g, '-'); -} - -export interface ApplyResult { - path: string; - changed: boolean; - backupPath?: string; - rendered: string; - previous: string; -} - -interface PlannedWrite extends ApplyResult { - file: GeneratedFile; - exists: boolean; -} - -function planGeneratedFile(file: GeneratedFile): PlannedWrite { - const exists = fs.existsSync(file.path); - const previous = exists ? fs.readFileSync(file.path, 'utf8') : ''; - const rendered = renderFile(file, previous); - return { - file, - exists, - path: file.path, - changed: rendered !== previous, - rendered, - previous, - }; -} - -function commitPlannedWrite(plan: PlannedWrite): ApplyResult { - const { file, exists, ...result } = plan; - if (!plan.changed) return result; - fs.mkdirSync(path.dirname(file.path), { recursive: true, mode: 0o700 }); - let backupPath: string | undefined; - if (exists) { - backupPath = `${file.path}.backup-${timestamp()}`; - fs.copyFileSync(file.path, backupPath); - } - const temporary = `${file.path}.freellmapi-${process.pid}.tmp`; - fs.writeFileSync(temporary, plan.rendered, { mode: file.sensitive ? 0o600 : 0o644 }); - fs.renameSync(temporary, file.path); - if (file.sensitive) fs.chmodSync(file.path, 0o600); - return { ...result, backupPath }; -} - -// Two-phase apply: every target file is read and merged BEFORE the first -// write, so a merge failure on any file aborts the whole batch without -// leaving earlier files modified. -export function applyGeneratedFiles( - files: GeneratedFile[], - dryRun: boolean, -): ApplyResult[] { - const plans = files.map(planGeneratedFile); - if (dryRun) return plans.map(({ file: _file, exists: _exists, ...result }) => result); - return plans.map(commitPlannedWrite); -} - -export function applyGeneratedFile(file: GeneratedFile, dryRun: boolean): ApplyResult { - return applyGeneratedFiles([file], dryRun)[0]; -} - -export function printDryRunDiff(result: ApplyResult): string { - const oldLines = result.previous.split(/\r?\n/); - const newLines = result.rendered.split(/\r?\n/); - const lines = [`--- ${result.path}`, `+++ ${result.path}`]; - if (!result.previous) { - lines.push(...newLines.map(line => `+${line}`)); - return lines.join('\n'); - } - const prefix = oldLines.findIndex((line, index) => line !== newLines[index]); - const changedAt = prefix < 0 ? 0 : prefix; - lines.push(`@@ line ${changedAt + 1} @@`); - lines.push(...oldLines.slice(changedAt).map(line => `-${line}`)); - lines.push(...newLines.slice(changedAt).map(line => `+${line}`)); - return lines.join('\n'); -} diff --git a/cli/src/detect.test.ts b/cli/src/detect.test.ts deleted file mode 100644 index d701c7d191c8f10c11a315c923814a7c0f50e895..0000000000000000000000000000000000000000 --- a/cli/src/detect.test.ts +++ /dev/null @@ -1,78 +0,0 @@ -import fs from 'node:fs'; -import { describe, expect, it, vi, afterEach } from 'vitest'; -import { - clientNotes, - desktopAppPath, - detectChatGptClient, -} from './detect.js'; - -afterEach(() => { - vi.restoreAllMocks(); - vi.unstubAllEnvs(); -}); - -describe('desktopAppPath', () => { - it('points at /Applications/ChatGPT.app on macOS only', () => { - expect(desktopAppPath('darwin')).toBe('/Applications/ChatGPT.app'); - expect(desktopAppPath('linux')).toBeUndefined(); - expect(desktopAppPath('win32')).toBeUndefined(); - }); -}); - -describe('detectChatGptClient', () => { - it('detects the CLI when a codex binary is on PATH', () => { - vi.stubEnv('PATH', '/opt/tools/bin'); - vi.spyOn(fs, 'accessSync').mockImplementation(() => {}); - vi.spyOn(fs, 'statSync').mockReturnValue({ isFile: () => true } as fs.Stats); - vi.spyOn(fs, 'existsSync').mockReturnValue(false); - const found = detectChatGptClient('linux'); - expect(found).toEqual({ kind: 'cli', cliPath: '/opt/tools/bin/codex' }); - }); - - it('ignores PATH directories that merely shadow the binary name', () => { - vi.stubEnv('PATH', '/opt/shadow'); - vi.spyOn(fs, 'accessSync').mockImplementation(() => {}); - vi.spyOn(fs, 'statSync').mockReturnValue({ isFile: () => false } as fs.Stats); - vi.spyOn(fs, 'existsSync').mockReturnValue(false); - expect(detectChatGptClient('linux')).toBeUndefined(); - }); - - it('detects the desktop app alone on macOS', () => { - vi.stubEnv('PATH', ''); - vi.spyOn(fs, 'existsSync').mockReturnValue(true); - const found = detectChatGptClient('darwin'); - expect(found).toEqual({ kind: 'desktop', desktopPath: '/Applications/ChatGPT.app' }); - }); - - it('reports both when the CLI and the desktop app coexist', () => { - vi.stubEnv('PATH', '/usr/bin'); - vi.spyOn(fs, 'accessSync').mockImplementation(() => {}); - vi.spyOn(fs, 'statSync').mockReturnValue({ isFile: () => true } as fs.Stats); - vi.spyOn(fs, 'existsSync').mockReturnValue(true); - const found = detectChatGptClient('darwin'); - expect(found?.kind).toBe('both'); - }); - - it('returns undefined when nothing is installed', () => { - vi.stubEnv('PATH', ''); - vi.spyOn(fs, 'existsSync').mockReturnValue(false); - expect(detectChatGptClient('darwin')).toBeUndefined(); - }); -}); - -describe('clientNotes', () => { - it('offers the install command when no client exists', () => { - const notes = clientNotes(undefined); - expect(notes.join('\n')).toContain('npm install -g @openai/codex'); - }); - - it('points a desktop-only install at the Codex CLI', () => { - const notes = clientNotes({ kind: 'desktop', desktopPath: '/Applications/ChatGPT.app' }); - expect(notes.join('\n')).toContain('this setup configures the Codex CLI'); - }); - - it('names the CLI path when it is detected', () => { - const notes = clientNotes({ kind: 'cli', cliPath: '/usr/local/bin/codex' }); - expect(notes.join('\n')).toContain('/usr/local/bin/codex'); - }); -}); diff --git a/cli/src/detect.ts b/cli/src/detect.ts deleted file mode 100644 index 709178c0758e15039e75b2071adb4f932e760895..0000000000000000000000000000000000000000 --- a/cli/src/detect.ts +++ /dev/null @@ -1,83 +0,0 @@ -import fs from 'node:fs'; -import path from 'node:path'; - -// Client detection for the Codex CLI integration. `setup-codex` only WRITES a -// config; this module tells the user WHICH client they actually have so the -// setup note can say something true about their machine instead of a generic -// "install X" line. Purely filesystem/PATH based — no network, no spawning -// processes — so it stays fast and testable. - -export interface DetectedClient { - /** `cli` — the `codex` binary is on PATH; `desktop` — the ChatGPT desktop - * app is installed (macOS); `both` — self-explanatory. */ - kind: 'cli' | 'desktop' | 'both'; - /** Absolute path of the first `codex` binary found on PATH, when present. */ - cliPath?: string; - /** Absolute path of the ChatGPT.app bundle, when present. */ - desktopPath?: string; -} - -/** Where the ChatGPT desktop app lives per platform. Only macOS ships a - * well-known .app bundle; Windows/Linux register no stable path we can - * probe cheaply, so those return nothing (a UI integration there would need - * registry/freedesktop lookups — out of scope). */ -export function desktopAppPath(platform: string = process.platform): string | undefined { - if (platform !== 'darwin') return undefined; - return '/Applications/ChatGPT.app'; -} - -function executableOnPath(name: string): string | undefined { - const dirs = (process.env.PATH ?? '').split(path.delimiter).filter(Boolean); - for (const dir of dirs) { - const candidate = path.join(dir, name); - try { - fs.accessSync(candidate, fs.constants.X_OK); - // Directories on PATH shadow real binaries (a common nvm artifact); - // only a regular file counts. - if (fs.statSync(candidate).isFile()) return candidate; - } catch { - // Not here; keep scanning. - } - } - return undefined; -} - -/** Detects the `codex` CLI on PATH and the ChatGPT desktop app on macOS. - * Only the `codex` binary is probed: the CLI still ships and runs as - * `codex`, and that is the only surface `setup-codex` configures. */ -export function detectChatGptClient( - platform: string = process.platform, -): DetectedClient | undefined { - const cliPath = executableOnPath('codex'); - const appPath = desktopAppPath(platform); - const hasDesktop = appPath !== undefined && fs.existsSync(appPath); - if (cliPath && hasDesktop) return { kind: 'both', cliPath, desktopPath: appPath }; - if (cliPath) return { kind: 'cli', cliPath }; - if (hasDesktop) return { kind: 'desktop', desktopPath: appPath }; - return undefined; -} - -/** Human-readable guidance appended to `setup-codex` output. This setup only - * writes the Codex CLI config, so the desktop app is reported but never - * configured. */ -export function clientNotes(client: DetectedClient | undefined): string[] { - if (!client) { - return [ - 'No Codex CLI detected on this machine.', - 'Install the CLI with: npm install -g @openai/codex', - ]; - } - if (client.kind === 'desktop') { - return [ - `ChatGPT desktop app detected at ${client.desktopPath}; this setup configures the Codex CLI.`, - 'The generated configuration targets the Codex CLI — install it with: npm install -g @openai/codex', - ]; - } - if (client.kind === 'both') { - return [ - `Codex CLI detected at ${client.cliPath}; the configuration above applies to it.`, - 'ChatGPT desktop app also detected; it is not configured by this setup.', - ]; - } - return [`Codex CLI detected at ${client.cliPath}; the configuration above applies to it.`]; -} diff --git a/cli/src/doctor.test.ts b/cli/src/doctor.test.ts deleted file mode 100644 index 4e42f6e944689fa4ef9cad9ea30576bfb84e31c7..0000000000000000000000000000000000000000 --- a/cli/src/doctor.test.ts +++ /dev/null @@ -1,399 +0,0 @@ -import fs from 'node:fs'; -import os from 'node:os'; -import path from 'node:path'; -import { afterEach, describe, expect, it } from 'vitest'; -import { - DEFAULT_PROBE_TIMEOUT_MS, - claudeLayers, - codexLayers, - diagnose, - exitCodeFor, - managedSettingsDir, - managedSettingsPaths, - normalizeUrl, - probeGateway, - sameGateway, -} from './doctor.js'; - -const temporary: string[] = []; -function tempHome(): string { - const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'freellmapi-doctor-')); - temporary.push(dir); - return dir; -} -afterEach(() => { - for (const dir of temporary.splice(0)) fs.rmSync(dir, { recursive: true, force: true }); -}); - -/** A fetch double that answers /livez exactly as the real gateway does. */ -function gatewayFetch( - body: unknown = { status: 'ok', version: '0.7.0', uptime_s: 12 }, - status = 200, -): typeof fetch { - return (async () => ({ status, json: async () => body })) as unknown as typeof fetch; -} - -/** The real 503 body from routes/status.ts when the db or encryption key is down. */ -const UNAVAILABLE = { status: 'unavailable', version: '0.7.0', uptime_s: 12, checks: { db: false } }; -const deadFetch = (async () => { throw new Error('ECONNREFUSED'); }) as unknown as typeof fetch; - -function writeClaudeSettings(home: string, value: Record): void { - const dir = path.join(home, '.claude'); - fs.mkdirSync(dir, { recursive: true }); - fs.writeFileSync(path.join(dir, 'settings.json'), JSON.stringify(value)); -} - -function writeCodexConfig(home: string, toml: string): void { - const dir = path.join(home, '.codex'); - fs.mkdirSync(dir, { recursive: true }); - fs.writeFileSync(path.join(dir, 'config.toml'), toml); -} - -describe('managed settings locations', () => { - // The reported paths are built with the TARGET platform's separator, but the - // test's own `path.join` uses the HOST's — on Windows the two disagree, so - // compare separator-normalized forms (#1392). - function asPosix(p: string): string { - return p.replaceAll('\\', '/'); - } - it('uses the documented system directory for each platform', () => { - expect(managedSettingsDir('darwin')).toBe('/Library/Application Support/ClaudeCode'); - expect(managedSettingsDir('linux')).toBe('/etc/claude-code'); - expect(managedSettingsDir('win32')).toBe('C:\\Program Files\\ClaudeCode'); - }); - - it('falls back to the base file alone when there is no drop-in directory', () => { - // The usual case: an unreadable or absent managed-settings.d contributes - // nothing rather than failing the whole report. - const dir = tempHome(); - expect(managedSettingsPaths('linux', dir).map(asPosix)).toEqual([path.join(dir, 'managed-settings.json')].map(asPosix)); - }); - - it('ranks managed-settings.d drop-ins above the base file, last one first', () => { - // The drop-in files are merged alphabetically ON TOP of the base, so the - // alphabetically LAST one wins and the base loses to all of them. Getting - // this order backwards would name the wrong layer as effective for a - // fleet-managed install — the exact confident-wrong answer doctor exists - // to prevent. - const dir = tempHome(); - fs.mkdirSync(path.join(dir, 'managed-settings.d')); - for (const name of ['20-b.json', '10-a.json', 'notes.txt']) { - fs.writeFileSync(path.join(dir, 'managed-settings.d', name), '{}'); - } - - expect(managedSettingsPaths('linux', dir).map(asPosix)).toEqual([ - path.join(dir, 'managed-settings.d', '20-b.json'), - path.join(dir, 'managed-settings.d', '10-a.json'), - path.join(dir, 'managed-settings.json'), - ].map(asPosix)); - }); -}); - -describe('url comparison', () => { - it('ignores trailing slashes, a /v1 suffix, and host case', () => { - expect(normalizeUrl('http://Localhost:3000/v1/')).toBe('http://localhost:3000'); - expect(sameGateway('http://localhost:3000', 'http://localhost:3000/v1')).toBe(true); - expect(sameGateway('http://localhost:3000', 'http://localhost:3001')).toBe(false); - expect(sameGateway(undefined, 'http://localhost:3000')).toBe(false); - }); - - it('keeps a path prefix, so two mounts on one host are not confused', () => { - // A gateway mounted under a prefix is a different endpoint. Folding the - // path away reported these as the same gateway. - expect(normalizeUrl('http://host/gateway/v1/')).toBe('http://host/gateway'); - expect(sameGateway('http://host/gateway', 'http://host/other')).toBe(false); - expect(sameGateway('http://host/gateway/v1', 'http://host/gateway/')).toBe(true); - expect(sameGateway('http://host/gateway', 'http://host')).toBe(false); - }); - - it('keeps path case, which is case-sensitive, while folding the host', () => { - expect(normalizeUrl('HTTP://Host:3000/Gateway')).toBe('http://host:3000/Gateway'); - expect(sameGateway('http://host/Gateway', 'http://host/gateway')).toBe(false); - }); - - it('drops query and fragment, which never identify the endpoint', () => { - expect(normalizeUrl('http://host:3000/v1?k=1#x')).toBe('http://host:3000'); - }); - - it('falls back to lexical trimming for an unparseable value', () => { - // A malformed config value must still produce a report, not an exception. - expect(normalizeUrl('localhost:3000/v1/')).toBe('localhost:3000'); - }); -}); - -describe('claude precedence', () => { - it('ranks a settings env block ABOVE the inherited shell environment', () => { - // The easy thing to get backwards, and I did: Claude Code writes each - // `env` entry into the process environment at startup, REPLACING what the - // shell exported. The shell is the lowest layer, not the highest. - const home = tempHome(); - writeClaudeSettings(home, { env: { ANTHROPIC_BASE_URL: 'http://localhost:3000' } }); - const layers = claudeLayers({ ANTHROPIC_BASE_URL: 'https://api.anthropic.com' }, home, tempHome()); - - expect(layers[0]).toMatchObject({ value: 'http://localhost:3000', effective: true }); - expect(layers[1]).toMatchObject({ value: 'https://api.anthropic.com', effective: false }); - }); - - it('uses the shell value when no settings file names one', () => { - expect(claudeLayers({ ANTHROPIC_BASE_URL: 'http://localhost:3000' }, tempHome(), tempHome())[0]) - .toMatchObject({ value: 'http://localhost:3000', effective: true }); - }); - - it('ranks project-local settings above the user scope', () => { - const home = tempHome(); - const project = tempHome(); - writeClaudeSettings(home, { env: { ANTHROPIC_BASE_URL: 'http://user:1' } }); - fs.mkdirSync(path.join(project, '.claude'), { recursive: true }); - fs.writeFileSync( - path.join(project, '.claude', 'settings.local.json'), - JSON.stringify({ env: { ANTHROPIC_BASE_URL: 'http://project-local:2' } }), - ); - - expect(claudeLayers({}, home, project)[0]) - .toMatchObject({ value: 'http://project-local:2', effective: true }); - }); - - it('honours CLAUDE_CONFIG_DIR for the user scope', () => { - // Relocates the user scope wholesale; missing it reads a settings file the - // running tool never opens. - const home = tempHome(); - const configDir = tempHome(); - writeClaudeSettings(home, { env: { ANTHROPIC_BASE_URL: 'http://ignored:1' } }); - fs.writeFileSync( - path.join(configDir, 'settings.json'), - JSON.stringify({ env: { ANTHROPIC_BASE_URL: 'http://relocated:2' } }), - ); - - expect(claudeLayers({ CLAUDE_CONFIG_DIR: configDir }, home, tempHome())[0]?.value) - .toBe('http://relocated:2'); - }); - - it('reports no layers rather than throwing on absent or malformed settings', () => { - const home = tempHome(); - expect(claudeLayers({}, home, tempHome())).toEqual([]); - fs.mkdirSync(path.join(home, '.claude'), { recursive: true }); - fs.writeFileSync(path.join(home, '.claude', 'settings.json'), '{ not json'); - expect(claudeLayers({}, home, tempHome())).toEqual([]); - }); -}); - -describe('codex precedence', () => { - it('resolves the SELECTED provider, not the first base_url in the file', () => { - // Codex names a provider and the provider carries the url. Reading any - // base_url would report a provider the user is not selecting. - const home = tempHome(); - writeCodexConfig(home, [ - 'model_provider = "freellmapi"', - '', - '[model_providers.other]', - 'base_url = "http://not-this-one:9999/v1"', - '', - '[model_providers.freellmapi]', - 'base_url = "http://localhost:3000/v1"', - ].join('\n')); - - expect(codexLayers({}, home)[0]).toMatchObject({ - value: 'http://localhost:3000/v1', - effective: true, - }); - }); - - it('reads the dotted-key spelling too', () => { - const home = tempHome(); - writeCodexConfig(home, [ - 'model_provider = "freellmapi"', - 'model_providers.freellmapi.base_url = "http://localhost:3000/v1"', - ].join('\n')); - expect(codexLayers({}, home)[0]?.value).toBe('http://localhost:3000/v1'); - }); - - it('honours CODEX_HOME over the default location', () => { - const home = tempHome(); - const alternate = tempHome(); - writeCodexConfig(home, 'model_provider = "a"\n[model_providers.a]\nbase_url = "http://default:1/v1"'); - writeCodexConfig(alternate, 'model_provider = "b"\n[model_providers.b]\nbase_url = "http://override:2/v1"'); - - const layers = codexLayers({ CODEX_HOME: path.join(alternate, '.codex') }, home); - expect(layers[0]?.value).toBe('http://override:2/v1'); - }); - - it('records a selected provider that names no base_url instead of skipping it', () => { - const home = tempHome(); - writeCodexConfig(home, 'model_provider = "freellmapi"\n'); - const layer = codexLayers({}, home)[0]; - expect(layer.effective).toBe(true); - expect(layer.value).toBeUndefined(); - expect(layer.source).toContain('no base_url found'); - }); -}); - -describe('verdicts', () => { - const url = 'http://localhost:3000'; - - it('routed only when the tool reaches THIS gateway', async () => { - const report = await diagnose('claude', { - expectedUrl: url, - env: { ANTHROPIC_BASE_URL: url }, - homeDir: tempHome(), cwd: tempHome(), - fetchImpl: gatewayFetch(), - }); - expect(report.verdict).toBe('routed'); - expect(report.gateway?.version).toBe('0.7.0'); - }); - - it('elsewhere — not routed — when the tool reaches a different host that is alive', async () => { - // The regression this guards: reporting `routed` for a Claude Code pinned - // to api.anthropic.com, because it does reach what it is configured to - // reach. That is a confident wrong answer to the question being asked. - const report = await diagnose('claude', { - expectedUrl: url, - env: { ANTHROPIC_BASE_URL: 'https://api.anthropic.com' }, - homeDir: tempHome(), cwd: tempHome(), - fetchImpl: gatewayFetch(), - }); - expect(report.verdict).toBe('elsewhere'); - expect(report.detail).toContain('does not touch this gateway'); - }); - - it('elsewhere when the right port answers, but not the way this gateway does', async () => { - const report = await diagnose('claude', { - expectedUrl: url, - env: { ANTHROPIC_BASE_URL: url }, - homeDir: tempHome(), cwd: tempHome(), - fetchImpl: gatewayFetch({ hello: 'some other service' }), - }); - expect(report.verdict).toBe('elsewhere'); - }); - - it('shadowed names the layer that wins, and the value it beat', async () => { - const home = tempHome(); - writeClaudeSettings(home, { env: { ANTHROPIC_BASE_URL: url } }); - const report = await diagnose('claude', { - expectedUrl: url, - env: { ANTHROPIC_BASE_URL: 'https://api.anthropic.com' }, - homeDir: home, cwd: tempHome(), - fetchImpl: gatewayFetch(), - }); - - expect(report.verdict).toBe('shadowed'); - // The settings block is what wins, so it is what shadows — and the shell - // export the user probably set by hand is what got overridden. - expect(report.shadowedBy).toContain('settings.json'); - expect(report.detail).toContain('process environment'); - expect(report.detail).toContain('https://api.anthropic.com'); - }); - - it('degraded — not routed, not elsewhere — when this gateway says it cannot serve', async () => { - // Routing is correct and the gateway is unmistakably this one; it is sick. - // `routed` would send the user hunting through config that is already - // right, and `elsewhere` would blame a service that is not there. - const report = await diagnose('claude', { - expectedUrl: url, - env: { ANTHROPIC_BASE_URL: url }, - homeDir: tempHome(), cwd: tempHome(), - fetchImpl: gatewayFetch(UNAVAILABLE, 503), - }); - expect(report.verdict).toBe('degraded'); - expect(report.detail).toContain('that IS this gateway'); - expect(report.detail).toContain('unavailable'); - expect(report.gateway).toMatchObject({ identified: true, healthy: false }); - }); - - it('unreachable when the configured endpoint answers nothing', async () => { - const report = await diagnose('claude', { - expectedUrl: url, - env: { ANTHROPIC_BASE_URL: url }, - homeDir: tempHome(), cwd: tempHome(), - fetchImpl: deadFetch, - }); - expect(report.verdict).toBe('unreachable'); - expect(report.detail).toContain('ECONNREFUSED'); - }); - - it('unknown when nothing configures the tool at all', async () => { - const report = await diagnose('claude', { - expectedUrl: url, - env: {}, - homeDir: tempHome(), cwd: tempHome(), - fetchImpl: gatewayFetch(), - }); - expect(report.verdict).toBe('unknown'); - expect(report.detail).toContain('vendor default'); - }); - - it('unknown, not a crash, for a tool doctor does not model', async () => { - const report = await diagnose('aider', { expectedUrl: url, env: {}, homeDir: tempHome() }); - expect(report.verdict).toBe('unknown'); - expect(report.detail).toContain('claude'); - }); -}); - -describe('probeGateway', () => { - it('requires all three /livez fields before claiming identity', async () => { - // /livez carries no product marker, so the shape is the only evidence - // available. Two of three fields is not it. - expect((await probeGateway('http://x', gatewayFetch({ status: 'ok', version: '1' }))).identified) - .toBe(false); - expect((await probeGateway('http://x', gatewayFetch())).identified).toBe(true); - }); - - it('treats a non-JSON body as reachable but unidentified', async () => { - const htmlFetch = (async () => ({ - status: 200, - json: async () => { throw new Error('not json'); }, - })) as unknown as typeof fetch; - expect(await probeGateway('http://x', htmlFetch)) - .toMatchObject({ reachable: true, identified: false, healthy: false }); - }); - - it('still IDENTIFIES the gateway when it answers 503 unavailable', async () => { - // The server really emits this when the db or encryption key is down, and - // it is unmistakably this gateway. Folding health into identity would - // report a correctly-routed sick gateway as a foreign service. - expect(await probeGateway('http://x', gatewayFetch(UNAVAILABLE, 503))).toMatchObject({ - reachable: true, - identified: true, - healthy: false, - serviceStatus: 'unavailable', - status: 503, - }); - }); - - it('treats an unrecognized status value as unhealthy, not unidentified', async () => { - // A status added later must degrade into a truthful "this gateway says - // ", never into a claim that it is some other service. - const probe = await probeGateway( - 'http://x', - gatewayFetch({ status: 'draining', version: '9', uptime_s: 1 }), - ); - expect(probe).toMatchObject({ identified: true, healthy: false, serviceStatus: 'draining' }); - }); - - it('is unhealthy when a non-2xx carries an ok body', async () => { - expect((await probeGateway('http://x', gatewayFetch(undefined, 500))).healthy).toBe(false); - }); - - it('honours the caller-supplied timeout instead of the hard-coded default', async () => { - // Threaded so a slow link is not misreported as unreachable. That the - // caller's 20ms fires at all is the proof: the 5s default would not have. - const neverAnswers = (async (_url: string, init: RequestInit) => new Promise((_resolve, reject) => { - init.signal?.addEventListener('abort', () => reject(new Error('The operation was aborted'))); - })) as unknown as typeof fetch; - - const started = Date.now(); - const probe = await probeGateway('http://x', neverAnswers, 20); - expect(Date.now() - started).toBeLessThan(DEFAULT_PROBE_TIMEOUT_MS); - expect(probe).toMatchObject({ reachable: false, identified: false, healthy: false }); - expect(probe.error).toContain('abort'); - }); -}); - -describe('exit code', () => { - it('is zero only when every tool is routed', () => { - expect(exitCodeFor([{ verdict: 'routed' } as never])).toBe(0); - expect(exitCodeFor([{ verdict: 'routed' } as never, { verdict: 'elsewhere' } as never])).toBe(1); - expect(exitCodeFor([{ verdict: 'unknown' } as never])).toBe(1); - // A gateway that cannot serve is a failed precondition, not a pass. - expect(exitCodeFor([{ verdict: 'degraded' } as never])).toBe(1); - }); -}); diff --git a/cli/src/doctor.ts b/cli/src/doctor.ts deleted file mode 100644 index e19b1e3f8b19c18106dfdbc7da7af97fb70fc8ca..0000000000000000000000000000000000000000 --- a/cli/src/doctor.ts +++ /dev/null @@ -1,508 +0,0 @@ -// `freellmapi doctor` — does this tool's traffic actually reach this gateway? -// -// The load-bearing constraint: the verdict must be computed in a process that -// INHERITS THE USER'S SESSION ENVIRONMENT. The server cannot answer this -// question, because the failure being diagnosed is a request that never -// arrives — there is nothing in any server-side log to look at. So the check -// resolves each tool's effective connection the way that tool resolves it, in -// this process, and only then asks the gateway whether it is alive. -// -// The motivating failure is real and silent: a launcher that sets -// ANTHROPIC_BASE_URL into the process environment overrides the `env` block of -// ~/.claude/settings.json, so a user who edited settings.json is routed -// somewhere else entirely and nothing says so. - -import fs from 'node:fs'; -import os from 'node:os'; -import path from 'node:path'; - -// 'elsewhere' is not decoration. Without it the first real run of this command -// reported `OK claude: routed` for a Claude Code pinned to api.anthropic.com — -// technically "it reaches what it is configured to reach", and precisely the -// confident-wrong answer the command exists to prevent. `routed` now means -// "reaches THIS gateway", which is the only question the user is asking. -export type Verdict = 'routed' | 'degraded' | 'elsewhere' | 'shadowed' | 'unreachable' | 'unknown'; - -export interface Layer { - /** Where the value came from, in the tool's own vocabulary. */ - source: string; - value?: string; - /** True for the layer whose value the tool will actually use. */ - effective: boolean; -} - -export interface ToolReport { - tool: string; - verdict: Verdict; - /** Every layer that set a value, highest precedence first. */ - layers: Layer[]; - /** The base URL the tool will really use, if one could be determined. */ - effectiveUrl?: string; - /** For 'shadowed': the layer that beat the one the user probably edited. */ - shadowedBy?: string; - /** Gateway identity, when the effective URL was probed. */ - gateway?: GatewayProbe; - detail: string; -} - -export interface GatewayProbe { - reachable: boolean; - /** Whether the response looks like this product. See probeGateway. */ - identified: boolean; - /** Whether the gateway says it can actually serve requests right now. */ - healthy: boolean; - /** The `status` field verbatim: 'ok', 'unavailable', or anything added later. */ - serviceStatus?: string; - version?: string; - /** HTTP status of the /livez response. */ - status?: number; - error?: string; -} - -/** - * Normalize for comparison: scheme + host + port + path prefix, with any - * trailing slash and `/v1` suffix removed. - * - * The path is KEPT, and kept case-sensitively. A gateway mounted under a prefix - * (`http://host/gateway`) is a different endpoint from `http://host/other`, and - * folding the path away would report the two as the same gateway. Only the - * scheme and host are case-folded, because only those are case-insensitive. - * Query and fragment are dropped: they never identify the endpoint. - * - * The result is also what the probe is built on, so it must stay a usable URL. - */ -export function normalizeUrl(url: string): string { - const trimmed = url.trim(); - const lexical = (): string => trimmed.replace(/\/+$/, '').replace(/\/v1$/i, '').toLowerCase(); - let parsed: URL; - try { - parsed = new URL(trimmed); - } catch { - // Not parseable at all — fall back to lexical trimming rather than - // throwing, so a malformed config value still yields a report. - return lexical(); - } - // A scheme-less value like `localhost:3000/v1` PARSES, as the opaque path - // `3000/v1` under a `localhost:` scheme, and rebuilding it from parts would - // produce nonsense. An empty host is the tell. - if (!parsed.host) return lexical(); - const prefix = parsed.pathname.replace(/\/+$/, '').replace(/\/v1$/i, ''); - return `${parsed.protocol.toLowerCase()}//${parsed.host.toLowerCase()}${prefix}`; -} - -export function sameGateway(a: string | undefined, b: string | undefined): boolean { - if (!a || !b) return false; - return normalizeUrl(a) === normalizeUrl(b); -} - -function readJsonIfPresent(file: string): Record | undefined { - try { - if (!fs.existsSync(file)) return undefined; - // Claude Code's settings.json is strict JSON, not JSONC. - return JSON.parse(fs.readFileSync(file, 'utf8')) as Record; - } catch { - return undefined; - } -} - -/** The system directory holding Claude Code's MANAGED settings — the scope - * nothing else can override. */ -export function managedSettingsDir(platform: string = process.platform): string { - if (platform === 'win32') return 'C:\\Program Files\\ClaudeCode'; - if (platform === 'darwin') return '/Library/Application Support/ClaudeCode'; - return '/etc/claude-code'; -} - -/** - * Managed settings files, HIGHEST PRECEDENCE FIRST. - * - * Not one file. The same directory also supports a `managed-settings.d/` - * drop-in, whose `*.json` files are merged alphabetically ON TOP of the base - * `managed-settings.json` — so a drop-in outranks the base, and the - * alphabetically LAST drop-in outranks the ones before it. Reading only the - * base file is how this command would confidently name the wrong effective - * URL for exactly the fleet-managed install most likely to have one imposed: - * it would report the layer the operator can see and miss the one actually - * winning. - * - * An unreadable directory (the usual case — it does not exist) contributes - * nothing rather than failing the report. - */ -export function managedSettingsPaths( - platform: string = process.platform, - dir: string = managedSettingsDir(platform), -): string[] { - const separator = platform === 'win32' ? '\\' : '/'; - const dropInDir = `${dir}${separator}managed-settings.d`; - let dropIns: string[] = []; - try { - dropIns = fs.readdirSync(dropInDir) - .filter(name => name.endsWith('.json')) - .sort() - .reverse() // alphabetically last is merged last, so it wins - .map(name => `${dropInDir}${separator}${name}`); - } catch { - dropIns = []; - } - return [...dropIns, `${dir}${separator}managed-settings.json`]; -} - -/** ANTHROPIC_BASE_URL out of a settings file's `env` block. */ -function envBlockUrl(file: string): string | undefined { - const block = readJsonIfPresent(file)?.env; - if (!block || typeof block !== 'object' || Array.isArray(block)) return undefined; - const value = (block as Record).ANTHROPIC_BASE_URL; - return typeof value === 'string' && value ? value : undefined; -} - -/** - * Claude Code's base-URL layers, HIGHEST PRECEDENCE FIRST. - * - * The ordering that matters, and the one it is easy to get backwards: a - * settings file's `env` block BEATS the inherited shell environment. Claude - * Code writes each `env` entry into the process environment at startup and - * again when the file changes, replacing what the shell exported. So the - * process environment is the LOWEST layer here, not the highest. - * - * The familiar "my launcher overrode my settings.json" case is not a - * counter-example: a launcher that wins is writing MANAGED settings, which - * outrank every other scope — note that the rest of that same `env` block - * still applies, which is what makes the override so hard to see by reading - * config alone. - */ -export function claudeLayers( - env: NodeJS.ProcessEnv, - homeDir: string, - cwd: string = process.cwd(), - platform: string = process.platform, -): Layer[] { - const layers: Layer[] = []; - - for (const managed of managedSettingsPaths(platform)) { - const value = envBlockUrl(managed); - if (value) layers.push({ source: `${managed} (MANAGED env block)`, value, effective: false }); - } - - for (const file of [ - path.join(cwd, '.claude', 'settings.local.json'), - path.join(cwd, '.claude', 'settings.json'), - // CLAUDE_CONFIG_DIR relocates the user scope wholesale; missing it reads - // a settings file the running tool never opens. - path.join(env.CLAUDE_CONFIG_DIR ?? path.join(homeDir, '.claude'), 'settings.json'), - ]) { - const value = envBlockUrl(file); - if (value) layers.push({ source: `${file} (env block)`, value, effective: false }); - } - - if (env.ANTHROPIC_BASE_URL) { - layers.push({ - source: 'ANTHROPIC_BASE_URL (process environment)', - value: env.ANTHROPIC_BASE_URL, - effective: false, - }); - } - - if (layers.length) layers[0].effective = true; - return layers; -} - -/** - * Codex's base-URL layers, highest precedence first. - * - * Codex resolves a PROVIDER, not a URL: `model_provider` names a key in the - * `[model_providers]` table and that entry carries base_url. Reading only - * `base_url` occurrences would report a provider the user is not selecting, - * so the selected name is resolved first. - */ -export function codexLayers( - env: NodeJS.ProcessEnv, - homeDir: string, -): Layer[] { - const layers: Layer[] = []; - const configPath = path.join(env.CODEX_HOME ?? path.join(homeDir, '.codex'), 'config.toml'); - let toml = ''; - try { - toml = fs.existsSync(configPath) ? fs.readFileSync(configPath, 'utf8') : ''; - } catch { - toml = ''; - } - - const selected = /^\s*model_provider\s*=\s*["']([^"']+)["']/m.exec(toml)?.[1]; - if (selected) { - // Both spellings the file format allows: a dotted key and a section. - const dotted = new RegExp( - `^\\s*model_providers\\.${escapeRegExp(selected)}\\.base_url\\s*=\\s*["']([^"']+)["']`, - 'm', - ).exec(toml)?.[1]; - const section = sectionValue(toml, `model_providers.${selected}`, 'base_url'); - const url = dotted ?? section; - if (url) { - layers.push({ - source: `${configPath} ([model_providers.${selected}] base_url)`, - value: url, - effective: false, - }); - } else { - layers.push({ - source: `${configPath} (model_provider = "${selected}", no base_url found)`, - effective: false, - }); - } - } - - if (layers.length) layers[0].effective = true; - return layers; -} - -function escapeRegExp(value: string): string { - return value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); -} - -/** Value of `key` inside a `[section]` block of a TOML document. */ -function sectionValue(toml: string, section: string, key: string): string | undefined { - const lines = toml.split('\n'); - let inSection = false; - for (const raw of lines) { - const line = raw.trim(); - const header = /^\[([^\]]+)\]$/.exec(line); - if (header) { - inSection = header[1].trim() === section; - continue; - } - if (!inSection) continue; - const match = new RegExp(`^${escapeRegExp(key)}\\s*=\\s*["']([^"']+)["']`).exec(line); - if (match) return match[1]; - } - return undefined; -} - -/** How long the /livez probe waits before calling an endpoint unreachable. */ -export const DEFAULT_PROBE_TIMEOUT_MS = 5_000; - -/** - * Probe a base URL's identity AND its health — two separate questions that must - * not be collapsed. - * - * IDENTITY is structural: `/livez` answers `{status, version, uptime_s}` and - * carries NO product identifier, so a body with that exact shape is evidence, - * not proof — another service could serve the same three keys. `identified` - * therefore means "responds the way this gateway responds", and the report says - * so. Asserting identity harder would need either an authenticated call (the - * CLI does not have a key here) or a product marker the server does not emit. - * - * HEALTH is the `status` VALUE, and it is deliberately not folded into - * identity. The server really does emit `{status: 'unavailable', ...}` with a - * 503 when the database or the encryption key is down (routes/status.ts), and - * that response is still unmistakably this gateway. Requiring `status === 'ok'` - * to identify it would report a correctly-routed but sick gateway as "probably - * a different service on that port" — the confident wrong answer this command - * exists to prevent. Anything that is not a 2xx `'ok'` is reported as - * unhealthy and the raw status is echoed, so a status added later degrades into - * a truthful "reaches this gateway, which says: " instead of a lie. - */ -export async function probeGateway( - baseUrl: string, - fetchImpl: typeof fetch = fetch, - timeoutMs: number = DEFAULT_PROBE_TIMEOUT_MS, -): Promise { - let response: Response; - try { - response = await fetchImpl(`${normalizeUrl(baseUrl)}/livez`, { - signal: AbortSignal.timeout(timeoutMs), - }); - } catch (error) { - return { - reachable: false, - identified: false, - healthy: false, - error: error instanceof Error ? error.message : String(error), - }; - } - let body: Record = {}; - try { - body = await response.json() as Record; - } catch { - return { - reachable: true, - identified: false, - healthy: false, - status: response.status, - error: 'response was not JSON', - }; - } - const serviceStatus = typeof body.status === 'string' ? body.status : undefined; - const identified = serviceStatus !== undefined - && typeof body.version === 'string' - && typeof body.uptime_s === 'number'; - return { - reachable: true, - identified, - healthy: identified && serviceStatus === 'ok' && response.status >= 200 && response.status < 300, - serviceStatus, - status: response.status, - version: typeof body.version === 'string' ? body.version : undefined, - }; -} - -export interface DoctorOptions { - /** The gateway the user believes they are using (--url / FREELLMAPI_URL). */ - expectedUrl: string; - env?: NodeJS.ProcessEnv; - homeDir?: string; - /** Project-scoped settings are resolved relative to this. */ - cwd?: string; - fetchImpl?: typeof fetch; - /** Probe timeout. Raise it on a slow link, where the default would report a - * reachable gateway as unreachable. */ - timeoutMs?: number; -} - -const LAYER_RESOLVERS: Record Layer[]> = { - claude: (env, homeDir, cwd) => claudeLayers(env, homeDir, cwd), - codex: (env, homeDir) => codexLayers(env, homeDir), -}; - -export const DOCTOR_TOOLS = Object.keys(LAYER_RESOLVERS); - -export async function diagnose(tool: string, options: DoctorOptions): Promise { - const env = options.env ?? process.env; - const homeDir = options.homeDir ?? os.homedir(); - const resolve = LAYER_RESOLVERS[tool]; - if (!resolve) { - return { - tool, - verdict: 'unknown', - layers: [], - detail: `doctor does not know how ${tool} resolves its endpoint. Known tools: ${DOCTOR_TOOLS.join(', ')}.`, - }; - } - - const layers = resolve(env, homeDir, options.cwd ?? process.cwd()); - const effective = layers.find(layer => layer.effective); - - if (!effective?.value) { - return { - tool, - verdict: 'unknown', - layers, - detail: layers.length - ? 'A configuration layer was found but names no base URL, so the tool falls back to its vendor default.' - : `No configuration found. ${tool} will use its vendor default endpoint, not this gateway.`, - }; - } - - const gateway = await probeGateway(effective.value, options.fetchImpl, options.timeoutMs); - - // A lower-precedence layer naming a DIFFERENT url is the silent-override - // case: the user edited that file and something outranks it. - const overridden = layers.find( - layer => !layer.effective && layer.value && !sameGateway(layer.value, effective.value), - ); - - if (!gateway.reachable) { - return { - tool, - verdict: 'unreachable', - layers, - effectiveUrl: effective.value, - gateway, - detail: `${tool} is configured to use ${effective.value}, but nothing answered there (${gateway.error}).`, - }; - } - - if (overridden) { - return { - tool, - verdict: 'shadowed', - layers, - effectiveUrl: effective.value, - shadowedBy: effective.source, - gateway, - detail: - `${overridden.source} sets ${overridden.value}, but ${effective.source} outranks it, ` - + `so ${tool} actually uses ${effective.value}.`, - }; - } - - // Two distinct ways to be pointed somewhere else, and both must fail the - // check: a different host entirely, or the right host answering nothing that - // looks like this gateway. - if (!sameGateway(effective.value, options.expectedUrl)) { - return { - tool, - verdict: 'elsewhere', - layers, - effectiveUrl: effective.value, - gateway, - detail: - `${tool} reaches ${effective.value}, NOT the ${options.expectedUrl} this command was pointed at. ` - + 'Its traffic does not touch this gateway.', - }; - } - - if (!gateway.identified) { - return { - tool, - verdict: 'elsewhere', - layers, - effectiveUrl: effective.value, - gateway, - detail: - `Something is listening at ${effective.value} and ${tool} would reach it, but it did not answer ` - + '/livez the way this gateway does — so it is probably a different service on that port.', - }; - } - - // Routing is correct here and the gateway is unmistakably this one — it just - // says it cannot serve. Reporting that as `routed` would send the user - // hunting through config that is already right, so it gets its own verdict - // and a nonzero exit. - if (!gateway.healthy) { - return { - tool, - verdict: 'degraded', - layers, - effectiveUrl: effective.value, - gateway, - detail: - `${tool} reaches ${effective.value} and that IS this gateway, but it reports ` - + `status "${gateway.serviceStatus}" (HTTP ${gateway.status}) — it cannot serve requests. ` - + 'The routing is fine; the gateway is not.', - }; - } - - return { - tool, - verdict: 'routed', - layers, - effectiveUrl: effective.value, - gateway, - detail: `${tool} reaches ${effective.value}${gateway.version ? ` (gateway v${gateway.version})` : ''}.`, - }; -} - -const SYMBOL: Record = { - routed: 'OK ', - degraded: 'WARN', - elsewhere: 'FAIL', - shadowed: 'WARN', - unreachable: 'FAIL', - unknown: '? ', -}; - -export function formatReport(report: ToolReport): string { - const lines = [`${SYMBOL[report.verdict]} ${report.tool}: ${report.verdict}`, ` ${report.detail}`]; - for (const layer of report.layers) { - lines.push(` ${layer.effective ? '->' : ' '} ${layer.source}${layer.value ? ` = ${layer.value}` : ''}`); - } - if (report.gateway?.reachable && !report.gateway.identified) { - lines.push(' note: something answered, but not with this gateway\'s /livez shape.'); - } - return lines.join('\n'); -} - -/** Exit code: 0 when every tool is routed, 1 otherwise — so it is usable in a - * script, not only by eye. */ -export function exitCodeFor(reports: ToolReport[]): number { - return reports.every(report => report.verdict === 'routed') ? 0 : 1; -} diff --git a/cli/src/index.test.ts b/cli/src/index.test.ts deleted file mode 100644 index 19776d6a8a06a1d4dd66f93a8679bb487c484e9e..0000000000000000000000000000000000000000 --- a/cli/src/index.test.ts +++ /dev/null @@ -1,271 +0,0 @@ -import fs from 'node:fs'; -import os from 'node:os'; -import path from 'node:path'; -import { afterEach, describe, expect, it } from 'vitest'; -import { claudeLaunchEnv, codexArgs, main, parseArgs, resolvePinnedModel, unsupportedNodeVersion } from './index.js'; -import { UnknownModelError } from './models.js'; -import type { CatalogModel } from './types.js'; - -describe('CLI arguments and launchers', () => { - it('defaults to the gateway port when no URL is configured', () => { - const previous = process.env.FREELLMAPI_URL; - delete process.env.FREELLMAPI_URL; - try { - expect(parseArgs(['setup-generic']).options.url).toBe('http://localhost:3001'); - } finally { - if (previous === undefined) delete process.env.FREELLMAPI_URL; - else process.env.FREELLMAPI_URL = previous; - } - }); - - it('parses setup options before or after the command', () => { - expect(parseArgs([ - '--url', - 'http://localhost:3001/v1', - 'setup-codex', - '--profile=work', - '--dry-run', - ])).toMatchObject({ - command: 'setup-codex', - options: { - url: 'http://localhost:3001/v1', - profile: 'work', - dryRun: true, - }, - }); - }); - - it('rejects profile names that can escape the intended config directory', () => { - expect(() => parseArgs(['setup-codex', '--profile', '../../outside'])) - .toThrow('--profile must use only'); - expect(() => parseArgs(['setup-codex', '--profile', '..'])) - .toThrow('--profile must use only'); - }); - - it('rejects a missing option value instead of consuming the next flag', () => { - expect(() => parseArgs(['setup-codex', '--profile', '--dry-run'])) - .toThrow('--profile requires a value'); - }); - - it('parses doctor --timeout, and rejects a value that is not milliseconds', () => { - expect(parseArgs(['doctor', '--timeout', '30000']).options.timeoutMs).toBe(30_000); - expect(parseArgs(['doctor', '--timeout=250']).options.timeoutMs).toBe(250); - // Left undefined rather than defaulted here, so doctor.ts owns the default. - expect(parseArgs(['doctor']).options.timeoutMs).toBeUndefined(); - // Rejected, not silently ignored: a typo quietly becoming the default is - // exactly the quiet no-op this command exists to catch. - expect(() => parseArgs(['doctor', '--timeout', '5s'])).toThrow('positive number of milliseconds'); - expect(() => parseArgs(['doctor', '--timeout', '0'])).toThrow('positive number of milliseconds'); - }); - - it('still rejects a stray positional for commands that take none', async () => { - // `doctor` is the only command with positional arguments, so parseArgs - // collects them generically. The dispatcher has to reject them for every - // other command BEFORE dispatch — checked after the setup-* branch, the - // stray word would be silently ignored instead of erroring. - await expect(main(['setup-claude', 'typo'])).rejects.toThrow('Unknown option: typo'); - await expect(main(['launch', 'typo'])).rejects.toThrow('Unknown option: typo'); - // ...and doctor still takes them. - expect(parseArgs(['doctor', 'claude']).options.args).toEqual(['claude']); - }); - - const models: CatalogModel[] = [ - { id: 'auto' }, - { id: 'fast-coder', available: true, context_window: 131072 }, - { id: 'reasoning-model', available: true, context_window: 262144 }, - ]; - const baseOptions = { - url: 'http://localhost:3000', - profile: 'default', - dryRun: false, - }; - const temporary: string[] = []; - - afterEach(() => { - for (const directory of temporary.splice(0)) { - fs.rmSync(directory, { recursive: true, force: true }); - } - }); - - it.skipIf(process.platform !== 'win32')('launches Windows command shims for both agents', async () => { - const directory = fs.mkdtempSync(path.join(os.tmpdir(), 'freellmapi-launch-')); - temporary.push(directory); - for (const command of ['codex', 'claude']) { - fs.writeFileSync(path.join(directory, `${command}.cmd`), - `@echo off\r\necho ${command} > "%FREELLMAPI_TEST_MARKER%"\r\nexit /b 17\r\n`); - } - const previousPath = process.env.PATH; - const previousMarker = process.env.FREELLMAPI_TEST_MARKER; - const previousFetch = globalThis.fetch; - const marker = path.join(directory, 'launched.txt'); - process.env.PATH = `${directory};${process.env.SystemRoot}\\System32`; - process.env.FREELLMAPI_TEST_MARKER = marker; - globalThis.fetch = async () => new Response(JSON.stringify({ - data: [{ id: 'test-model', available: true }], - }), { headers: { 'Content-Type': 'application/json' } }); - try { - for (const [command, agent] of [['launch-codex', 'codex'], ['launch', 'claude']]) { - fs.rmSync(marker, { force: true }); - expect(await main([command, '--url', 'http://127.0.0.1:3001', '--api-key', 'test-key'])) - .toBe(17); - expect(fs.readFileSync(marker, 'utf8').trim()).toBe(agent); - } - } finally { - globalThis.fetch = previousFetch; - if (previousPath === undefined) delete process.env.PATH; - else process.env.PATH = previousPath; - if (previousMarker === undefined) delete process.env.FREELLMAPI_TEST_MARKER; - else process.env.FREELLMAPI_TEST_MARKER = previousMarker; - } - }); - - it('uses --model for ANTHROPIC_MODEL and defaults to the first available model', () => { - const explicit = claudeLaunchEnv( - { ...baseOptions, model: 'reasoning-model' }, - 'unified-key', - models, - {}, - '/home/tester', - ); - expect(explicit.ANTHROPIC_MODEL).toBe('reasoning-model'); - expect(explicit.CLAUDE_CODE_AUTO_COMPACT_WINDOW).toBe('262144'); - - const fallback = claudeLaunchEnv(baseOptions, 'unified-key', models, {}, '/home/tester'); - expect(fallback.ANTHROPIC_MODEL).toBe('fast-coder'); - expect(fallback.ANTHROPIC_AUTH_TOKEN).toBe('unified-key'); - expect(fallback.ANTHROPIC_BASE_URL).toBe('http://localhost:3000'); - }); - - it('never uses the profile name as a model id', () => { - const home = fs.mkdtempSync(path.join(os.tmpdir(), 'freellmapi-cli-')); - temporary.push(home); - const profileDir = path.join(home, '.claude', 'profiles', 'work'); - fs.mkdirSync(profileDir, { recursive: true }); - const env = claudeLaunchEnv( - { ...baseOptions, profile: 'work' }, - 'unified-key', - models, - {}, - home, - ); - expect(env.ANTHROPIC_MODEL).toBe('fast-coder'); - expect(env.CLAUDE_CONFIG_DIR).toBe(profileDir); - }); - - it('rejects a profile whose configuration directory does not exist', () => { - const home = fs.mkdtempSync(path.join(os.tmpdir(), 'freellmapi-cli-')); - temporary.push(home); - expect(() => claudeLaunchEnv( - { ...baseOptions, profile: 'missing' }, - 'unified-key', - models, - {}, - home, - )).toThrow("freellmapi setup-claude --profile missing"); - }); - - it('strips the user\'s real Anthropic credentials from the child environment', () => { - const env = claudeLaunchEnv(baseOptions, 'unified-key', models, { - ANTHROPIC_API_KEY: 'real-anthropic-key', - ANTHROPIC_AUTH_TOKEN: 'real-anthropic-token', - PATH: '/usr/bin', - }, '/home/tester'); - expect(env.ANTHROPIC_API_KEY).toBeUndefined(); - expect(env.ANTHROPIC_AUTH_TOKEN).toBe('unified-key'); - expect(env.PATH).toBe('/usr/bin'); - expect(JSON.stringify(env)).not.toContain('real-anthropic'); - }); - - describe('resolvePinnedModel', () => { - const available: CatalogModel[] = [{ id: 'auto' }, { id: 'fast-coder', available: true }]; - const full: CatalogModel[] = [...available, { id: 'benched', available: false }]; - - function collect(): { warnings: string[]; warn: (message: string) => void } { - const warnings: string[] = []; - return { warnings, warn: message => { warnings.push(message); } }; - } - - it('is silent for an available pinned model', () => { - const { warnings, warn } = collect(); - expect(resolvePinnedModel('fast-coder', { available, full }, warn)?.id).toBe('fast-coder'); - expect(warnings).toEqual([]); - }); - - it('warns but still resolves a registered-but-unavailable model', () => { - const { warnings, warn } = collect(); - expect(resolvePinnedModel('benched', { available, full }, warn)) - .toMatchObject({ id: 'benched', unavailable: true }); - expect(warnings.join()).toContain('not currently available'); - }); - - it('WARNS when the unfiltered roster could not be fetched', () => { - // Silently degrading to the filtered roster would reinstate exactly the - // ambiguity this pair of fetches removes: the user would see a quota - // problem reported as a typo, with nothing saying the check was weaker. - const { warnings, warn } = collect(); - resolvePinnedModel( - 'fast-coder', - { available, full: available, degradedReason: 'HTTP 400' }, - warn, - ); - expect(warnings.join()).toContain('could not fetch the unfiltered model catalog'); - expect(warnings.join()).toContain('HTTP 400'); - }); - - it('still reports an id in neither roster as unknown, degraded or not', () => { - const { warn } = collect(); - expect(() => resolvePinnedModel('nope', { available, full }, warn)) - .toThrow(UnknownModelError); - }); - - it('does nothing at all when no model was pinned', () => { - // Nothing to warn about: the launcher picks from the filtered roster, - // where there is no unknown-vs-unavailable ambiguity to lose. - const { warnings, warn } = collect(); - expect(resolvePinnedModel(undefined, { available, full, degradedReason: 'x' }, warn)) - .toBeUndefined(); - expect(warnings).toEqual([]); - }); - }); - - it('passes a complete Responses provider to Codex', () => { - const args = codexArgs('http://localhost:3001/v1/', 'coder'); - expect(args).toContain('model_provider="freellmapi"'); - expect(args).toContain('model="coder"'); - expect(args).toContain('model_providers.freellmapi.name="FreeLLMAPI"'); - expect(args).toContain( - 'model_providers.freellmapi.base_url="http://localhost:3001/v1"', - ); - expect(args).toContain('model_providers.freellmapi.wire_api="responses"'); - }); - - // #1283: on Node <17.4 the CLI died at module-load time with - // ERR_UNKNOWN_BUILTIN_MODULE because `node:readline/promises` was imported - // statically, so no command — not even --help — could run, let alone print - // a readable reason. Two layers fix it: the readline import is now lazy - // (only the interactive key prompt needs it), and this pure check turns an - // old runtime into one clear sentence before main() runs. - describe('unsupportedNodeVersion (#1283)', () => { - it('accepts Node 20 and newer', () => { - expect(unsupportedNodeVersion('20.18.0')).toBeNull(); - expect(unsupportedNodeVersion('22.5.1')).toBeNull(); - expect(unsupportedNodeVersion('26.9.0')).toBeNull(); - }); - - it('rejects the older runtimes from the issue reports', () => { - for (const version of ['12.22.12', '14.21.3', '16.20.2', '18.19.0', '19.9.0']) { - const message = unsupportedNodeVersion(version); - expect(message).toContain('Node.js 20 or newer'); - expect(message).toContain(version); - } - }); - - it('says nothing when the runtime is unrecognised', () => { - // A non-Node runtime (or a mocked process) must not be told it is - // "old"; the failure, if any, should come from the code itself. - expect(unsupportedNodeVersion(undefined)).toBeNull(); - expect(unsupportedNodeVersion('')).toBeNull(); - expect(unsupportedNodeVersion('nightly')).toBeNull(); - }); - }); -}); diff --git a/cli/src/index.ts b/cli/src/index.ts deleted file mode 100644 index e765259c51f32b1f014f26f208f0d467046c5642..0000000000000000000000000000000000000000 --- a/cli/src/index.ts +++ /dev/null @@ -1,534 +0,0 @@ -#!/usr/bin/env node -import os from 'node:os'; -import fs from 'node:fs'; -import path from 'node:path'; -import process from 'node:process'; -import spawn from 'cross-spawn'; -import { Writable } from 'node:stream'; -import { fileURLToPath } from 'node:url'; -import { applyGeneratedFiles, printDryRunDiff } from './config-files.js'; -import { detectChatGptClient } from './detect.js'; -import { getTool, tools } from './tools.js'; -import { resolveLaunchModel, type ResolvedModel } from './models.js'; -import { DOCTOR_TOOLS, diagnose, exitCodeFor, formatReport, type ToolReport } from './doctor.js'; -import { keysHelp, runKeys, type KeyCommandOptions } from './keys.js'; -import type { CatalogModel, GenerateContext } from './types.js'; - -interface CliOptions extends KeyCommandOptions { - apiKey?: string; - profile: string; - model?: string; -} - -function rootUrl(url: string): string { - return url.trim().replace(/\/+$/, '').replace(/\/v1$/i, ''); -} - -function validateProfile(profile: string): string { - if ( - !/^[A-Za-z0-9][A-Za-z0-9._-]*$/.test(profile) - || profile === '.' - || profile === '..' - ) { - throw new Error( - '--profile must use only letters, numbers, dots, underscores, or hyphens', - ); - } - return profile; -} - -function parseTimeout(value: string): number { - const ms = Number(value); - // Rejected rather than clamped: a typo'd `--timeout 5s` parsing to NaN and - // silently becoming the default is the kind of quiet no-op this command is - // supposed to be immune to. - if (!Number.isFinite(ms) || ms <= 0) { - throw new Error(`--timeout must be a positive number of milliseconds, got '${value}'`); - } - return ms; -} - -export function parseArgs(argv: string[]): { command?: string; options: CliOptions } { - const options: CliOptions = { - url: process.env.FREELLMAPI_URL || 'http://localhost:3001', - apiKey: process.env.FREELLMAPI_API_KEY, - token: process.env.FREELLMAPI_DASHBOARD_TOKEN, - profile: 'default', - dryRun: false, - args: [], - }; - let command: string | undefined; - for (let index = 0; index < argv.length; index += 1) { - const arg = argv[index]; - if (!arg.startsWith('-') && !command) { - command = arg; - continue; - } - if (!arg.startsWith('-')) { - // Collected rather than rejected here so the parser stays generic; the - // dispatcher rejects extras for commands that take none, which keeps - // `setup-claude typo` an error instead of a silently ignored word. - options.args.push(arg); - continue; - } - if (arg === '--dry-run') { - options.dryRun = true; - continue; - } - const equals = arg.indexOf('='); - const flag = equals < 0 ? arg : arg.slice(0, equals); - const inline = equals < 0 ? undefined : arg.slice(equals + 1); - const value = inline ?? argv[index + 1]; - if ( - flag === '--url' || flag === '--api-key' || flag === '--profile' - || flag === '--model' || flag === '--timeout' - || flag === '--token' || flag === '--key' || flag === '--id' - ) { - if (inline === undefined) index += 1; - if (!value || (inline === undefined && value.startsWith('-'))) { - throw new Error(`${flag} requires a value`); - } - if (flag === '--url') options.url = value; - else if (flag === '--api-key') options.apiKey = value; - else if (flag === '--profile') options.profile = validateProfile(value); - else if (flag === '--timeout') options.timeoutMs = parseTimeout(value); - else if (flag === '--token') options.token = value; - else if (flag === '--key') options.key = value; - else if (flag === '--id') { - if (!/^[1-9]\d*$/.test(value) || !Number.isSafeInteger(Number(value))) { - throw new Error('--id must be a positive integer'); - } - options.keyId = Number(value); - } - else options.model = value; - continue; - } - if (arg !== '--help' && arg !== '-h') throw new Error(`Unknown option: ${arg}`); - } - return { command, options }; -} - -async function promptForKey(provider = false): Promise { - if (!process.stdin.isTTY) { - throw new Error( - provider - ? 'No provider key supplied. Pass --key or use an interactive terminal for hidden input.' - : 'No API key supplied. Pass --api-key or set FREELLMAPI_API_KEY.', - ); - } - // Imported lazily, not statically: `node:readline/promises` only exists on - // Node >=17.4, and a static import of it crashes the whole CLI at load time - // on older runtimes (#1283) — `npx freellmapi --help` died with - // ERR_UNKNOWN_BUILTIN_MODULE before main() or the engines check could say - // anything useful. Deferring it means only the interactive key prompt needs - // a modern Node; every other command degrades to the readable error below. - const readline = (await import('node:readline/promises')).default; - let muted = false; - const output = new Writable({ - write(chunk, _encoding, callback) { - if (!muted) process.stderr.write(chunk); - callback(); - }, - }); - const rl = readline.createInterface({ - input: process.stdin, - output, - terminal: true, - }); - const controller = new AbortController(); - const cancel = (): void => { controller.abort(); }; - rl.on('SIGINT', cancel); - rl.on('close', cancel); - try { - const answer = rl.question(provider ? 'Provider API key: ' : 'FreeLLMAPI unified API key: ', { - signal: controller.signal, - }); - muted = true; - const value = (await answer).trim(); - muted = false; - process.stderr.write('\n'); - if (!value) throw new Error('An API key is required'); - return value; - } catch (error) { - if (controller.signal.aborted) throw new Error('Key entry cancelled'); - throw error; - } finally { - muted = false; - rl.off('SIGINT', cancel); - rl.off('close', cancel); - rl.close(); - } -} - -async function catalog(url: string, apiKey: string, availableOnly = true): Promise { - let response: Response; - try { - response = await fetch(`${rootUrl(url)}/v1/models${availableOnly ? '?available=true' : ''}`, { - headers: { Authorization: `Bearer ${apiKey}` }, - signal: AbortSignal.timeout(10_000), - }); - } catch (error) { - const cause = error instanceof Error && error.cause instanceof Error - ? error.cause.message - : undefined; - const reason = cause ?? (error instanceof Error ? error.message : String(error)); - throw new Error( - `Could not reach the FreeLLMAPI gateway at ${rootUrl(url)} (${reason}). ` - + 'Check that the server is running, or point the CLI at it with --url or FREELLMAPI_URL.', - ); - } - if (!response.ok) { - const detail = (await response.text()).slice(0, 200); - throw new Error(`Catalog request failed (HTTP ${response.status}): ${detail}`); - } - const body = await response.json() as { data?: CatalogModel[] }; - const models = body.data ?? []; - if (!models.length) throw new Error('The live catalog returned no models'); - return models; -} - -export interface Catalogs { - available: CatalogModel[]; - full: CatalogModel[]; - /** Why the unfiltered fetch failed. Set means `full` is really the FILTERED - * roster, so "unknown model" and "out of quota" can no longer be told - * apart — the exact ambiguity this pair of fetches exists to remove. */ - degradedReason?: string; -} - -/** - * The available-only roster plus the unfiltered one. - * - * Only the unfiltered roster can distinguish "no such model" from "that model - * exists but is out of quota right now", and reporting the second as the first - * turns a rate limit into a spurious typo error. The unfiltered fetch is - * best-effort: an older gateway that ignores the parameter, or any failure, - * degrades to the filtered roster rather than blocking a launch — but it - * RECORDS that it degraded, so the degradation can be reported instead of - * silently reinstating the ambiguity. - */ -export async function catalogs(url: string, apiKey: string): Promise { - const available = await catalog(url, apiKey); - try { - return { available, full: await catalog(url, apiKey, false) }; - } catch (error) { - return { - available, - full: available, - degradedReason: error instanceof Error ? error.message : String(error), - }; - } -} - -/** - * Resolve a pinned `--model` ONCE, and say out loud anything that makes the - * verdict less than certain. - * - * One resolution feeding both the id that gets pinned and the warning the user - * reads, so the two can never disagree. Returns undefined when nothing was - * pinned — the launcher then picks from the filtered roster, where there is no - * ambiguity to lose and so nothing to warn about. - */ -export function resolvePinnedModel( - requested: string | undefined, - rosters: Catalogs, - warn: (message: string) => void = message => process.stderr.write(message), -): ResolvedModel | undefined { - if (!requested) return undefined; - - // Reported, never silent: degrading to the filtered roster is exactly the - // state in which an out-of-quota model gets called a typo, so the user has - // to be told which roster the verdict below came from. - if (rosters.degradedReason) { - warn( - `freellmapi: could not fetch the unfiltered model catalog (${rosters.degradedReason}). ` - + `Checking '${requested}' against the available-only roster instead — a model that ` - + 'exists but is out of quota may be reported as unknown.\n', - ); - } - - const resolved = resolveLaunchModel(requested, rosters.available, rosters.full); - // A pinned model that is registered but not servable right now is a launch we - // should still make — the router may recover it mid-session — but never one - // we should make silently. - if (resolved.unavailable) { - warn( - `freellmapi: '${resolved.id}' is registered but not currently available ` - + '(out of quota, cooling down, or its key is disabled). Launching anyway.\n', - ); - } - return resolved; -} - -function help(): string { - return [ - 'FreeLLMAPI coding-agent setup and provider key management', - '', - 'Usage:', - ' freellmapi [--url URL] [--api-key KEY] [--profile NAME] [--model ID] [--dry-run]', - '', - 'Commands:', - ...tools.map(tool => ` ${tool.command.padEnd(17)} ${tool.name}`), - ' doctor [toolâ€Ļ] Check whether a tool\'s requests actually reach this gateway', - ' (--timeout MS raises the probe wait on a slow link)', - ' launch Run Claude Code with credentials injected into the child environment', - ' launch-codex Run Codex CLI with provider overrides and injected credentials', - ' list List supported coding agents', - ' keys Add, list, remove, or test provider keys (keys --help)', - '', - 'Environment:', - ' FREELLMAPI_URL, FREELLMAPI_API_KEY, FREELLMAPI_DASHBOARD_TOKEN (keys only)', - ].join('\n'); -} - -async function setup(command: string, options: CliOptions): Promise { - const tool = getTool(command.replace(/^setup-/, '')); - if (!tool) throw new Error(`Unknown setup command '${command}'`); - const apiKey = options.apiKey ?? await promptForKey(); - const rosters = await catalogs(options.url, apiKey); - // --model was parsed but never reached the generators, so `setup-x --model y` - // silently wrote whatever primaryModel() preferred. Validate it against the - // unfiltered catalog (so an out-of-quota model is not reported as a typo) - // and thread it through. - const requestedModelId = resolvePinnedModel(options.model, rosters)?.id; - const context: GenerateContext = { - url: rootUrl(options.url), - apiKey, - profile: options.profile, - models: rosters.available, - homeDir: os.homedir(), - requestedModelId, - detectedClient: detectChatGptClient(), - }; - const generation = tool.generate(context); - - if (generation.files.length === 0) { - for (const note of generation.notes) process.stdout.write(`${note}\n`); - return; - } - - const results = applyGeneratedFiles(generation.files, options.dryRun); - for (const result of results) { - if (options.dryRun && result.changed) { - process.stdout.write(`${printDryRunDiff(result)}\n`); - } else if (result.changed) { - process.stdout.write(`Updated ${result.path}\n`); - if (result.backupPath) process.stdout.write(`Backup: ${result.backupPath}\n`); - } else { - process.stdout.write(`Unchanged ${result.path}\n`); - } - } - for (const note of generation.notes) process.stdout.write(`\n${note}\n`); -} - -// Runs an interactive child with inherited stdio. SIGINT is ignored in the -// wrapper (the foreground child receives it from the terminal and decides -// what to do), SIGTERM is forwarded, and the child's exit code is returned. -function runChild( - command: string, - args: string[], - env: NodeJS.ProcessEnv, -): Promise { - const child = spawn(command, args, { stdio: 'inherit', env }); - const ignoreSigint = (): void => {}; - const forwardSigterm = (): void => { child.kill('SIGTERM'); }; - process.on('SIGINT', ignoreSigint); - process.on('SIGTERM', forwardSigterm); - const cleanup = (): void => { - process.off('SIGINT', ignoreSigint); - process.off('SIGTERM', forwardSigterm); - }; - return new Promise((resolve, reject) => { - child.once('error', error => { - cleanup(); - reject(error); - }); - child.once('exit', (code, signal) => { - cleanup(); - if (code !== null) resolve(code); - else if (signal) resolve(128 + (os.constants.signals[signal] ?? 0)); - else resolve(1); - }); - }); -} - -// `--profile` selects a CLAUDE_CONFIG_DIR generated by setup-claude and is -// never a model id; `--model` picks the model explicitly. -export function claudeLaunchEnv( - options: CliOptions, - apiKey: string, - models: CatalogModel[], - baseEnv: NodeJS.ProcessEnv = process.env, - homeDir = os.homedir(), - fullCatalog: CatalogModel[] = models, -): NodeJS.ProcessEnv { - const resolved = resolveLaunchModel(options.model, models, fullCatalog); - const selected = resolved.id; - const env: NodeJS.ProcessEnv = { ...baseEnv }; - // Never leak the user's real Anthropic credentials to the gateway. - delete env.ANTHROPIC_API_KEY; - delete env.ANTHROPIC_AUTH_TOKEN; - if (options.profile !== 'default') { - const directory = path.join(homeDir, '.claude', 'profiles', options.profile); - if (!fs.existsSync(directory)) { - throw new Error( - `Claude profile '${options.profile}' has no configuration directory at ${directory}. ` - + `Create it with: freellmapi setup-claude --profile ${options.profile}`, - ); - } - env.CLAUDE_CONFIG_DIR = directory; - } - Object.assign(env, { - ANTHROPIC_BASE_URL: rootUrl(options.url), - ANTHROPIC_AUTH_TOKEN: apiKey, - ANTHROPIC_MODEL: selected, - ANTHROPIC_DEFAULT_OPUS_MODEL: selected, - ANTHROPIC_DEFAULT_SONNET_MODEL: selected, - ANTHROPIC_DEFAULT_HAIKU_MODEL: selected, - CLAUDE_CODE_ENABLE_GATEWAY_MODEL_DISCOVERY: '1', - }); - // Only pin the compaction window when the catalog actually published one. - // The old `?? 128_000` invented a number for every model with no stated - // window, and a wrong window is worse than none: too high and Claude Code - // compacts after the gateway has already rejected the request, too low and - // it compacts a conversation that still fits. Absent, Claude Code applies - // its own default. - if (resolved.contextWindow !== undefined) { - env.CLAUDE_CODE_AUTO_COMPACT_WINDOW = String(resolved.contextWindow); - } else { - delete env.CLAUDE_CODE_AUTO_COMPACT_WINDOW; - } - return env; -} - -async function launchClaude(options: CliOptions): Promise { - const apiKey = options.apiKey ?? await promptForKey(); - const rosters = await catalogs(options.url, apiKey); - // Resolved here for the warning; claudeLaunchEnv resolves again to build the - // environment. Both are pure calls over the same two rosters, so they cannot - // reach different verdicts — keeping claudeLaunchEnv a side-effect-free env - // builder is worth more than eliding the second call. - resolvePinnedModel(options.model, rosters); - const env = claudeLaunchEnv( - options, apiKey, rosters.available, process.env, os.homedir(), rosters.full, - ); - return runChild('claude', [], env); -} - -export function codexArgs(url: string, selected: string): string[] { - return [ - '-c', 'model_provider="freellmapi"', - '-c', `model=${JSON.stringify(selected)}`, - '-c', 'model_providers.freellmapi.name="FreeLLMAPI"', - '-c', `model_providers.freellmapi.base_url=${JSON.stringify(`${rootUrl(url)}/v1`)}`, - '-c', 'model_providers.freellmapi.wire_api="responses"', - '-c', 'model_providers.freellmapi.env_key="FREELLMAPI_API_KEY"', - '-c', 'model_providers.freellmapi.requires_openai_auth=false', - ]; -} - -async function launchCodex(options: CliOptions): Promise { - const apiKey = options.apiKey ?? await promptForKey(); - const rosters = await catalogs(options.url, apiKey); - const selected = ( - resolvePinnedModel(options.model, rosters) - ?? resolveLaunchModel(undefined, rosters.available, rosters.full) - ).id; - const env = { ...process.env, FREELLMAPI_API_KEY: apiKey }; - const args = codexArgs(options.url, selected); - return runChild('codex', args, env); -} - -async function runDoctor(options: CliOptions): Promise { - const requested = options.args.length ? options.args : DOCTOR_TOOLS; - const reports: ToolReport[] = []; - for (const tool of requested) { - reports.push(await diagnose(tool, { - expectedUrl: rootUrl(options.url), - timeoutMs: options.timeoutMs, - })); - } - for (const report of reports) process.stdout.write(`${formatReport(report)}\n`); - // Nonzero when anything is not routed, so this is usable as a precondition - // in a script rather than only readable by eye. - return exitCodeFor(reports); -} - -export async function main(argv = process.argv.slice(2)): Promise { - const { command, options } = parseArgs(argv); - if (!command || command === 'help' || argv.includes('--help') || argv.includes('-h')) { - process.stdout.write(`${command === 'keys' ? keysHelp() : help()}\n`); - return 0; - } - for (const arg of argv) { - const flag = arg.split('=', 1)[0]; - if (command !== 'keys' && ['--token', '--key', '--id'].includes(flag)) { - throw new Error(`${flag} is only supported by keys`); - } - if (command === 'keys' && ['--api-key', '--profile', '--model'].includes(flag)) { - throw new Error(`${flag} is not supported by keys; use keys --help for available options`); - } - } - if (command === 'keys') return runKeys(options, () => promptForKey(true)); - // `doctor` and `keys` take positional arguments. Every other - // one rejects them here, BEFORE dispatch — checking after the setup-* branch - // would let `setup-claude typo` run with the stray word silently ignored, - // where it used to be an error. - if (command !== 'doctor' && options.args.length) { - throw new Error(`Unknown option: ${options.args[0]}`); - } - if (command === 'list') { - for (const tool of tools) { - process.stdout.write(`${tool.id}\t${tool.protocol}\t${tool.baseUrlSupport}\n`); - } - return 0; - } - if (command.startsWith('setup-')) { - await setup(command, options); - return 0; - } - if (command === 'doctor') return runDoctor(options); - if (command === 'launch') return launchClaude(options); - if (command === 'launch-codex') return launchCodex(options); - throw new Error(`Unknown command '${command}'\n\n${help()}`); -} - -function isDirectExecution(): boolean { - if (!process.argv[1]) return false; - try { - return fs.realpathSync(process.argv[1]) === fs.realpathSync(fileURLToPath(import.meta.url)); - } catch { - return false; - } -} - -/** Minimum Node major this CLI actually runs on: `AbortSignal.timeout` - * (17.3), global `fetch` (18), `node:readline/promises` (17.4) — rounded to - * the package's engines floor of 20. npm treats `engines` as a warning by - * default (and npm 6, still common on Windows boxes hitting #1283, ignores it - * for `npx`), so without this check an unsupported runtime gets a stack - * trace instead of a sentence. Returns null when the runtime is fine. */ -export function unsupportedNodeVersion( - version: string | undefined = process.versions.node, - major: number | undefined = Number(version?.split('.')[0]), -): string | null { - if (!version || !Number.isFinite(major)) return null; // Non-Node runtimes: don't guess. - if (major >= 20) return null; - return `freellmapi requires Node.js 20 or newer (found ${version}). Install one from https://nodejs.org and re-run.`; -} - -if (isDirectExecution()) { - const unsupported = unsupportedNodeVersion(); - if (unsupported) { - process.stderr.write(`freellmapi: ${unsupported}\n`); - process.exitCode = 1; - } else { - main().then( - code => { process.exitCode = code; }, - error => { - process.stderr.write(`freellmapi: ${error instanceof Error ? error.message : String(error)}\n`); - process.exitCode = 1; - }, - ); - } -} diff --git a/cli/src/keys.test.ts b/cli/src/keys.test.ts deleted file mode 100644 index 3c4b23aa1f0d735d96fadd8c5876105582e60ba8..0000000000000000000000000000000000000000 --- a/cli/src/keys.test.ts +++ /dev/null @@ -1,210 +0,0 @@ -import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; -import { main, parseArgs } from './index.js'; - -const token = 'dashboard-session=='; -const secret = 'provider-secret=='; -const key = { id: 7, platform: 'groq', enabled: true, status: 'healthy', maskedKey: secret, lastHealthError: null as string | null }; -const calls: Array<{ path: string; init: RequestInit }> = []; -let keys: Array }>; -let output: string; - -beforeEach(() => { - keys = [{ ...key }]; - output = ''; - calls.length = 0; - vi.stubEnv('FREELLMAPI_DASHBOARD_TOKEN', token); - vi.spyOn(process.stdout, 'write').mockImplementation(chunk => { output += String(chunk); return true; }); - vi.stubGlobal('fetch', vi.fn(async (input: string, init: RequestInit = {}) => { - const path = new URL(input).pathname; - calls.push({ path, init }); - if (path === '/api/keys/providers') return Response.json({ providers: [{ platform: 'groq' }, { platform: 'kilo', keyless: true }] }); - if (path === '/api/keys' && init.method === 'POST') return Response.json({ id: 7, platform: 'groq', maskedKey: secret }, { status: 201 }); - if (path === '/api/keys') return Response.json(keys); - if (path === '/api/models') return Response.json([ - { id: 1, platform: 'groq', modelId: 'model-a', enabled: true }, - { id: 2, platform: 'groq', modelId: 'model-b', enabled: true }, - { id: 3, platform: 'groq', modelId: 'disabled', enabled: false }, - { id: 4, platform: 'other', modelId: 'other', enabled: true }, - ]); - if (path.startsWith('/api/health/check/')) return Response.json({ keyId: Number(path.split('/').at(-1)), status: 'healthy' }); - if (path.startsWith('/api/keys/') && init.method === 'DELETE') return Response.json({ success: true }); - throw new Error(`Unexpected request: ${path}`); - })); -}); - -afterEach(() => { - vi.restoreAllMocks(); - vi.unstubAllGlobals(); - vi.unstubAllEnvs(); -}); - -describe('provider key commands', () => { - it('preserves equals signs in credentials and lets --token override the environment', async () => { - expect(parseArgs(['keys', 'add', 'groq', `--token=${token}`, `--key=${secret}`]).options) - .toMatchObject({ token, key: secret }); - await main(['--url', 'http://localhost:3100/v1/', 'keys', 'add', 'groq', '--token', 'override', `--key=${secret}`]); - expect(calls.map(c => c.path)).toEqual(['/api/keys/providers', '/api/keys', '/api/health/check/7', '/api/keys']); - expect(calls[1].init.body).toBe(JSON.stringify({ platform: 'groq', key: secret })); - for (const call of calls) expect(new Headers(call.init.headers).get('Authorization')).toBe('Bearer override'); - expect(output).toContain('Saved groq key 7'); - expect(output).not.toContain(secret); - expect(output).not.toContain(token); - }); - - it('lists metadata and enabled chat model counts, without printing even masked keys', async () => { - keys.push({ ...key, id: 8, enabled: false, modelScope: ['model-b'] }); - expect(await main(['keys', 'list'])).toBe(0); - expect(output).toContain('7\tgroq\t2\tenabled\thealthy'); - expect(output).toContain('8\tgroq\t1\tdisabled\thealthy'); - expect(output).not.toContain(secret); - expect(output).not.toContain(token); - }); - - it('requires an id to remove one of several keys and verifies its platform', async () => { - keys.push({ ...key, id: 8 }); - await expect(main(['keys', 'remove', 'groq'])).rejects.toThrow('--id'); - await expect(main(['keys', 'remove', 'other', '--id', '7'])).rejects.toThrow('No matching key'); - expect(calls.every(c => c.init.method !== 'DELETE')).toBe(true); - expect(await main(['keys', 'remove', 'groq', '--id', '8'])).toBe(0); - expect(calls.at(-1)).toMatchObject({ path: '/api/keys/8', init: { method: 'DELETE' } }); - }); - - it('removes a single matching key without touching other providers', async () => { - keys.push({ ...key, id: 8, platform: 'other' }); - await main(['keys', 'remove', 'groq']); - expect(calls.filter(c => c.init.method === 'DELETE').map(c => c.path)).toEqual(['/api/keys/7']); - }); - - it('does not equate HTTP 200 with successful validation', async () => { - keys[0].status = 'invalid'; - expect(await main(['keys', 'test', 'groq'])).toBe(1); - expect(output).toContain('invalid'); - }); - - it('reports inconclusive validation when the server preserves a previously healthy status', async () => { - keys[0] = { ...key, lastHealthError: secret }; - expect(await main(['keys', 'test', 'groq'])).toBe(1); - expect(output).toContain('inconclusive'); - expect(output).not.toContain(secret); - }); - - it('checks every matching key and combines unsuccessful results', async () => { - keys.push({ ...key, id: 8, status: 'invalid' }, { ...key, id: 9, platform: 'other' }); - expect(await main(['keys', 'test', 'groq'])).toBe(1); - expect(calls.filter(c => c.path.startsWith('/api/health')).map(c => c.path)) - .toEqual(['/api/health/check/7', '/api/health/check/8']); - }); - - it('keeps a newly saved key when validation fails and returns a failure exit code', async () => { - keys[0].status = 'invalid'; - expect(await main(['keys', 'add', 'groq', '--key', secret])).toBe(1); - expect(output).toContain('Saved groq key 7'); - expect(calls.every(c => c.init.method !== 'DELETE')).toBe(true); - }); - - it('does not prompt for a keyless provider', async () => { - keys[0].platform = 'kilo'; - expect(await main(['keys', 'add', 'kilo'])).toBe(0); - expect(JSON.parse(calls[1].init.body as string)).toEqual({ platform: 'kilo', key: '' }); - }); - - it('rejects a missing provider key in a non-interactive terminal without adding one', async () => { - const descriptor = Object.getOwnPropertyDescriptor(process.stdin, 'isTTY'); - Object.defineProperty(process.stdin, 'isTTY', { configurable: true, value: false }); - try { - await expect(main(['keys', 'add', 'groq'])).rejects.toThrow('Pass --key'); - expect(calls.some(c => c.init.method === 'POST')).toBe(false); - } finally { - if (descriptor) Object.defineProperty(process.stdin, 'isTTY', descriptor); - else Reflect.deleteProperty(process.stdin, 'isTTY'); - } - }); - - it('does not treat a failed health probe as success when the stored status is healthy', async () => { - const request = vi.mocked(fetch).getMockImplementation()!; - vi.mocked(fetch).mockImplementation((input, init) => String(input).includes('/api/health/check/') - ? Promise.resolve(Response.json({ keyId: 7, status: 'error' })) - : request(input, init)); - expect(await main(['keys', 'test', 'groq'])).toBe(1); - expect(output).toContain('7\tgroq\terror'); - }); - - it('rejects malformed key IDs from the gateway before attempting deletion', async () => { - vi.mocked(fetch).mockResolvedValue(Response.json([{ ...key, id: '../providers' }])); - await expect(main(['keys', 'remove', 'groq'])).rejects.toThrow('invalid key list'); - expect(fetch).toHaveBeenCalledTimes(1); - }); - - it('counts only enabled chat models on the same custom endpoint', async () => { - keys = [{ ...key, platform: 'custom', models: [{ id: 1, kind: 'chat' }, { id: 4, kind: 'embedding' }] }]; - vi.mocked(fetch).mockImplementation(async (input) => new URL(String(input)).pathname === '/api/keys' - ? Response.json(keys) - : Response.json([ - { id: 1, platform: 'custom', modelId: 'first', enabled: true, keyId: 8 }, - { id: 2, platform: 'custom', modelId: 'other-endpoint', enabled: true, keyId: 9 }, - { id: 4, platform: 'custom', modelId: 'id-shared-with-embedding', enabled: true, keyId: 9 }, - ])); - await main(['keys', 'list']); - expect(output).toContain('7\tcustom\t1\tenabled\thealthy'); - }); - - it('does not write when the provider identifier is unknown', async () => { - await expect(main(['keys', 'add', 'missing', '--key', secret])).rejects.toThrow('Unknown provider'); - expect(calls.some(c => c.init.method === 'POST')).toBe(false); - }); - - it('requires a dashboard token rather than using the unified API key', async () => { - vi.stubEnv('FREELLMAPI_DASHBOARD_TOKEN', ''); - vi.stubEnv('FREELLMAPI_API_KEY', 'unified-secret'); - await expect(main(['keys', 'list'])).rejects.toThrow('FREELLMAPI_DASHBOARD_TOKEN'); - expect(calls).toEqual([]); - }); - - it.each([ - ['keys', 'remove', 'groq', '--id', '7abc'], - ['keys', 'remove', 'groq', '--id', '0'], - ['keys', 'list', 'extra'], - ['keys', 'remove'], - ['keys', 'remove', 'groq', '--dry-run'], - ['keys', 'list', '--key', secret], - ['keys', 'add', 'groq', '--api-key', secret], - ['keys', 'remove', 'groq', '--model', 'model-a'], - ['keys', 'remove', 'groq', '--profile', 'work'], - ['setup-claude', '--token', token], - ['setup-claude', '--key', secret], - ['list', '--id', '7'], - ])('rejects invalid or inapplicable arguments before making requests: %j', async (...args) => { - await expect(main(args)).rejects.toThrow(); - expect(calls).toEqual([]); - }); - - it('does not print server error bodies containing credentials', async () => { - vi.mocked(fetch).mockResolvedValue(Response.json({ error: { message: `${secret} ${token}` } }, { status: 401 })); - await expect(main(['keys', 'list'])).rejects.toThrow('Dashboard authentication failed'); - expect(output).toBe(''); - }); - - it('omits secrets from non-authentication failures and transport errors', async () => { - vi.mocked(fetch).mockResolvedValueOnce(Response.json({ error: secret }, { status: 500 })); - await expect(main(['keys', 'list'])).rejects.toThrow(/^Dashboard request failed \(HTTP 500\)$/); - vi.mocked(fetch).mockRejectedValueOnce(new Error(token)); - await expect(main(['keys', 'list'])).rejects.toThrow(/^Could not complete the dashboard request\./); - expect(output).toBe(''); - }); - - it('does not send dashboard credentials to redirects and applies the timeout', async () => { - const timeout = vi.spyOn(AbortSignal, 'timeout'); - await main(['keys', 'test', 'groq', '--id', '7', '--timeout', '5000']); - expect(calls[0].init).toMatchObject({ redirect: 'error', signal: expect.any(AbortSignal) }); - expect(timeout).toHaveBeenCalledWith(5000); - await expect(main(['keys', 'list', '--url', 'http://user:password@localhost:3000'])).rejects.toThrow('without credentials'); - }); - - it('shows key-specific help without requiring credentials or making requests', async () => { - vi.stubEnv('FREELLMAPI_DASHBOARD_TOKEN', ''); - expect(await main(['keys', '--help'])).toBe(0); - expect(output).toContain('keys add '); - expect(output).toContain('FREELLMAPI_DASHBOARD_TOKEN'); - expect(calls).toEqual([]); - }); -}); diff --git a/cli/src/keys.ts b/cli/src/keys.ts deleted file mode 100644 index b02109f58f0136cff6317e7480cfe736fe7bdda8..0000000000000000000000000000000000000000 --- a/cli/src/keys.ts +++ /dev/null @@ -1,184 +0,0 @@ -export interface KeyCommandOptions { - url: string; - args: string[]; - token?: string; - key?: string; - keyId?: number; - timeoutMs?: number; - dryRun: boolean; -} - -interface KeyRow { - id: number; - platform: string; - enabled: boolean; - status: string; - lastHealthError?: string | null; - modelScope?: string[] | null; - models?: Array<{ id: number; kind: string }>; -} - -interface ModelRow { - id: number; - platform: string; - modelId: string; - enabled: boolean; - keyId?: number | null; -} - -export function keysHelp(): string { - return [ - 'Provider key management (dashboard authentication):', - ' freellmapi keys add [--key KEY]', - ' freellmapi keys list', - ' freellmapi keys remove [--id ID]', - ' freellmapi keys test [--id ID]', - '', - 'Options: --url URL, --token TOKEN, --timeout MS (default 30000)', - 'Use FREELLMAPI_DASHBOARD_TOKEN or --token for the dashboard session token.', - 'The unified --api-key / FREELLMAPI_API_KEY is not dashboard authentication.', - 'Omit --key for hidden terminal input. Keyless providers do not prompt.', - 'remove requires --id when a platform has multiple keys; test checks all matches.', - 'list shows enabled chat model counts, respecting each key\'s model scope.', - 'add saves the key, then checks it; failed validation leaves it saved and exits 1.', - ].join('\n'); -} - -function gatewayRoot(value: string): string { - let url: URL; - try { url = new URL(value.trim()); } catch { throw new Error('--url must be an HTTP(S) gateway URL'); } - if (!['http:', 'https:'].includes(url.protocol) || url.username || url.password || url.search || url.hash) { - throw new Error('--url must be an HTTP(S) URL without credentials, a query, or a fragment'); - } - return url.href.replace(/\/+$/, '').replace(/\/v1$/i, ''); -} - -function modelCount(key: KeyRow, models: ModelRow[]): number { - const customIds = new Set(key.models?.filter(m => m.kind === 'chat').map(m => m.id)); - return models.filter(model => model.enabled && model.platform === key.platform - && (!key.modelScope?.length || key.modelScope.includes(model.modelId)) - && (key.platform === 'custom' ? customIds.has(model.id) : !model.keyId || model.keyId === key.id)).length; -} - -const statuses = new Set(['healthy', 'invalid', 'unknown', 'rate_limited', 'error']); -function statusOf(status: string): string { - return statuses.has(status) ? status : 'unknown'; -} - -export async function runKeys( - options: KeyCommandOptions, - prompt: () => Promise, -): Promise { - const [action, rawPlatform, ...extra] = options.args; - if (!action || !['add', 'list', 'remove', 'test'].includes(action)) { - throw new Error(`Expected keys add, list, remove, or test.\n\n${keysHelp()}`); - } - if (extra.length || (action === 'list' ? rawPlatform !== undefined : !rawPlatform)) { - throw new Error(`Invalid arguments for keys ${action}.\n\n${keysHelp()}`); - } - if (options.dryRun) throw new Error('--dry-run is not supported by keys; no request was made'); - if (options.key !== undefined && action !== 'add') throw new Error('--key is only supported by keys add'); - if (options.keyId !== undefined && action !== 'remove' && action !== 'test') { - throw new Error('--id is only supported by keys remove and keys test'); - } - const platform = rawPlatform?.toLowerCase(); - if (platform && !/^[a-z][a-z0-9_-]*$/.test(platform)) throw new Error('Invalid provider platform identifier'); - const token = options.token?.trim(); - if (!token) throw new Error('Set FREELLMAPI_DASHBOARD_TOKEN or pass --token with a dashboard session token'); - const root = gatewayRoot(options.url); - const timeout = options.timeoutMs ?? 30_000; - if (!Number.isSafeInteger(timeout) || timeout <= 0 || timeout > 2_147_483_647) { - throw new Error('--timeout must be a positive integer of at most 2147483647 milliseconds'); - } - - async function request(path: string, method = 'GET', body?: unknown): Promise { - let response: Response; - try { - response = await fetch(`${root}${path}`, { - method, - headers: { Authorization: `Bearer ${token}`, ...(body === undefined ? {} : { 'Content-Type': 'application/json' }) }, - body: body === undefined ? undefined : JSON.stringify(body), - signal: AbortSignal.timeout(timeout), - redirect: 'error', - cache: 'no-store', - }); - } catch { - throw new Error('Could not complete the dashboard request. Check --url, connectivity, and --timeout.'); - } - // Provider error bodies may contain credentials. Only report the HTTP - // status, never echo arbitrary response bodies or transport errors. - if (response.status === 401 || response.status === 403) { - throw new Error('Dashboard authentication failed. Supply a current dashboard session token with --token or FREELLMAPI_DASHBOARD_TOKEN.'); - } - if (!response.ok) throw new Error(`Dashboard request failed (HTTP ${response.status})`); - if (response.status === 204) return undefined as T; - try { return await response.json() as T; } catch { throw new Error('The gateway returned an invalid JSON response'); } - } - - async function listKeys(): Promise { - const rows = await request('/api/keys'); - if (!Array.isArray(rows) || rows.some(row => !row || !Number.isSafeInteger(row.id) - || row.id <= 0 || typeof row.platform !== 'string')) { - throw new Error('The gateway returned an invalid key list'); - } - return rows; - } - - async function check(id: number, provider: string): Promise { - const result = await request<{ keyId: number; status: string }>(`/api/health/check/${id}`, 'POST'); - if (!result || result.keyId !== id) throw new Error('The gateway returned an invalid health check result'); - // A transport failure preserves the previous status on the server, so - // HTTP 200 / status=healthy alone cannot prove this check succeeded. - const current = (await listKeys()).find(row => row.id === id); - if (!current) throw new Error('The key no longer exists'); - const status = statusOf(result.status === 'healthy' ? current.status : result.status); - const inconclusive = !!current.lastHealthError && status !== 'invalid'; - process.stdout.write(`${id}\t${provider}\t${inconclusive ? `inconclusive (stored status: ${statusOf(current.status)})` : status}\n`); - return status === 'healthy' && !inconclusive ? 0 : 1; - } - - if (action === 'add') { - const { providers } = await request<{ providers: Array<{ platform: string; keyless?: boolean }> }>('/api/keys/providers'); - const provider = providers.find(p => p.platform === platform); - if (!provider) { - throw new Error(platform === 'custom' - ? 'Custom endpoints require URL and model registration; add them in the dashboard.' - : 'Unknown provider platform. Use the provider identifier shown in the dashboard.'); - } - const value = (options.key ?? (provider.keyless ? '' : await prompt())).trim(); - if (!value && !provider.keyless) throw new Error('A provider API key is required'); - const saved = await request<{ id: number }>('/api/keys', 'POST', { platform, key: value }); - if (!Number.isSafeInteger(saved.id) || saved.id <= 0) throw new Error('The gateway did not return a valid saved key ID'); - process.stdout.write(`Saved ${platform} key ${saved.id}. Checking it now; the key remains saved if validation fails.\n`); - return check(saved.id, platform!); - } - - const rows = await listKeys(); - if (action === 'list') { - if (!rows.length) { - process.stdout.write('No provider keys configured.\n'); - return 0; - } - const models = await request('/api/models'); - if (!Array.isArray(models)) throw new Error('The gateway returned an invalid model list'); - process.stdout.write('ID\tPLATFORM\tMODELS\tENABLED\tSTATUS\n'); - for (const row of rows) { - const provider = /^[a-z][a-z0-9_-]*$/.test(row.platform) ? row.platform : 'unknown'; - process.stdout.write(`${row.id}\t${provider}\t${modelCount(row, models)}\t${row.enabled ? 'enabled' : 'disabled'}\t${statusOf(row.status)}\n`); - } - return 0; - } - - const selected = rows.filter(row => row.platform === platform && (options.keyId === undefined || row.id === options.keyId)); - if (!selected.length) throw new Error('No matching key found for that platform and ID'); - if (action === 'remove') { - if (selected.length > 1) throw new Error('Multiple keys match. Run keys list and specify one with --id.'); - await request(`/api/keys/${selected[0].id}`, 'DELETE'); - process.stdout.write(`Removed ${platform} key ${selected[0].id}.\n`); - return 0; - } - - let exitCode = 0; - for (const row of selected) exitCode = Math.max(exitCode, await check(row.id, platform!)); - return exitCode; -} diff --git a/cli/src/models.test.ts b/cli/src/models.test.ts deleted file mode 100644 index 409f9fbda50e990f7bbcdf9ab87c82fa14f558f5..0000000000000000000000000000000000000000 --- a/cli/src/models.test.ts +++ /dev/null @@ -1,81 +0,0 @@ -import { describe, expect, it } from 'vitest'; -import { UnknownModelError, contextWindowOf, defaultModelId, resolveLaunchModel } from './models.js'; -import type { CatalogModel } from './types.js'; - -// The distinction this module exists for: the CLI's catalog() fetches -// `?available=true`, so absence from that roster means EITHER "no such model" -// OR "exists, out of quota right now". Only the unfiltered roster separates -// them, and conflating them reports a healthy pinned model as a typo the -// moment its provider rate-limits. - -const AVAILABLE: CatalogModel[] = [ - { id: 'auto' }, - { id: 'fast-coder', available: true, context_window: 131072 }, -]; - -const FULL: CatalogModel[] = [ - ...AVAILABLE, - { id: 'benched-model', available: false, context_window: 32768 }, - { id: 'no-window-model', available: true }, -]; - -describe('resolveLaunchModel', () => { - it('accepts a model that is registered but not currently servable', () => { - const resolved = resolveLaunchModel('benched-model', AVAILABLE, FULL); - expect(resolved.id).toBe('benched-model'); - expect(resolved.unavailable).toBe(true); - expect(resolved.contextWindow).toBe(32768); - }); - - it('rejects a model that is in neither roster', () => { - expect(() => resolveLaunchModel('no-such-model', AVAILABLE, FULL)) - .toThrow(UnknownModelError); - }); - - it('suggests near-matches on an unknown id rather than only refusing', () => { - expect(() => resolveLaunchModel('coder', AVAILABLE, FULL)) - .toThrow(/fast-coder/); - }); - - it('reports an available model as available', () => { - expect(resolveLaunchModel('fast-coder', AVAILABLE, FULL)) - .toMatchObject({ id: 'fast-coder', unavailable: false, contextWindow: 131072 }); - }); - - it('leaves the context window undefined when the catalog publishes none', () => { - // Not 128000. An unpublished window is a different fact from a known one, - // and inventing a number is how the launcher pinned a wrong compaction - // threshold for every model whose provider states no window. - expect(resolveLaunchModel('no-window-model', AVAILABLE, FULL).contextWindow) - .toBeUndefined(); - }); - - it('serves `auto` even though the router never lists it as a catalog row', () => { - expect(resolveLaunchModel('auto', [], [])).toMatchObject({ id: 'auto', unavailable: false }); - }); - - it('falls back to the filtered roster when the unfiltered one is unavailable', () => { - // Degraded mode: an older gateway ignoring ?available=true, or a failed - // second fetch. A known-good id must still resolve. - expect(resolveLaunchModel('fast-coder', AVAILABLE).id).toBe('fast-coder'); - }); -}); - -describe('defaultModelId', () => { - it('prefers a concrete servable model over auto, so the session names its model', () => { - expect(defaultModelId(AVAILABLE)).toBe('fast-coder'); - }); - - it('falls back to auto when nothing concrete is servable', () => { - expect(defaultModelId([{ id: 'auto' }, { id: 'x', available: false }])).toBe('auto'); - }); -}); - -describe('contextWindowOf', () => { - it('accepts either catalog spelling and undefined for neither', () => { - expect(contextWindowOf({ id: 'a', context_window: 1000 })).toBe(1000); - expect(contextWindowOf({ id: 'b', context_length: 2000 })).toBe(2000); - expect(contextWindowOf({ id: 'c' })).toBeUndefined(); - expect(contextWindowOf({ id: 'd', context_window: null })).toBeUndefined(); - }); -}); diff --git a/cli/src/models.ts b/cli/src/models.ts deleted file mode 100644 index cbe8287fa09f81748452a17f7dcb4d8e7aa7487d..0000000000000000000000000000000000000000 --- a/cli/src/models.ts +++ /dev/null @@ -1,81 +0,0 @@ -// Resolving a model id against the catalog, for the launchers. -// -// The subtlety this module exists for: `catalog()` fetches `/v1/models` with -// `?available=true`, so a model missing from that response has TWO possible -// meanings — it does not exist, or it exists and is merely out of quota right -// now. Treating absence as "unknown model" reports a healthy pinned model as a -// typo the moment its provider hits a rate limit. Only the UNFILTERED roster -// can tell those apart, so `--model` is validated against that. - -import type { CatalogModel } from './types.js'; - -export interface ResolvedModel { - id: string; - /** Context window in tokens, or undefined when the catalog does not publish - * one. Undefined is a fact — an unpublished window is not the same as a - * known 128k, and callers must not invent one. */ - contextWindow?: number; - /** The catalog lists this model but it is not currently servable. */ - unavailable: boolean; -} - -export class UnknownModelError extends Error {} - -export function contextWindowOf(model: CatalogModel): number | undefined { - return model.context_window ?? model.context_length ?? undefined; -} - -/** - * The launcher's default when the caller did not pin one: the first servable - * concrete model, falling back to `auto`. - * - * Deliberately NOT the same default as the setup generators, which prefer - * `auto`. A generated config file is long-lived and should let the router - * choose per request; a launch pins one id into ANTHROPIC_MODEL for the whole - * session, and naming a concrete model there is what makes the session's model - * visible and reproducible. Unifying the two silently changes what - * `freellmapi launch` runs. - */ -export function defaultModelId(models: CatalogModel[]): string { - return models.find(model => model.id !== 'auto' && model.available !== false)?.id ?? 'auto'; -} - -/** - * Resolve the model a launcher should pin. - * - * `available` is the filtered roster the CLI already fetches; `full` is the - * unfiltered one. Pass the same array twice only when the unfiltered roster - * could not be fetched — the caller then loses the ability to distinguish an - * unknown id from an unavailable one, which is exactly the ambiguity this - * function exists to remove. - */ -export function resolveLaunchModel( - requested: string | undefined, - available: CatalogModel[], - full: CatalogModel[] = available, -): ResolvedModel { - const id = requested ?? defaultModelId(available); - const known = full.find(model => model.id === id) ?? available.find(model => model.id === id); - - if (!known) { - // `auto` is served by the router itself and need not appear in any roster. - if (id === 'auto') return { id, unavailable: false }; - const suggestions = full - .map(model => model.id) - .filter(candidate => candidate.includes(id) || id.includes(candidate)) - .slice(0, 5); - throw new UnknownModelError( - `No model '${id}' in this gateway's catalog.` - + (suggestions.length ? ` Did you mean: ${suggestions.join(', ')}?` : '') - + ' Run `freellmapi list-models` to see what is registered.', - ); - } - - return { - id, - contextWindow: contextWindowOf(known), - // Absent from the filtered roster but present in the full one = registered - // but not servable right now (quota, cooldown, a disabled key). - unavailable: known.available === false || !available.some(model => model.id === id), - }; -} diff --git a/cli/src/tools.test.ts b/cli/src/tools.test.ts deleted file mode 100644 index 7024978ef7f2cac4d70eabf8448b923fadd56b1b..0000000000000000000000000000000000000000 --- a/cli/src/tools.test.ts +++ /dev/null @@ -1,709 +0,0 @@ -import fs from 'node:fs'; -import path from 'node:path'; -import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; -import { tools } from './tools.js'; -import type { GenerateContext } from './types.js'; - -// Pinning ctx.detectedClient is not enough: tools.ts falls back to a live -// detectChatGptClient() when it is undefined, so a runner with `codex` on -// PATH (or ChatGPT.app installed) would still shift the golden notes. Mock -// the module outright — undefined keeps the snapshots host-independent. -vi.mock('./detect.js', () => ({ - detectChatGptClient: () => undefined, - clientNotes: () => [ - 'No Codex CLI detected on this machine.', - 'Install the CLI with: npm install -g @openai/codex', - ], -})); - -// CI runners export XDG_CONFIG_HOME (and could export MIMOCODE_HOME / DSH_HOME), -// which the XDG-aware generators honour over ctx.homeDir. Pin them so golden -// output is stable everywhere. -beforeEach(() => { - vi.stubEnv('XDG_CONFIG_HOME', ''); - vi.stubEnv('MIMOCODE_HOME', ''); - vi.stubEnv('DSH_HOME', ''); - vi.stubEnv('OPENCLAW_HOME', ''); - vi.stubEnv('OPENCLAW_STATE_DIR', ''); - vi.stubEnv('OPENCLAW_CONFIG_PATH', ''); - vi.stubEnv('HERMES_HOME', ''); - vi.stubEnv('PI_CODING_AGENT_DIR', ''); - vi.stubEnv('REASONIX_HOME', ''); - // Reasonix keeps its home under %APPDATA% on Windows instead of ~/.reasonix; - // pin it so asPosix can map that home back to the POSIX golden (#1392). - if (process.platform === 'win32') vi.stubEnv('APPDATA', WIN_APPDATA); -}); -afterEach(() => { - vi.unstubAllEnvs(); -}); - -// Golden snapshots must not depend on the host: pin the client detection the -// same way the XDG env vars above are pinned (a machine with `codex` on PATH -// or ChatGPT.app installed would otherwise produce different notes than CI). -const context: GenerateContext = { - url: 'http://localhost:3000', - apiKey: 'freellmapi-test-key', - profile: 'default', - homeDir: '/home/tester', - detectedClient: undefined, - models: [ - { - id: 'fast-coder', - name: 'Fast Coder', - available: true, - context_window: 131072, - }, - { - id: 'reasoning-model', - name: 'Reasoning Model', - available: true, - context_window: 262144, - }, - ], -}; - -// Generated file paths come from the host's path.join, so on Windows the -// separator is '\\' while the golden strings here are POSIX (#1392). Normalize -// before comparing, and map the pinned Windows Reasonix home to ~/.reasonix. -const WIN_APPDATA = 'C:\\Users\\tester\\AppData\\Roaming'; -function asPosix(p: string): string { - if (process.platform !== 'win32') return p; - return p.replaceAll('\\', '/').replace('C:/Users/tester/AppData/Roaming/reasonix', '/home/tester/.reasonix'); -} - -// Snapshots embed the generated file paths verbatim; normalize them so the -// golden files stay POSIX on every host (#1392). -interface GeneratedFile { - path: string; - [key: string]: unknown; -} -function normalizeGenerationPaths(generation: T): T { - return { - ...generation, - files: generation.files.map(f => ({ ...f, path: asPosix(f.path) })), - // Generators also interpolate host-joined paths into their note text — Roo's - // `autoImportSettingsPath` instructions quote the import file by full path — - // so the notes are golden input as well (#1392). - ...(generation.notes ? { notes: generation.notes.map(asPosix) } : {}), - }; -} - -describe('tool generators', () => { - it('defaults to auto, never fusion, when the live catalog lists virtual models first', () => { - const liveContext: GenerateContext = { - ...context, - models: [ - { id: 'auto', name: 'Auto', available: true, context_window: 200_000 }, - { id: 'fusion', name: 'Fusion', available: true, context_window: 2_000_000 }, - ...context.models, - ], - }; - const claude = tools.find(tool => tool.id === 'claude')!.generate(liveContext); - const settings = claude.files[0].value as { env: Record }; - expect(settings.env.ANTHROPIC_MODEL).toBe('auto'); - const codex = tools.find(tool => tool.id === 'codex')!.generate(liveContext); - expect(codex.files[0].content).toContain('model = "auto"'); - expect(codex.files[0].content).not.toContain('"fusion"'); - }); - - for (const tool of tools) { - it(`${tool.command} has stable golden output`, () => { - expect(normalizeGenerationPaths(tool.generate(context))).toMatchSnapshot(); - }); - } - - it('honours an explicit --model instead of the default-model heuristic', () => { - // `--model` was parsed into CliOptions and then dropped: it never reached - // GenerateContext, so `setup-claude --model X` wrote whatever - // primaryModel() preferred and said nothing about ignoring the flag. - const pinned = { ...context, requestedModelId: 'reasoning-model' }; - const claude = tools.find(tool => tool.id === 'claude')!.generate(pinned); - expect(JSON.stringify(claude.files)).toContain('reasoning-model'); - }); - - it('pins a requested model that the available-roster does not carry', () => { - // Validated against the UNFILTERED catalog upstream, so an id missing from - // ctx.models here is registered-but-out-of-quota, not a typo. Writing a - // different model into the user's config would be the wrong repair. - const pinned = { ...context, requestedModelId: 'benched-model' }; - const claude = tools.find(tool => tool.id === 'claude')!.generate(pinned); - expect(JSON.stringify(claude.files)).toContain('benched-model'); - }); - - it('setup-codex with a named profile has stable golden output', () => { - const generation = tools.find(tool => tool.id === 'codex')! - .generate({ ...context, profile: 'work' }); - expect(normalizeGenerationPaths(generation)).toMatchSnapshot(); - }); - - it('writes named Codex profiles as [profiles.NAME] inside config.toml', () => { - const generation = tools.find(tool => tool.id === 'codex')! - .generate({ ...context, profile: 'work' }); - expect(generation.files).toHaveLength(1); - const file = generation.files[0]; - // Codex only reads ~/.codex/config.toml; per-profile files are ignored. - expect(asPosix(file.path)).toBe('/home/tester/.codex/config.toml'); - expect(file.content).toContain('[profiles.work]'); - expect(file.content).toContain('model = "fast-coder"'); - expect(file.content).toContain('model_provider = "freellmapi"'); - expect(file.content).toContain('[model_providers.freellmapi]'); - // A named profile must not hijack the root/default model selection. - expect(file.content!.split('\n').findIndex(line => line.startsWith('model ='))) - .toBeGreaterThan(file.content!.split('\n').indexOf('[profiles.work]')); - expect(generation.notes.join('\n')).toContain('codex --profile work'); - }); - - it('generates Cline current provider settings with repeatable selection', () => { - const file = tools.find(tool => tool.id === 'cline')!.generate(context).files[0]; - const state = file.value as any; - expect(asPosix(file.path)).toBe('/home/tester/.cline/data/settings/providers.json'); - expect(state).toMatchObject({ - version: 1, - lastUsedProvider: 'openai-compatible', - }); - expect(state.providers['openai-compatible']).toMatchObject({ - settings: { - provider: 'openai-compatible', - protocol: 'openai-chat', - client: 'openai-compatible', - model: 'fast-coder', - baseUrl: 'http://localhost:3000/v1', - apiKey: 'freellmapi-test-key', - contextWindow: 131072, - capabilities: ['streaming', 'tools'], - }, - tokenSource: 'manual', - }); - }); - - it('generates a complete Continue v1 config with Agent tool support', () => { - const config = tools.find(tool => tool.id === 'continue')! - .generate(context).files[0].content!; - expect(config).toContain('name: FreeLLMAPI'); - expect(config).toContain('version: 1.0.0'); - expect(config).toContain('schema: v1'); - expect(config).toContain(' - tool_use'); - expect(config).toContain(' apiKey: ${{ secrets.FREELLMAPI_API_KEY }}'); - }); - - it('generates Goose custom-provider registration without persisting the key', () => { - const generation = tools.find(tool => tool.id === 'goose')!.generate(context); - const provider = generation.files.find(file => file.path.endsWith('freellmapi.json'))!.value as any; - const selection = generation.files.find(file => file.path.endsWith('config.yaml'))!.content!; - expect(provider).toMatchObject({ - name: 'freellmapi', - engine: 'openai', - api_key_env: 'FREELLMAPI_API_KEY', - base_url: 'http://localhost:3000/v1', - requires_auth: true, - dynamic_models: false, - }); - expect(provider.models.map((model: any) => model.name)).toEqual([ - 'fast-coder', - 'reasoning-model', - ]); - expect(selection).toContain('GOOSE_PROVIDER: freellmapi'); - expect(JSON.stringify(generation)).not.toContain('freellmapi-test-key'); - }); - - it('generates Qwen Code provider catalogs using a supported auth type', () => { - const generation = tools.find(tool => tool.id === 'qwen')!.generate(context); - const settings = generation.files.find(file => file.path.endsWith('settings.json'))!.value as any; - expect(settings.security.auth.selectedType).toBe('openai'); - expect(settings.model).toEqual({ name: 'fast-coder' }); - expect(settings.modelProviders.openai.protocol).toBe('openai'); - expect(settings.modelProviders.openai.models).toHaveLength(2); - expect(settings.modelProviders.openai.models[0]).toMatchObject({ - id: 'fast-coder', - envKey: 'FREELLMAPI_API_KEY', - baseUrl: 'http://localhost:3000/v1', - generationConfig: { contextWindowSize: 131072 }, - }); - expect(settings.modelProviders).not.toHaveProperty('freellmapi'); - }); - - it('wraps Roo Code imports in the documented provider profile envelope', () => { - const config = tools.find(tool => tool.id === 'roo')! - .generate(context).files[0].value as any; - expect(config.providerProfiles.currentApiConfigName).toBe('freellmapi'); - expect(config.providerProfiles.apiConfigs.freellmapi).toMatchObject({ - apiProvider: 'openai', - openAiBaseUrl: 'http://localhost:3000/v1', - openAiModelId: 'fast-coder', - }); - expect(config.globalSettings).toEqual({}); - }); - - it('writes Kilo trusted global config with every catalog model', () => { - const file = tools.find(tool => tool.id === 'kilo')!.generate(context).files[0]; - const config = file.value as any; - expect(asPosix(file.path)).toBe('/home/tester/.config/kilo/kilo.jsonc'); - expect(config.$schema).toBe('https://app.kilo.ai/config.json'); - expect(config.model).toBe('openai-compatible/fast-coder'); - expect(config.provider['openai-compatible'].options).toEqual({ - apiKey: '{env:FREELLMAPI_API_KEY}', - baseURL: 'http://localhost:3000/v1', - }); - expect(Object.keys(config.provider['openai-compatible'].models)).toEqual([ - 'fast-coder', - 'reasoning-model', - ]); - expect(config.provider['openai-compatible'].models['fast-coder']).toMatchObject({ - tool_call: true, - limit: { context: 131072, output: 8192 }, - }); - expect(JSON.stringify(config)).not.toContain('freellmapi-test-key'); - }); - - it('uses Crush openai-compat schema, lists every catalog model, and selects the default', () => { - const config = tools.find(tool => tool.id === 'crush')! - .generate(context).files[0].value as any; - expect(config.$schema).toBe('https://charm.land/crush.json'); - expect(config.providers.freellmapi.type).toBe('openai-compat'); - expect(config.providers.freellmapi.models.map((model: any) => model.id)).toEqual([ - 'fast-coder', - 'reasoning-model', - ]); - // A provider entry alone is never used: Crush auto-detects from whatever - // credentials it finds on the machine unless `models.large`/`small` name it. - expect(config.models).toEqual({ - large: { provider: 'freellmapi', model: 'fast-coder' }, - small: { provider: 'freellmapi', model: 'fast-coder' }, - }); - for (const model of config.providers.freellmapi.models) { - expect(model).toMatchObject({ - cost_per_1m_in: 0, - cost_per_1m_out: 0, - cost_per_1m_in_cached: 0, - cost_per_1m_out_cached: 0, - can_reason: false, - supports_attachments: false, - }); - } - }); - - it('keeps `auto` in the OpenCode, Crush and Goose rosters when it is the default', () => { - // OpenCode resolves `freellmapi/auto` only if `auto` is a declared model, - // and Crush's `models.large` must name a listed model; with `auto` in the - // live catalog all three generators put it first rather than filter it out. - const liveContext: GenerateContext = { - ...context, - models: [{ id: 'auto', name: 'Auto', available: true, context_window: 200_000 }, ...context.models], - }; - const opencode = tools.find(tool => tool.id === 'opencode')!.generate(liveContext).files[0].value as any; - expect(opencode.model).toBe('freellmapi/auto'); - expect(Object.keys(opencode.provider.freellmapi.models)).toEqual(['auto', 'fast-coder', 'reasoning-model']); - const crush = tools.find(tool => tool.id === 'crush')!.generate(liveContext).files[0].value as any; - expect(crush.models.large.model).toBe('auto'); - expect(crush.providers.freellmapi.models[0].id).toBe('auto'); - const goose = tools.find(tool => tool.id === 'goose')!.generate(liveContext); - const provider = goose.files.find(file => file.path.endsWith('freellmapi.json'))!.value as any; - expect(provider.models[0].name).toBe('auto'); - // Qwen Code silently falls back to the first listed model when the selected - // one is missing (it picked `fusion`); Kilo refuses to start outright. - const qwen = tools.find(tool => tool.id === 'qwen')!.generate(liveContext).files[0].value as any; - expect(qwen.modelProviders.openai.models[0].id).toBe('auto'); - const kilo = tools.find(tool => tool.id === 'kilo')!.generate(liveContext).files[0].value as any; - expect(kilo.model).toBe('openai-compatible/auto'); - expect(Object.keys(kilo.provider['openai-compatible'].models)[0]).toBe('auto'); - }); - - it('delivers the Continue secret through ~/.continue/.env', () => { - const generation = tools.find(tool => tool.id === 'continue')!.generate(context); - const env = generation.files.find(file => file.path.endsWith('.env'))!; - expect(asPosix(env.path)).toBe('/home/tester/.continue/.env'); - expect(env.sensitive).toBe(true); - expect(env.content).toBe('FREELLMAPI_API_KEY=freellmapi-test-key\n'); - const config = generation.files.find(file => file.path.endsWith('config.yaml'))!.content!; - expect(config).not.toContain('freellmapi-test-key'); - }); - - it('gives Roo an openai profile for the extension and a separate CLI import with an openrouter one', () => { - const files = tools.find(tool => tool.id === 'roo')!.generate(context).files; - const value = files[0].value as any; - const configs = value.providerProfiles.apiConfigs; - expect(value.providerProfiles.currentApiConfigName).toBe('freellmapi'); - // The CLI forces its provider onto the CURRENT profile of the file it - // imports, so its file must make the openrouter-type profile current. - const cli = files[1].value as any; - expect(asPosix(files[1].path)).toBe('/home/tester/.roo/freellmapi-cli.json'); - expect(cli.providerProfiles.currentApiConfigName).toBe('freellmapi-cli'); - expect(cli.providerProfiles.apiConfigs['freellmapi-cli']).toEqual(configs['freellmapi-cli']); - expect(asPosix(files[2].path)).toBe('/home/tester/.roo/cli-settings.json'); - expect(files[2].value).toEqual({ provider: 'openrouter', model: 'fast-coder' }); - expect(configs.freellmapi).toMatchObject({ - apiProvider: 'openai', - openAiBaseUrl: 'http://localhost:3000/v1', - openAiModelId: 'fast-coder', - openAiCustomModelInfo: { contextWindow: 131072, maxTokens: 8192 }, - }); - expect(configs['freellmapi-cli']).toEqual({ - apiProvider: 'openrouter', - openRouterBaseUrl: 'http://localhost:3000/v1', - openRouterApiKey: 'freellmapi-test-key', - openRouterModelId: 'fast-coder', - }); - }); - - it('declares a complete DeepSeek Harness route and claims the default model', () => { - const dsh = tools.find(tool => tool.id === 'dsh')!; - const generation = dsh.generate(context); - const [settings, env] = generation.files; - expect(asPosix(settings.path)).toBe('/home/tester/.dsh/settings.yaml'); - expect(settings.format).toBe('yaml'); - const value = settings.value as { - 'llm-pi-ai': { providers: Record }; - 'agent-default-model': { provider: string; model: string; reasoningEffort?: string }; - }; - const route = value['llm-pi-ai'].providers.freellmapi; - // A hand-declared route must carry api, baseURL, and a non-empty models - // list, or DSH refuses the whole section where it is written. - expect(route.api).toBe('openai-completions'); - expect(route.baseURL).toBe('http://localhost:3000/v1'); - expect(route.apiKeyEnv).toBe('FREELLMAPI_API_KEY'); - expect(route.models.map(model => model.id)).toEqual(['fast-coder', 'reasoning-model']); - expect(value['agent-default-model']).toEqual({ - provider: 'freellmapi', - model: 'fast-coder', - reasoningEffort: undefined, - }); - expect('reasoningEffort' in value['agent-default-model']).toBe(true); - // DSH loads $DSH_HOME/.env as its user environment layer, which is how - // `apiKeyEnv` resolves without an export. - expect({ ...env, path: asPosix(env.path) }).toMatchObject({ - path: '/home/tester/.dsh/.env', - format: 'env', - sensitive: true, - content: 'FREELLMAPI_API_KEY=freellmapi-test-key\n', - }); - }); - - it('adds a named DeepSeek Harness profile as a second route without moving the default', () => { - const dsh = tools.find(tool => tool.id === 'dsh')!; - const value = dsh.generate({ ...context, profile: 'Work Laptop' }).files[0].value as Record; - const providers = (value['llm-pi-ai'] as { providers: Record }).providers; - expect(Object.keys(providers)).toEqual(['freellmapi-work-laptop']); - expect(value['agent-default-model']).toBeUndefined(); - }); - - it('writes MiMo Code a custom provider its own schema accepts', () => { - const mimo = tools.find(tool => tool.id === 'mimo')!; - const [config] = mimo.generate(context).files; - // The global config directory is XDG-based, and `config.json` is the - // weakest of the three names it merges, so a hand-written - // `mimocode.json` still wins. - expect(asPosix(config.path)).toBe('/home/tester/.config/mimocode/config.json'); - const value = config.value as { - model: string; - provider: { - freellmapi: { - npm: string; - options: Record; - models: Record }>; - }; - }; - }; - const provider = value.provider.freellmapi; - expect(provider.npm).toBe('@ai-sdk/openai-compatible'); - expect(provider.options).toEqual({ - baseURL: 'http://localhost:3000/v1', - apiKey: '{env:FREELLMAPI_API_KEY}', - }); - // `provider..models..limit` requires context AND output. - expect(provider.models['fast-coder'].limit).toEqual({ context: 131072, output: 8192 }); - // The default model has to name an entry that exists in that map. - expect(value.model).toBe('freellmapi/fast-coder'); - expect(Object.keys(provider.models)).toContain(value.model.split('/')[1]); - // MIMOCODE_API_KEY and MIMOCODE_BASE_URL do not exist. - expect(JSON.stringify(mimo.generate(context))).not.toContain('MIMOCODE_'); - }); - - it('keeps the MiMo Code default model in its own provider map when auto leads', () => { - const liveContext: GenerateContext = { - ...context, - models: [ - { id: 'auto', name: 'Auto', available: true, context_window: 200_000 }, - ...context.models, - ], - }; - const value = tools.find(tool => tool.id === 'mimo')! - .generate(liveContext).files[0].value as { - model: string; - provider: { freellmapi: { models: Record } }; - }; - expect(value.model).toBe('freellmapi/auto'); - expect(Object.keys(value.provider.freellmapi.models)).toContain('auto'); - }); - - it('writes OpenClaw as a custom provider plus the default model ref, key via its own .env', () => { - // OpenClaw resolves `${VAR}` in openclaw.json from the global - // ~/.openclaw/.env it loads itself, so the pair works with nothing - // exported. A custom origin gets no attribution header from OpenClaw, so - // the provider carries a static User-Agent the gateway can classify. - const [config, env] = tools.find(tool => tool.id === 'openclaw')!.generate(context).files; - expect(asPosix(config.path)).toBe('/home/tester/.openclaw/openclaw.json'); - expect(config.format).toBe('json'); - const value = config.value as { - agents: { defaults: { model: { primary: string } } } - models: { providers: Record; models: { id: string }[] }> } - }; - expect(value.agents.defaults.model.primary).toBe('freellmapi/fast-coder'); - const provider = value.models.providers.freellmapi; - expect(provider.baseUrl).toBe('http://localhost:3000/v1'); - expect(provider.apiKey).toBe('${FREELLMAPI_API_KEY}'); - expect(provider.api).toBe('openai-completions'); - expect(provider.headers).toEqual({ 'User-Agent': 'openclaw' }); - expect(provider.models.map(model => model.id)).toEqual(['fast-coder', 'reasoning-model']); - expect(asPosix(env.path)).toBe('/home/tester/.openclaw/.env'); - expect(env.sensitive).toBe(true); - expect(env.content).toBe('FREELLMAPI_API_KEY=freellmapi-test-key\n'); - }); - - it('gives a named OpenClaw profile its own provider without touching the default model', () => { - const generation = tools.find(tool => tool.id === 'openclaw')!.generate({ ...context, profile: 'Work Box' }); - const value = generation.files[0].value as { agents?: unknown; models: { providers: Record } }; - expect(value.agents).toBeUndefined(); - expect(Object.keys(value.models.providers)).toEqual(['freellmapi-work-box']); - }); - - it('honours OPENCLAW_CONFIG_PATH and OPENCLAW_STATE_DIR the way OpenClaw does', () => { - vi.stubEnv('OPENCLAW_STATE_DIR', '/srv/claw'); - const [config, env] = tools.find(tool => tool.id === 'openclaw')!.generate(context).files; - expect(asPosix(config.path)).toBe('/srv/claw/openclaw.json'); - expect(asPosix(env.path)).toBe('/srv/claw/.env'); - vi.stubEnv('OPENCLAW_CONFIG_PATH', '/etc/openclaw.json'); - expect(tools.find(tool => tool.id === 'openclaw')!.generate(context).files[0].path).toBe('/etc/openclaw.json'); - }); - - it('writes Hermes Agent as a custom model block with the key routed through its .env', () => { - // config.yaml is Hermes's single source of truth for the endpoint: - // OPENAI_BASE_URL is ignored for non-OpenAI hosts and OPENAI_API_KEY is only - // sent to OpenAI hosts, so both go in the `model` block, the key as the - // `${VAR}` form Hermes expands from ~/.hermes/.env. Leftover key_env keys - // from the wizard are retired so they cannot shadow api_key. - const [config, env] = tools.find(tool => tool.id === 'hermes')!.generate(context).files; - expect(asPosix(config.path)).toBe('/home/tester/.hermes/config.yaml'); - expect(config.format).toBe('yaml'); - expect(config.value).toEqual({ - model: { - provider: 'custom', - default: 'fast-coder', - base_url: 'http://localhost:3000/v1', - api_key: '${FREELLMAPI_API_KEY}', - api_mode: 'chat_completions', - context_length: 131072, - default_headers: { 'User-Agent': 'hermes-agent' }, - key_env: undefined, - api_key_env: undefined, - }, - }); - expect(asPosix(env.path)).toBe('/home/tester/.hermes/.env'); - expect(env.sensitive).toBe(true); - expect(env.content).toBe('FREELLMAPI_API_KEY=freellmapi-test-key\n'); - }); - - it('gives a named Hermes profile a providers entry instead of the default model', () => { - vi.stubEnv('HERMES_HOME', '/srv/hermes'); - const [config] = tools.find(tool => tool.id === 'hermes')!.generate({ ...context, profile: 'lab' }).files; - expect(asPosix(config.path)).toBe('/srv/hermes/config.yaml'); - const value = config.value as { model?: unknown; providers: Record }; - expect(value.model).toBeUndefined(); - expect(value.providers['freellmapi-lab'].api).toBe('http://localhost:3000/v1'); - expect(value.providers['freellmapi-lab'].default_model).toBe('fast-coder'); - }); - - it('writes Pi as a models.json provider plus the startup default in settings.json', () => { - // Pi has no .env of its own, so the key is literal in the 0600 models.json. - // A provider entry only fills /model; defaultProvider + defaultModel in - // settings.json is what a fresh `pi` starts on. A custom origin gets the - // bare openai-node UA from Pi, so the provider names the client itself. - const [models, settings] = tools.find(tool => tool.id === 'pi')!.generate(context).files; - expect(asPosix(models.path)).toBe('/home/tester/.pi/agent/models.json'); - expect(models.format).toBe('json'); - expect(models.sensitive).toBe(true); - const provider = (models.value as { providers: Record }).providers.freellmapi; - expect(provider).toMatchObject({ - name: 'FreeLLMAPI', - baseUrl: 'http://localhost:3000/v1', - api: 'openai-completions', - apiKey: 'freellmapi-test-key', - headers: { 'User-Agent': 'pi-coding-agent' }, - }); - expect(provider.models.map((model: { id: string }) => model.id)).toEqual(['fast-coder', 'reasoning-model']); - expect(provider.models[0]).toEqual({ - id: 'fast-coder', - name: 'Fast Coder', - reasoning: false, - input: ['text'], - cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 }, - contextWindow: 131072, - maxTokens: 8192, - }); - expect(asPosix(settings.path)).toBe('/home/tester/.pi/agent/settings.json'); - expect(settings.sensitive).toBeUndefined(); - expect(settings.value).toEqual({ defaultProvider: 'freellmapi', defaultModel: 'fast-coder' }); - }); - - it('keeps `auto` first in the Pi roster when it is the default', () => { - const liveContext: GenerateContext = { - ...context, - models: [{ id: 'auto', name: 'Auto', available: true, context_window: 200_000 }, ...context.models], - }; - const [models, settings] = tools.find(tool => tool.id === 'pi')!.generate(liveContext).files; - const provider = (models.value as { providers: Record }).providers.freellmapi; - expect(provider.models.map(model => model.id)).toEqual(['auto', 'fast-coder', 'reasoning-model']); - expect(settings.value).toEqual({ defaultProvider: 'freellmapi', defaultModel: 'auto' }); - }); - - it('gives a named Pi profile its own provider and leaves the default model alone', () => { - vi.stubEnv('PI_CODING_AGENT_DIR', '/srv/pi'); - const files = tools.find(tool => tool.id === 'pi')!.generate({ ...context, profile: 'Work Box' }).files; - expect(files).toHaveLength(1); - expect(asPosix(files[0].path)).toBe('/srv/pi/models.json'); - const providers = (files[0].value as { providers: Record }).providers; - expect(Object.keys(providers)).toEqual(['freellmapi-work-box']); - expect(providers['freellmapi-work-box'].name).toBe('FreeLLMAPI (Work Box)'); - }); - - it('writes Reasonix as a [[providers]] entry plus its key in the Reasonix .env', () => { - // Reasonix names the key variable in config.toml and keeps the value in the - // .env beside it, which it loads itself. `default_model` is provider/model - // and must sit in the root region, above the array entry. - const [config, env] = tools.find(tool => tool.id === 'reasonix')!.generate(context).files; - expect(asPosix(config.path)).toBe('/home/tester/.reasonix/config.toml'); - expect(config.format).toBe('toml'); - expect(config.sensitive).toBeUndefined(); - const lines = (config.content ?? '').split('\n'); - expect(lines[0]).toBe('default_model = "freellmapi/fast-coder"'); - expect(lines.indexOf('[[providers]]')).toBeGreaterThan(0); - expect(lines).toContain('name = "freellmapi"'); - expect(lines).toContain('kind = "openai"'); - expect(lines).toContain('base_url = "http://localhost:3000/v1"'); - expect(lines).toContain('models = ["fast-coder", "reasoning-model"]'); - expect(lines).toContain('default = "fast-coder"'); - expect(lines).toContain('api_key_env = "FREELLMAPI_API_KEY"'); - expect(lines).toContain('context_window = 131072'); - expect(config.content).not.toContain('freellmapi-test-key'); - expect({ ...env, path: asPosix(env.path) }).toEqual({ - path: '/home/tester/.reasonix/.env', - format: 'env', - sensitive: true, - content: 'FREELLMAPI_API_KEY=freellmapi-test-key\n', - }); - }); - - it('keeps `auto` first in the Reasonix roster when it is the default', () => { - const liveContext: GenerateContext = { - ...context, - models: [{ id: 'auto', name: 'Auto', available: true, context_window: 200_000 }, ...context.models], - }; - const [config] = tools.find(tool => tool.id === 'reasonix')!.generate(liveContext).files; - expect(config.content).toContain('default_model = "freellmapi/auto"'); - expect(config.content).toContain('models = ["auto", "fast-coder", "reasoning-model"]'); - }); - - it('gives a named Reasonix profile its own provider and leaves the default model alone', () => { - vi.stubEnv('REASONIX_HOME', '/srv/reasonix'); - const [config, env] = tools.find(tool => tool.id === 'reasonix')!.generate({ ...context, profile: 'Work Box' }).files; - expect(asPosix(config.path)).toBe('/srv/reasonix/config.toml'); - expect(asPosix(env.path)).toBe('/srv/reasonix/.env'); - expect(config.content).not.toContain('default_model'); - expect(config.content!.split('\n')[0]).toBe('[[providers]]'); - expect(config.content).toContain('name = "freellmapi-work-box"'); - expect(config.content).toContain('display_name = "FreeLLMAPI (Work Box)"'); - }); - - it('writes ZCode as a custom provider in ~/.zcode/v2/provider_config.json', () => { - // ZCode has no env lookup for custom providers — the key has to live in - // the file, which the writer then protects with 0600 (sensitive: true). - const [config] = tools.find(tool => tool.id === 'zcode')!.generate(context).files; - expect(asPosix(config.path)).toBe('/home/tester/.zcode/v2/provider_config.json'); - expect(config.format).toBe('json'); - expect(config.sensitive).toBe(true); - const value = config.value as { - schemaVersion: number; - config: { - providerConfigRules: { providerRules: { - providerId: string; providerName: string; - config: { access: { type: string; apiKey: string }; api: { type: string; baseUrl: string }; personalModelIds: string[] }; - }[] }; - modelConfigRules: { - providerModelRules: { providerId: string; modelId: string; config: { properties: { contextWindow: number }; optionSpecs: { maxOutputTokens: { max: number; map: string } } } }[]; - manualProviderModelRules: unknown[]; - }; - providerOrder: string[]; - defaultModelSelection: { providerId: string; modelId: string }; - }; - }; - expect(value.schemaVersion).toBe(1); - const [provider] = value.config.providerConfigRules.providerRules; - expect(provider.providerId).toBe('freellmapi'); - expect(provider.providerName).toBe('FreeLLMAPI'); - expect(provider.config.access).toEqual({ type: 'api-key', apiKey: 'freellmapi-test-key' }); - expect(provider.config.api).toEqual({ type: 'openai-chat-completions', baseUrl: 'http://localhost:3000/v1' }); - expect(provider.config.personalModelIds).toEqual(['fast-coder', 'reasoning-model']); - const rules = value.config.modelConfigRules.providerModelRules; - expect(rules.map(rule => rule.modelId)).toEqual(['fast-coder', 'reasoning-model']); - expect(rules[0].config.properties.contextWindow).toBe(131072); - expect(rules[0].config.optionSpecs.maxOutputTokens.map).toBe('{"max_tokens": maxOutputTokens}'); - expect(value.config.modelConfigRules.manualProviderModelRules).toEqual([]); - expect(value.config.defaultModelSelection).toEqual({ providerId: 'freellmapi', modelId: 'fast-coder' }); - // The rule arrays are merged per provider, never replaced whole. - expect(config.arrays?.['config.providerConfigRules.providerRules']).toEqual({ ownedBy: { providerId: 'freellmapi' } }); - }); - - it('follows ZCODE_PERSONAL_PROVIDER_CONFIG_FILE for ZCode', () => { - vi.stubEnv('ZCODE_PERSONAL_PROVIDER_CONFIG_FILE', '/srv/zcode/provider_config.json'); - const [config] = tools.find(tool => tool.id === 'zcode')!.generate(context).files; - expect(asPosix(config.path)).toBe('/srv/zcode/provider_config.json'); - }); - - it('gives a named ZCode profile its own provider entry', () => { - const [config] = tools.find(tool => tool.id === 'zcode')!.generate({ ...context, profile: 'Work Box' }).files; - const value = config.value as { config: { providerConfigRules: { providerRules: { providerId: string; providerName: string }[] } } }; - const [provider] = value.config.providerConfigRules.providerRules; - expect(provider.providerId).toBe('freellmapi-work-box'); - expect(provider.providerName).toBe('FreeLLMAPI (Work Box)'); - }); - - it('writes AtomCode as default_provider plus a typed [providers.freellmapi] table', () => { - // AtomCode ignores a bare `[provider]` table: it looks up the table named - // by the root `default_provider` key, and only a `type = "openai"` table - // speaks the OpenAI-compatible wire. `context_window` is its own key. - const [config] = tools.find(tool => tool.id === 'atomcode')!.generate(context).files; - expect(asPosix(config.path)).toBe('/home/tester/.atomcode/config.toml'); - expect(config.format).toBe('toml'); - expect(config.sensitive).toBe(true); - const lines = (config.content ?? '').split('\n'); - expect(lines[0]).toBe('default_provider = "freellmapi"'); - expect(lines).toContain('[providers.freellmapi]'); - expect(lines.indexOf('[providers.freellmapi]')).toBeGreaterThan(0); - expect(lines).toContain('type = "openai"'); - expect(lines).toContain('base_url = "http://localhost:3000/v1"'); - expect(lines).toContain('api_key = "freellmapi-test-key"'); - expect(lines).toContain('model = "fast-coder"'); - expect(lines).toContain('context_window = 131072'); - expect(config.content).not.toContain('[provider]'); - }); - - it('uses /v1 for every OpenAI-compatible generated base URL', () => { - for (const tool of tools.filter(entry => entry.protocol.startsWith('OpenAI'))) { - expect(tool.baseUrlSupport, tool.id).toBe('/v1'); - } - expect(tools.find(tool => tool.id === 'claude')!.baseUrlSupport).toBe('root'); - }); - - it('keeps the dashboard metadata export in sync with the tool catalog', () => { - const expected = tools.map(({ generate: _generate, ...tool }) => tool); - const packageMetadata = JSON.parse(fs.readFileSync( - path.resolve(import.meta.dirname, '../tools.json'), - 'utf8', - )); - const dashboardMetadata = JSON.parse(fs.readFileSync( - path.resolve(import.meta.dirname, '../../client/src/data/agent-tools.json'), - 'utf8', - )); - expect(packageMetadata).toEqual(expected); - expect(dashboardMetadata).toEqual(expected); - }); -}); diff --git a/cli/src/tools.ts b/cli/src/tools.ts deleted file mode 100644 index 0a9961719711f274b96dd3a78d736fe47ab9d6ea..0000000000000000000000000000000000000000 --- a/cli/src/tools.ts +++ /dev/null @@ -1,1173 +0,0 @@ -import path from 'node:path'; -import { clientNotes, detectChatGptClient } from './detect.js'; -import type { - CatalogModel, - GenerateContext, - Generation, - ToolDefinition, -} from './types.js'; - -function rootUrl(url: string): string { - return url.trim().replace(/\/+$/, '').replace(/\/v1$/i, ''); -} - -function v1Url(url: string): string { - return `${rootUrl(url)}/v1`; -} - -function primaryModel(models: CatalogModel[], requestedId?: string): CatalogModel { - // An explicit --model wins over every heuristic below. It is validated - // against the UNFILTERED catalog before we get here, so an id absent from - // `models` (the available-only roster) is a real, registered model that is - // merely out of quota right now — pin it anyway rather than silently writing - // a different model into the user's config. - if (requestedId) return models.find(model => model.id === requestedId) ?? { id: requestedId }; - // `auto` — the router picking the best model per request — is the whole - // point of the gateway and the right default for a generated config. Never - // fall back to `fusion` (multi-model fan-out) by accident. - return models.find(model => model.id === 'auto') - ?? models.find(model => model.id !== 'fusion' && model.available !== false) - ?? models.find(model => model.id !== 'fusion') - ?? { id: 'auto', name: 'Auto', context_window: 128_000 }; -} - -function catalogModels(models: CatalogModel[]): CatalogModel[] { - const available = models.filter(model => model.id !== 'auto' && model.available !== false); - if (available.length) return available; - const nonAuto = models.filter(model => model.id !== 'auto'); - return nonAuto.length ? nonAuto : [primaryModel(models)]; -} - -function contextWindow(model: CatalogModel): number { - return model.context_window ?? model.context_length ?? 128_000; -} - -function outputLimit(model: CatalogModel): number { - return Math.min(8192, contextWindow(model)); -} - -function yamlString(value: string): string { - return JSON.stringify(value); -} - -function claude(ctx: GenerateContext): Generation { - const model = primaryModel(ctx.models, ctx.requestedModelId); - const directory = ctx.profile === 'default' - ? path.join(ctx.homeDir, '.claude') - : path.join(ctx.homeDir, '.claude', 'profiles', ctx.profile); - return { - files: [{ - path: path.join(directory, 'settings.json'), - format: 'json', - sensitive: true, - value: { - env: { - ANTHROPIC_BASE_URL: rootUrl(ctx.url), - ANTHROPIC_AUTH_TOKEN: ctx.apiKey, - ANTHROPIC_MODEL: model.id, - ANTHROPIC_DEFAULT_OPUS_MODEL: model.id, - ANTHROPIC_DEFAULT_SONNET_MODEL: model.id, - ANTHROPIC_DEFAULT_HAIKU_MODEL: model.id, - CLAUDE_CODE_AUTO_COMPACT_WINDOW: String(contextWindow(model)), - CLAUDE_CODE_ENABLE_GATEWAY_MODEL_DISCOVERY: '1', - }, - }, - }], - notes: [ - ctx.profile === 'default' - ? 'Claude Code will read this configuration automatically.' - : `Launch with CLAUDE_CONFIG_DIR=${directory} claude`, - 'For zero-persistence credentials, prefer: freellmapi launch', - ], - }; -} - -function codex(ctx: GenerateContext): Generation { - const model = primaryModel(ctx.models, ctx.requestedModelId); - const compact = Math.max(16_000, Math.floor(contextWindow(model) * 0.9)); - const providerTable = [ - '[model_providers.freellmapi]', - 'name = "FreeLLMAPI"', - `base_url = ${JSON.stringify(v1Url(ctx.url))}`, - 'wire_api = "responses"', - 'env_key = "FREELLMAPI_API_KEY"', - 'requires_openai_auth = false', - ]; - // Codex reads exactly one file, ~/.codex/config.toml. Named profiles are - // `[profiles.NAME]` tables in that same file, selected with - // `codex --profile NAME`; separate per-profile files are never read. - const content = ctx.profile === 'default' - ? [ - '# freellmapi:start', - `model = ${JSON.stringify(model.id)}`, - 'model_provider = "freellmapi"', - `model_context_window = ${contextWindow(model)}`, - `model_auto_compact_token_limit = ${compact}`, - 'tool_output_token_limit = 20000', - '', - ...providerTable, - '# freellmapi:end', - '', - ] - : [ - '# freellmapi:start', - ...providerTable, - '', - `[profiles.${/^[A-Za-z0-9_-]+$/.test(ctx.profile) ? ctx.profile : JSON.stringify(ctx.profile)}]`, - `model = ${JSON.stringify(model.id)}`, - 'model_provider = "freellmapi"', - '# freellmapi:end', - '', - ]; - return { - files: [{ - path: path.join(ctx.homeDir, '.codex', 'config.toml'), - format: 'toml', - content: content.join('\n'), - }], - notes: [ - ...clientNotes(ctx.detectedClient ?? detectChatGptClient()), - 'Export FREELLMAPI_API_KEY before running codex; the key is not written to config.toml.', - ...(ctx.profile === 'default' - ? [] - : [`Activate this profile with: codex --profile ${ctx.profile}`]), - `Selected ${model.id} with a ${contextWindow(model)} token context window.`, - ], - }; -} - -function cline(ctx: GenerateContext): Generation { - const model = primaryModel(ctx.models, ctx.requestedModelId); - return { - files: [{ - path: path.join(ctx.homeDir, '.cline', 'data', 'settings', 'providers.json'), - format: 'json', - sensitive: true, - value: { - version: 1, - lastUsedProvider: 'openai-compatible', - providers: { - 'openai-compatible': { - settings: { - provider: 'openai-compatible', - protocol: 'openai-chat', - client: 'openai-compatible', - model: model.id, - baseUrl: v1Url(ctx.url), - apiKey: ctx.apiKey, - contextWindow: contextWindow(model), - capabilities: ['streaming', 'tools'], - }, - // The field is required by Cline's persisted schema. Keeping it - // deterministic makes repeated setup runs idempotent. - updatedAt: '2026-07-27T00:00:00.000Z', - tokenSource: 'manual', - }, - }, - }, - }], - notes: ['Cline will use the generated OpenAI Compatible provider immediately.'], - }; -} - -function continueDev(ctx: GenerateContext): Generation { - const model = primaryModel(ctx.models, ctx.requestedModelId); - return { - files: [{ - path: path.join(ctx.homeDir, '.continue', 'config.yaml'), - format: 'yaml', - content: [ - '# freellmapi:start', - 'name: FreeLLMAPI', - 'version: 1.0.0', - 'schema: v1', - 'models:', - ' - name: FreeLLMAPI', - ' provider: openai', - ` model: ${yamlString(model.id)}`, - ` apiBase: ${yamlString(v1Url(ctx.url))}`, - ' apiKey: ${{ secrets.FREELLMAPI_API_KEY }}', - ' capabilities:', - ' - tool_use', - ' defaultCompletionOptions:', - ` contextLength: ${contextWindow(model)}`, - '# freellmapi:end', - '', - ].join('\n'), - }, - // `${{ secrets.NAME }}` is resolved, in order, from the process - // environment, ~/.continue/.env, /.continue/.env and /.env - // before Continue's Hub is consulted. The IDE extensions cannot see the - // shell environment, so ~/.continue/.env is the one place that serves the - // CLI and the extensions alike — and it keeps the raw key out of the YAML. - { - path: path.join(ctx.homeDir, '.continue', '.env'), - format: 'env', - sensitive: true, - content: `FREELLMAPI_API_KEY=${ctx.apiKey}\n`, - }], - notes: [ - 'The key is in ~/.continue/.env (read by the Continue CLI and the IDE extensions); config.yaml references it as a secret.', - 'One-shot check with the CLI (npm install -g @continuedev/cli): cn -p "Say hello"', - ], - }; -} - -function aider(ctx: GenerateContext): Generation { - const model = primaryModel(ctx.models, ctx.requestedModelId); - return { - files: [{ - path: path.join(ctx.homeDir, '.aider.conf.yml'), - format: 'yaml', - sensitive: true, - content: [ - '# freellmapi:start', - `openai-api-base: ${yamlString(v1Url(ctx.url))}`, - `openai-api-key: ${yamlString(ctx.apiKey)}`, - `model: ${yamlString(`openai/${model.id}`)}`, - '# freellmapi:end', - '', - ].join('\n'), - }], - notes: [ - `Environment-only alternative: OPENAI_API_BASE=${v1Url(ctx.url)} OPENAI_API_KEY=â€Ļ aider --model openai/${model.id}`, - ], - }; -} - -// OpenCode resolves `provider/model` refs only against the ids a provider -// declares, so the default model has to be in the `models` map — `auto`, the -// usual default, is filtered out of the plain catalog roster, so it goes back -// at the front. And a provider entry alone is inert: without a top-level -// `model` OpenCode keeps whatever it used before (or nothing on a fresh -// install), so the generated config names the default too. -function opencode(ctx: GenerateContext): Generation { - const model = primaryModel(ctx.models, ctx.requestedModelId); - const roster = catalogModels(ctx.models); - const modelEntries = Object.fromEntries( - [model, ...roster.filter(entry => entry.id !== model.id)].map(entry => [entry.id, { - name: entry.name ?? entry.id, - limit: { context: contextWindow(entry), output: outputLimit(entry) }, - }]), - ); - return { - files: [{ - path: path.join(ctx.homeDir, '.config', 'opencode', 'opencode.json'), - format: 'json', - value: { - $schema: 'https://opencode.ai/config.json', - model: `freellmapi/${model.id}`, - provider: { - freellmapi: { - npm: '@ai-sdk/openai-compatible', - name: 'FreeLLMAPI', - options: { - baseURL: v1Url(ctx.url), - apiKey: '{env:FREELLMAPI_API_KEY}', - }, - models: modelEntries, - }, - }, - }, - }], - notes: [ - 'Export FREELLMAPI_API_KEY before starting OpenCode.', - `freellmapi/${model.id} is now the default model.`, - ], - }; -} - -function goose(ctx: GenerateContext): Generation { - const model = primaryModel(ctx.models, ctx.requestedModelId); - // GOOSE_MODEL names the default, so it belongs in the provider's list too; - // `auto` is filtered out of the plain roster and goes back at the front. - const roster = catalogModels(ctx.models); - const models = [model, ...roster.filter(entry => entry.id !== model.id)].map(entry => ({ - name: entry.id, - context_limit: contextWindow(entry), - })); - return { - files: [ - { - path: path.join( - ctx.homeDir, - '.config', - 'goose', - 'custom_providers', - 'freellmapi.json', - ), - format: 'json', - value: { - name: 'freellmapi', - engine: 'openai', - display_name: 'FreeLLMAPI', - description: 'FreeLLMAPI OpenAI-compatible gateway', - api_key_env: 'FREELLMAPI_API_KEY', - base_url: v1Url(ctx.url), - models, - supports_streaming: true, - requires_auth: true, - dynamic_models: false, - preserves_thinking: true, - }, - }, - { - path: path.join(ctx.homeDir, '.config', 'goose', 'config.yaml'), - format: 'yaml', - content: [ - '# freellmapi:start', - 'GOOSE_PROVIDER: freellmapi', - `GOOSE_MODEL: ${yamlString(model.id)}`, - '# freellmapi:end', - '', - ].join('\n'), - }, - ], - notes: ['Export FREELLMAPI_API_KEY before starting Goose.'], - }; -} - -function qwen(ctx: GenerateContext): Generation { - const model = primaryModel(ctx.models, ctx.requestedModelId); - const dir = path.join(ctx.homeDir, '.qwen'); - // Qwen Code looks the selected model up in the provider list and, finding - // nothing, silently falls back to the FIRST listed model — with `auto` - // filtered out of the roster that was `fusion`, a multi-model fan-out. The - // default goes first so the lookup always succeeds. - const roster = catalogModels(ctx.models); - const models = [model, ...roster.filter(entry => entry.id !== model.id)].map(entry => ({ - id: entry.id, - name: entry.name ?? entry.id, - envKey: 'FREELLMAPI_API_KEY', - baseUrl: v1Url(ctx.url), - generationConfig: { - contextWindowSize: contextWindow(entry), - }, - })); - return { - files: [ - { - path: path.join(dir, 'settings.json'), - format: 'json', - value: { - model: { name: model.id }, - modelProviders: { - openai: { - protocol: 'openai', - models, - }, - }, - security: { auth: { selectedType: 'openai' } }, - }, - }, - { - path: path.join(dir, '.env'), - format: 'env', - sensitive: true, - content: `FREELLMAPI_API_KEY=${ctx.apiKey}\n`, - }, - ], - notes: [ - `Native Gemini mode is also available at ${rootUrl(ctx.url)}/v1beta with GEMINI_API_KEY.`, - ], - }; -} - -// Roo Code imports a settings file named by `roo-cline.autoImportSettingsPath` -// and makes that file's `currentApiConfigName` the active profile. The VS Code -// extension can use an `openai`-type profile (Roo's "OpenAI Compatible"), but -// the Roo CLI only starts with a provider from its own short list and then -// forces `apiProvider: openrouter` plus the --model onto whatever profile is -// CURRENT — so the CLI is served by a separate import file whose current -// profile is an `openrouter`-type one with the base URL overridden. The two -// stores never meet: the extension reads VS Code settings, the CLI reads -// ~/.vscode-mock/global-storage/global-state.json. -function roo(ctx: GenerateContext): Generation { - const model = primaryModel(ctx.models, ctx.requestedModelId); - const importPath = path.join(ctx.homeDir, '.roo', 'freellmapi.json'); - const cliImportPath = path.join(ctx.homeDir, '.roo', 'freellmapi-cli.json'); - const cliProfile = { - apiProvider: 'openrouter', - openRouterBaseUrl: v1Url(ctx.url), - openRouterApiKey: ctx.apiKey, - openRouterModelId: model.id, - }; - return { - files: [ - { - path: importPath, - format: 'json', - sensitive: true, - value: { - providerProfiles: { - currentApiConfigName: 'freellmapi', - apiConfigs: { - freellmapi: { - apiProvider: 'openai', - openAiBaseUrl: v1Url(ctx.url), - openAiApiKey: ctx.apiKey, - openAiModelId: model.id, - openAiStreamingEnabled: true, - openAiCustomModelInfo: { - contextWindow: contextWindow(model), - maxTokens: outputLimit(model), - supportsPromptCache: false, - }, - }, - 'freellmapi-cli': cliProfile, - }, - }, - globalSettings: {}, - }, - }, - { - path: cliImportPath, - format: 'json', - sensitive: true, - value: { - providerProfiles: { - currentApiConfigName: 'freellmapi-cli', - apiConfigs: { 'freellmapi-cli': cliProfile }, - }, - globalSettings: {}, - }, - }, - // The CLI's own defaults file. `provider` is honoured (it guards against - // the CLI defaulting to Roo Cloud when a Roo token exists); `model` is - // shadowed in Roo CLI 0.1.x by the -m flag's built-in default, so -m is - // still needed on the command line until Roo fixes that. - { - path: path.join(ctx.homeDir, '.roo', 'cli-settings.json'), - format: 'json', - value: { provider: 'openrouter', model: model.id }, - }, - ], - notes: [ - `VS Code: add "roo-cline.autoImportSettingsPath": "${importPath}" to settings.json and reload (or Roo Settings → Import); the freellmapi profile becomes current.`, - `Roo CLI: merge {"roo-cline.autoImportSettingsPath": "${cliImportPath}"} into ~/.vscode-mock/global-storage/global-state.json (create it if missing), then:`, - ` export OPENROUTER_API_KEY= # the CLI refuses to start without it, even with the key in the profile`, - ` roo -m ${model.id} "Say hello" # -m is required: the CLI's built-in default model shadows cli-settings.json`, - ], - }; -} - -function kilo(ctx: GenerateContext): Generation { - const model = primaryModel(ctx.models, ctx.requestedModelId); - // Kilo refuses to start (`Model not found: openai-compatible/auto`) unless - // the selected model is in the provider's map, so the default goes first. - const roster = catalogModels(ctx.models); - const models = Object.fromEntries([model, ...roster.filter(entry => entry.id !== model.id)].map(entry => [ - entry.id, - { - name: entry.name ?? entry.id, - tool_call: true, - limit: { - context: contextWindow(entry), - output: outputLimit(entry), - }, - }, - ])); - return { - files: [{ - path: path.join(ctx.homeDir, '.config', 'kilo', 'kilo.jsonc'), - format: 'json', - value: { - $schema: 'https://app.kilo.ai/config.json', - model: `openai-compatible/${model.id}`, - provider: { - 'openai-compatible': { - options: { - apiKey: '{env:FREELLMAPI_API_KEY}', - baseURL: v1Url(ctx.url), - }, - models, - }, - }, - }, - }], - notes: [ - 'Export FREELLMAPI_API_KEY before starting Kilo; global config is trusted for {env:â€Ļ} expansion.', - `One-shot check: kilo run "Say hello" — openai-compatible/${model.id} is the default model.`, - ], - }; -} - -// Crush picks its provider by the top-level `models.large` / `models.small` -// selection, and with neither set it auto-detects from whatever credentials -// are on the machine (an AWS profile, ANTHROPIC_API_KEY, a Charm login) — a -// provider entry alone never gets used. Both slots are pointed at the chosen -// model, which therefore has to be in the provider's list; `auto` is filtered -// out of the plain roster, so it goes back at the front. -function crush(ctx: GenerateContext): Generation { - const model = primaryModel(ctx.models, ctx.requestedModelId); - const roster = catalogModels(ctx.models); - const models = [model, ...roster.filter(entry => entry.id !== model.id)].map(entry => ({ - id: entry.id, - name: entry.name ?? entry.id, - cost_per_1m_in: 0, - cost_per_1m_out: 0, - cost_per_1m_in_cached: 0, - cost_per_1m_out_cached: 0, - context_window: contextWindow(entry), - default_max_tokens: outputLimit(entry), - can_reason: false, - supports_attachments: false, - })); - return { - files: [{ - path: path.join(ctx.homeDir, '.config', 'crush', 'crush.json'), - format: 'json', - value: { - $schema: 'https://charm.land/crush.json', - models: { - large: { provider: 'freellmapi', model: model.id }, - small: { provider: 'freellmapi', model: model.id }, - }, - providers: { - freellmapi: { - name: 'FreeLLMAPI', - type: 'openai-compat', - base_url: v1Url(ctx.url), - api_key: '$FREELLMAPI_API_KEY', - models, - }, - }, - }, - }], - notes: [ - 'Export FREELLMAPI_API_KEY before starting Crush.', - `freellmapi/${model.id} is now the large and small model.`, - ], - }; -} - -// DeepSeek Harness (dsh) reads one YAML settings document, $DSH_HOME/settings.yaml -// (default ~/.dsh). Every provider is a route under `llm-pi-ai.providers`; -// a route pi-ai's installed catalog does not ship must spell out `api`, -// `baseURL`, and a non-empty `models` list, which is exactly what a gateway -// with a per-user live catalog is. The key travels through `apiKeyEnv`: -// DSH loads $DSH_HOME/.env as its user environment layer, so writing the key -// there (0600) makes the route work on the next request without an export. -// No `compat` switches: the gateway already maps `developer` → `system` and -// honours `max_completion_tokens`, the two things pi-ai sends to an endpoint -// it cannot recognize. -function dsh(ctx: GenerateContext): Generation { - const model = primaryModel(ctx.models, ctx.requestedModelId); - // DSH_HOME relocates the whole harness home; nothing else about the - // layout changes, so honour it the way dsh itself does. - const home = process.env.DSH_HOME?.trim() || path.join(ctx.homeDir, '.dsh'); - // A route id is permanent in DSH (sessions, defaults, and credential refs - // name it) and must be lowercase; a named profile becomes a second route - // beside the default one rather than replacing it. - const route = ctx.profile === 'default' - ? 'freellmapi' - : `freellmapi-${ctx.profile.toLowerCase().replace(/[^a-z0-9]+/g, '-').replace(/^-+|-+$/g, '')}`; - const roster = catalogModels(ctx.models); - const entries = [model, ...roster.filter(entry => entry.id !== model.id)] - .map(entry => ({ - id: entry.id, - name: entry.name ?? entry.id, - contextWindow: contextWindow(entry), - maxTokens: outputLimit(entry), - })); - return { - files: [ - { - path: path.join(home, 'settings.yaml'), - format: 'yaml', - value: { - 'llm-pi-ai': { - providers: { - [route]: { - displayName: ctx.profile === 'default' ? 'FreeLLMAPI' : `FreeLLMAPI (${ctx.profile})`, - apiKeyEnv: 'FREELLMAPI_API_KEY', - api: 'openai-completions', - baseURL: v1Url(ctx.url), - models: entries, - }, - }, - }, - // The default model is only claimed for the default profile; a - // named route is an extra option in the picker, not a takeover. - // A previous default's `reasoningEffort` must go with it: DSH - // refuses a request naming a level the selected model cannot - // take, and a hand-declared model declares none. - ...(ctx.profile === 'default' - ? { - 'agent-default-model': { - provider: route, - model: model.id, - reasoningEffort: undefined, - }, - } - : {}), - }, - }, - { - path: path.join(home, '.env'), - format: 'env', - sensitive: true, - content: `FREELLMAPI_API_KEY=${ctx.apiKey}\n`, - }, - ], - notes: [ - `Start DeepSeek Harness with: npx @deepseek-ai/dsh web — ${route}/${model.id} is ${ctx.profile === 'default' ? 'the default model' : 'in the model picker'}.`, - 'One-shot check: npx @deepseek-ai/dsh --profile headless "Say hello"', - 'Settings are hot-reloaded, so a running dsh picks this up on its next request.', - `Models are declared text-only; give a vision model \`input: [text, image]\` under ${route}.models in settings.yaml to send it images.`, - ], - }; -} - -// MiMo Code is an OpenCode derivative, so the config is the same shape as the -// OpenCode one above: a custom `provider` entry backed by the AI SDK's -// openai-compatible package, with credentials as `options.apiKey` / -// `options.baseURL` (mimo.xiaomi.com/mimocode/models-provider). Two things -// differ from OpenCode and both matter: -// * the file lives in MiMo's own global config directory. That is -// `$XDG_CONFIG_HOME/mimocode` (`~/.config/mimocode` on macOS and Linux), -// relocated wholesale to `$MIMOCODE_HOME/config` when that is set. The -// directory accepts `config.json`, `mimocode.json` and `mimocode.jsonc` -// and merges them in that order, so writing `config.json` — the first and -// weakest layer — leaves a hand-written `mimocode.json` in charge. -// * `provider..models..limit` requires BOTH `context` and `output` -// in MiMo's schema, the same as OpenCode's (https://opencode.ai/config.json). -// There is no MIMOCODE_API_KEY or MIMOCODE_BASE_URL: MiMo's environment -// variables locate resources and toggle features, they are not a fallback for -// config fields. The key travels through the config file's own `{env:VAR}` -// substitution instead, which keeps it off disk. -function mimo(ctx: GenerateContext): Generation { - const model = primaryModel(ctx.models, ctx.requestedModelId); - const configDir = process.env.MIMOCODE_HOME?.trim() - ? path.join(process.env.MIMOCODE_HOME.trim(), 'config') - : path.join( - process.env.XDG_CONFIG_HOME?.trim() || path.join(ctx.homeDir, '.config'), - 'mimocode', - ); - // The default model is named as `freellmapi/`, so it has to exist in the - // provider's own `models` map — `auto`, the usual default, is filtered out - // of the plain catalog roster, so put the chosen model back at the front. - const roster = catalogModels(ctx.models); - const modelEntries = Object.fromEntries( - [model, ...roster.filter(entry => entry.id !== model.id)].map(entry => [entry.id, { - name: entry.name ?? entry.id, - limit: { context: contextWindow(entry), output: outputLimit(entry) }, - }]), - ); - return { - files: [{ - path: path.join(configDir, 'config.json'), - format: 'json', - value: { - $schema: 'https://mimo.xiaomi.com/mimocode/config.json', - provider: { - freellmapi: { - npm: '@ai-sdk/openai-compatible', - name: 'FreeLLMAPI', - options: { - baseURL: v1Url(ctx.url), - apiKey: '{env:FREELLMAPI_API_KEY}', - }, - models: modelEntries, - }, - }, - model: `freellmapi/${model.id}`, - }, - }], - notes: [ - 'Install MiMo Code with: curl -fsSL https://mimo.xiaomi.com/install | bash', - 'Export FREELLMAPI_API_KEY before starting MiMo Code.', - `Start it with \`mimo\` — freellmapi/${model.id} is the default model.`, - 'One-shot check: mimo run "Say hello"', - ], - }; -} - -// OpenClaw reads one JSON5 document, $OPENCLAW_CONFIG_PATH (default -// ~/.openclaw/openclaw.json, the state dir being relocatable through -// OPENCLAW_STATE_DIR / OPENCLAW_HOME). Every model endpoint is an entry under -// `models.providers`, and a provider its bundled catalog does not know must -// spell out `baseUrl`, `api` and a non-empty `models` list — exactly what a -// gateway with a per-user live catalog is. `api: openai-completions` is the -// adapter for a /v1/chat/completions backend; OpenClaw already strips the -// developer role and attribution headers for a custom origin, so no `compat` -// switches are needed. The key travels as `${FREELLMAPI_API_KEY}`, OpenClaw's -// own env substitution, and the value lands in $OPENCLAW_STATE_DIR/.env (0600), -// the global env file OpenClaw loads on its own — so the route works with -// nothing exported. The default model is `freellmapi/` under -// `agents.defaults.model.primary`; the patch keeps any `fallbacks` already -// declared beside it. -function openclawStateDir(homeDir: string): string { - return process.env.OPENCLAW_STATE_DIR?.trim() - || path.join(process.env.OPENCLAW_HOME?.trim() || homeDir, '.openclaw'); -} - -function openclaw(ctx: GenerateContext): Generation { - const model = primaryModel(ctx.models, ctx.requestedModelId); - const stateDir = openclawStateDir(ctx.homeDir); - const configPath = process.env.OPENCLAW_CONFIG_PATH?.trim() - || path.join(stateDir, 'openclaw.json'); - // A provider id is referenced by every model ref (`/`) and - // by session state, so it is permanent; a named profile becomes a second - // provider beside the default one rather than replacing it. - const provider = ctx.profile === 'default' - ? 'freellmapi' - : `freellmapi-${ctx.profile.toLowerCase().replace(/[^a-z0-9]+/g, '-').replace(/^-+|-+$/g, '')}`; - const roster = catalogModels(ctx.models); - const entries = [model, ...roster.filter(entry => entry.id !== model.id)] - .map(entry => ({ - id: entry.id, - name: entry.name ?? entry.id, - reasoning: false, - input: ['text'], - cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 }, - contextWindow: contextWindow(entry), - maxTokens: outputLimit(entry), - })); - return { - files: [ - { - path: configPath, - format: 'json', - value: { - ...(ctx.profile === 'default' - ? { agents: { defaults: { model: { primary: `${provider}/${model.id}` } } } } - : {}), - models: { - providers: { - [provider]: { - baseUrl: v1Url(ctx.url), - apiKey: '${FREELLMAPI_API_KEY}', - api: 'openai-completions', - // OpenClaw attaches its own `openclaw/` identity only - // to endpoints it recognises; a custom origin gets the bare - // openai-node SDK user agent, which the gateway's analytics - // would file under "openai-sdk". A static provider header - // (verified to reach chat requests) names the client instead. - headers: { 'User-Agent': 'openclaw' }, - models: entries, - }, - }, - }, - }, - }, - { - path: path.join(stateDir, '.env'), - format: 'env', - sensitive: true, - content: `FREELLMAPI_API_KEY=${ctx.apiKey}\n`, - }, - ], - notes: [ - 'Install OpenClaw with: npm install -g openclaw@latest --allow-scripts=openclaw', - `Try it without the gateway daemon: openclaw agent exec --model ${provider}/${model.id} "Say hello"`, - ctx.profile === 'default' - ? `${provider}/${model.id} is the default model; a running \`openclaw gateway\` needs a restart to pick the change up.` - : `${provider}/${model.id} is in the model picker; the default model is unchanged.`, - `Models are declared text-only; give a vision model \`input: ["text", "image"]\` under models.providers.${provider} to send it images.`, - ], - }; -} - -// Hermes Agent (Nous Research) reads one YAML document, $HERMES_HOME/config.yaml -// (default ~/.hermes), and treats it as the single source of truth for the -// endpoint: OPENAI_BASE_URL is deliberately ignored for anything but -// api.openai.com, and OPENAI_API_KEY is only sent to OpenAI hosts. So the -// gateway goes into the `model` block as `provider: custom` with `base_url`, -// and the key travels as `api_key: "${FREELLMAPI_API_KEY}"` — Hermes's own -// substitution — with the value in $HERMES_HOME/.env (0600), the file Hermes -// loads on its own, so nothing needs exporting. A fresh install ships -// `model: ""` (a "not configured" sentinel); replacing it with the mapping is -// exactly what `hermes setup` would do, and it is what lets a headless first -// run skip the wizard. A named profile becomes an entry in the `providers` -// dict instead — picked with `/model custom::` — leaving the -// default model alone. -function hermes(ctx: GenerateContext): Generation { - const model = primaryModel(ctx.models, ctx.requestedModelId); - const home = process.env.HERMES_HOME?.trim() || path.join(ctx.homeDir, '.hermes'); - const provider = `freellmapi-${ctx.profile.toLowerCase().replace(/[^a-z0-9]+/g, '-').replace(/^-+|-+$/g, '')}`; - // Hermes only identifies itself to hosts it knows; a custom endpoint gets the - // bare openai-python user agent. `default_headers` is its documented way to - // override that, and it is what lets the gateway's analytics name the client. - const headers = { 'User-Agent': 'hermes-agent' }; - const value: Record = ctx.profile === 'default' - ? { - model: { - provider: 'custom', - default: model.id, - base_url: v1Url(ctx.url), - api_key: '${FREELLMAPI_API_KEY}', - api_mode: 'chat_completions', - context_length: contextWindow(model), - default_headers: headers, - // The wizard strips these when it writes api_key; a leftover would - // point the key lookup at a variable this route never sets. - key_env: undefined, - api_key_env: undefined, - }, - } - : { - providers: { - [provider]: { - name: `FreeLLMAPI (${ctx.profile})`, - api: v1Url(ctx.url), - api_key: '${FREELLMAPI_API_KEY}', - transport: 'chat_completions', - default_model: model.id, - context_length: contextWindow(model), - extra_headers: headers, - }, - }, - }; - return { - files: [ - { path: path.join(home, 'config.yaml'), format: 'yaml', value }, - { - path: path.join(home, '.env'), - format: 'env', - sensitive: true, - content: `FREELLMAPI_API_KEY=${ctx.apiKey}\n`, - }, - ], - notes: [ - 'Install Hermes Agent with: curl -fsSL https://hermes-agent.nousresearch.com/install.sh | bash -s -- --skip-setup', - ctx.profile === 'default' - ? `Try it with: hermes -z "Say hello" — custom/${model.id} is the default model.` - : `Pick it inside a chat with: /model custom:${provider}:${model.id} — the default model is unchanged.`, - 'A running `hermes gateway` (Telegram, Discord, â€Ļ) needs a restart to pick the change up.', - ], - }; -} - -// Pi (pi.dev, @earendil-works/pi-coding-agent) keeps user configuration in its -// agent directory, $PI_CODING_AGENT_DIR (default ~/.pi/agent). An endpoint Pi -// does not ship is a provider entry in models.json — `baseUrl`, `api` and the -// `models` it serves — and `api: openai-completions` is the adapter for a -// /v1/chat/completions backend. Pi does not load a .env file of its own, so the -// key is written literally (the file is created 0600); its `$NAME` form would -// need the variable exported in every shell that starts Pi. A provider entry -// alone only adds models to `/model`: the startup choice is `defaultProvider` -// plus `defaultModel` in settings.json, which a named profile leaves alone. -function piAgentDir(homeDir: string): string { - return process.env.PI_CODING_AGENT_DIR?.trim() || path.join(homeDir, '.pi', 'agent'); -} - -function pi(ctx: GenerateContext): Generation { - const model = primaryModel(ctx.models, ctx.requestedModelId); - const dir = piAgentDir(ctx.homeDir); - const provider = ctx.profile === 'default' - ? 'freellmapi' - : `freellmapi-${ctx.profile.toLowerCase().replace(/[^a-z0-9]+/g, '-').replace(/^-+|-+$/g, '')}`; - const roster = catalogModels(ctx.models); - const entries = [model, ...roster.filter(entry => entry.id !== model.id)] - .map(entry => ({ - id: entry.id, - name: entry.name ?? entry.id, - // The gateway picks the upstream per request, so no model can promise a - // thinking API; Pi then sends no reasoning effort at all. - reasoning: false, - input: ['text'], - cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 }, - contextWindow: contextWindow(entry), - maxTokens: outputLimit(entry), - })); - const files: Generation['files'] = [{ - path: path.join(dir, 'models.json'), - format: 'json', - sensitive: true, - value: { - providers: { - [provider]: { - name: ctx.profile === 'default' ? 'FreeLLMAPI' : `FreeLLMAPI (${ctx.profile})`, - baseUrl: v1Url(ctx.url), - api: 'openai-completions', - apiKey: ctx.apiKey, - // Pi names itself only to a few hosts it knows; a custom endpoint gets - // the bare openai-node SDK user agent, which the gateway's analytics - // would file under "openai-sdk". This is the UA Pi already sends to - // Cloudflare, so analytics can name the client. - headers: { 'User-Agent': 'pi-coding-agent' }, - models: entries, - }, - }, - }, - }]; - if (ctx.profile === 'default') { - files.push({ - path: path.join(dir, 'settings.json'), - format: 'json', - value: { defaultProvider: provider, defaultModel: model.id }, - }); - } - return { - files, - notes: [ - 'Install Pi with: npm install -g @earendil-works/pi-coding-agent (or curl -fsSL https://pi.dev/install.sh | sh)', - ctx.profile === 'default' - ? `Try it with: pi -p "Say hello" — ${provider}/${model.id} is the default model.` - : `Try it with: pi --model ${provider}/${model.id} -p "Say hello" — the default model is unchanged.`, - 'Every catalog model is in /model; a running Pi session picks the change up when /model is opened.', - `Models are declared text-only; give a vision model \`input: ["text", "image"]\` under providers.${provider} in models.json to send it images.`, - ], - }; -} - -// Reasonix (reasonix.io, esengine/DeepSeek-Reasonix) reads one global TOML -// document, /config.toml — ~/.reasonix, %APPDATA%\reasonix on -// Windows, or $REASONIX_HOME when set. Every endpoint is a `[[providers]]` -// entry; `kind = "openai"` posts to `/chat/completions`, and a -// `models` list exposes the whole catalog under one connection. The entry only -// names its key variable (`api_key_env`); Reasonix keeps the value in the .env -// beside config.toml (0600) and reads it on its own, so nothing needs -// exporting. `default_model` is `provider/model`. Reasonix already sends -// `User-Agent: Reasonix/` to every endpoint, so no header is needed. -function reasonixHome(homeDir: string): string { - if (process.env.REASONIX_HOME?.trim()) return process.env.REASONIX_HOME.trim(); - if (process.platform === 'win32') { - return path.join(process.env.APPDATA?.trim() || path.join(homeDir, 'AppData', 'Roaming'), 'reasonix'); - } - return path.join(homeDir, '.reasonix'); -} - -function reasonix(ctx: GenerateContext): Generation { - const model = primaryModel(ctx.models, ctx.requestedModelId); - const home = reasonixHome(ctx.homeDir); - // A provider name is what every `provider/model` ref and saved session points - // at, so a named profile becomes a second entry rather than replacing it. - const provider = ctx.profile === 'default' - ? 'freellmapi' - : `freellmapi-${ctx.profile.toLowerCase().replace(/[^a-z0-9]+/g, '-').replace(/^-+|-+$/g, '')}`; - const roster = catalogModels(ctx.models); - const models = [model, ...roster.filter(entry => entry.id !== model.id)].map(entry => entry.id); - return { - files: [ - { - path: path.join(home, 'config.toml'), - format: 'toml', - content: [ - ...(ctx.profile === 'default' ? [`default_model = ${JSON.stringify(`${provider}/${model.id}`)}`, ''] : []), - '[[providers]]', - `name = ${JSON.stringify(provider)}`, - `display_name = ${JSON.stringify(ctx.profile === 'default' ? 'FreeLLMAPI' : `FreeLLMAPI (${ctx.profile})`)}`, - 'kind = "openai"', - `base_url = ${JSON.stringify(v1Url(ctx.url))}`, - `models = [${models.map(id => JSON.stringify(id)).join(', ')}]`, - `default = ${JSON.stringify(model.id)}`, - 'api_key_env = "FREELLMAPI_API_KEY"', - `context_window = ${contextWindow(model)}`, - // Without a price Reasonix bills unknown models at a guessed rate; - // the gateway's models are free. - 'price = { cache_hit = 0, input = 0, output = 0, currency = "$" }', - ].join('\n'), - }, - { - path: path.join(home, '.env'), - format: 'env', - sensitive: true, - content: `FREELLMAPI_API_KEY=${ctx.apiKey}\n`, - }, - ], - notes: [ - 'Install Reasonix with: npm install -g reasonix (or brew install esengine/reasonix/reasonix)', - ctx.profile === 'default' - ? `Try it with: reasonix -p "Say hello" — ${provider}/${model.id} is the default model.` - : `Try it with: reasonix --model ${provider}/${model.id} -p "Say hello" — the default model is unchanged.`, - `Every catalog model is in /model as ${provider}/; other [[providers]] entries in config.toml are kept.`, - `Reasonix Studio lists only the providers in [desktop].provider_access when that list is set; add "${provider}" there, or under Settings → Model → Access.`, - ], - }; -} - -// ZCode (zcode.z.ai, Z.ai's coding agent). The Desktop app and the CLI share -// one provider registry, ~/.zcode/v2/provider_config.json (underscore), which -// ZCODE_PERSONAL_PROVIDER_CONFIG_FILE can relocate. A custom provider is an -// entry in config.providerConfigRules.providerRules with an `api-key` access -// block and an `openai-chat-completions` endpoint; each model gets a "smart" -// rule in modelConfigRules.providerModelRules that pins only its context and -// output limits. Those are arrays, so the merge replaces only the entries this -// provider owns and keeps every other provider and rule. ZCode has no -// environment-variable lookup for a custom provider's key, so the key lives in -// the file, which the writer keeps at 0600. Live checked against the ZCode -// runtime 0.16.9 (zcode-app-cli 3.15.3): ~/.zcode/v2/config.json and -// ~/.zcode/cli/config.json are legacy files it reads once, for migration. -function zcodeProviderFile(homeDir: string): string { - const override = process.env.ZCODE_PERSONAL_PROVIDER_CONFIG_FILE?.trim(); - if (override && path.isAbsolute(override)) return override; - return path.join(homeDir, '.zcode', 'v2', 'provider_config.json'); -} - -function zcode(ctx: GenerateContext): Generation { - const model = primaryModel(ctx.models, ctx.requestedModelId); - // Like every other generator, a named profile is a second provider entry so - // both routes coexist in ZCode's model picker. - const provider = ctx.profile === 'default' - ? 'freellmapi' - : `freellmapi-${ctx.profile.toLowerCase().replace(/[^a-z0-9]+/g, '-').replace(/^-+|-+$/g, '')}`; - const ids = [model, ...catalogModels(ctx.models).filter(entry => entry.id !== model.id)]; - return { - files: [{ - path: zcodeProviderFile(ctx.homeDir), - format: 'json', - sensitive: true, - arrays: { - 'config.providerConfigRules.providerRules': { ownedBy: { providerId: provider } }, - 'config.modelConfigRules.providerModelRules': { ownedBy: { providerId: provider } }, - 'config.modelConfigRules.manualProviderModelRules': { ownedBy: { providerId: provider } }, - 'config.providerOrder': { union: true }, - }, - value: { - schemaVersion: 1, - config: { - providerConfigRules: { - providerRules: [{ - providerId: provider, - templateId: null, - providerName: ctx.profile === 'default' ? 'FreeLLMAPI' : `FreeLLMAPI (${ctx.profile})`, - enabled: true, - config: { - group: 'standard-personal', - access: { type: 'api-key', apiKey: ctx.apiKey }, - api: { type: 'openai-chat-completions', baseUrl: v1Url(ctx.url) }, - personalModelIds: ids.map(entry => entry.id), - modelOrder: ids.map(entry => entry.id), - visibility: 'visible', - }, - }], - }, - modelConfigRules: { - providerModelRules: ids.map(entry => ({ - providerId: provider, - modelId: entry.id, - config: { - enabled: true, - properties: { contextWindow: contextWindow(entry) }, - optionSpecs: { - maxOutputTokens: { max: outputLimit(entry), map: '{"max_tokens": maxOutputTokens}' }, - }, - }, - })), - // Present but empty: ZCode expects both rule arrays, and the merge - // drops any manual rule this provider had so a model never sits in both. - manualProviderModelRules: [], - }, - providerOrder: [provider], - defaultModelSelection: { providerId: provider, modelId: model.id }, - }, - }, - }], - notes: [ - 'Install ZCode from https://zcode.z.ai (or the community terminal client: npm i -g zcode-app-cli).', - 'Quit ZCode Desktop before running this so it does not save its in-memory registry over the file.', - `${provider}/${model.id} is now the default model; pick another under ${provider} in the model picker (or /model in the CLI). Other providers are kept.`, - 'The API key lives in the provider file (written 0600): ZCode has no env lookup for custom providers.', - ], - }; -} - -function cursor(_ctx: GenerateContext): Generation { - return { - files: [], - notes: [ - 'Cursor sends model traffic through its cloud service, so localhost is not reachable from Cursor.', - `Expose the gateway through a trusted HTTPS tunnel, then open Cursor Settings → Models, enable Override OpenAI Base URL, and enter /v1.`, - 'Cursor only documents custom keys for standard chat models; custom base-URL overrides may not support Responses-based or built-in models.', - 'The override affects Cursor globally while enabled, so disable it before switching back to built-in providers.', - 'Use a separately revocable URL token if the client cannot set an Authorization header.', - ], - }; -} - -function atomcode(ctx: GenerateContext): Generation { - const model = primaryModel(ctx.models, ctx.requestedModelId); - const configDir = path.join(ctx.homeDir, '.atomcode'); - // AtomCode picks its provider by name: a root `default_provider` key names - // one `[providers.]` table, and `type = "openai"` is what makes that - // table speak the OpenAI-compatible wire. Root key first, table second, so - // mergeToml can slot the key above any tables already in the file. - return { - files: [{ - path: path.join(configDir, 'config.toml'), - format: 'toml', - sensitive: true, - content: [ - 'default_provider = "freellmapi"', - '', - '[providers.freellmapi]', - 'type = "openai"', - `base_url = ${JSON.stringify(v1Url(ctx.url))}`, - `api_key = ${JSON.stringify(ctx.apiKey)}`, - `model = ${JSON.stringify(model.id)}`, - `context_window = ${contextWindow(model)}`, - ].join('\n'), - }], - notes: [ - 'AtomCode reads ~/.atomcode/config.toml; other [providers.*] tables in it are kept.', - `default_provider now points at [providers.freellmapi] with ${model.id} as its model.`, - 'Point base_url at the unified /v1 endpoint; api_key is the unified key shown on the Agents page.', - 'One-shot check: atomcode -p "Say hello" (telemetry is on by default: atomcode telemetry disable).', - ], - }; -} - -function generic(ctx: GenerateContext): Generation { - const model = primaryModel(ctx.models, ctx.requestedModelId); - return { - files: [], - notes: [ - `OPENAI_BASE_URL=${v1Url(ctx.url)}`, - `OPENAI_API_KEY=${ctx.apiKey || ''}`, - `OPENAI_MODEL=${model.id}`, - `curl ${v1Url(ctx.url)}/chat/completions -H "Authorization: Bearer $OPENAI_API_KEY" -H "Content-Type: application/json" -d '{"model":"${model.id}","messages":[{"role":"user","content":"Hello"}]}'`, - ], - }; -} - -const metadata = [ - ['claude', 'Claude Code', 'code', 'file', 'Anthropic Messages', 'root', 'setup-claude', 'https://docs.anthropic.com/en/docs/claude-code', claude], - ['codex', 'Codex CLI', 'code', 'file', 'OpenAI Responses', '/v1', 'setup-codex', 'https://developers.openai.com/codex', codex], - ['cline', 'Cline', 'code', 'file', 'OpenAI Chat', '/v1', 'setup-cline', 'https://docs.cline.bot', cline], - ['continue', 'Continue', 'code', 'file', 'OpenAI Chat', '/v1', 'setup-continue', 'https://docs.continue.dev', continueDev], - ['aider', 'Aider', 'code', 'file', 'OpenAI Chat', '/v1', 'setup-aider', 'https://aider.chat/docs', aider], - ['opencode', 'OpenCode', 'code', 'file', 'OpenAI Chat', '/v1', 'setup-opencode', 'https://opencode.ai/docs', opencode], - ['goose', 'Goose', 'agent', 'file', 'OpenAI Chat', '/v1', 'setup-goose', 'https://block.github.io/goose', goose], - ['qwen', 'Qwen Code', 'code', 'file', 'OpenAI or Gemini', '/v1', 'setup-qwen', 'https://qwenlm.github.io/qwen-code-docs', qwen], - ['roo', 'Roo Code', 'code', 'file', 'OpenAI Chat', '/v1', 'setup-roo', 'https://docs.roocode.com', roo], - ['kilo', 'Kilo Code', 'code', 'file', 'OpenAI Chat', '/v1', 'setup-kilo', 'https://kilocode.ai/docs', kilo], - ['crush', 'Crush', 'code', 'file', 'OpenAI Chat', '/v1', 'setup-crush', 'https://github.com/charmbracelet/crush', crush], - ['dsh', 'DeepSeek Harness', 'agent', 'file', 'OpenAI Chat', '/v1', 'setup-dsh', 'https://github.com/deepseek-ai/deepseek-harness', dsh], - ['mimo', 'MiMo Code', 'code', 'file', 'OpenAI Chat', '/v1', 'setup-mimo', 'https://mimo.xiaomi.com/mimocode', mimo], - ['atomcode', 'AtomCode', 'code', 'file', 'OpenAI Chat', '/v1', 'setup-atomcode', 'https://atomcode.atomgit.com/docs/en/', atomcode], - ['openclaw', 'OpenClaw', 'agent', 'file', 'OpenAI Chat', '/v1', 'setup-openclaw', 'https://docs.openclaw.ai/gateway/config-tools/custom-providers', openclaw], - ['hermes', 'Hermes Agent', 'agent', 'file', 'OpenAI Chat', '/v1', 'setup-hermes', 'https://hermes-agent.nousresearch.com/docs/', hermes], - ['pi', 'Pi', 'code', 'file', 'OpenAI Chat', '/v1', 'setup-pi', 'https://pi.dev/docs/latest/models', pi], - ['reasonix', 'Reasonix', 'code', 'file', 'OpenAI Chat', '/v1', 'setup-reasonix', 'https://github.com/esengine/DeepSeek-Reasonix/blob/studio/docs/CLI.md#configure-providers', reasonix], - ['zcode', 'ZCode', 'code', 'file', 'OpenAI Chat', '/v1', 'setup-zcode', 'https://zcode.z.ai/en/docs/configuration', zcode], - ['cursor', 'Cursor', 'code', 'guide', 'OpenAI Chat', '/v1', 'setup-cursor', 'https://docs.cursor.com', cursor], - ['generic', 'Generic OpenAI client', 'agent', 'guide', 'OpenAI Chat', '/v1', 'setup-generic', 'https://github.com/tashfeenahmed/freellmapi', generic], -] as const; - -export const tools: ToolDefinition[] = metadata.map(([ - id, - name, - category, - configType, - protocol, - baseUrlSupport, - command, - docsUrl, - generate, -]) => ({ - id, - name, - category, - configType, - protocol, - baseUrlSupport, - command, - docsUrl, - generate, -})); - -export function getTool(id: string): ToolDefinition | undefined { - return tools.find(tool => tool.id === id); -} diff --git a/cli/src/types.ts b/cli/src/types.ts deleted file mode 100644 index 1a4723bdc4e5c5028fc3e8727a99b1c05da40518..0000000000000000000000000000000000000000 --- a/cli/src/types.ts +++ /dev/null @@ -1,59 +0,0 @@ -import type { DetectedClient } from './detect.js'; - -export interface CatalogModel { - id: string; - name?: string; - context_window?: number | null; - context_length?: number | null; - available?: boolean; -} - -export interface GeneratedFile { - path: string; - format: 'json' | 'toml' | 'yaml' | 'env'; - /** Structured patch, deep-merged into the existing document (json, yaml). */ - value?: Record; - /** Literal text, merged as a marked `# freellmapi:start/end` block (toml, yaml, env). */ - content?: string; - sensitive?: boolean; - /** json only: arrays merged item by item instead of replaced whole, keyed by - * the array's dotted path. `ownedBy` drops the existing items whose fields - * all match it, then appends the patch's items; `union` keeps the existing - * values and appends the patch's missing ones. */ - arrays?: Record; -} - -export type JsonArrayMerge = { ownedBy: Record } | { union: true }; - -export interface GenerateContext { - url: string; - apiKey: string; - profile: string; - models: CatalogModel[]; - homeDir: string; - /** An explicit `--model`. Overrides each generator's default-model - * heuristic; validated against the unfiltered catalog by the caller. */ - requestedModelId?: string; - /** Result of `detectChatGptClient()`. Injected by the CLI entry point; - * when the property is absent generators probe the real machine, which - * golden-output tests must avoid (the snapshot must not depend on the - * host's installed clients). */ - detectedClient?: DetectedClient; -} - -export interface Generation { - files: GeneratedFile[]; - notes: string[]; -} - -export interface ToolDefinition { - id: string; - name: string; - category: 'code' | 'agent'; - configType: 'env' | 'file' | 'guide'; - protocol: string; - baseUrlSupport: 'root' | '/v1'; - command: string; - docsUrl: string; - generate(ctx: GenerateContext): Generation; -} diff --git a/cli/tools.json b/cli/tools.json deleted file mode 100644 index 5d7fb1aae6e58f5eff764a0d5ac7999da13ff0fc..0000000000000000000000000000000000000000 --- a/cli/tools.json +++ /dev/null @@ -1,23 +0,0 @@ -[ - {"id":"claude","name":"Claude Code","category":"code","configType":"file","protocol":"Anthropic Messages","baseUrlSupport":"root","command":"setup-claude","docsUrl":"https://docs.anthropic.com/en/docs/claude-code"}, - {"id":"codex","name":"Codex CLI","category":"code","configType":"file","protocol":"OpenAI Responses","baseUrlSupport":"/v1","command":"setup-codex","docsUrl":"https://developers.openai.com/codex"}, - {"id":"cline","name":"Cline","category":"code","configType":"file","protocol":"OpenAI Chat","baseUrlSupport":"/v1","command":"setup-cline","docsUrl":"https://docs.cline.bot"}, - {"id":"continue","name":"Continue","category":"code","configType":"file","protocol":"OpenAI Chat","baseUrlSupport":"/v1","command":"setup-continue","docsUrl":"https://docs.continue.dev"}, - {"id":"aider","name":"Aider","category":"code","configType":"file","protocol":"OpenAI Chat","baseUrlSupport":"/v1","command":"setup-aider","docsUrl":"https://aider.chat/docs"}, - {"id":"opencode","name":"OpenCode","category":"code","configType":"file","protocol":"OpenAI Chat","baseUrlSupport":"/v1","command":"setup-opencode","docsUrl":"https://opencode.ai/docs"}, - {"id":"goose","name":"Goose","category":"agent","configType":"file","protocol":"OpenAI Chat","baseUrlSupport":"/v1","command":"setup-goose","docsUrl":"https://block.github.io/goose"}, - {"id":"qwen","name":"Qwen Code","category":"code","configType":"file","protocol":"OpenAI or Gemini","baseUrlSupport":"/v1","command":"setup-qwen","docsUrl":"https://qwenlm.github.io/qwen-code-docs"}, - {"id":"roo","name":"Roo Code","category":"code","configType":"file","protocol":"OpenAI Chat","baseUrlSupport":"/v1","command":"setup-roo","docsUrl":"https://docs.roocode.com"}, - {"id":"kilo","name":"Kilo Code","category":"code","configType":"file","protocol":"OpenAI Chat","baseUrlSupport":"/v1","command":"setup-kilo","docsUrl":"https://kilocode.ai/docs"}, - {"id":"crush","name":"Crush","category":"code","configType":"file","protocol":"OpenAI Chat","baseUrlSupport":"/v1","command":"setup-crush","docsUrl":"https://github.com/charmbracelet/crush"}, - {"id":"dsh","name":"DeepSeek Harness","category":"agent","configType":"file","protocol":"OpenAI Chat","baseUrlSupport":"/v1","command":"setup-dsh","docsUrl":"https://github.com/deepseek-ai/deepseek-harness"}, - {"id":"mimo","name":"MiMo Code","category":"code","configType":"file","protocol":"OpenAI Chat","baseUrlSupport":"/v1","command":"setup-mimo","docsUrl":"https://mimo.xiaomi.com/mimocode"}, - {"id":"atomcode","name":"AtomCode","category":"code","configType":"file","protocol":"OpenAI Chat","baseUrlSupport":"/v1","command":"setup-atomcode","docsUrl":"https://atomcode.atomgit.com/docs/en/"}, - {"id":"openclaw","name":"OpenClaw","category":"agent","configType":"file","protocol":"OpenAI Chat","baseUrlSupport":"/v1","command":"setup-openclaw","docsUrl":"https://docs.openclaw.ai/gateway/config-tools/custom-providers"}, - {"id":"hermes","name":"Hermes Agent","category":"agent","configType":"file","protocol":"OpenAI Chat","baseUrlSupport":"/v1","command":"setup-hermes","docsUrl":"https://hermes-agent.nousresearch.com/docs/"}, - {"id":"pi","name":"Pi","category":"code","configType":"file","protocol":"OpenAI Chat","baseUrlSupport":"/v1","command":"setup-pi","docsUrl":"https://pi.dev/docs/latest/models"}, - {"id":"reasonix","name":"Reasonix","category":"code","configType":"file","protocol":"OpenAI Chat","baseUrlSupport":"/v1","command":"setup-reasonix","docsUrl":"https://github.com/esengine/DeepSeek-Reasonix/blob/studio/docs/CLI.md#configure-providers"}, - {"id":"zcode","name":"ZCode","category":"code","configType":"file","protocol":"OpenAI Chat","baseUrlSupport":"/v1","command":"setup-zcode","docsUrl":"https://zcode.z.ai/en/docs/configuration"}, - {"id":"cursor","name":"Cursor","category":"code","configType":"guide","protocol":"OpenAI Chat","baseUrlSupport":"/v1","command":"setup-cursor","docsUrl":"https://docs.cursor.com"}, - {"id":"generic","name":"Generic OpenAI client","category":"agent","configType":"guide","protocol":"OpenAI Chat","baseUrlSupport":"/v1","command":"setup-generic","docsUrl":"https://github.com/tashfeenahmed/freellmapi"} -] diff --git a/cli/tsconfig.json b/cli/tsconfig.json deleted file mode 100644 index 9bc941e4a150db339089b8360e9c2fcd44b00a05..0000000000000000000000000000000000000000 --- a/cli/tsconfig.json +++ /dev/null @@ -1,17 +0,0 @@ -{ - "compilerOptions": { - "target": "ES2022", - "module": "NodeNext", - "moduleResolution": "NodeNext", - "outDir": "dist", - "rootDir": "src", - "strict": true, - "esModuleInterop": true, - "skipLibCheck": true, - "resolveJsonModule": true, - "declaration": true, - "types": ["node"] - }, - "include": ["src/**/*.ts"], - "exclude": ["src/**/*.test.ts", "src/**/__snapshots__/**"] -} diff --git a/client/.gitignore b/client/.gitignore deleted file mode 100644 index a547bf36d8d11a4f89c59c144f24795749086dd1..0000000000000000000000000000000000000000 --- a/client/.gitignore +++ /dev/null @@ -1,24 +0,0 @@ -# Logs -logs -*.log -npm-debug.log* -yarn-debug.log* -yarn-error.log* -pnpm-debug.log* -lerna-debug.log* - -node_modules -dist -dist-ssr -*.local - -# Editor directories and files -.vscode/* -!.vscode/extensions.json -.idea -.DS_Store -*.suo -*.ntvs* -*.njsproj -*.sln -*.sw? diff --git a/client/README.md b/client/README.md deleted file mode 100644 index 7dbf7ebf3b2a3d84ad526bc47810d1d211331b8b..0000000000000000000000000000000000000000 --- a/client/README.md +++ /dev/null @@ -1,73 +0,0 @@ -# React + TypeScript + Vite - -This template provides a minimal setup to get React working in Vite with HMR and some ESLint rules. - -Currently, two official plugins are available: - -- [@vitejs/plugin-react](https://github.com/vitejs/vite-plugin-react/blob/main/packages/plugin-react) uses [Oxc](https://oxc.rs) -- [@vitejs/plugin-react-swc](https://github.com/vitejs/vite-plugin-react/blob/main/packages/plugin-react-swc) uses [SWC](https://swc.rs/) - -## React Compiler - -The React Compiler is not enabled on this template because of its impact on dev & build performances. To add it, see [this documentation](https://react.dev/learn/react-compiler/installation). - -## Expanding the ESLint configuration - -If you are developing a production application, we recommend updating the configuration to enable type-aware lint rules: - -```js -export default defineConfig([ - globalIgnores(['dist']), - { - files: ['**/*.{ts,tsx}'], - extends: [ - // Other configs... - - // Remove tseslint.configs.recommended and replace with this - tseslint.configs.recommendedTypeChecked, - // Alternatively, use this for stricter rules - tseslint.configs.strictTypeChecked, - // Optionally, add this for stylistic rules - tseslint.configs.stylisticTypeChecked, - - // Other configs... - ], - languageOptions: { - parserOptions: { - project: ['./tsconfig.node.json', './tsconfig.app.json'], - tsconfigRootDir: import.meta.dirname, - }, - // other options... - }, - }, -]) -``` - -You can also install [eslint-plugin-react-x](https://github.com/Rel1cx/eslint-react/tree/main/packages/plugins/eslint-plugin-react-x) and [eslint-plugin-react-dom](https://github.com/Rel1cx/eslint-react/tree/main/packages/plugins/eslint-plugin-react-dom) for React-specific lint rules: - -```js -// eslint.config.js -import reactX from 'eslint-plugin-react-x' -import reactDom from 'eslint-plugin-react-dom' - -export default defineConfig([ - globalIgnores(['dist']), - { - files: ['**/*.{ts,tsx}'], - extends: [ - // Other configs... - // Enable lint rules for React - reactX.configs['recommended-typescript'], - // Enable lint rules for React DOM - reactDom.configs.recommended, - ], - languageOptions: { - parserOptions: { - project: ['./tsconfig.node.json', './tsconfig.app.json'], - tsconfigRootDir: import.meta.dirname, - }, - // other options... - }, - }, -]) -``` diff --git a/client/components.json b/client/components.json deleted file mode 100644 index 15addee81fadab3e2f43e23425168d57f800bd8f..0000000000000000000000000000000000000000 --- a/client/components.json +++ /dev/null @@ -1,25 +0,0 @@ -{ - "$schema": "https://ui.shadcn.com/schema.json", - "style": "base-nova", - "rsc": false, - "tsx": true, - "tailwind": { - "config": "", - "css": "src/index.css", - "baseColor": "neutral", - "cssVariables": true, - "prefix": "" - }, - "iconLibrary": "lucide", - "rtl": false, - "aliases": { - "components": "@/components", - "utils": "@/lib/utils", - "ui": "@/components/ui", - "lib": "@/lib", - "hooks": "@/hooks" - }, - "menuColor": "default", - "menuAccent": "subtle", - "registries": {} -} diff --git a/client/dev/mockApi.ts b/client/dev/mockApi.ts deleted file mode 100644 index a63e54179466cc7b501afb4a72f6887c100979b1..0000000000000000000000000000000000000000 --- a/client/dev/mockApi.ts +++ /dev/null @@ -1,152 +0,0 @@ -import type { Plugin } from 'vite' - -// Dev-only in-memory mock of the /api surface the Fallback page needs, so the -// frontend can be exercised without a running backend. Enabled only when the -// dev server is started with MOCK_API=1 (see vite.config.ts) — never bundled. - -const PRESETS: Record = { - balanced: { reliability: 0.5, speed: 0.25, intelligence: 0.25 }, - smartest: { reliability: 0.35, speed: 0.1, intelligence: 0.55 }, - fastest: { reliability: 0.35, speed: 0.55, intelligence: 0.1 }, - reliable: { reliability: 0.7, speed: 0.15, intelligence: 0.15 }, -} - -interface MockModel { - modelDbId: number - platform: string - modelId: string - displayName: string - sizeLabel: string - intelligenceRank: number - speedRank: number - monthlyTokenBudget: string - supportsVision: boolean - rpmLimit: number | null - rpdLimit: number | null - enabled: boolean - priority: number - reliability: number - speed: number - intelligence: number - headroom: number - rateLimit: number - totalRequests: number -} - -let strategy = 'balanced' -// User-tuned vector for the 'custom' strategy (kept normalized, like the server). -let customWeights = { ...PRESETS.balanced } - -function activeWeights() { - if (strategy === 'custom') return customWeights - return PRESETS[strategy] ?? PRESETS.balanced -} - -const models: MockModel[] = [ - { modelDbId: 1, platform: 'google', modelId: 'gemini-2.5-pro', displayName: 'Gemini 2.5 Pro', sizeLabel: 'Frontier', intelligenceRank: 1, speedRank: 8, monthlyTokenBudget: '~12M', supportsVision: true, rpmLimit: 5, rpdLimit: 100, enabled: true, priority: 1, reliability: 0.94, speed: 0.55, intelligence: 1.0, headroom: 1, rateLimit: 1, totalRequests: 412 }, - { modelDbId: 2, platform: 'groq', modelId: 'openai/gpt-oss-120b', displayName: 'GPT-OSS 120B', sizeLabel: 'Frontier', intelligenceRank: 6, speedRank: 2, monthlyTokenBudget: '~6M', supportsVision: false, rpmLimit: 30, rpdLimit: 1000, enabled: true, priority: 2, reliability: 0.9, speed: 0.98, intelligence: 0.72, headroom: 1, rateLimit: 0.7, totalRequests: 833 }, - { modelDbId: 3, platform: 'cerebras', modelId: 'llama-3.3-70b', displayName: 'Llama 3.3 70B', sizeLabel: 'Large', intelligenceRank: 4, speedRank: 1, monthlyTokenBudget: '~50M', supportsVision: false, rpmLimit: 30, rpdLimit: 14400, enabled: true, priority: 3, reliability: 0.88, speed: 1.0, intelligence: 0.5, headroom: 1, rateLimit: 1, totalRequests: 256 }, - { modelDbId: 4, platform: 'google', modelId: 'gemini-2.5-flash', displayName: 'Gemini 2.5 Flash', sizeLabel: 'Large', intelligenceRank: 4, speedRank: 5, monthlyTokenBudget: '~3M', supportsVision: true, rpmLimit: 10, rpdLimit: 20, enabled: true, priority: 4, reliability: 0.97, speed: 0.78, intelligence: 0.52, headroom: 0.45, rateLimit: 1, totalRequests: 90 }, - { modelDbId: 5, platform: 'nvidia', modelId: 'llama-4-scout', displayName: 'Llama 4 Scout', sizeLabel: 'Medium', intelligenceRank: 7, speedRank: 4, monthlyTokenBudget: '~30M', supportsVision: true, rpmLimit: 40, rpdLimit: null, enabled: true, priority: 5, reliability: 0.7, speed: 0.83, intelligence: 0.33, headroom: 1, rateLimit: 1, totalRequests: 47 }, - { modelDbId: 6, platform: 'openrouter', modelId: 'deepseek/deepseek-v3.1:free', displayName: 'DeepSeek V3.1', sizeLabel: 'Frontier', intelligenceRank: 2, speedRank: 10, monthlyTokenBudget: '~6M', supportsVision: false, rpmLimit: 20, rpdLimit: 200, enabled: false, priority: 6, reliability: 0.5, speed: 0.4, intelligence: 0.67, headroom: 1, rateLimit: 1, totalRequests: 0 }, - { modelDbId: 7, platform: 'mistral', modelId: 'mistral-large', displayName: 'Mistral Large', sizeLabel: 'Large', intelligenceRank: 5, speedRank: 6, monthlyTokenBudget: '~15M', supportsVision: false, rpmLimit: 60, rpdLimit: 500, enabled: true, priority: 7, reliability: 0.82, speed: 0.6, intelligence: 0.45, headroom: 1, rateLimit: 1, totalRequests: 178 }, -] - -function budgetTokens(label: string): number { - const m = label.match(/~?([\d.]+)(?:-([\d.]+))?([MK])?/) - if (!m) return 0 - const n = parseFloat(m[2] ?? m[1]) - const unit = m[3] === 'M' ? 1_000_000 : m[3] === 'K' ? 1_000 : 1 - return n * unit -} - -function score(m: MockModel): number { - const w = activeWeights() - const base = w.reliability * m.reliability + w.speed * m.speed + w.intelligence * m.intelligence - return base * m.headroom * m.rateLimit -} - -function readBody(req: any): Promise { - return new Promise(resolve => { - let data = '' - req.on('data', (c: any) => { data += c }) - req.on('end', () => { try { resolve(JSON.parse(data || '{}')) } catch { resolve({}) } }) - }) -} - -export function mockApiPlugin(): Plugin { - return { - name: 'freellmapi-dev-mock', - configureServer(server) { - server.middlewares.use(async (req: any, res: any, next: any) => { - const url = (req.url || '').split('?')[0] - if (!url.startsWith('/api/')) return next() - const send = (obj: any, status = 200) => { - res.statusCode = status - res.setHeader('Content-Type', 'application/json') - res.end(JSON.stringify(obj)) - } - - // Auth gate — always "logged in" in mock mode. - if (url === '/api/auth/status') return send({ needsSetup: false, authenticated: true, email: 'dev@local' }) - if (url === '/api/ping') return send({ status: 'ok', timestamp: new Date().toISOString() }) - - if (url === '/api/fallback' && req.method === 'GET') { - const rows = [...models].sort((a, b) => a.priority - b.priority).map(m => ({ - modelDbId: m.modelDbId, priority: m.priority, effectivePriority: m.priority, - penalty: m.rateLimit < 1 ? Math.round((1 - m.rateLimit) * 10) : 0, - rateLimitHits: m.rateLimit < 1 ? 3 : 0, - enabled: m.enabled, platform: m.platform, modelId: m.modelId, displayName: m.displayName, - intelligenceRank: m.intelligenceRank, speedRank: m.speedRank, sizeLabel: m.sizeLabel, - rpmLimit: m.rpmLimit, rpdLimit: m.rpdLimit, monthlyTokenBudget: m.monthlyTokenBudget, - supportsVision: m.supportsVision, keyCount: 3, - })) - return send(rows) - } - - if (url === '/api/fallback' && req.method === 'PUT') { - const body = await readBody(req) - for (const e of body as any[]) { - const m = models.find(x => x.modelDbId === e.modelDbId) - if (m) { m.priority = e.priority; m.enabled = e.enabled } - } - return send({ success: true }) - } - - if (url === '/api/fallback/routing' && req.method === 'GET') { - const scores = [...models] - .map(m => ({ - modelDbId: m.modelDbId, platform: m.platform, modelId: m.modelId, displayName: m.displayName, - enabled: m.enabled, reliability: m.reliability, speed: m.speed, intelligence: m.intelligence, - headroom: m.headroom, rateLimit: m.rateLimit, score: score(m), totalRequests: m.totalRequests, - })) - .sort((a, b) => b.score - a.score) - return send({ strategy, weights: strategy === 'priority' ? null : activeWeights(), customWeights, scores }) - } - - if (url === '/api/fallback/routing' && req.method === 'PUT') { - const body = await readBody(req) - if (typeof body.strategy === 'string') strategy = body.strategy - if (body.weights && typeof body.weights === 'object') { - const { reliability = 0, speed = 0, intelligence = 0 } = body.weights - const sum = reliability + speed + intelligence - if (sum > 0) { - customWeights = { reliability: reliability / sum, speed: speed / sum, intelligence: intelligence / sum } - } - } - return send({ strategy, presets: PRESETS, customWeights }) - } - - if (url === '/api/fallback/token-usage') { - const withKeys = models - const modelBudgets = withKeys.map(m => ({ displayName: m.displayName, platform: m.platform, budget: budgetTokens(m.monthlyTokenBudget) })) - const totalBudget = modelBudgets.reduce((s, m) => s + m.budget, 0) - return send({ totalBudget, totalUsed: Math.round(totalBudget * 0.18), models: modelBudgets }) - } - - // Anything else under /api → empty 200 so pages don't hard-crash. - return send({}) - }) - }, - } -} diff --git a/client/eslint.config.js b/client/eslint.config.js deleted file mode 100644 index 5e6b472f583e34a1cca751440d4f241495475723..0000000000000000000000000000000000000000 --- a/client/eslint.config.js +++ /dev/null @@ -1,23 +0,0 @@ -import js from '@eslint/js' -import globals from 'globals' -import reactHooks from 'eslint-plugin-react-hooks' -import reactRefresh from 'eslint-plugin-react-refresh' -import tseslint from 'typescript-eslint' -import { defineConfig, globalIgnores } from 'eslint/config' - -export default defineConfig([ - globalIgnores(['dist']), - { - files: ['**/*.{ts,tsx}'], - extends: [ - js.configs.recommended, - tseslint.configs.recommended, - reactHooks.configs.flat.recommended, - reactRefresh.configs.vite, - ], - languageOptions: { - ecmaVersion: 2020, - globals: globals.browser, - }, - }, -]) diff --git a/client/index.html b/client/index.html deleted file mode 100644 index 29deca02cbfcfaff283ad157d28f527ed2cd0464..0000000000000000000000000000000000000000 --- a/client/index.html +++ /dev/null @@ -1,36 +0,0 @@ - - - - - - - - FreeLLMAPI ¡ Unified LLM Router - - -
- - - diff --git a/client/package.json b/client/package.json deleted file mode 100644 index da376819571a1847cb473b80c08c574dd090046b..0000000000000000000000000000000000000000 --- a/client/package.json +++ /dev/null @@ -1,59 +0,0 @@ -{ - "name": "@freellmapi/client", - "private": true, - "version": "0.0.0", - "type": "module", - "scripts": { - "dev": "vite", - "build": "tsc -b && vite build", - "lint": "eslint .", - "test": "vitest run && npm run check:i18n", - "check:i18n": "node scripts/check-i18n.mjs", - "preview": "vite preview" - }, - "dependencies": { - "@base-ui/react": "^1.3.0", - "@dnd-kit/core": "^6.3.1", - "@dnd-kit/sortable": "^10.0.0", - "@dnd-kit/utilities": "^3.2.2", - "@fontsource-variable/geist": "^5.2.8", - "@fontsource-variable/geist-mono": "^5.2.7", - "@tailwindcss/vite": "^4.2.2", - "@tanstack/react-query": "^5.97.0", - "class-variance-authority": "^0.7.1", - "clsx": "^2.1.1", - "highlight.js": "^11.12.0", - "liquid-gooey": "^0.2.1", - "lucide-react": "^1.8.0", - "react": "^19.2.4", - "react-dom": "^19.2.4", - "react-markdown": "^10.1.0", - "react-router-dom": "^7.14.0", - "recharts": "^3.8.1", - "remark-gfm": "^4.0.1", - "shadcn": "^4.2.0", - "simple-icons": "^16.31.0", - "tailwind-merge": "^3.5.0", - "tailwindcss": "^4.2.2", - "tw-animate-css": "^1.4.0" - }, - "devDependencies": { - "@eslint/js": "^9.39.4", - "@types/node": "^24.12.2", - "@types/react": "^19.2.14", - "@types/react-dom": "^19.2.3", - "@vitejs/plugin-react": "^6.0.1", - "eslint": "^9.39.4", - "eslint-plugin-react-hooks": "^7.0.1", - "eslint-plugin-react-refresh": "^0.5.2", - "globals": "^17.4.0", - "typescript": "~6.0.2", - "typescript-eslint": "^8.58.0", - "vite": "^8.0.4", - "vitest": "^3.1.3" - }, - "engines": { - "node": ">=20.18.0 <25.0.0", - "npm": ">=10.0.0" - } -} diff --git a/client/public/favicon.svg b/client/public/favicon.svg deleted file mode 100644 index 78bbf8f8b1fc09475b3e61388eb9c356b3fc571f..0000000000000000000000000000000000000000 --- a/client/public/favicon.svg +++ /dev/null @@ -1,9 +0,0 @@ - - - - - - diff --git a/client/public/icons.svg b/client/public/icons.svg deleted file mode 100644 index e9522193d9f796a9748e9ad8c952a5df73c87db9..0000000000000000000000000000000000000000 --- a/client/public/icons.svg +++ /dev/null @@ -1,24 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/client/scripts/check-i18n.mjs b/client/scripts/check-i18n.mjs deleted file mode 100644 index b374b19d62159078254e0939ab9fedb9648a57e1..0000000000000000000000000000000000000000 --- a/client/scripts/check-i18n.mjs +++ /dev/null @@ -1,88 +0,0 @@ -import { readdir, readFile } from 'node:fs/promises' -import path from 'node:path' -import { fileURLToPath } from 'node:url' - -const localeDirectory = path.resolve( - path.dirname(fileURLToPath(import.meta.url)), - '../src/i18n/locales', -) -const expectedLocales = [ - 'en', 'zh-CN', 'es', 'fr', 'pt-BR', 'it', 'hi', 'ar', 'bn', 'ru', - 'ur', 'id', 'de', 'ja', 'sw', 'mr', 'te', 'tr', 'ta', 'vi', - 'ko', 'fa', 'th', 'gu', 'pl', 'uk', 'kn', 'ml', 'or', 'my', - 'pa', 'ro', 'nl', 'ms', 'tl', 'ha', 'yo', 'ig', 'am', 'uz', - 'az', 'si', 'ne', 'km', 'el', 'cs', 'hu', 'sv', 'he', 'da', - 'fi', 'no', 'sk', 'bg', 'hr', 'sr', 'lt', 'zh-TW', 'pt-PT', 'ka', -] - -function flatten(value, prefix = '', output = new Map()) { - if (value && typeof value === 'object' && !Array.isArray(value)) { - for (const [key, child] of Object.entries(value)) { - flatten(child, prefix ? `${prefix}.${key}` : key, output) - } - } else { - output.set(prefix, value) - } - return output -} - -function placeholders(value) { - if (typeof value !== 'string') return [] - return [...value.matchAll(/\{(\w+)\}/g)].map(match => match[1]).sort() -} - -const fileNames = (await readdir(localeDirectory)) - .filter(fileName => fileName.endsWith('.json')) - .sort() -const actualLocales = fileNames.map(fileName => fileName.slice(0, -5)) -const missingFiles = expectedLocales.filter(locale => !actualLocales.includes(locale)) -const unexpectedFiles = actualLocales.filter(locale => !expectedLocales.includes(locale)) -const errors = [] - -if (missingFiles.length) errors.push(`Missing locale files: ${missingFiles.join(', ')}`) -if (unexpectedFiles.length) errors.push(`Unexpected locale files: ${unexpectedFiles.join(', ')}`) - -const english = JSON.parse(await readFile(path.join(localeDirectory, 'en.json'), 'utf8')) -const englishEntries = flatten(english) -const englishKeys = new Set(englishEntries.keys()) - -for (const locale of actualLocales) { - const dictionary = JSON.parse( - await readFile(path.join(localeDirectory, `${locale}.json`), 'utf8'), - ) - const entries = flatten(dictionary) - const keys = new Set(entries.keys()) - const missingKeys = [...englishKeys].filter(key => !keys.has(key)) - const extraKeys = [...keys].filter(key => !englishKeys.has(key)) - - if (missingKeys.length) { - errors.push(`${locale}: missing keys: ${missingKeys.join(', ')}`) - } - if (extraKeys.length) { - errors.push(`${locale}: extra keys: ${extraKeys.join(', ')}`) - } - - for (const [key, englishValue] of englishEntries) { - if (!entries.has(key)) continue - const localizedValue = entries.get(key) - if (typeof localizedValue !== typeof englishValue) { - errors.push(`${locale}:${key}: expected ${typeof englishValue}, got ${typeof localizedValue}`) - continue - } - const expectedPlaceholders = placeholders(englishValue) - const actualPlaceholders = placeholders(localizedValue) - if (expectedPlaceholders.join(',') !== actualPlaceholders.join(',')) { - errors.push( - `${locale}:${key}: placeholders {${actualPlaceholders.join(', ')}} do not match {${expectedPlaceholders.join(', ')}}`, - ) - } - } -} - -if (errors.length) { - console.error(`i18n validation failed with ${errors.length} error(s):`) - for (const error of errors) console.error(`- ${error}`) - process.exitCode = 1 -} else { - console.log(`i18n validation passed for ${actualLocales.length} locales and ${englishKeys.size} keys`) -} diff --git a/client/src/App.test.ts b/client/src/App.test.ts deleted file mode 100644 index b4b375e34ca14a941e59c5017d885b1ebd52cf98..0000000000000000000000000000000000000000 --- a/client/src/App.test.ts +++ /dev/null @@ -1,38 +0,0 @@ -import { readFileSync } from 'node:fs' -import path from 'node:path' -import { fileURLToPath } from 'node:url' -import { describe, expect, it } from 'vitest' - -// The Playground only scrolls inside its transcript pane if every ancestor -// between and that pane carries a definite height. One unconditional -// `min-h-screen` on the shell breaks it: min-height is a floor, not a ceiling, -// so the shell grows with the chat and the document scrolls instead. The -// contract lives in a couple of class strings in App.tsx -// and regresses silently, so it is pinned here like the chain manager pins -// its routing. -const here = path.dirname(fileURLToPath(import.meta.url)) -const app = readFileSync(path.join(here, 'App.tsx'), 'utf8') -const playground = readFileSync(path.join(here, 'pages/PlaygroundPage.tsx'), 'utf8') - -describe('app shell heights', () => { - it('pins full-bleed routes to the viewport, padded routes to the document', () => { - // h-dvh gives the shell a definite height so the flex chain below can - // constrain the transcript; overflow-hidden stops the document itself - // from scrolling. Padded routes keep plain min-height and scroll as a - // document — clipping them under h-dvh would leave long pages with no - // scrollbar at all. - expect(app).toMatch(/fullBleed \? 'h-dvh overflow-hidden' : 'min-h-screen'/) - }) - - it('passes a definite height down the flex chain to the page', () => { - // Dropping min-h-0/flex-1 anywhere between the shell and the page lets - // the content size its ancestors again, which is the original bug by - // another route. - expect(app).toContain("'flex min-h-0 flex-1 flex-col'") - expect(playground).toContain('flex min-h-0 flex-1 overflow-hidden') - }) - - it('keeps /playground in the full-bleed set', () => { - expect(app).toMatch(/FULL_BLEED_ROUTES = new Set\(\[[^\]]*'\/playground'[^\]]*\]\)/) - }) -}) diff --git a/client/src/App.tsx b/client/src/App.tsx deleted file mode 100644 index d2b54d3435b5d67e4bb2e8c1e6111a8571ea3931..0000000000000000000000000000000000000000 --- a/client/src/App.tsx +++ /dev/null @@ -1,522 +0,0 @@ -import { useEffect, useState, type ReactNode } from 'react' -import { BrowserRouter, Routes, Route, Navigate, NavLink, Link, useLocation, useNavigate } from 'react-router-dom' -import { MutationCache, QueryClient, QueryClientProvider } from '@tanstack/react-query' -import { ChevronDown, KeyRound, LogOut, Menu, MoreHorizontal, RefreshCw, Search, Settings, Sparkles } from 'lucide-react' -import { buttonVariants } from '@/components/ui/button' -import { - DropdownMenu, - DropdownMenuContent, - DropdownMenuGroup, - DropdownMenuItem, - DropdownMenuSeparator, - DropdownMenuSub, - DropdownMenuSubContent, - DropdownMenuSubTrigger, - DropdownMenuTrigger, -} from '@/components/ui/dropdown-menu' -import { AuthGate, ChangeCredentialsModal } from '@/components/auth-gate' -import { CommandPalette } from '@/components/command-palette' -import { openCommandPalette } from '@/components/command-palette-state' -import { ErrorBoundary } from '@/components/error-boundary' -import { SettingsDialog } from '@/components/settings-dialog' -import { UpdatesDialog } from '@/components/updates-dialog' -import { OPEN_UPDATES_EVENT } from '@/lib/updates' -import { Toaster } from '@/components/toaster' -import { UpdateReminder } from '@/components/update-reminder' -import { NewBuildPrompt } from '@/components/new-build-prompt' -import { usePremium } from '@/hooks/use-premium' -import { I18nProvider, useI18n } from '@/i18n' -import { logout } from '@/lib/api' -import { toast } from '@/lib/toast' -import { ThemeProvider } from '@/theme' -import KeysPage from '@/pages/KeysPage' -import PlaygroundPage from '@/pages/PlaygroundPage' -import FallbackPage from '@/pages/FallbackPage' -import ModelDetailPage from '@/pages/ModelDetailPage' -import FusionPage from '@/pages/FusionPage' -import EmbeddingsPage from '@/pages/EmbeddingsPage' -import ImagePage from '@/pages/ImagePage' -import VideoPage from '@/pages/VideoPage' -import AudioPage from '@/pages/AudioPage' -import SearchPage from '@/pages/SearchPage' -import DecisionPage from '@/pages/DecisionPage' -import MediaDetailPage from '@/pages/MediaDetailPage' -import EmbeddingDetailPage from '@/pages/EmbeddingDetailPage' -import AnalyticsPage from '@/pages/AnalyticsPage' -import LogsPage from '@/pages/LogsPage' -import PremiumPage from '@/pages/PremiumPage' -import NotFoundPage from '@/pages/NotFoundPage' -import AgentsPage from '@/pages/AgentsPage' - -// Every failed mutation surfaces as an error toast, so no action fails -// silently. A page that already shows the failure inline can opt out with -// `meta: { silenceToast: true }` on the mutation. -const queryClient = new QueryClient({ - // staleTime dedupes the mount storm (#1047) and keeps page-to-page - // navigation off the network: the Models page alone mounts ~13 queries, - // several of them the same endpoint from different components, and - // staleTime 0 refetched every one of them on every navigation and window - // focus. Cached data renders immediately either way; this only decides - // whether a background refetch follows. Thirty seconds is shorter than any - // poller here (refetchInterval still fires on its own clock), and mutations - // invalidate explicitly, so nothing user-visible goes stale. - defaultOptions: { queries: { staleTime: 30_000 } }, - mutationCache: new MutationCache({ - onError: (error, _variables, _context, mutation) => { - if (mutation.meta?.silenceToast) return - toast.error(error instanceof Error ? error.message : String(error)) - }, - }), -}) - -const navItems = [ - { to: '/models', labelKey: 'nav.models' }, - { to: '/playground', labelKey: 'nav.playground' }, - { to: '/keys', labelKey: 'nav.keys' }, - { to: '/agents', labelKey: 'nav.agents' }, - { to: '/analytics', labelKey: 'nav.analytics' }, - { to: '/premium', labelKey: 'nav.premium' }, -] - -// The modality pages behind "Models"; surfaced in the nav dropdown and -// the mobile submenu so Fusion/Embeddings/Image/Audio are discoverable without -// first landing on the chat table. -const modelItems = [ - { to: '/models/chat', labelKey: 'models.chatModelsTab' }, - { to: '/models/image', labelKey: 'models.imageTab' }, - { to: '/models/video', labelKey: 'models.videoTab' }, - { to: '/models/audio', labelKey: 'models.audioTab' }, - { to: '/models/embeddings', labelKey: 'models.embeddingsTab' }, - { to: '/models/decision', labelKey: 'decision.tab' }, - { to: '/models/search', labelKey: 'search.tab' }, - { to: '/models/fusion', labelKey: 'models.fusionTab' }, -] - -// The pages that hang off "Analytics". Logs is reachable only from here — it is -// deliberately kept out of navItems so the top bar does not grow a seventh entry. -const analyticsItems = [ - { to: '/analytics', labelKey: 'nav.analytics' }, - { to: '/logs', labelKey: 'nav.logs' }, -] - -// Nav entries rendered as a split control: the label still navigates, and a -// chevron (desktop) / submenu (mobile) reveals the pages behind it. Keyed by the -// nav entry's `to` so both branches below stay one lookup, not two hardcoded -// special cases. -const navMenus: Record< - string, - { ariaKey: string; items: { to: string; labelKey: string }[]; isActive: (pathname: string) => boolean } -> = { - '/models': { - ariaKey: 'nav.modelsMenu', - items: modelItems, - isActive: (pathname) => pathname.startsWith('/models'), - }, - '/analytics': { - ariaKey: 'nav.analyticsMenu', - items: analyticsItems, - isActive: (pathname) => pathname.startsWith('/analytics') || pathname.startsWith('/logs'), - }, -} - -const isMac = typeof navigator !== 'undefined' && /mac/i.test(navigator.platform) - -function NavItem({ to, children }: { to: string; children: React.ReactNode }) { - return ( - - `relative text-sm px-1 py-4 transition-colors ${ - isActive - ? 'text-foreground after:absolute after:inset-x-0 after:-bottom-px after:h-px after:bg-foreground' - : 'text-muted-foreground hover:text-foreground' - }` - } - > - {children} - - ) -} - -function Brand() { - return ( - - - FreeLLMAPI - - ) -} - -// True when the dashboard runs inside the desktop shell (Electron preload -// sets this). The navbar then doubles as the window title bar: draggable, -// padded for the macOS traffic lights, and without the web-only Sign out. -const isDesktopApp = typeof window !== 'undefined' - && (window as Window & { __FREEAPI_DESKTOP__?: boolean }).__FREEAPI_DESKTOP__ === true - -const desktopPlatform = typeof window !== 'undefined' - ? (window as Window & { __FREEAPI_PLATFORM__?: string }).__FREEAPI_PLATFORM__ - : undefined - -const isMacDesktop = isDesktopApp && ( - desktopPlatform ? desktopPlatform === 'darwin' : (typeof navigator !== 'undefined' && /Macintosh|Mac OS X/.test(navigator.userAgent)) -) - -// The preload's own early classList.add can be lost (it may run before this -// document exists), so the client claims the class itself at module load — -// before the first React paint — keeping html.desktop CSS (transparent body, -// glass backdrop on macOS) reliable. -if (isDesktopApp) { - document.documentElement.classList.add('desktop') - if (isMacDesktop) { - document.documentElement.classList.add('desktop-mac') - } -} - -function AccountMenuItems({ - showUpgrade, - upgradeLabel, - settingsLabel, - updatesLabel, - signOutLabel, - changeEmailLabel, - changePasswordLabel, - onUpgrade, - onOpenSettings, - onOpenUpdates, - onChangeEmail, - onChangePassword, -}: { - showUpgrade: boolean - upgradeLabel: string - settingsLabel: string - updatesLabel: string - signOutLabel: string - changeEmailLabel: string - changePasswordLabel: string - onUpgrade: () => void - onOpenSettings: () => void - onOpenUpdates: () => void - onChangeEmail: () => void - onChangePassword: () => void -}) { - return ( - <> - {showUpgrade && ( - - - {upgradeLabel} - - )} - - - {settingsLabel} - - - - {updatesLabel} - - {/* Desktop signs in with a hidden local account, so it has no credentials - to change and no session to end. */} - {!isDesktopApp && ( - <> - - - @ - {changeEmailLabel} - - - - {changePasswordLabel} - - logout()}> - - {signOutLabel} - - - )} - - ) -} - -function Navbar() { - const { t } = useI18n() - const location = useLocation() - const navigate = useNavigate() - const [settingsOpen, setSettingsOpen] = useState(false) - const [updatesOpen, setUpdatesOpen] = useState(false) - // The update pill lives outside the navbar; it asks for this dialog by event. - useEffect(() => { - const open = () => setUpdatesOpen(true) - window.addEventListener(OPEN_UPDATES_EVENT, open) - return () => window.removeEventListener(OPEN_UPDATES_EVENT, open) - }, []) - const [credentialsMode, setCredentialsMode] = useState<'password' | 'email' | null>(null) - const { data: premium, licensed, isLoading: premiumLoading, isError: premiumError } = usePremium() - const showUpgrade = Boolean(premium) && !licensed && !premiumLoading && !premiumError - - return ( - <> -
-
- - -
- - - - - - - navigate('/premium')} - onOpenSettings={() => setSettingsOpen(true)} - onOpenUpdates={() => setUpdatesOpen(true)} - onChangeEmail={() => setCredentialsMode('email')} - onChangePassword={() => setCredentialsMode('password')} - /> - - -
-
- - - - - - - {navItems.map((item) => { - const menu = navMenus[item.to] - return menu ? ( - - - {t(item.labelKey)} - - - {menu.items.map((entry) => ( - navigate(entry.to)}> - {t(entry.labelKey)} - - ))} - - - ) : ( - navigate(item.to)} - className={location.pathname === item.to ? 'bg-accent text-accent-foreground font-medium' : undefined} - > - {t(item.labelKey)} - - ) - })} - - - navigate('/premium')} - onOpenSettings={() => setSettingsOpen(true)} - onOpenUpdates={() => setUpdatesOpen(true)} - onChangeEmail={() => setCredentialsMode('email')} - onChangePassword={() => setCredentialsMode('password')} - /> - - -
-
-
- - - {credentialsMode && ( - setCredentialsMode(null)} /> - )} - - ) -} - -// Keyed by pathname so navigating away from a crashed page resets the boundary. -function PageBoundary({ children }: { children: ReactNode }) { - const location = useLocation() - return {children} -} - -// Routes that own the whole viewport instead of sitting in the shell's centred, -// padded column. Only the Playground so far: its three columns run edge to edge -// and the transcript scrolls inside its own pane, so the page must be exactly -// as tall as what is left under the navbar — not a centred card with margins. -const FULL_BLEED_ROUTES = new Set(['/playground']) - -// The shell's content container. A full-bleed route drops the max-width and the -// padding and becomes a flex child that fills the rest of the screen; every -// other route gets a centred column that is always exactly max-w-6xl wide. -// -// `w-full` is load-bearing, not decoration. This
is an item of a COLUMN -// flex container, so its cross axis is the horizontal one — and flexbox only -// stretches an item across the cross axis when neither cross-axis margin is -// auto (CSS Flexbox §9.6). `mx-auto` sets both, so without an explicit width -// the column shrink-to-fits its content instead: the page is only ever as wide -// as the widest thing that has finished rendering. On a page that fills in from -// several independent queries — Analytics fires ten — that turns every arriving -// response into a visible horizontal jump as the column re-fits, and pages -// whose content never reaches 72rem (Analytics, Premium) settle narrower than -// they were designed to be. A definite width makes the column 72rem from the -// first paint, and `mx-auto` goes back to only centring it. -function PageContainer({ children }: { children: ReactNode }) { - const location = useLocation() - const fullBleed = FULL_BLEED_ROUTES.has(location.pathname) - return ( -
- {children} -
- ) -} - -// The shell column. Padded routes keep `min-h-screen` and scroll as a document; -// a full-bleed route pins the shell to the dynamic viewport instead, because -// min-height alone is a floor, not a ceiling: with an indefinite shell height -// every flex-1 container below grows with its content and the document scrolls -// rather than the one pane (the Playground transcript) that means to. -function AppShell({ children }: { children: ReactNode }) { - const location = useLocation() - const fullBleed = FULL_BLEED_ROUTES.has(location.pathname) - return ( -
- {children} -
- ) -} - -function App() { - return ( - - - - - - {/* Column, so a full-bleed route can claim the height the navbar - leaves without anyone having to know how tall the navbar is. - Fixed-position children (toaster, palette, reminder) are out of - flow, and a padded route stretches to nothing it can show. */} - - - - - - } /> - } /> - } /> - } /> - } /> - } /> - } /> - } /> - } /> - } /> - } /> - } /> - } /> - } /> - } /> - } /> - } /> - } /> - } /> - } /> - } /> - } /> - } /> - } /> - } /> - } /> - - - - - - - - - - - - - - ) -} - -export default App diff --git a/client/src/assets/vite.svg b/client/src/assets/vite.svg deleted file mode 100644 index 5101b674df391399da71c767aa5c976426c9dc7a..0000000000000000000000000000000000000000 --- a/client/src/assets/vite.svg +++ /dev/null @@ -1 +0,0 @@ -Vite diff --git a/client/src/components/agent-icons.tsx b/client/src/components/agent-icons.tsx deleted file mode 100644 index 8e648c83b0beaa40a14fd1aa5976553d1785e36a..0000000000000000000000000000000000000000 --- a/client/src/components/agent-icons.tsx +++ /dev/null @@ -1,391 +0,0 @@ -import type { ReactNode, SVGProps } from 'react' -import { cn } from '@/lib/utils' - -// Brand marks for the tools listed on the Agents page. Every mark below is the -// tool's own published logo, taken verbatim from the source in the table and -// changed in only two ways: it is scaled and centred onto the shared 24px grid -// used by the rest of the icons, and it is painted in that brand's own colour -// for the tile it sits on. No mark is redrawn, recomposed or restyled. -// -// The marks identify tools this server is compatible with. They remain the -// trademarks of their respective owners and imply no affiliation or endorsement. -// -// COLOUR RULE — one rule for the whole set, applied in this order: -// 1. Use the brand's own artwork colour. Where a brand publishes separate -// artwork for light and dark backgrounds (most of these do) the matching -// variant is used verbatim on each tile — that is the `published pair` -// treatment below. -// 2. Where a brand publishes one value only and it falls under 3:1 against -// the tile it has to sit on, that value is moved in OKLCH lightness only: -// hue is held exactly, chroma is held and clamped to the sRGB gamut, and -// lightness stops at the first step that clears 3:1. The mark still reads -// as that brand's colour. Only claude, aider, kilo and reasonix need this, -// and only on one tile each; the untouched brand hex is kept on the other -// tile. -// 3. `generic` is not a brand — it stays `currentColor`. -// -// Contrast is measured against the tile itself, `bg-muted`, which resolves to -// #F5F5F5 in light and #1B1B1B in dark. Ratios are in the second table. -// -// id tool source licence -// claude Claude Code simple-icons `claude` (claude.ai; the code.claude.com mark) CC0-1.0 -// codex Codex CLI developers.openai.com/codex site header (OpenAI's own asset) OpenAI TM, nominative use -// cline Cline simple-icons `cline`; colours from cline.bot/assets/branding/ CC0-1.0 + Cline TM -// logos/cline-wordmark-{black,white}.svg -// continue Continue continuedev/continue `docs/logo/{light,dark}.svg` Apache-2.0 -// aider Aider Aider-AI/aider `aider/website/assets/icons/safari-pinned-tab.svg`; Apache-2.0 -// colours sampled from `android-chrome-192x192.png` (only raster) -// opencode OpenCode anomalyco/opencode `packages/identity/mark{,-light}.svg` MIT -// goose Goose block/goose `documentation/static/img/goose.svg`, with the Apache-2.0 -// published `goose-logo-white.png` as the dark variant -// qwen Qwen Code simple-icons `qwen` (qwen.ai) CC0-1.0 -// roo Roo Code RooCodeInc/Roo-Code `src/assets/icons/icon.svg`; colours from Apache-2.0 -// docs `img/roo-code-logo-{dark,white}.svg` -// kilo Kilo Code Kilo-Org/kilocode `packages/kilo-console/public/kilo-logo.svg` MIT -// cursor Cursor cursor.com/brand asset pack, `Cube/SVG/CUBE_2D_{LIGHT,DARK}.svg` Cursor TM, nominative use -// crush Crush no vector published; hue sampled from the official Charm TM, nominative use -// stuff.charm.sh/crush/charm-crush.png (Charm's `charple`) -// dsh DeepSeek lettermark only: DSH's brand guidelines ask third parties not DeepSeek TM, nominative use -// Harness to reuse official artwork; tinted with DeepSeek's brand blue -// mimo MiMo Code lettermark only: no vector published, only the 32px favicon Xiaomi TM, nominative use -// at cdn.cnbj1.fds.api.mi-img.com/.../mimo.ico, which is flat black -// atomcode AtomCode lettermark only: no vector published; hue sampled from the AtomGit TM, nominative use -// app icon cdn-news.gitcode.com/news/atomcode-icon1.png (the -// gradient's dominant blue, at two of its four corners) -// openclaw OpenClaw openclaw.ai/favicon.svg (the lobster mark: body, claws and MIT (openclaw/openclaw) -// antennae); the eyes are dropped with the gradient, as every -// mark here is painted flat in one brand colour -// hermes Hermes Agent lettermark only: the published mark (NousResearch/hermes-agent Nous Research TM, nominative use -// website/static/img/logo.png) is a raster portrait and the SVG -// favicon is a text glyph, so neither fits the 24px grid -// pi Pi pi.dev/favicon.svg (the monochrome mark; the press kit's Earendil TM, nominative use -// three-colour logo is the same geometry) -// reasonix Reasonix esengine/DeepSeek-Reasonix `docs/logo.svg` (the ◈ brand mark: MIT -// three concentric diamonds, without the wordmark) -// -// id light tile ratio dark tile ratio dark-mode treatment -// claude #D37152 3.08 #D97757 5.52 brand #D97757 on dark; light tile is -// the same hue tuned down (rule 2, 2.86→3.08) -// codex #000000 19.26 #FFFFFF 17.22 published pair (OpenAI paints the mark in -// currentColor: black on light, white on dark) -// cline #18181B 16.25 #FAFAFA 16.50 published pair (black/white wordmarks) -// continue #000000 19.26 #FFFFFF 17.22 published pair (logo/light.svg, logo/dark.svg) -// aider #07A160 3.07 #02F191 11.48 brand green on dark; light tile is the same -// hue tuned down (rule 2, 1.38→3.07) -// opencode #17181C + #E6E5E6 16.27 #FFFFFF + #5A5858 17.22 published pair; the second value is the -// mark's own inner block, a depth fill that is -// deliberately near-invisible in the brand's -// own artwork too (1.15 / 2.44) — the frame -// carries the legibility -// goose #101010 17.45 #FFFFFF 17.22 published pair (black mark / white logo) -// qwen #6950EF 4.82 #6950EF 3.28 brand hex unchanged; it clears 3:1 on both -// tiles, so no tuning is applied -// roo #000000 19.26 #FFFFFF 17.22 published pair (logo-dark.svg/logo-white.svg) -// kilo #949107 3.05 #FAF74F 15.16 brand yellow on dark; light tile is the same -// hue tuned down (rule 2, 1.04→3.05) — #FAF74F -// is oklch(0.95 0.18 108.4) and the tuned value -// is oklch(0.638 0.136 108.4), the most -// saturated yellow that clears 3:1 there -// cursor #26251E 14.10 #EDECEC 14.61 published pair (CUBE_2D_LIGHT/DARK) -// crush #6B50FF 4.57 #6B50FF 3.46 brand purple unchanged; clears on both tiles -// dsh #4D6BFE 3.97 #4D6BFE 3.98 brand blue unchanged; clears on both tiles -// mimo #000000 19.26 #FFFFFF 17.22 published pair (the favicon is black artwork on -// transparent, so it inverts on the dark tile) -// atomcode #3C82F6 3.37 #3C82F6 4.69 icon blue unchanged; clears on both tiles -// openclaw #991B1B 7.62 #FF4D4D 5.27 published pair: the mark is a gradient from -// #FF4D4D to #991B1B, and each tile takes the -// stop that clears on it (the light stop is -// 3.00 on the light tile, right at the line) -// hermes #000000 19.26 #FFFFFF 17.22 published pair (the logo is flat black artwork -// on white, so it inverts on the dark tile) -// pi #111111 17.32 #F6F6F6 15.94 published pair (the favicon's own light and -// dark-scheme fills) -// reasonix #6191C6 3.02 #93C5FD 9.55 the mark's gradient midpoint #93C5FD on dark; -// light tile is the same hue tuned down (rule 2, -// 1.65→3.02): oklch(0.809 0.096 251.8) → -// oklch(0.645 0.096 251.8) -// generic currentColor — currentColor — not a brand mark -// -// Cursor also publishes a 2.5D cube in five warm greys (#43413c #55544f #72716d -// #d6d5d2 #fff). It is not used: at 20px the facets stop resolving, and its -// darkest and lightest faces drop out on the dark and light tile respectively. -// The 2D cube is an official mark in its own right, not a flattening of that one. -// -// OpenCode's mark is the only two-tone one. Its inner block was dropped while -// the set was monochrome — it would have merged with the frame — and is restored -// here from the same published artwork, so the mark keeps both of its layers. -// -// Aider's and Kilo Code's logos are published as light artwork knocked out of a -// dark plate; both are used here as the plain letterform the plate carries, which -// is the same geometry with the background dropped for the colour swap. -// -// Crush has no published vector mark (charmbracelet/crush ships the icon as a -// PNG only), so it keeps the lettermark fallback below rather than an invented -// symbol; it is tinted with Charm's own purple so it sits in the coloured row. -// `generic` is not a brand: it is our own terminal glyph, on the same lucide grid -// and weight as the icons used elsewhere in the app. - -type Brand = { - // Tailwind arbitrary properties set `--mk` (and `--mk2` for the one two-tone - // mark) on the tile. The `dark:` half swaps in the dark-tile value, so the - // whole colour scheme is CSS — no theme hook, no flash on first paint. - tint: string - // Optical size correction, applied about the centre on top of the geometry - // already fitted to the grid; omitted means the mark is used at its fitted - // size. A bright saturated fill blooms next to a near-black or white one, so - // aider's #02F191 and kilo's #FAF74F — the two most luminous colours in the - // set, both on blocky marks — are pulled in slightly. Measured on the - // rendered tile as perceptual ink coverage, both sat ~1.5 points above the - // set's median and land on it at these values. Nothing is scaled up: every - // other mark already fills 21–24 of the 24-unit grid, so there is no room. - scale?: number - art?: ReactNode -} - -const brands: Record = { - claude: { - tint: '[--mk:#D37152] dark:[--mk:#D97757]', - art: ( - - ), - }, - codex: { - tint: '[--mk:#000000] dark:[--mk:#FFFFFF]', - art: ( - - ), - }, - cline: { - tint: '[--mk:#18181B] dark:[--mk:#FAFAFA]', - art: ( - - ), - }, - continue: { - tint: '[--mk:#000000] dark:[--mk:#FFFFFF]', - art: ( - - ), - }, - aider: { - tint: '[--mk:#07A160] dark:[--mk:#02F191]', - scale: 0.96, - art: ( - - ), - }, - opencode: { - tint: '[--mk:#17181C] [--mk2:#E6E5E6] dark:[--mk:#FFFFFF] dark:[--mk2:#5A5858]', - art: ( - <> - - - - ), - }, - goose: { - tint: '[--mk:#101010] dark:[--mk:#FFFFFF]', - art: ( - - ), - }, - qwen: { - tint: '[--mk:#6950EF] dark:[--mk:#6950EF]', - art: ( - - ), - }, - roo: { - tint: '[--mk:#000000] dark:[--mk:#FFFFFF]', - art: ( - - ), - }, - kilo: { - tint: '[--mk:#949107] dark:[--mk:#FAF74F]', - scale: 0.95, - art: ( - - ), - }, - cursor: { - tint: '[--mk:#26251E] dark:[--mk:#EDECEC]', - art: ( - - ), - }, - // No vector mark exists, so the lettermark fallback stands in — tinted with - // Charm's own purple so the tile still carries the brand. - crush: { - tint: '[--mk:#6B50FF] dark:[--mk:#6B50FF]', - }, - // DeepSeek Harness asks third parties not to reuse its official artwork - // (BRAND_GUIDELINES.md), so the lettermark stands in, in DeepSeek's blue. - dsh: { - tint: '[--mk:#4D6BFE] dark:[--mk:#4D6BFE]', - }, - // Xiaomi publishes no vector mark for MiMo Code — the only artwork on - // mimo.xiaomi.com is the 32px favicon — so the lettermark stands in. That - // favicon is drawn in flat black on transparent, which is the published - // pair treatment: black on the light tile, white on the dark one. - mimo: { - tint: '[--mk:#000000] dark:[--mk:#FFFFFF]', - }, - // AtomGit publishes AtomCode's icon only as a raster (the app icon the site - // header uses: an atom orbit on a cyan-to-violet gradient), so the lettermark - // stands in. It is tinted with the gradient's dominant blue, sampled from - // that PNG, which clears 3:1 on both tiles as published. - atomcode: { - tint: '[--mk:#3C82F6] dark:[--mk:#3C82F6]', - }, - // openclaw.ai/favicon.svg, drawn on a 120-unit box: the body and the two - // claws are filled paths, the antennae stroked. Scaled 0.2 and centred onto - // the 24 grid. The published mark paints the shell in a red gradient with - // dark eyes; here the shell is one flat brand red, like every other mark. - openclaw: { - tint: '[--mk:#991B1B] dark:[--mk:#FF4D4D]', - art: ( - - - - - - - - ), - }, - // Nous Research publishes the Hermes Agent mark only as a raster portrait, - // so the lettermark stands in. The artwork is flat black on white, which is - // the published pair treatment: black on the light tile, white on the dark. - hermes: { - tint: '[--mk:#000000] dark:[--mk:#FFFFFF]', - }, - // pi.dev/favicon.svg, drawn on a 560-unit box with three solid blocks. - // Scaled 0.0375 and centred, so it spans 21 units: a mark that fills its - // whole square reads heavier than the set's outlined marks at full size. - pi: { - tint: '[--mk:#111111] dark:[--mk:#F6F6F6]', - art: ( - - - - - - ), - }, - // esengine/DeepSeek-Reasonix docs/logo.svg: the ◈ mark is three concentric - // diamonds on a 92-unit box — two stroked, the inner one filled. Scaled - // 0.21 about the centre, so it spans 19.3 units. The published artwork - // pulses the strokes between 3.5 and 5.5; the peak width is used so the - // outlines survive at 20px. The cyan-to-violet gradient becomes one flat - // colour, its midpoint stop. - reasonix: { - tint: '[--mk:#6191C6] dark:[--mk:#93C5FD]', - art: ( - - - - - - ), - }, - // ZCode (Z.ai): its app icon is a white Z on a near-black rounded square, - // drawn here as the square with the Z knocked out so it reads on both tiles. - zcode: { - tint: '[--mk:#2B2B2B] dark:[--mk:#E5E5E5]', - art: ( - - ), - }, - // Any other OpenAI-compatible client: our own terminal glyph, not a brand - // mark, so it keeps the page foreground colour on both tiles. - generic: { - tint: '', - art: ( - - - - - - ), - }, -} - -function Mark({ - scale, - children, - ...props -}: SVGProps & { scale?: number; children: ReactNode }) { - return ( - // `--mk` is set by the tile below; the fallback keeps an untinted mark on - // the page foreground colour rather than leaving it unpainted. - - ) -} - -// Letters for a tool with no mark yet, so a catalog entry added later still -// gets a badge of the same size and weight instead of an empty tile. -function lettermark(name: string): string { - return name.replace(/[^A-Za-z0-9]/g, '').slice(0, 2).toUpperCase() -} - -export function AgentIcon({ - id, - name, - className, -}: { - id: string - name: string - className?: string -}) { - const brand = brands[id] - return ( - - ) -} diff --git a/client/src/components/api-usage.tsx b/client/src/components/api-usage.tsx deleted file mode 100644 index 09a380c53fde18d58ce8d67acdb7a95f909625e4..0000000000000000000000000000000000000000 --- a/client/src/components/api-usage.tsx +++ /dev/null @@ -1,26 +0,0 @@ -import { CopyButton } from '@/components/copy-button' -import { useI18n } from '@/i18n' - -// The /v1 base URL for ready-to-run snippets, derived the same way as the chat -// model page + Keys page: the dev server port in DEV, the page origin in a -// packaged/hosted build. -export function apiBaseUrl(): string { - return import.meta.env.DEV - ? `http://${window.location.hostname}:${__SERVER_PORT__}/v1` - : `${window.location.origin}/v1` -} - -// A copy-able "ways to use the API" code block, matching the chat detail page's -// snippet card so every modality's detail page looks the same. -export function ApiUsageBlock({ snippet }: { snippet: string }) { - const { t } = useI18n() - return ( -
-
- - {t('models.codeSnippetHeading')} -
-
{snippet}
-
- ) -} diff --git a/client/src/components/auth-gate.desktop-browser.test.tsx b/client/src/components/auth-gate.desktop-browser.test.tsx deleted file mode 100644 index 4eb2f254d1fa01ad3193536a39ad1285704ea86c..0000000000000000000000000000000000000000 --- a/client/src/components/auth-gate.desktop-browser.test.tsx +++ /dev/null @@ -1,93 +0,0 @@ -// @vitest-environment jsdom -// -// #1399: the desktop app's dashboard opened in a normal browser tab has no -// preload bridge. When the server reports `desktop: true` (a desktop install, -// a request from this computer) the gate fetches a session from -// /api/auth/desktop-session and never shows the Sign In form. Without the flag -// (web and Docker installs) the form stays. -import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest' -import { act } from 'react' -import { createRoot, type Root } from 'react-dom/client' -import { QueryClient, QueryClientProvider } from '@tanstack/react-query' -import { I18nProvider } from '@/i18n' -import { AuthGate } from './auth-gate' - -const MINTED = 'minted-desktop-session' - -let root: Root -let container: HTMLDivElement -let desktopFlag: boolean -let sessionCalls: number - -function json(body: unknown, status = 200): Response { - return new Response(JSON.stringify(body), { status, headers: { 'Content-Type': 'application/json' } }) -} - -async function flush() { - for (let i = 0; i < 6; i++) { - await act(async () => { await new Promise(r => setTimeout(r, 0)) }) - } -} - -function mount() { - const client = new QueryClient({ defaultOptions: { queries: { retry: false } } }) - act(() => { - root.render( - - -
dashboard
-
-
, - ) - }) -} - -beforeAll(() => { - ;(globalThis as unknown as { IS_REACT_ACT_ENVIRONMENT: boolean }).IS_REACT_ACT_ENVIRONMENT = true -}) - -beforeEach(() => { - container = document.createElement('div') - document.body.appendChild(container) - root = createRoot(container) - desktopFlag = true - sessionCalls = 0 - vi.stubGlobal('fetch', vi.fn(async (input: RequestInfo | URL, init?: RequestInit) => { - const url = String(input) - if (url.endsWith('/api/auth/desktop-session')) { - sessionCalls++ - return desktopFlag ? json({ token: MINTED }) : json({ error: { message: 'forbidden' } }, 403) - } - if (url.endsWith('/api/auth/status')) { - const ok = new Headers(init?.headers).get('Authorization') === `Bearer ${MINTED}` - return json({ needsSetup: false, authenticated: ok, email: ok ? 'desktop@localhost' : null, desktop: desktopFlag }) - } - return json({ error: { message: `unexpected ${url}` } }, 404) - })) -}) - -afterEach(() => { - act(() => root.unmount()) - container.remove() - localStorage.clear() - vi.unstubAllGlobals() -}) - -describe('AuthGate in a browser tab on a desktop install (#1399)', () => { - it('signs in through the server and never shows the Sign In form', async () => { - mount() - await flush() - expect(sessionCalls).toBe(1) - expect(container.querySelector('[data-testid="app"]')).not.toBeNull() - expect(container.querySelector('input[type="password"]')).toBeNull() - }) - - it('keeps the Sign In form when the server does not report a desktop install', async () => { - desktopFlag = false - mount() - await flush() - expect(sessionCalls).toBe(0) - expect(container.querySelector('[data-testid="app"]')).toBeNull() - expect(container.querySelector('input[type="password"]')).not.toBeNull() - }) -}) diff --git a/client/src/components/auth-gate.desktop.test.tsx b/client/src/components/auth-gate.desktop.test.tsx deleted file mode 100644 index 509afd4dcc241e3f736214c46b5760fe996ebcca..0000000000000000000000000000000000000000 --- a/client/src/components/auth-gate.desktop.test.tsx +++ /dev/null @@ -1,182 +0,0 @@ -// @vitest-environment jsdom -// -// The desktop shell signs the dashboard in as a hidden machine account whose -// password is random and never shown (desktop/src/server-host.ts). If that -// session is ever rejected — expired after a month of uptime, cleared by a -// 401 — the gate must ask the shell for a fresh one, never show a login form -// nobody can fill in. This mounts the real AuthGate against a stubbed -// /api/auth/status and checks exactly that. -import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest' -import { act } from 'react' -import { createRoot, type Root } from 'react-dom/client' -import { QueryClient, QueryClientProvider } from '@tanstack/react-query' -import { I18nProvider } from '@/i18n' -import { AuthGate } from './auth-gate' - -type DesktopWindow = Window & { - __FREEAPI_DESKTOP__?: boolean - __FREEAPI_SESSION__?: () => Promise -} - -const FRESH = 'fresh-session-token' -const TOKEN_KEY = 'freellmapi_dashboard_token' - -let root: Root -let container: HTMLDivElement -let accepted: Set -let statusCalls: string[] - -function json(body: unknown, status = 200): Response { - return new Response(JSON.stringify(body), { status, headers: { 'Content-Type': 'application/json' } }) -} - -async function flush() { - for (let i = 0; i < 6; i++) { - await act(async () => { await new Promise(r => setTimeout(r, 0)) }) - } -} - -function mount() { - const client = new QueryClient({ defaultOptions: { queries: { retry: false } } }) - act(() => { - root.render( - - -
dashboard
-
-
, - ) - }) -} - -beforeAll(() => { - ;(globalThis as unknown as { IS_REACT_ACT_ENVIRONMENT: boolean }).IS_REACT_ACT_ENVIRONMENT = true -}) - -beforeEach(() => { - container = document.createElement('div') - document.body.appendChild(container) - root = createRoot(container) - accepted = new Set() - statusCalls = [] - localStorage.setItem(TOKEN_KEY, 'stale-token') - vi.stubGlobal('fetch', vi.fn(async (input: RequestInfo | URL, init?: RequestInit) => { - const url = String(input) - if (!url.endsWith('/api/auth/status')) return json({ error: { message: `unexpected ${url}` } }, 404) - const auth = new Headers(init?.headers).get('Authorization') ?? '' - statusCalls.push(auth) - const ok = accepted.has(auth.replace(/^Bearer /, '')) - return json({ needsSetup: false, authenticated: ok, email: ok ? 'desktop@localhost' : null }) - })) -}) - -afterEach(() => { - act(() => root.unmount()) - container.remove() - localStorage.clear() - vi.unstubAllGlobals() - const w = window as DesktopWindow - delete w.__FREEAPI_DESKTOP__ - delete w.__FREEAPI_SESSION__ -}) - -const passwordField = () => container.querySelector('input[type="password"]') - -describe('AuthGate inside the desktop shell', () => { - it('replaces a rejected session through the shell and never shows the login form', async () => { - const w = window as DesktopWindow - w.__FREEAPI_DESKTOP__ = true - const bridge = vi.fn(async () => { - accepted.add(FRESH) - return FRESH - }) - w.__FREEAPI_SESSION__ = bridge - - mount() - await flush() - - expect(bridge).toHaveBeenCalledTimes(1) - expect(localStorage.getItem(TOKEN_KEY)).toBe(FRESH) - expect(statusCalls.at(-1)).toBe(`Bearer ${FRESH}`) - expect(container.querySelector('[data-testid="app"]')?.textContent).toBe('dashboard') - expect(passwordField()).toBeNull() - }) - - it('shows the recovery message, not a password prompt, when the fresh session is rejected too', async () => { - const w = window as DesktopWindow - w.__FREEAPI_DESKTOP__ = true - const bridge = vi.fn(async () => 'still-rejected') - w.__FREEAPI_SESSION__ = bridge - - mount() - await flush() - - expect(bridge).toHaveBeenCalledTimes(1) - expect(passwordField()).toBeNull() - expect(container.querySelector('[data-testid="app"]')).toBeNull() - expect(container.textContent).toContain('Quit FreeLLMAPI and open it again') - }) - - // The recovery message is the whole screen: a screen reader that has already - // announced "loading" must be told the wait ended in a failure. - it('announces the recovery message as an alert', async () => { - const w = window as DesktopWindow - w.__FREEAPI_DESKTOP__ = true - w.__FREEAPI_SESSION__ = vi.fn(async () => 'still-rejected') - - mount() - await flush() - - const alert = container.querySelector('[role="alert"]') - expect(alert).not.toBeNull() - expect(alert?.textContent?.trim()).not.toBe('') - }) -}) - -describe('AuthGate in a browser', () => { - it('still shows the sign-in form when the session is rejected', async () => { - mount() - await flush() - - expect(passwordField()).not.toBeNull() - expect(container.querySelector('[data-testid="app"]')).toBeNull() - }) - - // Assertions are on the wiring, not the wording: the copy comes from t() and - // is free to change, but a validation error nobody's input points at is - // silent to a screen reader. - it('ties each validation error to the field it describes', async () => { - mount() - await flush() - - const form = container.querySelector('form')! - await act(async () => { - form.dispatchEvent(new Event('submit', { bubbles: true, cancelable: true })) - }) - - for (const id of ['auth-email', 'auth-password']) { - const input = container.querySelector(`#${id}`)! - const describedBy = input.getAttribute('aria-describedby') - expect(describedBy).not.toBeNull() - expect(input.getAttribute('aria-invalid')).toBe('true') - const error = container.querySelector(`#${describedBy}`)! - expect(error).not.toBeNull() - expect(error.getAttribute('role')).toBe('alert') - expect(error.textContent?.trim()).not.toBe('') - } - }) - - it('gives every field a label pointing at it', async () => { - mount() - await flush() - - const inputs = [...container.querySelectorAll('input')] - expect(inputs.length).toBeGreaterThan(0) - for (const input of inputs) { - expect(input.id).not.toBe('') - const label = container.querySelector(`label[for="${input.id}"]`) - expect(label, `no label for #${input.id}`).not.toBeNull() - expect(label?.textContent?.trim()).not.toBe('') - } - }) -}) diff --git a/client/src/components/auth-gate.login-identifier.test.tsx b/client/src/components/auth-gate.login-identifier.test.tsx deleted file mode 100644 index 65453a71e3f22434df60307ff6f6a091e5996cc6..0000000000000000000000000000000000000000 --- a/client/src/components/auth-gate.login-identifier.test.tsx +++ /dev/null @@ -1,121 +0,0 @@ -// @vitest-environment jsdom -// -// The desktop app seeds a hidden account named `desktop@localhost`. The login -// route accepts that identifier on purpose, but the form used to reject it -// client-side ("Enter a valid email address") before any request was sent, so -// a desktop user who opened the dashboard in a browser could reset the -// password and still never sign in (#1250). Setup keeps the strict check. -import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest' -import { act } from 'react' -import { createRoot, type Root } from 'react-dom/client' -import { QueryClient, QueryClientProvider } from '@tanstack/react-query' -import { I18nProvider } from '@/i18n' -import { AuthGate } from './auth-gate' - -let root: Root -let container: HTMLDivElement -let needsSetup: boolean -let posts: { url: string; body: Record }[] - -function json(body: unknown, status = 200): Response { - return new Response(JSON.stringify(body), { status, headers: { 'Content-Type': 'application/json' } }) -} - -async function flush() { - for (let i = 0; i < 6; i++) { - await act(async () => { await new Promise(r => setTimeout(r, 0)) }) - } -} - -function mount() { - const client = new QueryClient({ defaultOptions: { queries: { retry: false } } }) - act(() => { - root.render( - - -
dashboard
-
-
, - ) - }) -} - -function type(selector: string, value: string) { - const input = container.querySelector(selector)! - const setter = Object.getOwnPropertyDescriptor(HTMLInputElement.prototype, 'value')!.set! - act(() => { - setter.call(input, value) - input.dispatchEvent(new Event('input', { bubbles: true })) - }) -} - -async function submit() { - const form = container.querySelector('form')! - act(() => { form.dispatchEvent(new Event('submit', { bubbles: true, cancelable: true })) }) - await flush() -} - -beforeAll(() => { - ;(globalThis as unknown as { IS_REACT_ACT_ENVIRONMENT: boolean }).IS_REACT_ACT_ENVIRONMENT = true -}) - -beforeEach(() => { - container = document.createElement('div') - document.body.appendChild(container) - root = createRoot(container) - needsSetup = false - posts = [] - vi.stubGlobal('fetch', vi.fn(async (input: RequestInfo | URL, init?: RequestInit) => { - const url = String(input) - if (url.endsWith('/api/auth/status')) return json({ needsSetup, authenticated: false, email: null }) - if (init?.method === 'POST') { - posts.push({ url, body: JSON.parse(String(init.body)) }) - return json({ error: { message: 'Invalid email or password' } }, 401) - } - return json({ error: { message: `unexpected ${url}` } }, 404) - })) -}) - -afterEach(() => { - act(() => root.unmount()) - container.remove() - localStorage.clear() - vi.unstubAllGlobals() -}) - -describe('AuthGate login identifier', () => { - it('sends the desktop account identifier to the login route instead of blocking it', async () => { - mount() - await flush() - type('#auth-email', 'desktop@localhost') - type('#auth-password', 'a-new-password') - await submit() - - expect(posts).toHaveLength(1) - expect(posts[0].url.endsWith('/api/auth/login')).toBe(true) - expect(posts[0].body.email).toBe('desktop@localhost') - expect(container.querySelector('#auth-email-error')).toBeNull() - }) - - it('still requires something in the email field on login', async () => { - mount() - await flush() - type('#auth-password', 'a-new-password') - await submit() - - expect(posts).toHaveLength(0) - expect(container.querySelector('#auth-email-error')).not.toBeNull() - }) - - it('keeps the strict email check when creating the first account', async () => { - needsSetup = true - mount() - await flush() - type('#auth-email', 'desktop@localhost') - type('#auth-password', 'a-long-enough-password') - await submit() - - expect(posts).toHaveLength(0) - expect(container.querySelector('#auth-email-error')?.textContent).toContain('valid email') - }) -}) diff --git a/client/src/components/auth-gate.reset-signs-in.test.tsx b/client/src/components/auth-gate.reset-signs-in.test.tsx deleted file mode 100644 index 13d40cec7cfaf9e0302c24275bcebe0e3c0cca20..0000000000000000000000000000000000000000 --- a/client/src/components/auth-gate.reset-signs-in.test.tsx +++ /dev/null @@ -1,124 +0,0 @@ -// @vitest-environment jsdom -// -// A password reset used to end on "Please sign in", but the login form needs -// the account email, which an operator who forgot the password may not -// remember either (#1477). The server now signs them in after a valid reset -// code; the form must store that session and open the dashboard. -import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest' -import { act } from 'react' -import { createRoot, type Root } from 'react-dom/client' -import { QueryClient, QueryClientProvider } from '@tanstack/react-query' -import { I18nProvider } from '@/i18n' -import { AuthGate } from './auth-gate' -import { getToken } from '@/lib/api' - -let root: Root -let container: HTMLDivElement -let authenticated: boolean -let resetResponse: { status: number; body: unknown } -let posts: { url: string; body: Record | null }[] - -function json(body: unknown, status = 200): Response { - return new Response(JSON.stringify(body), { status, headers: { 'Content-Type': 'application/json' } }) -} - -async function flush() { - for (let i = 0; i < 6; i++) { - await act(async () => { await new Promise(r => setTimeout(r, 0)) }) - } -} - -function mount() { - const client = new QueryClient({ defaultOptions: { queries: { retry: false } } }) - act(() => { - root.render( - - -
dashboard
-
-
, - ) - }) -} - -function type(selector: string, value: string) { - const input = container.querySelector(selector)! - const setter = Object.getOwnPropertyDescriptor(HTMLInputElement.prototype, 'value')!.set! - act(() => { - setter.call(input, value) - input.dispatchEvent(new Event('input', { bubbles: true })) - }) -} - -async function clickButton(label: string) { - const button = [...container.querySelectorAll('button')].find(b => b.textContent === label)! - act(() => { button.click() }) - await flush() -} - -async function resetWithCode() { - mount() - await flush() - await clickButton('Forgot password?') - await clickButton('Send reset code') - type('#reset-code', 'ABCDEFGHIJ') - type('#reset-new-password', 'brand-new-password') - const form = container.querySelector('form')! - act(() => { form.dispatchEvent(new Event('submit', { bubbles: true, cancelable: true })) }) - await flush() -} - -beforeAll(() => { - ;(globalThis as unknown as { IS_REACT_ACT_ENVIRONMENT: boolean }).IS_REACT_ACT_ENVIRONMENT = true -}) - -beforeEach(() => { - container = document.createElement('div') - document.body.appendChild(container) - root = createRoot(container) - authenticated = false - posts = [] - resetResponse = { status: 200, body: { success: true, token: 'session-after-reset', email: 'owner@example.test' } } - vi.stubGlobal('fetch', vi.fn(async (input: RequestInfo | URL, init?: RequestInit) => { - const url = String(input) - if (url.endsWith('/api/auth/status')) { - return json({ needsSetup: false, authenticated, email: authenticated ? 'owner@example.test' : null }) - } - if (init?.method === 'POST') { - posts.push({ url, body: init.body ? JSON.parse(String(init.body)) : null }) - if (url.endsWith('/api/auth/forgot-password')) return json({ success: true }) - if (url.endsWith('/api/auth/reset-password')) { - if (resetResponse.status === 200) authenticated = true - return json(resetResponse.body, resetResponse.status) - } - } - return json({ error: { message: `unexpected ${url}` } }, 404) - })) -}) - -afterEach(() => { - act(() => root.unmount()) - container.remove() - localStorage.clear() - vi.unstubAllGlobals() -}) - -describe('AuthGate password reset', () => { - it('signs in with the session the reset returns instead of asking for the email', async () => { - await resetWithCode() - - expect(posts.map(p => p.url.replace(/^.*\/api/, '/api'))).toEqual(['/api/auth/forgot-password', '/api/auth/reset-password']) - expect(posts[1].body).toEqual({ resetCode: 'ABCDEFGHIJ', newPassword: 'brand-new-password' }) - expect(getToken()).toBe('session-after-reset') - expect(container.querySelector('[data-testid="app"]')).not.toBeNull() - }) - - it('stays on the reset form with the error when the code is wrong', async () => { - resetResponse = { status: 403, body: { error: { message: 'Invalid or expired reset code' } } } - await resetWithCode() - - expect(getToken()).toBeNull() - expect(container.querySelector('[data-testid="app"]')).toBeNull() - expect(container.textContent).toContain('Invalid or expired reset code') - }) -}) diff --git a/client/src/components/auth-gate.tsx b/client/src/components/auth-gate.tsx deleted file mode 100644 index 48087be6dfcbb80e01aec55537db5c642fa56437..0000000000000000000000000000000000000000 --- a/client/src/components/auth-gate.tsx +++ /dev/null @@ -1,547 +0,0 @@ -import { useEffect, useRef, useState, type ReactNode } from 'react' -import { useQuery, useQueryClient } from '@tanstack/react-query' -import { Loader2 } from 'lucide-react' -import { apiFetch, setToken, UNAUTHORIZED_EVENT, type ApiError } from '@/lib/api' -import { Button } from '@/components/ui/button' -import { FieldError } from '@/components/ui/field-error' -import { Input } from '@/components/ui/input' -import { PasswordInput } from '@/components/ui/password-input' -import { Label } from '@/components/ui/label' -import { isEmail } from '@/lib/validate' -import { useI18n } from '@/i18n' -import { toast } from '@/lib/toast' - -// Matches the server rule (routes/auth.ts zod schema). -const PASSWORD_MIN = 8 - -// Inside the desktop shell the dashboard runs as a hidden machine account with -// a random password nobody knows (desktop/src/server-host.ts). There are no -// credentials to type, so the gate never shows a login form there: when the -// seeded session is gone (expired after a month of uptime, or cleared by a -// 401) it asks the shell for a fresh one through the preload bridge. -type DesktopWindow = Window & { - __FREEAPI_DESKTOP__?: boolean - __FREEAPI_SESSION__?: () => Promise -} -function desktopSessionBridge(): (() => Promise) | null { - if (typeof window === 'undefined') return null - const w = window as DesktopWindow - return w.__FREEAPI_DESKTOP__ === true && typeof w.__FREEAPI_SESSION__ === 'function' - ? w.__FREEAPI_SESSION__ - : null -} - -// The same dashboard opened in a normal browser tab on a desktop install has no -// preload bridge, so the server mints the session instead (#1399). It only -// does so for a request from this computer, and says so through `desktop` on -// /api/auth/status; web and Docker installs never set it and keep their login. -async function browserDesktopSession(): Promise { - const res = await apiFetch<{ token: string }>('/api/auth/desktop-session', { method: 'POST' }) - return res.token -} - -interface AuthStatus { - needsSetup: boolean - authenticated: boolean - email: string | null - desktop?: boolean -} - -function Centered({ children }: { children: ReactNode }) { - // dvh, not vh: on mobile the collapsing URL bar and the software keyboard both - // change the viewport, and 100vh leaves the card parked mid-scroll. - return ( -
-
{children}
-
- ) -} - -function AuthForm({ mode, onAuthed }: { mode: 'setup' | 'login'; onAuthed: () => void }) { - const { t } = useI18n() - const [email, setEmail] = useState('') - const [password, setPassword] = useState('') - const [setupCode, setSetupCode] = useState('') - // Revealed only after the server asks for it (remote first-run setup). A - // browser on the same machine as the server never sees this field. - const [codeRequired, setCodeRequired] = useState(false) - const [error, setError] = useState('') - const [busy, setBusy] = useState(false) - const [attempted, setAttempted] = useState(false) - const [showForgot, setShowForgot] = useState(false) - - const isSetup = mode === 'setup' - - // Inline field feedback; the server stays authoritative. Only the setup form - // enforces the password minimum client-side (an existing password of any - // length must still be able to log in). The same goes for the email shape: - // the desktop app's hidden account is `desktop@localhost` (no TLD), which - // the login route accepts on purpose (server/src/routes/auth.ts), so a - // browser tab must be able to submit it after a password reset (#1250). - const emailError = !email.trim() - ? t('validation.required') - : isSetup && !isEmail(email) - ? t('validation.email') - : null - const passwordError = !password - ? t('validation.required') - : isSetup && password.length < PASSWORD_MIN - ? t('validation.passwordMin', { min: PASSWORD_MIN }) - : null - - const showEmailError = attempted && !!emailError - const showPasswordError = attempted && !!passwordError - - async function submit(e: React.FormEvent) { - e.preventDefault() - if (emailError || passwordError) { - setAttempted(true) - return - } - setBusy(true) - setError('') - try { - const payload: Record = { email, password } - // Only the setup flow carries a code, and only once the server has asked - // for it. The server ignores it for local (loopback) setup. - if (isSetup && setupCode) payload.setupCode = setupCode.trim() - const res = await apiFetch<{ token: string }>(isSetup ? '/api/auth/setup' : '/api/auth/login', { - method: 'POST', - body: JSON.stringify(payload), - }) - setToken(res.token) - onAuthed() - } catch (err) { - // The server gates remote first-run setup behind a one-time code; reveal - // the field so the operator can paste the code from the server logs. - if (isSetup && (err as ApiError).code === 'setup_code_required') { - setCodeRequired(true) - } - setError((err as Error).message) - } finally { - setBusy(false) - } - } - - if (showForgot && !isSetup) { - return setShowForgot(false)} onAuthed={onAuthed} /> - } - - return ( - -
- - FreeLLMAPI -
-
-

{isSetup ? t('auth.createYourAccount') : t('auth.signIn')}

-

- {isSetup - ? t('auth.setupDescription') - : t('auth.loginDescription')} -

-
-
- - setEmail(e.target.value)} - placeholder={t('auth.emailPlaceholder')} - aria-invalid={showEmailError} - aria-describedby={showEmailError ? 'auth-email-error' : undefined} - /> - {attempted && } -
-
- - setPassword(e.target.value)} - placeholder={isSetup ? t('auth.passwordPlaceholderSetup') : t('auth.passwordPlaceholderLogin')} - aria-invalid={showPasswordError} - aria-describedby={showPasswordError ? 'auth-password-error' : undefined} - /> - {attempted && } -
- {isSetup && codeRequired && ( -
- - setSetupCode(e.target.value)} - placeholder={t('auth.setupCodePlaceholder')} - aria-describedby="auth-setup-code-hint" - /> -

{t('auth.setupCodeHint')}

-
- )} - {/* A server rejection is the one error nobody can predict; it gets the same - announcement and the same visual weight as the inline field errors. */} - {error && ( -

- {error} -

- )} - -
- {!isSetup && ( - - )} -
-
- ) -} - -// Forgot / Reset password flow - -type ForgotStep = 'request' | 'reset' | 'done' - -function ForgotPasswordForm({ onBack, onAuthed }: { onBack: () => void; onAuthed: () => void }) { - const { t } = useI18n() - const [step, setStep] = useState('request') - const [resetCode, setResetCode] = useState('') - const [newPassword, setNewPassword] = useState('') - const [error, setError] = useState('') - const [busy, setBusy] = useState(false) - const [attempted, setAttempted] = useState(false) - - const passwordError = !newPassword - ? t('validation.required') - : newPassword.length < PASSWORD_MIN - ? t('validation.passwordMin', { min: PASSWORD_MIN }) - : null - const codeError = !resetCode.trim() ? t('validation.required') : null - - async function requestCode() { - setBusy(true) - setError('') - try { - await apiFetch('/api/auth/forgot-password', { method: 'POST' }) - setStep('reset') - } catch (err) { - setError((err as Error).message) - } finally { - setBusy(false) - } - } - - async function submitReset(e: React.FormEvent) { - e.preventDefault() - if (codeError || passwordError) { - setAttempted(true) - return - } - setBusy(true) - setError('') - try { - const res = await apiFetch<{ token?: string }>('/api/auth/reset-password', { - method: 'POST', - body: JSON.stringify({ resetCode: resetCode.trim(), newPassword }), - }) - // The server signs the operator in after a reset, so nobody has to - // remember which email the account uses (#1477). - if (res.token) { - setToken(res.token) - onAuthed() - return - } - setStep('done') - } catch (err) { - setError((err as Error).message) - } finally { - setBusy(false) - } - } - - return ( - -
- - FreeLLMAPI -
-
-

{t('auth.forgotPassword')}

- - {step === 'request' && ( -
-

{t('auth.forgotPasswordDescription')}

- {error &&

{error}

} - -
- )} - - {step === 'reset' && ( -
-

{t('auth.resetCodeHint')}

-

{t('auth.resetCodeDockerHint')}

-
- - setResetCode(e.target.value)} - placeholder={t('auth.resetCodePlaceholder')} - aria-invalid={attempted && !!codeError} - /> - {attempted && } -
-
- - setNewPassword(e.target.value)} - placeholder={t('auth.passwordPlaceholderSetup')} - aria-invalid={attempted && !!passwordError} - /> - {attempted && } -
- {error &&

{error}

} - -
- )} - - {step === 'done' && ( -

{t('auth.passwordReset')}

- )} - - -
-
- ) -} - -// Change-credentials modal (rendered inside the authenticated shell) - -interface ChangeCredentialsModalProps { - mode: 'password' | 'email' - onClose: () => void -} - -export function ChangeCredentialsModal({ mode, onClose }: ChangeCredentialsModalProps) { - const { t } = useI18n() - const [currentPassword, setCurrentPassword] = useState('') - const [newValue, setNewValue] = useState('') - const [error, setError] = useState('') - const [busy, setBusy] = useState(false) - const [attempted, setAttempted] = useState(false) - - const isPassword = mode === 'password' - - const newValueError = !newValue.trim() - ? t('validation.required') - : isPassword && newValue.length < PASSWORD_MIN - ? t('validation.passwordMin', { min: PASSWORD_MIN }) - : !isPassword && !isEmail(newValue) - ? t('validation.email') - : null - const currentPwError = !currentPassword ? t('validation.required') : null - - async function submit(e: React.FormEvent) { - e.preventDefault() - if (newValueError || currentPwError) { - setAttempted(true) - return - } - setBusy(true) - setError('') - try { - if (isPassword) { - await apiFetch('/api/auth/change-password', { - method: 'POST', - body: JSON.stringify({ currentPassword, newPassword: newValue }), - }) - toast.success(t('auth.passwordChanged')) - } else { - await apiFetch('/api/auth/change-email', { - method: 'POST', - body: JSON.stringify({ currentPassword, newEmail: newValue }), - }) - toast.success(t('auth.emailChanged')) - } - onClose() - } catch (err) { - setError((err as Error).message) - } finally { - setBusy(false) - } - } - - return ( -
{ if (e.target === e.currentTarget) onClose() }} - > -
-

- {isPassword ? t('auth.changePassword') : t('auth.changeEmail')} -

-
-
- - setCurrentPassword(e.target.value)} - placeholder={t('auth.passwordPlaceholderLogin')} - aria-invalid={attempted && !!currentPwError} - /> - {attempted && } -
-
- - {isPassword ? setNewValue(e.target.value)} - placeholder={t('auth.passwordPlaceholderSetup')} - aria-invalid={attempted && !!newValueError} - /> : setNewValue(e.target.value)} - placeholder={t('auth.emailPlaceholder')} - aria-invalid={attempted && !!newValueError} - />} - {attempted && } -
- {error &&

{error}

} -
- - -
-
-
-
- ) -} - -export function AuthGate({ children }: { children: ReactNode }) { - const { t } = useI18n() - const queryClient = useQueryClient() - const { data, isLoading, isError, refetch } = useQuery({ - queryKey: ['auth-status'], - queryFn: () => apiFetch('/api/auth/status'), - retry: false, - }) - - useEffect(() => { - const handler = () => { refetch() } - window.addEventListener(UNAUTHORIZED_EVENT, handler) - return () => window.removeEventListener(UNAUTHORIZED_EVENT, handler) - }, [refetch]) - - // Desktop self-repair: one attempt at a time, and none after the shell has - // handed back a session the server still rejects — a broken install shows a - // message instead of looping. - const desktopSession = desktopSessionBridge() ?? (data?.desktop ? browserDesktopSession : null) - const desktopNeedsSession = !!desktopSession && !!data && (data.needsSetup || !data.authenticated) - const repairing = useRef(false) - const [repairFailed, setRepairFailed] = useState(false) - useEffect(() => { - if (!desktopNeedsSession || !desktopSession || repairing.current || repairFailed) return - repairing.current = true - ;(async () => { - try { - const token = await desktopSession() - if (!token) throw new Error('empty session token') - setToken(token) - queryClient.invalidateQueries() - const next = await refetch() - if (!next.data?.authenticated) setRepairFailed(true) - } catch (err) { - console.error('[auth-gate] desktop session repair failed', err) - setRepairFailed(true) - } finally { - repairing.current = false - } - })() - }, [desktopNeedsSession, desktopSession, repairFailed, queryClient, refetch]) - - function onAuthed() { - // New session: drop any cached (unauthenticated) data and re-check status. - queryClient.invalidateQueries() - refetch() - } - - if (isLoading) { - return ( - -

- - {t('auth.loading')} -

-
- ) - } - if (isError || !data) { - return ( - -
- {t('auth.serverUnreachableBefore')}npm run dev{t('auth.serverUnreachableAfter')} -
-
- ) - } - - if (desktopNeedsSession) { - if (repairFailed) { - return ( - -
- {t('auth.desktopSessionFailed')} -
-
- ) - } - return

{t('auth.loading')}

- } - if (data.needsSetup) return - if (!data.authenticated) return - - return <>{children} -} - diff --git a/client/src/components/chain-manager.test.ts b/client/src/components/chain-manager.test.ts deleted file mode 100644 index 0e6d750f22fa6eb38e4cff43ac8a728f6922a206..0000000000000000000000000000000000000000 --- a/client/src/components/chain-manager.test.ts +++ /dev/null @@ -1,116 +0,0 @@ -import { readdirSync, readFileSync } from 'node:fs' -import path from 'node:path' -import { fileURLToPath } from 'node:url' -import { describe, expect, it } from 'vitest' - -// The chain manager is the dashboard surface for named fallback chains -// (#960/#895). Two things about it are easy to break by accident and are -// pinned here: every string it renders has to exist in all 60 locales, and it -// has to stay a secondary panel on the Fallback page rather than growing into -// its own nav entry — one chain is all most installs ever use. -const here = path.dirname(fileURLToPath(import.meta.url)) -const localeDir = path.join(here, '../i18n/locales') -const source = readFileSync(path.join(here, 'chain-manager.tsx'), 'utf8') -const fallbackPage = readFileSync(path.join(here, '../pages/FallbackPage.tsx'), 'utf8') -const app = readFileSync(path.join(here, '../App.tsx'), 'utf8') -const playground = readFileSync(path.join(here, '../pages/PlaygroundPage.tsx'), 'utf8') - -function locale(name: string): Record { - return JSON.parse(readFileSync(path.join(localeDir, `${name}.json`), 'utf8')) -} - -function lookup(dictionary: Record, key: string): unknown { - return key.split('.').reduce( - (node, part) => (node && typeof node === 'object' ? (node as Record)[part] : undefined), - dictionary, - ) -} - -const locales = readdirSync(localeDir) - .filter(name => name.endsWith('.json')) - .map(name => name.slice(0, -5)) - -// Every t('...') the component actually calls. -const usedKeys = [...source.matchAll(/\bt\('([^']+)'/g)].map(match => match[1]) - -describe('chain manager', () => { - it('calls only keys that exist in every locale', () => { - expect(usedKeys.length).toBeGreaterThan(5) - expect(usedKeys).toContain('chains.title') - expect(locales.length).toBeGreaterThan(50) - - for (const name of locales) { - const dictionary = locale(name) - for (const key of usedKeys) { - expect(typeof lookup(dictionary, key), `${name} is missing ${key}`).toBe('string') - } - } - }) - - it('keeps the {name} and {count} placeholders in every locale', () => { - for (const name of locales) { - const dictionary = locale(name) - expect(lookup(dictionary, 'chains.deleteConfirm'), `${name}.chains.deleteConfirm`) - .toContain('{name}') - expect(lookup(dictionary, 'chains.count'), `${name}.chains.count`).toContain('{count}') - } - }) - - it('lives on the Fallback page as a collapsible panel, not a nav entry', () => { - expect(fallbackPage).toContain("import { ChainManager } from '@/components/chain-manager'") - expect(fallbackPage).toContain('') - // Collapsed by default and remembered per browser, like the penalty - // inspector next to it — the routing table is what the page is for. - expect(source).toContain('aria-expanded') - expect(source).toMatch(/stored === null \? true : stored === '1'/) - // No route or nav destination of its own. - expect(app).not.toContain('chain-manager') - expect(app).not.toContain('ChainManager') - }) - - it('surfaces the server error when a chain name is rejected', () => { - // POST /api/profiles enforces length, character set, reserved words and - // uniqueness. Swallowing that 400/409 leaves a button that does nothing. - expect(source).toContain('onError') - expect(source).toContain('setCreateError') - }) - - it('renames a chain through the profile update endpoint, with a confirm (#1179)', () => { - // A chain's name is the address clients route with (auto:), so the - // rename goes through PUT /api/profiles/:id — where the creation rules - // are enforced and protected chains get a real 403 — and must warn about - // in-flight auto: callers before committing. - expect(source).toMatch(/api\/profiles\/\$\{profileId\}`, \{ method: 'PUT'/) - expect(source).not.toContain('/rename') - expect(source).toContain('chains.renameConfirm') - expect(source).toContain('chains.renameFailed') - // Only custom chains get the pencil, matching the delete affordance. - expect(source).toMatch(/!isProtected[\s\S]*?chains\.renameHint/) - }) -}) - -// Switching the active chain has to change what the rest of the dashboard is -// looking at (#1021). Both of these were silently wrong: the routing table read -// one unscoped cache entry, so a switch re-rendered the previous chain's rows -// and a save wrote them into the new chain; and the Playground could only ever -// send 'auto', so a chain you had just built was untestable from the dashboard. -describe('chains reach the rest of the dashboard (#1021)', () => { - it('scopes the fallback table and its staged edits to the active chain', () => { - expect(fallbackPage).toContain("queryKey: ['profiles', 'active']") - expect(fallbackPage).toContain("queryKey: ['fallback', 'chain', activeProfileId]") - // Staged edits remember which chain they were made against instead of - // following whichever one happens to be active at save time. - expect(fallbackPage).toMatch(/staged\.profileId === activeProfileId/) - }) - - it('offers every custom chain in the playground picker as auto:', () => { - expect(playground).toContain("queryKey: ['profiles']") - // The id has to be derived exactly as the server derives it in - // routes/proxy.ts, or the picker offers models /v1/models never listed. - expect(playground).toContain('`auto:${c.name.toLowerCase()}`') - expect(playground).toContain('...chainOptions,') - // A chain is server-picked like plain auto, so the "can't see images" hint - // must not fire on one. - expect(playground).toContain("!selectedModel.startsWith('auto:')") - }) -}) diff --git a/client/src/components/chain-manager.tsx b/client/src/components/chain-manager.tsx deleted file mode 100644 index 9fbf437996178eca167aa42cfa75e2de5be5bd67..0000000000000000000000000000000000000000 --- a/client/src/components/chain-manager.tsx +++ /dev/null @@ -1,328 +0,0 @@ -import { useState } from 'react' -import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query' -import { Check, ChevronDown, Layers, Pencil, Plus, Trash2 } from 'lucide-react' -import { useI18n } from '@/i18n' -import { apiFetch, type ApiError } from '@/lib/api' -import { Button } from '@/components/ui/button' -import { Input } from '@/components/ui/input' -import { Badge } from '@/components/ui/badge' -import { Tooltip } from '@/components/tooltip' - -// Named fallback chains (#960/#895). The backend /api/profiles CRUD is -// complete and every chain is listed as an `auto:` model in /v1/models; -// this panel is the missing dashboard surface: list chains, create new ones, -// switch the active chain (the fallback table below then edits that chain), -// and delete custom ones. -// -// Deliberately a secondary, collapsed-by-default accordion on the Fallback -// page rather than its own nav entry: most installs use one chain forever, and -// the routing table below is what the page is for. - -const COLLAPSED_KEY = 'freellmapi.chainManager.collapsed' - -function readCollapsed(): boolean { - try { - const stored = localStorage.getItem(COLLAPSED_KEY) - return stored === null ? true : stored === '1' - } catch { - return true - } -} - -export interface Chain { - id: number - name: string - emoji: string - color: string - type: 'default' | 'builtin' | 'custom' - is_favorite: number - sort_order: number - auto_sort: string | null - layout_config: string | null - // 0 once the chain opts out of the catalog-sync backfill (#895), which is - // what an empty-created chain does — it stays exactly as hand-built. - auto_include_new_models: number - created_at: string -} - -export function ChainManager() { - const { t } = useI18n() - const queryClient = useQueryClient() - const [newName, setNewName] = useState('') - const [startEmpty, setStartEmpty] = useState(true) - const [createError, setCreateError] = useState('') - const [collapsed, setCollapsed] = useState(readCollapsed) - // Inline rename (#1179): one row is editable at a time, like the Playground - // conversation list. Renaming is safe mid-flight — routing resolves the name - // at request time — but in-flight auto: requests fail, so the - // confirm step says so. - const [renamingId, setRenamingId] = useState(null) - const [renameDraft, setRenameDraft] = useState('') - const [renameError, setRenameError] = useState('') - - const { data: chains = [] } = useQuery({ - queryKey: ['profiles'], - queryFn: () => apiFetch('/api/profiles'), - }) - const { data: active } = useQuery<{ activeProfileId: number | null }>({ - queryKey: ['profiles', 'active'], - queryFn: () => apiFetch('/api/profiles/active'), - }) - - const invalidate = async () => { - // Resolve WHICH chain is active before refreshing anything keyed on it - // (#1047): invalidating ['fallback'] first refetched the chain table under - // the old profile id — a wasted full-catalog round trip, and the write that - // poisoned the old chain's cache entry with the new chain's rows. - await queryClient.refetchQueries({ queryKey: ['profiles', 'active'], exact: true }) - queryClient.invalidateQueries({ queryKey: ['profiles'] }) - // The fallback table renders the active chain; refresh it too. The prefix - // covers the chain, routing, token-usage and rate-limit queries. - queryClient.invalidateQueries({ queryKey: ['fallback'] }) - } - - const createChain = useMutation({ - mutationFn: (payload: { name: string; empty: boolean }) => - apiFetch('/api/profiles', { method: 'POST', body: JSON.stringify(payload) }), - onSuccess: () => { - invalidate() - setNewName('') - setCreateError('') - }, - // Names are validated server-side (max 20 chars, Latin/digits/-/_ only, a - // list of reserved words, no duplicates). Say which rule was broken rather - // than swallowing the 400/409. - onError: (error: ApiError) => setCreateError(error.message || t('chains.createFailed')), - }) - const setActive = useMutation({ - mutationFn: (profileId: number) => - apiFetch('/api/profiles/active', { method: 'POST', body: JSON.stringify({ profileId }) }), - onSuccess: invalidate, - }) - const deleteChain = useMutation({ - mutationFn: (profileId: number) => - apiFetch(`/api/profiles/${profileId}`, { method: 'DELETE' }), - onSuccess: invalidate, - }) - const renameChain = useMutation({ - mutationFn: ({ profileId, name }: { profileId: number; name: string }) => - apiFetch(`/api/profiles/${profileId}`, { method: 'PUT', body: JSON.stringify({ name }) }), - onSuccess: () => { - invalidate() - setRenamingId(null) - setRenameError('') - }, - // Same server rules as create (charset, length, reserved, taken); show - // which one fired instead of leaving the row stuck in edit mode. - onError: (error: ApiError) => setRenameError(error.message || t('chains.renameFailed')), - }) - - if (chains.length === 0) return null - - const activeId = active?.activeProfileId ?? null - - function toggle() { - setCollapsed(prev => { - const next = !prev - try { localStorage.setItem(COLLAPSED_KEY, next ? '1' : '0') } catch { /* ignore */ } - return next - }) - } - - return ( -
- - - {!collapsed && ( -
-
- {chains.map(chain => { - const isActive = chain.id === activeId - const isProtected = chain.type === 'default' || chain.type === 'builtin' - const isRenaming = renamingId === chain.id - if (isRenaming) { - return ( -
{ - e.preventDefault() - const name = renameDraft.trim() - if (!name || renameChain.isPending) return - if (name !== chain.name - && !window.confirm(t('chains.renameConfirm', { oldName: chain.name, name }))) return - renameChain.mutate({ profileId: chain.id, name }) - }} - > - { - setRenameDraft(e.target.value) - setRenameError('') - }} - onKeyDown={e => { if (e.key === 'Escape') { e.preventDefault(); setRenamingId(null); setRenameError('') } }} - aria-label={t('chains.renamePlaceholder')} - placeholder={t('chains.renamePlaceholder')} - className="h-7 max-w-48 text-sm" - /> - - - {renameError && {renameError}} -
- ) - } - return ( -
- - {chain.emoji || } - {chain.name} - - {/* The id a client sends to pick this chain per request. */} - - auto:{chain.name.toLowerCase()} - - {isActive && ( - - - {t('chains.active')} - - )} - {isProtected && !isActive && ( - {t('chains.default')} - )} - {chain.auto_include_new_models === 0 && ( - - - {t('chains.curated')} - - - )} - - {!isActive && ( - - - - )} - {!isProtected && ( - - - - )} - {!isProtected && ( - - - - )} -
- ) - })} -
- -
{ - e.preventDefault() - const name = newName.trim() - if (name && !createChain.isPending) createChain.mutate({ name, empty: startEmpty }) - }} - > - { - setNewName(e.target.value) - setCreateError('') - }} - placeholder={t('chains.createPlaceholder')} - aria-label={t('chains.createPlaceholder')} - className="h-8" - /> - -
- - - {createError - ?

{createError}

- :

{t('chains.nameRules')}

} -
- )} -
- ) -} diff --git a/client/src/components/code-block.tsx b/client/src/components/code-block.tsx deleted file mode 100644 index 66cfec50d8f094b7ee23ff5356a5e5de8424db5b..0000000000000000000000000000000000000000 --- a/client/src/components/code-block.tsx +++ /dev/null @@ -1,24 +0,0 @@ -import { useMemo } from 'react' -import { cn } from '@/lib/utils' -import { highlightCode } from '@/lib/highlight' - -// Highlighted source: the reply code blocks and the artifact panel's Code tab. -// highlightCode returns escaped HTML with hljs-* spans, so injecting it is -// safe; the token colours live in index.css under `.code-highlight`. - -export interface CodeBlockProps { - code: string - language?: string | null - className?: string -} - -export function CodeBlock({ code, language, className }: CodeBlockProps) { - const { html, language: applied } = useMemo(() => highlightCode(code, language), [code, language]) - return ( - - ) -} diff --git a/client/src/components/command-palette-state.ts b/client/src/components/command-palette-state.ts deleted file mode 100644 index 7a056fefa1192d66484438e98a6cfc0fbbafbac2..0000000000000000000000000000000000000000 --- a/client/src/components/command-palette-state.ts +++ /dev/null @@ -1,5 +0,0 @@ -export const COMMAND_PALETTE_EVENT = 'freellmapi:command-palette' - -export function openCommandPalette() { - window.dispatchEvent(new CustomEvent(COMMAND_PALETTE_EVENT)) -} diff --git a/client/src/components/command-palette.tsx b/client/src/components/command-palette.tsx deleted file mode 100644 index 0998413aeb6875c1ac14d810d3dfd4b0a90129c2..0000000000000000000000000000000000000000 --- a/client/src/components/command-palette.tsx +++ /dev/null @@ -1,296 +0,0 @@ -import { useCallback, useEffect, useMemo, useRef, useState } from 'react' -import { useNavigate } from 'react-router-dom' -import { useQuery } from '@tanstack/react-query' -import { - AudioLines, - Bot, - Boxes, - ChartColumn, - Clapperboard, - Copy, - Globe, - Scale, - Image as ImageIcon, - KeyRound, - Layers, - MessageSquare, - Moon, - Search, - Sparkles, - SquareTerminal, - Zap, -} from 'lucide-react' -import { apiFetch } from '@/lib/api' -import { copyText } from '@/lib/clipboard' -import { toast } from '@/lib/toast' -import { apiBaseUrl } from '@/components/api-usage' -import { COMMAND_PALETTE_EVENT } from '@/components/command-palette-state' -import { useI18n } from '@/i18n' -import type { FallbackEntry } from '@/lib/routing' -import { useTheme } from '@/theme-context' - -interface Command { - id: string - group: 'pages' | 'actions' | 'models' - label: string - keywords: string - icon: React.ComponentType<{ className?: string }> - run: () => void -} - -// Copy and report what actually happened: on an insecure origin (a plain-HTTP -// LAN install) the copy can fail, and a success toast over an empty clipboard -// is worse than no toast at all (#734). -async function copyAndToast(value: string, success: string, failure: string) { - if (await copyText(value)) toast.success(success) - else toast.error(failure) -} - -// Cmd+K / Ctrl+K palette: jump to any page or model, toggle theme, copy the -// unified key or base URL. Zero-dep overlay; list is keyboard-driven -// (arrows + Enter) with proper listbox semantics. -export function CommandPalette() { - const { t } = useI18n() - const { resolvedDark, setTheme } = useTheme() - const navigate = useNavigate() - const [open, setOpen] = useState(false) - const [query, setQuery] = useState('') - const [active, setActive] = useState(0) - const openRef = useRef(false) - const inputRef = useRef(null) - const listRef = useRef(null) - - // Reset happens on show (not in an effect watching `open`): the palette - // always opens blank with the first row active. - const show = useCallback(() => { - openRef.current = true - setQuery('') - setActive(0) - setOpen(true) - // Focus after the overlay paints. - requestAnimationFrame(() => inputRef.current?.focus()) - }, []) - const hide = useCallback(() => { - openRef.current = false - setOpen(false) - }, []) - - useEffect(() => { - function onKey(e: KeyboardEvent) { - if ((e.metaKey || e.ctrlKey) && e.key.toLowerCase() === 'k') { - e.preventDefault() - if (openRef.current) hide() - else show() - } else if (e.key === 'Escape' && openRef.current) { - hide() - } - } - window.addEventListener('keydown', onKey) - window.addEventListener(COMMAND_PALETTE_EVENT, show) - return () => { - window.removeEventListener('keydown', onKey) - window.removeEventListener(COMMAND_PALETTE_EVENT, show) - } - }, [show, hide]) - - // Model index + unified key load lazily, only while the palette is open. - const { data: entries = [] } = useQuery({ - queryKey: ['fallback'], - queryFn: () => apiFetch('/api/fallback'), - enabled: open, - }) - const { data: keyData } = useQuery<{ apiKey: string }>({ - queryKey: ['unified-key'], - queryFn: () => apiFetch('/api/settings/api-key'), - enabled: open, - }) - - const commands = useMemo(() => { - const go = (to: string) => () => navigate(to) - const pages: Command[] = [ - { id: 'p-chat', group: 'pages', label: t('models.chatModelsTab'), keywords: 'models chat routing fallback', icon: MessageSquare, run: go('/models/chat') }, - { id: 'p-image', group: 'pages', label: t('models.imageTab'), keywords: 'models image generation', icon: ImageIcon, run: go('/models/image') }, - { id: 'p-video', group: 'pages', label: t('models.videoTab'), keywords: 'models video text to video generation mp4', icon: Clapperboard, run: go('/models/video') }, - { id: 'p-audio', group: 'pages', label: t('models.audioTab'), keywords: 'models audio speech tts stt transcription whisper', icon: AudioLines, run: go('/models/audio') }, - { id: 'p-embeddings', group: 'pages', label: t('models.embeddingsTab'), keywords: 'models embeddings vectors', icon: Layers, run: go('/models/embeddings') }, - { id: 'p-decision', group: 'pages', label: t('decision.tab'), keywords: 'models decision classify choice yes no score clef mercury', icon: Scale, run: go('/models/decision') }, - { id: 'p-search', group: 'pages', label: t('search.tab'), keywords: 'models search web tavily exa firecrawl linkup parallel keenable', icon: Globe, run: go('/models/search') }, - { id: 'p-fusion', group: 'pages', label: t('models.fusionTab'), keywords: 'models fusion synthesis panel judge', icon: Zap, run: go('/models/fusion') }, - { id: 'p-playground', group: 'pages', label: t('nav.playground'), keywords: 'playground test chat try', icon: SquareTerminal, run: go('/playground') }, - { id: 'p-keys', group: 'pages', label: t('nav.keys'), keywords: 'keys providers api tokens', icon: KeyRound, run: go('/keys') }, - { id: 'p-agents', group: 'pages', label: t('nav.agents'), keywords: 'agents claude codex cline ollama gemini', icon: Bot, run: go('/agents') }, - { id: 'p-analytics', group: 'pages', label: t('nav.analytics'), keywords: 'analytics usage stats savings latency', icon: ChartColumn, run: go('/analytics') }, - { id: 'p-premium', group: 'pages', label: t('nav.premium'), keywords: 'premium catalog license subscription', icon: Sparkles, run: go('/premium') }, - ] - const actions: Command[] = [ - { - id: 'a-theme', - group: 'actions', - label: t('palette.toggleTheme'), - keywords: 'theme dark light mode toggle', - icon: Moon, - run: () => { - setTheme(resolvedDark ? 'light' : 'dark') - }, - }, - { - id: 'a-copy-key', - group: 'actions', - label: t('setup.copyKey'), - keywords: 'copy api key unified token', - icon: Copy, - run: () => { - if (!keyData?.apiKey) return - void copyAndToast(keyData.apiKey, t('setup.copiedKey'), t('common.copyFailed')) - }, - }, - { - id: 'a-copy-url', - group: 'actions', - label: t('setup.copyUrl'), - keywords: 'copy base url endpoint', - icon: Copy, - run: () => { - void copyAndToast(apiBaseUrl(), t('setup.copiedUrl'), t('common.copyFailed')) - }, - }, - ] - // One entry per logical model (configured providers only). - const seen = new Set() - const models: Command[] = [] - for (const e of entries) { - if (e.keyCount === 0) continue - const canonical = e.canonicalId ?? e.modelId - if (seen.has(canonical)) continue - seen.add(canonical) - const label = e.groupLabel ?? e.displayName - models.push({ - id: `m-${canonical}`, - group: 'models', - label, - keywords: `${canonical} ${e.modelId} ${e.platform}`, - icon: Boxes, - run: go(`/models/chat/${encodeURIComponent(canonical)}`), - }) - } - return [...pages, ...actions, ...models] - }, [entries, keyData, navigate, resolvedDark, setTheme, t]) - - const filtered = useMemo(() => { - const q = query.trim().toLowerCase() - if (!q) return commands.filter(c => c.group !== 'models').concat(commands.filter(c => c.group === 'models').slice(0, 5)) - const matches = commands.filter(c => c.label.toLowerCase().includes(q) || c.keywords.toLowerCase().includes(q)) - // Pages/actions first, then at most 8 model hits so the list stays scannable. - return matches.filter(c => c.group !== 'models').concat(matches.filter(c => c.group === 'models').slice(0, 8)) - }, [commands, query]) - - useEffect(() => { - const el = listRef.current?.querySelector(`[data-index="${active}"]`) - el?.scrollIntoView({ block: 'nearest' }) - }, [active]) - - if (!open) return null - - function runCommand(cmd: Command) { - hide() - cmd.run() - } - - function onListKeyDown(e: React.KeyboardEvent) { - if (e.key === 'ArrowDown') { - e.preventDefault() - setActive(a => Math.min(a + 1, filtered.length - 1)) - } else if (e.key === 'ArrowUp') { - e.preventDefault() - setActive(a => Math.max(a - 1, 0)) - } else if (e.key === 'Enter' && filtered[active]) { - e.preventDefault() - runCommand(filtered[active]) - } - } - - const groups: { key: Command['group']; labelKey: string }[] = [ - { key: 'pages', labelKey: 'palette.groupPages' }, - { key: 'actions', labelKey: 'palette.groupActions' }, - { key: 'models', labelKey: 'palette.groupModels' }, - ] - - let flatIndex = -1 - - return ( -
{ - if (e.target === e.currentTarget) hide() - }} - > -
-
- - { - setQuery(e.target.value) - setActive(0) - }} - placeholder={t('palette.placeholder')} - aria-label={t('palette.placeholder')} - role="combobox" - aria-expanded="true" - aria-controls="command-palette-list" - aria-activedescendant={filtered[active] ? `cmd-${filtered[active].id}` : undefined} - className="h-11 w-full bg-transparent text-sm outline-none placeholder:text-muted-foreground" - /> - esc -
-
- {filtered.length === 0 && ( -

{t('palette.empty')}

- )} - {groups.map(g => { - const items = filtered.filter(c => c.group === g.key) - if (items.length === 0) return null - return ( -
-

- {t(g.labelKey)} -

- {items.map(cmd => { - flatIndex += 1 - const index = flatIndex - const isActive = index === active - const Icon = cmd.icon - return ( - - ) - })} -
- ) - })} -
-
-
- ) -} diff --git a/client/src/components/confirm-button.tsx b/client/src/components/confirm-button.tsx deleted file mode 100644 index a6cfe545271590011b6c8ae1a953a7c7692f3a40..0000000000000000000000000000000000000000 --- a/client/src/components/confirm-button.tsx +++ /dev/null @@ -1,81 +0,0 @@ -import { useEffect, useState, type ReactNode } from 'react' -import { Button } from '@/components/ui/button' -import { cn } from '@/lib/utils' -import { useI18n } from '@/i18n' - -// The dashboard's one destructive-action idiom: first click arms the button -// (label flips to "Confirm" in destructive color), second click within the -// timeout fires onConfirm, and doing nothing or pressing Escape disarms it again. -// Extracted from the hand-rolled copies on Keys and ModelDetail so Embeddings/Media -// deletes (which used to fire immediately, with no confirmation) behave the same way. -type ButtonSize = 'default' | 'xs' | 'sm' | 'lg' | 'icon' | 'icon-xs' | 'icon-sm' | 'icon-lg' -type ButtonVariant = 'default' | 'outline' | 'secondary' | 'ghost' | 'destructive' | 'link' - -export function ConfirmButton({ - onConfirm, - children, - confirmLabel, - timeout = 3000, - variant = 'ghost', - size = 'xs', - armedSize, - className, - armedClassName, - disabled, - title, - 'aria-label': ariaLabel, -}: { - onConfirm: () => void - /** Idle content: a text label or an icon. */ - children: ReactNode - /** Armed label; defaults to the shared "Confirm". */ - confirmLabel?: string - timeout?: number - variant?: ButtonVariant - size?: ButtonSize - /** Optional size swap while armed (icon buttons widen to fit the text). */ - armedSize?: ButtonSize - className?: string - armedClassName?: string - disabled?: boolean - title?: string - 'aria-label'?: string -}) { - const { t } = useI18n() - const [armed, setArmed] = useState(false) - - useEffect(() => { - if (!armed) return - const timer = window.setTimeout(() => setArmed(false), timeout) - return () => window.clearTimeout(timer) - }, [armed, timeout]) - - return ( - - ) -} diff --git a/client/src/components/copy-button.tsx b/client/src/components/copy-button.tsx deleted file mode 100644 index 88aff29ed7b464212dc691becfb3a1480460eb43..0000000000000000000000000000000000000000 --- a/client/src/components/copy-button.tsx +++ /dev/null @@ -1,44 +0,0 @@ -import { useState } from 'react' -import { Copy, Check } from 'lucide-react' -import { cn } from '@/lib/utils' -import { copyText } from '@/lib/clipboard' -import { toast } from '@/lib/toast' -import { useI18n } from '@/i18n' - -interface CopyButtonProps { - text: string - className?: string - label?: string -} - -// Small icon button that copies `text` to the clipboard and briefly shows a -// check. Shared by markdown code blocks and Playground replies. -export function CopyButton({ text, className, label = 'Copy' }: CopyButtonProps) { - const { t } = useI18n() - const [copied, setCopied] = useState(false) - return ( - - ) -} diff --git a/client/src/components/custom-weights-popover.tsx b/client/src/components/custom-weights-popover.tsx deleted file mode 100644 index 9f8f13aa56e90d5707c4910d34084ebba6ae292c..0000000000000000000000000000000000000000 --- a/client/src/components/custom-weights-popover.tsx +++ /dev/null @@ -1,114 +0,0 @@ -import { useState } from 'react' -import { SlidersHorizontal } from 'lucide-react' -import { useI18n } from '@/i18n' -import { Button } from '@/components/ui/button' -import { Popover, PopoverTrigger, PopoverContent } from '@/components/ui/popover' -import type { RoutingWeights } from '@/lib/routing' - -// Slider axes share the colors used by the score table columns. -// `tKey` is the i18n suffix under `strategies.weight*`. -const WEIGHT_AXES: { key: keyof RoutingWeights; tKey: string; color: string }[] = [ - { key: 'reliability', tKey: 'weightReliability', color: '#22c55e' }, - { key: 'speed', tKey: 'weightSpeed', color: '#3b82f6' }, - { key: 'intelligence', tKey: 'weightIntelligence', color: '#a855f7' }, -] - -// Slider popover for the 'custom' strategy. Sliders are independent (0-100) -// and the server renormalizes any vector, so we just show each axis's -// effective share live. Nothing is saved until Apply is pressed. -export function CustomWeightsPopover({ saved, onSave, saving }: { - saved: RoutingWeights - onSave: (w: RoutingWeights) => void - saving: boolean -}) { - const { t } = useI18n() - const [values, setValues] = useState(() => fromSaved(saved)) - const [dirty, setDirty] = useState(false) - - // Defensive: an older/partial server response (or a future field rename) could - // leave `saved` undefined; never let that white-screen the whole page. Fall - // back to an even split. - function fromSaved(w?: RoutingWeights): RoutingWeights { - const safe = w ?? { reliability: 1 / 3, speed: 1 / 3, intelligence: 1 / 3 } - return { - reliability: Math.round(safe.reliability * 100), - speed: Math.round(safe.speed * 100), - intelligence: Math.round(safe.intelligence * 100), - } - } - - function update(key: keyof RoutingWeights, v: number) { - setValues({ ...values, [key]: v }) - setDirty(true) - } - - function apply() { - if (sum <= 0) return - onSave({ - reliability: values.reliability / 100, - speed: values.speed / 100, - intelligence: values.intelligence / 100, - }) - setDirty(false) - } - - const sum = values.reliability + values.speed + values.intelligence - - return ( - { if (open) { setValues(fromSaved(saved)); setDirty(false) } }}> - - - {t('strategies.adjust')} - - -
-
-

{t('strategies.customWeights')}

-

- {t('strategies.customWeightsHelp')} -

-
- {WEIGHT_AXES.map(axis => { - const share = sum > 0 ? Math.round((values[axis.key] / sum) * 100) : 0 - const axisLabel = t(`strategies.${axis.tKey}`) - return ( -
-
- - - {axisLabel} - - {share}% -
- update(axis.key, Number(e.target.value))} - className="w-full cursor-pointer" - style={{ accentColor: axis.color }} - aria-label={`${axisLabel} weight`} - /> -
- ) - })} - {sum <= 0 && ( -

- {t('strategies.weightRequired')} -

- )} - -
-
-
- ) -} diff --git a/client/src/components/decision-models.test.tsx b/client/src/components/decision-models.test.tsx deleted file mode 100644 index fac7211c77e1c6a9e15fbd0032a3aaba5ca4c5e4..0000000000000000000000000000000000000000 --- a/client/src/components/decision-models.test.tsx +++ /dev/null @@ -1,106 +0,0 @@ -// @vitest-environment jsdom -// Models → Decision lists the catalog's decision models, toggles one with -// PATCH, and "Try it" runs one choice question through /api/decisions/try. -import { afterEach, beforeAll, beforeEach, expect, it, vi } from 'vitest' -import { act } from 'react' -import { createRoot, type Root } from 'react-dom/client' -import { MemoryRouter } from 'react-router-dom' -import { QueryClient, QueryClientProvider } from '@tanstack/react-query' -import { I18nProvider } from '@/i18n' -import { apiFetch } from '@/lib/api' -import type { DecisionModel } from '@/lib/decision' -import { DecisionModelsView } from './decision-models' -import { ModelsTabs } from './models-tabs' - -vi.mock('@/lib/api', () => ({ apiFetch: vi.fn() })) - -const model = (over: Partial): DecisionModel => ({ - id: 1, platform: 'cloudflare', modelId: '@cf/cloudflare/clef-flash', displayName: 'Clef Flash', priority: 0, - enabled: true, quotaLabel: 'Workers AI free daily neurons', contextWindow: 24576, - keyCount: 1, healthyKeyCount: 1, decisionsThisMonth: 4, ...over, -}) - -let root: Root -let container: HTMLDivElement -let client: QueryClient - -beforeAll(() => { (globalThis as unknown as { IS_REACT_ACT_ENVIRONMENT: boolean }).IS_REACT_ACT_ENVIRONMENT = true }) -beforeEach(() => { - client = new QueryClient({ defaultOptions: { queries: { retry: false, gcTime: 0 }, mutations: { retry: false } } }) - container = document.createElement('div'); document.body.appendChild(container); root = createRoot(container) -}) -afterEach(() => { act(() => root.unmount()); client.clear(); container.remove() }) - -async function flush() { for (let i = 0; i < 4; i++) await act(async () => { await new Promise(r => setTimeout(r, 0)) }) } -async function mount(models: DecisionModel[]) { - vi.mocked(apiFetch).mockReset().mockImplementation(async (path: string) => { - if (path === '/api/decisions/models') return models - if (path === '/api/decisions/try') { - return { - model: '@cf/cloudflare/clef-flash', provider: 'cloudflare', usage: { input_tokens: 10, output_tokens: 0 }, - answers: { answer: { type: 'choice', choice: 'code', confidence: 0.91, probabilities: { chat: 0.04, code: 0.93, reasoning: 0.03 } } }, - } - } - return {} - }) - act(() => root.render( - - - - - - )) - await flush() -} - -it('lists the decision models with usage and a no-key badge', async () => { - await mount([ - model({}), - model({ id: 2, platform: 'openrouter', modelId: 'inception/mercury-decide:free', displayName: 'Mercury Decide (free)', priority: 1, keyCount: 0, healthyKeyCount: 0, decisionsThisMonth: 0 }), - ]) - const text = container.textContent ?? '' - expect(text).toContain('Clef Flash') - expect(text).toContain('inception/mercury-decide:free') - expect(text).toContain('4 this month') - expect(text).toContain('POST /v1/decisions') - expect(text).toContain('Chat routing only uses them when the Decision routing strategy is on') - expect(container.querySelectorAll('[role="switch"]')).toHaveLength(2) -}) - -it('turns a model off with PATCH', async () => { - await mount([model({})]) - const toggle = container.querySelector('[role="switch"]') as HTMLElement - await act(async () => { toggle.click() }) - await flush() - expect(vi.mocked(apiFetch)).toHaveBeenCalledWith('/api/decisions/models/1', { method: 'PATCH', body: JSON.stringify({ enabled: false }) }) -}) - -it('runs a test decision and shows the answer with probabilities', async () => { - await mount([model({})]) - const run = [...container.querySelectorAll('button')].find(b => b.textContent === 'Decide') as HTMLButtonElement - await act(async () => { run.click() }) - await flush() - const call = vi.mocked(apiFetch).mock.calls.find(c => c[0] === '/api/decisions/try')! - expect(JSON.parse((call[1] as RequestInit).body as string)).toEqual({ - state: 'my react useEffect runs twice, why?', - question: 'Which kind of model should answer this?', - options: ['chat', 'code', 'reasoning', 'writing'], - }) - const text = container.textContent ?? '' - expect(text).toContain('Answer: code') - expect(text).toContain('91% confident') - expect(text).toContain('93%') -}) - -it('shows the empty state before the catalog syncs', async () => { - await mount([]) - expect(container.textContent).toContain('No decision models yet') -}) - -it('orders the Models tabs with Embeddings after Audio and Decision next to it', () => { - act(() => root.render( - - )) - const labels = [...container.querySelectorAll('a')].map(a => a.textContent) - expect(labels).toEqual(['Chat models', 'Image', 'Video', 'Audio', 'Embeddings', 'Decision', 'Search', 'Fusion']) -}) diff --git a/client/src/components/decision-models.tsx b/client/src/components/decision-models.tsx deleted file mode 100644 index b12143b1a3443fd1efe431ec2c0101b8d1885907..0000000000000000000000000000000000000000 --- a/client/src/components/decision-models.tsx +++ /dev/null @@ -1,243 +0,0 @@ -import { useState } from 'react' -import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query' -import { Scale } from 'lucide-react' -import { apiFetch } from '@/lib/api' -import { Switch } from '@/components/ui/switch' -import { Button } from '@/components/ui/button' -import { Input } from '@/components/ui/input' -import { Label } from '@/components/ui/label' -import { Textarea } from '@/components/ui/textarea' -import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select' -import { EmptyState } from '@/components/empty-state' -import { CardSkeleton } from '@/components/ui/skeleton' -import { PageHeader } from '@/components/page-header' -import { ModelsTabs } from '@/components/models-tabs' -import { CodeBlock } from '@/components/code-block' -import { useI18n } from '@/i18n' -import { - decisionExampleCurl, - parseOptions, - topProbabilities, - type DecisionModel, - type DecisionResult, -} from '@/lib/decision' - -const AUTO = 'auto' - -// One model from the catalog's `decisionModels` section. Decision models are -// not part of chat routing unless the Decision routing strategy is on (#1354); -// the toggle affects POST /v1/decisions and that strategy alike. -function DecisionModelCard({ - model: m, - onToggle, - pending, -}: { - model: DecisionModel - onToggle: (enabled: boolean) => void - pending: boolean -}) { - const { t } = useI18n() - return ( -
-
-
-
- {m.displayName} - {m.keyCount === 0 && ( - - {t('models.noKey')} - - )} -
-
{m.modelId}
-
- {[m.platform, m.quotaLabel].filter(Boolean).join(' ¡ ')} -
-
- - {t('decision.thisMonth', { count: m.decisionsThisMonth })} - - -
-
- ) -} - -// "Try it": one choice question over the typed text, run through the same -// chain as POST /v1/decisions. -function TryDecision({ models }: { models: DecisionModel[] }) { - const { t } = useI18n() - const [text, setText] = useState('') - const [question, setQuestion] = useState('') - const [options, setOptions] = useState('') - const [model, setModel] = useState(AUTO) - const [latency, setLatency] = useState(null) - - const run = useMutation({ - mutationFn: async (): Promise => { - const started = performance.now() - const result = await apiFetch('/api/decisions/try', { - method: 'POST', - body: JSON.stringify({ - state: text.trim() || t('decision.tryTextPlaceholder'), - question: question.trim() || t('decision.tryQuestionPlaceholder'), - options: parseOptions(options || t('decision.tryOptionsPlaceholder')), - ...(model !== AUTO ? { model } : {}), - }), - }) - setLatency(Math.round(performance.now() - started)) - return result - }, - }) - - const optionList = parseOptions(options || t('decision.tryOptionsPlaceholder')) - const answer = run.data?.answers?.answer as { choice?: string; confidence?: number; probabilities?: Record } | undefined - const enabled = models.filter(m => m.enabled) - - return ( -
-

{t('decision.tryTitle')}

-
- -