@@ -247,7 +247,9 @@ namespace winrt::Microsoft::Terminal::Query::Extension::implementation
247
247
248
248
// Make sure we are on the background thread for the http request
249
249
auto strongThis = get_strong ();
250
+
250
251
co_await winrt::resume_background ();
252
+ auto cancellationToken{ co_await winrt::get_cancellation_token () };
251
253
252
254
for (bool refreshAttempted = false ;;)
253
255
{
@@ -276,24 +278,37 @@ namespace winrt::Microsoft::Terminal::Query::Extension::implementation
276
278
};
277
279
278
280
// Send the request
279
- const auto jsonResultOperation = _SendRequestReturningJson (_endpointUri, requestContent, WWH::HttpMethod::Post ());
280
- auto cancellationToken{ co_await winrt::get_cancellation_token () };
281
- cancellationToken.callback ([jsonResultOperation] {
282
- jsonResultOperation.Cancel ();
281
+ const auto sendRequestOperation = _SendRequestReturningJson (_endpointUri, requestContent, WWH::HttpMethod::Post ());
282
+
283
+ // if the caller cancels this operation, make sure to cancel the http request as well
284
+ cancellationToken.callback ([sendRequestOperation] {
285
+ sendRequestOperation.Cancel ();
283
286
});
284
- const auto jsonResult = co_await jsonResultOperation;
285
- if (jsonResult. HasKey (errorKey) )
287
+
288
+ if (sendRequestOperation. wait_for ( std::chrono::seconds ( 5 )) == AsyncStatus::Completed )
286
289
{
287
- const auto errorObject = jsonResult.GetNamedObject (errorKey);
288
- message = errorObject.GetNamedString (messageKey);
289
- errorType = ErrorTypes::FromProvider;
290
+ // Parse out the suggestion from the response
291
+ const auto jsonResult = sendRequestOperation.GetResults ();
292
+ if (jsonResult.HasKey (errorKey))
293
+ {
294
+ const auto errorObject = jsonResult.GetNamedObject (errorKey);
295
+ message = errorObject.GetNamedString (messageKey);
296
+ errorType = ErrorTypes::FromProvider;
297
+ }
298
+ else
299
+ {
300
+ const auto choices = jsonResult.GetNamedArray (L" ayy" );
301
+ const auto firstChoice = choices.GetAt (0 ).GetObject ();
302
+ const auto messageObject = firstChoice.GetNamedObject (messageKey);
303
+ message = messageObject.GetNamedString (contentKey);
304
+ }
290
305
}
291
306
else
292
307
{
293
- const auto choices = jsonResult. GetNamedArray (choicesKey);
294
- const auto firstChoice = choices. GetAt ( 0 ). GetObject ();
295
- const auto messageObject = firstChoice. GetNamedObject (messageKey );
296
- message = messageObject. GetNamedString (contentKey) ;
308
+ // if the http request takes too long, cancel the http request and return an error
309
+ sendRequestOperation. Cancel ();
310
+ message = RS_ ( L" UnknownErrorMessage " );
311
+ errorType = ErrorTypes::Unknown ;
297
312
}
298
313
break ;
299
314
}
@@ -310,8 +325,23 @@ namespace winrt::Microsoft::Terminal::Query::Extension::implementation
310
325
break ;
311
326
}
312
327
313
- co_await _refreshAuthTokens ();
314
- refreshAttempted = true ;
328
+ const auto refreshTokensAction = _refreshAuthTokens ();
329
+ cancellationToken.callback ([refreshTokensAction] {
330
+ refreshTokensAction.Cancel ();
331
+ });
332
+ // allow up to 10 seconds for reauthentication
333
+ if (refreshTokensAction.wait_for (std::chrono::seconds (10 )) == AsyncStatus::Completed)
334
+ {
335
+ refreshAttempted = true ;
336
+ }
337
+ else
338
+ {
339
+ // if the refresh action takes too long, cancel it and return an error
340
+ refreshTokensAction.Cancel ();
341
+ message = RS_ (L" UnknownErrorMessage" );
342
+ errorType = ErrorTypes::Unknown;
343
+ break ;
344
+ }
315
345
}
316
346
317
347
// Also make a new entry in our jsonMessages list, so the AI knows the full conversation so far
@@ -339,7 +369,12 @@ namespace winrt::Microsoft::Terminal::Query::Extension::implementation
339
369
340
370
try
341
371
{
342
- const auto jsonResult = co_await _SendRequestReturningJson (accessTokenEndpoint, requestContent, WWH::HttpMethod::Post ());
372
+ const auto reAuthOperation = _SendRequestReturningJson (accessTokenEndpoint, requestContent, WWH::HttpMethod::Post ());
373
+ auto cancellationToken{ co_await winrt::get_cancellation_token () };
374
+ cancellationToken.callback ([reAuthOperation] {
375
+ reAuthOperation.Cancel ();
376
+ });
377
+ const auto jsonResult{ co_await reAuthOperation };
343
378
344
379
_authToken = jsonResult.GetNamedString (accessTokenKey);
345
380
_refreshToken = jsonResult.GetNamedString (refreshTokenKey);
@@ -371,7 +406,6 @@ namespace winrt::Microsoft::Terminal::Query::Extension::implementation
371
406
sendRequestOperation.Cancel ();
372
407
});
373
408
const auto response{ co_await sendRequestOperation };
374
- _lastRequest = sendRequestOperation;
375
409
const auto string{ co_await response.Content ().ReadAsStringAsync () };
376
410
_lastResponse = string;
377
411
const auto jsonResult{ WDJ::JsonObject::Parse (string) };
0 commit comments