Skip to content

Commit 45f4afb

Browse files
mend
1 parent 14dc063 commit 45f4afb

7 files changed

Lines changed: 95 additions & 45 deletions

File tree

scripts/install.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
$ErrorActionPreference = "Stop"
66

77
# Configuration
8-
$Repo = "Agent-Field/agentfield"
8+
$Repo = "BenjaminGoehry/agentfield"
99
$InstallDir = if ($env:AGENTFIELD_INSTALL_DIR) { $env:AGENTFIELD_INSTALL_DIR } else { "$env:USERPROFILE\.agentfield\bin" }
1010
$Version = if ($env:VERSION) { $env:VERSION } else { "latest" }
1111
$Verbose = if ($env:VERBOSE -eq "1") { $true } else { $false }

sdk/go/agent/agent_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -512,7 +512,9 @@ func TestAI(t *testing.T) {
512512
Choices: []ai.Choice{
513513
{
514514
Message: ai.Message{
515-
Content: "AI response",
515+
Content: []ai.ContentPart{
516+
{Type: "text", Text: "AI response"},
517+
},
516518
},
517519
},
518520
},

sdk/go/ai/client.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,14 +76,24 @@ func (c *Client) CompleteWithMessages(ctx context.Context, messages []Message, o
7676
if err := opt(req); err != nil {
7777
return nil, fmt.Errorf("apply option: %w", err)
7878
}
79-
8079
}
80+
8181
return c.doRequest(ctx, req)
8282
}
8383

