Commit Graph

133 Commits

Author SHA1 Message Date
simon-ami
e24dff279c feat(webui): add configuration presets for Claude CLI
- Add backend storage logic in `src/utils/claude-config.js` to save/load/delete presets
- Add API endpoints (`GET`, `POST`, `DELETE`) for presets in `src/webui/index.js`
- Update `public/views/settings.html` with new Presets UI card and modals
- Update `public/js/components/claude-config.js` with auto-load logic and unsaved changes protection
- Add translations (EN/ZH) for new UI elements in `public/js/store.js`
- Add integration tests in `tests/frontend/test-frontend-settings.cjs`
- Update compiled CSS

Co-Authored-By: Claude <noreply@anthropic.com>
2026-01-12 11:59:32 +01:00
copilot-swe-agent[bot]
cd594f6e82 Address code review: use constant array for proxy env vars and clean empty env
Co-authored-by: simon-ami <102378134+simon-ami@users.noreply.github.com>
2026-01-11 11:56:01 +00:00
copilot-swe-agent[bot]
8eba68e47a Add Restore Default Claude CLI button to settings page
Co-authored-by: simon-ami <102378134+simon-ami@users.noreply.github.com>
2026-01-11 11:54:13 +00:00
Badri Narayanan S
325acdba8c fix: preserve tool_use stop reason from being overwritten by finishReason
When a tool call is made, stopReason is set to 'tool_use'. However, when
finishReason: STOP arrives later, it was overwriting stopReason back to
'end_turn', breaking multi-turn tool conversations in clients like OpenCode.

Fix: Initialize stopReason to null and only set it from finishReason if
not already set. This ensures tool_use is preserved once detected.

Fixes #96

Co-Authored-By: Claude <noreply@anthropic.com>
2026-01-11 11:52:35 +05:30
Wha1eChai
14ef814c1a merge: sync with upstream/main (5xx error fallback feature) 2026-01-11 02:57:59 +08:00
Wha1eChai
bda9623f3a feat(webui): optimize CSS build system and enhance UI quality 2026-01-11 02:57:42 +08:00
Badri Narayanan S
6868bf217c Merge pull request #47 from Wha1eChai/feature/webui
feat: Add Web UI for account and quota management
2026-01-10 22:21:57 +05:30
Tiago Rodrigues
0b477c2552 feat: fallback to alternate model when max retries exceeded with 5xx errors
When all accounts fail with HTTP 500/503 errors (e.g., Google API returning
'Unknown Error' for Claude models on large conversations), the proxy now
attempts to use a fallback model if --fallback is enabled.

This enables graceful degradation when:
- All accounts are exhausted due to 5xx errors (not just rate limits)
- Claude models fail on very large conversations
- The API has temporary issues with specific models

The fallback uses the existing MODEL_FALLBACK_MAP configuration:
- claude-opus-4-5-thinking -> gemini-3-pro-high
- claude-sonnet-4-5-thinking -> gemini-3-flash

Relates to #88
2026-01-10 12:08:53 +00:00
Wha1eChai
ee6d222e4d feat(webui): add subscription tier and quota visualization
Backend:
2026-01-10 06:05:17 +08:00
Wha1eChai
369a66e8cf Merge branch 'main' into feature/webui 2026-01-10 04:46:30 +08:00
Wha1eChai
71c7c2e423 feat(webui): enhance settings UI, persistence and documentation
- Update CLAUDE.md with comprehensive WebUI architecture and API documentation
- Improve settings UI with searchable model dropdowns and visual family indicators
- Migrate usage statistics persistence to user config directory with auto-migration
- Refactor server request handling and fix model suffix logic
2026-01-10 04:22:59 +08:00
Badri Narayanan S
f1e945a7e6 change cleanSchemaForGemini to cleanSchema 2026-01-10 00:35:50 +05:30
Tiago Rodrigues
90214c43b0 fix: convert schema types to Google uppercase format (fixes #82)
The /compact command was failing with 'Proto field is not repeating,
cannot start list' error for Claude models because tool schemas were
sent with lowercase JSON Schema types (array, object, string) but
Google's Cloud Code API expects uppercase protobuf types (ARRAY,
OBJECT, STRING).

Changes:
- Add toGoogleType() function to convert JSON Schema types to Google format
- Add Phase 5 to cleanSchemaForGemini() for type conversion
- Apply cleanSchemaForGemini() for ALL models (not just Gemini) since
  all requests go through Cloud Code API which validates schema format
- Add comprehensive test suite with 10 tests covering nested arrays,
  complex schemas, and real-world Claude Code tool scenarios

