mirror of
https://github.com/MindWorkAI/AI-Studio.git
synced 2026-12-05 05:24:35 +00:00
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:
1 parent
1fbc1f4796
commit
7e2f83b19b
83 files changed
+1924
-347
No files matched your search
@@ -88,6 +88,11 @@ public sealed class AssistantSessionService(MessageBus messageBus)
|
||||
/// </summary>
|
||||
private readonly ConcurrentDictionary<AssistantSessionKey, AssistantSessionState> sessions = new();
|
||||
|
||||
/// <summary>
|
||||
/// Stores one draft per session key, see <see cref="SaveDraft"/>.
|
||||
/// </summary>
|
||||
private readonly ConcurrentDictionary<AssistantSessionKey, AssistantSessionSnapshot> drafts = new();
|
||||
|
||||
/// <summary>
|
||||
/// Gets whether at least one assistant session is still active.
|
||||
/// </summary>
|
||||
@@ -142,6 +147,47 @@ public sealed class AssistantSessionService(MessageBus messageBus)
|
||||
return ((ICollection<KeyValuePair<AssistantSessionKey, AssistantSessionState>>)this.sessions).Remove(new(key, session)) ? snapshot : null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Stores what an assistant holds when the user leaves it, so that it is there again when the user comes back.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// A draft is not a run: neither <see cref="GetSnapshots"/> nor <see cref="HasActiveSessions"/> sees it, so
|
||||
/// the assistant overview shows no status for it, and AI Studio may close without asking. It lives in
|
||||
/// memory only, until AI Studio closes, the user resets the assistant, or a text is sent to it. A newer
|
||||
/// draft replaces the older one. A draft holds everything its assistant captures, such as the chat
|
||||
/// thread, attached files, or the results of a batch, so there is at most one per assistant.
|
||||
/// </remarks>
|
||||
/// <param name="key">The assistant session key.</param>
|
||||
/// <param name="title">The user-visible assistant title.</param>
|
||||
/// <param name="chatThread">The assistant chat thread, if one exists.</param>
|
||||
/// <param name="state">The assistant component state.</param>
|
||||
public void SaveDraft(AssistantSessionKey key, string title, ChatThread? chatThread, Dictionary<string, IAssistantSessionSnapshotField> state)
|
||||
{
|
||||
var now = DateTimeOffset.Now;
|
||||
this.drafts[key] = new()
|
||||
{
|
||||
SessionId = Guid.NewGuid(),
|
||||
Key = key,
|
||||
Title = title,
|
||||
Status = AssistantSessionStatus.NONE,
|
||||
StartedAt = now,
|
||||
UpdatedAt = now,
|
||||
ChatThread = chatThread,
|
||||
State = new Dictionary<string, IAssistantSessionSnapshotField>(state, StringComparer.Ordinal),
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tries to get and remove the draft of an assistant.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// The assistant takes its draft once it opens and keeps it until the user leaves it again, which
|
||||
/// stores a new draft. So a draft never lives in two places.
|
||||
/// </remarks>
|
||||
/// <param name="key">The assistant session key to look up and remove.</param>
|
||||
/// <returns>The removed draft, or <c>null</c> when there is none.</returns>
|
||||
public AssistantSessionSnapshot? TryTakeDraft(AssistantSessionKey key) => this.drafts.TryRemove(key, out var draft) ? draft : null;
|
||||
|
||||
/// <summary>
|
||||
/// Starts a new assistant session when no active session exists for the key.
|
||||
/// </summary>
|
||||
@@ -279,11 +325,12 @@ public sealed class AssistantSessionService(MessageBus messageBus)
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Clears an inactive assistant session.
|
||||
/// Clears an inactive assistant session and the draft of the assistant.
|
||||
/// </summary>
|
||||
/// <param name="key">The assistant session key to clear.</param>
|
||||
public async Task ClearAsync(AssistantSessionKey key)
|
||||
{
|
||||
this.drafts.TryRemove(key, out _);
|
||||
if (!this.sessions.TryGetValue(key, out var session))
|
||||
return;
|
||||
|
||||
@@ -304,11 +351,14 @@ public sealed class AssistantSessionService(MessageBus messageBus)
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Clears all inactive sessions for a component.
|
||||
/// Clears all inactive sessions and all drafts for a component.
|
||||
/// </summary>
|
||||
/// <param name="component">The component whose inactive sessions should be cleared.</param>
|
||||
/// <param name="component">The component whose inactive sessions and drafts should be cleared.</param>
|
||||
public async Task ClearInactiveSessionsForComponentAsync(Components component)
|
||||
{
|
||||
foreach (var draftKey in this.drafts.Keys.Where(key => key.Component == component))
|
||||
this.drafts.TryRemove(draftKey, out _);
|
||||
|
||||
var matchingKeys = this.sessions
|
||||
.Where(pair => pair.Key.Component == component && pair.Value.Status is not AssistantSessionStatus.RUNNING and not AssistantSessionStatus.CANCELING)
|
||||
.Select(pair => pair.Key)
|
||||
|
||||
@@ -13,9 +13,11 @@ public sealed record AssistantSessionSnapshotField<T> : IAssistantSessionSnapsho
|
||||
/// Initializes a new typed snapshot field.
|
||||
/// </summary>
|
||||
/// <param name="value">The captured value.</param>
|
||||
public AssistantSessionSnapshotField(T value)
|
||||
/// <param name="countsAsChange">Whether another value of this field means that the assistant changed, see CountsAsChange.</param>
|
||||
public AssistantSessionSnapshotField(T value, bool countsAsChange = true)
|
||||
{
|
||||
this.Value = value;
|
||||
this.CountsAsChange = countsAsChange;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -26,6 +28,9 @@ public sealed record AssistantSessionSnapshotField<T> : IAssistantSessionSnapsho
|
||||
/// <inheritdoc />
|
||||
public Type ValueType => typeof(T);
|
||||
|
||||
/// <inheritdoc />
|
||||
public bool CountsAsChange { get; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public bool TryRead<TValue>(out TValue? value)
|
||||
{
|
||||
@@ -56,6 +61,10 @@ public sealed record AssistantSessionSnapshotField<T> : IAssistantSessionSnapsho
|
||||
if (value is null || otherValue is null)
|
||||
return false;
|
||||
|
||||
// A value which a snapshot holds as a copy of its own, e.g., the state of an assistant plugin, compares its content itself:
|
||||
if (value is IAssistantSessionValue sessionValue)
|
||||
return sessionValue.HasSameValueAs(otherValue);
|
||||
|
||||
// Nobody can change a string or a copied value type behind our back:
|
||||
if (value is string || value.GetType().IsValueType)
|
||||
return value.Equals(otherValue);
|
||||
|
||||
@@ -10,9 +10,13 @@ public readonly record struct AssistantSessionStateKey<T>
|
||||
/// Initializes a new assistant session state key.
|
||||
/// </summary>
|
||||
/// <param name="name">The stable dictionary name used in assistant session snapshots.</param>
|
||||
public AssistantSessionStateKey(string name)
|
||||
/// <param name="isKeptOutOfDrafts">Whether a draft leaves the value out, see <see cref="IsKeptOutOfDrafts"/>.</param>
|
||||
/// <param name="isDerived">Whether the value follows from other values, see IsDerived.</param>
|
||||
public AssistantSessionStateKey(string name, bool isKeptOutOfDrafts = false, bool isDerived = false)
|
||||
{
|
||||
this.Name = name;
|
||||
this.IsKeptOutOfDrafts = isKeptOutOfDrafts;
|
||||
this.IsDerived = isDerived;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -20,6 +24,28 @@ public readonly record struct AssistantSessionStateKey<T>
|
||||
/// </summary>
|
||||
public string Name { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets whether an assistant restores the value from a run only, never from a draft.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// This is meant for flags of work in progress, such as reading a web page. An assistant stores a draft
|
||||
/// when the user leaves it while no run goes on, and work outside of a run ends with the component which
|
||||
/// did it. A flag restored from a draft would therefore stay set and could keep the form locked. Left
|
||||
/// out, it keeps the value which a freshly opened assistant starts with.
|
||||
/// </remarks>
|
||||
public bool IsKeptOutOfDrafts { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets whether the value follows from other values, such as whether the form is valid, or is a cache.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// A draft restores such a value, but it does not count as a change of the assistant: when the user leaves an
|
||||
/// assistant, it stores a draft only when something else changed, see AssistantLowerBase.IsWorthADraft.
|
||||
/// A cache which fills while the assistant renders, e.g., the images of an assistant plugin, would
|
||||
/// otherwise make every assistant look changed as soon as it was shown.
|
||||
/// </remarks>
|
||||
public bool IsDerived { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Returns the stable dictionary name.
|
||||
/// </summary>
|
||||
|
||||
@@ -7,10 +7,21 @@ namespace AIStudio.Tools.AssistantSessions;
|
||||
/// </summary>
|
||||
/// <param name="fields">The captured snapshot fields.</param>
|
||||
/// <param name="assistantTitle">The user-visible assistant title.</param>
|
||||
public sealed class AssistantSessionStateReader(IReadOnlyDictionary<string, IAssistantSessionSnapshotField> fields, string assistantTitle)
|
||||
/// <param name="isDraft">Whether the fields come from a draft, which an assistant stored when the user left it, rather than from a run. A draft restores no field whose key is kept out of drafts, see <see cref="AssistantSessionStateKey{T}.IsKeptOutOfDrafts"/>.</param>
|
||||
public sealed class AssistantSessionStateReader(IReadOnlyDictionary<string, IAssistantSessionSnapshotField> fields, string assistantTitle, bool isDraft = false)
|
||||
{
|
||||
private static readonly ILogger<AssistantSessionStateReader> LOG = Program.LOGGER_FACTORY.CreateLogger<AssistantSessionStateReader>();
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Gets whether the fields come from a draft rather than from a run.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// An assistant whose state partly follows from something outside of it, such as a plugin or a policy in
|
||||
/// the settings, asks this to restore from a draft only what the user entered: the rest may have changed
|
||||
/// while the user was elsewhere.
|
||||
/// </remarks>
|
||||
public bool IsDraft => isDraft;
|
||||
|
||||
/// <summary>
|
||||
/// Restores a typed value when it exists in the snapshot.
|
||||
/// </summary>
|
||||
@@ -94,6 +105,9 @@ public sealed class AssistantSessionStateReader(IReadOnlyDictionary<string, IAss
|
||||
private bool TryRead<T>(AssistantSessionStateKey<T> key, out T? value)
|
||||
{
|
||||
value = default;
|
||||
if (isDraft && key.IsKeptOutOfDrafts)
|
||||
return false;
|
||||
|
||||
if (!fields.TryGetValue(key.Name, out var field))
|
||||
return false;
|
||||
|
||||
|
||||
@@ -20,7 +20,7 @@ public sealed class AssistantSessionStateWriter
|
||||
/// <param name="value">The captured value.</param>
|
||||
public void Set<T>(AssistantSessionStateKey<T> key, T value)
|
||||
{
|
||||
this.fields[key.Name] = new AssistantSessionSnapshotField<T>(value);
|
||||
this.fields[key.Name] = new AssistantSessionSnapshotField<T>(value, countsAsChange: !key.IsDerived && !key.IsKeptOutOfDrafts);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -10,6 +10,16 @@ public interface IAssistantSessionSnapshotField
|
||||
/// </summary>
|
||||
Type ValueType { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets whether another value of this field means that the assistant changed.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Not so for a value which follows from other values or is a cache, see AssistantSessionStateKey.IsDerived,
|
||||
/// nor for a flag of work in progress, see AssistantSessionStateKey.IsKeptOutOfDrafts: neither is a change
|
||||
/// the user made.
|
||||
/// </remarks>
|
||||
bool CountsAsChange { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Tries to read the captured value as the requested type.
|
||||
/// </summary>
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
namespace AIStudio.Tools.AssistantSessions;
|
||||
|
||||
/// <summary>
|
||||
/// A value of an assistant session which tells by itself whether another value holds the same.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// A snapshot compares most values by their reference, since an object may change without getting a new
|
||||
/// one, see IAssistantSessionSnapshotField.HasSameValueAs. A value which the assistant captures as a copy
|
||||
/// of its own, such as the state of an assistant plugin, gets a new reference with every snapshot, though,
|
||||
/// and would always count as changed. Such a value compares its content instead.
|
||||
/// </remarks>
|
||||
public interface IAssistantSessionValue
|
||||
{
|
||||
/// <summary>
|
||||
/// Tells whether another value holds the same content as this one.
|
||||
/// </summary>
|
||||
/// <param name="other">The value to compare with.</param>
|
||||
/// <returns><c>true</c> when both values hold the same content; otherwise, <c>false</c>.</returns>
|
||||
bool HasSameValueAs(object other);
|
||||
}
|
||||
Reference in new issue
Block a user