mirror of
https://github.com/jasonxu114514/opencode2api.git
synced 2026-12-05 08:02:53 +00:00
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 <AiFLYF@users.noreply.github.com>
This commit is contained in:
1 parent
ab55803cdc
commit
f9ef5693ab
2 files changed
+22
-1
No files matched your search
@@ -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 {
|
||||
|
||||
@@ -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.
|
||||
|
||||
Reference in new issue
Block a user