2024-04-02 19:09:42 +00:00
|
|
|
using BotSharp.Abstraction.Conversations.Enums;
|
2025-05-16 01:19:55 +00:00
|
|
|
using BotSharp.Abstraction.Hooks;
|
2024-03-27 18:50:27 +00:00
|
|
|
using BotSharp.Abstraction.Models;
|
|
|
|
|
|
2023-06-12 13:28:49 +00:00
|
|
|
namespace BotSharp.Core.Conversations.Services;
|
2023-06-03 02:07:30 +00:00
|
|
|
|
2023-08-19 12:50:26 +00:00
|
|
|
public partial class ConversationService : IConversationService
|
2023-06-03 02:07:30 +00:00
|
|
|
{
|
2023-08-09 21:49:55 +00:00
|
|
|
private readonly ILogger _logger;
|
2023-06-27 18:31:13 +00:00
|
|
|
private readonly IServiceProvider _services;
|
|
|
|
|
private readonly IUserIdentity _user;
|
|
|
|
|
private readonly ConversationSetting _settings;
|
|
|
|
|
private readonly IConversationStorage _storage;
|
2023-09-06 03:19:36 +00:00
|
|
|
private readonly IConversationStateService _state;
|
|
|
|
|
private string _conversationId;
|
2024-07-25 01:24:14 +00:00
|
|
|
private const string AIAssistant = BuiltInAgentId.AIAssistant;
|
2024-05-27 02:30:57 +00:00
|
|
|
|
2023-10-30 16:48:18 +00:00
|
|
|
public string ConversationId => _conversationId;
|
2023-09-06 03:19:36 +00:00
|
|
|
|
|
|
|
|
public IConversationStateService States => _state;
|
2023-06-03 02:07:30 +00:00
|
|
|
|
2023-09-06 03:39:56 +00:00
|
|
|
public ConversationService(
|
|
|
|
|
IServiceProvider services,
|
2023-06-27 18:31:13 +00:00
|
|
|
IUserIdentity user,
|
|
|
|
|
ConversationSetting settings,
|
2023-08-09 21:49:55 +00:00
|
|
|
IConversationStorage storage,
|
2023-09-06 03:19:36 +00:00
|
|
|
IConversationStateService state,
|
2023-08-09 21:49:55 +00:00
|
|
|
ILogger<ConversationService> logger)
|
2023-06-03 02:07:30 +00:00
|
|
|
{
|
2023-06-27 18:31:13 +00:00
|
|
|
_services = services;
|
|
|
|
|
_user = user;
|
|
|
|
|
_settings = settings;
|
|
|
|
|
_storage = storage;
|
2023-09-06 03:19:36 +00:00
|
|
|
_state = state;
|
2023-08-09 21:49:55 +00:00
|
|
|
_logger = logger;
|
2023-06-03 02:07:30 +00:00
|
|
|
}
|
|
|
|
|
|
2024-03-07 07:34:52 +00:00
|
|
|
public async Task<bool> DeleteConversations(IEnumerable<string> ids)
|
2023-06-03 02:07:30 +00:00
|
|
|
{
|
2023-11-26 06:29:06 +00:00
|
|
|
var db = _services.GetRequiredService<IBotSharpRepository>();
|
2024-08-08 01:56:29 +00:00
|
|
|
var fileStorage = _services.GetRequiredService<IFileStorageService>();
|
2024-03-07 07:34:52 +00:00
|
|
|
var isDeleted = db.DeleteConversations(ids);
|
2024-08-08 01:56:29 +00:00
|
|
|
fileStorage.DeleteConversationFiles(ids);
|
2023-11-26 06:29:06 +00:00
|
|
|
return await Task.FromResult(isDeleted);
|
2023-06-03 02:07:30 +00:00
|
|
|
}
|
2023-11-26 06:29:06 +00:00
|
|
|
|
2023-11-22 05:07:19 +00:00
|
|
|
public async Task<Conversation> UpdateConversationTitle(string id, string title)
|
|
|
|
|
{
|
|
|
|
|
var db = _services.GetRequiredService<IBotSharpRepository>();
|
|
|
|
|
db.UpdateConversationTitle(id, title);
|
|
|
|
|
var conversation = db.GetConversation(id);
|
|
|
|
|
return conversation;
|
|
|
|
|
}
|
2024-10-11 20:07:41 +00:00
|
|
|
|
2024-12-12 03:28:00 +00:00
|
|
|
public async Task<Conversation> UpdateConversationTitleAlias(string id, string titleAlias)
|
|
|
|
|
{
|
|
|
|
|
var db = _services.GetRequiredService<IBotSharpRepository>();
|
|
|
|
|
db.UpdateConversationTitleAlias(id, titleAlias);
|
|
|
|
|
var conversation = db.GetConversation(id);
|
|
|
|
|
return conversation;
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-13 18:06:00 +00:00
|
|
|
public async Task<bool> UpdateConversationTags(string conversationId, List<string> toAddTags, List<string> toDeleteTags)
|
2024-10-18 20:50:39 +00:00
|
|
|
{
|
|
|
|
|
var db = _services.GetRequiredService<IBotSharpRepository>();
|
2025-03-13 18:06:00 +00:00
|
|
|
return db.UpdateConversationTags(conversationId, toAddTags, toDeleteTags);
|
2024-10-18 20:50:39 +00:00
|
|
|
}
|
|
|
|
|
|
2024-10-11 20:07:41 +00:00
|
|
|
public async Task<bool> UpdateConversationMessage(string conversationId, UpdateMessageRequest request)
|
|
|
|
|
{
|
|
|
|
|
var db = _services.GetRequiredService<IBotSharpRepository>();
|
|
|
|
|
return db.UpdateConversationMessage(conversationId, request);
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-14 22:04:54 +00:00
|
|
|
public async Task<Conversation> GetConversation(string id, bool isLoadStates = false)
|
2023-07-19 22:30:23 +00:00
|
|
|
{
|
2023-08-10 04:53:22 +00:00
|
|
|
var db = _services.GetRequiredService<IBotSharpRepository>();
|
2023-09-03 22:43:50 +00:00
|
|
|
var conversation = db.GetConversation(id);
|
2023-09-07 22:04:34 +00:00
|
|
|
return conversation;
|
2023-07-19 22:30:23 +00:00
|
|
|
}
|
|
|
|
|
|
2023-12-04 23:42:46 +00:00
|
|
|
public async Task<PagedItems<Conversation>> GetConversations(ConversationFilter filter)
|
2023-06-03 02:07:30 +00:00
|
|
|
{
|
2025-03-14 22:04:54 +00:00
|
|
|
if (filter == null)
|
|
|
|
|
{
|
|
|
|
|
filter = ConversationFilter.Empty();
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-10 04:53:22 +00:00
|
|
|
var db = _services.GetRequiredService<IBotSharpRepository>();
|
2023-12-04 23:42:46 +00:00
|
|
|
var conversations = db.GetConversations(filter);
|
2024-01-18 05:23:20 +00:00
|
|
|
return conversations;
|
2023-06-03 02:07:30 +00:00
|
|
|
}
|
|
|
|
|
|
2023-11-10 16:01:03 +00:00
|
|
|
public async Task<List<Conversation>> GetLastConversations()
|
|
|
|
|
{
|
|
|
|
|
var db = _services.GetRequiredService<IBotSharpRepository>();
|
|
|
|
|
return db.GetLastConversations();
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-09 17:26:26 +00:00
|
|
|
public async Task<List<string>> GetIdleConversations(int batchSize, int messageLimit, int bufferHours, IEnumerable<string> excludeAgentIds)
|
2024-03-07 07:34:52 +00:00
|
|
|
{
|
|
|
|
|
var db = _services.GetRequiredService<IBotSharpRepository>();
|
2024-09-09 17:26:26 +00:00
|
|
|
return db.GetIdleConversations(batchSize, messageLimit, bufferHours, excludeAgentIds ?? new List<string>());
|
2024-03-07 07:34:52 +00:00
|
|
|
}
|
|
|
|
|
|
2023-06-27 18:31:13 +00:00
|
|
|
public async Task<Conversation> NewConversation(Conversation sess)
|
2023-06-03 02:07:30 +00:00
|
|
|
{
|
2023-08-10 04:53:22 +00:00
|
|
|
var db = _services.GetRequiredService<IBotSharpRepository>();
|
2023-11-14 01:25:25 +00:00
|
|
|
var user = db.GetUserById(_user.Id);
|
2023-09-28 17:16:39 +00:00
|
|
|
var foundUserId = user?.Id ?? string.Empty;
|
2023-06-27 18:31:13 +00:00
|
|
|
|
2023-09-07 22:04:34 +00:00
|
|
|
var record = sess;
|
2023-09-03 19:06:40 +00:00
|
|
|
record.Id = sess.Id.IfNullOrEmptyAs(Guid.NewGuid().ToString());
|
|
|
|
|
record.UserId = sess.UserId.IfNullOrEmptyAs(foundUserId);
|
2024-10-29 19:59:46 +00:00
|
|
|
record.Tags = sess.Tags;
|
2025-02-07 19:41:20 +00:00
|
|
|
record.Title = string.IsNullOrEmpty(record.Title) ? "New Conversation" : record.Title;
|
2023-06-27 18:31:13 +00:00
|
|
|
|
2023-09-03 22:43:50 +00:00
|
|
|
db.CreateNewConversation(record);
|
2023-10-16 20:07:07 +00:00
|
|
|
|
2025-05-16 01:19:55 +00:00
|
|
|
var hooks = _services.GetHooks<IConversationHook>(record.AgentId);
|
2024-12-07 21:47:17 +00:00
|
|
|
|
2023-10-16 20:07:07 +00:00
|
|
|
foreach (var hook in hooks)
|
|
|
|
|
{
|
2023-11-27 21:00:26 +00:00
|
|
|
// If user connect agent first time
|
|
|
|
|
await hook.OnUserAgentConnectedInitially(sess);
|
|
|
|
|
|
2023-10-16 20:07:07 +00:00
|
|
|
await hook.OnConversationInitialized(record);
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-07 22:04:34 +00:00
|
|
|
return record;
|
2023-06-03 02:07:30 +00:00
|
|
|
}
|
|
|
|
|
|
2023-06-27 18:31:13 +00:00
|
|
|
public Task CleanHistory(string agentId)
|
2023-06-03 02:07:30 +00:00
|
|
|
{
|
|
|
|
|
throw new NotImplementedException();
|
|
|
|
|
}
|
2023-06-27 18:31:13 +00:00
|
|
|
|
2024-10-07 21:42:20 +00:00
|
|
|
public List<RoleDialogModel> GetDialogHistory(int lastCount = 100, bool fromBreakpoint = true, IEnumerable<string>? includeMessageTypes = null)
|
2023-06-27 18:31:13 +00:00
|
|
|
{
|
2023-11-14 14:13:54 +00:00
|
|
|
if (string.IsNullOrEmpty(_conversationId))
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("ConversationId is null.");
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-06 03:19:36 +00:00
|
|
|
var dialogs = _storage.GetDialogs(_conversationId);
|
2024-03-24 01:31:15 +00:00
|
|
|
|
2024-10-07 21:42:20 +00:00
|
|
|
if (!includeMessageTypes.IsNullOrEmpty())
|
2024-10-07 21:35:39 +00:00
|
|
|
{
|
2024-10-07 21:42:20 +00:00
|
|
|
dialogs = dialogs.Where(x => string.IsNullOrEmpty(x.MessageType) || includeMessageTypes.Contains(x.MessageType)).ToList();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
dialogs = dialogs.Where(x => string.IsNullOrEmpty(x.MessageType) || x.MessageType.IsEqualTo(MessageTypeName.Plain)).ToList();
|
2024-10-07 21:35:39 +00:00
|
|
|
}
|
|
|
|
|
|
2024-03-24 01:31:15 +00:00
|
|
|
if (fromBreakpoint)
|
|
|
|
|
{
|
2024-11-04 22:44:41 +00:00
|
|
|
var db = _services.GetRequiredService<IBotSharpRepository>();
|
|
|
|
|
var breakpoint = db.GetConversationBreakpoint(_conversationId);
|
2024-10-29 19:59:46 +00:00
|
|
|
|
2024-04-08 03:15:51 +00:00
|
|
|
if (breakpoint != null)
|
|
|
|
|
{
|
|
|
|
|
dialogs = dialogs.Where(x => x.CreatedAt >= breakpoint.Breakpoint).ToList();
|
|
|
|
|
if (!string.IsNullOrEmpty(breakpoint.Reason))
|
|
|
|
|
{
|
|
|
|
|
dialogs.Insert(0, new RoleDialogModel(AgentRole.User, breakpoint.Reason));
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-03-24 01:31:15 +00:00
|
|
|
}
|
|
|
|
|
|
2024-12-13 20:34:17 +00:00
|
|
|
var agentMsgCount = GetAgentMessageCount();
|
|
|
|
|
var count = agentMsgCount.HasValue && agentMsgCount.Value > 0 ? agentMsgCount.Value : lastCount;
|
|
|
|
|
|
|
|
|
|
return dialogs.TakeLast(count).ToList();
|
2023-09-06 03:19:36 +00:00
|
|
|
}
|
|
|
|
|
|
2024-10-07 21:35:39 +00:00
|
|
|
public void SetConversationId(string conversationId, List<MessageState> states, bool isReadOnly = false)
|
2023-09-06 03:19:36 +00:00
|
|
|
{
|
|
|
|
|
_conversationId = conversationId;
|
2024-11-07 19:53:06 +00:00
|
|
|
_state.Load(_conversationId, isReadOnly);
|
2024-04-02 19:09:42 +00:00
|
|
|
states.ForEach(x => _state.SetState(x.Key, x.Value, activeRounds: x.ActiveRounds, source: StateSource.External));
|
2023-06-27 18:31:13 +00:00
|
|
|
}
|
2024-06-19 23:04:47 +00:00
|
|
|
|
|
|
|
|
public async Task<Conversation> GetConversationRecordOrCreateNew(string agentId)
|
|
|
|
|
{
|
|
|
|
|
var converation = await GetConversation(_conversationId);
|
|
|
|
|
|
|
|
|
|
// Create conversation if this conversation does not exist
|
|
|
|
|
if (converation == null)
|
|
|
|
|
{
|
|
|
|
|
var state = _services.GetRequiredService<IConversationStateService>();
|
|
|
|
|
var channel = state.GetState("channel");
|
2024-10-25 02:12:59 +00:00
|
|
|
var channelId = state.GetState("channel_id");
|
2024-11-28 10:31:20 +00:00
|
|
|
var userId = state.GetState("current_user_id");
|
2024-06-19 23:04:47 +00:00
|
|
|
var sess = new Conversation
|
|
|
|
|
{
|
|
|
|
|
Id = _conversationId,
|
|
|
|
|
Channel = channel,
|
2024-10-25 02:12:59 +00:00
|
|
|
ChannelId = channelId,
|
2024-11-28 10:31:20 +00:00
|
|
|
AgentId = agentId,
|
|
|
|
|
UserId = userId,
|
2024-06-19 23:04:47 +00:00
|
|
|
};
|
|
|
|
|
converation = await NewConversation(sess);
|
|
|
|
|
}
|
2025-04-16 19:26:32 +00:00
|
|
|
|
2024-06-19 23:04:47 +00:00
|
|
|
return converation;
|
|
|
|
|
}
|
2024-06-25 03:46:13 +00:00
|
|
|
|
|
|
|
|
public bool IsConversationMode()
|
|
|
|
|
{
|
|
|
|
|
return !string.IsNullOrWhiteSpace(_conversationId);
|
|
|
|
|
}
|
2024-12-13 20:34:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
private int? GetAgentMessageCount()
|
|
|
|
|
{
|
|
|
|
|
var db = _services.GetRequiredService<IBotSharpRepository>();
|
|
|
|
|
var routingCtx = _services.GetRequiredService<IRoutingContext>();
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(routingCtx.EntryAgentId)) return null;
|
|
|
|
|
|
|
|
|
|
var agent = db.GetAgent(routingCtx.EntryAgentId, basicsOnly: true);
|
|
|
|
|
return agent?.MaxMessageCount;
|
|
|
|
|
}
|
2025-01-28 23:00:08 +00:00
|
|
|
|
|
|
|
|
public void SaveStates()
|
|
|
|
|
{
|
|
|
|
|
_state.Save();
|
|
|
|
|
}
|
2025-02-11 20:27:53 +00:00
|
|
|
|
2025-03-10 17:14:44 +00:00
|
|
|
public async Task<List<string>> GetConversationStateSearhKeys(ConversationStateKeysFilter filter)
|
2025-02-11 20:27:53 +00:00
|
|
|
{
|
2025-03-10 17:14:44 +00:00
|
|
|
if (filter == null)
|
|
|
|
|
{
|
|
|
|
|
filter = ConversationStateKeysFilter.Empty();
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-11 20:27:53 +00:00
|
|
|
var keys = new List<string>();
|
2025-03-10 17:14:44 +00:00
|
|
|
if (!filter.PreLoad && string.IsNullOrWhiteSpace(filter.Query))
|
2025-02-11 20:27:53 +00:00
|
|
|
{
|
|
|
|
|
return keys;
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-10 17:14:44 +00:00
|
|
|
var userService = _services.GetRequiredService<IUserService>();
|
2025-02-11 20:27:53 +00:00
|
|
|
var db = _services.GetRequiredService<IBotSharpRepository>();
|
2025-03-10 17:14:44 +00:00
|
|
|
|
|
|
|
|
var (isAdmin, user) = await userService.IsAdminUser(_user.Id);
|
|
|
|
|
filter.UserIds = !isAdmin && user?.Id != null ? [user.Id] : [];
|
|
|
|
|
keys = db.GetConversationStateSearchKeys(filter);
|
|
|
|
|
keys = filter.PreLoad ? keys : keys.Where(x => x.Contains(filter.Query ?? string.Empty, StringComparison.OrdinalIgnoreCase)).ToList();
|
|
|
|
|
return keys.OrderBy(x => x).Take(filter.KeyLimit).ToList();
|
2025-02-11 20:27:53 +00:00
|
|
|
}
|
2023-06-03 02:07:30 +00:00
|
|
|
}
|