mirror of
https://github.com/MindWorkAI/AI-Studio.git
synced 2025-02-10 21:29:07 +00:00
Added settings & settings manager
This commit is contained in:
parent
6cbbcbc1e1
commit
cc75df4a15
@ -1,3 +1,4 @@
|
|||||||
|
using AIStudio.Settings;
|
||||||
using Microsoft.AspNetCore.Components;
|
using Microsoft.AspNetCore.Components;
|
||||||
using Microsoft.JSInterop;
|
using Microsoft.JSInterop;
|
||||||
|
|
||||||
|
46
app/MindWork AI Studio/Components/Pages/Settings.razor
Normal file
46
app/MindWork AI Studio/Components/Pages/Settings.razor
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
@page "/settings"
|
||||||
|
|
||||||
|
<MudText Typo="Typo.h3" Class="mb-12">Settings</MudText>
|
||||||
|
|
||||||
|
<MudPaper Class="pa-3">
|
||||||
|
<MudText Typo="Typo.h4" Class="mb-3">Configured Providers</MudText>
|
||||||
|
<MudTable Items="@this.Providers">
|
||||||
|
<ColGroup>
|
||||||
|
<col style="width: 3em;"/>
|
||||||
|
<col style="width: 6em;"/>
|
||||||
|
<col/>
|
||||||
|
<col style="width: 20em;"/>
|
||||||
|
</ColGroup>
|
||||||
|
<HeaderContent>
|
||||||
|
<MudTh>#</MudTh>
|
||||||
|
<MudTh>Provider</MudTh>
|
||||||
|
<MudTh>Name</MudTh>
|
||||||
|
<MudTh Style="text-align: left;">Actions</MudTh>
|
||||||
|
</HeaderContent>
|
||||||
|
<RowTemplate>
|
||||||
|
<MudTd></MudTd>
|
||||||
|
<MudTd>@context.UsedProvider</MudTd>
|
||||||
|
<MudTd>@context.InstanceName</MudTd>
|
||||||
|
<MudTd Style="text-align: left;">
|
||||||
|
<MudButton Variant="Variant.Filled" Color="Color.Info" StartIcon="@Icons.Material.Filled.Edit" Class="mr-2">
|
||||||
|
Edit
|
||||||
|
</MudButton>
|
||||||
|
<MudButton Variant="Variant.Filled" Color="Color.Error" StartIcon="@Icons.Material.Filled.Delete" Class="mr-2">
|
||||||
|
Delete
|
||||||
|
</MudButton>
|
||||||
|
</MudTd>
|
||||||
|
</RowTemplate>
|
||||||
|
</MudTable>
|
||||||
|
|
||||||
|
@if(this.Providers.Count == 0)
|
||||||
|
{
|
||||||
|
<MudText Typo="Typo.h6" Class="mt-3">No providers configured yet.</MudText>
|
||||||
|
}
|
||||||
|
|
||||||
|
<MudButton
|
||||||
|
Variant="Variant.Filled" Color="@Color.Primary"
|
||||||
|
StartIcon="@Icons.Material.Filled.AddRoad"
|
||||||
|
Class="mt-3" OnClick="@this.AddProvider">
|
||||||
|
Add Provider
|
||||||
|
</MudButton>
|
||||||
|
</MudPaper>
|
43
app/MindWork AI Studio/Components/Pages/Settings.razor.cs
Normal file
43
app/MindWork AI Studio/Components/Pages/Settings.razor.cs
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
using AIStudio.Settings;
|
||||||
|
using Microsoft.AspNetCore.Components;
|
||||||
|
|
||||||
|
using MudBlazor;
|
||||||
|
|
||||||
|
// ReSharper disable ClassNeverInstantiated.Global
|
||||||
|
|
||||||
|
namespace AIStudio.Components.Pages;
|
||||||
|
|
||||||
|
public partial class Settings : ComponentBase
|
||||||
|
{
|
||||||
|
[Inject]
|
||||||
|
public SettingsManager SettingsManager { get; init; } = null!;
|
||||||
|
|
||||||
|
[Inject]
|
||||||
|
public IDialogService DialogService { get; init; } = null!;
|
||||||
|
|
||||||
|
private List<global::AIStudio.Settings.Provider> Providers { get; set; } = new();
|
||||||
|
|
||||||
|
#region Overrides of ComponentBase
|
||||||
|
|
||||||
|
protected override async Task OnInitializedAsync()
|
||||||
|
{
|
||||||
|
var settings = await this.SettingsManager.LoadSettings();
|
||||||
|
this.Providers = settings.Providers;
|
||||||
|
|
||||||
|
await base.OnInitializedAsync();
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
private async Task AddProvider()
|
||||||
|
{
|
||||||
|
var dialogOptions = new DialogOptions { CloseOnEscapeKey = true, FullWidth = true, MaxWidth = MaxWidth.Medium };
|
||||||
|
var dialogReference = await this.DialogService.ShowAsync<ProviderDialog>("Add Provider", dialogOptions);
|
||||||
|
var dialogResult = await dialogReference.Result;
|
||||||
|
if (dialogResult.Canceled)
|
||||||
|
return;
|
||||||
|
|
||||||
|
var addedProvider = (AIStudio.Settings.Provider)dialogResult.Data;
|
||||||
|
this.Providers.Add(addedProvider);
|
||||||
|
}
|
||||||
|
}
|
8
app/MindWork AI Studio/Settings/Data.cs
Normal file
8
app/MindWork AI Studio/Settings/Data.cs
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
namespace AIStudio.Settings;
|
||||||
|
|
||||||
|
public sealed class Data
|
||||||
|
{
|
||||||
|
public Version Version { get; init; }
|
||||||
|
|
||||||
|
public List<Provider> Providers { get; init; } = new();
|
||||||
|
}
|
5
app/MindWork AI Studio/Settings/Provider.cs
Normal file
5
app/MindWork AI Studio/Settings/Provider.cs
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
using AIStudio.Provider;
|
||||||
|
|
||||||
|
namespace AIStudio.Settings;
|
||||||
|
|
||||||
|
public readonly record struct Provider(string Id, string InstanceName, Providers UsedProvider);
|
46
app/MindWork AI Studio/Settings/ProviderDialog.razor
Normal file
46
app/MindWork AI Studio/Settings/ProviderDialog.razor
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
@using AIStudio.Provider
|
||||||
|
@using MudBlazor
|
||||||
|
|
||||||
|
<MudDialog>
|
||||||
|
<DialogContent>
|
||||||
|
<MudForm @ref="@this.form" @bind-IsValid="@this.dataIsValid" @bind-Errors="@this.dataIssues">
|
||||||
|
<MudTextField
|
||||||
|
T="string"
|
||||||
|
@bind-Text="@this.dataInstanceName"
|
||||||
|
Label="Instance Name"
|
||||||
|
Adornment="Adornment.Start"
|
||||||
|
AdornmentIcon="@Icons.Material.Filled.Lightbulb"
|
||||||
|
AdornmentColor="Color.Info"
|
||||||
|
Required="@true"
|
||||||
|
RequiredError="Please add an instance name."
|
||||||
|
/>
|
||||||
|
|
||||||
|
@* ReSharper disable once CSharpWarnings::CS8974 *@
|
||||||
|
<MudSelect @bind-Value="@this.dataProvider" Label="Provider" OpenIcon="@Icons.Material.Filled.AccountBalance" AdornmentColor="Color.Info" Adornment="Adornment.Start" Validation="@this.ValidatingProvider">
|
||||||
|
@foreach (Providers provider in Enum.GetValues(typeof(Providers)))
|
||||||
|
{
|
||||||
|
<MudSelectItem Value="@provider">@provider</MudSelectItem>
|
||||||
|
}
|
||||||
|
</MudSelect>
|
||||||
|
</MudForm>
|
||||||
|
|
||||||
|
@if (this.dataIssues.Any())
|
||||||
|
{
|
||||||
|
<MudPaper Class="pa-2 mt-3">
|
||||||
|
<MudText Typo="Typo.h6">Issues</MudText>
|
||||||
|
<MudList Clickable="@true">
|
||||||
|
@foreach (var issue in this.dataIssues)
|
||||||
|
{
|
||||||
|
<MudListItem Icon="@Icons.Material.Filled.Error" IconColor="Color.Error">
|
||||||
|
@issue
|
||||||
|
</MudListItem>
|
||||||
|
}
|
||||||
|
</MudList>
|
||||||
|
</MudPaper>
|
||||||
|
}
|
||||||
|
</DialogContent>
|
||||||
|
<DialogActions>
|
||||||
|
<MudButton OnClick="@this.Cancel">Cancel</MudButton>
|
||||||
|
<MudButton OnClick="@this.Add" Color="Color.Primary">Add</MudButton>
|
||||||
|
</DialogActions>
|
||||||
|
</MudDialog>
|
46
app/MindWork AI Studio/Settings/ProviderDialog.razor.cs
Normal file
46
app/MindWork AI Studio/Settings/ProviderDialog.razor.cs
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
using AIStudio.Provider;
|
||||||
|
|
||||||
|
using Microsoft.AspNetCore.Components;
|
||||||
|
|
||||||
|
using MudBlazor;
|
||||||
|
|
||||||
|
namespace AIStudio.Settings;
|
||||||
|
|
||||||
|
public partial class ProviderDialog : ComponentBase
|
||||||
|
{
|
||||||
|
[CascadingParameter]
|
||||||
|
private MudDialogInstance MudDialog { get; set; } = null!;
|
||||||
|
|
||||||
|
private bool dataIsValid;
|
||||||
|
private string[] dataIssues = [];
|
||||||
|
private string dataInstanceName = string.Empty;
|
||||||
|
private Providers dataProvider = Providers.NONE;
|
||||||
|
|
||||||
|
private MudForm form = null!;
|
||||||
|
|
||||||
|
private async Task Add()
|
||||||
|
{
|
||||||
|
await this.form.Validate();
|
||||||
|
if (!this.dataIsValid)
|
||||||
|
return;
|
||||||
|
|
||||||
|
var addedProvider = new Provider
|
||||||
|
{
|
||||||
|
Id = Guid.NewGuid().ToString(),
|
||||||
|
InstanceName = this.dataInstanceName,
|
||||||
|
UsedProvider = this.dataProvider,
|
||||||
|
};
|
||||||
|
|
||||||
|
this.MudDialog.Close(DialogResult.Ok(addedProvider));
|
||||||
|
}
|
||||||
|
|
||||||
|
private string? ValidatingProvider(Providers provider)
|
||||||
|
{
|
||||||
|
if (provider == Providers.NONE)
|
||||||
|
return "Please select a provider.";
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void Cancel() => this.MudDialog.Cancel();
|
||||||
|
}
|
7
app/MindWork AI Studio/Settings/Version.cs
Normal file
7
app/MindWork AI Studio/Settings/Version.cs
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
namespace AIStudio.Settings;
|
||||||
|
|
||||||
|
public enum Version
|
||||||
|
{
|
||||||
|
UNKNOWN,
|
||||||
|
V1,
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user