diff --git a/dotnet/src/Microsoft.Agents.AI.A2A/A2AAgent.cs b/dotnet/src/Microsoft.Agents.AI.A2A/A2AAgent.cs index 5601653e8f..18d8bfacdd 100644 --- a/dotnet/src/Microsoft.Agents.AI.A2A/A2AAgent.cs +++ b/dotnet/src/Microsoft.Agents.AI.A2A/A2AAgent.cs @@ -63,7 +63,16 @@ protected sealed override ValueTask CreateSessionCoreAsync(Cancell /// The context id to continue. /// A value task representing the asynchronous operation. The task result contains a new instance. public ValueTask CreateSessionAsync(string contextId) - => new(new A2AAgentSession() { ContextId = contextId }); + => new(new A2AAgentSession() { ContextId = Throw.IfNullOrWhitespace(contextId) }); + + /// + /// Get a new instance using an existing context id and task id, to resume that conversation from a specific task. + /// + /// The context id to continue. + /// The task id to resume from. + /// A value task representing the asynchronous operation. The task result contains a new instance. + public ValueTask CreateSessionAsync(string contextId, string taskId) + => new(new A2AAgentSession() { ContextId = Throw.IfNullOrWhitespace(contextId), TaskId = Throw.IfNullOrWhitespace(taskId) }); /// protected override ValueTask SerializeSessionCoreAsync(AgentSession session, JsonSerializerOptions? jsonSerializerOptions = null, CancellationToken cancellationToken = default) diff --git a/dotnet/tests/Microsoft.Agents.AI.A2A.UnitTests/A2AAgentTests.cs b/dotnet/tests/Microsoft.Agents.AI.A2A.UnitTests/A2AAgentTests.cs index d6e736a528..50d83c140d 100644 --- a/dotnet/tests/Microsoft.Agents.AI.A2A.UnitTests/A2AAgentTests.cs +++ b/dotnet/tests/Microsoft.Agents.AI.A2A.UnitTests/A2AAgentTests.cs @@ -1146,6 +1146,100 @@ public void GetService_RequestingAIAgentMetadata_ReturnsConsistentMetadata() Assert.Equal("a2a", metadata.ProviderName); } + /// + /// Verify that CreateSessionAsync with contextId creates a session with the correct context ID. + /// + [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(session); + var typedSession = (A2AAgentSession)session; + Assert.Equal(ContextId, typedSession.ContextId); + Assert.Null(typedSession.TaskId); + } + + /// + /// Verify that CreateSessionAsync with contextId and taskId creates a session with both IDs set correctly. + /// + [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(session); + var typedSession = (A2AAgentSession)session; + Assert.Equal(ContextId, typedSession.ContextId); + Assert.Equal(TaskId, typedSession.TaskId); + } + + /// + /// Verify that CreateSessionAsync throws when contextId is null, empty, or whitespace. + /// + [Theory] + [InlineData(null)] + [InlineData("")] + [InlineData(" ")] + [InlineData("\t")] + [InlineData("\r\n")] + public async Task CreateSessionAsync_WithInvalidContextId_ThrowsArgumentExceptionAsync(string? contextId) + { + // Act & Assert + await Assert.ThrowsAnyAsync(async () => + await this._agent.CreateSessionAsync(contextId!)); + } + + /// + /// Verify that CreateSessionAsync with both parameters throws when contextId is null, empty, or whitespace. + /// + [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(async () => + await this._agent.CreateSessionAsync(contextId!, TaskId)); + } + + /// + /// Verify that CreateSessionAsync with both parameters throws when taskId is null, empty, or whitespace. + /// + [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(async () => + await this._agent.CreateSessionAsync(ContextId, taskId!)); + } #endregion public void Dispose()