Skip to content

Commit d408899

Browse files
authored
Fix a null-ref exception and add telemetry for error response from Az Copilot (#291)
1 parent 6beaf33 commit d408899

File tree

1 file changed

+18
-12
lines changed

1 file changed

+18
-12
lines changed

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

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -248,8 +248,10 @@ public async Task<bool> ChatAsync(string input, IShell shell)
248248

249249
if (_copilotResponse.IsError)
250250
{
251+
string errorMessage = _copilotResponse.Text;
252+
Telemetry.Trace(AzTrace.Exception(errorMessage));
251253
host.WriteErrorLine()
252-
.WriteErrorLine(_copilotResponse.Text)
254+
.WriteErrorLine(errorMessage)
253255
.WriteErrorLine();
254256
}
255257
else
@@ -300,21 +302,25 @@ public async Task<bool> ChatAsync(string input, IShell shell)
300302
}
301303
}
302304

305+
// The 'ConversationState' could be null when Azure Copilot returns an error response.
303306
var conversationState = _copilotResponse.ConversationState;
304-
_turnsLeft = conversationState.TurnLimit - conversationState.TurnNumber;
305-
if (_turnsLeft <= 5)
307+
if (conversationState is not null)
306308
{
307-
string message = _turnsLeft switch
309+
_turnsLeft = conversationState.TurnLimit - conversationState.TurnNumber;
310+
if (_turnsLeft <= 5)
308311
{
309-
1 => $"[yellow]{_turnsLeft} request left[/]",
310-
0 => $"[red]{_turnsLeft} request left[/]",
311-
_ => $"[yellow]{_turnsLeft} requests left[/]",
312-
};
312+
string message = _turnsLeft switch
313+
{
314+
1 => $"[yellow]{_turnsLeft} request left[/]",
315+
0 => $"[red]{_turnsLeft} request left[/]",
316+
_ => $"[yellow]{_turnsLeft} requests left[/]",
317+
};
313318

314-
host.RenderDivider(message, DividerAlignment.Right);
315-
if (_turnsLeft is 0)
316-
{
317-
host.WriteLine("\nYou've reached the maximum length of a conversation. To continue, please run '/refresh' to start a new conversation.\n");
319+
host.RenderDivider(message, DividerAlignment.Right);
320+
if (_turnsLeft is 0)
321+
{
322+
host.WriteLine("\nYou've reached the maximum length of a conversation. To continue, please run '/refresh' to start a new conversation.\n");
323+
}
318324
}
319325
}
320326

0 commit comments

Comments
 (0)