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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,5 @@ test-reports


# Cursor specific
.cursor/
.cursor/
build/
Comment thread
akshaydeo marked this conversation as resolved.
14 changes: 6 additions & 8 deletions core/schemas/bifrost.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import (
"context"
"encoding/json"
"errors"

"github.com/bytedance/sonic"
)

const (
Expand Down Expand Up @@ -468,17 +466,17 @@ type BifrostStream struct {
// This ensures that only the non-nil embedded struct is marshaled,
func (bs BifrostStream) MarshalJSON() ([]byte, error) {
if bs.BifrostTextCompletionResponse != nil {
return sonic.Marshal(bs.BifrostTextCompletionResponse)
return Marshal(bs.BifrostTextCompletionResponse)
} else if bs.BifrostChatResponse != nil {
return sonic.Marshal(bs.BifrostChatResponse)
return Marshal(bs.BifrostChatResponse)
} else if bs.BifrostResponsesStreamResponse != nil {
return sonic.Marshal(bs.BifrostResponsesStreamResponse)
return Marshal(bs.BifrostResponsesStreamResponse)
} else if bs.BifrostSpeechStreamResponse != nil {
return sonic.Marshal(bs.BifrostSpeechStreamResponse)
return Marshal(bs.BifrostSpeechStreamResponse)
} else if bs.BifrostTranscriptionStreamResponse != nil {
return sonic.Marshal(bs.BifrostTranscriptionStreamResponse)
return Marshal(bs.BifrostTranscriptionStreamResponse)
} else if bs.BifrostError != nil {
return sonic.Marshal(bs.BifrostError)
return Marshal(bs.BifrostError)
}
// Return empty object if both are nil (shouldn't happen in practice)
return []byte("{}"), nil
Expand Down
50 changes: 24 additions & 26 deletions core/schemas/chatcompletions.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"bytes"
"fmt"
"sort"

"github.com/bytedance/sonic"
)

// BifrostChatRequest is the request struct for chat completion requests
Expand Down Expand Up @@ -200,7 +198,7 @@ func (cp *ChatParameters) UnmarshalJSON(data []byte) error {
aux.Alias = (*Alias)(cp)

// Single unmarshal
if err := sonic.Unmarshal(data, &aux); err != nil {
if err := Unmarshal(data, &aux); err != nil {
return err
}

Expand Down Expand Up @@ -288,11 +286,11 @@ type ToolFunctionParameters struct {
func (t *ToolFunctionParameters) UnmarshalJSON(data []byte) error {
// First, try to unmarshal as a JSON string (xAI format)
var jsonStr string
if err := sonic.Unmarshal(data, &jsonStr); err == nil {
if err := Unmarshal(data, &jsonStr); err == nil {
// It's a string, so parse the string as JSON
type Alias ToolFunctionParameters
var temp Alias
if err := sonic.Unmarshal([]byte(jsonStr), &temp); err != nil {
if err := Unmarshal([]byte(jsonStr), &temp); err != nil {
return fmt.Errorf("failed to unmarshal parameters string: %w", err)
}
*t = ToolFunctionParameters(temp)
Expand All @@ -302,7 +300,7 @@ func (t *ToolFunctionParameters) UnmarshalJSON(data []byte) error {
// Otherwise, unmarshal as a normal JSON object
type Alias ToolFunctionParameters
var temp Alias
if err := sonic.Unmarshal(data, &temp); err != nil {
if err := Unmarshal(data, &temp); err != nil {
return err
}
*t = ToolFunctionParameters(temp)
Expand Down Expand Up @@ -370,15 +368,15 @@ func (om OrderedMap) MarshalJSON() ([]byte, error) {
}

// key
keyBytes, err := sonic.Marshal(k)
keyBytes, err := Marshal(k)
if err != nil {
return nil, err
}
buf.Write(keyBytes)
buf.WriteByte(':')

// value
valBytes, err := sonic.Marshal(norm[k])
valBytes, err := Marshal(norm[k])
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -443,13 +441,13 @@ func (ctc ChatToolChoice) MarshalJSON() ([]byte, error) {
}

if ctc.ChatToolChoiceStr != nil {
return sonic.Marshal(ctc.ChatToolChoiceStr)
return Marshal(ctc.ChatToolChoiceStr)
}
if ctc.ChatToolChoiceStruct != nil {
return sonic.Marshal(ctc.ChatToolChoiceStruct)
return Marshal(ctc.ChatToolChoiceStruct)
}
// If both are nil, return null
return sonic.Marshal(nil)
return Marshal(nil)
}

// UnmarshalJSON implements custom JSON unmarshalling for ChatMessageContent.
Expand All @@ -458,15 +456,15 @@ func (ctc ChatToolChoice) MarshalJSON() ([]byte, error) {
func (ctc *ChatToolChoice) UnmarshalJSON(data []byte) error {
// First, try to unmarshal as a direct string
var toolChoiceStr string
if err := sonic.Unmarshal(data, &toolChoiceStr); err == nil {
if err := Unmarshal(data, &toolChoiceStr); err == nil {
ctc.ChatToolChoiceStr = &toolChoiceStr
ctc.ChatToolChoiceStruct = nil
return nil
}

// Try to unmarshal as a direct array of ContentBlock
var chatToolChoice ChatToolChoiceStruct
if err := sonic.Unmarshal(data, &chatToolChoice); err == nil {
if err := Unmarshal(data, &chatToolChoice); err == nil {
ctc.ChatToolChoiceStr = nil
ctc.ChatToolChoiceStruct = &chatToolChoice
return nil
Expand Down Expand Up @@ -523,7 +521,7 @@ type ChatMessage struct {

// UnmarshalJSON implements custom JSON unmarshalling for ChatMessage.
// This is needed because ChatAssistantMessage has a custom UnmarshalJSON method,
// which interferes with sonic's handling of other fields in ChatMessage.
// which interferes with the JSON library's handling of other fields in ChatMessage.
func (cm *ChatMessage) UnmarshalJSON(data []byte) error {
// Unmarshal the base fields directly
type baseFields struct {
Expand All @@ -532,7 +530,7 @@ func (cm *ChatMessage) UnmarshalJSON(data []byte) error {
Content *ChatMessageContent `json:"content,omitempty"`
}
var base baseFields
if err := sonic.Unmarshal(data, &base); err != nil {
if err := Unmarshal(data, &base); err != nil {
return err
}
cm.Name = base.Name
Expand All @@ -542,7 +540,7 @@ func (cm *ChatMessage) UnmarshalJSON(data []byte) error {
// Unmarshal ChatToolMessage fields
type toolMsgAlias ChatToolMessage
var toolMsg toolMsgAlias
if err := sonic.Unmarshal(data, &toolMsg); err != nil {
if err := Unmarshal(data, &toolMsg); err != nil {
return err
}
if toolMsg.ToolCallID != nil {
Expand All @@ -551,7 +549,7 @@ func (cm *ChatMessage) UnmarshalJSON(data []byte) error {

// Unmarshal ChatAssistantMessage (which has its own custom unmarshaller)
var assistantMsg ChatAssistantMessage
if err := sonic.Unmarshal(data, &assistantMsg); err != nil {
if err := Unmarshal(data, &assistantMsg); err != nil {
return err
}
// Only set if any field is populated
Expand Down Expand Up @@ -579,13 +577,13 @@ func (mc ChatMessageContent) MarshalJSON() ([]byte, error) {
}

if mc.ContentStr != nil {
return sonic.Marshal(*mc.ContentStr)
return Marshal(*mc.ContentStr)
}
if mc.ContentBlocks != nil {
return sonic.Marshal(mc.ContentBlocks)
return Marshal(mc.ContentBlocks)
}
// If both are nil, return null
return sonic.Marshal(nil)
return Marshal(nil)
}

// UnmarshalJSON implements custom JSON unmarshalling for ChatMessageContent.
Expand All @@ -601,15 +599,15 @@ func (mc *ChatMessageContent) UnmarshalJSON(data []byte) error {

// First, try to unmarshal as a direct string
var stringContent string
if err := sonic.Unmarshal(data, &stringContent); err == nil {
if err := Unmarshal(data, &stringContent); err == nil {
mc.ContentStr = &stringContent
mc.ContentBlocks = nil
return nil
}

// Try to unmarshal as a direct array of ContentBlock
var arrayContent []ChatContentBlock
if err := sonic.Unmarshal(data, &arrayContent); err == nil {
if err := Unmarshal(data, &arrayContent); err == nil {
mc.ContentBlocks = arrayContent
mc.ContentStr = nil
return nil
Expand Down Expand Up @@ -709,7 +707,7 @@ func (cm *ChatAssistantMessage) UnmarshalJSON(data []byte) error {
ReasoningContent *string `json:"reasoning_content,omitempty"` // xAI uses this field name
}

if err := sonic.Unmarshal(data, &aux); err != nil {
if err := Unmarshal(data, &aux); err != nil {
return err
}

Expand Down Expand Up @@ -856,7 +854,7 @@ func (d *ChatStreamResponseChoiceDelta) UnmarshalJSON(data []byte) error {
ReasoningContent *string `json:"reasoning_content,omitempty"` // xAI uses this field name
}

if err := sonic.Unmarshal(data, &aux); err != nil {
if err := Unmarshal(data, &aux); err != nil {
return err
}

Expand Down Expand Up @@ -945,7 +943,7 @@ type BifrostCost struct {
func (bc *BifrostCost) UnmarshalJSON(data []byte) error {
// First, try to unmarshal as a direct float
var costFloat float64
if err := sonic.Unmarshal(data, &costFloat); err == nil {
if err := Unmarshal(data, &costFloat); err == nil {
bc.TotalCost = costFloat
return nil
}
Expand All @@ -954,7 +952,7 @@ func (bc *BifrostCost) UnmarshalJSON(data []byte) error {
// Use a type alias to avoid infinite recursion
type Alias BifrostCost
var costStruct Alias
if err := sonic.Unmarshal(data, &costStruct); err == nil {
if err := Unmarshal(data, &costStruct); err == nil {
*bc = BifrostCost(costStruct)
return nil
}
Expand Down
30 changes: 14 additions & 16 deletions core/schemas/embedding.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package schemas

import (
"fmt"

"github.com/bytedance/sonic"
)

type BifrostEmbeddingRequest struct {
Expand Down Expand Up @@ -58,16 +56,16 @@ func (e *EmbeddingInput) MarshalJSON() ([]byte, error) {
}

if e.Text != nil {
return sonic.Marshal(*e.Text)
return Marshal(*e.Text)
}
if e.Texts != nil {
return sonic.Marshal(e.Texts)
return Marshal(e.Texts)
}
if e.Embedding != nil {
return sonic.Marshal(e.Embedding)
return Marshal(e.Embedding)
}
if e.Embeddings != nil {
return sonic.Marshal(e.Embeddings)
return Marshal(e.Embeddings)
}

return nil, fmt.Errorf("invalid embedding input")
Expand All @@ -80,25 +78,25 @@ func (e *EmbeddingInput) UnmarshalJSON(data []byte) error {
e.Embeddings = nil
// Try string
var s string
if err := sonic.Unmarshal(data, &s); err == nil {
if err := Unmarshal(data, &s); err == nil {
e.Text = &s
return nil
}
// Try []string
var ss []string
if err := sonic.Unmarshal(data, &ss); err == nil {
if err := Unmarshal(data, &ss); err == nil {
e.Texts = ss
return nil
}
// Try []int
var i []int
if err := sonic.Unmarshal(data, &i); err == nil {
if err := Unmarshal(data, &i); err == nil {
e.Embedding = i
return nil
}
// Try [][]int
var i2 [][]int
if err := sonic.Unmarshal(data, &i2); err == nil {
if err := Unmarshal(data, &i2); err == nil {
e.Embeddings = i2
return nil
}
Expand Down Expand Up @@ -129,35 +127,35 @@ type EmbeddingStruct struct {

func (be EmbeddingStruct) MarshalJSON() ([]byte, error) {
if be.EmbeddingStr != nil {
return sonic.Marshal(be.EmbeddingStr)
return Marshal(be.EmbeddingStr)
}
if be.EmbeddingArray != nil {
return sonic.Marshal(be.EmbeddingArray)
return Marshal(be.EmbeddingArray)
}
if be.Embedding2DArray != nil {
return sonic.Marshal(be.Embedding2DArray)
return Marshal(be.Embedding2DArray)
}
return nil, fmt.Errorf("no embedding found")
}

func (be *EmbeddingStruct) UnmarshalJSON(data []byte) error {
// First, try to unmarshal as a direct string
var stringContent string
if err := sonic.Unmarshal(data, &stringContent); err == nil {
if err := Unmarshal(data, &stringContent); err == nil {
be.EmbeddingStr = &stringContent
return nil
}

// Try to unmarshal as a direct array of float32
var arrayContent []float32
if err := sonic.Unmarshal(data, &arrayContent); err == nil {
if err := Unmarshal(data, &arrayContent); err == nil {
be.EmbeddingArray = arrayContent
return nil
}

// Try to unmarshal as a direct 2D array of float32
var arrayContent2D [][]float32
if err := sonic.Unmarshal(data, &arrayContent2D); err == nil {
if err := Unmarshal(data, &arrayContent2D); err == nil {
be.Embedding2DArray = arrayContent2D
return nil
}
Expand Down
20 changes: 20 additions & 0 deletions core/schemas/json_native.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//go:build !tinygo && !wasm

package schemas

import "github.com/bytedance/sonic"

// Marshal encodes v to JSON bytes using the high-performance sonic library.
func Marshal(v interface{}) ([]byte, error) {
return sonic.Marshal(v)
}

// MarshalString encodes v to a JSON string using sonic.
func MarshalString(v interface{}) (string, error) {
return sonic.MarshalString(v)
}

// Unmarshal decodes JSON data into v using sonic.
func Unmarshal(data []byte, v interface{}) error {
return sonic.Unmarshal(data, v)
}
24 changes: 24 additions & 0 deletions core/schemas/json_wasm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//go:build tinygo || wasm

package schemas

import "encoding/json"

// Marshal encodes v to JSON bytes using the standard library.
func Marshal(v interface{}) ([]byte, error) {
return json.Marshal(v)
}

// MarshalString encodes v to a JSON string using the standard library.
func MarshalString(v interface{}) (string, error) {
data, err := json.Marshal(v)
if err != nil {
return "", err
}
return string(data), nil
}

// Unmarshal decodes JSON data into v using the standard library.
func Unmarshal(data []byte, v interface{}) error {
return json.Unmarshal(data, v)
}
2 changes: 2 additions & 0 deletions core/schemas/mcp.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build !tinygo && !wasm

// Package schemas defines the core schemas and types used by the Bifrost system.
package schemas

Expand Down
Loading