// Copyright (c) Microsoft. All rights reserved.
using System.Collections.Generic;
using System.Text.Json.Serialization;
using Microsoft.Extensions.AI;
namespace Microsoft.Agents.AI.Workflows.Declarative.Events;
///
/// Represents a request for external input.
///
public sealed class ExternalInputRequest : IExternalRequestEnvelope
{
///
/// The source message that triggered the request for external input.
///
public AgentResponse AgentResponse { get; }
[JsonConstructor]
internal ExternalInputRequest(AgentResponse agentResponse)
{
this.AgentResponse = agentResponse;
}
internal ExternalInputRequest(ChatMessage message)
{
this.AgentResponse = new AgentResponse(message);
}
internal ExternalInputRequest(string text)
{
this.AgentResponse = new AgentResponse(new ChatMessage(ChatRole.User, text));
}
///
///
/// Prefers (when the workflow declared
/// requireApproval: true) over so that
/// hosts which speak the approval protocol see the approval-bearing content.
///
AIContent? IExternalRequestEnvelope.GetInnerRequestContent()
{
IList? messages = this.AgentResponse?.Messages;
if (messages is null)
{
return null;
}
foreach (ChatMessage message in messages)
{
foreach (AIContent content in message.Contents)
{
if (content is ToolApprovalRequestContent toolApprovalRequest)
{
return toolApprovalRequest;
}
}
}
foreach (ChatMessage message in messages)
{
foreach (AIContent content in message.Contents)
{
if (content is FunctionCallContent functionCall)
{
return functionCall;
}
}
}
return null;
}
///
object IExternalRequestEnvelope.CreateResponse(IList messages)
=> new ExternalInputResponse(messages);
}