2025-04-24 13:50:14 +02:00
using AIStudio.Components ;
2026-04-09 10:01:24 +02:00
using AIStudio.Agents.AssistantAudit ;
2026-11-20 11:47:59 +01:00
using AIStudio.Dialogs.Settings ;
2026-04-09 10:01:24 +02:00
using AIStudio.Tools.PluginSystem ;
using AIStudio.Tools.PluginSystem.Assistants ;
using Microsoft.AspNetCore.Components ;
2024-08-21 08:30:01 +02:00
2026-11-20 11:47:59 +01:00
using DialogOptions = AIStudio . Dialogs . DialogOptions ;
2024-08-21 08:30:01 +02:00
namespace AIStudio.Pages ;
2026-04-09 10:01:24 +02:00
public partial class Assistants : MSGComponentBase
{
2026-11-20 11:47:59 +01:00
private static readonly AssistantCategoryKey COMMON = new ( PluginCategory . COMMON );
private static readonly AssistantCategoryKey BUSINESS = new ( PluginCategory . BUSINESS );
private static readonly AssistantCategoryKey EDUCATION = new ( PluginCategory . EDUCATION );
private static readonly AssistantCategoryKey SOFTWARE_DEVELOPMENT = new ( PluginCategory . SOFTWARE_DEVELOPMENT );
private readonly AssistantCategoryRegistry categoryRegistry = new ();
2026-04-09 10:01:24 +02:00
private bool isAutoAuditing ;
[Inject]
private AssistantPluginAuditService AssistantPluginAuditService { get ; init ; } = null !;
2026-07-05 15:20:29 +02:00
[Inject]
2026-11-20 11:47:59 +01:00
private IDialogService DialogService { get ; init ; } = null !;
2026-07-05 15:20:29 +02:00
2026-04-09 10:01:24 +02:00
protected override async Task OnInitializedAsync ()
{
this . ApplyFilters ([], [ Event . CONFIGURATION_CHANGED , Event . PLUGINS_RELOADED ]);
2026-11-20 11:47:59 +01:00
this . categoryRegistry . ShowsAnyCategoryChanged += this . StateHasChanged ;
2026-04-09 10:01:24 +02:00
await base . OnInitializedAsync ();
}
2026-11-20 11:47:59 +01:00
/// <inheritdoc />
protected override void DisposeResources ()
{
this . categoryRegistry . ShowsAnyCategoryChanged -= this . StateHasChanged ;
base . DisposeResources ();
}
/// <summary>
/// Gets whether the user hid every category the page would show, so that it shows none.
/// </summary>
/// <remarks>
/// Without a hint, the page would look broken. When your organization hid or disabled every
/// assistant instead, the gear brings nothing back, so we keep quiet then.
/// </remarks>
private bool HidesEveryCategory
{
get
{
var page = this . SettingsManager . ConfigurationData . AssistantsPage ;
return this . categoryRegistry . ShowsAnyCategory is false && ( page . HiddenCategories . Count > 0 || page . HiddenCustomCategories . Count > 0 );
}
}
private async Task OpenSettingsDialogAsync ()
{
var categories = AssistantPageSections . SelectForSettings ( this . categoryRegistry . ShownCategories , this . SettingsManager . ConfigurationData . App , this . SettingsManager . ConfigurationData . AssistantsPage );
var dialogParameters = new DialogParameters < SettingsDialogAssistantsPage >
{
{ x => x . Categories , categories },
};
await this . DialogService . ShowAsync < SettingsDialogAssistantsPage >( T ( "Assistant Settings" ), dialogParameters , DialogOptions . FULLSCREEN );
}
2026-04-09 10:01:24 +02:00
protected override async Task OnAfterRenderAsync ( bool firstRender )
{
if ( firstRender )
await this . TryAutoAuditAssistantsAsync ();
}
private IReadOnlyCollection < PluginAssistants > AssistantPlugins =>
PluginFactory . RunningPlugins . OfType < PluginAssistants >()
2026-11-20 11:47:59 +01:00
. Where ( plugin => this . SettingsManager . IsAssistantPluginAvailable ( plugin ))
2026-04-09 10:01:24 +02:00
. ToList ();
2026-11-20 11:47:59 +01:00
/// <summary>
/// Groups the enabled assistant plugins into the sections of the page.
/// </summary>
/// <remarks>
/// The page asks for the sections once per rendering, so that the plugins and their order stay the
/// same within one rendering.
/// </remarks>
private IReadOnlyList < AssistantPageSection < PluginAssistants >> CreateSections () => AssistantPageSections . Build ( this . AssistantPlugins , plugin => plugin . AssistantTitle );
2026-04-09 10:01:24 +02:00
private async Task TryAutoAuditAssistantsAsync ()
{
if ( this . isAutoAuditing || ! this . SettingsManager . ConfigurationData . AssistantPluginAudit . AutomaticallyAuditAssistants )
return ;
this . isAutoAuditing = true ;
try
{
var wasConfigurationChanged = false ;
var assistantPlugins = PluginFactory . RunningPlugins . OfType < PluginAssistants >(). ToList ();
foreach ( var assistantPlugin in assistantPlugins )
{
var securityState = PluginAssistantSecurityResolver . Resolve ( this . SettingsManager , assistantPlugin );
if (! securityState . RequiresAudit )
continue ;
var audit = await this . AssistantPluginAuditService . RunAuditAsync ( assistantPlugin );
if ( audit . Level is AssistantAuditLevel . UNKNOWN )
{
2026-11-13 10:16:56 +01:00
await MessageBus . INSTANCE . SendError ( new ( AppIcons . AGENT , string . Format ( this . TA ( "The automatic security audit for the assistant plugin '{0}' failed. Please run it manually from the plugins page." ), assistantPlugin . Name )));
2026-04-09 10:01:24 +02:00
continue ;
}
this . UpsertAuditCard ( audit );
wasConfigurationChanged = true ;
}
if (! wasConfigurationChanged )
return ;
await this . SettingsManager . StoreSettings ();
await this . MessageBus . SendMessage < bool >( this , Event . CONFIGURATION_CHANGED );
}
finally
{
this . isAutoAuditing = false ;
await this . InvokeAsync ( this . StateHasChanged );
}
}
private void UpsertAuditCard ( PluginAssistantAudit audit )
{
var audits = this . SettingsManager . ConfigurationData . AssistantPluginAudits ;
var existingIndex = audits . FindIndex ( x => x . PluginId == audit . PluginId );
if ( existingIndex >= 0 )
audits [ existingIndex ] = audit ;
else
audits . Add ( audit );
}
protected override async Task ProcessIncomingMessage < T >( ComponentBase ? sendingComponent , Event triggeredEvent , T ? data ) where T : default
{
if ( triggeredEvent is Event . PLUGINS_RELOADED )
await this . TryAutoAuditAssistantsAsync ();
if ( triggeredEvent is Event . CONFIGURATION_CHANGED or Event . PLUGINS_RELOADED )
await this . InvokeAsync ( this . StateHasChanged );
}
}