mirror of
https://github.com/jasonxu114514/opencode2api.git
synced 2026-12-05 08:02:53 +00:00
fix: fetch models.dev metadata through configured proxies
The models.dev metadata store used its own plain http.Client, so the periodic price/deprecation refresh always bypassed the configured proxy pool and failed on networks where direct access to models.dev is unreliable or blocked. Introduce a client provider on the store that resolves candidates at refresh time: the active runtime's healthy proxy transports first, then the original direct client as fallback. The provider reads the current runtime atomically, so WebUI hot reloads automatically pick up new proxy transports without touching the long-lived store.
This commit is contained in:
1 parent
2615455b87
commit
f63d8dd0b7
2 files changed
+82
-31
No files matched your search
+67
-31
@@ -55,14 +55,15 @@ type modelMetadataCache struct {
|
||||
}
|
||||
|
||||
type modelMetadataStore struct {
|
||||
mu sync.RWMutex
|
||||
models map[string]ModelPrice
|
||||
updatedAt time.Time
|
||||
lastError string
|
||||
cachePath string
|
||||
endpoint string
|
||||
client *http.Client
|
||||
logger *slog.Logger
|
||||
mu sync.RWMutex
|
||||
models map[string]ModelPrice
|
||||
updatedAt time.Time
|
||||
lastError string
|
||||
cachePath string
|
||||
endpoint string
|
||||
client *http.Client
|
||||
clientProvider func() []*http.Client
|
||||
logger *slog.Logger
|
||||
}
|
||||
|
||||
func newModelMetadataStore(configPath string, logger *slog.Logger) *modelMetadataStore {
|
||||
@@ -80,6 +81,29 @@ func newModelMetadataStore(configPath string, logger *slog.Logger) *modelMetadat
|
||||
return store
|
||||
}
|
||||
|
||||
// SetClientProvider supplies candidate HTTP clients for refresh attempts, most
|
||||
// preferred first. It lets the periodic models.dev refresh ride the proxy
|
||||
// transports exposed by whichever gateway runtime is currently active.
|
||||
func (store *modelMetadataStore) SetClientProvider(provider func() []*http.Client) {
|
||||
store.mu.Lock()
|
||||
defer store.mu.Unlock()
|
||||
store.clientProvider = provider
|
||||
}
|
||||
|
||||
func (store *modelMetadataStore) refreshClients() []*http.Client {
|
||||
store.mu.RLock()
|
||||
provider := store.clientProvider
|
||||
store.mu.RUnlock()
|
||||
if provider == nil {
|
||||
return []*http.Client{store.client}
|
||||
}
|
||||
clients := provider()
|
||||
if len(clients) == 0 {
|
||||
return []*http.Client{store.client}
|
||||
}
|
||||
return clients
|
||||
}
|
||||
|
||||
func (store *modelMetadataStore) Start(ctx context.Context) {
|
||||
go func() {
|
||||
store.refreshAndLog(ctx)
|
||||
@@ -109,41 +133,53 @@ func (store *modelMetadataStore) refreshAndLog(ctx context.Context) {
|
||||
}
|
||||
|
||||
func (store *modelMetadataStore) Refresh(ctx context.Context) error {
|
||||
var lastErr error
|
||||
for _, client := range store.refreshClients() {
|
||||
data, err := store.fetch(ctx, client)
|
||||
if err != nil {
|
||||
lastErr = err
|
||||
continue
|
||||
}
|
||||
models, err := decodeModelsDev(data)
|
||||
if err != nil {
|
||||
return store.recordError(err)
|
||||
}
|
||||
now := time.Now().UTC()
|
||||
cache := modelMetadataCache{UpdatedAt: now, Models: models}
|
||||
if store.cachePath != "" {
|
||||
if err := saveMetadataCache(store.cachePath, cache); err != nil {
|
||||
return store.recordError(err)
|
||||
}
|
||||
}
|
||||
store.mu.Lock()
|
||||
store.models, store.updatedAt, store.lastError = models, now, ""
|
||||
store.mu.Unlock()
|
||||
return nil
|
||||
}
|
||||
if lastErr == nil {
|
||||
lastErr = errors.New("no HTTP client available for models.dev refresh")
|
||||
}
|
||||
return store.recordError(lastErr)
|
||||
}
|
||||
|
||||
func (store *modelMetadataStore) fetch(ctx context.Context, client *http.Client) ([]byte, error) {
|
||||
refreshCtx, cancel := context.WithTimeout(ctx, modelsDevTimeout)
|
||||
defer cancel()
|
||||
req, err := http.NewRequestWithContext(refreshCtx, http.MethodGet, store.endpoint, nil)
|
||||
if err != nil {
|
||||
return store.recordError(err)
|
||||
return nil, err
|
||||
}
|
||||
req.Header.Set("Accept", "application/json")
|
||||
req.Header.Set("User-Agent", opencodeUserAgent())
|
||||
resp, err := store.client.Do(req)
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return store.recordError(err)
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
if resp.StatusCode/100 != 2 {
|
||||
return store.recordError(fmt.Errorf("models.dev returned HTTP %d", resp.StatusCode))
|
||||
return nil, fmt.Errorf("models.dev returned HTTP %d", resp.StatusCode)
|
||||
}
|
||||
data, err := io.ReadAll(io.LimitReader(resp.Body, 32<<20))
|
||||
if err != nil {
|
||||
return store.recordError(err)
|
||||
}
|
||||
models, err := decodeModelsDev(data)
|
||||
if err != nil {
|
||||
return store.recordError(err)
|
||||
}
|
||||
now := time.Now().UTC()
|
||||
cache := modelMetadataCache{UpdatedAt: now, Models: models}
|
||||
if store.cachePath != "" {
|
||||
if err := saveMetadataCache(store.cachePath, cache); err != nil {
|
||||
return store.recordError(err)
|
||||
}
|
||||
}
|
||||
store.mu.Lock()
|
||||
store.models, store.updatedAt, store.lastError = models, now, ""
|
||||
store.mu.Unlock()
|
||||
return nil
|
||||
return io.ReadAll(io.LimitReader(resp.Body, 32<<20))
|
||||
}
|
||||
|
||||
func (store *modelMetadataStore) recordError(err error) error {
|
||||
|
||||
+15
@@ -52,6 +52,21 @@ func NewRuntimeManager(root context.Context, configPath string, cfg Config, logg
|
||||
effective: effectiveListeners{API: cfg.Listen, WebUI: cfg.WebUI.Listen, WebUIEnabled: cfg.WebUI.Enabled},
|
||||
}
|
||||
manager.metadata = newModelMetadataStore(configPath, logger)
|
||||
// models.dev refreshes ride the active runtime's healthy proxy transports
|
||||
// and fall back to the store's direct client when none are available.
|
||||
manager.metadata.SetClientProvider(func() []*http.Client {
|
||||
runtime := manager.current.Load()
|
||||
if runtime == nil || runtime.gateway == nil || runtime.gateway.transports == nil {
|
||||
return nil
|
||||
}
|
||||
clients := make([]*http.Client, 0, len(runtime.gateway.transports.items))
|
||||
for _, proxy := range runtime.gateway.transports.items {
|
||||
if proxy != nil && proxy.healthy.Load() {
|
||||
clients = append(clients, proxy.client)
|
||||
}
|
||||
}
|
||||
return clients
|
||||
})
|
||||
if cfg.WebUI.Password != "" {
|
||||
hash, err := hashPassword(cfg.WebUI.Password)
|
||||
if err != nil {
|
||||
|
||||
Reference in new issue
Block a user