File size: 2,133 Bytes
2414d31
 
 
 
 
 
 
 
657f0fa
2414d31
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
"""HTTP smoke test for the running ClarifyRL server."""

import json
import sys
import time
from urllib import error, request


BASE = "http://127.0.0.1:7860"


def _post(path: str, payload: dict) -> tuple[int, str]:
    req = request.Request(
        BASE + path,
        data=json.dumps(payload).encode(),
        headers={"content-type": "application/json"},
        method="POST",
    )
    try:
        with request.urlopen(req, timeout=15) as resp:
            return resp.status, resp.read().decode()
    except error.HTTPError as e:
        return e.code, e.read().decode()


def _get(path: str) -> tuple[int, str]:
    try:
        with request.urlopen(BASE + path, timeout=15) as resp:
            return resp.status, resp.read().decode()
    except error.HTTPError as e:
        return e.code, e.read().decode()


def _wait_until_up(timeout_s: int = 20) -> bool:
    deadline = time.time() + timeout_s
    while time.time() < deadline:
        try:
            with request.urlopen(BASE + "/health", timeout=2) as resp:
                if resp.status == 200:
                    return True
        except (error.URLError, ConnectionError, OSError):
            time.sleep(0.5)
    return False


def main() -> int:
    if not _wait_until_up():
        print("ERROR: server did not respond to /health within 20s")
        return 1

    print("--- /health ---")
    code, body = _get("/health")
    print(f"HTTP {code}\n{body}\n")

    print("--- /reset ---")
    code, body = _post("/reset", {"seed": 7, "task_id": "medium"})
    print(f"HTTP {code}\n{body}\n")

    print("--- /step ask_question ---")
    code, body = _post(
        "/step",
        {
            "action": {
                "tool_name": "ask_question",
                "arguments": {"question": "what is the order id?"},
            }
        },
    )
    print(f"HTTP {code}\n{body}\n")

    print("--- /state ---")
    code, body = _get("/state")
    print(f"HTTP {code}\n{body}\n")

    print("--- /metadata ---")
    code, body = _get("/metadata")
    print(f"HTTP {code}\n{body}\n")

    return 0


if __name__ == "__main__":
    sys.exit(main())