Skip to content
Closed
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
25 changes: 19 additions & 6 deletions sdk/api/handlers/openai/openai_responses_compact_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package openai

import (
"context"
"encoding/json"
"errors"
"net/http"
"net/http/httptest"
Expand All @@ -20,6 +21,7 @@ type compactCaptureExecutor struct {
alt string
sourceFormat string
calls int
payload []byte
}

func (e *compactCaptureExecutor) Identifier() string { return "test-provider" }
Expand All @@ -28,6 +30,7 @@ func (e *compactCaptureExecutor) Execute(ctx context.Context, auth *coreauth.Aut
e.calls++
e.alt = opts.Alt
e.sourceFormat = opts.SourceFormat.String()
e.payload = append(e.payload[:0], req.Payload...)
return coreexecutor.Response{Payload: []byte(`{"ok":true}`)}, nil
}

Expand All @@ -47,7 +50,7 @@ func (e *compactCaptureExecutor) HttpRequest(context.Context, *coreauth.Auth, *h
return nil, errors.New("not implemented")
}

func TestOpenAIResponsesCompactRejectsStream(t *testing.T) {
func TestOpenAIResponsesCompactNormalizesStream(t *testing.T) {
gin.SetMode(gin.TestMode)
executor := &compactCaptureExecutor{}
manager := coreauth.NewManager(nil, nil, nil)
Expand All @@ -67,16 +70,26 @@ func TestOpenAIResponsesCompactRejectsStream(t *testing.T) {
router := gin.New()
router.POST("/v1/responses/compact", h.Compact)

req := httptest.NewRequest(http.MethodPost, "/v1/responses/compact", strings.NewReader(`{"model":"test-model","stream":true}`))
req := httptest.NewRequest(http.MethodPost, "/v1/responses/compact", strings.NewReader(`{"model":"test-model","input":"hello","stream":true}`))
req.Header.Set("Content-Type", "application/json")
resp := httptest.NewRecorder()
router.ServeHTTP(resp, req)

if resp.Code != http.StatusBadRequest {
t.Fatalf("status = %d, want %d", resp.Code, http.StatusBadRequest)
if resp.Code != http.StatusOK {
t.Fatalf("status = %d, want %d", resp.Code, http.StatusOK)
}
if executor.calls != 1 {
t.Fatalf("executor calls = %d, want 1", executor.calls)
}
var payload map[string]any
if err := json.Unmarshal(executor.payload, &payload); err != nil {
t.Fatalf("payload JSON: %v", err)
}
if executor.calls != 0 {
t.Fatalf("executor calls = %d, want 0", executor.calls)
if _, ok := payload["stream"]; ok {
t.Fatalf("payload still includes stream: %s", string(executor.payload))
}
if executor.alt != "responses/compact" {
t.Fatalf("alt = %q, want %q", executor.alt, "responses/compact")
}
}

Expand Down
9 changes: 0 additions & 9 deletions sdk/api/handlers/openai/openai_responses_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -405,15 +405,6 @@ func (h *OpenAIResponsesAPIHandler) Compact(c *gin.Context) {
}

streamResult := gjson.GetBytes(rawJSON, "stream")
if streamResult.Type == gjson.True {
c.JSON(http.StatusBadRequest, handlers.ErrorResponse{
Error: handlers.ErrorDetail{
Message: "Streaming not supported for compact responses",
Type: "invalid_request_error",
},
})
return
}
if streamResult.Exists() {
if updated, err := sjson.DeleteBytes(rawJSON, "stream"); err == nil {
rawJSON = updated
Expand Down
Loading