11using System . Diagnostics ;
22using System . Text ;
3- using System . Text . Json ;
3+
44using AIShell . Abstraction ;
55using Azure . Identity ;
66using Serilog ;
@@ -17,6 +17,7 @@ public sealed class AzureAgent : ILLMAgent
1717 public string SettingFile { private set ; get ; }
1818
1919 internal ArgumentPlaceholder ArgPlaceholder { set ; get ; }
20+ internal CopilotResponse CopilotResponse { set ; get ; }
2021
2122 private const string SettingFileName = "az.config.json" ;
2223 private const string LoggingFileName = "log..txt" ;
@@ -37,13 +38,13 @@ 7. DO NOT include the placeholder summary when the commands contains no placehol
3738 """ ;
3839
3940 private int _turnsLeft ;
40- internal CopilotResponse _copilotResponse ;
41+ private CopilotResponse _copilotResponse ;
4142 private AgentSetting _setting ;
4243
4344 private readonly string _instructions ;
4445 private readonly StringBuilder _buffer ;
4546 private readonly HttpClient _httpClient ;
46- internal readonly ChatSession _chatSession ;
47+ private readonly ChatSession _chatSession ;
4748 private readonly Dictionary < string , string > _valueStore ;
4849 // private MetricHelper _metricHelper;
4950
@@ -111,36 +112,33 @@ public void Initialize(AgentConfig config)
111112 . CreateLogger ( ) ;
112113 Log . Information ( "Azure agent initialized." ) ;
113114 }
115+
116+ if ( _setting . Telemetry )
117+ {
118+ Telemetry . Initialize ( ) ;
119+ }
114120 }
115121
116122 public IEnumerable < CommandBase > GetCommands ( ) => [ new ReplaceCommand ( this ) ] ;
117- public bool CanAcceptFeedback ( UserAction action ) => ! MetricHelper . TelemetryOptOut ;
123+ public bool CanAcceptFeedback ( UserAction action ) => _setting . Telemetry ;
118124 public void OnUserAction ( UserActionPayload actionPayload ) {
119125 // Send telemetry about the user action.
120- // DisLike Action
121- string DetailedMessage = null ;
122- bool IsUserFeedback = false ;
123- if ( actionPayload . Action == UserAction . Dislike )
126+ bool isUserFeedback = false ;
127+ string details = null ;
128+ UserAction action = actionPayload . Action ;
129+
130+ if ( action is UserAction . Dislike )
124131 {
125- IsUserFeedback = true ;
126- DislikePayload dislikePayload = ( DislikePayload ) actionPayload ;
127- DetailedMessage = string . Format ( "{0} | {1}" , dislikePayload . ShortFeedback , dislikePayload . LongFeedback ) ;
132+ var dislike = ( DislikePayload ) actionPayload ;
133+ isUserFeedback = true ;
134+ details = string . Format ( "{0} | {1}" , dislike . ShortFeedback , dislike . LongFeedback ) ;
128135 }
129- else if ( actionPayload . Action == UserAction . Like )
136+ else if ( action is UserAction . Like )
130137 {
131- IsUserFeedback = true ;
138+ isUserFeedback = true ;
132139 }
133140
134- MetricHelper . metricHelper . LogTelemetry (
135- new AzTrace ( )
136- {
137- Command = actionPayload . Action . ToString ( ) ,
138- ConversationId = _chatSession . ConversationId ,
139- ActivityId = _copilotResponse . ReplyToId ,
140- EventType = IsUserFeedback ? "Feedback" : "UserAction" ,
141- TopicName = _copilotResponse . TopicName ,
142- DetailedMessage = DetailedMessage
143- } ) ;
141+ Telemetry . Log ( AzTrace . UserAction ( action . ToString ( ) , _copilotResponse , details , isUserFeedback ) ) ;
144142 }
145143
146144 public async Task RefreshChatAsync ( IShell shell , bool force )
@@ -282,16 +280,9 @@ public async Task<bool> ChatAsync(string input, IShell shell)
282280 }
283281 }
284282
285- if ( ! MetricHelper . TelemetryOptOut )
283+ if ( Telemetry . Enabled )
286284 {
287- MetricHelper . metricHelper . LogTelemetry (
288- new AzTrace ( )
289- {
290- ConversationId = _chatSession . ConversationId ,
291- EventType = "Chat" ,
292- TopicName = _copilotResponse . TopicName ,
293- ActivityId = _copilotResponse . ReplyToId
294- } ) ;
285+ Telemetry . Log ( AzTrace . Chat ( _copilotResponse ) ) ;
295286 }
296287 }
297288 catch ( Exception ex ) when ( ex is TokenRequestException or ConnectionDroppedException )
@@ -401,18 +392,9 @@ private ResponseData ParseCLIHandlerResponse(IShell shell)
401392 else
402393 {
403394 // The placeholder section is not in the format as we've instructed ...
404- // TODO: send telemetry about this case.
405- if ( ! MetricHelper . TelemetryOptOut )
395+ if ( Telemetry . Enabled )
406396 {
407- MetricHelper . metricHelper . LogTelemetry (
408- new AzTrace ( )
409- {
410- ConversationId = _chatSession . ConversationId ,
411- ActivityId = _copilotResponse . ReplyToId ,
412- EventType = "Exception" ,
413- TopicName = _copilotResponse . TopicName ,
414- DetailedMessage = $ "Placeholder section not in expected format:{ text } "
415- } ) ;
397+ Telemetry . Log ( AzTrace . Exception ( _copilotResponse , $ "Placeholder section not in expected format: { text } ") ) ;
416398 }
417399 Log . Error ( "Placeholder section not in expected format:\n {0}" , text ) ;
418400 }
0 commit comments