File size: 2,262 Bytes
016b413
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Web search tool adapter for Pydantic AI agents.

Adapts the folder/tools/web_search.py implementation to work with Pydantic AI.
"""

import structlog

logger = structlog.get_logger()


async def web_search(query: str) -> str:
    """
    Perform a web search for a given query and return formatted results.

    Use this tool to search the web for information relevant to the query.
    Provide a query with 3-6 words as input.

    Args:
        query: The search query (3-6 words recommended)

    Returns:
        Formatted string with search results including titles, descriptions, and URLs
    """
    try:
        # Lazy import to avoid requiring folder/ dependencies at import time
        # This will use the existing web_search tool from folder/tools
        from folder.llm_config import create_default_config
        from folder.tools.web_search import create_web_search_tool

        config = create_default_config()
        web_search_tool = create_web_search_tool(config)

        # Call the tool function
        # The tool returns List[ScrapeResult] or str
        results = await web_search_tool(query)

        if isinstance(results, str):
            # Error message returned
            logger.warning("Web search returned error", error=results)
            return results

        if not results:
            return f"No web search results found for: {query}"

        # Format results for agent consumption
        formatted = [f"Found {len(results)} web search results:\n"]
        for i, result in enumerate(results[:5], 1):  # Limit to 5 results
            formatted.append(f"{i}. **{result.title}**")
            if result.description:
                formatted.append(f"   {result.description[:200]}...")
            formatted.append(f"   URL: {result.url}")
            if result.text:
                formatted.append(f"   Content: {result.text[:300]}...")
            formatted.append("")

        return "\n".join(formatted)

    except ImportError as e:
        logger.error("Web search tool not available", error=str(e))
        return f"Web search tool not available: {e!s}"
    except Exception as e:
        logger.error("Web search failed", error=str(e), query=query)
        return f"Error performing web search: {e!s}"