// Copyright (c) Microsoft. All rights reserved.
using System;
using System.Diagnostics.CodeAnalysis;
using System.Text.Json.Serialization;
namespace Microsoft.Agents.AI.Workflows;
///
/// Event triggered when a workflow executor yields output.
///
[JsonDerivedType(typeof(AgentResponseEvent))]
[JsonDerivedType(typeof(AgentResponseUpdateEvent))]
public class WorkflowOutputEvent : WorkflowEvent
{
///
/// Initializes a new instance of the class.
///
/// The output data.
/// The identifier of the executor that yielded this output.
public WorkflowOutputEvent(object data, string executorId) : base(data)
{
this.ExecutorId = executorId;
}
///
/// The unique identifier of the executor that yielded this output.
///
public string ExecutorId { get; }
///
/// The unique identifier of the executor that yielded this output.
///
[Obsolete("Use ExecutorId instead.")]
public string SourceId => this.ExecutorId;
///
/// Determines whether the underlying data is of the specified type or a derived type.
///
/// The type to compare with the type of the underlying data.
/// true if the underlying data is assignable to type T; otherwise, false.
public bool Is() => this.IsType(typeof(T));
///
/// Determines whether the underlying data is of the specified type or a derived type, and
/// returns it as that type if it is.
///
/// The type to compare with the type of the underlying data.
/// true if the underlying data is assignable to type T; otherwise, false.
public bool Is([NotNullWhen(true)] out T? maybeValue)
{
if (this.Data is T value)
{
maybeValue = value;
return true;
}
maybeValue = default;
return false;
}
///
/// Determines whether the underlying data is of the specified type or a derived type.
///
/// The type to compare with the type of the underlying data.
/// true if the underlying data is assignable to type T; otherwise, false.
public bool IsType(Type type) => this.Data is { } data && type.IsInstanceOfType(data);
///
/// Attempts to retrieve the underlying data as the specified type.
///
/// The type to which to cast.
/// The value of Data as to the target type.
public T? As() => this.Data is T value ? value : default;
///
/// Attempts to retrieve the underlying data as the specified type.
///
/// The type to which to cast.
/// The value of Data as to the target type.
public object? AsType(Type type) => this.IsType(type) ? this.Data : null;
}