Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a timeout for getting suggestions from the LMProvider #18234

Open
wants to merge 7 commits into
base: feature/llm
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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() };
PankajBhojwani marked this conversation as resolved.
Show resolved Hide resolved
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;
PankajBhojwani marked this conversation as resolved.
Show resolved Hide resolved
// 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 };
PankajBhojwani marked this conversation as resolved.
Show resolved Hide resolved

Extension::IContext _context;

Expand Down
Loading