using AIStudio.Chat; namespace AIStudio.Tools.AssistantSessions; /// /// Immutable-style view of an assistant session for UI consumers and message bus events. /// /// /// The service creates snapshots by copying its internal runtime state. Consumers must /// treat the contained chat thread and state objects as read-only views of the session. /// public sealed record AssistantSessionSnapshot { /// /// Identifies the concrete run represented by this snapshot. /// public required Guid SessionId { get; init; } /// /// Identifies the assistant and logical session slot represented by this snapshot. /// public required AssistantSessionKey Key { get; init; } /// /// Gets the user-visible assistant title. /// public required string Title { get; init; } /// /// Gets the current lifecycle status. /// public required AssistantSessionStatus Status { get; init; } /// /// Gets when the session run started. /// public required DateTimeOffset StartedAt { get; init; } /// /// Gets when the session was last changed. /// public required DateTimeOffset UpdatedAt { get; init; } /// /// Gets when the session reached a terminal state. /// public DateTimeOffset? FinishedAt { get; init; } /// /// Gets the user-visible error message for failed sessions. /// public string ErrorMessage { get; init; } = string.Empty; /// /// Gets the assistant chat thread captured for this session. /// public ChatThread? ChatThread { get; init; } /// /// Gets the assistant component state captured for this session. /// public IReadOnlyDictionary State { get; init; } = new Dictionary(); /// /// Gets whether the session is still running or canceling. /// public bool IsActive => this.Status is AssistantSessionStatus.RUNNING or AssistantSessionStatus.CANCELING; /// /// Gets the state fields which are new or hold another value than in an earlier snapshot of the same run. /// /// /// 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. /// /// The snapshot taken over last, or null for none. /// The whole state when there is no earlier snapshot of the same run; otherwise, the new and changed fields. public IReadOnlyDictionary 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); } }