Skip to content

Commit

Permalink
Fix a null-ref exception and add telemetry for error response from Az…
Browse files Browse the repository at this point in the history
… Copilot (#291)
  • Loading branch information
daxian-dbw authored Nov 13, 2024
1 parent 6beaf33 commit d408899
Showing 1 changed file with 18 additions and 12 deletions.
30 changes: 18 additions & 12 deletions shell/agents/Microsoft.Azure.Agent/AzureAgent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -248,8 +248,10 @@ public async Task<bool> ChatAsync(string input, IShell shell)

if (_copilotResponse.IsError)
{
string errorMessage = _copilotResponse.Text;
Telemetry.Trace(AzTrace.Exception(errorMessage));
host.WriteErrorLine()
.WriteErrorLine(_copilotResponse.Text)
.WriteErrorLine(errorMessage)
.WriteErrorLine();
}
else
Expand Down Expand Up @@ -300,21 +302,25 @@ public async Task<bool> ChatAsync(string input, IShell shell)
}
}

// The 'ConversationState' could be null when Azure Copilot returns an error response.
var conversationState = _copilotResponse.ConversationState;
_turnsLeft = conversationState.TurnLimit - conversationState.TurnNumber;
if (_turnsLeft <= 5)
if (conversationState is not null)
{
string message = _turnsLeft switch
_turnsLeft = conversationState.TurnLimit - conversationState.TurnNumber;
if (_turnsLeft <= 5)
{
1 => $"[yellow]{_turnsLeft} request left[/]",
0 => $"[red]{_turnsLeft} request left[/]",
_ => $"[yellow]{_turnsLeft} requests left[/]",
};
string message = _turnsLeft switch
{
1 => $"[yellow]{_turnsLeft} request left[/]",
0 => $"[red]{_turnsLeft} request left[/]",
_ => $"[yellow]{_turnsLeft} requests left[/]",
};

host.RenderDivider(message, DividerAlignment.Right);
if (_turnsLeft is 0)
{
host.WriteLine("\nYou've reached the maximum length of a conversation. To continue, please run '/refresh' to start a new conversation.\n");
host.RenderDivider(message, DividerAlignment.Right);
if (_turnsLeft is 0)
{
host.WriteLine("\nYou've reached the maximum length of a conversation. To continue, please run '/refresh' to start a new conversation.\n");
}
}
}

Expand Down

0 comments on commit d408899

Please sign in to comment.