mirror of
https://github.com/microsoft/agent-framework.git
synced 2026-06-16 21:04:09 +08:00
72 lines
2.7 KiB
C#
72 lines
2.7 KiB
C#
// Copyright (c) Microsoft. All rights reserved.
|
|
|
|
using System.Collections.Generic;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.Extensions.AI;
|
|
|
|
namespace Microsoft.Agents.AI.Workflows.Declarative.Extensions;
|
|
|
|
internal static class AgentProviderExtensions
|
|
{
|
|
public static async ValueTask<AgentResponse> InvokeAgentAsync(
|
|
this ResponseAgentProvider agentProvider,
|
|
string executorId,
|
|
IWorkflowContext context,
|
|
string agentName,
|
|
string? conversationId,
|
|
bool autoSend,
|
|
IEnumerable<ChatMessage>? inputMessages = null,
|
|
IDictionary<string, object?>? inputArguments = null,
|
|
CancellationToken cancellationToken = default)
|
|
{
|
|
IAsyncEnumerable<AgentResponseUpdate> agentUpdates = agentProvider.InvokeAgentAsync(agentName, null, conversationId, inputMessages, inputArguments, cancellationToken);
|
|
|
|
// Enable "autoSend" behavior if this is the workflow conversation.
|
|
bool isWorkflowConversation = context.IsWorkflowConversation(conversationId, out string? workflowConversationId);
|
|
autoSend |= isWorkflowConversation;
|
|
|
|
// Process the agent response updates.
|
|
List<AgentResponseUpdate> updates = [];
|
|
await foreach (AgentResponseUpdate update in agentUpdates.ConfigureAwait(false))
|
|
{
|
|
await AssignConversationIdAsync(((ChatResponseUpdate?)update.RawRepresentation)?.ConversationId).ConfigureAwait(false);
|
|
|
|
updates.Add(update);
|
|
|
|
if (autoSend)
|
|
{
|
|
await context.AddEventAsync(new AgentResponseUpdateEvent(executorId, update), cancellationToken).ConfigureAwait(false);
|
|
}
|
|
}
|
|
|
|
AgentResponse response = updates.ToAgentResponse();
|
|
|
|
if (autoSend)
|
|
{
|
|
await context.AddEventAsync(new AgentResponseEvent(executorId, response), cancellationToken).ConfigureAwait(false);
|
|
}
|
|
|
|
// If autoSend is enabled and this is not the workflow conversation, copy messages to the workflow conversation.
|
|
if (autoSend && !isWorkflowConversation && workflowConversationId is not null)
|
|
{
|
|
foreach (ChatMessage message in response.Messages)
|
|
{
|
|
await agentProvider.CreateMessageAsync(workflowConversationId, message, cancellationToken).ConfigureAwait(false);
|
|
}
|
|
}
|
|
|
|
return response;
|
|
|
|
async ValueTask AssignConversationIdAsync(string? assignValue)
|
|
{
|
|
if (assignValue is not null && conversationId is null)
|
|
{
|
|
conversationId = assignValue;
|
|
|
|
await context.QueueConversationUpdateAsync(conversationId, cancellationToken).ConfigureAwait(false);
|
|
}
|
|
}
|
|
}
|
|
}
|