111 lines
3.2 KiB
Go
111 lines
3.2 KiB
Go
package handlers
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"log"
|
|
|
|
"git.gobha.me/xcaliber/chat-switchboard/models"
|
|
"git.gobha.me/xcaliber/chat-switchboard/providers"
|
|
"git.gobha.me/xcaliber/chat-switchboard/store"
|
|
)
|
|
|
|
// syncResult holds the outcome of a model sync operation.
|
|
type syncResult struct {
|
|
Added int `json:"added"`
|
|
Updated int `json:"updated"`
|
|
Total int `json:"total"`
|
|
}
|
|
|
|
// syncProviderModels fetches models from a provider's API and syncs them into the catalog.
|
|
// New models default to 'disabled' visibility (admin must explicitly enable for global providers).
|
|
// apiKeyPlain is the decrypted API key — callers are responsible for decryption.
|
|
func syncProviderModels(ctx context.Context, stores store.Stores, cfg *models.ProviderConfig, apiKeyPlain string) (syncResult, error) {
|
|
prov, err := providers.Get(cfg.Provider)
|
|
if err != nil {
|
|
return syncResult{}, fmt.Errorf("unknown provider type: %s", cfg.Provider)
|
|
}
|
|
|
|
provCfg := providers.ProviderConfig{
|
|
Endpoint: cfg.Endpoint,
|
|
APIKey: apiKeyPlain,
|
|
}
|
|
|
|
if cfg.Headers != nil {
|
|
customHeaders := make(map[string]string)
|
|
for k, v := range cfg.Headers {
|
|
if s, ok := v.(string); ok {
|
|
customHeaders[k] = s
|
|
}
|
|
}
|
|
provCfg.CustomHeaders = customHeaders
|
|
}
|
|
|
|
// Parse config for any extra settings the provider needs
|
|
if cfg.Config != nil {
|
|
raw, _ := json.Marshal(cfg.Config)
|
|
var extra map[string]string
|
|
if json.Unmarshal(raw, &extra) == nil {
|
|
if provCfg.CustomHeaders == nil {
|
|
provCfg.CustomHeaders = make(map[string]string)
|
|
}
|
|
for k, v := range extra {
|
|
provCfg.CustomHeaders[k] = v
|
|
}
|
|
}
|
|
}
|
|
|
|
provModels, err := prov.ListModels(ctx, provCfg)
|
|
if err != nil {
|
|
return syncResult{}, err
|
|
}
|
|
|
|
syncEntries := make([]store.CatalogSyncEntry, len(provModels))
|
|
for i, m := range provModels {
|
|
syncEntries[i] = store.CatalogSyncEntry{
|
|
ModelID: m.ID,
|
|
DisplayName: m.Name,
|
|
ModelType: m.Type,
|
|
Capabilities: m.Capabilities,
|
|
Pricing: m.Pricing,
|
|
}
|
|
}
|
|
|
|
added, updated, err := stores.Catalog.UpsertFromSync(ctx, cfg.ID, syncEntries)
|
|
if err != nil {
|
|
return syncResult{}, fmt.Errorf("failed to sync: %w", err)
|
|
}
|
|
|
|
// Sync pricing from provider catalog (won't overwrite manual admin overrides)
|
|
if stores.Pricing != nil {
|
|
for _, m := range provModels {
|
|
if m.Pricing != nil {
|
|
if err := stores.Pricing.UpsertFromCatalog(ctx, cfg.ID, m.ID, m.Pricing); err != nil {
|
|
log.Printf("warn: pricing sync for %s/%s failed: %v", cfg.ID, m.ID, err)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return syncResult{Added: added, Updated: updated, Total: len(provModels)}, nil
|
|
}
|
|
|
|
// syncAndEnableProviderModels fetches models and auto-enables them all.
|
|
// Used for personal (BYOK) providers — the user explicitly added this provider to use it.
|
|
func syncAndEnableProviderModels(ctx context.Context, stores store.Stores, cfg *models.ProviderConfig, apiKeyPlain string) (syncResult, error) {
|
|
result, err := syncProviderModels(ctx, stores, cfg, apiKeyPlain)
|
|
if err != nil {
|
|
return result, err
|
|
}
|
|
|
|
// Auto-enable: user added this key to USE it, not to stare at disabled models
|
|
if result.Total > 0 {
|
|
if err := stores.Catalog.BulkSetVisibility(ctx, cfg.ID, "enabled"); err != nil {
|
|
log.Printf("warn: auto-enable models for provider %s failed: %v", cfg.ID, err)
|
|
}
|
|
}
|
|
|
|
return result, nil
|
|
}
|