using System.Text;
namespace AIStudio.Tools.AssistantSessions;
///
/// Collects typed assistant session state values for a snapshot.
///
public sealed class AssistantSessionStateWriter
{
///
/// Stores captured fields by their stable dictionary names.
///
private readonly Dictionary fields = new(StringComparer.Ordinal);
///
/// Stores a typed state value.
///
/// The value type.
/// The typed state key.
/// The captured value.
public void Set(AssistantSessionStateKey key, T value)
{
this.fields[key.Name] = new AssistantSessionSnapshotField(value);
}
///
/// Stores a list copy.
///
/// The list item type.
/// The typed state key.
/// The values to copy.
public void SetList(AssistantSessionStateKey> key, IEnumerable values)
{
this.Set(key, values.ToList());
}
///
/// Stores a hash set copy.
///
/// The set item type.
/// The typed state key.
/// The values to copy.
public void SetHashSet(AssistantSessionStateKey> key, IEnumerable values)
{
this.Set(key, values.ToHashSet());
}
///
/// Stores a dictionary copy.
///
/// The dictionary key type.
/// The dictionary value type.
/// The typed state key.
/// The values to copy.
public void SetDictionary(AssistantSessionStateKey> key, IDictionary values) where TKey : notnull
{
this.Set(key, new Dictionary(values));
}
///
/// Stores the current text from a string builder.
///
/// The typed state key.
/// The string builder to read.
public void SetStringBuilder(AssistantSessionStateKey key, StringBuilder value)
{
this.Set(key, value.ToString());
}
///
/// Returns the captured fields as a dictionary.
///
/// A copied dictionary containing the captured fields.
public Dictionary ToDictionary()
{
return new(this.fields, StringComparer.Ordinal);
}
}