ChuanFF opened a new pull request, #13090:
URL: https://github.com/apache/apisix/pull/13090
## Problem
The original SSE parser used `ngx.re.split("\n\n")`, which failed when
events were fragmented across HTTP chunks. This caused data loss and truncated
AI responses, especially in high-latency networks.
fix: https://github.com/apache/apisix/issues/12733
## Solution
- **State Machine Parser:** Replaced split logic with a
character-by-character state machine.
- event_start_index: Start of current SSE event
- line_start_index: Start of current line within event
- value_start_index: Start of value after : separator
- **Buffer Management:** Preserves incomplete events between chunks to
ensure correct reassembly.
- **Spec Compliant:** Properly handles multi-line `data:` fields and
`[DONE]` markers.
## Testing
- Added `t/plugin/ai-proxy-sse-split.t` with 3 scenarios:
- Single event split across chunks
- Multi-line `data` fields
- JSON fragmentation within events
## Changes
- `apisix/plugins/ai-drivers/sse.lua`: New parser logic
- `apisix/plugins/ai-drivers/openai-base.lua`: Buffer state propagation
- `t/plugin/ai-proxy-sse-split.t`: New test suite
## Compatibility
✅ Fully backward compatible. Buffer parameter is optional.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]