AQIT
0.1.0
Toggle main menu visibility
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
6
from
__future__
import
annotations
7
8
import
json
9
import
os
10
import
re
11
from
pathlib
import
Path
12
from
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
18
19
def
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
23
24
def
_base_url
() -> str:
25
return
os.environ.get(
"AQUIN_BASE_URL"
, _BASE_URL_DEFAULT).rstrip(
"/"
)
26
27
28
def
_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
37
def
_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
41
42
def
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)
46
47
48
def
ensure_engine_auth_persisted
() -> None:
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
62
def
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
66
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
aquin.auth_config
Definition
auth_config.py:1
aquin.compute.model_loader
Definition
model_loader.py:1
aquin.engine.main.resolve_api_key
str resolve_api_key(*, bool allow_missing=False)
Definition
main.py:46
aquin.engine.main._load_state
dict _load_state()
Definition
main.py:32
aquin.engine.main.format_engine_status_lines
list[str] format_engine_status_lines()
Definition
main.py:66
aquin.engine.main._save_state
None _save_state(dict data)
Definition
main.py:41
aquin.engine.main.ensure_engine_auth_persisted
None ensure_engine_auth_persisted()
Definition
main.py:52
aquin.engine.main._base_url
str _base_url()
Definition
main.py:28
aquin.engine.main.scrub_user_message
str scrub_user_message(str text)
Definition
main.py:23
aquin.session_mode
Definition
session_mode.py:1
aquin
engine
main.py
AQIT · Aquin Labs Private Limited · Apache 2.0 · Generated by
1.18.0