Skip to content

Automation foundation model for tiny devices: 2-bit, 8-29 MB, tool calls, ASR, structured extraction and embeddings on phones, wearables, smart homes, robots, cars and microcontrollers.

ui

jakmroe7eb34a8bbea
diff --git a/pyproject.toml b/pyproject.toml
index c14683d..99eef8b 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -15,6 +15,7 @@ dependencies = [
"scipy",
"gcsfs",
"tqdm",
+ "google-genai",
]
[project.optional-dependencies]
diff --git a/requirements.txt b/requirements.txt
index ec5f4fb..a84051c 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -9,5 +9,6 @@ wandb
pyyaml
sentencepiece
google-cloud-texttospeech
+google-genai
audiomentations
gcsfs
\ No newline at end of file
diff --git a/src/cli.py b/src/cli.py
index f866549..026ec30 100644
--- a/src/cli.py
+++ b/src/cli.py
@@ -247,6 +247,11 @@ def main():
choices=["wikitext2", "lambada", "hellaswag", "arc_easy"])
p.add_argument("--max-samples", type=int, default=500)
+ p = sub.add_parser("ui", add_help=False)
+ p.add_argument("--checkpoint", type=str, required=True)
+ p.add_argument("--port", type=int, default=7860)
+ p.add_argument("--host", type=str, default="127.0.0.1")
+
p = sub.add_parser("tpu", add_help=False)
tpu_sub = p.add_subparsers(dest="tpu_action")
@@ -337,6 +342,9 @@ def main():
elif args.command == "evaluate":
from .evaluate import main as eval_main
eval_main(args)
+ elif args.command == "ui":
+ from .ui.server import main as ui_main
+ ui_main(args)
elif args.command == "tpu":
from .tpu import tpu_dispatch
tpu_dispatch(args)
diff --git a/src/finetune.py b/src/finetune.py
new file mode 100644
index 0000000..6b111b9
--- /dev/null
+++ b/src/finetune.py
@@ -0,0 +1,359 @@
+import argparse
+import glob
+import json
+import os
+import random
+import shutil
+import tempfile
+import time
+
+from . import data as data_mod
+from .data import _cache_key, _save_cache_metadata, get_tokenizer, pack_sequences, prepare_tool_call_pairs
+
+
+def _write_split(split, examples, tokenizer, cache_dir, max_enc_len, max_dec_len):
+ from datasets import Dataset
+
+ ds = Dataset.from_list(examples)
+ enc_vl, dec_in_vl, dec_tgt_vl, loss_vl, kept, _ = prepare_tool_call_pairs(
+ ds, tokenizer, max_enc_len=max_enc_len, max_dec_len=max_dec_len, shuffle_tools=True,
+ )
+ # Cache key weights must match prepare_tool_call_pairs (1.0)
+ cache_id = _cache_key("toolcall", len(ds), max_enc_len, max_dec_len,
+ 1.0, 1.0, 1.0, True)
+ cache_path = os.path.join(cache_dir, cache_id)
+ pack_sequences(cache_path, enc_vl, dec_in_vl, dec_tgt_vl, loss_vl)
+ _save_cache_metadata(split, cache_id, len(kept), max_enc_len, max_dec_len, 256)
+ return len(kept)
+
+
+def _ensure_best_checkpoint(checkpoint_dir, run_id):
+ """Find or promote the best checkpoint scoped to this run's unique ID."""
+ prefix = f"needle_finetuned_{run_id}_"
+ best = sorted(
+ p for p in glob.glob(os.path.join(checkpoint_dir, f"{prefix}*_best.pkl"))
+ )
+ if best:
+ return best[-1]
+
+ candidates = sorted(
+ p for p in glob.glob(os.path.join(checkpoint_dir, f"{prefix}*.pkl"))
+ if not p.endswith("_best.pkl")
+ )
+ if not candidates:
+ return None
+
+ latest = candidates[-1]
+ best_name = os.path.basename(latest).rsplit("_", 1)[0] + "_best.pkl"
+ best_path = os.path.join(checkpoint_dir, best_name)
+ shutil.copy2(latest, best_path)
+ print(f"Promoted final checkpoint to best: {best_path}")
+ return best_path
+
+
+def _call_key(c):
+ """Canonical key for a tool call (name + sorted arguments). Matches train.py."""
+ if not isinstance(c, dict):
+ return None
+ return json.dumps({"name": c.get("name"), "arguments": c.get("arguments")}, sort_keys=True)
+
+
+def _quick_tool_eval(model, params, tokenizer, examples, max_gen_len=512, max_enc_len=1024):
+ """Tool-call eval matching needle's F1 methodology (TP/FP/FN)."""
+ from .run import generate_batch
+
+ samples = [ex for ex in examples
+ if ex.get("answers", "").strip() not in ("", "[]")]
+ if not samples:
+ return {}
+
+ _BATCH = 32
+ all_preds = []
+ for i in range(0, len(samples), _BATCH):
+ chunk = samples[i:i + _BATCH]
+ all_preds.extend(generate_batch(
+ model, params, tokenizer,
+ [s["query"] for s in chunk],
+ [s["tools"] for s in chunk],
+ max_gen_len=max_gen_len, max_enc_len=max_enc_len, constrained=True,
+ ))
+
+ n = len(samples)
+ parse_ok, exact = 0, 0
+ name_tp, name_fp, name_fn = 0, 0, 0
+ call_tp, call_fp, call_fn = 0, 0, 0
+ args_correct, args_total = 0, 0
+ per_tool = {}
+
+ for ex, pred_text in zip(samples, all_preds):
+ pred_text = pred_text.strip()
+ ref_text = ex["answers"].strip()
+ try:
+ ref_calls = json.loads(ref_text)
+ except (ValueError, TypeError):
+ ref_calls = []
+ try:
+ pred_calls = json.loads(pred_text)
+ if not isinstance(pred_calls, list):
+ pred_calls = [pred_calls] if isinstance(pred_calls, dict) else []
+ parse_ok += 1
+ except (ValueError, TypeError):
+ pred_calls = []
+
+ ref_keys = sorted(_call_key(c) for c in ref_calls if _call_key(c))
+ pred_keys = sorted(_call_key(c) for c in pred_calls if _call_key(c))
+ if ref_keys == pred_keys and len(ref_keys) == len(ref_calls) and len(pred_keys) == len(pred_calls):
+ exact += 1
+
+ ref_names = {c["name"] for c in ref_calls if isinstance(c, dict) and "name" in c}
+ pred_names = {c["name"] for c in pred_calls if isinstance(c, dict) and "name" in c}
+ name_tp += len(pred_names & ref_names)
+ name_fp += len(pred_names - ref_names)
+ name_fn += len(ref_names - pred_names)
+
+ rk = {_call_key(c) for c in ref_calls} - {None}
+ pk = {_call_key(c) for c in pred_calls} - {None}
+ call_tp += len(pk & rk)
+ call_fp += len(pk - rk)
+ call_fn += len(rk - pk)
+
+ ref_by_name = {}
+ for c in ref_calls:
+ if isinstance(c, dict) and "name" in c:
+ ref_by_name.setdefault(c["name"], []).append(c.get("arguments", {}))
+ for c in pred_calls:
+ if isinstance(c, dict) and "name" in c and c["name"] in ref_by_name:
+ args_total += 1
+ pred_args = json.dumps(c.get("arguments", {}), sort_keys=True)
+ if any(pred_args == json.dumps(ra, sort_keys=True) for ra in ref_by_name[c["name"]]):
+ args_correct += 1
+
+ for c in ref_calls:
+ if not isinstance(c, dict) or "name" not in c:
+ continue
+ rname = c["name"]
+ rargs = json.dumps(c.get("arguments", {}), sort_keys=True)
+ if rname not in per_tool:
+ per_tool[rname] = {"correct": 0, "total": 0}
+ per_tool[rname]["total"] += 1
+ for pc in pred_calls:
+ if isinstance(pc, dict) and pc.get("name") == rname:
+ if json.dumps(pc.get("arguments", {}), sort_keys=True) == rargs:
+ per_tool[rname]["correct"] += 1
+ break
+
+ name_p = name_tp + name_fp
+ name_r = name_tp + name_fn
+ call_p = call_tp + call_fp
+ call_r = call_tp + call_fn
+
+ return {
+ "call_f1": round(2 * call_tp / max(call_p + call_r, 1), 4),
+ "name_f1": round(2 * name_tp / max(name_p + name_r, 1), 4),
+ "exact_match": round(exact / max(n, 1), 4),
+ "parse_rate": round(parse_ok / max(n, 1), 4),
+ "args_acc": round(args_correct / max(args_total, 1), 4),
+ "n": n,
+ "per_tool": per_tool,
+ }
+
+
+def _per_tool_split(examples, val_per_tool=10, test_per_tool=10, seed=42):
+ """Split examples per tool: remainder->train, val_per_tool->val, test_per_tool->test.
+
+ Each tool gets exactly val_per_tool examples in val and test_per_tool in test.
+ If a tool has fewer than val_per_tool + test_per_tool + 1, the split is
+ proportional (at least 1 in each when possible).
+ """
+ rng = random.Random(seed)
+
+ # Group by primary tool (first tool in answers)
+ tool_buckets = {}
+ for i, ex in enumerate(examples):
+ try:
+ calls = json.loads(ex.get("answers", "[]"))
+ except (ValueError, TypeError):
+ calls = []
+ names = [c["name"] for c in calls if isinstance(c, dict) and "name" in c]
+ primary = names[0] if names else "__no_tool__"
+ tool_buckets.setdefault(primary, []).append(i)
+
+ train_idx, val_idx, test_idx = [], [], []
+ for tool, indices in tool_buckets.items():
+ rng.shuffle(indices)
+ n = len(indices)
+ needed = val_per_tool + test_per_tool
+ if n < needed:
+ if n == 1:
+ n_test, n_val, n_train = 1, 0, 0
+ elif n == 2:
+ n_test, n_val, n_train = 1, 1, 0
+ else:
+ n_test = max(1, n // 3)
+ n_val = max(1, (n - n_test) // 3)
+ n_train = n - n_val - n_test
+ else:
+ n_test = test_per_tool
+ n_val = val_per_tool
+ n_train = n - n_val - n_test
+
+ test_idx.extend(indices[:n_test])
+ val_idx.extend(indices[n_test:n_test + n_val])
+ train_idx.extend(indices[n_test + n_val:])
+
+ train = [examples[i] for i in train_idx]
+ val = [examples[i] for i in val_idx]
+ test = [examples[i] for i in test_idx]
+ return train, val, test
+
+
+def _emit(tag, data):
+ print(f"{tag}:{json.dumps(data)}", flush=True)
+
+
+def finetune_local(args):
+ if not args.checkpoint:
+ raise ValueError("--checkpoint is required: finetune needs a base model to start from")
+
+ with open(args.jsonl_path) as f:
+ examples = [json.loads(line) for line in f if line.strip()]
+
+ print(f"Loaded {len(examples)} examples from {args.jsonl_path}")
+ if len(examples) < 3:
+ raise ValueError("finetune requires at least 3 examples for train/val/test splits")
+
+ from .tokenizer import DEFAULT_MAX_ENC_LEN, DEFAULT_MAX_DEC_LEN
+ max_enc_len = DEFAULT_MAX_ENC_LEN
+ max_dec_len = DEFAULT_MAX_DEC_LEN
+
+ _owns_cache = args.cache_dir is None
+ cache_dir = args.cache_dir or tempfile.mkdtemp(prefix="needle-finetune-cache-")
+ os.makedirs(cache_dir, exist_ok=True)
+
+ train_examples, val_examples, test_examples = _per_tool_split(examples)
+
+ # If not enough data for 3-way split, merge test into val
+ if len(train_examples) == 0 and len(test_examples) > 0:
+ val_examples = val_examples + test_examples
+ test_examples = list(val_examples) # test = val (same data)
+ train_remaining = []
+ # Steal half of val for train
+ for i in range(0, len(val_examples), 2):
+ if i + 1 < len(val_examples):
+ train_remaining.append(val_examples[i])
+ if train_remaining:
+ train_examples = train_remaining
+ val_examples = [ex for ex in val_examples if ex not in train_remaining]
+ test_examples = list(val_examples)
+
+ if len(train_examples) == 0:
+ raise ValueError("Not enough data — need at least 3 examples per tool for train/val/test splits")
+
+ print(f"Split: {len(train_examples)} train / {len(val_examples)} val / {len(test_examples)} test (per-tool)")
+
+ # Load checkpoint config
+ import pickle as _pkl
+ with open(args.checkpoint, "rb") as f:
+ ckpt_config = _pkl.load(f)["config"]
+
+ # Unique run ID for checkpoint scoping
+ run_id = f"{time.strftime('%Y%m%d%H%M%S')}_{os.getpid()}"
+ experiment_name = f"needle_finetuned_{run_id}"
+
+ original_cache_dir = data_mod.CACHE_DIR
+ data_mod.CACHE_DIR = cache_dir
+ try:
+ tokenizer = get_tokenizer()
+ train_kept = _write_split("train", train_examples, tokenizer, cache_dir, max_enc_len, max_dec_len)
+ val_kept = _write_split("val", val_examples, tokenizer, cache_dir, max_enc_len, max_dec_len)
+
+ if train_kept == 0:
+ raise ValueError(f"Tokenization produced 0 training sequences from {len(train_examples)} examples")
+ if val_kept == 0:
+ raise ValueError(f"Tokenization produced 0 validation sequences from {len(val_examples)} examples")
+ print(f"Tokenized: {train_kept} train / {val_kept} val sequences")
+
+ from datasets import Dataset as _Dataset
+ val_ds = _Dataset.from_list(val_examples)
+ test_ds = _Dataset.from_list(test_examples)
+
+ # Base model eval on TEST set (never seen by model)
+ from .run import load_checkpoint
+ from .model import EncoderDecoderTransformer
+ print(f"Evaluating base model on {len(test_examples)} test examples...")
+ base_params, base_config = load_checkpoint(args.checkpoint)
+ base_model = EncoderDecoderTransformer(base_config)
+ base_metrics = _quick_tool_eval(
+ base_model, base_params, tokenizer, test_examples,
+ max_gen_len=min(max_dec_len, 512), max_enc_len=max_enc_len,
+ )
+ del base_params, base_model
+ if base_metrics:
+ _emit("BASE_EVAL", base_metrics)
+ print(f" Base: call_f1={base_metrics['call_f1']:.1%}, "
+ f"exact={base_metrics['exact_match']:.1%}")
+
+ # Training (val_ds used for checkpoint selection inside train())
+ # Architecture from checkpoint config
+ print("Starting training...")
+ approx_steps = max(1, (train_kept // args.batch_size) * args.epochs)
+ cfg = ckpt_config
+ train_args = argparse.Namespace(
+ name=experiment_name, checkpoint=args.checkpoint, init_from=None,
+ epochs=args.epochs, batch_size=args.batch_size, lr=3e-5, muon_lr=0.02,
+ d_model=cfg["d_model"],
+ num_heads=cfg["num_heads"],
+ num_kv_heads=cfg.get("num_kv_heads", cfg["num_heads"]),
+ num_layers=cfg["num_encoder_layers"],
+ num_dec_layers=cfg["num_decoder_layers"],
+ d_ff=cfg.get("d_ff", cfg["d_model"] * 4),
+ max_enc_len=max_enc_len, max_dec_len=max_dec_len, max_samples=None,
+ warmup_ratio=0.05, decay_ratio=0.05, wandb=False,
+ dtype=cfg.get("dtype", "bfloat16"),
+ checkpoint_dir=args.checkpoint_dir, seed=42,
+ eval_every=max(1, approx_steps), max_eval_samples=min(val_kept, 200),
+ sparsity_ratio=0.0, group_size=32, precision="int4",
+ prune_interval=100, prune_start_frac=0.33, prune_end_frac=0.67,
+ activation=cfg.get("activation", "swiglu"),
+ mat_factors=[2, 4], dropout=0.0,
+ contrastive_weight=0.1,
+ contrastive_dim=cfg.get("contrastive_dim", 128),
+ num_memory_slots=cfg.get("num_memory_slots", 64),
+ enable_speech=cfg.get("enable_speech", False),
+ w_name=2.0, w_value=4.0, w_key=1.5,
+ no_feedforward=cfg.get("no_feedforward", True),
+ val_ds=val_ds,
+ )
+
+ from .train import train
+ train(train_args)
+ best_path = _ensure_best_checkpoint(args.checkpoint_dir, run_id)
+
+ # Finetuned model eval on TEST set (same held-out set as base eval)
+ if best_path and os.path.exists(best_path) and test_examples:
+ print(f"Evaluating finetuned model on {len(test_examples)} test examples...")
+ ft_params, ft_config = load_checkpoint(best_path)
+ ft_model = EncoderDecoderTransformer(ft_config)
+ ft_metrics = _quick_tool_eval(
+ ft_model, ft_params, tokenizer, test_examples,
+ max_gen_len=min(max_dec_len, 512), max_enc_len=max_enc_len,
+ )
+ del ft_params, ft_model
+ if ft_metrics:
+ _emit("FINETUNED_EVAL", ft_metrics)
+ print(f" Finetuned: call_f1={ft_metrics['call_f1']:.1%}, "
+ f"exact={ft_metrics['exact_match']:.1%}")
+ finally:
+ data_mod.CACHE_DIR = original_cache_dir
+ if _owns_cache:
+ shutil.rmtree(cache_dir, ignore_errors=True)
+
+if __name__ == "__main__":
+ p = argparse.ArgumentParser()
+ p.add_argument("jsonl_path")
+ p.add_argument("--epochs", type=int, default=1)
+ p.add_argument("--batch-size", type=int, default=64)
+ p.add_argument("--checkpoint-dir", type=str, default="checkpoints")
+ p.add_argument("--checkpoint", type=str, required=True)
+ p.add_argument("--cache-dir", type=str, default=None)
+ finetune_local(p.parse_args())
diff --git a/src/generate_data.py b/src/generate_data.py
index fa7cb83..5783084 100644
--- a/src/generate_data.py
+++ b/src/generate_data.py
@@ -1654,11 +1654,11 @@ def generate_all(num_samples, workers=8, batch_size=25, model=MODEL, client_pool
rng = random.Random(42)
target = int(num_samples * 1.3)
- num_batches = (target + batch_size - 1) // batch_size
+ max_submissions = target // batch_size * 3 # hard cap to prevent infinite loop
all_examples = []
failed = 0
- pbar = tqdm(total=num_batches, desc="Generating", unit="batch")
+ pbar = tqdm(total=num_samples, desc="Generating", unit="ex")
with ThreadPoolExecutor(max_workers=workers) as pool:
pending = set()
@@ -1672,7 +1672,8 @@ def generate_all(num_samples, workers=8, batch_size=25, model=MODEL, client_pool
pending.add(f)
submitted += 1
- for _ in range(min(workers, num_batches)):
+ initial = min(workers, (target + batch_size - 1) // batch_size)
+ for _ in range(initial):
_submit_one()
while pending:
@@ -1685,9 +1686,10 @@ def generate_all(num_samples, workers=8, batch_size=25, model=MODEL, client_pool
all_examples.extend(results)
except Exception as e:
failed += 1
- pbar.update(1)
- pbar.set_postfix(examples=len(all_examples), failed=failed)
- if submitted < num_batches:
+ pbar.n = min(len(all_examples), num_samples)
+ pbar.set_postfix(failed=failed)
+ pbar.refresh()
+ if len(all_examples) < target and submitted < max_submissions:
_submit_one()
pbar.close()
diff --git a/src/train.py b/src/train.py
index 6f2deeb..4975c78 100644
--- a/src/train.py
+++ b/src/train.py
@@ -262,11 +262,13 @@ def train(args):
val_dec_seg = val_data["packed_dec_seg"]
print(f" {len(enc_inputs):,} train / {len(val_enc):,} val packed bins (memory-mapped)")
- try:
- val_ds = load_tool_calls("val", max_samples=args.max_samples)
- except (FileNotFoundError, Exception) as e:
- print(f" WARNING: could not load val dataset for eval samples: {e}")
- val_ds = None
+ val_ds = getattr(args, "val_ds", None)
+ if val_ds is None:
+ try:
+ val_ds = load_tool_calls("val", max_samples=args.max_samples)
+ except (FileNotFoundError, Exception) as e:
+ print(f" WARNING: could not load val dataset for eval samples: {e}")
+ val_ds = None
cl_query_tokens = train_data.get("query_only")
cl_tool_tokens = train_data.get("tool_individual")
diff --git a/src/ui/__init__.py b/src/ui/__init__.py
new file mode 100644
index 0000000..e69de29
diff --git a/src/ui/server.py b/src/ui/server.py
new file mode 100644
index 0000000..0680a88
--- /dev/null
+++ b/src/ui/server.py
@@ -0,0 +1,756 @@
+import json as _json
+import os
+import re
+import shutil
+import subprocess
+import sys
+import tempfile
+import threading
+import time
+import urllib.parse
+from email.parser import BytesParser
+from email.policy import default as _email_policy
+from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
+from pathlib import Path
+
+_STATIC_DIR = Path(__file__).parent / "static"
+_CONTENT_TYPES = {
+ ".html": "text/html; charset=utf-8",
+ ".css": "text/css; charset=utf-8",
+ ".js": "application/javascript; charset=utf-8",
+}
+_DEFAULT_MAX_GEN_LEN = 512
+_MAX_MAX_GEN_LEN = 4096
+_MAX_JSON_BYTES = 1024 * 1024
+_MAX_UPLOAD_BYTES = 512 * 1024 * 1024
+_SOCKET_TIMEOUT = 30
+
+_model = None
+_params = None
+_tokenizer = None
+_current_model = ""
+_current_model_path = None
+_lock = threading.Lock()
+_finetune_lock = threading.Lock()
+_finetune_status = {
+ "running": False, "step": "", "log": [], "checkpoint": None,
+ "validation": None, "eval_results": None, "base_eval": None,
+}
+
+
+def _is_local_request(client_ip):
+ return client_ip in {"127.0.0.1", "::1", "::ffff:127.0.0.1"}
+
+
+def _is_same_origin(handler):
+ """Reject cross-origin POST requests (CSRF protection).
+
+ Browsers always send Origin on cross-origin POSTs. If Origin is present
+ it must match the Host header; if absent we allow (non-browser client).
+ """
+ origin = handler.headers.get("Origin")
+ if origin is None:
+ return True
+ try:
+ parsed = urllib.parse.urlparse(origin)
+ origin_host = parsed.hostname or ""
+ default_port = 443 if parsed.scheme == "https" else 80
+ origin_port = parsed.port or default_port
+ host_header = handler.headers.get("Host", "")
+ if ":" in host_header:
+ h_host, h_port = host_header.rsplit(":", 1)
+ h_port = int(h_port)
+ else:
+ h_host, h_port = host_header, default_port
+ return origin_host == h_host and origin_port == h_port
+ except Exception:
+ return False
+
+
+def _project_root():
+ return Path(__file__).parent.parent.parent
+
+
+def _checkpoints_dir():
+ return _project_root() / "checkpoints"
+
+
+def _snapshot_finetune_status():
+ with _finetune_lock:
+ return {
+ "running": _finetune_status["running"],
+ "step": _finetune_status["step"],
+ "log": list(_finetune_status["log"]),
+ "checkpoint": _finetune_status["checkpoint"],
+ "validation": _finetune_status["validation"],
+ "eval_results": _finetune_status["eval_results"],
+ "base_eval": _finetune_status["base_eval"],
+ }
+
+
+def _set_finetune_status(**updates):
+ with _finetune_lock:
+ _finetune_status.update(updates)
+
+
+_MAX_FINETUNE_LOG_LINES = 2000
+
+
+def _append_finetune_log(msg):
+ with _finetune_lock:
+ log = _finetune_status["log"]
+ log.append(msg)
+ if len(log) > _MAX_FINETUNE_LOG_LINES:
+ del log[: len(log) - _MAX_FINETUNE_LOG_LINES]
+ print(f"[finetune] {msg}", file=sys.stderr)
+
+
+
+def _read_request_body(handler, max_bytes):
+ try:
+ length = int(handler.headers.get("Content-Length", "0"))
+ except ValueError:
+ raise ValueError("invalid Content-Length")
+ if length <= 0:
+ raise ValueError("empty request body")
+ if length > max_bytes:
+ raise ValueError(f"request body too large (max {max_bytes} bytes)")
+ return handler.rfile.read(length)
+
+
+def _read_json_request(handler, max_bytes=_MAX_JSON_BYTES):
+ try:
+ raw = _read_request_body(handler, max_bytes)
+ body = _json.loads(raw)
+ except _json.JSONDecodeError as exc:
+ raise ValueError("invalid JSON") from exc
+ if not isinstance(body, dict):
+ raise ValueError("JSON body must be an object")
+ return body
+
+
+def _parse_bool(value, default):
+ if value is None:
+ return default
+ if isinstance(value, bool):
+ return value
+ if isinstance(value, str):
+ lowered = value.strip().lower()
+ if lowered in {"1", "true", "yes", "on"}:
+ return True
+ if lowered in {"0", "false", "no", "off"}:
+ return False
+ raise ValueError("boolean field is invalid")
+
+
+def _clamp_int(value, default, minimum, maximum, field_name):
+ if value in (None, ""):
+ return default
+ try:
+ parsed = int(value)
+ except (TypeError, ValueError) as exc:
+ raise ValueError(f"{field_name} must be an integer") from exc
+ return max(minimum, min(parsed, maximum))
+
+
+def _normalize_tools_json(tools):
+ if tools is None or tools == "":
+ return "[]"
+ if isinstance(tools, str):
+ try:
+ parsed = _json.loads(tools)
+ except _json.JSONDecodeError as exc:
+ raise ValueError("tools must be valid JSON") from exc
+ elif isinstance(tools, list):
+ parsed = tools
+ else:
+ raise ValueError("tools must be a JSON string or array")
+ if not isinstance(parsed, list):
+ raise ValueError("tools must be a JSON array")
+ return _json.dumps(parsed, separators=(",", ":"))
+
+
+def _parse_generate_request(body):
+ query = body.get("query")
+ if not isinstance(query, str) or not query.strip():
+ raise ValueError("query is required")
+ tools = _normalize_tools_json(body.get("tools", "[]"))
+ seed = _clamp_int(body.get("seed", 0), 0, 0, 2**31 - 1, "seed")
+ max_gen_len = _clamp_int(
+ body.get("max_gen_len", _DEFAULT_MAX_GEN_LEN),
+ _DEFAULT_MAX_GEN_LEN,
+ 1,
+ _MAX_MAX_GEN_LEN,
+ "max_gen_len",
+ )
+ constrained = _parse_bool(body.get("constrained", True), True)
+ return query.strip(), tools, seed, max_gen_len, constrained
+
+
+def _parse_finetune_request(body):
+ tools = _normalize_tools_json(body.get("tools", "[]"))
+ parsed_tools = _json.loads(tools)
+ if not parsed_tools:
+ raise ValueError("tools must be a non-empty JSON array")
+ for i, t in enumerate(parsed_tools):
+ if not isinstance(t, dict):
+ raise ValueError(f"tool[{i}] must be an object")
+ if "name" not in t:
+ if "function" in t:
+ raise ValueError(
+ 'Wrong format: tools use OpenAI format ({"type":"function","function":{...}}). '
+ 'Needle requires [{"name":"...","description":"...","parameters":{...}}]'
+ )
+ raise ValueError(f"tool[{i}] missing 'name' field")
+ if "description" not in t:
+ raise ValueError(f"tool[{i}] missing 'description' field")
+ api_key = body.get("api_key")
+ if not isinstance(api_key, str) or not api_key.strip():
+ raise ValueError("Gemini API key is required")
+ return tools, api_key.strip()
+
+
+def _stream_upload_to_file(handler, max_bytes, target_dir):
+ """Parse multipart upload and stream the file part directly to disk.
+
+ Returns (filename, target_path) on success. Raises ValueError on bad input.
+ """
+ content_type = handler.headers.get("Content-Type", "")
+ if "multipart/form-data" not in content_type:
+ raise ValueError("expected multipart/form-data")
+ if "boundary=" not in content_type:
+ raise ValueError("missing multipart boundary")
+ try:
+ length = int(handler.headers.get("Content-Length", "0"))
+ except ValueError:
+ raise ValueError("invalid Content-Length")
+ if length <= 0:
+ raise ValueError("empty request body")
+ if length > max_bytes:
+ raise ValueError(f"request body too large (max {max_bytes} bytes)")
+
+ body = handler.rfile.read(length)
+ header = f"Content-Type: {content_type}\r\nMIME-Version: 1.0\r\n\r\n".encode("utf-8")
+ message = BytesParser(policy=_email_policy).parsebytes(header + body)
+ del body # free raw body (~512 MB)
+ if not message.is_multipart():
+ raise ValueError("expected multipart/form-data")
+
+ filename = None
+ payload = None
+ for part in message.iter_parts():
+ if part.get_param("name", header="content-disposition") != "file":
+ continue
+ filename = part.get_filename()
+ payload = part.get_payload(decode=True)
+ break
+ del message # free parsed MIME tree
+ if not filename or payload is None:
+ raise ValueError("expected a .pkl file")
+ if not filename.endswith(".pkl"):
+ raise ValueError("expected a .pkl file")
+ target_dir.mkdir(parents=True, exist_ok=True)
+ safe_name = re.sub(r"[^A-Za-z0-9._-]", "_", Path(filename).name)
+ target = target_dir / safe_name
+ with open(target, "wb") as f:
+ f.write(payload)
+ del payload # free file data
+ return filename, target
+
+
+_datagen_lock = threading.Lock()
+
+
+def _generate_custom_data(tools_json, api_key, num_samples, data_file):
+ import json
+ from .. import generate_data as gd
+
+ tools = json.loads(tools_json)
+
+ def _pick_tools(_rng, force_empty=False, few_tools=False):
+ if force_empty:
+ return []
+ return [dict(t) for t in tools]
+
+ def _synthesize_tools(*args, **kwargs):
+ return None
+
+ with _datagen_lock:
+ old_api_key = os.environ.get("GEMINI_API_KEY")
+ old_pick_tools = gd._pick_tools
+ old_synthesize_tools = gd._synthesize_tools
+ old_overlap_pairs = gd._OVERLAP_PAIRS
+ old_languages = gd.LANGUAGES
+ try:
+ os.environ["GEMINI_API_KEY"] = api_key
+ gd._pick_tools = _pick_tools
+ gd._synthesize_tools = _synthesize_tools
+ gd._OVERLAP_PAIRS = []
+ gd.LANGUAGES = ["English"]
+ client_pool = gd.ClientPool(gd.make_clients())
+ examples = gd.generate_all(
+ num_samples,
+ workers=4,
+ batch_size=25,
+ model=gd.MODEL,
+ client_pool=client_pool,
+ )
+ with open(data_file, "w") as f:
+ for ex in examples:
+ f.write(json.dumps(ex) + "\n")
+ return len(examples)
+ finally:
+ gd._pick_tools = old_pick_tools
+ gd._synthesize_tools = old_synthesize_tools
+ gd._OVERLAP_PAIRS = old_overlap_pairs
+ gd.LANGUAGES = old_languages
+ if old_api_key is None:
+ os.environ.pop("GEMINI_API_KEY", None)
+ else:
+ os.environ["GEMINI_API_KEY"] = old_api_key
+
+
+class _Handler(BaseHTTPRequestHandler):
+ def do_GET(self):
+ path = self.path.split("?")[0]
+ if path == "/":
+ path = "/static/index.html"
+ if path == "/model-info":
+ self._json_response(200, {"name": _current_model})
+ return
+ if path == "/finetune/status":
+ if not _is_local_request(self.client_address[0]):
+ self._json_response(403, {"error": "finetune status is only available from localhost"})
+ return
+ self._json_response(200, _snapshot_finetune_status())
+ return
+ if path.startswith("/download/"):
+ if not _is_local_request(self.client_address[0]):
+ self._json_response(403, {"error": "checkpoint download is only allowed from localhost"})
+ return
+ name = urllib.parse.unquote(path[len("/download/"):])
+ if "/" in name or "\\" in name or name.startswith("."):
+ self.send_error(400)
+ return
+ fpath = (_checkpoints_dir() / name).resolve()
+ ckpt_dir = _checkpoints_dir().resolve()
+ if fpath.is_file() and fpath.is_relative_to(ckpt_dir) and fpath.suffix in (".pkl", ".zip"):
+ ctype = "application/zip" if fpath.suffix == ".zip" else "application/octet-stream"
+ self.send_response(200)
+ self.send_header("Content-Type", ctype)
+ self.send_header("Content-Disposition", f'attachment; filename="{name}"')
+ self.send_header("Content-Length", str(fpath.stat().st_size))
+ self.end_headers()
+ with open(fpath, "rb") as f:
+ while chunk := f.read(65536):
+ self.wfile.write(chunk)
+ return
+ if path.startswith("/static/"):
+ name = path[len("/static/"):]
+ fpath = (_STATIC_DIR / name).resolve()
+ if fpath.is_file() and fpath.is_relative_to(_STATIC_DIR.resolve()):
+ self.send_response(200)
+ self.send_header("Content-Type", _CONTENT_TYPES.get(fpath.suffix, "application/octet-stream"))
+ self.end_headers()
+ self.wfile.write(fpath.read_bytes())
+ return
+ self.send_error(404)
+
+ def do_POST(self):
+ if not _is_same_origin(self):
+ self._json_response(403, {"error": "cross-origin requests are not allowed"})
+ return
+ if self.path == "/generate":
+ self._handle_generate()
+ elif self.path == "/finetune":
+ self._handle_finetune()
+ elif self.path == "/load-model":
+ self._handle_load_model()
+ else:
+ self.send_error(404)
+
+ def _handle_generate(self):
+ try:
+ body = _read_json_request(self)
+ query, tools, seed, max_gen_len, constrained = _parse_generate_request(body)
+ except ValueError as exc:
+ self._json_response(400, {"error": str(exc)})
+ return
+
+ result, error = _run_generate(query, tools, seed, max_gen_len, constrained)
+ if error:
+ self._json_response(500, {"error": error})
+ else:
+ self._json_response(200, {"result": result})
+
+ def _handle_finetune(self):
+ if not _is_local_request(self.client_address[0]):
+ self._json_response(403, {"error": "finetune is only allowed from localhost"})
+ return
+ try:
+ body = _read_json_request(self)
+ tools, api_key = _parse_finetune_request(body)
+ except ValueError as exc:
+ self._json_response(400, {"error": str(exc)})
+ return
+ if not _current_model_path:
+ self._json_response(400, {"error": "Load a model before finetuning"})
+ return
+ if not _start_finetune(tools, api_key):
+ self._json_response(409, {"error": "finetune already running"})
+ return
+ self._json_response(200, {"status": "started"})
+
+ def _handle_load_model(self):
+ if not _is_local_request(self.client_address[0]):
+ self._json_response(403, {"error": "model upload is only allowed from localhost"})
+ return
+ upload_dir = _checkpoints_dir() / "_uploaded"
+ try:
+ filename, target = _stream_upload_to_file(self, _MAX_UPLOAD_BYTES, upload_dir)
+ except ValueError as exc:
+ self._json_response(400, {"error": str(exc)})
+ return
+ try:
+ _load_checkpoint(str(target), display_name=filename)
+ self._json_response(200, {"name": filename})
+ except Exception as exc:
+ target.unlink(missing_ok=True)
+ self._json_response(500, {"error": "Failed to load checkpoint"})
+
+ def _json_response(self, code, data):
+ payload = _json.dumps(data).encode()
+ self.send_response(code)
+ self.send_header("Content-Type", "application/json; charset=utf-8")
+ self.send_header("Content-Length", str(len(payload)))
+ self.end_headers()
+ self.wfile.write(payload)
+
+ def log_message(self, fmt, *args):
+ pass
+
+
+def _run_generate(query, tools, seed, max_gen_len, constrained):
+ from ..run import generate
+
+ with _lock:
+ if _model is None or _params is None or _tokenizer is None:
+ return None, "model is not loaded"
+ try:
+ result = generate(
+ _model,
+ _params,
+ _tokenizer,
+ query,
+ tools=tools,
+ max_gen_len=max_gen_len,
+ seed=seed,
+ stream=False,
+ constrained=constrained,
+ )
+ return result, None
+ except Exception as exc:
+ print(f"[generate] {exc}", file=sys.stderr)
+ return None, "Generation failed"
+
+
+def _load_checkpoint(path, display_name=None):
+ global _model, _params, _tokenizer, _current_model, _current_model_path
+ from ..data import get_tokenizer
+ from ..model import EncoderDecoderTransformer
+ from ..run import load_checkpoint
+
+ with _lock:
+ _params, config = load_checkpoint(path)
+ _model = EncoderDecoderTransformer(config)
+ _tokenizer = get_tokenizer()
+ _current_model = display_name or Path(path).name
+ _current_model_path = path
+ print(f"Loaded: {_current_model}", file=sys.stderr)
+
+
+def _validate_training_data(data_file_path):
+ """Validate JSONL training data and return a report dict."""
+ import collections
+ examples = []
+ warnings = []
+ with open(data_file_path) as f:
+ for i, line in enumerate(f):
+ if not line.strip():
+ continue
+ try:
+ ex = _json.loads(line)
+ except _json.JSONDecodeError:
+ warnings.append(f"Line {i+1}: invalid JSON")
+ continue
+ for key in ("query", "tools", "answers"):
+ if key not in ex:
+ warnings.append(f"Line {i+1}: missing '{key}'")
+ examples.append(ex)
+
+ tool_counts = collections.Counter()
+ for ex in examples:
+ try:
+ answers = _json.loads(ex.get("answers", "[]"))
+ for a in answers:
+ if isinstance(a, dict) and "name" in a:
+ tool_counts[a["name"]] += 1
+ except (_json.JSONDecodeError, TypeError):
+ pass
+
+ seen = set()
+ dup_count = 0
+ for ex in examples:
+ key = (ex.get("query", ""), ex.get("answers", ""))
+ if key in seen:
+ dup_count += 1
+ seen.add(key)
+
+ counts = list(tool_counts.values()) or [0]
+ return {
+ "total": len(examples),
+ "per_tool": dict(tool_counts),
+ "duplicates": dup_count,
+ "min_per_tool": min(counts),
+ "max_per_tool": max(counts),
+ "warnings": warnings[:20],
+ }
+
+
+_SAMPLES_PER_TOOL = 120 # 100 train + 10 val + 10 test
+_EPOCHS = 3
+
+
+def _start_finetune(tools_json, api_key):
+ with _finetune_lock:
+ if _finetune_status["running"]:
+ return False
+ _finetune_status["running"] = True
+ _finetune_status["step"] = "starting"
+ _finetune_status["log"] = []
+ _finetune_status["checkpoint"] = None
+ _finetune_status["validation"] = None
+ _finetune_status["eval_results"] = None
+ _finetune_status["base_eval"] = None
+
+ def _run():
+ python = sys.executable
+ work_dir = Path(tempfile.mkdtemp(prefix="needle-ui-finetune-", dir=str(_project_root())))
+ data_file = work_dir / "data.jsonl"
+ cache_dir = work_dir / "cache"
+
+ try:
+ num_tools = len(_json.loads(tools_json))
+ num_samples = _SAMPLES_PER_TOOL * num_tools
+
+ _set_finetune_status(step="generating data")
+ _append_finetune_log(f"Generating {_SAMPLES_PER_TOOL} samples/tool for {num_tools} tools...")
+ generated = _generate_custom_data(tools_json, api_key, num_samples, data_file)
+ if generated < 3:
+ raise RuntimeError("generated fewer than 3 examples; need more data for train/val/test")
+ _append_finetune_log(f"Generated {generated} samples.")
+
+ # Data validation
+ # Validation runs as part of data generation step
+ _append_finetune_log("Validating training data...")
+ validation = _validate_training_data(str(data_file))
+ _set_finetune_status(validation=validation)
+ _append_finetune_log(
+ f"Validated: {validation['total']} examples, "
+ f"{len(validation['per_tool'])} tools, "
+ f"{validation['duplicates']} duplicates"
+ )
+ if validation["warnings"]:
+ for w in validation["warnings"][:5]:
+ _append_finetune_log(f" WARNING: {w}")
+
+ # Training subprocess
+ _set_finetune_status(step="training")
+ checkpoint_arg = []
+ ckpt_name = "scratch"
+ ckpt_dir = _checkpoints_dir()
+ existing_pkls = set()
+ if ckpt_dir.exists():
+ existing_pkls = set(p.name for p in ckpt_dir.glob("*.pkl"))
+ if _current_model_path:
+ checkpoint_arg = ["--checkpoint", _current_model_path]
+ ckpt_name = Path(_current_model_path).name
+ batch_size = min(32, generated)
+ _append_finetune_log(
+ f"Training {_EPOCHS} epochs, batch_size={batch_size}, "
+ f"{generated} examples, from {ckpt_name}"
+ )
+
+ _set_finetune_status(step="evaluating base")
+ last_eval = None
+ proc = subprocess.Popen(
+ [
+ python, "-u",
+ "-m", "src.finetune",
+ str(data_file),
+ "--epochs", str(_EPOCHS),
+ "--batch-size", str(batch_size),
+ "--checkpoint-dir", str(ckpt_dir),
+ "--cache-dir", str(cache_dir),
+ *checkpoint_arg,
+ ],
+ cwd=str(_project_root()),
+ stdout=subprocess.PIPE,
+ stderr=subprocess.STDOUT,
+ text=True,
+ )
+ try:
+ for line in proc.stdout:
+ line = line.rstrip("\n")
+ if line.startswith("BASE_EVAL:"):
+ try:
+ _set_finetune_status(base_eval=_json.loads(line[10:]), step="training")
+ except _json.JSONDecodeError:
+ pass
+ elif line.startswith("FINETUNED_EVAL:"):
+ try:
+ last_eval = _json.loads(line[15:])
+ _set_finetune_status(eval_results=last_eval)
+ except _json.JSONDecodeError:
+ pass
+ elif line:
+ if "Evaluating finetuned" in line:
+ _set_finetune_status(step="evaluating finetuned")
+ _append_finetune_log(line)
+ proc.wait(timeout=7200)
+ except subprocess.TimeoutExpired:
+ proc.kill()
+ _append_finetune_log("Training timed out (2h limit).")
+ _set_finetune_status(step="failed")
+ return
+
+ if proc.returncode != 0:
+ _append_finetune_log("Training failed (see log above).")
+ _set_finetune_status(step="failed")
+ return
+ _append_finetune_log("Training complete.")
+
+ # Store final eval results
+ if last_eval:
+ _set_finetune_status(eval_results=last_eval)
+
+ new_files = [p for p in ckpt_dir.glob("*.pkl") if p.name not in existing_pkls]
+ if not new_files:
+ _append_finetune_log("No new checkpoint produced.")
+ _set_finetune_status(step="done")
+ return
+
+ newest = max(new_files, key=lambda p: p.stat().st_mtime)
+ ts = time.strftime("%Y%m%d_%H%M%S")
+ final_name = f"needle_finetuned_{ts}.pkl"
+ final_path = ckpt_dir / final_name
+ newest.replace(final_path)
+
+ # Assemble download bundle (same per-tool split as finetune)
+ import zipfile
+ from ..finetune import _per_tool_split
+
+ with open(data_file) as _df:
+ all_examples = [_json.loads(ln) for ln in _df if ln.strip()]
+ train_examples, val_examples, test_examples = _per_tool_split(all_examples)
+
+ with _finetune_lock:
+ base_eval = _finetune_status.get("base_eval")
+
+ per_tool_report = {}
+ if base_eval and base_eval.get("per_tool"):
+ for t, bm in base_eval["per_tool"].items():
+ per_tool_report[t] = {"base": round(bm["correct"] / max(bm["total"], 1), 3)}
+ if last_eval and last_eval.get("per_tool"):
+ for t, fm in last_eval["per_tool"].items():
+ entry = per_tool_report.setdefault(t, {})
+ entry["finetuned"] = round(fm["correct"] / max(fm["total"], 1), 3)
+
+ eval_report = {
+ "model": ckpt_name,
+ "finetuned_checkpoint": final_name,
+ "training": {"examples": len(train_examples), "tools": num_tools, "epochs": _EPOCHS},
+ "validation": {"examples": len(val_examples)},
+ "test": {"examples": len(test_examples), "duplicates": validation.get("duplicates", 0)},
+ "base": {k: base_eval.get(k) for k in ("call_f1", "name_f1", "exact_match", "parse_rate", "args_acc")} if base_eval else None,
+ "finetuned": {k: last_eval.get(k) for k in ("call_f1", "name_f1", "exact_match", "parse_rate", "args_acc")} if last_eval else None,
+ "per_tool": per_tool_report,
+ }
+
+ def _pct(v):
+ return f"{v * 100:.1f}%" if v is not None else "n/a"
+
+ b = eval_report.get("base") or {}
+ f_ = eval_report.get("finetuned") or {}
+ readme = (
+ f"# Needle Finetuned Model\n\n"
+ f"## Training\n"
+ f"- Base model: {ckpt_name}\n"
+ f"- Epochs: {_EPOCHS}\n"
+ f"- Training examples: {len(train_examples)}\n"
+ f"- Validation examples: {len(val_examples)} (used for checkpoint selection)\n"
+ f"- Test examples: {len(test_examples)} (held out, used for eval)\n"
+ f"- Tools: {num_tools}\n\n"
+ f"## Evaluation (on held-out test set)\n\n"
+ f"| Metric | Base | Finetuned |\n"
+ f"|--------|------|----------|\n"
+ f"| Call F1 | {_pct(b.get('call_f1'))} | {_pct(f_.get('call_f1'))} |\n"
+ f"| Name F1 | {_pct(b.get('name_f1'))} | {_pct(f_.get('name_f1'))} |\n"
+ f"| Exact Match | {_pct(b.get('exact_match'))} | {_pct(f_.get('exact_match'))} |\n"
+ f"| Parse Rate | {_pct(b.get('parse_rate'))} | {_pct(f_.get('parse_rate'))} |\n"
+ f"| Args Accuracy | {_pct(b.get('args_acc'))} | {_pct(f_.get('args_acc'))} |\n\n"
+ f"## Contents\n"
+ f"- `checkpoint.pkl` — Finetuned model weights\n"
+ f"- `tools.json` — Tool definitions\n"
+ f"- `train.jsonl` — Training data ({len(train_examples)} examples)\n"
+ f"- `val.jsonl` — Validation data ({len(val_examples)} examples)\n"
+ f"- `test.jsonl` — Test data ({len(test_examples)} examples)\n"
+ f"- `eval_report.json` — Machine-readable evaluation\n"
+ )
+
+ bundle_name = f"needle_finetuned_{ts}.zip"
+ bundle_path = ckpt_dir / bundle_name
+ with zipfile.ZipFile(str(bundle_path), "w", zipfile.ZIP_DEFLATED) as zf:
+ zf.write(str(final_path), "checkpoint.pkl")
+ zf.writestr("tools.json", tools_json)
+ zf.writestr("train.jsonl", "\n".join(_json.dumps(ex) for ex in train_examples) + "\n")
+ zf.writestr("val.jsonl", "\n".join(_json.dumps(ex) for ex in val_examples) + "\n")
+ zf.writestr("test.jsonl", "\n".join(_json.dumps(ex) for ex in test_examples) + "\n")
+ zf.writestr("eval_report.json", _json.dumps(eval_report, indent=2))
+ zf.writestr("README.md", readme)
+
+ _set_finetune_status(checkpoint=bundle_name)
+ _append_finetune_log(f"Bundle ready: {bundle_name}")
+ _set_finetune_status(step="done")
+ except Exception as exc:
+ _append_finetune_log(f"Error: {exc}")
+ _set_finetune_status(step="failed")
+ finally:
+ _set_finetune_status(running=False)
+ shutil.rmtree(work_dir, ignore_errors=True)
+
+ threading.Thread(target=_run, daemon=True).start()
+ return True
+
+
+def main(args):
+ global _model, _params, _tokenizer, _current_model, _current_model_path
+ from ..data import get_tokenizer
+ from ..model import EncoderDecoderTransformer
+ from ..run import load_checkpoint
+
+ print(f"Loading checkpoint: {args.checkpoint}", file=sys.stderr)
+ _params, config = load_checkpoint(args.checkpoint)
+ _model = EncoderDecoderTransformer(config)
+ _tokenizer = get_tokenizer()
+ _current_model = Path(args.checkpoint).name
+ _current_model_path = args.checkpoint
+
+ param_count = sum(x.size for x in __import__("jax").tree.leaves(_params))
+ print(f"Model parameters: {param_count:,}", file=sys.stderr)
+
+ server = ThreadingHTTPServer((args.host, args.port), _Handler)
+ server.timeout = _SOCKET_TIMEOUT
+ server.request_queue_size = 16
+ print(f"Needle UI: http://{args.host}:{args.port}", file=sys.stderr)
+ server.serve_forever()
diff --git a/src/ui/static/app.js b/src/ui/static/app.js
new file mode 100644
index 0000000..cb06b58
--- /dev/null
+++ b/src/ui/static/app.js
@@ -0,0 +1,354 @@
+var _toastTimer = null;
+
+function showError(msg) {
+ if (_toastTimer) clearTimeout(_toastTimer);
+ document.getElementById("toastMsg").textContent = msg;
+ document.getElementById("toast").classList.add("visible");
+ _toastTimer = setTimeout(dismissToast, 8000);
+}
+
+function dismissToast() {
+ document.getElementById("toast").classList.remove("visible");
+}
+
+function loadToolsFile(input) {
+ var file = input.files[0];
+ if (!file) return;
+ var reader = new FileReader();
+ reader.onload = function () {
+ try {
+ JSON.parse(reader.result);
+ document.getElementById("tools").value = reader.result;
+ } catch (e) {
+ showError("Invalid JSON file");
+ }
+ };
+ reader.readAsText(file);
+ input.value = "";
+}
+
+function fetchModelName() {
+ fetch("/model-info")
+ .then(function (r) {
+ if (!r.ok) throw new Error(r.status);
+ return r.json();
+ })
+ .then(function (data) {
+ document.getElementById("modelName").textContent = data.name || "";
+ })
+ .catch(function () {});
+}
+
+fetchModelName();
+
+function loadModelFile(input) {
+ var file = input.files[0];
+ if (!file) return;
+ var formData = new FormData();
+ formData.append("file", file);
+ document.getElementById("result").textContent = "Loading model...";
+ fetch("/load-model", { method: "POST", body: formData })
+ .then(function (r) {
+ if (!r.ok && r.headers.get("content-type") !== "application/json; charset=utf-8") throw new Error("Server error " + r.status);
+ return r.json();
+ })
+ .then(function (data) {
+ if (data.error) {
+ showError(data.error);
+ document.getElementById("result").textContent = "";
+ } else {
+ document.getElementById("result").className = "result-box has-result";
+ document.getElementById("result").textContent = "Model loaded: " + data.name;
+ document.getElementById("modelName").textContent = data.name;
+ }
+ })
+ .catch(function (e) { showError("Upload failed: " + e.message); });
+ input.value = "";
+}
+
+async function send() {
+ var input = document.getElementById("query");
+ var btn = document.getElementById("sendBtn");
+ var result = document.getElementById("result");
+ var query = input.value.trim();
+ if (!query) return;
+
+ input.disabled = true;
+ btn.disabled = true;
+ result.className = "result-box";
+ result.textContent = "Running...";
+
+ try {
+ var r = await fetch("/generate", {
+ method: "POST",
+ headers: { "Content-Type": "application/json" },
+ body: JSON.stringify({
+ query: query,
+ tools: document.getElementById("tools").value.trim() || "[]",
+ seed: 0,
+ max_gen_len: 512,
+ constrained: true,
+ }),
+ });
+ if (!r.ok && !(r.headers.get("content-type") || "").includes("json")) throw new Error("Server error " + r.status);
+ var data = await r.json();
+ if (data.error) {
+ result.textContent = "";
+ showError(data.error);
+ } else if (data.result === null || data.result === undefined || data.result === "") {
+ result.textContent = "";
+ showError("Empty response from model");
+ } else {
+ result.className = "result-box has-result";
+ result.textContent = data.result;
+ }
+ } catch (e) {
+ result.textContent = "";
+ showError("Request failed: " + e.message);
+ } finally {
+ input.disabled = false;
+ btn.disabled = false;
+ input.focus();
+ }
+}
+
+function togglePanel() {
+ var sidebar = document.getElementById("sidebar");
+ sidebar.classList.toggle("open");
+ var label = document.querySelector(".tools-toggle span");
+ label.textContent = sidebar.classList.contains("open") ? "Query" : "Tools";
+}
+
+var _pollTimer = null;
+var _ftRunning = false;
+
+function openFinetuneModal() {
+ var tools = document.getElementById("tools").value.trim() || "[]";
+ try {
+ JSON.parse(tools);
+ } catch (e) {
+ showError("Invalid tools JSON");
+ return;
+ }
+ _resetModal();
+ document.getElementById("modalOverlay").classList.add("visible");
+ document.getElementById("ftApiKey").focus();
+}
+
+function closeModal(e) {
+ if (e && e.target && e.target !== document.getElementById("modalOverlay")) return;
+ if (_ftRunning) return;
+ document.getElementById("modalOverlay").classList.remove("visible");
+}
+
+function _resetModal() {
+ document.getElementById("ftSteps").classList.remove("visible");
+ document.getElementById("ftStartBtn").disabled = false;
+ document.getElementById("ftStartBtn").textContent = "Start Finetune";
+ document.getElementById("ftStartBtn").style.display = "";
+ document.getElementById("modalCloseBtn").style.display = "";
+ var dl = document.getElementById("ftDownload");
+ if (dl) dl.remove();
+ document.getElementById("ftEvalResults").innerHTML = "";
+ var steps = document.querySelectorAll(".modal-step");
+ for (var i = 0; i < steps.length; i++) {
+ steps[i].classList.remove("active", "done");
+ }
+}
+
+async function startFinetune() {
+ var apiKey = document.getElementById("ftApiKey").value.trim();
+ if (!apiKey) {
+ showError("Gemini API key is required");
+ return;
+ }
+
+ var tools = document.getElementById("tools").value.trim() || "[]";
+ try {
+ JSON.parse(tools);
+ } catch (e) {
+ showError("Invalid tools JSON");
+ return;
+ }
+
+ var btn = document.getElementById("ftStartBtn");
+ btn.disabled = true;
+ btn.textContent = "Starting...";
+ document.getElementById("modalCloseBtn").style.display = "none";
+ document.getElementById("ftSteps").classList.add("visible");
+ var steps = document.querySelectorAll(".modal-step");
+ for (var i = 0; i < steps.length; i++) steps[i].classList.remove("active", "done");
+ _ftRunning = true;
+
+ try {
+ var r = await fetch("/finetune", {
+ method: "POST",
+ headers: { "Content-Type": "application/json" },
+ body: JSON.stringify({
+ tools: tools,
+ api_key: apiKey,
+ }),
+ });
+ if (!r.ok && !(r.headers.get("content-type") || "").includes("json")) throw new Error("Server error " + r.status);
+ var data = await r.json();
+ if (data.error) {
+ showError(data.error);
+ _ftRunning = false;
+ _resetModal();
+ return;
+ }
+ _pollTimer = setInterval(pollFinetune, 2000);
+ } catch (e) {
+ showError("Request failed: " + e.message);
+ _ftRunning = false;
+ _resetModal();
+ }
+}
+
+function _updateSteps(currentStep) {
+ var steps = document.querySelectorAll(".modal-step");
+ var found = false;
+ for (var i = 0; i < steps.length; i++) {
+ if (steps[i].getAttribute("data-step") === currentStep) found = true;
+ }
+ if (!found) return;
+
+ var past = true;
+ for (var i = 0; i < steps.length; i++) {
+ var stepName = steps[i].getAttribute("data-step");
+ steps[i].classList.remove("active", "done");
+ if (stepName === currentStep) {
+ steps[i].classList.add("active");
+ past = false;
+ } else if (past) {
+ steps[i].classList.add("done");
+ }
+ }
+}
+
+function _renderEvalResults(ft, base) {
+ var el = document.getElementById("ftEvalResults");
+ if (!ft && !base) { el.innerHTML = ""; return; }
+ var metrics = ["call_f1", "name_f1", "exact_match", "parse_rate", "args_acc"];
+ var labels = {"call_f1": "Call F1", "name_f1": "Name F1", "exact_match": "Exact Match", "parse_rate": "Parse Rate", "args_acc": "Args Accuracy"};
+ var hasBase = base && base.call_f1 != null;
+ var rows = metrics.map(function (k) {
+ var bv = base && base[k] != null ? (base[k] * 100).toFixed(1) + '%' : '-';
+ var fv = ft && ft[k] != null ? (ft[k] * 100).toFixed(1) + '%' : '-';
+ if (hasBase) {
+ return '<tr><td>' + labels[k] + '</td><td>' + bv + '</td><td>' + fv + '</td></tr>';
+ }
+ return '<tr><td>' + labels[k] + '</td><td>' + fv + '</td></tr>';
+ }).join("");
+ var header = hasBase
+ ? '<tr><th>Metric</th><th>Base</th><th>Finetuned</th></tr>'
+ : '<tr><th>Metric</th><th>Score</th></tr>';
+ el.innerHTML =
+ '<div class="ft-section-title">Evaluation Results</div>' +
+ '<table class="ft-table"><thead>' + header + '</thead><tbody>' + rows + '</tbody></table>';
+}
+
+async function pollFinetune() {
+ try {
+ var r = await fetch("/finetune/status");
+ if (!r.ok) throw new Error("Server error " + r.status);
+ var data = await r.json();
+
+ _updateSteps(data.step);
+
+ var btn = document.getElementById("ftStartBtn");
+ var labels = {
+ "starting": "Starting...",
+ "generating data": "Generating data...",
+ "evaluating base": "Training...",
+ "training": "Training...",
+ "evaluating finetuned": "Training...",
+ };
+ var label = labels[data.step] || data.step;
+ btn.textContent = label;
+
+ _renderEvalResults(data.eval_results, data.base_eval);
+
+ if (!data.running) {
+ clearInterval(_pollTimer);
+ _pollTimer = null;
+ _ftRunning = false;
+ document.getElementById("modalCloseBtn").style.display = "";
+
+ if (data.step === "done") {
+ var steps = document.querySelectorAll(".modal-step");
+ for (var i = 0; i < steps.length; i++) {
+ steps[i].classList.remove("active");
+ steps[i].classList.add("done");
+ }
+ btn.style.display = "none";
+ if (data.checkpoint) {
+ var footer = document.getElementById("ftFooter");
+ var dl = document.createElement("a");
+ dl.id = "ftDownload";
+ dl.className = "modal-download";
+ dl.href = "/download/" + data.checkpoint;
+ dl.download = data.checkpoint;
+ dl.textContent = "Download " + data.checkpoint;
+ footer.appendChild(dl);
+ }
+ } else if (data.step === "failed") {
+ var last = data.log.length > 0 ? data.log[data.log.length - 1] : "Unknown error";
+ showError("Finetune failed — " + last);
+ btn.textContent = "Retry";
+ btn.disabled = false;
+ }
+ }
+ } catch (e) {
+ clearInterval(_pollTimer);
+ _pollTimer = null;
+ _ftRunning = false;
+ document.getElementById("modalCloseBtn").style.display = "";
+ showError("Lost connection to server");
+ document.getElementById("ftStartBtn").textContent = "Retry";
+ document.getElementById("ftStartBtn").disabled = false;
+ }
+}
+
+document.getElementById("query").addEventListener("keydown", function (e) {
+ if (e.key === "Enter" && !e.shiftKey) {
+ e.preventDefault();
+ send();
+ }
+});
+
+document.addEventListener("keydown", function (e) {
+ if (e.key === "Escape") closeModal();
+});
+
+(function () {
+ var handle = document.getElementById("resizeHandle");
+ var sidebar = document.getElementById("sidebar");
+ var dragging = false;
+
+ handle.addEventListener("mousedown", function (e) {
+ if (window.innerWidth <= 768) return;
+ e.preventDefault();
+ dragging = true;
+ handle.classList.add("active");
+ document.body.style.cursor = "col-resize";
+ document.body.style.userSelect = "none";
+ });
+
+ window.addEventListener("mousemove", function (e) {
+ if (!dragging) return;
+ sidebar.style.width = Math.min(Math.max(e.clientX, 200), window.innerWidth * 0.6) + "px";
+ });
+
+ window.addEventListener("mouseup", function () {
+ if (!dragging) return;
+ dragging = false;
+ handle.classList.remove("active");
+ document.body.style.cursor = "";
+ document.body.style.userSelect = "";
+ });
+
+ window.addEventListener("resize", function () {
+ if (window.innerWidth <= 768) sidebar.style.width = "";
+ });
+})();
diff --git a/src/ui/static/index.html b/src/ui/static/index.html
new file mode 100644
index 0000000..a06db16
--- /dev/null
+++ b/src/ui/static/index.html
@@ -0,0 +1,101 @@
+<!DOCTYPE html>
+<html lang="en" class="dark">
+<head>
+ <meta charset="utf-8">
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
+ <title>Needle Playground | Cactus</title>
+ <link rel="preconnect" href="https://fonts.googleapis.com">
+ <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
+ <link href="https://fonts.googleapis.com/css2?family=Geist:wght@400;500;600;700&family=Geist+Mono:wght@400;500&display=swap" rel="stylesheet">
+ <link rel="stylesheet" href="/static/style.css">
+</head>
+<body>
+ <header>
+ <div class="header-border">
+ <div class="header-inner">
+ <a class="header-logo" href="https://cactuscompute.com">
+ <img src="https://cactuscompute.com/assets/cactus_white.png" alt="Cactus" width="32" height="32">
+ <span>Cactus</span>
+ </a>
+ <nav class="header-nav">
+ <span class="nav-current">Needle Playground</span>
+ </nav>
+ <div class="header-spacer"></div>
+ <button class="tools-toggle" onclick="togglePanel()" aria-label="Toggle tools panel">
+ <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M12.22 2h-.44a2 2 0 0 0-2 2v.18a2 2 0 0 1-1 1.73l-.43.25a2 2 0 0 1-2 0l-.15-.08a2 2 0 0 0-2.73.73l-.22.38a2 2 0 0 0 .73 2.73l.15.1a2 2 0 0 1 1 1.72v.51a2 2 0 0 1-1 1.74l-.15.09a2 2 0 0 0-.73 2.73l.22.38a2 2 0 0 0 2.73.73l.15-.08a2 2 0 0 1 2 0l.43.25a2 2 0 0 1 1 1.73V20a2 2 0 0 0 2 2h.44a2 2 0 0 0 2-2v-.18a2 2 0 0 1 1-1.73l.43-.25a2 2 0 0 1 2 0l.15.08a2 2 0 0 0 2.73-.73l.22-.39a2 2 0 0 0-.73-2.73l-.15-.08a2 2 0 0 1-1-1.74v-.5a2 2 0 0 1 1-1.74l.15-.09a2 2 0 0 0 .73-2.73l-.22-.38a2 2 0 0 0-2.73-.73l-.15.08a2 2 0 0 1-2 0l-.43-.25a2 2 0 0 1-1-1.73V4a2 2 0 0 0-2-2z"/><circle cx="12" cy="12" r="3"/></svg>
+ <span>Tools</span>
+ </button>
+ </div>
+ </div>
+ </header>
+ <main>
+ <aside id="sidebar">
+ <div class="panel-section panel-tools">
+ <div class="panel-header">
+ <label class="panel-label">Tools JSON</label>
+ <label class="file-btn">
+ Upload
+ <input type="file" id="toolsFile" accept=".json" onchange="loadToolsFile(this)">
+ </label>
+ </div>
+ <textarea id="tools" spellcheck="false" placeholder="Paste tool definitions here...">[
+ {"name": "set_timer", "description": "Set a timer for the specified duration or end time.", "parameters": {"time_human": {"type": "string", "description": "The duration or target end time in human readable format e.g. '1 hour and 30 minutes', '45 minutes', 'for 1:50pm', 'at 13:30'.", "required": true}}},
+ {"name": "send_email", "description": "Send an email to a recipient.", "parameters": {"to": {"type": "string", "description": "The recipient's email address.", "required": true}, "subject": {"type": "string", "description": "The email subject line.", "required": true}, "body": {"type": "string", "description": "The email body text.", "required": true}}},
+ {"name": "create_note", "description": "Create a new note with the given text.", "parameters": {"text": {"type": "string", "description": "The text of the note.", "required": true}, "title": {"type": "string", "description": "Optional title for the note.", "required": false}}}
+]</textarea>
+ <button class="btn-finetune" onclick="openFinetuneModal()">Finetune on these tools</button>
+ </div>
+ </aside>
+ <div class="resize-handle" id="resizeHandle"></div>
+ <section class="query-area">
+ <div class="query-section">
+ <div class="panel-header">
+ <label class="panel-label">Query</label>
+ <span class="model-name" id="modelName"></span>
+ <label class="file-btn">
+ Load model
+ <input type="file" id="modelFile" accept=".pkl" onchange="loadModelFile(this)">
+ </label>
+ </div>
+ <div class="input-row">
+ <input id="query" type="text" placeholder="e.g. Set a 25 minute timer" autofocus>
+ <button class="btn-primary" id="sendBtn" onclick="send()">Run</button>
+ </div>
+ </div>
+ <div class="result-section">
+ <label class="panel-label">Result</label>
+ <pre class="result-box" id="result"></pre>
+ </div>
+ </section>
+ </main>
+
+ <div class="modal-overlay" id="modalOverlay">
+ <div class="modal" onclick="event.stopPropagation()">
+ <div class="modal-header">
+ <span class="modal-title">Finetune</span>
+ <button class="modal-close" id="modalCloseBtn" onclick="closeModal()">&times;</button>
+ </div>
+ <div class="modal-body">
+ <div class="modal-field">
+ <label class="modal-label">Gemini API Key</label>
+ <input type="password" id="ftApiKey" class="modal-input" placeholder="Paste your API key" autocomplete="off" spellcheck="false">
+ </div>
+ <div class="modal-steps" id="ftSteps">
+ <div class="modal-step" data-step="generating data"><span class="step-dot"></span>Generating data</div>
+ <div class="modal-step" data-step="training"><span class="step-dot"></span>Training</div>
+ </div>
+ <div class="ft-eval" id="ftEvalResults"></div>
+ </div>
+ <div class="modal-footer" id="ftFooter">
+ <button class="btn-primary modal-start" id="ftStartBtn" onclick="startFinetune()">Start Finetune</button>
+ </div>
+ </div>
+ </div>
+
+ <div class="toast" id="toast">
+ <span class="toast-msg" id="toastMsg"></span>
+ <button class="toast-close" onclick="dismissToast()">&times;</button>
+ </div>
+ <script src="/static/app.js"></script>
+</body>
+</html>
diff --git a/src/ui/static/style.css b/src/ui/static/style.css
new file mode 100644
index 0000000..a9b680c
--- /dev/null
+++ b/src/ui/static/style.css
@@ -0,0 +1,653 @@
+:root {
+ --background: #000000;
+ --foreground: #ffffff;
+ --card: #0a0a0a;
+ --primary: #ffffff;
+ --primary-foreground: #000000;
+ --muted-foreground: #a3a3a3;
+ --accent: #262626;
+ --accent-foreground: #ffffff;
+ --destructive: #FF3B30;
+ --border: #262626;
+ --input: #171717;
+ --ring: #262626;
+ --radius: 0.5rem;
+}
+
+*, *::before, *::after {
+ box-sizing: border-box;
+ margin: 0;
+ padding: 0;
+ border-color: var(--border);
+}
+
+body {
+ background: var(--background);
+ color: var(--foreground);
+ font-family: 'Geist', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
+ font-size: 14px;
+ line-height: 1.5;
+ height: 100vh;
+ display: flex;
+ flex-direction: column;
+ -webkit-font-smoothing: antialiased;
+ -moz-osx-font-smoothing: grayscale;
+}
+
+header {
+ position: sticky;
+ top: 0;
+ z-index: 50;
+ background: rgba(0, 0, 0, 0.8);
+ backdrop-filter: blur(12px);
+ -webkit-backdrop-filter: blur(12px);
+ flex-shrink: 0;
+}
+
+.header-border {
+ border-bottom: 1px solid var(--border);
+}
+
+.header-inner {
+ width: 100%;
+ height: 64px;
+ padding: 0 24px;
+ display: flex;
+ align-items: center;
+}
+
+.header-logo {
+ display: flex;
+ align-items: center;
+ gap: 8px;
+ text-decoration: none;
+ color: var(--foreground);
+}
+
+.header-logo img {
+ height: 32px;
+ width: auto;
+ object-fit: contain;
+}
+
+.header-logo span {
+ font-size: 18px;
+ font-weight: 600;
+ letter-spacing: -0.02em;
+}
+
+.header-nav {
+ display: flex;
+ align-items: center;
+ margin-left: 12px;
+ padding-left: 12px;
+ border-left: 1px solid var(--border);
+}
+
+.nav-current {
+ color: var(--muted-foreground);
+ font-size: 14px;
+}
+
+.header-spacer {
+ flex: 1;
+}
+
+.tools-toggle {
+ display: none;
+ align-items: center;
+ gap: 6px;
+ height: 32px;
+ padding: 0 12px;
+ background: transparent;
+ color: var(--muted-foreground);
+ border: 1px solid var(--border);
+ border-radius: calc(var(--radius) - 2px);
+ font-family: inherit;
+ font-size: 13px;
+ font-weight: 500;
+ cursor: pointer;
+ transition: all 150ms;
+}
+
+.tools-toggle:hover {
+ color: var(--foreground);
+ background: var(--accent);
+}
+
+main {
+ flex: 1;
+ display: flex;
+ overflow: hidden;
+ width: 100%;
+}
+
+aside {
+ width: 340px;
+ min-width: 200px;
+ max-width: 60vw;
+ flex-shrink: 0;
+ padding: 20px;
+ display: flex;
+ flex-direction: column;
+ gap: 16px;
+ overflow-y: auto;
+}
+
+.resize-handle {
+ width: 5px;
+ flex-shrink: 0;
+ cursor: col-resize;
+ background: transparent;
+ border-left: 1px solid var(--border);
+ transition: background 150ms;
+}
+
+.resize-handle:hover,
+.resize-handle.active {
+ background: var(--accent);
+}
+
+.panel-section {
+ display: flex;
+ flex-direction: column;
+ gap: 8px;
+}
+
+.panel-tools {
+ flex: 1;
+ min-height: 0;
+}
+
+.panel-header {
+ display: flex;
+ align-items: center;
+ justify-content: space-between;
+}
+
+.panel-label {
+ font-size: 13px;
+ font-weight: 600;
+ letter-spacing: -0.01em;
+}
+
+.file-btn {
+ font-size: 12px;
+ font-weight: 500;
+ color: var(--muted-foreground);
+ cursor: pointer;
+ transition: color 150ms;
+}
+
+.file-btn:hover {
+ color: var(--foreground);
+}
+
+.file-btn input {
+ display: none;
+}
+
+.model-name {
+ font-size: 12px;
+ font-family: 'Geist Mono', monospace;
+ color: var(--muted-foreground);
+}
+
+.btn-finetune {
+ flex-shrink: 0;
+ height: 36px;
+ padding: 0 14px;
+ background: var(--primary);
+ color: var(--primary-foreground);
+ border: none;
+ border-radius: var(--radius);
+ font-family: inherit;
+ font-size: 13px;
+ font-weight: 500;
+ cursor: pointer;
+ transition: background 150ms;
+ width: 100%;
+}
+
+.btn-finetune:hover {
+ background: rgba(255, 255, 255, 0.9);
+}
+
+.btn-finetune:disabled {
+ opacity: 0.5;
+ pointer-events: none;
+}
+
+textarea {
+ flex: 1;
+ font-family: 'Geist Mono', 'SFMono-Regular', 'Menlo', 'Monaco', 'Consolas', monospace;
+ font-size: 13px;
+ line-height: 1.6;
+ resize: none;
+ padding: 12px;
+ background: var(--card);
+ color: var(--foreground);
+ border: 1px solid var(--border);
+ border-radius: var(--radius);
+ outline: none;
+ transition: border-color 150ms;
+}
+
+textarea:focus {
+ border-color: var(--ring);
+ box-shadow: 0 0 0 3px rgba(38, 38, 38, 0.5);
+}
+
+textarea::placeholder {
+ color: var(--muted-foreground);
+}
+
+.query-area {
+ flex: 1;
+ display: flex;
+ flex-direction: column;
+ padding: 20px;
+ gap: 20px;
+ min-width: 0;
+}
+
+.query-section {
+ display: flex;
+ flex-direction: column;
+ gap: 8px;
+ flex-shrink: 0;
+}
+
+.input-row {
+ display: flex;
+ gap: 8px;
+}
+
+.input-row input {
+ flex: 1;
+ height: 40px;
+ padding: 0 14px;
+ background: var(--input);
+ color: var(--foreground);
+ border: 1px solid var(--border);
+ border-radius: var(--radius);
+ font-family: inherit;
+ font-size: 14px;
+ outline: none;
+ transition: border-color 150ms;
+}
+
+.input-row input:focus {
+ border-color: var(--ring);
+ box-shadow: 0 0 0 3px rgba(38, 38, 38, 0.5);
+}
+
+.input-row input::placeholder {
+ color: var(--muted-foreground);
+}
+
+.btn-primary {
+ height: 40px;
+ padding: 0 20px;
+ background: var(--primary);
+ color: var(--primary-foreground);
+ border: none;
+ border-radius: var(--radius);
+ font-family: inherit;
+ font-size: 14px;
+ font-weight: 500;
+ cursor: pointer;
+ transition: background 150ms;
+ white-space: nowrap;
+}
+
+.btn-primary:hover {
+ background: rgba(255, 255, 255, 0.9);
+}
+
+.btn-primary:disabled {
+ opacity: 0.5;
+ pointer-events: none;
+}
+
+.result-section {
+ flex: 1;
+ min-height: 0;
+ display: flex;
+ flex-direction: column;
+ gap: 8px;
+}
+
+.result-box {
+ flex: 1;
+ margin: 0;
+ padding: 14px;
+ background: var(--card);
+ border: 1px solid var(--border);
+ border-radius: var(--radius);
+ font-family: 'Geist Mono', 'SFMono-Regular', 'Menlo', 'Monaco', 'Consolas', monospace;
+ font-size: 13px;
+ line-height: 1.6;
+ color: var(--muted-foreground);
+ white-space: pre-wrap;
+ word-break: break-word;
+ overflow-y: auto;
+}
+
+.result-box.has-result {
+ color: var(--foreground);
+}
+
+.modal-overlay {
+ position: fixed;
+ inset: 0;
+ z-index: 60;
+ display: none;
+ align-items: center;
+ justify-content: center;
+ background: rgba(0, 0, 0, 0.6);
+ padding: 16px;
+}
+
+.modal-overlay.visible {
+ display: flex;
+}
+
+.modal {
+ width: 100%;
+ max-width: 520px;
+ max-height: 85vh;
+ background: var(--card);
+ border: 1px solid var(--border);
+ border-radius: 12px;
+ display: flex;
+ flex-direction: column;
+ overflow: hidden;
+}
+
+.modal-header {
+ display: flex;
+ align-items: center;
+ justify-content: space-between;
+ padding: 16px 20px;
+ border-bottom: 1px solid var(--border);
+ flex-shrink: 0;
+}
+
+.modal-title {
+ font-size: 15px;
+ font-weight: 600;
+}
+
+.modal-close {
+ width: 28px;
+ height: 28px;
+ display: flex;
+ align-items: center;
+ justify-content: center;
+ background: none;
+ border: none;
+ color: var(--muted-foreground);
+ font-size: 20px;
+ cursor: pointer;
+ border-radius: calc(var(--radius) - 2px);
+ transition: color 150ms, background 150ms;
+ padding: 0;
+ line-height: 1;
+}
+
+.modal-close:hover {
+ color: var(--foreground);
+ background: var(--accent);
+}
+
+.modal-body {
+ padding: 20px;
+ overflow-y: auto;
+ display: flex;
+ flex-direction: column;
+ gap: 16px;
+}
+
+.modal-field {
+ display: flex;
+ flex-direction: column;
+ gap: 6px;
+ flex: 1;
+}
+
+.modal-label {
+ font-size: 13px;
+ font-weight: 500;
+ color: var(--muted-foreground);
+}
+
+.modal-input {
+ height: 36px;
+ padding: 0 12px;
+ background: var(--input);
+ color: var(--foreground);
+ border: 1px solid var(--border);
+ border-radius: var(--radius);
+ font-family: inherit;
+ font-size: 13px;
+ outline: none;
+ transition: border-color 150ms;
+ width: 100%;
+}
+
+.modal-input:focus {
+ border-color: var(--ring);
+ box-shadow: 0 0 0 3px rgba(38, 38, 38, 0.5);
+}
+
+.modal-input::placeholder {
+ color: var(--muted-foreground);
+}
+
+.modal-input-sm {
+ font-family: 'Geist Mono', monospace;
+}
+
+.modal-row {
+ display: flex;
+ gap: 12px;
+}
+
+.modal-steps {
+ display: none;
+ gap: 4px;
+}
+
+.modal-steps.visible {
+ display: flex;
+ flex-direction: column;
+}
+
+.modal-step {
+ display: flex;
+ align-items: center;
+ gap: 8px;
+ font-size: 13px;
+ color: var(--muted-foreground);
+ padding: 4px 0;
+}
+
+.modal-step.active {
+ color: var(--foreground);
+}
+
+.modal-step.done {
+ color: #4ade80;
+}
+
+.step-dot {
+ width: 8px;
+ height: 8px;
+ border-radius: 50%;
+ background: var(--accent);
+ flex-shrink: 0;
+ transition: background 200ms;
+}
+
+.modal-step.active .step-dot {
+ background: var(--foreground);
+}
+
+.modal-step.done .step-dot {
+ background: #4ade80;
+}
+
+.ft-progress {
+ min-height: 20px;
+ margin-top: 4px;
+ color: var(--foreground);
+ font-family: 'Geist Mono', monospace;
+ font-size: 13px;
+}
+
+.modal-footer {
+ padding: 16px 20px;
+ border-top: 1px solid var(--border);
+ flex-shrink: 0;
+}
+
+.modal-start {
+ width: 100%;
+}
+
+.modal-download {
+ display: block;
+ width: 100%;
+ height: 40px;
+ line-height: 40px;
+ text-align: center;
+ background: var(--primary);
+ color: var(--primary-foreground);
+ border-radius: var(--radius);
+ font-family: inherit;
+ font-size: 14px;
+ font-weight: 500;
+ text-decoration: none;
+ transition: background 150ms;
+}
+
+.modal-download:hover {
+ background: rgba(255, 255, 255, 0.9);
+}
+
+.toast {
+ position: fixed;
+ top: 80px;
+ right: 24px;
+ display: flex;
+ align-items: flex-start;
+ gap: 10px;
+ max-width: 420px;
+ padding: 12px 14px;
+ background: #1a0000;
+ border: 1px solid rgba(255, 59, 48, 0.3);
+ border-radius: var(--radius);
+ color: #ff6b6b;
+ font-size: 13px;
+ line-height: 1.5;
+ z-index: 100;
+ opacity: 0;
+ transform: translateX(20px);
+ pointer-events: none;
+ transition: opacity 200ms, transform 200ms;
+}
+
+.toast.visible {
+ opacity: 1;
+ transform: translateX(0);
+ pointer-events: auto;
+}
+
+.toast-msg {
+ flex: 1;
+ word-break: break-word;
+}
+
+.toast-close {
+ flex-shrink: 0;
+ width: 20px;
+ height: 20px;
+ display: flex;
+ align-items: center;
+ justify-content: center;
+ background: none;
+ border: none;
+ color: #ff6b6b;
+ font-size: 16px;
+ cursor: pointer;
+ opacity: 0.6;
+ transition: opacity 150ms;
+ padding: 0;
+ line-height: 1;
+}
+
+.toast-close:hover {
+ opacity: 1;
+}
+
+.ft-validation, .ft-eval { margin-top: 12px; }
+.ft-section-title { font-size: 11px; font-weight: 600; text-transform: uppercase; letter-spacing: 0.05em; color: var(--muted-foreground); margin-bottom: 8px; }
+.ft-stat-row { display: flex; gap: 16px; font-size: 13px; color: var(--foreground); margin-bottom: 6px; flex-wrap: wrap; }
+.ft-stat-row span { white-space: nowrap; }
+.ft-warn { color: var(--destructive); font-size: 12px; margin-bottom: 6px; }
+.ft-table { width: 100%; border-collapse: collapse; font-size: 12px; margin-top: 6px; }
+.ft-table th { text-align: left; color: var(--muted-foreground); font-weight: 500; padding: 4px 8px; border-bottom: 1px solid var(--border); }
+.ft-table td { padding: 3px 8px; border-bottom: 1px solid var(--border); }
+.ft-table tbody tr:last-child td { border-bottom: none; }
+
+::-webkit-scrollbar { width: 6px; }
+::-webkit-scrollbar-track { background: transparent; }
+::-webkit-scrollbar-thumb { background: var(--accent); border-radius: 3px; }
+
+@media (max-width: 768px) {
+ .header-inner { padding: 0 16px; height: 56px; }
+ .header-nav { display: none; }
+ .tools-toggle { display: flex; }
+
+ main { flex-direction: column; min-height: 0; }
+
+ .resize-handle { display: none !important; }
+
+ aside {
+ width: 100% !important;
+ min-width: 0 !important;
+ max-width: none !important;
+ flex-shrink: 0;
+ border-right: none;
+ display: none !important;
+ padding: 16px;
+ overflow-y: auto;
+ }
+
+ aside.open {
+ display: flex !important;
+ flex: 1;
+ max-height: none;
+ }
+
+ aside.open .panel-tools { flex: 1; min-height: 0; }
+ aside.open textarea { flex: 1; min-height: 0; }
+
+ aside.open ~ .resize-handle,
+ aside.open ~ .query-area { display: none !important; }
+
+ .query-area { flex: 1; min-height: 0; padding: 16px; gap: 16px; }
+ .result-section { flex: 1; min-height: 0; }
+
+ .modal { max-width: none; max-height: none; height: 100%; border-radius: 0; }
+ .modal-overlay { padding: 0; }
+}
+
+@media (max-width: 480px) {
+ .header-inner { padding: 0 12px; }
+ .header-logo span { display: none; }
+ aside.open { padding: 12px; }
+ .query-area { padding: 12px; }
+ .btn-primary { padding: 0 14px; }
+}