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 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"; /// /// The minimum time between two renderings of the assistant while an answer streams in. /// /// /// 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. /// private protected static readonly TimeSpan STREAMING_RENDER_MIN_TIME = TimeSpan.FromMilliseconds(100); /// /// The minimum time between two checkpoints of the assistant session while an answer streams in, /// as in the chat. /// private protected static readonly TimeSpan STREAMING_CHECKPOINT_MIN_TIME = TimeSpan.FromSeconds(3); protected static readonly AssistantSessionStateKey PROVIDER_SETTINGS_STATE_KEY = new(nameof(ProviderSettings)); protected static readonly AssistantSessionStateKey INPUT_IS_VALID_STATE_KEY = new(nameof(InputIsValid), isDerived: true); protected static readonly AssistantSessionStateKey CURRENT_PROFILE_STATE_KEY = new(nameof(CurrentProfile)); protected static readonly AssistantSessionStateKey CURRENT_CHAT_TEMPLATE_STATE_KEY = new(nameof(CurrentChatTemplate)); protected static readonly AssistantSessionStateKey CHAT_THREAD_STATE_KEY = new(nameof(ChatThread)); protected static readonly AssistantSessionStateKey LAST_USER_PROMPT_STATE_KEY = new(nameof(LastUserPrompt)); protected static readonly AssistantSessionStateKey RESULTING_CONTENT_BLOCK_STATE_KEY = new(nameof(ResultingContentBlock)); protected static readonly AssistantSessionStateKey INPUT_ISSUES_STATE_KEY = new(nameof(InputIssues), isDerived: true); protected static readonly AssistantSessionStateKey IS_PROCESSING_STATE_KEY = new(nameof(IsProcessing), isKeptOutOfDrafts: true); protected static readonly AssistantSessionStateKey> SELECTED_TOOL_IDS_STATE_KEY = new("SelectedToolIds"); protected static readonly AssistantSessionStateKey TRANSFERRED_RESTRICTIONS_STATE_KEY = new("TransferredRestrictions"); protected static readonly AssistantSessionStateKey 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; /// /// Clears everything one assistant run has produced. /// /// /// 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. /// protected void ClearConversationState() { this.ChatThread = null; this.LastUserPrompt = null; this.ResultingContentBlock = null; } /// /// Decides whether an assistant which the user leaves stores what it holds as a draft. /// /// /// 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. /// /// The state the assistant holds now. /// The state the assistant held once its defaults were applied. /// Whether the assistant holds a text sent to it. /// true when the assistant stores a draft; otherwise, false. internal static bool IsWorthADraft(IReadOnlyDictionary state, IReadOnlyDictionary 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; } }