Skip to content

Commit

Permalink
cancel the http request too
Browse files Browse the repository at this point in the history
  • Loading branch information
PankajBhojwani committed Nov 22, 2024
1 parent ddb864d commit 48debd9
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 3 deletions.
12 changes: 11 additions & 1 deletion src/cascadia/QueryExtension/AzureLLMProvider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,14 @@ namespace winrt::Microsoft::Terminal::Query::Extension::implementation

winrt::Windows::Foundation::IAsyncOperation<Extension::IResponse> AzureLLMProvider::GetResponseAsync(const winrt::hstring& userPrompt)
{
auto cancelation_token{ co_await winrt::get_cancellation_token() };
cancelation_token.callback([=] {
if (_lastRequest)
{
_lastRequest.Cancel();
}
});

// Use the ErrorTypes enum to flag whether the response the user receives is an error message
// we pass this enum back to the caller so they can handle it appropriately (specifically, ExtensionPalette will send the correct telemetry event)
ErrorTypes errorType{ ErrorTypes::None };
Expand Down Expand Up @@ -145,7 +153,9 @@ namespace winrt::Microsoft::Terminal::Query::Extension::implementation
// Send the request
try
{
const auto response = _httpClient.SendRequestAsync(request).get();
const auto sendRequestOperation = _httpClient.SendRequestAsync(request);
const auto response{ co_await sendRequestOperation };
_lastRequest = sendRequestOperation;
// Parse out the suggestion from the response
const auto string{ response.Content().ReadAsStringAsync().get() };
const auto jsonResult{ WDJ::JsonObject::Parse(string) };
Expand Down
1 change: 1 addition & 0 deletions src/cascadia/QueryExtension/AzureLLMProvider.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ namespace winrt::Microsoft::Terminal::Query::Extension::implementation
winrt::hstring _azureKey;
winrt::Windows::Web::Http::HttpClient _httpClient{ nullptr };
IBrandingData _brandingData{ winrt::make<AzureBranding>() };
winrt::Windows::Foundation::IAsyncOperationWithProgress<winrt::Windows::Web::Http::HttpResponseMessage, winrt::Windows::Web::Http::HttpProgress> _lastRequest{ nullptr };

Extension::IContext _context;

Expand Down
1 change: 1 addition & 0 deletions src/cascadia/QueryExtension/ExtensionPalette.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ namespace winrt::Microsoft::Terminal::Query::Extension::implementation
}
else
{
asyncOperation.Cancel();
result = winrt::make<SystemResponse>(RS_(L"UnknownErrorMessage"), ErrorTypes::Unknown, winrt::hstring{});
}
}
Expand Down
12 changes: 11 additions & 1 deletion src/cascadia/QueryExtension/GithubCopilotLLMProvider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,14 @@ namespace winrt::Microsoft::Terminal::Query::Extension::implementation

winrt::Windows::Foundation::IAsyncOperation<Extension::IResponse> GithubCopilotLLMProvider::GetResponseAsync(const winrt::hstring& userPrompt)
{
auto cancelation_token{ co_await winrt::get_cancellation_token() };
cancelation_token.callback([=] {
if (_lastRequest)
{
_lastRequest.Cancel();
}
});

// Use the ErrorTypes enum to flag whether the response the user receives is an error message
// we pass this enum back to the caller so they can handle it appropriately (specifically, ExtensionPalette will send the correct telemetry event)
ErrorTypes errorType{ ErrorTypes::None };
Expand Down Expand Up @@ -360,7 +368,9 @@ namespace winrt::Microsoft::Terminal::Query::Extension::implementation
WWH::HttpRequestMessage request{ method, Uri{ uri } };
request.Content(content);

const auto response{ co_await _httpClient.SendRequestAsync(request) };
const auto sendRequestOperation = _httpClient.SendRequestAsync(request);
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) };
Expand Down
1 change: 1 addition & 0 deletions src/cascadia/QueryExtension/GithubCopilotLLMProvider.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ namespace winrt::Microsoft::Terminal::Query::Extension::implementation
winrt::Windows::Web::Http::HttpClient _httpClient{ nullptr };
IBrandingData _brandingData{ winrt::make<GithubCopilotBranding>() };
winrt::hstring _lastResponse;
winrt::Windows::Foundation::IAsyncOperationWithProgress<winrt::Windows::Web::Http::HttpResponseMessage, winrt::Windows::Web::Http::HttpProgress> _lastRequest{ nullptr };

Extension::IContext _context;

Expand Down
12 changes: 11 additions & 1 deletion src/cascadia/QueryExtension/OpenAILLMProvider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,14 @@ namespace winrt::Microsoft::Terminal::Query::Extension::implementation

winrt::Windows::Foundation::IAsyncOperation<Extension::IResponse> OpenAILLMProvider::GetResponseAsync(const winrt::hstring userPrompt)
{
auto cancelation_token{ co_await winrt::get_cancellation_token() };
cancelation_token.callback([=] {
if (_lastRequest)
{
_lastRequest.Cancel();
}
});

// Use the ErrorTypes enum to flag whether the response the user receives is an error message
// we pass this enum back to the caller so they can handle it appropriately (specifically, ExtensionPalette will send the correct telemetry event)
ErrorTypes errorType{ ErrorTypes::None };
Expand Down Expand Up @@ -100,7 +108,9 @@ namespace winrt::Microsoft::Terminal::Query::Extension::implementation
// Send the request
try
{
const auto response = co_await _httpClient.SendRequestAsync(request);
const auto sendRequestOperation = _httpClient.SendRequestAsync(request);
const auto response{ co_await sendRequestOperation };
_lastRequest = sendRequestOperation;
// Parse out the suggestion from the response
const auto string{ co_await response.Content().ReadAsStringAsync() };
const auto jsonResult{ WDJ::JsonObject::Parse(string) };
Expand Down
1 change: 1 addition & 0 deletions src/cascadia/QueryExtension/OpenAILLMProvider.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ namespace winrt::Microsoft::Terminal::Query::Extension::implementation
winrt::hstring _AIKey;
winrt::Windows::Web::Http::HttpClient _httpClient{ nullptr };
IBrandingData _brandingData{ winrt::make<OpenAIBranding>() };
winrt::Windows::Foundation::IAsyncOperationWithProgress<winrt::Windows::Web::Http::HttpResponseMessage, winrt::Windows::Web::Http::HttpProgress> _lastRequest{ nullptr };

Extension::IContext _context;

Expand Down

0 comments on commit 48debd9

Please sign in to comment.