// Copyright (c) Microsoft. All rights reserved.
using System;
using Microsoft.Extensions.AI;
namespace Microsoft.Agents.AI;
///
/// Represents attribution information for the source of an agent request message for a specific run, including the component type and
/// identifier.
///
///
/// Use this struct to identify which component provided a message during an agent run.
/// This is useful to allow filtering of messages based on their source, such as distinguishing between user input, middleware-generated messages, and chat history.
///
public readonly struct AgentRequestMessageSourceAttribution : IEquatable
{
///
/// Provides the key used in to store the
/// associated with the agent request message.
///
public static readonly string AdditionalPropertiesKey = "_attribution";
///
/// Initializes a new instance of the struct with the specified source type and identifier.
///
/// The of the component that provided the message.
/// The unique identifier of the component that provided the message.
public AgentRequestMessageSourceAttribution(AgentRequestMessageSourceType sourceType, string? sourceId)
{
this.SourceType = sourceType;
this.SourceId = sourceId;
}
///
/// Gets the type of component that provided the message for the current agent run.
///
public AgentRequestMessageSourceType SourceType { get; }
///
/// Gets the unique identifier of the component that provided the message for the current agent run.
///
public string? SourceId { get; }
///
/// Determines whether the specified is equal to the current instance.
///
/// The to compare with the current instance.
/// if the specified instance is equal to the current instance; otherwise, .
public bool Equals(AgentRequestMessageSourceAttribution other)
{
return this.SourceType == other.SourceType &&
string.Equals(this.SourceId, other.SourceId, StringComparison.Ordinal);
}
///
/// Determines whether the specified object is equal to the current instance.
///
/// The object to compare with the current instance.
/// if the specified object is equal to the current instance; otherwise, .
public override bool Equals(object? obj)
{
return obj is AgentRequestMessageSourceAttribution other && this.Equals(other);
}
///
/// Returns a string representation of the current instance.
///
/// A string containing the source type and source identifier.
public override string ToString()
{
return this.SourceId is null
? $"{this.SourceType}"
: $"{this.SourceType}:{this.SourceId}";
}
///
/// Returns a hash code for the current instance.
///
/// A hash code for the current instance.
public override int GetHashCode()
{
unchecked
{
int hash = 17;
hash = (hash * 31) + this.SourceType.GetHashCode();
hash = (hash * 31) + (this.SourceId?.GetHashCode() ?? 0);
return hash;
}
}
///
/// Determines whether two instances are equal.
///
/// The first instance to compare.
/// The second instance to compare.
/// if the instances are equal; otherwise, .
public static bool operator ==(AgentRequestMessageSourceAttribution left, AgentRequestMessageSourceAttribution right)
{
return left.Equals(right);
}
///
/// Determines whether two instances are not equal.
///
/// The first instance to compare.
/// The second instance to compare.
/// if the instances are not equal; otherwise, .
public static bool operator !=(AgentRequestMessageSourceAttribution left, AgentRequestMessageSourceAttribution right)
{
return !left.Equals(right);
}
}