mirror of
https://github.com/Wei-Shaw/sub2api.git
synced 2026-12-05 16:07:33 +00:00
Problem: on native Responses streams the gateway stages every event of an
attempt until the first semantic output, so it can still fail over to
another account. Upstream keepalive events are staged with the rest. When
the upstream is silent for a long time (long high-effort reasoning), the
client only receives ":" comment heartbeats. Codex resets its stream idle
timer on parsed SSE events only, so it gives up after its 300 second idle
timeout ("idle timeout waiting for SSE") while the upstream is still
working, and the gateway keeps reading a stream nobody will receive.
Fix: while the first-output stage is open, write upstream keepalive
events (both the "event: keepalive" + data form and the data-only form)
straight to the client, the same way the comment keepalive bypasses the
stage: only the stable SSE headers are committed, the attempt's
lifecycle events and account headers stay private, and the bytes are
recorded with recordOpenAIStreamKeepaliveBytes so they still do not count
as client output and pre-output failover keeps working. The WebSocket to
HTTP bridge already forwards keepalive before first output this way.
Events after output started are unchanged.
Keepalive events are not output, so they do not stop the first-output
timer: with gateway.openai_first_output_timeout_seconds set, a stalled
attempt still fails over on time while the client stays connected.
Test: TestOpenAINativeStreamForwardsUpstreamKeepaliveBeforeFirstOutput
covers both frame shapes. TestOpenAINativeStreamKeepaliveThenFailoverContinuesWithNextAccount
checks that failover after a forwarded keepalive streams the next account
from its start. TestOpenAINativeStreamKeepaliveDoesNotResetFirstOutputTimeout
checks that paced keepalives do not postpone the timeout.
TestOpenAINativeStreamKeepaliveAfterOutputStaysInOrder guards the
post-output path.
TestOpenAIStreamMetadataAndKeepaliveBeforeOverloadFailOver,
TestOpenAIStreamKeepaliveOnlyFailureDoesNotRecordFirstToken and
TestOpenAINativeMetadataAndKeepaliveDoNotDisarmFirstOutputTimeout asserted
that the client receives nothing before first output on the native path.
Their native cases now assert that only the upstream keepalive frames
reach the client and that those bytes still do not count as output; the
passthrough cases are unchanged.
182 lines
7.7 KiB
Go
182 lines
7.7 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
"io"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/Wei-Shaw/sub2api/internal/config"
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestOpenAIVisibleOutputClassification(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
data string
|
|
eventType string
|
|
want bool
|
|
}{
|
|
{name: "keepalive", data: `{"type":"keepalive"}`, want: false},
|
|
{name: "created", data: `{"type":"response.created"}`, want: false},
|
|
{name: "empty output item", data: `{"type":"response.output_item.added","item":{"id":"item_test","type":"reasoning","summary":[]}}`, want: false},
|
|
{name: "empty delta", data: `{"type":"response.output_text.delta","delta":""}`, want: false},
|
|
{name: "text delta", data: `{"type":"response.output_text.delta","delta":"test output"}`, want: true},
|
|
{name: "tool arguments", data: `{"type":"response.function_call_arguments.delta","delta":"{}"}`, want: true},
|
|
{name: "partial image", data: `{"type":"response.image_generation_call.partial_image","partial_image_b64":"dGVzdA=="}`, want: true},
|
|
{name: "completed image item", data: `{"type":"response.output_item.done","item":{"id":"item_test","type":"image_generation_call","result":"dGVzdA=="}}`, want: true},
|
|
{name: "empty completed", data: `{"type":"response.completed","response":{"id":"resp_test","output":[]}}`, want: false},
|
|
{name: "completed with output usage only", data: `{"type":"response.completed","response":{"id":"resp_test","usage":{"input_tokens":1,"output_tokens":2}}}`, want: false},
|
|
{name: "completed with text", data: `{"type":"response.completed","response":{"id":"resp_test","output":[{"type":"message","content":[{"type":"output_text","text":"test output"}]}]}}`, want: true},
|
|
{name: "done marker", data: `[DONE]`, want: false},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
require.Equal(t, tt.want, openAIStreamDataStartsVisibleOutput(tt.data, tt.eventType))
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestOpenAIResponsesTTFTStartsAtVisibleOutput(t *testing.T) {
|
|
for _, passthrough := range []bool{false, true} {
|
|
name := "native"
|
|
if passthrough {
|
|
name = "passthrough"
|
|
}
|
|
t.Run(name, func(t *testing.T) {
|
|
result := runSyntheticVisibleTTFTStream(t, passthrough, 120*time.Millisecond, 0, OpenAITTFTModeVisible,
|
|
`{"type":"response.output_text.delta","delta":"test output"}`)
|
|
require.NotNil(t, result.firstTokenMs)
|
|
require.GreaterOrEqual(t, *result.firstTokenMs, 100)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestOpenAIResponsesTTFTStartsAtCompletedImage(t *testing.T) {
|
|
for _, passthrough := range []bool{false, true} {
|
|
name := "native"
|
|
if passthrough {
|
|
name = "passthrough"
|
|
}
|
|
t.Run(name, func(t *testing.T) {
|
|
result := runSyntheticVisibleTTFTStream(t, passthrough, 120*time.Millisecond, 0, OpenAITTFTModeVisible,
|
|
`{"type":"response.output_item.done","item":{"id":"item_test","type":"image_generation_call","result":"dGVzdA=="}}`)
|
|
require.NotNil(t, result.firstTokenMs)
|
|
require.GreaterOrEqual(t, *result.firstTokenMs, 100)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestOpenAINativeMetadataAndKeepaliveDoNotDisarmFirstOutputTimeout(t *testing.T) {
|
|
gin.SetMode(gin.TestMode)
|
|
svc := &OpenAIGatewayService{cfg: &config.Config{Gateway: config.GatewayConfig{
|
|
MaxLineSize: defaultMaxLineSize,
|
|
OpenAIFirstOutputTimeoutSeconds: 1,
|
|
}}}
|
|
reader, writer := io.Pipe()
|
|
writerDone := make(chan struct{})
|
|
go func() {
|
|
defer close(writerDone)
|
|
defer func() { _ = writer.Close() }()
|
|
_, _ = io.WriteString(writer, "data: {\"type\":\"response.created\",\"response\":{\"id\":\"resp_test\"}}\n\n")
|
|
_, _ = io.WriteString(writer, "data: {\"type\":\"response.output_item.added\",\"item\":{\"id\":\"item_test\",\"type\":\"reasoning\",\"summary\":[]}}\n\n")
|
|
_, _ = io.WriteString(writer, "data: {\"type\":\"keepalive\"}\n\n")
|
|
time.Sleep(1200 * time.Millisecond)
|
|
}()
|
|
|
|
recorder := httptest.NewRecorder()
|
|
c, _ := gin.CreateTestContext(recorder)
|
|
c.Request = httptest.NewRequest(http.MethodPost, "/v1/responses", nil)
|
|
resp := &http.Response{StatusCode: http.StatusOK, Header: http.Header{}, Body: reader}
|
|
account := &Account{ID: 1, Name: "account_test", Platform: PlatformOpenAI}
|
|
|
|
_, err := svc.handleStreamingResponse(context.Background(), resp, c, account, time.Now(), "test-model", "test-model")
|
|
var failoverErr *UpstreamFailoverError
|
|
require.ErrorAs(t, err, &failoverErr)
|
|
require.True(t, failoverErr.SafeToFailoverAfterWrite)
|
|
// Only the upstream keepalive event reaches the client; it is not output.
|
|
require.Equal(t, "data: {\"type\":\"keepalive\"}\n\n", recorder.Body.String())
|
|
require.Equal(t, -1, OpenAICompactKeepaliveAdjustedWrittenSize(c))
|
|
select {
|
|
case <-writerDone:
|
|
case <-time.After(time.Second):
|
|
t.Fatal("synthetic upstream writer did not exit")
|
|
}
|
|
}
|
|
|
|
func TestOpenAIResponsesTTFTDefaultsToSemanticOutput(t *testing.T) {
|
|
for _, passthrough := range []bool{false, true} {
|
|
name := "native"
|
|
if passthrough {
|
|
name = "passthrough"
|
|
}
|
|
t.Run(name, func(t *testing.T) {
|
|
result := runSyntheticVisibleTTFTStream(t, passthrough, 120*time.Millisecond, 0, "",
|
|
`{"type":"response.output_text.delta","delta":"test output"}`)
|
|
require.NotNil(t, result.firstTokenMs)
|
|
require.Less(t, *result.firstTokenMs, 100)
|
|
})
|
|
}
|
|
}
|
|
|
|
func runSyntheticVisibleTTFTStream(t *testing.T, passthrough bool, visibleDelay time.Duration, timeoutSeconds int, ttftMode string, visibleEvent string) *openaiStreamingResult {
|
|
t.Helper()
|
|
gin.SetMode(gin.TestMode)
|
|
mode := ttftMode
|
|
if mode == "" {
|
|
mode = OpenAITTFTModeSemantic
|
|
}
|
|
gatewayForwardingCache.Store(&cachedGatewayForwardingSettings{openAITTFTMode: mode, expiresAt: time.Now().Add(time.Minute).UnixNano()})
|
|
t.Cleanup(func() {
|
|
gatewayForwardingCache.Store(&cachedGatewayForwardingSettings{openAITTFTMode: OpenAITTFTModeSemantic, expiresAt: time.Now().Add(time.Minute).UnixNano()})
|
|
})
|
|
svc := &OpenAIGatewayService{cfg: &config.Config{Gateway: config.GatewayConfig{
|
|
MaxLineSize: defaultMaxLineSize,
|
|
OpenAIFirstOutputTimeoutSeconds: timeoutSeconds,
|
|
}}}
|
|
reader, writer := io.Pipe()
|
|
writerDone := make(chan struct{})
|
|
go func() {
|
|
defer close(writerDone)
|
|
defer func() { _ = writer.Close() }()
|
|
_, _ = io.WriteString(writer, "data: {\"type\":\"response.created\",\"response\":{\"id\":\"resp_test\"}}\n\n")
|
|
_, _ = io.WriteString(writer, "data: {\"type\":\"response.output_item.added\",\"item\":{\"id\":\"item_test\",\"type\":\"reasoning\",\"summary\":[]}}\n\n")
|
|
time.Sleep(visibleDelay)
|
|
_, _ = io.WriteString(writer, "data: "+visibleEvent+"\n\n")
|
|
_, _ = io.WriteString(writer, "data: {\"type\":\"response.completed\",\"response\":{\"id\":\"resp_test\",\"usage\":{\"input_tokens\":1,\"output_tokens\":1}}}\n\n")
|
|
}()
|
|
|
|
recorder := httptest.NewRecorder()
|
|
c, _ := gin.CreateTestContext(recorder)
|
|
c.Request = httptest.NewRequest(http.MethodPost, "/v1/responses", nil)
|
|
resp := &http.Response{StatusCode: http.StatusOK, Header: http.Header{}, Body: reader}
|
|
account := &Account{ID: 1, Name: "account_test", Platform: PlatformOpenAI}
|
|
started := time.Now()
|
|
|
|
var result *openaiStreamingResult
|
|
var err error
|
|
if passthrough {
|
|
var passthroughResult *openaiStreamingResultPassthrough
|
|
passthroughResult, err = svc.handleStreamingResponsePassthrough(context.Background(), resp, c, account, started, "test-model", "test-model")
|
|
if passthroughResult != nil {
|
|
result = &openaiStreamingResult{firstTokenMs: passthroughResult.firstTokenMs}
|
|
}
|
|
} else {
|
|
result, err = svc.handleStreamingResponse(context.Background(), resp, c, account, started, "test-model", "test-model")
|
|
}
|
|
require.NoError(t, err)
|
|
require.NotNil(t, result)
|
|
require.Contains(t, recorder.Body.String(), `"type":"response.output_item.added"`)
|
|
require.Contains(t, recorder.Body.String(), visibleEvent)
|
|
select {
|
|
case <-writerDone:
|
|
case <-time.After(time.Second):
|
|
t.Fatal("synthetic upstream writer did not exit")
|
|
}
|
|
return result
|
|
}
|