mirror of
https://github.com/router-for-me/CLIProxyAPI.git
synced 2026-02-18 20:30:51 +08:00
feat(runtime): add model alias support and enhance payload rule matching
- Introduced `payloadModelAliases` and `payloadModelCandidates` functions to support model aliases for improved flexibility. - Updated rule matching logic to handle multiple model candidates. - Refactored variable naming in executor to improve code clarity and consistency.
This commit is contained in:
@@ -517,8 +517,8 @@ func (e *AntigravityExecutor) convertStreamToNonStream(stream []byte) []byte {
|
|||||||
}
|
}
|
||||||
if usageResult := responseNode.Get("usageMetadata"); usageResult.Exists() {
|
if usageResult := responseNode.Get("usageMetadata"); usageResult.Exists() {
|
||||||
usageRaw = usageResult.Raw
|
usageRaw = usageResult.Raw
|
||||||
} else if usageResult := root.Get("usageMetadata"); usageResult.Exists() {
|
} else if usageMetadataResult := root.Get("usageMetadata"); usageMetadataResult.Exists() {
|
||||||
usageRaw = usageResult.Raw
|
usageRaw = usageMetadataResult.Raw
|
||||||
}
|
}
|
||||||
|
|
||||||
if partsResult := responseNode.Get("candidates.0.content.parts"); partsResult.IsArray() {
|
if partsResult := responseNode.Get("candidates.0.content.parts"); partsResult.IsArray() {
|
||||||
@@ -642,7 +642,6 @@ func (e *AntigravityExecutor) ExecuteStream(ctx context.Context, auth *cliproxya
|
|||||||
err = errReq
|
err = errReq
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
httpResp, errDo := httpClient.Do(httpReq)
|
httpResp, errDo := httpClient.Do(httpReq)
|
||||||
if errDo != nil {
|
if errDo != nil {
|
||||||
recordAPIResponseError(ctx, e.cfg, errDo)
|
recordAPIResponseError(ctx, e.cfg, errDo)
|
||||||
@@ -1004,10 +1003,10 @@ func FetchAntigravityModels(ctx context.Context, auth *cliproxyauth.Auth, cfg *c
|
|||||||
case "chat_20706", "chat_23310", "gemini-2.5-flash-thinking", "gemini-3-pro-low", "gemini-2.5-pro":
|
case "chat_20706", "chat_23310", "gemini-2.5-flash-thinking", "gemini-3-pro-low", "gemini-2.5-pro":
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
cfg := modelConfig[modelID]
|
modelCfg := modelConfig[modelID]
|
||||||
modelName := modelID
|
modelName := modelID
|
||||||
if cfg != nil && cfg.Name != "" {
|
if modelCfg != nil && modelCfg.Name != "" {
|
||||||
modelName = cfg.Name
|
modelName = modelCfg.Name
|
||||||
}
|
}
|
||||||
modelInfo := ®istry.ModelInfo{
|
modelInfo := ®istry.ModelInfo{
|
||||||
ID: modelID,
|
ID: modelID,
|
||||||
@@ -1021,12 +1020,12 @@ func FetchAntigravityModels(ctx context.Context, auth *cliproxyauth.Auth, cfg *c
|
|||||||
Type: antigravityAuthType,
|
Type: antigravityAuthType,
|
||||||
}
|
}
|
||||||
// Look up Thinking support from static config using upstream model name.
|
// Look up Thinking support from static config using upstream model name.
|
||||||
if cfg != nil {
|
if modelCfg != nil {
|
||||||
if cfg.Thinking != nil {
|
if modelCfg.Thinking != nil {
|
||||||
modelInfo.Thinking = cfg.Thinking
|
modelInfo.Thinking = modelCfg.Thinking
|
||||||
}
|
}
|
||||||
if cfg.MaxCompletionTokens > 0 {
|
if modelCfg.MaxCompletionTokens > 0 {
|
||||||
modelInfo.MaxCompletionTokens = cfg.MaxCompletionTokens
|
modelInfo.MaxCompletionTokens = modelCfg.MaxCompletionTokens
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
models = append(models, modelInfo)
|
models = append(models, modelInfo)
|
||||||
|
|||||||
@@ -25,6 +25,7 @@ func applyPayloadConfigWithRoot(cfg *config.Config, model, protocol, root string
|
|||||||
if model == "" {
|
if model == "" {
|
||||||
return payload
|
return payload
|
||||||
}
|
}
|
||||||
|
candidates := payloadModelCandidates(cfg, model, protocol)
|
||||||
out := payload
|
out := payload
|
||||||
source := original
|
source := original
|
||||||
if len(source) == 0 {
|
if len(source) == 0 {
|
||||||
@@ -34,7 +35,7 @@ func applyPayloadConfigWithRoot(cfg *config.Config, model, protocol, root string
|
|||||||
// Apply default rules: first write wins per field across all matching rules.
|
// Apply default rules: first write wins per field across all matching rules.
|
||||||
for i := range rules.Default {
|
for i := range rules.Default {
|
||||||
rule := &rules.Default[i]
|
rule := &rules.Default[i]
|
||||||
if !payloadRuleMatchesModel(rule, model, protocol) {
|
if !payloadRuleMatchesModels(rule, protocol, candidates) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
for path, value := range rule.Params {
|
for path, value := range rule.Params {
|
||||||
@@ -59,7 +60,7 @@ func applyPayloadConfigWithRoot(cfg *config.Config, model, protocol, root string
|
|||||||
// Apply default raw rules: first write wins per field across all matching rules.
|
// Apply default raw rules: first write wins per field across all matching rules.
|
||||||
for i := range rules.DefaultRaw {
|
for i := range rules.DefaultRaw {
|
||||||
rule := &rules.DefaultRaw[i]
|
rule := &rules.DefaultRaw[i]
|
||||||
if !payloadRuleMatchesModel(rule, model, protocol) {
|
if !payloadRuleMatchesModels(rule, protocol, candidates) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
for path, value := range rule.Params {
|
for path, value := range rule.Params {
|
||||||
@@ -88,7 +89,7 @@ func applyPayloadConfigWithRoot(cfg *config.Config, model, protocol, root string
|
|||||||
// Apply override rules: last write wins per field across all matching rules.
|
// Apply override rules: last write wins per field across all matching rules.
|
||||||
for i := range rules.Override {
|
for i := range rules.Override {
|
||||||
rule := &rules.Override[i]
|
rule := &rules.Override[i]
|
||||||
if !payloadRuleMatchesModel(rule, model, protocol) {
|
if !payloadRuleMatchesModels(rule, protocol, candidates) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
for path, value := range rule.Params {
|
for path, value := range rule.Params {
|
||||||
@@ -106,7 +107,7 @@ func applyPayloadConfigWithRoot(cfg *config.Config, model, protocol, root string
|
|||||||
// Apply override raw rules: last write wins per field across all matching rules.
|
// Apply override raw rules: last write wins per field across all matching rules.
|
||||||
for i := range rules.OverrideRaw {
|
for i := range rules.OverrideRaw {
|
||||||
rule := &rules.OverrideRaw[i]
|
rule := &rules.OverrideRaw[i]
|
||||||
if !payloadRuleMatchesModel(rule, model, protocol) {
|
if !payloadRuleMatchesModels(rule, protocol, candidates) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
for path, value := range rule.Params {
|
for path, value := range rule.Params {
|
||||||
@@ -128,6 +129,18 @@ func applyPayloadConfigWithRoot(cfg *config.Config, model, protocol, root string
|
|||||||
return out
|
return out
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func payloadRuleMatchesModels(rule *config.PayloadRule, protocol string, models []string) bool {
|
||||||
|
if rule == nil || len(models) == 0 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
for _, model := range models {
|
||||||
|
if payloadRuleMatchesModel(rule, model, protocol) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
func payloadRuleMatchesModel(rule *config.PayloadRule, model, protocol string) bool {
|
func payloadRuleMatchesModel(rule *config.PayloadRule, model, protocol string) bool {
|
||||||
if rule == nil {
|
if rule == nil {
|
||||||
return false
|
return false
|
||||||
@@ -150,6 +163,65 @@ func payloadRuleMatchesModel(rule *config.PayloadRule, model, protocol string) b
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func payloadModelCandidates(cfg *config.Config, model, protocol string) []string {
|
||||||
|
model = strings.TrimSpace(model)
|
||||||
|
if model == "" {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
candidates := []string{model}
|
||||||
|
if cfg == nil {
|
||||||
|
return candidates
|
||||||
|
}
|
||||||
|
aliases := payloadModelAliases(cfg, model, protocol)
|
||||||
|
if len(aliases) == 0 {
|
||||||
|
return candidates
|
||||||
|
}
|
||||||
|
seen := map[string]struct{}{strings.ToLower(model): struct{}{}}
|
||||||
|
for _, alias := range aliases {
|
||||||
|
alias = strings.TrimSpace(alias)
|
||||||
|
if alias == "" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
key := strings.ToLower(alias)
|
||||||
|
if _, ok := seen[key]; ok {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
seen[key] = struct{}{}
|
||||||
|
candidates = append(candidates, alias)
|
||||||
|
}
|
||||||
|
return candidates
|
||||||
|
}
|
||||||
|
|
||||||
|
func payloadModelAliases(cfg *config.Config, model, protocol string) []string {
|
||||||
|
if cfg == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
model = strings.TrimSpace(model)
|
||||||
|
if model == "" {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
channel := strings.ToLower(strings.TrimSpace(protocol))
|
||||||
|
if channel == "" {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
entries := cfg.OAuthModelAlias[channel]
|
||||||
|
if len(entries) == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
aliases := make([]string, 0, 2)
|
||||||
|
for _, entry := range entries {
|
||||||
|
if !strings.EqualFold(strings.TrimSpace(entry.Name), model) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
alias := strings.TrimSpace(entry.Alias)
|
||||||
|
if alias == "" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
aliases = append(aliases, alias)
|
||||||
|
}
|
||||||
|
return aliases
|
||||||
|
}
|
||||||
|
|
||||||
// buildPayloadPath combines an optional root path with a relative parameter path.
|
// buildPayloadPath combines an optional root path with a relative parameter path.
|
||||||
// When root is empty, the parameter path is used as-is. When root is non-empty,
|
// When root is empty, the parameter path is used as-is. When root is non-empty,
|
||||||
// the parameter path is treated as relative to root.
|
// the parameter path is treated as relative to root.
|
||||||
|
|||||||
Reference in New Issue
Block a user