feat: enable SSE streaming support in Anthropic handler

- Updated AnthropicHandler to check for streaming configuration before handling SSE transform.
- Modified handleStreaming to use the original request body instead of forcing stream to true.
- Adjusted the transformation logic in AnthropicToBifrost to respect the original stream setting.
- Added logging for SSE streaming configuration in the main application.
This commit is contained in:
Beyhan Ogur
2026-05-11 15:28:56 +03:00
parent f0a4e89e0f
commit c56ae7194c
10 changed files with 193 additions and 21 deletions
+3 -11
View File
@@ -140,8 +140,8 @@ func AnthropicHandler(cfg *config.Config, db *gorm.DB) fiber.Handler {
targetURL := strings.TrimRight(cfg.OpenAIBackend, "/") + "/v1/chat/completions"
logger.Debug("│ 🚀 Forwarding to: %s", targetURL)
// Streaming path: if original request had stream=true, handle SSE transform
if isStreamRequest(bodyBytes) {
// Streaming path: if enabled and original request had stream=true, handle SSE transform
if cfg.Streaming && isStreamRequest(bodyBytes) {
return handleStreaming(c, targetURL, reqHeaders, converted, cfg, db, bodyBytes, start)
}
@@ -269,19 +269,11 @@ func handleStreaming(
originalBody []byte,
start time.Time,
) error {
// Enable streaming in the request body
var reqMap map[string]interface{}
if err := json.Unmarshal(body, &reqMap); err != nil {
return c.Status(fiber.StatusInternalServerError).SendString("invalid body")
}
reqMap["stream"] = true
streamBody, _ := json.Marshal(reqMap)
// Direct HTTP request to upstream (bypass proxy.Forward for streaming)
ctx, cancel := context.WithTimeout(c.Context(), time.Duration(cfg.RequestTimeoutSeconds)*time.Second)
defer cancel()
req, err := http.NewRequestWithContext(ctx, "POST", targetURL, bytes.NewReader(streamBody))
req, err := http.NewRequestWithContext(ctx, "POST", targetURL, bytes.NewReader(body))
if err != nil {
return c.Status(fiber.StatusInternalServerError).SendString("build request failed")
}