File size: 2,755 Bytes
8086c56
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
"""Run suite tasks (by name filter) with a per-step trace: operation, target label, typed text, page change, url.
    python apps/browser_trace.py [name-filter] [max_steps=12]
"""
import json, os, sys, time
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from browser_suite import TASKS, Agent
flt = sys.argv[1] if len(sys.argv) > 1 else ""; max_steps = int(sys.argv[2]) if len(sys.argv) > 2 else 12
for name, url, goal, check in TASKS:
    if flt and flt not in name.split(",") and flt not in name: continue
    t0 = time.time(); page = None; status = "error"; err = ""
    print(f"\n### {name}: {goal}", flush=True)
    try:
        with Agent(url, goal) as agent:
            page = agent.state["page"]
            print(f"  start: {len(page['actions'])} actions, title={page['title'][:50]!r}", flush=True)
            for state in agent.run():
                status = state["status"]; page = state["page"]
                h = state["history"][-1] if state["history"] else None
                d = state["decisions"][-1] if state["decisions"] else None
                if state.get("plan") and len(state["plan"]) > 1 and state.get("_shown_plan") is None:
                    state["_shown_plan"] = 1; print("  plan:", state["plan"], flush=True)
                if state.get("subgoals_done") and state.get("_shown_sg") != state["subgoals_done"]:
                    state["_shown_sg"] = state["subgoals_done"]; print(f"  -- sub-goal {state['subgoals_done']} done, now: {state['goal']!r}", flush=True)
                if h and d and h["step"] == len(state["history"]):
                    tp = d.get("target_probabilities") or {}
                    top = sorted(tp.items(), key=lambda kv: -kv[1])[:3]
                    print(f"  step {h['step']:2d} {d['operation']:10s} p_op={d['operation_probabilities'].get(d['operation'],0):.2f} "
                          f"target={h['action'][:60]!r} text={h['text']!r} changed={h['page_changed']} -> {page['url'][:80]}", flush=True)
                if len(state["history"]) >= max_steps: break
            if status in ("done", "blocked"):
                d = state["decisions"][-1] if state["decisions"] else None
                print(f"  final decision: {status} p={d['operation_probabilities'].get(d['operation'],0) if d else 0:.2f}", flush=True)
            time.sleep(1.5)
            try: page = agent.browser.observe(screenshot=False)
            except Exception: pass
    except Exception as e:
        status = f"error:{type(e).__name__}"; err = str(e)[:200]
    ok = bool(page and check(page["url"], page["title"], page.get("text", "")))
    print(f"  => {'PASS' if ok else 'FAIL'} status={status} {err} url={(page or {}).get('url','')[:90]} wall={time.time()-t0:.1f}s", flush=True)