mirror of
https://github.com/Finesssee/ProxyPilot.git
synced 2026-11-18 18:04:20 +00:00
Generate config files to point CLI agents at ProxyPilot: - Claude Code: ~/.claude/settings.json - Codex CLI: ~/.codex/config.toml - Factory Droid: ~/.factory/config.json 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
203 lines
4.9 KiB
Go
203 lines
4.9 KiB
Go
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)
|
|
}
|