feat(model-mapper): sync model header on remap and disable reroute (#3827)

Signed-off-by: 钰诚 <yucheng.lxr@alibaba-inc.com>
This commit is contained in:
rinfx
2026-05-15 14:38:54 +08:00
committed by GitHub
parent a93847e07f
commit e497d8017a
2 changed files with 266 additions and 13 deletions

View File

@@ -42,6 +42,7 @@ type Config struct {
prefixModelMapping []ModelMapping
defaultModel string
enableOnPathSuffix []string
modelToHeader string
}
func parseConfig(json gjson.Result, config *Config) error {
@@ -50,6 +51,11 @@ func parseConfig(json gjson.Result, config *Config) error {
config.modelKey = "model"
}
config.modelToHeader = json.Get("modelToHeader").String()
if config.modelToHeader == "" {
config.modelToHeader = "x-higress-llm-model-final"
}
modelMapping := json.Get("modelMapping")
if modelMapping.Exists() && !modelMapping.IsObject() {
return errors.New("modelMapping must be an object")
@@ -144,6 +150,8 @@ func onHttpRequestHeaders(ctx wrapper.HttpContext, config Config) types.Action {
return types.ActionContinue
}
// Disable re-route since the plugin may modify some headers related to the chosen route.
ctx.DisableReroute()
// Prepare for body processing
proxywasm.RemoveHttpRequestHeader("content-length")
// 100MB buffer limit
@@ -182,6 +190,9 @@ func onHttpRequestBody(ctx wrapper.HttpContext, config Config, body []byte) type
}
}
// update x-higress-llm-model-final header
proxywasm.ReplaceHttpRequestHeader(config.modelToHeader, newModel)
log.Debugf("set header %s: %s", config.modelToHeader, newModel)
if newModel != "" && newModel != oldModel {
newBody, err := sjson.SetBytes(body, config.modelKey, newModel)
if err != nil {