Compare commits

...
Author SHA1 Message Date
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
79 changed files with 7763 additions and 4978 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"
+11 -8
View File
@@ -16,6 +16,8 @@ pgstore/*
gitstore/*
objectstore/*
static/*
!static/icon.png
test/
refs/*
dist/*
.proxypilot/*
@@ -36,6 +38,7 @@ docs/*
!docs/memory.md
!docs/proxypilot.md
!docs/agentic-harness-demo.md
!docs/long-context-features.md
AGENTS.md
CLAUDE.md
GEMINI.md
@@ -55,11 +58,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)
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
+275
View File
@@ -238,6 +238,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 +615,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 +756,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 +864,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 +897,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 {
+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:
-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`).
+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})
}
@@ -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})
}
+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()
+1044 -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
}
-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)
}
+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)
}
-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
}
}
-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"
+14
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.
+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: 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))
}
}
+178 -1172
View File
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,246 @@
import { useState, useEffect } from 'react'
import { Button } from '@/components/ui/button'
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card'
import { Switch } from '@/components/ui/switch'
import { Label } from '@/components/ui/label'
import { Separator } from '@/components/ui/separator'
import { Tooltip, TooltipContent, TooltipTrigger } from '@/components/ui/tooltip'
import {
Play,
Square,
RotateCcw,
FolderOpen,
FileText,
Copy,
Activity,
Settings,
Shield,
Globe,
AlertCircle,
} from 'lucide-react'
import { useProxyContext } from '@/hooks/useProxyContext'
export function EngineControl() {
const { status, loading, handleAction, showToast } = useProxyContext()
const [privateOAuth, setPrivateOAuth] = useState(false)
const isRunning = status?.running ?? false
useEffect(() => {
;(async () => {
try {
if (window.pp_get_oauth_private) {
const priv = await window.pp_get_oauth_private()
setPrivateOAuth(priv)
}
} catch (e) {
console.error('OAuth private error:', e)
}
})()
}, [])
const handlePrivateOAuthChange = async (checked: boolean) => {
try {
if (window.pp_set_oauth_private) {
await window.pp_set_oauth_private(checked)
setPrivateOAuth(checked)
showToast('Preference saved', 'success')
}
} catch (e) {
showToast(e instanceof Error ? e.message : String(e), 'error')
}
}
return (
<Card className="backdrop-blur-sm bg-card/60 border-border/50 shadow-xl">
<CardHeader className="pb-4">
<div className="flex items-center gap-3">
<div className="h-10 w-10 rounded-xl bg-primary/10 flex items-center justify-center">
<Activity className="h-5 w-5 text-primary" />
</div>
<div>
<CardTitle className="text-lg">Engine Control</CardTitle>
<CardDescription>Start, stop or restart the proxy</CardDescription>
</div>
</div>
</CardHeader>
<CardContent className="space-y-5">
{/* Control Buttons */}
<div className="flex gap-3">
<Tooltip>
<TooltipTrigger asChild>
<Button
className={`flex-1 gap-2 transition-all duration-200 font-semibold shadow-md ${
isRunning
? 'opacity-50 cursor-not-allowed'
: 'hover:scale-[1.02] active:scale-[0.98]'
}`}
variant={isRunning ? 'secondary' : 'default'}
disabled={isRunning || loading === 'start'}
onClick={() => handleAction(window.pp_start, 'start', 'Proxy started')}
>
<Play className="h-4 w-4 fill-current" />
Start
</Button>
</TooltipTrigger>
<TooltipContent>Start the proxy server</TooltipContent>
</Tooltip>
<Tooltip>
<TooltipTrigger asChild>
<Button
variant="destructive"
className={`flex-1 gap-2 transition-all duration-200 shadow-md ${
!isRunning
? 'opacity-50 cursor-not-allowed'
: 'hover:bg-destructive/90 hover:scale-[1.02]'
}`}
disabled={!isRunning || loading === 'stop'}
onClick={() => handleAction(window.pp_stop, 'stop', 'Proxy stopped')}
>
<Square className="h-4 w-4 fill-current" />
Stop
</Button>
</TooltipTrigger>
<TooltipContent>Stop the proxy server</TooltipContent>
</Tooltip>
<Tooltip>
<TooltipTrigger asChild>
<Button
variant="outline"
className={`flex-1 gap-2 transition-all duration-200 ${
!isRunning
? 'opacity-50 cursor-not-allowed'
: 'hover:bg-secondary hover:text-secondary-foreground'
}`}
disabled={!isRunning || loading === 'restart'}
onClick={() => handleAction(window.pp_restart, 'restart', 'Proxy restarted')}
>
<RotateCcw className="h-4 w-4" />
Restart
</Button>
</TooltipTrigger>
<TooltipContent>Restart the proxy server</TooltipContent>
</Tooltip>
</div>
{/* Status Info */}
<div className="rounded-xl bg-muted/50 border border-border/50 p-4 space-y-3">
<div className="flex items-center justify-between">
<span className="text-sm text-muted-foreground flex items-center gap-2">
<Globe className="h-4 w-4" /> Base URL
</span>
<code className="text-sm font-mono text-primary bg-background/50 px-2 py-1 rounded border border-border/50">
{status?.base_url || '—'}
</code>
</div>
<Separator className="bg-border/50" />
<div className="flex items-center justify-between">
<span className="text-sm text-muted-foreground">Port</span>
<span className="text-sm font-mono">{status?.port || '—'}</span>
</div>
{status?.last_error && (
<>
<Separator className="bg-border/50" />
<div className="flex items-start gap-2 text-destructive">
<AlertCircle className="h-4 w-4 mt-0.5 flex-shrink-0" />
<span className="text-sm break-all font-medium">{status.last_error}</span>
</div>
</>
)}
</div>
{/* Quick Actions */}
<div className="flex flex-wrap gap-2">
<Tooltip>
<TooltipTrigger asChild>
<Button
variant="ghost"
size="sm"
className="gap-2 text-muted-foreground hover:text-foreground"
onClick={() => handleAction(window.pp_open_logs, 'logs', 'Opened logs')}
>
<FileText className="h-4 w-4" /> Logs
</Button>
</TooltipTrigger>
<TooltipContent>View log files</TooltipContent>
</Tooltip>
<Tooltip>
<TooltipTrigger asChild>
<Button
variant="ghost"
size="sm"
className="gap-2 text-muted-foreground hover:text-foreground"
onClick={() => handleAction(window.pp_open_auth_folder, 'auth', 'Opened auth folder')}
>
<FolderOpen className="h-4 w-4" /> Auth Folder
</Button>
</TooltipTrigger>
<TooltipContent>Open authentication files folder</TooltipContent>
</Tooltip>
<Tooltip>
<TooltipTrigger asChild>
<Button
variant="ghost"
size="sm"
className="gap-2 text-muted-foreground hover:text-foreground"
onClick={() => handleAction(window.pp_copy_diagnostics, 'diag-copy', 'Copied to clipboard')}
>
<Copy className="h-4 w-4" /> Copy Diagnostics
</Button>
</TooltipTrigger>
<TooltipContent>Copy diagnostics to clipboard</TooltipContent>
</Tooltip>
<Tooltip>
<TooltipTrigger asChild>
<Button
variant="ghost"
size="sm"
className="gap-2 text-muted-foreground hover:text-foreground"
disabled={!isRunning}
onClick={() => handleAction(window.pp_open_diagnostics, 'diag-open', 'Opened diagnostics')}
>
<Activity className="h-4 w-4" /> Diagnostics
</Button>
</TooltipTrigger>
<TooltipContent>Open diagnostics panel</TooltipContent>
</Tooltip>
<Tooltip>
<TooltipTrigger asChild>
<Button
variant="ghost"
size="sm"
className="gap-2 text-muted-foreground hover:text-foreground"
disabled={!isRunning}
onClick={() => handleAction(window.pp_open_legacy_ui, 'legacy', 'Opened advanced UI')}
>
<Settings className="h-4 w-4" /> Advanced
</Button>
</TooltipTrigger>
<TooltipContent>Open advanced management UI</TooltipContent>
</Tooltip>
</div>
{/* Private OAuth Toggle */}
<div className="flex items-center justify-between rounded-lg bg-muted/30 border border-border/50 p-3">
<div className="flex items-center gap-3">
<Shield className="h-5 w-5 text-muted-foreground" />
<Label htmlFor="private-oauth" className="text-sm cursor-pointer">
Open OAuth in private window
</Label>
</div>
<Switch
id="private-oauth"
checked={privateOAuth}
onCheckedChange={handlePrivateOAuthChange}
/>
</div>
</CardContent>
</Card>
)
}
@@ -0,0 +1,118 @@
import { useEffect, useState } from 'react'
import { Button } from '@/components/ui/button'
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card'
import { Terminal, Check, X } from 'lucide-react'
import { useProxyContext } from '@/hooks/useProxyContext'
interface Integration {
id: string
name?: string
detected: boolean
binary_path?: string
config_path?: string
}
export function Integrations() {
const { mgmtKey, mgmtFetch, showToast } = useProxyContext()
const [integrations, setIntegrations] = useState<Integration[]>([])
const [loading, setLoading] = useState(false)
const fetchIntegrations = async () => {
if (!mgmtKey) return
setLoading(true)
try {
const data = await mgmtFetch('/v0/management/integrations/status')
setIntegrations(data.integrations || [])
} catch (e) {
console.error('Failed to fetch integrations:', e)
}
setLoading(false)
}
const configureIntegration = async (integrationId: string) => {
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')
}
}
useEffect(() => {
if (mgmtKey) fetchIntegrations()
}, [mgmtKey])
return (
<Card className="backdrop-blur-sm bg-card/60 border-border/50 shadow-xl">
<CardHeader className="pb-4">
<div className="flex items-center gap-3">
<div className="h-10 w-10 rounded-xl bg-green-500/10 flex items-center justify-center">
<Terminal className="h-5 w-5 text-green-500" />
</div>
<div className="flex-1">
<CardTitle className="text-lg">CLI Agent Integrations</CardTitle>
<CardDescription>Detected AI coding assistants</CardDescription>
</div>
<Button
size="sm"
variant="outline"
onClick={() => fetchIntegrations()}
disabled={loading}
>
{loading ? 'Loading...' : 'Refresh'}
</Button>
</div>
</CardHeader>
<CardContent>
<div className="grid gap-4 md:grid-cols-2 lg:grid-cols-4">
{integrations.length === 0 && !loading && (
<div className="col-span-full text-center text-sm text-muted-foreground py-4">
No integrations detected. Click Refresh to scan for CLI agents.
</div>
)}
{integrations.map((integration) => (
<div
key={integration.id}
className="rounded-lg border border-border/50 bg-muted/30 p-4 space-y-3"
>
<div className="flex items-center justify-between">
<span className="font-medium text-sm">{integration.name || integration.id}</span>
{integration.detected ? (
<Check className="h-5 w-5 text-green-500" />
) : (
<X className="h-5 w-5 text-muted-foreground" />
)}
</div>
{integration.detected && (
<>
{integration.binary_path && (
<div className="text-xs text-muted-foreground truncate" title={integration.binary_path}>
<span className="font-mono">Binary:</span> {integration.binary_path}
</div>
)}
{integration.config_path && (
<div className="text-xs text-muted-foreground truncate" title={integration.config_path}>
<span className="font-mono">Config:</span> {integration.config_path}
</div>
)}
</>
)}
<Button
size="sm"
variant="outline"
className="w-full"
disabled={!integration.detected}
onClick={() => configureIntegration(integration.id)}
>
Configure
</Button>
</div>
))}
</div>
</CardContent>
</Card>
)
}
@@ -0,0 +1,119 @@
import { useState } from 'react'
import { useProxyContext } from '@/hooks/useProxyContext'
import { Card, CardHeader, CardTitle, CardDescription, CardContent } from '@/components/ui/card'
import { Button } from '@/components/ui/button'
import { Copy, FileText } from 'lucide-react'
export function LogsViewer() {
const { mgmtFetch, showToast } = useProxyContext()
const [diagnostics, setDiagnostics] = useState('')
const [logText, setLogText] = useState('')
const [activeLogType, setActiveLogType] = useState<'stdout' | 'stderr'>('stdout')
const loadDiagnostics = async () => {
try {
const res = await mgmtFetch('/v0/management/proxypilot/diagnostics?lines=120')
setDiagnostics(res.text || '')
} catch (e) {
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 = 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) {
showToast(e instanceof Error ? e.message : String(e), 'error')
}
}
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 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>
<CardTitle className="text-lg">Diagnostics</CardTitle>
<CardDescription>Engine snapshot</CardDescription>
</div>
</div>
</CardHeader>
<CardContent className="space-y-3">
<div className="flex gap-2">
<Button size="sm" variant="outline" onClick={loadDiagnostics}>
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">
{diagnostics || 'No diagnostics loaded.'}
</pre>
</CardContent>
</Card>
{/* Logs Tail Card */}
<Card className="backdrop-blur-sm bg-card/60 border-border/50 shadow-xl">
<CardHeader className="pb-4">
<div className="flex items-center gap-3">
<div className="h-10 w-10 rounded-xl bg-green-500/10 flex items-center justify-center">
<FileText className="h-5 w-5 text-green-500" />
</div>
<div>
<CardTitle className="text-lg">Logs (tail)</CardTitle>
<CardDescription>Engine stdout/stderr</CardDescription>
</div>
</div>
</CardHeader>
<CardContent className="space-y-3">
<div className="flex gap-2">
<Button
size="sm"
variant={activeLogType === 'stdout' ? 'default' : 'outline'}
onClick={() => tailLogs('stdout')}
>
Stdout
</Button>
<Button
size="sm"
variant={activeLogType === 'stderr' ? 'default' : 'outline'}
onClick={() => tailLogs('stderr')}
>
Stderr
</Button>
</div>
<pre className="max-h-48 overflow-auto rounded-md border border-border/50 bg-muted/30 p-2 text-xs">
{logText || 'No logs loaded.'}
</pre>
</CardContent>
</Card>
</div>
)
}
@@ -0,0 +1,641 @@
import { useState, useEffect, useCallback } from 'react'
import { Button } from '@/components/ui/button'
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card'
import { Switch } from '@/components/ui/switch'
import { Label } from '@/components/ui/label'
import { Separator } from '@/components/ui/separator'
import {
Database,
Download,
Upload,
Trash2,
RefreshCw,
Save,
FileText,
Anchor,
ListTodo,
Pin,
Scissors,
Brain,
} from 'lucide-react'
import { useProxyContext } from '@/hooks/useProxyContext'
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;
}
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);
};
export function MemoryManager() {
const { mgmtKey, mgmtFetch, showToast } = 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
const [memoryEvents, setMemoryEvents] = useState('')
const [memoryAnchors, setMemoryAnchors] = useState('')
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,
})
// 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) {
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) {
showToast(e instanceof Error ? e.message : String(e), 'error')
}
}, [memorySession, mgmtFetch, showToast])
// Load events
const loadMemoryEvents = useCallback(async () => {
if (!memorySession) {
setMemoryEvents('Select a session.')
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('No events.')
return
}
const lines = events.map((e: any) => {
const ts = e.ts || ''
const kind = e.kind || ''
const role = e.role || ''
const text = (e.text || '').toString()
return `[${ts}][${kind}][${role}] ${text}`
})
setMemoryEvents(lines.join('\n\n'))
} catch (e) {
showToast(e instanceof Error ? e.message : String(e), 'error')
}
}, [memorySession, memoryEventsLimit, mgmtFetch, showToast])
// Load anchors
const loadMemoryAnchors = useCallback(async () => {
if (!memorySession) {
setMemoryAnchors('Select a session.')
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('No anchors.')
return
}
const lines = anchors.map((a: any) => {
const ts = a.ts || ''
const summary = (a.summary || '').toString()
return `[${ts}]\n${summary}`
})
setMemoryAnchors(lines.join('\n\n---\n\n'))
} catch (e) {
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, `proxypilot-session-${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, 'proxypilot-memory-all.zip')
} catch (e) {
showToast(e instanceof Error ? e.message : String(e), 'error')
}
}
// Delete all sessions
const deleteAllMemory = async () => {
if (!mgmtKey) return
if (!window.confirm('Delete all memory data? This cannot be undone.')) return
try {
await mgmtFetch('/v0/management/memory/delete-all?confirm=true', { method: 'POST' })
setMemorySession('')
setMemoryDetails(null)
setMemoryEvents('')
setMemoryAnchors('')
await loadMemorySessions()
showToast('Deleted all memory', '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('Imported session', '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('Prune completed', 'success')
} catch (e) {
showToast(e instanceof Error ? e.message : String(e), 'error')
}
}
// Load sessions on mount
useEffect(() => {
if (mgmtKey) {
loadMemorySessions()
}
}, [mgmtKey, loadMemorySessions])
if (!mgmtKey) {
return null
}
return (
<Card className="backdrop-blur-sm bg-card/60 border-border/50 shadow-xl">
<CardHeader className="pb-4">
<div className="flex items-center gap-3">
<div className="h-10 w-10 rounded-xl bg-violet-500/10 flex items-center justify-center">
<Database className="h-5 w-5 text-violet-500" />
</div>
<div>
<CardTitle className="text-lg">Memory</CardTitle>
<CardDescription>Anchors, pinned context, TODOs, and session history</CardDescription>
</div>
</div>
</CardHeader>
<CardContent className="space-y-4">
{/* Action buttons */}
<div className="flex flex-wrap gap-2">
<Button size="sm" variant="outline" onClick={loadMemorySessions} className="gap-1">
<RefreshCw className="h-3 w-3" />
Refresh sessions
</Button>
<Button size="sm" variant="outline" onClick={loadMemorySessionDetails} className="gap-1">
<FileText className="h-3 w-3" />
Load session
</Button>
<Button size="sm" variant="outline" onClick={loadMemoryEvents} className="gap-1">
<FileText className="h-3 w-3" />
Events
</Button>
<Button size="sm" variant="outline" onClick={loadMemoryAnchors} className="gap-1">
<Anchor className="h-3 w-3" />
Anchors
</Button>
<Button size="sm" variant="outline" onClick={exportMemorySession} className="gap-1">
<Download className="h-3 w-3" />
Export
</Button>
<Button size="sm" variant="outline" onClick={exportAllMemory} className="gap-1">
<Download className="h-3 w-3" />
Export all
</Button>
<Button size="sm" variant="destructive" onClick={deleteMemorySession} className="gap-1">
<Trash2 className="h-3 w-3" />
Delete
</Button>
<Button size="sm" variant="destructive" onClick={deleteAllMemory} className="gap-1">
<Trash2 className="h-3 w-3" />
Delete all
</Button>
</div>
{/* Session selector and limits */}
<div className="flex flex-wrap items-center gap-2">
<select
className="min-w-[220px] rounded-md border border-border bg-background/60 px-2 py-1 text-xs font-mono"
value={memorySession}
onChange={(e) => setMemorySession(e.target.value)}
>
{memorySessions.length === 0 && <option value="">(no sessions)</option>}
{memorySessions.map((s) => (
<option key={s.key} value={s.key}>
{s.key}
</option>
))}
</select>
<div className="flex items-center gap-1">
<Label htmlFor="events-limit" className="text-xs text-muted-foreground">Events:</Label>
<input
id="events-limit"
type="number"
min={10}
max={500}
className="w-20 rounded-md border border-border bg-background/60 px-2 py-1 text-xs"
value={memoryEventsLimit}
onChange={(e) => setMemoryEventsLimit(parseInt(e.target.value || '120', 10))}
/>
</div>
<div className="flex items-center gap-1">
<Label htmlFor="anchors-limit" className="text-xs text-muted-foreground">Anchors:</Label>
<input
id="anchors-limit"
type="number"
min={5}
max={200}
className="w-20 rounded-md border border-border bg-background/60 px-2 py-1 text-xs"
value={memoryAnchorsLimit}
onChange={(e) => setMemoryAnchorsLimit(parseInt(e.target.value || '20', 10))}
/>
</div>
<span className="text-xs text-muted-foreground">
{memoryDetails?.updated_at ? `Updated ${memoryDetails.updated_at}` : 'No session loaded.'}
</span>
<div className="flex items-center gap-2">
<Brain className="h-4 w-4 text-muted-foreground" />
<Label htmlFor="semantic-toggle" className="text-xs text-muted-foreground cursor-pointer">
Semantic
</Label>
<Switch
id="semantic-toggle"
checked={memorySemanticEnabled}
disabled={!memorySession}
onCheckedChange={toggleMemorySemantic}
/>
</div>
</div>
<Separator className="bg-border/50" />
{/* Session details: Summary, Pinned, TODO */}
<div className="grid gap-4 lg:grid-cols-3">
<div className="space-y-2">
<div className="flex items-center gap-2 text-xs text-muted-foreground">
<Anchor className="h-3 w-3" />
Anchor summary
</div>
<textarea
className="h-40 w-full rounded-md border border-border bg-background/60 px-2 py-1 text-xs font-mono resize-y"
value={memorySummary}
onChange={(e) => setMemorySummary(e.target.value)}
placeholder="Session anchor summary..."
/>
<Button size="sm" variant="outline" onClick={saveMemorySummary} className="gap-1">
<Save className="h-3 w-3" />
Save summary
</Button>
</div>
<div className="space-y-2">
<div className="flex items-center gap-2 text-xs text-muted-foreground">
<Pin className="h-3 w-3" />
Pinned context
</div>
<textarea
className="h-40 w-full rounded-md border border-border bg-background/60 px-2 py-1 text-xs font-mono resize-y"
value={memoryPinned}
onChange={(e) => setMemoryPinned(e.target.value)}
placeholder="Pinned context that persists across turns..."
/>
<Button size="sm" variant="outline" onClick={saveMemoryPinned} className="gap-1">
<Save className="h-3 w-3" />
Save pinned
</Button>
</div>
<div className="space-y-2">
<div className="flex items-center gap-2 text-xs text-muted-foreground">
<ListTodo className="h-3 w-3" />
TODO
</div>
<textarea
className="h-40 w-full rounded-md border border-border bg-background/60 px-2 py-1 text-xs font-mono resize-y"
value={memoryTodo}
onChange={(e) => setMemoryTodo(e.target.value)}
placeholder="Session TODO list..."
/>
<Button size="sm" variant="outline" onClick={saveMemoryTodo} className="gap-1">
<Save className="h-3 w-3" />
Save TODO
</Button>
</div>
</div>
<Separator className="bg-border/50" />
{/* Events and Anchors display */}
<div className="grid gap-4 lg:grid-cols-2">
<div className="space-y-2">
<div className="flex items-center gap-2 text-xs text-muted-foreground">
<FileText className="h-3 w-3" />
Events
</div>
<pre className="max-h-56 overflow-auto rounded-md border border-border/50 bg-muted/30 p-2 text-xs whitespace-pre-wrap">
{memoryEvents || 'No events loaded.'}
</pre>
</div>
<div className="space-y-2">
<div className="flex items-center gap-2 text-xs text-muted-foreground">
<Anchor className="h-3 w-3" />
Anchors
</div>
<pre className="max-h-56 overflow-auto rounded-md border border-border/50 bg-muted/30 p-2 text-xs whitespace-pre-wrap">
{memoryAnchors || 'No anchors loaded.'}
</pre>
</div>
</div>
<Separator className="bg-border/50" />
{/* Import and Prune */}
<div className="grid gap-4 lg:grid-cols-2">
{/* Import section */}
<div className="space-y-2 p-3 rounded-lg bg-muted/30 border border-border/50">
<div className="flex items-center gap-2 text-xs text-muted-foreground">
<Upload className="h-3 w-3" />
Import session (zip)
</div>
<div className="flex flex-wrap items-center gap-2">
<input
type="file"
accept=".zip"
className="text-xs file:mr-2 file:py-1 file:px-2 file:rounded file:border-0 file:text-xs file:bg-primary file:text-primary-foreground hover:file:bg-primary/90"
onChange={(e) => importMemorySession(e.target.files?.[0] || null)}
/>
<div className="flex items-center gap-2">
<Switch
id="import-replace"
checked={memoryImportReplace}
onCheckedChange={setMemoryImportReplace}
/>
<Label htmlFor="import-replace" className="text-xs text-muted-foreground cursor-pointer">
Replace existing
</Label>
</div>
</div>
</div>
{/* Prune section */}
<div className="space-y-2 p-3 rounded-lg bg-muted/30 border border-border/50">
<div className="flex items-center gap-2 text-xs text-muted-foreground">
<Scissors className="h-3 w-3" />
Prune limits
</div>
<div className="grid grid-cols-2 gap-2">
<div className="space-y-1">
<Label htmlFor="prune-age" className="text-xs text-muted-foreground">Max age (days)</Label>
<input
id="prune-age"
type="number"
min={0}
className="w-full rounded-md border border-border bg-background/60 px-2 py-1 text-xs"
value={memoryPrune.maxAgeDays}
onChange={(e) => setMemoryPrune({ ...memoryPrune, maxAgeDays: parseInt(e.target.value || '0', 10) })}
/>
</div>
<div className="space-y-1">
<Label htmlFor="prune-sessions" className="text-xs text-muted-foreground">Max sessions</Label>
<input
id="prune-sessions"
type="number"
min={0}
className="w-full rounded-md border border-border bg-background/60 px-2 py-1 text-xs"
value={memoryPrune.maxSessions}
onChange={(e) => setMemoryPrune({ ...memoryPrune, maxSessions: parseInt(e.target.value || '0', 10) })}
/>
</div>
<div className="space-y-1">
<Label htmlFor="prune-namespaces" className="text-xs text-muted-foreground">Max namespaces</Label>
<input
id="prune-namespaces"
type="number"
min={0}
className="w-full rounded-md border border-border bg-background/60 px-2 py-1 text-xs"
value={memoryPrune.maxNamespaces}
onChange={(e) => setMemoryPrune({ ...memoryPrune, maxNamespaces: parseInt(e.target.value || '0', 10) })}
/>
</div>
<div className="space-y-1">
<Label htmlFor="prune-bytes-session" className="text-xs text-muted-foreground">Max bytes/session</Label>
<input
id="prune-bytes-session"
type="number"
min={0}
className="w-full rounded-md border border-border bg-background/60 px-2 py-1 text-xs"
value={memoryPrune.maxBytesPerSession}
onChange={(e) => setMemoryPrune({ ...memoryPrune, maxBytesPerSession: parseInt(e.target.value || '0', 10) })}
/>
</div>
<div className="space-y-1 col-span-2">
<Label htmlFor="prune-bytes-namespace" className="text-xs text-muted-foreground">Max bytes/namespace</Label>
<input
id="prune-bytes-namespace"
type="number"
min={0}
className="w-full rounded-md border border-border bg-background/60 px-2 py-1 text-xs"
value={memoryPrune.maxBytesPerNamespace}
onChange={(e) => setMemoryPrune({ ...memoryPrune, maxBytesPerNamespace: parseInt(e.target.value || '0', 10) })}
/>
</div>
</div>
<Button size="sm" variant="outline" onClick={pruneMemory} className="gap-1 mt-2">
<Scissors className="h-3 w-3" />
Prune memory
</Button>
</div>
</div>
</CardContent>
</Card>
)
}
@@ -0,0 +1,313 @@
import { useState, useEffect } from 'react'
import { useProxyContext } from '@/hooks/useProxyContext'
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card'
import { Button } from '@/components/ui/button'
import { ArrowRight, Plus, Trash2, Pencil, Check, X } from 'lucide-react'
interface ModelMapping {
from: string
to: string
provider: string
}
export function ModelMappings() {
const { mgmtKey, mgmtFetch, showToast } = useProxyContext()
const [modelMappings, setModelMappings] = useState<ModelMapping[]>([])
const [newMapping, setNewMapping] = useState<ModelMapping>({ from: '', to: '', provider: '' })
const [editingMapping, setEditingMapping] = useState<number | null>(null)
const [editingMappingValues, setEditingMappingValues] = useState<ModelMapping>({ from: '', to: '', provider: '' })
const [mappingTestInput, setMappingTestInput] = useState('')
const [mappingTestResult, setMappingTestResult] = useState('')
const fetchModelMappings = async () => {
try {
const data = await mgmtFetch('/v0/management/model-mappings')
setModelMappings(data.mappings || [])
} catch (e) {
console.error('Failed to fetch model mappings:', e)
}
}
const addModelMapping = async () => {
if (!newMapping.from.trim() || !newMapping.to.trim()) {
showToast('Both "from" and "to" fields are required', 'error')
return
}
try {
await mgmtFetch('/v0/management/model-mappings', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(newMapping),
})
setNewMapping({ from: '', to: '', provider: '' })
await fetchModelMappings()
showToast('Mapping added', 'success')
} catch (e) {
showToast(e instanceof Error ? e.message : String(e), 'error')
}
}
const updateModelMapping = async (index: number, mapping: ModelMapping) => {
try {
await mgmtFetch(`/v0/management/model-mappings/${index}`, {
method: 'PUT',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(mapping),
})
setEditingMapping(null)
await fetchModelMappings()
showToast('Mapping updated', 'success')
} catch (e) {
showToast(e instanceof Error ? e.message : String(e), 'error')
}
}
const deleteModelMapping = async (index: number) => {
try {
await mgmtFetch(`/v0/management/model-mappings/${index}`, {
method: 'DELETE',
})
await fetchModelMappings()
showToast('Mapping deleted', 'success')
} catch (e) {
showToast(e instanceof Error ? e.message : String(e), 'error')
}
}
const testModelMapping = async () => {
if (!mappingTestInput.trim()) {
setMappingTestResult('Enter a model name to test')
return
}
try {
const data = await mgmtFetch(`/v0/management/model-mappings/test?model=${encodeURIComponent(mappingTestInput.trim())}`)
setMappingTestResult(JSON.stringify(data, null, 2))
} catch (e) {
setMappingTestResult(e instanceof Error ? e.message : String(e))
}
}
useEffect(() => {
if (mgmtKey) {
fetchModelMappings()
}
}, [mgmtKey])
if (!mgmtKey) {
return null
}
return (
<Card className="backdrop-blur-sm bg-card/60 border-border/50 shadow-xl">
<CardHeader className="pb-4">
<div className="flex items-center gap-3">
<div className="h-10 w-10 rounded-xl bg-orange-500/10 flex items-center justify-center">
<ArrowRight className="h-5 w-5 text-orange-500" />
</div>
<div className="flex-1">
<CardTitle className="text-lg">Global Model Mappings</CardTitle>
<CardDescription>Rewrite model names before routing</CardDescription>
</div>
<Button
size="sm"
variant="outline"
onClick={() => fetchModelMappings().catch((e) => showToast(String(e), 'error'))}
>
Refresh
</Button>
</div>
</CardHeader>
<CardContent className="space-y-4">
{/* Add new mapping form */}
<div className="flex flex-wrap items-end gap-3 p-3 rounded-lg bg-muted/30 border border-border/50">
<div className="flex-1 min-w-[150px] space-y-1">
<label className="text-xs text-muted-foreground">From model</label>
<input
type="text"
className="w-full rounded-md border border-border bg-background/60 px-2 py-1 text-xs font-mono"
placeholder="gpt-4"
value={newMapping.from}
onChange={(e) => setNewMapping({ ...newMapping, from: e.target.value })}
/>
</div>
<div className="flex-1 min-w-[150px] space-y-1">
<label className="text-xs text-muted-foreground">To model</label>
<input
type="text"
className="w-full rounded-md border border-border bg-background/60 px-2 py-1 text-xs font-mono"
placeholder="claude-3-opus"
value={newMapping.to}
onChange={(e) => setNewMapping({ ...newMapping, to: e.target.value })}
/>
</div>
<div className="flex-1 min-w-[120px] space-y-1">
<label className="text-xs text-muted-foreground">Provider (optional)</label>
<input
type="text"
className="w-full rounded-md border border-border bg-background/60 px-2 py-1 text-xs font-mono"
placeholder="anthropic"
value={newMapping.provider}
onChange={(e) => setNewMapping({ ...newMapping, provider: e.target.value })}
/>
</div>
<Button
size="sm"
variant="default"
onClick={() => addModelMapping().catch((e) => showToast(String(e), 'error'))}
className="gap-1"
>
<Plus className="h-4 w-4" />
Add
</Button>
</div>
{/* Mappings list */}
<div className="max-h-64 overflow-auto rounded-md border border-border/50">
<table className="w-full text-xs">
<thead className="bg-muted/40 sticky top-0">
<tr>
<th className="px-3 py-2 text-left">From</th>
<th className="px-3 py-2 text-left"></th>
<th className="px-3 py-2 text-left">To</th>
<th className="px-3 py-2 text-left">Provider</th>
<th className="px-3 py-2 text-left">Actions</th>
</tr>
</thead>
<tbody>
{modelMappings.length === 0 && (
<tr>
<td className="px-3 py-3 text-muted-foreground" colSpan={5}>
No model mappings configured.
</td>
</tr>
)}
{modelMappings.map((mapping, index) => (
<tr key={index} className="border-t border-border/40">
{editingMapping === index ? (
<>
<td className="px-3 py-2">
<input
type="text"
className="w-full rounded-md border border-border bg-background/60 px-2 py-1 text-xs font-mono"
value={editingMappingValues.from}
onChange={(e) => setEditingMappingValues(v => ({ ...v, from: e.target.value }))}
/>
</td>
<td className="px-3 py-2 text-muted-foreground">
<ArrowRight className="h-4 w-4" />
</td>
<td className="px-3 py-2">
<input
type="text"
className="w-full rounded-md border border-border bg-background/60 px-2 py-1 text-xs font-mono"
value={editingMappingValues.to}
onChange={(e) => setEditingMappingValues(v => ({ ...v, to: e.target.value }))}
/>
</td>
<td className="px-3 py-2">
<input
type="text"
className="w-full rounded-md border border-border bg-background/60 px-2 py-1 text-xs font-mono"
value={editingMappingValues.provider}
onChange={(e) => setEditingMappingValues(v => ({ ...v, provider: e.target.value }))}
/>
</td>
<td className="px-3 py-2">
<div className="flex gap-1">
<Button
size="sm"
variant="ghost"
className="h-7 w-7 p-0"
onClick={() => {
updateModelMapping(index, editingMappingValues)
.catch((e) => showToast(String(e), 'error'))
}}
>
<Check className="h-4 w-4 text-green-500" />
</Button>
<Button
size="sm"
variant="ghost"
className="h-7 w-7 p-0"
onClick={() => setEditingMapping(null)}
>
<X className="h-4 w-4" />
</Button>
</div>
</td>
</>
) : (
<>
<td className="px-3 py-2 font-mono">{mapping.from}</td>
<td className="px-3 py-2 text-muted-foreground">
<ArrowRight className="h-4 w-4" />
</td>
<td className="px-3 py-2 font-mono">{mapping.to}</td>
<td className="px-3 py-2 font-mono text-muted-foreground">
{mapping.provider || '-'}
</td>
<td className="px-3 py-2">
<div className="flex gap-1">
<Button
size="sm"
variant="ghost"
className="h-7 w-7 p-0"
onClick={() => {
setEditingMapping(index)
setEditingMappingValues({
from: mapping.from || '',
to: mapping.to || '',
provider: mapping.provider || '',
})
}}
>
<Pencil className="h-4 w-4" />
</Button>
<Button
size="sm"
variant="ghost"
className="h-7 w-7 p-0 text-destructive hover:text-destructive"
onClick={() => deleteModelMapping(index).catch((e) => showToast(String(e), 'error'))}
>
<Trash2 className="h-4 w-4" />
</Button>
</div>
</td>
</>
)}
</tr>
))}
</tbody>
</table>
</div>
{/* Test mapping */}
<div className="flex flex-wrap items-end gap-3 p-3 rounded-lg bg-muted/30 border border-border/50">
<div className="flex-1 min-w-[200px] space-y-1">
<label className="text-xs text-muted-foreground">Test model name</label>
<input
type="text"
className="w-full rounded-md border border-border bg-background/60 px-2 py-1 text-xs font-mono"
placeholder="Enter model to test mapping"
value={mappingTestInput}
onChange={(e) => setMappingTestInput(e.target.value)}
/>
</div>
<Button
size="sm"
variant="outline"
onClick={() => testModelMapping().catch((e) => showToast(String(e), 'error'))}
>
Test
</Button>
</div>
{mappingTestResult && (
<pre className="max-h-24 overflow-auto rounded-md border border-border/50 bg-muted/30 p-2 text-xs">
{mappingTestResult}
</pre>
)}
</CardContent>
</Card>
)
}
@@ -0,0 +1,73 @@
import { Button } from '@/components/ui/button'
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card'
import { Key, ExternalLink, Info } from 'lucide-react'
import { useProxyContext } from '@/hooks/useProxyContext'
const providers = [
{ id: 'antigravity', name: 'Antigravity', color: 'from-purple-500 to-indigo-600' },
{ id: 'gemini-cli', name: 'Gemini CLI', color: 'from-blue-500 to-cyan-500' },
{ id: 'codex', name: 'Codex', color: 'from-green-500 to-emerald-600' },
{ id: 'claude', name: 'Claude', color: 'from-orange-500 to-amber-500' },
{ id: 'qwen', name: 'Qwen', color: 'from-pink-500 to-rose-500' },
{ id: 'iflow', name: 'iFlow', color: 'from-teal-500 to-cyan-600' },
]
export function ProviderLogins() {
const { loading, setLoading, showToast } = useProxyContext()
const handleOAuth = async (provider: string) => {
setLoading(`oauth-${provider}`)
try {
if (window.pp_oauth) {
await window.pp_oauth(provider)
showToast(`Opening ${provider} login...`, 'success')
}
} catch (e) {
showToast(e instanceof Error ? e.message : String(e), 'error')
} finally {
setLoading(null)
}
}
return (
<Card className="backdrop-blur-sm bg-card/60 border-border/50 shadow-xl">
<CardHeader className="pb-4">
<div className="flex items-center gap-3">
<div className="h-10 w-10 rounded-xl bg-purple-500/10 flex items-center justify-center">
<Key className="h-5 w-5 text-purple-500" />
</div>
<div>
<CardTitle className="text-lg">Provider Logins</CardTitle>
<CardDescription>Authenticate with AI providers</CardDescription>
</div>
</div>
</CardHeader>
<CardContent className="space-y-4">
<div className="grid grid-cols-2 gap-3">
{providers.map((provider) => (
<Button
key={provider.id}
variant="outline"
className={`relative overflow-hidden transition-all duration-300 hover:border-primary/50 group h-12 bg-background/50`}
onClick={() => handleOAuth(provider.id)}
disabled={loading === `oauth-${provider.id}`}
>
<div className={`absolute inset-0 bg-gradient-to-r ${provider.color} opacity-0 group-hover:opacity-10 transition-opacity duration-300`} />
<span className="relative flex items-center gap-2 font-medium">
<ExternalLink className="h-4 w-4 opacity-50 group-hover:opacity-100 transition-opacity" />
{provider.name}
</span>
</Button>
))}
</div>
<div className="flex items-start gap-3 rounded-lg bg-blue-500/5 border border-blue-500/10 p-4">
<Info className="h-5 w-5 text-blue-500 mt-0.5 flex-shrink-0" />
<p className="text-sm text-muted-foreground leading-relaxed">
Click a provider to open their login flow in your browser. Authentication tokens will be saved locally for ProxyPilot to use.
</p>
</div>
</CardContent>
</Card>
)
}
@@ -0,0 +1,168 @@
import { useState, useEffect, useCallback } from 'react'
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card'
import { Button } from '@/components/ui/button'
import { Badge } from '@/components/ui/badge'
import { useProxyContext } from '@/hooks/useProxyContext'
interface SemanticHealthData {
status?: string
model?: string
version?: string
model_present?: boolean
latency_ms?: number
queue?: {
queued: number
dropped: number
processed: number
failed: number
}
}
interface SemanticNamespace {
key: string
label?: string
}
interface SemanticItem {
ts?: string
source?: string
role?: string
text?: string
}
export function SemanticMemory() {
const { mgmtKey, mgmtFetch, showToast } = useProxyContext()
const [semanticHealth, setSemanticHealth] = useState<SemanticHealthData | null>(null)
const [semanticNamespaces, setSemanticNamespaces] = useState<SemanticNamespace[]>([])
const [semanticNamespace, setSemanticNamespace] = useState('')
const [semanticLimit, setSemanticLimit] = useState(50)
const [semanticItems, setSemanticItems] = useState('')
const loadSemanticHealth = useCallback(async () => {
try {
const res = await mgmtFetch('/v0/management/semantic/health')
setSemanticHealth(res)
} catch (e) {
showToast(e instanceof Error ? e.message : String(e), 'error')
}
}, [mgmtFetch, showToast])
const loadSemanticNamespaces = useCallback(async () => {
try {
const res = await mgmtFetch('/v0/management/semantic/namespaces')
const namespaces = res.namespaces || []
setSemanticNamespaces(namespaces)
if (!semanticNamespace && namespaces.length > 0) {
setSemanticNamespace(namespaces[0].key)
}
} catch (e) {
showToast(e instanceof Error ? e.message : String(e), 'error')
}
}, [mgmtFetch, showToast, semanticNamespace])
const loadSemanticItems = useCallback(async () => {
if (!semanticNamespace) {
setSemanticItems('Select a namespace.')
return
}
try {
const res = await mgmtFetch(
`/v0/management/semantic/items?namespace=${encodeURIComponent(semanticNamespace)}&limit=${encodeURIComponent(semanticLimit)}`
)
const items: SemanticItem[] = res.items || []
if (!Array.isArray(items) || items.length === 0) {
setSemanticItems('No items.')
return
}
const lines = items.map((it) => {
const ts = it.ts || ''
const src = it.source || ''
const role = it.role || ''
const text = (it.text || '').toString()
return `[${ts}][${src}][${role}] ${text}`
})
setSemanticItems(lines.join('\n\n'))
} catch (e) {
showToast(e instanceof Error ? e.message : String(e), 'error')
}
}, [mgmtFetch, showToast, semanticNamespace, semanticLimit])
// Load initial data when mgmtKey is available
useEffect(() => {
if (mgmtKey) {
loadSemanticHealth()
loadSemanticNamespaces()
}
}, [mgmtKey, loadSemanticHealth, loadSemanticNamespaces])
if (!mgmtKey) {
return null
}
return (
<Card className="backdrop-blur-sm bg-card/60 border-border/50 shadow-xl">
<CardHeader className="pb-4">
<CardTitle className="text-lg">Semantic Memory</CardTitle>
<CardDescription>Ollama embeddings + per-repo namespaces</CardDescription>
</CardHeader>
<CardContent className="space-y-3">
{/* Health Status */}
<div className="flex items-center gap-2">
<Button size="sm" variant="outline" onClick={loadSemanticHealth}>
Refresh
</Button>
<Badge variant={semanticHealth?.status === 'ok' ? 'default' : 'secondary'}>
{semanticHealth?.status || 'unknown'}
</Badge>
<span className="text-xs text-muted-foreground">
{semanticHealth?.model} {semanticHealth?.version ? `v${semanticHealth.version}` : ''}
</span>
<span className="text-xs text-muted-foreground">
{semanticHealth?.model_present === false ? 'model missing' : ''}
</span>
</div>
{/* Queue Statistics */}
<div className="text-xs text-muted-foreground">
{semanticHealth?.latency_ms ? `latency ${semanticHealth.latency_ms}ms` : 'latency n/a'}
{semanticHealth?.queue
? ` · queue q${semanticHealth.queue.queued} d${semanticHealth.queue.dropped} p${semanticHealth.queue.processed} f${semanticHealth.queue.failed}`
: ''}
</div>
{/* Namespace Selection and Items */}
<div className="flex items-center gap-2">
<select
className="min-w-[200px] rounded-md border border-border bg-background/60 px-2 py-1 text-xs font-mono"
value={semanticNamespace}
onChange={(e) => setSemanticNamespace(e.target.value)}
>
{semanticNamespaces.length === 0 && <option value="">(no namespaces)</option>}
{semanticNamespaces.map((n) => (
<option key={n.key} value={n.key}>
{n.label || n.key}
</option>
))}
</select>
<input
type="number"
min={10}
max={200}
className="w-20 rounded-md border border-border bg-background/60 px-2 py-1 text-xs"
value={semanticLimit}
onChange={(e) => setSemanticLimit(parseInt(e.target.value || '50', 10))}
/>
<Button size="sm" variant="outline" onClick={loadSemanticItems}>
Load
</Button>
</div>
{/* Items Display */}
<pre className="max-h-56 overflow-auto rounded-md border border-border/50 bg-muted/30 p-2 text-xs">
{semanticItems || 'No items loaded.'}
</pre>
</CardContent>
</Card>
)
}
+7
View File
@@ -0,0 +1,7 @@
export { EngineControl } from './EngineControl'
export { ProviderLogins } from './ProviderLogins'
export { Integrations } from './Integrations'
export { SemanticMemory } from './SemanticMemory'
export { LogsViewer } from './LogsViewer'
export { ModelMappings } from './ModelMappings'
export { MemoryManager } from './MemoryManager'
+1 -1
View File
@@ -43,4 +43,4 @@ function Badge({
);
}
export { Badge, badgeVariants };
export { Badge };
+1 -1
View File
@@ -59,4 +59,4 @@ function Button({
);
}
export { Button, buttonVariants };
export { Button };
-25
View File
@@ -48,19 +48,6 @@ function CardDescription({ className, ...props }: React.ComponentProps<"div">) {
);
}
function CardAction({ className, ...props }: React.ComponentProps<"div">) {
return (
<div
data-slot="card-action"
className={cn(
"col-start-2 row-span-2 row-start-1 self-start justify-self-end",
className,
)}
{...props}
/>
);
}
function CardContent({ className, ...props }: React.ComponentProps<"div">) {
return (
<div
@@ -71,22 +58,10 @@ function CardContent({ className, ...props }: React.ComponentProps<"div">) {
);
}
function CardFooter({ className, ...props }: React.ComponentProps<"div">) {
return (
<div
data-slot="card-footer"
className={cn("flex items-center px-6 [.border-t]:pt-6", className)}
{...props}
/>
);
}
export {
Card,
CardHeader,
CardFooter,
CardTitle,
CardAction,
CardDescription,
CardContent,
};
-21
View File
@@ -1,21 +0,0 @@
import type * as React from "react";
import { cn } from "@/lib/utils";
function Input({ className, type, ...props }: React.ComponentProps<"input">) {
return (
<input
type={type}
data-slot="input"
className={cn(
"file:text-foreground placeholder:text-muted-foreground selection:bg-primary selection:text-primary-foreground dark:bg-input/30 border-input h-9 w-full min-w-0 rounded-md border bg-transparent px-3 py-1 text-base shadow-xs transition-[color,box-shadow] outline-none file:inline-flex file:h-7 file:border-0 file:bg-transparent file:text-sm file:font-medium disabled:pointer-events-none disabled:cursor-not-allowed disabled:opacity-50 md:text-sm",
"focus-visible:border-ring focus-visible:ring-ring/50 focus-visible:ring-[3px]",
"aria-invalid:ring-destructive/20 dark:aria-invalid:ring-destructive/40 aria-invalid:border-destructive",
className,
)}
{...props}
/>
);
}
export { Input };
-31
View File
@@ -1,31 +0,0 @@
"use client";
import * as ProgressPrimitive from "@radix-ui/react-progress";
import type * as React from "react";
import { cn } from "@/lib/utils";
function Progress({
className,
value,
...props
}: React.ComponentProps<typeof ProgressPrimitive.Root>) {
return (
<ProgressPrimitive.Root
data-slot="progress"
className={cn(
"bg-primary/20 relative h-2 w-full overflow-hidden rounded-full",
className,
)}
{...props}
>
<ProgressPrimitive.Indicator
data-slot="progress-indicator"
className="bg-primary h-full w-full flex-1 transition-all"
style={{ transform: `translateX(-${100 - (value || 0)}%)` }}
/>
</ProgressPrimitive.Root>
);
}
export { Progress };
+47 -58
View File
@@ -1,64 +1,53 @@
import * as TabsPrimitive from "@radix-ui/react-tabs";
import type * as React from "react";
import * as React from "react"
import * as TabsPrimitive from "@radix-ui/react-tabs"
import { cn } from "@/lib/utils";
import { cn } from "@/lib/utils"
function Tabs({
className,
...props
}: React.ComponentProps<typeof TabsPrimitive.Root>) {
return (
<TabsPrimitive.Root
data-slot="tabs"
className={cn("flex flex-col gap-2", className)}
{...props}
/>
);
}
const Tabs = TabsPrimitive.Root
function TabsList({
className,
...props
}: React.ComponentProps<typeof TabsPrimitive.List>) {
return (
<TabsPrimitive.List
data-slot="tabs-list"
className={cn(
"bg-muted text-muted-foreground inline-flex h-9 w-fit items-center justify-center rounded-lg p-[3px]",
className,
)}
{...props}
/>
);
}
const TabsList = React.forwardRef<
React.ElementRef<typeof TabsPrimitive.List>,
React.ComponentPropsWithoutRef<typeof TabsPrimitive.List>
>(({ className, ...props }, ref) => (
<TabsPrimitive.List
ref={ref}
className={cn(
"inline-flex h-10 items-center justify-center rounded-lg bg-muted p-1 text-muted-foreground",
className
)}
{...props}
/>
))
TabsList.displayName = TabsPrimitive.List.displayName
function TabsTrigger({
className,
...props
}: React.ComponentProps<typeof TabsPrimitive.Trigger>) {
return (
<TabsPrimitive.Trigger
data-slot="tabs-trigger"
className={cn(
"data-[state=active]:bg-background dark:data-[state=active]:text-foreground focus-visible:border-ring focus-visible:ring-ring/50 focus-visible:outline-ring dark:data-[state=active]:border-input dark:data-[state=active]:bg-input/30 text-foreground dark:text-muted-foreground inline-flex h-[calc(100%-1px)] flex-1 items-center justify-center gap-1.5 rounded-md border border-transparent px-2 py-1 text-sm font-medium whitespace-nowrap transition-[color,box-shadow] focus-visible:ring-[3px] focus-visible:outline-1 disabled:pointer-events-none disabled:opacity-50 data-[state=active]:shadow-sm [&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*='size-'])]:size-4",
className,
)}
{...props}
/>
);
}
const TabsTrigger = React.forwardRef<
React.ElementRef<typeof TabsPrimitive.Trigger>,
React.ComponentPropsWithoutRef<typeof TabsPrimitive.Trigger>
>(({ className, ...props }, ref) => (
<TabsPrimitive.Trigger
ref={ref}
className={cn(
"inline-flex items-center justify-center whitespace-nowrap rounded-md px-3 py-1.5 text-sm font-medium ring-offset-background transition-all focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:pointer-events-none disabled:opacity-50 data-[state=active]:bg-background data-[state=active]:text-foreground data-[state=active]:shadow-sm",
className
)}
{...props}
/>
))
TabsTrigger.displayName = TabsPrimitive.Trigger.displayName
function TabsContent({
className,
...props
}: React.ComponentProps<typeof TabsPrimitive.Content>) {
return (
<TabsPrimitive.Content
data-slot="tabs-content"
className={cn("flex-1 outline-none", className)}
{...props}
/>
);
}
const TabsContent = React.forwardRef<
React.ElementRef<typeof TabsPrimitive.Content>,
React.ComponentPropsWithoutRef<typeof TabsPrimitive.Content>
>(({ className, ...props }, ref) => (
<TabsPrimitive.Content
ref={ref}
className={cn(
"mt-4 ring-offset-background focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2",
className
)}
{...props}
/>
))
TabsContent.displayName = TabsPrimitive.Content.displayName
export { Tabs, TabsList, TabsTrigger, TabsContent };
export { Tabs, TabsList, TabsTrigger, TabsContent }
+27
View File
@@ -0,0 +1,27 @@
import { Moon, Sun, Monitor } from 'lucide-react'
import { Button } from './button'
import { useTheme } from '@/hooks/useTheme'
export function ThemeToggle() {
const { theme, setTheme } = useTheme()
const cycleTheme = () => {
if (theme === 'dark') setTheme('light')
else if (theme === 'light') setTheme('system')
else setTheme('dark')
}
return (
<Button
variant="ghost"
size="icon-sm"
onClick={cycleTheme}
className="text-muted-foreground hover:text-foreground"
title={`Theme: ${theme}`}
>
{theme === 'dark' && <Moon className="h-4 w-4" />}
{theme === 'light' && <Sun className="h-4 w-4" />}
{theme === 'system' && <Monitor className="h-4 w-4" />}
</Button>
)
}
+157
View File
@@ -0,0 +1,157 @@
import { createContext, useContext, useEffect, useState, useCallback } from 'react'
import type { ReactNode } from 'react'
// Type declarations for desktop app bindings
declare global {
interface Window {
pp_status?: () => Promise<ProxyStatus>;
pp_start?: () => Promise<void>;
pp_stop?: () => Promise<void>;
pp_restart?: () => Promise<void>;
pp_open_logs?: () => Promise<void>;
pp_open_auth_folder?: () => Promise<void>;
pp_open_legacy_ui?: () => Promise<void>;
pp_open_diagnostics?: () => Promise<void>;
pp_get_oauth_private?: () => Promise<boolean>;
pp_set_oauth_private?: (enabled: boolean) => Promise<void>;
pp_oauth?: (provider: string) => Promise<void>;
pp_copy_diagnostics?: () => Promise<void>;
pp_get_management_key?: () => Promise<string>;
}
}
export interface ProxyStatus {
running: boolean;
port: number;
base_url: string;
last_error: string;
}
interface ProxyContextType {
status: ProxyStatus | null;
isDesktop: boolean;
mgmtKey: string | null;
loading: string | null;
setLoading: (id: string | null) => void;
refreshStatus: () => Promise<void>;
showToast: (message: string, type?: 'success' | 'error') => void;
toast: { message: string; type: 'success' | 'error' } | null;
mgmtFetch: (path: string, opts?: RequestInit) => Promise<any>;
handleAction: (action: (() => Promise<void>) | undefined, actionId: string, successMsg: string) => Promise<void>;
}
const ProxyContext = createContext<ProxyContextType | null>(null)
export function useProxyContext() {
const ctx = useContext(ProxyContext)
if (!ctx) throw new Error('useProxyContext must be used within ProxyProvider')
return ctx
}
export function ProxyProvider({ children }: { children: ReactNode }) {
const [status, setStatus] = useState<ProxyStatus | null>(null)
const [mgmtKey, setMgmtKey] = useState<string | null>(null)
const [loading, setLoading] = useState<string | null>(null)
const [toast, setToast] = useState<{ message: string; type: 'success' | 'error' } | null>(null)
const isDesktop = typeof window.pp_status === 'function'
const showToast = useCallback((message: string, type: 'success' | 'error' = 'success') => {
setToast({ message, type })
setTimeout(() => setToast(null), 2500)
}, [])
const refreshStatus = useCallback(async () => {
try {
if (window.pp_status) {
const s = await window.pp_status()
setStatus(s)
} else {
const res = await fetch('/healthz')
if (!res.ok) {
setStatus({ running: false, port: 0, base_url: location.origin, last_error: res.statusText })
return
}
const body = await res.json().catch(() => ({}))
setStatus({
running: true,
port: body.port || 0,
base_url: location.origin,
last_error: '',
})
}
} catch (e) {
console.error('Status error:', e)
}
}, [])
const mgmtFetch = useCallback(async (path: string, opts: RequestInit = {}) => {
if (!mgmtKey) throw new Error('Missing management key')
const headers = Object.assign({}, opts.headers || {}, { 'X-Management-Key': mgmtKey })
const res = await fetch(path, { ...opts, headers })
const ct = (res.headers.get('content-type') || '').toLowerCase()
const body = ct.includes('application/json') ? await res.json() : await res.text()
if (!res.ok) {
const msg = typeof body === 'string' ? body : body?.error ? body.error : JSON.stringify(body)
throw new Error(`${res.status} ${res.statusText}: ${msg}`)
}
return body
}, [mgmtKey])
const handleAction = useCallback(async (
action: (() => Promise<void>) | undefined,
actionId: string,
successMsg: string
) => {
if (!action) return
setLoading(actionId)
try {
await action()
showToast(successMsg, 'success')
await refreshStatus()
} catch (e) {
showToast(e instanceof Error ? e.message : String(e), 'error')
} finally {
setLoading(null)
}
}, [showToast, refreshStatus])
// Initialize on mount
useEffect(() => {
refreshStatus()
const interval = setInterval(refreshStatus, 1200)
;(async () => {
try {
if (window.pp_get_management_key) {
const key = await window.pp_get_management_key()
setMgmtKey(key)
} else if (!isDesktop) {
const meta = document.querySelector('meta[name="pp-mgmt-key"]')
setMgmtKey(meta ? meta.getAttribute('content') : null)
}
} catch (e) {
console.error('Management key error:', e)
}
})()
return () => clearInterval(interval)
}, [refreshStatus, isDesktop])
return (
<ProxyContext.Provider value={{
status,
isDesktop,
mgmtKey,
loading,
setLoading,
refreshStatus,
showToast,
toast,
mgmtFetch,
handleAction,
}}>
{children}
</ProxyContext.Provider>
)
}
+43
View File
@@ -0,0 +1,43 @@
import { useEffect, useState } from 'react'
type Theme = 'light' | 'dark' | 'system'
export function useTheme() {
const [theme, setThemeState] = useState<Theme>(() => {
if (typeof window === 'undefined') return 'dark'
const stored = localStorage.getItem('pp-theme') as Theme | null
return stored || 'dark'
})
useEffect(() => {
const root = document.documentElement
const systemDark = window.matchMedia('(prefers-color-scheme: dark)').matches
const effectiveTheme = theme === 'system' ? (systemDark ? 'dark' : 'light') : theme
root.classList.remove('light', 'dark')
root.classList.add(effectiveTheme)
localStorage.setItem('pp-theme', theme)
}, [theme])
// Listen for system theme changes
useEffect(() => {
if (theme !== 'system') return
const mediaQuery = window.matchMedia('(prefers-color-scheme: dark)')
const handler = (e: MediaQueryListEvent) => {
const root = document.documentElement
root.classList.remove('light', 'dark')
root.classList.add(e.matches ? 'dark' : 'light')
}
mediaQuery.addEventListener('change', handler)
return () => mediaQuery.removeEventListener('change', handler)
}, [theme])
const setTheme = (newTheme: Theme) => {
setThemeState(newTheme)
}
return { theme, setTheme }
}
-1
View File
@@ -4,7 +4,6 @@ import "./index.css";
import App from "./App.tsx";
import "react-grab";
console.log("react-grab loaded");
import "./react-grab.css";
createRoot(document.getElementById("root")!).render(