Skip to content

Commit 2683ed8

Browse files
committed
Prepare for telemetry collection
1 parent 9bb9f8a commit 2683ed8

File tree

3 files changed

+31
-27
lines changed

3 files changed

+31
-27
lines changed

shell/agents/Microsoft.Azure.Agent/AzureAgent.cs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ 7. DO NOT include the placeholder summary when the commands contains no placehol
3636
""";
3737

3838
private int _turnsLeft;
39+
private CopilotResponse _copilotResponse;
3940

4041
private readonly string _instructions;
4142
private readonly StringBuilder _buffer;
@@ -120,36 +121,36 @@ public async Task<bool> ChatAsync(string input, IShell shell)
120121
try
121122
{
122123
string query = $"{input}\n\n---\n\n{_instructions}";
123-
CopilotResponse copilotResponse = await host.RunWithSpinnerAsync(
124+
_copilotResponse = await host.RunWithSpinnerAsync(
124125
status: "Thinking ...",
125126
spinnerKind: SpinnerKind.Processing,
126127
func: async context => await _chatSession.GetChatResponseAsync(query, context, token)
127128
).ConfigureAwait(false);
128129

129-
if (copilotResponse is null)
130+
if (_copilotResponse is null)
130131
{
131132
// User cancelled the operation.
132133
return true;
133134
}
134135

135-
if (copilotResponse.ChunkReader is null)
136+
if (_copilotResponse.ChunkReader is null)
136137
{
137138
ArgPlaceholder?.DataRetriever?.Dispose();
138139
ArgPlaceholder = null;
139140

140141
// Process CLI handler response specially to support parameter injection.
141142
ResponseData data = null;
142-
if (copilotResponse.TopicName == CopilotActivity.CLIHandlerTopic)
143+
if (_copilotResponse.TopicName == CopilotActivity.CLIHandlerTopic)
143144
{
144-
data = ParseCLIHandlerResponse(copilotResponse, shell);
145+
data = ParseCLIHandlerResponse(shell);
145146
}
146147

147148
if (data?.PlaceholderSet is not null)
148149
{
149150
ArgPlaceholder = new ArgumentPlaceholder(input, data, _httpClient);
150151
}
151152

152-
string answer = data is null ? copilotResponse.Text : GenerateAnswer(data);
153+
string answer = data is null ? _copilotResponse.Text : GenerateAnswer(data);
153154
host.RenderFullResponse(answer);
154155
}
155156
else
@@ -161,12 +162,12 @@ public async Task<bool> ChatAsync(string input, IShell shell)
161162

162163
while (true)
163164
{
164-
CopilotActivity activity = copilotResponse.ChunkReader.ReadChunk(token);
165+
CopilotActivity activity = _copilotResponse.ChunkReader.ReadChunk(token);
165166
if (activity is null)
166167
{
167168
prevActivity.ExtractMetadata(out string[] suggestion, out ConversationState state);
168-
copilotResponse.SuggestedUserResponses = suggestion;
169-
copilotResponse.ConversationState = state;
169+
_copilotResponse.SuggestedUserResponses = suggestion;
170+
_copilotResponse.ConversationState = state;
170171
break;
171172
}
172173

@@ -182,7 +183,7 @@ public async Task<bool> ChatAsync(string input, IShell shell)
182183
}
183184
}
184185

185-
var conversationState = copilotResponse.ConversationState;
186+
var conversationState = _copilotResponse.ConversationState;
186187
_turnsLeft = conversationState.TurnLimit - conversationState.TurnNumber;
187188
if (_turnsLeft <= 5)
188189
{
@@ -210,9 +211,9 @@ public async Task<bool> ChatAsync(string input, IShell shell)
210211
return true;
211212
}
212213

213-
private ResponseData ParseCLIHandlerResponse(CopilotResponse copilotResponse, IShell shell)
214+
private ResponseData ParseCLIHandlerResponse(IShell shell)
214215
{
215-
string text = copilotResponse.Text;
216+
string text = _copilotResponse.Text;
216217
List<CodeBlock> codeBlocks = shell.ExtractCodeBlocks(text, out List<SourceInfo> sourceInfos);
217218
if (codeBlocks is null || codeBlocks.Count is 0)
218219
{
@@ -264,7 +265,7 @@ private ResponseData ParseCLIHandlerResponse(CopilotResponse copilotResponse, IS
264265
Text = text,
265266
CommandSet = commands,
266267
PlaceholderSet = placeholders,
267-
Locale = copilotResponse.Locale,
268+
Locale = _copilotResponse.Locale,
268269
};
269270

270271
string first = placeholders[0].Name;

shell/agents/Microsoft.Azure.Agent/ChatSession.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ internal class ChatSession : IDisposable
1616

1717
private string _token;
1818
private string _streamUrl;
19+
private string _conversationId;
1920
private string _conversationUrl;
2021
private DateTime _expireOn;
2122
private AzureCopilotReceiver _copilotReceiver;
@@ -24,6 +25,8 @@ internal class ChatSession : IDisposable
2425
private readonly HttpClient _httpClient;
2526
private readonly Dictionary<string, object> _flights;
2627

28+
internal string ConversationId => _conversationId;
29+
2730
internal ChatSession(HttpClient httpClient)
2831
{
2932
_dl_secret = Environment.GetEnvironmentVariable("DL_SECRET");
@@ -88,6 +91,7 @@ private void Reset()
8891
{
8992
_token = null;
9093
_streamUrl = null;
94+
_conversationId = null;
9195
_conversationUrl = null;
9296
_expireOn = DateTime.MinValue;
9397

@@ -135,7 +139,8 @@ private async Task StartConversationAsync(IHost host, CancellationToken cancella
135139
SessionPayload spl = JsonSerializer.Deserialize<SessionPayload>(content, Utils.JsonOptions);
136140

137141
_token = spl.Token;
138-
_conversationUrl = $"{CONVERSATION_URL}/{spl.ConversationId}/activities";
142+
_conversationId = spl.ConversationId;
143+
_conversationUrl = $"{CONVERSATION_URL}/{_conversationId}/activities";
139144
_streamUrl = spl.StreamUrl;
140145
_expireOn = DateTime.UtcNow.AddSeconds(spl.ExpiresIn);
141146
_copilotReceiver = await AzureCopilotReceiver.CreateAsync(_streamUrl);
@@ -278,8 +283,8 @@ internal async Task<CopilotResponse> GetChatResponseAsync(string input, IStatusC
278283
{
279284
CopilotResponse ret = activity.InputHint switch
280285
{
281-
"typing" => new CopilotResponse(new ChunkReader(_copilotReceiver, activity), activity.Locale, activity.TopicName),
282-
"acceptingInput" => new CopilotResponse(activity.Text, activity.Locale, activity.TopicName),
286+
"typing" => new CopilotResponse(activity, new ChunkReader(_copilotReceiver, activity)),
287+
"acceptingInput" => new CopilotResponse(activity),
283288
_ => throw CorruptDataException.Create($"The 'inputHint' is {activity.InputHint}.", activity)
284289
};
285290

shell/agents/Microsoft.Azure.Agent/Schema.cs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -102,30 +102,28 @@ internal class ConversationState
102102

103103
internal class CopilotResponse
104104
{
105-
internal CopilotResponse(ChunkReader chunkReader, string locale, string topicName)
105+
internal CopilotResponse(CopilotActivity activity, ChunkReader chunkReader)
106+
: this(activity)
106107
{
107108
ArgumentNullException.ThrowIfNull(chunkReader);
108-
ArgumentException.ThrowIfNullOrEmpty(topicName);
109-
110109
ChunkReader = chunkReader;
111-
Locale = locale;
112-
TopicName = topicName;
113110
}
114111

115-
internal CopilotResponse(string text, string locale, string topicName)
112+
internal CopilotResponse(CopilotActivity activity)
116113
{
117-
ArgumentException.ThrowIfNullOrEmpty(text);
118-
ArgumentException.ThrowIfNullOrEmpty(topicName);
114+
ArgumentNullException.ThrowIfNull(activity);
119115

120-
Text = text;
121-
Locale = locale;
122-
TopicName = topicName;
116+
Text = activity.Text;
117+
Locale = activity.Locale;
118+
TopicName = activity.TopicName;
119+
ReplyToId = activity.ReplyToId;
123120
}
124121

125122
internal ChunkReader ChunkReader { get; }
126123
internal string Text { get; }
127124
internal string Locale { get; }
128125
internal string TopicName { get; }
126+
internal string ReplyToId { get; }
129127
internal string[] SuggestedUserResponses { get; set; }
130128
internal ConversationState ConversationState { get; set; }
131129
}

0 commit comments

Comments
 (0)