12
12
// limitations under the License.
13
13
// ----------------------------------------------------------------------------------
14
14
15
+ using Microsoft . WindowsAzure . Commands . Utilities . Common ;
15
16
using Newtonsoft . Json ;
16
17
using Newtonsoft . Json . Serialization ;
17
18
using System ;
19
+ using System . Collections . Concurrent ;
18
20
using System . Collections . Generic ;
19
21
using System . Linq ;
20
22
using System . Management . Automation . Language ;
@@ -48,16 +50,15 @@ public sealed class RequestContext
48
50
public PredictionRequestBody ( string history ) => this . History = history ;
49
51
} ;
50
52
51
- private const int PredictionRequestInProgress = 1 ;
52
- private const int PredictionRequestNotInProgress = 0 ;
53
53
private static readonly HttpClient _client = new HttpClient ( ) ;
54
54
private readonly string _commandsEndpoint ;
55
55
private readonly string _predictionsEndpoint ;
56
56
private volatile Tuple < string , Predictor > _historySuggestions ; // The history and the prediction for that.
57
57
private volatile Predictor _commands ;
58
58
private volatile string _history ;
59
- private HashSet < string > _commandSet = new HashSet < string > ( StringComparer . OrdinalIgnoreCase ) ;
59
+ private HashSet < string > _commandSet ;
60
60
private CancellationTokenSource _predictionRequestCancellationSource ;
61
+ private ParameterValuePredictor _parameterValuePredictor = new ParameterValuePredictor ( ) ;
61
62
62
63
/// <summary>
63
64
/// The AzPredictor service interacts with the Aladdin service specified in serviceUri.
@@ -144,53 +145,58 @@ public Tuple<string, PredictionSource> GetSuggestion(Ast input, CancellationToke
144
145
}
145
146
146
147
/// <inheritdoc/>
147
- public virtual void RequestPredictions ( string history )
148
+ public virtual void RequestPredictions ( IEnumerable < string > history )
148
149
{
149
150
// Even if it's called multiple times, we only need to keep the one for the latest history.
150
151
151
152
this . _predictionRequestCancellationSource ? . Cancel ( ) ;
152
153
this . _predictionRequestCancellationSource = new CancellationTokenSource ( ) ;
153
154
var cancellationToken = this . _predictionRequestCancellationSource . Token ;
154
- this . _history = history ;
155
+ var localHistory = string . Join ( AzPredictorConstants . CommandConcatenator , history ) ;
156
+ this . _history = localHistory ;
155
157
156
158
// We don't need to block on the task. We send the HTTP request and update prediction list at the background.
157
159
Task . Run ( async ( ) => {
158
- var requestBody = JsonConvert . SerializeObject ( new PredictionRequestBody ( history ) ) ;
160
+ var requestBody = JsonConvert . SerializeObject ( new PredictionRequestBody ( localHistory ) ) ;
159
161
var httpResponseMessage = await _client . PostAsync ( this . _predictionsEndpoint , new StringContent ( requestBody , Encoding . UTF8 , "application/json" ) , cancellationToken ) ;
160
162
161
163
var reply = await httpResponseMessage . Content . ReadAsStringAsync ( cancellationToken ) ;
162
164
var suggestionsList = JsonConvert . DeserializeObject < List < string > > ( reply ) ;
163
165
164
- this . SetSuggestionPredictor ( history , suggestionsList ) ;
166
+ this . SetSuggestionPredictor ( localHistory , suggestionsList ) ;
165
167
} ,
166
168
cancellationToken ) ;
167
169
}
168
170
169
- /// <summary>
170
- /// For logging purposes, get the rank of the user input in the model suggestions list.
171
- /// </summary>
172
- public int ? GetRankOfSuggestion ( CommandAst command , Ast input )
171
+ /// <inheritdoc/>
172
+ public virtual void RecordHistory ( IEnumerable < CommandAst > history )
173
+ {
174
+ history . ForEach ( ( h ) => this . _parameterValuePredictor . ProcessHistoryCommand ( h ) ) ;
175
+ }
176
+
177
+ /// <inhericdoc/>
178
+ public int ? GetRankOfSuggestion ( string commandName )
173
179
{
174
180
var historySuggestions = this . _historySuggestions ;
175
- return historySuggestions ? . Item2 ? . GetCommandPrediction ( command , input , CancellationToken . None ) . Item2 ;
181
+ return historySuggestions ? . Item2 ? . GetCommandPrediction ( commandName , isCommandNameComplete : true , cancellationToken : CancellationToken . None ) . Item2 ;
176
182
}
177
183
178
- /// <inheritdoc />
179
- public int ? GetRankOfFallback ( CommandAst command , Ast input )
184
+ /// <inhericdoc />
185
+ public int ? GetRankOfFallback ( string commandName )
180
186
{
181
187
var commands = this . _commands ;
182
- return commands ? . GetCommandPrediction ( command , input , CancellationToken . None ) . Item2 ;
188
+ return commands ? . GetCommandPrediction ( commandName , isCommandNameComplete : true , cancellationToken : CancellationToken . None ) . Item2 ;
183
189
}
184
190
185
- /// <inheritdoc />
191
+ /// <inhericdoc />
186
192
public IEnumerable < string > GetTopNSuggestions ( int n )
187
193
{
188
194
var historySuggestions = this . _historySuggestions ;
189
195
return historySuggestions ? . Item2 ? . GetTopNPrediction ( n ) ;
190
196
}
191
197
192
198
/// <inheritdoc/>
193
- public bool IsSupportedCommand ( string cmd ) => ! string . IsNullOrWhiteSpace ( cmd ) && _commandSet . Contains ( cmd ) ;
199
+ public bool IsSupportedCommand ( string cmd ) => ! string . IsNullOrWhiteSpace ( cmd ) && ( _commandSet ? . Contains ( cmd ) == true ) ;
194
200
195
201
/// <summary>
196
202
/// Requests a list of popular commands from service. These commands are used as fallback suggestion
@@ -209,7 +215,10 @@ protected virtual void RequestCommands()
209
215
210
216
// Initialize predictions
211
217
var startHistory = $ "{ AzPredictorConstants . CommandHistoryPlaceholder } { AzPredictorConstants . CommandConcatenator } { AzPredictorConstants . CommandHistoryPlaceholder } ";
212
- RequestPredictions ( startHistory ) ;
218
+ RequestPredictions ( new string [ ] {
219
+ AzPredictorConstants . CommandHistoryPlaceholder ,
220
+ AzPredictorConstants . CommandHistoryPlaceholder } ) ;
221
+
213
222
} ) ;
214
223
}
215
224
@@ -219,8 +228,8 @@ protected virtual void RequestCommands()
219
228
/// <param name="commands">The command collection to set the predictor</param>
220
229
protected void SetCommandsPredictor ( IList < string > commands )
221
230
{
222
- this . _commands = new Predictor ( commands ) ;
223
- this . _commandSet = new HashSet < string > ( commands . Select ( x => AzPredictorService . GetCommandName ( x ) ) ) ; // this could be slow
231
+ this . _commands = new Predictor ( commands , this . _parameterValuePredictor ) ;
232
+ this . _commandSet = commands . Select ( x => AzPredictorService . GetCommandName ( x ) ) . ToHashSet < string > ( StringComparer . OrdinalIgnoreCase ) ; // this could be slow
224
233
225
234
}
226
235
@@ -231,7 +240,7 @@ protected void SetCommandsPredictor(IList<string> commands)
231
240
/// <param name="suggestions">The suggestion collection to set the predictor</param>
232
241
protected void SetSuggestionPredictor ( string history , IList < string > suggestions )
233
242
{
234
- this . _historySuggestions = Tuple . Create ( history , new Predictor ( suggestions ) ) ;
243
+ this . _historySuggestions = Tuple . Create ( history , new Predictor ( suggestions , this . _parameterValuePredictor ) ) ;
235
244
}
236
245
237
246
/// <summary>
0 commit comments