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.
This commit is contained in:
Luis Pater
2025-07-26 17:16:55 +08:00
parent e64fa48823
commit 3b2ab0d7bd
2 changed files with 18 additions and 2 deletions

View File

@@ -99,6 +99,15 @@ func (h *APIHandlers) CLIHandler(c *gin.Context) {
} }
func (h *APIHandlers) internalStreamGenerateContent(c *gin.Context, rawJson []byte) { 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. // Get the http.Flusher interface to manually flush the response.
flusher, ok := c.Writer.(http.Flusher) flusher, ok := c.Writer.(http.Flusher)
if !ok { if !ok {

View File

@@ -107,6 +107,15 @@ func (h *APIHandlers) GeminiHandler(c *gin.Context) {
} }
func (h *APIHandlers) geminiStreamGenerateContent(c *gin.Context, rawJson []byte) { 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. // Get the http.Flusher interface to manually flush the response.
flusher, ok := c.Writer.(http.Flusher) flusher, ok := c.Writer.(http.Flusher)
if !ok { if !ok {
@@ -122,8 +131,6 @@ func (h *APIHandlers) geminiStreamGenerateContent(c *gin.Context, rawJson []byte
modelResult := gjson.GetBytes(rawJson, "model") modelResult := gjson.GetBytes(rawJson, "model")
modelName := modelResult.String() modelName := modelResult.String()
alt := h.getAlt(c)
cliCtx, cliCancel := context.WithCancel(context.Background()) cliCtx, cliCancel := context.WithCancel(context.Background())
var cliClient *client.Client var cliClient *client.Client
defer func() { defer func() {