mirror of
https://github.com/chenyme/grok2api.git
synced 2026-12-06 00:01:05 +00:00
fix: preserve output-loop code across audits and SSE
This commit is contained in:
1 parent
c6acb00e0f
commit
f97be4e553
3 files changed
+38
-2
No files matched your search
@@ -58,6 +58,12 @@ func TestIsUpstreamResponseEmpty(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsUpstreamOutputLoop(t *testing.T) {
|
||||
if !IsUpstreamOutputLoop(fmt.Errorf("read body: %w", ErrUpstreamOutputLoop)) || IsUpstreamOutputLoop(io.EOF) {
|
||||
t.Fatal("output-loop sentinel classification failed")
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsClientRequestCancel(t *testing.T) {
|
||||
idleCtx, idleCancel := context.WithCancelCause(context.Background())
|
||||
idleCancel(ErrUpstreamStreamIdleTimeout)
|
||||
|
||||
@@ -1536,9 +1536,13 @@ func streamAbortTrailer(protocol streamProtocol, cause error, meta responseMetad
|
||||
}
|
||||
return []byte("event: response.failed\ndata: " + string(payload) + "\n\n")
|
||||
case streamProtocolAnthropic:
|
||||
anthropicMessage := message
|
||||
if code == "upstream_output_loop" {
|
||||
anthropicMessage = code + ": " + message
|
||||
}
|
||||
payload, err := json.Marshal(map[string]any{
|
||||
"type": "error",
|
||||
"error": map[string]any{"type": "api_error", "message": message},
|
||||
"error": map[string]any{"type": "api_error", "message": anthropicMessage},
|
||||
})
|
||||
if err != nil {
|
||||
return nil
|
||||
|
||||
@@ -1056,6 +1056,10 @@ func TestClassifyCopyErrorDistinguishesClientFromUpstream(t *testing.T) {
|
||||
if got := classifyCopyError(idle, idleErr); got != "upstream_stream_idle_timeout" {
|
||||
t.Fatalf("idle timeout = %q", got)
|
||||
}
|
||||
loopErr := fmt.Errorf("%w: %w", errUpstreamStreamRead, neterror.ErrUpstreamOutputLoop)
|
||||
if got := classifyCopyError(context.Background(), loopErr); got != "upstream_output_loop" {
|
||||
t.Fatalf("output loop = %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCopyStreamWritesTerminalOnIdleTimeout(t *testing.T) {
|
||||
@@ -1096,7 +1100,7 @@ func TestCopyStreamWritesTerminalOnOutputLoop(t *testing.T) {
|
||||
}{
|
||||
{name: "chat", protocol: streamProtocolChat, want: []string{`"code":"upstream_output_loop"`, "data: [DONE]"}},
|
||||
{name: "responses", protocol: streamProtocolResponses, want: []string{`"type":"response.failed"`, `"code":"server_error"`, `"message":"upstream_output_loop:`, `"status":"failed"`}},
|
||||
{name: "anthropic", protocol: streamProtocolAnthropic, want: []string{`"type":"error"`, "上游输出陷入循环"}},
|
||||
{name: "anthropic", protocol: streamProtocolAnthropic, want: []string{`"type":"error"`, `"message":"upstream_output_loop: 上游输出陷入循环"`}},
|
||||
}
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
@@ -1640,6 +1644,28 @@ func TestWriteResultUpstreamCutStaysUpstreamInterrupted(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestWriteResultOutputLoopFinalizesDistinctCode(t *testing.T) {
|
||||
gin.SetMode(gin.TestMode)
|
||||
handler := NewHandler(nil, nil, 1<<20)
|
||||
var finalCode string
|
||||
result := &gateway.Result{
|
||||
StatusCode: http.StatusOK,
|
||||
Status: "200 OK",
|
||||
Header: http.Header{"Content-Type": {"text/event-stream"}},
|
||||
Body: io.NopCloser(&outputLoopErrorReader{}),
|
||||
Finalize: func(_ gateway.Usage, _, code string) { finalCode = code },
|
||||
}
|
||||
router := gin.New()
|
||||
router.POST("/", func(c *gin.Context) {
|
||||
handler.writeResult(c, result, true, streamProtocolResponses)
|
||||
})
|
||||
recorder := httptest.NewRecorder()
|
||||
router.ServeHTTP(recorder, httptest.NewRequest(http.MethodPost, "/", nil))
|
||||
if recorder.Code != http.StatusOK || finalCode != "upstream_output_loop" || !strings.Contains(recorder.Body.String(), `"message":"upstream_output_loop:`) {
|
||||
t.Fatalf("status=%d finalize=%q body=%q", recorder.Code, finalCode, recorder.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestProjectStreamFailureDiagnosticBoundsErrorMessage(t *testing.T) {
|
||||
diagnostic := projectStreamFailureDiagnostic([]byte(`{"type":"error","error":{"code":"server_error","message":"` + strings.Repeat("错误", maxStreamFailureDiagnosticBytes) + `"},"output":"must-not-be-audited"}`))
|
||||
if !diagnostic.BodyTruncated || len(diagnostic.Body) > maxStreamFailureDiagnosticBytes || len(diagnostic.Body) == 0 || !utf8.Valid(diagnostic.Body) || strings.Contains(string(diagnostic.Body), "must-not-be-audited") {
|
||||
|
||||
Reference in new issue
Block a user