2026-11-27 16:35:51 +01:00
using AIStudio.AIOperations ;
using AIStudio.AIOperations.ConversationSummary ;
2026-11-21 14:16:04 +01:00
using AIStudio.Chat ;
using AIStudio.Chat.Compression ;
using AIStudio.Components ;
2026-11-27 16:35:51 +01:00
using AIStudio.Settings ;
using AIStudio.Tools.PluginSystem ;
2026-11-21 14:16:04 +01:00
using Microsoft.AspNetCore.Components ;
using Microsoft.AspNetCore.Components.Web ;
namespace AIStudio.Dialogs ;
/// <summary>
/// Asks the AI for a summary of a chat, for continuing the conversation in a new chat.
/// </summary>
/// <remarks>
/// The dialog only writes the summary. Creating the new chat and switching to it is left to the
/// chat, which knows what is on the screen, see ChatComponent.CompressCurrentChat. Canceling while
/// the AI writes stops the request, and nothing changes.
/// </remarks>
public partial class ChatCompressionDialog : MSGComponentBase
{
[CascadingParameter]
private IMudDialogInstance MudDialog { get ; set ; } = null !;
2026-11-27 16:35:51 +01:00
[Inject]
private ConversationSummaryOperation ConversationSummary { get ; init ; } = null !;
2026-11-21 14:16:04 +01:00
[Inject]
private ChatCompressionService ChatCompressionService { get ; init ; } = null !;
2026-11-27 16:35:51 +01:00
[Inject]
private AIOperationProviderResolver ProviderResolver { get ; init ; } = null !;
2026-11-21 14:16:04 +01:00
/// <summary>
/// The chat to compress.
/// </summary>
[Parameter]
public ChatThread SourceChat { get ; set ; } = null !;
/// <summary>
/// Which messages the summary replaces, see ChatCompressionPlan.Of.
/// </summary>
[Parameter]
public ChatCompressionPlan Plan { get ; set ; } = null !;
/// <summary>
2026-11-27 16:35:51 +01:00
/// The provider of the chat: the one it would send its next message to, and which continues with the summary.
2026-11-21 14:16:04 +01:00
/// </summary>
2026-11-27 16:35:51 +01:00
/// <remarks>
/// It writes the summary as well, unless the settings pick another model for compressing which may see the
/// chat, see ChatCompressionService.GetProviderOptions.
/// </remarks>
2026-11-21 14:16:04 +01:00
[Parameter]
2026-11-27 16:35:51 +01:00
public AIStudio . Settings . Provider ChatProvider { get ; set ; } = AIStudio . Settings . Provider . NONE ;
2026-11-21 14:16:04 +01:00
/// <summary>
/// Where the conversation goes on: in a new chat, which the user names here, or in the same chat.
/// </summary>
[Parameter]
public ChatCompressionTarget Target { get ; set ; } = ChatCompressionTarget . NEW_CHAT ;
/// <summary>
/// The name suggested for the new chat; not asked for when the conversation goes on in the same chat.
/// </summary>
[Parameter]
public string SuggestedName { get ; set ; } = string . Empty ;
/// <summary>
/// Whether the chat has unsaved changes, which it stores before the new chat opens.
/// </summary>
[Parameter]
public bool StoresChatFirst { get ; set ; }
2026-11-27 16:35:51 +01:00
/// <summary>
/// Whether the summary starts right away, with the suggested name and the preselected focus, since the settings say
/// to compress without asking, see ContextWindowFullHandling.Decide.
/// </summary>
/// <remarks>
/// The dialog stays, so the user sees the progress and may cancel. When the summary fails, the dialog shows why and
/// waits for the user, as it does otherwise.
/// </remarks>
[Parameter]
public bool StartsAutomatically { get ; set ; }
2026-11-21 14:16:04 +01:00
private static readonly Dictionary < string , object? > USER_INPUT_ATTRIBUTES = new ();
private readonly CancellationTokenSource cancellationTokenSource = new ();
private MudForm form = null !;
private string chatName = string . Empty ;
private string focus = string . Empty ;
private string issue = string . Empty ;
private bool isSummarizing ;
2026-11-27 16:35:51 +01:00
private ConversationSummaryProgress progress ;
private ChatCompressionOptions options = ChatCompressionOptions . DEFAULT ;
private AIOperationProviderChoice writer = new ( AIStudio . Settings . Provider . NONE , AIOperationProviderIssue . NOT_SET , AIOperationProviderIssue . NOT_SET );
2026-11-21 14:16:04 +01:00
#region Overrides of ComponentBase
protected override async Task OnInitializedAsync ()
{
// Configure the spellchecking for the user input:
this . SettingsManager . InjectSpellchecking ( USER_INPUT_ATTRIBUTES );
this . chatName = this . SuggestedName ;
2026-11-27 16:35:51 +01:00
// Read once, and again only when the user changes them through the gear, see ApplyChangedSettings:
this . options = ChatCompressionOptions . Of ( this . SettingsManager . ConfigurationData . ChatCompression );
this . focus = this . options . PreselectedFocus ;
this . writer = this . ChatCompressionService . ChooseWriter ( this . SourceChat , this . ChatProvider );
2026-11-21 14:16:04 +01:00
await base . OnInitializedAsync ();
}
2026-11-27 16:35:51 +01:00
protected override async Task OnAfterRenderAsync ( bool firstRender )
{
await base . OnAfterRenderAsync ( firstRender );
// The form exists only after the first render, and CompressAsync validates it:
if ( firstRender && this . StartsAutomatically )
await this . CompressAsync ();
}
2026-11-21 14:16:04 +01:00
#endregion
private bool HasDocumentsToSummarize => this . Plan . ToSummarize . Any ( block => block . Content ?. FileAttachments . MayContainDocuments () is true );
private bool HasImagesToSummarize => this . Plan . ToSummarize . Any ( block => block . Content ?. FileAttachments . ContainsImages () is true );
private string SummaryScope => this . Plan . ToSummarize . Count is 1
2026-11-27 16:35:51 +01:00
? string . Format ( T ( "'{0}' summarizes 1 message." ), this . writer . Provider . InstanceName )
: string . Format ( T ( "'{0}' summarizes {1} messages." ), this . writer . Provider . InstanceName , this . Plan . ToSummarize . Count );
/// <summary>
/// Why the model of the chat writes the summary instead of the model picked for compressing, or an empty string when it does not.
/// </summary>
/// <remarks>
/// Told from the reason the choice reports, never guessed afterward, see AIOperationProviderIssue. Without a model
/// picked for compressing, the model of the chat writes the summary as intended, and there is nothing to tell.
/// </remarks>
private string FallbackNotice => ! this . writer . UsesFallback ? string . Empty : this . writer . PreferredIssue switch
{
AIOperationProviderIssue . NOT_SET => string . Empty ,
AIOperationProviderIssue . MISSING => T ( "The model picked for compressing chats no longer exists, so the model of this chat writes the summary." ),
AIOperationProviderIssue . PROHIBITED => TA ( "Your organization does not allow the model picked for compressing chats, so the model of this chat writes the summary." ),
AIOperationProviderIssue . BELOW_MINIMUM_CONFIDENCE => T ( "The model picked for compressing chats does not reach the minimum confidence level, so the model of this chat writes the summary." ),
AIOperationProviderIssue . NOT_ALLOWED_FOR_CONTENT => T ( "This chat holds data which the model picked for compressing chats must not see, so the model of this chat writes the summary." ),
AIOperationProviderIssue . CONTEXT_WINDOW_TOO_SMALL => T ( "The context window of the model picked for compressing chats is too small, so the model of this chat writes the summary." ),
_ => T ( "The model picked for compressing chats cannot write this summary, so the model of this chat writes it." ),
};
2026-11-21 14:16:04 +01:00
private string ProgressMessage => this . progress . Step switch
{
2026-11-27 16:35:51 +01:00
ConversationSummaryStep . SUMMARIZING when this . progress . Total > 1 => string . Format ( T ( "The AI is summarizing part {0} of {1}..." ), this . progress . Current , this . progress . Total ),
ConversationSummaryStep . SUMMARIZING => T ( "The AI is summarizing the chat..." ),
ConversationSummaryStep . CONDENSING => T ( "The summary grew too long. The AI is shortening it..." ),
2026-11-21 14:16:04 +01:00
_ => T ( "AI Studio is reading the messages and their files..." ),
};
private string? ValidateChatName ( string? value ) => string . IsNullOrWhiteSpace ( value ) ? TA ( "Please enter a chat name." ) : null ;
2026-11-27 16:35:51 +01:00
/// <summary>
/// Reads the settings again once the user closed them through the gear.
/// </summary>
/// <remarks>
/// The model which writes the summary may have changed, and so may the preselected focus. A focus the user
/// wrote stays; one which is still the preselected focus follows the settings.
/// </remarks>
private void ApplyChangedSettings ()
{
var previousFocus = this . options . PreselectedFocus ;
this . options = ChatCompressionOptions . Of ( this . SettingsManager . ConfigurationData . ChatCompression );
if ( string . IsNullOrWhiteSpace ( this . focus ) || this . focus == previousFocus )
this . focus = this . options . PreselectedFocus ;
this . writer = this . ChatCompressionService . ChooseWriter ( this . SourceChat , this . ChatProvider );
this . StateHasChanged ();
}
2026-11-21 14:16:04 +01:00
private async Task HandleChatNameKeyDown ( KeyboardEventArgs keyEvent )
{
if ( keyEvent . IsPlainEnter )
await this . CompressAsync ();
}
private async Task CompressAsync ()
{
await this . form . ValidateAsync ();
if (! this . form . IsValid || this . isSummarizing )
return ;
this . isSummarizing = true ;
this . issue = string . Empty ;
this . progress = default ;
await this . InvokeAsync ( this . StateHasChanged );
2026-11-27 16:35:51 +01:00
var progressReporter = new Progress < ConversationSummaryProgress >( reported =>
2026-11-21 14:16:04 +01:00
{
this . progress = reported ;
this . InvokeAsync ( this . StateHasChanged ). Observe ( $"{nameof(ChatCompressionDialog)}: showing the progress of the summary" );
});
2026-11-27 16:35:51 +01:00
var request = new ConversationSummaryRequest ( this . SourceChat , this . Plan , this . focus , this . options . MaxSummaryTokens , this . ChatProvider . GetModelProfile (). Context , progressReporter );
// Chosen again right before running, since the settings may have changed while the dialog was open:
this . writer = this . ProviderResolver . Resolve ( this . ConversationSummary , request , ConversationSummaryOperation . GetProviderSettings ( this . SettingsManager ), ChatCompressionService . GetProviderOptions ( this . SourceChat , this . ChatProvider ));
if (! this . writer . HasProvider )
{
this . issue = string . Format ( TA ( "No model may write the summary of this chat. Please select a model with a context window of at least {0} tokens." ), ConversationSummaryBudget . MIN_CONTEXT_WINDOW . ToString ( "N0" , I18N . I . Culture ));
this . isSummarizing = false ;
await this . InvokeAsync ( this . StateHasChanged );
return ;
}
var result = await this . ConversationSummary . RunAsync ( request , new AIOperationContext ( this . writer . Provider ), this . cancellationTokenSource . Token );
2026-11-21 14:16:04 +01:00
// The user canceled, and the dialog is gone already:
if ( this . cancellationTokenSource . IsCancellationRequested )
return ;
if (! result . Success )
{
this . issue = result . Issue ;
this . isSummarizing = false ;
await this . InvokeAsync ( this . StateHasChanged );
return ;
}
this . MudDialog . Close ( DialogResult . Ok ( new ChatCompressionDialogResult ( this . chatName . Trim (), result . Summary )));
}
private void Cancel ()
{
this . cancellationTokenSource . Cancel ();
this . MudDialog . Cancel ();
}
#region Overrides of MSGComponentBase
protected override void DisposeResources ()
{
this . cancellationTokenSource . Cancel ();
this . cancellationTokenSource . Dispose ();
base . DisposeResources ();
}
#endregion
}