Skip to content

Commit

Permalink
got an idl
Browse files Browse the repository at this point in the history
  • Loading branch information
PankajBhojwani committed Jun 6, 2024
1 parent 5fd708f commit b44216b
Show file tree
Hide file tree
Showing 9 changed files with 110 additions and 20 deletions.
26 changes: 26 additions & 0 deletions src/cascadia/QueryExtension/AzureLLMProvider.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

#include "pch.h"
#include "AzureLLMProvider.h"
#include "../../types/inc/utils.hpp"
#include "LibraryResources.h"

#include "AzureLLMProvider.g.cpp"

namespace winrt::Microsoft::Terminal::Query::Extension::implementation
{
AzureLLMProvider::AzureLLMProvider(winrt::hstring endpoint, winrt::hstring key)
{
_AIEndpoint = endpoint;
_AIKey = key;
_httpClient = winrt::Windows::Web::Http::HttpClient{};
_httpClient.DefaultRequestHeaders().Accept().TryParseAdd(L"application/json");
_httpClient.DefaultRequestHeaders().Append(L"api-key", _AIKey);
}

void AzureLLMProvider::Initialize()
{
_Thing = L"ayy lmao";
}
}
29 changes: 29 additions & 0 deletions src/cascadia/QueryExtension/AzureLLMProvider.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

#pragma once

#include "AzureLLMProvider.g.h"

namespace winrt::Microsoft::Terminal::Query::Extension::implementation
{
struct AzureLLMProvider : AzureLLMProviderT<AzureLLMProvider>
{
AzureLLMProvider(winrt::hstring endpoint, winrt::hstring key);
void Initialize();

WINRT_PROPERTY(winrt::hstring, Thing);

private:
winrt::hstring _AIEndpoint;
winrt::hstring _AIKey;
winrt::Windows::Web::Http::HttpClient _httpClient{ nullptr };

winrt::Windows::Data::Json::JsonArray _jsonMessages;
};
}

namespace winrt::Microsoft::Terminal::Query::Extension::factory_implementation
{
BASIC_FACTORY(AzureLLMProvider);
}
14 changes: 14 additions & 0 deletions src/cascadia/QueryExtension/AzureLLMProvider.idl
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

import "ILLMProvider.idl";

namespace Microsoft.Terminal.Query.Extension
{
[default_interface] runtimeclass AzureLLMProvider : ILLMProvider
{
AzureLLMProvider(String endpt, String key);

String Thing();
}
}
14 changes: 4 additions & 10 deletions src/cascadia/QueryExtension/ExtensionPalette.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,13 @@ const std::wregex azureOpenAIEndpointRegex{ LR"(^https.*openai\.azure\.com)" };

