// Copyright (c) Microsoft. All rights reserved. using System.Collections.Generic; using System.Text.Json; using System.Threading; using System.Threading.Tasks; using Microsoft.Extensions.AI; using Microsoft.Extensions.AI.Agents; namespace Microsoft.Agents.Workflows; internal class WorkflowMessageStore : IChatMessageStore { private int _bookmark = 0; private readonly List _chatMessages = new(); internal class StoreState { public int Bookmark { get; set; } public IList Messages { get; set; } = new List(); } internal void AddMessages(params ChatMessage[] messages) { this._chatMessages.AddRange(messages); } public Task AddMessagesAsync(IEnumerable messages, CancellationToken cancellationToken) { this._chatMessages.AddRange(messages); return Task.CompletedTask; } public Task> GetMessagesAsync(CancellationToken cancellationToken) { return Task.FromResult>(this._chatMessages.AsReadOnly()); } public IEnumerable GetFromBookmark() { for (int i = this._bookmark; i < this._chatMessages.Count; i++) { yield return this._chatMessages[i]; } } public void UpdateBookmark() { this._bookmark = this._chatMessages.Count; } public ValueTask DeserializeStateAsync(JsonElement? serializedStoreState, JsonSerializerOptions? jsonSerializerOptions = null, CancellationToken cancellationToken = default) { if (serializedStoreState is null) { return default; } object? maybeState = JsonSerializer.Deserialize( serializedStoreState.Value, AgentAbstractionsJsonUtilities.DefaultOptions.GetTypeInfo(typeof(StoreState))); if (maybeState is not StoreState state) { throw new JsonException("Invalid state format for WorkflowMessageStore."); } this._chatMessages.Clear(); this._chatMessages.AddRange(state.Messages); this._bookmark = state.Bookmark; return default; } public ValueTask SerializeStateAsync(JsonSerializerOptions? jsonSerializerOptions = null, CancellationToken cancellationToken = default) { StoreState state = new() { Bookmark = this._bookmark, Messages = this._chatMessages, }; return new ValueTask (JsonSerializer.SerializeToElement(state, WorkflowsJsonUtilities.DefaultOptions.GetTypeInfo(typeof(StoreState)))); } }