From be9250a7a6da8e424d90bef6422813e8f1b777d8 Mon Sep 17 00:00:00 2001 From: Luqman Rizal Date: Tue, 17 Nov 2026 11:42:54 +0800 Subject: [PATCH] fix(llm): prevent duplicate version segments in API paths --- internal/llm/fantasy_adapter.go | 7 +++++ internal/llm/url.go | 46 ++++++++++++++++++++++++++++++--- 2 files changed, 50 insertions(+), 3 deletions(-) diff --git a/internal/llm/fantasy_adapter.go b/internal/llm/fantasy_adapter.go index b70990ceb..93325520e 100644 --- a/internal/llm/fantasy_adapter.go +++ b/internal/llm/fantasy_adapter.go @@ -204,6 +204,13 @@ func fantasyOpenAIBaseURL(baseURL, endpointPath string) string { for _, suffix := range []string{"/chat/completions", "/responses"} { path = strings.TrimSuffix(path, suffix) } + if path == "" || path == "/" { + return base + } + // The SDK appends /chat/completions (or /responses) to whatever base it is + // handed, so an API root that already carries its version — a gateway at + // https:///v1/unified-model, say — must not receive a second /v1. + path = dropDuplicateVersionPrefix(base, path) if path == "" || path == "/" || strings.HasSuffix(base, path) { return base } diff --git a/internal/llm/url.go b/internal/llm/url.go index ac88abf86..9875db932 100644 --- a/internal/llm/url.go +++ b/internal/llm/url.go @@ -21,12 +21,52 @@ func joinProviderPath(baseURL, apiPath string) string { if base == "" { return path } - if strings.HasPrefix(path, "/v1/") && strings.HasSuffix(base, "/v1") { - path = strings.TrimPrefix(path, "/v1") - } + path = dropDuplicateVersionPrefix(base, path) return base + path } +// dropDuplicateVersionPrefix removes a leading version segment ("/v1", +// "/v1beta", …) from apiPath when the base URL already carries that same +// version as one of its own path segments. +// +// The built-in provider definitions keep the version inside the API path +// (/v1/chat/completions, /v1/models), which is correct for a host-root base URL +// (https://api.deepseek.com) and for a version-root base URL +// (https://api.deepseek.com/v1). It is wrong for an API root that continues +// after the version: joining https://gateway.example.com/v1/unified-model with +// /v1/models produced .../v1/unified-model/v1/models, which the upstream +// rejects because the extra segment reads as a resource id. A base URL whose +// path has no version segment at all (a reverse-proxy prefix such as +// https://gateway.example.com/openai) keeps the version, preserving the +// prefix-then-/v1 layout. +func dropDuplicateVersionPrefix(baseURL, apiPath string) string { + segment, _, _ := strings.Cut(strings.TrimPrefix(apiPath, "/"), "/") + if !isVersionPathSegment(segment) { + return apiPath + } + for _, baseSegment := range strings.Split(baseURL, "/") { + if baseSegment == segment { + return strings.TrimPrefix(apiPath, "/"+segment) + } + } + return apiPath +} + +// isVersionPathSegment reports whether path segment looks like an API version +// ("v1", "v4", "v1beta") rather than a resource name, so only version prefixes +// are ever de-duplicated. +func isVersionPathSegment(segment string) bool { + if len(segment) < 2 || segment[0] != 'v' || segment[1] < '0' || segment[1] > '9' { + return false + } + for _, r := range segment[2:] { + if (r < '0' || r > '9') && (r < 'a' || r > 'z') { + return false + } + } + return true +} + // newAdminConfiguredHTTPClient returns a client for URLs configured by a system // administrator. Local/internal endpoints are reachable by default; operators // can restore loopback/private/metadata blocking with