File size: 2,262 Bytes
9936912
 
 
 
 
 
 
48ee375
9936912
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48ee375
 
 
9936912
 
 
 
 
 
 
 
 
 
48ee375
 
 
 
9936912
 
 
 
 
 
 
 
 
 
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
"""Local document retrieval tool for ControlAI Agent."""

from __future__ import annotations

from typing import Any

from controlai_agent.registry import registry
from controlai_rag.index import get_shared_index


@registry.register(
    name="search_control_references",
    description="Search local engineering textbooks, MathWorks manuals, MIT/Stanford notes, and user-provided documentation for control theory theorems, formulas, or syntax.",
    parameters_schema={
        "type": "object",
        "properties": {
            "query": {
                "type": "string",
                "description": "Keywords or concept phrase (e.g., 'Discrete Algebraic Riccati Equation CARE vs DARE', 'LQR robustness gain margin')",
            },
            "top_k": {
                "type": "integer",
                "minimum": 1,
                "maximum": 10,
                "default": 3,
                "description": "Number of reference passages to retrieve",
            },
            "source_filter": {
                "type": "string",
                "description": "Optional substring filter on source file path",
            },
        },
        "required": ["query"],
    },
)
def search_control_references(
    query: str,
    top_k: int = 3,
    source_filter: str | None = None,
) -> dict[str, Any]:
    # Resolved per call (not at import) so newly uploaded documents are visible
    # immediately, without restarting the server.
    hits = get_shared_index().search(query=query, top_k=top_k, source_filter=source_filter)
    if not hits:
        return {
            "query": query,
            "results_found": 0,
            "message": "No matching reference passages found in local index.",
        }

    formatted_passages = []
    for hit in hits:
        formatted_passages.append({
            "citation": (
                f"[{hit.get('source_name') or hit['filename']}"
                + (f", p. {hit['page']}]" if hit.get("page") else "]")
            ),
            "source_path": hit["source"],
            "relevance_score": hit["score"],
            "content": hit["text"][:600],
        })

    return {
        "query": query,
        "results_found": len(formatted_passages),
        "passages": formatted_passages,
    }