import json
from localmark_client import LocalMarkClient, LocalMarkError

client = LocalMarkClient(timeout=30)
policy = {
    "q": "extract text from PDF documents",
    "checked_within_hours": 24,
    "a2a_response_within_hours": 24,
    "tls_reachable": True,
    "limit": 3,
}
try:
    shortlist = client.recommend(policy=policy)
    print("Matches:", shortlist["total_matches"])
    for agent in shortlist["items"]:
        plan = client.connection_plan(agent["id"])
        print(json.dumps({"name": agent["name"],
            "reasons": agent["recommendation"]["reasons"],
            "interfaces": plan["interfaces"],
            "authentication": plan["authentication"]}, ensure_ascii=True))
    if not shortlist["items"]:
        print("No recorded candidate meets this policy. Keep the requirements explicit.")
except LocalMarkError as error:
    raise SystemExit("LocalMark request failed: %s; retry after %s seconds" %
                     (error, error.retry_after or "the documented backoff"))
# Reading results and connection plans does not run an agent task.
