package tools import ( "context" "encoding/json" "fmt" "io" "net/http" llm "gitea.stevedudenhoeffer.com/steve/go-llm/v2" ) // BrowserParams defines parameters for the browser tool. type BrowserParams struct { URL string `json:"url" description:"The URL to fetch and extract text from"` } // Browser creates a simple web content fetcher tool. // It fetches a URL and returns the text content. // // For a full headless browser, consider using an MCP server like Playwright MCP. // // Example: // // tools := llm.NewToolBox(tools.Browser()) func Browser() llm.Tool { return llm.Define[BrowserParams]("browser", "Fetch a web page and return its text content", func(ctx context.Context, p BrowserParams) (string, error) { req, err := http.NewRequestWithContext(ctx, http.MethodGet, p.URL, nil) if err != nil { return "", fmt.Errorf("creating request: %w", err) } req.Header.Set("User-Agent", "go-llm/2.0 (Web Fetcher)") resp, err := http.DefaultClient.Do(req) if err != nil { return "", fmt.Errorf("fetching URL: %w", err) } defer resp.Body.Close() // Limit to 1MB limited := io.LimitReader(resp.Body, 1<<20) body, err := io.ReadAll(limited) if err != nil { return "", fmt.Errorf("reading body: %w", err) } result := map[string]any{ "url": p.URL, "status": resp.StatusCode, "content_type": resp.Header.Get("Content-Type"), "body": string(body), } out, _ := json.MarshalIndent(result, "", " ") return string(out), nil }, ) }