mirror of
https://github.com/Finesssee/ProxyPilot.git
synced 2026-11-21 04:44:40 +00:00
Compare commits
128
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6163489aca | ||
|
|
0b12eafe6c | ||
|
|
e89a0f5fcb | ||
|
|
c7d06a3ace | ||
|
|
81848357c4 | ||
|
|
74319cd89e | ||
|
|
018cba3491 | ||
|
|
a6da22f391 | ||
|
|
8ae32d440a | ||
|
|
26a17c7456 | ||
|
|
d0c29cb584 | ||
|
|
0f5836e14b | ||
|
|
e96d568671 | ||
|
|
21125722cd | ||
|
|
3e47344da9 | ||
|
|
31e52f26e8 | ||
|
|
3cf08319d4 | ||
|
|
b1a4bede65 | ||
|
|
6cfd29ff1d | ||
|
|
2874e52a2a | ||
|
|
40c46acc08 | ||
|
|
d8eb83c8fd | ||
|
|
5de9cfa8ea | ||
|
|
185dcc46dd | ||
|
|
705039b410 | ||
|
|
782e3f26a5 | ||
|
|
25e73af418 | ||
|
|
ceec470273 | ||
|
|
bbcf06f163 | ||
|
|
00c64b3bcb | ||
|
|
24467aa957 | ||
|
|
68cf672e6b | ||
|
|
d9f67d1a8a | ||
|
|
7e8863b45b | ||
|
|
2ccbba05e5 | ||
|
|
5c60aea115 | ||
|
|
0a5b2f1fc0 | ||
|
|
8dd850241a | ||
|
|
3ee272620e | ||
|
|
6a1038871f | ||
|
|
2c09998780 | ||
|
|
9c91f3c511 | ||
|
|
281ac2e0ba | ||
|
|
2e7b68922a | ||
|
|
bb562e3d36 | ||
|
|
3f8366de1a | ||
|
|
f4a2ac6962 | ||
|
|
d014612bd6 | ||
|
|
f7b02ce207 | ||
|
|
9e2f45fcf5 | ||
|
|
95eb896831 | ||
|
|
660c0f7656 | ||
|
|
aca1f54478 | ||
|
|
c10abab37c | ||
|
|
d71582d7d8 | ||
|
|
f3bc99cc35 | ||
|
|
923f3f68b7 | ||
|
|
0e03289627 | ||
|
|
7631297204 | ||
|
|
3e3d0bf933 | ||
|
|
c6d8e6abb1 | ||
|
|
9fb39ff966 | ||
|
|
e08235b4d1 | ||
|
|
7a5fd20272 | ||
|
|
b37551edd8 | ||
|
|
39c7c6760a | ||
|
|
7e13a0dfdd | ||
|
|
9394166777 | ||
|
|
a6ea590cce | ||
|
|
7329675592 | ||
|
|
cab21a5089 | ||
|
|
c4701abb00 | ||
|
|
7b3ef0ea43 | ||
|
|
5a9c652f48 | ||
|
|
715c71830e | ||
|
|
aab8294d96 | ||
|
|
59e947e8a8 | ||
|
|
2617902571 | ||
|
|
270a176925 | ||
|
|
f4e7d5b92e | ||
|
|
eb84cbe543 | ||
|
|
e71648f4b6 | ||
|
|
1b5bd183b9 | ||
|
|
095718e31e | ||
|
|
abe7597d99 | ||
|
|
01cd7942b5 | ||
|
|
b669cfa650 | ||
|
|
c53fabd3a8 | ||
|
|
4ceed0428b | ||
|
|
462856639c | ||
|
|
b75ef23d5c | ||
|
|
8e236dd69d | ||
|
|
20146a1f7d | ||
|
|
b5b6d65e79 | ||
|
|
12aa47b91c | ||
|
|
5ce65eed85 | ||
|
|
e75e5716e9 | ||
|
|
d519feeba1 | ||
|
|
f1f8e2c1a6 | ||
|
|
bd893efee9 | ||
|
|
18757ebe7c | ||
|
|
09e96a45ca | ||
|
|
8e729276b0 | ||
|
|
1a1c85f043 | ||
|
|
9e187cd08f | ||
|
|
a8c389ffa6 | ||
|
|
999529c154 | ||
|
|
355932e138 | ||
|
|
d3466b31af | ||
|
|
f19594e490 | ||
|
|
946cc75e71 | ||
|
|
93c0b8703c | ||
|
|
0dda5a01a2 | ||
|
|
572792f2c6 | ||
|
|
12ac05a22f | ||
|
|
af37fbf2e0 | ||
|
|
f52bc59b2e | ||
|
|
5cf7518f26 | ||
|
|
6767188a8e | ||
|
|
7cbbf954d6 | ||
|
|
6e8f055816 | ||
|
|
87dbbedd4b | ||
|
|
a84e59f31c | ||
|
|
ca06786892 | ||
|
|
25dd8b2087 | ||
|
|
57d5b4b1b1 | ||
|
|
f80e1bb068 | ||
|
|
d68f32f00e |
@@ -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"
|
||||
@@ -0,0 +1,79 @@
|
||||
name: Build Release
|
||||
|
||||
on:
|
||||
push:
|
||||
tags:
|
||||
- 'v*'
|
||||
workflow_dispatch:
|
||||
inputs:
|
||||
version:
|
||||
description: 'Version (e.g., 0.1.0)'
|
||||
required: true
|
||||
default: '0.1.0'
|
||||
|
||||
permissions:
|
||||
contents: write
|
||||
|
||||
jobs:
|
||||
build-windows:
|
||||
runs-on: windows-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
|
||||
- name: Set up Go
|
||||
uses: actions/setup-go@v5
|
||||
with:
|
||||
go-version: '1.24'
|
||||
|
||||
- name: Set up Node.js
|
||||
uses: actions/setup-node@v4
|
||||
with:
|
||||
node-version: '22'
|
||||
|
||||
- name: Build WebUI
|
||||
run: |
|
||||
cd webui
|
||||
npm ci
|
||||
npm run build
|
||||
|
||||
- name: Build Windows binaries
|
||||
run: |
|
||||
mkdir -p dist
|
||||
# Build tray app (includes embedded dashboard)
|
||||
go build -ldflags="-s -w -H=windowsgui" -o dist/ProxyPilot.exe ./cmd/cliproxytray
|
||||
|
||||
- name: Install Inno Setup
|
||||
run: |
|
||||
choco install innosetup -y
|
||||
|
||||
- name: Get version
|
||||
id: version
|
||||
run: |
|
||||
if ("${{ github.event.inputs.version }}" -ne "") {
|
||||
echo "VERSION=${{ github.event.inputs.version }}" >> $env:GITHUB_OUTPUT
|
||||
} else {
|
||||
$tag = "${{ github.ref_name }}"
|
||||
$version = $tag -replace '^v', ''
|
||||
echo "VERSION=$version" >> $env:GITHUB_OUTPUT
|
||||
}
|
||||
|
||||
- name: Build Installer
|
||||
run: |
|
||||
& "C:\Program Files (x86)\Inno Setup 6\ISCC.exe" /DAppVersion="${{ steps.version.outputs.VERSION }}" installer\proxypilot.iss
|
||||
|
||||
- name: Upload artifacts
|
||||
uses: actions/upload-artifact@v4
|
||||
with:
|
||||
name: windows-release
|
||||
path: |
|
||||
dist/ProxyPilot.exe
|
||||
dist/ProxyPilot-*-Setup.exe
|
||||
|
||||
- name: Upload to Release
|
||||
if: startsWith(github.ref, 'refs/tags/')
|
||||
uses: softprops/action-gh-release@v1
|
||||
with:
|
||||
files: |
|
||||
dist/ProxyPilot.exe
|
||||
dist/ProxyPilot-${{ steps.version.outputs.VERSION }}-Setup.exe
|
||||
config.example.yaml
|
||||
+13
-8
@@ -16,6 +16,9 @@ pgstore/*
|
||||
gitstore/*
|
||||
objectstore/*
|
||||
static/*
|
||||
!static/icon.png
|
||||
!static/icon.ico
|
||||
test/
|
||||
refs/*
|
||||
dist/*
|
||||
.proxypilot/*
|
||||
@@ -36,6 +39,8 @@ docs/*
|
||||
!docs/memory.md
|
||||
!docs/proxypilot.md
|
||||
!docs/agentic-harness-demo.md
|
||||
!docs/long-context-features.md
|
||||
!docs/desktop-app-build-process.md
|
||||
AGENTS.md
|
||||
CLAUDE.md
|
||||
GEMINI.md
|
||||
@@ -55,11 +60,11 @@ _bmad/*
|
||||
._*
|
||||
|
||||
*.log
|
||||
*_out.txt
|
||||
|
||||
# Added by Gemini CLI fix
|
||||
node_modules/
|
||||
dist/
|
||||
.turbo/
|
||||
.env
|
||||
|
||||
*_out.txt
|
||||
|
||||
# Added by Gemini CLI fix
|
||||
node_modules/
|
||||
dist/
|
||||
.turbo/
|
||||
.env
|
||||
|
||||
|
||||
@@ -1,3 +1,9 @@
|
||||
before:
|
||||
hooks:
|
||||
# Remove Windows resource files before building
|
||||
# These .syso files cause ARM64 cross-compile failures on Linux/Darwin
|
||||
- find . -name "*.syso" -type f -delete || true
|
||||
|
||||
builds:
|
||||
- id: "cli-proxy-api-plus"
|
||||
env:
|
||||
@@ -9,6 +15,10 @@ builds:
|
||||
goarch:
|
||||
- amd64
|
||||
- arm64
|
||||
ignore:
|
||||
# Skip Windows ARM64 (not commonly used)
|
||||
- goos: windows
|
||||
goarch: arm64
|
||||
main: ./cmd/server/
|
||||
binary: cli-proxy-api-plus
|
||||
ldflags:
|
||||
|
||||
@@ -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.
|
||||
@@ -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
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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
@@ -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.
|
||||
@@ -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.
|
||||
@@ -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)
|
||||
@@ -0,0 +1,197 @@
|
||||
//go:build windows
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/router-for-me/CLIProxyAPI/v6/internal/config"
|
||||
"github.com/router-for-me/CLIProxyAPI/v6/internal/desktopctl"
|
||||
"github.com/router-for-me/CLIProxyAPI/v6/sdk/cliproxy"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
// EmbeddedEngine runs the proxy service in-process within the tray application.
|
||||
// It provides thread-safe methods for starting, stopping, and querying the service status.
|
||||
type EmbeddedEngine struct {
|
||||
mu sync.Mutex
|
||||
|
||||
// running indicates whether the service is currently active.
|
||||
running bool
|
||||
|
||||
// port is the port number the service is listening on.
|
||||
port int
|
||||
|
||||
// configPath stores the path to the configuration file.
|
||||
configPath string
|
||||
|
||||
// lastError holds the most recent error encountered during operation.
|
||||
lastError error
|
||||
|
||||
// startedAt records when the service was started.
|
||||
startedAt time.Time
|
||||
|
||||
// cancel is the function to cancel the service context.
|
||||
cancel context.CancelFunc
|
||||
|
||||
// wg waits for the service goroutine to complete.
|
||||
wg sync.WaitGroup
|
||||
}
|
||||
|
||||
// NewEmbeddedEngine creates a new EmbeddedEngine instance.
|
||||
func NewEmbeddedEngine() *EmbeddedEngine {
|
||||
return &EmbeddedEngine{}
|
||||
}
|
||||
|
||||
// Start builds and runs the proxy service in a background goroutine.
|
||||
// It returns an error if the service is already running or if building the service fails.
|
||||
func (e *EmbeddedEngine) Start(cfg *config.Config, configPath, password string) error {
|
||||
e.mu.Lock()
|
||||
defer e.mu.Unlock()
|
||||
|
||||
if e.running {
|
||||
return errors.New("engine is already running")
|
||||
}
|
||||
|
||||
if cfg == nil {
|
||||
return errors.New("configuration is required")
|
||||
}
|
||||
|
||||
if configPath == "" {
|
||||
return errors.New("configuration path is required")
|
||||
}
|
||||
|
||||
// Build the service using the SDK builder pattern
|
||||
builder := cliproxy.NewBuilder().
|
||||
WithConfig(cfg).
|
||||
WithConfigPath(configPath).
|
||||
WithLocalManagementPassword(password)
|
||||
|
||||
service, err := builder.Build()
|
||||
if err != nil {
|
||||
e.lastError = fmt.Errorf("failed to build proxy service: %w", err)
|
||||
return e.lastError
|
||||
}
|
||||
|
||||
// Create a cancelable context for the service
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
e.cancel = cancel
|
||||
|
||||
// Track state
|
||||
e.running = true
|
||||
e.port = cfg.Port
|
||||
e.configPath = configPath
|
||||
e.lastError = nil
|
||||
e.startedAt = time.Now()
|
||||
|
||||
log.Infof("starting embedded proxy engine on port %d", e.port)
|
||||
|
||||
// Run the service in a goroutine
|
||||
e.wg.Add(1)
|
||||
go func() {
|
||||
defer e.wg.Done()
|
||||
defer func() {
|
||||
e.mu.Lock()
|
||||
e.running = false
|
||||
e.cancel = nil
|
||||
e.mu.Unlock()
|
||||
log.Info("embedded proxy engine stopped")
|
||||
}()
|
||||
|
||||
err := service.Run(ctx)
|
||||
if err != nil && !errors.Is(err, context.Canceled) {
|
||||
e.mu.Lock()
|
||||
e.lastError = fmt.Errorf("proxy service exited with error: %w", err)
|
||||
e.mu.Unlock()
|
||||
log.Errorf("embedded proxy engine error: %v", err)
|
||||
}
|
||||
}()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Stop cancels the running service and waits for it to shut down.
|
||||
// It returns an error if the service is not running.
|
||||
func (e *EmbeddedEngine) Stop() error {
|
||||
e.mu.Lock()
|
||||
|
||||
if !e.running {
|
||||
e.mu.Unlock()
|
||||
return errors.New("engine is not running")
|
||||
}
|
||||
|
||||
if e.cancel != nil {
|
||||
log.Info("stopping embedded proxy engine...")
|
||||
e.cancel()
|
||||
}
|
||||
|
||||
e.mu.Unlock()
|
||||
|
||||
// Wait for the goroutine to complete
|
||||
e.wg.Wait()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Restart stops the running service (if any) and starts it again with the new configuration.
|
||||
func (e *EmbeddedEngine) Restart(cfg *config.Config, configPath, password string) error {
|
||||
// Stop if running (ignore error if not running)
|
||||
if e.IsRunning() {
|
||||
if err := e.Stop(); err != nil {
|
||||
log.Warnf("error stopping engine during restart: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// Start with new configuration
|
||||
return e.Start(cfg, configPath, password)
|
||||
}
|
||||
|
||||
// Status returns the current status of the embedded engine.
|
||||
// The returned status is compatible with desktopctl.Status for UI integration.
|
||||
func (e *EmbeddedEngine) Status() desktopctl.Status {
|
||||
e.mu.Lock()
|
||||
defer e.mu.Unlock()
|
||||
|
||||
status := desktopctl.Status{
|
||||
Running: e.running,
|
||||
Managed: true, // Embedded engine is always managed by the tray
|
||||
Port: e.port,
|
||||
ConfigPath: e.configPath,
|
||||
StartedAt: e.startedAt,
|
||||
}
|
||||
|
||||
if e.running && e.port > 0 {
|
||||
status.BaseURL = fmt.Sprintf("http://127.0.0.1:%d", e.port)
|
||||
}
|
||||
|
||||
if e.lastError != nil {
|
||||
status.LastError = e.lastError.Error()
|
||||
}
|
||||
|
||||
return status
|
||||
}
|
||||
|
||||
// IsRunning returns whether the engine is currently running.
|
||||
func (e *EmbeddedEngine) IsRunning() bool {
|
||||
e.mu.Lock()
|
||||
defer e.mu.Unlock()
|
||||
return e.running
|
||||
}
|
||||
|
||||
// LastError returns the most recent error encountered, or nil if none.
|
||||
func (e *EmbeddedEngine) LastError() error {
|
||||
e.mu.Lock()
|
||||
defer e.mu.Unlock()
|
||||
return e.lastError
|
||||
}
|
||||
|
||||
// Port returns the port number the engine is configured to listen on.
|
||||
func (e *EmbeddedEngine) Port() int {
|
||||
e.mu.Lock()
|
||||
defer e.mu.Unlock()
|
||||
return e.port
|
||||
}
|
||||
+312
-288
@@ -7,6 +7,7 @@ import (
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/http/httputil"
|
||||
@@ -14,36 +15,84 @@ import (
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/getlantern/systray"
|
||||
"github.com/router-for-me/CLIProxyAPI/v6/internal/buildinfo"
|
||||
"github.com/jchv/go-webview2"
|
||||
"github.com/router-for-me/CLIProxyAPI/v6/cmd/proxypilotui/assets"
|
||||
"github.com/router-for-me/CLIProxyAPI/v6/internal/api/middleware"
|
||||
"github.com/router-for-me/CLIProxyAPI/v6/internal/config"
|
||||
"github.com/router-for-me/CLIProxyAPI/v6/internal/desktopctl"
|
||||
"github.com/router-for-me/CLIProxyAPI/v6/internal/proxypilotupdate"
|
||||
"github.com/router-for-me/CLIProxyAPI/v6/internal/integrations"
|
||||
"github.com/router-for-me/CLIProxyAPI/v6/internal/logging"
|
||||
_ "github.com/router-for-me/CLIProxyAPI/v6/internal/translator"
|
||||
"github.com/router-for-me/CLIProxyAPI/v6/internal/trayicon"
|
||||
"github.com/router-for-me/CLIProxyAPI/v6/internal/updates"
|
||||
"github.com/router-for-me/CLIProxyAPI/v6/internal/usage"
|
||||
"github.com/router-for-me/CLIProxyAPI/v6/internal/util"
|
||||
configaccess "github.com/router-for-me/CLIProxyAPI/v6/sdk/access/config"
|
||||
sdkAuth "github.com/router-for-me/CLIProxyAPI/v6/sdk/auth"
|
||||
)
|
||||
|
||||
const autostartAppName = "ProxyPilot"
|
||||
const thinkingProxyPort = 8317
|
||||
|
||||
var assetServerURL string
|
||||
var dashboardMu sync.Mutex
|
||||
var dashboardOpen bool
|
||||
|
||||
func main() {
|
||||
// Start embedded asset server for the dashboard UI
|
||||
assetLn, err := net.Listen("tcp", "127.0.0.1:0")
|
||||
if err == nil {
|
||||
assetServerURL = "http://" + assetLn.Addr().String()
|
||||
go func() {
|
||||
fsys, _ := fs.Sub(assets.FS, ".")
|
||||
http.Serve(assetLn, http.FileServer(http.FS(fsys)))
|
||||
}()
|
||||
}
|
||||
|
||||
var repoRoot string
|
||||
var configPath string
|
||||
var exePath string
|
||||
flag.StringVar(&repoRoot, "repo", "", "Repo root (used to locate bin/ and logs/)")
|
||||
flag.StringVar(&repoRoot, "repo", "", "Repo root (used to locate logs/)")
|
||||
flag.StringVar(&configPath, "config", "", "Path to config.yaml (defaults to <repo>/config.yaml)")
|
||||
flag.StringVar(&exePath, "exe", "", "Path to ProxyPilot Engine binary (defaults to <repo>/bin/proxypilot-engine.exe)")
|
||||
flag.Parse()
|
||||
|
||||
repoRoot, configPath, exePath = applyDefaults(repoRoot, configPath, exePath)
|
||||
run(repoRoot, configPath, exePath)
|
||||
repoRoot, configPath = applyDefaults(repoRoot, configPath)
|
||||
run(repoRoot, configPath)
|
||||
}
|
||||
|
||||
func run(repoRoot, configPath, exePath string) {
|
||||
func run(repoRoot, configPath string) {
|
||||
// Initialize logging
|
||||
logging.SetupBaseLogger()
|
||||
|
||||
// Load config
|
||||
cfg, err := config.LoadConfigOptional(configPath, false)
|
||||
if err == nil && cfg != nil {
|
||||
logging.ConfigureLogOutput(cfg.LoggingToFile, cfg.LogsMaxTotalSizeMB)
|
||||
util.SetLogLevel(cfg)
|
||||
}
|
||||
if cfg == nil {
|
||||
cfg = &config.Config{Port: 8318} // Default config if load fails
|
||||
}
|
||||
|
||||
// Register token store
|
||||
sdkAuth.RegisterTokenStore(sdkAuth.NewFileTokenStore())
|
||||
|
||||
// Register access providers
|
||||
configaccess.Register()
|
||||
|
||||
// Create embedded engine (will be used instead of desktopctl)
|
||||
engine := NewEmbeddedEngine()
|
||||
|
||||
// Get or create management password
|
||||
password, _ := desktopctl.GetManagementPassword()
|
||||
|
||||
systray.Run(func() {
|
||||
thinkingProxy := startThinkingProxy(configPath)
|
||||
thinkingProxy := startThinkingProxy(engine)
|
||||
defer thinkingProxy.Close()
|
||||
|
||||
if ico := trayicon.ProxyPilotICO(); len(ico) > 0 {
|
||||
@@ -52,99 +101,76 @@ func run(repoRoot, configPath, exePath string) {
|
||||
systray.SetTitle("ProxyPilot")
|
||||
systray.SetTooltip("ProxyPilot")
|
||||
|
||||
statusItem := systray.AddMenuItem("Status: ...", "Current status")
|
||||
// Header
|
||||
systray.AddMenuItem("ProxyPilot", "ProxyPilot").Disable()
|
||||
systray.AddSeparator()
|
||||
|
||||
// Status display item (disabled, updated dynamically)
|
||||
statusItem := systray.AddMenuItem("○ Stopped", "Current proxy status")
|
||||
statusItem.Disable()
|
||||
systray.AddSeparator()
|
||||
|
||||
openProxyUI := systray.AddMenuItem("Open Dashboard", "Open ProxyPilot Control Center")
|
||||
|
||||
// Main actions
|
||||
openDashboard := systray.AddMenuItem("Open Dashboard", "Open ProxyPilot Dashboard")
|
||||
toggleItem := systray.AddMenuItem("Start Proxy", "Start/Stop proxy")
|
||||
copyURLItem := systray.AddMenuItem("Copy API URL", "Copy http://127.0.0.1:8317/v1")
|
||||
systray.AddSeparator()
|
||||
|
||||
// Engine submenu
|
||||
engineMenu := systray.AddMenuItem("Engine", "Engine controls")
|
||||
startItem := engineMenu.AddSubMenuItem("Start", "Start the ProxyPilot engine")
|
||||
stopItem := engineMenu.AddSubMenuItem("Stop", "Stop the ProxyPilot engine")
|
||||
restartItem := engineMenu.AddSubMenuItem("Restart", "Restart the ProxyPilot engine")
|
||||
// Providers submenu
|
||||
providersMenu := systray.AddMenuItem("Providers", "Provider status")
|
||||
claudeItem := providersMenu.AddSubMenuItem("○ Claude - Inactive", "Claude provider status")
|
||||
geminiItem := providersMenu.AddSubMenuItem("○ Gemini - Inactive", "Gemini provider status")
|
||||
codexItem := providersMenu.AddSubMenuItem("○ Codex - Inactive", "Codex provider status")
|
||||
qwenItem := providersMenu.AddSubMenuItem("○ Qwen - Inactive", "Qwen provider status")
|
||||
anthropicItem := providersMenu.AddSubMenuItem("○ Anthropic - Inactive", "Anthropic provider status")
|
||||
// Disable provider items (they're informational only for now)
|
||||
claudeItem.Disable()
|
||||
geminiItem.Disable()
|
||||
codexItem.Disable()
|
||||
qwenItem.Disable()
|
||||
anthropicItem.Disable()
|
||||
|
||||
// Connect submenu (VibeProxy-style easy setup)
|
||||
connectMenu := systray.AddMenuItem("Connect", "Environment variables")
|
||||
copyAnthropicEnv := connectMenu.AddSubMenuItem("Copy ANTHROPIC_BASE_URL", "Copy export ANTHROPIC_BASE_URL=http://127.0.0.1:8317/v1")
|
||||
copyOpenAIEnv := connectMenu.AddSubMenuItem("Copy OPENAI_BASE_URL", "Copy export OPENAI_BASE_URL=http://127.0.0.1:8317/v1")
|
||||
|
||||
// Settings submenu
|
||||
settingsMenu := systray.AddMenuItem("Settings", "ProxyPilot settings")
|
||||
autoOn, _, _ := desktopctl.IsWindowsRunAutostartEnabled(autostartAppName)
|
||||
autoStartItem := settingsMenu.AddSubMenuItemCheckbox("Launch on login", "Start this tray app when you log in", autoOn)
|
||||
// Diagnostics submenu
|
||||
diagMenu := systray.AddMenuItem("Diagnostics", "Diagnostic tools")
|
||||
copyDiagItem := diagMenu.AddSubMenuItem("Copy Diagnostics", "Copy diagnostics to clipboard")
|
||||
openLogsItem := diagMenu.AddSubMenuItem("Open Logs Folder", "Open logs folder in explorer")
|
||||
openAuthItem := diagMenu.AddSubMenuItem("Open Auth Folder", "Open auth folder in explorer")
|
||||
|
||||
systray.AddSeparator()
|
||||
quitItem := systray.AddMenuItem("Quit", "Quit ProxyPilot")
|
||||
|
||||
// Auto-start proxy on launch if enabled
|
||||
autoProxyOn, _ := desktopctl.GetAutoStartProxy()
|
||||
autoStartProxyItem := settingsMenu.AddSubMenuItemCheckbox("Auto-start proxy", "Start the proxy server automatically", autoProxyOn)
|
||||
|
||||
// Tools submenu
|
||||
toolsMenu := systray.AddMenuItem("Tools", "ProxyPilot tools")
|
||||
openLogs := toolsMenu.AddSubMenuItem("Open Logs", "Open logs folder")
|
||||
copyDiagnostics := toolsMenu.AddSubMenuItem("Copy Diagnostics", "Copy a support bundle to clipboard")
|
||||
openLegacyUI := toolsMenu.AddSubMenuItem("Advanced UI", "Open management UI (advanced)")
|
||||
|
||||
// Updates submenu
|
||||
updatesMenu := systray.AddMenuItem("Updates", "Check for updates")
|
||||
checkUpdates := updatesMenu.AddSubMenuItem("Check for Updates", "Check GitHub Releases for a newer ProxyPilot")
|
||||
updateNow := updatesMenu.AddSubMenuItem("Update ProxyPilot...", "Download and run the latest installer")
|
||||
updateNow.Disable()
|
||||
|
||||
systray.AddSeparator()
|
||||
|
||||
quitItem := systray.AddMenuItem("Quit", "Quit")
|
||||
|
||||
lastErr := ""
|
||||
var (
|
||||
latestVersion string
|
||||
assetName string
|
||||
assetURL string
|
||||
)
|
||||
refresh := func() {
|
||||
st, _ := desktopctl.StatusFor(configPath)
|
||||
title := "Stopped"
|
||||
if st.Running {
|
||||
title = fmt.Sprintf("Running (:%d)", st.Port)
|
||||
}
|
||||
if st.LastError != "" && lastErr == "" {
|
||||
lastErr = st.LastError
|
||||
}
|
||||
if lastErr != "" {
|
||||
title = title + " - " + shorten(lastErr, 80)
|
||||
}
|
||||
statusItem.SetTitle("Status: " + title)
|
||||
systray.SetTooltip("ProxyPilot - " + title)
|
||||
|
||||
if st.Running {
|
||||
startItem.Disable()
|
||||
stopItem.Enable()
|
||||
restartItem.Enable()
|
||||
} else {
|
||||
startItem.Enable()
|
||||
stopItem.Disable()
|
||||
restartItem.Disable()
|
||||
}
|
||||
}
|
||||
|
||||
refresh()
|
||||
|
||||
// Optional: auto-start the proxy server on launch.
|
||||
if autoProxyOn {
|
||||
go func() {
|
||||
st, _ := desktopctl.StatusFor(configPath)
|
||||
if st.Running {
|
||||
return
|
||||
if !engine.IsRunning() {
|
||||
engine.Start(cfg, configPath, password)
|
||||
}
|
||||
_, err := desktopctl.Start(desktopctl.StartOptions{RepoRoot: repoRoot, ConfigPath: configPath, ExePath: exePath})
|
||||
if err != nil {
|
||||
lastErr = err.Error()
|
||||
} else {
|
||||
lastErr = ""
|
||||
}
|
||||
refresh()
|
||||
}()
|
||||
}
|
||||
|
||||
// Update UI based on status
|
||||
refresh := func() {
|
||||
st := engine.Status()
|
||||
if st.Running {
|
||||
port := st.Port
|
||||
if port <= 0 {
|
||||
port = 8318
|
||||
}
|
||||
statusItem.SetTitle(fmt.Sprintf("● Running on :%d", port))
|
||||
systray.SetTooltip(fmt.Sprintf("ProxyPilot - Running (:%d)", port))
|
||||
toggleItem.SetTitle("Stop Proxy")
|
||||
toggleItem.SetTooltip("Stop the proxy")
|
||||
} else {
|
||||
statusItem.SetTitle("○ Stopped")
|
||||
systray.SetTooltip("ProxyPilot - Stopped")
|
||||
toggleItem.SetTitle("Start Proxy")
|
||||
toggleItem.SetTooltip("Start the proxy")
|
||||
}
|
||||
}
|
||||
refresh()
|
||||
|
||||
// Refresh status periodically
|
||||
go func() {
|
||||
t := time.NewTicker(2 * time.Second)
|
||||
defer t.Stop()
|
||||
@@ -153,155 +179,29 @@ func run(repoRoot, configPath, exePath string) {
|
||||
}
|
||||
}()
|
||||
|
||||
// Handle clicks
|
||||
go func() {
|
||||
for {
|
||||
select {
|
||||
case <-startItem.ClickedCh:
|
||||
_, err := desktopctl.Start(desktopctl.StartOptions{RepoRoot: repoRoot, ConfigPath: configPath, ExePath: exePath})
|
||||
if err != nil {
|
||||
lastErr = err.Error()
|
||||
refresh()
|
||||
continue
|
||||
}
|
||||
lastErr = ""
|
||||
refresh()
|
||||
case <-stopItem.ClickedCh:
|
||||
if err := desktopctl.Stop(desktopctl.StopOptions{}); err != nil {
|
||||
lastErr = err.Error()
|
||||
refresh()
|
||||
continue
|
||||
}
|
||||
lastErr = ""
|
||||
refresh()
|
||||
case <-restartItem.ClickedCh:
|
||||
_, err := desktopctl.Restart(desktopctl.StartOptions{RepoRoot: repoRoot, ConfigPath: configPath, ExePath: exePath})
|
||||
if err != nil {
|
||||
lastErr = err.Error()
|
||||
refresh()
|
||||
continue
|
||||
}
|
||||
lastErr = ""
|
||||
refresh()
|
||||
case <-copyAnthropicEnv.ClickedCh:
|
||||
_ = copyToClipboard(fmt.Sprintf("http://127.0.0.1:%d/v1", thinkingProxyPort))
|
||||
case <-copyOpenAIEnv.ClickedCh:
|
||||
_ = copyToClipboard(fmt.Sprintf("http://127.0.0.1:%d/v1", thinkingProxyPort))
|
||||
case <-autoStartItem.ClickedCh:
|
||||
if autoStartItem.Checked() {
|
||||
_ = desktopctl.DisableWindowsRunAutostart(autostartAppName)
|
||||
autoStartItem.Uncheck()
|
||||
lastErr = ""
|
||||
refresh()
|
||||
continue
|
||||
}
|
||||
cmd, err := autostartCommand(repoRoot, configPath, exePath)
|
||||
if err != nil {
|
||||
lastErr = err.Error()
|
||||
refresh()
|
||||
continue
|
||||
}
|
||||
if err := desktopctl.EnableWindowsRunAutostart(autostartAppName, cmd); err != nil {
|
||||
lastErr = err.Error()
|
||||
refresh()
|
||||
continue
|
||||
}
|
||||
autoStartItem.Check()
|
||||
lastErr = ""
|
||||
refresh()
|
||||
case <-autoStartProxyItem.ClickedCh:
|
||||
if autoStartProxyItem.Checked() {
|
||||
_ = desktopctl.SetAutoStartProxy(false)
|
||||
autoStartProxyItem.Uncheck()
|
||||
lastErr = ""
|
||||
refresh()
|
||||
continue
|
||||
}
|
||||
_ = desktopctl.SetAutoStartProxy(true)
|
||||
autoStartProxyItem.Check()
|
||||
lastErr = ""
|
||||
refresh()
|
||||
case <-openProxyUI.ClickedCh:
|
||||
if err := openProxyUIWithAutostart(repoRoot, configPath, exePath); err != nil {
|
||||
lastErr = err.Error()
|
||||
refresh()
|
||||
case <-openDashboard.ClickedCh:
|
||||
openProxyUIWithAutostart(engine, cfg, configPath, password)
|
||||
case <-toggleItem.ClickedCh:
|
||||
if engine.IsRunning() {
|
||||
engine.Stop()
|
||||
} else {
|
||||
lastErr = ""
|
||||
refresh()
|
||||
}
|
||||
case <-openLegacyUI.ClickedCh:
|
||||
st, _ := desktopctl.StatusFor(configPath)
|
||||
if !st.Running || strings.TrimSpace(st.BaseURL) == "" {
|
||||
lastErr = "proxy is not running"
|
||||
refresh()
|
||||
continue
|
||||
}
|
||||
_ = desktopctl.OpenBrowser(st.BaseURL + "/management.html?legacy=1")
|
||||
case <-openLogs.ClickedCh:
|
||||
if err := desktopctl.OpenLogsFolder(repoRoot, configPath); err != nil {
|
||||
lastErr = err.Error()
|
||||
refresh()
|
||||
}
|
||||
case <-copyDiagnostics.ClickedCh:
|
||||
if err := copyDiagnosticsToClipboard(configPath); err != nil {
|
||||
lastErr = err.Error()
|
||||
} else {
|
||||
lastErr = "Diagnostics copied"
|
||||
engine.Start(cfg, configPath, password)
|
||||
}
|
||||
refresh()
|
||||
case <-checkUpdates.ClickedCh:
|
||||
go func() {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
|
||||
defer cancel()
|
||||
rel, err := proxypilotupdate.FetchLatestRelease(ctx)
|
||||
if err != nil {
|
||||
lastErr = err.Error()
|
||||
refresh()
|
||||
return
|
||||
}
|
||||
latestVersion = rel.Version()
|
||||
assetName, assetURL = rel.FindPreferredAsset()
|
||||
if strings.TrimSpace(assetURL) == "" {
|
||||
lastErr = "no ProxyPilot asset found in latest release"
|
||||
refresh()
|
||||
return
|
||||
}
|
||||
// If this build is already tagged, avoid prompting unnecessarily.
|
||||
if buildinfo.Version != "" && buildinfo.Version != "dev" && strings.EqualFold(buildinfo.Version, latestVersion) {
|
||||
lastErr = "Up to date (" + latestVersion + ")"
|
||||
updateNow.Disable()
|
||||
refresh()
|
||||
return
|
||||
}
|
||||
updateNow.SetTitle("Update ProxyPilot... (" + latestVersion + ")")
|
||||
updateNow.Enable()
|
||||
lastErr = "Update available (" + latestVersion + ")"
|
||||
refresh()
|
||||
}()
|
||||
case <-updateNow.ClickedCh:
|
||||
if strings.TrimSpace(assetURL) == "" {
|
||||
lastErr = "no update asset URL (run Check for Updates)"
|
||||
refresh()
|
||||
continue
|
||||
case <-copyURLItem.ClickedCh:
|
||||
copyToClipboard(fmt.Sprintf("http://127.0.0.1:%d/v1", thinkingProxyPort))
|
||||
case <-copyDiagItem.ClickedCh:
|
||||
copyDiagnosticsToClipboard(engine)
|
||||
case <-openLogsItem.ClickedCh:
|
||||
desktopctl.OpenLogsFolder(repoRoot, configPath)
|
||||
case <-openAuthItem.ClickedCh:
|
||||
if dir, err := desktopctl.AuthDirFor(configPath); err == nil {
|
||||
desktopctl.OpenFolder(dir)
|
||||
}
|
||||
go func() {
|
||||
dest := filepath.Join(os.TempDir(), "ProxyPilot-"+sanitizeFileName(latestVersion)+"-"+sanitizeFileName(assetName))
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Minute)
|
||||
defer cancel()
|
||||
updateNow.SetTitle("Updating...")
|
||||
lastErr = "Downloading update..."
|
||||
refresh()
|
||||
if err := proxypilotupdate.DownloadToFile(ctx, assetURL, dest); err != nil {
|
||||
lastErr = err.Error()
|
||||
updateNow.SetTitle("Update ProxyPilot...")
|
||||
refresh()
|
||||
return
|
||||
}
|
||||
// Run installer/zip handler and exit ProxyPilot so files can be replaced.
|
||||
lastErr = "Launching installer..."
|
||||
refresh()
|
||||
_ = launchUpdate(dest)
|
||||
systray.Quit()
|
||||
}()
|
||||
case <-quitItem.ClickedCh:
|
||||
systray.Quit()
|
||||
return
|
||||
@@ -315,7 +215,7 @@ type closeFn func() error
|
||||
|
||||
func (c closeFn) Close() error { return c() }
|
||||
|
||||
func startThinkingProxy(configPath string) ioCloser {
|
||||
func startThinkingProxy(engine *EmbeddedEngine) ioCloser {
|
||||
ln, err := net.Listen("tcp", fmt.Sprintf("127.0.0.1:%d", thinkingProxyPort))
|
||||
if err != nil {
|
||||
// Best effort: don't crash the tray app if the port is already taken.
|
||||
@@ -330,7 +230,7 @@ func startThinkingProxy(configPath string) ioCloser {
|
||||
)
|
||||
|
||||
getProxy := func() (*httputil.ReverseProxy, *url.URL) {
|
||||
st, _ := desktopctl.StatusFor(configPath)
|
||||
st := engine.Status()
|
||||
port := st.Port
|
||||
if port <= 0 {
|
||||
port = 8318
|
||||
@@ -391,45 +291,183 @@ type ioCloser interface {
|
||||
Close() error
|
||||
}
|
||||
|
||||
func openProxyUIWithAutostart(repoRoot, configPath, exePath string) error {
|
||||
// Prefer opening the desktop window if the UI binary exists next to this tray app.
|
||||
if exe, err := os.Executable(); err == nil && strings.TrimSpace(exe) != "" {
|
||||
dir := filepath.Dir(filepath.Clean(exe))
|
||||
uiExe := filepath.Join(dir, "ProxyPilotUI.exe")
|
||||
if _, err := os.Stat(uiExe); err == nil {
|
||||
args := []string{}
|
||||
if strings.TrimSpace(repoRoot) != "" {
|
||||
args = append(args, "-repo", repoRoot)
|
||||
}
|
||||
if strings.TrimSpace(configPath) != "" {
|
||||
args = append(args, "-config", configPath)
|
||||
}
|
||||
if strings.TrimSpace(exePath) != "" {
|
||||
args = append(args, "-exe", exePath)
|
||||
}
|
||||
return exec.Command(uiExe, args...).Start()
|
||||
func openProxyUIWithAutostart(engine *EmbeddedEngine, cfg *config.Config, configPath, password string) error {
|
||||
// Start proxy if not running
|
||||
if !engine.IsRunning() {
|
||||
if err := engine.Start(cfg, configPath, password); err != nil {
|
||||
// Continue anyway to show UI
|
||||
}
|
||||
}
|
||||
// Fallback: open browser to the management UI (starts proxy if needed).
|
||||
st, _ := desktopctl.StatusFor(configPath)
|
||||
if !st.Running {
|
||||
if _, err := desktopctl.Start(desktopctl.StartOptions{RepoRoot: repoRoot, ConfigPath: configPath, ExePath: exePath}); err != nil {
|
||||
|
||||
// Open embedded WebView2 dashboard
|
||||
go func() {
|
||||
openEmbeddedDashboard(engine, cfg, configPath, password)
|
||||
}()
|
||||
return nil
|
||||
}
|
||||
|
||||
func openEmbeddedDashboard(engine *EmbeddedEngine, cfg *config.Config, configPath, password string) {
|
||||
// Lock this goroutine to an OS thread - required for Windows COM/GUI operations
|
||||
runtime.LockOSThread()
|
||||
defer runtime.UnlockOSThread()
|
||||
|
||||
// Prevent opening multiple dashboard windows
|
||||
dashboardMu.Lock()
|
||||
if dashboardOpen {
|
||||
dashboardMu.Unlock()
|
||||
return
|
||||
}
|
||||
dashboardOpen = true
|
||||
dashboardMu.Unlock()
|
||||
|
||||
defer func() {
|
||||
dashboardMu.Lock()
|
||||
dashboardOpen = false
|
||||
dashboardMu.Unlock()
|
||||
}()
|
||||
|
||||
// Recover from any panics to prevent the tray app from crashing
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
fmt.Fprintf(os.Stderr, "WebView2 panic: %v\n", r)
|
||||
// Try to open in browser as fallback
|
||||
st := engine.Status()
|
||||
if st.Running && strings.TrimSpace(st.BaseURL) != "" {
|
||||
_ = desktopctl.OpenBrowser(st.BaseURL + "/proxypilot.html")
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
target := assetServerURL + "/index.html"
|
||||
if assetServerURL == "" {
|
||||
// Fallback to browser if asset server failed
|
||||
st := engine.Status()
|
||||
if st.Running && strings.TrimSpace(st.BaseURL) != "" {
|
||||
_ = desktopctl.OpenBrowser(st.BaseURL + "/proxypilot.html")
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
w := webview2.NewWithOptions(webview2.WebViewOptions{
|
||||
Debug: true,
|
||||
AutoFocus: true,
|
||||
WindowOptions: webview2.WindowOptions{
|
||||
Title: "ProxyPilot",
|
||||
Width: 1200,
|
||||
Height: 850,
|
||||
Center: true,
|
||||
IconId: 1, // Use embedded icon from resource.syso
|
||||
},
|
||||
})
|
||||
if w == nil {
|
||||
// Fallback to browser - WebView2 runtime may not be installed
|
||||
fmt.Fprintf(os.Stderr, "WebView2 failed to initialize, falling back to browser\n")
|
||||
_ = desktopctl.OpenBrowser(target)
|
||||
return
|
||||
}
|
||||
defer w.Destroy()
|
||||
|
||||
// Bind desktop functions for the React UI
|
||||
_ = w.Bind("pp_status", func() (map[string]any, error) {
|
||||
cur := engine.Status()
|
||||
return map[string]any{
|
||||
"running": cur.Running,
|
||||
"port": cur.Port,
|
||||
"base_url": cur.BaseURL,
|
||||
"last_error": cur.LastError,
|
||||
}, nil
|
||||
})
|
||||
_ = w.Bind("pp_start", func() error {
|
||||
return engine.Start(cfg, configPath, password)
|
||||
})
|
||||
_ = w.Bind("pp_stop", func() error { return engine.Stop() })
|
||||
_ = w.Bind("pp_restart", func() error {
|
||||
return engine.Restart(cfg, configPath, password)
|
||||
})
|
||||
_ = w.Bind("pp_open_logs", func() error { return desktopctl.OpenLogsFolder("", configPath) })
|
||||
_ = w.Bind("pp_open_auth_folder", func() error {
|
||||
dir, err := desktopctl.AuthDirFor(configPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
deadline := time.Now().Add(3 * time.Second)
|
||||
for time.Now().Before(deadline) {
|
||||
st2, _ := desktopctl.StatusFor(configPath)
|
||||
if st2.Running {
|
||||
break
|
||||
}
|
||||
time.Sleep(150 * time.Millisecond)
|
||||
return desktopctl.OpenFolder(dir)
|
||||
})
|
||||
_ = w.Bind("pp_get_oauth_private", func() (bool, error) { return desktopctl.GetOAuthPrivate() })
|
||||
_ = w.Bind("pp_set_oauth_private", func(enabled bool) error { return desktopctl.SetOAuthPrivate(enabled) })
|
||||
_ = w.Bind("pp_oauth", func(provider string) error { return startOAuthFlow(engine, getOAuthEndpoint(provider)) })
|
||||
_ = w.Bind("pp_copy_diagnostics", func() error { return copyDiagnosticsToClipboard(engine) })
|
||||
_ = w.Bind("pp_get_management_key", func() (string, error) { return desktopctl.GetManagementPassword() })
|
||||
_ = w.Bind("pp_open_legacy_ui", func() error {
|
||||
cur := engine.Status()
|
||||
if !cur.Running {
|
||||
return fmt.Errorf("proxy not running")
|
||||
}
|
||||
return desktopctl.OpenBrowser(cur.BaseURL + "/management.html?legacy=1")
|
||||
})
|
||||
_ = w.Bind("pp_open_diagnostics", func() error {
|
||||
cur := engine.Status()
|
||||
if !cur.Running {
|
||||
return fmt.Errorf("proxy not running")
|
||||
}
|
||||
return desktopctl.OpenBrowser(cur.BaseURL + "/proxypilot.html")
|
||||
})
|
||||
_ = w.Bind("pp_get_requests", func() (any, error) {
|
||||
return middleware.GetRequestMonitor(), nil
|
||||
})
|
||||
_ = w.Bind("pp_get_usage", func() (any, error) {
|
||||
stats := usage.GetRequestStatistics()
|
||||
if stats == nil {
|
||||
return nil, fmt.Errorf("usage statistics not available")
|
||||
}
|
||||
return usage.ComputeUsageStats(stats.Snapshot()), nil
|
||||
})
|
||||
_ = w.Bind("pp_detect_agents", func() ([]integrations.Agent, error) {
|
||||
st := engine.Status()
|
||||
proxyURL := st.BaseURL
|
||||
if proxyURL == "" {
|
||||
proxyURL = fmt.Sprintf("http://127.0.0.1:%d", st.Port)
|
||||
}
|
||||
return integrations.DetectCLIAgents(proxyURL), nil
|
||||
})
|
||||
_ = w.Bind("pp_configure_agent", func(agentID string) error {
|
||||
st := engine.Status()
|
||||
proxyURL := st.BaseURL
|
||||
if proxyURL == "" {
|
||||
proxyURL = fmt.Sprintf("http://127.0.0.1:%d", st.Port)
|
||||
}
|
||||
return integrations.ConfigureCLIAgent(agentID, proxyURL)
|
||||
})
|
||||
_ = w.Bind("pp_unconfigure_agent", func(agentID string) error {
|
||||
return integrations.UnconfigureCLIAgent(agentID)
|
||||
})
|
||||
_ = w.Bind("pp_check_updates", func() (*updates.UpdateInfo, error) {
|
||||
return updates.CheckForUpdates()
|
||||
})
|
||||
_ = w.Bind("pp_download_update", func(url string) error {
|
||||
return desktopctl.OpenBrowser(url)
|
||||
})
|
||||
|
||||
w.Navigate(target)
|
||||
w.Run()
|
||||
}
|
||||
|
||||
func getOAuthEndpoint(provider string) string {
|
||||
switch strings.ToLower(strings.TrimSpace(provider)) {
|
||||
case "antigravity":
|
||||
return "/v0/management/antigravity-auth-url"
|
||||
case "gemini-cli", "geminicli":
|
||||
return "/v0/management/gemini-cli-auth-url"
|
||||
case "codex":
|
||||
return "/v0/management/codex-auth-url"
|
||||
case "claude", "anthropic":
|
||||
return "/v0/management/anthropic-auth-url"
|
||||
case "qwen":
|
||||
return "/v0/management/qwen-auth-url"
|
||||
case "iflow":
|
||||
return "/v0/management/iflow-auth-url"
|
||||
default:
|
||||
return ""
|
||||
}
|
||||
st, _ = desktopctl.StatusFor(configPath)
|
||||
if strings.TrimSpace(st.BaseURL) == "" {
|
||||
return fmt.Errorf("proxy base URL not available")
|
||||
}
|
||||
return desktopctl.OpenBrowser(st.BaseURL + "/proxypilot.html")
|
||||
}
|
||||
|
||||
func shorten(s string, max int) string {
|
||||
@@ -470,8 +508,8 @@ type authURLResponse struct {
|
||||
Error string `json:"error"`
|
||||
}
|
||||
|
||||
func startOAuthFlow(configPath string, endpointPath string) error {
|
||||
st, _ := desktopctl.StatusFor(configPath)
|
||||
func startOAuthFlow(engine *EmbeddedEngine, endpointPath string) error {
|
||||
st := engine.Status()
|
||||
if !st.Running || strings.TrimSpace(st.BaseURL) == "" {
|
||||
return fmt.Errorf("proxy is not running")
|
||||
}
|
||||
@@ -511,8 +549,8 @@ func startOAuthFlow(configPath string, endpointPath string) error {
|
||||
return openOAuthURL(out.URL, private)
|
||||
}
|
||||
|
||||
func copyDiagnosticsToClipboard(configPath string) error {
|
||||
st, _ := desktopctl.StatusFor(configPath)
|
||||
func copyDiagnosticsToClipboard(engine *EmbeddedEngine) error {
|
||||
st := engine.Status()
|
||||
if !st.Running || strings.TrimSpace(st.BaseURL) == "" {
|
||||
return fmt.Errorf("proxy is not running")
|
||||
}
|
||||
@@ -588,7 +626,7 @@ func findEdge() (string, error) {
|
||||
return "", fmt.Errorf("msedge.exe not found")
|
||||
}
|
||||
|
||||
func autostartCommand(repoRoot, configPath, exePath string) (string, error) {
|
||||
func autostartCommand(repoRoot, configPath string) (string, error) {
|
||||
exe, err := os.Executable()
|
||||
if err == nil && strings.TrimSpace(exe) != "" {
|
||||
exe = filepath.Clean(exe)
|
||||
@@ -598,23 +636,19 @@ func autostartCommand(repoRoot, configPath, exePath string) (string, error) {
|
||||
if exe == "" {
|
||||
return "", fmt.Errorf("unable to resolve tray executable path")
|
||||
}
|
||||
args := make([]string, 0, 6)
|
||||
args := make([]string, 0, 4)
|
||||
if strings.TrimSpace(repoRoot) != "" {
|
||||
args = append(args, "-repo", repoRoot)
|
||||
}
|
||||
if strings.TrimSpace(configPath) != "" {
|
||||
args = append(args, "-config", configPath)
|
||||
}
|
||||
if strings.TrimSpace(exePath) != "" {
|
||||
args = append(args, "-exe", exePath)
|
||||
}
|
||||
return quoteWindowsCommand(exe, args), nil
|
||||
}
|
||||
|
||||
func applyDefaults(repoRoot, configPath, exePath string) (string, string, string) {
|
||||
func applyDefaults(repoRoot, configPath string) (string, string) {
|
||||
repoRoot = strings.TrimSpace(repoRoot)
|
||||
configPath = strings.TrimSpace(configPath)
|
||||
exePath = strings.TrimSpace(exePath)
|
||||
|
||||
exe, _ := os.Executable()
|
||||
exeDir := ""
|
||||
@@ -640,17 +674,7 @@ func applyDefaults(repoRoot, configPath, exePath string) (string, string, string
|
||||
ensureConfig(configPath)
|
||||
}
|
||||
|
||||
if exePath == "" && exeDir != "" {
|
||||
for _, name := range []string{"proxypilot-engine.exe", "cliproxyapi-latest.exe"} {
|
||||
cand := filepath.Join(exeDir, name)
|
||||
if _, err := os.Stat(cand); err == nil {
|
||||
exePath = cand
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return repoRoot, configPath, exePath
|
||||
return repoRoot, configPath
|
||||
}
|
||||
|
||||
func ensureConfig(configPath string) {
|
||||
|
||||
Binary file not shown.
@@ -0,0 +1,40 @@
|
||||
{
|
||||
"FixedFileInfo": {
|
||||
"FileVersion": {
|
||||
"Major": 0,
|
||||
"Minor": 1,
|
||||
"Patch": 3,
|
||||
"Build": 0
|
||||
},
|
||||
"ProductVersion": {
|
||||
"Major": 0,
|
||||
"Minor": 1,
|
||||
"Patch": 3,
|
||||
"Build": 0
|
||||
},
|
||||
"FileFlagsMask": "3f",
|
||||
"FileFlags ": "00",
|
||||
"FileOS": "040004",
|
||||
"FileType": "01",
|
||||
"FileSubType": "00"
|
||||
},
|
||||
"StringFileInfo": {
|
||||
"Comments": "Local AI Proxy with Embedded Engine",
|
||||
"CompanyName": "ProxyPilot",
|
||||
"FileDescription": "ProxyPilot - Single Binary Desktop App",
|
||||
"FileVersion": "0.1.3",
|
||||
"InternalName": "ProxyPilot",
|
||||
"LegalCopyright": "MIT License",
|
||||
"OriginalFilename": "ProxyPilot.exe",
|
||||
"ProductName": "ProxyPilot",
|
||||
"ProductVersion": "0.1.3"
|
||||
},
|
||||
"VarFileInfo": {
|
||||
"Translation": {
|
||||
"LangID": "0409",
|
||||
"CharsetID": "04B0"
|
||||
}
|
||||
},
|
||||
"IconPath": "../../static/icon.ico",
|
||||
"ManifestPath": ""
|
||||
}
|
||||
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
@@ -2,5 +2,5 @@ package assets
|
||||
|
||||
import "embed"
|
||||
|
||||
//go:embed index.html vite.svg assets/*
|
||||
//go:embed index.html vite.svg logo.png assets/*
|
||||
var FS embed.FS
|
||||
|
||||
@@ -5,6 +5,11 @@
|
||||
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<title>webui</title>
|
||||
<!-- Google Fonts preconnect for performance -->
|
||||
<link rel="preconnect" href="https://fonts.googleapis.com">
|
||||
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
|
||||
<!-- Google Fonts: Space Grotesk (display/headers), JetBrains Mono (code), IBM Plex Sans (body) -->
|
||||
<link href="https://fonts.googleapis.com/css2?family=IBM+Plex+Sans:wght@400;500;600&family=JetBrains+Mono:wght@400;500;600&family=Space+Grotesk:wght@400;500;600;700&display=swap" rel="stylesheet">
|
||||
<script type="module" crossorigin src="/assets/index.js"></script>
|
||||
<link rel="stylesheet" crossorigin href="/assets/index.css">
|
||||
</head>
|
||||
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 194 KiB |
@@ -21,6 +21,8 @@ import (
|
||||
"github.com/jchv/go-webview2"
|
||||
"github.com/router-for-me/CLIProxyAPI/v6/cmd/proxypilotui/assets"
|
||||
"github.com/router-for-me/CLIProxyAPI/v6/internal/desktopctl"
|
||||
"github.com/router-for-me/CLIProxyAPI/v6/internal/logging"
|
||||
_ "github.com/router-for-me/CLIProxyAPI/v6/internal/translator"
|
||||
)
|
||||
|
||||
func main() {
|
||||
@@ -29,13 +31,21 @@ func main() {
|
||||
var exePath string
|
||||
var url string
|
||||
var standalone bool
|
||||
var legacyMode bool
|
||||
flag.StringVar(&repoRoot, "repo", "", "Repo root (used to locate bin/ and logs/)")
|
||||
flag.StringVar(&configPath, "config", "", "Path to config.yaml (defaults to <repo>/config.yaml)")
|
||||
flag.StringVar(&exePath, "exe", "", "Path to proxy engine binary (defaults to <repo>/bin/proxypilot-engine.exe)")
|
||||
flag.StringVar(&exePath, "exe", "", "Path to proxy engine binary (legacy mode only)")
|
||||
flag.StringVar(&url, "url", "", "Open a specific URL in-app (advanced)")
|
||||
flag.BoolVar(&standalone, "standalone", false, "Run standalone UI without connecting to proxy server")
|
||||
flag.BoolVar(&legacyMode, "legacy", false, "Use legacy subprocess mode (separate engine binary)")
|
||||
flag.Parse()
|
||||
|
||||
// Enable embedded mode by default (single binary)
|
||||
if !legacyMode {
|
||||
desktopctl.SetEmbeddedMode(true)
|
||||
logging.SetupBaseLogger()
|
||||
}
|
||||
|
||||
repoRoot, configPath, exePath = applyDefaults(repoRoot, configPath, exePath)
|
||||
|
||||
var assetServerURL string
|
||||
@@ -130,6 +140,7 @@ func main() {
|
||||
cur, _ := desktopctl.StatusFor(configPath)
|
||||
return map[string]any{
|
||||
"running": cur.Running,
|
||||
"version": cur.Version,
|
||||
"port": cur.Port,
|
||||
"thinking_port": cur.ThinkingPort,
|
||||
"thinking_running": cur.ThinkingRunning,
|
||||
@@ -193,6 +204,31 @@ func main() {
|
||||
_ = w.Bind("pp_copy_diagnostics", func() error { return copyDiagnosticsToClipboard(configPath) })
|
||||
_ = w.Bind("pp_get_management_key", func() (string, error) { return desktopctl.GetManagementPassword() })
|
||||
|
||||
_ = w.Bind("pp_check_updates", func() (map[string]any, error) {
|
||||
st, _ := desktopctl.StatusFor(configPath)
|
||||
if !st.Running {
|
||||
return nil, fmt.Errorf("proxy not running")
|
||||
}
|
||||
key, _ := desktopctl.GetManagementPassword()
|
||||
client := &http.Client{Timeout: 10 * time.Second}
|
||||
req, _ := http.NewRequest("GET", st.BaseURL+"/v0/management/updates/check", nil)
|
||||
req.Header.Set("X-Management-Key", key)
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
var res map[string]any
|
||||
if err := json.NewDecoder(resp.Body).Decode(&res); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return res, nil
|
||||
})
|
||||
|
||||
_ = w.Bind("pp_download_update", func(url string) error {
|
||||
return desktopctl.OpenBrowser(url)
|
||||
})
|
||||
|
||||
// Integration bindings
|
||||
_ = w.Bind("pp_get_integrations", func() ([]map[string]any, error) {
|
||||
st, _ := desktopctl.StatusFor(configPath)
|
||||
@@ -238,6 +274,42 @@ func main() {
|
||||
return nil
|
||||
})
|
||||
|
||||
// Agent detection bindings
|
||||
_ = w.Bind("pp_detect_agents", func() ([]map[string]any, error) {
|
||||
agents := detectCLIAgents()
|
||||
return agents, nil
|
||||
})
|
||||
|
||||
_ = w.Bind("pp_configure_agent", func(agentID string) error {
|
||||
st, _ := desktopctl.StatusFor(configPath)
|
||||
if !st.Running {
|
||||
return fmt.Errorf("proxy not running")
|
||||
}
|
||||
key, _ := desktopctl.GetManagementPassword()
|
||||
client := &http.Client{Timeout: 10 * time.Second}
|
||||
req, _ := http.NewRequest("POST", st.BaseURL+"/v0/management/integrations/"+agentID, nil)
|
||||
req.Header.Set("X-Management-Key", key)
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
if resp.StatusCode != 200 {
|
||||
b, _ := io.ReadAll(resp.Body)
|
||||
return fmt.Errorf("failed: %s", string(b))
|
||||
}
|
||||
return nil
|
||||
})
|
||||
|
||||
// Model mappings bindings
|
||||
_ = w.Bind("pp_get_model_mappings", func() ([]map[string]any, error) {
|
||||
return getModelMappings(), nil
|
||||
})
|
||||
|
||||
_ = w.Bind("pp_save_model_mappings", func(mappings []map[string]any) error {
|
||||
return saveModelMappings(mappings)
|
||||
})
|
||||
|
||||
if target != "" {
|
||||
w.Navigate(target)
|
||||
w.Run()
|
||||
@@ -579,6 +651,29 @@ func controlCenterHTML() string {
|
||||
<div class="hint">These open the provider login flow in your browser and save auth files for ProxyPilot.</div>
|
||||
</div>
|
||||
|
||||
<div class="card" style="grid-column: 1 / -1">
|
||||
<div class="sectionTitle">CLI Agents</div>
|
||||
<div class="row" id="agentsList">
|
||||
<div style="color:var(--muted);font-size:13px">Detecting agents...</div>
|
||||
</div>
|
||||
<div class="note">
|
||||
Detected CLI agents. Click to configure for ProxyPilot.
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="card" style="grid-column: 1 / -1">
|
||||
<div class="sectionTitle">Model Mappings</div>
|
||||
<div id="mappingsList" style="margin-bottom:12px"></div>
|
||||
<div class="row" style="gap:8px;flex-wrap:wrap">
|
||||
<input type="text" id="mapFrom" placeholder="Alias (e.g. smart)" style="flex:1;min-width:120px;padding:8px 10px;background:rgba(0,0,0,.3);border:1px solid var(--line);border-radius:8px;color:var(--text);font-size:13px" />
|
||||
<input type="text" id="mapTo" placeholder="Model (e.g. claude-opus-4-5)" style="flex:2;min-width:180px;padding:8px 10px;background:rgba(0,0,0,.3);border:1px solid var(--line);border-radius:8px;color:var(--text);font-size:13px" />
|
||||
<button id="addMappingBtn" class="btnPrimary">Add</button>
|
||||
</div>
|
||||
<div class="note">
|
||||
Route friendly model names to actual models.
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="card" style="grid-column: 1 / -1">
|
||||
<div class="sectionTitle">Integrations</div>
|
||||
<div class="row" id="integrationsList">
|
||||
@@ -697,12 +792,84 @@ func controlCenterHTML() string {
|
||||
}
|
||||
}
|
||||
|
||||
async function refreshAgents() {
|
||||
const list = $('agentsList');
|
||||
try {
|
||||
const agents = await window.pp_detect_agents();
|
||||
if(!agents || agents.length === 0) {
|
||||
list.innerHTML = '<div style="color:var(--muted);font-size:13px">No agents detected</div>';
|
||||
return;
|
||||
}
|
||||
list.innerHTML = '';
|
||||
agents.forEach(a => {
|
||||
const btn = document.createElement('button');
|
||||
if (!a.detected) {
|
||||
btn.className = 'btn';
|
||||
btn.disabled = true;
|
||||
btn.textContent = a.name + ' (Not Found)';
|
||||
} else if (a.configured) {
|
||||
btn.className = 'btnGood';
|
||||
btn.textContent = a.name + ' (Configured)';
|
||||
} else {
|
||||
btn.className = 'btnPrimary';
|
||||
btn.textContent = a.name + ' (Configure)';
|
||||
}
|
||||
btn.onclick = async () => {
|
||||
if (!a.detected) return;
|
||||
try {
|
||||
await window.pp_configure_agent(a.id);
|
||||
toast('Configured ' + a.name);
|
||||
refreshAgents();
|
||||
} catch(e) { toast(e.message||String(e)); }
|
||||
};
|
||||
list.appendChild(btn);
|
||||
});
|
||||
} catch(e) {
|
||||
list.innerHTML = '<div style="color:var(--muted);font-size:13px">Error: ' + (e.message||e) + '</div>';
|
||||
}
|
||||
}
|
||||
|
||||
async function refreshMappings() {
|
||||
const list = $('mappingsList');
|
||||
try {
|
||||
const mappings = await window.pp_get_model_mappings();
|
||||
if(!mappings || mappings.length === 0) {
|
||||
list.innerHTML = '<div style="color:var(--muted);font-size:13px">No mappings configured</div>';
|
||||
return;
|
||||
}
|
||||
list.innerHTML = '<div style="display:flex;flex-direction:column;gap:6px">' +
|
||||
mappings.map((m, i) =>
|
||||
'<div style="display:flex;align-items:center;gap:8px;padding:8px 10px;background:rgba(0,0,0,.25);border-radius:6px;font-size:13px">' +
|
||||
'<code style="color:var(--brand)">' + m.from + '</code>' +
|
||||
'<span style="color:var(--muted)">→</span>' +
|
||||
'<code style="color:var(--good)">' + m.to + '</code>' +
|
||||
'<button class="btnDanger" style="margin-left:auto;padding:4px 8px;font-size:11px" data-idx="' + i + '">Remove</button>' +
|
||||
'</div>'
|
||||
).join('') + '</div>';
|
||||
list.querySelectorAll('button[data-idx]').forEach(btn => {
|
||||
btn.onclick = async () => {
|
||||
const idx = parseInt(btn.dataset.idx);
|
||||
const updated = mappings.filter((_, i) => i !== idx);
|
||||
try {
|
||||
await window.pp_save_model_mappings(updated);
|
||||
toast('Mapping removed');
|
||||
refreshMappings();
|
||||
} catch(e) { toast(e.message||String(e)); }
|
||||
};
|
||||
});
|
||||
} catch(e) {
|
||||
list.innerHTML = '<div style="color:var(--muted);font-size:13px">Error loading mappings</div>';
|
||||
}
|
||||
}
|
||||
|
||||
async function init() {
|
||||
try{
|
||||
const priv = await window.pp_get_oauth_private();
|
||||
$('privateOAuth').checked = !!priv;
|
||||
}catch(e){}
|
||||
await refreshStatus();
|
||||
await refreshAgents();
|
||||
await refreshMappings();
|
||||
setInterval(refreshStatus, 1200);
|
||||
}
|
||||
|
||||
@@ -733,6 +900,21 @@ func controlCenterHTML() string {
|
||||
$('oauthQwen').addEventListener('click', oauth('qwen'));
|
||||
$('oauthIflow').addEventListener('click', oauth('iflow'));
|
||||
|
||||
$('addMappingBtn').addEventListener('click', async () => {
|
||||
const from = $('mapFrom').value.trim();
|
||||
const to = $('mapTo').value.trim();
|
||||
if (!from || !to) { toast('Both fields required'); return; }
|
||||
try {
|
||||
const existing = await window.pp_get_model_mappings() || [];
|
||||
existing.push({ from, to, enabled: true });
|
||||
await window.pp_save_model_mappings(existing);
|
||||
$('mapFrom').value = '';
|
||||
$('mapTo').value = '';
|
||||
toast('Mapping added');
|
||||
refreshMappings();
|
||||
} catch(e) { toast(e.message||String(e)); }
|
||||
});
|
||||
|
||||
init();
|
||||
</script>
|
||||
</body>
|
||||
@@ -751,3 +933,132 @@ func messageBox(title, text string) error {
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// detectCLIAgents returns a list of detected CLI agents
|
||||
func detectCLIAgents() []map[string]any {
|
||||
agents := []map[string]any{}
|
||||
|
||||
// Claude Code
|
||||
claude := map[string]any{
|
||||
"id": "claude",
|
||||
"name": "Claude Code",
|
||||
"detected": false,
|
||||
"configured": false,
|
||||
}
|
||||
if path, err := exec.LookPath("claude"); err == nil {
|
||||
claude["detected"] = true
|
||||
claude["binary_path"] = path
|
||||
}
|
||||
home, _ := os.UserHomeDir()
|
||||
claudeConfig := filepath.Join(home, ".claude", "settings.json")
|
||||
if _, err := os.Stat(claudeConfig); err == nil {
|
||||
claude["detected"] = true
|
||||
claude["config_path"] = claudeConfig
|
||||
if content, err := os.ReadFile(claudeConfig); err == nil {
|
||||
if strings.Contains(string(content), "127.0.0.1:8317") || strings.Contains(string(content), "127.0.0.1:8318") {
|
||||
claude["configured"] = true
|
||||
}
|
||||
}
|
||||
}
|
||||
agents = append(agents, claude)
|
||||
|
||||
// Codex CLI
|
||||
codex := map[string]any{
|
||||
"id": "codex",
|
||||
"name": "Codex CLI",
|
||||
"detected": false,
|
||||
"configured": false,
|
||||
}
|
||||
if path, err := exec.LookPath("codex"); err == nil {
|
||||
codex["detected"] = true
|
||||
codex["binary_path"] = path
|
||||
}
|
||||
codexDir := filepath.Join(home, ".codex")
|
||||
if info, err := os.Stat(codexDir); err == nil && info.IsDir() {
|
||||
codex["detected"] = true
|
||||
codex["config_path"] = codexDir
|
||||
codexConfig := filepath.Join(codexDir, "config.toml")
|
||||
if content, err := os.ReadFile(codexConfig); err == nil {
|
||||
if strings.Contains(string(content), "127.0.0.1:8317") || strings.Contains(string(content), "127.0.0.1:8318") {
|
||||
codex["configured"] = true
|
||||
}
|
||||
}
|
||||
}
|
||||
agents = append(agents, codex)
|
||||
|
||||
// Factory Droid
|
||||
droid := map[string]any{
|
||||
"id": "droid",
|
||||
"name": "Factory Droid",
|
||||
"detected": false,
|
||||
"configured": false,
|
||||
}
|
||||
if path, err := exec.LookPath("droid"); err == nil {
|
||||
droid["detected"] = true
|
||||
droid["binary_path"] = path
|
||||
} else if path, err := exec.LookPath("factory"); err == nil {
|
||||
droid["detected"] = true
|
||||
droid["binary_path"] = path
|
||||
}
|
||||
// Droid uses settings.json for runtime config (not config.json)
|
||||
droidSettings := filepath.Join(home, ".factory", "settings.json")
|
||||
if _, err := os.Stat(droidSettings); err == nil {
|
||||
droid["config_path"] = droidSettings
|
||||
droid["detected"] = true
|
||||
if content, err := os.ReadFile(droidSettings); err == nil {
|
||||
// Check for ProxyPilot models in customModels array
|
||||
if strings.Contains(string(content), "ProxyPilot") || strings.Contains(string(content), "127.0.0.1:8318") {
|
||||
droid["configured"] = true
|
||||
}
|
||||
}
|
||||
}
|
||||
agents = append(agents, droid)
|
||||
|
||||
// Gemini CLI
|
||||
gemini := map[string]any{
|
||||
"id": "gemini",
|
||||
"name": "Gemini CLI",
|
||||
"detected": false,
|
||||
"configured": false,
|
||||
}
|
||||
if path, err := exec.LookPath("gemini"); err == nil {
|
||||
gemini["detected"] = true
|
||||
gemini["binary_path"] = path
|
||||
}
|
||||
agents = append(agents, gemini)
|
||||
|
||||
return agents
|
||||
}
|
||||
|
||||
// getModelMappings reads model mappings from the config file
|
||||
func getModelMappings() []map[string]any {
|
||||
home, _ := os.UserHomeDir()
|
||||
mappingsPath := filepath.Join(home, ".proxypilot", "model-mappings.json")
|
||||
|
||||
data, err := os.ReadFile(mappingsPath)
|
||||
if err != nil {
|
||||
return []map[string]any{}
|
||||
}
|
||||
|
||||
var mappings []map[string]any
|
||||
if err := json.Unmarshal(data, &mappings); err != nil {
|
||||
return []map[string]any{}
|
||||
}
|
||||
return mappings
|
||||
}
|
||||
|
||||
// saveModelMappings saves model mappings to the config file
|
||||
func saveModelMappings(mappings []map[string]any) error {
|
||||
home, _ := os.UserHomeDir()
|
||||
mappingsDir := filepath.Join(home, ".proxypilot")
|
||||
if err := os.MkdirAll(mappingsDir, 0755); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
mappingsPath := filepath.Join(mappingsDir, "model-mappings.json")
|
||||
data, err := json.MarshalIndent(mappings, "", " ")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return os.WriteFile(mappingsPath, data, 0644)
|
||||
}
|
||||
|
||||
@@ -81,6 +81,10 @@ func main() {
|
||||
var kiroAWSAuthCode bool
|
||||
var kiroImport bool
|
||||
// var githubCopilotLogin bool // REMOVED - GitHub Copilot excluded
|
||||
var detectAgents bool
|
||||
var setupClaude bool
|
||||
var setupCodex bool
|
||||
var setupDroid bool
|
||||
var projectID string
|
||||
var vertexImport string
|
||||
var configPath string
|
||||
@@ -105,6 +109,10 @@ func main() {
|
||||
flag.BoolVar(&kiroAWSAuthCode, "kiro-aws-authcode", false, "Login to Kiro using AWS Builder ID (authorization code flow, better UX)")
|
||||
flag.BoolVar(&kiroImport, "kiro-import", false, "Import Kiro token from Kiro IDE (~/.aws/sso/cache/kiro-auth-token.json)")
|
||||
// GitHub Copilot login removed
|
||||
flag.BoolVar(&detectAgents, "detect-agents", false, "Detect installed CLI agents")
|
||||
flag.BoolVar(&setupClaude, "setup-claude", false, "Configure Claude Code to use ProxyPilot")
|
||||
flag.BoolVar(&setupCodex, "setup-codex", false, "Configure Codex CLI to use ProxyPilot")
|
||||
flag.BoolVar(&setupDroid, "setup-droid", false, "Configure Factory Droid to use ProxyPilot")
|
||||
flag.StringVar(&projectID, "project_id", "", "Project ID (Gemini only, not required)")
|
||||
flag.StringVar(&configPath, "config", DefaultConfigPath, "Configure File Path")
|
||||
flag.StringVar(&vertexImport, "vertex-import", "", "Import Vertex service account key JSON file")
|
||||
@@ -533,6 +541,14 @@ func main() {
|
||||
cmd.DoKiroAWSAuthCodeLogin(cfg, options)
|
||||
} else if kiroImport {
|
||||
cmd.DoKiroImport(cfg, options)
|
||||
} else if detectAgents {
|
||||
cmd.DoDetectAgents()
|
||||
} else if setupClaude {
|
||||
cmd.DoSetupClaude(cfg)
|
||||
} else if setupCodex {
|
||||
cmd.DoSetupCodex(cfg)
|
||||
} else if setupDroid {
|
||||
cmd.DoSetupDroid(cfg)
|
||||
} else {
|
||||
// In cloud deploy mode without config file, just wait for shutdown signals
|
||||
if isCloudDeploy && !configFileExists {
|
||||
|
||||
Binary file not shown.
@@ -0,0 +1,40 @@
|
||||
{
|
||||
"FixedFileInfo": {
|
||||
"FileVersion": {
|
||||
"Major": 0,
|
||||
"Minor": 1,
|
||||
"Patch": 3,
|
||||
"Build": 0
|
||||
},
|
||||
"ProductVersion": {
|
||||
"Major": 0,
|
||||
"Minor": 1,
|
||||
"Patch": 3,
|
||||
"Build": 0
|
||||
},
|
||||
"FileFlagsMask": "3f",
|
||||
"FileFlags ": "00",
|
||||
"FileOS": "040004",
|
||||
"FileType": "01",
|
||||
"FileSubType": "00"
|
||||
},
|
||||
"StringFileInfo": {
|
||||
"Comments": "Local AI Proxy Engine",
|
||||
"CompanyName": "ProxyPilot",
|
||||
"FileDescription": "ProxyPilot Engine",
|
||||
"FileVersion": "0.1.3",
|
||||
"InternalName": "proxypilot-engine",
|
||||
"LegalCopyright": "MIT License",
|
||||
"OriginalFilename": "proxypilot-engine.exe",
|
||||
"ProductName": "ProxyPilot",
|
||||
"ProductVersion": "0.1.3"
|
||||
},
|
||||
"VarFileInfo": {
|
||||
"Translation": {
|
||||
"LangID": "0409",
|
||||
"CharsetID": "04B0"
|
||||
}
|
||||
},
|
||||
"IconPath": "../../static/icon.ico",
|
||||
"ManifestPath": ""
|
||||
}
|
||||
+45
-2
@@ -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:
|
||||
|
||||
@@ -0,0 +1,287 @@
|
||||
# ProxyPilot Desktop App Build Process
|
||||
|
||||
Documentation of the desktop application build and release process established on December 27, 2025.
|
||||
|
||||
## Overview
|
||||
|
||||
ProxyPilot Desktop is a **single executable** that includes both the system tray UI and the proxy engine:
|
||||
- **ProxyPilot.exe** - System tray app with embedded WebView2 dashboard and embedded proxy engine
|
||||
|
||||
## Architecture
|
||||
|
||||
```
|
||||
┌─────────────────────────────────────────────────────────────────┐
|
||||
│ ProxyPilot.exe │
|
||||
│ ┌─────────────┐ ┌─────────────┐ ┌─────────────────────────┐ │
|
||||
│ │ System Tray │ │ WebView2 │ │ Embedded Assets │ │
|
||||
│ │ (systray) │ │ Dashboard │ │ (webui/dist) │ │
|
||||
│ └─────────────┘ └─────────────┘ └─────────────────────────┘ │
|
||||
│ │
|
||||
│ ┌─────────────────────────────────────────────────────────────┐│
|
||||
│ │ Embedded Proxy Engine (goroutine) ││
|
||||
│ │ ┌─────────────┐ ┌─────────────┐ ┌─────────────────────┐ ││
|
||||
│ │ │Thinking Proxy│ │ Routing │ │ Provider Auth │ ││
|
||||
│ │ │ (port 8317) │ │ Engine │ │ Management │ ││
|
||||
│ │ └─────────────┘ └─────────────┘ └─────────────────────┘ ││
|
||||
│ └─────────────────────────────────────────────────────────────┘│
|
||||
└─────────────────────────────────────────────────────────────────┘
|
||||
```
|
||||
|
||||
### In-Process Engine
|
||||
|
||||
The proxy engine runs as a goroutine within the same process, managed by `EmbeddedEngine`:
|
||||
|
||||
```go
|
||||
// cmd/cliproxytray/engine.go
|
||||
type EmbeddedEngine struct {
|
||||
mu sync.Mutex
|
||||
service *cliproxy.Service
|
||||
cancel context.CancelFunc
|
||||
running bool
|
||||
port int
|
||||
// ...
|
||||
}
|
||||
|
||||
func (e *EmbeddedEngine) Start(cfg *config.Config, configPath, password string) error
|
||||
func (e *EmbeddedEngine) Stop() error
|
||||
func (e *EmbeddedEngine) Restart(cfg *config.Config, configPath, password string) error
|
||||
func (e *EmbeddedEngine) Status() Status
|
||||
```
|
||||
|
||||
Benefits of single-binary architecture:
|
||||
- **Simpler deployment**: One file to distribute and install
|
||||
- **Smaller total size**: ~29MB vs ~46MB (shared Go runtime)
|
||||
- **Faster startup**: No process spawning overhead
|
||||
- **Easier debugging**: Single process to monitor
|
||||
|
||||
## Build Process
|
||||
|
||||
### Prerequisites
|
||||
|
||||
```bash
|
||||
# Required tools
|
||||
go install github.com/josephspurrier/goversioninfo/cmd/goversioninfo@latest
|
||||
```
|
||||
|
||||
Inno Setup 6 must be installed for creating the Windows installer.
|
||||
|
||||
### Step 1: Build Web UI Assets
|
||||
|
||||
```bash
|
||||
cd webui
|
||||
npm install
|
||||
npm run build
|
||||
```
|
||||
|
||||
This generates `webui/dist/` with the dashboard assets.
|
||||
|
||||
### Step 2: Create Multi-Resolution Icon
|
||||
|
||||
The icon must have multiple sizes for proper Windows display (16px to 256px):
|
||||
|
||||
```python
|
||||
from PIL import Image
|
||||
|
||||
img = Image.open('static/icon.png')
|
||||
if img.mode != 'RGBA':
|
||||
img = img.convert('RGBA')
|
||||
|
||||
sizes = [(16,16), (24,24), (32,32), (48,48), (64,64), (128,128), (256,256)]
|
||||
img.save('static/icon.ico', format='ICO', sizes=sizes)
|
||||
```
|
||||
|
||||
Copy to tray icon location:
|
||||
```bash
|
||||
cp static/icon.ico internal/trayicon/icon.ico
|
||||
```
|
||||
|
||||
### Step 3: Generate Windows Resource Files
|
||||
|
||||
Generate `.syso` files for embedding icons in executables:
|
||||
|
||||
```bash
|
||||
cd cmd/cliproxytray
|
||||
goversioninfo -icon="../../static/icon.ico"
|
||||
```
|
||||
|
||||
This creates `resource.syso` files that Go automatically includes during build.
|
||||
|
||||
### Step 4: Build Executable
|
||||
|
||||
```bash
|
||||
# Build single-binary tray app (with -H windowsgui to hide console)
|
||||
GOOS=windows GOARCH=amd64 go build -ldflags="-s -w -H windowsgui" -o dist/ProxyPilot.exe ./cmd/cliproxytray
|
||||
```
|
||||
|
||||
### Step 5: Build Installer
|
||||
|
||||
```bash
|
||||
# Using Inno Setup
|
||||
"C:\Users\FSOS\AppData\Local\Programs\Inno Setup 6\ISCC.exe" installer/proxypilot.iss
|
||||
```
|
||||
|
||||
Output: `dist/ProxyPilot-0.1.0-Setup.exe`
|
||||
|
||||
## Key Files
|
||||
|
||||
### Version Info Configuration
|
||||
|
||||
**cmd/cliproxytray/versioninfo.json**
|
||||
```json
|
||||
{
|
||||
"FixedFileInfo": {
|
||||
"FileVersion": {"Major": 0, "Minor": 1, "Patch": 0, "Build": 0},
|
||||
"ProductVersion": {"Major": 0, "Minor": 1, "Patch": 0, "Build": 0}
|
||||
},
|
||||
"StringFileInfo": {
|
||||
"FileDescription": "ProxyPilot Desktop App",
|
||||
"ProductName": "ProxyPilot",
|
||||
"ProductVersion": "0.1.0"
|
||||
},
|
||||
"IconPath": "../../static/icon.ico"
|
||||
}
|
||||
```
|
||||
|
||||
### Embedded Engine
|
||||
|
||||
**cmd/cliproxytray/engine.go** - Manages the in-process proxy service:
|
||||
- Uses `cliproxy.NewBuilder()` SDK to build the service
|
||||
- Runs in a goroutine with cancellable context
|
||||
- Thread-safe start/stop/restart operations
|
||||
|
||||
### Tray Icon Embedding
|
||||
|
||||
**internal/trayicon/ico.go**
|
||||
```go
|
||||
package trayicon
|
||||
|
||||
import (
|
||||
_ "embed"
|
||||
)
|
||||
|
||||
//go:embed icon.ico
|
||||
var proxyPilotICO []byte
|
||||
|
||||
func ProxyPilotICO() []byte {
|
||||
return proxyPilotICO
|
||||
}
|
||||
```
|
||||
|
||||
### Installer Script
|
||||
|
||||
**installer/proxypilot.iss** - Inno Setup script that:
|
||||
- Installs ProxyPilot.exe to `%LOCALAPPDATA%\ProxyPilot`
|
||||
- Creates Start Menu and Desktop shortcuts
|
||||
- Adds optional Windows startup entry
|
||||
- Copies config.example.yaml and creates config.yaml on first install
|
||||
- Uses ProxyPilot icon for installer and shortcuts
|
||||
|
||||
## System Tray Menu
|
||||
|
||||
Simplified flat menu structure:
|
||||
|
||||
```
|
||||
┌─────────────────────┐
|
||||
│ Open Dashboard │ → Opens WebView2 window
|
||||
├─────────────────────┤
|
||||
│ Start/Stop │ → Toggle proxy engine
|
||||
│ Copy API URL │ → Copies http://127.0.0.1:8317/v1
|
||||
├─────────────────────┤
|
||||
│ Quit │ → Exit application
|
||||
└─────────────────────┘
|
||||
```
|
||||
|
||||
## Release Process
|
||||
|
||||
### Create Release on GitHub
|
||||
|
||||
```bash
|
||||
# Tag the release
|
||||
git tag v0.1.0
|
||||
git push origin v0.1.0
|
||||
|
||||
# Create release with assets
|
||||
gh release create v0.1.0 \
|
||||
--title "ProxyPilot v0.1.0" \
|
||||
--notes "Release notes here" \
|
||||
dist/ProxyPilot-0.1.0-Setup.exe \
|
||||
dist/ProxyPilot.exe \
|
||||
config.example.yaml
|
||||
```
|
||||
|
||||
### Update Existing Release
|
||||
|
||||
```bash
|
||||
# Delete old asset
|
||||
gh release delete-asset v0.1.0 ProxyPilot.exe --yes
|
||||
|
||||
# Upload new asset
|
||||
gh release upload v0.1.0 dist/ProxyPilot.exe
|
||||
```
|
||||
|
||||
## Troubleshooting
|
||||
|
||||
### Icon Not Showing in File Explorer
|
||||
|
||||
1. Ensure icon.ico has 256x256 size (required for Windows)
|
||||
2. Clear Windows icon cache: `ie4uinit.exe -show`
|
||||
3. Verify resource.syso was generated after icon update
|
||||
4. Rebuild the executable
|
||||
|
||||
### WebView2 Dashboard Not Loading
|
||||
|
||||
1. Ensure WebView2 runtime is installed on the system
|
||||
2. Check that webui/dist/ assets are embedded (go:embed directive)
|
||||
3. Verify the asset server is starting on a free port
|
||||
|
||||
### Tray Icon Shows Wrong Image
|
||||
|
||||
1. Check internal/trayicon/icon.ico is the correct file
|
||||
2. Rebuild the tray app to re-embed the icon
|
||||
3. Restart the application
|
||||
|
||||
### Engine Not Starting
|
||||
|
||||
1. Check if port 8317/8318 are already in use
|
||||
2. Review logs in `%LOCALAPPDATA%\ProxyPilot\logs\`
|
||||
3. Ensure config.yaml is valid YAML
|
||||
|
||||
## File Structure
|
||||
|
||||
```
|
||||
ProxyPilot/
|
||||
├── cmd/
|
||||
│ └── cliproxytray/
|
||||
│ ├── main_windows.go # Tray app with embedded engine
|
||||
│ ├── engine.go # EmbeddedEngine implementation
|
||||
│ ├── versioninfo.json # Version/icon config
|
||||
│ └── resource.syso # Compiled resources
|
||||
├── internal/
|
||||
│ └── trayicon/
|
||||
│ ├── ico.go # Embeds icon.ico
|
||||
│ └── icon.ico # ProxyPilot logo (multi-res)
|
||||
├── static/
|
||||
│ ├── icon.ico # Source icon (7 sizes)
|
||||
│ └── icon.png # Original PNG logo
|
||||
├── webui/
|
||||
│ ├── src/ # React dashboard source
|
||||
│ └── dist/ # Built assets (embedded)
|
||||
├── installer/
|
||||
│ └── proxypilot.iss # Inno Setup script
|
||||
└── dist/ # Distribution files
|
||||
└── ProxyPilot.exe # Single-binary (~29MB)
|
||||
```
|
||||
|
||||
## Version History
|
||||
|
||||
- **v0.2.0** (2025-12-27) - Single-binary architecture
|
||||
- Embedded proxy engine runs in-process
|
||||
- Removed separate proxypilot-engine.exe
|
||||
- Reduced total size from ~46MB to ~29MB
|
||||
- Faster startup with no process spawning
|
||||
|
||||
- **v0.1.0** (2025-12-27) - Initial desktop app release
|
||||
- System tray with minimal menu
|
||||
- Embedded WebView2 dashboard
|
||||
- Multi-resolution icon support
|
||||
- Single-file Windows installer
|
||||
@@ -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)?
|
||||
@@ -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)
|
||||
@@ -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).
|
||||
|
||||
@@ -1,171 +0,0 @@
|
||||
# Windows UI for ProxyPilot — Spec (Wails + Tauri)
|
||||
|
||||
This is the Windows-specific portion of the desktop UI plan. Shared (cross-platform) decisions and architecture live in `docs/desktop-ui-spec.md`.
|
||||
|
||||
## Goals
|
||||
|
||||
- Provide a Windows-friendly way to run and manage `ProxyPilot` without PowerShell.
|
||||
- Surface request/log diagnostics to reduce “prompt too long” + debugging pain.
|
||||
- Support two desktop shells over time:
|
||||
- Primary: Wails (Go backend + web UI)
|
||||
- Secondary/experimental: Tauri (Rust + web UI)
|
||||
- Share logic via a Go control layer so features don’t fork.
|
||||
|
||||
## Non-goals (v1)
|
||||
|
||||
- Replacing the existing Management API UI shipped with the server.
|
||||
- Full cross-platform parity (macOS/Linux can come later).
|
||||
- Full OAuth flows inside the desktop UI (v1 can “open browser” and show status).
|
||||
- Becoming an agent/IDE: this is a small controller + diagnostics utility.
|
||||
|
||||
## Product decisions (to avoid bikeshedding)
|
||||
|
||||
- v1 ships as Wails.
|
||||
- Tauri is optional later; it should reuse the same control layer via a helper CLI.
|
||||
- The UI controls an existing `proxypilot-engine.exe` (built by the UI or already present).
|
||||
- The UI should not require elevated privileges and should default to localhost-only behavior.
|
||||
|
||||
## Personas / Use-cases
|
||||
|
||||
- “I just want it running”: start/stop, see port, see health.
|
||||
- “I need to debug”: view logs, open request logs, copy diagnostics.
|
||||
- “I’m configuring tools”: show local base URL, API key hints, and quick-copy snippets.
|
||||
- “I want it to launch on login”: enable/disable autostart (per-user).
|
||||
|
||||
## UX requirements
|
||||
|
||||
- Tray-first: app sits in the system tray with minimal friction.
|
||||
- Single click status: Running/Stopped + port + quick actions.
|
||||
- No admin required for normal operation.
|
||||
- Clear failure reasons: missing config, port in use, binary missing, auth dir missing.
|
||||
|
||||
## Functional requirements (v1)
|
||||
|
||||
### Process control
|
||||
|
||||
- Build (optional): build `cmd/server` into `bin/proxypilot-engine.exe`.
|
||||
- Start:
|
||||
- Uses a selectable config path (default: repo `config.yaml` if present).
|
||||
- Writes logs to `logs/proxypilot-engine.out.log` and `logs/proxypilot-engine.err.log`.
|
||||
- Stop:
|
||||
- Prefer graceful stop; fallback to kill process if needed.
|
||||
- Restart.
|
||||
- Detect running instance:
|
||||
- PID-based if started by UI, plus best-effort discovery by listening port.
|
||||
- If an instance is running but not owned by the UI, show “External” and offer read-only “Attach” (don’t blindly kill it).
|
||||
|
||||
### Status & health
|
||||
|
||||
- Show:
|
||||
- port (from config or detection)
|
||||
- reachable status via `GET http://127.0.0.1:<port>/v1/models` (or a future `/healthz`)
|
||||
- last start time and PID (when known)
|
||||
- Buttons:
|
||||
- Open Management UI (browser)
|
||||
- Open logs folder
|
||||
- Copy base URL (`http://127.0.0.1:<port>` and optionally `/v1`)
|
||||
|
||||
### Config
|
||||
|
||||
- Config path selector (browse + remember last choice).
|
||||
- Validate YAML on save (or warn on parse failure).
|
||||
- Minimal editor:
|
||||
- open in external editor, and/or inline text editor with Save.
|
||||
- Safe defaults:
|
||||
- warn if management routes are remotely accessible.
|
||||
|
||||
### Autostart (Windows)
|
||||
|
||||
- Enable/disable per-user startup on Windows.
|
||||
- Backend supports:
|
||||
- registry `HKCU\Software\Microsoft\Windows\CurrentVersion\Run`
|
||||
- scheduled task fallback (optional; clearly labeled)
|
||||
|
||||
### Quick-copy snippets (v1)
|
||||
|
||||
- Show ready-to-paste snippets:
|
||||
- `OPENAI_BASE_URL=http://127.0.0.1:<port>/v1`
|
||||
- `OPENAI_API_KEY=<your local key>`
|
||||
- curl sanity check for `GET /v1/models`
|
||||
|
||||
### Diagnostics / support bundle (v1.1)
|
||||
|
||||
- One-click “Copy diagnostics”:
|
||||
- version, config path, port, PID, last 200 lines of logs
|
||||
- recent request logs list (or last N request IDs)
|
||||
|
||||
## Shared Go control layer (Windows notes)
|
||||
|
||||
Wails and Tauri should both call the same operations (process management, config, logs, autostart, health checks). See `docs/desktop-ui-spec.md` for the proposed package/API shape.
|
||||
|
||||
Windows-specific notes:
|
||||
|
||||
- Persist UI state in `%LOCALAPPDATA%/ProxyPilot/ui-state.json`.
|
||||
- Prefer discover-by-port over discover-by-process-name.
|
||||
- When opening folders/URLs, use native Windows shell APIs (no admin).
|
||||
|
||||
## Wails app (primary) — MVP
|
||||
|
||||
### Packaging
|
||||
|
||||
- Ship `ProxyPilot.exe` with embedded web UI assets.
|
||||
- Bundle or download `proxypilot-engine.exe` (open decision).
|
||||
|
||||
### Screens
|
||||
|
||||
- Tray menu:
|
||||
- Start / Stop / Restart
|
||||
- Open UI
|
||||
- Open logs
|
||||
- Quit
|
||||
- Main window:
|
||||
- Status card (Running/Stopped, port, last error)
|
||||
- Buttons: Start/Stop/Restart, Open Management UI, Open Logs
|
||||
- Config path selector + Open config
|
||||
- Autostart toggle
|
||||
- Copy snippets section (base URL + key + curl check)
|
||||
|
||||
### First-run UX
|
||||
|
||||
- If no config path is set:
|
||||
- Detect `config.yaml` in common locations (repo root, `%LOCALAPPDATA%/ProxyPilot/`, user home).
|
||||
- Offer “Use detected config” or “Browse…”.
|
||||
- If no API key is configured:
|
||||
- Offer “Generate local key” (writes to config) or “Open config” guidance.
|
||||
|
||||
## Tauri app (secondary) — MVP
|
||||
|
||||
### Purpose
|
||||
|
||||
- Evaluate smaller footprint distribution vs Wails.
|
||||
- Prototype a more native-feeling tray + notifications.
|
||||
|
||||
### Integration options
|
||||
|
||||
- Option A (recommended): Tauri calls a helper CLI (`cliproxyctl.exe`).
|
||||
- Option B: Tauri starts a small local HTTP control server.
|
||||
|
||||
## Security considerations
|
||||
|
||||
- Default to localhost-only assumptions.
|
||||
- Don’t display or log API keys unless explicitly requested.
|
||||
- When enabling autostart, show what command will run.
|
||||
- Avoid writing outside the user profile unless configured.
|
||||
|
||||
## Open decisions
|
||||
|
||||
- Where config lives by default (repo-local vs user config dir).
|
||||
- Whether UI bundles the server binary or manages an existing install.
|
||||
- Whether to add a stable `/healthz` endpoint for fast health checks.
|
||||
- Whether “Generate local key” should mutate `config.yaml` or use a UI-only key store.
|
||||
- Whether the UI should surface auth selection state (primary/backup) for providers like `antigravity`.
|
||||
|
||||
## Acceptance criteria (v1)
|
||||
|
||||
- Installing and launching the app shows a tray icon and a status window.
|
||||
- Start/Stop/Restart works reliably across app restarts (PID state persisted).
|
||||
- Health check distinguishes:
|
||||
- process running but endpoint not reachable
|
||||
- endpoint reachable with `GET /v1/models` returning 200
|
||||
- Open logs opens the correct folder.
|
||||
- Copy base URL copies `http://127.0.0.1:<port>` (and optionally `/v1`).
|
||||
+26
-19
@@ -1,6 +1,6 @@
|
||||
#define AppName "ProxyPilot"
|
||||
#ifndef AppVersion
|
||||
#define AppVersion "0.0.0-dev"
|
||||
#define AppVersion "0.1.0"
|
||||
#endif
|
||||
#ifndef RepoRoot
|
||||
#define RepoRoot ".."
|
||||
@@ -14,41 +14,48 @@ AppId={{3D9053A0-3F6A-47D7-9D91-8BB1D1CC2A4E}
|
||||
AppName={#AppName}
|
||||
AppVersion={#AppVersion}
|
||||
AppPublisher=ProxyPilot
|
||||
SetupIconFile={#RepoRoot}\\bin\\ProxyPilot.ico
|
||||
DefaultDirName={localappdata}\\{#AppName}
|
||||
AppPublisherURL=https://github.com/Finesssee/ProxyPilot
|
||||
DefaultDirName={localappdata}\{#AppName}
|
||||
DefaultGroupName={#AppName}
|
||||
DisableProgramGroupPage=yes
|
||||
PrivilegesRequired=lowest
|
||||
Compression=lzma2
|
||||
Compression=lzma2/ultra64
|
||||
SolidCompression=yes
|
||||
WizardStyle=modern
|
||||
ArchitecturesInstallIn64BitMode=x64
|
||||
ArchitecturesInstallIn64BitMode=x64compatible
|
||||
OutputDir={#OutDir}
|
||||
OutputBaseFilename=ProxyPilot-Setup
|
||||
UninstallDisplayIcon={app}\\ProxyPilot.ico
|
||||
OutputBaseFilename=ProxyPilot-{#AppVersion}-Setup
|
||||
SetupIconFile={#RepoRoot}\static\icon.ico
|
||||
UninstallDisplayIcon={app}\icon.ico
|
||||
|
||||
[Languages]
|
||||
Name: "english"; MessagesFile: "compiler:Default.isl"
|
||||
|
||||
[Tasks]
|
||||
Name: "desktopicon"; Description: "Create a &desktop icon"; GroupDescription: "Additional icons:"
|
||||
Name: "startupicon"; Description: "Start ProxyPilot when Windows starts"; GroupDescription: "Startup:"
|
||||
|
||||
[Files]
|
||||
Source: "{#RepoRoot}\\bin\\ProxyPilot.exe"; DestDir: "{app}"; Flags: ignoreversion
|
||||
Source: "{#RepoRoot}\\bin\\ProxyPilotUI.exe"; DestDir: "{app}"; Flags: ignoreversion
|
||||
Source: "{#RepoRoot}\\bin\\proxypilot-engine.exe"; DestDir: "{app}"; Flags: ignoreversion
|
||||
Source: "{#RepoRoot}\\bin\\ProxyPilot.ico"; DestDir: "{app}"; Flags: ignoreversion
|
||||
Source: "{#RepoRoot}\\config.example.yaml"; DestDir: "{app}"; Flags: ignoreversion
|
||||
; Main app (tray + dashboard UI)
|
||||
Source: "{#OutDir}\ProxyPilot.exe"; DestDir: "{app}"; Flags: ignoreversion
|
||||
; Config
|
||||
Source: "{#RepoRoot}\config.example.yaml"; DestDir: "{app}"; Flags: ignoreversion
|
||||
; Icons
|
||||
Source: "{#RepoRoot}\static\icon.ico"; DestDir: "{app}"; Flags: ignoreversion
|
||||
Source: "{#RepoRoot}\static\icon.png"; DestDir: "{app}"; Flags: ignoreversion
|
||||
|
||||
[Icons]
|
||||
Name: "{autoprograms}\\{#AppName}"; Filename: "{app}\\ProxyPilot.exe"; WorkingDir: "{app}"; IconFilename: "{app}\\ProxyPilot.ico"
|
||||
Name: "{autodesktop}\\{#AppName}"; Filename: "{app}\\ProxyPilot.exe"; Tasks: desktopicon; WorkingDir: "{app}"; IconFilename: "{app}\\ProxyPilot.ico"
|
||||
Name: "{autoprograms}\{#AppName}"; Filename: "{app}\ProxyPilot.exe"; WorkingDir: "{app}"; IconFilename: "{app}\icon.ico"
|
||||
Name: "{autodesktop}\{#AppName}"; Filename: "{app}\ProxyPilot.exe"; Tasks: desktopicon; WorkingDir: "{app}"; IconFilename: "{app}\icon.ico"
|
||||
|
||||
[Registry]
|
||||
Root: HKCU; Subkey: "Software\Microsoft\Windows\CurrentVersion\Run"; ValueType: string; ValueName: "ProxyPilot"; ValueData: """{app}\ProxyPilot.exe"""; Flags: uninsdeletevalue; Tasks: startupicon
|
||||
|
||||
[Run]
|
||||
Filename: "{app}\\ProxyPilot.exe"; Description: "Launch {#AppName}"; Flags: nowait postinstall skipifsilent
|
||||
Filename: "{app}\ProxyPilot.exe"; Description: "Launch {#AppName}"; Flags: nowait postinstall skipifsilent
|
||||
|
||||
[UninstallRun]
|
||||
Filename: "cmd.exe"; Parameters: "/c reg delete HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Run /v ProxyPilot /f >nul 2>nul"; Flags: runhidden
|
||||
Filename: "taskkill"; Parameters: "/F /IM ProxyPilot.exe"; Flags: runhidden; RunOnceId: "KillTray"
|
||||
|
||||
[UninstallDelete]
|
||||
Type: filesandordirs; Name: "{app}"
|
||||
@@ -61,11 +68,11 @@ var
|
||||
begin
|
||||
if CurStep = ssPostInstall then
|
||||
begin
|
||||
ConfigYaml := ExpandConstant('{app}\\config.yaml');
|
||||
ExampleYaml := ExpandConstant('{app}\\config.example.yaml');
|
||||
ConfigYaml := ExpandConstant('{app}\config.yaml');
|
||||
ExampleYaml := ExpandConstant('{app}\config.example.yaml');
|
||||
if (not FileExists(ConfigYaml)) and FileExists(ExampleYaml) then
|
||||
begin
|
||||
FileCopy(ExampleYaml, ConfigYaml, False);
|
||||
CopyFile(ExampleYaml, ConfigYaml, False);
|
||||
end;
|
||||
end;
|
||||
end;
|
||||
|
||||
@@ -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")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,171 @@
|
||||
package management
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/router-for-me/CLIProxyAPI/v6/internal/memory"
|
||||
)
|
||||
|
||||
// GetHarnessFiles returns the list of harness files for a session.
|
||||
// GET /v0/management/harness/files?session=...
|
||||
func (h *Handler) GetHarnessFiles(c *gin.Context) {
|
||||
base := memoryBaseDir()
|
||||
if base == "" {
|
||||
c.JSON(http.StatusOK, gin.H{"files": []string{}})
|
||||
return
|
||||
}
|
||||
session := strings.TrimSpace(c.Query("session"))
|
||||
if session == "" {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "missing session"})
|
||||
return
|
||||
}
|
||||
store := memory.NewFileStore(base)
|
||||
files, err := store.ListHarnessFiles(session)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
if files == nil {
|
||||
files = []string{}
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"files": files})
|
||||
}
|
||||
|
||||
// GetHarnessFile returns the content of a specific harness file.
|
||||
// GET /v0/management/harness/file?session=...&filename=...
|
||||
func (h *Handler) GetHarnessFile(c *gin.Context) {
|
||||
base := memoryBaseDir()
|
||||
if base == "" {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "memory not configured"})
|
||||
return
|
||||
}
|
||||
session := strings.TrimSpace(c.Query("session"))
|
||||
if session == "" {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "missing session"})
|
||||
return
|
||||
}
|
||||
filename := strings.TrimSpace(c.Query("filename"))
|
||||
if filename == "" {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "missing filename"})
|
||||
return
|
||||
}
|
||||
store := memory.NewFileStore(base)
|
||||
content := store.ReadHarnessFile(session, filename, 100000)
|
||||
c.JSON(http.StatusOK, gin.H{"filename": filename, "content": content})
|
||||
}
|
||||
|
||||
type putHarnessFileRequest struct {
|
||||
Session string `json:"session"`
|
||||
Filename string `json:"filename"`
|
||||
Content string `json:"content"`
|
||||
}
|
||||
|
||||
// PutHarnessFile creates or updates a harness file.
|
||||
// PUT /v0/management/harness/file
|
||||
// Body: {"session": "...", "filename": "...", "content": "..."}
|
||||
func (h *Handler) PutHarnessFile(c *gin.Context) {
|
||||
base := memoryBaseDir()
|
||||
if base == "" {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "memory not configured"})
|
||||
return
|
||||
}
|
||||
var req putHarnessFileRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid body"})
|
||||
return
|
||||
}
|
||||
if strings.TrimSpace(req.Session) == "" {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "missing session"})
|
||||
return
|
||||
}
|
||||
if strings.TrimSpace(req.Filename) == "" {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "missing filename"})
|
||||
return
|
||||
}
|
||||
store := memory.NewFileStore(base)
|
||||
if err := store.WriteHarnessFile(req.Session, req.Filename, req.Content, 100000); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"status": "ok"})
|
||||
}
|
||||
|
||||
// ExportHarness returns all harness files for a session as JSON.
|
||||
// GET /v0/management/harness/export?session=...
|
||||
func (h *Handler) ExportHarness(c *gin.Context) {
|
||||
base := memoryBaseDir()
|
||||
if base == "" {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "memory not configured"})
|
||||
return
|
||||
}
|
||||
session := strings.TrimSpace(c.Query("session"))
|
||||
if session == "" {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "missing session"})
|
||||
return
|
||||
}
|
||||
store := memory.NewFileStore(base)
|
||||
files, err := store.ListHarnessFiles(session)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
result := make(map[string]string, len(files))
|
||||
for _, filename := range files {
|
||||
content := store.ReadHarnessFile(session, filename, 100000)
|
||||
result[filename] = content
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"session": session, "files": result})
|
||||
}
|
||||
|
||||
type importHarnessRequest struct {
|
||||
Session string `json:"session"`
|
||||
Files map[string]string `json:"files"`
|
||||
}
|
||||
|
||||
// ImportHarness imports harness files from JSON.
|
||||
// POST /v0/management/harness/import
|
||||
// Body: {"session": "...", "files": {"feature_list.json": "...", "claude-progress.txt": "..."}}
|
||||
func (h *Handler) ImportHarness(c *gin.Context) {
|
||||
base := memoryBaseDir()
|
||||
if base == "" {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "memory not configured"})
|
||||
return
|
||||
}
|
||||
var req importHarnessRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid body"})
|
||||
return
|
||||
}
|
||||
if strings.TrimSpace(req.Session) == "" {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "missing session"})
|
||||
return
|
||||
}
|
||||
if len(req.Files) == 0 {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "missing files"})
|
||||
return
|
||||
}
|
||||
store := memory.NewFileStore(base)
|
||||
var errors []string
|
||||
imported := 0
|
||||
for filename, content := range req.Files {
|
||||
if strings.TrimSpace(filename) == "" {
|
||||
continue
|
||||
}
|
||||
if err := store.WriteHarnessFile(req.Session, filename, content, 100000); err != nil {
|
||||
errors = append(errors, filename+": "+err.Error())
|
||||
continue
|
||||
}
|
||||
imported++
|
||||
}
|
||||
if len(errors) > 0 {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{
|
||||
"status": "partial",
|
||||
"imported": imported,
|
||||
"errors": errors,
|
||||
})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"status": "ok", "imported": imported})
|
||||
}
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/router-for-me/CLIProxyAPI/v6/internal/integrations"
|
||||
)
|
||||
|
||||
// GetIntegrationsStatus returns the list of detected tools and their configuration status.
|
||||
@@ -35,3 +36,45 @@ func (h *Handler) PostIntegrationConfigure(c *gin.Context) {
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{"status": "configured", "id": id})
|
||||
}
|
||||
|
||||
// GetCLIAgents returns the list of detected CLI agents and IDEs.
|
||||
func (h *Handler) GetCLIAgents(c *gin.Context) {
|
||||
proxyURL := h.cfg.ProxyURL
|
||||
if proxyURL == "" {
|
||||
proxyURL = "http://localhost:8080"
|
||||
}
|
||||
agents := integrations.DetectCLIAgents(proxyURL)
|
||||
c.JSON(http.StatusOK, gin.H{"agents": agents})
|
||||
}
|
||||
|
||||
// PostCLIAgentConfigure configures a CLI agent to use the proxy.
|
||||
func (h *Handler) PostCLIAgentConfigure(c *gin.Context) {
|
||||
id := c.Param("id")
|
||||
if id == "" {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "missing agent id"})
|
||||
return
|
||||
}
|
||||
proxyURL := h.cfg.ProxyURL
|
||||
if proxyURL == "" {
|
||||
proxyURL = "http://localhost:8080"
|
||||
}
|
||||
if err := integrations.ConfigureCLIAgent(id, proxyURL); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"status": "configured", "id": id})
|
||||
}
|
||||
|
||||
// PostCLIAgentUnconfigure removes proxy configuration from a CLI agent.
|
||||
func (h *Handler) PostCLIAgentUnconfigure(c *gin.Context) {
|
||||
id := c.Param("id")
|
||||
if id == "" {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "missing agent id"})
|
||||
return
|
||||
}
|
||||
if err := integrations.UnconfigureCLIAgent(id); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"status": "unconfigured", "id": id})
|
||||
}
|
||||
|
||||
@@ -0,0 +1,95 @@
|
||||
package management
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/router-for-me/CLIProxyAPI/v6/internal/config"
|
||||
)
|
||||
|
||||
// ModelMappingEntry represents a global model mapping for the API
|
||||
type ModelMappingEntry struct {
|
||||
From string `json:"from"`
|
||||
To string `json:"to"`
|
||||
Provider string `json:"provider,omitempty"`
|
||||
Enabled bool `json:"enabled"`
|
||||
}
|
||||
|
||||
// GetModelMappings returns the current global model mappings
|
||||
func (h *Handler) GetModelMappings(c *gin.Context) {
|
||||
if h.cfg == nil {
|
||||
c.JSON(http.StatusOK, gin.H{"mappings": []any{}})
|
||||
return
|
||||
}
|
||||
|
||||
mappings := make([]ModelMappingEntry, 0, len(h.cfg.GlobalModelMappings))
|
||||
for _, m := range h.cfg.GlobalModelMappings {
|
||||
mappings = append(mappings, ModelMappingEntry{
|
||||
From: m.From,
|
||||
To: m.To,
|
||||
Provider: m.Provider,
|
||||
Enabled: m.IsEnabled(),
|
||||
})
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{"mappings": mappings})
|
||||
}
|
||||
|
||||
// SetModelMappings replaces all global model mappings
|
||||
func (h *Handler) SetModelMappings(c *gin.Context) {
|
||||
var req struct {
|
||||
Mappings []ModelMappingEntry `json:"mappings"`
|
||||
}
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid body"})
|
||||
return
|
||||
}
|
||||
|
||||
if h.cfg == nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "config not loaded"})
|
||||
return
|
||||
}
|
||||
|
||||
// Convert to config type
|
||||
newMappings := make([]config.GlobalModelMapping, 0, len(req.Mappings))
|
||||
for _, m := range req.Mappings {
|
||||
enabled := m.Enabled
|
||||
newMappings = append(newMappings, config.GlobalModelMapping{
|
||||
From: m.From,
|
||||
To: m.To,
|
||||
Provider: m.Provider,
|
||||
Enabled: &enabled,
|
||||
})
|
||||
}
|
||||
|
||||
h.cfg.GlobalModelMappings = newMappings
|
||||
|
||||
// Try to save config if configPath is available
|
||||
// Note: This requires the config path to be stored somewhere accessible
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{"status": "ok", "count": len(newMappings)})
|
||||
}
|
||||
|
||||
// TestModelMapping tests a model name against global mappings
|
||||
func (h *Handler) TestModelMapping(c *gin.Context) {
|
||||
model := c.Query("model")
|
||||
provider := c.Query("provider")
|
||||
|
||||
if model == "" {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "missing model parameter"})
|
||||
return
|
||||
}
|
||||
|
||||
if h.cfg == nil {
|
||||
c.JSON(http.StatusOK, gin.H{"model": model, "mapped_to": model, "matched": false})
|
||||
return
|
||||
}
|
||||
|
||||
mappedTo := h.cfg.LookupGlobalModelMapping(model, provider)
|
||||
if mappedTo == "" {
|
||||
c.JSON(http.StatusOK, gin.H{"model": model, "mapped_to": model, "matched": false})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{"model": model, "mapped_to": mappedTo, "matched": true})
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package management
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/router-for-me/CLIProxyAPI/v6/internal/api/middleware"
|
||||
)
|
||||
|
||||
// GetRequests returns the list of recent requests from the in-memory ring buffer.
|
||||
func (h *Handler) GetRequests(c *gin.Context) {
|
||||
requests := middleware.GetRequestMonitor()
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"requests": requests,
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,145 @@
|
||||
package management
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"sync"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/router-for-me/CLIProxyAPI/v6/internal/updates"
|
||||
)
|
||||
|
||||
// updateState tracks the current update operation state.
|
||||
type updateState struct {
|
||||
mu sync.RWMutex
|
||||
downloading bool
|
||||
progress updates.DownloadProgress
|
||||
downloadResult *updates.DownloadResult
|
||||
lastError string
|
||||
}
|
||||
|
||||
var currentUpdate = &updateState{}
|
||||
|
||||
func (h *Handler) GetUpdateInfo(c *gin.Context) {
|
||||
info, err := updates.CheckForUpdates()
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, info)
|
||||
}
|
||||
|
||||
// GetUpdateStatus returns the current update download status.
|
||||
func (h *Handler) GetUpdateStatus(c *gin.Context) {
|
||||
currentUpdate.mu.RLock()
|
||||
defer currentUpdate.mu.RUnlock()
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"downloading": currentUpdate.downloading,
|
||||
"progress": currentUpdate.progress,
|
||||
"ready": currentUpdate.downloadResult != nil,
|
||||
"error": currentUpdate.lastError,
|
||||
})
|
||||
}
|
||||
|
||||
// DownloadUpdate starts downloading an update.
|
||||
func (h *Handler) DownloadUpdate(c *gin.Context) {
|
||||
var req struct {
|
||||
Version string `json:"version"`
|
||||
}
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "version is required"})
|
||||
return
|
||||
}
|
||||
|
||||
currentUpdate.mu.Lock()
|
||||
if currentUpdate.downloading {
|
||||
currentUpdate.mu.Unlock()
|
||||
c.JSON(http.StatusConflict, gin.H{"error": "download already in progress"})
|
||||
return
|
||||
}
|
||||
currentUpdate.downloading = true
|
||||
currentUpdate.progress = updates.DownloadProgress{}
|
||||
currentUpdate.downloadResult = nil
|
||||
currentUpdate.lastError = ""
|
||||
currentUpdate.mu.Unlock()
|
||||
|
||||
// Start download in background
|
||||
go func() {
|
||||
result, err := updates.DownloadUpdate(req.Version, func(progress updates.DownloadProgress) {
|
||||
currentUpdate.mu.Lock()
|
||||
currentUpdate.progress = progress
|
||||
currentUpdate.mu.Unlock()
|
||||
})
|
||||
|
||||
currentUpdate.mu.Lock()
|
||||
currentUpdate.downloading = false
|
||||
if err != nil {
|
||||
currentUpdate.lastError = err.Error()
|
||||
} else {
|
||||
currentUpdate.downloadResult = result
|
||||
}
|
||||
currentUpdate.mu.Unlock()
|
||||
}()
|
||||
|
||||
c.JSON(http.StatusAccepted, gin.H{"message": "download started"})
|
||||
}
|
||||
|
||||
// VerifyUpdate verifies the downloaded update.
|
||||
func (h *Handler) VerifyUpdate(c *gin.Context) {
|
||||
currentUpdate.mu.RLock()
|
||||
result := currentUpdate.downloadResult
|
||||
currentUpdate.mu.RUnlock()
|
||||
|
||||
if result == nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "no download available to verify"})
|
||||
return
|
||||
}
|
||||
|
||||
verifyResult, err := updates.VerifyDownload(result)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, verifyResult)
|
||||
}
|
||||
|
||||
// InstallUpdate installs the downloaded update.
|
||||
func (h *Handler) InstallUpdate(c *gin.Context) {
|
||||
currentUpdate.mu.RLock()
|
||||
downloadResult := currentUpdate.downloadResult
|
||||
currentUpdate.mu.RUnlock()
|
||||
|
||||
if downloadResult == nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "no download available to install"})
|
||||
return
|
||||
}
|
||||
|
||||
// Verify before install
|
||||
verifyResult, err := updates.VerifyDownload(downloadResult)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
if !verifyResult.Valid {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "verification failed: " + verifyResult.Message})
|
||||
return
|
||||
}
|
||||
|
||||
// Prepare the update (extract if needed)
|
||||
executablePath, err := updates.PrepareInstall(downloadResult)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
// Install
|
||||
installResult, err := updates.InstallUpdate(executablePath)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, installResult)
|
||||
}
|
||||
@@ -13,8 +13,12 @@ func (h *Handler) GetUsageStatistics(c *gin.Context) {
|
||||
if h != nil && h.usageStats != nil {
|
||||
snapshot = h.usageStats.Snapshot()
|
||||
}
|
||||
|
||||
usageStats := usage.ComputeUsageStats(snapshot)
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"usage": snapshot,
|
||||
"usage": usageStats,
|
||||
"raw_snapshot": snapshot,
|
||||
"failed_requests": snapshot.FailureCount,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -0,0 +1,203 @@
|
||||
// Package handlers provides HTTP handlers for the API server.
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/router-for-me/CLIProxyAPI/v6/sdk/translator"
|
||||
)
|
||||
|
||||
// TranslatorHandler provides HTTP handlers for translation-related endpoints.
|
||||
type TranslatorHandler struct{}
|
||||
|
||||
// NewTranslatorHandler creates a new translator handler.
|
||||
func NewTranslatorHandler() *TranslatorHandler {
|
||||
return &TranslatorHandler{}
|
||||
}
|
||||
|
||||
// TranslationsMatrixResponse represents the response for the translations matrix endpoint.
|
||||
type TranslationsMatrixResponse struct {
|
||||
Matrix map[string][]string `json:"matrix"`
|
||||
Formats []string `json:"formats"`
|
||||
Total int `json:"total_translations"`
|
||||
Details []translator.TranslationInfo `json:"details,omitempty"`
|
||||
}
|
||||
|
||||
// GetTranslationsMatrix returns the full compatibility matrix of supported translations.
|
||||
// GET /v1/translations
|
||||
func (h *TranslatorHandler) GetTranslationsMatrix(c *gin.Context) {
|
||||
matrix := translator.GetCompatibilityMatrix()
|
||||
formats := translator.GetSupportedFormats()
|
||||
translations := translator.GetAllTranslations()
|
||||
|
||||
// Convert formats to strings
|
||||
formatStrings := make([]string, len(formats))
|
||||
for i, f := range formats {
|
||||
formatStrings[i] = f.String()
|
||||
}
|
||||
|
||||
// Count total translations
|
||||
total := 0
|
||||
for _, targets := range matrix {
|
||||
total += len(targets)
|
||||
}
|
||||
|
||||
// Check if detailed info is requested
|
||||
includeDetails := c.Query("details") == "true"
|
||||
|
||||
response := TranslationsMatrixResponse{
|
||||
Matrix: matrix,
|
||||
Formats: formatStrings,
|
||||
Total: total,
|
||||
}
|
||||
|
||||
if includeDetails {
|
||||
response.Details = translations
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, response)
|
||||
}
|
||||
|
||||
// CheckTranslationResponse represents the response for checking a specific translation.
|
||||
type CheckTranslationResponse struct {
|
||||
Supported bool `json:"supported"`
|
||||
Fallback bool `json:"fallback"`
|
||||
From string `json:"from"`
|
||||
To string `json:"to"`
|
||||
Info *translator.TranslationInfo `json:"info,omitempty"`
|
||||
Alternatives []string `json:"alternatives,omitempty"`
|
||||
}
|
||||
|
||||
// CheckTranslation checks if a specific translation path is supported.
|
||||
// GET /v1/translations/check?from=X&to=Y
|
||||
func (h *TranslatorHandler) CheckTranslation(c *gin.Context) {
|
||||
from := c.Query("from")
|
||||
to := c.Query("to")
|
||||
|
||||
if from == "" || to == "" {
|
||||
c.JSON(http.StatusBadRequest, gin.H{
|
||||
"error": "missing required query parameters",
|
||||
"details": "both 'from' and 'to' query parameters are required",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
fromFormat := translator.FromString(from)
|
||||
toFormat := translator.FromString(to)
|
||||
|
||||
supported := translator.IsTranslationSupported(fromFormat, toFormat)
|
||||
info := translator.GetTranslationInfo(fromFormat, toFormat)
|
||||
|
||||
response := CheckTranslationResponse{
|
||||
Supported: supported,
|
||||
Fallback: false,
|
||||
From: from,
|
||||
To: to,
|
||||
}
|
||||
|
||||
// Include detailed info if translation exists
|
||||
if supported {
|
||||
response.Info = info
|
||||
} else {
|
||||
// Check for potential fallback paths or alternatives
|
||||
matrix := translator.GetCompatibilityMatrix()
|
||||
if targets, exists := matrix[from]; exists {
|
||||
response.Alternatives = targets
|
||||
}
|
||||
|
||||
// Mark as fallback if the request would pass through unchanged
|
||||
// (same format translation is always supported as a no-op)
|
||||
if from == to {
|
||||
response.Fallback = true
|
||||
response.Supported = true
|
||||
}
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, response)
|
||||
}
|
||||
|
||||
// GetTranslationDocs returns markdown documentation for all translations.
|
||||
// GET /v1/translations/docs
|
||||
func (h *TranslatorHandler) GetTranslationDocs(c *gin.Context) {
|
||||
format := c.Query("format")
|
||||
|
||||
switch format {
|
||||
case "mermaid":
|
||||
c.Header("Content-Type", "text/plain; charset=utf-8")
|
||||
c.String(http.StatusOK, translator.GenerateMermaidDiagram())
|
||||
case "summary":
|
||||
c.Header("Content-Type", "text/plain; charset=utf-8")
|
||||
c.String(http.StatusOK, translator.GenerateTranslationSummary())
|
||||
default:
|
||||
// Default to markdown
|
||||
c.Header("Content-Type", "text/markdown; charset=utf-8")
|
||||
c.String(http.StatusOK, translator.GenerateMarkdownDocs())
|
||||
}
|
||||
}
|
||||
|
||||
// ScoreTranslationRequest represents the request body for scoring a translation.
|
||||
type ScoreTranslationRequest struct {
|
||||
From string `json:"from" binding:"required"`
|
||||
To string `json:"to" binding:"required"`
|
||||
Before string `json:"before" binding:"required"`
|
||||
After string `json:"after" binding:"required"`
|
||||
}
|
||||
|
||||
// ScoreTranslation analyzes the quality of a translation.
|
||||
// POST /v1/translations/score
|
||||
func (h *TranslatorHandler) ScoreTranslation(c *gin.Context) {
|
||||
var req ScoreTranslationRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{
|
||||
"error": "invalid request body",
|
||||
"details": err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
fromFormat := translator.FromString(req.From)
|
||||
toFormat := translator.FromString(req.To)
|
||||
|
||||
report := translator.ScoreTranslation(
|
||||
fromFormat,
|
||||
toFormat,
|
||||
[]byte(req.Before),
|
||||
[]byte(req.After),
|
||||
)
|
||||
|
||||
c.JSON(http.StatusOK, report)
|
||||
}
|
||||
|
||||
// CompareStructuresRequest represents the request body for comparing JSON structures.
|
||||
type CompareStructuresRequest struct {
|
||||
Before string `json:"before" binding:"required"`
|
||||
After string `json:"after" binding:"required"`
|
||||
}
|
||||
|
||||
// CompareStructures provides detailed comparison of two JSON structures.
|
||||
// POST /v1/translations/compare
|
||||
func (h *TranslatorHandler) CompareStructures(c *gin.Context) {
|
||||
var req CompareStructuresRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{
|
||||
"error": "invalid request body",
|
||||
"details": err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
comparison, err := translator.CompareJSONStructures(
|
||||
[]byte(req.Before),
|
||||
[]byte(req.After),
|
||||
)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{
|
||||
"error": "failed to compare structures",
|
||||
"details": err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, comparison)
|
||||
}
|
||||
@@ -0,0 +1,414 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/router-for-me/CLIProxyAPI/v6/sdk/translator"
|
||||
)
|
||||
|
||||
func init() {
|
||||
gin.SetMode(gin.TestMode)
|
||||
}
|
||||
|
||||
func setupTestRouter() *gin.Engine {
|
||||
router := gin.New()
|
||||
handler := NewTranslatorHandler()
|
||||
|
||||
v1 := router.Group("/v1")
|
||||
{
|
||||
v1.GET("/translations", handler.GetTranslationsMatrix)
|
||||
v1.GET("/translations/check", handler.CheckTranslation)
|
||||
v1.GET("/translations/docs", handler.GetTranslationDocs)
|
||||
v1.POST("/translations/score", handler.ScoreTranslation)
|
||||
v1.POST("/translations/compare", handler.CompareStructures)
|
||||
}
|
||||
|
||||
return router
|
||||
}
|
||||
|
||||
func TestGetTranslationsMatrix(t *testing.T) {
|
||||
router := setupTestRouter()
|
||||
|
||||
req, _ := http.NewRequest("GET", "/v1/translations", nil)
|
||||
w := httptest.NewRecorder()
|
||||
router.ServeHTTP(w, req)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Errorf("Expected status %d, got %d", http.StatusOK, w.Code)
|
||||
}
|
||||
|
||||
var response TranslationsMatrixResponse
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &response); err != nil {
|
||||
t.Fatalf("Failed to parse response: %v", err)
|
||||
}
|
||||
|
||||
if response.Matrix == nil {
|
||||
t.Error("Matrix should not be nil")
|
||||
}
|
||||
if response.Formats == nil {
|
||||
t.Error("Formats should not be nil")
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetTranslationsMatrix_WithDetails(t *testing.T) {
|
||||
router := setupTestRouter()
|
||||
|
||||
req, _ := http.NewRequest("GET", "/v1/translations?details=true", nil)
|
||||
w := httptest.NewRecorder()
|
||||
router.ServeHTTP(w, req)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Errorf("Expected status %d, got %d", http.StatusOK, w.Code)
|
||||
}
|
||||
|
||||
var response TranslationsMatrixResponse
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &response); err != nil {
|
||||
t.Fatalf("Failed to parse response: %v", err)
|
||||
}
|
||||
|
||||
// When details=true, Details should be populated
|
||||
// (may be empty if no translations registered, but should be non-nil conceptually)
|
||||
}
|
||||
|
||||
func TestCheckTranslation_Supported(t *testing.T) {
|
||||
router := setupTestRouter()
|
||||
|
||||
// Register a test translator
|
||||
translator.Register(translator.FormatOpenAI, translator.FormatClaude, func(model string, data []byte, stream bool) []byte {
|
||||
return data
|
||||
}, translator.ResponseTransform{})
|
||||
defer translator.Unregister(translator.FormatOpenAI, translator.FormatClaude)
|
||||
|
||||
req, _ := http.NewRequest("GET", "/v1/translations/check?from=openai&to=claude", nil)
|
||||
w := httptest.NewRecorder()
|
||||
router.ServeHTTP(w, req)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Errorf("Expected status %d, got %d", http.StatusOK, w.Code)
|
||||
}
|
||||
|
||||
var response CheckTranslationResponse
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &response); err != nil {
|
||||
t.Fatalf("Failed to parse response: %v", err)
|
||||
}
|
||||
|
||||
if !response.Supported {
|
||||
t.Error("Translation should be supported")
|
||||
}
|
||||
if response.From != "openai" {
|
||||
t.Errorf("From = %s, want openai", response.From)
|
||||
}
|
||||
if response.To != "claude" {
|
||||
t.Errorf("To = %s, want claude", response.To)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheckTranslation_NotSupported(t *testing.T) {
|
||||
router := setupTestRouter()
|
||||
|
||||
req, _ := http.NewRequest("GET", "/v1/translations/check?from=unknown&to=unknown2", nil)
|
||||
w := httptest.NewRecorder()
|
||||
router.ServeHTTP(w, req)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Errorf("Expected status %d, got %d", http.StatusOK, w.Code)
|
||||
}
|
||||
|
||||
var response CheckTranslationResponse
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &response); err != nil {
|
||||
t.Fatalf("Failed to parse response: %v", err)
|
||||
}
|
||||
|
||||
if response.Supported {
|
||||
t.Error("Translation should not be supported for unknown formats")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheckTranslation_MissingParams(t *testing.T) {
|
||||
router := setupTestRouter()
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
url string
|
||||
}{
|
||||
{"missing both", "/v1/translations/check"},
|
||||
{"missing to", "/v1/translations/check?from=openai"},
|
||||
{"missing from", "/v1/translations/check?to=claude"},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
req, _ := http.NewRequest("GET", tt.url, nil)
|
||||
w := httptest.NewRecorder()
|
||||
router.ServeHTTP(w, req)
|
||||
|
||||
if w.Code != http.StatusBadRequest {
|
||||
t.Errorf("Expected status %d, got %d", http.StatusBadRequest, w.Code)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheckTranslation_SameFormat(t *testing.T) {
|
||||
router := setupTestRouter()
|
||||
|
||||
req, _ := http.NewRequest("GET", "/v1/translations/check?from=openai&to=openai", nil)
|
||||
w := httptest.NewRecorder()
|
||||
router.ServeHTTP(w, req)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Errorf("Expected status %d, got %d", http.StatusOK, w.Code)
|
||||
}
|
||||
|
||||
var response CheckTranslationResponse
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &response); err != nil {
|
||||
t.Fatalf("Failed to parse response: %v", err)
|
||||
}
|
||||
|
||||
// Same format translation is a fallback/no-op
|
||||
if !response.Fallback {
|
||||
t.Error("Same format should be marked as fallback")
|
||||
}
|
||||
if !response.Supported {
|
||||
t.Error("Same format should be supported")
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetTranslationDocs_Markdown(t *testing.T) {
|
||||
router := setupTestRouter()
|
||||
|
||||
req, _ := http.NewRequest("GET", "/v1/translations/docs", nil)
|
||||
w := httptest.NewRecorder()
|
||||
router.ServeHTTP(w, req)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Errorf("Expected status %d, got %d", http.StatusOK, w.Code)
|
||||
}
|
||||
|
||||
contentType := w.Header().Get("Content-Type")
|
||||
if contentType != "text/markdown; charset=utf-8" {
|
||||
t.Errorf("Content-Type = %s, want text/markdown; charset=utf-8", contentType)
|
||||
}
|
||||
|
||||
body := w.Body.String()
|
||||
if body == "" {
|
||||
t.Error("Response body should not be empty")
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetTranslationDocs_Mermaid(t *testing.T) {
|
||||
router := setupTestRouter()
|
||||
|
||||
req, _ := http.NewRequest("GET", "/v1/translations/docs?format=mermaid", nil)
|
||||
w := httptest.NewRecorder()
|
||||
router.ServeHTTP(w, req)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Errorf("Expected status %d, got %d", http.StatusOK, w.Code)
|
||||
}
|
||||
|
||||
contentType := w.Header().Get("Content-Type")
|
||||
if contentType != "text/plain; charset=utf-8" {
|
||||
t.Errorf("Content-Type = %s, want text/plain; charset=utf-8", contentType)
|
||||
}
|
||||
|
||||
body := w.Body.String()
|
||||
if !containsString(body, "mermaid") {
|
||||
t.Error("Response should contain mermaid diagram")
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetTranslationDocs_Summary(t *testing.T) {
|
||||
router := setupTestRouter()
|
||||
|
||||
req, _ := http.NewRequest("GET", "/v1/translations/docs?format=summary", nil)
|
||||
w := httptest.NewRecorder()
|
||||
router.ServeHTTP(w, req)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Errorf("Expected status %d, got %d", http.StatusOK, w.Code)
|
||||
}
|
||||
|
||||
contentType := w.Header().Get("Content-Type")
|
||||
if contentType != "text/plain; charset=utf-8" {
|
||||
t.Errorf("Content-Type = %s, want text/plain; charset=utf-8", contentType)
|
||||
}
|
||||
|
||||
body := w.Body.String()
|
||||
if !containsString(body, "Summary") {
|
||||
t.Error("Response should contain summary")
|
||||
}
|
||||
}
|
||||
|
||||
func TestScoreTranslation(t *testing.T) {
|
||||
router := setupTestRouter()
|
||||
|
||||
requestBody := ScoreTranslationRequest{
|
||||
From: "openai",
|
||||
To: "claude",
|
||||
Before: `{"model": "gpt-4", "messages": []}`,
|
||||
After: `{"model": "gpt-4", "messages": []}`,
|
||||
}
|
||||
body, _ := json.Marshal(requestBody)
|
||||
|
||||
req, _ := http.NewRequest("POST", "/v1/translations/score", bytes.NewBuffer(body))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
w := httptest.NewRecorder()
|
||||
router.ServeHTTP(w, req)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Errorf("Expected status %d, got %d", http.StatusOK, w.Code)
|
||||
}
|
||||
|
||||
var report translator.QualityReport
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &report); err != nil {
|
||||
t.Fatalf("Failed to parse response: %v", err)
|
||||
}
|
||||
|
||||
// Perfect match should have high score
|
||||
if report.Score < 0.9 {
|
||||
t.Errorf("Score = %v, expected >= 0.9 for identical payloads", report.Score)
|
||||
}
|
||||
}
|
||||
|
||||
func TestScoreTranslation_InvalidBody(t *testing.T) {
|
||||
router := setupTestRouter()
|
||||
|
||||
req, _ := http.NewRequest("POST", "/v1/translations/score", bytes.NewBufferString("invalid json"))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
w := httptest.NewRecorder()
|
||||
router.ServeHTTP(w, req)
|
||||
|
||||
if w.Code != http.StatusBadRequest {
|
||||
t.Errorf("Expected status %d, got %d", http.StatusBadRequest, w.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestScoreTranslation_MissingFields(t *testing.T) {
|
||||
router := setupTestRouter()
|
||||
|
||||
// Missing required fields
|
||||
requestBody := map[string]string{
|
||||
"from": "openai",
|
||||
// missing to, before, after
|
||||
}
|
||||
body, _ := json.Marshal(requestBody)
|
||||
|
||||
req, _ := http.NewRequest("POST", "/v1/translations/score", bytes.NewBuffer(body))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
w := httptest.NewRecorder()
|
||||
router.ServeHTTP(w, req)
|
||||
|
||||
if w.Code != http.StatusBadRequest {
|
||||
t.Errorf("Expected status %d, got %d", http.StatusBadRequest, w.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCompareStructures(t *testing.T) {
|
||||
router := setupTestRouter()
|
||||
|
||||
requestBody := CompareStructuresRequest{
|
||||
Before: `{"a": 1, "b": 2}`,
|
||||
After: `{"a": 1, "c": 3}`,
|
||||
}
|
||||
body, _ := json.Marshal(requestBody)
|
||||
|
||||
req, _ := http.NewRequest("POST", "/v1/translations/compare", bytes.NewBuffer(body))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
w := httptest.NewRecorder()
|
||||
router.ServeHTTP(w, req)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Errorf("Expected status %d, got %d", http.StatusOK, w.Code)
|
||||
}
|
||||
|
||||
var comparison translator.StructureComparison
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &comparison); err != nil {
|
||||
t.Fatalf("Failed to parse response: %v", err)
|
||||
}
|
||||
|
||||
if comparison.SourceType != "object" {
|
||||
t.Errorf("SourceType = %s, want object", comparison.SourceType)
|
||||
}
|
||||
if comparison.TargetType != "object" {
|
||||
t.Errorf("TargetType = %s, want object", comparison.TargetType)
|
||||
}
|
||||
if len(comparison.Diffs) == 0 {
|
||||
t.Error("Should detect differences between the structures")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCompareStructures_InvalidBody(t *testing.T) {
|
||||
router := setupTestRouter()
|
||||
|
||||
req, _ := http.NewRequest("POST", "/v1/translations/compare", bytes.NewBufferString("invalid json"))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
w := httptest.NewRecorder()
|
||||
router.ServeHTTP(w, req)
|
||||
|
||||
if w.Code != http.StatusBadRequest {
|
||||
t.Errorf("Expected status %d, got %d", http.StatusBadRequest, w.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCompareStructures_InvalidJSON(t *testing.T) {
|
||||
router := setupTestRouter()
|
||||
|
||||
requestBody := CompareStructuresRequest{
|
||||
Before: `{invalid}`,
|
||||
After: `{"valid": true}`,
|
||||
}
|
||||
body, _ := json.Marshal(requestBody)
|
||||
|
||||
req, _ := http.NewRequest("POST", "/v1/translations/compare", bytes.NewBuffer(body))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
w := httptest.NewRecorder()
|
||||
router.ServeHTTP(w, req)
|
||||
|
||||
if w.Code != http.StatusBadRequest {
|
||||
t.Errorf("Expected status %d, got %d for invalid JSON in payload", http.StatusBadRequest, w.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCompareStructures_IdenticalStructures(t *testing.T) {
|
||||
router := setupTestRouter()
|
||||
|
||||
requestBody := CompareStructuresRequest{
|
||||
Before: `{"a": 1, "b": 2}`,
|
||||
After: `{"a": 1, "b": 2}`,
|
||||
}
|
||||
body, _ := json.Marshal(requestBody)
|
||||
|
||||
req, _ := http.NewRequest("POST", "/v1/translations/compare", bytes.NewBuffer(body))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
w := httptest.NewRecorder()
|
||||
router.ServeHTTP(w, req)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Errorf("Expected status %d, got %d", http.StatusOK, w.Code)
|
||||
}
|
||||
|
||||
var comparison translator.StructureComparison
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &comparison); err != nil {
|
||||
t.Fatalf("Failed to parse response: %v", err)
|
||||
}
|
||||
|
||||
if len(comparison.Diffs) != 0 {
|
||||
t.Errorf("Identical structures should have no diffs, got %d", len(comparison.Diffs))
|
||||
}
|
||||
}
|
||||
|
||||
// Helper function
|
||||
func containsString(s, substr string) bool {
|
||||
for i := 0; i <= len(s)-len(substr); i++ {
|
||||
if s[i:i+len(substr)] == substr {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
@@ -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")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -18,6 +18,7 @@ import (
|
||||
"github.com/router-for-me/CLIProxyAPI/v6/internal/embeddings"
|
||||
"github.com/router-for-me/CLIProxyAPI/v6/internal/memory"
|
||||
"github.com/router-for-me/CLIProxyAPI/v6/internal/registry"
|
||||
"github.com/router-for-me/CLIProxyAPI/v6/internal/runtime/executor"
|
||||
"github.com/router-for-me/CLIProxyAPI/v6/internal/util"
|
||||
"github.com/tidwall/gjson"
|
||||
"github.com/tidwall/sjson"
|
||||
@@ -134,10 +135,56 @@ func agenticMemoryStore() memory.Store {
|
||||
}
|
||||
}
|
||||
memStore = memory.NewFileStore(base)
|
||||
|
||||
// Initialize summarizer with default config (uses regex fallback until executor is set)
|
||||
if agenticLLMSummaryEnabled() {
|
||||
if fs, ok := memStore.(*memory.FileStore); ok {
|
||||
config := memory.DefaultSummarizerConfig()
|
||||
// Start with NoOp executor - will fall back to regex
|
||||
// Call SetSummarizerExecutor() later with a real executor to enable LLM summarization
|
||||
summarizer := memory.NewSummarizer(config, memory.NewNoOpSummarizerExecutor())
|
||||
fs.SetSummarizer(summarizer)
|
||||
}
|
||||
}
|
||||
})
|
||||
return memStore
|
||||
}
|
||||
|
||||
// SetSummarizerExecutor configures the LLM executor for context summarization.
|
||||
// Call this from server initialization with the auth manager to enable full LLM summarization.
|
||||
// Example: middleware.SetSummarizerExecutor(memory.NewPipelineSummarizerExecutor(authManager, providers))
|
||||
func SetSummarizerExecutor(executor memory.SummarizerExecutor) {
|
||||
store := agenticMemoryStore()
|
||||
if store == nil {
|
||||
return
|
||||
}
|
||||
fs, ok := store.(*memory.FileStore)
|
||||
if !ok || fs == nil {
|
||||
return
|
||||
}
|
||||
summarizer := fs.GetSummarizer()
|
||||
if summarizer == nil {
|
||||
config := memory.DefaultSummarizerConfig()
|
||||
summarizer = memory.NewSummarizer(config, executor)
|
||||
fs.SetSummarizer(summarizer)
|
||||
} else {
|
||||
// Replace the existing summarizer with one that has the real executor
|
||||
config := memory.DefaultSummarizerConfig()
|
||||
fs.SetSummarizer(memory.NewSummarizer(config, executor))
|
||||
}
|
||||
}
|
||||
|
||||
// InitSummarizerWithAuthManager configures LLM-based summarization using the core auth manager.
|
||||
// Call this after the auth manager is initialized to enable Factory.ai-style context compression.
|
||||
func InitSummarizerWithAuthManager(manager memory.CoreManagerExecutor, providers []string) {
|
||||
if manager == nil {
|
||||
return
|
||||
}
|
||||
adapter := memory.NewManagerAuthAdapter(manager)
|
||||
executor := memory.NewPipelineSummarizerExecutor(adapter, providers)
|
||||
SetSummarizerExecutor(executor)
|
||||
}
|
||||
|
||||
func agenticSemanticEnabled() bool {
|
||||
if v := strings.TrimSpace(os.Getenv("CLIPROXY_SEMANTIC_ENABLED")); v != "" {
|
||||
if strings.EqualFold(v, "0") || strings.EqualFold(v, "false") || strings.EqualFold(v, "off") || strings.EqualFold(v, "no") {
|
||||
@@ -400,6 +447,176 @@ func agenticAnchorSummaryMaxChars() int {
|
||||
return 14_000
|
||||
}
|
||||
|
||||
// agenticCompressionThreshold returns the context usage ratio at which to trigger LLM compression.
|
||||
// Default: 0.75 (75% of context window)
|
||||
func agenticCompressionThreshold() float64 {
|
||||
if v := strings.TrimSpace(os.Getenv("CLIPROXY_COMPRESSION_THRESHOLD")); v != "" {
|
||||
if f, err := strconv.ParseFloat(v, 64); err == nil && f > 0 && f < 1 {
|
||||
return f
|
||||
}
|
||||
}
|
||||
return 0.75
|
||||
}
|
||||
|
||||
// agenticLLMSummaryEnabled returns whether LLM-based summarization is enabled.
|
||||
// Default: true
|
||||
func agenticLLMSummaryEnabled() bool {
|
||||
if v := strings.TrimSpace(os.Getenv("CLIPROXY_LLM_SUMMARY_ENABLED")); v != "" {
|
||||
return strings.EqualFold(v, "1") || strings.EqualFold(v, "true") || strings.EqualFold(v, "on")
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// getModelContextWindow returns the context window size for a model.
|
||||
func getModelContextWindow(model string) int {
|
||||
// Try registry first
|
||||
info := registry.GetGlobalRegistry().GetModelInfo(model)
|
||||
if info != nil && info.ContextLength > 0 {
|
||||
return info.ContextLength
|
||||
}
|
||||
// Fallback based on model name patterns
|
||||
lowerModel := strings.ToLower(model)
|
||||
switch {
|
||||
case strings.Contains(lowerModel, "claude-3.5"), strings.Contains(lowerModel, "claude-3-5"):
|
||||
return 200000
|
||||
case strings.Contains(lowerModel, "claude-3"):
|
||||
return 200000
|
||||
case strings.Contains(lowerModel, "claude"):
|
||||
return 100000
|
||||
case strings.Contains(lowerModel, "gpt-4-turbo"), strings.Contains(lowerModel, "gpt-4o"):
|
||||
return 128000
|
||||
case strings.Contains(lowerModel, "gpt-4"):
|
||||
return 8192
|
||||
case strings.Contains(lowerModel, "gpt-3.5"):
|
||||
return 16384
|
||||
case strings.Contains(lowerModel, "gemini"):
|
||||
return 1000000
|
||||
case strings.Contains(lowerModel, "o1"), strings.Contains(lowerModel, "o3"):
|
||||
return 200000
|
||||
default:
|
||||
return 100000
|
||||
}
|
||||
}
|
||||
|
||||
// agenticTokenAwareEnabled returns whether token-aware compression is enabled.
|
||||
// Default: true
|
||||
func agenticTokenAwareEnabled() bool {
|
||||
if v := strings.TrimSpace(os.Getenv("CLIPROXY_TOKEN_AWARE_ENABLED")); v != "" {
|
||||
return strings.EqualFold(v, "1") || strings.EqualFold(v, "true") || strings.EqualFold(v, "on")
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// agenticReserveTokens returns the number of tokens to reserve for model output.
|
||||
// Default: 8192 (reasonable output buffer)
|
||||
func agenticReserveTokens() int {
|
||||
if v := strings.TrimSpace(os.Getenv("CLIPROXY_RESERVE_TOKENS")); v != "" {
|
||||
if n, err := strconv.Atoi(v); err == nil && n > 0 {
|
||||
return n
|
||||
}
|
||||
}
|
||||
return 8192
|
||||
}
|
||||
|
||||
// tokenAwareCompressionResult contains the result of token-aware analysis
|
||||
type tokenAwareCompressionResult struct {
|
||||
ShouldTrim bool // Whether trimming is needed
|
||||
CurrentTokens int64 // Estimated current token count
|
||||
ContextWindow int // Model's context window
|
||||
TargetTokens int64 // Target token count after trimming
|
||||
TargetMaxBytes int // Approximate bytes to achieve target tokens
|
||||
Model string // The model name
|
||||
}
|
||||
|
||||
// analyzeTokenBudget checks if the request exceeds the token threshold and calculates trimming targets.
|
||||
// Returns whether trimming is needed and the target byte limit.
|
||||
func analyzeTokenBudget(body []byte) *tokenAwareCompressionResult {
|
||||
result := &tokenAwareCompressionResult{
|
||||
ShouldTrim: false,
|
||||
TargetMaxBytes: agenticMaxBodyBytes(), // Default fallback
|
||||
}
|
||||
|
||||
if !agenticTokenAwareEnabled() {
|
||||
return result
|
||||
}
|
||||
|
||||
model := gjson.GetBytes(body, "model").String()
|
||||
if model == "" {
|
||||
return result
|
||||
}
|
||||
result.Model = model
|
||||
|
||||
// Get context window for this model
|
||||
contextWindow := getModelContextWindow(model)
|
||||
result.ContextWindow = contextWindow
|
||||
|
||||
// Estimate current token count
|
||||
currentTokens, err := executor.EstimateRequestTokens(model, body)
|
||||
if err != nil {
|
||||
// Fallback to byte-based heuristic: ~4 chars per token
|
||||
currentTokens = int64(len(body) / 4)
|
||||
}
|
||||
result.CurrentTokens = currentTokens
|
||||
|
||||
// Reserve tokens for output
|
||||
reserveTokens := agenticReserveTokens()
|
||||
availableContext := contextWindow - reserveTokens
|
||||
if availableContext < 1000 {
|
||||
availableContext = contextWindow / 2 // At least half for input
|
||||
}
|
||||
|
||||
// Check against threshold
|
||||
threshold := agenticCompressionThreshold()
|
||||
maxInputTokens := int64(float64(availableContext) * threshold)
|
||||
|
||||
if currentTokens <= maxInputTokens {
|
||||
// Under threshold, no trimming needed
|
||||
result.ShouldTrim = false
|
||||
return result
|
||||
}
|
||||
|
||||
// Calculate target tokens (aim for 70% of threshold to give buffer)
|
||||
targetRatio := threshold * 0.9 // Slightly under threshold
|
||||
targetTokens := int64(float64(availableContext) * targetRatio)
|
||||
result.TargetTokens = targetTokens
|
||||
result.ShouldTrim = true
|
||||
|
||||
// Convert target tokens to approximate byte limit
|
||||
// Use ~4 bytes per token as heuristic, but be conservative
|
||||
targetBytes := int(targetTokens * 4)
|
||||
|
||||
// Clamp to reasonable bounds
|
||||
minBytes := 32 * 1024 // 32KB minimum
|
||||
if targetBytes < minBytes {
|
||||
targetBytes = minBytes
|
||||
}
|
||||
|
||||
// Don't exceed the configured max
|
||||
maxBytes := agenticMaxBodyBytes()
|
||||
if targetBytes > maxBytes {
|
||||
targetBytes = maxBytes
|
||||
}
|
||||
|
||||
result.TargetMaxBytes = targetBytes
|
||||
return result
|
||||
}
|
||||
|
||||
// getTokenAwareMaxBytes returns the maximum body size based on token analysis.
|
||||
// This is more accurate than the byte-based heuristic in agenticMaxBodyBytesForModel.
|
||||
func getTokenAwareMaxBytes(body []byte) int {
|
||||
if !agenticTokenAwareEnabled() {
|
||||
return agenticMaxBodyBytesForModel(body)
|
||||
}
|
||||
|
||||
result := analyzeTokenBudget(body)
|
||||
if result.ShouldTrim {
|
||||
return result.TargetMaxBytes
|
||||
}
|
||||
|
||||
// Not over threshold, but still apply model-specific limits
|
||||
return agenticMaxBodyBytesForModel(body)
|
||||
}
|
||||
|
||||
func allowMemoryWrite(session string) bool {
|
||||
limit := agenticMemoryMaxWritesPerMin()
|
||||
if limit <= 0 || session == "" {
|
||||
@@ -523,7 +740,31 @@ func CodexPromptBudgetMiddlewareWithRootDir(rootDir string) gin.HandlerFunc {
|
||||
}
|
||||
|
||||
originalLen := len(body)
|
||||
maxBytes := agenticMaxBodyBytesForModel(body)
|
||||
|
||||
// Token-aware compression: analyze token budget before byte-based check
|
||||
tokenAnalysis := analyzeTokenBudget(body)
|
||||
maxBytes := tokenAnalysis.TargetMaxBytes
|
||||
|
||||
// If token analysis didn't trigger, fall back to byte-based model limit
|
||||
if !tokenAnalysis.ShouldTrim {
|
||||
maxBytes = agenticMaxBodyBytesForModel(body)
|
||||
}
|
||||
|
||||
// Add diagnostic headers for localhost debugging
|
||||
if c != nil {
|
||||
ip := c.ClientIP()
|
||||
if ip == "127.0.0.1" || ip == "::1" {
|
||||
if tokenAnalysis.Model != "" {
|
||||
c.Header("X-ProxyPilot-Model", tokenAnalysis.Model)
|
||||
c.Header("X-ProxyPilot-Context-Window", strconv.Itoa(tokenAnalysis.ContextWindow))
|
||||
c.Header("X-ProxyPilot-Current-Tokens", strconv.FormatInt(tokenAnalysis.CurrentTokens, 10))
|
||||
if tokenAnalysis.ShouldTrim {
|
||||
c.Header("X-ProxyPilot-Token-Triggered", "true")
|
||||
c.Header("X-ProxyPilot-Target-Tokens", strconv.FormatInt(tokenAnalysis.TargetTokens, 10))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Session-scoped state (pinned + anchor + TODO + spec) is injected as append-only
|
||||
// scaffolding when enabled. This preserves prompt-cache friendliness.
|
||||
@@ -533,7 +774,9 @@ func CodexPromptBudgetMiddlewareWithRootDir(rootDir string) gin.HandlerFunc {
|
||||
originalLen = len(body)
|
||||
}
|
||||
|
||||
if originalLen <= maxBytes {
|
||||
// Proactive compression: trim if over token threshold OR over byte limit
|
||||
needsTrim := tokenAnalysis.ShouldTrim || originalLen > maxBytes
|
||||
if !needsTrim {
|
||||
req.Body = io.NopCloser(bytes.NewReader(body))
|
||||
req.ContentLength = int64(originalLen)
|
||||
req.Header.Set("Content-Length", strconv.Itoa(originalLen))
|
||||
@@ -1349,7 +1592,13 @@ func agenticStoreAndInjectMemory(c *gin.Context, req *http.Request, session stri
|
||||
_ = fs.WritePinned(session, pinned, 8000)
|
||||
}
|
||||
if len(res.Dropped) > 0 {
|
||||
_ = agenticUpdateAnchoredSummary(fs, session, res.Dropped, pinned, res.Query)
|
||||
if agenticLLMSummaryEnabled() {
|
||||
model := gjson.GetBytes(res.Body, "model").String()
|
||||
ctx := c.Request.Context()
|
||||
_ = agenticUpdateAnchoredSummaryWithLLM(ctx, model, fs, session, res.Dropped, pinned, res.Query)
|
||||
} else {
|
||||
_ = agenticUpdateAnchoredSummary(fs, session, res.Dropped, pinned, res.Query)
|
||||
}
|
||||
}
|
||||
if agenticSemanticEnabled() && len(res.Dropped) > 0 && !fs.IsSemanticDisabled(session) {
|
||||
ns := semanticNamespace(req, res.Body, session)
|
||||
@@ -1410,6 +1659,28 @@ func agenticUpdateAnchoredSummary(fs *memory.FileStore, session string, dropped
|
||||
return fs.WriteSummary(session, next, agenticAnchorSummaryMaxChars())
|
||||
}
|
||||
|
||||
// agenticUpdateAnchoredSummaryWithLLM updates the anchored summary using LLM.
|
||||
func agenticUpdateAnchoredSummaryWithLLM(ctx context.Context, model string, fs *memory.FileStore, session string, dropped []memory.Event, pinned string, latestIntent string) error {
|
||||
if fs == nil || session == "" {
|
||||
return nil
|
||||
}
|
||||
summarizer := fs.GetSummarizer()
|
||||
if summarizer == nil {
|
||||
// Fall back to regex-based
|
||||
return agenticUpdateAnchoredSummary(fs, session, dropped, pinned, latestIntent)
|
||||
}
|
||||
|
||||
prev := fs.ReadSummary(session, agenticAnchorSummaryMaxChars())
|
||||
next := memory.BuildAnchoredSummaryWithLLM(ctx, model, prev, dropped, latestIntent, summarizer)
|
||||
if strings.TrimSpace(next) == "" {
|
||||
return nil
|
||||
}
|
||||
if agenticAnchorAppendOnly() {
|
||||
return fs.SetAnchorSummary(session, next, agenticAnchorSummaryMaxChars())
|
||||
}
|
||||
return fs.WriteSummary(session, next, agenticAnchorSummaryMaxChars())
|
||||
}
|
||||
|
||||
func agenticMaybePruneMemory() {
|
||||
maxAge := agenticMemoryMaxAgeDays()
|
||||
maxSessions := agenticMemoryMaxSessions()
|
||||
|
||||
@@ -8,12 +8,41 @@ import (
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/router-for-me/CLIProxyAPI/v6/internal/interfaces"
|
||||
"github.com/router-for-me/CLIProxyAPI/v6/internal/logging"
|
||||
"github.com/router-for-me/CLIProxyAPI/v6/internal/util"
|
||||
)
|
||||
|
||||
var (
|
||||
requestMonitorMu sync.RWMutex
|
||||
requestMonitor []interfaces.RequestLogEntry
|
||||
maxMonitorSize = 100
|
||||
)
|
||||
|
||||
func addRequestToMonitor(entry interfaces.RequestLogEntry) {
|
||||
requestMonitorMu.Lock()
|
||||
defer requestMonitorMu.Unlock()
|
||||
|
||||
requestMonitor = append(requestMonitor, entry)
|
||||
if len(requestMonitor) > maxMonitorSize {
|
||||
requestMonitor = requestMonitor[1:]
|
||||
}
|
||||
}
|
||||
|
||||
// GetRequestMonitor returns a copy of the current request monitor entries.
|
||||
func GetRequestMonitor() []interfaces.RequestLogEntry {
|
||||
requestMonitorMu.RLock()
|
||||
defer requestMonitorMu.RUnlock()
|
||||
|
||||
result := make([]interfaces.RequestLogEntry, len(requestMonitor))
|
||||
copy(result, requestMonitor)
|
||||
return result
|
||||
}
|
||||
|
||||
// RequestLoggingMiddleware creates a Gin middleware that logs HTTP requests and responses.
|
||||
// It captures detailed information about the request and response, including headers and body,
|
||||
// and uses the provided RequestLogger to record this data. When logging is disabled in the
|
||||
@@ -53,13 +82,68 @@ func RequestLoggingMiddleware(logger logging.RequestLogger) gin.HandlerFunc {
|
||||
c.Writer = wrapper
|
||||
|
||||
// Process the request
|
||||
startTime := time.Now()
|
||||
c.Next()
|
||||
latency := time.Since(startTime)
|
||||
|
||||
// Finalize logging after request processing
|
||||
if err = wrapper.Finalize(c); err != nil {
|
||||
// Log error but don't interrupt the response
|
||||
// In a real implementation, you might want to use a proper logger here
|
||||
}
|
||||
|
||||
// Add to monitor
|
||||
entry := interfaces.RequestLogEntry{
|
||||
ID: logging.GetGinRequestID(c),
|
||||
Timestamp: startTime,
|
||||
Method: c.Request.Method,
|
||||
Path: c.Request.URL.Path,
|
||||
Status: c.Writer.Status(),
|
||||
LatencyMs: latency.Milliseconds(),
|
||||
}
|
||||
|
||||
// Try to get Model and Provider from context
|
||||
if model, ok := c.Get("model"); ok {
|
||||
if m, ok := model.(string); ok {
|
||||
entry.Model = m
|
||||
}
|
||||
}
|
||||
if provider, ok := c.Get("provider"); ok {
|
||||
if p, ok := provider.(string); ok {
|
||||
entry.Provider = p
|
||||
}
|
||||
}
|
||||
|
||||
// Try to get tokens from context
|
||||
if inputTokens, ok := c.Get("input_tokens"); ok {
|
||||
if it, ok := inputTokens.(int); ok {
|
||||
entry.InputTokens = it
|
||||
} else if it, ok := inputTokens.(int64); ok {
|
||||
entry.InputTokens = int(it)
|
||||
}
|
||||
}
|
||||
if outputTokens, ok := c.Get("output_tokens"); ok {
|
||||
if ot, ok := outputTokens.(int); ok {
|
||||
entry.OutputTokens = ot
|
||||
} else if ot, ok := outputTokens.(int64); ok {
|
||||
entry.OutputTokens = int(ot)
|
||||
}
|
||||
}
|
||||
|
||||
// Try to get error from context
|
||||
if apiErr, ok := c.Get("API_RESPONSE_ERROR"); ok {
|
||||
if errs, ok := apiErr.([]*interfaces.ErrorMessage); ok && len(errs) > 0 {
|
||||
var errMsgs []string
|
||||
for _, e := range errs {
|
||||
if e != nil && e.Error != nil {
|
||||
errMsgs = append(errMsgs, e.Error.Error())
|
||||
}
|
||||
}
|
||||
entry.Error = strings.Join(errMsgs, "; ")
|
||||
}
|
||||
}
|
||||
|
||||
addRequestToMonitor(entry)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -23,6 +23,7 @@ func (s *Server) registerProxyPilotDashboardRoutes() {
|
||||
s.engine.GET("/proxypilot.html", s.serveProxyPilotDashboard)
|
||||
s.engine.GET("/assets/*filepath", s.serveProxyPilotAsset)
|
||||
s.engine.GET("/vite.svg", s.serveProxyPilotViteIcon)
|
||||
s.engine.GET("/logo.png", s.serveProxyPilotLogo)
|
||||
}
|
||||
|
||||
func (s *Server) serveProxyPilotDashboard(c *gin.Context) {
|
||||
@@ -37,7 +38,12 @@ func (s *Server) serveProxyPilotDashboard(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
html := string(index)
|
||||
key := strings.TrimSpace(getManagementKey())
|
||||
// Use localPassword from the server instance (set via WithLocalManagementPassword)
|
||||
// Fall back to MANAGEMENT_PASSWORD env var for legacy subprocess mode
|
||||
key := strings.TrimSpace(s.localPassword)
|
||||
if key == "" {
|
||||
key = strings.TrimSpace(os.Getenv("MANAGEMENT_PASSWORD"))
|
||||
}
|
||||
if key != "" && s.managementRoutesEnabled.Load() {
|
||||
meta := `<meta name="pp-mgmt-key" content="` + escapeAttr(key) + `">`
|
||||
html = strings.Replace(html, "</head>", meta+"</head>", 1)
|
||||
@@ -79,6 +85,19 @@ func (s *Server) serveProxyPilotViteIcon(c *gin.Context) {
|
||||
writeAssetResponse(c, "vite.svg", data)
|
||||
}
|
||||
|
||||
func (s *Server) serveProxyPilotLogo(c *gin.Context) {
|
||||
if !isLocalClient(c) {
|
||||
c.AbortWithStatus(http.StatusForbidden)
|
||||
return
|
||||
}
|
||||
data, err := fsReadFile("logo.png")
|
||||
if err != nil {
|
||||
c.AbortWithStatus(http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
writeAssetResponse(c, "logo.png", data)
|
||||
}
|
||||
|
||||
func writeAssetResponse(c *gin.Context, name string, data []byte) {
|
||||
ext := strings.TrimPrefix(path.Ext(name), ".")
|
||||
if mt := misc.MimeTypes[strings.ToLower(ext)]; mt != "" {
|
||||
@@ -105,10 +124,6 @@ func isLocalClient(c *gin.Context) bool {
|
||||
return clientIP == "127.0.0.1" || clientIP == "::1"
|
||||
}
|
||||
|
||||
func getManagementKey() string {
|
||||
return strings.TrimSpace(os.Getenv("MANAGEMENT_PASSWORD"))
|
||||
}
|
||||
|
||||
func escapeAttr(s string) string {
|
||||
return strings.ReplaceAll(strings.TrimSpace(s), `"`, "")
|
||||
}
|
||||
|
||||
+1089
-1010
File diff suppressed because it is too large
Load Diff
@@ -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",
|
||||
|
||||
@@ -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",
|
||||
|
||||
@@ -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()
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
@@ -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
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -2,11 +2,11 @@ package desktopctl
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/router-for-me/CLIProxyAPI/v6/internal/config"
|
||||
"github.com/router-for-me/CLIProxyAPI/v6/internal/util"
|
||||
)
|
||||
|
||||
func resolveConfigPath(repoRoot, configPath string) (string, error) {
|
||||
@@ -45,21 +45,9 @@ func authDir(configPath string) (string, error) {
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
dir := strings.TrimSpace(cfg.AuthDir)
|
||||
if dir == "" {
|
||||
return "", fmt.Errorf("auth-dir not configured")
|
||||
}
|
||||
if strings.HasPrefix(dir, "~/") || dir == "~" {
|
||||
home, _ := os.UserHomeDir()
|
||||
if home != "" {
|
||||
if dir == "~" {
|
||||
dir = home
|
||||
} else {
|
||||
dir = filepath.Join(home, strings.TrimPrefix(dir, "~/"))
|
||||
}
|
||||
}
|
||||
}
|
||||
return dir, nil
|
||||
// ResolveAuthDir handles empty string by returning DefaultAuthDir()
|
||||
// and also handles tilde expansion.
|
||||
return util.ResolveAuthDir(cfg.AuthDir)
|
||||
}
|
||||
|
||||
// AuthDirFor returns the resolved auth directory for the given config file.
|
||||
|
||||
@@ -11,10 +11,36 @@ import (
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
"sync"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"github.com/router-for-me/CLIProxyAPI/v6/internal/buildinfo"
|
||||
"github.com/router-for-me/CLIProxyAPI/v6/internal/embedded"
|
||||
)
|
||||
|
||||
var (
|
||||
embeddedMode bool
|
||||
embeddedMu sync.RWMutex
|
||||
embeddedConfig string
|
||||
)
|
||||
|
||||
// SetEmbeddedMode enables or disables embedded server mode.
|
||||
// When enabled, Start/Stop/Restart will use the in-process server
|
||||
// instead of spawning a separate engine process.
|
||||
func SetEmbeddedMode(enabled bool) {
|
||||
embeddedMu.Lock()
|
||||
defer embeddedMu.Unlock()
|
||||
embeddedMode = enabled
|
||||
}
|
||||
|
||||
// IsEmbeddedMode returns whether embedded mode is enabled.
|
||||
func IsEmbeddedMode() bool {
|
||||
embeddedMu.RLock()
|
||||
defer embeddedMu.RUnlock()
|
||||
return embeddedMode
|
||||
}
|
||||
|
||||
func StatusFor(configPath string) (Status, error) {
|
||||
statePath := defaultStatePath()
|
||||
s, _ := loadState(statePath)
|
||||
@@ -36,27 +62,32 @@ func StatusFor(configPath string) (Status, error) {
|
||||
healthErr := checkHealth(baseURL)
|
||||
|
||||
running := healthErr == nil
|
||||
managed := s != nil && s.PID > 0
|
||||
if managed {
|
||||
|
||||
// Check if we're in embedded mode
|
||||
isEmbedded := IsEmbeddedMode() || embedded.GlobalIsRunning()
|
||||
|
||||
managed := s != nil && (s.PID > 0 || isEmbedded)
|
||||
if managed && !isEmbedded {
|
||||
if alive, _ := isProcessAlive(s.PID); !alive {
|
||||
managed = false
|
||||
}
|
||||
}
|
||||
if s != nil {
|
||||
if s != nil && !isEmbedded {
|
||||
repairStateIfStale(statePath, s, running, managed)
|
||||
}
|
||||
|
||||
out := Status{
|
||||
Running: running,
|
||||
Managed: managed,
|
||||
AutoStartProxy: s != nil && s.AutoStartProxy,
|
||||
PID: pidOrZero(s),
|
||||
Port: port,
|
||||
ThinkingPort: 8317,
|
||||
BaseURL: baseURL,
|
||||
ConfigPath: resolvedConfig,
|
||||
ExePath: exeOrEmpty(s),
|
||||
StartedAt: startedAtOrZero(s),
|
||||
Running: running,
|
||||
Version: buildinfo.Version,
|
||||
Managed: managed,
|
||||
AutoStartProxy: s != nil && s.AutoStartProxy,
|
||||
PID: pidOrZero(s),
|
||||
Port: port,
|
||||
ThinkingPort: 8317,
|
||||
BaseURL: baseURL,
|
||||
ConfigPath: resolvedConfig,
|
||||
ExePath: exeOrEmpty(s),
|
||||
StartedAt: startedAtOrZero(s),
|
||||
}
|
||||
if inUse, _ := isLocalPortInUse(8317); inUse {
|
||||
out.ThinkingRunning = true
|
||||
@@ -68,6 +99,71 @@ func StatusFor(configPath string) (Status, error) {
|
||||
}
|
||||
|
||||
func Start(opts StartOptions) (Status, error) {
|
||||
// Use embedded mode if enabled or explicitly requested
|
||||
if opts.Embedded || IsEmbeddedMode() {
|
||||
return startEmbedded(opts)
|
||||
}
|
||||
return startSubprocess(opts)
|
||||
}
|
||||
|
||||
// startEmbedded starts the proxy server in-process using the embedded package.
|
||||
func startEmbedded(opts StartOptions) (Status, error) {
|
||||
statePath := defaultStatePath()
|
||||
prev, _ := loadState(statePath)
|
||||
configPath, err := resolveConfigPath(opts.RepoRoot, opts.ConfigPath)
|
||||
if err != nil {
|
||||
return Status{}, err
|
||||
}
|
||||
if _, err := os.Stat(configPath); err != nil {
|
||||
return Status{}, fmt.Errorf("config not found: %s", configPath)
|
||||
}
|
||||
|
||||
port, err := loadPort(configPath)
|
||||
if err != nil {
|
||||
return Status{}, err
|
||||
}
|
||||
|
||||
if inUse, _ := isLocalPortInUse(port); inUse {
|
||||
return StatusFor(configPath)
|
||||
}
|
||||
|
||||
pw, errPw := getOrCreateManagementPassword()
|
||||
if errPw != nil {
|
||||
return Status{}, errPw
|
||||
}
|
||||
|
||||
// Set management password env var for the embedded server
|
||||
os.Setenv("MANAGEMENT_PASSWORD", pw)
|
||||
|
||||
// Start embedded server
|
||||
if err := embedded.StartGlobal(configPath, pw); err != nil {
|
||||
return Status{}, err
|
||||
}
|
||||
|
||||
// Store config path for embedded mode
|
||||
embeddedMu.Lock()
|
||||
embeddedConfig = configPath
|
||||
embeddedMu.Unlock()
|
||||
|
||||
// Save state (with PID 0 to indicate embedded mode)
|
||||
s := &state{
|
||||
PID: 0, // 0 indicates embedded mode
|
||||
ConfigPath: configPath,
|
||||
ExePath: "", // No separate exe in embedded mode
|
||||
StartedAt: embedded.GlobalServer().StartedAt(),
|
||||
}
|
||||
if prev != nil {
|
||||
s.AutoStartProxy = prev.AutoStartProxy
|
||||
s.OAuthPrivate = prev.OAuthPrivate
|
||||
s.ManagementPassword = prev.ManagementPassword
|
||||
}
|
||||
_ = saveState(statePath, s)
|
||||
|
||||
return StatusFor(configPath)
|
||||
}
|
||||
|
||||
// startSubprocess starts the proxy server as a separate process (legacy mode).
|
||||
func startSubprocess(opts StartOptions) (Status, error) {
|
||||
statePath := defaultStatePath()
|
||||
prev, _ := loadState(statePath)
|
||||
configPath, err := resolveConfigPath(opts.RepoRoot, opts.ConfigPath)
|
||||
@@ -175,6 +271,27 @@ func Stop(opts StopOptions) error {
|
||||
if pid == 0 && s != nil {
|
||||
pid = s.PID
|
||||
}
|
||||
|
||||
// Check if embedded server is running
|
||||
if IsEmbeddedMode() || embedded.GlobalIsRunning() {
|
||||
if err := embedded.StopGlobal(); err != nil {
|
||||
return err
|
||||
}
|
||||
// Clear embedded config
|
||||
embeddedMu.Lock()
|
||||
embeddedConfig = ""
|
||||
embeddedMu.Unlock()
|
||||
// Update state
|
||||
if s == nil {
|
||||
s = &state{}
|
||||
}
|
||||
s.PID = 0
|
||||
s.StartedAt = time.Time{}
|
||||
_ = saveState(statePath, s)
|
||||
return nil
|
||||
}
|
||||
|
||||
// Legacy subprocess mode
|
||||
if pid <= 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import "time"
|
||||
|
||||
type Status struct {
|
||||
Running bool `json:"running"`
|
||||
Version string `json:"version,omitempty"`
|
||||
Managed bool `json:"managed"`
|
||||
AutoStartProxy bool `json:"auto_start_proxy,omitempty"`
|
||||
PID int `json:"pid,omitempty"`
|
||||
@@ -22,6 +23,7 @@ type StartOptions struct {
|
||||
ConfigPath string
|
||||
ExePath string
|
||||
LogDir string
|
||||
Embedded bool // If true, run server in-process instead of spawning subprocess
|
||||
}
|
||||
|
||||
type StopOptions struct {
|
||||
|
||||
@@ -0,0 +1,224 @@
|
||||
// Package embedded provides an in-process proxy server that can be embedded
|
||||
// directly into the tray application, eliminating the need for a separate engine binary.
|
||||
package embedded
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/joho/godotenv"
|
||||
"github.com/router-for-me/CLIProxyAPI/v6/internal/api"
|
||||
"github.com/router-for-me/CLIProxyAPI/v6/internal/config"
|
||||
"github.com/router-for-me/CLIProxyAPI/v6/internal/logging"
|
||||
"github.com/router-for-me/CLIProxyAPI/v6/internal/util"
|
||||
configaccess "github.com/router-for-me/CLIProxyAPI/v6/sdk/access/config"
|
||||
sdkAuth "github.com/router-for-me/CLIProxyAPI/v6/sdk/auth"
|
||||
"github.com/router-for-me/CLIProxyAPI/v6/sdk/cliproxy"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
// Server represents an embedded proxy server instance.
|
||||
type Server struct {
|
||||
mu sync.Mutex
|
||||
running bool
|
||||
cancel context.CancelFunc
|
||||
done chan struct{}
|
||||
configPath string
|
||||
password string
|
||||
port int
|
||||
startedAt time.Time
|
||||
}
|
||||
|
||||
// NewServer creates a new embedded server instance.
|
||||
func NewServer() *Server {
|
||||
return &Server{}
|
||||
}
|
||||
|
||||
// Start starts the embedded proxy server with the given configuration.
|
||||
func (s *Server) Start(configPath, password string) error {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
|
||||
if s.running {
|
||||
return errors.New("server is already running")
|
||||
}
|
||||
|
||||
// Resolve config path
|
||||
resolvedPath := strings.TrimSpace(configPath)
|
||||
if resolvedPath == "" {
|
||||
return errors.New("config path is required")
|
||||
}
|
||||
|
||||
// Load .env if present
|
||||
if wd, err := os.Getwd(); err == nil {
|
||||
_ = godotenv.Load(filepath.Join(wd, ".env"))
|
||||
}
|
||||
|
||||
// Load configuration
|
||||
cfg, err := config.LoadConfig(resolvedPath)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to load config: %w", err)
|
||||
}
|
||||
|
||||
// Resolve auth-dir to apply default if empty
|
||||
if resolvedAuthDir, errResolve := util.ResolveAuthDir(cfg.AuthDir); errResolve == nil {
|
||||
cfg.AuthDir = resolvedAuthDir
|
||||
}
|
||||
|
||||
s.configPath = resolvedPath
|
||||
s.password = password
|
||||
s.port = cfg.Port
|
||||
|
||||
// Setup logging with config-driven output
|
||||
logging.SetupBaseLogger()
|
||||
logging.ConfigureLogOutput(cfg.LoggingToFile, cfg.LogsMaxTotalSizeMB)
|
||||
|
||||
// Create context with cancel
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
s.cancel = cancel
|
||||
s.done = make(chan struct{})
|
||||
|
||||
// Build service
|
||||
builder := cliproxy.NewBuilder().
|
||||
WithConfig(cfg).
|
||||
WithConfigPath(resolvedPath).
|
||||
WithLocalManagementPassword(password)
|
||||
|
||||
// Add keep-alive endpoint if password is set
|
||||
if password != "" {
|
||||
builder = builder.WithServerOptions(api.WithKeepAliveEndpoint(10*time.Second, func() {
|
||||
log.Warn("keep-alive endpoint idle for 10s (embedded mode - ignoring)")
|
||||
// In embedded mode, we don't auto-shutdown on keep-alive timeout
|
||||
}))
|
||||
}
|
||||
|
||||
// Register SDK providers
|
||||
sdkAuth.RegisterTokenStore(sdkAuth.NewFileTokenStore())
|
||||
configaccess.Register()
|
||||
|
||||
service, err := builder.Build()
|
||||
if err != nil {
|
||||
cancel()
|
||||
return fmt.Errorf("failed to build service: %w", err)
|
||||
}
|
||||
|
||||
s.running = true
|
||||
s.startedAt = time.Now()
|
||||
|
||||
// Run service in goroutine
|
||||
go func() {
|
||||
defer close(s.done)
|
||||
err := service.Run(ctx)
|
||||
if err != nil && !errors.Is(err, context.Canceled) {
|
||||
log.Errorf("embedded proxy service exited with error: %v", err)
|
||||
}
|
||||
s.mu.Lock()
|
||||
s.running = false
|
||||
s.mu.Unlock()
|
||||
}()
|
||||
|
||||
// Wait briefly for server to start
|
||||
time.Sleep(500 * time.Millisecond)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Stop stops the embedded proxy server.
|
||||
func (s *Server) Stop() error {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
|
||||
if !s.running {
|
||||
return nil
|
||||
}
|
||||
|
||||
if s.cancel != nil {
|
||||
s.cancel()
|
||||
}
|
||||
|
||||
// Wait for shutdown with timeout
|
||||
select {
|
||||
case <-s.done:
|
||||
case <-time.After(5 * time.Second):
|
||||
log.Warn("embedded server shutdown timeout")
|
||||
}
|
||||
|
||||
s.running = false
|
||||
return nil
|
||||
}
|
||||
|
||||
// Restart restarts the embedded proxy server.
|
||||
func (s *Server) Restart() error {
|
||||
configPath := s.configPath
|
||||
password := s.password
|
||||
|
||||
if err := s.Stop(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
time.Sleep(200 * time.Millisecond)
|
||||
|
||||
return s.Start(configPath, password)
|
||||
}
|
||||
|
||||
// IsRunning returns whether the server is currently running.
|
||||
func (s *Server) IsRunning() bool {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
return s.running
|
||||
}
|
||||
|
||||
// Port returns the port the server is running on.
|
||||
func (s *Server) Port() int {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
return s.port
|
||||
}
|
||||
|
||||
// StartedAt returns when the server was started.
|
||||
func (s *Server) StartedAt() time.Time {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
return s.startedAt
|
||||
}
|
||||
|
||||
// ConfigPath returns the configuration file path.
|
||||
func (s *Server) ConfigPath() string {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
return s.configPath
|
||||
}
|
||||
|
||||
// Global embedded server instance
|
||||
var globalServer = NewServer()
|
||||
|
||||
// StartGlobal starts the global embedded server.
|
||||
func StartGlobal(configPath, password string) error {
|
||||
return globalServer.Start(configPath, password)
|
||||
}
|
||||
|
||||
// StopGlobal stops the global embedded server.
|
||||
func StopGlobal() error {
|
||||
return globalServer.Stop()
|
||||
}
|
||||
|
||||
// RestartGlobal restarts the global embedded server.
|
||||
func RestartGlobal() error {
|
||||
return globalServer.Restart()
|
||||
}
|
||||
|
||||
// GlobalIsRunning returns whether the global server is running.
|
||||
func GlobalIsRunning() bool {
|
||||
return globalServer.IsRunning()
|
||||
}
|
||||
|
||||
// GlobalServer returns the global server instance.
|
||||
func GlobalServer() *Server {
|
||||
return globalServer
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,407 @@
|
||||
package integrations
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Agent struct {
|
||||
Name string `json:"name"`
|
||||
ID string `json:"id"`
|
||||
Detected bool `json:"detected"`
|
||||
Configured bool `json:"configured"`
|
||||
ConfigInstructions string `json:"configInstructions"`
|
||||
EnvVars map[string]string `json:"envVars"`
|
||||
CanAutoConfigure bool `json:"canAutoConfigure"`
|
||||
}
|
||||
|
||||
func DetectCLIAgents(proxyURL string) []Agent {
|
||||
if proxyURL == "" {
|
||||
proxyURL = "http://localhost:8080"
|
||||
}
|
||||
return []Agent{
|
||||
detectClaudeCode(proxyURL),
|
||||
detectCodexCLI(proxyURL),
|
||||
detectAider(proxyURL),
|
||||
detectGeminiCLI(proxyURL),
|
||||
detectDroidCLI(proxyURL),
|
||||
}
|
||||
}
|
||||
|
||||
func ConfigureCLIAgent(agentID, proxyURL string) error {
|
||||
if proxyURL == "" {
|
||||
proxyURL = "http://localhost:8080"
|
||||
}
|
||||
switch agentID {
|
||||
case "claude-code":
|
||||
return configureClaudeCode(proxyURL)
|
||||
case "codex":
|
||||
return configureCodexCLI(proxyURL)
|
||||
case "aider":
|
||||
return configureAider(proxyURL)
|
||||
case "gemini-cli":
|
||||
return configureGeminiCLI(proxyURL)
|
||||
case "droid":
|
||||
return configureDroidCLI(proxyURL)
|
||||
default:
|
||||
return fmt.Errorf("unknown agent: %s", agentID)
|
||||
}
|
||||
}
|
||||
|
||||
func getHomeDir() string {
|
||||
if runtime.GOOS == "windows" {
|
||||
return os.Getenv("USERPROFILE")
|
||||
}
|
||||
return os.Getenv("HOME")
|
||||
}
|
||||
|
||||
func getShellProfile() string {
|
||||
home := getHomeDir()
|
||||
if runtime.GOOS == "windows" {
|
||||
return filepath.Join(home, "Documents", "PowerShell", "Microsoft.PowerShell_profile.ps1")
|
||||
}
|
||||
shell := os.Getenv("SHELL")
|
||||
if strings.Contains(shell, "zsh") {
|
||||
return filepath.Join(home, ".zshrc")
|
||||
}
|
||||
if strings.Contains(shell, "fish") {
|
||||
return filepath.Join(home, ".config", "fish", "config.fish")
|
||||
}
|
||||
return filepath.Join(home, ".bashrc")
|
||||
}
|
||||
|
||||
func appendToShellProfile(content string) error {
|
||||
profilePath := getShellProfile()
|
||||
if err := os.MkdirAll(filepath.Dir(profilePath), 0755); err != nil {
|
||||
return err
|
||||
}
|
||||
existing, _ := os.ReadFile(profilePath)
|
||||
if strings.Contains(string(existing), "# ProxyPilot") {
|
||||
lines := strings.Split(string(existing), "\n")
|
||||
var newLines []string
|
||||
skip := false
|
||||
for _, line := range lines {
|
||||
if strings.Contains(line, "# ProxyPilot START") {
|
||||
skip = true
|
||||
continue
|
||||
}
|
||||
if strings.Contains(line, "# ProxyPilot END") {
|
||||
skip = false
|
||||
continue
|
||||
}
|
||||
if !skip {
|
||||
newLines = append(newLines, line)
|
||||
}
|
||||
}
|
||||
existing = []byte(strings.Join(newLines, "\n"))
|
||||
}
|
||||
f, err := os.OpenFile(profilePath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer f.Close()
|
||||
if len(existing) > 0 {
|
||||
f.Write(existing)
|
||||
if !strings.HasSuffix(string(existing), "\n") {
|
||||
f.WriteString("\n")
|
||||
}
|
||||
}
|
||||
f.WriteString("\n" + content)
|
||||
return nil
|
||||
}
|
||||
|
||||
func detectClaudeCode(proxyURL string) Agent {
|
||||
detected := false
|
||||
if _, err := exec.LookPath("claude"); err == nil {
|
||||
detected = true
|
||||
}
|
||||
if !detected {
|
||||
if appData := os.Getenv("APPDATA"); appData != "" {
|
||||
if _, err := os.Stat(filepath.Join(appData, "Claude")); err == nil {
|
||||
detected = true
|
||||
}
|
||||
}
|
||||
}
|
||||
configured := false
|
||||
if detected {
|
||||
i := &ClaudeIntegration{}
|
||||
configured, _ = i.IsConfigured(proxyURL)
|
||||
}
|
||||
return Agent{
|
||||
ID: "claude-code", Name: "Claude Code", Detected: detected, Configured: configured,
|
||||
ConfigInstructions: "Set ANTHROPIC_BASE_URL environment variable",
|
||||
EnvVars: map[string]string{"ANTHROPIC_BASE_URL": proxyURL + "/v1"}, CanAutoConfigure: true,
|
||||
}
|
||||
}
|
||||
|
||||
func configureClaudeCode(proxyURL string) error {
|
||||
var content string
|
||||
if runtime.GOOS == "windows" {
|
||||
content = fmt.Sprintf("# ProxyPilot START\n$env:ANTHROPIC_BASE_URL = \"%s/v1\"\n# ProxyPilot END\n", proxyURL)
|
||||
} else {
|
||||
content = fmt.Sprintf("# ProxyPilot START\nexport ANTHROPIC_BASE_URL=\"%s/v1\"\n# ProxyPilot END\n", proxyURL)
|
||||
}
|
||||
return appendToShellProfile(content)
|
||||
}
|
||||
|
||||
func detectCodexCLI(proxyURL string) Agent {
|
||||
detected := false
|
||||
if _, err := exec.LookPath("codex"); err == nil {
|
||||
detected = true
|
||||
}
|
||||
configured := false
|
||||
if detected {
|
||||
i := &CodexIntegration{}
|
||||
configured, _ = i.IsConfigured(proxyURL)
|
||||
}
|
||||
return Agent{
|
||||
ID: "codex", Name: "Codex CLI", Detected: detected, Configured: configured,
|
||||
ConfigInstructions: "Configure ~/.codex/config.toml",
|
||||
EnvVars: map[string]string{"OPENAI_BASE_URL": proxyURL + "/v1"}, CanAutoConfigure: true,
|
||||
}
|
||||
}
|
||||
|
||||
func configureCodexCLI(proxyURL string) error {
|
||||
home := getHomeDir()
|
||||
codexDir := filepath.Join(home, ".codex")
|
||||
if err := os.MkdirAll(codexDir, 0755); err != nil {
|
||||
return err
|
||||
}
|
||||
configContent := fmt.Sprintf("# Configured by ProxyPilot\nmodel_provider = \"openai\"\n\n[openai]\nbase_url = \"%s/v1\"\n", proxyURL)
|
||||
if err := os.WriteFile(filepath.Join(codexDir, "config.toml"), []byte(configContent), 0644); err != nil {
|
||||
return err
|
||||
}
|
||||
return os.WriteFile(filepath.Join(codexDir, "auth.json"), []byte("{\"OPENAI_API_KEY\": \"proxypilot-local\"}"), 0644)
|
||||
}
|
||||
|
||||
func detectAider(proxyURL string) Agent {
|
||||
detected := false
|
||||
if _, err := exec.LookPath("aider"); err == nil {
|
||||
detected = true
|
||||
}
|
||||
configured := false
|
||||
if detected {
|
||||
apiBase := os.Getenv("OPENAI_API_BASE")
|
||||
aiderBase := os.Getenv("AIDER_OPENAI_API_BASE")
|
||||
if strings.Contains(apiBase, proxyURL) || strings.Contains(aiderBase, proxyURL) {
|
||||
configured = true
|
||||
}
|
||||
}
|
||||
return Agent{
|
||||
ID: "aider", Name: "Aider", Detected: detected, Configured: configured,
|
||||
ConfigInstructions: "Set OPENAI_API_BASE environment variable",
|
||||
EnvVars: map[string]string{"OPENAI_API_BASE": proxyURL + "/v1"}, CanAutoConfigure: true,
|
||||
}
|
||||
}
|
||||
|
||||
func configureAider(proxyURL string) error {
|
||||
var content string
|
||||
if runtime.GOOS == "windows" {
|
||||
content = fmt.Sprintf("# ProxyPilot START - Aider\n$env:OPENAI_API_BASE = \"%s/v1\"\n$env:OPENAI_API_KEY = \"proxypilot-local\"\n# ProxyPilot END - Aider\n", proxyURL)
|
||||
} else {
|
||||
content = fmt.Sprintf("# ProxyPilot START - Aider\nexport OPENAI_API_BASE=\"%s/v1\"\nexport OPENAI_API_KEY=\"proxypilot-local\"\n# ProxyPilot END - Aider\n", proxyURL)
|
||||
}
|
||||
return appendToShellProfile(content)
|
||||
}
|
||||
|
||||
func detectGeminiCLI(proxyURL string) Agent {
|
||||
detected := false
|
||||
if _, err := exec.LookPath("gemini"); err == nil {
|
||||
detected = true
|
||||
}
|
||||
configured := false
|
||||
if detected {
|
||||
baseURL := os.Getenv("GOOGLE_GEMINI_BASE_URL")
|
||||
if strings.Contains(baseURL, proxyURL) {
|
||||
configured = true
|
||||
}
|
||||
}
|
||||
return Agent{
|
||||
ID: "gemini-cli", Name: "Gemini CLI", Detected: detected, Configured: configured,
|
||||
ConfigInstructions: "Set GOOGLE_GEMINI_BASE_URL environment variable",
|
||||
EnvVars: map[string]string{"GOOGLE_GEMINI_BASE_URL": proxyURL}, CanAutoConfigure: true,
|
||||
}
|
||||
}
|
||||
|
||||
func configureGeminiCLI(proxyURL string) error {
|
||||
var content string
|
||||
if runtime.GOOS == "windows" {
|
||||
content = fmt.Sprintf("# ProxyPilot START - Gemini\n$env:GOOGLE_GEMINI_BASE_URL = \"%s\"\n$env:GEMINI_API_KEY = \"proxypilot-local\"\n# ProxyPilot END - Gemini\n", proxyURL)
|
||||
} else {
|
||||
content = fmt.Sprintf("# ProxyPilot START - Gemini\nexport GOOGLE_GEMINI_BASE_URL=\"%s\"\nexport GEMINI_API_KEY=\"proxypilot-local\"\n# ProxyPilot END - Gemini\n", proxyURL)
|
||||
}
|
||||
return appendToShellProfile(content)
|
||||
}
|
||||
|
||||
func detectDroidCLI(proxyURL string) Agent {
|
||||
detected := false
|
||||
if _, err := exec.LookPath("droid"); err == nil {
|
||||
detected = true
|
||||
}
|
||||
home := getHomeDir()
|
||||
factoryDir := filepath.Join(home, ".factory")
|
||||
if _, err := os.Stat(factoryDir); err == nil {
|
||||
detected = true
|
||||
}
|
||||
configured := false
|
||||
if detected {
|
||||
configPath := filepath.Join(factoryDir, "config.json")
|
||||
if data, err := os.ReadFile(configPath); err == nil {
|
||||
if strings.Contains(string(data), proxyURL) {
|
||||
configured = true
|
||||
}
|
||||
}
|
||||
}
|
||||
return Agent{
|
||||
ID: "droid", Name: "Droid CLI", Detected: detected, Configured: configured,
|
||||
ConfigInstructions: "Configure ~/.factory/config.json",
|
||||
EnvVars: map[string]string{}, CanAutoConfigure: true,
|
||||
}
|
||||
}
|
||||
|
||||
func configureDroidCLI(proxyURL string) error {
|
||||
home := getHomeDir()
|
||||
factoryDir := filepath.Join(home, ".factory")
|
||||
configPath := filepath.Join(factoryDir, "config.json")
|
||||
if err := os.MkdirAll(factoryDir, 0755); err != nil {
|
||||
return err
|
||||
}
|
||||
var config map[string]interface{}
|
||||
if data, err := os.ReadFile(configPath); err == nil {
|
||||
json.Unmarshal(data, &config)
|
||||
}
|
||||
if config == nil {
|
||||
config = make(map[string]interface{})
|
||||
}
|
||||
customModels := []map[string]interface{}{
|
||||
{"model_display_name": "Claude Sonnet 4 (ProxyPilot)", "model": "claude-sonnet-4-20250514", "base_url": proxyURL + "/v1/", "api_key": "proxypilot-local", "provider": "generic-chat-completion-api", "max_tokens": 128000},
|
||||
{"model_display_name": "Claude Opus 4 (ProxyPilot)", "model": "claude-opus-4-20250514", "base_url": proxyURL + "/v1/", "api_key": "proxypilot-local", "provider": "generic-chat-completion-api", "max_tokens": 128000},
|
||||
{"model_display_name": "GPT-4o (ProxyPilot)", "model": "gpt-4o", "base_url": proxyURL + "/v1/", "api_key": "proxypilot-local", "provider": "generic-chat-completion-api", "max_tokens": 128000},
|
||||
{"model_display_name": "Gemini 2.5 Pro (ProxyPilot)", "model": "gemini-2.5-pro", "base_url": proxyURL + "/v1/", "api_key": "proxypilot-local", "provider": "generic-chat-completion-api", "max_tokens": 128000},
|
||||
}
|
||||
if existing, ok := config["custom_models"].([]interface{}); ok {
|
||||
for _, m := range existing {
|
||||
if model, ok := m.(map[string]interface{}); ok {
|
||||
if baseURL, ok := model["base_url"].(string); ok {
|
||||
if !strings.Contains(baseURL, "localhost:8") {
|
||||
customModels = append(customModels, model)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
config["custom_models"] = customModels
|
||||
data, err := json.MarshalIndent(config, "", " ")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return os.WriteFile(configPath, data, 0644)
|
||||
}
|
||||
|
||||
func UnconfigureCLIAgent(agentID string) error {
|
||||
switch agentID {
|
||||
case "claude-code":
|
||||
return removeFromShellProfile("# ProxyPilot START", "# ProxyPilot END")
|
||||
case "codex":
|
||||
return unconfigureCodexCLI()
|
||||
case "aider":
|
||||
return removeFromShellProfile("# ProxyPilot START - Aider", "# ProxyPilot END - Aider")
|
||||
case "gemini-cli":
|
||||
return removeFromShellProfile("# ProxyPilot START - Gemini", "# ProxyPilot END - Gemini")
|
||||
case "droid":
|
||||
return unconfigureDroidCLI()
|
||||
default:
|
||||
return fmt.Errorf("unknown agent: %s", agentID)
|
||||
}
|
||||
}
|
||||
|
||||
func removeFromShellProfile(startMarker, endMarker string) error {
|
||||
profilePath := getShellProfile()
|
||||
existing, err := os.ReadFile(profilePath)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
if !strings.Contains(string(existing), startMarker) {
|
||||
return nil
|
||||
}
|
||||
lines := strings.Split(string(existing), "\n")
|
||||
var newLines []string
|
||||
skip := false
|
||||
for _, line := range lines {
|
||||
if strings.Contains(line, startMarker) {
|
||||
skip = true
|
||||
continue
|
||||
}
|
||||
if strings.Contains(line, endMarker) {
|
||||
skip = false
|
||||
continue
|
||||
}
|
||||
if !skip {
|
||||
newLines = append(newLines, line)
|
||||
}
|
||||
}
|
||||
result := strings.TrimSpace(strings.Join(newLines, "\n")) + "\n"
|
||||
return os.WriteFile(profilePath, []byte(result), 0644)
|
||||
}
|
||||
|
||||
func unconfigureCodexCLI() error {
|
||||
home := getHomeDir()
|
||||
codexDir := filepath.Join(home, ".codex")
|
||||
configPath := filepath.Join(codexDir, "config.toml")
|
||||
data, err := os.ReadFile(configPath)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
if strings.Contains(string(data), "Configured by ProxyPilot") {
|
||||
os.Remove(configPath)
|
||||
os.Remove(filepath.Join(codexDir, "auth.json"))
|
||||
return nil
|
||||
}
|
||||
lines := strings.Split(string(data), "\n")
|
||||
var newLines []string
|
||||
for _, line := range lines {
|
||||
if !strings.Contains(line, "base_url") || !strings.Contains(line, "localhost:8") {
|
||||
newLines = append(newLines, line)
|
||||
}
|
||||
}
|
||||
return os.WriteFile(configPath, []byte(strings.Join(newLines, "\n")), 0644)
|
||||
}
|
||||
|
||||
func unconfigureDroidCLI() error {
|
||||
home := getHomeDir()
|
||||
factoryDir := filepath.Join(home, ".factory")
|
||||
configPath := filepath.Join(factoryDir, "config.json")
|
||||
data, err := os.ReadFile(configPath)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
var config map[string]interface{}
|
||||
if err := json.Unmarshal(data, &config); err != nil {
|
||||
return err
|
||||
}
|
||||
if existing, ok := config["custom_models"].([]interface{}); ok {
|
||||
var filtered []interface{}
|
||||
for _, m := range existing {
|
||||
if model, ok := m.(map[string]interface{}); ok {
|
||||
if baseURL, ok := model["base_url"].(string); ok {
|
||||
if !strings.Contains(baseURL, "localhost:8") {
|
||||
filtered = append(filtered, model)
|
||||
}
|
||||
} else {
|
||||
filtered = append(filtered, model)
|
||||
}
|
||||
}
|
||||
}
|
||||
config["custom_models"] = filtered
|
||||
}
|
||||
result, err := json.MarshalIndent(config, "", " ")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return os.WriteFile(configPath, result, 0644)
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -159,3 +159,46 @@ type ToolDeclaration struct {
|
||||
// FunctionDeclarations is a list of available functions that the model can call.
|
||||
FunctionDeclarations []interface{} `json:"functionDeclarations"`
|
||||
}
|
||||
|
||||
// RequestLogEntry represents a single entry in the request monitor.
|
||||
type RequestLogEntry struct {
|
||||
ID string `json:"id"`
|
||||
Timestamp time.Time `json:"timestamp"`
|
||||
Method string `json:"method"`
|
||||
Path string `json:"path"`
|
||||
Model string `json:"model"`
|
||||
Provider string `json:"provider"`
|
||||
Status int `json:"status"`
|
||||
LatencyMs int64 `json:"latencyMs"`
|
||||
InputTokens int `json:"inputTokens"`
|
||||
OutputTokens int `json:"outputTokens"`
|
||||
Error string `json:"error,omitempty"`
|
||||
}
|
||||
|
||||
// UsageStats represents aggregated usage statistics for the dashboard.
|
||||
type UsageStats struct {
|
||||
TotalRequests int64 `json:"totalRequests"`
|
||||
SuccessCount int64 `json:"successCount"`
|
||||
FailureCount int64 `json:"failureCount"`
|
||||
TotalInputTokens int64 `json:"totalInputTokens"`
|
||||
TotalOutputTokens int64 `json:"totalOutputTokens"`
|
||||
EstimatedCostSaved float64 `json:"estimatedCostSaved"` // Deprecated: use ActualCost
|
||||
ActualCost float64 `json:"actualCost"` // Cost via ProxyPilot
|
||||
DirectAPICost float64 `json:"directApiCost"` // What it would cost via direct API
|
||||
Savings float64 `json:"savings"` // DirectAPICost - ActualCost
|
||||
SavingsPercent float64 `json:"savingsPercent"` // Savings as percentage
|
||||
ByModel map[string]int64 `json:"byModel"`
|
||||
ByProvider map[string]int64 `json:"byProvider"`
|
||||
CostByModel map[string]float64 `json:"costByModel"` // Cost breakdown by model
|
||||
CostByProvider map[string]float64 `json:"costByProvider"` // Cost breakdown by provider
|
||||
Daily []DailyUsage `json:"daily"`
|
||||
}
|
||||
|
||||
// DailyUsage represents usage statistics for a single day.
|
||||
type DailyUsage struct {
|
||||
Date string `json:"date"`
|
||||
Requests int64 `json:"requests"`
|
||||
Tokens int64 `json:"tokens"`
|
||||
InputTokens int64 `json:"inputTokens"`
|
||||
OutputTokens int64 `json:"outputTokens"`
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
@@ -5,7 +5,6 @@ package misc
|
||||
|
||||
import (
|
||||
"embed"
|
||||
_ "embed"
|
||||
"strings"
|
||||
)
|
||||
|
||||
@@ -21,7 +20,6 @@ func CodexInstructionsForModel(modelName, systemInstructions string) (bool, stri
|
||||
last51Prompt := ""
|
||||
last52Prompt := ""
|
||||
last52CodexPrompt := ""
|
||||
// lastReviewPrompt := ""
|
||||
for _, entry := range entries {
|
||||
content, _ := codexInstructionsDir.ReadFile("codex_instructions/" + entry.Name())
|
||||
if strings.HasPrefix(systemInstructions, string(content)) {
|
||||
@@ -39,8 +37,6 @@ func CodexInstructionsForModel(modelName, systemInstructions string) (bool, stri
|
||||
last52Prompt = string(content)
|
||||
} else if strings.HasPrefix(entry.Name(), "gpt-5.2-codex_prompt.md") {
|
||||
last52CodexPrompt = string(content)
|
||||
} else if strings.HasPrefix(entry.Name(), "review_prompt.md") {
|
||||
// lastReviewPrompt = string(content)
|
||||
}
|
||||
}
|
||||
if strings.Contains(modelName, "codex-max") {
|
||||
|
||||
@@ -495,3 +495,78 @@ func addIfNotEmpty(segments *[]string, value string) {
|
||||
*segments = append(*segments, trimmed)
|
||||
}
|
||||
}
|
||||
|
||||
// EstimateRequestTokens estimates total tokens for a request payload.
|
||||
// It auto-detects the format (OpenAI chat, Claude, etc.) and counts accordingly.
|
||||
func EstimateRequestTokens(model string, payload []byte) (int64, error) {
|
||||
enc, err := getTokenizer(model)
|
||||
if err != nil {
|
||||
// Fallback: estimate 4 chars per token
|
||||
return int64(len(payload) / 4), nil
|
||||
}
|
||||
|
||||
// Detect format and count accordingly
|
||||
if gjson.GetBytes(payload, "messages").Exists() {
|
||||
if gjson.GetBytes(payload, "system").Exists() {
|
||||
// Claude format
|
||||
return countClaudeChatTokens(enc, payload)
|
||||
}
|
||||
// OpenAI chat format
|
||||
return countOpenAIChatTokens(enc, payload)
|
||||
}
|
||||
|
||||
// OpenAI responses format or other - use general text counting
|
||||
count, err := enc.Count(string(payload))
|
||||
if err != nil {
|
||||
return int64(len(payload) / 4), nil
|
||||
}
|
||||
return int64(count), nil
|
||||
}
|
||||
|
||||
// CalculateCompressionBudget determines how many tokens should be compressed
|
||||
// to bring context usage under the target percentage.
|
||||
func CalculateCompressionBudget(currentTokens int64, contextWindow int, targetPercent float64) int64 {
|
||||
if contextWindow <= 0 || targetPercent <= 0 || targetPercent >= 1 {
|
||||
return 0
|
||||
}
|
||||
targetTokens := int64(float64(contextWindow) * targetPercent)
|
||||
if currentTokens <= targetTokens {
|
||||
return 0
|
||||
}
|
||||
return currentTokens - targetTokens
|
||||
}
|
||||
|
||||
// ShouldTriggerCompression checks if compression should be triggered based on
|
||||
// current token usage vs context window size.
|
||||
func ShouldTriggerCompression(currentTokens int64, contextWindow int, thresholdPercent float64) bool {
|
||||
if contextWindow <= 0 || thresholdPercent <= 0 {
|
||||
return false
|
||||
}
|
||||
ratio := float64(currentTokens) / float64(contextWindow)
|
||||
return ratio >= thresholdPercent
|
||||
}
|
||||
|
||||
// GetDefaultContextWindow returns a conservative default context window for a model.
|
||||
func GetDefaultContextWindow(model string) int {
|
||||
lowerModel := strings.ToLower(model)
|
||||
switch {
|
||||
case strings.Contains(lowerModel, "claude-3.5"), strings.Contains(lowerModel, "claude-3-5"):
|
||||
return 200000
|
||||
case strings.Contains(lowerModel, "claude-3"):
|
||||
return 200000
|
||||
case strings.Contains(lowerModel, "claude"):
|
||||
return 100000
|
||||
case strings.Contains(lowerModel, "gpt-4-turbo"), strings.Contains(lowerModel, "gpt-4o"):
|
||||
return 128000
|
||||
case strings.Contains(lowerModel, "gpt-4"):
|
||||
return 8192
|
||||
case strings.Contains(lowerModel, "gpt-3.5"):
|
||||
return 16384
|
||||
case strings.Contains(lowerModel, "gemini"):
|
||||
return 1000000
|
||||
case strings.Contains(lowerModel, "o1"), strings.Contains(lowerModel, "o3"):
|
||||
return 128000
|
||||
default:
|
||||
return 100000 // Conservative default
|
||||
}
|
||||
}
|
||||
|
||||
+6
-185
@@ -1,192 +1,13 @@
|
||||
package trayicon
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"image"
|
||||
"image/color"
|
||||
_ "embed"
|
||||
)
|
||||
|
||||
// ProxyPilotICO returns a small multi-size .ico payload suitable for Windows tray icons.
|
||||
// It uses classic BMP/AND-mask frames for maximum compatibility with Windows loaders.
|
||||
//go:embed icon.ico
|
||||
var proxyPilotICO []byte
|
||||
|
||||
// ProxyPilotICO returns the embedded ProxyPilot icon for Windows tray.
|
||||
func ProxyPilotICO() []byte {
|
||||
sizes := []int{16, 32, 48}
|
||||
|
||||
type iconImg struct {
|
||||
size int
|
||||
bmp []byte
|
||||
}
|
||||
|
||||
images := make([]iconImg, 0, len(sizes))
|
||||
for _, s := range sizes {
|
||||
img := renderProxyPilotIcon(s)
|
||||
bmp := encodeICOFrameBMP32(img)
|
||||
if len(bmp) == 0 {
|
||||
continue
|
||||
}
|
||||
images = append(images, iconImg{size: s, bmp: bmp})
|
||||
}
|
||||
if len(images) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
// ICONDIR (6 bytes) + N*ICONDIRENTRY (16 bytes) + image blobs
|
||||
headerSize := 6 + 16*len(images)
|
||||
total := headerSize
|
||||
for _, im := range images {
|
||||
total += len(im.bmp)
|
||||
}
|
||||
out := make([]byte, 0, total)
|
||||
|
||||
// ICONDIR
|
||||
var hdr [6]byte
|
||||
binary.LittleEndian.PutUint16(hdr[0:2], 0) // reserved
|
||||
binary.LittleEndian.PutUint16(hdr[2:4], 1) // type=icon
|
||||
binary.LittleEndian.PutUint16(hdr[4:6], uint16(len(images)))
|
||||
out = append(out, hdr[:]...)
|
||||
|
||||
// Entries
|
||||
offset := uint32(headerSize)
|
||||
for _, im := range images {
|
||||
var e [16]byte
|
||||
w := byte(im.size)
|
||||
h := byte(im.size)
|
||||
if im.size >= 256 {
|
||||
w, h = 0, 0
|
||||
}
|
||||
e[0] = w
|
||||
e[1] = h
|
||||
e[2] = 0 // colors
|
||||
e[3] = 0 // reserved
|
||||
binary.LittleEndian.PutUint16(e[4:6], 1) // planes
|
||||
binary.LittleEndian.PutUint16(e[6:8], 32) // bitcount
|
||||
binary.LittleEndian.PutUint32(e[8:12], uint32(len(im.bmp)))
|
||||
binary.LittleEndian.PutUint32(e[12:16], offset)
|
||||
offset += uint32(len(im.bmp))
|
||||
out = append(out, e[:]...)
|
||||
}
|
||||
|
||||
// Image data
|
||||
for _, im := range images {
|
||||
out = append(out, im.bmp...)
|
||||
}
|
||||
return out
|
||||
return proxyPilotICO
|
||||
}
|
||||
|
||||
func renderProxyPilotIcon(size int) image.Image {
|
||||
img := image.NewRGBA(image.Rect(0, 0, size, size))
|
||||
|
||||
bg := color.RGBA{R: 12, G: 18, B: 28, A: 255}
|
||||
accent := color.RGBA{R: 66, G: 165, B: 245, A: 255}
|
||||
fg := color.RGBA{R: 235, G: 245, B: 255, A: 255}
|
||||
|
||||
// Background
|
||||
for y := 0; y < size; y++ {
|
||||
for x := 0; x < size; x++ {
|
||||
img.SetRGBA(x, y, bg)
|
||||
}
|
||||
}
|
||||
|
||||
// Accent border (1px)
|
||||
for x := 0; x < size; x++ {
|
||||
img.SetRGBA(x, 0, accent)
|
||||
img.SetRGBA(x, size-1, accent)
|
||||
}
|
||||
for y := 0; y < size; y++ {
|
||||
img.SetRGBA(0, y, accent)
|
||||
img.SetRGBA(size-1, y, accent)
|
||||
}
|
||||
|
||||
// Draw a simple "PP" monogram using a tiny bitmap font scaled to size.
|
||||
scale := 1
|
||||
if size >= 32 {
|
||||
scale = 2
|
||||
}
|
||||
if size >= 64 {
|
||||
scale = 3
|
||||
}
|
||||
drawGlyph(img, size, fg, scale, 2*scale, 2*scale, glyphP)
|
||||
drawGlyph(img, size, fg, scale, (2*scale)+(6*scale), 2*scale, glyphP)
|
||||
|
||||
// Small wing accent.
|
||||
drawRect(img, size, accent, 2*scale, (2*scale)+(11*scale), 12*scale, 2*scale)
|
||||
|
||||
return img
|
||||
}
|
||||
|
||||
var glyphP = []string{
|
||||
"11110",
|
||||
"10001",
|
||||
"11110",
|
||||
"10000",
|
||||
"10000",
|
||||
}
|
||||
|
||||
func drawGlyph(img *image.RGBA, size int, c color.RGBA, scale int, ox int, oy int, rows []string) {
|
||||
for y, row := range rows {
|
||||
for x, ch := range row {
|
||||
if ch != '1' {
|
||||
continue
|
||||
}
|
||||
drawRect(img, size, c, ox+(x*scale), oy+(y*scale), scale, scale)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func drawRect(img *image.RGBA, size int, c color.RGBA, x int, y int, w int, h int) {
|
||||
for yy := y; yy < y+h; yy++ {
|
||||
if yy < 0 || yy >= size {
|
||||
continue
|
||||
}
|
||||
for xx := x; xx < x+w; xx++ {
|
||||
if xx < 0 || xx >= size {
|
||||
continue
|
||||
}
|
||||
img.SetRGBA(xx, yy, c)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func encodeICOFrameBMP32(img image.Image) []byte {
|
||||
b := img.Bounds()
|
||||
w := b.Dx()
|
||||
h := b.Dy()
|
||||
if w <= 0 || h <= 0 || w > 255 || h > 255 {
|
||||
return nil
|
||||
}
|
||||
|
||||
// AND mask: 1bpp, rows padded to 32-bit boundaries.
|
||||
maskStride := ((w + 31) / 32) * 4
|
||||
maskSize := maskStride * h
|
||||
mask := make([]byte, maskSize) // all zeros (opaque)
|
||||
|
||||
// XOR bitmap: 32bpp BGRA, bottom-up rows.
|
||||
pixels := make([]byte, w*h*4)
|
||||
i := 0
|
||||
for y := h - 1; y >= 0; y-- {
|
||||
for x := 0; x < w; x++ {
|
||||
r, g, bb, a := img.At(b.Min.X+x, b.Min.Y+y).RGBA()
|
||||
pixels[i+0] = byte(bb >> 8)
|
||||
pixels[i+1] = byte(g >> 8)
|
||||
pixels[i+2] = byte(r >> 8)
|
||||
pixels[i+3] = byte(a >> 8)
|
||||
i += 4
|
||||
}
|
||||
}
|
||||
|
||||
// BITMAPINFOHEADER (40 bytes).
|
||||
var bih [40]byte
|
||||
binary.LittleEndian.PutUint32(bih[0:4], 40) // biSize
|
||||
binary.LittleEndian.PutUint32(bih[4:8], uint32(w)) // biWidth
|
||||
binary.LittleEndian.PutUint32(bih[8:12], uint32(h*2)) // biHeight (XOR + AND)
|
||||
binary.LittleEndian.PutUint16(bih[12:14], 1) // biPlanes
|
||||
binary.LittleEndian.PutUint16(bih[14:16], 32) // biBitCount
|
||||
binary.LittleEndian.PutUint32(bih[16:20], 0) // biCompression (BI_RGB)
|
||||
binary.LittleEndian.PutUint32(bih[20:24], uint32(len(pixels)+len(mask))) // biSizeImage
|
||||
|
||||
out := make([]byte, 0, len(bih)+len(pixels)+len(mask))
|
||||
out = append(out, bih[:]...)
|
||||
out = append(out, pixels...)
|
||||
out = append(out, mask...)
|
||||
return out
|
||||
}
|
||||
|
||||
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 87 KiB |
@@ -0,0 +1,338 @@
|
||||
package updates
|
||||
|
||||
import (
|
||||
"archive/tar"
|
||||
"archive/zip"
|
||||
"compress/gzip"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// InstallResult contains the result of an installation attempt.
|
||||
type InstallResult struct {
|
||||
Success bool `json:"success"`
|
||||
Message string `json:"message"`
|
||||
RestartCmd string `json:"restart_cmd,omitempty"`
|
||||
NeedsRestart bool `json:"needs_restart"`
|
||||
}
|
||||
|
||||
// PrepareInstall extracts and prepares the update for installation.
|
||||
// Returns the path to the extracted executable.
|
||||
func PrepareInstall(downloadResult *DownloadResult) (string, error) {
|
||||
if downloadResult == nil || downloadResult.FilePath == "" {
|
||||
return "", fmt.Errorf("no download result provided")
|
||||
}
|
||||
|
||||
filePath := downloadResult.FilePath
|
||||
ext := strings.ToLower(filepath.Ext(filePath))
|
||||
|
||||
// Handle different archive formats
|
||||
switch ext {
|
||||
case ".zip":
|
||||
return extractZip(filePath)
|
||||
case ".gz":
|
||||
if strings.HasSuffix(strings.ToLower(filePath), ".tar.gz") {
|
||||
return extractTarGz(filePath)
|
||||
}
|
||||
return "", fmt.Errorf("unsupported archive format: %s", ext)
|
||||
case ".exe", ".msi":
|
||||
// Already an executable or installer
|
||||
return filePath, nil
|
||||
default:
|
||||
// Assume it's a raw binary (Linux/macOS)
|
||||
// Make it executable
|
||||
if err := os.Chmod(filePath, 0755); err != nil {
|
||||
return "", fmt.Errorf("failed to make binary executable: %w", err)
|
||||
}
|
||||
return filePath, nil
|
||||
}
|
||||
}
|
||||
|
||||
// InstallUpdate installs the update by replacing the current binary.
|
||||
// On Windows, this schedules a replacement for the next restart.
|
||||
// On Unix, this replaces the binary directly.
|
||||
func InstallUpdate(executablePath string) (*InstallResult, error) {
|
||||
currentExe, err := os.Executable()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to get current executable path: %w", err)
|
||||
}
|
||||
|
||||
// Resolve any symlinks
|
||||
currentExe, err = filepath.EvalSymlinks(currentExe)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to resolve executable path: %w", err)
|
||||
}
|
||||
|
||||
switch runtime.GOOS {
|
||||
case "windows":
|
||||
return installWindows(executablePath, currentExe)
|
||||
default:
|
||||
return installUnix(executablePath, currentExe)
|
||||
}
|
||||
}
|
||||
|
||||
func installWindows(newExe, currentExe string) (*InstallResult, error) {
|
||||
// On Windows, we can't replace a running executable directly.
|
||||
// We use a batch script that waits for the process to exit, then replaces the file.
|
||||
|
||||
backupPath := currentExe + ".old"
|
||||
tempScript := filepath.Join(os.TempDir(), "proxypilot-update.bat")
|
||||
|
||||
// Create update script
|
||||
script := fmt.Sprintf(`@echo off
|
||||
echo Waiting for ProxyPilot to exit...
|
||||
timeout /t 2 /nobreak >nul
|
||||
:retry
|
||||
tasklist /FI "IMAGENAME eq %s" 2>NUL | find /I /N "%s">NUL
|
||||
if "%%ERRORLEVEL%%"=="0" (
|
||||
timeout /t 1 /nobreak >nul
|
||||
goto retry
|
||||
)
|
||||
echo Backing up old version...
|
||||
if exist "%s" del /f "%s"
|
||||
move /y "%s" "%s"
|
||||
echo Installing new version...
|
||||
copy /y "%s" "%s"
|
||||
echo Starting new version...
|
||||
start "" "%s"
|
||||
del "%%~f0"
|
||||
`, filepath.Base(currentExe), filepath.Base(currentExe),
|
||||
backupPath, backupPath,
|
||||
currentExe, backupPath,
|
||||
newExe, currentExe,
|
||||
currentExe)
|
||||
|
||||
if err := os.WriteFile(tempScript, []byte(script), 0755); err != nil {
|
||||
return nil, fmt.Errorf("failed to create update script: %w", err)
|
||||
}
|
||||
|
||||
// Start the update script
|
||||
cmd := exec.Command("cmd", "/c", "start", "/min", tempScript)
|
||||
if err := cmd.Start(); err != nil {
|
||||
return nil, fmt.Errorf("failed to start update script: %w", err)
|
||||
}
|
||||
|
||||
return &InstallResult{
|
||||
Success: true,
|
||||
Message: "Update scheduled. The application will restart shortly.",
|
||||
RestartCmd: tempScript,
|
||||
NeedsRestart: true,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func installUnix(newExe, currentExe string) (*InstallResult, error) {
|
||||
// On Unix, we can replace the binary while it's running.
|
||||
// The new binary will be used on next execution.
|
||||
|
||||
backupPath := currentExe + ".old"
|
||||
|
||||
// Remove old backup if exists
|
||||
os.Remove(backupPath)
|
||||
|
||||
// Rename current to backup
|
||||
if err := os.Rename(currentExe, backupPath); err != nil {
|
||||
return nil, fmt.Errorf("failed to backup current executable: %w", err)
|
||||
}
|
||||
|
||||
// Copy new executable to current location
|
||||
if err := copyFile(newExe, currentExe); err != nil {
|
||||
// Try to restore backup
|
||||
os.Rename(backupPath, currentExe)
|
||||
return nil, fmt.Errorf("failed to install new executable: %w", err)
|
||||
}
|
||||
|
||||
// Make executable
|
||||
if err := os.Chmod(currentExe, 0755); err != nil {
|
||||
return nil, fmt.Errorf("failed to set permissions: %w", err)
|
||||
}
|
||||
|
||||
return &InstallResult{
|
||||
Success: true,
|
||||
Message: "Update installed successfully. Please restart the application.",
|
||||
NeedsRestart: true,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func copyFile(src, dst string) error {
|
||||
in, err := os.Open(src)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer in.Close()
|
||||
|
||||
out, err := os.Create(dst)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer out.Close()
|
||||
|
||||
_, err = io.Copy(out, in)
|
||||
return err
|
||||
}
|
||||
|
||||
func extractZip(zipPath string) (string, error) {
|
||||
r, err := zip.OpenReader(zipPath)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to open zip: %w", err)
|
||||
}
|
||||
defer r.Close()
|
||||
|
||||
extractDir := filepath.Dir(zipPath)
|
||||
var executablePath string
|
||||
|
||||
for _, f := range r.File {
|
||||
fpath := filepath.Join(extractDir, f.Name)
|
||||
|
||||
// Check for ZipSlip vulnerability
|
||||
if !strings.HasPrefix(fpath, filepath.Clean(extractDir)+string(os.PathSeparator)) {
|
||||
return "", fmt.Errorf("invalid file path in zip: %s", f.Name)
|
||||
}
|
||||
|
||||
if f.FileInfo().IsDir() {
|
||||
os.MkdirAll(fpath, os.ModePerm)
|
||||
continue
|
||||
}
|
||||
|
||||
if err := os.MkdirAll(filepath.Dir(fpath), os.ModePerm); err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
outFile, err := os.OpenFile(fpath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode())
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
rc, err := f.Open()
|
||||
if err != nil {
|
||||
outFile.Close()
|
||||
return "", err
|
||||
}
|
||||
|
||||
_, err = io.Copy(outFile, rc)
|
||||
outFile.Close()
|
||||
rc.Close()
|
||||
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
// Look for the main executable
|
||||
if isExecutable(f.Name) {
|
||||
executablePath = fpath
|
||||
}
|
||||
}
|
||||
|
||||
if executablePath == "" {
|
||||
return "", fmt.Errorf("no executable found in archive")
|
||||
}
|
||||
|
||||
return executablePath, nil
|
||||
}
|
||||
|
||||
func extractTarGz(tarGzPath string) (string, error) {
|
||||
f, err := os.Open(tarGzPath)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to open tar.gz: %w", err)
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
gzr, err := gzip.NewReader(f)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to create gzip reader: %w", err)
|
||||
}
|
||||
defer gzr.Close()
|
||||
|
||||
tr := tar.NewReader(gzr)
|
||||
extractDir := filepath.Dir(tarGzPath)
|
||||
var executablePath string
|
||||
|
||||
for {
|
||||
header, err := tr.Next()
|
||||
if err == io.EOF {
|
||||
break
|
||||
}
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to read tar: %w", err)
|
||||
}
|
||||
|
||||
fpath := filepath.Join(extractDir, header.Name)
|
||||
|
||||
// Check for path traversal
|
||||
if !strings.HasPrefix(fpath, filepath.Clean(extractDir)+string(os.PathSeparator)) {
|
||||
return "", fmt.Errorf("invalid file path in tar: %s", header.Name)
|
||||
}
|
||||
|
||||
switch header.Typeflag {
|
||||
case tar.TypeDir:
|
||||
if err := os.MkdirAll(fpath, 0755); err != nil {
|
||||
return "", err
|
||||
}
|
||||
case tar.TypeReg:
|
||||
if err := os.MkdirAll(filepath.Dir(fpath), 0755); err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
outFile, err := os.Create(fpath)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
if _, err := io.Copy(outFile, tr); err != nil {
|
||||
outFile.Close()
|
||||
return "", err
|
||||
}
|
||||
outFile.Close()
|
||||
|
||||
if err := os.Chmod(fpath, os.FileMode(header.Mode)); err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
if isExecutable(header.Name) {
|
||||
executablePath = fpath
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if executablePath == "" {
|
||||
return "", fmt.Errorf("no executable found in archive")
|
||||
}
|
||||
|
||||
return executablePath, nil
|
||||
}
|
||||
|
||||
func isExecutable(name string) bool {
|
||||
base := strings.ToLower(filepath.Base(name))
|
||||
// Look for common executable patterns
|
||||
if strings.HasSuffix(base, ".exe") {
|
||||
return true
|
||||
}
|
||||
if strings.Contains(base, "proxypilot") && !strings.Contains(base, ".") {
|
||||
return true
|
||||
}
|
||||
if base == "proxypilot" || base == "cliproxyapi" {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// CleanupOldVersions removes backup files from previous updates.
|
||||
func CleanupOldVersions() error {
|
||||
currentExe, err := os.Executable()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
currentExe, _ = filepath.EvalSymlinks(currentExe)
|
||||
backupPath := currentExe + ".old"
|
||||
|
||||
if _, err := os.Stat(backupPath); err == nil {
|
||||
return os.Remove(backupPath)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,259 @@
|
||||
package updates
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/router-for-me/CLIProxyAPI/v6/internal/buildinfo"
|
||||
)
|
||||
|
||||
type UpdateInfo struct {
|
||||
Available bool `json:"available"`
|
||||
Version string `json:"version"`
|
||||
DownloadURL string `json:"download_url"`
|
||||
ReleaseNotes string `json:"release_notes,omitempty"`
|
||||
Assets []Asset `json:"assets,omitempty"`
|
||||
}
|
||||
|
||||
type Asset struct {
|
||||
Name string `json:"name"`
|
||||
DownloadURL string `json:"browser_download_url"`
|
||||
Size int64 `json:"size"`
|
||||
}
|
||||
|
||||
type DownloadProgress struct {
|
||||
TotalBytes int64 `json:"total_bytes"`
|
||||
DownloadedBytes int64 `json:"downloaded_bytes"`
|
||||
Percent float64 `json:"percent"`
|
||||
}
|
||||
|
||||
type DownloadResult struct {
|
||||
FilePath string `json:"file_path"`
|
||||
Version string `json:"version"`
|
||||
SignaturePath string `json:"signature_path,omitempty"`
|
||||
}
|
||||
|
||||
// GitHubRepo is the GitHub repository for update checks.
|
||||
const GitHubRepo = "Finesssee/ProxyPilot"
|
||||
|
||||
func CheckForUpdates() (*UpdateInfo, error) {
|
||||
client := &http.Client{Timeout: 10 * time.Second}
|
||||
// Use a User-Agent as required by GitHub API
|
||||
url := fmt.Sprintf("https://api.github.com/repos/%s/releases/latest", GitHubRepo)
|
||||
req, err := http.NewRequest("GET", url, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req.Header.Set("User-Agent", "ProxyPilot-Updater")
|
||||
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
// Handle 404 - no releases found
|
||||
if resp.StatusCode == http.StatusNotFound {
|
||||
return &UpdateInfo{
|
||||
Available: false,
|
||||
Version: "",
|
||||
DownloadURL: "",
|
||||
ReleaseNotes: "No releases published yet",
|
||||
Assets: nil,
|
||||
}, nil
|
||||
}
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return nil, fmt.Errorf("failed to check for updates: %s", resp.Status)
|
||||
}
|
||||
|
||||
var release struct {
|
||||
TagName string `json:"tag_name"`
|
||||
HTMLURL string `json:"html_url"`
|
||||
Body string `json:"body"`
|
||||
Assets []struct {
|
||||
Name string `json:"name"`
|
||||
BrowserDownloadURL string `json:"browser_download_url"`
|
||||
Size int64 `json:"size"`
|
||||
} `json:"assets"`
|
||||
}
|
||||
if err := json.NewDecoder(resp.Body).Decode(&release); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
currentVersion := strings.TrimPrefix(buildinfo.Version, "v")
|
||||
latestVersion := strings.TrimPrefix(release.TagName, "v")
|
||||
|
||||
available := false
|
||||
if currentVersion != "dev" && latestVersion != "" && latestVersion != currentVersion {
|
||||
// Simple comparison: if versions are different and not dev, an update is available.
|
||||
available = true
|
||||
}
|
||||
|
||||
assets := make([]Asset, 0, len(release.Assets))
|
||||
for _, a := range release.Assets {
|
||||
assets = append(assets, Asset{
|
||||
Name: a.Name,
|
||||
DownloadURL: a.BrowserDownloadURL,
|
||||
Size: a.Size,
|
||||
})
|
||||
}
|
||||
|
||||
return &UpdateInfo{
|
||||
Available: available,
|
||||
Version: latestVersion,
|
||||
DownloadURL: release.HTMLURL,
|
||||
ReleaseNotes: release.Body,
|
||||
Assets: assets,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// GetAssetForPlatform returns the appropriate asset for the current OS/arch.
|
||||
func GetAssetForPlatform(assets []Asset) (*Asset, *Asset) {
|
||||
goos := runtime.GOOS
|
||||
goarch := runtime.GOARCH
|
||||
|
||||
// Map Go arch to common naming conventions
|
||||
archName := goarch
|
||||
if goarch == "amd64" {
|
||||
archName = "x86_64"
|
||||
} else if goarch == "386" {
|
||||
archName = "i386"
|
||||
}
|
||||
|
||||
var binaryAsset, sigAsset *Asset
|
||||
for i := range assets {
|
||||
name := strings.ToLower(assets[i].Name)
|
||||
|
||||
// Check for matching platform binary
|
||||
if strings.Contains(name, goos) && (strings.Contains(name, archName) || strings.Contains(name, goarch)) {
|
||||
if strings.HasSuffix(name, ".sig") || strings.HasSuffix(name, ".asc") {
|
||||
sigAsset = &assets[i]
|
||||
} else if strings.HasSuffix(name, ".exe") || strings.HasSuffix(name, ".zip") ||
|
||||
strings.HasSuffix(name, ".tar.gz") || !strings.Contains(name, ".") ||
|
||||
strings.HasSuffix(name, ".dmg") || strings.HasSuffix(name, ".msi") {
|
||||
binaryAsset = &assets[i]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return binaryAsset, sigAsset
|
||||
}
|
||||
|
||||
// DownloadUpdate downloads the update to a temporary directory.
|
||||
func DownloadUpdate(version string, progressCb func(DownloadProgress)) (*DownloadResult, error) {
|
||||
// Get update info to find assets
|
||||
info, err := CheckForUpdates()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to check for updates: %w", err)
|
||||
}
|
||||
|
||||
if !info.Available || info.Version != version {
|
||||
return nil, fmt.Errorf("version %s is not available", version)
|
||||
}
|
||||
|
||||
binaryAsset, sigAsset := GetAssetForPlatform(info.Assets)
|
||||
if binaryAsset == nil {
|
||||
return nil, fmt.Errorf("no suitable binary found for %s/%s", runtime.GOOS, runtime.GOARCH)
|
||||
}
|
||||
|
||||
// Create temp directory for downloads
|
||||
tempDir, err := os.MkdirTemp("", "proxypilot-update-*")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to create temp directory: %w", err)
|
||||
}
|
||||
|
||||
// Download binary
|
||||
binaryPath := filepath.Join(tempDir, binaryAsset.Name)
|
||||
if err := downloadFile(binaryAsset.DownloadURL, binaryPath, binaryAsset.Size, progressCb); err != nil {
|
||||
os.RemoveAll(tempDir)
|
||||
return nil, fmt.Errorf("failed to download binary: %w", err)
|
||||
}
|
||||
|
||||
result := &DownloadResult{
|
||||
FilePath: binaryPath,
|
||||
Version: version,
|
||||
}
|
||||
|
||||
// Download signature if available
|
||||
if sigAsset != nil {
|
||||
sigPath := filepath.Join(tempDir, sigAsset.Name)
|
||||
if err := downloadFile(sigAsset.DownloadURL, sigPath, sigAsset.Size, nil); err == nil {
|
||||
result.SignaturePath = sigPath
|
||||
}
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func downloadFile(url, destPath string, expectedSize int64, progressCb func(DownloadProgress)) error {
|
||||
client := &http.Client{Timeout: 30 * time.Minute}
|
||||
req, err := http.NewRequest("GET", url, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
req.Header.Set("User-Agent", "ProxyPilot-Updater")
|
||||
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return fmt.Errorf("download failed: %s", resp.Status)
|
||||
}
|
||||
|
||||
out, err := os.Create(destPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer out.Close()
|
||||
|
||||
// Use expected size from API if content-length is not set
|
||||
totalSize := resp.ContentLength
|
||||
if totalSize <= 0 {
|
||||
totalSize = expectedSize
|
||||
}
|
||||
|
||||
if progressCb != nil && totalSize > 0 {
|
||||
// Wrap reader with progress tracking
|
||||
pr := &progressReader{
|
||||
reader: resp.Body,
|
||||
total: totalSize,
|
||||
callback: progressCb,
|
||||
}
|
||||
_, err = io.Copy(out, pr)
|
||||
} else {
|
||||
_, err = io.Copy(out, resp.Body)
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
type progressReader struct {
|
||||
reader io.Reader
|
||||
total int64
|
||||
current int64
|
||||
callback func(DownloadProgress)
|
||||
}
|
||||
|
||||
func (pr *progressReader) Read(p []byte) (int, error) {
|
||||
n, err := pr.reader.Read(p)
|
||||
pr.current += int64(n)
|
||||
if pr.callback != nil {
|
||||
pr.callback(DownloadProgress{
|
||||
TotalBytes: pr.total,
|
||||
DownloadedBytes: pr.current,
|
||||
Percent: float64(pr.current) / float64(pr.total) * 100,
|
||||
})
|
||||
}
|
||||
return n, err
|
||||
}
|
||||
@@ -0,0 +1,146 @@
|
||||
package updates
|
||||
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// VerifyResult contains the result of a verification check.
|
||||
type VerifyResult struct {
|
||||
Valid bool `json:"valid"`
|
||||
Message string `json:"message"`
|
||||
Checksum string `json:"checksum,omitempty"`
|
||||
}
|
||||
|
||||
// VerifyDownload verifies the downloaded file.
|
||||
// For now, this does basic checksum verification.
|
||||
// In the future, this can be extended to verify GPG signatures.
|
||||
func VerifyDownload(result *DownloadResult) (*VerifyResult, error) {
|
||||
if result == nil || result.FilePath == "" {
|
||||
return &VerifyResult{
|
||||
Valid: false,
|
||||
Message: "no download result provided",
|
||||
}, nil
|
||||
}
|
||||
|
||||
// Check file exists
|
||||
info, err := os.Stat(result.FilePath)
|
||||
if err != nil {
|
||||
return &VerifyResult{
|
||||
Valid: false,
|
||||
Message: fmt.Sprintf("file not found: %v", err),
|
||||
}, nil
|
||||
}
|
||||
|
||||
// Check file has reasonable size (at least 1MB for a Go binary)
|
||||
if info.Size() < 1024*1024 {
|
||||
return &VerifyResult{
|
||||
Valid: false,
|
||||
Message: "downloaded file is too small to be valid",
|
||||
}, nil
|
||||
}
|
||||
|
||||
// Compute SHA256 checksum
|
||||
checksum, err := computeSHA256(result.FilePath)
|
||||
if err != nil {
|
||||
return &VerifyResult{
|
||||
Valid: false,
|
||||
Message: fmt.Sprintf("failed to compute checksum: %v", err),
|
||||
}, nil
|
||||
}
|
||||
|
||||
// If we have a signature file, try to verify
|
||||
if result.SignaturePath != "" {
|
||||
sigValid, err := verifySignatureFile(result.FilePath, result.SignaturePath)
|
||||
if err != nil {
|
||||
// Signature verification failed - but file may still be valid
|
||||
return &VerifyResult{
|
||||
Valid: true, // Still valid, just no signature verification
|
||||
Message: fmt.Sprintf("signature verification skipped: %v", err),
|
||||
Checksum: checksum,
|
||||
}, nil
|
||||
}
|
||||
if !sigValid {
|
||||
return &VerifyResult{
|
||||
Valid: false,
|
||||
Message: "signature verification failed",
|
||||
}, nil
|
||||
}
|
||||
return &VerifyResult{
|
||||
Valid: true,
|
||||
Message: "signature verified successfully",
|
||||
Checksum: checksum,
|
||||
}, nil
|
||||
}
|
||||
|
||||
return &VerifyResult{
|
||||
Valid: true,
|
||||
Message: "basic verification passed (no signature available)",
|
||||
Checksum: checksum,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func computeSHA256(filePath string) (string, error) {
|
||||
f, err := os.Open(filePath)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
h := sha256.New()
|
||||
if _, err := io.Copy(h, f); err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return hex.EncodeToString(h.Sum(nil)), nil
|
||||
}
|
||||
|
||||
// verifySignatureFile attempts to verify a GPG/PGP signature.
|
||||
// This is a placeholder - real implementation would use golang.org/x/crypto/openpgp
|
||||
// or similar library with an embedded public key.
|
||||
func verifySignatureFile(filePath, sigPath string) (bool, error) {
|
||||
// Check signature file exists
|
||||
sigData, err := os.ReadFile(sigPath)
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("failed to read signature file: %w", err)
|
||||
}
|
||||
|
||||
// For now, just check it's not empty and looks like a signature
|
||||
sigStr := strings.TrimSpace(string(sigData))
|
||||
if len(sigStr) < 64 {
|
||||
return false, fmt.Errorf("signature file too short")
|
||||
}
|
||||
|
||||
// TODO: Implement actual GPG signature verification
|
||||
// This requires embedding a public key and using openpgp library
|
||||
return false, fmt.Errorf("GPG signature verification not yet implemented")
|
||||
}
|
||||
|
||||
// VerifyChecksum verifies a file against an expected SHA256 checksum.
|
||||
func VerifyChecksum(filePath, expectedChecksum string) (*VerifyResult, error) {
|
||||
actualChecksum, err := computeSHA256(filePath)
|
||||
if err != nil {
|
||||
return &VerifyResult{
|
||||
Valid: false,
|
||||
Message: fmt.Sprintf("failed to compute checksum: %v", err),
|
||||
}, nil
|
||||
}
|
||||
|
||||
if strings.EqualFold(actualChecksum, expectedChecksum) {
|
||||
return &VerifyResult{
|
||||
Valid: true,
|
||||
Message: "checksum verified successfully",
|
||||
Checksum: actualChecksum,
|
||||
}, nil
|
||||
}
|
||||
|
||||
return &VerifyResult{
|
||||
Valid: false,
|
||||
Message: fmt.Sprintf("checksum mismatch: expected %s, got %s", expectedChecksum, actualChecksum),
|
||||
Checksum: actualChecksum,
|
||||
}, nil
|
||||
}
|
||||
+170
-21
@@ -6,11 +6,13 @@ package usage
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/router-for-me/CLIProxyAPI/v6/internal/interfaces"
|
||||
coreusage "github.com/router-for-me/CLIProxyAPI/v6/sdk/cliproxy/usage"
|
||||
)
|
||||
|
||||
@@ -59,17 +61,21 @@ func StatisticsEnabled() bool { return statisticsEnabled.Load() }
|
||||
type RequestStatistics struct {
|
||||
mu sync.RWMutex
|
||||
|
||||
totalRequests int64
|
||||
successCount int64
|
||||
failureCount int64
|
||||
totalTokens int64
|
||||
totalRequests int64
|
||||
successCount int64
|
||||
failureCount int64
|
||||
totalTokens int64
|
||||
totalInputTokens int64
|
||||
totalOutputTokens int64
|
||||
|
||||
apis map[string]*apiStats
|
||||
|
||||
requestsByDay map[string]int64
|
||||
requestsByHour map[int]int64
|
||||
tokensByDay map[string]int64
|
||||
tokensByHour map[int]int64
|
||||
requestsByDay map[string]int64
|
||||
requestsByHour map[int]int64
|
||||
tokensByDay map[string]int64
|
||||
inputTokensByDay map[string]int64
|
||||
outputTokensByDay map[string]int64
|
||||
tokensByHour map[int]int64
|
||||
}
|
||||
|
||||
// apiStats holds aggregated metrics for a single API key.
|
||||
@@ -106,17 +112,21 @@ type TokenStats struct {
|
||||
|
||||
// StatisticsSnapshot represents an immutable view of the aggregated metrics.
|
||||
type StatisticsSnapshot struct {
|
||||
TotalRequests int64 `json:"total_requests"`
|
||||
SuccessCount int64 `json:"success_count"`
|
||||
FailureCount int64 `json:"failure_count"`
|
||||
TotalTokens int64 `json:"total_tokens"`
|
||||
TotalRequests int64 `json:"total_requests"`
|
||||
SuccessCount int64 `json:"success_count"`
|
||||
FailureCount int64 `json:"failure_count"`
|
||||
TotalTokens int64 `json:"total_tokens"`
|
||||
TotalInputTokens int64 `json:"total_input_tokens"`
|
||||
TotalOutputTokens int64 `json:"total_output_tokens"`
|
||||
|
||||
APIs map[string]APISnapshot `json:"apis"`
|
||||
|
||||
RequestsByDay map[string]int64 `json:"requests_by_day"`
|
||||
RequestsByHour map[string]int64 `json:"requests_by_hour"`
|
||||
TokensByDay map[string]int64 `json:"tokens_by_day"`
|
||||
TokensByHour map[string]int64 `json:"tokens_by_hour"`
|
||||
RequestsByDay map[string]int64 `json:"requests_by_day"`
|
||||
RequestsByHour map[string]int64 `json:"requests_by_hour"`
|
||||
TokensByDay map[string]int64 `json:"tokens_by_day"`
|
||||
InputTokensByDay map[string]int64 `json:"input_tokens_by_day"`
|
||||
OutputTokensByDay map[string]int64 `json:"output_tokens_by_day"`
|
||||
TokensByHour map[string]int64 `json:"tokens_by_hour"`
|
||||
}
|
||||
|
||||
// APISnapshot summarises metrics for a single API key.
|
||||
@@ -141,11 +151,13 @@ func GetRequestStatistics() *RequestStatistics { return defaultRequestStatistics
|
||||
// NewRequestStatistics constructs an empty statistics store.
|
||||
func NewRequestStatistics() *RequestStatistics {
|
||||
return &RequestStatistics{
|
||||
apis: make(map[string]*apiStats),
|
||||
requestsByDay: make(map[string]int64),
|
||||
requestsByHour: make(map[int]int64),
|
||||
tokensByDay: make(map[string]int64),
|
||||
tokensByHour: make(map[int]int64),
|
||||
apis: make(map[string]*apiStats),
|
||||
requestsByDay: make(map[string]int64),
|
||||
requestsByHour: make(map[int]int64),
|
||||
tokensByDay: make(map[string]int64),
|
||||
inputTokensByDay: make(map[string]int64),
|
||||
outputTokensByDay: make(map[string]int64),
|
||||
tokensByHour: make(map[int]int64),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -163,6 +175,8 @@ func (s *RequestStatistics) Record(ctx context.Context, record coreusage.Record)
|
||||
}
|
||||
detail := normaliseDetail(record.Detail)
|
||||
totalTokens := detail.TotalTokens
|
||||
inputTokens := detail.InputTokens
|
||||
outputTokens := detail.OutputTokens
|
||||
statsKey := record.APIKey
|
||||
if statsKey == "" {
|
||||
statsKey = resolveAPIIdentifier(ctx, record)
|
||||
@@ -189,6 +203,8 @@ func (s *RequestStatistics) Record(ctx context.Context, record coreusage.Record)
|
||||
s.failureCount++
|
||||
}
|
||||
s.totalTokens += totalTokens
|
||||
s.totalInputTokens += inputTokens
|
||||
s.totalOutputTokens += outputTokens
|
||||
|
||||
stats, ok := s.apis[statsKey]
|
||||
if !ok {
|
||||
@@ -206,6 +222,8 @@ func (s *RequestStatistics) Record(ctx context.Context, record coreusage.Record)
|
||||
s.requestsByDay[dayKey]++
|
||||
s.requestsByHour[hourKey]++
|
||||
s.tokensByDay[dayKey] += totalTokens
|
||||
s.inputTokensByDay[dayKey] += inputTokens
|
||||
s.outputTokensByDay[dayKey] += outputTokens
|
||||
s.tokensByHour[hourKey] += totalTokens
|
||||
}
|
||||
|
||||
@@ -236,6 +254,8 @@ func (s *RequestStatistics) Snapshot() StatisticsSnapshot {
|
||||
result.SuccessCount = s.successCount
|
||||
result.FailureCount = s.failureCount
|
||||
result.TotalTokens = s.totalTokens
|
||||
result.TotalInputTokens = s.totalInputTokens
|
||||
result.TotalOutputTokens = s.totalOutputTokens
|
||||
|
||||
result.APIs = make(map[string]APISnapshot, len(s.apis))
|
||||
for apiName, stats := range s.apis {
|
||||
@@ -272,6 +292,16 @@ func (s *RequestStatistics) Snapshot() StatisticsSnapshot {
|
||||
result.TokensByDay[k] = v
|
||||
}
|
||||
|
||||
result.InputTokensByDay = make(map[string]int64, len(s.inputTokensByDay))
|
||||
for k, v := range s.inputTokensByDay {
|
||||
result.InputTokensByDay[k] = v
|
||||
}
|
||||
|
||||
result.OutputTokensByDay = make(map[string]int64, len(s.outputTokensByDay))
|
||||
for k, v := range s.outputTokensByDay {
|
||||
result.OutputTokensByDay[k] = v
|
||||
}
|
||||
|
||||
result.TokensByHour = make(map[string]int64, len(s.tokensByHour))
|
||||
for hour, v := range s.tokensByHour {
|
||||
key := formatHour(hour)
|
||||
@@ -347,3 +377,122 @@ func formatHour(hour int) string {
|
||||
hour = hour % 24
|
||||
return fmt.Sprintf("%02d", hour)
|
||||
}
|
||||
|
||||
// ComputeUsageStats converts a StatisticsSnapshot into a structured UsageStats object.
|
||||
func ComputeUsageStats(snapshot StatisticsSnapshot) interfaces.UsageStats {
|
||||
stats := interfaces.UsageStats{
|
||||
TotalRequests: snapshot.TotalRequests,
|
||||
SuccessCount: snapshot.SuccessCount,
|
||||
FailureCount: snapshot.FailureCount,
|
||||
TotalInputTokens: snapshot.TotalInputTokens,
|
||||
TotalOutputTokens: snapshot.TotalOutputTokens,
|
||||
EstimatedCostSaved: 0,
|
||||
ActualCost: 0,
|
||||
DirectAPICost: 0,
|
||||
Savings: 0,
|
||||
SavingsPercent: 0,
|
||||
ByModel: make(map[string]int64),
|
||||
ByProvider: make(map[string]int64),
|
||||
CostByModel: make(map[string]float64),
|
||||
CostByProvider: make(map[string]float64),
|
||||
Daily: make([]interfaces.DailyUsage, 0),
|
||||
}
|
||||
|
||||
var totalProxyCost float64
|
||||
var totalDirectCost float64
|
||||
|
||||
for apiName, apiSnapshot := range snapshot.APIs {
|
||||
// Try to identify provider from apiName
|
||||
provider := apiName
|
||||
// If apiName is "METHOD PATH", it's not a provider name.
|
||||
if strings.Contains(apiName, " ") {
|
||||
provider = "unknown"
|
||||
}
|
||||
|
||||
for modelName, modelSnapshot := range apiSnapshot.Models {
|
||||
stats.ByModel[modelName] += modelSnapshot.TotalRequests
|
||||
|
||||
// If provider is unknown, try to infer from model name
|
||||
actualProvider := provider
|
||||
if actualProvider == "unknown" {
|
||||
actualProvider = inferProviderFromModel(modelName)
|
||||
}
|
||||
stats.ByProvider[actualProvider] += modelSnapshot.TotalRequests
|
||||
|
||||
for _, detail := range modelSnapshot.Details {
|
||||
proxyCost, directCost := estimateCostWithSavings(
|
||||
actualProvider,
|
||||
modelName,
|
||||
detail.Tokens.InputTokens,
|
||||
detail.Tokens.OutputTokens,
|
||||
detail.Tokens.CachedTokens,
|
||||
)
|
||||
totalProxyCost += proxyCost
|
||||
totalDirectCost += directCost
|
||||
stats.CostByModel[modelName] += proxyCost
|
||||
stats.CostByProvider[actualProvider] += proxyCost
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
stats.ActualCost = totalProxyCost
|
||||
stats.DirectAPICost = totalDirectCost
|
||||
stats.Savings = totalDirectCost - totalProxyCost
|
||||
if totalDirectCost > 0 {
|
||||
stats.SavingsPercent = (stats.Savings / totalDirectCost) * 100
|
||||
}
|
||||
// Keep backward compatibility
|
||||
stats.EstimatedCostSaved = totalProxyCost
|
||||
|
||||
// Last 7 days
|
||||
now := time.Now()
|
||||
for i := 6; i >= 0; i-- {
|
||||
date := now.AddDate(0, 0, -i).Format("2006-01-02")
|
||||
daily := interfaces.DailyUsage{
|
||||
Date: date,
|
||||
Requests: snapshot.RequestsByDay[date],
|
||||
Tokens: snapshot.TokensByDay[date],
|
||||
InputTokens: snapshot.InputTokensByDay[date],
|
||||
OutputTokens: snapshot.OutputTokensByDay[date],
|
||||
}
|
||||
stats.Daily = append(stats.Daily, daily)
|
||||
}
|
||||
|
||||
return stats
|
||||
}
|
||||
|
||||
func inferProviderFromModel(model string) string {
|
||||
m := strings.ToLower(model)
|
||||
if strings.Contains(m, "claude") {
|
||||
return "anthropic"
|
||||
}
|
||||
if strings.Contains(m, "gpt") {
|
||||
return "openai"
|
||||
}
|
||||
if strings.Contains(m, "gemini") {
|
||||
return "google"
|
||||
}
|
||||
return "unknown"
|
||||
}
|
||||
|
||||
func estimateCost(provider string, model string, inputTokens, outputTokens int64) float64 {
|
||||
// Try model-specific pricing first
|
||||
proxyCost, _, found := EstimateModelCost(model, inputTokens, outputTokens, 0)
|
||||
if found {
|
||||
return proxyCost
|
||||
}
|
||||
// Fallback to provider-based estimation
|
||||
return FallbackEstimateCost(provider, model, inputTokens, outputTokens)
|
||||
}
|
||||
|
||||
// estimateCostWithSavings calculates both proxy cost and direct API cost.
|
||||
// Returns (proxyCost, directCost).
|
||||
func estimateCostWithSavings(provider string, model string, inputTokens, outputTokens, cachedTokens int64) (float64, float64) {
|
||||
proxyCost, directCost, found := EstimateModelCost(model, inputTokens, outputTokens, cachedTokens)
|
||||
if found {
|
||||
return proxyCost, directCost
|
||||
}
|
||||
// Fallback - same cost for both
|
||||
fallback := FallbackEstimateCost(provider, model, inputTokens, outputTokens)
|
||||
return fallback, fallback
|
||||
}
|
||||
|
||||
@@ -0,0 +1,194 @@
|
||||
// Package usage provides usage tracking and cost estimation.
|
||||
package usage
|
||||
|
||||
import "strings"
|
||||
|
||||
// ModelPricing defines the cost per million tokens for a model.
|
||||
type ModelPricing struct {
|
||||
InputPerMillion float64 // Cost per 1M input tokens
|
||||
OutputPerMillion float64 // Cost per 1M output tokens
|
||||
CachedPerMillion float64 // Cost per 1M cached input tokens (usually discounted)
|
||||
}
|
||||
|
||||
// PricingTable maps model name patterns to their pricing.
|
||||
// Prices are in USD per million tokens (as of Dec 2024).
|
||||
var PricingTable = map[string]ModelPricing{
|
||||
// Anthropic Claude models
|
||||
"claude-opus-4": {15.00, 75.00, 1.50},
|
||||
"claude-sonnet-4": {3.00, 15.00, 0.30},
|
||||
"claude-3-5-sonnet": {3.00, 15.00, 0.30},
|
||||
"claude-3-5-haiku": {0.80, 4.00, 0.08},
|
||||
"claude-3-opus": {15.00, 75.00, 1.50},
|
||||
"claude-3-sonnet": {3.00, 15.00, 0.30},
|
||||
"claude-3-haiku": {0.25, 1.25, 0.025},
|
||||
|
||||
// OpenAI GPT models
|
||||
"gpt-4o": {2.50, 10.00, 1.25},
|
||||
"gpt-4o-mini": {0.15, 0.60, 0.075},
|
||||
"gpt-4-turbo": {10.00, 30.00, 5.00},
|
||||
"gpt-4": {30.00, 60.00, 15.00},
|
||||
"gpt-3.5-turbo": {0.50, 1.50, 0.25},
|
||||
"o1": {15.00, 60.00, 7.50},
|
||||
"o1-mini": {3.00, 12.00, 1.50},
|
||||
"o1-preview": {15.00, 60.00, 7.50},
|
||||
"o3-mini": {1.10, 4.40, 0.55},
|
||||
|
||||
// Google Gemini models
|
||||
"gemini-2.0-flash": {0.10, 0.40, 0.025},
|
||||
"gemini-1.5-pro": {1.25, 5.00, 0.3125},
|
||||
"gemini-1.5-flash": {0.075, 0.30, 0.01875},
|
||||
"gemini-1.5-flash-8b": {0.0375, 0.15, 0.01},
|
||||
"gemini-1.0-pro": {0.50, 1.50, 0.125},
|
||||
|
||||
// Qwen models (via API providers)
|
||||
"qwen-turbo": {0.30, 0.60, 0.15},
|
||||
"qwen-plus": {0.80, 2.00, 0.40},
|
||||
"qwen-max": {2.40, 9.60, 1.20},
|
||||
"qwq-32b": {0.15, 0.60, 0.075},
|
||||
|
||||
// DeepSeek models
|
||||
"deepseek-chat": {0.14, 0.28, 0.07},
|
||||
"deepseek-reasoner": {0.55, 2.19, 0.275},
|
||||
|
||||
// Codex/Copilot (estimated)
|
||||
"codex": {0.00, 0.00, 0.00}, // Free tier proxy
|
||||
"copilot": {0.00, 0.00, 0.00}, // Free tier proxy
|
||||
}
|
||||
|
||||
// DirectAPIPricing stores the "official" direct API pricing for comparison.
|
||||
// This is used to calculate savings when using ProxyPilot vs direct API.
|
||||
var DirectAPIPricing = map[string]ModelPricing{
|
||||
// Same as PricingTable - these are the direct API prices
|
||||
"claude-opus-4": {15.00, 75.00, 1.50},
|
||||
"claude-sonnet-4": {3.00, 15.00, 0.30},
|
||||
"claude-3-5-sonnet": {3.00, 15.00, 0.30},
|
||||
"claude-3-5-haiku": {0.80, 4.00, 0.08},
|
||||
"claude-3-opus": {15.00, 75.00, 1.50},
|
||||
"claude-3-sonnet": {3.00, 15.00, 0.30},
|
||||
"claude-3-haiku": {0.25, 1.25, 0.025},
|
||||
|
||||
"gpt-4o": {2.50, 10.00, 1.25},
|
||||
"gpt-4o-mini": {0.15, 0.60, 0.075},
|
||||
"gpt-4-turbo": {10.00, 30.00, 5.00},
|
||||
"gpt-4": {30.00, 60.00, 15.00},
|
||||
"gpt-3.5-turbo": {0.50, 1.50, 0.25},
|
||||
"o1": {15.00, 60.00, 7.50},
|
||||
"o1-mini": {3.00, 12.00, 1.50},
|
||||
"o1-preview": {15.00, 60.00, 7.50},
|
||||
"o3-mini": {1.10, 4.40, 0.55},
|
||||
|
||||
"gemini-2.0-flash": {0.10, 0.40, 0.025},
|
||||
"gemini-1.5-pro": {1.25, 5.00, 0.3125},
|
||||
"gemini-1.5-flash": {0.075, 0.30, 0.01875},
|
||||
"gemini-1.5-flash-8b": {0.0375, 0.15, 0.01},
|
||||
"gemini-1.0-pro": {0.50, 1.50, 0.125},
|
||||
|
||||
"qwen-turbo": {0.30, 0.60, 0.15},
|
||||
"qwen-plus": {0.80, 2.00, 0.40},
|
||||
"qwen-max": {2.40, 9.60, 1.20},
|
||||
"qwq-32b": {0.15, 0.60, 0.075},
|
||||
|
||||
"deepseek-chat": {0.14, 0.28, 0.07},
|
||||
"deepseek-reasoner": {0.55, 2.19, 0.275},
|
||||
}
|
||||
|
||||
// GetModelPricing returns the pricing for a given model.
|
||||
// It attempts exact match first, then fuzzy matching on model name patterns.
|
||||
func GetModelPricing(model string) (ModelPricing, bool) {
|
||||
m := strings.ToLower(model)
|
||||
|
||||
// Exact match first
|
||||
if pricing, ok := PricingTable[m]; ok {
|
||||
return pricing, true
|
||||
}
|
||||
|
||||
// Fuzzy match - check if model contains known pattern
|
||||
for pattern, pricing := range PricingTable {
|
||||
if strings.Contains(m, pattern) {
|
||||
return pricing, true
|
||||
}
|
||||
}
|
||||
|
||||
// Try to match by prefix
|
||||
for pattern, pricing := range PricingTable {
|
||||
if strings.HasPrefix(m, pattern) || strings.HasPrefix(pattern, m) {
|
||||
return pricing, true
|
||||
}
|
||||
}
|
||||
|
||||
return ModelPricing{}, false
|
||||
}
|
||||
|
||||
// GetDirectAPIPricing returns the direct API pricing for comparison.
|
||||
func GetDirectAPIPricing(model string) (ModelPricing, bool) {
|
||||
m := strings.ToLower(model)
|
||||
|
||||
if pricing, ok := DirectAPIPricing[m]; ok {
|
||||
return pricing, true
|
||||
}
|
||||
|
||||
for pattern, pricing := range DirectAPIPricing {
|
||||
if strings.Contains(m, pattern) {
|
||||
return pricing, true
|
||||
}
|
||||
}
|
||||
|
||||
return ModelPricing{}, false
|
||||
}
|
||||
|
||||
// CalculateCost calculates the cost for given token usage.
|
||||
func CalculateCost(pricing ModelPricing, inputTokens, outputTokens, cachedTokens int64) float64 {
|
||||
inputCost := float64(inputTokens) * pricing.InputPerMillion / 1_000_000
|
||||
outputCost := float64(outputTokens) * pricing.OutputPerMillion / 1_000_000
|
||||
cachedCost := float64(cachedTokens) * pricing.CachedPerMillion / 1_000_000
|
||||
return inputCost + outputCost + cachedCost
|
||||
}
|
||||
|
||||
// EstimateModelCost estimates the cost for a specific model and token usage.
|
||||
// Returns (proxyCost, directCost, found).
|
||||
func EstimateModelCost(model string, inputTokens, outputTokens, cachedTokens int64) (proxyCost, directCost float64, found bool) {
|
||||
pricing, ok := GetModelPricing(model)
|
||||
if !ok {
|
||||
return 0, 0, false
|
||||
}
|
||||
|
||||
directPricing, directOk := GetDirectAPIPricing(model)
|
||||
if !directOk {
|
||||
directPricing = pricing // Fallback to same pricing
|
||||
}
|
||||
|
||||
proxyCost = CalculateCost(pricing, inputTokens, outputTokens, cachedTokens)
|
||||
directCost = CalculateCost(directPricing, inputTokens, outputTokens, cachedTokens)
|
||||
|
||||
return proxyCost, directCost, true
|
||||
}
|
||||
|
||||
// FallbackEstimateCost provides a fallback cost estimation based on provider.
|
||||
// Used when model-specific pricing is not found.
|
||||
func FallbackEstimateCost(provider, model string, inputTokens, outputTokens int64) float64 {
|
||||
p := strings.ToLower(provider)
|
||||
m := strings.ToLower(model)
|
||||
|
||||
var inputRate, outputRate float64
|
||||
|
||||
if strings.Contains(p, "anthropic") || strings.Contains(p, "claude") || strings.Contains(m, "claude") {
|
||||
inputRate = 3.0 / 1_000_000
|
||||
outputRate = 15.0 / 1_000_000
|
||||
} else if strings.Contains(p, "openai") || strings.Contains(m, "gpt-4") || strings.Contains(m, "gpt-3.5") {
|
||||
inputRate = 10.0 / 1_000_000
|
||||
outputRate = 30.0 / 1_000_000
|
||||
} else if strings.Contains(p, "google") || strings.Contains(p, "gemini") || strings.Contains(m, "gemini") {
|
||||
inputRate = 0.50 / 1_000_000
|
||||
outputRate = 1.50 / 1_000_000
|
||||
} else if strings.Contains(p, "qwen") || strings.Contains(m, "qwen") {
|
||||
inputRate = 0.80 / 1_000_000
|
||||
outputRate = 2.00 / 1_000_000
|
||||
} else if strings.Contains(p, "deepseek") || strings.Contains(m, "deepseek") {
|
||||
inputRate = 0.14 / 1_000_000
|
||||
outputRate = 0.28 / 1_000_000
|
||||
} else {
|
||||
return 0
|
||||
}
|
||||
|
||||
return (float64(inputTokens) * inputRate) + (float64(outputTokens) * outputRate)
|
||||
}
|
||||
+19
-1
@@ -31,11 +31,29 @@ func SetLogLevel(cfg *config.Config) {
|
||||
}
|
||||
}
|
||||
|
||||
// DefaultAuthDir returns the default auth directory path based on the OS.
|
||||
// On Windows: %LOCALAPPDATA%\ProxyPilot\auth
|
||||
// On other OS: ~/.config/proxypilot/auth (XDG compliant)
|
||||
func DefaultAuthDir() string {
|
||||
if dir := os.Getenv("LOCALAPPDATA"); dir != "" {
|
||||
// Windows
|
||||
return filepath.Join(dir, "ProxyPilot", "auth")
|
||||
}
|
||||
if dir := os.Getenv("XDG_CONFIG_HOME"); dir != "" {
|
||||
return filepath.Join(dir, "proxypilot", "auth")
|
||||
}
|
||||
if home, err := os.UserHomeDir(); err == nil {
|
||||
return filepath.Join(home, ".config", "proxypilot", "auth")
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// ResolveAuthDir normalizes the auth directory path for consistent reuse throughout the app.
|
||||
// It expands a leading tilde (~) to the user's home directory and returns a cleaned path.
|
||||
// If authDir is empty, it returns the default auth directory.
|
||||
func ResolveAuthDir(authDir string) (string, error) {
|
||||
if authDir == "" {
|
||||
return "", nil
|
||||
return DefaultAuthDir(), nil
|
||||
}
|
||||
if strings.HasPrefix(authDir, "~") {
|
||||
home, err := os.UserHomeDir()
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
# Migrate auth files from ~/.cli-proxy-api to %LOCALAPPDATA%\ProxyPilot\auth
|
||||
|
||||
$oldDir = Join-Path $Env:USERPROFILE ".cli-proxy-api"
|
||||
$newDir = Join-Path $Env:LOCALAPPDATA "ProxyPilot\auth"
|
||||
|
||||
Write-Host "Migrating auth files..."
|
||||
Write-Host " From: $oldDir"
|
||||
Write-Host " To: $newDir"
|
||||
|
||||
# Create new directory
|
||||
if (-not (Test-Path $newDir)) {
|
||||
New-Item -ItemType Directory -Path $newDir -Force | Out-Null
|
||||
Write-Host "Created: $newDir"
|
||||
}
|
||||
|
||||
# Copy files
|
||||
$files = Get-ChildItem $oldDir -Filter "*.json" -ErrorAction SilentlyContinue
|
||||
foreach ($file in $files) {
|
||||
$dest = Join-Path $newDir $file.Name
|
||||
Copy-Item $file.FullName $dest -Force
|
||||
Write-Host " Copied: $($file.Name)"
|
||||
}
|
||||
|
||||
Write-Host "`nMigration complete! $($files.Count) files copied."
|
||||
Write-Host "New auth directory: $newDir"
|
||||
@@ -17,8 +17,7 @@ if (-not (Test-Path -LiteralPath $iexpress)) {
|
||||
throw "IExpress not found at: $iexpress. Install/enable IExpress or use the zip package script instead."
|
||||
}
|
||||
|
||||
# Build binaries
|
||||
& (Join-Path $repoRoot "scripts\\windows\\build-cliproxyapi.ps1")
|
||||
# Build single binary (tray app with embedded engine)
|
||||
& (Join-Path $repoRoot "scripts\\windows\\build-cliproxytray.ps1")
|
||||
|
||||
$distRoot = Join-Path $repoRoot "dist"
|
||||
@@ -28,13 +27,11 @@ $staging = Join-Path $distRoot "ProxyPilot-Staging"
|
||||
if (Test-Path -LiteralPath $staging) { Remove-Item -Recurse -Force -LiteralPath $staging }
|
||||
Ensure-Dir $staging
|
||||
|
||||
# Payload files
|
||||
# Payload files (single binary with embedded engine)
|
||||
$mgrExe = Join-Path $repoRoot "bin\\ProxyPilot.exe"
|
||||
$srvExe = Join-Path $repoRoot "bin\\proxypilot-engine.exe"
|
||||
$cfgSrc = Join-Path $repoRoot "config.example.yaml"
|
||||
|
||||
Copy-Item -Force -LiteralPath $mgrExe -Destination (Join-Path $staging "ProxyPilot.exe")
|
||||
Copy-Item -Force -LiteralPath $srvExe -Destination (Join-Path $staging "proxypilot-engine.exe")
|
||||
if (Test-Path -LiteralPath $cfgSrc) {
|
||||
Copy-Item -Force -LiteralPath $cfgSrc -Destination (Join-Path $staging "config.example.yaml")
|
||||
}
|
||||
@@ -82,14 +79,12 @@ SourceFiles=SourceFiles
|
||||
SourceFiles0=$escapedStaging
|
||||
[SourceFiles0]
|
||||
%FILE0%=ProxyPilot.exe
|
||||
%FILE1%=proxypilot-engine.exe
|
||||
%FILE2%=config.example.yaml
|
||||
%FILE3%=run-manager.cmd
|
||||
%FILE1%=config.example.yaml
|
||||
%FILE2%=run-manager.cmd
|
||||
[Strings]
|
||||
FILE0=ProxyPilot.exe
|
||||
FILE1=proxypilot-engine.exe
|
||||
FILE2=config.example.yaml
|
||||
FILE3=run-manager.cmd
|
||||
FILE1=config.example.yaml
|
||||
FILE2=run-manager.cmd
|
||||
"@ | Set-Content -Encoding ASCII -LiteralPath $sedPath
|
||||
|
||||
# If config.example.yaml was absent, remove it from the SED to avoid build failure.
|
||||
|
||||
@@ -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."
|
||||
@@ -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"
|
||||
|
||||
|
||||
@@ -48,6 +48,7 @@ const idempotencyKeyMetadataKey = "idempotency_key"
|
||||
const (
|
||||
defaultStreamingKeepAliveSeconds = 0
|
||||
defaultStreamingBootstrapRetries = 0
|
||||
defaultStreamingMaxChunkSize = 65536 // 64KB like vibeproxy
|
||||
)
|
||||
|
||||
// BuildErrorResponseBody builds an OpenAI-compatible JSON error response body.
|
||||
@@ -125,6 +126,19 @@ func StreamingBootstrapRetries(cfg *config.SDKConfig) int {
|
||||
return retries
|
||||
}
|
||||
|
||||
// StreamingMaxChunkSize returns the maximum size (in bytes) for individual response chunks.
|
||||
// Returns 0 when chunk size limiting is disabled. Default is 64KB.
|
||||
func StreamingMaxChunkSize(cfg *config.SDKConfig) int {
|
||||
chunkSize := defaultStreamingMaxChunkSize
|
||||
if cfg != nil && cfg.Streaming.MaxChunkSize != nil {
|
||||
chunkSize = *cfg.Streaming.MaxChunkSize
|
||||
}
|
||||
if chunkSize < 0 {
|
||||
chunkSize = 0
|
||||
}
|
||||
return chunkSize
|
||||
}
|
||||
|
||||
func requestExecutionMetadata(ctx context.Context) map[string]any {
|
||||
// Idempotency-Key is an optional client-supplied header used to correlate retries.
|
||||
// It is forwarded as execution metadata; when absent we generate a UUID.
|
||||
@@ -323,6 +337,15 @@ func (h *BaseAPIHandler) ExecuteWithAuthManager(ctx context.Context, handlerType
|
||||
if errMsg != nil {
|
||||
return nil, errMsg
|
||||
}
|
||||
|
||||
// Set model and provider in context for logging/monitoring
|
||||
if ginCtx, ok := ctx.Value("gin").(*gin.Context); ok && ginCtx != nil {
|
||||
ginCtx.Set("model", normalizedModel)
|
||||
if len(providers) > 0 {
|
||||
ginCtx.Set("provider", providers[0])
|
||||
}
|
||||
}
|
||||
|
||||
reqMeta := requestExecutionMetadata(ctx)
|
||||
req := coreexecutor.Request{
|
||||
Model: normalizedModel,
|
||||
@@ -364,6 +387,15 @@ func (h *BaseAPIHandler) ExecuteCountWithAuthManager(ctx context.Context, handle
|
||||
if errMsg != nil {
|
||||
return nil, errMsg
|
||||
}
|
||||
|
||||
// Set model and provider in context for logging/monitoring
|
||||
if ginCtx, ok := ctx.Value("gin").(*gin.Context); ok && ginCtx != nil {
|
||||
ginCtx.Set("model", normalizedModel)
|
||||
if len(providers) > 0 {
|
||||
ginCtx.Set("provider", providers[0])
|
||||
}
|
||||
}
|
||||
|
||||
reqMeta := requestExecutionMetadata(ctx)
|
||||
req := coreexecutor.Request{
|
||||
Model: normalizedModel,
|
||||
@@ -408,6 +440,15 @@ func (h *BaseAPIHandler) ExecuteStreamWithAuthManager(ctx context.Context, handl
|
||||
close(errChan)
|
||||
return nil, errChan
|
||||
}
|
||||
|
||||
// Set model and provider in context for logging/monitoring
|
||||
if ginCtx, ok := ctx.Value("gin").(*gin.Context); ok && ginCtx != nil {
|
||||
ginCtx.Set("model", normalizedModel)
|
||||
if len(providers) > 0 {
|
||||
ginCtx.Set("provider", providers[0])
|
||||
}
|
||||
}
|
||||
|
||||
reqMeta := requestExecutionMetadata(ctx)
|
||||
req := coreexecutor.Request{
|
||||
Model: normalizedModel,
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -13,19 +13,68 @@ 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/translator" // Auto-register all translators
|
||||
_ "github.com/router-for-me/CLIProxyAPI/v6/internal/usage"
|
||||
"github.com/router-for-me/CLIProxyAPI/v6/internal/watcher"
|
||||
"github.com/router-for-me/CLIProxyAPI/v6/internal/wsrelay"
|
||||
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 +510,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()
|
||||
}
|
||||
|
||||
@@ -0,0 +1,235 @@
|
||||
package translator
|
||||
|
||||
import (
|
||||
"sort"
|
||||
)
|
||||
|
||||
// GetCompatibilityMatrix returns a map of source formats to their supported target formats.
|
||||
// The keys are source format strings, and values are slices of target format strings.
|
||||
func (r *Registry) GetCompatibilityMatrix() map[string][]string {
|
||||
r.mu.RLock()
|
||||
defer r.mu.RUnlock()
|
||||
|
||||
matrix := make(map[string][]string)
|
||||
|
||||
// Collect all unique from -> to pairs from requests registry
|
||||
for from, targets := range r.requests {
|
||||
if _, exists := matrix[from.String()]; !exists {
|
||||
matrix[from.String()] = []string{}
|
||||
}
|
||||
for to := range targets {
|
||||
matrix[from.String()] = append(matrix[from.String()], to.String())
|
||||
}
|
||||
}
|
||||
|
||||
// Also include response translators that may not have request translators
|
||||
for from, targets := range r.responses {
|
||||
if _, exists := matrix[from.String()]; !exists {
|
||||
matrix[from.String()] = []string{}
|
||||
}
|
||||
for to := range targets {
|
||||
// Check if already added from requests
|
||||
found := false
|
||||
for _, existing := range matrix[from.String()] {
|
||||
if existing == to.String() {
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
matrix[from.String()] = append(matrix[from.String()], to.String())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Sort targets for consistent output
|
||||
for from := range matrix {
|
||||
sort.Strings(matrix[from])
|
||||
}
|
||||
|
||||
return matrix
|
||||
}
|
||||
|
||||
// GetSupportedFormats returns all known formats that have been registered
|
||||
// as either a source or target for translations.
|
||||
func (r *Registry) GetSupportedFormats() []Format {
|
||||
r.mu.RLock()
|
||||
defer r.mu.RUnlock()
|
||||
|
||||
formatSet := make(map[Format]struct{})
|
||||
|
||||
// Collect formats from request translators
|
||||
for from, targets := range r.requests {
|
||||
formatSet[from] = struct{}{}
|
||||
for to := range targets {
|
||||
formatSet[to] = struct{}{}
|
||||
}
|
||||
}
|
||||
|
||||
// Collect formats from response translators
|
||||
for from, targets := range r.responses {
|
||||
formatSet[from] = struct{}{}
|
||||
for to := range targets {
|
||||
formatSet[to] = struct{}{}
|
||||
}
|
||||
}
|
||||
|
||||
// Convert set to slice
|
||||
formats := make([]Format, 0, len(formatSet))
|
||||
for f := range formatSet {
|
||||
formats = append(formats, f)
|
||||
}
|
||||
|
||||
// Sort for consistent output
|
||||
sort.Slice(formats, func(i, j int) bool {
|
||||
return formats[i].String() < formats[j].String()
|
||||
})
|
||||
|
||||
return formats
|
||||
}
|
||||
|
||||
// IsTranslationSupported checks if a translation path exists between two formats.
|
||||
// It returns true if either a request or response translator is registered for the pair.
|
||||
func (r *Registry) IsTranslationSupported(from, to Format) bool {
|
||||
r.mu.RLock()
|
||||
defer r.mu.RUnlock()
|
||||
|
||||
// Check request translators
|
||||
if byTarget, ok := r.requests[from]; ok {
|
||||
if _, exists := byTarget[to]; exists {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
// Check response translators
|
||||
if byTarget, ok := r.responses[from]; ok {
|
||||
if _, exists := byTarget[to]; exists {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
// Package-level helper functions that use the default registry
|
||||
|
||||
// GetCompatibilityMatrix returns the compatibility matrix from the default registry.
|
||||
func GetCompatibilityMatrix() map[string][]string {
|
||||
return defaultRegistry.GetCompatibilityMatrix()
|
||||
}
|
||||
|
||||
// GetSupportedFormats returns all supported formats from the default registry.
|
||||
func GetSupportedFormats() []Format {
|
||||
return defaultRegistry.GetSupportedFormats()
|
||||
}
|
||||
|
||||
// IsTranslationSupported checks if a translation is supported in the default registry.
|
||||
func IsTranslationSupported(from, to Format) bool {
|
||||
return defaultRegistry.IsTranslationSupported(from, to)
|
||||
}
|
||||
|
||||
// TranslationInfo contains metadata about a translation path.
|
||||
type TranslationInfo struct {
|
||||
From Format `json:"from"`
|
||||
To Format `json:"to"`
|
||||
HasRequest bool `json:"has_request"`
|
||||
HasResponse bool `json:"has_response"`
|
||||
HasStream bool `json:"has_stream"`
|
||||
HasNonStream bool `json:"has_non_stream"`
|
||||
HasTokenCount bool `json:"has_token_count"`
|
||||
}
|
||||
|
||||
// GetTranslationInfo returns detailed information about a specific translation path.
|
||||
func (r *Registry) GetTranslationInfo(from, to Format) *TranslationInfo {
|
||||
r.mu.RLock()
|
||||
defer r.mu.RUnlock()
|
||||
|
||||
info := &TranslationInfo{
|
||||
From: from,
|
||||
To: to,
|
||||
}
|
||||
|
||||
// Check request translator
|
||||
if byTarget, ok := r.requests[from]; ok {
|
||||
if _, exists := byTarget[to]; exists {
|
||||
info.HasRequest = true
|
||||
}
|
||||
}
|
||||
|
||||
// Check response translators
|
||||
if byTarget, ok := r.responses[from]; ok {
|
||||
if resp, exists := byTarget[to]; exists {
|
||||
info.HasResponse = true
|
||||
info.HasStream = resp.Stream != nil
|
||||
info.HasNonStream = resp.NonStream != nil
|
||||
info.HasTokenCount = resp.TokenCount != nil
|
||||
}
|
||||
}
|
||||
|
||||
return info
|
||||
}
|
||||
|
||||
// GetTranslationInfo returns translation info from the default registry.
|
||||
func GetTranslationInfo(from, to Format) *TranslationInfo {
|
||||
return defaultRegistry.GetTranslationInfo(from, to)
|
||||
}
|
||||
|
||||
// GetAllTranslations returns information about all registered translation paths.
|
||||
func (r *Registry) GetAllTranslations() []TranslationInfo {
|
||||
r.mu.RLock()
|
||||
defer r.mu.RUnlock()
|
||||
|
||||
// Use a map to track unique pairs
|
||||
pairs := make(map[string]*TranslationInfo)
|
||||
|
||||
// Collect from request translators
|
||||
for from, targets := range r.requests {
|
||||
for to := range targets {
|
||||
key := from.String() + "->" + to.String()
|
||||
if _, exists := pairs[key]; !exists {
|
||||
pairs[key] = &TranslationInfo{
|
||||
From: from,
|
||||
To: to,
|
||||
}
|
||||
}
|
||||
pairs[key].HasRequest = true
|
||||
}
|
||||
}
|
||||
|
||||
// Collect from response translators
|
||||
for from, targets := range r.responses {
|
||||
for to, resp := range targets {
|
||||
key := from.String() + "->" + to.String()
|
||||
if _, exists := pairs[key]; !exists {
|
||||
pairs[key] = &TranslationInfo{
|
||||
From: from,
|
||||
To: to,
|
||||
}
|
||||
}
|
||||
pairs[key].HasResponse = true
|
||||
pairs[key].HasStream = resp.Stream != nil
|
||||
pairs[key].HasNonStream = resp.NonStream != nil
|
||||
pairs[key].HasTokenCount = resp.TokenCount != nil
|
||||
}
|
||||
}
|
||||
|
||||
// Convert to slice and sort
|
||||
result := make([]TranslationInfo, 0, len(pairs))
|
||||
for _, info := range pairs {
|
||||
result = append(result, *info)
|
||||
}
|
||||
|
||||
sort.Slice(result, func(i, j int) bool {
|
||||
if result[i].From.String() != result[j].From.String() {
|
||||
return result[i].From.String() < result[j].From.String()
|
||||
}
|
||||
return result[i].To.String() < result[j].To.String()
|
||||
})
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
// GetAllTranslations returns all translations from the default registry.
|
||||
func GetAllTranslations() []TranslationInfo {
|
||||
return defaultRegistry.GetAllTranslations()
|
||||
}
|
||||
@@ -0,0 +1,235 @@
|
||||
package translator
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestGetCompatibilityMatrix(t *testing.T) {
|
||||
// Create a test registry with known translations
|
||||
reg := NewRegistry()
|
||||
reg.Register(FormatOpenAI, FormatClaude, func(model string, data []byte, stream bool) []byte {
|
||||
return data
|
||||
}, ResponseTransform{})
|
||||
reg.Register(FormatOpenAI, FormatGemini, func(model string, data []byte, stream bool) []byte {
|
||||
return data
|
||||
}, ResponseTransform{})
|
||||
reg.Register(FormatClaude, FormatOpenAI, func(model string, data []byte, stream bool) []byte {
|
||||
return data
|
||||
}, ResponseTransform{})
|
||||
|
||||
matrix := reg.GetCompatibilityMatrix()
|
||||
|
||||
// Check OpenAI targets
|
||||
openAITargets := matrix[FormatOpenAI.String()]
|
||||
if len(openAITargets) != 2 {
|
||||
t.Errorf("OpenAI should have 2 targets, got %d", len(openAITargets))
|
||||
}
|
||||
|
||||
// Check Claude targets
|
||||
claudeTargets := matrix[FormatClaude.String()]
|
||||
if len(claudeTargets) != 1 {
|
||||
t.Errorf("Claude should have 1 target, got %d", len(claudeTargets))
|
||||
}
|
||||
|
||||
// Verify targets are sorted
|
||||
if len(openAITargets) >= 2 && openAITargets[0] > openAITargets[1] {
|
||||
t.Error("Targets should be sorted alphabetically")
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetSupportedFormats(t *testing.T) {
|
||||
reg := NewRegistry()
|
||||
reg.Register(FormatOpenAI, FormatClaude, func(model string, data []byte, stream bool) []byte {
|
||||
return data
|
||||
}, ResponseTransform{})
|
||||
reg.Register(FormatGemini, FormatOpenAI, func(model string, data []byte, stream bool) []byte {
|
||||
return data
|
||||
}, ResponseTransform{})
|
||||
|
||||
formats := reg.GetSupportedFormats()
|
||||
|
||||
// Should have 3 unique formats: OpenAI, Claude, Gemini
|
||||
if len(formats) != 3 {
|
||||
t.Errorf("Expected 3 formats, got %d", len(formats))
|
||||
}
|
||||
|
||||
// Check all expected formats are present
|
||||
formatSet := make(map[Format]bool)
|
||||
for _, f := range formats {
|
||||
formatSet[f] = true
|
||||
}
|
||||
|
||||
if !formatSet[FormatOpenAI] {
|
||||
t.Error("OpenAI format should be present")
|
||||
}
|
||||
if !formatSet[FormatClaude] {
|
||||
t.Error("Claude format should be present")
|
||||
}
|
||||
if !formatSet[FormatGemini] {
|
||||
t.Error("Gemini format should be present")
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsTranslationSupported(t *testing.T) {
|
||||
reg := NewRegistry()
|
||||
reg.Register(FormatOpenAI, FormatClaude, func(model string, data []byte, stream bool) []byte {
|
||||
return data
|
||||
}, ResponseTransform{})
|
||||
|
||||
// Registered translation should be supported
|
||||
if !reg.IsTranslationSupported(FormatOpenAI, FormatClaude) {
|
||||
t.Error("OpenAI -> Claude should be supported")
|
||||
}
|
||||
|
||||
// Non-registered translation should not be supported
|
||||
if reg.IsTranslationSupported(FormatClaude, FormatOpenAI) {
|
||||
t.Error("Claude -> OpenAI should not be supported")
|
||||
}
|
||||
|
||||
// Unknown formats should not be supported
|
||||
if reg.IsTranslationSupported("unknown", FormatOpenAI) {
|
||||
t.Error("unknown -> OpenAI should not be supported")
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetTranslationInfo(t *testing.T) {
|
||||
reg := NewRegistry()
|
||||
reg.Register(FormatOpenAI, FormatClaude, func(model string, data []byte, stream bool) []byte {
|
||||
return data
|
||||
}, ResponseTransform{
|
||||
Stream: func(ctx context.Context, model string, origReq, convReq, resp []byte, param *any) []string { return []string{string(resp)} },
|
||||
NonStream: func(ctx context.Context, model string, origReq, convReq, resp []byte, param *any) string { return string(resp) },
|
||||
TokenCount: func(ctx context.Context, count int64) string { return "" },
|
||||
})
|
||||
|
||||
info := reg.GetTranslationInfo(FormatOpenAI, FormatClaude)
|
||||
|
||||
if info.From != FormatOpenAI {
|
||||
t.Errorf("From = %v, want %v", info.From, FormatOpenAI)
|
||||
}
|
||||
if info.To != FormatClaude {
|
||||
t.Errorf("To = %v, want %v", info.To, FormatClaude)
|
||||
}
|
||||
if !info.HasRequest {
|
||||
t.Error("HasRequest should be true")
|
||||
}
|
||||
if !info.HasResponse {
|
||||
t.Error("HasResponse should be true")
|
||||
}
|
||||
if !info.HasStream {
|
||||
t.Error("HasStream should be true")
|
||||
}
|
||||
if !info.HasNonStream {
|
||||
t.Error("HasNonStream should be true")
|
||||
}
|
||||
if !info.HasTokenCount {
|
||||
t.Error("HasTokenCount should be true")
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetTranslationInfo_PartialResponse(t *testing.T) {
|
||||
reg := NewRegistry()
|
||||
reg.Register(FormatOpenAI, FormatClaude, nil, ResponseTransform{
|
||||
Stream: func(ctx context.Context, model string, origReq, convReq, resp []byte, param *any) []string { return []string{string(resp)} },
|
||||
// NonStream and TokenCount not registered
|
||||
})
|
||||
|
||||
info := reg.GetTranslationInfo(FormatOpenAI, FormatClaude)
|
||||
|
||||
if info.HasRequest {
|
||||
t.Error("HasRequest should be false")
|
||||
}
|
||||
if !info.HasResponse {
|
||||
t.Error("HasResponse should be true")
|
||||
}
|
||||
if !info.HasStream {
|
||||
t.Error("HasStream should be true")
|
||||
}
|
||||
if info.HasNonStream {
|
||||
t.Error("HasNonStream should be false")
|
||||
}
|
||||
if info.HasTokenCount {
|
||||
t.Error("HasTokenCount should be false")
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetAllTranslations(t *testing.T) {
|
||||
reg := NewRegistry()
|
||||
reg.Register(FormatOpenAI, FormatClaude, func(model string, data []byte, stream bool) []byte {
|
||||
return data
|
||||
}, ResponseTransform{})
|
||||
reg.Register(FormatClaude, FormatGemini, func(model string, data []byte, stream bool) []byte {
|
||||
return data
|
||||
}, ResponseTransform{})
|
||||
reg.Register(FormatGemini, FormatOpenAI, nil, ResponseTransform{
|
||||
NonStream: func(ctx context.Context, model string, origReq, convReq, resp []byte, param *any) string { return string(resp) },
|
||||
})
|
||||
|
||||
translations := reg.GetAllTranslations()
|
||||
|
||||
if len(translations) != 3 {
|
||||
t.Errorf("Expected 3 translations, got %d", len(translations))
|
||||
}
|
||||
|
||||
// Verify sorting (by From, then To)
|
||||
for i := 1; i < len(translations); i++ {
|
||||
prev := translations[i-1]
|
||||
curr := translations[i]
|
||||
if prev.From.String() > curr.From.String() {
|
||||
t.Error("Translations should be sorted by From format")
|
||||
}
|
||||
if prev.From == curr.From && prev.To.String() > curr.To.String() {
|
||||
t.Error("Translations with same From should be sorted by To format")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestPackageLevelAPIFunctions(t *testing.T) {
|
||||
// These just verify the package-level functions don't panic
|
||||
// and delegate to the default registry
|
||||
|
||||
matrix := GetCompatibilityMatrix()
|
||||
if matrix == nil {
|
||||
t.Error("GetCompatibilityMatrix should not return nil")
|
||||
}
|
||||
|
||||
formats := GetSupportedFormats()
|
||||
if formats == nil {
|
||||
t.Error("GetSupportedFormats should not return nil")
|
||||
}
|
||||
|
||||
// IsTranslationSupported should work
|
||||
_ = IsTranslationSupported(FormatOpenAI, FormatClaude)
|
||||
|
||||
// GetTranslationInfo should return a valid pointer
|
||||
info := GetTranslationInfo(FormatOpenAI, FormatClaude)
|
||||
if info == nil {
|
||||
t.Error("GetTranslationInfo should not return nil")
|
||||
}
|
||||
|
||||
// GetAllTranslations should work
|
||||
translations := GetAllTranslations()
|
||||
if translations == nil {
|
||||
t.Error("GetAllTranslations should not return nil")
|
||||
}
|
||||
}
|
||||
|
||||
func TestEmptyRegistry_API(t *testing.T) {
|
||||
reg := NewRegistry()
|
||||
|
||||
matrix := reg.GetCompatibilityMatrix()
|
||||
if len(matrix) != 0 {
|
||||
t.Error("Empty registry should have empty matrix")
|
||||
}
|
||||
|
||||
formats := reg.GetSupportedFormats()
|
||||
if len(formats) != 0 {
|
||||
t.Error("Empty registry should have no formats")
|
||||
}
|
||||
|
||||
translations := reg.GetAllTranslations()
|
||||
if len(translations) != 0 {
|
||||
t.Error("Empty registry should have no translations")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,341 @@
|
||||
package translator
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
)
|
||||
|
||||
// BatchRequest represents a single request in a batch translation.
|
||||
type BatchRequest struct {
|
||||
Format Format
|
||||
Model string
|
||||
Payload []byte
|
||||
Stream bool
|
||||
}
|
||||
|
||||
// BatchResult contains the result of a single batch translation.
|
||||
type BatchResult struct {
|
||||
Payload []byte `json:"payload"`
|
||||
Error error `json:"error,omitempty"`
|
||||
Duration time.Duration `json:"duration"`
|
||||
Index int `json:"index"`
|
||||
}
|
||||
|
||||
// BatchTranslator handles parallel translation of multiple requests.
|
||||
type BatchTranslator struct {
|
||||
registry *Registry
|
||||
cache *TranslationCache
|
||||
workers int
|
||||
workerPool chan struct{}
|
||||
mu sync.RWMutex
|
||||
}
|
||||
|
||||
// NewBatchTranslator creates a new batch translator.
|
||||
func NewBatchTranslator(registry *Registry, cache *TranslationCache) *BatchTranslator {
|
||||
if registry == nil {
|
||||
registry = defaultRegistry
|
||||
}
|
||||
workers := 4 // default worker count
|
||||
return &BatchTranslator{
|
||||
registry: registry,
|
||||
cache: cache,
|
||||
workers: workers,
|
||||
workerPool: make(chan struct{}, workers),
|
||||
}
|
||||
}
|
||||
|
||||
// SetBatchWorkers sets the number of concurrent workers.
|
||||
func (bt *BatchTranslator) SetBatchWorkers(n int) {
|
||||
bt.mu.Lock()
|
||||
defer bt.mu.Unlock()
|
||||
if n < 1 {
|
||||
n = 1
|
||||
}
|
||||
bt.workers = n
|
||||
bt.workerPool = make(chan struct{}, n)
|
||||
}
|
||||
|
||||
// GetWorkerCount returns the current number of workers.
|
||||
func (bt *BatchTranslator) GetWorkerCount() int {
|
||||
bt.mu.RLock()
|
||||
defer bt.mu.RUnlock()
|
||||
return bt.workers
|
||||
}
|
||||
|
||||
// TranslateBatch translates multiple requests in parallel.
|
||||
func (bt *BatchTranslator) TranslateBatch(ctx context.Context, from, to Format, requests []BatchRequest) []BatchResult {
|
||||
if len(requests) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
results := make([]BatchResult, len(requests))
|
||||
var wg sync.WaitGroup
|
||||
|
||||
bt.mu.RLock()
|
||||
workerPool := bt.workerPool
|
||||
bt.mu.RUnlock()
|
||||
|
||||
for i, req := range requests {
|
||||
wg.Add(1)
|
||||
go func(idx int, r BatchRequest) {
|
||||
defer wg.Done()
|
||||
|
||||
// Acquire worker slot
|
||||
select {
|
||||
case workerPool <- struct{}{}:
|
||||
defer func() { <-workerPool }()
|
||||
case <-ctx.Done():
|
||||
results[idx] = BatchResult{
|
||||
Index: idx,
|
||||
Error: ctx.Err(),
|
||||
Duration: 0,
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
start := time.Now()
|
||||
|
||||
// Try cache first if available
|
||||
var payload []byte
|
||||
var cached bool
|
||||
if bt.cache != nil {
|
||||
payload, cached = bt.cache.Get(from, to, r.Model, r.Payload)
|
||||
}
|
||||
|
||||
if !cached {
|
||||
payload = bt.registry.TranslateRequest(from, to, r.Model, r.Payload, r.Stream)
|
||||
if bt.cache != nil {
|
||||
bt.cache.Set(from, to, r.Model, r.Payload, payload)
|
||||
}
|
||||
}
|
||||
|
||||
results[idx] = BatchResult{
|
||||
Index: idx,
|
||||
Payload: payload,
|
||||
Duration: time.Since(start),
|
||||
}
|
||||
}(i, req)
|
||||
}
|
||||
|
||||
wg.Wait()
|
||||
return results
|
||||
}
|
||||
|
||||
// TranslateBatchWithCallback translates with per-result callback.
|
||||
func (bt *BatchTranslator) TranslateBatchWithCallback(
|
||||
ctx context.Context,
|
||||
from, to Format,
|
||||
requests []BatchRequest,
|
||||
callback func(result BatchResult),
|
||||
) {
|
||||
if len(requests) == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
var wg sync.WaitGroup
|
||||
|
||||
bt.mu.RLock()
|
||||
workerPool := bt.workerPool
|
||||
bt.mu.RUnlock()
|
||||
|
||||
for i, req := range requests {
|
||||
wg.Add(1)
|
||||
go func(idx int, r BatchRequest) {
|
||||
defer wg.Done()
|
||||
|
||||
// Acquire worker slot
|
||||
select {
|
||||
case workerPool <- struct{}{}:
|
||||
defer func() { <-workerPool }()
|
||||
case <-ctx.Done():
|
||||
if callback != nil {
|
||||
callback(BatchResult{
|
||||
Index: idx,
|
||||
Error: ctx.Err(),
|
||||
Duration: 0,
|
||||
})
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
start := time.Now()
|
||||
|
||||
var payload []byte
|
||||
var cached bool
|
||||
if bt.cache != nil {
|
||||
payload, cached = bt.cache.Get(from, to, r.Model, r.Payload)
|
||||
}
|
||||
|
||||
if !cached {
|
||||
payload = bt.registry.TranslateRequest(from, to, r.Model, r.Payload, r.Stream)
|
||||
if bt.cache != nil {
|
||||
bt.cache.Set(from, to, r.Model, r.Payload, payload)
|
||||
}
|
||||
}
|
||||
|
||||
result := BatchResult{
|
||||
Index: idx,
|
||||
Payload: payload,
|
||||
Duration: time.Since(start),
|
||||
}
|
||||
|
||||
if callback != nil {
|
||||
callback(result)
|
||||
}
|
||||
}(i, req)
|
||||
}
|
||||
|
||||
wg.Wait()
|
||||
}
|
||||
|
||||
// BatchStats contains statistics about batch processing.
|
||||
type BatchStats struct {
|
||||
TotalRequests int64 `json:"total_requests"`
|
||||
TotalDuration time.Duration `json:"total_duration"`
|
||||
AverageDuration time.Duration `json:"average_duration"`
|
||||
MinDuration time.Duration `json:"min_duration"`
|
||||
MaxDuration time.Duration `json:"max_duration"`
|
||||
Errors int `json:"errors"`
|
||||
}
|
||||
|
||||
// CalculateBatchStats computes statistics from batch results.
|
||||
func CalculateBatchStats(results []BatchResult) BatchStats {
|
||||
if len(results) == 0 {
|
||||
return BatchStats{}
|
||||
}
|
||||
|
||||
stats := BatchStats{
|
||||
TotalRequests: int64(len(results)),
|
||||
MinDuration: results[0].Duration,
|
||||
MaxDuration: results[0].Duration,
|
||||
}
|
||||
|
||||
var total time.Duration
|
||||
for _, r := range results {
|
||||
total += r.Duration
|
||||
if r.Duration < stats.MinDuration {
|
||||
stats.MinDuration = r.Duration
|
||||
}
|
||||
if r.Duration > stats.MaxDuration {
|
||||
stats.MaxDuration = r.Duration
|
||||
}
|
||||
if r.Error != nil {
|
||||
stats.Errors++
|
||||
}
|
||||
}
|
||||
|
||||
stats.TotalDuration = total
|
||||
if len(results) > 0 {
|
||||
stats.AverageDuration = total / time.Duration(len(results))
|
||||
}
|
||||
|
||||
return stats
|
||||
}
|
||||
|
||||
// defaultBatchTranslator is the package-level batch translator.
|
||||
var defaultBatchTranslator = NewBatchTranslator(nil, nil)
|
||||
var batchWorkerCount atomic.Int32
|
||||
|
||||
func init() {
|
||||
batchWorkerCount.Store(4)
|
||||
}
|
||||
|
||||
// SetBatchWorkers sets the worker count for the default batch translator.
|
||||
func SetBatchWorkers(n int) {
|
||||
batchWorkerCount.Store(int32(n))
|
||||
defaultBatchTranslator.SetBatchWorkers(n)
|
||||
}
|
||||
|
||||
// GetBatchWorkers returns the current worker count.
|
||||
func GetBatchWorkers() int {
|
||||
return int(batchWorkerCount.Load())
|
||||
}
|
||||
|
||||
// TranslateBatch translates multiple requests using the default translator.
|
||||
func TranslateBatch(ctx context.Context, from, to Format, requests []BatchRequest) []BatchResult {
|
||||
return defaultBatchTranslator.TranslateBatch(ctx, from, to, requests)
|
||||
}
|
||||
|
||||
// TranslateBatchWithCache translates multiple requests with caching.
|
||||
func TranslateBatchWithCache(ctx context.Context, from, to Format, requests []BatchRequest, cache *TranslationCache) []BatchResult {
|
||||
bt := NewBatchTranslator(defaultRegistry, cache)
|
||||
bt.SetBatchWorkers(int(batchWorkerCount.Load()))
|
||||
return bt.TranslateBatch(ctx, from, to, requests)
|
||||
}
|
||||
|
||||
// BatchProcessor provides a fluent API for batch translations.
|
||||
type BatchProcessor struct {
|
||||
translator *BatchTranslator
|
||||
from Format
|
||||
to Format
|
||||
requests []BatchRequest
|
||||
ctx context.Context
|
||||
}
|
||||
|
||||
// NewBatchProcessor creates a new batch processor.
|
||||
func NewBatchProcessor(from, to Format) *BatchProcessor {
|
||||
return &BatchProcessor{
|
||||
translator: defaultBatchTranslator,
|
||||
from: from,
|
||||
to: to,
|
||||
requests: make([]BatchRequest, 0),
|
||||
ctx: context.Background(),
|
||||
}
|
||||
}
|
||||
|
||||
// WithContext sets the context for the batch operation.
|
||||
func (bp *BatchProcessor) WithContext(ctx context.Context) *BatchProcessor {
|
||||
bp.ctx = ctx
|
||||
return bp
|
||||
}
|
||||
|
||||
// WithRegistry sets a custom registry.
|
||||
func (bp *BatchProcessor) WithRegistry(r *Registry) *BatchProcessor {
|
||||
bp.translator = NewBatchTranslator(r, nil)
|
||||
return bp
|
||||
}
|
||||
|
||||
// WithCache sets a cache for the batch operation.
|
||||
func (bp *BatchProcessor) WithCache(c *TranslationCache) *BatchProcessor {
|
||||
bp.translator = NewBatchTranslator(bp.translator.registry, c)
|
||||
return bp
|
||||
}
|
||||
|
||||
// WithWorkers sets the number of workers.
|
||||
func (bp *BatchProcessor) WithWorkers(n int) *BatchProcessor {
|
||||
bp.translator.SetBatchWorkers(n)
|
||||
return bp
|
||||
}
|
||||
|
||||
// Add adds a request to the batch.
|
||||
func (bp *BatchProcessor) Add(model string, payload []byte, stream bool) *BatchProcessor {
|
||||
bp.requests = append(bp.requests, BatchRequest{
|
||||
Format: bp.from,
|
||||
Model: model,
|
||||
Payload: payload,
|
||||
Stream: stream,
|
||||
})
|
||||
return bp
|
||||
}
|
||||
|
||||
// AddMany adds multiple payloads with the same model.
|
||||
func (bp *BatchProcessor) AddMany(model string, payloads [][]byte, stream bool) *BatchProcessor {
|
||||
for _, p := range payloads {
|
||||
bp.Add(model, p, stream)
|
||||
}
|
||||
return bp
|
||||
}
|
||||
|
||||
// Execute runs the batch translation.
|
||||
func (bp *BatchProcessor) Execute() []BatchResult {
|
||||
return bp.translator.TranslateBatch(bp.ctx, bp.from, bp.to, bp.requests)
|
||||
}
|
||||
|
||||
// ExecuteWithStats runs the batch and returns results with statistics.
|
||||
func (bp *BatchProcessor) ExecuteWithStats() ([]BatchResult, BatchStats) {
|
||||
results := bp.Execute()
|
||||
stats := CalculateBatchStats(results)
|
||||
return results, stats
|
||||
}
|
||||
@@ -0,0 +1,293 @@
|
||||
package translator
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestBatchTranslator_BasicBatch(t *testing.T) {
|
||||
reg := NewRegistry()
|
||||
|
||||
// Register a simple transformer
|
||||
from := Format("test-from")
|
||||
to := Format("test-to")
|
||||
var callCount atomic.Int32
|
||||
|
||||
reg.Register(from, to, func(model string, rawJSON []byte, stream bool) []byte {
|
||||
callCount.Add(1)
|
||||
return append([]byte("translated:"), rawJSON...)
|
||||
}, ResponseTransform{})
|
||||
|
||||
bt := NewBatchTranslator(reg, nil)
|
||||
|
||||
requests := []BatchRequest{
|
||||
{Model: "model1", Payload: []byte("payload1")},
|
||||
{Model: "model2", Payload: []byte("payload2")},
|
||||
{Model: "model3", Payload: []byte("payload3")},
|
||||
}
|
||||
|
||||
results := bt.TranslateBatch(context.Background(), from, to, requests)
|
||||
|
||||
if len(results) != 3 {
|
||||
t.Errorf("expected 3 results, got %d", len(results))
|
||||
}
|
||||
|
||||
for i, r := range results {
|
||||
if r.Error != nil {
|
||||
t.Errorf("result %d has error: %v", i, r.Error)
|
||||
}
|
||||
if r.Index != i {
|
||||
t.Errorf("result %d has wrong index: %d", i, r.Index)
|
||||
}
|
||||
// Duration can be 0 on fast machines - that's OK
|
||||
expected := "translated:payload" + string('1'+byte(i))
|
||||
if string(r.Payload) != expected {
|
||||
t.Errorf("result %d: expected %s, got %s", i, expected, string(r.Payload))
|
||||
}
|
||||
}
|
||||
|
||||
if callCount.Load() != 3 {
|
||||
t.Errorf("expected 3 translator calls, got %d", callCount.Load())
|
||||
}
|
||||
}
|
||||
|
||||
func TestBatchTranslator_WithCache(t *testing.T) {
|
||||
reg := NewRegistry()
|
||||
cache := NewTranslationCache()
|
||||
|
||||
from := Format("test-from")
|
||||
to := Format("test-to")
|
||||
var callCount atomic.Int32
|
||||
|
||||
reg.Register(from, to, func(model string, rawJSON []byte, stream bool) []byte {
|
||||
callCount.Add(1)
|
||||
return append([]byte("translated:"), rawJSON...)
|
||||
}, ResponseTransform{})
|
||||
|
||||
bt := NewBatchTranslator(reg, cache)
|
||||
|
||||
// Same payload twice
|
||||
requests := []BatchRequest{
|
||||
{Model: "model", Payload: []byte("same-payload")},
|
||||
{Model: "model", Payload: []byte("same-payload")},
|
||||
{Model: "model", Payload: []byte("different")},
|
||||
}
|
||||
|
||||
results := bt.TranslateBatch(context.Background(), from, to, requests)
|
||||
|
||||
if len(results) != 3 {
|
||||
t.Errorf("expected 3 results, got %d", len(results))
|
||||
}
|
||||
|
||||
// Due to parallel execution, both identical payloads might be processed
|
||||
// before caching kicks in. This is expected behavior.
|
||||
// Just verify all results are correct
|
||||
for _, r := range results {
|
||||
if r.Error != nil {
|
||||
t.Errorf("unexpected error: %v", r.Error)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestBatchTranslator_SetWorkers(t *testing.T) {
|
||||
bt := NewBatchTranslator(nil, nil)
|
||||
|
||||
bt.SetBatchWorkers(8)
|
||||
if bt.GetWorkerCount() != 8 {
|
||||
t.Errorf("expected 8 workers, got %d", bt.GetWorkerCount())
|
||||
}
|
||||
|
||||
bt.SetBatchWorkers(0) // Should clamp to 1
|
||||
if bt.GetWorkerCount() != 1 {
|
||||
t.Errorf("expected 1 worker (minimum), got %d", bt.GetWorkerCount())
|
||||
}
|
||||
}
|
||||
|
||||
func TestBatchTranslator_ContextCancellation(t *testing.T) {
|
||||
reg := NewRegistry()
|
||||
|
||||
from := Format("test-from")
|
||||
to := Format("test-to")
|
||||
|
||||
// Slow transformer
|
||||
reg.Register(from, to, func(model string, rawJSON []byte, stream bool) []byte {
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
return rawJSON
|
||||
}, ResponseTransform{})
|
||||
|
||||
bt := NewBatchTranslator(reg, nil)
|
||||
bt.SetBatchWorkers(1) // Force sequential processing
|
||||
|
||||
requests := make([]BatchRequest, 10)
|
||||
for i := range requests {
|
||||
requests[i] = BatchRequest{Model: "m", Payload: []byte("p")}
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 50*time.Millisecond)
|
||||
defer cancel()
|
||||
|
||||
results := bt.TranslateBatch(ctx, from, to, requests)
|
||||
|
||||
// Some results should have context errors
|
||||
hasContextError := false
|
||||
for _, r := range results {
|
||||
if r.Error == context.DeadlineExceeded {
|
||||
hasContextError = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if !hasContextError {
|
||||
t.Log("Note: context cancellation may not affect all results due to timing")
|
||||
}
|
||||
}
|
||||
|
||||
func TestBatchTranslator_EmptyBatch(t *testing.T) {
|
||||
bt := NewBatchTranslator(nil, nil)
|
||||
|
||||
results := bt.TranslateBatch(context.Background(), "from", "to", nil)
|
||||
if results != nil {
|
||||
t.Errorf("expected nil for empty batch, got %v", results)
|
||||
}
|
||||
|
||||
results = bt.TranslateBatch(context.Background(), "from", "to", []BatchRequest{})
|
||||
if results != nil {
|
||||
t.Errorf("expected nil for empty batch, got %v", results)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBatchTranslator_WithCallback(t *testing.T) {
|
||||
reg := NewRegistry()
|
||||
|
||||
from := Format("test-from")
|
||||
to := Format("test-to")
|
||||
|
||||
reg.Register(from, to, func(model string, rawJSON []byte, stream bool) []byte {
|
||||
return rawJSON
|
||||
}, ResponseTransform{})
|
||||
|
||||
bt := NewBatchTranslator(reg, nil)
|
||||
|
||||
requests := []BatchRequest{
|
||||
{Model: "m1", Payload: []byte("p1")},
|
||||
{Model: "m2", Payload: []byte("p2")},
|
||||
}
|
||||
|
||||
var callbackCount atomic.Int32
|
||||
bt.TranslateBatchWithCallback(context.Background(), from, to, requests, func(r BatchResult) {
|
||||
callbackCount.Add(1)
|
||||
})
|
||||
|
||||
if callbackCount.Load() != 2 {
|
||||
t.Errorf("expected 2 callbacks, got %d", callbackCount.Load())
|
||||
}
|
||||
}
|
||||
|
||||
func TestCalculateBatchStats(t *testing.T) {
|
||||
results := []BatchResult{
|
||||
{Duration: 10 * time.Millisecond},
|
||||
{Duration: 20 * time.Millisecond},
|
||||
{Duration: 30 * time.Millisecond, Error: context.Canceled},
|
||||
{Duration: 40 * time.Millisecond},
|
||||
{Duration: 50 * time.Millisecond},
|
||||
}
|
||||
|
||||
stats := CalculateBatchStats(results)
|
||||
|
||||
if stats.TotalRequests != 5 {
|
||||
t.Errorf("expected 5 requests, got %d", stats.TotalRequests)
|
||||
}
|
||||
if stats.MinDuration != 10*time.Millisecond {
|
||||
t.Errorf("expected min 10ms, got %v", stats.MinDuration)
|
||||
}
|
||||
if stats.MaxDuration != 50*time.Millisecond {
|
||||
t.Errorf("expected max 50ms, got %v", stats.MaxDuration)
|
||||
}
|
||||
if stats.Errors != 1 {
|
||||
t.Errorf("expected 1 error, got %d", stats.Errors)
|
||||
}
|
||||
|
||||
expectedAvg := 30 * time.Millisecond
|
||||
if stats.AverageDuration != expectedAvg {
|
||||
t.Errorf("expected avg %v, got %v", expectedAvg, stats.AverageDuration)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCalculateBatchStats_Empty(t *testing.T) {
|
||||
stats := CalculateBatchStats(nil)
|
||||
if stats.TotalRequests != 0 {
|
||||
t.Errorf("expected 0 requests for empty input, got %d", stats.TotalRequests)
|
||||
}
|
||||
|
||||
stats = CalculateBatchStats([]BatchResult{})
|
||||
if stats.TotalRequests != 0 {
|
||||
t.Errorf("expected 0 requests for empty slice, got %d", stats.TotalRequests)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBatchProcessor_FluentAPI(t *testing.T) {
|
||||
reg := NewRegistry()
|
||||
|
||||
from := Format("test-from")
|
||||
to := Format("test-to")
|
||||
|
||||
reg.Register(from, to, func(model string, rawJSON []byte, stream bool) []byte {
|
||||
return append([]byte("ok:"), rawJSON...)
|
||||
}, ResponseTransform{})
|
||||
|
||||
results, stats := NewBatchProcessor(from, to).
|
||||
WithRegistry(reg).
|
||||
WithWorkers(2).
|
||||
Add("model", []byte("data1"), false).
|
||||
Add("model", []byte("data2"), false).
|
||||
ExecuteWithStats()
|
||||
|
||||
if len(results) != 2 {
|
||||
t.Errorf("expected 2 results, got %d", len(results))
|
||||
}
|
||||
if stats.TotalRequests != 2 {
|
||||
t.Errorf("expected 2 total requests, got %d", stats.TotalRequests)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBatchProcessor_AddMany(t *testing.T) {
|
||||
from := Format("from")
|
||||
to := Format("to")
|
||||
|
||||
processor := NewBatchProcessor(from, to)
|
||||
|
||||
payloads := [][]byte{
|
||||
[]byte("p1"),
|
||||
[]byte("p2"),
|
||||
[]byte("p3"),
|
||||
}
|
||||
|
||||
processor.AddMany("model", payloads, false)
|
||||
|
||||
// Execute will use default registry which has no transformer
|
||||
// So payloads pass through unchanged
|
||||
results := processor.Execute()
|
||||
|
||||
if len(results) != 3 {
|
||||
t.Errorf("expected 3 results, got %d", len(results))
|
||||
}
|
||||
}
|
||||
|
||||
func TestPackageLevelBatchFunctions(t *testing.T) {
|
||||
// Test package-level functions
|
||||
SetBatchWorkers(4)
|
||||
if GetBatchWorkers() != 4 {
|
||||
t.Errorf("expected 4 workers, got %d", GetBatchWorkers())
|
||||
}
|
||||
|
||||
// TranslateBatch with no registered translator - should pass through
|
||||
requests := []BatchRequest{
|
||||
{Model: "m", Payload: []byte("test")},
|
||||
}
|
||||
results := TranslateBatch(context.Background(), "from", "to", requests)
|
||||
if len(results) != 1 {
|
||||
t.Errorf("expected 1 result, got %d", len(results))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,427 @@
|
||||
package translator
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"runtime"
|
||||
"sort"
|
||||
"time"
|
||||
)
|
||||
|
||||
// BenchmarkResult contains timing statistics from a benchmark run.
|
||||
type BenchmarkResult struct {
|
||||
Iterations int `json:"iterations"`
|
||||
Min time.Duration `json:"min"`
|
||||
Max time.Duration `json:"max"`
|
||||
Avg time.Duration `json:"avg"`
|
||||
P50 time.Duration `json:"p50"`
|
||||
P95 time.Duration `json:"p95"`
|
||||
P99 time.Duration `json:"p99"`
|
||||
Total time.Duration `json:"total"`
|
||||
OpsPerSec float64 `json:"ops_per_sec"`
|
||||
Errors int `json:"errors"`
|
||||
}
|
||||
|
||||
// BenchmarkConfig configures benchmark behavior.
|
||||
type BenchmarkConfig struct {
|
||||
// WarmupIterations runs before measurement starts
|
||||
WarmupIterations int
|
||||
// ForceGC runs garbage collection between iterations
|
||||
ForceGC bool
|
||||
// DisableCache disables caching during benchmark
|
||||
DisableCache bool
|
||||
// Parallel runs iterations in parallel with this many goroutines
|
||||
Parallel int
|
||||
}
|
||||
|
||||
// DefaultBenchmarkConfig returns sensible defaults.
|
||||
func DefaultBenchmarkConfig() BenchmarkConfig {
|
||||
return BenchmarkConfig{
|
||||
WarmupIterations: 10,
|
||||
ForceGC: false,
|
||||
DisableCache: true,
|
||||
Parallel: 1,
|
||||
}
|
||||
}
|
||||
|
||||
// BenchmarkTranslation measures translation performance.
|
||||
func BenchmarkTranslation(from, to Format, model string, payload []byte, iterations int) BenchmarkResult {
|
||||
return BenchmarkTranslationWithConfig(from, to, model, payload, iterations, DefaultBenchmarkConfig())
|
||||
}
|
||||
|
||||
// BenchmarkTranslationWithConfig measures translation with custom configuration.
|
||||
func BenchmarkTranslationWithConfig(from, to Format, model string, payload []byte, iterations int, config BenchmarkConfig) BenchmarkResult {
|
||||
if iterations < 1 {
|
||||
iterations = 1
|
||||
}
|
||||
|
||||
// Save and restore cache state if disabling
|
||||
if config.DisableCache {
|
||||
wasEnabled := defaultCache.IsEnabled()
|
||||
defaultCache.SetCacheEnabled(false)
|
||||
defer defaultCache.SetCacheEnabled(wasEnabled)
|
||||
}
|
||||
|
||||
// Warmup
|
||||
for i := 0; i < config.WarmupIterations; i++ {
|
||||
_ = TranslateRequest(from, to, model, payload, false)
|
||||
}
|
||||
|
||||
if config.ForceGC {
|
||||
runtime.GC()
|
||||
}
|
||||
|
||||
durations := make([]time.Duration, iterations)
|
||||
errors := 0
|
||||
|
||||
if config.Parallel > 1 {
|
||||
durations, errors = runParallelBenchmark(from, to, model, payload, iterations, config.Parallel)
|
||||
} else {
|
||||
for i := 0; i < iterations; i++ {
|
||||
start := time.Now()
|
||||
result := TranslateRequest(from, to, model, payload, false)
|
||||
durations[i] = time.Since(start)
|
||||
if result == nil {
|
||||
errors++
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return calculateBenchmarkResult(durations, errors)
|
||||
}
|
||||
|
||||
// runParallelBenchmark runs iterations across multiple goroutines.
|
||||
func runParallelBenchmark(from, to Format, model string, payload []byte, iterations, parallel int) ([]time.Duration, int) {
|
||||
durations := make([]time.Duration, iterations)
|
||||
errCount := make(chan int, parallel)
|
||||
|
||||
iterPerWorker := iterations / parallel
|
||||
remainder := iterations % parallel
|
||||
|
||||
done := make(chan struct{})
|
||||
idx := 0
|
||||
|
||||
for w := 0; w < parallel; w++ {
|
||||
count := iterPerWorker
|
||||
if w < remainder {
|
||||
count++
|
||||
}
|
||||
startIdx := idx
|
||||
idx += count
|
||||
|
||||
go func(start, cnt int) {
|
||||
localErrors := 0
|
||||
for i := 0; i < cnt; i++ {
|
||||
s := time.Now()
|
||||
result := TranslateRequest(from, to, model, payload, false)
|
||||
durations[start+i] = time.Since(s)
|
||||
if result == nil {
|
||||
localErrors++
|
||||
}
|
||||
}
|
||||
errCount <- localErrors
|
||||
done <- struct{}{}
|
||||
}(startIdx, count)
|
||||
}
|
||||
|
||||
totalErrors := 0
|
||||
for i := 0; i < parallel; i++ {
|
||||
<-done
|
||||
totalErrors += <-errCount
|
||||
}
|
||||
|
||||
return durations, totalErrors
|
||||
}
|
||||
|
||||
// calculateBenchmarkResult computes statistics from durations.
|
||||
func calculateBenchmarkResult(durations []time.Duration, errors int) BenchmarkResult {
|
||||
if len(durations) == 0 {
|
||||
return BenchmarkResult{}
|
||||
}
|
||||
|
||||
// Sort for percentile calculation
|
||||
sorted := make([]time.Duration, len(durations))
|
||||
copy(sorted, durations)
|
||||
sort.Slice(sorted, func(i, j int) bool {
|
||||
return sorted[i] < sorted[j]
|
||||
})
|
||||
|
||||
var total time.Duration
|
||||
for _, d := range durations {
|
||||
total += d
|
||||
}
|
||||
|
||||
n := len(durations)
|
||||
result := BenchmarkResult{
|
||||
Iterations: n,
|
||||
Min: sorted[0],
|
||||
Max: sorted[n-1],
|
||||
Avg: total / time.Duration(n),
|
||||
P50: sorted[n*50/100],
|
||||
P95: sorted[n*95/100],
|
||||
P99: sorted[n*99/100],
|
||||
Total: total,
|
||||
Errors: errors,
|
||||
}
|
||||
|
||||
if total > 0 {
|
||||
result.OpsPerSec = float64(n) / total.Seconds()
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
// PathKey represents a translation path for benchmarking.
|
||||
type PathKey struct {
|
||||
From Format
|
||||
To Format
|
||||
}
|
||||
|
||||
// String returns a string representation of the path.
|
||||
func (pk PathKey) String() string {
|
||||
return fmt.Sprintf("%s->%s", pk.From, pk.To)
|
||||
}
|
||||
|
||||
// BenchmarkAllPaths benchmarks all registered translation paths.
|
||||
func BenchmarkAllPaths(model string, payload []byte, iterations int) map[string]BenchmarkResult {
|
||||
return BenchmarkAllPathsWithConfig(model, payload, iterations, DefaultBenchmarkConfig())
|
||||
}
|
||||
|
||||
// BenchmarkAllPathsWithConfig benchmarks all paths with custom configuration.
|
||||
func BenchmarkAllPathsWithConfig(model string, payload []byte, iterations int, config BenchmarkConfig) map[string]BenchmarkResult {
|
||||
results := make(map[string]BenchmarkResult)
|
||||
|
||||
defaultRegistry.mu.RLock()
|
||||
paths := make([]PathKey, 0)
|
||||
for from, targets := range defaultRegistry.requests {
|
||||
for to := range targets {
|
||||
paths = append(paths, PathKey{From: from, To: to})
|
||||
}
|
||||
}
|
||||
defaultRegistry.mu.RUnlock()
|
||||
|
||||
for _, path := range paths {
|
||||
key := path.String()
|
||||
results[key] = BenchmarkTranslationWithConfig(path.From, path.To, model, payload, iterations, config)
|
||||
}
|
||||
|
||||
return results
|
||||
}
|
||||
|
||||
// BenchmarkRegistry benchmarks a specific registry instance.
|
||||
func BenchmarkRegistry(r *Registry, from, to Format, model string, payload []byte, iterations int) BenchmarkResult {
|
||||
if iterations < 1 {
|
||||
iterations = 1
|
||||
}
|
||||
|
||||
config := DefaultBenchmarkConfig()
|
||||
|
||||
// Warmup
|
||||
for i := 0; i < config.WarmupIterations; i++ {
|
||||
_ = r.TranslateRequest(from, to, model, payload, false)
|
||||
}
|
||||
|
||||
durations := make([]time.Duration, iterations)
|
||||
errors := 0
|
||||
|
||||
for i := 0; i < iterations; i++ {
|
||||
start := time.Now()
|
||||
result := r.TranslateRequest(from, to, model, payload, false)
|
||||
durations[i] = time.Since(start)
|
||||
if result == nil {
|
||||
errors++
|
||||
}
|
||||
}
|
||||
|
||||
return calculateBenchmarkResult(durations, errors)
|
||||
}
|
||||
|
||||
// CompareBenchmarks compares two benchmark results.
|
||||
type BenchmarkComparison struct {
|
||||
Name1 string `json:"name1"`
|
||||
Name2 string `json:"name2"`
|
||||
AvgDiff time.Duration `json:"avg_diff"`
|
||||
AvgDiffPct float64 `json:"avg_diff_pct"`
|
||||
P95Diff time.Duration `json:"p95_diff"`
|
||||
P95DiffPct float64 `json:"p95_diff_pct"`
|
||||
Faster string `json:"faster"`
|
||||
SpeedupRatio float64 `json:"speedup_ratio"`
|
||||
}
|
||||
|
||||
// CompareBenchmarks compares two benchmark results.
|
||||
func CompareBenchmarks(name1 string, result1 BenchmarkResult, name2 string, result2 BenchmarkResult) BenchmarkComparison {
|
||||
comp := BenchmarkComparison{
|
||||
Name1: name1,
|
||||
Name2: name2,
|
||||
AvgDiff: result1.Avg - result2.Avg,
|
||||
P95Diff: result1.P95 - result2.P95,
|
||||
}
|
||||
|
||||
if result2.Avg > 0 {
|
||||
comp.AvgDiffPct = float64(comp.AvgDiff) / float64(result2.Avg) * 100
|
||||
}
|
||||
if result2.P95 > 0 {
|
||||
comp.P95DiffPct = float64(comp.P95Diff) / float64(result2.P95) * 100
|
||||
}
|
||||
|
||||
if result1.Avg < result2.Avg {
|
||||
comp.Faster = name1
|
||||
if result1.Avg > 0 {
|
||||
comp.SpeedupRatio = float64(result2.Avg) / float64(result1.Avg)
|
||||
}
|
||||
} else {
|
||||
comp.Faster = name2
|
||||
if result2.Avg > 0 {
|
||||
comp.SpeedupRatio = float64(result1.Avg) / float64(result2.Avg)
|
||||
}
|
||||
}
|
||||
|
||||
return comp
|
||||
}
|
||||
|
||||
// BenchmarkSuite provides a comprehensive benchmarking API.
|
||||
type BenchmarkSuite struct {
|
||||
registry *Registry
|
||||
config BenchmarkConfig
|
||||
results map[string]BenchmarkResult
|
||||
}
|
||||
|
||||
// NewBenchmarkSuite creates a new benchmark suite.
|
||||
func NewBenchmarkSuite(registry *Registry) *BenchmarkSuite {
|
||||
if registry == nil {
|
||||
registry = defaultRegistry
|
||||
}
|
||||
return &BenchmarkSuite{
|
||||
registry: registry,
|
||||
config: DefaultBenchmarkConfig(),
|
||||
results: make(map[string]BenchmarkResult),
|
||||
}
|
||||
}
|
||||
|
||||
// WithConfig sets the benchmark configuration.
|
||||
func (bs *BenchmarkSuite) WithConfig(config BenchmarkConfig) *BenchmarkSuite {
|
||||
bs.config = config
|
||||
return bs
|
||||
}
|
||||
|
||||
// Run benchmarks a single translation path.
|
||||
func (bs *BenchmarkSuite) Run(name string, from, to Format, model string, payload []byte, iterations int) *BenchmarkSuite {
|
||||
result := BenchmarkTranslationWithConfig(from, to, model, payload, iterations, bs.config)
|
||||
bs.results[name] = result
|
||||
return bs
|
||||
}
|
||||
|
||||
// RunAll benchmarks all registered paths.
|
||||
func (bs *BenchmarkSuite) RunAll(model string, payload []byte, iterations int) *BenchmarkSuite {
|
||||
allResults := BenchmarkAllPathsWithConfig(model, payload, iterations, bs.config)
|
||||
for k, v := range allResults {
|
||||
bs.results[k] = v
|
||||
}
|
||||
return bs
|
||||
}
|
||||
|
||||
// GetResults returns all benchmark results.
|
||||
func (bs *BenchmarkSuite) GetResults() map[string]BenchmarkResult {
|
||||
return bs.results
|
||||
}
|
||||
|
||||
// GetResult returns a specific benchmark result.
|
||||
func (bs *BenchmarkSuite) GetResult(name string) (BenchmarkResult, bool) {
|
||||
r, ok := bs.results[name]
|
||||
return r, ok
|
||||
}
|
||||
|
||||
// Compare compares two named benchmarks in the suite.
|
||||
func (bs *BenchmarkSuite) Compare(name1, name2 string) (BenchmarkComparison, bool) {
|
||||
r1, ok1 := bs.results[name1]
|
||||
r2, ok2 := bs.results[name2]
|
||||
if !ok1 || !ok2 {
|
||||
return BenchmarkComparison{}, false
|
||||
}
|
||||
return CompareBenchmarks(name1, r1, name2, r2), true
|
||||
}
|
||||
|
||||
// Report generates a summary report of all benchmarks.
|
||||
func (bs *BenchmarkSuite) Report() []BenchmarkReportEntry {
|
||||
entries := make([]BenchmarkReportEntry, 0, len(bs.results))
|
||||
for name, result := range bs.results {
|
||||
entries = append(entries, BenchmarkReportEntry{
|
||||
Name: name,
|
||||
Result: result,
|
||||
})
|
||||
}
|
||||
sort.Slice(entries, func(i, j int) bool {
|
||||
return entries[i].Result.Avg < entries[j].Result.Avg
|
||||
})
|
||||
return entries
|
||||
}
|
||||
|
||||
// BenchmarkReportEntry is a single entry in a benchmark report.
|
||||
type BenchmarkReportEntry struct {
|
||||
Name string `json:"name"`
|
||||
Result BenchmarkResult `json:"result"`
|
||||
}
|
||||
|
||||
// BenchmarkWithContext benchmarks a translation with context timeout support.
|
||||
func BenchmarkWithContext(ctx context.Context, from, to Format, model string, payload []byte, iterations int) (BenchmarkResult, error) {
|
||||
_ = DefaultBenchmarkConfig() // Config available for future use
|
||||
|
||||
if ctx == nil {
|
||||
ctx = context.Background()
|
||||
}
|
||||
|
||||
durations := make([]time.Duration, 0, iterations)
|
||||
errors := 0
|
||||
|
||||
for i := 0; i < iterations; i++ {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
// Return partial results
|
||||
return calculateBenchmarkResult(durations, errors), ctx.Err()
|
||||
default:
|
||||
start := time.Now()
|
||||
result := TranslateRequest(from, to, model, payload, false)
|
||||
durations = append(durations, time.Since(start))
|
||||
if result == nil {
|
||||
errors++
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return calculateBenchmarkResult(durations, errors), nil
|
||||
}
|
||||
|
||||
// MemoryBenchmark measures memory allocation during translation.
|
||||
type MemoryBenchmark struct {
|
||||
AllocsBefore uint64 `json:"allocs_before"`
|
||||
AllocsAfter uint64 `json:"allocs_after"`
|
||||
AllocsDiff uint64 `json:"allocs_diff"`
|
||||
BytesBefore uint64 `json:"bytes_before"`
|
||||
BytesAfter uint64 `json:"bytes_after"`
|
||||
BytesDiff uint64 `json:"bytes_diff"`
|
||||
Duration time.Duration `json:"duration"`
|
||||
}
|
||||
|
||||
// BenchmarkMemory measures memory usage of a translation.
|
||||
func BenchmarkMemory(from, to Format, model string, payload []byte) MemoryBenchmark {
|
||||
var memBefore, memAfter runtime.MemStats
|
||||
|
||||
runtime.GC()
|
||||
runtime.ReadMemStats(&memBefore)
|
||||
|
||||
start := time.Now()
|
||||
_ = TranslateRequest(from, to, model, payload, false)
|
||||
duration := time.Since(start)
|
||||
|
||||
runtime.ReadMemStats(&memAfter)
|
||||
|
||||
return MemoryBenchmark{
|
||||
AllocsBefore: memBefore.Mallocs,
|
||||
AllocsAfter: memAfter.Mallocs,
|
||||
AllocsDiff: memAfter.Mallocs - memBefore.Mallocs,
|
||||
BytesBefore: memBefore.TotalAlloc,
|
||||
BytesAfter: memAfter.TotalAlloc,
|
||||
BytesDiff: memAfter.TotalAlloc - memBefore.TotalAlloc,
|
||||
Duration: duration,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,337 @@
|
||||
package translator
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestBenchmarkTranslation_Basic(t *testing.T) {
|
||||
reg := NewRegistry()
|
||||
|
||||
from := Format("test-from")
|
||||
to := Format("test-to")
|
||||
|
||||
reg.Register(from, to, func(model string, rawJSON []byte, stream bool) []byte {
|
||||
// Simulate some work
|
||||
time.Sleep(1 * time.Millisecond)
|
||||
return rawJSON
|
||||
}, ResponseTransform{})
|
||||
|
||||
// Use the default registry for the benchmark
|
||||
defaultRegistry.Register(from, to, func(model string, rawJSON []byte, stream bool) []byte {
|
||||
time.Sleep(1 * time.Millisecond)
|
||||
return rawJSON
|
||||
}, ResponseTransform{})
|
||||
|
||||
result := BenchmarkTranslation(from, to, "model", []byte("test payload"), 10)
|
||||
|
||||
if result.Iterations != 10 {
|
||||
t.Errorf("expected 10 iterations, got %d", result.Iterations)
|
||||
}
|
||||
if result.Min == 0 {
|
||||
t.Error("expected non-zero min duration")
|
||||
}
|
||||
if result.Max == 0 {
|
||||
t.Error("expected non-zero max duration")
|
||||
}
|
||||
if result.Avg == 0 {
|
||||
t.Error("expected non-zero avg duration")
|
||||
}
|
||||
if result.Min > result.Avg || result.Avg > result.Max {
|
||||
t.Error("invalid min/avg/max relationship")
|
||||
}
|
||||
if result.OpsPerSec == 0 {
|
||||
t.Error("expected non-zero ops/sec")
|
||||
}
|
||||
}
|
||||
|
||||
func TestBenchmarkTranslation_WithConfig(t *testing.T) {
|
||||
from := Format("test-from")
|
||||
to := Format("test-to")
|
||||
|
||||
config := BenchmarkConfig{
|
||||
WarmupIterations: 2,
|
||||
ForceGC: true,
|
||||
DisableCache: true,
|
||||
Parallel: 1,
|
||||
}
|
||||
|
||||
result := BenchmarkTranslationWithConfig(from, to, "model", []byte("test"), 5, config)
|
||||
|
||||
if result.Iterations != 5 {
|
||||
t.Errorf("expected 5 iterations, got %d", result.Iterations)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBenchmarkTranslation_Parallel(t *testing.T) {
|
||||
from := Format("test-from")
|
||||
to := Format("test-to")
|
||||
|
||||
config := BenchmarkConfig{
|
||||
WarmupIterations: 1,
|
||||
Parallel: 4,
|
||||
}
|
||||
|
||||
result := BenchmarkTranslationWithConfig(from, to, "model", []byte("test"), 20, config)
|
||||
|
||||
if result.Iterations != 20 {
|
||||
t.Errorf("expected 20 iterations, got %d", result.Iterations)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBenchmarkResult_Percentiles(t *testing.T) {
|
||||
from := Format("test-from")
|
||||
to := Format("test-to")
|
||||
|
||||
// Need enough iterations for meaningful percentiles
|
||||
result := BenchmarkTranslation(from, to, "model", []byte("test"), 100)
|
||||
|
||||
if result.P50 == 0 {
|
||||
t.Error("expected non-zero P50")
|
||||
}
|
||||
if result.P95 == 0 {
|
||||
t.Error("expected non-zero P95")
|
||||
}
|
||||
if result.P99 == 0 {
|
||||
t.Error("expected non-zero P99")
|
||||
}
|
||||
if result.P50 > result.P95 || result.P95 > result.P99 {
|
||||
t.Error("percentiles should be in ascending order")
|
||||
}
|
||||
}
|
||||
|
||||
func TestBenchmarkAllPaths(t *testing.T) {
|
||||
// Register some test translators on default registry
|
||||
from1 := Format("bench-from1")
|
||||
to1 := Format("bench-to1")
|
||||
from2 := Format("bench-from2")
|
||||
to2 := Format("bench-to2")
|
||||
|
||||
Register(from1, to1, func(model string, rawJSON []byte, stream bool) []byte {
|
||||
return rawJSON
|
||||
}, ResponseTransform{})
|
||||
|
||||
Register(from2, to2, func(model string, rawJSON []byte, stream bool) []byte {
|
||||
return rawJSON
|
||||
}, ResponseTransform{})
|
||||
|
||||
results := BenchmarkAllPaths("model", []byte("test"), 5)
|
||||
|
||||
if len(results) < 2 {
|
||||
t.Errorf("expected at least 2 paths benchmarked, got %d", len(results))
|
||||
}
|
||||
|
||||
// Check specific paths exist
|
||||
key1 := PathKey{From: from1, To: to1}.String()
|
||||
if _, ok := results[key1]; !ok {
|
||||
t.Errorf("expected results for %s", key1)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPathKey_String(t *testing.T) {
|
||||
pk := PathKey{From: "openai", To: "anthropic"}
|
||||
expected := "openai->anthropic"
|
||||
if pk.String() != expected {
|
||||
t.Errorf("expected %s, got %s", expected, pk.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestCompareBenchmarks(t *testing.T) {
|
||||
r1 := BenchmarkResult{
|
||||
Iterations: 100,
|
||||
Avg: 10 * time.Millisecond,
|
||||
P95: 15 * time.Millisecond,
|
||||
}
|
||||
|
||||
r2 := BenchmarkResult{
|
||||
Iterations: 100,
|
||||
Avg: 20 * time.Millisecond,
|
||||
P95: 30 * time.Millisecond,
|
||||
}
|
||||
|
||||
comp := CompareBenchmarks("fast", r1, "slow", r2)
|
||||
|
||||
if comp.Faster != "fast" {
|
||||
t.Errorf("expected 'fast' to be faster, got %s", comp.Faster)
|
||||
}
|
||||
if comp.SpeedupRatio != 2.0 {
|
||||
t.Errorf("expected 2x speedup, got %f", comp.SpeedupRatio)
|
||||
}
|
||||
if comp.AvgDiff != -10*time.Millisecond {
|
||||
t.Errorf("expected -10ms diff, got %v", comp.AvgDiff)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBenchmarkSuite(t *testing.T) {
|
||||
from := Format("suite-from")
|
||||
to := Format("suite-to")
|
||||
|
||||
Register(from, to, func(model string, rawJSON []byte, stream bool) []byte {
|
||||
return rawJSON
|
||||
}, ResponseTransform{})
|
||||
|
||||
suite := NewBenchmarkSuite(nil).
|
||||
WithConfig(BenchmarkConfig{
|
||||
WarmupIterations: 1,
|
||||
DisableCache: true,
|
||||
}).
|
||||
Run("test1", from, to, "model", []byte("data1"), 5).
|
||||
Run("test2", from, to, "model", []byte("data2"), 5)
|
||||
|
||||
results := suite.GetResults()
|
||||
if len(results) < 2 {
|
||||
t.Errorf("expected at least 2 results, got %d", len(results))
|
||||
}
|
||||
|
||||
r, ok := suite.GetResult("test1")
|
||||
if !ok {
|
||||
t.Error("expected to find test1 result")
|
||||
}
|
||||
if r.Iterations != 5 {
|
||||
t.Errorf("expected 5 iterations, got %d", r.Iterations)
|
||||
}
|
||||
|
||||
_, ok = suite.GetResult("nonexistent")
|
||||
if ok {
|
||||
t.Error("expected nonexistent to not be found")
|
||||
}
|
||||
}
|
||||
|
||||
func TestBenchmarkSuite_Compare(t *testing.T) {
|
||||
from := Format("compare-from")
|
||||
to := Format("compare-to")
|
||||
|
||||
suite := NewBenchmarkSuite(nil).
|
||||
Run("a", from, to, "m", []byte("d"), 5).
|
||||
Run("b", from, to, "m", []byte("d"), 5)
|
||||
|
||||
comp, ok := suite.Compare("a", "b")
|
||||
if !ok {
|
||||
t.Error("expected comparison to succeed")
|
||||
}
|
||||
if comp.Name1 != "a" || comp.Name2 != "b" {
|
||||
t.Error("comparison names don't match")
|
||||
}
|
||||
|
||||
_, ok = suite.Compare("a", "nonexistent")
|
||||
if ok {
|
||||
t.Error("expected comparison to fail for nonexistent")
|
||||
}
|
||||
}
|
||||
|
||||
func TestBenchmarkSuite_Report(t *testing.T) {
|
||||
from := Format("report-from")
|
||||
to := Format("report-to")
|
||||
|
||||
suite := NewBenchmarkSuite(nil).
|
||||
Run("slow", from, to, "m", []byte("d"), 5).
|
||||
Run("fast", from, to, "m", []byte("d"), 5)
|
||||
|
||||
report := suite.Report()
|
||||
if len(report) != 2 {
|
||||
t.Errorf("expected 2 entries, got %d", len(report))
|
||||
}
|
||||
|
||||
// Report should be sorted by avg duration
|
||||
for i := 0; i < len(report)-1; i++ {
|
||||
if report[i].Result.Avg > report[i+1].Result.Avg {
|
||||
t.Error("report should be sorted by avg duration")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestBenchmarkWithContext(t *testing.T) {
|
||||
from := Format("ctx-from")
|
||||
to := Format("ctx-to")
|
||||
|
||||
result, err := BenchmarkWithContext(context.Background(), from, to, "m", []byte("d"), 10)
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
if result.Iterations != 10 {
|
||||
t.Errorf("expected 10 iterations, got %d", result.Iterations)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBenchmarkWithContext_Timeout(t *testing.T) {
|
||||
from := Format("timeout-from")
|
||||
to := Format("timeout-to")
|
||||
|
||||
// Register a slow transformer
|
||||
defaultRegistry.Register(from, to, func(model string, rawJSON []byte, stream bool) []byte {
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
return rawJSON
|
||||
}, ResponseTransform{})
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
|
||||
defer cancel()
|
||||
|
||||
result, err := BenchmarkWithContext(ctx, from, to, "m", []byte("d"), 100)
|
||||
|
||||
// Should be cancelled before all iterations complete
|
||||
if err == nil {
|
||||
t.Log("benchmark completed before timeout (this is OK if machine is fast)")
|
||||
}
|
||||
if result.Iterations == 0 {
|
||||
t.Error("should have completed at least some iterations")
|
||||
}
|
||||
}
|
||||
|
||||
func TestBenchmarkWithContext_NilContext(t *testing.T) {
|
||||
from := Format("nil-ctx-from")
|
||||
to := Format("nil-ctx-to")
|
||||
|
||||
result, err := BenchmarkWithContext(nil, from, to, "m", []byte("d"), 5)
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
if result.Iterations != 5 {
|
||||
t.Errorf("expected 5 iterations, got %d", result.Iterations)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBenchmarkMemory(t *testing.T) {
|
||||
from := Format("mem-from")
|
||||
to := Format("mem-to")
|
||||
|
||||
result := BenchmarkMemory(from, to, "m", []byte("test data"))
|
||||
|
||||
// Duration can be 0 on very fast machines
|
||||
// Memory stats might be 0 if GC runs during measurement
|
||||
// Just check they're not negative
|
||||
if result.AllocsDiff < 0 {
|
||||
t.Error("allocs diff should not be negative")
|
||||
}
|
||||
}
|
||||
|
||||
func TestBenchmarkRegistry(t *testing.T) {
|
||||
reg := NewRegistry()
|
||||
|
||||
from := Format("reg-from")
|
||||
to := Format("reg-to")
|
||||
|
||||
reg.Register(from, to, func(model string, rawJSON []byte, stream bool) []byte {
|
||||
return rawJSON
|
||||
}, ResponseTransform{})
|
||||
|
||||
result := BenchmarkRegistry(reg, from, to, "model", []byte("test"), 10)
|
||||
|
||||
if result.Iterations != 10 {
|
||||
t.Errorf("expected 10 iterations, got %d", result.Iterations)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDefaultBenchmarkConfig(t *testing.T) {
|
||||
config := DefaultBenchmarkConfig()
|
||||
|
||||
if config.WarmupIterations != 10 {
|
||||
t.Errorf("expected 10 warmup iterations, got %d", config.WarmupIterations)
|
||||
}
|
||||
if config.Parallel != 1 {
|
||||
t.Errorf("expected 1 parallel, got %d", config.Parallel)
|
||||
}
|
||||
if !config.DisableCache {
|
||||
t.Error("expected DisableCache to be true by default")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,285 @@
|
||||
package translator
|
||||
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
)
|
||||
|
||||
// CacheStats contains statistics about cache performance.
|
||||
type CacheStats struct {
|
||||
Hits int64 `json:"hits"`
|
||||
Misses int64 `json:"misses"`
|
||||
Size int `json:"size"`
|
||||
}
|
||||
|
||||
// cacheEntry represents a cached translation result with expiration.
|
||||
type cacheEntry struct {
|
||||
value []byte
|
||||
expiresAt time.Time
|
||||
}
|
||||
|
||||
// TranslationCache provides LRU-based caching for translation results.
|
||||
type TranslationCache struct {
|
||||
mu sync.RWMutex
|
||||
enabled atomic.Bool
|
||||
maxSize int
|
||||
ttl time.Duration
|
||||
cache map[string]*cacheEntry
|
||||
order []string // LRU order: oldest first
|
||||
hits atomic.Int64
|
||||
misses atomic.Int64
|
||||
}
|
||||
|
||||
// NewTranslationCache creates a new translation cache with default settings.
|
||||
func NewTranslationCache() *TranslationCache {
|
||||
tc := &TranslationCache{
|
||||
maxSize: 1000,
|
||||
ttl: 5 * time.Minute,
|
||||
cache: make(map[string]*cacheEntry),
|
||||
order: make([]string, 0),
|
||||
}
|
||||
tc.enabled.Store(true)
|
||||
return tc
|
||||
}
|
||||
|
||||
// SetCacheEnabled enables or disables the cache.
|
||||
func (tc *TranslationCache) SetCacheEnabled(enabled bool) {
|
||||
tc.enabled.Store(enabled)
|
||||
}
|
||||
|
||||
// IsEnabled returns whether the cache is enabled.
|
||||
func (tc *TranslationCache) IsEnabled() bool {
|
||||
return tc.enabled.Load()
|
||||
}
|
||||
|
||||
// SetCacheMaxSize sets the maximum number of entries in the cache.
|
||||
func (tc *TranslationCache) SetCacheMaxSize(size int) {
|
||||
tc.mu.Lock()
|
||||
defer tc.mu.Unlock()
|
||||
tc.maxSize = size
|
||||
tc.evictExcess()
|
||||
}
|
||||
|
||||
// SetCacheTTL sets the time-to-live for cache entries.
|
||||
func (tc *TranslationCache) SetCacheTTL(ttl time.Duration) {
|
||||
tc.mu.Lock()
|
||||
defer tc.mu.Unlock()
|
||||
tc.ttl = ttl
|
||||
}
|
||||
|
||||
// GetCacheStats returns current cache statistics.
|
||||
func (tc *TranslationCache) GetCacheStats() CacheStats {
|
||||
tc.mu.RLock()
|
||||
defer tc.mu.RUnlock()
|
||||
return CacheStats{
|
||||
Hits: tc.hits.Load(),
|
||||
Misses: tc.misses.Load(),
|
||||
Size: len(tc.cache),
|
||||
}
|
||||
}
|
||||
|
||||
// Clear removes all entries from the cache and resets statistics.
|
||||
func (tc *TranslationCache) Clear() {
|
||||
tc.mu.Lock()
|
||||
defer tc.mu.Unlock()
|
||||
tc.cache = make(map[string]*cacheEntry)
|
||||
tc.order = make([]string, 0)
|
||||
tc.hits.Store(0)
|
||||
tc.misses.Store(0)
|
||||
}
|
||||
|
||||
// generateKey creates a cache key from translation parameters.
|
||||
func (tc *TranslationCache) generateKey(from, to Format, model string, payload []byte) string {
|
||||
h := sha256.New()
|
||||
h.Write([]byte(from.String()))
|
||||
h.Write([]byte(to.String()))
|
||||
h.Write([]byte(model))
|
||||
h.Write(payload)
|
||||
return hex.EncodeToString(h.Sum(nil))
|
||||
}
|
||||
|
||||
// Get retrieves a cached translation result.
|
||||
func (tc *TranslationCache) Get(from, to Format, model string, payload []byte) ([]byte, bool) {
|
||||
if !tc.enabled.Load() {
|
||||
return nil, false
|
||||
}
|
||||
|
||||
key := tc.generateKey(from, to, model, payload)
|
||||
|
||||
tc.mu.Lock()
|
||||
defer tc.mu.Unlock()
|
||||
|
||||
entry, exists := tc.cache[key]
|
||||
if !exists {
|
||||
tc.misses.Add(1)
|
||||
return nil, false
|
||||
}
|
||||
|
||||
// Check expiration
|
||||
if time.Now().After(entry.expiresAt) {
|
||||
tc.removeKey(key)
|
||||
tc.misses.Add(1)
|
||||
return nil, false
|
||||
}
|
||||
|
||||
// Move to end of LRU order (most recently used)
|
||||
tc.moveToEnd(key)
|
||||
tc.hits.Add(1)
|
||||
|
||||
// Return a copy to prevent mutation
|
||||
result := make([]byte, len(entry.value))
|
||||
copy(result, entry.value)
|
||||
return result, true
|
||||
}
|
||||
|
||||
// Set stores a translation result in the cache.
|
||||
func (tc *TranslationCache) Set(from, to Format, model string, payload, result []byte) {
|
||||
if !tc.enabled.Load() {
|
||||
return
|
||||
}
|
||||
|
||||
key := tc.generateKey(from, to, model, payload)
|
||||
|
||||
tc.mu.Lock()
|
||||
defer tc.mu.Unlock()
|
||||
|
||||
// Check if key already exists
|
||||
if _, exists := tc.cache[key]; exists {
|
||||
tc.moveToEnd(key)
|
||||
} else {
|
||||
tc.order = append(tc.order, key)
|
||||
}
|
||||
|
||||
// Store a copy to prevent mutation
|
||||
valueCopy := make([]byte, len(result))
|
||||
copy(valueCopy, result)
|
||||
|
||||
tc.cache[key] = &cacheEntry{
|
||||
value: valueCopy,
|
||||
expiresAt: time.Now().Add(tc.ttl),
|
||||
}
|
||||
|
||||
tc.evictExcess()
|
||||
}
|
||||
|
||||
// moveToEnd moves a key to the end of the LRU order.
|
||||
func (tc *TranslationCache) moveToEnd(key string) {
|
||||
for i, k := range tc.order {
|
||||
if k == key {
|
||||
tc.order = append(tc.order[:i], tc.order[i+1:]...)
|
||||
tc.order = append(tc.order, key)
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// removeKey removes a key from cache and order.
|
||||
func (tc *TranslationCache) removeKey(key string) {
|
||||
delete(tc.cache, key)
|
||||
for i, k := range tc.order {
|
||||
if k == key {
|
||||
tc.order = append(tc.order[:i], tc.order[i+1:]...)
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// evictExcess removes oldest entries if cache exceeds max size.
|
||||
func (tc *TranslationCache) evictExcess() {
|
||||
for len(tc.cache) > tc.maxSize && len(tc.order) > 0 {
|
||||
oldest := tc.order[0]
|
||||
tc.order = tc.order[1:]
|
||||
delete(tc.cache, oldest)
|
||||
}
|
||||
}
|
||||
|
||||
// evictExpired removes all expired entries.
|
||||
func (tc *TranslationCache) evictExpired() {
|
||||
tc.mu.Lock()
|
||||
defer tc.mu.Unlock()
|
||||
|
||||
now := time.Now()
|
||||
var expiredKeys []string
|
||||
for key, entry := range tc.cache {
|
||||
if now.After(entry.expiresAt) {
|
||||
expiredKeys = append(expiredKeys, key)
|
||||
}
|
||||
}
|
||||
for _, key := range expiredKeys {
|
||||
tc.removeKey(key)
|
||||
}
|
||||
}
|
||||
|
||||
// defaultCache is the package-level cache instance.
|
||||
var defaultCache = NewTranslationCache()
|
||||
|
||||
// DefaultCache returns the package-level translation cache.
|
||||
func DefaultCache() *TranslationCache {
|
||||
return defaultCache
|
||||
}
|
||||
|
||||
// SetCacheEnabled enables or disables the default cache.
|
||||
func SetCacheEnabled(enabled bool) {
|
||||
defaultCache.SetCacheEnabled(enabled)
|
||||
}
|
||||
|
||||
// SetCacheMaxSize sets the maximum size of the default cache.
|
||||
func SetCacheMaxSize(size int) {
|
||||
defaultCache.SetCacheMaxSize(size)
|
||||
}
|
||||
|
||||
// SetCacheTTL sets the TTL for the default cache.
|
||||
func SetCacheTTL(ttl time.Duration) {
|
||||
defaultCache.SetCacheTTL(ttl)
|
||||
}
|
||||
|
||||
// GetCacheStats returns statistics for the default cache.
|
||||
func GetCacheStats() CacheStats {
|
||||
return defaultCache.GetCacheStats()
|
||||
}
|
||||
|
||||
// ClearCache clears the default cache.
|
||||
func ClearCache() {
|
||||
defaultCache.Clear()
|
||||
}
|
||||
|
||||
// CachedRegistry wraps a Registry with caching support.
|
||||
type CachedRegistry struct {
|
||||
*Registry
|
||||
cache *TranslationCache
|
||||
}
|
||||
|
||||
// NewCachedRegistry creates a registry with integrated caching.
|
||||
func NewCachedRegistry(cache *TranslationCache) *CachedRegistry {
|
||||
if cache == nil {
|
||||
cache = defaultCache
|
||||
}
|
||||
return &CachedRegistry{
|
||||
Registry: NewRegistry(),
|
||||
cache: cache,
|
||||
}
|
||||
}
|
||||
|
||||
// TranslateRequest translates with caching support.
|
||||
func (cr *CachedRegistry) TranslateRequest(from, to Format, model string, rawJSON []byte, stream bool) []byte {
|
||||
// Try cache first
|
||||
if result, found := cr.cache.Get(from, to, model, rawJSON); found {
|
||||
return result
|
||||
}
|
||||
|
||||
// Perform translation
|
||||
result := cr.Registry.TranslateRequest(from, to, model, rawJSON, stream)
|
||||
|
||||
// Cache the result
|
||||
cr.cache.Set(from, to, model, rawJSON, result)
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
// GetCache returns the underlying cache.
|
||||
func (cr *CachedRegistry) GetCache() *TranslationCache {
|
||||
return cr.cache
|
||||
}
|
||||
@@ -0,0 +1,225 @@
|
||||
package translator
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestTranslationCache_BasicOperations(t *testing.T) {
|
||||
cache := NewTranslationCache()
|
||||
|
||||
from := Format("openai")
|
||||
to := Format("anthropic")
|
||||
model := "gpt-4"
|
||||
payload := []byte(`{"test": "data"}`)
|
||||
result := []byte(`{"translated": "data"}`)
|
||||
|
||||
// Test cache miss
|
||||
_, found := cache.Get(from, to, model, payload)
|
||||
if found {
|
||||
t.Error("expected cache miss on empty cache")
|
||||
}
|
||||
|
||||
stats := cache.GetCacheStats()
|
||||
if stats.Misses != 1 {
|
||||
t.Errorf("expected 1 miss, got %d", stats.Misses)
|
||||
}
|
||||
|
||||
// Test cache set and hit
|
||||
cache.Set(from, to, model, payload, result)
|
||||
|
||||
retrieved, found := cache.Get(from, to, model, payload)
|
||||
if !found {
|
||||
t.Error("expected cache hit after set")
|
||||
}
|
||||
if string(retrieved) != string(result) {
|
||||
t.Errorf("expected %s, got %s", string(result), string(retrieved))
|
||||
}
|
||||
|
||||
stats = cache.GetCacheStats()
|
||||
if stats.Hits != 1 {
|
||||
t.Errorf("expected 1 hit, got %d", stats.Hits)
|
||||
}
|
||||
if stats.Size != 1 {
|
||||
t.Errorf("expected size 1, got %d", stats.Size)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTranslationCache_TTLExpiration(t *testing.T) {
|
||||
cache := NewTranslationCache()
|
||||
cache.SetCacheTTL(50 * time.Millisecond)
|
||||
|
||||
from := Format("openai")
|
||||
to := Format("anthropic")
|
||||
model := "gpt-4"
|
||||
payload := []byte(`{"test": "data"}`)
|
||||
result := []byte(`{"translated": "data"}`)
|
||||
|
||||
cache.Set(from, to, model, payload, result)
|
||||
|
||||
// Should be found immediately
|
||||
_, found := cache.Get(from, to, model, payload)
|
||||
if !found {
|
||||
t.Error("expected cache hit before TTL expiration")
|
||||
}
|
||||
|
||||
// Wait for TTL to expire
|
||||
time.Sleep(60 * time.Millisecond)
|
||||
|
||||
// Should be expired now
|
||||
_, found = cache.Get(from, to, model, payload)
|
||||
if found {
|
||||
t.Error("expected cache miss after TTL expiration")
|
||||
}
|
||||
}
|
||||
|
||||
func TestTranslationCache_MaxSize(t *testing.T) {
|
||||
cache := NewTranslationCache()
|
||||
cache.SetCacheMaxSize(3)
|
||||
|
||||
from := Format("openai")
|
||||
to := Format("anthropic")
|
||||
model := "gpt-4"
|
||||
|
||||
// Add 5 entries - should only keep 3
|
||||
for i := 0; i < 5; i++ {
|
||||
payload := []byte{byte(i)}
|
||||
result := []byte{byte(i + 10)}
|
||||
cache.Set(from, to, model, payload, result)
|
||||
}
|
||||
|
||||
stats := cache.GetCacheStats()
|
||||
if stats.Size != 3 {
|
||||
t.Errorf("expected size 3 after eviction, got %d", stats.Size)
|
||||
}
|
||||
|
||||
// Oldest entries should be evicted (0 and 1)
|
||||
_, found := cache.Get(from, to, model, []byte{0})
|
||||
if found {
|
||||
t.Error("entry 0 should have been evicted")
|
||||
}
|
||||
|
||||
_, found = cache.Get(from, to, model, []byte{1})
|
||||
if found {
|
||||
t.Error("entry 1 should have been evicted")
|
||||
}
|
||||
|
||||
// Newer entries should still exist
|
||||
_, found = cache.Get(from, to, model, []byte{4})
|
||||
if !found {
|
||||
t.Error("entry 4 should still exist")
|
||||
}
|
||||
}
|
||||
|
||||
func TestTranslationCache_DisableEnable(t *testing.T) {
|
||||
cache := NewTranslationCache()
|
||||
|
||||
from := Format("openai")
|
||||
to := Format("anthropic")
|
||||
model := "gpt-4"
|
||||
payload := []byte(`{"test": "data"}`)
|
||||
result := []byte(`{"translated": "data"}`)
|
||||
|
||||
// Disable cache
|
||||
cache.SetCacheEnabled(false)
|
||||
|
||||
// Set should be no-op
|
||||
cache.Set(from, to, model, payload, result)
|
||||
|
||||
// Get should always miss
|
||||
_, found := cache.Get(from, to, model, payload)
|
||||
if found {
|
||||
t.Error("expected cache miss when disabled")
|
||||
}
|
||||
|
||||
// Enable and set
|
||||
cache.SetCacheEnabled(true)
|
||||
cache.Set(from, to, model, payload, result)
|
||||
|
||||
_, found = cache.Get(from, to, model, payload)
|
||||
if !found {
|
||||
t.Error("expected cache hit after re-enabling")
|
||||
}
|
||||
}
|
||||
|
||||
func TestTranslationCache_Clear(t *testing.T) {
|
||||
cache := NewTranslationCache()
|
||||
|
||||
from := Format("openai")
|
||||
to := Format("anthropic")
|
||||
model := "gpt-4"
|
||||
payload := []byte(`{"test": "data"}`)
|
||||
result := []byte(`{"translated": "data"}`)
|
||||
|
||||
cache.Set(from, to, model, payload, result)
|
||||
|
||||
stats := cache.GetCacheStats()
|
||||
if stats.Size != 1 {
|
||||
t.Errorf("expected size 1, got %d", stats.Size)
|
||||
}
|
||||
|
||||
cache.Clear()
|
||||
|
||||
stats = cache.GetCacheStats()
|
||||
if stats.Size != 0 {
|
||||
t.Errorf("expected size 0 after clear, got %d", stats.Size)
|
||||
}
|
||||
if stats.Hits != 0 || stats.Misses != 0 {
|
||||
t.Error("expected stats reset after clear")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCachedRegistry_Integration(t *testing.T) {
|
||||
cache := NewTranslationCache()
|
||||
reg := NewCachedRegistry(cache)
|
||||
|
||||
from := Format("test-from")
|
||||
to := Format("test-to")
|
||||
|
||||
// Register a transformer that modifies the payload
|
||||
reg.Register(from, to, func(model string, rawJSON []byte, stream bool) []byte {
|
||||
return []byte(`{"modified": true}`)
|
||||
}, ResponseTransform{})
|
||||
|
||||
payload := []byte(`{"original": true}`)
|
||||
|
||||
// First call should execute the transformer
|
||||
result1 := reg.TranslateRequest(from, to, "model", payload, false)
|
||||
if string(result1) != `{"modified": true}` {
|
||||
t.Errorf("unexpected result: %s", string(result1))
|
||||
}
|
||||
|
||||
stats := cache.GetCacheStats()
|
||||
if stats.Misses != 1 {
|
||||
t.Errorf("expected 1 miss, got %d", stats.Misses)
|
||||
}
|
||||
|
||||
// Second call should hit cache
|
||||
result2 := reg.TranslateRequest(from, to, "model", payload, false)
|
||||
if string(result2) != `{"modified": true}` {
|
||||
t.Errorf("unexpected result: %s", string(result2))
|
||||
}
|
||||
|
||||
stats = cache.GetCacheStats()
|
||||
if stats.Hits != 1 {
|
||||
t.Errorf("expected 1 hit, got %d", stats.Hits)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDefaultCacheFunctions(t *testing.T) {
|
||||
// Test package-level functions
|
||||
SetCacheEnabled(true)
|
||||
SetCacheMaxSize(100)
|
||||
SetCacheTTL(5 * time.Minute)
|
||||
|
||||
stats := GetCacheStats()
|
||||
if stats.Size < 0 {
|
||||
t.Error("invalid cache size")
|
||||
}
|
||||
|
||||
ClearCache()
|
||||
stats = GetCacheStats()
|
||||
if stats.Size != 0 {
|
||||
t.Errorf("expected size 0 after clear, got %d", stats.Size)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,234 @@
|
||||
package translator
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/tidwall/gjson"
|
||||
)
|
||||
|
||||
// DetectFormat attempts to detect the format of a JSON payload by examining its structure.
|
||||
// Returns empty Format if the format cannot be determined.
|
||||
func DetectFormat(payload []byte) Format {
|
||||
if len(payload) == 0 {
|
||||
return ""
|
||||
}
|
||||
|
||||
// Parse the JSON once
|
||||
result := gjson.ParseBytes(payload)
|
||||
if !result.IsObject() {
|
||||
return ""
|
||||
}
|
||||
|
||||
// Check for Antigravity format: has "request.contents" nested path
|
||||
if result.Get("request.contents").Exists() {
|
||||
return FormatAntigravity
|
||||
}
|
||||
|
||||
// Check for Gemini format: has "contents" + "generationConfig"
|
||||
if result.Get("contents").Exists() && result.Get("generationConfig").Exists() {
|
||||
return FormatGemini
|
||||
}
|
||||
|
||||
// Check for Gemini format (alternative): has "contents" array with "parts"
|
||||
if contents := result.Get("contents"); contents.Exists() && contents.IsArray() {
|
||||
if len(contents.Array()) > 0 {
|
||||
firstContent := contents.Array()[0]
|
||||
if firstContent.Get("parts").Exists() {
|
||||
return FormatGemini
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Check for OpenAI Response format: has "input" + "instructions"
|
||||
if result.Get("input").Exists() && result.Get("instructions").Exists() {
|
||||
return FormatOpenAIResponse
|
||||
}
|
||||
|
||||
// Check for messages-based formats (Claude, OpenAI)
|
||||
if result.Get("messages").Exists() && result.Get("model").Exists() {
|
||||
model := result.Get("model").String()
|
||||
modelLower := strings.ToLower(model)
|
||||
|
||||
// Check for Claude-specific patterns
|
||||
// Claude uses: max_tokens (required), system (string or array), anthropic_version
|
||||
if result.Get("anthropic_version").Exists() {
|
||||
return FormatClaude
|
||||
}
|
||||
|
||||
// Check model name patterns for Claude
|
||||
if strings.Contains(modelLower, "claude") ||
|
||||
strings.Contains(modelLower, "anthropic") {
|
||||
return FormatClaude
|
||||
}
|
||||
|
||||
// Check for Claude-specific message structure
|
||||
// Claude messages can have content as array of objects with "type" field
|
||||
messages := result.Get("messages")
|
||||
if messages.IsArray() && len(messages.Array()) > 0 {
|
||||
firstMsg := messages.Array()[0]
|
||||
content := firstMsg.Get("content")
|
||||
if content.IsArray() && len(content.Array()) > 0 {
|
||||
firstContent := content.Array()[0]
|
||||
// Claude uses "type" with values like "text", "image", "tool_use", "tool_result"
|
||||
contentType := firstContent.Get("type").String()
|
||||
if contentType == "tool_use" || contentType == "tool_result" {
|
||||
return FormatClaude
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Check for OpenAI-specific patterns
|
||||
// OpenAI uses: n, presence_penalty, frequency_penalty, logprobs, response_format
|
||||
if result.Get("n").Exists() ||
|
||||
result.Get("presence_penalty").Exists() ||
|
||||
result.Get("frequency_penalty").Exists() ||
|
||||
result.Get("logprobs").Exists() ||
|
||||
result.Get("logit_bias").Exists() {
|
||||
return FormatOpenAI
|
||||
}
|
||||
|
||||
// Check model name patterns for OpenAI
|
||||
if strings.Contains(modelLower, "gpt") ||
|
||||
strings.Contains(modelLower, "o1") ||
|
||||
strings.Contains(modelLower, "o3") ||
|
||||
strings.Contains(modelLower, "chatgpt") ||
|
||||
strings.Contains(modelLower, "text-davinci") ||
|
||||
strings.Contains(modelLower, "davinci") {
|
||||
return FormatOpenAI
|
||||
}
|
||||
|
||||
// Default to OpenAI format for messages+model structure
|
||||
// as it's the most common API format
|
||||
return FormatOpenAI
|
||||
}
|
||||
|
||||
// Check for Codex format: has "prompt" field (legacy completion API)
|
||||
if result.Get("prompt").Exists() && !result.Get("messages").Exists() {
|
||||
return FormatCodex
|
||||
}
|
||||
|
||||
return ""
|
||||
}
|
||||
|
||||
// DetectFormatWithConfidence returns the detected format along with a confidence score.
|
||||
type FormatDetection struct {
|
||||
Format Format
|
||||
Confidence float64 // 0.0 to 1.0
|
||||
Reason string
|
||||
}
|
||||
|
||||
// DetectFormatDetailed provides more detailed format detection with confidence scoring.
|
||||
func DetectFormatDetailed(payload []byte) FormatDetection {
|
||||
if len(payload) == 0 {
|
||||
return FormatDetection{Format: "", Confidence: 0, Reason: "empty payload"}
|
||||
}
|
||||
|
||||
result := gjson.ParseBytes(payload)
|
||||
if !result.IsObject() {
|
||||
return FormatDetection{Format: "", Confidence: 0, Reason: "not a JSON object"}
|
||||
}
|
||||
|
||||
// Antigravity: highest confidence due to unique nested structure
|
||||
if result.Get("request.contents").Exists() {
|
||||
return FormatDetection{
|
||||
Format: FormatAntigravity,
|
||||
Confidence: 1.0,
|
||||
Reason: "has request.contents nested path",
|
||||
}
|
||||
}
|
||||
|
||||
// Gemini with generationConfig: high confidence
|
||||
if result.Get("contents").Exists() && result.Get("generationConfig").Exists() {
|
||||
return FormatDetection{
|
||||
Format: FormatGemini,
|
||||
Confidence: 0.95,
|
||||
Reason: "has contents and generationConfig",
|
||||
}
|
||||
}
|
||||
|
||||
// OpenAI Response format
|
||||
if result.Get("input").Exists() && result.Get("instructions").Exists() {
|
||||
return FormatDetection{
|
||||
Format: FormatOpenAIResponse,
|
||||
Confidence: 0.95,
|
||||
Reason: "has input and instructions fields",
|
||||
}
|
||||
}
|
||||
|
||||
// Claude with anthropic_version
|
||||
if result.Get("messages").Exists() && result.Get("anthropic_version").Exists() {
|
||||
return FormatDetection{
|
||||
Format: FormatClaude,
|
||||
Confidence: 1.0,
|
||||
Reason: "has messages and anthropic_version",
|
||||
}
|
||||
}
|
||||
|
||||
// Messages-based detection
|
||||
if result.Get("messages").Exists() && result.Get("model").Exists() {
|
||||
model := result.Get("model").String()
|
||||
modelLower := strings.ToLower(model)
|
||||
|
||||
if strings.Contains(modelLower, "claude") {
|
||||
return FormatDetection{
|
||||
Format: FormatClaude,
|
||||
Confidence: 0.9,
|
||||
Reason: "model name contains 'claude'",
|
||||
}
|
||||
}
|
||||
|
||||
if strings.Contains(modelLower, "gpt") || strings.Contains(modelLower, "o1") {
|
||||
return FormatDetection{
|
||||
Format: FormatOpenAI,
|
||||
Confidence: 0.9,
|
||||
Reason: "model name matches OpenAI pattern",
|
||||
}
|
||||
}
|
||||
|
||||
// OpenAI-specific parameters
|
||||
if result.Get("n").Exists() || result.Get("presence_penalty").Exists() {
|
||||
return FormatDetection{
|
||||
Format: FormatOpenAI,
|
||||
Confidence: 0.8,
|
||||
Reason: "has OpenAI-specific parameters",
|
||||
}
|
||||
}
|
||||
|
||||
// Default to OpenAI with lower confidence
|
||||
return FormatDetection{
|
||||
Format: FormatOpenAI,
|
||||
Confidence: 0.5,
|
||||
Reason: "has messages and model (defaulting to OpenAI)",
|
||||
}
|
||||
}
|
||||
|
||||
// Gemini with just contents
|
||||
if result.Get("contents").Exists() {
|
||||
return FormatDetection{
|
||||
Format: FormatGemini,
|
||||
Confidence: 0.7,
|
||||
Reason: "has contents field",
|
||||
}
|
||||
}
|
||||
|
||||
return FormatDetection{Format: "", Confidence: 0, Reason: "no matching format patterns"}
|
||||
}
|
||||
|
||||
// MustDetectFormat detects format or panics if detection fails.
|
||||
func MustDetectFormat(payload []byte) Format {
|
||||
f := DetectFormat(payload)
|
||||
if f == "" {
|
||||
panic("translator: unable to detect format from payload")
|
||||
}
|
||||
return f
|
||||
}
|
||||
|
||||
// IsKnownFormat checks if the given format is a known/registered format.
|
||||
func IsKnownFormat(f Format) bool {
|
||||
switch f {
|
||||
case FormatOpenAI, FormatOpenAIResponse, FormatClaude, FormatGemini, FormatGeminiCLI, FormatCodex, FormatAntigravity, FormatKiro:
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,314 @@
|
||||
package translator
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestDetectFormat_OpenAI(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
payload []byte
|
||||
expected Format
|
||||
}{
|
||||
{
|
||||
name: "OpenAI with GPT model",
|
||||
payload: []byte(`{
|
||||
"model": "gpt-4",
|
||||
"messages": [{"role": "user", "content": "Hello"}]
|
||||
}`),
|
||||
expected: FormatOpenAI,
|
||||
},
|
||||
{
|
||||
name: "OpenAI with o1 model",
|
||||
payload: []byte(`{
|
||||
"model": "o1-preview",
|
||||
"messages": [{"role": "user", "content": "Hello"}]
|
||||
}`),
|
||||
expected: FormatOpenAI,
|
||||
},
|
||||
{
|
||||
name: "OpenAI with presence_penalty",
|
||||
payload: []byte(`{
|
||||
"model": "custom-model",
|
||||
"messages": [{"role": "user", "content": "Hello"}],
|
||||
"presence_penalty": 0.5
|
||||
}`),
|
||||
expected: FormatOpenAI,
|
||||
},
|
||||
{
|
||||
name: "OpenAI with n parameter",
|
||||
payload: []byte(`{
|
||||
"model": "custom-model",
|
||||
"messages": [{"role": "user", "content": "Hello"}],
|
||||
"n": 2
|
||||
}`),
|
||||
expected: FormatOpenAI,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
result := DetectFormat(tt.payload)
|
||||
if result != tt.expected {
|
||||
t.Errorf("DetectFormat() = %v, want %v", result, tt.expected)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestDetectFormat_Claude(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
payload []byte
|
||||
expected Format
|
||||
}{
|
||||
{
|
||||
name: "Claude with anthropic_version",
|
||||
payload: []byte(`{
|
||||
"model": "claude-3",
|
||||
"messages": [{"role": "user", "content": "Hello"}],
|
||||
"anthropic_version": "2023-06-01"
|
||||
}`),
|
||||
expected: FormatClaude,
|
||||
},
|
||||
{
|
||||
name: "Claude model name",
|
||||
payload: []byte(`{
|
||||
"model": "claude-3-opus",
|
||||
"messages": [{"role": "user", "content": "Hello"}]
|
||||
}`),
|
||||
expected: FormatClaude,
|
||||
},
|
||||
{
|
||||
name: "Claude with tool_use content",
|
||||
payload: []byte(`{
|
||||
"model": "some-model",
|
||||
"messages": [{"role": "assistant", "content": [{"type": "tool_use", "id": "123"}]}]
|
||||
}`),
|
||||
expected: FormatClaude,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
result := DetectFormat(tt.payload)
|
||||
if result != tt.expected {
|
||||
t.Errorf("DetectFormat() = %v, want %v", result, tt.expected)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestDetectFormat_Gemini(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
payload []byte
|
||||
expected Format
|
||||
}{
|
||||
{
|
||||
name: "Gemini with contents and generationConfig",
|
||||
payload: []byte(`{
|
||||
"contents": [{"parts": [{"text": "Hello"}]}],
|
||||
"generationConfig": {"temperature": 0.7}
|
||||
}`),
|
||||
expected: FormatGemini,
|
||||
},
|
||||
{
|
||||
name: "Gemini with contents and parts",
|
||||
payload: []byte(`{
|
||||
"contents": [{"parts": [{"text": "Hello"}], "role": "user"}]
|
||||
}`),
|
||||
expected: FormatGemini,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
result := DetectFormat(tt.payload)
|
||||
if result != tt.expected {
|
||||
t.Errorf("DetectFormat() = %v, want %v", result, tt.expected)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestDetectFormat_Antigravity(t *testing.T) {
|
||||
payload := []byte(`{
|
||||
"request": {
|
||||
"contents": [{"parts": [{"text": "Hello"}]}]
|
||||
}
|
||||
}`)
|
||||
|
||||
result := DetectFormat(payload)
|
||||
if result != FormatAntigravity {
|
||||
t.Errorf("DetectFormat() = %v, want %v", result, FormatAntigravity)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDetectFormat_OpenAIResponse(t *testing.T) {
|
||||
payload := []byte(`{
|
||||
"input": "Hello",
|
||||
"instructions": "Be helpful"
|
||||
}`)
|
||||
|
||||
result := DetectFormat(payload)
|
||||
if result != FormatOpenAIResponse {
|
||||
t.Errorf("DetectFormat() = %v, want %v", result, FormatOpenAIResponse)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDetectFormat_Empty(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
payload []byte
|
||||
}{
|
||||
{"nil payload", nil},
|
||||
{"empty payload", []byte{}},
|
||||
{"non-object JSON", []byte(`["array"]`)},
|
||||
{"invalid JSON", []byte(`{invalid}`)},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
result := DetectFormat(tt.payload)
|
||||
if result != "" {
|
||||
t.Errorf("DetectFormat() = %v, want empty string", result)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestDetectFormatDetailed_Confidence(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
payload []byte
|
||||
expectedFormat Format
|
||||
minConfidence float64
|
||||
}{
|
||||
{
|
||||
name: "Antigravity - highest confidence",
|
||||
payload: []byte(`{
|
||||
"request": {"contents": []}
|
||||
}`),
|
||||
expectedFormat: FormatAntigravity,
|
||||
minConfidence: 1.0,
|
||||
},
|
||||
{
|
||||
name: "Claude with anthropic_version - high confidence",
|
||||
payload: []byte(`{
|
||||
"model": "claude",
|
||||
"messages": [],
|
||||
"anthropic_version": "2023-06-01"
|
||||
}`),
|
||||
expectedFormat: FormatClaude,
|
||||
minConfidence: 1.0,
|
||||
},
|
||||
{
|
||||
name: "Gemini with generationConfig - high confidence",
|
||||
payload: []byte(`{
|
||||
"contents": [],
|
||||
"generationConfig": {}
|
||||
}`),
|
||||
expectedFormat: FormatGemini,
|
||||
minConfidence: 0.9,
|
||||
},
|
||||
{
|
||||
name: "Generic messages+model - lower confidence",
|
||||
payload: []byte(`{
|
||||
"model": "unknown-model",
|
||||
"messages": []
|
||||
}`),
|
||||
expectedFormat: FormatOpenAI,
|
||||
minConfidence: 0.5,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
result := DetectFormatDetailed(tt.payload)
|
||||
if result.Format != tt.expectedFormat {
|
||||
t.Errorf("Format = %v, want %v", result.Format, tt.expectedFormat)
|
||||
}
|
||||
if result.Confidence < tt.minConfidence {
|
||||
t.Errorf("Confidence = %v, want >= %v", result.Confidence, tt.minConfidence)
|
||||
}
|
||||
if result.Reason == "" {
|
||||
t.Error("Reason should not be empty")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestDetectFormatDetailed_EmptyPayload(t *testing.T) {
|
||||
result := DetectFormatDetailed([]byte{})
|
||||
if result.Format != "" {
|
||||
t.Errorf("Format = %v, want empty", result.Format)
|
||||
}
|
||||
if result.Confidence != 0 {
|
||||
t.Errorf("Confidence = %v, want 0", result.Confidence)
|
||||
}
|
||||
if result.Reason != "empty payload" {
|
||||
t.Errorf("Reason = %v, want 'empty payload'", result.Reason)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDetectFormatDetailed_NonObject(t *testing.T) {
|
||||
result := DetectFormatDetailed([]byte(`["array"]`))
|
||||
if result.Format != "" {
|
||||
t.Errorf("Format = %v, want empty", result.Format)
|
||||
}
|
||||
if result.Reason != "not a JSON object" {
|
||||
t.Errorf("Reason = %v, want 'not a JSON object'", result.Reason)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMustDetectFormat_Success(t *testing.T) {
|
||||
payload := []byte(`{"model": "gpt-4", "messages": []}`)
|
||||
|
||||
// Should not panic
|
||||
result := MustDetectFormat(payload)
|
||||
if result != FormatOpenAI {
|
||||
t.Errorf("MustDetectFormat() = %v, want %v", result, FormatOpenAI)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMustDetectFormat_Panic(t *testing.T) {
|
||||
defer func() {
|
||||
if r := recover(); r == nil {
|
||||
t.Error("MustDetectFormat should panic on undetectable format")
|
||||
}
|
||||
}()
|
||||
|
||||
MustDetectFormat([]byte(`{"unknown": "structure"}`))
|
||||
}
|
||||
|
||||
func TestIsKnownFormat(t *testing.T) {
|
||||
knownFormats := []Format{
|
||||
FormatOpenAI,
|
||||
FormatOpenAIResponse,
|
||||
FormatClaude,
|
||||
FormatGemini,
|
||||
FormatGeminiCLI,
|
||||
FormatCodex,
|
||||
FormatAntigravity,
|
||||
FormatKiro,
|
||||
}
|
||||
|
||||
for _, f := range knownFormats {
|
||||
if !IsKnownFormat(f) {
|
||||
t.Errorf("IsKnownFormat(%v) = false, want true", f)
|
||||
}
|
||||
}
|
||||
|
||||
unknownFormats := []Format{
|
||||
"unknown",
|
||||
"custom-format",
|
||||
"",
|
||||
}
|
||||
|
||||
for _, f := range unknownFormats {
|
||||
if IsKnownFormat(f) {
|
||||
t.Errorf("IsKnownFormat(%v) = true, want false", f)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,180 @@
|
||||
package translator
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sort"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// GenerateMarkdownDocs generates a markdown documentation string for all registered translations.
|
||||
// The output includes a table showing all translation paths and their capabilities.
|
||||
func (r *Registry) GenerateMarkdownDocs() string {
|
||||
translations := r.GetAllTranslations()
|
||||
if len(translations) == 0 {
|
||||
return "# Translation Registry\n\nNo translations registered.\n"
|
||||
}
|
||||
|
||||
var sb strings.Builder
|
||||
|
||||
sb.WriteString("# Translation Registry\n\n")
|
||||
sb.WriteString("## Supported Translations\n\n")
|
||||
sb.WriteString("| From | To | Registered |\n")
|
||||
sb.WriteString("|------|-----|------------|\n")
|
||||
|
||||
for _, t := range translations {
|
||||
registered := ""
|
||||
if t.HasRequest || t.HasResponse {
|
||||
registered = "yes"
|
||||
}
|
||||
sb.WriteString(fmt.Sprintf("| %s | %s | %s |\n", t.From, t.To, registered))
|
||||
}
|
||||
|
||||
sb.WriteString("\n## Detailed Capabilities\n\n")
|
||||
sb.WriteString("| From | To | Request | Stream | Non-Stream | Token Count |\n")
|
||||
sb.WriteString("|------|-----|---------|--------|------------|-------------|\n")
|
||||
|
||||
for _, t := range translations {
|
||||
request := boolToCheck(t.HasRequest)
|
||||
stream := boolToCheck(t.HasStream)
|
||||
nonStream := boolToCheck(t.HasNonStream)
|
||||
tokenCount := boolToCheck(t.HasTokenCount)
|
||||
sb.WriteString(fmt.Sprintf("| %s | %s | %s | %s | %s | %s |\n",
|
||||
t.From, t.To, request, stream, nonStream, tokenCount))
|
||||
}
|
||||
|
||||
sb.WriteString("\n## Supported Formats\n\n")
|
||||
formats := r.GetSupportedFormats()
|
||||
for _, f := range formats {
|
||||
sb.WriteString(fmt.Sprintf("- `%s`\n", f))
|
||||
}
|
||||
|
||||
return sb.String()
|
||||
}
|
||||
|
||||
// GenerateMermaidDiagram generates a Mermaid flowchart diagram showing translation paths.
|
||||
// The output can be rendered by Mermaid-compatible markdown viewers.
|
||||
func (r *Registry) GenerateMermaidDiagram() string {
|
||||
translations := r.GetAllTranslations()
|
||||
if len(translations) == 0 {
|
||||
return "```mermaid\nflowchart LR\n NoTranslations[\"No translations registered\"]\n```\n"
|
||||
}
|
||||
|
||||
var sb strings.Builder
|
||||
sb.WriteString("```mermaid\n")
|
||||
sb.WriteString("flowchart LR\n")
|
||||
|
||||
// Collect unique formats for node definitions
|
||||
formatSet := make(map[Format]struct{})
|
||||
for _, t := range translations {
|
||||
formatSet[t.From] = struct{}{}
|
||||
formatSet[t.To] = struct{}{}
|
||||
}
|
||||
|
||||
// Sort formats for consistent output
|
||||
formats := make([]Format, 0, len(formatSet))
|
||||
for f := range formatSet {
|
||||
formats = append(formats, f)
|
||||
}
|
||||
sort.Slice(formats, func(i, j int) bool {
|
||||
return formats[i].String() < formats[j].String()
|
||||
})
|
||||
|
||||
// Define nodes with sanitized IDs
|
||||
for _, f := range formats {
|
||||
nodeID := sanitizeMermaidID(f.String())
|
||||
sb.WriteString(fmt.Sprintf(" %s[\"%s\"]\n", nodeID, f))
|
||||
}
|
||||
|
||||
sb.WriteString("\n")
|
||||
|
||||
// Define edges
|
||||
for _, t := range translations {
|
||||
fromID := sanitizeMermaidID(t.From.String())
|
||||
toID := sanitizeMermaidID(t.To.String())
|
||||
|
||||
// Add edge label based on capabilities
|
||||
var labels []string
|
||||
if t.HasRequest {
|
||||
labels = append(labels, "req")
|
||||
}
|
||||
if t.HasStream {
|
||||
labels = append(labels, "stream")
|
||||
}
|
||||
if t.HasNonStream {
|
||||
labels = append(labels, "non-stream")
|
||||
}
|
||||
|
||||
if len(labels) > 0 {
|
||||
sb.WriteString(fmt.Sprintf(" %s -->|%s| %s\n", fromID, strings.Join(labels, ", "), toID))
|
||||
} else {
|
||||
sb.WriteString(fmt.Sprintf(" %s --> %s\n", fromID, toID))
|
||||
}
|
||||
}
|
||||
|
||||
sb.WriteString("```\n")
|
||||
|
||||
return sb.String()
|
||||
}
|
||||
|
||||
// GenerateMarkdownDocs generates markdown docs from the default registry.
|
||||
func GenerateMarkdownDocs() string {
|
||||
return defaultRegistry.GenerateMarkdownDocs()
|
||||
}
|
||||
|
||||
// GenerateMermaidDiagram generates a mermaid diagram from the default registry.
|
||||
func GenerateMermaidDiagram() string {
|
||||
return defaultRegistry.GenerateMermaidDiagram()
|
||||
}
|
||||
|
||||
// boolToCheck converts a boolean to a checkmark or empty string.
|
||||
func boolToCheck(b bool) string {
|
||||
if b {
|
||||
return "yes"
|
||||
}
|
||||
return "-"
|
||||
}
|
||||
|
||||
// sanitizeMermaidID converts a format string into a valid Mermaid node ID.
|
||||
// Mermaid node IDs must be alphanumeric with underscores.
|
||||
func sanitizeMermaidID(s string) string {
|
||||
result := strings.Builder{}
|
||||
for _, r := range s {
|
||||
if (r >= 'a' && r <= 'z') || (r >= 'A' && r <= 'Z') || (r >= '0' && r <= '9') || r == '_' {
|
||||
result.WriteRune(r)
|
||||
} else {
|
||||
result.WriteRune('_')
|
||||
}
|
||||
}
|
||||
return result.String()
|
||||
}
|
||||
|
||||
// GenerateTranslationSummary returns a concise summary of translation capabilities.
|
||||
func (r *Registry) GenerateTranslationSummary() string {
|
||||
matrix := r.GetCompatibilityMatrix()
|
||||
formats := r.GetSupportedFormats()
|
||||
|
||||
var sb strings.Builder
|
||||
sb.WriteString(fmt.Sprintf("Translation Registry Summary\n"))
|
||||
sb.WriteString(fmt.Sprintf("============================\n\n"))
|
||||
sb.WriteString(fmt.Sprintf("Total Formats: %d\n", len(formats)))
|
||||
|
||||
totalPaths := 0
|
||||
for _, targets := range matrix {
|
||||
totalPaths += len(targets)
|
||||
}
|
||||
sb.WriteString(fmt.Sprintf("Total Translation Paths: %d\n\n", totalPaths))
|
||||
|
||||
sb.WriteString("Registered Paths:\n")
|
||||
for from, targets := range matrix {
|
||||
if len(targets) > 0 {
|
||||
sb.WriteString(fmt.Sprintf(" %s -> %s\n", from, strings.Join(targets, ", ")))
|
||||
}
|
||||
}
|
||||
|
||||
return sb.String()
|
||||
}
|
||||
|
||||
// GenerateTranslationSummary returns a summary from the default registry.
|
||||
func GenerateTranslationSummary() string {
|
||||
return defaultRegistry.GenerateTranslationSummary()
|
||||
}
|
||||
@@ -0,0 +1,226 @@
|
||||
package translator
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestGenerateMarkdownDocs_EmptyRegistry(t *testing.T) {
|
||||
reg := NewRegistry()
|
||||
docs := reg.GenerateMarkdownDocs()
|
||||
|
||||
if !strings.Contains(docs, "# Translation Registry") {
|
||||
t.Error("Docs should contain title")
|
||||
}
|
||||
if !strings.Contains(docs, "No translations registered") {
|
||||
t.Error("Empty registry should mention no translations")
|
||||
}
|
||||
}
|
||||
|
||||
func TestGenerateMarkdownDocs_WithTranslations(t *testing.T) {
|
||||
reg := NewRegistry()
|
||||
reg.Register(FormatOpenAI, FormatClaude, func(model string, data []byte, stream bool) []byte {
|
||||
return data
|
||||
}, ResponseTransform{
|
||||
Stream: func(ctx context.Context, model string, origReq, convReq, resp []byte, param *any) []string { return []string{string(resp)} },
|
||||
NonStream: func(ctx context.Context, model string, origReq, convReq, resp []byte, param *any) string { return string(resp) },
|
||||
})
|
||||
|
||||
docs := reg.GenerateMarkdownDocs()
|
||||
|
||||
// Check for main sections
|
||||
if !strings.Contains(docs, "# Translation Registry") {
|
||||
t.Error("Docs should contain title")
|
||||
}
|
||||
if !strings.Contains(docs, "## Supported Translations") {
|
||||
t.Error("Docs should contain translations section")
|
||||
}
|
||||
if !strings.Contains(docs, "## Detailed Capabilities") {
|
||||
t.Error("Docs should contain capabilities section")
|
||||
}
|
||||
if !strings.Contains(docs, "## Supported Formats") {
|
||||
t.Error("Docs should contain formats section")
|
||||
}
|
||||
|
||||
// Check for table headers
|
||||
if !strings.Contains(docs, "| From | To |") {
|
||||
t.Error("Docs should contain translation table")
|
||||
}
|
||||
|
||||
// Check for format names
|
||||
if !strings.Contains(docs, "openai") {
|
||||
t.Error("Docs should contain OpenAI format")
|
||||
}
|
||||
if !strings.Contains(docs, "claude") {
|
||||
t.Error("Docs should contain Claude format")
|
||||
}
|
||||
}
|
||||
|
||||
func TestGenerateMermaidDiagram_EmptyRegistry(t *testing.T) {
|
||||
reg := NewRegistry()
|
||||
diagram := reg.GenerateMermaidDiagram()
|
||||
|
||||
if !strings.Contains(diagram, "```mermaid") {
|
||||
t.Error("Diagram should start with mermaid code block")
|
||||
}
|
||||
if !strings.Contains(diagram, "flowchart LR") {
|
||||
t.Error("Diagram should be a left-to-right flowchart")
|
||||
}
|
||||
if !strings.Contains(diagram, "No translations registered") {
|
||||
t.Error("Empty registry should mention no translations")
|
||||
}
|
||||
if !strings.Contains(diagram, "```") {
|
||||
t.Error("Diagram should end with code block")
|
||||
}
|
||||
}
|
||||
|
||||
func TestGenerateMermaidDiagram_WithTranslations(t *testing.T) {
|
||||
reg := NewRegistry()
|
||||
reg.Register(FormatOpenAI, FormatClaude, func(model string, data []byte, stream bool) []byte {
|
||||
return data
|
||||
}, ResponseTransform{
|
||||
Stream: func(ctx context.Context, model string, origReq, convReq, resp []byte, param *any) []string { return []string{string(resp)} },
|
||||
})
|
||||
|
||||
diagram := reg.GenerateMermaidDiagram()
|
||||
|
||||
// Check structure
|
||||
if !strings.Contains(diagram, "```mermaid") {
|
||||
t.Error("Diagram should start with mermaid code block")
|
||||
}
|
||||
if !strings.Contains(diagram, "flowchart LR") {
|
||||
t.Error("Diagram should be a left-to-right flowchart")
|
||||
}
|
||||
|
||||
// Check for node definitions
|
||||
if !strings.Contains(diagram, "openai") {
|
||||
t.Error("Diagram should contain OpenAI node")
|
||||
}
|
||||
if !strings.Contains(diagram, "claude") {
|
||||
t.Error("Diagram should contain Claude node")
|
||||
}
|
||||
|
||||
// Check for edge with labels
|
||||
if !strings.Contains(diagram, "-->") {
|
||||
t.Error("Diagram should contain edges")
|
||||
}
|
||||
}
|
||||
|
||||
func TestGenerateTranslationSummary(t *testing.T) {
|
||||
reg := NewRegistry()
|
||||
reg.Register(FormatOpenAI, FormatClaude, func(model string, data []byte, stream bool) []byte {
|
||||
return data
|
||||
}, ResponseTransform{})
|
||||
reg.Register(FormatClaude, FormatGemini, func(model string, data []byte, stream bool) []byte {
|
||||
return data
|
||||
}, ResponseTransform{})
|
||||
|
||||
summary := reg.GenerateTranslationSummary()
|
||||
|
||||
if !strings.Contains(summary, "Translation Registry Summary") {
|
||||
t.Error("Summary should contain title")
|
||||
}
|
||||
if !strings.Contains(summary, "Total Formats:") {
|
||||
t.Error("Summary should contain format count")
|
||||
}
|
||||
if !strings.Contains(summary, "Total Translation Paths:") {
|
||||
t.Error("Summary should contain path count")
|
||||
}
|
||||
if !strings.Contains(summary, "Registered Paths:") {
|
||||
t.Error("Summary should contain registered paths section")
|
||||
}
|
||||
}
|
||||
|
||||
func TestBoolToCheck(t *testing.T) {
|
||||
if boolToCheck(true) != "yes" {
|
||||
t.Error("boolToCheck(true) should return 'yes'")
|
||||
}
|
||||
if boolToCheck(false) != "-" {
|
||||
t.Error("boolToCheck(false) should return '-'")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSanitizeMermaidID(t *testing.T) {
|
||||
tests := []struct {
|
||||
input string
|
||||
expected string
|
||||
}{
|
||||
{"openai", "openai"},
|
||||
{"open-ai", "open_ai"},
|
||||
{"format.v2", "format_v2"},
|
||||
{"test@format", "test_format"},
|
||||
{"ABC123", "ABC123"},
|
||||
{"a_b_c", "a_b_c"},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.input, func(t *testing.T) {
|
||||
result := sanitizeMermaidID(tt.input)
|
||||
if result != tt.expected {
|
||||
t.Errorf("sanitizeMermaidID(%q) = %q, want %q", tt.input, result, tt.expected)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestPackageLevelDocFunctions(t *testing.T) {
|
||||
// These verify package-level functions work
|
||||
markdown := GenerateMarkdownDocs()
|
||||
if markdown == "" {
|
||||
t.Error("GenerateMarkdownDocs should not return empty string")
|
||||
}
|
||||
|
||||
mermaid := GenerateMermaidDiagram()
|
||||
if mermaid == "" {
|
||||
t.Error("GenerateMermaidDiagram should not return empty string")
|
||||
}
|
||||
|
||||
summary := GenerateTranslationSummary()
|
||||
if summary == "" {
|
||||
t.Error("GenerateTranslationSummary should not return empty string")
|
||||
}
|
||||
}
|
||||
|
||||
func TestGenerateMarkdownDocs_TableFormat(t *testing.T) {
|
||||
reg := NewRegistry()
|
||||
reg.Register(FormatOpenAI, FormatClaude, func(model string, data []byte, stream bool) []byte {
|
||||
return data
|
||||
}, ResponseTransform{})
|
||||
|
||||
docs := reg.GenerateMarkdownDocs()
|
||||
|
||||
// Verify markdown table structure
|
||||
lines := strings.Split(docs, "\n")
|
||||
foundTableSeparator := false
|
||||
for _, line := range lines {
|
||||
if strings.Contains(line, "|---") {
|
||||
foundTableSeparator = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if !foundTableSeparator {
|
||||
t.Error("Markdown should contain proper table separators")
|
||||
}
|
||||
}
|
||||
|
||||
func TestGenerateMermaidDiagram_EdgeLabels(t *testing.T) {
|
||||
reg := NewRegistry()
|
||||
reg.Register(FormatOpenAI, FormatClaude, func(model string, data []byte, stream bool) []byte {
|
||||
return data
|
||||
}, ResponseTransform{
|
||||
Stream: func(ctx context.Context, model string, origReq, convReq, resp []byte, param *any) []string { return []string{string(resp)} },
|
||||
NonStream: func(ctx context.Context, model string, origReq, convReq, resp []byte, param *any) string { return string(resp) },
|
||||
})
|
||||
|
||||
diagram := reg.GenerateMermaidDiagram()
|
||||
|
||||
// Check for labeled edges
|
||||
if !strings.Contains(diagram, "|") {
|
||||
t.Error("Diagram should contain edge labels")
|
||||
}
|
||||
if !strings.Contains(diagram, "req") {
|
||||
t.Error("Diagram should indicate request capability")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,133 @@
|
||||
package translator
|
||||
|
||||
import (
|
||||
"sync"
|
||||
)
|
||||
|
||||
// fallbackChain represents a chain of intermediate formats for translation.
|
||||
type fallbackChain struct {
|
||||
via []Format
|
||||
}
|
||||
|
||||
// FallbackRegistry manages fallback chains for translations.
|
||||
type FallbackRegistry struct {
|
||||
mu sync.RWMutex
|
||||
chains map[Format]map[Format]fallbackChain
|
||||
}
|
||||
|
||||
// NewFallbackRegistry creates a new FallbackRegistry.
|
||||
func NewFallbackRegistry() *FallbackRegistry {
|
||||
return &FallbackRegistry{
|
||||
chains: make(map[Format]map[Format]fallbackChain),
|
||||
}
|
||||
}
|
||||
|
||||
// RegisterChain registers a fallback chain: from -> to via intermediate formats.
|
||||
// For example, RegisterChain(A, B, []Format{X, Y}) means try A->X->Y->B.
|
||||
func (fr *FallbackRegistry) RegisterChain(from, to Format, via []Format) {
|
||||
fr.mu.Lock()
|
||||
defer fr.mu.Unlock()
|
||||
|
||||
if _, ok := fr.chains[from]; !ok {
|
||||
fr.chains[from] = make(map[Format]fallbackChain)
|
||||
}
|
||||
fr.chains[from][to] = fallbackChain{via: via}
|
||||
}
|
||||
|
||||
// GetChain returns the registered fallback chain for from -> to.
|
||||
// Returns nil if no chain is registered.
|
||||
func (fr *FallbackRegistry) GetChain(from, to Format) []Format {
|
||||
fr.mu.RLock()
|
||||
defer fr.mu.RUnlock()
|
||||
|
||||
if byTarget, ok := fr.chains[from]; ok {
|
||||
if chain, isOk := byTarget[to]; isOk {
|
||||
// Return a copy to prevent mutation
|
||||
result := make([]Format, len(chain.via))
|
||||
copy(result, chain.via)
|
||||
return result
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// UnregisterChain removes a fallback chain.
|
||||
func (fr *FallbackRegistry) UnregisterChain(from, to Format) {
|
||||
fr.mu.Lock()
|
||||
defer fr.mu.Unlock()
|
||||
|
||||
if byTarget, ok := fr.chains[from]; ok {
|
||||
delete(byTarget, to)
|
||||
if len(byTarget) == 0 {
|
||||
delete(fr.chains, from)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Clone creates a deep copy of the FallbackRegistry.
|
||||
func (fr *FallbackRegistry) Clone() *FallbackRegistry {
|
||||
fr.mu.RLock()
|
||||
defer fr.mu.RUnlock()
|
||||
|
||||
newFR := NewFallbackRegistry()
|
||||
for from, targets := range fr.chains {
|
||||
newFR.chains[from] = make(map[Format]fallbackChain)
|
||||
for to, chain := range targets {
|
||||
viaCopy := make([]Format, len(chain.via))
|
||||
copy(viaCopy, chain.via)
|
||||
newFR.chains[from][to] = fallbackChain{via: viaCopy}
|
||||
}
|
||||
}
|
||||
return newFR
|
||||
}
|
||||
|
||||
// defaultFallbackRegistry is the package-level fallback registry.
|
||||
var defaultFallbackRegistry = NewFallbackRegistry()
|
||||
|
||||
// DefaultFallbackRegistry returns the package-level fallback registry.
|
||||
func DefaultFallbackRegistry() *FallbackRegistry {
|
||||
return defaultFallbackRegistry
|
||||
}
|
||||
|
||||
// RegisterFallbackChain registers a fallback chain in the default registry.
|
||||
func RegisterFallbackChain(from, to Format, via []Format) {
|
||||
defaultFallbackRegistry.RegisterChain(from, to, via)
|
||||
}
|
||||
|
||||
// GetFallbackChain returns the fallback chain from the default registry.
|
||||
func GetFallbackChain(from, to Format) []Format {
|
||||
return defaultFallbackRegistry.GetChain(from, to)
|
||||
}
|
||||
|
||||
// UnregisterFallbackChain removes a fallback chain from the default registry.
|
||||
func UnregisterFallbackChain(from, to Format) {
|
||||
defaultFallbackRegistry.UnregisterChain(from, to)
|
||||
}
|
||||
|
||||
// buildFullPath builds the complete translation path including via formats.
|
||||
// For A->B via [X, Y], returns [A, X, Y, B].
|
||||
func buildFullPath(from, to Format, via []Format) []Format {
|
||||
path := make([]Format, 0, len(via)+2)
|
||||
path = append(path, from)
|
||||
path = append(path, via...)
|
||||
path = append(path, to)
|
||||
return path
|
||||
}
|
||||
|
||||
// TranslateRequestViaChain translates a request through a chain of formats.
|
||||
func (r *Registry) TranslateRequestViaChain(path []Format, model string, rawJSON []byte, stream bool) []byte {
|
||||
if len(path) < 2 {
|
||||
return rawJSON
|
||||
}
|
||||
|
||||
current := rawJSON
|
||||
for i := 0; i < len(path)-1; i++ {
|
||||
current = r.TranslateRequest(path[i], path[i+1], model, current, stream)
|
||||
}
|
||||
return current
|
||||
}
|
||||
|
||||
// TranslateRequestViaChain is a helper on the default registry.
|
||||
func TranslateRequestViaChain(path []Format, model string, rawJSON []byte, stream bool) []byte {
|
||||
return defaultRegistry.TranslateRequestViaChain(path, model, rawJSON, stream)
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user