11from fastapi import APIRouter , Depends , HTTPException , Query
22from fastapi .responses import JSONResponse , StreamingResponse
33import asyncio
4+ from bson import ObjectId
5+ import datetime
46
57from main .auth .utils import PermissionChecker
68from main .search .models import UnifiedSearchRequest
1618 tags = ["Search" ]
1719)
1820
21+ def sanitize_dict (d : dict ) -> dict :
22+ """Converts non-serializable types like ObjectId and datetime to strings."""
23+ for key , value in d .items ():
24+ if isinstance (value , ObjectId ):
25+ d [key ] = str (value )
26+ elif isinstance (value , datetime .datetime ):
27+ d [key ] = value .isoformat ()
28+ return d
29+
1930@router .post ("/unified" , summary = "Perform a unified search across all data sources" )
2031async def unified_search_endpoint (
2132 request : UnifiedSearchRequest ,
@@ -73,10 +84,10 @@ async def search_memories_coro():
7384 tasks_coro , chats_coro , search_memories_coro ()
7485 )
7586
76- # Format results
77- formatted_tasks = [{"type" : "task" , ** t } for t in tasks_res ]
78- formatted_chats = [{"type" : "chat" , ** c } for c in chats_res ]
79- formatted_memories = [{"type" : "memory" , ** m } for m in memories_res ]
87+ # Format and sanitize results
88+ formatted_tasks = [{"type" : "task" , ** sanitize_dict ( t ) } for t in tasks_res ]
89+ formatted_chats = [{"type" : "chat" , ** sanitize_dict ( c ) } for c in chats_res ]
90+ formatted_memories = [{"type" : "memory" , ** sanitize_dict ( m ) } for m in memories_res ]
8091
8192 all_results = formatted_tasks + formatted_chats + formatted_memories
8293
@@ -89,4 +100,4 @@ async def search_memories_coro():
89100 # Log the full error for debugging
90101 import traceback
91102 traceback .print_exc ()
92- raise HTTPException (status_code = 500 , detail = f"An error occurred during search: { str (e )} " )
103+ raise HTTPException (status_code = 500 , detail = f"An error occurred during search: { str (e )} " )
0 commit comments