Respond with text/event-stream if requested

This commit is contained in:
Ferdinand Prantl 2025-07-30 16:56:00 +02:00
parent 522c11a763
commit 71c8df1359
No known key found for this signature in database
GPG Key ID: 043B1941D0934C53
1 changed files with 21 additions and 3 deletions

View File

@ -1686,7 +1686,18 @@ func waitForStream(c *gin.Context, ch chan any) {
}
func streamResponse(c *gin.Context, ch chan any) {
c.Header("Content-Type", "application/x-ndjson")
// If the preferred content type is text/event-stream, assume SSE
accept := c.GetHeader("Accept")
sse := strings.HasPrefix(accept, "text/event-stream")
var contentType string
if sse {
contentType = "text/event-stream"
} else {
contentType = "application/x-ndjson"
}
c.Header("Content-Type", contentType)
c.Stream(func(w io.Writer) bool {
val, ok := <-ch
if !ok {
@ -1723,8 +1734,15 @@ func streamResponse(c *gin.Context, ch chan any) {
return false
}
// Delineate chunks with new-line delimiter
bts = append(bts, '\n')
if sse {
// For text/event-stream, format chunks as events of the default type "message"
bts = append([]byte{'d', 'a', 't', 'a', ':', ' '}, bts...)
bts = append(bts, '\n', '\n')
} else {
// For application/x-ndjson, delineate chunks with new-line delimiter
bts = append(bts, '\n')
}
if _, err := w.Write(bts); err != nil {
slog.Info(fmt.Sprintf("streamResponse: w.Write failed with %s", err))
return false