feat(registry): refresh model catalog from network
This commit is contained in:
@@ -24,6 +24,7 @@ import (
|
|||||||
"github.com/router-for-me/CLIProxyAPI/v6/internal/logging"
|
"github.com/router-for-me/CLIProxyAPI/v6/internal/logging"
|
||||||
"github.com/router-for-me/CLIProxyAPI/v6/internal/managementasset"
|
"github.com/router-for-me/CLIProxyAPI/v6/internal/managementasset"
|
||||||
"github.com/router-for-me/CLIProxyAPI/v6/internal/misc"
|
"github.com/router-for-me/CLIProxyAPI/v6/internal/misc"
|
||||||
|
"github.com/router-for-me/CLIProxyAPI/v6/internal/registry"
|
||||||
"github.com/router-for-me/CLIProxyAPI/v6/internal/store"
|
"github.com/router-for-me/CLIProxyAPI/v6/internal/store"
|
||||||
_ "github.com/router-for-me/CLIProxyAPI/v6/internal/translator"
|
_ "github.com/router-for-me/CLIProxyAPI/v6/internal/translator"
|
||||||
"github.com/router-for-me/CLIProxyAPI/v6/internal/tui"
|
"github.com/router-for-me/CLIProxyAPI/v6/internal/tui"
|
||||||
@@ -494,6 +495,7 @@ func main() {
|
|||||||
if standalone {
|
if standalone {
|
||||||
// Standalone mode: start an embedded local server and connect TUI client to it.
|
// Standalone mode: start an embedded local server and connect TUI client to it.
|
||||||
managementasset.StartAutoUpdater(context.Background(), configFilePath)
|
managementasset.StartAutoUpdater(context.Background(), configFilePath)
|
||||||
|
registry.StartModelsUpdater(context.Background())
|
||||||
hook := tui.NewLogHook(2000)
|
hook := tui.NewLogHook(2000)
|
||||||
hook.SetFormatter(&logging.LogFormatter{})
|
hook.SetFormatter(&logging.LogFormatter{})
|
||||||
log.AddHook(hook)
|
log.AddHook(hook)
|
||||||
@@ -566,6 +568,7 @@ func main() {
|
|||||||
} else {
|
} else {
|
||||||
// Start the main proxy service
|
// Start the main proxy service
|
||||||
managementasset.StartAutoUpdater(context.Background(), configFilePath)
|
managementasset.StartAutoUpdater(context.Background(), configFilePath)
|
||||||
|
registry.StartModelsUpdater(context.Background())
|
||||||
cmd.StartService(cfg, configFilePath, password)
|
cmd.StartService(cfg, configFilePath, password)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
// Package registry provides model definitions and lookup helpers for various AI providers.
|
// Package registry provides model definitions and lookup helpers for various AI providers.
|
||||||
// Static model metadata is stored in model_definitions_static_data.go.
|
// Static model metadata is loaded from the embedded models.json file and can be refreshed from network.
|
||||||
package registry
|
package registry
|
||||||
|
|
||||||
import (
|
import (
|
||||||
@@ -7,6 +7,131 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// AntigravityModelConfig captures static antigravity model overrides, including
|
||||||
|
// Thinking budget limits and provider max completion tokens.
|
||||||
|
type AntigravityModelConfig struct {
|
||||||
|
Thinking *ThinkingSupport `json:"thinking,omitempty"`
|
||||||
|
MaxCompletionTokens int `json:"max_completion_tokens,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// staticModelsJSON mirrors the top-level structure of models.json.
|
||||||
|
type staticModelsJSON struct {
|
||||||
|
Claude []*ModelInfo `json:"claude"`
|
||||||
|
Gemini []*ModelInfo `json:"gemini"`
|
||||||
|
Vertex []*ModelInfo `json:"vertex"`
|
||||||
|
GeminiCLI []*ModelInfo `json:"gemini-cli"`
|
||||||
|
AIStudio []*ModelInfo `json:"aistudio"`
|
||||||
|
CodexFree []*ModelInfo `json:"codex-free"`
|
||||||
|
CodexTeam []*ModelInfo `json:"codex-team"`
|
||||||
|
CodexPlus []*ModelInfo `json:"codex-plus"`
|
||||||
|
CodexPro []*ModelInfo `json:"codex-pro"`
|
||||||
|
Qwen []*ModelInfo `json:"qwen"`
|
||||||
|
IFlow []*ModelInfo `json:"iflow"`
|
||||||
|
Kimi []*ModelInfo `json:"kimi"`
|
||||||
|
Antigravity map[string]*AntigravityModelConfig `json:"antigravity"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetClaudeModels returns the standard Claude model definitions.
|
||||||
|
func GetClaudeModels() []*ModelInfo {
|
||||||
|
return cloneModelInfos(getModels().Claude)
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetGeminiModels returns the standard Gemini model definitions.
|
||||||
|
func GetGeminiModels() []*ModelInfo {
|
||||||
|
return cloneModelInfos(getModels().Gemini)
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetGeminiVertexModels returns Gemini model definitions for Vertex AI.
|
||||||
|
func GetGeminiVertexModels() []*ModelInfo {
|
||||||
|
return cloneModelInfos(getModels().Vertex)
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetGeminiCLIModels returns Gemini model definitions for the Gemini CLI.
|
||||||
|
func GetGeminiCLIModels() []*ModelInfo {
|
||||||
|
return cloneModelInfos(getModels().GeminiCLI)
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetAIStudioModels returns model definitions for AI Studio.
|
||||||
|
func GetAIStudioModels() []*ModelInfo {
|
||||||
|
return cloneModelInfos(getModels().AIStudio)
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetCodexFreeModels returns model definitions for the Codex free plan tier.
|
||||||
|
func GetCodexFreeModels() []*ModelInfo {
|
||||||
|
return cloneModelInfos(getModels().CodexFree)
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetCodexTeamModels returns model definitions for the Codex team plan tier.
|
||||||
|
func GetCodexTeamModels() []*ModelInfo {
|
||||||
|
return cloneModelInfos(getModels().CodexTeam)
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetCodexPlusModels returns model definitions for the Codex plus plan tier.
|
||||||
|
func GetCodexPlusModels() []*ModelInfo {
|
||||||
|
return cloneModelInfos(getModels().CodexPlus)
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetCodexProModels returns model definitions for the Codex pro plan tier.
|
||||||
|
func GetCodexProModels() []*ModelInfo {
|
||||||
|
return cloneModelInfos(getModels().CodexPro)
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetQwenModels returns the standard Qwen model definitions.
|
||||||
|
func GetQwenModels() []*ModelInfo {
|
||||||
|
return cloneModelInfos(getModels().Qwen)
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetIFlowModels returns the standard iFlow model definitions.
|
||||||
|
func GetIFlowModels() []*ModelInfo {
|
||||||
|
return cloneModelInfos(getModels().IFlow)
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetKimiModels returns the standard Kimi (Moonshot AI) model definitions.
|
||||||
|
func GetKimiModels() []*ModelInfo {
|
||||||
|
return cloneModelInfos(getModels().Kimi)
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetAntigravityModelConfig returns static configuration for antigravity models.
|
||||||
|
// Keys use upstream model names returned by the Antigravity models endpoint.
|
||||||
|
func GetAntigravityModelConfig() map[string]*AntigravityModelConfig {
|
||||||
|
data := getModels()
|
||||||
|
if len(data.Antigravity) == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
out := make(map[string]*AntigravityModelConfig, len(data.Antigravity))
|
||||||
|
for k, v := range data.Antigravity {
|
||||||
|
out[k] = cloneAntigravityModelConfig(v)
|
||||||
|
}
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|
||||||
|
func cloneAntigravityModelConfig(cfg *AntigravityModelConfig) *AntigravityModelConfig {
|
||||||
|
if cfg == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
copyConfig := *cfg
|
||||||
|
if cfg.Thinking != nil {
|
||||||
|
copyThinking := *cfg.Thinking
|
||||||
|
if len(cfg.Thinking.Levels) > 0 {
|
||||||
|
copyThinking.Levels = append([]string(nil), cfg.Thinking.Levels...)
|
||||||
|
}
|
||||||
|
copyConfig.Thinking = ©Thinking
|
||||||
|
}
|
||||||
|
return ©Config
|
||||||
|
}
|
||||||
|
|
||||||
|
// cloneModelInfos returns a shallow copy of the slice with each element deep-cloned.
|
||||||
|
func cloneModelInfos(models []*ModelInfo) []*ModelInfo {
|
||||||
|
if len(models) == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
out := make([]*ModelInfo, len(models))
|
||||||
|
for i, m := range models {
|
||||||
|
out[i] = cloneModelInfo(m)
|
||||||
|
}
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|
||||||
// GetStaticModelDefinitionsByChannel returns static model definitions for a given channel/provider.
|
// GetStaticModelDefinitionsByChannel returns static model definitions for a given channel/provider.
|
||||||
// It returns nil when the channel is unknown.
|
// It returns nil when the channel is unknown.
|
||||||
//
|
//
|
||||||
@@ -77,27 +202,28 @@ func LookupStaticModelInfo(modelID string) *ModelInfo {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
data := getModels()
|
||||||
allModels := [][]*ModelInfo{
|
allModels := [][]*ModelInfo{
|
||||||
GetClaudeModels(),
|
data.Claude,
|
||||||
GetGeminiModels(),
|
data.Gemini,
|
||||||
GetGeminiVertexModels(),
|
data.Vertex,
|
||||||
GetGeminiCLIModels(),
|
data.GeminiCLI,
|
||||||
GetAIStudioModels(),
|
data.AIStudio,
|
||||||
GetCodexProModels(),
|
data.CodexPro,
|
||||||
GetQwenModels(),
|
data.Qwen,
|
||||||
GetIFlowModels(),
|
data.IFlow,
|
||||||
GetKimiModels(),
|
data.Kimi,
|
||||||
}
|
}
|
||||||
for _, models := range allModels {
|
for _, models := range allModels {
|
||||||
for _, m := range models {
|
for _, m := range models {
|
||||||
if m != nil && m.ID == modelID {
|
if m != nil && m.ID == modelID {
|
||||||
return m
|
return cloneModelInfo(m)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check Antigravity static config
|
// Check Antigravity static config
|
||||||
if cfg := GetAntigravityModelConfig()[modelID]; cfg != nil {
|
if cfg := cloneAntigravityModelConfig(data.Antigravity[modelID]); cfg != nil {
|
||||||
return &ModelInfo{
|
return &ModelInfo{
|
||||||
ID: modelID,
|
ID: modelID,
|
||||||
Thinking: cfg.Thinking,
|
Thinking: cfg.Thinking,
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,209 @@
|
|||||||
|
package registry
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
_ "embed"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
modelsFetchTimeout = 30 * time.Second
|
||||||
|
modelsRefreshInterval = 3 * time.Hour
|
||||||
|
)
|
||||||
|
|
||||||
|
var modelsURLs = []string{
|
||||||
|
"https://raw.githubusercontent.com/router-for-me/models/refs/heads/main/models.json",
|
||||||
|
"https://models.router-for.me/models.json",
|
||||||
|
}
|
||||||
|
|
||||||
|
//go:embed models/models.json
|
||||||
|
var embeddedModelsJSON []byte
|
||||||
|
|
||||||
|
type modelStore struct {
|
||||||
|
mu sync.RWMutex
|
||||||
|
data *staticModelsJSON
|
||||||
|
}
|
||||||
|
|
||||||
|
var modelsCatalogStore = &modelStore{}
|
||||||
|
|
||||||
|
var updaterOnce sync.Once
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
// Load embedded data as fallback on startup.
|
||||||
|
if err := loadModelsFromBytes(embeddedModelsJSON, "embed"); err != nil {
|
||||||
|
panic(fmt.Sprintf("registry: failed to parse embedded models.json: %v", err))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// StartModelsUpdater starts the background models refresh goroutine.
|
||||||
|
// It immediately attempts to fetch models from network, then refreshes every 3 hours.
|
||||||
|
// Safe to call multiple times; only one updater will be started.
|
||||||
|
func StartModelsUpdater(ctx context.Context) {
|
||||||
|
updaterOnce.Do(func() {
|
||||||
|
go runModelsUpdater(ctx)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func runModelsUpdater(ctx context.Context) {
|
||||||
|
// Immediately try network fetch once
|
||||||
|
tryRefreshModels(ctx)
|
||||||
|
|
||||||
|
ticker := time.NewTicker(modelsRefreshInterval)
|
||||||
|
defer ticker.Stop()
|
||||||
|
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-ctx.Done():
|
||||||
|
return
|
||||||
|
case <-ticker.C:
|
||||||
|
tryRefreshModels(ctx)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func tryRefreshModels(ctx context.Context) {
|
||||||
|
client := &http.Client{Timeout: modelsFetchTimeout}
|
||||||
|
for _, url := range modelsURLs {
|
||||||
|
reqCtx, cancel := context.WithTimeout(ctx, modelsFetchTimeout)
|
||||||
|
req, err := http.NewRequestWithContext(reqCtx, "GET", url, nil)
|
||||||
|
if err != nil {
|
||||||
|
cancel()
|
||||||
|
log.Debugf("models fetch request creation failed for %s: %v", url, err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
resp, err := client.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
cancel()
|
||||||
|
log.Debugf("models fetch failed from %s: %v", url, err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if resp.StatusCode != 200 {
|
||||||
|
resp.Body.Close()
|
||||||
|
cancel()
|
||||||
|
log.Debugf("models fetch returned %d from %s", resp.StatusCode, url)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
data, err := io.ReadAll(resp.Body)
|
||||||
|
resp.Body.Close()
|
||||||
|
cancel()
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
log.Debugf("models fetch read error from %s: %v", url, err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := loadModelsFromBytes(data, url); err != nil {
|
||||||
|
log.Warnf("models parse failed from %s: %v", url, err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Infof("models updated from %s", url)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
log.Warn("models refresh failed from all URLs, using current data")
|
||||||
|
}
|
||||||
|
|
||||||
|
func loadModelsFromBytes(data []byte, source string) error {
|
||||||
|
var parsed staticModelsJSON
|
||||||
|
if err := json.Unmarshal(data, &parsed); err != nil {
|
||||||
|
return fmt.Errorf("%s: decode models catalog: %w", source, err)
|
||||||
|
}
|
||||||
|
if err := validateModelsCatalog(&parsed); err != nil {
|
||||||
|
return fmt.Errorf("%s: validate models catalog: %w", source, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
modelsCatalogStore.mu.Lock()
|
||||||
|
modelsCatalogStore.data = &parsed
|
||||||
|
modelsCatalogStore.mu.Unlock()
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func getModels() *staticModelsJSON {
|
||||||
|
modelsCatalogStore.mu.RLock()
|
||||||
|
defer modelsCatalogStore.mu.RUnlock()
|
||||||
|
return modelsCatalogStore.data
|
||||||
|
}
|
||||||
|
|
||||||
|
func validateModelsCatalog(data *staticModelsJSON) error {
|
||||||
|
if data == nil {
|
||||||
|
return fmt.Errorf("catalog is nil")
|
||||||
|
}
|
||||||
|
|
||||||
|
requiredSections := []struct {
|
||||||
|
name string
|
||||||
|
models []*ModelInfo
|
||||||
|
}{
|
||||||
|
{name: "claude", models: data.Claude},
|
||||||
|
{name: "gemini", models: data.Gemini},
|
||||||
|
{name: "vertex", models: data.Vertex},
|
||||||
|
{name: "gemini-cli", models: data.GeminiCLI},
|
||||||
|
{name: "aistudio", models: data.AIStudio},
|
||||||
|
{name: "codex-free", models: data.CodexFree},
|
||||||
|
{name: "codex-team", models: data.CodexTeam},
|
||||||
|
{name: "codex-plus", models: data.CodexPlus},
|
||||||
|
{name: "codex-pro", models: data.CodexPro},
|
||||||
|
{name: "qwen", models: data.Qwen},
|
||||||
|
{name: "iflow", models: data.IFlow},
|
||||||
|
{name: "kimi", models: data.Kimi},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, section := range requiredSections {
|
||||||
|
if err := validateModelSection(section.name, section.models); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if err := validateAntigravitySection(data.Antigravity); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func validateModelSection(section string, models []*ModelInfo) error {
|
||||||
|
if len(models) == 0 {
|
||||||
|
return fmt.Errorf("%s section is empty", section)
|
||||||
|
}
|
||||||
|
|
||||||
|
seen := make(map[string]struct{}, len(models))
|
||||||
|
for i, model := range models {
|
||||||
|
if model == nil {
|
||||||
|
return fmt.Errorf("%s[%d] is null", section, i)
|
||||||
|
}
|
||||||
|
modelID := strings.TrimSpace(model.ID)
|
||||||
|
if modelID == "" {
|
||||||
|
return fmt.Errorf("%s[%d] has empty id", section, i)
|
||||||
|
}
|
||||||
|
if _, exists := seen[modelID]; exists {
|
||||||
|
return fmt.Errorf("%s contains duplicate model id %q", section, modelID)
|
||||||
|
}
|
||||||
|
seen[modelID] = struct{}{}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func validateAntigravitySection(configs map[string]*AntigravityModelConfig) error {
|
||||||
|
if len(configs) == 0 {
|
||||||
|
return fmt.Errorf("antigravity section is empty")
|
||||||
|
}
|
||||||
|
|
||||||
|
for modelID, cfg := range configs {
|
||||||
|
trimmedID := strings.TrimSpace(modelID)
|
||||||
|
if trimmedID == "" {
|
||||||
|
return fmt.Errorf("antigravity contains empty model id")
|
||||||
|
}
|
||||||
|
if cfg == nil {
|
||||||
|
return fmt.Errorf("antigravity[%q] is null", trimmedID)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user