mirror of
https://github.com/microsoft/agent-framework.git
synced 2026-06-16 21:04:09 +08:00
715769e649
* Add ADR for different run response options * Add another option to the list. * Update agno non-streaming with further clarification * Add another option * Adding optional includeUpdates option * Adding Pros/Cons for each option * Make pros/cons a list * Add some thoughts on structured outputs and custom AIContent types * Update design doc to clarify primary and secondary better and split out custom response types with it's own options * Add structured outputs competitive comparison and suggestion * Address PR comments. * Remove AgentRunFinishReason until we can find a good use case for it. * Add finish reason to list of excluded properties. * Add custom agent run response types. Usage to follow. * Update Agent run response types * Add additional code coverage * Remove onIntermediateMessage since it is unecessary with the new response approach. * Add AgentId to response. * Rename ParseAsStructuredOutput to Deserialize * Update decision doc. * Fix formatting. * Update CopilotStudio to return new response types * Address PR comment Co-authored-by: Roger Barreto <19890735+rogerbarreto@users.noreply.github.com> --------- Co-authored-by: Roger Barreto <19890735+rogerbarreto@users.noreply.github.com>
162 lines
5.9 KiB
C#
162 lines
5.9 KiB
C#
// Copyright (c) Microsoft. All rights reserved.
|
|
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Text.Json;
|
|
|
|
namespace Microsoft.Extensions.AI.Agents.Abstractions.UnitTests;
|
|
|
|
public class AgentRunResponseUpdateTests
|
|
{
|
|
[Fact]
|
|
public void ConstructorPropsDefaulted()
|
|
{
|
|
AgentRunResponseUpdate update = new();
|
|
Assert.Null(update.AuthorName);
|
|
Assert.Null(update.Role);
|
|
Assert.Empty(update.Text);
|
|
Assert.Empty(update.Contents);
|
|
Assert.Null(update.RawRepresentation);
|
|
Assert.Null(update.AdditionalProperties);
|
|
Assert.Null(update.ResponseId);
|
|
Assert.Null(update.MessageId);
|
|
Assert.Null(update.CreatedAt);
|
|
Assert.Equal(string.Empty, update.ToString());
|
|
}
|
|
|
|
[Fact]
|
|
public void PropertiesRoundtrip()
|
|
{
|
|
AgentRunResponseUpdate update = new();
|
|
|
|
Assert.Null(update.AuthorName);
|
|
update.AuthorName = "author";
|
|
Assert.Equal("author", update.AuthorName);
|
|
|
|
Assert.Null(update.Role);
|
|
update.Role = ChatRole.Assistant;
|
|
Assert.Equal(ChatRole.Assistant, update.Role);
|
|
|
|
Assert.Empty(update.Contents);
|
|
update.Contents.Add(new TextContent("text"));
|
|
Assert.Single(update.Contents);
|
|
Assert.Equal("text", update.Text);
|
|
Assert.Same(update.Contents, update.Contents);
|
|
IList<AIContent> newList = [new TextContent("text")];
|
|
update.Contents = newList;
|
|
Assert.Same(newList, update.Contents);
|
|
update.Contents = null;
|
|
Assert.NotNull(update.Contents);
|
|
Assert.Empty(update.Contents);
|
|
|
|
Assert.Empty(update.Text);
|
|
|
|
Assert.Null(update.RawRepresentation);
|
|
object raw = new();
|
|
update.RawRepresentation = raw;
|
|
Assert.Same(raw, update.RawRepresentation);
|
|
|
|
Assert.Null(update.AdditionalProperties);
|
|
AdditionalPropertiesDictionary props = new() { ["key"] = "value" };
|
|
update.AdditionalProperties = props;
|
|
Assert.Same(props, update.AdditionalProperties);
|
|
|
|
Assert.Null(update.ResponseId);
|
|
update.ResponseId = "id";
|
|
Assert.Equal("id", update.ResponseId);
|
|
|
|
Assert.Null(update.MessageId);
|
|
update.MessageId = "messageid";
|
|
Assert.Equal("messageid", update.MessageId);
|
|
|
|
Assert.Null(update.CreatedAt);
|
|
update.CreatedAt = new DateTimeOffset(2022, 1, 1, 0, 0, 0, TimeSpan.Zero);
|
|
Assert.Equal(new DateTimeOffset(2022, 1, 1, 0, 0, 0, TimeSpan.Zero), update.CreatedAt);
|
|
}
|
|
|
|
[Fact]
|
|
public void TextGetUsesAllTextContent()
|
|
{
|
|
AgentRunResponseUpdate update = new()
|
|
{
|
|
Role = ChatRole.User,
|
|
Contents =
|
|
[
|
|
new DataContent("data:image/audio;base64,aGVsbG8="),
|
|
new DataContent("data:image/image;base64,aGVsbG8="),
|
|
new FunctionCallContent("callId1", "fc1"),
|
|
new TextContent("text-1"),
|
|
new TextContent("text-2"),
|
|
new FunctionResultContent("callId1", "result"),
|
|
],
|
|
};
|
|
|
|
TextContent textContent = Assert.IsType<TextContent>(update.Contents[3]);
|
|
Assert.Equal("text-1", textContent.Text);
|
|
Assert.Equal("text-1text-2", update.Text);
|
|
Assert.Equal("text-1text-2", update.ToString());
|
|
|
|
((TextContent)update.Contents[3]).Text = "text-3";
|
|
Assert.Equal("text-3text-2", update.Text);
|
|
Assert.Same(textContent, update.Contents[3]);
|
|
Assert.Equal("text-3text-2", update.ToString());
|
|
}
|
|
|
|
[Fact]
|
|
public void JsonSerializationRoundtrips()
|
|
{
|
|
AgentRunResponseUpdate original = new()
|
|
{
|
|
AuthorName = "author",
|
|
Role = ChatRole.Assistant,
|
|
Contents =
|
|
[
|
|
new TextContent("text-1"),
|
|
new DataContent("data:image/png;base64,aGVsbG8="),
|
|
new FunctionCallContent("callId1", "fc1"),
|
|
new DataContent("data"u8.ToArray(), "text/plain"),
|
|
new TextContent("text-2"),
|
|
],
|
|
RawRepresentation = new object(),
|
|
ResponseId = "id",
|
|
MessageId = "messageid",
|
|
CreatedAt = new DateTimeOffset(2022, 1, 1, 0, 0, 0, TimeSpan.Zero),
|
|
AdditionalProperties = new() { ["key"] = "value" },
|
|
};
|
|
|
|
string json = JsonSerializer.Serialize(original, TestJsonSerializerContext.Default.AgentRunResponseUpdate);
|
|
|
|
AgentRunResponseUpdate? result = JsonSerializer.Deserialize(json, TestJsonSerializerContext.Default.AgentRunResponseUpdate);
|
|
|
|
Assert.NotNull(result);
|
|
Assert.Equal(5, result.Contents.Count);
|
|
|
|
Assert.IsType<TextContent>(result.Contents[0]);
|
|
Assert.Equal("text-1", ((TextContent)result.Contents[0]).Text);
|
|
|
|
Assert.IsType<DataContent>(result.Contents[1]);
|
|
Assert.Equal("data:image/png;base64,aGVsbG8=", ((DataContent)result.Contents[1]).Uri);
|
|
|
|
Assert.IsType<FunctionCallContent>(result.Contents[2]);
|
|
Assert.Equal("fc1", ((FunctionCallContent)result.Contents[2]).Name);
|
|
|
|
Assert.IsType<DataContent>(result.Contents[3]);
|
|
Assert.Equal("data"u8.ToArray(), ((DataContent)result.Contents[3]).Data.ToArray());
|
|
|
|
Assert.IsType<TextContent>(result.Contents[4]);
|
|
Assert.Equal("text-2", ((TextContent)result.Contents[4]).Text);
|
|
|
|
Assert.Equal("author", result.AuthorName);
|
|
Assert.Equal(ChatRole.Assistant, result.Role);
|
|
Assert.Equal("id", result.ResponseId);
|
|
Assert.Equal("messageid", result.MessageId);
|
|
Assert.Equal(new DateTimeOffset(2022, 1, 1, 0, 0, 0, TimeSpan.Zero), result.CreatedAt);
|
|
|
|
Assert.NotNull(result.AdditionalProperties);
|
|
Assert.Single(result.AdditionalProperties);
|
|
Assert.True(result.AdditionalProperties.TryGetValue("key", out object? value));
|
|
Assert.IsType<JsonElement>(value);
|
|
Assert.Equal("value", ((JsonElement)value!).GetString());
|
|
}
|
|
}
|