// Copyright (c) Microsoft. All rights reserved.
using System.Collections.Generic;
using System.Text.Json;
using System.Threading.Tasks;
using Microsoft.Agents.AI.Workflows.Checkpointing;
namespace Microsoft.Agents.AI.Workflows;
///
/// A manager for storing and retrieving workflow execution checkpoints.
///
public sealed class CheckpointManager : ICheckpointManager
{
private readonly ICheckpointManager _impl;
private static CheckpointManagerImpl CreateImpl(
IWireMarshaller marshaller,
ICheckpointStore store)
{
return new CheckpointManagerImpl(marshaller, store);
}
internal CheckpointManager(ICheckpointManager impl)
{
this._impl = impl;
}
///
/// Creates a new instance of that uses the specified marshaller and store.
///
///
public static CheckpointManager CreateInMemory() => new(new InMemoryCheckpointManager());
///
/// Gets the default in-memory checkpoint manager instance.
///
public static CheckpointManager Default { get; } = CreateInMemory();
///
/// Creates a new instance of the CheckpointManager that uses JSON serialization for checkpoint data.
///
/// The checkpoint store to use for persisting and retrieving checkpoint data as JSON elements. Cannot be null.
/// Optional custom JSON serializer options to use for serialization and deserialization. Must be provided if
/// using custom types in messages or state.
/// A CheckpointManager instance configured to serialize checkpoint data as JSON.
public static CheckpointManager CreateJson(ICheckpointStore store, JsonSerializerOptions? customOptions = null)
{
JsonMarshaller marshaller = new(customOptions);
return new(CreateImpl(marshaller, store));
}
ValueTask ICheckpointManager.CommitCheckpointAsync(string sessionId, Checkpoint checkpoint)
=> this._impl.CommitCheckpointAsync(sessionId, checkpoint);
ValueTask ICheckpointManager.LookupCheckpointAsync(string sessionId, CheckpointInfo checkpointInfo)
=> this._impl.LookupCheckpointAsync(sessionId, checkpointInfo);
ValueTask> ICheckpointManager.RetrieveIndexAsync(string sessionId, CheckpointInfo? withParent)
=> this._impl.RetrieveIndexAsync(sessionId, withParent);
}