diff --git a/CHANGELOG.md b/CHANGELOG.md index 223dfb2..5eec869 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ verbatim as the body of the matching GitHub release. | Version | Date | Notes | |---|---|---| +| v0.2.7 | 2026-11-23 | [changelog/v0.2.7.md](changelog/v0.2.7.md) | | v0.2.6 | 2026-11-23 | [changelog/v0.2.6.md](changelog/v0.2.6.md) | | v0.2.5 | 2026-11-23 | [changelog/v0.2.5.md](changelog/v0.2.5.md) | | v0.2.4 | 2026-11-23 | [changelog/v0.2.4.md](changelog/v0.2.4.md) | diff --git a/changelog/v0.2.7.md b/changelog/v0.2.7.md new file mode 100644 index 0000000..bf66382 --- /dev/null +++ b/changelog/v0.2.7.md @@ -0,0 +1,24 @@ +# v0.2.7 + +OpenCode Go works through the gateway. + +## Fixed + +- **Requests to an OpenCode provider now carry `x-opencode-session`.** OpenCode Go + rejects any request without it with 400 `upstream_invalid_request` ("cannot be routed + efficiently"), which made every model on that provider uncallable through the gateway. + The value is derived from the upstream model and the first user message, so one + conversation keeps one id across retries, fallbacks and streaming, and a new + conversation gets a new one. +- **Every OpenAI-compatible request identifies the gateway.** The upstream `User-Agent` + was `Java-http-client/`, which reads as a generic library; OpenCode's Go terms + ask a client to name itself, so the header is now `path-gateway`. + +Both changes are additive. Providers other than `opencode*` send the same headers as +before apart from the user agent. + +## Notes + +- `x-opencode-session` applies to slugs starting with `opencode`, matching the documented + provider ids (`opencode`, `opencode-go`). A provider with a different slug pointed at + OpenCode's endpoint needs one of those slugs or the header will be missing again. diff --git a/deploy/dokploy/README.md b/deploy/dokploy/README.md index b8d4003..1e75ea3 100644 --- a/deploy/dokploy/README.md +++ b/deploy/dokploy/README.md @@ -20,7 +20,7 @@ database. The console is always a plain Application. 3. Compose path: `deploy/dokploy/compose.yml`. 4. **Trigger Type: On Tag**, with auto-deploy enabled. The gateway is a production service; a pull request or a push to `Develop` must not restart it. A release tag - (`v0.2.6`) is the only thing that deploys — see "When it deploys" below. + (`v0.2.7`) is the only thing that deploys — see "When it deploys" below. 5. **Environment** (the compose file references all of these): ```bash diff --git a/docs/CONFIGURATION.md b/docs/CONFIGURATION.md index 2abf486..cdaff9c 100644 --- a/docs/CONFIGURATION.md +++ b/docs/CONFIGURATION.md @@ -137,6 +137,13 @@ Each route needs the upstream model id and a price per million tokens; a `0.00` records usage but never charges it. `POST /admin/models/sync` imports a provider's list and prices only ids in its built-in table, so check anything it leaves at `0.00`. +**OpenCode Go** requires two things no OpenAI SDK sends: a client that names itself in +`User-Agent` and a stable `x-opencode-session` per conversation, otherwise it answers 400 +`upstream_invalid_request`. The gateway sends `User-Agent: path-gateway` and derives the +session id from the model plus the first user message, so the same conversation keeps one +id across retries and fallbacks. Point the provider at +`https://opencode.ai/inference/go/openai/v1`. + ### Gemini Google exposes an OpenAI-compatible endpoint, so a plain `OPENAI`-type provider works: diff --git a/docs/SPRING-AI.md b/docs/SPRING-AI.md index 78f6698..6b66519 100644 --- a/docs/SPRING-AI.md +++ b/docs/SPRING-AI.md @@ -13,7 +13,7 @@ on Spring AI; the gateway never depends on it. io.github.an1ndra path-spring-ai - 0.2.6 + 0.2.7 ``` diff --git a/path-gateway/pom.xml b/path-gateway/pom.xml index 2638280..f35877c 100644 --- a/path-gateway/pom.xml +++ b/path-gateway/pom.xml @@ -7,7 +7,7 @@ io.github.an1ndra path-parent - 0.2.6 + 0.2.7 ../pom.xml diff --git a/path-gateway/src/main/java/com/anindra/path/gateway/OpenAiCompatibleAdapter.java b/path-gateway/src/main/java/com/anindra/path/gateway/OpenAiCompatibleAdapter.java index a36dbbe..0c720b3 100644 --- a/path-gateway/src/main/java/com/anindra/path/gateway/OpenAiCompatibleAdapter.java +++ b/path-gateway/src/main/java/com/anindra/path/gateway/OpenAiCompatibleAdapter.java @@ -11,7 +11,11 @@ import org.springframework.stereotype.Component; import java.math.BigDecimal; +import java.nio.charset.StandardCharsets; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; import java.time.Instant; +import java.util.HexFormat; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; @@ -47,10 +51,44 @@ public UpstreamRequest build(ChatRequestSpec spec, ModelRoute route, Provider pr } Map headers = ProviderUrls.headers(provider, apiKey, stream); + if (isOpenCode(provider)) { + headers.put("x-opencode-session", sessionId(route.getUpstreamModelId(), body)); + } return new UpstreamRequest(ProviderUrls.chat(provider, route.getUpstreamModelId()), headers, body.toString()); } + /** + * OpenCode Go (and its OpenAI-compatible endpoint) rejects a request without + * {@code x-opencode-session}: "Request is missing x-opencode-session and cannot be + * routed efficiently". The header is meant to be stable per conversation, so it is + * derived from the model and the first user message: the same conversation keeps the + * same id across retries and fallbacks, a new conversation gets a new one. + */ + private boolean isOpenCode(Provider provider) { + return provider.getSlug() != null && provider.getSlug().startsWith("opencode"); + } + + private String sessionId(String model, ObjectNode body) { + StringBuilder seed = new StringBuilder(model).append('\n'); + JsonNode messages = body.get("messages"); + if (messages != null && messages.isArray()) { + for (JsonNode message : messages) { + if ("user".equals(message.path("role").asText())) { + seed.append(message.path("content")); + break; + } + } + } + try { + byte[] digest = MessageDigest.getInstance("SHA-256") + .digest(seed.toString().getBytes(StandardCharsets.UTF_8)); + return "path-" + HexFormat.of().formatHex(digest).substring(0, 32); + } catch (NoSuchAlgorithmException e) { + return "path-gateway"; + } + } + @Override public JsonNode parseFullResponse(JsonNode upstream, ChatRequestSpec spec, ModelRoute route) { ObjectNode copy = upstream.deepCopy(); diff --git a/path-gateway/src/main/java/com/anindra/path/gateway/ProviderUrls.java b/path-gateway/src/main/java/com/anindra/path/gateway/ProviderUrls.java index 147711d..3af82f6 100644 --- a/path-gateway/src/main/java/com/anindra/path/gateway/ProviderUrls.java +++ b/path-gateway/src/main/java/com/anindra/path/gateway/ProviderUrls.java @@ -56,6 +56,10 @@ public static Map headers(Provider provider, String apiKey, bool headers.put("Authorization", "Bearer " + apiKey); } headers.put("Accept", stream ? "text/event-stream" : "application/json"); + // Java's HttpClient identifies itself as "Java-http-client/21". Some upstreams + // (OpenCode Go among them) require a client to name itself instead of looking like + // a generic library, and the version helps when reading provider-side logs. + headers.put("User-Agent", "path-gateway"); return headers; } diff --git a/path-gateway/src/test/java/com/anindra/path/gateway/OpenCodeSessionHeaderTest.java b/path-gateway/src/test/java/com/anindra/path/gateway/OpenCodeSessionHeaderTest.java new file mode 100644 index 0000000..3bd04fa --- /dev/null +++ b/path-gateway/src/test/java/com/anindra/path/gateway/OpenCodeSessionHeaderTest.java @@ -0,0 +1,72 @@ +package com.anindra.path.gateway; + +import com.anindra.path.domain.ModelRoute; +import com.anindra.path.domain.Provider; +import com.anindra.path.domain.ProviderType; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ObjectNode; +import org.junit.jupiter.api.Test; + +import java.math.BigDecimal; +import java.util.Map; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * OpenCode Go answers 400 "Request is missing x-opencode-session" to any request without + * that header, including traffic that reaches it through the gateway. The header is meant + * to be stable per conversation, so it is derived from the model and the first user + * message. + */ +class OpenCodeSessionHeaderTest { + + private final ObjectMapper mapper = new ObjectMapper(); + private final OpenAiCompatibleAdapter adapter = new OpenAiCompatibleAdapter(mapper); + + private Provider provider(String slug) { + return new Provider(slug, slug, ProviderType.OPENAI, + "https://opencode.ai/inference/go/openai/v1"); + } + + private ModelRoute route() { + return new ModelRoute("deepseek-v4.1-flash", 1L, "deepseek-v4.1-flash", + new BigDecimal("0.15"), new BigDecimal("0.60"), 10); + } + + private Map headers(String slug, String firstMessage) throws Exception { + ChatRequestSpec spec = ChatRequestSpec.parse((ObjectNode) mapper.readTree( + "{\"model\":\"deepseek-v4.1-flash\",\"messages\":[{\"role\":\"system\",\"content\":\"be brief\"}," + + "{\"role\":\"user\",\"content\":\"" + firstMessage + "\"}]}")); + return adapter.build(spec, route(), provider(slug), "key", false).headers(); + } + + @Test + void openCodeRequestsCarryASessionHeader() throws Exception { + Map headers = headers("opencode-go", "first question"); + + assertThat(headers).containsKey("x-opencode-session"); + assertThat(headers.get("x-opencode-session")).startsWith("path-").hasSize(37); + } + + @Test + void theSessionIsStableWithinAConversation() throws Exception { + assertThat(headers("opencode-go", "first question")) + .isEqualTo(headers("opencode-go", "first question")); + } + + @Test + void aDifferentConversationGetsADifferentSession() throws Exception { + assertThat(headers("opencode-go", "first question").get("x-opencode-session")) + .isNotEqualTo(headers("opencode-go", "another question").get("x-opencode-session")); + } + + @Test + void otherProvidersAreUntouched() throws Exception { + assertThat(headers("groq", "first question")).doesNotContainKey("x-opencode-session"); + } + + @Test + void everyRequestIdentifiesTheGateway() throws Exception { + assertThat(headers("groq", "hi")).containsEntry("User-Agent", "path-gateway"); + } +} diff --git a/path-spring-ai/pom.xml b/path-spring-ai/pom.xml index 389a84c..ee6d67b 100644 --- a/path-spring-ai/pom.xml +++ b/path-spring-ai/pom.xml @@ -7,7 +7,7 @@ io.github.an1ndra path-parent - 0.2.6 + 0.2.7 ../pom.xml diff --git a/pom.xml b/pom.xml index 3b9ece2..2cb94fb 100644 --- a/pom.xml +++ b/pom.xml @@ -13,7 +13,7 @@ io.github.an1ndra path-parent - 0.2.6 + 0.2.7 pom path-parent Build parent for Path, the provider-agnostic LLM gateway, and its Spring AI adapter