2023-09-01 22:33:36 +00:00
|
|
|
using BotSharp.Plugin.MongoStorage.Collections;
|
2023-08-26 04:41:01 +00:00
|
|
|
|
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
|
|
|
{
|
2023-10-16 22:52:48 +00:00
|
|
|
var mongoDbConnectionString = dbSettings.BotSharpMongoDb;
|
2023-08-26 04:41:01 +00:00
|
|
|
_mongoClient = new MongoClient(mongoDbConnectionString);
|
|
|
|
|
_mongoDbDatabaseName = GetDatabaseName(mongoDbConnectionString);
|
2023-10-16 22:52:48 +00:00
|
|
|
_collectionPrefix = dbSettings.TablePrefix.IfNullOrEmptyAs("BotSharp");
|
2023-08-26 04:41:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string GetDatabaseName(string mongoDbConnectionString)
|
|
|
|
|
{
|
|
|
|
|
var databaseName = mongoDbConnectionString.Substring(mongoDbConnectionString.LastIndexOf("/", StringComparison.InvariantCultureIgnoreCase) + 1);
|
|
|
|
|
if (databaseName.Contains("?"))
|
|
|
|
|
{
|
|
|
|
|
databaseName = databaseName.Substring(0, databaseName.IndexOf("?", StringComparison.InvariantCultureIgnoreCase));
|
|
|
|
|
}
|
|
|
|
|
return databaseName;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private IMongoDatabase Database { get { return _mongoClient.GetDatabase(_mongoDbDatabaseName); } }
|
|
|
|
|
|
2023-12-13 17:58:25 +00:00
|
|
|
public IMongoCollection<AgentDocument> Agents
|
|
|
|
|
=> Database.GetCollection<AgentDocument>($"{_collectionPrefix}_Agents");
|
2023-08-26 04:41:01 +00:00
|
|
|
|
2023-12-13 17:58:25 +00:00
|
|
|
public IMongoCollection<ConversationDocument> Conversations
|
|
|
|
|
=> Database.GetCollection<ConversationDocument>($"{_collectionPrefix}_Conversations");
|
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
|
|
|
|
|
=> Database.GetCollection<ConversationStateDocument>($"{_collectionPrefix}_ConversationStates");
|
|
|
|
|
|
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
|
|
|
|
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
|
|
|
|
2023-12-13 17:58:25 +00:00
|
|
|
public IMongoCollection<LlmCompletionLogDocument> LlmCompletionLogs
|
|
|
|
|
=> Database.GetCollection<LlmCompletionLogDocument>($"{_collectionPrefix}_Llm_Completion_Logs");
|
2023-08-26 04:41:01 +00:00
|
|
|
}
|