diff --git a/dotnet/src/Microsoft.Agents.AI.Foundry/FoundryAgent.cs b/dotnet/src/Microsoft.Agents.AI.Foundry/FoundryAgent.cs index b1d97a5c9c..6a40c129ec 100644 --- a/dotnet/src/Microsoft.Agents.AI.Foundry/FoundryAgent.cs +++ b/dotnet/src/Microsoft.Agents.AI.Foundry/FoundryAgent.cs @@ -109,6 +109,27 @@ public sealed class FoundryAgent : DelegatingAIAgent #region Convenience methods + /// + /// Creates a new agent session instance using an existing conversation identifier to continue that conversation. + /// + /// The identifier of an existing conversation to continue. + /// The to monitor for cancellation requests. + /// + /// A value task representing the asynchronous operation. The task result contains a new instance configured to work with the specified conversation. + /// + /// + /// + /// This method creates an that relies on server-side chat history storage, where the chat history + /// is maintained by the underlying AI service rather than by a local . + /// + /// + /// Agent sessions created with this method will only work with + /// instances that support server-side conversation storage through their underlying . + /// + /// + public ValueTask CreateSessionAsync(string conversationId, CancellationToken cancellationToken = default) + => ((ChatClientAgent)this.InnerAgent).CreateSessionAsync(conversationId, cancellationToken); + /// /// Creates a server-side conversation session that appears in the Foundry Project UI. /// diff --git a/dotnet/tests/Microsoft.Agents.AI.Foundry.UnitTests/FoundryAgentTests.cs b/dotnet/tests/Microsoft.Agents.AI.Foundry.UnitTests/FoundryAgentTests.cs index 1ddc8c7c82..31f981d5c6 100644 --- a/dotnet/tests/Microsoft.Agents.AI.Foundry.UnitTests/FoundryAgentTests.cs +++ b/dotnet/tests/Microsoft.Agents.AI.Foundry.UnitTests/FoundryAgentTests.cs @@ -196,6 +196,48 @@ public class FoundryAgentTests #endregion + #region CreateSessionAsync tests + + [Fact] + public async Task CreateSessionAsync_WithConversationId_ReturnsChatClientAgentSessionAsync() + { + // Arrange + FoundryAgent agent = new( + s_testEndpoint, + new FakeAuthenticationTokenProvider(), + model: "gpt-4o-mini", + instructions: "Test"); + + const string ConversationId = "test-conversation-id"; + + // Act + AgentSession session = await agent.CreateSessionAsync(ConversationId); + + // Assert + ChatClientAgentSession chatSession = Assert.IsType(session); + Assert.Equal(ConversationId, chatSession.ConversationId); + } + + [Fact] + public async Task CreateSessionAsync_WithoutConversationId_ReturnsChatClientAgentSessionWithoutConversationIdAsync() + { + // Arrange + FoundryAgent agent = new( + s_testEndpoint, + new FakeAuthenticationTokenProvider(), + model: "gpt-4o-mini", + instructions: "Test"); + + // Act + AgentSession session = await agent.CreateSessionAsync(); + + // Assert + ChatClientAgentSession chatSession = Assert.IsType(session); + Assert.Null(chatSession.ConversationId); + } + + #endregion + #region Functional tests [Fact]