mirror of
https://github.com/QuantumNous/new-api.git
synced 2026-03-30 02:25:00 +00:00
Merge pull request #3066 from seefs001/fix/aws-header-override
Fix/aws header override
This commit is contained in:
@@ -120,8 +120,18 @@ func ApplyParamOverride(jsonData []byte, paramOverride map[string]interface{}, c
|
||||
|
||||
// 尝试断言为操作格式
|
||||
if operations, ok := tryParseOperations(paramOverride); ok {
|
||||
legacyOverride := buildLegacyParamOverride(paramOverride)
|
||||
workingJSON := jsonData
|
||||
var err error
|
||||
if len(legacyOverride) > 0 {
|
||||
workingJSON, err = applyOperationsLegacy(workingJSON, legacyOverride)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
// 使用新方法
|
||||
result, err := applyOperations(string(jsonData), operations, conditionContext)
|
||||
result, err := applyOperations(string(workingJSON), operations, conditionContext)
|
||||
return []byte(result), err
|
||||
}
|
||||
|
||||
@@ -129,6 +139,20 @@ func ApplyParamOverride(jsonData []byte, paramOverride map[string]interface{}, c
|
||||
return applyOperationsLegacy(jsonData, paramOverride)
|
||||
}
|
||||
|
||||
func buildLegacyParamOverride(paramOverride map[string]interface{}) map[string]interface{} {
|
||||
if len(paramOverride) == 0 {
|
||||
return nil
|
||||
}
|
||||
legacy := make(map[string]interface{}, len(paramOverride))
|
||||
for key, value := range paramOverride {
|
||||
if strings.EqualFold(strings.TrimSpace(key), "operations") {
|
||||
continue
|
||||
}
|
||||
legacy[key] = value
|
||||
}
|
||||
return legacy
|
||||
}
|
||||
|
||||
func ApplyParamOverrideWithRelayInfo(jsonData []byte, info *RelayInfo) ([]byte, error) {
|
||||
paramOverride := getParamOverrideMap(info)
|
||||
if len(paramOverride) == 0 {
|
||||
|
||||
@@ -74,6 +74,48 @@ func TestApplyParamOverrideTrimNoop(t *testing.T) {
|
||||
assertJSONEqual(t, `{"model":"gpt-4","temperature":0.7}`, string(out))
|
||||
}
|
||||
|
||||
func TestApplyParamOverrideMixedLegacyAndOperations(t *testing.T) {
|
||||
input := []byte(`{"model":"openai/gpt-4","temperature":0.7}`)
|
||||
override := map[string]interface{}{
|
||||
"temperature": 0.2,
|
||||
"top_p": 0.95,
|
||||
"operations": []interface{}{
|
||||
map[string]interface{}{
|
||||
"path": "model",
|
||||
"mode": "trim_prefix",
|
||||
"value": "openai/",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
out, err := ApplyParamOverride(input, override, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("ApplyParamOverride returned error: %v", err)
|
||||
}
|
||||
assertJSONEqual(t, `{"model":"gpt-4","temperature":0.2,"top_p":0.95}`, string(out))
|
||||
}
|
||||
|
||||
func TestApplyParamOverrideMixedLegacyAndOperationsConflictPrefersOperations(t *testing.T) {
|
||||
input := []byte(`{"model":"openai/gpt-4","temperature":0.7}`)
|
||||
override := map[string]interface{}{
|
||||
"model": "legacy-model",
|
||||
"temperature": 0.2,
|
||||
"operations": []interface{}{
|
||||
map[string]interface{}{
|
||||
"path": "model",
|
||||
"mode": "set",
|
||||
"value": "op-model",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
out, err := ApplyParamOverride(input, override, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("ApplyParamOverride returned error: %v", err)
|
||||
}
|
||||
assertJSONEqual(t, `{"model":"op-model","temperature":0.2}`, string(out))
|
||||
}
|
||||
|
||||
func TestApplyParamOverrideTrimRequiresValue(t *testing.T) {
|
||||
// trim_prefix requires value example:
|
||||
// {"operations":[{"path":"model","mode":"trim_prefix"}]}
|
||||
@@ -1429,6 +1471,44 @@ func TestApplyParamOverrideWithRelayInfoSyncRuntimeHeaders(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestApplyParamOverrideWithRelayInfoMixedLegacyAndOperations(t *testing.T) {
|
||||
info := &RelayInfo{
|
||||
RequestHeaders: map[string]string{
|
||||
"Originator": "Codex CLI",
|
||||
},
|
||||
ChannelMeta: &ChannelMeta{
|
||||
ParamOverride: map[string]interface{}{
|
||||
"temperature": 0.2,
|
||||
"operations": []interface{}{
|
||||
map[string]interface{}{
|
||||
"mode": "pass_headers",
|
||||
"value": []interface{}{"Originator"},
|
||||
},
|
||||
},
|
||||
},
|
||||
HeadersOverride: map[string]interface{}{
|
||||
"X-Static": "legacy-static",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
out, err := ApplyParamOverrideWithRelayInfo([]byte(`{"model":"gpt-5","temperature":0.7}`), info)
|
||||
if err != nil {
|
||||
t.Fatalf("ApplyParamOverrideWithRelayInfo returned error: %v", err)
|
||||
}
|
||||
assertJSONEqual(t, `{"model":"gpt-5","temperature":0.2}`, string(out))
|
||||
|
||||
if !info.UseRuntimeHeadersOverride {
|
||||
t.Fatalf("expected runtime header override to be enabled")
|
||||
}
|
||||
if info.RuntimeHeadersOverride["x-static"] != "legacy-static" {
|
||||
t.Fatalf("expected x-static to be preserved, got: %v", info.RuntimeHeadersOverride["x-static"])
|
||||
}
|
||||
if info.RuntimeHeadersOverride["originator"] != "Codex CLI" {
|
||||
t.Fatalf("expected originator header to be passed, got: %v", info.RuntimeHeadersOverride["originator"])
|
||||
}
|
||||
}
|
||||
|
||||
func TestApplyParamOverrideWithRelayInfoMoveAndCopyHeaders(t *testing.T) {
|
||||
info := &RelayInfo{
|
||||
ChannelMeta: &ChannelMeta{
|
||||
|
||||
Reference in New Issue
Block a user