@@ -8,6 +8,7 @@ namespace HelloWorldA365.AgentLogic.ResponsesApi;
88using Microsoft . Agents . Builder . State ;
99using Microsoft . Agents . Core . Models ;
1010using System . Net . Http . Headers ;
11+ using System . Security . Cryptography ;
1112using System . Text ;
1213using System . Text . Json ;
1314using System . Text . Json . Serialization ;
@@ -61,7 +62,8 @@ public async Task NewActivityReceived(ITurnContext turnContext, ITurnState turnS
6162 {
6263 incomingText = $ "Respond to this chat message with chat id { turnContext . Activity . Conversation . Id } " +
6364 $ "From: { sender ? . Name } ({ sender ? . Id } )\n " +
64- $ "Message: { incomingText } ";
65+ $ "Message: { incomingText } \n " +
66+ "If user hasn't explicitly asked to send teams messages don't use teams mcp tool to respond, that causes double responses." ;
6567 }
6668 else if ( turnContext . Activity . Type == ActivityTypes . InstallationUpdate )
6769 {
@@ -101,11 +103,17 @@ public async Task<string> NewChatReceived(string chatId, string fromUser, string
101103
102104 public async Task HandleEmailNotificationAsync ( ITurnContext turnContext , ITurnState turnState , AgentNotificationActivity emailEvent )
103105 {
104- _logger . LogInformation ( "Processing email notification (Responses API) - NotificationType: {NotificationType}" , emailEvent . NotificationType ) ;
106+ var fromEmail = emailEvent . From . Id ;
107+ var emailJson = JsonSerializer . Serialize ( emailEvent , new JsonSerializerOptions { WriteIndented = true } ) ;
105108 var conversationId = turnContext . Activity . Conversation ? . Id ?? "email-notification" ;
106- var response = await InvokeResponsesApiAsync ( emailEvent . Text ?? string . Empty , conversationId ) ;
107- var responseActivity = MessageFactory . Text ( "a" ) ;
108- responseActivity . Entities . Add ( new EmailResponse ( response ) ) ;
109+ var response = await InvokeResponsesApiAsync ( $ "You received a new email. Please look at the email and return a response in html format. From: { fromEmail } \n Email details:\n { emailJson } ", conversationId ) ;
110+ var responseActivity = EmailResponse . CreateEmailResponseActivity ( response ) ;
111+
112+ _logger . LogInformation (
113+ "Outgoing email response activity - original ReplyToId={OriginalReplyToId}, ConversationId={ConversationId}" ,
114+ responseActivity . ReplyToId ,
115+ responseActivity . Conversation ? . Id ) ;
116+
109117 await turnContext . SendActivityAsync ( responseActivity ) ;
110118 }
111119
@@ -115,12 +123,6 @@ public Task HandleCommentNotificationAsync(ITurnContext turnContext, ITurnState
115123 return Task . CompletedTask ;
116124 }
117125
118- public Task HandleTeamsMessageAsync ( ITurnContext turnContext , ITurnState turnState , AgentNotificationActivity teamsEvent )
119- {
120- _logger . LogInformation ( "Processing Teams message (Responses API)" ) ;
121- return Task . CompletedTask ;
122- }
123-
124126 public Task HandleInstallationUpdateAsync ( ITurnContext turnContext , ITurnState turnState , AgentNotificationActivity installationEvent )
125127 {
126128 _logger . LogInformation ( "Processing installation update (Responses API)" ) ;
@@ -229,9 +231,10 @@ private static string GetResponseStoreDir()
229231
230232 private static string GetResponseIdFilePath ( string conversationId )
231233 {
232- // Sanitize conversation ID for use as filename
233- var safeId = Convert . ToBase64String ( Encoding . UTF8 . GetBytes ( conversationId ) )
234- . Replace ( '/' , '_' ) . Replace ( '+' , '-' ) . TrimEnd ( '=' ) ;
234+ // SHA-256 hash conversation ID to produce a fixed-length, filesystem-safe filename
235+ // and avoid PathTooLongException for long conversation IDs.
236+ var hashBytes = SHA256 . HashData ( Encoding . UTF8 . GetBytes ( conversationId ) ) ;
237+ var safeId = Convert . ToHexString ( hashBytes ) . ToLowerInvariant ( ) ;
235238 return Path . Combine ( GetResponseStoreDir ( ) , $ "{ safeId } .responseid") ;
236239 }
237240
0 commit comments