Kept the inputs of assistants when you leave them (#1209)

Co-authored-by: Thorsten Sommer <SommerEngineering@users.noreply.github.com>
This commit is contained in:
Peer HogeterpandThorsten Sommer authored and GitHub committed 2026-11-28 11:52:17 +01:00
1 parent 1fbc1f4796
commit 7e2f83b19b
83 files changed
+1924 -347

No files matched your search

@@ -140,6 +140,7 @@ else
var webState = this.assistantState.WebContent[webContent.Name];
<div class="@webContent.Class" style="@GetOptionalStyle(webContent.Style)">
<ReadWebContent @bind-Content="@webState.Content"
ImportTargetId="@webContent.Name"
@bind-URL="@webState.URL"
ProviderSettings="@this.ProviderSettings"
@bind-CleanerIsRunning="@webState.CleanerIsRunning"
@@ -154,7 +155,7 @@ else
{
var fileState = this.assistantState.FileContent[fileContent.Name];
<div class="@fileContent.Class" style="@GetOptionalStyle(fileContent.Style)">
<ReadFileContent @bind-FileContent="@fileState.Content" MediaImportTargetId="@fileContent.Name" EnableDragDrop="true" CatchAllDocuments="@this.HasSingleDropZone" ShowAttachedDocumentState="@fileContent.ShowAttachedDocumentState" />
<ReadFileContent @bind-FileContent="@fileState.Content" ImportTargetId="@fileContent.Name" EnableDragDrop="true" CatchAllDocuments="@this.HasSingleDropZone" ShowAttachedDocumentState="@fileContent.ShowAttachedDocumentState" />
</div>
}
break;
@@ -88,9 +88,9 @@ public partial class AssistantDynamic : AssistantBaseCore<NoSettingsPanel>
private static readonly AssistantSessionStateKey<HashSet<string>?> ASSISTANT_TOOL_IDS_STATE_KEY = new(nameof(assistantToolIds));
private static readonly AssistantSessionStateKey<PluginAssistants?> ASSISTANT_PLUGIN_STATE_KEY = new(nameof(assistantPlugin));
private static readonly AssistantSessionStateKey<AssistantState> ASSISTANT_STATE_STATE_KEY = new(nameof(assistantState));
private static readonly AssistantSessionStateKey<Dictionary<string, string>> IMAGE_CACHE_STATE_KEY = new(nameof(imageCache));
private static readonly AssistantSessionStateKey<HashSet<string>> EXECUTING_BUTTON_ACTIONS_STATE_KEY = new(nameof(executingButtonActions));
private static readonly AssistantSessionStateKey<HashSet<string>> EXECUTING_SWITCH_ACTIONS_STATE_KEY = new(nameof(executingSwitchActions));
private static readonly AssistantSessionStateKey<Dictionary<string, string>> IMAGE_CACHE_STATE_KEY = new(nameof(imageCache), isDerived: true);
private static readonly AssistantSessionStateKey<HashSet<string>> EXECUTING_BUTTON_ACTIONS_STATE_KEY = new(nameof(executingButtonActions), isKeptOutOfDrafts: true);
private static readonly AssistantSessionStateKey<HashSet<string>> EXECUTING_SWITCH_ACTIONS_STATE_KEY = new(nameof(executingSwitchActions), isKeptOutOfDrafts: true);
private static readonly AssistantSessionStateKey<string> PLUGIN_PATH_STATE_KEY = new(nameof(pluginPath));
private static readonly AssistantSessionStateKey<PluginAssistantAudit?> AUDIT_STATE_KEY = new(nameof(audit));
private static readonly AssistantSessionStateKey<string> SECURITY_MESSAGE_STATE_KEY = new(nameof(securityMessage));
@@ -124,6 +124,12 @@ public partial class AssistantDynamic : AssistantBaseCore<NoSettingsPanel>
/// <inheritdoc />
protected override void RestoreCustomAssistantSessionState(AssistantSessionStateReader state)
{
if (state.IsDraft)
{
this.RestoreDraftState(state);
return;
}
state.Restore(TITLE_STATE_KEY, value => this.title = value);
state.Restore(DESCRIPTION_STATE_KEY, value => this.description = value);
state.Restore(SYSTEM_PROMPT_STATE_KEY, value => this.systemPrompt = value);
@@ -143,6 +149,40 @@ public partial class AssistantDynamic : AssistantBaseCore<NoSettingsPanel>
state.Restore(SHARES_ASSISTANT_NAME_STATE_KEY, value => this.sharesAssistantName = value);
}
/// <summary>
/// Restores from a draft what the user entered into the form.
/// </summary>
/// <remarks>
/// Everything else follows from the plugin as it is now, see OnInitialized: while the user was elsewhere,
/// the plugin may have changed, or the organization may have withdrawn its approval. A draft therefore
/// brings back neither the plugin, its audit, its security state, nor its system prompt, so that a
/// blocked assistant stays blocked. What the user entered comes back only into the same version of a
/// plugin which may start: the components of another version may differ, and a blocked assistant shows
/// no form. Flags of work in progress inside the form, such as a running content cleaner, stay cleared.
/// </remarks>
/// <param name="draft">The reader of the draft.</param>
private void RestoreDraftState(AssistantSessionStateReader draft)
{
if (this.isSecurityBlocked || this.assistantPlugin is not { } currentPlugin)
return;
PluginAssistants? pluginOfDraft = null;
draft.Restore(ASSISTANT_PLUGIN_STATE_KEY, value => pluginOfDraft = value);
if (pluginOfDraft is null || pluginOfDraft.Id != currentPlugin.Id)
return;
if (!ReferenceEquals(pluginOfDraft, currentPlugin) && !string.Equals(pluginOfDraft.ComputeAuditHash(), currentPlugin.ComputeAuditHash(), StringComparison.Ordinal))
return;
draft.Restore(ASSISTANT_STATE_STATE_KEY, value =>
{
this.assistantState.CopyFrom(value);
this.assistantState.ClearFlagsOfWorkInProgress();
});
draft.RestoreDictionary(IMAGE_CACHE_STATE_KEY, this.imageCache);
}
#region Implementation of AssistantBase
protected override void OnInitialized()
@@ -5,4 +5,17 @@ namespace AIStudio.Assistants.Dynamic;
public sealed class FileAttachmentState
{
public HashSet<FileAttachment> DocumentPaths { get; set; } = [];
/// <summary>
/// Creates a copy which shares nothing with this state.
/// </summary>
/// <returns>The copy.</returns>
public FileAttachmentState Copy() => new() { DocumentPaths = new HashSet<FileAttachment>(this.DocumentPaths, this.DocumentPaths.Comparer) };
/// <summary>
/// Tells whether another state holds the same files as this one.
/// </summary>
/// <param name="other">The state to compare with.</param>
/// <returns><c>true</c> when both states hold the same files; otherwise, <c>false</c>.</returns>
public bool HasSameInputsAs(FileAttachmentState other) => this.DocumentPaths.SetEquals(other.DocumentPaths);
}
@@ -3,4 +3,17 @@ namespace AIStudio.Assistants.Dynamic;
public sealed class FileContentState
{
public string Content { get; set; } = string.Empty;
/// <summary>
/// Creates a copy which shares nothing with this state.
/// </summary>
/// <returns>The copy.</returns>
public FileContentState Copy() => new() { Content = this.Content };
/// <summary>
/// Tells whether another state holds the same content as this one.
/// </summary>
/// <param name="other">The state to compare with.</param>
/// <returns><c>true</c> when both states hold the same content; otherwise, <c>false</c>.</returns>
public bool HasSameInputsAs(FileContentState other) => string.Equals(this.Content, other.Content, StringComparison.Ordinal);
}
@@ -7,4 +7,30 @@ public sealed class WebContentState
public bool Preselect { get; set; }
public bool PreselectContentCleaner { get; set; }
public bool CleanerIsRunning { get; set; }
/// <summary>
/// Creates a copy which shares nothing with this state.
/// </summary>
/// <returns>The copy.</returns>
public WebContentState Copy() => new()
{
Content = this.Content,
URL = this.URL,
Preselect = this.Preselect,
PreselectContentCleaner = this.PreselectContentCleaner,
CleanerIsRunning = this.CleanerIsRunning,
};
/// <summary>
/// Tells whether another state holds the same inputs as this one.
/// </summary>
/// <remarks>
/// Whether the content cleaner runs is not an input: it is work in progress, which ends with the component that started it.
/// </remarks>
/// <param name="other">The state to compare with.</param>
/// <returns><c>true</c> when both states hold the same inputs; otherwise, <c>false</c>.</returns>
public bool HasSameInputsAs(WebContentState other) => string.Equals(this.Content, other.Content, StringComparison.Ordinal)
&& string.Equals(this.URL, other.URL, StringComparison.Ordinal)
&& this.Preselect == other.Preselect
&& this.PreselectContentCleaner == other.PreselectContentCleaner;
}