From 3b2ab0d7bd345b3fc74103b41f53a4045ce85ad3 Mon Sep 17 00:00:00 2001 From: Luis Pater Date: Sat, 26 Jul 2025 17:16:55 +0800 Subject: [PATCH] Fix SSE headers initialization for `geminiStreamGenerateContent` and `internalStreamGenerateContent` - Added conditional logic to properly initialize SSE headers only when `alt` is empty. - Ensured headers like `Content-Type`, `Cache-Control`, and `Access-Control-Allow-Origin` are set for better compatibility. --- internal/api/cli-handlers.go | 9 +++++++++ internal/api/gemini-handlers.go | 11 +++++++++-- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/internal/api/cli-handlers.go b/internal/api/cli-handlers.go index 9e41bd54..75488907 100644 --- a/internal/api/cli-handlers.go +++ b/internal/api/cli-handlers.go @@ -99,6 +99,15 @@ func (h *APIHandlers) CLIHandler(c *gin.Context) { } func (h *APIHandlers) internalStreamGenerateContent(c *gin.Context, rawJson []byte) { + alt := h.getAlt(c) + + if alt == "" { + c.Header("Content-Type", "text/event-stream") + c.Header("Cache-Control", "no-cache") + c.Header("Connection", "keep-alive") + c.Header("Access-Control-Allow-Origin", "*") + } + // Get the http.Flusher interface to manually flush the response. flusher, ok := c.Writer.(http.Flusher) if !ok { diff --git a/internal/api/gemini-handlers.go b/internal/api/gemini-handlers.go index ba1ff3c1..cf56d36d 100644 --- a/internal/api/gemini-handlers.go +++ b/internal/api/gemini-handlers.go @@ -107,6 +107,15 @@ func (h *APIHandlers) GeminiHandler(c *gin.Context) { } func (h *APIHandlers) geminiStreamGenerateContent(c *gin.Context, rawJson []byte) { + alt := h.getAlt(c) + + if alt == "" { + c.Header("Content-Type", "text/event-stream") + c.Header("Cache-Control", "no-cache") + c.Header("Connection", "keep-alive") + c.Header("Access-Control-Allow-Origin", "*") + } + // Get the http.Flusher interface to manually flush the response. flusher, ok := c.Writer.(http.Flusher) if !ok { @@ -122,8 +131,6 @@ func (h *APIHandlers) geminiStreamGenerateContent(c *gin.Context, rawJson []byte modelResult := gjson.GetBytes(rawJson, "model") modelName := modelResult.String() - alt := h.getAlt(c) - cliCtx, cliCancel := context.WithCancel(context.Background()) var cliClient *client.Client defer func() {