BotSharp/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.SendMessage.cs

124 lines
4 KiB
C#
Raw Normal View History

2023-08-28 03:50:10 +00:00
using BotSharp.Abstraction.Agents.Models;
2023-10-27 22:55:28 +00:00
using BotSharp.Abstraction.Messaging.Models.RichContent;
2023-09-19 12:17:50 +00:00
using BotSharp.Abstraction.Routing;
using BotSharp.Abstraction.Routing.Settings;
2023-09-24 16:20:02 +00:00
using System.Drawing;
namespace BotSharp.Core.Conversations.Services;
public partial class ConversationService
{
2023-09-06 03:19:36 +00:00
public async Task<bool> SendMessage(string agentId,
RoleDialogModel message,
Func<RoleDialogModel, Task> onMessageReceived,
Func<RoleDialogModel, Task> onFunctionExecuting,
Func<RoleDialogModel, Task> onFunctionExecuted)
{
2023-09-06 03:19:36 +00:00
var conversation = await GetConversationRecord(agentId);
var agentService = _services.GetRequiredService<IAgentService>();
Agent agent = await agentService.LoadAgent(agentId);
var content = $"Received [{agent.Name}] {message.Role}: {message.Content}";
#if DEBUG
Console.WriteLine(content, Color.OrangeRed);
#else
_logger.LogInformation(content);
#endif
message.CurrentAgentId = agent.Id;
2023-08-20 22:53:53 +00:00
_storage.Append(_conversationId, message);
var hooks = _services.GetServices<IConversationHook>().ToList();
// Before chat completion hook
foreach (var hook in hooks)
{
hook.SetAgent(agent)
2023-09-06 03:19:36 +00:00
.SetConversation(conversation);
await hook.OnMessageReceived(message);
2023-08-31 02:09:38 +00:00
// Interrupted by hook
if (message.StopCompletion)
2023-08-31 02:09:38 +00:00
{
await onMessageReceived(message);
_storage.Append(_conversationId, message);
2023-08-31 02:09:38 +00:00
return true;
}
}
2023-09-20 10:31:50 +00:00
// Routing with reasoning
2023-09-23 21:33:05 +00:00
var routing = _services.GetRequiredService<IRoutingService>();
var settings = _services.GetRequiredService<RoutingSettings>();
2023-09-09 20:13:19 +00:00
var ret = agentId == settings.RouterId ?
await routing.InstructLoop(message) :
await routing.ExecuteOnce(agent, message);
2023-08-24 12:18:41 +00:00
await HandleAssistantMessage(message, onMessageReceived);
2023-09-24 21:32:58 +00:00
var statistics = _services.GetRequiredService<ITokenStatistics>();
statistics.PrintStatistics();
routing.ResetRecursiveCounter();
2023-10-19 17:24:00 +00:00
routing.RefreshDialogs();
return ret;
}
2023-09-06 03:19:36 +00:00
private async Task<Conversation> GetConversationRecord(string agentId)
{
var converation = await GetConversation(_conversationId);
// Create conversation if this conversation not exists
if (converation == null)
{
var sess = new Conversation
{
Id = _conversationId,
AgentId = agentId
};
converation = await NewConversation(sess);
}
return converation;
}
2023-09-23 21:33:05 +00:00
private async Task HandleAssistantMessage(RoleDialogModel message, Func<RoleDialogModel, Task> onMessageReceived)
{
var agentService = _services.GetRequiredService<IAgentService>();
var agent = await agentService.GetAgent(message.CurrentAgentId);
var agentName = agent.Name;
2023-09-23 21:33:05 +00:00
var text = message.Role == AgentRole.Function ?
$"Sending [{agentName}] {message.FunctionName}: {message.Content}" :
$"Sending [{agentName}] {message.Role}: {message.Content}";
2023-09-24 16:20:02 +00:00
#if DEBUG
Console.WriteLine(text, Color.Yellow);
2023-09-24 16:20:02 +00:00
#else
_logger.LogInformation(text);
2023-09-24 16:20:02 +00:00
#endif
2023-09-23 21:33:05 +00:00
2023-10-27 22:55:28 +00:00
// Only read content from RichContent for UI rendering. When richContent is null, create a basic text message for richContent.
var state = _services.GetRequiredService<IConversationStateService>();
message.RichContent = message.RichContent ?? new RichContent<TextMessage>
{
Recipient = new Recipient { Id = state.GetConversationId() },
Message = new TextMessage { Text = message.Content }
};
2023-10-16 20:07:07 +00:00
var hooks = _services.GetServices<IConversationHook>().ToList();
foreach (var hook in hooks)
{
await hook.OnResponseGenerated(message);
}
2023-09-23 21:33:05 +00:00
await onMessageReceived(message);
// Add to dialog history
_storage.Append(_conversationId, message);
}
}