Skip to content

Commit 4b1ff75

Browse files
authored
Fix duplicate text (#13326)
* Improve the debugging experience. - GetSuggestion gets cancelled during debugging because it times out. So we use an environment variable to control whether to allow cancellation from psreadline. - Use another macro to control if print message for telemetry. * Fix a bug that the command line is repeated in the suggestion. - When we parse the command line and there is an incomplete parameter list, we repeat the command line in the suggestion. - The cause is that we parse the command line and get the wrong parameter set from the user input
1 parent a239d68 commit 4b1ff75

File tree

6 files changed

+77
-11
lines changed

6 files changed

+77
-11
lines changed

tools/Az.Tools.Predictor/Az.Tools.Predictor.Test/AzPredictorServiceTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// ----------------------------------------------------------------------------------
1+
// ----------------------------------------------------------------------------------
22
//
33
// Copyright Microsoft Corporation
44
// Licensed under the Apache License, Version 2.0 (the "License");

tools/Az.Tools.Predictor/Az.Tools.Predictor.Test/AzPredictorTests.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,5 +146,26 @@ public void VerifySuggestion(string userInput)
146146

147147
Assert.Equal(expected.Select(e => e.Item1), actual.Select(a => a.SuggestionText));
148148
}
149+
150+
/// <summary>
151+
/// Verifies that we still return correct prediction when the user has input an incomplete command line.
152+
/// </summary>
153+
[Fact]
154+
public void VerifySuggestionOnIncompleteCommand()
155+
{
156+
// We need to get the suggestions for more than one. So we create a local version az predictor.
157+
var localAzPredictor = new AzPredictor(this._service, this._telemetryClient, new Settings()
158+
{
159+
SuggestionCount = 7,
160+
});
161+
162+
var userInput = "New-AzResourceGroup -Name 'ResourceGroup01' -Location 'Central US' -WhatIf -";
163+
var expected = "New-AzResourceGroup -Name 'ResourceGroup01' -Location 'Central US' -WhatIf -Verbose ***";
164+
165+
var predictionContext = PredictionContext.Create(userInput);
166+
var actual = localAzPredictor.GetSuggestion(predictionContext, CancellationToken.None);
167+
168+
Assert.Equal(expected, actual.First().SuggestionText);
169+
}
149170
}
150171
}

