From 0996ad21b1ad9547a89fa40c1d9357331ae7aa23 Mon Sep 17 00:00:00 2001 From: woody Date: Thu, 3 Jul 2025 20:34:14 +0800 Subject: [PATCH] feat(ai-proxy): add basePathHandling option (#2535) Co-authored-by: Se7en --- .../extensions/ai-proxy/provider/provider.go | 28 ++++++++++++++----- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/plugins/wasm-go/extensions/ai-proxy/provider/provider.go b/plugins/wasm-go/extensions/ai-proxy/provider/provider.go index 30e457721..278a4f062 100644 --- a/plugins/wasm-go/extensions/ai-proxy/provider/provider.go +++ b/plugins/wasm-go/extensions/ai-proxy/provider/provider.go @@ -5,6 +5,7 @@ import ( "errors" "math/rand" "net/http" + "path" "regexp" "strings" @@ -18,8 +19,9 @@ import ( ) type ( - ApiName string - Pointcut string + ApiName string + Pointcut string + basePathHandling string ) const ( @@ -143,6 +145,9 @@ const ( wildcard = "*" defaultTimeout = 2 * 60 * 1000 // ms + + basePathHandlingRemovePrefix basePathHandling = "removePrefix" + basePathHandlingPrepend basePathHandling = "prepend" ) type providerInitializer interface { @@ -358,8 +363,10 @@ type ProviderConfig struct { // @Title zh-CN 额外支持的ai能力 // @Description zh-CN 开放的ai能力和urlpath映射,例如: {"openai/v1/chatcompletions": "/v1/chat/completions"} capabilities map[string]string - // @Title zh-CN 如果配置了subPath,将会先移除请求path中该前缀,再进行后续处理 - subPath string `required:"false" yaml:"subPath" json:"subPath"` + // @Title zh-CN 如果配置了basePath,可用于在请求path中移除该前缀,或添加至请求path中,默认为进行移除 + basePath string `required:"false" yaml:"basePath" json:"basePath"` + // @Title zh-CN basePathHandling用于指定basePath的处理方式,可选值:removePrefix、prepend + basePathHandling basePathHandling `required:"false" yaml:"basePathHandling" json:"basePathHandling"` } func (c *ProviderConfig) GetId() string { @@ -527,7 +534,11 @@ func (c *ProviderConfig) FromJson(json gjson.Result) { c.capabilities[capability] = pathJson.String() } } - c.subPath = json.Get("subPath").String() + c.basePath = json.Get("basePath").String() + c.basePathHandling = basePathHandling(json.Get("basePathHandling").String()) + if c.basePath != "" && c.basePathHandling == "" { + c.basePathHandling = basePathHandlingRemovePrefix + } } func (c *ProviderConfig) Validate() error { @@ -839,12 +850,15 @@ func (c *ProviderConfig) handleRequestBody( func (c *ProviderConfig) handleRequestHeaders(provider Provider, ctx wrapper.HttpContext, apiName ApiName) { headers := util.GetOriginalRequestHeaders() originPath := headers.Get(":path") - if c.subPath != "" { - headers.Set(":path", strings.TrimPrefix(originPath, c.subPath)) + if c.basePath != "" && c.basePathHandling == basePathHandlingRemovePrefix { + headers.Set(":path", strings.TrimPrefix(originPath, c.basePath)) } if handler, ok := provider.(TransformRequestHeadersHandler); ok { handler.TransformRequestHeaders(ctx, apiName, headers) } + if c.basePath != "" && c.basePathHandling == basePathHandlingPrepend && !strings.HasPrefix(headers.Get(":path"), c.basePath) { + headers.Set(":path", path.Join(c.basePath, headers.Get(":path"))) + } if headers.Get(":path") != originPath { headers.Set("X-ENVOY-ORIGINAL-PATH", originPath) }