1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
|
package agent
import (
"encoding/json"
"fmt"
"regexp"
"strconv"
"strings"
"gf-lt/models"
)
type ToolFunc func(map[string]string) []byte
var pwToolMap = make(map[string]ToolFunc)
func RegisterPWTool(name string, fn ToolFunc) {
pwToolMap[name] = fn
}
func GetPWTools() []models.Tool {
return pwTools
}
var pwTools = []models.Tool{
{
Type: "function",
Function: models.ToolFunc{
Name: "pw_start",
Description: "Start a Playwright browser instance. Must be called first before any other browser automation. Uses headless mode by default.",
Parameters: models.ToolFuncParams{
Type: "object",
Required: []string{},
Properties: map[string]models.ToolArgProps{},
},
},
},
{
Type: "function",
Function: models.ToolFunc{
Name: "pw_stop",
Description: "Stop the Playwright browser instance. Call when done with browser automation.",
Parameters: models.ToolFuncParams{
Type: "object",
Required: []string{},
Properties: map[string]models.ToolArgProps{},
},
},
},
{
Type: "function",
Function: models.ToolFunc{
Name: "pw_is_running",
Description: "Check if Playwright browser is currently running.",
Parameters: models.ToolFuncParams{
Type: "object",
Required: []string{},
Properties: map[string]models.ToolArgProps{},
},
},
},
{
Type: "function",
Function: models.ToolFunc{
Name: "pw_navigate",
Description: "Navigate to a URL in the browser.",
Parameters: models.ToolFuncParams{
Type: "object",
Required: []string{"url"},
Properties: map[string]models.ToolArgProps{
"url": {Type: "string", Description: "URL to navigate to"},
},
},
},
},
{
Type: "function",
Function: models.ToolFunc{
Name: "pw_click",
Description: "Click on an element on the current webpage. Use 'index' for multiple matches (default 0).",
Parameters: models.ToolFuncParams{
Type: "object",
Required: []string{"selector"},
Properties: map[string]models.ToolArgProps{
"selector": {Type: "string", Description: "CSS selector for the element"},
"index": {Type: "integer", Description: "Index for multiple matches (default 0)"},
},
},
},
},
{
Type: "function",
Function: models.ToolFunc{
Name: "pw_fill",
Description: "Type text into an input field. Use 'index' for multiple matches (default 0).",
Parameters: models.ToolFuncParams{
Type: "object",
Required: []string{"selector", "text"},
Properties: map[string]models.ToolArgProps{
"selector": {Type: "string", Description: "CSS selector for the input element"},
"text": {Type: "string", Description: "Text to type into the field"},
"index": {Type: "integer", Description: "Index for multiple matches (default 0)"},
},
},
},
},
{
Type: "function",
Function: models.ToolFunc{
Name: "pw_extract_text",
Description: "Extract text content from the page or specific elements. Use selector 'body' for all page text.",
Parameters: models.ToolFuncParams{
Type: "object",
Required: []string{},
Properties: map[string]models.ToolArgProps{
"selector": {Type: "string", Description: "CSS selector (default 'body' for all page text)"},
},
},
},
},
{
Type: "function",
Function: models.ToolFunc{
Name: "pw_screenshot",
Description: "Take a screenshot of the page or a specific element. Returns a file path to the image.",
Parameters: models.ToolFuncParams{
Type: "object",
Required: []string{},
Properties: map[string]models.ToolArgProps{
"selector": {Type: "string", Description: "CSS selector for element to screenshot"},
"full_page": {Type: "boolean", Description: "Capture full page (default false)"},
},
},
},
},
{
Type: "function",
Function: models.ToolFunc{
Name: "pw_screenshot_and_view",
Description: "Take a screenshot and return the image for viewing. Use to visually verify page state.",
Parameters: models.ToolFuncParams{
Type: "object",
Required: []string{},
Properties: map[string]models.ToolArgProps{
"selector": {Type: "string", Description: "CSS selector for element to screenshot"},
"full_page": {Type: "boolean", Description: "Capture full page (default false)"},
},
},
},
},
{
Type: "function",
Function: models.ToolFunc{
Name: "pw_wait_for_selector",
Description: "Wait for an element to appear on the page before proceeding.",
Parameters: models.ToolFuncParams{
Type: "object",
Required: []string{"selector"},
Properties: map[string]models.ToolArgProps{
"selector": {Type: "string", Description: "CSS selector to wait for"},
"timeout": {Type: "integer", Description: "Timeout in milliseconds (default 30000)"},
},
},
},
},
{
Type: "function",
Function: models.ToolFunc{
Name: "pw_drag",
Description: "Drag the mouse from point (x1,y1) to (x2,y2).",
Parameters: models.ToolFuncParams{
Type: "object",
Required: []string{"x1", "y1", "x2", "y2"},
Properties: map[string]models.ToolArgProps{
"x1": {Type: "number", Description: "Starting X coordinate"},
"y1": {Type: "number", Description: "Starting Y coordinate"},
"x2": {Type: "number", Description: "Ending X coordinate"},
"y2": {Type: "number", Description: "Ending Y coordinate"},
},
},
},
},
{
Type: "function",
Function: models.ToolFunc{
Name: "pw_click_at",
Description: "Click at specific X,Y coordinates on the page.",
Parameters: models.ToolFuncParams{
Type: "object",
Required: []string{"x", "y"},
Properties: map[string]models.ToolArgProps{
"x": {Type: "number", Description: "X coordinate"},
"y": {Type: "number", Description: "Y coordinate"},
},
},
},
},
{
Type: "function",
Function: models.ToolFunc{
Name: "pw_get_html",
Description: "Get the HTML content of the page or a specific element.",
Parameters: models.ToolFuncParams{
Type: "object",
Required: []string{},
Properties: map[string]models.ToolArgProps{
"selector": {Type: "string", Description: "CSS selector (default 'body')"},
},
},
},
},
{
Type: "function",
Function: models.ToolFunc{
Name: "pw_get_dom",
Description: "Get a structured DOM representation with tag, attributes, text, and children.",
Parameters: models.ToolFuncParams{
Type: "object",
Required: []string{},
Properties: map[string]models.ToolArgProps{
"selector": {Type: "string", Description: "CSS selector (default 'body')"},
},
},
},
},
{
Type: "function",
Function: models.ToolFunc{
Name: "pw_search_elements",
Description: "Search for elements by text content or CSS selector.",
Parameters: models.ToolFuncParams{
Type: "object",
Required: []string{},
Properties: map[string]models.ToolArgProps{
"text": {Type: "string", Description: "Text content to search for"},
"selector": {Type: "string", Description: "CSS selector to search for"},
},
},
},
},
}
var toolCallRE = regexp.MustCompile(`__tool_call__(.+?)__tool_call__`)
type ParsedToolCall struct {
ID string
Name string
Args map[string]string
}
func findToolCall(resp []byte) (func() []byte, string, bool) {
var genericResp map[string]interface{}
if err := json.Unmarshal(resp, &genericResp); err != nil {
return findToolCallFromText(string(resp))
}
if choices, ok := genericResp["choices"].([]interface{}); ok && len(choices) > 0 {
if firstChoice, ok := choices[0].(map[string]interface{}); ok {
if message, ok := firstChoice["message"].(map[string]interface{}); ok {
if toolCalls, ok := message["tool_calls"].([]interface{}); ok && len(toolCalls) > 0 {
return parseOpenAIToolCall(toolCalls)
}
if content, ok := message["content"].(string); ok {
return findToolCallFromText(content)
}
}
if text, ok := firstChoice["text"].(string); ok {
return findToolCallFromText(text)
}
}
}
if content, ok := genericResp["content"].(string); ok {
return findToolCallFromText(content)
}
return findToolCallFromText(string(resp))
}
func parseOpenAIToolCall(toolCalls []interface{}) (func() []byte, string, bool) {
if len(toolCalls) == 0 {
return nil, "", false
}
tc := toolCalls[0].(map[string]interface{})
id, _ := tc["id"].(string)
function, _ := tc["function"].(map[string]interface{})
name, _ := function["name"].(string)
argsStr, _ := function["arguments"].(string)
var args map[string]string
if err := json.Unmarshal([]byte(argsStr), &args); err != nil {
return func() []byte {
return []byte(fmt.Sprintf(`{"error": "failed to parse arguments: %v"}`, err))
}, id, true
}
return func() []byte {
fn, ok := pwToolMap[name]
if !ok {
return []byte(fmt.Sprintf(`{"error": "tool %s not found"}`, name))
}
return fn(args)
}, id, true
}
func findToolCallFromText(text string) (func() []byte, string, bool) {
jsStr := toolCallRE.FindString(text)
if jsStr == "" {
return nil, "", false
}
jsStr = strings.TrimSpace(jsStr)
jsStr = strings.TrimPrefix(jsStr, "__tool_call__")
jsStr = strings.TrimSuffix(jsStr, "__tool_call__")
jsStr = strings.TrimSpace(jsStr)
start := strings.Index(jsStr, "{")
end := strings.LastIndex(jsStr, "}")
if start == -1 || end == -1 || end <= start {
return func() []byte {
return []byte(`{"error": "no valid JSON found in tool call"}`)
}, "", true
}
jsStr = jsStr[start : end+1]
var fc models.FuncCall
if err := json.Unmarshal([]byte(jsStr), &fc); err != nil {
return func() []byte {
return []byte(fmt.Sprintf(`{"error": "failed to parse tool call: %v}`, err))
}, "", true
}
if fc.ID == "" {
fc.ID = "call_" + generateToolCallID()
}
return func() []byte {
fn, ok := pwToolMap[fc.Name]
if !ok {
return []byte(fmt.Sprintf(`{"error": "tool %s not found"}`, fc.Name))
}
return fn(fc.Args)
}, fc.ID, true
}
func generateToolCallID() string {
return strconv.Itoa(len(pwToolMap) % 10000)
}
|