2026-02-01 13:50:19 +00:00
using AIStudio.Dialogs.Settings ;
2026-04-09 08:01:24 +00:00
using AIStudio.Settings.DataModel ;
2026-07-03 13:02:20 +00:00
using AIStudio.Tools.AssistantSessions ;
2024-07-14 19:46:17 +00:00
using Microsoft.AspNetCore.Components ;
2025-03-12 10:31:01 +00:00
using DialogOptions = AIStudio . Dialogs . DialogOptions ;
2024-08-21 06:30:01 +00:00
namespace AIStudio.Components ;
2024-07-14 19:46:17 +00:00
2025-04-27 07:06:05 +00:00
public partial class AssistantBlock < TSettings > : MSGComponentBase where TSettings : IComponent
2024-07-14 19:46:17 +00:00
{
2026-07-03 13:02:20 +00:00
/// <summary>
/// Describes the assistant session indicator shown on top of the assistant icon.
/// </summary>
/// <param name="Icon">The icon that communicates the session status.</param>
/// <param name="Color">The color that communicates the session status.</param>
/// <param name="Tooltip">The tooltip text that explains the session status.</param>
private sealed record AssistantSessionIndicatorData ( string Icon , Color Color , string Tooltip ) ;
2024-07-14 19:46:17 +00:00
[Parameter]
public string Name { get ; set ; } = string . Empty ;
2026-01-12 19:43:45 +00:00
2024-07-14 19:46:17 +00:00
[Parameter]
public string Description { get ; set ; } = string . Empty ;
2026-01-12 19:43:45 +00:00
2024-07-14 19:46:17 +00:00
[Parameter]
public string Icon { get ; set ; } = Icons . Material . Filled . DisabledByDefault ;
2026-01-12 19:43:45 +00:00
2024-07-14 19:46:17 +00:00
[Parameter]
public string ButtonText { get ; set ; } = "Start" ;
2026-01-12 19:43:45 +00:00
2024-07-14 19:46:17 +00:00
[Parameter]
public string Link { get ; set ; } = string . Empty ;
2026-01-12 19:43:45 +00:00
2026-07-05 13:20:29 +00:00
[Parameter]
public EventCallback OnClick { get ; set ; }
2026-04-09 08:01:24 +00:00
[Parameter]
public bool Disabled { get ; set ; }
[Parameter]
public RenderFragment ? SecurityBadge { get ; set ; }
2026-01-12 19:43:45 +00:00
[Parameter]
public Tools . Components Component { get ; set ; } = Tools . Components . NONE ;
2026-07-03 13:02:20 +00:00
/// <summary>
/// Gets or sets the optional assistant session instance ID represented by this block.
/// </summary>
[Parameter]
public string AssistantSessionInstanceId { get ; set ; } = string . Empty ;
2026-01-12 19:43:45 +00:00
[Parameter]
public PreviewFeatures RequiredPreviewFeature { get ; set ; } = PreviewFeatures . NONE ;
2024-09-15 10:30:07 +00:00
[Inject]
private MudTheme ColorTheme { get ; init ; } = null ! ;
2026-01-12 19:43:45 +00:00
2025-03-12 10:31:01 +00:00
[Inject]
private IDialogService DialogService { get ; init ; } = null ! ;
2026-07-03 13:02:20 +00:00
[Inject]
private AssistantSessionService AssistantSessionService { get ; init ; } = null ! ;
2025-03-12 10:31:01 +00:00
private async Task OpenSettingsDialog ( )
{
2026-02-01 13:50:19 +00:00
if ( ! this . HasSettingsPanel )
return ;
2025-03-12 10:31:01 +00:00
var dialogParameters = new DialogParameters ( ) ;
2026-01-12 19:43:45 +00:00
2025-04-27 07:06:05 +00:00
await this . DialogService . ShowAsync < TSettings > ( T ( "Open Settings" ) , dialogParameters , DialogOptions . FULLSCREEN ) ;
2024-09-15 10:30:07 +00:00
}
2026-01-12 19:43:45 +00:00
2026-07-03 13:02:20 +00:00
private string BorderColor = > this . AssistantSessionSnapshot ? . IsActive is true ? this . ColorTheme . GetActivityIndicatorColor ( this . SettingsManager ) : this . SettingsManager . IsDarkMode switch
2024-09-15 10:30:07 +00:00
{
2026-07-03 13:02:20 +00:00
true = > this . ColorTheme . GetCurrentPalette ( this . SettingsManager ) . GrayDefault ,
false = > this . ColorTheme . GetCurrentPalette ( this . SettingsManager ) . GrayDefault ,
2024-09-15 10:30:07 +00:00
} ;
2026-07-03 13:02:20 +00:00
private string BlockStyle = > $"border-width: 3px; border-color: {this.BorderColor}; border-radius: 12px; border-style: solid; max-width: 20em;" ;
2026-01-12 19:43:45 +00:00
private bool IsVisible = > this . SettingsManager . IsAssistantVisible ( this . Component , assistantName : this . Name , requiredPreviewFeature : this . RequiredPreviewFeature ) ;
2026-02-01 13:50:19 +00:00
private bool HasSettingsPanel = > typeof ( TSettings ) ! = typeof ( NoSettingsPanel ) ;
2026-07-03 13:02:20 +00:00
2026-07-05 13:20:29 +00:00
private bool HasStartAction = > this . OnClick . HasDelegate ;
2026-07-03 13:02:20 +00:00
/// <summary>
/// Gets the newest assistant session snapshot represented by this block.
/// </summary>
private AssistantSessionSnapshot ? AssistantSessionSnapshot = > string . IsNullOrWhiteSpace ( this . AssistantSessionInstanceId )
? this . AssistantSessionService . GetSnapshots ( ) . FirstOrDefault ( snapshot = > snapshot . Key . Component = = this . Component )
: this . AssistantSessionService . GetSnapshots ( ) . FirstOrDefault ( snapshot = > snapshot . Key . InstanceId = = this . AssistantSessionInstanceId ) ;
/// <summary>
/// Gets the assistant session indicator shown on top of the assistant icon.
/// </summary>
private AssistantSessionIndicatorData ? AssistantSessionIndicator = > this . AssistantSessionSnapshot ? . Status switch
{
AssistantSessionStatus . RUNNING or AssistantSessionStatus . CANCELING = > new ( Icons . Material . Filled . ChangeCircle , Color . Info , this . T ( "Assistant is still running." ) ) ,
AssistantSessionStatus . COMPLETED = > new ( Icons . Material . Filled . TaskAlt , Color . Success , this . T ( "The result is ready." ) ) ,
AssistantSessionStatus . FAILED = > new ( Icons . Material . Filled . Error , Color . Error , this . T ( "Assistant failed. Open it to review the result." ) ) ,
AssistantSessionStatus . CANCELED = > new ( Icons . Material . Filled . Cancel , Color . Warning , this . T ( "Assistant was canceled. Open it to review the result." ) ) ,
_ = > null ,
} ;
/// <summary>
/// Refreshes the block when assistant session activity changes.
/// </summary>
/// <typeparam name="T">The message payload type.</typeparam>
/// <param name="sendingComponent">The component that sent the message, if any.</param>
/// <param name="triggeredEvent">The event that was triggered.</param>
/// <param name="data">The message payload.</param>
/// <returns>A task that completes after the message was processed.</returns>
protected override Task ProcessIncomingMessage < T > ( ComponentBase ? sendingComponent , Event triggeredEvent , T ? data ) where T : default
{
if ( triggeredEvent is Event . ASSISTANT_SESSION_CHANGED or Event . ASSISTANT_SESSION_FINISHED )
this . StateHasChanged ( ) ;
return base . ProcessIncomingMessage ( sendingComponent , triggeredEvent , data ) ;
}
}