-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.py
More file actions
1381 lines (1204 loc) · 45.1 KB
/
api.py
File metadata and controls
1381 lines (1204 loc) · 45.1 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
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import logging
import os
from datetime import datetime
from flask import Flask, request, jsonify, abort
from flask_cors import CORS
# Import centralized configuration
from config import Config
from youtube_client import get_video_details
from scoring_modules import score_description, score_title, score_tags, score_category
from data_manager import save_feedback, load_feedback
from simple_scoring import compute_simple_score, compute_simple_score_from_title, compute_simple_score_title_and_clean_desc
from transcript_service import get_transcript, get_transcript_excerpt
from coach_agent import get_coach_agent
from librarian_agent import get_librarian_agent
from navigator_agent import get_navigator_agent
from gatekeeper_agent import get_gatekeeper_agent
from intent_agent import get_intent_agent
import numpy as np
# --- Custom Error Codes and Messages ---
class APIErrorCodes:
# Video-related errors (1000-1099)
VIDEO_NOT_FOUND = 1001
VIDEO_PRIVATE = 1002
VIDEO_DELETED = 1003
INVALID_VIDEO_ID = 1004
INVALID_VIDEO_URL = 1005
# Data availability errors (1100-1199)
MISSING_TITLE = 1101
MISSING_DESCRIPTION = 1102
MISSING_TAGS = 1103
MISSING_CATEGORY = 1104
INSUFFICIENT_DATA = 1105
# API configuration errors (1200-1299)
YOUTUBE_API_KEY_MISSING = 1201
YOUTUBE_API_QUOTA_EXCEEDED = 1202
YOUTUBE_API_DISABLED = 1203
YOUTUBE_API_INVALID_KEY = 1204
# Scoring errors (1300-1399)
SCORING_MODELS_NOT_LOADED = 1301
SCORING_FAILED = 1302
INVALID_PARAMETERS = 1303
# Request validation errors (1400-1499)
INVALID_GOAL = 1401
INVALID_API_KEY = 1402
MISSING_REQUIRED_FIELDS = 1403
# System errors (1500-1599)
INTERNAL_ERROR = 1501
SERVICE_UNAVAILABLE = 1502
class APIError(Exception):
def __init__(self, error_code, message, http_status=400, details=None):
self.error_code = error_code
self.message = message
self.http_status = http_status
self.details = details or {}
def create_error_response(error_code, message, http_status=400, details=None):
"""Create standardized error response"""
return jsonify({
'error': True,
'error_code': error_code,
'message': message,
'details': details or {},
'timestamp': __import__('datetime').datetime.now().isoformat()
}), http_status
def handle_missing_data(details, required_parameters):
"""Check for missing data and return appropriate error codes"""
missing_data = []
available_parameters = []
if 'title' in required_parameters:
if not details.get('title'):
missing_data.append('title')
else:
available_parameters.append('title')
if 'description' in required_parameters:
if not details.get('description'):
missing_data.append('description')
else:
available_parameters.append('description')
if 'tags' in required_parameters:
if not details.get('tags') or len(details.get('tags', [])) == 0:
missing_data.append('tags')
else:
available_parameters.append('tags')
if 'category' in required_parameters:
if not details.get('category'):
missing_data.append('category')
else:
available_parameters.append('category')
return missing_data, available_parameters
# --- Logging setup ---
logging.basicConfig(level=logging.INFO, format='%(asctime)s %(levelname)s %(message)s')
logger = logging.getLogger(__name__)
from flask_limiter import Limiter
from flask_limiter.util import get_remote_address
from google import genai
# ... (imports)
app = Flask(__name__)
CORS(app, resources={r"/*": {"origins": "*"}}, methods=["GET", "POST", "OPTIONS"], allow_headers=["Content-Type", "X-API-KEY"])
@app.after_request
def after_request(response):
response.headers.add('Access-Control-Allow-Origin', '*')
response.headers.add('Access-Control-Allow-Headers', 'Content-Type, X-API-KEY')
return response
@app.after_request
def after_request(response):
"""Ensure CORS headers are present on all responses, including errors."""
response.headers.add('Access-Control-Allow-Origin', '*')
response.headers.add('Access-Control-Allow-Headers', 'Content-Type, X-API-KEY')
response.headers.add('Access-Control-Allow-Methods', 'GET, POST, OPTIONS')
return response
# --- Rate Limiter Setup ---
limiter = Limiter(
get_remote_address,
app=app,
default_limits=[Config.RATELIMIT_DEFAULT],
storage_uri=Config.RATELIMIT_STORAGE_URL
)
# --- Security: API Key check ---
def require_api_key():
key = request.headers.get('X-API-KEY')
if not key or key != Config.API_KEY:
logger.warning('Unauthorized access attempt.')
# Raise a structured API error so clients always receive JSON
raise APIError(APIErrorCodes.INVALID_API_KEY,
'Unauthorized: Invalid or missing API key.',
http_status=401)
@app.before_request
def log_request_info():
logger.info(f"{request.method} {request.path} - {request.remote_addr}")
@app.route('/health', methods=['GET'])
@limiter.exempt # Exempt health check from rate limits
def health():
"""Health check endpoint with system status and dependency verification"""
# Verify Dependencies
dependencies = {
'youtube_api': {'status': 'unknown', 'latency_ms': 0},
'gemini_api': {'status': 'unknown', 'latency_ms': 0}
}
status = 'healthy'
# Check 1: YouTube API (via Client)
start_time = __import__('time').time()
try:
# Simple lightweight call to verify connectivity
if Config.YOUTUBE_API_KEY:
# Minimal call to check key validity (using requests directly or client)
# We rely on Config validation usually, but here we can check connectivity if desired.
# For now, just checking configuration as "healthy" implies configuration is present.
dependencies['youtube_api']['status'] = 'configured'
else:
dependencies['youtube_api']['status'] = 'missing'
status = 'degraded'
except Exception as e:
dependencies['youtube_api']['status'] = f'error: {str(e)}'
status = 'degraded'
dependencies['youtube_api']['latency_ms'] = int((__import__('time').time() - start_time) * 1000)
# Check 2: Gemini API
start_time = __import__('time').time()
try:
if Config.GOOGLE_API_KEY:
client = genai.Client(api_key=Config.GOOGLE_API_KEY)
# List models as a lightweight check
list(client.models.list_models(page_size=1))
dependencies['gemini_api']['status'] = 'connected'
else:
dependencies['gemini_api']['status'] = 'missing'
status = 'degraded'
except Exception as e:
dependencies['gemini_api']['status'] = f'error: {str(e)}'
status = 'unhealthy' # Critical dependency
dependencies['gemini_api']['latency_ms'] = int((__import__('time').time() - start_time) * 1000)
# Check 3: Firestore (via Librarian Agent connection check)
start_time = __import__('time').time()
try:
from librarian_agent import get_librarian_agent
agent = get_librarian_agent()
if agent and agent.db:
# Lightweight check: Access collection reference (doesn't make network call yet usually)
# or try a minimal read. Let's trust initialization.
dependencies['firestore'] = {'status': 'connected', 'latency_ms': 0}
else:
dependencies['firestore'] = {'status': 'disconnected', 'latency_ms': 0}
status = 'degraded' # Librarian features unavailable
except Exception as e:
dependencies['firestore'] = {'status': f'error: {str(e)}', 'latency_ms': 0}
status = 'degraded'
dependencies['firestore']['latency_ms'] = int((__import__('time').time() - start_time) * 1000)
try:
return jsonify({
'status': status,
'service': 'TubeFocus API',
'timestamp': __import__('datetime').datetime.now().isoformat(),
'dependencies': dependencies,
'system_info': {
'environment': Config.ENVIRONMENT,
'python_version': __import__('sys').version
}
}), 200 # Always return 200 to allow clients to see the status details
except Exception as e:
return create_error_response(
APIErrorCodes.SERVICE_UNAVAILABLE,
"Health check failed",
200, # Return 200 even on error to see details
{'error_details': str(e)}
)
@app.route('/score', methods=['POST'])
def score_endpoint():
require_api_key()
try:
data = request.get_json(force=True)
video_url = data.get('video_url')
goal = data.get('goal')
mode = data.get('mode', 'title_and_description') # Default to "title_and_description"
transcript = data.get('transcript', '')
# Infer Intent (Cached/Lightweight)
intent = get_intent_agent().infer_intent(goal)
logger.info(f"Inferred Intent for '{goal}': {intent['intent']}")
# Validate required fields
if not video_url:
return create_error_response(
APIErrorCodes.MISSING_REQUIRED_FIELDS,
"video_url is required",
400,
{'missing_field': 'video_url'}
)
if not goal:
return create_error_response(
APIErrorCodes.MISSING_REQUIRED_FIELDS,
"goal is required",
400,
{'missing_field': 'goal'}
)
# Sanitize inputs
if not isinstance(video_url, str) or not video_url:
return create_error_response(
APIErrorCodes.INVALID_VIDEO_URL,
"Invalid video_url format",
400,
{'video_url': video_url, 'expected_format': 'Valid YouTube URL'}
)
if not isinstance(goal, str) or not 2 < len(goal) < 200:
return create_error_response(
APIErrorCodes.INVALID_GOAL,
"Invalid goal format",
400,
{'goal': goal, 'expected_format': '2-200 characters'}
)
if mode not in ['title_only', 'title_and_description', 'title_and_clean_desc']:
return create_error_response(
APIErrorCodes.INVALID_PARAMETERS,
"Invalid mode value",
400,
{'mode': mode, 'valid_modes': ['title_only', 'title_and_description', 'title_and_clean_desc']}
)
# Check YouTube API key availability
if not Config.YOUTUBE_API_KEY:
return create_error_response(
APIErrorCodes.YOUTUBE_API_KEY_MISSING,
"YouTube API key not configured",
503,
{'solution': 'Set YOUTUBE_API_KEY environment variable'}
)
# Compute score using simplified approach
try:
if mode == "title_only":
# These alias functions need update too if used, but for now specific on main function
score = compute_simple_score_from_title(video_url, goal)
debug_info = {}
elif mode == "title_and_clean_desc":
score = compute_simple_score_title_and_clean_desc(video_url, goal)
debug_info = {}
else:
gatekeeper = get_gatekeeper_agent()
blocked_channels = gatekeeper.get_blocked_channels()
score, reasoning, debug_info = compute_simple_score(video_url, goal, transcript=transcript, intent=intent, blocked_channels=blocked_channels)
logger.info(f"/score/simple {video_url} {mode} -> {score}")
return jsonify({
"score": score,
"mode": mode,
"video_url": video_url,
"goal": goal,
"debug_details": debug_info,
"intent": intent.get('intent', 'General')
}), 200
except ValueError as ve:
# Check if we have attached debug info
debug_details = getattr(ve, 'debug_info', {})
# Handle video not found, private, deleted, etc.
if "Video not found" in str(ve):
return create_error_response(
APIErrorCodes.VIDEO_NOT_FOUND,
"Video not found or inaccessible",
404,
{'video_url': video_url, 'possible_reasons': ['Video is private', 'Video is deleted', 'Invalid URL'], 'debug_details': debug_details}
)
else:
return create_error_response(
APIErrorCodes.INVALID_VIDEO_URL,
"Invalid video URL format",
400,
{'video_url': video_url, 'error': str(ve), 'debug_details': debug_details}
)
except RuntimeError as re:
if "Simple scoring models are not loaded" in str(re):
return create_error_response(
APIErrorCodes.SCORING_MODELS_NOT_LOADED,
"Scoring models not available",
503,
{'solution': 'Check if models are properly loaded'}
)
else:
raise re
except Exception as e:
logger.error(f"/score/simple error: {e}", exc_info=True)
return create_error_response(
APIErrorCodes.INTERNAL_ERROR,
"Internal server error during simple scoring",
500,
{'error_details': str(e)}
)
@app.route('/coach/analyze', methods=['POST'])
def coach_analyze():
"""
Coach Agent endpoint - Session analysis and behavior intervention.
POST /coach/analyze
Body: {
"session_id": "...",
"goal": "...",
"session_data": [
{"video_id": "...", "title": "...", "score": 75, "timestamp": "..."},
...
]
}
"""
require_api_key()
try:
data = request.get_json(force=True)
session_id = data.get('session_id')
goal = data.get('goal')
session_data = data.get('session_data', [])
# Validate required fields
if not session_id:
return create_error_response(
APIErrorCodes.MISSING_REQUIRED_FIELDS,
"session_id is required",
400,
{'missing_field': 'session_id'}
)
if not goal:
return create_error_response(
APIErrorCodes.MISSING_REQUIRED_FIELDS,
"goal is required",
400,
{'missing_field': 'goal'}
)
if not isinstance(session_data, list):
return create_error_response(
APIErrorCodes.INVALID_PARAMETERS,
"session_data must be an array",
400,
{'provided_type': type(session_data).__name__}
)
# Get Coach Agent instance
coach = get_coach_agent()
# Perform autonomous analysis
logger.info(f"Coach analyzing session: {session_id} with {len(session_data)} videos")
analysis = coach.analyze_session(
session_id=session_id,
session_data=session_data,
goal=goal
)
# Return analysis results
return jsonify({
'success': True,
'session_id': session_id,
'analysis': analysis,
'timestamp': __import__('datetime').datetime.now().isoformat()
}), 200
except Exception as e:
logger.error(f"/coach/analyze error: {e}", exc_info=True)
# Fail-open so frontend coaching does not spam hard errors in the extension console.
return jsonify({
'success': False,
'session_id': data.get('session_id') if 'data' in locals() and isinstance(data, dict) else None,
'analysis': {
'intervention_needed': False,
'pattern_detected': 'error',
'message': 'Coach temporarily unavailable.',
'suggested_action': 'continue'
},
'error': str(e),
'timestamp': __import__('datetime').datetime.now().isoformat()
}), 200
@app.route('/librarian/index', methods=['POST'])
def librarian_index():
"""
Librarian Agent endpoint - Index a video for search.
POST /librarian/index
Body: {
"video_id": "...",
"title": "...",
"transcript": "...",
"goal": "...",
"score": 75,
"metadata": {} (optional)
}
"""
require_api_key()
try:
data = request.get_json(force=True)
video_id = data.get('video_id')
title = data.get('title')
transcript = data.get('transcript')
goal = data.get('goal')
score = data.get('score')
metadata = data.get('metadata', {})
# Validate required fields
if not video_id:
return create_error_response(
APIErrorCodes.MISSING_REQUIRED_FIELDS,
"video_id is required",
400,
{'missing_field': 'video_id'}
)
if not title:
return create_error_response(
APIErrorCodes.MISSING_REQUIRED_FIELDS,
"title is required",
400,
{'missing_field': 'title'}
)
if not transcript:
return create_error_response(
APIErrorCodes.MISSING_REQUIRED_FIELDS,
"transcript is required",
400,
{'missing_field': 'transcript'}
)
if not goal:
return create_error_response(
APIErrorCodes.MISSING_REQUIRED_FIELDS,
"goal is required",
400,
{'missing_field': 'goal'}
)
if score is None:
return create_error_response(
APIErrorCodes.MISSING_REQUIRED_FIELDS,
"score is required",
400,
{'missing_field': 'score'}
)
# Get Librarian Agent instance
librarian = get_librarian_agent()
# Index the video (pass segments for hierarchical chunking)
logger.info(f"Librarian indexing video: {video_id}")
segments = data.get('segments') # timestamped transcript segments
success = librarian.index_video(
video_id=video_id,
title=title,
transcript=transcript,
goal=goal,
score=score,
metadata=metadata,
segments=segments
)
if success:
stats = librarian.get_stats()
return jsonify({
'success': True,
'video_id': video_id,
'message': 'Video indexed successfully',
'stats': stats
}), 200
else:
return jsonify({
'success': False,
'error': 'Failed to index video'
}), 500
except Exception as e:
logger.error(f"/librarian/index error: {e}", exc_info=True)
return create_error_response(
APIErrorCodes.INTERNAL_ERROR,
"Librarian indexing failed",
500,
{'error_details': str(e)}
)
@app.route('/librarian/search', methods=['POST'])
def librarian_search():
"""
Librarian Agent endpoint - Semantic search over history.
POST /librarian/search
Body: {
"query": "...",
"n_results": 5 (optional),
"goal_filter": "..." (optional)
}
"""
require_api_key()
try:
data = request.get_json(force=True)
query = data.get('query')
n_results = data.get('n_results', 5)
goal_filter = data.get('goal_filter')
# Validate required fields
if not query:
return create_error_response(
APIErrorCodes.MISSING_REQUIRED_FIELDS,
"query is required",
400,
{'missing_field': 'query'}
)
# Get Librarian Agent instance
librarian = get_librarian_agent()
# Perform search
logger.info(f"Librarian searching for: '{query}'")
results = librarian.search_history(
query=query,
n_results=n_results,
goal_filter=goal_filter
)
return jsonify({
'success': True,
'search_results': results
}), 200
except Exception as e:
logger.error(f"/librarian/search error: {e}", exc_info=True)
return create_error_response(
APIErrorCodes.INTERNAL_ERROR,
"Librarian search failed",
500,
{'error_details': str(e)}
)
@app.route('/librarian/video/<video_id>', methods=['GET', 'DELETE'])
def librarian_get_or_delete_video(video_id):
"""
Get or delete full indexed video by ID.
GET /librarian/video/<video_id>
DELETE /librarian/video/<video_id>
"""
require_api_key()
try:
librarian = get_librarian_agent()
if request.method == 'DELETE':
success = librarian.delete_video(video_id)
if success:
return jsonify({
'success': True,
'message': 'Video deleted successfully'
}), 200
else:
return jsonify({
'success': False,
'error': 'Video not found or deletion failed'
}), 404
# GET request
video = librarian.get_video_by_id(video_id)
if video:
return jsonify({
'success': True,
'video': video
}), 200
else:
return jsonify({
'success': False,
'error': 'Video not found'
}), 404
except Exception as e:
logger.error(f"/librarian/video error: {e}", exc_info=True)
return create_error_response(
APIErrorCodes.INTERNAL_ERROR,
"Failed to process video request",
500,
{'error_details': str(e)}
)
@app.route('/librarian/stats', methods=['GET'])
def librarian_stats():
"""
Get Librarian statistics.
GET /librarian/stats
"""
require_api_key()
try:
librarian = get_librarian_agent()
stats = librarian.get_stats()
return jsonify({
'success': True,
'stats': stats
}), 200
except Exception as e:
logger.error(f"/librarian/stats error: {e}", exc_info=True)
return create_error_response(
APIErrorCodes.INTERNAL_ERROR,
"Failed to retrieve stats",
500,
{'error_details': str(e)}
)
@app.route('/librarian/chat', methods=['POST'])
def librarian_chat():
"""
Librarian Agent endpoint - RAG Chat.
POST /librarian/chat
Body: {
"query": "...",
"focus_video_id": "..." (optional),
"chat_history": [{"role": "user"|"assistant", "content": "..."}] (optional),
"attached_highlight": {"video_id": "...", "video_title": "...", "range_label": "...", "note": "...", "transcript": "..."} (optional)
}
"""
require_api_key()
try:
data = request.get_json(force=True)
query = data.get('query')
focus_video_id = data.get('focus_video_id')
chat_history = data.get('chat_history') or []
attached_highlight = data.get('attached_highlight')
if not query:
return create_error_response(
APIErrorCodes.MISSING_REQUIRED_FIELDS,
"query is required",
400,
{'missing_field': 'query'}
)
librarian = get_librarian_agent()
response = librarian.chat(
query,
focus_video_id=focus_video_id,
chat_history=chat_history,
attached_highlight=attached_highlight
)
return jsonify({
'success': True,
'response': response
}), 200
except Exception as e:
logger.error(f"/librarian/chat error: {e}", exc_info=True)
return create_error_response(
APIErrorCodes.INTERNAL_ERROR,
"Librarian chat failed",
500,
{'error_details': str(e)}
)
@app.route('/navigator/chapters', methods=['POST'])
def navigator_get_chapters():
"""
Navigator Agent endpoint - Get video chapters.
POST /navigator/chapters
Body: { "video_id": "..." }
"""
require_api_key()
try:
data = request.get_json(force=True)
video_id = data.get('video_id')
if not video_id:
return create_error_response(
APIErrorCodes.MISSING_REQUIRED_FIELDS,
"video_id is required",
400,
{'missing_field': 'video_id'}
)
navigator = get_navigator_agent()
result = navigator.get_chapters(video_id)
return jsonify({
'success': True,
'result': result
}), 200
except Exception as e:
logger.error(f"/navigator/chapters error: {e}", exc_info=True)
return create_error_response(
APIErrorCodes.INTERNAL_ERROR,
"Navigator chapters failed",
500,
{'error_details': str(e)}
)
@app.route('/gatekeeper/filter', methods=['POST'])
def gatekeeper_filter():
"""
Gatekeeper Agent endpoint - Filter recommendations.
POST /gatekeeper/filter
Body: {
"goal": "...",
"videos": [ {"id": "...", "title": "..."}, ... ]
}
"""
require_api_key()
try:
data = request.get_json(force=True)
goal = data.get('goal')
videos = data.get('videos', [])
if not goal:
return create_error_response(
APIErrorCodes.MISSING_REQUIRED_FIELDS,
"goal is required",
400,
{'missing_field': 'goal'}
)
if not videos:
return jsonify({'success': True, 'results': []}), 200
# Infer Intent
intent = get_intent_agent().infer_intent(goal)
gatekeeper = get_gatekeeper_agent()
results = gatekeeper.filter_recommendations(videos, goal, intent=intent)
return jsonify({
'success': True,
'results': results
}), 200
except Exception as e:
logger.error(f"/gatekeeper/filter error: {e}", exc_info=True)
# Fail open (return empty results, frontend should probably keep videos)
return create_error_response(
APIErrorCodes.INTERNAL_ERROR,
"Gatekeeper filtering failed",
500,
{'error_details': str(e)}
)
@app.route('/gatekeeper/block_channel', methods=['POST'])
def gatekeeper_block_channel():
"""
Block a specific channel.
POST /gatekeeper/block_channel
Body: { "channel_name": "..." }
"""
require_api_key()
try:
data = request.get_json(force=True)
channel = data.get('channel_name')
if not channel:
return create_error_response(APIErrorCodes.MISSING_REQUIRED_FIELDS, "channel_name required", 400)
get_gatekeeper_agent().block_channel(channel)
return jsonify({'success': True, 'message': f'Blocked {channel}'}), 200
except Exception as e:
return create_error_response(APIErrorCodes.INTERNAL_ERROR, str(e), 500)
@app.route('/gatekeeper/unblock_channel', methods=['POST'])
def gatekeeper_unblock_channel():
"""
Unblock a specific channel.
POST /gatekeeper/unblock_channel
Body: { "channel_name": "..." }
"""
require_api_key()
try:
data = request.get_json(force=True)
channel = data.get('channel_name')
if not channel:
return create_error_response(APIErrorCodes.MISSING_REQUIRED_FIELDS, "channel_name required", 400)
get_gatekeeper_agent().unblock_channel(channel)
return jsonify({'success': True, 'message': f'Unblocked {channel}'}), 200
except Exception as e:
return create_error_response(APIErrorCodes.INTERNAL_ERROR, str(e), 500)
# ===== FIRESTORE ENDPOINTS: Persistent Storage =====
@app.route('/firestore/sessions', methods=['GET'])
def fs_get_sessions():
"""
Get recent sessions from Firestore.
GET /firestore/sessions
Query params:
- limit: max results (default 20)
"""
require_api_key()
try:
from firestore_service import get_recent_sessions
limit = int(request.args.get('limit', 20))
sessions = get_recent_sessions(limit=limit)
return jsonify({
'success': True,
'sessions': sessions,
'count': len(sessions)
}), 200
except Exception as e:
logger.error(f"/firestore/sessions GET error: {e}", exc_info=True)
return create_error_response(
APIErrorCodes.INTERNAL_ERROR,
"Failed to retrieve sessions",
500,
{'error_details': str(e)}
)
@app.route('/firestore/sessions', methods=['POST'])
def fs_save_session_endpoint():
"""
Save a session snapshot to Firestore.
POST /firestore/sessions
Body: {
"session_id": "...",
"goal": "...",
"focus_score": 72,
"videos_watched": 5,
"highlights_count": 3,
"watch_time_minutes": 42,
"date": "2026-02-18"
}
"""
require_api_key()
try:
from firestore_service import save_session
data = request.get_json(force=True) or {}
session_id = data.get('session_id')
if not session_id:
return create_error_response(
APIErrorCodes.MISSING_REQUIRED_FIELDS,
"session_id is required",
400,
{'missing_field': 'session_id'}
)
session_payload = {
'goal': data.get('goal', ''),
'focus_score': float(data.get('focus_score', 0) or 0),
'videos_watched': int(data.get('videos_watched', 0) or 0),
'highlights_count': int(data.get('highlights_count', 0) or 0),
'watch_time_minutes': int(data.get('watch_time_minutes', 0) or 0),
'date': data.get('date') or datetime.now().strftime('%Y-%m-%d'),
'created_at': data.get('created_at') or datetime.now().isoformat()
}
success = save_session(session_id, session_payload)
if not success:
return jsonify({'success': False, 'message': 'Failed to save session'}), 500
return jsonify({'success': True, 'session_id': session_id}), 200
except Exception as e:
logger.error(f"/firestore/sessions POST error: {e}", exc_info=True)
return create_error_response(
APIErrorCodes.INTERNAL_ERROR,
"Failed to save session",
500,
{'error_details': str(e)}
)
@app.route('/highlights', methods=['POST'])
def save_highlight():
"""
Save a highlight to Firestore.
POST /highlights
"""
require_api_key()
try:
from firestore_service import save_highlight as fs_save_highlight
data = request.get_json()
if not data:
return create_error_response(
APIErrorCodes.MISSING_REQUIRED_FIELDS,
"Request body is required",
400
)
# Validate required fields
if not data.get('video_id') or data.get('timestamp') is None:
return create_error_response(
APIErrorCodes.MISSING_REQUIRED_FIELDS,
"video_id and timestamp are required",
400
)
doc_id = fs_save_highlight(data)
if doc_id:
return jsonify({
'success': True,
'highlight_id': doc_id,
'message': 'Highlight saved successfully'
}), 201
else:
return jsonify({
'success': False,
'message': 'Highlight saved locally only (Firestore not available)'
}), 200
except Exception as e:
logger.error(f"/highlights POST error: {e}", exc_info=True)
return create_error_response(
APIErrorCodes.INTERNAL_ERROR,
"Failed to save highlight",
500,
{'error_details': str(e)}
)
@app.route('/highlights', methods=['GET'])
def get_highlights():
"""
Get all highlights.
GET /highlights
Query params:
- user_id: optional user ID filter
- limit: max results (default 100)
"""