diff --git a/strata_cli/launch/access.py b/strata_cli/launch/access.py index 2832896b..9ae286ca 100644 --- a/strata_cli/launch/access.py +++ b/strata_cli/launch/access.py @@ -148,19 +148,19 @@ def options(port: int, *, tunnel_url: str | None = None) -> list[AccessOption]: cf = find_cloudflared() ts = tailscale_status() out = [AccessOption("local", "This computer only", f"http://127.0.0.1:{port}", - "for apps on this computer (OpenCode, Codex, Claude Code...)", True)] + "for apps on this computer", True)] out.append(AccessOption("lan", "My local network", f"http://{lan or ''}:{port}", - "other computers at home or in the office" if lan else - "other computers on your network; no network address was found on this computer", bool(lan), warn=not lan)) + "other computers at home or the office" if lan else + "no network address found on this computer", bool(lan), warn=not lan)) out.append(AccessOption("cloudflare", "Cloudflare tunnel", tunnel_url or "https://.trycloudflare.com", - "from anywhere, no router setup · cloudflared found" if cf else "cloudflared is not installed", + "from anywhere · cloudflared found" if cf else "cloudflared is not installed", bool(cf), warn=not cf)) if ts["running"]: out.append(AccessOption("tailscale", "Tailscale", f"http://{ts['name'] or ts['ip']}:{port}", - f"your Tailscale devices · tailnet address {ts['ip']}", True)) + "your Tailscale devices", True)) else: out.append(AccessOption("tailscale", "Tailscale", f"http://:{port}", - "Tailscale is installed but not connected (run: tailscale up)" if ts["installed"] else + "Tailscale is not connected" if ts["installed"] else "Tailscale is not installed", False, warn=True)) return out diff --git a/strata_cli/launch/app.py b/strata_cli/launch/app.py new file mode 100644 index 00000000..017563dd --- /dev/null +++ b/strata_cli/launch/app.py @@ -0,0 +1,354 @@ +"""The `strata launch` application: the screens in order, the run, and the hand-over to the monitor. + +`run_tui` opens the screens (or, with settings already chosen, goes straight to the run), then the Strata Monitor with the +launch-aware extras: a Connect view (key 7) and a quit question (keep serving in the background, or stop). +""" + +from __future__ import annotations + +import json +import sys +import threading +from dataclasses import dataclass, field +from pathlib import Path +from typing import Any + +from rich.text import Text +from textual.app import App +from textual.binding import Binding + +from strata_cli.launch import access as acc +from strata_cli.launch import params as P +from strata_cli.launch import settings as S +from strata_cli.launch import store +from strata_cli.launch.catalog import Catalog +from strata_cli.launch.runner import Cancelled, Event, LaunchError, Result, Runner, read_config +from strata_cli.launch.screens import (AccessScreen, AdvancedScreen, CardsScreen, ConfirmScreen, ContextScreen, FolderScreen, + ModelScreen, ParallelScreen, PicturesScreen, RunScreen, SaveScreen, StartScreen, + TuningScreen, flow_for) +from strata_cli.launch.widgets import CSS, Glyphs + +SCREENS = {"model": ModelScreen, "cards": CardsScreen, "folder": FolderScreen, "context": ContextScreen, "pictures": PicturesScreen, + "parallel": ParallelScreen, "tuning": TuningScreen, "advanced": AdvancedScreen, "access": AccessScreen, "save": SaveScreen} + + +@dataclass +class Ctx: + """Everything the screens share: the catalog, this computer, the choices so far.""" + + catalog: Catalog + computer: Any + rows: list + settings: S.LaunchSettings + glyphs: Glyphs = field(default_factory=Glyphs) + feed: str | None = None + notes: list[str] = field(default_factory=list) + start_own_files: bool = False + picked_in_list: bool = False + tune_chosen: bool = False + fresh_setup: bool = True + engine_note: str = "" + engine_flags: list[str] | None = None + _adv: list | None = None + _help: str | None = None + + # ---- names and warnings + def default_name(self) -> str: + s = self.settings + base = f"{s.model or 'my model'} {S.context_label(s.context)}" + names = {p.name for p in store.list_presets()} + if base not in names: + return base + n = 2 + while f"{base} ({n})" in names: + n += 1 + return f"{base} ({n})" + + def warnings(self) -> list[str]: + s = self.settings + out = [n.line() for n in S.check_access(s)] + out += [n.line() for n in S.check_parallel(s, vram_gb=self.computer.main_vram_gb or None)] + out += [n.line() for n in S.check_context(s)] + r = next((x for x in self.rows if (x.family, x.model) == (s.family, s.model)), None) + if r is not None and r.verdict != "fits": + out.append(f"{r.title}: {r.words}.") + missing = acc.missing_helper(s.access) + if missing: + out.append(missing) + return out + + # ---- the installed model's configuration (for the Advanced table and the command) + def base_config(self) -> dict | None: + s = self.settings + if not (s.family and s.model): + return None + path = self.catalog.installed_config(self.catalog.tag_of(s.family, s.model)) + if path is None: + return None + try: + return read_config(path) + except (OSError, ValueError): + return None + + def engine_exe(self) -> str | None: + try: + from strata_control import engine_component as ec + rep = ec.status() + return (rep.get("engine") or {}).get("exe") if rep.get("usable") else None + except Exception: # noqa: BLE001 + return None + + def invalidate_rows(self) -> None: + self._adv = None + + def advanced_rows(self) -> list[P.Row]: + if self._adv is not None: + return self._adv + s = self.settings + cfg = self.base_config() + base_args = list((cfg or {}).get("args") or []) + base_env = dict((cfg or {}).get("env") or {}) + tuned_args, tuned_env = None, None + if cfg: + try: + from strata_control import presets + merged, info = presets.overlay(cfg) + if info.get("applied"): + tuned_args, tuned_env = list(merged.get("args") or []), dict(merged.get("env") or {}) + except Exception: # noqa: BLE001 + pass + exe = self.engine_exe() + text = P.engine_help_text(exe) if exe else None + if exe and text: + self.engine_flags = sorted({p.name for p in P.parse_help(text)}) + self.engine_note = "" + else: + self.engine_flags = None + self.engine_note = ("The engine is not installed yet, so this list shows only what this launch uses. The full list " + "appears after the first launch installs it.") if not exe else \ + "The engine's own list could not be read here; names are not checked." + if not cfg and not s.extra: + self.engine_note = self.engine_note or "The model is not installed yet; the defaults and tuned values appear after it is." + self._adv = P.build_rows(text, base_args, tuned_args, base_env, tuned_env, s.extra) + return self._adv + + def command_text(self) -> str: + s = self.settings + name = "launch-" + S.slug(s.name or self.default_name()) + cfg = self.base_config() + line = f"strata runtime start --profile {name}" + if cfg is None: + return line + " (the engine command is shown once the model is installed)" + try: + from strata_control import tune_knobs + from strata_cli.launch.runner import derive_config + from serve.server import engine_args + import contextlib + import io + d = derive_config(cfg, s, bool_flags=tune_knobs.BOOL_FLAGS) + if s.parallel: + d["parallel"] = s.parallel + with contextlib.redirect_stdout(io.StringIO()): + argv = engine_args(d) + argv = ["***" if (i and argv[i - 1] in ("--api-key",)) else a for i, a in enumerate(argv)] + return f"{Path(str(d.get('exe', 'engine'))).name} {' '.join(str(a) for a in argv)}" + except Exception: # noqa: BLE001 + return line + + +class LaunchApp(App): + TITLE = "strata launch" + CSS = CSS + + def __init__(self, ctx: Ctx, *, auto_run: bool = False) -> None: + super().__init__() + self.ctx = ctx + self.history: list[str] = [] + self.result: Result | None = None + self.stopped_after_save = False + self.cancel = threading.Event() + self.auto_run = auto_run + self.runner: Runner | None = None + self.run_screen: RunScreen | None = None + self.running = False + + def on_mount(self) -> None: + if self.auto_run: + self.run_launch() + else: + self.push_screen(StartScreen()) + + # ---- navigation + def flow(self) -> list[str]: + return flow_for(self.ctx) + + def goto(self, step_id: str) -> None: + self.current = step_id + self.switch_screen(SCREENS[step_id]()) + + def begin_setup(self, *, preset: str | None = None, own_files: bool = False) -> None: + self.ctx.tune_chosen = False + self.ctx.fresh_setup = not preset + if preset: + s = store.load(preset) + self.ctx.settings = s + else: + self.ctx.settings = S.LaunchSettings() + self.ctx.start_own_files = own_files + self.ctx.picked_in_list = False + self.ctx.invalidate_rows() + self.current = "model" + self.switch_screen(ModelScreen()) + + def advance(self) -> None: + flow = self.flow() + i = flow.index(self.current) if self.current in flow else -1 + if i + 1 < len(flow): + self.goto(flow[i + 1]) + + def back(self) -> None: + flow = self.flow() + i = flow.index(self.current) if self.current in flow else 0 + if i > 0: + self.goto(flow[i - 1]) + else: + self.current = "start" + self.switch_screen(StartScreen()) + + # ---- starting + def start_preset(self, name: str) -> None: + s = store.load(name) + self.ctx.settings = s + self.run_launch() + + def run_launch(self) -> None: + s = self.ctx.settings + self.cancel = threading.Event() + self.runner = Runner(s, catalog=self.ctx.catalog, emit=self._emit, cancel=self.cancel, feed=self.ctx.feed) + screen = RunScreen() + self.run_screen = screen + self.current = "run" + self.switch_screen(screen) + self.running = True + threading.Thread(target=self._work, daemon=True).start() + + def _emit(self, ev: Event) -> None: + screen = self.run_screen + if screen is not None: + try: + self.call_from_thread(screen.on_event, ev) + except Exception: # noqa: BLE001 - the app is closing + pass + + def _work(self) -> None: + screen = self.run_screen + try: + result = self.runner.run() # type: ignore[union-attr] + except Cancelled: + self.running = False + self.call_from_thread(self._after_cancel) + return + except LaunchError as e: + self.running = False + self.call_from_thread(screen.on_failure, f"{e.text} {e.next}".strip()) + return + except Exception as e: # noqa: BLE001 + self.running = False + self.call_from_thread(screen.on_failure, f"Something unexpected went wrong: {type(e).__name__}: {e}") + return + self.running = False + s = self.ctx.settings + try: + if s.name and store.load(s.name, missing_ok=True) is not None: + store.save(s, s.name) + store.touch_used(s.name) + except Exception: # noqa: BLE001 + pass + self.result = result + self.call_from_thread(screen.on_finished) + self.call_from_thread(self.exit, result) + + def _after_cancel(self) -> None: + self.run_screen.on_failure("Stopped. Nothing was started.") # type: ignore[union-attr] + + def stop_run(self, screen) -> None: + if not self.running: + if screen.failed: + self.back_to_start() + else: + self.exit(None) + return + + def done(choice: str | None) -> None: + if choice == "stop": + self.cancel.set() + + self.push_screen(ConfirmScreen("Stop now?", "Files already downloaded are kept; a download continues where it stopped next time. " + "Nothing is started.", [("stop", "Stop"), ("go", "Keep going")]), done) + + def back_to_start(self) -> None: + self.current = "start" + self.switch_screen(StartScreen()) + + +# ------------------------------------------------------------------------------------------------ the monitor + +def _info_for(result: Result, s: S.LaunchSettings) -> dict: + cfg = {} + try: + cfg = read_config(Path(result.config)) + except (OSError, ValueError): + pass + return {"url": result.urls[0] if result.urls else f"http://127.0.0.1:{s.port}", "urls": result.urls, "key": result.key, + "model": cfg.get("model_name") or "strata", "name": s.name, "profile": result.profile, "running": True, + "tunnel": result.tunnel} + + +def run_monitor(info: dict, *, keep_default: bool = True, color: str = "auto") -> int: + """Open the monitor for a running launch. q asks whether to keep serving or stop.""" + from strata_cli.client import Endpoint + from strata_cli.launch.monitor_ext import LaunchMonitor + local = next((u for u in info.get("urls", []) if "127.0.0.1" in u), None) or f"http://127.0.0.1:{info.get('port', S.DEFAULT_PORT)}" + endpoint = Endpoint(local, info.get("key"), 3.0, "") + app = LaunchMonitor(endpoint, None, info=info, color=color) + app.run() + if app.stop_requested: + from strata_cli.launch.runner import Runner as R + from strata_control import runtime + try: + runtime.stop(info["profile"], timeout=180) + except Exception as e: # noqa: BLE001 + print(f"strata: could not stop the server: {e}", file=sys.stderr) + return 6 + acc.stop_tunnel(info["profile"]) + print("Stopped. The model is unloaded and the graphics memory is free.") + else: + print(f"Still serving at {info['url']}. Watch it again: strata monitor --url {info['url']} Stop: strata launch --stop") + return 0 + + +def run_tui(settings: S.LaunchSettings | None, *, catalog: Catalog | None = None, feed: str | None = None, auto_run: bool = False, + save: bool = False) -> int: + """Open the launch screens. `settings` given and `auto_run`: skip the questions and go straight to the run.""" + catalog = catalog or Catalog() + print("Looking at this computer…", flush=True) + computer = catalog.computer() + rows = catalog.rows(computer.raw) + from strata_cli.launch.cmd import apply_defaults + s = settings or S.LaunchSettings() + notes: list[S.Notice] = [] + if settings is not None: + s, notes = apply_defaults(s, catalog) + if not s.name: + s.name = f"{s.model or 'model'} {S.context_label(s.context)}" + if save: + store.save(s, s.name) + ctx = Ctx(catalog=catalog, computer=computer, rows=rows, settings=s, glyphs=Glyphs(), feed=feed) + app = LaunchApp(ctx, auto_run=auto_run) + result = app.run() + if app.stopped_after_save: + print(f"Saved the preset '{ctx.settings.name}'. Start it with: strata launch --preset \"{ctx.settings.name}\"") + return 0 + if result is None: + return 0 + return run_monitor(_info_for(result, ctx.settings)) diff --git a/strata_cli/launch/catalog.py b/strata_cli/launch/catalog.py index a911a565..ff4d1ab6 100644 --- a/strata_cli/launch/catalog.py +++ b/strata_cli/launch/catalog.py @@ -70,12 +70,12 @@ def verdict(on_this_pc: str | None) -> tuple[str, str]: if not text: return TIGHT, "not checked (this computer's memory could not be read)" if low.startswith("does not fit"): - return NO, "does not fit (you can still try)" + return NO, "does not fit (can still try)" if "low-ram mode" in low: - return FITS, "fits (slower: part of it stays in RAM or on the SSD)" + return FITS, "fits, a bit slower" if "part of its experts" in low or "read from the ssd" in low: base = "slow here" in low - return TIGHT, ("tight: part is read from the SSD, noticeably slower" if base else "fits, with part read from the SSD") + return (TIGHT, "tight: much read from SSD") if base else (FITS, "fits, part read from SSD") if low.startswith("tight"): return TIGHT, "tight: close other programs" if "fits" in low: diff --git a/strata_cli/launch/check.py b/strata_cli/launch/check.py new file mode 100644 index 00000000..4399906e --- /dev/null +++ b/strata_cli/launch/check.py @@ -0,0 +1,150 @@ +"""Check my harness: send one simple prompt and one tool call to the running server the way an app configured by +`strata connect` would, and say in plain words whether each worked. + +The check uses the harness's own wire format (chat completions, responses, or messages) with the address, key and model name its +config carries, so it proves the settings `strata connect` prints reach a server that answers correctly. It does not start the app +itself. The prompt's answer text is checked (it must contain the expected word) and the tool call's arguments are checked (the +city must be named), not just the HTTP status. +""" + +from __future__ import annotations + +import json +import time +import urllib.error +import urllib.request +from dataclasses import dataclass + +#: harness id -> the endpoint family it speaks (the others use chat completions) +WIRE = {"codex": "responses", "claude-code": "messages"} + +PROMPT = "Reply with exactly one word: pong" +TOOL_PROMPT = "What is the weather in Paris? Use the get_weather tool." +TOOL_SCHEMA = {"type": "object", "properties": {"city": {"type": "string"}}, "required": ["city"]} + + +@dataclass +class Check: + name: str # "prompt" | "tool call" + ok: bool + detail: str # plain words + seconds: float = 0.0 + + +def _post(url: str, key: str, body: dict, headers: dict | None = None, timeout: float = 240.0) -> dict: + h = {"Content-Type": "application/json", "Accept": "application/json", **(headers or {})} + if key: + h["Authorization"] = "Bearer " + key + h["x-api-key"] = key + req = urllib.request.Request(url, data=json.dumps(body).encode("utf-8"), headers=h, method="POST") + with urllib.request.urlopen(req, timeout=timeout) as r: + return json.loads(r.read().decode("utf-8", "replace")) + + +def wire_for(harness_id: str) -> str: + return WIRE.get(harness_id, "chat") + + +def _prompt_request(wire: str, base: str, model: str) -> tuple[str, dict]: + if wire == "responses": + return base + "/v1/responses", {"model": model, "input": PROMPT, "max_output_tokens": 256} + if wire == "messages": + return base + "/v1/messages", {"model": model, "max_tokens": 256, "messages": [{"role": "user", "content": PROMPT}]} + return base + "/v1/chat/completions", {"model": model, "max_tokens": 256, "messages": [{"role": "user", "content": PROMPT}]} + + +def _tool_request(wire: str, base: str, model: str) -> tuple[str, dict]: + if wire == "responses": + return base + "/v1/responses", {"model": model, "input": TOOL_PROMPT, "max_output_tokens": 512, "tool_choice": "auto", + "tools": [{"type": "function", "name": "get_weather", "description": "Weather of a city", + "parameters": TOOL_SCHEMA}]} + if wire == "messages": + return base + "/v1/messages", {"model": model, "max_tokens": 512, "messages": [{"role": "user", "content": TOOL_PROMPT}], + "tools": [{"name": "get_weather", "description": "Weather of a city", "input_schema": TOOL_SCHEMA}]} + return base + "/v1/chat/completions", {"model": model, "max_tokens": 512, "messages": [{"role": "user", "content": TOOL_PROMPT}], + "tools": [{"type": "function", "function": {"name": "get_weather", "description": "Weather of a city", + "parameters": TOOL_SCHEMA}}]} + + +def answer_text(wire: str, data: dict) -> str: + if wire == "responses": + if isinstance(data.get("output_text"), str): + return data["output_text"] + parts = [] + for item in data.get("output") or []: + for c in item.get("content") or []: + if isinstance(c, dict) and c.get("text"): + parts.append(c["text"]) + return "".join(parts) + if wire == "messages": + return "".join(b.get("text", "") for b in data.get("content") or [] if isinstance(b, dict) and b.get("type") == "text") + try: + return data["choices"][0]["message"].get("content") or "" + except (KeyError, IndexError, TypeError, AttributeError): + return "" + + +def tool_calls(wire: str, data: dict) -> list[tuple[str, dict]]: + out: list[tuple[str, dict]] = [] + try: + if wire == "responses": + for item in data.get("output") or []: + if item.get("type") == "function_call": + out.append((item.get("name", ""), json.loads(item.get("arguments") or "{}"))) + elif wire == "messages": + for b in data.get("content") or []: + if isinstance(b, dict) and b.get("type") == "tool_use": + out.append((b.get("name", ""), b.get("input") or {})) + else: + for tc in data["choices"][0]["message"].get("tool_calls") or []: + fn = tc.get("function") or {} + out.append((fn.get("name", ""), json.loads(fn.get("arguments") or "{}"))) + except (KeyError, IndexError, TypeError, ValueError, AttributeError): + return out + return out + + +def run_checks(harness_id: str, url: str, key: str, model: str, *, timeout: float = 240.0, poster=_post) -> list[Check]: + """The two checks for one harness. `poster` is replaceable for tests.""" + base = url.rstrip("/") + wire = wire_for(harness_id) + checks: list[Check] = [] + t0 = time.monotonic() + try: + u, body = _prompt_request(wire, base, model) + data = poster(u, key, body, None, timeout) + text = answer_text(wire, data).strip() + ok = "pong" in text.lower() + checks.append(Check("prompt", ok, f"It answered: {text[:60]!r}." if ok else + (f"It answered {text[:60]!r}, not the word asked for." if text else "The server replied but with no text."), + time.monotonic() - t0)) + except Exception as e: # noqa: BLE001 + checks.append(Check("prompt", False, _why(e), time.monotonic() - t0)) + t1 = time.monotonic() + try: + u, body = _tool_request(wire, base, model) + data = poster(u, key, body, None, timeout) + calls = tool_calls(wire, data) + hit = next(((n, a) for n, a in calls if n == "get_weather" and "paris" in json.dumps(a).lower()), None) + if hit: + checks.append(Check("tool call", True, f"It called {hit[0]} with {json.dumps(hit[1])}.", time.monotonic() - t1)) + elif calls: + checks.append(Check("tool call", False, f"It called {calls[0][0]} with {json.dumps(calls[0][1])[:60]}, not the city asked for.", + time.monotonic() - t1)) + else: + checks.append(Check("tool call", False, "The model answered in words and did not call the tool.", time.monotonic() - t1)) + except Exception as e: # noqa: BLE001 + checks.append(Check("tool call", False, _why(e), time.monotonic() - t1)) + return checks + + +def _why(e: Exception) -> str: + if isinstance(e, urllib.error.HTTPError): + if e.code in (401, 403): + return "The server refused the key. Check the API key in the config." + if e.code == 404: + return "The server does not have that address. Check the server address and the app's endpoint type." + return f"The server answered with an error (HTTP {e.code})." + if isinstance(e, urllib.error.URLError): + return "The server could not be reached. Is it running (strata launch --status)?" + return f"The check could not finish: {type(e).__name__}." diff --git a/strata_cli/launch/monitor_ext.py b/strata_cli/launch/monitor_ext.py new file mode 100644 index 00000000..f811bc32 --- /dev/null +++ b/strata_cli/launch/monitor_ext.py @@ -0,0 +1,255 @@ +"""The Strata Monitor as `strata launch` opens it: the same monitor plus a Connect view and a quit question. + +Everything about the monitor itself (the views, colours, polling, key hints) is `strata_cli.monitor.MonitorApp`, unchanged. +This subclass adds +* key 7: the Connect view (the address, the API key hint and copy-ready setup for each app, with `c` copy, `w` write the config for + you after a backup, `h` check that the app's settings reach a server that answers a prompt and a tool call correctly); +* q: asks whether to keep serving in the background or stop (stopping frees the graphics memory). +""" + +from __future__ import annotations + +import threading + +from rich.text import Text +from textual.binding import Binding +from textual.widgets import Static + +from strata_cli.launch import check, harness +from strata_cli.launch.screens import ConfirmScreen +from strata_cli.launch.widgets import ACCENT, BAD, MUTE, WARN, Glyphs +from strata_cli.monitor import MonitorApp +from strata_cli.render import VIEWS, safe + + +class LaunchMonitor(MonitorApp): + BINDINGS = MonitorApp.BINDINGS + [Binding("7", "connect", "Connect view", show=False)] + + def __init__(self, endpoint, snapshot, *, info: dict, **kw) -> None: + super().__init__(endpoint, snapshot, **kw) + self.info = info + self.connect_open = False + self.connect_index = 0 + self.show_key = False + self.check_results: dict[str, list[check.Check] | str] = {} + self.glyphs = Glyphs() + self.stop_requested = False + self.notice = "" + + # ---- quit asks + def action_quit(self) -> None: + if self.closing: + return + + def done(choice: str | None) -> None: + if choice == "keep": + self.closing = True + self.exit() + elif choice == "stop": + self.stop_requested = True + self.closing = True + self.exit() + + self.push_screen(ConfirmScreen("Leave the monitor?", "Keeping it serving lets your apps go on using the model. " + "Stopping unloads the model and frees the graphics memory.", + [("keep", "Keep serving in the background"), ("stop", "Stop serving and free the memory"), + ("cancel", "Stay in the monitor")]), done) + + # ---- the Connect view + def _harness(self) -> harness.Harness: + return harness.HARNESSES[self.connect_index % len(harness.HARNESSES)] + + def action_connect(self) -> None: + self.connect_open = not self.connect_open + self.notice = "" + self._paint() + + def _set_view(self, index: int) -> None: + self.connect_open = False + super()._set_view(index) + + def action_next_row(self) -> None: + if self.connect_open: + self.connect_index = (self.connect_index + 1) % len(harness.HARNESSES) + self.notice = "" + self._paint() + else: + super().action_next_row() + + def action_previous_row(self) -> None: + if self.connect_open: + self.connect_index = (self.connect_index - 1) % len(harness.HARNESSES) + self.notice = "" + self._paint() + else: + super().action_previous_row() + + def action_filter_clear(self) -> None: + if self.connect_open: + self.connect_open = False + self._paint() + else: + super().action_filter_clear() + + def action_request_detail(self) -> None: + if not self.connect_open: + super().action_request_detail() + + def action_next_view(self) -> None: + if self.connect_open: + self.connect_open = False + self._paint() + super().action_next_view() + + def on_key(self, event) -> None: + if not self.connect_open: + return + key = event.key + if key == "c": + self._copy() + elif key == "w": + self._write() + elif key == "k": + self.show_key = not self.show_key + self._paint() + elif key == "h": + self._check() + else: + return + event.stop() + + def _rendered(self) -> dict: + h = self._harness() + key = self.info.get("key") or "" + return harness.render(h, url=self.info["url"], key=key or "", model=self.info.get("model") or "strata") + + def _copy(self) -> None: + out = self._rendered() + text = "\n\n".join(f"# {f['path']}\n{f['text']}" for f in out["files"]) + if out["env"]: + text += "\n\n" + "\n".join(f"{k}={v}" for k, v in out["env"].items()) + try: + self.copy_to_clipboard(text) + self.notice = "Copied (if your terminal allows it)." + except Exception: # noqa: BLE001 + self.notice = "! Your terminal does not allow copying; use: strata connect " + self._harness().id + self._paint() + + def _write(self) -> None: + h = self._harness() + out = self._rendered() + if not out["files"]: + self.notice = "! Nothing to write for this app." + self._paint() + return + + def done(choice: str | None) -> None: + if choice != "write": + return + lines = [] + for f in out["files"]: + try: + r = harness.write_file(f) + lines.append(f"{r['action']} {r['path']}" + (f" (backup {r['backup']})" if r["backup"] else "")) + except OSError as e: + lines.append(f"could not write {f['path']}: {e}") + self.notice = "; ".join(lines) + self._paint() + + self.push_screen(ConfirmScreen(f"Write {h.title}'s settings?", "A file that exists is copied to a backup first.\n" + + "\n".join(f" {f['path']}" for f in out["files"]), + [("write", "Write it"), ("no", "Not now")]), done) + + def _check(self) -> None: + h = self._harness() + self.check_results[h.id] = "running" + self.notice = "Checking: one prompt and one tool call. This can take a minute on a big model." + self._paint() + key = self.info.get("key") or "" + url = self.info["url"] + model = self.info.get("model") or "strata" + + def work() -> None: + res = check.run_checks(h.id, url, key, model) + self.check_results[h.id] = res + try: + self.call_from_thread(self._paint) + except Exception: # noqa: BLE001 + pass + + threading.Thread(target=work, daemon=True).start() + + # ---- painting + def _paint(self, size=None) -> None: + if not getattr(self, "connect_open", False): + return super()._paint(size) + size = self.screen.size if size is None else size + width = max(40, size.width - 2) + self.query_one("#content", Static).update(self.connect_text(width)) + idle = "c copy w write it for me (keeps a backup) h check it 1-7 views q quit (asks: keep serving / stop)" + msg = self.notice or idle + self.query_one("#message", Static).update(Text(msg, style=WARN if msg.startswith("!") else "")) + + def connect_text(self, width: int) -> Text: + g = self.glyphs + state = {"connected": "[LIVE]", "stale": "[STALE]", "connecting": "[CONNECTING]", "disconnected": "[DISCONNECTED]", + "auth_required": "[AUTH REQUIRED]"}.get(self.connection_state, "[LIVE]") + t = Text(no_wrap=True, overflow="crop") + head = f"STRATA Connect {self.info['url']}" + t.append(head, style=f"bold {ACCENT}") + t.append(" " * max(2, width - len(head) - len(state)) + state + "\n", style=f"bold {ACCENT}") + nav = [] + for i, name in enumerate(VIEWS): + nav.append(f"{i + 1} {name.title()}") + nav.append("[7 Connect]") + t.append(" ".join(nav) + "\n") + t.append(g.rule * min(width, 100) + "\n", style=MUTE) + key = self.info.get("key") or "" + shown = key if self.show_key else (key[:10] + g.mask * 4 if key else "off") + t.append("Connect an app", style=f"bold {ACCENT}") + t.append(f" URL {self.info['url']}/v1 model {self.info.get('model') or 'strata'} key {shown} ") + t.append("(k hide)" if self.show_key else "(k show)", style=MUTE) + t.append("\n") + for u in self.info.get("urls", [])[1:]: + t.append(f" also reachable at {u}/v1\n", style=MUTE) + t.append("\n " + "App".ljust(20) + "Works with".ljust(22) + "Status\n", style="bold") + for i, h in enumerate(harness.HARNESSES): + sel = i == self.connect_index % len(harness.HARNESSES) + res = self.check_results.get(h.id) + status = h.status + (f" {g.tick}" if h.tested else "") + style = "" + if res == "running": + status, style = "checking…", WARN + elif isinstance(res, list): + okn = sum(1 for c in res if c.ok) + status, style = (f"works {g.tick} ({okn}/{len(res)} checks)" if okn == len(res) else f"problem ({okn}/{len(res)} checks)"), \ + (ACCENT if okn == len(res) else BAD) + row = (" " + (g.cursor + " " if sel else " ")) + h.title.ljust(20) + h.speaks.ljust(22) + status + t.append(row.ljust(width - 1) + "\n", style=(f"bold {ACCENT} on #1f2a27" if sel else style)) + h = self._harness() + out = self._rendered() + t.append("\n") + paths = ", ".join(f["path"] for f in out["files"]) or ("built into the Strata app" if h.built_in else "") + t.append(f"{h.title} setup", style=f"bold {ACCENT}") + t.append(f" {paths}\n", style=MUTE) + if not h.tested and not h.built_in: + t.append(" Not tested with this app yet: a starting point, not a promise.\n", style=WARN) + for n in h.notes: + t.append(f" {n}\n", style=MUTE) + shown_lines = [] + for f in out["files"][:1]: + body = f["text"] if self.show_key else harness.mask(f["text"], key) + shown_lines += body.splitlines() + for k, v in out["env"].items(): + shown_lines.append(f"{k}={v if self.show_key else harness.mask(v, key)}") + room = max(3, 0) + for line in shown_lines[:14]: + t.append(" " + line + "\n") + if len(shown_lines) > 14: + t.append(f" … {len(shown_lines) - 14} more lines (c copies all)\n", style=MUTE) + res = self.check_results.get(h.id) + if isinstance(res, list): + t.append("\n") + for c in res: + t.append(f" {g.tick if c.ok else 'x'} {c.name}: {c.detail}\n", style=ACCENT if c.ok else BAD) + return t diff --git a/strata_cli/launch/screens.py b/strata_cli/launch/screens.py new file mode 100644 index 00000000..9a28af77 --- /dev/null +++ b/strata_cli/launch/screens.py @@ -0,0 +1,1341 @@ +"""The screens of `strata launch`: the saved-preset start screen, the nine setup steps, the confirm pop-up and the progress screen. + +One decision per screen. Every screen has a short plain explanation under the content (what it means, what to do next) and a line +of key hints. Esc goes back, Enter goes on, q leaves (and says what is kept). Choices are written into `app.ctx.settings` +(strata_cli/launch/settings.py); nothing is installed or started until the last screen. +""" + +from __future__ import annotations + +import os +import shutil +import threading +from pathlib import Path +from typing import Any + +from rich.text import Text +from textual.app import ComposeResult +from textual.binding import Binding +from textual.containers import Vertical +from textual.screen import ModalScreen, Screen +from textual.widgets import Input, Static + +from strata_cli.launch import access as acc +from strata_cli.launch import params as P +from strata_cli.launch import settings as S +from strata_cli.launch import store +from strata_cli.launch.widgets import (ACCENT, BAD, FG, MUTE, WARN, Cell, Item, Menu, bar) + +STEPS = (("model", "Model"), ("cards", "Cards"), ("folder", "Folder"), ("context", "Context"), ("pictures", "Pictures"), + ("parallel", "Parallel"), ("tuning", "Tuning"), ("advanced", "Advanced"), ("access", "Access"), ("save", "Save")) + + +def flow_for(ctx) -> list[str]: + """The steps of this computer: the Cards step exists only when two or more graphics cards can be used.""" + ids = [sid for sid, _ in STEPS] + if len(ctx.computer.usable_gpus) < 2: + ids.remove("cards") + return ids + + +def _t(*parts: tuple[str, str] | str) -> Text: + out = Text(no_wrap=False) + for p in parts: + if isinstance(p, tuple): + out.append(p[0], style=p[1]) + else: + out.append(p) + return out + + +class Base(Screen): + """Shared frame: heading line, rule, content, rule, explanation, key hints, message.""" + + BINDINGS = [ + Binding("escape", "back", "Back", show=False), Binding("q", "quit_app", "Quit", show=False), + Binding("question_mark", "help", "Help", show=False), Binding("f1", "help", "Help", show=False), + Binding("down", "move(1)", show=False), Binding("up", "move(-1)", show=False), + Binding("pagedown", "page(1)", show=False), Binding("pageup", "page(-1)", show=False), + Binding("enter", "next", "Next", show=False), + ] + + step_id = "" + title_line = "Set up" + hints = "↑↓ choose Enter next Esc back ? help q quit" + help_text = "Up/Down choose, Enter next, Esc back, q quit. Nothing is installed or started until the last screen." + + def __init__(self) -> None: + super().__init__() + self.message = "" + + # ---- frame + @property + def ctx(self): + return self.app.ctx # type: ignore[attr-defined] + + @property + def g(self): + return self.ctx.glyphs + + def compose(self) -> ComposeResult: + with Vertical(id="shell"): + yield Static("", id="head", markup=False) + yield Static("", id="rule1", markup=False) + with Vertical(id="body"): + yield from self.body() + yield Static("", id="rule2", markup=False) + yield Static("", id="explain", markup=False) + yield Static("", id="hints", markup=False) + yield Static("", id="message", markup=False) + + def body(self) -> ComposeResult: + yield from () + + def on_mount(self) -> None: + self.paint() + + def on_resize(self, _e) -> None: + self.paint() + + def width(self) -> int: + return max(40, (self.size.width or 100) - 2) + + def rule(self) -> str: + return self.g.rule * self.width() + + def head_text(self) -> Text: + flow = flow_for(self.ctx) + out = Text(no_wrap=True, overflow="crop") + out.append("STRATA ", style=f"bold {ACCENT}") + out.append(f"{self.title_line}", style=f"bold {ACCENT}") + if self.step_id in flow: + labels = dict(STEPS) + n = flow.index(self.step_id) + if self.width() >= 118: + out.append(" ") + for i, sid in enumerate(flow): + mark = f"[{i + 1} {labels[sid]}]" if i == n else f"{i + 1} {labels[sid]}" + out.append(mark + " ", style=f"bold {ACCENT}" if i == n else "") + else: + out.append(f" Step {n + 1} of {len(flow)}: {labels[self.step_id]}", style="") + return out + + def paint(self) -> None: + try: + self.query_one("#head", Static).update(self.head_text()) + self.query_one("#rule1", Static).update(Text(self.rule(), style=MUTE)) + self.query_one("#rule2", Static).update(Text(self.rule(), style=MUTE)) + self.query_one("#explain", Static).update(self.explain()) + self.query_one("#hints", Static).update(Text(self.hints_text(), style=MUTE)) + self.query_one("#message", Static).update(Text(self.message, style=WARN if self.message.startswith("!") else MUTE)) + except Exception: # noqa: BLE001 - a screen that is closing + return + self.refresh_body() + + def hints_text(self) -> str: + return self.hints.replace("↑↓", self.g.updown) + + def explain(self) -> Text: + return Text("") + + def refresh_body(self) -> None: + pass + + def say(self, text: str, warn: bool = False) -> None: + self.message = ("! " if warn else "") + text + self.paint() + + # ---- actions + def menu(self) -> Menu | None: + try: + return self.query_one(Menu) + except Exception: # noqa: BLE001 + return None + + def action_move(self, step: int) -> None: + m = self.menu() + if m is not None and not self.input_open(): + m.move(step) + self.on_cursor() + self.paint() + + def action_page(self, step: int) -> None: + m = self.menu() + if m is not None: + m.page(step) + self.on_cursor() + self.paint() + + def on_cursor(self) -> None: + pass + + def input_open(self) -> bool: + return any(i.has_class("open") for i in self.query(".field")) + + def action_help(self) -> None: + self.say(self.help_text) + + def action_quit_app(self) -> None: + self.app.exit(None) + + def action_back(self) -> None: + self.app.back() # type: ignore[attr-defined] + + def action_next(self) -> None: + self.commit() + self.app.advance() # type: ignore[attr-defined] + + def commit(self) -> None: + pass + + # ---- text input helper + def open_field(self, field_id: str, value: str = "", placeholder: str = "") -> None: + f = self.query_one(f"#{field_id}", Input) + f.disabled = False + f.add_class("open") + f.value = value + f.placeholder = placeholder + f.focus() + + def close_field(self, field_id: str) -> None: + f = self.query_one(f"#{field_id}", Input) + f.remove_class("open") + f.disabled = True + self.set_focus(None) + + +# ------------------------------------------------------------------------------------------------ start + +class StartScreen(Base): + step_id = "start" + title_line = "Launch" + hints = "↑↓ choose Enter launch e edit preset d delete ? help q quit" + help_text = ("Pick a saved preset to start it at once, or set up from the beginning. A preset remembers the model, " + "context size, parallel requests, tuned settings and who can reach it.") + + BINDINGS = Base.BINDINGS + [Binding("e", "edit", "Edit", show=False), Binding("d", "delete", "Delete", show=False)] + + def body(self) -> ComposeResult: + yield Static("", id="intro", markup=False) + yield Menu([("", 34), ("", 14), ("", 8), ("", 14), ("", 16)], id="menu") + + def on_mount(self) -> None: + self.reload() + self.paint() + + def reload(self) -> None: + self.presets = store.list_presets() + items: list[Item] = [] + for p in self.presets: + sm = store.summary(p) + req = (f"{p.parallel} request" + ("s" if p.parallel != 1 else "")) if p.parallel else "fit to card" + tuned = f"tuned {p.tuned_at[5:]}" if p.tuned_preset and p.tuned_at else "not tuned" + used = f"last used {p.last_used[5:10]}" if p.last_used else "not used yet" + items.append(Item([p.name or "?", str(sm["model"]), S.context_label(p.context), req, Cell(tuned, MUTE if not p.tuned_preset else ""), + Cell(used, MUTE)], key=("preset", p.name))) + items.append(Item([Cell("Set up from the beginning", "")], key=("new", None))) + items.append(Item([Cell("Use model files I already have", "")], key=("own", None))) + m = self.query_one(Menu) + m.columns = [("", 34), ("", 14), ("", 8), ("", 14), ("", 14)] + m.show_header = False + m.set_items(items, 0) + + def refresh_body(self) -> None: + intro = Text() + if self.presets: + intro.append("Start from a saved preset\n", style=f"bold {ACCENT}") + else: + intro.append("No saved presets yet\n", style=f"bold {ACCENT}") + intro.append("A preset remembers all your choices so next time you start with one key.\n", style=MUTE) + self.query_one("#intro", Static).update(intro) + + def explain(self) -> Text: + c = self.ctx.computer + return Text(f"This computer: {c.line()}. " + ("Preset: model, context, parallel requests, tuned settings, access. " + "Shared with the Strata app." if self.presets else "Choose how to begin."), style=MUTE) + + def head_text(self) -> Text: + out = Text(no_wrap=True, overflow="crop") + out.append("STRATA Launch", style=f"bold {ACCENT}") + info = self.ctx.computer.line() + gap = max(2, self.width() - len("STRATA Launch") - len(info)) + out.append(" " * gap + info, style=MUTE) + return out + + def action_next(self) -> None: + it = self.query_one(Menu).current + if it is None: + return + kind, name = it.key + if kind == "preset": + self.app.start_preset(name) # type: ignore[attr-defined] + elif kind == "new": + self.app.begin_setup() # type: ignore[attr-defined] + else: + self.app.begin_setup(own_files=True) # type: ignore[attr-defined] + + def action_edit(self) -> None: + it = self.query_one(Menu).current + if it and it.key[0] == "preset": + self.app.begin_setup(preset=it.key[1]) # type: ignore[attr-defined] + + def action_delete(self) -> None: + it = self.query_one(Menu).current + if not it or it.key[0] != "preset": + return + name = it.key[1] + + def done(choice: str | None) -> None: + if choice == "yes": + try: + store.delete(name) + except store.PresetError as e: + self.say(str(e), True) + self.reload() + self.say(f"Deleted '{name}'. Models and tuning results were not touched.") + + self.app.push_screen(ConfirmScreen(f"Delete the preset '{name}'?", + "Only the saved choices go. The model files and tuning results stay.", + [("yes", "Delete it"), ("no", "Keep it")]), done) + + def action_back(self) -> None: + self.app.exit(None) + + +# ------------------------------------------------------------------------------------------------ confirm pop-up + +class ConfirmScreen(ModalScreen): + BINDINGS = [Binding("up", "move(-1)", show=False), Binding("down", "move(1)", show=False), + Binding("enter", "pick", show=False), Binding("escape", "cancel", show=False)] + DEFAULT_CSS = f""" + ConfirmScreen {{ align: center middle; background: #0e1113 80%; }} + #dialog {{ width: 70; height: auto; background: #161a1d; border: solid {ACCENT}; padding: 1 2; }} + """ + + def __init__(self, question: str, detail: str, options: list[tuple[str, str]]): + super().__init__() + self.question, self.detail, self.options = question, detail, options + + def compose(self) -> ComposeResult: + with Vertical(id="dialog"): + yield Static(Text(self.question, style=f"bold {ACCENT}"), markup=False) + yield Static(Text(self.detail, style=MUTE), markup=False) + yield Static("", markup=False) + yield Menu(id="confirm-menu") + yield Static(Text("Up/Down choose Enter confirm Esc cancel", style=MUTE), markup=False) + + def on_mount(self) -> None: + m = self.query_one(Menu) + m.set_items([Item([label], key=key) for key, label in self.options], 0) + + def action_move(self, step: int) -> None: + self.query_one(Menu).move(step) + + def action_pick(self) -> None: + self.dismiss(self.query_one(Menu).current.key) + + def action_cancel(self) -> None: + self.dismiss(None) + + +# ------------------------------------------------------------------------------------------------ 1 model + +class ModelScreen(Base): + step_id = "model" + hints = "↑↓ choose Enter next o use my own files Esc back ? help" + help_text = ("Pick the model. 'Needs' is the graphics memory and RAM it uses on this computer; the n-gram table stays on " + "your SSD and is not counted. 'Does not fit' is a warning, not a block: you can still try it.") + + BINDINGS = Base.BINDINGS + [Binding("o", "own", "Own files", show=False)] + + def body(self) -> ComposeResult: + yield Static("", id="question", markup=False) + yield Menu(id="menu") + yield Input(placeholder="Folder with the model files, or one .gguf file", id="own", classes="field", disabled=True) + + def on_mount(self) -> None: + self.rows = self.ctx.rows + s = self.ctx.settings + self.build_menu(initial=True) + self.paint() + if self.ctx.start_own_files and not s.model_path: + self.ctx.start_own_files = False + self.action_own() + + def widths(self) -> list[tuple[str, int]]: + w = self.width() + if w >= 100: + return [("Model", 42), ("Download", 10), ("Needs (graphics + RAM)", 24), ("On this computer", 0)] + return [("Model", 41), ("Download", 9), ("Needs (gfx + RAM)", 18), ("On this computer", 0)] + + def build_menu(self, initial: bool = False) -> None: + s = self.ctx.settings + if not (s.family and s.model) and not s.model_path: + rec = next((r for r in self.rows if r.recommended), None) or next((r for r in self.rows if r.verdict == "fits"), None) + if rec is not None: + s.family, s.model = rec.family, rec.model + items: list[Item] = [] + sel = 0 + for i, r in enumerate(self.rows): + col = {"fits": "", "tight": WARN, "no": BAD}[r.verdict] + tag = (" (recommended)" if r.recommended else "") + (" (installed)" if r.installed else "") + (" [experimental]" if r.experimental else "") + need = f"{r.need_vram_gb:.0f} GB + {r.need_ram_gb:.0f} GB" if r.need_vram_gb or r.need_ram_gb else "-" + items.append(Item([r.title, f"{r.download_gb:.0f} GB", need, Cell(r.words + tag, col)], key=(r.family, r.model))) + if (s.family, s.model) == (r.family, r.model): + sel = i + m = self.query_one(Menu) + m.columns = self.widths() + m.window = max(4, (self.size.height or 30) - 12) + m.set_items(items, sel if (initial or not m.items) else None) + + def on_resize(self, _e) -> None: + if hasattr(self, "rows"): + self.build_menu() + self.paint() + + def refresh_body(self) -> None: + q = Text() + q.append("Which model?", style=f"bold {ACCENT}") + s = self.ctx.settings + if s.model_path: + q.append(f" your files: {s.model_path}", style=MUTE) + else: + info = "This computer: " + self.ctx.computer.line() + if len("Which model?") + len(info) + 3 <= self.width(): + q.append(" " * (self.width() - len("Which model?") - len(info)) + info, style=MUTE) + self.query_one("#question", Static).update(q) + + def explain(self) -> Text: + m = self.menu() + cur = m.current if m else None + out = Text() + if cur is not None and hasattr(self, "rows"): + r = next((x for x in self.rows if (x.family, x.model) == cur.key), None) + if r: + out.append(f"{r.title}: {r.about}.\n", style=FG) + if r.verdict != "fits": + out.append(f"{r.words}. ", style=WARN if r.verdict == "tight" else BAD) + if r.spill_gb: + out.append(f"About {r.spill_gb:.0f} GB would be read from the SSD while it answers. ", style=MUTE) + out.append("The n-gram table stays on your SSD and is not counted above.", style=MUTE) + return out + + def on_cursor(self) -> None: + pass + + def commit(self) -> None: + if self.input_open(): + return + cur = self.menu().current + if cur is not None: + s = self.ctx.settings + fam, model = cur.key + if (s.family, s.model) != (fam, model) or s.model_path: + s.model_path = None + s.family, s.model = fam, model + r = next(x for x in self.rows if (x.family, x.model) == (fam, model)) + if r.verdict == "no": + self.ctx.notes.append(f"{r.title} does not fit this computer; it may be slow or run out of memory.") + + def action_next(self) -> None: + if self.input_open(): + return + if self.ctx.settings.model_path and not self.ctx.picked_in_list: + self.app.advance() # type: ignore[attr-defined] + return + super().action_next() + + def action_move(self, step: int) -> None: + if self.input_open(): + return + self.ctx.picked_in_list = True + super().action_move(step) + + # ---- own files + def action_own(self) -> None: + self.open_field("own", self.ctx.settings.model_path or "", "Folder with the model files, or one .gguf file") + self.say("Type or paste the folder (or the first .gguf file), then press Enter.") + + def on_input_submitted(self, event: Input.Submitted) -> None: + if event.input.id != "own": + return + path = event.value.strip().strip('"') + if not path: + self.close_field("own") + return + self.check_own(path) + + def check_own(self, path: str) -> None: + from strata_cli.launch.runner import plain_issue + from strata_control import models + try: + plan = models.plan_import(str(Path(path).expanduser().resolve())) + except Exception as e: # noqa: BLE001 + self.say(f"These files cannot be used: {e}. Pick the folder with every shard of one model, or a .gguf file of it.", True) + return + sel = plan.get("candidate_selection") or {} + if sel.get("family") and sel.get("model") and not plan.get("issues"): + s = self.ctx.settings + s.model_path, s.family, s.model = str(Path(path).expanduser()), sel["family"], sel["model"] + self.ctx.picked_in_list = False + self.close_field("own") + gb = plan.get("total_bytes", 0) / 1e9 + self.say(f"These files are {sel['family']} {sel['model']} ({plan['file_count']} file(s), {gb:.0f} GB). Press Enter to use them.") + self.menu().select((sel["family"], sel["model"])) + self.paint() + else: + why = "; ".join(plain_issue(str(x)) for x in (plan.get("issues") or [])[:3]) or "they are not a model Strata knows" + self.say(f"These files cannot be used as they are ({why}). Check that every shard is in the folder, or pick a model from the list.", True) + + def action_back(self) -> None: + if self.input_open(): + self.close_field("own") + self.say("") + return + super().action_back() + + +# ------------------------------------------------------------------------------------------------ 2 cards (only with 2+ cards) + +class CardsScreen(Base): + step_id = "cards" + help_text = "Strata can use several graphics cards for one model. 'Let Strata choose' is the recommendation." + + def body(self) -> ComposeResult: + yield Static(Text("Which graphics cards should it use?", style=f"bold {ACCENT}"), markup=False) + yield Menu(id="menu") + + def on_mount(self) -> None: + gpus = self.ctx.computer.usable_gpus + items = [Item([f"Let Strata choose (recommended): {len(gpus)} cards"], key=None)] + for g in gpus: + items.append(Item([f"Only card {g.get('index')}: {g.get('name')}, {float(g.get('vram_gb') or 0):.0f} GB"], key=[g.get("index")])) + if len(gpus) > 2: + pass + items.append(Item(["All of them together, as one"], key=[g.get("index") for g in gpus])) + sel = 0 + cur = self.ctx.settings.gpus + for i, it in enumerate(items): + if it.key == cur: + sel = i + self.query_one(Menu).set_items(items, sel) + self.paint() + + def explain(self) -> Text: + return Text("This computer has several graphics cards. One card is simplest; several cards share the model so bigger " + "models fit. Strata's own choice is a good default.", style=MUTE) + + def commit(self) -> None: + self.ctx.settings.gpus = self.menu().current.key + + +# ------------------------------------------------------------------------------------------------ 3 folder + +class FolderScreen(Base): + step_id = "folder" + hints = "Type to change the folder Enter next Esc back ? help" + help_text = "Models are big. Pick a drive with free space. The default is where Strata keeps its data." + + BINDINGS = [b for b in Base.BINDINGS if b.key not in ("q", "up", "down")] + [Binding("down", "noop", show=False)] + + def body(self) -> ComposeResult: + yield Static(Text("Where should the model files be stored?", style=f"bold {ACCENT}"), markup=False) + yield Static("", id="folderinfo", markup=False) + yield Input(id="folderpath", classes="field open") + + def on_mount(self) -> None: + s = self.ctx.settings + f = self.query_one("#folderpath", Input) + f.value = s.data_dir or str(self.ctx.catalog.data_dir()) + f.focus() + self.paint() + + def action_noop(self) -> None: + pass + + def on_input_changed(self, event: Input.Changed) -> None: + self.paint() + + def on_input_submitted(self, event: Input.Submitted) -> None: + self.action_next() + + def need_gb(self) -> float: + s = self.ctx.settings + r = next((x for x in self.ctx.rows if (x.family, x.model) == (s.family, s.model)), None) + if r is None: + return 8.0 + return 8.0 + (0 if s.model_path else r.download_gb) + + def refresh_body(self) -> None: + try: + value = self.query_one("#folderpath", Input).value.strip() + except Exception: # noqa: BLE001 + return + free = self.ctx.catalog.disk_free(value or self.ctx.catalog.data_dir()) + need = self.need_gb() + s = self.ctx.settings + out = Text() + if s.model_path: + out.append(f"Your model files stay where they are: {s.model_path}\n", style=FG) + out.append("Strata only writes its prepared data here (packs, a small draft layer).\n", style=MUTE) + else: + out.append(f"Needs about {need:.0f} GB (the download plus working room).", style=FG) + out.append("\n") + if free is not None: + warn = free < need + out.append(f"Free space in that folder's drive: {free:,.0f} GB", style=WARN if warn else FG) + if warn: + out.append(f" Not enough for {need:.0f} GB. You can still go on, but the download is likely to stop; pick a bigger drive.", style=WARN) + else: + out.append("The free space of that folder could not be read.", style=MUTE) + self.query_one("#folderinfo", Static).update(out) + + def explain(self) -> Text: + return Text("Models are large files. Strata keeps them in one folder; the default is Strata's own data folder. " + "Press Enter to keep it, or type another folder (it is created if it does not exist).", style=MUTE) + + def commit(self) -> None: + value = self.query_one("#folderpath", Input).value.strip().strip('"') + default = str(self.ctx.catalog.data_dir()) + self.ctx.settings.data_dir = None if (not value or value == default) else value + + +# ------------------------------------------------------------------------------------------------ 4 context + +class ContextScreen(Base): + step_id = "context" + hints = "↑↓ choose Enter next m memory format Esc back ? help" + help_text = ("Context is how much text the model can see at once (your chat, files, tool output). A longer one lets agents read " + "bigger projects, uses more memory, and makes the first reply of a long chat slower.") + + BINDINGS = Base.BINDINGS + [Binding("m", "memory", "Memory format", show=False), Binding("c", "custom", "Custom", show=False)] + + def body(self) -> ComposeResult: + yield Static(Text("How much text should it remember at once?", style=f"bold {ACCENT}"), markup=False) + yield Menu([("", 7), ("", 24), ("", 12), ("", 0)], id="menu") + yield Static("", id="kvline", markup=False) + yield Input(placeholder="a size like 100k, or a number of tokens", id="custom", classes="field", disabled=True) + + def kv(self) -> str: + s = self.ctx.settings + return s.kv or "int8" + + def on_mount(self) -> None: + s = self.ctx.settings + cat = self.ctx.catalog + fam, model = s.family, s.model + self.rec = cat.max_context(fam, model, self.ctx.computer.raw) if fam and model else 32768 + if s.context is None: + s.context = self.rec + self.build() + self.paint() + + def build(self) -> None: + s, cat = self.ctx.settings, self.ctx.catalog + items = [] + sel = 0 + for i, c in enumerate(cat.contexts()): + kvgb = cat.kv_gb(c, self.kv()) + note, style = "", "" + if c == self.rec: + note = "recommended" + elif c > max(self.rec * 2, 131072) and c <= 262144: + note, style = "tight on this computer", WARN + elif c > 262144: + note, style = "experimental: rope scaling", WARN + items.append(Item([S.context_label(c), S.words_for(c), f"+{kvgb:.1f} GB", Cell(note, style)], key=c)) + if c == s.context: + sel = i + if s.context and s.context not in cat.contexts(): + items.append(Item([S.context_label(s.context), S.words_for(s.context), f"+{cat.kv_gb(s.context, self.kv()):.1f} GB", + Cell("your own size", "")], key=s.context)) + sel = len(items) - 1 + items.append(Item(["Custom…"], key="custom")) + self.query_one(Menu).set_items(items, sel) + + def refresh_body(self) -> None: + s = self.ctx.settings + kv = self.kv() + label = {"int8": "8-bit (recommended: what the published numbers were measured with)", + "q4_0": "4-bit (half the memory, a little less precise on long documents)", + "k8v4": "mixed 8-bit and 4-bit", "fp16": "full precision"}.get(kv, kv) + small = (s.context or 0) <= 8192 + self.query_one("#kvline", Static).update(Text( + f"Conversation memory: {label}" + ("" if not small else " (small contexts always use full precision)") + " press m to change", + style=MUTE)) + + def explain(self) -> Text: + cur = self.menu().current if self.menu() else None + out = Text("More context lets agents read bigger projects. It uses more memory and the first reply of a long chat is slower. " + "The memory shown is only the extra for the conversation itself.", style=MUTE) + if cur is not None and isinstance(cur.key, int) and cur.key > 262144: + out.append("\nThis is past what the model was trained on; setup adds rope scaling and answers may get less reliable.", style=WARN) + return out + + def action_memory(self) -> None: + s = self.ctx.settings + order = ["int8", "q4_0"] + cur = s.kv or "int8" + s.kv = order[(order.index(cur) + 1) % len(order)] if cur in order else "int8" + self.build() + self.paint() + + def action_custom(self) -> None: + self.open_field("custom", "", "a size like 100k, or a number of tokens") + self.say("Type a size and press Enter.") + + def on_input_submitted(self, event: Input.Submitted) -> None: + if event.input.id != "custom": + return + try: + n = S.parse_context(event.value) + except ValueError as e: + self.say(str(e), True) + return + self.ctx.settings.context = n + self.close_field("custom") + self.build() + self.say(f"{S.context_label(n)} chosen.") + self.paint() + + def action_back(self) -> None: + if self.input_open(): + self.close_field("custom") + return + super().action_back() + + def commit(self) -> None: + cur = self.menu().current + if cur is None: + return + if cur.key == "custom": + return + self.ctx.settings.context = int(cur.key) + + def action_next(self) -> None: + if self.input_open(): + return + cur = self.menu().current + if cur is not None and cur.key == "custom": + self.action_custom() + return + super().action_next() + + +# ------------------------------------------------------------------------------------------------ 5 pictures + +class PicturesScreen(Base): + step_id = "pictures" + help_text = "Pictures let the model read screenshots, photos and scanned pages. It is a small extra download and a little graphics memory." + + def body(self) -> ComposeResult: + yield Static(Text("Should the model read pictures?", style=f"bold {ACCENT}"), markup=False) + yield Menu(id="menu") + + def row(self): + s = self.ctx.settings + return next((x for x in self.ctx.rows if (x.family, x.model) == (s.family, s.model)), None) + + def on_mount(self) -> None: + s = self.ctx.settings + r = self.row() + self.supported = True if r is None else r.images + items = [Item(["No pictures"], key="off")] + if self.supported: + items.append(Item(["Yes, read pictures"], key="on")) + else: + items.append(Item([Cell("Yes, read pictures: not available for this model yet", MUTE)], key="on", enabled=False)) + sel = 1 if s.vision != "off" and self.supported else 0 + self.query_one(Menu).set_items(items, sel) + self.paint() + + def explain(self) -> Text: + vram = self.ctx.computer.main_vram_gb + extra = ("a 0.9 GB download. On a card under 24 GB the picture reader runs on the CPU (a few seconds per picture, nothing " + "taken from the graphics card). On a bigger card it runs on the GPU and keeps about 1.4 GB free, costing a few % of text speed." + if vram < 24 else "a 0.9 GB download, and about 1.4 GB of graphics memory kept free for the picture reader (text is a few % slower).") + if not self.supported: + return Text("This model cannot read pictures yet, so pictures stay off. Pick another model if you need them.", style=WARN) + return Text("Pictures add " + extra + " Say No if you only chat in text.", style=MUTE) + + def commit(self) -> None: + self.ctx.settings.vision = self.menu().current.key + + +# ------------------------------------------------------------------------------------------------ 6 parallel + +class ParallelScreen(Base): + step_id = "parallel" + help_text = ("Parallel requests: how many people or apps are answered at the same time. More at once makes each answer a bit slower " + "and needs graphics memory the model could use otherwise.") + + def body(self) -> ComposeResult: + yield Static(Text("How many requests at the same time?", style=f"bold {ACCENT}"), markup=False) + yield Menu(id="menu") + + def on_mount(self) -> None: + vram = self.ctx.computer.main_vram_gb + self.fit = S.fit_parallel(vram) if vram else 1 + s = self.ctx.settings + items = [Item([f"Fit to my graphics card ({self.fit})", Cell("recommended", "")], key=None)] + for n in (1, 2, 3, 4): + note, style = "", "" + if n > self.fit: + note, style = "may not fit: slower answers", WARN + items.append(Item([f"{n} request" + ("s" if n != 1 else ""), Cell(note, style)], key=n)) + sel = 0 if s.parallel is None else min(4, max(1, s.parallel)) + self.query_one(Menu).columns = [("", 36), ("", 0)] + self.query_one(Menu).set_items(items, sel) + self.paint() + + def explain(self) -> Text: + vram = self.ctx.computer.main_vram_gb + return Text(f"Your card has {vram:.0f} GB of graphics memory, which fits {self.fit} at once by Strata's rule " + "(12 GB: 1, 20 GB: 2, 24 GB: 3, 32 GB or more: 4). One person: pick 1. A team or several coding agents: more. " + "More than fits still starts, only slower." if vram else + "No graphics card was found, so Strata cannot say how many fit. 1 is the safe choice.", style=MUTE) + + def commit(self) -> None: + self.ctx.settings.parallel = self.menu().current.key + + +# ------------------------------------------------------------------------------------------------ 7 tuning + +class TuningScreen(Base): + step_id = "tuning" + hints = "↑↓ choose Enter next a answer-changing settings on/off Esc back ? help" + help_text = ("Tuning measures a few settings on this computer and keeps only real gains. It runs after the model is downloaded, " + "before serving starts. You can stop it at any time: nothing changes until it is done.") + + BINDINGS = Base.BINDINGS + [Binding("a", "allow", "Allow", show=False)] + + def body(self) -> ComposeResult: + yield Static(Text("Tune for the best speed on this computer?", style=f"bold {ACCENT}"), markup=False) + yield Menu([("", 12), ("", 22), ("", 0)], id="menu") + yield Static("", id="allowline", markup=False) + + def on_mount(self) -> None: + s = self.ctx.settings + items = [Item(["Quick", "about 20 minutes", "checks the most likely gains"], key="quick"), + Item(["Thorough", "up to 2 hours", "tries more settings, measures each one several times"], key="thorough"), + Item(["Skip", "", "use the default settings (already good on most computers)"], key="skip")] + if not self.ctx.tune_chosen and s.tune == "skip" and not s.tuned_preset and self.ctx.fresh_setup: + s.tune = "quick" + self.query_one(Menu).set_items(items, {"quick": 0, "thorough": 1, "skip": 2}.get(s.tune, 0)) + self.paint() + + def refresh_body(self) -> None: + on = self.ctx.settings.tune_allow_small + self.query_one("#allowline", Static).update(Text( + f"\n [{'x' if on else ' '}] Allow settings that change answers very slightly (same quality) press a to change", style=MUTE)) + + def explain(self) -> Text: + return Text("Tuning is optional. It tries single changes against your settings, keeps only gains above the noise, and saves the " + "result as a preset the Strata app can use too. Close other heavy programs while it measures. If your computer is " + "already at its best it says so and changes nothing.", style=MUTE) + + def action_allow(self) -> None: + self.ctx.settings.tune_allow_small = not self.ctx.settings.tune_allow_small + self.paint() + + def commit(self) -> None: + self.ctx.settings.tune = self.menu().current.key + self.ctx.tune_chosen = True + + +# ------------------------------------------------------------------------------------------------ 8 advanced + +class AdvancedScreen(Base): + step_id = "advanced" + hints = "↑↓ choose Enter edit Del reset x experimental / filter a add by name c copy command n next Esc back" + help_text = ("Every engine setting with its default, the value the tuner chose and your own value. You never have to touch this. " + "A name the engine does not list gets a warning, not a refusal.") + + BINDINGS = [b for b in Base.BINDINGS if b.key != "enter"] + [ + Binding("enter", "edit", show=False), Binding("delete", "reset", show=False), Binding("backspace", "reset", show=False), + Binding("x", "experimental", show=False), Binding("slash", "filter", show=False), Binding("a", "add", show=False), + Binding("c", "copy", show=False), Binding("n", "go_next", show=False), Binding("right", "go_next", show=False)] + + def body(self) -> ComposeResult: + yield Static("", id="advhead", markup=False) + yield Menu([("Setting", 28), ("Default", 13), ("Tuned", 15), ("Yours", 13), ("What it does", 0)], id="menu", window=8) + yield Static("", id="cmdline", markup=False) + yield Input(id="edit", classes="field", disabled=True) + + def on_mount(self) -> None: + self.only_experimental = False + self.filter = "" + self.mode = "" # "", "edit", "filter", "add" + self.rows: list[P.Row] = [] + self.load_rows() + self.paint() + + def load_rows(self) -> None: + self.rows = self.ctx.advanced_rows() + self.build() + + def build(self) -> None: + items = [] + for r in self.rows: + if self.only_experimental and not r.experimental: + continue + if self.filter and self.filter.lower() not in r.name.lower() and self.filter.lower() not in r.description.lower(): + continue + d, t, y = r.cells()[1:] + desc = r.description + (" [experimental]" if r.experimental else "") + items.append(Item([r.name, d, t, Cell(y, ACCENT if y != "-" else ""), Cell(desc, WARN if r.experimental else (BAD if not r.known else ""))], + key=r.name)) + items.append(Item([Cell("+ Add a setting by name…", ""), "", "", "", Cell("for flags of newer engine versions", MUTE)], key="__add__")) + m = self.query_one(Menu) + m.window = max(4, (self.size.height or 30) - 15) + m.set_items(items, min(m.index, len(items) - 1)) + + def on_resize(self, _e) -> None: + if hasattr(self, "rows"): + self.build() + self.paint() + + def refresh_body(self) -> None: + eng = self.ctx.engine_note + t = Text() + t.append("All settings", style=f"bold {ACCENT}") + t.append(" (optional · for tinkerers)", style=MUTE) + extra = " / filter" + (f": {self.filter}" if self.filter else "") + " x show experimental only" + (" (on)" if self.only_experimental else "") + t.append(extra, style=MUTE) + self.query_one("#advhead", Static).update(t) + s = self.ctx.settings + cmd = self.ctx.command_text() + out = Text() + out.append("Command that will run\n", style=f"bold {ACCENT}") + out.append(" " + cmd + "\n") + out.append(" Same as: " + S.flags_text(s) + "\n", style=MUTE) + if eng: + out.append(" " + eng, style=WARN) + self.query_one("#cmdline", Static).update(out) + + def explain(self) -> Text: + m = self.menu() + cur = m.current if m else None + if cur is not None and cur.key not in (None, "__add__"): + r = next((x for x in self.rows if x.name == cur.key), None) + if r: + return Text(f"{r.name}: {r.description}" + (" This one is experimental: it may change answers or not work on every card." if r.experimental else ""), style=MUTE) + return Text("Press Enter on a row to set your own value; Del puts the tuned value back. You can go on without touching anything: n.", style=MUTE) + + # ---- editing + def action_edit(self) -> None: + if self.mode: + return + cur = self.menu().current + if cur is None: + return + if cur.key == "__add__": + return self.action_add() + self.mode = "edit" + self.edit_name = cur.key + r = next(x for x in self.rows if x.name == cur.key) + self.open_field("edit", r.yours or (r.tuned if r.tuned != "-" else r.default) or "", f"value for {cur.key} (empty = on, 'off' removes it)") + self.say(f"Type the value for {cur.key} and press Enter. Esc cancels.") + + def action_add(self) -> None: + if self.mode: + return + self.mode = "add" + self.open_field("edit", "", "name=value, e.g. prefill=65536 or STRATA_PIN_BY_NEED=1") + self.say("Type the setting as name=value (the name of a newer engine's flag is fine).") + + def action_filter(self) -> None: + if self.mode: + return + self.mode = "filter" + self.open_field("edit", self.filter, "part of a name or description") + + def on_input_submitted(self, event: Input.Submitted) -> None: + if event.input.id != "edit": + return + text = event.value.strip() + mode = self.mode + self.mode = "" + self.close_field("edit") + if mode == "filter": + self.filter = text + elif mode == "edit": + self.set_value(self.edit_name, text) + elif mode == "add" and text: + try: + key, value = S.split_setting(text) + except ValueError as e: + self.say(str(e), True) + return + self.set_value(key, value) + self.build() + self.paint() + + def set_value(self, key: str, value: str) -> None: + s = self.ctx.settings + s.extra[key] = value + warn = P.check_name(key, self.ctx.engine_flags) + self.ctx.invalidate_rows() + self.rows = self.ctx.advanced_rows() + self.say(warn or f"{key} set to {value or 'on'}. It is used for this launch.", bool(warn)) + + def action_reset(self) -> None: + if self.mode: + return + cur = self.menu().current + if cur is None or cur.key in (None, "__add__"): + return + if self.ctx.settings.extra.pop(cur.key, None) is not None: + self.ctx.invalidate_rows() + self.rows = self.ctx.advanced_rows() + self.build() + self.say(f"{cur.key} is back to the tuned value.") + self.paint() + + def action_experimental(self) -> None: + if self.mode: + return + self.only_experimental = not self.only_experimental + self.build() + self.paint() + + def action_copy(self) -> None: + if self.mode: + return + text = self.ctx.command_text() + "\n" + S.flags_text(self.ctx.settings) + try: + self.app.copy_to_clipboard(text) + self.say("Copied the command to the clipboard (if your terminal allows it).") + except Exception: # noqa: BLE001 + self.say("Your terminal does not allow copying; select the lines above with the mouse.", True) + + def action_go_next(self) -> None: + if self.mode: + return + self.app.advance() # type: ignore[attr-defined] + + def action_next(self) -> None: + self.action_edit() + + def action_back(self) -> None: + if self.mode: + self.mode = "" + self.close_field("edit") + self.say("") + return + super().action_back() + + def action_move(self, step: int) -> None: + if self.mode: + return + super().action_move(step) + + +# ------------------------------------------------------------------------------------------------ 9 access + +class AccessScreen(Base): + step_id = "access" + hints = "↑↓ choose Enter next p port k new key t key on/off Esc back ? help" + help_text = ("Who can use the model. 'This computer only' is the safe default. Anything beyond it needs the API key: the server will not " + "start on a network without one.") + + BINDINGS = Base.BINDINGS + [Binding("p", "port", show=False), Binding("k", "newkey", show=False), Binding("t", "keytoggle", show=False)] + + def body(self) -> ComposeResult: + yield Static(Text("Who can use it?", style=f"bold {ACCENT}"), markup=False) + yield Menu([("", 22), ("", 46), ("", 0)], id="menu") + yield Static("", id="portkey", markup=False) + yield Input(id="portfield", classes="field", disabled=True) + + def on_mount(self) -> None: + self.build() + self.paint() + + def build(self) -> None: + s = self.ctx.settings + opts = acc.options(s.port) + items = [] + sel = 0 + for i, o in enumerate(opts): + note = o.note + style = WARN if o.warn else "" + items.append(Item([o.label, o.url, Cell(note, style)], key=o.kind)) + if o.kind == s.access: + sel = i + self.opts = {o.kind: o for o in opts} + self.query_one(Menu).set_items(items, sel) + + def refresh_body(self) -> None: + s = self.ctx.settings + on = bool(s.api_key) + beyond = s.access != "local" + t = Text() + t.append(f"\n Port [ {s.port} ] press p to change\n") + t.append(f" API key [ {'on' if on else 'off'} ] {acc.mask_key(s.api_key)} ", style=FG if on else WARN) + t.append("(recommended for anything beyond this computer)" if beyond else "(optional on this computer)", style=MUTE) + self.query_one("#portkey", Static).update(t) + + def explain(self) -> Text: + cur = self.menu().current if self.menu() else None + out = Text() + if cur is not None: + missing = acc.missing_helper(cur.key) + if missing: + out.append(missing + "\n", style=WARN) + if cur.key == "cloudflare": + out.append("A Cloudflare quick tunnel gives a temporary public address; anyone with it can try to use the server, so keep the API key on.", style=MUTE) + elif cur.key == "lan": + out.append("Other computers on your network can use it at the address shown. Keep the API key on.", style=MUTE) + elif cur.key == "tailscale": + out.append("Only devices signed in to your Tailscale network can reach it.", style=MUTE) + else: + out.append("Only programs on this computer can reach it, so no key is needed.", style=MUTE) + return out + + def on_cursor(self) -> None: + kind = self.menu().current.key + s = self.ctx.settings + s.access = kind + if kind != "local" and not s.api_key: + s.api_key = acc.new_key() + self.say("An API key was made for you, because this reaches beyond your computer.") + + def action_port(self) -> None: + self.open_field("portfield", str(self.ctx.settings.port), "port number") + self.say("Type a port from 1 to 65535 and press Enter.") + + def on_input_submitted(self, event: Input.Submitted) -> None: + if event.input.id != "portfield": + return + n = S.check_port(event.value.strip()) + if n and n.level == "error": + self.say(n.line(), True) + return + self.ctx.settings.port = int(event.value.strip()) + self.close_field("portfield") + self.build() + self.say(n.line() if n else f"Port {self.ctx.settings.port}.", bool(n)) + self.paint() + + def action_newkey(self) -> None: + self.ctx.settings.api_key = acc.new_key() + self.say("A new API key was made. Apps that used the old one need the new one (strata connect shows it).") + self.paint() + + def action_keytoggle(self) -> None: + s = self.ctx.settings + if s.api_key: + s.api_key = None + self.say("The API key is off." + (" The server will not start on a network without one." if s.access != "local" else ""), s.access != "local") + else: + s.api_key = acc.new_key() + self.say("The API key is on.") + self.paint() + + def action_next(self) -> None: + if self.input_open(): + return + self.commit() + self.app.advance() # type: ignore[attr-defined] + + def commit(self) -> None: + s = self.ctx.settings + s.access = self.menu().current.key + if s.access != "local" and not s.api_key: + s.api_key = acc.new_key() + + def action_back(self) -> None: + if self.input_open(): + self.close_field("portfield") + return + super().action_back() + + +# ------------------------------------------------------------------------------------------------ 10 save + launch + +class SaveScreen(Base): + step_id = "save" + hints = "↑↓ choose Enter do it Esc back ? help" + help_text = "Save remembers all your choices under a name so next time you start with one key. Launch downloads what is missing, tunes if you asked, and starts serving." + + def body(self) -> ComposeResult: + yield Static("", id="summary", markup=False) + yield Input(id="name", classes="field open") + yield Menu(id="menu") + + def on_mount(self) -> None: + s = self.ctx.settings + f = self.query_one("#name", Input) + f.value = s.name or self.ctx.default_name() + items = [Item(["Save as a preset and launch"], key="save_launch"), Item(["Launch without saving"], key="launch"), + Item(["Save the preset and stop here"], key="save")] + self.query_one(Menu).set_items(items, 0) + self.paint() + + def refresh_body(self) -> None: + s = self.ctx.settings + r = next((x for x in self.ctx.rows if (x.family, x.model) == (s.family, s.model)), None) + t = Text() + t.append("Save as preset, then launch\n", style=f"bold {ACCENT}") + t.append(" Name ") + t.append("(type below)\n", style=MUTE) + lines = [("Model", (r.title if r else (s.model or "?")) + (" (your files)" if s.model_path else "")), + ("Context", S.context_label(s.context) + f", memory {s.kv or 'int8'}"), + ("Pictures", "on" if s.vision != "off" else "off"), + ("Parallel", f"{s.parallel} at a time" if s.parallel else f"fit to your card ({S.fit_parallel(self.ctx.computer.main_vram_gb)})"), + ("Tuning", {"quick": "quick (about 20 min)", "thorough": "thorough (up to 2 h)", "skip": "skipped"}[s.tune]), + ("Access", f"{S.ACCESS_WORDS[s.access]}, port {s.port}" + (", API key on" if s.api_key else ", no API key")), + ("Storage", s.data_dir or "Strata's data folder")] + if s.extra: + lines.append(("Advanced", ", ".join(f"{k}={v}" for k, v in s.extra.items()))) + for k, v in lines: + t.append(f" {k:<10}{v}\n") + for n in self.ctx.warnings(): + t.append(f" ! {n}\n", style=WARN) + self.query_one("#summary", Static).update(t) + + def explain(self) -> Text: + return Text("Press Enter on the first choice to save and start. The first start may download the model and tune; the monitor opens when it is serving.", style=MUTE) + + def action_move(self, step: int) -> None: + super().action_move(step) + + def action_next(self) -> None: + name = self.query_one("#name", Input).value.strip() or self.ctx.default_name() + choice = self.menu().current.key + s = self.ctx.settings + s.name = name + if choice in ("save", "save_launch"): + try: + store.save(s, name) + except store.PresetError as e: + self.say(str(e), True) + return + if choice == "save": + self.app.exit(None) + self.app.stopped_after_save = True # type: ignore[attr-defined] + return + self.app.run_launch() # type: ignore[attr-defined] + + def action_quit_app(self) -> None: + pass + + +# ------------------------------------------------------------------------------------------------ progress + +class RunScreen(Base): + step_id = "run" + title_line = "Launch" + hints = "s stop q quit" + BINDINGS = [Binding("s", "stop_run", show=False), Binding("q", "stop_run", show=False), Binding("escape", "stop_run", show=False), + Binding("r", "retry", show=False)] + + STAGE_WORDS = (("engine", "Engine"), ("model", "Model"), ("config", "Settings"), ("tune", "Tuning"), ("start", "Starting"), + ("access", "Access")) + + def body(self) -> ComposeResult: + yield Static("", id="stages", markup=False) + yield Static("", id="tunepanel", markup=False) + + def on_mount(self) -> None: + self.state: dict[str, dict] = {k: {"text": "", "pct": None, "status": "wait"} for k, _ in self.STAGE_WORDS} + self.failed: str | None = None + self.tune_info: dict = {} + self.paint() + + def head_text(self) -> Text: + out = Text(no_wrap=True, overflow="crop") + out.append("STRATA Launch", style=f"bold {ACCENT}") + s = self.ctx.settings + out.append(f" {s.name or ''}", style=MUTE) + return out + + def on_event(self, ev) -> None: + if ev.stage == "done": + return + st = self.state.get(ev.stage) + if st is None: + return + for k, _ in self.STAGE_WORDS: # earlier stages are finished + if k == ev.stage: + break + if self.state[k]["status"] in ("wait", "run"): + self.state[k]["status"] = "done" if self.state[k]["text"] else "skip" + st["text"] = ev.text + st["pct"] = ev.pct + st["level"] = ev.level + st["status"] = "done" if (ev.pct is not None and ev.pct >= 100) else "run" + if ev.stage == "tune": + self.tune_info = ev.data or {} + self.paint() + + def on_failure(self, text: str) -> None: + self.failed = text + self.paint() + + def on_finished(self) -> None: + for k, st in self.state.items(): + if st["status"] in ("wait", "run"): + st["status"] = "done" if st["text"] else "skip" + self.paint() + + def refresh_body(self) -> None: + t = Text() + g = self.g + width = max(20, min(40, self.width() - 50)) + for k, label in self.STAGE_WORDS: + st = self.state.get(k) if hasattr(self, "state") else None + if st is None: + continue + mark = {"wait": " ", "run": "> ", "done": g.tick + " ", "skip": "- "}[st["status"]] + style = ACCENT if st["status"] in ("run", "done") else MUTE + if st.get("level") == "warn" and st["status"] != "run": + style = WARN + t.append(f" {mark}{label:<10}", style=f"bold {style}") + if st["pct"] is not None and st["status"] == "run": + t.append(bar(st["pct"] / 100.0, width, g) + f" {st['pct']:.0f}% ", style=ACCENT) + t.append((st["text"] or "") + "\n", style=FG if st["status"] == "run" else MUTE) + if self.failed: + t.append("\n " + self.failed + "\n", style=BAD) + t.append(" Press r to try again, Esc to go back to the screens, q to quit.\n", style=MUTE) + try: + self.query_one("#stages", Static).update(t) + self.query_one("#tunepanel", Static).update(self.tune_panel()) + except Exception: # noqa: BLE001 + pass + + def tune_panel(self) -> Text: + t = Text() + info = self.tune_info + if not info or self.state["tune"]["status"] == "wait": + return t + t.append("\n Tuning", style=f"bold {ACCENT}") + rnd = info.get("round") + if rnd and rnd[1]: + t.append(f" measurement {rnd[0]} of {rnd[1]}") + if info.get("minutes_left"): + t.append(f" about {info['minutes_left']} min left") + t.append("\n") + for sp in (info.get("speeds") or [])[-6:]: + label = sp.get("label") or sp.get("id") or "?" + val = sp.get("decode_tok_s") or sp.get("value") or sp.get("tok_s") + t.append(f" {str(label)[:34]:<36}{val if val is not None else '-'}" + (" tokens/s" if val is not None else "") + "\n") + t.append(" Close other heavy programs while it measures. You can stop it any time: nothing changes until it is done.\n", style=MUTE) + return t + + def explain(self) -> Text: + return Text("Strata is getting everything ready. This can take a while the first time (the model is large). " + "Stopping is safe: files already downloaded are kept and a download continues where it stopped.", style=MUTE) + + def action_stop_run(self) -> None: + self.app.stop_run(self) # type: ignore[attr-defined] + + def action_retry(self) -> None: + if self.failed: + self.app.run_launch() # type: ignore[attr-defined] + + def action_move(self, step: int) -> None: + pass + + def action_next(self) -> None: + pass diff --git a/strata_cli/launch/settings.py b/strata_cli/launch/settings.py index b15faf9b..03183029 100644 --- a/strata_cli/launch/settings.py +++ b/strata_cli/launch/settings.py @@ -276,7 +276,7 @@ def flags_text(s: LaunchSettings, *, preset: str | None = None) -> str: def slug(name: str) -> str: """A name safe for profile ids and file names: lowercase letters, digits, - and _, starting with a letter.""" - text = re.sub(r"[^a-z0-9_-]+", "-", name.lower()).strip("-_") or "launch" + text = re.sub(r"-{2,}", "-", re.sub(r"[^a-z0-9_-]+", "-", name.lower())).strip("-_") or "launch" if not text[0].isalpha(): text = "l" + text return text[:48] diff --git a/strata_cli/launch/store.py b/strata_cli/launch/store.py index 4ce70455..21b43298 100644 --- a/strata_cli/launch/store.py +++ b/strata_cli/launch/store.py @@ -60,7 +60,8 @@ def _private_write(path: Path, payload: bytes) -> None: def now_text() -> str: - return time.strftime("%Y-%m-%d %H:%M") + now = time.time() + return time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(now)) + f".{int((now % 1) * 1000000):06d}" def save(settings: LaunchSettings, name: str | None = None) -> LaunchSettings: diff --git a/strata_cli/launch/widgets.py b/strata_cli/launch/widgets.py index 28cc190d..a2636734 100644 --- a/strata_cli/launch/widgets.py +++ b/strata_cli/launch/widgets.py @@ -83,7 +83,7 @@ def cell(c: Cell | str) -> Cell: class Menu(Static): """A column list with a cursor: `items` is a list of `Item`, `columns` a list of (title, width) (width 0 = the rest).""" - DEFAULT_CSS = "Menu { height: auto; }" + DEFAULT_CSS = "Menu { height: auto; text-wrap: nowrap; text-overflow: clip; }" def __init__(self, columns: list[tuple[str, int]] | None = None, *, glyphs: Glyphs | None = None, id: str | None = None, window: int = 0) -> None: diff --git a/tests/terminal/launch_support.py b/tests/terminal/launch_support.py new file mode 100644 index 00000000..a693c9c4 --- /dev/null +++ b/tests/terminal/launch_support.py @@ -0,0 +1,70 @@ +"""Shared fixtures for the `strata launch` tests: an isolated home, a fake computer, and a text dump of a Textual screen.""" + +from __future__ import annotations + +import contextlib +import io +import os +import tempfile +from pathlib import Path +from unittest import mock + +HW_12 = {"ram_gb": 64.0, "os": "Test OS", "cpu": {"avx2": True, "name": "test cpu"}, + "gpus": [{"index": 0, "name": "NVIDIA GeForce RTX 5070", "vram_gb": 12.0, "usable": True, "vendor": "nvidia"}]} +HW_24 = {**HW_12, "gpus": [{"index": 0, "name": "NVIDIA GeForce RTX 4090", "vram_gb": 24.0, "usable": True, "vendor": "nvidia"}]} +HW_2X16 = {**HW_12, "gpus": [{"index": 0, "name": "NVIDIA RTX A4000", "vram_gb": 16.0, "usable": True, "vendor": "nvidia"}, + {"index": 1, "name": "NVIDIA RTX A4000", "vram_gb": 16.0, "usable": True, "vendor": "nvidia"}]} +HW_32GB_RAM = {**HW_12, "ram_gb": 32.0} + + +class Home: + """A throw-away per-user home: profiles, presets, launch presets, state, components and install folder.""" + + def __init__(self) -> None: + self.tmp = tempfile.TemporaryDirectory(prefix="strata-launch-test-") + base = Path(self.tmp.name) + self.base = base + self.env = { + "STRATA_PROFILE_DIR": str(base / "config" / "profiles"), + "STRATA_PRESET_DIR": str(base / "config" / "presets"), + "STRATA_LAUNCH_PRESET_DIR": str(base / "config" / "launch-presets"), + "STRATA_COMPONENTS_DIR": str(base / "components"), + "STRATA_INSTALL_DIR": str(base / "install"), + "STRATA_RUNTIME_DIR": str(base / "runtime"), + "XDG_STATE_HOME": str(base / "state"), + "LOCALAPPDATA": str(base / "local"), + "HOME": str(base / "home"), + "USERPROFILE": str(base / "home"), + } + (base / "home").mkdir(parents=True, exist_ok=True) + self.patch = mock.patch.dict(os.environ, self.env) + + def __enter__(self) -> "Home": + self.patch.start() + return self + + def __exit__(self, *exc) -> None: + self.patch.stop() + self.tmp.cleanup() + + +def catalog(hw: dict | None = None, root: Path | None = None): + from strata_cli.launch.catalog import Catalog + return Catalog(root=root or Path(os.environ["STRATA_INSTALL_DIR"]), hardware=hw or HW_12) + + +def text_of(app) -> str: + """What the current screen shows, one line per terminal row (for assertions and the text previews).""" + strips = app.screen._compositor.render_strips() + return "\n".join(s.text.rstrip() for s in strips) + + +def run_cli(argv: list[str]) -> tuple[int, str, str]: + from strata_cli.cli import main + out, err = io.StringIO(), io.StringIO() + with contextlib.redirect_stdout(out), contextlib.redirect_stderr(err): + try: + code = main(argv) + except SystemExit as e: + code = int(e.code or 0) + return code, out.getvalue(), err.getvalue() diff --git a/tests/terminal/test_launch.py b/tests/terminal/test_launch.py new file mode 100644 index 00000000..d34c7f55 --- /dev/null +++ b/tests/terminal/test_launch.py @@ -0,0 +1,558 @@ +"""`strata launch` without screens: the settings rules, the catalog, presets, parameters, access, connect, and every flag.""" + +from __future__ import annotations + +import json +import os +import unittest +from pathlib import Path +from unittest import mock + +from strata_cli.launch import access, harness, params, settings as S, store +from strata_cli.launch.catalog import Catalog, CatalogUnavailable, fit_on, verdict +from strata_cli.launch.runner import Result, Runner, derive_config, desc_matches, tune_words +from tests.terminal.launch_support import HW_12, HW_24, HW_2X16, Home, catalog, run_cli + + +class SettingsRules(unittest.TestCase): + def test_parse_context(self): + self.assertEqual(S.parse_context("128k"), 131072) + self.assertEqual(S.parse_context("128K"), 131072) + self.assertEqual(S.parse_context("32 k"), 32768) + self.assertEqual(S.parse_context("65536"), 65536) + with self.assertRaises(ValueError): + S.parse_context("lots") + with self.assertRaises(ValueError): + S.parse_context("100") + + def test_fit_parallel_follows_the_spec(self): + self.assertEqual([S.fit_parallel(v) for v in (8, 12, 16, 20, 24, 32, 48)], [1, 1, 1, 2, 3, 4, 4]) + self.assertEqual(S.fit_parallel(None), 1) + + def test_words_and_labels(self): + self.assertEqual(S.context_label(131072), "128K") + self.assertEqual(S.words_for(131072), "about 98,000 words") + + def test_parallel_warning_is_a_warning_not_an_error(self): + notes = S.check_parallel(S.LaunchSettings(parallel=4), vram_gb=12) + self.assertEqual([n.level for n in notes], ["warn"]) + self.assertIn("still start", notes[0].line()) + self.assertEqual(S.check_parallel(S.LaunchSettings(parallel=9), vram_gb=12)[0].level, "error") + self.assertEqual(S.check_parallel(S.LaunchSettings(parallel=2), vram_gb=24), []) + + def test_access_notes(self): + self.assertEqual(S.check_access(S.LaunchSettings(access="local")), []) + lan = S.check_access(S.LaunchSettings(access="lan")) + self.assertEqual([n.level for n in lan], ["warn"]) + self.assertIn("API key", lan[0].line()) + bad = S.check_access(S.LaunchSettings(access="lann")) + self.assertEqual(bad[0].level, "error") + self.assertIn("lan", bad[0].line()) + self.assertEqual(S.check_port("abc").level, "error") + self.assertEqual(S.check_port(70000).level, "error") + self.assertEqual(S.check_port(80).level, "warn") + self.assertIsNone(S.check_port(8080)) + + def test_split_setting(self): + self.assertEqual(S.split_setting("prefill=65536"), ("--prefill", "65536")) + self.assertEqual(S.split_setting("--kv-grow"), ("--kv-grow", "")) + self.assertEqual(S.split_setting("STRATA_PIN_BY_NEED=1"), ("STRATA_PIN_BY_NEED", "1")) + self.assertEqual(S.split_setting("expert_cache=auto"), ("--expert-cache", "auto")) + with self.assertRaises(ValueError): + S.split_setting("no spaces allowed=1") + + def test_flags_text_round_trips_to_the_same_choices(self): + s = S.LaunchSettings(family="qwen", model="IQ3_XXS", context=131072, parallel=2, tune="quick", access="lan", port=9000, + extra={"--prefill": "65536", "STRATA_PIN_BY_NEED": "1"}) + text = S.flags_text(s) + for part in ("--model iq3_xxs", "--context 128k", "--parallel 2", "--tune quick", "--access lan", "--port 9000", + "--set prefill=65536", "--set STRATA_PIN_BY_NEED=1"): + self.assertIn(part, text) + self.assertIn('--preset "Fast"', S.flags_text(s, preset="Fast")) + + def test_slug(self): + self.assertEqual(S.slug("IQ3_XXS 128K - Full speed"), "iq3_xxs-128k-full-speed") + self.assertTrue(S.slug("123 go")[0].isalpha()) + + def test_roundtrip_dict(self): + s = S.LaunchSettings(name="a", model="Q2_0", family="qwen", context=32768, extra={"--spec": "3"}, tuned_preset="abc") + again = S.LaunchSettings.from_dict(json.loads(json.dumps(s.to_dict()))) + self.assertEqual(again, s) + self.assertEqual(s.to_dict(with_key=False)["api_key"], None) + self.assertEqual(S.LaunchSettings(api_key="sk-x").to_dict(with_key=False)["api_key"], "set") + + +class CatalogRules(unittest.TestCase): + def test_one_source_of_truth_and_fit_numbers(self): + with Home(): + cat = catalog(HW_12) + rows = cat.rows() + ids = {(r.family, r.model) for r in rows} + self.assertIn(("qwen", "IQ3_XXS"), ids) + models, families, contexts, source = cat.tables() + self.assertEqual(source, "setup.py") # the same tables setup.py and the app read + for r in rows: + self.assertAlmostEqual(r.download_gb, models[r.model]["download_gb"]) + rec = [r for r in rows if r.recommended] + self.assertEqual([(r.family, r.model) for r in rec], [("qwen", "IQ3_XXS")]) + + def test_fit_on_matches_native_fit_ts(self): + # apps/host/src/lib/native-fit.ts: memory = experts + 1; GPU holds experts up to vram - 5; RAM holds up to ram - 10; the rest spills + f = fit_on({"experts_gb": 42.9, "ram_needed_gb": 60}, 64.0, 12.0) + self.assertEqual((f["memoryGb"], f["vramUseGb"], f["spillGb"]), (43.9, 12.0, 0.0)) + self.assertAlmostEqual(f["ramUseGb"], 35.9, places=1) + spill = fit_on({"experts_gb": 98.4, "ram_needed_gb": 108}, 64.0, 12.0) + self.assertGreater(spill["spillGb"], 30) + self.assertFalse(fit_on({"experts_gb": 10}, None, 12)["known"]) + gyro = fit_on({"mem_total": True, "ram_needed_gb": 27.6}, 64, 12) + self.assertEqual(gyro["memoryGb"], 27.6) + + def test_verdict_words_never_block(self): + self.assertEqual(verdict("fits")[0], "fits") + self.assertEqual(verdict("does not fit")[0], "no") + self.assertIn("can still try", verdict("does not fit")[1]) + self.assertEqual(verdict("tight")[0], "tight") + self.assertEqual(verdict("fits in the low-RAM mode (slower)")[0], "fits") + + def test_resolve_names_and_closest(self): + with Home(): + cat = catalog() + self.assertEqual(cat.resolve("iq3_xxs"), ("qwen", "IQ3_XXS")) + self.assertEqual(cat.resolve("IQ1_M"), ("coder", "IQ1_M")) + self.assertEqual(cat.resolve("iq3_xxs", "swift"), ("swift", "IQ3_XXS")) + with self.assertRaises(ValueError) as e: + cat.resolve("iq3xxs") + self.assertIn("Did you mean 'IQ3_XXS'", str(e.exception)) + with self.assertRaises(ValueError) as e: + cat.resolve("q2_0", "swiift") + self.assertIn("swift", str(e.exception)) + with self.assertRaises(ValueError) as e: + cat.resolve("IQ1_M", "swift") + self.assertIn("has no size", str(e.exception)) + + def test_small_computer_gets_warnings_not_blocks(self): + with Home(): + rows = catalog({**HW_12, "ram_gb": 32.0}).rows() + big = [r for r in rows if r.model == "IQ3_S" and r.family == "qwen"][0] + self.assertIn(big.verdict, ("no", "tight")) + + def test_kv_figures(self): + with Home(): + cat = catalog() + self.assertAlmostEqual(cat.kv_gb(131072, "int8"), 1.799, places=2) + self.assertLess(cat.kv_gb(131072, "q4_0"), cat.kv_gb(131072, "int8")) + self.assertEqual(cat.max_context("qwen", "IQ3_XXS", HW_12), 32768) + self.assertEqual(cat.max_context("qwen", "IQ3_XXS", HW_24), 131072) + + def test_missing_installer_files_are_said_in_words(self): + with Home(): + with self.assertRaises(CatalogUnavailable) as e: + Catalog(src="/nonexistent-folder", hardware=HW_12) + self.assertIn("one-line installer", str(e.exception)) + + +class PresetStore(unittest.TestCase): + def test_save_load_list_delete(self): + with Home(): + s = S.LaunchSettings(name="IQ3_XXS 128K", family="qwen", model="IQ3_XXS", context=131072, api_key="sk-secret") + store.save(s) + store.save(S.LaunchSettings(name="Coder 64K", family="coder", model="IQ1_M", context=65536)) + names = [p.name for p in store.list_presets()] + self.assertEqual(sorted(names), ["Coder 64K", "IQ3_XXS 128K"]) + back = store.load("IQ3_XXS 128K") + self.assertEqual((back.model, back.context, back.api_key), ("IQ3_XXS", 131072, "sk-secret")) + store.touch_used("IQ3_XXS 128K") + self.assertTrue(store.load("IQ3_XXS 128K").last_used) + self.assertEqual(store.list_presets()[0].name, "IQ3_XXS 128K") # most recently used first + store.delete("Coder 64K") + self.assertEqual([p.name for p in store.list_presets()], ["IQ3_XXS 128K"]) + with self.assertRaises(store.PresetError) as e: + store.load("IQ3 XXS 128") + self.assertIn("Did you mean", str(e.exception)) + with self.assertRaises(store.PresetError): + store.delete("nope") + + def test_key_is_not_world_readable_on_posix(self): + if os.name == "nt": + self.skipTest("POSIX modes") + with Home(): + store.save(S.LaunchSettings(name="k", api_key="sk-secret")) + path = next(store.preset_dir().glob("*.json")) + self.assertEqual(path.stat().st_mode & 0o077, 0) + + +class Advanced(unittest.TestCase): + HELP = """strata generate --pack DIR [options] + + --pack DIR the pack directory (default pack/full) + --kv fp16|int8 KV storage (plan v0.3 P7): int8 codes, half the + VRAM; default fp16 until the gate accepts int8 + --kv q4_0 4-bit K/V after a Hadamard rotation + --kv-grow the K/V takes VRAM only for the cells the requests reach + --native-rope experimental pinned text-only CUDA rotary arithmetic + --prefill N prompt chunk size (default 2048) +""" + + def test_parse_help(self): + ps = {p.name: p for p in params.parse_help(self.HELP)} + self.assertEqual(set(ps), {"--pack", "--kv", "--kv-grow", "--native-rope", "--prefill"}) + self.assertEqual(ps["--pack"].metavar, "DIR") + self.assertEqual(ps["--pack"].help_default, "pack/full") + self.assertEqual(ps["--kv"].values, ["fp16", "int8"]) + self.assertIn("4-bit", ps["--kv"].description) # a flag listed twice keeps both texts + self.assertTrue(ps["--native-rope"].experimental) + self.assertFalse(ps["--prefill"].experimental) + self.assertEqual(ps["--kv-grow"].metavar, "") + self.assertLessEqual(len(ps["--prefill"].short), 70) + + def test_rows_show_default_tuned_and_yours(self): + rows = {r.name: r for r in params.build_rows(self.HELP, ["--kv", "int8", "--prefill", "auto"], ["--kv", "int8", "--prefill", "auto:32768"], + {}, {}, {"--prefill": "65536"})} + self.assertEqual((rows["--prefill"].default, rows["--prefill"].tuned, rows["--prefill"].yours), ("auto", "auto:32768", "65536")) + self.assertEqual(rows["--kv"].yours, "") + self.assertIn("--native-rope", rows) + names = list(rows) + self.assertEqual(names[:2], ["--kv", "--prefill"]) # what this launch uses comes first + + def test_unknown_names_warn_and_are_kept(self): + rows = params.build_rows(self.HELP, [], None, {}, {}, {"--prefil": "1"}) + r = next(x for x in rows if x.name == "--prefil") + self.assertFalse(r.known) + warn = params.check_name("--prefil", ["--prefill", "--kv"]) + self.assertIn("Did you mean --prefill", warn) + self.assertIsNone(params.check_name("--prefill", ["--prefill"])) + self.assertIsNone(params.check_name("--prefil", None)) # no list to check against: no warning + + def test_extra_becomes_argv_and_env(self): + a, e = S.as_argv_changes({"--prefill": "65536", "--kv-grow": "", "--spec": "off", "STRATA_PIN_BY_NEED": "1"}, frozenset({"--kv-grow"})) + self.assertEqual(a, {"--prefill": "65536", "--kv-grow": True, "--spec": None}) + self.assertEqual(e, {"STRATA_PIN_BY_NEED": "1"}) + + +class RunConfig(unittest.TestCase): + BASE = {"exe": "component:strata-engine", "args": ["--native", "x.gguf", "--max-context", "32768", "--kv", "int8", "--prefill", "auto"], + "port": 8095, "model_name": "m", "env": {}} + + def test_derive_applies_access_key_and_advanced(self): + s = S.LaunchSettings(access="lan", port=9001, api_key="sk-k", extra={"--prefill": "65536", "--kv-grow": "", "STRATA_PIN_BY_NEED": "1"}) + cfg = derive_config(self.BASE, s, bool_flags=frozenset({"--kv-grow"})) + self.assertEqual((cfg["host"], cfg["port"], cfg["api_key"]), ("0.0.0.0", 9001, "sk-k")) + args = cfg["args"] + self.assertEqual(args[args.index("--prefill") + 1], "65536") + self.assertIn("--kv-grow", args) + self.assertEqual(cfg["env"], {"STRATA_PIN_BY_NEED": "1"}) + self.assertEqual(self.BASE["args"][-1], "auto") # the installed config is never edited + + def test_local_and_cloudflare_stay_on_loopback_without_a_key(self): + for kind in ("local", "cloudflare"): + cfg = derive_config(self.BASE, S.LaunchSettings(access=kind), bool_flags=frozenset()) + self.assertEqual(cfg["host"], "127.0.0.1") + self.assertNotIn("api_key", cfg) + + def test_installed_config_matching(self): + d = {"context": 32768, "kv": "int8", "images": "off"} + self.assertTrue(desc_matches(d, S.LaunchSettings(context=32768))) + self.assertFalse(desc_matches(d, S.LaunchSettings(context=65536))) + self.assertFalse(desc_matches(d, S.LaunchSettings(context=32768, vision="on"))) + self.assertFalse(desc_matches({**d, "images": "cpu"}, S.LaunchSettings(context=32768, vision="off"))) + self.assertFalse(desc_matches(d, S.LaunchSettings(context=32768, kv="q4_0"))) + + def test_tune_words(self): + self.assertEqual(tune_words({"outcome": "improved", "gain_pct": 4.2})[0], "faster") + self.assertEqual(tune_words({"outcome": "default_confirmed"})[0], "already fastest") + self.assertEqual(tune_words({"outcome": "inconclusive", "reason_code": "noisy"})[0], "busy PC") + self.assertEqual(tune_words({"outcome": "base_failed", "message": "Not tuned: no memory"})[0], "failed") + self.assertEqual(tune_words(None)[0], "failed") + self.assertEqual(tune_words({"outcome": "cancelled"})[0], "stopped") + + +class AccessRules(unittest.TestCase): + def test_hosts_and_keys(self): + self.assertEqual(access.host_for("local"), "127.0.0.1") + self.assertEqual(access.host_for("cloudflare"), "127.0.0.1") + self.assertEqual(access.host_for("lan"), "0.0.0.0") + self.assertTrue(access.new_key().startswith("sk-strata-")) + self.assertNotIn(access.new_key(), access.mask_key(access.new_key())) + self.assertEqual(access.mask_key(None), "off") + + def test_private_ranges(self): + for ip in ("10.1.2.3", "192.168.88.21", "172.16.0.9", "100.101.1.1"): + self.assertTrue(access._is_private(ip), ip) + for ip in ("8.8.8.8", "172.32.0.1", "1.2.3.4"): + self.assertFalse(access._is_private(ip), ip) + + def test_missing_helpers_say_how_to_install(self): + with mock.patch.object(access, "find_cloudflared", return_value=None): + msg = access.missing_helper("cloudflare") + self.assertIn("cloudflared is not installed", msg) + self.assertIn("quick tunnel", msg) + with mock.patch.object(access, "find_tailscale", return_value=None): + self.assertIn("Tailscale is not installed", access.missing_helper("tailscale")) + with mock.patch.object(access, "tailscale_status", return_value={"installed": True, "running": False, "ip": None, "name": None}): + self.assertIn("tailscale up", access.missing_helper("tailscale")) + self.assertIsNone(access.missing_helper("local")) + + def test_options_and_urls(self): + with mock.patch.object(access, "lan_ips", return_value=["192.168.1.5"]), \ + mock.patch.object(access, "find_cloudflared", return_value="/bin/cloudflared"), \ + mock.patch.object(access, "tailscale_status", return_value={"installed": True, "running": True, "ip": "100.64.0.1", "name": "pc.tail.ts.net"}): + opts = {o.kind: o for o in access.options(8080)} + self.assertEqual(opts["lan"].url, "http://192.168.1.5:8080") + self.assertTrue(opts["cloudflare"].ready) + self.assertEqual(opts["tailscale"].url, "http://pc.tail.ts.net:8080") + urls = access.urls_for(S.LaunchSettings(access="lan", port=8080)) + self.assertEqual(urls, ["http://127.0.0.1:8080", "http://192.168.1.5:8080"]) + self.assertEqual(access.urls_for(S.LaunchSettings(access="cloudflare", port=8080), tunnel_url="https://a.trycloudflare.com")[0], + "https://a.trycloudflare.com") + + def test_helper_output_filter_drops_login_lines(self): + self.assertEqual(access.filter_line("Please open this URL to login: https://dash.cloudflare.com/argotunnel?token=abc"), "") + self.assertEqual(access.filter_line("Your quick Tunnel has been created: https://x.trycloudflare.com"), + "Your quick Tunnel has been created: https://x.trycloudflare.com") + self.assertEqual(access.TUNNEL_URL.search("| https://calm-river-1234.trycloudflare.com |").group(0), + "https://calm-river-1234.trycloudflare.com") + + +class FlagsCommand(unittest.TestCase): + """Every `strata launch` flag, run through the real parser with the computer faked and nothing installed.""" + + def setUp(self): + self.home = Home().__enter__() + self.addCleanup(self.home.__exit__, None, None, None) + p = mock.patch.object(Catalog, "hardware", lambda self, fresh=False: dict(HW_12, disk_free_gb=500.0)) + p.start() + self.addCleanup(p.stop) + + def plan(self, *args): + code, out, err = run_cli(["launch", "--plan", "--output", "json", *args]) + self.assertEqual(code, 0, err or out) + return json.loads(out)["plan"] + + def test_help_lists_everything_with_examples(self): + code, out, err = run_cli(["launch", "--help"]) + self.assertEqual(code, 0) + for flag in ("--preset", "--model", "--family", "--model-path", "--data-dir", "--context", "--kv", "--vision", "--parallel", "--gpu", + "--tune", "--tune-allow-small", "--set", "--access", "--port", "--api-key", "--api-key-env", "--new-key", "--save-preset", + "--no-tui", "--plan", "--feed", "--list-presets", "--delete-preset", "--status", "--stop", "--show-key", "examples:"): + self.assertIn(flag, out) + code, out, _ = run_cli(["--help"]) + self.assertIn("launch", out) + self.assertIn("connect", out) + + def test_model_context_parallel_vision_tune_access_port(self): + p = self.plan("--model", "iq3_xxs", "--context", "128k", "--parallel", "2", "--vision", "off", "--tune", "quick", "--access", "lan", + "--port", "9090") + s = p["settings"] + self.assertEqual((s["family"], s["model"], s["context"], s["parallel"], s["vision"], s["tune"], s["access"], s["port"]), + ("qwen", "IQ3_XXS", 131072, 2, "off", "quick", "lan", 9090)) + self.assertEqual(s["api_key"], "set") # shared beyond this computer: a key was made + self.assertTrue(any("2 requests" in w and "12 GB" in w for w in p["warnings"])) + + def test_defaults_come_from_the_recommendation(self): + p = self.plan() + s = p["settings"] + self.assertEqual((s["family"], s["model"], s["context"], s["access"], s["tune"]), ("qwen", "IQ3_XXS", 32768, "local", "skip")) + self.assertTrue(any("picked" in w for w in p["warnings"])) + + def test_family_and_kv_and_gpu_and_data_dir_and_set(self): + p = self.plan("--model", "iq3_xxs", "--family", "swift", "--kv", "q4_0", "--gpu", "0,1", "--data-dir", "/data/x", + "--set", "prefill=65536", "--set", "STRATA_PIN_BY_NEED=1", "--tune-allow-small") + s = p["settings"] + self.assertEqual((s["family"], s["kv"], s["gpus"], s["data_dir"], s["tune_allow_small"]), ("swift", "q4_0", [0, 1], "/data/x", True)) + self.assertEqual(s["extra"], {"--prefill": "65536", "STRATA_PIN_BY_NEED": "1"}) + self.assertIn("--set prefill=65536", p["flags"]) + + def test_api_key_flags(self): + p = self.plan("--model", "q2_0", "--access", "lan", "--api-key", "sk-mine") + self.assertEqual(p["settings"]["api_key"], "set") + self.assertFalse(any("new API key was made" in w for w in p["warnings"])) + with mock.patch.dict(os.environ, {"MY_KEY": "sk-env"}): + p = self.plan("--model", "q2_0", "--api-key-env", "MY_KEY") + self.assertEqual(p["settings"]["api_key"], "set") + code, out, err = run_cli(["launch", "--plan", "--api-key-env", "NOT_SET_ANYWHERE"]) + self.assertEqual(code, 2) + self.assertIn("NOT_SET_ANYWHERE", err) + p = self.plan("--model", "q2_0", "--new-key") + self.assertEqual(p["settings"]["api_key"], "set") + + def test_model_path_is_used_instead_of_a_catalog_model(self): + p = self.plan("--model-path", "/data/models/IQ3_XXS") + s = p["settings"] + self.assertEqual(s["model_path"], str(Path("/data/models/IQ3_XXS"))) + self.assertIsNone(s["model"]) + + def test_unknown_model_names_the_closest_one(self): + code, out, err = run_cli(["launch", "--plan", "--model", "iq3xxs"]) + self.assertEqual(code, 2) + self.assertIn("Did you mean 'IQ3_XXS'", err) + self.assertNotIn("Traceback", err) + + def test_unknown_flag_names_the_closest_one(self): + code, out, err = run_cli(["launch", "--modle", "iq3_xxs"]) + self.assertEqual(code, 2) + self.assertIn("Did you mean '--model'", err) + + def test_bad_values_are_plain_sentences(self): + for argv, word in ((["--tune", "quik"], "quick"), (["--access", "lann"], "lan"), (["--context", "huge"], "context"), + (["--port", "99999"], "1 to 65535"), (["--parallel", "12"], "1 to 8"), (["--vision", "maybe"], "off")): + code, out, err = run_cli(["launch", "--plan", *argv]) + self.assertEqual(code, 2, argv) + self.assertIn(word, err, argv) + + def test_preset_with_flags_on_top(self): + store.save(S.LaunchSettings(name="Office", family="qwen", model="IQ3_XXS", context=65536, parallel=2, access="local")) + p = self.plan("--preset", "Office", "--context", "128k") + s = p["settings"] + self.assertEqual((s["name"], s["context"], s["parallel"]), ("Office", 131072, 2)) + code, out, err = run_cli(["launch", "--plan", "--preset", "Offce"]) + self.assertEqual(code, 2) + self.assertIn("Did you mean 'Office'", err) + + def test_vision_is_turned_off_for_a_model_without_pictures(self): + p = self.plan("--model", "gyro-s", "--vision", "on") + self.assertEqual(p["settings"]["vision"], "off") + self.assertTrue(any("cannot read pictures" in w for w in p["warnings"])) + + def test_does_not_fit_is_a_warning_with_the_reason(self): + p = self.plan("--model", "nvfp4") + self.assertTrue(any("does not fit" in w for w in p["warnings"])) + + def test_human_plan_changes_nothing(self): + code, out, err = run_cli(["launch", "--plan", "--model", "q2_0", "--save-preset", "X"]) + self.assertEqual(code, 0) + self.assertIn("Nothing was changed.", out) + self.assertEqual(store.list_presets(), []) + + def test_run_without_screens_prints_the_address_and_saves_the_preset(self): + fake = Result(profile="launch-office", urls=["http://127.0.0.1:8080", "http://192.168.1.5:8080"], key="sk-strata-abcdefghijkl", + tune_word=None, notices=[], config="/x") + with mock.patch.object(Runner, "run", return_value=fake): + code, out, err = run_cli(["launch", "--no-tui", "--model", "q2_0", "--access", "lan", "--save-preset", "Office"]) + self.assertEqual(code, 0, err) + self.assertIn("Strata is serving.", out) + self.assertIn("http://127.0.0.1:8080/v1", out) + self.assertIn("http://192.168.1.5:8080/v1", out) + self.assertNotIn("sk-strata-abcdefghijkl", out) # the key is masked + self.assertIn("strata connect", out) + self.assertEqual([p.name for p in store.list_presets()], ["Office"]) + code, out, err = run_cli(["launch", "--list-presets"]) + self.assertIn("Office", out) + code, out, _ = run_cli(["launch", "--show-key", "--preset", "Office"]) + self.assertEqual(code, 0) # the preset's own key + self.assertTrue(out.strip().startswith("sk-strata-")) + + def test_launch_error_is_one_plain_sentence(self): + from strata_cli.launch.runner import LaunchError + with mock.patch.object(Runner, "run", side_effect=LaunchError("The engine could not be installed.", "Check your internet.", 6)): + code, out, err = run_cli(["launch", "--no-tui", "--model", "q2_0"]) + self.assertEqual(code, 6) + self.assertIn("The engine could not be installed. Check your internet.", err) + self.assertNotIn("Traceback", err) + + def test_list_delete_status_stop_without_anything_running(self): + store.save(S.LaunchSettings(name="A", family="qwen", model="Q2_0", context=32768)) + code, out, _ = run_cli(["launch", "--list-presets"]) + self.assertIn("A", out) + code, out, _ = run_cli(["launch", "--delete-preset", "A"]) + self.assertEqual(code, 0) + self.assertIn("Models and tuning results were not touched", out) + code, out, _ = run_cli(["launch", "--list-presets"]) + self.assertIn("No saved presets", out) + code, out, _ = run_cli(["launch", "--status"]) + self.assertEqual(code, 0) + self.assertIn("Nothing from `strata launch` is running.", out) + code, out, _ = run_cli(["launch", "--stop"]) + self.assertEqual(code, 0) + self.assertIn("Nothing", out) + code, out, err = run_cli(["launch", "--delete-preset", "gone"]) + self.assertEqual(code, 2) + self.assertIn("no saved preset", err) + + def test_feed_flag_reaches_the_runner(self): + seen = {} + + def fake(self, *a, **k): + seen["feed"] = self.feed + return Result("launch-x", ["http://127.0.0.1:8080"], None, None, [], "/x") + + with mock.patch.object(Runner, "run", fake): + code, _, err = run_cli(["launch", "--no-tui", "--model", "q2_0", "--feed", "http://127.0.0.1:9/feed"]) + self.assertEqual(code, 0, err) + self.assertEqual(seen["feed"], "http://127.0.0.1:9/feed") + + def test_output_json_for_scripts(self): + fake = Result("launch-x", ["http://127.0.0.1:8080"], "sk-k", "faster", ["a note"], "/x") + with mock.patch.object(Runner, "run", return_value=fake): + code, out, err = run_cli(["launch", "--no-tui", "--output", "json", "--model", "q2_0"]) + data = json.loads(out) + self.assertTrue(data["ok"]) + self.assertEqual(data["urls"], ["http://127.0.0.1:8080"]) + self.assertEqual(data["api_key"], "set") # never the key itself + self.assertEqual(data["tuned"], "faster") + + +class ConnectCommand(unittest.TestCase): + def setUp(self): + self.home = Home().__enter__() + self.addCleanup(self.home.__exit__, None, None, None) + + def test_list_and_unknown_names(self): + code, out, _ = run_cli(["connect"]) + self.assertEqual(code, 0) + for h in ("opencode", "codex", "claude-code", "omp", "pi", "hermes", "openclaw", "deepseek-harness"): + self.assertIn(h, out) + code, out, err = run_cli(["connect", "opncode"]) + self.assertEqual(code, 2) + self.assertIn("Did you mean 'opencode'", err) + code, out, _ = run_cli(["connect", "--output", "json"]) + self.assertEqual(len(json.loads(out)["harnesses"]), 8) + + def test_prints_the_config_with_a_masked_key(self): + code, out, err = run_cli(["connect", "opencode", "--url", "http://127.0.0.1:8123", "--key", "sk-strata-abcdefghij1234", "--model", "m1"]) + self.assertEqual(code, 0, err) + self.assertIn("http://127.0.0.1:8123/v1", out) + self.assertIn("opencode.json", out) + self.assertNotIn("abcdefghij1234", out) + code, out, _ = run_cli(["connect", "opencode", "--url", "http://127.0.0.1:8123", "--key", "sk-strata-abcdefghij1234", "--show-key"]) + self.assertIn("sk-strata-abcdefghij1234", out) + + def test_claude_code_uses_the_anthropic_variables_without_a_login(self): + code, out, _ = run_cli(["connect", "claude-code", "--url", "http://127.0.0.1:8080", "--key", "sk-x1234567890abc", "--show-key"]) + self.assertIn("ANTHROPIC_BASE_URL", out) + self.assertIn("ANTHROPIC_AUTH_TOKEN", out) + self.assertNotIn("/v1", out.split("ANTHROPIC_BASE_URL")[1].splitlines()[0]) # the Anthropic client adds /v1 itself + + def test_write_keeps_a_backup_and_merges(self): + target = Path(os.environ["HOME"]) / ".config" / "opencode" / "opencode.json" + target.parent.mkdir(parents=True) + target.write_text(json.dumps({"theme": "dark", "provider": {"other": {"a": 1}}})) + code, out, err = run_cli(["connect", "opencode", "--url", "http://127.0.0.1:8080", "--key", "sk-x1234567890abc", "--write", "--yes"]) + self.assertEqual(code, 0, err) + data = json.loads(target.read_text()) + self.assertEqual(data["theme"], "dark") + self.assertIn("other", data["provider"]) + self.assertIn("strata", data["provider"]) + backups = list(target.parent.glob("opencode.json.strata-bak-*")) + self.assertEqual(len(backups), 1) + self.assertEqual(json.loads(backups[0].read_text())["theme"], "dark") + self.assertIn("backup", out) + + def test_write_asks_first_without_yes(self): + code, out, err = run_cli(["connect", "opencode", "--write"]) + self.assertEqual(code, 2) + self.assertIn("asks first", err) + + def test_built_in_harness_has_nothing_to_write(self): + code, out, _ = run_cli(["connect", "deepseek-harness", "--write", "--yes"]) + self.assertEqual(code, 0) + self.assertIn("Nothing to write", out) + + def test_untested_drafts_say_so(self): + h = harness.find("hermes") + if not h.tested: + code, out, _ = run_cli(["connect", "hermes"]) + self.assertIn("not been tested", out) + + +if __name__ == "__main__": + unittest.main()