#!/usr/bin/env python3
"""Seed a fresh running T3 dev server, print the exact UI prompt, or extract its real tool reply.
Run from each checkout. Start it first with T3CODE_DEV_SEED=0 vp run dev.
UI: pair, open the printed thread, hide sidebar/details, open Browser, navigate to
https://example.com/, wait for Example Domain, click Release control, record,
paste the printed prompt as ONE message and submit. Expand the tool call to inspect.
"""
import argparse, json, sqlite3, subprocess
from pathlib import Path
p = argparse.ArgumentParser()
p.add_argument("action", choices=["seed", "prompt", "extract"])
p.add_argument("--home", default=".t3")
p.add_argument("--thread")
a = p.parse_args()
def cli(*args):
    return subprocess.check_output(["node", "apps/server/src/bin.ts", *args, "--base-dir", a.home], text=True)
prompt = 'Call preview_act exactly once with action "click", timeoutMs 500, and selector "' + 'text=x' + '🎉' * 150 + '". Copy it exactly. Do not retry or call any other tool. Then quote the tool response verbatim.'
if a.action == "prompt":
    print(prompt)
elif a.action == "seed":
    cli("project", "add", str(Path.cwd()), "--title", "Unicode click timeout")
    print(cli("thread", "start", str(Path.cwd()), "Respond Ready only.", "--title", "Unicode click timeout", "--provider", "claudeAgent", "--model", "claude-haiku-5-5", "--wait", "--json"))
else:
    c = sqlite3.connect(f"file:{a.home}/userdata/statev2.sqlite?mode=ro", uri=True)
    replies = []
    for row, in c.execute("select payload_json from orchestration_v2_projection_turn_items where thread_id=? and type='dynamic_tool'", (a.thread,)):
        d = json.loads(row)
        if d["toolName"].endswith("preview_act"):
            output = d["output"]["content"]
            target = output.split("No element matches ")[1].split(" after the timeout")[0]
            replies.append({"tool": d["toolName"], "input": d["input"], "output": output, "selectorIsExact": d["input"]["selector"] == "text=x" + "🎉" * 150, "wellFormed": not any(0xd800 <= ord(c) <= 0xdfff for c in output), "targetTailCodepoints": [hex(ord(c)) for c in target[-7:]]})
    print(json.dumps(replies, ensure_ascii=True, indent=2))
