#!/usr/bin/env python3
"""Run against each checkout's fresh dev server (T3CODE_DEV_SEED=0 vp run dev).

Install omo-ai@5.1.62 separately and configure its provider credentials.
Set PROOF_MODEL to an available low-cost model; no model identifier is stored here.
Pass absolute paths for --checkout, --base-dir, --workspace, --pi-executable.
Capture the returned thread in the real web client at 1280x800, light theme.
"""
import argparse
import json
import os
from pathlib import Path
import subprocess

p = argparse.ArgumentParser(description=__doc__)
for flag in ('checkout', 'base-dir', 'workspace', 'pi-executable'):
    p.add_argument('--' + flag, required=True)
a = p.parse_args()
model = os.environ['PROOF_MODEL']
checkout, base, workspace = map(Path, (a.checkout, a.base_dir, a.workspace))
workspace.mkdir(parents=True, exist_ok=True)
if not (workspace / '.git').exists():
    subprocess.run(['git', 'init', str(workspace)], check=True, capture_output=True)
settings_path = base / 'userdata/settings.json'
settings = json.loads(settings_path.read_text()) if settings_path.exists() else {}
settings.setdefault('providerInstances', {})['pi'] = {
    'driver': 'pi', 'displayName': 'OmO Native',
    'config': {'enabled': True, 'binaryPath': a.pi_executable},
}
settings['enableAgentBrowserAccess'] = True
settings_path.parent.mkdir(parents=True, exist_ok=True)
settings_path.write_text(json.dumps(settings, indent=2) + '\n')
cli = ['node', str(checkout / 'apps/server/src/bin.ts')]
subprocess.run(cli + ['project', 'add', '--base-dir', str(base), '--title',
                      'Tool search reproduction', str(workspace)],
               cwd=checkout, check=True, capture_output=True)
prompt = Path(__file__).with_name('prompt.txt').read_text()
r = subprocess.run(cli + ['thread', 'start', '--base-dir', str(base),
                         '--provider', 'pi', '--model', model,
                         '--title', 'Builtin tool search', '--json',
                         str(workspace), '-'], input=prompt, text=True,
                   cwd=checkout, check=True, capture_output=True)
print(r.stdout, end='')
