Spaces:
Paused
Paused
Download server/agents.py from 2Btainment/studius: direct link, hf CLI and curl.
- Browser
- Download file 18.8 kB
-
https://huggingface.co/spaces/2Btainment/studius/resolve/main/server/agents.py
- Command line
-
hf download hf://spaces/2Btainment/studius/server/agents.py
-
curl -L -o agents.py https://huggingface.co/spaces/2Btainment/studius/resolve/main/server/agents.py
18.8 kB
| """P2.5 agents backend: agents/teams persisted in the user's bucket, real LLM function-calling | |
| (Inference Providers `tools=` with the user's token; fallback: JSON tool-calls via the allow-listed | |
| Llama ZeroGPU Space on the user's quota) and a server-side job list with approval gating for | |
| external/persisting actions. Tokens never leave the server.""" | |
| import json, re, secrets, time, tempfile | |
| from pathlib import Path | |
| from fastapi import APIRouter, Request, HTTPException | |
| from huggingface_hub import HfApi, InferenceClient | |
| PROVIDER_MODELS = ["meta-llama/Llama-3.3-70B-Instruct", "Qwen/Qwen2.5-72B-Instruct"] | |
| LLAMA = "huggingface-projects/llama-3.2-3B-Instruct" | |
| GEMMA = "huggingface-projects/gemma-3-12b-it" | |
| EXTERNAL = {"workspace.save", "avatar.save", "shop.draft", "docs.save", "board.create"} # actions that persist/publish → need approval | |
| JOBS: dict[str, dict] = {} | |
| NOPROV: dict[str, float] = {} # token-hash -> until (provider returned 402: skip for a while) # job id -> job (scoped by user) | |
| ID = re.compile(r"[a-z0-9][a-z0-9-]{0,47}") | |
| def slug(s): return re.sub(r"[^a-z0-9]+", "-", (s or "").lower()).strip("-")[:48] or "x" + secrets.token_hex(3) | |
| def to_schema(tools): # [{name, desc, params:{k:type}}] -> OpenAI tools | |
| return [{"type": "function", "function": {"name": t["name"].replace(".", "__"), "description": t.get("desc", "")[:300], | |
| "parameters": {"type": "object", "properties": {k: {"type": "number" if v == "number" else "string"} for k, v in (t.get("params") or {}).items()}, | |
| "required": list((t.get("params") or {}).keys())}}} for t in tools] | |
| def parse_json(txt): | |
| m = re.search(r"\{[\s\S]*\}", txt or "") | |
| if not m: return None | |
| s = m.group(0) | |
| for end in range(len(s), 0, -1): # tolerate trailing junk | |
| if s[end - 1] != "}": continue | |
| try: return json.loads(s[:end]) | |
| except Exception: pass | |
| return None | |
| import threading | |
| JD = ".studius/jobs/" | |
| LOCKS: dict[str, threading.Lock] = {} | |
| def persist(j, token): | |
| """Write the job's latest state to the bucket in a background thread (serialized per job, newest wins).""" | |
| if not j.get("bucket"): return | |
| j["_rev"] = j.get("_rev", 0) + 1 | |
| lock = LOCKS.setdefault(j["id"], threading.Lock()) | |
| def w(): | |
| with lock: | |
| rev = j["_rev"] | |
| if j.get("persisted", 0) >= rev: return | |
| data = json.dumps({k: v for k, v in j.items() if k not in ("user", "_rev", "persisted", "persist_error")} | {"owner": j["user"]}, indent=1, ensure_ascii=False).encode() | |
| try: HfApi(token=token).batch_bucket_files(j["bucket"], add=[(data, f"{JD}{j['id']}.json")]); j["persisted"] = rev; j.pop("persist_error", None) | |
| except Exception as e: j["persist_error"] = str(e)[:120] | |
| threading.Thread(target=w, daemon=True).start() | |
| def load_jobs(api, bucket, user): | |
| out = [] | |
| try: | |
| files = sorted([f for f in api.list_bucket_tree(bucket, prefix=JD.rstrip("/"), recursive=True) if getattr(f, "type", "") == "file" and f.path.endswith(".json")], key=lambda f: str(f.mtime), reverse=True)[:40] | |
| with tempfile.TemporaryDirectory() as t: | |
| api.download_bucket_files(bucket, [(f.path, f"{t}/{i}.json") for i, f in enumerate(files)]) | |
| for i, _ in enumerate(files): | |
| try: j = json.loads(Path(f"{t}/{i}.json").read_text()) | |
| except Exception: continue | |
| if j.get("owner") == user: out.append(j) | |
| except Exception: pass | |
| return out | |
| def make_agents_router(session, need, bid, client_for): | |
| r = APIRouter() | |
| def uid(s): return s["user"]["name"] | |
| # ---------- agents & teams in the bucket ---------- | |
| def kind_dir(kind): | |
| if kind not in ("agents", "teams"): raise HTTPException(400, "kind") | |
| return f".studius/{kind}/" | |
| def crew_list(kind: str, req: Request, bucket: str): | |
| s = need(req); api = HfApi(token=s["token"]); bucket = bid(bucket, s); d = kind_dir(kind) | |
| try: | |
| files = [f.path for f in api.list_bucket_tree(bucket, prefix=d.rstrip("/"), recursive=True) if getattr(f, "type", "") == "file" and f.path.endswith(".json")] | |
| out = [] | |
| with tempfile.TemporaryDirectory() as t: | |
| api.download_bucket_files(bucket, [(p, f"{t}/{i}.json") for i, p in enumerate(files)]) | |
| for i, _ in enumerate(files): | |
| try: out.append(json.loads(Path(f"{t}/{i}.json").read_text())) | |
| except Exception: pass | |
| return {kind: out} | |
| except HTTPException: raise | |
| except Exception as e: raise HTTPException(502, str(e)[:200]) | |
| async def crew_save(kind: str, req: Request): | |
| s = need(req); body = await req.json(); api = HfApi(token=s["token"]); bucket = bid(body.get("bucket"), s); d = kind_dir(kind) | |
| obj = body.get("item") or {} | |
| obj["id"] = obj.get("id") if ID.fullmatch(str(obj.get("id", ""))) else slug(obj.get("name")) | |
| obj["name"] = str(obj.get("name", obj["id"]))[:60]; obj["updated"] = int(time.time()) | |
| if kind == "agents": | |
| obj["modules"] = [m for m in obj.get("modules", []) if re.fullmatch(r"[a-z]{2,20}", str(m))][:12] | |
| obj["role"] = str(obj.get("role", ""))[:400]; obj["avatar"] = str(obj.get("avatar", ""))[:60] | |
| else: | |
| obj["members"] = [m for m in obj.get("members", []) if ID.fullmatch(str(m))][:8]; obj["task"] = str(obj.get("task", ""))[:300] | |
| try: api.batch_bucket_files(bucket, add=[(json.dumps(obj, indent=1, ensure_ascii=False).encode(), f"{d}{obj['id']}.json")]) | |
| except Exception as e: raise HTTPException(502, str(e)[:200]) | |
| return {"saved": obj} | |
| # ---------- LLM function-calling: best available model, validated plans, retries ---------- | |
| def gr_llm(space, token, prompt, system): | |
| from gradio_client import Client # fresh session per call: chat Spaces keep history per session | |
| c = Client(space, hf_token=token, verbose=False) | |
| if space == GEMMA: out = c.predict({"text": prompt, "files": []}, system, 500, api_name="/run") | |
| else: out = c.predict((system + "\n\n" if system else "") + prompt, 500, 0.2, 0.9, 50, 1.1, api_name="/generate") | |
| return out if isinstance(out, str) else json.dumps(out) | |
| def validate(call, tools, hist, known): | |
| """Return (call, problem). Coerces types, repairs unknown asset urls to the latest known one.""" | |
| t = next((x for x in tools if x["name"] == call.get("tool")), None) | |
| if not t: return None, f"Werkzeug '{call.get('tool')}' existiert nicht. Erlaubt: {', '.join(x['name'] for x in tools)}" | |
| args = call.get("args") if isinstance(call.get("args"), dict) else {} | |
| params = t.get("params") or {}; fixed = [] | |
| args = {k: v for k, v in args.items() if k in params} | |
| for k, typ in params.items(): | |
| v = args.get(k) | |
| if typ == "number": | |
| try: args[k] = float(v) if v not in (None, "") else None | |
| except Exception: args[k] = None | |
| if args[k] is None: args.pop(k) | |
| elif k in ("url", "source"): | |
| if v not in known and known: args[k] = known[-1]; fixed.append(k) | |
| elif not known and not v: return None, f"'{k}' braucht die url eines vorherigen Ergebnisses – zuerst ein Werkzeug aufrufen, das eines erzeugt" | |
| elif k == "prefix": args.setdefault(k, "") | |
| elif not str(v or "").strip(): return None, f"Parameter '{k}' fehlt für {t['name']}" | |
| if any(h.get("tool") == t["name"] and h.get("args") == args for h in hist): return None, f"{t['name']} wurde mit diesen Argumenten schon ausgeführt – nächster Schritt oder final" | |
| return {"tool": t["name"], "args": args, **({"repaired": fixed} if fixed else {})}, None | |
| async def step(req: Request): | |
| """One planning step. Body: {task, tools:[{name,desc,params}], history:[{tool,args,result}], system, known_urls, require_tool}. | |
| Returns {call:{tool,args}} | {final}, plus via + attempts (model/validation trail).""" | |
| s = session(req) | |
| if not s: raise HTTPException(401, "not signed in") | |
| b = await req.json(); tools = b.get("tools") or [] | |
| hist = b.get("history") or []; system = str(b.get("system") or "")[:2000]; task = str(b.get("task") or "")[:2000] | |
| known = [u for u in (b.get("known_urls") or []) if isinstance(u, str)][-10:]; need_tool = bool(b.get("require_tool")) | |
| if not tools: raise HTTPException(400, "no tools") | |
| sysmsg = (system + "\n\n" if system else "") + "Du bist ein STUDIUS-Mitarbeiter. Erledige den Auftrag mit den Werkzeugen, Schritt für Schritt. Nutze Ergebnisse vorheriger Schritte (deren url) als Argumente. Wenn alles erledigt ist, antworte kurz ohne Werkzeug." | |
| attempts = [] | |
| def accept(call, via): | |
| c, prob = validate(call, tools, hist, known) | |
| attempts.append({"via": via, "tool": call.get("tool"), "ok": not prob, **({"problem": prob} if prob else {})}) | |
| return c, prob | |
| def final_ok(via): | |
| ok = not need_tool | |
| attempts.append({"via": via, "final": True, "ok": ok, **({} if ok else {"problem": "Werkzeug nötig"})}); return ok | |
| # 1) Inference Providers with native tools (needs credits/PRO on the user's account) | |
| th = str(hash(s["token"])) | |
| msgs = [{"role": "system", "content": sysmsg}, {"role": "user", "content": task}] | |
| for i, h in enumerate(hist): | |
| msgs.append({"role": "assistant", "content": "", "tool_calls": [{"id": f"c{i}", "type": "function", "function": {"name": h["tool"].replace(".", "__"), "arguments": json.dumps(h.get("args", {}))}}]}) | |
| msgs.append({"role": "tool", "tool_call_id": f"c{i}", "content": json.dumps(h.get("result", {}))[:1500]}) | |
| for m in ([] if NOPROV.get(th, 0) > time.time() else PROVIDER_MODELS): | |
| for k in range(2): | |
| try: | |
| out = InferenceClient(token=s["token"], timeout=60).chat_completion(model=m, messages=msgs, tools=to_schema(tools), tool_choice="required" if need_tool else "auto", max_tokens=400) | |
| msg = out.choices[0].message | |
| if msg.tool_calls: | |
| tc = msg.tool_calls[0].function; a = tc.arguments; a = json.loads(a) if isinstance(a, str) else (a or {}) | |
| c, prob = accept({"tool": tc.name.replace("__", "."), "args": a}, f"tools:{m}") | |
| if c: return {"call": c, "via": f"tools:{m}", "attempts": attempts} | |
| msgs.append({"role": "user", "content": "Ungültiger Aufruf: " + prob}) | |
| elif final_ok(f"tools:{m}"): return {"final": msg.content or "", "via": f"tools:{m}", "attempts": attempts} | |
| else: msgs.append({"role": "user", "content": "Rufe jetzt genau ein Werkzeug auf."}) | |
| except Exception as e: | |
| attempts.append({"via": f"tools:{m}", "ok": False, "problem": str(e)[:90]}) | |
| if "402" in str(e): NOPROV[th] = time.time() + 600 | |
| break | |
| if NOPROV.get(th, 0) > time.time(): break | |
| # 2) JSON function-calling on ZeroGPU LLM Spaces (user's quota): strongest first | |
| tl = "\n".join(f'- {t["name"]}({", ".join(f"{k}: {v}" for k, v in (t.get("params") or {}).items())}): {t.get("desc", "")}' for t in tools) | |
| done = "\n".join(f'{i+1}. {h["tool"]}({json.dumps(h.get("args", {}), ensure_ascii=False)}) -> {json.dumps(h.get("result", {}), ensure_ascii=False)[:300]}' for i, h in enumerate(hist)) or "(noch nichts)" | |
| base = (f"Werkzeuge:\n{tl}\n\nAuftrag: {task}\n\nBisher ausgeführt:\n{done}\n\nVerfügbare Ergebnis-URLs: {', '.join(known) or '(keine)'}\n\n" | |
| 'Antworte NUR mit einem JSON-Objekt in einer Zeile. Nächster Werkzeugaufruf: {"tool":"<name>","args":{...}} ' | |
| + ('(ein Werkzeugaufruf ist jetzt Pflicht).' if need_tool else 'oder, wenn der Auftrag vollständig erledigt ist: {"final":"<kurze Zusammenfassung>"}.')) | |
| for space, label in ((GEMMA, "json:gemma-3-12b"), (LLAMA, "json:llama-3.2-3B")): | |
| prompt = base | |
| for k in range(2): | |
| try: txt = gr_llm(space, s["token"], prompt, sysmsg) | |
| except Exception as e: | |
| attempts.append({"via": label, "ok": False, "problem": str(e)[:110]}); break | |
| j = parse_json(txt) or {} | |
| if j.get("tool"): | |
| c, prob = accept(j, label) | |
| if c: return {"call": c, "via": label, "attempts": attempts} | |
| elif "final" in j and final_ok(label): return {"final": str(j["final"]), "via": label, "attempts": attempts} | |
| else: | |
| prob = "Werkzeug nötig" if "final" in j else "keine gültige JSON-Antwort" | |
| if "final" not in j: attempts.append({"via": label, "ok": False, "problem": prob, "raw": txt[:120]}) | |
| prompt = base + f"\n\nDein letzter Vorschlag war ungültig: {prob}. Korrigiere ihn." | |
| raise HTTPException(502, "Kein Planer-Modell lieferte einen gültigen Plan: " + "; ".join(f"{a['via']}: {a.get('problem','')}" for a in attempts)[-400:]) | |
| # ---------- server-side job list with approval ---------- | |
| def restore(s, bucket): | |
| for j in load_jobs(HfApi(token=s["token"]), bucket, uid(s)): | |
| if j["id"] not in JOBS: | |
| j["user"] = j.pop("owner", uid(s)); j["restored"] = True | |
| if j.get("state") in ("running", "awaiting_approval"): j["state"] = "interrupted" # server restarted mid-run; runner (browser) is gone | |
| JOBS[j["id"]] = j | |
| def job_of(s, jid, bucket=None): | |
| j = JOBS.get(jid) | |
| if not j and bucket: restore(s, bid(bucket, s)); j = JOBS.get(jid) | |
| if not j or j["user"] != uid(s): raise HTTPException(404, "job") | |
| return j | |
| def pub(j): return {k: v for k, v in j.items() if k not in ("user", "_rev")} | {"saved": j.get("persisted", 0) >= j.get("_rev", 0)} | |
| def save(s, j): persist(j, s["token"]); return pub(j) | |
| def jobs(req: Request, bucket: str = ""): | |
| s = need(req) | |
| if bucket: restore(s, bid(bucket, s)) | |
| return {"jobs": [pub(j) for j in sorted(JOBS.values(), key=lambda x: -x["created"]) if j["user"] == uid(s) and (not bucket or j.get("bucket") == bucket)][:50]} | |
| async def job_new(req: Request): | |
| s = need(req); b = await req.json(); jid = "j" + secrets.token_hex(5) | |
| bk = bid(b["bucket"], s) if b.get("bucket") else "" | |
| JOBS[jid] = {"id": jid, "user": uid(s), "bucket": bk, "task": str(b.get("task", ""))[:500], "agent": str(b.get("agent", ""))[:60], "team": str(b.get("team", ""))[:60], | |
| "state": "running", "steps": [], "created": int(time.time())} | |
| return save(s, JOBS[jid]) | |
| async def job_step(jid: str, req: Request): | |
| """Register a tool call. External actions are held as 'awaiting_approval' until approved.""" | |
| b = await req.json(); s = need(req); j = job_of(s, jid, b.get("bucket")); tool = str(b.get("tool", ""))[:60] | |
| st = {"n": len(j["steps"]) + 1, "tool": tool, "agent": str(b.get("agent", ""))[:60], "args": b.get("args") or {}, "external": tool in EXTERNAL, | |
| "state": "awaiting_approval" if tool in EXTERNAL else "approved", "t": int(time.time())} | |
| j["steps"].append(st) | |
| if st["external"]: j["state"] = "awaiting_approval" | |
| return {"step": st, "job": save(s, j)} | |
| async def job_decide(jid: str, n: int, decision: str, req: Request, bucket: str = ""): | |
| try: body = await req.json() | |
| except Exception: body = {} | |
| s = need(req); j = job_of(s, jid, bucket or body.get("bucket")) | |
| if decision not in ("approve", "reject", "done", "error"): raise HTTPException(400, "decision") | |
| if not 0 < n <= len(j["steps"]): raise HTTPException(404, "step") | |
| st = j["steps"][n - 1] | |
| if decision in ("approve", "reject"): | |
| if st["state"] != "awaiting_approval": raise HTTPException(409, "not awaiting approval") | |
| st["state"] = "approved" if decision == "approve" else "rejected"; st["decided"] = int(time.time()) | |
| j["state"] = "running" if decision == "approve" else "rejected" | |
| else: | |
| if st["state"] != "approved": raise HTTPException(409, "step not approved") | |
| st["state"] = decision; st["finished"] = int(time.time()) | |
| if isinstance(body.get("result"), dict): st["result"] = {k: (v[:600] if isinstance(v, str) else v) for k, v in body["result"].items() if k in ("kind", "url", "path", "text", "name", "error")} | |
| return {"step": st, "job": save(s, j)} | |
| def job_poll(jid: str, n: int, req: Request, bucket: str = ""): | |
| s = need(req); j = job_of(s, jid, bucket) | |
| if not 0 < n <= len(j["steps"]): raise HTTPException(404, "step") | |
| return {"step": j["steps"][n - 1], "job": pub(j)} | |
| async def job_cursor(jid: str, req: Request): | |
| """Team progress pointer: which member is working (for resume).""" | |
| b = await req.json(); s = need(req); j = job_of(s, jid, b.get("bucket")) | |
| j["cursor"] = {"member": int(b.get("member", 0)), "agent": str(b.get("agent", ""))[:60]} | |
| if isinstance(b.get("members"), list): j["members"] = [str(m)[:60] for m in b["members"]][:8] | |
| return save(s, j) | |
| async def job_resume(jid: str, req: Request): | |
| """Resume an interrupted/errored job: completed steps are kept, unfinished ones are superseded.""" | |
| b = await req.json(); s = need(req); j = job_of(s, jid, b.get("bucket")) | |
| if j["state"] not in ("interrupted", "error", "running", "awaiting_approval"): raise HTTPException(409, f"job is {j['state']}") | |
| for st in j["steps"]: | |
| if st["state"] in ("awaiting_approval", "approved"): st["state"] = "superseded" | |
| j["state"] = "running"; j["resumed"] = j.get("resumed", 0) + 1; j.pop("finished", None) | |
| return save(s, j) | |
| async def job_finish(jid: str, req: Request): | |
| b = await req.json(); s = need(req); j = job_of(s, jid, b.get("bucket")) | |
| j["state"] = b.get("state") if b.get("state") in ("done", "error", "rejected") else "done"; j["summary"] = str(b.get("summary", ""))[:1000]; j["finished"] = int(time.time()) | |
| return save(s, j) | |
| return r | |