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:
+3
-11
@@ -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")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user