(ctx context.Context, args map[string]interface{})
| 41 | } |
| 42 | |
| 43 | func executeWebFetch(ctx context.Context, args map[string]interface{}) (*ToolResult, error) { |
| 44 | rawURL, ok := args["url"].(string) |
| 45 | if !ok || strings.TrimSpace(rawURL) == "" { |
| 46 | return nil, fmt.Errorf("missing url argument") |
| 47 | } |
| 48 | rawURL = strings.TrimSpace(rawURL) |
| 49 | |
| 50 | extractMode := defaultWebFetchExtractMode |
| 51 | if mode, ok := args["extractMode"].(string); ok { |
| 52 | mode = strings.TrimSpace(strings.ToLower(mode)) |
| 53 | if mode == "text" || mode == "markdown" { |
| 54 | extractMode = mode |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | maxChars := defaultWebFetchMaxChars |
| 59 | if v, exists := args["maxChars"]; exists { |
| 60 | maxChars = parseIntValue(v, defaultWebFetchMaxChars) |
| 61 | if maxChars < 256 { |
| 62 | maxChars = 256 |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | parsed, err := url.Parse(rawURL) |
| 67 | if err != nil { |
| 68 | return nil, fmt.Errorf("invalid url: %w", err) |
| 69 | } |
| 70 | if parsed.Scheme != "http" && parsed.Scheme != "https" { |
| 71 | return nil, fmt.Errorf("only http and https URLs are supported") |
| 72 | } |
| 73 | if strings.TrimSpace(parsed.Hostname()) == "" { |
| 74 | return nil, fmt.Errorf("missing hostname in url") |
| 75 | } |
| 76 | |
| 77 | proxyURL := webFetchProxyURLForRequest(parsed) |
| 78 | proxyAware := proxyURL != nil |
| 79 | if !proxyAware { |
| 80 | if err := checkWebFetchSSRF(ctx, parsed.Hostname(), defaultWebFetchResolver); err != nil { |
| 81 | return nil, err |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | result, err := fetchWebContent(ctx, rawURL, proxyURL) |
| 86 | if err != nil { |
| 87 | return nil, err |
| 88 | } |
| 89 | |
| 90 | content := result.Content |
| 91 | if strings.Contains(result.ContentType, "text/html") { |
| 92 | content = extractTextFromHTML(result.Content) |
| 93 | } |
| 94 | |
| 95 | if !utf8.ValidString(content) { |
| 96 | content = webFetchBinaryPreviewPrefix |
| 97 | } |
| 98 | if extractMode == "markdown" { |
| 99 | content = normalizeWebFetchText(content) |
| 100 | } |
no test coverage detected