2025-11-24 11:37:18 +00:00
|
|
|
using AIStudio.Dialogs;
|
2025-12-18 14:00:27 +00:00
|
|
|
using AIStudio.Tools.PluginSystem;
|
2025-11-24 11:37:18 +00:00
|
|
|
using AIStudio.Tools.Rust;
|
|
|
|
|
using AIStudio.Tools.Services;
|
2025-12-15 12:26:55 +00:00
|
|
|
using AIStudio.Tools.Validation;
|
2025-11-24 11:37:18 +00:00
|
|
|
|
|
|
|
|
using Microsoft.AspNetCore.Components;
|
|
|
|
|
|
|
|
|
|
namespace AIStudio.Components;
|
|
|
|
|
|
2025-12-05 19:48:54 +00:00
|
|
|
using DialogOptions = Dialogs.DialogOptions;
|
2025-11-24 11:37:18 +00:00
|
|
|
|
|
|
|
|
public partial class AttachDocuments : MSGComponentBase
|
|
|
|
|
{
|
2025-12-18 14:00:27 +00:00
|
|
|
private static string TB(string fallbackEN) => I18N.I.T(fallbackEN, typeof(AttachDocuments).Namespace, nameof(AttachDocuments));
|
|
|
|
|
|
2025-11-24 11:37:18 +00:00
|
|
|
[Parameter]
|
|
|
|
|
public string Name { get; set; } = string.Empty;
|
|
|
|
|
|
|
|
|
|
[Parameter]
|
|
|
|
|
public HashSet<string> DocumentPaths { get; set; } = [];
|
|
|
|
|
|
|
|
|
|
[Parameter]
|
|
|
|
|
public EventCallback<HashSet<string>> DocumentPathsChanged { get; set; }
|
|
|
|
|
|
|
|
|
|
[Parameter]
|
|
|
|
|
public Func<HashSet<string>, Task> OnChange { get; set; } = _ => Task.CompletedTask;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Catch all documents that are hovered over the AI Studio window and not only over the drop zone.
|
|
|
|
|
/// </summary>
|
|
|
|
|
[Parameter]
|
|
|
|
|
public bool CatchAllDocuments { get; set; }
|
|
|
|
|
|
2025-12-10 12:48:13 +00:00
|
|
|
[Parameter]
|
|
|
|
|
public bool UseSmallForm { get; set; }
|
|
|
|
|
|
2025-11-24 11:37:18 +00:00
|
|
|
[Inject]
|
|
|
|
|
private ILogger<AttachDocuments> Logger { get; set; } = null!;
|
|
|
|
|
|
|
|
|
|
[Inject]
|
|
|
|
|
private RustService RustService { get; init; } = null!;
|
|
|
|
|
|
|
|
|
|
[Inject]
|
|
|
|
|
private IDialogService DialogService { get; init; } = null!;
|
2025-12-11 19:44:27 +00:00
|
|
|
|
|
|
|
|
[Inject]
|
|
|
|
|
private PandocAvailabilityService PandocAvailabilityService { get; init; } = null!;
|
|
|
|
|
|
2025-12-10 12:48:13 +00:00
|
|
|
private const Placement TOOLBAR_TOOLTIP_PLACEMENT = Placement.Top;
|
2025-12-18 14:00:27 +00:00
|
|
|
private static readonly string DROP_FILES_HERE_TEXT = TB("Drop files here to attach them.");
|
|
|
|
|
|
|
|
|
|
private bool isComponentHovered;
|
|
|
|
|
private bool isDraggingOver;
|
2025-12-10 12:48:13 +00:00
|
|
|
|
2025-11-24 11:37:18 +00:00
|
|
|
#region Overrides of MSGComponentBase
|
|
|
|
|
|
|
|
|
|
protected override async Task OnInitializedAsync()
|
|
|
|
|
{
|
|
|
|
|
this.ApplyFilters([], [ Event.TAURI_EVENT_RECEIVED ]);
|
|
|
|
|
await base.OnInitializedAsync();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override async Task ProcessIncomingMessage<T>(ComponentBase? sendingComponent, Event triggeredEvent, T? data) where T : default
|
|
|
|
|
{
|
|
|
|
|
switch (triggeredEvent)
|
|
|
|
|
{
|
|
|
|
|
case Event.TAURI_EVENT_RECEIVED when data is TauriEvent { EventType: TauriEventType.FILE_DROP_HOVERED }:
|
|
|
|
|
if(!this.isComponentHovered && !this.CatchAllDocuments)
|
|
|
|
|
{
|
|
|
|
|
this.Logger.LogDebug("Attach documents component '{Name}' is not hovered, ignoring file drop hovered event.", this.Name);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-18 14:00:27 +00:00
|
|
|
this.isDraggingOver = true;
|
2025-11-24 11:37:18 +00:00
|
|
|
this.SetDragClass();
|
|
|
|
|
this.StateHasChanged();
|
|
|
|
|
break;
|
|
|
|
|
|
2025-12-18 14:00:27 +00:00
|
|
|
case Event.TAURI_EVENT_RECEIVED when data is TauriEvent { EventType: TauriEventType.FILE_DROP_CANCELED }:
|
|
|
|
|
this.isDraggingOver = false;
|
|
|
|
|
this.StateHasChanged();
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case Event.TAURI_EVENT_RECEIVED when data is TauriEvent { EventType: TauriEventType.WINDOW_NOT_FOCUSED }:
|
|
|
|
|
this.isDraggingOver = false;
|
|
|
|
|
this.isComponentHovered = false;
|
|
|
|
|
this.ClearDragClass();
|
|
|
|
|
this.StateHasChanged();
|
|
|
|
|
break;
|
|
|
|
|
|
2025-11-24 11:37:18 +00:00
|
|
|
case Event.TAURI_EVENT_RECEIVED when data is TauriEvent { EventType: TauriEventType.FILE_DROP_DROPPED, Payload: var paths }:
|
|
|
|
|
if(!this.isComponentHovered && !this.CatchAllDocuments)
|
|
|
|
|
{
|
|
|
|
|
this.Logger.LogDebug("Attach documents component '{Name}' is not hovered, ignoring file drop dropped event.", this.Name);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2025-12-11 19:44:27 +00:00
|
|
|
|
|
|
|
|
// Ensure that Pandoc is installed and ready:
|
|
|
|
|
var pandocState = await this.PandocAvailabilityService.EnsureAvailabilityAsync(
|
|
|
|
|
showSuccessMessage: false,
|
|
|
|
|
showDialog: true);
|
|
|
|
|
|
|
|
|
|
// If Pandoc is not available (user cancelled installation), abort file drop:
|
|
|
|
|
if (!pandocState.IsAvailable)
|
|
|
|
|
{
|
2025-12-15 11:45:54 +00:00
|
|
|
this.Logger.LogWarning("The user cancelled the Pandoc installation or Pandoc is not available. Aborting file drop.");
|
2025-12-18 14:00:27 +00:00
|
|
|
this.isDraggingOver = false;
|
2025-12-11 19:44:27 +00:00
|
|
|
this.ClearDragClass();
|
|
|
|
|
this.StateHasChanged();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-24 11:37:18 +00:00
|
|
|
foreach (var path in paths)
|
|
|
|
|
{
|
2025-12-15 12:26:55 +00:00
|
|
|
if(!await FileExtensionValidation.IsExtensionValidWithNotifyAsync(path))
|
2025-11-24 11:37:18 +00:00
|
|
|
continue;
|
2025-12-11 19:44:27 +00:00
|
|
|
|
2025-11-24 11:37:18 +00:00
|
|
|
this.DocumentPaths.Add(path);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await this.DocumentPathsChanged.InvokeAsync(this.DocumentPaths);
|
|
|
|
|
await this.OnChange(this.DocumentPaths);
|
2025-12-18 14:00:27 +00:00
|
|
|
this.isDraggingOver = false;
|
|
|
|
|
this.ClearDragClass();
|
2025-11-24 11:37:18 +00:00
|
|
|
this.StateHasChanged();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
private const string DEFAULT_DRAG_CLASS = "relative rounded-lg border-2 border-dashed pa-4 mt-4 mud-width-full mud-height-full";
|
|
|
|
|
|
|
|
|
|
private string dragClass = DEFAULT_DRAG_CLASS;
|
|
|
|
|
|
|
|
|
|
private async Task AddFilesManually()
|
|
|
|
|
{
|
2025-12-11 19:44:27 +00:00
|
|
|
// Ensure that Pandoc is installed and ready:
|
|
|
|
|
var pandocState = await this.PandocAvailabilityService.EnsureAvailabilityAsync(
|
|
|
|
|
showSuccessMessage: false,
|
|
|
|
|
showDialog: true);
|
|
|
|
|
|
|
|
|
|
// If Pandoc is not available (user cancelled installation), abort file selection:
|
|
|
|
|
if (!pandocState.IsAvailable)
|
|
|
|
|
{
|
2025-12-15 11:45:54 +00:00
|
|
|
this.Logger.LogWarning("The user cancelled the Pandoc installation or Pandoc is not available. Aborting file selection.");
|
2025-12-11 19:44:27 +00:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-18 14:00:27 +00:00
|
|
|
var selectFiles = await this.RustService.SelectFiles(T("Select files to attach"));
|
2025-12-16 18:14:27 +00:00
|
|
|
if (selectFiles.UserCancelled)
|
2025-11-24 11:37:18 +00:00
|
|
|
return;
|
|
|
|
|
|
2025-12-16 18:14:27 +00:00
|
|
|
foreach (var selectedFilePath in selectFiles.SelectedFilePaths)
|
|
|
|
|
{
|
|
|
|
|
if (!File.Exists(selectedFilePath))
|
|
|
|
|
continue;
|
2025-11-24 11:37:18 +00:00
|
|
|
|
2025-12-16 18:14:27 +00:00
|
|
|
if (!await FileExtensionValidation.IsExtensionValidWithNotifyAsync(selectedFilePath))
|
2025-12-18 16:40:18 +00:00
|
|
|
continue;
|
2025-11-24 11:37:18 +00:00
|
|
|
|
2025-12-16 18:14:27 +00:00
|
|
|
this.DocumentPaths.Add(selectedFilePath);
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-24 11:37:18 +00:00
|
|
|
await this.DocumentPathsChanged.InvokeAsync(this.DocumentPaths);
|
|
|
|
|
await this.OnChange(this.DocumentPaths);
|
|
|
|
|
}
|
2025-12-18 14:00:27 +00:00
|
|
|
|
|
|
|
|
private async Task OpenAttachmentsDialog()
|
|
|
|
|
{
|
|
|
|
|
this.DocumentPaths = await ReviewAttachmentsDialog.OpenDialogAsync(this.DialogService, this.DocumentPaths);
|
|
|
|
|
}
|
2025-11-24 11:37:18 +00:00
|
|
|
|
|
|
|
|
private async Task ClearAllFiles()
|
|
|
|
|
{
|
|
|
|
|
this.DocumentPaths.Clear();
|
|
|
|
|
await this.DocumentPathsChanged.InvokeAsync(this.DocumentPaths);
|
|
|
|
|
await this.OnChange(this.DocumentPaths);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void SetDragClass() => this.dragClass = $"{DEFAULT_DRAG_CLASS} mud-border-primary border-4";
|
|
|
|
|
|
|
|
|
|
private void ClearDragClass() => this.dragClass = DEFAULT_DRAG_CLASS;
|
|
|
|
|
|
|
|
|
|
private void OnMouseEnter(EventArgs _)
|
|
|
|
|
{
|
|
|
|
|
this.Logger.LogDebug("Attach documents component '{Name}' is hovered.", this.Name);
|
|
|
|
|
this.isComponentHovered = true;
|
|
|
|
|
this.SetDragClass();
|
|
|
|
|
this.StateHasChanged();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnMouseLeave(EventArgs _)
|
|
|
|
|
{
|
|
|
|
|
this.Logger.LogDebug("Attach documents component '{Name}' is no longer hovered.", this.Name);
|
|
|
|
|
this.isComponentHovered = false;
|
|
|
|
|
this.ClearDragClass();
|
|
|
|
|
this.StateHasChanged();
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-18 16:40:18 +00:00
|
|
|
private async Task RemoveDocument(string filePath)
|
2025-11-24 11:37:18 +00:00
|
|
|
{
|
2025-12-18 16:40:18 +00:00
|
|
|
this.DocumentPaths.Remove(filePath);
|
2025-11-24 11:37:18 +00:00
|
|
|
|
|
|
|
|
await this.DocumentPathsChanged.InvokeAsync(this.DocumentPaths);
|
|
|
|
|
await this.OnChange(this.DocumentPaths);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2025-12-05 19:48:54 +00:00
|
|
|
/// The user might want to check what we actually extract from his file and therefore give the LLM as an input.
|
2025-11-24 11:37:18 +00:00
|
|
|
/// </summary>
|
2025-12-18 16:40:18 +00:00
|
|
|
/// <param name="filePath">The file to check.</param>
|
|
|
|
|
private async Task InvestigateFile(string filePath)
|
2025-11-24 11:37:18 +00:00
|
|
|
{
|
2025-12-05 19:48:54 +00:00
|
|
|
var dialogParameters = new DialogParameters<DocumentCheckDialog>
|
|
|
|
|
{
|
2025-12-18 16:40:18 +00:00
|
|
|
{ x => x.FilePath, filePath },
|
2025-12-05 19:48:54 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
await this.DialogService.ShowAsync<DocumentCheckDialog>(T("Document Preview"), dialogParameters, DialogOptions.FULLSCREEN);
|
2025-11-24 11:37:18 +00:00
|
|
|
}
|
|
|
|
|
}
|