BotSharp/src/Plugins/BotSharp.Plugin.MongoStorage/MongoDbContext.cs

151 lines
6.5 KiB
C#
Raw Normal View History

2024-07-22 04:33:22 +00:00
using System.Web;
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
2023-09-12 22:34:50 +00:00
public MongoDbContext(BotSharpDatabaseSettings dbSettings)
2023-08-26 04:41:01 +00:00
{
2024-07-22 04:58:40 +00:00
var mongoDbConnectionString = dbSettings.BotSharpMongoDb.ConnectionString;
var dbNameIndex = dbSettings.BotSharpMongoDb.DbNameIndex;
2023-08-26 04:41:01 +00:00
_mongoClient = new MongoClient(mongoDbConnectionString);
2024-07-22 04:58:40 +00:00
_mongoDbDatabaseName = GetDatabaseName(mongoDbConnectionString, dbNameIndex);
2023-10-16 22:52:48 +00:00
_collectionPrefix = dbSettings.TablePrefix.IfNullOrEmptyAs("BotSharp");
2023-08-26 04:41:01 +00:00
}
2024-07-22 04:58:40 +00:00
private string GetDatabaseName(string mongoDbConnectionString, string? dbNameIndex = null)
2023-08-26 04:41:01 +00:00
{
2024-07-22 04:33:22 +00:00
var dbName = string.Empty;
if (!Uri.TryCreate(mongoDbConnectionString, UriKind.Absolute, out var conn))
2023-08-26 04:41:01 +00:00
{
2024-07-22 04:33:22 +00:00
return dbName;
2023-08-26 04:41:01 +00:00
}
2024-07-22 04:33:22 +00:00
var query = HttpUtility.ParseQueryString(conn.Query);
var keys = query.AllKeys ?? [];
2024-07-22 04:58:40 +00:00
if (!string.IsNullOrWhiteSpace(dbNameIndex) && keys.Contains(dbNameIndex))
{
dbName = query[dbNameIndex];
}
else
2024-07-22 04:33:22 +00:00
{
2024-07-22 04:58:40 +00:00
dbName = conn.Segments?.FirstOrDefault(x => x != "/") ?? string.Empty;
2024-07-22 04:33:22 +00:00
}
return dbName;
2023-08-26 04:41:01 +00:00
}
2024-07-22 04:33:22 +00:00
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
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
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();
public IMongoCollection<UserDocument> Users
=> Database.GetCollection<UserDocument>($"{_collectionPrefix}_Users");
2023-08-26 04:41:01 +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");
2023-08-26 04:41:01 +00:00
}