Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
478 changes: 478 additions & 0 deletions docs/integrations.md

Large diffs are not rendered by default.

69 changes: 69 additions & 0 deletions transports/bifrost-http/integrations/anthropic/router.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package anthropic

import (
"encoding/json"

"github.com/fasthttp/router"
bifrost "github.com/maximhq/bifrost/core"
"github.com/maximhq/bifrost/transports/bifrost-http/lib"
"github.com/valyala/fasthttp"
)

// AnthropicRouter holds route registrations for Anthropic endpoints.
type AnthropicRouter struct {
client *bifrost.Bifrost
}

// NewAnthropicRouter creates a new AnthropicRouter with the given bifrost client.
func NewAnthropicRouter(client *bifrost.Bifrost) *AnthropicRouter {
return &AnthropicRouter{client: client}
}

// RegisterRoutes registers all Anthropic routes on the given router.
func (a *AnthropicRouter) RegisterRoutes(r *router.Router) {
r.POST("/anthropic/v1/messages", a.handleMessages)
}

// handleMessages handles POST /v1/messages
func (a *AnthropicRouter) handleMessages(ctx *fasthttp.RequestCtx) {
var req AnthropicMessageRequest
if err := json.Unmarshal(ctx.PostBody(), &req); err != nil {
ctx.SetStatusCode(fasthttp.StatusBadRequest)
ctx.SetContentType("application/json")
errResponse := map[string]string{"error": err.Error()}
jsonBytes, _ := json.Marshal(errResponse)
ctx.SetBody(jsonBytes)
return
}

if req.Model == "" {
ctx.SetStatusCode(fasthttp.StatusBadRequest)
ctx.SetBodyString("Model parameter is required")
return
}

if req.MaxTokens == 0 {
ctx.SetStatusCode(fasthttp.StatusBadRequest)
ctx.SetBodyString("max_tokens parameter is required")
return
}

bifrostReq := req.ConvertToBifrostRequest()

bifrostCtx := lib.ConvertToBifrostContext(ctx)

result, err := a.client.ChatCompletionRequest(*bifrostCtx, bifrostReq)
if err != nil {
ctx.SetStatusCode(fasthttp.StatusInternalServerError)
ctx.SetContentType("application/json")
jsonBytes, _ := json.Marshal(err)
ctx.SetBody(jsonBytes)
return
}
Comment on lines +55 to +62
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick (assertive)

Consider adopting sophisticated error handling for consistency.

The current error handling returns 500 for all Bifrost client errors. For consistency with the Mistral router in this PR, consider implementing the more sophisticated error handling pattern that distinguishes between client and server errors.

Adopt the error handling pattern from transports/bifrost-http/integrations/mistral/router.go lines 51-60:

-	if err != nil {
-		ctx.SetStatusCode(fasthttp.StatusInternalServerError)
-		ctx.SetContentType("application/json")
-		jsonBytes, _ := json.Marshal(err)
-		ctx.SetBody(jsonBytes)
-		return
-	}
+	if err != nil {
+		// Determine appropriate HTTP status code based on error details
+		statusCode := fasthttp.StatusInternalServerError // Default to 500
+
+		// If the error has a specific status code from the provider, use it
+		if err.StatusCode != nil {
+			statusCode = *err.StatusCode
+		} else if !err.IsBifrostError {
+			// If it's not a Bifrost internal error, treat as client error
+			statusCode = fasthttp.StatusBadRequest
+		}
+
+		ctx.SetStatusCode(statusCode)
+		ctx.SetContentType("application/json")
+		jsonBytes, _ := json.Marshal(err)
+		ctx.SetBody(jsonBytes)
+		return
+	}
🤖 Prompt for AI Agents
In transports/bifrost-http/integrations/anthropic/router.go around lines 55 to
62, the error handling treats all client errors as internal server errors with a
500 status code. To fix this, refactor the error handling to distinguish between
client and server errors by checking the error type or status, similar to the
pattern used in transports/bifrost-http/integrations/mistral/router.go lines
51-60. Adjust the response status code and message accordingly to provide more
accurate error feedback.


anthropicResponse := DeriveAnthropicFromBifrostResponse(result)
ctx.SetStatusCode(fasthttp.StatusOK)
ctx.SetContentType("application/json")
jsonBytes, _ := json.Marshal(anthropicResponse)
ctx.SetBody(jsonBytes)
}
Loading