From f9ef5693ab58d4c2c03f63329399732f8b0e9007 Mon Sep 17 00:00:00 2001 From: FLYF <150890698+AiFLYF@users.noreply.github.com> Date: Fri, 23 Oct 2026 09:23:58 +0800 Subject: [PATCH] feat(upstream): follow the OpenCode 2.1 client's version and /inference routes (#141) Captured a 2.1.1 CLI request with the plugin hook that produced the 2.0.32 capture. The body is byte-identical; what moved is the version string and the route. The 2.1 client groups /inference routes by provider family, so a chat request goes to /inference/openai/v1/chat/completions, responses to /inference/openai/v1/responses and messages to /inference/anthropic/v1/messages. A 2.0.32 client posted the protocol path directly and that route still answers, so this is fidelity rather than a fix. The family segment is only inserted for the /inference origin: a custom base URL and the /zen/go lane keep the flat route, because the grouping belongs to /inference alone. Co-authored-by: AiFLYF --- internal/gateway/upstream.go | 21 +++++++++++++++++++++ internal/httpx/client.go | 2 +- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/internal/gateway/upstream.go b/internal/gateway/upstream.go index 04075d3..2f37df1 100644 --- a/internal/gateway/upstream.go +++ b/internal/gateway/upstream.go @@ -1369,6 +1369,9 @@ func (g *Gateway) recordUpstreamAttempt(ctx context.Context, route models.Route, // lane passes anonymousZenKey. func upstreamRequest(ctx context.Context, baseURL string, protocol wire.Protocol, body []byte, ids identity.RequestIDs, key string) (*http.Request, error) { endpoint := strings.TrimRight(baseURL, "/") + wire.Path(protocol) + if !models.IsV1Upstream(baseURL) { + endpoint = strings.TrimRight(baseURL, "/") + v2EndpointPath(baseURL, protocol) + } if protocol == wire.Anthropic && !models.IsV1Upstream(baseURL) { endpoint = withAnthropicBeta(endpoint) } @@ -1394,6 +1397,24 @@ func upstreamRequest(ctx context.Context, baseURL string, protocol wire.Protocol return req, nil } +// v2EndpointPath returns the route a 2.1 client posts to on a v2 origin. The +// 2.1 client groups /inference routes by provider family, so the family +// segment sits between the origin and the protocol path (a 2.0.32 client +// posted the protocol path directly, and that route still answers). Origins +// that are not the /inference gateway — a custom base, or /zen/go — keep the +// flat route, because the family grouping is specific to /inference. +func v2EndpointPath(baseURL string, protocol wire.Protocol) string { + parsed, err := url.Parse(baseURL) + if err != nil || !strings.HasSuffix(strings.TrimRight(parsed.Path, "/"), "/inference") { + return wire.Path(protocol) + } + family := "openai" + if protocol == wire.Anthropic { + family = "anthropic" + } + return "/" + family + wire.Path(protocol) +} + // withAnthropicBeta marks an Anthropic endpoint as beta the way the OpenCode // 2.x client does, preserving any query a configured base URL already carries. func withAnthropicBeta(endpoint string) string { diff --git a/internal/httpx/client.go b/internal/httpx/client.go index c8aa2b8..dcf6172 100644 --- a/internal/httpx/client.go +++ b/internal/httpx/client.go @@ -4,7 +4,7 @@ package httpx // the v2 lanes (/inference and /zen/go). Upstream accepts a range of releases // (the version number itself is not validated), so this only needs bumping // when it starts rejecting older ones. -const v2ClientVersion = "2.0.32" +const v2ClientVersion = "2.1.1" // v1ClientVersion is the OpenCode 1.x release the gateway claims to be on the // v1 lane (/zen), and v1BunVersion is the Bun runtime that release ships with.