@@ -69,6 +69,31 @@ def create_app():
6969 app .register_blueprint (bp )
7070 app .config ["TEMPLATES_AUTO_RELOAD" ] = True
7171 app .config ['PROVIDE_AUTOMATIC_OPTIONS' ] = True
72+
73+ @app .after_serving
74+ async def shutdown ():
75+ """
76+ Perform any cleanup tasks after the app stops serving requests.
77+ """
78+ print ("Shutting down the application..." , flush = True )
79+ try :
80+ # Clean up agent instances
81+ await BrowseAgentFactory .delete_agent ()
82+ await TemplateAgentFactory .delete_agent ()
83+ await SectionAgentFactory .delete_agent ()
84+
85+ # clear app state
86+ if hasattr (app , 'state' ):
87+ app .browse_agent = None
88+ app .template_agent = None
89+ app .section_agent = None
90+
91+ track_event_if_configured ("ApplicationShutdown" , {"status" : "success" })
92+ except Exception as e :
93+ logging .exception ("Error during application shutdown" )
94+ track_event_if_configured ("ApplicationShutdownError" , {"status" : "error" })
95+ raise e
96+
7297 return app
7398
7499
@@ -251,7 +276,11 @@ async def send_chat_request(request_body, request_headers) -> AsyncGenerator[Dic
251276 # Browse
252277 if request_body ["chat_type" ] == "browse" :
253278 try :
254- browse_agent_data = await BrowseAgentFactory .get_browse_agent (system_instruction = app_settings .azure_openai .system_message )
279+ # Create browse agent if it doesn't exist
280+ if getattr (app , "browse_agent" , None ) is None :
281+ app .browse_agent = await BrowseAgentFactory .get_agent ()
282+
283+ browse_agent_data = app .browse_agent
255284 browse_project_client = browse_agent_data ["client" ]
256285 browse_agent = browse_agent_data ["agent" ]
257286
@@ -283,10 +312,18 @@ async def send_chat_request(request_body, request_headers) -> AsyncGenerator[Dic
283312
284313 if delta_text and delta_text .value :
285314 answer ["answer" ] += delta_text .value
286- yield {
287- "answer" : convert_citation_markers (delta_text .value , doc_mapping ),
288- "citations" : json .dumps (answer ["citations" ])
289- }
315+
316+ # check if citation markers are present
317+ has_citation_markers = bool (re .search (r'【(\d+:\d+)†source】' , delta_text .value ))
318+ if has_citation_markers :
319+ yield {
320+ "answer" : convert_citation_markers (delta_text .value , doc_mapping ),
321+ "citations" : json .dumps (answer ["citations" ])
322+ }
323+ else :
324+ yield {
325+ "answer" : delta_text .value
326+ }
290327
291328 if delta_text and delta_text .annotations :
292329 for annotation in delta_text .annotations :
@@ -305,10 +342,11 @@ async def send_chat_request(request_body, request_headers) -> AsyncGenerator[Dic
305342 if run_id :
306343 await extract_citations_from_run_steps (browse_project_client , thread .id , run_id , answer , streamed_titles )
307344
308- yield {
309- # "answer": answer["answer"],
310- "citations" : json .dumps (answer ["citations" ])
311- }
345+ has_final_citation_markers = bool (re .search (r'【(\d+:\d+)†source】' , answer ["answer" ]))
346+ if has_final_citation_markers :
347+ yield {
348+ "citations" : json .dumps (answer ["citations" ])
349+ }
312350
313351 else :
314352 run = await browse_project_client .agents .runs .create_and_process (
@@ -324,12 +362,19 @@ async def send_chat_request(request_body, request_headers) -> AsyncGenerator[Dic
324362 async for msg in messages :
325363 if msg .role == MessageRole .AGENT and msg .text_messages :
326364 answer ["answer" ] = msg .text_messages [- 1 ].text .value
327- answer ["answer" ] = convert_citation_markers (answer ["answer" ], doc_mapping )
328365 break
329- yield {
330- "answer" : answer ["answer" ],
366+
367+ has_citation_markers = bool (re .search (r'【(\d+:\d+)†source】' , answer ["answer" ]))
368+
369+ if has_citation_markers :
370+ yield {
371+ "answer" : convert_citation_markers (answer ["answer" ], doc_mapping ),
331372 "citations" : json .dumps (answer ["citations" ])
332- }
373+ }
374+ else :
375+ yield {
376+ "answer" : answer ["answer" ]
377+ }
333378 finally :
334379 if thread :
335380 print (f"Deleting browse thread: { thread .id } " , flush = True )
@@ -338,7 +383,19 @@ async def send_chat_request(request_body, request_headers) -> AsyncGenerator[Dic
338383 # Generate Template
339384 else :
340385 try :
341- template_agent_data = await TemplateAgentFactory .get_template_agent (system_instruction = app_settings .azure_openai .template_system_message )
386+ # Create template agent if it doesn't exist
387+ if getattr (app , "template_agent" , None ) is None :
388+ app .template_agent = await TemplateAgentFactory .get_agent ()
389+
390+ # Create section_agent if missing; log errors without stopping flow
391+ try :
392+ if getattr (app , "section_agent" , None ) is None :
393+ app .section_agent = await SectionAgentFactory .get_agent ()
394+ except Exception as e :
395+ logging .exception ("Error initializing Section Agent" , e )
396+ raise e
397+
398+ template_agent_data = app .template_agent
342399 template_project_client = template_agent_data ["client" ]
343400 template_agent = template_agent_data ["agent" ]
344401
@@ -1032,7 +1089,7 @@ async def ensure_cosmos():
10321089 success , err = await cosmos_conversation_client .ensure ()
10331090 if not cosmos_conversation_client or not success :
10341091 if err :
1035- track_event_if_configured ("CosmosEnsureFailed" , err )
1092+ track_event_if_configured ("CosmosEnsureFailed" , { "error" : err } )
10361093 return jsonify ({"error" : err }), 422
10371094 return jsonify ({"error" : "CosmosDB is not configured or not working" }), 500
10381095
@@ -1193,11 +1250,17 @@ async def get_section_content(request_body, request_headers):
11931250 messages .append ({"role" : "user" , "content" : user_prompt })
11941251
11951252 request_body ["messages" ] = messages
1253+ thread = None
1254+ response_text = ""
11961255
11971256 try :
11981257 # Use Foundry SDK for section content generation
11991258 track_event_if_configured ("Foundry_sdk_for_section" , {"status" : "success" })
1200- section_agent_data = await SectionAgentFactory .get_sections_agent (system_instruction = app_settings .azure_openai .generate_section_content_prompt )
1259+ # Create section agent if not already created
1260+ if getattr (app , "section_agent" , None ) is None :
1261+ app .section_agent = await SectionAgentFactory .get_agent ()
1262+
1263+ section_agent_data = app .section_agent
12011264 section_project_client = section_agent_data ["client" ]
12021265 section_agent = section_agent_data ["agent" ]
12031266
0 commit comments