Enhanced API handler with progress tracking support
| 236 | raise |
| 237 | |
| 238 | class EnhancedRagApiHandler(http.server.BaseHTTPRequestHandler): |
| 239 | """Enhanced API handler with progress tracking support""" |
| 240 | |
| 241 | def do_OPTIONS(self): |
| 242 | """Handle CORS preflight requests for frontend integration.""" |
| 243 | self.send_response(200) |
| 244 | self.send_header('Access-Control-Allow-Origin', '*') |
| 245 | self.send_header('Access-Control-Allow-Methods', 'POST, GET, OPTIONS') |
| 246 | self.send_header('Access-Control-Allow-Headers', 'Content-Type') |
| 247 | self.end_headers() |
| 248 | |
| 249 | def do_GET(self): |
| 250 | """Handle GET requests for progress status and SSE streams""" |
| 251 | parsed_path = urlparse(self.path) |
| 252 | |
| 253 | if parsed_path.path == '/progress': |
| 254 | self.handle_progress_status() |
| 255 | elif parsed_path.path == '/stream': |
| 256 | self.handle_progress_stream() |
| 257 | else: |
| 258 | self.send_json_response({"error": "Not Found"}, status_code=404) |
| 259 | |
| 260 | def do_POST(self): |
| 261 | """Handle POST requests for chat and indexing.""" |
| 262 | parsed_path = urlparse(self.path) |
| 263 | |
| 264 | if parsed_path.path == '/chat': |
| 265 | self.handle_chat() |
| 266 | elif parsed_path.path == '/index': |
| 267 | self.handle_index_with_progress() |
| 268 | else: |
| 269 | self.send_json_response({"error": "Not Found"}, status_code=404) |
| 270 | |
| 271 | def handle_chat(self): |
| 272 | """Handles a chat query by calling the agentic RAG pipeline.""" |
| 273 | try: |
| 274 | content_length = int(self.headers['Content-Length']) |
| 275 | post_data = self.rfile.read(content_length) |
| 276 | data = json.loads(post_data.decode('utf-8')) |
| 277 | |
| 278 | query = data.get('query') |
| 279 | if not query: |
| 280 | self.send_json_response({"error": "Query is required"}, status_code=400) |
| 281 | return |
| 282 | |
| 283 | # Use the single, persistent agent instance to run the query |
| 284 | result = RAG_AGENT.run(query) |
| 285 | |
| 286 | # The result is a dict, so we need to dump it to a JSON string |
| 287 | self.send_json_response(result) |
| 288 | |
| 289 | except json.JSONDecodeError: |
| 290 | self.send_json_response({"error": "Invalid JSON"}, status_code=400) |
| 291 | except Exception as e: |
| 292 | self.send_json_response({"error": f"Server error: {str(e)}"}, status_code=500) |
| 293 | |
| 294 | def handle_index_with_progress(self): |
| 295 | """Triggers the document indexing pipeline with real-time progress tracking.""" |
nothing calls this directly
no outgoing calls
no test coverage detected