@@ -52,7 +52,7 @@ def get_agent(request: Request) -> Agent:
5252def serialize_sse_event (data : Dict ) -> str :
5353 return f"data: { json .dumps (data )} \n \n "
5454
55- async def get_completed_message (ai_client : AIProjectClient , message : ThreadMessage ) -> Dict :
55+ async def get_message_and_annotations (ai_client : AIProjectClient , message : ThreadMessage ) -> Dict :
5656 annotations = []
5757 # Get file annotations for the file search.
5858 for annotation in (a .as_dict () for a in message .file_citation_annotations ):
@@ -72,8 +72,7 @@ async def get_completed_message(ai_client : AIProjectClient, message: ThreadMess
7272
7373 return {
7474 'content' : message .text_messages [0 ].text .value ,
75- 'annotations' : annotations ,
76- 'type' : "completed_message"
75+ 'annotations' : annotations
7776 }
7877class MyEventHandler (AsyncAgentEventHandler [str ]):
7978 def __init__ (self , ai_client : AIProjectClient ):
@@ -92,7 +91,8 @@ async def on_thread_message(self, message: ThreadMessage) -> Optional[str]:
9291
9392 logger .info ("MyEventHandler: Received completed message" )
9493
95- stream_data = await get_completed_message (self .ai_client , message )
94+ stream_data = await get_message_and_annotations (self .ai_client , message )
95+ stream_data ['type' ] = "completed_message"
9696 return serialize_sse_event (stream_data )
9797 except Exception as e :
9898 logger .error (f"Error in event handler for thread message: { e } " , exc_info = True )
@@ -185,10 +185,11 @@ async def history(
185185 # Create a new message from the user's input.
186186 try :
187187 content = []
188- for message in await ai_client .agents .list_messages (
188+ response = await ai_client .agents .list_messages (
189189 thread_id = thread_id ,
190- ):
191- content .append (await get_completed_message (ai_client , message ))
190+ )
191+ for message in response .data :
192+ content .append (await get_message_and_annotations (ai_client , message ))
192193
193194 logger .info (f"List message, thread ID: { thread_id } " )
194195 response = JSONResponse (content = content )
0 commit comments