File size: 7,753 Bytes
068bc7f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
"""WebSearchTool β€” search the web via DuckDuckGo."""

from __future__ import annotations

import json
import os
import time
from dataclasses import dataclass, asdict
from typing import Any

from .base import BaseTool, ToolResult
from .registry import get_registry

try:
    from ddgs import DDGS
except ImportError:
    DDGS = None  # type: ignore


TOOL_NAME = "WebSearch"
DATA_DIR = os.path.expanduser("~/.stack-2.9")
CACHE_FILE = os.path.join(DATA_DIR, "web_search_cache.json")


def _load_cache() -> dict[str, Any]:
    """Load the web search result cache."""
    if os.path.exists(CACHE_FILE):
        try:
            with open(CACHE_FILE) as f:
                return json.load(f)
        except Exception:
            pass
    return {}


def _save_cache(cache: dict[str, Any]) -> None:
    """Persist the web search cache."""
    os.makedirs(DATA_DIR, exist_ok=True)
    with open(CACHE_FILE, "w") as f:
        json.dump(cache, f)


@dataclass
class SearchHit:
    """A single search result."""

    title: str
    url: str
    snippet: str = ""


@dataclass
class SearchOutput:
    """Output of a web search."""

    query: str
    results: list[SearchHit]
    duration_seconds: float
    source: str = "duckduckgo"


class WebSearchTool(BaseTool[dict[str, Any], SearchOutput]):
    """Search the web using DuckDuckGo.

    Requires the `ddgs` package: pip install duckduckgo-search

    Parameters
    ----------
    query : str
        The search query (required, min 2 chars).
    allowed_domains : list[str], optional
        Restrict results to these domains.
    blocked_domains : list[str], optional
        Exclude results from these domains.
    max_results : int, optional
        Maximum number of results to return (default 10, max 20).
    """

    name = TOOL_NAME
    description = "Search the web for current information using DuckDuckGo."
    search_hint = "search the web for current information"

    # ── schema ────────────────────────────────────────────────────────────────

    @property
    def input_schema(self) -> dict[str, Any]:
        return {
            "type": "object",
            "properties": {
                "query": {
                    "type": "string",
                    "description": "The search query (minimum 2 characters)",
                    "minLength": 2,
                },
                "allowed_domains": {
                    "type": "array",
                    "items": {"type": "string"},
                    "description": "Restrict results to these domains",
                },
                "blocked_domains": {
                    "type": "array",
                    "items": {"type": "string"},
                    "description": "Exclude results from these domains",
                },
                "max_results": {
                    "type": "integer",
                    "description": "Maximum number of results (default 10, max 20)",
                    "default": 10,
                    "minimum": 1,
                    "maximum": 20,
                },
            },
            "required": ["query"],
        }

    # ── validation ────────────────────────────────────────────────────────────

    def validate_input(self, input_data: dict[str, Any]) -> tuple[bool, str | None]:
        query = input_data.get("query", "")
        if not query or len(query) < 2:
            return False, "Error: query must be at least 2 characters"
        if input_data.get("allowed_domains") and input_data.get("blocked_domains"):
            return False, "Error: cannot specify both allowed_domains and blocked_domains"
        return True, None

    # ── execution ─────────────────────────────────────────────────────────────

    def execute(self, input_data: dict[str, Any]) -> ToolResult[SearchOutput]:
        if DDGS is None:
            return ToolResult(
                success=False,
                error="duckduckgo-search not installed. Run: pip install duckduckgo-search",
            )

        query = input_data["query"]
        allowed = input_data.get("allowed_domains")
        blocked = input_data.get("blocked_domains")
        max_results = min(input_data.get("max_results", 10), 20)

        cache = _load_cache()
        cache_key = f"{query}|{json.dumps(allowed)}|{json.dumps(blocked)}"

        # Return cached result if fresh (5 minutes)
        now = time.time()
        if cache_key in cache:
            entry = cache[cache_key]
            if now - entry.get("cached_at", 0) < 300:
                output = SearchOutput(
                    query=query,
                    results=[SearchHit(**h) for h in entry["results"]],
                    duration_seconds=entry.get("duration", 0),
                    source="duckduckgo (cached)",
                )
                return ToolResult(success=True, data=asdict(output))

        try:
            hits: list[SearchHit] = []
            with DDGS() as ddgs:
                if allowed:
                    keywords = " ".join(allowed)
                    generator = ddgs.text(query, max_results=max_results)
                else:
                    generator = ddgs.text(query, max_results=max_results)

                for i, result in enumerate(generator):
                    if i >= max_results:
                        break
                    url = result.get("href", "")
                    # Apply blocked domains filter
                    if blocked and any(domain in url for domain in blocked):
                        continue
                    hits.append(
                        SearchHit(
                            title=result.get("title", ""),
                            url=url,
                            snippet=result.get("body", ""),
                        )
                    )

            output = SearchOutput(
                query=query,
                results=hits,
                duration_seconds=0.0,
                source="duckduckgo",
            )

            # Cache the result
            cache[cache_key] = {
                "results": [asdict(h) for h in hits],
                "cached_at": now,
            }
            _save_cache(cache)

            return ToolResult(success=True, data=asdict(output))

        except Exception as exc:
            return ToolResult(success=False, error=f"Web search failed: {exc}")

    def map_result_to_message(self, result: SearchOutput | dict, tool_use_id: str | None = None) -> str:
        """Format search results for display."""
        if isinstance(result, dict):
            query = result.get("query", "")
            hits = result.get("results", [])
        else:
            query = result.query
            hits = result.results

        lines = [f"Web search results for: \"{query}\"\n"]
        if not hits:
            lines.append("No results found.")
            return "\n".join(lines)

        lines.append(f"{len(hits)} results:\n")
        for i, hit in enumerate(hits, 1):
            snippet = hit.snippet[:200] + "..." if len(hit.snippet) > 200 else hit.snippet
            lines.append(f"{i}. {hit.title}")
            lines.append(f"   URL: {hit.url}")
            if snippet:
                lines.append(f"   {snippet}")
            lines.append("")

        return "\n".join(lines)


# Register the tool
_registry = get_registry()
_registry.register(WebSearchTool())