File size: 1,946 Bytes
de962d2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Minimal PS-11 run lifecycle over HTTP: start a run, read the request, confirm, submit report and action log."""
import argparse
import json
from datetime import datetime

import requests

from confirm import confirm


def main():
    ap = argparse.ArgumentParser()
    ap.add_argument("--portal", required=True, help="e.g. http://<PORTAL_URL>")
    ap.add_argument("--team-code", required=True)
    ap.add_argument("--task", default="p01")
    a = ap.parse_args()
    base = a.portal.rstrip("/")
    log = []

    def act(action, **kw):
        log.append({"t": datetime.now().astimezone().isoformat(timespec="seconds"), "step": len(log) + 1, "action": action, **kw})

    print("tasks:", requests.get(f"{base}/api/tasks", headers={"X-Team-Code": a.team_code}).json())
    run = requests.post(f"{base}/api/runs", json={"task_id": a.task}, headers={"X-Team-Code": a.team_code}).json()
    print("run:", json.dumps(run, indent=1))
    run_id, start_url = run["run_id"], run["portal_url"]
    act("start_run", run_id=run_id, url=start_url)

    # Your agent works here: open start_url, log in with the credentials in run["request"], do the task.
    s = requests.Session()
    page = s.get(start_url)
    act("navigate", url=page.url, status=page.status_code)

    # Before every irreversible action (register, drop, pay, submit leave):
    desc = "Register for open elective OE123 Example Course"
    ok = confirm(desc, base_url=base, run_id=run_id)
    act("confirm", description=desc, approved=ok)
    # if ok: submit the form; else: do not do it, and say so in the report.

    report = {"task_id": run["task_id"], "status": "not_done", "summary": "Example client: no task actions were taken."}
    act("report", status=report["status"])
    r = requests.post(f"{base}/api/run/{run_id}/report", json=report | {"actions": log})
    print("report:", r.status_code, json.dumps(r.json(), indent=1)[:2000])


if __name__ == "__main__":
    main()