Files
intendant/Intendant/Intendant.Shared/Pages/Settings.razor
T
2026-10-06 01:27:51 +02:00

119 lines
3.2 KiB
Plaintext

@page "/settings"
@using Intendant.Shared.Services
@inject IOpenAiSettingsService SettingsService
@inject IOpenAiResponsesService ResponsesService
<PageTitle>Paramètres</PageTitle>
<h1>Paramètres OpenAI</h1>
<div class="mb-3">
<label class="form-label" for="openai-api-key">Clé API (optionnelle)</label>
<input id="openai-api-key" class="form-control" @bind="apiKey" />
</div>
<div class="mb-3">
<label class="form-label" for="openai-endpoint">Endpoint</label>
<input id="openai-endpoint" class="form-control" @bind="endpoint" placeholder="https://api.openai.com/v1" />
</div>
<div class="mb-3">
<label class="form-label" for="openai-model">Modèle</label>
<input id="openai-model" class="form-control" @bind="model" placeholder="gpt-5-mini" />
</div>
<div class="d-flex gap-2">
<button type="button" class="btn btn-primary" @onclick="SaveAsync" disabled="@isBusy">Enregistrer</button>
<button type="button" class="btn btn-outline-secondary" @onclick="TestAsync" disabled="@isBusy">Tester</button>
</div>
@if (!string.IsNullOrWhiteSpace(message))
{
<div class="alert alert-success mt-3" role="alert">@message</div>
}
@if (!string.IsNullOrWhiteSpace(error))
{
<div class="alert alert-danger mt-3" role="alert">@error</div>
}
@if (!string.IsNullOrWhiteSpace(testResult))
{
<div class="alert alert-info mt-3" role="alert">@testResult</div>
}
@code {
private string apiKey = string.Empty;
private string endpoint = string.Empty;
private string model = string.Empty;
private string message = string.Empty;
private string error = string.Empty;
private string testResult = string.Empty;
private bool isBusy;
protected override async Task OnInitializedAsync()
{
OpenAiResponsesOptions settings = await SettingsService.GetAsync();
apiKey = settings.ApiKey;
endpoint = settings.Endpoint;
model = settings.Model;
}
private async Task SaveAsync()
{
isBusy = true;
error = string.Empty;
testResult = string.Empty;
try
{
await SettingsService.SaveAsync(new OpenAiResponsesOptions
{
ApiKey = apiKey,
Endpoint = endpoint,
Model = model,
});
message = "Paramètres enregistrés.";
}
catch (Exception ex)
{
message = string.Empty;
error = ex.Message;
}
finally
{
isBusy = false;
}
}
private async Task TestAsync()
{
isBusy = true;
error = string.Empty;
message = string.Empty;
testResult = string.Empty;
try
{
await SettingsService.SaveAsync(new OpenAiResponsesOptions
{
ApiKey = apiKey,
Endpoint = endpoint,
Model = model,
});
string response = await ResponsesService.GenerateTextAsync("Réponds uniquement par: OK");
testResult = response;
}
catch (Exception ex)
{
error = ex.Message;
}
finally
{
isBusy = false;
}
}
}