mirror of
https://github.com/MindWorkAI/AI-Studio.git
synced 2026-10-06 05:49:40 +00:00
Reduced memory usage and fixed several memory leaks (#933)
Build and Release / Determine run mode (push) Has been cancelled
Build and Release / Read metadata (push) Has been cancelled
Build and Release / Sync Flatpak repo (push) Has been cancelled
Build and Release / Collect Flatpak artifacts (push) Has been cancelled
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-aarch64-apple-darwin, osx-arm64, macos-latest, aarch64-apple-darwin, dmg,app,updater, dmg) (push) Has been cancelled
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-aarch64-pc-windows-msvc.exe, win-arm64, windows-latest, aarch64-pc-windows-msvc, nsis,updater, nsis) (push) Has been cancelled
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-aarch64-unknown-linux-gnu, linux-arm64, ubuntu-22.04-arm, aarch64-unknown-linux-gnu, appimage,updater, appimage) (push) Has been cancelled
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-x86_64-apple-darwin, osx-x64, macos-latest, x86_64-apple-darwin, dmg,app,updater, dmg) (push) Has been cancelled
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-x86_64-pc-windows-msvc.exe, win-x64, windows-latest, x86_64-pc-windows-msvc, nsis,updater, nsis) (push) Has been cancelled
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-x86_64-unknown-linux-gnu, linux-x64, ubuntu-22.04, x86_64-unknown-linux-gnu, appimage,updater, appimage) (push) Has been cancelled
Build and Release / Prepare & create release (push) Has been cancelled
Build and Release / Publish release (push) Has been cancelled
Build and Release / Determine run mode (push) Has been cancelled
Build and Release / Read metadata (push) Has been cancelled
Build and Release / Sync Flatpak repo (push) Has been cancelled
Build and Release / Collect Flatpak artifacts (push) Has been cancelled
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-aarch64-apple-darwin, osx-arm64, macos-latest, aarch64-apple-darwin, dmg,app,updater, dmg) (push) Has been cancelled
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-aarch64-pc-windows-msvc.exe, win-arm64, windows-latest, aarch64-pc-windows-msvc, nsis,updater, nsis) (push) Has been cancelled
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-aarch64-unknown-linux-gnu, linux-arm64, ubuntu-22.04-arm, aarch64-unknown-linux-gnu, appimage,updater, appimage) (push) Has been cancelled
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-x86_64-apple-darwin, osx-x64, macos-latest, x86_64-apple-darwin, dmg,app,updater, dmg) (push) Has been cancelled
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-x86_64-pc-windows-msvc.exe, win-x64, windows-latest, x86_64-pc-windows-msvc, nsis,updater, nsis) (push) Has been cancelled
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-x86_64-unknown-linux-gnu, linux-x64, ubuntu-22.04, x86_64-unknown-linux-gnu, appimage,updater, appimage) (push) Has been cancelled
Build and Release / Prepare & create release (push) Has been cancelled
Build and Release / Publish release (push) Has been cancelled
This commit is contained in:
1 parent
902a01a4d0
commit
7d9a4f5ab1
51 files changed
+531
-85
No files matched your search
@@ -21,11 +21,41 @@ public partial class DocumentCheckDialog : MSGComponentBase
|
||||
[Parameter]
|
||||
public string FileContent { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// How many characters we show at most. Rendering a huge document costs us a large Markdown
|
||||
/// syntax tree and an equally large render tree. This dialog answers the question of how we
|
||||
/// read the file, though — the beginning of the document is enough for that, and the AI still
|
||||
/// receives the entire content.
|
||||
/// </summary>
|
||||
private const int PREVIEW_CHARACTER_LIMIT = 200_000;
|
||||
|
||||
/// <summary>
|
||||
/// Set when reading the file failed, so the dialog shows the reason instead of empty content.
|
||||
/// </summary>
|
||||
private string? loadFailureMessage;
|
||||
|
||||
/// <summary>
|
||||
/// What we show to the user: either the entire file content, or its beginning. We keep this in
|
||||
/// its own field so that we cut the content only once, instead of on every render.
|
||||
/// </summary>
|
||||
private string previewContent = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// How many characters we cut off from the preview. Zero when we show the entire content.
|
||||
/// </summary>
|
||||
private int previewCutOffCharacters;
|
||||
|
||||
/// <summary>
|
||||
/// Ends the extraction when this dialog is gone before the file was read completely.
|
||||
/// </summary>
|
||||
private readonly CancellationTokenSource extractionCancellation = new();
|
||||
|
||||
/// <summary>
|
||||
/// True once this dialog was disposed. The extraction runs across awaits, so it may return
|
||||
/// long after the user closed the dialog — it must not touch this component afterwards.
|
||||
/// </summary>
|
||||
private bool isDisposed;
|
||||
|
||||
/// <summary>
|
||||
/// True while we extract the file content. Reading happens after the first render, so the
|
||||
/// dialog can tell the user that it is working instead of showing an empty document.
|
||||
@@ -54,6 +84,7 @@ public partial class DocumentCheckDialog : MSGComponentBase
|
||||
this.Document.Exists &&
|
||||
string.IsNullOrWhiteSpace(this.FileContent);
|
||||
|
||||
this.UpdatePreview();
|
||||
await base.OnInitializedAsync();
|
||||
}
|
||||
|
||||
@@ -66,7 +97,10 @@ public partial class DocumentCheckDialog : MSGComponentBase
|
||||
|
||||
try
|
||||
{
|
||||
var extraction = await UserFile.LoadFileData(this.Document.FilePath, this.RustService, this.DialogService);
|
||||
var extraction = await UserFile.LoadFileData(this.Document.FilePath, this.RustService, this.DialogService, this.extractionCancellation.Token);
|
||||
if (this.isDisposed)
|
||||
return;
|
||||
|
||||
this.FileContent = extraction.Content;
|
||||
|
||||
//
|
||||
@@ -76,6 +110,10 @@ public partial class DocumentCheckDialog : MSGComponentBase
|
||||
if (!extraction.HasUsableContent)
|
||||
this.loadFailureMessage = extraction.ToUserMessage(this.Document.FileName);
|
||||
}
|
||||
catch (OperationCanceledException)
|
||||
{
|
||||
// The user closed this dialog while we were reading the file. Nothing left to do.
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
this.Logger.LogError(ex, "Failed to load file content from '{FilePath}'", this.Document);
|
||||
@@ -84,14 +122,67 @@ public partial class DocumentCheckDialog : MSGComponentBase
|
||||
}
|
||||
finally
|
||||
{
|
||||
this.isLoadingContent = false;
|
||||
this.StateHasChanged();
|
||||
if (!this.isDisposed)
|
||||
{
|
||||
this.isLoadingContent = false;
|
||||
this.UpdatePreview();
|
||||
this.StateHasChanged();
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (firstRender)
|
||||
this.Logger.LogWarning("Document check dialog opened without a valid file path.");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Called when the user loads a file through this dialog. We don't use a two-way binding here,
|
||||
/// since we have to refresh the preview whenever the content changes.
|
||||
/// </summary>
|
||||
/// <param name="fileContent">The content of the file the user has loaded.</param>
|
||||
private void ApplyLoadedFileContent(string fileContent)
|
||||
{
|
||||
this.FileContent = fileContent;
|
||||
this.UpdatePreview();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Determines what part of the file content we show to the user.
|
||||
/// </summary>
|
||||
private void UpdatePreview()
|
||||
{
|
||||
if (this.FileContent.Length <= PREVIEW_CHARACTER_LIMIT)
|
||||
{
|
||||
this.previewContent = this.FileContent;
|
||||
this.previewCutOffCharacters = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
//
|
||||
// We cut at the last line break before our limit. Otherwise, we might tear apart a Markdown
|
||||
// construct like a table row or a code fence in the middle of a line:
|
||||
//
|
||||
var cutIndex = this.FileContent.LastIndexOf('\n', PREVIEW_CHARACTER_LIMIT - 1) + 1;
|
||||
if (cutIndex < 1)
|
||||
cutIndex = PREVIEW_CHARACTER_LIMIT;
|
||||
|
||||
this.previewContent = this.FileContent[..cutIndex];
|
||||
this.previewCutOffCharacters = this.FileContent.Length - cutIndex;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Ends a running extraction. Without this, reading a large document would continue after the
|
||||
/// user closed this dialog and would keep this component, the extracted content, and the
|
||||
/// response stream alive until the runtime is done.
|
||||
/// </summary>
|
||||
protected override void DisposeResources()
|
||||
{
|
||||
this.isDisposed = true;
|
||||
this.extractionCancellation.Cancel();
|
||||
this.extractionCancellation.Dispose();
|
||||
|
||||
base.DisposeResources();
|
||||
}
|
||||
|
||||
private CodeBlockTheme CodeColorPalette => this.SettingsManager.IsDarkMode ? CodeBlockTheme.Dark : CodeBlockTheme.Default;
|
||||
|
||||
private MudMarkdownStyling MarkdownStyling => new()
|
||||
|
||||
Reference in new issue
Block a user