Skip to content

fix(openai): stream compact responses to Codex clients#3876

Closed
specialpointcentral wants to merge 1 commit into
Wei-Shaw:mainfrom
specialpointcentral:fix/compact-stream-sse
Closed

fix(openai): stream compact responses to Codex clients#3876
specialpointcentral wants to merge 1 commit into
Wei-Shaw:mainfrom
specialpointcentral:fix/compact-stream-sse

Conversation

@specialpointcentral

Copy link
Copy Markdown

Summary

  • preserve whether a compact request originally asked for streaming before compact body normalization removes stream
  • keep upstream /responses/compact requests non-streaming, but wrap successful compact JSON responses as a single response.completed SSE event for streaming Codex clients
  • cover OpenAI OAuth passthrough compact responses where upstream returns text/plain JSON

Close: #3875

Tests

  • docker run --rm -v /tmp/sub2api-compact-pr:/workspace -w /workspace/backend golang:1.26.5 go test ./internal/service ./internal/handler -run 'TestOpenAIGatewayService_OAuthPassthrough_CompactClientStreamWrapsJSONAsSSE|TestOpenAIGatewayService_OAuthPassthrough_CompactOnlyModelMappingOverridesUpstreamModel|TestOpenAIGatewayService_Forward_CompactOnlyModelMappingOverridesOAuthUpstreamModel|TestNormalizeOpenAIResponsesCompactRequest_BodySignalPromoted|TestNormalizeOpenAIResponsesCompactRequest_PathBasedNoDoubleSuffix'

Copilot AI review requested due to automatic review settings July 9, 2026 12:30

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adjusts the OpenAI /responses/compact flow to preserve whether a client originally requested streaming (even though the compact body normalization drops stream), and to support Codex clients that expect an SSE stream by wrapping successful compact JSON responses into a single response.completed SSE event.

Changes:

  • Preserve the original compact request’s stream: true signal on the gin.Context before compact-body normalization removes it.
  • When a compact request was originally streaming, return the non-streaming upstream compact JSON as a single response.completed SSE event (including OAuth passthrough where upstream may return text/plain JSON).
  • Add test coverage for the SSE wrapping behavior and for propagation of the stream-requested flag.

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
backend/internal/service/openai_gateway_service.go Adds a context key to track whether a compact request originally requested streaming.
backend/internal/service/openai_gateway_response_handling.go Wraps compact JSON responses as response.completed SSE for streaming Codex clients.
backend/internal/service/openai_gateway_request_body.go Adds helpers to store/read the compact “client stream requested” flag on gin.Context.
backend/internal/service/openai_gateway_passthrough.go Applies the same compact SSE wrapping behavior to OAuth passthrough non-streaming responses.
backend/internal/service/openai_compact_model_mapping_test.go Adds a test validating SSE wrapping for OAuth passthrough compact responses (including text/plain JSON).
backend/internal/handler/openai_gateway_handler.go Captures stream: true from compact request bodies before normalization drops stream.
backend/internal/handler/openai_gateway_compact_body_signal_test.go Extends tests to assert the stream-requested flag is set when stream: true is present.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread backend/internal/service/openai_gateway_response_handling.go
@specialpointcentral specialpointcentral force-pushed the fix/compact-stream-sse branch 3 times, most recently from c80216a to 5bcc701 Compare July 9, 2026 13:32
@specialpointcentral

Copy link
Copy Markdown
Author

close because: #3880

@specialpointcentral specialpointcentral deleted the fix/compact-stream-sse branch July 9, 2026 13:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

v0.1.147 openai 无法压缩上下文了!!!

2 participants