Fixes #82
2026-01-09 18:36:19 +00:00
Wha1eChai
169e18402f merge: sync with origin/feature/webui after upstream merge 2026-01-09 18:23:52 +08:00
Wha1eChai
f2f0a7452e merge: integrate upstream/main (v1.2.15) into feature/webui
- Resolved conflict in src/constants.js: kept config-driven approach

- Adopted upstream 10-second cooldown default

- Added MAX_EMPTY_RESPONSE_RETRIES constant from upstream

- Incorporated new test files and GitHub issue templates
2026-01-09 18:08:45 +08:00
Wha1eChai
a914821d49 perf(webui): refactor dashboard modules and optimize API performance 2026-01-09 17:58:09 +08:00
Badri Narayanan S
4c5236d4b3 fix: filter Antigravity system prompt from model responses
- Add [ignore] tags around system instruction to prevent model from
  identifying as "Antigravity" when asked "Who are you?"
- Replace full system instruction with minimal version used by
  CLIProxyAPI/gcli2api to reduce token usage and improve response quality

Fixes #76

Co-Authored-By: Claude <noreply@anthropic.com>
2026-01-09 14:10:41 +05:30
SvDp
45755bfa18 fix: add optimistic reset for transient 429 rate limit errors
Fixes issue #71 - 'No accounts available' error when API returns 429

The Google Cloud Code API can return 429 RESOURCE_EXHAUSTED errors even
when accounts have quota available due to:
- Temporary API load/throttling
- Per-minute request limits (not per-day quota)
- Transient backend issues

This fix adds:
1. A 500ms buffer after waiting for rate limits to expire
2. Optimistic rate limit reset when all accounts appear stuck
3. Retry logic that clears rate limits and tries again

The fix works in conjunction with the server-level optimistic reset
that already exists, providing multiple layers of protection against
false 'No accounts available' errors.

Co-Authored-By: Claude <noreply@anthropic.com>
2026-01-08 21:31:50 +05:30
Wha1eChai
c9c5e7d486 feat(webui): add hot-reload account management with OAuth support 2026-01-08 23:52:31 +08:00
Badri Narayanan S
5f6ce1b97d Update daily Cloud Code endpoint to production URL
Remove sandbox subdomain from daily-cloudcode-pa endpoint.

Co-Authored-By: Claude <noreply@anthropic.com>
2026-01-08 20:24:23 +05:30
Badri Narayanan S
a696ed0872 Merge pull request #64 from BrunoMarc/fix/empty-response-retry
fix: add retry mechanism for empty API responses
2026-01-08 17:38:18 +05:30
Wha1eChai
217053839f feat(webui): Enhance dashboard, global styles, and settings module
## Dashboard Enhancements
- Add Request Volume trend chart with Chart.js line graph
  - Support Family/Model display modes for aggregation levels
  - Show Total/Today/1H usage statistics
  - Hierarchical filter dropdown with Smart select (Top 5 by 24h usage)
  - Persist chart preferences to localStorage
- Improve account health detection logic
  - Core models (sonnet/opus/pro/flash) require >5% quota to be healthy
  - Dynamic quota ring chart supporting any model family
- Unify table styles with standard-table class

## Global Style Refactoring
- Add CSS variable system for theming
  - Space color scale (950/900/850/800/border)
  - Neon accent colors (purple/green/cyan/yellow/red)
  - Text hierarchy (main/dim/muted/bright)
  - Chart palette (16 colors)
- Add unified component classes
  - .view-container for consistent page layouts
  - .section-header/.section-title/.section-desc
  - .standard-table for table styling
- Update scrollbar, nav-item, progress-bar to use theme variables

## Settings Module Extensions
- Add model mapping column in Models tab
- Enhance model selectors with family color indicators
- Support horizontal scroll for tabs on narrow screens
- Add defaultCooldownMs and maxWaitBeforeErrorMs config options

## New Module
- Add src/modules/usage-stats.js for request tracking
  - Track /v1/messages and /v1/chat/completions endpoints
  - Hierarchical storage: { hour: { family: { model: count } } }
  - Auto-save every minute, 30-day retention
  - GET /api/stats/history endpoint for dashboard chart

## Backend Changes
- Add direct account manipulation helpers (bypass AccountManager)
- Add POST /api/config/password endpoint for WebUI password change
- Auto-reload AccountManager after account operations
- Use CSS variables in OAuth callback pages

## Other
- Update .gitignore for runtime data directory
- Add i18n keys for new UI elements (EN/zh_CN)

