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
This commit is contained in:
Roger Barreto
2026-04-07 13:56:07 +01:00
committed by GitHub
Unverified
parent 86b49d800e
commit 4134c74060
2 changed files with 63 additions and 0 deletions
@@ -109,6 +109,27 @@ public sealed class FoundryAgent : DelegatingAIAgent
#region Convenience methods
/// <summary>
/// Creates a new agent session instance using an existing conversation identifier to continue that conversation.
/// </summary>
/// <param name="conversationId">The identifier of an existing conversation to continue.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests.</param>
/// <returns>
/// A value task representing the asynchronous operation. The task result contains a new <see cref="AgentSession"/> instance configured to work with the specified conversation.
/// </returns>
/// <remarks>
/// <para>
/// This method creates an <see cref="AgentSession"/> that relies on server-side chat history storage, where the chat history
/// is maintained by the underlying AI service rather than by a local <see cref="ChatHistoryProvider"/>.
/// </para>
/// <para>
/// Agent sessions created with this method will only work with <see cref="FoundryAgent"/>
/// instances that support server-side conversation storage through their underlying <see cref="IChatClient"/>.
/// </para>
/// </remarks>
public ValueTask<AgentSession> CreateSessionAsync(string conversationId, CancellationToken cancellationToken = default)
=> ((ChatClientAgent)this.InnerAgent).CreateSessionAsync(conversationId, cancellationToken);
/// <summary>
/// Creates a server-side conversation session that appears in the Foundry Project UI.
/// </summary>
@@ -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<ChatClientAgentSession>(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<ChatClientAgentSession>(session);
Assert.Null(chatSession.ConversationId);
}
#endregion
#region Functional tests
[Fact]