feat: add Media field to Message struct and implement serializeMessages for vision API support

- Add Media []string field to Message struct for image/media URLs
- Implement serializeMessages() to format messages with image_url content parts
- Enables OpenAI-compatible vision APIs to receive image attachments
This commit is contained in:
Zachary Guerrero 2026-02-20 14:32:58 -08:00
parent e23795e51b
commit 42725e0fe0
2 changed files with 43 additions and 1 deletions

View file

@ -69,7 +69,7 @@ func (p *Provider) Chat(ctx context.Context, messages []Message, tools []ToolDef
requestBody := map[string]interface{}{
"model": model,
"messages": messages,
"messages": serializeMessages(messages),
}
if len(tools) > 0 {
@ -135,6 +135,47 @@ func (p *Provider) Chat(ctx context.Context, messages []Message, tools []ToolDef
return parseResponse(body)
}
func serializeMessages(messages []Message) []map[string]interface{} {
result := make([]map[string]interface{}, 0, len(messages))
for _, m := range messages {
if len(m.Media) == 0 {
msg := map[string]interface{}{
"role": m.Role,
"content": m.Content,
}
if m.ToolCallID != "" {
msg["tool_call_id"] = m.ToolCallID
}
if len(m.ToolCalls) > 0 {
msg["tool_calls"] = m.ToolCalls
}
result = append(result, msg)
continue
}
parts := make([]map[string]interface{}, 0, 1+len(m.Media))
if m.Content != "" {
parts = append(parts, map[string]interface{}{
"type": "text",
"text": m.Content,
})
}
for _, mediaURL := range m.Media {
parts = append(parts, map[string]interface{}{
"type": "image_url",
"image_url": map[string]interface{}{
"url": mediaURL,
},
})
}
result = append(result, map[string]interface{}{
"role": m.Role,
"content": parts,
})
}
return result
}
func parseResponse(body []byte) (*LLMResponse, error) {
var apiResponse struct {
Choices []struct {

View file

@ -40,6 +40,7 @@ type UsageInfo struct {
type Message struct {
Role string `json:"role"`
Content string `json:"content"`
Media []string `json:"media,omitempty"` // URLs of images or other media attachments
ToolCalls []ToolCall `json:"tool_calls,omitempty"`
ToolCallID string `json:"tool_call_id,omitempty"`
}