mirror of
https://github.com/MindWorkAI/AI-Studio.git
synced 2026-12-03 03:54:24 +00:00
Co-authored-by: Thorsten Sommer <SommerEngineering@users.noreply.github.com>
256 lines
11 KiB
C#
256 lines
11 KiB
C#
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;
|
|
|
|
/// <summary>
|
|
/// An assistant which works on a whole document as well as on a text, e.g., translates either of them.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// <para>
|
|
/// 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.
|
|
/// </para>
|
|
/// <para>
|
|
/// An assistant which overrides ResetForm, CaptureCustomAssistantSessionState, or
|
|
/// RestoreCustomAssistantSessionState calls the base as well: it keeps the mode and the document.
|
|
/// </para>
|
|
/// <para>
|
|
/// 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.
|
|
/// </para>
|
|
/// </remarks>
|
|
/// <typeparam name="TSettings">The dialog with the settings of the assistant.</typeparam>
|
|
public abstract partial class DocumentAssistantBase<TSettings> : AssistantBaseCore<TSettings> where TSettings : IComponent
|
|
{
|
|
/// <summary>
|
|
/// What the assistant works on right now; undecided until the user chose.
|
|
/// </summary>
|
|
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
|
|
|
|
/// <summary>
|
|
/// How the assistant starts, as its settings say.
|
|
/// </summary>
|
|
protected abstract AssistantStartMode StartMode { get; }
|
|
|
|
/// <summary>
|
|
/// The two options the user chooses from, one for the document and one for the text.
|
|
/// </summary>
|
|
protected abstract IReadOnlyList<AssistantModeOption<AssistantContentMode>> ModeOptions { get; }
|
|
|
|
/// <summary>
|
|
/// The tooltip of the button next to the settings, which switches to the given mode, e.g., "Switch to text translation".
|
|
/// </summary>
|
|
/// <param name="target">The mode the button switches to.</param>
|
|
protected abstract string SwitchTooltip(AssistantContentMode target);
|
|
|
|
/// <summary>
|
|
/// What the model does with each text of a document, as the form stands when the run starts.
|
|
/// </summary>
|
|
protected abstract DocumentRequest CreateDocumentRequest();
|
|
|
|
/// <summary>
|
|
/// The title of the dialog in which the user selects a document.
|
|
/// </summary>
|
|
protected abstract string DocumentDialogTitle { get; }
|
|
|
|
/// <summary>
|
|
/// What the result of a document is called, e.g., "Translated document".
|
|
/// </summary>
|
|
protected abstract string DocumentResultTitle { get; }
|
|
|
|
/// <summary>
|
|
/// The text of the button which saves the new document, which titles the save dialog as well.
|
|
/// </summary>
|
|
protected abstract string DocumentSaveText { get; }
|
|
|
|
/// <summary>
|
|
/// Tells the user what became of the document, e.g., into which language it was translated.
|
|
/// </summary>
|
|
/// <param name="document">The document the assistant worked on.</param>
|
|
protected abstract string DocumentDoneText(EditedDocument document);
|
|
|
|
/// <summary>
|
|
/// Tells the user how many sections of the document the assistant changed.
|
|
/// </summary>
|
|
/// <param name="results">What became of the segments of the document.</param>
|
|
protected abstract string DocumentSectionsText(SegmentResults results);
|
|
|
|
/// <summary>
|
|
/// Puts a text which another assistant or a chat sent here into the field for the text.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// The assistant works on the text then, so ApplyTextLaunchPayload switched to the text before.
|
|
/// </remarks>
|
|
/// <param name="payload">The text sent.</param>
|
|
protected abstract void ApplyTextLaunchPayloadToText(TextLaunchPayload payload);
|
|
|
|
/// <summary>
|
|
/// The text of the last run and its result, which the assistant shows as a diff; NONE for no diff.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// The diff is no part of the form, so it leaves with the text and comes back with it.
|
|
/// </remarks>
|
|
protected virtual AssistantTextDiff TextResultDiff => AssistantTextDiff.NONE;
|
|
|
|
/// <summary>
|
|
/// The buttons below the result of a text, e.g., to send it to another assistant.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// A document gets none: it becomes a file of its own, which the summary of the document offers to save.
|
|
/// </remarks>
|
|
protected virtual IReadOnlyList<IButtonData> 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<string>? AssistantManagedToolIds => this.Mode is AssistantContentMode.DOCUMENT ? FrozenSet<string>.Empty : null;
|
|
|
|
protected override bool SubmitDisabled => this.isSavingDocument;
|
|
|
|
protected sealed override IReadOnlyList<IButtonData> 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;
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
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);
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
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
|
|
|
|
/// <summary>
|
|
/// 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.
|
|
/// </summary>
|
|
private AssistantContentMode? OtherMode => this.Mode switch
|
|
{
|
|
AssistantContentMode.DOCUMENT => AssistantContentMode.TEXT,
|
|
AssistantContentMode.TEXT => AssistantContentMode.DOCUMENT,
|
|
|
|
_ => null,
|
|
};
|
|
|
|
/// <summary>
|
|
/// The document whose summary shows below the submit button: once a run is over, while the assistant works on a document.
|
|
/// </summary>
|
|
private EditedDocument? ShownEditedDocument => this.Mode is AssistantContentMode.DOCUMENT && !this.IsProcessing ? this.editedDocument : null;
|
|
|
|
/// <summary>
|
|
/// Switches what the assistant works on, as the user chose with the cards or with the button next to the settings.
|
|
/// </summary>
|
|
/// <param name="chosenMode">The mode the user chose.</param>
|
|
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);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Shows the diff between the text of the last run and its result, while the assistant works on a text.
|
|
/// </summary>
|
|
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);
|
|
}
|
|
} |