@namespace Heimdall.Web.Components.Settings @inject HeimdallApiClient Api @inject ISnackbar Snackbar @inject Heimdall.Web.Services.UiText Text
@Text["AiSettingsSection_Title"] @Text["AiSettingsSection_Detection"]
@Text["AiSettingsSection_Description"]
@(_testing ? Text["AiSettingsSection_Testing"] : Text["AiSettingsSection_Test"])
@if (_testResult is not null) { @_testResult.Status — @_testResult.Message }
@code { private List _settings = []; private bool _testing; private TestActionResult? _testResult; protected override async Task OnInitializedAsync() { try { _settings = await Api.GetSettingsAsync(); } catch { _settings = []; } } private string? GetValue(string key) => _settings.FirstOrDefault(s => s.Key == key)?.Value; private Task OnCpaiUrlChanged(string v) => SaveValueAsync("ai.cpaiurl", v); private Task OnOllamaUrlChanged(string v) => SaveValueAsync("ai.ollamaurl", v); private Task OnDetectionEnabledChanged(bool v) => SaveValueAsync("ai.detection.enabled", v ? "true" : "false"); private async Task TestAiAsync() { _testing = true; _testResult = null; try { _testResult = await Api.TestAiAsync() ?? new TestActionResult(false, Text["AiSettingsSection_TestUnavailable"]); } catch (Exception ex) { _testResult = new TestActionResult(false, string.Format(Text["AiSettingsSection_TestFailed"], ex.Message)); } finally { _testing = false; } } private async Task SaveValueAsync(string key, string? value) { value ??= string.Empty; var existing = _settings.FirstOrDefault(s => s.Key == key); try { if (existing is null) { await Api.CreateSettingAsync(new CreateAppSettingRequest( Key: key, Value: value, Category: "ai", Description: key, ValueType: "string", AllowedValues: null, AgentScoped: true)); } else { await Api.UpdateSettingAsync(key, new UpdateAppSettingRequest( Value: value, Description: existing.Description ?? key, Category: "ai", ValueType: "string", AllowedValues: null, AgentScoped: null)); } _settings = await Api.GetSettingsAsync(); Snackbar.Add(string.Format(Text["AiSettingsSection_SettingUpdated"], key), Severity.Success); } catch (Exception ex) { Snackbar.Add(string.Format(Text["AiSettingsSection_SettingUpdateFailed"], ex.Message), Severity.Error); } } }