2026-11-21 19:47:10 +01:00
using AIStudio.AIOperations ;
using AIStudio.AIOperations.TextContentCleaning ;
using AIStudio.Provider ;
2026-11-28 11:52:17 +01:00
using AIStudio.Tools.Imports ;
2026-09-04 15:48:07 +02:00
using AIStudio.Tools.Web ;
2024-08-01 21:53:28 +02:00
using Microsoft.AspNetCore.Components ;
2024-08-21 08:30:01 +02:00
namespace AIStudio.Components ;
2024-08-01 21:53:28 +02:00
2026-11-28 11:52:17 +01:00
/// <summary>
/// Loads the content of a web page into a text field of an assistant.
/// </summary>
/// <remarks>
/// The loading itself runs in WebContentImportService, so that it goes on while the user is elsewhere: this
/// reader goes away together with its assistant. The reader starts the import, shows its progress, and takes
/// its result, also when it is new and the import ran while the user was away.
/// </remarks>
2025-04-27 09:06:05 +02:00
public partial class ReadWebContent : MSGComponentBase
2024-08-01 21:53:28 +02:00
{
2026-09-04 15:48:07 +02:00
/// <summary>
2026-11-28 11:52:17 +01:00
/// The owner of the imports of a reader outside an assistant, which cascades no owner.
2026-09-04 15:48:07 +02:00
/// </summary>
2026-11-28 11:52:17 +01:00
private readonly ImportOwner fallbackImportOwner = new ( ImportOwnerKind . ASSISTANT , $"read-web-content:{Guid.NewGuid():N}" );
2026-09-04 15:48:07 +02:00
2024-08-01 21:53:28 +02:00
[Inject]
2026-11-28 11:52:17 +01:00
private WebContentImportService WebContentImportService { get ; init ; } = null !;
2026-11-21 19:47:10 +01:00
[Inject]
private AIOperationProviderResolver ProviderResolver { get ; init ; } = null !;
2024-08-05 21:12:52 +02:00
2026-11-28 11:52:17 +01:00
/// <summary>
/// The assistant around this reader, which owns its imports.
/// </summary>
[CascadingParameter]
private ImportOwner ? ImportOwner { get ; set ; }
/// <summary>
/// Tells the readers of one assistant apart, e.g., by the name of the component of an assistant plugin.
/// </summary>
/// <remarks>
/// An assistant with a single reader keeps the default.
/// </remarks>
[Parameter]
public string ImportTargetId { get ; set ; } = "primary" ;
2026-08-23 11:09:11 +02:00
2024-08-01 21:53:28 +02:00
[Parameter]
public string Content { get ; set ; } = string . Empty ;
[Parameter]
public EventCallback < string > ContentChanged { get ; set ; }
2026-09-13 16:24:36 +02:00
/// <summary>
/// The URL the content is loaded from.
/// </summary>
/// <remarks>
/// The URL belongs to the parent, so that it is cleared when the parent resets its form and
/// is kept when the parent stores its state.
/// </remarks>
[Parameter]
public string URL { get ; set ; } = string . Empty ;
[Parameter]
public EventCallback < string > URLChanged { get ; set ; }
2024-08-01 21:53:28 +02:00
[Parameter]
2025-08-26 10:59:56 +02:00
public AIStudio . Settings . Provider ProviderSettings { get ; set ; } = AIStudio . Settings . Provider . NONE ;
2024-08-01 21:53:28 +02:00
[Parameter]
2026-11-27 12:08:00 +01:00
public bool CleanerIsRunning { get ; set ; }
2024-08-01 21:53:28 +02:00
[Parameter]
2026-11-27 12:08:00 +01:00
public EventCallback < bool > CleanerIsRunningChanged { get ; set ; }
2024-08-01 21:53:28 +02:00
[Parameter]
public bool Preselect { get ; set ; }
2025-09-01 19:13:02 +02:00
[Parameter]
public EventCallback < bool > PreselectChanged { get ; set ; }
2024-08-01 21:53:28 +02:00
[Parameter]
2026-11-27 12:08:00 +01:00
public bool PreselectContentCleaner { get ; set ; }
2025-09-01 19:13:02 +02:00
[Parameter]
2026-11-27 12:08:00 +01:00
public EventCallback < bool > PreselectContentCleanerChanged { get ; set ; }
2024-08-01 21:53:28 +02:00
2025-08-26 10:59:56 +02:00
private readonly Process < ReadWebContentSteps > process = Process < ReadWebContentSteps >. INSTANCE ;
2024-08-01 21:53:28 +02:00
2026-09-19 21:39:16 +02:00
/// <summary>
2026-11-21 19:47:10 +01:00
/// The model the content cleaner would run with, and why others were not chosen.
2026-09-19 21:39:16 +02:00
/// </summary>
/// <remarks>
/// This is a resolved value, not a chosen one: the reader has no model selection of its own,
2026-11-21 19:47:10 +01:00
/// it takes what the assistant around it uses, unless a dedicated one for the cleaner takes
/// precedence, or the app-wide default fills in. Because the assistant's model can change at any
/// moment, this is resolved again on every render instead of being remembered from the first one.
2026-09-19 21:39:16 +02:00
/// </remarks>
2026-11-21 19:47:10 +01:00
private AIOperationProviderChoice cleanerChoice = new ( AIStudio . Settings . Provider . NONE , AIOperationProviderIssue . NOT_SET , AIOperationProviderIssue . NOT_SET );
2024-08-01 21:53:28 +02:00
#region Overrides of ComponentBase
protected override async Task OnInitializedAsync ()
{
2026-11-28 11:52:17 +01:00
this . WebContentImportService . StateChanged += this . OnImportStateChanged ;
2026-09-19 21:39:16 +02:00
this . ApplyFilters ([], [ Event . CONFIGURATION_CHANGED ]);
this . ResolveProvider ();
2024-08-01 21:53:28 +02:00
await base . OnInitializedAsync ();
2026-11-28 11:52:17 +01:00
await this . SyncWithImportAsync ();
2024-08-01 21:53:28 +02:00
}
protected override async Task OnParametersSetAsync ()
{
2026-09-19 21:39:16 +02:00
this . ResolveProvider ();
2024-08-01 21:53:28 +02:00
await base . OnParametersSetAsync ();
}
#endregion
2026-09-19 21:39:16 +02:00
#region Overrides of MSGComponentBase
protected override Task ProcessIncomingMessage < T >( ComponentBase ? sendingComponent , Event triggeredEvent , T ? data ) where T : default
{
if ( triggeredEvent is Event . CONFIGURATION_CHANGED )
{
//
// A dedicated model for the cleaner, or the app-wide default, may be set while this
// assistant is open. Nothing about that reaches us as a parameter, so without this the
// user would have to leave the assistant and come back for it to take effect.
//
this . ResolveProvider ();
this . StateHasChanged ();
}
return Task . CompletedTask ;
}
2026-11-28 11:52:17 +01:00
/// <summary>
/// Stops following the imports. An import keeps running, and its result waits for the next reader of this field.
/// </summary>
protected override void DisposeResources ()
{
this . WebContentImportService . StateChanged -= this . OnImportStateChanged ;
base . DisposeResources ();
}
2026-09-19 21:39:16 +02:00
#endregion
/// <summary>
/// Determines the model the content cleaner runs with.
/// </summary>
/// <remarks>
/// Called from both lifecycle methods, and with the same arguments: the assistant's model is a
/// parameter, and a parameter arrives whenever the parent renders. Resolving only once would
/// leave the cleaner with whatever was set the first time this component was built.
/// </remarks>
2026-11-28 11:52:17 +01:00
private void ResolveProvider () => this . cleanerChoice = this . ProviderResolver . Preview ( TextContentCleaningOperation . GetProviderSettings ( this . SettingsManager ), ConfidenceLevel . NONE , WebContentImportService . GetCleanerProviderOptions ( this . SettingsManager , this . ProviderSettings ));
private ImportOwner EffectiveImportOwner => this . ImportOwner ?? this . fallbackImportOwner ;
private ImportTarget EffectiveImportTarget => new ( this . EffectiveImportOwner , this . ImportTargetId );
2026-11-21 19:47:10 +01:00
/// <summary>
2026-11-28 11:52:17 +01:00
/// The import which fills this field, while it runs.
/// </summary>
private WebContentImportSnapshot ? CurrentImport => this . WebContentImportService . GetSnapshot ( this . EffectiveImportTarget );
/// <summary>
/// Whether the content of this field is being loaded. The controls of the reader wait for the whole import,
/// from loading the page to cleaning it.
/// </summary>
private bool IsImporting => this . CurrentImport is not null ;
private ProcessStepValue ProcessStep => this . process [ this . CurrentImport ?. Step ?? ReadWebContentSteps . START ];
private void LoadFromWeb ()
{
if (! this . IsReady || this . IsImporting )
return ;
this . WebContentImportService . TryStart ( this . EffectiveImportTarget , new ( this . URL , this . PreselectContentCleaner , this . ProviderSettings ));
}
private void OnImportStateChanged ( ImportOwner owner )
{
if ( owner == this . EffectiveImportOwner )
this . InvokeAsync ( async () =>
{
await this . SyncWithImportAsync ();
this . StateHasChanged ();
}). Observe ( $"{nameof(ReadWebContent)}: following the web content import" );
}
/// <summary>
/// Takes over what the import of this field reached: its options while it runs, its result once it ended, and
/// whether it cleans.
2026-11-21 19:47:10 +01:00
/// </summary>
/// <remarks>
2026-11-28 11:52:17 +01:00
/// This runs when the reader starts, too: the user may have left the assistant while the import ran. The
/// assistant restores its draft in OnInitializedAsync, before its first render creates this reader, so the
/// result lands on the draft and not under it, as the transcripts of ReadFileContent do. The assistant hears
/// about the cleaning only, as before, e.g., to lock its input field while the cleaner prepares the text
/// which replaces it.
2026-11-21 19:47:10 +01:00
/// </remarks>
2026-11-28 11:52:17 +01:00
private async Task SyncWithImportAsync ()
2026-11-21 19:47:10 +01:00
{
2026-11-28 11:52:17 +01:00
var result = this . WebContentImportService . TryTakeResult ( this . EffectiveImportTarget );
var currentImport = this . CurrentImport ;
if ( currentImport is not null )
await this . ShowRunningImportAsync ( currentImport );
2026-09-19 21:39:16 +02:00
2026-11-28 11:52:17 +01:00
var isCleaning = currentImport ?. Step is ReadWebContentSteps . CLEANING ;
if ( isCleaning != this . CleanerIsRunning )
{
this . CleanerIsRunning = isCleaning ;
await this . CleanerIsRunningChanged . InvokeAsync ( this . CleanerIsRunning );
}
if ( result is not null )
await this . ApplyResultAsync ( result );
}
/// <summary>
/// Shows the options with which the import of this field runs: the reader expanded, the address it loads,
/// and whether it cleans the page.
/// </summary>
/// <remarks>
/// An assistant which starts anew when the user comes back, see AssistantInputsWhenLeaving.DISCARD_INPUTS,
/// holds its preselected values instead. The reader would then stay collapsed, with its switch locked and
/// the progress of the import hidden. The options belong to the assistant, so they go there as well.
/// </remarks>
private async Task ShowRunningImportAsync ( WebContentImportSnapshot import )
2024-08-01 21:53:28 +02:00
{
2026-11-28 11:52:17 +01:00
if (! this . Preselect )
2024-08-01 21:53:28 +02:00
{
2026-11-28 11:52:17 +01:00
this . Preselect = true ;
await this . PreselectChanged . InvokeAsync ( this . Preselect );
}
2026-09-04 15:48:07 +02:00
2026-11-28 11:52:17 +01:00
if ( this . URL != import . Url )
{
this . URL = import . Url ;
await this . URLChanged . InvokeAsync ( this . URL );
2024-08-01 21:53:28 +02:00
}
2026-11-28 11:52:17 +01:00
if ( this . PreselectContentCleaner != import . CleanContent )
2024-08-01 21:53:28 +02:00
{
2026-11-28 11:52:17 +01:00
this . PreselectContentCleaner = import . CleanContent ;
await this . PreselectContentCleanerChanged . InvokeAsync ( this . PreselectContentCleaner );
}
}
private async Task ApplyResultAsync ( WebContentImportResult result )
{
switch ( result . Issue )
{
//
// Say that the cleaning did not happen. The user asked for it, the page arrives,
// and without a word they would take the raw markdown -- navigation, cookie banner
// and advertising included -- for the cleaned result.
//
case WebContentImportIssue . NOT_CLEANED_NO_MODEL :
await this . MessageBus . SendError ( new ( AppIcons . AI_OPERATION , T ( "The content was loaded, but not cleaned: no model is available for the content cleaner." )));
break ;
case WebContentImportIssue . NOT_CLEANED_NO_ANSWER :
await this . MessageBus . SendWarning ( new ( AppIcons . AI_OPERATION , T ( "The content was loaded, but not cleaned: the model of the content cleaner gave no answer." )));
break ;
2026-09-04 15:48:07 +02:00
//
// Say why nothing was loaded. An empty text field looks like a page without content,
// and the reasons a page cannot be read are things the user can act on: a link to a
// PDF rather than a page, a host that does not answer, a server refusing the request.
//
2026-11-28 11:52:17 +01:00
case WebContentImportIssue . LOAD_FAILED :
await this . MessageBus . SendError ( new ( AppIcons . PROVIDER_ERROR , string . Format ( T ( "The content of '{0}' could not be loaded: {1}" ), result . Url , result . ErrorMessage )));
break ;
2024-08-01 21:53:28 +02:00
}
2026-11-28 11:52:17 +01:00
this . Content = result . Markdown ;
2024-08-01 21:53:28 +02:00
await this . ContentChanged . InvokeAsync ( this . Content );
}
2026-09-19 21:39:16 +02:00
/// <summary>
/// Whether the content can be fetched.
/// </summary>
/// <remarks>
/// A missing model for the content cleaner is deliberately not part of this. Cleaning is an
/// option of the fetch, not a condition for it: making it one would leave the user with a
/// switch they turned on, no way to get their page, and a dead button to explain it. The page
/// is fetched, and LoadFromWeb says that it arrived uncleaned.
/// </remarks>
private bool IsReady => this . UrlIsValid ;
2024-08-01 21:53:28 +02:00
2026-09-13 16:24:36 +02:00
/// <summary>
/// Whether the current URL can be loaded.
/// </summary>
/// <remarks>
/// Asked of the current value instead of remembered from the last validation run: the parent
/// clears the URL when it resets its form, and the form validation does not run again at that
/// point. The fetch button would otherwise stay enabled with an empty field.
/// </remarks>
private bool UrlIsValid => this . ValidateURL ( this . URL ) is null ;
2026-10-31 10:48:20 +01:00
private async Task URLValueChanged ( string? url )
2026-09-13 16:24:36 +02:00
{
2026-10-31 10:48:20 +01:00
await this . URLChanged . InvokeAsync ( url ?? string . Empty );
2026-09-13 16:24:36 +02:00
}
2025-09-01 19:13:02 +02:00
private async Task ShowWebContentReaderChanged ( bool state )
{
await this . PreselectChanged . InvokeAsync ( state );
}
2026-11-27 12:08:00 +01:00
private async Task UseContentCleanerChanged ( bool state )
2025-09-01 19:13:02 +02:00
{
2026-11-27 12:08:00 +01:00
await this . PreselectContentCleanerChanged . InvokeAsync ( state );
2025-09-01 19:13:02 +02:00
}
2026-09-19 21:39:16 +02:00
/// <summary>
/// Says why the content cleaner has no model, or nothing when it has one.
/// </summary>
/// <remarks>
/// This is a hint, not a validation: the cleaner is an option of the reader, and an option
/// nobody can use yet must not keep the assistant around it from running. It is also stated
/// rather than remembered, so that choosing a model below makes it disappear at once.
2026-11-21 19:47:10 +01:00
/// The causes lead to different places, which is why they are told apart: no model was chosen
/// at all, the organization does not allow the chosen one, or it is not trusted enough for the
/// cleaner. The reason comes from the choice itself, where the first named fallback is the model
/// of the assistant, or the app-wide default when the assistant has none.
2026-09-19 21:39:16 +02:00
/// </remarks>
private string? ContentCleanerHint
2024-08-01 21:53:28 +02:00
{
2026-09-19 21:39:16 +02:00
get
2024-08-01 21:53:28 +02:00
{
2026-11-27 12:08:00 +01:00
if (! this . PreselectContentCleaner || this . cleanerChoice . HasProvider )
2026-09-19 21:39:16 +02:00
return null ;
2024-08-01 21:53:28 +02:00
2026-11-21 19:47:10 +01:00
return this . cleanerChoice . FallbackIssue switch
{
AIOperationProviderIssue . NOT_SET => TA ( "The content cleaner uses the model of this assistant. Please select one below." ),
AIOperationProviderIssue . PROHIBITED => TA ( "Your organization does not allow the selected model. Please select another model, or configure an eligible one in the app settings." ),
_ => TA ( "The selected model does not meet the confidence requirements of the content cleaner. Please select another model, or configure an eligible one in the app settings." ),
};
2026-09-19 21:39:16 +02:00
}
2024-08-01 21:53:28 +02:00
}
2026-09-19 21:39:16 +02:00
2024-08-01 21:53:28 +02:00
private string? ValidateURL ( string url )
{
if ( string . IsNullOrWhiteSpace ( url ))
2026-11-13 10:16:56 +01:00
return TA ( "Please provide a URL to load the content from." );
2024-08-01 21:53:28 +02:00
var urlParsingResult = Uri . TryCreate ( url , UriKind . Absolute , out var uriResult );
if (! urlParsingResult )
2026-11-13 10:16:56 +01:00
return TA ( "Please provide a valid URL." );
2024-08-01 21:53:28 +02:00
if ( uriResult is not { Scheme : "http" or "https" })
2026-11-13 10:16:56 +01:00
return TA ( "Please provide a valid HTTP or HTTPS URL." );
2024-08-01 21:53:28 +02:00
return null ;
}
}