namespace winrt::Microsoft::Terminal::Query::Extension::implementation
{
ExtensionPalette::ExtensionPalette()
ExtensionPalette::ExtensionPalette(winrt::hstring endpoint, winrt::hstring key)
{
InitializeComponent();

AIKeyAndEndpoint(endpoint, key);
_llmProvider = Extension::AzureLLMProvider{ endpoint, key };

_clearAndInitializeMessages(nullptr, nullptr);
ControlName(RS_(L"ControlName"));
QueryBoxPlaceholderText(RS_(L"CurrentShell"));
Expand All @@ -52,9 +55,6 @@ namespace winrt::Microsoft::Terminal::Query::Extension::implementation

_setFocusAndPlaceholderTextHelper();

// For the purposes of data collection, request the API key/endpoint *now*
_AIKeyAndEndpointRequestedHandlers(nullptr, nullptr);

TraceLoggingWrite(
g_hQueryExtensionProvider,
"QueryPaletteOpened",
Expand All @@ -74,9 +74,6 @@ namespace winrt::Microsoft::Terminal::Query::Extension::implementation

_setFocusAndPlaceholderTextHelper();

// For the purposes of data collection, request the API key/endpoint *now*
_AIKeyAndEndpointRequestedHandlers(nullptr, nullptr);

TraceLoggingWrite(
g_hQueryExtensionProvider,
"QueryPaletteOpened",
Expand Down Expand Up @@ -123,9 +120,6 @@ namespace winrt::Microsoft::Terminal::Query::Extension::implementation
TraceLoggingKeyword(MICROSOFT_KEYWORD_CRITICAL_DATA),
TelemetryPrivacyDataTag(PDT_ProductAndServiceUsage));

// request the latest LLM key and endpoint
_AIKeyAndEndpointRequestedHandlers(nullptr, nullptr);

// Use a flag for whether the response the user receives is an error message
// we pass this flag to _splitResponseAndAddToChatHelper so it can send the relevant telemetry event
// there is only one case downstream from here that sets this flag to false, so start with it being true
Expand Down
6 changes: 4 additions & 2 deletions src/cascadia/QueryExtension/ExtensionPalette.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
#include "ChatMessage.g.h"
#include "GroupedChatMessages.g.h"

#include "AzureLLMProvider.h"

namespace winrt::Microsoft::Terminal::Query::Extension::implementation
{
struct ExtensionPalette : ExtensionPaletteT<ExtensionPalette>
{
ExtensionPalette();
ExtensionPalette(winrt::hstring endpoint, winrt::hstring key);

// We don't use the winrt_property macro here because we just need the setter
void AIKeyAndEndpoint(const winrt::hstring& endpoint, const winrt::hstring& key);
Expand All @@ -27,7 +29,6 @@ namespace winrt::Microsoft::Terminal::Query::Extension::implementation
WINRT_OBSERVABLE_PROPERTY(Windows::UI::Xaml::Controls::IconElement, ResolvedIcon, _PropertyChangedHandlers, nullptr);

TYPED_EVENT(ActiveControlInfoRequested, winrt::Microsoft::Terminal::Query::Extension::ExtensionPalette, Windows::Foundation::IInspectable);
TYPED_EVENT(AIKeyAndEndpointRequested, winrt::Microsoft::Terminal::Query::Extension::ExtensionPalette, Windows::Foundation::IInspectable);
TYPED_EVENT(InputSuggestionRequested, winrt::Microsoft::Terminal::Query::Extension::ExtensionPalette, winrt::hstring);

private:
Expand All @@ -39,6 +40,7 @@ namespace winrt::Microsoft::Terminal::Query::Extension::implementation
winrt::hstring _AIEndpoint;
winrt::hstring _AIKey;
winrt::Windows::Web::Http::HttpClient _httpClient{ nullptr };
ILLMProvider _llmProvider{ nullptr };

// chat history storage
Windows::Foundation::Collections::IObservableVector<GroupedChatMessages> _messages{ nullptr };
Expand Down
5 changes: 1 addition & 4 deletions src/cascadia/QueryExtension/ExtensionPalette.idl
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ namespace Microsoft.Terminal.Query.Extension

[default_interface] runtimeclass ExtensionPalette : Windows.UI.Xaml.Controls.UserControl, Windows.UI.Xaml.Data.INotifyPropertyChanged
{
ExtensionPalette();

void AIKeyAndEndpoint(String endpoint, String key);
ExtensionPalette(String endpoint, String key);

String ControlName { get; };
String QueryBoxPlaceholderText { get; };
Expand All @@ -36,7 +34,6 @@ namespace Microsoft.Terminal.Query.Extension
Windows.UI.Xaml.Controls.IconElement ResolvedIcon { get; };

event Windows.Foundation.TypedEventHandler<ExtensionPalette, IInspectable> ActiveControlInfoRequested;
event Windows.Foundation.TypedEventHandler<ExtensionPalette, IInspectable> AIKeyAndEndpointRequested;
event Windows.Foundation.TypedEventHandler<ExtensionPalette, String> InputSuggestionRequested;
}
}
10 changes: 10 additions & 0 deletions src/cascadia/QueryExtension/ILLMProvider.idl
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

namespace Microsoft.Terminal.Query.Extension
{
interface ILLMProvider
{
void Initialize();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@
<DependentUpon>ExtensionPaletteTemplateSelectors.idl</DependentUpon>
<SubType>Code</SubType>
</ClInclude>
<ClInclude Include="AzureLLMProvider.h">
<DependentUpon>AzureLLMProvider.idl</DependentUpon>
</ClInclude>
</ItemGroup>
<!-- ========================= XAML files ======================== -->
<ItemGroup>
Expand All @@ -74,6 +77,9 @@
<DependentUpon>ExtensionPaletteTemplateSelectors.idl</DependentUpon>
<SubType>Code</SubType>
</ClCompile>
<ClCompile Include="AzureLLMProvider.cpp">
<DependentUpon>AzureLLMProvider.idl</DependentUpon>
</ClCompile>
</ItemGroup>
<!-- ========================= idl Files ======================== -->
<ItemGroup>
Expand All @@ -84,6 +90,12 @@
<Midl Include="ExtensionPaletteTemplateSelectors.idl">
<SubType>Designer</SubType>
</Midl>
<Midl Include="ILLMProvider.idl">
<SubType>Code</SubType>
</Midl>
<Midl Include="AzureLLMProvider.idl">
<SubType>Code</SubType>
</Midl>
</ItemGroup>
<!-- ========================= Misc Files ======================== -->
<ItemGroup>
Expand Down
14 changes: 10 additions & 4 deletions src/cascadia/TerminalApp/TerminalPage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,14 @@ namespace winrt::TerminalApp::implementation
p.SetActionMap(_settings.ActionMap());
}

if (_extensionPalette)
{
// the extension palette had been loaded with the previous settings
// reload it with the new settings
_extensionPalette = nullptr;
_loadQueryExtension();
}

if (needRefreshUI)
{
_RefreshUIForSettingsReload();
Expand Down Expand Up @@ -5219,7 +5227,7 @@ namespace winrt::TerminalApp::implementation
appPrivate->PrepareForAIChat();
}
}
_extensionPalette = winrt::Microsoft::Terminal::Query::Extension::ExtensionPalette();
_extensionPalette = winrt::Microsoft::Terminal::Query::Extension::ExtensionPalette(_settings.AIEndpoint(), _settings.AIKey());
_extensionPalette.RegisterPropertyChangedCallback(UIElement::VisibilityProperty(), [&](auto&&, auto&&) {
if (_extensionPalette.Visibility() == Visibility::Collapsed)
{
Expand Down Expand Up @@ -5260,9 +5268,7 @@ namespace winrt::TerminalApp::implementation
_extensionPalette.ActiveCommandline(L"");
}
});
_extensionPalette.AIKeyAndEndpointRequested([&](IInspectable const&, IInspectable const&) {
_extensionPalette.AIKeyAndEndpoint(_settings.AIEndpoint(), _settings.AIKey());
});

ExtensionPresenter().Content(_extensionPalette);
}
}

0 comments on commit b44216b

Please sign in to comment.