Refactor AttachDocuments and ChatComponent to use FileAttachment type for improved handling and consistency

This commit is contained in:
Thorsten Sommer 2025-12-28 16:02:55 +01:00
parent 69c3e490d7
commit 26d963de23
Signed by: tsommer
GPG Key ID: 371BBA77A02C0108
3 changed files with 20 additions and 19 deletions

View File

@ -68,9 +68,9 @@ else
</MudStack>
<div @onmouseenter="@this.OnMouseEnter" @onmouseleave="@this.OnMouseLeave">
<MudPaper Height="20em" Outlined="true" Class="@this.dragClass" Style="overflow-y: auto;">
@foreach (var filePath in this.DocumentPaths)
@foreach (var fileAttachment in this.DocumentPaths)
{
<MudChip T="string" Color="Color.Dark" Text="@Path.GetFileName(filePath)" tabindex="-1" Icon="@Icons.Material.Filled.Search" OnClick="@(() => this.InvestigateFile(filePath))" OnClose="@(() => this.RemoveDocument(filePath))"/>
<MudChip T="string" Color="Color.Dark" Text="@fileAttachment.FileName" tabindex="-1" Icon="@Icons.Material.Filled.Search" OnClick="@(() => this.InvestigateFile(fileAttachment))" OnClose="@(() => this.RemoveDocument(fileAttachment))"/>
}
</MudPaper>
</div>

View File

@ -1,3 +1,4 @@
using AIStudio.Chat;
using AIStudio.Dialogs;
using AIStudio.Tools.PluginSystem;
using AIStudio.Tools.Rust;
@ -16,15 +17,15 @@ public partial class AttachDocuments : MSGComponentBase
[Parameter]
public string Name { get; set; } = string.Empty;
[Parameter]
public HashSet<string> DocumentPaths { get; set; } = [];
public HashSet<FileAttachment> DocumentPaths { get; set; } = [];
[Parameter]
public EventCallback<HashSet<string>> DocumentPathsChanged { get; set; }
public EventCallback<HashSet<FileAttachment>> DocumentPathsChanged { get; set; }
[Parameter]
public Func<HashSet<string>, Task> OnChange { get; set; } = _ => Task.CompletedTask;
public Func<HashSet<FileAttachment>, 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.
@ -116,7 +117,7 @@ public partial class AttachDocuments : MSGComponentBase
if(!await FileExtensionValidation.IsExtensionValidWithNotifyAsync(path))
continue;
this.DocumentPaths.Add(path);
this.DocumentPaths.Add(FileAttachment.FromPath(path));
}
await this.DocumentPathsChanged.InvokeAsync(this.DocumentPaths);
@ -160,7 +161,7 @@ public partial class AttachDocuments : MSGComponentBase
if (!await FileExtensionValidation.IsExtensionValidWithNotifyAsync(selectedFilePath))
continue;
this.DocumentPaths.Add(selectedFilePath);
this.DocumentPaths.Add(FileAttachment.FromPath(selectedFilePath));
}
await this.DocumentPathsChanged.InvokeAsync(this.DocumentPaths);
@ -199,23 +200,23 @@ public partial class AttachDocuments : MSGComponentBase
this.StateHasChanged();
}
private async Task RemoveDocument(string filePath)
private async Task RemoveDocument(FileAttachment fileAttachment)
{
this.DocumentPaths.Remove(filePath);
this.DocumentPaths.Remove(fileAttachment);
await this.DocumentPathsChanged.InvokeAsync(this.DocumentPaths);
await this.OnChange(this.DocumentPaths);
}
/// <summary>
/// The user might want to check what we actually extract from his file and therefore give the LLM as an input.
/// The user might want to check what we actually extract from his file and therefore give the LLM as an input.
/// </summary>
/// <param name="filePath">The file to check.</param>
private async Task InvestigateFile(string filePath)
/// <param name="fileAttachment">The file to check.</param>
private async Task InvestigateFile(FileAttachment fileAttachment)
{
var dialogParameters = new DialogParameters<DocumentCheckDialog>
{
{ x => x.FilePath, filePath },
{ x => x.FilePath, fileAttachment.FilePath },
};
await this.DialogService.ShowAsync<DocumentCheckDialog>(T("Document Preview"), dialogParameters, DialogOptions.FULLSCREEN);

View File

@ -57,7 +57,7 @@ public partial class ChatComponent : MSGComponentBase, IAsyncDisposable
private string currentWorkspaceName = string.Empty;
private Guid currentWorkspaceId = Guid.Empty;
private CancellationTokenSource? cancellationTokenSource;
private HashSet<string> chatDocumentPaths = [];
private HashSet<FileAttachment> chatDocumentPaths = [];
// Unfortunately, we need the input field reference to blur the focus away. Without
// this, we cannot clear the input field.
@ -464,7 +464,7 @@ public partial class ChatComponent : MSGComponentBase, IAsyncDisposable
lastUserPrompt = new ContentText
{
Text = this.userInput,
FileAttachments = this.chatDocumentPaths.ToList(),
FileAttachments = [..this.chatDocumentPaths],
};
//