mirror of
https://github.com/router-for-me/CLIProxyAPI.git
synced 2026-02-03 21:10:51 +08:00
feat(watcher, auth): add stable hash for OpenAI compatibility models
- Introduced `computeOpenAICompatModelsHash` for generating a stable hash of compatibility models. - Enhanced `watcher` to include the hash in auth attributes, enabling dynamic updates on model list changes.
This commit is contained in:
@@ -320,6 +320,20 @@ func normalizeAuth(a *coreauth.Auth) *coreauth.Auth {
|
|||||||
return clone
|
return clone
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// computeOpenAICompatModelsHash returns a stable hash for the compatibility models so that
|
||||||
|
// changes to the model list trigger auth updates during hot reload.
|
||||||
|
func computeOpenAICompatModelsHash(models []config.OpenAICompatibilityModel) string {
|
||||||
|
if len(models) == 0 {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
data, err := json.Marshal(models)
|
||||||
|
if err != nil || len(data) == 0 {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
sum := sha256.Sum256(data)
|
||||||
|
return hex.EncodeToString(sum[:])
|
||||||
|
}
|
||||||
|
|
||||||
// SetClients sets the file-based clients.
|
// SetClients sets the file-based clients.
|
||||||
// SetClients removed
|
// SetClients removed
|
||||||
// SetAPIKeyClients removed
|
// SetAPIKeyClients removed
|
||||||
@@ -700,20 +714,24 @@ func (w *Watcher) SnapshotCoreAuths() []*coreauth.Auth {
|
|||||||
base := compat.BaseURL
|
base := compat.BaseURL
|
||||||
for j := range compat.APIKeys {
|
for j := range compat.APIKeys {
|
||||||
key := compat.APIKeys[j]
|
key := compat.APIKeys[j]
|
||||||
|
attrs := map[string]string{
|
||||||
|
"source": fmt.Sprintf("config:%s#%d", compat.Name, j),
|
||||||
|
"base_url": base,
|
||||||
|
"api_key": key,
|
||||||
|
"compat_name": compat.Name,
|
||||||
|
"provider_key": providerName,
|
||||||
|
}
|
||||||
|
if hash := computeOpenAICompatModelsHash(compat.Models); hash != "" {
|
||||||
|
attrs["models_hash"] = hash
|
||||||
|
}
|
||||||
a := &coreauth.Auth{
|
a := &coreauth.Auth{
|
||||||
ID: fmt.Sprintf("openai-compatibility:%s:%d", compat.Name, j),
|
ID: fmt.Sprintf("openai-compatibility:%s:%d", compat.Name, j),
|
||||||
Provider: providerName,
|
Provider: providerName,
|
||||||
Label: compat.Name,
|
Label: compat.Name,
|
||||||
Status: coreauth.StatusActive,
|
Status: coreauth.StatusActive,
|
||||||
Attributes: map[string]string{
|
Attributes: attrs,
|
||||||
"source": fmt.Sprintf("config:%s#%d", compat.Name, j),
|
CreatedAt: now,
|
||||||
"base_url": base,
|
UpdatedAt: now,
|
||||||
"api_key": key,
|
|
||||||
"compat_name": compat.Name,
|
|
||||||
"provider_key": providerName,
|
|
||||||
},
|
|
||||||
CreatedAt: now,
|
|
||||||
UpdatedAt: now,
|
|
||||||
}
|
}
|
||||||
out = append(out, a)
|
out = append(out, a)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user