Skip to content
Merged
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
155 changes: 154 additions & 1 deletion core/bifrost.go
Original file line number Diff line number Diff line change
Expand Up @@ -2313,6 +2313,123 @@ func (bifrost *Bifrost) FileContentRequest(ctx *schemas.BifrostContext, req *sch
return response.FileContentResponse, nil
}

// CachedContentCreateRequest creates a new cached content (Gemini / Vertex AI named cache lifecycle).
func (bifrost *Bifrost) CachedContentCreateRequest(ctx *schemas.BifrostContext, req *schemas.BifrostCachedContentCreateRequest) (*schemas.BifrostCachedContentCreateResponse, *schemas.BifrostError) {
if req == nil {
return nil, &schemas.BifrostError{IsBifrostError: false, Error: &schemas.ErrorField{Message: "cached content create request is nil"}}
}
if req.Provider == "" {
return nil, &schemas.BifrostError{IsBifrostError: false, Error: &schemas.ErrorField{Message: "provider is required for cached content create request"}}
}
if req.Model == "" {
return nil, &schemas.BifrostError{IsBifrostError: false, Error: &schemas.ErrorField{Message: "model is required for cached content create request"}}
}
if ctx == nil {
ctx = bifrost.ctx
}
bifrostReq := bifrost.getBifrostRequest()
bifrostReq.RequestType = schemas.CachedContentCreateRequest
bifrostReq.CachedContentCreateRequest = req
response, err := bifrost.handleRequest(ctx, bifrostReq)
if err != nil {
return nil, err
}
return response.CachedContentCreateResponse, nil
}

// CachedContentListRequest lists cached contents.
func (bifrost *Bifrost) CachedContentListRequest(ctx *schemas.BifrostContext, req *schemas.BifrostCachedContentListRequest) (*schemas.BifrostCachedContentListResponse, *schemas.BifrostError) {
if req == nil {
return nil, &schemas.BifrostError{IsBifrostError: false, Error: &schemas.ErrorField{Message: "cached content list request is nil"}}
}
if req.Provider == "" {
return nil, &schemas.BifrostError{IsBifrostError: false, Error: &schemas.ErrorField{Message: "provider is required for cached content list request"}}
}
if ctx == nil {
ctx = bifrost.ctx
}
bifrostReq := bifrost.getBifrostRequest()
bifrostReq.RequestType = schemas.CachedContentListRequest
bifrostReq.CachedContentListRequest = req
response, err := bifrost.handleRequest(ctx, bifrostReq)
if err != nil {
return nil, err
}
return response.CachedContentListResponse, nil
}

// CachedContentRetrieveRequest retrieves a single cached content by name.
func (bifrost *Bifrost) CachedContentRetrieveRequest(ctx *schemas.BifrostContext, req *schemas.BifrostCachedContentRetrieveRequest) (*schemas.BifrostCachedContentRetrieveResponse, *schemas.BifrostError) {
if req == nil {
return nil, &schemas.BifrostError{IsBifrostError: false, Error: &schemas.ErrorField{Message: "cached content retrieve request is nil"}}
}
if req.Provider == "" {
return nil, &schemas.BifrostError{IsBifrostError: false, Error: &schemas.ErrorField{Message: "provider is required for cached content retrieve request"}}
}
if req.Name == "" {
return nil, &schemas.BifrostError{IsBifrostError: false, Error: &schemas.ErrorField{Message: "name is required for cached content retrieve request"}}
}
if ctx == nil {
ctx = bifrost.ctx
}
bifrostReq := bifrost.getBifrostRequest()
bifrostReq.RequestType = schemas.CachedContentRetrieveRequest
bifrostReq.CachedContentRetrieveRequest = req
response, err := bifrost.handleRequest(ctx, bifrostReq)
if err != nil {
return nil, err
}
return response.CachedContentRetrieveResponse, nil
}

