120 lines
3.5 KiB
Go
120 lines
3.5 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"flag"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"github.com/router-for-me/CLIProxyAPI/v7/internal/buildinfo"
|
|
"github.com/router-for-me/CLIProxyAPI/v7/internal/cmd"
|
|
"github.com/router-for-me/CLIProxyAPI/v7/internal/config"
|
|
"github.com/router-for-me/CLIProxyAPI/v7/internal/logging"
|
|
_ "github.com/router-for-me/CLIProxyAPI/v7/internal/translator"
|
|
"github.com/router-for-me/CLIProxyAPI/v7/internal/util"
|
|
sdkAuth "github.com/router-for-me/CLIProxyAPI/v7/sdk/auth"
|
|
coreauth "github.com/router-for-me/CLIProxyAPI/v7/sdk/cliproxy/auth"
|
|
log "github.com/sirupsen/logrus"
|
|
)
|
|
|
|
type codexStore struct {
|
|
fileStore *sdkAuth.FileTokenStore
|
|
}
|
|
|
|
func (s *codexStore) SetBaseDir(dir string) {
|
|
s.fileStore.SetBaseDir(dir)
|
|
}
|
|
|
|
func (s *codexStore) List(ctx context.Context) ([]*coreauth.Auth, error) {
|
|
auths, errList := s.fileStore.List(ctx)
|
|
if errList != nil {
|
|
return nil, errList
|
|
}
|
|
codexAuths := make([]*coreauth.Auth, 0, len(auths))
|
|
for _, auth := range auths {
|
|
if auth != nil && strings.EqualFold(strings.TrimSpace(auth.Provider), "codex") && auth.AuthKind() == "oauth" {
|
|
codexAuths = append(codexAuths, auth)
|
|
}
|
|
}
|
|
return codexAuths, nil
|
|
}
|
|
|
|
func (s *codexStore) Save(ctx context.Context, auth *coreauth.Auth) (string, error) {
|
|
if auth == nil || !strings.EqualFold(strings.TrimSpace(auth.Provider), "codex") || auth.AuthKind() != "oauth" {
|
|
return "", errors.New("only Codex OAuth credentials are supported")
|
|
}
|
|
return s.fileStore.Save(ctx, auth)
|
|
}
|
|
|
|
func (s *codexStore) Delete(ctx context.Context, id string) error {
|
|
return s.fileStore.Delete(ctx, id)
|
|
}
|
|
|
|
var (
|
|
Version = "dev"
|
|
Commit = "none"
|
|
BuildDate = "unknown"
|
|
DefaultConfigPath = ""
|
|
)
|
|
|
|
func init() {
|
|
logging.SetupBaseLogger()
|
|
buildinfo.Version = Version
|
|
buildinfo.Commit = Commit
|
|
buildinfo.BuildDate = BuildDate
|
|
}
|
|
|
|
func main() {
|
|
var configPath string
|
|
flag.StringVar(&configPath, "config", DefaultConfigPath, "configuration file path")
|
|
flag.Parse()
|
|
|
|
if strings.TrimSpace(configPath) == "" {
|
|
workingDirectory, errWorkingDirectory := os.Getwd()
|
|
if errWorkingDirectory != nil {
|
|
log.WithError(errWorkingDirectory).Error("failed to get working directory")
|
|
return
|
|
}
|
|
configPath = filepath.Join(workingDirectory, "config.yaml")
|
|
}
|
|
|
|
cfg, errLoadConfig := config.LoadConfig(configPath)
|
|
if errLoadConfig != nil {
|
|
log.WithError(errLoadConfig).Error("failed to load configuration")
|
|
return
|
|
}
|
|
resolvedAuthDir, errResolveAuthDir := util.ResolveAuthDir(cfg.AuthDir)
|
|
if errResolveAuthDir != nil {
|
|
log.WithError(errResolveAuthDir).Error("failed to resolve auth directory")
|
|
return
|
|
}
|
|
cfg.AuthDir = resolvedAuthDir
|
|
cfg.GeminiKey = nil
|
|
cfg.InteractionsKey = nil
|
|
cfg.CodexKey = nil
|
|
cfg.XAIKey = nil
|
|
cfg.ClaudeKey = nil
|
|
cfg.OpenAICompatibility = nil
|
|
cfg.VertexCompatAPIKey = nil
|
|
cfg.OAuthExcludedModels = nil
|
|
cfg.OAuthModelAlias = nil
|
|
cfg.Plugins.Enabled = false
|
|
cfg.Home.Enabled = false
|
|
cfg.Routing.Strategy = "round-robin"
|
|
cfg.Routing.SessionAffinity = false
|
|
|
|
if errConfigureLogging := logging.ConfigureLogOutput(cfg); errConfigureLogging != nil {
|
|
log.WithError(errConfigureLogging).Error("failed to configure log output")
|
|
return
|
|
}
|
|
util.SetLogLevel(cfg)
|
|
coreauth.SetQuotaCooldownDisabled(cfg.DisableCooling)
|
|
coreauth.SetTransientErrorCooldownSeconds(cfg.TransientErrorCooldownSeconds)
|
|
sdkAuth.RegisterTokenStore(&codexStore{fileStore: sdkAuth.NewFileTokenStore()})
|
|
|
|
fmt.Printf("Vibe Proxy %s (%s), built %s\n", buildinfo.Version, buildinfo.Commit, buildinfo.BuildDate)
|
|
cmd.StartService(cfg, configPath, "")
|
|
}
|