"""Agent-loop smoke test: weather / math / web search through the real brain + tools."""
import sys, json, time
sys.path.insert(0, r"C:\oracle\phase0")
import server  # loads whisper+silero at import; that's fine

def agent_loop(question, max_passes=2):
    msgs = [{"role": "system", "content": server.PERSONA},
            {"role": "user", "content": question}]
    for _pass in range(max_passes):
        msg = server.llm_respond(msgs, server.TOOLS)
        content = (msg.get("content") or "").strip()
        tcs = msg.get("tool_calls")
        if not tcs:
            return content
        msgs.append({"role": "assistant", "content": content or None, "tool_calls": tcs})
        for i, tc in enumerate(tcs):
            fn = (tc.get("function") or {}).get("name", "?")
            try: args = json.loads((tc.get("function") or {}).get("arguments") or "{}")
            except Exception: args = {}
            print(f"   -> tool {fn}({json.dumps(args)[:90]})", flush=True)
            res = server.run_tool(fn, args)
            print(f"   <- {res[:150]}", flush=True)
            msgs.append({"role": "tool", "tool_call_id": tc.get("id") or f"call_{i}", "content": res})
    return "(no answer)"

for q in ["What is the weather in Oceanside California today?",
          "what is 137 times 89?",
          "Who won the most recent super bowl?",
          "What is today's date?",
          "What time is it in London right now?",
          "how many miles is 42 kilometers?",
          "convert 98.6 degrees Fahrenheit to Celsius"]:
    print(f"\n=== Q: {q}", flush=True)
    t0 = time.time()
    ans = agent_loop(q)
    print(f"=== A ({time.time()-t0:.1f}s): {ans[:400]}", flush=True)
