@@ -49,14 +49,14 @@ def get_authenticated_user():
4949 Get the authenticated user from EasyAuth headers.
5050
5151 In production (with App Service Auth), the X-Ms-Client-Principal-Id header
52- contains the user's ID. In development mode, returns empty/None values .
52+ contains the user's ID. In development mode, returns "anonymous" .
5353 """
5454 user_principal_id = request .headers .get ("X-Ms-Client-Principal-Id" , "" )
5555 user_name = request .headers .get ("X-Ms-Client-Principal-Name" , "" )
5656 auth_provider = request .headers .get ("X-Ms-Client-Principal-Idp" , "" )
5757
5858 return {
59- "user_principal_id" : user_principal_id or "" ,
59+ "user_principal_id" : user_principal_id or "anonymous " ,
6060 "user_name" : user_name or "" ,
6161 "auth_provider" : auth_provider or "" ,
6262 "is_authenticated" : bool (user_principal_id )
@@ -216,7 +216,33 @@ async def parse_brief():
216216 logger .warning (f"Failed to save brief message to CosmosDB: { e } " )
217217
218218 orchestrator = get_orchestrator ()
219- parsed_brief , clarifying_questions = await orchestrator .parse_brief (brief_text )
219+ parsed_brief , clarifying_questions , rai_blocked = await orchestrator .parse_brief (brief_text )
220+
221+ # Check if request was blocked due to harmful content
222+ if rai_blocked :
223+ # Save the refusal as assistant response
224+ try :
225+ cosmos_service = await get_cosmos_service ()
226+ await cosmos_service .add_message_to_conversation (
227+ conversation_id = conversation_id ,
228+ user_id = user_id ,
229+ message = {
230+ "role" : "assistant" ,
231+ "content" : clarifying_questions , # This is the refusal message
232+ "agent" : "ContentSafety" ,
233+ "timestamp" : datetime .now (timezone .utc ).isoformat ()
234+ }
235+ )
236+ except Exception as e :
237+ logger .warning (f"Failed to save RAI response to CosmosDB: { e } " )
238+
239+ return jsonify ({
240+ "rai_blocked" : True ,
241+ "requires_clarification" : False ,
242+ "requires_confirmation" : False ,
243+ "conversation_id" : conversation_id ,
244+ "message" : clarifying_questions
245+ })
220246
221247 # Check if we need clarifying questions
222248 if clarifying_questions :
@@ -1051,14 +1077,13 @@ async def list_conversations():
10511077 List conversations for a user.
10521078
10531079 Uses authenticated user from EasyAuth headers. In development mode
1054- (when not authenticated), returns conversations where user_id is empty/null .
1080+ (when not authenticated), uses "anonymous" as user_id.
10551081
10561082 Query params:
10571083 limit: Max number of results (default 20)
10581084 """
1059- # Get authenticated user from headers
10601085 auth_user = get_authenticated_user ()
1061- user_id = auth_user ["user_principal_id" ] # Empty string if not authenticated
1086+ user_id = auth_user ["user_principal_id" ]
10621087
10631088 limit = int (request .args .get ("limit" , 20 ))
10641089
0 commit comments