Skip to content

Commit d81b8db

Browse files
chore: maxim plugin refactor to support core/v1.1.0
1 parent 8b814ca commit d81b8db

File tree

4 files changed

+36
-33
lines changed

4 files changed

+36
-33
lines changed

plugins/maxim/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module github.com/maximhq/bifrost/plugins/maxim
33
go 1.24.1
44

55
require (
6-
github.com/maximhq/bifrost/core v1.0.9
6+
github.com/maximhq/bifrost/core v1.1.0
77
github.com/maximhq/maxim-go v0.1.3
88
)
99

plugins/maxim/go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
3636
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
3737
github.com/klauspost/compress v1.18.0 h1:c/Cqfb0r+Yi+JtIEq73FWXVkRonBlf0CRNYc8Zttxdo=
3838
github.com/klauspost/compress v1.18.0/go.mod h1:2Pp+KzxcywXVXMr50+X0Q/Lsb43OQHYWRCY2AiWywWQ=
39-
github.com/maximhq/bifrost/core v1.0.9 h1:OWUHCWCQsBH43YPIy2AsqNMZhoFXYe/qhJSCLbw5su8=
40-
github.com/maximhq/bifrost/core v1.0.9/go.mod h1:8ycaWQ9bjQezoUT/x6a82VmPjoqLzyGglQ0RnnlZjqo=
39+
github.com/maximhq/bifrost/core v1.1.0 h1:qZ3rm6SjZqx4R/5Wu9Fen7yLkL3SFK8dXyiQ2k08Oro=
40+
github.com/maximhq/bifrost/core v1.1.0/go.mod h1:8ycaWQ9bjQezoUT/x6a82VmPjoqLzyGglQ0RnnlZjqo=
4141
github.com/maximhq/maxim-go v0.1.3 h1:nVzdz3hEjZVxmWHARWIM+Yrn1Jp50qrsK4BA/sz2jj8=
4242
github.com/maximhq/maxim-go v0.1.3/go.mod h1:0+UTWM7UZwNNE5VnljLtr/vpRGtYP8r/2q9WDwlLWFw=
4343
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=

plugins/maxim/main.go

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -103,15 +103,15 @@ type Plugin struct {
103103
// Returns:
104104
// - *schemas.BifrostRequest: The original request, unmodified
105105
// - error: Any error that occurred during trace/generation creation
106-
func (plugin *Plugin) PreHook(ctx *context.Context, req *schemas.BifrostRequest) (*schemas.BifrostRequest, error) {
106+
func (plugin *Plugin) PreHook(ctx *context.Context, req *schemas.BifrostRequest) (*schemas.BifrostRequest, *schemas.BifrostResponse, error) {
107107
var traceID string
108108
var sessionID string
109109

110110
// Check if context already has traceID and generationID
111111
if ctx != nil {
112112
if existingGenerationID, ok := (*ctx).Value(GenerationIDKey).(string); ok && existingGenerationID != "" {
113113
// If generationID exists, return early
114-
return req, nil
114+
return req, nil, nil
115115
}
116116

117117
if existingTraceID, ok := (*ctx).Value(TraceIDKey).(string); ok && existingTraceID != "" {
@@ -137,32 +137,28 @@ func (plugin *Plugin) PreHook(ctx *context.Context, req *schemas.BifrostRequest)
137137
"model": req.Model,
138138
}
139139
for _, message := range *req.Input.ChatCompletionInput {
140-
if message.Content != nil {
141-
messages = append(messages, logging.CompletionRequest{
142-
Role: string(message.Role),
143-
Content: message.Content,
144-
})
145-
} else if message.UserMessage != nil && message.UserMessage.ImageContent != nil {
146-
messages = append(messages, logging.CompletionRequest{
147-
Role: string(message.Role),
148-
Content: message.UserMessage.ImageContent,
149-
})
150-
} else if message.ToolCalls != nil {
151-
messages = append(messages, logging.CompletionRequest{
152-
Role: string(message.Role),
153-
Content: message.ToolCalls,
154-
})
155-
} else if message.ToolMessage != nil && message.ToolMessage.ToolCallID != nil {
156-
messages = append(messages, logging.CompletionRequest{
157-
Role: string(message.Role),
158-
Content: message.ToolMessage,
159-
})
160-
}
140+
messages = append(messages, logging.CompletionRequest{
141+
Role: string(message.Role),
142+
Content: message.Content,
143+
})
161144
}
162145
if len(*req.Input.ChatCompletionInput) > 0 {
163146
lastMsg := (*req.Input.ChatCompletionInput)[len(*req.Input.ChatCompletionInput)-1]
164-
if lastMsg.Content != nil {
165-
latestMessage = *lastMsg.Content
147+
if lastMsg.Content.ContentStr != nil {
148+
latestMessage = *lastMsg.Content.ContentStr
149+
} else if lastMsg.Content.ContentBlocks != nil {
150+
// Find the last text content block
151+
for i := len(*lastMsg.Content.ContentBlocks) - 1; i >= 0; i-- {
152+
block := (*lastMsg.Content.ContentBlocks)[i]
153+
if block.Type == "text" && block.Text != nil {
154+
latestMessage = *block.Text
155+
break
156+
}
157+
}
158+
// If no text block found, use placeholder
159+
if latestMessage == "" {
160+
latestMessage = "-"
161+
}
166162
}
167163
}
168164
} else if req.Input.TextCompletionInput != nil {
@@ -172,7 +168,7 @@ func (plugin *Plugin) PreHook(ctx *context.Context, req *schemas.BifrostRequest)
172168
"model": req.Model,
173169
}
174170
messages = append(messages, logging.CompletionRequest{
175-
Role: "user",
171+
Role: string(schemas.ModelChatMessageRoleUser),
176172
Content: req.Input.TextCompletionInput,
177173
})
178174
latestMessage = *req.Input.TextCompletionInput
@@ -225,7 +221,7 @@ func (plugin *Plugin) PreHook(ctx *context.Context, req *schemas.BifrostRequest)
225221
*ctx = context.WithValue(*ctx, GenerationIDKey, generationID)
226222
}
227223

228-
return req, nil
224+
return req, nil, nil
229225
}
230226

231227
// PostHook is called after a request has been processed by Bifrost.
@@ -265,3 +261,9 @@ func (plugin *Plugin) PostHook(ctxRef *context.Context, res *schemas.BifrostResp
265261

266262
return res, nil
267263
}
264+
265+
func (plugin *Plugin) Cleanup() error {
266+
plugin.logger.Flush()
267+
268+
return nil
269+
}

plugins/maxim/plugin_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,10 @@ func TestMaximLoggerPlugin(t *testing.T) {
109109
Input: schemas.RequestInput{
110110
ChatCompletionInput: &[]schemas.BifrostMessage{
111111
{
112-
Role: "user",
113-
Content: bifrost.Ptr("Hello, how are you?"),
112+
Role: "user",
113+
Content: schemas.MessageContent{
114+
ContentStr: bifrost.Ptr("Hello, how are you?"),
115+
},
114116
},
115117
},
116118
},
@@ -122,6 +124,5 @@ func TestMaximLoggerPlugin(t *testing.T) {
122124

123125
log.Println("Bifrost request completed, check your Maxim Dashboard for the trace")
124126

125-
// Clean up resources
126127
client.Cleanup()
127128
}

0 commit comments

Comments
 (0)