File size: 1,834 Bytes
49aa045 | 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
"""Public agent client; depends only on the Python standard library."""
import argparse
import json
import os
import sys
import urllib.error
import urllib.request
def main():
parser = argparse.ArgumentParser(description="Interact with the escape room")
parser.add_argument(
"action",
choices=[
"observe",
"state",
"investigate",
"tool-call",
"use-item",
"submit-answer",
],
)
parser.add_argument("value", nargs="?")
parser.add_argument("--arguments-json", default="{}")
args = parser.parse_args()
base = os.environ.get("ESCAPE_URL", "http://puzzle:8000").rstrip("/")
if args.action in ("observe", "state"):
request = urllib.request.Request(
base + ("/observation" if args.action == "observe" else "/state")
)
else:
if args.value is None:
parser.error("node ID or answer is required")
payload = {"action": args.action.replace("-", "_")}
if args.action == "submit-answer":
payload["answer"] = args.value
else:
payload["node_id"] = args.value
if args.action in ("tool-call", "use-item"):
payload["arguments"] = json.loads(args.arguments_json)
request = urllib.request.Request(
base + "/action",
data=json.dumps(payload).encode(),
headers={"Content-Type": "application/json"},
)
try:
with urllib.request.urlopen(request, timeout=30) as response:
print(json.dumps(json.load(response), ensure_ascii=False))
except (urllib.error.URLError, ValueError) as exc:
print(json.dumps({"ok": False, "error": str(exc)}))
sys.exit(2)
if __name__ == "__main__":
main()
|