// Copyright (c) Microsoft. All rights reserved. using System; using System.Threading; using System.Threading.Tasks; using Microsoft.Shared.Diagnostics; namespace Microsoft.Agents.AI.Hosting; /// /// Provides an abstract base class for agent session stores that delegate operations to an inner store /// instance while allowing for extensibility and customization. /// /// /// /// implements the decorator pattern for s, /// enabling the creation of pipelines where each layer can add functionality while delegating core operations to an /// underlying store. /// /// /// The default implementation provides transparent pass-through behavior, forwarding all operations to the inner store. /// Derived classes can override specific methods to add custom behavior while maintaining compatibility with the store /// interface. /// /// public abstract class DelegatingAgentSessionStore : AgentSessionStore { /// /// Initializes a new instance of the class with the specified inner /// store. /// /// The underlying session store instance that will handle the core operations. /// is . /// /// The inner session store serves as the foundation of the delegation chain. All operations not overridden by /// derived classes will be forwarded to this store. /// protected DelegatingAgentSessionStore(AgentSessionStore innerStore) { this.InnerStore = Throw.IfNull(innerStore); } /// /// Gets the inner session store instance that receives delegated operations. /// /// /// The underlying instance that handles core storage operations. /// /// /// Derived classes can use this property to access the inner session store for custom delegation scenarios /// or to forward operations with additional processing. /// protected AgentSessionStore InnerStore { get; } /// public override ValueTask GetSessionAsync(AIAgent agent, string conversationId, CancellationToken cancellationToken = default) => this.InnerStore.GetSessionAsync(agent, conversationId, cancellationToken); /// public override ValueTask SaveSessionAsync(AIAgent agent, string conversationId, AgentSession session, CancellationToken cancellationToken = default) => this.InnerStore.SaveSessionAsync(agent, conversationId, session, cancellationToken); /// /// /// This implementation first checks if this instance satisfies the service request. /// If not, it chains the request to the inner store, allowing services to be retrieved /// from any store in the delegation chain. /// public override object? GetService(Type serviceType, object? serviceKey = null) { // First, check if this instance satisfies the request object? service = base.GetService(serviceType, serviceKey); if (service is not null) { return service; } // Chain to the inner store return this.InnerStore.GetService(serviceType, serviceKey); } }