5
5
"encoding/json"
6
6
"io"
7
7
"net/http"
8
+ "net/url"
8
9
"os"
9
10
"reflect"
10
11
"strings"
@@ -31,6 +32,9 @@ type Message struct {
31
32
// object or an array of content objects
32
33
Content any `json:"content,omitempty"`
33
34
35
+ // Any tool calls
36
+ ToolCalls []ToolCall `json:"tool_calls,omitempty"`
37
+
34
38
// Time the message was created, in unix seconds
35
39
Created int64 `json:"created,omitempty"`
36
40
}
@@ -72,8 +76,12 @@ type Content struct {
72
76
Type string `json:"type" writer:",width:4"`
73
77
Text string `json:"text,omitempty" writer:",width:60,wrap"`
74
78
Source * contentSource `json:"source,omitempty"`
79
+ Url * contentImage `json:"image_url,omitempty"`
80
+
81
+ // Tool Function Call
75
82
toolUse
76
83
84
+ // Tool Result
77
85
ToolId string `json:"tool_use_id,omitempty"`
78
86
Result string `json:"content,omitempty"`
79
87
}
@@ -85,6 +93,25 @@ type contentSource struct {
85
93
Data string `json:"data,omitempty"`
86
94
}
87
95
96
+ // Image Source
97
+ type contentImage struct {
98
+ Url string `json:"url,omitempty"`
99
+ Detail string `json:"detail,omitempty"`
100
+ }
101
+
102
+ // Tool Call
103
+ type ToolCall struct {
104
+ Id string `json:"id,omitempty"`
105
+ Type string `json:"type,omitempty"`
106
+ Function ToolFunction `json:"function,omitempty"`
107
+ }
108
+
109
+ // Tool Function and Arguments
110
+ type ToolFunction struct {
111
+ Name string `json:"name,omitempty"`
112
+ Arguments string `json:"arguments,omitempty"`
113
+ }
114
+
88
115
// Tool call
89
116
type toolUse struct {
90
117
Name string `json:"name,omitempty"`
@@ -148,6 +175,46 @@ func ImageData(path string) (*Content, error) {
148
175
return Image (r )
149
176
}
150
177
178
+ // Return a new content object of type image, from a Url
179
+ func ImageUrl (v , detail string ) (* Content , error ) {
180
+ url , err := url .Parse (v )
181
+ if err != nil {
182
+ return nil , err
183
+ }
184
+ if url .Scheme != "https" {
185
+ return nil , ErrBadParameter .With ("ImageUrl: not an https url" )
186
+ }
187
+ return & Content {
188
+ Type : "image_url" ,
189
+ Url : & contentImage {
190
+ Url : url .String (),
191
+ Detail : detail ,
192
+ },
193
+ }, nil
194
+ }
195
+
196
+ // Return tool usage
197
+ func ToolUse (t ToolCall ) * Content {
198
+ var input map [string ]any
199
+
200
+ // Decode the arguments
201
+ if t .Function .Arguments != "" {
202
+ if err := json .Unmarshal ([]byte (t .Function .Arguments ), & input ); err != nil {
203
+ return nil
204
+ }
205
+ }
206
+
207
+ // Return the content
208
+ return & Content {
209
+ Type : t .Type ,
210
+ Id : t .Id ,
211
+ toolUse : toolUse {
212
+ Name : t .Function .Name ,
213
+ Input : input ,
214
+ },
215
+ }
216
+ }
217
+
151
218
// Return a tool result
152
219
func ToolResult (id string , result string ) * Content {
153
220
return & Content {Type : "tool_result" , ToolId : id , Result : result }
0 commit comments