8484
func (c *Client) doRequest(ctx context.Context, req *Request) (*Response, error) {
8585
// Marshal request
86-
body, err := json.Marshal(req)
86+
87+
var body []byte
88+
var err error
89+
90+
if c.config.IsOpenRouter() {
91+
payload := transformForOpenRouter(req)
92+
body, err = json.Marshal(payload)
93+
fmt.Printf("[DEBUG] OpenRouter JSON:\n%s\n", string(body))
94+
} else {
95+
body, err = json.Marshal(req)
96+
}
8797
if err != nil {
8898
return nil, fmt.Errorf("marshal request: %w", err)
8999
}

sdk/go/ai/config.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,3 +89,58 @@ func (c *Config) IsOpenRouter() bool {
8989
return c.BaseURL == "https://openrouter.ai/api/v1" ||
9090
c.BaseURL == "https://openrouter.ai/api/v1/"
9191
}
92+
93+
type OpenRouterRequest struct {
94+
Messages []OpenRouterMessage `json:"messages"`
95+
Model string `json:"model,omitempty"`
96+
}
97+
98+
type OpenRouterMessage struct {
99+
Role string `json:"role"`
100+
Content []OpenRouterContentPart `json:"content"`
101+
}
102+
103+
type OpenRouterContentPart struct {
104+
Type string `json:"type"`
105+
Text string `json:"text,omitempty"`
106+
ImageURL *ImageData `json:"image_url,omitempty"`
107+
}
108+
109+
type ImageData struct {
110+
URL string `json:"url"`
111+
Detail string `json:"detail,omitempty"`
112+
}
113+
114+
func transformForOpenRouter(req *Request) *OpenRouterRequest {
115+
var messages []OpenRouterMessage
116+
117+
for _, m := range req.Messages {
118+
msg := OpenRouterMessage{
119+
Role: m.Role,
120+
}
121+
122+
for _, c := range m.Content {
123+
part := OpenRouterContentPart{
124+
Type: c.Type,
125+
Text: c.Text,
126+
}
127+
128+
// Map Go struct's input_image to OpenRouter's image_url
129+
if c.Type == "input_image" && c.ImageURL != "" {
130+
part.Type = "image_url"
131+
part.ImageURL = &ImageData{
132+
URL: c.ImageURL,
133+
}
134+
}
135+
136+
msg.Content = append(msg.Content, part)
137+
}
138+
139+
messages = append(messages, msg)
140+
}
141+
142+
return &OpenRouterRequest{
143+
Messages: messages,
144+
Model: req.Model,
145+
}
146+
}

sdk/go/ai/multimodal.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,6 @@ package ai
22

33
import "strings"
44

5-
type ImageData struct {
6-
URL string `json:"url"`
7-
Detail string `json:"detail,omitempty"`
8-
}
9-
105
func detectMIMEType(path string) string {
116
switch {
127
case strings.HasSuffix(path, ".png"):

sdk/go/ai/request.go

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ type Message struct {
4141
}
4242

4343
type ContentPart struct {
44-
Type string `json:"type"` // "text" or "image_url"
45-
Text string `json:"text,omitempty"`
46-
ImageURL *ImageData `json:"image_url,omitempty"`
44+
Type string `json:"type"` // "text" or "image_url"
45+
Text string `json:"text,omitempty"`
46+
ImageURL string `json:"image_url,omitempty"`
4747
}
4848

4949
func (m *Message) UnmarshalJSON(data []byte) error {
@@ -216,11 +216,8 @@ func WithImageFile(path string) Option {
216216

217217
last := &r.Messages[len(r.Messages)-1]
218218
last.Content = append(last.Content, ContentPart{
219-
Type: "image_url",
220-
ImageURL: &ImageData{
221-
URL: "data:" + mimeType + ";base64," + encoded,
222-
Detail: "auto",
223-
},
219+
Type: "input_image",
220+
ImageURL: "data:" + mimeType + ";base64," + encoded,
224221
})
225222

226223
return nil
@@ -235,14 +232,13 @@ func WithImageURL(url string) Option {
235232
Content: []ContentPart{},
236233
})
237234
}
235+
238236
last := &r.Messages[len(r.Messages)-1]
239237
last.Content = append(last.Content, ContentPart{
240-
Type: "image_url",
241-
ImageURL: &ImageData{
242-
URL: url,
243-
Detail: "auto",
244-
},
238+
Type: "input_image",
239+
ImageURL: url,
245240
})
241+
246242
return nil
247243
}
248244
}
@@ -252,6 +248,7 @@ func WithImageBytes(data []byte, mimeType string) Option {
252248
if len(data) == 0 {
253249
return nil
254250
}
251+
255252
encoded := base64.StdEncoding.EncodeToString(data)
256253

257254
if len(r.Messages) == 0 {
@@ -263,11 +260,8 @@ func WithImageBytes(data []byte, mimeType string) Option {
263260

264261
last := &r.Messages[len(r.Messages)-1]
265262
last.Content = append(last.Content, ContentPart{
266-
Type: "image_url",
267-
ImageURL: &ImageData{
268-
URL: "data:" + mimeType + ";base64," + encoded,
269-
Detail: "auto",
270-
},
263+
Type: "input_image",
264+
ImageURL: "data:" + mimeType + ";base64," + encoded,
271265
})
272266

273267
return nil

sdk/go/ai/request_test.go

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -176,9 +176,8 @@ func TestWithImageFile(t *testing.T) {
176176
assert.Len(t, req.Messages[0].Content, 1)
177177

178178
part := req.Messages[0].Content[0]
179-
assert.Equal(t, "image_url", part.Type)
180-
assert.NotNil(t, part.ImageURL)
181-
assert.Contains(t, part.ImageURL.URL, "data:image/jpeg;base64,")
179+
assert.Equal(t, "input_image", part.Type)
180+
assert.Contains(t, part.ImageURL, "data:image/jpeg;base64,")
182181
}
183182

184183
func TestWithImageURL(t *testing.T) {
@@ -193,9 +192,8 @@ func TestWithImageURL(t *testing.T) {
193192
assert.Len(t, req.Messages[0].Content, 1)
194193

195194
part := req.Messages[0].Content[0]
196-
assert.Equal(t, "image_url", part.Type)
197-
assert.NotNil(t, part.ImageURL)
198-
assert.Equal(t, testURL, part.ImageURL.URL)
195+
assert.Equal(t, "input_image", part.Type)
196+
assert.Equal(t, testURL, part.ImageURL)
199197
}
200198

201199
func TestWithImageBytes(t *testing.T) {
@@ -211,9 +209,8 @@ func TestWithImageBytes(t *testing.T) {
211209
assert.Len(t, req.Messages[0].Content, 1)
212210

213211
part := req.Messages[0].Content[0]
214-
assert.Equal(t, "image_url", part.Type)
215-
assert.NotNil(t, part.ImageURL)
216-
assert.Contains(t, part.ImageURL.URL, "data:image/jpeg;base64,")
212+
assert.Equal(t, "input_image", part.Type)
213+
assert.Contains(t, part.ImageURL, "data:image/jpeg;base64,")
217214
}
218215

219216
func TestWithImageFile_Error(t *testing.T) {
@@ -266,19 +263,16 @@ func TestMultipleImages(t *testing.T) {
266263
assert.Len(t, req.Messages[0].Content, 3)
267264

268265
part1 := req.Messages[0].Content[0]
269-
assert.Equal(t, "image_url", part1.Type)
270-
assert.NotNil(t, part1.ImageURL)
271-
assert.Equal(t, "https://example.com/image1.jpg", part1.ImageURL.URL)
266+
assert.Equal(t, "input_image", part1.Type)
267+
assert.Equal(t, "https://example.com/image1.jpg", part1.ImageURL)
272268

273269
part2 := req.Messages[0].Content[1]
274-
assert.Equal(t, "image_url", part2.Type)
275-
assert.NotNil(t, part2.ImageURL)
276-
assert.Contains(t, part2.ImageURL.URL, "data:image/jpeg;base64,")
270+
assert.Equal(t, "input_image", part2.Type)
271+
assert.Contains(t, part2.ImageURL, "data:image/jpeg;base64,")
277272

278273
part3 := req.Messages[0].Content[2]
279-
assert.Equal(t, "image_url", part3.Type)
280-
assert.NotNil(t, part3.ImageURL)
281-
assert.Contains(t, part3.ImageURL.URL, "data:image/png;base64,")
274+
assert.Equal(t, "input_image", part3.Type)
275+
assert.Contains(t, part3.ImageURL, "data:image/png;base64,")
282276
}
283277

284278
func TestStructToJSONSchema(t *testing.T) {

0 commit comments

Comments
 (0)