-
Notifications
You must be signed in to change notification settings - Fork 574
feat: add provider management API with high-performance config store #141
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
akshaydeo
merged 1 commit into
main
from
07-03-feat_hot_reload_for_provider_config_added_and_handers_restructured
Jul 10, 2025
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,3 +5,4 @@ | |
| **/venv/ | ||
| **/__pycache__/** | ||
| private.* | ||
| .venv | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,138 @@ | ||
| // Package handlers provides HTTP request handlers for the Bifrost HTTP transport. | ||
| // This file contains completion request handlers for text and chat completions. | ||
| package handlers | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "fmt" | ||
|
|
||
| "github.com/fasthttp/router" | ||
| bifrost "github.com/maximhq/bifrost/core" | ||
| "github.com/maximhq/bifrost/core/schemas" | ||
| "github.com/maximhq/bifrost/transports/bifrost-http/lib" | ||
| "github.com/valyala/fasthttp" | ||
| ) | ||
|
|
||
| // CompletionHandler manages HTTP requests for completion operations | ||
| type CompletionHandler struct { | ||
| client *bifrost.Bifrost | ||
| logger schemas.Logger | ||
| } | ||
|
|
||
| // NewCompletionHandler creates a new completion handler instance | ||
| func NewCompletionHandler(client *bifrost.Bifrost, logger schemas.Logger) *CompletionHandler { | ||
| return &CompletionHandler{ | ||
| client: client, | ||
| logger: logger, | ||
| } | ||
| } | ||
|
|
||
| // CompletionRequest represents a request for either text or chat completion | ||
| type CompletionRequest struct { | ||
| Provider schemas.ModelProvider `json:"provider"` // The AI model provider to use | ||
| Messages []schemas.BifrostMessage `json:"messages"` // Chat messages (for chat completion) | ||
| Text string `json:"text"` // Text input (for text completion) | ||
| Model string `json:"model"` // Model to use | ||
| Params *schemas.ModelParameters `json:"params"` // Additional model parameters | ||
| Fallbacks []schemas.Fallback `json:"fallbacks"` // Fallback providers and models | ||
| } | ||
|
|
||
| type CompletionType string | ||
|
|
||
| const ( | ||
| CompletionTypeText CompletionType = "text" | ||
| CompletionTypeChat CompletionType = "chat" | ||
| ) | ||
|
|
||
| // RegisterRoutes registers all completion-related routes | ||
| func (h *CompletionHandler) RegisterRoutes(r *router.Router) { | ||
| // Completion endpoints | ||
| r.POST("/v1/text/completions", h.TextCompletion) | ||
| r.POST("/v1/chat/completions", h.ChatCompletion) | ||
| } | ||
|
|
||
| // TextCompletion handles POST /v1/text/completions - Process text completion requests | ||
| func (h *CompletionHandler) TextCompletion(ctx *fasthttp.RequestCtx) { | ||
| h.handleCompletion(ctx, CompletionTypeText) | ||
| } | ||
|
|
||
| // ChatCompletion handles POST /v1/chat/completions - Process chat completion requests | ||
| func (h *CompletionHandler) ChatCompletion(ctx *fasthttp.RequestCtx) { | ||
| h.handleCompletion(ctx, CompletionTypeChat) | ||
| } | ||
|
|
||
| // handleCompletion processes both text and chat completion requests | ||
| // It handles request parsing, validation, and response formatting | ||
| func (h *CompletionHandler) handleCompletion(ctx *fasthttp.RequestCtx, completionType CompletionType) { | ||
| var req CompletionRequest | ||
| if err := json.Unmarshal(ctx.PostBody(), &req); err != nil { | ||
| SendError(ctx, fasthttp.StatusBadRequest, fmt.Sprintf("Invalid request format: %v", err), h.logger) | ||
| return | ||
| } | ||
|
|
||
| // Validate required fields | ||
| if req.Provider == "" { | ||
| SendError(ctx, fasthttp.StatusBadRequest, "Provider is required", h.logger) | ||
| return | ||
| } | ||
|
|
||
| if req.Model == "" { | ||
| SendError(ctx, fasthttp.StatusBadRequest, "Model is required", h.logger) | ||
| return | ||
| } | ||
|
|
||
| // Create BifrostRequest | ||
| bifrostReq := &schemas.BifrostRequest{ | ||
| Provider: req.Provider, | ||
| Model: req.Model, | ||
| Params: req.Params, | ||
| Fallbacks: req.Fallbacks, | ||
| } | ||
|
|
||
| // Validate and set input based on completion type | ||
| switch completionType { | ||
| case CompletionTypeText: | ||
| if req.Text == "" { | ||
| SendError(ctx, fasthttp.StatusBadRequest, "Text is required for text completion", h.logger) | ||
| return | ||
| } | ||
| bifrostReq.Input = schemas.RequestInput{ | ||
| TextCompletionInput: &req.Text, | ||
| } | ||
| case CompletionTypeChat: | ||
| if len(req.Messages) == 0 { | ||
| SendError(ctx, fasthttp.StatusBadRequest, "Messages array is required for chat completion", h.logger) | ||
| return | ||
| } | ||
| bifrostReq.Input = schemas.RequestInput{ | ||
| ChatCompletionInput: &req.Messages, | ||
| } | ||
| } | ||
|
|
||
| // Convert context | ||
| bifrostCtx := lib.ConvertToBifrostContext(ctx) | ||
| if bifrostCtx == nil { | ||
| SendError(ctx, fasthttp.StatusInternalServerError, "Failed to convert context", h.logger) | ||
| return | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| // Execute request | ||
| var resp *schemas.BifrostResponse | ||
| var bifrostErr *schemas.BifrostError | ||
|
|
||
| switch completionType { | ||
| case CompletionTypeText: | ||
| resp, bifrostErr = h.client.TextCompletionRequest(*bifrostCtx, bifrostReq) | ||
| case CompletionTypeChat: | ||
| resp, bifrostErr = h.client.ChatCompletionRequest(*bifrostCtx, bifrostReq) | ||
| } | ||
|
|
||
| // Handle response | ||
| if bifrostErr != nil { | ||
| SendBifrostError(ctx, bifrostErr, h.logger) | ||
| return | ||
| } | ||
|
|
||
| // Send successful response | ||
| SendJSON(ctx, resp, h.logger) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| // Package handlers provides HTTP request handlers for the Bifrost HTTP transport. | ||
| // This file contains integration management handlers for AI provider integrations. | ||
| package handlers | ||
|
|
||
| import ( | ||
| "github.com/fasthttp/router" | ||
| bifrost "github.com/maximhq/bifrost/core" | ||
| "github.com/maximhq/bifrost/transports/bifrost-http/integrations" | ||
| "github.com/maximhq/bifrost/transports/bifrost-http/integrations/anthropic" | ||
| "github.com/maximhq/bifrost/transports/bifrost-http/integrations/genai" | ||
| "github.com/maximhq/bifrost/transports/bifrost-http/integrations/litellm" | ||
| "github.com/maximhq/bifrost/transports/bifrost-http/integrations/openai" | ||
| ) | ||
|
|
||
| // IntegrationHandler manages HTTP requests for AI provider integrations | ||
| type IntegrationHandler struct { | ||
| extensions []integrations.ExtensionRouter | ||
| } | ||
|
|
||
| // NewIntegrationHandler creates a new integration handler instance | ||
| func NewIntegrationHandler(client *bifrost.Bifrost) *IntegrationHandler { | ||
| // Initialize all available integration routers | ||
| extensions := []integrations.ExtensionRouter{ | ||
| genai.NewGenAIRouter(client), | ||
| openai.NewOpenAIRouter(client), | ||
| anthropic.NewAnthropicRouter(client), | ||
| litellm.NewLiteLLMRouter(client), | ||
| } | ||
|
|
||
| return &IntegrationHandler{ | ||
| extensions: extensions, | ||
| } | ||
| } | ||
|
|
||
| // RegisterRoutes registers all integration routes for AI provider compatibility endpoints | ||
| func (h *IntegrationHandler) RegisterRoutes(r *router.Router) { | ||
| // Register routes for each integration extension | ||
| for _, extension := range h.extensions { | ||
| extension.RegisterRoutes(r) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| // Package handlers provides HTTP request handlers for the Bifrost HTTP transport. | ||
| // This file contains MCP (Model Context Protocol) tool execution handlers. | ||
| package handlers | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "fmt" | ||
|
|
||
| "github.com/fasthttp/router" | ||
| bifrost "github.com/maximhq/bifrost/core" | ||
| "github.com/maximhq/bifrost/core/schemas" | ||
| "github.com/maximhq/bifrost/transports/bifrost-http/lib" | ||
| "github.com/valyala/fasthttp" | ||
| ) | ||
|
|
||
| // MCPHandler manages HTTP requests for MCP tool operations | ||
| type MCPHandler struct { | ||
| client *bifrost.Bifrost | ||
| logger schemas.Logger | ||
| } | ||
|
|
||
| // NewMCPHandler creates a new MCP handler instance | ||
| func NewMCPHandler(client *bifrost.Bifrost, logger schemas.Logger) *MCPHandler { | ||
| return &MCPHandler{ | ||
| client: client, | ||
| logger: logger, | ||
| } | ||
| } | ||
|
|
||
| // RegisterRoutes registers all MCP-related routes | ||
| func (h *MCPHandler) RegisterRoutes(r *router.Router) { | ||
| // MCP tool execution endpoint | ||
| r.POST("/v1/mcp/tool/execute", h.ExecuteTool) | ||
| } | ||
|
|
||
| // ExecuteTool handles POST /v1/mcp/tool/execute - Execute MCP tool | ||
| func (h *MCPHandler) ExecuteTool(ctx *fasthttp.RequestCtx) { | ||
| var req schemas.ToolCall | ||
| if err := json.Unmarshal(ctx.PostBody(), &req); err != nil { | ||
| SendError(ctx, fasthttp.StatusBadRequest, fmt.Sprintf("Invalid request format: %v", err), h.logger) | ||
| return | ||
| } | ||
|
|
||
| // Validate required fields | ||
| if req.Function.Name == nil || *req.Function.Name == "" { | ||
| SendError(ctx, fasthttp.StatusBadRequest, "Tool function name is required", h.logger) | ||
| return | ||
| } | ||
|
|
||
| // Convert context | ||
| bifrostCtx := lib.ConvertToBifrostContext(ctx) | ||
| if bifrostCtx == nil { | ||
| SendError(ctx, fasthttp.StatusInternalServerError, "Failed to convert context", h.logger) | ||
| return | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| // Execute MCP tool | ||
| resp, bifrostErr := h.client.ExecuteMCPTool(*bifrostCtx, req) | ||
| if bifrostErr != nil { | ||
| SendBifrostError(ctx, bifrostErr, h.logger) | ||
| return | ||
| } | ||
|
|
||
| // Send successful response | ||
| ctx.SetStatusCode(fasthttp.StatusOK) | ||
| ctx.SetContentType("application/json") | ||
| if encodeErr := json.NewEncoder(ctx).Encode(resp); encodeErr != nil { | ||
| h.logger.Warn(fmt.Sprintf("Failed to encode response: %v", encodeErr)) | ||
| SendError(ctx, fasthttp.StatusInternalServerError, fmt.Sprintf("Failed to encode response: %v", encodeErr), h.logger) | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.