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;
|
2023-08-30 02:19:19 +00:00
|
|
|
using BotSharp.Abstraction.Routing.Settings;
|
2023-08-19 12:50:26 +00:00
|
|
|
|
|
|
|
|
namespace BotSharp.Core.Conversations.Services;
|
|
|
|
|
|
|
|
|
|
public partial class ConversationService
|
|
|
|
|
{
|
2023-09-06 03:19:36 +00:00
|
|
|
public async Task<bool> SendMessage(string agentId,
|
2023-08-19 12:50:26 +00:00
|
|
|
RoleDialogModel lastDialog,
|
|
|
|
|
Func<RoleDialogModel, Task> onMessageReceived,
|
2023-08-20 19:02:59 +00:00
|
|
|
Func<RoleDialogModel, Task> onFunctionExecuting,
|
|
|
|
|
Func<RoleDialogModel, Task> onFunctionExecuted)
|
2023-08-19 12:50:26 +00:00
|
|
|
{
|
2023-09-06 03:19:36 +00:00
|
|
|
var conversation = await GetConversationRecord(agentId);
|
2023-08-19 12:50:26 +00:00
|
|
|
|
2023-08-30 02:19:19 +00:00
|
|
|
var agentService = _services.GetRequiredService<IAgentService>();
|
|
|
|
|
Agent agent = await agentService.LoadAgent(agentId);
|
2023-08-19 12:50:26 +00:00
|
|
|
|
|
|
|
|
_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);
|
2023-08-19 12:50:26 +00:00
|
|
|
|
|
|
|
|
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);
|
2023-08-19 12:50:26 +00:00
|
|
|
|
|
|
|
|
await hook.OnDialogsLoaded(wholeDialogs);
|
2023-08-31 02:09:38 +00:00
|
|
|
await hook.BeforeCompletion(lastDialog);
|
|
|
|
|
|
|
|
|
|
// Interrupted by hook
|
|
|
|
|
if (lastDialog.StopCompletion)
|
|
|
|
|
{
|
2023-09-11 17:09:41 +00:00
|
|
|
var message = new RoleDialogModel(AgentRole.Assistant, lastDialog.Content)
|
|
|
|
|
{
|
2023-09-14 01:41:51 +00:00
|
|
|
CurrentAgentId = agent.Id
|
2023-09-11 17:09:41 +00:00
|
|
|
};
|
|
|
|
|
await onMessageReceived(message);
|
|
|
|
|
_storage.Append(_conversationId, message);
|
2023-08-31 02:09:38 +00:00
|
|
|
return true;
|
|
|
|
|
}
|
2023-08-19 12:50:26 +00:00
|
|
|
}
|
|
|
|
|
|
2023-08-28 03:50:10 +00:00
|
|
|
// reasoning
|
2023-08-30 02:19:19 +00:00
|
|
|
var settings = _services.GetRequiredService<RoutingSettings>();
|
2023-09-19 12:17:50 +00:00
|
|
|
if (settings.RouterId == agent.Id)
|
2023-08-28 03:50:10 +00:00
|
|
|
{
|
2023-09-19 12:17:50 +00:00
|
|
|
var routing = _services.GetRequiredService<IRoutingService>();
|
|
|
|
|
var reasonedContext = await routing.Enter(agent, wholeDialogs);
|
2023-08-28 03:50:10 +00:00
|
|
|
|
|
|
|
|
if (reasonedContext.FunctionName == "interrupt_task_execution")
|
|
|
|
|
{
|
2023-09-09 20:13:19 +00:00
|
|
|
await HandleAssistantMessage(agent, new RoleDialogModel(AgentRole.Assistant, reasonedContext.Content)
|
2023-08-28 03:50:10 +00:00
|
|
|
{
|
2023-09-14 01:41:51 +00:00
|
|
|
CurrentAgentId = agent.Id
|
2023-08-28 15:58:35 +00:00
|
|
|
}, onMessageReceived);
|
2023-09-09 20:13:19 +00:00
|
|
|
|
2023-08-28 15:58:35 +00:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
else if (reasonedContext.FunctionName == "response_to_user")
|
|
|
|
|
{
|
2023-09-09 20:13:19 +00:00
|
|
|
await HandleAssistantMessage(agent, new RoleDialogModel(AgentRole.Assistant, reasonedContext.Content)
|
2023-08-28 15:58:35 +00:00
|
|
|
{
|
2023-09-14 01:41:51 +00:00
|
|
|
CurrentAgentId = agent.Id
|
2023-08-28 03:50:10 +00:00
|
|
|
}, onMessageReceived);
|
2023-09-09 20:13:19 +00:00
|
|
|
|
2023-08-28 03:50:10 +00:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
else if (reasonedContext.FunctionName == "continue_execute_task")
|
|
|
|
|
{
|
|
|
|
|
if (reasonedContext.CurrentAgentId != agent.Id)
|
|
|
|
|
{
|
|
|
|
|
agent = await agentService.LoadAgent(reasonedContext.CurrentAgentId);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-19 12:17:50 +00:00
|
|
|
routing.Dialogs.ForEach(x =>
|
2023-08-28 03:50:10 +00:00
|
|
|
{
|
|
|
|
|
wholeDialogs.Add(x);
|
2023-09-06 03:19:36 +00:00
|
|
|
if (x.Content != null)
|
|
|
|
|
{
|
2023-09-09 20:13:19 +00:00
|
|
|
_storage.Append(_conversationId, x);
|
2023-09-06 03:19:36 +00:00
|
|
|
}
|
2023-08-28 03:50:10 +00:00
|
|
|
});
|
|
|
|
|
}
|
2023-08-24 12:18:41 +00:00
|
|
|
|
2023-09-09 15:37:38 +00:00
|
|
|
var result = await GetChatCompletionsAsyncRecursively(agent,
|
2023-08-19 12:50:26 +00:00
|
|
|
wholeDialogs,
|
|
|
|
|
onMessageReceived,
|
2023-08-20 19:02:59 +00:00
|
|
|
onFunctionExecuting,
|
|
|
|
|
onFunctionExecuted);
|
2023-08-19 12:50:26 +00:00
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
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-08-19 12:50:26 +00:00
|
|
|
|
|
|
|
|
private void SaveStateByArgs(string args)
|
|
|
|
|
{
|
|
|
|
|
var stateService = _services.GetRequiredService<IConversationStateService>();
|
|
|
|
|
var jo = JsonSerializer.Deserialize<object>(args);
|
|
|
|
|
if (jo is JsonElement root)
|
|
|
|
|
{
|
|
|
|
|
foreach (JsonProperty property in root.EnumerateObject())
|
|
|
|
|
{
|
2023-08-22 04:48:17 +00:00
|
|
|
if (!string.IsNullOrEmpty(property.Value.ToString()))
|
|
|
|
|
{
|
2023-09-18 08:35:02 +00:00
|
|
|
stateService.SetState(property.Name, property.Value);
|
2023-08-22 04:48:17 +00:00
|
|
|
}
|
2023-08-19 12:50:26 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|