mirror of
https://github.com/microsoft/agent-framework.git
synced 2026-06-16 21:04:09 +08:00
8b4f7d5e29
* Initial plan * Refactor AIAgent: Make RunAsync and RunStreamingAsync non-abstract, add RunCoreAsync and RunCoreStreamingAsync Co-authored-by: SergeyMenshykh <68852919+SergeyMenshykh@users.noreply.github.com> * Fix infinite recursion in test implementations Co-authored-by: SergeyMenshykh <68852919+SergeyMenshykh@users.noreply.github.com> * Make RunAsync and RunStreamingAsync non-virtual as requested Co-authored-by: SergeyMenshykh <68852919+SergeyMenshykh@users.noreply.github.com> * Fix DelegatingAIAgent subclasses to use RunCoreAsync/RunCoreStreamingAsync Co-authored-by: SergeyMenshykh <68852919+SergeyMenshykh@users.noreply.github.com> * Fix XML documentation references in AnonymousDelegatingAIAgent Co-authored-by: SergeyMenshykh <68852919+SergeyMenshykh@users.noreply.github.com> * Restore <see cref> tags with proper qualified signatures in AnonymousDelegatingAIAgent Co-authored-by: SergeyMenshykh <68852919+SergeyMenshykh@users.noreply.github.com> * Rollback unnecessary XML documentation changes in AnonymousDelegatingAIAgent Co-authored-by: SergeyMenshykh <68852919+SergeyMenshykh@users.noreply.github.com> * Remove pragma and update crefs to RunCoreAsync/RunCoreStreamingAsync Co-authored-by: SergeyMenshykh <68852919+SergeyMenshykh@users.noreply.github.com> * Fix EntityAgentWrapper to call base.RunCoreAsync/RunCoreStreamingAsync Co-authored-by: SergeyMenshykh <68852919+SergeyMenshykh@users.noreply.github.com> * fix compilation issues * fix compilatio issue * fix tests * fix unit tests * fix unit test --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: SergeyMenshykh <68852919+SergeyMenshykh@users.noreply.github.com> Co-authored-by: SergeyMenshykh <sergemenshikh@gmail.com> Co-authored-by: Chris <66376200+crickman@users.noreply.github.com>
89 lines
3.8 KiB
C#
89 lines
3.8 KiB
C#
// Copyright (c) Microsoft. All rights reserved.
|
|
|
|
using System.Diagnostics.CodeAnalysis;
|
|
using System.Runtime.CompilerServices;
|
|
using System.Text.Json;
|
|
using Microsoft.Agents.AI;
|
|
using Microsoft.Extensions.AI;
|
|
|
|
namespace AGUIDojoServer.AgenticUI;
|
|
|
|
[SuppressMessage("Performance", "CA1812:Avoid uninstantiated internal classes", Justification = "Instantiated by ChatClientAgentFactory.CreateAgenticUI")]
|
|
internal sealed class AgenticUIAgent : DelegatingAIAgent
|
|
{
|
|
private readonly JsonSerializerOptions _jsonSerializerOptions;
|
|
|
|
public AgenticUIAgent(AIAgent innerAgent, JsonSerializerOptions jsonSerializerOptions)
|
|
: base(innerAgent)
|
|
{
|
|
this._jsonSerializerOptions = jsonSerializerOptions;
|
|
}
|
|
|
|
protected override Task<AgentRunResponse> RunCoreAsync(IEnumerable<ChatMessage> messages, AgentThread? thread = null, AgentRunOptions? options = null, CancellationToken cancellationToken = default)
|
|
{
|
|
return this.RunCoreStreamingAsync(messages, thread, options, cancellationToken).ToAgentRunResponseAsync(cancellationToken);
|
|
}
|
|
|
|
protected override async IAsyncEnumerable<AgentRunResponseUpdate> RunCoreStreamingAsync(
|
|
IEnumerable<ChatMessage> messages,
|
|
AgentThread? thread = null,
|
|
AgentRunOptions? options = null,
|
|
[EnumeratorCancellation] CancellationToken cancellationToken = default)
|
|
{
|
|
// Track function calls that should trigger state events
|
|
var trackedFunctionCalls = new Dictionary<string, FunctionCallContent>();
|
|
|
|
await foreach (var update in this.InnerAgent.RunStreamingAsync(messages, thread, options, cancellationToken).ConfigureAwait(false))
|
|
{
|
|
// Process contents: track function calls and emit state events for results
|
|
List<AIContent> stateEventsToEmit = new();
|
|
foreach (var content in update.Contents)
|
|
{
|
|
if (content is FunctionCallContent callContent)
|
|
{
|
|
if (callContent.Name == "create_plan" || callContent.Name == "update_plan_step")
|
|
{
|
|
trackedFunctionCalls[callContent.CallId] = callContent;
|
|
break;
|
|
}
|
|
}
|
|
else if (content is FunctionResultContent resultContent)
|
|
{
|
|
// Check if this result matches a tracked function call
|
|
if (trackedFunctionCalls.TryGetValue(resultContent.CallId, out var matchedCall))
|
|
{
|
|
var bytes = JsonSerializer.SerializeToUtf8Bytes((JsonElement)resultContent.Result!, this._jsonSerializerOptions);
|
|
|
|
// Determine event type based on the function name
|
|
if (matchedCall.Name == "create_plan")
|
|
{
|
|
stateEventsToEmit.Add(new DataContent(bytes, "application/json"));
|
|
}
|
|
else if (matchedCall.Name == "update_plan_step")
|
|
{
|
|
stateEventsToEmit.Add(new DataContent(bytes, "application/json-patch+json"));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
yield return update;
|
|
|
|
yield return new AgentRunResponseUpdate(
|
|
new ChatResponseUpdate(role: ChatRole.System, stateEventsToEmit)
|
|
{
|
|
MessageId = "delta_" + Guid.NewGuid().ToString("N"),
|
|
CreatedAt = update.CreatedAt,
|
|
ResponseId = update.ResponseId,
|
|
AuthorName = update.AuthorName,
|
|
Role = update.Role,
|
|
ContinuationToken = update.ContinuationToken,
|
|
AdditionalProperties = update.AdditionalProperties,
|
|
})
|
|
{
|
|
AgentId = update.AgentId
|
|
};
|
|
}
|
|
}
|
|
}
|