using Microsoft.Extensions.AI;
using xAiModels.Constants;
using xCommons.Extensions;
namespace xAiModels.Extensions
{
///
/// Extensions for ChatRole ...
///
public static class ChatRoleExtensions
{
///
/// Normalize an String to Propper Chat Role Value ...
/// >
///
///
///
public static string GetChatRoleTitle(this string source)
{
//
var result = XAiChatRoles.None;
//
if (source.ToNormalString() == XAiChatRoles.None.ToNormalString())
{
result = XAiChatRoles.None;
}
else if (source.ToNormalString() == XAiChatRoles.User.ToNormalString())
{
result = XAiChatRoles.User;
}
else if (source.ToNormalString() == XAiChatRoles.Tool.ToNormalString())
{
result = XAiChatRoles.Tool;
}
else if (source.ToNormalString() == XAiChatRoles.System.ToNormalString())
{
result = XAiChatRoles.System;
}
else if (source.ToNormalString() == XAiChatRoles.Assistant.ToNormalString())
{
result = XAiChatRoles.Assistant;
}
//
return result;
}
///
/// Get Chat Role Title from Chat Role Class ...
///
///
///
///
public static string GetRole(this ChatRole source)
{
//
var result = source.Value
.GetChatRoleTitle();
//
return result;
}
///
/// Generate Chat Role Class Based on role Title ...
///
///
///
public static ChatRole ToChatRole(this string source)
{
//
var role = source.GetChatRoleTitle();
ChatRole result = new ChatRole(role);
//
return result;
}
///
/// Extract Chat Role Enum from Chat Role ...
///
///
///
///
///
public static XAiChatRole ToChatRole(this ChatRole source)
{
//
var role = source
.GetRole()
.GetChatRoleTitle();
//
XAiChatRole result = XAiChatRole.User;
//
if (role == XAiChatRoles.User)
{
result = XAiChatRole.User;
}
else if (role == XAiChatRoles.Tool)
{
result = XAiChatRole.Tool;
}
else if (role == XAiChatRoles.System)
{
result = XAiChatRole.System;
}
else if (role == XAiChatRoles.Assistant)
{
result = XAiChatRole.Assistant;
}
//
return result;
}
///
/// Validate an String as Chat Role ...
///
///
///
public static bool IsValid(this string source)
{
//
var result =
source != XAiChatRoles.None &&
source.ToNormalString() != XAiChatRoles.None.ToNormalString();
//
return result;
}
///
/// Validate Role ...
/// >
///
///
///
public static bool IsValid(this XAiChatRole source)
{
//
var result = source != XAiChatRole.None;
return result;
}
///
/// Validate Role ...
/// >
///
///
///
public static bool IsValid(this ChatRole source)
{
//
var role = source.ToChatRole();
var result = role.IsValid();
return result;
}
}
}