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 3 commits
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 cancellationToken{ co_await winrt::get_cancellation_token() };
cancellationToken.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
11 changes: 10 additions & 1 deletion src/cascadia/QueryExtension/ExtensionPalette.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,16 @@ namespace winrt::Microsoft::Terminal::Query::Extension::implementation

if (_lmProvider)
{
result = _lmProvider.GetResponseAsync(promptCopy).get();
const auto asyncOperation = _lmProvider.GetResponseAsync(promptCopy);
if (asyncOperation.wait_for(std::chrono::seconds(5)) == AsyncStatus::Completed)
PankajBhojwani marked this conversation as resolved.
Show resolved Hide resolved
{
result = asyncOperation.GetResults();
}
else
{
asyncOperation.Cancel();
result = winrt::make<SystemResponse>(RS_(L"UnknownErrorMessage"), ErrorTypes::Unknown, winrt::hstring{});
}
}
else
{
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 cancellationToken{ co_await winrt::get_cancellation_token() };
cancellationToken.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 cancellationToken{ co_await winrt::get_cancellation_token() };
cancellationToken.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
2 changes: 1 addition & 1 deletion src/cascadia/QueryExtension/Resources/en-US/Resources.resw
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@
<comment>The message presented to the user when they attempt to use the AI chat feature without providing an AI endpoint and key.</comment>
</data>
<data name="UnknownErrorMessage" xml:space="preserve">
<value>An error occurred. Your AI provider might not be correctly configured, or the service might be temporarily unavailable.</value>
<value>An error occurred. The service might be temporarily unavailable or there might be network connectivity issues.</value>
<comment>The error message presented to the user when we were unable to query the provided endpoint.</comment>
</data>
<data name="InvalidModelMessage" xml:space="preserve">
Expand Down
2 changes: 2 additions & 0 deletions src/cascadia/QueryExtension/pch.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ TRACELOGGING_DECLARE_PROVIDER(g_hQueryExtensionProvider);

#include <winrt/Windows.Data.Json.h>

#include <chrono>

// Manually include til after we include Windows.Foundation to give it winrt superpowers
#include "til.h"

Expand Down
Loading