T7yrttrd commited on
Commit
82d8420
·
verified ·
1 Parent(s): e3674c2

Delete database.py

Browse files
Files changed (1) hide show
  1. database.py +0 -136
database.py DELETED
@@ -1,136 +0,0 @@
1
- import json
2
- import datetime
3
- import httpx
4
- from config import SUPABASE_URL, SUPABASE_KEY, user_tokens
5
-
6
- async def save_token_to_supabase(user_id: str, service: str, token_data: dict):
7
- google_services = ["google", "gmail", "calendar", "drive"]
8
- target_services = google_services if service in google_services else [service]
9
- now_iso = datetime.datetime.now(datetime.timezone.utc).isoformat()
10
-
11
- for s in target_services:
12
- user_tokens[(user_id, s)] = token_data
13
- try:
14
- url = f"{SUPABASE_URL}/rest/v1/user_connectors"
15
- headers = {
16
- "apikey": SUPABASE_KEY,
17
- "Authorization": f"Bearer {SUPABASE_KEY}",
18
- "Content-Type": "application/json",
19
- "Prefer": "resolution=merge-duplicates"
20
- }
21
- payload = {
22
- "user_id": user_id,
23
- "service": s,
24
- "token_data": token_data,
25
- "updated_at": now_iso
26
- }
27
- async with httpx.AsyncClient(timeout=15.0) as c:
28
- await c.post(url, headers=headers, json=payload)
29
- except Exception:
30
- pass
31
-
32
- async def get_token_from_supabase(user_id: str, service: str):
33
- google_services = ["google", "gmail", "calendar", "drive"]
34
- check_services = [service] + [x for x in google_services if x != service] if service in google_services else [service]
35
-
36
- for s in check_services:
37
- if (user_id, s) in user_tokens:
38
- return user_tokens[(user_id, s)]
39
- try:
40
- url = f"{SUPABASE_URL}/rest/v1/user_connectors?user_id=eq.{user_id}&service=eq.{s}&select=token_data"
41
- headers = {
42
- "apikey": SUPABASE_KEY,
43
- "Authorization": f"Bearer {SUPABASE_KEY}"
44
- }
45
- async with httpx.AsyncClient(timeout=15.0) as c:
46
- res = await c.get(url, headers=headers)
47
- if res.status_code == 200:
48
- data = res.json()
49
- if data and len(data) > 0:
50
- raw_t = data[0].get("token_data")
51
- if isinstance(raw_t, dict):
52
- t_data = raw_t
53
- elif isinstance(raw_t, str):
54
- t_data = json.loads(raw_t)
55
- else:
56
- t_data = {}
57
- if t_data:
58
- user_tokens[(user_id, s)] = t_data
59
- return t_data
60
- except Exception:
61
- pass
62
- return None
63
-
64
- async def delete_token_from_supabase(user_id: str, service: str):
65
- google_services = ["google", "gmail", "calendar", "drive"]
66
- target_services = google_services if service in google_services else [service]
67
-
68
- for s in target_services:
69
- user_tokens.pop((user_id, s), None)
70
- try:
71
- url = f"{SUPABASE_URL}/rest/v1/user_connectors?user_id=eq.{user_id}&service=eq.{s}"
72
- headers = {
73
- "apikey": SUPABASE_KEY,
74
- "Authorization": f"Bearer {SUPABASE_KEY}"
75
- }
76
- async with httpx.AsyncClient(timeout=15.0) as c:
77
- await c.delete(url, headers=headers)
78
- except Exception:
79
- pass
80
-
81
- async def fetch_session_messages(session_id: str):
82
- try:
83
- url = f"{SUPABASE_URL}/rest/v1/chat_messages?session_id=eq.{session_id}&order=created_at.asc"
84
- headers = {
85
- "apikey": SUPABASE_KEY,
86
- "Authorization": f"Bearer {SUPABASE_KEY}"
87
- }
88
- async with httpx.AsyncClient(timeout=20.0) as c:
89
- res = await c.get(url, headers=headers)
90
- if res.status_code == 200:
91
- return res.json()
92
- except Exception:
93
- pass
94
- return []
95
-
96
- async def save_assistant_message_to_db(session_id: str, user_id: str, content: str, thought_reasoning: str = "", sources = None, tool_calls = None):
97
- try:
98
- url = f"{SUPABASE_URL}/rest/v1/chat_messages"
99
- headers = {
100
- "apikey": SUPABASE_KEY,
101
- "Authorization": f"Bearer {SUPABASE_KEY}",
102
- "Content-Type": "application/json",
103
- "Prefer": "return=minimal"
104
- }
105
- payload = {
106
- "session_id": session_id,
107
- "user_id": user_id,
108
- "sender": "assistant",
109
- "content": content,
110
- "thought_reasoning": thought_reasoning
111
- }
112
- if sources is not None:
113
- payload["sources"] = sources
114
- if tool_calls is not None:
115
- payload["tool_calls"] = tool_calls
116
-
117
- async with httpx.AsyncClient(timeout=20.0) as c:
118
- await c.post(url, headers=headers, json=payload)
119
- except Exception:
120
- pass
121
-
122
- async def update_user_message_file_content(session_id: str, file_name: str, file_content: str):
123
- try:
124
- url = f"{SUPABASE_URL}/rest/v1/chat_messages?session_id=eq.{session_id}&sender=eq.user&file_name=eq.{file_name}&order=created_at.desc&limit=1"
125
- headers = {
126
- "apikey": SUPABASE_KEY,
127
- "Authorization": f"Bearer {SUPABASE_KEY}",
128
- "Content-Type": "application/json"
129
- }
130
- payload = {
131
- "file_content": file_content
132
- }
133
- async with httpx.AsyncClient(timeout=20.0) as c:
134
- await c.patch(url, headers=headers, json=payload)
135
- except Exception:
136
- pass