diff --git a/dotnet/packages/Microsoft.TeamsAI/Microsoft.TeamsAI.Tests/AITests/PromptsTests/SectionsTests/ActionOutputMessageSectionTests.cs b/dotnet/packages/Microsoft.TeamsAI/Microsoft.TeamsAI.Tests/AITests/PromptsTests/SectionsTests/ActionOutputMessageSectionTests.cs new file mode 100644 index 000000000..6b7a3e882 --- /dev/null +++ b/dotnet/packages/Microsoft.TeamsAI/Microsoft.TeamsAI.Tests/AITests/PromptsTests/SectionsTests/ActionOutputMessageSectionTests.cs @@ -0,0 +1,102 @@ +using Microsoft.Teams.AI.AI.Models; +using Microsoft.Teams.AI.AI.Prompts.Sections; +using Microsoft.Teams.AI.State; +using Microsoft.Teams.AI.Tests.TestUtils; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Microsoft.Teams.AI.Tests.AITests.PromptsTests.SectionsTests +{ + public class ActionOutputMessageSectionTests + { + [Fact] + public async Task Test_RenderAsMessages_NoHistory_ReturnsEmptyList() + { + // Arrange + var historyVariable = "temp.history"; + var turnContext = TurnStateConfig.CreateConfiguredTurnContext(); + var turnState = await TurnStateConfig.GetTurnStateWithConversationStateAsync(turnContext); + turnState.SetValue(historyVariable, new List() { }); + turnState.SetValue(TempState.ActionOutputsKey, new Dictionary()); + var section = new ActionOutputMessageSection(historyVariable); + + // Act + var sections = await section.RenderAsMessagesAsync(turnContext, turnState, null!, null!, 0); + + // Assert + Assert.Empty(sections.Output); + } + + [Fact] + public async Task Test_RenderAsMessages_HistoryWithoutActionCalls_ReturnsEmptyList() + { + // Arrange + var historyVariable = "temp.history"; + var turnContext = TurnStateConfig.CreateConfiguredTurnContext(); + var turnState = await TurnStateConfig.GetTurnStateWithConversationStateAsync(turnContext); + turnState.SetValue(historyVariable, new List() { new ChatMessage(ChatRole.Assistant) }); + turnState.SetValue(TempState.ActionOutputsKey, new Dictionary()); + var section = new ActionOutputMessageSection(historyVariable); + + // Act + var sections = await section.RenderAsMessagesAsync(turnContext, turnState, null!, null!, 0); + + // Assert + Assert.Empty(sections.Output); + } + + [Fact] + public async Task Test_RenderAsMessages_WithActionCalls_AddsToolMessage() + { + // Arrange + var historyVariable = "temp.history"; + var turnContext = TurnStateConfig.CreateConfiguredTurnContext(); + var turnState = await TurnStateConfig.GetTurnStateWithConversationStateAsync(turnContext); + turnState.SetValue(historyVariable, new List() { + new ChatMessage(ChatRole.Assistant) { + ActionCalls = new List { new ActionCall("testId", new ActionFunction("testName", "{}")) } + } + }); + turnState.SetValue(TempState.ActionOutputsKey, new Dictionary() + { + { "testId", "testOutput" } + }); + var section = new ActionOutputMessageSection(historyVariable); + + // Act + var sections = await section.RenderAsMessagesAsync(turnContext, turnState, null!, null!, 0); + + // Assert + Assert.Single(sections.Output); + Assert.Equal("testOutput", sections.Output[0].Content); + } + + [Fact] + public async Task Test_RenderAsMessages_WithInvalidActionCalls_AddsToolMessage_WithEmptyOutput() + { + // Arrange + var historyVariable = "temp.history"; + var turnContext = TurnStateConfig.CreateConfiguredTurnContext(); + var turnState = await TurnStateConfig.GetTurnStateWithConversationStateAsync(turnContext); + turnState.SetValue(historyVariable, new List() { + new ChatMessage(ChatRole.Assistant) { + ActionCalls = new List { new ActionCall("testId", new ActionFunction("testName", "{}")) } + } + }); + turnState.SetValue(TempState.ActionOutputsKey, new Dictionary() + { + { "InvalidTestId", "testOutput" } + }); + var section = new ActionOutputMessageSection(historyVariable); + + // Act + var sections = await section.RenderAsMessagesAsync(turnContext, turnState, null!, null!, 0); + + // Assert + Assert.Empty(sections.Output); + } + } +} diff --git a/dotnet/packages/Microsoft.TeamsAI/Microsoft.TeamsAI/AI/Prompts/Sections/ActionOutputMessageSection.cs b/dotnet/packages/Microsoft.TeamsAI/Microsoft.TeamsAI/AI/Prompts/Sections/ActionOutputMessageSection.cs index 469b271cd..ee429548f 100644 --- a/dotnet/packages/Microsoft.TeamsAI/Microsoft.TeamsAI/AI/Prompts/Sections/ActionOutputMessageSection.cs +++ b/dotnet/packages/Microsoft.TeamsAI/Microsoft.TeamsAI/AI/Prompts/Sections/ActionOutputMessageSection.cs @@ -31,7 +31,7 @@ public override Task>> RenderAsMessagesA List history = memory.GetValue(_HistoryVariable) as List ?? new(); List messages = new(); - if (history.Count > 1) + if (history.Count >= 1) { Dictionary actionOutputs = memory.GetValue(_OutputVariable) as Dictionary ?? new(); List actionCalls = history.Last().ActionCalls ?? new();