-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f101933
commit 733e123
Showing
5 changed files
with
108 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -247,7 +247,9 @@ namespace winrt::Microsoft::Terminal::Query::Extension::implementation | |
|
||
// Make sure we are on the background thread for the http request | ||
auto strongThis = get_strong(); | ||
|
||
co_await winrt::resume_background(); | ||
auto cancellationToken{ co_await winrt::get_cancellation_token() }; | ||
|
||
for (bool refreshAttempted = false;;) | ||
{ | ||
|
@@ -276,24 +278,37 @@ namespace winrt::Microsoft::Terminal::Query::Extension::implementation | |
}; | ||
|
||
// Send the request | ||
const auto jsonResultOperation = _SendRequestReturningJson(_endpointUri, requestContent, WWH::HttpMethod::Post()); | ||
auto cancellationToken{ co_await winrt::get_cancellation_token() }; | ||
cancellationToken.callback([jsonResultOperation] { | ||
jsonResultOperation.Cancel(); | ||
const auto sendRequestOperation = _SendRequestReturningJson(_endpointUri, requestContent, WWH::HttpMethod::Post()); | ||
|
||
// if the caller cancels this operation, make sure to cancel the http request as well | ||
cancellationToken.callback([sendRequestOperation] { | ||
sendRequestOperation.Cancel(); | ||
}); | ||
const auto jsonResult = co_await jsonResultOperation; | ||
if (jsonResult.HasKey(errorKey)) | ||
|
||
if (sendRequestOperation.wait_for(std::chrono::seconds(5)) == AsyncStatus::Completed) | ||
{ | ||
const auto errorObject = jsonResult.GetNamedObject(errorKey); | ||
message = errorObject.GetNamedString(messageKey); | ||
errorType = ErrorTypes::FromProvider; | ||
// Parse out the suggestion from the response | ||
const auto jsonResult = sendRequestOperation.GetResults(); | ||
if (jsonResult.HasKey(errorKey)) | ||
{ | ||
const auto errorObject = jsonResult.GetNamedObject(errorKey); | ||
message = errorObject.GetNamedString(messageKey); | ||
errorType = ErrorTypes::FromProvider; | ||
} | ||
else | ||
{ | ||
const auto choices = jsonResult.GetNamedArray(L"ayy"); | ||
Check failure Code scanning / check-spelling Unrecognized Spelling Error
ayy is not a recognized word. (unrecognized-spelling)
|
||
const auto firstChoice = choices.GetAt(0).GetObject(); | ||
const auto messageObject = firstChoice.GetNamedObject(messageKey); | ||
message = messageObject.GetNamedString(contentKey); | ||
} | ||
} | ||
else | ||
{ | ||
const auto choices = jsonResult.GetNamedArray(choicesKey); | ||
const auto firstChoice = choices.GetAt(0).GetObject(); | ||
const auto messageObject = firstChoice.GetNamedObject(messageKey); | ||
message = messageObject.GetNamedString(contentKey); | ||
// if the http request takes too long, cancel the http request and return an error | ||
sendRequestOperation.Cancel(); | ||
message = RS_(L"UnknownErrorMessage"); | ||
errorType = ErrorTypes::Unknown; | ||
} | ||
break; | ||
} | ||
|
@@ -310,8 +325,23 @@ namespace winrt::Microsoft::Terminal::Query::Extension::implementation | |
break; | ||
} | ||
|
||
co_await _refreshAuthTokens(); | ||
refreshAttempted = true; | ||
const auto refreshTokensAction = _refreshAuthTokens(); | ||
cancellationToken.callback([refreshTokensAction] { | ||
refreshTokensAction.Cancel(); | ||
}); | ||
// allow up to 10 seconds for reauthentication | ||
if (refreshTokensAction.wait_for(std::chrono::seconds(10)) == AsyncStatus::Completed) | ||
{ | ||
refreshAttempted = true; | ||
} | ||
else | ||
{ | ||
// if the refresh action takes too long, cancel it and return an error | ||
refreshTokensAction.Cancel(); | ||
message = RS_(L"UnknownErrorMessage"); | ||
errorType = ErrorTypes::Unknown; | ||
break; | ||
} | ||
} | ||
|
||
// 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 | |
|
||
try | ||
{ | ||
const auto jsonResult = co_await _SendRequestReturningJson(accessTokenEndpoint, requestContent, WWH::HttpMethod::Post()); | ||
const auto reAuthOperation = _SendRequestReturningJson(accessTokenEndpoint, requestContent, WWH::HttpMethod::Post()); | ||
auto cancellationToken{ co_await winrt::get_cancellation_token() }; | ||
cancellationToken.callback([reAuthOperation] { | ||
reAuthOperation.Cancel(); | ||
}); | ||
const auto jsonResult{ co_await reAuthOperation }; | ||
|
||
_authToken = jsonResult.GetNamedString(accessTokenKey); | ||
_refreshToken = jsonResult.GetNamedString(refreshTokenKey); | ||
|
@@ -371,7 +406,6 @@ namespace winrt::Microsoft::Terminal::Query::Extension::implementation | |
sendRequestOperation.Cancel(); | ||
}); | ||
const auto response{ co_await sendRequestOperation }; | ||
_lastRequest = sendRequestOperation; | ||
const auto string{ co_await response.Content().ReadAsStringAsync() }; | ||
_lastResponse = string; | ||
const auto jsonResult{ WDJ::JsonObject::Parse(string) }; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters