AQIT
0.1.0
Toggle main menu visibility
Loading...
Searching...
No Matches
_runtime.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
"""Shared runtime for the Aquin SDK — same engine path the CLI uses."""
7
8
from
__future__
import
annotations
9
10
import
os
11
from
typing
import
Any
12
13
14
class
AquinError
(RuntimeError):
15
"""Raised when an SDK call returns ``{"error": ...}`` or fails to run."""
16
17
def
__init__(self, message: str, *, result: dict[str, Any] |
None
=
None
):
18
super().
__init__
(message)
19
self.
result
= result
or
{}
20
21
22
def
build_ctx
(*, model_id: str |
None
=
None
, extra_state: dict[str, Any] |
None
=
None
) -> dict[str, Any]:
23
"""Build the tool context dict expected by bridge / registry / daemon."""
24
from
aquin.compute.model_loader
import
get_active_model_id, resolve_model_id
25
from
aquin.engine.main
import
resolve_api_key
26
from
aquin.engine.session_memory_store
import
load_local_memory
27
28
active = model_id
or
get_active_model_id()
or
""
29
if
active:
30
try
:
31
active = resolve_model_id(active)
32
except
ValueError:
33
pass
34
35
try
:
36
api_key = resolve_api_key(allow_missing=
True
)
or
""
37
except
Exception:
38
api_key =
""
39
40
base_url = os.environ.get(
"AQUIN_BASE_URL"
,
"https://api.aquin.app"
).rstrip(
"/"
)
41
try
:
42
from
aquin.engine.main
import
_load_state
43
44
persisted = _load_state()
or
{}
45
api_key = persisted.get(
"api_key"
)
or
api_key
46
base_url = (persisted.get(
"base_url"
)
or
base_url).rstrip(
"/"
)
47
except
Exception:
48
pass
49
50
local_mem = load_local_memory(
"local"
)
51
state: dict[str, Any] = {
52
"activeModelId"
: active,
53
"memory"
: local_mem
if
isinstance(local_mem, dict)
else
{},
54
}
55
try
:
56
from
aquin.session_mode
import
mode_for_model_id
57
58
if
active:
59
state[
"session_mode"
] = mode_for_model_id(active)
60
except
Exception:
61
pass
62
if
extra_state:
63
state.update(extra_state)
64
65
return
{
66
"session_id"
:
""
,
67
"api_key"
: api_key,
68
"base_url"
: base_url,
69
"cwd"
: os.getcwd(),
70
"state"
: state,
71
}
72
73
74
def
prepare_compute
(*, model_id: str |
None
=
None
) -> str:
75
"""Ensure loader shim + local server; return resolved active model id."""
76
from
aquin.compute.loader_shim
import
apply
as
shim_apply
77
from
aquin.compute.model_loader
import
get_active_model_id, resolve_model_id
78
from
aquin.engine.local_server
import
start
as
start_local_server
79
80
shim_apply()
81
start_local_server()
82
active = model_id
or
get_active_model_id()
or
""
83
if
active:
84
try
:
85
active = resolve_model_id(active)
86
except
ValueError:
87
pass
88
return
active
89
90
91
def
invoke
(
92
tool_name: str,
93
args: dict[str, Any] |
None
=
None
,
94
*,
95
command: str |
None
=
None
,
96
needs_model: bool =
True
,
97
model_id: str |
None
=
None
,
98
raise_on_error: bool =
True
,
99
) -> dict[str, Any]:
100
"""
101
Run a named compute tool through the same dispatch path as the CLI.
102
103
Parameters
104
----------
105
tool_name:
106
Bridge / registry tool id (e.g. ``run_consistency_eval``).
107
args:
108
Tool arguments (snake or camelCase accepted by most handlers).
109
command:
110
Optional CLI-style label for local command tracking.
111
needs_model:
112
If True, ensure a model is loaded / daemon-ready.
113
model_id:
114
Override active model for this call.
115
raise_on_error:
116
If True, raise :class:`AquinError` when the result has ``error``.
117
"""
118
from
aquin.engine.sync_dispatch
import
dispatch_model_free, run_dispatch
119
120
tool_args = dict(args
or
{})
121
active =
prepare_compute
(model_id=model_id)
if
needs_model
else
(model_id
or
""
)
122
if
not
needs_model
and
model_id:
123
try
:
124
from
aquin.compute.model_loader
import
resolve_model_id
125
126
active = resolve_model_id(model_id)
127
except
Exception:
128
active = model_id
129
if
needs_model
and
active:
130
tool_args.setdefault(
"model_id"
, active)
131
132
ctx =
build_ctx
(model_id=active
or
model_id)
133
134
if
needs_model:
135
result = run_dispatch(
136
tool_name,
137
tool_args,
138
ctx,
139
command=command
or
tool_name,
140
ensure_model=active
or
None
,
141
)
142
else
:
143
result = dispatch_model_free(
144
tool_name,
145
tool_args,
146
ctx,
147
command=command
or
tool_name,
148
)
149
150
if
not
isinstance(result, dict):
151
result = {
"content"
: result}
152
153
if
raise_on_error
and
result.get(
"error"
):
154
raise
AquinError
(str(result[
"error"
]), result=result)
155
return
result
156
157
158
def
invoke_direct
(
159
fn: Any,
160
*args: Any,
161
command: str |
None
=
None
,
162
tool_name: str |
None
=
None
,
163
track: bool =
True
,
164
**kwargs: Any,
165
) -> Any:
166
"""Call a compute function directly (for paths that skip the tool registry)."""
167
result = fn(*args, **kwargs)
168
if
track:
169
try
:
170
from
aquin.engine.sync_dispatch
import
track_cli_result
171
172
track_cli_result(
173
tool_name
or
(command
or
getattr(fn,
"__name__"
,
"direct"
)),
174
kwargs
if
kwargs
else
{
"args"
: list(args)},
175
result
if
isinstance(result, dict)
else
{
"result"
: result},
176
command=command,
177
)
178
except
Exception:
179
pass
180
if
isinstance(result, dict)
and
result.get(
"error"
):
181
raise
AquinError
(str(result[
"error"
]), result=result)
182
return
result
aquin.sdk._runtime.AquinError
Definition
_runtime.py:18
aquin.sdk._runtime.AquinError.__init__
__init__(self, str message, *, dict[str, Any]|None result=None)
Definition
_runtime.py:21
aquin.sdk._runtime.AquinError.result
result
Definition
_runtime.py:23
aquin.compute.loader_shim
Definition
loader_shim.py:1
aquin.compute.model_loader
Definition
model_loader.py:1
aquin.engine.local_server
Definition
local_server.py:1
aquin.engine.main
Definition
main.py:1
aquin.engine.session_memory_store
Definition
session_memory_store.py:1
aquin.engine.sync_dispatch
Definition
sync_dispatch.py:1
aquin.sdk._runtime.build_ctx
dict[str, Any] build_ctx(*, str|None model_id=None, dict[str, Any]|None extra_state=None)
Definition
_runtime.py:26
aquin.sdk._runtime.prepare_compute
str prepare_compute(*, str|None model_id=None)
Definition
_runtime.py:78
aquin.sdk._runtime.invoke
dict[str, Any] invoke(str tool_name, dict[str, Any]|None args=None, *, str|None command=None, bool needs_model=True, str|None model_id=None, bool raise_on_error=True)
Definition
_runtime.py:103
aquin.sdk._runtime.invoke_direct
Any invoke_direct(Any fn, *Any args, str|None command=None, str|None tool_name=None, bool track=True, **Any kwargs)
Definition
_runtime.py:169
aquin.session_mode
Definition
session_mode.py:1
aquin
sdk
_runtime.py
AQIT · Aquin Labs Private Limited · Apache 2.0 · Generated by
1.18.0