AQIT 0.1.0
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"""
7Headless tool runner for npm / @aquin/engine-client spawn fallback.
8
9Usage:
10 echo '{"name":"read_session_memory","args":{"key":"x"},"ctx":{}}' \\
11 | python -m aquin.engine.tool_exec
12
13Prints **exactly one** JSON object on stdout (logs must go to stderr).
14"""
15from __future__ import annotations
16
17import json
18import sys
19from typing import Any
20
21from aquin.engine.json_safe import dumps_json_safe
22
23
24def _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
34def _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()
39
40def 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
68if __name__ == "__main__":
69 sys.exit(main())
dict[str, Any] _read_payload()
Definition tool_exec.py:28
None _emit(dict[str, Any] payload)
Definition tool_exec.py:38