File size: 4,244 Bytes
0d3f7cc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Documentation search tool implementation."""

from __future__ import annotations

import logging
from typing import Any

from hermes.tools.base.tool import BaseTool, ToolSchema

logger = logging.getLogger(__name__)


class DocumentationTool(BaseTool):
    """Tool for searching and retrieving documentation."""

    def __init__(self) -> None:
        super().__init__()
        self._doc_cache: dict[str, dict[str, Any]] = {}

    def _define_schema(self) -> ToolSchema:
        return ToolSchema(
            name="documentation_search",
            description="Search and retrieve documentation from various sources",
            parameters={
                "query": {
                    "type": "string",
                    "description": "Documentation search query",
                },
                "source": {
                    "type": "string",
                    "description": "Documentation source (python, javascript, general)",
                    "default": "general",
                },
                "max_results": {
                    "type": "integer",
                    "description": "Maximum number of results",
                    "default": 5,
                },
            },
            required=["query"],
            category="research",
            tags=["documentation", "docs", "search"],
        )

    async def execute(self, **kwargs: Any) -> dict[str, Any]:
        """Search documentation."""
        query = kwargs["query"]
        source = kwargs.get("source", "general")
        max_results = kwargs.get("max_results", 5)

        try:
            results = await self._search_docs(query, source, max_results)
            return {
                "query": query,
                "source": source,
                "results": results,
                "total": len(results),
            }
        except Exception as e:
            logger.error(f"Documentation search error: {e}")
            return {"error": str(e), "query": query, "results": []}

    async def _search_docs(

        self, query: str, source: str, max_results: int

    ) -> list[dict[str, Any]]:
        """Search documentation sources."""
        results = []

        doc_sources = {
            "python": "https://docs.python.org/3/",
            "javascript": "https://developer.mozilla.org/en-US/docs/Web/JavaScript",
            "fastapi": "https://fastapi.tiangolo.com/",
            "pydantic": "https://docs.pydantic.dev/",
        }

        base_url = doc_sources.get(source, "https://docs.python.org/3/")

        results.append(
            {
                "title": f"Documentation for: {query}",
                "url": f"{base_url}search?q={query.replace(' ', '+')}",
                "snippet": f"Search results for '{query}' in {source} documentation",
                "source": source,
            }
        )

        common_docs = {
            "fastapi": [
                {
                    "title": "FastAPI Request Body",
                    "url": "https://fastapi.tiangolo.com/tutorial/request-body/",
                    "snippet": "Request body with Pydantic models for FastAPI endpoints.",
                },
                {
                    "title": "FastAPI Dependencies",
                    "url": "https://fastapi.tiangolo.com/tutorial/dependencies/",
                    "snippet": "Dependency injection in FastAPI for shared logic.",
                },
            ],
            "pydantic": [
                {
                    "title": "Pydantic Models",
                    "url": "https://docs.pydantic.dev/latest/concepts/models/",
                    "snippet": "Data validation using Pydantic BaseModel classes.",
                },
                {
                    "title": "Pydantic Settings",
                    "url": "https://docs.pydantic.dev/latest/concepts/pydantic_settings/",
                    "snippet": "Settings management using pydantic-settings.",
                },
            ],
        }

        if source in common_docs:
            results.extend(common_docs[source][:max_results])

        return results[:max_results]