Fix OpenAI capability rewrite dropping query string (#3168)

This commit is contained in:
woody
2025-11-28 17:44:22 +08:00
committed by jingze
parent 8ec48b3b85
commit 48433a6549
3 changed files with 153 additions and 3 deletions

View File

@@ -93,6 +93,19 @@ func MapRequestPathByCapability(apiName string, originPath string, mapping map[s
if !exist {
return ""
}
mappedPathOnly := mappedPath
mappedQuery := ""
if queryIndex := strings.Index(mappedPathOnly, "?"); queryIndex >= 0 {
mappedPathOnly = mappedPathOnly[:queryIndex]
mappedQuery = mappedPath[queryIndex:]
}
// 将查询字符串从原始路径中剥离,避免干扰正则匹配 video_id 等占位符
pathOnly := originPath
query := ""
if queryIndex := strings.Index(originPath, "?"); queryIndex >= 0 {
pathOnly = originPath[:queryIndex]
query = originPath[queryIndex:]
}
if strings.Contains(mappedPath, "{") && strings.Contains(mappedPath, "}") {
replacements := []struct {
regx *regexp.Regexp
@@ -108,8 +121,8 @@ func MapRequestPathByCapability(apiName string, originPath string, mapping map[s
}
for _, r := range replacements {
if r.regx.MatchString(originPath) {
subMatch := r.regx.FindStringSubmatch(originPath)
if r.regx.MatchString(pathOnly) {
subMatch := r.regx.FindStringSubmatch(pathOnly)
if subMatch == nil {
continue
}
@@ -118,12 +131,25 @@ func MapRequestPathByCapability(apiName string, originPath string, mapping map[s
continue
}
id := subMatch[index]
mappedPath = r.regx.ReplaceAllStringFunc(mappedPath, func(s string) string {
mappedPathOnly = r.regx.ReplaceAllStringFunc(mappedPathOnly, func(s string) string {
return strings.Replace(s, "{"+r.key+"}", id, 1)
})
}
}
}
if mappedQuery != "" {
mappedPath = mappedPathOnly + mappedQuery
} else {
mappedPath = mappedPathOnly
}
if query != "" {
// 保留原始查询参数,例如 variant=thumbnail
if strings.Contains(mappedPath, "?") {
mappedPath = mappedPath + "&" + strings.TrimPrefix(query, "?")
} else {
mappedPath += query
}
}
return mappedPath
}