diff --git a/bu_keys.py b/bu_keys.py new file mode 100644 index 0000000..87bd277 --- /dev/null +++ b/bu_keys.py @@ -0,0 +1,101 @@ +"""Composio browser_use: run a cloud-browser task on our Instant credit and return the result. + +Usage: + python bu_keys.py "your browser task" + python bu_keys.py --status + python bu_keys.py --result + +Instant accounts accept only these models: gpt-5.6-luna, bu-ultrafast. +`max_cost_usd` is mandatory for Instant accounts. +""" +import json +import re +import subprocess +import sys +import time + +BK = "C:/Users/User/tmp/composio/bank/accounts.jsonl" +MCP = "https://connect.composio.dev/mcp" + + +def key(): + for line in open(BK, encoding="utf-8"): + r = json.loads(line) + if r.get("ok") and r.get("api_key") and r.get("credit_usd"): + return r["api_key"] + raise SystemExit("no credited account") + + +KEY = key() + + +def call(tool, args, timeout=300): + body = json.dumps({"jsonrpc": "2.0", "id": 1, "method": "tools/call", + "params": {"name": "COMPOSIO_MULTI_EXECUTE_TOOL", + "arguments": {"tools": [{"tool_slug": tool, "arguments": args}]}}}) + out = subprocess.run(["curl", "-s", "-X", "POST", MCP, + "-H", "content-type: application/json", + "-H", "accept: application/json, text/event-stream", + "-H", "x-consumer-api-key: " + KEY, "-d", body, + "--max-time", str(timeout)], + capture_output=True, text=True, encoding="utf-8", errors="replace").stdout + m = re.search(r"data: (\{.*)", out) + if not m: + return {"raw": out[:300]} + try: + return json.loads(json.loads(m.group(1))["result"]["content"][0]["text"]) + except Exception: + return {"raw": out[:400]} + + +def data_of(resp): + try: + return resp["data"]["results"][0]["response"]["data"] + except Exception: + return {} + + +def start(task, model="gpt-5.6-luna", budget=0.10): + r = call("BROWSER_USE_START_RUN", {"task": task, "model": model, "max_cost_usd": budget}) + d = data_of(r) + return d.get("id"), r + + +def status(run_id): + return data_of(call("BROWSER_USE_GET_RUN_STATUS", {"run_id": run_id})).get("status") + + +def result(run_id): + d = data_of(call("BROWSER_USE_GET_RUN", {"run_id": run_id})) + run = d.get("run", {}) + return {"status": run.get("status"), "result": run.get("result"), + "in_tokens": run.get("total_input_tokens"), "out_tokens": run.get("total_output_tokens"), + "attachments": d.get("attachments")} + + +def main(): + if len(sys.argv) < 2: + print(__doc__) + return + if sys.argv[1] == "--status": + print(status(sys.argv[2])) + return + if sys.argv[1] == "--result": + print(json.dumps(result(sys.argv[2]), ensure_ascii=False, indent=1)[:4000]) + return + task = " ".join(sys.argv[1:]) + run_id, raw = start(task) + if not run_id: + print("start failed:", json.dumps(raw, ensure_ascii=False)[:400]) + return + print("run:", run_id, flush=True) + for _ in range(20): + time.sleep(10) + st = status(run_id) + print(" status:", st, flush=True) + if st in ("completed", "failed", "error", "stopped"): + break + print(json.dumps(result(run_id), ensure_ascii=False, indent=1)[:4000]) + + +main() \ No newline at end of file