mirror of
https://github.com/MindWorkAI/AI-Studio.git
synced 2026-06-07 03:56:35 +00:00
Improved the dialog for moving chats into workspaces (#796)
Some checks are pending
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) Blocked by required conditions
Some checks are pending
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) Blocked by required conditions
This commit is contained in:
parent
0b41f5eb96
commit
102b344557
@ -3253,6 +3253,9 @@ UI_TEXT_CONTENT["AISTUDIO::COMPONENTS::WORKSPACES::T3045856778"] = "Move Chat to
|
||||
-- Please enter a new or edit the name for your workspace '{0}':
|
||||
UI_TEXT_CONTENT["AISTUDIO::COMPONENTS::WORKSPACES::T323280982"] = "Please enter a new or edit the name for your workspace '{0}':"
|
||||
|
||||
-- There is already a workspace with this name. Please choose a different name.
|
||||
UI_TEXT_CONTENT["AISTUDIO::COMPONENTS::WORKSPACES::T3249036008"] = "There is already a workspace with this name. Please choose a different name."
|
||||
|
||||
-- Please enter a workspace name.
|
||||
UI_TEXT_CONTENT["AISTUDIO::COMPONENTS::WORKSPACES::T3288132732"] = "Please enter a workspace name."
|
||||
|
||||
@ -5794,6 +5797,21 @@ UI_TEXT_CONTENT["AISTUDIO::DIALOGS::UPDATEDIALOG::T25417398"] = "Update from v{0
|
||||
-- Install later
|
||||
UI_TEXT_CONTENT["AISTUDIO::DIALOGS::UPDATEDIALOG::T2936430090"] = "Install later"
|
||||
|
||||
-- Create new workspace
|
||||
UI_TEXT_CONTENT["AISTUDIO::DIALOGS::WORKSPACESELECTIONDIALOG::T1541251414"] = "Create new workspace"
|
||||
|
||||
-- Add workspace
|
||||
UI_TEXT_CONTENT["AISTUDIO::DIALOGS::WORKSPACESELECTIONDIALOG::T1586005241"] = "Add workspace"
|
||||
|
||||
-- Workspace name
|
||||
UI_TEXT_CONTENT["AISTUDIO::DIALOGS::WORKSPACESELECTIONDIALOG::T295876489"] = "Workspace name"
|
||||
|
||||
-- There is already a workspace with this name. Please choose a different name.
|
||||
UI_TEXT_CONTENT["AISTUDIO::DIALOGS::WORKSPACESELECTIONDIALOG::T3249036008"] = "There is already a workspace with this name. Please choose a different name."
|
||||
|
||||
-- Please enter a workspace name.
|
||||
UI_TEXT_CONTENT["AISTUDIO::DIALOGS::WORKSPACESELECTIONDIALOG::T3288132732"] = "Please enter a workspace name."
|
||||
|
||||
-- Cancel
|
||||
UI_TEXT_CONTENT["AISTUDIO::DIALOGS::WORKSPACESELECTIONDIALOG::T900713019"] = "Cancel"
|
||||
|
||||
|
||||
@ -101,7 +101,7 @@ public partial class ChatComponent : MSGComponentBase, IAsyncDisposable
|
||||
protected override async Task OnInitializedAsync()
|
||||
{
|
||||
// Apply the filters for the message bus:
|
||||
this.ApplyFilters([], [ Event.HAS_CHAT_UNSAVED_CHANGES, Event.RESET_CHAT_STATE, Event.CHAT_STREAMING_DONE, Event.AI_JOB_CHANGED, Event.AI_JOB_FINISHED, Event.CHAT_GENERATION_CHANGED ]);
|
||||
this.ApplyFilters([], [ Event.HAS_CHAT_UNSAVED_CHANGES, Event.RESET_CHAT_STATE, Event.CHAT_STREAMING_DONE, Event.AI_JOB_CHANGED, Event.AI_JOB_FINISHED, Event.CHAT_GENERATION_CHANGED, Event.WORKSPACE_RENAMED ]);
|
||||
|
||||
// Configure the spellchecking for the user input:
|
||||
this.SettingsManager.InjectSpellchecking(USER_INPUT_ATTRIBUTES);
|
||||
@ -377,6 +377,29 @@ public partial class ChatComponent : MSGComponentBase, IAsyncDisposable
|
||||
this.WorkspaceName(this.currentWorkspaceName);
|
||||
}
|
||||
|
||||
private async Task RefreshRenamedWorkspaceHeaderAsync(Guid workspaceId)
|
||||
{
|
||||
var currentChatThread = this.ChatThread;
|
||||
if (currentChatThread is null || currentChatThread.WorkspaceId != workspaceId)
|
||||
return;
|
||||
|
||||
var syncVersion = Interlocked.Increment(ref this.workspaceHeaderSyncVersion);
|
||||
var chatThreadId = currentChatThread.ChatId;
|
||||
var loadedWorkspaceName = await WorkspaceBehaviour.LoadWorkspaceNameAsync(workspaceId);
|
||||
|
||||
if (syncVersion != this.workspaceHeaderSyncVersion)
|
||||
return;
|
||||
|
||||
if (this.ChatThread is null
|
||||
|| this.ChatThread.ChatId != chatThreadId
|
||||
|| this.ChatThread.WorkspaceId != workspaceId)
|
||||
return;
|
||||
|
||||
this.currentChatThreadId = chatThreadId;
|
||||
this.currentWorkspaceId = workspaceId;
|
||||
this.PublishWorkspaceNameIfChanged(loadedWorkspaceName);
|
||||
}
|
||||
|
||||
private async Task SyncForegroundChatAsync()
|
||||
{
|
||||
var nextForegroundChatId = this.ChatThread?.ChatId ?? Guid.Empty;
|
||||
@ -864,7 +887,7 @@ public partial class ChatComponent : MSGComponentBase, IAsyncDisposable
|
||||
{ x => x.ConfirmText, T("Move chat") },
|
||||
};
|
||||
|
||||
var dialogReference = await this.DialogService.ShowAsync<WorkspaceSelectionDialog>(T("Move Chat to Workspace"), dialogParameters, DialogOptions.FULLSCREEN);
|
||||
var dialogReference = await this.DialogService.ShowAsync<WorkspaceSelectionDialog>(T("Move Chat to Workspace"), dialogParameters, DialogOptions.FULLSCREEN_MANUAL_ESCAPE);
|
||||
var dialogResult = await dialogReference.Result;
|
||||
if (dialogResult is null || dialogResult.Canceled)
|
||||
return;
|
||||
@ -1047,6 +1070,11 @@ public partial class ChatComponent : MSGComponentBase, IAsyncDisposable
|
||||
await this.SaveThread();
|
||||
break;
|
||||
|
||||
case Event.WORKSPACE_RENAMED:
|
||||
if (data is Guid workspaceId)
|
||||
await this.RefreshRenamedWorkspaceHeaderAsync(workspaceId);
|
||||
break;
|
||||
|
||||
case Event.AI_JOB_CHANGED:
|
||||
case Event.AI_JOB_FINISHED:
|
||||
case Event.CHAT_GENERATION_CHANGED:
|
||||
|
||||
@ -3,7 +3,6 @@ using System.Text.Json;
|
||||
|
||||
using AIStudio.Chat;
|
||||
using AIStudio.Dialogs;
|
||||
using AIStudio.Settings;
|
||||
using AIStudio.Tools.AIJobs;
|
||||
|
||||
using Microsoft.AspNetCore.Components;
|
||||
@ -57,7 +56,7 @@ public partial class Workspaces : MSGComponentBase
|
||||
protected override async Task OnInitializedAsync()
|
||||
{
|
||||
await base.OnInitializedAsync();
|
||||
this.ApplyFilters([], [ Event.AI_JOB_CHANGED, Event.AI_JOB_FINISHED, Event.CHAT_GENERATION_CHANGED ]);
|
||||
this.ApplyFilters([], [ Event.AI_JOB_CHANGED, Event.AI_JOB_FINISHED, Event.CHAT_GENERATION_CHANGED, Event.WORKSPACE_CREATED ]);
|
||||
_ = this.LoadTreeItemsAsync(startPrefetch: true);
|
||||
}
|
||||
|
||||
@ -101,6 +100,27 @@ public partial class Workspaces : MSGComponentBase
|
||||
|
||||
private string GetAddChatToWorkspaceTooltip(string workspaceName) => string.Format(T("Start a new chat in workspace '{0}'"), workspaceName);
|
||||
|
||||
private async Task<Func<string?, string?>> CreateWorkspaceNameValidationAsync(Guid excludedWorkspaceId = default, string? originalWorkspaceName = null)
|
||||
{
|
||||
var snapshot = await WorkspaceBehaviour.GetOrLoadWorkspaceTreeShellAsync();
|
||||
return workspaceName =>
|
||||
{
|
||||
var normalizedWorkspaceName = WorkspaceBehaviour.NormalizeWorkspaceName(workspaceName ?? string.Empty);
|
||||
if (string.IsNullOrWhiteSpace(normalizedWorkspaceName))
|
||||
return null;
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(originalWorkspaceName) &&
|
||||
string.Equals(WorkspaceBehaviour.NormalizeWorkspaceName(originalWorkspaceName), normalizedWorkspaceName, StringComparison.OrdinalIgnoreCase))
|
||||
return null;
|
||||
|
||||
var nameExists = snapshot.Workspaces.Any(workspace =>
|
||||
workspace.WorkspaceId != excludedWorkspaceId &&
|
||||
string.Equals(WorkspaceBehaviour.NormalizeWorkspaceName(workspace.Name), normalizedWorkspaceName, StringComparison.OrdinalIgnoreCase));
|
||||
|
||||
return nameExists ? T("There is already a workspace with this name. Please choose a different name.") : null;
|
||||
};
|
||||
}
|
||||
|
||||
private void BuildTreeItems(WorkspaceTreeCacheSnapshot snapshot)
|
||||
{
|
||||
this.treeItems.Clear();
|
||||
@ -720,6 +740,7 @@ public partial class Workspaces : MSGComponentBase
|
||||
{ x => x.ConfirmColor, Color.Info },
|
||||
{ x => x.AllowEmptyInput, false },
|
||||
{ x => x.EmptyInputErrorMessage, T("Please enter a workspace name.") },
|
||||
{ x => x.AdditionalValidation, await this.CreateWorkspaceNameValidationAsync(workspaceId, workspaceName) },
|
||||
};
|
||||
|
||||
var dialogReference = await this.DialogService.ShowAsync<SingleInputDialog>(T("Rename Workspace"), dialogParameters, DialogOptions.FULLSCREEN);
|
||||
@ -728,9 +749,10 @@ public partial class Workspaces : MSGComponentBase
|
||||
return;
|
||||
|
||||
var alteredWorkspaceName = (dialogResult.Data as string)!;
|
||||
var workspaceNamePath = Path.Join(workspacePath, "name");
|
||||
await File.WriteAllTextAsync(workspaceNamePath, alteredWorkspaceName, Encoding.UTF8);
|
||||
await WorkspaceBehaviour.UpdateWorkspaceNameInCacheAsync(workspaceId, alteredWorkspaceName);
|
||||
if (!await WorkspaceBehaviour.RenameWorkspaceAsync(workspaceId, alteredWorkspaceName))
|
||||
return;
|
||||
|
||||
await this.SendMessage(Event.WORKSPACE_RENAMED, workspaceId);
|
||||
await this.LoadTreeItemsAsync(startPrefetch: false);
|
||||
}
|
||||
|
||||
@ -745,6 +767,7 @@ public partial class Workspaces : MSGComponentBase
|
||||
{ x => x.ConfirmColor, Color.Info },
|
||||
{ x => x.AllowEmptyInput, false },
|
||||
{ x => x.EmptyInputErrorMessage, T("Please enter a workspace name.") },
|
||||
{ x => x.AdditionalValidation, await this.CreateWorkspaceNameValidationAsync() },
|
||||
};
|
||||
|
||||
var dialogReference = await this.DialogService.ShowAsync<SingleInputDialog>(T("Add Workspace"), dialogParameters, DialogOptions.FULLSCREEN);
|
||||
@ -752,14 +775,10 @@ public partial class Workspaces : MSGComponentBase
|
||||
if (dialogResult is null || dialogResult.Canceled)
|
||||
return;
|
||||
|
||||
var workspaceId = Guid.NewGuid();
|
||||
var workspacePath = Path.Join(SettingsManager.DataDirectory, "workspaces", workspaceId.ToString());
|
||||
Directory.CreateDirectory(workspacePath);
|
||||
|
||||
var workspaceName = (dialogResult.Data as string)!;
|
||||
var workspaceNamePath = Path.Join(workspacePath, "name");
|
||||
await File.WriteAllTextAsync(workspaceNamePath, workspaceName, Encoding.UTF8);
|
||||
await WorkspaceBehaviour.AddWorkspaceToCacheAsync(workspaceId, workspacePath, workspaceName);
|
||||
var result = await WorkspaceBehaviour.TryCreateWorkspaceAsync(workspaceName);
|
||||
if (!result.Success)
|
||||
return;
|
||||
|
||||
await this.LoadTreeItemsAsync(startPrefetch: false);
|
||||
}
|
||||
@ -804,7 +823,7 @@ public partial class Workspaces : MSGComponentBase
|
||||
{ x => x.ConfirmText, T("Move chat") },
|
||||
};
|
||||
|
||||
var dialogReference = await this.DialogService.ShowAsync<WorkspaceSelectionDialog>(T("Move Chat to Workspace"), dialogParameters, DialogOptions.FULLSCREEN);
|
||||
var dialogReference = await this.DialogService.ShowAsync<WorkspaceSelectionDialog>(T("Move Chat to Workspace"), dialogParameters, DialogOptions.FULLSCREEN_MANUAL_ESCAPE);
|
||||
var dialogResult = await dialogReference.Result;
|
||||
if (dialogResult is null || dialogResult.Canceled)
|
||||
return;
|
||||
@ -868,6 +887,10 @@ public partial class Workspaces : MSGComponentBase
|
||||
await this.ForceRefreshFromDiskAsync();
|
||||
break;
|
||||
|
||||
case Event.WORKSPACE_CREATED:
|
||||
await this.LoadTreeItemsAsync(startPrefetch: false);
|
||||
break;
|
||||
|
||||
case Event.AI_JOB_CHANGED:
|
||||
case Event.AI_JOB_FINISHED:
|
||||
case Event.CHAT_GENERATION_CHANGED:
|
||||
|
||||
@ -8,6 +8,12 @@ public static class DialogOptions
|
||||
FullWidth = true, MaxWidth = MaxWidth.Medium,
|
||||
};
|
||||
|
||||
public static readonly MudBlazor.DialogOptions FULLSCREEN_MANUAL_ESCAPE = new()
|
||||
{
|
||||
CloseOnEscapeKey = false,
|
||||
FullWidth = true, MaxWidth = MaxWidth.Medium,
|
||||
};
|
||||
|
||||
public static readonly MudBlazor.DialogOptions FULLSCREEN_NO_HEADER = new()
|
||||
{
|
||||
NoHeader = true,
|
||||
|
||||
@ -31,6 +31,9 @@ public partial class SingleInputDialog : MSGComponentBase
|
||||
[Parameter]
|
||||
public string EmptyInputErrorMessage { get; set; } = string.Empty;
|
||||
|
||||
[Parameter]
|
||||
public Func<string?, string?>? AdditionalValidation { get; set; }
|
||||
|
||||
private static readonly Dictionary<string, object?> USER_INPUT_ATTRIBUTES = new();
|
||||
|
||||
private MudForm form = null!;
|
||||
@ -53,7 +56,7 @@ public partial class SingleInputDialog : MSGComponentBase
|
||||
if (!this.AllowEmptyInput && string.IsNullOrWhiteSpace(value))
|
||||
return string.IsNullOrWhiteSpace(this.EmptyInputErrorMessage) ? T("Please enter a value.") : this.EmptyInputErrorMessage;
|
||||
|
||||
return null;
|
||||
return this.AdditionalValidation?.Invoke(value);
|
||||
}
|
||||
|
||||
private void Cancel() => this.MudDialog.Cancel();
|
||||
|
||||
@ -5,18 +5,57 @@
|
||||
@this.Message
|
||||
</MudText>
|
||||
<MudList T="Guid" @bind-SelectedValue="@this.selectedWorkspace">
|
||||
@foreach (var (workspaceName, workspaceId) in this.workspaces)
|
||||
@foreach (var workspace in this.workspaces)
|
||||
{
|
||||
<MudListItem Text="@workspaceName" Icon="@Icons.Material.Filled.Description" Value="@workspaceId" />
|
||||
<MudListItem Text="@workspace.Name" Icon="@Icons.Material.Filled.Description" Value="@workspace.WorkspaceId" />
|
||||
}
|
||||
</MudList>
|
||||
</DialogContent>
|
||||
<DialogActions>
|
||||
<MudButton OnClick="@this.Cancel" Variant="Variant.Filled">
|
||||
@T("Cancel")
|
||||
</MudButton>
|
||||
<MudButton OnClick="@this.Confirm" Variant="Variant.Filled" Color="Color.Info">
|
||||
@this.ConfirmText
|
||||
</MudButton>
|
||||
<MudStack Style="width: 100%;" Spacing="2">
|
||||
<MudDivider/>
|
||||
|
||||
@if (this.showCreateWorkspaceForm)
|
||||
{
|
||||
<MudForm @ref="this.createWorkspaceForm">
|
||||
<MudTextField T="string"
|
||||
@ref="@this.newWorkspaceNameField"
|
||||
@bind-Text="@this.newWorkspaceName"
|
||||
Variant="Variant.Outlined"
|
||||
AutoGrow="@false"
|
||||
Lines="1"
|
||||
Label="@T("Workspace name")"
|
||||
AutoFocus="@true"
|
||||
Immediate="@true"
|
||||
Disabled="@this.isCreatingWorkspace"
|
||||
OnKeyDown="@this.HandleNewWorkspaceNameKeyDown"
|
||||
Validation="@this.ValidateNewWorkspaceName" />
|
||||
</MudForm>
|
||||
}
|
||||
else
|
||||
{
|
||||
<MudButton StartIcon="@Icons.Material.Filled.LibraryAdd" Variant="Variant.Filled" OnClick="@this.ShowCreateWorkspaceForm">
|
||||
@T("Create new workspace")
|
||||
</MudButton>
|
||||
}
|
||||
|
||||
<MudStack Row="@true" Justify="Justify.FlexEnd" AlignItems="AlignItems.Center" Wrap="Wrap.NoWrap" Spacing="2">
|
||||
<MudButton OnClick="@this.Cancel" Variant="Variant.Filled">
|
||||
@T("Cancel")
|
||||
</MudButton>
|
||||
@if (this.showCreateWorkspaceForm)
|
||||
{
|
||||
<MudButton OnClick="@this.CreateWorkspaceAsync" Variant="Variant.Filled" Color="Color.Info" Disabled="@this.isCreatingWorkspace">
|
||||
@T("Add workspace")
|
||||
</MudButton>
|
||||
}
|
||||
else
|
||||
{
|
||||
<MudButton OnClick="@this.Confirm" Variant="Variant.Filled" Color="Color.Info" Disabled="@(this.selectedWorkspace == Guid.Empty)">
|
||||
@this.ConfirmText
|
||||
</MudButton>
|
||||
}
|
||||
</MudStack>
|
||||
</MudStack>
|
||||
</DialogActions>
|
||||
</MudDialog>
|
||||
@ -1,14 +1,20 @@
|
||||
using AIStudio.Components;
|
||||
|
||||
using Microsoft.AspNetCore.Components;
|
||||
using Microsoft.AspNetCore.Components.Web;
|
||||
|
||||
namespace AIStudio.Dialogs;
|
||||
|
||||
public partial class WorkspaceSelectionDialog : MSGComponentBase
|
||||
{
|
||||
private readonly record struct WorkspaceSelectionItem(Guid WorkspaceId, string Name);
|
||||
|
||||
[CascadingParameter]
|
||||
private IMudDialogInstance MudDialog { get; set; } = null!;
|
||||
|
||||
[Inject]
|
||||
private IJSRuntime JsRuntime { get; init; } = null!;
|
||||
|
||||
[Parameter]
|
||||
public string Message { get; set; } = string.Empty;
|
||||
|
||||
@ -18,8 +24,18 @@ public partial class WorkspaceSelectionDialog : MSGComponentBase
|
||||
[Parameter]
|
||||
public string ConfirmText { get; set; } = "OK";
|
||||
|
||||
private readonly Dictionary<string, Guid> workspaces = new();
|
||||
private readonly List<WorkspaceSelectionItem> workspaces = [];
|
||||
private readonly string escapeHandlerId = $"workspace-selection-dialog-{Guid.NewGuid():N}";
|
||||
private MudForm? createWorkspaceForm;
|
||||
private MudTextField<string>? newWorkspaceNameField;
|
||||
private DotNetObjectReference<WorkspaceSelectionDialog>? dotNetReference;
|
||||
private Guid selectedWorkspace;
|
||||
private string newWorkspaceName = string.Empty;
|
||||
private bool isCreatingWorkspace;
|
||||
private bool showCreateWorkspaceForm;
|
||||
private bool shouldFocusNewWorkspaceName;
|
||||
private string? createWorkspaceError;
|
||||
private string? createWorkspaceErrorName;
|
||||
|
||||
#region Overrides of ComponentBase
|
||||
|
||||
@ -29,15 +45,156 @@ public partial class WorkspaceSelectionDialog : MSGComponentBase
|
||||
|
||||
var snapshot = await WorkspaceBehaviour.GetOrLoadWorkspaceTreeShellAsync();
|
||||
foreach (var workspace in snapshot.Workspaces)
|
||||
this.workspaces[workspace.Name] = workspace.WorkspaceId;
|
||||
this.workspaces.Add(new(workspace.WorkspaceId, workspace.Name));
|
||||
|
||||
this.StateHasChanged();
|
||||
await base.OnInitializedAsync();
|
||||
}
|
||||
|
||||
protected override async Task OnAfterRenderAsync(bool firstRender)
|
||||
{
|
||||
if (firstRender)
|
||||
{
|
||||
this.dotNetReference = DotNetObjectReference.Create(this);
|
||||
await this.JsRuntime.InvokeVoidAsync("registerEscapeHandler", this.escapeHandlerId, this.dotNetReference);
|
||||
}
|
||||
|
||||
if (this.shouldFocusNewWorkspaceName && this.newWorkspaceNameField is not null)
|
||||
{
|
||||
this.shouldFocusNewWorkspaceName = false;
|
||||
await this.newWorkspaceNameField.FocusAsync();
|
||||
}
|
||||
|
||||
await base.OnAfterRenderAsync(firstRender);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
private void Cancel() => this.MudDialog.Cancel();
|
||||
private string? ValidateNewWorkspaceName(string? workspaceName)
|
||||
{
|
||||
var normalizedWorkspaceName = WorkspaceBehaviour.NormalizeWorkspaceName(workspaceName ?? string.Empty);
|
||||
if (string.IsNullOrWhiteSpace(normalizedWorkspaceName))
|
||||
return T("Please enter a workspace name.");
|
||||
|
||||
if (this.IsWorkspaceNameExisting(normalizedWorkspaceName))
|
||||
return T("There is already a workspace with this name. Please choose a different name.");
|
||||
|
||||
if (this.createWorkspaceError is not null && string.Equals(this.createWorkspaceErrorName, normalizedWorkspaceName, StringComparison.OrdinalIgnoreCase))
|
||||
return this.createWorkspaceError;
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private bool IsWorkspaceNameExisting(string normalizedWorkspaceName)
|
||||
{
|
||||
return this.workspaces.Any(workspace =>
|
||||
string.Equals(WorkspaceBehaviour.NormalizeWorkspaceName(workspace.Name), normalizedWorkspaceName, StringComparison.OrdinalIgnoreCase));
|
||||
}
|
||||
|
||||
private async Task HandleNewWorkspaceNameKeyDown(KeyboardEventArgs keyEvent)
|
||||
{
|
||||
var key = keyEvent.Key.ToLowerInvariant();
|
||||
var code = keyEvent.Code.ToLowerInvariant();
|
||||
if (key is not "enter" && code is not "enter" and not "numpadenter")
|
||||
return;
|
||||
|
||||
if (keyEvent is { AltKey: true } or { CtrlKey: true } or { MetaKey: true })
|
||||
return;
|
||||
|
||||
await this.CreateWorkspaceAsync();
|
||||
}
|
||||
|
||||
private void ShowCreateWorkspaceForm()
|
||||
{
|
||||
this.createWorkspaceError = null;
|
||||
this.createWorkspaceErrorName = null;
|
||||
this.newWorkspaceName = string.Empty;
|
||||
this.showCreateWorkspaceForm = true;
|
||||
this.shouldFocusNewWorkspaceName = true;
|
||||
}
|
||||
|
||||
private async Task CreateWorkspaceAsync()
|
||||
{
|
||||
if (this.createWorkspaceForm is null)
|
||||
return;
|
||||
|
||||
this.createWorkspaceError = null;
|
||||
this.createWorkspaceErrorName = null;
|
||||
await this.createWorkspaceForm.Validate();
|
||||
if (!this.createWorkspaceForm.IsValid)
|
||||
return;
|
||||
|
||||
this.isCreatingWorkspace = true;
|
||||
try
|
||||
{
|
||||
var result = await WorkspaceBehaviour.TryCreateWorkspaceAsync(this.newWorkspaceName);
|
||||
if (!result.Success)
|
||||
{
|
||||
this.createWorkspaceError = T("There is already a workspace with this name. Please choose a different name.");
|
||||
this.createWorkspaceErrorName = WorkspaceBehaviour.NormalizeWorkspaceName(this.newWorkspaceName);
|
||||
await this.createWorkspaceForm.Validate();
|
||||
return;
|
||||
}
|
||||
|
||||
this.workspaces.Add(new(result.Workspace.WorkspaceId, result.Workspace.Name));
|
||||
this.selectedWorkspace = result.Workspace.WorkspaceId;
|
||||
this.newWorkspaceName = string.Empty;
|
||||
this.createWorkspaceForm?.ResetValidation();
|
||||
this.showCreateWorkspaceForm = false;
|
||||
await this.SendMessage(Event.WORKSPACE_CREATED, result.Workspace.WorkspaceId);
|
||||
}
|
||||
finally
|
||||
{
|
||||
this.isCreatingWorkspace = false;
|
||||
}
|
||||
}
|
||||
|
||||
private void Cancel()
|
||||
{
|
||||
if (!this.showCreateWorkspaceForm)
|
||||
{
|
||||
this.MudDialog.Cancel();
|
||||
return;
|
||||
}
|
||||
|
||||
this.createWorkspaceError = null;
|
||||
this.createWorkspaceErrorName = null;
|
||||
this.newWorkspaceName = string.Empty;
|
||||
this.createWorkspaceForm?.ResetValidation();
|
||||
this.showCreateWorkspaceForm = false;
|
||||
this.shouldFocusNewWorkspaceName = false;
|
||||
}
|
||||
|
||||
[JSInvokable]
|
||||
public async Task HandleEscapeKeyAsync()
|
||||
{
|
||||
await this.InvokeAsync(() =>
|
||||
{
|
||||
this.Cancel();
|
||||
this.StateHasChanged();
|
||||
});
|
||||
}
|
||||
|
||||
private void Confirm() => this.MudDialog.Close(DialogResult.Ok(this.selectedWorkspace));
|
||||
|
||||
#region Overrides of MSGComponentBase
|
||||
|
||||
protected override void DisposeResources()
|
||||
{
|
||||
try
|
||||
{
|
||||
_ = this.JsRuntime.InvokeVoidAsync("unregisterEscapeHandler", this.escapeHandlerId).AsTask();
|
||||
}
|
||||
catch
|
||||
{
|
||||
// Ignore JS cleanup errors while the dialog is being disposed.
|
||||
}
|
||||
|
||||
this.dotNetReference?.Dispose();
|
||||
this.dotNetReference = null;
|
||||
|
||||
base.DisposeResources();
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
@ -3255,6 +3255,9 @@ UI_TEXT_CONTENT["AISTUDIO::COMPONENTS::WORKSPACES::T3045856778"] = "Chat in den
|
||||
-- Please enter a new or edit the name for your workspace '{0}':
|
||||
UI_TEXT_CONTENT["AISTUDIO::COMPONENTS::WORKSPACES::T323280982"] = "Bitte geben Sie einen neuen Namen für ihren Arbeitsbereich „{0}“ ein oder bearbeiten Sie ihn:"
|
||||
|
||||
-- There is already a workspace with this name. Please choose a different name.
|
||||
UI_TEXT_CONTENT["AISTUDIO::COMPONENTS::WORKSPACES::T3249036008"] = "Es gibt bereits einen Arbeitsbereich mit diesem Namen. Bitte wählen Sie einen anderen Namen."
|
||||
|
||||
-- Please enter a workspace name.
|
||||
UI_TEXT_CONTENT["AISTUDIO::COMPONENTS::WORKSPACES::T3288132732"] = "Bitte geben Sie einen Namen für diesen Arbeitsbereich ein."
|
||||
|
||||
@ -5796,6 +5799,21 @@ UI_TEXT_CONTENT["AISTUDIO::DIALOGS::UPDATEDIALOG::T25417398"] = "Aktualisieren v
|
||||
-- Install later
|
||||
UI_TEXT_CONTENT["AISTUDIO::DIALOGS::UPDATEDIALOG::T2936430090"] = "Später installieren"
|
||||
|
||||
-- Create new workspace
|
||||
UI_TEXT_CONTENT["AISTUDIO::DIALOGS::WORKSPACESELECTIONDIALOG::T1541251414"] = "Neuen Arbeitsbereich erstellen"
|
||||
|
||||
-- Add workspace
|
||||
UI_TEXT_CONTENT["AISTUDIO::DIALOGS::WORKSPACESELECTIONDIALOG::T1586005241"] = "Arbeitsbereich hinzufügen"
|
||||
|
||||
-- Workspace name
|
||||
UI_TEXT_CONTENT["AISTUDIO::DIALOGS::WORKSPACESELECTIONDIALOG::T295876489"] = "Name des Arbeitsbereichs"
|
||||
|
||||
-- There is already a workspace with this name. Please choose a different name.
|
||||
UI_TEXT_CONTENT["AISTUDIO::DIALOGS::WORKSPACESELECTIONDIALOG::T3249036008"] = "Es gibt bereits einen Arbeitsbereich mit diesem Namen. Bitte wählen Sie einen anderen Namen."
|
||||
|
||||
-- Please enter a workspace name.
|
||||
UI_TEXT_CONTENT["AISTUDIO::DIALOGS::WORKSPACESELECTIONDIALOG::T3288132732"] = "Bitte geben Sie einen Namen für diesen Arbeitsbereich ein."
|
||||
|
||||
-- Cancel
|
||||
UI_TEXT_CONTENT["AISTUDIO::DIALOGS::WORKSPACESELECTIONDIALOG::T900713019"] = "Abbrechen"
|
||||
|
||||
|
||||
@ -3255,6 +3255,9 @@ UI_TEXT_CONTENT["AISTUDIO::COMPONENTS::WORKSPACES::T3045856778"] = "Move Chat to
|
||||
-- Please enter a new or edit the name for your workspace '{0}':
|
||||
UI_TEXT_CONTENT["AISTUDIO::COMPONENTS::WORKSPACES::T323280982"] = "Please enter a new or edit the name for your workspace '{0}':"
|
||||
|
||||
-- There is already a workspace with this name. Please choose a different name.
|
||||
UI_TEXT_CONTENT["AISTUDIO::COMPONENTS::WORKSPACES::T3249036008"] = "There is already a workspace with this name. Please choose a different name."
|
||||
|
||||
-- Please enter a workspace name.
|
||||
UI_TEXT_CONTENT["AISTUDIO::COMPONENTS::WORKSPACES::T3288132732"] = "Please enter a workspace name."
|
||||
|
||||
@ -5796,6 +5799,21 @@ UI_TEXT_CONTENT["AISTUDIO::DIALOGS::UPDATEDIALOG::T25417398"] = "Update from v{0
|
||||
-- Install later
|
||||
UI_TEXT_CONTENT["AISTUDIO::DIALOGS::UPDATEDIALOG::T2936430090"] = "Install later"
|
||||
|
||||
-- Create new workspace
|
||||
UI_TEXT_CONTENT["AISTUDIO::DIALOGS::WORKSPACESELECTIONDIALOG::T1541251414"] = "Create new workspace"
|
||||
|
||||
-- Add workspace
|
||||
UI_TEXT_CONTENT["AISTUDIO::DIALOGS::WORKSPACESELECTIONDIALOG::T1586005241"] = "Add workspace"
|
||||
|
||||
-- Workspace name
|
||||
UI_TEXT_CONTENT["AISTUDIO::DIALOGS::WORKSPACESELECTIONDIALOG::T295876489"] = "Workspace name"
|
||||
|
||||
-- There is already a workspace with this name. Please choose a different name.
|
||||
UI_TEXT_CONTENT["AISTUDIO::DIALOGS::WORKSPACESELECTIONDIALOG::T3249036008"] = "There is already a workspace with this name. Please choose a different name."
|
||||
|
||||
-- Please enter a workspace name.
|
||||
UI_TEXT_CONTENT["AISTUDIO::DIALOGS::WORKSPACESELECTIONDIALOG::T3288132732"] = "Please enter a workspace name."
|
||||
|
||||
-- Cancel
|
||||
UI_TEXT_CONTENT["AISTUDIO::DIALOGS::WORKSPACESELECTIONDIALOG::T900713019"] = "Cancel"
|
||||
|
||||
|
||||
@ -156,6 +156,16 @@ public enum Event
|
||||
/// </summary>
|
||||
WORKSPACE_TOGGLE_OVERLAY,
|
||||
|
||||
/// <summary>
|
||||
/// Notifies receivers that a workspace was renamed.
|
||||
/// </summary>
|
||||
WORKSPACE_RENAMED,
|
||||
|
||||
/// <summary>
|
||||
/// Notifies receivers that a workspace was created.
|
||||
/// </summary>
|
||||
WORKSPACE_CREATED,
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@ -12,6 +12,8 @@ namespace AIStudio.Tools;
|
||||
|
||||
public static class WorkspaceBehaviour
|
||||
{
|
||||
public readonly record struct TryCreateWorkspaceResult(bool Success, WorkspaceTreeWorkspace Workspace);
|
||||
|
||||
private sealed class WorkspaceChatCacheEntry
|
||||
{
|
||||
public Guid WorkspaceId { get; init; }
|
||||
@ -76,9 +78,9 @@ public static class WorkspaceBehaviour
|
||||
|
||||
private static readonly TimeSpan PREFETCH_DELAY_DURATION = TimeSpan.FromMilliseconds(45);
|
||||
|
||||
private static string WorkspaceRootDirectory => Path.Join(SettingsManager.DataDirectory, "workspaces");
|
||||
private static readonly string WORKSPACE_ROOT_DIRECTORY = Path.Join(SettingsManager.DataDirectory, "workspaces");
|
||||
|
||||
private static string TemporaryChatsRootDirectory => Path.Join(SettingsManager.DataDirectory, "tempChats");
|
||||
private static readonly string TEMPORARY_CHATS_ROOT_DIRECTORY = Path.Join(SettingsManager.DataDirectory, "tempChats");
|
||||
|
||||
private static SemaphoreSlim GetChatSemaphore(Guid workspaceId, Guid chatId)
|
||||
{
|
||||
@ -156,9 +158,9 @@ public static class WorkspaceBehaviour
|
||||
private static async Task<List<WorkspaceChatCacheEntry>> ReadTemporaryChatsCoreAsync()
|
||||
{
|
||||
var chats = new List<WorkspaceChatCacheEntry>();
|
||||
Directory.CreateDirectory(TemporaryChatsRootDirectory);
|
||||
Directory.CreateDirectory(TEMPORARY_CHATS_ROOT_DIRECTORY);
|
||||
|
||||
foreach (var tempChatPath in Directory.EnumerateDirectories(TemporaryChatsRootDirectory))
|
||||
foreach (var tempChatPath in Directory.EnumerateDirectories(TEMPORARY_CHATS_ROOT_DIRECTORY))
|
||||
{
|
||||
if (!Guid.TryParse(Path.GetFileName(tempChatPath), out var chatId))
|
||||
continue;
|
||||
@ -188,8 +190,8 @@ public static class WorkspaceBehaviour
|
||||
WORKSPACE_TREE_CACHE.Workspaces.Clear();
|
||||
WORKSPACE_TREE_CACHE.WorkspaceOrder.Clear();
|
||||
|
||||
Directory.CreateDirectory(WorkspaceRootDirectory);
|
||||
foreach (var workspacePath in Directory.EnumerateDirectories(WorkspaceRootDirectory))
|
||||
Directory.CreateDirectory(WORKSPACE_ROOT_DIRECTORY);
|
||||
foreach (var workspacePath in Directory.EnumerateDirectories(WORKSPACE_ROOT_DIRECTORY))
|
||||
{
|
||||
if (!Guid.TryParse(Path.GetFileName(workspacePath), out var workspaceId))
|
||||
continue;
|
||||
@ -259,6 +261,13 @@ public static class WorkspaceBehaviour
|
||||
return false;
|
||||
}
|
||||
|
||||
private static bool WorkspaceNameExistsCore(string workspaceName, Guid excludedWorkspaceId = default)
|
||||
{
|
||||
return WORKSPACE_TREE_CACHE.Workspaces.Values.Any(workspace =>
|
||||
workspace.WorkspaceId != excludedWorkspaceId &&
|
||||
string.Equals(workspace.WorkspaceName.Trim(), workspaceName, StringComparison.OrdinalIgnoreCase));
|
||||
}
|
||||
|
||||
private static async Task<bool> ThreadContainsTermsAsync(WorkspaceTreeChat chat, IReadOnlyList<string> terms, CancellationToken token)
|
||||
{
|
||||
var (acquired, semaphore) = await TryAcquireChatSemaphoreAsync(chat.WorkspaceId, chat.ChatId, nameof(ThreadContainsTermsAsync));
|
||||
@ -588,6 +597,100 @@ public static class WorkspaceBehaviour
|
||||
}
|
||||
}
|
||||
|
||||
public static string NormalizeWorkspaceName(string workspaceName) => workspaceName.Trim();
|
||||
|
||||
public static async Task<bool> IsWorkspaceNameExistingAsync(string workspaceName, Guid excludedWorkspaceId = default)
|
||||
{
|
||||
var normalizedWorkspaceName = NormalizeWorkspaceName(workspaceName);
|
||||
if (string.IsNullOrWhiteSpace(normalizedWorkspaceName))
|
||||
return false;
|
||||
|
||||
await WORKSPACE_TREE_CACHE_SEMAPHORE.WaitAsync();
|
||||
try
|
||||
{
|
||||
await EnsureTreeShellLoadedCoreAsync();
|
||||
return WorkspaceNameExistsCore(normalizedWorkspaceName, excludedWorkspaceId);
|
||||
}
|
||||
finally
|
||||
{
|
||||
WORKSPACE_TREE_CACHE_SEMAPHORE.Release();
|
||||
}
|
||||
}
|
||||
|
||||
public static async Task<TryCreateWorkspaceResult> TryCreateWorkspaceAsync(string workspaceName)
|
||||
{
|
||||
var normalizedWorkspaceName = NormalizeWorkspaceName(workspaceName);
|
||||
if (string.IsNullOrWhiteSpace(normalizedWorkspaceName))
|
||||
return new(false, default);
|
||||
|
||||
await WORKSPACE_TREE_CACHE_SEMAPHORE.WaitAsync();
|
||||
try
|
||||
{
|
||||
await EnsureTreeShellLoadedCoreAsync();
|
||||
if (WorkspaceNameExistsCore(normalizedWorkspaceName))
|
||||
return new(false, default);
|
||||
|
||||
var workspaceId = Guid.NewGuid();
|
||||
var workspacePath = Path.Join(WORKSPACE_ROOT_DIRECTORY, workspaceId.ToString());
|
||||
Directory.CreateDirectory(workspacePath);
|
||||
|
||||
var workspaceNamePath = Path.Join(workspacePath, "name");
|
||||
await File.WriteAllTextAsync(workspaceNamePath, normalizedWorkspaceName, Encoding.UTF8);
|
||||
|
||||
var workspace = new WorkspaceCacheEntry
|
||||
{
|
||||
WorkspaceId = workspaceId,
|
||||
WorkspacePath = workspacePath,
|
||||
WorkspaceName = normalizedWorkspaceName,
|
||||
Chats = [],
|
||||
ChatsLoaded = false,
|
||||
};
|
||||
WORKSPACE_TREE_CACHE.Workspaces[workspaceId] = workspace;
|
||||
WORKSPACE_TREE_CACHE.WorkspaceOrder.Add(workspaceId);
|
||||
|
||||
return new(true, ToPublicWorkspace(workspace));
|
||||
}
|
||||
finally
|
||||
{
|
||||
WORKSPACE_TREE_CACHE_SEMAPHORE.Release();
|
||||
}
|
||||
}
|
||||
|
||||
public static async Task<bool> RenameWorkspaceAsync(Guid workspaceId, string workspaceName)
|
||||
{
|
||||
var normalizedWorkspaceName = NormalizeWorkspaceName(workspaceName);
|
||||
if (string.IsNullOrWhiteSpace(normalizedWorkspaceName))
|
||||
return false;
|
||||
|
||||
await WORKSPACE_TREE_CACHE_SEMAPHORE.WaitAsync();
|
||||
try
|
||||
{
|
||||
await EnsureTreeShellLoadedCoreAsync();
|
||||
if (!WORKSPACE_TREE_CACHE.Workspaces.TryGetValue(workspaceId, out var workspace))
|
||||
return false;
|
||||
|
||||
var workspaceNamePath = Path.Join(workspace.WorkspacePath, "name");
|
||||
if (string.Equals(workspace.WorkspaceName.Trim(), normalizedWorkspaceName, StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
await File.WriteAllTextAsync(workspaceNamePath, normalizedWorkspaceName, Encoding.UTF8);
|
||||
workspace.WorkspaceName = normalizedWorkspaceName;
|
||||
return true;
|
||||
}
|
||||
|
||||
if (WorkspaceNameExistsCore(normalizedWorkspaceName, workspaceId))
|
||||
return false;
|
||||
|
||||
await File.WriteAllTextAsync(workspaceNamePath, normalizedWorkspaceName, Encoding.UTF8);
|
||||
workspace.WorkspaceName = normalizedWorkspaceName;
|
||||
|
||||
return true;
|
||||
}
|
||||
finally
|
||||
{
|
||||
WORKSPACE_TREE_CACHE_SEMAPHORE.Release();
|
||||
}
|
||||
}
|
||||
|
||||
public static bool IsChatExisting(LoadChat loadChat)
|
||||
{
|
||||
var chatPath = loadChat.WorkspaceId == Guid.Empty
|
||||
@ -668,7 +771,7 @@ public static class WorkspaceBehaviour
|
||||
|
||||
// Not in cache — read from disk and update cache in the same semaphore scope
|
||||
// to avoid a second semaphore acquisition via UpdateWorkspaceNameInCacheAsync:
|
||||
var workspacePath = Path.Join(WorkspaceRootDirectory, workspaceId.ToString());
|
||||
var workspacePath = Path.Join(WORKSPACE_ROOT_DIRECTORY, workspaceId.ToString());
|
||||
var workspaceNamePath = Path.Join(workspacePath, "name");
|
||||
string workspaceName;
|
||||
|
||||
@ -756,7 +859,7 @@ public static class WorkspaceBehaviour
|
||||
|
||||
private static async Task EnsureWorkspace(Guid workspaceId, string workspaceName)
|
||||
{
|
||||
var workspacePath = Path.Join(WorkspaceRootDirectory, workspaceId.ToString());
|
||||
var workspacePath = Path.Join(WORKSPACE_ROOT_DIRECTORY, workspaceId.ToString());
|
||||
var workspaceNamePath = Path.Join(workspacePath, "name");
|
||||
|
||||
if (!Path.Exists(workspacePath))
|
||||
|
||||
@ -131,3 +131,30 @@ window.formatChatInputMarkdown = function (inputId, formatType) {
|
||||
|
||||
return nextValue
|
||||
}
|
||||
|
||||
const escapeHandlers = new Map()
|
||||
|
||||
window.registerEscapeHandler = function (id, dotNetReference) {
|
||||
window.unregisterEscapeHandler(id)
|
||||
|
||||
const handler = function (event) {
|
||||
if (event.key !== 'Escape')
|
||||
return
|
||||
|
||||
event.preventDefault()
|
||||
event.stopPropagation()
|
||||
dotNetReference.invokeMethodAsync('HandleEscapeKeyAsync').catch(() => {})
|
||||
}
|
||||
|
||||
document.addEventListener('keydown', handler, true)
|
||||
escapeHandlers.set(id, handler)
|
||||
}
|
||||
|
||||
window.unregisterEscapeHandler = function (id) {
|
||||
const handler = escapeHandlers.get(id)
|
||||
if (!handler)
|
||||
return
|
||||
|
||||
document.removeEventListener('keydown', handler, true)
|
||||
escapeHandlers.delete(id)
|
||||
}
|
||||
@ -6,5 +6,7 @@
|
||||
- Added startup path and Linux package type details to the information page to make support easier.
|
||||
- Added the option to search for chats in all workspaces.
|
||||
- Improved workspaces by adding a shortcut to start a new chat directly from each workspace row.
|
||||
- Improved workspaces by allowing new workspaces to be created while moving a chat.
|
||||
- Improved the enterprise configuration details on the information page by showing where each configuration comes from and which configuration slot was used.
|
||||
- Fixed workspace creation and renaming to prevent new workspaces from using an existing name.
|
||||
- Upgraded dependencies.
|
||||
Loading…
Reference in New Issue
Block a user