Skip to content

Commit

Permalink
aiconfig struct
Browse files Browse the repository at this point in the history
  • Loading branch information
PankajBhojwani committed Jul 4, 2024
1 parent 08dd951 commit 1934b30
Show file tree
Hide file tree
Showing 11 changed files with 144 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/cascadia/TerminalSettingsEditor/AISettingsViewModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ namespace winrt::Microsoft::Terminal::Settings::Editor::implementation
winrt::hstring OpenAIKey();
void OpenAIKey(winrt::hstring key);

GETSET_BINDABLE_ENUM_SETTING(ActiveProvider, Model::LLMProvider, _Settings.GlobalSettings().ActiveProvider);
GETSET_BINDABLE_ENUM_SETTING(ActiveProvider, Model::LLMProvider, _Settings.GlobalSettings().AIInfo().ActiveProvider);

private:
Model::CascadiaSettings _Settings;
Expand Down
2 changes: 0 additions & 2 deletions src/cascadia/TerminalSettingsEditor/AISettingsViewModel.idl
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ namespace Microsoft.Terminal.Settings.Editor
{
AISettingsViewModel(Microsoft.Terminal.Settings.Model.CascadiaSettings settings);

//Microsoft.Terminal.Settings.Model.LLMProvider ActiveProvider;

Boolean AreAzureOpenAIKeyAndEndpointSet { get; };
String AzureOpenAIEndpoint;
String AzureOpenAIKey;
Expand Down
48 changes: 48 additions & 0 deletions src/cascadia/TerminalSettingsModel/AIConfig.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

#include "pch.h"
#include "AIConfig.h"
#include "AIConfig.g.cpp"

#include "TerminalSettingsSerializationHelpers.h"
#include "JsonUtils.h"

using namespace Microsoft::Terminal::Settings::Model;
using namespace winrt::Microsoft::Terminal::Settings::Model::implementation;

static constexpr std::string_view AIConfigKey{ "aiConfig" };

winrt::com_ptr<AIConfig> AIConfig::CopyAIConfig(const AIConfig* source)
{
auto aiConfig{ winrt::make_self<AIConfig>() };

#define AI_SETTINGS_COPY(type, name, jsonKey, ...) \
aiConfig->_##name = source->_##name;
MTSM_AI_SETTINGS(AI_SETTINGS_COPY)
#undef AI_SETTINGS_COPY

return aiConfig;
}

Json::Value AIConfig::ToJson() const
{
Json::Value json{ Json::ValueType::objectValue };

#define AI_SETTINGS_TO_JSON(type, name, jsonKey, ...) \
JsonUtils::SetValueForKey(json, jsonKey, _##name);
MTSM_AI_SETTINGS(AI_SETTINGS_TO_JSON)
#undef AI_SETTINGS_TO_JSON

return json;
}

void AIConfig::LayerJson(const Json::Value& json)
{
const auto aiConfigJson = json[JsonKey(AIConfigKey)];

#define AI_SETTINGS_LAYER_JSON(type, name, jsonKey, ...) \
JsonUtils::GetValueForKey(aiConfigJson, jsonKey, _##name);
MTSM_AI_SETTINGS(AI_SETTINGS_LAYER_JSON)
#undef AI_SETTINGS_LAYER_JSON
}
41 changes: 41 additions & 0 deletions src/cascadia/TerminalSettingsModel/AIConfig.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*++
Copyright (c) Microsoft Corporation
Licensed under the MIT license.
Module Name:
- AIConfig
Abstract:
- The implementation of the AIConfig winrt class. Provides settings related
to the AI settings of the terminal
Author(s):
- Pankaj Bhojwani - June 2024
--*/

#pragma once

#include "pch.h"
#include "AIConfig.g.h"
#include "IInheritable.h"
#include "JsonUtils.h"
#include "MTSMSettings.h"
#include <DefaultSettings.h>

namespace winrt::Microsoft::Terminal::Settings::Model::implementation
{
struct AIConfig : AIConfigT<AIConfig>, IInheritable<AIConfig>
{
public:
AIConfig() = default;
static winrt::com_ptr<AIConfig> CopyAIConfig(const AIConfig* source);
Json::Value ToJson() const;
void LayerJson(const Json::Value& json);

#define AI_SETTINGS_INITIALIZE(type, name, jsonKey, ...) \
INHERITABLE_SETTING(Model::AIConfig, type, name, ##__VA_ARGS__)
MTSM_AI_SETTINGS(AI_SETTINGS_INITIALIZE)
#undef AI_SETTINGS_INITIALIZE
};
}
17 changes: 17 additions & 0 deletions src/cascadia/TerminalSettingsModel/AIConfig.idl
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

#include "IInheritable.idl.h"

namespace Microsoft.Terminal.Settings.Model
{
enum LLMProvider
{
AzureOpenAI,
OpenAI
};

[default_interface] runtimeclass AIConfig {
INHERITABLE_SETTING(LLMProvider, ActiveProvider);
}
}
18 changes: 18 additions & 0 deletions src/cascadia/TerminalSettingsModel/GlobalAppSettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ static constexpr std::string_view ThemeKey{ "theme" };
static constexpr std::string_view DefaultProfileKey{ "defaultProfile" };
static constexpr std::string_view LegacyUseTabSwitcherModeKey{ "useTabSwitcher" };
static constexpr std::string_view LegacyReloadEnvironmentVariablesKey{ "compatibility.reloadEnvironmentVariables" };
static constexpr std::string_view AIInfoKey{ "aiConfig" };

// Method Description:
// - Copies any extraneous data from the parent before completing a CreateChild call
Expand All @@ -45,6 +46,7 @@ void GlobalAppSettings::_FinalizeInheritance()
}
}
}

_actionMap->_FinalizeInheritance();
}

