CIT_Hackathon_PS11_webagent / example_client.py
Tarakeshwaran's picture
Add ps11
de962d2 verified
Raw History Blame Contribute Delete
1.95 kB
"""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()