Skip to content

Commit 5893e78

Browse files
committed
fix(gateway): OpenCode 分组自动生成 x-opencode-session 支持通用客户端,发布 1.2.65
- 通用 OpenAI 客户端(Cherry Studio)不携带 x-opencode-session,仅透传无法满足上游要求 - 网关在缺失时生成 UUID 会话标识,覆盖 chat raw 与 responses passthrough 路径 - 无数据库迁移
1 parent 8714c70 commit 5893e78

5 files changed

Lines changed: 24 additions & 1 deletion

File tree

backend/cmd/server/VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.2.64
1+
1.2.65

backend/internal/service/openai_gateway_chat_completions_raw.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ func (s *OpenAIGatewayService) forwardAsRawChatCompletions(
129129
upstreamReq.Header.Set("user-agent", userAgent)
130130
}
131131
account.ApplyHeaderOverrides(upstreamReq.Header)
132+
ensureOpencodeSessionHeader(account, upstreamReq)
132133

133134
proxyURL := ""
134135
if account.Proxy != nil {

backend/internal/service/openai_gateway_service.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5196,6 +5196,7 @@ func (s *OpenAIGatewayService) buildUpstreamRequestOpenAIPassthrough(
51965196
if account.IsOpenAIOAuth() {
51975197
enforceCodexIdentityHeaders(req.Header)
51985198
}
5199+
ensureOpencodeSessionHeader(account, req)
51995200

52005201
return req, nil
52015202
}
@@ -6670,6 +6671,7 @@ func (s *OpenAIGatewayService) buildUpstreamRequest(ctx context.Context, c *gin.
66706671
if account.IsOpenAIOAuth() {
66716672
enforceCodexIdentityHeaders(req.Header)
66726673
}
6674+
ensureOpencodeSessionHeader(account, req)
66736675

66746676
return req, nil
66756677
}

backend/internal/service/opencode_go_gateway.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,26 @@ import (
1212

1313
"github.com/Wei-Shaw/sub2api/internal/pkg/apicompat"
1414
"github.com/gin-gonic/gin"
15+
"github.com/google/uuid"
1516
"github.com/tidwall/sjson"
1617
)
1718

1819
const opencodeResponsesRawEndpoint = "/v1/responses"
1920

21+
// ensureOpencodeSessionHeader 确保 OpenCode 上游请求携带 x-opencode-session。
22+
// OpenCode Go 上游用该头做会话路由和 prompt 缓存;原生 OpenCode 客户端会自动携带,
23+
// 但通用 OpenAI 兼容客户端(如 Cherry Studio)不会。网关在缺失时生成一个稳定标识,
24+
// 避免上游报 "missing x-opencode-session" 400。
25+
func ensureOpencodeSessionHeader(account *Account, req *http.Request) {
26+
if account == nil || req == nil || !account.IsOpencode() {
27+
return
28+
}
29+
if req.Header.Get("x-opencode-session") != "" {
30+
return
31+
}
32+
req.Header.Set("x-opencode-session", uuid.New().String())
33+
}
34+
2035
// OpencodeGoResolvedModel is the single routing decision shared by all three
2136
// OpenCode Go ingress protocols. Protocol selection must use UpstreamModel,
2237
// after channel/account mapping and provider-prefix normalization.

docs/site/content/docs/operations/changelog.mdx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ title: 更新日志
33
description: 按版本记录 Pixel API 的主要功能更新。
44
---
55

6+
## v1.2.65
7+
8+
- 修复 OpenCode 分组对通用客户端(Cherry Studio 等)仍报 `missing x-opencode-session` 的问题:网关现在在客户端未携带该头时自动生成会话标识,而不仅是透传。
9+
- 数据库无新增迁移。
10+
611
## v1.2.64
712

813
- 补完 OpenCode Go 会话标识 `x-opencode-session` 的透传:Chat Completions 的 Anthropic 桥接路径此前仍会丢弃该请求头导致上游 400,现覆盖全部转发路径。

0 commit comments

Comments
 (0)