BotSharp/src/Infrastructure/BotSharp.Core/Evaluations/EvaluationConversationHook.cs

62 lines
2.2 KiB
C#
Raw Normal View History

2023-10-23 00:31:49 +00:00
using BotSharp.Abstraction.Evaluations;
2025-08-04 19:21:11 +00:00
using BotSharp.Abstraction.Routing.Models;
2023-10-23 00:31:49 +00:00
namespace BotSharp.Core.Evaluations;
public class EvaluationConversationHook : ConversationHookBase
{
private readonly IExecutionLogger _logger;
private readonly ConversationSetting _convSettings;
2023-10-23 00:31:49 +00:00
public EvaluationConversationHook(IExecutionLogger logger, ConversationSetting convSettings)
2023-10-23 00:31:49 +00:00
{
_logger = logger;
_convSettings = convSettings;
2023-10-23 00:31:49 +00:00
}
public override Task OnMessageReceived(RoleDialogModel message)
{
2024-12-12 01:44:29 +00:00
if (Conversation != null && _convSettings.EnableExecutionLog)
{
2024-12-12 01:44:29 +00:00
_logger.Append(Conversation.Id, $"[{DateTime.Now}] {message.Role}: {message.Content}");
}
2023-10-23 00:31:49 +00:00
return base.OnMessageReceived(message);
}
2025-08-04 19:21:11 +00:00
public override Task OnFunctionExecuted(RoleDialogModel message, InvokeFunctionOptions? options = null)
2023-10-23 00:31:49 +00:00
{
2024-12-12 01:44:29 +00:00
if (Conversation != null && _convSettings.EnableExecutionLog)
{
2024-12-12 01:44:29 +00:00
_logger.Append(Conversation.Id, $"[{DateTime.Now}] {message.Role}: {message.FunctionName}({message.FunctionArgs}) => {message.Content}");
}
2025-08-04 19:21:11 +00:00
return base.OnFunctionExecuted(message, options);
2023-10-23 00:31:49 +00:00
}
public override Task OnResponseGenerated(RoleDialogModel message)
{
2024-12-12 01:44:29 +00:00
if (Conversation != null && _convSettings.EnableExecutionLog)
{
2024-12-12 01:44:29 +00:00
_logger.Append(Conversation.Id, $"[{DateTime.Now}] {message.Role}: {message.Content}");
}
2023-10-23 00:31:49 +00:00
return base.OnResponseGenerated(message);
}
public override Task OnHumanInterventionNeeded(RoleDialogModel message)
{
2024-12-12 01:44:29 +00:00
if (Conversation != null && _convSettings.EnableExecutionLog)
{
2024-12-12 01:44:29 +00:00
_logger.Append(Conversation.Id, $"[{DateTime.Now}] {AgentRole.Function}: trigger_event({{\"event\": \"{message.FunctionName}\"}})");
}
2023-10-23 00:31:49 +00:00
return base.OnHumanInterventionNeeded(message);
}
public override Task OnConversationEnding(RoleDialogModel message)
{
2024-12-12 01:44:29 +00:00
if (Conversation != null && _convSettings.EnableExecutionLog)
{
2024-12-12 01:44:29 +00:00
_logger.Append(Conversation.Id, $"[{DateTime.Now}] {AgentRole.Function}: trigger_event({{\"event\": \"{message.FunctionName}\"}})");
}
2023-10-23 00:31:49 +00:00
return base.OnConversationEnding(message);
}
}