mirror of
https://github.com/Wei-Shaw/sub2api.git
synced 2026-10-16 07:33:06 +08:00
Fixes #4653 ## Problem The /v1/responses endpoint (Responses → Anthropic bridge) failed when upstream providers returned SSE events in compact format without spaces after colons (e.g. 'event:message_start' instead of 'event: message_start'). This affected Kimi API (api.kimi.com) and potentially other upstreams, causing 'Upstream stream ended without a response' errors. ## Root Cause The SSE parser in gateway_forward_as_responses.go used strict prefix matching with hardcoded spaces: if !strings.HasPrefix(line, "event: ") { ... } if !strings.HasPrefix(dataLine, "data: ") { ... } According to the SSE spec (https://html.spec.whatwg.org/multipage/server-sent-events.html), the space after the colon is optional. Both formats are valid: event: message_start ✓ event:message_start ✓ ## Solution Introduced parseAnthropicSSEField() helper that: - Accepts both 'field: value' and 'field:value' formats - Uses TrimSpace() to handle optional/multiple spaces - Returns (value, ok) for consistent error handling Applied to both streaming handlers: - handleResponsesBufferedStreamingResponse - handleResponsesStreamingResponse ## Testing Added comprehensive unit tests: - TestParseAnthropicSSEField: validates parser with 8 edge cases - TestHandleResponsesBufferedStreamingResponse_CompactSSEFormat - TestHandleResponsesStreamingResponse_CompactSSEFormat All existing tests pass, confirming backward compatibility.