File size: 2,515 Bytes
ef26eb0
 
 
 
 
 
 
 
 
 
c00f902
ef26eb0
b4020af
6553c90
 
 
c00f902
8223481
 
6553c90
 
 
 
c00f902
6553c90
 
 
c00f902
6553c90
c00f902
 
6553c90
 
 
 
c00f902
 
6553c90
 
c00f902
504d406
6553c90
504d406
 
 
c00f902
504d406
 
 
 
 
c00f902
504d406
 
 
 
c00f902
8223481
6553c90
 
 
c00f902
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
from typing import Any, Optional
from smolagents.tools import Tool
import requests
import markdownify
import smolagents
import playwright.sync_api as playwright
import re
import yaml
from smolagents import CodeAgent, HfApiModel
from tools.final_answer import FinalAnswerTool
import json

class getDeviceInfo(Tool):
    name = "visit_webpage"
    description = "Visits a webpage at the given URL, reads its content as a markdown string, and extracts device information."
    inputs = {'url': {'type': 'string', 'description': 'The URL of the webpage to visit.'}}
    output_type = "string"  # ✅ Fix: Use "string" instead of "json"

    def forward(self, url: str) -> str:  # ✅ Return a JSON string
        try:
            from smolagents.utils import truncate_content
        except ImportError as e:
            raise ImportError(
                "You must install the `smolagents` package to run this tool."
            ) from e
        
        try:
            # Fetch the webpage
            response = requests.get(url, timeout=20)
            response.raise_for_status()
            markdown_content = markdownify.markdownify(response.text).strip()
            markdown_content = re.sub(r"\n{3,}", "\n\n", markdown_content)
            webpage_content = truncate_content(markdown_content, 10000)
        
        except requests.exceptions.Timeout:
            webpage_content = "The request timed out."
        except requests.exceptions.RequestException as e:
            webpage_content = f"Error fetching the webpage: {str(e)}"
        except Exception as e:
            webpage_content = f"Unexpected error: {str(e)}"
        
        # Extract device information using Playwright
        with playwright.sync_api.sync_playwright() as p:
            browser = p.chromium.launch(headless=True)
            page = browser.new_page()
            script = """() => ({
                userAgent: navigator.userAgent,
                platform: navigator.platform,
                language: navigator.language,
                screenWidth: window.screen.width,
                screenHeight: window.screen.height
            })"""
            page.goto("about:blank")
            device_info = page.evaluate(script)
            browser.close()
        
        # ✅ Convert output to JSON string for compatibility
        result = {
            "webpage_content": webpage_content,
            "device_info": device_info
        }
        return json.dumps(result)  # ✅ Ensure return type matches "string"