This commit is contained in:
Ferdinand Prantl 2026-01-05 11:35:27 +03:30 committed by GitHub
commit 7855e783e2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 21 additions and 3 deletions

View File

@ -1696,7 +1696,18 @@ func waitForStream(c *gin.Context, ch chan any) {
} }
func streamResponse(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 { c.Stream(func(w io.Writer) bool {
val, ok := <-ch val, ok := <-ch
if !ok { if !ok {
@ -1733,8 +1744,15 @@ func streamResponse(c *gin.Context, ch chan any) {
return false return false
} }
// Delineate chunks with new-line delimiter if sse {
bts = append(bts, '\n') // 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 { if _, err := w.Write(bts); err != nil {
slog.Info(fmt.Sprintf("streamResponse: w.Write failed with %s", err)) slog.Info(fmt.Sprintf("streamResponse: w.Write failed with %s", err))
return false return false