CrossRelay stage 2: Codex /v1/responses relay, config.toml manager, Codex page

This commit is contained in:
kate522 committed 2026-10-18 18:35:01 +08:00
1 parent 404704171d
commit 5112f7ca06
30 files changed
+3539 -72

No files matched your search

+63
View File
@@ -88,6 +88,21 @@ func (s *Store) ActiveClaude() *Provider {
return nil
}
// ActiveCodex returns the provider currently applied to Codex.
func (s *Store) ActiveCodex() *Provider {
s.mu.Lock()
defer s.mu.Unlock()
if s.cfg.ActiveCodexID == "" {
return nil
}
for i := range s.cfg.Providers {
if s.cfg.Providers[i].ID == s.cfg.ActiveCodexID {
return s.cfg.Providers[i].clone()
}
}
return nil
}
// AddProvider validates and appends a provider.
func (s *Store) AddProvider(name, preset, baseURL, apiKey string, models Models) (*Provider, error) {
s.mu.Lock()
@@ -128,6 +143,7 @@ func (s *Store) UpdateProvider(id, name, preset, baseURL, apiKey string, models
}
updated.ID = existing.ID
updated.EnabledForClaude = existing.EnabledForClaude
updated.EnabledForCodex = existing.EnabledForCodex
updated.CreatedAt = existing.CreatedAt
s.cfg.Providers[idx] = *updated
if err := s.saveLocked(); err != nil {
@@ -154,6 +170,9 @@ func (s *Store) DeleteProvider(id string) error {
if s.cfg.ActiveClaudeID == id {
s.cfg.ActiveClaudeID = ""
}
if s.cfg.ActiveCodexID == id {
s.cfg.ActiveCodexID = ""
}
return s.saveLocked()
}
@@ -192,6 +211,50 @@ func (s *Store) ClearActiveClaude() error {
return s.saveLocked()
}
// ActivateCodex marks a provider as the one written into Codex's
// config.toml and records it as active.
func (s *Store) ActivateCodex(id string) (*Provider, error) {
s.mu.Lock()
defer s.mu.Unlock()
idx := -1
for i := range s.cfg.Providers {
if s.cfg.Providers[i].ID == id {
idx = i
break
}
}
if idx < 0 {
return nil, fmt.Errorf("provider %q not found", id)
}
s.cfg.Providers[idx].EnabledForCodex = true
s.cfg.ActiveCodexID = id
if err := s.saveLocked(); err != nil {
return nil, err
}
return s.cfg.Providers[idx].clone(), nil
}
// ClearActiveCodex forgets the active Codex provider without touching
// the provider list (used after restoring the official config).
func (s *Store) ClearActiveCodex() error {
s.mu.Lock()
defer s.mu.Unlock()
if s.cfg.ActiveCodexID == "" {
return nil
}
s.cfg.ActiveCodexID = ""
return s.saveLocked()
}
// SetCodexPriorProvider records (or clears) the model_provider value
// that config.toml carried before CrossRelay took it over.
func (s *Store) SetCodexPriorProvider(prior string) error {
s.mu.Lock()
defer s.mu.Unlock()
s.cfg.CodexPriorProvider = strings.TrimSpace(prior)
return s.saveLocked()
}
// SetGateway updates listener settings.
func (s *Store) SetGateway(g Gateway) error {
g.PortMode = strings.TrimSpace(g.PortMode)