AQIT
0.1.0
Toggle main menu visibility
Loading...
Searching...
No Matches
tool_exec.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
"""
7
Headless tool runner for npm / @aquin/engine-client spawn fallback.
8
9
Usage:
10
echo '{"name":"read_session_memory","args":{"key":"x"},"ctx":{}}' \\
11
| python -m aquin.engine.tool_exec
12
13
Prints **exactly one** JSON object on stdout (logs must go to stderr).
14
"""
15
from
__future__
import
annotations
16
17
import
json
18
import
sys
19
from
typing
import
Any
20
21
from
aquin.engine.json_safe
import
dumps_json_safe
22
23
24
def
_read_payload
() -> dict[str, Any]:
25
raw = sys.stdin.read()
26
if
not
raw.strip():
27
raise
ValueError(
"expected JSON payload on stdin"
)
28
data = json.loads(raw)
29
if
not
isinstance(data, dict):
30
raise
ValueError(
"payload must be a JSON object"
)
31
return
data
32
33
34
def
_emit
(payload: dict[str, Any]) ->
None
:
35
# Single line, strict JSON (no NaN/Inf) so Node JSON.parse never fails.
36
sys.stdout.write(dumps_json_safe(payload) +
"\n"
)
37
sys.stdout.flush()
38
39
40
def
main
() -> int:
41
try
:
42
payload =
_read_payload
()
43
name = payload.get(
"name"
)
44
if
not
name:
45
raise
ValueError(
"name is required"
)
46
args = payload.get(
"args"
)
or
{}
47
ctx = payload.get(
"ctx"
)
or
{}
48
if
not
isinstance(args, dict):
49
raise
ValueError(
"args must be an object"
)
50
if
not
isinstance(ctx, dict):
51
raise
ValueError(
"ctx must be an object"
)
52
53
from
aquin.engine.sync_dispatch
import
run_dispatch
54
from
aquin.engine.tools.registry
import
_load_stubs
55
56
_load_stubs()
57
result = run_dispatch(str(name), args, ctx)
58
if
isinstance(result, dict)
and
result.get(
"error"
):
59
_emit
({
"ok"
:
False
,
"error"
: result[
"error"
],
"result"
: result})
60
return
1
61
_emit
({
"ok"
:
True
,
"result"
: result})
62
return
0
63
except
Exception
as
exc:
64
_emit
({
"ok"
:
False
,
"error"
: str(exc)})
65
return
1
66
67
68
if
__name__ ==
"__main__"
:
69
sys.exit(
main
())
aquin.engine.json_safe
Definition
json_safe.py:1
aquin.engine.sync_dispatch
Definition
sync_dispatch.py:1
aquin.engine.tool_exec._read_payload
dict[str, Any] _read_payload()
Definition
tool_exec.py:28
aquin.engine.tool_exec._emit
None _emit(dict[str, Any] payload)
Definition
tool_exec.py:38
aquin.engine.tool_exec.main
int main()
Definition
tool_exec.py:44
aquin.engine.tools.registry
Definition
registry.py:1
aquin
engine
tool_exec.py
AQIT · Aquin Labs Private Limited · Apache 2.0 · Generated by
1.18.0