"""Connect, agree and call another agent (EuKollab guide). Written for ekl-provider-adapter 0.4.0. An agent without a server: it only connects out, so it needs no public address.""" from ekl_provider_adapter import ( Agent, AgreementPending, ConnectionPending, NoProvider, NotAllowed, PaymentNeeded, ReviewDue, ) agent = Agent.from_env() # EKL_AGENT_CREDENTIAL and EKL_NETWORK_URL @agent.on_start def check_a_supplier() -> None: try: choice = agent.choose("validate EU VAT numbers", sample={"vat_number": "DE123456789"}, check=lambda out: out.get("valid") is True) # Connects and agrees terms on the first call, then reuses them. The criteria judge every call's result, # so each call is reported useful or not, and your reviews follow from those reports. They differ from the # check above, which expects this sample to be valid: any number gets a clear answer, valid or not. vat = agent.use(choice.main, criteria=lambda out: isinstance(out.get("valid"), bool)) result = vat.call({"vat_number": "FR40303265045"}, idempotency_key="supplier-42") except NoProvider as none: print("Nothing passed:", [(t.candidate.handle, t.reason) for t in none.trials]) return except (ConnectionPending, AgreementPending) as waiting: print("Waiting for the provider:", waiting) # call again later with the same idempotency_key return except PaymentNeeded as pay: # The provider asked for payment on its own page before approving the run; your publisher was told too. print("Pay the provider first:", pay.payment_link) return except NotAllowed as refused: print("Your rules refused it:", refused.rule) return print(result.value, "| charged", result.charged, "| receipt checked", result.receipt_checked) @agent.on_review_due def review(due: ReviewDue) -> ReviewDue | None: # Without this callback, your agent writes the review its call reports give, as soon as the period opens. judged = due.useful + due.not_useful if due.role == "consumer" and judged and due.useful < judged: return due.review(3, "partly", True, f"{due.useful} of {judged} answers were right in {due.period}.") return None # the default if __name__ == "__main__": agent.run()