From 190881f13a19aad360c32c44284c9205c81c5949 Mon Sep 17 00:00:00 2001 From: CJACK Date: Mon, 16 Feb 2026 19:11:55 +0800 Subject: [PATCH] refactor: introduce a public `app` package to expose the `internal/server` router, resolving `internal` package import restrictions. --- internal/adapter/claude/handler.go | 11 ++++++----- internal/adapter/openai/handler.go | 21 +++++++++++++-------- 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/internal/adapter/claude/handler.go b/internal/adapter/claude/handler.go index 391fd83..83a82ac 100644 --- a/internal/adapter/claude/handler.go +++ b/internal/adapter/claude/handler.go @@ -232,17 +232,18 @@ func (h *Handler) writeClaudeStream(w http.ResponseWriter, r *http.Request, mode w.Header().Set("Content-Type", "text/event-stream") w.Header().Set("Cache-Control", "no-cache") w.Header().Set("Connection", "keep-alive") - flusher, ok := w.(http.Flusher) - if !ok { - writeJSON(w, http.StatusInternalServerError, map[string]any{"error": map[string]any{"type": "api_error", "message": "streaming unsupported"}}) - return + flusher, hasFlusher := w.(http.Flusher) + if !hasFlusher { + config.Logger.Warn("[claude_stream] response writer does not support flush; falling back to buffered SSE") } send := func(v any) { b, _ := json.Marshal(v) _, _ = w.Write([]byte("data: ")) _, _ = w.Write(b) _, _ = w.Write([]byte("\n\n")) - flusher.Flush() + if hasFlusher { + flusher.Flush() + } } messageID := fmt.Sprintf("msg_%d", time.Now().UnixNano()) inputTokens := util.EstimateTokens(fmt.Sprintf("%v", messages)) diff --git a/internal/adapter/openai/handler.go b/internal/adapter/openai/handler.go index 06af18e..23d17d5 100644 --- a/internal/adapter/openai/handler.go +++ b/internal/adapter/openai/handler.go @@ -193,10 +193,9 @@ func (h *Handler) handleStream(w http.ResponseWriter, r *http.Request, resp *htt w.Header().Set("Content-Type", "text/event-stream") w.Header().Set("Cache-Control", "no-cache") w.Header().Set("Connection", "keep-alive") - flusher, ok := w.(http.Flusher) - if !ok { - writeOpenAIError(w, http.StatusInternalServerError, "streaming unsupported") - return + flusher, hasFlusher := w.(http.Flusher) + if !hasFlusher { + config.Logger.Warn("[stream] response writer does not support flush; falling back to buffered SSE") } lines := make(chan []byte, 128) @@ -233,11 +232,15 @@ func (h *Handler) handleStream(w http.ResponseWriter, r *http.Request, resp *htt _, _ = w.Write([]byte("data: ")) _, _ = w.Write(b) _, _ = w.Write([]byte("\n\n")) - flusher.Flush() + if hasFlusher { + flusher.Flush() + } } sendDone := func() { _, _ = w.Write([]byte("data: [DONE]\n\n")) - flusher.Flush() + if hasFlusher { + flusher.Flush() + } } finalize := func(finishReason string) { @@ -313,8 +316,10 @@ func (h *Handler) handleStream(w http.ResponseWriter, r *http.Request, resp *htt finalize("stop") return } - _, _ = w.Write([]byte(": keep-alive\n\n")) - flusher.Flush() + if hasFlusher { + _, _ = w.Write([]byte(": keep-alive\n\n")) + flusher.Flush() + } case line, ok := <-lines: if !ok { // Ensure scanner completion is observed only after all queued