handle data:[DONE] more robustly

although https://learn.microsoft.com/en-us/azure/ai-services/openai/reference#request-body says

>stream    boolean    Whether to stream back partial progress. If set, tokens will be sent as data-only server-sent events as they become available, with the stream terminated by a data: [DONE] message

but what I see is `data:[DONE]` (without space), e.g.

```
curl -q --location 'http://localhost:8080/openai/deployments/gpt-4o/chat/completions?api-version=2023-07-01-preview' \
--header 'Authorization: Bearer <token>' \
--header 'X-Forwarded-Client-Cert: secret' \
--header 'Content-Type: application/json' \
--data '{
    "stream": true,
    "messages": [
        {
            "role": "user",
            "content": "hello."
        }
    ]
}'
data:{"choices":[],"created":0,"id":"","model":"","object":"","prompt_filter_results":[{"prompt_index":0,"content_filter_results":{"hate":{"filtered":false,"severity":"safe"},"self_harm":{"filtered":false,"severity":"safe"},"sexual":{"filtered":false,"severity":"safe"},"violence":{"filtered":false,"severity":"safe"}}}]}

data:{"choices":[{"content_filter_results":{},"delta":{"refusal":null,"role":"assistant"},"finish_reason":null,"index":0}],"created":1746741184,"id":"chatcmpl-BV3SqfEtCuk3f8fW5x7azEkELkJZ0","model":"gpt-4o-2024-05-13","object":"chat.completion.chunk","system_fingerprint":"fp_ee1d74bde0"}
...
data:{"choices":[{"content_filter_results":{"hate":{"filtered":false,"severity":"safe"},"self_harm":{"filtered":false,"severity":"safe"},"sexual":{"filtered":false,"severity":"safe"},"violence":{"filtered":false,"severity":"safe"}},"delta":{"content":"?"},"finish_reason":null,"index":0}],"created":1746741184,"id":"chatcmpl-BV3SqfEtCuk3f8fW5x7azEkELkJZ0","model":"gpt-4o-2024-05-13","object":"chat.completion.chunk","system_fingerprint":"fp_ee1d74bde0"}

data:{"choices":[{"content_filter_results":{},"delta":{},"finish_reason":"stop","index":0}],"created":1746741184,"id":"chatcmpl-BV3SqfEtCuk3f8fW5x7azEkELkJZ0","model":"gpt-4o-2024-05-13","object":"chat.completion.chunk","system_fingerprint":"fp_ee1d74bde0"}

data:[DONE]
```
This commit is contained in:
Zhuyi Xue 2025-05-08 15:09:39 -07:00 • committed by GitHub
parent ec0180bf95
commit 5db24e4bbc
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -58,7 +58,7 @@ function parseDataLine(line: string): any {
}
function parseSseLine(line: string): { done: boolean; data: any } {
if (line.startsWith("data: [DONE]")) {
if (line.startsWith("data:[DONE]") || line.startsWith("data: [DONE]")) {
return { done: true, data: undefined };
}
if (line.startsWith("data:")) {