// CachedContentUpdateRequest updates expiration on a cached content.
func (bifrost *Bifrost) CachedContentUpdateRequest(ctx *schemas.BifrostContext, req *schemas.BifrostCachedContentUpdateRequest) (*schemas.BifrostCachedContentUpdateResponse, *schemas.BifrostError) {
if req == nil {
return nil, &schemas.BifrostError{IsBifrostError: false, Error: &schemas.ErrorField{Message: "cached content update request is nil"}}
}
if req.Provider == "" {
return nil, &schemas.BifrostError{IsBifrostError: false, Error: &schemas.ErrorField{Message: "provider is required for cached content update request"}}
}
if req.Name == "" {
return nil, &schemas.BifrostError{IsBifrostError: false, Error: &schemas.ErrorField{Message: "name is required for cached content update request"}}
}
if ctx == nil {
ctx = bifrost.ctx
}
bifrostReq := bifrost.getBifrostRequest()
bifrostReq.RequestType = schemas.CachedContentUpdateRequest
bifrostReq.CachedContentUpdateRequest = req
response, err := bifrost.handleRequest(ctx, bifrostReq)
if err != nil {
return nil, err
}
return response.CachedContentUpdateResponse, nil
}

// CachedContentDeleteRequest deletes a cached content by name.
func (bifrost *Bifrost) CachedContentDeleteRequest(ctx *schemas.BifrostContext, req *schemas.BifrostCachedContentDeleteRequest) (*schemas.BifrostCachedContentDeleteResponse, *schemas.BifrostError) {
if req == nil {
return nil, &schemas.BifrostError{IsBifrostError: false, Error: &schemas.ErrorField{Message: "cached content delete request is nil"}}
}
if req.Provider == "" {
return nil, &schemas.BifrostError{IsBifrostError: false, Error: &schemas.ErrorField{Message: "provider is required for cached content delete request"}}
}
if req.Name == "" {
return nil, &schemas.BifrostError{IsBifrostError: false, Error: &schemas.ErrorField{Message: "name is required for cached content delete request"}}
}
if ctx == nil {
ctx = bifrost.ctx
}
bifrostReq := bifrost.getBifrostRequest()
bifrostReq.RequestType = schemas.CachedContentDeleteRequest
bifrostReq.CachedContentDeleteRequest = req
response, err := bifrost.handleRequest(ctx, bifrostReq)
if err != nil {
return nil, err
}
return response.CachedContentDeleteResponse, nil
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.

func (bifrost *Bifrost) Passthrough(
ctx *schemas.BifrostContext,
provider schemas.ModelProvider,
Expand Down Expand Up @@ -5608,8 +5725,9 @@ func (bifrost *Bifrost) requestWorker(provider schemas.Provider, config *schemas
isMultiKeyBatchOp := isBatchRequestType(req.RequestType) && req.RequestType != schemas.BatchCreateRequest
isMultiKeyFileOp := isFileRequestType(req.RequestType) && req.RequestType != schemas.FileUploadRequest
isMultiKeyContainerOp := isContainerRequestType(req.RequestType) && req.RequestType != schemas.ContainerCreateRequest && req.RequestType != schemas.ContainerFileCreateRequest
isMultiKeyCachedContentOp := isCachedContentRequestType(req.RequestType) && req.RequestType != schemas.CachedContentCreateRequest

if isMultiKeyBatchOp || isMultiKeyFileOp || isMultiKeyContainerOp {
if isMultiKeyBatchOp || isMultiKeyFileOp || isMultiKeyContainerOp || isMultiKeyCachedContentOp {
Comment thread
akshaydeo marked this conversation as resolved.
var modelPtr *string
if model != "" {
modelPtr = &model
Expand Down Expand Up @@ -6033,6 +6151,36 @@ func (bifrost *Bifrost) handleProviderRequest(provider schemas.Provider, config
return nil, bifrostError
}
response.FileContentResponse = fileContentResponse
case schemas.CachedContentCreateRequest:
cachedContentCreateResponse, bifrostError := provider.CachedContentCreate(req.Context, key, req.BifrostRequest.CachedContentCreateRequest)
if bifrostError != nil {
return nil, bifrostError
}
response.CachedContentCreateResponse = cachedContentCreateResponse
case schemas.CachedContentListRequest:
cachedContentListResponse, bifrostError := provider.CachedContentList(req.Context, keys, req.BifrostRequest.CachedContentListRequest)
if bifrostError != nil {
return nil, bifrostError
}
response.CachedContentListResponse = cachedContentListResponse
case schemas.CachedContentRetrieveRequest:
cachedContentRetrieveResponse, bifrostError := provider.CachedContentRetrieve(req.Context, keys, req.BifrostRequest.CachedContentRetrieveRequest)
if bifrostError != nil {
return nil, bifrostError
}
response.CachedContentRetrieveResponse = cachedContentRetrieveResponse
case schemas.CachedContentUpdateRequest:
cachedContentUpdateResponse, bifrostError := provider.CachedContentUpdate(req.Context, keys, req.BifrostRequest.CachedContentUpdateRequest)
if bifrostError != nil {
return nil, bifrostError
}
response.CachedContentUpdateResponse = cachedContentUpdateResponse
case schemas.CachedContentDeleteRequest:
cachedContentDeleteResponse, bifrostError := provider.CachedContentDelete(req.Context, keys, req.BifrostRequest.CachedContentDeleteRequest)
if bifrostError != nil {
return nil, bifrostError
}
response.CachedContentDeleteResponse = cachedContentDeleteResponse
Comment thread
coderabbitai[bot] marked this conversation as resolved.
case schemas.BatchCreateRequest:
batchCreateResponse, bifrostError := provider.BatchCreate(req.Context, key, req.BifrostRequest.BatchCreateRequest)
if bifrostError != nil {
Expand Down Expand Up @@ -6920,6 +7068,11 @@ func resetBifrostRequest(req *schemas.BifrostRequest) {
req.FileRetrieveRequest = nil
req.FileDeleteRequest = nil
req.FileContentRequest = nil
req.CachedContentCreateRequest = nil
req.CachedContentListRequest = nil
req.CachedContentRetrieveRequest = nil
req.CachedContentUpdateRequest = nil
req.CachedContentDeleteRequest = nil
req.BatchCreateRequest = nil
req.BatchListRequest = nil
req.BatchRetrieveRequest = nil
Expand Down
34 changes: 34 additions & 0 deletions core/providers/anthropic/cachedcontents.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package anthropic

import (
providerUtils "github.com/maximhq/bifrost/core/providers/utils"
"github.com/maximhq/bifrost/core/schemas"
)

// CachedContentCreate is unsupported on AnthropicProvider. Only Gemini and Vertex AI
// implement the cached-content lifecycle (Google AI Studio + Vertex AI named
// caches). Other providers either lack named cache management entirely or
// handle caching implicitly via per-message cache_control markers.
func (provider *AnthropicProvider) CachedContentCreate(ctx *schemas.BifrostContext, key schemas.Key, request *schemas.BifrostCachedContentCreateRequest) (*schemas.BifrostCachedContentCreateResponse, *schemas.BifrostError) {
return nil, providerUtils.NewUnsupportedOperationError(schemas.CachedContentCreateRequest, provider.GetProviderKey())
}

// CachedContentList is unsupported on AnthropicProvider (see CachedContentCreate).
func (provider *AnthropicProvider) CachedContentList(ctx *schemas.BifrostContext, keys []schemas.Key, request *schemas.BifrostCachedContentListRequest) (*schemas.BifrostCachedContentListResponse, *schemas.BifrostError) {
return nil, providerUtils.NewUnsupportedOperationError(schemas.CachedContentListRequest, provider.GetProviderKey())
}

// CachedContentRetrieve is unsupported on AnthropicProvider (see CachedContentCreate).
func (provider *AnthropicProvider) CachedContentRetrieve(ctx *schemas.BifrostContext, keys []schemas.Key, request *schemas.BifrostCachedContentRetrieveRequest) (*schemas.BifrostCachedContentRetrieveResponse, *schemas.BifrostError) {
return nil, providerUtils.NewUnsupportedOperationError(schemas.CachedContentRetrieveRequest, provider.GetProviderKey())
}

// CachedContentUpdate is unsupported on AnthropicProvider (see CachedContentCreate).
func (provider *AnthropicProvider) CachedContentUpdate(ctx *schemas.BifrostContext, keys []schemas.Key, request *schemas.BifrostCachedContentUpdateRequest) (*schemas.BifrostCachedContentUpdateResponse, *schemas.BifrostError) {
return nil, providerUtils.NewUnsupportedOperationError(schemas.CachedContentUpdateRequest, provider.GetProviderKey())
}

// CachedContentDelete is unsupported on AnthropicProvider (see CachedContentCreate).
func (provider *AnthropicProvider) CachedContentDelete(ctx *schemas.BifrostContext, keys []schemas.Key, request *schemas.BifrostCachedContentDeleteRequest) (*schemas.BifrostCachedContentDeleteResponse, *schemas.BifrostError) {
return nil, providerUtils.NewUnsupportedOperationError(schemas.CachedContentDeleteRequest, provider.GetProviderKey())
}
34 changes: 34 additions & 0 deletions core/providers/azure/cachedcontents.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package azure

import (
providerUtils "github.com/maximhq/bifrost/core/providers/utils"
"github.com/maximhq/bifrost/core/schemas"
)

// CachedContentCreate is unsupported on AzureProvider. Only Gemini and Vertex AI
// implement the cached-content lifecycle (Google AI Studio + Vertex AI named
// caches). Other providers either lack named cache management entirely or
// handle caching implicitly via per-message cache_control markers.
func (provider *AzureProvider) CachedContentCreate(ctx *schemas.BifrostContext, key schemas.Key, request *schemas.BifrostCachedContentCreateRequest) (*schemas.BifrostCachedContentCreateResponse, *schemas.BifrostError) {
return nil, providerUtils.NewUnsupportedOperationError(schemas.CachedContentCreateRequest, provider.GetProviderKey())
}

// CachedContentList is unsupported on AzureProvider (see CachedContentCreate).
func (provider *AzureProvider) CachedContentList(ctx *schemas.BifrostContext, keys []schemas.Key, request *schemas.BifrostCachedContentListRequest) (*schemas.BifrostCachedContentListResponse, *schemas.BifrostError) {
return nil, providerUtils.NewUnsupportedOperationError(schemas.CachedContentListRequest, provider.GetProviderKey())
}

// CachedContentRetrieve is unsupported on AzureProvider (see CachedContentCreate).
func (provider *AzureProvider) CachedContentRetrieve(ctx *schemas.BifrostContext, keys []schemas.Key, request *schemas.BifrostCachedContentRetrieveRequest) (*schemas.BifrostCachedContentRetrieveResponse, *schemas.BifrostError) {
return nil, providerUtils.NewUnsupportedOperationError(schemas.CachedContentRetrieveRequest, provider.GetProviderKey())
}

// CachedContentUpdate is unsupported on AzureProvider (see CachedContentCreate).
func (provider *AzureProvider) CachedContentUpdate(ctx *schemas.BifrostContext, keys []schemas.Key, request *schemas.BifrostCachedContentUpdateRequest) (*schemas.BifrostCachedContentUpdateResponse, *schemas.BifrostError) {
return nil, providerUtils.NewUnsupportedOperationError(schemas.CachedContentUpdateRequest, provider.GetProviderKey())
}

// CachedContentDelete is unsupported on AzureProvider (see CachedContentCreate).
func (provider *AzureProvider) CachedContentDelete(ctx *schemas.BifrostContext, keys []schemas.Key, request *schemas.BifrostCachedContentDeleteRequest) (*schemas.BifrostCachedContentDeleteResponse, *schemas.BifrostError) {
return nil, providerUtils.NewUnsupportedOperationError(schemas.CachedContentDeleteRequest, provider.GetProviderKey())
}
8 changes: 4 additions & 4 deletions core/providers/bedrock/bedrock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2605,7 +2605,7 @@ func TestToolResultJSONParsingResponsesAPI(t *testing.T) {
},
}

messages, _, err := bedrock.ConvertBifrostMessagesToBedrockMessages(input)
messages, _, err := bedrock.ConvertBifrostMessagesToBedrockMessages(context.Background(), input)
require.NoError(t, err)
require.Len(t, messages, 1)

Expand Down Expand Up @@ -4510,7 +4510,7 @@ func TestToolResultImageContentResponsesAPI(t *testing.T) {
},
}

messages, _, err := bedrock.ConvertBifrostMessagesToBedrockMessages(input)
messages, _, err := bedrock.ConvertBifrostMessagesToBedrockMessages(context.Background(), input)
require.NoError(t, err)
require.Len(t, messages, 1)

Expand Down Expand Up @@ -4554,7 +4554,7 @@ func TestToolResultImageContentResponsesAPI(t *testing.T) {
},
}

messages, _, err := bedrock.ConvertBifrostMessagesToBedrockMessages(input)
messages, _, err := bedrock.ConvertBifrostMessagesToBedrockMessages(context.Background(), input)
require.NoError(t, err)
require.Len(t, messages, 1)

Expand Down Expand Up @@ -4587,7 +4587,7 @@ func TestToolResultImageContentResponsesAPI(t *testing.T) {
},
}

messages, _, err := bedrock.ConvertBifrostMessagesToBedrockMessages(input)
messages, _, err := bedrock.ConvertBifrostMessagesToBedrockMessages(context.Background(), input)
require.NoError(t, err)
require.Len(t, messages, 1)

Expand Down
34 changes: 34 additions & 0 deletions core/providers/bedrock/cachedcontents.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package bedrock

import (
providerUtils "github.com/maximhq/bifrost/core/providers/utils"
"github.com/maximhq/bifrost/core/schemas"
)

// CachedContentCreate is unsupported on BedrockProvider. Only Gemini and Vertex AI
// implement the cached-content lifecycle (Google AI Studio + Vertex AI named
// caches). Other providers either lack named cache management entirely or
// handle caching implicitly via per-message cache_control markers.
func (provider *BedrockProvider) CachedContentCreate(ctx *schemas.BifrostContext, key schemas.Key, request *schemas.BifrostCachedContentCreateRequest) (*schemas.BifrostCachedContentCreateResponse, *schemas.BifrostError) {
return nil, providerUtils.NewUnsupportedOperationError(schemas.CachedContentCreateRequest, provider.GetProviderKey())
}

// CachedContentList is unsupported on BedrockProvider (see CachedContentCreate).
func (provider *BedrockProvider) CachedContentList(ctx *schemas.BifrostContext, keys []schemas.Key, request *schemas.BifrostCachedContentListRequest) (*schemas.BifrostCachedContentListResponse, *schemas.BifrostError) {
return nil, providerUtils.NewUnsupportedOperationError(schemas.CachedContentListRequest, provider.GetProviderKey())
}

// CachedContentRetrieve is unsupported on BedrockProvider (see CachedContentCreate).
func (provider *BedrockProvider) CachedContentRetrieve(ctx *schemas.BifrostContext, keys []schemas.Key, request *schemas.BifrostCachedContentRetrieveRequest) (*schemas.BifrostCachedContentRetrieveResponse, *schemas.BifrostError) {
return nil, providerUtils.NewUnsupportedOperationError(schemas.CachedContentRetrieveRequest, provider.GetProviderKey())
}

// CachedContentUpdate is unsupported on BedrockProvider (see CachedContentCreate).
func (provider *BedrockProvider) CachedContentUpdate(ctx *schemas.BifrostContext, keys []schemas.Key, request *schemas.BifrostCachedContentUpdateRequest) (*schemas.BifrostCachedContentUpdateResponse, *schemas.BifrostError) {
return nil, providerUtils.NewUnsupportedOperationError(schemas.CachedContentUpdateRequest, provider.GetProviderKey())
}

// CachedContentDelete is unsupported on BedrockProvider (see CachedContentCreate).
func (provider *BedrockProvider) CachedContentDelete(ctx *schemas.BifrostContext, keys []schemas.Key, request *schemas.BifrostCachedContentDeleteRequest) (*schemas.BifrostCachedContentDeleteResponse, *schemas.BifrostError) {
return nil, providerUtils.NewUnsupportedOperationError(schemas.CachedContentDeleteRequest, provider.GetProviderKey())
}
2 changes: 1 addition & 1 deletion core/providers/bedrock/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func ToBedrockChatCompletionRequest(ctx *schemas.BifrostContext, bifrostReq *sch
}

// Convert messages and system messages
messages, systemMessages, err := convertMessages(bifrostReq.Input)
messages, systemMessages, err := convertMessages(ctx, bifrostReq.Input)
if err != nil {
return nil, fmt.Errorf("failed to convert messages: %w", err)
}
Expand Down
Loading
Loading