Co-Authored-By: Claude <noreply@anthropic.com>
2026-01-08 19:04:43 +08:00
Badri Narayanan S
f34aa50ba4 Antigravity compatibility to fix antigravity usage 2026-01-08 10:24:54 +05:30
BrunoMarc
1c80c8ba52 fix: address second round code review feedback
Issues found by Claude Opus 4.5 + Gemini 3 Pro:

HIGH PRIORITY FIXES:
- Mark account rate-limited when 429 occurs during retry (was losing resetMs)
- Add exponential backoff between retries (500ms, 1000ms, 2000ms)
- Fix 5xx handling: don't pass error response to streamer, refetch instead
- Use recognizable error messages (429/401) for isRateLimitError/isAuthError

MEDIUM PRIORITY FIXES:
- Refactor while loop to for loop for clearer retry semantics
- Simplify logic flow with early returns

Code review by: Claude Opus 4.5 + Gemini 3 Pro Preview

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-07 18:21:25 -03:00
BrunoMarc
05cd80ebb5 fix: address code review feedback
- Move MAX_EMPTY_RESPONSE_RETRIES to constants.js for consistency
- Handle 429/401/5xx errors properly during retry fetch
- Use proper message ID format (crypto.randomBytes) instead of Date.now()
- Add crypto import for UUID generation

Code review by: Gemini 3 Pro Preview + Claude Opus 4.5

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-07 18:16:09 -03:00
BrunoMarc
49480847b6 fix: add retry mechanism for empty API responses
When Claude Code sends requests with large thinking_budget values,
the model may spend all tokens on "thinking" and return empty responses,
causing Claude Code to stop mid-conversation.

This commit adds a retry mechanism that:
- Throws EmptyResponseError instead of emitting fake message on empty response
- Retries up to 2 times before giving up
- Emits fallback message only after all retries are exhausted

Changes:
- src/errors.js: Added EmptyResponseError class and isEmptyResponseError()
- src/cloudcode/sse-streamer.js: Throw error instead of yielding fake message
- src/cloudcode/streaming-handler.js: Added retry loop with fallback

Tested for 6+ hours with 1,884 API requests and 88% recovery rate
on empty responses.

Fixes #61

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-07 18:11:03 -03:00
Badri Narayanan S
56e5042700 Merge branch 'main' into feature/webui 2026-01-07 02:01:23 +05:30
Badri Narayanan S
63fa90c04b set cooldown to 10 seconds 2026-01-07 00:33:42 +05:30
Badri Narayanan S
57ba5f9c1c setting cooldown back to 30 seconds 2026-01-07 00:20:22 +05:30
Badri Narayanan S
5b70b7703e Changed default cooldown time to 10 seconds 2026-01-06 22:22:32 +05:30
Badri Narayanan S
59d0a92b37 Merge pull request #55 from jgor20/fix/oauth-callback-utf8-encoding
fix(oauth): add UTF-8 encoding to callback HTML pages
2026-01-06 21:49:02 +05:30
jgor20
df9b935329 fix(auth): add UTF-8 charset to OAuth callback HTML responses
Ensure proper encoding for international characters in error and success pages
by specifying charset=utf-8 in Content-Type headers and adding meta charset tags.
2026-01-05 01:43:15 +00:00
jgor20
e29cd5fa9d refactor(auth): use NativeModuleError for native module load failures
Replace generic Error instances with NativeModuleError in loadDatabaseModule
to provide more structured error information, including rebuild status and
restart requirements. Update getAuthStatus to re-throw NativeModuleError
instances without wrapping.
2026-01-05 01:20:35 +00:00
jgor20
b90eb63f22 feat(errors): add NativeModuleError for native module version mismatches
Add a new error class to handle native module errors, including version mismatches and rebuild requirements. This supports the auto-rebuild functionality by providing structured error information for rebuild success and restart needs.
2026-01-05 01:20:28 +00:00
jgor20
69b7e130a0 refactor(auth): move clearRequireCache to utils and update import
Remove the local clearRequireCache function from database.js and import it from
utils/native-module-helper.js. Update the function call to pass require.cache
as the second parameter for proper cache clearing.
2026-01-05 01:13:55 +00:00
jgor20
2d4693b4c6 refactor(utils): rename and refactor clearModuleCache to recursively clear dependencies
The function has been renamed to clearRequireCache and updated to recursively
clear the require cache for a module and all its dependencies, preventing
cycles with a visited set. This improves reliability after rebuilding native
modules by ensuring complete cache invalidation. Removed unused createRequire
import as it's no longer needed.
2026-01-05 01:13:45 +00:00
jgor20
02ceeb2ff5 feat(utils): enhance rebuildModule error handling and logging
Add detailed stdout/stderr capture and logging for failed npm rebuild commands to improve troubleshooting. Also log successful rebuild output for debugging purposes.
2026-01-05 01:11:13 +00:00
jgor20
ff188f5cf6 fix(utils): fix cross-platform root detection in findPackageRoot
Improve the findPackageRoot function to correctly detect filesystem root on all platforms by checking if dirname returns the same path, replacing the Unix-specific '/' check.
2026-01-05 01:09:00 +00:00
jgor20
e6027ec5a6 feat: auto-rebuild native modules on Node.js version mismatch
When Node.js is updated, native modules like better-sqlite3 can become
   incompatible due to NODE_MODULE_VERSION differences. This change adds
   automatic detection and rebuild capability:

   - Add native-module-helper.js utility for detecting version errors
   - Lazy-load better-sqlite3 to catch import errors at runtime
   - Automatically run npm rebuild when version mismatch is detected
   - Clear require cache and retry loading after successful rebuild
   - Provide clear instructions if automatic rebuild fails

   Fixes the issue where users running via npx encounter module errors
   after updating Node.js.
