Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions internal/llm/fantasy_adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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://<host>/v1/unified-model, say — must not receive a second /v1.
path = dropDuplicateVersionPrefix(base, path)
if path == "" || path == "/" || strings.HasSuffix(base, path) {
return base
}
Expand Down
46 changes: 43 additions & 3 deletions internal/llm/url.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading