mirror of
https://github.com/microsoft/agent-framework.git
synced 2026-06-16 21:04:09 +08:00
b03a4fb95e
* Initial plan * Infrastructure setup * Plan for minimal client * Plan update * Basic agentic chat * cleanup * Cleanups * More cleanups * Cleanups * More cleanups * Test plan * Sample * Fix streaming and error handling * Fix notifications * Cleanups * cleanup sample * Additional tests * Additional tests * Run dotnet format * Remove unnecessary files * Mark packages as non packable * Fix build * Address feedback * Fix build * Fix remaining warnings * Feedback * Feedback and cleanup * Cleanup * Cleanups * Cleanups * Cleanups * Retrieve existing messages from the store to send them along the way and update the sample client * Run dotnet format * Add ADR for AG-UI * Switch to use the SG and use a convention for run ids * Cleanup MapAGUI API * Fix formatting * Fix solution * Fix solution
166 lines
6.2 KiB
C#
166 lines
6.2 KiB
C#
// Copyright (c) Microsoft. All rights reserved.
|
|
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.Agents.AI.Hosting.AGUI.AspNetCore.Shared;
|
|
using Microsoft.Extensions.AI;
|
|
|
|
namespace Microsoft.Agents.AI.Hosting.AGUI.AspNetCore.UnitTests;
|
|
|
|
public sealed class AgentRunResponseUpdateAGUIExtensionsTests
|
|
{
|
|
[Fact]
|
|
public async Task AsAGUIEventStreamAsync_YieldsRunStartedEvent_AtBeginningWithCorrectIdsAsync()
|
|
{
|
|
// Arrange
|
|
const string ThreadId = "thread1";
|
|
const string RunId = "run1";
|
|
List<AgentRunResponseUpdate> updates = [];
|
|
|
|
// Act
|
|
List<BaseEvent> events = [];
|
|
await foreach (BaseEvent evt in updates.ToAsyncEnumerableAsync().AsAGUIEventStreamAsync(ThreadId, RunId, CancellationToken.None))
|
|
{
|
|
events.Add(evt);
|
|
}
|
|
|
|
// Assert
|
|
Assert.NotEmpty(events);
|
|
RunStartedEvent startEvent = Assert.IsType<RunStartedEvent>(events.First());
|
|
Assert.Equal(ThreadId, startEvent.ThreadId);
|
|
Assert.Equal(RunId, startEvent.RunId);
|
|
Assert.Equal(AGUIEventTypes.RunStarted, startEvent.Type);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task AsAGUIEventStreamAsync_YieldsRunFinishedEvent_AtEndWithCorrectIdsAsync()
|
|
{
|
|
// Arrange
|
|
const string ThreadId = "thread1";
|
|
const string RunId = "run1";
|
|
List<AgentRunResponseUpdate> updates = [];
|
|
|
|
// Act
|
|
List<BaseEvent> events = [];
|
|
await foreach (BaseEvent evt in updates.ToAsyncEnumerableAsync().AsAGUIEventStreamAsync(ThreadId, RunId, CancellationToken.None))
|
|
{
|
|
events.Add(evt);
|
|
}
|
|
|
|
// Assert
|
|
Assert.NotEmpty(events);
|
|
RunFinishedEvent finishEvent = Assert.IsType<RunFinishedEvent>(events.Last());
|
|
Assert.Equal(ThreadId, finishEvent.ThreadId);
|
|
Assert.Equal(RunId, finishEvent.RunId);
|
|
Assert.Equal(AGUIEventTypes.RunFinished, finishEvent.Type);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task AsAGUIEventStreamAsync_ConvertsTextContentUpdates_ToTextMessageEventsAsync()
|
|
{
|
|
// Arrange
|
|
const string ThreadId = "thread1";
|
|
const string RunId = "run1";
|
|
List<AgentRunResponseUpdate> updates =
|
|
[
|
|
new AgentRunResponseUpdate(new ChatResponseUpdate(ChatRole.Assistant, "Hello") { MessageId = "msg1" }),
|
|
new AgentRunResponseUpdate(new ChatResponseUpdate(ChatRole.Assistant, " World") { MessageId = "msg1" })
|
|
];
|
|
|
|
// Act
|
|
List<BaseEvent> events = [];
|
|
await foreach (BaseEvent evt in updates.ToAsyncEnumerableAsync().AsAGUIEventStreamAsync(ThreadId, RunId, CancellationToken.None))
|
|
{
|
|
events.Add(evt);
|
|
}
|
|
|
|
// Assert
|
|
Assert.Contains(events, e => e is TextMessageStartEvent);
|
|
Assert.Contains(events, e => e is TextMessageContentEvent);
|
|
Assert.Contains(events, e => e is TextMessageEndEvent);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task AsAGUIEventStreamAsync_GroupsConsecutiveUpdates_WithSameMessageIdAsync()
|
|
{
|
|
// Arrange
|
|
const string ThreadId = "thread1";
|
|
const string RunId = "run1";
|
|
const string MessageId = "msg1";
|
|
List<AgentRunResponseUpdate> updates =
|
|
[
|
|
new AgentRunResponseUpdate(new ChatResponseUpdate(ChatRole.Assistant, "Hello") { MessageId = MessageId }),
|
|
new AgentRunResponseUpdate(new ChatResponseUpdate(ChatRole.Assistant, " ") { MessageId = MessageId }),
|
|
new AgentRunResponseUpdate(new ChatResponseUpdate(ChatRole.Assistant, "World") { MessageId = MessageId })
|
|
];
|
|
|
|
// Act
|
|
List<BaseEvent> events = [];
|
|
await foreach (BaseEvent evt in updates.ToAsyncEnumerableAsync().AsAGUIEventStreamAsync(ThreadId, RunId, CancellationToken.None))
|
|
{
|
|
events.Add(evt);
|
|
}
|
|
|
|
// Assert
|
|
List<TextMessageStartEvent> startEvents = events.OfType<TextMessageStartEvent>().ToList();
|
|
List<TextMessageEndEvent> endEvents = events.OfType<TextMessageEndEvent>().ToList();
|
|
Assert.Single(startEvents);
|
|
Assert.Single(endEvents);
|
|
Assert.Equal(MessageId, startEvents[0].MessageId);
|
|
Assert.Equal(MessageId, endEvents[0].MessageId);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task AsAGUIEventStreamAsync_WithRoleChanges_EmitsProperTextMessageStartEventsAsync()
|
|
{
|
|
// Arrange
|
|
const string ThreadId = "thread1";
|
|
const string RunId = "run1";
|
|
List<AgentRunResponseUpdate> updates =
|
|
[
|
|
new AgentRunResponseUpdate(new ChatResponseUpdate(ChatRole.Assistant, "Hello") { MessageId = "msg1" }),
|
|
new AgentRunResponseUpdate(new ChatResponseUpdate(ChatRole.User, "Hi") { MessageId = "msg2" })
|
|
];
|
|
|
|
// Act
|
|
List<BaseEvent> events = [];
|
|
await foreach (BaseEvent evt in updates.ToAsyncEnumerableAsync().AsAGUIEventStreamAsync(ThreadId, RunId, CancellationToken.None))
|
|
{
|
|
events.Add(evt);
|
|
}
|
|
|
|
// Assert
|
|
List<TextMessageStartEvent> startEvents = events.OfType<TextMessageStartEvent>().ToList();
|
|
Assert.Equal(2, startEvents.Count);
|
|
Assert.Equal("msg1", startEvents[0].MessageId);
|
|
Assert.Equal("msg2", startEvents[1].MessageId);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task AsAGUIEventStreamAsync_EmitsTextMessageEndEvent_WhenMessageIdChangesAsync()
|
|
{
|
|
// Arrange
|
|
const string ThreadId = "thread1";
|
|
const string RunId = "run1";
|
|
List<AgentRunResponseUpdate> updates =
|
|
[
|
|
new AgentRunResponseUpdate(new ChatResponseUpdate(ChatRole.Assistant, "First") { MessageId = "msg1" }),
|
|
new AgentRunResponseUpdate(new ChatResponseUpdate(ChatRole.Assistant, "Second") { MessageId = "msg2" })
|
|
];
|
|
|
|
// Act
|
|
List<BaseEvent> events = [];
|
|
await foreach (BaseEvent evt in updates.ToAsyncEnumerableAsync().AsAGUIEventStreamAsync(ThreadId, RunId, CancellationToken.None))
|
|
{
|
|
events.Add(evt);
|
|
}
|
|
|
|
// Assert
|
|
List<TextMessageEndEvent> endEvents = events.OfType<TextMessageEndEvent>().ToList();
|
|
Assert.NotEmpty(endEvents);
|
|
Assert.Contains(endEvents, e => e.MessageId == "msg1");
|
|
}
|
|
}
|