Expand All @@ -58,6 +60,9 @@ winrt::com_ptr<GlobalAppSettings> GlobalAppSettings::Copy() const
globals->_actionMap = _actionMap->Copy();
globals->_keybindingsWarnings = _keybindingsWarnings;

const auto aiInfo = AIConfig::CopyAIConfig(winrt::get_self<AIConfig>(_AIInfo));
globals->_AIInfo = *aiInfo;

#define GLOBAL_SETTINGS_COPY(type, name, jsonKey, ...) \
globals->_##name = _##name;
MTSM_GLOBAL_SETTINGS(GLOBAL_SETTINGS_COPY)
Expand Down Expand Up @@ -133,6 +138,10 @@ void GlobalAppSettings::LayerJson(const Json::Value& json, const OriginTag origi
// "useTabSwitcher", but prefer "tabSwitcherMode"
JsonUtils::GetValueForKey(json, LegacyUseTabSwitcherModeKey, _TabSwitcherMode);

// AI Settings
auto aiInfoImpl = winrt::get_self<implementation::AIConfig>(_AIInfo);
aiInfoImpl->LayerJson(json);

#define GLOBAL_SETTINGS_LAYER_JSON(type, name, jsonKey, ...) \
JsonUtils::GetValueForKey(json, jsonKey, _##name);
MTSM_GLOBAL_SETTINGS(GLOBAL_SETTINGS_LAYER_JSON)
Expand Down Expand Up @@ -263,6 +272,10 @@ Json::Value GlobalAppSettings::ToJson()

json[JsonKey(ActionsKey)] = _actionMap->ToJson();
json[JsonKey(KeybindingsKey)] = _actionMap->KeyBindingsToJson();
if (auto aiJSON = winrt::get_self<AIConfig>(_AIInfo)->ToJson(); !aiJSON.empty())
{
json[JsonKey(AIInfoKey)] = std::move(aiJSON);
}

return json;
}
Expand Down Expand Up @@ -312,3 +325,8 @@ bool GlobalAppSettings::ShouldUsePersistedLayout() const
{
return FirstWindowPreference() == FirstWindowPreference::PersistedWindowLayout && !IsolatedMode();
}

winrt::Microsoft::Terminal::Settings::Model::AIConfig GlobalAppSettings::AIInfo()
{
return _AIInfo;
}
4 changes: 4 additions & 0 deletions src/cascadia/TerminalSettingsModel/GlobalAppSettings.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Author(s):
#include "Theme.h"
#include "NewTabMenuEntry.h"
#include "RemainingProfilesEntry.h"
#include "AIConfig.h"

// fwdecl unittest classes
namespace SettingsModelUnitTests
Expand Down Expand Up @@ -72,6 +73,8 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation

bool LegacyReloadEnvironmentVariables() const noexcept { return _legacyReloadEnvironmentVariables; }

Model::AIConfig AIInfo();

INHERITABLE_SETTING(Model::GlobalAppSettings, hstring, UnparsedDefaultProfile, L"");

#define GLOBAL_SETTINGS_INITIALIZE(type, name, jsonKey, ...) \
Expand All @@ -93,5 +96,6 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
std::vector<SettingsLoadWarnings> _keybindingsWarnings;
Windows::Foundation::Collections::IMap<winrt::hstring, Model::ColorScheme> _colorSchemes{ winrt::single_threaded_map<winrt::hstring, Model::ColorScheme>() };
Windows::Foundation::Collections::IMap<winrt::hstring, Model::Theme> _themes{ winrt::single_threaded_map<winrt::hstring, Model::Theme>() };
Model::AIConfig _AIInfo{ winrt::make<AIConfig>() };
};
}
10 changes: 3 additions & 7 deletions src/cascadia/TerminalSettingsModel/GlobalAppSettings.idl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import "Theme.idl";
import "ColorScheme.idl";
import "ActionMap.idl";
import "NewTabMenuEntry.idl";
import "AIConfig.idl";

