Added the "attach documents" component

This commit is contained in:
Thorsten Sommer 2025-10-27 15:17:20 +01:00
parent 0125215111
commit 211f8acbdd
Signed by: tsommer
GPG Key ID: 371BBA77A02C0108
5 changed files with 120 additions and 7 deletions

View File

@ -68,6 +68,6 @@ else
</ExpansionPanel>
<ExpansionPanel HeaderIcon="@Icons.Material.Filled.DocumentScanner" HeaderText="@(T("Document analysis") + $": {this.selectedPolicy?.PolicyName}")" IsExpanded="@(this.selectedPolicy?.IsProtected ?? false)">
TODO
<AttachDocuments @bind-DocumentPaths="@this.loadedDocumentPaths"/>
</ExpansionPanel>
</MudExpansionPanels>

View File

@ -33,7 +33,7 @@ public partial class DocumentAnalysisAssistant : AssistantBaseCore<SettingsDialo
get
{
var sb = new StringBuilder();
#warning TODO
#warning Add system prompt for document analysis
return sb.ToString();
}
}
@ -44,7 +44,7 @@ public partial class DocumentAnalysisAssistant : AssistantBaseCore<SettingsDialo
protected override bool ShowSendTo => true;
protected override string SubmitText => T("Analyze document");
protected override string SubmitText => T("Analyze documents");
protected override Func<Task> SubmitAction => this.Analyze;
@ -99,10 +99,9 @@ public partial class DocumentAnalysisAssistant : AssistantBaseCore<SettingsDialo
this.selectedPolicy = this.SettingsManager.ConfigurationData.DocumentAnalysis.Policies.First();
}
var deferredContent = MessageBus.INSTANCE.CheckDeferredMessages<string>(Event.SEND_TO_DOCUMENT_ANALYSIS_ASSISTANT).FirstOrDefault();
// if (deferredContent is not null)
#warning Add handling of deferred content -> load into input area
//this.
var receivedDeferredContent = MessageBus.INSTANCE.CheckDeferredMessages<string>(Event.SEND_TO_DOCUMENT_ANALYSIS_ASSISTANT).FirstOrDefault();
if (receivedDeferredContent is not null)
this.deferredContent = receivedDeferredContent;
await base.OnInitializedAsync();
}
@ -137,6 +136,8 @@ public partial class DocumentAnalysisAssistant : AssistantBaseCore<SettingsDialo
private string policyDescription = string.Empty;
private string policyAnalysisRules = string.Empty;
private string policyOutputRules = string.Empty;
private string deferredContent = string.Empty;
private List<string> loadedDocumentPaths = [];
private bool IsNoPolicySelectedOrProtected => this.selectedPolicy is null || this.selectedPolicy.IsProtected;

View File

@ -1459,6 +1459,15 @@ UI_TEXT_CONTENT["AISTUDIO::CHAT::CONTENTBLOCKCOMPONENT::T4188329028"] = "No, kee
-- Open Settings
UI_TEXT_CONTENT["AISTUDIO::COMPONENTS::ASSISTANTBLOCK::T1172211894"] = "Open Settings"
-- Images are not supported yet
UI_TEXT_CONTENT["AISTUDIO::COMPONENTS::ATTACHDOCUMENTS::T298062956"] = "Images are not supported yet"
-- Executables are not allowed
UI_TEXT_CONTENT["AISTUDIO::COMPONENTS::ATTACHDOCUMENTS::T4167762413"] = "Executables are not allowed"
-- Select a file to attach
UI_TEXT_CONTENT["AISTUDIO::COMPONENTS::ATTACHDOCUMENTS::T595772870"] = "Select a file to attach"
-- Changelog
UI_TEXT_CONTENT["AISTUDIO::COMPONENTS::CHANGELOG::T3017574265"] = "Changelog"

View File

@ -0,0 +1,15 @@
@inherits MSGComponentBase
<MudLink OnClick="@(() => this.AddFilesManually())" Style="text-decoration: none;">
<MudPaper Height="20em"
Outlined="true"
Class="@this.dragClass">
<MudText Typo="Typo.h6">
Drag and drop files here or click to attach documents.
</MudText>
@foreach (var fileInfo in this.DocumentPaths.Select(file => new FileInfo(file)))
{
<MudChip T="string" Color="Color.Dark" Text="@fileInfo.Name" tabindex="-1" />
}
</MudPaper>
</MudLink>

View File

@ -0,0 +1,88 @@
using AIStudio.Tools.Rust;
using AIStudio.Tools.Services;
using Microsoft.AspNetCore.Components;
namespace AIStudio.Components;
public partial class AttachDocuments : MSGComponentBase
{
[Parameter]
public List<string> DocumentPaths { get; set; } = [];
[Parameter]
public EventCallback<List<string>> DocumentPathsChanged { get; set; }
[Parameter]
public Func<List<string>, Task> OnChange { get; set; } = _ => Task.CompletedTask;
[Inject]
private RustService RustService { get; init; } = null!;
#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 }:
this.SetDragClass();
break;
case Event.TAURI_EVENT_RECEIVED when data is TauriEvent { EventType: TauriEventType.FILE_DROP_DROPPED, Payload: var paths }:
this.ClearDragClass();
foreach (var path in paths)
this.DocumentPaths.Add(path);
await this.DocumentPathsChanged.InvokeAsync(this.DocumentPaths);
await this.OnChange(this.DocumentPaths);
break;
case Event.TAURI_EVENT_RECEIVED when data is TauriEvent { EventType: TauriEventType.FILE_DROP_CANCELED }:
this.ClearDragClass();
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()
{
var selectedFile = await this.RustService.SelectFile(T("Select a file to attach"));
if (selectedFile.UserCancelled)
return;
if (!File.Exists(selectedFile.SelectedFilePath))
return;
var ext = Path.GetExtension(selectedFile.SelectedFilePath).TrimStart('.');
if (Array.Exists(FileTypeFilter.Executables.FilterExtensions, x => x.Equals(ext, StringComparison.OrdinalIgnoreCase)))
{
await MessageBus.INSTANCE.SendError(new(Icons.Material.Filled.AppBlocking, T("Executables are not allowed")));
return;
}
if (Array.Exists(FileTypeFilter.AllImages.FilterExtensions, x => x.Equals(ext, StringComparison.OrdinalIgnoreCase)))
{
await MessageBus.INSTANCE.SendWarning(new(Icons.Material.Filled.ImageNotSupported, T("Images are not supported yet")));
return;
}
this.DocumentPaths.Add(selectedFile.SelectedFilePath);
await this.DocumentPathsChanged.InvokeAsync(this.DocumentPaths);
await this.OnChange(this.DocumentPaths);
}
private void SetDragClass() => this.dragClass = $"{DEFAULT_DRAG_CLASS} mud-border-primary";
private void ClearDragClass() => this.dragClass = DEFAULT_DRAG_CLASS;
}