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

115 lines
3.6 KiB
C#
Raw Normal View History

2023-08-28 03:50:10 +00:00
using BotSharp.Abstraction.Agents.Models;
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 lastDialog,
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);
_logger.LogInformation($"[{agent.Name}] {lastDialog.Role}: {lastDialog.Content}");
lastDialog.CurrentAgentId = agent.Id;
2023-08-20 22:53:53 +00:00
2023-09-06 03:19:36 +00:00
var wholeDialogs = GetDialogHistory();
2023-08-20 22:53:53 +00:00
wholeDialogs.Add(lastDialog);
2023-09-09 20:13:19 +00:00
_storage.Append(_conversationId, lastDialog);
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.OnDialogsLoaded(wholeDialogs);
2023-08-31 02:09:38 +00:00
await hook.BeforeCompletion(lastDialog);
// Interrupted by hook
if (lastDialog.StopCompletion)
{
var message = new RoleDialogModel(AgentRole.Assistant, lastDialog.Content)
{
2023-09-14 01:41:51 +00:00
CurrentAgentId = agent.Id
};
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
2023-09-23 21:33:05 +00:00
routing.SetDialogs(wholeDialogs);
2023-08-28 03:50:10 +00:00
2023-09-23 21:33:05 +00:00
var response = settings.RouterId == agent.Id ?
await routing.InstructLoop(agent) :
await routing.ExecuteOnce(agent);
2023-08-24 12:18:41 +00:00
2023-09-23 21:33:05 +00:00
await HandleAssistantMessage(response, onMessageReceived);
2023-09-23 21:33:05 +00:00
return true;
}
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)
{
2023-09-23 21:33:05 +00:00
var hooks = _services.GetServices<IConversationHook>().ToList();
// After chat completion hook
foreach (var hook in hooks)
{
2023-09-23 21:33:05 +00:00
await hook.AfterCompletion(message);
}
2023-09-23 21:33:05 +00:00
2023-09-24 16:20:02 +00:00
var routingSetting = _services.GetRequiredService<RoutingSettings>();
var agentName = routingSetting.RouterId == message.CurrentAgentId ?
routingSetting.RouterName :
(await _services.GetRequiredService<IAgentService>().GetAgent(message.CurrentAgentId)).Name;
2023-09-23 21:33:05 +00:00
2023-09-24 16:20:02 +00:00
#if DEBUG
Console.WriteLine($"[{agentName}] {message.Role}: {message.Content}", Color.Pink);
#else
_logger.LogInformation($"[{agentName}] {message.Role}: {message.Content}");
#endif
2023-09-23 21:33:05 +00:00
await onMessageReceived(message);
// Add to dialog history
_storage.Append(_conversationId, message);
}
}