tools/Az.Tools.Predictor/Az.Tools.Predictor/AzPredictor.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,9 +158,13 @@ public List<PredictiveSuggestion> GetSuggestion(PredictionContext context, Cance
158158

159159
try
160160
{
161-
suggestions = _service.GetSuggestion(context.InputAst, _settings.SuggestionCount.Value, cancellationToken);
161+
suggestions = _service.GetSuggestion(context.InputAst, _settings.SuggestionCount.Value, Settings.ContinueOnTimeout ? CancellationToken.None : cancellationToken);
162+
163+
if (!Settings.ContinueOnTimeout)
164+
{
165+
cancellationToken.ThrowIfCancellationRequested();
166+
}
162167

163-
cancellationToken.ThrowIfCancellationRequested();
164168
var userInput = context.InputAst.Extent.Text;
165169
return suggestions.Select((r, index) =>
166170
{

tools/Az.Tools.Predictor/Az.Tools.Predictor/AzPredictorTelemetryClient.cs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@
1818
using Newtonsoft.Json;
1919
using System;
2020
using System.Collections.Generic;
21-
using System.Diagnostics;
2221
using System.Globalization;
23-
using System.IO;
2422

2523
namespace Microsoft.Azure.PowerShell.Tools.AzPredictor
2624
{
@@ -69,7 +67,7 @@ public void OnHistory(string historyLine)
6967

7068
_telemetryClient.TrackEvent($"{AzPredictorTelemetryClient.TelemetryEventPrefix}/CommandHistory", currentLog);
7169

72-
#if DEBUG
70+
#if TELEMETRY_TRACE && DEBUG
7371
Console.WriteLine("Recording CommandHistory");
7472
#endif
7573
}
@@ -93,7 +91,7 @@ public void OnRequestPrediction(string command)
9391

9492
_telemetryClient.TrackEvent($"{AzPredictorTelemetryClient.TelemetryEventPrefix}/RequestPrediction", currentLog);
9593

96-
#if DEBUG
94+
#if TELEMETRY_TRACE && DEBUG
9795
Console.WriteLine("Recording RequestPrediction");
9896
#endif
9997
}
@@ -116,7 +114,7 @@ public void OnRequestPredictionError(string command, Exception e)
116114

117115
_telemetryClient.TrackEvent($"{AzPredictorTelemetryClient.TelemetryEventPrefix}/RequestPredictionError", currentLog);
118116

119-
#if DEBUG
117+
#if TELEMETRY_TRACE && DEBUG
120118
Console.WriteLine("Recording RequestPredictionError");
121119
#endif
122120
}
@@ -138,7 +136,7 @@ public void OnSuggestionAccepted(string acceptedSuggestion)
138136

139137
_telemetryClient.TrackEvent($"{AzPredictorTelemetryClient.TelemetryEventPrefix}/AcceptSuggestion", properties);
140138

141-
#if DEBUG
139+
#if TELEMETRY_TRACE && DEBUG
142140
Console.WriteLine("Recording AcceptSuggestion");
143141
#endif
144142
}
@@ -162,7 +160,7 @@ public void OnGetSuggestion(string maskedUserInput, IEnumerable<ValueTuple<strin
162160

163161
_telemetryClient.TrackEvent($"{AzPredictorTelemetryClient.TelemetryEventPrefix}/GetSuggestion", properties);
164162

165-
#if DEBUG
163+
#if TELEMETRY_TRACE && DEBUG
166164
Console.WriteLine("Recording GetSuggestioin");
167165
#endif
168166
}
@@ -184,7 +182,7 @@ public void OnGetSuggestionError(Exception e)
184182

185183
_telemetryClient.TrackEvent($"{AzPredictorTelemetryClient.TelemetryEventPrefix}/GetSuggestionError", properties);
186184

187-
#if DEBUG
185+
#if TELEMETRY_TRACE && DEBUG
188186
Console.WriteLine("Recording GetSuggestioinError");
189187
#endif
190188
}

tools/Az.Tools.Predictor/Az.Tools.Predictor/ParameterSet.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,19 @@ public ParameterSet(CommandAst commandAst)
4545
param = elem;
4646
arg = null;
4747
}
48+
else if (AzPredictorConstants.ParameterIndicator == elem?.ToString().Trim().FirstOrDefault())
49+
{
50+
// We have an incomplete command line such as
51+
// `New-AzResourceGroup -Name ResourceGroup01 -Location WestUS -`
52+
// We'll ignore the incomplete parameter.
53+
if (param != null)
54+
{
55+
Parameters.Add(new Tuple<string, string>(param.ToString(), arg?.ToString()));
56+
}
57+
58+
param = null;
59+
arg = null;
60+
}
4861
else
4962
{
5063
arg = elem;

tools/Az.Tools.Predictor/Az.Tools.Predictor/Settings.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,36 @@ sealed class Settings
4141
/// </summary>
4242
public int? SuggestionCount { get; set; }
4343

44+
private static bool? _isContinueOnTimeout;
45+
/// <summary>
46+
/// Gets the value to indicate whether to ignore cancellation request and keep running.
47+
/// This should be only set during debugging.
48+
/// </summary>
49+
[JsonIgnore]
50+
internal static bool ContinueOnTimeout
51+
{
52+
get
53+
{
54+
if (_isContinueOnTimeout.HasValue)
55+
{
56+
return _isContinueOnTimeout.Value;
57+
}
58+
59+
var envValue = Environment.GetEnvironmentVariable("AzPredictorContinueOnTimeout");
60+
61+
if (bool.TryParse(envValue, out bool envBoolValue))
62+
{
63+
_isContinueOnTimeout = envBoolValue;
64+
}
65+
else
66+
{
67+
_isContinueOnTimeout = false;
68+
}
69+
70+
return _isContinueOnTimeout.Value;
71+
}
72+
}
73+
4474
/// <summary>
4575
/// Gets an instance of the settings.
4676
/// </summary>

0 commit comments

Comments
 (0)