2023-10-23 00:31:49 +00:00
|
|
|
using BotSharp.Abstraction.Evaluations;
|
|
|
|
|
|
|
|
|
|
namespace BotSharp.Core.Evaluations;
|
|
|
|
|
|
|
|
|
|
public class EvaluationConversationHook : ConversationHookBase
|
|
|
|
|
{
|
|
|
|
|
private readonly IExecutionLogger _logger;
|
2023-12-13 17:58:25 +00:00
|
|
|
private readonly ConversationSetting _convSettings;
|
2023-10-23 00:31:49 +00:00
|
|
|
|
2023-12-13 17:58:25 +00:00
|
|
|
public EvaluationConversationHook(IExecutionLogger logger, ConversationSetting convSettings)
|
2023-10-23 00:31:49 +00:00
|
|
|
{
|
|
|
|
|
_logger = logger;
|
2023-12-13 17:58:25 +00:00
|
|
|
_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)
|
2023-12-13 17:58:25 +00:00
|
|
|
{
|
2024-12-12 01:44:29 +00:00
|
|
|
_logger.Append(Conversation.Id, $"[{DateTime.Now}] {message.Role}: {message.Content}");
|
2023-12-13 17:58:25 +00:00
|
|
|
}
|
2023-10-23 00:31:49 +00:00
|
|
|
return base.OnMessageReceived(message);
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-15 21:52:13 +00:00
|
|
|
public override Task OnFunctionExecuted(RoleDialogModel message, string from = InvokeSource.Manual)
|
2023-10-23 00:31:49 +00:00
|
|
|
{
|
2024-12-12 01:44:29 +00:00
|
|
|
if (Conversation != null && _convSettings.EnableExecutionLog)
|
2023-12-13 17:58:25 +00:00
|
|
|
{
|
2024-12-12 01:44:29 +00:00
|
|
|
_logger.Append(Conversation.Id, $"[{DateTime.Now}] {message.Role}: {message.FunctionName}({message.FunctionArgs}) => {message.Content}");
|
2023-12-13 17:58:25 +00:00
|
|
|
}
|
2025-07-15 21:52:13 +00:00
|
|
|
return base.OnFunctionExecuted(message, from: from);
|
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)
|
2023-12-13 17:58:25 +00:00
|
|
|
{
|
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)
|
2023-12-13 17:58:25 +00:00
|
|
|
{
|
2024-12-12 01:44:29 +00:00
|
|
|
_logger.Append(Conversation.Id, $"[{DateTime.Now}] {AgentRole.Function}: trigger_event({{\"event\": \"{message.FunctionName}\"}})");
|
2023-12-13 17:58:25 +00:00
|
|
|
}
|
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)
|
2023-12-13 17:58:25 +00:00
|
|
|
{
|
2024-12-12 01:44:29 +00:00
|
|
|
_logger.Append(Conversation.Id, $"[{DateTime.Now}] {AgentRole.Function}: trigger_event({{\"event\": \"{message.FunctionName}\"}})");
|
2023-12-13 17:58:25 +00:00
|
|
|
}
|
2023-10-23 00:31:49 +00:00
|
|
|
return base.OnConversationEnding(message);
|
|
|
|
|
}
|
|
|
|
|
}
|