mirror of
https://github.com/MindWorkAI/AI-Studio.git
synced 2026-11-04 18:27:21 +00:00
Fixed assistants undoing your changes while a run you left continues (#1111)
This commit is contained in:
1 parent
31c271eb7b
commit
7f72da628e
8 files changed
+322
-6
No files matched your search
@@ -65,4 +65,23 @@ public sealed record AssistantSessionSnapshot
|
||||
/// Gets whether the session is still running or canceling.
|
||||
/// </summary>
|
||||
public bool IsActive => this.Status is AssistantSessionStatus.RUNNING or AssistantSessionStatus.CANCELING;
|
||||
|
||||
/// <summary>
|
||||
/// Gets the state fields which are new or hold another value than in an earlier snapshot of the same run.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// A component which shows a run it did not start takes over only these, so that whatever the
|
||||
/// running assistant left alone stays as the user has it in that component.
|
||||
/// </remarks>
|
||||
/// <param name="previous">The snapshot taken over last, or null for none.</param>
|
||||
/// <returns>The whole state when there is no earlier snapshot of the same run; otherwise, the new and changed fields.</returns>
|
||||
public IReadOnlyDictionary<string, IAssistantSessionSnapshotField> StateChangedSince(AssistantSessionSnapshot? previous)
|
||||
{
|
||||
if (previous is null || previous.SessionId != this.SessionId)
|
||||
return this.State;
|
||||
|
||||
return this.State
|
||||
.Where(field => !previous.State.TryGetValue(field.Key, out var previousField) || !previousField.HasSameValueAs(field.Value))
|
||||
.ToDictionary(field => field.Key, field => field.Value, StringComparer.Ordinal);
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,6 @@
|
||||
// ReSharper disable MemberCanBePrivate.Global
|
||||
using System.Collections;
|
||||
|
||||
namespace AIStudio.Tools.AssistantSessions;
|
||||
|
||||
/// <summary>
|
||||
@@ -42,4 +44,37 @@ public sealed record AssistantSessionSnapshotField<T> : IAssistantSessionSnapsho
|
||||
value = default;
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public bool HasSameValueAs(IAssistantSessionSnapshotField other) => other is AssistantSessionSnapshotField<T> otherField && AreSame(this.Value, otherField.Value);
|
||||
|
||||
private static bool AreSame(object? value, object? otherValue)
|
||||
{
|
||||
if (ReferenceEquals(value, otherValue))
|
||||
return true;
|
||||
|
||||
if (value is null || otherValue is null)
|
||||
return false;
|
||||
|
||||
// Nobody can change a string or a copied value type behind our back:
|
||||
if (value is string || value.GetType().IsValueType)
|
||||
return value.Equals(otherValue);
|
||||
|
||||
//
|
||||
// The writer copies lists, sets, and dictionaries for every snapshot, so these never share a
|
||||
// reference. Their items, though, are those of the assistant. Any other object may change
|
||||
// without a new reference, e.g., a record holding a list, so only its reference tells:
|
||||
//
|
||||
if (value is IDictionary dictionary && otherValue is IDictionary otherDictionary)
|
||||
return dictionary.Count == otherDictionary.Count && dictionary.Keys.Cast<object>().All(key => otherDictionary.Contains(key) && AreSame(dictionary[key], otherDictionary[key]));
|
||||
|
||||
if (value is IEnumerable items && otherValue is IEnumerable otherItems)
|
||||
{
|
||||
var itemList = items.Cast<object?>().ToList();
|
||||
var otherItemList = otherItems.Cast<object?>().ToList();
|
||||
return itemList.Count == otherItemList.Count && itemList.Zip(otherItemList).All(pair => AreSame(pair.First, pair.Second));
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -17,4 +17,17 @@ public interface IAssistantSessionSnapshotField
|
||||
/// <param name="value">The typed value when reading succeeds.</param>
|
||||
/// <returns><c>true</c> when the captured value matches <typeparamref name="T"/>; otherwise, <c>false</c>.</returns>
|
||||
bool TryRead<T>(out T? value);
|
||||
|
||||
/// <summary>
|
||||
/// Tells whether another field provably holds the same value as this one.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// The same means: the same captured type, and the same reference, an equal string or value of a
|
||||
/// value type, or a collection whose items are the same by this rule in turn. Everything else counts
|
||||
/// as changed. A field wrongly taken for changed is only restored once more, while a field wrongly
|
||||
/// taken for the same would lose an update.
|
||||
/// </remarks>
|
||||
/// <param name="other">The field to compare with.</param>
|
||||
/// <returns><c>true</c> when both fields provably hold the same value; otherwise, <c>false</c>.</returns>
|
||||
bool HasSameValueAs(IAssistantSessionSnapshotField other);
|
||||
}
|
||||
Reference in new issue
Block a user