Skip to content

Commit b334f41

Browse files
feat: responses api option added in openai provider
1 parent f3cbeda commit b334f41

File tree

124 files changed

+9175
-5639
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

124 files changed

+9175
-5639
lines changed

core/bifrost.go

Lines changed: 226 additions & 80 deletions
Large diffs are not rendered by default.

core/mcp.go

Lines changed: 72 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,12 @@ type MCPManager struct {
5656

5757
// MCPClient represents a connected MCP client with its configuration and tools.
5858
type MCPClient struct {
59-
Name string // Unique name for this client
60-
Conn *client.Client // Active MCP client connection
61-
ExecutionConfig schemas.MCPClientConfig // Tool filtering settings
62-
ToolMap map[string]schemas.Tool // Available tools mapped by name
63-
ConnectionInfo MCPClientConnectionInfo `json:"connection_info"` // Connection metadata for management
64-
cancelFunc context.CancelFunc `json:"-"` // Cancel function for SSE connections (not serialized)
59+
Name string // Unique name for this client
60+
Conn *client.Client // Active MCP client connection
61+
ExecutionConfig schemas.MCPClientConfig // Tool filtering settings
62+
ToolMap map[string]schemas.ChatTool // Available tools mapped by name
63+
ConnectionInfo MCPClientConnectionInfo `json:"connection_info"` // Connection metadata for management
64+
cancelFunc context.CancelFunc `json:"-"` // Cancel function for SSE connections (not serialized)
6565
}
6666

6767
// MCPClientConnectionInfo stores metadata about how a client is connected.
@@ -173,7 +173,7 @@ func (m *MCPManager) AddClient(config schemas.MCPClientConfig) error {
173173
m.clientMap[config.Name] = &MCPClient{
174174
Name: config.Name,
175175
ExecutionConfig: config,
176-
ToolMap: make(map[string]schemas.Tool),
176+
ToolMap: make(map[string]schemas.ChatTool),
177177
}
178178

179179
// Temporarily unlock for the connection attempt
@@ -228,7 +228,7 @@ func (m *MCPManager) removeClientUnsafe(name string) error {
228228
}
229229

230230
// Clear client tool map
231-
client.ToolMap = make(map[string]schemas.Tool)
231+
client.ToolMap = make(map[string]schemas.ChatTool)
232232

233233
delete(m.clientMap, name)
234234
return nil
@@ -256,7 +256,7 @@ func (m *MCPManager) EditClientTools(name string, toolsToAdd []string, toolsToRe
256256
client.ExecutionConfig = config
257257

258258
// Clear current tool map
259-
client.ToolMap = make(map[string]schemas.Tool)
259+
client.ToolMap = make(map[string]schemas.ChatTool)
260260

261261
// Temporarily unlock for the network call
262262
m.mu.Unlock()
@@ -288,7 +288,7 @@ func (m *MCPManager) EditClientTools(name string, toolsToAdd []string, toolsToRe
288288

289289
// getAvailableTools returns all tools from connected MCP clients.
290290
// Applies client filtering if specified in the context.
291-
func (m *MCPManager) getAvailableTools(ctx context.Context) []schemas.Tool {
291+
func (m *MCPManager) getAvailableTools(ctx context.Context) []schemas.ChatTool {
292292
m.mu.RLock()
293293
defer m.mu.RUnlock()
294294

@@ -303,7 +303,7 @@ func (m *MCPManager) getAvailableTools(ctx context.Context) []schemas.Tool {
303303
excludeClients = existingExcludeClients
304304
}
305305

306-
tools := make([]schemas.Tool, 0)
306+
tools := make([]schemas.ChatTool, 0)
307307
for clientName, client := range m.clientMap {
308308
// Apply client filtering logic
309309
if !m.shouldIncludeClient(clientName, includeClients, excludeClients) {
@@ -348,7 +348,7 @@ func (m *MCPManager) getAvailableTools(ctx context.Context) []schemas.Tool {
348348
// func(args EchoArgs) (string, error) {
349349
// return args.Message, nil
350350
// }, toolSchema)
351-
func (m *MCPManager) registerTool(name, description string, handler MCPToolHandler[any], toolSchema schemas.Tool) error {
351+
func (m *MCPManager) registerTool(name, description string, handler MCPToolHandler[any], toolSchema schemas.ChatTool) error {
352352
// Ensure local server is set up
353353
if err := m.setupLocalHost(); err != nil {
354354
return fmt.Errorf("failed to setup local host: %w", err)
@@ -453,7 +453,7 @@ func (m *MCPManager) createLocalMCPClient() (*MCPClient, error) {
453453
ExecutionConfig: schemas.MCPClientConfig{
454454
Name: BifrostMCPClientName,
455455
},
456-
ToolMap: make(map[string]schemas.Tool),
456+
ToolMap: make(map[string]schemas.ChatTool),
457457
ConnectionInfo: MCPClientConnectionInfo{
458458
Type: schemas.MCPConnectionTypeInProcess, // Accurate: in-process (in-memory) transport
459459
},
@@ -524,9 +524,9 @@ func (m *MCPManager) startLocalMCPServer() error {
524524
// - toolCall: The tool call to execute (from assistant message)
525525
//
526526
// Returns:
527-
// - schemas.BifrostMessage: Tool message with execution result
527+
// - schemas.ChatMessage: Tool message with execution result
528528
// - error: Any execution error
529-
func (m *MCPManager) executeTool(ctx context.Context, toolCall schemas.ToolCall) (*schemas.BifrostMessage, error) {
529+
func (m *MCPManager) executeTool(ctx context.Context, toolCall schemas.ChatAssistantMessageToolCall) (*schemas.ChatMessage, error) {
530530
if toolCall.Function.Name == nil {
531531
return nil, fmt.Errorf("tool call missing function name")
532532
}
@@ -600,7 +600,7 @@ func (m *MCPManager) connectToMCPClient(config schemas.MCPClientConfig) error {
600600
m.clientMap[config.Name] = &MCPClient{
601601
Name: config.Name,
602602
ExecutionConfig: config,
603-
ToolMap: make(map[string]schemas.Tool),
603+
ToolMap: make(map[string]schemas.ChatTool),
604604
ConnectionInfo: MCPClientConnectionInfo{
605605
Type: config.ConnectionType,
606606
},
@@ -679,7 +679,7 @@ func (m *MCPManager) connectToMCPClient(config schemas.MCPClientConfig) error {
679679
if err != nil {
680680
m.logger.Warn(fmt.Sprintf("%s Failed to retrieve tools from %s: %v", MCPLogPrefix, config.Name, err))
681681
// Continue with connection even if tool retrieval fails
682-
tools = make(map[string]schemas.Tool)
682+
tools = make(map[string]schemas.ChatTool)
683683
}
684684

685685
// Second lock: Update client with final connection details and tools
@@ -711,7 +711,7 @@ func (m *MCPManager) connectToMCPClient(config schemas.MCPClientConfig) error {
711711
}
712712

713713
// retrieveExternalTools retrieves and filters tools from an external MCP server without holding locks.
714-
func (m *MCPManager) retrieveExternalTools(ctx context.Context, client *client.Client, config schemas.MCPClientConfig) (map[string]schemas.Tool, error) {
714+
func (m *MCPManager) retrieveExternalTools(ctx context.Context, client *client.Client, config schemas.MCPClientConfig) (map[string]schemas.ChatTool, error) {
715715
// Get available tools from external server
716716
listRequest := mcp.ListToolsRequest{
717717
PaginatedRequest: mcp.PaginatedRequest{
@@ -727,10 +727,10 @@ func (m *MCPManager) retrieveExternalTools(ctx context.Context, client *client.C
727727
}
728728

729729
if toolsResponse == nil {
730-
return make(map[string]schemas.Tool), nil // No tools available
730+
return make(map[string]schemas.ChatTool), nil // No tools available
731731
}
732732

733-
tools := make(map[string]schemas.Tool)
733+
tools := make(map[string]schemas.ChatTool)
734734

735735
// toolsResponse is already a ListToolsResult
736736
for _, mcpTool := range toolsResponse.Tools {
@@ -796,13 +796,13 @@ func (m *MCPManager) shouldSkipToolForRequest(toolName string, ctx context.Conte
796796
}
797797

798798
// convertMCPToolToBifrostSchema converts an MCP tool definition to Bifrost format.
799-
func (m *MCPManager) convertMCPToolToBifrostSchema(mcpTool *mcp.Tool) schemas.Tool {
800-
return schemas.Tool{
801-
Type: "function",
802-
Function: schemas.Function{
799+
func (m *MCPManager) convertMCPToolToBifrostSchema(mcpTool *mcp.Tool) schemas.ChatTool {
800+
return schemas.ChatTool{
801+
Type: schemas.ChatToolTypeFunction,
802+
Function: &schemas.ChatToolFunction{
803803
Name: mcpTool.Name,
804-
Description: mcpTool.Description,
805-
Parameters: schemas.FunctionParameters{
804+
Description: Ptr(mcpTool.Description),
805+
Parameters: &schemas.ToolFunctionParameters{
806806
Type: mcpTool.InputSchema.Type,
807807
Properties: mcpTool.InputSchema.Properties,
808808
Required: mcpTool.InputSchema.Required,
@@ -852,13 +852,13 @@ func (m *MCPManager) extractTextFromMCPResponse(toolResponse *mcp.CallToolResult
852852
}
853853

854854
// createToolResponseMessage creates a tool response message with the execution result.
855-
func (m *MCPManager) createToolResponseMessage(toolCall schemas.ToolCall, responseText string) *schemas.BifrostMessage {
856-
return &schemas.BifrostMessage{
857-
Role: schemas.ModelChatMessageRoleTool,
858-
Content: schemas.MessageContent{
855+
func (m *MCPManager) createToolResponseMessage(toolCall schemas.ChatAssistantMessageToolCall, responseText string) *schemas.ChatMessage {
856+
return &schemas.ChatMessage{
857+
Role: schemas.ChatMessageRoleTool,
858+
Content: schemas.ChatMessageContent{
859859
ContentStr: &responseText,
860860
},
861-
ToolMessage: &schemas.ToolMessage{
861+
ChatToolMessage: &schemas.ChatToolMessage{
862862
ToolCallID: toolCall.ID,
863863
},
864864
}
@@ -867,31 +867,51 @@ func (m *MCPManager) createToolResponseMessage(toolCall schemas.ToolCall, respon
867867
func (m *MCPManager) addMCPToolsToBifrostRequest(ctx context.Context, req *schemas.BifrostRequest) *schemas.BifrostRequest {
868868
mcpTools := m.getAvailableTools(ctx)
869869
if len(mcpTools) > 0 {
870-
// Initialize tools array if needed
871-
if req.Params == nil {
872-
req.Params = &schemas.ModelParameters{}
873-
}
874-
if req.Params.Tools == nil {
875-
req.Params.Tools = &[]schemas.Tool{}
876-
}
877-
tools := *req.Params.Tools
870+
switch req.RequestType {
871+
case schemas.ChatCompletionRequest, schemas.ChatCompletionStreamRequest:
872+
req.ChatRequest.Params = &schemas.ChatParameters{}
878873

879-
// Create a map of existing tool names for O(1) lookup
880-
existingToolsMap := make(map[string]bool)
881-
for _, tool := range tools {
882-
existingToolsMap[tool.Function.Name] = true
883-
}
874+
tools := req.ChatRequest.Params.Tools
884875

885-
// Add MCP tools that are not already present
886-
for _, mcpTool := range mcpTools {
887-
if !existingToolsMap[mcpTool.Function.Name] {
888-
tools = append(tools, mcpTool)
889-
// Update the map to prevent duplicates within MCP tools as well
890-
existingToolsMap[mcpTool.Function.Name] = true
876+
// Create a map of existing tool names for O(1) lookup
877+
existingToolsMap := make(map[string]bool)
878+
for _, tool := range tools {
879+
existingToolsMap[tool.Function.Name] = true
891880
}
892-
}
893-
req.Params.Tools = &tools
894881

882+
// Add MCP tools that are not already present
883+
for _, mcpTool := range mcpTools {
884+
if !existingToolsMap[mcpTool.Function.Name] {
885+
tools = append(tools, mcpTool)
886+
// Update the map to prevent duplicates within MCP tools as well
887+
existingToolsMap[mcpTool.Function.Name] = true
888+
}
889+
}
890+
req.ChatRequest.Params.Tools = tools
891+
case schemas.ResponsesRequest, schemas.ResponsesStreamRequest:
892+
req.ResponsesRequest.Params = &schemas.ResponsesParameters{}
893+
894+
tools := req.ResponsesRequest.Params.Tools
895+
896+
// Create a map of existing tool names for O(1) lookup
897+
existingToolsMap := make(map[string]bool)
898+
for _, tool := range tools {
899+
if tool.Name != nil {
900+
existingToolsMap[*tool.Name] = true
901+
}
902+
}
903+
904+
// Add MCP tools that are not already present
905+
for _, mcpTool := range mcpTools {
906+
if !existingToolsMap[mcpTool.Function.Name] {
907+
responsesTool := mcpTool.ToResponsesTool()
908+
tools = append(tools, *responsesTool)
909+
// Update the map to prevent duplicates within MCP tools as well
910+
existingToolsMap[*responsesTool.Name] = true
911+
}
912+
}
913+
req.ResponsesRequest.Params.Tools = tools
914+
}
895915
}
896916
return req
897917
}

0 commit comments

Comments
 (0)