mirror of
https://github.com/MindWorkAI/AI-Studio.git
synced 2026-12-04 23:04:35 +00:00
Co-authored-by: Thorsten Sommer <SommerEngineering@users.noreply.github.com>
94 lines
5.4 KiB
C#
94 lines
5.4 KiB
C#
using AIStudio.Chat;
|
|
using AIStudio.Components;
|
|
using AIStudio.Settings;
|
|
using AIStudio.Tools.AssistantSessions;
|
|
|
|
namespace AIStudio.Assistants;
|
|
|
|
public abstract class AssistantLowerBase : MSGComponentBase
|
|
{
|
|
protected static readonly Dictionary<string, object?> USER_INPUT_ATTRIBUTES = new();
|
|
|
|
internal const string RESULT_DIV_ID = "assistantResult";
|
|
internal const string BEFORE_RESULT_DIV_ID = "beforeAssistantResult";
|
|
internal const string AFTER_RESULT_DIV_ID = "afterAssistantResult";
|
|
|
|
/// <summary>
|
|
/// The minimum time between two renderings of the assistant while an answer streams in.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// The stream waits for each rendering, and an assistant renders its whole form, which may hold
|
|
/// more than a hundred fields. Ten renderings a second still read as a live stream.
|
|
/// </remarks>
|
|
private protected static readonly TimeSpan STREAMING_RENDER_MIN_TIME = TimeSpan.FromMilliseconds(100);
|
|
|
|
/// <summary>
|
|
/// The minimum time between two checkpoints of the assistant session while an answer streams in,
|
|
/// as in the chat.
|
|
/// </summary>
|
|
private protected static readonly TimeSpan STREAMING_CHECKPOINT_MIN_TIME = TimeSpan.FromSeconds(3);
|
|
|
|
protected static readonly AssistantSessionStateKey<AIStudio.Settings.Provider> PROVIDER_SETTINGS_STATE_KEY = new(nameof(ProviderSettings));
|
|
protected static readonly AssistantSessionStateKey<bool> INPUT_IS_VALID_STATE_KEY = new(nameof(InputIsValid), isDerived: true);
|
|
protected static readonly AssistantSessionStateKey<Profile> CURRENT_PROFILE_STATE_KEY = new(nameof(CurrentProfile));
|
|
protected static readonly AssistantSessionStateKey<ChatTemplate> CURRENT_CHAT_TEMPLATE_STATE_KEY = new(nameof(CurrentChatTemplate));
|
|
protected static readonly AssistantSessionStateKey<ChatThread?> CHAT_THREAD_STATE_KEY = new(nameof(ChatThread));
|
|
protected static readonly AssistantSessionStateKey<IContent?> LAST_USER_PROMPT_STATE_KEY = new(nameof(LastUserPrompt));
|
|
protected static readonly AssistantSessionStateKey<ContentBlock?> RESULTING_CONTENT_BLOCK_STATE_KEY = new(nameof(ResultingContentBlock));
|
|
protected static readonly AssistantSessionStateKey<string[]> INPUT_ISSUES_STATE_KEY = new(nameof(InputIssues), isDerived: true);
|
|
protected static readonly AssistantSessionStateKey<bool> IS_PROCESSING_STATE_KEY = new(nameof(IsProcessing), isKeptOutOfDrafts: true);
|
|
protected static readonly AssistantSessionStateKey<HashSet<string>> SELECTED_TOOL_IDS_STATE_KEY = new("SelectedToolIds");
|
|
protected static readonly AssistantSessionStateKey<ChatRestrictions> TRANSFERRED_RESTRICTIONS_STATE_KEY = new("TransferredRestrictions");
|
|
protected static readonly AssistantSessionStateKey<string?> CUSTOM_CHAT_SYSTEM_PROMPT_STATE_KEY = new("CustomChatSystemPromptOfRun");
|
|
|
|
protected AIStudio.Settings.Provider ProviderSettings = Settings.Provider.NONE;
|
|
protected bool InputIsValid;
|
|
protected Profile CurrentProfile = Profile.NO_PROFILE;
|
|
protected ChatTemplate CurrentChatTemplate = ChatTemplate.NO_CHAT_TEMPLATE;
|
|
protected ChatThread? ChatThread;
|
|
protected IContent? LastUserPrompt;
|
|
|
|
protected ContentBlock? ResultingContentBlock;
|
|
protected string[] InputIssues = [];
|
|
protected bool IsProcessing;
|
|
|
|
/// <summary>
|
|
/// Clears everything one assistant run has produced.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// Assistants call this whenever the previous run must not carry over: a follow-up run would
|
|
/// otherwise append to the old chat thread, and the old result would stay on screen.
|
|
/// </remarks>
|
|
protected void ClearConversationState()
|
|
{
|
|
this.ChatThread = null;
|
|
this.LastUserPrompt = null;
|
|
this.ResultingContentBlock = null;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Decides whether an assistant which the user leaves stores what it holds as a draft.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// A text sent to the assistant is kept in any case. Otherwise, a draft needs a change from the state
|
|
/// with the defaults: a draft of just the defaults would bring back old defaults after the user changed
|
|
/// them in the settings. Only fields which count as a change are compared, see
|
|
/// IAssistantSessionSnapshotField.CountsAsChange: whether the form is valid, for example, follows from
|
|
/// the inputs and does not count as a change on its own.
|
|
/// </remarks>
|
|
/// <param name="state">The state the assistant holds now.</param>
|
|
/// <param name="stateWithDefaults">The state the assistant held once its defaults were applied.</param>
|
|
/// <param name="holdsSentText">Whether the assistant holds a text sent to it.</param>
|
|
/// <returns><c>true</c> when the assistant stores a draft; otherwise, <c>false</c>.</returns>
|
|
internal static bool IsWorthADraft(IReadOnlyDictionary<string, IAssistantSessionSnapshotField> state, IReadOnlyDictionary<string, IAssistantSessionSnapshotField> stateWithDefaults, bool holdsSentText)
|
|
{
|
|
if (holdsSentText)
|
|
return true;
|
|
|
|
var changeableFields = state.Where(field => field.Value.CountsAsChange).ToList();
|
|
var holdsSameValues = changeableFields.Count == stateWithDefaults.Count(field => field.Value.CountsAsChange)
|
|
&& changeableFields.All(field => stateWithDefaults.TryGetValue(field.Key, out var otherField) && field.Value.HasSameValueAs(otherField));
|
|
|
|
return !holdsSameValues;
|
|
}
|
|
} |