mirror of
https://github.com/microsoft/agent-framework.git
synced 2026-06-16 21:04:09 +08:00
.NET: Add CreateSessionAsync overload with taskId for A2AAgent session resumption (#3924)
* Initial plan * Add CreateSessionAsync overload with contextId and taskId parameters Co-authored-by: westey-m <164392973+westey-m@users.noreply.github.com> * Update dotnet/tests/Microsoft.Agents.AI.A2A.UnitTests/A2AAgentTests.cs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Add parameter validation to CreateSessionAsync methods Co-authored-by: westey-m <164392973+westey-m@users.noreply.github.com> * Inline parameter validation in CreateSessionAsync methods Co-authored-by: westey-m <164392973+westey-m@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: westey-m <164392973+westey-m@users.noreply.github.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
This commit is contained in:
committed by
GitHub
Unverified
parent
b68d0f93e3
commit
7ae4b7b537
@@ -63,7 +63,16 @@ public sealed class A2AAgent : AIAgent
|
||||
/// <param name="contextId">The context id to continue.</param>
|
||||
/// <returns>A value task representing the asynchronous operation. The task result contains a new <see cref="AgentSession"/> instance.</returns>
|
||||
public ValueTask<AgentSession> CreateSessionAsync(string contextId)
|
||||
=> new(new A2AAgentSession() { ContextId = contextId });
|
||||
=> new(new A2AAgentSession() { ContextId = Throw.IfNullOrWhitespace(contextId) });
|
||||
|
||||
/// <summary>
|
||||
/// Get a new <see cref="AgentSession"/> instance using an existing context id and task id, to resume that conversation from a specific task.
|
||||
/// </summary>
|
||||
/// <param name="contextId">The context id to continue.</param>
|
||||
/// <param name="taskId">The task id to resume from.</param>
|
||||
/// <returns>A value task representing the asynchronous operation. The task result contains a new <see cref="AgentSession"/> instance.</returns>
|
||||
public ValueTask<AgentSession> CreateSessionAsync(string contextId, string taskId)
|
||||
=> new(new A2AAgentSession() { ContextId = Throw.IfNullOrWhitespace(contextId), TaskId = Throw.IfNullOrWhitespace(taskId) });
|
||||
|
||||
/// <inheritdoc/>
|
||||
protected override ValueTask<JsonElement> SerializeSessionCoreAsync(AgentSession session, JsonSerializerOptions? jsonSerializerOptions = null, CancellationToken cancellationToken = default)
|
||||
|
||||
@@ -1146,6 +1146,100 @@ public sealed class A2AAgentTests : IDisposable
|
||||
Assert.Equal("a2a", metadata.ProviderName);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Verify that CreateSessionAsync with contextId creates a session with the correct context ID.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public async Task CreateSessionAsync_WithContextId_CreatesSessionWithContextIdAsync()
|
||||
{
|
||||
// Arrange
|
||||
const string ContextId = "test-context-123";
|
||||
|
||||
// Act
|
||||
var session = await this._agent.CreateSessionAsync(ContextId);
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(session);
|
||||
Assert.IsType<A2AAgentSession>(session);
|
||||
var typedSession = (A2AAgentSession)session;
|
||||
Assert.Equal(ContextId, typedSession.ContextId);
|
||||
Assert.Null(typedSession.TaskId);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Verify that CreateSessionAsync with contextId and taskId creates a session with both IDs set correctly.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public async Task CreateSessionAsync_WithContextIdAndTaskId_CreatesSessionWithBothIdsAsync()
|
||||
{
|
||||
// Arrange
|
||||
const string ContextId = "test-context-456";
|
||||
const string TaskId = "test-task-789";
|
||||
|
||||
// Act
|
||||
var session = await this._agent.CreateSessionAsync(ContextId, TaskId);
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(session);
|
||||
Assert.IsType<A2AAgentSession>(session);
|
||||
var typedSession = (A2AAgentSession)session;
|
||||
Assert.Equal(ContextId, typedSession.ContextId);
|
||||
Assert.Equal(TaskId, typedSession.TaskId);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Verify that CreateSessionAsync throws when contextId is null, empty, or whitespace.
|
||||
/// </summary>
|
||||
[Theory]
|
||||
[InlineData(null)]
|
||||
[InlineData("")]
|
||||
[InlineData(" ")]
|
||||
[InlineData("\t")]
|
||||
[InlineData("\r\n")]
|
||||
public async Task CreateSessionAsync_WithInvalidContextId_ThrowsArgumentExceptionAsync(string? contextId)
|
||||
{
|
||||
// Act & Assert
|
||||
await Assert.ThrowsAnyAsync<ArgumentException>(async () =>
|
||||
await this._agent.CreateSessionAsync(contextId!));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Verify that CreateSessionAsync with both parameters throws when contextId is null, empty, or whitespace.
|
||||
/// </summary>
|
||||
[Theory]
|
||||
[InlineData(null)]
|
||||
[InlineData("")]
|
||||
[InlineData(" ")]
|
||||
[InlineData("\t")]
|
||||
[InlineData("\r\n")]
|
||||
public async Task CreateSessionAsync_WithInvalidContextIdAndValidTaskId_ThrowsArgumentExceptionAsync(string? contextId)
|
||||
{
|
||||
// Arrange
|
||||
const string TaskId = "valid-task-id";
|
||||
|
||||
// Act & Assert
|
||||
await Assert.ThrowsAnyAsync<ArgumentException>(async () =>
|
||||
await this._agent.CreateSessionAsync(contextId!, TaskId));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Verify that CreateSessionAsync with both parameters throws when taskId is null, empty, or whitespace.
|
||||
/// </summary>
|
||||
[Theory]
|
||||
[InlineData(null)]
|
||||
[InlineData("")]
|
||||
[InlineData(" ")]
|
||||
[InlineData("\t")]
|
||||
[InlineData("\r\n")]
|
||||
public async Task CreateSessionAsync_WithValidContextIdAndInvalidTaskId_ThrowsArgumentExceptionAsync(string? taskId)
|
||||
{
|
||||
// Arrange
|
||||
const string ContextId = "valid-context-id";
|
||||
|
||||
// Act & Assert
|
||||
await Assert.ThrowsAnyAsync<ArgumentException>(async () =>
|
||||
await this._agent.CreateSessionAsync(ContextId, taskId!));
|
||||
}
|
||||
#endregion
|
||||
|
||||
public void Dispose()
|
||||
|
||||
Reference in New Issue
Block a user