-
-
Notifications
You must be signed in to change notification settings - Fork 596
optimize mongodb use async method #1260
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
PR Compliance Guide 🔍Below is a summary of compliance checks for this PR:
Compliance status legend🟢 - Fully Compliant🟡 - Partial Compliant 🔴 - Not Compliant ⚪ - Requires Further Human Verification 🏷️ - Compliance label |
|||||||||||||||||||||||||
PR Code Suggestions ✨Explore these optional code suggestions:
|
|||||||||||||||||||||||||||||||
|
Reviewed |
PR Type
Enhancement
Description
Comprehensive conversion of synchronous methods to async/await pattern across the entire codebase
Converted all repository implementations (FileRepository and MongoRepository) to use async methods for database and file I/O operations
Replaced synchronous MongoDB operations with async equivalents (
UpdateOneAsync,InsertManyAsync,DeleteManyAsync, etc.)Replaced synchronous file I/O operations with async versions (
File.WriteAllTextAsync,File.ReadAllTextAsync, etc.)Replaced
Thread.Sleep()calls withawait Task.Delay()for non-blocking delaysReplaced
lockstatements withSemaphoreSlimfor thread-safe async operations in conversation managementUpdated all interface signatures in
IBotSharpRepositoryandIConversationServiceto returnTask,Task<T>, orTask<bool>Updated service layer methods across ConversationService, AgentService, RoleService, and other services to properly await async repository calls
Updated controller methods to use
awaitfor async service operationsUpdated plugin and hook implementations to properly handle async method calls
Removed unnecessary
.ConfigureAwait(false).GetAwaiter().GetResult()calls and replaced with properawaitsyntaxDiagram Walkthrough
File Walkthrough
51 files
FileRepository.Agent.cs
Convert FileRepository Agent methods to async operationssrc/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.Agent.cs
Taskmethods withawaitkeywords
File.WriteAllText,File.ReadAllText,File.WriteAllLines) with async equivalents(
File.WriteAllTextAsync,File.ReadAllTextAsync,File.WriteAllLinesAsync)Thread.Sleep()calls withawait Task.Delay()for non-blockingdelays
Task,Task, orTaskinstead ofvoidor direct return typesMongoRepository.Agent.cs
Convert MongoRepository Agent methods to async operationssrc/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.Agent.cs
(e.g.,
UpdateOnetoUpdateOneAsync,InsertManytoInsertManyAsync,DeleteManytoDeleteManyAsync)Task,Task, orTaskinstead ofvoidor direct return typesawaitkeywords for all async MongoDB driver callsFileRepository.Conversation.cs
Convert FileRepository Conversation methods to async operationssrc/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.Conversation.cs
throughout conversation management methods
lockstatements withSemaphoreSlimfor thread-safe asyncoperations on
_dialogLockand_stateLockTask,Task, orTaskinstead ofvoidor direct return typesThread.Sleep()withawait Task.Delay()for non-blockingdelays
MongoRepository.Conversation.cs
Convert MongoRepository Conversation methods to async operationssrc/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.Conversation.cs
awaitkeywordsTask,Task, orTaskinstead ofvoidor direct return typesand aggregation pipelines
GetIdleConversationsto use MongoDB filter builders insteadof LINQ for better async support
IBotSharpRepository.cs
Update IBotSharpRepository interface signatures to asyncsrc/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs
Task,Task, orTaskinstead of
voidor direct return typesstatistics, translations, and knowledge base operations
NotImplementedExceptionimplementations
StreamingLogHook.cs
Convert StreamingLogHook methods to async operationssrc/Plugins/BotSharp.Plugin.ChatHub/Hooks/StreamingLogHook.cs
BuildContentLogandBuildStateLogmethods to asyncTaskmethods
awaitkeywordSaveConversationContentLogandSaveConversationStateLogcallsto use
awaitfor async repository operationsConversationService.cs
Update ConversationService to use async repository methodssrc/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.cs
awaitfor async repository operations(
DeleteConversations,UpdateConversationTitle,UpdateConversationTags,etc.)
GetDialogHistoryandSetConversationIdto async methods withawaitkeywordsGetAgentMessageCountandSaveStatesto async methodsawaitfor async state loading and dialog retrieval operationsFileRepository.KnowledgeBase.cs
Convert FileRepository KnowledgeBase methods to async operationssrc/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.KnowledgeBase.cs
(
File.WriteAllTextAsync,File.ReadAllTextAsync)Taskinstead ofboolawaitkeywords for all async file operationsMongoRepository.Role.cs
MongoDB Role repository methods converted to asyncsrc/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.Role.cs
RefreshRoles,GetRoles,GetRoleDetails, andUpdateRolemethods to async by adding
async Taskreturn typesDeleteMany,InsertMany,Find().ToList(),UpdateOne) with their async counterparts(
DeleteManyAsync,InsertManyAsync,ToListAsync,UpdateOneAsync)awaitkeywords for all async MongoDB callsFileRepository.Log.cs
File repository log methods converted to asyncsrc/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.Log.cs
SaveLlmCompletionLog,SaveConversationContentLog,GetConversationContentLogs,SaveConversationStateLog,GetConversationStateLogs) to asyncFile.WriteAllText,File.ReadAllText) with async versions (File.WriteAllTextAsync,File.ReadAllTextAsync)awaitkeywords for all async file operationsFileRepository.Role.cs
File repository role methods converted to asyncsrc/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.Role.cs
Taskreturn typesThread.Sleep(50)withawait Task.Delay(50)for async delaysFile.WriteAllTextAsync)Task.FromResult()for consistencyPluginLoader.cs
Plugin loader methods converted to asyncsrc/Infrastructure/BotSharp.Core/Plugins/PluginLoader.cs
GetPlugins,GetPagedPlugins, andUpdatePluginStatusmethodsto async
await.ConfigureAwait(false).GetAwaiter().GetResult()calls and using properawaitMongoRepository.Log.cs
MongoDB log repository methods converted to asyncsrc/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.Log.cs
Taskreturn types(
InsertOneAsync,FirstOrDefaultAsync,ToListAsync)awaitkeywords for all async MongoDB callsConversationStateService.cs
Conversation state service methods converted to asyncsrc/Infrastructure/BotSharp.Core/Conversations/Services/ConversationStateService.cs
LoadandSavemethods to async withTaskreturn typesawait.Wait()calls to properawaitsyntax for async operationsDisposemethod to use.ConfigureAwait(false).GetAwaiter().GetResult()for async cleanupMongoRepository.KnowledgeBase.cs
MongoDB knowledge base repository methods converted to asyncsrc/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.KnowledgeBase.cs
(
DeleteManyAsync,InsertManyAsync,ReplaceOneAsync,ToListAsync)awaitkeywords for all async database callsMongoRepository.AgentTask.cs
MongoDB agent task repository methods converted to asyncsrc/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.AgentTask.cs
Taskreturn types(
FirstOrDefaultAsync,InsertOneAsync,InsertManyAsync,ReplaceOneAsync,DeleteManyAsync)async Taskinstead ofvoidfor asyncoperations
FileRepository.AgentTask.cs
File repository agent task methods converted to asyncsrc/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.AgentTask.cs
Taskreturn types(
File.WriteAllTextAsync)BulkInsertAgentTasksmethod with proper async iterationTask.FromResult()for consistencyAgentService.Coding.cs
Agent service coding methods updated for async callssrc/Infrastructure/BotSharp.Core/Agents/Services/AgentService.Coding.cs
awaitfor async database operationsTask.FromResult()wrappersFileRepository.Stats.cs
File repository stats methods converted to asyncsrc/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.Stats.cs
Taskreturn types(
File.ReadAllTextAsync,File.WriteAllTextAsync)awaitkeywords for all async file operationsEvaluatingService.Evaluate.cs
Evaluation service methods converted to asyncsrc/Infrastructure/BotSharp.Core/Evaluations/Services/EvaluatingService.Evaluate.cs
GetInitialStatesmethod to async withTaskreturn typeawaitfor async repository operationsFileRepository.Crontab.cs
File repository crontab methods converted to asyncsrc/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.Crontab.cs
Taskreturn types(
File.WriteAllTextAsync,File.ReadAllTextAsync)Task.FromResult()for consistencyConversationStorage.cs
Conversation storage methods converted to async with semaphoresrc/Infrastructure/BotSharp.Core/Conversations/Services/ConversationStorage.cs
AppendandGetDialogsmethods to async withTaskreturntypes
SemaphoreSlimfor async-safesynchronization
awaitfor async repository operationsBotSharpConversationSideCar.cs
Conversation sidecar methods converted to asyncsrc/Infrastructure/BotSharp.Core.SideCar/Services/BotSharpConversationSideCar.cs
Taskreturntypes
await Task.CompletedTaskfor methods that don't perform actualasync operations
TaskorTaskinstead ofvoidMongoRepository.Translation.cs
MongoDB translation repository methods converted to asyncsrc/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.Translation.cs
Taskreturn types(
ToListAsync,InsertManyAsync,ReplaceOneAsync)Thread.Sleep(50)withawait Task.Delay(50)for async delaysMongoRepository.AgentCodeScript.cs
MongoDB agent code script repository methods converted to asyncsrc/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.AgentCodeScript.cs
Taskreturn types(
BulkWriteAsync,InsertManyAsync,DeleteManyAsync)awaitkeywords for all async database callsConversationController.cs
Conversation controller methods updated for async callssrc/Infrastructure/BotSharp.OpenAPI/Controllers/Conversation/ConversationController.cs
awaitfor async servicecalls
SetConversationIdandGetDialogHistorycalls to async withawaitFileRepository.Translation.cs
File repository translation methods converted to asyncsrc/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.Translation.cs
Taskreturn types(
File.ReadAllTextAsync,File.WriteAllTextAsync)awaitkeywords for all async file operationsTwilioMessageQueueService.cs
Twilio message queue service methods converted to asyncsrc/Plugins/BotSharp.Plugin.Twilio/Services/TwilioMessageQueueService.cs
Taskreturn typesawaitfor async service operationsGetDialogHistoryto async equivalentsFileRepository.AgentCodeScript.cs
File repository agent code script methods converted to asyncsrc/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.AgentCodeScript.cs
Taskreturn types(
File.WriteAllTextAsync)Task.FromResult()for consistencyRealtimeHub.cs
Realtime hub methods updated for async callssrc/Infrastructure/BotSharp.Core.Realtime/Services/RealtimeHub.cs
awaitfor async service operationsSetConversationId,GetDialogHistory, andAppendcalls to asyncwith
awaitRoleService.cs
Role service methods updated for async callssrc/Infrastructure/BotSharp.Core/Roles/Services/RoleService.cs
awaitfor async repository operationsGetRoleOptionsto returnTask>instead ofasync TaskConversationService.Migration.cs
Conversation migration service methods updated for async callssrc/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.Migration.cs
awaitfor async repository operationsAgentService.GetAgents.cs
Agent service GetAgents methods updated for async callssrc/Infrastructure/BotSharp.Core/Agents/Services/AgentService.GetAgents.cs
awaitfor async repository and serviceoperations
GetAgentsandGetPlugincalls to async withawaitIConversationService.cs
Conversation service interface methods converted to asyncsrc/Infrastructure/BotSharp.Abstraction/Conversations/IConversationService.cs
Taskfor asyncoperations
SetConversationId,GetDialogHistory, andSaveStatesto asyncPlotChartFn.cs
Chart handler function methods converted to asyncsrc/Plugins/BotSharp.Plugin.ChartHandler/Functions/PlotChartFn.cs
Taskreturn typeawaitfor async repository and serviceoperations
GetDialogHistoryandGetAgentTemplatecalls to async withawaitPyProgrammerFn.cs
Python interpreter function methods converted to asyncsrc/Plugins/BotSharp.Plugin.PythonInterpreter/Functions/PyProgrammerFn.cs
Taskreturn typeawaitfor async repository and serviceoperations
GetDialogHistoryandGetAgentTemplatecalls to async withawaitAgentTaskService.cs
Agent task service methods updated for async callssrc/Infrastructure/BotSharp.Core/Tasks/Services/AgentTaskService.cs
awaitfor async repository operationsTask.FromResult()wrappersPluginController.cs
Plugin controller methods updated for async callssrc/Infrastructure/BotSharp.OpenAPI/Controllers/Setting/PluginController.cs
awaitfor async plugin loaderoperations
GetPagedPluginsandGetPluginscalls to async withawaitSqlDriverController.cs
SQL driver controller methods updated for async callssrc/Plugins/BotSharp.Plugin.SqlDriver/Controllers/SqlDriverController.cs
awaitfor async service operationsSetConversationIdandAppendcalls to async withawaitMongoRepository.Crontab.cs
MongoDB crontab repository methods converted to asyncsrc/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.Crontab.cs
Taskreturn types(
ReplaceOneAsync,DeleteManyAsync)awaitkeywords for all async database callsTokenStatsConversationHook.cs
Convert AddToken to async method callsrc/Infrastructure/BotSharp.Logger/Hooks/TokenStatsConversationHook.cs
AddTokenmethod call to useawaitkeyword, making it properlyasynchronous
await Task.CompletedTaskstatementoperation
ReadImageFn.cs
Add await to GetDialogHistory async callsrc/Plugins/BotSharp.Plugin.ImageHandler/Functions/ReadImageFn.cs
awaitkeyword toGetDialogHistory()method callAgentService.cs
Add await to GetUserAgents database callsrc/Infrastructure/BotSharp.Core/Agents/Services/AgentService.cs
awaitkeyword to_db.GetUserAgents()method callReadPdfFn.cs
Add await to GetDialogHistory async callsrc/Plugins/BotSharp.Plugin.FileHandler/Functions/ReadPdfFn.cs
awaitkeyword toGetDialogHistory()method callReadAudioFn.cs
Add await to GetDialogHistory async callsrc/Plugins/BotSharp.Plugin.AudioHandler/Functions/ReadAudioFn.cs
awaitkeyword toGetDialogHistory()method callReadExcelFn.cs
Add await to GetDialogHistory async callsrc/Plugins/BotSharp.Plugin.ExcelHandler/Functions/ReadExcelFn.cs
awaitkeyword toGetDialogHistory()method callWelcomeHook.cs
Add await to storage Append async callsrc/Plugins/BotSharp.Plugin.ChatHub/Hooks/WelcomeHook.cs
awaitkeyword to_storage.Append()method callWebIntelligentSearchFn.cs
Add await to GetDialogHistory async callsrc/Infrastructure/BotSharp.Core/WebSearch/Functions/WebIntelligentSearchFn.cs
awaitkeyword toGetDialogHistory()method callIBotSharpStatsService.cs
Convert UpdateStats interface to async methodsrc/Infrastructure/BotSharp.Abstraction/Statistics/Services/IBotSharpStatsService.cs
UpdateStatsmethod signature to returnTaskinstead ofboolISettingService.cs
Convert GetDetail interface to async methodsrc/Infrastructure/BotSharp.Abstraction/Settings/ISettingService.cs
GetDetailmethod signature to returnTask instead of
objectConverts the method to be asynchronousTwilioConversationHook.cs
Add await to states Save async callsrc/Plugins/BotSharp.Plugin.Twilio/Hooks/TwilioConversationHook.cs
awaitkeyword tostates.Save()method call61 files