// Copyright (c) Microsoft. All rights reserved.
using System;
using Microsoft.Shared.Diagnostics;
namespace Microsoft.Agents.AI.Workflows.Specialized;
internal static class RequestPortExtensions
{
///
/// Attempts to process the incoming as a response to a request sent
/// through the specified . If the response is to a different port, returns
/// . If the port matches, but the response data cannot be interpreted as the
/// expected response type, throws an . Otherwise, returns
/// .
///
/// The request port through which the original request was sent.
/// The candidate response to be processed
/// if the response is for the specified port and the data could be
/// interpreted as the expected response type; otherwise, .
/// Thrown if the response is for the specified port,
/// but the data could not be interpreted as the expected response type.
public static bool ShouldProcessResponse(this RequestPort port, ExternalResponse response)
{
Throw.IfNull(response);
Throw.IfNull(response.Data);
if (!port.IsResponsePort(response))
{
return false;
}
if (!response.Data.IsType(port.Response))
{
throw port.CreateExceptionForType(response);
}
return true;
}
internal static bool IsResponsePort(this RequestPort port, ExternalResponse response)
=> Throw.IfNull(response).PortInfo.PortId == port.Id;
internal static InvalidOperationException CreateExceptionForType(this RequestPort port, ExternalResponse response)
=> new($"Message type {response.Data.TypeId} is not assignable to the response type {port.Response.Name}" +
$" of input port {port.Id}.");
}