Ctrl K
ComunidadGuías…

scripts/manual_search_demo.py

Rama master · réplica de lectura del repositorio original.

Archivo original ↗Preguntar a KiBot
"""Manual demo for the web search plugin."""

from __future__ import annotations

# ruff: noqa: E402
import os
import sys

_REPO_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
if _REPO_ROOT not in sys.path:
    sys.path.insert(0, _REPO_ROOT)

# Load .env before accessing any environment variables
from utils.env_loader import load as _load_env

_load_env()

import argparse

from plugins.web_search import search_web


def run_search(query: str) -> None:
    if not query.strip():
        print("⚠️  Empty query. Please type something meaningful.")
        return

    try:
        result = search_web(query)
    except Exception as e:
        print(f"❌ Search failed: {e}")
        return

    print("\n--- Result ---")
    if result:
        print(result)
    else:
        print("⚠️  No result found or search failed.")


def interactive_loop() -> None:
    print("🔎 Rex Web Search Plugin Demo (type 'exit' to quit)\n")
    while True:
        try:
            query = input("Enter your search query: ").strip()
            if query.lower() in {"exit", "quit"}:
                print("👋 Exiting search demo.")
                break
            run_search(query)
        except KeyboardInterrupt:
            print("\n👋 Exiting search demo.")
            break


def main() -> None:
    parser = argparse.ArgumentParser(description="Rex Web Search CLI Tester")
    parser.add_argument("--query", "-q", type=str, help="Search query (non-interactive mode)")
    args = parser.parse_args()

    if args.query:
        run_search(args.query)
    else:
        interactive_loop()


if __name__ == "__main__":
    main()