namespace Microsoft.Terminal.Settings.Model
{
Expand Down Expand Up @@ -49,12 +50,6 @@ namespace Microsoft.Terminal.Settings.Model
AfterCurrentTab,
};

enum LLMProvider
{
AzureOpenAI,
OpenAI
};

[default_interface] runtimeclass GlobalAppSettings {
Guid DefaultProfile;

Expand Down Expand Up @@ -109,7 +104,6 @@ namespace Microsoft.Terminal.Settings.Model
INHERITABLE_SETTING(Boolean, IsolatedMode);
INHERITABLE_SETTING(Boolean, AllowHeadless);
INHERITABLE_SETTING(String, SearchWebDefaultQueryUrl);
INHERITABLE_SETTING(LLMProvider, ActiveProvider);

Windows.Foundation.Collections.IMapView<String, ColorScheme> ColorSchemes();
void AddColorScheme(ColorScheme scheme);
Expand All @@ -124,5 +118,7 @@ namespace Microsoft.Terminal.Settings.Model
Theme CurrentTheme { get; };

Boolean ShouldUsePersistedLayout();

AIConfig AIInfo { get; };
}
}
6 changes: 4 additions & 2 deletions src/cascadia/TerminalSettingsModel/MTSMSettings.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,7 @@ Author(s):
X(winrt::Windows::Foundation::Collections::IVector<Model::NewTabMenuEntry>, NewTabMenu, "newTabMenu", winrt::single_threaded_vector<Model::NewTabMenuEntry>({ Model::RemainingProfilesEntry{} })) \
X(bool, AllowHeadless, "compatibility.allowHeadless", false) \
X(bool, IsolatedMode, "compatibility.isolatedMode", false) \
X(hstring, SearchWebDefaultQueryUrl, "searchWebDefaultQueryUrl", L"https://www.bing.com/search?q=%22%s%22") \
X(LLMProvider, ActiveProvider, "activeLLMProvider")
X(hstring, SearchWebDefaultQueryUrl, "searchWebDefaultQueryUrl", L"https://www.bing.com/search?q=%22%s%22")

// Also add these settings to:
// * Profile.idl
Expand Down Expand Up @@ -158,3 +157,6 @@ Author(s):
X(winrt::Microsoft::Terminal::Settings::Model::ThemeColor, UnfocusedBackground, "unfocusedBackground", nullptr) \
X(winrt::Microsoft::Terminal::Settings::Model::IconStyle, IconStyle, "iconStyle", winrt::Microsoft::Terminal::Settings::Model::IconStyle::Default) \
X(winrt::Microsoft::Terminal::Settings::Model::TabCloseButtonVisibility, ShowCloseButton, "showCloseButton", winrt::Microsoft::Terminal::Settings::Model::TabCloseButtonVisibility::Always)

#define MTSM_AI_SETTINGS(X) \
X(winrt::Microsoft::Terminal::Settings::Model::LLMProvider, ActiveProvider, "activeProvider")
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@
<ClInclude Include="FontConfig.h">
<DependentUpon>FontConfig.idl</DependentUpon>
</ClInclude>
<ClInclude Include="AIConfig.h">
<DependentUpon>AIConfig.idl</DependentUpon>
</ClInclude>
<ClInclude Include="EnumMappings.h">
<DependentUpon>EnumMappings.idl</DependentUpon>
</ClInclude>
Expand Down Expand Up @@ -176,6 +179,9 @@
<ClCompile Include="FontConfig.cpp">
<DependentUpon>FontConfig.idl</DependentUpon>
</ClCompile>
<ClCompile Include="AIConfig.cpp">
<DependentUpon>AIConfig.idl</DependentUpon>
</ClCompile>
<ClCompile Include="TerminalSettings.cpp">
<DependentUpon>TerminalSettings.idl</DependentUpon>
</ClCompile>
Expand Down Expand Up @@ -239,6 +245,7 @@
<Midl Include="IAppearanceConfig.idl" />
<Midl Include="ISettingsModelObject.idl" />
<Midl Include="FontConfig.idl" />
<Midl Include="AIConfig.idl" />
</ItemGroup>
<!-- ========================= Misc Files ======================== -->
<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@
<Midl Include="AppearanceConfig.idl" />
<Midl Include="IAppearanceConfig.idl" />
<Midl Include="FontConfig.idl" />
<Midl Include="AIConfig.idl" />
<Midl Include="DefaultTerminal.idl" />
<Midl Include="ApplicationState.idl" />
<Midl Include="NewTabMenuEntry.idl" />
Expand Down

0 comments on commit 1934b30

Please sign in to comment.