mirror of
https://github.com/Wei-Shaw/sub2api.git
synced 2026-11-25 15:23:25 +00:00
合并 main 解决拆分冲突后,将原先 Forward 内的 body-signal 提升重构到 handler 的 compact 归一化入口之前,修复原方案的四个问题: - reqStream 未重推导:body-signal 原始请求带 stream:true,Forward 级提升 后 compact 上游返回 JSON(Accept: application/json)却被流式 handler 解析,"stream ended before a terminal event" 会触发最多 max_account_switches 次换号 failover,且每次都白烧一次上游 compact 配额; handler 级提升让白名单归一化先删除 stream,reqStream 自然为 false。 - requireCompact 调度过滤失效:原方案 path 改写发生在 requireCompact 判定 之后,调度器不会过滤不支持 compact 的账号;现在改写先于该判定。 - passthrough / Grok / chat-completions 桥接分支位于 Forward 检测点之前, passthrough 账号完全无法命中;handler 级改写对所有分支生效。 - body 归一化口径不一致:body-signal 现在与 path-based 一样走白名单归一化 (prompt_cache_key 等一并删除),而非仅依赖 OAuth 黑名单转换。 检测函数导出为 HasCompactionTriggerInInput 供 handler 使用,保留原 PR 的 7 个单测;新增 6 个 handler 级回归测试(提升、codex 别名路由、尾斜杠、 子路径不误伤、path-based 无双重后缀、普通请求不受影响)。 Refs #3777
120 lines
4.6 KiB
Go
120 lines
4.6 KiB
Go
package handler
|
|
|
|
import (
|
|
"bytes"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"github.com/Wei-Shaw/sub2api/internal/service"
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/stretchr/testify/require"
|
|
"github.com/tidwall/gjson"
|
|
"go.uber.org/zap"
|
|
)
|
|
|
|
func newCompactBodySignalTestContext(t *testing.T, path string, body []byte) *gin.Context {
|
|
t.Helper()
|
|
gin.SetMode(gin.TestMode)
|
|
rec := httptest.NewRecorder()
|
|
c, _ := gin.CreateTestContext(rec)
|
|
c.Request = httptest.NewRequest(http.MethodPost, path, bytes.NewReader(body))
|
|
c.Request.Header.Set("Content-Type", "application/json")
|
|
return c
|
|
}
|
|
|
|
// body-signal 提升后必须与 path-based compact 走同一条链路:
|
|
// path 改写、requireCompact 判定、stream/store/prompt_cache_key 归一化删除。
|
|
// 回归防护:若 stream 字段存活,Forward 会用流式 handler 解析 compact 的
|
|
// JSON 响应,导致 "stream ended before a terminal event" 的换号 failover 风暴。
|
|
func TestNormalizeOpenAIResponsesCompactRequest_BodySignalPromoted(t *testing.T) {
|
|
h := &OpenAIGatewayHandler{}
|
|
body := []byte(`{
|
|
"model":"gpt-5.5",
|
|
"stream":true,
|
|
"store":true,
|
|
"prompt_cache_key":"pck-signal-1",
|
|
"input":[
|
|
{"type":"message","role":"user","content":"hello"},
|
|
{"type":"compaction_trigger"}
|
|
]
|
|
}`)
|
|
c := newCompactBodySignalTestContext(t, "/v1/responses", body)
|
|
|
|
normalized, ok := h.normalizeOpenAIResponsesCompactRequest(c, zap.NewNop(), body)
|
|
require.True(t, ok)
|
|
|
|
require.Equal(t, "/v1/responses/compact", c.Request.URL.Path)
|
|
require.True(t, isOpenAIRemoteCompactPath(c))
|
|
|
|
require.False(t, gjson.GetBytes(normalized, "stream").Exists())
|
|
require.False(t, gjson.GetBytes(normalized, "store").Exists())
|
|
require.False(t, gjson.GetBytes(normalized, "prompt_cache_key").Exists())
|
|
require.Equal(t, "gpt-5.5", gjson.GetBytes(normalized, "model").String())
|
|
require.True(t, gjson.GetBytes(normalized, "input").IsArray())
|
|
|
|
reqStream, streamOK := parseOpenAICompatibleStream(normalized)
|
|
require.True(t, streamOK)
|
|
require.False(t, reqStream)
|
|
|
|
seed, exists := c.Get(service.OpenAICompactSessionSeedKeyForTest())
|
|
require.True(t, exists)
|
|
require.Equal(t, "pck-signal-1", seed)
|
|
}
|
|
|
|
func TestNormalizeOpenAIResponsesCompactRequest_BodySignalTrailingSlash(t *testing.T) {
|
|
h := &OpenAIGatewayHandler{}
|
|
body := []byte(`{"model":"gpt-5.5","input":[{"type":"compaction_trigger"}]}`)
|
|
c := newCompactBodySignalTestContext(t, "/v1/responses/", body)
|
|
|
|
_, ok := h.normalizeOpenAIResponsesCompactRequest(c, zap.NewNop(), body)
|
|
require.True(t, ok)
|
|
require.Equal(t, "/v1/responses/compact", c.Request.URL.Path)
|
|
}
|
|
|
|
func TestNormalizeOpenAIResponsesCompactRequest_CodexDirectAliasPromoted(t *testing.T) {
|
|
h := &OpenAIGatewayHandler{}
|
|
body := []byte(`{"model":"gpt-5.5","input":[{"type":"compaction_trigger"}]}`)
|
|
c := newCompactBodySignalTestContext(t, "/backend-api/codex/responses", body)
|
|
|
|
_, ok := h.normalizeOpenAIResponsesCompactRequest(c, zap.NewNop(), body)
|
|
require.True(t, ok)
|
|
require.Equal(t, "/backend-api/codex/responses/compact", c.Request.URL.Path)
|
|
}
|
|
|
|
func TestNormalizeOpenAIResponsesCompactRequest_NoTriggerUntouched(t *testing.T) {
|
|
h := &OpenAIGatewayHandler{}
|
|
body := []byte(`{"model":"gpt-5.5","stream":true,"input":[{"type":"message","role":"user","content":"hello"}]}`)
|
|
c := newCompactBodySignalTestContext(t, "/v1/responses", body)
|
|
|
|
normalized, ok := h.normalizeOpenAIResponsesCompactRequest(c, zap.NewNop(), body)
|
|
require.True(t, ok)
|
|
require.Equal(t, "/v1/responses", c.Request.URL.Path)
|
|
require.False(t, isOpenAIRemoteCompactPath(c))
|
|
require.Equal(t, body, normalized)
|
|
require.True(t, gjson.GetBytes(normalized, "stream").Bool())
|
|
}
|
|
|
|
func TestNormalizeOpenAIResponsesCompactRequest_PathBasedNoDoubleSuffix(t *testing.T) {
|
|
h := &OpenAIGatewayHandler{}
|
|
body := []byte(`{"model":"gpt-5.5","stream":true,"store":true,"input":[{"type":"message","role":"user","content":"hello"}]}`)
|
|
c := newCompactBodySignalTestContext(t, "/v1/responses/compact", body)
|
|
|
|
normalized, ok := h.normalizeOpenAIResponsesCompactRequest(c, zap.NewNop(), body)
|
|
require.True(t, ok)
|
|
require.Equal(t, "/v1/responses/compact", c.Request.URL.Path)
|
|
require.False(t, gjson.GetBytes(normalized, "stream").Exists())
|
|
require.False(t, gjson.GetBytes(normalized, "store").Exists())
|
|
}
|
|
|
|
func TestNormalizeOpenAIResponsesCompactRequest_SubpathNotPromoted(t *testing.T) {
|
|
h := &OpenAIGatewayHandler{}
|
|
body := []byte(`{"model":"gpt-5.5","input":[{"type":"compaction_trigger"}]}`)
|
|
c := newCompactBodySignalTestContext(t, "/v1/responses/resp_123/cancel", body)
|
|
|
|
normalized, ok := h.normalizeOpenAIResponsesCompactRequest(c, zap.NewNop(), body)
|
|
require.True(t, ok)
|
|
require.Equal(t, "/v1/responses/resp_123/cancel", c.Request.URL.Path)
|
|
require.Equal(t, body, normalized)
|
|
}
|