"""Run against an already running, isolated T3 dev server; no copied database.
Usage: python3 drive.py CHECKOUT PROJECT_PATH [mode]
Enable OpenCode 2.2.5 in Settings > Providers first. The project must be added.
Open the returned thread link in the real web client to inspect the result.
"""
import subprocess,sys,json
from pathlib import Path
checkout,project=sys.argv[1:3]
mode=sys.argv[3] if len(sys.argv)>3 else 'done'
original='Show me a small HTML bar chart of 4 made-up fruit sales numbers. Call html_preview and then html_render in ONE execute script, and make that script return only the string "done". Do not call html_render outside that script.'
html='''<style>h2{font-size:20px;margin:8px 0 16px}.row{display:flex;align-items:center;gap:12px;margin:10px 0}.name{width:65px}.bar{height:22px;background:var(--chart-1)}.value{font-variant-numeric:tabular-nums}</style><h2>Fruit sales</h2><div class="row"><span class="name">Apples</span><div class="bar" style="width:240px"></div><span class="value">120</span></div><div class="row"><span class="name">Bananas</span><div class="bar" style="width:160px;background:var(--chart-2)"></div><span class="value">80</span></div><div class="row"><span class="name">Oranges</span><div class="bar" style="width:200px;background:var(--chart-3)"></div><span class="value">100</span></div><div class="row"><span class="name">Grapes</span><div class="bar" style="width:100px;background:var(--chart-4)"></div><span class="value">50</span></div>'''
ending={'done':'return "done";','nothing':'Do not include any return statement after awaiting html_render. The execute script must return nothing.','unrelated':'Your final statement MUST be exactly return {status: "complete", count: 4}; Return ONLY those two keys. Discard ALL tool results, and do not include render, htmlRender, preview, or any reference in the returned object.','reference':'Assign the html_render result to result. Your final statement MUST be exactly return result; Do not wrap it, and do not return preview, height, or other keys.','repeated':'Call html_render twice consecutively with EXACTLY THE SAME arguments (same html string, title and height) within that script. Discard both results and return "done";'}
prompt=original if mode=='original' else f'''Show this exact small fruit sales chart. Use ONE execute script to call html_preview then html_render. Use the exact HTML below unchanged; publish exactly {'twice' if mode=='repeated' else 'once'} and do not revise it. Title: Fruit sales — {mode}. Use the preview contentHeight. {ending[mode]} Do not call html_render outside that script. End your final reply with a short statement of the script return value.\nHTML:\n{html}'''
args=['node','apps/server/src/bin.ts','thread','start',project,prompt,'--provider','opencode','--model','opencode/deepseek-v4.1-flash-free','--title',f'Fruit sales — {mode}','--base-dir','.t3','--json']
r=subprocess.run(args,cwd=checkout,capture_output=True,text=True,check=True)
print(r.stdout,end='')
