2023-09-01 22:33:36 +00:00
|
|
|
namespace BotSharp.Plugin.MongoStorage;
|
2023-08-26 04:41:01 +00:00
|
|
|
|
|
|
|
|
public class MongoDbContext
|
|
|
|
|
{
|
|
|
|
|
private readonly MongoClient _mongoClient;
|
|
|
|
|
private readonly string _mongoDbDatabaseName;
|
2023-09-12 22:34:50 +00:00
|
|
|
private readonly string _collectionPrefix;
|
2023-08-26 04:41:01 +00:00
|
|
|
|
2024-07-22 22:44:19 +00:00
|
|
|
private const string DB_NAME_INDEX = "authSource";
|
|
|
|
|
|
2023-09-12 22:34:50 +00:00
|
|
|
public MongoDbContext(BotSharpDatabaseSettings dbSettings)
|
2023-08-26 04:41:01 +00:00
|
|
|
{
|
2024-07-22 22:44:19 +00:00
|
|
|
var mongoDbConnectionString = dbSettings.BotSharpMongoDb;
|
2023-08-26 04:41:01 +00:00
|
|
|
_mongoClient = new MongoClient(mongoDbConnectionString);
|
2024-07-22 22:44:19 +00:00
|
|
|
_mongoDbDatabaseName = GetDatabaseName(mongoDbConnectionString);
|
2023-10-16 22:52:48 +00:00
|
|
|
_collectionPrefix = dbSettings.TablePrefix.IfNullOrEmptyAs("BotSharp");
|
2023-08-26 04:41:01 +00:00
|
|
|
}
|
|
|
|
|
|
2024-07-22 22:44:19 +00:00
|
|
|
private string GetDatabaseName(string mongoDbConnectionString)
|
2023-08-26 04:41:01 +00:00
|
|
|
{
|
2024-07-24 00:38:18 +00:00
|
|
|
var databaseName = mongoDbConnectionString.Substring(mongoDbConnectionString.LastIndexOf("/", StringComparison.InvariantCultureIgnoreCase) + 1);
|
2024-07-24 01:28:39 +00:00
|
|
|
|
|
|
|
|
var symbol = "?";
|
|
|
|
|
if (databaseName.Contains(symbol))
|
2023-08-26 04:41:01 +00:00
|
|
|
{
|
2024-07-24 01:28:39 +00:00
|
|
|
var markIdx = databaseName.IndexOf(symbol, StringComparison.InvariantCultureIgnoreCase);
|
|
|
|
|
var db = databaseName.Substring(0, markIdx);
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(db))
|
|
|
|
|
{
|
|
|
|
|
return db;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var queryStr = databaseName.Substring(markIdx + 1);
|
2024-07-24 02:22:03 +00:00
|
|
|
var queries = queryStr.Split("&", StringSplitOptions.RemoveEmptyEntries).Select(x => new
|
2024-07-24 01:28:39 +00:00
|
|
|
{
|
|
|
|
|
Key = x.Split("=")[0],
|
|
|
|
|
Value = x.Split("=")[1]
|
|
|
|
|
}).ToList();
|
2024-12-12 09:09:46 +00:00
|
|
|
|
2024-07-24 01:28:39 +00:00
|
|
|
var source = queries.FirstOrDefault(x => x.Key.IsEqualTo(DB_NAME_INDEX));
|
|
|
|
|
if (source != null)
|
|
|
|
|
{
|
|
|
|
|
databaseName = source.Value;
|
|
|
|
|
}
|
2023-08-26 04:41:01 +00:00
|
|
|
}
|
2024-07-24 00:38:18 +00:00
|
|
|
return databaseName;
|
2023-08-26 04:41:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private IMongoDatabase Database { get { return _mongoClient.GetDatabase(_mongoDbDatabaseName); } }
|
|
|
|
|
|
2024-02-15 20:43:36 +00:00
|
|
|
#region Indexes
|
2024-02-14 20:15:23 +00:00
|
|
|
private IMongoCollection<ConversationDocument> CreateConversationIndex()
|
|
|
|
|
{
|
|
|
|
|
var collection = Database.GetCollection<ConversationDocument>($"{_collectionPrefix}_Conversations");
|
|
|
|
|
var indexes = collection.Indexes.List().ToList();
|
|
|
|
|
var createTimeIndex = indexes.FirstOrDefault(x => x.GetElement("name").ToString().StartsWith("CreatedTime"));
|
|
|
|
|
if (createTimeIndex == null)
|
|
|
|
|
{
|
|
|
|
|
var indexDef = Builders<ConversationDocument>.IndexKeys.Descending(x => x.CreatedTime);
|
|
|
|
|
collection.Indexes.CreateOne(new CreateIndexModel<ConversationDocument>(indexDef));
|
|
|
|
|
}
|
|
|
|
|
return collection;
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-09 07:34:49 +00:00
|
|
|
private IMongoCollection<ConversationStateDocument> CreateConversationStateIndex()
|
|
|
|
|
{
|
|
|
|
|
var collection = Database.GetCollection<ConversationStateDocument>($"{_collectionPrefix}_ConversationStates");
|
|
|
|
|
var indexes = collection.Indexes.List().ToList();
|
|
|
|
|
var stateIndex = indexes.FirstOrDefault(x => x.GetElement("name").ToString().StartsWith("States.Key"));
|
|
|
|
|
if (stateIndex == null)
|
|
|
|
|
{
|
|
|
|
|
var indexDef = Builders<ConversationStateDocument>.IndexKeys.Ascending("States.Key");
|
|
|
|
|
collection.Indexes.CreateOne(new CreateIndexModel<ConversationStateDocument>(indexDef));
|
|
|
|
|
}
|
|
|
|
|
return collection;
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-14 20:15:23 +00:00
|
|
|
private IMongoCollection<AgentTaskDocument> CreateAgentTaskIndex()
|
|
|
|
|
{
|
|
|
|
|
var collection = Database.GetCollection<AgentTaskDocument>($"{_collectionPrefix}_AgentTasks");
|
|
|
|
|
var indexes = collection.Indexes.List().ToList();
|
|
|
|
|
var createTimeIndex = indexes.FirstOrDefault(x => x.GetElement("name").ToString().StartsWith("CreatedTime"));
|
|
|
|
|
if (createTimeIndex == null)
|
|
|
|
|
{
|
|
|
|
|
var indexDef = Builders<AgentTaskDocument>.IndexKeys.Descending(x => x.CreatedTime);
|
|
|
|
|
collection.Indexes.CreateOne(new CreateIndexModel<AgentTaskDocument>(indexDef));
|
|
|
|
|
}
|
|
|
|
|
return collection;
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-15 20:43:36 +00:00
|
|
|
private IMongoCollection<ConversationContentLogDocument> CreateContentLogIndex()
|
2024-02-14 20:15:23 +00:00
|
|
|
{
|
2024-02-15 20:43:36 +00:00
|
|
|
var collection = Database.GetCollection<ConversationContentLogDocument>($"{_collectionPrefix}_ConversationContentLogs");
|
|
|
|
|
var indexes = collection.Indexes.List().ToList();
|
|
|
|
|
var createTimeIndex = indexes.FirstOrDefault(x => x.GetElement("name").ToString().StartsWith("CreateTime"));
|
|
|
|
|
if (createTimeIndex == null)
|
2024-02-14 20:15:23 +00:00
|
|
|
{
|
2024-02-15 20:43:36 +00:00
|
|
|
var indexDef = Builders<ConversationContentLogDocument>.IndexKeys.Ascending(x => x.CreateTime);
|
|
|
|
|
collection.Indexes.CreateOne(new CreateIndexModel<ConversationContentLogDocument>(indexDef));
|
2024-02-14 20:15:23 +00:00
|
|
|
}
|
2024-02-15 20:43:36 +00:00
|
|
|
return collection;
|
2024-02-14 20:15:23 +00:00
|
|
|
}
|
2024-02-04 05:42:13 +00:00
|
|
|
|
2024-02-15 20:43:36 +00:00
|
|
|
private IMongoCollection<ConversationStateLogDocument> CreateStateLogIndex()
|
2024-02-14 20:15:23 +00:00
|
|
|
{
|
2024-02-15 20:43:36 +00:00
|
|
|
var collection = Database.GetCollection<ConversationStateLogDocument>($"{_collectionPrefix}_ConversationStateLogs");
|
|
|
|
|
var indexes = collection.Indexes.List().ToList();
|
|
|
|
|
var createTimeIndex = indexes.FirstOrDefault(x => x.GetElement("name").ToString().StartsWith("CreateTime"));
|
|
|
|
|
if (createTimeIndex == null)
|
2024-02-14 20:15:23 +00:00
|
|
|
{
|
2024-02-15 20:43:36 +00:00
|
|
|
var indexDef = Builders<ConversationStateLogDocument>.IndexKeys.Ascending(x => x.CreateTime);
|
|
|
|
|
collection.Indexes.CreateOne(new CreateIndexModel<ConversationStateLogDocument>(indexDef));
|
2024-02-14 20:15:23 +00:00
|
|
|
}
|
2024-02-15 20:43:36 +00:00
|
|
|
return collection;
|
2024-02-14 20:15:23 +00:00
|
|
|
}
|
2024-02-15 20:43:36 +00:00
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
public IMongoCollection<AgentDocument> Agents
|
|
|
|
|
=> Database.GetCollection<AgentDocument>($"{_collectionPrefix}_Agents");
|
|
|
|
|
|
|
|
|
|
public IMongoCollection<AgentTaskDocument> AgentTasks
|
|
|
|
|
=> CreateAgentTaskIndex();
|
|
|
|
|
|
|
|
|
|
public IMongoCollection<ConversationDocument> Conversations
|
|
|
|
|
=> CreateConversationIndex();
|
2023-08-26 04:41:01 +00:00
|
|
|
|
2023-12-13 17:58:25 +00:00
|
|
|
public IMongoCollection<ConversationDialogDocument> ConversationDialogs
|
|
|
|
|
=> Database.GetCollection<ConversationDialogDocument>($"{_collectionPrefix}_ConversationDialogs");
|
2023-09-06 04:41:49 +00:00
|
|
|
|
2024-01-01 04:56:10 +00:00
|
|
|
public IMongoCollection<ConversationStateDocument> ConversationStates
|
2024-07-09 07:34:49 +00:00
|
|
|
=> CreateConversationStateIndex();
|
2024-01-01 04:56:10 +00:00
|
|
|
|
2023-12-13 17:58:25 +00:00
|
|
|
public IMongoCollection<ExecutionLogDocument> ExectionLogs
|
|
|
|
|
=> Database.GetCollection<ExecutionLogDocument>($"{_collectionPrefix}_ExecutionLogs");
|
2023-10-23 20:33:24 +00:00
|
|
|
|
2024-02-15 20:43:36 +00:00
|
|
|
public IMongoCollection<LlmCompletionLogDocument> LlmCompletionLogs
|
|
|
|
|
=> Database.GetCollection<LlmCompletionLogDocument>($"{_collectionPrefix}_LlmCompletionLogs");
|
|
|
|
|
|
|
|
|
|
public IMongoCollection<ConversationContentLogDocument> ContentLogs
|
|
|
|
|
=> CreateContentLogIndex();
|
|
|
|
|
|
|
|
|
|
public IMongoCollection<ConversationStateLogDocument> StateLogs
|
|
|
|
|
=> CreateStateLogIndex();
|
|
|
|
|
|
2023-12-13 17:58:25 +00:00
|
|
|
public IMongoCollection<UserDocument> Users
|
|
|
|
|
=> Database.GetCollection<UserDocument>($"{_collectionPrefix}_Users");
|
2023-08-26 04:41:01 +00:00
|
|
|
|
2023-12-13 17:58:25 +00:00
|
|
|
public IMongoCollection<UserAgentDocument> UserAgents
|
|
|
|
|
=> Database.GetCollection<UserAgentDocument>($"{_collectionPrefix}_UserAgents");
|
2023-11-27 23:49:24 +00:00
|
|
|
|
2024-01-13 02:13:38 +00:00
|
|
|
public IMongoCollection<PluginDocument> Plugins
|
|
|
|
|
=> Database.GetCollection<PluginDocument>($"{_collectionPrefix}_Plugins");
|
2024-06-14 22:10:25 +00:00
|
|
|
|
|
|
|
|
public IMongoCollection<TranslationMemoryDocument> TranslationMemories
|
|
|
|
|
=> Database.GetCollection<TranslationMemoryDocument>($"{_collectionPrefix}_TranslationMemories");
|
2024-09-09 17:05:11 +00:00
|
|
|
|
|
|
|
|
public IMongoCollection<KnowledgeCollectionConfigDocument> KnowledgeCollectionConfigs
|
|
|
|
|
=> Database.GetCollection<KnowledgeCollectionConfigDocument>($"{_collectionPrefix}_KnowledgeCollectionConfigs");
|
2024-09-13 22:23:05 +00:00
|
|
|
|
2024-09-17 17:50:39 +00:00
|
|
|
public IMongoCollection<KnowledgeCollectionFileMetaDocument> KnowledgeCollectionFileMeta
|
|
|
|
|
=> Database.GetCollection<KnowledgeCollectionFileMetaDocument>($"{_collectionPrefix}_KnowledgeCollectionFileMeta");
|
2024-11-14 23:33:26 +00:00
|
|
|
|
|
|
|
|
public IMongoCollection<RoleDocument> Roles
|
|
|
|
|
=> Database.GetCollection<RoleDocument>($"{_collectionPrefix}_Roles");
|
|
|
|
|
|
|
|
|
|
public IMongoCollection<RoleAgentDocument> RoleAgents
|
|
|
|
|
=> Database.GetCollection<RoleAgentDocument>($"{_collectionPrefix}_RoleAgents");
|
2024-12-09 00:29:04 +00:00
|
|
|
|
|
|
|
|
public IMongoCollection<CrontabItemDocument> CrontabItems
|
|
|
|
|
=> Database.GetCollection<CrontabItemDocument>($"{_collectionPrefix}_CronTabItems");
|
2024-12-12 09:09:46 +00:00
|
|
|
|
2023-08-26 04:41:01 +00:00
|
|
|
}
|