2026-01-05 00:43:21 +00:00
Wha1eChai
85f7d3bae7 feat: Add Web UI for account and quota management
## Summary
Add an optional Web UI for managing accounts and monitoring quotas.
WebUI is implemented as a modular plugin with minimal changes to server.js (only 5 lines added).

## New Features
- Dashboard: Real-time model quota visualization with Chart.js
- Accounts: OAuth-based account management (add/enable/disable/refresh/remove)
- Logs: Live server log streaming via SSE with search and level filtering
- Settings: System configuration with 4 tabs
  - Interface: Language (EN/zh_CN), polling interval, log buffer size, display options
  - Claude CLI: Proxy connection config, model selection, alias overrides (~/.claude.json)
  - Models: Model visibility and ordering management
  - Server Info: Runtime info and account config reload

## Technical Changes
- Add src/webui/index.js as modular plugin (all WebUI routes encapsulated)
- Add src/config.js for centralized configuration (~/.config/antigravity-proxy/config.json)
- Add authMiddleware for optional password protection (WEBUI_PASSWORD env var)
- Enhance logger with EventEmitter for SSE log streaming
- Make constants configurable via config.json
- Merge with main v1.2.6 (model fallback, cross-model thinking)
- server.js changes: only 5 lines added to import and mount WebUI module

## Bug Fixes
- Fix Alpine.js $watch error in settings-store.js (not supported in store init)
- Fix "OK" label to "SUCCESS" in logs filter
- Add saveSettings() calls to settings toggles for proper persistence
- Improve Claude CLI config robustness (handle empty/invalid JSON files)
- Add safety check for empty config.env in claude-config component
- Improve config.example.json instructions with clear Windows/macOS/Linux paths

## New Files
- src/webui/index.js - WebUI module with all API routes
- public/ - Complete Web UI frontend (Alpine.js + TailwindCSS + DaisyUI)
- src/config.js - Configuration management
- src/utils/claude-config.js - Claude CLI settings helper
- tests/frontend/ - Frontend test suite

## API Endpoints Added
- GET/POST /api/config - Server configuration
- GET/POST /api/claude/config - Claude CLI configuration
- POST /api/models/config - Model alias/hidden settings
- GET /api/accounts - Account list with status
- POST /api/accounts/:email/toggle - Enable/disable account
- POST /api/accounts/:email/refresh - Refresh account token
- DELETE /api/accounts/:email - Remove account
- GET /api/logs - Log history
- GET /api/logs/stream - Live log streaming (SSE)
- GET /api/auth/url - OAuth URL generation
- GET /oauth/callback - OAuth callback handler

## Backward Compatibility
- Default port remains 8080
- All existing CLI/API functionality unchanged
- WebUI is entirely optional
- Can be disabled by removing mountWebUI() call
2026-01-04 18:35:29 +08:00
Badri Narayanan S
1628696ca1 fix: correct state destructuring and document headless mode
- Fix extractCodeFromInput destructuring: returns { code, state } not
  { code, extractedState }, so state validation was being bypassed
- Add --no-browser hint to CLI banner for discoverability
- Document --no-browser mode in README.md and CLAUDE.md
- Add test:oauth script to package.json
- Add OAuth test to run-all.cjs test suite

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2026-01-04 14:51:05 +05:30
Caixiaopig
573ba57db6 feat: add --no-browser OAuth mode for headless servers
## Feature Description

