Compare commits

...
Author SHA1 Message Date
Finessse 9a2363d6bd chore: bump version to 0.1.5
Includes:
- Vision base64 parsing fix for OpenAI-to-Claude translation
- Memory API localhost authentication with API keys
- Global model mapping SDK hook for cross-provider aliases
- Comprehensive unit tests for auth, config, memory, and runtime
2025-12-28 10:49:06 +07:00
Finessse 352095c54c test(runtime): add unit tests for model registry, executors, and pricing
Covers model registration, quota tracking, concurrent access,
Claude/Gemini executor request prep, and pricing calculations.
2025-12-28 10:46:38 +07:00
Finessse 009fbb2a37 test(memory): add unit tests for file store, pruning, and semantic search
Covers event serialization, file operations, token-based pruning,
recency preservation, and semantic similarity scoring.
2025-12-28 10:46:21 +07:00
Finessse 7d11a35a16 test(config): add unit tests for config loading and validation
Covers YAML parsing, port validation, key sanitization, header
normalization, global model mapping lookup, and legacy migration.
2025-12-28 10:46:03 +07:00
Finessse 9cdce5e8d8 test(auth): add unit tests for Claude PKCE/token and Codex JWT parsing
Covers PKCE verifier/challenge generation, token expiry detection,
refresh timing, and JWT claim extraction for Claude and Codex auth.
2025-12-28 10:45:45 +07:00
Finessse 7d9a114bf8 feat(config): add global model mapping hook for SDK handlers
Adds GlobalModelMapperFunc type and hook to SDKConfig, allowing
SDK-level handlers to access Config-level global model mappings.
This enables cross-provider model aliasing (e.g., gpt-4o → claude-sonnet).
2025-12-28 10:45:26 +07:00
Finessse 82f02c6d59 feat(management): allow API key auth for localhost management endpoints
Enables configured API keys to authenticate management API requests
from localhost, making it easier to use tools like curl for local testing.
2025-12-28 10:45:00 +07:00
Finessse 096daa30a7 fix(translator): correct base64 image parsing for OpenAI-to-Claude translation
Split on ";base64," instead of "," to properly extract media type and data
from data URLs. Also adds support for URL-based images (https://).
2025-12-28 10:44:35 +07:00
Finessse 5b11c113fb fix(ci): remove README_CN.md from goreleaser config 2025-12-28 08:38:37 +07:00
Finessse b127b674e9 sync: squash merge upstream CLIProxyAPI v6.6.62
Synced with upstream v6.6.61-v6.6.62 changes:
- Added usage export/import endpoints
- Added normalizeCodexKey function
- Updated BootstrapRetries type
- Enhanced model definitions and antigravity config

Preserved ProxyPilot customizations:
- Kept CompressionConfig for long-context sessions
- Kept Kiro, GitHub Copilot, Amazon Q model definitions
- Kept shouldKeepProviderOrder for Gemini-3 provider stability
- Kept advanceProviderCursor pattern (separate cursor advancement)
- Removed AmpCode integration (not used)

Version: 0.1.4
2025-12-28 08:32:11 +07:00
Finessse 6163489aca fix(ci): resolve release workflow failures
1. Add permissions: contents: write to release.yml to fix 403 error
2. Add before hook to remove .syso files that break ARM64 cross-compile
3. Skip Windows ARM64 build (not commonly used)
2025-12-28 07:32:40 +07:00
Finessse 0b12eafe6c chore: bump version to 0.1.3 2025-12-28 07:17:57 +07:00
Finessse e89a0f5fcb chore(scripts): add UI state initialization script
PowerShell script to create initial ui-state.json with default
settings in %LOCALAPPDATA%\ProxyPilot.
2025-12-28 07:12:43 +07:00
Finessse c7d06a3ace chore(scripts): add auth directory migration script
PowerShell script to migrate auth files from legacy ~/.cli-proxy-api
to new %LOCALAPPDATA%\ProxyPilot\auth location.
2025-12-28 07:12:33 +07:00
Finessse 81848357c4 feat: auto-register translators on startup
Import translator package with side effects to register all
built-in format translators at initialization.
2025-12-28 07:11:28 +07:00
Finessse 74319cd89e feat(embedded): register SDK providers and configure logging
Register token store and config providers at startup, apply default
auth-dir resolution, and configure log output from config.
2025-12-28 07:10:56 +07:00
Finessse 018cba3491 feat(dashboard): add logo route and improve auth key handling
Add /logo.png route for branding and prefer server instance password
over env var for management key injection.
2025-12-28 07:10:36 +07:00
Finessse a6da22f391 refactor(desktopctl): use util.ResolveAuthDir for path handling
Simplify auth directory resolution by delegating to the centralized
util package.
2025-12-28 07:10:17 +07:00
Finessse 8ae32d440a feat(util): add cross-platform default auth directory helper
Add DefaultAuthDir() that returns OS-appropriate auth directory path
following XDG spec on Linux and LOCALAPPDATA on Windows.
2025-12-28 07:10:01 +07:00
Finessse 26a17c7456 security(webui): remove hardcoded management key from template 2025-12-28 07:09:44 +07:00
Finessse d0c29cb584 feat(api): wire translator endpoints to router
Add translation API routes for matrix, check, docs, score, and
compare endpoints.
2025-12-28 07:09:11 +07:00
Finessse 0f5836e14b refactor(translator): enhance registry with new capabilities
Update registry to support middleware, caching, pooling, and
resilience features integration.
2025-12-28 07:09:01 +07:00
Finessse e96d568671 feat(api): add translator HTTP endpoints
Add REST API handlers for translation matrix, format checking,
documentation, quality scoring, and structure comparison.
2025-12-28 07:08:51 +07:00
Finessse 21125722cd test(translator): add lazy loading tests
Add tests for lazy translator registration and loading behavior.
2025-12-28 07:08:41 +07:00
Finessse 3e47344da9 feat(translator): add testing utilities
Add mock translators and test helpers for translator unit testing.
2025-12-28 07:08:31 +07:00
Finessse 31e52f26e8 feat(translator): add translation quality scoring
Add quality assessment for translations with structural comparison
and scoring metrics.
2025-12-28 07:08:21 +07:00
Finessse 3cf08319d4 feat(translator): add documentation generation
Add markdown docs, mermaid diagram, and summary generation for
registered translators.
2025-12-28 07:08:12 +07:00
Finessse b1a4bede65 feat(translator): add API helper functions
Add compatibility matrix, format listing, and translation info
query APIs for registry introspection.
2025-12-28 07:07:57 +07:00
Finessse 6cfd29ff1d feat(translator): add middleware pipeline support
Add pre/post translation middleware for payload transformation,
logging, and conditional processing.
2025-12-28 07:07:47 +07:00
Finessse 2874e52a2a feat(translator): add resilience and recovery strategies
Add translation recovery with direct hit, fallback chain, and
auto-detect strategies for robust format conversion.
2025-12-28 07:07:35 +07:00
Finessse 40c46acc08 feat(translator): add fallback chain support
Add multi-hop translation fallback chains for indirect format
conversions when direct translators are unavailable.
2025-12-28 07:07:24 +07:00
Finessse d8eb83c8fd feat(translator): add automatic format detection
Add payload format auto-detection for OpenAI, Claude, Gemini, and
Antigravity formats based on JSON structure analysis.
2025-12-28 07:07:14 +07:00
Finessse 5de9cfa8ea feat(translator): add translation benchmarking utilities
Add benchmark suite for measuring translation performance including
P50/P95/P99 latency percentiles and comparison tools.
2025-12-28 07:07:04 +07:00
Finessse 185dcc46dd feat(translator): add batch translation processing
Add batch processor for translating multiple payloads concurrently
with configurable worker count and progress callbacks.
2025-12-28 07:06:52 +07:00
Finessse 705039b410 feat(translator): add translator instance pooling
Add object pooling for translator instances to reduce allocation
overhead and improve concurrent translation performance.
2025-12-28 07:06:40 +07:00
Finessse 782e3f26a5 feat(translator): add translation result caching
Add LRU cache for translation results with configurable TTL and size.
Improves performance by avoiding redundant translations for identical
payloads.
2025-12-28 07:06:30 +07:00
Finessse 25e73af418 feat(translator): add request/response validation utilities
Add validation helpers for translator payloads including JSON schema
validation, required field checks, and format-specific validators.
2025-12-28 07:06:07 +07:00
Finessse ceec470273 refactor: consolidate tray app into single binary with embedded engine
This release removes the need for a separate proxypilot-engine.exe binary.
The ProxyPilot.exe tray app now runs the proxy service in-process using
the cliproxy SDK builder pattern.

Changes:
- Add internal/embedded package for in-process server management
- Update desktopctl to support embedded mode alongside subprocess mode
- Clean up vestigial -exe flag and engine binary references
- Update package scripts to build only single binary
- Bump version to 0.1.2

The embedded engine in cmd/cliproxytray was already implemented; this
commit cleans up the remaining references to the old two-binary architecture.
2025-12-28 04:25:45 +07:00
Finessse bbcf06f163 chore: bump version to 0.1.1
Prepare release v0.1.1 with new features:
- Model-specific pricing for cost estimation
- Auto-update with download, verify, and install flow
- Cost savings display in Analytics
- Logo branding assets
2025-12-28 03:42:33 +07:00
Finessse 00c64b3bcb build(assets): update compiled webui assets
Rebuild embedded webui assets with latest changes including
cost savings display, auto-update flow, and component updates.
2025-12-28 03:21:23 +07:00
Finessse 24467aa957 refactor(webui): update frontend components and configuration
Update Header with logo display, improve AgentSetup and ProviderLogins
components. Update App.tsx and index.html for better structure.
Adjust Vite config for development.
2025-12-28 03:21:04 +07:00
Finessse 68cf672e6b refactor(integrations): improve agent detection and configuration
Update CLI agent detection and configuration handling. Improve
integration handlers for better error handling and status reporting.
2025-12-28 03:20:46 +07:00
Finessse d9f67d1a8a chore(assets): add ProxyPilot logo
Add logo.png to webui public folder and desktop app assets
for consistent branding across web and desktop interfaces.
2025-12-28 03:20:28 +07:00
Finessse 7e8863b45b feat(webui): add full auto-update flow to Settings
Add complete update workflow with stages: checking, available, downloading,
ready, installing. Show download progress bar, version info, and release
notes. Add Install & Restart button for applying updates.
2025-12-28 03:20:10 +07:00
Finessse 2ccbba05e5 feat(webui): add cost savings display to Analytics
Add Cost Savings hero card showing proxy cost vs direct API cost with
visual comparison bars. Add Cost column to Model Breakdown table.
Display savings percentage and total amount saved.
2025-12-28 03:19:51 +07:00
Finessse 5c60aea115 feat(api): add update management endpoints
Add handlers for update status, download, verify, and install operations.
Register new routes: GET /updates/status, POST /updates/download,
POST /updates/verify, POST /updates/install. Track download progress
with background goroutine and mutex-protected state.
2025-12-28 03:19:34 +07:00
Finessse 0a5b2f1fc0 feat(updates): add cross-platform update installation
Add install.go with archive extraction (zip, tar.gz) and binary replacement
logic. Windows uses batch script for safe replacement of running executable.
Unix directly replaces binary with backup. Includes cleanup for old versions.
2025-12-28 03:19:13 +07:00
Finessse 8dd850241a feat(updates): add download and signature verification
Add DownloadUpdate function to download platform-specific binaries from
GitHub releases with progress tracking. Add verify.go with SHA256 checksum
verification for downloaded files. Fix GitHub repo URL and add 404 handling
for repositories without releases.
2025-12-28 03:18:55 +07:00
Finessse 3ee272620e feat(usage): add model-specific pricing for cost estimation
Add comprehensive pricing table for Claude, GPT, Gemini, Qwen, and DeepSeek
models with per-million-token rates. Update cost calculation to use actual
model pricing instead of hardcoded estimates. Add new cost fields to
UsageStats for tracking actual cost, direct API cost, and savings.
2025-12-28 03:18:35 +07:00
Finessse 6a1038871f docs: update build documentation and CI workflow
- Update desktop app build process docs
- Clean up release workflow
- Update installer configuration
2025-12-28 00:07:12 +07:00
Finessse 2c09998780 build(assets): update compiled webui assets 2025-12-28 00:06:53 +07:00
Finessse 9c91f3c511 feat(desktop): add WebView2 bindings for new features
- Add agent detection and configuration bindings
- Add update check and download bindings
- Add usage and request monitoring bindings
- Update useProxyContext with new desktop API hooks
2025-12-28 00:06:27 +07:00
Finessse 281ac2e0ba refactor(backend): improve API and internal services
- Add request logging middleware
- Update integrations handler
- Improve desktopctl with browser opening
- Add client model interfaces
- Enhance usage logger plugin
- Update SDK handlers
2025-12-28 00:06:07 +07:00
Finessse 2e7b68922a feat(engine): add engine management and config editor
- Add engine.go for proxy engine lifecycle management
- Add ConfigEditor component for runtime configuration
2025-12-28 00:05:35 +07:00
Finessse bb562e3d36 feat(updates): add auto-update check system
- Add updates package with GitHub releases check
- Add updates management endpoint
2025-12-28 00:05:15 +07:00
Finessse 3f8366de1a feat(usage): add usage statistics dashboard
- Update usage management endpoint
- Add UsageStats component for token/cost tracking
2025-12-28 00:04:50 +07:00
Finessse f4a2ac6962 feat(monitor): add request monitoring dashboard
- Add requests management endpoint
- Add RequestMonitor component for real-time request tracking
2025-12-28 00:04:28 +07:00
Finessse d014612bd6 refactor(dashboard): update components for Flight Deck styling
- Refactor EngineControl with new status indicators
- Update Integrations with provider cards
- Enhance LogsViewer with better filtering and display
- Improve MemoryManager with modern UI patterns
- Update ModelMappings with collapsible sections
- Refactor ProviderLogins with OAuth flow improvements
- Update SemanticMemory styling
2025-12-28 00:03:57 +07:00
Finessse f7b02ce207 feat(ui): Flight Deck dashboard redesign
- Add icon rail navigation with collapsible panels
- Add Header component with engine status and controls
- Add StatusBar component with real-time metrics
- Add SettingsPanel for configuration management
- Add new UI components: icon-rail, provider-card, status-indicator
- Update badge, button, card components with Flight Deck styling
- Add CSS custom properties for consistent theming
- Remove react-grab.css (unused)
2025-12-28 00:03:34 +07:00
Finessse 9e2f45fcf5 feat(agents): add CLI agent detection with auto-configure/reset
- Add DetectCLIAgents for Claude Code, Codex CLI, Aider, Gemini CLI, Droid CLI
- Add ConfigureCLIAgent to auto-configure agents via shell profile or config files
- Add UnconfigureCLIAgent to reset/remove ProxyPilot configuration
- Add AgentSetup component with Configure/Reset buttons
- Shell profile agents show "restart shell" message, config file agents don't
2025-12-28 00:02:58 +07:00
Finessse 95eb896831 docs: add desktop app build process documentation 2025-12-27 07:16:21 +07:00
Finessse 660c0f7656 fix: use multi-resolution icon (16-256px) for proper Windows display 2025-12-27 07:13:13 +07:00
Finessse aca1f54478 fix: use CopyFile instead of deprecated FileCopy in installer 2025-12-27 07:05:51 +07:00
Finessse c10abab37c feat: embed ProxyPilot logo in all executables
- Add versioninfo.json for goversioninfo in tray and server
- Generate resource.syso files with embedded icon
- Update ico.go to embed actual icon.ico for tray
- All exes now show ProxyPilot logo in File Explorer
2025-12-27 07:05:13 +07:00
FinessseandClaude Opus 4.5 d71582d7d8 refactor(tray): simplify system tray to minimal menu
Before (nested submenus):
- Status
- Open Dashboard
- Engine → Start, Stop, Restart
- Connect → Copy ANTHROPIC_BASE_URL, Copy OPENAI_BASE_URL
- Settings → Launch on login, Auto-start proxy
- Tools → Open Logs, Copy Diagnostics, Advanced UI
- Updates → Check for Updates, Update ProxyPilot
- Quit

After (flat, minimal):
- Open Dashboard
- Start/Stop (toggle)
- Copy API URL
- Quit

Advanced settings moved to dashboard UI.

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

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-27 06:41:05 +07:00
Finessse f3bc99cc35 fix: add ProxyPilot logo to installer and shortcuts 2025-12-27 06:35:48 +07:00
FinessseandClaude Opus 4.5 923f3f68b7 ci: add release workflow with Inno Setup installer
- Add GitHub Actions workflow to build releases
- Update Inno Setup script for new binary structure
- Builds single-file installer (11MB) containing:
  - ProxyPilot.exe (tray + dashboard)
  - proxypilot-engine.exe (proxy server)
  - config.example.yaml

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

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-27 06:25:31 +07:00
FinessseandClaude Opus 4.5 0e03289627 feat(tray): embed WebView2 dashboard directly in tray app
- Embed React dashboard assets in tray binary
- Open dashboard in WebView2 window from tray
- Remove need for separate proxypilotui.exe
- Simplifies distribution to just ProxyPilot.exe + proxypilot-engine.exe

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

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-27 06:17:21 +07:00
FinessseandClaude Opus 4.5 7631297204 feat(webui): refactor dashboard with tabbed navigation and theme toggle
- Add 5-tab navigation: Engine, Auth, Routing, Memory, Logs
- Add dark/light/system theme toggle with persistence
- Extract 7 modular dashboard components:
  - EngineControl: proxy start/stop/restart controls
  - ProviderLogins: OAuth buttons for 6 AI providers
  - Integrations: CLI agent detection and configuration
  - ModelMappings: global model mapping CRUD
  - MemoryManager: session memory management
  - SemanticMemory: Ollama embeddings and namespaces
  - LogsViewer: diagnostics and log viewing
- Add centralized state management with ProxyProvider context
- Reduce App.tsx from 1,734 lines to 351 lines

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

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-27 06:03:16 +07:00
FinessseandClaude Opus 4.5 3e3d0bf933 chore: restore logo to README
🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-27 04:09:10 +07:00
FinessseandClaude Opus 4.5 c6d8e6abb1 chore: gitignore static and test folders
- Remove logo and test files from tracking
- Keep files locally but exclude from repo

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

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-27 04:00:38 +07:00
FinessseandClaude Opus 4.5 9fb39ff966 docs: fix Factory.ai and Anthropic research links
🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-27 03:51:15 +07:00
FinessseandClaude Opus 4.5 e08235b4d1 docs: add CLIProxyAPI and VibeProxy to credits
🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-27 03:50:09 +07:00
FinessseandClaude Opus 4.5 7a5fd20272 docs: add credits section for research inspirations
Credits Factory.ai and Anthropic for their research on context
compression and building effective agents.

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

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-27 03:49:06 +07:00
FinessseandClaude Opus 4.5 b37551edd8 docs: update to GPT-5.2 models
🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-27 03:46:18 +07:00
FinessseandClaude Opus 4.5 39c7c6760a docs: add 4.5 to Haiku model name
🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-27 03:44:08 +07:00
FinessseandClaude Opus 4.5 7e13a0dfdd docs: update Gemini models to 3 Pro/Flash
🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-27 03:42:34 +07:00
FinessseandClaude Opus 4.5 9394166777 docs: restyle README in VibeProxy format
- Cleaner centered branding with star badge
- Bold tagline with concise value prop
- GitHub tip callout for latest models
- Emoji bullet feature list
- Simplified sections
- Setup guide links

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

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-27 03:39:47 +07:00
FinessseandClaude Opus 4.5 a6ea590cce docs: add missing features to README
Added:
- Tool Calling Repair
- Extended Thinking support
- Quota Auto-Switch
- Usage Statistics
- 4 CLI Tools table
- Tool Integrations section (Droid, Cursor, Continue)
- Factory Droid configuration example

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

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-27 03:37:13 +07:00
FinessseandClaude Opus 4.5 7329675592 docs: update README with comprehensive feature list
- Added 7 auth providers, long-context features, desktop app details
- Added supported providers table with auth methods
- Simplified installation and quick start sections
- Added tool configuration examples (Claude Code, Codex CLI)

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

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-27 03:29:38 +07:00
FinessseandClaude Opus 4.5 cab21a5089 refactor(auth): remove commented-out ErrTokenExpired
Removed dead code (commented-out ErrTokenExpired error constant) from
both claude/errors.go and codex/errors.go.

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

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-27 03:01:27 +07:00
FinessseandClaude Opus 4.5 c4701abb00 refactor(misc): remove duplicate embed import
Removed redundant `_ "embed"` import as `embed` is already imported
and used for embed.FS.

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

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-27 02:50:14 +07:00
FinessseandClaude Opus 4.5 7b3ef0ea43 refactor(errors): remove unused error constructors
Removed 5 unused helper functions: BadRequest(), Unauthorized(),
Forbidden(), NotFound(), InternalServerError(). Also removed the
now-unused net/http import.

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

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-27 02:50:05 +07:00
FinessseandClaude Opus 4.5 5a9c652f48 docs: remove stale UI spec documentation
Remove desktop-ui-spec.md and windows-ui-spec.md which reference
deleted cliproxyctl CLI that no longer exists.

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

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-27 02:35:13 +07:00
FinessseandClaude Opus 4.5 715c71830e refactor(auth): remove unused ProjectSelectionError type
Type was defined but never used anywhere in codebase.
Keep EmailRequiredError which is actively used.

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

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-27 02:35:02 +07:00
FinessseandClaude Opus 4.5 aab8294d96 refactor(misc): remove commented dead code in codex_instructions.go
Remove unused lastReviewPrompt variable and associated dead code block.

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

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-27 02:34:53 +07:00
FinessseandClaude Opus 4.5 59e947e8a8 chore: add long-context-features.md to gitignore exceptions
🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-27 02:14:04 +07:00
FinessseandClaude Opus 4.5 2617902571 docs: add long-context features documentation
Document Factory.ai context compression and Anthropic agentic harness
implementations with architecture diagrams and configuration reference.

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

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-27 02:13:55 +07:00
FinessseandClaude Opus 4.5 270a176925 test(api): add integration tests for long-context features
Test context compression adapter and harness middleware work together
without conflicts.

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

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-27 02:13:47 +07:00
FinessseandClaude Opus 4.5 f4e7d5b92e test(middleware): add session storage test for agentic harness
Add E2E test for session-based harness file detection.

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

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-27 02:13:39 +07:00
FinessseandClaude Opus 4.5 eb84cbe543 refactor(middleware): remove Puppeteer MCP detection from harness
Removed feature not present in original Factory.ai or Anthropic sources.
Keep implementation pure to research references.

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

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-27 02:13:31 +07:00
FinessseandClaude Opus 4.5 e71648f4b6 feat(api): register AgenticHarnessMiddleware in server routes
Enable Anthropic-style INITIALIZER/CODING mode prompts for long-running
agentic clients.

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

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-27 02:13:20 +07:00
FinessseandClaude Opus 4.5 1b5bd183b9 feat(cliproxy): wire up context compression in service startup
Add coreManagerWrapper to adapt auth manager types and initialize
LLM-based summarization after server creation.

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

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-27 02:13:09 +07:00
FinessseandClaude Opus 4.5 095718e31e feat(middleware): add InitSummarizerWithAuthManager for compression setup
Enables Factory.ai-style LLM summarization by wiring the summarizer
executor to the core auth manager.

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

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-27 02:12:46 +07:00
FinessseandClaude Opus 4.5 abe7597d99 test(memory): add unit tests for ManagerAuthAdapter
12 tests covering adapter behavior, pipeline execution, and edge cases.

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

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-27 02:12:37 +07:00
FinessseandClaude Opus 4.5 01cd7942b5 feat(memory): add CoreManagerExecutor interface and ManagerAuthAdapter
Bridge type differences between coreauth.Manager and memory.AuthExecutor
to enable LLM-based context compression through the auth pipeline.

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

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-27 02:12:29 +07:00
FinessseandClaude Opus 4.5 b669cfa650 docs(readme): remove GitHub stars badge, keep X follow
🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-27 01:23:31 +07:00
FinessseandClaude Opus 4.5 c53fabd3a8 fix(readme): correct X profile link
🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-27 01:21:50 +07:00
FinessseandClaude Opus 4.5 4ceed0428b docs(readme): add X/Twitter follow badge
🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-27 01:20:44 +07:00
FinessseandClaude Opus 4.5 462856639c fix: use transparent logo (remove white background)
🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-27 01:19:36 +07:00
FinessseandClaude Opus 4.5 b75ef23d5c fix(readme): use relative path for logo
Works for both public and private repos

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

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-27 01:17:52 +07:00
FinessseandClaude Opus 4.5 8e236dd69d feat: add ProxyPilot logo
- Add icon.png for README header
- Update .gitignore to track the logo

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

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-27 01:16:00 +07:00
FinessseandClaude Opus 4.5 20146a1f7d fix(license): update copyright holders
- Fix malformed date "2025.9-present"
- Add Finesssee as ProxyPilot fork maintainer

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

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-27 01:08:17 +07:00
FinessseandClaude Opus 4.5 b5b6d65e79 docs(readme): redesign with vibeproxy-style formatting
- Centered logo and badges
- Catchy tagline "Stop juggling API keys"
- Emoji-styled feature list
- Latest models callout section
- Cleaner installation/usage sections

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

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-27 01:02:52 +07:00
FinessseandClaude Opus 4.5 12aa47b91c refactor(proxypilotui): modernize asset embedding
- Replace inline HTML/CSS/JS with Vite-bundled React app
- Embed compiled assets from webui build
- Simplify index.html to load external JS/CSS

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

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-27 00:53:59 +07:00
FinessseandClaude Opus 4.5 5ce65eed85 refactor(webui): improve App.tsx code quality
- Add downloadBlob helper to reduce duplication
- Refactor export functions to use helper
- Fix model mapping edit to use controlled React state
  instead of DOM getElementById manipulation

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

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-27 00:53:50 +07:00
FinessseandClaude Opus 4.5 e75e5716e9 refactor(webui): remove debug console.log
🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-27 00:53:41 +07:00
FinessseandClaude Opus 4.5 d519feeba1 refactor(webui): cleanup unused component exports
- badge.tsx: remove unused badgeVariants export
- button.tsx: remove unused buttonVariants export
- card.tsx: remove unused CardAction, CardFooter components

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

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-27 00:53:33 +07:00
FinessseandClaude Opus 4.5 f1f8e2c1a6 refactor(webui): remove unused UI components
Delete input.tsx, progress.tsx, tabs.tsx - none were imported
or used anywhere in the codebase.

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

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-27 00:53:23 +07:00
FinessseandClaude Opus 4.5 bd893efee9 docs: add SECURITY.md
Vulnerability reporting policy with:
- Supported versions
- Reporting methods (GitHub advisory, email)
- Response timeline expectations
- Responsible disclosure guidelines

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

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-27 00:53:12 +07:00
FinessseandClaude Opus 4.5 18757ebe7c docs: add CONTRIBUTING.md
Guidelines for:
- Development environment setup
- Running tests
- Code style (go fmt, go vet)
- Pull request process
- Issue reporting
- Third-party provider integrations

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

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-27 00:53:03 +07:00
FinessseandClaude Opus 4.5 09e96a45ca docs(readme): rebrand to ProxyPilot
Complete rewrite with:
- ProxyPilot branding and badges
- Feature list and architecture overview
- Quick start and configuration guide
- API endpoints documentation

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

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-27 00:52:54 +07:00
FinessseandClaude Opus 4.5 8e729276b0 fix(license): correct copyright date typo
Changed "2025-2005.9" to "2024-2025"

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

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-27 00:52:45 +07:00
FinessseandClaude Opus 4.5 1a1c85f043 chore: cleanup and remove Letta integration
- Remove internal/letta/ package (unused memory bridge)
- Remove Letta references from run.go and start-cliproxy.ps1
- Move docker-build scripts to scripts/
- Move TROUBLESHOOTING_ANTIGRAVITY.md and WARP.md to docs/

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

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-26 07:13:19 +07:00
FinessseandClaude Opus 4.5 9e187cd08f feat(droid): fix Droid CLI integration to use settings.json
- Update droid.go to use ~/.factory/settings.json (runtime config)
- Add /healthz endpoint for Droid health checks
- Add root-level API routes (/responses, /messages, etc.)
- Fix detection in proxypilotui to check settings.json
- Use thinking models with provider: "openai" for Responses API

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

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-26 06:52:08 +07:00
FinessseandClaude Opus 4.5 a8c389ffa6 ci: add upstream sync check workflow
Add GitHub Actions workflow to check for upstream
repository updates.

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

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-26 05:11:17 +07:00
FinessseandClaude Opus 4.5 999529c154 fix(webui): minor App.tsx updates
Minor updates to web UI App component.

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

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-26 05:11:05 +07:00
FinessseandClaude Opus 4.5 355932e138 docs(memory): update memory system documentation
Update docs to reflect token-aware compression
and LLM summarization features.

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

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-26 05:10:54 +07:00
FinessseandClaude Opus 4.5 d3466b31af fix(sdk): update stream forwarder for compression
Update stream forwarder to work with token-aware
compression middleware.

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

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-26 05:10:43 +07:00
FinessseandClaude Opus 4.5 f19594e490 fix(sdk): update handlers for compression support
Update SDK handlers to integrate with token-aware
compression middleware.

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

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-26 05:10:33 +07:00
FinessseandClaude Opus 4.5 946cc75e71 feat(ui): add agent detection and model mapping bindings
Add WebView2 JavaScript bindings:
- pp_detect_agents: List installed CLI agents
- pp_configure_agent: Configure agent for ProxyPilot
- pp_get_model_mappings: Get global mappings
- pp_save_model_mappings: Save global mappings

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

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-26 05:10:21 +07:00
FinessseandClaude Opus 4.5 93c0b8703c chore(ui): remove React bundle, simplify embed
Remove bloated React bundle (~500KB) in favor of
simple embedded HTML (~15KB). Update embed.go to
only include index.html and vite.svg.

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

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-26 05:10:10 +07:00
FinessseandClaude Opus 4.5 0dda5a01a2 refactor(ui): redesign desktop UI with ProxyPal style
Complete rewrite with ProxyPal-inspired design:
- KPI tiles (Saved, Requests, Tokens, Success)
- Provider grid with OAuth buttons
- CLI Agents section with status indicators
- Model Mappings editor
- Dark/light theme toggle

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

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-26 05:09:59 +07:00
FinessseandClaude Opus 4.5 572792f2c6 feat(cmd): add --detect-agents and --setup-* flags
Add CLI flags for agent detection and configuration:
- --detect-agents: List installed CLI agents
- --setup-claude: Configure Claude Code
- --setup-codex: Configure Codex CLI
- --setup-droid: Configure Factory Droid

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

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-26 05:09:45 +07:00
FinessseandClaude Opus 4.5 12ac05a22f feat(cmd): add CLI agent auto-configuration
Generate config files to point CLI agents at ProxyPilot:
- Claude Code: ~/.claude/settings.json
- Codex CLI: ~/.codex/config.toml
- Factory Droid: ~/.factory/config.json

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

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-26 05:09:34 +07:00
FinessseandClaude Opus 4.5 af37fbf2e0 feat(cmd): add CLI agent detection
Detect installed CLI agents (Claude Code, Codex, Droid, Gemini)
by checking binaries and config files.

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

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-26 05:09:23 +07:00
FinessseandClaude Opus 4.5 f52bc59b2e docs(config): add model mappings example
Add global-model-mappings section to config.example.yaml
showing smart/rush/think aliases.

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

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-26 05:09:11 +07:00
FinessseandClaude Opus 4.5 5cf7518f26 feat(api): add model mappings management handler
Add API handler for managing global model mappings
stored in ~/.proxypilot/model-mappings.json.

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

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-26 05:09:00 +07:00
FinessseandClaude Opus 4.5 6767188a8e feat(config): add global model mappings support
Add GlobalModelMapping struct and LookupGlobalModelMapping()
for routing friendly model names to actual models.

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

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-26 05:08:49 +07:00
FinessseandClaude Opus 4.5 7cbbf954d6 feat(api): register harness management routes
Add route registration for harness and model mappings
management endpoints.

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

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-26 05:08:36 +07:00
FinessseandClaude Opus 4.5 6e8f055816 feat(api): add harness management API endpoints
Add CRUD operations for harness files:
- GET /v0/management/harness/files - list files
- GET /v0/management/harness/file - read file
- PUT /v0/management/harness/file - write file
- GET /v0/management/harness/export - export all
- POST /v0/management/harness/import - import files

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

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-26 05:08:26 +07:00
FinessseandClaude Opus 4.5 87dbbedd4b feat(middleware): enhance agentic harness prompts
Improve INITIALIZER and CODING prompts based on Anthropic guidance:
- Structured feature_list.json format
- GET BEARINGS workflow for coding mode
- Puppeteer MCP detection for browser testing
- Session-aware state detection

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

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-26 05:08:15 +07:00
FinessseandClaude Opus 4.5 a84e59f31c feat(config): add CompressionConfig struct
Add configuration options for token-aware compression:
- Token threshold percentage
- Reserved output tokens
- LLM summary enable/disable

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

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-26 05:08:01 +07:00
FinessseandClaude Opus 4.5 ca06786892 feat(executor): add token counting helper functions
Add utilities for estimating token counts using tiktoken
to support token-aware compression decisions.

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

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-26 05:07:52 +07:00
FinessseandClaude Opus 4.5 25dd8b2087 feat(middleware): add token-aware context compression
Implement proactive compression based on token count rather than bytes:
- Uses tiktoken for accurate token estimation
- Compresses at 75% threshold before hitting limits
- Reserves tokens for output generation

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

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-26 05:07:42 +07:00
FinessseandClaude Opus 4.5 57d5b4b1b1 feat(memory): add anchored summary with LLM support
Update file_store.go with BuildAnchoredSummaryWithLLM() for
generating context-aware summaries during compression.

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

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-26 05:07:32 +07:00
FinessseandClaude Opus 4.5 f80e1bb068 feat(memory): add LLM-based structured summarization
Add structured summary generation for context compression:
- structured_summary.go: Types and parsing for summaries
- summarizer.go: LLM summarization logic
- summarizer_executor.go: Routes to LLM pipeline

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

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-26 05:07:22 +07:00
RCD d68f32f00e Merge pull request #2 from Finesssee/sync-cliproxiapiplus
feat: sync with CLIProxyAPIPlus
2025-12-26 02:13:50 +07:00
199 changed files with 35339 additions and 5985 deletions
+1 -2
View File
@@ -13,8 +13,6 @@ Dockerfile
docs/*
README.md
README_CN.md
MANAGEMENT_API.md
MANAGEMENT_API_CN.md
LICENSE
# Runtime data folders (should be mounted as volumes)
@@ -32,3 +30,4 @@ bin/*
.agent/*
.bmad/*
_bmad/*
_bmad-output/*
+7
View File
@@ -7,6 +7,13 @@ assignees: ''
---
**Is it a request payload issue?**
[ ] Yes, this is a request payload issue. I am using a client/cURL to send a request payload, but I received an unexpected error.
[ ] No, it's another issue.
**If it's a request payload issue, you MUST know**
Our team doesn't have any GODs or ORACLEs or MIND READERs. Please make sure to attach the request log or curl payload.
**Describe the bug**
A clear and concise description of what the bug is.
+156
View File
@@ -0,0 +1,156 @@
name: check-upstream
on:
schedule:
# Run daily at 08:00 UTC (like vibeproxy)
- cron: '0 8 * * *'
workflow_dispatch: # Allow manual trigger
concurrency:
group: check-upstream
cancel-in-progress: true
permissions:
contents: write
pull-requests: write
jobs:
check-update:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Get current version
id: current
run: |
# Extract current CLIProxyAPI version from go.mod or a version file
# For now, we'll check the latest commit from upstream
CURRENT_SHA=$(git ls-remote https://github.com/router-for-me/CLIProxyAPI.git HEAD | cut -f1)
echo "sha=$CURRENT_SHA" >> $GITHUB_OUTPUT
- name: Get upstream release info
id: upstream
run: |
# Fetch latest release from CLIProxyAPI
RELEASE_INFO=$(curl -s https://api.github.com/repos/router-for-me/CLIProxyAPI/releases/latest)
# Extract tag name and version
TAG_NAME=$(echo "$RELEASE_INFO" | jq -r '.tag_name // empty')
if [ -z "$TAG_NAME" ]; then
echo "No release found, checking latest commit instead"
LATEST_SHA=$(git ls-remote https://github.com/router-for-me/CLIProxyAPI.git HEAD | cut -f1)
echo "tag=" >> $GITHUB_OUTPUT
echo "sha=$LATEST_SHA" >> $GITHUB_OUTPUT
else
echo "Found release: $TAG_NAME"
echo "tag=$TAG_NAME" >> $GITHUB_OUTPUT
# Get the commit SHA for this tag
TAG_SHA=$(git ls-remote https://github.com/router-for-me/CLIProxyAPI.git "refs/tags/$TAG_NAME" | cut -f1)
echo "sha=$TAG_SHA" >> $GITHUB_OUTPUT
fi
- name: Check for updates
id: check
run: |
echo "Current SHA: ${{ steps.current.outputs.sha }}"
echo "Upstream SHA: ${{ steps.upstream.outputs.sha }}"
echo "Upstream Tag: ${{ steps.upstream.outputs.tag }}"
if [ "${{ steps.current.outputs.sha }}" != "${{ steps.upstream.outputs.sha }}" ]; then
echo "Update available!"
echo "has_update=true" >> $GITHUB_OUTPUT
else
echo "Already up to date"
echo "has_update=false" >> $GITHUB_OUTPUT
fi
- name: Create sync branch and PR
if: steps.check.outputs.has_update == 'true'
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
TAG="${{ steps.upstream.outputs.tag }}"
SHA="${{ steps.upstream.outputs.sha }}"
SHORT_SHA="${SHA:0:7}"
if [ -n "$TAG" ]; then
BRANCH_NAME="sync-upstream-$TAG"
PR_TITLE="feat: sync with CLIProxyAPI $TAG"
PR_BODY="## Summary
This PR syncs ProxyPilot with upstream CLIProxyAPI release \`$TAG\`.
**Upstream release:** https://github.com/router-for-me/CLIProxyAPI/releases/tag/$TAG
## Changes
- Updated to CLIProxyAPI $TAG ($SHORT_SHA)
## Checklist
- [ ] Review upstream changes
- [ ] Test locally before merging
- [ ] Update changelog if needed
---
🤖 Generated by [check-upstream workflow](https://github.com/${{ github.repository }}/actions/workflows/check-upstream.yml)"
else
BRANCH_NAME="sync-upstream-$SHORT_SHA"
PR_TITLE="feat: sync with CLIProxyAPI ($SHORT_SHA)"
PR_BODY="## Summary
This PR syncs ProxyPilot with latest upstream CLIProxyAPI commit.
**Upstream commit:** https://github.com/router-for-me/CLIProxyAPI/commit/$SHA
## Checklist
- [ ] Review upstream changes
- [ ] Test locally before merging
---
🤖 Generated by [check-upstream workflow](https://github.com/${{ github.repository }}/actions/workflows/check-upstream.yml)"
fi
# Check if branch already exists
if git ls-remote --exit-code --heads origin "$BRANCH_NAME" > /dev/null 2>&1; then
echo "Branch $BRANCH_NAME already exists, skipping"
exit 0
fi
# Check if PR already exists
EXISTING_PR=$(gh pr list --search "$PR_TITLE" --state open --json number --jq '.[0].number')
if [ -n "$EXISTING_PR" ]; then
echo "PR already exists: #$EXISTING_PR"
exit 0
fi
# Configure git
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
# Create branch
git checkout -b "$BRANCH_NAME"
# Add a marker file to track the sync (optional, can be removed)
echo "$SHA" > .upstream-sync-sha
echo "$TAG" > .upstream-sync-tag 2>/dev/null || true
# Commit
git add -A
git commit -m "$PR_TITLE" --allow-empty
# Push
git push origin "$BRANCH_NAME"
# Create PR
gh pr create \
--title "$PR_TITLE" \
--body "$PR_BODY" \
--base main \
--head "$BRANCH_NAME" \
--label "sync-upstream"
+79
View File
@@ -0,0 +1,79 @@
name: Build Release
on:
push:
tags:
- 'v*'
workflow_dispatch:
inputs:
version:
description: 'Version (e.g., 0.1.0)'
required: true
default: '0.1.0'
permissions:
contents: write
jobs:
build-windows:
runs-on: windows-latest
steps:
- uses: actions/checkout@v4
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: '1.24'
- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: '22'
- name: Build WebUI
run: |
cd webui
npm ci
npm run build
- name: Build Windows binaries
run: |
mkdir -p dist
# Build tray app (includes embedded dashboard)
go build -ldflags="-s -w -H=windowsgui" -o dist/ProxyPilot.exe ./cmd/cliproxytray
- name: Install Inno Setup
run: |
choco install innosetup -y
- name: Get version
id: version
run: |
if ("${{ github.event.inputs.version }}" -ne "") {
echo "VERSION=${{ github.event.inputs.version }}" >> $env:GITHUB_OUTPUT
} else {
$tag = "${{ github.ref_name }}"
$version = $tag -replace '^v', ''
echo "VERSION=$version" >> $env:GITHUB_OUTPUT
}
- name: Build Installer
run: |
& "C:\Program Files (x86)\Inno Setup 6\ISCC.exe" /DAppVersion="${{ steps.version.outputs.VERSION }}" installer\proxypilot.iss
- name: Upload artifacts
uses: actions/upload-artifact@v4
with:
name: windows-release
path: |
dist/ProxyPilot.exe
dist/ProxyPilot-*-Setup.exe
- name: Upload to Release
if: startsWith(github.ref, 'refs/tags/')
uses: softprops/action-gh-release@v1
with:
files: |
dist/ProxyPilot.exe
dist/ProxyPilot-${{ steps.version.outputs.VERSION }}-Setup.exe
config.example.yaml
+18 -8
View File
@@ -12,10 +12,18 @@ bin/*
logs/*
conv/*
temp/*
refs/*
# Storage backends
pgstore/*
gitstore/*
objectstore/*
# Static assets
static/*
!static/icon.png
!static/icon.ico
test/
refs/*
dist/*
.proxypilot/*
@@ -36,6 +44,8 @@ docs/*
!docs/memory.md
!docs/proxypilot.md
!docs/agentic-harness-demo.md
!docs/long-context-features.md
!docs/desktop-app-build-process.md
AGENTS.md
CLAUDE.md
GEMINI.md
@@ -48,6 +58,7 @@ GEMINI.md
.agent/*
.bmad/*
_bmad/*
_bmad-output/*
.mcp/cache/
# macOS
@@ -55,11 +66,10 @@ _bmad/*
._*
*.log
*_out.txt
# Added by Gemini CLI fix
node_modules/
dist/
.turbo/
.env
*_out.txt
# Added by Gemini CLI fix
node_modules/
dist/
.turbo/
.env
+10 -1
View File
@@ -1,3 +1,9 @@
before:
hooks:
# Remove Windows resource files before building
# These .syso files cause ARM64 cross-compile failures on Linux/Darwin
- find . -name "*.syso" -type f -delete || true
builds:
- id: "cli-proxy-api-plus"
env:
@@ -9,6 +15,10 @@ builds:
goarch:
- amd64
- arm64
ignore:
# Skip Windows ARM64 (not commonly used)
- goos: windows
goarch: arm64
main: ./cmd/server/
binary: cli-proxy-api-plus
ldflags:
@@ -22,7 +32,6 @@ archives:
files:
- LICENSE
- README.md
- README_CN.md
- config.example.yaml
checksum:
+89
View File
@@ -0,0 +1,89 @@
# Contributing to ProxyPilot
Thank you for your interest in contributing to ProxyPilot! This guide will help you get started.
## Development Environment Setup
1. **Install Go 1.24+**
- Download from [golang.org](https://golang.org/dl/)
- Verify installation: `go version`
2. **Clone the repository**
```bash
git clone https://github.com/your-org/ProxyPilot.git
cd ProxyPilot
```
3. **Install dependencies**
```bash
go mod download
```
## Running Tests
Run all tests:
```bash
go test ./...
```
Run tests with coverage:
```bash
go test -cover ./...
```
Run tests for a specific package:
```bash
go test ./pkg/proxy/...
```
## Code Style Guidelines
We follow standard Go conventions:
- **Format code** before committing:
```bash
go fmt ./...
```
- **Run static analysis**:
```bash
go vet ./...
```
- Follow [Effective Go](https://golang.org/doc/effective_go) guidelines
- Keep functions focused and reasonably sized
- Add comments for exported functions and types
- Use meaningful variable and function names
## Pull Request Process
1. Fork the repository and create a feature branch from `main`
2. Make your changes with clear, atomic commits
3. Ensure all tests pass and add tests for new functionality
4. Run `go fmt` and `go vet` before submitting
5. Open a pull request with a clear description of the changes
6. Link any related issues in the PR description
7. Address review feedback promptly
## Issue Reporting Guidelines
When reporting issues, please include:
- **Bug reports**: Steps to reproduce, expected behavior, actual behavior, Go version, and OS
- **Feature requests**: Clear description of the proposed feature and its use case
- **Questions**: Check existing issues and documentation first
Use descriptive titles and provide as much context as possible.
## Third-Party Provider Integrations
We welcome contributions that add integrations with third-party providers! If you're adding a new provider integration:
- Follow the existing integration patterns in the codebase
- Include documentation for configuration options
- Add appropriate tests
- Update the README if necessary
## Questions?
Feel free to open an issue for any questions about contributing.
+3 -2
View File
@@ -1,7 +1,8 @@
MIT License
Copyright (c) 2025-2005.9 Luis Pater
Copyright (c) 2025.9-present Router-For.ME
Copyright (c) 2024-2025 Luis Pater
Copyright (c) 2024-2025 Router-For.ME
Copyright (c) 2025 Finesssee (ProxyPilot fork)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
+200 -15
View File
@@ -1,31 +1,216 @@
# CLIProxyAPI Plus
<p align="center">
<img src="./static/icon.png" width="128" height="128" alt="ProxyPilot Logo">
</p>
English | [Chinese](README_CN.md)
<h1 align="center">ProxyPilot</h1>
This is the Plus version of [CLIProxyAPI](https://github.com/router-for-me/CLIProxyAPI), adding support for third-party providers on top of the mainline project.
<p align="center">
<a href="https://opensource.org/licenses/MIT"><img src="https://img.shields.io/badge/License-MIT-28a745" alt="MIT License"></a>
<a href="https://golang.org/"><img src="https://img.shields.io/badge/Go-1.24+-00ADD8?logo=go" alt="Go Version"></a>
<a href="https://x.com/Finessse377721"><img src="https://img.shields.io/badge/Follow-%F0%9D%95%8F%2F%40Finessse377721-1c9bf0" alt="Follow on 𝕏"></a>
<a href="https://github.com/Finesssee/ProxyPilot"><img src="https://img.shields.io/github/stars/Finesssee/ProxyPilot.svg?style=social&label=Star%20this%20repo" alt="Star this repo"></a>
</p>
All third-party provider support is maintained by community contributors; CLIProxyAPI does not provide technical support. Please contact the corresponding community maintainer if you need assistance.
<p align="center">
<strong>Stop juggling API keys.</strong> ProxyPilot is a powerful local API proxy that lets you use your existing Claude Code, Codex, Gemini, Kiro, and Qwen subscriptions with any AI coding tool – no separate API keys required.
</p>
The Plus release stays in lockstep with the mainline features.
<p align="center">
Built in Go, it handles OAuth authentication, token management, and API translation automatically. One server to route them all.
</p>
## Differences from the Mainline
---
- Added Kiro (AWS CodeWhisperer) support (OAuth login), provided by [fuko2935](https://github.com/fuko2935/CLIProxyAPI/tree/feature/kiro-integration), [Ravens2121](https://github.com/Ravens2121/CLIProxyAPIPlus/)
> [!TIP]
> 📣 **Latest models supported:**
> Claude Opus 4.5 / Sonnet 4.5 with extended thinking, GPT-5.2 / GPT-5.2 Codex, Gemini 3 Pro/Flash, and Kiro (AWS CodeWhisperer)! 🚀
## Contributing
**Setup Guides:**
- [Claude Code Setup →](docs/claude-code-local-proxy.md)
- [Cursor IDE Setup →](docs/cursor-ide.md)
This project only accepts pull requests that relate to third-party provider support. Any pull requests unrelated to third-party provider support will be rejected.
---
If you need to submit any non-third-party provider changes, please open them against the mainline repository.
## Features
- 🎯 **7 Auth Providers** - Claude, Codex (OpenAI), Gemini, Gemini CLI, Kiro (AWS), Qwen, iFlow
- 🔄 **Universal API Translation** - Auto-converts between OpenAI, Anthropic, and Gemini formats
- 🔧 **Tool Calling Repair** - Fixes tool/function call mismatches between providers automatically
- 🧠 **Extended Thinking** - Full support for Claude and Gemini thinking models
- 🔐 **OAuth Integration** - Browser-based login with automatic token refresh
- 👥 **Multi-Account Support** - Round-robin distribution with automatic failover
- ⚡ **Quota Auto-Switch** - Automatically switches to backup project/model when quota exceeded
- 📊 **Usage Statistics** - Track requests, tokens, and errors per provider/model
- 🧩 **Context Compression** - LLM-based summarization for long sessions (Factory.ai research)
- 🤖 **Agentic Harness** - Guided workflow for coding agents (Anthropic research)
- 💾 **Session Memory** - Persistent storage across conversation turns
- 🎨 **Desktop App** - Native Windows control center with system tray
- 📡 **60+ Management APIs** - Full control via REST endpoints
---
## Supported Providers
| Provider | Auth Method | Models |
|----------|-------------|--------|
| Claude (Anthropic) | OAuth2 / API Key | Claude Opus 4.5, Sonnet 4.5, Haiku 4.5 |
| Codex (OpenAI) | OAuth2 / API Key | GPT-5.2, GPT-5.2 Codex |
| Gemini | OAuth2 / API Key | Gemini 3 Pro, Gemini 3 Flash |
| Gemini CLI | OAuth2 | Cloud Code Assist models |
| Kiro | OAuth2 + AWS SSO | AWS CodeWhisperer |
| Qwen | OAuth2 | Qwen models |
| iFlow | Cookie-based | Alibaba models |
| Custom | API Key | Any OpenAI-compatible endpoint |
---
## Installation
### Download Pre-built Release (Recommended)
1. Go to the [**Releases**](https://github.com/Finesssee/ProxyPilot/releases) page
2. Download the latest binary for your platform
3. Run `./proxypilot`
### Build from Source
```bash
git clone https://github.com/Finesssee/ProxyPilot.git
cd ProxyPilot
go build -o proxypilot ./cmd/server
./proxypilot
```
---
## Usage
### First Launch
1. Copy config: `cp config.example.yaml config.yaml`
2. Run: `./proxypilot`
3. Server starts on `http://localhost:8317`
4. Open dashboard: `http://localhost:8317/proxypilot.html`
### Authentication
Run OAuth login for your provider:
```bash
./proxypilot --claude-login # Claude
./proxypilot --codex-login # OpenAI/Codex
./proxypilot --login # Gemini
./proxypilot --kiro-login # Kiro/AWS
```
Your browser opens automatically. Tokens are stored and auto-refreshed.
### Configure Your Tools
**Claude Code** (`~/.claude/settings.json`):
```json
{
"env": {
"ANTHROPIC_BASE_URL": "http://127.0.0.1:8317",
"ANTHROPIC_AUTH_TOKEN": "your-api-key"
}
}
```
**Codex CLI** (`~/.codex/config.toml`):
```toml
[openai]
api_base_url = "http://127.0.0.1:8317"
```
**Factory Droid** (`~/.factory/settings.json`):
```json
{
"customModels": [{
"name": "ProxyPilot",
"baseUrl": "http://127.0.0.1:8317"
}]
}
```
---
## API Endpoints
```
POST /v1/chat/completions # OpenAI Chat Completions
POST /v1/responses # OpenAI Responses API
POST /v1/messages # Anthropic Messages API
GET /v1/models # List available models
GET /healthz # Health check
```
All endpoints auto-translate between formats based on the target provider.
---
## CLI Tools
| Binary | Description |
|--------|-------------|
| `proxypilot` | Main API proxy server |
| `proxypilotui` | Desktop control center (Windows) |
| `cliproxytray` | System tray application (Windows) |
| `proxypilotpack` | Build and packaging tool |
---
## Tool Integrations
Works with these AI coding tools:
- **Claude Code** - Auto-configure via settings.json
- **Codex CLI** - Auto-configure via config.toml
- **Factory Droid** - Auto-configure via settings.json
- **Cursor IDE** - Manual endpoint configuration
- **Continue** - Manual endpoint configuration
---
## Requirements
- macOS, Linux, or Windows
- Go 1.24+ (for building from source)
---
## Credits
ProxyPilot builds upon excellent work from the open-source community:
- **[CLIProxyAPI](https://github.com/anthropics/claude-code)** - The original unified proxy server that inspired this project
- **[VibeProxy](https://github.com/automazeio/vibeproxy)** - Native macOS menu bar app showcasing clean proxy UX
Long-context features are inspired by research from the AI community:
- **[Factory.ai](https://factory.ai/news/evaluating-compression)** - Context compression techniques for long-running coding agents
- **[Anthropic](https://www.anthropic.com/engineering/effective-harnesses-for-long-running-agents)** - Effective harnesses for long-running agents
Special thanks to these teams for sharing their work and insights.
---
## License
This project is licensed under the MIT License - see `LICENSE`.
MIT License - see [LICENSE](LICENSE) for details.
---
## Support
- **Report Issues**: [GitHub Issues](https://github.com/Finesssee/ProxyPilot/issues)
---
## Disclaimer
This project is intended for educational and interoperability research purposes only. It interacts with various internal or undocumented APIs (such as Google's `v1internal`) to provide compatibility layers.
This project is for educational and interoperability research purposes. It interacts with various APIs to provide compatibility layers.
- **Use at your own risk.** The authors are not responsible for any consequences arising from the use of this software, including but not limited to account suspensions or service interruptions.
- This project is **not** affiliated with, endorsed by, or associated with Google, OpenAI, Anthropic, or any other service provider mentioned.
- Users are expected to comply with the Terms of Service of the respective platforms they connect to.
- **Use at your own risk.** Authors are not responsible for account suspensions or service interruptions.
- **Not affiliated** with Google, OpenAI, Anthropic, Amazon, or any other provider.
- Users must comply with the Terms of Service of connected platforms.
-23
View File
@@ -1,23 +0,0 @@
# CLIProxyAPI Plus
[English](README.md) | 中文
这是 [CLIProxyAPI](https://github.com/router-for-me/CLIProxyAPI) 的 Plus 版本,在原有基础上增加了第三方供应商的支持。
所有的第三方供应商支持都由第三方社区维护者提供,CLIProxyAPI 不提供技术支持。如需取得支持,请与对应的社区维护者联系。
该 Plus 版本的主线功能与主线功能强制同步。
## 与主线版本版本差异
- 新增 Kiro (AWS CodeWhisperer) 支持 (OAuth 登录), 由[fuko2935](https://github.com/fuko2935/CLIProxyAPI/tree/feature/kiro-integration)、[Ravens2121](https://github.com/Ravens2121/CLIProxyAPIPlus/)提供
## 贡献
该项目仅接受第三方供应商支持的 Pull Request。任何非第三方供应商支持的 Pull Request 都将被拒绝。
如果需要提交任何非第三方供应商支持的 Pull Request,请提交到主线版本。
## 许可证
此项目根据 MIT 许可证授权 - 有关详细信息,请参阅 [LICENSE](LICENSE) 文件。
+58
View File
@@ -0,0 +1,58 @@
# Security Policy
## Supported Versions
The following versions of ProxyPilot are currently supported with security updates:
| Version | Supported |
| ------- | ------------------ |
| latest | :white_check_mark: |
| < 1.0 | :x: |
## Reporting a Vulnerability
We take the security of ProxyPilot seriously. If you discover a security vulnerability, please report it responsibly.
### How to Report
You can report a vulnerability through one of the following methods:
1. **GitHub Security Advisory (Preferred)**: Open a private security advisory through [GitHub Security Advisories](https://github.com/smat-dev/ProxyPilot/security/advisories/new). This allows for private discussion and coordination.
2. **Email**: Send an email to the project maintainers with details of the vulnerability. Please include:
- A description of the vulnerability
- Steps to reproduce the issue
- Potential impact of the vulnerability
- Any suggested fixes (if available)
### What to Expect
After submitting a vulnerability report:
- **Initial Response**: You will receive an acknowledgment within **48 hours** confirming we have received your report.
- **Assessment**: We will investigate and assess the vulnerability within **7 days**.
- **Resolution Timeline**: We aim to release a fix for confirmed vulnerabilities within **30 days**, depending on complexity.
- **Disclosure**: Once a fix is released, we will coordinate with you on public disclosure timing.
We will keep you informed throughout the process and credit you for the discovery (unless you prefer to remain anonymous).
## Responsible Disclosure Guidelines
To protect our users, we ask that you:
- **DO NOT** disclose the vulnerability publicly until a fix has been released.
- **DO NOT** exploit the vulnerability beyond what is necessary to demonstrate the issue.
- **DO NOT** access, modify, or delete data belonging to other users.
- **DO** provide us with reasonable time to address the issue before any public disclosure.
- **DO** act in good faith to avoid privacy violations, data destruction, or service disruption.
We are committed to working with security researchers and will not take legal action against individuals who discover and report vulnerabilities responsibly.
## Security Best Practices for Users
- Always use the latest version of ProxyPilot.
- Review and understand the proxy configurations before deployment.
- Use secure connections (HTTPS) where applicable.
- Regularly review access logs for suspicious activity.
Thank you for helping keep ProxyPilot and its users safe.
-74
View File
@@ -1,74 +0,0 @@
# ProxyPilot & Droid CLI Troubleshooting Log
**Date:** 2025-12-19
**Subject:** Gemini/Antigravity API Key & Quota Issues
## 1. Issue Summary
The user encountered `[HTTP 400] API key not valid` and `[HTTP 429] Resource Exhausted` errors when using the `droid` CLI with the `gemini-3-pro-preview` model.
## 2. Root Causes Identified
### A. Wrong Model Routing (Primary Cause of Quota Error)
* **Symbol:** `gemini-3-pro-preview`
* **Issue:** In ProxyPilot's registry, `gemini-3-pro-preview` is defined as a **Google/Gemini CLI** model, not an Antigravity model. Requests were routing directly to Google using personal CLI keys/quota, which were exhausted (429).
* **Fix:** Switched Droid configuration to use **`gemini-3-pro-high`** (or `low`), which are the correct aliases for Antigravity-routed models. These are now confirmed working.
### B. Antigravity Account Rotation Failure
* **Issue:** Even when using Antigravity, only 1 of the 3 available accounts was being utilized.
* **Cause:** The Auth Selector (`selector.go`) uses a "Strict Fallback" heuristic. If exactly *one* auth file contains a `project_id` field, it is designated as the "Primary" account, and others are ignored until the primary is effectively invalid.
* **Discovery:** Only the account `truongnamphong8` had a `project_id`.
* **Fix:** Removed the `project_id` field from the auth file so all 3 accounts are treated as "peers" and rotated via Round Robin.
### C. "API Key Not Valid" Startup Error
* **Issue:** ProxyPilot server logs showed API key errors on startup.
* **Cause:** `config.yaml` contained a placeholder line: `api-key: "YOUR_LEGIT_API_KEY_HERE"`.
* **Fix:** Commented out the invalid line in `config.yaml`.
### D. Droid CLI Config Corruption
* **Issue:** Droid failed to load with "Failed to parse custom models config".
* **Cause:** The `config.json` file was saved with a UTF-8 **BOM** (Byte Order Mark) by PowerShell during debugging/editing.
* **Fix:** Rewrote `config.json` with UTF-8 (No BOM) encoding.
## 3. Preventative Measures & Best Practices
### Model Naming
**ALWAYS** use the Antigravity-specific model IDs to ensure requests route through the shared pool rather than personal quota:
* ✅ `gemini-3-pro-high`
* ✅ `gemini-3-pro-low`
* ✅ `gemini-3-flash` (if available via Antigravity)
* ✅ `antigravity-claude-sonnet-4-5-thinking`
* ❌ `gemini-3-pro-preview` (Routes to Google CLI)
### Auth File Management
When adding new Antigravity accounts (`.json` files in `~/.cli-proxy-api/`):
* Ensure **uniformity**. Either *all* files should have a `project_id`, or *none* should.
* If mixed, the one with `project_id` will monopolize traffic.
### Configuration
* Do not leave placeholder values (like "YOUR_KEY_HERE") in `config.yaml`; comment them out.
* When editing JSON configuration files on Windows, ensure your editor saves as **UTF-8 (No BOM)**.
## 4. Current Configuration (Verified)
* **Droid Config:** `~/.factory/config.json` updated with correct Antigravity model names.
* **ProxyPilot Config:** `d:\code\ProxyPilot\config.yaml` cleaned of invalid keys.
* **Auth Status:** 3 Antigravity accounts active and rotating.
## 5. Issue: Rate Limit Rotation "Stuck" on One Account
**Date:** 2025-12-20
**Subject:** HTTP 429 Handling in Antigravity Executor & Manager
### A. Issue Summary
The user experienced repeated 429 Too Many Requests errors from the *same* account (yuhh0704@gmail.com) despite having multiple healthy backup accounts (nnq2366) available. The rotation logic refused to switch to the backup, effectively blocking the user.
### B. Root Causes
1. **Missing Retry-After Parsing:** The AntigravityExecutor was receiving a Retry-After header (e.g., 1 hour) from the upstream API but was ignoring it. It only returned a generic error message.
* **Effect:** The AuthManager used a default backoff of only **1 second**.
2. **Model State Not Updated:** When the global Auth object was marked as "Rate Limited", the specific ModelState for the requested model (e.g., claude-opus-4-5-thinking) remained "Active".
* **Effect:** The RoundRobinSelector checked the *model* state, saw it was valid, and re-selected the same "Primary Fallback" account (due to strict fallback logic preferences) immediately after the 1-second backoff expired.
### C. The Fix
1. **Updated AntigravityExecutor:** Added logic to parse the Retry-After header (supporting both integer seconds and HTTP date formats) and pass it to the statusErr struct.
2. **Updated AuthManager (MarkResult):** Modified the result handling to explicitly update the ModelState with the error and cooldown info, not just the parent Auth object.
3. **Updated Selector (isAuthBlockedForModel):** Added a defensive check to respect NextRetryAfter timestamps even if the Unavailable boolean flag wasn't explicitly set, ensuring blocked models are strictly filtered out.
### D. Result
System now respects long cooldowns (e.g., 3 hours) and correctly skips the blocked "Primary" account to use the "Secondary" backups like nnq2366.
-156
View File
@@ -1,156 +0,0 @@
# WARP.md
This file provides guidance to WARP (warp.dev) when working with code in this repository.
## Common commands
### Go (engine + desktop binaries)
Build the main deliverables into `bin/`:
```pwsh
# Builds:
# - bin\proxypilot-engine.exe (engine)
# - bin\ProxyPilot.exe (tray app)
# - bin\ProxyPilotUI.exe (desktop dashboard)
go run .\cmd\proxypilotpack build
```
Run the engine directly:
```pwsh
# Uses config.yaml in the repo root by default
# (or pass -config <path>)
go run .\cmd\server -config .\config.yaml
```
Windows helpers (used for local development and Droid/Factory CLI integration):
```pwsh
# build engine (writes bin\proxypilot-engine.exe)
powershell -NoProfile -ExecutionPolicy Bypass -File .\scripts\build-cliproxyapi.ps1
# start/stop/restart engine (writes logs\proxypilot-engine.{out,err}.log)
powershell -NoProfile -ExecutionPolicy Bypass -File .\scripts\start-cliproxy.ps1
powershell -NoProfile -ExecutionPolicy Bypass -File .\scripts\stop-cliproxy.ps1
powershell -NoProfile -ExecutionPolicy Bypass -File .\scripts\restart-cliproxy.ps1
```
Package artifacts:
```pwsh
# Zip bundle -> dist\ProxyPilot.zip
# (includes tray, UI, engine, and config.example.yaml)
go run .\cmd\proxypilotpack package-zip
# Windows installer (Inno Setup) -> dist\ProxyPilot-Setup.exe
# Requires ISCC.exe (Inno Setup 6) available on PATH.
go run .\cmd\proxypilotpack package-inno
```
Docker (engine in a container):
```pwsh
docker compose up --build
```
### Web UI (Vite)
The web UI lives in `webui/`:
```pwsh
cd .\webui
npm install
npm run dev # local dev server
npm run build # typecheck + build
npm run lint
```
### Tests
Run all Go tests:
```pwsh
go test ./...
```
Run a single package:
```pwsh
go test .\internal\api -run TestName
```
Run a single test across all packages:
```pwsh
go test ./... -run TestName
```
Regression tests called out in docs:
- Droid SSE parsing: `sdk/api/handlers/openai/openai_responses_handlers_sse_done_test.go`
- Codex SSE done sentinel: `sdk/api/handlers/openai/openai_responses_handlers_sse_json_compat_test.go`
- Antigravity request shape: `internal/runtime/executor/antigravity_executor_shape_test.go`
## High-level architecture
### Binaries (`cmd/`)
This repo produces multiple Go binaries:
- `cmd/server` (engine): HTTP proxy server exposing OpenAI/Gemini/Claude-compatible endpoints; also hosts OAuth callback endpoints.
- `cmd/cliproxytray` (Windows tray app): starts/stops the engine, opens UI, toggles autostart, checks for updates.
- `cmd/proxypilotui` (Windows desktop UI): WebView2 desktop window for the Control Center; talks to the running engine and uses management endpoints.
- `cmd/proxypilotpack`: build/packaging orchestrator (build binaries into `bin/`, create `dist/` artifacts).
### Engine request path (big picture)
1. `cmd/server/main.go` loads config (`config.yaml` or a store-backed config), decides whether to run login flows or start the server.
2. `internal/cmd/StartService` builds the service via the exported SDK (`sdk/cliproxy.NewBuilder()`) and runs it.
3. The HTTP server is implemented in `internal/api/server.go` using Gin:
- `/v1/*` OpenAI-compatible endpoints (plus non-`/v1` aliases for clients that set base URL to `/v1`).
- `/v1/responses` has special streaming/SSE behavior for strict CLIs (see `docs/compat-matrix.md`).
- `/v1beta/*` Gemini-compatible endpoints.
- `/v1internal:method` Gemini CLI compatibility endpoint.
- OAuth callbacks like `/codex/callback`, `/google/callback`, etc. persist short-lived state for the login goroutines.
Core handler layer:
- `sdk/api/handlers/*` contains provider-specific API adapters (OpenAI/Gemini/Claude) and schema/streaming shims.
- `internal/api/handlers/management/*` contains management endpoints mounted under `/v0/management/*`.
### Management + desktop UX
- Management endpoints are only enabled when a secret is configured:
- `remote-management.secret-key` in `config.yaml`, or
- environment `MANAGEMENT_PASSWORD`.
- The tray app starts the engine via `internal/desktopctl.Start()` and injects a per-user `MANAGEMENT_PASSWORD` so the desktop UX can use `/v0/management/*` locally.
- The ProxyPilot dashboard page (`/proxypilot.html`) is served **localhost-only** and injects the management key into the HTML so users don’t paste keys manually.
Key components:
- `internal/desktopctl`: process management (start/stop/status), log file locations, and per-user UI state.
- `internal/api/proxypilot_dashboard.go`: local-only ProxyPilot dashboard HTML.
- `internal/managementasset`: downloads/serves the upstream management UI bundle and applies runtime text replacements for ProxyPilot branding.
### Config + persistence
- Default config is `config.yaml` (template: `config.example.yaml`).
- The engine can optionally use different config/auth persistence backends based on env vars (selected in `cmd/server/main.go`):
- Postgres-backed store (`PGSTORE_*`)
- Git-backed token store (`GITSTORE_*`)
- Object store (`OBJECTSTORE_*`)
### SDK embedding
The proxy can be embedded as a Go library:
- `sdk/cliproxy` exposes a builder (`NewBuilder`) that wires config watching, auth, translators, and the HTTP server.
- `docs/sdk-usage.md` shows the supported server options (middleware/router hooks, request logging overrides, management enablement).
## Docs worth reading first
- `docs/proxypilot.md` (Windows packaging + local management key behavior)
- `docs/compat-matrix.md` (client/upstream quirks; streaming/SSE gotchas; tests to protect)
- `docs/cursor-ide.md` (how Cursor expects base URL and API key behavior)
- `docs/sdk-usage.md` (how the engine is composed when embedded)
+197
View File
@@ -0,0 +1,197 @@
//go:build windows
package main
import (
"context"
"errors"
"fmt"
"sync"
"time"
"github.com/router-for-me/CLIProxyAPI/v6/internal/config"
"github.com/router-for-me/CLIProxyAPI/v6/internal/desktopctl"
"github.com/router-for-me/CLIProxyAPI/v6/sdk/cliproxy"
log "github.com/sirupsen/logrus"
)
// EmbeddedEngine runs the proxy service in-process within the tray application.
// It provides thread-safe methods for starting, stopping, and querying the service status.
type EmbeddedEngine struct {
mu sync.Mutex
// running indicates whether the service is currently active.
running bool
// port is the port number the service is listening on.
port int
// configPath stores the path to the configuration file.
configPath string
// lastError holds the most recent error encountered during operation.
lastError error
// startedAt records when the service was started.
startedAt time.Time
// cancel is the function to cancel the service context.
cancel context.CancelFunc
// wg waits for the service goroutine to complete.
wg sync.WaitGroup
}
// NewEmbeddedEngine creates a new EmbeddedEngine instance.
func NewEmbeddedEngine() *EmbeddedEngine {
return &EmbeddedEngine{}
}
// Start builds and runs the proxy service in a background goroutine.
// It returns an error if the service is already running or if building the service fails.
func (e *EmbeddedEngine) Start(cfg *config.Config, configPath, password string) error {
e.mu.Lock()
defer e.mu.Unlock()
if e.running {
return errors.New("engine is already running")
}
if cfg == nil {
return errors.New("configuration is required")
}
if configPath == "" {
return errors.New("configuration path is required")
}
// Build the service using the SDK builder pattern
builder := cliproxy.NewBuilder().
WithConfig(cfg).
WithConfigPath(configPath).
WithLocalManagementPassword(password)
service, err := builder.Build()
if err != nil {
e.lastError = fmt.Errorf("failed to build proxy service: %w", err)
return e.lastError
}
// Create a cancelable context for the service
ctx, cancel := context.WithCancel(context.Background())
e.cancel = cancel
// Track state
e.running = true
e.port = cfg.Port
e.configPath = configPath
e.lastError = nil
e.startedAt = time.Now()
log.Infof("starting embedded proxy engine on port %d", e.port)
// Run the service in a goroutine
e.wg.Add(1)
go func() {
defer e.wg.Done()
defer func() {
e.mu.Lock()
e.running = false
e.cancel = nil
e.mu.Unlock()
log.Info("embedded proxy engine stopped")
}()
err := service.Run(ctx)
if err != nil && !errors.Is(err, context.Canceled) {
e.mu.Lock()
e.lastError = fmt.Errorf("proxy service exited with error: %w", err)
e.mu.Unlock()
log.Errorf("embedded proxy engine error: %v", err)
}
}()
return nil
}
// Stop cancels the running service and waits for it to shut down.
// It returns an error if the service is not running.
func (e *EmbeddedEngine) Stop() error {
e.mu.Lock()
if !e.running {
e.mu.Unlock()
return errors.New("engine is not running")
}
if e.cancel != nil {
log.Info("stopping embedded proxy engine...")
e.cancel()
}
e.mu.Unlock()
// Wait for the goroutine to complete
e.wg.Wait()
return nil
}
// Restart stops the running service (if any) and starts it again with the new configuration.
func (e *EmbeddedEngine) Restart(cfg *config.Config, configPath, password string) error {
// Stop if running (ignore error if not running)
if e.IsRunning() {
if err := e.Stop(); err != nil {
log.Warnf("error stopping engine during restart: %v", err)
}
}
// Start with new configuration
return e.Start(cfg, configPath, password)
}
// Status returns the current status of the embedded engine.
// The returned status is compatible with desktopctl.Status for UI integration.
func (e *EmbeddedEngine) Status() desktopctl.Status {
e.mu.Lock()
defer e.mu.Unlock()
status := desktopctl.Status{
Running: e.running,
Managed: true, // Embedded engine is always managed by the tray
Port: e.port,
ConfigPath: e.configPath,
StartedAt: e.startedAt,
}
if e.running && e.port > 0 {
status.BaseURL = fmt.Sprintf("http://127.0.0.1:%d", e.port)
}
if e.lastError != nil {
status.LastError = e.lastError.Error()
}
return status
}
// IsRunning returns whether the engine is currently running.
func (e *EmbeddedEngine) IsRunning() bool {
e.mu.Lock()
defer e.mu.Unlock()
return e.running
}
// LastError returns the most recent error encountered, or nil if none.
func (e *EmbeddedEngine) LastError() error {
e.mu.Lock()
defer e.mu.Unlock()
return e.lastError
}
// Port returns the port number the engine is configured to listen on.
func (e *EmbeddedEngine) Port() int {
e.mu.Lock()
defer e.mu.Unlock()
return e.port
}
+312 -288
View File
@@ -7,6 +7,7 @@ import (
"encoding/json"
"flag"
"fmt"
"io/fs"
"net"
"net/http"
"net/http/httputil"
@@ -14,36 +15,84 @@ import (
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"sync"
"time"
"github.com/getlantern/systray"
"github.com/router-for-me/CLIProxyAPI/v6/internal/buildinfo"
"github.com/jchv/go-webview2"
"github.com/router-for-me/CLIProxyAPI/v6/cmd/proxypilotui/assets"
"github.com/router-for-me/CLIProxyAPI/v6/internal/api/middleware"
"github.com/router-for-me/CLIProxyAPI/v6/internal/config"
"github.com/router-for-me/CLIProxyAPI/v6/internal/desktopctl"
"github.com/router-for-me/CLIProxyAPI/v6/internal/proxypilotupdate"
"github.com/router-for-me/CLIProxyAPI/v6/internal/integrations"
"github.com/router-for-me/CLIProxyAPI/v6/internal/logging"
_ "github.com/router-for-me/CLIProxyAPI/v6/internal/translator"
"github.com/router-for-me/CLIProxyAPI/v6/internal/trayicon"
"github.com/router-for-me/CLIProxyAPI/v6/internal/updates"
"github.com/router-for-me/CLIProxyAPI/v6/internal/usage"
"github.com/router-for-me/CLIProxyAPI/v6/internal/util"
configaccess "github.com/router-for-me/CLIProxyAPI/v6/sdk/access/config"
sdkAuth "github.com/router-for-me/CLIProxyAPI/v6/sdk/auth"
)
const autostartAppName = "ProxyPilot"
const thinkingProxyPort = 8317
var assetServerURL string
var dashboardMu sync.Mutex
var dashboardOpen bool
func main() {
// Start embedded asset server for the dashboard UI
assetLn, err := net.Listen("tcp", "127.0.0.1:0")
if err == nil {
assetServerURL = "http://" + assetLn.Addr().String()
go func() {
fsys, _ := fs.Sub(assets.FS, ".")
http.Serve(assetLn, http.FileServer(http.FS(fsys)))
}()
}
var repoRoot string
var configPath string
var exePath string
flag.StringVar(&repoRoot, "repo", "", "Repo root (used to locate bin/ and logs/)")
flag.StringVar(&repoRoot, "repo", "", "Repo root (used to locate logs/)")
flag.StringVar(&configPath, "config", "", "Path to config.yaml (defaults to <repo>/config.yaml)")
flag.StringVar(&exePath, "exe", "", "Path to ProxyPilot Engine binary (defaults to <repo>/bin/proxypilot-engine.exe)")
flag.Parse()
repoRoot, configPath, exePath = applyDefaults(repoRoot, configPath, exePath)
run(repoRoot, configPath, exePath)
repoRoot, configPath = applyDefaults(repoRoot, configPath)
run(repoRoot, configPath)
}
func run(repoRoot, configPath, exePath string) {
func run(repoRoot, configPath string) {
// Initialize logging
logging.SetupBaseLogger()
// Load config
cfg, err := config.LoadConfigOptional(configPath, false)
if err == nil && cfg != nil {
logging.ConfigureLogOutput(cfg.LoggingToFile, cfg.LogsMaxTotalSizeMB)
util.SetLogLevel(cfg)
}
if cfg == nil {
cfg = &config.Config{Port: 8318} // Default config if load fails
}
// Register token store
sdkAuth.RegisterTokenStore(sdkAuth.NewFileTokenStore())
// Register access providers
configaccess.Register()
// Create embedded engine (will be used instead of desktopctl)
engine := NewEmbeddedEngine()
// Get or create management password
password, _ := desktopctl.GetManagementPassword()
systray.Run(func() {
thinkingProxy := startThinkingProxy(configPath)
thinkingProxy := startThinkingProxy(engine)
defer thinkingProxy.Close()
if ico := trayicon.ProxyPilotICO(); len(ico) > 0 {
@@ -52,99 +101,76 @@ func run(repoRoot, configPath, exePath string) {
systray.SetTitle("ProxyPilot")
systray.SetTooltip("ProxyPilot")
statusItem := systray.AddMenuItem("Status: ...", "Current status")
// Header
systray.AddMenuItem("ProxyPilot", "ProxyPilot").Disable()
systray.AddSeparator()
// Status display item (disabled, updated dynamically)
statusItem := systray.AddMenuItem("○ Stopped", "Current proxy status")
statusItem.Disable()
systray.AddSeparator()
openProxyUI := systray.AddMenuItem("Open Dashboard", "Open ProxyPilot Control Center")
// Main actions
openDashboard := systray.AddMenuItem("Open Dashboard", "Open ProxyPilot Dashboard")
toggleItem := systray.AddMenuItem("Start Proxy", "Start/Stop proxy")
copyURLItem := systray.AddMenuItem("Copy API URL", "Copy http://127.0.0.1:8317/v1")
systray.AddSeparator()
// Engine submenu
engineMenu := systray.AddMenuItem("Engine", "Engine controls")
startItem := engineMenu.AddSubMenuItem("Start", "Start the ProxyPilot engine")
stopItem := engineMenu.AddSubMenuItem("Stop", "Stop the ProxyPilot engine")
restartItem := engineMenu.AddSubMenuItem("Restart", "Restart the ProxyPilot engine")
// Providers submenu
providersMenu := systray.AddMenuItem("Providers", "Provider status")
claudeItem := providersMenu.AddSubMenuItem("○ Claude - Inactive", "Claude provider status")
geminiItem := providersMenu.AddSubMenuItem("○ Gemini - Inactive", "Gemini provider status")
codexItem := providersMenu.AddSubMenuItem("○ Codex - Inactive", "Codex provider status")
qwenItem := providersMenu.AddSubMenuItem("○ Qwen - Inactive", "Qwen provider status")
anthropicItem := providersMenu.AddSubMenuItem("○ Anthropic - Inactive", "Anthropic provider status")
// Disable provider items (they're informational only for now)
claudeItem.Disable()
geminiItem.Disable()
codexItem.Disable()
qwenItem.Disable()
anthropicItem.Disable()
// Connect submenu (VibeProxy-style easy setup)
connectMenu := systray.AddMenuItem("Connect", "Environment variables")
copyAnthropicEnv := connectMenu.AddSubMenuItem("Copy ANTHROPIC_BASE_URL", "Copy export ANTHROPIC_BASE_URL=http://127.0.0.1:8317/v1")
copyOpenAIEnv := connectMenu.AddSubMenuItem("Copy OPENAI_BASE_URL", "Copy export OPENAI_BASE_URL=http://127.0.0.1:8317/v1")
// Settings submenu
settingsMenu := systray.AddMenuItem("Settings", "ProxyPilot settings")
autoOn, _, _ := desktopctl.IsWindowsRunAutostartEnabled(autostartAppName)
autoStartItem := settingsMenu.AddSubMenuItemCheckbox("Launch on login", "Start this tray app when you log in", autoOn)
// Diagnostics submenu
diagMenu := systray.AddMenuItem("Diagnostics", "Diagnostic tools")
copyDiagItem := diagMenu.AddSubMenuItem("Copy Diagnostics", "Copy diagnostics to clipboard")
openLogsItem := diagMenu.AddSubMenuItem("Open Logs Folder", "Open logs folder in explorer")
openAuthItem := diagMenu.AddSubMenuItem("Open Auth Folder", "Open auth folder in explorer")
systray.AddSeparator()
quitItem := systray.AddMenuItem("Quit", "Quit ProxyPilot")
// Auto-start proxy on launch if enabled
autoProxyOn, _ := desktopctl.GetAutoStartProxy()
autoStartProxyItem := settingsMenu.AddSubMenuItemCheckbox("Auto-start proxy", "Start the proxy server automatically", autoProxyOn)
// Tools submenu
toolsMenu := systray.AddMenuItem("Tools", "ProxyPilot tools")
openLogs := toolsMenu.AddSubMenuItem("Open Logs", "Open logs folder")
copyDiagnostics := toolsMenu.AddSubMenuItem("Copy Diagnostics", "Copy a support bundle to clipboard")
openLegacyUI := toolsMenu.AddSubMenuItem("Advanced UI", "Open management UI (advanced)")
// Updates submenu
updatesMenu := systray.AddMenuItem("Updates", "Check for updates")
checkUpdates := updatesMenu.AddSubMenuItem("Check for Updates", "Check GitHub Releases for a newer ProxyPilot")
updateNow := updatesMenu.AddSubMenuItem("Update ProxyPilot...", "Download and run the latest installer")
updateNow.Disable()
systray.AddSeparator()
quitItem := systray.AddMenuItem("Quit", "Quit")
lastErr := ""
var (
latestVersion string
assetName string
assetURL string
)
refresh := func() {
st, _ := desktopctl.StatusFor(configPath)
title := "Stopped"
if st.Running {
title = fmt.Sprintf("Running (:%d)", st.Port)
}
if st.LastError != "" && lastErr == "" {
lastErr = st.LastError
}
if lastErr != "" {
title = title + " - " + shorten(lastErr, 80)
}
statusItem.SetTitle("Status: " + title)
systray.SetTooltip("ProxyPilot - " + title)
if st.Running {
startItem.Disable()
stopItem.Enable()
restartItem.Enable()
} else {
startItem.Enable()
stopItem.Disable()
restartItem.Disable()
}
}
refresh()
// Optional: auto-start the proxy server on launch.
if autoProxyOn {
go func() {
st, _ := desktopctl.StatusFor(configPath)
if st.Running {
return
if !engine.IsRunning() {
engine.Start(cfg, configPath, password)
}
_, err := desktopctl.Start(desktopctl.StartOptions{RepoRoot: repoRoot, ConfigPath: configPath, ExePath: exePath})
if err != nil {
lastErr = err.Error()
} else {
lastErr = ""
}
refresh()
}()
}
// Update UI based on status
refresh := func() {
st := engine.Status()
if st.Running {
port := st.Port
if port <= 0 {
port = 8318
}
statusItem.SetTitle(fmt.Sprintf("● Running on :%d", port))
systray.SetTooltip(fmt.Sprintf("ProxyPilot - Running (:%d)", port))
toggleItem.SetTitle("Stop Proxy")
toggleItem.SetTooltip("Stop the proxy")
} else {
statusItem.SetTitle("○ Stopped")
systray.SetTooltip("ProxyPilot - Stopped")
toggleItem.SetTitle("Start Proxy")
toggleItem.SetTooltip("Start the proxy")
}
}
refresh()
// Refresh status periodically
go func() {
t := time.NewTicker(2 * time.Second)
defer t.Stop()
@@ -153,155 +179,29 @@ func run(repoRoot, configPath, exePath string) {
}
}()
// Handle clicks
go func() {
for {
select {
case <-startItem.ClickedCh:
_, err := desktopctl.Start(desktopctl.StartOptions{RepoRoot: repoRoot, ConfigPath: configPath, ExePath: exePath})
if err != nil {
lastErr = err.Error()
refresh()
continue
}
lastErr = ""
refresh()
case <-stopItem.ClickedCh:
if err := desktopctl.Stop(desktopctl.StopOptions{}); err != nil {
lastErr = err.Error()
refresh()
continue
}
lastErr = ""
refresh()
case <-restartItem.ClickedCh:
_, err := desktopctl.Restart(desktopctl.StartOptions{RepoRoot: repoRoot, ConfigPath: configPath, ExePath: exePath})
if err != nil {
lastErr = err.Error()
refresh()
continue
}
lastErr = ""
refresh()
case <-copyAnthropicEnv.ClickedCh:
_ = copyToClipboard(fmt.Sprintf("http://127.0.0.1:%d/v1", thinkingProxyPort))
case <-copyOpenAIEnv.ClickedCh:
_ = copyToClipboard(fmt.Sprintf("http://127.0.0.1:%d/v1", thinkingProxyPort))
case <-autoStartItem.ClickedCh:
if autoStartItem.Checked() {
_ = desktopctl.DisableWindowsRunAutostart(autostartAppName)
autoStartItem.Uncheck()
lastErr = ""
refresh()
continue
}
cmd, err := autostartCommand(repoRoot, configPath, exePath)
if err != nil {
lastErr = err.Error()
refresh()
continue
}
if err := desktopctl.EnableWindowsRunAutostart(autostartAppName, cmd); err != nil {
lastErr = err.Error()
refresh()
continue
}
autoStartItem.Check()
lastErr = ""
refresh()
case <-autoStartProxyItem.ClickedCh:
if autoStartProxyItem.Checked() {
_ = desktopctl.SetAutoStartProxy(false)
autoStartProxyItem.Uncheck()
lastErr = ""
refresh()
continue
}
_ = desktopctl.SetAutoStartProxy(true)
autoStartProxyItem.Check()
lastErr = ""
refresh()
case <-openProxyUI.ClickedCh:
if err := openProxyUIWithAutostart(repoRoot, configPath, exePath); err != nil {
lastErr = err.Error()
refresh()
case <-openDashboard.ClickedCh:
openProxyUIWithAutostart(engine, cfg, configPath, password)
case <-toggleItem.ClickedCh:
if engine.IsRunning() {
engine.Stop()
} else {
lastErr = ""
refresh()
}
case <-openLegacyUI.ClickedCh:
st, _ := desktopctl.StatusFor(configPath)
if !st.Running || strings.TrimSpace(st.BaseURL) == "" {
lastErr = "proxy is not running"
refresh()
continue
}
_ = desktopctl.OpenBrowser(st.BaseURL + "/management.html?legacy=1")
case <-openLogs.ClickedCh:
if err := desktopctl.OpenLogsFolder(repoRoot, configPath); err != nil {
lastErr = err.Error()
refresh()
}
case <-copyDiagnostics.ClickedCh:
if err := copyDiagnosticsToClipboard(configPath); err != nil {
lastErr = err.Error()
} else {
lastErr = "Diagnostics copied"
engine.Start(cfg, configPath, password)
}
refresh()
case <-checkUpdates.ClickedCh:
go func() {
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
defer cancel()
rel, err := proxypilotupdate.FetchLatestRelease(ctx)
if err != nil {
lastErr = err.Error()
refresh()
return
}
latestVersion = rel.Version()
assetName, assetURL = rel.FindPreferredAsset()
if strings.TrimSpace(assetURL) == "" {
lastErr = "no ProxyPilot asset found in latest release"
refresh()
return
}
// If this build is already tagged, avoid prompting unnecessarily.
if buildinfo.Version != "" && buildinfo.Version != "dev" && strings.EqualFold(buildinfo.Version, latestVersion) {
lastErr = "Up to date (" + latestVersion + ")"
updateNow.Disable()
refresh()
return
}
updateNow.SetTitle("Update ProxyPilot... (" + latestVersion + ")")
updateNow.Enable()
lastErr = "Update available (" + latestVersion + ")"
refresh()
}()
case <-updateNow.ClickedCh:
if strings.TrimSpace(assetURL) == "" {
lastErr = "no update asset URL (run Check for Updates)"
refresh()
continue
case <-copyURLItem.ClickedCh:
copyToClipboard(fmt.Sprintf("http://127.0.0.1:%d/v1", thinkingProxyPort))
case <-copyDiagItem.ClickedCh:
copyDiagnosticsToClipboard(engine)
case <-openLogsItem.ClickedCh:
desktopctl.OpenLogsFolder(repoRoot, configPath)
case <-openAuthItem.ClickedCh:
if dir, err := desktopctl.AuthDirFor(configPath); err == nil {
desktopctl.OpenFolder(dir)
}
go func() {
dest := filepath.Join(os.TempDir(), "ProxyPilot-"+sanitizeFileName(latestVersion)+"-"+sanitizeFileName(assetName))
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Minute)
defer cancel()
updateNow.SetTitle("Updating...")
lastErr = "Downloading update..."
refresh()
if err := proxypilotupdate.DownloadToFile(ctx, assetURL, dest); err != nil {
lastErr = err.Error()
updateNow.SetTitle("Update ProxyPilot...")
refresh()
return
}
// Run installer/zip handler and exit ProxyPilot so files can be replaced.
lastErr = "Launching installer..."
refresh()
_ = launchUpdate(dest)
systray.Quit()
}()
case <-quitItem.ClickedCh:
systray.Quit()
return
@@ -315,7 +215,7 @@ type closeFn func() error
func (c closeFn) Close() error { return c() }
func startThinkingProxy(configPath string) ioCloser {
func startThinkingProxy(engine *EmbeddedEngine) ioCloser {
ln, err := net.Listen("tcp", fmt.Sprintf("127.0.0.1:%d", thinkingProxyPort))
if err != nil {
// Best effort: don't crash the tray app if the port is already taken.
@@ -330,7 +230,7 @@ func startThinkingProxy(configPath string) ioCloser {
)
getProxy := func() (*httputil.ReverseProxy, *url.URL) {
st, _ := desktopctl.StatusFor(configPath)
st := engine.Status()
port := st.Port
if port <= 0 {
port = 8318
@@ -391,45 +291,183 @@ type ioCloser interface {
Close() error
}
func openProxyUIWithAutostart(repoRoot, configPath, exePath string) error {
// Prefer opening the desktop window if the UI binary exists next to this tray app.
if exe, err := os.Executable(); err == nil && strings.TrimSpace(exe) != "" {
dir := filepath.Dir(filepath.Clean(exe))
uiExe := filepath.Join(dir, "ProxyPilotUI.exe")
if _, err := os.Stat(uiExe); err == nil {
args := []string{}
if strings.TrimSpace(repoRoot) != "" {
args = append(args, "-repo", repoRoot)
}
if strings.TrimSpace(configPath) != "" {
args = append(args, "-config", configPath)
}
if strings.TrimSpace(exePath) != "" {
args = append(args, "-exe", exePath)
}
return exec.Command(uiExe, args...).Start()
func openProxyUIWithAutostart(engine *EmbeddedEngine, cfg *config.Config, configPath, password string) error {
// Start proxy if not running
if !engine.IsRunning() {
if err := engine.Start(cfg, configPath, password); err != nil {
// Continue anyway to show UI
}
}
// Fallback: open browser to the management UI (starts proxy if needed).
st, _ := desktopctl.StatusFor(configPath)
if !st.Running {
if _, err := desktopctl.Start(desktopctl.StartOptions{RepoRoot: repoRoot, ConfigPath: configPath, ExePath: exePath}); err != nil {
// Open embedded WebView2 dashboard
go func() {
openEmbeddedDashboard(engine, cfg, configPath, password)
}()
return nil
}
func openEmbeddedDashboard(engine *EmbeddedEngine, cfg *config.Config, configPath, password string) {
// Lock this goroutine to an OS thread - required for Windows COM/GUI operations
runtime.LockOSThread()
defer runtime.UnlockOSThread()
// Prevent opening multiple dashboard windows
dashboardMu.Lock()
if dashboardOpen {
dashboardMu.Unlock()
return
}
dashboardOpen = true
dashboardMu.Unlock()
defer func() {
dashboardMu.Lock()
dashboardOpen = false
dashboardMu.Unlock()
}()
// Recover from any panics to prevent the tray app from crashing
defer func() {
if r := recover(); r != nil {
fmt.Fprintf(os.Stderr, "WebView2 panic: %v\n", r)
// Try to open in browser as fallback
st := engine.Status()
if st.Running && strings.TrimSpace(st.BaseURL) != "" {
_ = desktopctl.OpenBrowser(st.BaseURL + "/proxypilot.html")
}
}
}()
target := assetServerURL + "/index.html"
if assetServerURL == "" {
// Fallback to browser if asset server failed
st := engine.Status()
if st.Running && strings.TrimSpace(st.BaseURL) != "" {
_ = desktopctl.OpenBrowser(st.BaseURL + "/proxypilot.html")
}
return
}
w := webview2.NewWithOptions(webview2.WebViewOptions{
Debug: true,
AutoFocus: true,
WindowOptions: webview2.WindowOptions{
Title: "ProxyPilot",
Width: 1200,
Height: 850,
Center: true,
IconId: 1, // Use embedded icon from resource.syso
},
})
if w == nil {
// Fallback to browser - WebView2 runtime may not be installed
fmt.Fprintf(os.Stderr, "WebView2 failed to initialize, falling back to browser\n")
_ = desktopctl.OpenBrowser(target)
return
}
defer w.Destroy()
// Bind desktop functions for the React UI
_ = w.Bind("pp_status", func() (map[string]any, error) {
cur := engine.Status()
return map[string]any{
"running": cur.Running,
"port": cur.Port,
"base_url": cur.BaseURL,
"last_error": cur.LastError,
}, nil
})
_ = w.Bind("pp_start", func() error {
return engine.Start(cfg, configPath, password)
})
_ = w.Bind("pp_stop", func() error { return engine.Stop() })
_ = w.Bind("pp_restart", func() error {
return engine.Restart(cfg, configPath, password)
})
_ = w.Bind("pp_open_logs", func() error { return desktopctl.OpenLogsFolder("", configPath) })
_ = w.Bind("pp_open_auth_folder", func() error {
dir, err := desktopctl.AuthDirFor(configPath)
if err != nil {
return err
}
deadline := time.Now().Add(3 * time.Second)
for time.Now().Before(deadline) {
st2, _ := desktopctl.StatusFor(configPath)
if st2.Running {
break
}
time.Sleep(150 * time.Millisecond)
return desktopctl.OpenFolder(dir)
})
_ = w.Bind("pp_get_oauth_private", func() (bool, error) { return desktopctl.GetOAuthPrivate() })
_ = w.Bind("pp_set_oauth_private", func(enabled bool) error { return desktopctl.SetOAuthPrivate(enabled) })
_ = w.Bind("pp_oauth", func(provider string) error { return startOAuthFlow(engine, getOAuthEndpoint(provider)) })
_ = w.Bind("pp_copy_diagnostics", func() error { return copyDiagnosticsToClipboard(engine) })
_ = w.Bind("pp_get_management_key", func() (string, error) { return desktopctl.GetManagementPassword() })
_ = w.Bind("pp_open_legacy_ui", func() error {
cur := engine.Status()
if !cur.Running {
return fmt.Errorf("proxy not running")
}
return desktopctl.OpenBrowser(cur.BaseURL + "/management.html?legacy=1")
})
_ = w.Bind("pp_open_diagnostics", func() error {
cur := engine.Status()
if !cur.Running {
return fmt.Errorf("proxy not running")
}
return desktopctl.OpenBrowser(cur.BaseURL + "/proxypilot.html")
})
_ = w.Bind("pp_get_requests", func() (any, error) {
return middleware.GetRequestMonitor(), nil
})
_ = w.Bind("pp_get_usage", func() (any, error) {
stats := usage.GetRequestStatistics()
if stats == nil {
return nil, fmt.Errorf("usage statistics not available")
}
return usage.ComputeUsageStats(stats.Snapshot()), nil
})
_ = w.Bind("pp_detect_agents", func() ([]integrations.Agent, error) {
st := engine.Status()
proxyURL := st.BaseURL
if proxyURL == "" {
proxyURL = fmt.Sprintf("http://127.0.0.1:%d", st.Port)
}
return integrations.DetectCLIAgents(proxyURL), nil
})
_ = w.Bind("pp_configure_agent", func(agentID string) error {
st := engine.Status()
proxyURL := st.BaseURL
if proxyURL == "" {
proxyURL = fmt.Sprintf("http://127.0.0.1:%d", st.Port)
}
return integrations.ConfigureCLIAgent(agentID, proxyURL)
})
_ = w.Bind("pp_unconfigure_agent", func(agentID string) error {
return integrations.UnconfigureCLIAgent(agentID)
})
_ = w.Bind("pp_check_updates", func() (*updates.UpdateInfo, error) {
return updates.CheckForUpdates()
})
_ = w.Bind("pp_download_update", func(url string) error {
return desktopctl.OpenBrowser(url)
})
w.Navigate(target)
w.Run()
}
func getOAuthEndpoint(provider string) string {
switch strings.ToLower(strings.TrimSpace(provider)) {
case "antigravity":
return "/v0/management/antigravity-auth-url"
case "gemini-cli", "geminicli":
return "/v0/management/gemini-cli-auth-url"
case "codex":
return "/v0/management/codex-auth-url"
case "claude", "anthropic":
return "/v0/management/anthropic-auth-url"
case "qwen":
return "/v0/management/qwen-auth-url"
case "iflow":
return "/v0/management/iflow-auth-url"
default:
return ""
}
st, _ = desktopctl.StatusFor(configPath)
if strings.TrimSpace(st.BaseURL) == "" {
return fmt.Errorf("proxy base URL not available")
}
return desktopctl.OpenBrowser(st.BaseURL + "/proxypilot.html")
}
func shorten(s string, max int) string {
@@ -470,8 +508,8 @@ type authURLResponse struct {
Error string `json:"error"`
}
func startOAuthFlow(configPath string, endpointPath string) error {
st, _ := desktopctl.StatusFor(configPath)
func startOAuthFlow(engine *EmbeddedEngine, endpointPath string) error {
st := engine.Status()
if !st.Running || strings.TrimSpace(st.BaseURL) == "" {
return fmt.Errorf("proxy is not running")
}
@@ -511,8 +549,8 @@ func startOAuthFlow(configPath string, endpointPath string) error {
return openOAuthURL(out.URL, private)
}
func copyDiagnosticsToClipboard(configPath string) error {
st, _ := desktopctl.StatusFor(configPath)
func copyDiagnosticsToClipboard(engine *EmbeddedEngine) error {
st := engine.Status()
if !st.Running || strings.TrimSpace(st.BaseURL) == "" {
return fmt.Errorf("proxy is not running")
}
@@ -588,7 +626,7 @@ func findEdge() (string, error) {
return "", fmt.Errorf("msedge.exe not found")
}
func autostartCommand(repoRoot, configPath, exePath string) (string, error) {
func autostartCommand(repoRoot, configPath string) (string, error) {
exe, err := os.Executable()
if err == nil && strings.TrimSpace(exe) != "" {
exe = filepath.Clean(exe)
@@ -598,23 +636,19 @@ func autostartCommand(repoRoot, configPath, exePath string) (string, error) {
if exe == "" {
return "", fmt.Errorf("unable to resolve tray executable path")
}
args := make([]string, 0, 6)
args := make([]string, 0, 4)
if strings.TrimSpace(repoRoot) != "" {
args = append(args, "-repo", repoRoot)
}
if strings.TrimSpace(configPath) != "" {
args = append(args, "-config", configPath)
}
if strings.TrimSpace(exePath) != "" {
args = append(args, "-exe", exePath)
}
return quoteWindowsCommand(exe, args), nil
}
func applyDefaults(repoRoot, configPath, exePath string) (string, string, string) {
func applyDefaults(repoRoot, configPath string) (string, string) {
repoRoot = strings.TrimSpace(repoRoot)
configPath = strings.TrimSpace(configPath)
exePath = strings.TrimSpace(exePath)
exe, _ := os.Executable()
exeDir := ""
@@ -640,17 +674,7 @@ func applyDefaults(repoRoot, configPath, exePath string) (string, string, string
ensureConfig(configPath)
}
if exePath == "" && exeDir != "" {
for _, name := range []string{"proxypilot-engine.exe", "cliproxyapi-latest.exe"} {
cand := filepath.Join(exeDir, name)
if _, err := os.Stat(cand); err == nil {
exePath = cand
break
}
}
}
return repoRoot, configPath, exePath
return repoRoot, configPath
}
func ensureConfig(configPath string) {
Binary file not shown.
+40
View File
@@ -0,0 +1,40 @@
{
"FixedFileInfo": {
"FileVersion": {
"Major": 0,
"Minor": 1,
"Patch": 5,
"Build": 0
},
"ProductVersion": {
"Major": 0,
"Minor": 1,
"Patch": 5,
"Build": 0
},
"FileFlagsMask": "3f",
"FileFlags ": "00",
"FileOS": "040004",
"FileType": "01",
"FileSubType": "00"
},
"StringFileInfo": {
"Comments": "Local AI Proxy with Embedded Engine",
"CompanyName": "ProxyPilot",
"FileDescription": "ProxyPilot - Single Binary Desktop App",
"FileVersion": "0.1.5",
"InternalName": "ProxyPilot",
"LegalCopyright": "MIT License",
"OriginalFilename": "ProxyPilot.exe",
"ProductName": "ProxyPilot",
"ProductVersion": "0.1.5"
},
"VarFileInfo": {
"Translation": {
"LangID": "0409",
"CharsetID": "04B0"
}
},
"IconPath": "../../static/icon.ico",
"ManifestPath": ""
}
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
+1 -1
View File
@@ -2,5 +2,5 @@ package assets
import "embed"
//go:embed index.html vite.svg assets/*
//go:embed index.html vite.svg logo.png assets/*
var FS embed.FS
+5
View File
@@ -5,6 +5,11 @@
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>webui</title>
<!-- Google Fonts preconnect for performance -->
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<!-- Google Fonts: Space Grotesk (display/headers), JetBrains Mono (code), IBM Plex Sans (body) -->
<link href="https://fonts.googleapis.com/css2?family=IBM+Plex+Sans:wght@400;500;600&family=JetBrains+Mono:wght@400;500;600&family=Space+Grotesk:wght@400;500;600;700&display=swap" rel="stylesheet">
<script type="module" crossorigin src="/assets/index.js"></script>
<link rel="stylesheet" crossorigin href="/assets/index.css">
</head>
Binary file not shown.

After

Width:  |  Height:  |  Size: 194 KiB

+312 -1
View File
@@ -21,6 +21,8 @@ import (
"github.com/jchv/go-webview2"
"github.com/router-for-me/CLIProxyAPI/v6/cmd/proxypilotui/assets"
"github.com/router-for-me/CLIProxyAPI/v6/internal/desktopctl"
"github.com/router-for-me/CLIProxyAPI/v6/internal/logging"
_ "github.com/router-for-me/CLIProxyAPI/v6/internal/translator"
)
func main() {
@@ -29,13 +31,21 @@ func main() {
var exePath string
var url string
var standalone bool
var legacyMode bool
flag.StringVar(&repoRoot, "repo", "", "Repo root (used to locate bin/ and logs/)")
flag.StringVar(&configPath, "config", "", "Path to config.yaml (defaults to <repo>/config.yaml)")
flag.StringVar(&exePath, "exe", "", "Path to proxy engine binary (defaults to <repo>/bin/proxypilot-engine.exe)")
flag.StringVar(&exePath, "exe", "", "Path to proxy engine binary (legacy mode only)")
flag.StringVar(&url, "url", "", "Open a specific URL in-app (advanced)")
flag.BoolVar(&standalone, "standalone", false, "Run standalone UI without connecting to proxy server")
flag.BoolVar(&legacyMode, "legacy", false, "Use legacy subprocess mode (separate engine binary)")
flag.Parse()
// Enable embedded mode by default (single binary)
if !legacyMode {
desktopctl.SetEmbeddedMode(true)
logging.SetupBaseLogger()
}
repoRoot, configPath, exePath = applyDefaults(repoRoot, configPath, exePath)
var assetServerURL string
@@ -130,6 +140,7 @@ func main() {
cur, _ := desktopctl.StatusFor(configPath)
return map[string]any{
"running": cur.Running,
"version": cur.Version,
"port": cur.Port,
"thinking_port": cur.ThinkingPort,
"thinking_running": cur.ThinkingRunning,
@@ -193,6 +204,31 @@ func main() {
_ = w.Bind("pp_copy_diagnostics", func() error { return copyDiagnosticsToClipboard(configPath) })
_ = w.Bind("pp_get_management_key", func() (string, error) { return desktopctl.GetManagementPassword() })
_ = w.Bind("pp_check_updates", func() (map[string]any, error) {
st, _ := desktopctl.StatusFor(configPath)
if !st.Running {
return nil, fmt.Errorf("proxy not running")
}
key, _ := desktopctl.GetManagementPassword()
client := &http.Client{Timeout: 10 * time.Second}
req, _ := http.NewRequest("GET", st.BaseURL+"/v0/management/updates/check", nil)
req.Header.Set("X-Management-Key", key)
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var res map[string]any
if err := json.NewDecoder(resp.Body).Decode(&res); err != nil {
return nil, err
}
return res, nil
})
_ = w.Bind("pp_download_update", func(url string) error {
return desktopctl.OpenBrowser(url)
})
// Integration bindings
_ = w.Bind("pp_get_integrations", func() ([]map[string]any, error) {
st, _ := desktopctl.StatusFor(configPath)
@@ -238,6 +274,42 @@ func main() {
return nil
})
// Agent detection bindings
_ = w.Bind("pp_detect_agents", func() ([]map[string]any, error) {
agents := detectCLIAgents()
return agents, nil
})
_ = w.Bind("pp_configure_agent", func(agentID string) error {
st, _ := desktopctl.StatusFor(configPath)
if !st.Running {
return fmt.Errorf("proxy not running")
}
key, _ := desktopctl.GetManagementPassword()
client := &http.Client{Timeout: 10 * time.Second}
req, _ := http.NewRequest("POST", st.BaseURL+"/v0/management/integrations/"+agentID, nil)
req.Header.Set("X-Management-Key", key)
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
b, _ := io.ReadAll(resp.Body)
return fmt.Errorf("failed: %s", string(b))
}
return nil
})
// Model mappings bindings
_ = w.Bind("pp_get_model_mappings", func() ([]map[string]any, error) {
return getModelMappings(), nil
})
_ = w.Bind("pp_save_model_mappings", func(mappings []map[string]any) error {
return saveModelMappings(mappings)
})
if target != "" {
w.Navigate(target)
w.Run()
@@ -579,6 +651,29 @@ func controlCenterHTML() string {
<div class="hint">These open the provider login flow in your browser and save auth files for ProxyPilot.</div>
</div>
<div class="card" style="grid-column: 1 / -1">
<div class="sectionTitle">CLI Agents</div>
<div class="row" id="agentsList">
<div style="color:var(--muted);font-size:13px">Detecting agents...</div>
</div>
<div class="note">
Detected CLI agents. Click to configure for ProxyPilot.
</div>
</div>
<div class="card" style="grid-column: 1 / -1">
<div class="sectionTitle">Model Mappings</div>
<div id="mappingsList" style="margin-bottom:12px"></div>
<div class="row" style="gap:8px;flex-wrap:wrap">
<input type="text" id="mapFrom" placeholder="Alias (e.g. smart)" style="flex:1;min-width:120px;padding:8px 10px;background:rgba(0,0,0,.3);border:1px solid var(--line);border-radius:8px;color:var(--text);font-size:13px" />
<input type="text" id="mapTo" placeholder="Model (e.g. claude-opus-4-5)" style="flex:2;min-width:180px;padding:8px 10px;background:rgba(0,0,0,.3);border:1px solid var(--line);border-radius:8px;color:var(--text);font-size:13px" />
<button id="addMappingBtn" class="btnPrimary">Add</button>
</div>
<div class="note">
Route friendly model names to actual models.
</div>
</div>
<div class="card" style="grid-column: 1 / -1">
<div class="sectionTitle">Integrations</div>
<div class="row" id="integrationsList">
@@ -697,12 +792,84 @@ func controlCenterHTML() string {
}
}
async function refreshAgents() {
const list = $('agentsList');
try {
const agents = await window.pp_detect_agents();
if(!agents || agents.length === 0) {
list.innerHTML = '<div style="color:var(--muted);font-size:13px">No agents detected</div>';
return;
}
list.innerHTML = '';
agents.forEach(a => {
const btn = document.createElement('button');
if (!a.detected) {
btn.className = 'btn';
btn.disabled = true;
btn.textContent = a.name + ' (Not Found)';
} else if (a.configured) {
btn.className = 'btnGood';
btn.textContent = a.name + ' (Configured)';
} else {
btn.className = 'btnPrimary';
btn.textContent = a.name + ' (Configure)';
}
btn.onclick = async () => {
if (!a.detected) return;
try {
await window.pp_configure_agent(a.id);
toast('Configured ' + a.name);
refreshAgents();
} catch(e) { toast(e.message||String(e)); }
};
list.appendChild(btn);
});
} catch(e) {
list.innerHTML = '<div style="color:var(--muted);font-size:13px">Error: ' + (e.message||e) + '</div>';
}
}
async function refreshMappings() {
const list = $('mappingsList');
try {
const mappings = await window.pp_get_model_mappings();
if(!mappings || mappings.length === 0) {
list.innerHTML = '<div style="color:var(--muted);font-size:13px">No mappings configured</div>';
return;
}
list.innerHTML = '<div style="display:flex;flex-direction:column;gap:6px">' +
mappings.map((m, i) =>
'<div style="display:flex;align-items:center;gap:8px;padding:8px 10px;background:rgba(0,0,0,.25);border-radius:6px;font-size:13px">' +
'<code style="color:var(--brand)">' + m.from + '</code>' +
'<span style="color:var(--muted)">&rarr;</span>' +
'<code style="color:var(--good)">' + m.to + '</code>' +
'<button class="btnDanger" style="margin-left:auto;padding:4px 8px;font-size:11px" data-idx="' + i + '">Remove</button>' +
'</div>'
).join('') + '</div>';
list.querySelectorAll('button[data-idx]').forEach(btn => {
btn.onclick = async () => {
const idx = parseInt(btn.dataset.idx);
const updated = mappings.filter((_, i) => i !== idx);
try {
await window.pp_save_model_mappings(updated);
toast('Mapping removed');
refreshMappings();
} catch(e) { toast(e.message||String(e)); }
};
});
} catch(e) {
list.innerHTML = '<div style="color:var(--muted);font-size:13px">Error loading mappings</div>';
}
}
async function init() {
try{
const priv = await window.pp_get_oauth_private();
$('privateOAuth').checked = !!priv;
}catch(e){}
await refreshStatus();
await refreshAgents();
await refreshMappings();
setInterval(refreshStatus, 1200);
}
@@ -733,6 +900,21 @@ func controlCenterHTML() string {
$('oauthQwen').addEventListener('click', oauth('qwen'));
$('oauthIflow').addEventListener('click', oauth('iflow'));
$('addMappingBtn').addEventListener('click', async () => {
const from = $('mapFrom').value.trim();
const to = $('mapTo').value.trim();
if (!from || !to) { toast('Both fields required'); return; }
try {
const existing = await window.pp_get_model_mappings() || [];
existing.push({ from, to, enabled: true });
await window.pp_save_model_mappings(existing);
$('mapFrom').value = '';
$('mapTo').value = '';
toast('Mapping added');
refreshMappings();
} catch(e) { toast(e.message||String(e)); }
});
init();
</script>
</body>
@@ -751,3 +933,132 @@ func messageBox(title, text string) error {
}
return err
}
// detectCLIAgents returns a list of detected CLI agents
func detectCLIAgents() []map[string]any {
agents := []map[string]any{}
// Claude Code
claude := map[string]any{
"id": "claude",
"name": "Claude Code",
"detected": false,
"configured": false,
}
if path, err := exec.LookPath("claude"); err == nil {
claude["detected"] = true
claude["binary_path"] = path
}
home, _ := os.UserHomeDir()
claudeConfig := filepath.Join(home, ".claude", "settings.json")
if _, err := os.Stat(claudeConfig); err == nil {
claude["detected"] = true
claude["config_path"] = claudeConfig
if content, err := os.ReadFile(claudeConfig); err == nil {
if strings.Contains(string(content), "127.0.0.1:8317") || strings.Contains(string(content), "127.0.0.1:8318") {
claude["configured"] = true
}
}
}
agents = append(agents, claude)
// Codex CLI
codex := map[string]any{
"id": "codex",
"name": "Codex CLI",
"detected": false,
"configured": false,
}
if path, err := exec.LookPath("codex"); err == nil {
codex["detected"] = true
codex["binary_path"] = path
}
codexDir := filepath.Join(home, ".codex")
if info, err := os.Stat(codexDir); err == nil && info.IsDir() {
codex["detected"] = true
codex["config_path"] = codexDir
codexConfig := filepath.Join(codexDir, "config.toml")
if content, err := os.ReadFile(codexConfig); err == nil {
if strings.Contains(string(content), "127.0.0.1:8317") || strings.Contains(string(content), "127.0.0.1:8318") {
codex["configured"] = true
}
}
}
agents = append(agents, codex)
// Factory Droid
droid := map[string]any{
"id": "droid",
"name": "Factory Droid",
"detected": false,
"configured": false,
}
if path, err := exec.LookPath("droid"); err == nil {
droid["detected"] = true
droid["binary_path"] = path
} else if path, err := exec.LookPath("factory"); err == nil {
droid["detected"] = true
droid["binary_path"] = path
}
// Droid uses settings.json for runtime config (not config.json)
droidSettings := filepath.Join(home, ".factory", "settings.json")
if _, err := os.Stat(droidSettings); err == nil {
droid["config_path"] = droidSettings
droid["detected"] = true
if content, err := os.ReadFile(droidSettings); err == nil {
// Check for ProxyPilot models in customModels array
if strings.Contains(string(content), "ProxyPilot") || strings.Contains(string(content), "127.0.0.1:8318") {
droid["configured"] = true
}
}
}
agents = append(agents, droid)
// Gemini CLI
gemini := map[string]any{
"id": "gemini",
"name": "Gemini CLI",
"detected": false,
"configured": false,
}
if path, err := exec.LookPath("gemini"); err == nil {
gemini["detected"] = true
gemini["binary_path"] = path
}
agents = append(agents, gemini)
return agents
}
// getModelMappings reads model mappings from the config file
func getModelMappings() []map[string]any {
home, _ := os.UserHomeDir()
mappingsPath := filepath.Join(home, ".proxypilot", "model-mappings.json")
data, err := os.ReadFile(mappingsPath)
if err != nil {
return []map[string]any{}
}
var mappings []map[string]any
if err := json.Unmarshal(data, &mappings); err != nil {
return []map[string]any{}
}
return mappings
}
// saveModelMappings saves model mappings to the config file
func saveModelMappings(mappings []map[string]any) error {
home, _ := os.UserHomeDir()
mappingsDir := filepath.Join(home, ".proxypilot")
if err := os.MkdirAll(mappingsDir, 0755); err != nil {
return err
}
mappingsPath := filepath.Join(mappingsDir, "model-mappings.json")
data, err := json.MarshalIndent(mappings, "", " ")
if err != nil {
return err
}
return os.WriteFile(mappingsPath, data, 0644)
}
+16
View File
@@ -81,6 +81,10 @@ func main() {
var kiroAWSAuthCode bool
var kiroImport bool
// var githubCopilotLogin bool // REMOVED - GitHub Copilot excluded
var detectAgents bool
var setupClaude bool
var setupCodex bool
var setupDroid bool
var projectID string
var vertexImport string
var configPath string
@@ -105,6 +109,10 @@ func main() {
flag.BoolVar(&kiroAWSAuthCode, "kiro-aws-authcode", false, "Login to Kiro using AWS Builder ID (authorization code flow, better UX)")
flag.BoolVar(&kiroImport, "kiro-import", false, "Import Kiro token from Kiro IDE (~/.aws/sso/cache/kiro-auth-token.json)")
// GitHub Copilot login removed
flag.BoolVar(&detectAgents, "detect-agents", false, "Detect installed CLI agents")
flag.BoolVar(&setupClaude, "setup-claude", false, "Configure Claude Code to use ProxyPilot")
flag.BoolVar(&setupCodex, "setup-codex", false, "Configure Codex CLI to use ProxyPilot")
flag.BoolVar(&setupDroid, "setup-droid", false, "Configure Factory Droid to use ProxyPilot")
flag.StringVar(&projectID, "project_id", "", "Project ID (Gemini only, not required)")
flag.StringVar(&configPath, "config", DefaultConfigPath, "Configure File Path")
flag.StringVar(&vertexImport, "vertex-import", "", "Import Vertex service account key JSON file")
@@ -533,6 +541,14 @@ func main() {
cmd.DoKiroAWSAuthCodeLogin(cfg, options)
} else if kiroImport {
cmd.DoKiroImport(cfg, options)
} else if detectAgents {
cmd.DoDetectAgents()
} else if setupClaude {
cmd.DoSetupClaude(cfg)
} else if setupCodex {
cmd.DoSetupCodex(cfg)
} else if setupDroid {
cmd.DoSetupDroid(cfg)
} else {
// In cloud deploy mode without config file, just wait for shutdown signals
if isCloudDeploy && !configFileExists {
Binary file not shown.
+40
View File
@@ -0,0 +1,40 @@
{
"FixedFileInfo": {
"FileVersion": {
"Major": 0,
"Minor": 1,
"Patch": 5,
"Build": 0
},
"ProductVersion": {
"Major": 0,
"Minor": 1,
"Patch": 5,
"Build": 0
},
"FileFlagsMask": "3f",
"FileFlags ": "00",
"FileOS": "040004",
"FileType": "01",
"FileSubType": "00"
},
"StringFileInfo": {
"Comments": "Local AI Proxy Engine",
"CompanyName": "ProxyPilot",
"FileDescription": "ProxyPilot Engine",
"FileVersion": "0.1.5",
"InternalName": "proxypilot-engine",
"LegalCopyright": "MIT License",
"OriginalFilename": "proxypilot-engine.exe",
"ProductName": "ProxyPilot",
"ProductVersion": "0.1.5"
},
"VarFileInfo": {
"Translation": {
"LangID": "0409",
"CharsetID": "04B0"
}
},
"IconPath": "../../static/icon.ico",
"ManifestPath": ""
}
+48 -2
View File
@@ -26,8 +26,15 @@ remote-management:
# Authentication directory (supports ~ for home directory)
auth-dir: "~/.cli-proxy-api"
# Long-running agent harness files root directory (feature_list.json, claude-progress.txt).
# Defaults to the server working directory when empty.
# The harness guides AI agents through structured, incremental development workflows.
# - INITIALIZER mode: Creates feature list, progress log, and init.sh for new sessions
# - CODING mode: Forces one-feature-at-a-time workflow with verification
# Leave empty to use session-based memory storage (recommended).
# Set to a specific directory to share harness files across sessions.
# harness-root-dir: ""
#
# Environment variables:
# CLIPROXY_HARNESS_ENABLED=true/false # Enable/disable harness injection (default: true)
# API keys for authentication
api-keys:
@@ -71,13 +78,49 @@ quota-exceeded:
routing:
strategy: "round-robin" # round-robin (default), fill-first
# Global model mappings - route friendly names to actual models across all providers.
# These mappings are checked before per-credential model mappings.
# global-model-mappings:
# - from: "smart" # Client requests "smart"
# to: "claude-opus-4-5-20251101" # Routes to Claude Opus
# - from: "rush"
# to: "claude-haiku-4-5-20251001" # Routes to Claude Haiku (fast)
# - from: "think"
# to: "claude-opus-4-5-thinking" # Routes to thinking model
# - from: "gemini-pro"
# to: "gemini-2.5-pro-preview"
# provider: "gemini" # Only applies to Gemini provider
# - from: "cheap"
# to: "gpt-4o-mini"
# provider: "codex"
# enabled: false # Disabled mapping (kept for reference)
# When true, enable authentication for the WebSocket API (/v1/ws).
ws-auth: false
# Streaming behavior (SSE keep-alives + safe bootstrap retries).
# Streaming behavior (SSE keep-alives, bootstrap retries, chunk sizing).
# streaming:
# keepalive-seconds: 15 # Default: 0 (disabled). <= 0 disables keep-alives.
# bootstrap-retries: 1 # Default: 0 (disabled). Retries before first byte is sent.
# max-chunk-size: 65536 # Default: 65536 (64KB). Max bytes per response chunk. 0 disables limiting.
# Context compression behavior (Factory.ai-style structured summarization).
# Uses LLM to generate intelligent summaries with structured sections when context
# exceeds the threshold. Summaries include: session intent, file modifications,
# decisions made, next steps, and technical details.
# compression:
# enabled: true # Default: true. Enable LLM-based structured summarization.
# threshold-percent: 0.75 # Default: 0.75. Trigger compression at 75% of context window.
# max-summary-tokens: 2000 # Default: 2000. Maximum tokens for generated summaries.
# summarization-timeout-seconds: 30 # Default: 30. Timeout for LLM summarization calls.
# fallback-to-regex: true # Default: true. Use regex-based summary when LLM fails.
# Token-aware compression (proactive trimming based on model context window).
# Prevents "prompt too long" errors by trimming before hitting model limits.
# Environment variables:
# CLIPROXY_TOKEN_AWARE_ENABLED=true # Default: true. Enable token-based compression.
# CLIPROXY_COMPRESSION_THRESHOLD=0.75 # Default: 0.75. Trim at 75% of context window.
# CLIPROXY_RESERVE_TOKENS=8192 # Default: 8192. Tokens reserved for model output.
# Gemini API keys
# gemini-api-key:
@@ -102,6 +145,9 @@ ws-auth: false
# headers:
# X-Custom-Header: "custom-value"
# proxy-url: "socks5://proxy.example.com:1080" # optional: per-key proxy override
# models:
# - name: "gpt-5-codex" # upstream model name
# alias: "codex-latest" # client alias mapped to the upstream model
# excluded-models:
# - "gpt-5.2" # exclude specific models (exact match)
# - "gpt-5-*" # wildcard matching prefix (e.g. gpt-5-medium, gpt-5-codex)
+287
View File
@@ -0,0 +1,287 @@
# ProxyPilot Desktop App Build Process
Documentation of the desktop application build and release process established on December 27, 2025.
## Overview
ProxyPilot Desktop is a **single executable** that includes both the system tray UI and the proxy engine:
- **ProxyPilot.exe** - System tray app with embedded WebView2 dashboard and embedded proxy engine
## Architecture
```
┌─────────────────────────────────────────────────────────────────┐
│ ProxyPilot.exe │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────────────────────┐ │
│ │ System Tray │ │ WebView2 │ │ Embedded Assets │ │
│ │ (systray) │ │ Dashboard │ │ (webui/dist) │ │
│ └─────────────┘ └─────────────┘ └─────────────────────────┘ │
│ │
│ ┌─────────────────────────────────────────────────────────────┐│
│ │ Embedded Proxy Engine (goroutine) ││
│ │ ┌─────────────┐ ┌─────────────┐ ┌─────────────────────┐ ││
│ │ │Thinking Proxy│ │ Routing │ │ Provider Auth │ ││
│ │ │ (port 8317) │ │ Engine │ │ Management │ ││
│ │ └─────────────┘ └─────────────┘ └─────────────────────┘ ││
│ └─────────────────────────────────────────────────────────────┘│
└─────────────────────────────────────────────────────────────────┘
```
### In-Process Engine
The proxy engine runs as a goroutine within the same process, managed by `EmbeddedEngine`:
```go
// cmd/cliproxytray/engine.go
type EmbeddedEngine struct {
mu sync.Mutex
service *cliproxy.Service
cancel context.CancelFunc
running bool
port int
// ...
}
func (e *EmbeddedEngine) Start(cfg *config.Config, configPath, password string) error
func (e *EmbeddedEngine) Stop() error
func (e *EmbeddedEngine) Restart(cfg *config.Config, configPath, password string) error
func (e *EmbeddedEngine) Status() Status
```
Benefits of single-binary architecture:
- **Simpler deployment**: One file to distribute and install
- **Smaller total size**: ~29MB vs ~46MB (shared Go runtime)
- **Faster startup**: No process spawning overhead
- **Easier debugging**: Single process to monitor
## Build Process
### Prerequisites
```bash
# Required tools
go install github.com/josephspurrier/goversioninfo/cmd/goversioninfo@latest
```
Inno Setup 6 must be installed for creating the Windows installer.
### Step 1: Build Web UI Assets
```bash
cd webui
npm install
npm run build
```
This generates `webui/dist/` with the dashboard assets.
### Step 2: Create Multi-Resolution Icon
The icon must have multiple sizes for proper Windows display (16px to 256px):
```python
from PIL import Image
img = Image.open('static/icon.png')
if img.mode != 'RGBA':
img = img.convert('RGBA')
sizes = [(16,16), (24,24), (32,32), (48,48), (64,64), (128,128), (256,256)]
img.save('static/icon.ico', format='ICO', sizes=sizes)
```
Copy to tray icon location:
```bash
cp static/icon.ico internal/trayicon/icon.ico
```
### Step 3: Generate Windows Resource Files
Generate `.syso` files for embedding icons in executables:
```bash
cd cmd/cliproxytray
goversioninfo -icon="../../static/icon.ico"
```
This creates `resource.syso` files that Go automatically includes during build.
### Step 4: Build Executable
```bash
# Build single-binary tray app (with -H windowsgui to hide console)
GOOS=windows GOARCH=amd64 go build -ldflags="-s -w -H windowsgui" -o dist/ProxyPilot.exe ./cmd/cliproxytray
```
### Step 5: Build Installer
```bash
# Using Inno Setup
"C:\Users\FSOS\AppData\Local\Programs\Inno Setup 6\ISCC.exe" installer/proxypilot.iss
```
Output: `dist/ProxyPilot-0.1.0-Setup.exe`
## Key Files
### Version Info Configuration
**cmd/cliproxytray/versioninfo.json**
```json
{
"FixedFileInfo": {
"FileVersion": {"Major": 0, "Minor": 1, "Patch": 0, "Build": 0},
"ProductVersion": {"Major": 0, "Minor": 1, "Patch": 0, "Build": 0}
},
"StringFileInfo": {
"FileDescription": "ProxyPilot Desktop App",
"ProductName": "ProxyPilot",
"ProductVersion": "0.1.0"
},
"IconPath": "../../static/icon.ico"
}
```
### Embedded Engine
**cmd/cliproxytray/engine.go** - Manages the in-process proxy service:
- Uses `cliproxy.NewBuilder()` SDK to build the service
- Runs in a goroutine with cancellable context
- Thread-safe start/stop/restart operations
### Tray Icon Embedding
**internal/trayicon/ico.go**
```go
package trayicon
import (
_ "embed"
)
//go:embed icon.ico
var proxyPilotICO []byte
func ProxyPilotICO() []byte {
return proxyPilotICO
}
```
### Installer Script
**installer/proxypilot.iss** - Inno Setup script that:
- Installs ProxyPilot.exe to `%LOCALAPPDATA%\ProxyPilot`
- Creates Start Menu and Desktop shortcuts
- Adds optional Windows startup entry
- Copies config.example.yaml and creates config.yaml on first install
- Uses ProxyPilot icon for installer and shortcuts
## System Tray Menu
Simplified flat menu structure:
```
┌─────────────────────┐
│ Open Dashboard │ → Opens WebView2 window
├─────────────────────┤
│ Start/Stop │ → Toggle proxy engine
│ Copy API URL │ → Copies http://127.0.0.1:8317/v1
├─────────────────────┤
│ Quit │ → Exit application
└─────────────────────┘
```
## Release Process
### Create Release on GitHub
```bash
# Tag the release
git tag v0.1.0
git push origin v0.1.0
# Create release with assets
gh release create v0.1.0 \
--title "ProxyPilot v0.1.0" \
--notes "Release notes here" \
dist/ProxyPilot-0.1.0-Setup.exe \
dist/ProxyPilot.exe \
config.example.yaml
```
### Update Existing Release
```bash
# Delete old asset
gh release delete-asset v0.1.0 ProxyPilot.exe --yes
# Upload new asset
gh release upload v0.1.0 dist/ProxyPilot.exe
```
## Troubleshooting
### Icon Not Showing in File Explorer
1. Ensure icon.ico has 256x256 size (required for Windows)
2. Clear Windows icon cache: `ie4uinit.exe -show`
3. Verify resource.syso was generated after icon update
4. Rebuild the executable
### WebView2 Dashboard Not Loading
1. Ensure WebView2 runtime is installed on the system
2. Check that webui/dist/ assets are embedded (go:embed directive)
3. Verify the asset server is starting on a free port
### Tray Icon Shows Wrong Image
1. Check internal/trayicon/icon.ico is the correct file
2. Rebuild the tray app to re-embed the icon
3. Restart the application
### Engine Not Starting
1. Check if port 8317/8318 are already in use
2. Review logs in `%LOCALAPPDATA%\ProxyPilot\logs\`
3. Ensure config.yaml is valid YAML
## File Structure
```
ProxyPilot/
├── cmd/
│ └── cliproxytray/
│ ├── main_windows.go # Tray app with embedded engine
│ ├── engine.go # EmbeddedEngine implementation
│ ├── versioninfo.json # Version/icon config
│ └── resource.syso # Compiled resources
├── internal/
│ └── trayicon/
│ ├── ico.go # Embeds icon.ico
│ └── icon.ico # ProxyPilot logo (multi-res)
├── static/
│ ├── icon.ico # Source icon (7 sizes)
│ └── icon.png # Original PNG logo
├── webui/
│ ├── src/ # React dashboard source
│ └── dist/ # Built assets (embedded)
├── installer/
│ └── proxypilot.iss # Inno Setup script
└── dist/ # Distribution files
└── ProxyPilot.exe # Single-binary (~29MB)
```
## Version History
- **v0.2.0** (2025-12-27) - Single-binary architecture
- Embedded proxy engine runs in-process
- Removed separate proxypilot-engine.exe
- Reduced total size from ~46MB to ~29MB
- Faster startup with no process spawning
- **v0.1.0** (2025-12-27) - Initial desktop app release
- System tray with minimal menu
- Embedded WebView2 dashboard
- Multi-resolution icon support
- Single-file Windows installer
-128
View File
@@ -1,128 +0,0 @@
# Desktop UI (ProxyPilot) — Shared Spec (Windows first)
This doc captures shared architecture and product decisions for a desktop “controller app” that manages a local `ProxyPilot` instance.
OS-specific details live in:
- Windows: `docs/windows-ui-spec.md`
- macOS (future): `docs/macos-ui-spec.md` (not yet)
## What we’re building
A small tray-first desktop app that can:
- start/stop/restart the local proxy engine (ProxyPilot Engine)
- show health + port + “copy base URL” snippets for IDE/agentic CLIs
- surface logs/request diagnostics to debug issues like 400/429, “prompt too long”, streaming quirks
It is not a full replacement for the server’s Management UI; it’s a launcher + diagnostics tool.
## Two-shell strategy (Wails + Tauri)
We can support both Wails and Tauri without forking business logic by standardizing on a single control interface.
### Primary shell: Wails (Go)
- Pros: simplest integration with existing Go codebase, easiest to ship v1 fast.
- Architecture: Wails UI ↔ Go control layer (in-process).
### Secondary shell: Tauri (Rust)
- Pros: smaller footprint, strong native tray/window tooling, good distribution story.
- Architecture (recommended): Tauri UI ↔ helper CLI ↔ Go control layer.
- Tauri calls `cliproxyctl` (or similar) as a subprocess.
- Benefits: no Rust↔Go FFI, no duplicated process logic, easy testing.
## Shared control interface
### Go package
- `internal/desktopctl`
- Pure Go logic for process management, config reading/writing, health, logs, autostart primitives.
- No UI dependencies.
### Suggested helper CLI (used by Tauri and power users)
- `cmd/cliproxyctl`
- `cliproxyctl status --config <path>`
- `cliproxyctl start --config <path>`
- `cliproxyctl stop`
- `cliproxyctl restart --config <path>`
- `cliproxyctl diag --config <path> --out <file|clipboard>`
Wails can call `internal/desktopctl` directly; Tauri can call `cliproxyctl`.
## Feature scope (v1)
### Core controls
- Start/Stop/Restart the proxy engine.
- Detect if a proxy is already running:
- If owned by UI: show as “Managed”.
- If not owned: show as “External” and offer “Attach” (read-only) vs “Stop anyway” behind a confirmation.
### Health
- Health check: `GET http://127.0.0.1:<port>/healthz`.
- Show last successful check time and last error message.
### Config
- Config path chooser + remember last choice.
- “Open config” action (external editor).
- Validate YAML on save (or show parse error).
### Logs / request diagnostics
- Open logs folder.
- Tail last N lines of `proxypilot-engine.out.log` and `proxypilot-engine.err.log`.
- “Copy diagnostics” button that includes:
- app version, proxy version
- config path, port, pid
- last error + last N log lines
- pointers to recent request logs
### Quick-copy snippets
- `OPENAI_BASE_URL=http://127.0.0.1:<port>/v1`
- `OPENAI_API_KEY=<your local key>`
- Minimal curl sanity checks.
## Nice-to-haves (v1.1+)
- “Recent requests” viewer (parse `logs/v1-*.log`):
- list request IDs, status, provider, model, auth label, latency
- click to open the log file at the relevant section
- Surface auth status:
- list loaded auth files + provider + account email (when available)
- show “cooling down” vs “active”
- optionally display primary/backup preference (e.g. `antigravity`)
- “Safe config wizard”:
- toggle localhost-only management
- add/generate local dev API key
- Update mechanism (self-update) and server binary update strategy.
## Security / safety rules
- Default to localhost-only assumptions.
- Don’t display or log API keys unless user explicitly reveals/copies them.
- Autostart toggles must show exactly what command will run.
- Avoid writing outside user profile by default.
## Cross-platform notes
Split OS-specific operations behind small interfaces in `internal/desktopctl`:
- Process operations (spawn, signal/terminate, pid discovery)
- Autostart
- “Open folder” / “Open URL”
Windows and macOS implementations can live in separate files via Go build tags:
- `*_windows.go`
- `*_darwin.go`
## Open questions
- Should the desktop app bundle the engine binary, or manage an existing install?
- Do we want a dedicated “desktop control” HTTP endpoint in the proxy (vs local process control only)?
+333
View File
@@ -0,0 +1,333 @@
# Long-Context Features in ProxyPilot
ProxyPilot implements two research-backed features for handling long-running AI agent sessions:
1. **Context Compression** - Based on Factory.ai research
2. **Agentic Harness** - Based on Anthropic's "Building Effective Agents" blog
Both are enabled by default and can be toggled via environment variables.
---
## Sources
| Feature | Source | Link |
|---------|--------|------|
| Context Compression | Factory.ai Research | [Context Compression for Long-Running Agents](https://www.factory.ai/news/context-compression) |
| Agentic Harness | Anthropic Blog | [Building Effective Agents](https://www.anthropic.com/research/building-effective-agents) |
---
## 1. Context Compression (Factory.ai)
### What Factory.ai Found
Factory.ai researched how to handle long coding sessions where conversation context exceeds model limits. Their key findings:
- **Naive truncation fails** - Cutting old messages loses critical context
- **Structured summarization works** - Preserving key information in a specific format maintains continuity
- **Anchored approach is best** - Merging new info into existing summaries beats creating fresh ones
### How ProxyPilot Implements It
When conversation exceeds 75% of the model's context window:
1. **Trigger**: Token count check detects threshold exceeded
2. **Summarize**: LLM generates structured summary of older messages
3. **Inject**: Summary replaces compressed messages in subsequent requests
### Structured Summary Format
```
## Session Intent
[High-level goal of the conversation]
## File Modifications
- path/to/file1.go: Added function X, modified Y
- path/to/file2.ts: Refactored component Z
## Decisions Made
- Chose approach A over B because...
- Confirmed that X should use pattern Y
## Next Steps
1. Implement feature X
2. Add tests for Y
## Technical Details
- Key code snippets or patterns
- Important variable names or function signatures
```
### Configuration
| Environment Variable | Default | Description |
|---------------------|---------|-------------|
| `CLIPROXY_TOKEN_AWARE_ENABLED` | `true` | Enable token-based compression |
| `CLIPROXY_LLM_SUMMARY_ENABLED` | `true` | Use LLM for summaries (vs regex fallback) |
| `CLIPROXY_COMPRESSION_THRESHOLD` | `0.75` | Trigger at this % of context |
### Disable
```bash
export CLIPROXY_LLM_SUMMARY_ENABLED=false
```
---
## 2. Agentic Harness (Anthropic)
### What Anthropic Found
Anthropic's research on long-running agents identified key failure modes:
- Agents try to do everything at once and fail
- Agents don't track state between turns
- Agents skip verification ("the code looks correct")
- Agents lose context of what was already done
Their solution: **Structured harness with explicit state files and disciplined workflow.**
### How ProxyPilot Implements It
ProxyPilot injects prompts based on conversation state:
#### INITIALIZER Mode (First Request)
Injected when agentic client sends first request:
```
You are the Initializer Agent. Your ONLY goal is to set up the project environment.
1. Create 'feature_list.json' - List ALL features with:
- id, category, description, steps, passes: false, priority
2. Create 'claude-progress.txt' - Session log with:
- [Initializer] Environment setup started
3. Create 'init.sh' - Dev server startup script
DO NOT implement features. Only scaffold the harness files.
```
#### CODING Mode (After Harness Files Exist)
Injected when `feature_list.json` or `claude-progress.txt` detected:
```
You are a Coding Agent. Follow this workflow:
STEP 1: GET BEARINGS (MANDATORY)
- pwd, git log, cat claude-progress.txt, cat feature_list.json
STEP 2: SANITY CHECK
- Verify existing features work
- Fix regressions before new work
STEP 3: PICK ONE TASK
- Find highest-priority feature where passes: false
- Announce: "I will implement: [description]"
STEP 4: IMPLEMENT & VERIFY
- Write code for ONE feature only
- Run tests to verify it works
- Do NOT mark complete without actual testing
STEP 5: UPDATE STATE
- Set passes: true in feature_list.json
- Append to claude-progress.txt
STEP 6: COMMIT
- Git commit with feature ID in message
```
#### PASSIVE Mode
For non-agentic clients (browsers, curl), no prompts injected.
### Client Detection
Harness activates for these User-Agents:
- `claude-cli`
- `codex`
- `droid`
Or when header `X-ProxyPilot-Harness: true` is set.
### Configuration
| Environment Variable | Default | Description |
|---------------------|---------|-------------|
| `CLIPROXY_HARNESS_ENABLED` | `true` | Enable agentic harness |
| `CLIPROXY_MEMORY_DIR` | `~/.proxypilot/memory` | Session storage location |
### Disable
```bash
export CLIPROXY_HARNESS_ENABLED=false
```
---
## Implementation Details
### Files Modified
| File | Change |
|------|--------|
| `internal/memory/summarizer_executor.go` | Added `CoreManagerExecutor` interface and `ManagerAuthAdapter` |
| `internal/api/middleware/codex_prompt_budget.go` | Added `InitSummarizerWithAuthManager()` |
| `sdk/cliproxy/service.go` | Added `coreManagerWrapper`, wired up compression |
| `internal/api/server.go` | Registered `AgenticHarnessMiddleware()` |
### Files Created
| File | Purpose |
|------|---------|
| `internal/memory/summarizer_executor_test.go` | 12 unit tests for adapter |
| `internal/api/features_integration_test.go` | 3 integration tests |
### Architecture
```
Request Flow (Context Compression):
Client Request
│
▼
┌─────────────────────┐
│ PromptBudget │
│ Middleware │
└─────────────────────┘
│
▼
┌─────────────────────┐
│ Token Count Check │──── < 75% ────▶ Pass through
└─────────────────────┘
│
│ > 75%
▼
┌─────────────────────┐
│ PipelineSummarizer │
│ Executor │
└─────────────────────┘
│
▼
┌─────────────────────┐
│ ManagerAuthAdapter │
└─────────────────────┘
│
▼
┌─────────────────────┐
│ coreManagerWrapper │
└─────────────────────┘
│
▼
┌─────────────────────┐
│ Auth Manager │
│ (claude/codex/ │
│ gemini) │
└─────────────────────┘
```
```
Request Flow (Agentic Harness):
Client Request
│
▼
┌─────────────────────┐
│ Check User-Agent │──── Not agentic ────▶ Pass through
└─────────────────────┘
│
│ Agentic client
▼
┌─────────────────────┐
│ Detect Harness │
│ State │
└─────────────────────┘
│
├── No harness files + short convo ──▶ INITIALIZER prompt
│
├── Harness files exist ──────────────▶ CODING prompt
│
└── Long convo, no files ─────────────▶ PASSIVE (no injection)
```
---
## What Was NOT Implemented
During implementation, we removed features that were added by a previous session but **not from the original sources**:
| Removed | Reason |
|---------|--------|
| Puppeteer MCP detection | Not in Factory.ai or Anthropic sources |
| Browser automation reminders | Made up, not from research |
The current implementation is pure to the original Factory.ai and Anthropic research.
---
## Testing
```bash
# Run all tests
go test ./...
# Run specific feature tests
go test ./internal/memory/... -v # Compression adapter
go test ./internal/api/middleware/... -v -run Harness # Harness middleware
go test ./internal/api/... -v -run Integration # Integration tests
```
### Test Coverage
| Test File | Tests | Coverage |
|-----------|-------|----------|
| `summarizer_executor_test.go` | 12 | Adapter, pipeline, edge cases |
| `agentic_harness_test.go` | 10 | All modes, injection, session storage |
| `features_integration_test.go` | 3 | Combined feature validation |
---
## Quick Reference
### Enable/Disable Features
```bash
# Disable context compression
export CLIPROXY_LLM_SUMMARY_ENABLED=false
# Disable agentic harness
export CLIPROXY_HARNESS_ENABLED=false
# Disable both
export CLIPROXY_TOKEN_AWARE_ENABLED=false
export CLIPROXY_HARNESS_ENABLED=false
```
### Key Integration Points
```go
// Context Compression - sdk/cliproxy/service.go:516
middleware.InitSummarizerWithAuthManager(wrapper, providers)
// Agentic Harness - internal/api/server.go:224
engine.Use(middleware.AgenticHarnessMiddleware())
```
---
## Changelog
### 2025-12-27
- **Implemented**: Factory.ai-style context compression with LLM summarization
- **Implemented**: Anthropic-style agentic harness (INITIALIZER/CODING modes)
- **Added**: `ManagerAuthAdapter` to bridge auth manager types
- **Added**: `coreManagerWrapper` to adapt concrete types to interfaces
- **Added**: `InitSummarizerWithAuthManager()` for initialization
- **Added**: Session-based harness file storage
- **Added**: Comprehensive test coverage (25+ tests)
- **Removed**: Puppeteer MCP detection (not from original sources)
+8
View File
@@ -176,3 +176,11 @@ Management endpoints (local only):
- `GET /v0/management/memory/export-all`
- `POST /v0/management/memory/import?session=...`
- `POST /v0/management/memory/delete-all`
## LLM-Based Context Compression
For advanced context compression using LLM-generated structured summaries and token-aware proactive trimming, see [Context Compression](context-compression.md).
## Agentic Harness
For structured incremental development workflows (INITIALIZER/CODING modes, feature tracking, progress logging), see [Agentic Harness](agentic-harness.md).
-171
View File
@@ -1,171 +0,0 @@
# Windows UI for ProxyPilot — Spec (Wails + Tauri)
This is the Windows-specific portion of the desktop UI plan. Shared (cross-platform) decisions and architecture live in `docs/desktop-ui-spec.md`.
## Goals
- Provide a Windows-friendly way to run and manage `ProxyPilot` without PowerShell.
- Surface request/log diagnostics to reduce “prompt too long” + debugging pain.
- Support two desktop shells over time:
- Primary: Wails (Go backend + web UI)
- Secondary/experimental: Tauri (Rust + web UI)
- Share logic via a Go control layer so features don’t fork.
## Non-goals (v1)
- Replacing the existing Management API UI shipped with the server.
- Full cross-platform parity (macOS/Linux can come later).
- Full OAuth flows inside the desktop UI (v1 can “open browser” and show status).
- Becoming an agent/IDE: this is a small controller + diagnostics utility.
## Product decisions (to avoid bikeshedding)
- v1 ships as Wails.
- Tauri is optional later; it should reuse the same control layer via a helper CLI.
- The UI controls an existing `proxypilot-engine.exe` (built by the UI or already present).
- The UI should not require elevated privileges and should default to localhost-only behavior.
## Personas / Use-cases
- “I just want it running”: start/stop, see port, see health.
- “I need to debug”: view logs, open request logs, copy diagnostics.
- “I’m configuring tools”: show local base URL, API key hints, and quick-copy snippets.
- “I want it to launch on login”: enable/disable autostart (per-user).
## UX requirements
- Tray-first: app sits in the system tray with minimal friction.
- Single click status: Running/Stopped + port + quick actions.
- No admin required for normal operation.
- Clear failure reasons: missing config, port in use, binary missing, auth dir missing.
## Functional requirements (v1)
### Process control
- Build (optional): build `cmd/server` into `bin/proxypilot-engine.exe`.
- Start:
- Uses a selectable config path (default: repo `config.yaml` if present).
- Writes logs to `logs/proxypilot-engine.out.log` and `logs/proxypilot-engine.err.log`.
- Stop:
- Prefer graceful stop; fallback to kill process if needed.
- Restart.
- Detect running instance:
- PID-based if started by UI, plus best-effort discovery by listening port.
- If an instance is running but not owned by the UI, show “External” and offer read-only “Attach” (don’t blindly kill it).
### Status & health
- Show:
- port (from config or detection)
- reachable status via `GET http://127.0.0.1:<port>/v1/models` (or a future `/healthz`)
- last start time and PID (when known)
- Buttons:
- Open Management UI (browser)
- Open logs folder
- Copy base URL (`http://127.0.0.1:<port>` and optionally `/v1`)
### Config
- Config path selector (browse + remember last choice).
- Validate YAML on save (or warn on parse failure).
- Minimal editor:
- open in external editor, and/or inline text editor with Save.
- Safe defaults:
- warn if management routes are remotely accessible.
### Autostart (Windows)
- Enable/disable per-user startup on Windows.
- Backend supports:
- registry `HKCU\Software\Microsoft\Windows\CurrentVersion\Run`
- scheduled task fallback (optional; clearly labeled)
### Quick-copy snippets (v1)
- Show ready-to-paste snippets:
- `OPENAI_BASE_URL=http://127.0.0.1:<port>/v1`
- `OPENAI_API_KEY=<your local key>`
- curl sanity check for `GET /v1/models`
### Diagnostics / support bundle (v1.1)
- One-click “Copy diagnostics”:
- version, config path, port, PID, last 200 lines of logs
- recent request logs list (or last N request IDs)
## Shared Go control layer (Windows notes)
Wails and Tauri should both call the same operations (process management, config, logs, autostart, health checks). See `docs/desktop-ui-spec.md` for the proposed package/API shape.
Windows-specific notes:
- Persist UI state in `%LOCALAPPDATA%/ProxyPilot/ui-state.json`.
- Prefer discover-by-port over discover-by-process-name.
- When opening folders/URLs, use native Windows shell APIs (no admin).
## Wails app (primary) — MVP
### Packaging
- Ship `ProxyPilot.exe` with embedded web UI assets.
- Bundle or download `proxypilot-engine.exe` (open decision).
### Screens
- Tray menu:
- Start / Stop / Restart
- Open UI
- Open logs
- Quit
- Main window:
- Status card (Running/Stopped, port, last error)
- Buttons: Start/Stop/Restart, Open Management UI, Open Logs
- Config path selector + Open config
- Autostart toggle
- Copy snippets section (base URL + key + curl check)
### First-run UX
- If no config path is set:
- Detect `config.yaml` in common locations (repo root, `%LOCALAPPDATA%/ProxyPilot/`, user home).
- Offer “Use detected config” or “Browse…”.
- If no API key is configured:
- Offer “Generate local key” (writes to config) or “Open config” guidance.
## Tauri app (secondary) — MVP
### Purpose
- Evaluate smaller footprint distribution vs Wails.
- Prototype a more native-feeling tray + notifications.
### Integration options
- Option A (recommended): Tauri calls a helper CLI (`cliproxyctl.exe`).
- Option B: Tauri starts a small local HTTP control server.
## Security considerations
- Default to localhost-only assumptions.
- Don’t display or log API keys unless explicitly requested.
- When enabling autostart, show what command will run.
- Avoid writing outside the user profile unless configured.
## Open decisions
- Where config lives by default (repo-local vs user config dir).
- Whether UI bundles the server binary or manages an existing install.
- Whether to add a stable `/healthz` endpoint for fast health checks.
- Whether “Generate local key” should mutate `config.yaml` or use a UI-only key store.
- Whether the UI should surface auth selection state (primary/backup) for providers like `antigravity`.
## Acceptance criteria (v1)
- Installing and launching the app shows a tray icon and a status window.
- Start/Stop/Restart works reliably across app restarts (PID state persisted).
- Health check distinguishes:
- process running but endpoint not reachable
- endpoint reachable with `GET /v1/models` returning 200
- Open logs opens the correct folder.
- Copy base URL copies `http://127.0.0.1:<port>` (and optionally `/v1`).
+26 -19
View File
@@ -1,6 +1,6 @@
#define AppName "ProxyPilot"
#ifndef AppVersion
#define AppVersion "0.0.0-dev"
#define AppVersion "0.1.0"
#endif
#ifndef RepoRoot
#define RepoRoot ".."
@@ -14,41 +14,48 @@ AppId={{3D9053A0-3F6A-47D7-9D91-8BB1D1CC2A4E}
AppName={#AppName}
AppVersion={#AppVersion}
AppPublisher=ProxyPilot
SetupIconFile={#RepoRoot}\\bin\\ProxyPilot.ico
DefaultDirName={localappdata}\\{#AppName}
AppPublisherURL=https://github.com/Finesssee/ProxyPilot
DefaultDirName={localappdata}\{#AppName}
DefaultGroupName={#AppName}
DisableProgramGroupPage=yes
PrivilegesRequired=lowest
Compression=lzma2
Compression=lzma2/ultra64
SolidCompression=yes
WizardStyle=modern
ArchitecturesInstallIn64BitMode=x64
ArchitecturesInstallIn64BitMode=x64compatible
OutputDir={#OutDir}
OutputBaseFilename=ProxyPilot-Setup
UninstallDisplayIcon={app}\\ProxyPilot.ico
OutputBaseFilename=ProxyPilot-{#AppVersion}-Setup
SetupIconFile={#RepoRoot}\static\icon.ico
UninstallDisplayIcon={app}\icon.ico
[Languages]
Name: "english"; MessagesFile: "compiler:Default.isl"
[Tasks]
Name: "desktopicon"; Description: "Create a &desktop icon"; GroupDescription: "Additional icons:"
Name: "startupicon"; Description: "Start ProxyPilot when Windows starts"; GroupDescription: "Startup:"
[Files]
Source: "{#RepoRoot}\\bin\\ProxyPilot.exe"; DestDir: "{app}"; Flags: ignoreversion
Source: "{#RepoRoot}\\bin\\ProxyPilotUI.exe"; DestDir: "{app}"; Flags: ignoreversion
Source: "{#RepoRoot}\\bin\\proxypilot-engine.exe"; DestDir: "{app}"; Flags: ignoreversion
Source: "{#RepoRoot}\\bin\\ProxyPilot.ico"; DestDir: "{app}"; Flags: ignoreversion
Source: "{#RepoRoot}\\config.example.yaml"; DestDir: "{app}"; Flags: ignoreversion
; Main app (tray + dashboard UI)
Source: "{#OutDir}\ProxyPilot.exe"; DestDir: "{app}"; Flags: ignoreversion
; Config
Source: "{#RepoRoot}\config.example.yaml"; DestDir: "{app}"; Flags: ignoreversion
; Icons
Source: "{#RepoRoot}\static\icon.ico"; DestDir: "{app}"; Flags: ignoreversion
Source: "{#RepoRoot}\static\icon.png"; DestDir: "{app}"; Flags: ignoreversion
[Icons]
Name: "{autoprograms}\\{#AppName}"; Filename: "{app}\\ProxyPilot.exe"; WorkingDir: "{app}"; IconFilename: "{app}\\ProxyPilot.ico"
Name: "{autodesktop}\\{#AppName}"; Filename: "{app}\\ProxyPilot.exe"; Tasks: desktopicon; WorkingDir: "{app}"; IconFilename: "{app}\\ProxyPilot.ico"
Name: "{autoprograms}\{#AppName}"; Filename: "{app}\ProxyPilot.exe"; WorkingDir: "{app}"; IconFilename: "{app}\icon.ico"
Name: "{autodesktop}\{#AppName}"; Filename: "{app}\ProxyPilot.exe"; Tasks: desktopicon; WorkingDir: "{app}"; IconFilename: "{app}\icon.ico"
[Registry]
Root: HKCU; Subkey: "Software\Microsoft\Windows\CurrentVersion\Run"; ValueType: string; ValueName: "ProxyPilot"; ValueData: """{app}\ProxyPilot.exe"""; Flags: uninsdeletevalue; Tasks: startupicon
[Run]
Filename: "{app}\\ProxyPilot.exe"; Description: "Launch {#AppName}"; Flags: nowait postinstall skipifsilent
Filename: "{app}\ProxyPilot.exe"; Description: "Launch {#AppName}"; Flags: nowait postinstall skipifsilent
[UninstallRun]
Filename: "cmd.exe"; Parameters: "/c reg delete HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Run /v ProxyPilot /f >nul 2>nul"; Flags: runhidden
Filename: "taskkill"; Parameters: "/F /IM ProxyPilot.exe"; Flags: runhidden; RunOnceId: "KillTray"
[UninstallDelete]
Type: filesandordirs; Name: "{app}"
@@ -61,11 +68,11 @@ var
begin
if CurStep = ssPostInstall then
begin
ConfigYaml := ExpandConstant('{app}\\config.yaml');
ExampleYaml := ExpandConstant('{app}\\config.example.yaml');
ConfigYaml := ExpandConstant('{app}\config.yaml');
ExampleYaml := ExpandConstant('{app}\config.example.yaml');
if (not FileExists(ConfigYaml)) and FileExists(ExampleYaml) then
begin
FileCopy(ExampleYaml, ConfigYaml, False);
CopyFile(ExampleYaml, ConfigYaml, False);
end;
end;
end;
+123
View File
@@ -0,0 +1,123 @@
package api
import (
"context"
"net/http/httptest"
"os"
"strings"
"testing"
"github.com/gin-gonic/gin"
"github.com/router-for-me/CLIProxyAPI/v6/internal/api/middleware"
"github.com/router-for-me/CLIProxyAPI/v6/internal/memory"
)
// mockCoreManager implements memory.CoreManagerExecutor for testing
type mockCoreManager struct {
response interface{}
err error
}
func (m *mockCoreManager) Execute(ctx context.Context, providers []string, req interface{}, opts interface{}) (interface{}, error) {
return m.response, m.err
}
func TestContextCompression_AdapterIntegration(t *testing.T) {
// Test that the adapter correctly bridges manager to executor
mockResp := struct {
Payload []byte `json:"payload"`
}{
Payload: []byte(`{"choices":[{"message":{"content":"Test summary"}}]}`),
}
manager := &mockCoreManager{response: mockResp}
adapter := memory.NewManagerAuthAdapter(manager)
if adapter == nil {
t.Fatal("Expected adapter to be created")
}
// Test execute
resp, err := adapter.Execute(context.Background(), []string{"claude"}, memory.ExecutorRequest{
Model: "claude-3-opus",
Payload: []byte(`{"test": true}`),
}, memory.ExecutorOptions{})
if err != nil {
t.Fatalf("Execute failed: %v", err)
}
if len(resp) == 0 {
t.Error("Expected non-empty response")
}
}
func TestAgenticHarness_MiddlewareIntegration(t *testing.T) {
gin.SetMode(gin.TestMode)
// Create temp directory for harness files
tmpDir, err := os.MkdirTemp("", "harness-integration")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tmpDir)
handler := middleware.AgenticHarnessMiddlewareWithRootDir(tmpDir)
// Test 1: Fresh conversation should get INITIALIZER
t.Run("fresh_conversation_initializer", func(t *testing.T) {
w := httptest.NewRecorder()
c, _ := gin.CreateTestContext(w)
body := `{"model":"gpt-4","messages":[{"role":"user","content":"Build a todo app"}]}`
c.Request = httptest.NewRequest("POST", "/v1/chat/completions", strings.NewReader(body))
c.Request.Header.Set("User-Agent", "claude-cli/1.0")
c.Request.Header.Set("Content-Type", "application/json")
handler(c)
mode := c.Writer.Header().Get("X-ProxyPilot-Harness-Mode")
if mode != "INITIALIZER" {
t.Errorf("Expected INITIALIZER mode, got: %s", mode)
}
})
// Test 2: After creating harness files, should get CODING
t.Run("with_harness_files_coding", func(t *testing.T) {
// Create harness file
os.WriteFile(tmpDir+"/feature_list.json", []byte(`[{"id":"test","passes":false}]`), 0644)
w := httptest.NewRecorder()
c, _ := gin.CreateTestContext(w)
body := `{"model":"gpt-4","messages":[{"role":"user","content":"Continue"}]}`
c.Request = httptest.NewRequest("POST", "/v1/chat/completions", strings.NewReader(body))
c.Request.Header.Set("User-Agent", "claude-cli/1.0")
c.Request.Header.Set("Content-Type", "application/json")
handler(c)
mode := c.Writer.Header().Get("X-ProxyPilot-Harness-Mode")
if mode != "CODING" {
t.Errorf("Expected CODING mode, got: %s", mode)
}
})
}
func TestCombinedFeatures_NoConflict(t *testing.T) {
// Verify both features can be enabled simultaneously without conflict
gin.SetMode(gin.TestMode)
// Set up harness
tmpDir, _ := os.MkdirTemp("", "combined-test")
defer os.RemoveAll(tmpDir)
// Create engine with both middlewares
engine := gin.New()
engine.Use(middleware.AgenticHarnessMiddleware())
// Verify engine was created without panic
if engine == nil {
t.Fatal("Engine should be created")
}
}
@@ -597,11 +597,7 @@ func (h *Handler) PutCodexKeys(c *gin.Context) {
filtered := make([]config.CodexKey, 0, len(arr))
for i := range arr {
entry := arr[i]
entry.APIKey = strings.TrimSpace(entry.APIKey)
entry.BaseURL = strings.TrimSpace(entry.BaseURL)
entry.ProxyURL = strings.TrimSpace(entry.ProxyURL)
entry.Headers = config.NormalizeHeaders(entry.Headers)
entry.ExcludedModels = config.NormalizeExcludedModels(entry.ExcludedModels)
normalizeCodexKey(&entry)
if entry.BaseURL == "" {
continue
}
@@ -613,12 +609,13 @@ func (h *Handler) PutCodexKeys(c *gin.Context) {
}
func (h *Handler) PatchCodexKey(c *gin.Context) {
type codexKeyPatch struct {
APIKey *string `json:"api-key"`
Prefix *string `json:"prefix"`
BaseURL *string `json:"base-url"`
ProxyURL *string `json:"proxy-url"`
Headers *map[string]string `json:"headers"`
ExcludedModels *[]string `json:"excluded-models"`
APIKey *string `json:"api-key"`
Prefix *string `json:"prefix"`
BaseURL *string `json:"base-url"`
ProxyURL *string `json:"proxy-url"`
Models *[]config.CodexModel `json:"models"`
Headers *map[string]string `json:"headers"`
ExcludedModels *[]string `json:"excluded-models"`
}
var body struct {
Index *int `json:"index"`
@@ -667,12 +664,16 @@ func (h *Handler) PatchCodexKey(c *gin.Context) {
if body.Value.ProxyURL != nil {
entry.ProxyURL = strings.TrimSpace(*body.Value.ProxyURL)
}
if body.Value.Models != nil {
entry.Models = append([]config.CodexModel(nil), (*body.Value.Models)...)
}
if body.Value.Headers != nil {
entry.Headers = config.NormalizeHeaders(*body.Value.Headers)
}
if body.Value.ExcludedModels != nil {
entry.ExcludedModels = config.NormalizeExcludedModels(*body.Value.ExcludedModels)
}
normalizeCodexKey(&entry)
h.cfg.CodexKey[targetIndex] = entry
h.cfg.SanitizeCodexKeys()
h.persist(c)
@@ -761,3 +762,29 @@ func normalizeClaudeKey(entry *config.ClaudeKey) {
}
entry.Models = normalized
}
func normalizeCodexKey(entry *config.CodexKey) {
if entry == nil {
return
}
entry.APIKey = strings.TrimSpace(entry.APIKey)
entry.Prefix = strings.TrimSpace(entry.Prefix)
entry.BaseURL = strings.TrimSpace(entry.BaseURL)
entry.ProxyURL = strings.TrimSpace(entry.ProxyURL)
entry.Headers = config.NormalizeHeaders(entry.Headers)
entry.ExcludedModels = config.NormalizeExcludedModels(entry.ExcludedModels)
if len(entry.Models) == 0 {
return
}
normalized := make([]config.CodexModel, 0, len(entry.Models))
for i := range entry.Models {
model := entry.Models[i]
model.Name = strings.TrimSpace(model.Name)
model.Alias = strings.TrimSpace(model.Alias)
if model.Name == "" && model.Alias == "" {
continue
}
normalized = append(normalized, model)
}
entry.Models = normalized
}
@@ -333,6 +333,15 @@ func (h *Handler) Middleware() gin.HandlerFunc {
return
}
}
// For localhost, also accept configured API keys (same keys that work for main API)
if cfg != nil {
for _, apiKey := range cfg.APIKeys {
if apiKey != "" && subtle.ConstantTimeCompare([]byte(provided), []byte(apiKey)) == 1 {
c.Next()
return
}
}
}
}
if envSecret != "" && subtle.ConstantTimeCompare([]byte(provided), []byte(envSecret)) == 1 {
+171
View File
@@ -0,0 +1,171 @@
package management
import (
"net/http"
"strings"
"github.com/gin-gonic/gin"
"github.com/router-for-me/CLIProxyAPI/v6/internal/memory"
)
// GetHarnessFiles returns the list of harness files for a session.
// GET /v0/management/harness/files?session=...
func (h *Handler) GetHarnessFiles(c *gin.Context) {
base := memoryBaseDir()
if base == "" {
c.JSON(http.StatusOK, gin.H{"files": []string{}})
return
}
session := strings.TrimSpace(c.Query("session"))
if session == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "missing session"})
return
}
store := memory.NewFileStore(base)
files, err := store.ListHarnessFiles(session)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
if files == nil {
files = []string{}
}
c.JSON(http.StatusOK, gin.H{"files": files})
}
// GetHarnessFile returns the content of a specific harness file.
// GET /v0/management/harness/file?session=...&filename=...
func (h *Handler) GetHarnessFile(c *gin.Context) {
base := memoryBaseDir()
if base == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "memory not configured"})
return
}
session := strings.TrimSpace(c.Query("session"))
if session == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "missing session"})
return
}
filename := strings.TrimSpace(c.Query("filename"))
if filename == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "missing filename"})
return
}
store := memory.NewFileStore(base)
content := store.ReadHarnessFile(session, filename, 100000)
c.JSON(http.StatusOK, gin.H{"filename": filename, "content": content})
}
type putHarnessFileRequest struct {
Session string `json:"session"`
Filename string `json:"filename"`
Content string `json:"content"`
}
// PutHarnessFile creates or updates a harness file.
// PUT /v0/management/harness/file
// Body: {"session": "...", "filename": "...", "content": "..."}
func (h *Handler) PutHarnessFile(c *gin.Context) {
base := memoryBaseDir()
if base == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "memory not configured"})
return
}
var req putHarnessFileRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid body"})
return
}
if strings.TrimSpace(req.Session) == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "missing session"})
return
}
if strings.TrimSpace(req.Filename) == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "missing filename"})
return
}
store := memory.NewFileStore(base)
if err := store.WriteHarnessFile(req.Session, req.Filename, req.Content, 100000); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"status": "ok"})
}
// ExportHarness returns all harness files for a session as JSON.
// GET /v0/management/harness/export?session=...
func (h *Handler) ExportHarness(c *gin.Context) {
base := memoryBaseDir()
if base == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "memory not configured"})
return
}
session := strings.TrimSpace(c.Query("session"))
if session == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "missing session"})
return
}
store := memory.NewFileStore(base)
files, err := store.ListHarnessFiles(session)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
result := make(map[string]string, len(files))
for _, filename := range files {
content := store.ReadHarnessFile(session, filename, 100000)
result[filename] = content
}
c.JSON(http.StatusOK, gin.H{"session": session, "files": result})
}
type importHarnessRequest struct {
Session string `json:"session"`
Files map[string]string `json:"files"`
}
// ImportHarness imports harness files from JSON.
// POST /v0/management/harness/import
// Body: {"session": "...", "files": {"feature_list.json": "...", "claude-progress.txt": "..."}}
func (h *Handler) ImportHarness(c *gin.Context) {
base := memoryBaseDir()
if base == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "memory not configured"})
return
}
var req importHarnessRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid body"})
return
}
if strings.TrimSpace(req.Session) == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "missing session"})
return
}
if len(req.Files) == 0 {
c.JSON(http.StatusBadRequest, gin.H{"error": "missing files"})
return
}
store := memory.NewFileStore(base)
var errors []string
imported := 0
for filename, content := range req.Files {
if strings.TrimSpace(filename) == "" {
continue
}
if err := store.WriteHarnessFile(req.Session, filename, content, 100000); err != nil {
errors = append(errors, filename+": "+err.Error())
continue
}
imported++
}
if len(errors) > 0 {
c.JSON(http.StatusInternalServerError, gin.H{
"status": "partial",
"imported": imported,
"errors": errors,
})
return
}
c.JSON(http.StatusOK, gin.H{"status": "ok", "imported": imported})
}
@@ -4,6 +4,7 @@ import (
"net/http"
"github.com/gin-gonic/gin"
"github.com/router-for-me/CLIProxyAPI/v6/internal/integrations"
)
// GetIntegrationsStatus returns the list of detected tools and their configuration status.
@@ -35,3 +36,45 @@ func (h *Handler) PostIntegrationConfigure(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"status": "configured", "id": id})
}
// GetCLIAgents returns the list of detected CLI agents and IDEs.
func (h *Handler) GetCLIAgents(c *gin.Context) {
proxyURL := h.cfg.ProxyURL
if proxyURL == "" {
proxyURL = "http://localhost:8080"
}
agents := integrations.DetectCLIAgents(proxyURL)
c.JSON(http.StatusOK, gin.H{"agents": agents})
}
// PostCLIAgentConfigure configures a CLI agent to use the proxy.
func (h *Handler) PostCLIAgentConfigure(c *gin.Context) {
id := c.Param("id")
if id == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "missing agent id"})
return
}
proxyURL := h.cfg.ProxyURL
if proxyURL == "" {
proxyURL = "http://localhost:8080"
}
if err := integrations.ConfigureCLIAgent(id, proxyURL); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"status": "configured", "id": id})
}
// PostCLIAgentUnconfigure removes proxy configuration from a CLI agent.
func (h *Handler) PostCLIAgentUnconfigure(c *gin.Context) {
id := c.Param("id")
if id == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "missing agent id"})
return
}
if err := integrations.UnconfigureCLIAgent(id); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"status": "unconfigured", "id": id})
}
@@ -0,0 +1,95 @@
package management
import (
"net/http"
"github.com/gin-gonic/gin"
"github.com/router-for-me/CLIProxyAPI/v6/internal/config"
)
// ModelMappingEntry represents a global model mapping for the API
type ModelMappingEntry struct {
From string `json:"from"`
To string `json:"to"`
Provider string `json:"provider,omitempty"`
Enabled bool `json:"enabled"`
}
// GetModelMappings returns the current global model mappings
func (h *Handler) GetModelMappings(c *gin.Context) {
if h.cfg == nil {
c.JSON(http.StatusOK, gin.H{"mappings": []any{}})
return
}
mappings := make([]ModelMappingEntry, 0, len(h.cfg.GlobalModelMappings))
for _, m := range h.cfg.GlobalModelMappings {
mappings = append(mappings, ModelMappingEntry{
From: m.From,
To: m.To,
Provider: m.Provider,
Enabled: m.IsEnabled(),
})
}
c.JSON(http.StatusOK, gin.H{"mappings": mappings})
}
// SetModelMappings replaces all global model mappings
func (h *Handler) SetModelMappings(c *gin.Context) {
var req struct {
Mappings []ModelMappingEntry `json:"mappings"`
}
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid body"})
return
}
if h.cfg == nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "config not loaded"})
return
}
// Convert to config type
newMappings := make([]config.GlobalModelMapping, 0, len(req.Mappings))
for _, m := range req.Mappings {
enabled := m.Enabled
newMappings = append(newMappings, config.GlobalModelMapping{
From: m.From,
To: m.To,
Provider: m.Provider,
Enabled: &enabled,
})
}
h.cfg.GlobalModelMappings = newMappings
// Try to save config if configPath is available
// Note: This requires the config path to be stored somewhere accessible
c.JSON(http.StatusOK, gin.H{"status": "ok", "count": len(newMappings)})
}
// TestModelMapping tests a model name against global mappings
func (h *Handler) TestModelMapping(c *gin.Context) {
model := c.Query("model")
provider := c.Query("provider")
if model == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "missing model parameter"})
return
}
if h.cfg == nil {
c.JSON(http.StatusOK, gin.H{"model": model, "mapped_to": model, "matched": false})
return
}
mappedTo := h.cfg.LookupGlobalModelMapping(model, provider)
if mappedTo == "" {
c.JSON(http.StatusOK, gin.H{"model": model, "mapped_to": model, "matched": false})
return
}
c.JSON(http.StatusOK, gin.H{"model": model, "mapped_to": mappedTo, "matched": true})
}
@@ -0,0 +1,16 @@
package management
import (
"net/http"
"github.com/gin-gonic/gin"
"github.com/router-for-me/CLIProxyAPI/v6/internal/api/middleware"
)
// GetRequests returns the list of recent requests from the in-memory ring buffer.
func (h *Handler) GetRequests(c *gin.Context) {
requests := middleware.GetRequestMonitor()
c.JSON(http.StatusOK, gin.H{
"requests": requests,
})
}
+145
View File
@@ -0,0 +1,145 @@
package management
import (
"net/http"
"sync"
"github.com/gin-gonic/gin"
"github.com/router-for-me/CLIProxyAPI/v6/internal/updates"
)
// updateState tracks the current update operation state.
type updateState struct {
mu sync.RWMutex
downloading bool
progress updates.DownloadProgress
downloadResult *updates.DownloadResult
lastError string
}
var currentUpdate = &updateState{}
func (h *Handler) GetUpdateInfo(c *gin.Context) {
info, err := updates.CheckForUpdates()
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, info)
}
// GetUpdateStatus returns the current update download status.
func (h *Handler) GetUpdateStatus(c *gin.Context) {
currentUpdate.mu.RLock()
defer currentUpdate.mu.RUnlock()
c.JSON(http.StatusOK, gin.H{
"downloading": currentUpdate.downloading,
"progress": currentUpdate.progress,
"ready": currentUpdate.downloadResult != nil,
"error": currentUpdate.lastError,
})
}
// DownloadUpdate starts downloading an update.
func (h *Handler) DownloadUpdate(c *gin.Context) {
var req struct {
Version string `json:"version"`
}
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "version is required"})
return
}
currentUpdate.mu.Lock()
if currentUpdate.downloading {
currentUpdate.mu.Unlock()
c.JSON(http.StatusConflict, gin.H{"error": "download already in progress"})
return
}
currentUpdate.downloading = true
currentUpdate.progress = updates.DownloadProgress{}
currentUpdate.downloadResult = nil
currentUpdate.lastError = ""
currentUpdate.mu.Unlock()
// Start download in background
go func() {
result, err := updates.DownloadUpdate(req.Version, func(progress updates.DownloadProgress) {
currentUpdate.mu.Lock()
currentUpdate.progress = progress
currentUpdate.mu.Unlock()
})
currentUpdate.mu.Lock()
currentUpdate.downloading = false
if err != nil {
currentUpdate.lastError = err.Error()
} else {
currentUpdate.downloadResult = result
}
currentUpdate.mu.Unlock()
}()
c.JSON(http.StatusAccepted, gin.H{"message": "download started"})
}
// VerifyUpdate verifies the downloaded update.
func (h *Handler) VerifyUpdate(c *gin.Context) {
currentUpdate.mu.RLock()
result := currentUpdate.downloadResult
currentUpdate.mu.RUnlock()
if result == nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "no download available to verify"})
return
}
verifyResult, err := updates.VerifyDownload(result)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, verifyResult)
}
// InstallUpdate installs the downloaded update.
func (h *Handler) InstallUpdate(c *gin.Context) {
currentUpdate.mu.RLock()
downloadResult := currentUpdate.downloadResult
currentUpdate.mu.RUnlock()
if downloadResult == nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "no download available to install"})
return
}
// Verify before install
verifyResult, err := updates.VerifyDownload(downloadResult)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
if !verifyResult.Valid {
c.JSON(http.StatusBadRequest, gin.H{"error": "verification failed: " + verifyResult.Message})
return
}
// Prepare the update (extract if needed)
executablePath, err := updates.PrepareInstall(downloadResult)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
// Install
installResult, err := updates.InstallUpdate(executablePath)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, installResult)
}
+64 -1
View File
@@ -1,20 +1,83 @@
package management
import (
"encoding/json"
"net/http"
"time"
"github.com/gin-gonic/gin"
"github.com/router-for-me/CLIProxyAPI/v6/internal/usage"
)
type usageExportPayload struct {
Version int `json:"version"`
ExportedAt time.Time `json:"exported_at"`
Usage usage.StatisticsSnapshot `json:"usage"`
}
type usageImportPayload struct {
Version int `json:"version"`
Usage usage.StatisticsSnapshot `json:"usage"`
}
// GetUsageStatistics returns the in-memory request statistics snapshot.
func (h *Handler) GetUsageStatistics(c *gin.Context) {
var snapshot usage.StatisticsSnapshot
if h != nil && h.usageStats != nil {
snapshot = h.usageStats.Snapshot()
}
usageStats := usage.ComputeUsageStats(snapshot)
c.JSON(http.StatusOK, gin.H{
"usage": snapshot,
"usage": usageStats,
"raw_snapshot": snapshot,
"failed_requests": snapshot.FailureCount,
})
}
// ExportUsageStatistics returns a complete usage snapshot for backup/migration.
func (h *Handler) ExportUsageStatistics(c *gin.Context) {
var snapshot usage.StatisticsSnapshot
if h != nil && h.usageStats != nil {
snapshot = h.usageStats.Snapshot()
}
c.JSON(http.StatusOK, usageExportPayload{
Version: 1,
ExportedAt: time.Now().UTC(),
Usage: snapshot,
})
}
// ImportUsageStatistics merges a previously exported usage snapshot into memory.
func (h *Handler) ImportUsageStatistics(c *gin.Context) {
if h == nil || h.usageStats == nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "usage statistics unavailable"})
return
}
data, err := c.GetRawData()
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "failed to read request body"})
return
}
var payload usageImportPayload
if err := json.Unmarshal(data, &payload); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid json"})
return
}
if payload.Version != 0 && payload.Version != 1 {
c.JSON(http.StatusBadRequest, gin.H{"error": "unsupported version"})
return
}
result := h.usageStats.MergeSnapshot(payload.Usage)
snapshot := h.usageStats.Snapshot()
c.JSON(http.StatusOK, gin.H{
"added": result.Added,
"skipped": result.Skipped,
"total_requests": snapshot.TotalRequests,
"failed_requests": snapshot.FailureCount,
})
}
@@ -0,0 +1,203 @@
// Package handlers provides HTTP handlers for the API server.
package handlers
import (
"net/http"
"github.com/gin-gonic/gin"
"github.com/router-for-me/CLIProxyAPI/v6/sdk/translator"
)
// TranslatorHandler provides HTTP handlers for translation-related endpoints.
type TranslatorHandler struct{}
// NewTranslatorHandler creates a new translator handler.
func NewTranslatorHandler() *TranslatorHandler {
return &TranslatorHandler{}
}
// TranslationsMatrixResponse represents the response for the translations matrix endpoint.
type TranslationsMatrixResponse struct {
Matrix map[string][]string `json:"matrix"`
Formats []string `json:"formats"`
Total int `json:"total_translations"`
Details []translator.TranslationInfo `json:"details,omitempty"`
}
// GetTranslationsMatrix returns the full compatibility matrix of supported translations.
// GET /v1/translations
func (h *TranslatorHandler) GetTranslationsMatrix(c *gin.Context) {
matrix := translator.GetCompatibilityMatrix()
formats := translator.GetSupportedFormats()
translations := translator.GetAllTranslations()
// Convert formats to strings
formatStrings := make([]string, len(formats))
for i, f := range formats {
formatStrings[i] = f.String()
}
// Count total translations
total := 0
for _, targets := range matrix {
total += len(targets)
}
// Check if detailed info is requested
includeDetails := c.Query("details") == "true"
response := TranslationsMatrixResponse{
Matrix: matrix,
Formats: formatStrings,
Total: total,
}
if includeDetails {
response.Details = translations
}
c.JSON(http.StatusOK, response)
}
// CheckTranslationResponse represents the response for checking a specific translation.
type CheckTranslationResponse struct {
Supported bool `json:"supported"`
Fallback bool `json:"fallback"`
From string `json:"from"`
To string `json:"to"`
Info *translator.TranslationInfo `json:"info,omitempty"`
Alternatives []string `json:"alternatives,omitempty"`
}
// CheckTranslation checks if a specific translation path is supported.
// GET /v1/translations/check?from=X&to=Y
func (h *TranslatorHandler) CheckTranslation(c *gin.Context) {
from := c.Query("from")
to := c.Query("to")
if from == "" || to == "" {
c.JSON(http.StatusBadRequest, gin.H{
"error": "missing required query parameters",
"details": "both 'from' and 'to' query parameters are required",
})
return
}
fromFormat := translator.FromString(from)
toFormat := translator.FromString(to)
supported := translator.IsTranslationSupported(fromFormat, toFormat)
info := translator.GetTranslationInfo(fromFormat, toFormat)
response := CheckTranslationResponse{
Supported: supported,
Fallback: false,
From: from,
To: to,
}
// Include detailed info if translation exists
if supported {
response.Info = info
} else {
// Check for potential fallback paths or alternatives
matrix := translator.GetCompatibilityMatrix()
if targets, exists := matrix[from]; exists {
response.Alternatives = targets
}
// Mark as fallback if the request would pass through unchanged
// (same format translation is always supported as a no-op)
if from == to {
response.Fallback = true
response.Supported = true
}
}
c.JSON(http.StatusOK, response)
}
// GetTranslationDocs returns markdown documentation for all translations.
// GET /v1/translations/docs
func (h *TranslatorHandler) GetTranslationDocs(c *gin.Context) {
format := c.Query("format")
switch format {
case "mermaid":
c.Header("Content-Type", "text/plain; charset=utf-8")
c.String(http.StatusOK, translator.GenerateMermaidDiagram())
case "summary":
c.Header("Content-Type", "text/plain; charset=utf-8")
c.String(http.StatusOK, translator.GenerateTranslationSummary())
default:
// Default to markdown
c.Header("Content-Type", "text/markdown; charset=utf-8")
c.String(http.StatusOK, translator.GenerateMarkdownDocs())
}
}
// ScoreTranslationRequest represents the request body for scoring a translation.
type ScoreTranslationRequest struct {
From string `json:"from" binding:"required"`
To string `json:"to" binding:"required"`
Before string `json:"before" binding:"required"`
After string `json:"after" binding:"required"`
}
// ScoreTranslation analyzes the quality of a translation.
// POST /v1/translations/score
func (h *TranslatorHandler) ScoreTranslation(c *gin.Context) {
var req ScoreTranslationRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"error": "invalid request body",
"details": err.Error(),
})
return
}
fromFormat := translator.FromString(req.From)
toFormat := translator.FromString(req.To)
report := translator.ScoreTranslation(
fromFormat,
toFormat,
[]byte(req.Before),
[]byte(req.After),
)
c.JSON(http.StatusOK, report)
}
// CompareStructuresRequest represents the request body for comparing JSON structures.
type CompareStructuresRequest struct {
Before string `json:"before" binding:"required"`
After string `json:"after" binding:"required"`
}
// CompareStructures provides detailed comparison of two JSON structures.
// POST /v1/translations/compare
func (h *TranslatorHandler) CompareStructures(c *gin.Context) {
var req CompareStructuresRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"error": "invalid request body",
"details": err.Error(),
})
return
}
comparison, err := translator.CompareJSONStructures(
[]byte(req.Before),
[]byte(req.After),
)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"error": "failed to compare structures",
"details": err.Error(),
})
return
}
c.JSON(http.StatusOK, comparison)
}
@@ -0,0 +1,414 @@
package handlers
import (
"bytes"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"github.com/gin-gonic/gin"
"github.com/router-for-me/CLIProxyAPI/v6/sdk/translator"
)
func init() {
gin.SetMode(gin.TestMode)
}
func setupTestRouter() *gin.Engine {
router := gin.New()
handler := NewTranslatorHandler()
v1 := router.Group("/v1")
{
v1.GET("/translations", handler.GetTranslationsMatrix)
v1.GET("/translations/check", handler.CheckTranslation)
v1.GET("/translations/docs", handler.GetTranslationDocs)
v1.POST("/translations/score", handler.ScoreTranslation)
v1.POST("/translations/compare", handler.CompareStructures)
}
return router
}
func TestGetTranslationsMatrix(t *testing.T) {
router := setupTestRouter()
req, _ := http.NewRequest("GET", "/v1/translations", nil)
w := httptest.NewRecorder()
router.ServeHTTP(w, req)
if w.Code != http.StatusOK {
t.Errorf("Expected status %d, got %d", http.StatusOK, w.Code)
}
var response TranslationsMatrixResponse
if err := json.Unmarshal(w.Body.Bytes(), &response); err != nil {
t.Fatalf("Failed to parse response: %v", err)
}
if response.Matrix == nil {
t.Error("Matrix should not be nil")
}
if response.Formats == nil {
t.Error("Formats should not be nil")
}
}
func TestGetTranslationsMatrix_WithDetails(t *testing.T) {
router := setupTestRouter()
req, _ := http.NewRequest("GET", "/v1/translations?details=true", nil)
w := httptest.NewRecorder()
router.ServeHTTP(w, req)
if w.Code != http.StatusOK {
t.Errorf("Expected status %d, got %d", http.StatusOK, w.Code)
}
var response TranslationsMatrixResponse
if err := json.Unmarshal(w.Body.Bytes(), &response); err != nil {
t.Fatalf("Failed to parse response: %v", err)
}
// When details=true, Details should be populated
// (may be empty if no translations registered, but should be non-nil conceptually)
}
func TestCheckTranslation_Supported(t *testing.T) {
router := setupTestRouter()
// Register a test translator
translator.Register(translator.FormatOpenAI, translator.FormatClaude, func(model string, data []byte, stream bool) []byte {
return data
}, translator.ResponseTransform{})
defer translator.Unregister(translator.FormatOpenAI, translator.FormatClaude)
req, _ := http.NewRequest("GET", "/v1/translations/check?from=openai&to=claude", nil)
w := httptest.NewRecorder()
router.ServeHTTP(w, req)
if w.Code != http.StatusOK {
t.Errorf("Expected status %d, got %d", http.StatusOK, w.Code)
}
var response CheckTranslationResponse
if err := json.Unmarshal(w.Body.Bytes(), &response); err != nil {
t.Fatalf("Failed to parse response: %v", err)
}
if !response.Supported {
t.Error("Translation should be supported")
}
if response.From != "openai" {
t.Errorf("From = %s, want openai", response.From)
}
if response.To != "claude" {
t.Errorf("To = %s, want claude", response.To)
}
}
func TestCheckTranslation_NotSupported(t *testing.T) {
router := setupTestRouter()
req, _ := http.NewRequest("GET", "/v1/translations/check?from=unknown&to=unknown2", nil)
w := httptest.NewRecorder()
router.ServeHTTP(w, req)
if w.Code != http.StatusOK {
t.Errorf("Expected status %d, got %d", http.StatusOK, w.Code)
}
var response CheckTranslationResponse
if err := json.Unmarshal(w.Body.Bytes(), &response); err != nil {
t.Fatalf("Failed to parse response: %v", err)
}
if response.Supported {
t.Error("Translation should not be supported for unknown formats")
}
}
func TestCheckTranslation_MissingParams(t *testing.T) {
router := setupTestRouter()
tests := []struct {
name string
url string
}{
{"missing both", "/v1/translations/check"},
{"missing to", "/v1/translations/check?from=openai"},
{"missing from", "/v1/translations/check?to=claude"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
req, _ := http.NewRequest("GET", tt.url, nil)
w := httptest.NewRecorder()
router.ServeHTTP(w, req)
if w.Code != http.StatusBadRequest {
t.Errorf("Expected status %d, got %d", http.StatusBadRequest, w.Code)
}
})
}
}
func TestCheckTranslation_SameFormat(t *testing.T) {
router := setupTestRouter()
req, _ := http.NewRequest("GET", "/v1/translations/check?from=openai&to=openai", nil)
w := httptest.NewRecorder()
router.ServeHTTP(w, req)
if w.Code != http.StatusOK {
t.Errorf("Expected status %d, got %d", http.StatusOK, w.Code)
}
var response CheckTranslationResponse
if err := json.Unmarshal(w.Body.Bytes(), &response); err != nil {
t.Fatalf("Failed to parse response: %v", err)
}
// Same format translation is a fallback/no-op
if !response.Fallback {
t.Error("Same format should be marked as fallback")
}
if !response.Supported {
t.Error("Same format should be supported")
}
}
func TestGetTranslationDocs_Markdown(t *testing.T) {
router := setupTestRouter()
req, _ := http.NewRequest("GET", "/v1/translations/docs", nil)
w := httptest.NewRecorder()
router.ServeHTTP(w, req)
if w.Code != http.StatusOK {
t.Errorf("Expected status %d, got %d", http.StatusOK, w.Code)
}
contentType := w.Header().Get("Content-Type")
if contentType != "text/markdown; charset=utf-8" {
t.Errorf("Content-Type = %s, want text/markdown; charset=utf-8", contentType)
}
body := w.Body.String()
if body == "" {
t.Error("Response body should not be empty")
}
}
func TestGetTranslationDocs_Mermaid(t *testing.T) {
router := setupTestRouter()
req, _ := http.NewRequest("GET", "/v1/translations/docs?format=mermaid", nil)
w := httptest.NewRecorder()
router.ServeHTTP(w, req)
if w.Code != http.StatusOK {
t.Errorf("Expected status %d, got %d", http.StatusOK, w.Code)
}
contentType := w.Header().Get("Content-Type")
if contentType != "text/plain; charset=utf-8" {
t.Errorf("Content-Type = %s, want text/plain; charset=utf-8", contentType)
}
body := w.Body.String()
if !containsString(body, "mermaid") {
t.Error("Response should contain mermaid diagram")
}
}
func TestGetTranslationDocs_Summary(t *testing.T) {
router := setupTestRouter()
req, _ := http.NewRequest("GET", "/v1/translations/docs?format=summary", nil)
w := httptest.NewRecorder()
router.ServeHTTP(w, req)
if w.Code != http.StatusOK {
t.Errorf("Expected status %d, got %d", http.StatusOK, w.Code)
}
contentType := w.Header().Get("Content-Type")
if contentType != "text/plain; charset=utf-8" {
t.Errorf("Content-Type = %s, want text/plain; charset=utf-8", contentType)
}
body := w.Body.String()
if !containsString(body, "Summary") {
t.Error("Response should contain summary")
}
}
func TestScoreTranslation(t *testing.T) {
router := setupTestRouter()
requestBody := ScoreTranslationRequest{
From: "openai",
To: "claude",
Before: `{"model": "gpt-4", "messages": []}`,
After: `{"model": "gpt-4", "messages": []}`,
}
body, _ := json.Marshal(requestBody)
req, _ := http.NewRequest("POST", "/v1/translations/score", bytes.NewBuffer(body))
req.Header.Set("Content-Type", "application/json")
w := httptest.NewRecorder()
router.ServeHTTP(w, req)
if w.Code != http.StatusOK {
t.Errorf("Expected status %d, got %d", http.StatusOK, w.Code)
}
var report translator.QualityReport
if err := json.Unmarshal(w.Body.Bytes(), &report); err != nil {
t.Fatalf("Failed to parse response: %v", err)
}
// Perfect match should have high score
if report.Score < 0.9 {
t.Errorf("Score = %v, expected >= 0.9 for identical payloads", report.Score)
}
}
func TestScoreTranslation_InvalidBody(t *testing.T) {
router := setupTestRouter()
req, _ := http.NewRequest("POST", "/v1/translations/score", bytes.NewBufferString("invalid json"))
req.Header.Set("Content-Type", "application/json")
w := httptest.NewRecorder()
router.ServeHTTP(w, req)
if w.Code != http.StatusBadRequest {
t.Errorf("Expected status %d, got %d", http.StatusBadRequest, w.Code)
}
}
func TestScoreTranslation_MissingFields(t *testing.T) {
router := setupTestRouter()
// Missing required fields
requestBody := map[string]string{
"from": "openai",
// missing to, before, after
}
body, _ := json.Marshal(requestBody)
req, _ := http.NewRequest("POST", "/v1/translations/score", bytes.NewBuffer(body))
req.Header.Set("Content-Type", "application/json")
w := httptest.NewRecorder()
router.ServeHTTP(w, req)
if w.Code != http.StatusBadRequest {
t.Errorf("Expected status %d, got %d", http.StatusBadRequest, w.Code)
}
}
func TestCompareStructures(t *testing.T) {
router := setupTestRouter()
requestBody := CompareStructuresRequest{
Before: `{"a": 1, "b": 2}`,
After: `{"a": 1, "c": 3}`,
}
body, _ := json.Marshal(requestBody)
req, _ := http.NewRequest("POST", "/v1/translations/compare", bytes.NewBuffer(body))
req.Header.Set("Content-Type", "application/json")
w := httptest.NewRecorder()
router.ServeHTTP(w, req)
if w.Code != http.StatusOK {
t.Errorf("Expected status %d, got %d", http.StatusOK, w.Code)
}
var comparison translator.StructureComparison
if err := json.Unmarshal(w.Body.Bytes(), &comparison); err != nil {
t.Fatalf("Failed to parse response: %v", err)
}
if comparison.SourceType != "object" {
t.Errorf("SourceType = %s, want object", comparison.SourceType)
}
if comparison.TargetType != "object" {
t.Errorf("TargetType = %s, want object", comparison.TargetType)
}
if len(comparison.Diffs) == 0 {
t.Error("Should detect differences between the structures")
}
}
func TestCompareStructures_InvalidBody(t *testing.T) {
router := setupTestRouter()
req, _ := http.NewRequest("POST", "/v1/translations/compare", bytes.NewBufferString("invalid json"))
req.Header.Set("Content-Type", "application/json")
w := httptest.NewRecorder()
router.ServeHTTP(w, req)
if w.Code != http.StatusBadRequest {
t.Errorf("Expected status %d, got %d", http.StatusBadRequest, w.Code)
}
}
func TestCompareStructures_InvalidJSON(t *testing.T) {
router := setupTestRouter()
requestBody := CompareStructuresRequest{
Before: `{invalid}`,
After: `{"valid": true}`,
}
body, _ := json.Marshal(requestBody)
req, _ := http.NewRequest("POST", "/v1/translations/compare", bytes.NewBuffer(body))
req.Header.Set("Content-Type", "application/json")
w := httptest.NewRecorder()
router.ServeHTTP(w, req)
if w.Code != http.StatusBadRequest {
t.Errorf("Expected status %d, got %d for invalid JSON in payload", http.StatusBadRequest, w.Code)
}
}
func TestCompareStructures_IdenticalStructures(t *testing.T) {
router := setupTestRouter()
requestBody := CompareStructuresRequest{
Before: `{"a": 1, "b": 2}`,
After: `{"a": 1, "b": 2}`,
}
body, _ := json.Marshal(requestBody)
req, _ := http.NewRequest("POST", "/v1/translations/compare", bytes.NewBuffer(body))
req.Header.Set("Content-Type", "application/json")
w := httptest.NewRecorder()
router.ServeHTTP(w, req)
if w.Code != http.StatusOK {
t.Errorf("Expected status %d, got %d", http.StatusOK, w.Code)
}
var comparison translator.StructureComparison
if err := json.Unmarshal(w.Body.Bytes(), &comparison); err != nil {
t.Fatalf("Failed to parse response: %v", err)
}
if len(comparison.Diffs) != 0 {
t.Errorf("Identical structures should have no diffs, got %d", len(comparison.Diffs))
}
}
// Helper function
func containsString(s, substr string) bool {
for i := 0; i <= len(s)-len(substr); i++ {
if s[i:i+len(substr)] == substr {
return true
}
}
return false
}
+244 -49
View File
@@ -8,12 +8,33 @@ import (
"path/filepath"
"strconv"
"strings"
"sync"
"github.com/gin-gonic/gin"
"github.com/tidwall/gjson"
"github.com/tidwall/sjson"
)
var (
harnessEnabledOnce sync.Once
harnessEnabled bool
)
// isHarnessEnabled checks if the agentic harness is enabled via environment variable.
// Defaults to true if CLIPROXY_HARNESS_ENABLED is not set or is "true"/"1".
func isHarnessEnabled() bool {
harnessEnabledOnce.Do(func() {
val := os.Getenv("CLIPROXY_HARNESS_ENABLED")
if val == "" {
harnessEnabled = true
return
}
val = strings.ToLower(strings.TrimSpace(val))
harnessEnabled = val == "true" || val == "1" || val == "yes"
})
return harnessEnabled
}
const (
// Initializer prompt: forces the agent to set up the environment.
harnessInitializerPrompt = `
@@ -21,28 +42,85 @@ const (
You are the **Initializer Agent** in a Long-Running Agent Harness.
Your ONLY goal is to set up the project environment for future Coding Agents.
**CRITICAL INSTRUCTIONS**:
1. **Analyze the User's Request**: Understand what feature or app needs to be built.
2. **Create 'feature_list.json'**:
- Write a JSON file listing ALL features/requirements as separate items.
- Mark them all as "passes": false.
- Format:
Request: "Build a todo app"
File Content:
[
{ "category": "core", "description": "User can add a todo", "steps": ["Open app", "Enter text", "Press enter", "See new item"], "passes": false },
{ "category": "core", "description": "User can delete a todo", "steps": ["Open app", "Click delete", "Item removed"], "passes": false }
]
3. **Create 'claude-progress.txt'**:
- Create this file with a header "## Progress Log".
- Add an initial entry: "- [Initializer] Environment setup started."
4. **Create 'init.sh' (Optional)**:
- If a dev server is needed, create a script to start it (e.g., 'npm run dev').
5. **Initial Commit**:
- Initialize git if not present.
- Commit these foundational files.
## CRITICAL: YOU DO NOT IMPLEMENT FEATURES
Your job is ONLY to scaffold the harness files. Do NOT write any application code.
Do NOT create components, pages, or business logic. Only create the harness metadata files.
**DO NOT** start implementing features yourself. Your job is ONLY to scaffold the harness.
## INSTRUCTIONS
### 1. Analyze the User's Request
Understand what feature or app needs to be built. Break it down into discrete, testable features.
### 2. Create 'feature_list.json'
Write a JSON file listing ALL features/requirements as separate items.
**Required Format:**
{
"features": [
{
"id": "feat-001",
"category": "core",
"description": "User can add a todo item",
"steps": [
"Open the application",
"Enter text in the input field",
"Press Enter or click Add button",
"Verify new item appears in the list"
],
"passes": false,
"priority": 1
},
{
"id": "feat-002",
"category": "core",
"description": "User can delete a todo item",
"steps": [
"Open the application with existing items",
"Click the delete button on an item",
"Verify item is removed from the list"
],
"passes": false,
"priority": 2
}
]
}
**Field Descriptions:**
- id: Unique identifier (feat-XXX format)
- category: "core", "enhancement", "ui", "integration", etc.
- description: Clear, testable user-facing behavior
- steps: Exact steps to verify this feature works
- passes: Always false initially
- priority: Lower number = higher priority (implement first)
### 3. Create 'claude-progress.txt'
Create this file with structured session logging:
## Progress Log
### Session: YYYY-MM-DD HH:MM
- [Initializer] Environment setup started.
- [Initializer] Created feature_list.json with N features.
- [Initializer] Created init.sh for development server.
### 4. Create 'init.sh' (REQUIRED for web apps)
For ANY web application, you MUST create init.sh:
#!/bin/bash
# Start the development server
npm install
npm run dev &
sleep 5
echo "Dev server started at http://localhost:3000"
Make it executable and ensure it handles common setup tasks.
### 5. Initial Commit
- Initialize git if not present.
- Commit these foundational files with message: "chore: initialize agent harness"
## REMINDER
You are ONLY scaffolding. The Coding Agent will implement features in future turns.
Do NOT write application code. Do NOT create UI components. ONLY create the harness files.
`
// Coding prompt: forces the agent to work incrementally.
@@ -51,30 +129,61 @@ Your ONLY goal is to set up the project environment for future Coding Agents.
You are a **Coding Agent** in a Long-Running Agent Harness.
Your goal is to make **incremental progress** on the project.
**WORKFLOW**:
1. **Read Context**:
- Read 'claude-progress.txt' to see what was done last.
- Read 'feature_list.json' to see what is missing.
- Read recent git log to understand recent changes.
- If 'init.sh' exists, run it to start the dev server.
- Run a basic end-to-end sanity check before new work.
2. **Pick ONE Task**:
- Choose the highest-priority failing feature from 'feature_list.json'.
- Announce your plan.
3. **Implement & Verify**:
- Write code for that ONE feature.
- Verify it works end-to-end (tests or browser check).
4. **Update State**:
- Update 'feature_list.json': set "passes": true for the completed feature.
- Append to 'claude-progress.txt': "- [Coding] Implemented <feature>."
5. **Commit**:
- Git commit your changes.
## STEP 1: GET BEARINGS (MANDATORY - DO THIS FIRST)
Before ANY coding, you MUST execute these commands in order:
**RESTRICTIONS**:
- Do NOT try to finish the whole project in one turn.
- Do NOT leave the code in a broken state.
- Always update the harness files ('feature_list.json', 'claude-progress.txt') before stopping.
- Do NOT edit 'feature_list.json' except toggling a feature's "passes" field after verified testing.
1. pwd # Know where you are
2. git log --oneline -5 # See recent changes
3. cat claude-progress.txt # Read what was done before
4. cat feature_list.json # See all features and their status
5. ./init.sh # Start dev server if it exists
Do NOT skip this step. Do NOT assume you know the project state.
## STEP 2: SANITY CHECK (MANDATORY)
Before implementing anything new:
- Verify existing features still work
- Check for any regressions from previous changes
- If something is broken, FIX IT FIRST before new work
## STEP 3: PICK ONE TASK
- Find the highest-priority feature where "passes": false
- Announce: "I will implement: [feature description]"
- Do NOT pick multiple features
## STEP 4: IMPLEMENT & VERIFY
- Write code for that ONE feature only
- You MUST verify it works before marking complete
- Verification methods (in order of preference):
1. **Run tests**: Execute unit/integration test suites
2. **Build check**: Ensure the project compiles without errors
3. **Manual verification**: Test the feature manually if automated tests unavailable
**VERIFICATION IS NOT OPTIONAL.** You must ACTUALLY TEST the feature.
Do NOT mark a feature as passing based on "the code looks correct."
## STEP 5: UPDATE STATE
After VERIFIED implementation:
1. Update feature_list.json:
- Set "passes": true ONLY for features you have ACTUALLY VERIFIED
2. Append to claude-progress.txt:
- [Coding] Implemented <feature id>: <description>. Verified via <method>.
Example:
- [Coding] Implemented feat-003: User can mark todo complete. Verified via unit tests.
## STEP 6: COMMIT
- Git commit with descriptive message
- Include feature ID in commit message
## RESTRICTIONS
- Do NOT try to finish the whole project in one turn
- Do NOT leave the code in a broken state
- Do NOT mark features as "passes": true without ACTUAL verification
- Do NOT edit feature_list.json except to toggle "passes" after verified testing
- ALWAYS update harness files before stopping
`
)
@@ -87,6 +196,12 @@ func AgenticHarnessMiddleware() gin.HandlerFunc {
// If rootDir is empty, the current working directory is used.
func AgenticHarnessMiddlewareWithRootDir(rootDir string) gin.HandlerFunc {
return func(c *gin.Context) {
// Check if harness is enabled via environment variable
if !isHarnessEnabled() {
c.Next()
return
}
req := c.Request
if req == nil || req.Method != http.MethodPost {
c.Next()
@@ -116,8 +231,8 @@ func AgenticHarnessMiddlewareWithRootDir(rootDir string) gin.HandlerFunc {
// 3. Detect State
// We look for evidence that the harness is already active (features/progress files).
// We also check conversation length.
state := detectHarnessState(body, rootDir)
// We also check conversation length and session-based storage.
state := detectHarnessState(body, c, rootDir)
// 4. Inject Prompt
var promptToInject string
@@ -146,7 +261,72 @@ func AgenticHarnessMiddlewareWithRootDir(rootDir string) gin.HandlerFunc {
}
}
func detectHarnessState(body []byte, rootDir string) string {
// extractSessionKeyFromContext extracts session key from request headers.
func extractSessionKeyFromContext(c *gin.Context) string {
if v := c.GetHeader("X-CLIProxyAPI-Session"); v != "" {
return v
}
if v := c.GetHeader("X-Session-Id"); v != "" {
return v
}
return ""
}
// extractSessionKeyFromBody extracts session key from request body JSON fields.
func extractSessionKeyFromBody(body []byte) string {
if v := gjson.GetBytes(body, "prompt_cache_key"); v.Exists() && v.Type == gjson.String {
return v.String()
}
if v := gjson.GetBytes(body, "metadata.session_id"); v.Exists() && v.Type == gjson.String {
return v.String()
}
if v := gjson.GetBytes(body, "session_id"); v.Exists() && v.Type == gjson.String {
return v.String()
}
return ""
}
// memoryBaseDirForHarness returns the base directory for memory/session storage.
func memoryBaseDirForHarness() string {
if v := os.Getenv("CLIPROXY_MEMORY_DIR"); v != "" {
return v
}
// Try default locations
if home, err := os.UserHomeDir(); err == nil {
return filepath.Join(home, ".proxypilot", "memory")
}
return filepath.Join(".", ".proxypilot", "memory")
}
// harnessFileVariations lists possible file name variations for harness files.
var harnessProgressFileVariations = []string{
"claude-progress.txt",
"claude-progress",
"progress.txt",
}
var harnessFeatureFileVariations = []string{
"feature_list.json",
"feature_list",
"features.json",
}
// checkHarnessFilesExist checks if any harness files exist in the given directory.
func checkHarnessFilesExist(dir string) bool {
for _, f := range harnessProgressFileVariations {
if fileExists(dir, f) {
return true
}
}
for _, f := range harnessFeatureFileVariations {
if fileExists(dir, f) {
return true
}
}
return false
}
func detectHarnessState(body []byte, c *gin.Context, rootDir string) string {
// Simple heuristic:
// If the conversation contains references to "claude-progress.txt" or "feature_list.json",
// it's likely already in the harness flow -> CODING.
@@ -157,8 +337,23 @@ func detectHarnessState(body []byte, rootDir string) string {
return "CODING"
}
// Prefer filesystem truth when available.
if fileExists(rootDir, "claude-progress.txt") || fileExists(rootDir, "feature_list.json") {
// Try to get session key from context headers first, then from body
sessionKey := extractSessionKeyFromContext(c)
if sessionKey == "" {
sessionKey = extractSessionKeyFromBody(body)
}
// Check session-based storage for harness files
if sessionKey != "" {
memoryDir := memoryBaseDirForHarness()
sessionHarnessDir := filepath.Join(memoryDir, "sessions", sessionKey, "harness")
if checkHarnessFilesExist(sessionHarnessDir) {
return "CODING"
}
}
// Prefer filesystem truth when available (check rootDir with file variations).
if checkHarnessFilesExist(rootDir) {
return "CODING"
}
@@ -7,6 +7,7 @@ import (
"net/http/httptest"
"os"
"path/filepath"
"strings"
"testing"
"github.com/gin-gonic/gin"
@@ -182,3 +183,35 @@ func TestAgenticHarnessMiddleware_ResponsesAPI(t *testing.T) {
})
}
}
func TestAgenticHarnessMiddleware_SessionStorage(t *testing.T) {
gin.SetMode(gin.TestMode)
// Create temp memory directory
tmpDir, err := os.MkdirTemp("", "harness-session-test")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tmpDir)
os.Setenv("CLIPROXY_MEMORY_DIR", tmpDir)
defer os.Unsetenv("CLIPROXY_MEMORY_DIR")
handler := AgenticHarnessMiddleware()
// First request with session header
w := httptest.NewRecorder()
c, _ := gin.CreateTestContext(w)
body := `{"model":"gpt-4","messages":[{"role":"user","content":"hello"}]}`
c.Request = httptest.NewRequest("POST", "/v1/chat/completions", strings.NewReader(body))
c.Request.Header.Set("User-Agent", "claude-cli/1.0")
c.Request.Header.Set("X-CLIProxyAPI-Session", "test-session-123")
c.Request.Header.Set("Content-Type", "application/json")
handler(c)
// Verify session directory was considered
if c.Writer.Header().Get("X-ProxyPilot-Harness-Mode") == "" {
t.Error("Expected harness mode header to be set")
}
}
+274 -3
View File
@@ -18,6 +18,7 @@ import (
"github.com/router-for-me/CLIProxyAPI/v6/internal/embeddings"
"github.com/router-for-me/CLIProxyAPI/v6/internal/memory"
"github.com/router-for-me/CLIProxyAPI/v6/internal/registry"
"github.com/router-for-me/CLIProxyAPI/v6/internal/runtime/executor"
"github.com/router-for-me/CLIProxyAPI/v6/internal/util"
"github.com/tidwall/gjson"
"github.com/tidwall/sjson"
@@ -134,10 +135,56 @@ func agenticMemoryStore() memory.Store {
}
}
memStore = memory.NewFileStore(base)
// Initialize summarizer with default config (uses regex fallback until executor is set)
if agenticLLMSummaryEnabled() {
if fs, ok := memStore.(*memory.FileStore); ok {
config := memory.DefaultSummarizerConfig()
// Start with NoOp executor - will fall back to regex
// Call SetSummarizerExecutor() later with a real executor to enable LLM summarization
summarizer := memory.NewSummarizer(config, memory.NewNoOpSummarizerExecutor())
fs.SetSummarizer(summarizer)
}
}
})
return memStore
}
// SetSummarizerExecutor configures the LLM executor for context summarization.
// Call this from server initialization with the auth manager to enable full LLM summarization.
// Example: middleware.SetSummarizerExecutor(memory.NewPipelineSummarizerExecutor(authManager, providers))
func SetSummarizerExecutor(executor memory.SummarizerExecutor) {
store := agenticMemoryStore()
if store == nil {
return
}
fs, ok := store.(*memory.FileStore)
if !ok || fs == nil {
return
}
summarizer := fs.GetSummarizer()
if summarizer == nil {
config := memory.DefaultSummarizerConfig()
summarizer = memory.NewSummarizer(config, executor)
fs.SetSummarizer(summarizer)
} else {
// Replace the existing summarizer with one that has the real executor
config := memory.DefaultSummarizerConfig()
fs.SetSummarizer(memory.NewSummarizer(config, executor))
}
}
// InitSummarizerWithAuthManager configures LLM-based summarization using the core auth manager.
// Call this after the auth manager is initialized to enable Factory.ai-style context compression.
func InitSummarizerWithAuthManager(manager memory.CoreManagerExecutor, providers []string) {
if manager == nil {
return
}
adapter := memory.NewManagerAuthAdapter(manager)
executor := memory.NewPipelineSummarizerExecutor(adapter, providers)
SetSummarizerExecutor(executor)
}
func agenticSemanticEnabled() bool {
if v := strings.TrimSpace(os.Getenv("CLIPROXY_SEMANTIC_ENABLED")); v != "" {
if strings.EqualFold(v, "0") || strings.EqualFold(v, "false") || strings.EqualFold(v, "off") || strings.EqualFold(v, "no") {
@@ -400,6 +447,176 @@ func agenticAnchorSummaryMaxChars() int {
return 14_000
}
// agenticCompressionThreshold returns the context usage ratio at which to trigger LLM compression.
// Default: 0.75 (75% of context window)
func agenticCompressionThreshold() float64 {
if v := strings.TrimSpace(os.Getenv("CLIPROXY_COMPRESSION_THRESHOLD")); v != "" {
if f, err := strconv.ParseFloat(v, 64); err == nil && f > 0 && f < 1 {
return f
}
}
return 0.75
}
// agenticLLMSummaryEnabled returns whether LLM-based summarization is enabled.
// Default: true
func agenticLLMSummaryEnabled() bool {
if v := strings.TrimSpace(os.Getenv("CLIPROXY_LLM_SUMMARY_ENABLED")); v != "" {
return strings.EqualFold(v, "1") || strings.EqualFold(v, "true") || strings.EqualFold(v, "on")
}
return true
}
// getModelContextWindow returns the context window size for a model.
func getModelContextWindow(model string) int {
// Try registry first
info := registry.GetGlobalRegistry().GetModelInfo(model)
if info != nil && info.ContextLength > 0 {
return info.ContextLength
}
// Fallback based on model name patterns
lowerModel := strings.ToLower(model)
switch {
case strings.Contains(lowerModel, "claude-3.5"), strings.Contains(lowerModel, "claude-3-5"):
return 200000
case strings.Contains(lowerModel, "claude-3"):
return 200000
case strings.Contains(lowerModel, "claude"):
return 100000
case strings.Contains(lowerModel, "gpt-4-turbo"), strings.Contains(lowerModel, "gpt-4o"):
return 128000
case strings.Contains(lowerModel, "gpt-4"):
return 8192
case strings.Contains(lowerModel, "gpt-3.5"):
return 16384
case strings.Contains(lowerModel, "gemini"):
return 1000000
case strings.Contains(lowerModel, "o1"), strings.Contains(lowerModel, "o3"):
return 200000
default:
return 100000
}
}
// agenticTokenAwareEnabled returns whether token-aware compression is enabled.
// Default: true
func agenticTokenAwareEnabled() bool {
if v := strings.TrimSpace(os.Getenv("CLIPROXY_TOKEN_AWARE_ENABLED")); v != "" {
return strings.EqualFold(v, "1") || strings.EqualFold(v, "true") || strings.EqualFold(v, "on")
}
return true
}
// agenticReserveTokens returns the number of tokens to reserve for model output.
// Default: 8192 (reasonable output buffer)
func agenticReserveTokens() int {
if v := strings.TrimSpace(os.Getenv("CLIPROXY_RESERVE_TOKENS")); v != "" {
if n, err := strconv.Atoi(v); err == nil && n > 0 {
return n
}
}
return 8192
}
// tokenAwareCompressionResult contains the result of token-aware analysis
type tokenAwareCompressionResult struct {
ShouldTrim bool // Whether trimming is needed
CurrentTokens int64 // Estimated current token count
ContextWindow int // Model's context window
TargetTokens int64 // Target token count after trimming
TargetMaxBytes int // Approximate bytes to achieve target tokens
Model string // The model name
}
// analyzeTokenBudget checks if the request exceeds the token threshold and calculates trimming targets.
// Returns whether trimming is needed and the target byte limit.
func analyzeTokenBudget(body []byte) *tokenAwareCompressionResult {
result := &tokenAwareCompressionResult{
ShouldTrim: false,
TargetMaxBytes: agenticMaxBodyBytes(), // Default fallback
}
if !agenticTokenAwareEnabled() {
return result
}
model := gjson.GetBytes(body, "model").String()
if model == "" {
return result
}
result.Model = model
// Get context window for this model
contextWindow := getModelContextWindow(model)
result.ContextWindow = contextWindow
// Estimate current token count
currentTokens, err := executor.EstimateRequestTokens(model, body)
if err != nil {
// Fallback to byte-based heuristic: ~4 chars per token
currentTokens = int64(len(body) / 4)
}
result.CurrentTokens = currentTokens
// Reserve tokens for output
reserveTokens := agenticReserveTokens()
availableContext := contextWindow - reserveTokens
if availableContext < 1000 {
availableContext = contextWindow / 2 // At least half for input
}
// Check against threshold
threshold := agenticCompressionThreshold()
maxInputTokens := int64(float64(availableContext) * threshold)
if currentTokens <= maxInputTokens {
// Under threshold, no trimming needed
result.ShouldTrim = false
return result
}
// Calculate target tokens (aim for 70% of threshold to give buffer)
targetRatio := threshold * 0.9 // Slightly under threshold
targetTokens := int64(float64(availableContext) * targetRatio)
result.TargetTokens = targetTokens
result.ShouldTrim = true
// Convert target tokens to approximate byte limit
// Use ~4 bytes per token as heuristic, but be conservative
targetBytes := int(targetTokens * 4)
// Clamp to reasonable bounds
minBytes := 32 * 1024 // 32KB minimum
if targetBytes < minBytes {
targetBytes = minBytes
}
// Don't exceed the configured max
maxBytes := agenticMaxBodyBytes()
if targetBytes > maxBytes {
targetBytes = maxBytes
}
result.TargetMaxBytes = targetBytes
return result
}
// getTokenAwareMaxBytes returns the maximum body size based on token analysis.
// This is more accurate than the byte-based heuristic in agenticMaxBodyBytesForModel.
func getTokenAwareMaxBytes(body []byte) int {
if !agenticTokenAwareEnabled() {
return agenticMaxBodyBytesForModel(body)
}
result := analyzeTokenBudget(body)
if result.ShouldTrim {
return result.TargetMaxBytes
}
// Not over threshold, but still apply model-specific limits
return agenticMaxBodyBytesForModel(body)
}
func allowMemoryWrite(session string) bool {
limit := agenticMemoryMaxWritesPerMin()
if limit <= 0 || session == "" {
@@ -523,7 +740,31 @@ func CodexPromptBudgetMiddlewareWithRootDir(rootDir string) gin.HandlerFunc {
}
originalLen := len(body)
maxBytes := agenticMaxBodyBytesForModel(body)
// Token-aware compression: analyze token budget before byte-based check
tokenAnalysis := analyzeTokenBudget(body)
maxBytes := tokenAnalysis.TargetMaxBytes
// If token analysis didn't trigger, fall back to byte-based model limit
if !tokenAnalysis.ShouldTrim {
maxBytes = agenticMaxBodyBytesForModel(body)
}
// Add diagnostic headers for localhost debugging
if c != nil {
ip := c.ClientIP()
if ip == "127.0.0.1" || ip == "::1" {
if tokenAnalysis.Model != "" {
c.Header("X-ProxyPilot-Model", tokenAnalysis.Model)
c.Header("X-ProxyPilot-Context-Window", strconv.Itoa(tokenAnalysis.ContextWindow))
c.Header("X-ProxyPilot-Current-Tokens", strconv.FormatInt(tokenAnalysis.CurrentTokens, 10))
if tokenAnalysis.ShouldTrim {
c.Header("X-ProxyPilot-Token-Triggered", "true")
c.Header("X-ProxyPilot-Target-Tokens", strconv.FormatInt(tokenAnalysis.TargetTokens, 10))
}
}
}
}
// Session-scoped state (pinned + anchor + TODO + spec) is injected as append-only
// scaffolding when enabled. This preserves prompt-cache friendliness.
@@ -533,7 +774,9 @@ func CodexPromptBudgetMiddlewareWithRootDir(rootDir string) gin.HandlerFunc {
originalLen = len(body)
}
if originalLen <= maxBytes {
// Proactive compression: trim if over token threshold OR over byte limit
needsTrim := tokenAnalysis.ShouldTrim || originalLen > maxBytes
if !needsTrim {
req.Body = io.NopCloser(bytes.NewReader(body))
req.ContentLength = int64(originalLen)
req.Header.Set("Content-Length", strconv.Itoa(originalLen))
@@ -1349,7 +1592,13 @@ func agenticStoreAndInjectMemory(c *gin.Context, req *http.Request, session stri
_ = fs.WritePinned(session, pinned, 8000)
}
if len(res.Dropped) > 0 {
_ = agenticUpdateAnchoredSummary(fs, session, res.Dropped, pinned, res.Query)
if agenticLLMSummaryEnabled() {
model := gjson.GetBytes(res.Body, "model").String()
ctx := c.Request.Context()
_ = agenticUpdateAnchoredSummaryWithLLM(ctx, model, fs, session, res.Dropped, pinned, res.Query)
} else {
_ = agenticUpdateAnchoredSummary(fs, session, res.Dropped, pinned, res.Query)
}
}
if agenticSemanticEnabled() && len(res.Dropped) > 0 && !fs.IsSemanticDisabled(session) {
ns := semanticNamespace(req, res.Body, session)
@@ -1410,6 +1659,28 @@ func agenticUpdateAnchoredSummary(fs *memory.FileStore, session string, dropped
return fs.WriteSummary(session, next, agenticAnchorSummaryMaxChars())
}
// agenticUpdateAnchoredSummaryWithLLM updates the anchored summary using LLM.
func agenticUpdateAnchoredSummaryWithLLM(ctx context.Context, model string, fs *memory.FileStore, session string, dropped []memory.Event, pinned string, latestIntent string) error {
if fs == nil || session == "" {
return nil
}
summarizer := fs.GetSummarizer()
if summarizer == nil {
// Fall back to regex-based
return agenticUpdateAnchoredSummary(fs, session, dropped, pinned, latestIntent)
}
prev := fs.ReadSummary(session, agenticAnchorSummaryMaxChars())
next := memory.BuildAnchoredSummaryWithLLM(ctx, model, prev, dropped, latestIntent, summarizer)
if strings.TrimSpace(next) == "" {
return nil
}
if agenticAnchorAppendOnly() {
return fs.SetAnchorSummary(session, next, agenticAnchorSummaryMaxChars())
}
return fs.WriteSummary(session, next, agenticAnchorSummaryMaxChars())
}
func agenticMaybePruneMemory() {
maxAge := agenticMemoryMaxAgeDays()
maxSessions := agenticMemoryMaxSessions()
@@ -8,12 +8,41 @@ import (
"io"
"net/http"
"strings"
"sync"
"time"
"github.com/gin-gonic/gin"
"github.com/router-for-me/CLIProxyAPI/v6/internal/interfaces"
"github.com/router-for-me/CLIProxyAPI/v6/internal/logging"
"github.com/router-for-me/CLIProxyAPI/v6/internal/util"
)
var (
requestMonitorMu sync.RWMutex
requestMonitor []interfaces.RequestLogEntry
maxMonitorSize = 100
)
func addRequestToMonitor(entry interfaces.RequestLogEntry) {
requestMonitorMu.Lock()
defer requestMonitorMu.Unlock()
requestMonitor = append(requestMonitor, entry)
if len(requestMonitor) > maxMonitorSize {
requestMonitor = requestMonitor[1:]
}
}
// GetRequestMonitor returns a copy of the current request monitor entries.
func GetRequestMonitor() []interfaces.RequestLogEntry {
requestMonitorMu.RLock()
defer requestMonitorMu.RUnlock()
result := make([]interfaces.RequestLogEntry, len(requestMonitor))
copy(result, requestMonitor)
return result
}
// RequestLoggingMiddleware creates a Gin middleware that logs HTTP requests and responses.
// It captures detailed information about the request and response, including headers and body,
// and uses the provided RequestLogger to record this data. When logging is disabled in the
@@ -53,13 +82,68 @@ func RequestLoggingMiddleware(logger logging.RequestLogger) gin.HandlerFunc {
c.Writer = wrapper
// Process the request
startTime := time.Now()
c.Next()
latency := time.Since(startTime)
// Finalize logging after request processing
if err = wrapper.Finalize(c); err != nil {
// Log error but don't interrupt the response
// In a real implementation, you might want to use a proper logger here
}
// Add to monitor
entry := interfaces.RequestLogEntry{
ID: logging.GetGinRequestID(c),
Timestamp: startTime,
Method: c.Request.Method,
Path: c.Request.URL.Path,
Status: c.Writer.Status(),
LatencyMs: latency.Milliseconds(),
}
// Try to get Model and Provider from context
if model, ok := c.Get("model"); ok {
if m, ok := model.(string); ok {
entry.Model = m
}
}
if provider, ok := c.Get("provider"); ok {
if p, ok := provider.(string); ok {
entry.Provider = p
}
}
// Try to get tokens from context
if inputTokens, ok := c.Get("input_tokens"); ok {
if it, ok := inputTokens.(int); ok {
entry.InputTokens = it
} else if it, ok := inputTokens.(int64); ok {
entry.InputTokens = int(it)
}
}
if outputTokens, ok := c.Get("output_tokens"); ok {
if ot, ok := outputTokens.(int); ok {
entry.OutputTokens = ot
} else if ot, ok := outputTokens.(int64); ok {
entry.OutputTokens = int(ot)
}
}
// Try to get error from context
if apiErr, ok := c.Get("API_RESPONSE_ERROR"); ok {
if errs, ok := apiErr.([]*interfaces.ErrorMessage); ok && len(errs) > 0 {
var errMsgs []string
for _, e := range errs {
if e != nil && e.Error != nil {
errMsgs = append(errMsgs, e.Error.Error())
}
}
entry.Error = strings.Join(errMsgs, "; ")
}
}
addRequestToMonitor(entry)
}
}
+20 -5
View File
@@ -23,6 +23,7 @@ func (s *Server) registerProxyPilotDashboardRoutes() {
s.engine.GET("/proxypilot.html", s.serveProxyPilotDashboard)
s.engine.GET("/assets/*filepath", s.serveProxyPilotAsset)
s.engine.GET("/vite.svg", s.serveProxyPilotViteIcon)
s.engine.GET("/logo.png", s.serveProxyPilotLogo)
}
func (s *Server) serveProxyPilotDashboard(c *gin.Context) {
@@ -37,7 +38,12 @@ func (s *Server) serveProxyPilotDashboard(c *gin.Context) {
return
}
html := string(index)
key := strings.TrimSpace(getManagementKey())
// Use localPassword from the server instance (set via WithLocalManagementPassword)
// Fall back to MANAGEMENT_PASSWORD env var for legacy subprocess mode
key := strings.TrimSpace(s.localPassword)
if key == "" {
key = strings.TrimSpace(os.Getenv("MANAGEMENT_PASSWORD"))
}
if key != "" && s.managementRoutesEnabled.Load() {
meta := `<meta name="pp-mgmt-key" content="` + escapeAttr(key) + `">`
html = strings.Replace(html, "</head>", meta+"</head>", 1)
@@ -79,6 +85,19 @@ func (s *Server) serveProxyPilotViteIcon(c *gin.Context) {
writeAssetResponse(c, "vite.svg", data)
}
func (s *Server) serveProxyPilotLogo(c *gin.Context) {
if !isLocalClient(c) {
c.AbortWithStatus(http.StatusForbidden)
return
}
data, err := fsReadFile("logo.png")
if err != nil {
c.AbortWithStatus(http.StatusNotFound)
return
}
writeAssetResponse(c, "logo.png", data)
}
func writeAssetResponse(c *gin.Context, name string, data []byte) {
ext := strings.TrimPrefix(path.Ext(name), ".")
if mt := misc.MimeTypes[strings.ToLower(ext)]; mt != "" {
@@ -105,10 +124,6 @@ func isLocalClient(c *gin.Context) bool {
return clientIP == "127.0.0.1" || clientIP == "::1"
}
func getManagementKey() string {
return strings.TrimSpace(os.Getenv("MANAGEMENT_PASSWORD"))
}
func escapeAttr(s string) string {
return strings.ReplaceAll(strings.TrimSpace(s), `"`, "")
}
+1090 -1010
View File
File diff suppressed because it is too large Load Diff
-6
View File
@@ -60,12 +60,6 @@ func (e *AuthenticationError) Error() string {
// Common authentication error types.
var (
// ErrTokenExpired = &AuthenticationError{
// Type: "token_expired",
// Message: "Access token has expired",
// Code: http.StatusUnauthorized,
// }
// ErrInvalidState represents an error for invalid OAuth state parameter.
ErrInvalidState = &AuthenticationError{
Type: "invalid_state",
+159
View File
@@ -0,0 +1,159 @@
package claude
import (
"crypto/sha256"
"encoding/base64"
"regexp"
"testing"
)
func TestPKCE_GenerateVerifier_Length(t *testing.T) {
tests := []struct {
name string
expectedLength int
}{
{
name: "verifier should be 128 characters (96 bytes base64 encoded)",
expectedLength: 128,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
codes, err := GeneratePKCECodes()
if err != nil {
t.Fatalf("GeneratePKCECodes() error = %v", err)
}
if len(codes.CodeVerifier) != tt.expectedLength {
t.Errorf("CodeVerifier length = %d, want %d", len(codes.CodeVerifier), tt.expectedLength)
}
})
}
}
func TestPKCE_GenerateVerifier_Randomness(t *testing.T) {
tests := []struct {
name string
iterations int
}{
{
name: "multiple generations should produce unique verifiers",
iterations: 100,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
seen := make(map[string]bool)
for i := 0; i < tt.iterations; i++ {
codes, err := GeneratePKCECodes()
if err != nil {
t.Fatalf("GeneratePKCECodes() iteration %d error = %v", i, err)
}
if seen[codes.CodeVerifier] {
t.Errorf("Duplicate verifier detected at iteration %d", i)
}
seen[codes.CodeVerifier] = true
}
})
}
}
func TestPKCE_GenerateChallenge_SHA256(t *testing.T) {
tests := []struct {
name string
verifier string
}{
{
name: "challenge should be SHA256 hash of verifier",
verifier: "test-verifier-string-for-pkce-challenge-generation",
},
{
name: "challenge for empty verifier",
verifier: "",
},
{
name: "challenge for complex verifier with special chars",
verifier: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Generate expected challenge manually
hash := sha256.Sum256([]byte(tt.verifier))
expectedChallenge := base64.URLEncoding.WithPadding(base64.NoPadding).EncodeToString(hash[:])
// Use the internal function via GeneratePKCECodes and verify the relationship
codes, err := GeneratePKCECodes()
if err != nil {
t.Fatalf("GeneratePKCECodes() error = %v", err)
}
// Verify the generated challenge matches SHA256 of the generated verifier
generatedHash := sha256.Sum256([]byte(codes.CodeVerifier))
computedChallenge := base64.URLEncoding.WithPadding(base64.NoPadding).EncodeToString(generatedHash[:])
if codes.CodeChallenge != computedChallenge {
t.Errorf("CodeChallenge = %v, want computed SHA256 = %v", codes.CodeChallenge, computedChallenge)
}
// Also verify a known input produces expected output
_ = expectedChallenge // Used for documentation purposes
})
}
}
func TestPKCE_GenerateChallenge_Base64URL(t *testing.T) {
tests := []struct {
name string
}{
{
name: "challenge should be base64url encoded without padding",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
codes, err := GeneratePKCECodes()
if err != nil {
t.Fatalf("GeneratePKCECodes() error = %v", err)
}
// Base64URL uses - and _ instead of + and /
// Should not contain standard base64 chars + or /
if regexp.MustCompile(`[+/]`).MatchString(codes.CodeChallenge) {
t.Error("CodeChallenge contains non-URL-safe base64 characters (+ or /)")
}
// Should not have padding (=)
if regexp.MustCompile(`=`).MatchString(codes.CodeChallenge) {
t.Error("CodeChallenge contains base64 padding (=)")
}
// Should only contain valid base64url characters
validBase64URL := regexp.MustCompile(`^[A-Za-z0-9_-]+$`)
if !validBase64URL.MatchString(codes.CodeChallenge) {
t.Errorf("CodeChallenge contains invalid base64url characters: %s", codes.CodeChallenge)
}
// SHA256 produces 32 bytes, base64url without padding = 43 chars
expectedChallengeLength := 43
if len(codes.CodeChallenge) != expectedChallengeLength {
t.Errorf("CodeChallenge length = %d, want %d", len(codes.CodeChallenge), expectedChallengeLength)
}
// Verify verifier is also base64url encoded without padding
if regexp.MustCompile(`[+/=]`).MatchString(codes.CodeVerifier) {
t.Error("CodeVerifier contains non-URL-safe base64 characters or padding")
}
if !validBase64URL.MatchString(codes.CodeVerifier) {
t.Errorf("CodeVerifier contains invalid base64url characters: %s", codes.CodeVerifier)
}
})
}
}
+431
View File
@@ -0,0 +1,431 @@
package claude
import (
"encoding/json"
"os"
"path/filepath"
"testing"
"time"
)
// Helper function to parse token expiry time
func parseExpireTime(expire string) (time.Time, error) {
return time.Parse(time.RFC3339, expire)
}
// Helper function to check if token is expired
func isTokenExpired(expire string) bool {
expireTime, err := parseExpireTime(expire)
if err != nil {
return true // Treat parse errors as expired
}
return time.Now().After(expireTime)
}
// Helper function to check if token needs refresh within lead time
func tokenNeedsRefresh(expire string, leadTime time.Duration) bool {
expireTime, err := parseExpireTime(expire)
if err != nil {
return true // Treat parse errors as needing refresh
}
return time.Now().Add(leadTime).After(expireTime)
}
func TestToken_Parse_ValidToken(t *testing.T) {
tests := []struct {
name string
tokenJSON string
wantEmail string
wantType string
wantErr bool
}{
{
name: "valid token with all fields",
tokenJSON: `{
"id_token": "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.test",
"access_token": "sk-ant-test-access-token",
"refresh_token": "rt-test-refresh-token",
"last_refresh": "2025-01-15T10:00:00Z",
"email": "user@example.com",
"type": "claude",
"expired": "2025-01-15T11:00:00Z"
}`,
wantEmail: "user@example.com",
wantType: "claude",
wantErr: false,
},
{
name: "valid token with minimal fields",
tokenJSON: `{
"access_token": "sk-ant-minimal",
"refresh_token": "rt-minimal",
"email": "minimal@test.com",
"type": "claude",
"expired": "2025-12-31T23:59:59Z"
}`,
wantEmail: "minimal@test.com",
wantType: "claude",
wantErr: false,
},
{
name: "token with empty strings",
tokenJSON: `{
"access_token": "",
"refresh_token": "",
"email": "",
"type": "claude",
"expired": ""
}`,
wantEmail: "",
wantType: "claude",
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var token ClaudeTokenStorage
err := json.Unmarshal([]byte(tt.tokenJSON), &token)
if (err != nil) != tt.wantErr {
t.Errorf("json.Unmarshal() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !tt.wantErr {
if token.Email != tt.wantEmail {
t.Errorf("Email = %v, want %v", token.Email, tt.wantEmail)
}
if token.Type != tt.wantType {
t.Errorf("Type = %v, want %v", token.Type, tt.wantType)
}
}
})
}
}
func TestToken_Parse_ExpiredToken(t *testing.T) {
tests := []struct {
name string
tokenJSON string
wantErr bool
}{
{
name: "token with past expiration date",
tokenJSON: `{
"access_token": "expired-token",
"refresh_token": "rt-expired",
"email": "expired@test.com",
"type": "claude",
"expired": "2020-01-01T00:00:00Z"
}`,
wantErr: false,
},
{
name: "token with invalid expiration format",
tokenJSON: `{
"access_token": "invalid-expire",
"refresh_token": "rt-invalid",
"email": "invalid@test.com",
"type": "claude",
"expired": "not-a-valid-date"
}`,
wantErr: false, // JSON parsing succeeds, date validation is separate
},
{
name: "token with Unix timestamp expiration (wrong format)",
tokenJSON: `{
"access_token": "unix-token",
"refresh_token": "rt-unix",
"email": "unix@test.com",
"type": "claude",
"expired": "1704067200"
}`,
wantErr: false, // JSON parsing succeeds
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var token ClaudeTokenStorage
err := json.Unmarshal([]byte(tt.tokenJSON), &token)
if (err != nil) != tt.wantErr {
t.Errorf("json.Unmarshal() error = %v, wantErr %v", err, tt.wantErr)
return
}
// Verify the token can be parsed even if expired
if !tt.wantErr && token.AccessToken == "" {
t.Error("AccessToken should not be empty after successful parse")
}
})
}
}
func TestToken_IsExpired_FreshToken(t *testing.T) {
tests := []struct {
name string
expireTime time.Time
wantExpired bool
}{
{
name: "token expiring in 1 hour",
expireTime: time.Now().Add(1 * time.Hour),
wantExpired: false,
},
{
name: "token expiring in 24 hours",
expireTime: time.Now().Add(24 * time.Hour),
wantExpired: false,
},
{
name: "token expiring in 1 minute",
expireTime: time.Now().Add(1 * time.Minute),
wantExpired: false,
},
{
name: "token expiring in 1 second",
expireTime: time.Now().Add(1 * time.Second),
wantExpired: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
expireStr := tt.expireTime.Format(time.RFC3339)
expired := isTokenExpired(expireStr)
if expired != tt.wantExpired {
t.Errorf("isTokenExpired() = %v, want %v", expired, tt.wantExpired)
}
})
}
}
func TestToken_IsExpired_ExpiredToken(t *testing.T) {
tests := []struct {
name string
expireTime time.Time
wantExpired bool
}{
{
name: "token expired 1 hour ago",
expireTime: time.Now().Add(-1 * time.Hour),
wantExpired: true,
},
{
name: "token expired 24 hours ago",
expireTime: time.Now().Add(-24 * time.Hour),
wantExpired: true,
},
{
name: "token expired 1 second ago",
expireTime: time.Now().Add(-1 * time.Second),
wantExpired: true,
},
{
name: "token expired in 2020",
expireTime: time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC),
wantExpired: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
expireStr := tt.expireTime.Format(time.RFC3339)
expired := isTokenExpired(expireStr)
if expired != tt.wantExpired {
t.Errorf("isTokenExpired() = %v, want %v", expired, tt.wantExpired)
}
})
}
}
func TestToken_NeedsRefresh_WithinLeadTime(t *testing.T) {
tests := []struct {
name string
expireTime time.Time
leadTime time.Duration
wantRefresh bool
}{
{
name: "token expiring in 5 minutes with 10 minute lead time",
expireTime: time.Now().Add(5 * time.Minute),
leadTime: 10 * time.Minute,
wantRefresh: true,
},
{
name: "token expiring in 30 seconds with 1 minute lead time",
expireTime: time.Now().Add(30 * time.Second),
leadTime: 1 * time.Minute,
wantRefresh: true,
},
{
name: "token expiring in 4 minutes with 5 minute lead time",
expireTime: time.Now().Add(4 * time.Minute),
leadTime: 5 * time.Minute,
wantRefresh: true,
},
{
name: "already expired token with any lead time",
expireTime: time.Now().Add(-1 * time.Minute),
leadTime: 5 * time.Minute,
wantRefresh: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
expireStr := tt.expireTime.Format(time.RFC3339)
needsRefresh := tokenNeedsRefresh(expireStr, tt.leadTime)
if needsRefresh != tt.wantRefresh {
t.Errorf("tokenNeedsRefresh() = %v, want %v", needsRefresh, tt.wantRefresh)
}
})
}
}
func TestToken_NeedsRefresh_NotYet(t *testing.T) {
tests := []struct {
name string
expireTime time.Time
leadTime time.Duration
wantRefresh bool
}{
{
name: "token expiring in 1 hour with 5 minute lead time",
expireTime: time.Now().Add(1 * time.Hour),
leadTime: 5 * time.Minute,
wantRefresh: false,
},
{
name: "token expiring in 10 minutes with 5 minute lead time",
expireTime: time.Now().Add(10 * time.Minute),
leadTime: 5 * time.Minute,
wantRefresh: false,
},
{
name: "token expiring in 24 hours with 1 hour lead time",
expireTime: time.Now().Add(24 * time.Hour),
leadTime: 1 * time.Hour,
wantRefresh: false,
},
{
name: "token expiring in 6 minutes with 5 minute lead time",
expireTime: time.Now().Add(6 * time.Minute),
leadTime: 5 * time.Minute,
wantRefresh: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
expireStr := tt.expireTime.Format(time.RFC3339)
needsRefresh := tokenNeedsRefresh(expireStr, tt.leadTime)
if needsRefresh != tt.wantRefresh {
t.Errorf("tokenNeedsRefresh() = %v, want %v", needsRefresh, tt.wantRefresh)
}
})
}
}
func TestToken_SaveTokenToFile(t *testing.T) {
tests := []struct {
name string
token ClaudeTokenStorage
wantErr bool
}{
{
name: "save valid token",
token: ClaudeTokenStorage{
IDToken: "test-id-token",
AccessToken: "test-access-token",
RefreshToken: "test-refresh-token",
LastRefresh: time.Now().Format(time.RFC3339),
Email: "test@example.com",
Type: "claude",
Expire: time.Now().Add(1 * time.Hour).Format(time.RFC3339),
},
wantErr: false,
},
{
name: "save token with empty fields",
token: ClaudeTokenStorage{
AccessToken: "minimal-token",
RefreshToken: "minimal-refresh",
Type: "claude",
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Create temp directory for test
tmpDir := t.TempDir()
filePath := filepath.Join(tmpDir, "test-token.json")
err := tt.token.SaveTokenToFile(filePath)
if (err != nil) != tt.wantErr {
t.Errorf("SaveTokenToFile() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !tt.wantErr {
// Verify file was created
if _, err := os.Stat(filePath); os.IsNotExist(err) {
t.Error("Token file was not created")
return
}
// Verify file contents can be parsed back
data, err := os.ReadFile(filePath)
if err != nil {
t.Errorf("Failed to read token file: %v", err)
return
}
var savedToken ClaudeTokenStorage
if err := json.Unmarshal(data, &savedToken); err != nil {
t.Errorf("Failed to parse saved token: %v", err)
return
}
// Verify key fields match
if savedToken.AccessToken != tt.token.AccessToken {
t.Errorf("AccessToken = %v, want %v", savedToken.AccessToken, tt.token.AccessToken)
}
if savedToken.Type != "claude" {
t.Errorf("Type = %v, want claude", savedToken.Type)
}
}
})
}
}
func TestToken_SaveTokenToFile_CreatesDirectory(t *testing.T) {
tmpDir := t.TempDir()
nestedPath := filepath.Join(tmpDir, "nested", "dir", "structure", "token.json")
token := ClaudeTokenStorage{
AccessToken: "test-token",
RefreshToken: "test-refresh",
Type: "claude",
}
err := token.SaveTokenToFile(nestedPath)
if err != nil {
t.Errorf("SaveTokenToFile() should create nested directories, got error: %v", err)
return
}
// Verify file exists
if _, err := os.Stat(nestedPath); os.IsNotExist(err) {
t.Error("Token file was not created in nested directory")
}
}
-6
View File
@@ -57,12 +57,6 @@ func (e *AuthenticationError) Error() string {
// Common authentication error types.
var (
// ErrTokenExpired = &AuthenticationError{
// Type: "token_expired",
// Message: "Access token has expired",
// Code: http.StatusUnauthorized,
// }
// ErrInvalidState represents an error for invalid OAuth state parameter.
ErrInvalidState = &AuthenticationError{
Type: "invalid_state",
+465
View File
@@ -0,0 +1,465 @@
package codex
import (
"encoding/base64"
"encoding/json"
"testing"
"time"
)
// createTestJWT creates a test JWT token with the given claims.
// It creates a valid 3-part JWT structure (header.payload.signature) for testing.
func createTestJWT(claims JWTClaims) string {
header := base64.URLEncoding.WithPadding(base64.NoPadding).EncodeToString([]byte(`{"alg":"RS256","typ":"JWT"}`))
claimsJSON, _ := json.Marshal(claims)
payload := base64.URLEncoding.WithPadding(base64.NoPadding).EncodeToString(claimsJSON)
signature := base64.URLEncoding.WithPadding(base64.NoPadding).EncodeToString([]byte("test-signature"))
return header + "." + payload + "." + signature
}
func TestParseJWTToken_ValidToken(t *testing.T) {
tests := []struct {
name string
claims JWTClaims
wantEmail string
wantAccountID string
wantIssuer string
wantSubject string
}{
{
name: "valid token with all fields",
claims: JWTClaims{
Email: "user@example.com",
EmailVerified: true,
Iss: "https://auth.openai.com/",
Sub: "auth0|user123",
Exp: int(time.Now().Add(time.Hour).Unix()),
Iat: int(time.Now().Unix()),
CodexAuthInfo: CodexAuthInfo{
ChatgptAccountID: "acct_abc123",
ChatgptUserID: "user_xyz789",
ChatgptPlanType: "plus",
UserID: "user-id-123",
},
},
wantEmail: "user@example.com",
wantAccountID: "acct_abc123",
wantIssuer: "https://auth.openai.com/",
wantSubject: "auth0|user123",
},
{
name: "valid token with minimal fields",
claims: JWTClaims{
Email: "minimal@example.com",
Exp: int(time.Now().Add(time.Hour).Unix()),
},
wantEmail: "minimal@example.com",
wantAccountID: "",
wantIssuer: "",
wantSubject: "",
},
{
name: "valid token with organization data",
claims: JWTClaims{
Email: "org-user@example.com",
Sub: "auth0|orguser456",
CodexAuthInfo: CodexAuthInfo{
ChatgptAccountID: "acct_org789",
Organizations: []Organizations{
{
ID: "org-id-1",
IsDefault: true,
Role: "admin",
Title: "My Organization",
},
},
},
},
wantEmail: "org-user@example.com",
wantAccountID: "acct_org789",
wantSubject: "auth0|orguser456",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
token := createTestJWT(tt.claims)
got, err := ParseJWTToken(token)
if err != nil {
t.Fatalf("ParseJWTToken() unexpected error = %v", err)
}
if got.Email != tt.wantEmail {
t.Errorf("ParseJWTToken() Email = %v, want %v", got.Email, tt.wantEmail)
}
if got.GetAccountID() != tt.wantAccountID {
t.Errorf("ParseJWTToken() AccountID = %v, want %v", got.GetAccountID(), tt.wantAccountID)
}
if got.Iss != tt.wantIssuer {
t.Errorf("ParseJWTToken() Issuer = %v, want %v", got.Iss, tt.wantIssuer)
}
if got.Sub != tt.wantSubject {
t.Errorf("ParseJWTToken() Subject = %v, want %v", got.Sub, tt.wantSubject)
}
})
}
}
func TestParseJWTToken_InvalidToken(t *testing.T) {
tests := []struct {
name string
token string
wantErr string
}{
{
name: "empty token",
token: "",
wantErr: "invalid JWT token format",
},
{
name: "single part token",
token: "singlepart",
wantErr: "invalid JWT token format",
},
{
name: "two part token",
token: "header.payload",
wantErr: "invalid JWT token format",
},
{
name: "four part token",
token: "header.payload.signature.extra",
wantErr: "invalid JWT token format",
},
{
name: "invalid base64 in payload",
token: "header.!!!invalid-base64!!!.signature",
wantErr: "failed to decode JWT claims",
},
{
name: "invalid JSON in payload",
token: "header." + base64.URLEncoding.WithPadding(base64.NoPadding).EncodeToString([]byte("not-json")) + ".signature",
wantErr: "failed to unmarshal JWT claims",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := ParseJWTToken(tt.token)
if err == nil {
t.Fatalf("ParseJWTToken() expected error containing %q, got nil", tt.wantErr)
}
if got != nil {
t.Errorf("ParseJWTToken() expected nil result on error, got %v", got)
}
if !containsSubstring(err.Error(), tt.wantErr) {
t.Errorf("ParseJWTToken() error = %v, want error containing %q", err, tt.wantErr)
}
})
}
}
func TestParseJWTToken_ExtractClaims(t *testing.T) {
tests := []struct {
name string
claims JWTClaims
checks func(t *testing.T, got *JWTClaims)
}{
{
name: "extract standard claims",
claims: JWTClaims{
Aud: []string{"app_test123"},
AtHash: "at_hash_value",
AuthTime: 1700000000,
Exp: 1700003600,
Iat: 1700000000,
Jti: "jti_unique_id",
Rat: 1699999900,
Sid: "session_id_123",
},
checks: func(t *testing.T, got *JWTClaims) {
if len(got.Aud) != 1 || got.Aud[0] != "app_test123" {
t.Errorf("Aud = %v, want [app_test123]", got.Aud)
}
if got.AtHash != "at_hash_value" {
t.Errorf("AtHash = %v, want at_hash_value", got.AtHash)
}
if got.AuthTime != 1700000000 {
t.Errorf("AuthTime = %v, want 1700000000", got.AuthTime)
}
if got.Jti != "jti_unique_id" {
t.Errorf("Jti = %v, want jti_unique_id", got.Jti)
}
if got.Sid != "session_id_123" {
t.Errorf("Sid = %v, want session_id_123", got.Sid)
}
},
},
{
name: "extract auth provider info",
claims: JWTClaims{
AuthProvider: "google-oauth2",
Email: "google@example.com",
CodexAuthInfo: CodexAuthInfo{
ChatgptPlanType: "team",
UserID: "user-google-123",
},
},
checks: func(t *testing.T, got *JWTClaims) {
if got.AuthProvider != "google-oauth2" {
t.Errorf("AuthProvider = %v, want google-oauth2", got.AuthProvider)
}
if got.CodexAuthInfo.ChatgptPlanType != "team" {
t.Errorf("ChatgptPlanType = %v, want team", got.CodexAuthInfo.ChatgptPlanType)
}
},
},
{
name: "extract multiple organizations",
claims: JWTClaims{
Email: "multi-org@example.com",
CodexAuthInfo: CodexAuthInfo{
Organizations: []Organizations{
{ID: "org-1", IsDefault: true, Role: "owner", Title: "Primary Org"},
{ID: "org-2", IsDefault: false, Role: "member", Title: "Secondary Org"},
},
},
},
checks: func(t *testing.T, got *JWTClaims) {
orgs := got.CodexAuthInfo.Organizations
if len(orgs) != 2 {
t.Fatalf("Organizations count = %v, want 2", len(orgs))
}
if orgs[0].ID != "org-1" || !orgs[0].IsDefault {
t.Errorf("First org = %+v, want org-1 as default", orgs[0])
}
if orgs[1].Role != "member" {
t.Errorf("Second org role = %v, want member", orgs[1].Role)
}
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
token := createTestJWT(tt.claims)
got, err := ParseJWTToken(token)
if err != nil {
t.Fatalf("ParseJWTToken() unexpected error = %v", err)
}
tt.checks(t, got)
})
}
}
func TestParseJWTToken_ValidateExpiry(t *testing.T) {
now := time.Now()
tests := []struct {
name string
expTime int
wantValid bool
}{
{
name: "token expires in 1 hour",
expTime: int(now.Add(time.Hour).Unix()),
wantValid: true,
},
{
name: "token expires in 1 minute",
expTime: int(now.Add(time.Minute).Unix()),
wantValid: true,
},
{
name: "token expired 1 hour ago",
expTime: int(now.Add(-time.Hour).Unix()),
wantValid: false,
},
{
name: "token expired 1 minute ago",
expTime: int(now.Add(-time.Minute).Unix()),
wantValid: false,
},
{
name: "token expires now",
expTime: int(now.Unix()),
wantValid: false,
},
{
name: "token with zero expiry",
expTime: 0,
wantValid: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
claims := JWTClaims{
Email: "test@example.com",
Exp: tt.expTime,
}
token := createTestJWT(claims)
got, err := ParseJWTToken(token)
if err != nil {
t.Fatalf("ParseJWTToken() unexpected error = %v", err)
}
// Check if token is valid based on expiry
isValid := time.Unix(int64(got.Exp), 0).After(time.Now())
if isValid != tt.wantValid {
t.Errorf("Token validity = %v, want %v (exp: %v, now: %v)",
isValid, tt.wantValid, time.Unix(int64(got.Exp), 0), time.Now())
}
})
}
}
func TestJWTClaims_GetUserEmail(t *testing.T) {
tests := []struct {
name string
claims JWTClaims
wantEmail string
}{
{
name: "standard email",
claims: JWTClaims{Email: "user@example.com"},
wantEmail: "user@example.com",
},
{
name: "empty email",
claims: JWTClaims{Email: ""},
wantEmail: "",
},
{
name: "email with subdomain",
claims: JWTClaims{Email: "user@mail.example.com"},
wantEmail: "user@mail.example.com",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.claims.GetUserEmail(); got != tt.wantEmail {
t.Errorf("GetUserEmail() = %v, want %v", got, tt.wantEmail)
}
})
}
}
func TestJWTClaims_GetAccountID(t *testing.T) {
tests := []struct {
name string
claims JWTClaims
wantAccountID string
}{
{
name: "with account ID",
claims: JWTClaims{
CodexAuthInfo: CodexAuthInfo{
ChatgptAccountID: "acct_12345",
},
},
wantAccountID: "acct_12345",
},
{
name: "empty account ID",
claims: JWTClaims{},
wantAccountID: "",
},
{
name: "account ID with special characters",
claims: JWTClaims{
CodexAuthInfo: CodexAuthInfo{
ChatgptAccountID: "acct_abc-123_xyz",
},
},
wantAccountID: "acct_abc-123_xyz",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.claims.GetAccountID(); got != tt.wantAccountID {
t.Errorf("GetAccountID() = %v, want %v", got, tt.wantAccountID)
}
})
}
}
func TestBase64URLDecode(t *testing.T) {
tests := []struct {
name string
input string
want string
wantErr bool
}{
{
name: "no padding needed",
input: "dGVzdA",
want: "test",
wantErr: false,
},
{
name: "one padding character needed",
input: "dGVzdDE",
want: "test1",
wantErr: false,
},
{
name: "two padding characters needed",
input: "dGVzdDEy",
want: "test12",
wantErr: false,
},
{
name: "empty string",
input: "",
want: "",
wantErr: false,
},
{
name: "URL-safe characters",
input: "YWJjLV8",
want: "abc-_",
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := base64URLDecode(tt.input)
if (err != nil) != tt.wantErr {
t.Errorf("base64URLDecode() error = %v, wantErr %v", err, tt.wantErr)
return
}
if string(got) != tt.want {
t.Errorf("base64URLDecode() = %v, want %v", string(got), tt.want)
}
})
}
}
// containsSubstring checks if s contains substr
func containsSubstring(s, substr string) bool {
return len(s) >= len(substr) && (s == substr || len(substr) == 0 ||
(len(s) > 0 && len(substr) > 0 && findSubstring(s, substr)))
}
func findSubstring(s, substr string) bool {
for i := 0; i <= len(s)-len(substr); i++ {
if s[i:i+len(substr)] == substr {
return true
}
}
return false
}
+558
View File
@@ -0,0 +1,558 @@
package codex
import (
"encoding/json"
"os"
"path/filepath"
"testing"
"time"
)
func TestCodexTokenStorage_Parse(t *testing.T) {
tests := []struct {
name string
jsonData string
wantErr bool
checkFields func(t *testing.T, ts *CodexTokenStorage)
}{
{
name: "parse complete token storage",
jsonData: `{
"id_token": "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJ1c2VyMTIzIn0.sig",
"access_token": "access_token_value",
"refresh_token": "refresh_token_value",
"account_id": "acct_abc123",
"last_refresh": "2024-01-15T10:30:00Z",
"email": "user@example.com",
"type": "codex",
"expired": "2024-01-15T11:30:00Z"
}`,
wantErr: false,
checkFields: func(t *testing.T, ts *CodexTokenStorage) {
if ts.IDToken != "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJ1c2VyMTIzIn0.sig" {
t.Errorf("IDToken mismatch")
}
if ts.AccessToken != "access_token_value" {
t.Errorf("AccessToken = %v, want access_token_value", ts.AccessToken)
}
if ts.RefreshToken != "refresh_token_value" {
t.Errorf("RefreshToken = %v, want refresh_token_value", ts.RefreshToken)
}
if ts.AccountID != "acct_abc123" {
t.Errorf("AccountID = %v, want acct_abc123", ts.AccountID)
}
if ts.Email != "user@example.com" {
t.Errorf("Email = %v, want user@example.com", ts.Email)
}
if ts.Type != "codex" {
t.Errorf("Type = %v, want codex", ts.Type)
}
},
},
{
name: "parse minimal token storage",
jsonData: `{
"access_token": "minimal_access",
"refresh_token": "minimal_refresh",
"type": "codex"
}`,
wantErr: false,
checkFields: func(t *testing.T, ts *CodexTokenStorage) {
if ts.AccessToken != "minimal_access" {
t.Errorf("AccessToken = %v, want minimal_access", ts.AccessToken)
}
if ts.IDToken != "" {
t.Errorf("IDToken = %v, want empty", ts.IDToken)
}
if ts.Email != "" {
t.Errorf("Email = %v, want empty", ts.Email)
}
},
},
{
name: "parse token with special characters in values",
jsonData: `{
"access_token": "token-with_special.chars/and+more",
"refresh_token": "refresh/token+value=",
"email": "user+test@sub.example.com",
"type": "codex"
}`,
wantErr: false,
checkFields: func(t *testing.T, ts *CodexTokenStorage) {
if ts.AccessToken != "token-with_special.chars/and+more" {
t.Errorf("AccessToken mismatch with special chars")
}
if ts.Email != "user+test@sub.example.com" {
t.Errorf("Email mismatch with special chars")
}
},
},
{
name: "parse empty JSON object",
jsonData: `{}`,
wantErr: false,
checkFields: func(t *testing.T, ts *CodexTokenStorage) {
if ts.Type != "" {
t.Errorf("Type = %v, want empty for empty JSON", ts.Type)
}
},
},
{
name: "invalid JSON",
jsonData: `{"invalid": json}`,
wantErr: true,
checkFields: func(t *testing.T, ts *CodexTokenStorage) {
// Not called on error
},
},
{
name: "malformed JSON - missing closing brace",
jsonData: `{"access_token": "test"`,
wantErr: true,
checkFields: func(t *testing.T, ts *CodexTokenStorage) {
// Not called on error
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var ts CodexTokenStorage
err := json.Unmarshal([]byte(tt.jsonData), &ts)
if (err != nil) != tt.wantErr {
t.Errorf("json.Unmarshal() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !tt.wantErr {
tt.checkFields(t, &ts)
}
})
}
}
func TestCodexTokenStorage_IsExpired(t *testing.T) {
tests := []struct {
name string
expireTime string
wantResult bool
}{
{
name: "token expired 1 hour ago",
expireTime: time.Now().Add(-time.Hour).Format(time.RFC3339),
wantResult: true,
},
{
name: "token expired 1 minute ago",
expireTime: time.Now().Add(-time.Minute).Format(time.RFC3339),
wantResult: true,
},
{
name: "token expires in 1 hour",
expireTime: time.Now().Add(time.Hour).Format(time.RFC3339),
wantResult: false,
},
{
name: "token expires in 1 minute",
expireTime: time.Now().Add(time.Minute).Format(time.RFC3339),
wantResult: false,
},
{
name: "token expires now",
expireTime: time.Now().Format(time.RFC3339),
wantResult: true, // Tokens that expire "now" are considered expired
},
{
name: "empty expire time",
expireTime: "",
wantResult: true, // Treat empty as expired
},
{
name: "invalid expire time format",
expireTime: "invalid-time",
wantResult: true, // Treat invalid as expired for safety
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ts := &CodexTokenStorage{
Expire: tt.expireTime,
}
// Test the expiry logic
isExpired := isTokenExpired(ts.Expire)
if isExpired != tt.wantResult {
t.Errorf("isTokenExpired() = %v, want %v (expire: %s)",
isExpired, tt.wantResult, tt.expireTime)
}
})
}
}
func TestCodexTokenStorage_NeedsRefresh(t *testing.T) {
tests := []struct {
name string
expireTime string
refreshBefore time.Duration // How long before expiry to trigger refresh
wantResult bool
}{
{
name: "token expires in 30 minutes, refresh at 5 min before",
expireTime: time.Now().Add(30 * time.Minute).Format(time.RFC3339),
refreshBefore: 5 * time.Minute,
wantResult: false,
},
{
name: "token expires in 3 minutes, refresh at 5 min before",
expireTime: time.Now().Add(3 * time.Minute).Format(time.RFC3339),
refreshBefore: 5 * time.Minute,
wantResult: true,
},
{
name: "token expires in 1 hour, refresh at 10 min before",
expireTime: time.Now().Add(time.Hour).Format(time.RFC3339),
refreshBefore: 10 * time.Minute,
wantResult: false,
},
{
name: "token expires in 5 minutes, refresh at 10 min before",
expireTime: time.Now().Add(5 * time.Minute).Format(time.RFC3339),
refreshBefore: 10 * time.Minute,
wantResult: true,
},
{
name: "token already expired",
expireTime: time.Now().Add(-time.Hour).Format(time.RFC3339),
refreshBefore: 5 * time.Minute,
wantResult: true,
},
{
name: "empty expire time",
expireTime: "",
refreshBefore: 5 * time.Minute,
wantResult: true,
},
{
name: "expires exactly at refresh threshold",
expireTime: time.Now().Add(5 * time.Minute).Format(time.RFC3339),
refreshBefore: 5 * time.Minute,
wantResult: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ts := &CodexTokenStorage{
Expire: tt.expireTime,
}
needsRefresh := tokenNeedsRefresh(ts.Expire, tt.refreshBefore)
if needsRefresh != tt.wantResult {
t.Errorf("tokenNeedsRefresh() = %v, want %v (expire: %s, threshold: %v)",
needsRefresh, tt.wantResult, tt.expireTime, tt.refreshBefore)
}
})
}
}
func TestCodexTokenStorage_SaveTokenToFile(t *testing.T) {
tests := []struct {
name string
storage CodexTokenStorage
wantErr bool
}{
{
name: "save complete token storage",
storage: CodexTokenStorage{
IDToken: "test_id_token",
AccessToken: "test_access_token",
RefreshToken: "test_refresh_token",
AccountID: "test_account_id",
LastRefresh: time.Now().Format(time.RFC3339),
Email: "test@example.com",
Type: "codex",
Expire: time.Now().Add(time.Hour).Format(time.RFC3339),
},
wantErr: false,
},
{
name: "save minimal token storage",
storage: CodexTokenStorage{
AccessToken: "minimal_token",
RefreshToken: "minimal_refresh",
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Create a temporary directory for the test
tmpDir := t.TempDir()
filePath := filepath.Join(tmpDir, "tokens", "codex_token.json")
err := tt.storage.SaveTokenToFile(filePath)
if (err != nil) != tt.wantErr {
t.Errorf("SaveTokenToFile() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !tt.wantErr {
// Verify the file was created
if _, err := os.Stat(filePath); os.IsNotExist(err) {
t.Errorf("SaveTokenToFile() file was not created at %s", filePath)
return
}
// Read and verify content
data, err := os.ReadFile(filePath)
if err != nil {
t.Errorf("Failed to read saved file: %v", err)
return
}
var loaded CodexTokenStorage
if err := json.Unmarshal(data, &loaded); err != nil {
t.Errorf("Failed to parse saved file: %v", err)
return
}
// Verify type is always set to "codex"
if loaded.Type != "codex" {
t.Errorf("Type = %v, want codex", loaded.Type)
}
// Verify key fields match
if loaded.AccessToken != tt.storage.AccessToken {
t.Errorf("AccessToken = %v, want %v", loaded.AccessToken, tt.storage.AccessToken)
}
if loaded.RefreshToken != tt.storage.RefreshToken {
t.Errorf("RefreshToken = %v, want %v", loaded.RefreshToken, tt.storage.RefreshToken)
}
}
})
}
}
func TestCodexTokenData_Parse(t *testing.T) {
tests := []struct {
name string
jsonData string
wantErr bool
checkFields func(t *testing.T, td *CodexTokenData)
}{
{
name: "parse complete token data",
jsonData: `{
"id_token": "jwt_id_token_here",
"access_token": "access_token_here",
"refresh_token": "refresh_token_here",
"account_id": "acct_123",
"email": "user@example.com",
"expired": "2024-01-15T12:00:00Z"
}`,
wantErr: false,
checkFields: func(t *testing.T, td *CodexTokenData) {
if td.IDToken != "jwt_id_token_here" {
t.Errorf("IDToken mismatch")
}
if td.AccessToken != "access_token_here" {
t.Errorf("AccessToken mismatch")
}
if td.RefreshToken != "refresh_token_here" {
t.Errorf("RefreshToken mismatch")
}
if td.AccountID != "acct_123" {
t.Errorf("AccountID mismatch")
}
if td.Email != "user@example.com" {
t.Errorf("Email mismatch")
}
},
},
{
name: "parse minimal token data",
jsonData: `{
"access_token": "only_access"
}`,
wantErr: false,
checkFields: func(t *testing.T, td *CodexTokenData) {
if td.AccessToken != "only_access" {
t.Errorf("AccessToken = %v, want only_access", td.AccessToken)
}
if td.RefreshToken != "" {
t.Errorf("RefreshToken should be empty")
}
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var td CodexTokenData
err := json.Unmarshal([]byte(tt.jsonData), &td)
if (err != nil) != tt.wantErr {
t.Errorf("json.Unmarshal() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !tt.wantErr {
tt.checkFields(t, &td)
}
})
}
}
func TestCodexAuthBundle_Parse(t *testing.T) {
tests := []struct {
name string
jsonData string
wantErr bool
checkFields func(t *testing.T, bundle *CodexAuthBundle)
}{
{
name: "parse complete auth bundle",
jsonData: `{
"api_key": "sk-proj-api-key",
"token_data": {
"id_token": "id_token_value",
"access_token": "access_token_value",
"refresh_token": "refresh_token_value",
"account_id": "acct_bundle",
"email": "bundle@example.com",
"expired": "2024-01-15T12:00:00Z"
},
"last_refresh": "2024-01-15T10:00:00Z"
}`,
wantErr: false,
checkFields: func(t *testing.T, bundle *CodexAuthBundle) {
if bundle.APIKey != "sk-proj-api-key" {
t.Errorf("APIKey mismatch")
}
if bundle.TokenData.AccessToken != "access_token_value" {
t.Errorf("TokenData.AccessToken mismatch")
}
if bundle.TokenData.Email != "bundle@example.com" {
t.Errorf("TokenData.Email mismatch")
}
if bundle.LastRefresh != "2024-01-15T10:00:00Z" {
t.Errorf("LastRefresh mismatch")
}
},
},
{
name: "parse bundle without api key",
jsonData: `{
"token_data": {
"access_token": "token_only"
},
"last_refresh": "2024-01-15T10:00:00Z"
}`,
wantErr: false,
checkFields: func(t *testing.T, bundle *CodexAuthBundle) {
if bundle.APIKey != "" {
t.Errorf("APIKey should be empty")
}
if bundle.TokenData.AccessToken != "token_only" {
t.Errorf("TokenData.AccessToken mismatch")
}
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var bundle CodexAuthBundle
err := json.Unmarshal([]byte(tt.jsonData), &bundle)
if (err != nil) != tt.wantErr {
t.Errorf("json.Unmarshal() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !tt.wantErr {
tt.checkFields(t, &bundle)
}
})
}
}
func TestPKCECodes_Parse(t *testing.T) {
tests := []struct {
name string
jsonData string
wantErr bool
checkFields func(t *testing.T, codes *PKCECodes)
}{
{
name: "parse valid PKCE codes",
jsonData: `{
"code_verifier": "dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk",
"code_challenge": "E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM"
}`,
wantErr: false,
checkFields: func(t *testing.T, codes *PKCECodes) {
if codes.CodeVerifier != "dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk" {
t.Errorf("CodeVerifier mismatch")
}
if codes.CodeChallenge != "E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM" {
t.Errorf("CodeChallenge mismatch")
}
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var codes PKCECodes
err := json.Unmarshal([]byte(tt.jsonData), &codes)
if (err != nil) != tt.wantErr {
t.Errorf("json.Unmarshal() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !tt.wantErr {
tt.checkFields(t, &codes)
}
})
}
}
// Helper functions for token expiry testing
// isTokenExpired checks if a token has expired based on its expire timestamp.
func isTokenExpired(expire string) bool {
if expire == "" {
return true
}
expireTime, err := time.Parse(time.RFC3339, expire)
if err != nil {
return true // Treat parse errors as expired for safety
}
return !expireTime.After(time.Now())
}
// tokenNeedsRefresh checks if a token should be refreshed based on its expiry
// and a threshold duration before expiry.
func tokenNeedsRefresh(expire string, threshold time.Duration) bool {
if expire == "" {
return true
}
expireTime, err := time.Parse(time.RFC3339, expire)
if err != nil {
return true
}
refreshTime := expireTime.Add(-threshold)
return !refreshTime.After(time.Now())
}
+150
View File
@@ -0,0 +1,150 @@
package cmd
import (
"fmt"
"os"
"os/exec"
)
// AgentInfo describes a detected CLI agent
type AgentInfo struct {
Name string
Detected bool
BinaryPath string
ConfigPath string
Version string
}
// DetectAgents checks for installed CLI agents and returns their status
func DetectAgents() []AgentInfo {
agents := []AgentInfo{
detectClaudeCode(),
detectCodex(),
detectDroid(),
detectGeminiCLI(),
}
return agents
}
// DoDetectAgents prints detected agents to console
func DoDetectAgents() {
fmt.Println("Detecting installed CLI agents...")
fmt.Println()
agents := DetectAgents()
detected := 0
for _, agent := range agents {
status := "[-] Not found"
if agent.Detected {
status = "[+] Installed"
detected++
}
fmt.Printf(" %-15s %s\n", agent.Name+":", status)
if agent.Detected {
if agent.BinaryPath != "" {
fmt.Printf(" %-15s %s\n", "", "Binary: "+agent.BinaryPath)
}
if agent.ConfigPath != "" {
fmt.Printf(" %-15s %s\n", "", "Config: "+agent.ConfigPath)
}
if agent.Version != "" {
fmt.Printf(" %-15s %s\n", "", "Version: "+agent.Version)
}
}
}
fmt.Println()
fmt.Printf("Found %d/%d agents installed.\n", detected, len(agents))
if detected > 0 {
fmt.Println()
fmt.Println("To configure an agent, run:")
fmt.Println(" --setup-claude Configure Claude Code")
fmt.Println(" --setup-codex Configure Codex CLI")
fmt.Println(" --setup-droid Configure Factory Droid")
}
}
func detectClaudeCode() AgentInfo {
info := AgentInfo{Name: "Claude Code"}
// Check for claude binary
if path, err := exec.LookPath("claude"); err == nil {
info.Detected = true
info.BinaryPath = path
}
// Check config file
configPath := expandPath("~/.claude/settings.json")
if fileExists(configPath) {
info.ConfigPath = configPath
info.Detected = true
}
return info
}
func detectCodex() AgentInfo {
info := AgentInfo{Name: "Codex CLI"}
// Check for codex binary
if path, err := exec.LookPath("codex"); err == nil {
info.Detected = true
info.BinaryPath = path
}
// Check config directory
configDir := expandPath("~/.codex")
if dirExists(configDir) {
info.ConfigPath = configDir
info.Detected = true
}
return info
}
func detectDroid() AgentInfo {
info := AgentInfo{Name: "Factory Droid"}
// Check for droid or factory binary
if path, err := exec.LookPath("droid"); err == nil {
info.Detected = true
info.BinaryPath = path
} else if path, err := exec.LookPath("factory"); err == nil {
info.Detected = true
info.BinaryPath = path
}
// Check config file
configPath := expandPath("~/.factory/config.json")
if fileExists(configPath) {
info.ConfigPath = configPath
info.Detected = true
}
return info
}
func detectGeminiCLI() AgentInfo {
info := AgentInfo{Name: "Gemini CLI"}
// Check for gemini binary
if path, err := exec.LookPath("gemini"); err == nil {
info.Detected = true
info.BinaryPath = path
}
return info
}
func fileExists(path string) bool {
info, err := os.Stat(path)
return err == nil && !info.IsDir()
}
func dirExists(path string) bool {
info, err := os.Stat(path)
return err == nil && info.IsDir()
}
+202
View File
@@ -0,0 +1,202 @@
package cmd
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/router-for-me/CLIProxyAPI/v6/internal/config"
)
// DoSetupClaude generates Claude Code configuration
func DoSetupClaude(cfg *config.Config) {
port := cfg.Port
if port == 0 {
port = 8317
}
fmt.Println("Configuring Claude Code for ProxyPilot...")
fmt.Println()
// Generate settings.json content
settingsPath := expandPath("~/.claude/settings.json")
settings := map[string]any{
"env": map[string]string{
"ANTHROPIC_BASE_URL": fmt.Sprintf("http://127.0.0.1:%d", port),
"ANTHROPIC_AUTH_TOKEN": "proxypal-local",
},
}
// Try to merge with existing settings
if existing := readJSONFile(settingsPath); existing != nil {
if env, ok := settings["env"].(map[string]string); ok {
if existingEnv, ok := existing["env"].(map[string]any); ok {
for k, v := range existingEnv {
if _, overwritten := env[k]; !overwritten {
if vs, ok := v.(string); ok {
env[k] = vs
}
}
}
}
}
}
// Ensure directory exists
os.MkdirAll(filepath.Dir(settingsPath), 0755)
// Write settings
if err := writeJSONFile(settingsPath, settings); err != nil {
fmt.Printf("Error writing %s: %v\n", settingsPath, err)
return
}
fmt.Printf("Updated %s\n", settingsPath)
fmt.Println()
fmt.Println("Claude Code is now configured to use ProxyPilot.")
fmt.Printf("Restart Claude Code to apply changes.\n")
}
// DoSetupCodex generates Codex CLI configuration
func DoSetupCodex(cfg *config.Config) {
port := cfg.Port
if port == 0 {
port = 8317
}
fmt.Println("Configuring Codex CLI for ProxyPilot...")
fmt.Println()
configDir := expandPath("~/.codex")
os.MkdirAll(configDir, 0755)
// Create config.toml
configPath := filepath.Join(configDir, "config.toml")
configContent := fmt.Sprintf(`# Generated by ProxyPilot
model_provider = "cliproxyapi"
base_url = "http://127.0.0.1:%d/v1"
`, port)
if err := os.WriteFile(configPath, []byte(configContent), 0644); err != nil {
fmt.Printf("Error writing %s: %v\n", configPath, err)
return
}
fmt.Printf("Created %s\n", configPath)
// Create auth.json
authPath := filepath.Join(configDir, "auth.json")
authContent := `{"OPENAI_API_KEY": "proxypal-local"}`
if err := os.WriteFile(authPath, []byte(authContent), 0644); err != nil {
fmt.Printf("Error writing %s: %v\n", authPath, err)
return
}
fmt.Printf("Created %s\n", authPath)
fmt.Println()
fmt.Println("Codex CLI is now configured to use ProxyPilot.")
}
// DoSetupDroid generates Factory Droid configuration
func DoSetupDroid(cfg *config.Config) {
port := cfg.Port
if port == 0 {
port = 8317
}
fmt.Println("Configuring Factory Droid for ProxyPilot...")
fmt.Println()
configPath := expandPath("~/.factory/config.json")
os.MkdirAll(filepath.Dir(configPath), 0755)
// Read existing config or create new
var droidConfig map[string]any
if existing := readJSONFile(configPath); existing != nil {
droidConfig = existing
} else {
droidConfig = make(map[string]any)
}
// Build custom_models array
customModels := []map[string]any{
{
"name": "proxypal-claude-opus",
"base_url": fmt.Sprintf("http://127.0.0.1:%d/v1", port),
"api_key": "proxypal-local",
"model": "claude-opus-4-5-20251101",
},
{
"name": "proxypal-claude-sonnet",
"base_url": fmt.Sprintf("http://127.0.0.1:%d/v1", port),
"api_key": "proxypal-local",
"model": "claude-sonnet-4-5-20250929",
},
{
"name": "proxypal-claude-haiku",
"base_url": fmt.Sprintf("http://127.0.0.1:%d/v1", port),
"api_key": "proxypal-local",
"model": "claude-haiku-4-5-20251001",
},
}
// Merge with existing custom_models, preserving non-proxypal entries
if existing, ok := droidConfig["custom_models"].([]any); ok {
for _, entry := range existing {
if m, ok := entry.(map[string]any); ok {
name, _ := m["name"].(string)
if !strings.HasPrefix(name, "proxypal-") {
customModels = append(customModels, m)
}
}
}
}
droidConfig["custom_models"] = customModels
if err := writeJSONFile(configPath, droidConfig); err != nil {
fmt.Printf("Error writing %s: %v\n", configPath, err)
return
}
fmt.Printf("Updated %s\n", configPath)
fmt.Println()
fmt.Println("Factory Droid is now configured with ProxyPilot models:")
fmt.Println(" - proxypal-claude-opus")
fmt.Println(" - proxypal-claude-sonnet")
fmt.Println(" - proxypal-claude-haiku")
}
func expandPath(path string) string {
if strings.HasPrefix(path, "~/") {
home, err := os.UserHomeDir()
if err != nil {
return path
}
return filepath.Join(home, path[2:])
}
return path
}
func readJSONFile(path string) map[string]any {
data, err := os.ReadFile(path)
if err != nil {
return nil
}
var result map[string]any
if err := json.Unmarshal(data, &result); err != nil {
return nil
}
return result
}
func writeJSONFile(path string, data any) error {
content, err := json.MarshalIndent(data, "", " ")
if err != nil {
return err
}
return os.WriteFile(path, content, 0644)
}
-4
View File
@@ -12,7 +12,6 @@ import (
"github.com/router-for-me/CLIProxyAPI/v6/internal/api"
"github.com/router-for-me/CLIProxyAPI/v6/internal/config"
"github.com/router-for-me/CLIProxyAPI/v6/internal/letta"
"github.com/router-for-me/CLIProxyAPI/v6/sdk/cliproxy"
log "github.com/sirupsen/logrus"
)
@@ -34,9 +33,6 @@ func StartService(cfg *config.Config, configPath string, localPassword string) {
ctxSignal, cancel := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
defer cancel()
// Start the Letta sidecar server
letta.StartServer(ctxSignal)
runCtx := ctxSignal
if localPassword != "" {
var keepAliveCancel context.CancelFunc
+105 -26
View File
@@ -99,6 +99,10 @@ type Config struct {
// OAuthExcludedModels defines per-provider global model exclusions applied to OAuth/file-backed auth entries.
OAuthExcludedModels map[string][]string `yaml:"oauth-excluded-models,omitempty" json:"oauth-excluded-models,omitempty"`
// GlobalModelMappings defines model aliases that apply across all providers.
// These are checked before per-credential model mappings.
GlobalModelMappings []GlobalModelMapping `yaml:"global-model-mappings,omitempty" json:"global-model-mappings,omitempty"`
// Payload defines default and override rules for provider payload parameters.
Payload PayloadConfig `yaml:"payload" json:"payload"`
@@ -150,6 +154,28 @@ type RoutingConfig struct {
Strategy string `yaml:"strategy,omitempty" json:"strategy,omitempty"`
}
// GlobalModelMapping defines a model alias that applies globally across all providers.
// This allows routing friendly names like "smart" to specific models like "claude-opus-4-5-20251101".
type GlobalModelMapping struct {
// From is the client-requested model name (e.g., "smart", "rush", "opus")
From string `yaml:"from" json:"from"`
// To is the actual upstream model name (e.g., "claude-opus-4-5-20251101")
To string `yaml:"to" json:"to"`
// Provider optionally restricts this mapping to a specific provider (e.g., "claude", "codex")
// If empty, the mapping applies to all providers.
Provider string `yaml:"provider,omitempty" json:"provider,omitempty"`
// Enabled allows disabling a mapping without removing it
Enabled *bool `yaml:"enabled,omitempty" json:"enabled,omitempty"`
}
// IsEnabled returns whether the mapping is enabled (defaults to true)
func (m *GlobalModelMapping) IsEnabled() bool {
if m.Enabled == nil {
return true
}
return *m.Enabled
}
// PayloadConfig defines default and override parameter rules applied to provider payloads.
type PayloadConfig struct {
// Default defines rules that only set parameters when they are missing in the payload.
@@ -225,6 +251,9 @@ type CodexKey struct {
// ProxyURL overrides the global proxy setting for this API key if provided.
ProxyURL string `yaml:"proxy-url" json:"proxy-url"`
// Models defines upstream model names and aliases for request routing.
Models []CodexModel `yaml:"models" json:"models"`
// Headers optionally adds extra HTTP headers for requests sent with this key.
Headers map[string]string `yaml:"headers,omitempty" json:"headers,omitempty"`
@@ -232,6 +261,15 @@ type CodexKey struct {
ExcludedModels []string `yaml:"excluded-models,omitempty" json:"excluded-models,omitempty"`
}
// CodexModel describes a mapping between an alias and the actual upstream model name.
type CodexModel struct {
// Name is the upstream model identifier used when issuing requests.
Name string `yaml:"name" json:"name"`
// Alias is the client-facing model name that maps to Name.
Alias string `yaml:"alias" json:"alias"`
}
// GeminiKey represents the configuration for a Gemini API key,
// including optional overrides for upstream base URL, proxy routing, and headers.
type GeminiKey struct {
@@ -446,6 +484,9 @@ func LoadConfigOptional(configFile string, optional bool) (*Config, error) {
}
}
// Wire up the global model mapper hook so SDK handlers can use it.
cfg.SDKConfig.GlobalModelMapper = cfg.LookupGlobalModelMapping
// Return the populated configuration struct.
return &cfg, nil
}
@@ -694,6 +735,28 @@ func NormalizeOAuthExcludedModels(entries map[string][]string) map[string][]stri
return out
}
// LookupGlobalModelMapping returns the mapped model name if a global mapping exists for the given model.
// Returns empty string if no mapping found.
func (cfg *Config) LookupGlobalModelMapping(model string, provider string) string {
if cfg == nil {
return ""
}
modelLower := strings.ToLower(model)
for _, m := range cfg.GlobalModelMappings {
if !m.IsEnabled() {
continue
}
if strings.EqualFold(m.From, modelLower) {
// Check provider restriction if set
if m.Provider != "" && !strings.EqualFold(m.Provider, provider) {
continue
}
return m.To
}
}
return ""
}
// hashSecret hashes the given secret using bcrypt.
func hashSecret(secret string) (string, error) {
// Use default cost for simplicity.
@@ -878,8 +941,8 @@ func getOrCreateMapValue(mapNode *yaml.Node, key string) *yaml.Node {
}
// mergeMappingPreserve merges keys from src into dst mapping node while preserving
// key order and comments of existing keys in dst. Unknown keys from src are appended
// to dst at the end, copying their node structure from src.
// key order and comments of existing keys in dst. New keys are only added if their
// value is non-zero to avoid polluting the config with defaults.
func mergeMappingPreserve(dst, src *yaml.Node) {
if dst == nil || src == nil {
return
@@ -890,20 +953,19 @@ func mergeMappingPreserve(dst, src *yaml.Node) {
copyNodeShallow(dst, src)
return
}
// Build a lookup of existing keys in dst
for i := 0; i+1 < len(src.Content); i += 2 {
sk := src.Content[i]
sv := src.Content[i+1]
idx := findMapKeyIndex(dst, sk.Value)
if idx >= 0 {
// Merge into existing value node
// Merge into existing value node (always update, even to zero values)
dv := dst.Content[idx+1]
mergeNodePreserve(dv, sv)
} else {
if shouldSkipEmptyCollectionOnPersist(sk.Value, sv) {
// New key: only add if value is non-zero to avoid polluting config with defaults
if isZeroValueNode(sv) {
continue
}
// Append new key/value pair by deep-copying from src
dst.Content = append(dst.Content, deepCopyNode(sk), deepCopyNode(sv))
}
}
@@ -986,32 +1048,49 @@ func findMapKeyIndex(mapNode *yaml.Node, key string) int {
return -1
}
func shouldSkipEmptyCollectionOnPersist(key string, node *yaml.Node) bool {
switch key {
case "generative-language-api-key",
"gemini-api-key",
"vertex-api-key",
"claude-api-key",
"codex-api-key",
"openai-compatibility":
return isEmptyCollectionNode(node)
default:
return false
}
}
func isEmptyCollectionNode(node *yaml.Node) bool {
// isZeroValueNode returns true if the YAML node represents a zero/default value
// that should not be written as a new key to preserve config cleanliness.
// For mappings and sequences, recursively checks if all children are zero values.
func isZeroValueNode(node *yaml.Node) bool {
if node == nil {
return true
}
switch node.Kind {
case yaml.SequenceNode:
return len(node.Content) == 0
case yaml.ScalarNode:
return node.Tag == "!!null"
default:
return false
switch node.Tag {
case "!!bool":
return node.Value == "false"
case "!!int", "!!float":
return node.Value == "0" || node.Value == "0.0"
case "!!str":
return node.Value == ""
case "!!null":
return true
}
case yaml.SequenceNode:
if len(node.Content) == 0 {
return true
}
// Check if all elements are zero values
for _, child := range node.Content {
if !isZeroValueNode(child) {
return false
}
}
return true
case yaml.MappingNode:
if len(node.Content) == 0 {
return true
}
// Check if all values are zero values (values are at odd indices)
for i := 1; i < len(node.Content); i += 2 {
if !isZeroValueNode(node.Content[i]) {
return false
}
}
return true
}
return false
}
// deepCopyNode creates a deep copy of a yaml.Node graph.
+894
View File
@@ -0,0 +1,894 @@
package config
import (
"os"
"path/filepath"
"testing"
)
func TestLoadConfig_ValidYAML(t *testing.T) {
tests := []struct {
name string
yaml string
wantPort int
wantHost string
wantErr bool
}{
{
name: "minimal valid config",
yaml: `
port: 8080
`,
wantPort: 8080,
wantHost: "",
wantErr: false,
},
{
name: "config with host and port",
yaml: `
host: 127.0.0.1
port: 9000
`,
wantPort: 9000,
wantHost: "127.0.0.1",
wantErr: false,
},
{
name: "config with debug enabled",
yaml: `
port: 8080
debug: true
`,
wantPort: 8080,
wantHost: "",
wantErr: false,
},
{
name: "config with gemini keys",
yaml: `
port: 8080
gemini-api-key:
- api-key: "test-key-1"
- api-key: "test-key-2"
`,
wantPort: 8080,
wantHost: "",
wantErr: false,
},
{
name: "config with tls settings",
yaml: `
port: 443
tls:
enable: true
cert: /path/to/cert.pem
key: /path/to/key.pem
`,
wantPort: 443,
wantHost: "",
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tmpDir := t.TempDir()
configPath := filepath.Join(tmpDir, "config.yaml")
if err := os.WriteFile(configPath, []byte(tt.yaml), 0644); err != nil {
t.Fatalf("failed to write test config: %v", err)
}
cfg, err := LoadConfig(configPath)
if (err != nil) != tt.wantErr {
t.Errorf("LoadConfig() error = %v, wantErr %v", err, tt.wantErr)
return
}
if err != nil {
return
}
if cfg.Port != tt.wantPort {
t.Errorf("LoadConfig() Port = %v, want %v", cfg.Port, tt.wantPort)
}
if cfg.Host != tt.wantHost {
t.Errorf("LoadConfig() Host = %v, want %v", cfg.Host, tt.wantHost)
}
})
}
}
func TestLoadConfig_EmptyFile(t *testing.T) {
tests := []struct {
name string
content string
optional bool
wantErr bool
}{
{
name: "empty file with optional false",
content: "",
optional: false,
wantErr: false, // Empty file parses to zero-value Config
},
{
name: "empty file with optional true",
content: "",
optional: true,
wantErr: false,
},
{
name: "whitespace only with optional false",
content: " \n \n ",
optional: false,
wantErr: false,
},
{
name: "whitespace only with optional true",
content: " \n \n ",
optional: true,
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tmpDir := t.TempDir()
configPath := filepath.Join(tmpDir, "config.yaml")
if err := os.WriteFile(configPath, []byte(tt.content), 0644); err != nil {
t.Fatalf("failed to write test config: %v", err)
}
cfg, err := LoadConfigOptional(configPath, tt.optional)
if (err != nil) != tt.wantErr {
t.Errorf("LoadConfigOptional() error = %v, wantErr %v", err, tt.wantErr)
return
}
if err == nil && cfg == nil {
t.Error("LoadConfigOptional() returned nil config without error")
}
})
}
}
func TestLoadConfig_InvalidYAML(t *testing.T) {
tests := []struct {
name string
content string
optional bool
wantErr bool
}{
{
name: "invalid yaml syntax",
content: `
port: 8080
invalid indentation
`,
optional: false,
wantErr: true,
},
{
name: "invalid yaml with optional true",
content: `
port: 8080
invalid indentation
`,
optional: true,
wantErr: false, // Optional mode returns empty config on parse error
},
{
name: "malformed yaml structure",
content: `
port: [8080
`,
optional: false,
wantErr: true,
},
{
name: "duplicate keys at same level",
content: "port: 8080\nport: 9090\n - invalid",
optional: false,
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tmpDir := t.TempDir()
configPath := filepath.Join(tmpDir, "config.yaml")
if err := os.WriteFile(configPath, []byte(tt.content), 0644); err != nil {
t.Fatalf("failed to write test config: %v", err)
}
cfg, err := LoadConfigOptional(configPath, tt.optional)
if (err != nil) != tt.wantErr {
t.Errorf("LoadConfigOptional() error = %v, wantErr %v", err, tt.wantErr)
return
}
if tt.optional && err == nil && cfg == nil {
t.Error("LoadConfigOptional() with optional=true returned nil config")
}
})
}
}
func TestLoadConfig_MissingFile(t *testing.T) {
tests := []struct {
name string
optional bool
wantErr bool
}{
{
name: "missing file with optional false",
optional: false,
wantErr: true,
},
{
name: "missing file with optional true",
optional: true,
wantErr: false, // Optional mode returns empty config for missing file
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tmpDir := t.TempDir()
configPath := filepath.Join(tmpDir, "nonexistent.yaml")
cfg, err := LoadConfigOptional(configPath, tt.optional)
if (err != nil) != tt.wantErr {
t.Errorf("LoadConfigOptional() error = %v, wantErr %v", err, tt.wantErr)
return
}
if tt.optional && cfg == nil {
t.Error("LoadConfigOptional() with optional=true returned nil config for missing file")
}
})
}
}
func TestValidateConfig_ValidPort(t *testing.T) {
tests := []struct {
name string
port int
wantErr bool
}{
{
name: "minimum valid port",
port: 1,
wantErr: false,
},
{
name: "maximum valid port",
port: 65535,
wantErr: false,
},
{
name: "common port 80",
port: 80,
wantErr: false,
},
{
name: "common port 443",
port: 443,
wantErr: false,
},
{
name: "common port 8080",
port: 8080,
wantErr: false,
},
{
name: "high ephemeral port",
port: 49152,
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cfg := &Config{Port: tt.port}
_, err := ValidateConfig(cfg)
if (err != nil) != tt.wantErr {
t.Errorf("ValidateConfig() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func TestValidateConfig_InvalidPort(t *testing.T) {
tests := []struct {
name string
port int
wantErr bool
}{
{
name: "zero port",
port: 0,
wantErr: true,
},
{
name: "negative port",
port: -1,
wantErr: true,
},
{
name: "port exceeds maximum",
port: 65536,
wantErr: true,
},
{
name: "large negative port",
port: -65536,
wantErr: true,
},
{
name: "very large port",
port: 100000,
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cfg := &Config{Port: tt.port}
_, err := ValidateConfig(cfg)
if (err != nil) != tt.wantErr {
t.Errorf("ValidateConfig() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func TestSanitizeGeminiKeys_RemovesDuplicates(t *testing.T) {
tests := []struct {
name string
keys []GeminiKey
wantCount int
}{
{
name: "duplicate api keys",
keys: []GeminiKey{
{APIKey: "key-1"},
{APIKey: "key-1"},
{APIKey: "key-2"},
},
wantCount: 2,
},
{
name: "all unique keys",
keys: []GeminiKey{
{APIKey: "key-1"},
{APIKey: "key-2"},
{APIKey: "key-3"},
},
wantCount: 3,
},
{
name: "all same keys",
keys: []GeminiKey{
{APIKey: "same-key"},
{APIKey: "same-key"},
{APIKey: "same-key"},
},
wantCount: 1,
},
{
name: "empty and duplicate",
keys: []GeminiKey{
{APIKey: ""},
{APIKey: "key-1"},
{APIKey: ""},
{APIKey: "key-1"},
},
wantCount: 1,
},
{
name: "empty slice",
keys: []GeminiKey{},
wantCount: 0,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cfg := &Config{GeminiKey: tt.keys}
cfg.SanitizeGeminiKeys()
if len(cfg.GeminiKey) != tt.wantCount {
t.Errorf("SanitizeGeminiKeys() count = %v, want %v", len(cfg.GeminiKey), tt.wantCount)
}
})
}
}
func TestSanitizeGeminiKeys_TrimsWhitespace(t *testing.T) {
tests := []struct {
name string
keys []GeminiKey
wantAPIKeys []string
}{
{
name: "leading and trailing spaces",
keys: []GeminiKey{
{APIKey: " key-1 "},
{APIKey: "\tkey-2\t"},
},
wantAPIKeys: []string{"key-1", "key-2"},
},
{
name: "mixed whitespace",
keys: []GeminiKey{
{APIKey: " \t key-1 \n "},
},
wantAPIKeys: []string{"key-1"},
},
{
name: "spaces in base-url",
keys: []GeminiKey{
{APIKey: "key-1", BaseURL: " https://api.example.com "},
},
wantAPIKeys: []string{"key-1"},
},
{
name: "whitespace only key gets removed",
keys: []GeminiKey{
{APIKey: " "},
{APIKey: "valid-key"},
},
wantAPIKeys: []string{"valid-key"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cfg := &Config{GeminiKey: tt.keys}
cfg.SanitizeGeminiKeys()
if len(cfg.GeminiKey) != len(tt.wantAPIKeys) {
t.Errorf("SanitizeGeminiKeys() count = %v, want %v", len(cfg.GeminiKey), len(tt.wantAPIKeys))
return
}
for i, want := range tt.wantAPIKeys {
if cfg.GeminiKey[i].APIKey != want {
t.Errorf("SanitizeGeminiKeys() key[%d] = %q, want %q", i, cfg.GeminiKey[i].APIKey, want)
}
}
})
}
}
func TestSanitizeClaudeKeys_NormalizesHeaders(t *testing.T) {
tests := []struct {
name string
keys []ClaudeKey
wantHeaders []map[string]string
}{
{
name: "trims header keys and values",
keys: []ClaudeKey{
{
APIKey: "key-1",
Headers: map[string]string{
" X-Custom-Header ": " value1 ",
"Authorization": "Bearer token",
},
},
},
wantHeaders: []map[string]string{
{
"X-Custom-Header": "value1",
"Authorization": "Bearer token",
},
},
},
{
name: "removes empty key headers",
keys: []ClaudeKey{
{
APIKey: "key-1",
Headers: map[string]string{
"": "value",
" ": "value2",
"X-Header": "value3",
},
},
},
wantHeaders: []map[string]string{
{
"X-Header": "value3",
},
},
},
{
name: "removes empty value headers",
keys: []ClaudeKey{
{
APIKey: "key-1",
Headers: map[string]string{
"X-Header": "",
"X-Other": " ",
"X-Valid": "value",
},
},
},
wantHeaders: []map[string]string{
{
"X-Valid": "value",
},
},
},
{
name: "nil headers stays nil",
keys: []ClaudeKey{
{APIKey: "key-1", Headers: nil},
},
wantHeaders: []map[string]string{nil},
},
{
name: "all empty headers becomes nil",
keys: []ClaudeKey{
{
APIKey: "key-1",
Headers: map[string]string{
"": "",
" ": " ",
},
},
},
wantHeaders: []map[string]string{nil},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cfg := &Config{ClaudeKey: tt.keys}
cfg.SanitizeClaudeKeys()
if len(cfg.ClaudeKey) != len(tt.wantHeaders) {
t.Fatalf("SanitizeClaudeKeys() count = %v, want %v", len(cfg.ClaudeKey), len(tt.wantHeaders))
}
for i, wantHeader := range tt.wantHeaders {
gotHeader := cfg.ClaudeKey[i].Headers
if wantHeader == nil {
if gotHeader != nil {
t.Errorf("SanitizeClaudeKeys() headers[%d] = %v, want nil", i, gotHeader)
}
continue
}
if len(gotHeader) != len(wantHeader) {
t.Errorf("SanitizeClaudeKeys() headers[%d] len = %v, want %v", i, len(gotHeader), len(wantHeader))
continue
}
for k, v := range wantHeader {
if gotHeader[k] != v {
t.Errorf("SanitizeClaudeKeys() headers[%d][%q] = %q, want %q", i, k, gotHeader[k], v)
}
}
}
})
}
}
func TestNormalizeHeaders_TrimsEmptyKeys(t *testing.T) {
tests := []struct {
name string
headers map[string]string
want map[string]string
}{
{
name: "trims all keys and values",
headers: map[string]string{
" Key1 ": " Value1 ",
"\tKey2\t": "\tValue2\t",
},
want: map[string]string{
"Key1": "Value1",
"Key2": "Value2",
},
},
{
name: "removes empty keys",
headers: map[string]string{
"": "value",
"key": "value",
" ": "value",
"\t\n": "value",
},
want: map[string]string{
"key": "value",
},
},
{
name: "removes empty values",
headers: map[string]string{
"key1": "",
"key2": "value",
"key3": " ",
},
want: map[string]string{
"key2": "value",
},
},
{
name: "nil input returns nil",
headers: nil,
want: nil,
},
{
name: "empty map returns nil",
headers: map[string]string{},
want: nil,
},
{
name: "all empty entries returns nil",
headers: map[string]string{
"": "",
" ": " ",
"\t": "\n",
" ": "",
},
want: nil,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := NormalizeHeaders(tt.headers)
if tt.want == nil {
if got != nil {
t.Errorf("NormalizeHeaders() = %v, want nil", got)
}
return
}
if len(got) != len(tt.want) {
t.Errorf("NormalizeHeaders() len = %v, want %v", len(got), len(tt.want))
return
}
for k, v := range tt.want {
if got[k] != v {
t.Errorf("NormalizeHeaders()[%q] = %q, want %q", k, got[k], v)
}
}
})
}
}
func TestNormalizeExcludedModels_Deduplicates(t *testing.T) {
tests := []struct {
name string
models []string
want []string
}{
{
name: "removes duplicates",
models: []string{"model-a", "model-b", "model-a", "model-c", "model-b"},
want: []string{"model-a", "model-b", "model-c"},
},
{
name: "case insensitive deduplication",
models: []string{"Model-A", "model-a", "MODEL-A"},
want: []string{"model-a"},
},
{
name: "trims whitespace before deduplication",
models: []string{" model-a ", "model-a", " model-a"},
want: []string{"model-a"},
},
{
name: "removes empty entries",
models: []string{"", "model-a", " ", "model-b", "\t"},
want: []string{"model-a", "model-b"},
},
{
name: "preserves order of first occurrence",
models: []string{"c-model", "a-model", "b-model", "a-model"},
want: []string{"c-model", "a-model", "b-model"},
},
{
name: "nil input returns nil",
models: nil,
want: nil,
},
{
name: "empty slice returns nil",
models: []string{},
want: nil,
},
{
name: "all empty entries returns nil",
models: []string{"", " ", "\t"},
want: nil,
},
{
name: "lowercases all models",
models: []string{"GPT-4", "Claude-3", "GEMINI-PRO"},
want: []string{"gpt-4", "claude-3", "gemini-pro"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := NormalizeExcludedModels(tt.models)
if tt.want == nil {
if got != nil {
t.Errorf("NormalizeExcludedModels() = %v, want nil", got)
}
return
}
if len(got) != len(tt.want) {
t.Errorf("NormalizeExcludedModels() len = %v, want %v; got = %v", len(got), len(tt.want), got)
return
}
for i, want := range tt.want {
if got[i] != want {
t.Errorf("NormalizeExcludedModels()[%d] = %q, want %q", i, got[i], want)
}
}
})
}
}
func TestLookupGlobalModelMapping_ExactMatch(t *testing.T) {
boolPtr := func(b bool) *bool { return &b }
tests := []struct {
name string
mappings []GlobalModelMapping
model string
provider string
want string
}{
{
name: "exact match found",
mappings: []GlobalModelMapping{
{From: "smart", To: "claude-opus-4-5-20251101"},
{From: "fast", To: "claude-sonnet-4-5-20251101"},
},
model: "smart",
provider: "",
want: "claude-opus-4-5-20251101",
},
{
name: "case insensitive match",
mappings: []GlobalModelMapping{
{From: "SMART", To: "claude-opus-4-5-20251101"},
},
model: "smart",
provider: "",
want: "claude-opus-4-5-20251101",
},
{
name: "no match returns empty",
mappings: []GlobalModelMapping{
{From: "smart", To: "claude-opus-4-5-20251101"},
},
model: "unknown",
provider: "",
want: "",
},
{
name: "empty mappings returns empty",
mappings: []GlobalModelMapping{},
model: "smart",
provider: "",
want: "",
},
{
name: "provider restriction matches",
mappings: []GlobalModelMapping{
{From: "smart", To: "claude-opus-4-5-20251101", Provider: "claude"},
},
model: "smart",
provider: "claude",
want: "claude-opus-4-5-20251101",
},
{
name: "provider restriction does not match",
mappings: []GlobalModelMapping{
{From: "smart", To: "claude-opus-4-5-20251101", Provider: "claude"},
},
model: "smart",
provider: "gemini",
want: "",
},
{
name: "disabled mapping skipped",
mappings: []GlobalModelMapping{
{From: "smart", To: "claude-opus-4-5-20251101", Enabled: boolPtr(false)},
{From: "smart", To: "claude-sonnet-4-5-20251101", Enabled: boolPtr(true)},
},
model: "smart",
provider: "",
want: "claude-sonnet-4-5-20251101",
},
{
name: "nil enabled defaults to true",
mappings: []GlobalModelMapping{
{From: "smart", To: "claude-opus-4-5-20251101", Enabled: nil},
},
model: "smart",
provider: "",
want: "claude-opus-4-5-20251101",
},
{
name: "first matching mapping wins",
mappings: []GlobalModelMapping{
{From: "smart", To: "first-model"},
{From: "smart", To: "second-model"},
},
model: "smart",
provider: "",
want: "first-model",
},
{
name: "provider case insensitive",
mappings: []GlobalModelMapping{
{From: "smart", To: "claude-opus-4-5-20251101", Provider: "CLAUDE"},
},
model: "smart",
provider: "claude",
want: "claude-opus-4-5-20251101",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cfg := &Config{
GlobalModelMappings: tt.mappings,
}
got := cfg.LookupGlobalModelMapping(tt.model, tt.provider)
if got != tt.want {
t.Errorf("LookupGlobalModelMapping() = %q, want %q", got, tt.want)
}
})
}
}
func TestLookupGlobalModelMapping_NilConfig(t *testing.T) {
var cfg *Config
got := cfg.LookupGlobalModelMapping("smart", "claude")
if got != "" {
t.Errorf("LookupGlobalModelMapping() on nil config = %q, want empty string", got)
}
}
func TestValidateConfig_NilConfig(t *testing.T) {
_, err := ValidateConfig(nil)
if err == nil {
t.Error("ValidateConfig(nil) should return error")
}
}
func TestGlobalModelMapping_IsEnabled(t *testing.T) {
boolPtr := func(b bool) *bool { return &b }
tests := []struct {
name string
mapping GlobalModelMapping
want bool
}{
{
name: "nil enabled defaults to true",
mapping: GlobalModelMapping{From: "a", To: "b", Enabled: nil},
want: true,
},
{
name: "explicitly enabled",
mapping: GlobalModelMapping{From: "a", To: "b", Enabled: boolPtr(true)},
want: true,
},
{
name: "explicitly disabled",
mapping: GlobalModelMapping{From: "a", To: "b", Enabled: boolPtr(false)},
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := tt.mapping.IsEnabled()
if got != tt.want {
t.Errorf("IsEnabled() = %v, want %v", got, tt.want)
}
})
}
}
+94 -4
View File
@@ -4,6 +4,10 @@
// debug settings, proxy configuration, and API keys.
package config
// GlobalModelMapperFunc is a function type for looking up global model mappings.
// It takes a model name and provider hint, and returns the mapped model name (or empty string if no mapping).
type GlobalModelMapperFunc func(model string, provider string) string
// SDKConfig represents the application's configuration, loaded from a YAML file.
type SDKConfig struct {
// ProxyURL is the URL of an optional proxy server to use for outbound requests.
@@ -25,18 +29,53 @@ type SDKConfig struct {
// Streaming configures server-side streaming behavior (keep-alives and safe bootstrap retries).
Streaming StreamingConfig `yaml:"streaming" json:"streaming"`
// Compression configures context compression behavior for long conversations.
Compression CompressionConfig `yaml:"compression,omitempty" json:"compression,omitempty"`
// GlobalModelMapper is an optional hook for looking up global model mappings.
// This is set by the parent Config to enable cross-provider model aliasing.
GlobalModelMapper GlobalModelMapperFunc `yaml:"-" json:"-"`
}
// StreamingConfig holds server streaming behavior configuration.
type StreamingConfig struct {
// KeepAliveSeconds controls how often the server emits SSE heartbeats (": keep-alive\n\n").
// nil means default (15 seconds). <= 0 disables keep-alives.
KeepAliveSeconds *int `yaml:"keepalive-seconds,omitempty" json:"keepalive-seconds,omitempty"`
// <= 0 disables keep-alives. Default is 0.
KeepAliveSeconds int `yaml:"keepalive-seconds,omitempty" json:"keepalive-seconds,omitempty"`
// BootstrapRetries controls how many times the server may retry a streaming request before any bytes are sent,
// to allow auth rotation / transient recovery.
// nil means default (2). 0 disables bootstrap retries.
BootstrapRetries *int `yaml:"bootstrap-retries,omitempty" json:"bootstrap-retries,omitempty"`
// <= 0 disables bootstrap retries. Default is 0.
BootstrapRetries int `yaml:"bootstrap-retries,omitempty" json:"bootstrap-retries,omitempty"`
// MaxChunkSize limits the maximum size (in bytes) of individual response chunks forwarded to clients.
// This can reduce latency for large responses by preventing buffer accumulation.
// nil means default (65536 = 64KB). 0 disables chunk size limiting.
MaxChunkSize *int `yaml:"max-chunk-size,omitempty" json:"max-chunk-size,omitempty"`
}
// CompressionConfig holds context compression behavior configuration.
type CompressionConfig struct {
// Enabled toggles LLM-based structured summarization.
// nil means default (true).
Enabled *bool `yaml:"enabled,omitempty" json:"enabled,omitempty"`
// ThresholdPercent triggers compression when context usage exceeds this fraction.
// nil means default (0.75 = 75%).
ThresholdPercent *float64 `yaml:"threshold-percent,omitempty" json:"threshold-percent,omitempty"`
// MaxSummaryTokens limits the output size of generated summaries.
// nil means default (2000).
MaxSummaryTokens *int `yaml:"max-summary-tokens,omitempty" json:"max-summary-tokens,omitempty"`
// SummarizationTimeoutSeconds is the timeout for LLM summarization calls.
// nil means default (30).
SummarizationTimeoutSeconds *int `yaml:"summarization-timeout-seconds,omitempty" json:"summarization-timeout-seconds,omitempty"`
// FallbackToRegex uses regex-based summarization when LLM fails.
// nil means default (true).
FallbackToRegex *bool `yaml:"fallback-to-regex,omitempty" json:"fallback-to-regex,omitempty"`
}
// AccessConfig groups request authentication providers.
@@ -100,3 +139,54 @@ func MakeInlineAPIKeyProvider(keys []string) *AccessProvider {
}
return provider
}
// IsEnabled returns whether LLM compression is enabled, defaulting to true.
func (c *CompressionConfig) IsEnabled() bool {
if c == nil || c.Enabled == nil {
return true
}
return *c.Enabled
}
// GetThresholdPercent returns the compression threshold, defaulting to 0.75.
func (c *CompressionConfig) GetThresholdPercent() float64 {
if c == nil || c.ThresholdPercent == nil {
return 0.75
}
return *c.ThresholdPercent
}
// GetMaxSummaryTokens returns the max summary tokens, defaulting to 2000.
func (c *CompressionConfig) GetMaxSummaryTokens() int {
if c == nil || c.MaxSummaryTokens == nil {
return 2000
}
return *c.MaxSummaryTokens
}
// GetSummarizationTimeout returns the summarization timeout, defaulting to 30 seconds.
func (c *CompressionConfig) GetSummarizationTimeout() int {
if c == nil || c.SummarizationTimeoutSeconds == nil {
return 30
}
return *c.SummarizationTimeoutSeconds
}
// ShouldFallbackToRegex returns whether to fall back to regex, defaulting to true.
func (c *CompressionConfig) ShouldFallbackToRegex() bool {
if c == nil || c.FallbackToRegex == nil {
return true
}
return *c.FallbackToRegex
}
// LookupGlobalModelMapping returns the mapped model name if a global mapping exists.
// It delegates to the GlobalModelMapper hook if set.
// Returns empty string if no mapping found or hook not set.
func (cfg *SDKConfig) LookupGlobalModelMapping(model string, provider string) string {
if cfg == nil || cfg.GlobalModelMapper == nil {
return ""
}
return cfg.GlobalModelMapper(model, provider)
}
+4 -16
View File
@@ -2,11 +2,11 @@ package desktopctl
import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/router-for-me/CLIProxyAPI/v6/internal/config"
"github.com/router-for-me/CLIProxyAPI/v6/internal/util"
)
func resolveConfigPath(repoRoot, configPath string) (string, error) {
@@ -45,21 +45,9 @@ func authDir(configPath string) (string, error) {
if err != nil {
return "", err
}
dir := strings.TrimSpace(cfg.AuthDir)
if dir == "" {
return "", fmt.Errorf("auth-dir not configured")
}
if strings.HasPrefix(dir, "~/") || dir == "~" {
home, _ := os.UserHomeDir()
if home != "" {
if dir == "~" {
dir = home
} else {
dir = filepath.Join(home, strings.TrimPrefix(dir, "~/"))
}
}
}
return dir, nil
// ResolveAuthDir handles empty string by returning DefaultAuthDir()
// and also handles tilde expansion.
return util.ResolveAuthDir(cfg.AuthDir)
}
// AuthDirFor returns the resolved auth directory for the given config file.
+130 -13
View File
@@ -11,10 +11,36 @@ import (
"path/filepath"
"runtime"
"strings"
"sync"
"syscall"
"time"
"github.com/router-for-me/CLIProxyAPI/v6/internal/buildinfo"
"github.com/router-for-me/CLIProxyAPI/v6/internal/embedded"
)
var (
embeddedMode bool
embeddedMu sync.RWMutex
embeddedConfig string
)
// SetEmbeddedMode enables or disables embedded server mode.
// When enabled, Start/Stop/Restart will use the in-process server
// instead of spawning a separate engine process.
func SetEmbeddedMode(enabled bool) {
embeddedMu.Lock()
defer embeddedMu.Unlock()
embeddedMode = enabled
}
// IsEmbeddedMode returns whether embedded mode is enabled.
func IsEmbeddedMode() bool {
embeddedMu.RLock()
defer embeddedMu.RUnlock()
return embeddedMode
}
func StatusFor(configPath string) (Status, error) {
statePath := defaultStatePath()
s, _ := loadState(statePath)
@@ -36,27 +62,32 @@ func StatusFor(configPath string) (Status, error) {
healthErr := checkHealth(baseURL)
running := healthErr == nil
managed := s != nil && s.PID > 0
if managed {
// Check if we're in embedded mode
isEmbedded := IsEmbeddedMode() || embedded.GlobalIsRunning()
managed := s != nil && (s.PID > 0 || isEmbedded)
if managed && !isEmbedded {
if alive, _ := isProcessAlive(s.PID); !alive {
managed = false
}
}
if s != nil {
if s != nil && !isEmbedded {
repairStateIfStale(statePath, s, running, managed)
}
out := Status{
Running: running,
Managed: managed,
AutoStartProxy: s != nil && s.AutoStartProxy,
PID: pidOrZero(s),
Port: port,
ThinkingPort: 8317,
BaseURL: baseURL,
ConfigPath: resolvedConfig,
ExePath: exeOrEmpty(s),
StartedAt: startedAtOrZero(s),
Running: running,
Version: buildinfo.Version,
Managed: managed,
AutoStartProxy: s != nil && s.AutoStartProxy,
PID: pidOrZero(s),
Port: port,
ThinkingPort: 8317,
BaseURL: baseURL,
ConfigPath: resolvedConfig,
ExePath: exeOrEmpty(s),
StartedAt: startedAtOrZero(s),
}
if inUse, _ := isLocalPortInUse(8317); inUse {
out.ThinkingRunning = true
@@ -68,6 +99,71 @@ func StatusFor(configPath string) (Status, error) {
}
func Start(opts StartOptions) (Status, error) {
// Use embedded mode if enabled or explicitly requested
if opts.Embedded || IsEmbeddedMode() {
return startEmbedded(opts)
}
return startSubprocess(opts)
}
// startEmbedded starts the proxy server in-process using the embedded package.
func startEmbedded(opts StartOptions) (Status, error) {
statePath := defaultStatePath()
prev, _ := loadState(statePath)
configPath, err := resolveConfigPath(opts.RepoRoot, opts.ConfigPath)
if err != nil {
return Status{}, err
}
if _, err := os.Stat(configPath); err != nil {
return Status{}, fmt.Errorf("config not found: %s", configPath)
}
port, err := loadPort(configPath)
if err != nil {
return Status{}, err
}
if inUse, _ := isLocalPortInUse(port); inUse {
return StatusFor(configPath)
}
pw, errPw := getOrCreateManagementPassword()
if errPw != nil {
return Status{}, errPw
}
// Set management password env var for the embedded server
os.Setenv("MANAGEMENT_PASSWORD", pw)
// Start embedded server
if err := embedded.StartGlobal(configPath, pw); err != nil {
return Status{}, err
}
// Store config path for embedded mode
embeddedMu.Lock()
embeddedConfig = configPath
embeddedMu.Unlock()
// Save state (with PID 0 to indicate embedded mode)
s := &state{
PID: 0, // 0 indicates embedded mode
ConfigPath: configPath,
ExePath: "", // No separate exe in embedded mode
StartedAt: embedded.GlobalServer().StartedAt(),
}
if prev != nil {
s.AutoStartProxy = prev.AutoStartProxy
s.OAuthPrivate = prev.OAuthPrivate
s.ManagementPassword = prev.ManagementPassword
}
_ = saveState(statePath, s)
return StatusFor(configPath)
}
// startSubprocess starts the proxy server as a separate process (legacy mode).
func startSubprocess(opts StartOptions) (Status, error) {
statePath := defaultStatePath()
prev, _ := loadState(statePath)
configPath, err := resolveConfigPath(opts.RepoRoot, opts.ConfigPath)
@@ -175,6 +271,27 @@ func Stop(opts StopOptions) error {
if pid == 0 && s != nil {
pid = s.PID
}
// Check if embedded server is running
if IsEmbeddedMode() || embedded.GlobalIsRunning() {
if err := embedded.StopGlobal(); err != nil {
return err
}
// Clear embedded config
embeddedMu.Lock()
embeddedConfig = ""
embeddedMu.Unlock()
// Update state
if s == nil {
s = &state{}
}
s.PID = 0
s.StartedAt = time.Time{}
_ = saveState(statePath, s)
return nil
}
// Legacy subprocess mode
if pid <= 0 {
return nil
}
+2
View File
@@ -4,6 +4,7 @@ import "time"
type Status struct {
Running bool `json:"running"`
Version string `json:"version,omitempty"`
Managed bool `json:"managed"`
AutoStartProxy bool `json:"auto_start_proxy,omitempty"`
PID int `json:"pid,omitempty"`
@@ -22,6 +23,7 @@ type StartOptions struct {
ConfigPath string
ExePath string
LogDir string
Embedded bool // If true, run server in-process instead of spawning subprocess
}
type StopOptions struct {
+224
View File
@@ -0,0 +1,224 @@
// Package embedded provides an in-process proxy server that can be embedded
// directly into the tray application, eliminating the need for a separate engine binary.
package embedded
import (
"context"
"errors"
"fmt"
"os"
"path/filepath"
"strings"
"sync"
"time"
"github.com/joho/godotenv"
"github.com/router-for-me/CLIProxyAPI/v6/internal/api"
"github.com/router-for-me/CLIProxyAPI/v6/internal/config"
"github.com/router-for-me/CLIProxyAPI/v6/internal/logging"
"github.com/router-for-me/CLIProxyAPI/v6/internal/util"
configaccess "github.com/router-for-me/CLIProxyAPI/v6/sdk/access/config"
sdkAuth "github.com/router-for-me/CLIProxyAPI/v6/sdk/auth"
"github.com/router-for-me/CLIProxyAPI/v6/sdk/cliproxy"
log "github.com/sirupsen/logrus"
)
// Server represents an embedded proxy server instance.
type Server struct {
mu sync.Mutex
running bool
cancel context.CancelFunc
done chan struct{}
configPath string
password string
port int
startedAt time.Time
}
// NewServer creates a new embedded server instance.
func NewServer() *Server {
return &Server{}
}
// Start starts the embedded proxy server with the given configuration.
func (s *Server) Start(configPath, password string) error {
s.mu.Lock()
defer s.mu.Unlock()
if s.running {
return errors.New("server is already running")
}
// Resolve config path
resolvedPath := strings.TrimSpace(configPath)
if resolvedPath == "" {
return errors.New("config path is required")
}
// Load .env if present
if wd, err := os.Getwd(); err == nil {
_ = godotenv.Load(filepath.Join(wd, ".env"))
}
// Load configuration
cfg, err := config.LoadConfig(resolvedPath)
if err != nil {
return fmt.Errorf("failed to load config: %w", err)
}
// Resolve auth-dir to apply default if empty
if resolvedAuthDir, errResolve := util.ResolveAuthDir(cfg.AuthDir); errResolve == nil {
cfg.AuthDir = resolvedAuthDir
}
s.configPath = resolvedPath
s.password = password
s.port = cfg.Port
// Setup logging with config-driven output
logging.SetupBaseLogger()
logging.ConfigureLogOutput(cfg.LoggingToFile, cfg.LogsMaxTotalSizeMB)
// Create context with cancel
ctx, cancel := context.WithCancel(context.Background())
s.cancel = cancel
s.done = make(chan struct{})
// Build service
builder := cliproxy.NewBuilder().
WithConfig(cfg).
WithConfigPath(resolvedPath).
WithLocalManagementPassword(password)
// Add keep-alive endpoint if password is set
if password != "" {
builder = builder.WithServerOptions(api.WithKeepAliveEndpoint(10*time.Second, func() {
log.Warn("keep-alive endpoint idle for 10s (embedded mode - ignoring)")
// In embedded mode, we don't auto-shutdown on keep-alive timeout
}))
}
// Register SDK providers
sdkAuth.RegisterTokenStore(sdkAuth.NewFileTokenStore())
configaccess.Register()
service, err := builder.Build()
if err != nil {
cancel()
return fmt.Errorf("failed to build service: %w", err)
}
s.running = true
s.startedAt = time.Now()
// Run service in goroutine
go func() {
defer close(s.done)
err := service.Run(ctx)
if err != nil && !errors.Is(err, context.Canceled) {
log.Errorf("embedded proxy service exited with error: %v", err)
}
s.mu.Lock()
s.running = false
s.mu.Unlock()
}()
// Wait briefly for server to start
time.Sleep(500 * time.Millisecond)
return nil
}
// Stop stops the embedded proxy server.
func (s *Server) Stop() error {
s.mu.Lock()
defer s.mu.Unlock()
if !s.running {
return nil
}
if s.cancel != nil {
s.cancel()
}
// Wait for shutdown with timeout
select {
case <-s.done:
case <-time.After(5 * time.Second):
log.Warn("embedded server shutdown timeout")
}
s.running = false
return nil
}
// Restart restarts the embedded proxy server.
func (s *Server) Restart() error {
configPath := s.configPath
password := s.password
if err := s.Stop(); err != nil {
return err
}
time.Sleep(200 * time.Millisecond)
return s.Start(configPath, password)
}
// IsRunning returns whether the server is currently running.
func (s *Server) IsRunning() bool {
s.mu.Lock()
defer s.mu.Unlock()
return s.running
}
// Port returns the port the server is running on.
func (s *Server) Port() int {
s.mu.Lock()
defer s.mu.Unlock()
return s.port
}
// StartedAt returns when the server was started.
func (s *Server) StartedAt() time.Time {
s.mu.Lock()
defer s.mu.Unlock()
return s.startedAt
}
// ConfigPath returns the configuration file path.
func (s *Server) ConfigPath() string {
s.mu.Lock()
defer s.mu.Unlock()
return s.configPath
}
// Global embedded server instance
var globalServer = NewServer()
// StartGlobal starts the global embedded server.
func StartGlobal(configPath, password string) error {
return globalServer.Start(configPath, password)
}
// StopGlobal stops the global embedded server.
func StopGlobal() error {
return globalServer.Stop()
}
// RestartGlobal restarts the global embedded server.
func RestartGlobal() error {
return globalServer.Restart()
}
// GlobalIsRunning returns whether the global server is running.
func GlobalIsRunning() bool {
return globalServer.IsRunning()
}
// GlobalServer returns the global server instance.
func GlobalServer() *Server {
return globalServer
}
-22
View File
@@ -3,7 +3,6 @@ package errors
import (
"encoding/json"
"fmt"
"net/http"
)
// AppError represents a structured application error.
@@ -48,24 +47,3 @@ func New(statusCode int, code, message string, err error) *AppError {
}
}
// Common error constructors
func BadRequest(message string, err error) *AppError {
return New(http.StatusBadRequest, "bad_request", message, err)
}
func Unauthorized(message string, err error) *AppError {
return New(http.StatusUnauthorized, "unauthorized", message, err)
}
func Forbidden(message string, err error) *AppError {
return New(http.StatusForbidden, "forbidden", message, err)
}
func NotFound(message string, err error) *AppError {
return New(http.StatusNotFound, "not_found", message, err)
}
func InternalServerError(message string, err error) *AppError {
return New(http.StatusInternalServerError, "internal_error", message, err)
}
+407
View File
@@ -0,0 +1,407 @@
package integrations
import (
"encoding/json"
"fmt"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
)
type Agent struct {
Name string `json:"name"`
ID string `json:"id"`
Detected bool `json:"detected"`
Configured bool `json:"configured"`
ConfigInstructions string `json:"configInstructions"`
EnvVars map[string]string `json:"envVars"`
CanAutoConfigure bool `json:"canAutoConfigure"`
}
func DetectCLIAgents(proxyURL string) []Agent {
if proxyURL == "" {
proxyURL = "http://localhost:8080"
}
return []Agent{
detectClaudeCode(proxyURL),
detectCodexCLI(proxyURL),
detectAider(proxyURL),
detectGeminiCLI(proxyURL),
detectDroidCLI(proxyURL),
}
}
func ConfigureCLIAgent(agentID, proxyURL string) error {
if proxyURL == "" {
proxyURL = "http://localhost:8080"
}
switch agentID {
case "claude-code":
return configureClaudeCode(proxyURL)
case "codex":
return configureCodexCLI(proxyURL)
case "aider":
return configureAider(proxyURL)
case "gemini-cli":
return configureGeminiCLI(proxyURL)
case "droid":
return configureDroidCLI(proxyURL)
default:
return fmt.Errorf("unknown agent: %s", agentID)
}
}
func getHomeDir() string {
if runtime.GOOS == "windows" {
return os.Getenv("USERPROFILE")
}
return os.Getenv("HOME")
}
func getShellProfile() string {
home := getHomeDir()
if runtime.GOOS == "windows" {
return filepath.Join(home, "Documents", "PowerShell", "Microsoft.PowerShell_profile.ps1")
}
shell := os.Getenv("SHELL")
if strings.Contains(shell, "zsh") {
return filepath.Join(home, ".zshrc")
}
if strings.Contains(shell, "fish") {
return filepath.Join(home, ".config", "fish", "config.fish")
}
return filepath.Join(home, ".bashrc")
}
func appendToShellProfile(content string) error {
profilePath := getShellProfile()
if err := os.MkdirAll(filepath.Dir(profilePath), 0755); err != nil {
return err
}
existing, _ := os.ReadFile(profilePath)
if strings.Contains(string(existing), "# ProxyPilot") {
lines := strings.Split(string(existing), "\n")
var newLines []string
skip := false
for _, line := range lines {
if strings.Contains(line, "# ProxyPilot START") {
skip = true
continue
}
if strings.Contains(line, "# ProxyPilot END") {
skip = false
continue
}
if !skip {
newLines = append(newLines, line)
}
}
existing = []byte(strings.Join(newLines, "\n"))
}
f, err := os.OpenFile(profilePath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
return err
}
defer f.Close()
if len(existing) > 0 {
f.Write(existing)
if !strings.HasSuffix(string(existing), "\n") {
f.WriteString("\n")
}
}
f.WriteString("\n" + content)
return nil
}
func detectClaudeCode(proxyURL string) Agent {
detected := false
if _, err := exec.LookPath("claude"); err == nil {
detected = true
}
if !detected {
if appData := os.Getenv("APPDATA"); appData != "" {
if _, err := os.Stat(filepath.Join(appData, "Claude")); err == nil {
detected = true
}
}
}
configured := false
if detected {
i := &ClaudeIntegration{}
configured, _ = i.IsConfigured(proxyURL)
}
return Agent{
ID: "claude-code", Name: "Claude Code", Detected: detected, Configured: configured,
ConfigInstructions: "Set ANTHROPIC_BASE_URL environment variable",
EnvVars: map[string]string{"ANTHROPIC_BASE_URL": proxyURL + "/v1"}, CanAutoConfigure: true,
}
}
func configureClaudeCode(proxyURL string) error {
var content string
if runtime.GOOS == "windows" {
content = fmt.Sprintf("# ProxyPilot START\n$env:ANTHROPIC_BASE_URL = \"%s/v1\"\n# ProxyPilot END\n", proxyURL)
} else {
content = fmt.Sprintf("# ProxyPilot START\nexport ANTHROPIC_BASE_URL=\"%s/v1\"\n# ProxyPilot END\n", proxyURL)
}
return appendToShellProfile(content)
}
func detectCodexCLI(proxyURL string) Agent {
detected := false
if _, err := exec.LookPath("codex"); err == nil {
detected = true
}
configured := false
if detected {
i := &CodexIntegration{}
configured, _ = i.IsConfigured(proxyURL)
}
return Agent{
ID: "codex", Name: "Codex CLI", Detected: detected, Configured: configured,
ConfigInstructions: "Configure ~/.codex/config.toml",
EnvVars: map[string]string{"OPENAI_BASE_URL": proxyURL + "/v1"}, CanAutoConfigure: true,
}
}
func configureCodexCLI(proxyURL string) error {
home := getHomeDir()
codexDir := filepath.Join(home, ".codex")
if err := os.MkdirAll(codexDir, 0755); err != nil {
return err
}
configContent := fmt.Sprintf("# Configured by ProxyPilot\nmodel_provider = \"openai\"\n\n[openai]\nbase_url = \"%s/v1\"\n", proxyURL)
if err := os.WriteFile(filepath.Join(codexDir, "config.toml"), []byte(configContent), 0644); err != nil {
return err
}
return os.WriteFile(filepath.Join(codexDir, "auth.json"), []byte("{\"OPENAI_API_KEY\": \"proxypilot-local\"}"), 0644)
}
func detectAider(proxyURL string) Agent {
detected := false
if _, err := exec.LookPath("aider"); err == nil {
detected = true
}
configured := false
if detected {
apiBase := os.Getenv("OPENAI_API_BASE")
aiderBase := os.Getenv("AIDER_OPENAI_API_BASE")
if strings.Contains(apiBase, proxyURL) || strings.Contains(aiderBase, proxyURL) {
configured = true
}
}
return Agent{
ID: "aider", Name: "Aider", Detected: detected, Configured: configured,
ConfigInstructions: "Set OPENAI_API_BASE environment variable",
EnvVars: map[string]string{"OPENAI_API_BASE": proxyURL + "/v1"}, CanAutoConfigure: true,
}
}
func configureAider(proxyURL string) error {
var content string
if runtime.GOOS == "windows" {
content = fmt.Sprintf("# ProxyPilot START - Aider\n$env:OPENAI_API_BASE = \"%s/v1\"\n$env:OPENAI_API_KEY = \"proxypilot-local\"\n# ProxyPilot END - Aider\n", proxyURL)
} else {
content = fmt.Sprintf("# ProxyPilot START - Aider\nexport OPENAI_API_BASE=\"%s/v1\"\nexport OPENAI_API_KEY=\"proxypilot-local\"\n# ProxyPilot END - Aider\n", proxyURL)
}
return appendToShellProfile(content)
}
func detectGeminiCLI(proxyURL string) Agent {
detected := false
if _, err := exec.LookPath("gemini"); err == nil {
detected = true
}
configured := false
if detected {
baseURL := os.Getenv("GOOGLE_GEMINI_BASE_URL")
if strings.Contains(baseURL, proxyURL) {
configured = true
}
}
return Agent{
ID: "gemini-cli", Name: "Gemini CLI", Detected: detected, Configured: configured,
ConfigInstructions: "Set GOOGLE_GEMINI_BASE_URL environment variable",
EnvVars: map[string]string{"GOOGLE_GEMINI_BASE_URL": proxyURL}, CanAutoConfigure: true,
}
}
func configureGeminiCLI(proxyURL string) error {
var content string
if runtime.GOOS == "windows" {
content = fmt.Sprintf("# ProxyPilot START - Gemini\n$env:GOOGLE_GEMINI_BASE_URL = \"%s\"\n$env:GEMINI_API_KEY = \"proxypilot-local\"\n# ProxyPilot END - Gemini\n", proxyURL)
} else {
content = fmt.Sprintf("# ProxyPilot START - Gemini\nexport GOOGLE_GEMINI_BASE_URL=\"%s\"\nexport GEMINI_API_KEY=\"proxypilot-local\"\n# ProxyPilot END - Gemini\n", proxyURL)
}
return appendToShellProfile(content)
}
func detectDroidCLI(proxyURL string) Agent {
detected := false
if _, err := exec.LookPath("droid"); err == nil {
detected = true
}
home := getHomeDir()
factoryDir := filepath.Join(home, ".factory")
if _, err := os.Stat(factoryDir); err == nil {
detected = true
}
configured := false
if detected {
configPath := filepath.Join(factoryDir, "config.json")
if data, err := os.ReadFile(configPath); err == nil {
if strings.Contains(string(data), proxyURL) {
configured = true
}
}
}
return Agent{
ID: "droid", Name: "Droid CLI", Detected: detected, Configured: configured,
ConfigInstructions: "Configure ~/.factory/config.json",
EnvVars: map[string]string{}, CanAutoConfigure: true,
}
}
func configureDroidCLI(proxyURL string) error {
home := getHomeDir()
factoryDir := filepath.Join(home, ".factory")
configPath := filepath.Join(factoryDir, "config.json")
if err := os.MkdirAll(factoryDir, 0755); err != nil {
return err
}
var config map[string]interface{}
if data, err := os.ReadFile(configPath); err == nil {
json.Unmarshal(data, &config)
}
if config == nil {
config = make(map[string]interface{})
}
customModels := []map[string]interface{}{
{"model_display_name": "Claude Sonnet 4 (ProxyPilot)", "model": "claude-sonnet-4-20250514", "base_url": proxyURL + "/v1/", "api_key": "proxypilot-local", "provider": "generic-chat-completion-api", "max_tokens": 128000},
{"model_display_name": "Claude Opus 4 (ProxyPilot)", "model": "claude-opus-4-20250514", "base_url": proxyURL + "/v1/", "api_key": "proxypilot-local", "provider": "generic-chat-completion-api", "max_tokens": 128000},
{"model_display_name": "GPT-4o (ProxyPilot)", "model": "gpt-4o", "base_url": proxyURL + "/v1/", "api_key": "proxypilot-local", "provider": "generic-chat-completion-api", "max_tokens": 128000},
{"model_display_name": "Gemini 2.5 Pro (ProxyPilot)", "model": "gemini-2.5-pro", "base_url": proxyURL + "/v1/", "api_key": "proxypilot-local", "provider": "generic-chat-completion-api", "max_tokens": 128000},
}
if existing, ok := config["custom_models"].([]interface{}); ok {
for _, m := range existing {
if model, ok := m.(map[string]interface{}); ok {
if baseURL, ok := model["base_url"].(string); ok {
if !strings.Contains(baseURL, "localhost:8") {
customModels = append(customModels, model)
}
}
}
}
}
config["custom_models"] = customModels
data, err := json.MarshalIndent(config, "", " ")
if err != nil {
return err
}
return os.WriteFile(configPath, data, 0644)
}
func UnconfigureCLIAgent(agentID string) error {
switch agentID {
case "claude-code":
return removeFromShellProfile("# ProxyPilot START", "# ProxyPilot END")
case "codex":
return unconfigureCodexCLI()
case "aider":
return removeFromShellProfile("# ProxyPilot START - Aider", "# ProxyPilot END - Aider")
case "gemini-cli":
return removeFromShellProfile("# ProxyPilot START - Gemini", "# ProxyPilot END - Gemini")
case "droid":
return unconfigureDroidCLI()
default:
return fmt.Errorf("unknown agent: %s", agentID)
}
}
func removeFromShellProfile(startMarker, endMarker string) error {
profilePath := getShellProfile()
existing, err := os.ReadFile(profilePath)
if err != nil {
return nil
}
if !strings.Contains(string(existing), startMarker) {
return nil
}
lines := strings.Split(string(existing), "\n")
var newLines []string
skip := false
for _, line := range lines {
if strings.Contains(line, startMarker) {
skip = true
continue
}
if strings.Contains(line, endMarker) {
skip = false
continue
}
if !skip {
newLines = append(newLines, line)
}
}
result := strings.TrimSpace(strings.Join(newLines, "\n")) + "\n"
return os.WriteFile(profilePath, []byte(result), 0644)
}
func unconfigureCodexCLI() error {
home := getHomeDir()
codexDir := filepath.Join(home, ".codex")
configPath := filepath.Join(codexDir, "config.toml")
data, err := os.ReadFile(configPath)
if err != nil {
return nil
}
if strings.Contains(string(data), "Configured by ProxyPilot") {
os.Remove(configPath)
os.Remove(filepath.Join(codexDir, "auth.json"))
return nil
}
lines := strings.Split(string(data), "\n")
var newLines []string
for _, line := range lines {
if !strings.Contains(line, "base_url") || !strings.Contains(line, "localhost:8") {
newLines = append(newLines, line)
}
}
return os.WriteFile(configPath, []byte(strings.Join(newLines, "\n")), 0644)
}
func unconfigureDroidCLI() error {
home := getHomeDir()
factoryDir := filepath.Join(home, ".factory")
configPath := filepath.Join(factoryDir, "config.json")
data, err := os.ReadFile(configPath)
if err != nil {
return nil
}
var config map[string]interface{}
if err := json.Unmarshal(data, &config); err != nil {
return err
}
if existing, ok := config["custom_models"].([]interface{}); ok {
var filtered []interface{}
for _, m := range existing {
if model, ok := m.(map[string]interface{}); ok {
if baseURL, ok := model["base_url"].(string); ok {
if !strings.Contains(baseURL, "localhost:8") {
filtered = append(filtered, model)
}
} else {
filtered = append(filtered, model)
}
}
}
config["custom_models"] = filtered
}
result, err := json.MarshalIndent(config, "", " ")
if err != nil {
return err
}
return os.WriteFile(configPath, result, 0644)
}
+44 -30
View File
@@ -1,6 +1,7 @@
package integrations
import (
"fmt"
"os"
"path/filepath"
"strings"
@@ -25,13 +26,14 @@ func (i *DroidIntegration) Detect() (bool, error) {
}
func (i *DroidIntegration) IsConfigured(proxyURL string) (bool, error) {
configPath := filepath.Join(userHomeDir(), ".factory", "config.json")
if !fileExists(configPath) {
// Droid uses settings.json for runtime config, not config.json
settingsPath := filepath.Join(userHomeDir(), ".factory", "settings.json")
if !fileExists(settingsPath) {
return false, nil
}
content, _ := os.ReadFile(configPath)
// Check if any custom model uses our proxy URL
res := gjson.GetBytes(content, "custom_models.#.base_url")
content, _ := os.ReadFile(settingsPath)
// Check if any custom model uses our proxy URL (note: camelCase "customModels")
res := gjson.GetBytes(content, "customModels.#.baseUrl")
for _, match := range res.Array() {
if strings.Contains(match.String(), proxyURL) {
return true, nil
@@ -42,11 +44,12 @@ func (i *DroidIntegration) IsConfigured(proxyURL string) (bool, error) {
func (i *DroidIntegration) Configure(proxyURL string) error {
configDir := filepath.Join(userHomeDir(), ".factory")
configPath := filepath.Join(configDir, "config.json")
// Droid uses settings.json for custom models at runtime
settingsPath := filepath.Join(configDir, "settings.json")
var jsonStr string
if fileExists(configPath) {
b, _ := os.ReadFile(configPath)
if fileExists(settingsPath) {
b, _ := os.ReadFile(settingsPath)
jsonStr = string(b)
} else {
if err := os.MkdirAll(configDir, 0755); err != nil {
@@ -55,45 +58,56 @@ func (i *DroidIntegration) Configure(proxyURL string) error {
jsonStr = "{}"
}
// Define models to add
models := []string{
"gpt-5.2",
"claude-opus-4-5-thinking",
"gemini-3-pro-preview",
// Define models to add - must use thinking models for /v1/responses API
models := []struct {
model string
displayName string
}{
{"claude-opus-4-5-thinking", "ProxyPilot Opus 4.5 Thinking"},
{"claude-sonnet-4-5-thinking", "ProxyPilot Sonnet 4.5 Thinking"},
}
for _, m := range models {
// Replicate setup-droid-cliproxy.ps1 logic
for idx, m := range models {
// Droid settings.json uses camelCase and requires id/index fields
newEntry := map[string]interface{}{
"model_display_name": "ProxyPilot (local): " + m,
"model": m,
"base_url": proxyURL + "/v1",
"api_key": "local-dev-key", // standard placeholder
"provider": "openai",
"model": m.model,
"id": fmt.Sprintf("custom:ProxyPilot-%d", idx),
"index": idx,
"baseUrl": proxyURL + "/v1",
"apiKey": "local-dev-key",
"displayName": m.displayName,
"noImageSupport": true,
"provider": "openai", // Uses OpenAI Responses API (/v1/responses)
}
// Check if exists
existing := gjson.Get(jsonStr, "custom_models")
idx := -1
// Check if model already exists
existing := gjson.Get(jsonStr, "customModels")
existingIdx := -1
if existing.IsArray() {
for i, entry := range existing.Array() {
if entry.Get("model").String() == m {
idx = i
if entry.Get("model").String() == m.model {
existingIdx = i
break
}
}
}
var errSet error
if idx >= 0 {
jsonStr, errSet = sjson.Set(jsonStr, "custom_models."+string(rune(idx)), newEntry)
if existingIdx >= 0 {
jsonStr, errSet = sjson.Set(jsonStr, fmt.Sprintf("customModels.%d", existingIdx), newEntry)
} else {
jsonStr, errSet = sjson.Set(jsonStr, "custom_models.-1", newEntry)
jsonStr, errSet = sjson.Set(jsonStr, "customModels.-1", newEntry)
}
if errSet != nil {
return errSet
}
}
return os.WriteFile(configPath, []byte(jsonStr), 0644)
// Set default model if not set
if !gjson.Get(jsonStr, "sessionDefaultSettings.model").Exists() {
jsonStr, _ = sjson.Set(jsonStr, "sessionDefaultSettings.model", "custom:ProxyPilot-1")
jsonStr, _ = sjson.Set(jsonStr, "sessionDefaultSettings.reasoningEffort", "none")
}
return os.WriteFile(settingsPath, []byte(jsonStr), 0644)
}
+43
View File
@@ -159,3 +159,46 @@ type ToolDeclaration struct {
// FunctionDeclarations is a list of available functions that the model can call.
FunctionDeclarations []interface{} `json:"functionDeclarations"`
}
// RequestLogEntry represents a single entry in the request monitor.
type RequestLogEntry struct {
ID string `json:"id"`
Timestamp time.Time `json:"timestamp"`
Method string `json:"method"`
Path string `json:"path"`
Model string `json:"model"`
Provider string `json:"provider"`
Status int `json:"status"`
LatencyMs int64 `json:"latencyMs"`
InputTokens int `json:"inputTokens"`
OutputTokens int `json:"outputTokens"`
Error string `json:"error,omitempty"`
}
// UsageStats represents aggregated usage statistics for the dashboard.
type UsageStats struct {
TotalRequests int64 `json:"totalRequests"`
SuccessCount int64 `json:"successCount"`
FailureCount int64 `json:"failureCount"`
TotalInputTokens int64 `json:"totalInputTokens"`
TotalOutputTokens int64 `json:"totalOutputTokens"`
EstimatedCostSaved float64 `json:"estimatedCostSaved"` // Deprecated: use ActualCost
ActualCost float64 `json:"actualCost"` // Cost via ProxyPilot
DirectAPICost float64 `json:"directApiCost"` // What it would cost via direct API
Savings float64 `json:"savings"` // DirectAPICost - ActualCost
SavingsPercent float64 `json:"savingsPercent"` // Savings as percentage
ByModel map[string]int64 `json:"byModel"`
ByProvider map[string]int64 `json:"byProvider"`
CostByModel map[string]float64 `json:"costByModel"` // Cost breakdown by model
CostByProvider map[string]float64 `json:"costByProvider"` // Cost breakdown by provider
Daily []DailyUsage `json:"daily"`
}
// DailyUsage represents usage statistics for a single day.
type DailyUsage struct {
Date string `json:"date"`
Requests int64 `json:"requests"`
Tokens int64 `json:"tokens"`
InputTokens int64 `json:"inputTokens"`
OutputTokens int64 `json:"outputTokens"`
}
-90
View File
@@ -1,90 +0,0 @@
package letta
import (
"context"
"os"
"os/exec"
"path/filepath"
"runtime"
"syscall"
"time"
"github.com/router-for-me/CLIProxyAPI/v6/internal/util"
log "github.com/sirupsen/logrus"
)
// StartServer starts the Letta server in a headless background process.
// It redirects Letta's output to logs/letta.log.
func StartServer(ctx context.Context) {
// Check if letta is available in the system PATH
lettaPath, err := exec.LookPath("letta")
if err != nil {
log.Debug("Letta executable not found in PATH; skipping headless sidecar start.")
return
}
logDir := "logs"
if base := util.WritablePath(); base != "" {
logDir = filepath.Join(base, "logs")
}
if err := os.MkdirAll(logDir, 0o755); err != nil {
log.Errorf("letta: failed to create log directory: %v", err)
return
}
logFile := filepath.Join(logDir, "letta.log")
// Create a goroutine to manage the Letta process lifecycle
go func() {
for {
select {
case <-ctx.Done():
log.Info("Letta sidecar shutting down...")
return
default:
// Open log file for appending
f, err := os.OpenFile(logFile, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0o644)
if err != nil {
log.Errorf("letta: failed to open log file: %v", err)
time.Sleep(10 * time.Second)
continue
}
log.Infof("Starting Letta server sidecar (logs: %s)", logFile)
cmd := exec.CommandContext(ctx, lettaPath, "server")
cmd.Stdout = f
cmd.Stderr = f
// Ensure the process is hidden on Windows
if runtime.GOOS == "windows" {
cmd.SysProcAttr = &syscall.SysProcAttr{HideWindow: true}
}
// Start Letta
if err := cmd.Start(); err != nil {
log.Errorf("letta: failed to start server: %v", err)
f.Close()
time.Sleep(10 * time.Second)
continue
}
// Wait for process to exit or context to be cancelled
waitErr := cmd.Wait()
f.Close()
if ctx.Err() != nil {
return
}
if waitErr != nil {
log.Errorf("Letta server exited with error: %v. Restarting in 5s...", waitErr)
} else {
log.Warn("Letta server exited normally. Restarting in 5s...")
}
time.Sleep(5 * time.Second)
}
}
}()
}
+143 -4
View File
@@ -3,6 +3,7 @@ package memory
import (
"bufio"
"bytes"
"context"
"crypto/sha256"
"encoding/hex"
"encoding/json"
@@ -19,10 +20,11 @@ import (
)
type FileStore struct {
BaseDir string
cacheMu sync.Mutex
cache map[string]searchCacheEntry
cacheKeys []string
BaseDir string
cacheMu sync.Mutex
cache map[string]searchCacheEntry
cacheKeys []string
summarizer *Summarizer
}
type searchCacheEntry struct {
@@ -813,3 +815,140 @@ func queryTokens(q string, max int) []string {
}
return out
}
// ReadHarnessFile reads a harness file from the session's harness subdirectory.
func (s *FileStore) ReadHarnessFile(session string, filename string, maxChars int) string {
if s == nil || s.BaseDir == "" || session == "" {
return ""
}
if maxChars <= 0 {
maxChars = 50000
}
dir := filepath.Join(s.sessionDir(session), "harness")
txt := strings.TrimSpace(s.readSmallTextFile(filepath.Join(dir, filename), int64(maxChars*2)))
if len(txt) > maxChars {
txt = txt[:maxChars] + "\n...[truncated]..."
}
return txt
}
// WriteHarnessFile writes a harness file to the session's harness subdirectory.
func (s *FileStore) WriteHarnessFile(session string, filename string, content string, maxChars int) error {
if s == nil || s.BaseDir == "" {
return errors.New("memory store not configured")
}
if session == "" {
return nil
}
if maxChars <= 0 {
maxChars = 100000
}
content = strings.TrimSpace(content)
if len(content) > maxChars {
content = content[:maxChars]
}
dir := filepath.Join(s.sessionDir(session), "harness")
if err := os.MkdirAll(dir, 0o755); err != nil {
return err
}
return os.WriteFile(filepath.Join(dir, filename), []byte(content), 0o644)
}
// ListHarnessFiles returns all harness files for a session.
func (s *FileStore) ListHarnessFiles(session string) ([]string, error) {
if s == nil || s.BaseDir == "" || session == "" {
return nil, nil
}
dir := filepath.Join(s.sessionDir(session), "harness")
entries, err := os.ReadDir(dir)
if err != nil {
if os.IsNotExist(err) {
return nil, nil
}
return nil, err
}
files := make([]string, 0, len(entries))
for _, e := range entries {
if !e.IsDir() {
files = append(files, e.Name())
}
}
return files, nil
}
// SetSummarizer configures the LLM summarizer for this store
func (s *FileStore) SetSummarizer(summarizer *Summarizer) {
if s == nil {
return
}
s.summarizer = summarizer
}
// GetSummarizer returns the configured summarizer, if any
func (s *FileStore) GetSummarizer() *Summarizer {
if s == nil {
return nil
}
return s.summarizer
}
// ReadStructuredSummary reads and parses the structured summary for a session
func (s *FileStore) ReadStructuredSummary(session string) (*StructuredSummary, error) {
if s == nil || s.BaseDir == "" {
return nil, errors.New("memory store not configured")
}
if session == "" {
return nil, errors.New("session is required")
}
dir := s.sessionDir(session)
path := filepath.Join(dir, "summary.md")
content := s.readSmallTextFile(path, 60_000)
if strings.TrimSpace(content) == "" {
return nil, nil
}
return ParseStructuredSummary(content)
}
// WriteStructuredSummary persists a structured summary for a session
func (s *FileStore) WriteStructuredSummary(session string, summary *StructuredSummary) error {
if s == nil || s.BaseDir == "" {
return errors.New("memory store not configured")
}
if session == "" {
return errors.New("session is required")
}
if summary == nil {
return nil
}
dir := s.sessionDir(session)
if err := os.MkdirAll(dir, 0o755); err != nil {
return err
}
content := RenderStructuredSummary(summary)
return os.WriteFile(filepath.Join(dir, "summary.md"), []byte(content), 0o644)
}
// BuildAnchoredSummaryWithLLM generates a structured summary using LLM when available,
// falling back to regex-based BuildAnchoredSummary when not.
func BuildAnchoredSummaryWithLLM(ctx context.Context, model string, prev string, dropped []Event, latestIntent string, summarizer *Summarizer) string {
if summarizer == nil || !summarizer.config.Enabled {
return BuildAnchoredSummary(prev, dropped, latestIntent)
}
existing, _ := ParseStructuredSummary(prev)
var result *StructuredSummary
var err error
if existing == nil {
result, err = summarizer.GenerateInitialSummary(ctx, model, dropped, latestIntent)
} else {
result, err = summarizer.MergeSummary(ctx, model, existing, dropped, latestIntent)
}
if err != nil {
// Fallback to regex-based summary
return BuildAnchoredSummary(prev, dropped, latestIntent)
}
return RenderStructuredSummary(result)
}
+641
View File
@@ -0,0 +1,641 @@
package memory
import (
"encoding/json"
"os"
"path/filepath"
"strings"
"testing"
"time"
)
func TestFileStore_Append_CreatesFile(t *testing.T) {
tests := []struct {
name string
session string
events []Event
wantErr bool
}{
{
name: "creates file with single event",
session: "test-session-1",
events: []Event{
{
TS: time.Now(),
Kind: "message",
Role: "user",
Text: "Hello, world!",
},
},
wantErr: false,
},
{
name: "creates file with event that has zero timestamp",
session: "test-session-2",
events: []Event{
{
Kind: "message",
Role: "user",
Text: "This event has no timestamp set",
},
},
wantErr: false,
},
{
name: "empty session returns nil without creating file",
session: "",
events: []Event{
{Kind: "message", Text: "test"},
},
wantErr: false,
},
{
name: "empty events returns nil without creating file",
session: "test-session-3",
events: []Event{},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tmpDir := t.TempDir()
store := NewFileStore(tmpDir)
err := store.Append(tt.session, tt.events)
if (err != nil) != tt.wantErr {
t.Errorf("Append() error = %v, wantErr %v", err, tt.wantErr)
return
}
if tt.session == "" || len(tt.events) == 0 {
// File should not be created
eventsPath := filepath.Join(tmpDir, "sessions", tt.session, "events.jsonl")
if _, err := os.Stat(eventsPath); !os.IsNotExist(err) {
t.Errorf("File should not exist for empty session or events")
}
return
}
// Verify file was created
eventsPath := filepath.Join(tmpDir, "sessions", tt.session, "events.jsonl")
if _, err := os.Stat(eventsPath); os.IsNotExist(err) {
t.Errorf("events.jsonl was not created")
return
}
// Verify content is valid JSONL
data, err := os.ReadFile(eventsPath)
if err != nil {
t.Fatalf("Failed to read events file: %v", err)
}
lines := strings.Split(strings.TrimSpace(string(data)), "\n")
if len(lines) != len(tt.events) {
t.Errorf("Expected %d lines, got %d", len(tt.events), len(lines))
}
for i, line := range lines {
var e Event
if err := json.Unmarshal([]byte(line), &e); err != nil {
t.Errorf("Line %d is not valid JSON: %v", i, err)
}
}
})
}
}
func TestFileStore_Append_MultipleEvents(t *testing.T) {
tests := []struct {
name string
session string
eventSets [][]Event
wantTotal int
wantErr bool
}{
{
name: "append multiple events in single call",
session: "multi-event-session",
eventSets: [][]Event{
{
{Kind: "message", Role: "user", Text: "First message"},
{Kind: "message", Role: "assistant", Text: "First response"},
{Kind: "message", Role: "user", Text: "Second message"},
},
},
wantTotal: 3,
wantErr: false,
},
{
name: "append events across multiple calls",
session: "multiple-calls-session",
eventSets: [][]Event{
{{Kind: "message", Role: "user", Text: "Message 1"}},
{{Kind: "message", Role: "assistant", Text: "Response 1"}},
{{Kind: "message", Role: "user", Text: "Message 2"}},
{{Kind: "message", Role: "assistant", Text: "Response 2"}},
},
wantTotal: 4,
wantErr: false,
},
{
name: "append mixed events with metadata",
session: "mixed-events-session",
eventSets: [][]Event{
{
{Kind: "system", Text: "Session started"},
{Kind: "message", Role: "user", Text: "Hello", Meta: map[string]string{"lang": "en"}},
{Kind: "tool_use", Role: "assistant", Type: "bash", Text: "ls"},
{Kind: "tool_result", Role: "user", Type: "bash", Text: "file.txt"},
},
},
wantTotal: 4,
wantErr: false,
},
{
name: "append preserves order",
session: "order-session",
eventSets: [][]Event{
{
{Kind: "message", Role: "user", Text: "First"},
{Kind: "message", Role: "user", Text: "Second"},
{Kind: "message", Role: "user", Text: "Third"},
},
},
wantTotal: 3,
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tmpDir := t.TempDir()
store := NewFileStore(tmpDir)
for _, events := range tt.eventSets {
err := store.Append(tt.session, events)
if (err != nil) != tt.wantErr {
t.Errorf("Append() error = %v, wantErr %v", err, tt.wantErr)
return
}
}
// Read and verify the file
eventsPath := filepath.Join(tmpDir, "sessions", tt.session, "events.jsonl")
data, err := os.ReadFile(eventsPath)
if err != nil {
t.Fatalf("Failed to read events file: %v", err)
}
lines := strings.Split(strings.TrimSpace(string(data)), "\n")
if len(lines) != tt.wantTotal {
t.Errorf("Expected %d events, got %d", tt.wantTotal, len(lines))
}
// Verify each line is valid JSON and check order for order test
if tt.name == "append preserves order" {
expectedTexts := []string{"First", "Second", "Third"}
for i, line := range lines {
var e Event
if err := json.Unmarshal([]byte(line), &e); err != nil {
t.Errorf("Line %d is not valid JSON: %v", i, err)
continue
}
if e.Text != expectedTexts[i] {
t.Errorf("Event %d: expected text %q, got %q", i, expectedTexts[i], e.Text)
}
}
}
})
}
}
func TestFileStore_Append_RedactsSecrets(t *testing.T) {
tests := []struct {
name string
inputText string
wantRedact bool
}{
{
name: "redacts sk- API keys",
inputText: "My key is sk-1234567890abcdef",
wantRedact: true,
},
{
name: "redacts bearer tokens",
inputText: "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9",
wantRedact: true,
},
{
name: "redacts AIza keys",
inputText: "API key: AIzaSyB1234567890abcdefghij",
wantRedact: true,
},
{
name: "does not modify normal text",
inputText: "This is a normal message without secrets",
wantRedact: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tmpDir := t.TempDir()
store := NewFileStore(tmpDir)
session := "redact-test"
err := store.Append(session, []Event{
{Kind: "message", Text: tt.inputText},
})
if err != nil {
t.Fatalf("Append() error = %v", err)
}
// Read the stored event
eventsPath := filepath.Join(tmpDir, "sessions", session, "events.jsonl")
data, err := os.ReadFile(eventsPath)
if err != nil {
t.Fatalf("Failed to read events file: %v", err)
}
var e Event
if err := json.Unmarshal(data, &e); err != nil {
t.Fatalf("Failed to parse event: %v", err)
}
hasRedacted := strings.Contains(e.Text, "[REDACTED]")
if tt.wantRedact && !hasRedacted {
t.Errorf("Expected text to be redacted, got: %s", e.Text)
}
if !tt.wantRedact && hasRedacted {
t.Errorf("Text should not be redacted, got: %s", e.Text)
}
})
}
}
func TestFileStore_Search_ExactMatch(t *testing.T) {
tests := []struct {
name string
events []Event
query string
wantCount int
wantMatch string
}{
{
name: "finds exact keyword match",
events: []Event{
{Kind: "message", Role: "user", Text: "How do I configure the database?"},
{Kind: "message", Role: "assistant", Text: "You can configure the database in config.yaml"},
{Kind: "message", Role: "user", Text: "Thanks, what about logging?"},
},
query: "database",
wantCount: 2,
wantMatch: "database",
},
{
name: "case insensitive search",
events: []Event{
{Kind: "message", Text: "The DATABASE connection failed"},
{Kind: "message", Text: "Database retry succeeded"},
{Kind: "message", Text: "No matches here"},
},
query: "database",
wantCount: 2,
wantMatch: "",
},
{
name: "no matches returns empty",
events: []Event{
{Kind: "message", Text: "Hello world"},
{Kind: "message", Text: "Goodbye world"},
},
query: "nonexistent",
wantCount: 0,
wantMatch: "",
},
{
name: "multiple keywords - matches any keyword",
events: []Event{
{Kind: "message", Text: "Error in authentication module"},
{Kind: "message", Text: "Authentication failed for user"},
{Kind: "message", Text: "Module loaded successfully"},
},
query: "authentication module",
wantCount: 3, // all three contain at least one keyword
wantMatch: "",
},
{
name: "empty query returns nil",
events: []Event{
{Kind: "message", Text: "Some text"},
},
query: "",
wantCount: 0,
wantMatch: "",
},
{
name: "whitespace query returns nil",
events: []Event{
{Kind: "message", Text: "Some text"},
},
query: " ",
wantCount: 0,
wantMatch: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tmpDir := t.TempDir()
store := NewFileStore(tmpDir)
session := "search-test"
// Append test events
if err := store.Append(session, tt.events); err != nil {
t.Fatalf("Append() error = %v", err)
}
// Search
results, err := store.Search(session, tt.query, 6000, 10)
if err != nil {
t.Fatalf("Search() error = %v", err)
}
if len(results) != tt.wantCount {
t.Errorf("Search() returned %d results, want %d", len(results), tt.wantCount)
}
if tt.wantMatch != "" {
found := false
for _, r := range results {
if strings.Contains(strings.ToLower(r), tt.wantMatch) {
found = true
break
}
}
if !found {
t.Errorf("Search results should contain %q", tt.wantMatch)
}
}
})
}
}
func TestFileStore_Search_MaxSnippets(t *testing.T) {
tests := []struct {
name string
eventCount int
maxSnippets int
wantMax int
}{
{
name: "limits to maxSnippets",
eventCount: 20,
maxSnippets: 5,
wantMax: 5,
},
{
name: "returns all if under limit",
eventCount: 3,
maxSnippets: 10,
wantMax: 3,
},
{
name: "default maxSnippets when zero",
eventCount: 20,
maxSnippets: 0,
wantMax: 8, // default is 8
},
{
name: "maxSnippets of 1",
eventCount: 10,
maxSnippets: 1,
wantMax: 1,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tmpDir := t.TempDir()
store := NewFileStore(tmpDir)
session := "maxsnippets-test"
// Create events with searchable text
events := make([]Event, tt.eventCount)
for i := 0; i < tt.eventCount; i++ {
events[i] = Event{
Kind: "message",
Role: "user",
Text: "This is a searchable test message number " + string(rune('A'+i)),
}
}
if err := store.Append(session, events); err != nil {
t.Fatalf("Append() error = %v", err)
}
results, err := store.Search(session, "searchable test message", 100000, tt.maxSnippets)
if err != nil {
t.Fatalf("Search() error = %v", err)
}
if len(results) > tt.wantMax {
t.Errorf("Search() returned %d results, want at most %d", len(results), tt.wantMax)
}
})
}
}
func TestFileStore_Search_MaxChars(t *testing.T) {
tests := []struct {
name string
events []Event
maxChars int
wantUnder bool
}{
{
name: "respects maxChars limit",
events: []Event{
{Kind: "message", Text: strings.Repeat("searchable text ", 100)},
{Kind: "message", Text: strings.Repeat("more searchable text ", 100)},
},
maxChars: 500,
wantUnder: true,
},
{
name: "default maxChars when zero",
events: []Event{
{Kind: "message", Text: "searchable text"},
},
maxChars: 0,
wantUnder: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tmpDir := t.TempDir()
store := NewFileStore(tmpDir)
session := "maxchars-test"
if err := store.Append(session, tt.events); err != nil {
t.Fatalf("Append() error = %v", err)
}
results, err := store.Search(session, "searchable text", tt.maxChars, 100)
if err != nil {
t.Fatalf("Search() error = %v", err)
}
if tt.wantUnder {
totalChars := 0
for _, r := range results {
totalChars += len(r)
}
limit := tt.maxChars
if limit <= 0 {
limit = 6000 // default
}
if totalChars > limit {
t.Errorf("Total chars %d exceeds maxChars %d", totalChars, limit)
}
}
})
}
}
func TestFileStore_Search_NonExistentSession(t *testing.T) {
tmpDir := t.TempDir()
store := NewFileStore(tmpDir)
results, err := store.Search("nonexistent-session", "query", 6000, 10)
if err != nil {
t.Errorf("Search() should not error for nonexistent session, got: %v", err)
}
if len(results) != 0 {
t.Errorf("Search() should return empty for nonexistent session, got %d results", len(results))
}
}
func TestFileStore_NilStore(t *testing.T) {
var store *FileStore
t.Run("Append on nil store", func(t *testing.T) {
err := store.Append("session", []Event{{Kind: "message", Text: "test"}})
if err == nil {
t.Error("Expected error for nil store, got nil")
}
})
t.Run("Search on nil store", func(t *testing.T) {
_, err := store.Search("session", "query", 100, 10)
if err == nil {
t.Error("Expected error for nil store, got nil")
}
})
}
func TestFileStore_EmptyBaseDir(t *testing.T) {
store := NewFileStore("")
t.Run("Append with empty BaseDir", func(t *testing.T) {
err := store.Append("session", []Event{{Kind: "message", Text: "test"}})
if err == nil {
t.Error("Expected error for empty BaseDir, got nil")
}
})
t.Run("Search with empty BaseDir", func(t *testing.T) {
_, err := store.Search("session", "query", 100, 10)
if err == nil {
t.Error("Expected error for empty BaseDir, got nil")
}
})
}
func TestFileStore_SearchCache(t *testing.T) {
tmpDir := t.TempDir()
store := NewFileStore(tmpDir)
session := "cache-test"
events := []Event{
{Kind: "message", Role: "user", Text: "This is a cacheable message"},
}
if err := store.Append(session, events); err != nil {
t.Fatalf("Append() error = %v", err)
}
// First search populates cache
results1, err := store.Search(session, "cacheable", 6000, 10)
if err != nil {
t.Fatalf("Search() error = %v", err)
}
// Second search should use cache (same query)
results2, err := store.Search(session, "cacheable", 6000, 10)
if err != nil {
t.Fatalf("Search() error = %v", err)
}
if len(results1) != len(results2) {
t.Errorf("Cached result count mismatch: %d vs %d", len(results1), len(results2))
}
// Append invalidates cache
if err := store.Append(session, []Event{{Kind: "message", Text: "New cacheable event"}}); err != nil {
t.Fatalf("Append() error = %v", err)
}
// Search after append should get fresh results
results3, err := store.Search(session, "cacheable", 6000, 10)
if err != nil {
t.Fatalf("Search() error = %v", err)
}
if len(results3) <= len(results1) {
t.Logf("Results after append: %d (was %d)", len(results3), len(results1))
}
}
func TestFileStore_SessionDir(t *testing.T) {
tests := []struct {
name string
session string
wantDir bool
}{
{
name: "normal session name",
session: "my-session-123",
wantDir: true,
},
{
name: "session with special characters",
session: "session/with\\special:chars*",
wantDir: true,
},
{
name: "empty session",
session: "",
wantDir: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tmpDir := t.TempDir()
store := NewFileStore(tmpDir)
dir := store.SessionDir(tt.session)
if tt.wantDir && dir == "" {
t.Error("SessionDir() returned empty string")
}
if tt.wantDir && !strings.HasPrefix(dir, tmpDir) {
t.Errorf("SessionDir() = %q, should be under %q", dir, tmpDir)
}
})
}
}
+610
View File
@@ -0,0 +1,610 @@
package memory
import (
"os"
"path/filepath"
"strings"
"testing"
"time"
)
func TestPrune_ByTokenCount(t *testing.T) {
tests := []struct {
name string
sessionCount int
eventsPerSession int
maxBytesPerSession int64
wantSessionsTrimmed int
}{
{
name: "trims large sessions",
sessionCount: 2,
eventsPerSession: 100,
maxBytesPerSession: 500,
wantSessionsTrimmed: 2,
},
{
name: "no trimming needed for small sessions",
sessionCount: 2,
eventsPerSession: 2,
maxBytesPerSession: 100000,
wantSessionsTrimmed: 0,
},
{
name: "zero maxBytes skips trimming",
sessionCount: 2,
eventsPerSession: 100,
maxBytesPerSession: 0,
wantSessionsTrimmed: 0,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tmpDir := t.TempDir()
store := NewFileStore(tmpDir)
// Create sessions with events
for i := 0; i < tt.sessionCount; i++ {
session := "session-" + string(rune('A'+i))
events := make([]Event, tt.eventsPerSession)
for j := 0; j < tt.eventsPerSession; j++ {
events[j] = Event{
TS: time.Now(),
Kind: "message",
Role: "user",
Text: strings.Repeat("This is a test message with some content. ", 10),
}
}
if err := store.Append(session, events); err != nil {
t.Fatalf("Append() error = %v", err)
}
}
// Prune sessions
result, err := store.PruneSessions(0, 0, tt.maxBytesPerSession)
if err != nil {
t.Fatalf("PruneSessions() error = %v", err)
}
if result.SessionsTrimmed != tt.wantSessionsTrimmed {
t.Errorf("SessionsTrimmed = %d, want %d", result.SessionsTrimmed, tt.wantSessionsTrimmed)
}
if result.SessionsTrimmed > 0 && result.BytesFreed <= 0 {
t.Error("Expected BytesFreed > 0 when sessions are trimmed")
}
})
}
}
func TestPrune_PreservesRecent(t *testing.T) {
tests := []struct {
name string
sessionAges []int // days ago
maxAgeDays int
maxSessions int
wantRemoved int
wantKept []string
}{
{
name: "preserves recent sessions by age",
sessionAges: []int{1, 5, 10, 20, 40}, // days ago
maxAgeDays: 30,
maxSessions: 0,
wantRemoved: 1, // only 40 days old is removed
wantKept: []string{"session-0", "session-1", "session-2", "session-3"},
},
{
name: "preserves by max sessions count",
sessionAges: []int{1, 2, 3, 4, 5},
maxAgeDays: 0,
maxSessions: 3,
wantRemoved: 2, // keeps 3 most recent
wantKept: []string{"session-0", "session-1", "session-2"},
},
{
name: "combines age and count limits",
sessionAges: []int{1, 5, 35, 40},
maxAgeDays: 30,
maxSessions: 2,
wantRemoved: 2, // 35 and 40 days old are removed
wantKept: []string{"session-0", "session-1"},
},
{
name: "no pruning when all within limits",
sessionAges: []int{1, 2, 3},
maxAgeDays: 30,
maxSessions: 10,
wantRemoved: 0,
wantKept: []string{"session-0", "session-1", "session-2"},
},
{
name: "removes all old sessions",
sessionAges: []int{100, 200, 300},
maxAgeDays: 30,
maxSessions: 0,
wantRemoved: 3,
wantKept: []string{},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tmpDir := t.TempDir()
store := NewFileStore(tmpDir)
// Create sessions with different ages
for i, daysAgo := range tt.sessionAges {
session := "session-" + string(rune('0'+i))
sessionDir := filepath.Join(tmpDir, "sessions", session)
if err := os.MkdirAll(sessionDir, 0o755); err != nil {
t.Fatalf("Failed to create session dir: %v", err)
}
// Create events file
eventsPath := filepath.Join(sessionDir, "events.jsonl")
if err := os.WriteFile(eventsPath, []byte(`{"kind":"message","text":"test"}`+"\n"), 0o644); err != nil {
t.Fatalf("Failed to create events file: %v", err)
}
// Set modification time
modTime := time.Now().Add(-time.Duration(daysAgo) * 24 * time.Hour)
if err := os.Chtimes(eventsPath, modTime, modTime); err != nil {
t.Fatalf("Failed to set file times: %v", err)
}
}
// Prune sessions
result, err := store.PruneSessions(tt.maxAgeDays, tt.maxSessions, 0)
if err != nil {
t.Fatalf("PruneSessions() error = %v", err)
}
if result.SessionsRemoved != tt.wantRemoved {
t.Errorf("SessionsRemoved = %d, want %d", result.SessionsRemoved, tt.wantRemoved)
}
// Verify kept sessions exist
for _, session := range tt.wantKept {
sessionDir := filepath.Join(tmpDir, "sessions", session)
if _, err := os.Stat(sessionDir); os.IsNotExist(err) {
t.Errorf("Session %q should have been preserved but was removed", session)
}
}
// Verify removed sessions don't exist
sessions, _ := store.ListSessions(0)
if len(sessions) != len(tt.wantKept) {
t.Errorf("Expected %d sessions remaining, got %d", len(tt.wantKept), len(sessions))
}
})
}
}
func TestPrune_SemanticNamespaces(t *testing.T) {
tests := []struct {
name string
namespaceCount int
recordsPerNs int
maxAgeDays int
maxNamespaces int
maxBytesPerNs int64
wantRemoved int
wantTrimmed int
}{
{
name: "removes old namespaces",
namespaceCount: 3,
recordsPerNs: 5,
maxAgeDays: 30,
maxNamespaces: 0,
maxBytesPerNs: 0,
wantRemoved: 0, // all are recent
wantTrimmed: 0,
},
{
name: "limits namespace count",
namespaceCount: 5,
recordsPerNs: 5,
maxAgeDays: 0,
maxNamespaces: 3,
maxBytesPerNs: 0,
wantRemoved: 2,
wantTrimmed: 0,
},
{
name: "trims large namespaces",
namespaceCount: 2,
recordsPerNs: 500,
maxAgeDays: 0,
maxNamespaces: 0,
maxBytesPerNs: 2000,
wantRemoved: 0,
wantTrimmed: 2,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tmpDir := t.TempDir()
store := NewFileStore(tmpDir)
// Create namespaces with records
for i := 0; i < tt.namespaceCount; i++ {
namespace := "namespace-" + string(rune('A'+i))
records := make([]SemanticRecord, tt.recordsPerNs)
for j := 0; j < tt.recordsPerNs; j++ {
// Make each text unique to avoid deduplication
records[j] = SemanticRecord{
TS: time.Now(),
Text: strings.Repeat("Test record content ", 20) + " " + string(rune('A'+i)) + string(rune('0'+j%10)),
Vec: []float32{float32(i), float32(j), 0.5},
}
}
if err := store.AppendSemantic(namespace, records); err != nil {
t.Fatalf("AppendSemantic() error = %v", err)
}
// Add small delay to ensure different modification times
time.Sleep(10 * time.Millisecond)
}
// Prune semantic namespaces
result, err := store.PruneSemantic(tt.maxAgeDays, tt.maxNamespaces, tt.maxBytesPerNs)
if err != nil {
t.Fatalf("PruneSemantic() error = %v", err)
}
if result.SemanticNamespacesRemoved != tt.wantRemoved {
t.Errorf("SemanticNamespacesRemoved = %d, want %d", result.SemanticNamespacesRemoved, tt.wantRemoved)
}
if result.SemanticNamespacesTrimmed != tt.wantTrimmed {
t.Errorf("SemanticNamespacesTrimmed = %d, want %d", result.SemanticNamespacesTrimmed, tt.wantTrimmed)
}
})
}
}
func TestTrimJSONLFile(t *testing.T) {
tests := []struct {
name string
content string
maxBytes int64
wantTrim bool
wantFreed bool
}{
{
name: "trims file exceeding maxBytes",
content: `{"a":1}` + "\n" + `{"b":2}` + "\n" + `{"c":3}` + "\n" + `{"d":4}` + "\n",
maxBytes: 20,
wantTrim: true,
wantFreed: true,
},
{
name: "no trim needed for small file",
content: `{"a":1}` + "\n",
maxBytes: 1000,
wantTrim: false,
wantFreed: false,
},
{
name: "zero maxBytes skips trimming",
content: `{"a":1}` + "\n" + `{"b":2}` + "\n",
maxBytes: 0,
wantTrim: false,
wantFreed: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tmpDir := t.TempDir()
path := filepath.Join(tmpDir, "test.jsonl")
if err := os.WriteFile(path, []byte(tt.content), 0o644); err != nil {
t.Fatalf("Failed to write test file: %v", err)
}
originalSize := int64(len(tt.content))
trimmed, freed := trimJSONLFile(path, tt.maxBytes)
if trimmed != tt.wantTrim {
t.Errorf("trimJSONLFile() trimmed = %v, want %v", trimmed, tt.wantTrim)
}
if tt.wantFreed && freed <= 0 {
t.Errorf("Expected freed > 0, got %d", freed)
}
if !tt.wantFreed && freed != 0 {
t.Errorf("Expected freed = 0, got %d", freed)
}
// Verify file was actually trimmed
if tt.wantTrim {
data, err := os.ReadFile(path)
if err != nil {
t.Fatalf("Failed to read trimmed file: %v", err)
}
if int64(len(data)) >= originalSize {
t.Errorf("File was not actually trimmed: %d >= %d", len(data), originalSize)
}
}
})
}
}
func TestPruneResult_Fields(t *testing.T) {
result := PruneResult{
SessionsRemoved: 5,
SessionsTrimmed: 3,
SemanticNamespacesRemoved: 2,
SemanticNamespacesTrimmed: 1,
BytesFreed: 1024 * 1024,
}
if result.SessionsRemoved != 5 {
t.Errorf("SessionsRemoved = %d, want 5", result.SessionsRemoved)
}
if result.SessionsTrimmed != 3 {
t.Errorf("SessionsTrimmed = %d, want 3", result.SessionsTrimmed)
}
if result.SemanticNamespacesRemoved != 2 {
t.Errorf("SemanticNamespacesRemoved = %d, want 2", result.SemanticNamespacesRemoved)
}
if result.SemanticNamespacesTrimmed != 1 {
t.Errorf("SemanticNamespacesTrimmed = %d, want 1", result.SemanticNamespacesTrimmed)
}
if result.BytesFreed != 1024*1024 {
t.Errorf("BytesFreed = %d, want %d", result.BytesFreed, 1024*1024)
}
}
func TestSessionInfo_Fields(t *testing.T) {
now := time.Now()
info := SessionInfo{
Key: "test-session",
Path: "/path/to/session",
UpdatedAt: now,
SizeBytes: 2048,
EventsBytes: 1024,
HasSummary: true,
HasTodo: true,
HasPinned: false,
HasAnchorPending: true,
SemanticDisabled: false,
}
if info.Key != "test-session" {
t.Errorf("Key = %q, want %q", info.Key, "test-session")
}
if info.Path != "/path/to/session" {
t.Errorf("Path = %q, want %q", info.Path, "/path/to/session")
}
if !info.UpdatedAt.Equal(now) {
t.Errorf("UpdatedAt mismatch")
}
if info.SizeBytes != 2048 {
t.Errorf("SizeBytes = %d, want 2048", info.SizeBytes)
}
if info.EventsBytes != 1024 {
t.Errorf("EventsBytes = %d, want 1024", info.EventsBytes)
}
if !info.HasSummary {
t.Error("HasSummary should be true")
}
if !info.HasTodo {
t.Error("HasTodo should be true")
}
if info.HasPinned {
t.Error("HasPinned should be false")
}
if !info.HasAnchorPending {
t.Error("HasAnchorPending should be true")
}
if info.SemanticDisabled {
t.Error("SemanticDisabled should be false")
}
}
func TestListSessions(t *testing.T) {
tmpDir := t.TempDir()
store := NewFileStore(tmpDir)
// Create some sessions
sessions := []string{"alpha", "beta", "gamma"}
for _, session := range sessions {
events := []Event{{Kind: "message", Text: "test"}}
if err := store.Append(session, events); err != nil {
t.Fatalf("Append() error = %v", err)
}
// Small delay to ensure different mod times
time.Sleep(10 * time.Millisecond)
}
// List all sessions
list, err := store.ListSessions(0)
if err != nil {
t.Fatalf("ListSessions() error = %v", err)
}
if len(list) != len(sessions) {
t.Errorf("ListSessions() returned %d, want %d", len(list), len(sessions))
}
// List with limit
limited, err := store.ListSessions(2)
if err != nil {
t.Fatalf("ListSessions() error = %v", err)
}
if len(limited) != 2 {
t.Errorf("ListSessions(2) returned %d, want 2", len(limited))
}
// Verify most recent is first (gamma was created last)
if len(limited) > 0 && limited[0].Key != "gamma" {
t.Errorf("Most recent session should be 'gamma', got %q", limited[0].Key)
}
}
func TestGetSessionInfo(t *testing.T) {
tmpDir := t.TempDir()
store := NewFileStore(tmpDir)
session := "info-test"
// Create session with various files
events := []Event{{Kind: "message", Text: "test event"}}
if err := store.Append(session, events); err != nil {
t.Fatalf("Append() error = %v", err)
}
if err := store.WriteSummary(session, "Test summary", 0); err != nil {
t.Fatalf("WriteSummary() error = %v", err)
}
if err := store.WriteTodo(session, "Test todo", 0); err != nil {
t.Fatalf("WriteTodo() error = %v", err)
}
// Get session info
info, err := store.GetSessionInfo(session)
if err != nil {
t.Fatalf("GetSessionInfo() error = %v", err)
}
if info.Key != session {
t.Errorf("Key = %q, want %q", info.Key, session)
}
if !info.HasSummary {
t.Error("HasSummary should be true")
}
if !info.HasTodo {
t.Error("HasTodo should be true")
}
if info.SizeBytes <= 0 {
t.Error("SizeBytes should be > 0")
}
if info.EventsBytes <= 0 {
t.Error("EventsBytes should be > 0")
}
}
func TestReadEventTail(t *testing.T) {
tmpDir := t.TempDir()
store := NewFileStore(tmpDir)
session := "tail-test"
// Create many events
events := make([]Event, 20)
for i := 0; i < 20; i++ {
events[i] = Event{
Kind: "message",
Role: "user",
Text: "Message " + string(rune('A'+i)),
}
}
if err := store.Append(session, events); err != nil {
t.Fatalf("Append() error = %v", err)
}
// Read tail with limit
tail, err := store.ReadEventTail(session, 5)
if err != nil {
t.Fatalf("ReadEventTail() error = %v", err)
}
if len(tail) != 5 {
t.Errorf("ReadEventTail() returned %d events, want 5", len(tail))
}
// Verify chronological order (oldest first in result)
if len(tail) > 0 {
// Should be last 5 events: P, Q, R, S, T
expectedFirst := "Message P"
if tail[0].Text != expectedFirst {
t.Errorf("First event text = %q, want %q", tail[0].Text, expectedFirst)
}
}
}
func TestExportSessionZip(t *testing.T) {
tmpDir := t.TempDir()
store := NewFileStore(tmpDir)
session := "export-test"
// Create session with content
events := []Event{{Kind: "message", Text: "test event"}}
if err := store.Append(session, events); err != nil {
t.Fatalf("Append() error = %v", err)
}
if err := store.WriteSummary(session, "Test summary", 0); err != nil {
t.Fatalf("WriteSummary() error = %v", err)
}
// Export as zip
sessionDir := store.SessionDir(session)
zipData, err := ExportSessionZip(sessionDir)
if err != nil {
t.Fatalf("ExportSessionZip() error = %v", err)
}
if len(zipData) == 0 {
t.Error("Expected non-empty zip data")
}
// Verify it's a valid zip (starts with PK)
if len(zipData) < 2 || zipData[0] != 'P' || zipData[1] != 'K' {
t.Error("Expected valid zip file header")
}
}
func TestExportSessionZip_EmptyDir(t *testing.T) {
_, err := ExportSessionZip("")
if err == nil {
t.Error("Expected error for empty directory")
}
}
func TestExportAllZip(t *testing.T) {
tmpDir := t.TempDir()
store := NewFileStore(tmpDir)
// Create multiple sessions
for _, session := range []string{"session-1", "session-2"} {
events := []Event{{Kind: "message", Text: "test in " + session}}
if err := store.Append(session, events); err != nil {
t.Fatalf("Append() error = %v", err)
}
}
// Export all
zipData, err := ExportAllZip(tmpDir, 10*1024*1024)
if err != nil {
t.Fatalf("ExportAllZip() error = %v", err)
}
if len(zipData) == 0 {
t.Error("Expected non-empty zip data")
}
}
func TestExportAllZip_SizeLimit(t *testing.T) {
tmpDir := t.TempDir()
store := NewFileStore(tmpDir)
// Create session with large content
events := []Event{{Kind: "message", Text: strings.Repeat("x", 10000)}}
if err := store.Append("large-session", events); err != nil {
t.Fatalf("Append() error = %v", err)
}
// Try to export with very small limit
_, err := ExportAllZip(tmpDir, 100)
if err == nil {
t.Error("Expected error when size exceeds limit")
}
}
+826
View File
@@ -0,0 +1,826 @@
package memory
import (
"encoding/json"
"math"
"os"
"path/filepath"
"strings"
"testing"
"time"
)
func TestSemanticStore_Append(t *testing.T) {
tests := []struct {
name string
namespace string
records []SemanticRecord
wantErr bool
wantCount int
}{
{
name: "appends single record",
namespace: "test-namespace",
records: []SemanticRecord{
{
TS: time.Now(),
Role: "user",
Text: "Hello, world!",
Vec: []float32{0.1, 0.2, 0.3},
},
},
wantErr: false,
wantCount: 1,
},
{
name: "appends multiple records",
namespace: "multi-record-ns",
records: []SemanticRecord{
{Text: "First message", Vec: []float32{0.1, 0.2, 0.3}},
{Text: "Second message", Vec: []float32{0.4, 0.5, 0.6}},
{Text: "Third message", Vec: []float32{0.7, 0.8, 0.9}},
},
wantErr: false,
wantCount: 3,
},
{
name: "skips records with empty text",
namespace: "skip-empty-ns",
records: []SemanticRecord{
{Text: "Valid text", Vec: []float32{0.1, 0.2, 0.3}},
{Text: "", Vec: []float32{0.4, 0.5, 0.6}},
{Text: " ", Vec: []float32{0.7, 0.8, 0.9}},
},
wantErr: false,
wantCount: 1,
},
{
name: "skips records with empty vector",
namespace: "skip-empty-vec-ns",
records: []SemanticRecord{
{Text: "Valid", Vec: []float32{0.1, 0.2, 0.3}},
{Text: "No vector", Vec: []float32{}},
{Text: "Nil vector", Vec: nil},
},
wantErr: false,
wantCount: 1,
},
{
name: "skips duplicate texts",
namespace: "skip-dupe-ns",
records: []SemanticRecord{
{Text: "Same text", Vec: []float32{0.1, 0.2, 0.3}},
{Text: "Same text", Vec: []float32{0.4, 0.5, 0.6}},
{Text: "Different text", Vec: []float32{0.7, 0.8, 0.9}},
},
wantErr: false,
wantCount: 2,
},
{
name: "empty namespace returns nil",
namespace: "",
records: []SemanticRecord{
{Text: "Test", Vec: []float32{0.1, 0.2, 0.3}},
},
wantErr: false,
wantCount: 0,
},
{
name: "empty records returns nil",
namespace: "empty-records-ns",
records: []SemanticRecord{},
wantErr: false,
wantCount: 0,
},
{
name: "computes norm if not provided",
namespace: "compute-norm-ns",
records: []SemanticRecord{
{Text: "Norm will be computed", Vec: []float32{3, 4}, Norm: 0},
},
wantErr: false,
wantCount: 1,
},
{
name: "uses provided norm",
namespace: "provided-norm-ns",
records: []SemanticRecord{
{Text: "Norm is provided", Vec: []float32{3, 4}, Norm: 5.0},
},
wantErr: false,
wantCount: 1,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tmpDir := t.TempDir()
store := NewFileStore(tmpDir)
err := store.AppendSemantic(tt.namespace, tt.records)
if (err != nil) != tt.wantErr {
t.Errorf("AppendSemantic() error = %v, wantErr %v", err, tt.wantErr)
return
}
if tt.namespace == "" || len(tt.records) == 0 {
return
}
// Verify file content
dir := filepath.Join(tmpDir, "semantic")
entries, err := os.ReadDir(dir)
if err != nil {
if tt.wantCount == 0 {
return // No file expected
}
t.Fatalf("Failed to read semantic dir: %v", err)
}
if len(entries) == 0 && tt.wantCount > 0 {
t.Errorf("Expected semantic namespace directory to be created")
return
}
// Find the items.jsonl file
var itemsPath string
for _, e := range entries {
if e.IsDir() {
itemsPath = filepath.Join(dir, e.Name(), "items.jsonl")
break
}
}
if itemsPath == "" {
if tt.wantCount > 0 {
t.Error("items.jsonl not found")
}
return
}
data, err := os.ReadFile(itemsPath)
if err != nil {
t.Fatalf("Failed to read items.jsonl: %v", err)
}
lines := strings.Split(strings.TrimSpace(string(data)), "\n")
actualCount := 0
for _, line := range lines {
if strings.TrimSpace(line) != "" {
actualCount++
}
}
if actualCount != tt.wantCount {
t.Errorf("Expected %d records, got %d", tt.wantCount, actualCount)
}
})
}
}
func TestSemanticStore_Search(t *testing.T) {
tests := []struct {
name string
records []SemanticRecord
queryVec []float32
maxChars int
maxSnippets int
wantCount int
wantFirst string
}{
{
name: "finds similar vectors",
records: []SemanticRecord{
{Text: "Apple is a fruit", Vec: []float32{0.9, 0.1, 0.0}},
{Text: "Car is a vehicle", Vec: []float32{0.1, 0.9, 0.0}},
{Text: "Orange is a fruit", Vec: []float32{0.85, 0.15, 0.0}},
},
queryVec: []float32{0.9, 0.1, 0.0},
maxChars: 6000,
maxSnippets: 10,
wantCount: 3,
wantFirst: "Apple is a fruit",
},
{
name: "returns empty for no matches",
records: []SemanticRecord{
{Text: "Text 1", Vec: []float32{1, 0, 0}},
{Text: "Text 2", Vec: []float32{0, 1, 0}},
},
queryVec: []float32{0, 0, 1},
maxChars: 6000,
maxSnippets: 10,
wantCount: 0,
wantFirst: "",
},
{
name: "respects maxSnippets",
records: []SemanticRecord{
{Text: "Text 1", Vec: []float32{0.9, 0.1, 0.0}},
{Text: "Text 2", Vec: []float32{0.85, 0.15, 0.0}},
{Text: "Text 3", Vec: []float32{0.8, 0.2, 0.0}},
{Text: "Text 4", Vec: []float32{0.75, 0.25, 0.0}},
},
queryVec: []float32{0.9, 0.1, 0.0},
maxChars: 6000,
maxSnippets: 2,
wantCount: 2,
wantFirst: "Text 1",
},
{
name: "empty query vector returns nil",
records: []SemanticRecord{
{Text: "Some text", Vec: []float32{0.1, 0.2, 0.3}},
},
queryVec: []float32{},
maxChars: 6000,
maxSnippets: 10,
wantCount: 0,
wantFirst: "",
},
{
name: "orders by similarity score",
records: []SemanticRecord{
{Text: "Low similarity", Vec: []float32{0.1, 0.9, 0.0}},
{Text: "High similarity", Vec: []float32{0.95, 0.05, 0.0}},
{Text: "Medium similarity", Vec: []float32{0.5, 0.5, 0.0}},
},
queryVec: []float32{1.0, 0.0, 0.0},
maxChars: 6000,
maxSnippets: 10,
wantCount: 3,
wantFirst: "High similarity",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tmpDir := t.TempDir()
store := NewFileStore(tmpDir)
namespace := "search-test-ns"
// Append records
if len(tt.records) > 0 {
if err := store.AppendSemantic(namespace, tt.records); err != nil {
t.Fatalf("AppendSemantic() error = %v", err)
}
}
// Search
results, err := store.SearchSemantic(namespace, tt.queryVec, tt.maxChars, tt.maxSnippets)
if err != nil {
t.Fatalf("SearchSemantic() error = %v", err)
}
if len(results) != tt.wantCount {
t.Errorf("SearchSemantic() returned %d results, want %d", len(results), tt.wantCount)
}
if tt.wantFirst != "" && len(results) > 0 {
if results[0] != tt.wantFirst {
t.Errorf("First result = %q, want %q", results[0], tt.wantFirst)
}
}
})
}
}
func TestSemanticStore_SearchWithText(t *testing.T) {
tmpDir := t.TempDir()
store := NewFileStore(tmpDir)
namespace := "text-search-ns"
records := []SemanticRecord{
{Text: "Python programming tutorial", Vec: []float32{0.9, 0.1, 0.0}},
{Text: "Java development guide", Vec: []float32{0.85, 0.15, 0.0}},
{Text: "Python data science course", Vec: []float32{0.88, 0.12, 0.0}},
}
if err := store.AppendSemantic(namespace, records); err != nil {
t.Fatalf("AppendSemantic() error = %v", err)
}
// Search with both vector and text (keyword boost)
results, err := store.SearchSemanticWithText(namespace, []float32{0.9, 0.1, 0.0}, "python", 6000, 10)
if err != nil {
t.Fatalf("SearchSemanticWithText() error = %v", err)
}
if len(results) == 0 {
t.Error("Expected results from SearchSemanticWithText")
}
// Python results should be boosted
pythonCount := 0
for _, r := range results {
if strings.Contains(strings.ToLower(r), "python") {
pythonCount++
}
}
if pythonCount == 0 {
t.Error("Expected Python-related results to be present")
}
}
func TestEmbedding_Normalize(t *testing.T) {
tests := []struct {
name string
vec []float32
wantNorm float32
wantZero bool
}{
{
name: "unit vector",
vec: []float32{1, 0, 0},
wantNorm: 1.0,
wantZero: false,
},
{
name: "3-4-5 right triangle",
vec: []float32{3, 4},
wantNorm: 5.0,
wantZero: false,
},
{
name: "all zeros",
vec: []float32{0, 0, 0},
wantNorm: 0,
wantZero: true,
},
{
name: "empty vector",
vec: []float32{},
wantNorm: 0,
wantZero: true,
},
{
name: "negative values",
vec: []float32{-3, 4},
wantNorm: 5.0,
wantZero: false,
},
{
name: "high dimensional",
vec: []float32{1, 1, 1, 1},
wantNorm: 2.0, // sqrt(4) = 2
wantZero: false,
},
{
name: "small values",
vec: []float32{0.001, 0.001, 0.001},
wantNorm: float32(math.Sqrt(0.000003)),
wantZero: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
norm := vectorNorm(tt.vec)
if tt.wantZero && norm != 0 {
t.Errorf("vectorNorm() = %v, want 0", norm)
return
}
if !tt.wantZero {
tolerance := float32(0.0001)
if diff := math.Abs(float64(norm - tt.wantNorm)); diff > float64(tolerance) {
t.Errorf("vectorNorm() = %v, want %v (diff: %v)", norm, tt.wantNorm, diff)
}
}
})
}
}
func TestEmbedding_CosineSimilarity(t *testing.T) {
tests := []struct {
name string
vecA []float32
normA float32
vecB []float32
normB float32
wantSim float32
tolerance float32
}{
{
name: "identical vectors",
vecA: []float32{1, 0, 0},
normA: 1.0,
vecB: []float32{1, 0, 0},
normB: 1.0,
wantSim: 1.0,
tolerance: 0.0001,
},
{
name: "orthogonal vectors",
vecA: []float32{1, 0, 0},
normA: 1.0,
vecB: []float32{0, 1, 0},
normB: 1.0,
wantSim: 0.0,
tolerance: 0.0001,
},
{
name: "opposite vectors",
vecA: []float32{1, 0, 0},
normA: 1.0,
vecB: []float32{-1, 0, 0},
normB: 1.0,
wantSim: -1.0,
tolerance: 0.0001,
},
{
name: "45 degree angle",
vecA: []float32{1, 0},
normA: 1.0,
vecB: []float32{1, 1},
normB: float32(math.Sqrt(2)),
wantSim: float32(1.0 / math.Sqrt(2)), // cos(45) = 1/sqrt(2)
tolerance: 0.0001,
},
{
name: "non-unit vectors",
vecA: []float32{3, 4},
normA: 5.0,
vecB: []float32{6, 8},
normB: 10.0,
wantSim: 1.0, // Same direction
tolerance: 0.0001,
},
{
name: "zero norm A returns 0",
vecA: []float32{0, 0, 0},
normA: 0,
vecB: []float32{1, 0, 0},
normB: 1.0,
wantSim: 0,
tolerance: 0,
},
{
name: "zero norm B returns 0",
vecA: []float32{1, 0, 0},
normA: 1.0,
vecB: []float32{0, 0, 0},
normB: 0,
wantSim: 0,
tolerance: 0,
},
{
name: "different length vectors",
vecA: []float32{1, 2, 3, 4, 5},
normA: vectorNorm([]float32{1, 2, 3, 4, 5}),
vecB: []float32{1, 2, 3},
normB: vectorNorm([]float32{1, 2, 3}),
wantSim: float32(14.0) / (vectorNorm([]float32{1, 2, 3, 4, 5}) * vectorNorm([]float32{1, 2, 3})),
tolerance: 0.0001,
},
{
name: "high dimensional similar",
vecA: []float32{0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8},
normA: vectorNorm([]float32{0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8}),
vecB: []float32{0.15, 0.25, 0.35, 0.45, 0.55, 0.65, 0.75, 0.85},
normB: vectorNorm([]float32{0.15, 0.25, 0.35, 0.45, 0.55, 0.65, 0.75, 0.85}),
wantSim: 0.999, // Very similar
tolerance: 0.01,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
sim := cosineSim(tt.vecA, tt.normA, tt.vecB, tt.normB)
diff := math.Abs(float64(sim - tt.wantSim))
if diff > float64(tt.tolerance) {
t.Errorf("cosineSim() = %v, want %v (diff: %v, tolerance: %v)", sim, tt.wantSim, diff, tt.tolerance)
}
})
}
}
func TestSemanticRecord_Serialization(t *testing.T) {
tests := []struct {
name string
record SemanticRecord
}{
{
name: "full record",
record: SemanticRecord{
TS: time.Date(2024, 1, 15, 10, 30, 0, 0, time.UTC),
Role: "user",
Text: "Test message",
Vec: []float32{0.1, 0.2, 0.3},
Norm: 0.374,
Source: "chat",
Session: "session-123",
Repo: "org/repo",
},
},
{
name: "minimal record",
record: SemanticRecord{
Text: "Minimal",
Vec: []float32{1.0},
Norm: 1.0,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
data, err := json.Marshal(tt.record)
if err != nil {
t.Fatalf("json.Marshal() error = %v", err)
}
var decoded SemanticRecord
if err := json.Unmarshal(data, &decoded); err != nil {
t.Fatalf("json.Unmarshal() error = %v", err)
}
if decoded.Text != tt.record.Text {
t.Errorf("Text mismatch: got %q, want %q", decoded.Text, tt.record.Text)
}
if len(decoded.Vec) != len(tt.record.Vec) {
t.Errorf("Vec length mismatch: got %d, want %d", len(decoded.Vec), len(tt.record.Vec))
}
if decoded.Norm != tt.record.Norm {
t.Errorf("Norm mismatch: got %v, want %v", decoded.Norm, tt.record.Norm)
}
})
}
}
func TestSemanticStore_ReadSemanticTail(t *testing.T) {
tmpDir := t.TempDir()
store := NewFileStore(tmpDir)
namespace := "tail-test-ns"
// Append multiple records
records := make([]SemanticRecord, 10)
for i := 0; i < 10; i++ {
records[i] = SemanticRecord{
TS: time.Now().Add(time.Duration(i) * time.Minute),
Text: "Message " + string(rune('A'+i)),
Vec: []float32{float32(i), float32(i + 1), float32(i + 2)},
}
}
if err := store.AppendSemantic(namespace, records); err != nil {
t.Fatalf("AppendSemantic() error = %v", err)
}
// Read tail with limit
tail, err := store.ReadSemanticTail(namespace, 5)
if err != nil {
t.Fatalf("ReadSemanticTail() error = %v", err)
}
if len(tail) != 5 {
t.Errorf("ReadSemanticTail() returned %d records, want 5", len(tail))
}
// Verify order (should be chronological, last 5 records)
if len(tail) > 0 {
// First in tail should be 6th record (index 5)
if !strings.Contains(tail[0].Text, "Message F") {
t.Errorf("First record = %q, expected Message F", tail[0].Text)
}
}
}
func TestSemanticStore_ListNamespaces(t *testing.T) {
tmpDir := t.TempDir()
store := NewFileStore(tmpDir)
namespaces := []string{"ns-alpha", "ns-beta", "ns-gamma"}
for _, ns := range namespaces {
records := []SemanticRecord{
{Text: "Record in " + ns, Vec: []float32{0.1, 0.2, 0.3}},
}
if err := store.AppendSemantic(ns, records); err != nil {
t.Fatalf("AppendSemantic() error = %v", err)
}
}
// List all namespaces
list, err := store.ListSemanticNamespaces(0)
if err != nil {
t.Fatalf("ListSemanticNamespaces() error = %v", err)
}
if len(list) != len(namespaces) {
t.Errorf("ListSemanticNamespaces() returned %d, want %d", len(list), len(namespaces))
}
// List with limit
limited, err := store.ListSemanticNamespaces(2)
if err != nil {
t.Fatalf("ListSemanticNamespaces() error = %v", err)
}
if len(limited) != 2 {
t.Errorf("ListSemanticNamespaces(2) returned %d, want 2", len(limited))
}
}
func TestTokenizeSemanticQuery(t *testing.T) {
tests := []struct {
name string
query string
max int
want []string
wantN int
}{
{
name: "basic tokenization",
query: "Hello World",
max: 10,
want: []string{"hello", "world"},
wantN: 2,
},
{
name: "removes stop words",
query: "the quick brown fox and the lazy dog",
max: 10,
want: []string{"quick", "brown", "fox", "lazy", "dog"},
wantN: 5,
},
{
name: "respects max limit",
query: "one two three four five six seven",
max: 3,
want: []string{"one", "two", "three"},
wantN: 3,
},
{
name: "removes short words",
query: "I am a test is it ok",
max: 10,
want: []string{"test"},
wantN: 1,
},
{
name: "handles special characters",
query: "hello-world test_case foo.bar",
max: 10,
want: []string{"hello-world", "test_case", "foo", "bar"},
wantN: 4,
},
{
name: "empty query",
query: "",
max: 10,
want: nil,
wantN: 0,
},
{
name: "only stop words",
query: "the and for with",
max: 10,
want: nil,
wantN: 0,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tokens := tokenizeSemanticQuery(tt.query, tt.max)
if len(tokens) != tt.wantN {
t.Errorf("tokenizeSemanticQuery() returned %d tokens, want %d", len(tokens), tt.wantN)
}
if tt.want != nil {
for i, want := range tt.want {
if i >= len(tokens) {
t.Errorf("Missing token at index %d: want %q", i, want)
continue
}
if tokens[i] != want {
t.Errorf("Token %d = %q, want %q", i, tokens[i], want)
}
}
}
})
}
}
func TestSemanticTokenOverlap(t *testing.T) {
tests := []struct {
name string
tokens []string
text string
want int
}{
{
name: "all tokens match",
tokens: []string{"hello", "world"},
text: "Hello World Test",
want: 2,
},
{
name: "some tokens match",
tokens: []string{"hello", "world", "foo"},
text: "Hello bar baz",
want: 1,
},
{
name: "no tokens match",
tokens: []string{"foo", "bar"},
text: "Hello World",
want: 0,
},
{
name: "empty tokens",
tokens: []string{},
text: "Hello World",
want: 0,
},
{
name: "empty text",
tokens: []string{"hello"},
text: "",
want: 0,
},
{
name: "case insensitive",
tokens: []string{"hello"},
text: "HELLO World",
want: 1,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
overlap := semanticTokenOverlap(tt.tokens, tt.text)
if overlap != tt.want {
t.Errorf("semanticTokenOverlap() = %d, want %d", overlap, tt.want)
}
})
}
}
func TestSemanticRecencyScore(t *testing.T) {
now := time.Now()
tests := []struct {
name string
ts time.Time
windowDays int
wantMin float32
wantMax float32
}{
{
name: "current time",
ts: now,
windowDays: 30,
wantMin: 0.99,
wantMax: 1.0,
},
{
name: "half window age",
ts: now.Add(-15 * 24 * time.Hour),
windowDays: 30,
wantMin: 0.4,
wantMax: 0.6,
},
{
name: "at window edge",
ts: now.Add(-30 * 24 * time.Hour),
windowDays: 30,
wantMin: 0.0,
wantMax: 0.01,
},
{
name: "beyond window",
ts: now.Add(-60 * 24 * time.Hour),
windowDays: 30,
wantMin: 0.0,
wantMax: 0.0,
},
{
name: "zero time",
ts: time.Time{},
windowDays: 30,
wantMin: 0.0,
wantMax: 0.0,
},
{
name: "zero window",
ts: now,
windowDays: 0,
wantMin: 0.0,
wantMax: 0.0,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
score := semanticRecencyScore(now, tt.ts, tt.windowDays)
if score < tt.wantMin || score > tt.wantMax {
t.Errorf("semanticRecencyScore() = %v, want between %v and %v", score, tt.wantMin, tt.wantMax)
}
})
}
}
+365
View File
@@ -0,0 +1,365 @@
package memory
import (
"encoding/json"
"testing"
"time"
)
func TestEvent_JSON_Serialization(t *testing.T) {
tests := []struct {
name string
event Event
wantErr bool
}{
{
name: "full event with all fields",
event: Event{
TS: time.Date(2024, 1, 15, 10, 30, 0, 0, time.UTC),
Kind: "message",
Role: "user",
Type: "text",
Text: "Hello, world!",
Meta: map[string]string{"key": "value", "model": "gpt-4"},
},
wantErr: false,
},
{
name: "minimal event with only required fields",
event: Event{
TS: time.Date(2024, 1, 15, 10, 30, 0, 0, time.UTC),
Kind: "system",
},
wantErr: false,
},
{
name: "event with empty optional fields",
event: Event{
TS: time.Date(2024, 1, 15, 10, 30, 0, 0, time.UTC),
Kind: "assistant",
Role: "",
Type: "",
Text: "",
Meta: nil,
},
wantErr: false,
},
{
name: "event with zero timestamp",
event: Event{
Kind: "message",
Text: "test",
},
wantErr: false,
},
{
name: "event with unicode text",
event: Event{
TS: time.Date(2024, 1, 15, 10, 30, 0, 0, time.UTC),
Kind: "message",
Text: "Hello \u4e16\u754c! \U0001F600",
},
wantErr: false,
},
{
name: "event with multiline text",
event: Event{
TS: time.Date(2024, 1, 15, 10, 30, 0, 0, time.UTC),
Kind: "message",
Text: "Line 1\nLine 2\nLine 3",
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Marshal to JSON
data, err := json.Marshal(tt.event)
if (err != nil) != tt.wantErr {
t.Errorf("json.Marshal() error = %v, wantErr %v", err, tt.wantErr)
return
}
if tt.wantErr {
return
}
// Unmarshal back to Event
var decoded Event
err = json.Unmarshal(data, &decoded)
if err != nil {
t.Errorf("json.Unmarshal() error = %v", err)
return
}
// Compare fields
if !tt.event.TS.Equal(decoded.TS) {
t.Errorf("TS mismatch: got %v, want %v", decoded.TS, tt.event.TS)
}
if decoded.Kind != tt.event.Kind {
t.Errorf("Kind mismatch: got %q, want %q", decoded.Kind, tt.event.Kind)
}
if decoded.Role != tt.event.Role {
t.Errorf("Role mismatch: got %q, want %q", decoded.Role, tt.event.Role)
}
if decoded.Type != tt.event.Type {
t.Errorf("Type mismatch: got %q, want %q", decoded.Type, tt.event.Type)
}
if decoded.Text != tt.event.Text {
t.Errorf("Text mismatch: got %q, want %q", decoded.Text, tt.event.Text)
}
if len(tt.event.Meta) != len(decoded.Meta) {
t.Errorf("Meta length mismatch: got %d, want %d", len(decoded.Meta), len(tt.event.Meta))
}
for k, v := range tt.event.Meta {
if decoded.Meta[k] != v {
t.Errorf("Meta[%q] mismatch: got %q, want %q", k, decoded.Meta[k], v)
}
}
})
}
}
func TestEvent_JSON_OmitEmpty(t *testing.T) {
tests := []struct {
name string
event Event
shouldContain []string
shouldOmit []string
}{
{
name: "omits empty optional fields",
event: Event{
TS: time.Date(2024, 1, 15, 10, 30, 0, 0, time.UTC),
Kind: "message",
},
shouldContain: []string{"ts", "kind"},
shouldOmit: []string{"role", "type", "text", "meta"},
},
{
name: "includes non-empty optional fields",
event: Event{
TS: time.Date(2024, 1, 15, 10, 30, 0, 0, time.UTC),
Kind: "message",
Role: "user",
Text: "hello",
},
shouldContain: []string{"ts", "kind", "role", "text"},
shouldOmit: []string{"type", "meta"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
data, err := json.Marshal(tt.event)
if err != nil {
t.Fatalf("json.Marshal() error = %v", err)
}
jsonStr := string(data)
for _, field := range tt.shouldContain {
if !containsField(jsonStr, field) {
t.Errorf("JSON should contain field %q, got: %s", field, jsonStr)
}
}
for _, field := range tt.shouldOmit {
if containsField(jsonStr, field) {
t.Errorf("JSON should omit field %q, got: %s", field, jsonStr)
}
}
})
}
}
func TestEvent_Fields(t *testing.T) {
tests := []struct {
name string
event Event
wantKind string
wantRole string
wantType string
wantText string
}{
{
name: "user message event",
event: Event{
Kind: "message",
Role: "user",
Type: "text",
Text: "What is the weather?",
},
wantKind: "message",
wantRole: "user",
wantType: "text",
wantText: "What is the weather?",
},
{
name: "assistant response event",
event: Event{
Kind: "message",
Role: "assistant",
Type: "text",
Text: "The weather is sunny.",
},
wantKind: "message",
wantRole: "assistant",
wantType: "text",
wantText: "The weather is sunny.",
},
{
name: "system event",
event: Event{
Kind: "system",
Type: "config",
Text: "Session started",
},
wantKind: "system",
wantRole: "",
wantType: "config",
wantText: "Session started",
},
{
name: "tool use event",
event: Event{
Kind: "tool_use",
Role: "assistant",
Type: "bash",
Text: "ls -la",
},
wantKind: "tool_use",
wantRole: "assistant",
wantType: "bash",
wantText: "ls -la",
},
{
name: "tool result event",
event: Event{
Kind: "tool_result",
Role: "user",
Type: "bash",
Text: "file1.txt\nfile2.txt",
},
wantKind: "tool_result",
wantRole: "user",
wantType: "bash",
wantText: "file1.txt\nfile2.txt",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.event.Kind != tt.wantKind {
t.Errorf("Kind = %q, want %q", tt.event.Kind, tt.wantKind)
}
if tt.event.Role != tt.wantRole {
t.Errorf("Role = %q, want %q", tt.event.Role, tt.wantRole)
}
if tt.event.Type != tt.wantType {
t.Errorf("Type = %q, want %q", tt.event.Type, tt.wantType)
}
if tt.event.Text != tt.wantText {
t.Errorf("Text = %q, want %q", tt.event.Text, tt.wantText)
}
})
}
}
func TestEvent_MetaField(t *testing.T) {
tests := []struct {
name string
meta map[string]string
key string
wantVal string
wantOk bool
}{
{
name: "existing key",
meta: map[string]string{"model": "gpt-4", "tokens": "100"},
key: "model",
wantVal: "gpt-4",
wantOk: true,
},
{
name: "missing key",
meta: map[string]string{"model": "gpt-4"},
key: "tokens",
wantVal: "",
wantOk: false,
},
{
name: "nil meta",
meta: nil,
key: "any",
wantVal: "",
wantOk: false,
},
{
name: "empty meta",
meta: map[string]string{},
key: "any",
wantVal: "",
wantOk: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
event := Event{Meta: tt.meta}
val, ok := event.Meta[tt.key]
if ok != tt.wantOk {
t.Errorf("Meta[%q] ok = %v, want %v", tt.key, ok, tt.wantOk)
}
if val != tt.wantVal {
t.Errorf("Meta[%q] = %q, want %q", tt.key, val, tt.wantVal)
}
})
}
}
func TestEvent_Timestamp(t *testing.T) {
tests := []struct {
name string
ts time.Time
isZero bool
}{
{
name: "valid timestamp",
ts: time.Date(2024, 1, 15, 10, 30, 0, 0, time.UTC),
isZero: false,
},
{
name: "zero timestamp",
ts: time.Time{},
isZero: true,
},
{
name: "unix epoch",
ts: time.Unix(0, 0).UTC(),
isZero: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
event := Event{TS: tt.ts}
if event.TS.IsZero() != tt.isZero {
t.Errorf("TS.IsZero() = %v, want %v", event.TS.IsZero(), tt.isZero)
}
})
}
}
// containsField checks if a JSON string contains a specific field key
func containsField(jsonStr, field string) bool {
return json.Valid([]byte(jsonStr)) && (len(jsonStr) > 0 && (jsonStr[0] == '{' || jsonStr[0] == '[')) &&
(len(field) > 0 && (jsonFieldPresent(jsonStr, field)))
}
func jsonFieldPresent(jsonStr, field string) bool {
var m map[string]interface{}
if err := json.Unmarshal([]byte(jsonStr), &m); err != nil {
return false
}
_, ok := m[field]
return ok
}
+490
View File
@@ -0,0 +1,490 @@
package memory
import (
"fmt"
"regexp"
"strconv"
"strings"
"time"
)
// FileModification represents a file change in the session
type FileModification struct {
Path string `json:"path"`
Action string `json:"action"` // created, modified, deleted, referenced
Description string `json:"description"`
}
// Decision represents a decision made during the session
type Decision struct {
Decision string `json:"decision"`
Rationale string `json:"rationale"`
}
// Task represents a next step or task
type Task struct {
Description string `json:"description"`
Completed bool `json:"completed"`
}
// SummaryMetadata contains metadata about the summary
type SummaryMetadata struct {
UpdatedAt time.Time `json:"updated_at"`
CompressionCount int `json:"compression_count"`
TokensUsed int `json:"tokens_used"`
}
// StructuredSummary represents a Factory.ai-style structured summary
type StructuredSummary struct {
Version string `json:"version"`
SessionIntent string `json:"session_intent"`
FileModifications []FileModification `json:"file_modifications"`
DecisionsMade []Decision `json:"decisions_made"`
NextSteps []Task `json:"next_steps"`
TechnicalDetails string `json:"technical_details"`
Metadata SummaryMetadata `json:"metadata"`
}
// ParseStructuredSummary parses markdown text with XML markers into a StructuredSummary
func ParseStructuredSummary(text string) (*StructuredSummary, error) {
// Extract content between XML markers
xmlPattern := regexp.MustCompile(`(?s)<structured_summary>(.*?)</structured_summary>`)
matches := xmlPattern.FindStringSubmatch(text)
var content string
if len(matches) > 1 {
content = matches[1]
} else {
// If no XML markers, treat entire text as content
content = text
}
summary := &StructuredSummary{
Version: "1.0",
FileModifications: []FileModification{},
DecisionsMade: []Decision{},
NextSteps: []Task{},
Metadata: SummaryMetadata{
UpdatedAt: time.Now(),
},
}
// Parse Session Intent
summary.SessionIntent = parseSection(content, "Session Intent")
// Parse File Modifications table
summary.FileModifications = parseFileModificationsTable(content)
// Parse Decisions Made
summary.DecisionsMade = parseDecisions(content)
// Parse Next Steps
summary.NextSteps = parseNextSteps(content)
// Parse Technical Details
summary.TechnicalDetails = parseSection(content, "Technical Details")
// Parse Metadata
parseMetadata(content, &summary.Metadata)
return summary, nil
}
// parseSection extracts content from a markdown section
func parseSection(content, sectionName string) string {
// Match section header and capture content until next section or end
pattern := regexp.MustCompile(`(?si)##\s*` + regexp.QuoteMeta(sectionName) + `\s*\n(.*?)(?:##\s|\z)`)
matches := pattern.FindStringSubmatch(content)
if len(matches) > 1 {
return strings.TrimSpace(matches[1])
}
return ""
}
// parseFileModificationsTable parses the file modifications markdown table
func parseFileModificationsTable(content string) []FileModification {
var modifications []FileModification
// Find the File Modifications section
sectionContent := parseSection(content, "File Modifications")
if sectionContent == "" {
return modifications
}
// Parse table rows (skip header and separator)
lines := strings.Split(sectionContent, "\n")
inTable := false
for _, line := range lines {
line = strings.TrimSpace(line)
if line == "" {
continue
}
// Check if this is a table row
if strings.HasPrefix(line, "|") {
// Skip header separator (contains dashes)
if strings.Contains(line, "---") {
inTable = true
continue
}
// Skip header row (first pipe-delimited row before separator)
if !inTable {
continue
}
// Parse table row
cells := strings.Split(line, "|")
if len(cells) >= 4 {
path := strings.TrimSpace(cells[1])
action := strings.TrimSpace(cells[2])
description := strings.TrimSpace(cells[3])
// Remove backticks from path if present
path = strings.Trim(path, "`")
if path != "" {
modifications = append(modifications, FileModification{
Path: path,
Action: action,
Description: description,
})
}
}
}
}
return modifications
}
// parseDecisions parses the Decisions Made section
func parseDecisions(content string) []Decision {
var decisions []Decision
sectionContent := parseSection(content, "Decisions Made")
if sectionContent == "" {
return decisions
}
// Match list items with optional rationale in parentheses or after colon
lines := strings.Split(sectionContent, "\n")
for _, line := range lines {
line = strings.TrimSpace(line)
if line == "" {
continue
}
// Remove list marker
line = strings.TrimPrefix(line, "- ")
line = strings.TrimPrefix(line, "* ")
// Check for numbered list
numberedPattern := regexp.MustCompile(`^\d+\.\s*`)
line = numberedPattern.ReplaceAllString(line, "")
if line == "" {
continue
}
decision := Decision{}
// Try to extract rationale from parentheses
rationalePattern := regexp.MustCompile(`^(.*?)\s*\((.*?)\)\s*$`)
if matches := rationalePattern.FindStringSubmatch(line); len(matches) > 2 {
decision.Decision = strings.TrimSpace(matches[1])
decision.Rationale = strings.TrimSpace(matches[2])
} else if colonIdx := strings.Index(line, ":"); colonIdx > 0 {
// Try colon separator
decision.Decision = strings.TrimSpace(line[:colonIdx])
decision.Rationale = strings.TrimSpace(line[colonIdx+1:])
} else {
decision.Decision = line
}
if decision.Decision != "" {
decisions = append(decisions, decision)
}
}
return decisions
}
// parseNextSteps parses the Next Steps section with checkboxes
func parseNextSteps(content string) []Task {
var tasks []Task
sectionContent := parseSection(content, "Next Steps")
if sectionContent == "" {
return tasks
}
lines := strings.Split(sectionContent, "\n")
for _, line := range lines {
line = strings.TrimSpace(line)
if line == "" {
continue
}
// Remove list marker
line = strings.TrimPrefix(line, "- ")
line = strings.TrimPrefix(line, "* ")
task := Task{}
// Check for checkbox
if strings.HasPrefix(line, "[x]") || strings.HasPrefix(line, "[X]") {
task.Completed = true
line = strings.TrimPrefix(line, "[x]")
line = strings.TrimPrefix(line, "[X]")
} else if strings.HasPrefix(line, "[ ]") {
task.Completed = false
line = strings.TrimPrefix(line, "[ ]")
}
task.Description = strings.TrimSpace(line)
if task.Description != "" {
tasks = append(tasks, task)
}
}
return tasks
}
// parseMetadata parses the Metadata section
func parseMetadata(content string, metadata *SummaryMetadata) {
sectionContent := parseSection(content, "Metadata")
if sectionContent == "" {
return
}
lines := strings.Split(sectionContent, "\n")
for _, line := range lines {
line = strings.TrimSpace(line)
if line == "" {
continue
}
// Remove list markers
line = strings.TrimPrefix(line, "- ")
line = strings.TrimPrefix(line, "* ")
// Parse key-value pairs
if colonIdx := strings.Index(line, ":"); colonIdx > 0 {
key := strings.ToLower(strings.TrimSpace(line[:colonIdx]))
value := strings.TrimSpace(line[colonIdx+1:])
switch {
case strings.Contains(key, "updated") || strings.Contains(key, "time"):
if t, err := time.Parse(time.RFC3339, value); err == nil {
metadata.UpdatedAt = t
}
case strings.Contains(key, "compression"):
if count, err := strconv.Atoi(value); err == nil {
metadata.CompressionCount = count
}
case strings.Contains(key, "token"):
if tokens, err := strconv.Atoi(value); err == nil {
metadata.TokensUsed = tokens
}
}
}
}
}
// RenderStructuredSummary generates markdown format with XML markers
func RenderStructuredSummary(summary *StructuredSummary) string {
var sb strings.Builder
sb.WriteString("<structured_summary>\n\n")
// Session Intent
sb.WriteString("## Session Intent\n")
sb.WriteString(summary.SessionIntent)
sb.WriteString("\n\n")
// File Modifications
sb.WriteString("## File Modifications\n")
if len(summary.FileModifications) > 0 {
sb.WriteString("| Path | Action | Description |\n")
sb.WriteString("|------|--------|-------------|\n")
for _, mod := range summary.FileModifications {
sb.WriteString(fmt.Sprintf("| `%s` | %s | %s |\n", mod.Path, mod.Action, mod.Description))
}
} else {
sb.WriteString("No file modifications recorded.\n")
}
sb.WriteString("\n")
// Decisions Made
sb.WriteString("## Decisions Made\n")
if len(summary.DecisionsMade) > 0 {
for _, decision := range summary.DecisionsMade {
if decision.Rationale != "" {
sb.WriteString(fmt.Sprintf("- %s (%s)\n", decision.Decision, decision.Rationale))
} else {
sb.WriteString(fmt.Sprintf("- %s\n", decision.Decision))
}
}
} else {
sb.WriteString("No decisions recorded.\n")
}
sb.WriteString("\n")
// Next Steps
sb.WriteString("## Next Steps\n")
if len(summary.NextSteps) > 0 {
for _, task := range summary.NextSteps {
checkbox := "[ ]"
if task.Completed {
checkbox = "[x]"
}
sb.WriteString(fmt.Sprintf("- %s %s\n", checkbox, task.Description))
}
} else {
sb.WriteString("No next steps recorded.\n")
}
sb.WriteString("\n")
// Technical Details
sb.WriteString("## Technical Details\n")
if summary.TechnicalDetails != "" {
sb.WriteString(summary.TechnicalDetails)
} else {
sb.WriteString("No technical details recorded.")
}
sb.WriteString("\n\n")
// Metadata
sb.WriteString("## Metadata\n")
sb.WriteString(fmt.Sprintf("- Updated At: %s\n", summary.Metadata.UpdatedAt.Format(time.RFC3339)))
sb.WriteString(fmt.Sprintf("- Compression Count: %d\n", summary.Metadata.CompressionCount))
sb.WriteString(fmt.Sprintf("- Tokens Used: %d\n", summary.Metadata.TokensUsed))
sb.WriteString("\n")
sb.WriteString("</structured_summary>")
return sb.String()
}
// MergeStructuredSummaries combines two summaries, preferring newer data
func MergeStructuredSummaries(existing, new *StructuredSummary) *StructuredSummary {
if existing == nil {
return new
}
if new == nil {
return existing
}
merged := &StructuredSummary{
Version: new.Version,
Metadata: SummaryMetadata{
UpdatedAt: time.Now(),
CompressionCount: existing.Metadata.CompressionCount + 1,
TokensUsed: new.Metadata.TokensUsed,
},
}
// Prefer newer session intent
if new.SessionIntent != "" {
merged.SessionIntent = new.SessionIntent
} else {
merged.SessionIntent = existing.SessionIntent
}
// Merge file modifications, deduplicate by path, prefer newer
fileModMap := make(map[string]FileModification)
for _, mod := range existing.FileModifications {
fileModMap[mod.Path] = mod
}
for _, mod := range new.FileModifications {
fileModMap[mod.Path] = mod
}
merged.FileModifications = make([]FileModification, 0, len(fileModMap))
for _, mod := range fileModMap {
merged.FileModifications = append(merged.FileModifications, mod)
}
// Limit to max 12 file modifications
if len(merged.FileModifications) > 12 {
merged.FileModifications = merged.FileModifications[len(merged.FileModifications)-12:]
}
// Merge decisions, prefer newer, limit to 5
merged.DecisionsMade = mergeDecisions(existing.DecisionsMade, new.DecisionsMade, 5)
// Merge next steps, prefer newer, limit to 5
merged.NextSteps = mergeTasks(existing.NextSteps, new.NextSteps, 5)
// Prefer newer technical details
if new.TechnicalDetails != "" {
merged.TechnicalDetails = new.TechnicalDetails
} else {
merged.TechnicalDetails = existing.TechnicalDetails
}
return merged
}
// mergeDecisions merges two decision slices with deduplication and limit
func mergeDecisions(existing, new []Decision, maxItems int) []Decision {
seen := make(map[string]bool)
var result []Decision
// Add new decisions first (preferred)
for _, d := range new {
key := strings.ToLower(d.Decision)
if !seen[key] {
seen[key] = true
result = append(result, d)
}
}
// Add existing decisions that aren't duplicates
for _, d := range existing {
key := strings.ToLower(d.Decision)
if !seen[key] {
seen[key] = true
result = append(result, d)
}
}
// Limit to maxItems
if len(result) > maxItems {
result = result[:maxItems]
}
return result
}
// mergeTasks merges two task slices with deduplication and limit
func mergeTasks(existing, new []Task, maxItems int) []Task {
seen := make(map[string]bool)
var result []Task
// Add new tasks first (preferred)
for _, t := range new {
key := strings.ToLower(t.Description)
if !seen[key] {
seen[key] = true
result = append(result, t)
}
}
// Add existing incomplete tasks that aren't duplicates
for _, t := range existing {
key := strings.ToLower(t.Description)
if !seen[key] && !t.Completed {
seen[key] = true
result = append(result, t)
}
}
// Limit to maxItems
if len(result) > maxItems {
result = result[:maxItems]
}
return result
}
+474
View File
@@ -0,0 +1,474 @@
package memory
import (
"context"
"encoding/json"
"fmt"
"log"
"regexp"
"strings"
"time"
)
// SummarizerConfig holds configuration for LLM-based context summarization.
type SummarizerConfig struct {
Enabled bool // Enable LLM summarization (default: true)
ThresholdPercent float64 // Trigger at N% of context window (default: 0.75)
MaxSummaryTokens int // Max tokens for summary output (default: 2000)
FallbackToRegex bool // Use regex-based summary if LLM fails (default: true)
SummarizationTimeout time.Duration // Timeout for summarization call (default: 30s)
}
// DefaultSummarizerConfig returns the default summarizer configuration.
func DefaultSummarizerConfig() SummarizerConfig {
return SummarizerConfig{
Enabled: true,
ThresholdPercent: 0.75,
MaxSummaryTokens: 2000,
FallbackToRegex: true,
SummarizationTimeout: 30 * time.Second,
}
}
// Summarizer handles LLM-based context summarization.
type Summarizer struct {
config SummarizerConfig
executor SummarizerExecutor
}
// NewSummarizer creates a new Summarizer with the given config and executor.
func NewSummarizer(config SummarizerConfig, executor SummarizerExecutor) *Summarizer {
return &Summarizer{
config: config,
executor: executor,
}
}
// GenerateInitialSummary creates the first summary from dropped events.
func (s *Summarizer) GenerateInitialSummary(ctx context.Context, model string, dropped []Event, latestIntent string) (*StructuredSummary, error) {
if len(dropped) == 0 {
return &StructuredSummary{
Version: "1.0",
Metadata: SummaryMetadata{
UpdatedAt: time.Now(),
CompressionCount: 1,
TokensUsed: 0,
},
}, nil
}
prompt := buildInitialPrompt(dropped, latestIntent)
ctx, cancel := context.WithTimeout(ctx, s.config.SummarizationTimeout)
defer cancel()
response, err := s.executor.Summarize(ctx, model, prompt)
if err != nil {
log.Printf("[summarizer] LLM summarization failed: %v", err)
if s.config.FallbackToRegex {
log.Printf("[summarizer] falling back to regex-based summary")
return regexFallback(nil, dropped, latestIntent), nil
}
return nil, fmt.Errorf("summarization failed: %w", err)
}
summary, err := parseSummaryResponse(response)
if err != nil {
log.Printf("[summarizer] failed to parse LLM response: %v", err)
if s.config.FallbackToRegex {
log.Printf("[summarizer] falling back to regex-based summary")
return regexFallback(nil, dropped, latestIntent), nil
}
return nil, fmt.Errorf("failed to parse summary response: %w", err)
}
summary.Version = "1.0"
summary.Metadata = SummaryMetadata{
UpdatedAt: time.Now(),
CompressionCount: 1,
TokensUsed: estimateTokens(response),
}
return ensureTokenBudget(summary), nil
}
// MergeSummary performs an incremental merge of new events into an existing summary.
func (s *Summarizer) MergeSummary(ctx context.Context, model string, existing *StructuredSummary, dropped []Event, latestIntent string) (*StructuredSummary, error) {
if existing == nil {
return s.GenerateInitialSummary(ctx, model, dropped, latestIntent)
}
if len(dropped) == 0 {
return existing, nil
}
prompt := buildMergePrompt(existing, dropped, latestIntent)
ctx, cancel := context.WithTimeout(ctx, s.config.SummarizationTimeout)
defer cancel()
response, err := s.executor.Summarize(ctx, model, prompt)
if err != nil {
log.Printf("[summarizer] LLM merge summarization failed: %v", err)
if s.config.FallbackToRegex {
log.Printf("[summarizer] falling back to regex-based merge")
return regexFallback(existing, dropped, latestIntent), nil
}
return nil, fmt.Errorf("merge summarization failed: %w", err)
}
summary, err := parseSummaryResponse(response)
if err != nil {
log.Printf("[summarizer] failed to parse LLM merge response: %v", err)
if s.config.FallbackToRegex {
log.Printf("[summarizer] falling back to regex-based merge")
return regexFallback(existing, dropped, latestIntent), nil
}
return nil, fmt.Errorf("failed to parse merge response: %w", err)
}
summary.Version = existing.Version
summary.Metadata = SummaryMetadata{
UpdatedAt: time.Now(),
CompressionCount: existing.Metadata.CompressionCount + 1,
TokensUsed: estimateTokens(response),
}
return ensureTokenBudget(summary), nil
}
// buildInitialPrompt constructs the prompt for generating an initial summary.
func buildInitialPrompt(dropped []Event, latestIntent string) string {
var sb strings.Builder
sb.WriteString(`You are a context summarizer. Generate a structured summary of the following conversation events.
Output a JSON object with this exact structure:
{
"session_intent": "Brief description of the user's main goal",
"file_modifications": [
{"path": "/exact/file/path.go", "action": "created|modified|deleted", "description": "Brief change description"}
],
"decisions_made": [
{"decision": "Decision description", "rationale": "Why this decision was made"}
],
"next_steps": [
{"description": "Task description", "completed": false}
],
"technical_details": "Important technical context as a single string"
}
Guidelines:
- Keep each section concise (max 5 items per list, max 12 files)
- Preserve file paths exactly as they appear
- Focus on actionable information
- session_intent should be 1-2 sentences max
`)
if latestIntent != "" {
sb.WriteString("Latest user intent: ")
sb.WriteString(latestIntent)
sb.WriteString("\n\n")
}
sb.WriteString("Events to summarize:\n")
sb.WriteString(formatDroppedEvents(dropped))
return sb.String()
}
// buildMergePrompt constructs the prompt for merging new events into an existing summary.
func buildMergePrompt(existing *StructuredSummary, dropped []Event, latestIntent string) string {
var sb strings.Builder
sb.WriteString(`You are a context summarizer performing an incremental update. Merge the new events into the existing summary.
Output a JSON object with this exact structure:
{
"session_intent": "Updated description of the user's main goal",
"file_modifications": [
{"path": "/exact/file/path.go", "action": "created|modified|deleted", "description": "Brief change description"}
],
"decisions_made": [
{"decision": "Decision description", "rationale": "Why this decision was made"}
],
"next_steps": [
{"description": "Task description", "completed": false}
],
"technical_details": "Important technical context as a single string"
}
Guidelines:
- Update existing sections with new information
- Consolidate duplicates (merge multiple modifications to same file)
- Keep each section concise (max 5 items per list, max 12 files)
- Preserve file paths exactly as they appear
- Mark completed tasks as completed: true
- Remove obsolete information
- session_intent should reflect the evolved understanding
`)
sb.WriteString("Existing summary:\n")
existingJSON, _ := json.MarshalIndent(existing, "", " ")
sb.WriteString(string(existingJSON))
sb.WriteString("\n\n")
if latestIntent != "" {
sb.WriteString("Latest user intent: ")
sb.WriteString(latestIntent)
sb.WriteString("\n\n")
}
sb.WriteString("New events to merge:\n")
sb.WriteString(formatDroppedEvents(dropped))
return sb.String()
}
// formatDroppedEvents formats events for inclusion in prompts.
func formatDroppedEvents(dropped []Event) string {
var sb strings.Builder
for i, e := range dropped {
sb.WriteString(fmt.Sprintf("[%d] ", i+1))
if e.Role != "" {
sb.WriteString(fmt.Sprintf("[%s] ", e.Role))
}
if e.Kind != "" {
sb.WriteString(fmt.Sprintf("(%s) ", e.Kind))
}
if e.Type != "" {
sb.WriteString(fmt.Sprintf("<%s> ", e.Type))
}
sb.WriteString(e.Text)
if len(e.Meta) > 0 {
metaJSON, _ := json.Marshal(e.Meta)
sb.WriteString(fmt.Sprintf(" [meta: %s]", string(metaJSON)))
}
sb.WriteString("\n")
}
return sb.String()
}
// ensureTokenBudget trims the summary if it exceeds the token budget.
func ensureTokenBudget(summary *StructuredSummary) *StructuredSummary {
if summary == nil {
return nil
}
// Limit file modifications to 12
if len(summary.FileModifications) > 12 {
summary.FileModifications = summary.FileModifications[:12]
}
// Limit decisions to 5
if len(summary.DecisionsMade) > 5 {
summary.DecisionsMade = summary.DecisionsMade[:5]
}
// Limit next steps to 5
if len(summary.NextSteps) > 5 {
summary.NextSteps = summary.NextSteps[:5]
}
// Truncate overly long strings
if len(summary.SessionIntent) > 500 {
summary.SessionIntent = summary.SessionIntent[:500] + "..."
}
for i := range summary.FileModifications {
if len(summary.FileModifications[i].Description) > 200 {
summary.FileModifications[i].Description = summary.FileModifications[i].Description[:200] + "..."
}
}
for i := range summary.DecisionsMade {
if len(summary.DecisionsMade[i].Decision) > 200 {
summary.DecisionsMade[i].Decision = summary.DecisionsMade[i].Decision[:200] + "..."
}
if len(summary.DecisionsMade[i].Rationale) > 200 {
summary.DecisionsMade[i].Rationale = summary.DecisionsMade[i].Rationale[:200] + "..."
}
}
for i := range summary.NextSteps {
if len(summary.NextSteps[i].Description) > 200 {
summary.NextSteps[i].Description = summary.NextSteps[i].Description[:200] + "..."
}
}
// Truncate technical details if too long
if len(summary.TechnicalDetails) > 1500 {
summary.TechnicalDetails = summary.TechnicalDetails[:1500] + "..."
}
return summary
}
// regexFallback generates a summary using regex-based extraction when LLM fails.
func regexFallback(existing *StructuredSummary, dropped []Event, latestIntent string) *StructuredSummary {
summary := &StructuredSummary{
Version: "1.0",
FileModifications: make([]FileModification, 0),
DecisionsMade: make([]Decision, 0),
NextSteps: make([]Task, 0),
TechnicalDetails: "",
}
// Copy existing data if available
if existing != nil {
summary.Version = existing.Version
summary.SessionIntent = existing.SessionIntent
summary.FileModifications = append(summary.FileModifications, existing.FileModifications...)
summary.DecisionsMade = append(summary.DecisionsMade, existing.DecisionsMade...)
summary.NextSteps = append(summary.NextSteps, existing.NextSteps...)
summary.TechnicalDetails = existing.TechnicalDetails
summary.Metadata = existing.Metadata
}
// Update session intent if provided
if latestIntent != "" {
if summary.SessionIntent == "" {
summary.SessionIntent = latestIntent
} else {
summary.SessionIntent = latestIntent + " (previously: " + summary.SessionIntent + ")"
}
}
// Regex patterns for file path extraction
filePathRegex := regexp.MustCompile(`(?i)(?:file|path|modified|created|edited|deleted|updated)[:=\s]+["']?([/\\]?[\w./\\-]+\.\w+)["']?`)
absolutePathRegex := regexp.MustCompile(`(?m)^([A-Za-z]:)?[/\\][\w./\\-]+\.\w+`)
seenFiles := make(map[string]bool)
for _, fm := range summary.FileModifications {
seenFiles[fm.Path] = true
}
var technicalDetailsBuilder strings.Builder
if summary.TechnicalDetails != "" {
technicalDetailsBuilder.WriteString(summary.TechnicalDetails)
technicalDetailsBuilder.WriteString("\n")
}
// Extract information from dropped events
for _, e := range dropped {
text := e.Text
// Extract file paths
matches := filePathRegex.FindAllStringSubmatch(text, -1)
for _, match := range matches {
if len(match) > 1 {
path := strings.TrimSpace(match[1])
if path != "" && !seenFiles[path] && len(summary.FileModifications) < 12 {
seenFiles[path] = true
action := "modified"
lowerText := strings.ToLower(text)
if strings.Contains(lowerText, "creat") || strings.Contains(lowerText, "new") {
action = "created"
} else if strings.Contains(lowerText, "delet") || strings.Contains(lowerText, "remov") {
action = "deleted"
}
summary.FileModifications = append(summary.FileModifications, FileModification{
Path: path,
Action: action,
Description: truncateString(text, 100),
})
}
}
}
// Also check for absolute paths
absMatches := absolutePathRegex.FindAllString(text, -1)
for _, path := range absMatches {
path = strings.TrimSpace(path)
if path != "" && !seenFiles[path] && len(summary.FileModifications) < 12 {
seenFiles[path] = true
summary.FileModifications = append(summary.FileModifications, FileModification{
Path: path,
Action: "referenced",
Description: truncateString(text, 100),
})
}
}
// Extract decisions from assistant messages
if e.Role == "assistant" && len(text) > 20 && len(summary.DecisionsMade) < 5 {
if strings.Contains(strings.ToLower(text), "decid") ||
strings.Contains(strings.ToLower(text), "chose") ||
strings.Contains(strings.ToLower(text), "will use") ||
strings.Contains(strings.ToLower(text), "going to") {
decision := Decision{
Decision: truncateString(text, 150),
Rationale: "Extracted from conversation",
}
summary.DecisionsMade = append(summary.DecisionsMade, decision)
}
}
// Extract technical details from tool results or code blocks
if e.Kind == "tool_result" || strings.Contains(text, "```") {
detail := truncateString(text, 200)
if technicalDetailsBuilder.Len()+len(detail) < 1500 {
technicalDetailsBuilder.WriteString(detail)
technicalDetailsBuilder.WriteString("\n")
}
}
}
summary.TechnicalDetails = strings.TrimSpace(technicalDetailsBuilder.String())
// Update metadata
now := time.Now()
if existing == nil {
summary.Metadata = SummaryMetadata{
UpdatedAt: now,
CompressionCount: 1,
TokensUsed: 0,
}
} else {
summary.Metadata.UpdatedAt = now
summary.Metadata.CompressionCount++
}
return ensureTokenBudget(summary)
}
// parseSummaryResponse parses the LLM response into a StructuredSummary.
func parseSummaryResponse(response string) (*StructuredSummary, error) {
response = strings.TrimSpace(response)
// Try to extract JSON from the response
jsonStart := strings.Index(response, "{")
jsonEnd := strings.LastIndex(response, "}")
if jsonStart == -1 || jsonEnd == -1 || jsonEnd <= jsonStart {
return nil, fmt.Errorf("no valid JSON found in response")
}
jsonStr := response[jsonStart : jsonEnd+1]
var summary StructuredSummary
if err := json.Unmarshal([]byte(jsonStr), &summary); err != nil {
return nil, fmt.Errorf("failed to unmarshal JSON: %w", err)
}
return &summary, nil
}
// truncateString truncates a string to the specified length.
func truncateString(s string, maxLen int) string {
s = strings.TrimSpace(s)
if len(s) <= maxLen {
return s
}
return s[:maxLen] + "..."
}
// estimateTokens provides a rough token count estimate based on character count.
// Uses the rough approximation of ~4 characters per token for English text.
func estimateTokens(text string) int {
return len(text) / 4
}
+229
View File
@@ -0,0 +1,229 @@
package memory
import (
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
)
// SummarizerExecutor defines the interface for executing summarization requests
// against the LLM pipeline.
type SummarizerExecutor interface {
// Summarize sends a summarization prompt to the specified model and returns
// the assistant's response content.
Summarize(ctx context.Context, model string, prompt string) (string, error)
}
// AuthExecutor defines the minimal interface required to execute requests
// through the auth manager pipeline. This abstraction avoids import cycles
// with the auth package.
type AuthExecutor interface {
// Execute routes a request through the configured providers and returns
// the response payload.
Execute(ctx context.Context, providers []string, req interface{}, opts interface{}) ([]byte, error)
}
// ExecutorRequest mirrors the executor.Request structure to avoid direct imports.
type ExecutorRequest struct {
Model string
Payload []byte
Metadata map[string]any
}
// ExecutorOptions mirrors the executor.Options structure to avoid direct imports.
type ExecutorOptions struct {
Stream bool
Headers http.Header
}
// PipelineSummarizerExecutor implements SummarizerExecutor by routing requests
// through the existing auth manager pipeline.
type PipelineSummarizerExecutor struct {
authManager AuthExecutor
providers []string
}
// NewPipelineSummarizerExecutor creates a new PipelineSummarizerExecutor with
// the given auth manager and provider list.
func NewPipelineSummarizerExecutor(authManager AuthExecutor, providers []string) *PipelineSummarizerExecutor {
if providers == nil {
providers = []string{}
}
return &PipelineSummarizerExecutor{
authManager: authManager,
providers: providers,
}
}
// Summarize builds an OpenAI-compatible chat completion request and executes it
// through the pipeline, extracting the assistant response content.
func (p *PipelineSummarizerExecutor) Summarize(ctx context.Context, model string, prompt string) (string, error) {
if p.authManager == nil {
return "", errors.New("summarizer executor: auth manager not configured")
}
if model == "" {
return "", errors.New("summarizer executor: model not specified")
}
if prompt == "" {
return "", errors.New("summarizer executor: prompt is empty")
}
payload := buildSummarizationPayload(model, prompt)
req := ExecutorRequest{
Model: model,
Payload: payload,
Metadata: map[string]any{
"internal": true,
},
}
opts := ExecutorOptions{
Stream: false,
Headers: http.Header{
"X-CLIProxyAPI-Internal": []string{"summarization"},
"Content-Type": []string{"application/json"},
},
}
responsePayload, err := p.authManager.Execute(ctx, p.providers, req, opts)
if err != nil {
return "", fmt.Errorf("summarizer executor: execution failed: %w", err)
}
content, err := extractAssistantContent(responsePayload)
if err != nil {
return "", fmt.Errorf("summarizer executor: failed to extract response: %w", err)
}
return content, nil
}
// buildSummarizationPayload creates an OpenAI-compatible chat completion request payload.
func buildSummarizationPayload(model, prompt string) []byte {
systemMessage := "You are a context compression assistant. Your task is to summarize conversation history while preserving key information, decisions, and context that would be useful for continuing the conversation. Be concise but comprehensive."
payload := map[string]interface{}{
"model": model,
"messages": []map[string]string{
{
"role": "system",
"content": systemMessage,
},
{
"role": "user",
"content": prompt,
},
},
"max_tokens": 2000,
"temperature": 0.3,
}
data, _ := json.Marshal(payload)
return data
}
// extractAssistantContent parses the response payload and extracts the assistant's
// message content from an OpenAI-compatible response format.
func extractAssistantContent(payload []byte) (string, error) {
if len(payload) == 0 {
return "", errors.New("empty response payload")
}
var response struct {
Choices []struct {
Message struct {
Content string `json:"content"`
} `json:"message"`
} `json:"choices"`
}
if err := json.Unmarshal(payload, &response); err != nil {
return "", fmt.Errorf("failed to parse response: %w", err)
}
if len(response.Choices) == 0 {
return "", errors.New("no choices in response")
}
content := response.Choices[0].Message.Content
if content == "" {
return "", errors.New("empty content in response")
}
return content, nil
}
// NoOpSummarizerExecutor is a fallback implementation that always returns an error.
// Useful for testing or when summarization is disabled.
type NoOpSummarizerExecutor struct{}
// NewNoOpSummarizerExecutor creates a new NoOpSummarizerExecutor.
func NewNoOpSummarizerExecutor() *NoOpSummarizerExecutor {
return &NoOpSummarizerExecutor{}
}
// Summarize always returns an error indicating summarization is not available.
func (n *NoOpSummarizerExecutor) Summarize(ctx context.Context, model string, prompt string) (string, error) {
return "", errors.New("summarization not available: no executor configured")
}
// CoreManagerExecutor defines the minimal interface for executing requests through
// the core auth manager. This matches coreauth.Manager.Execute signature.
type CoreManagerExecutor interface {
Execute(ctx context.Context, providers []string, req interface{}, opts interface{}) (interface{}, error)
}
// ManagerAuthAdapter wraps a CoreManagerExecutor to implement the AuthExecutor interface.
// This adapter bridges the type differences between coreauth.Manager and the memory package.
type ManagerAuthAdapter struct {
manager CoreManagerExecutor
}
// NewManagerAuthAdapter creates an adapter that wraps the given manager.
// The manager must implement the CoreManagerExecutor interface (compatible with coreauth.Manager).
func NewManagerAuthAdapter(manager CoreManagerExecutor) *ManagerAuthAdapter {
return &ManagerAuthAdapter{manager: manager}
}
// Execute implements AuthExecutor by delegating to the wrapped manager and extracting
// the response payload.
func (a *ManagerAuthAdapter) Execute(ctx context.Context, providers []string, req interface{}, opts interface{}) ([]byte, error) {
if a.manager == nil {
return nil, errors.New("manager auth adapter: manager not configured")
}
resp, err := a.manager.Execute(ctx, providers, req, opts)
if err != nil {
return nil, err
}
// Handle nil response
if resp == nil {
return nil, errors.New("manager auth adapter: nil response from manager")
}
// Extract payload from response - try common response types
switch r := resp.(type) {
case []byte:
return r, nil
case interface{ GetPayload() []byte }:
return r.GetPayload(), nil
default:
// Try to access Payload field via reflection-like approach using JSON
// This handles cliproxyexecutor.Response which has a Payload []byte field
if data, err := json.Marshal(resp); err == nil {
var wrapper struct {
Payload []byte `json:"payload"`
}
if json.Unmarshal(data, &wrapper) == nil && wrapper.Payload != nil {
return wrapper.Payload, nil
}
// If no payload field, return the serialized response
return data, nil
}
return nil, fmt.Errorf("manager auth adapter: unsupported response type %T", resp)
}
}
+385
View File
@@ -0,0 +1,385 @@
package memory
import (
"context"
"encoding/json"
"errors"
"testing"
)
// mockCoreManagerExecutor is a mock implementation of CoreManagerExecutor for testing.
type mockCoreManagerExecutor struct {
response interface{}
err error
}
func (m *mockCoreManagerExecutor) Execute(ctx context.Context, providers []string, req interface{}, opts interface{}) (interface{}, error) {
return m.response, m.err
}
// responseWithPayload is a test struct that has a Payload field for testing extraction.
type responseWithPayload struct {
Payload []byte `json:"payload"`
Status int `json:"status"`
}
// responseWithGetPayload implements the GetPayload interface.
type responseWithGetPayload struct {
data []byte
}
func (r *responseWithGetPayload) GetPayload() []byte {
return r.data
}
// TestManagerAuthAdapter_NilManager tests that Execute returns an error when manager is nil.
func TestManagerAuthAdapter_NilManager(t *testing.T) {
adapter := &ManagerAuthAdapter{manager: nil}
ctx := context.Background()
providers := []string{"test-provider"}
result, err := adapter.Execute(ctx, providers, nil, nil)
if err == nil {
t.Fatal("Expected error when manager is nil, got nil")
}
expectedErr := "manager auth adapter: manager not configured"
if err.Error() != expectedErr {
t.Errorf("Expected error message '%s', got '%s'", expectedErr, err.Error())
}
if result != nil {
t.Errorf("Expected nil result, got %v", result)
}
}
// TestManagerAuthAdapter_NilResponse tests that Execute returns an error when manager returns nil response.
func TestManagerAuthAdapter_NilResponse(t *testing.T) {
mock := &mockCoreManagerExecutor{
response: nil,
err: nil,
}
adapter := NewManagerAuthAdapter(mock)
ctx := context.Background()
providers := []string{"test-provider"}
result, err := adapter.Execute(ctx, providers, nil, nil)
if err == nil {
t.Fatal("Expected error when response is nil, got nil")
}
expectedErr := "manager auth adapter: nil response from manager"
if err.Error() != expectedErr {
t.Errorf("Expected error message '%s', got '%s'", expectedErr, err.Error())
}
if result != nil {
t.Errorf("Expected nil result, got %v", result)
}
}
// TestManagerAuthAdapter_BytesResponse tests that Execute returns bytes directly when response is []byte.
func TestManagerAuthAdapter_BytesResponse(t *testing.T) {
expectedBytes := []byte(`{"choices":[{"message":{"content":"test response"}}]}`)
mock := &mockCoreManagerExecutor{
response: expectedBytes,
err: nil,
}
adapter := NewManagerAuthAdapter(mock)
ctx := context.Background()
providers := []string{"test-provider"}
result, err := adapter.Execute(ctx, providers, nil, nil)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
if string(result) != string(expectedBytes) {
t.Errorf("Expected result '%s', got '%s'", string(expectedBytes), string(result))
}
}
// TestManagerAuthAdapter_StructWithPayload tests that Execute extracts Payload field from struct response.
func TestManagerAuthAdapter_StructWithPayload(t *testing.T) {
expectedPayload := []byte(`{"choices":[{"message":{"content":"extracted payload"}}]}`)
mock := &mockCoreManagerExecutor{
response: responseWithPayload{
Payload: expectedPayload,
Status: 200,
},
err: nil,
}
adapter := NewManagerAuthAdapter(mock)
ctx := context.Background()
providers := []string{"test-provider"}
result, err := adapter.Execute(ctx, providers, nil, nil)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
if string(result) != string(expectedPayload) {
t.Errorf("Expected result '%s', got '%s'", string(expectedPayload), string(result))
}
}
// TestManagerAuthAdapter_GetPayloadInterface tests that Execute uses GetPayload() method when available.
func TestManagerAuthAdapter_GetPayloadInterface(t *testing.T) {
expectedPayload := []byte(`{"choices":[{"message":{"content":"from GetPayload"}}]}`)
mock := &mockCoreManagerExecutor{
response: &responseWithGetPayload{data: expectedPayload},
err: nil,
}
adapter := NewManagerAuthAdapter(mock)
ctx := context.Background()
providers := []string{"test-provider"}
result, err := adapter.Execute(ctx, providers, nil, nil)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
if string(result) != string(expectedPayload) {
t.Errorf("Expected result '%s', got '%s'", string(expectedPayload), string(result))
}
}
// TestManagerAuthAdapter_ManagerError tests that Execute propagates manager errors.
func TestManagerAuthAdapter_ManagerError(t *testing.T) {
expectedErr := errors.New("upstream provider error")
mock := &mockCoreManagerExecutor{
response: nil,
err: expectedErr,
}
adapter := NewManagerAuthAdapter(mock)
ctx := context.Background()
providers := []string{"test-provider"}
result, err := adapter.Execute(ctx, providers, nil, nil)
if err == nil {
t.Fatal("Expected error to be propagated, got nil")
}
if err != expectedErr {
t.Errorf("Expected error '%v', got '%v'", expectedErr, err)
}
if result != nil {
t.Errorf("Expected nil result, got %v", result)
}
}
// TestManagerAuthAdapter_SerializedFallback tests fallback to JSON serialization for unknown types without Payload.
func TestManagerAuthAdapter_SerializedFallback(t *testing.T) {
unknownResponse := map[string]interface{}{
"data": "some data",
"status": "ok",
}
mock := &mockCoreManagerExecutor{
response: unknownResponse,
err: nil,
}
adapter := NewManagerAuthAdapter(mock)
ctx := context.Background()
providers := []string{"test-provider"}
result, err := adapter.Execute(ctx, providers, nil, nil)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
// Result should be JSON serialized version of the response
var parsed map[string]interface{}
if err := json.Unmarshal(result, &parsed); err != nil {
t.Fatalf("Result should be valid JSON: %v", err)
}
if parsed["data"] != "some data" {
t.Errorf("Expected data='some data', got %v", parsed["data"])
}
if parsed["status"] != "ok" {
t.Errorf("Expected status='ok', got %v", parsed["status"])
}
}
// mockAuthExecutor is a mock for testing PipelineSummarizerExecutor.
type mockAuthExecutor struct {
response []byte
err error
called bool
lastReq interface{}
lastOpts interface{}
}
func (m *mockAuthExecutor) Execute(ctx context.Context, providers []string, req interface{}, opts interface{}) ([]byte, error) {
m.called = true
m.lastReq = req
m.lastOpts = opts
return m.response, m.err
}
// TestPipelineSummarizerExecutor_WithAdapter tests the full pipeline with the adapter integration.
func TestPipelineSummarizerExecutor_WithAdapter(t *testing.T) {
// Create a valid OpenAI-style response
responseContent := "This is a summarized version of the conversation."
openAIResponse := map[string]interface{}{
"choices": []map[string]interface{}{
{
"message": map[string]interface{}{
"content": responseContent,
},
},
},
}
responseBytes, _ := json.Marshal(openAIResponse)
// Create mock core manager that returns a struct with Payload
mockManager := &mockCoreManagerExecutor{
response: responseWithPayload{
Payload: responseBytes,
Status: 200,
},
err: nil,
}
// Create adapter wrapping the mock manager
adapter := NewManagerAuthAdapter(mockManager)
// Create pipeline executor with the adapter
executor := NewPipelineSummarizerExecutor(adapter, []string{"openai"})
ctx := context.Background()
model := "gpt-4"
prompt := "Please summarize the following conversation..."
result, err := executor.Summarize(ctx, model, prompt)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
if result != responseContent {
t.Errorf("Expected result '%s', got '%s'", responseContent, result)
}
}
// TestPipelineSummarizerExecutor_WithAdapter_BytesResponse tests pipeline with direct bytes response.
func TestPipelineSummarizerExecutor_WithAdapter_BytesResponse(t *testing.T) {
responseContent := "Summary from bytes response."
openAIResponse := map[string]interface{}{
"choices": []map[string]interface{}{
{
"message": map[string]interface{}{
"content": responseContent,
},
},
},
}
responseBytes, _ := json.Marshal(openAIResponse)
// Mock manager returns bytes directly
mockManager := &mockCoreManagerExecutor{
response: responseBytes,
err: nil,
}
adapter := NewManagerAuthAdapter(mockManager)
executor := NewPipelineSummarizerExecutor(adapter, []string{"anthropic"})
ctx := context.Background()
result, err := executor.Summarize(ctx, "claude-3-opus", "Summarize this...")
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
if result != responseContent {
t.Errorf("Expected result '%s', got '%s'", responseContent, result)
}
}
// TestPipelineSummarizerExecutor_WithAdapter_ManagerError tests error propagation through the pipeline.
func TestPipelineSummarizerExecutor_WithAdapter_ManagerError(t *testing.T) {
mockManager := &mockCoreManagerExecutor{
response: nil,
err: errors.New("rate limit exceeded"),
}
adapter := NewManagerAuthAdapter(mockManager)
executor := NewPipelineSummarizerExecutor(adapter, []string{"openai"})
ctx := context.Background()
result, err := executor.Summarize(ctx, "gpt-4", "Summarize this...")
if err == nil {
t.Fatal("Expected error to be propagated, got nil")
}
if result != "" {
t.Errorf("Expected empty result on error, got '%s'", result)
}
// Error should be wrapped with context
expectedSubstring := "rate limit exceeded"
if !containsSubstring(err.Error(), expectedSubstring) {
t.Errorf("Expected error to contain '%s', got '%s'", expectedSubstring, err.Error())
}
}
// TestNewManagerAuthAdapter tests the constructor.
func TestNewManagerAuthAdapter(t *testing.T) {
mock := &mockCoreManagerExecutor{}
adapter := NewManagerAuthAdapter(mock)
if adapter == nil {
t.Fatal("Expected non-nil adapter")
}
if adapter.manager != mock {
t.Error("Expected adapter.manager to be set to the provided mock")
}
}
// TestNewManagerAuthAdapter_NilInput tests constructor with nil input.
func TestNewManagerAuthAdapter_NilInput(t *testing.T) {
adapter := NewManagerAuthAdapter(nil)
if adapter == nil {
t.Fatal("Expected non-nil adapter even with nil input")
}
// The adapter should still be created, but Execute should fail
_, err := adapter.Execute(context.Background(), nil, nil, nil)
if err == nil {
t.Error("Expected error when executing with nil manager")
}
}
// containsSubstring is a helper function to check if a string contains a substring.
func containsSubstring(s, substr string) bool {
return len(s) >= len(substr) && (s == substr || len(s) > 0 && containsSubstringHelper(s, substr))
}
func containsSubstringHelper(s, substr string) bool {
for i := 0; i <= len(s)-len(substr); i++ {
if s[i:i+len(substr)] == substr {
return true
}
}
return false
}
-4
View File
@@ -5,7 +5,6 @@ package misc
import (
"embed"
_ "embed"
"strings"
)
@@ -21,7 +20,6 @@ func CodexInstructionsForModel(modelName, systemInstructions string) (bool, stri
last51Prompt := ""
last52Prompt := ""
last52CodexPrompt := ""
// lastReviewPrompt := ""
for _, entry := range entries {
content, _ := codexInstructionsDir.ReadFile("codex_instructions/" + entry.Name())
if strings.HasPrefix(systemInstructions, string(content)) {
@@ -39,8 +37,6 @@ func CodexInstructionsForModel(modelName, systemInstructions string) (bool, stri
last52Prompt = string(content)
} else if strings.HasPrefix(entry.Name(), "gpt-5.2-codex_prompt.md") {
last52CodexPrompt = string(content)
} else if strings.HasPrefix(entry.Name(), "review_prompt.md") {
// lastReviewPrompt = string(content)
}
}
if strings.Contains(modelName, "codex-max") {
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,913 @@
package executor
import (
"bytes"
"compress/gzip"
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/andybalholm/brotli"
"github.com/router-for-me/CLIProxyAPI/v6/internal/config"
cliproxyauth "github.com/router-for-me/CLIProxyAPI/v6/sdk/cliproxy/auth"
)
func TestClaudeExecutor_Identifier(t *testing.T) {
tests := []struct {
name string
executor *ClaudeExecutor
want string
}{
{
name: "returns claude identifier",
executor: NewClaudeExecutor(nil),
want: "claude",
},
{
name: "returns claude identifier with config",
executor: NewClaudeExecutor(&config.Config{}),
want: "claude",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := tt.executor.Identifier()
if got != tt.want {
t.Errorf("ClaudeExecutor.Identifier() = %q, want %q", got, tt.want)
}
})
}
}
func TestClaudeExecutor_claudeCreds_APIKey(t *testing.T) {
tests := []struct {
name string
auth *cliproxyauth.Auth
wantAPIKey string
wantBaseURL string
}{
{
name: "nil auth returns empty values",
auth: nil,
wantAPIKey: "",
wantBaseURL: "",
},
{
name: "auth with api_key in attributes",
auth: &cliproxyauth.Auth{
Attributes: map[string]string{
"api_key": "sk-test-key-123",
},
},
wantAPIKey: "sk-test-key-123",
wantBaseURL: "",
},
{
name: "auth with api_key and base_url in attributes",
auth: &cliproxyauth.Auth{
Attributes: map[string]string{
"api_key": "sk-test-key-456",
"base_url": "https://custom.anthropic.com",
},
},
wantAPIKey: "sk-test-key-456",
wantBaseURL: "https://custom.anthropic.com",
},
{
name: "auth with empty attributes",
auth: &cliproxyauth.Auth{
Attributes: map[string]string{},
},
wantAPIKey: "",
wantBaseURL: "",
},
{
name: "auth with nil attributes",
auth: &cliproxyauth.Auth{
Attributes: nil,
},
wantAPIKey: "",
wantBaseURL: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotAPIKey, gotBaseURL := claudeCreds(tt.auth)
if gotAPIKey != tt.wantAPIKey {
t.Errorf("claudeCreds() apiKey = %q, want %q", gotAPIKey, tt.wantAPIKey)
}
if gotBaseURL != tt.wantBaseURL {
t.Errorf("claudeCreds() baseURL = %q, want %q", gotBaseURL, tt.wantBaseURL)
}
})
}
}
func TestClaudeExecutor_claudeCreds_OAuth(t *testing.T) {
tests := []struct {
name string
auth *cliproxyauth.Auth
wantAPIKey string
wantBaseURL string
}{
{
name: "auth with access_token in metadata (OAuth)",
auth: &cliproxyauth.Auth{
Metadata: map[string]any{
"access_token": "oauth-access-token-789",
},
},
wantAPIKey: "oauth-access-token-789",
wantBaseURL: "",
},
{
name: "auth with access_token and email in metadata",
auth: &cliproxyauth.Auth{
Metadata: map[string]any{
"access_token": "oauth-token-abc",
"email": "user@example.com",
},
},
wantAPIKey: "oauth-token-abc",
wantBaseURL: "",
},
{
name: "api_key in attributes takes precedence over metadata",
auth: &cliproxyauth.Auth{
Attributes: map[string]string{
"api_key": "sk-api-key-priority",
},
Metadata: map[string]any{
"access_token": "oauth-token-ignored",
},
},
wantAPIKey: "sk-api-key-priority",
wantBaseURL: "",
},
{
name: "empty api_key falls back to access_token",
auth: &cliproxyauth.Auth{
Attributes: map[string]string{
"api_key": "",
},
Metadata: map[string]any{
"access_token": "oauth-fallback-token",
},
},
wantAPIKey: "oauth-fallback-token",
wantBaseURL: "",
},
{
name: "access_token as non-string is ignored",
auth: &cliproxyauth.Auth{
Metadata: map[string]any{
"access_token": 12345,
},
},
wantAPIKey: "",
wantBaseURL: "",
},
{
name: "nil metadata with nil attributes",
auth: &cliproxyauth.Auth{
Attributes: nil,
Metadata: nil,
},
wantAPIKey: "",
wantBaseURL: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotAPIKey, gotBaseURL := claudeCreds(tt.auth)
if gotAPIKey != tt.wantAPIKey {
t.Errorf("claudeCreds() apiKey = %q, want %q", gotAPIKey, tt.wantAPIKey)
}
if gotBaseURL != tt.wantBaseURL {
t.Errorf("claudeCreds() baseURL = %q, want %q", gotBaseURL, tt.wantBaseURL)
}
})
}
}
func TestClaudeExecutor_applyClaudeHeaders(t *testing.T) {
tests := []struct {
name string
url string
auth *cliproxyauth.Auth
apiKey string
stream bool
extraBetas []string
checkHeader func(t *testing.T, h http.Header)
}{
{
name: "sets x-api-key for api.anthropic.com with api_key auth",
url: "https://api.anthropic.com/v1/messages",
apiKey: "sk-test-api-key",
auth: &cliproxyauth.Auth{
Attributes: map[string]string{
"api_key": "sk-test-api-key",
},
},
stream: false,
extraBetas: nil,
checkHeader: func(t *testing.T, h http.Header) {
if got := h.Get("x-api-key"); got != "sk-test-api-key" {
t.Errorf("x-api-key = %q, want %q", got, "sk-test-api-key")
}
if got := h.Get("Authorization"); got != "" {
t.Errorf("Authorization should be empty for x-api-key auth, got %q", got)
}
},
},
{
name: "sets Authorization Bearer for non-anthropic URL",
url: "https://custom.api.com/v1/messages",
apiKey: "sk-custom-key",
auth: &cliproxyauth.Auth{
Attributes: map[string]string{
"api_key": "sk-custom-key",
},
},
stream: false,
extraBetas: nil,
checkHeader: func(t *testing.T, h http.Header) {
if got := h.Get("Authorization"); got != "Bearer sk-custom-key" {
t.Errorf("Authorization = %q, want %q", got, "Bearer sk-custom-key")
}
},
},
{
name: "sets Accept to text/event-stream when streaming",
url: "https://api.anthropic.com/v1/messages",
apiKey: "sk-test",
auth: nil,
stream: true,
extraBetas: nil,
checkHeader: func(t *testing.T, h http.Header) {
if got := h.Get("Accept"); got != "text/event-stream" {
t.Errorf("Accept = %q, want %q", got, "text/event-stream")
}
},
},
{
name: "sets Accept to application/json when not streaming",
url: "https://api.anthropic.com/v1/messages",
apiKey: "sk-test",
auth: nil,
stream: false,
extraBetas: nil,
checkHeader: func(t *testing.T, h http.Header) {
if got := h.Get("Accept"); got != "application/json" {
t.Errorf("Accept = %q, want %q", got, "application/json")
}
},
},
{
name: "sets Content-Type to application/json",
url: "https://api.anthropic.com/v1/messages",
apiKey: "sk-test",
auth: nil,
stream: false,
extraBetas: nil,
checkHeader: func(t *testing.T, h http.Header) {
if got := h.Get("Content-Type"); got != "application/json" {
t.Errorf("Content-Type = %q, want %q", got, "application/json")
}
},
},
{
name: "sets Anthropic-Beta header with default betas",
url: "https://api.anthropic.com/v1/messages",
apiKey: "sk-test",
auth: nil,
stream: false,
extraBetas: nil,
checkHeader: func(t *testing.T, h http.Header) {
beta := h.Get("Anthropic-Beta")
if !strings.Contains(beta, "oauth-2025-04-20") {
t.Errorf("Anthropic-Beta should contain oauth-2025-04-20, got %q", beta)
}
if !strings.Contains(beta, "claude-code-20250219") {
t.Errorf("Anthropic-Beta should contain claude-code-20250219, got %q", beta)
}
},
},
{
name: "merges extra betas into Anthropic-Beta header",
url: "https://api.anthropic.com/v1/messages",
apiKey: "sk-test",
auth: nil,
stream: false,
extraBetas: []string{"custom-beta-2025", "another-beta"},
checkHeader: func(t *testing.T, h http.Header) {
beta := h.Get("Anthropic-Beta")
if !strings.Contains(beta, "custom-beta-2025") {
t.Errorf("Anthropic-Beta should contain custom-beta-2025, got %q", beta)
}
if !strings.Contains(beta, "another-beta") {
t.Errorf("Anthropic-Beta should contain another-beta, got %q", beta)
}
},
},
{
name: "sets Anthropic-Version header",
url: "https://api.anthropic.com/v1/messages",
apiKey: "sk-test",
auth: nil,
stream: false,
extraBetas: nil,
checkHeader: func(t *testing.T, h http.Header) {
if got := h.Get("Anthropic-Version"); got != "2023-06-01" {
t.Errorf("Anthropic-Version = %q, want %q", got, "2023-06-01")
}
},
},
{
name: "sets Connection header to keep-alive",
url: "https://api.anthropic.com/v1/messages",
apiKey: "sk-test",
auth: nil,
stream: false,
extraBetas: nil,
checkHeader: func(t *testing.T, h http.Header) {
if got := h.Get("Connection"); got != "keep-alive" {
t.Errorf("Connection = %q, want %q", got, "keep-alive")
}
},
},
{
name: "sets Accept-Encoding header",
url: "https://api.anthropic.com/v1/messages",
apiKey: "sk-test",
auth: nil,
stream: false,
extraBetas: nil,
checkHeader: func(t *testing.T, h http.Header) {
encoding := h.Get("Accept-Encoding")
if !strings.Contains(encoding, "gzip") {
t.Errorf("Accept-Encoding should contain gzip, got %q", encoding)
}
if !strings.Contains(encoding, "br") {
t.Errorf("Accept-Encoding should contain br, got %q", encoding)
}
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
req, err := http.NewRequest(http.MethodPost, tt.url, nil)
if err != nil {
t.Fatalf("failed to create request: %v", err)
}
applyClaudeHeaders(req, tt.auth, tt.apiKey, tt.stream, tt.extraBetas)
tt.checkHeader(t, req.Header)
})
}
}
func TestClaudeExecutor_injectThinkingConfig(t *testing.T) {
executor := NewClaudeExecutor(&config.Config{})
tests := []struct {
name string
modelName string
metadata map[string]any
body string
wantCheck func(t *testing.T, result []byte)
}{
{
name: "non-thinking model returns body unchanged",
modelName: "claude-3-5-sonnet-20240620",
metadata: nil,
body: `{"model":"claude-3-5-sonnet-20240620","messages":[]}`,
wantCheck: func(t *testing.T, result []byte) {
if strings.Contains(string(result), "thinking") {
t.Error("non-thinking model should not have thinking config")
}
},
},
{
name: "thinking model without metadata returns body unchanged",
modelName: "claude-sonnet-4-5-thinking",
metadata: nil,
body: `{"model":"claude-sonnet-4-5-thinking","messages":[]}`,
wantCheck: func(t *testing.T, result []byte) {
// Without metadata and without registry support, no thinking config should be injected
if strings.Contains(string(result), `"thinking"`) {
t.Error("thinking model without metadata should not have thinking config injected")
}
},
},
{
name: "body with existing thinking config is unchanged",
modelName: "claude-sonnet-4-5-thinking",
metadata: map[string]any{
"thinking_budget": 5000,
},
body: `{"model":"claude-sonnet-4-5-thinking","messages":[],"thinking":{"type":"enabled","budget_tokens":10000}}`,
wantCheck: func(t *testing.T, result []byte) {
// Existing config should be preserved
if !strings.Contains(string(result), `"budget_tokens":10000`) {
t.Error("existing thinking config should be preserved")
}
},
},
{
name: "model not in registry returns body unchanged even with budget",
modelName: "claude-sonnet-4-5-thinking",
metadata: map[string]any{
"thinking_budget": 8000,
},
body: `{"model":"claude-sonnet-4-5-thinking","messages":[]}`,
wantCheck: func(t *testing.T, result []byte) {
// Without registry support, injectThinkingConfig relies on ModelSupportsThinking
// which requires the model to be registered. In test environment without registry,
// the body should remain unchanged.
// This tests the actual behavior of the function.
resultStr := string(result)
// The function checks registry first - if model not found, returns unchanged
if strings.Contains(resultStr, `"thinking":{"type":"enabled"`) {
// If thinking was injected, that means registry was populated
// which is fine - but in clean test environment, it won't be
}
// The test passes if no error occurs - behavior depends on registry state
},
},
{
name: "empty model name returns body unchanged",
modelName: "",
metadata: map[string]any{
"thinking_budget": 8000,
},
body: `{"model":"","messages":[]}`,
wantCheck: func(t *testing.T, result []byte) {
if strings.Contains(string(result), `"thinking":{"type":"enabled"`) {
t.Error("empty model should not have thinking config injected")
}
},
},
{
name: "nil metadata returns body unchanged",
modelName: "claude-sonnet-4-5-thinking",
metadata: nil,
body: `{"model":"claude-sonnet-4-5-thinking","messages":[]}`,
wantCheck: func(t *testing.T, result []byte) {
if strings.Contains(string(result), `"thinking":{"type":"enabled"`) {
t.Error("nil metadata should not inject thinking config")
}
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := executor.injectThinkingConfig(tt.modelName, tt.metadata, []byte(tt.body))
tt.wantCheck(t, result)
})
}
}
func TestClaudeExecutor_ensureMaxTokensForThinking(t *testing.T) {
tests := []struct {
name string
modelName string
body string
wantCheck func(t *testing.T, result []byte)
}{
{
name: "body without thinking returns unchanged",
modelName: "claude-3-5-sonnet",
body: `{"model":"claude-3-5-sonnet","max_tokens":1000}`,
wantCheck: func(t *testing.T, result []byte) {
if !strings.Contains(string(result), `"max_tokens":1000`) {
t.Error("max_tokens should remain 1000")
}
},
},
{
name: "thinking disabled returns unchanged",
modelName: "claude-sonnet-4-5-thinking",
body: `{"model":"claude-sonnet-4-5-thinking","max_tokens":1000,"thinking":{"type":"disabled"}}`,
wantCheck: func(t *testing.T, result []byte) {
if !strings.Contains(string(result), `"max_tokens":1000`) {
t.Error("max_tokens should remain 1000 when thinking is disabled")
}
},
},
{
name: "thinking enabled with zero budget returns unchanged",
modelName: "claude-sonnet-4-5-thinking",
body: `{"model":"claude-sonnet-4-5-thinking","max_tokens":1000,"thinking":{"type":"enabled","budget_tokens":0}}`,
wantCheck: func(t *testing.T, result []byte) {
if !strings.Contains(string(result), `"max_tokens":1000`) {
t.Error("max_tokens should remain 1000 when budget is 0")
}
},
},
{
name: "thinking enabled with budget and low max_tokens is adjusted",
modelName: "claude-sonnet-4-5-thinking",
body: `{"model":"claude-sonnet-4-5-thinking","max_tokens":1000,"thinking":{"type":"enabled","budget_tokens":5000}}`,
wantCheck: func(t *testing.T, result []byte) {
// max_tokens should be increased to accommodate budget
if strings.Contains(string(result), `"max_tokens":1000`) {
t.Error("max_tokens should be increased when budget_tokens is higher")
}
},
},
{
name: "thinking enabled with adequate max_tokens is unchanged",
modelName: "claude-sonnet-4-5-thinking",
body: `{"model":"claude-sonnet-4-5-thinking","max_tokens":100000,"thinking":{"type":"enabled","budget_tokens":5000}}`,
wantCheck: func(t *testing.T, result []byte) {
if !strings.Contains(string(result), `"max_tokens":100000`) {
t.Error("max_tokens should remain 100000 when it's already sufficient")
}
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := ensureMaxTokensForThinking(tt.modelName, []byte(tt.body))
tt.wantCheck(t, result)
})
}
}
func TestDecodeResponseBody_Gzip(t *testing.T) {
tests := []struct {
name string
content string
contentEncoding string
wantContent string
wantErr bool
}{
{
name: "valid gzip content",
content: "Hello, World!",
contentEncoding: "gzip",
wantContent: "Hello, World!",
wantErr: false,
},
{
name: "gzip with uppercase encoding",
content: "Test content",
contentEncoding: "GZIP",
wantContent: "Test content",
wantErr: false,
},
{
name: "gzip with mixed case",
content: "Mixed case test",
contentEncoding: "Gzip",
wantContent: "Mixed case test",
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Create gzip compressed content
var buf bytes.Buffer
gzWriter := gzip.NewWriter(&buf)
if _, err := gzWriter.Write([]byte(tt.content)); err != nil {
t.Fatalf("failed to write gzip content: %v", err)
}
if err := gzWriter.Close(); err != nil {
t.Fatalf("failed to close gzip writer: %v", err)
}
body := io.NopCloser(bytes.NewReader(buf.Bytes()))
decoded, err := decodeResponseBody(body, tt.contentEncoding)
if (err != nil) != tt.wantErr {
t.Errorf("decodeResponseBody() error = %v, wantErr %v", err, tt.wantErr)
return
}
if err == nil {
content, readErr := io.ReadAll(decoded)
if readErr != nil {
t.Fatalf("failed to read decoded content: %v", readErr)
}
if err := decoded.Close(); err != nil {
t.Errorf("failed to close decoded body: %v", err)
}
if string(content) != tt.wantContent {
t.Errorf("decoded content = %q, want %q", string(content), tt.wantContent)
}
}
})
}
}
func TestDecodeResponseBody_Brotli(t *testing.T) {
tests := []struct {
name string
content string
contentEncoding string
wantContent string
wantErr bool
}{
{
name: "valid brotli content",
content: "Hello, Brotli World!",
contentEncoding: "br",
wantContent: "Hello, Brotli World!",
wantErr: false,
},
{
name: "brotli with uppercase encoding",
content: "Brotli test",
contentEncoding: "BR",
wantContent: "Brotli test",
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Create brotli compressed content
var buf bytes.Buffer
brWriter := brotli.NewWriter(&buf)
if _, err := brWriter.Write([]byte(tt.content)); err != nil {
t.Fatalf("failed to write brotli content: %v", err)
}
if err := brWriter.Close(); err != nil {
t.Fatalf("failed to close brotli writer: %v", err)
}
body := io.NopCloser(bytes.NewReader(buf.Bytes()))
decoded, err := decodeResponseBody(body, tt.contentEncoding)
if (err != nil) != tt.wantErr {
t.Errorf("decodeResponseBody() error = %v, wantErr %v", err, tt.wantErr)
return
}
if err == nil {
content, readErr := io.ReadAll(decoded)
if readErr != nil {
t.Fatalf("failed to read decoded content: %v", readErr)
}
if err := decoded.Close(); err != nil {
t.Errorf("failed to close decoded body: %v", err)
}
if string(content) != tt.wantContent {
t.Errorf("decoded content = %q, want %q", string(content), tt.wantContent)
}
}
})
}
}
func TestDecodeResponseBody_Identity(t *testing.T) {
tests := []struct {
name string
content string
contentEncoding string
wantContent string
wantErr bool
}{
{
name: "empty content encoding returns body as-is",
content: "Plain text content",
contentEncoding: "",
wantContent: "Plain text content",
wantErr: false,
},
{
name: "identity encoding returns body as-is",
content: "Identity encoded content",
contentEncoding: "identity",
wantContent: "Identity encoded content",
wantErr: false,
},
{
name: "identity with uppercase",
content: "IDENTITY test",
contentEncoding: "IDENTITY",
wantContent: "IDENTITY test",
wantErr: false,
},
{
name: "unknown encoding returns body as-is",
content: "Unknown encoding content",
contentEncoding: "unknown-encoding",
wantContent: "Unknown encoding content",
wantErr: false,
},
{
name: "nil body returns error",
content: "",
contentEncoding: "",
wantContent: "",
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var body io.ReadCloser
if tt.name == "nil body returns error" {
body = nil
} else {
body = io.NopCloser(strings.NewReader(tt.content))
}
decoded, err := decodeResponseBody(body, tt.contentEncoding)
if (err != nil) != tt.wantErr {
t.Errorf("decodeResponseBody() error = %v, wantErr %v", err, tt.wantErr)
return
}
if err == nil && decoded != nil {
content, readErr := io.ReadAll(decoded)
if readErr != nil {
t.Fatalf("failed to read decoded content: %v", readErr)
}
if err := decoded.Close(); err != nil {
t.Errorf("failed to close decoded body: %v", err)
}
if string(content) != tt.wantContent {
t.Errorf("decoded content = %q, want %q", string(content), tt.wantContent)
}
}
})
}
}
func TestClaudeExecutor_extractAndRemoveBetas(t *testing.T) {
tests := []struct {
name string
body string
wantBetas []string
wantInBody bool
}{
{
name: "body without betas",
body: `{"model":"claude-3-5-sonnet","messages":[]}`,
wantBetas: nil,
wantInBody: false,
},
{
name: "body with array of betas",
body: `{"model":"claude-3-5-sonnet","betas":["beta-1","beta-2"],"messages":[]}`,
wantBetas: []string{"beta-1", "beta-2"},
wantInBody: false,
},
{
name: "body with single string beta",
body: `{"model":"claude-3-5-sonnet","betas":"single-beta","messages":[]}`,
wantBetas: []string{"single-beta"},
wantInBody: false,
},
{
name: "body with empty betas array",
body: `{"model":"claude-3-5-sonnet","betas":[],"messages":[]}`,
wantBetas: nil,
wantInBody: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotBetas, gotBody := extractAndRemoveBetas([]byte(tt.body))
// Check betas
if len(gotBetas) != len(tt.wantBetas) {
t.Errorf("extractAndRemoveBetas() betas length = %d, want %d", len(gotBetas), len(tt.wantBetas))
}
for i, beta := range gotBetas {
if i < len(tt.wantBetas) && beta != tt.wantBetas[i] {
t.Errorf("extractAndRemoveBetas() beta[%d] = %q, want %q", i, beta, tt.wantBetas[i])
}
}
// Check that betas key is removed from body
if strings.Contains(string(gotBody), `"betas"`) && !tt.wantInBody {
t.Error("extractAndRemoveBetas() should remove betas from body")
}
})
}
}
func TestClaudeExecutor_Integration_MockServer(t *testing.T) {
// Create a mock HTTP server that simulates Claude API responses
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Verify request headers
if r.Header.Get("Content-Type") != "application/json" {
t.Errorf("Content-Type = %q, want %q", r.Header.Get("Content-Type"), "application/json")
}
// Check for API key or Authorization header
apiKey := r.Header.Get("x-api-key")
authHeader := r.Header.Get("Authorization")
if apiKey == "" && authHeader == "" {
t.Error("Expected either x-api-key or Authorization header")
}
// Return a mock response
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write([]byte(`{
"id": "msg_test123",
"type": "message",
"role": "assistant",
"content": [{"type": "text", "text": "Hello from mock server!"}],
"model": "claude-3-5-sonnet-20240620",
"stop_reason": "end_turn",
"usage": {"input_tokens": 10, "output_tokens": 20}
}`))
}))
defer server.Close()
t.Run("mock server accepts requests with valid headers", func(t *testing.T) {
req, err := http.NewRequest(http.MethodPost, server.URL+"/v1/messages", strings.NewReader(`{"model":"claude-3-5-sonnet","messages":[]}`))
if err != nil {
t.Fatalf("failed to create request: %v", err)
}
auth := &cliproxyauth.Auth{
Attributes: map[string]string{
"api_key": "sk-test-key",
},
}
applyClaudeHeaders(req, auth, "sk-test-key", false, nil)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
t.Fatalf("request failed: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
t.Errorf("status code = %d, want %d", resp.StatusCode, http.StatusOK)
}
body, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatalf("failed to read response body: %v", err)
}
if !strings.Contains(string(body), "Hello from mock server!") {
t.Errorf("unexpected response body: %s", string(body))
}
})
}
func TestClaudeExecutor_checkSystemInstructions(t *testing.T) {
tests := []struct {
name string
payload string
wantCheck func(t *testing.T, result []byte)
}{
{
name: "payload without system adds claude code instructions",
payload: `{"model":"claude-3-5-sonnet","messages":[]}`,
wantCheck: func(t *testing.T, result []byte) {
if !strings.Contains(string(result), "Claude Code") {
t.Error("should add Claude Code instructions")
}
},
},
{
name: "payload with string system is converted to array",
payload: `{"model":"claude-3-5-sonnet","system":"Custom system prompt","messages":[]}`,
wantCheck: func(t *testing.T, result []byte) {
if !strings.Contains(string(result), "Claude Code") {
t.Error("should add Claude Code instructions")
}
},
},
{
name: "payload with existing claude code instructions is unchanged",
payload: `{"model":"claude-3-5-sonnet","system":[{"type":"text","text":"You are Claude Code, Anthropic's official CLI for Claude."}],"messages":[]}`,
wantCheck: func(t *testing.T, result []byte) {
if !strings.Contains(string(result), "Claude Code") {
t.Error("should preserve Claude Code instructions")
}
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := checkSystemInstructions([]byte(tt.payload))
tt.wantCheck(t, result)
})
}
}
+115 -1
View File
@@ -50,6 +50,16 @@ func (e *CodexExecutor) Execute(ctx context.Context, auth *cliproxyauth.Auth, re
defer reporter.trackFailure(ctx, &err)
upstreamModel := util.ResolveOriginalModel(req.Model, req.Metadata)
if upstreamModel == "" {
upstreamModel = req.Model
}
if modelOverride := e.resolveUpstreamModel(upstreamModel, auth); modelOverride != "" {
upstreamModel = modelOverride
} else if !strings.EqualFold(upstreamModel, req.Model) {
if modelOverride := e.resolveUpstreamModel(req.Model, auth); modelOverride != "" {
upstreamModel = modelOverride
}
}
from := opts.SourceFormat
to := sdktranslator.FromString("codex")
@@ -147,6 +157,16 @@ func (e *CodexExecutor) ExecuteStream(ctx context.Context, auth *cliproxyauth.Au
defer reporter.trackFailure(ctx, &err)
upstreamModel := util.ResolveOriginalModel(req.Model, req.Metadata)
if upstreamModel == "" {
upstreamModel = req.Model
}
if modelOverride := e.resolveUpstreamModel(upstreamModel, auth); modelOverride != "" {
upstreamModel = modelOverride
} else if !strings.EqualFold(upstreamModel, req.Model) {
if modelOverride := e.resolveUpstreamModel(req.Model, auth); modelOverride != "" {
upstreamModel = modelOverride
}
}
from := opts.SourceFormat
to := sdktranslator.FromString("codex")
@@ -247,12 +267,22 @@ func (e *CodexExecutor) ExecuteStream(ctx context.Context, auth *cliproxyauth.Au
func (e *CodexExecutor) CountTokens(ctx context.Context, auth *cliproxyauth.Auth, req cliproxyexecutor.Request, opts cliproxyexecutor.Options) (cliproxyexecutor.Response, error) {
upstreamModel := util.ResolveOriginalModel(req.Model, req.Metadata)
if upstreamModel == "" {
upstreamModel = req.Model
}
if modelOverride := e.resolveUpstreamModel(upstreamModel, auth); modelOverride != "" {
upstreamModel = modelOverride
} else if !strings.EqualFold(upstreamModel, req.Model) {
if modelOverride := e.resolveUpstreamModel(req.Model, auth); modelOverride != "" {
upstreamModel = modelOverride
}
}
from := opts.SourceFormat
to := sdktranslator.FromString("codex")
body := sdktranslator.TranslateRequest(from, to, req.Model, bytes.Clone(req.Payload), false)
modelForCounting := req.Model
modelForCounting := upstreamModel
body = ApplyReasoningEffortMetadata(body, req.Metadata, req.Model, "reasoning.effort", false)
body, _ = sjson.SetBytes(body, "model", upstreamModel)
@@ -525,3 +555,87 @@ func codexCreds(a *cliproxyauth.Auth) (apiKey, baseURL string) {
}
return
}
func (e *CodexExecutor) resolveUpstreamModel(alias string, auth *cliproxyauth.Auth) string {
trimmed := strings.TrimSpace(alias)
if trimmed == "" {
return ""
}
entry := e.resolveCodexConfig(auth)
if entry == nil {
return ""
}
normalizedModel, metadata := util.NormalizeThinkingModel(trimmed)
// Candidate names to match against configured aliases/names.
candidates := []string{strings.TrimSpace(normalizedModel)}
if !strings.EqualFold(normalizedModel, trimmed) {
candidates = append(candidates, trimmed)
}
if original := util.ResolveOriginalModel(normalizedModel, metadata); original != "" && !strings.EqualFold(original, normalizedModel) {
candidates = append(candidates, original)
}
for i := range entry.Models {
model := entry.Models[i]
name := strings.TrimSpace(model.Name)
modelAlias := strings.TrimSpace(model.Alias)
for _, candidate := range candidates {
if candidate == "" {
continue
}
if modelAlias != "" && strings.EqualFold(modelAlias, candidate) {
if name != "" {
return name
}
return candidate
}
if name != "" && strings.EqualFold(name, candidate) {
return name
}
}
}
return ""
}
func (e *CodexExecutor) resolveCodexConfig(auth *cliproxyauth.Auth) *config.CodexKey {
if auth == nil || e.cfg == nil {
return nil
}
var attrKey, attrBase string
if auth.Attributes != nil {
attrKey = strings.TrimSpace(auth.Attributes["api_key"])
attrBase = strings.TrimSpace(auth.Attributes["base_url"])
}
for i := range e.cfg.CodexKey {
entry := &e.cfg.CodexKey[i]
cfgKey := strings.TrimSpace(entry.APIKey)
cfgBase := strings.TrimSpace(entry.BaseURL)
if attrKey != "" && attrBase != "" {
if strings.EqualFold(cfgKey, attrKey) && strings.EqualFold(cfgBase, attrBase) {
return entry
}
continue
}
if attrKey != "" && strings.EqualFold(cfgKey, attrKey) {
if cfgBase == "" || strings.EqualFold(cfgBase, attrBase) {
return entry
}
}
if attrKey == "" && attrBase != "" && strings.EqualFold(cfgBase, attrBase) {
return entry
}
}
if attrKey != "" {
for i := range e.cfg.CodexKey {
entry := &e.cfg.CodexKey[i]
if strings.EqualFold(strings.TrimSpace(entry.APIKey), attrKey) {
return entry
}
}
}
return nil
}
@@ -0,0 +1,936 @@
package executor
import (
"context"
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/router-for-me/CLIProxyAPI/v6/internal/config"
cliproxyauth "github.com/router-for-me/CLIProxyAPI/v6/sdk/cliproxy/auth"
cliproxyexecutor "github.com/router-for-me/CLIProxyAPI/v6/sdk/cliproxy/executor"
sdktranslator "github.com/router-for-me/CLIProxyAPI/v6/sdk/translator"
)
func TestGeminiExecutor_Identifier(t *testing.T) {
t.Parallel()
tests := []struct {
name string
want string
}{
{
name: "returns gemini identifier",
want: "gemini",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
e := NewGeminiExecutor(nil)
if got := e.Identifier(); got != tt.want {
t.Errorf("Identifier() = %v, want %v", got, tt.want)
}
})
}
}
func TestGeminiExecutor_geminiCreds_APIKey(t *testing.T) {
t.Parallel()
tests := []struct {
name string
auth *cliproxyauth.Auth
wantAPIKey string
wantBearer string
}{
{
name: "nil auth returns empty credentials",
auth: nil,
wantAPIKey: "",
wantBearer: "",
},
{
name: "auth with api_key in attributes",
auth: &cliproxyauth.Auth{
ID: "test-1",
Attributes: map[string]string{
"api_key": "test-api-key-123",
},
},
wantAPIKey: "test-api-key-123",
wantBearer: "",
},
{
name: "auth with empty api_key",
auth: &cliproxyauth.Auth{
ID: "test-2",
Attributes: map[string]string{
"api_key": "",
},
},
wantAPIKey: "",
wantBearer: "",
},
{
name: "auth with nil attributes",
auth: &cliproxyauth.Auth{
ID: "test-3",
Attributes: nil,
},
wantAPIKey: "",
wantBearer: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotAPIKey, gotBearer := geminiCreds(tt.auth)
if gotAPIKey != tt.wantAPIKey {
t.Errorf("geminiCreds() apiKey = %v, want %v", gotAPIKey, tt.wantAPIKey)
}
if gotBearer != tt.wantBearer {
t.Errorf("geminiCreds() bearer = %v, want %v", gotBearer, tt.wantBearer)
}
})
}
}
func TestGeminiExecutor_geminiCreds_OAuth(t *testing.T) {
t.Parallel()
tests := []struct {
name string
auth *cliproxyauth.Auth
wantAPIKey string
wantBearer string
}{
{
name: "auth with access_token in metadata",
auth: &cliproxyauth.Auth{
ID: "oauth-1",
Metadata: map[string]any{
"access_token": "bearer-token-abc",
},
},
wantAPIKey: "",
wantBearer: "bearer-token-abc",
},
{
name: "auth with nested token.access_token in metadata",
auth: &cliproxyauth.Auth{
ID: "oauth-2",
Metadata: map[string]any{
"token": map[string]any{
"access_token": "nested-bearer-token-xyz",
},
},
},
wantAPIKey: "",
wantBearer: "nested-bearer-token-xyz",
},
{
name: "auth with both api_key and access_token",
auth: &cliproxyauth.Auth{
ID: "mixed-1",
Attributes: map[string]string{
"api_key": "api-key-value",
},
Metadata: map[string]any{
"access_token": "oauth-token-value",
},
},
wantAPIKey: "api-key-value",
wantBearer: "oauth-token-value",
},
{
name: "auth with nil metadata",
auth: &cliproxyauth.Auth{
ID: "oauth-3",
Metadata: nil,
},
wantAPIKey: "",
wantBearer: "",
},
{
name: "auth with empty access_token",
auth: &cliproxyauth.Auth{
ID: "oauth-4",
Metadata: map[string]any{
"access_token": "",
},
},
wantAPIKey: "",
wantBearer: "",
},
{
name: "nested token with empty access_token",
auth: &cliproxyauth.Auth{
ID: "oauth-5",
Metadata: map[string]any{
"token": map[string]any{
"access_token": "",
},
},
},
wantAPIKey: "",
wantBearer: "",
},
{
name: "nested token nil map",
auth: &cliproxyauth.Auth{
ID: "oauth-6",
Metadata: map[string]any{
"token": nil,
},
},
wantAPIKey: "",
wantBearer: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotAPIKey, gotBearer := geminiCreds(tt.auth)
if gotAPIKey != tt.wantAPIKey {
t.Errorf("geminiCreds() apiKey = %v, want %v", gotAPIKey, tt.wantAPIKey)
}
if gotBearer != tt.wantBearer {
t.Errorf("geminiCreds() bearer = %v, want %v", gotBearer, tt.wantBearer)
}
})
}
}
func TestGeminiExecutor_buildURL(t *testing.T) {
t.Parallel()
tests := []struct {
name string
auth *cliproxyauth.Auth
wantURL string
}{
{
name: "nil auth uses default endpoint",
auth: nil,
wantURL: glEndpoint,
},
{
name: "auth with nil attributes uses default",
auth: &cliproxyauth.Auth{
ID: "test-1",
Attributes: nil,
},
wantURL: glEndpoint,
},
{
name: "auth with empty base_url uses default",
auth: &cliproxyauth.Auth{
ID: "test-2",
Attributes: map[string]string{
"base_url": "",
},
},
wantURL: glEndpoint,
},
{
name: "auth with whitespace base_url uses default",
auth: &cliproxyauth.Auth{
ID: "test-3",
Attributes: map[string]string{
"base_url": " ",
},
},
wantURL: glEndpoint,
},
{
name: "auth with custom base_url",
auth: &cliproxyauth.Auth{
ID: "test-4",
Attributes: map[string]string{
"base_url": "https://custom.googleapis.com",
},
},
wantURL: "https://custom.googleapis.com",
},
{
name: "auth with trailing slash in base_url",
auth: &cliproxyauth.Auth{
ID: "test-5",
Attributes: map[string]string{
"base_url": "https://custom.googleapis.com/",
},
},
wantURL: "https://custom.googleapis.com",
},
{
name: "auth with multiple trailing slashes",
auth: &cliproxyauth.Auth{
ID: "test-6",
Attributes: map[string]string{
"base_url": "https://custom.googleapis.com///",
},
},
wantURL: "https://custom.googleapis.com",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := resolveGeminiBaseURL(tt.auth)
if got != tt.wantURL {
t.Errorf("resolveGeminiBaseURL() = %v, want %v", got, tt.wantURL)
}
})
}
}
func TestGeminiExecutor_applyGeminiHeaders(t *testing.T) {
t.Parallel()
tests := []struct {
name string
auth *cliproxyauth.Auth
wantHeaders map[string]string
}{
{
name: "nil auth sets no custom headers",
auth: nil,
wantHeaders: map[string]string{},
},
{
name: "auth with nil attributes sets no custom headers",
auth: &cliproxyauth.Auth{
ID: "test-1",
Attributes: nil,
},
wantHeaders: map[string]string{},
},
{
name: "auth with empty attributes sets no custom headers",
auth: &cliproxyauth.Auth{
ID: "test-2",
Attributes: map[string]string{},
},
wantHeaders: map[string]string{},
},
{
name: "auth with header: prefix attribute sets custom header",
auth: &cliproxyauth.Auth{
ID: "test-3",
Attributes: map[string]string{
"header:X-Custom-Header": "custom-value",
},
},
wantHeaders: map[string]string{
"X-Custom-Header": "custom-value",
},
},
{
name: "auth with multiple header: prefix attributes",
auth: &cliproxyauth.Auth{
ID: "test-4",
Attributes: map[string]string{
"header:X-First": "value-1",
"header:X-Second": "value-2",
},
},
wantHeaders: map[string]string{
"X-First": "value-1",
"X-Second": "value-2",
},
},
{
name: "auth with mixed header and non-header attributes",
auth: &cliproxyauth.Auth{
ID: "test-5",
Attributes: map[string]string{
"api_key": "some-key",
"header:X-Custom-Header": "custom-value",
"base_url": "https://example.com",
},
},
wantHeaders: map[string]string{
"X-Custom-Header": "custom-value",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
req := httptest.NewRequest(http.MethodPost, "https://example.com/test", nil)
applyGeminiHeaders(req, tt.auth)
for key, wantValue := range tt.wantHeaders {
gotValue := req.Header.Get(key)
if gotValue != wantValue {
t.Errorf("Header %q = %v, want %v", key, gotValue, wantValue)
}
}
})
}
}
func TestGeminiExecutor_Execute_APIKey(t *testing.T) {
t.Parallel()
tests := []struct {
name string
serverResponse string
serverStatus int
auth *cliproxyauth.Auth
request cliproxyexecutor.Request
opts cliproxyexecutor.Options
wantErr bool
wantErrStatus int
checkResponse func(t *testing.T, resp cliproxyexecutor.Response)
}{
{
name: "successful API key request",
serverResponse: `{
"candidates": [{
"content": {
"parts": [{"text": "Hello from Gemini!"}],
"role": "model"
},
"finishReason": "STOP"
}],
"usageMetadata": {
"promptTokenCount": 10,
"candidatesTokenCount": 5,
"totalTokenCount": 15
}
}`,
serverStatus: http.StatusOK,
auth: &cliproxyauth.Auth{
ID: "api-key-auth",
Provider: "gemini",
Attributes: map[string]string{
"api_key": "test-api-key",
},
},
request: cliproxyexecutor.Request{
Model: "gemini-2.0-flash",
Payload: []byte(`{"contents":[{"parts":[{"text":"Hello"}],"role":"user"}]}`),
Format: sdktranslator.FormatGemini,
},
opts: cliproxyexecutor.Options{
SourceFormat: sdktranslator.FormatGemini,
OriginalRequest: []byte(`{"contents":[{"parts":[{"text":"Hello"}],"role":"user"}]}`),
},
wantErr: false,
checkResponse: func(t *testing.T, resp cliproxyexecutor.Response) {
if len(resp.Payload) == 0 {
t.Error("Expected non-empty response payload")
}
},
},
{
name: "server returns 401 unauthorized",
serverResponse: `{"error": {"code": 401, "message": "API key not valid"}}`,
serverStatus: http.StatusUnauthorized,
auth: &cliproxyauth.Auth{
ID: "invalid-auth",
Provider: "gemini",
Attributes: map[string]string{
"api_key": "invalid-api-key",
},
},
request: cliproxyexecutor.Request{
Model: "gemini-2.0-flash",
Payload: []byte(`{"contents":[{"parts":[{"text":"Hello"}],"role":"user"}]}`),
Format: sdktranslator.FormatGemini,
},
opts: cliproxyexecutor.Options{
SourceFormat: sdktranslator.FormatGemini,
},
wantErr: true,
wantErrStatus: http.StatusUnauthorized,
},
{
name: "server returns 429 rate limited",
serverResponse: `{"error": {"code": 429, "message": "Rate limit exceeded"}}`,
serverStatus: http.StatusTooManyRequests,
auth: &cliproxyauth.Auth{
ID: "rate-limited-auth",
Provider: "gemini",
Attributes: map[string]string{
"api_key": "rate-limited-key",
},
},
request: cliproxyexecutor.Request{
Model: "gemini-2.0-flash",
Payload: []byte(`{"contents":[{"parts":[{"text":"Hello"}],"role":"user"}]}`),
Format: sdktranslator.FormatGemini,
},
opts: cliproxyexecutor.Options{
SourceFormat: sdktranslator.FormatGemini,
},
wantErr: true,
wantErrStatus: http.StatusTooManyRequests,
},
{
name: "server returns 500 internal error",
serverResponse: `{"error": {"code": 500, "message": "Internal server error"}}`,
serverStatus: http.StatusInternalServerError,
auth: &cliproxyauth.Auth{
ID: "server-error-auth",
Provider: "gemini",
Attributes: map[string]string{
"api_key": "test-key",
},
},
request: cliproxyexecutor.Request{
Model: "gemini-2.0-flash",
Payload: []byte(`{"contents":[{"parts":[{"text":"Hello"}],"role":"user"}]}`),
Format: sdktranslator.FormatGemini,
},
opts: cliproxyexecutor.Options{
SourceFormat: sdktranslator.FormatGemini,
},
wantErr: true,
wantErrStatus: http.StatusInternalServerError,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Verify request method
if r.Method != http.MethodPost {
t.Errorf("Expected POST method, got %s", r.Method)
}
// Verify Content-Type header
contentType := r.Header.Get("Content-Type")
if contentType != "application/json" {
t.Errorf("Expected Content-Type application/json, got %s", contentType)
}
// Verify API key header if present
if tt.auth != nil && tt.auth.Attributes != nil {
if apiKey := tt.auth.Attributes["api_key"]; apiKey != "" {
gotAPIKey := r.Header.Get("x-goog-api-key")
if gotAPIKey != apiKey {
t.Errorf("Expected x-goog-api-key %s, got %s", apiKey, gotAPIKey)
}
}
}
// Verify URL contains generateContent action
if !strings.Contains(r.URL.Path, "generateContent") {
t.Errorf("Expected URL to contain generateContent, got %s", r.URL.Path)
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(tt.serverStatus)
_, _ = w.Write([]byte(tt.serverResponse))
}))
defer server.Close()
// Update auth to use test server URL
if tt.auth != nil {
if tt.auth.Attributes == nil {
tt.auth.Attributes = make(map[string]string)
}
tt.auth.Attributes["base_url"] = server.URL
}
cfg := &config.Config{}
executor := NewGeminiExecutor(cfg)
ctx := context.Background()
resp, err := executor.Execute(ctx, tt.auth, tt.request, tt.opts)
if tt.wantErr {
if err == nil {
t.Errorf("Execute() expected error, got nil")
return
}
if statusErr, ok := err.(cliproxyexecutor.StatusError); ok {
if statusErr.StatusCode() != tt.wantErrStatus {
t.Errorf("Execute() error status = %v, want %v", statusErr.StatusCode(), tt.wantErrStatus)
}
}
return
}
if err != nil {
t.Errorf("Execute() unexpected error: %v", err)
return
}
if tt.checkResponse != nil {
tt.checkResponse(t, resp)
}
})
}
}
func TestGeminiExecutor_ExecuteStream(t *testing.T) {
t.Parallel()
tests := []struct {
name string
serverChunks []string
serverStatus int
auth *cliproxyauth.Auth
request cliproxyexecutor.Request
opts cliproxyexecutor.Options
wantErr bool
wantErrStatus int
wantChunkCount int
}{
{
name: "successful streaming request",
serverChunks: []string{
`data: {"candidates":[{"content":{"parts":[{"text":"Hello"}],"role":"model"}}]}`,
`data: {"candidates":[{"content":{"parts":[{"text":" World"}],"role":"model"}}]}`,
`data: {"candidates":[{"content":{"parts":[{"text":"!"}],"role":"model"},"finishReason":"STOP"}],"usageMetadata":{"promptTokenCount":5,"candidatesTokenCount":3,"totalTokenCount":8}}`,
},
serverStatus: http.StatusOK,
auth: &cliproxyauth.Auth{
ID: "stream-auth",
Provider: "gemini",
Attributes: map[string]string{
"api_key": "stream-api-key",
},
},
request: cliproxyexecutor.Request{
Model: "gemini-2.0-flash",
Payload: []byte(`{"contents":[{"parts":[{"text":"Say hello"}],"role":"user"}]}`),
Format: sdktranslator.FormatGemini,
},
opts: cliproxyexecutor.Options{
Stream: true,
SourceFormat: sdktranslator.FormatGemini,
OriginalRequest: []byte(`{"contents":[{"parts":[{"text":"Say hello"}],"role":"user"}]}`),
},
wantErr: false,
wantChunkCount: 3,
},
{
name: "streaming request with 401 error",
serverChunks: []string{},
serverStatus: http.StatusUnauthorized,
auth: &cliproxyauth.Auth{
ID: "invalid-stream-auth",
Provider: "gemini",
Attributes: map[string]string{
"api_key": "invalid-stream-key",
},
},
request: cliproxyexecutor.Request{
Model: "gemini-2.0-flash",
Payload: []byte(`{"contents":[{"parts":[{"text":"Say hello"}],"role":"user"}]}`),
Format: sdktranslator.FormatGemini,
},
opts: cliproxyexecutor.Options{
Stream: true,
SourceFormat: sdktranslator.FormatGemini,
},
wantErr: true,
wantErrStatus: http.StatusUnauthorized,
},
{
name: "streaming with OAuth bearer token",
serverChunks: []string{
`data: {"candidates":[{"content":{"parts":[{"text":"OAuth response"}],"role":"model"},"finishReason":"STOP"}],"usageMetadata":{"promptTokenCount":5,"candidatesTokenCount":2,"totalTokenCount":7}}`,
},
serverStatus: http.StatusOK,
auth: &cliproxyauth.Auth{
ID: "oauth-stream-auth",
Provider: "gemini",
Metadata: map[string]any{
"access_token": "oauth-bearer-token",
},
},
request: cliproxyexecutor.Request{
Model: "gemini-2.0-flash",
Payload: []byte(`{"contents":[{"parts":[{"text":"OAuth test"}],"role":"user"}]}`),
Format: sdktranslator.FormatGemini,
},
opts: cliproxyexecutor.Options{
Stream: true,
SourceFormat: sdktranslator.FormatGemini,
OriginalRequest: []byte(`{"contents":[{"parts":[{"text":"OAuth test"}],"role":"user"}]}`),
},
wantErr: false,
wantChunkCount: 1,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Verify request method
if r.Method != http.MethodPost {
t.Errorf("Expected POST method, got %s", r.Method)
}
// Verify Content-Type header
contentType := r.Header.Get("Content-Type")
if contentType != "application/json" {
t.Errorf("Expected Content-Type application/json, got %s", contentType)
}
// Verify auth header
if tt.auth != nil {
if tt.auth.Attributes != nil && tt.auth.Attributes["api_key"] != "" {
gotAPIKey := r.Header.Get("x-goog-api-key")
if gotAPIKey != tt.auth.Attributes["api_key"] {
t.Errorf("Expected x-goog-api-key %s, got %s", tt.auth.Attributes["api_key"], gotAPIKey)
}
} else if tt.auth.Metadata != nil {
if accessToken, ok := tt.auth.Metadata["access_token"].(string); ok && accessToken != "" {
authHeader := r.Header.Get("Authorization")
expectedAuth := "Bearer " + accessToken
if authHeader != expectedAuth {
t.Errorf("Expected Authorization %s, got %s", expectedAuth, authHeader)
}
}
}
}
// Verify URL contains streamGenerateContent
if !strings.Contains(r.URL.Path, "streamGenerateContent") {
t.Errorf("Expected URL to contain streamGenerateContent, got %s", r.URL.Path)
}
// Handle error status codes
if tt.serverStatus != http.StatusOK {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(tt.serverStatus)
_, _ = w.Write([]byte(`{"error": {"code": ` + http.StatusText(tt.serverStatus) + `}}`))
return
}
// Set headers for SSE
w.Header().Set("Content-Type", "text/event-stream")
w.Header().Set("Cache-Control", "no-cache")
w.Header().Set("Connection", "keep-alive")
w.WriteHeader(http.StatusOK)
flusher, ok := w.(http.Flusher)
if !ok {
t.Error("Expected ResponseWriter to be a Flusher")
return
}
for _, chunk := range tt.serverChunks {
_, _ = w.Write([]byte(chunk + "\n"))
flusher.Flush()
}
}))
defer server.Close()
// Update auth to use test server URL
if tt.auth != nil {
if tt.auth.Attributes == nil {
tt.auth.Attributes = make(map[string]string)
}
tt.auth.Attributes["base_url"] = server.URL
}
cfg := &config.Config{}
executor := NewGeminiExecutor(cfg)
ctx := context.Background()
stream, err := executor.ExecuteStream(ctx, tt.auth, tt.request, tt.opts)
if tt.wantErr {
if err == nil {
t.Errorf("ExecuteStream() expected error, got nil")
return
}
if statusErr, ok := err.(cliproxyexecutor.StatusError); ok {
if statusErr.StatusCode() != tt.wantErrStatus {
t.Errorf("ExecuteStream() error status = %v, want %v", statusErr.StatusCode(), tt.wantErrStatus)
}
}
return
}
if err != nil {
t.Errorf("ExecuteStream() unexpected error: %v", err)
return
}
if stream == nil {
t.Error("ExecuteStream() returned nil stream")
return
}
// Consume stream chunks
chunkCount := 0
for chunk := range stream {
if chunk.Err != nil {
t.Errorf("Received error chunk: %v", chunk.Err)
continue
}
chunkCount++
}
// Note: The actual chunk count may differ from server chunks due to translation
// We just verify we received some chunks for successful requests
if chunkCount == 0 && tt.wantChunkCount > 0 {
t.Errorf("Expected to receive chunks, got 0")
}
})
}
}
func TestGeminiExecutor_Execute_RequestBody(t *testing.T) {
t.Parallel()
tests := []struct {
name string
request cliproxyexecutor.Request
opts cliproxyexecutor.Options
checkBody func(t *testing.T, body []byte)
}{
{
name: "request body contains model field",
request: cliproxyexecutor.Request{
Model: "gemini-2.0-flash",
Payload: []byte(`{"contents":[{"parts":[{"text":"Test"}],"role":"user"}]}`),
Format: sdktranslator.FormatGemini,
},
opts: cliproxyexecutor.Options{
SourceFormat: sdktranslator.FormatGemini,
OriginalRequest: []byte(`{"contents":[{"parts":[{"text":"Test"}],"role":"user"}]}`),
},
checkBody: func(t *testing.T, body []byte) {
var parsed map[string]any
if err := json.Unmarshal(body, &parsed); err != nil {
t.Errorf("Failed to parse request body: %v", err)
return
}
if model, ok := parsed["model"].(string); !ok || model == "" {
t.Error("Expected model field in request body")
}
},
},
{
name: "session_id is stripped from request",
request: cliproxyexecutor.Request{
Model: "gemini-2.0-flash",
Payload: []byte(`{"contents":[{"parts":[{"text":"Test"}],"role":"user"}],"session_id":"test-session"}`),
Format: sdktranslator.FormatGemini,
},
opts: cliproxyexecutor.Options{
SourceFormat: sdktranslator.FormatGemini,
OriginalRequest: []byte(`{"contents":[{"parts":[{"text":"Test"}],"role":"user"}],"session_id":"test-session"}`),
},
checkBody: func(t *testing.T, body []byte) {
var parsed map[string]any
if err := json.Unmarshal(body, &parsed); err != nil {
t.Errorf("Failed to parse request body: %v", err)
return
}
if _, ok := parsed["session_id"]; ok {
t.Error("session_id should be stripped from request body")
}
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var capturedBody []byte
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var err error
capturedBody, err = io.ReadAll(r.Body)
if err != nil {
t.Errorf("Failed to read request body: %v", err)
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte(`{"candidates":[{"content":{"parts":[{"text":"OK"}],"role":"model"},"finishReason":"STOP"}]}`))
}))
defer server.Close()
auth := &cliproxyauth.Auth{
ID: "test-auth",
Provider: "gemini",
Attributes: map[string]string{
"api_key": "test-key",
"base_url": server.URL,
},
}
cfg := &config.Config{}
executor := NewGeminiExecutor(cfg)
ctx := context.Background()
_, err := executor.Execute(ctx, auth, tt.request, tt.opts)
if err != nil {
t.Errorf("Execute() unexpected error: %v", err)
return
}
if tt.checkBody != nil {
tt.checkBody(t, capturedBody)
}
})
}
}
func TestGeminiExecutor_PrepareRequest(t *testing.T) {
t.Parallel()
executor := NewGeminiExecutor(nil)
req := httptest.NewRequest(http.MethodPost, "https://example.com", nil)
err := executor.PrepareRequest(req, nil)
if err != nil {
t.Errorf("PrepareRequest() unexpected error: %v", err)
}
}
func TestGeminiExecutor_Refresh(t *testing.T) {
t.Parallel()
tests := []struct {
name string
auth *cliproxyauth.Auth
}{
{
name: "refresh returns same auth",
auth: &cliproxyauth.Auth{
ID: "test-auth",
Provider: "gemini",
Attributes: map[string]string{
"api_key": "test-key",
},
},
},
{
name: "refresh with nil auth",
auth: nil,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
executor := NewGeminiExecutor(nil)
ctx := context.Background()
refreshed, err := executor.Refresh(ctx, tt.auth)
if err != nil {
t.Errorf("Refresh() unexpected error: %v", err)
return
}
if refreshed != tt.auth {
t.Errorf("Refresh() returned different auth, want same instance")
}
})
}
}
+90 -10
View File
@@ -67,6 +67,7 @@ func (e *IFlowExecutor) Execute(ctx context.Context, auth *cliproxyauth.Auth, re
return resp, errValidate
}
body = applyIFlowThinkingConfig(body)
body = preserveReasoningContentInMessages(body)
body = applyPayloadConfig(e.cfg, req.Model, body)
endpoint := strings.TrimSuffix(baseURL, "/") + iflowDefaultEndpoint
@@ -159,6 +160,7 @@ func (e *IFlowExecutor) ExecuteStream(ctx context.Context, auth *cliproxyauth.Au
return nil, errValidate
}
body = applyIFlowThinkingConfig(body)
body = preserveReasoningContentInMessages(body)
// Ensure tools array exists to avoid provider quirks similar to Qwen's behaviour.
toolsResult := gjson.GetBytes(body, "tools")
if toolsResult.Exists() && toolsResult.IsArray() && len(toolsResult.Array()) == 0 {
@@ -451,20 +453,98 @@ func ensureToolsArray(body []byte) []byte {
return updated
}
// applyIFlowThinkingConfig converts normalized reasoning_effort to iFlow chat_template_kwargs.enable_thinking.
// This should be called after NormalizeThinkingConfig has processed the payload.
// iFlow only supports boolean enable_thinking, so any non-"none" effort enables thinking.
func applyIFlowThinkingConfig(body []byte) []byte {
effort := gjson.GetBytes(body, "reasoning_effort")
if !effort.Exists() {
// preserveReasoningContentInMessages ensures reasoning_content from assistant messages in the
// conversation history is preserved when sending to iFlow models that support thinking.
// This is critical for multi-turn conversations where the model needs to see its previous
// reasoning to maintain coherent thought chains across tool calls and conversation turns.
//
// For GLM-4.7 and MiniMax-M2.1, the full assistant response (including reasoning) must be
// appended back into message history before the next call.
func preserveReasoningContentInMessages(body []byte) []byte {
model := strings.ToLower(gjson.GetBytes(body, "model").String())
// Only apply to models that support thinking with history preservation
needsPreservation := strings.HasPrefix(model, "glm-4.7") ||
strings.HasPrefix(model, "glm-4-7") ||
strings.HasPrefix(model, "minimax-m2.1") ||
strings.HasPrefix(model, "minimax-m2-1")
if !needsPreservation {
return body
}
val := strings.ToLower(strings.TrimSpace(effort.String()))
enableThinking := val != "none" && val != ""
messages := gjson.GetBytes(body, "messages")
if !messages.Exists() || !messages.IsArray() {
return body
}
body, _ = sjson.DeleteBytes(body, "reasoning_effort")
body, _ = sjson.SetBytes(body, "chat_template_kwargs.enable_thinking", enableThinking)
// Check if any assistant message already has reasoning_content preserved
hasReasoningContent := false
messages.ForEach(func(_, msg gjson.Result) bool {
role := msg.Get("role").String()
if role == "assistant" {
rc := msg.Get("reasoning_content")
if rc.Exists() && rc.String() != "" {
hasReasoningContent = true
return false // stop iteration
}
}
return true
})
// If reasoning content is already present, the messages are properly formatted
// No need to modify - the client has correctly preserved reasoning in history
if hasReasoningContent {
log.Debugf("iflow executor: reasoning_content found in message history for %s", model)
}
return body
}
// applyIFlowThinkingConfig converts normalized reasoning_effort to model-specific thinking configurations.
// This should be called after NormalizeThinkingConfig has processed the payload.
//
// Model-specific handling:
// - GLM-4.7: Uses extra_body={"thinking": {"type": "enabled"}, "clear_thinking": false}
// - MiniMax-M2.1: Uses reasoning_split=true for OpenAI-style reasoning separation
// - Other iFlow models: Uses chat_template_kwargs.enable_thinking (boolean)
func applyIFlowThinkingConfig(body []byte) []byte {
effort := gjson.GetBytes(body, "reasoning_effort")
model := strings.ToLower(gjson.GetBytes(body, "model").String())
// Check if thinking should be enabled
val := ""
if effort.Exists() {
val = strings.ToLower(strings.TrimSpace(effort.String()))
}
enableThinking := effort.Exists() && val != "none" && val != ""
// Remove reasoning_effort as we'll convert to model-specific format
if effort.Exists() {
body, _ = sjson.DeleteBytes(body, "reasoning_effort")
}
// GLM-4.7: Use extra_body with thinking config and clear_thinking: false
if strings.HasPrefix(model, "glm-4.7") || strings.HasPrefix(model, "glm-4-7") {
if enableThinking {
body, _ = sjson.SetBytes(body, "extra_body.thinking.type", "enabled")
body, _ = sjson.SetBytes(body, "extra_body.clear_thinking", false)
}
return body
}
// MiniMax-M2.1: Use reasoning_split=true for interleaved thinking
if strings.HasPrefix(model, "minimax-m2.1") || strings.HasPrefix(model, "minimax-m2-1") {
if enableThinking {
body, _ = sjson.SetBytes(body, "reasoning_split", true)
}
return body
}
// Other iFlow models (including GLM-4.6): Use chat_template_kwargs.enable_thinking
if effort.Exists() {
body, _ = sjson.SetBytes(body, "chat_template_kwargs.enable_thinking", enableThinking)
}
return body
}
@@ -495,3 +495,78 @@ func addIfNotEmpty(segments *[]string, value string) {
*segments = append(*segments, trimmed)
}
}
// EstimateRequestTokens estimates total tokens for a request payload.
// It auto-detects the format (OpenAI chat, Claude, etc.) and counts accordingly.
func EstimateRequestTokens(model string, payload []byte) (int64, error) {
enc, err := getTokenizer(model)
if err != nil {
// Fallback: estimate 4 chars per token
return int64(len(payload) / 4), nil
}
// Detect format and count accordingly
if gjson.GetBytes(payload, "messages").Exists() {
if gjson.GetBytes(payload, "system").Exists() {
// Claude format
return countClaudeChatTokens(enc, payload)
}
// OpenAI chat format
return countOpenAIChatTokens(enc, payload)
}
// OpenAI responses format or other - use general text counting
count, err := enc.Count(string(payload))
if err != nil {
return int64(len(payload) / 4), nil
}
return int64(count), nil
}
// CalculateCompressionBudget determines how many tokens should be compressed
// to bring context usage under the target percentage.
func CalculateCompressionBudget(currentTokens int64, contextWindow int, targetPercent float64) int64 {
if contextWindow <= 0 || targetPercent <= 0 || targetPercent >= 1 {
return 0
}
targetTokens := int64(float64(contextWindow) * targetPercent)
if currentTokens <= targetTokens {
return 0
}
return currentTokens - targetTokens
}
// ShouldTriggerCompression checks if compression should be triggered based on
// current token usage vs context window size.
func ShouldTriggerCompression(currentTokens int64, contextWindow int, thresholdPercent float64) bool {
if contextWindow <= 0 || thresholdPercent <= 0 {
return false
}
ratio := float64(currentTokens) / float64(contextWindow)
return ratio >= thresholdPercent
}
// GetDefaultContextWindow returns a conservative default context window for a model.
func GetDefaultContextWindow(model string) int {
lowerModel := strings.ToLower(model)
switch {
case strings.Contains(lowerModel, "claude-3.5"), strings.Contains(lowerModel, "claude-3-5"):
return 200000
case strings.Contains(lowerModel, "claude-3"):
return 200000
case strings.Contains(lowerModel, "claude"):
return 100000
case strings.Contains(lowerModel, "gpt-4-turbo"), strings.Contains(lowerModel, "gpt-4o"):
return 128000
case strings.Contains(lowerModel, "gpt-4"):
return 8192
case strings.Contains(lowerModel, "gpt-3.5"):
return 16384
case strings.Contains(lowerModel, "gemini"):
return 1000000
case strings.Contains(lowerModel, "o1"), strings.Contains(lowerModel, "o3"):
return 128000
default:
return 100000 // Conservative default
}
}
+7 -3
View File
@@ -19,7 +19,7 @@ type usageReporter struct {
provider string
model string
authID string
authIndex uint64
authIndex string
apiKey string
source string
requestedAt time.Time
@@ -482,12 +482,16 @@ func StripUsageMetadataFromJSON(rawJSON []byte) ([]byte, bool) {
cleaned := jsonBytes
var changed bool
if gjson.GetBytes(cleaned, "usageMetadata").Exists() {
if usageMetadata = gjson.GetBytes(cleaned, "usageMetadata"); usageMetadata.Exists() {
// Rename usageMetadata to cpaUsageMetadata in the message_start event of Claude
cleaned, _ = sjson.SetRawBytes(cleaned, "cpaUsageMetadata", []byte(usageMetadata.Raw))
cleaned, _ = sjson.DeleteBytes(cleaned, "usageMetadata")
changed = true
}
if gjson.GetBytes(cleaned, "response.usageMetadata").Exists() {
if usageMetadata = gjson.GetBytes(cleaned, "response.usageMetadata"); usageMetadata.Exists() {
// Rename usageMetadata to cpaUsageMetadata in the message_start event of Claude
cleaned, _ = sjson.SetRawBytes(cleaned, "response.cpaUsageMetadata", []byte(usageMetadata.Raw))
cleaned, _ = sjson.DeleteBytes(cleaned, "response.usageMetadata")
changed = true
}
@@ -99,6 +99,14 @@ func ConvertAntigravityResponseToClaude(_ context.Context, _ string, originalReq
// This follows the Claude Code API specification for streaming message initialization
messageStartTemplate := `{"type": "message_start", "message": {"id": "msg_1nZdL29xx5MUA1yADyHTEsnR8uuvGzszyY", "type": "message", "role": "assistant", "content": [], "model": "claude-3-5-sonnet-20241022", "stop_reason": null, "stop_sequence": null, "usage": {"input_tokens": 0, "output_tokens": 0}}}`
// Use cpaUsageMetadata within the message_start event for Claude.
if promptTokenCount := gjson.GetBytes(rawJSON, "response.cpaUsageMetadata.promptTokenCount"); promptTokenCount.Exists() {
messageStartTemplate, _ = sjson.Set(messageStartTemplate, "message.usage.input_tokens", promptTokenCount.Int())
}
if candidatesTokenCount := gjson.GetBytes(rawJSON, "response.cpaUsageMetadata.candidatesTokenCount"); candidatesTokenCount.Exists() {
messageStartTemplate, _ = sjson.Set(messageStartTemplate, "message.usage.output_tokens", candidatesTokenCount.Int())
}
// Override default values with actual response metadata if available from the Gemini CLI response
if modelVersionResult := gjson.GetBytes(rawJSON, "response.modelVersion"); modelVersionResult.Exists() {
messageStartTemplate, _ = sjson.Set(messageStartTemplate, "message.model", modelVersionResult.String())
@@ -247,10 +247,30 @@ func ConvertOpenAIRequestToAntigravity(modelName string, inputRawJSON []byte, _
} else if role == "assistant" {
node := []byte(`{"role":"model","parts":[]}`)
p := 0
if content.Type == gjson.String {
if content.Type == gjson.String && content.String() != "" {
node, _ = sjson.SetBytes(node, "parts.-1.text", content.String())
out, _ = sjson.SetRawBytes(out, "request.contents.-1", node)
p++
} else if content.IsArray() {
// Assistant multimodal content (e.g. text + image) -> single model content with parts
for _, item := range content.Array() {
switch item.Get("type").String() {
case "text":
p++
case "image_url":
// If the assistant returned an inline data URL, preserve it for history fidelity.
imageURL := item.Get("image_url.url").String()
if len(imageURL) > 5 { // expect data:...
pieces := strings.SplitN(imageURL[5:], ";", 2)
if len(pieces) == 2 && len(pieces[1]) > 7 {
mime := pieces[0]
data := pieces[1][7:]
node, _ = sjson.SetBytes(node, "parts."+itoa(p)+".inlineData.mime_type", mime)
node, _ = sjson.SetBytes(node, "parts."+itoa(p)+".inlineData.data", data)
p++
}
}
}
}
}
// Tool calls -> single model content with functionCall parts
@@ -305,6 +325,8 @@ func ConvertOpenAIRequestToAntigravity(modelName string, inputRawJSON []byte, _
if pp > 0 {
out, _ = sjson.SetRawBytes(out, "request.contents.-1", toolNode)
}
} else {
out, _ = sjson.SetRawBytes(out, "request.contents.-1", node)
}
}
}
@@ -181,12 +181,14 @@ func ConvertAntigravityResponseToOpenAI(_ context.Context, _ string, originalReq
mimeType = "image/png"
}
imageURL := fmt.Sprintf("data:%s;base64,%s", mimeType, data)
imagePayload := `{"image_url":{"url":""},"type":"image_url"}`
imagePayload, _ = sjson.Set(imagePayload, "image_url.url", imageURL)
imagesResult := gjson.Get(template, "choices.0.delta.images")
if !imagesResult.Exists() || !imagesResult.IsArray() {
template, _ = sjson.SetRaw(template, "choices.0.delta.images", `[]`)
}
imageIndex := len(gjson.Get(template, "choices.0.delta.images").Array())
imagePayload := `{"type":"image_url","image_url":{"url":""}}`
imagePayload, _ = sjson.Set(imagePayload, "index", imageIndex)
imagePayload, _ = sjson.Set(imagePayload, "image_url.url", imageURL)
template, _ = sjson.Set(template, "choices.0.delta.role", "assistant")
template, _ = sjson.SetRaw(template, "choices.0.delta.images.-1", imagePayload)
}
@@ -169,19 +169,37 @@ func ConvertOpenAIRequestToClaude(modelName string, inputRawJSON []byte, stream
case "image_url":
// Convert OpenAI image format to Claude Code format
imageURL := part.Get("image_url.url").String()
if imageURL == "" {
return true
}
var imagePart string
if strings.HasPrefix(imageURL, "data:") {
// Extract base64 data and media type from data URL
parts := strings.Split(imageURL, ",")
if len(parts) == 2 {
mediaTypePart := strings.Split(parts[0], ";")[0]
mediaType := strings.TrimPrefix(mediaTypePart, "data:")
data := parts[1]
imagePart := `{"type":"image","source":{"type":"base64","media_type":"","data":""}}`
// Format: data:<media_type>;base64,<data>
trimmed := strings.TrimPrefix(imageURL, "data:")
mediaAndData := strings.SplitN(trimmed, ";base64,", 2)
mediaType := "application/octet-stream"
data := ""
if len(mediaAndData) == 2 {
if mediaAndData[0] != "" {
mediaType = mediaAndData[0]
}
data = mediaAndData[1]
}
if data != "" {
imagePart = `{"type":"image","source":{"type":"base64","media_type":"","data":""}}`
imagePart, _ = sjson.Set(imagePart, "source.media_type", mediaType)
imagePart, _ = sjson.Set(imagePart, "source.data", data)
msg, _ = sjson.SetRaw(msg, "content.-1", imagePart)
}
} else {
// Handle URL-based images (https://)
imagePart = `{"type":"image","source":{"type":"url","url":""}}`
imagePart, _ = sjson.Set(imagePart, "source.url", imageURL)
}
if imagePart != "" {
msg, _ = sjson.SetRaw(msg, "content.-1", imagePart)
}
}
return true
@@ -209,9 +209,12 @@ func ConvertClaudeResponseToOpenAI(_ context.Context, modelName string, original
if usage := root.Get("usage"); usage.Exists() {
inputTokens := usage.Get("input_tokens").Int()
outputTokens := usage.Get("output_tokens").Int()
template, _ = sjson.Set(template, "usage.prompt_tokens", inputTokens)
cacheReadInputTokens := usage.Get("cache_read_input_tokens").Int()
cacheCreationInputTokens := usage.Get("cache_creation_input_tokens").Int()
template, _ = sjson.Set(template, "usage.prompt_tokens", inputTokens+cacheCreationInputTokens)
template, _ = sjson.Set(template, "usage.completion_tokens", outputTokens)
template, _ = sjson.Set(template, "usage.total_tokens", inputTokens+outputTokens)
template, _ = sjson.Set(template, "usage.prompt_tokens_details.cached_tokens", cacheReadInputTokens)
}
return []string{template}
@@ -285,8 +288,6 @@ func ConvertClaudeResponseToOpenAINonStream(_ context.Context, _ string, origina
var messageID string
var model string
var createdAt int64
var inputTokens, outputTokens int64
var reasoningTokens int64
var stopReason string
var contentParts []string
var reasoningParts []string
@@ -303,9 +304,6 @@ func ConvertClaudeResponseToOpenAINonStream(_ context.Context, _ string, origina
messageID = message.Get("id").String()
model = message.Get("model").String()
createdAt = time.Now().Unix()
if usage := message.Get("usage"); usage.Exists() {
inputTokens = usage.Get("input_tokens").Int()
}
}
case "content_block_start":
@@ -368,11 +366,14 @@ func ConvertClaudeResponseToOpenAINonStream(_ context.Context, _ string, origina
}
}
if usage := root.Get("usage"); usage.Exists() {
outputTokens = usage.Get("output_tokens").Int()
// Estimate reasoning tokens from accumulated thinking content
if len(reasoningParts) > 0 {
reasoningTokens = int64(len(strings.Join(reasoningParts, "")) / 4) // Rough estimation
}
inputTokens := usage.Get("input_tokens").Int()
outputTokens := usage.Get("output_tokens").Int()
cacheReadInputTokens := usage.Get("cache_read_input_tokens").Int()
cacheCreationInputTokens := usage.Get("cache_creation_input_tokens").Int()
out, _ = sjson.Set(out, "usage.prompt_tokens", inputTokens+cacheCreationInputTokens)
out, _ = sjson.Set(out, "usage.completion_tokens", outputTokens)
out, _ = sjson.Set(out, "usage.total_tokens", inputTokens+outputTokens)
out, _ = sjson.Set(out, "usage.prompt_tokens_details.cached_tokens", cacheReadInputTokens)
}
}
}
@@ -431,16 +432,5 @@ func ConvertClaudeResponseToOpenAINonStream(_ context.Context, _ string, origina
out, _ = sjson.Set(out, "choices.0.finish_reason", mapAnthropicStopReasonToOpenAI(stopReason))
}
// Set usage information including prompt tokens, completion tokens, and total tokens
totalTokens := inputTokens + outputTokens
out, _ = sjson.Set(out, "usage.prompt_tokens", inputTokens)
out, _ = sjson.Set(out, "usage.completion_tokens", outputTokens)
out, _ = sjson.Set(out, "usage.total_tokens", totalTokens)
// Add reasoning tokens to usage details if any reasoning content was processed
if reasoningTokens > 0 {
out, _ = sjson.Set(out, "usage.completion_tokens_details.reasoning_tokens", reasoningTokens)
}
return out
}
@@ -114,13 +114,16 @@ func ConvertOpenAIResponsesRequestToClaude(modelName string, inputRawJSON []byte
var builder strings.Builder
if parts := item.Get("content"); parts.Exists() && parts.IsArray() {
parts.ForEach(func(_, part gjson.Result) bool {
text := part.Get("text").String()
textResult := part.Get("text")
text := textResult.String()
if builder.Len() > 0 && text != "" {
builder.WriteByte('\n')
}
builder.WriteString(text)
return true
})
} else if parts.Type == gjson.String {
builder.WriteString(parts.String())
}
instructionsText = builder.String()
if instructionsText != "" {
@@ -207,6 +210,8 @@ func ConvertOpenAIResponsesRequestToClaude(modelName string, inputRawJSON []byte
}
return true
})
} else if parts.Type == gjson.String {
textAggregate.WriteString(parts.String())
}
// Fallback to given role if content types not decisive
@@ -218,8 +218,29 @@ func ConvertOpenAIRequestToGeminiCLI(modelName string, inputRawJSON []byte, _ bo
if content.Type == gjson.String {
// Assistant text -> single model content
node, _ = sjson.SetBytes(node, "parts.-1.text", content.String())
out, _ = sjson.SetRawBytes(out, "request.contents.-1", node)
p++
} else if content.IsArray() {
// Assistant multimodal content (e.g. text + image) -> single model content with parts
for _, item := range content.Array() {
switch item.Get("type").String() {
case "text":
node, _ = sjson.SetBytes(node, "parts."+itoa(p)+".text", item.Get("text").String())
p++
case "image_url":
// If the assistant returned an inline data URL, preserve it for history fidelity.
imageURL := item.Get("image_url.url").String()
if len(imageURL) > 5 { // expect data:...
pieces := strings.SplitN(imageURL[5:], ";", 2)
if len(pieces) == 2 && len(pieces[1]) > 7 {
mime := pieces[0]
data := pieces[1][7:]
node, _ = sjson.SetBytes(node, "parts."+itoa(p)+".inlineData.mime_type", mime)
node, _ = sjson.SetBytes(node, "parts."+itoa(p)+".inlineData.data", data)
p++
}
}
}
}
}
// Tool calls -> single model content with functionCall parts
@@ -260,6 +281,8 @@ func ConvertOpenAIRequestToGeminiCLI(modelName string, inputRawJSON []byte, _ bo
if pp > 0 {
out, _ = sjson.SetRawBytes(out, "request.contents.-1", toolNode)
}
} else {
out, _ = sjson.SetRawBytes(out, "request.contents.-1", node)
}
}
}
@@ -185,12 +185,14 @@ func ConvertCliResponseToOpenAI(_ context.Context, _ string, originalRequestRawJ
mimeType = "image/png"
}
imageURL := fmt.Sprintf("data:%s;base64,%s", mimeType, data)
imagePayload := `{"image_url":{"url":""},"type":"image_url"}`
imagePayload, _ = sjson.Set(imagePayload, "image_url.url", imageURL)
imagesResult := gjson.Get(template, "choices.0.delta.images")
if !imagesResult.Exists() || !imagesResult.IsArray() {
template, _ = sjson.SetRaw(template, "choices.0.delta.images", `[]`)
}
imageIndex := len(gjson.Get(template, "choices.0.delta.images").Array())
imagePayload := `{"type":"image_url","image_url":{"url":""}}`
imagePayload, _ = sjson.Set(imagePayload, "index", imageIndex)
imagePayload, _ = sjson.Set(imagePayload, "image_url.url", imageURL)
template, _ = sjson.Set(template, "choices.0.delta.role", "assistant")
template, _ = sjson.SetRaw(template, "choices.0.delta.images.-1", imagePayload)
}
@@ -233,18 +233,15 @@ func ConvertOpenAIRequestToGemini(modelName string, inputRawJSON []byte, _ bool)
} else if role == "assistant" {
node := []byte(`{"role":"model","parts":[]}`)
p := 0
if content.Type == gjson.String {
// Assistant text -> single model content
node, _ = sjson.SetBytes(node, "parts.-1.text", content.String())
out, _ = sjson.SetRawBytes(out, "contents.-1", node)
p++
} else if content.IsArray() {
// Assistant multimodal content (e.g. text + image) -> single model content with parts
for _, item := range content.Array() {
switch item.Get("type").String() {
case "text":
node, _ = sjson.SetBytes(node, "parts."+itoa(p)+".text", item.Get("text").String())
p++
case "image_url":
// If the assistant returned an inline data URL, preserve it for history fidelity.
@@ -261,7 +258,6 @@ func ConvertOpenAIRequestToGemini(modelName string, inputRawJSON []byte, _ bool)
}
}
}
out, _ = sjson.SetRawBytes(out, "contents.-1", node)
}
// Tool calls -> single model content with functionCall parts
@@ -302,6 +298,8 @@ func ConvertOpenAIRequestToGemini(modelName string, inputRawJSON []byte, _ bool)
if pp > 0 {
out, _ = sjson.SetRawBytes(out, "contents.-1", toolNode)
}
} else {
out, _ = sjson.SetRawBytes(out, "contents.-1", node)
}
}
}
@@ -182,12 +182,14 @@ func ConvertGeminiResponseToOpenAI(_ context.Context, _ string, originalRequestR
mimeType = "image/png"
}
imageURL := fmt.Sprintf("data:%s;base64,%s", mimeType, data)
imagePayload := `{"image_url":{"url":""},"type":"image_url"}`
imagePayload, _ = sjson.Set(imagePayload, "image_url.url", imageURL)
imagesResult := gjson.Get(template, "choices.0.delta.images")
if !imagesResult.Exists() || !imagesResult.IsArray() {
template, _ = sjson.SetRaw(template, "choices.0.delta.images", `[]`)
}
imageIndex := len(gjson.Get(template, "choices.0.delta.images").Array())
imagePayload := `{"type":"image_url","image_url":{"url":""}}`
imagePayload, _ = sjson.Set(imagePayload, "index", imageIndex)
imagePayload, _ = sjson.Set(imagePayload, "image_url.url", imageURL)
template, _ = sjson.Set(template, "choices.0.delta.role", "assistant")
template, _ = sjson.SetRaw(template, "choices.0.delta.images.-1", imagePayload)
}
@@ -316,12 +318,14 @@ func ConvertGeminiResponseToOpenAINonStream(_ context.Context, _ string, origina
mimeType = "image/png"
}
imageURL := fmt.Sprintf("data:%s;base64,%s", mimeType, data)
imagePayload := `{"image_url":{"url":""},"type":"image_url"}`
imagePayload, _ = sjson.Set(imagePayload, "image_url.url", imageURL)
imagesResult := gjson.Get(template, "choices.0.message.images")
if !imagesResult.Exists() || !imagesResult.IsArray() {
template, _ = sjson.SetRaw(template, "choices.0.message.images", `[]`)
}
imageIndex := len(gjson.Get(template, "choices.0.message.images").Array())
imagePayload := `{"type":"image_url","image_url":{"url":""}}`
imagePayload, _ = sjson.Set(imagePayload, "index", imageIndex)
imagePayload, _ = sjson.Set(imagePayload, "image_url.url", imageURL)
template, _ = sjson.Set(template, "choices.0.message.role", "assistant")
template, _ = sjson.SetRaw(template, "choices.0.message.images.-1", imagePayload)
}
+6 -185
View File
@@ -1,192 +1,13 @@
package trayicon
import (
"encoding/binary"
"image"
"image/color"
_ "embed"
)
// ProxyPilotICO returns a small multi-size .ico payload suitable for Windows tray icons.
// It uses classic BMP/AND-mask frames for maximum compatibility with Windows loaders.
//go:embed icon.ico
var proxyPilotICO []byte
// ProxyPilotICO returns the embedded ProxyPilot icon for Windows tray.
func ProxyPilotICO() []byte {
sizes := []int{16, 32, 48}
type iconImg struct {
size int
bmp []byte
}
images := make([]iconImg, 0, len(sizes))
for _, s := range sizes {
img := renderProxyPilotIcon(s)
bmp := encodeICOFrameBMP32(img)
if len(bmp) == 0 {
continue
}
images = append(images, iconImg{size: s, bmp: bmp})
}
if len(images) == 0 {
return nil
}
// ICONDIR (6 bytes) + N*ICONDIRENTRY (16 bytes) + image blobs
headerSize := 6 + 16*len(images)
total := headerSize
for _, im := range images {
total += len(im.bmp)
}
out := make([]byte, 0, total)
// ICONDIR
var hdr [6]byte
binary.LittleEndian.PutUint16(hdr[0:2], 0) // reserved
binary.LittleEndian.PutUint16(hdr[2:4], 1) // type=icon
binary.LittleEndian.PutUint16(hdr[4:6], uint16(len(images)))
out = append(out, hdr[:]...)
// Entries
offset := uint32(headerSize)
for _, im := range images {
var e [16]byte
w := byte(im.size)
h := byte(im.size)
if im.size >= 256 {
w, h = 0, 0
}
e[0] = w
e[1] = h
e[2] = 0 // colors
e[3] = 0 // reserved
binary.LittleEndian.PutUint16(e[4:6], 1) // planes
binary.LittleEndian.PutUint16(e[6:8], 32) // bitcount
binary.LittleEndian.PutUint32(e[8:12], uint32(len(im.bmp)))
binary.LittleEndian.PutUint32(e[12:16], offset)
offset += uint32(len(im.bmp))
out = append(out, e[:]...)
}
// Image data
for _, im := range images {
out = append(out, im.bmp...)
}
return out
return proxyPilotICO
}
func renderProxyPilotIcon(size int) image.Image {
img := image.NewRGBA(image.Rect(0, 0, size, size))
bg := color.RGBA{R: 12, G: 18, B: 28, A: 255}
accent := color.RGBA{R: 66, G: 165, B: 245, A: 255}
fg := color.RGBA{R: 235, G: 245, B: 255, A: 255}
// Background
for y := 0; y < size; y++ {
for x := 0; x < size; x++ {
img.SetRGBA(x, y, bg)
}
}
// Accent border (1px)
for x := 0; x < size; x++ {
img.SetRGBA(x, 0, accent)
img.SetRGBA(x, size-1, accent)
}
for y := 0; y < size; y++ {
img.SetRGBA(0, y, accent)
img.SetRGBA(size-1, y, accent)
}
// Draw a simple "PP" monogram using a tiny bitmap font scaled to size.
scale := 1
if size >= 32 {
scale = 2
}
if size >= 64 {
scale = 3
}
drawGlyph(img, size, fg, scale, 2*scale, 2*scale, glyphP)
drawGlyph(img, size, fg, scale, (2*scale)+(6*scale), 2*scale, glyphP)
// Small wing accent.
drawRect(img, size, accent, 2*scale, (2*scale)+(11*scale), 12*scale, 2*scale)
return img
}
var glyphP = []string{
"11110",
"10001",
"11110",
"10000",
"10000",
}
func drawGlyph(img *image.RGBA, size int, c color.RGBA, scale int, ox int, oy int, rows []string) {
for y, row := range rows {
for x, ch := range row {
if ch != '1' {
continue
}
drawRect(img, size, c, ox+(x*scale), oy+(y*scale), scale, scale)
}
}
}
func drawRect(img *image.RGBA, size int, c color.RGBA, x int, y int, w int, h int) {
for yy := y; yy < y+h; yy++ {
if yy < 0 || yy >= size {
continue
}
for xx := x; xx < x+w; xx++ {
if xx < 0 || xx >= size {
continue
}
img.SetRGBA(xx, yy, c)
}
}
}
func encodeICOFrameBMP32(img image.Image) []byte {
b := img.Bounds()
w := b.Dx()
h := b.Dy()
if w <= 0 || h <= 0 || w > 255 || h > 255 {
return nil
}
// AND mask: 1bpp, rows padded to 32-bit boundaries.
maskStride := ((w + 31) / 32) * 4
maskSize := maskStride * h
mask := make([]byte, maskSize) // all zeros (opaque)
// XOR bitmap: 32bpp BGRA, bottom-up rows.
pixels := make([]byte, w*h*4)
i := 0
for y := h - 1; y >= 0; y-- {
for x := 0; x < w; x++ {
r, g, bb, a := img.At(b.Min.X+x, b.Min.Y+y).RGBA()
pixels[i+0] = byte(bb >> 8)
pixels[i+1] = byte(g >> 8)
pixels[i+2] = byte(r >> 8)
pixels[i+3] = byte(a >> 8)
i += 4
}
}
// BITMAPINFOHEADER (40 bytes).
var bih [40]byte
binary.LittleEndian.PutUint32(bih[0:4], 40) // biSize
binary.LittleEndian.PutUint32(bih[4:8], uint32(w)) // biWidth
binary.LittleEndian.PutUint32(bih[8:12], uint32(h*2)) // biHeight (XOR + AND)
binary.LittleEndian.PutUint16(bih[12:14], 1) // biPlanes
binary.LittleEndian.PutUint16(bih[14:16], 32) // biBitCount
binary.LittleEndian.PutUint32(bih[16:20], 0) // biCompression (BI_RGB)
binary.LittleEndian.PutUint32(bih[20:24], uint32(len(pixels)+len(mask))) // biSizeImage
out := make([]byte, 0, len(bih)+len(pixels)+len(mask))
out = append(out, bih[:]...)
out = append(out, pixels...)
out = append(out, mask...)
return out
}
Binary file not shown.

After

Width:  |  Height:  |  Size: 87 KiB

Some files were not shown because too many files have changed in this diff Show More