File size: 1,946 Bytes
339ddd8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/usr/bin/env python3
"""External Python topology node executable built on LingoLOGOS codex.

Reads one JSON envelope on stdin, injects the external PythonLanguageAdapter
when no explicit adapter or spec is already set on the dossier, delegates the
step to lgcodex.agents.run_step, and writes one JSON envelope to stdout.
"""

from __future__ import annotations

import argparse
import json
import sys
from pathlib import Path

PYTHON_ROOT = Path(__file__).resolve().parent.parent
if str(PYTHON_ROOT) not in sys.path:
    sys.path.insert(0, str(PYTHON_ROOT))

from lgpython._codex_bridge import ensure_codex_on_path  # noqa: E402

ensure_codex_on_path(PYTHON_ROOT)

from lgcodex import agents, envelope as _envelope  # noqa: E402


def main() -> int:
    parser = argparse.ArgumentParser(description="External Python topology node")
    parser.add_argument("--role", required=True, choices=sorted(agents.STEPS))
    parser.add_argument("--node-id", default=None)
    parser.add_argument(
        "--strict-tools",
        action="store_true",
        help="Use StrictPythonLanguageAdapter requiring mypy and ruff.",
    )
    args, _ignored = parser.parse_known_args()

    incoming = json.loads(sys.stdin.read())
    dossier = dict(_envelope.read_dossier(incoming))
    if not dossier.get("adapter") and not dossier.get("spec"):
        dossier["adapter"] = (
            "lgpython.adapter:StrictPythonLanguageAdapter"
            if args.strict_tools or dossier.get("strictTools")
            else "lgpython.adapter:PythonLanguageAdapter"
        )
        incoming = dict(incoming)
        payload = dict(incoming.get("payload", {}))
        payload["abstractSyntaxTree"] = json.dumps(dossier, sort_keys=True)
        incoming["payload"] = payload

    outgoing = agents.run_step(args.role, incoming, args.node_id or args.role)
    sys.stdout.write(json.dumps(outgoing))
    return 0


if __name__ == "__main__":
    raise SystemExit(main())