"""A scriptable LlmGateway for API-level tests. Safe to call from any thread's event loop.""" import asyncio import threading from estimator.domain.llm import LlmResult, Usage from tests.unit.agent.conftest import ( clarify_out, critic_out, decompose_out, estimate_out, ) DEFAULTS = { "clarify": clarify_out, "decompose": decompose_out, "estimate": estimate_out, "critic": critic_out, } class AutoGateway: """Answers every step with the worked example from docs/. `script[step]` is consumed first (items may be outputs or exceptions); `gate` blocks every call until released.""" def __init__(self, **script): self.script = {k: list(v) for k, v in script.items()} self.calls: list[dict] = [] self.delay = 0.0 self.gate: threading.Event | None = None self._lock = threading.Lock() def block(self) -> threading.Event: self.gate = threading.Event() return self.gate async def structured(self, step, *, system, user, schema): with self._lock: self.calls.append({"step": step, "user": user}) queue = self.script.get(step) or [] item = queue.pop(0) if queue else DEFAULTS[step]() if self.gate is not None: while not self.gate.is_set(): await asyncio.sleep(0.01) if self.delay: await asyncio.sleep(self.delay) if isinstance(item, Exception): raise item assert isinstance(item, schema) return LlmResult(item, Usage(10, 5), "fake", f"fake-{step}") def steps(self) -> list[str]: return [c["step"] for c in self.calls]