omgy commited on
Commit
9e755c1
·
verified ·
1 Parent(s): 929252a

Create utils/supabase_client.py

Browse files
Files changed (1) hide show
  1. utils/supabase_client.py +48 -0
utils/supabase_client.py ADDED
@@ -0,0 +1,48 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from supabase import create_client, Client
3
+
4
+ SUPABASE_URL = os.environ.get("SUPABASE_URL")
5
+ SUPABASE_KEY = os.environ.get("SUPABASE_KEY")
6
+
7
+ if not SUPABASE_URL or not SUPABASE_KEY:
8
+ raise RuntimeError("Set SUPABASE_URL and SUPABASE_KEY in environment (HF secrets).")
9
+
10
+ supabase: Client = create_client(SUPABASE_URL, SUPABASE_KEY)
11
+
12
+ # helper functions (examples)
13
+ def get_pending_tasks(role: str):
14
+ # tasks table columns: id, type, agent_role, assigned_to, bed_id, patient_id, status, options
15
+ resp = supabase.table("tasks").select("*").eq("status", "pending").or_(
16
+ f"agent_role.eq.{role},target_role.eq.HUMAN"
17
+ ).execute()
18
+ return resp.data
19
+
20
+ def lock_task(task_id: int):
21
+ return supabase.table("tasks").update({"status": "locked"}).eq("id", task_id).execute()
22
+
23
+ def unlock_task(task_id: int):
24
+ return supabase.table("tasks").update({"status": "pending"}).eq("id", task_id).execute()
25
+
26
+ def submit_task_decision(task_id: int, decision: dict):
27
+ # store decision in a decisions table OR update task result
28
+ supabase.table("task_decisions").insert([{
29
+ "task_id": task_id,
30
+ "decision": decision
31
+ }]).execute()
32
+ # mark task as completed/assigned as appropriate (backend should validate)
33
+ return supabase.table("tasks").update({"status": "completed"}).eq("id", task_id).execute()
34
+
35
+ def fetch_context_for_task(task):
36
+ # example: get beds, staff, patient info needed for decision
37
+ data = {}
38
+ # get available beds
39
+ beds = supabase.table("beds").select("*").in_("status", ["available"]).execute().data
40
+ data["available_beds"] = beds or []
41
+ # staff list
42
+ staff = supabase.table("staff").select("*").execute().data
43
+ data["staff"] = staff or []
44
+ # patient if any
45
+ if task.get("patient_id"):
46
+ patient = supabase.table("patients").select("*").eq("id", task["patient_id"]).single().execute().data
47
+ data["patient"] = patient
48
+ return data