mirror of
https://github.com/Finesssee/ProxyPilot.git
synced 2026-11-21 04:44:40 +00:00
Compare commits
130
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5b11c113fb | ||
|
|
b127b674e9 | ||
|
|
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 |
+1
-2
@@ -13,8 +13,6 @@ Dockerfile
|
||||
docs/*
|
||||
README.md
|
||||
README_CN.md
|
||||
MANAGEMENT_API.md
|
||||
MANAGEMENT_API_CN.md
|
||||
LICENSE
|
||||
|
||||
# Runtime data folders (should be mounted as volumes)
|
||||
@@ -32,3 +30,4 @@ bin/*
|
||||
.agent/*
|
||||
.bmad/*
|
||||
_bmad/*
|
||||
_bmad-output/*
|
||||
|
||||
@@ -7,6 +7,13 @@ assignees: ''
|
||||
|
||||
---
|
||||
|
||||
**Is it a request payload issue?**
|
||||
[ ] Yes, this is a request payload issue. I am using a client/cURL to send a request payload, but I received an unexpected error.
|
||||
[ ] No, it's another issue.
|
||||
|
||||
**If it's a request payload issue, you MUST know**
|
||||
Our team doesn't have any GODs or ORACLEs or MIND READERs. Please make sure to attach the request log or curl payload.
|
||||
|
||||
**Describe the bug**
|
||||
A clear and concise description of what the bug is.
|
||||
|
||||
|
||||
@@ -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
|
||||
+18
-8
@@ -12,10 +12,18 @@ bin/*
|
||||
logs/*
|
||||
conv/*
|
||||
temp/*
|
||||
refs/*
|
||||
|
||||
# Storage backends
|
||||
pgstore/*
|
||||
gitstore/*
|
||||
objectstore/*
|
||||
|
||||
# Static assets
|
||||
static/*
|
||||
!static/icon.png
|
||||
!static/icon.ico
|
||||
test/
|
||||
refs/*
|
||||
dist/*
|
||||
.proxypilot/*
|
||||
@@ -36,6 +44,8 @@ docs/*
|
||||
!docs/memory.md
|
||||
!docs/proxypilot.md
|
||||
!docs/agentic-harness-demo.md
|
||||
!docs/long-context-features.md
|
||||
!docs/desktop-app-build-process.md
|
||||
AGENTS.md
|
||||
CLAUDE.md
|
||||
GEMINI.md
|
||||
@@ -48,6 +58,7 @@ GEMINI.md
|
||||
.agent/*
|
||||
.bmad/*
|
||||
_bmad/*
|
||||
_bmad-output/*
|
||||
.mcp/cache/
|
||||
|
||||
# macOS
|
||||
@@ -55,11 +66,10 @@ _bmad/*
|
||||
._*
|
||||
|
||||
*.log
|
||||
*_out.txt
|
||||
|
||||
# Added by Gemini CLI fix
|
||||
node_modules/
|
||||
dist/
|
||||
.turbo/
|
||||
.env
|
||||
|
||||
*_out.txt
|
||||
|
||||
# Added by Gemini CLI fix
|
||||
node_modules/
|
||||
dist/
|
||||
.turbo/
|
||||
.env
|
||||
|
||||
+10
-1
@@ -1,3 +1,9 @@
|
||||
before:
|
||||
hooks:
|
||||
# Remove Windows resource files before building
|
||||
# These .syso files cause ARM64 cross-compile failures on Linux/Darwin
|
||||
- find . -name "*.syso" -type f -delete || true
|
||||
|
||||
builds:
|
||||
- id: "cli-proxy-api-plus"
|
||||
env:
|
||||
@@ -9,6 +15,10 @@ builds:
|
||||
goarch:
|
||||
- amd64
|
||||
- arm64
|
||||
ignore:
|
||||
# Skip Windows ARM64 (not commonly used)
|
||||
- goos: windows
|
||||
goarch: arm64
|
||||
main: ./cmd/server/
|
||||
binary: cli-proxy-api-plus
|
||||
ldflags:
|
||||
@@ -22,7 +32,6 @@ archives:
|
||||
files:
|
||||
- LICENSE
|
||||
- README.md
|
||||
- README_CN.md
|
||||
- config.example.yaml
|
||||
|
||||
checksum:
|
||||
|
||||
@@ -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": 4,
|
||||
"Build": 0
|
||||
},
|
||||
"ProductVersion": {
|
||||
"Major": 0,
|
||||
"Minor": 1,
|
||||
"Patch": 4,
|
||||
"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.4",
|
||||
"InternalName": "ProxyPilot",
|
||||
"LegalCopyright": "MIT License",
|
||||
"OriginalFilename": "ProxyPilot.exe",
|
||||
"ProductName": "ProxyPilot",
|
||||
"ProductVersion": "0.1.4"
|
||||
},
|
||||
"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": 4,
|
||||
"Build": 0
|
||||
},
|
||||
"ProductVersion": {
|
||||
"Major": 0,
|
||||
"Minor": 1,
|
||||
"Patch": 4,
|
||||
"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.4",
|
||||
"InternalName": "proxypilot-engine",
|
||||
"LegalCopyright": "MIT License",
|
||||
"OriginalFilename": "proxypilot-engine.exe",
|
||||
"ProductName": "ProxyPilot",
|
||||
"ProductVersion": "0.1.4"
|
||||
},
|
||||
"VarFileInfo": {
|
||||
"Translation": {
|
||||
"LangID": "0409",
|
||||
"CharsetID": "04B0"
|
||||
}
|
||||
},
|
||||
"IconPath": "../../static/icon.ico",
|
||||
"ManifestPath": ""
|
||||
}
|
||||
+48
-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:
|
||||
@@ -102,6 +145,9 @@ ws-auth: false
|
||||
# headers:
|
||||
# X-Custom-Header: "custom-value"
|
||||
# proxy-url: "socks5://proxy.example.com:1080" # optional: per-key proxy override
|
||||
# models:
|
||||
# - name: "gpt-5-codex" # upstream model name
|
||||
# alias: "codex-latest" # client alias mapped to the upstream model
|
||||
# excluded-models:
|
||||
# - "gpt-5.2" # exclude specific models (exact match)
|
||||
# - "gpt-5-*" # wildcard matching prefix (e.g. gpt-5-medium, gpt-5-codex)
|
||||
|
||||
@@ -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")
|
||||
}
|
||||
}
|
||||
@@ -597,11 +597,7 @@ func (h *Handler) PutCodexKeys(c *gin.Context) {
|
||||
filtered := make([]config.CodexKey, 0, len(arr))
|
||||
for i := range arr {
|
||||
entry := arr[i]
|
||||
entry.APIKey = strings.TrimSpace(entry.APIKey)
|
||||
entry.BaseURL = strings.TrimSpace(entry.BaseURL)
|
||||
entry.ProxyURL = strings.TrimSpace(entry.ProxyURL)
|
||||
entry.Headers = config.NormalizeHeaders(entry.Headers)
|
||||
entry.ExcludedModels = config.NormalizeExcludedModels(entry.ExcludedModels)
|
||||
normalizeCodexKey(&entry)
|
||||
if entry.BaseURL == "" {
|
||||
continue
|
||||
}
|
||||
@@ -613,12 +609,13 @@ func (h *Handler) PutCodexKeys(c *gin.Context) {
|
||||
}
|
||||
func (h *Handler) PatchCodexKey(c *gin.Context) {
|
||||
type codexKeyPatch struct {
|
||||
APIKey *string `json:"api-key"`
|
||||
Prefix *string `json:"prefix"`
|
||||
BaseURL *string `json:"base-url"`
|
||||
ProxyURL *string `json:"proxy-url"`
|
||||
Headers *map[string]string `json:"headers"`
|
||||
ExcludedModels *[]string `json:"excluded-models"`
|
||||
APIKey *string `json:"api-key"`
|
||||
Prefix *string `json:"prefix"`
|
||||
BaseURL *string `json:"base-url"`
|
||||
ProxyURL *string `json:"proxy-url"`
|
||||
Models *[]config.CodexModel `json:"models"`
|
||||
Headers *map[string]string `json:"headers"`
|
||||
ExcludedModels *[]string `json:"excluded-models"`
|
||||
}
|
||||
var body struct {
|
||||
Index *int `json:"index"`
|
||||
@@ -667,12 +664,16 @@ func (h *Handler) PatchCodexKey(c *gin.Context) {
|
||||
if body.Value.ProxyURL != nil {
|
||||
entry.ProxyURL = strings.TrimSpace(*body.Value.ProxyURL)
|
||||
}
|
||||
if body.Value.Models != nil {
|
||||
entry.Models = append([]config.CodexModel(nil), (*body.Value.Models)...)
|
||||
}
|
||||
if body.Value.Headers != nil {
|
||||
entry.Headers = config.NormalizeHeaders(*body.Value.Headers)
|
||||
}
|
||||
if body.Value.ExcludedModels != nil {
|
||||
entry.ExcludedModels = config.NormalizeExcludedModels(*body.Value.ExcludedModels)
|
||||
}
|
||||
normalizeCodexKey(&entry)
|
||||
h.cfg.CodexKey[targetIndex] = entry
|
||||
h.cfg.SanitizeCodexKeys()
|
||||
h.persist(c)
|
||||
@@ -761,3 +762,29 @@ func normalizeClaudeKey(entry *config.ClaudeKey) {
|
||||
}
|
||||
entry.Models = normalized
|
||||
}
|
||||
|
||||
func normalizeCodexKey(entry *config.CodexKey) {
|
||||
if entry == nil {
|
||||
return
|
||||
}
|
||||
entry.APIKey = strings.TrimSpace(entry.APIKey)
|
||||
entry.Prefix = strings.TrimSpace(entry.Prefix)
|
||||
entry.BaseURL = strings.TrimSpace(entry.BaseURL)
|
||||
entry.ProxyURL = strings.TrimSpace(entry.ProxyURL)
|
||||
entry.Headers = config.NormalizeHeaders(entry.Headers)
|
||||
entry.ExcludedModels = config.NormalizeExcludedModels(entry.ExcludedModels)
|
||||
if len(entry.Models) == 0 {
|
||||
return
|
||||
}
|
||||
normalized := make([]config.CodexModel, 0, len(entry.Models))
|
||||
for i := range entry.Models {
|
||||
model := entry.Models[i]
|
||||
model.Name = strings.TrimSpace(model.Name)
|
||||
model.Alias = strings.TrimSpace(model.Alias)
|
||||
if model.Name == "" && model.Alias == "" {
|
||||
continue
|
||||
}
|
||||
normalized = append(normalized, model)
|
||||
}
|
||||
entry.Models = normalized
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
@@ -1,20 +1,83 @@
|
||||
package management
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/router-for-me/CLIProxyAPI/v6/internal/usage"
|
||||
)
|
||||
|
||||
type usageExportPayload struct {
|
||||
Version int `json:"version"`
|
||||
ExportedAt time.Time `json:"exported_at"`
|
||||
Usage usage.StatisticsSnapshot `json:"usage"`
|
||||
}
|
||||
|
||||
type usageImportPayload struct {
|
||||
Version int `json:"version"`
|
||||
Usage usage.StatisticsSnapshot `json:"usage"`
|
||||
}
|
||||
|
||||
// GetUsageStatistics returns the in-memory request statistics snapshot.
|
||||
func (h *Handler) GetUsageStatistics(c *gin.Context) {
|
||||
var snapshot usage.StatisticsSnapshot
|
||||
if h != nil && h.usageStats != nil {
|
||||
snapshot = h.usageStats.Snapshot()
|
||||
}
|
||||
|
||||
usageStats := usage.ComputeUsageStats(snapshot)
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"usage": snapshot,
|
||||
"usage": usageStats,
|
||||
"raw_snapshot": snapshot,
|
||||
"failed_requests": snapshot.FailureCount,
|
||||
})
|
||||
}
|
||||
|
||||
// ExportUsageStatistics returns a complete usage snapshot for backup/migration.
|
||||
func (h *Handler) ExportUsageStatistics(c *gin.Context) {
|
||||
var snapshot usage.StatisticsSnapshot
|
||||
if h != nil && h.usageStats != nil {
|
||||
snapshot = h.usageStats.Snapshot()
|
||||
}
|
||||
c.JSON(http.StatusOK, usageExportPayload{
|
||||
Version: 1,
|
||||
ExportedAt: time.Now().UTC(),
|
||||
Usage: snapshot,
|
||||
})
|
||||
}
|
||||
|
||||
// ImportUsageStatistics merges a previously exported usage snapshot into memory.
|
||||
func (h *Handler) ImportUsageStatistics(c *gin.Context) {
|
||||
if h == nil || h.usageStats == nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "usage statistics unavailable"})
|
||||
return
|
||||
}
|
||||
|
||||
data, err := c.GetRawData()
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "failed to read request body"})
|
||||
return
|
||||
}
|
||||
|
||||
var payload usageImportPayload
|
||||
if err := json.Unmarshal(data, &payload); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid json"})
|
||||
return
|
||||
}
|
||||
if payload.Version != 0 && payload.Version != 1 {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "unsupported version"})
|
||||
return
|
||||
}
|
||||
|
||||
result := h.usageStats.MergeSnapshot(payload.Usage)
|
||||
snapshot := h.usageStats.Snapshot()
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"added": result.Added,
|
||||
"skipped": result.Skipped,
|
||||
"total_requests": snapshot.TotalRequests,
|
||||
"failed_requests": snapshot.FailureCount,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -0,0 +1,203 @@
|
||||
// Package handlers provides HTTP handlers for the API server.
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/router-for-me/CLIProxyAPI/v6/sdk/translator"
|
||||
)
|
||||
|
||||
// TranslatorHandler provides HTTP handlers for translation-related endpoints.
|
||||
type TranslatorHandler struct{}
|
||||
|
||||
// NewTranslatorHandler creates a new translator handler.
|
||||
func NewTranslatorHandler() *TranslatorHandler {
|
||||
return &TranslatorHandler{}
|
||||
}
|
||||
|
||||
// TranslationsMatrixResponse represents the response for the translations matrix endpoint.
|
||||
type TranslationsMatrixResponse struct {
|
||||
Matrix map[string][]string `json:"matrix"`
|
||||
Formats []string `json:"formats"`
|
||||
Total int `json:"total_translations"`
|
||||
Details []translator.TranslationInfo `json:"details,omitempty"`
|
||||
}
|
||||
|
||||
// GetTranslationsMatrix returns the full compatibility matrix of supported translations.
|
||||
// GET /v1/translations
|
||||
func (h *TranslatorHandler) GetTranslationsMatrix(c *gin.Context) {
|
||||
matrix := translator.GetCompatibilityMatrix()
|
||||
formats := translator.GetSupportedFormats()
|
||||
translations := translator.GetAllTranslations()
|
||||
|
||||
// Convert formats to strings
|
||||
formatStrings := make([]string, len(formats))
|
||||
for i, f := range formats {
|
||||
formatStrings[i] = f.String()
|
||||
}
|
||||
|
||||
// Count total translations
|
||||
total := 0
|
||||
for _, targets := range matrix {
|
||||
total += len(targets)
|
||||
}
|
||||
|
||||
// Check if detailed info is requested
|
||||
includeDetails := c.Query("details") == "true"
|
||||
|
||||
response := TranslationsMatrixResponse{
|
||||
Matrix: matrix,
|
||||
Formats: formatStrings,
|
||||
Total: total,
|
||||
}
|
||||
|
||||
if includeDetails {
|
||||
response.Details = translations
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, response)
|
||||
}
|
||||
|
||||
// CheckTranslationResponse represents the response for checking a specific translation.
|
||||
type CheckTranslationResponse struct {
|
||||
Supported bool `json:"supported"`
|
||||
Fallback bool `json:"fallback"`
|
||||
From string `json:"from"`
|
||||
To string `json:"to"`
|
||||
Info *translator.TranslationInfo `json:"info,omitempty"`
|
||||
Alternatives []string `json:"alternatives,omitempty"`
|
||||
}
|
||||
|
||||
// CheckTranslation checks if a specific translation path is supported.
|
||||
// GET /v1/translations/check?from=X&to=Y
|
||||
func (h *TranslatorHandler) CheckTranslation(c *gin.Context) {
|
||||
from := c.Query("from")
|
||||
to := c.Query("to")
|
||||
|
||||
if from == "" || to == "" {
|
||||
c.JSON(http.StatusBadRequest, gin.H{
|
||||
"error": "missing required query parameters",
|
||||
"details": "both 'from' and 'to' query parameters are required",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
fromFormat := translator.FromString(from)
|
||||
toFormat := translator.FromString(to)
|
||||
|
||||
supported := translator.IsTranslationSupported(fromFormat, toFormat)
|
||||
info := translator.GetTranslationInfo(fromFormat, toFormat)
|
||||
|
||||
response := CheckTranslationResponse{
|
||||
Supported: supported,
|
||||
Fallback: false,
|
||||
From: from,
|
||||
To: to,
|
||||
}
|
||||
|
||||
// Include detailed info if translation exists
|
||||
if supported {
|
||||
response.Info = info
|
||||
} else {
|
||||
// Check for potential fallback paths or alternatives
|
||||
matrix := translator.GetCompatibilityMatrix()
|
||||
if targets, exists := matrix[from]; exists {
|
||||
response.Alternatives = targets
|
||||
}
|
||||
|
||||
// Mark as fallback if the request would pass through unchanged
|
||||
// (same format translation is always supported as a no-op)
|
||||
if from == to {
|
||||
response.Fallback = true
|
||||
response.Supported = true
|
||||
}
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, response)
|
||||
}
|
||||
|
||||
// GetTranslationDocs returns markdown documentation for all translations.
|
||||
// GET /v1/translations/docs
|
||||
func (h *TranslatorHandler) GetTranslationDocs(c *gin.Context) {
|
||||
format := c.Query("format")
|
||||
|
||||
switch format {
|
||||
case "mermaid":
|
||||
c.Header("Content-Type", "text/plain; charset=utf-8")
|
||||
c.String(http.StatusOK, translator.GenerateMermaidDiagram())
|
||||
case "summary":
|
||||
c.Header("Content-Type", "text/plain; charset=utf-8")
|
||||
c.String(http.StatusOK, translator.GenerateTranslationSummary())
|
||||
default:
|
||||
// Default to markdown
|
||||
c.Header("Content-Type", "text/markdown; charset=utf-8")
|
||||
c.String(http.StatusOK, translator.GenerateMarkdownDocs())
|
||||
}
|
||||
}
|
||||
|
||||
// ScoreTranslationRequest represents the request body for scoring a translation.
|
||||
type ScoreTranslationRequest struct {
|
||||
From string `json:"from" binding:"required"`
|
||||
To string `json:"to" binding:"required"`
|
||||
Before string `json:"before" binding:"required"`
|
||||
After string `json:"after" binding:"required"`
|
||||
}
|
||||
|
||||
// ScoreTranslation analyzes the quality of a translation.
|
||||
// POST /v1/translations/score
|
||||
func (h *TranslatorHandler) ScoreTranslation(c *gin.Context) {
|
||||
var req ScoreTranslationRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{
|
||||
"error": "invalid request body",
|
||||
"details": err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
fromFormat := translator.FromString(req.From)
|
||||
toFormat := translator.FromString(req.To)
|
||||
|
||||
report := translator.ScoreTranslation(
|
||||
fromFormat,
|
||||
toFormat,
|
||||
[]byte(req.Before),
|
||||
[]byte(req.After),
|
||||
)
|
||||
|
||||
c.JSON(http.StatusOK, report)
|
||||
}
|
||||
|
||||
// CompareStructuresRequest represents the request body for comparing JSON structures.
|
||||
type CompareStructuresRequest struct {
|
||||
Before string `json:"before" binding:"required"`
|
||||
After string `json:"after" binding:"required"`
|
||||
}
|
||||
|
||||
// CompareStructures provides detailed comparison of two JSON structures.
|
||||
// POST /v1/translations/compare
|
||||
func (h *TranslatorHandler) CompareStructures(c *gin.Context) {
|
||||
var req CompareStructuresRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{
|
||||
"error": "invalid request body",
|
||||
"details": err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
comparison, err := translator.CompareJSONStructures(
|
||||
[]byte(req.Before),
|
||||
[]byte(req.After),
|
||||
)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{
|
||||
"error": "failed to compare structures",
|
||||
"details": err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, comparison)
|
||||
}
|
||||
@@ -0,0 +1,414 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/router-for-me/CLIProxyAPI/v6/sdk/translator"
|
||||
)
|
||||
|
||||
func init() {
|
||||
gin.SetMode(gin.TestMode)
|
||||
}
|
||||
|
||||
func setupTestRouter() *gin.Engine {
|
||||
router := gin.New()
|
||||
handler := NewTranslatorHandler()
|
||||
|
||||
v1 := router.Group("/v1")
|
||||
{
|
||||
v1.GET("/translations", handler.GetTranslationsMatrix)
|
||||
v1.GET("/translations/check", handler.CheckTranslation)
|
||||
v1.GET("/translations/docs", handler.GetTranslationDocs)
|
||||
v1.POST("/translations/score", handler.ScoreTranslation)
|
||||
v1.POST("/translations/compare", handler.CompareStructures)
|
||||
}
|
||||
|
||||
return router
|
||||
}
|
||||
|
||||
func TestGetTranslationsMatrix(t *testing.T) {
|
||||
router := setupTestRouter()
|
||||
|
||||
req, _ := http.NewRequest("GET", "/v1/translations", nil)
|
||||
w := httptest.NewRecorder()
|
||||
router.ServeHTTP(w, req)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Errorf("Expected status %d, got %d", http.StatusOK, w.Code)
|
||||
}
|
||||
|
||||
var response TranslationsMatrixResponse
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &response); err != nil {
|
||||
t.Fatalf("Failed to parse response: %v", err)
|
||||
}
|
||||
|
||||
if response.Matrix == nil {
|
||||
t.Error("Matrix should not be nil")
|
||||
}
|
||||
if response.Formats == nil {
|
||||
t.Error("Formats should not be nil")
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetTranslationsMatrix_WithDetails(t *testing.T) {
|
||||
router := setupTestRouter()
|
||||
|
||||
req, _ := http.NewRequest("GET", "/v1/translations?details=true", nil)
|
||||
w := httptest.NewRecorder()
|
||||
router.ServeHTTP(w, req)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Errorf("Expected status %d, got %d", http.StatusOK, w.Code)
|
||||
}
|
||||
|
||||
var response TranslationsMatrixResponse
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &response); err != nil {
|
||||
t.Fatalf("Failed to parse response: %v", err)
|
||||
}
|
||||
|
||||
// When details=true, Details should be populated
|
||||
// (may be empty if no translations registered, but should be non-nil conceptually)
|
||||
}
|
||||
|
||||
func TestCheckTranslation_Supported(t *testing.T) {
|
||||
router := setupTestRouter()
|
||||
|
||||
// Register a test translator
|
||||
translator.Register(translator.FormatOpenAI, translator.FormatClaude, func(model string, data []byte, stream bool) []byte {
|
||||
return data
|
||||
}, translator.ResponseTransform{})
|
||||
defer translator.Unregister(translator.FormatOpenAI, translator.FormatClaude)
|
||||
|
||||
req, _ := http.NewRequest("GET", "/v1/translations/check?from=openai&to=claude", nil)
|
||||
w := httptest.NewRecorder()
|
||||
router.ServeHTTP(w, req)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Errorf("Expected status %d, got %d", http.StatusOK, w.Code)
|
||||
}
|
||||
|
||||
var response CheckTranslationResponse
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &response); err != nil {
|
||||
t.Fatalf("Failed to parse response: %v", err)
|
||||
}
|
||||
|
||||
if !response.Supported {
|
||||
t.Error("Translation should be supported")
|
||||
}
|
||||
if response.From != "openai" {
|
||||
t.Errorf("From = %s, want openai", response.From)
|
||||
}
|
||||
if response.To != "claude" {
|
||||
t.Errorf("To = %s, want claude", response.To)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheckTranslation_NotSupported(t *testing.T) {
|
||||
router := setupTestRouter()
|
||||
|
||||
req, _ := http.NewRequest("GET", "/v1/translations/check?from=unknown&to=unknown2", nil)
|
||||
w := httptest.NewRecorder()
|
||||
router.ServeHTTP(w, req)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Errorf("Expected status %d, got %d", http.StatusOK, w.Code)
|
||||
}
|
||||
|
||||
var response CheckTranslationResponse
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &response); err != nil {
|
||||
t.Fatalf("Failed to parse response: %v", err)
|
||||
}
|
||||
|
||||
if response.Supported {
|
||||
t.Error("Translation should not be supported for unknown formats")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheckTranslation_MissingParams(t *testing.T) {
|
||||
router := setupTestRouter()
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
url string
|
||||
}{
|
||||
{"missing both", "/v1/translations/check"},
|
||||
{"missing to", "/v1/translations/check?from=openai"},
|
||||
{"missing from", "/v1/translations/check?to=claude"},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
req, _ := http.NewRequest("GET", tt.url, nil)
|
||||
w := httptest.NewRecorder()
|
||||
router.ServeHTTP(w, req)
|
||||
|
||||
if w.Code != http.StatusBadRequest {
|
||||
t.Errorf("Expected status %d, got %d", http.StatusBadRequest, w.Code)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheckTranslation_SameFormat(t *testing.T) {
|
||||
router := setupTestRouter()
|
||||
|
||||
req, _ := http.NewRequest("GET", "/v1/translations/check?from=openai&to=openai", nil)
|
||||
w := httptest.NewRecorder()
|
||||
router.ServeHTTP(w, req)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Errorf("Expected status %d, got %d", http.StatusOK, w.Code)
|
||||
}
|
||||
|
||||
var response CheckTranslationResponse
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &response); err != nil {
|
||||
t.Fatalf("Failed to parse response: %v", err)
|
||||
}
|
||||
|
||||
// Same format translation is a fallback/no-op
|
||||
if !response.Fallback {
|
||||
t.Error("Same format should be marked as fallback")
|
||||
}
|
||||
if !response.Supported {
|
||||
t.Error("Same format should be supported")
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetTranslationDocs_Markdown(t *testing.T) {
|
||||
router := setupTestRouter()
|
||||
|
||||
req, _ := http.NewRequest("GET", "/v1/translations/docs", nil)
|
||||
w := httptest.NewRecorder()
|
||||
router.ServeHTTP(w, req)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Errorf("Expected status %d, got %d", http.StatusOK, w.Code)
|
||||
}
|
||||
|
||||
contentType := w.Header().Get("Content-Type")
|
||||
if contentType != "text/markdown; charset=utf-8" {
|
||||
t.Errorf("Content-Type = %s, want text/markdown; charset=utf-8", contentType)
|
||||
}
|
||||
|
||||
body := w.Body.String()
|
||||
if body == "" {
|
||||
t.Error("Response body should not be empty")
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetTranslationDocs_Mermaid(t *testing.T) {
|
||||
router := setupTestRouter()
|
||||
|
||||
req, _ := http.NewRequest("GET", "/v1/translations/docs?format=mermaid", nil)
|
||||
w := httptest.NewRecorder()
|
||||
router.ServeHTTP(w, req)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Errorf("Expected status %d, got %d", http.StatusOK, w.Code)
|
||||
}
|
||||
|
||||
contentType := w.Header().Get("Content-Type")
|
||||
if contentType != "text/plain; charset=utf-8" {
|
||||
t.Errorf("Content-Type = %s, want text/plain; charset=utf-8", contentType)
|
||||
}
|
||||
|
||||
body := w.Body.String()
|
||||
if !containsString(body, "mermaid") {
|
||||
t.Error("Response should contain mermaid diagram")
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetTranslationDocs_Summary(t *testing.T) {
|
||||
router := setupTestRouter()
|
||||
|
||||
req, _ := http.NewRequest("GET", "/v1/translations/docs?format=summary", nil)
|
||||
w := httptest.NewRecorder()
|
||||
router.ServeHTTP(w, req)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Errorf("Expected status %d, got %d", http.StatusOK, w.Code)
|
||||
}
|
||||
|
||||
contentType := w.Header().Get("Content-Type")
|
||||
if contentType != "text/plain; charset=utf-8" {
|
||||
t.Errorf("Content-Type = %s, want text/plain; charset=utf-8", contentType)
|
||||
}
|
||||
|
||||
body := w.Body.String()
|
||||
if !containsString(body, "Summary") {
|
||||
t.Error("Response should contain summary")
|
||||
}
|
||||
}
|
||||
|
||||
func TestScoreTranslation(t *testing.T) {
|
||||
router := setupTestRouter()
|
||||
|
||||
requestBody := ScoreTranslationRequest{
|
||||
From: "openai",
|
||||
To: "claude",
|
||||
Before: `{"model": "gpt-4", "messages": []}`,
|
||||
After: `{"model": "gpt-4", "messages": []}`,
|
||||
}
|
||||
body, _ := json.Marshal(requestBody)
|
||||
|
||||
req, _ := http.NewRequest("POST", "/v1/translations/score", bytes.NewBuffer(body))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
w := httptest.NewRecorder()
|
||||
router.ServeHTTP(w, req)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Errorf("Expected status %d, got %d", http.StatusOK, w.Code)
|
||||
}
|
||||
|
||||
var report translator.QualityReport
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &report); err != nil {
|
||||
t.Fatalf("Failed to parse response: %v", err)
|
||||
}
|
||||
|
||||
// Perfect match should have high score
|
||||
if report.Score < 0.9 {
|
||||
t.Errorf("Score = %v, expected >= 0.9 for identical payloads", report.Score)
|
||||
}
|
||||
}
|
||||
|
||||
func TestScoreTranslation_InvalidBody(t *testing.T) {
|
||||
router := setupTestRouter()
|
||||
|
||||
req, _ := http.NewRequest("POST", "/v1/translations/score", bytes.NewBufferString("invalid json"))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
w := httptest.NewRecorder()
|
||||
router.ServeHTTP(w, req)
|
||||
|
||||
if w.Code != http.StatusBadRequest {
|
||||
t.Errorf("Expected status %d, got %d", http.StatusBadRequest, w.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestScoreTranslation_MissingFields(t *testing.T) {
|
||||
router := setupTestRouter()
|
||||
|
||||
// Missing required fields
|
||||
requestBody := map[string]string{
|
||||
"from": "openai",
|
||||
// missing to, before, after
|
||||
}
|
||||
body, _ := json.Marshal(requestBody)
|
||||
|
||||
req, _ := http.NewRequest("POST", "/v1/translations/score", bytes.NewBuffer(body))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
w := httptest.NewRecorder()
|
||||
router.ServeHTTP(w, req)
|
||||
|
||||
if w.Code != http.StatusBadRequest {
|
||||
t.Errorf("Expected status %d, got %d", http.StatusBadRequest, w.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCompareStructures(t *testing.T) {
|
||||
router := setupTestRouter()
|
||||
|
||||
requestBody := CompareStructuresRequest{
|
||||
Before: `{"a": 1, "b": 2}`,
|
||||
After: `{"a": 1, "c": 3}`,
|
||||
}
|
||||
body, _ := json.Marshal(requestBody)
|
||||
|
||||
req, _ := http.NewRequest("POST", "/v1/translations/compare", bytes.NewBuffer(body))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
w := httptest.NewRecorder()
|
||||
router.ServeHTTP(w, req)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Errorf("Expected status %d, got %d", http.StatusOK, w.Code)
|
||||
}
|
||||
|
||||
var comparison translator.StructureComparison
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &comparison); err != nil {
|
||||
t.Fatalf("Failed to parse response: %v", err)
|
||||
}
|
||||
|
||||
if comparison.SourceType != "object" {
|
||||
t.Errorf("SourceType = %s, want object", comparison.SourceType)
|
||||
}
|
||||
if comparison.TargetType != "object" {
|
||||
t.Errorf("TargetType = %s, want object", comparison.TargetType)
|
||||
}
|
||||
if len(comparison.Diffs) == 0 {
|
||||
t.Error("Should detect differences between the structures")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCompareStructures_InvalidBody(t *testing.T) {
|
||||
router := setupTestRouter()
|
||||
|
||||
req, _ := http.NewRequest("POST", "/v1/translations/compare", bytes.NewBufferString("invalid json"))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
w := httptest.NewRecorder()
|
||||
router.ServeHTTP(w, req)
|
||||
|
||||
if w.Code != http.StatusBadRequest {
|
||||
t.Errorf("Expected status %d, got %d", http.StatusBadRequest, w.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCompareStructures_InvalidJSON(t *testing.T) {
|
||||
router := setupTestRouter()
|
||||
|
||||
requestBody := CompareStructuresRequest{
|
||||
Before: `{invalid}`,
|
||||
After: `{"valid": true}`,
|
||||
}
|
||||
body, _ := json.Marshal(requestBody)
|
||||
|
||||
req, _ := http.NewRequest("POST", "/v1/translations/compare", bytes.NewBuffer(body))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
w := httptest.NewRecorder()
|
||||
router.ServeHTTP(w, req)
|
||||
|
||||
if w.Code != http.StatusBadRequest {
|
||||
t.Errorf("Expected status %d, got %d for invalid JSON in payload", http.StatusBadRequest, w.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCompareStructures_IdenticalStructures(t *testing.T) {
|
||||
router := setupTestRouter()
|
||||
|
||||
requestBody := CompareStructuresRequest{
|
||||
Before: `{"a": 1, "b": 2}`,
|
||||
After: `{"a": 1, "b": 2}`,
|
||||
}
|
||||
body, _ := json.Marshal(requestBody)
|
||||
|
||||
req, _ := http.NewRequest("POST", "/v1/translations/compare", bytes.NewBuffer(body))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
w := httptest.NewRecorder()
|
||||
router.ServeHTTP(w, req)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Errorf("Expected status %d, got %d", http.StatusOK, w.Code)
|
||||
}
|
||||
|
||||
var comparison translator.StructureComparison
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &comparison); err != nil {
|
||||
t.Fatalf("Failed to parse response: %v", err)
|
||||
}
|
||||
|
||||
if len(comparison.Diffs) != 0 {
|
||||
t.Errorf("Identical structures should have no diffs, got %d", len(comparison.Diffs))
|
||||
}
|
||||
}
|
||||
|
||||
// Helper function
|
||||
func containsString(s, substr string) bool {
|
||||
for i := 0; i <= len(s)-len(substr); i++ {
|
||||
if s[i:i+len(substr)] == substr {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
@@ -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), `"`, "")
|
||||
}
|
||||
|
||||
+1090
-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
|
||||
|
||||
+102
-26
@@ -99,6 +99,10 @@ type Config struct {
|
||||
// OAuthExcludedModels defines per-provider global model exclusions applied to OAuth/file-backed auth entries.
|
||||
OAuthExcludedModels map[string][]string `yaml:"oauth-excluded-models,omitempty" json:"oauth-excluded-models,omitempty"`
|
||||
|
||||
// GlobalModelMappings defines model aliases that apply across all providers.
|
||||
// These are checked before per-credential model mappings.
|
||||
GlobalModelMappings []GlobalModelMapping `yaml:"global-model-mappings,omitempty" json:"global-model-mappings,omitempty"`
|
||||
|
||||
// Payload defines default and override rules for provider payload parameters.
|
||||
Payload PayloadConfig `yaml:"payload" json:"payload"`
|
||||
|
||||
@@ -150,6 +154,28 @@ type RoutingConfig struct {
|
||||
Strategy string `yaml:"strategy,omitempty" json:"strategy,omitempty"`
|
||||
}
|
||||
|
||||
// GlobalModelMapping defines a model alias that applies globally across all providers.
|
||||
// This allows routing friendly names like "smart" to specific models like "claude-opus-4-5-20251101".
|
||||
type GlobalModelMapping struct {
|
||||
// From is the client-requested model name (e.g., "smart", "rush", "opus")
|
||||
From string `yaml:"from" json:"from"`
|
||||
// To is the actual upstream model name (e.g., "claude-opus-4-5-20251101")
|
||||
To string `yaml:"to" json:"to"`
|
||||
// Provider optionally restricts this mapping to a specific provider (e.g., "claude", "codex")
|
||||
// If empty, the mapping applies to all providers.
|
||||
Provider string `yaml:"provider,omitempty" json:"provider,omitempty"`
|
||||
// Enabled allows disabling a mapping without removing it
|
||||
Enabled *bool `yaml:"enabled,omitempty" json:"enabled,omitempty"`
|
||||
}
|
||||
|
||||
// IsEnabled returns whether the mapping is enabled (defaults to true)
|
||||
func (m *GlobalModelMapping) IsEnabled() bool {
|
||||
if m.Enabled == nil {
|
||||
return true
|
||||
}
|
||||
return *m.Enabled
|
||||
}
|
||||
|
||||
// PayloadConfig defines default and override parameter rules applied to provider payloads.
|
||||
type PayloadConfig struct {
|
||||
// Default defines rules that only set parameters when they are missing in the payload.
|
||||
@@ -225,6 +251,9 @@ type CodexKey struct {
|
||||
// ProxyURL overrides the global proxy setting for this API key if provided.
|
||||
ProxyURL string `yaml:"proxy-url" json:"proxy-url"`
|
||||
|
||||
// Models defines upstream model names and aliases for request routing.
|
||||
Models []CodexModel `yaml:"models" json:"models"`
|
||||
|
||||
// Headers optionally adds extra HTTP headers for requests sent with this key.
|
||||
Headers map[string]string `yaml:"headers,omitempty" json:"headers,omitempty"`
|
||||
|
||||
@@ -232,6 +261,15 @@ type CodexKey struct {
|
||||
ExcludedModels []string `yaml:"excluded-models,omitempty" json:"excluded-models,omitempty"`
|
||||
}
|
||||
|
||||
// CodexModel describes a mapping between an alias and the actual upstream model name.
|
||||
type CodexModel struct {
|
||||
// Name is the upstream model identifier used when issuing requests.
|
||||
Name string `yaml:"name" json:"name"`
|
||||
|
||||
// Alias is the client-facing model name that maps to Name.
|
||||
Alias string `yaml:"alias" json:"alias"`
|
||||
}
|
||||
|
||||
// GeminiKey represents the configuration for a Gemini API key,
|
||||
// including optional overrides for upstream base URL, proxy routing, and headers.
|
||||
type GeminiKey struct {
|
||||
@@ -694,6 +732,28 @@ func NormalizeOAuthExcludedModels(entries map[string][]string) map[string][]stri
|
||||
return out
|
||||
}
|
||||
|
||||
// LookupGlobalModelMapping returns the mapped model name if a global mapping exists for the given model.
|
||||
// Returns empty string if no mapping found.
|
||||
func (cfg *Config) LookupGlobalModelMapping(model string, provider string) string {
|
||||
if cfg == nil {
|
||||
return ""
|
||||
}
|
||||
modelLower := strings.ToLower(model)
|
||||
for _, m := range cfg.GlobalModelMappings {
|
||||
if !m.IsEnabled() {
|
||||
continue
|
||||
}
|
||||
if strings.EqualFold(m.From, modelLower) {
|
||||
// Check provider restriction if set
|
||||
if m.Provider != "" && !strings.EqualFold(m.Provider, provider) {
|
||||
continue
|
||||
}
|
||||
return m.To
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// hashSecret hashes the given secret using bcrypt.
|
||||
func hashSecret(secret string) (string, error) {
|
||||
// Use default cost for simplicity.
|
||||
@@ -878,8 +938,8 @@ func getOrCreateMapValue(mapNode *yaml.Node, key string) *yaml.Node {
|
||||
}
|
||||
|
||||
// mergeMappingPreserve merges keys from src into dst mapping node while preserving
|
||||
// key order and comments of existing keys in dst. Unknown keys from src are appended
|
||||
// to dst at the end, copying their node structure from src.
|
||||
// key order and comments of existing keys in dst. New keys are only added if their
|
||||
// value is non-zero to avoid polluting the config with defaults.
|
||||
func mergeMappingPreserve(dst, src *yaml.Node) {
|
||||
if dst == nil || src == nil {
|
||||
return
|
||||
@@ -890,20 +950,19 @@ func mergeMappingPreserve(dst, src *yaml.Node) {
|
||||
copyNodeShallow(dst, src)
|
||||
return
|
||||
}
|
||||
// Build a lookup of existing keys in dst
|
||||
for i := 0; i+1 < len(src.Content); i += 2 {
|
||||
sk := src.Content[i]
|
||||
sv := src.Content[i+1]
|
||||
idx := findMapKeyIndex(dst, sk.Value)
|
||||
if idx >= 0 {
|
||||
// Merge into existing value node
|
||||
// Merge into existing value node (always update, even to zero values)
|
||||
dv := dst.Content[idx+1]
|
||||
mergeNodePreserve(dv, sv)
|
||||
} else {
|
||||
if shouldSkipEmptyCollectionOnPersist(sk.Value, sv) {
|
||||
// New key: only add if value is non-zero to avoid polluting config with defaults
|
||||
if isZeroValueNode(sv) {
|
||||
continue
|
||||
}
|
||||
// Append new key/value pair by deep-copying from src
|
||||
dst.Content = append(dst.Content, deepCopyNode(sk), deepCopyNode(sv))
|
||||
}
|
||||
}
|
||||
@@ -986,32 +1045,49 @@ func findMapKeyIndex(mapNode *yaml.Node, key string) int {
|
||||
return -1
|
||||
}
|
||||
|
||||
func shouldSkipEmptyCollectionOnPersist(key string, node *yaml.Node) bool {
|
||||
switch key {
|
||||
case "generative-language-api-key",
|
||||
"gemini-api-key",
|
||||
"vertex-api-key",
|
||||
"claude-api-key",
|
||||
"codex-api-key",
|
||||
"openai-compatibility":
|
||||
return isEmptyCollectionNode(node)
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
func isEmptyCollectionNode(node *yaml.Node) bool {
|
||||
// isZeroValueNode returns true if the YAML node represents a zero/default value
|
||||
// that should not be written as a new key to preserve config cleanliness.
|
||||
// For mappings and sequences, recursively checks if all children are zero values.
|
||||
func isZeroValueNode(node *yaml.Node) bool {
|
||||
if node == nil {
|
||||
return true
|
||||
}
|
||||
switch node.Kind {
|
||||
case yaml.SequenceNode:
|
||||
return len(node.Content) == 0
|
||||
case yaml.ScalarNode:
|
||||
return node.Tag == "!!null"
|
||||
default:
|
||||
return false
|
||||
switch node.Tag {
|
||||
case "!!bool":
|
||||
return node.Value == "false"
|
||||
case "!!int", "!!float":
|
||||
return node.Value == "0" || node.Value == "0.0"
|
||||
case "!!str":
|
||||
return node.Value == ""
|
||||
case "!!null":
|
||||
return true
|
||||
}
|
||||
case yaml.SequenceNode:
|
||||
if len(node.Content) == 0 {
|
||||
return true
|
||||
}
|
||||
// Check if all elements are zero values
|
||||
for _, child := range node.Content {
|
||||
if !isZeroValueNode(child) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
case yaml.MappingNode:
|
||||
if len(node.Content) == 0 {
|
||||
return true
|
||||
}
|
||||
// Check if all values are zero values (values are at odd indices)
|
||||
for i := 1; i < len(node.Content); i += 2 {
|
||||
if !isZeroValueNode(node.Content[i]) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// deepCopyNode creates a deep copy of a yaml.Node graph.
|
||||
|
||||
@@ -25,18 +25,49 @@ 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.
|
||||
type StreamingConfig struct {
|
||||
// KeepAliveSeconds controls how often the server emits SSE heartbeats (": keep-alive\n\n").
|
||||
// nil means default (15 seconds). <= 0 disables keep-alives.
|
||||
KeepAliveSeconds *int `yaml:"keepalive-seconds,omitempty" json:"keepalive-seconds,omitempty"`
|
||||
// <= 0 disables keep-alives. Default is 0.
|
||||
KeepAliveSeconds int `yaml:"keepalive-seconds,omitempty" json:"keepalive-seconds,omitempty"`
|
||||
|
||||
// BootstrapRetries controls how many times the server may retry a streaming request before any bytes are sent,
|
||||
// to allow auth rotation / transient recovery.
|
||||
// nil means default (2). 0 disables bootstrap retries.
|
||||
BootstrapRetries *int `yaml:"bootstrap-retries,omitempty" json:"bootstrap-retries,omitempty"`
|
||||
// <= 0 disables bootstrap retries. Default is 0.
|
||||
BootstrapRetries int `yaml:"bootstrap-retries,omitempty" json:"bootstrap-retries,omitempty"`
|
||||
|
||||
// MaxChunkSize limits the maximum size (in bytes) of individual response chunks forwarded to clients.
|
||||
// This can reduce latency for large responses by preventing buffer accumulation.
|
||||
// nil means default (65536 = 64KB). 0 disables chunk size limiting.
|
||||
MaxChunkSize *int `yaml:"max-chunk-size,omitempty" json:"max-chunk-size,omitempty"`
|
||||
}
|
||||
|
||||
// CompressionConfig holds context compression behavior configuration.
|
||||
type CompressionConfig struct {
|
||||
// Enabled toggles LLM-based structured summarization.
|
||||
// nil means default (true).
|
||||
Enabled *bool `yaml:"enabled,omitempty" json:"enabled,omitempty"`
|
||||
|
||||
// ThresholdPercent triggers compression when context usage exceeds this fraction.
|
||||
// nil means default (0.75 = 75%).
|
||||
ThresholdPercent *float64 `yaml:"threshold-percent,omitempty" json:"threshold-percent,omitempty"`
|
||||
|
||||
// MaxSummaryTokens limits the output size of generated summaries.
|
||||
// nil means default (2000).
|
||||
MaxSummaryTokens *int `yaml:"max-summary-tokens,omitempty" json:"max-summary-tokens,omitempty"`
|
||||
|
||||
// SummarizationTimeoutSeconds is the timeout for LLM summarization calls.
|
||||
// nil means default (30).
|
||||
SummarizationTimeoutSeconds *int `yaml:"summarization-timeout-seconds,omitempty" json:"summarization-timeout-seconds,omitempty"`
|
||||
|
||||
// FallbackToRegex uses regex-based summarization when LLM fails.
|
||||
// nil means default (true).
|
||||
FallbackToRegex *bool `yaml:"fallback-to-regex,omitempty" json:"fallback-to-regex,omitempty"`
|
||||
}
|
||||
|
||||
// AccessConfig groups request authentication providers.
|
||||
@@ -100,3 +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") {
|
||||
|
||||
@@ -50,6 +50,16 @@ func (e *CodexExecutor) Execute(ctx context.Context, auth *cliproxyauth.Auth, re
|
||||
defer reporter.trackFailure(ctx, &err)
|
||||
|
||||
upstreamModel := util.ResolveOriginalModel(req.Model, req.Metadata)
|
||||
if upstreamModel == "" {
|
||||
upstreamModel = req.Model
|
||||
}
|
||||
if modelOverride := e.resolveUpstreamModel(upstreamModel, auth); modelOverride != "" {
|
||||
upstreamModel = modelOverride
|
||||
} else if !strings.EqualFold(upstreamModel, req.Model) {
|
||||
if modelOverride := e.resolveUpstreamModel(req.Model, auth); modelOverride != "" {
|
||||
upstreamModel = modelOverride
|
||||
}
|
||||
}
|
||||
|
||||
from := opts.SourceFormat
|
||||
to := sdktranslator.FromString("codex")
|
||||
@@ -147,6 +157,16 @@ func (e *CodexExecutor) ExecuteStream(ctx context.Context, auth *cliproxyauth.Au
|
||||
defer reporter.trackFailure(ctx, &err)
|
||||
|
||||
upstreamModel := util.ResolveOriginalModel(req.Model, req.Metadata)
|
||||
if upstreamModel == "" {
|
||||
upstreamModel = req.Model
|
||||
}
|
||||
if modelOverride := e.resolveUpstreamModel(upstreamModel, auth); modelOverride != "" {
|
||||
upstreamModel = modelOverride
|
||||
} else if !strings.EqualFold(upstreamModel, req.Model) {
|
||||
if modelOverride := e.resolveUpstreamModel(req.Model, auth); modelOverride != "" {
|
||||
upstreamModel = modelOverride
|
||||
}
|
||||
}
|
||||
|
||||
from := opts.SourceFormat
|
||||
to := sdktranslator.FromString("codex")
|
||||
@@ -247,12 +267,22 @@ func (e *CodexExecutor) ExecuteStream(ctx context.Context, auth *cliproxyauth.Au
|
||||
|
||||
func (e *CodexExecutor) CountTokens(ctx context.Context, auth *cliproxyauth.Auth, req cliproxyexecutor.Request, opts cliproxyexecutor.Options) (cliproxyexecutor.Response, error) {
|
||||
upstreamModel := util.ResolveOriginalModel(req.Model, req.Metadata)
|
||||
if upstreamModel == "" {
|
||||
upstreamModel = req.Model
|
||||
}
|
||||
if modelOverride := e.resolveUpstreamModel(upstreamModel, auth); modelOverride != "" {
|
||||
upstreamModel = modelOverride
|
||||
} else if !strings.EqualFold(upstreamModel, req.Model) {
|
||||
if modelOverride := e.resolveUpstreamModel(req.Model, auth); modelOverride != "" {
|
||||
upstreamModel = modelOverride
|
||||
}
|
||||
}
|
||||
|
||||
from := opts.SourceFormat
|
||||
to := sdktranslator.FromString("codex")
|
||||
body := sdktranslator.TranslateRequest(from, to, req.Model, bytes.Clone(req.Payload), false)
|
||||
|
||||
modelForCounting := req.Model
|
||||
modelForCounting := upstreamModel
|
||||
|
||||
body = ApplyReasoningEffortMetadata(body, req.Metadata, req.Model, "reasoning.effort", false)
|
||||
body, _ = sjson.SetBytes(body, "model", upstreamModel)
|
||||
@@ -525,3 +555,87 @@ func codexCreds(a *cliproxyauth.Auth) (apiKey, baseURL string) {
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (e *CodexExecutor) resolveUpstreamModel(alias string, auth *cliproxyauth.Auth) string {
|
||||
trimmed := strings.TrimSpace(alias)
|
||||
if trimmed == "" {
|
||||
return ""
|
||||
}
|
||||
|
||||
entry := e.resolveCodexConfig(auth)
|
||||
if entry == nil {
|
||||
return ""
|
||||
}
|
||||
|
||||
normalizedModel, metadata := util.NormalizeThinkingModel(trimmed)
|
||||
|
||||
// Candidate names to match against configured aliases/names.
|
||||
candidates := []string{strings.TrimSpace(normalizedModel)}
|
||||
if !strings.EqualFold(normalizedModel, trimmed) {
|
||||
candidates = append(candidates, trimmed)
|
||||
}
|
||||
if original := util.ResolveOriginalModel(normalizedModel, metadata); original != "" && !strings.EqualFold(original, normalizedModel) {
|
||||
candidates = append(candidates, original)
|
||||
}
|
||||
|
||||
for i := range entry.Models {
|
||||
model := entry.Models[i]
|
||||
name := strings.TrimSpace(model.Name)
|
||||
modelAlias := strings.TrimSpace(model.Alias)
|
||||
|
||||
for _, candidate := range candidates {
|
||||
if candidate == "" {
|
||||
continue
|
||||
}
|
||||
if modelAlias != "" && strings.EqualFold(modelAlias, candidate) {
|
||||
if name != "" {
|
||||
return name
|
||||
}
|
||||
return candidate
|
||||
}
|
||||
if name != "" && strings.EqualFold(name, candidate) {
|
||||
return name
|
||||
}
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (e *CodexExecutor) resolveCodexConfig(auth *cliproxyauth.Auth) *config.CodexKey {
|
||||
if auth == nil || e.cfg == nil {
|
||||
return nil
|
||||
}
|
||||
var attrKey, attrBase string
|
||||
if auth.Attributes != nil {
|
||||
attrKey = strings.TrimSpace(auth.Attributes["api_key"])
|
||||
attrBase = strings.TrimSpace(auth.Attributes["base_url"])
|
||||
}
|
||||
for i := range e.cfg.CodexKey {
|
||||
entry := &e.cfg.CodexKey[i]
|
||||
cfgKey := strings.TrimSpace(entry.APIKey)
|
||||
cfgBase := strings.TrimSpace(entry.BaseURL)
|
||||
if attrKey != "" && attrBase != "" {
|
||||
if strings.EqualFold(cfgKey, attrKey) && strings.EqualFold(cfgBase, attrBase) {
|
||||
return entry
|
||||
}
|
||||
continue
|
||||
}
|
||||
if attrKey != "" && strings.EqualFold(cfgKey, attrKey) {
|
||||
if cfgBase == "" || strings.EqualFold(cfgBase, attrBase) {
|
||||
return entry
|
||||
}
|
||||
}
|
||||
if attrKey == "" && attrBase != "" && strings.EqualFold(cfgBase, attrBase) {
|
||||
return entry
|
||||
}
|
||||
}
|
||||
if attrKey != "" {
|
||||
for i := range e.cfg.CodexKey {
|
||||
entry := &e.cfg.CodexKey[i]
|
||||
if strings.EqualFold(strings.TrimSpace(entry.APIKey), attrKey) {
|
||||
return entry
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -67,6 +67,7 @@ func (e *IFlowExecutor) Execute(ctx context.Context, auth *cliproxyauth.Auth, re
|
||||
return resp, errValidate
|
||||
}
|
||||
body = applyIFlowThinkingConfig(body)
|
||||
body = preserveReasoningContentInMessages(body)
|
||||
body = applyPayloadConfig(e.cfg, req.Model, body)
|
||||
|
||||
endpoint := strings.TrimSuffix(baseURL, "/") + iflowDefaultEndpoint
|
||||
@@ -159,6 +160,7 @@ func (e *IFlowExecutor) ExecuteStream(ctx context.Context, auth *cliproxyauth.Au
|
||||
return nil, errValidate
|
||||
}
|
||||
body = applyIFlowThinkingConfig(body)
|
||||
body = preserveReasoningContentInMessages(body)
|
||||
// Ensure tools array exists to avoid provider quirks similar to Qwen's behaviour.
|
||||
toolsResult := gjson.GetBytes(body, "tools")
|
||||
if toolsResult.Exists() && toolsResult.IsArray() && len(toolsResult.Array()) == 0 {
|
||||
@@ -451,20 +453,98 @@ func ensureToolsArray(body []byte) []byte {
|
||||
return updated
|
||||
}
|
||||
|
||||
// applyIFlowThinkingConfig converts normalized reasoning_effort to iFlow chat_template_kwargs.enable_thinking.
|
||||
// This should be called after NormalizeThinkingConfig has processed the payload.
|
||||
// iFlow only supports boolean enable_thinking, so any non-"none" effort enables thinking.
|
||||
func applyIFlowThinkingConfig(body []byte) []byte {
|
||||
effort := gjson.GetBytes(body, "reasoning_effort")
|
||||
if !effort.Exists() {
|
||||
// preserveReasoningContentInMessages ensures reasoning_content from assistant messages in the
|
||||
// conversation history is preserved when sending to iFlow models that support thinking.
|
||||
// This is critical for multi-turn conversations where the model needs to see its previous
|
||||
// reasoning to maintain coherent thought chains across tool calls and conversation turns.
|
||||
//
|
||||
// For GLM-4.7 and MiniMax-M2.1, the full assistant response (including reasoning) must be
|
||||
// appended back into message history before the next call.
|
||||
func preserveReasoningContentInMessages(body []byte) []byte {
|
||||
model := strings.ToLower(gjson.GetBytes(body, "model").String())
|
||||
|
||||
// Only apply to models that support thinking with history preservation
|
||||
needsPreservation := strings.HasPrefix(model, "glm-4.7") ||
|
||||
strings.HasPrefix(model, "glm-4-7") ||
|
||||
strings.HasPrefix(model, "minimax-m2.1") ||
|
||||
strings.HasPrefix(model, "minimax-m2-1")
|
||||
|
||||
if !needsPreservation {
|
||||
return body
|
||||
}
|
||||
|
||||
val := strings.ToLower(strings.TrimSpace(effort.String()))
|
||||
enableThinking := val != "none" && val != ""
|
||||
messages := gjson.GetBytes(body, "messages")
|
||||
if !messages.Exists() || !messages.IsArray() {
|
||||
return body
|
||||
}
|
||||
|
||||
body, _ = sjson.DeleteBytes(body, "reasoning_effort")
|
||||
body, _ = sjson.SetBytes(body, "chat_template_kwargs.enable_thinking", enableThinking)
|
||||
// Check if any assistant message already has reasoning_content preserved
|
||||
hasReasoningContent := false
|
||||
messages.ForEach(func(_, msg gjson.Result) bool {
|
||||
role := msg.Get("role").String()
|
||||
if role == "assistant" {
|
||||
rc := msg.Get("reasoning_content")
|
||||
if rc.Exists() && rc.String() != "" {
|
||||
hasReasoningContent = true
|
||||
return false // stop iteration
|
||||
}
|
||||
}
|
||||
return true
|
||||
})
|
||||
|
||||
// If reasoning content is already present, the messages are properly formatted
|
||||
// No need to modify - the client has correctly preserved reasoning in history
|
||||
if hasReasoningContent {
|
||||
log.Debugf("iflow executor: reasoning_content found in message history for %s", model)
|
||||
}
|
||||
|
||||
return body
|
||||
}
|
||||
|
||||
// applyIFlowThinkingConfig converts normalized reasoning_effort to model-specific thinking configurations.
|
||||
// This should be called after NormalizeThinkingConfig has processed the payload.
|
||||
//
|
||||
// Model-specific handling:
|
||||
// - GLM-4.7: Uses extra_body={"thinking": {"type": "enabled"}, "clear_thinking": false}
|
||||
// - MiniMax-M2.1: Uses reasoning_split=true for OpenAI-style reasoning separation
|
||||
// - Other iFlow models: Uses chat_template_kwargs.enable_thinking (boolean)
|
||||
func applyIFlowThinkingConfig(body []byte) []byte {
|
||||
effort := gjson.GetBytes(body, "reasoning_effort")
|
||||
model := strings.ToLower(gjson.GetBytes(body, "model").String())
|
||||
|
||||
// Check if thinking should be enabled
|
||||
val := ""
|
||||
if effort.Exists() {
|
||||
val = strings.ToLower(strings.TrimSpace(effort.String()))
|
||||
}
|
||||
enableThinking := effort.Exists() && val != "none" && val != ""
|
||||
|
||||
// Remove reasoning_effort as we'll convert to model-specific format
|
||||
if effort.Exists() {
|
||||
body, _ = sjson.DeleteBytes(body, "reasoning_effort")
|
||||
}
|
||||
|
||||
// GLM-4.7: Use extra_body with thinking config and clear_thinking: false
|
||||
if strings.HasPrefix(model, "glm-4.7") || strings.HasPrefix(model, "glm-4-7") {
|
||||
if enableThinking {
|
||||
body, _ = sjson.SetBytes(body, "extra_body.thinking.type", "enabled")
|
||||
body, _ = sjson.SetBytes(body, "extra_body.clear_thinking", false)
|
||||
}
|
||||
return body
|
||||
}
|
||||
|
||||
// MiniMax-M2.1: Use reasoning_split=true for interleaved thinking
|
||||
if strings.HasPrefix(model, "minimax-m2.1") || strings.HasPrefix(model, "minimax-m2-1") {
|
||||
if enableThinking {
|
||||
body, _ = sjson.SetBytes(body, "reasoning_split", true)
|
||||
}
|
||||
return body
|
||||
}
|
||||
|
||||
// Other iFlow models (including GLM-4.6): Use chat_template_kwargs.enable_thinking
|
||||
if effort.Exists() {
|
||||
body, _ = sjson.SetBytes(body, "chat_template_kwargs.enable_thinking", enableThinking)
|
||||
}
|
||||
|
||||
return body
|
||||
}
|
||||
|
||||
@@ -495,3 +495,78 @@ func addIfNotEmpty(segments *[]string, value string) {
|
||||
*segments = append(*segments, trimmed)
|
||||
}
|
||||
}
|
||||
|
||||
// EstimateRequestTokens estimates total tokens for a request payload.
|
||||
// It auto-detects the format (OpenAI chat, Claude, etc.) and counts accordingly.
|
||||
func EstimateRequestTokens(model string, payload []byte) (int64, error) {
|
||||
enc, err := getTokenizer(model)
|
||||
if err != nil {
|
||||
// Fallback: estimate 4 chars per token
|
||||
return int64(len(payload) / 4), nil
|
||||
}
|
||||
|
||||
// Detect format and count accordingly
|
||||
if gjson.GetBytes(payload, "messages").Exists() {
|
||||
if gjson.GetBytes(payload, "system").Exists() {
|
||||
// Claude format
|
||||
return countClaudeChatTokens(enc, payload)
|
||||
}
|
||||
// OpenAI chat format
|
||||
return countOpenAIChatTokens(enc, payload)
|
||||
}
|
||||
|
||||
// OpenAI responses format or other - use general text counting
|
||||
count, err := enc.Count(string(payload))
|
||||
if err != nil {
|
||||
return int64(len(payload) / 4), nil
|
||||
}
|
||||
return int64(count), nil
|
||||
}
|
||||
|
||||
// CalculateCompressionBudget determines how many tokens should be compressed
|
||||
// to bring context usage under the target percentage.
|
||||
func CalculateCompressionBudget(currentTokens int64, contextWindow int, targetPercent float64) int64 {
|
||||
if contextWindow <= 0 || targetPercent <= 0 || targetPercent >= 1 {
|
||||
return 0
|
||||
}
|
||||
targetTokens := int64(float64(contextWindow) * targetPercent)
|
||||
if currentTokens <= targetTokens {
|
||||
return 0
|
||||
}
|
||||
return currentTokens - targetTokens
|
||||
}
|
||||
|
||||
// ShouldTriggerCompression checks if compression should be triggered based on
|
||||
// current token usage vs context window size.
|
||||
func ShouldTriggerCompression(currentTokens int64, contextWindow int, thresholdPercent float64) bool {
|
||||
if contextWindow <= 0 || thresholdPercent <= 0 {
|
||||
return false
|
||||
}
|
||||
ratio := float64(currentTokens) / float64(contextWindow)
|
||||
return ratio >= thresholdPercent
|
||||
}
|
||||
|
||||
// GetDefaultContextWindow returns a conservative default context window for a model.
|
||||
func GetDefaultContextWindow(model string) int {
|
||||
lowerModel := strings.ToLower(model)
|
||||
switch {
|
||||
case strings.Contains(lowerModel, "claude-3.5"), strings.Contains(lowerModel, "claude-3-5"):
|
||||
return 200000
|
||||
case strings.Contains(lowerModel, "claude-3"):
|
||||
return 200000
|
||||
case strings.Contains(lowerModel, "claude"):
|
||||
return 100000
|
||||
case strings.Contains(lowerModel, "gpt-4-turbo"), strings.Contains(lowerModel, "gpt-4o"):
|
||||
return 128000
|
||||
case strings.Contains(lowerModel, "gpt-4"):
|
||||
return 8192
|
||||
case strings.Contains(lowerModel, "gpt-3.5"):
|
||||
return 16384
|
||||
case strings.Contains(lowerModel, "gemini"):
|
||||
return 1000000
|
||||
case strings.Contains(lowerModel, "o1"), strings.Contains(lowerModel, "o3"):
|
||||
return 128000
|
||||
default:
|
||||
return 100000 // Conservative default
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,7 +19,7 @@ type usageReporter struct {
|
||||
provider string
|
||||
model string
|
||||
authID string
|
||||
authIndex uint64
|
||||
authIndex string
|
||||
apiKey string
|
||||
source string
|
||||
requestedAt time.Time
|
||||
@@ -482,12 +482,16 @@ func StripUsageMetadataFromJSON(rawJSON []byte) ([]byte, bool) {
|
||||
cleaned := jsonBytes
|
||||
var changed bool
|
||||
|
||||
if gjson.GetBytes(cleaned, "usageMetadata").Exists() {
|
||||
if usageMetadata = gjson.GetBytes(cleaned, "usageMetadata"); usageMetadata.Exists() {
|
||||
// Rename usageMetadata to cpaUsageMetadata in the message_start event of Claude
|
||||
cleaned, _ = sjson.SetRawBytes(cleaned, "cpaUsageMetadata", []byte(usageMetadata.Raw))
|
||||
cleaned, _ = sjson.DeleteBytes(cleaned, "usageMetadata")
|
||||
changed = true
|
||||
}
|
||||
|
||||
if gjson.GetBytes(cleaned, "response.usageMetadata").Exists() {
|
||||
if usageMetadata = gjson.GetBytes(cleaned, "response.usageMetadata"); usageMetadata.Exists() {
|
||||
// Rename usageMetadata to cpaUsageMetadata in the message_start event of Claude
|
||||
cleaned, _ = sjson.SetRawBytes(cleaned, "response.cpaUsageMetadata", []byte(usageMetadata.Raw))
|
||||
cleaned, _ = sjson.DeleteBytes(cleaned, "response.usageMetadata")
|
||||
changed = true
|
||||
}
|
||||
|
||||
@@ -99,6 +99,14 @@ func ConvertAntigravityResponseToClaude(_ context.Context, _ string, originalReq
|
||||
// This follows the Claude Code API specification for streaming message initialization
|
||||
messageStartTemplate := `{"type": "message_start", "message": {"id": "msg_1nZdL29xx5MUA1yADyHTEsnR8uuvGzszyY", "type": "message", "role": "assistant", "content": [], "model": "claude-3-5-sonnet-20241022", "stop_reason": null, "stop_sequence": null, "usage": {"input_tokens": 0, "output_tokens": 0}}}`
|
||||
|
||||
// Use cpaUsageMetadata within the message_start event for Claude.
|
||||
if promptTokenCount := gjson.GetBytes(rawJSON, "response.cpaUsageMetadata.promptTokenCount"); promptTokenCount.Exists() {
|
||||
messageStartTemplate, _ = sjson.Set(messageStartTemplate, "message.usage.input_tokens", promptTokenCount.Int())
|
||||
}
|
||||
if candidatesTokenCount := gjson.GetBytes(rawJSON, "response.cpaUsageMetadata.candidatesTokenCount"); candidatesTokenCount.Exists() {
|
||||
messageStartTemplate, _ = sjson.Set(messageStartTemplate, "message.usage.output_tokens", candidatesTokenCount.Int())
|
||||
}
|
||||
|
||||
// Override default values with actual response metadata if available from the Gemini CLI response
|
||||
if modelVersionResult := gjson.GetBytes(rawJSON, "response.modelVersion"); modelVersionResult.Exists() {
|
||||
messageStartTemplate, _ = sjson.Set(messageStartTemplate, "message.model", modelVersionResult.String())
|
||||
|
||||
+24
-2
@@ -247,10 +247,30 @@ func ConvertOpenAIRequestToAntigravity(modelName string, inputRawJSON []byte, _
|
||||
} else if role == "assistant" {
|
||||
node := []byte(`{"role":"model","parts":[]}`)
|
||||
p := 0
|
||||
if content.Type == gjson.String {
|
||||
if content.Type == gjson.String && content.String() != "" {
|
||||
node, _ = sjson.SetBytes(node, "parts.-1.text", content.String())
|
||||
out, _ = sjson.SetRawBytes(out, "request.contents.-1", node)
|
||||
p++
|
||||
} else if content.IsArray() {
|
||||
// Assistant multimodal content (e.g. text + image) -> single model content with parts
|
||||
for _, item := range content.Array() {
|
||||
switch item.Get("type").String() {
|
||||
case "text":
|
||||
p++
|
||||
case "image_url":
|
||||
// If the assistant returned an inline data URL, preserve it for history fidelity.
|
||||
imageURL := item.Get("image_url.url").String()
|
||||
if len(imageURL) > 5 { // expect data:...
|
||||
pieces := strings.SplitN(imageURL[5:], ";", 2)
|
||||
if len(pieces) == 2 && len(pieces[1]) > 7 {
|
||||
mime := pieces[0]
|
||||
data := pieces[1][7:]
|
||||
node, _ = sjson.SetBytes(node, "parts."+itoa(p)+".inlineData.mime_type", mime)
|
||||
node, _ = sjson.SetBytes(node, "parts."+itoa(p)+".inlineData.data", data)
|
||||
p++
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Tool calls -> single model content with functionCall parts
|
||||
@@ -305,6 +325,8 @@ func ConvertOpenAIRequestToAntigravity(modelName string, inputRawJSON []byte, _
|
||||
if pp > 0 {
|
||||
out, _ = sjson.SetRawBytes(out, "request.contents.-1", toolNode)
|
||||
}
|
||||
} else {
|
||||
out, _ = sjson.SetRawBytes(out, "request.contents.-1", node)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+4
-2
@@ -181,12 +181,14 @@ func ConvertAntigravityResponseToOpenAI(_ context.Context, _ string, originalReq
|
||||
mimeType = "image/png"
|
||||
}
|
||||
imageURL := fmt.Sprintf("data:%s;base64,%s", mimeType, data)
|
||||
imagePayload := `{"image_url":{"url":""},"type":"image_url"}`
|
||||
imagePayload, _ = sjson.Set(imagePayload, "image_url.url", imageURL)
|
||||
imagesResult := gjson.Get(template, "choices.0.delta.images")
|
||||
if !imagesResult.Exists() || !imagesResult.IsArray() {
|
||||
template, _ = sjson.SetRaw(template, "choices.0.delta.images", `[]`)
|
||||
}
|
||||
imageIndex := len(gjson.Get(template, "choices.0.delta.images").Array())
|
||||
imagePayload := `{"type":"image_url","image_url":{"url":""}}`
|
||||
imagePayload, _ = sjson.Set(imagePayload, "index", imageIndex)
|
||||
imagePayload, _ = sjson.Set(imagePayload, "image_url.url", imageURL)
|
||||
template, _ = sjson.Set(template, "choices.0.delta.role", "assistant")
|
||||
template, _ = sjson.SetRaw(template, "choices.0.delta.images.-1", imagePayload)
|
||||
}
|
||||
|
||||
@@ -209,9 +209,12 @@ func ConvertClaudeResponseToOpenAI(_ context.Context, modelName string, original
|
||||
if usage := root.Get("usage"); usage.Exists() {
|
||||
inputTokens := usage.Get("input_tokens").Int()
|
||||
outputTokens := usage.Get("output_tokens").Int()
|
||||
template, _ = sjson.Set(template, "usage.prompt_tokens", inputTokens)
|
||||
cacheReadInputTokens := usage.Get("cache_read_input_tokens").Int()
|
||||
cacheCreationInputTokens := usage.Get("cache_creation_input_tokens").Int()
|
||||
template, _ = sjson.Set(template, "usage.prompt_tokens", inputTokens+cacheCreationInputTokens)
|
||||
template, _ = sjson.Set(template, "usage.completion_tokens", outputTokens)
|
||||
template, _ = sjson.Set(template, "usage.total_tokens", inputTokens+outputTokens)
|
||||
template, _ = sjson.Set(template, "usage.prompt_tokens_details.cached_tokens", cacheReadInputTokens)
|
||||
}
|
||||
return []string{template}
|
||||
|
||||
@@ -285,8 +288,6 @@ func ConvertClaudeResponseToOpenAINonStream(_ context.Context, _ string, origina
|
||||
var messageID string
|
||||
var model string
|
||||
var createdAt int64
|
||||
var inputTokens, outputTokens int64
|
||||
var reasoningTokens int64
|
||||
var stopReason string
|
||||
var contentParts []string
|
||||
var reasoningParts []string
|
||||
@@ -303,9 +304,6 @@ func ConvertClaudeResponseToOpenAINonStream(_ context.Context, _ string, origina
|
||||
messageID = message.Get("id").String()
|
||||
model = message.Get("model").String()
|
||||
createdAt = time.Now().Unix()
|
||||
if usage := message.Get("usage"); usage.Exists() {
|
||||
inputTokens = usage.Get("input_tokens").Int()
|
||||
}
|
||||
}
|
||||
|
||||
case "content_block_start":
|
||||
@@ -368,11 +366,14 @@ func ConvertClaudeResponseToOpenAINonStream(_ context.Context, _ string, origina
|
||||
}
|
||||
}
|
||||
if usage := root.Get("usage"); usage.Exists() {
|
||||
outputTokens = usage.Get("output_tokens").Int()
|
||||
// Estimate reasoning tokens from accumulated thinking content
|
||||
if len(reasoningParts) > 0 {
|
||||
reasoningTokens = int64(len(strings.Join(reasoningParts, "")) / 4) // Rough estimation
|
||||
}
|
||||
inputTokens := usage.Get("input_tokens").Int()
|
||||
outputTokens := usage.Get("output_tokens").Int()
|
||||
cacheReadInputTokens := usage.Get("cache_read_input_tokens").Int()
|
||||
cacheCreationInputTokens := usage.Get("cache_creation_input_tokens").Int()
|
||||
out, _ = sjson.Set(out, "usage.prompt_tokens", inputTokens+cacheCreationInputTokens)
|
||||
out, _ = sjson.Set(out, "usage.completion_tokens", outputTokens)
|
||||
out, _ = sjson.Set(out, "usage.total_tokens", inputTokens+outputTokens)
|
||||
out, _ = sjson.Set(out, "usage.prompt_tokens_details.cached_tokens", cacheReadInputTokens)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -431,16 +432,5 @@ func ConvertClaudeResponseToOpenAINonStream(_ context.Context, _ string, origina
|
||||
out, _ = sjson.Set(out, "choices.0.finish_reason", mapAnthropicStopReasonToOpenAI(stopReason))
|
||||
}
|
||||
|
||||
// Set usage information including prompt tokens, completion tokens, and total tokens
|
||||
totalTokens := inputTokens + outputTokens
|
||||
out, _ = sjson.Set(out, "usage.prompt_tokens", inputTokens)
|
||||
out, _ = sjson.Set(out, "usage.completion_tokens", outputTokens)
|
||||
out, _ = sjson.Set(out, "usage.total_tokens", totalTokens)
|
||||
|
||||
// Add reasoning tokens to usage details if any reasoning content was processed
|
||||
if reasoningTokens > 0 {
|
||||
out, _ = sjson.Set(out, "usage.completion_tokens_details.reasoning_tokens", reasoningTokens)
|
||||
}
|
||||
|
||||
return out
|
||||
}
|
||||
|
||||
@@ -114,13 +114,16 @@ func ConvertOpenAIResponsesRequestToClaude(modelName string, inputRawJSON []byte
|
||||
var builder strings.Builder
|
||||
if parts := item.Get("content"); parts.Exists() && parts.IsArray() {
|
||||
parts.ForEach(func(_, part gjson.Result) bool {
|
||||
text := part.Get("text").String()
|
||||
textResult := part.Get("text")
|
||||
text := textResult.String()
|
||||
if builder.Len() > 0 && text != "" {
|
||||
builder.WriteByte('\n')
|
||||
}
|
||||
builder.WriteString(text)
|
||||
return true
|
||||
})
|
||||
} else if parts.Type == gjson.String {
|
||||
builder.WriteString(parts.String())
|
||||
}
|
||||
instructionsText = builder.String()
|
||||
if instructionsText != "" {
|
||||
@@ -207,6 +210,8 @@ func ConvertOpenAIResponsesRequestToClaude(modelName string, inputRawJSON []byte
|
||||
}
|
||||
return true
|
||||
})
|
||||
} else if parts.Type == gjson.String {
|
||||
textAggregate.WriteString(parts.String())
|
||||
}
|
||||
|
||||
// Fallback to given role if content types not decisive
|
||||
|
||||
+24
-1
@@ -218,8 +218,29 @@ func ConvertOpenAIRequestToGeminiCLI(modelName string, inputRawJSON []byte, _ bo
|
||||
if content.Type == gjson.String {
|
||||
// Assistant text -> single model content
|
||||
node, _ = sjson.SetBytes(node, "parts.-1.text", content.String())
|
||||
out, _ = sjson.SetRawBytes(out, "request.contents.-1", node)
|
||||
p++
|
||||
} else if content.IsArray() {
|
||||
// Assistant multimodal content (e.g. text + image) -> single model content with parts
|
||||
for _, item := range content.Array() {
|
||||
switch item.Get("type").String() {
|
||||
case "text":
|
||||
node, _ = sjson.SetBytes(node, "parts."+itoa(p)+".text", item.Get("text").String())
|
||||
p++
|
||||
case "image_url":
|
||||
// If the assistant returned an inline data URL, preserve it for history fidelity.
|
||||
imageURL := item.Get("image_url.url").String()
|
||||
if len(imageURL) > 5 { // expect data:...
|
||||
pieces := strings.SplitN(imageURL[5:], ";", 2)
|
||||
if len(pieces) == 2 && len(pieces[1]) > 7 {
|
||||
mime := pieces[0]
|
||||
data := pieces[1][7:]
|
||||
node, _ = sjson.SetBytes(node, "parts."+itoa(p)+".inlineData.mime_type", mime)
|
||||
node, _ = sjson.SetBytes(node, "parts."+itoa(p)+".inlineData.data", data)
|
||||
p++
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Tool calls -> single model content with functionCall parts
|
||||
@@ -260,6 +281,8 @@ func ConvertOpenAIRequestToGeminiCLI(modelName string, inputRawJSON []byte, _ bo
|
||||
if pp > 0 {
|
||||
out, _ = sjson.SetRawBytes(out, "request.contents.-1", toolNode)
|
||||
}
|
||||
} else {
|
||||
out, _ = sjson.SetRawBytes(out, "request.contents.-1", node)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+4
-2
@@ -185,12 +185,14 @@ func ConvertCliResponseToOpenAI(_ context.Context, _ string, originalRequestRawJ
|
||||
mimeType = "image/png"
|
||||
}
|
||||
imageURL := fmt.Sprintf("data:%s;base64,%s", mimeType, data)
|
||||
imagePayload := `{"image_url":{"url":""},"type":"image_url"}`
|
||||
imagePayload, _ = sjson.Set(imagePayload, "image_url.url", imageURL)
|
||||
imagesResult := gjson.Get(template, "choices.0.delta.images")
|
||||
if !imagesResult.Exists() || !imagesResult.IsArray() {
|
||||
template, _ = sjson.SetRaw(template, "choices.0.delta.images", `[]`)
|
||||
}
|
||||
imageIndex := len(gjson.Get(template, "choices.0.delta.images").Array())
|
||||
imagePayload := `{"type":"image_url","image_url":{"url":""}}`
|
||||
imagePayload, _ = sjson.Set(imagePayload, "index", imageIndex)
|
||||
imagePayload, _ = sjson.Set(imagePayload, "image_url.url", imageURL)
|
||||
template, _ = sjson.Set(template, "choices.0.delta.role", "assistant")
|
||||
template, _ = sjson.SetRaw(template, "choices.0.delta.images.-1", imagePayload)
|
||||
}
|
||||
|
||||
@@ -233,18 +233,15 @@ func ConvertOpenAIRequestToGemini(modelName string, inputRawJSON []byte, _ bool)
|
||||
} else if role == "assistant" {
|
||||
node := []byte(`{"role":"model","parts":[]}`)
|
||||
p := 0
|
||||
|
||||
if content.Type == gjson.String {
|
||||
// Assistant text -> single model content
|
||||
node, _ = sjson.SetBytes(node, "parts.-1.text", content.String())
|
||||
out, _ = sjson.SetRawBytes(out, "contents.-1", node)
|
||||
p++
|
||||
} else if content.IsArray() {
|
||||
// Assistant multimodal content (e.g. text + image) -> single model content with parts
|
||||
for _, item := range content.Array() {
|
||||
switch item.Get("type").String() {
|
||||
case "text":
|
||||
node, _ = sjson.SetBytes(node, "parts."+itoa(p)+".text", item.Get("text").String())
|
||||
p++
|
||||
case "image_url":
|
||||
// If the assistant returned an inline data URL, preserve it for history fidelity.
|
||||
@@ -261,7 +258,6 @@ func ConvertOpenAIRequestToGemini(modelName string, inputRawJSON []byte, _ bool)
|
||||
}
|
||||
}
|
||||
}
|
||||
out, _ = sjson.SetRawBytes(out, "contents.-1", node)
|
||||
}
|
||||
|
||||
// Tool calls -> single model content with functionCall parts
|
||||
@@ -302,6 +298,8 @@ func ConvertOpenAIRequestToGemini(modelName string, inputRawJSON []byte, _ bool)
|
||||
if pp > 0 {
|
||||
out, _ = sjson.SetRawBytes(out, "contents.-1", toolNode)
|
||||
}
|
||||
} else {
|
||||
out, _ = sjson.SetRawBytes(out, "contents.-1", node)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -182,12 +182,14 @@ func ConvertGeminiResponseToOpenAI(_ context.Context, _ string, originalRequestR
|
||||
mimeType = "image/png"
|
||||
}
|
||||
imageURL := fmt.Sprintf("data:%s;base64,%s", mimeType, data)
|
||||
imagePayload := `{"image_url":{"url":""},"type":"image_url"}`
|
||||
imagePayload, _ = sjson.Set(imagePayload, "image_url.url", imageURL)
|
||||
imagesResult := gjson.Get(template, "choices.0.delta.images")
|
||||
if !imagesResult.Exists() || !imagesResult.IsArray() {
|
||||
template, _ = sjson.SetRaw(template, "choices.0.delta.images", `[]`)
|
||||
}
|
||||
imageIndex := len(gjson.Get(template, "choices.0.delta.images").Array())
|
||||
imagePayload := `{"type":"image_url","image_url":{"url":""}}`
|
||||
imagePayload, _ = sjson.Set(imagePayload, "index", imageIndex)
|
||||
imagePayload, _ = sjson.Set(imagePayload, "image_url.url", imageURL)
|
||||
template, _ = sjson.Set(template, "choices.0.delta.role", "assistant")
|
||||
template, _ = sjson.SetRaw(template, "choices.0.delta.images.-1", imagePayload)
|
||||
}
|
||||
@@ -316,12 +318,14 @@ func ConvertGeminiResponseToOpenAINonStream(_ context.Context, _ string, origina
|
||||
mimeType = "image/png"
|
||||
}
|
||||
imageURL := fmt.Sprintf("data:%s;base64,%s", mimeType, data)
|
||||
imagePayload := `{"image_url":{"url":""},"type":"image_url"}`
|
||||
imagePayload, _ = sjson.Set(imagePayload, "image_url.url", imageURL)
|
||||
imagesResult := gjson.Get(template, "choices.0.message.images")
|
||||
if !imagesResult.Exists() || !imagesResult.IsArray() {
|
||||
template, _ = sjson.SetRaw(template, "choices.0.message.images", `[]`)
|
||||
}
|
||||
imageIndex := len(gjson.Get(template, "choices.0.message.images").Array())
|
||||
imagePayload := `{"type":"image_url","image_url":{"url":""}}`
|
||||
imagePayload, _ = sjson.Set(imagePayload, "index", imageIndex)
|
||||
imagePayload, _ = sjson.Set(imagePayload, "image_url.url", imageURL)
|
||||
template, _ = sjson.Set(template, "choices.0.message.role", "assistant")
|
||||
template, _ = sjson.SetRaw(template, "choices.0.message.images.-1", imagePayload)
|
||||
}
|
||||
|
||||
+6
-185
@@ -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
|
||||
}
|
||||
+293
-22
@@ -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.
|
||||
@@ -90,7 +96,7 @@ type modelStats struct {
|
||||
type RequestDetail struct {
|
||||
Timestamp time.Time `json:"timestamp"`
|
||||
Source string `json:"source"`
|
||||
AuthIndex uint64 `json:"auth_index"`
|
||||
AuthIndex string `json:"auth_index"`
|
||||
Tokens TokenStats `json:"tokens"`
|
||||
Failed bool `json:"failed"`
|
||||
}
|
||||
@@ -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)
|
||||
@@ -281,6 +311,118 @@ func (s *RequestStatistics) Snapshot() StatisticsSnapshot {
|
||||
return result
|
||||
}
|
||||
|
||||
type MergeResult struct {
|
||||
Added int64 `json:"added"`
|
||||
Skipped int64 `json:"skipped"`
|
||||
}
|
||||
|
||||
// MergeSnapshot merges an exported statistics snapshot into the current store.
|
||||
// Existing data is preserved and duplicate request details are skipped.
|
||||
func (s *RequestStatistics) MergeSnapshot(snapshot StatisticsSnapshot) MergeResult {
|
||||
result := MergeResult{}
|
||||
if s == nil {
|
||||
return result
|
||||
}
|
||||
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
|
||||
seen := make(map[string]struct{})
|
||||
for apiName, stats := range s.apis {
|
||||
if stats == nil {
|
||||
continue
|
||||
}
|
||||
for modelName, modelStatsValue := range stats.Models {
|
||||
if modelStatsValue == nil {
|
||||
continue
|
||||
}
|
||||
for _, detail := range modelStatsValue.Details {
|
||||
seen[dedupKey(apiName, modelName, detail)] = struct{}{}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for apiName, apiSnapshot := range snapshot.APIs {
|
||||
apiName = strings.TrimSpace(apiName)
|
||||
if apiName == "" {
|
||||
continue
|
||||
}
|
||||
stats, ok := s.apis[apiName]
|
||||
if !ok || stats == nil {
|
||||
stats = &apiStats{Models: make(map[string]*modelStats)}
|
||||
s.apis[apiName] = stats
|
||||
} else if stats.Models == nil {
|
||||
stats.Models = make(map[string]*modelStats)
|
||||
}
|
||||
for modelName, modelSnapshot := range apiSnapshot.Models {
|
||||
modelName = strings.TrimSpace(modelName)
|
||||
if modelName == "" {
|
||||
modelName = "unknown"
|
||||
}
|
||||
for _, detail := range modelSnapshot.Details {
|
||||
detail.Tokens = normaliseTokenStats(detail.Tokens)
|
||||
if detail.Timestamp.IsZero() {
|
||||
detail.Timestamp = time.Now()
|
||||
}
|
||||
key := dedupKey(apiName, modelName, detail)
|
||||
if _, exists := seen[key]; exists {
|
||||
result.Skipped++
|
||||
continue
|
||||
}
|
||||
seen[key] = struct{}{}
|
||||
s.recordImported(apiName, modelName, stats, detail)
|
||||
result.Added++
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
func (s *RequestStatistics) recordImported(apiName, modelName string, stats *apiStats, detail RequestDetail) {
|
||||
totalTokens := detail.Tokens.TotalTokens
|
||||
if totalTokens < 0 {
|
||||
totalTokens = 0
|
||||
}
|
||||
|
||||
s.totalRequests++
|
||||
if detail.Failed {
|
||||
s.failureCount++
|
||||
} else {
|
||||
s.successCount++
|
||||
}
|
||||
s.totalTokens += totalTokens
|
||||
|
||||
s.updateAPIStats(stats, modelName, detail)
|
||||
|
||||
dayKey := detail.Timestamp.Format("2006-01-02")
|
||||
hourKey := detail.Timestamp.Hour()
|
||||
|
||||
s.requestsByDay[dayKey]++
|
||||
s.requestsByHour[hourKey]++
|
||||
s.tokensByDay[dayKey] += totalTokens
|
||||
s.tokensByHour[hourKey] += totalTokens
|
||||
}
|
||||
|
||||
func dedupKey(apiName, modelName string, detail RequestDetail) string {
|
||||
timestamp := detail.Timestamp.UTC().Format(time.RFC3339Nano)
|
||||
tokens := normaliseTokenStats(detail.Tokens)
|
||||
return fmt.Sprintf(
|
||||
"%s|%s|%s|%s|%s|%t|%d|%d|%d|%d|%d",
|
||||
apiName,
|
||||
modelName,
|
||||
timestamp,
|
||||
detail.Source,
|
||||
detail.AuthIndex,
|
||||
detail.Failed,
|
||||
tokens.InputTokens,
|
||||
tokens.OutputTokens,
|
||||
tokens.ReasoningTokens,
|
||||
tokens.CachedTokens,
|
||||
tokens.TotalTokens,
|
||||
)
|
||||
}
|
||||
|
||||
func resolveAPIIdentifier(ctx context.Context, record coreusage.Record) string {
|
||||
if ctx != nil {
|
||||
if ginCtx, ok := ctx.Value("gin").(*gin.Context); ok && ginCtx != nil {
|
||||
@@ -340,6 +482,16 @@ func normaliseDetail(detail coreusage.Detail) TokenStats {
|
||||
return tokens
|
||||
}
|
||||
|
||||
func normaliseTokenStats(tokens TokenStats) TokenStats {
|
||||
if tokens.TotalTokens == 0 {
|
||||
tokens.TotalTokens = tokens.InputTokens + tokens.OutputTokens + tokens.ReasoningTokens
|
||||
}
|
||||
if tokens.TotalTokens == 0 {
|
||||
tokens.TotalTokens = tokens.InputTokens + tokens.OutputTokens + tokens.ReasoningTokens + tokens.CachedTokens
|
||||
}
|
||||
return tokens
|
||||
}
|
||||
|
||||
func formatHour(hour int) string {
|
||||
if hour < 0 {
|
||||
hour = 0
|
||||
@@ -347,3 +499,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()
|
||||
|
||||
@@ -56,6 +56,21 @@ func ComputeClaudeModelsHash(models []config.ClaudeModel) string {
|
||||
return hashJoined(keys)
|
||||
}
|
||||
|
||||
// ComputeCodexModelsHash returns a stable hash for Codex model aliases.
|
||||
func ComputeCodexModelsHash(models []config.CodexModel) string {
|
||||
keys := normalizeModelPairs(func(out func(key string)) {
|
||||
for _, model := range models {
|
||||
name := strings.TrimSpace(model.Name)
|
||||
alias := strings.TrimSpace(model.Alias)
|
||||
if name == "" && alias == "" {
|
||||
continue
|
||||
}
|
||||
out(strings.ToLower(name) + "|" + strings.ToLower(alias))
|
||||
}
|
||||
})
|
||||
return hashJoined(keys)
|
||||
}
|
||||
|
||||
// ComputeExcludedModelsHash returns a normalized hash for excluded model lists.
|
||||
func ComputeExcludedModelsHash(excluded []string) string {
|
||||
if len(excluded) == 0 {
|
||||
|
||||
@@ -81,6 +81,15 @@ func TestComputeClaudeModelsHash_Empty(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestComputeCodexModelsHash_Empty(t *testing.T) {
|
||||
if got := ComputeCodexModelsHash(nil); got != "" {
|
||||
t.Fatalf("expected empty hash for nil models, got %q", got)
|
||||
}
|
||||
if got := ComputeCodexModelsHash([]config.CodexModel{}); got != "" {
|
||||
t.Fatalf("expected empty hash for empty slice, got %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestComputeClaudeModelsHash_IgnoresBlankAndDedup(t *testing.T) {
|
||||
a := []config.ClaudeModel{
|
||||
{Name: "m1", Alias: "a1"},
|
||||
@@ -95,6 +104,20 @@ func TestComputeClaudeModelsHash_IgnoresBlankAndDedup(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestComputeCodexModelsHash_IgnoresBlankAndDedup(t *testing.T) {
|
||||
a := []config.CodexModel{
|
||||
{Name: "m1", Alias: "a1"},
|
||||
{Name: " "},
|
||||
{Name: "M1", Alias: "A1"},
|
||||
}
|
||||
b := []config.CodexModel{
|
||||
{Name: "m1", Alias: "a1"},
|
||||
}
|
||||
if h1, h2 := ComputeCodexModelsHash(a), ComputeCodexModelsHash(b); h1 == "" || h1 != h2 {
|
||||
t.Fatalf("expected same hash ignoring blanks/dupes, got %q / %q", h1, h2)
|
||||
}
|
||||
}
|
||||
|
||||
func TestComputeExcludedModelsHash_Normalizes(t *testing.T) {
|
||||
hash1 := ComputeExcludedModelsHash([]string{" A ", "b", "a"})
|
||||
hash2 := ComputeExcludedModelsHash([]string{"a", " b", "A"})
|
||||
@@ -157,3 +180,15 @@ func TestComputeClaudeModelsHash_Deterministic(t *testing.T) {
|
||||
t.Fatalf("expected different hash when models change, got %s", h3)
|
||||
}
|
||||
}
|
||||
|
||||
func TestComputeCodexModelsHash_Deterministic(t *testing.T) {
|
||||
models := []config.CodexModel{{Name: "a", Alias: "A"}, {Name: "b"}}
|
||||
h1 := ComputeCodexModelsHash(models)
|
||||
h2 := ComputeCodexModelsHash(models)
|
||||
if h1 == "" || h1 != h2 {
|
||||
t.Fatalf("expected deterministic hash, got %s / %s", h1, h2)
|
||||
}
|
||||
if h3 := ComputeCodexModelsHash([]config.CodexModel{{Name: "a"}}); h3 == h1 {
|
||||
t.Fatalf("expected different hash when models change, got %s", h3)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -151,6 +151,9 @@ func (s *ConfigSynthesizer) synthesizeCodexKeys(ctx *SynthesisContext) []*coreau
|
||||
if ck.BaseURL != "" {
|
||||
attrs["base_url"] = ck.BaseURL
|
||||
}
|
||||
if hash := diff.ComputeCodexModelsHash(ck.Models); hash != "" {
|
||||
attrs["models_hash"] = hash
|
||||
}
|
||||
addConfigHeadersToAttrs(ck.Headers, attrs)
|
||||
proxyURL := strings.TrimSpace(ck.ProxyURL)
|
||||
a := &coreauth.Auth{
|
||||
|
||||
@@ -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."
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user