File size: 2,150 Bytes
4d393e8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
`dw search` command.
Queries DocWeave's hybrid search engine over committed knowledge.
"""
from __future__ import annotations

from typing import Optional
import typer
from rich.table import Table

from diffweave.cli.config import get_active_workspace
from diffweave.cli.output import print_error, print_json, console
from diffweave.mcp.client import DiffWeaveMCPClient


def search_command(
    query: str = typer.Argument(..., help="Search text query across knowledge items"),
    top_k: int = typer.Option(10, "--top-k", "-k", help="Maximum results to return"),
    workspace_id: Optional[str] = typer.Option(None, "--workspace", "-w", help="Workspace UUID override"),
    as_json: bool = typer.Option(False, "--json", help="Output machine-readable JSON"),
):
    """
    Search the Knowledge Register using hybrid lexical + semantic search.
    """
    try:
        active_ws_id = get_active_workspace(workspace_id)
    except Exception as e:
        print_error(str(e))
        raise typer.Exit(code=1)

    client = DiffWeaveMCPClient()

    try:
        results = client.call_tool_sync("search_knowledge", {"workspace_id": active_ws_id, "query": query})
    except Exception as e:
        print_error(f"Search failed: {e}")
        raise typer.Exit(code=1)

    results = results[:top_k]

    if as_json:
        print_json(results)
        return

    if not results:
        console.print(f"[dim]No matching knowledge items found for '{query}'.[/dim]")
        return

    table = Table(title=f"Knowledge Search: \"{query}\" ({len(results)} matches)", header_style="bold green")
    table.add_column("Type", style="cyan")
    table.add_column("Title", style="bold white")
    table.add_column("Value", style="white")
    table.add_column("Confidence", style="yellow")
    table.add_column("Source File", style="dim")

    for r in results:
        table.add_row(
            r.get("type", "CLAIM"),
            r.get("title", ""),
            r.get("value", "")[:50] + ("..." if len(r.get("value", "")) > 50 else ""),
            str(r.get("confidence", 1.0)),
            r.get("filename") or "n/a",
        )

    console.print(table)