AQIT 0.1.0
Loading...
Searching...
No Matches
main.py
Go to the documentation of this file.
1# Copyright (c) 2025-present Aquin Labs Private Limited. All Rights Reserved.
2# This file is part of the Aquin Engine. Unauthorized copying, modification,
3# distribution, or use of this file, via any medium, is strictly prohibited.
4# Proprietary and confidential. See LICENSE for terms.
5
6from __future__ import annotations
7
8import json
9import os
10import re
11from pathlib import Path
12from typing import Any
13
14_STATE_PATH = Path.home() / ".aquin" / "engine.json"
15_BASE_URL_DEFAULT = "https://api.aquin.app"
16_API_URL_RE = re.compile(r"https?://api\.aquin\.app(?:/\S*)?")
17
19def scrub_user_message(text: str) -> str:
20 """Strip internal API host from strings shown in the terminal."""
21 return _API_URL_RE.sub("Aquin cloud", str(text))
22
24def _base_url() -> str:
25 return os.environ.get("AQUIN_BASE_URL", _BASE_URL_DEFAULT).rstrip("/")
26
27
28def _load_state() -> dict:
29 if _STATE_PATH.exists():
30 try:
31 return json.loads(_STATE_PATH.read_text())
32 except Exception:
33 pass
34 return {}
35
36
37def _save_state(data: dict) -> None:
38 _STATE_PATH.parent.mkdir(parents=True, exist_ok=True)
39 _STATE_PATH.write_text(json.dumps(data, indent=2))
40
42def resolve_api_key(*, allow_missing: bool = False) -> str:
43 """Saved login (config.json / env) — never prompts interactively."""
44 from aquin.auth_config import resolve_api_key as _resolve
45 return _resolve(allow_missing=allow_missing)
47
49 """Copy logged-in credentials into engine.json when missing."""
50 state = _load_state()
51 api_key = resolve_api_key(allow_missing=True)
52 base_url = (state.get("base_url") or _base_url()).rstrip("/")
53 patch: dict[str, Any] = {}
54 if api_key and state.get("api_key") != api_key:
55 patch["api_key"] = api_key
56 if state.get("base_url") != base_url:
57 patch["base_url"] = base_url
58 if patch:
59 _save_state({**state, **patch})
60
61
62def format_engine_status_lines() -> list[str]:
63 """Account-adjacent engine state for `aquin status`."""
64 from aquin.compute.model_loader import get_active_model_id
65 from aquin.session_mode import mode_for_active_model, mode_label
67 state = _load_state()
68 model_id = get_active_model_id()
69 mode = mode_for_active_model()
70
71 lines: list[str] = []
72 if model_id:
73 lines.append(f"Model: {model_id}")
74 if mode:
75 lines.append(f" mode: {mode_label(mode)}")
76 else:
77 lines.append("Model: (none — run: aquin load --model <id>)")
78
79 if state.get("device"):
80 lines.append(f"Device: {state.get('device')}")
81 loc = state.get("location") or {}
82 if isinstance(loc, dict) and loc.get("location_label"):
83 lines.append(f" location: {loc['location_label']}")
84 gpu_info = state.get("gpu_info")
85 if gpu_info:
86 try:
87 parsed = json.loads(gpu_info) if isinstance(gpu_info, str) else gpu_info
88 gpus = parsed.get("gpus") or []
89 if gpus:
90 g = gpus[0]
91 lines.append(f" gpu: {g.get('name', '?')} ({g.get('vram_gb', '?')} GB)")
92 except Exception:
93 pass
94 return lines
str resolve_api_key(*, bool allow_missing=False)
Definition main.py:46
dict _load_state()
Definition main.py:32
list[str] format_engine_status_lines()
Definition main.py:66
None _save_state(dict data)
Definition main.py:41
None ensure_engine_auth_persisted()
Definition main.py:52
str _base_url()
Definition main.py:28
str scrub_user_message(str text)
Definition main.py:23