using System.Collections.Frozen; using AIStudio.Components; using AIStudio.Settings.DataModel; using AIStudio.Tools.AssistantLaunch; using AIStudio.Tools.AssistantSessions; using AIStudio.Tools.DocumentSegments; using Microsoft.AspNetCore.Components; namespace AIStudio.Assistants; /// /// An assistant which works on a whole document as well as on a text, e.g., translates either of them. /// /// /// /// It asks at the start what to work on, unless its settings decide, and a button next to its settings /// switches between both. A document goes through the document segments, see the AGENTS.md in /// Tools/DocumentSegments/, and becomes a new file of the same type, which the user saves. Everything this /// takes lives here, so the assistant itself brings only its prompts, the texts which name what it does, and /// its fields for a text. /// /// /// An assistant which overrides ResetForm, CaptureCustomAssistantSessionState, or /// RestoreCustomAssistantSessionState calls the base as well: it keeps the mode and the document. /// /// /// The assistant renders its own form, see AssistantBaseCore: until the user chose, an AssistantModeChoice with /// ModeOptions and ChooseMode, and a SelectDocument with DocumentPath while it works on a document. The switch /// next to the settings and the summary of a document go into the extension points of AssistantBase, which /// DocumentAssistantBase.razor fills with one component each. /// /// /// The dialog with the settings of the assistant. public abstract partial class DocumentAssistantBase : AssistantBaseCore where TSettings : IComponent { /// /// What the assistant works on right now; undecided until the user chose. /// protected AssistantContentMode Mode { get; private set; } [Inject] private LastContentModeService LastContentModes { get; init; } = null!; private string TB(string fallbackEN) => this.T(fallbackEN, typeof(DocumentAssistantBase<>).Namespace, nameof(DocumentAssistantBase<>)); private new string TA(string fallbackEN) => this.TA(fallbackEN, typeof(DocumentAssistantBase<>).Namespace, nameof(DocumentAssistantBase<>)); #region What the assistant brings /// /// How the assistant starts, as its settings say. /// protected abstract AssistantStartMode StartMode { get; } /// /// The two options the user chooses from, one for the document and one for the text. /// protected abstract IReadOnlyList> ModeOptions { get; } /// /// The tooltip of the button next to the settings, which switches to the given mode, e.g., "Switch to text translation". /// /// The mode the button switches to. protected abstract string SwitchTooltip(AssistantContentMode target); /// /// What the model does with each text of a document, as the form stands when the run starts. /// protected abstract DocumentRequest CreateDocumentRequest(); /// /// The title of the dialog in which the user selects a document. /// protected abstract string DocumentDialogTitle { get; } /// /// What the result of a document is called, e.g., "Translated document". /// protected abstract string DocumentResultTitle { get; } /// /// The text of the button which saves the new document, which titles the save dialog as well. /// protected abstract string DocumentSaveText { get; } /// /// Tells the user what became of the document, e.g., into which language it was translated. /// /// The document the assistant worked on. protected abstract string DocumentDoneText(EditedDocument document); /// /// Tells the user how many sections of the document the assistant changed. /// /// What became of the segments of the document. protected abstract string DocumentSectionsText(SegmentResults results); /// /// Puts a text which another assistant or a chat sent here into the field for the text. /// /// /// The assistant works on the text then, so ApplyTextLaunchPayload switched to the text before. /// /// The text sent. protected abstract void ApplyTextLaunchPayloadToText(TextLaunchPayload payload); /// /// The text of the last run and its result, which the assistant shows as a diff; NONE for no diff. /// /// /// The diff is no part of the form, so it leaves with the text and comes back with it. /// protected virtual AssistantTextDiff TextResultDiff => AssistantTextDiff.NONE; /// /// The buttons below the result of a text, e.g., to send it to another assistant. /// /// /// A document gets none: it becomes a file of its own, which the summary of the document offers to save. /// protected virtual IReadOnlyList TextFooterButtons => []; #endregion #region Overrides of AssistantBase protected override bool ShowSubmit => this.Mode is not AssistantContentMode.UNDECIDED; protected override bool ShowReset => this.Mode is not AssistantContentMode.UNDECIDED; protected override bool ShowResult => this.Mode is AssistantContentMode.TEXT; protected override bool ShowCopyResult => this.Mode is AssistantContentMode.TEXT; protected override bool ShowSendTo => this.Mode is AssistantContentMode.TEXT; protected override bool ShowDedicatedProgress => this.Mode is AssistantContentMode.DOCUMENT; protected override string ProgressText => this.Mode is AssistantContentMode.DOCUMENT ? this.DocumentProgressText : string.Empty; // A document goes to the model in many small requests, which need no tools: protected override IReadOnlySet? AssistantManagedToolIds => this.Mode is AssistantContentMode.DOCUMENT ? FrozenSet.Empty : null; protected override bool SubmitDisabled => this.isSavingDocument; protected sealed override IReadOnlyList FooterButtons => this.Mode is AssistantContentMode.TEXT ? this.TextFooterButtons : []; protected sealed override void ApplyTextLaunchPayload(TextLaunchPayload payload) { this.Mode = AssistantContentMode.TEXT; this.ApplyTextLaunchPayloadToText(payload); } protected override async Task OnDefaultsAppliedAsync() { // A text sent here chose the text mode already, and a reset keeps the mode the user chose. // A run which is attached afterward brings its own mode, a draft does not: if (this.Mode is AssistantContentMode.UNDECIDED) this.Mode = this.StartMode.InitialContentMode(this.LastContentModes.LastOf(this.Component)); await base.OnDefaultsAppliedAsync(); } protected override void ResetForm() { // The mode stays as it is: the button next to the settings switches it. this.DocumentPath = string.Empty; this.editedDocument = null; this.documentProgress = null; } /// protected override void CaptureCustomAssistantSessionState(AssistantSessionStateWriter state) { state.Set(DocumentAssistantStateKeys.MODE, this.Mode); state.Set(DocumentAssistantStateKeys.DOCUMENT_PATH, this.DocumentPath); state.Set(DocumentAssistantStateKeys.EDITED_DOCUMENT, this.editedDocument); state.Set(DocumentAssistantStateKeys.DOCUMENT_PROGRESS, this.documentProgress); } /// protected override void RestoreCustomAssistantSessionState(AssistantSessionStateReader state) { state.Restore(DocumentAssistantStateKeys.MODE, value => this.Mode = value); state.Restore(DocumentAssistantStateKeys.DOCUMENT_PATH, value => this.DocumentPath = value); state.Restore(DocumentAssistantStateKeys.EDITED_DOCUMENT, value => this.editedDocument = value); state.Restore(DocumentAssistantStateKeys.DOCUMENT_PROGRESS, value => this.documentProgress = value); } protected override async Task OnAssistantSessionRenderedAsync(AssistantSessionSnapshot snapshot) { if (!snapshot.IsActive) await this.ShowTextResultDiffAsync(); await base.OnAssistantSessionRenderedAsync(snapshot); } #endregion /// /// The mode the assistant does not work on right now, to which the button next to the settings switches; null /// while the user has not chosen yet. /// private AssistantContentMode? OtherMode => this.Mode switch { AssistantContentMode.DOCUMENT => AssistantContentMode.TEXT, AssistantContentMode.TEXT => AssistantContentMode.DOCUMENT, _ => null, }; /// /// The document whose summary shows below the submit button: once a run is over, while the assistant works on a document. /// private EditedDocument? ShownEditedDocument => this.Mode is AssistantContentMode.DOCUMENT && !this.IsProcessing ? this.editedDocument : null; /// /// Switches what the assistant works on, as the user chose with the cards or with the button next to the settings. /// /// The mode the user chose. private protected async Task ChooseMode(AssistantContentMode chosenMode) { this.Mode = chosenMode; this.LastContentModes.Remember(this.Component, chosenMode); // The issues of the fields of the other mode would stay below the form otherwise: if (this.Form is not null) await this.Form.ResetValidationAsync(); if (chosenMode is AssistantContentMode.TEXT) { await this.ShowTextResultDiffAsync(); return; } if (this.TextResultDiff.IsShown) { await this.JsRuntime.ClearDiv(BEFORE_RESULT_DIV_ID); await this.JsRuntime.ClearDiv(RESULT_DIV_ID); } } /// /// Shows the diff between the text of the last run and its result, while the assistant works on a text. /// protected async Task ShowTextResultDiffAsync() { var diff = this.TextResultDiff; if (this.IsAssistantComponentDisposed || this.Mode is not AssistantContentMode.TEXT || !diff.IsShown) return; await this.JsRuntime.GenerateAndShowDiff(diff.Original, diff.Changed); } }