| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "fmt" |
| 7 | ) |
| 8 | |
| 9 | func (e *hostBrowserExecutor) rememberFilePreview(ctx context.Context, tabID string, request FileBrowserPreviewRequest) { |
| 10 | // Optional metadata, never an authorization or a reusable preview token. |
| 11 | _ = e.call(ctx, "host/browser.preview.remember", map[string]any{"tabId": tabID, "source": request.Source, "path": request.Path, "toolCallId": request.ToolCallID}, nil) |
| 12 | } |
| 13 | |
| 14 | func (e *tabBrowserExecutor) BrowserCapability(ctx context.Context, capability string, args json.RawMessage) (json.RawMessage, error) { |
| 15 | current, err := e.current() |
| 16 | if err != nil { |
| 17 | return nil, err |
| 18 | } |
| 19 | return current.BrowserCapability(ctx, capability, args) |
| 20 | } |
| 21 | |
| 22 | func (e *hostBrowserExecutor) BrowserCapability(ctx context.Context, capability string, args json.RawMessage) (json.RawMessage, error) { |
| 23 | var supported struct { |
| 24 | Capabilities map[string]bool `json:"capabilities"` |
| 25 | } |
| 26 | if err := e.call(ctx, "host/browser.capabilities", nil, &supported); err != nil { |
| 27 | return nil, fmt.Errorf("capability_unsupported: %s: %w", capability, err) |
| 28 | } |
| 29 | if !supported.Capabilities[capability] { |
| 30 | return nil, fmt.Errorf("capability_unsupported: %s", capability) |
| 31 | } |
| 32 | var params map[string]any |
| 33 | if err := json.Unmarshal(args, ¶ms); err != nil { |
| 34 | return nil, err |
| 35 | } |
| 36 | var out json.RawMessage |
| 37 | var err error |
| 38 | action, _ := params["action"].(string) |
| 39 | if capability == "record" && action == "start" { |
| 40 | directory, err := e.captureDir() |
| 41 | if err != nil { |
| 42 | return nil, err |
| 43 | } |
| 44 | params["directory"] = directory |
| 45 | } |
| 46 | switch capability { |
| 47 | case "query", "wait", "diagnostics": |
| 48 | err = e.call(ctx, "host/browser."+capability, params, &out) |
| 49 | case "viewport", "pointer", "record": |
| 50 | if action == "get" || action == "status" { |
| 51 | err = e.call(ctx, "host/browser."+capability, params, &out) |
| 52 | } else { |
| 53 | id, _ := params["operationId"].(string) |
| 54 | tab, _ := params["tabId"].(string) |
| 55 | err = e.write(ctx, id, capability+":"+action, tab, args, "host/browser."+capability, params, &out) |
| 56 | } |
| 57 | default: |
| 58 | return nil, fmt.Errorf("capability_unsupported: %s", capability) |
| 59 | } |
| 60 | return out, err |
| 61 | } |
| 62 |