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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) |
Expand Down
24 changes: 24 additions & 0 deletions changelog/v0.2.7.md
Original file line number Diff line number Diff line change
@@ -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/<version>`, 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.
2 changes: 1 addition & 1 deletion deploy/dokploy/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions docs/CONFIGURATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion docs/SPRING-AI.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ on Spring AI; the gateway never depends on it.
<dependency>
<groupId>io.github.an1ndra</groupId>
<artifactId>path-spring-ai</artifactId>
<version>0.2.6</version>
<version>0.2.7</version>
</dependency>
```

Expand Down
2 changes: 1 addition & 1 deletion path-gateway/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>io.github.an1ndra</groupId>
<artifactId>path-parent</artifactId>
<version>0.2.6</version>
<version>0.2.7</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -47,10 +51,44 @@ public UpstreamRequest build(ChatRequestSpec spec, ModelRoute route, Provider pr
}

Map<String, String> 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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ public static Map<String, String> 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;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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<String, String> 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<String, String> 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");
}
}
2 changes: 1 addition & 1 deletion path-spring-ai/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>io.github.an1ndra</groupId>
<artifactId>path-parent</artifactId>
<version>0.2.6</version>
<version>0.2.7</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

<groupId>io.github.an1ndra</groupId>
<artifactId>path-parent</artifactId>
<version>0.2.6</version>
<version>0.2.7</version>
<packaging>pom</packaging>
<name>path-parent</name>
<description>Build parent for Path, the provider-agnostic LLM gateway, and its Spring AI adapter</description>
Expand Down
Loading