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

@@ -280,6 +280,30 @@ public partial class DocumentAnalysisAssistant : AssistantBaseCore<NoSettingsPan
#endregion
#region Overrides of MSGComponentBase
protected override void DisposeResources()
{
//
// AutoSave writes the form into the policy only once the user paused typing. A draft restores no
// policy field, see RestoreDraftState, so a change made just before leaving would be lost without
// this. Disposing must not wait, so the settings are stored in the background:
//
try
{
if (this.selectedPolicy is { } policy && this.ApplyFormToPolicy(policy, force: false))
this.SettingsManager.StoreSettings().Observe($"{nameof(DocumentAnalysisAssistant)}: storing the policy when leaving");
}
catch (Exception e)
{
this.Logger.LogError(e, "Could not store the document analysis policy when leaving the assistant.");
}
base.DisposeResources();
}
#endregion
private async Task AutoSave(bool force = false)
{
//
@@ -416,6 +440,7 @@ public partial class DocumentAnalysisAssistant : AssistantBaseCore<NoSettingsPan
private static readonly AssistantSessionStateKey<ConfidenceLevel> POLICY_MINIMUM_PROVIDER_CONFIDENCE_STATE_KEY = new(nameof(policyMinimumProviderConfidence));
private static readonly AssistantSessionStateKey<string> POLICY_PRESELECTED_PROVIDER_ID_STATE_KEY = new(nameof(policyPreselectedProviderId));
private static readonly AssistantSessionStateKey<ProfilePreselection> POLICY_PRESELECTED_PROFILE_STATE_KEY = new(nameof(policyPreselectedProfile));
private static readonly AssistantSessionStateKey<HashSet<string>> POLICY_ALLOWED_TOOL_IDS_STATE_KEY = new(nameof(policyAllowedToolIds));
private static readonly AssistantSessionStateKey<HashSet<FileAttachment>> LOADED_DOCUMENT_PATHS_STATE_KEY = new(nameof(loadedDocumentPaths));
private static readonly AssistantSessionStateKey<string> ANALYZED_POLICY_NAME_STATE_KEY = new(nameof(analyzedPolicyName));
private static readonly AssistantSessionStateKey<List<ConfigurationSelectData<string>>> AVAILABLE_LLM_PROVIDERS_STATE_KEY = new(nameof(availableLLMProviders));
@@ -434,6 +459,7 @@ public partial class DocumentAnalysisAssistant : AssistantBaseCore<NoSettingsPan
state.Set(POLICY_MINIMUM_PROVIDER_CONFIDENCE_STATE_KEY, this.policyMinimumProviderConfidence);
state.Set(POLICY_PRESELECTED_PROVIDER_ID_STATE_KEY, this.policyPreselectedProviderId);
state.Set(POLICY_PRESELECTED_PROFILE_STATE_KEY, this.policyPreselectedProfile);
state.SetHashSet(POLICY_ALLOWED_TOOL_IDS_STATE_KEY, this.policyAllowedToolIds);
state.SetHashSet(LOADED_DOCUMENT_PATHS_STATE_KEY, this.loadedDocumentPaths);
state.Set(ANALYZED_POLICY_NAME_STATE_KEY, this.analyzedPolicyName);
state.SetList(AVAILABLE_LLM_PROVIDERS_STATE_KEY, this.availableLLMProviders);
@@ -442,6 +468,12 @@ public partial class DocumentAnalysisAssistant : AssistantBaseCore<NoSettingsPan
/// <inheritdoc />
protected override void RestoreCustomAssistantSessionState(AssistantSessionStateReader state)
{
if (state.IsDraft)
{
this.RestoreDraftState(state);
return;
}
state.Restore(SELECTED_POLICY_STATE_KEY, value => this.selectedPolicy = value);
state.Restore(POLICY_IS_PROTECTED_STATE_KEY, value => this.policyIsProtected = value);
state.Restore(POLICY_HIDE_POLICY_DEFINITION_STATE_KEY, value => this.policyHidePolicyDefinition = value);
@@ -457,10 +489,46 @@ public partial class DocumentAnalysisAssistant : AssistantBaseCore<NoSettingsPan
state.Restore(POLICY_MINIMUM_PROVIDER_CONFIDENCE_STATE_KEY, value => this.policyMinimumProviderConfidence = value);
state.Restore(POLICY_PRESELECTED_PROVIDER_ID_STATE_KEY, value => this.policyPreselectedProviderId = value);
state.Restore(POLICY_PRESELECTED_PROFILE_STATE_KEY, value => this.policyPreselectedProfile = value);
//
// The tools belong to the policy as well. On opening, the form holds those of the first policy,
// which AutoSave would otherwise write into the restored one:
//
state.Restore(POLICY_ALLOWED_TOOL_IDS_STATE_KEY, value => this.policyAllowedToolIds = [..value]);
state.RestoreHashSet(LOADED_DOCUMENT_PATHS_STATE_KEY, this.loadedDocumentPaths);
state.Restore(ANALYZED_POLICY_NAME_STATE_KEY, value => this.analyzedPolicyName = value);
state.RestoreList(AVAILABLE_LLM_PROVIDERS_STATE_KEY, this.availableLLMProviders);
}
/// <summary>
/// Restores from a draft which policy was selected and what the user entered besides the policy.
/// </summary>
/// <remarks>
/// The policies live in the settings, and AutoSave writes every change of the form into them. While the
/// user was elsewhere, a policy may have changed nonetheless, e.g., through a configuration plugin of the
/// organization, or it may be gone. A draft therefore brings back none of the policy fields, which hold
/// a copy of the policy, among them its minimum provider confidence: the form takes the policy as it is
/// now. A policy which no longer exists leaves the one selected on opening.
/// </remarks>
/// <param name="draft">The reader of the draft.</param>
private void RestoreDraftState(AssistantSessionStateReader draft)
{
DataDocumentAnalysisPolicy? policyOfDraft = null;
draft.Restore(SELECTED_POLICY_STATE_KEY, value => policyOfDraft = value);
if (policyOfDraft is not null && this.SettingsManager.ConfigurationData.DocumentAnalysis.Policies.FirstOrDefault(policy => policy.Id == policyOfDraft.Id) is { } currentPolicy)
{
this.selectedPolicy = currentPolicy;
this.MightPreselectValues();
draft.Restore(POLICY_DEFINITION_EXPANDED_STATE_KEY, value =>
{
this.policyDefinitionExpanded = value;
this.documentSelectionExpanded = !value;
});
}
draft.RestoreHashSet(LOADED_DOCUMENT_PATHS_STATE_KEY, this.loadedDocumentPaths);
draft.Restore(ANALYZED_POLICY_NAME_STATE_KEY, value => this.analyzedPolicyName = value);
}
private bool IsNoPolicySelectedOrProtected => this.selectedPolicy is null || this.selectedPolicy.IsProtected;