From 4134c74060d37db60c600a5b748a4f7bfeb2ae41 Mon Sep 17 00:00:00 2001 From: Roger Barreto <19890735+rogerbarreto@users.noreply.github.com> Date: Tue, 7 Apr 2026 13:56:07 +0100 Subject: [PATCH] Add CreateSessionAsync(conversationId) to FoundryAgent (#5144) Adds a public CreateSessionAsync(string conversationId, CancellationToken) method to FoundryAgent that delegates to the inner ChatClientAgent, allowing users to create sessions with existing server-side conversation IDs. Fixes #5138 --- .../FoundryAgent.cs | 21 ++++++++++ .../FoundryAgentTests.cs | 42 +++++++++++++++++++ 2 files changed, 63 insertions(+) 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]