"""TinyClaw web UI — Flask app with SSE real-time progress.""" import json import os import queue import threading from pathlib import Path from flask import Flask, Response, jsonify, render_template, request, stream_with_context from tinyclaw import history as hist from tinyclaw.driver import RunConfig, run, status as driver_status from tinyclaw.queue import ( Task, TaskAction, TaskStatus, parse_queue, write_queue, queue_summary, ) _app = Flask(__name__, template_folder="templates") # ── global state ───────────────────────────────────────────────────────────── _state = { "running": False, "queue_path": "QUEUE.md", "history_path": "HISTORY.jsonl", "model": os.environ.get("TINYCLAW_MODEL", "google/gemma-3-4b-it"), } _sse_clients: list[queue.Queue] = [] _sse_lock = threading.Lock() # ── SSE broadcast ───────────────────────────────────────────────────────────── def _broadcast(event: dict) -> None: data = json.dumps(event) with _sse_lock: dead = [] for q in _sse_clients: try: q.put_nowait(data) except queue.Full: dead.append(q) for q in dead: _sse_clients.remove(q) def _progress_cb(event: dict) -> None: _broadcast(event) # ── routes ──────────────────────────────────────────────────────────────────── @_app.get("/") def index(): return render_template("index.html") @_app.get("/api/queue") def api_queue(): qp = _state["queue_path"] if not Path(qp).exists(): return jsonify({"tasks": [], "summary": "No queue file"}) tasks = parse_queue(qp) return jsonify({ "tasks": [ { "action": t.action.value, "filepath": t.filepath, "target": t.target, "instruction": t.instruction, "status": t.status.value, "attempts": t.attempts, "error": t.error, "line": t.line_number, } for t in tasks ], "summary": queue_summary(tasks), "running": _state["running"], "model": _state["model"], }) @_app.post("/api/queue/add") def api_queue_add(): d = request.json or {} action = d.get("action", "EDIT").upper() filepath = d.get("filepath", "") target = d.get("target", "-") instruction = d.get("instruction", "") if not filepath or not instruction: return jsonify({"error": "filepath and instruction required"}), 400 if action not in ("EDIT", "CREATE", "DELETE", "TEST"): return jsonify({"error": "invalid action"}), 400 qp = _state["queue_path"] line = f'{action} {filepath} {target} "{instruction}"\n' p = Path(qp) if not p.exists(): p.write_text("# TinyClaw task queue\n") with p.open("a") as f: f.write(line) return jsonify({"ok": True, "line": line.strip()}) @_app.delete("/api/queue/") def api_queue_delete(line_number: int): qp = _state["queue_path"] tasks = parse_queue(qp) remaining = [t for t in tasks if t.line_number != line_number] if len(remaining) == len(tasks): return jsonify({"error": "task not found"}), 404 write_queue(qp, remaining) return jsonify({"ok": True}) @_app.post("/api/run") def api_run(): if _state["running"]: return jsonify({"error": "already running"}), 409 d = request.json or {} model = d.get("model", _state["model"]) _state["model"] = model def _worker(): _state["running"] = True _broadcast({"type": "run_start", "model": model}) try: cfg = RunConfig( queue_path=_state["queue_path"], history_path=_state["history_path"], model=model, dry_run=False, auto_commit=False, ) cfg._progress_cb = _progress_cb result = run(cfg) _broadcast({ "type": "run_done", "done": result.done, "failed": result.failed, "skipped": result.skipped, "cost": result.total_cost, }) except Exception as e: _broadcast({"type": "error", "msg": str(e)}) finally: _state["running"] = False threading.Thread(target=_worker, daemon=True).start() return jsonify({"ok": True}) @_app.get("/api/stream") def api_stream(): client_q: queue.Queue = queue.Queue(maxsize=100) with _sse_lock: _sse_clients.append(client_q) def _generate(): try: while True: try: data = client_q.get(timeout=15) yield f"data: {data}\n\n" except queue.Empty: yield ": ping\n\n" except GeneratorExit: pass finally: with _sse_lock: if client_q in _sse_clients: _sse_clients.remove(client_q) return Response( stream_with_context(_generate()), mimetype="text/event-stream", headers={"Cache-Control": "no-cache", "X-Accel-Buffering": "no"}, ) @_app.get("/api/history") def api_history(): entries = hist.load(_state["history_path"], limit=int(request.args.get("limit", 50))) return jsonify(entries) @_app.get("/api/models") def api_models(): """Return a small curated list of models good for TinyClaw.""" return jsonify([ {"id": "google/gemma-3-4b-it", "label": "Gemma 3 4B ($0.04/M)"}, {"id": "google/gemma-3-12b-it", "label": "Gemma 3 12B ($0.07/M)"}, {"id": "qwen/qwen2.5-coder-7b-instruct", "label": "Qwen2.5-Coder 7B ($0.09/M)"}, {"id": "meta-llama/llama-3.2-3b-instruct:free", "label": "Llama 3.2 3B (free, rate-limited)"}, {"id": "google/gemma-3-4b-it:free", "label": "Gemma 3 4B (free, rate-limited)"}, {"id": "google/gemma-3-12b-it:free", "label": "Gemma 3 12B (free, rate-limited)"}, ]) def serve( queue_path: str = "QUEUE.md", history_path: str = "HISTORY.jsonl", host: str = "127.0.0.1", port: int = 7842, debug: bool = False, ) -> None: from tinyclaw.driver import _load_dotenv _load_dotenv() _state["queue_path"] = queue_path _state["history_path"] = history_path print(f"TinyClaw UI → http://{host}:{port}") _app.run(host=host, port=port, debug=debug, threaded=True)