File size: 18,817 Bytes
cb8b80f
 
 
 
 
 
 
 
 
 
 
1c96c02
911cadf
cb8b80f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e73dcb3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cb8b80f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d00d60c
cb8b80f
 
 
 
1c96c02
 
 
 
 
 
cb8b80f
 
1c96c02
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cb8b80f
 
1c96c02
 
cb8b80f
 
1c96c02
cb8b80f
1c96c02
cb8b80f
1c96c02
 
 
 
 
 
 
 
 
 
 
cb8b80f
 
1c96c02
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cb8b80f
 
1c96c02
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cb8b80f
 
e73dcb3
 
 
 
 
 
 
 
cb8b80f
e73dcb3
cb8b80f
 
 
e73dcb3
 
cb8b80f
 
e73dcb3
 
 
 
cb8b80f
 
 
 
e73dcb3
 
cb8b80f
e73dcb3
cb8b80f
 
 
 
e73dcb3
cb8b80f
 
 
 
e73dcb3
cb8b80f
 
450c3f0
 
 
 
cb8b80f
 
 
 
 
 
 
 
 
e73dcb3
450c3f0
e73dcb3
cb8b80f
 
e73dcb3
 
cb8b80f
 
 
450c3f0
 
 
 
 
 
 
 
 
 
 
 
c891949
450c3f0
 
 
 
 
cb8b80f
 
e73dcb3
cb8b80f
e73dcb3
cb8b80f
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
"""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}/"

    @r.get("/api/crew/{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])

    @r.post("/api/crew/{kind}")
    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

    @r.post("/api/agent/step")
    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)

    @r.get("/api/jobs")
    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]}

    @r.post("/api/jobs")
    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])

    @r.post("/api/jobs/{jid}/step")
    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)}

    @r.post("/api/jobs/{jid}/steps/{n}/{decision}")
    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)}

    @r.get("/api/jobs/{jid}/steps/{n}")
    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)}

    @r.post("/api/jobs/{jid}/cursor")
    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)

    @r.post("/api/jobs/{jid}/resume")
    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)

    @r.post("/api/jobs/{jid}/finish")
    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