Compare commits

...
Author SHA1 Message Date
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
125 changed files with 16304 additions and 5778 deletions
+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"
+76
View File
@@ -0,0 +1,76 @@
name: Build Release
on:
push:
tags:
- 'v*'
workflow_dispatch:
inputs:
version:
description: 'Version (e.g., 0.1.0)'
required: true
default: '0.1.0'
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
+13 -8
View File
@@ -16,6 +16,9 @@ pgstore/*
gitstore/*
objectstore/*
static/*
!static/icon.png
!static/icon.ico
test/
refs/*
dist/*
.proxypilot/*
@@ -36,6 +39,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
@@ -55,11 +60,11 @@ _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
+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
}
+304 -264
View File
@@ -7,6 +7,7 @@ import (
"encoding/json"
"flag"
"fmt"
"io/fs"
"net"
"net/http"
"net/http/httputil"
@@ -14,21 +15,46 @@ 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
@@ -42,8 +68,33 @@ func main() {
}
func run(repoRoot, configPath, exePath 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 +103,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 +181,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 +217,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 +232,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 +293,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 +510,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 +551,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")
}
Binary file not shown.
+40
View File
@@ -0,0 +1,40 @@
{
"FixedFileInfo": {
"FileVersion": {
"Major": 0,
"Minor": 1,
"Patch": 1,
"Build": 0
},
"ProductVersion": {
"Major": 0,
"Minor": 1,
"Patch": 1,
"Build": 0
},
"FileFlagsMask": "3f",
"FileFlags ": "00",
"FileOS": "040004",
"FileType": "01",
"FileSubType": "00"
},
"StringFileInfo": {
"Comments": "Local AI Proxy Controller",
"CompanyName": "ProxyPilot",
"FileDescription": "ProxyPilot Desktop App",
"FileVersion": "0.1.1",
"InternalName": "ProxyPilot",
"LegalCopyright": "MIT License",
"OriginalFilename": "ProxyPilot.exe",
"ProductName": "ProxyPilot",
"ProductVersion": "0.1.1"
},
"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
+6
View File
@@ -4,7 +4,13 @@
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="pp-mgmt-key" content="test123" />
<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

+301
View File
@@ -130,6 +130,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 +194,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 +264,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 +641,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 +782,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 +890,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 +923,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": 1,
"Build": 0
},
"ProductVersion": {
"Major": 0,
"Minor": 1,
"Patch": 1,
"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.1",
"InternalName": "proxypilot-engine",
"LegalCopyright": "MIT License",
"OriginalFilename": "proxypilot-engine.exe",
"ProductName": "ProxyPilot",
"ProductVersion": "0.1.1"
},
"VarFileInfo": {
"Translation": {
"LangID": "0409",
"CharsetID": "04B0"
}
},
"IconPath": "../../static/icon.ico",
"ManifestPath": ""
}
+45 -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:
+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")
}
}
+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)
}
+5 -1
View File
@@ -13,8 +13,12 @@ func (h *Handler) GetUsageStatistics(c *gin.Context) {
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,
})
}
+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)
}
}
+1077 -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",
-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",
+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
+48
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.
@@ -694,6 +720,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.
+71
View File
@@ -25,6 +25,9 @@ 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"`
}
// StreamingConfig holds server streaming behavior configuration.
@@ -37,6 +40,34 @@ type StreamingConfig struct {
// to allow auth rotation / transient recovery.
// nil means default (2). 0 disables bootstrap retries.
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 +131,43 @@ 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
}
+13 -10
View File
@@ -13,6 +13,8 @@ import (
"strings"
"syscall"
"time"
"github.com/router-for-me/CLIProxyAPI/v6/internal/buildinfo"
)
func StatusFor(configPath string) (Status, error) {
@@ -47,16 +49,17 @@ func StatusFor(configPath string) (Status, error) {
}
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
+1
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
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)
}
+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") {
@@ -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
}
}
+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

+338
View File
@@ -0,0 +1,338 @@
package updates
import (
"archive/tar"
"archive/zip"
"compress/gzip"
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
)
// InstallResult contains the result of an installation attempt.
type InstallResult struct {
Success bool `json:"success"`
Message string `json:"message"`
RestartCmd string `json:"restart_cmd,omitempty"`
NeedsRestart bool `json:"needs_restart"`
}
// PrepareInstall extracts and prepares the update for installation.
// Returns the path to the extracted executable.
func PrepareInstall(downloadResult *DownloadResult) (string, error) {
if downloadResult == nil || downloadResult.FilePath == "" {
return "", fmt.Errorf("no download result provided")
}
filePath := downloadResult.FilePath
ext := strings.ToLower(filepath.Ext(filePath))
// Handle different archive formats
switch ext {
case ".zip":
return extractZip(filePath)
case ".gz":
if strings.HasSuffix(strings.ToLower(filePath), ".tar.gz") {
return extractTarGz(filePath)
}
return "", fmt.Errorf("unsupported archive format: %s", ext)
case ".exe", ".msi":
// Already an executable or installer
return filePath, nil
default:
// Assume it's a raw binary (Linux/macOS)
// Make it executable
if err := os.Chmod(filePath, 0755); err != nil {
return "", fmt.Errorf("failed to make binary executable: %w", err)
}
return filePath, nil
}
}
// InstallUpdate installs the update by replacing the current binary.
// On Windows, this schedules a replacement for the next restart.
// On Unix, this replaces the binary directly.
func InstallUpdate(executablePath string) (*InstallResult, error) {
currentExe, err := os.Executable()
if err != nil {
return nil, fmt.Errorf("failed to get current executable path: %w", err)
}
// Resolve any symlinks
currentExe, err = filepath.EvalSymlinks(currentExe)
if err != nil {
return nil, fmt.Errorf("failed to resolve executable path: %w", err)
}
switch runtime.GOOS {
case "windows":
return installWindows(executablePath, currentExe)
default:
return installUnix(executablePath, currentExe)
}
}
func installWindows(newExe, currentExe string) (*InstallResult, error) {
// On Windows, we can't replace a running executable directly.
// We use a batch script that waits for the process to exit, then replaces the file.
backupPath := currentExe + ".old"
tempScript := filepath.Join(os.TempDir(), "proxypilot-update.bat")
// Create update script
script := fmt.Sprintf(`@echo off
echo Waiting for ProxyPilot to exit...
timeout /t 2 /nobreak >nul
:retry
tasklist /FI "IMAGENAME eq %s" 2>NUL | find /I /N "%s">NUL
if "%%ERRORLEVEL%%"=="0" (
timeout /t 1 /nobreak >nul
goto retry
)
echo Backing up old version...
if exist "%s" del /f "%s"
move /y "%s" "%s"
echo Installing new version...
copy /y "%s" "%s"
echo Starting new version...
start "" "%s"
del "%%~f0"
`, filepath.Base(currentExe), filepath.Base(currentExe),
backupPath, backupPath,
currentExe, backupPath,
newExe, currentExe,
currentExe)
if err := os.WriteFile(tempScript, []byte(script), 0755); err != nil {
return nil, fmt.Errorf("failed to create update script: %w", err)
}
// Start the update script
cmd := exec.Command("cmd", "/c", "start", "/min", tempScript)
if err := cmd.Start(); err != nil {
return nil, fmt.Errorf("failed to start update script: %w", err)
}
return &InstallResult{
Success: true,
Message: "Update scheduled. The application will restart shortly.",
RestartCmd: tempScript,
NeedsRestart: true,
}, nil
}
func installUnix(newExe, currentExe string) (*InstallResult, error) {
// On Unix, we can replace the binary while it's running.
// The new binary will be used on next execution.
backupPath := currentExe + ".old"
// Remove old backup if exists
os.Remove(backupPath)
// Rename current to backup
if err := os.Rename(currentExe, backupPath); err != nil {
return nil, fmt.Errorf("failed to backup current executable: %w", err)
}
// Copy new executable to current location
if err := copyFile(newExe, currentExe); err != nil {
// Try to restore backup
os.Rename(backupPath, currentExe)
return nil, fmt.Errorf("failed to install new executable: %w", err)
}
// Make executable
if err := os.Chmod(currentExe, 0755); err != nil {
return nil, fmt.Errorf("failed to set permissions: %w", err)
}
return &InstallResult{
Success: true,
Message: "Update installed successfully. Please restart the application.",
NeedsRestart: true,
}, nil
}
func copyFile(src, dst string) error {
in, err := os.Open(src)
if err != nil {
return err
}
defer in.Close()
out, err := os.Create(dst)
if err != nil {
return err
}
defer out.Close()
_, err = io.Copy(out, in)
return err
}
func extractZip(zipPath string) (string, error) {
r, err := zip.OpenReader(zipPath)
if err != nil {
return "", fmt.Errorf("failed to open zip: %w", err)
}
defer r.Close()
extractDir := filepath.Dir(zipPath)
var executablePath string
for _, f := range r.File {
fpath := filepath.Join(extractDir, f.Name)
// Check for ZipSlip vulnerability
if !strings.HasPrefix(fpath, filepath.Clean(extractDir)+string(os.PathSeparator)) {
return "", fmt.Errorf("invalid file path in zip: %s", f.Name)
}
if f.FileInfo().IsDir() {
os.MkdirAll(fpath, os.ModePerm)
continue
}
if err := os.MkdirAll(filepath.Dir(fpath), os.ModePerm); err != nil {
return "", err
}
outFile, err := os.OpenFile(fpath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode())
if err != nil {
return "", err
}
rc, err := f.Open()
if err != nil {
outFile.Close()
return "", err
}
_, err = io.Copy(outFile, rc)
outFile.Close()
rc.Close()
if err != nil {
return "", err
}
// Look for the main executable
if isExecutable(f.Name) {
executablePath = fpath
}
}
if executablePath == "" {
return "", fmt.Errorf("no executable found in archive")
}
return executablePath, nil
}
func extractTarGz(tarGzPath string) (string, error) {
f, err := os.Open(tarGzPath)
if err != nil {
return "", fmt.Errorf("failed to open tar.gz: %w", err)
}
defer f.Close()
gzr, err := gzip.NewReader(f)
if err != nil {
return "", fmt.Errorf("failed to create gzip reader: %w", err)
}
defer gzr.Close()
tr := tar.NewReader(gzr)
extractDir := filepath.Dir(tarGzPath)
var executablePath string
for {
header, err := tr.Next()
if err == io.EOF {
break
}
if err != nil {
return "", fmt.Errorf("failed to read tar: %w", err)
}
fpath := filepath.Join(extractDir, header.Name)
// Check for path traversal
if !strings.HasPrefix(fpath, filepath.Clean(extractDir)+string(os.PathSeparator)) {
return "", fmt.Errorf("invalid file path in tar: %s", header.Name)
}
switch header.Typeflag {
case tar.TypeDir:
if err := os.MkdirAll(fpath, 0755); err != nil {
return "", err
}
case tar.TypeReg:
if err := os.MkdirAll(filepath.Dir(fpath), 0755); err != nil {
return "", err
}
outFile, err := os.Create(fpath)
if err != nil {
return "", err
}
if _, err := io.Copy(outFile, tr); err != nil {
outFile.Close()
return "", err
}
outFile.Close()
if err := os.Chmod(fpath, os.FileMode(header.Mode)); err != nil {
return "", err
}
if isExecutable(header.Name) {
executablePath = fpath
}
}
}
if executablePath == "" {
return "", fmt.Errorf("no executable found in archive")
}
return executablePath, nil
}
func isExecutable(name string) bool {
base := strings.ToLower(filepath.Base(name))
// Look for common executable patterns
if strings.HasSuffix(base, ".exe") {
return true
}
if strings.Contains(base, "proxypilot") && !strings.Contains(base, ".") {
return true
}
if base == "proxypilot" || base == "cliproxyapi" {
return true
}
return false
}
// CleanupOldVersions removes backup files from previous updates.
func CleanupOldVersions() error {
currentExe, err := os.Executable()
if err != nil {
return err
}
currentExe, _ = filepath.EvalSymlinks(currentExe)
backupPath := currentExe + ".old"
if _, err := os.Stat(backupPath); err == nil {
return os.Remove(backupPath)
}
return nil
}
+259
View File
@@ -0,0 +1,259 @@
package updates
import (
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"runtime"
"strings"
"time"
"github.com/router-for-me/CLIProxyAPI/v6/internal/buildinfo"
)
type UpdateInfo struct {
Available bool `json:"available"`
Version string `json:"version"`
DownloadURL string `json:"download_url"`
ReleaseNotes string `json:"release_notes,omitempty"`
Assets []Asset `json:"assets,omitempty"`
}
type Asset struct {
Name string `json:"name"`
DownloadURL string `json:"browser_download_url"`
Size int64 `json:"size"`
}
type DownloadProgress struct {
TotalBytes int64 `json:"total_bytes"`
DownloadedBytes int64 `json:"downloaded_bytes"`
Percent float64 `json:"percent"`
}
type DownloadResult struct {
FilePath string `json:"file_path"`
Version string `json:"version"`
SignaturePath string `json:"signature_path,omitempty"`
}
// GitHubRepo is the GitHub repository for update checks.
const GitHubRepo = "Finesssee/ProxyPilot"
func CheckForUpdates() (*UpdateInfo, error) {
client := &http.Client{Timeout: 10 * time.Second}
// Use a User-Agent as required by GitHub API
url := fmt.Sprintf("https://api.github.com/repos/%s/releases/latest", GitHubRepo)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
req.Header.Set("User-Agent", "ProxyPilot-Updater")
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
// Handle 404 - no releases found
if resp.StatusCode == http.StatusNotFound {
return &UpdateInfo{
Available: false,
Version: "",
DownloadURL: "",
ReleaseNotes: "No releases published yet",
Assets: nil,
}, nil
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("failed to check for updates: %s", resp.Status)
}
var release struct {
TagName string `json:"tag_name"`
HTMLURL string `json:"html_url"`
Body string `json:"body"`
Assets []struct {
Name string `json:"name"`
BrowserDownloadURL string `json:"browser_download_url"`
Size int64 `json:"size"`
} `json:"assets"`
}
if err := json.NewDecoder(resp.Body).Decode(&release); err != nil {
return nil, err
}
currentVersion := strings.TrimPrefix(buildinfo.Version, "v")
latestVersion := strings.TrimPrefix(release.TagName, "v")
available := false
if currentVersion != "dev" && latestVersion != "" && latestVersion != currentVersion {
// Simple comparison: if versions are different and not dev, an update is available.
available = true
}
assets := make([]Asset, 0, len(release.Assets))
for _, a := range release.Assets {
assets = append(assets, Asset{
Name: a.Name,
DownloadURL: a.BrowserDownloadURL,
Size: a.Size,
})
}
return &UpdateInfo{
Available: available,
Version: latestVersion,
DownloadURL: release.HTMLURL,
ReleaseNotes: release.Body,
Assets: assets,
}, nil
}
// GetAssetForPlatform returns the appropriate asset for the current OS/arch.
func GetAssetForPlatform(assets []Asset) (*Asset, *Asset) {
goos := runtime.GOOS
goarch := runtime.GOARCH
// Map Go arch to common naming conventions
archName := goarch
if goarch == "amd64" {
archName = "x86_64"
} else if goarch == "386" {
archName = "i386"
}
var binaryAsset, sigAsset *Asset
for i := range assets {
name := strings.ToLower(assets[i].Name)
// Check for matching platform binary
if strings.Contains(name, goos) && (strings.Contains(name, archName) || strings.Contains(name, goarch)) {
if strings.HasSuffix(name, ".sig") || strings.HasSuffix(name, ".asc") {
sigAsset = &assets[i]
} else if strings.HasSuffix(name, ".exe") || strings.HasSuffix(name, ".zip") ||
strings.HasSuffix(name, ".tar.gz") || !strings.Contains(name, ".") ||
strings.HasSuffix(name, ".dmg") || strings.HasSuffix(name, ".msi") {
binaryAsset = &assets[i]
}
}
}
return binaryAsset, sigAsset
}
// DownloadUpdate downloads the update to a temporary directory.
func DownloadUpdate(version string, progressCb func(DownloadProgress)) (*DownloadResult, error) {
// Get update info to find assets
info, err := CheckForUpdates()
if err != nil {
return nil, fmt.Errorf("failed to check for updates: %w", err)
}
if !info.Available || info.Version != version {
return nil, fmt.Errorf("version %s is not available", version)
}
binaryAsset, sigAsset := GetAssetForPlatform(info.Assets)
if binaryAsset == nil {
return nil, fmt.Errorf("no suitable binary found for %s/%s", runtime.GOOS, runtime.GOARCH)
}
// Create temp directory for downloads
tempDir, err := os.MkdirTemp("", "proxypilot-update-*")
if err != nil {
return nil, fmt.Errorf("failed to create temp directory: %w", err)
}
// Download binary
binaryPath := filepath.Join(tempDir, binaryAsset.Name)
if err := downloadFile(binaryAsset.DownloadURL, binaryPath, binaryAsset.Size, progressCb); err != nil {
os.RemoveAll(tempDir)
return nil, fmt.Errorf("failed to download binary: %w", err)
}
result := &DownloadResult{
FilePath: binaryPath,
Version: version,
}
// Download signature if available
if sigAsset != nil {
sigPath := filepath.Join(tempDir, sigAsset.Name)
if err := downloadFile(sigAsset.DownloadURL, sigPath, sigAsset.Size, nil); err == nil {
result.SignaturePath = sigPath
}
}
return result, nil
}
func downloadFile(url, destPath string, expectedSize int64, progressCb func(DownloadProgress)) error {
client := &http.Client{Timeout: 30 * time.Minute}
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return err
}
req.Header.Set("User-Agent", "ProxyPilot-Updater")
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("download failed: %s", resp.Status)
}
out, err := os.Create(destPath)
if err != nil {
return err
}
defer out.Close()
// Use expected size from API if content-length is not set
totalSize := resp.ContentLength
if totalSize <= 0 {
totalSize = expectedSize
}
if progressCb != nil && totalSize > 0 {
// Wrap reader with progress tracking
pr := &progressReader{
reader: resp.Body,
total: totalSize,
callback: progressCb,
}
_, err = io.Copy(out, pr)
} else {
_, err = io.Copy(out, resp.Body)
}
return err
}
type progressReader struct {
reader io.Reader
total int64
current int64
callback func(DownloadProgress)
}
func (pr *progressReader) Read(p []byte) (int, error) {
n, err := pr.reader.Read(p)
pr.current += int64(n)
if pr.callback != nil {
pr.callback(DownloadProgress{
TotalBytes: pr.total,
DownloadedBytes: pr.current,
Percent: float64(pr.current) / float64(pr.total) * 100,
})
}
return n, err
}
+146
View File
@@ -0,0 +1,146 @@
package updates
import (
"crypto/sha256"
"encoding/hex"
"fmt"
"io"
"os"
"strings"
)
// VerifyResult contains the result of a verification check.
type VerifyResult struct {
Valid bool `json:"valid"`
Message string `json:"message"`
Checksum string `json:"checksum,omitempty"`
}
// VerifyDownload verifies the downloaded file.
// For now, this does basic checksum verification.
// In the future, this can be extended to verify GPG signatures.
func VerifyDownload(result *DownloadResult) (*VerifyResult, error) {
if result == nil || result.FilePath == "" {
return &VerifyResult{
Valid: false,
Message: "no download result provided",
}, nil
}
// Check file exists
info, err := os.Stat(result.FilePath)
if err != nil {
return &VerifyResult{
Valid: false,
Message: fmt.Sprintf("file not found: %v", err),
}, nil
}
// Check file has reasonable size (at least 1MB for a Go binary)
if info.Size() < 1024*1024 {
return &VerifyResult{
Valid: false,
Message: "downloaded file is too small to be valid",
}, nil
}
// Compute SHA256 checksum
checksum, err := computeSHA256(result.FilePath)
if err != nil {
return &VerifyResult{
Valid: false,
Message: fmt.Sprintf("failed to compute checksum: %v", err),
}, nil
}
// If we have a signature file, try to verify
if result.SignaturePath != "" {
sigValid, err := verifySignatureFile(result.FilePath, result.SignaturePath)
if err != nil {
// Signature verification failed - but file may still be valid
return &VerifyResult{
Valid: true, // Still valid, just no signature verification
Message: fmt.Sprintf("signature verification skipped: %v", err),
Checksum: checksum,
}, nil
}
if !sigValid {
return &VerifyResult{
Valid: false,
Message: "signature verification failed",
}, nil
}
return &VerifyResult{
Valid: true,
Message: "signature verified successfully",
Checksum: checksum,
}, nil
}
return &VerifyResult{
Valid: true,
Message: "basic verification passed (no signature available)",
Checksum: checksum,
}, nil
}
func computeSHA256(filePath string) (string, error) {
f, err := os.Open(filePath)
if err != nil {
return "", err
}
defer f.Close()
h := sha256.New()
if _, err := io.Copy(h, f); err != nil {
return "", err
}
return hex.EncodeToString(h.Sum(nil)), nil
}
// verifySignatureFile attempts to verify a GPG/PGP signature.
// This is a placeholder - real implementation would use golang.org/x/crypto/openpgp
// or similar library with an embedded public key.
func verifySignatureFile(filePath, sigPath string) (bool, error) {
// Check signature file exists
sigData, err := os.ReadFile(sigPath)
if err != nil {
return false, fmt.Errorf("failed to read signature file: %w", err)
}
// For now, just check it's not empty and looks like a signature
sigStr := strings.TrimSpace(string(sigData))
if len(sigStr) < 64 {
return false, fmt.Errorf("signature file too short")
}
// TODO: Implement actual GPG signature verification
// This requires embedding a public key and using openpgp library
return false, fmt.Errorf("GPG signature verification not yet implemented")
}
// VerifyChecksum verifies a file against an expected SHA256 checksum.
func VerifyChecksum(filePath, expectedChecksum string) (*VerifyResult, error) {
actualChecksum, err := computeSHA256(filePath)
if err != nil {
return &VerifyResult{
Valid: false,
Message: fmt.Sprintf("failed to compute checksum: %v", err),
}, nil
}
if strings.EqualFold(actualChecksum, expectedChecksum) {
return &VerifyResult{
Valid: true,
Message: "checksum verified successfully",
Checksum: actualChecksum,
}, nil
}
return &VerifyResult{
Valid: false,
Message: fmt.Sprintf("checksum mismatch: expected %s, got %s", expectedChecksum, actualChecksum),
Checksum: actualChecksum,
}, nil
}
+170 -21
View File
@@ -6,11 +6,13 @@ package usage
import (
"context"
"fmt"
"strings"
"sync"
"sync/atomic"
"time"
"github.com/gin-gonic/gin"
"github.com/router-for-me/CLIProxyAPI/v6/internal/interfaces"
coreusage "github.com/router-for-me/CLIProxyAPI/v6/sdk/cliproxy/usage"
)
@@ -59,17 +61,21 @@ func StatisticsEnabled() bool { return statisticsEnabled.Load() }
type RequestStatistics struct {
mu sync.RWMutex
totalRequests int64
successCount int64
failureCount int64
totalTokens int64
totalRequests int64
successCount int64
failureCount int64
totalTokens int64
totalInputTokens int64
totalOutputTokens int64
apis map[string]*apiStats
requestsByDay map[string]int64
requestsByHour map[int]int64
tokensByDay map[string]int64
tokensByHour map[int]int64
requestsByDay map[string]int64
requestsByHour map[int]int64
tokensByDay map[string]int64
inputTokensByDay map[string]int64
outputTokensByDay map[string]int64
tokensByHour map[int]int64
}
// apiStats holds aggregated metrics for a single API key.
@@ -106,17 +112,21 @@ type TokenStats struct {
// StatisticsSnapshot represents an immutable view of the aggregated metrics.
type StatisticsSnapshot struct {
TotalRequests int64 `json:"total_requests"`
SuccessCount int64 `json:"success_count"`
FailureCount int64 `json:"failure_count"`
TotalTokens int64 `json:"total_tokens"`
TotalRequests int64 `json:"total_requests"`
SuccessCount int64 `json:"success_count"`
FailureCount int64 `json:"failure_count"`
TotalTokens int64 `json:"total_tokens"`
TotalInputTokens int64 `json:"total_input_tokens"`
TotalOutputTokens int64 `json:"total_output_tokens"`
APIs map[string]APISnapshot `json:"apis"`
RequestsByDay map[string]int64 `json:"requests_by_day"`
RequestsByHour map[string]int64 `json:"requests_by_hour"`
TokensByDay map[string]int64 `json:"tokens_by_day"`
TokensByHour map[string]int64 `json:"tokens_by_hour"`
RequestsByDay map[string]int64 `json:"requests_by_day"`
RequestsByHour map[string]int64 `json:"requests_by_hour"`
TokensByDay map[string]int64 `json:"tokens_by_day"`
InputTokensByDay map[string]int64 `json:"input_tokens_by_day"`
OutputTokensByDay map[string]int64 `json:"output_tokens_by_day"`
TokensByHour map[string]int64 `json:"tokens_by_hour"`
}
// APISnapshot summarises metrics for a single API key.
@@ -141,11 +151,13 @@ func GetRequestStatistics() *RequestStatistics { return defaultRequestStatistics
// NewRequestStatistics constructs an empty statistics store.
func NewRequestStatistics() *RequestStatistics {
return &RequestStatistics{
apis: make(map[string]*apiStats),
requestsByDay: make(map[string]int64),
requestsByHour: make(map[int]int64),
tokensByDay: make(map[string]int64),
tokensByHour: make(map[int]int64),
apis: make(map[string]*apiStats),
requestsByDay: make(map[string]int64),
requestsByHour: make(map[int]int64),
tokensByDay: make(map[string]int64),
inputTokensByDay: make(map[string]int64),
outputTokensByDay: make(map[string]int64),
tokensByHour: make(map[int]int64),
}
}
@@ -163,6 +175,8 @@ func (s *RequestStatistics) Record(ctx context.Context, record coreusage.Record)
}
detail := normaliseDetail(record.Detail)
totalTokens := detail.TotalTokens
inputTokens := detail.InputTokens
outputTokens := detail.OutputTokens
statsKey := record.APIKey
if statsKey == "" {
statsKey = resolveAPIIdentifier(ctx, record)
@@ -189,6 +203,8 @@ func (s *RequestStatistics) Record(ctx context.Context, record coreusage.Record)
s.failureCount++
}
s.totalTokens += totalTokens
s.totalInputTokens += inputTokens
s.totalOutputTokens += outputTokens
stats, ok := s.apis[statsKey]
if !ok {
@@ -206,6 +222,8 @@ func (s *RequestStatistics) Record(ctx context.Context, record coreusage.Record)
s.requestsByDay[dayKey]++
s.requestsByHour[hourKey]++
s.tokensByDay[dayKey] += totalTokens
s.inputTokensByDay[dayKey] += inputTokens
s.outputTokensByDay[dayKey] += outputTokens
s.tokensByHour[hourKey] += totalTokens
}
@@ -236,6 +254,8 @@ func (s *RequestStatistics) Snapshot() StatisticsSnapshot {
result.SuccessCount = s.successCount
result.FailureCount = s.failureCount
result.TotalTokens = s.totalTokens
result.TotalInputTokens = s.totalInputTokens
result.TotalOutputTokens = s.totalOutputTokens
result.APIs = make(map[string]APISnapshot, len(s.apis))
for apiName, stats := range s.apis {
@@ -272,6 +292,16 @@ func (s *RequestStatistics) Snapshot() StatisticsSnapshot {
result.TokensByDay[k] = v
}
result.InputTokensByDay = make(map[string]int64, len(s.inputTokensByDay))
for k, v := range s.inputTokensByDay {
result.InputTokensByDay[k] = v
}
result.OutputTokensByDay = make(map[string]int64, len(s.outputTokensByDay))
for k, v := range s.outputTokensByDay {
result.OutputTokensByDay[k] = v
}
result.TokensByHour = make(map[string]int64, len(s.tokensByHour))
for hour, v := range s.tokensByHour {
key := formatHour(hour)
@@ -347,3 +377,122 @@ func formatHour(hour int) string {
hour = hour % 24
return fmt.Sprintf("%02d", hour)
}
// ComputeUsageStats converts a StatisticsSnapshot into a structured UsageStats object.
func ComputeUsageStats(snapshot StatisticsSnapshot) interfaces.UsageStats {
stats := interfaces.UsageStats{
TotalRequests: snapshot.TotalRequests,
SuccessCount: snapshot.SuccessCount,
FailureCount: snapshot.FailureCount,
TotalInputTokens: snapshot.TotalInputTokens,
TotalOutputTokens: snapshot.TotalOutputTokens,
EstimatedCostSaved: 0,
ActualCost: 0,
DirectAPICost: 0,
Savings: 0,
SavingsPercent: 0,
ByModel: make(map[string]int64),
ByProvider: make(map[string]int64),
CostByModel: make(map[string]float64),
CostByProvider: make(map[string]float64),
Daily: make([]interfaces.DailyUsage, 0),
}
var totalProxyCost float64
var totalDirectCost float64
for apiName, apiSnapshot := range snapshot.APIs {
// Try to identify provider from apiName
provider := apiName
// If apiName is "METHOD PATH", it's not a provider name.
if strings.Contains(apiName, " ") {
provider = "unknown"
}
for modelName, modelSnapshot := range apiSnapshot.Models {
stats.ByModel[modelName] += modelSnapshot.TotalRequests
// If provider is unknown, try to infer from model name
actualProvider := provider
if actualProvider == "unknown" {
actualProvider = inferProviderFromModel(modelName)
}
stats.ByProvider[actualProvider] += modelSnapshot.TotalRequests
for _, detail := range modelSnapshot.Details {
proxyCost, directCost := estimateCostWithSavings(
actualProvider,
modelName,
detail.Tokens.InputTokens,
detail.Tokens.OutputTokens,
detail.Tokens.CachedTokens,
)
totalProxyCost += proxyCost
totalDirectCost += directCost
stats.CostByModel[modelName] += proxyCost
stats.CostByProvider[actualProvider] += proxyCost
}
}
}
stats.ActualCost = totalProxyCost
stats.DirectAPICost = totalDirectCost
stats.Savings = totalDirectCost - totalProxyCost
if totalDirectCost > 0 {
stats.SavingsPercent = (stats.Savings / totalDirectCost) * 100
}
// Keep backward compatibility
stats.EstimatedCostSaved = totalProxyCost
// Last 7 days
now := time.Now()
for i := 6; i >= 0; i-- {
date := now.AddDate(0, 0, -i).Format("2006-01-02")
daily := interfaces.DailyUsage{
Date: date,
Requests: snapshot.RequestsByDay[date],
Tokens: snapshot.TokensByDay[date],
InputTokens: snapshot.InputTokensByDay[date],
OutputTokens: snapshot.OutputTokensByDay[date],
}
stats.Daily = append(stats.Daily, daily)
}
return stats
}
func inferProviderFromModel(model string) string {
m := strings.ToLower(model)
if strings.Contains(m, "claude") {
return "anthropic"
}
if strings.Contains(m, "gpt") {
return "openai"
}
if strings.Contains(m, "gemini") {
return "google"
}
return "unknown"
}
func estimateCost(provider string, model string, inputTokens, outputTokens int64) float64 {
// Try model-specific pricing first
proxyCost, _, found := EstimateModelCost(model, inputTokens, outputTokens, 0)
if found {
return proxyCost
}
// Fallback to provider-based estimation
return FallbackEstimateCost(provider, model, inputTokens, outputTokens)
}
// estimateCostWithSavings calculates both proxy cost and direct API cost.
// Returns (proxyCost, directCost).
func estimateCostWithSavings(provider string, model string, inputTokens, outputTokens, cachedTokens int64) (float64, float64) {
proxyCost, directCost, found := EstimateModelCost(model, inputTokens, outputTokens, cachedTokens)
if found {
return proxyCost, directCost
}
// Fallback - same cost for both
fallback := FallbackEstimateCost(provider, model, inputTokens, outputTokens)
return fallback, fallback
}
+194
View File
@@ -0,0 +1,194 @@
// Package usage provides usage tracking and cost estimation.
package usage
import "strings"
// ModelPricing defines the cost per million tokens for a model.
type ModelPricing struct {
InputPerMillion float64 // Cost per 1M input tokens
OutputPerMillion float64 // Cost per 1M output tokens
CachedPerMillion float64 // Cost per 1M cached input tokens (usually discounted)
}
// PricingTable maps model name patterns to their pricing.
// Prices are in USD per million tokens (as of Dec 2024).
var PricingTable = map[string]ModelPricing{
// Anthropic Claude models
"claude-opus-4": {15.00, 75.00, 1.50},
"claude-sonnet-4": {3.00, 15.00, 0.30},
"claude-3-5-sonnet": {3.00, 15.00, 0.30},
"claude-3-5-haiku": {0.80, 4.00, 0.08},
"claude-3-opus": {15.00, 75.00, 1.50},
"claude-3-sonnet": {3.00, 15.00, 0.30},
"claude-3-haiku": {0.25, 1.25, 0.025},
// OpenAI GPT models
"gpt-4o": {2.50, 10.00, 1.25},
"gpt-4o-mini": {0.15, 0.60, 0.075},
"gpt-4-turbo": {10.00, 30.00, 5.00},
"gpt-4": {30.00, 60.00, 15.00},
"gpt-3.5-turbo": {0.50, 1.50, 0.25},
"o1": {15.00, 60.00, 7.50},
"o1-mini": {3.00, 12.00, 1.50},
"o1-preview": {15.00, 60.00, 7.50},
"o3-mini": {1.10, 4.40, 0.55},
// Google Gemini models
"gemini-2.0-flash": {0.10, 0.40, 0.025},
"gemini-1.5-pro": {1.25, 5.00, 0.3125},
"gemini-1.5-flash": {0.075, 0.30, 0.01875},
"gemini-1.5-flash-8b": {0.0375, 0.15, 0.01},
"gemini-1.0-pro": {0.50, 1.50, 0.125},
// Qwen models (via API providers)
"qwen-turbo": {0.30, 0.60, 0.15},
"qwen-plus": {0.80, 2.00, 0.40},
"qwen-max": {2.40, 9.60, 1.20},
"qwq-32b": {0.15, 0.60, 0.075},
// DeepSeek models
"deepseek-chat": {0.14, 0.28, 0.07},
"deepseek-reasoner": {0.55, 2.19, 0.275},
// Codex/Copilot (estimated)
"codex": {0.00, 0.00, 0.00}, // Free tier proxy
"copilot": {0.00, 0.00, 0.00}, // Free tier proxy
}
// DirectAPIPricing stores the "official" direct API pricing for comparison.
// This is used to calculate savings when using ProxyPilot vs direct API.
var DirectAPIPricing = map[string]ModelPricing{
// Same as PricingTable - these are the direct API prices
"claude-opus-4": {15.00, 75.00, 1.50},
"claude-sonnet-4": {3.00, 15.00, 0.30},
"claude-3-5-sonnet": {3.00, 15.00, 0.30},
"claude-3-5-haiku": {0.80, 4.00, 0.08},
"claude-3-opus": {15.00, 75.00, 1.50},
"claude-3-sonnet": {3.00, 15.00, 0.30},
"claude-3-haiku": {0.25, 1.25, 0.025},
"gpt-4o": {2.50, 10.00, 1.25},
"gpt-4o-mini": {0.15, 0.60, 0.075},
"gpt-4-turbo": {10.00, 30.00, 5.00},
"gpt-4": {30.00, 60.00, 15.00},
"gpt-3.5-turbo": {0.50, 1.50, 0.25},
"o1": {15.00, 60.00, 7.50},
"o1-mini": {3.00, 12.00, 1.50},
"o1-preview": {15.00, 60.00, 7.50},
"o3-mini": {1.10, 4.40, 0.55},
"gemini-2.0-flash": {0.10, 0.40, 0.025},
"gemini-1.5-pro": {1.25, 5.00, 0.3125},
"gemini-1.5-flash": {0.075, 0.30, 0.01875},
"gemini-1.5-flash-8b": {0.0375, 0.15, 0.01},
"gemini-1.0-pro": {0.50, 1.50, 0.125},
"qwen-turbo": {0.30, 0.60, 0.15},
"qwen-plus": {0.80, 2.00, 0.40},
"qwen-max": {2.40, 9.60, 1.20},
"qwq-32b": {0.15, 0.60, 0.075},
"deepseek-chat": {0.14, 0.28, 0.07},
"deepseek-reasoner": {0.55, 2.19, 0.275},
}
// GetModelPricing returns the pricing for a given model.
// It attempts exact match first, then fuzzy matching on model name patterns.
func GetModelPricing(model string) (ModelPricing, bool) {
m := strings.ToLower(model)
// Exact match first
if pricing, ok := PricingTable[m]; ok {
return pricing, true
}
// Fuzzy match - check if model contains known pattern
for pattern, pricing := range PricingTable {
if strings.Contains(m, pattern) {
return pricing, true
}
}
// Try to match by prefix
for pattern, pricing := range PricingTable {
if strings.HasPrefix(m, pattern) || strings.HasPrefix(pattern, m) {
return pricing, true
}
}
return ModelPricing{}, false
}
// GetDirectAPIPricing returns the direct API pricing for comparison.
func GetDirectAPIPricing(model string) (ModelPricing, bool) {
m := strings.ToLower(model)
if pricing, ok := DirectAPIPricing[m]; ok {
return pricing, true
}
for pattern, pricing := range DirectAPIPricing {
if strings.Contains(m, pattern) {
return pricing, true
}
}
return ModelPricing{}, false
}
// CalculateCost calculates the cost for given token usage.
func CalculateCost(pricing ModelPricing, inputTokens, outputTokens, cachedTokens int64) float64 {
inputCost := float64(inputTokens) * pricing.InputPerMillion / 1_000_000
outputCost := float64(outputTokens) * pricing.OutputPerMillion / 1_000_000
cachedCost := float64(cachedTokens) * pricing.CachedPerMillion / 1_000_000
return inputCost + outputCost + cachedCost
}
// EstimateModelCost estimates the cost for a specific model and token usage.
// Returns (proxyCost, directCost, found).
func EstimateModelCost(model string, inputTokens, outputTokens, cachedTokens int64) (proxyCost, directCost float64, found bool) {
pricing, ok := GetModelPricing(model)
if !ok {
return 0, 0, false
}
directPricing, directOk := GetDirectAPIPricing(model)
if !directOk {
directPricing = pricing // Fallback to same pricing
}
proxyCost = CalculateCost(pricing, inputTokens, outputTokens, cachedTokens)
directCost = CalculateCost(directPricing, inputTokens, outputTokens, cachedTokens)
return proxyCost, directCost, true
}
// FallbackEstimateCost provides a fallback cost estimation based on provider.
// Used when model-specific pricing is not found.
func FallbackEstimateCost(provider, model string, inputTokens, outputTokens int64) float64 {
p := strings.ToLower(provider)
m := strings.ToLower(model)
var inputRate, outputRate float64
if strings.Contains(p, "anthropic") || strings.Contains(p, "claude") || strings.Contains(m, "claude") {
inputRate = 3.0 / 1_000_000
outputRate = 15.0 / 1_000_000
} else if strings.Contains(p, "openai") || strings.Contains(m, "gpt-4") || strings.Contains(m, "gpt-3.5") {
inputRate = 10.0 / 1_000_000
outputRate = 30.0 / 1_000_000
} else if strings.Contains(p, "google") || strings.Contains(p, "gemini") || strings.Contains(m, "gemini") {
inputRate = 0.50 / 1_000_000
outputRate = 1.50 / 1_000_000
} else if strings.Contains(p, "qwen") || strings.Contains(m, "qwen") {
inputRate = 0.80 / 1_000_000
outputRate = 2.00 / 1_000_000
} else if strings.Contains(p, "deepseek") || strings.Contains(m, "deepseek") {
inputRate = 0.14 / 1_000_000
outputRate = 0.28 / 1_000_000
} else {
return 0
}
return (float64(inputTokens) * inputRate) + (float64(outputTokens) * outputRate)
}
-77
View File
@@ -1,77 +0,0 @@
Set-StrictMode -Version Latest
$ErrorActionPreference = "Stop"
param(
[Parameter(Mandatory = $false)]
[string]$ProxyApiKey = $env:CLIPROXY_API_KEY,
[Parameter(Mandatory = $false)]
[string]$ProxyBaseUrl = "http://127.0.0.1:8317/v1",
[Parameter(Mandatory = $false)]
[string]$Model = "antigravity-claude-sonnet-4-5-thinking",
[Parameter(Mandatory = $false)]
[string]$EmbeddingModel = "text-embedding-004"
)
function Ensure-LettaInstalled {
Write-Host "Checking for Letta installation..."
if (Get-Command "letta" -ErrorAction SilentlyContinue) {
Write-Host "Letta is already installed."
return
}
Write-Host "Letta not found. Attempting to install via pip..."
& pip install letta
if ($LASTEXITCODE -ne 0) {
throw "Failed to install Letta. Please ensure Python and pip are installed and in your PATH."
}
Write-Host "Letta installed successfully."
}
function Configure-Letta {
param($BaseUrl, $ApiKey, $Model, $EmbeddingModel)
Write-Host "Configuring Letta to use ProxyPilot..."
# Set environment variables for the current session to ensure configure works
$env:LETTA_OPENAI_API_BASE = $BaseUrl
$env:LETTA_OPENAI_API_KEY = $ApiKey
# Run Letta configuration
# Note: Letta's config command structure might vary, but setting env vars
# and running a basic init is the most reliable way to bootstrap it.
Write-Host "Setting up Letta with:"
Write-Host " LLM Backend: $BaseUrl"
Write-Host " LLM Model: $Model"
Write-Host " Embed Model: $EmbeddingModel"
# We use the environment variable approach as it's the most robust across Letta versions
# Letta will pick up these variables when started by ProxyPilot.
# Create the .letta directory if it doesn't exist
$lettaDir = Join-Path $env:USERPROFILE ".letta"
if (-not (Test-Path $lettaDir)) {
New-Item -ItemType Directory -Path $lettaDir | Out-Null
}
Write-Host "Letta configuration prepared."
}
if (-not $ProxyApiKey -or $ProxyApiKey.Trim() -eq "") {
$ProxyApiKey = "local-dev-key"
Write-Host "No Proxy API Key provided, using default: $ProxyApiKey"
}
Ensure-LettaInstalled
Configure-Letta -BaseUrl $ProxyBaseUrl -ApiKey $ProxyApiKey -Model $Model -EmbeddingModel $EmbeddingModel
Write-Host ""
Write-Host "Setup Complete!"
Write-Host "1) Restart ProxyPilot to launch the Letta sidecar."
Write-Host "2) Your Letta logs will be available at: logs\letta.log"
Write-Host "3) Use 'letta run' in a new terminal to interact with your memory agent."
Write-Host ""
Write-Host "Note: ProxyPilot now provides REAL embeddings via Gemini text-embedding-004."
-8
View File
@@ -20,14 +20,6 @@ $configPath = Join-Path $repoRoot "config.yaml"
$logsDir = Join-Path $repoRoot "logs"
Ensure-Dir $logsDir
# Ensure Microsoft Store Python user Scripts path is on PATH so Letta can be found.
$pyScripts = Join-Path $env:USERPROFILE "AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.13_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python313\\Scripts"
if (Test-Path -LiteralPath $pyScripts) {
if ($env:Path -notlike "*$pyScripts*") {
$env:Path = "$pyScripts;$env:Path"
}
}
$stdoutLog = Join-Path $logsDir "proxypilot-engine.out.log"
$stderrLog = Join-Path $logsDir "proxypilot-engine.err.log"
+41
View File
@@ -48,6 +48,7 @@ const idempotencyKeyMetadataKey = "idempotency_key"
const (
defaultStreamingKeepAliveSeconds = 0
defaultStreamingBootstrapRetries = 0
defaultStreamingMaxChunkSize = 65536 // 64KB like vibeproxy
)
// BuildErrorResponseBody builds an OpenAI-compatible JSON error response body.
@@ -125,6 +126,19 @@ func StreamingBootstrapRetries(cfg *config.SDKConfig) int {
return retries
}
// StreamingMaxChunkSize returns the maximum size (in bytes) for individual response chunks.
// Returns 0 when chunk size limiting is disabled. Default is 64KB.
func StreamingMaxChunkSize(cfg *config.SDKConfig) int {
chunkSize := defaultStreamingMaxChunkSize
if cfg != nil && cfg.Streaming.MaxChunkSize != nil {
chunkSize = *cfg.Streaming.MaxChunkSize
}
if chunkSize < 0 {
chunkSize = 0
}
return chunkSize
}
func requestExecutionMetadata(ctx context.Context) map[string]any {
// Idempotency-Key is an optional client-supplied header used to correlate retries.
// It is forwarded as execution metadata; when absent we generate a UUID.
@@ -323,6 +337,15 @@ func (h *BaseAPIHandler) ExecuteWithAuthManager(ctx context.Context, handlerType
if errMsg != nil {
return nil, errMsg
}
// Set model and provider in context for logging/monitoring
if ginCtx, ok := ctx.Value("gin").(*gin.Context); ok && ginCtx != nil {
ginCtx.Set("model", normalizedModel)
if len(providers) > 0 {
ginCtx.Set("provider", providers[0])
}
}
reqMeta := requestExecutionMetadata(ctx)
req := coreexecutor.Request{
Model: normalizedModel,
@@ -364,6 +387,15 @@ func (h *BaseAPIHandler) ExecuteCountWithAuthManager(ctx context.Context, handle
if errMsg != nil {
return nil, errMsg
}
// Set model and provider in context for logging/monitoring
if ginCtx, ok := ctx.Value("gin").(*gin.Context); ok && ginCtx != nil {
ginCtx.Set("model", normalizedModel)
if len(providers) > 0 {
ginCtx.Set("provider", providers[0])
}
}
reqMeta := requestExecutionMetadata(ctx)
req := coreexecutor.Request{
Model: normalizedModel,
@@ -408,6 +440,15 @@ func (h *BaseAPIHandler) ExecuteStreamWithAuthManager(ctx context.Context, handl
close(errChan)
return nil, errChan
}
// Set model and provider in context for logging/monitoring
if ginCtx, ok := ctx.Value("gin").(*gin.Context); ok && ginCtx != nil {
ginCtx.Set("model", normalizedModel)
if len(providers) > 0 {
ginCtx.Set("provider", providers[0])
}
}
reqMeta := requestExecutionMetadata(ctx)
req := coreexecutor.Request{
Model: normalizedModel,
+24 -2
View File
@@ -13,6 +13,10 @@ type StreamForwardOptions struct {
// If nil, the configured default is used. If set to <= 0, keep-alives are disabled.
KeepAliveInterval *time.Duration
// MaxChunkSize overrides the configured maximum chunk size.
// If nil, the configured default is used. If set to <= 0, chunk splitting is disabled.
MaxChunkSize *int
// WriteChunk writes a single data chunk to the response body. It should not flush.
WriteChunk func(chunk []byte)
@@ -61,6 +65,12 @@ func (h *BaseAPIHandler) ForwardStream(c *gin.Context, flusher http.Flusher, can
keepAliveC = keepAlive.C
}
// Determine max chunk size for splitting large payloads
maxChunkSize := StreamingMaxChunkSize(h.Cfg)
if opts.MaxChunkSize != nil {
maxChunkSize = *opts.MaxChunkSize
}
var terminalErr *interfaces.ErrorMessage
for {
select {
@@ -94,8 +104,20 @@ func (h *BaseAPIHandler) ForwardStream(c *gin.Context, flusher http.Flusher, can
cancel(nil)
return
}
writeChunk(chunk)
flusher.Flush()
// Split large chunks to reduce latency (like vibeproxy's 64KB optimization)
if maxChunkSize > 0 && len(chunk) > maxChunkSize {
for offset := 0; offset < len(chunk); offset += maxChunkSize {
end := offset + maxChunkSize
if end > len(chunk) {
end = len(chunk)
}
writeChunk(chunk[offset:end])
flusher.Flush()
}
} else {
writeChunk(chunk)
flusher.Flush()
}
case errMsg, ok := <-errs:
if !ok {
continue
-27
View File
@@ -1,32 +1,5 @@
package auth
import (
"fmt"
"github.com/router-for-me/CLIProxyAPI/v6/internal/interfaces"
)
// ProjectSelectionError indicates that the user must choose a specific project ID.
type ProjectSelectionError struct {
Email string
Projects []interfaces.GCPProjectProjects
}
func (e *ProjectSelectionError) Error() string {
if e == nil {
return "cliproxy auth: project selection required"
}
return fmt.Sprintf("cliproxy auth: project selection required for %s", e.Email)
}
// ProjectsDisplay returns the projects list for caller presentation.
func (e *ProjectSelectionError) ProjectsDisplay() []interfaces.GCPProjectProjects {
if e == nil {
return nil
}
return e.Projects
}
// EmailRequiredError indicates that the calling context must provide an email or alias.
type EmailRequiredError struct {
Prompt string
+55
View File
@@ -13,6 +13,8 @@ import (
"time"
"github.com/router-for-me/CLIProxyAPI/v6/internal/api"
"github.com/router-for-me/CLIProxyAPI/v6/internal/api/middleware"
"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/usage"
@@ -21,11 +23,57 @@ import (
sdkaccess "github.com/router-for-me/CLIProxyAPI/v6/sdk/access"
sdkAuth "github.com/router-for-me/CLIProxyAPI/v6/sdk/auth"
coreauth "github.com/router-for-me/CLIProxyAPI/v6/sdk/cliproxy/auth"
cliproxyexecutor "github.com/router-for-me/CLIProxyAPI/v6/sdk/cliproxy/executor"
"github.com/router-for-me/CLIProxyAPI/v6/sdk/cliproxy/usage"
"github.com/router-for-me/CLIProxyAPI/v6/sdk/config"
log "github.com/sirupsen/logrus"
)
// coreManagerWrapper adapts coreauth.Manager to memory.CoreManagerExecutor interface.
// This allows the summarization executor to route requests through the auth manager
// without importing the cliproxyexecutor package directly in the memory package.
type coreManagerWrapper struct {
manager *coreauth.Manager
}
// Execute implements memory.CoreManagerExecutor by converting interface{} parameters
// to/from cliproxyexecutor types.
func (w *coreManagerWrapper) Execute(ctx context.Context, providers []string, req interface{}, opts interface{}) (interface{}, error) {
if w.manager == nil {
return nil, errors.New("coreManagerWrapper: manager not configured")
}
// Convert request from memory.ExecutorRequest to cliproxyexecutor.Request
memReq, ok := req.(memory.ExecutorRequest)
if !ok {
return nil, fmt.Errorf("coreManagerWrapper: expected memory.ExecutorRequest, got %T", req)
}
coreReq := cliproxyexecutor.Request{
Model: memReq.Model,
Payload: memReq.Payload,
Metadata: memReq.Metadata,
}
// Convert options from memory.ExecutorOptions to cliproxyexecutor.Options
var coreOpts cliproxyexecutor.Options
if memOpts, ok := opts.(memory.ExecutorOptions); ok {
coreOpts = cliproxyexecutor.Options{
Stream: memOpts.Stream,
Headers: memOpts.Headers,
}
}
// Execute through the real manager
resp, err := w.manager.Execute(ctx, providers, coreReq, coreOpts)
if err != nil {
return nil, err
}
// Return response as interface{} - the adapter will extract Payload
return resp, nil
}
// Service wraps the proxy server lifecycle so external programs can embed the CLI proxy.
// It manages the complete lifecycle including authentication, file watching, HTTP server,
// and integration with various AI service providers.
@@ -461,6 +509,13 @@ func (s *Service) Run(ctx context.Context) error {
// handlers no longer depend on legacy clients; pass nil slice initially
s.server = api.NewServer(s.cfg, s.coreManager, s.accessManager, s.configPath, s.serverOptions...)
// Wire up LLM summarization for Factory.ai-style context compression
if s.coreManager != nil {
providers := []string{"claude", "codex", "gemini"}
wrapper := &coreManagerWrapper{manager: s.coreManager}
middleware.InitSummarizerWithAuthManager(wrapper, providers)
}
if s.authManager == nil {
s.authManager = newDefaultAuthManager()
}
BIN
View File
Binary file not shown.

After

Width:  |  Height:  |  Size: 87 KiB

BIN
View File
Binary file not shown.

After

Width:  |  Height:  |  Size: 194 KiB

@@ -1,37 +0,0 @@
package test
import (
"context"
"testing"
sdktranslator "github.com/router-for-me/CLIProxyAPI/v6/sdk/translator"
"github.com/tidwall/gjson"
)
func TestAntigravityChatCompletionsStreamSupportsWrappedResponse(t *testing.T) {
from := sdktranslator.FromString("antigravity")
to := sdktranslator.FromString("openai")
originalReq := []byte(`{"model":"gemini-3-pro-preview","stream":true}`)
requestRaw := []byte(`{"model":"gemini-3-pro-preview","stream":true}`)
// Upstreams send the response wrapped in a "response" object.
chunk := []byte(`{"response":{"responseId":"r1","createTime":"2025-01-01T00:00:00Z","modelVersion":"gemini-3-pro-preview","candidates":[{"finishReason":"","content":{"parts":[{"text":"Hi"}]}}]}}`)
var param any
segments := sdktranslator.TranslateStream(context.Background(), from, to, "gemini-3-pro-preview", originalReq, requestRaw, chunk, &param)
if len(segments) != 1 {
t.Fatalf("expected 1 segment, got %d", len(segments))
}
out := segments[0]
if !gjson.Valid(out) {
t.Fatalf("expected valid JSON output, got: %s", out)
}
content := gjson.Get(out, "choices.0.delta.content").String()
if content != "Hi" {
t.Fatalf("expected content %q, got %q (out=%s)", "Hi", content, out)
}
}
-167
View File
@@ -1,167 +0,0 @@
package test
import (
"os"
"path/filepath"
"strings"
"testing"
"github.com/router-for-me/CLIProxyAPI/v6/internal/config"
)
func TestLegacyConfigMigration(t *testing.T) {
t.Run("onlyLegacyFields", func(t *testing.T) {
path := writeConfig(t, `
port: 8080
generative-language-api-key:
- "legacy-gemini-1"
openai-compatibility:
- name: "legacy-provider"
base-url: "https://example.com"
api-keys:
- "legacy-openai-1"
`)
cfg, err := config.LoadConfig(path)
if err != nil {
t.Fatalf("load legacy config: %v", err)
}
if got := len(cfg.GeminiKey); got != 1 || cfg.GeminiKey[0].APIKey != "legacy-gemini-1" {
t.Fatalf("gemini migration mismatch: %+v", cfg.GeminiKey)
}
if got := len(cfg.OpenAICompatibility); got != 1 {
t.Fatalf("expected 1 openai-compat provider, got %d", got)
}
if entries := cfg.OpenAICompatibility[0].APIKeyEntries; len(entries) != 1 || entries[0].APIKey != "legacy-openai-1" {
t.Fatalf("openai-compat migration mismatch: %+v", entries)
}
updated := readFile(t, path)
if strings.Contains(updated, "generative-language-api-key") {
t.Fatalf("legacy gemini key still present:\n%s", updated)
}
if strings.Contains(updated, "\n api-keys:") {
t.Fatalf("legacy openai compat keys still present:\n%s", updated)
}
})
t.Run("mixedLegacyAndNewFields", func(t *testing.T) {
path := writeConfig(t, `
gemini-api-key:
- api-key: "new-gemini"
generative-language-api-key:
- "new-gemini"
- "legacy-gemini-only"
openai-compatibility:
- name: "mixed-provider"
base-url: "https://mixed.example.com"
api-key-entries:
- api-key: "new-entry"
api-keys:
- "legacy-entry"
- "new-entry"
`)
cfg, err := config.LoadConfig(path)
if err != nil {
t.Fatalf("load mixed config: %v", err)
}
if got := len(cfg.GeminiKey); got != 2 {
t.Fatalf("expected 2 gemini entries, got %d: %+v", got, cfg.GeminiKey)
}
seen := make(map[string]struct{}, len(cfg.GeminiKey))
for _, entry := range cfg.GeminiKey {
if _, exists := seen[entry.APIKey]; exists {
t.Fatalf("duplicate gemini key %q after migration", entry.APIKey)
}
seen[entry.APIKey] = struct{}{}
}
provider := cfg.OpenAICompatibility[0]
if got := len(provider.APIKeyEntries); got != 2 {
t.Fatalf("expected 2 openai entries, got %d: %+v", got, provider.APIKeyEntries)
}
entrySeen := make(map[string]struct{}, len(provider.APIKeyEntries))
for _, entry := range provider.APIKeyEntries {
if _, ok := entrySeen[entry.APIKey]; ok {
t.Fatalf("duplicate openai key %q after migration", entry.APIKey)
}
entrySeen[entry.APIKey] = struct{}{}
}
})
t.Run("onlyNewFields", func(t *testing.T) {
path := writeConfig(t, `
gemini-api-key:
- api-key: "new-only"
openai-compatibility:
- name: "new-only-provider"
base-url: "https://new-only.example.com"
api-key-entries:
- api-key: "new-only-entry"
`)
cfg, err := config.LoadConfig(path)
if err != nil {
t.Fatalf("load new config: %v", err)
}
if len(cfg.GeminiKey) != 1 || cfg.GeminiKey[0].APIKey != "new-only" {
t.Fatalf("unexpected gemini entries: %+v", cfg.GeminiKey)
}
if len(cfg.OpenAICompatibility) != 1 || len(cfg.OpenAICompatibility[0].APIKeyEntries) != 1 {
t.Fatalf("unexpected openai compat entries: %+v", cfg.OpenAICompatibility)
}
})
t.Run("duplicateNamesDifferentBase", func(t *testing.T) {
path := writeConfig(t, `
openai-compatibility:
- name: "dup-provider"
base-url: "https://provider-a"
api-keys:
- "key-a"
- name: "dup-provider"
base-url: "https://provider-b"
api-keys:
- "key-b"
`)
cfg, err := config.LoadConfig(path)
if err != nil {
t.Fatalf("load duplicate config: %v", err)
}
if len(cfg.OpenAICompatibility) != 2 {
t.Fatalf("expected 2 providers, got %d", len(cfg.OpenAICompatibility))
}
for _, entry := range cfg.OpenAICompatibility {
if len(entry.APIKeyEntries) != 1 {
t.Fatalf("expected 1 key entry per provider: %+v", entry)
}
switch entry.BaseURL {
case "https://provider-a":
if entry.APIKeyEntries[0].APIKey != "key-a" {
t.Fatalf("provider-a key mismatch: %+v", entry.APIKeyEntries)
}
case "https://provider-b":
if entry.APIKeyEntries[0].APIKey != "key-b" {
t.Fatalf("provider-b key mismatch: %+v", entry.APIKeyEntries)
}
default:
t.Fatalf("unexpected provider base url: %s", entry.BaseURL)
}
}
})
}
func writeConfig(t *testing.T, content string) string {
t.Helper()
dir := t.TempDir()
path := filepath.Join(dir, "config.yaml")
if err := os.WriteFile(path, []byte(strings.TrimSpace(content)+"\n"), 0o644); err != nil {
t.Fatalf("write temp config: %v", err)
}
return path
}
func readFile(t *testing.T, path string) string {
t.Helper()
data, err := os.ReadFile(path)
if err != nil {
t.Fatalf("read temp config: %v", err)
}
return string(data)
}
-423
View File
@@ -1,423 +0,0 @@
package test
import (
"fmt"
"testing"
"time"
"github.com/router-for-me/CLIProxyAPI/v6/internal/registry"
"github.com/router-for-me/CLIProxyAPI/v6/internal/util"
"github.com/tidwall/gjson"
)
// registerGemini3Models loads Gemini 3 models into the registry for testing.
func registerGemini3Models(t *testing.T) func() {
t.Helper()
reg := registry.GetGlobalRegistry()
uid := fmt.Sprintf("gemini3-test-%d", time.Now().UnixNano())
reg.RegisterClient(uid+"-gemini", "gemini", registry.GetGeminiModels())
reg.RegisterClient(uid+"-aistudio", "aistudio", registry.GetAIStudioModels())
return func() {
reg.UnregisterClient(uid + "-gemini")
reg.UnregisterClient(uid + "-aistudio")
}
}
func TestIsGemini3Model(t *testing.T) {
cases := []struct {
model string
expected bool
}{
{"gemini-3-pro-preview", true},
{"gemini-3-flash-preview", true},
{"gemini_3_pro_preview", true},
{"gemini-3-pro", true},
{"gemini-3-flash", true},
{"GEMINI-3-PRO-PREVIEW", true},
{"gemini-2.5-pro", false},
{"gemini-2.5-flash", false},
{"gpt-5", false},
{"claude-sonnet-4-5", false},
{"", false},
}
for _, cs := range cases {
t.Run(cs.model, func(t *testing.T) {
got := util.IsGemini3Model(cs.model)
if got != cs.expected {
t.Fatalf("IsGemini3Model(%q) = %v, want %v", cs.model, got, cs.expected)
}
})
}
}
func TestIsGemini3ProModel(t *testing.T) {
cases := []struct {
model string
expected bool
}{
{"gemini-3-pro-preview", true},
{"gemini_3_pro_preview", true},
{"gemini-3-pro", true},
{"GEMINI-3-PRO-PREVIEW", true},
{"gemini-3-flash-preview", false},
{"gemini-3-flash", false},
{"gemini-2.5-pro", false},
{"", false},
}
for _, cs := range cases {
t.Run(cs.model, func(t *testing.T) {
got := util.IsGemini3ProModel(cs.model)
if got != cs.expected {
t.Fatalf("IsGemini3ProModel(%q) = %v, want %v", cs.model, got, cs.expected)
}
})
}
}
func TestIsGemini3FlashModel(t *testing.T) {
cases := []struct {
model string
expected bool
}{
{"gemini-3-flash-preview", true},
{"gemini_3_flash_preview", true},
{"gemini-3-flash", true},
{"GEMINI-3-FLASH-PREVIEW", true},
{"gemini-3-pro-preview", false},
{"gemini-3-pro", false},
{"gemini-2.5-flash", false},
{"", false},
}
for _, cs := range cases {
t.Run(cs.model, func(t *testing.T) {
got := util.IsGemini3FlashModel(cs.model)
if got != cs.expected {
t.Fatalf("IsGemini3FlashModel(%q) = %v, want %v", cs.model, got, cs.expected)
}
})
}
}
func TestValidateGemini3ThinkingLevel(t *testing.T) {
cases := []struct {
name string
model string
level string
wantOK bool
wantVal string
}{
// Gemini 3 Pro: supports "low", "high"
{"pro-low", "gemini-3-pro-preview", "low", true, "low"},
{"pro-high", "gemini-3-pro-preview", "high", true, "high"},
{"pro-minimal-invalid", "gemini-3-pro-preview", "minimal", false, ""},
{"pro-medium-invalid", "gemini-3-pro-preview", "medium", false, ""},
// Gemini 3 Flash: supports "minimal", "low", "medium", "high"
{"flash-minimal", "gemini-3-flash-preview", "minimal", true, "minimal"},
{"flash-low", "gemini-3-flash-preview", "low", true, "low"},
{"flash-medium", "gemini-3-flash-preview", "medium", true, "medium"},
{"flash-high", "gemini-3-flash-preview", "high", true, "high"},
// Case insensitivity
{"flash-LOW-case", "gemini-3-flash-preview", "LOW", true, "low"},
{"flash-High-case", "gemini-3-flash-preview", "High", true, "high"},
{"pro-HIGH-case", "gemini-3-pro-preview", "HIGH", true, "high"},
// Invalid levels
{"flash-invalid", "gemini-3-flash-preview", "xhigh", false, ""},
{"flash-invalid-auto", "gemini-3-flash-preview", "auto", false, ""},
{"flash-empty", "gemini-3-flash-preview", "", false, ""},
// Non-Gemini 3 models
{"non-gemini3", "gemini-2.5-pro", "high", false, ""},
{"gpt5", "gpt-5", "high", false, ""},
}
for _, cs := range cases {
t.Run(cs.name, func(t *testing.T) {
got, ok := util.ValidateGemini3ThinkingLevel(cs.model, cs.level)
if ok != cs.wantOK {
t.Fatalf("ValidateGemini3ThinkingLevel(%q, %q) ok = %v, want %v", cs.model, cs.level, ok, cs.wantOK)
}
if got != cs.wantVal {
t.Fatalf("ValidateGemini3ThinkingLevel(%q, %q) = %q, want %q", cs.model, cs.level, got, cs.wantVal)
}
})
}
}
func TestThinkingBudgetToGemini3Level(t *testing.T) {
cases := []struct {
name string
model string
budget int
wantOK bool
wantVal string
}{
// Gemini 3 Pro: maps to "low" or "high"
{"pro-dynamic", "gemini-3-pro-preview", -1, true, "high"},
{"pro-zero", "gemini-3-pro-preview", 0, true, "low"},
{"pro-small", "gemini-3-pro-preview", 1000, true, "low"},
{"pro-medium", "gemini-3-pro-preview", 8000, true, "low"},
{"pro-large", "gemini-3-pro-preview", 20000, true, "high"},
{"pro-huge", "gemini-3-pro-preview", 50000, true, "high"},
// Gemini 3 Flash: maps to "minimal", "low", "medium", "high"
{"flash-dynamic", "gemini-3-flash-preview", -1, true, "high"},
{"flash-zero", "gemini-3-flash-preview", 0, true, "minimal"},
{"flash-tiny", "gemini-3-flash-preview", 500, true, "minimal"},
{"flash-small", "gemini-3-flash-preview", 1000, true, "low"},
{"flash-medium-val", "gemini-3-flash-preview", 8000, true, "medium"},
{"flash-large", "gemini-3-flash-preview", 20000, true, "high"},
{"flash-huge", "gemini-3-flash-preview", 50000, true, "high"},
// Non-Gemini 3 models should return false
{"gemini25-budget", "gemini-2.5-pro", 8000, false, ""},
{"gpt5-budget", "gpt-5", 8000, false, ""},
}
for _, cs := range cases {
t.Run(cs.name, func(t *testing.T) {
got, ok := util.ThinkingBudgetToGemini3Level(cs.model, cs.budget)
if ok != cs.wantOK {
t.Fatalf("ThinkingBudgetToGemini3Level(%q, %d) ok = %v, want %v", cs.model, cs.budget, ok, cs.wantOK)
}
if got != cs.wantVal {
t.Fatalf("ThinkingBudgetToGemini3Level(%q, %d) = %q, want %q", cs.model, cs.budget, got, cs.wantVal)
}
})
}
}
func TestApplyGemini3ThinkingLevelFromMetadata(t *testing.T) {
cleanup := registerGemini3Models(t)
defer cleanup()
cases := []struct {
name string
model string
metadata map[string]any
inputBody string
wantLevel string
wantInclude bool
wantNoChange bool
}{
{
name: "flash-minimal-from-suffix",
model: "gemini-3-flash-preview",
metadata: map[string]any{"reasoning_effort": "minimal"},
inputBody: `{"generationConfig":{"thinkingConfig":{"includeThoughts":true}}}`,
wantLevel: "minimal",
wantInclude: true,
},
{
name: "flash-medium-from-suffix",
model: "gemini-3-flash-preview",
metadata: map[string]any{"reasoning_effort": "medium"},
inputBody: `{"generationConfig":{"thinkingConfig":{"includeThoughts":true}}}`,
wantLevel: "medium",
wantInclude: true,
},
{
name: "pro-high-from-suffix",
model: "gemini-3-pro-preview",
metadata: map[string]any{"reasoning_effort": "high"},
inputBody: `{"generationConfig":{"thinkingConfig":{"includeThoughts":true}}}`,
wantLevel: "high",
wantInclude: true,
},
{
name: "no-metadata-no-change",
model: "gemini-3-flash-preview",
metadata: nil,
inputBody: `{"generationConfig":{"thinkingConfig":{"includeThoughts":true}}}`,
wantNoChange: true,
},
{
name: "non-gemini3-no-change",
model: "gemini-2.5-pro",
metadata: map[string]any{"reasoning_effort": "high"},
inputBody: `{"generationConfig":{"thinkingConfig":{"thinkingBudget":-1}}}`,
wantNoChange: true,
},
{
name: "invalid-level-no-change",
model: "gemini-3-flash-preview",
metadata: map[string]any{"reasoning_effort": "xhigh"},
inputBody: `{"generationConfig":{"thinkingConfig":{"includeThoughts":true}}}`,
wantNoChange: true,
},
}
for _, cs := range cases {
t.Run(cs.name, func(t *testing.T) {
input := []byte(cs.inputBody)
result := util.ApplyGemini3ThinkingLevelFromMetadata(cs.model, cs.metadata, input)
if cs.wantNoChange {
if string(result) != cs.inputBody {
t.Fatalf("expected no change, but got: %s", string(result))
}
return
}
level := gjson.GetBytes(result, "generationConfig.thinkingConfig.thinkingLevel")
if !level.Exists() {
t.Fatalf("thinkingLevel not set in result: %s", string(result))
}
if level.String() != cs.wantLevel {
t.Fatalf("thinkingLevel = %q, want %q", level.String(), cs.wantLevel)
}
include := gjson.GetBytes(result, "generationConfig.thinkingConfig.includeThoughts")
if cs.wantInclude && (!include.Exists() || !include.Bool()) {
t.Fatalf("includeThoughts should be true, got: %s", string(result))
}
})
}
}
func TestApplyGemini3ThinkingLevelFromMetadataCLI(t *testing.T) {
cleanup := registerGemini3Models(t)
defer cleanup()
cases := []struct {
name string
model string
metadata map[string]any
inputBody string
wantLevel string
wantInclude bool
wantNoChange bool
}{
{
name: "flash-minimal-from-suffix-cli",
model: "gemini-3-flash-preview",
metadata: map[string]any{"reasoning_effort": "minimal"},
inputBody: `{"request":{"generationConfig":{"thinkingConfig":{"includeThoughts":true}}}}`,
wantLevel: "minimal",
wantInclude: true,
},
{
name: "flash-low-from-suffix-cli",
model: "gemini-3-flash-preview",
metadata: map[string]any{"reasoning_effort": "low"},
inputBody: `{"request":{"generationConfig":{"thinkingConfig":{"includeThoughts":true}}}}`,
wantLevel: "low",
wantInclude: true,
},
{
name: "pro-low-from-suffix-cli",
model: "gemini-3-pro-preview",
metadata: map[string]any{"reasoning_effort": "low"},
inputBody: `{"request":{"generationConfig":{"thinkingConfig":{"includeThoughts":true}}}}`,
wantLevel: "low",
wantInclude: true,
},
{
name: "no-metadata-no-change-cli",
model: "gemini-3-flash-preview",
metadata: nil,
inputBody: `{"request":{"generationConfig":{"thinkingConfig":{"includeThoughts":true}}}}`,
wantNoChange: true,
},
{
name: "non-gemini3-no-change-cli",
model: "gemini-2.5-pro",
metadata: map[string]any{"reasoning_effort": "high"},
inputBody: `{"request":{"generationConfig":{"thinkingConfig":{"thinkingBudget":-1}}}}`,
wantNoChange: true,
},
}
for _, cs := range cases {
t.Run(cs.name, func(t *testing.T) {
input := []byte(cs.inputBody)
result := util.ApplyGemini3ThinkingLevelFromMetadataCLI(cs.model, cs.metadata, input)
if cs.wantNoChange {
if string(result) != cs.inputBody {
t.Fatalf("expected no change, but got: %s", string(result))
}
return
}
level := gjson.GetBytes(result, "request.generationConfig.thinkingConfig.thinkingLevel")
if !level.Exists() {
t.Fatalf("thinkingLevel not set in result: %s", string(result))
}
if level.String() != cs.wantLevel {
t.Fatalf("thinkingLevel = %q, want %q", level.String(), cs.wantLevel)
}
include := gjson.GetBytes(result, "request.generationConfig.thinkingConfig.includeThoughts")
if cs.wantInclude && (!include.Exists() || !include.Bool()) {
t.Fatalf("includeThoughts should be true, got: %s", string(result))
}
})
}
}
func TestNormalizeGeminiThinkingBudget_Gemini3Conversion(t *testing.T) {
cleanup := registerGemini3Models(t)
defer cleanup()
cases := []struct {
name string
model string
inputBody string
wantLevel string
wantBudget bool // if true, expect thinkingBudget instead of thinkingLevel
}{
{
name: "gemini3-flash-budget-to-level",
model: "gemini-3-flash-preview",
inputBody: `{"generationConfig":{"thinkingConfig":{"thinkingBudget":8000}}}`,
wantLevel: "medium",
},
{
name: "gemini3-pro-budget-to-level",
model: "gemini-3-pro-preview",
inputBody: `{"generationConfig":{"thinkingConfig":{"thinkingBudget":20000}}}`,
wantLevel: "high",
},
{
name: "gemini25-keeps-budget",
model: "gemini-2.5-pro",
inputBody: `{"generationConfig":{"thinkingConfig":{"thinkingBudget":8000}}}`,
wantBudget: true,
},
}
for _, cs := range cases {
t.Run(cs.name, func(t *testing.T) {
result := util.NormalizeGeminiThinkingBudget(cs.model, []byte(cs.inputBody))
if cs.wantBudget {
budget := gjson.GetBytes(result, "generationConfig.thinkingConfig.thinkingBudget")
if !budget.Exists() {
t.Fatalf("thinkingBudget should exist for non-Gemini3 model: %s", string(result))
}
level := gjson.GetBytes(result, "generationConfig.thinkingConfig.thinkingLevel")
if level.Exists() {
t.Fatalf("thinkingLevel should not exist for non-Gemini3 model: %s", string(result))
}
} else {
level := gjson.GetBytes(result, "generationConfig.thinkingConfig.thinkingLevel")
if !level.Exists() {
t.Fatalf("thinkingLevel should exist for Gemini3 model: %s", string(result))
}
if level.String() != cs.wantLevel {
t.Fatalf("thinkingLevel = %q, want %q", level.String(), cs.wantLevel)
}
budget := gjson.GetBytes(result, "generationConfig.thinkingConfig.thinkingBudget")
if budget.Exists() {
t.Fatalf("thinkingBudget should be removed for Gemini3 model: %s", string(result))
}
}
})
}
}
-35
View File
@@ -1,35 +0,0 @@
package test
import (
"context"
"testing"
sdktranslator "github.com/router-for-me/CLIProxyAPI/v6/sdk/translator"
"github.com/tidwall/gjson"
)
func TestGeminiCLIChatCompletionsStripsSSEDataPrefix(t *testing.T) {
from := sdktranslator.FromString("gemini-cli")
to := sdktranslator.FromString("openai")
originalReq := []byte(`{"model":"gemini-3-pro-preview","stream":true}`)
requestRaw := []byte(`{"model":"gemini-3-pro-preview","stream":true}`)
chunk := []byte(`data: {"response":{"responseId":"r1","createTime":"2025-01-01T00:00:00Z","modelVersion":"gemini-3-pro-preview","candidates":[{"finishReason":"","content":{"parts":[{"text":"Hello"}]}}]}}`)
var param any
segments := sdktranslator.TranslateStream(context.Background(), from, to, "gemini-3-pro-preview", originalReq, requestRaw, chunk, &param)
if len(segments) != 1 {
t.Fatalf("expected 1 segment, got %d", len(segments))
}
out := segments[0]
if !gjson.Valid(out) {
t.Fatalf("expected valid JSON output, got: %s", out)
}
content := gjson.Get(out, "choices.0.delta.content").String()
if content != "Hello" {
t.Fatalf("expected content %q, got %q (out=%s)", "Hello", content, out)
}
}
@@ -1,36 +0,0 @@
package test
import (
"testing"
geminiresponses "github.com/router-for-me/CLIProxyAPI/v6/internal/translator/gemini/openai/responses"
"github.com/tidwall/gjson"
)
func TestGeminiResponsesRequest_SkipsEmptyAssistantTextMessage(t *testing.T) {
req := []byte(`{
"model":"gemini-claude-sonnet-4-5-thinking",
"input":[
{"type":"message","role":"user","content":[{"type":"input_text","text":"hi"}]},
{"type":"message","role":"assistant","content":[{"type":"output_text","text":""}]},
{"type":"function_call","call_id":"call_1","name":"shell","arguments":"{\"command\":[\"powershell.exe\",\"-Command\",\"echo hi\"]}"},
{"type":"function_call_output","call_id":"call_1","output":"hi"},
{"type":"message","role":"user","content":[{"type":"input_text","text":"ok"}]}
],
"tools":[{"type":"function","name":"shell","description":"Runs a Powershell command","parameters":{"type":"object","properties":{"command":{"type":"array","items":{"type":"string"}}},"required":["command"]}}],
"tool_choice":"auto"
}`)
out := geminiresponses.ConvertOpenAIResponsesRequestToGemini("gemini-claude-sonnet-4-5-thinking", req, false)
contents := gjson.GetBytes(out, "contents").Array()
for _, c := range contents {
if c.Get("role").String() != "model" {
continue
}
parts := c.Get("parts").Array()
if len(parts) == 1 && parts[0].Get("text").Exists() && parts[0].Get("text").String() == "" && !parts[0].Get("functionCall").Exists() {
t.Fatalf("found unexpected empty model text part in contents: %s", c.Raw)
}
}
}
@@ -1,48 +0,0 @@
package test
import (
"context"
"strings"
"testing"
geminiresponses "github.com/router-for-me/CLIProxyAPI/v6/internal/translator/gemini/openai/responses"
"github.com/tidwall/gjson"
)
func TestGeminiResponsesStream_DoneEventsContainFullText(t *testing.T) {
var state any
ctx := context.Background()
chunk1 := []byte(`{
"responseId":"resp_1",
"candidates":[{"content":{"role":"model","parts":[{"text":"Hello"}]}}]
}`)
evs1 := geminiresponses.ConvertGeminiResponseToOpenAIResponses(ctx, "gemini-claude-sonnet-4-5-thinking", nil, nil, chunk1, &state)
if len(evs1) == 0 {
t.Fatalf("expected initial chunk to emit events")
}
chunk2 := []byte(`{
"responseId":"resp_1",
"candidates":[{"finishReason":"STOP","content":{"role":"model","parts":[]}}]
}`)
evs2 := geminiresponses.ConvertGeminiResponseToOpenAIResponses(ctx, "gemini-claude-sonnet-4-5-thinking", nil, nil, chunk2, &state)
var itemDoneData string
for _, ev := range evs2 {
if strings.Contains(ev, "event: response.output_item.done") {
parts := strings.SplitN(ev, "data: ", 2)
if len(parts) == 2 {
itemDoneData = parts[1]
break
}
}
}
if itemDoneData == "" {
t.Fatalf("expected finish chunk to emit response.output_item.done events, got: %v", evs2)
}
got := gjson.Get(itemDoneData, "item.content.0.text").String()
if got != "Hello" {
t.Fatalf("expected output_item.done content text to be %q, got %q (data=%s)", "Hello", got, itemDoneData)
}
}
@@ -1,44 +0,0 @@
package test
import (
"context"
"testing"
openairesponses "github.com/router-for-me/CLIProxyAPI/v6/internal/translator/openai/openai/responses"
"github.com/tidwall/gjson"
)
func TestOpenAIResponsesNonStream_ReasoningOutputFirst(t *testing.T) {
// When reasoning_content is present, the OpenAI Responses API puts reasoning first.
// This matches the order in which they appear during streaming.
chatCompletion := []byte(`{
"id":"cmpl_1",
"object":"chat.completion",
"created":1700000000,
"model":"gemini-3-pro-preview",
"choices":[{"index":0,"message":{"role":"assistant","content":"hello","reasoning_content":"thinking..."},"finish_reason":"stop"}],
"usage":{"prompt_tokens":1,"completion_tokens":1,"total_tokens":2}
}`)
resp := openairesponses.ConvertOpenAIChatCompletionsResponseToOpenAIResponsesNonStream(
context.Background(),
"gemini-3-pro-preview",
nil,
[]byte(`{"model":"gemini-3-pro-preview"}`),
chatCompletion,
nil,
)
// Reasoning comes first when present
firstType := gjson.Get(resp, "output.0.type").String()
if firstType != "reasoning" {
t.Fatalf("expected output[0].type to be %q, got %q (resp=%s)", "reasoning", firstType, resp)
}
// Message comes second
secondType := gjson.Get(resp, "output.1.type").String()
if secondType != "message" {
t.Fatalf("expected output[1].type to be %q, got %q (resp=%s)", "message", secondType, resp)
}
}
-798
View File
@@ -1,798 +0,0 @@
package test
import (
"fmt"
"strings"
"testing"
"time"
_ "github.com/router-for-me/CLIProxyAPI/v6/internal/translator"
"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"
sdktranslator "github.com/router-for-me/CLIProxyAPI/v6/sdk/translator"
"github.com/tidwall/gjson"
"github.com/tidwall/sjson"
)
// isOpenAICompatModel returns true if the model is configured as an OpenAI-compatible
// model that should have reasoning effort passed through even if not in registry.
// This simulates the allowCompat behavior from OpenAICompatExecutor.
func isOpenAICompatModel(model string) bool {
return model == "openai-compat"
}
// registerCoreModels loads representative models across providers into the registry
// so NormalizeThinkingBudget and level validation use real ranges.
func registerCoreModels(t *testing.T) func() {
t.Helper()
reg := registry.GetGlobalRegistry()
uid := fmt.Sprintf("thinking-core-%d", time.Now().UnixNano())
reg.RegisterClient(uid+"-gemini", "gemini", registry.GetGeminiModels())
reg.RegisterClient(uid+"-claude", "claude", registry.GetClaudeModels())
reg.RegisterClient(uid+"-openai", "codex", registry.GetOpenAIModels())
reg.RegisterClient(uid+"-qwen", "qwen", registry.GetQwenModels())
// Custom openai-compatible model with forced thinking suffix passthrough.
// No Thinking field - simulates an external model added via openai-compat
// where the registry has no knowledge of its thinking capabilities.
// The allowCompat flag should preserve reasoning effort for such models.
customOpenAIModels := []*registry.ModelInfo{
{
ID: "openai-compat",
Object: "model",
Created: 1700000000,
OwnedBy: "custom-provider",
Type: "openai",
DisplayName: "OpenAI Compatible Model",
Description: "OpenAI-compatible model with forced thinking suffix support",
},
}
reg.RegisterClient(uid+"-custom-openai", "codex", customOpenAIModels)
return func() {
reg.UnregisterClient(uid + "-gemini")
reg.UnregisterClient(uid + "-claude")
reg.UnregisterClient(uid + "-openai")
reg.UnregisterClient(uid + "-qwen")
reg.UnregisterClient(uid + "-custom-openai")
}
}
var (
thinkingTestModels = []string{
"gpt-5", // level-based thinking model
"gemini-2.5-pro", // numeric-budget thinking model
"qwen3-code-plus", // no thinking support
"openai-compat", // allowCompat=true (OpenAI-compatible channel)
}
thinkingTestFromProtocols = []string{"openai", "claude", "gemini", "openai-response"}
thinkingTestToProtocols = []string{"gemini", "claude", "openai", "codex"}
// Numeric budgets and their level equivalents:
// -1 -> auto
// 0 -> none
// 1..1024 -> low
// 1025..8192 -> medium
// 8193..24576 -> high
// >24576 -> model highest level (right-most in Levels)
thinkingNumericSamples = []int{-1, 0, 1023, 1025, 8193, 64000}
// Levels and their numeric equivalents:
// auto -> -1
// none -> 0
// minimal -> 512
// low -> 1024
// medium -> 8192
// high -> 24576
// xhigh -> 32768
// invalid -> invalid (no mapping)
thinkingLevelSamples = []string{"auto", "none", "minimal", "low", "medium", "high", "xhigh", "invalid"}
)
func buildRawPayload(fromProtocol, modelWithSuffix string) []byte {
switch fromProtocol {
case "gemini":
return []byte(fmt.Sprintf(`{"model":"%s","contents":[{"role":"user","parts":[{"text":"hi"}]}]}`, modelWithSuffix))
case "openai-response":
return []byte(fmt.Sprintf(`{"model":"%s","input":[{"role":"user","content":[{"type":"text","text":"hi"}]}]}`, modelWithSuffix))
default: // openai / claude and other chat-style payloads
return []byte(fmt.Sprintf(`{"model":"%s","messages":[{"role":"user","content":"hi"}]}`, modelWithSuffix))
}
}
// normalizeCodexPayload mirrors codex_executor's reasoning + streaming tweaks.
func normalizeCodexPayload(body []byte, upstreamModel string, allowCompat bool) ([]byte, error) {
body = executor.NormalizeThinkingConfig(body, upstreamModel, allowCompat)
if err := executor.ValidateThinkingConfig(body, upstreamModel); err != nil {
return body, err
}
body, _ = sjson.SetBytes(body, "model", upstreamModel)
body, _ = sjson.SetBytes(body, "stream", true)
body, _ = sjson.DeleteBytes(body, "previous_response_id")
return body, nil
}
// buildBodyForProtocol runs a minimal request through the same translation and
// thinking pipeline used in executors for the given target protocol.
func buildBodyForProtocol(t *testing.T, fromProtocol, toProtocol, modelWithSuffix string) ([]byte, error) {
t.Helper()
normalizedModel, metadata := util.NormalizeThinkingModel(modelWithSuffix)
upstreamModel := util.ResolveOriginalModel(normalizedModel, metadata)
raw := buildRawPayload(fromProtocol, modelWithSuffix)
stream := fromProtocol != toProtocol
body := sdktranslator.TranslateRequest(
sdktranslator.FromString(fromProtocol),
sdktranslator.FromString(toProtocol),
normalizedModel,
raw,
stream,
)
var err error
allowCompat := isOpenAICompatModel(normalizedModel)
switch toProtocol {
case "gemini":
body = executor.ApplyThinkingMetadata(body, metadata, normalizedModel)
body = util.ApplyDefaultThinkingIfNeeded(normalizedModel, body)
body = util.NormalizeGeminiThinkingBudget(normalizedModel, body)
body = util.StripThinkingConfigIfUnsupported(normalizedModel, body)
case "claude":
if budget, ok := util.ResolveClaudeThinkingConfig(normalizedModel, metadata); ok {
body = util.ApplyClaudeThinkingConfig(body, budget)
}
case "openai":
body = executor.ApplyReasoningEffortMetadata(body, metadata, normalizedModel, "reasoning_effort", allowCompat)
body = executor.NormalizeThinkingConfig(body, upstreamModel, allowCompat)
err = executor.ValidateThinkingConfig(body, upstreamModel)
case "codex": // OpenAI responses / codex
// Codex does not support allowCompat; always use false.
body = executor.ApplyReasoningEffortMetadata(body, metadata, normalizedModel, "reasoning.effort", false)
// Mirror CodexExecutor final normalization and model override so tests log the final body.
body, err = normalizeCodexPayload(body, upstreamModel, false)
default:
}
// Mirror executor behavior: final payload uses the upstream (base) model name.
if upstreamModel != "" {
body, _ = sjson.SetBytes(body, "model", upstreamModel)
}
// For tests we only keep model + thinking-related fields to avoid noise.
body = filterThinkingBody(toProtocol, body, upstreamModel, normalizedModel)
return body, err
}
// filterThinkingBody projects the translated payload down to only model and
// thinking-related fields for the given target protocol.
func filterThinkingBody(toProtocol string, body []byte, upstreamModel, normalizedModel string) []byte {
if len(body) == 0 {
return body
}
out := []byte(`{}`)
// Preserve model if present, otherwise fall back to upstream/normalized model.
if m := gjson.GetBytes(body, "model"); m.Exists() {
out, _ = sjson.SetBytes(out, "model", m.Value())
} else if upstreamModel != "" {
out, _ = sjson.SetBytes(out, "model", upstreamModel)
} else if normalizedModel != "" {
out, _ = sjson.SetBytes(out, "model", normalizedModel)
}
switch toProtocol {
case "gemini":
if tc := gjson.GetBytes(body, "generationConfig.thinkingConfig"); tc.Exists() {
out, _ = sjson.SetRawBytes(out, "generationConfig.thinkingConfig", []byte(tc.Raw))
}
case "claude":
if tcfg := gjson.GetBytes(body, "thinking"); tcfg.Exists() {
out, _ = sjson.SetRawBytes(out, "thinking", []byte(tcfg.Raw))
}
case "openai":
if re := gjson.GetBytes(body, "reasoning_effort"); re.Exists() {
out, _ = sjson.SetBytes(out, "reasoning_effort", re.Value())
}
case "codex":
if re := gjson.GetBytes(body, "reasoning.effort"); re.Exists() {
out, _ = sjson.SetBytes(out, "reasoning.effort", re.Value())
}
}
return out
}
func TestThinkingConversionsAcrossProtocolsAndModels(t *testing.T) {
cleanup := registerCoreModels(t)
defer cleanup()
type scenario struct {
name string
modelSuffix string
}
numericName := func(budget int) string {
if budget < 0 {
return "numeric-neg1"
}
return fmt.Sprintf("numeric-%d", budget)
}
for _, model := range thinkingTestModels {
_ = registry.GetGlobalRegistry().GetModelInfo(model)
for _, from := range thinkingTestFromProtocols {
// Scenario selection follows protocol semantics:
// - OpenAI-style protocols (openai/openai-response) express thinking as levels.
// - Claude/Gemini-style protocols express thinking as numeric budgets.
cases := []scenario{
{name: "no-suffix", modelSuffix: model},
}
if from == "openai" || from == "openai-response" {
for _, lvl := range thinkingLevelSamples {
cases = append(cases, scenario{
name: "level-" + lvl,
modelSuffix: fmt.Sprintf("%s(%s)", model, lvl),
})
}
} else { // claude or gemini
for _, budget := range thinkingNumericSamples {
budget := budget
cases = append(cases, scenario{
name: numericName(budget),
modelSuffix: fmt.Sprintf("%s(%d)", model, budget),
})
}
}
for _, to := range thinkingTestToProtocols {
if from == to {
continue
}
t.Logf("─────────────────────────────────────────────────────────────────────────────────")
t.Logf(" %s -> %s | model: %s", from, to, model)
t.Logf("─────────────────────────────────────────────────────────────────────────────────")
for _, cs := range cases {
from := from
to := to
cs := cs
testName := fmt.Sprintf("%s->%s/%s/%s", from, to, model, cs.name)
t.Run(testName, func(t *testing.T) {
normalizedModel, metadata := util.NormalizeThinkingModel(cs.modelSuffix)
expectPresent, expectValue, expectErr := func() (bool, string, bool) {
switch to {
case "gemini":
budget, include, ok := util.ResolveThinkingConfigFromMetadata(normalizedModel, metadata)
if !ok || !util.ModelSupportsThinking(normalizedModel) {
return false, "", false
}
if include != nil && !*include {
return false, "", false
}
if budget == nil {
return false, "", false
}
norm := util.NormalizeThinkingBudget(normalizedModel, *budget)
return true, fmt.Sprintf("%d", norm), false
case "claude":
if !util.ModelSupportsThinking(normalizedModel) {
return false, "", false
}
budget, ok := util.ResolveClaudeThinkingConfig(normalizedModel, metadata)
if !ok || budget == nil {
return false, "", false
}
return true, fmt.Sprintf("%d", *budget), false
case "openai":
allowCompat := isOpenAICompatModel(normalizedModel)
if !util.ModelSupportsThinking(normalizedModel) && !allowCompat {
return false, "", false
}
// For allowCompat models, pass through effort directly without validation
if allowCompat {
effort, ok := util.ReasoningEffortFromMetadata(metadata)
if ok && strings.TrimSpace(effort) != "" {
return true, strings.ToLower(strings.TrimSpace(effort)), false
}
// Check numeric budget fallback for allowCompat
if budget, _, _, matched := util.ThinkingFromMetadata(metadata); matched && budget != nil {
if mapped, okMap := util.ThinkingBudgetToEffort(normalizedModel, *budget); okMap && mapped != "" {
return true, mapped, false
}
}
return false, "", false
}
if !util.ModelUsesThinkingLevels(normalizedModel) {
// Non-levels models don't support effort strings in openai
return false, "", false
}
effort, ok := util.ReasoningEffortFromMetadata(metadata)
if !ok || strings.TrimSpace(effort) == "" {
if budget, _, _, matched := util.ThinkingFromMetadata(metadata); matched && budget != nil {
if mapped, okMap := util.ThinkingBudgetToEffort(normalizedModel, *budget); okMap {
effort = mapped
ok = true
}
}
}
if !ok || strings.TrimSpace(effort) == "" {
return false, "", false
}
effort = strings.ToLower(strings.TrimSpace(effort))
if normalized, okLevel := util.NormalizeReasoningEffortLevel(normalizedModel, effort); okLevel {
return true, normalized, false
}
return false, "", true // validation would fail
case "codex":
// Codex does not support allowCompat; require thinking-capable level models.
if !util.ModelSupportsThinking(normalizedModel) || !util.ModelUsesThinkingLevels(normalizedModel) {
return false, "", false
}
effort, ok := util.ReasoningEffortFromMetadata(metadata)
if ok && strings.TrimSpace(effort) != "" {
effort = strings.ToLower(strings.TrimSpace(effort))
if normalized, okLevel := util.NormalizeReasoningEffortLevel(normalizedModel, effort); okLevel {
return true, normalized, false
}
return false, "", true
}
if budget, _, _, matched := util.ThinkingFromMetadata(metadata); matched && budget != nil {
if mapped, okMap := util.ThinkingBudgetToEffort(normalizedModel, *budget); okMap && mapped != "" {
mapped = strings.ToLower(strings.TrimSpace(mapped))
if normalized, okLevel := util.NormalizeReasoningEffortLevel(normalizedModel, mapped); okLevel {
return true, normalized, false
}
return false, "", true
}
}
if from != "openai-response" {
// Codex translators default reasoning.effort to "medium" when
// no explicit thinking suffix/metadata is provided.
return true, "medium", false
}
return false, "", false
default:
return false, "", false
}
}()
body, err := buildBodyForProtocol(t, from, to, cs.modelSuffix)
actualPresent, actualValue := func() (bool, string) {
path := ""
switch to {
case "gemini":
path = "generationConfig.thinkingConfig.thinkingBudget"
case "claude":
path = "thinking.budget_tokens"
case "openai":
path = "reasoning_effort"
case "codex":
path = "reasoning.effort"
}
if path == "" {
return false, ""
}
val := gjson.GetBytes(body, path)
if to == "codex" && !val.Exists() {
reasoning := gjson.GetBytes(body, "reasoning")
if reasoning.Exists() {
val = reasoning.Get("effort")
}
}
if !val.Exists() {
return false, ""
}
if val.Type == gjson.Number {
return true, fmt.Sprintf("%d", val.Int())
}
return true, val.String()
}()
t.Logf("from=%s to=%s model=%s suffix=%s present(expect=%v got=%v) value(expect=%s got=%s) err(expect=%v got=%v) body=%s",
from, to, model, cs.modelSuffix, expectPresent, actualPresent, expectValue, actualValue, expectErr, err != nil, string(body))
if expectErr {
if err == nil {
t.Fatalf("expected validation error but got none, body=%s", string(body))
}
return
}
if err != nil {
t.Fatalf("unexpected error: %v body=%s", err, string(body))
}
if expectPresent != actualPresent {
t.Fatalf("presence mismatch: expect %v got %v body=%s", expectPresent, actualPresent, string(body))
}
if expectPresent && expectValue != actualValue {
t.Fatalf("value mismatch: expect %s got %s body=%s", expectValue, actualValue, string(body))
}
})
}
}
}
}
}
// buildRawPayloadWithThinking creates a payload with thinking parameters already in the body.
// This tests the path where thinking comes from the raw payload, not model suffix.
func buildRawPayloadWithThinking(fromProtocol, model string, thinkingParam any) []byte {
switch fromProtocol {
case "gemini":
base := fmt.Sprintf(`{"model":"%s","contents":[{"role":"user","parts":[{"text":"hi"}]}]}`, model)
if budget, ok := thinkingParam.(int); ok {
base, _ = sjson.Set(base, "generationConfig.thinkingConfig.thinkingBudget", budget)
}
return []byte(base)
case "openai-response":
base := fmt.Sprintf(`{"model":"%s","input":[{"role":"user","content":[{"type":"text","text":"hi"}]}]}`, model)
if effort, ok := thinkingParam.(string); ok && effort != "" {
base, _ = sjson.Set(base, "reasoning.effort", effort)
}
return []byte(base)
case "openai":
base := fmt.Sprintf(`{"model":"%s","messages":[{"role":"user","content":"hi"}]}`, model)
if effort, ok := thinkingParam.(string); ok && effort != "" {
base, _ = sjson.Set(base, "reasoning_effort", effort)
}
return []byte(base)
case "claude":
base := fmt.Sprintf(`{"model":"%s","messages":[{"role":"user","content":"hi"}]}`, model)
if budget, ok := thinkingParam.(int); ok {
base, _ = sjson.Set(base, "thinking.type", "enabled")
base, _ = sjson.Set(base, "thinking.budget_tokens", budget)
}
return []byte(base)
default:
return []byte(fmt.Sprintf(`{"model":"%s","messages":[{"role":"user","content":"hi"}]}`, model))
}
}
// buildBodyForProtocolWithRawThinking translates payload with raw thinking params.
func buildBodyForProtocolWithRawThinking(t *testing.T, fromProtocol, toProtocol, model string, thinkingParam any) ([]byte, error) {
t.Helper()
raw := buildRawPayloadWithThinking(fromProtocol, model, thinkingParam)
stream := fromProtocol != toProtocol
body := sdktranslator.TranslateRequest(
sdktranslator.FromString(fromProtocol),
sdktranslator.FromString(toProtocol),
model,
raw,
stream,
)
var err error
allowCompat := isOpenAICompatModel(model)
switch toProtocol {
case "gemini":
body = util.ApplyDefaultThinkingIfNeeded(model, body)
body = util.NormalizeGeminiThinkingBudget(model, body)
body = util.StripThinkingConfigIfUnsupported(model, body)
case "claude":
// For raw payload, Claude thinking is passed through by translator
// No additional processing needed as thinking is already in body
case "openai":
body = executor.NormalizeThinkingConfig(body, model, allowCompat)
err = executor.ValidateThinkingConfig(body, model)
case "codex":
// Codex does not support allowCompat; always use false.
body, err = normalizeCodexPayload(body, model, false)
}
body, _ = sjson.SetBytes(body, "model", model)
body = filterThinkingBody(toProtocol, body, model, model)
return body, err
}
func TestRawPayloadThinkingConversions(t *testing.T) {
cleanup := registerCoreModels(t)
defer cleanup()
type scenario struct {
name string
thinkingParam any // int for budget, string for effort level
}
numericName := func(budget int) string {
if budget < 0 {
return "budget-neg1"
}
return fmt.Sprintf("budget-%d", budget)
}
for _, model := range thinkingTestModels {
supportsThinking := util.ModelSupportsThinking(model)
usesLevels := util.ModelUsesThinkingLevels(model)
allowCompat := isOpenAICompatModel(model)
for _, from := range thinkingTestFromProtocols {
var cases []scenario
switch from {
case "openai", "openai-response":
cases = []scenario{
{name: "no-thinking", thinkingParam: nil},
}
for _, lvl := range thinkingLevelSamples {
cases = append(cases, scenario{
name: "effort-" + lvl,
thinkingParam: lvl,
})
}
case "gemini", "claude":
cases = []scenario{
{name: "no-thinking", thinkingParam: nil},
}
for _, budget := range thinkingNumericSamples {
budget := budget
cases = append(cases, scenario{
name: numericName(budget),
thinkingParam: budget,
})
}
}
for _, to := range thinkingTestToProtocols {
if from == to {
continue
}
t.Logf("═══════════════════════════════════════════════════════════════════════════════")
t.Logf(" RAW PAYLOAD: %s -> %s | model: %s", from, to, model)
t.Logf("═══════════════════════════════════════════════════════════════════════════════")
for _, cs := range cases {
from := from
to := to
cs := cs
testName := fmt.Sprintf("raw/%s->%s/%s/%s", from, to, model, cs.name)
t.Run(testName, func(t *testing.T) {
expectPresent, expectValue, expectErr := func() (bool, string, bool) {
if cs.thinkingParam == nil {
if to == "codex" && from != "openai-response" && supportsThinking && usesLevels {
// Codex translators default reasoning.effort to "medium" for thinking-capable level models
return true, "medium", false
}
return false, "", false
}
switch to {
case "gemini":
if !supportsThinking || usesLevels {
return false, "", false
}
// Gemini expects numeric budget (only for non-level models)
if budget, ok := cs.thinkingParam.(int); ok {
norm := util.NormalizeThinkingBudget(model, budget)
return true, fmt.Sprintf("%d", norm), false
}
// Convert effort level to budget for non-level models only
if effort, ok := cs.thinkingParam.(string); ok && effort != "" {
// "none" disables thinking - no thinkingBudget in output
if strings.ToLower(effort) == "none" {
return false, "", false
}
if budget, okB := util.ThinkingEffortToBudget(model, effort); okB {
// ThinkingEffortToBudget already returns normalized budget
return true, fmt.Sprintf("%d", budget), false
}
// Invalid effort does not map to a budget
return false, "", false
}
return false, "", false
case "claude":
if !supportsThinking || usesLevels {
return false, "", false
}
// Claude expects numeric budget (only for non-level models)
if budget, ok := cs.thinkingParam.(int); ok && budget > 0 {
norm := util.NormalizeThinkingBudget(model, budget)
return true, fmt.Sprintf("%d", norm), false
}
// Convert effort level to budget for non-level models only
if effort, ok := cs.thinkingParam.(string); ok && effort != "" {
// "none" and "auto" don't produce budget_tokens
lower := strings.ToLower(effort)
if lower == "none" || lower == "auto" {
return false, "", false
}
if budget, okB := util.ThinkingEffortToBudget(model, effort); okB {
// ThinkingEffortToBudget already returns normalized budget
return true, fmt.Sprintf("%d", budget), false
}
// Invalid effort - claude sets thinking.type:enabled but no budget_tokens
return false, "", false
}
return false, "", false
case "openai":
if allowCompat {
if effort, ok := cs.thinkingParam.(string); ok && strings.TrimSpace(effort) != "" {
normalized := strings.ToLower(strings.TrimSpace(effort))
return true, normalized, false
}
if budget, ok := cs.thinkingParam.(int); ok {
if mapped, okM := util.ThinkingBudgetToEffort(model, budget); okM && mapped != "" {
return true, mapped, false
}
}
return false, "", false
}
if !supportsThinking || !usesLevels {
return false, "", false
}
if effort, ok := cs.thinkingParam.(string); ok && effort != "" {
if normalized, okN := util.NormalizeReasoningEffortLevel(model, effort); okN {
return true, normalized, false
}
return false, "", true // invalid level
}
if budget, ok := cs.thinkingParam.(int); ok {
if mapped, okM := util.ThinkingBudgetToEffort(model, budget); okM && mapped != "" {
// Check if the mapped effort is valid for this model
if _, validLevel := util.NormalizeReasoningEffortLevel(model, mapped); !validLevel {
return true, mapped, true // expect validation error
}
return true, mapped, false
}
}
return false, "", false
case "codex":
// Codex does not support allowCompat; require thinking-capable level models.
if !supportsThinking || !usesLevels {
return false, "", false
}
if effort, ok := cs.thinkingParam.(string); ok && effort != "" {
if normalized, okN := util.NormalizeReasoningEffortLevel(model, effort); okN {
return true, normalized, false
}
return false, "", true
}
if budget, ok := cs.thinkingParam.(int); ok {
if mapped, okM := util.ThinkingBudgetToEffort(model, budget); okM && mapped != "" {
// Check if the mapped effort is valid for this model
if _, validLevel := util.NormalizeReasoningEffortLevel(model, mapped); !validLevel {
return true, mapped, true // expect validation error
}
return true, mapped, false
}
}
if from != "openai-response" {
// Codex translators default reasoning.effort to "medium" for thinking-capable models
return true, "medium", false
}
return false, "", false
}
return false, "", false
}()
body, err := buildBodyForProtocolWithRawThinking(t, from, to, model, cs.thinkingParam)
actualPresent, actualValue := func() (bool, string) {
path := ""
switch to {
case "gemini":
path = "generationConfig.thinkingConfig.thinkingBudget"
case "claude":
path = "thinking.budget_tokens"
case "openai":
path = "reasoning_effort"
case "codex":
path = "reasoning.effort"
}
if path == "" {
return false, ""
}
val := gjson.GetBytes(body, path)
if to == "codex" && !val.Exists() {
reasoning := gjson.GetBytes(body, "reasoning")
if reasoning.Exists() {
val = reasoning.Get("effort")
}
}
if !val.Exists() {
return false, ""
}
if val.Type == gjson.Number {
return true, fmt.Sprintf("%d", val.Int())
}
return true, val.String()
}()
t.Logf("from=%s to=%s model=%s param=%v present(expect=%v got=%v) value(expect=%s got=%s) err(expect=%v got=%v) body=%s",
from, to, model, cs.thinkingParam, expectPresent, actualPresent, expectValue, actualValue, expectErr, err != nil, string(body))
if expectErr {
if err == nil {
t.Fatalf("expected validation error but got none, body=%s", string(body))
}
return
}
if err != nil {
t.Fatalf("unexpected error: %v body=%s", err, string(body))
}
if expectPresent != actualPresent {
t.Fatalf("presence mismatch: expect %v got %v body=%s", expectPresent, actualPresent, string(body))
}
if expectPresent && expectValue != actualValue {
t.Fatalf("value mismatch: expect %s got %s body=%s", expectValue, actualValue, string(body))
}
})
}
}
}
}
}
func TestThinkingBudgetToEffort(t *testing.T) {
cleanup := registerCoreModels(t)
defer cleanup()
cases := []struct {
name string
model string
budget int
want string
ok bool
}{
{name: "dynamic-auto", model: "gpt-5", budget: -1, want: "auto", ok: true},
{name: "zero-none", model: "gpt-5", budget: 0, want: "minimal", ok: true},
{name: "low-min", model: "gpt-5", budget: 1, want: "low", ok: true},
{name: "low-max", model: "gpt-5", budget: 1024, want: "low", ok: true},
{name: "medium-min", model: "gpt-5", budget: 1025, want: "medium", ok: true},
{name: "medium-max", model: "gpt-5", budget: 8192, want: "medium", ok: true},
{name: "high-min", model: "gpt-5", budget: 8193, want: "high", ok: true},
{name: "high-max", model: "gpt-5", budget: 24576, want: "high", ok: true},
{name: "over-max-clamps-to-highest", model: "gpt-5", budget: 64000, want: "high", ok: true},
{name: "over-max-xhigh-model", model: "gpt-5.2", budget: 64000, want: "xhigh", ok: true},
{name: "negative-unsupported", model: "gpt-5", budget: -5, want: "", ok: false},
}
for _, cs := range cases {
cs := cs
t.Run(cs.name, func(t *testing.T) {
got, ok := util.ThinkingBudgetToEffort(cs.model, cs.budget)
if ok != cs.ok {
t.Fatalf("ok mismatch for model=%s budget=%d: expect %v got %v", cs.model, cs.budget, cs.ok, ok)
}
if got != cs.want {
t.Fatalf("value mismatch for model=%s budget=%d: expect %q got %q", cs.model, cs.budget, cs.want, got)
}
})
}
}
func TestThinkingEffortToBudget(t *testing.T) {
cleanup := registerCoreModels(t)
defer cleanup()
cases := []struct {
name string
model string
effort string
want int
ok bool
}{
{name: "none", model: "gemini-2.5-pro", effort: "none", want: 0, ok: true},
{name: "auto", model: "gemini-2.5-pro", effort: "auto", want: -1, ok: true},
{name: "minimal", model: "gemini-2.5-pro", effort: "minimal", want: 512, ok: true},
{name: "low", model: "gemini-2.5-pro", effort: "low", want: 1024, ok: true},
{name: "medium", model: "gemini-2.5-pro", effort: "medium", want: 8192, ok: true},
{name: "high", model: "gemini-2.5-pro", effort: "high", want: 24576, ok: true},
{name: "xhigh", model: "gemini-2.5-pro", effort: "xhigh", want: 32768, ok: true},
{name: "empty-unsupported", model: "gemini-2.5-pro", effort: "", want: 0, ok: false},
{name: "invalid-unsupported", model: "gemini-2.5-pro", effort: "ultra", want: 0, ok: false},
{name: "case-insensitive", model: "gemini-2.5-pro", effort: "LOW", want: 1024, ok: true},
{name: "case-insensitive-medium", model: "gemini-2.5-pro", effort: "MEDIUM", want: 8192, ok: true},
}
for _, cs := range cases {
cs := cs
t.Run(cs.name, func(t *testing.T) {
got, ok := util.ThinkingEffortToBudget(cs.model, cs.effort)
if ok != cs.ok {
t.Fatalf("ok mismatch for model=%s effort=%s: expect %v got %v", cs.model, cs.effort, cs.ok, ok)
}
if got != cs.want {
t.Fatalf("value mismatch for model=%s effort=%s: expect %d got %d", cs.model, cs.effort, cs.want, got)
}
})
}
}
-117
View File
@@ -1,117 +0,0 @@
package test
import (
"testing"
_ "github.com/router-for-me/CLIProxyAPI/v6/internal/translator"
sdktranslator "github.com/router-for-me/CLIProxyAPI/v6/sdk/translator"
"github.com/tidwall/gjson"
)
func TestOpenAIToClaude_ToolResultsAsSeparateMessages(t *testing.T) {
// The translator produces separate messages for each tool result.
// Grouping is handled later by NormalizeClaudeToolResults in the executor.
raw := []byte(`{
"model":"gpt-5.2",
"messages":[
{"role":"user","content":"hi"},
{"role":"assistant","tool_calls":[
{"id":"call_a","type":"function","function":{"name":"a","arguments":"{}"}},
{"id":"call_b","type":"function","function":{"name":"b","arguments":"{}"}}
]},
{"role":"tool","tool_call_id":"call_a","content":"outA"},
{"role":"tool","tool_call_id":"call_b","content":"outB"},
{"role":"user","content":"done"}
]
}`)
out := sdktranslator.TranslateRequest(
sdktranslator.FromString("openai"),
sdktranslator.FromString("claude"),
"claude-sonnet-4-5-20250929",
raw,
false,
)
// Translator produces 5 messages: user, assistant (with 2 tool_use), user (tool_result a), user (tool_result b), user (done)
if got := gjson.GetBytes(out, "messages.#").Int(); got != 5 {
t.Fatalf("expected 5 messages, got %d body=%s", got, string(out))
}
if gjson.GetBytes(out, "messages.1.role").String() != "assistant" {
t.Fatalf("expected messages.1.role=assistant body=%s", string(out))
}
if got := gjson.GetBytes(out, "messages.1.content.#").Int(); got != 2 {
t.Fatalf("expected 2 tool_use blocks, got %d body=%s", got, string(out))
}
// Tool results are in separate user messages
if gjson.GetBytes(out, "messages.2.role").String() != "user" {
t.Fatalf("expected messages.2.role=user body=%s", string(out))
}
if gjson.GetBytes(out, "messages.2.content.0.tool_use_id").String() != "call_a" {
t.Fatalf("expected messages.2 tool_use_id=call_a body=%s", string(out))
}
if gjson.GetBytes(out, "messages.3.role").String() != "user" {
t.Fatalf("expected messages.3.role=user body=%s", string(out))
}
if gjson.GetBytes(out, "messages.3.content.0.tool_use_id").String() != "call_b" {
t.Fatalf("expected messages.3 tool_use_id=call_b body=%s", string(out))
}
}
func TestOpenAIResponsesToClaude_CallOutputsAsSeparateMessages(t *testing.T) {
// The translator produces separate messages for each function_call and function_call_output.
// Grouping is handled later by NormalizeClaudeToolResults in the executor.
raw := []byte(`{
"model":"gpt-5.2",
"input":[
{"type":"message","role":"user","content":[{"type":"input_text","text":"hi"}]},
{"type":"function_call","call_id":"call_a","name":"a","arguments":"{}"},
{"type":"function_call","call_id":"call_b","name":"b","arguments":"{}"},
{"type":"function_call_output","call_id":"call_a","output":"outA"},
{"type":"function_call_output","call_id":"call_b","output":"outB"},
{"type":"message","role":"user","content":[{"type":"input_text","text":"done"}]}
]
}`)
out := sdktranslator.TranslateRequest(
sdktranslator.FromString("openai-response"),
sdktranslator.FromString("claude"),
"claude-sonnet-4-5-20250929",
raw,
false,
)
// Translator produces 6 messages: user (hi), assistant (tool_use a), assistant (tool_use b),
// user (tool_result a), user (tool_result b), user (done)
if got := gjson.GetBytes(out, "messages.#").Int(); got != 6 {
t.Fatalf("expected 6 messages, got %d body=%s", got, string(out))
}
// First message is user "hi"
if gjson.GetBytes(out, "messages.0.role").String() != "user" {
t.Fatalf("expected messages.0.role=user body=%s", string(out))
}
// Function calls become separate assistant messages with tool_use
if gjson.GetBytes(out, "messages.1.role").String() != "assistant" {
t.Fatalf("expected messages.1.role=assistant body=%s", string(out))
}
if gjson.GetBytes(out, "messages.1.content.0.type").String() != "tool_use" {
t.Fatalf("expected messages.1.content.0.type=tool_use body=%s", string(out))
}
if gjson.GetBytes(out, "messages.2.role").String() != "assistant" {
t.Fatalf("expected messages.2.role=assistant body=%s", string(out))
}
// Function call outputs become separate user messages with tool_result
if gjson.GetBytes(out, "messages.3.role").String() != "user" {
t.Fatalf("expected messages.3.role=user body=%s", string(out))
}
if gjson.GetBytes(out, "messages.3.content.0.tool_use_id").String() != "call_a" {
t.Fatalf("expected messages.3 tool_use_id=call_a body=%s", string(out))
}
if gjson.GetBytes(out, "messages.4.role").String() != "user" {
t.Fatalf("expected messages.4.role=user body=%s", string(out))
}
if gjson.GetBytes(out, "messages.4.content.0.tool_use_id").String() != "call_b" {
t.Fatalf("expected messages.4 tool_use_id=call_b body=%s", string(out))
}
}
+6
View File
@@ -4,7 +4,13 @@
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="pp-mgmt-key" content="test123" />
<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">
</head>
<body>
<div id="root"></div>
+10 -64
View File
@@ -20,7 +20,7 @@
"lucide-react": "^0.562.0",
"react": "^19.2.0",
"react-dom": "^19.2.0",
"react-grab": "^0.0.91",
"sonner": "^2.0.7",
"tailwind-merge": "^3.4.0"
},
"devDependencies": {
@@ -2486,6 +2486,7 @@
"version": "19.2.7",
"resolved": "https://registry.npmjs.org/@types/react/-/react-19.2.7.tgz",
"integrity": "sha512-MWtvHrGZLFttgeEj28VXHxpmwYbor/ATPYbBfSFZEIRK0ecCFLl2Qo55z52Hss+UV9CRN7trSeq1zbgx7YDWWg==",
"devOptional": true,
"license": "MIT",
"dependencies": {
"csstype": "^3.2.2"
@@ -2501,15 +2502,6 @@
"@types/react": "^19.2.0"
}
},
"node_modules/@types/react-reconciler": {
"version": "0.28.9",
"resolved": "https://registry.npmjs.org/@types/react-reconciler/-/react-reconciler-0.28.9.tgz",
"integrity": "sha512-HHM3nxyUZ3zAylX8ZEyrDNd2XZOnQ0D5XfunJF5FLQnZbHHYq4UWvW1QfelQNXv1ICNkwYhfxjwfnqivYB6bFg==",
"license": "MIT",
"peerDependencies": {
"@types/react": "*"
}
},
"node_modules/@typescript-eslint/eslint-plugin": {
"version": "8.50.0",
"resolved": "https://registry.npmjs.org/@typescript-eslint/eslint-plugin/-/eslint-plugin-8.50.0.tgz",
@@ -2880,18 +2872,6 @@
"baseline-browser-mapping": "dist/cli.js"
}
},
"node_modules/bippy": {
"version": "0.5.26",
"resolved": "https://registry.npmjs.org/bippy/-/bippy-0.5.26.tgz",
"integrity": "sha512-pOy5vxOX2gXo0L7lIwo5H9mqvrgAE5mCZgrPOli5SaxxXx8VnPHi1vxDr5nancX5F6rYqmigMpAalPrM2I3nCA==",
"license": "MIT",
"dependencies": {
"@types/react-reconciler": "^0.28.9"
},
"peerDependencies": {
"react": ">=17.0.1"
}
},
"node_modules/brace-expansion": {
"version": "1.1.12",
"resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.12.tgz",
@@ -3059,6 +3039,7 @@
"version": "3.2.3",
"resolved": "https://registry.npmjs.org/csstype/-/csstype-3.2.3.tgz",
"integrity": "sha512-z1HGKcYy2xA8AGQfwrn0PAy+PB7X/GSj3UVJW9qKyn43xWa+gl5nXmU4qqLMRzWVLFC8KusUX8T/0kCiOYpAIQ==",
"devOptional": true,
"license": "MIT"
},
"node_modules/debug": {
@@ -4248,19 +4229,6 @@
"react": "^19.2.3"
}
},
"node_modules/react-grab": {
"version": "0.0.91",
"resolved": "https://registry.npmjs.org/react-grab/-/react-grab-0.0.91.tgz",
"integrity": "sha512-X1FFkbstgYTegSjsi4xQUX9JI7m1Ou/ljApgSf3vSEN4uXv8lGsewMiF48Y5o2RPnoPUYmiz89PFLU8TkL9ivQ==",
"license": "MIT",
"dependencies": {
"bippy": "^0.5.26",
"solid-js": "^1.9.10"
},
"bin": {
"react-grab": "dist/cli.cjs"
}
},
"node_modules/react-refresh": {
"version": "0.18.0",
"resolved": "https://registry.npmjs.org/react-refresh/-/react-refresh-0.18.0.tgz",
@@ -4339,27 +4307,6 @@
"semver": "bin/semver.js"
}
},
"node_modules/seroval": {
"version": "1.3.2",
"resolved": "https://registry.npmjs.org/seroval/-/seroval-1.3.2.tgz",
"integrity": "sha512-RbcPH1n5cfwKrru7v7+zrZvjLurgHhGyso3HTyGtRivGWgYjbOmGuivCQaORNELjNONoK35nj28EoWul9sb1zQ==",
"license": "MIT",
"engines": {
"node": ">=10"
}
},
"node_modules/seroval-plugins": {
"version": "1.3.3",
"resolved": "https://registry.npmjs.org/seroval-plugins/-/seroval-plugins-1.3.3.tgz",
"integrity": "sha512-16OL3NnUBw8JG1jBLUoZJsLnQq0n5Ua6aHalhJK4fMQkz1lqR7Osz1sA30trBtd9VUDc2NgkuRCn8+/pBwqZ+w==",
"license": "MIT",
"engines": {
"node": ">=10"
},
"peerDependencies": {
"seroval": "^1.0"
}
},
"node_modules/shebang-command": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/shebang-command/-/shebang-command-2.0.0.tgz",
@@ -4383,15 +4330,14 @@
"node": ">=8"
}
},
"node_modules/solid-js": {
"version": "1.9.10",
"resolved": "https://registry.npmjs.org/solid-js/-/solid-js-1.9.10.tgz",
"integrity": "sha512-Coz956cos/EPDlhs6+jsdTxKuJDPT7B5SVIWgABwROyxjY7Xbr8wkzD68Et+NxnV7DLJ3nJdAC2r9InuV/4Jew==",
"node_modules/sonner": {
"version": "2.0.7",
"resolved": "https://registry.npmjs.org/sonner/-/sonner-2.0.7.tgz",
"integrity": "sha512-W6ZN4p58k8aDKA4XPcx2hpIQXBRAgyiWVkYhT7CvK6D3iAu7xjvVyhQHg2/iaKJZ1XVJ4r7XuwGL+WGEK37i9w==",
"license": "MIT",
"dependencies": {
"csstype": "^3.1.0",
"seroval": "~1.3.0",
"seroval-plugins": "~1.3.0"
"peerDependencies": {
"react": "^18.0.0 || ^19.0.0 || ^19.0.0-rc",
"react-dom": "^18.0.0 || ^19.0.0 || ^19.0.0-rc"
}
},
"node_modules/source-map-js": {
+1 -1
View File
@@ -22,7 +22,7 @@
"lucide-react": "^0.562.0",
"react": "^19.2.0",
"react-dom": "^19.2.0",
"react-grab": "^0.0.91",
"sonner": "^2.0.7",
"tailwind-merge": "^3.4.0"
},
"devDependencies": {
Binary file not shown.

After

Width:  |  Height:  |  Size: 194 KiB

+195 -1336
View File
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,267 @@
import { useEffect, useState, useCallback } from 'react'
import { Tooltip, TooltipContent, TooltipTrigger } from '@/components/ui/tooltip'
import { Terminal, Check, AlertTriangle, Copy, Loader2, Wrench, RefreshCw, Info, Undo2 } from 'lucide-react'
import { useProxyContext, EngineOfflineError } from '@/hooks/useProxyContext'
interface Agent {
id: string
name: string
detected: boolean
configured: boolean
configInstructions?: string
envVars?: Record<string, string>
canAutoConfigure?: boolean
}
export function AgentSetup() {
const { mgmtFetch, showToast, status, isMgmtLoading, pp_detect_agents, pp_configure_agent, pp_unconfigure_agent, isDesktop } = useProxyContext()
const [agents, setAgents] = useState<Agent[]>([])
const [loading, setLoading] = useState(false)
const [configuring, setConfiguring] = useState<string | null>(null)
const [unconfiguring, setUnconfiguring] = useState<string | null>(null)
const isRunning = status?.running ?? false
const fetchAgents = useCallback(async () => {
if (!isRunning) return
setLoading(true)
try {
let data: Agent[] = []
if (isDesktop && pp_detect_agents) {
data = await pp_detect_agents()
} else {
const res = await mgmtFetch('/v0/management/agents')
data = res.agents || []
}
setAgents(data)
} catch (e) {
if (!(e instanceof EngineOfflineError)) {
console.error('Failed to fetch agents:', e)
}
}
setLoading(false)
}, [mgmtFetch, isRunning, isDesktop, pp_detect_agents])
useEffect(() => {
if (isRunning) {
fetchAgents()
} else {
setAgents([])
}
}, [isRunning, fetchAgents])
const copyEnvVars = (envVars: Record<string, string>) => {
const text = Object.entries(envVars)
.map(([key, value]) => `${key}=${value}`)
.join('\n')
navigator.clipboard.writeText(text)
showToast('Environment variables copied to clipboard', 'success')
}
// Agents that use shell profile (need restart) vs config files (immediate)
const needsShellRestart = (id: string) => ['claude-code', 'aider', 'gemini-cli'].includes(id)
const handleConfigure = async (agent: Agent) => {
if (!agent.id) return
setConfiguring(agent.id)
try {
if (isDesktop && pp_configure_agent) {
await pp_configure_agent(agent.id)
} else {
await mgmtFetch(`/v0/management/agents/${agent.id}/configure`, { method: 'POST' })
}
const msg = needsShellRestart(agent.id)
? `${agent.name} configured! Restart shell to apply.`
: `${agent.name} configured!`
showToast(msg, 'success')
await fetchAgents()
} catch (e) {
showToast(e instanceof Error ? e.message : String(e), 'error')
}
setConfiguring(null)
}
const handleUnconfigure = async (agent: Agent) => {
if (!agent.id) return
setUnconfiguring(agent.id)
try {
if (isDesktop && pp_unconfigure_agent) {
await pp_unconfigure_agent(agent.id)
} else {
await mgmtFetch(`/v0/management/agents/${agent.id}/unconfigure`, { method: 'POST' })
}
const msg = needsShellRestart(agent.id)
? `${agent.name} configuration removed. Restart shell to apply.`
: `${agent.name} configuration removed.`
showToast(msg, 'success')
await fetchAgents()
} catch (e) {
showToast(e instanceof Error ? e.message : String(e), 'error')
}
setUnconfiguring(null)
}
return (
<div className="rounded-lg border border-[var(--border-default)] bg-[var(--bg-panel)] shadow-xl overflow-hidden">
{/* Header */}
<div className="flex items-center gap-2 px-4 py-3 bg-[var(--bg-elevated)] border-b border-[var(--border-default)]">
<Terminal className={`h-4 w-4 text-[var(--accent-primary)] ${loading ? 'animate-pulse' : ''}`} />
<span className="font-mono text-xs font-bold uppercase tracking-widest text-[var(--text-secondary)]">
CLI Agent Detection
</span>
{isMgmtLoading && <Loader2 className="h-3 w-3 animate-spin text-muted-foreground" />}
<div className="flex-1" />
<button
onClick={fetchAgents}
disabled={loading || !isRunning}
className="p-1.5 rounded hover:bg-[var(--bg-void)] text-[var(--text-muted)] hover:text-[var(--accent-primary)] transition-colors disabled:opacity-50"
>
<RefreshCw className={`h-3.5 w-3.5 ${loading ? 'animate-spin' : ''}`} />
</button>
</div>
<div className="p-4">
{!isRunning ? (
<div className="flex flex-col items-center justify-center py-8 text-center">
<div className="font-mono text-sm text-[var(--text-muted)] uppercase tracking-wider mb-1">
Engine Offline
</div>
<div className="text-xs text-[var(--text-muted)]">
Start the proxy engine to detect agents
</div>
</div>
) : agents.length === 0 && !loading ? (
<div className="text-center py-8 text-[var(--text-muted)] font-mono text-xs uppercase">
No agents detected
</div>
) : (
<div className="grid gap-3">
{agents.map((agent) => (
<div
key={agent.name}
className={`
flex items-center gap-4 p-3 rounded border transition-all
${agent.detected
? 'bg-[var(--bg-elevated)]/50 border-[var(--border-default)]'
: 'bg-[var(--bg-void)]/30 border-[var(--border-subtle)] opacity-60'
}
`}
>
{/* Status Icon */}
<div className="shrink-0">
{agent.configured ? (
<div className="p-1.5 rounded-full bg-[var(--status-online)]/10 text-[var(--status-online)]">
<Check className="h-4 w-4" />
</div>
) : agent.detected ? (
<div className="p-1.5 rounded-full bg-[var(--status-warning)]/10 text-[var(--status-warning)]">
<AlertTriangle className="h-4 w-4" />
</div>
) : (
<div className="p-1.5 rounded-full bg-[var(--bg-void)] text-[var(--text-muted)]">
<Terminal className="h-4 w-4" />
</div>
)}
</div>
{/* Info */}
<div className="flex-1 min-w-0">
<div className="flex items-center gap-2">
<span className="font-mono text-sm font-bold text-[var(--text-primary)]">
{agent.name}
</span>
{agent.detected && !agent.configured && (
<span className="text-[10px] font-mono uppercase px-1.5 py-0.5 rounded bg-[var(--status-warning)]/10 text-[var(--status-warning)] border border-[var(--status-warning)]/20">
Needs Config
</span>
)}
</div>
<div className="text-[10px] font-mono text-[var(--text-muted)] truncate">
{agent.detected
? (agent.configured ? 'Fully configured and ready' : 'Detected but not configured')
: 'Not found on this system'
}
</div>
</div>
{/* Actions */}
<div className="flex items-center gap-2">
{/* Auto-Configure Button */}
{agent.detected && !agent.configured && agent.canAutoConfigure && (
<Tooltip>
<TooltipTrigger asChild>
<button
onClick={() => handleConfigure(agent)}
disabled={configuring === agent.id}
className="flex items-center gap-1.5 px-2.5 py-1.5 rounded border border-[var(--accent-primary)] bg-[var(--accent-primary)]/10 text-[var(--accent-primary)] hover:bg-[var(--accent-primary)]/20 transition-all disabled:opacity-50 font-mono text-xs uppercase tracking-wider"
>
{configuring === agent.id ? (
<Loader2 className="h-3.5 w-3.5 animate-spin" />
) : (
<Wrench className="h-3.5 w-3.5" />
)}
Configure
</button>
</TooltipTrigger>
<TooltipContent>Auto-configure {agent.name} for ProxyPilot</TooltipContent>
</Tooltip>
)}
{/* Unconfigure Button */}
{agent.detected && agent.configured && (
<Tooltip>
<TooltipTrigger asChild>
<button
onClick={() => handleUnconfigure(agent)}
disabled={unconfiguring === agent.id}
className="flex items-center gap-1.5 px-2.5 py-1.5 rounded border border-[var(--status-error)] bg-[var(--status-error)]/10 text-[var(--status-error)] hover:bg-[var(--status-error)]/20 transition-all disabled:opacity-50 font-mono text-xs uppercase tracking-wider"
>
{unconfiguring === agent.id ? (
<Loader2 className="h-3.5 w-3.5 animate-spin" />
) : (
<Undo2 className="h-3.5 w-3.5" />
)}
Reset
</button>
</TooltipTrigger>
<TooltipContent>Remove ProxyPilot configuration from {agent.name}</TooltipContent>
</Tooltip>
)}
{agent.envVars && (
<Tooltip>
<TooltipTrigger asChild>
<button
onClick={() => copyEnvVars(agent.envVars!)}
className="p-2 rounded border border-[var(--border-default)] bg-[var(--bg-panel)] text-[var(--text-secondary)] hover:border-[var(--accent-primary)] hover:text-[var(--accent-primary)] transition-all"
>
<Copy className="h-3.5 w-3.5" />
</button>
</TooltipTrigger>
<TooltipContent>Copy Environment Variables</TooltipContent>
</Tooltip>
)}
{agent.configInstructions && (
<Tooltip>
<TooltipTrigger asChild>
<button
onClick={() => {
showToast(agent.configInstructions!, 'success')
}}
className="p-2 rounded border border-[var(--border-default)] bg-[var(--bg-panel)] text-[var(--text-secondary)] hover:border-[var(--accent-primary)] hover:text-[var(--accent-primary)] transition-all"
>
<Info className="h-3.5 w-3.5" />
</button>
</TooltipTrigger>
<TooltipContent>View Setup Instructions</TooltipContent>
</Tooltip>
)}
</div>
</div>
))}
</div>
)}
</div>
</div>
)
}
@@ -0,0 +1,113 @@
import { useState, useEffect } from 'react'
import { useProxyContext, EngineOfflineError } from '@/hooks/useProxyContext'
import { Card, CardContent, CardHeader, CardTitle, CardDescription } from '@/components/ui/card'
import { Button } from '@/components/ui/button'
import { Save, RotateCcw, Loader2, FileCode } from 'lucide-react'
export function ConfigEditor() {
const { mgmtFetch, showToast, status, isMgmtLoading } = useProxyContext()
const [configYaml, setConfigYaml] = useState('')
const [isSaving, setIsSaving] = useState(false)
const isRunning = status?.running ?? false
const fetchConfig = async () => {
try {
const data = await mgmtFetch('/v0/management/config.yaml')
setConfigYaml(typeof data === 'string' ? data : JSON.stringify(data, null, 2))
} catch (e) {
if (!(e instanceof EngineOfflineError)) {
showToast(e instanceof Error ? e.message : String(e), 'error')
}
}
}
const saveConfig = async () => {
setIsSaving(true)
try {
await mgmtFetch('/v0/management/config.yaml', {
method: 'PUT',
headers: { 'Content-Type': 'application/yaml' },
body: configYaml,
})
showToast('Configuration saved successfully', 'success')
} catch (e) {
showToast(e instanceof Error ? e.message : String(e), 'error')
} finally {
setIsSaving(false)
}
}
useEffect(() => {
if (isRunning) {
fetchConfig()
}
}, [isRunning])
return (
<Card className="backdrop-blur-sm bg-card/60 border-border/50 shadow-xl overflow-hidden">
<CardHeader className="pb-4 border-b border-border/50 bg-gradient-to-r from-purple-500/5 to-transparent">
<div className="flex items-center justify-between">
<div className="flex items-center gap-3">
<div className="h-10 w-10 rounded-xl bg-purple-500/10 flex items-center justify-center">
<FileCode className="h-5 w-5 text-purple-500" />
</div>
<div>
<CardTitle className="text-lg">Config Editor</CardTitle>
<CardDescription>Edit config.yaml directly</CardDescription>
</div>
</div>
<div className="flex items-center gap-2">
<Button
size="sm"
variant="outline"
onClick={fetchConfig}
disabled={!isRunning || isMgmtLoading}
className="gap-1.5 text-xs"
>
<RotateCcw className="h-3.5 w-3.5" />
Reset
</Button>
<Button
size="sm"
onClick={saveConfig}
disabled={!isRunning || isSaving || isMgmtLoading}
className="gap-1.5 text-xs bg-purple-600 hover:bg-purple-700 text-white"
>
{isSaving ? <Loader2 className="h-3.5 w-3.5 animate-spin" /> : <Save className="h-3.5 w-3.5" />}
Save Changes
</Button>
</div>
</div>
</CardHeader>
<CardContent className="p-0">
{!isRunning ? (
<div className="py-12 text-center text-muted-foreground">
<p className="text-sm uppercase tracking-widest font-mono">⚠️ Engine Offline</p>
<p className="text-xs mt-1">Start the proxy engine to edit configuration</p>
</div>
) : (
<div className="relative">
<textarea
value={configYaml}
onChange={(e) => setConfigYaml(e.target.value)}
spellCheck={false}
className="w-full h-[500px] p-6 bg-[var(--bg-void)] text-[var(--text-primary)] font-mono text-sm resize-none focus:outline-none selection:bg-purple-500/30"
style={{
fontFamily: '"JetBrains Mono", "Fira Code", monospace',
lineHeight: '1.6',
}}
/>
{/* Decorative grid background for the editor */}
<div
className="absolute inset-0 pointer-events-none opacity-[0.03]"
style={{
backgroundImage: `linear-gradient(var(--border-subtle) 1px, transparent 1px), linear-gradient(90deg, var(--border-subtle) 1px, transparent 1px)`,
backgroundSize: '20px 20px'
}}
/>
</div>
)}
</CardContent>
</Card>
)
}
@@ -0,0 +1,399 @@
import { useState, useEffect, useRef } from "react";
import { Tooltip, TooltipContent, TooltipTrigger } from "@/components/ui/tooltip";
import {
Play,
Square,
RotateCw,
FolderOpen,
FileText,
Copy,
Activity,
Check,
AlertTriangle,
Radio,
} from "lucide-react";
import { useProxyContext } from "@/hooks/useProxyContext";
function formatUptime(seconds: number): string {
const h = Math.floor(seconds / 3600);
const m = Math.floor((seconds % 3600) / 60);
const s = seconds % 60;
if (h > 0) {
return `${h}h ${m}m`;
}
if (m > 0) {
return `${m}m ${s}s`;
}
return `${s}s`;
}
export function EngineControl() {
const { status, loading, handleAction, showToast, isDesktop } = useProxyContext();
const [uptime, setUptime] = useState(0);
const [copied, setCopied] = useState(false);
const startTimeRef = useRef<number | null>(null);
const isRunning = status?.running ?? false;
// Track uptime
useEffect(() => {
if (isRunning) {
if (startTimeRef.current === null) {
startTimeRef.current = Date.now();
}
const interval = setInterval(() => {
if (startTimeRef.current) {
setUptime(Math.floor((Date.now() - startTimeRef.current) / 1000));
}
}, 1000);
return () => clearInterval(interval);
}
startTimeRef.current = null;
setUptime(0);
}, [isRunning]);
const handleCopyUrl = async () => {
if (status?.base_url) {
try {
await navigator.clipboard.writeText(status.base_url);
setCopied(true);
showToast("URL copied to clipboard", "success");
setTimeout(() => setCopied(false), 2000);
} catch {
showToast("Failed to copy URL", "error");
}
}
};
return (
<div className="rounded-lg border border-[var(--border-default)] bg-[var(--bg-panel)] shadow-xl overflow-hidden">
{/* Flight Deck Header */}
<div className="flex items-center gap-2 px-4 py-3 bg-[var(--bg-elevated)] border-b border-[var(--border-default)]">
<Radio className="h-4 w-4 text-[var(--accent-primary)]" />
<span className="font-mono text-xs font-bold uppercase tracking-widest text-[var(--text-secondary)]">
Engine Control
</span>
<div className="flex-1" />
<div className={`h-2 w-2 rounded-full ${isRunning ? "bg-[var(--status-online)] animate-pulse" : "bg-[var(--status-offline)]"}`} />
</div>
{/* Radar Display Area */}
<div className="p-6 flex flex-col items-center">
{/* Radar Container */}
<div className="relative w-48 h-48 mb-6">
{/* Outer pulse rings - only visible when running */}
{isRunning && (
<>
<div
className="absolute inset-0 rounded-full border-2 border-[var(--status-online)] opacity-0"
style={{
animation: "radar-pulse 2s ease-out infinite",
}}
/>
<div
className="absolute inset-0 rounded-full border-2 border-[var(--status-online)] opacity-0"
style={{
animation: "radar-pulse 2s ease-out infinite 0.5s",
}}
/>
<div
className="absolute inset-0 rounded-full border-2 border-[var(--status-online)] opacity-0"
style={{
animation: "radar-pulse 2s ease-out infinite 1s",
}}
/>
</>
)}
{/* Static outer ring */}
<div
className={`absolute inset-4 rounded-full border-2 transition-colors duration-300 ${
isRunning
? "border-[var(--status-online)]/30"
: "border-[var(--border-subtle)]"
}`}
/>
{/* Middle ring with tick marks */}
<div
className={`absolute inset-8 rounded-full border transition-colors duration-300 ${
isRunning
? "border-[var(--status-online)]/20"
: "border-[var(--border-subtle)]/50"
}`}
/>
{/* Inner radar circle - main status display */}
<div
className={`absolute inset-12 rounded-full flex flex-col items-center justify-center transition-all duration-500 ${
isRunning
? "bg-gradient-to-br from-[var(--accent-primary)] to-[var(--accent-secondary)] shadow-[0_0_30px_var(--status-online)]"
: "bg-gradient-to-br from-[var(--bg-elevated)] to-[var(--bg-active)] border border-[var(--border-default)]"
}`}
>
{/* Status text */}
<span
className={`font-mono text-xs font-bold tracking-wider ${
isRunning
? "text-white"
: "text-[var(--text-muted)]"
}`}
>
{isRunning ? "ONLINE" : "OFFLINE"}
</span>
</div>
{/* Rotating sweep line - only when running */}
{isRunning && (
<div
className="absolute inset-0 rounded-full overflow-hidden"
style={{
animation: "radar-sweep 3s linear infinite",
}}
>
<div
className="absolute top-1/2 left-1/2 w-1/2 h-0.5 origin-left"
style={{
background: "linear-gradient(90deg, var(--status-online) 0%, transparent 100%)",
}}
/>
</div>
)}
</div>
{/* Instrument Readouts */}
<div className="w-full grid grid-cols-2 gap-3 mb-6">
{/* Port Readout */}
<div className="bg-[var(--bg-void)] rounded-md border border-[var(--border-subtle)] p-3">
<div className="font-mono text-[10px] uppercase tracking-wider text-[var(--text-muted)] mb-1">
Port
</div>
<div className={`font-mono text-lg font-bold ${isRunning ? "text-[var(--accent-glow)]" : "text-[var(--text-muted)]"}`}>
{isRunning ? status?.port || "---" : "---"}
</div>
</div>
{/* Uptime Readout */}
<div className="bg-[var(--bg-void)] rounded-md border border-[var(--border-subtle)] p-3">
<div className="font-mono text-[10px] uppercase tracking-wider text-[var(--text-muted)] mb-1">
Uptime
</div>
<div className={`font-mono text-lg font-bold ${isRunning ? "text-[var(--accent-glow)]" : "text-[var(--text-muted)]"}`}>
{isRunning ? formatUptime(uptime) : "--:--"}
</div>
</div>
</div>
{/* Error Display - Warning Panel */}
{status?.last_error && (
<div className="w-full mb-6 rounded-md border-2 border-[var(--status-warning)] bg-[var(--status-warning)]/10 p-4">
<div className="flex items-start gap-3">
<div className="p-1.5 rounded bg-[var(--status-warning)]/20">
<AlertTriangle className="h-4 w-4 text-[var(--status-warning)]" />
</div>
<div className="flex-1 min-w-0">
<div className="font-mono text-[10px] uppercase tracking-wider text-[var(--status-warning)] mb-1">
Caution
</div>
<div className="font-mono text-sm text-[var(--status-warning)] break-all">
{status.last_error}
</div>
</div>
</div>
</div>
)}
{/* Cockpit Control Buttons */}
<div className="w-full grid grid-cols-3 gap-3 mb-6">
{/* START Button */}
<Tooltip>
<TooltipTrigger asChild>
<button
type="button"
disabled={isRunning || loading === "start"}
onClick={() => handleAction(window.pp_start, "start", "Proxy started")}
className={`
relative flex flex-col items-center justify-center gap-1 py-4 px-3 rounded-lg
font-mono text-xs font-bold uppercase tracking-wider
border-2 transition-all duration-150
${isRunning || loading === "start"
? "opacity-40 cursor-not-allowed border-[var(--border-subtle)] bg-[var(--bg-elevated)] text-[var(--text-muted)]"
: "border-[var(--status-online)] bg-gradient-to-b from-[var(--bg-elevated)] to-[var(--bg-panel)] text-[var(--status-online)] hover:shadow-[0_0_15px_var(--status-online)] active:translate-y-0.5 active:shadow-none cursor-pointer"
}
shadow-[inset_0_1px_0_rgba(255,255,255,0.1),0_2px_4px_rgba(0,0,0,0.3)]
`}
>
<Play className="h-5 w-5 fill-current" />
<span>Start</span>
</button>
</TooltipTrigger>
<TooltipContent>Start the proxy server</TooltipContent>
</Tooltip>
{/* STOP Button */}
<Tooltip>
<TooltipTrigger asChild>
<button
type="button"
disabled={!isRunning || loading === "stop"}
onClick={() => handleAction(window.pp_stop, "stop", "Proxy stopped")}
className={`
relative flex flex-col items-center justify-center gap-1 py-4 px-3 rounded-lg
font-mono text-xs font-bold uppercase tracking-wider
border-2 transition-all duration-150
${!isRunning || loading === "stop"
? "opacity-40 cursor-not-allowed border-[var(--border-subtle)] bg-[var(--bg-elevated)] text-[var(--text-muted)]"
: "border-[var(--status-offline)] bg-gradient-to-b from-[var(--bg-elevated)] to-[var(--bg-panel)] text-[var(--status-offline)] hover:shadow-[0_0_15px_var(--status-offline)] active:translate-y-0.5 active:shadow-none cursor-pointer"
}
shadow-[inset_0_1px_0_rgba(255,255,255,0.1),0_2px_4px_rgba(0,0,0,0.3)]
`}
>
<Square className="h-5 w-5 fill-current" />
<span>Stop</span>
</button>
</TooltipTrigger>
<TooltipContent>Stop the proxy server</TooltipContent>
</Tooltip>
{/* RESTART Button */}
<Tooltip>
<TooltipTrigger asChild>
<button
type="button"
disabled={!isRunning || loading === "restart"}
onClick={() => handleAction(window.pp_restart, "restart", "Proxy restarted")}
className={`
relative flex flex-col items-center justify-center gap-1 py-4 px-3 rounded-lg
font-mono text-xs font-bold uppercase tracking-wider
border-2 transition-all duration-150
${!isRunning || loading === "restart"
? "opacity-40 cursor-not-allowed border-[var(--border-subtle)] bg-[var(--bg-elevated)] text-[var(--text-muted)]"
: "border-[var(--accent-primary)] bg-gradient-to-b from-[var(--bg-elevated)] to-[var(--bg-panel)] text-[var(--accent-primary)] hover:shadow-[0_0_15px_var(--accent-primary)] active:translate-y-0.5 active:shadow-none cursor-pointer"
}
shadow-[inset_0_1px_0_rgba(255,255,255,0.1),0_2px_4px_rgba(0,0,0,0.3)]
`}
>
<RotateCw className={`h-5 w-5 ${loading === "restart" ? "animate-spin" : ""}`} />
<span>Restart</span>
</button>
</TooltipTrigger>
<TooltipContent>Restart the proxy server</TooltipContent>
</Tooltip>
</div>
{/* Base URL Display */}
{status?.base_url && (
<div className="w-full bg-[var(--bg-void)] rounded-md border border-[var(--border-subtle)] p-3">
<div className="flex items-center justify-between gap-3">
<div className="flex-1 min-w-0">
<div className="font-mono text-[10px] uppercase tracking-wider text-[var(--text-muted)] mb-1">
Base URL
</div>
<div className="font-mono text-sm text-[var(--text-accent)] truncate">
{status.base_url}
</div>
</div>
<button
type="button"
onClick={handleCopyUrl}
className={`
flex items-center gap-1.5 px-3 py-1.5 rounded
font-mono text-xs uppercase tracking-wider
border transition-all duration-150
${copied
? "border-[var(--status-online)] bg-[var(--status-online)]/10 text-[var(--status-online)]"
: "border-[var(--border-default)] bg-[var(--bg-elevated)] text-[var(--text-secondary)] hover:border-[var(--accent-primary)] hover:text-[var(--accent-primary)]"
}
active:translate-y-0.5 cursor-pointer
`}
>
{copied ? <Check className="h-3.5 w-3.5" /> : <Copy className="h-3.5 w-3.5" />}
{copied ? "Copied" : "Copy"}
</button>
</div>
</div>
)}
{/* Quick Actions - Desktop Only */}
{isDesktop && (
<div className="w-full mt-4 pt-4 border-t border-[var(--border-subtle)]">
<div className="font-mono text-[10px] uppercase tracking-wider text-[var(--text-muted)] mb-3">
Quick Actions
</div>
<div className="flex flex-wrap gap-2">
<button
type="button"
onClick={() => handleAction(window.pp_open_logs, "logs", "Opened logs")}
className="flex items-center gap-1.5 px-3 py-1.5 rounded border border-[var(--border-default)] bg-[var(--bg-elevated)] text-[var(--text-secondary)] font-mono text-xs hover:border-[var(--accent-primary)] hover:text-[var(--accent-primary)] transition-colors active:translate-y-0.5 cursor-pointer"
>
<FileText className="h-3.5 w-3.5" />
Logs
</button>
<button
type="button"
onClick={() => handleAction(window.pp_open_auth_folder, "auth", "Opened auth folder")}
className="flex items-center gap-1.5 px-3 py-1.5 rounded border border-[var(--border-default)] bg-[var(--bg-elevated)] text-[var(--text-secondary)] font-mono text-xs hover:border-[var(--accent-primary)] hover:text-[var(--accent-primary)] transition-colors active:translate-y-0.5 cursor-pointer"
>
<FolderOpen className="h-3.5 w-3.5" />
Auth
</button>
<button
type="button"
disabled={!isRunning}
onClick={() => handleAction(window.pp_open_diagnostics, "diag-open", "Opened diagnostics")}
className={`flex items-center gap-1.5 px-3 py-1.5 rounded border font-mono text-xs transition-colors active:translate-y-0.5 cursor-pointer ${
!isRunning
? "opacity-40 cursor-not-allowed border-[var(--border-subtle)] bg-[var(--bg-elevated)] text-[var(--text-muted)]"
: "border-[var(--border-default)] bg-[var(--bg-elevated)] text-[var(--text-secondary)] hover:border-[var(--accent-primary)] hover:text-[var(--accent-primary)]"
}`}
>
<Activity className="h-3.5 w-3.5" />
Diagnostics
</button>
</div>
</div>
)}
{/* Non-desktop Copy URL */}
{!isDesktop && !status?.base_url && (
<div className="w-full mt-4 pt-4 border-t border-[var(--border-subtle)]">
<button
type="button"
onClick={handleCopyUrl}
disabled={!status?.base_url}
className="flex items-center gap-1.5 px-3 py-1.5 rounded border border-[var(--border-default)] bg-[var(--bg-elevated)] text-[var(--text-secondary)] font-mono text-xs hover:border-[var(--accent-primary)] hover:text-[var(--accent-primary)] transition-colors disabled:opacity-40 disabled:cursor-not-allowed active:translate-y-0.5 cursor-pointer"
>
{copied ? <Check className="h-3.5 w-3.5" /> : <Copy className="h-3.5 w-3.5" />}
{copied ? "Copied" : "Copy URL"}
</button>
</div>
)}
</div>
{/* CSS for radar animations */}
<style>{`
@keyframes radar-pulse {
0% {
transform: scale(0.8);
opacity: 0.8;
}
100% {
transform: scale(1.5);
opacity: 0;
}
}
@keyframes radar-sweep {
0% {
transform: rotate(0deg);
}
100% {
transform: rotate(360deg);
}
}
`}</style>
</div>
);
}
@@ -0,0 +1,406 @@
import { useEffect, useState, useCallback } from 'react'
import { Tooltip, TooltipContent, TooltipTrigger } from '@/components/ui/tooltip'
import { Radar, Settings, Download, ExternalLink, Check, AlertCircle, Loader2 } from 'lucide-react'
import { useProxyContext, EngineOfflineError } from '@/hooks/useProxyContext'
import { AgentSetup } from './AgentSetup'
interface Integration {
id: string
name?: string
detected: boolean
binary_path?: string
config_path?: string
}
export function Integrations() {
const { mgmtKey, mgmtFetch, showToast, status, isMgmtLoading } = useProxyContext()
const [integrations, setIntegrations] = useState<Integration[]>([])
const [loading, setLoading] = useState(false)
const [configuring, setConfiguring] = useState<string | null>(null)
const [scanKey, setScanKey] = useState(0)
const isRunning = status?.running ?? false
const fetchIntegrations = useCallback(async () => {
if (!mgmtKey || !isRunning) return
setLoading(true)
setScanKey(prev => prev + 1)
try {
const data = await mgmtFetch('/v0/management/integrations/status')
setIntegrations(data.integrations || [])
} catch (e) {
if (!(e instanceof EngineOfflineError)) {
console.error('Failed to fetch integrations:', e)
}
}
setLoading(false)
}, [mgmtKey, mgmtFetch, isRunning])
const configureIntegration = async (integrationId: string) => {
setConfiguring(integrationId)
try {
await mgmtFetch(`/v0/management/integrations/${integrationId}/configure`, {
method: 'POST',
})
await fetchIntegrations()
showToast(`Configured ${integrationId}`, 'success')
} catch (e) {
showToast(e instanceof Error ? e.message : String(e), 'error')
}
setConfiguring(null)
}
useEffect(() => {
if (mgmtKey && isRunning) {
fetchIntegrations()
} else if (!isRunning) {
setIntegrations([])
}
}, [mgmtKey, isRunning, fetchIntegrations])
const truncatePath = (path: string, maxLen: number = 35) => {
if (path.length <= maxLen) return path
const start = path.slice(0, 15)
const end = path.slice(-17)
return `${start}...${end}`
}
return (
<div className="space-y-6">
<div className="rounded-lg border border-[var(--border-default)] bg-[var(--bg-panel)] shadow-xl overflow-hidden">
{/* Ground Control Header */}
<div className="flex items-center gap-2 px-4 py-3 bg-[var(--bg-elevated)] border-b border-[var(--border-default)]">
<div className="relative">
<Radar className={`h-4 w-4 text-[var(--accent-primary)] ${loading || isMgmtLoading ? 'animate-spin' : ''}`} />
{(loading || isMgmtLoading) && (
<div className="absolute inset-0 rounded-full border border-[var(--accent-primary)] animate-ping opacity-50" />
)}
</div>
<span className="font-mono text-xs font-bold uppercase tracking-widest text-[var(--text-secondary)]">
Ground Control
</span>
{isMgmtLoading && <Loader2 className="h-3 w-3 animate-spin text-muted-foreground" />}
<div className="flex-1" />
{/* Scan Button */}
<Tooltip>
<TooltipTrigger asChild>
<button
type="button"
onClick={fetchIntegrations}
disabled={loading || !isRunning}
className={`
relative flex items-center gap-1.5 px-3 py-1.5 rounded
font-mono text-[10px] uppercase tracking-wider font-bold
border transition-all duration-150
${loading
? 'border-[var(--status-processing)] bg-[var(--status-processing)]/10 text-[var(--status-processing)]'
: !isRunning
? 'border-[var(--border-subtle)] bg-transparent text-[var(--text-muted)] cursor-not-allowed opacity-50'
: 'border-[var(--border-default)] bg-[var(--bg-panel)] text-[var(--text-secondary)] hover:border-[var(--accent-primary)] hover:text-[var(--accent-primary)] hover:shadow-[0_0_10px_var(--accent-primary)]'
}
active:translate-y-0.5 cursor-pointer
overflow-hidden
`}
>
{/* Scan sweep effect */}
{loading && (
<div
className="absolute inset-0 bg-gradient-to-r from-transparent via-[var(--status-processing)]/30 to-transparent"
style={{
animation: 'scan-sweep 1s ease-in-out infinite',
}}
/>
)}
<Radar className={`h-3 w-3 relative z-10 ${loading ? 'animate-spin' : ''}`} />
<span className="relative z-10">{loading ? 'Scanning' : 'Scan'}</span>
</button>
</TooltipTrigger>
<TooltipContent>Scan for CLI agent integrations</TooltipContent>
</Tooltip>
</div>
{/* Station Grid */}
<div className="p-4 relative">
{/* Radar sweep overlay during scan */}
{loading && (
<div
className="absolute inset-0 pointer-events-none z-10 overflow-hidden"
style={{
background: 'linear-gradient(90deg, transparent 0%, var(--accent-primary) 50%, transparent 100%)',
opacity: 0.08,
animation: 'radar-scan-horizontal 1.5s ease-in-out',
}}
/>
)}
{/* Empty State */}
{!isRunning ? (
<div className="flex flex-col items-center justify-center py-12 text-center">
<div className="font-mono text-sm text-[var(--text-muted)] uppercase tracking-wider mb-1">
⚠️ Engine Offline
</div>
<div className="text-xs text-[var(--text-muted)]">
Start the proxy engine to manage integrations
</div>
</div>
) : integrations.length === 0 && !loading && (
<div className="flex flex-col items-center justify-center py-12 text-center">
<div className="relative mb-4">
<div className="w-16 h-16 rounded-full border-2 border-dashed border-[var(--border-subtle)] flex items-center justify-center">
<Radar className="h-6 w-6 text-[var(--text-muted)]" />
</div>
</div>
<div className="font-mono text-sm text-[var(--text-muted)] uppercase tracking-wider mb-1">
No Signals Detected
</div>
<div className="text-xs text-[var(--text-muted)]">
Click SCAN to search for CLI agents
</div>
</div>
)}
{/* Integration Cards Grid */}
<div
key={scanKey}
className="grid gap-4 sm:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4"
>
{integrations.map((integration, index) => (
<div
key={integration.id}
className={`
group relative rounded-lg border overflow-hidden
transition-all duration-300
${integration.detected
? 'border-[var(--status-online)]/50 bg-gradient-to-b from-[var(--bg-elevated)] to-[var(--bg-panel)]'
: 'border-[var(--border-subtle)] bg-[var(--bg-void)]'
}
hover:border-[var(--accent-primary)] hover:shadow-[0_0_20px_var(--accent-primary)/15]
hover:translate-y-[-2px]
`}
style={{
animation: `station-fade-in 0.4s ease-out ${index * 0.08}s both`,
}}
>
{/* Station Header Badge */}
<div className={`
flex items-center gap-2 px-3 py-2 border-b
${integration.detected
? 'border-[var(--status-online)]/30 bg-[var(--status-online)]/5'
: 'border-[var(--border-subtle)] bg-[var(--bg-elevated)]'
}
`}>
{/* Status Indicator */}
<div className="relative">
<div className={`
w-3 h-3 rounded-full transition-all duration-300
${integration.detected
? 'bg-[var(--status-online)] shadow-[0_0_8px_var(--status-online)]'
: 'border-2 border-[var(--text-muted)] bg-transparent'
}
`}>
{integration.detected && (
<div
className="absolute inset-0 rounded-full bg-[var(--status-online)]"
style={{
animation: 'status-glow 2s ease-in-out infinite',
}}
/>
)}
</div>
</div>
{/* Integration Name */}
<span className={`
font-mono text-xs font-bold uppercase tracking-wider flex-1
${integration.detected ? 'text-[var(--text-primary)]' : 'text-[var(--text-muted)]'}
`}>
{integration.name || integration.id}
</span>
</div>
{/* Station Body */}
<div className="p-3 space-y-3">
{/* Status Text */}
<div className={`
flex items-center gap-2 font-mono text-xs uppercase tracking-wider
${integration.detected ? 'text-[var(--status-online)]' : 'text-[var(--text-muted)]'}
`}>
{integration.detected ? (
<>
<Check className="h-3 w-3" />
<span>Detected</span>
</>
) : (
<>
<AlertCircle className="h-3 w-3" />
<span>Not Detected</span>
</>
)}
</div>
{/* Path Info (when detected) */}
{integration.detected && (
<div className="space-y-1.5 opacity-0 group-hover:opacity-100 transition-opacity duration-200">
{integration.binary_path && (
<Tooltip>
<TooltipTrigger asChild>
<div className="flex items-center gap-1.5 text-[10px] font-mono text-[var(--text-muted)] truncate cursor-help">
<ExternalLink className="h-2.5 w-2.5 shrink-0" />
<span className="truncate">{truncatePath(integration.binary_path)}</span>
</div>
</TooltipTrigger>
<TooltipContent side="bottom" className="max-w-xs">
<div className="font-mono text-xs break-all">{integration.binary_path}</div>
</TooltipContent>
</Tooltip>
)}
{integration.config_path && (
<Tooltip>
<TooltipTrigger asChild>
<div className="flex items-center gap-1.5 text-[10px] font-mono text-[var(--text-muted)] truncate cursor-help">
<Settings className="h-2.5 w-2.5 shrink-0" />
<span className="truncate">{truncatePath(integration.config_path)}</span>
</div>
</TooltipTrigger>
<TooltipContent side="bottom" className="max-w-xs">
<div className="font-mono text-xs break-all">{integration.config_path}</div>
</TooltipContent>
</Tooltip>
)}
</div>
)}
{/* Spacer for non-detected items to maintain consistent height */}
{!integration.detected && (
<div className="h-[30px]" />
)}
{/* Action Button */}
<button
type="button"
onClick={() => integration.detected && configureIntegration(integration.id)}
disabled={!integration.detected || configuring === integration.id}
className={`
w-full flex items-center justify-center gap-2 px-3 py-2 rounded
font-mono text-xs font-bold uppercase tracking-wider
border transition-all duration-150
${configuring === integration.id
? 'border-[var(--status-processing)] bg-[var(--status-processing)]/10 text-[var(--status-processing)]'
: integration.detected
? 'border-[var(--accent-primary)] bg-transparent text-[var(--accent-primary)] hover:bg-[var(--accent-primary)] hover:text-[var(--bg-panel)] hover:shadow-[0_0_15px_var(--accent-primary)]'
: 'border-[var(--border-subtle)] bg-transparent text-[var(--text-muted)] cursor-not-allowed opacity-50'
}
active:translate-y-0.5 cursor-pointer
disabled:active:translate-y-0
`}
>
{configuring === integration.id ? (
<>
<Settings className="h-3 w-3 animate-spin" />
<span>Configuring</span>
</>
) : integration.detected ? (
<>
<Settings className="h-3 w-3" />
<span>Configure</span>
</>
) : (
<>
<Download className="h-3 w-3" />
<span>Install</span>
</>
)}
</button>
</div>
{/* Configuring overlay effect */}
{configuring === integration.id && (
<div
className="absolute inset-0 pointer-events-none"
style={{
background: 'linear-gradient(180deg, transparent 0%, var(--status-processing) 50%, transparent 100%)',
opacity: 0.1,
animation: 'config-scan 1s ease-in-out infinite',
}}
/>
)}
</div>
))}
</div>
</div>
{/* Footer Stats */}
{integrations.length > 0 && (
<div className="flex items-center gap-4 px-4 py-2 bg-[var(--bg-void)] border-t border-[var(--border-subtle)]">
<div className="flex items-center gap-2">
<div className="w-2 h-2 rounded-full bg-[var(--status-online)]" />
<span className="font-mono text-[10px] uppercase tracking-wider text-[var(--text-muted)]">
{integrations.filter(i => i.detected).length} Online
</span>
</div>
<div className="flex items-center gap-2">
<div className="w-2 h-2 rounded-full border border-[var(--text-muted)]" />
<span className="font-mono text-[10px] uppercase tracking-wider text-[var(--text-muted)]">
{integrations.filter(i => !i.detected).length} Offline
</span>
</div>
</div>
)}
</div>
<AgentSetup />
{/* Animation Keyframes */}
<style>{`
@keyframes station-fade-in {
0% {
opacity: 0;
transform: translateY(10px) scale(0.98);
}
100% {
opacity: 1;
transform: translateY(0) scale(1);
}
}
@keyframes status-glow {
0%, 100% {
opacity: 1;
transform: scale(1);
}
50% {
opacity: 0.6;
transform: scale(1.3);
}
}
@keyframes scan-sweep {
0% {
transform: translateX(-100%);
}
100% {
transform: translateX(200%);
}
}
@keyframes radar-scan-horizontal {
0% {
transform: translateX(-100%);
}
100% {
transform: translateX(100%);
}
}
@keyframes config-scan {
0% {
transform: translateY(-100%);
}
100% {
transform: translateY(100%);
}
}
`}</style>
</div>
)
}
@@ -0,0 +1,477 @@
import { useState, useRef, useEffect, useCallback } from 'react'
import { useProxyContext, EngineOfflineError } from '@/hooks/useProxyContext'
import { Card, CardHeader, CardContent } from '@/components/ui/card'
import { Button } from '@/components/ui/button'
import { Copy, Trash2, FileText, Circle, Download, Loader2 } from 'lucide-react'
type LogLevel = 'ERROR' | 'WARN' | 'INFO' | 'DEBUG' | 'ALL'
interface ParsedLogEntry {
id: string
timestamp: string
level: LogLevel
message: string
raw: string
}
// Parse log line to extract timestamp, level, and message
function parseLogLine(line: string, index: number): ParsedLogEntry {
// Common log patterns: [LEVEL] message, LEVEL: message, timestamp LEVEL message
const patterns = [
// Pattern: [2024-01-15 18:45:23.456] [INFO] message
/^\[?(\d{4}-\d{2}-\d{2}\s+)?(\d{2}:\d{2}:\d{2}(?:\.\d{3})?)\]?\s*\[?(ERROR|WARN(?:ING)?|INFO|DEBUG)\]?:?\s*(.*)$/i,
// Pattern: ERROR: message or [ERROR] message
/^\[?(ERROR|WARN(?:ING)?|INFO|DEBUG)\]?:?\s*(.*)$/i,
// Pattern with timestamp prefix: 18:45:23 INFO message
/^(\d{2}:\d{2}:\d{2}(?:\.\d{3})?)\s+(ERROR|WARN(?:ING)?|INFO|DEBUG)\s+(.*)$/i,
]
const now = new Date()
const defaultTimestamp = now.toTimeString().slice(0, 12).replace(',', '.')
for (const pattern of patterns) {
const match = line.match(pattern)
if (match) {
if (pattern === patterns[0]) {
// Full pattern with optional date and time
const timestamp = match[2] || defaultTimestamp
const level = normalizeLevel(match[3])
const message = match[4] || ''
return { id: `log-${index}`, timestamp: formatTimestamp(timestamp), level, message, raw: line }
} else if (pattern === patterns[1]) {
// Level only pattern
const level = normalizeLevel(match[1])
const message = match[2] || ''
return { id: `log-${index}`, timestamp: defaultTimestamp, level, message, raw: line }
} else if (pattern === patterns[2]) {
// Timestamp + level pattern
const timestamp = match[1]
const level = normalizeLevel(match[2])
const message = match[3] || ''
return { id: `log-${index}`, timestamp: formatTimestamp(timestamp), level, message, raw: line }
}
}
}
// Default: treat as INFO message
return { id: `log-${index}`, timestamp: defaultTimestamp, level: 'INFO', message: line, raw: line }
}
function normalizeLevel(level: string): LogLevel {
const upper = level.toUpperCase()
if (upper === 'WARNING') return 'WARN'
if (['ERROR', 'WARN', 'INFO', 'DEBUG'].includes(upper)) return upper as LogLevel
return 'INFO'
}
function formatTimestamp(ts: string): string {
// Ensure HH:MM:SS.mmm format
if (ts.includes('.')) {
const [time, ms] = ts.split('.')
return `${time}.${ms.padEnd(3, '0').slice(0, 3)}`
}
return `${ts}.000`
}
function getLevelBorderColor(level: LogLevel): string {
switch (level) {
case 'ERROR': return 'border-l-[var(--status-offline)]'
case 'WARN': return 'border-l-[var(--status-warning)]'
case 'INFO': return 'border-l-[var(--accent-glow)]'
case 'DEBUG': return 'border-l-[var(--text-muted)]'
default: return 'border-l-[var(--text-muted)]'
}
}
function getLevelBadgeStyle(level: LogLevel): string {
switch (level) {
case 'ERROR': return 'bg-[var(--status-offline)]/20 text-[var(--status-offline)]'
case 'WARN': return 'bg-[var(--status-warning)]/20 text-[var(--status-warning)]'
case 'INFO': return 'bg-[var(--accent-glow)]/20 text-[var(--accent-glow)]'
case 'DEBUG': return 'bg-[var(--text-muted)]/20 text-[var(--text-muted)]'
default: return 'bg-[var(--text-muted)]/20 text-[var(--text-muted)]'
}
}
export function LogsViewer() {
const { mgmtFetch, showToast, status, isMgmtLoading } = useProxyContext()
const [diagnostics, setDiagnostics] = useState('')
const [logText, setLogText] = useState('')
const [activeLogType, setActiveLogType] = useState<'stdout' | 'stderr'>('stdout')
const [activeFilter, setActiveFilter] = useState<LogLevel>('ALL')
const [isLive, setIsLive] = useState(false)
const [autoScroll, setAutoScroll] = useState(true)
const logsContainerRef = useRef<HTMLDivElement>(null)
const liveIntervalRef = useRef<ReturnType<typeof setInterval> | null>(null)
const isRunning = status?.running ?? false
const loadDiagnostics = async () => {
try {
const res = await mgmtFetch('/v0/management/proxypilot/diagnostics?lines=120')
setDiagnostics(res.text || '')
} catch (e) {
if (!(e instanceof EngineOfflineError)) {
showToast(e instanceof Error ? e.message : String(e), 'error')
}
}
}
const copyDiagnostics = async () => {
if (!diagnostics) {
showToast('No diagnostics to copy', 'error')
return
}
try {
await navigator.clipboard.writeText(diagnostics)
showToast('Copied diagnostics to clipboard', 'success')
} catch (e) {
showToast(e instanceof Error ? e.message : String(e), 'error')
}
}
const tailLogs = useCallback(async (kind: 'stdout' | 'stderr') => {
try {
setActiveLogType(kind)
const res = await mgmtFetch(`/v0/management/proxypilot/logs/tail?file=${kind}&lines=200`)
setLogText((res.lines || []).join('\n'))
} catch (e) {
if (!(e instanceof EngineOfflineError)) {
showToast(e instanceof Error ? e.message : String(e), 'error')
}
}
}, [mgmtFetch, showToast])
const copyLogs = async () => {
if (!logText) {
showToast('No logs to copy', 'error')
return
}
try {
await navigator.clipboard.writeText(logText)
showToast('Copied logs to clipboard', 'success')
} catch (e) {
showToast(e instanceof Error ? e.message : String(e), 'error')
}
}
const clearLogs = () => {
setLogText('')
setIsLive(false)
if (liveIntervalRef.current) {
clearInterval(liveIntervalRef.current)
liveIntervalRef.current = null
}
}
const downloadLogs = async () => {
try {
const res = await mgmtFetch(`/v0/management/proxypilot/logs/tail?file=${activeLogType}&lines=2000`)
const text = (res.lines || []).join('\n')
if (!text) {
showToast('No logs available to download', 'error')
return
}
const blob = new Blob([text], { type: 'text/plain' })
const url = URL.createObjectURL(blob)
const a = document.createElement('a')
a.href = url
a.download = `proxypilot-${activeLogType}-${new Date().toISOString().slice(0, 19).replace(/:/g, '-')}.log`
document.body.appendChild(a)
a.click()
document.body.removeChild(a)
URL.revokeObjectURL(url)
showToast('Logs download started', 'success')
} catch (e) {
showToast(e instanceof Error ? e.message : String(e), 'error')
}
}
const toggleLive = () => {
if (isLive) {
setIsLive(false)
if (liveIntervalRef.current) {
clearInterval(liveIntervalRef.current)
liveIntervalRef.current = null
}
} else {
setIsLive(true)
tailLogs(activeLogType)
liveIntervalRef.current = setInterval(() => {
tailLogs(activeLogType)
}, 2000)
}
}
// Auto-scroll when new logs arrive
useEffect(() => {
if (autoScroll && logsContainerRef.current) {
logsContainerRef.current.scrollTop = logsContainerRef.current.scrollHeight
}
}, [logText, autoScroll])
// Cleanup interval on unmount
useEffect(() => {
return () => {
if (liveIntervalRef.current) {
clearInterval(liveIntervalRef.current)
}
}
}, [])
// Parse log entries
const logEntries: ParsedLogEntry[] = logText
? logText.split('\n').filter(Boolean).map((line, i) => parseLogLine(line, i))
: []
// Filter log entries
const filteredEntries = activeFilter === 'ALL'
? logEntries
: logEntries.filter(entry => entry.level === activeFilter)
const filterButtons: LogLevel[] = ['ALL', 'ERROR', 'WARN', 'INFO', 'DEBUG']
return (
<div className="grid gap-6 lg:grid-cols-2">
{/* Diagnostics Card */}
<Card className="backdrop-blur-sm bg-card/60 border-border/50 shadow-xl">
<CardHeader className="pb-4">
<div className="flex items-center justify-between">
<div className="flex items-center gap-3">
<div className="h-10 w-10 rounded-xl bg-blue-500/10 flex items-center justify-center">
<FileText className="h-5 w-5 text-blue-500" />
</div>
<div>
<h3 className="text-lg font-semibold">Diagnostics</h3>
<p className="text-sm text-muted-foreground">Engine snapshot</p>
</div>
</div>
{isMgmtLoading && <Loader2 className="h-4 w-4 animate-spin text-muted-foreground" />}
</div>
</CardHeader>
<CardContent className="space-y-3">
{!isRunning && (
<div className="py-4 text-center text-muted-foreground">
<p className="text-xs uppercase tracking-widest font-mono">⚠️ Engine Offline</p>
</div>
)}
{isRunning && (
<>
<div className="flex gap-2">
<Button size="sm" variant="outline" onClick={loadDiagnostics}>
<Download className="h-4 w-4 mr-1" />
Load
</Button>
<Button
size="sm"
variant="outline"
onClick={copyDiagnostics}
disabled={!diagnostics}
className="gap-2"
>
<Copy className="h-4 w-4" />
Copy
</Button>
</div>
<pre className="max-h-48 overflow-auto rounded-md border border-border/50 bg-muted/30 p-2 text-xs font-mono">
{diagnostics || 'No diagnostics loaded.'}
</pre>
</>
)}
</CardContent>
</Card>
{/* Flight Recorder Logs Card */}
<Card className="backdrop-blur-sm bg-[var(--bg-void)] border-[var(--border-default)] shadow-xl overflow-hidden">
{/* Flight Recorder Header */}
<CardHeader className="pb-3 border-b border-[var(--border-subtle)] bg-[var(--bg-panel)]">
<div className="flex items-center justify-between flex-wrap gap-3">
{/* Title with record indicator */}
<div className="flex items-center gap-3">
<div className="flex items-center gap-2">
<Circle
className={`h-3 w-3 fill-current ${isLive && isRunning ? 'text-[var(--status-offline)] animate-pulse' : 'text-[var(--text-muted)]'}`}
/>
<span className="font-mono text-sm font-semibold tracking-wider text-[var(--text-primary)]">
FLIGHT RECORDER
</span>
</div>
{isMgmtLoading && <Loader2 className="h-3 w-3 animate-spin text-muted-foreground" />}
{/* LIVE indicator */}
<button
onClick={toggleLive}
disabled={!isRunning}
className={`flex items-center gap-1.5 px-2 py-0.5 rounded text-xs font-mono transition-all ${isLive && isRunning
? 'bg-[var(--status-offline)]/20 text-[var(--status-offline)] animate-pulse'
: 'bg-[var(--bg-elevated)] text-[var(--text-muted)] hover:text-[var(--text-secondary)]'
} ${!isRunning ? 'opacity-50 cursor-not-allowed' : ''}`}
>
<span className={`h-1.5 w-1.5 rounded-full ${isLive && isRunning ? 'bg-[var(--status-offline)]' : 'bg-[var(--text-muted)]'}`} />
LIVE
</button>
</div>
{/* Filter chips */}
<div className="flex items-center gap-1">
{filterButtons.map(level => (
<button
key={level}
onClick={() => setActiveFilter(level)}
className={`px-2 py-0.5 rounded text-xs font-mono uppercase transition-all ${activeFilter === level
? level === 'ALL'
? 'bg-[var(--accent-primary)] text-[var(--primary-foreground)]'
: getLevelBadgeStyle(level).replace('/20', '/40')
: 'bg-[var(--bg-elevated)] text-[var(--text-muted)] hover:text-[var(--text-secondary)]'
}`}
>
{level === 'ALL' ? 'ALL' : level.slice(0, 3)}
</button>
))}
</div>
</div>
{/* Secondary controls */}
<div className="flex items-center gap-2 mt-3">
<Button
size="sm"
variant={activeLogType === 'stdout' ? 'default' : 'outline'}
onClick={() => tailLogs('stdout')}
className="text-xs h-7 font-mono"
>
STDOUT
</Button>
<Button
size="sm"
variant={activeLogType === 'stderr' ? 'default' : 'outline'}
onClick={() => tailLogs('stderr')}
className="text-xs h-7 font-mono"
>
STDERR
</Button>
<div className="flex-1" />
<Button
size="sm"
variant="ghost"
onClick={copyLogs}
disabled={!logText}
className="text-xs h-7 gap-1 text-[var(--text-muted)] hover:text-[var(--text-primary)]"
>
<Copy className="h-3 w-3" />
</Button>
<Button
size="sm"
variant="ghost"
onClick={downloadLogs}
disabled={!logText}
className="text-xs h-7 gap-1 text-[var(--text-muted)] hover:text-[var(--text-primary)]"
>
<Download className="h-3 w-3" />
</Button>
<Button
size="sm"
variant="ghost"
onClick={clearLogs}
disabled={!logText}
className="text-xs h-7 gap-1 text-[var(--text-muted)] hover:text-[var(--status-offline)]"
>
<Trash2 className="h-3 w-3" />
</Button>
</div>
</CardHeader>
{/* Log entries with scanline effect */}
<CardContent className="p-0 relative">
{/* Scanline overlay */}
<div
className="absolute inset-0 pointer-events-none z-10 overflow-hidden"
aria-hidden="true"
>
<div
className="absolute left-0 right-0 h-8 bg-gradient-to-b from-[var(--accent-glow)]/[0.07] to-transparent animate-scanline"
style={{ top: '-10%' }}
/>
</div>
{/* Log content */}
<div
ref={logsContainerRef}
className="max-h-64 overflow-auto bg-[var(--bg-void)] font-mono text-xs"
onScroll={(e) => {
const target = e.target as HTMLDivElement
const isAtBottom = target.scrollHeight - target.scrollTop <= target.clientHeight + 50
setAutoScroll(isAtBottom)
}}
>
{!isRunning ? (
<div className="p-8 text-center text-[var(--text-muted)]">
<p className="text-xs uppercase tracking-widest font-mono">⚠️ Engine Offline</p>
<p className="text-[10px] mt-1">Start the proxy engine to view logs</p>
</div>
) : filteredEntries.length === 0 ? (
<div className="p-4 text-center text-[var(--text-muted)]">
{logText ? 'No matching log entries.' : 'No logs loaded. Click STDOUT or STDERR to load logs.'}
</div>
) : (
<div className="divide-y divide-[var(--border-subtle)]/30">
{filteredEntries.map((entry) => (
<div
key={entry.id}
className={`flex items-start gap-2 px-3 py-1.5 border-l-2 hover:bg-[var(--bg-panel)]/50 transition-colors ${getLevelBorderColor(entry.level)}`}
>
{/* Timestamp */}
<span className="text-[var(--text-muted)] shrink-0 tabular-nums">
{entry.timestamp}
</span>
{/* Separator */}
<span className="text-[var(--border-default)]">|</span>
{/* Level badge */}
<span className={`shrink-0 px-1.5 py-0 rounded text-[10px] font-semibold uppercase ${getLevelBadgeStyle(entry.level)}`}>
{entry.level.padEnd(5)}
</span>
{/* Separator */}
<span className="text-[var(--border-default)]">|</span>
{/* Message */}
<span className="text-[var(--text-primary)] break-all">
{entry.message}
</span>
</div>
))}
</div>
)}
</div>
{/* Auto-scroll indicator */}
{!autoScroll && filteredEntries.length > 0 && (
<button
onClick={() => {
setAutoScroll(true)
if (logsContainerRef.current) {
logsContainerRef.current.scrollTop = logsContainerRef.current.scrollHeight
}
}}
className="absolute bottom-2 right-4 px-2 py-1 rounded bg-[var(--accent-primary)] text-[var(--primary-foreground)] text-xs font-mono shadow-lg hover:opacity-90 transition-opacity z-20"
>
SCROLL TO BOTTOM
</button>
)}
</CardContent>
{/* Status bar */}
<div className="px-3 py-1.5 bg-[var(--bg-panel)] border-t border-[var(--border-subtle)] flex items-center justify-between text-[10px] font-mono text-[var(--text-muted)]">
<span>{filteredEntries.length} entries {activeFilter !== 'ALL' && `(filtered: ${activeFilter})`}</span>
<span className="flex items-center gap-2">
<span className={autoScroll ? 'text-[var(--status-online)]' : ''}>
{autoScroll ? 'AUTO-SCROLL ON' : 'AUTO-SCROLL OFF'}
</span>
<span>|</span>
<span>{activeLogType.toUpperCase()}</span>
</span>
</div>
</Card>
</div>
)
}
@@ -0,0 +1,937 @@
import { useState, useEffect, useCallback, useRef } from 'react'
import { Button } from '@/components/ui/button'
import { Switch } from '@/components/ui/switch'
import { Label } from '@/components/ui/label'
import {
Download,
Upload,
Trash2,
RefreshCw,
Save,
Anchor,
Pin,
Scissors,
Brain,
ChevronDown,
ChevronRight,
Circle,
AlertTriangle,
Wrench,
Loader2,
} from 'lucide-react'
import { useProxyContext, EngineOfflineError } from '@/hooks/useProxyContext'
import { cn } from '@/lib/utils'
interface MemorySession {
key: string;
updated_at?: string;
summary?: string;
pinned?: string;
todo?: string;
semantic_disabled?: boolean;
}
interface PruneConfig {
maxAgeDays: number;
maxSessions: number;
maxBytesPerSession: number;
maxNamespaces: number;
maxBytesPerNamespace: number;
}
interface ParsedAnchor {
ts: string;
summary: string;
}
interface ParsedEvent {
ts: string;
kind: string;
role: string;
text: string;
}
const downloadBlob = (blob: Blob, filename: string) => {
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = filename;
a.click();
URL.revokeObjectURL(url);
};
// Format timestamp to HH:MM:SS
const formatTime = (ts: string): string => {
if (!ts) return '--:--:--';
try {
const date = new Date(ts);
return date.toTimeString().slice(0, 8);
} catch {
return ts.slice(11, 19) || ts;
}
};
// Calculate duration between two timestamps
const calculateDuration = (start: string, end: string): string => {
try {
const startDate = new Date(start);
const endDate = new Date(end);
const diffMs = endDate.getTime() - startDate.getTime();
const hours = Math.floor(diffMs / 3600000);
const minutes = Math.floor((diffMs % 3600000) / 60000);
const seconds = Math.floor((diffMs % 60000) / 1000);
return `${hours.toString().padStart(2, '0')}:${minutes.toString().padStart(2, '0')}:${seconds.toString().padStart(2, '0')}`;
} catch {
return '--:--:--';
}
};
// Collapsible section component with smooth animation
function CollapsibleSection({
title,
icon: Icon,
count,
children,
defaultOpen = false,
}: {
title: string;
icon: React.ElementType;
count?: number;
children: React.ReactNode;
defaultOpen?: boolean;
}) {
const [isOpen, setIsOpen] = useState(defaultOpen);
const contentRef = useRef<HTMLDivElement>(null);
const [height, setHeight] = useState<number | undefined>(defaultOpen ? undefined : 0);
useEffect(() => {
if (isOpen) {
const contentEl = contentRef.current;
if (contentEl) {
setHeight(contentEl.scrollHeight);
// After animation, set to auto for dynamic content
const timer = setTimeout(() => setHeight(undefined), 300);
return () => clearTimeout(timer);
}
} else {
setHeight(0);
}
}, [isOpen]);
return (
<div className="border border-orange-500/30 rounded bg-black/40">
<button
onClick={() => setIsOpen(!isOpen)}
className="w-full flex items-center gap-2 px-3 py-2 text-left hover:bg-orange-500/10 transition-colors"
>
{isOpen ? (
<ChevronDown className="h-4 w-4 text-orange-400" />
) : (
<ChevronRight className="h-4 w-4 text-orange-400" />
)}
<Icon className="h-4 w-4 text-orange-400" />
<span className="text-xs font-mono text-orange-300 uppercase tracking-wider">
{title}
</span>
{count !== undefined && (
<span className="ml-auto text-xs font-mono bg-orange-500/20 text-orange-300 px-2 py-0.5 rounded">
{count}
</span>
)}
</button>
<div
style={{ height: height !== undefined ? `${height}px` : 'auto' }}
className="overflow-hidden transition-[height] duration-300 ease-in-out"
>
<div ref={contentRef} className="px-3 pb-3">
{children}
</div>
</div>
</div>
);
}
export function MemoryManager() {
const { mgmtKey, mgmtFetch, showToast, status, isMgmtLoading } = useProxyContext()
// Session state
const [memorySessions, setMemorySessions] = useState<MemorySession[]>([])
const [memorySession, setMemorySession] = useState('')
const [memoryDetails, setMemoryDetails] = useState<MemorySession | null>(null)
// Session details
const [memorySummary, setMemorySummary] = useState('')
const [memoryPinned, setMemoryPinned] = useState('')
const [memoryTodo, setMemoryTodo] = useState('')
const [memorySemanticEnabled, setMemorySemanticEnabled] = useState(true)
// Events and anchors - now parsed
const [memoryEvents, setMemoryEvents] = useState<ParsedEvent[]>([])
const [memoryAnchors, setMemoryAnchors] = useState<ParsedAnchor[]>([])
const [memoryEventsLimit, setMemoryEventsLimit] = useState(120)
const [memoryAnchorsLimit, setMemoryAnchorsLimit] = useState(20)
// Import
const [memoryImportReplace, setMemoryImportReplace] = useState(false)
// Prune configuration
const [memoryPrune, setMemoryPrune] = useState<PruneConfig>({
maxAgeDays: 30,
maxSessions: 200,
maxBytesPerSession: 2000000,
maxNamespaces: 200,
maxBytesPerNamespace: 2000000,
})
// Session dropdown open
const [sessionDropdownOpen, setSessionDropdownOpen] = useState(false)
const isRunning = status?.running ?? false
// Load sessions list
const loadMemorySessions = useCallback(async () => {
try {
const res = await mgmtFetch('/v0/management/memory/sessions?limit=200')
const sessions = res.sessions || []
setMemorySessions(sessions)
if (!memorySession && sessions.length > 0) {
setMemorySession(sessions[0].key)
}
} catch (e) {
if (!(e instanceof EngineOfflineError)) {
showToast(e instanceof Error ? e.message : String(e), 'error')
}
}
}, [mgmtFetch, showToast, memorySession])
// Load session details
const loadMemorySessionDetails = useCallback(async () => {
if (!memorySession) {
setMemoryDetails(null)
return
}
try {
const res = await mgmtFetch(`/v0/management/memory/session?session=${encodeURIComponent(memorySession)}`)
const session = res.session || null
setMemoryDetails(session)
if (session) {
setMemorySummary(session.summary || '')
setMemoryPinned(session.pinned || '')
setMemoryTodo(session.todo || '')
setMemorySemanticEnabled(!session.semantic_disabled)
}
} catch (e) {
if (!(e instanceof EngineOfflineError)) {
showToast(e instanceof Error ? e.message : String(e), 'error')
}
}
}, [memorySession, mgmtFetch, showToast])
// Load events
const loadMemoryEvents = useCallback(async () => {
if (!memorySession) {
setMemoryEvents([])
return
}
try {
const res = await mgmtFetch(`/v0/management/memory/events?session=${encodeURIComponent(memorySession)}&limit=${encodeURIComponent(memoryEventsLimit)}`)
const events = res.events || []
if (!Array.isArray(events) || events.length === 0) {
setMemoryEvents([])
return
}
const parsed: ParsedEvent[] = events.map((e: any) => ({
ts: e.ts || '',
kind: e.kind || '',
role: e.role || '',
text: (e.text || '').toString(),
}))
setMemoryEvents(parsed)
} catch (e) {
if (!(e instanceof EngineOfflineError)) {
showToast(e instanceof Error ? e.message : String(e), 'error')
}
}
}, [memorySession, memoryEventsLimit, mgmtFetch, showToast])
// Load anchors
const loadMemoryAnchors = useCallback(async () => {
if (!memorySession) {
setMemoryAnchors([])
return
}
try {
const res = await mgmtFetch(`/v0/management/memory/anchors?session=${encodeURIComponent(memorySession)}&limit=${encodeURIComponent(memoryAnchorsLimit)}`)
const anchors = res.anchors || []
if (!Array.isArray(anchors) || anchors.length === 0) {
setMemoryAnchors([])
return
}
const parsed: ParsedAnchor[] = anchors.map((a: any) => ({
ts: a.ts || '',
summary: (a.summary || '').toString(),
}))
setMemoryAnchors(parsed)
} catch (e) {
if (!(e instanceof EngineOfflineError)) {
showToast(e instanceof Error ? e.message : String(e), 'error')
}
}
}, [memorySession, memoryAnchorsLimit, mgmtFetch, showToast])
// Save TODO
const saveMemoryTodo = async () => {
if (!memorySession) return
try {
await mgmtFetch('/v0/management/memory/todo', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ session: memorySession, value: memoryTodo }),
})
await loadMemorySessionDetails()
showToast('Saved TODO', 'success')
} catch (e) {
showToast(e instanceof Error ? e.message : String(e), 'error')
}
}
// Save pinned context
const saveMemoryPinned = async () => {
if (!memorySession) return
try {
await mgmtFetch('/v0/management/memory/pinned', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ session: memorySession, value: memoryPinned }),
})
await loadMemorySessionDetails()
showToast('Saved pinned context', 'success')
} catch (e) {
showToast(e instanceof Error ? e.message : String(e), 'error')
}
}
// Save summary
const saveMemorySummary = async () => {
if (!memorySession) return
try {
await mgmtFetch('/v0/management/memory/summary', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ session: memorySession, value: memorySummary }),
})
await loadMemorySessionDetails()
showToast('Saved anchor summary', 'success')
} catch (e) {
showToast(e instanceof Error ? e.message : String(e), 'error')
}
}
// Toggle semantic memory
const toggleMemorySemantic = async (enabled: boolean) => {
if (!memorySession) return
try {
await mgmtFetch('/v0/management/memory/semantic', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ session: memorySession, enabled }),
})
setMemorySemanticEnabled(enabled)
await loadMemorySessionDetails()
showToast(`Semantic ${enabled ? 'enabled' : 'disabled'}`, 'success')
} catch (e) {
showToast(e instanceof Error ? e.message : String(e), 'error')
}
}
// Delete session
const deleteMemorySession = async () => {
if (!memorySession) return
try {
await mgmtFetch(`/v0/management/memory/session?session=${encodeURIComponent(memorySession)}`, {
method: 'DELETE',
})
setMemorySession('')
setMemoryDetails(null)
setMemoryEvents([])
setMemoryAnchors([])
await loadMemorySessions()
showToast('Deleted session', 'success')
} catch (e) {
showToast(e instanceof Error ? e.message : String(e), 'error')
}
}
// Export session
const exportMemorySession = async () => {
if (!memorySession || !mgmtKey) return
try {
const res = await fetch(`/v0/management/memory/export?session=${encodeURIComponent(memorySession)}`, {
headers: { 'X-Management-Key': mgmtKey },
})
if (!res.ok) {
const msg = await res.text()
throw new Error(msg)
}
const blob = await res.blob()
downloadBlob(blob, `flight-log-${memorySession}.zip`)
} catch (e) {
showToast(e instanceof Error ? e.message : String(e), 'error')
}
}
// Export all sessions
const exportAllMemory = async () => {
if (!mgmtKey) return
try {
const res = await fetch('/v0/management/memory/export-all', {
headers: { 'X-Management-Key': mgmtKey },
})
if (!res.ok) {
const msg = await res.text()
throw new Error(msg)
}
const blob = await res.blob()
downloadBlob(blob, 'flight-data-complete.zip')
} catch (e) {
showToast(e instanceof Error ? e.message : String(e), 'error')
}
}
// Delete all sessions
const deleteAllMemory = async () => {
if (!mgmtKey) return
if (!window.confirm('PURGE ALL FLIGHT DATA? This action cannot be reversed.')) return
try {
await mgmtFetch('/v0/management/memory/delete-all?confirm=true', { method: 'POST' })
setMemorySession('')
setMemoryDetails(null)
setMemoryEvents([])
setMemoryAnchors([])
await loadMemorySessions()
showToast('All flight data purged', 'success')
} catch (e) {
showToast(e instanceof Error ? e.message : String(e), 'error')
}
}
// Import session
const importMemorySession = async (file: File | null) => {
if (!file || !memorySession || !mgmtKey) return
try {
const form = new FormData()
form.append('file', file)
const res = await fetch(`/v0/management/memory/import?session=${encodeURIComponent(memorySession)}&replace=${memoryImportReplace ? 'true' : 'false'}`, {
method: 'POST',
headers: { 'X-Management-Key': mgmtKey },
body: form,
})
if (!res.ok) {
const msg = await res.text()
throw new Error(msg)
}
await loadMemorySessionDetails()
showToast('Flight data imported', 'success')
} catch (e) {
showToast(e instanceof Error ? e.message : String(e), 'error')
}
}
// Prune memory
const pruneMemory = async () => {
try {
await mgmtFetch('/v0/management/memory/prune', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
max_age_days: memoryPrune.maxAgeDays,
max_sessions: memoryPrune.maxSessions,
max_bytes_per_session: memoryPrune.maxBytesPerSession,
max_namespaces: memoryPrune.maxNamespaces,
max_bytes_per_namespace: memoryPrune.maxBytesPerNamespace,
}),
})
await loadMemorySessions()
showToast('Maintenance complete', 'success')
} catch (e) {
showToast(e instanceof Error ? e.message : String(e), 'error')
}
}
// Load sessions on mount
useEffect(() => {
if (mgmtKey && isRunning) {
loadMemorySessions()
} else if (!isRunning) {
setMemorySessions([])
setMemoryDetails(null)
setMemoryEvents([])
setMemoryAnchors([])
}
}, [mgmtKey, isRunning, loadMemorySessions])
// Auto-load session details when session changes
useEffect(() => {
if (memorySession && isRunning) {
loadMemorySessionDetails()
loadMemoryAnchors()
loadMemoryEvents()
}
}, [memorySession, isRunning, loadMemorySessionDetails, loadMemoryAnchors, loadMemoryEvents])
if (!mgmtKey) {
return null
}
// Get session start time and calculate duration
const sessionStartTime = memoryEvents.length > 0 ? memoryEvents[memoryEvents.length - 1]?.ts : '';
const sessionEndTime = memoryEvents.length > 0 ? memoryEvents[0]?.ts : '';
const sessionDuration = sessionStartTime && sessionEndTime ? calculateDuration(sessionStartTime, sessionEndTime) : '--:--:--';
return (
<div className="bg-gradient-to-b from-zinc-900 to-black border border-orange-500/40 rounded-lg shadow-2xl shadow-orange-500/10 overflow-hidden">
{/* Header */}
<div className="bg-gradient-to-r from-orange-950/80 via-orange-900/60 to-orange-950/80 border-b border-orange-500/40 px-4 py-3">
<div className="flex items-center justify-between">
<div className="flex items-center gap-3">
<div className="relative">
<Circle className={`h-4 w-4 ${isRunning ? 'text-red-500 fill-red-500 animate-pulse' : 'text-muted'}`} />
{isRunning && <div className="absolute inset-0 h-4 w-4 bg-red-500 rounded-full blur-sm opacity-50 animate-pulse" />}
</div>
<span className="text-sm font-mono font-bold text-orange-300 uppercase tracking-widest">
Black Box Recorder
</span>
{isMgmtLoading && <Loader2 className="h-4 w-4 animate-spin text-orange-500/60" />}
<span className="text-xs font-mono text-orange-500/60">
[{memorySessions.length} LOGS]
</span>
</div>
<div className="flex items-center gap-2">
<Button
size="sm"
variant="ghost"
onClick={exportMemorySession}
disabled={!memorySession || !isRunning}
className="gap-1.5 text-xs font-mono bg-orange-500/10 hover:bg-orange-500/20 text-orange-300 border border-orange-500/30"
>
<Download className="h-3 w-3" />
EXPORT
</Button>
<Button
size="sm"
variant="ghost"
onClick={exportAllMemory}
disabled={!isRunning}
className="gap-1.5 text-xs font-mono bg-orange-500/10 hover:bg-orange-500/20 text-orange-300 border border-orange-500/30"
>
<Download className="h-3 w-3" />
EXPORT ALL
</Button>
<label className={cn("cursor-pointer", !isRunning && "opacity-50 pointer-events-none")}>
<input
type="file"
accept=".zip"
className="hidden"
disabled={!isRunning}
onChange={(e) => importMemorySession(e.target.files?.[0] || null)}
/>
<div className="flex items-center gap-1.5 text-xs font-mono bg-orange-500/10 hover:bg-orange-500/20 text-orange-300 border border-orange-500/30 px-3 py-1.5 rounded-md transition-colors">
<Upload className="h-3 w-3" />
IMPORT
</div>
</label>
</div>
</div>
</div>
{/* Content */}
<div className="p-4 space-y-4">
{!isRunning && (
<div className="py-8 text-center text-muted-foreground border border-dashed border-orange-500/20 rounded bg-black/20">
<p className="text-sm uppercase tracking-widest font-mono">⚠️ Engine Offline</p>
<p className="text-xs mt-1">Start the proxy engine to access flight data</p>
</div>
)}
{isRunning && (
<>
{/* Session Selector */}
<div className="space-y-3">
<div className="flex items-center gap-3">
<span className="text-xs font-mono text-orange-400 uppercase tracking-wider">Session:</span>
<div className="relative flex-1">
<button
onClick={() => setSessionDropdownOpen(!sessionDropdownOpen)}
className="w-full flex items-center justify-between px-3 py-2 bg-black/60 border border-orange-500/30 rounded text-left hover:border-orange-500/50 transition-colors"
>
<span className="font-mono text-sm text-orange-200">
{memorySession || '(no session selected)'}
</span>
<ChevronDown className={`h-4 w-4 text-orange-400 transition-transform ${sessionDropdownOpen ? 'rotate-180' : ''}`} />
</button>
{sessionDropdownOpen && (
<div className="absolute z-10 w-full mt-1 max-h-60 overflow-auto bg-zinc-900 border border-orange-500/30 rounded shadow-xl">
{memorySessions.length === 0 ? (
<div className="px-3 py-2 text-xs font-mono text-orange-500/60">(no sessions)</div>
) : (
memorySessions.map((s) => (
<button
key={s.key}
onClick={() => {
setMemorySession(s.key)
setSessionDropdownOpen(false)
}}
className={`w-full px-3 py-2 text-left text-sm font-mono hover:bg-orange-500/10 transition-colors ${s.key === memorySession ? 'bg-orange-500/20 text-orange-200' : 'text-orange-300/80'
}`}
>
{s.key}
</button>
))
)}
</div>
)}
</div>
<Button
size="sm"
variant="ghost"
onClick={loadMemorySessions}
className="text-orange-400 hover:text-orange-300 hover:bg-orange-500/10"
>
<RefreshCw className="h-4 w-4" />
</Button>
</div>
{/* Session Metadata Card */}
{memorySession && (
<div className="bg-black/40 border border-orange-500/20 rounded p-3">
<div className="grid grid-cols-2 md:grid-cols-4 gap-4 text-xs font-mono">
<div>
<span className="text-orange-500/60">Started:</span>
<div className="text-orange-200">{formatTime(sessionStartTime)} UTC</div>
</div>
<div>
<span className="text-orange-500/60">Duration:</span>
<div className="text-orange-200">{sessionDuration}</div>
</div>
<div>
<span className="text-orange-500/60">Anchors:</span>
<div className="text-orange-200">{memoryAnchors.length}</div>
</div>
<div>
<span className="text-orange-500/60">Events:</span>
<div className="text-orange-200">{memoryEvents.length}</div>
</div>
</div>
<div className="flex items-center gap-4 mt-3 pt-3 border-t border-orange-500/20">
<div className="flex items-center gap-2">
<Brain className="h-4 w-4 text-orange-400" />
<Label htmlFor="semantic-toggle" className="text-xs font-mono text-orange-300 cursor-pointer">
SEMANTIC
</Label>
<Switch
id="semantic-toggle"
checked={memorySemanticEnabled}
disabled={!memorySession}
onCheckedChange={toggleMemorySemantic}
/>
</div>
<span className="text-xs font-mono text-orange-500/60">
{memoryDetails?.updated_at ? `Last update: ${memoryDetails.updated_at}` : ''}
</span>
</div>
</div>
)}
</div>
{/* Anchors Section */}
<CollapsibleSection
title="Anchors"
icon={Anchor}
count={memoryAnchors.length}
defaultOpen={true}
>
<div className="space-y-2">
<div className="flex items-center gap-2 mb-2">
<Label className="text-xs font-mono text-orange-500/60">Limit:</Label>
<input
type="number"
min={5}
max={200}
className="w-16 rounded bg-black/60 border border-orange-500/30 px-2 py-1 text-xs font-mono text-orange-200 focus:border-orange-500/50 focus:outline-none"
value={memoryAnchorsLimit}
onChange={(e) => setMemoryAnchorsLimit(parseInt(e.target.value || '20', 10))}
/>
<Button
size="sm"
variant="ghost"
onClick={loadMemoryAnchors}
className="text-xs font-mono text-orange-400 hover:bg-orange-500/10"
>
<RefreshCw className="h-3 w-3 mr-1" />
Reload
</Button>
</div>
<div className="bg-black/60 border border-orange-500/20 rounded max-h-48 overflow-auto">
{memoryAnchors.length === 0 ? (
<div className="px-3 py-4 text-xs font-mono text-orange-500/50 text-center">No anchors recorded.</div>
) : (
<div className="divide-y divide-orange-500/10">
{memoryAnchors.map((anchor, i) => (
<div key={i} className="px-3 py-2 hover:bg-orange-500/5">
<div className="flex items-start gap-2">
<span className="text-xs font-mono text-orange-400 shrink-0">
{formatTime(anchor.ts)}
</span>
<span className="text-xs font-mono text-orange-500/30">|</span>
<span className="text-xs font-mono text-orange-200/80 break-words">
{anchor.summary.length > 100 ? anchor.summary.slice(0, 100) + '...' : anchor.summary}
</span>
</div>
</div>
))}
</div>
)}
</div>
<div className="space-y-2 mt-3">
<Label className="text-xs font-mono text-orange-500/60">Anchor Summary:</Label>
<textarea
className="w-full h-24 rounded bg-black/60 border border-orange-500/30 px-3 py-2 text-xs font-mono text-orange-200 resize-y focus:border-orange-500/50 focus:outline-none"
value={memorySummary}
onChange={(e) => setMemorySummary(e.target.value)}
placeholder="Session anchor summary..."
/>
<Button
size="sm"
onClick={saveMemorySummary}
className="gap-1 text-xs font-mono bg-orange-500/20 hover:bg-orange-500/30 text-orange-300 border border-orange-500/30"
>
<Save className="h-3 w-3" />
Save Summary
</Button>
</div>
</div>
</CollapsibleSection>
{/* Pinned Context Section */}
<CollapsibleSection
title="Pinned Context"
icon={Pin}
defaultOpen={false}
>
<div className="space-y-2">
<textarea
className="w-full h-32 rounded bg-black/60 border border-orange-500/30 px-3 py-2 text-xs font-mono text-orange-200 resize-y focus:border-orange-500/50 focus:outline-none"
value={memoryPinned}
onChange={(e) => setMemoryPinned(e.target.value)}
placeholder="Pinned context that persists across turns..."
/>
<Button
size="sm"
onClick={saveMemoryPinned}
className="gap-1 text-xs font-mono bg-orange-500/20 hover:bg-orange-500/30 text-orange-300 border border-orange-500/30"
>
<Save className="h-3 w-3" />
Save Pinned
</Button>
</div>
</CollapsibleSection>
{/* TODO Section */}
<CollapsibleSection
title="TODO Log"
icon={Anchor}
defaultOpen={false}
>
<div className="space-y-2">
<textarea
className="w-full h-32 rounded bg-black/60 border border-orange-500/30 px-3 py-2 text-xs font-mono text-orange-200 resize-y focus:border-orange-500/50 focus:outline-none"
value={memoryTodo}
onChange={(e) => setMemoryTodo(e.target.value)}
placeholder="Session TODO list..."
/>
<Button
size="sm"
onClick={saveMemoryTodo}
className="gap-1 text-xs font-mono bg-orange-500/20 hover:bg-orange-500/30 text-orange-300 border border-orange-500/30"
>
<Save className="h-3 w-3" />
Save TODO
</Button>
</div>
</CollapsibleSection>
{/* Events Section */}
<CollapsibleSection
title="Event Log"
icon={Circle}
count={memoryEvents.length}
defaultOpen={false}
>
<div className="space-y-2">
<div className="flex items-center gap-2 mb-2">
<Label className="text-xs font-mono text-orange-500/60">Limit:</Label>
<input
type="number"
min={10}
max={500}
className="w-16 rounded bg-black/60 border border-orange-500/30 px-2 py-1 text-xs font-mono text-orange-200 focus:border-orange-500/50 focus:outline-none"
value={memoryEventsLimit}
onChange={(e) => setMemoryEventsLimit(parseInt(e.target.value || '120', 10))}
/>
<Button
size="sm"
variant="ghost"
onClick={loadMemoryEvents}
className="text-xs font-mono text-orange-400 hover:bg-orange-500/10"
>
<RefreshCw className="h-3 w-3 mr-1" />
Reload
</Button>
</div>
<div className="bg-black/60 border border-orange-500/20 rounded max-h-64 overflow-auto">
{memoryEvents.length === 0 ? (
<div className="px-3 py-4 text-xs font-mono text-orange-500/50 text-center">No events recorded.</div>
) : (
<div className="divide-y divide-orange-500/10">
{memoryEvents.map((event, i) => (
<div key={i} className="px-3 py-2 hover:bg-orange-500/5">
<div className="flex items-start gap-2">
<span className="text-xs font-mono text-orange-400 shrink-0">
{formatTime(event.ts)}
</span>
<span className="text-xs font-mono px-1.5 py-0.5 rounded bg-orange-500/20 text-orange-300 shrink-0">
{event.kind}
</span>
<span className="text-xs font-mono px-1.5 py-0.5 rounded bg-zinc-700/50 text-zinc-300 shrink-0">
{event.role}
</span>
<span className="text-xs font-mono text-orange-200/70 break-words">
{event.text.length > 80 ? event.text.slice(0, 80) + '...' : event.text}
</span>
</div>
</div>
))}
</div>
)}
</div>
</div>
</CollapsibleSection>
{/* Maintenance Panel */}
<div className="relative pt-6">
<div className="absolute top-0 left-0 right-0 flex items-center">
<div className="flex-1 border-t border-dashed border-orange-500/30" />
<span className="px-3 text-xs font-mono text-orange-500/60 uppercase tracking-widest flex items-center gap-2">
<Wrench className="h-3 w-3" />
Maintenance
</span>
<div className="flex-1 border-t border-dashed border-orange-500/30" />
</div>
<div className="bg-black/40 border border-orange-500/20 rounded p-4 space-y-4">
{/* Import Options */}
<div className="flex items-center gap-4 pb-3 border-b border-orange-500/20">
<div className="flex items-center gap-2">
<Switch
id="import-replace"
checked={memoryImportReplace}
onCheckedChange={setMemoryImportReplace}
/>
<Label htmlFor="import-replace" className="text-xs font-mono text-orange-300 cursor-pointer">
Replace on Import
</Label>
</div>
</div>
{/* Prune Settings */}
<div className="grid grid-cols-2 md:grid-cols-5 gap-3">
<div className="space-y-1">
<Label className="text-xs font-mono text-orange-500/60">Max Sessions</Label>
<input
type="number"
min={0}
className="w-full rounded bg-black/60 border border-orange-500/30 px-2 py-1.5 text-xs font-mono text-orange-200 focus:border-orange-500/50 focus:outline-none"
value={memoryPrune.maxSessions}
onChange={(e) => setMemoryPrune({ ...memoryPrune, maxSessions: parseInt(e.target.value || '0', 10) })}
/>
</div>
<div className="space-y-1">
<Label className="text-xs font-mono text-orange-500/60">Max Age (days)</Label>
<input
type="number"
min={0}
className="w-full rounded bg-black/60 border border-orange-500/30 px-2 py-1.5 text-xs font-mono text-orange-200 focus:border-orange-500/50 focus:outline-none"
value={memoryPrune.maxAgeDays}
onChange={(e) => setMemoryPrune({ ...memoryPrune, maxAgeDays: parseInt(e.target.value || '0', 10) })}
/>
</div>
<div className="space-y-1">
<Label className="text-xs font-mono text-orange-500/60">Max Namespaces</Label>
<input
type="number"
min={0}
className="w-full rounded bg-black/60 border border-orange-500/30 px-2 py-1.5 text-xs font-mono text-orange-200 focus:border-orange-500/50 focus:outline-none"
value={memoryPrune.maxNamespaces}
onChange={(e) => setMemoryPrune({ ...memoryPrune, maxNamespaces: parseInt(e.target.value || '0', 10) })}
/>
</div>
<div className="space-y-1">
<Label className="text-xs font-mono text-orange-500/60">Bytes/Session</Label>
<input
type="number"
min={0}
className="w-full rounded bg-black/60 border border-orange-500/30 px-2 py-1.5 text-xs font-mono text-orange-200 focus:border-orange-500/50 focus:outline-none"
value={memoryPrune.maxBytesPerSession}
onChange={(e) => setMemoryPrune({ ...memoryPrune, maxBytesPerSession: parseInt(e.target.value || '0', 10) })}
/>
</div>
<div className="space-y-1">
<Label className="text-xs font-mono text-orange-500/60">Bytes/Namespace</Label>
<input
type="number"
min={0}
className="w-full rounded bg-black/60 border border-orange-500/30 px-2 py-1.5 text-xs font-mono text-orange-200 focus:border-orange-500/50 focus:outline-none"
value={memoryPrune.maxBytesPerNamespace}
onChange={(e) => setMemoryPrune({ ...memoryPrune, maxBytesPerNamespace: parseInt(e.target.value || '0', 10) })}
/>
</div>
</div>
{/* Action Buttons */}
<div className="flex items-center gap-3 pt-2">
<Button
size="sm"
onClick={pruneMemory}
className="gap-1.5 text-xs font-mono bg-amber-600/80 hover:bg-amber-600 text-black border border-amber-500"
>
<Scissors className="h-3 w-3" />
PRUNE NOW
</Button>
<Button
size="sm"
variant="ghost"
onClick={deleteMemorySession}
disabled={!memorySession}
className="gap-1.5 text-xs font-mono bg-red-500/20 hover:bg-red-500/30 text-red-400 border border-red-500/30"
>
<Trash2 className="h-3 w-3" />
Delete Session
</Button>
<Button
size="sm"
variant="ghost"
onClick={deleteAllMemory}
className="gap-1.5 text-xs font-mono bg-red-500/20 hover:bg-red-500/30 text-red-400 border border-red-500/30"
>
<AlertTriangle className="h-3 w-3" />
Purge All Data
</Button>
</div>
</div>
</div>
</>
)}
</div>
</div>
)
}

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