Enables adding Google accounts on remote servers without a desktop
environment (headless Linux, Docker containers, SSH sessions) where
automatic browser opening is not possible. Users can manually copy the
authorization URL to a device with a browser, complete authentication,
and paste the authorization code back.

## Usage

npm run accounts:add -- --no-browser

## Code Architecture

### New Modules

1. oauth.js - extractCodeFromInput()
   - Parses user input (full callback URL or raw authorization code)
   - Extracts code and state parameters
   - Handles OAuth error responses

2. accounts.js - addAccountNoBrowser()
   - Account addition flow for no-browser mode
   - Displays authorization URL for manual copying
   - Waits for user to paste authorization code
   - Calls extractCodeFromInput to parse input
   - Completes OAuth flow and saves account

3. tests/test-oauth-no-browser.cjs
   - 13 unit tests covering valid URLs, raw codes, error handling, edge cases

### Modified Modules

1. accounts.js - interactiveAdd()
   - Added noBrowser parameter
   - Selects addAccount or addAccountNoBrowser based on mode

2. accounts.js - main()
   - Parses --no-browser CLI argument
   - Updated help information

## User Flow

┌─────────────────────────────────────────────────────────────┐
│                   Headless Server Terminal                   │
└─────────────────────────────────────────────────────────────┘
                              │
                              ▼
            ┌─────────────────────────────────┐
            │ npm run accounts:add -- --no-browser │
            └─────────────────────────────────┘
                              │
                              ▼
            ┌─────────────────────────────────┐
            │   Display Google OAuth URL       │
            │   (manual copy required)         │
            └─────────────────────────────────┘
                              │
         ┌────────────────────┴────────────────────┐
         │                                         │
         ▼                                         ▼
┌─────────────────┐                    ┌─────────────────────┐
│  Local Browser  │                    │  Phone/Other Device │
│  Open URL       │                    │  Open URL           │
│  Google Sign-in │                    │  Google Sign-in     │
│  Authorize App  │                    │  Authorize App      │
└─────────────────┘                    └─────────────────────┘
         │                                         │
         └────────────────────┬────────────────────┘
                              │
                              ▼
            ┌─────────────────────────────────┐
            │ Browser redirects to localhost   │
            │ (page won't load - this is OK)   │
            │ Copy full URL or code parameter  │
            └─────────────────────────────────┘
                              │
                              ▼
            ┌─────────────────────────────────┐
            │ Return to server terminal        │
            │ Paste URL or authorization code  │
            └─────────────────────────────────┘
                              │
                              ▼
            ┌─────────────────────────────────┐
            │ ✓ Account added successfully     │
            └─────────────────────────────────┘

## Security Considerations

- Supports state parameter validation (when user pastes full URL)
- Warns on state mismatch but allows continuation (manual mode tolerance)
- Authorization code length validation to prevent incorrect input

## Compatibility

- Does not affect existing automatic browser OAuth flow
- All changes are additive, no modifications to upstream logic
- Easy to merge future upstream updates
2026-01-04 14:46:16 +05:30
Badri Narayanan S
141558dd62 Improve cross-model thinking handling and add gemini-3-flash fallback
- Add gemini-3-flash to MODEL_FALLBACK_MAP for completeness
- Add hasGeminiHistory() to detect Gemini→Claude cross-model switch
- Trigger recovery for Claude only when Gemini history detected
- Remove unnecessary thinking block filtering for Claude-only conversations
- Add comments explaining '.' placeholder usage
- Remove unused filterUnsignedThinkingFromMessages function

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2026-01-04 00:11:14 +05:30
Badri Narayanan S
53f8d7f6cc Add debug logging when stripping thinking blocks
🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2026-01-03 23:29:21 +05:30
Badri Narayanan S
12e427e9d5 Fix needsThinkingRecovery to require tool loop context
🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2026-01-03 23:24:52 +05:30
Badri Narayanan S
dc65499c49 Preserve valid thinking blocks during recovery
Instead of stripping all thinking blocks during thinking recovery,
now only strips invalid or incompatible blocks. Uses signature cache
to validate family compatibility for cross-model fallback scenarios.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2026-01-03 23:17:38 +05:30
Badri Narayanan S
668c7aef26 correct els if condition for state.inToolLoop 2026-01-03 22:38:46 +05:30
Badri Narayanan S
b7286059ee remove targetFamily from analyzeConversationState 2026-01-03 22:13:35 +05:30
Badri Narayanan S
602d6ca0f8 move fallback map to constants 2026-01-03 22:05:16 +05:30
Badri Narayanan S
ac9ec6b358 Signature handling for fallback 2026-01-03 22:01:57 +05:30