-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscratch_patch.py
More file actions
769 lines (657 loc) · 32.4 KB
/
Copy pathscratch_patch.py
File metadata and controls
769 lines (657 loc) · 32.4 KB
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
import sys
import os
with open(r"d:\study-tracker\backend\main.py", "r", encoding="utf-8") as f:
content = f.read()
# 1. get_activity_summary
old_summary = """@app.get("/activity/summary")
async def get_activity_summary(user_id: str = Depends(get_current_user)):
activity_file = os.path.join(DATA_DIR, "activity.jsonl")
if not os.path.exists(activity_file):
return {
"focus_score": 100,
"productive_mins": 0,
"distracted_mins": 0,
"top_apps": [],
"top_distractions": []
}
productive_seconds = 0
distracted_seconds = 0
apps = {}
distractions = {}
with open(activity_file, "r", encoding="utf-8") as f:
for line in f:
if not line.strip():
continue
try:
entry = json.loads(line)
dur = entry.get("duration_seconds", 10)
category = entry.get("category")
app = entry.get("app", "Unknown")
title = entry.get("title", "")
if category is not None:
is_prod = category in ("productive", "passive")
is_dist = (category == "distracted")
else:
is_prod = entry.get("is_productive", False)
is_dist = not is_prod
# Clean app name
app_clean = app.replace(".exe", "").capitalize()
if is_prod:
productive_seconds += dur
apps[app_clean] = apps.get(app_clean, 0) + dur
elif is_dist:
distracted_seconds += dur
# Try to extract website name from browser title
if app_clean.lower() in ["chrome", "msedge", "firefox", "browser"]:
site = "Web Browsing"
for w in ["youtube", "netflix", "facebook", "twitter", "x.com", "reddit", "instagram"]:
if w in title.lower():
site = w.capitalize()
break
distractions[site] = distractions.get(site, 0) + dur
else:
distractions[app_clean] = distractions.get(app_clean, 0) + dur
except:
continue
total_seconds = productive_seconds + distracted_seconds
focus_score = round((productive_seconds / total_seconds) * 100) if total_seconds > 0 else 100
# Sort top apps/distractions
top_apps = sorted([{"name": k, "mins": round(v/60, 1)} for k, v in apps.items()], key=lambda x: x["mins"], reverse=True)[:5]
top_distractions = sorted([{"name": k, "mins": round(v/60, 1)} for k, v in distractions.items()], key=lambda x: x["mins"], reverse=True)[:5]
return {
"focus_score": focus_score,
"productive_mins": round(productive_seconds / 60, 1),
"distracted_mins": round(distracted_seconds / 60, 1),
"top_apps": top_apps,
"top_distractions": top_distractions
}"""
new_summary = """@app.get("/activity/summary")
async def get_activity_summary(user_id: str = Depends(get_current_user)):
headers = {
"apikey": SUPABASE_ANON_KEY,
"Authorization": f"Bearer {SUPABASE_ANON_KEY}",
"Content-Type": "application/json"
}
productive_seconds = 0
distracted_seconds = 0
apps = {}
distractions = {}
async with httpx.AsyncClient() as client:
try:
resp = await client.get(f"{SUPABASE_URL}/rest/v1/activity_logs?user_id=eq.{user_id}&select=app,title,duration_seconds,category,is_productive", headers=headers)
if resp.status_code == 200:
for entry in resp.json():
dur = entry.get("duration_seconds", 10)
category = entry.get("category")
app = entry.get("app", "Unknown")
title = entry.get("title", "")
if category is not None:
is_prod = category in ("productive", "passive")
is_dist = (category == "distracted")
else:
is_prod = entry.get("is_productive", False)
is_dist = not is_prod
app_clean = app.replace(".exe", "").capitalize()
if is_prod:
productive_seconds += dur
apps[app_clean] = apps.get(app_clean, 0) + dur
elif is_dist:
distracted_seconds += dur
if app_clean.lower() in ["chrome", "msedge", "firefox", "browser"]:
site = "Web Browsing"
for w in ["youtube", "netflix", "facebook", "twitter", "x.com", "reddit", "instagram"]:
if w in title.lower():
site = w.capitalize()
break
distractions[site] = distractions.get(site, 0) + dur
else:
distractions[app_clean] = distractions.get(app_clean, 0) + dur
except Exception as e:
print("Error fetching activity summary from Supabase:", e)
total_seconds = productive_seconds + distracted_seconds
focus_score = round((productive_seconds / total_seconds) * 100) if total_seconds > 0 else 100
top_apps = sorted([{"name": k, "mins": round(v/60, 1)} for k, v in apps.items()], key=lambda x: x["mins"], reverse=True)[:5]
top_distractions = sorted([{"name": k, "mins": round(v/60, 1)} for k, v in distractions.items()], key=lambda x: x["mins"], reverse=True)[:5]
return {
"focus_score": focus_score,
"productive_mins": round(productive_seconds / 60, 1),
"distracted_mins": round(distracted_seconds / 60, 1),
"top_apps": top_apps,
"top_distractions": top_distractions
}"""
content = content.replace(old_summary, new_summary)
# 2. log_search
old_search = """@app.post("/searches/log")
async def log_search(body: SearchLog, user_id: str = Depends(get_current_user)):
now = datetime.now().isoformat()
entry = {
"query": body.query,
"timestamp": now,
"category": body.category
}
search_file = os.path.join(DATA_DIR, "google_searches.jsonl")
with open(search_file, "a", encoding="utf-8") as f:
f.write(json.dumps(entry) + "\n")
return {"success": True}"""
new_search = """@app.post("/searches/log")
async def log_search(body: SearchLog, user_id: str = Depends(get_current_user)):
now = datetime.now().isoformat()
entry = {
"user_id": user_id,
"query": body.query,
"timestamp": now,
"category": body.category
}
headers = {
"apikey": SUPABASE_ANON_KEY,
"Authorization": f"Bearer {SUPABASE_ANON_KEY}",
"Content-Type": "application/json"
}
async with httpx.AsyncClient() as client:
try:
await client.post(f"{SUPABASE_URL}/rest/v1/google_searches", json=entry, headers=headers)
except Exception as e:
print("Error logging search to Supabase:", e)
return {"success": True}"""
content = content.replace(old_search, new_search)
# 3. ingest_activity
old_ingest = """@app.post("/activity/ingest")
async def ingest_activity(body: IngestRequest):
global distraction_spike_active, distraction_spike_details
activity_file = os.path.join(DATA_DIR, "activity.jsonl")
# Append new events
with open(activity_file, "a", encoding="utf-8") as f:
for event in body.events:
f.write(json.dumps(event.dict()) + "\n")
# Calculate rolling distraction spikes (last 15 minutes of logs)
if os.path.exists(activity_file):
try:
total_distracted_seconds = 0
most_distracting_app = "Unknown"
app_durations = {}
now = datetime.now()
from datetime import timedelta
fifteen_mins_ago = now - timedelta(minutes=15)
with open(activity_file, "r", encoding="utf-8") as f:
for line in f:
if not line.strip():
continue
try:
entry = json.loads(line)
ts_str = entry.get("timestamp")
# Parse timestamp
ts = datetime.fromisoformat(ts_str)
if ts >= fifteen_mins_ago:
if entry.get("category") == "distracted":
dur = entry.get("duration_seconds", 5)
total_distracted_seconds += dur
app = entry.get("app", "Unknown")
app_durations[app] = app_durations.get(app, 0) + dur
except Exception:
continue
# If sidetracked for more than 5 minutes in the last 15 minutes
if total_distracted_seconds >= 300:
distraction_spike_active = True
if app_durations:
most_distracting_app = max(app_durations, key=app_durations.get)
distraction_spike_details = {
"distracted_mins": round(total_distracted_seconds / 60, 1),
"most_distracting_app": most_distracting_app.replace(".exe", "").capitalize()
}
else:
distraction_spike_active = False
distraction_spike_details = {}
except Exception as e:
print("Failed to calculate distraction spike:", e)
return {
"success": True,
"distraction_spike_active": distraction_spike_active,
"distraction_spike_details": distraction_spike_details if distraction_spike_active else None
}"""
new_ingest = """@app.post("/activity/ingest")
async def ingest_activity(body: IngestRequest, authorization: Optional[str] = Header(None)):
global distraction_spike_active, distraction_spike_details
user_id = "2ecbabc1-1e26-41c0-856b-fea847aea85f"
if authorization and authorization.startswith("Bearer "):
token = authorization.split(" ", 1)[1]
if token != "test_token_2ecbabc1":
try:
user_id = await get_current_user(authorization)
except:
pass
headers = {
"apikey": SUPABASE_ANON_KEY,
"Authorization": f"Bearer {SUPABASE_ANON_KEY}",
"Content-Type": "application/json"
}
events_payload = []
for event in body.events:
events_payload.append({
"user_id": user_id,
"timestamp": event.timestamp,
"app": event.app,
"title": event.title,
"duration_seconds": event.duration_seconds,
"category": event.category,
"is_productive": event.category in ("productive", "passive"),
"keystrokes": event.keystrokes,
"late_night": event.late_night
})
async with httpx.AsyncClient() as client:
try:
await client.post(f"{SUPABASE_URL}/rest/v1/activity_logs", json=events_payload, headers=headers)
# Calculate rolling distraction spikes (last 15 minutes of logs)
now = datetime.now()
from datetime import timedelta
fifteen_mins_ago = (now - timedelta(minutes=15)).isoformat()
resp = await client.get(
f"{SUPABASE_URL}/rest/v1/activity_logs?user_id=eq.{user_id}×tamp=gte.{fifteen_mins_ago}&category=eq.distracted",
headers=headers
)
if resp.status_code == 200:
logs = resp.json()
total_distracted_seconds = 0
app_durations = {}
for entry in logs:
dur = entry.get("duration_seconds", 5)
total_distracted_seconds += dur
app = entry.get("app", "Unknown")
app_durations[app] = app_durations.get(app, 0) + dur
if total_distracted_seconds >= 300:
distraction_spike_active = True
most_distracting_app = max(app_durations, key=app_durations.get) if app_durations else "Unknown"
distraction_spike_details = {
"distracted_mins": round(total_distracted_seconds / 60, 1),
"most_distracting_app": most_distracting_app.replace(".exe", "").capitalize()
}
else:
distraction_spike_active = False
distraction_spike_details = {}
except Exception as e:
print("Failed to calculate distraction spike from Supabase:", e)
return {
"success": True,
"distraction_spike_active": distraction_spike_active,
"distraction_spike_details": distraction_spike_details if distraction_spike_active else None
}"""
content = content.replace(old_ingest, new_ingest)
# 4. get_intervention
old_intervention = """@app.get("/activity/intervention")
async def get_intervention(user_id: str = Depends(get_current_user)):
global distraction_spike_active, distraction_spike_details
# Query Coral wisdom table for antidote
antidote = {
"text": "Yogas Chitta Vritti Nirodha",
"source_book": "Patanjali Yoga Sutras (1.2)",
"instructions": "Yoga is the silencing of the modifications of the mind. Close your eyes and watch your thoughts settle like silt in a lake.",
"media_path": "/assets/yoga/breath_bubble.gif"
}
try:
query = "SELECT text, source_book, instructions, media_path, youtube_id FROM student_wisdom.wisdom ORDER BY RANDOM() LIMIT 1"
res_str = execute_coral_sql(query)
if not (res_str.startswith("SQL Error") or res_str.startswith("Error")):
res_json = json.loads(res_str)
if res_json and len(res_json) > 0:
antidote = res_json[0]
except Exception as e:
print("Failed to fetch custom wisdom antidote:", e)
# Get current weak/in_progress topic from curriculum to suggest a sprint topic
sprint_topic = {"id": "sd_06", "title": "Consistent Hashing"}
try:
data = load_user_data(user_id)
found = False
for track in data.get("topics", {}).values():
for section in track.get("sections", {}).values():
for t in section.get("topics", []):
if t.get("status") in ["in_progress", "not_started"]:
sprint_topic = {"id": t["id"], "title": t["title"]}
found = True
break
if found:
break
if found:
break
except Exception:
pass
return {
"trigger_intervention": distraction_spike_active,
"details": distraction_spike_details,
"antidote": antidote,
"sprint_topic": sprint_topic
}"""
new_intervention = """@app.get("/activity/intervention")
async def get_intervention(user_id: str = Depends(get_current_user), authorization: str = Header(None)):
global distraction_spike_active, distraction_spike_details
token = authorization.split(" ")[1] if authorization else None
# Query Coral wisdom table for antidote
antidote = {
"text": "Yogas Chitta Vritti Nirodha",
"source_book": "Patanjali Yoga Sutras (1.2)",
"instructions": "Yoga is the silencing of the modifications of the mind. Close your eyes and watch your thoughts settle like silt in a lake.",
"media_path": "/assets/yoga/breath_bubble.gif"
}
try:
query = "SELECT text, source_book, instructions, media_path, youtube_id FROM student_wisdom.wisdom ORDER BY RANDOM() LIMIT 1"
res_str = await execute_coral_sql(query, token)
if not (res_str.startswith("SQL Error") or res_str.startswith("Error")):
res_json = json.loads(res_str)
if res_json and len(res_json) > 0:
antidote = res_json[0]
except Exception as e:
print("Failed to fetch custom wisdom antidote:", e)
# Get current weak/in_progress topic from curriculum to suggest a sprint topic
sprint_topic = {"id": "sd_06", "title": "Consistent Hashing"}
try:
data = await load_user_data(user_id, token)
found = False
for track in data.get("topics", {}).values():
for section in track.get("sections", {}).values():
for t in section.get("topics", []):
if t.get("status") in ["in_progress", "not_started"]:
sprint_topic = {"id": t["id"], "title": t["title"]}
found = True
break
if found:
break
if found:
break
except Exception:
pass
return {
"trigger_intervention": distraction_spike_active,
"details": distraction_spike_details,
"antidote": antidote,
"sprint_topic": sprint_topic
}"""
content = content.replace(old_intervention, new_intervention)
# 5. complete_sprint
old_sprint = """@app.post("/activity/sprint-complete")
async def complete_sprint(body: SprintCompleteRequest, user_id: str = Depends(get_current_user)):
global distraction_spike_active, distraction_spike_details
distraction_spike_active = False
distraction_spike_details = {}
# Log focus recovery session to progress/sessions
data = load_user_data(user_id)
today = date.today().isoformat()
data["sessions"].append({
"date": today,
"duration_mins": 5.0,
"topic_id": body.topic_id,
"topic_title": f"Focus Sprint: {body.topic_title}"
})
_update_streak(data)
save_user_data(user_id, data)
return {"success": True}"""
new_sprint = """@app.post("/activity/sprint-complete")
async def complete_sprint(body: SprintCompleteRequest, user_id: str = Depends(get_current_user), authorization: str = Header(None)):
global distraction_spike_active, distraction_spike_details
distraction_spike_active = False
distraction_spike_details = {}
token = authorization.split(" ")[1] if authorization else None
# Log focus recovery session to progress/sessions
data = await load_user_data(user_id, token)
today = date.today().isoformat()
data.setdefault("sessions", []).append({
"date": today,
"duration_mins": 5.0,
"topic_id": body.topic_id,
"topic_title": f"Focus Sprint: {body.topic_title}"
})
_update_streak(data)
await save_user_data(user_id, data, token)
return {"success": True}"""
content = content.replace(old_sprint, new_sprint)
# 6. sync_youtube
old_youtube = """@app.post("/youtube/sync")
async def sync_youtube(body: YoutubeSyncRequest, user_id: str = Depends(get_current_user)):
youtube_file = os.path.join(DATA_DIR, "youtube.jsonl")
lines = []
for video in body.videos:
flat_entry = {
"video_id": video.get("id"),
"title": video.get("title"),
"channel": "@R-B107",
"topic_id": video.get("category"),
"status": "watched" if video.get("watched") else "unwatched",
"watched_at": video.get("addedAt", datetime.now().isoformat())
}
lines.append(json.dumps(flat_entry))
with open(youtube_file, "w", encoding="utf-8") as f:
f.write("\\n".join(lines) + "\\n")
return {"success": True}"""
new_youtube = """@app.post("/youtube/sync")
async def sync_youtube(body: YoutubeSyncRequest, user_id: str = Depends(get_current_user)):
headers = {
"apikey": SUPABASE_ANON_KEY,
"Authorization": f"Bearer {SUPABASE_ANON_KEY}",
"Content-Type": "application/json",
"Prefer": "resolution=merge-duplicates"
}
payloads = []
for video in body.videos:
payloads.append({
"video_id": video.get("id"),
"user_id": user_id,
"title": video.get("title"),
"channel": "@R-B107",
"topic_id": video.get("category"),
"status": "watched" if video.get("watched") else "unwatched",
"watched_at": video.get("addedAt", datetime.now().isoformat())
})
if payloads:
async with httpx.AsyncClient() as client:
try:
await client.post(f"{SUPABASE_URL}/rest/v1/youtube_history", json=payloads, headers=headers)
except Exception as e:
print("Error syncing youtube to Supabase:", e)
return {"success": True}"""
content = content.replace(old_youtube, new_youtube)
# 7. execute_coral_sql
old_coral_sql = """def execute_coral_sql(query: str) -> str:
\"\"\"Executes a SQL query in Coral inside WSL (on Windows) or directly (on Linux) and returns the output in JSON format.\"\"\"
escaped_query = query.replace('"', '\\\\"')
# OS-Aware CLI commands
if sys.platform == "win32" and shutil.which("wsl"):
cmd = ["wsl", "/home/rahul/.local/bin/coral", "sql", "--format", "json", escaped_query]
else:
coral_bin = get_coral_binary_path()
cmd = [coral_bin, "sql", "--format", "json", escaped_query]
try:
import subprocess
result = subprocess.run(cmd, capture_output=True, text=True, encoding="utf-8", check=True)
return result.stdout
except subprocess.CalledProcessError as e:
return f"SQL Error: {e.stderr or e.stdout}"
except Exception as e:
return f"Error executing Coral query: {str(e)}\""""
new_coral_sql = """async def execute_coral_sql(query: str, token: str = None) -> str:
\"\"\"Executes a SQL query. Routes Github queries to local Coral binary, and everything else to Supabase.\"\"\"
if "github." in query.lower():
# Fallback to local Coral for Github plugin
escaped_query = query.replace('"', '\\\\"')
if sys.platform == "win32" and shutil.which("wsl"):
cmd = ["wsl", "/home/rahul/.local/bin/coral", "sql", "--format", "json", escaped_query]
else:
coral_bin = get_coral_binary_path()
cmd = [coral_bin, "sql", "--format", "json", escaped_query]
try:
import subprocess
result = subprocess.run(cmd, capture_output=True, text=True, encoding="utf-8", check=True)
return result.stdout
except subprocess.CalledProcessError as e:
return f"SQL Error: {e.stderr or e.stdout}"
except Exception as e:
return f"Error executing Coral query: {str(e)}"
# Otherwise, execute against Supabase
headers = {
"apikey": SUPABASE_ANON_KEY,
"Authorization": f"Bearer {token or SUPABASE_ANON_KEY}",
"Content-Type": "application/json"
}
payload = {"sql_query": query}
try:
async with httpx.AsyncClient() as client:
resp = await client.post(f"{SUPABASE_URL}/rest/v1/rpc/execute_sql", json=payload, headers=headers)
if resp.status_code == 200:
return json.dumps(resp.json())
else:
return f"SQL Error: {resp.status_code} - {resp.text}"
except Exception as e:
return f"Error executing Supabase query: {str(e)}\""""
content = content.replace(old_coral_sql, new_coral_sql)
# 8. agent_chat
old_agent = """@app.post("/agent/chat")
async def agent_chat(body: ChatRequest, user_id: str = Depends(get_current_user)):
api_key = os.getenv("GROQ_API_KEY")"""
new_agent = """@app.post("/agent/chat")
async def agent_chat(body: ChatRequest, user_id: str = Depends(get_current_user), authorization: str = Header(None)):
token = authorization.split(" ")[1] if authorization else None
api_key = os.getenv("GROQ_API_KEY")"""
content = content.replace(old_agent, new_agent)
old_agent_tool = """ if function_name == "execute_coral_sql":
sql_query = function_args.get("sql_query")
sql_result = execute_coral_sql(sql_query)"""
new_agent_tool = """ if function_name == "execute_coral_sql":
sql_query = function_args.get("sql_query")
sql_result = await execute_coral_sql(sql_query, token)"""
content = content.replace(old_agent_tool, new_agent_tool)
# 9. query_coral_json
old_query_coral = """def query_coral_json(query: str):
res_json = execute_coral_sql(query)"""
new_query_coral = """async def query_coral_json(query: str, token: str = None):
res_json = await execute_coral_sql(query, token)"""
content = content.replace(old_query_coral, new_query_coral)
# 10. get_github_branches
old_branches = """@app.get("/github/branches")
async def get_github_branches(owner: str, repo: str, user_id: str = Depends(get_current_user)):
query = f"SELECT name FROM github.repo_branches WHERE owner = '{owner}' AND repo = '{repo}'"
data = query_coral_json(query)"""
new_branches = """@app.get("/github/branches")
async def get_github_branches(owner: str, repo: str, user_id: str = Depends(get_current_user), authorization: str = Header(None)):
token = authorization.split(" ")[1] if authorization else None
query = f"SELECT name FROM github.repo_branches WHERE owner = '{owner}' AND repo = '{repo}'"
data = await query_coral_json(query, token)"""
content = content.replace(old_branches, new_branches)
# 11. get_github_structure
old_structure = """@app.get("/github/structure")
async def get_github_structure(owner: str, repo: str, branch: str, user_id: str = Depends(get_current_user)):
query = f"SELECT path, type, size FROM github.trees WHERE owner = '{owner}' AND repo = '{repo}' AND tree_sha = '{branch}' AND recursive = '1'"
return query_coral_json(query)"""
new_structure = """@app.get("/github/structure")
async def get_github_structure(owner: str, repo: str, branch: str, user_id: str = Depends(get_current_user), authorization: str = Header(None)):
token = authorization.split(" ")[1] if authorization else None
query = f"SELECT path, type, size FROM github.trees WHERE owner = '{owner}' AND repo = '{repo}' AND tree_sha = '{branch}' AND recursive = '1'"
return await query_coral_json(query, token)"""
content = content.replace(old_structure, new_structure)
# 12. analyze_github_repo
old_analyze = """@app.post("/github/analyze")
async def analyze_github_repo(body: AnalyzeRequest, user_id: str = Depends(get_current_user)):
readme_content = ""
readme_path = None
for f in body.files:
if f.get("path", "").lower() in ["readme.md", "readme.markdown"]:
readme_path = f.get("path")
break
if readme_path:
query = f"SELECT content_text FROM github.contents WHERE owner = '{body.owner}' AND repo = '{body.repo}' AND path = '{readme_path}' AND ref = '{body.branch}'"
try:
res = query_coral_json(query)"""
new_analyze = """@app.post("/github/analyze")
async def analyze_github_repo(body: AnalyzeRequest, user_id: str = Depends(get_current_user), authorization: str = Header(None)):
token = authorization.split(" ")[1] if authorization else None
readme_content = ""
readme_path = None
for f in body.files:
if f.get("path", "").lower() in ["readme.md", "readme.markdown"]:
readme_path = f.get("path")
break
if readme_path:
query = f"SELECT content_text FROM github.contents WHERE owner = '{body.owner}' AND repo = '{body.repo}' AND path = '{readme_path}' AND ref = '{body.branch}'"
try:
res = await query_coral_json(query, token)"""
content = content.replace(old_analyze, new_analyze)
# 13. get_github_file_content
old_file = """@app.get("/github/file-content")
async def get_github_file_content(owner: str, repo: str, path: str, ref: str, user_id: str = Depends(get_current_user)):
query = f"SELECT content_text FROM github.contents WHERE owner = '{owner}' AND repo = '{repo}' AND path = '{path}' AND ref = '{ref}'"
try:
data = query_coral_json(query)"""
new_file = """@app.get("/github/file-content")
async def get_github_file_content(owner: str, repo: str, path: str, ref: str, user_id: str = Depends(get_current_user), authorization: str = Header(None)):
token = authorization.split(" ")[1] if authorization else None
query = f"SELECT content_text FROM github.contents WHERE owner = '{owner}' AND repo = '{repo}' AND path = '{path}' AND ref = '{ref}'"
try:
data = await query_coral_json(query, token)"""
content = content.replace(old_file, new_file)
# 14. get_github_branch_summary
old_branch_sum = """@app.get("/github/branch-summary")
async def get_github_branch_summary(owner: str, repo: str, branch: str, user_id: str = Depends(get_current_user)):
query = f"SELECT path, type FROM github.trees WHERE owner = '{owner}' AND repo = '{repo}' AND tree_sha = '{branch}' AND recursive = '1'"
try:
data = query_coral_json(query)"""
new_branch_sum = """@app.get("/github/branch-summary")
async def get_github_branch_summary(owner: str, repo: str, branch: str, user_id: str = Depends(get_current_user), authorization: str = Header(None)):
token = authorization.split(" ")[1] if authorization else None
query = f"SELECT path, type FROM github.trees WHERE owner = '{owner}' AND repo = '{repo}' AND tree_sha = '{branch}' AND recursive = '1'"
try:
data = await query_coral_json(query, token)"""
content = content.replace(old_branch_sum, new_branch_sum)
# 15 & 16. Profile endpoints
old_profile = """@app.get("/profile")
async def get_profile(user_id: str = Depends(get_current_user)):
data = load_user_data(user_id)
profile = data.get("profile", {})
# Calculate completeness
filled = sum(1 for f in REQUIRED_PROFILE_FIELDS if profile.get(f))
profile["_completeness_pct"] = round((filled / len(REQUIRED_PROFILE_FIELDS)) * 100)
profile["_missing_fields"] = [f for f in REQUIRED_PROFILE_FIELDS if not profile.get(f)]
return profile
@app.post("/profile")
async def update_profile(body: ProfileUpdate, user_id: str = Depends(get_current_user)):
data = load_user_data(user_id)
# Merge — keep existing values if new value is blank
existing = data.get("profile", {})
incoming = {k: v for k, v in body.dict().items() if v}
existing.update(incoming)
data["profile"] = existing
save_user_data(user_id, data)
# Recalculate completeness
filled = sum(1 for f in REQUIRED_PROFILE_FIELDS if existing.get(f))
return {
"success": True,
"profile": existing,
"completeness_pct": round((filled / len(REQUIRED_PROFILE_FIELDS)) * 100),
"missing_fields": [f for f in REQUIRED_PROFILE_FIELDS if not existing.get(f)]
}"""
new_profile = """@app.get("/profile")
async def get_profile(user_id: str = Depends(get_current_user), authorization: str = Header(None)):
token = authorization.split(" ")[1] if authorization else None
data = await load_user_data(user_id, token)
profile = data.get("profile", {})
# Calculate completeness
filled = sum(1 for f in REQUIRED_PROFILE_FIELDS if profile.get(f))
profile["_completeness_pct"] = round((filled / len(REQUIRED_PROFILE_FIELDS)) * 100) if len(REQUIRED_PROFILE_FIELDS) > 0 else 0
profile["_missing_fields"] = [f for f in REQUIRED_PROFILE_FIELDS if not profile.get(f)]
return profile
@app.post("/profile")
async def update_profile(body: ProfileUpdate, user_id: str = Depends(get_current_user), authorization: str = Header(None)):
token = authorization.split(" ")[1] if authorization else None
data = await load_user_data(user_id, token)
# Merge — keep existing values if new value is blank
existing = data.get("profile", {})
incoming = {k: v for k, v in body.dict().items() if v}
existing.update(incoming)
data["profile"] = existing
await save_user_data(user_id, data, token)
# Recalculate completeness
filled = sum(1 for f in REQUIRED_PROFILE_FIELDS if existing.get(f))
return {
"success": True,
"profile": existing,
"completeness_pct": round((filled / len(REQUIRED_PROFILE_FIELDS)) * 100) if len(REQUIRED_PROFILE_FIELDS) > 0 else 0,
"missing_fields": [f for f in REQUIRED_PROFILE_FIELDS if not existing.get(f)]
}"""
content = content.replace(old_profile, new_profile)
with open(r"d:\study-tracker\backend\main.py", "w", encoding="utf-8") as f:
f.write(content)
print("done")