diff --git a/src/Client/StellarChat.Client.Web/Components/Settings/IntegrationsForm.razor b/src/Client/StellarChat.Client.Web/Components/Settings/IntegrationsForm.razor index 7b9ddb1..7c152ad 100644 --- a/src/Client/StellarChat.Client.Web/Components/Settings/IntegrationsForm.razor +++ b/src/Client/StellarChat.Client.Web/Components/Settings/IntegrationsForm.razor @@ -1,21 +1,52 @@ @inject ISettingsService _settingsService +@inject ISnackbar Snackbar
- + -
- - Ollama +
+ OpenAI + OpenAI + +
+ + +
+ API Key +
+ + + +
+
+ + You can generate one here + +
+
+
+ + + + +
+ + Ollama +
Endpoint
-
- Pull model -
- - - @foreach (var model in ModelCatalog) - { - - } - - Pull -
-
Save
+ + @code { + public bool Basic_Switch2 { get; set; } = false; + [CascadingParameter] private bool _IsDarkModeEnabled { get; set; } + private string BackgroundColorStyle => $"background-color: {(_IsDarkModeEnabled ? "var(--mud-palette-surface)" : "#d6d3d1")};"; + private AppSettingsResponse AppSettings { get; set; } = new(); private List ModelCatalog { get; set; } = new(); - private const string OllamaProvider = "ollama"; - private const string OpenAiProvider = "openai"; + private const string OllamaProvider = "Ollama"; + private const string OpenAiProvider = "OpenAI"; private bool showOpenAiApiKey = false; private bool showOllamaEndpoint = false; public string OllamaEndpoint { get; set; } = string.Empty; + public bool IsEnabledOllama { get; set; } = false; + public bool IsOllamaPanelExpanded { get; set; } = false; + + public bool IsEnabledOpenAi { get; set; } = false; + public bool IsOpenAiPanelExpanded { get; set; } = false; private void ToggleShowOpenAiApiKeyVisibility() => showOpenAiApiKey = !showOpenAiApiKey; private void ToggleShowOllamaEndpointVisibility() => showOllamaEndpoint = !showOllamaEndpoint; protected async override Task OnInitializedAsync() { + SetupInitialSettings(); await PopulateForm(); } @@ -79,12 +121,78 @@ if (settings is not null) { AppSettings = settings; - OllamaEndpoint = settings.Integrations.First(x => x.Name.Equals(OllamaProvider, StringComparison.InvariantCultureIgnoreCase))!.Endpoint ?? string.Empty; } } + private void SetupInitialSettings() + { + AppSettings.Integrations = + [ + new() + { + Name = OpenAiProvider, + ApiKey = string.Empty, + Endpoint = string.Empty, + IsEnabled = false, + }, + new() + { + Name = OllamaProvider, + ApiKey = string.Empty, + Endpoint = string.Empty, + IsEnabled = false, + }, + ]; + } + private async Task OnValidSubmit(EditContext context) { + await UpdateIntegrationsAsync(AppSettings); + StateHasChanged(); + } + + private async Task UpdateIntegrationsAsync(AppSettingsResponse appSettings) + { + if (appSettings is not null) + { + var response = await _settingsService.UpdateIntegrationsAsync(appSettings.Integrations); + + if (!response.Succeeded) + { + ShowSnackbar("Failed to update integration settings. Please try again.", Severity.Error); + } + ShowSnackbar("Integration settings updated successfully!", Severity.Success); + } + } + + private void OnOllamaSwitchChanged(ChangeEventArgs e) + { + var ollamaSettings = AppSettings.FetchIntegrationSettings(OllamaProvider); + + if (!ollamaSettings.IsEnabled) + { + IsOllamaPanelExpanded = false; + } + } + + private void OnOpenAiSwitchChanged(ChangeEventArgs e) + { + var openAiSettings = AppSettings.FetchIntegrationSettings(OpenAiProvider); + + if (!openAiSettings.IsEnabled) + { + IsOpenAiPanelExpanded = false; + } + } + + private void ShowSnackbar(string message, Severity severity) + { + Snackbar.Configuration.PositionClass = Defaults.Classes.Position.TopRight; + + Snackbar.Add(message, severity, options => + { + options.HideTransitionDuration = 100; + }); } } diff --git a/src/Client/StellarChat.Client.Web/Components/Settings/SettingsDrawer.razor b/src/Client/StellarChat.Client.Web/Components/Settings/SettingsDrawer.razor index 5b11f44..946edd9 100644 --- a/src/Client/StellarChat.Client.Web/Components/Settings/SettingsDrawer.razor +++ b/src/Client/StellarChat.Client.Web/Components/Settings/SettingsDrawer.razor @@ -4,7 +4,7 @@ - + @@ -24,6 +24,12 @@ + + @code { [Parameter] @@ -40,6 +46,7 @@ private string AppLogoSmall => _configuration["app:logoSmall"] ?? string.Empty; private string AppReleases => _configuration["app:releases"] ?? string.Empty; + private string TabsBackgroundColorStyle => $"color: {(_IsDarkModeEnabled ? "var(--mud-palette-surface)" : "#d6d3d1")}; height: 100%;"; private string BackgroundColorStyle => $"background-color: {(_IsDarkModeEnabled ? "#1e1f22" : "#d6d3d1")};"; protected override async Task OnInitializedAsync() diff --git a/src/Client/StellarChat.Client.Web/Extensions.cs b/src/Client/StellarChat.Client.Web/Extensions.cs index fc30125..5c83f8d 100644 --- a/src/Client/StellarChat.Client.Web/Extensions.cs +++ b/src/Client/StellarChat.Client.Web/Extensions.cs @@ -9,6 +9,7 @@ using StellarChat.Client.Web.Services.Storage; using StellarChat.Client.Web.Shared.Http; using StellarChat.Client.Web.State; +using StellarChat.Shared.Contracts.Settings; namespace StellarChat.Client.Web; @@ -31,4 +32,7 @@ public static WebAssemblyHostBuilder AddServices(this WebAssemblyHostBuilder bui return builder; } + + public static Integration FetchIntegrationSettings(this AppSettingsResponse settings, string providerName) + => settings.Integrations.First(x => x.Name.Equals(providerName, StringComparison.InvariantCultureIgnoreCase)); } diff --git a/src/Client/StellarChat.Client.Web/Services/Settings/SettingsService.cs b/src/Client/StellarChat.Client.Web/Services/Settings/SettingsService.cs index 965679e..03fb5be 100644 --- a/src/Client/StellarChat.Client.Web/Services/Settings/SettingsService.cs +++ b/src/Client/StellarChat.Client.Web/Services/Settings/SettingsService.cs @@ -20,7 +20,7 @@ public async ValueTask UpdateProfileAsync(string name, string avata return response; } - public async ValueTask UpdateIntegrationsAsync(List Integrations, string key = "app-settings") + public async ValueTask UpdateIntegrationsAsync(List Integrations, string key = SettingsKey) { var payload = new UpdateIntegrationsRequest(key, Integrations);