// Copyright (c) Microsoft. All rights reserved.
using System;
using System.Threading;
using Microsoft.Shared.Diagnostics;
namespace Microsoft.Agents.AI.Workflows.Execution;
///
/// This class represents the result of a call to a message handler.
///
internal sealed class CallResult
{
///
/// Indicates whether the call was to a void-return executor (i.e., no result expected).
///
public bool IsVoid { get; init; }
///
/// If the call was successful, this property contains the result of the call. For calls to
/// void handlers, this will be null.
///
public object? Result { get; init; }
///
/// If the call failed, this property contains the exception that was raised during the call.
///
public Exception? Exception { get; init; }
///
/// Indicated whether the call was cancelled (e.g., via a ).
///
public bool IsCancelled { get; init; }
///
/// Indicates whether the call was successful. A call is considered successful if it returned
/// without throwing an exception.
///
public bool IsSuccess => this.Exception is null && !this.IsCancelled;
private CallResult(bool isVoid = false, bool isCancelled = false)
{
// Private constructor to enforce use of static methods.
this.IsVoid = isVoid;
this.IsCancelled = isCancelled;
}
///
/// Create a indicating a successful call that returned a result (non-void).
///
/// The result to return.
/// A indicating the result of the call.
public static CallResult ReturnResult(object? result = null) => new() { Result = result };
///
/// Create a indicating a successful call that returned no result (void).
///
/// A indicating the result of the call.
public static CallResult ReturnVoid() => new(isVoid: true);
///
/// Create a indicating that the call was cancelled.
///
/// A boolean specifying whether the call was void (was not expected to return
/// a value).
/// A indicating the result of the call.
public static CallResult Cancelled(bool wasVoid) => new(wasVoid, isCancelled: true);
///
/// Create a indicating that an exception was raised during the call.
///
/// A boolean specifying whether the call was void (was not expected to return
/// a value).
/// The exception that was raised during the call.
/// A indicating the result of the call.
/// Thrown when is null.
public static CallResult RaisedException(bool wasVoid, Exception exception)
{
Throw.IfNull(exception);
return new(wasVoid) { Exception = exception };
}
}