@@ -1419,13 +1419,18 @@ async def get_thread(
14191419 class_id : str , thread_id : str , request : Request , openai_client : OpenAIClient
14201420):
14211421 thread = await models .Thread .get_by_id (request .state .db , int (thread_id ))
1422- messages , assistant , runs_result = await asyncio .gather (
1422+ messages , [ assistant , file_names ] , runs_result = await asyncio .gather (
14231423 openai_client .beta .threads .messages .list (
14241424 thread .thread_id , limit = 20 , order = "desc"
14251425 ),
1426- models .Assistant . get_by_id (request .state .db , thread .assistant_id ),
1426+ models .Thread . get_file_search_files_assistant (request .state .db , thread .id ),
14271427 openai_client .beta .threads .runs .list (thread .thread_id , limit = 1 , order = "desc" ),
14281428 )
1429+ if not assistant :
1430+ raise HTTPException (
1431+ status_code = 404 ,
1432+ detail = "Assistant not found" ,
1433+ )
14291434 last_run = [r async for r in runs_result ]
14301435 current_user_ids = [
14311436 request .state .session .user .id
@@ -1436,6 +1441,13 @@ async def get_thread(
14361441 users = {str (u .id ): u for u in thread .users }
14371442
14381443 for message in messages .data :
1444+ for content in message .content :
1445+ if content .type and content .type == "text" and content .text .annotations :
1446+ for annotation in content .text .annotations :
1447+ if annotation .type == "file_citation" :
1448+ annotation .file_citation .file_name = file_names .get (
1449+ annotation .file_citation .file_id , ""
1450+ )
14391451 user_id = message .metadata .pop ("user_id" , None )
14401452 if not user_id :
14411453 continue
@@ -1527,11 +1539,19 @@ async def list_thread_messages(
15271539 messages = await openai_client .beta .threads .messages .list (
15281540 thread .thread_id , limit = limit , order = "asc" , before = before
15291541 )
1542+ file_names = await models .Thread .get_file_search_files (request .state .db , thread .id )
15301543
15311544 if messages .data :
15321545 users = {u .id : u .created for u in thread .users }
15331546
15341547 for message in messages .data :
1548+ for content in message .content :
1549+ if content .type == "text" and content .text .annotations :
1550+ for annotation in content .text .annotations :
1551+ if annotation .type == "file_citation" :
1552+ annotation .file_citation .file_name = file_names .get (
1553+ annotation .file_citation .file_id , ""
1554+ )
15351555 user_id = message .metadata .pop ("user_id" , None )
15361556 if not user_id :
15371557 continue
@@ -1831,12 +1851,13 @@ async def create_run(
18311851):
18321852 thread = await models .Thread .get_by_id (request .state .db , int (thread_id ))
18331853 asst = await models .Assistant .get_by_id (request .state .db , thread .assistant_id )
1834-
1854+ file_names = await models . Thread . get_file_search_files ( request . state . db , thread . id )
18351855 stream = run_thread (
18361856 openai_client ,
18371857 thread_id = thread .thread_id ,
18381858 assistant_id = asst .assistant_id ,
18391859 message = [],
1860+ file_names = file_names ,
18401861 )
18411862
18421863 return StreamingResponse (stream , media_type = "text/event-stream" )
@@ -1926,13 +1947,15 @@ async def send_message(
19261947 thread = thread .thread_id ,
19271948 )
19281949
1950+ file_names = await models .Thread .get_file_search_files (request .state .db , thread .id )
19291951 # Create a generator that will stream chunks to the client.
19301952 stream = run_thread (
19311953 openai_client ,
19321954 thread_id = thread .thread_id ,
19331955 assistant_id = asst .assistant_id ,
19341956 message = messageContent ,
19351957 metadata = {"user_id" : str (request .state .session .user .id )},
1958+ file_names = file_names ,
19361959 )
19371960 return StreamingResponse (stream , media_type = "text/event-stream" )
19381961
0 commit comments