diff --git a/src/Infrastructure/BotSharp.Abstraction/Agents/IAgentService.cs b/src/Infrastructure/BotSharp.Abstraction/Agents/IAgentService.cs index d0284b224..503617666 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Agents/IAgentService.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Agents/IAgentService.cs @@ -67,7 +67,7 @@ public interface IAgentService Task> GetUserAgents(string userId); - PluginDef GetPlugin(string agentId); + Task GetPlugin(string agentId); Task> GetAgentCodeScripts(string agentId, AgentCodeScriptFilter? filter = null) => Task.FromResult(new List()); diff --git a/src/Infrastructure/BotSharp.Abstraction/Conversations/IConversationService.cs b/src/Infrastructure/BotSharp.Abstraction/Conversations/IConversationService.cs index 322d86b6f..4a39028ed 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Conversations/IConversationService.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Conversations/IConversationService.cs @@ -7,7 +7,7 @@ public interface IConversationService IConversationStateService States { get; } string ConversationId { get; } Task NewConversation(Conversation conversation); - void SetConversationId(string conversationId, List states, bool isReadOnly = false); + Task SetConversationId(string conversationId, List states, bool isReadOnly = false); Task GetConversation(string id, bool isLoadStates = false); Task> GetConversations(ConversationFilter filter); Task UpdateConversationTitle(string id, string title); @@ -40,7 +40,7 @@ Task SendMessage(string agentId, PostbackMessageModel? replyMessage, Func onResponseReceived); - List GetDialogHistory(int lastCount = 100, bool fromBreakpoint = true, IEnumerable? includeMessageTypes = null); + Task> GetDialogHistory(int lastCount = 100, bool fromBreakpoint = true, IEnumerable? includeMessageTypes = null); Task CleanHistory(string agentId); /// @@ -58,7 +58,7 @@ Task SendMessage(string agentId, bool IsConversationMode(); - void SaveStates(); + Task SaveStates(); Task> GetConversationStateSearhKeys(ConversationStateKeysFilter filter); diff --git a/src/Infrastructure/BotSharp.Abstraction/Conversations/IConversationStateService.cs b/src/Infrastructure/BotSharp.Abstraction/Conversations/IConversationStateService.cs index a5563cf30..64a96bac9 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Conversations/IConversationStateService.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Conversations/IConversationStateService.cs @@ -9,7 +9,7 @@ namespace BotSharp.Abstraction.Conversations; public interface IConversationStateService : IDisposable { string GetConversationId(); - Dictionary Load(string conversationId, bool isReadOnly = false); + Task> Load(string conversationId, bool isReadOnly = false); string GetState(string name, string defaultValue = ""); bool ContainsState(string name); Dictionary GetStates(); @@ -18,7 +18,7 @@ IConversationStateService SetState(string name, T value, bool isNeedVersion = void SaveStateByArgs(JsonDocument args); bool RemoveState(string name); void CleanStates(params string[] excludedStates); - void Save(); + Task Save(); ConversationState GetCurrentState(); void SetCurrentState(ConversationState state); diff --git a/src/Infrastructure/BotSharp.Abstraction/Conversations/IConversationStorage.cs b/src/Infrastructure/BotSharp.Abstraction/Conversations/IConversationStorage.cs index f5f5492c9..ae4e628fc 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Conversations/IConversationStorage.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Conversations/IConversationStorage.cs @@ -2,7 +2,7 @@ namespace BotSharp.Abstraction.Conversations; public interface IConversationStorage { - void Append(string conversationId, RoleDialogModel dialog); - void Append(string conversationId, IEnumerable dialogs); - List GetDialogs(string conversationId); + Task Append(string conversationId, RoleDialogModel dialog); + Task Append(string conversationId, IEnumerable dialogs); + Task> GetDialogs(string conversationId); } diff --git a/src/Infrastructure/BotSharp.Abstraction/Conversations/ITokenStatistics.cs b/src/Infrastructure/BotSharp.Abstraction/Conversations/ITokenStatistics.cs index 8e2f8f223..47e6f2d43 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Conversations/ITokenStatistics.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Conversations/ITokenStatistics.cs @@ -7,6 +7,6 @@ public interface ITokenStatistics float Cost { get; } void StartTimer(); void StopTimer(); - void AddToken(TokenStatsModel stats, RoleDialogModel message); + Task AddToken(TokenStatsModel stats, RoleDialogModel message); void PrintStatistics(); } \ No newline at end of file diff --git a/src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs b/src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs index 60c4120e5..20af9f03c 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs @@ -21,20 +21,20 @@ namespace BotSharp.Abstraction.Repositories; public interface IBotSharpRepository : IHaveServiceProvider { #region Plugin - PluginConfig GetPluginConfig() + Task GetPluginConfig() => throw new NotImplementedException(); - void SavePluginConfig(PluginConfig config) + Task SavePluginConfig(PluginConfig config) => throw new NotImplementedException(); #endregion #region Role - bool RefreshRoles(IEnumerable roles) + Task RefreshRoles(IEnumerable roles) => throw new NotImplementedException(); - IEnumerable GetRoles(RoleFilter filter) + Task> GetRoles(RoleFilter filter) => throw new NotImplementedException(); - Role? GetRoleDetails(string roleId, bool includeAgent = false) + Task GetRoleDetails(string roleId, bool includeAgent = false) => throw new NotImplementedException(); - bool UpdateRole(Role role, bool updateRoleAgents = false) + Task UpdateRole(Role role, bool updateRoleAgents = false) => throw new NotImplementedException(); #endregion @@ -68,127 +68,127 @@ bool UpdateRole(Role role, bool updateRoleAgents = false) #endregion #region Agent - void UpdateAgent(Agent agent, AgentField field) + Task UpdateAgent(Agent agent, AgentField field) => throw new NotImplementedException(); Agent? GetAgent(string agentId, bool basicsOnly = false) => throw new NotImplementedException(); Task GetAgentAsync(string agentId, bool basicsOnly = false) => throw new NotImplementedException(); - List GetAgents(AgentFilter filter) + Task> GetAgents(AgentFilter filter) => throw new NotImplementedException(); - List GetUserAgents(string userId) + Task> GetUserAgents(string userId) => throw new NotImplementedException(); - void BulkInsertAgents(List agents) + Task BulkInsertAgents(List agents) => throw new NotImplementedException(); - void BulkInsertUserAgents(List userAgents) + Task BulkInsertUserAgents(List userAgents) => throw new NotImplementedException(); - bool DeleteAgents() + Task DeleteAgents() => throw new NotImplementedException(); - bool DeleteAgent(string agentId, AgentDeleteOptions? options = null) + Task DeleteAgent(string agentId, AgentDeleteOptions? options = null) => throw new NotImplementedException(); - List GetAgentResponses(string agentId, string prefix, string intent) + Task> GetAgentResponses(string agentId, string prefix, string intent) => throw new NotImplementedException(); - string GetAgentTemplate(string agentId, string templateName) + Task GetAgentTemplate(string agentId, string templateName) => throw new NotImplementedException(); - bool PatchAgentTemplate(string agentId, AgentTemplate template) + Task PatchAgentTemplate(string agentId, AgentTemplate template) => throw new NotImplementedException(); - bool UpdateAgentLabels(string agentId, List labels) + Task UpdateAgentLabels(string agentId, List labels) => throw new NotImplementedException(); - bool AppendAgentLabels(string agentId, List labels) + Task AppendAgentLabels(string agentId, List labels) => throw new NotImplementedException(); #endregion #region Agent Task ValueTask> GetAgentTasks(AgentTaskFilter filter) => throw new NotImplementedException(); - AgentTask? GetAgentTask(string agentId, string taskId) + Task GetAgentTask(string agentId, string taskId) => throw new NotImplementedException(); - void InsertAgentTask(AgentTask task) + Task InsertAgentTask(AgentTask task) => throw new NotImplementedException(); - void BulkInsertAgentTasks(string agentId, List tasks) + Task BulkInsertAgentTasks(string agentId, List tasks) => throw new NotImplementedException(); - void UpdateAgentTask(AgentTask task, AgentTaskField field) + Task UpdateAgentTask(AgentTask task, AgentTaskField field) => throw new NotImplementedException(); - bool DeleteAgentTasks(string agentId, List? taskIds = null) + Task DeleteAgentTasks(string agentId, List? taskIds = null) => throw new NotImplementedException(); #endregion #region Agent Code - List GetAgentCodeScripts(string agentId, AgentCodeScriptFilter? filter = null) + Task> GetAgentCodeScripts(string agentId, AgentCodeScriptFilter? filter = null) => throw new NotImplementedException(); Task GetAgentCodeScript(string agentId, string scriptName, string scriptType = AgentCodeScriptType.Src) => throw new NotImplementedException(); - bool UpdateAgentCodeScripts(string agentId, List scripts, AgentCodeScriptDbUpdateOptions? options = null) + Task UpdateAgentCodeScripts(string agentId, List scripts, AgentCodeScriptDbUpdateOptions? options = null) => throw new NotImplementedException(); - bool BulkInsertAgentCodeScripts(string agentId, List scripts) + Task BulkInsertAgentCodeScripts(string agentId, List scripts) => throw new NotImplementedException(); - bool DeleteAgentCodeScripts(string agentId, List? scripts = null) + Task DeleteAgentCodeScripts(string agentId, List? scripts = null) => throw new NotImplementedException(); #endregion #region Conversation Task CreateNewConversation(Conversation conversation) => throw new NotImplementedException(); - bool DeleteConversations(IEnumerable conversationIds) + Task DeleteConversations(IEnumerable conversationIds) => throw new NotImplementedException(); - List GetConversationDialogs(string conversationId) + Task> GetConversationDialogs(string conversationId) => throw new NotImplementedException(); - void AppendConversationDialogs(string conversationId, List dialogs) + Task AppendConversationDialogs(string conversationId, List dialogs) => throw new NotImplementedException(); - ConversationState GetConversationStates(string conversationId) + Task GetConversationStates(string conversationId) => throw new NotImplementedException(); - void UpdateConversationStates(string conversationId, List states) + Task UpdateConversationStates(string conversationId, List states) => throw new NotImplementedException(); - void UpdateConversationStatus(string conversationId, string status) + Task UpdateConversationStatus(string conversationId, string status) => throw new NotImplementedException(); Task GetConversation(string conversationId, bool isLoadStates = false) => throw new NotImplementedException(); ValueTask> GetConversations(ConversationFilter filter) => throw new NotImplementedException(); - void UpdateConversationTitle(string conversationId, string title) + Task UpdateConversationTitle(string conversationId, string title) => throw new NotImplementedException(); - void UpdateConversationTitleAlias(string conversationId, string titleAlias) + Task UpdateConversationTitleAlias(string conversationId, string titleAlias) => throw new NotImplementedException(); - bool UpdateConversationTags(string conversationId, List toAddTags, List toDeleteTags) + Task UpdateConversationTags(string conversationId, List toAddTags, List toDeleteTags) => throw new NotImplementedException(); - bool AppendConversationTags(string conversationId, List tags) + Task AppendConversationTags(string conversationId, List tags) => throw new NotImplementedException(); - bool UpdateConversationMessage(string conversationId, UpdateMessageRequest request) + Task UpdateConversationMessage(string conversationId, UpdateMessageRequest request) => throw new NotImplementedException(); - void UpdateConversationBreakpoint(string conversationId, ConversationBreakpoint breakpoint) + Task UpdateConversationBreakpoint(string conversationId, ConversationBreakpoint breakpoint) => throw new NotImplementedException(); - ConversationBreakpoint? GetConversationBreakpoint(string conversationId) + Task GetConversationBreakpoint(string conversationId) => throw new NotImplementedException(); - List GetLastConversations() + Task> GetLastConversations() => throw new NotImplementedException(); - List GetIdleConversations(int batchSize, int messageLimit, int bufferHours, IEnumerable excludeAgentIds) + Task> GetIdleConversations(int batchSize, int messageLimit, int bufferHours, IEnumerable excludeAgentIds) => throw new NotImplementedException(); - List TruncateConversation(string conversationId, string messageId, bool cleanLog = false) + Task> TruncateConversation(string conversationId, string messageId, bool cleanLog = false) => throw new NotImplementedException(); - List GetConversationStateSearchKeys(ConversationStateKeysFilter filter) + Task> GetConversationStateSearchKeys(ConversationStateKeysFilter filter) => throw new NotImplementedException(); - List GetConversationsToMigrate(int batchSize = 100) + Task> GetConversationsToMigrate(int batchSize = 100) => throw new NotImplementedException(); - bool MigrateConvsersationLatestStates(string conversationId) + Task MigrateConvsersationLatestStates(string conversationId) => throw new NotImplementedException(); #endregion #region LLM Completion Log - void SaveLlmCompletionLog(LlmCompletionLog log) + Task SaveLlmCompletionLog(LlmCompletionLog log) => throw new NotImplementedException(); #endregion #region Conversation Content Log - void SaveConversationContentLog(ContentLogOutputModel log) + Task SaveConversationContentLog(ContentLogOutputModel log) => throw new NotImplementedException(); - DateTimePagination GetConversationContentLogs(string conversationId, ConversationLogFilter filter) + Task> GetConversationContentLogs(string conversationId, ConversationLogFilter filter) => throw new NotImplementedException(); #endregion #region Conversation State Log - void SaveConversationStateLog(ConversationStateLogModel log) + Task SaveConversationStateLog(ConversationStateLogModel log) => throw new NotImplementedException(); - DateTimePagination GetConversationStateLogs(string conversationId, ConversationLogFilter filter) + Task> GetConversationStateLogs(string conversationId, ConversationLogFilter filter) => throw new NotImplementedException(); #endregion @@ -207,17 +207,17 @@ Task UpdateInstructionLogStates(UpdateInstructionLogStatesModel updateInst #endregion #region Statistics - BotSharpStats? GetGlobalStats(string agentId, DateTime recordTime, StatsInterval interval) + Task GetGlobalStats(string agentId, DateTime recordTime, StatsInterval interval) => throw new NotImplementedException(); - bool SaveGlobalStats(BotSharpStatsDelta delta) + Task SaveGlobalStats(BotSharpStatsDelta delta) => throw new NotImplementedException(); #endregion #region Translation - IEnumerable GetTranslationMemories(IEnumerable queries) + Task> GetTranslationMemories(IEnumerable queries) => throw new NotImplementedException(); - bool SaveTranslationMemories(IEnumerable inputs) + Task SaveTranslationMemories(IEnumerable inputs) => throw new NotImplementedException(); #endregion @@ -229,15 +229,15 @@ bool SaveTranslationMemories(IEnumerable inputs) /// /// /// - bool AddKnowledgeCollectionConfigs(List configs, bool reset = false) + Task AddKnowledgeCollectionConfigs(List configs, bool reset = false) => throw new NotImplementedException(); - bool DeleteKnowledgeCollectionConfig(string collectionName) + Task DeleteKnowledgeCollectionConfig(string collectionName) => throw new NotImplementedException(); Task> GetKnowledgeCollectionConfigs(VectorCollectionConfigFilter filter) => throw new NotImplementedException(); Task GetKnowledgeCollectionConfig(string collectionName, string vectorStroageProvider) => throw new NotImplementedException(); - bool SaveKnolwedgeBaseFileMeta(KnowledgeDocMetaData metaData) + Task SaveKnolwedgeBaseFileMeta(KnowledgeDocMetaData metaData) => throw new NotImplementedException(); /// @@ -247,16 +247,16 @@ bool SaveKnolwedgeBaseFileMeta(KnowledgeDocMetaData metaData) /// /// /// - bool DeleteKnolwedgeBaseFileMeta(string collectionName, string vectorStoreProvider, Guid? fileId = null) + Task DeleteKnolwedgeBaseFileMeta(string collectionName, string vectorStoreProvider, Guid? fileId = null) => throw new NotImplementedException(); ValueTask> GetKnowledgeBaseFileMeta(string collectionName, string vectorStoreProvider, KnowledgeFileFilter filter) => throw new NotImplementedException(); #endregion #region Crontab - bool UpsertCrontabItem(CrontabItem cron) + Task UpsertCrontabItem(CrontabItem cron) => throw new NotImplementedException(); - bool DeleteCrontabItem(string conversationId) + Task DeleteCrontabItem(string conversationId) => throw new NotImplementedException(); ValueTask> GetCrontabItems(CrontabItemFilter filter) => throw new NotImplementedException(); diff --git a/src/Infrastructure/BotSharp.Abstraction/Settings/ISettingService.cs b/src/Infrastructure/BotSharp.Abstraction/Settings/ISettingService.cs index 9ac3646e5..084f314d2 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Settings/ISettingService.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Settings/ISettingService.cs @@ -9,5 +9,5 @@ public interface ISettingService { T Bind(string path) where T : new(); - object GetDetail(string settingName, bool mask = false); + Task GetDetail(string settingName, bool mask = false); } diff --git a/src/Infrastructure/BotSharp.Abstraction/SideCar/IConversationSideCar.cs b/src/Infrastructure/BotSharp.Abstraction/SideCar/IConversationSideCar.cs index e561e6ae5..762860511 100644 --- a/src/Infrastructure/BotSharp.Abstraction/SideCar/IConversationSideCar.cs +++ b/src/Infrastructure/BotSharp.Abstraction/SideCar/IConversationSideCar.cs @@ -7,11 +7,11 @@ public interface IConversationSideCar string Provider { get; } bool IsEnabled { get; } - void AppendConversationDialogs(string conversationId, List messages); - List GetConversationDialogs(string conversationId); - void UpdateConversationBreakpoint(string conversationId, ConversationBreakpoint breakpoint); - ConversationBreakpoint? GetConversationBreakpoint(string conversationId); - void UpdateConversationStates(string conversationId, List states); + Task AppendConversationDialogs(string conversationId, List messages); + Task> GetConversationDialogs(string conversationId); + Task UpdateConversationBreakpoint(string conversationId, ConversationBreakpoint breakpoint); + Task GetConversationBreakpoint(string conversationId); + Task UpdateConversationStates(string conversationId, List states); Task SendMessage(string agentId, string text, PostbackMessageModel? postback = null, List? states = null, diff --git a/src/Infrastructure/BotSharp.Abstraction/Statistics/Services/IBotSharpStatsService.cs b/src/Infrastructure/BotSharp.Abstraction/Statistics/Services/IBotSharpStatsService.cs index f4230dcf4..5b80e8893 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Statistics/Services/IBotSharpStatsService.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Statistics/Services/IBotSharpStatsService.cs @@ -4,5 +4,5 @@ namespace BotSharp.Abstraction.Statistics.Services; public interface IBotSharpStatsService { - bool UpdateStats(string @event, BotSharpStatsDelta delta); + Task UpdateStats(string @event, BotSharpStatsDelta delta); } diff --git a/src/Infrastructure/BotSharp.Core.Crontab/Functions/ScheduleTaskFn.cs b/src/Infrastructure/BotSharp.Core.Crontab/Functions/ScheduleTaskFn.cs index b538cb3d6..271935713 100644 --- a/src/Infrastructure/BotSharp.Core.Crontab/Functions/ScheduleTaskFn.cs +++ b/src/Infrastructure/BotSharp.Core.Crontab/Functions/ScheduleTaskFn.cs @@ -30,7 +30,7 @@ public async Task Execute(RoleDialogModel message) if (string.IsNullOrEmpty(args.Cron)) { - var ret = db.DeleteCrontabItem(routing.ConversationId); + var ret = await db.DeleteCrontabItem(routing.ConversationId); message.Content = $"Task schedule canceled result: {ret}"; } else @@ -46,7 +46,7 @@ public async Task Execute(RoleDialogModel message) Tasks = args.Tasks, }; - var ret = db.UpsertCrontabItem(crontabItem); + var ret = await db.UpsertCrontabItem(crontabItem); message.Content = $"Task scheduled result: {ret}"; } diff --git a/src/Infrastructure/BotSharp.Core.Realtime/Hooks/RealtimeConversationHook.cs b/src/Infrastructure/BotSharp.Core.Realtime/Hooks/RealtimeConversationHook.cs index 07b8646cb..369f8b87e 100644 --- a/src/Infrastructure/BotSharp.Core.Realtime/Hooks/RealtimeConversationHook.cs +++ b/src/Infrastructure/BotSharp.Core.Realtime/Hooks/RealtimeConversationHook.cs @@ -30,6 +30,8 @@ public async Task OnFunctionExecuting(RoleDialogModel message, InvokeFunctionOpt var states = _services.GetRequiredService(); states.SaveStateByArgs(message.FunctionArgs?.JsonContent()); } + + await Task.CompletedTask; } public async Task OnFunctionExecuted(RoleDialogModel message, InvokeFunctionOptions? options = null) @@ -83,5 +85,6 @@ public async Task OnFunctionExecuted(RoleDialogModel message, InvokeFunctionOpti await hub.Completer.TriggerModelInference(instruction); } } + await Task.CompletedTask; } } diff --git a/src/Infrastructure/BotSharp.Core.Realtime/Services/RealtimeHub.cs b/src/Infrastructure/BotSharp.Core.Realtime/Services/RealtimeHub.cs index ab42a084d..d847e21b4 100644 --- a/src/Infrastructure/BotSharp.Core.Realtime/Services/RealtimeHub.cs +++ b/src/Infrastructure/BotSharp.Core.Realtime/Services/RealtimeHub.cs @@ -39,7 +39,7 @@ public async Task ConnectToModel( RealtimeOptions? options = null) { var convService = _services.GetRequiredService(); - convService.SetConversationId(_conn.ConversationId, initStates ?? []); + await convService.SetConversationId(_conn.ConversationId, initStates ?? []); var conversation = await convService.GetConversation(_conn.ConversationId); var routing = _services.GetRequiredService(); @@ -47,7 +47,7 @@ public async Task ConnectToModel( var agent = await agentService.GetAgent(_conn.CurrentAgentId); var storage = _services.GetRequiredService(); - var dialogs = convService.GetDialogHistory(); + var dialogs = await convService.GetDialogHistory(); routing.Context.SetDialogs(dialogs); routing.Context.SetMessageId(_conn.ConversationId, Guid.Empty.ToString()); @@ -129,7 +129,7 @@ await HookEmitter.Emit(_services, async hook => await hook.OnRouti { // append output audio transcript to conversation dialogs.Add(message); - storage.Append(_conn.ConversationId, message); + await storage.Append(_conn.ConversationId, message); var convHooks = _services.GetHooksOrderByPriority(_conn.CurrentAgentId); foreach (var hook in convHooks) @@ -150,7 +150,7 @@ await HookEmitter.Emit(_services, async hook => await hook.OnRouti { // append input audio transcript to conversation dialogs.Add(message); - storage.Append(_conn.ConversationId, message); + await storage.Append(_conn.ConversationId, message); routing.Context.SetMessageId(_conn.ConversationId, message.MessageId); var hooks = _services.GetHooksOrderByPriority(_conn.CurrentAgentId); diff --git a/src/Infrastructure/BotSharp.Core.Realtime/Services/WaveStreamChannel.cs b/src/Infrastructure/BotSharp.Core.Realtime/Services/WaveStreamChannel.cs index fba874ffe..e47d7bebe 100644 --- a/src/Infrastructure/BotSharp.Core.Realtime/Services/WaveStreamChannel.cs +++ b/src/Infrastructure/BotSharp.Core.Realtime/Services/WaveStreamChannel.cs @@ -19,7 +19,7 @@ public WaveStreamChannel(IServiceProvider services, ILogger l _logger = logger; } - public async Task ConnectAsync(string conversationId) + public Task ConnectAsync(string conversationId) { // Initialize the WaveInEvent _waveIn = new WaveInEvent @@ -48,6 +48,8 @@ public async Task ConnectAsync(string conversationId) }; _waveOut.Init(_bufferedWaveProvider); _waveOut.Play(); + + return Task.CompletedTask; } public async Task ReceiveAsync(ArraySegment buffer, CancellationToken cancellation) @@ -98,7 +100,7 @@ private void WaveIn_DataAvailable(object? sender, WaveInEventArgs e) _audioBufferQueue.Enqueue(e.Buffer); } - public async Task CloseAsync(StreamChannelStatus status, string description, CancellationToken cancellation) + public Task CloseAsync(StreamChannelStatus status, string description, CancellationToken cancellation) { // Stop recording and clean up _waveIn?.StopRecording(); @@ -109,5 +111,7 @@ public async Task CloseAsync(StreamChannelStatus status, string description, Can _waveOut?.Stop(); _waveOut?.Dispose(); _waveOut = null; + + return Task.CompletedTask; } } diff --git a/src/Infrastructure/BotSharp.Core.Rules/Engines/RuleEngine.cs b/src/Infrastructure/BotSharp.Core.Rules/Engines/RuleEngine.cs index 2d52ef6d2..6d7a20d80 100644 --- a/src/Infrastructure/BotSharp.Core.Rules/Engines/RuleEngine.cs +++ b/src/Infrastructure/BotSharp.Core.Rules/Engines/RuleEngine.cs @@ -80,14 +80,14 @@ public async Task> Triggered(IRuleTrigger trigger, string te allStates.AddRange(states); } - convService.SetConversationId(conv.Id, allStates); + await convService.SetConversationId(conv.Id, allStates); await convService.SendMessage(agent.Id, message, null, msg => Task.CompletedTask); - convService.SaveStates(); + await convService.SaveStates(); newConversationIds.Add(conv.Id); } diff --git a/src/Infrastructure/BotSharp.Core.SideCar/Services/BotSharpConversationSideCar.cs b/src/Infrastructure/BotSharp.Core.SideCar/Services/BotSharpConversationSideCar.cs index bb8bb5380..0d12ebc50 100644 --- a/src/Infrastructure/BotSharp.Core.SideCar/Services/BotSharpConversationSideCar.cs +++ b/src/Infrastructure/BotSharp.Core.SideCar/Services/BotSharpConversationSideCar.cs @@ -41,7 +41,7 @@ public BotSharpConversationSideCar( _logger = logger; } - public void AppendConversationDialogs(string conversationId, List messages) + public async Task AppendConversationDialogs(string conversationId, List messages) { if (!IsValid(conversationId)) { @@ -50,19 +50,23 @@ public void AppendConversationDialogs(string conversationId, List var top = _contextStack.Peek(); top.Dialogs.AddRange(messages); + + await Task.CompletedTask; } - public List GetConversationDialogs(string conversationId) + public async Task> GetConversationDialogs(string conversationId) { if (!IsValid(conversationId)) { return new List(); } + await Task.CompletedTask; + return _contextStack.Peek().Dialogs; } - public void UpdateConversationBreakpoint(string conversationId, ConversationBreakpoint breakpoint) + public async Task UpdateConversationBreakpoint(string conversationId, ConversationBreakpoint breakpoint) { if (!IsValid(conversationId)) { @@ -71,9 +75,11 @@ public void UpdateConversationBreakpoint(string conversationId, ConversationBrea var top = _contextStack.Peek().Breakpoints; top.Add(breakpoint); + + await Task.CompletedTask; } - public ConversationBreakpoint? GetConversationBreakpoint(string conversationId) + public async Task GetConversationBreakpoint(string conversationId) { if (!IsValid(conversationId)) { @@ -81,10 +87,12 @@ public void UpdateConversationBreakpoint(string conversationId, ConversationBrea } var top = _contextStack.Peek().Breakpoints; + + await Task.CompletedTask; return top.LastOrDefault(); } - public void UpdateConversationStates(string conversationId, List states) + public async Task UpdateConversationStates(string conversationId, List states) { if (!IsValid(conversationId)) { @@ -93,6 +101,8 @@ public void UpdateConversationStates(string conversationId, List var top = _contextStack.Peek(); top.State = new ConversationState(states); + + await Task.CompletedTask; } public async Task SendMessage( diff --git a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.Coding.cs b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.Coding.cs index 61749828a..0404fcbd2 100644 --- a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.Coding.cs +++ b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.Coding.cs @@ -10,8 +10,8 @@ public partial class AgentService public async Task> GetAgentCodeScripts(string agentId, AgentCodeScriptFilter? filter = null) { var db = _services.GetRequiredService(); - var scripts = db.GetAgentCodeScripts(agentId, filter); - return await Task.FromResult(scripts); + var scripts = await db.GetAgentCodeScripts(agentId, filter); + return scripts; } public async Task GetAgentCodeScript(string agentId, string scriptName, string scriptType = AgentCodeScriptType.Src) @@ -34,7 +34,7 @@ public async Task UpdateAgentCodeScripts(string agentId, List UpdateAgentCodeScripts(string agentId, List !codePaths.Contains(x.CodePath)).ToList(); } - var updateResult = db.UpdateAgentCodeScripts(agentId, codeScripts, options); + var updateResult = await db.UpdateAgentCodeScripts(agentId, codeScripts, options); if (!toDeleteScripts.IsNullOrEmpty()) { - db.DeleteAgentCodeScripts(agentId, toDeleteScripts); + await db.DeleteAgentCodeScripts(agentId, toDeleteScripts); } return updateResult; @@ -63,8 +63,8 @@ public async Task DeleteAgentCodeScripts(string agentId, List(); - var deleted = db.DeleteAgentCodeScripts(agentId, codeScripts); - return await Task.FromResult(deleted); + var deleted = await db.DeleteAgentCodeScripts(agentId, codeScripts); + return deleted; } public async Task GenerateCodeScript(string agentId, string text, CodeGenHandleOptions? options = null) @@ -104,7 +104,7 @@ public async Task GenerateCodeScript(string agentId, strin ScriptType = options?.ScriptType ?? AgentCodeScriptType.Src } }; - var saved = db.UpdateAgentCodeScripts(agentId, scripts, new() { IsUpsert = true }); + var saved = await db.UpdateAgentCodeScripts(agentId, scripts, new() { IsUpsert = true }); result.Success = saved; } diff --git a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.CreateAgent.cs b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.CreateAgent.cs index 3126b9f2b..334e385b9 100644 --- a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.CreateAgent.cs +++ b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.CreateAgent.cs @@ -11,7 +11,7 @@ public partial class AgentService public async Task CreateAgent(Agent agent) { var userIdentity = _services.GetRequiredService(); - var userAgents = _db.GetUserAgents(userIdentity.Id); + var userAgents = await _db.GetUserAgents(userIdentity.Id); var found = userAgents?.FirstOrDefault(x => x.Agent != null && x.Agent.Name.IsEqualTo(agent.Name)); if (found != null) { @@ -30,10 +30,10 @@ public async Task CreateAgent(Agent agent) var userService = _services.GetRequiredService(); var auth = await userService.GetUserAuthorizations(); - _db.BulkInsertAgents(new List { agentRecord }); + await _db.BulkInsertAgents(new List { agentRecord }); if (auth.IsAdmin || auth.Permissions.Contains(UserPermission.CreateAgent)) { - _db.BulkInsertUserAgents(new List + await _db.BulkInsertUserAgents(new List { new UserAgent { @@ -47,7 +47,7 @@ public async Task CreateAgent(Agent agent) } Utilities.ClearCache(); - return await Task.FromResult(agentRecord); + return agentRecord; } private (string, List) GetInstructionsFromFile(string fileDir) diff --git a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.DeleteAgent.cs b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.DeleteAgent.cs index 5237dbdc9..287ae72fb 100644 --- a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.DeleteAgent.cs +++ b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.DeleteAgent.cs @@ -16,7 +16,7 @@ public async Task DeleteAgent(string id, AgentDeleteOptions? options = nul return false; } - var deleted = _db.DeleteAgent(id, options); - return await Task.FromResult(deleted); + var deleted = await _db.DeleteAgent(id, options); + return deleted; } } diff --git a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.GetAgents.cs b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.GetAgents.cs index 6d9ef934c..875785738 100644 --- a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.GetAgents.cs +++ b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.GetAgents.cs @@ -8,12 +8,12 @@ public partial class AgentService [SharpCache(10)] public async Task> GetAgents(AgentFilter filter) { - var agents = _db.GetAgents(filter); + var agents = await _db.GetAgents(filter); var routeSetting = _services.GetRequiredService(); foreach (var agent in agents) { - agent.Plugin = GetPlugin(agent.Id); + agent.Plugin = await GetPlugin(agent.Id); } agents = agents.Where(x => x.Installed).ToList(); @@ -29,11 +29,11 @@ public async Task> GetAgents(AgentFilter filter) public async Task> GetAgentOptions(List? agentIdsOrNames, bool byName = false) { var agents = byName ? - _db.GetAgents(new AgentFilter + await _db.GetAgents(new AgentFilter { AgentNames = !agentIdsOrNames.IsNullOrEmpty() ? agentIdsOrNames : null }) : - _db.GetAgents(new AgentFilter + await _db.GetAgents(new AgentFilter { AgentIds = !agentIdsOrNames.IsNullOrEmpty() ? agentIdsOrNames : null }); @@ -70,7 +70,7 @@ public async Task GetAgent(string id) profile.LlmConfig.Model = agentSetting.LlmConfig.Model; } - profile.Plugin = GetPlugin(profile.Id); + profile.Plugin = await GetPlugin(profile.Id); AddDefaultInstruction(profile, profile.Instruction); diff --git a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.GetPlugin.cs b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.GetPlugin.cs index 747a5cf06..eae5f9140 100644 --- a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.GetPlugin.cs +++ b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.GetPlugin.cs @@ -5,10 +5,10 @@ namespace BotSharp.Core.Agents.Services; public partial class AgentService { - public PluginDef GetPlugin(string agentId) + public async Task GetPlugin(string agentId) { var loader = _services.GetRequiredService(); - var plugins = loader.GetPlugins(_services); + var plugins = await loader.GetPlugins(_services); return plugins.FirstOrDefault(x => x.AgentIds.Contains(agentId)) ?? new PluginDef { diff --git a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.RefreshAgents.cs b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.RefreshAgents.cs index 266f69935..0e5683f44 100644 --- a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.RefreshAgents.cs +++ b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.RefreshAgents.cs @@ -62,7 +62,7 @@ public async Task RefreshAgents(IEnumerable? agentIds = null) var tasks = GetTasksFromFile(dir); var codeScripts = GetCodeScriptsFromFile(dir); - var isAgentDeleted = _db.DeleteAgent(agent.Id, options: new() + var isAgentDeleted = await _db.DeleteAgent(agent.Id, options: new() { DeleteRoleAgents = false, DeleteUserAgents = false, @@ -72,9 +72,9 @@ public async Task RefreshAgents(IEnumerable? agentIds = null) if (isAgentDeleted) { await Task.Delay(100); - _db.BulkInsertAgents([agent]); - _db.BulkInsertAgentTasks(agent.Id, tasks); - _db.BulkInsertAgentCodeScripts(agent.Id, codeScripts); + await _db.BulkInsertAgents([agent]); + await _db.BulkInsertAgentTasks(agent.Id, tasks); + await _db.BulkInsertAgentCodeScripts(agent.Id, codeScripts); refreshedAgents.Add(agent.Name); _logger.LogInformation($"Agent {agent.Name} has been migrated."); diff --git a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.UpdateAgent.cs b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.UpdateAgent.cs index 924077fed..b89b5d787 100644 --- a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.UpdateAgent.cs +++ b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.UpdateAgent.cs @@ -48,7 +48,7 @@ public async Task UpdateAgent(Agent agent, AgentField updateField) record.LlmConfig = agent.LlmConfig; } - _db.UpdateAgent(record, updateField); + await _db.UpdateAgent(record, updateField); Utilities.ClearCache(); await Task.CompletedTask; @@ -79,7 +79,7 @@ public async Task PatchAgentTemplate(Agent agent) { if (template == null) continue; - var result = _db.PatchAgentTemplate(agent.Id, template); + var result = await _db.PatchAgentTemplate(agent.Id, template); if (result) { successTemplates.Add(template.Name); diff --git a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.cs b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.cs index d06cc3764..760e7a00a 100644 --- a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.cs +++ b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.cs @@ -49,7 +49,7 @@ public async Task> GetUserAgents(string userId) return []; } - var userAgents = _db.GetUserAgents(userId); + var userAgents = await _db.GetUserAgents(userId); return userAgents; } } diff --git a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.Migration.cs b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.Migration.cs index 4bb989bfa..6d802f8b1 100644 --- a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.Migration.cs +++ b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.Migration.cs @@ -21,7 +21,7 @@ public async Task MigrateLatestStates(int batchSize = 100, int errorLimit #endif var sw = Stopwatch.StartNew(); - var convIds = db.GetConversationsToMigrate(batchSize); + var convIds = await db.GetConversationsToMigrate(batchSize); while (!convIds.IsNullOrEmpty()) { @@ -38,7 +38,7 @@ public async Task MigrateLatestStates(int batchSize = 100, int errorLimit var convId = convIds.ElementAt(i); try { - var done = db.MigrateConvsersationLatestStates(convId); + var done = await db.MigrateConvsersationLatestStates(convId); info = $"Conversation {convId} latest states have been migrated ({i + 1}/{convIds.Count})!"; #if DEBUG Console.WriteLine($"\r\n{info}\r\n"); @@ -81,7 +81,7 @@ public async Task MigrateLatestStates(int batchSize = 100, int errorLimit #endif await Task.Delay(100); - convIds = db.GetConversationsToMigrate(batchSize); + convIds = await db.GetConversationsToMigrate(batchSize); } sw.Stop(); diff --git a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.SendMessage.cs b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.SendMessage.cs index 80873ca5c..eb4d2d74f 100644 --- a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.SendMessage.cs +++ b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.SendMessage.cs @@ -26,7 +26,7 @@ public async Task SendMessage(string agentId, } var conv = _services.GetRequiredService(); - var dialogs = conv.GetDialogHistory(); + var dialogs = await conv.GetDialogHistory(); var statistics = _services.GetRequiredService(); @@ -170,6 +170,6 @@ await HookEmitter.Emit(_services, async hook => await hook.On await onResponseReceived(response); // Add to dialog history - _storage.Append(_conversationId, response); + await _storage.Append(_conversationId, response); } } diff --git a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.Summary.cs b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.Summary.cs index 04c3c6f5d..c02187ecf 100644 --- a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.Summary.cs +++ b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.Summary.cs @@ -19,7 +19,7 @@ public async Task GetConversationSummary(ConversationSummaryModel model) { if (string.IsNullOrEmpty(conversationId)) continue; - var dialogs = _storage.GetDialogs(conversationId); + var dialogs = await _storage.GetDialogs(conversationId); if (dialogs.IsNullOrEmpty()) continue; diff --git a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.TruncateMessage.cs b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.TruncateMessage.cs index 05d04f253..34549c6c4 100644 --- a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.TruncateMessage.cs +++ b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.TruncateMessage.cs @@ -6,7 +6,7 @@ public async Task TruncateConversation(string conversationId, string messa { var db = _services.GetRequiredService(); var fileStorage = _services.GetRequiredService(); - var deleteMessageIds = db.TruncateConversation(conversationId, messageId, cleanLog: true); + var deleteMessageIds = await db.TruncateConversation(conversationId, messageId, cleanLog: true); fileStorage.DeleteMessageFiles(conversationId, deleteMessageIds, messageId, newMessageId); var hooks = _services.GetServices(); @@ -14,6 +14,6 @@ public async Task TruncateConversation(string conversationId, string messa { await hook.OnMessageDeleted(conversationId, messageId); } - return await Task.FromResult(true); + return true; } } diff --git a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.UpdateBreakpoint.cs b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.UpdateBreakpoint.cs index 25cf32d19..12be718f2 100644 --- a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.UpdateBreakpoint.cs +++ b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.UpdateBreakpoint.cs @@ -11,7 +11,7 @@ public async Task UpdateBreakpoint(bool resetStates = false, string? reason = nu var routingCtx = _services.GetRequiredService(); var messageId = routingCtx.MessageId; - db.UpdateConversationBreakpoint(_conversationId, new ConversationBreakpoint + await db.UpdateConversationBreakpoint(_conversationId, new ConversationBreakpoint { MessageId = messageId, Breakpoint = DateTime.UtcNow, diff --git a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.cs b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.cs index ffa4dcbfa..8eadeeab4 100644 --- a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.cs +++ b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.cs @@ -36,15 +36,15 @@ public async Task DeleteConversations(IEnumerable ids) { var db = _services.GetRequiredService(); var fileStorage = _services.GetRequiredService(); - var isDeleted = db.DeleteConversations(ids); + var isDeleted = await db.DeleteConversations(ids); fileStorage.DeleteConversationFiles(ids); - return await Task.FromResult(isDeleted); + return isDeleted; } public async Task UpdateConversationTitle(string id, string title) { var db = _services.GetRequiredService(); - db.UpdateConversationTitle(id, title); + await db.UpdateConversationTitle(id, title); var conversation = await db.GetConversation(id); return conversation; } @@ -52,7 +52,7 @@ public async Task UpdateConversationTitle(string id, string title) public async Task UpdateConversationTitleAlias(string id, string titleAlias) { var db = _services.GetRequiredService(); - db.UpdateConversationTitleAlias(id, titleAlias); + await db.UpdateConversationTitleAlias(id, titleAlias); var conversation = await db.GetConversation(id); return conversation; } @@ -60,13 +60,13 @@ public async Task UpdateConversationTitleAlias(string id, string t public async Task UpdateConversationTags(string conversationId, List toAddTags, List toDeleteTags) { var db = _services.GetRequiredService(); - return db.UpdateConversationTags(conversationId, toAddTags, toDeleteTags); + return await db.UpdateConversationTags(conversationId, toAddTags, toDeleteTags); } public async Task UpdateConversationMessage(string conversationId, UpdateMessageRequest request) { var db = _services.GetRequiredService(); - return db.UpdateConversationMessage(conversationId, request); + return await db.UpdateConversationMessage(conversationId, request); } public async Task GetConversation(string id, bool isLoadStates = false) @@ -91,13 +91,13 @@ public async Task> GetConversations(ConversationFilter public async Task> GetLastConversations() { var db = _services.GetRequiredService(); - return db.GetLastConversations(); + return await db.GetLastConversations(); } public async Task> GetIdleConversations(int batchSize, int messageLimit, int bufferHours, IEnumerable excludeAgentIds) { var db = _services.GetRequiredService(); - return db.GetIdleConversations(batchSize, messageLimit, bufferHours, excludeAgentIds ?? new List()); + return await db.GetIdleConversations(batchSize, messageLimit, bufferHours, excludeAgentIds ?? new List()); } public async Task NewConversation(Conversation sess) @@ -132,14 +132,14 @@ public Task CleanHistory(string agentId) throw new NotImplementedException(); } - public List GetDialogHistory(int lastCount = 100, bool fromBreakpoint = true, IEnumerable? includeMessageTypes = null) + public async Task> GetDialogHistory(int lastCount = 100, bool fromBreakpoint = true, IEnumerable? includeMessageTypes = null) { if (string.IsNullOrEmpty(_conversationId)) { throw new ArgumentNullException("ConversationId is null."); } - var dialogs = _storage.GetDialogs(_conversationId); + var dialogs = await _storage.GetDialogs(_conversationId); if (!includeMessageTypes.IsNullOrEmpty()) { @@ -153,7 +153,7 @@ public List GetDialogHistory(int lastCount = 100, bool fromBrea if (fromBreakpoint) { var db = _services.GetRequiredService(); - var breakpoint = db.GetConversationBreakpoint(_conversationId); + var breakpoint = await db.GetConversationBreakpoint(_conversationId); if (breakpoint != null) { @@ -165,16 +165,16 @@ public List GetDialogHistory(int lastCount = 100, bool fromBrea } } - var agentMsgCount = GetAgentMessageCount(); + var agentMsgCount = await GetAgentMessageCount(); var count = agentMsgCount.HasValue && agentMsgCount.Value > 0 ? agentMsgCount.Value : lastCount; return dialogs.TakeLast(count).ToList(); } - public void SetConversationId(string conversationId, List states, bool isReadOnly = false) + public async Task SetConversationId(string conversationId, List states, bool isReadOnly = false) { _conversationId = conversationId; - _state.Load(_conversationId, isReadOnly); + await _state.Load(_conversationId, isReadOnly); states.ForEach(x => _state.SetState(x.Key, x.Value, activeRounds: x.ActiveRounds, isNeedVersion: !x.Global, source: StateSource.External)); } @@ -209,20 +209,20 @@ public bool IsConversationMode() } - private int? GetAgentMessageCount() + private async Task GetAgentMessageCount() { var db = _services.GetRequiredService(); var routingCtx = _services.GetRequiredService(); if (string.IsNullOrEmpty(routingCtx.EntryAgentId)) return null; - var agent = db.GetAgent(routingCtx.EntryAgentId, basicsOnly: true); + var agent = await db.GetAgentAsync(routingCtx.EntryAgentId, basicsOnly: true); return agent?.MaxMessageCount; } - public void SaveStates() + public async Task SaveStates() { - _state.Save(); + await _state.Save(); } public async Task> GetConversationStateSearhKeys(ConversationStateKeysFilter filter) @@ -243,7 +243,7 @@ public async Task> GetConversationStateSearhKeys(ConversationStateK var (isAdmin, user) = await userService.IsAdminUser(_user.Id); filter.UserIds = !isAdmin && user?.Id != null ? [user.Id] : []; - keys = db.GetConversationStateSearchKeys(filter); + keys = await 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(); } diff --git a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationStateService.cs b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationStateService.cs index 50bfa5303..455a23a02 100644 --- a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationStateService.cs +++ b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationStateService.cs @@ -116,7 +116,7 @@ public IConversationStateService SetState(string name, T value, bool isNeedVe DataType = valueType, Source = source, Readonly = readOnly - }).Wait(); + }).ConfigureAwait(false).GetAwaiter().GetResult(); } } @@ -151,7 +151,7 @@ public IConversationStateService SetState(string name, T value, bool isNeedVe return this; } - public Dictionary Load(string conversationId, bool isReadOnly = false) + public async Task> Load(string conversationId, bool isReadOnly = false) { _conversationId = !isReadOnly ? conversationId : null; Reset(); @@ -162,14 +162,14 @@ public Dictionary Load(string conversationId, bool isReadOnly = return endNodes; } - _historyStates = _db.GetConversationStates(conversationId); + _historyStates = await _db.GetConversationStates(conversationId); if (_historyStates.IsNullOrEmpty()) { return endNodes; } var curMsgId = _routingContext.MessageId; - var dialogs = _db.GetConversationDialogs(conversationId); + var dialogs = await _db.GetConversationDialogs(conversationId); var userDialogs = dialogs.Where(x => x.MetaData?.Role == AgentRole.User) .GroupBy(x => x.MetaData?.MessageId) .Select(g => g.First()) @@ -224,13 +224,13 @@ public Dictionary Load(string conversationId, bool isReadOnly = var hooks = _services.GetHooks(_routingContext.GetCurrentAgentId()); foreach (var hook in hooks) { - hook.OnStateLoaded(_curStates).Wait(); + await hook.OnStateLoaded(_curStates); } return endNodes; } - public void Save() + public async Task Save() { if (_conversationId == null || _sidecar?.IsEnabled == true) { @@ -265,7 +265,7 @@ public void Save() } } - _db.UpdateConversationStates(_conversationId, states); + await _db.UpdateConversationStates(_conversationId, states); _logger.LogInformation($"Saved states of conversation {_conversationId}"); } @@ -303,7 +303,7 @@ public bool RemoveState(string name) DataType = leafNode.DataType, Source = leafNode.Source, Readonly = value.Readonly - }).Wait(); + }).ConfigureAwait(false).GetAwaiter().GetResult(); } return true; @@ -366,7 +366,7 @@ public string GetState(string name, string defaultValue = "") public void Dispose() { - Save(); + Save().ConfigureAwait(false).GetAwaiter().GetResult(); } public bool ContainsState(string name) diff --git a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationStorage.cs b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationStorage.cs index 3a8b3cdca..c0cac22c9 100644 --- a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationStorage.cs +++ b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationStorage.cs @@ -8,7 +8,7 @@ public class ConversationStorage : IConversationStorage { private readonly BotSharpOptions _options; private readonly IServiceProvider _services; - private static readonly object _lock = new object(); + private readonly SemaphoreSlim _semaphore = new SemaphoreSlim(1, 1); public ConversationStorage( BotSharpOptions options, @@ -18,15 +18,20 @@ public ConversationStorage( _options = options; } - public void Append(string conversationId, RoleDialogModel dialog) + public async Task Append(string conversationId, RoleDialogModel dialog) { - lock (_lock) + await _semaphore.WaitAsync(); + try { - Append(conversationId, [dialog]); + await Append(conversationId, [dialog]); + } + finally + { + _semaphore.Release(); } } - public void Append(string conversationId, IEnumerable dialogs) + public async Task Append(string conversationId, IEnumerable dialogs) { if (dialogs.IsNullOrEmpty()) return; @@ -42,13 +47,13 @@ public void Append(string conversationId, IEnumerable dialogs) } } - db.AppendConversationDialogs(conversationId, dialogElements); + await db.AppendConversationDialogs(conversationId, dialogElements); } - public List GetDialogs(string conversationId) + public async Task> GetDialogs(string conversationId) { var db = _services.GetRequiredService(); - var dialogs = db.GetConversationDialogs(conversationId); + var dialogs = await db.GetConversationDialogs(conversationId); var hooks = _services.GetServices(); var results = new List(); @@ -86,13 +91,13 @@ public List GetDialogs(string conversationId) foreach(var hook in hooks) { - hook.OnDialogRecordLoaded(record).Wait(); + await hook.OnDialogRecordLoaded(record); } } foreach (var hook in hooks) { - hook.OnDialogsLoaded(results).Wait(); + await hook.OnDialogsLoaded(results); } return results; diff --git a/src/Infrastructure/BotSharp.Core/Conversations/Services/TokenStatistics.cs b/src/Infrastructure/BotSharp.Core/Conversations/Services/TokenStatistics.cs index a92c4c8f3..627def6ca 100644 --- a/src/Infrastructure/BotSharp.Core/Conversations/Services/TokenStatistics.cs +++ b/src/Infrastructure/BotSharp.Core/Conversations/Services/TokenStatistics.cs @@ -31,7 +31,7 @@ public TokenStatistics(IServiceProvider services, ILogger logge _logger = logger; } - public void AddToken(TokenStatsModel stats, RoleDialogModel message) + public async Task AddToken(TokenStatsModel stats, RoleDialogModel message) { _model = stats.Model; _promptTokenCount += stats.TotalInputTokens; @@ -108,7 +108,7 @@ public void AddToken(TokenStatsModel stats, RoleDialogModel message) ImageGenerationTotalCostDelta = deltaImageGenerationCost } }; - globalStats.UpdateStats($"global-{metric}-{dim}-{agentId}", delta); + await globalStats.UpdateStats($"global-{metric}-{dim}-{agentId}", delta); } public void PrintStatistics() diff --git a/src/Infrastructure/BotSharp.Core/Evaluations/Services/EvaluatingService.Evaluate.cs b/src/Infrastructure/BotSharp.Core/Evaluations/Services/EvaluatingService.Evaluate.cs index 16e25b865..b3cf7f848 100644 --- a/src/Infrastructure/BotSharp.Core/Evaluations/Services/EvaluatingService.Evaluate.cs +++ b/src/Infrastructure/BotSharp.Core/Evaluations/Services/EvaluatingService.Evaluate.cs @@ -16,7 +16,7 @@ public async Task Evaluate(string conversationId, EvaluationRe } var storage = _services.GetRequiredService(); - var refDialogs = storage.GetDialogs(conversationId); + var refDialogs = await storage.GetDialogs(conversationId); if (refDialogs.IsNullOrEmpty()) { @@ -32,7 +32,7 @@ public async Task Evaluate(string conversationId, EvaluationRe return result; } - var initialStates = GetInitialStates(conversationId); + var initialStates = await GetInitialStates(conversationId); var generatedConvId = await SimulateConversation(initMessage, refDialogContents, request, initialStates); var metricResult = await EvaluateMetrics(generatedConvId, refDialogContents, request); @@ -118,7 +118,7 @@ private async Task SimulateConversation(string initMessage, IEnumerable< var agentService = _services.GetRequiredService(); var instructService = _services.GetRequiredService(); - var curDialogs = storage.GetDialogs(curConversationId); + var curDialogs = await storage.GetDialogs(curConversationId); var curDialogContents = GetConversationContent(curDialogs); var query = "Please follow the instruction for evaluation."; @@ -161,7 +161,7 @@ private IEnumerable GetConversationContent(IEnumerable return contents; } - private IEnumerable GetInitialStates(string conversationId) + private async Task> GetInitialStates(string conversationId) { if (string.IsNullOrWhiteSpace(conversationId)) { @@ -169,7 +169,7 @@ private IEnumerable GetInitialStates(string conversationId) } var db = _services.GetRequiredService(); - var states = db.GetConversationStates(conversationId); + var states = await db.GetConversationStates(conversationId); var initialStates = new List(); foreach (var state in states) diff --git a/src/Infrastructure/BotSharp.Core/Evaluations/Services/EvaluatingService.cs b/src/Infrastructure/BotSharp.Core/Evaluations/Services/EvaluatingService.cs index 95bcf040c..fbfb6ff24 100644 --- a/src/Infrastructure/BotSharp.Core/Evaluations/Services/EvaluatingService.cs +++ b/src/Infrastructure/BotSharp.Core/Evaluations/Services/EvaluatingService.cs @@ -98,7 +98,7 @@ private async Task SendMessage(string agentId, string conversat var inputMsg = new RoleDialogModel(AgentRole.User, text); routing.Context.SetMessageId(conversationId, inputMsg.MessageId); - conv.SetConversationId(conversationId, states ?? []); + await conv.SetConversationId(conversationId, states ?? []); RoleDialogModel response = default; diff --git a/src/Infrastructure/BotSharp.Core/Files/Services/Instruct/FileInstructService.SelectFile.cs b/src/Infrastructure/BotSharp.Core/Files/Services/Instruct/FileInstructService.SelectFile.cs index c20ea7f36..ef05c34c7 100644 --- a/src/Infrastructure/BotSharp.Core/Files/Services/Instruct/FileInstructService.SelectFile.cs +++ b/src/Infrastructure/BotSharp.Core/Files/Services/Instruct/FileInstructService.SelectFile.cs @@ -21,7 +21,7 @@ public async Task> SelectMessageFiles(string conve var dialogs = routingCtx.GetDialogs(); if (dialogs.IsNullOrEmpty()) { - dialogs = convService.GetDialogHistory(fromBreakpoint: options.FromBreakpoint); + dialogs = await convService.GetDialogHistory(fromBreakpoint: options.FromBreakpoint); } if (options.MessageLimit > 0) @@ -122,7 +122,7 @@ private async Task> SelectFiles(IEnumerable { diff --git a/src/Infrastructure/BotSharp.Core/Infrastructures/SettingService.cs b/src/Infrastructure/BotSharp.Core/Infrastructures/SettingService.cs index cbde5f445..785f71bae 100644 --- a/src/Infrastructure/BotSharp.Core/Infrastructures/SettingService.cs +++ b/src/Infrastructure/BotSharp.Core/Infrastructures/SettingService.cs @@ -25,10 +25,10 @@ public SettingService(IServiceProvider services, return settings; } - public object GetDetail(string settingName, bool mask = false) + public async Task GetDetail(string settingName, bool mask = false) { var pluginService = _services.GetRequiredService(); - var plugins = pluginService.GetPlugins(_services); + var plugins = await pluginService.GetPlugins(_services); var plugin = plugins.First(x => x.Module.Settings.Name == settingName); var instance = plugin.Module.GetNewSettingsInstance(); diff --git a/src/Infrastructure/BotSharp.Core/Instructs/Services/InstructService.Instruct.cs b/src/Infrastructure/BotSharp.Core/Instructs/Services/InstructService.Instruct.cs index df1751cbd..07ca2cf11 100644 --- a/src/Infrastructure/BotSharp.Core/Instructs/Services/InstructService.Instruct.cs +++ b/src/Infrastructure/BotSharp.Core/Instructs/Services/InstructService.Instruct.cs @@ -86,21 +86,21 @@ private string BuildInstruction(string instruction, Dictionary d private async Task GetAiResponse(string text, Agent agent, InstructOptions? options) { - var dialogs = BuildDialogs(text, options); + var dialogs = await BuildDialogs(text, options); var provider = options?.Provider ?? agent?.LlmConfig?.Provider ?? "openai"; var model = options?.Model ?? agent?.LlmConfig?.Model ?? "gpt-4o"; var completion = CompletionProvider.GetChatCompletion(_services, provider: provider, model: model); return await completion.GetChatCompletions(agent, dialogs); } - private List BuildDialogs(string text, InstructOptions? options) + private async Task> BuildDialogs(string text, InstructOptions? options) { var messages = new List(); if (!string.IsNullOrWhiteSpace(options?.ConversationId)) { var conv = _services.GetRequiredService(); - var dialogs = conv.GetDialogHistory(); + var dialogs = await conv.GetDialogHistory(); messages.AddRange(dialogs); } diff --git a/src/Infrastructure/BotSharp.Core/Loggers/Services/LoggerService.Conversation.cs b/src/Infrastructure/BotSharp.Core/Loggers/Services/LoggerService.Conversation.cs index c424d237a..69bdc737a 100644 --- a/src/Infrastructure/BotSharp.Core/Loggers/Services/LoggerService.Conversation.cs +++ b/src/Infrastructure/BotSharp.Core/Loggers/Services/LoggerService.Conversation.cs @@ -12,8 +12,8 @@ public async Task> GetConversationCont } var db = _services.GetRequiredService(); - var logs = db.GetConversationContentLogs(conversationId, filter); - return await Task.FromResult(logs); + var logs = await db.GetConversationContentLogs(conversationId, filter); + return logs; } @@ -25,7 +25,7 @@ public async Task> GetConversation } var db = _services.GetRequiredService(); - var logs = db.GetConversationStateLogs(conversationId, filter); - return await Task.FromResult(logs); + var logs = await db.GetConversationStateLogs(conversationId, filter); + return logs; } } diff --git a/src/Infrastructure/BotSharp.Core/MessageHub/Observers/ConversationObserver.cs b/src/Infrastructure/BotSharp.Core/MessageHub/Observers/ConversationObserver.cs index df953ee58..294031fa3 100644 --- a/src/Infrastructure/BotSharp.Core/MessageHub/Observers/ConversationObserver.cs +++ b/src/Infrastructure/BotSharp.Core/MessageHub/Observers/ConversationObserver.cs @@ -50,7 +50,7 @@ public override void OnNext(HubObserveData value) routingCtx.AddDialogs([value.Data]); if (value.SaveDataToDb) { - storage.Append(conv.ConversationId, value.Data); + storage.Append(conv.ConversationId, value.Data).ConfigureAwait(false).GetAwaiter().GetResult(); } } } diff --git a/src/Infrastructure/BotSharp.Core/Plugins/PluginLoader.cs b/src/Infrastructure/BotSharp.Core/Plugins/PluginLoader.cs index 583f8b69a..05dc9b81a 100644 --- a/src/Infrastructure/BotSharp.Core/Plugins/PluginLoader.cs +++ b/src/Infrastructure/BotSharp.Core/Plugins/PluginLoader.cs @@ -99,11 +99,11 @@ private void InitModule(string assembly, IBotSharpPlugin module) } } - public List GetPlugins(IServiceProvider services) + public async Task> GetPlugins(IServiceProvider services) { // Apply user configurations var db = services.GetRequiredService(); - var config = db.GetPluginConfig(); + var config = await db.GetPluginConfig(); foreach (var plugin in _plugins) { plugin.Enabled = plugin.IsCore || config.EnabledPlugins.Contains(plugin.Id); @@ -111,9 +111,9 @@ public List GetPlugins(IServiceProvider services) return _plugins; } - public PagedItems GetPagedPlugins(IServiceProvider services, PluginFilter filter) + public async Task> GetPagedPlugins(IServiceProvider services, PluginFilter filter) { - var plugins = GetPlugins(services); + var plugins = await GetPlugins(services); var pager = filter?.Pager ?? new Pagination(); // Apply filter @@ -135,14 +135,14 @@ public PagedItems GetPagedPlugins(IServiceProvider services, PluginFi }; } - public PluginDef UpdatePluginStatus(IServiceProvider services, string id, bool enable) + public async Task UpdatePluginStatus(IServiceProvider services, string id, bool enable) { var plugin = _plugins.First(x => x.Id == id); plugin.Enabled = enable; // save to config var db = services.GetRequiredService(); - var config = db.GetPluginConfig(); + var config = await db.GetPluginConfig(); if (enable) { var dependentPlugins = new HashSet(); @@ -152,27 +152,27 @@ public PluginDef UpdatePluginStatus(IServiceProvider services, string id, bool e if (!missingPlugins.IsNullOrEmpty()) { config.EnabledPlugins.AddRange(missingPlugins); - db.SavePluginConfig(config); + await db.SavePluginConfig(config); } // enable agents var agentService = services.GetRequiredService(); foreach (var agentId in dependentAgentIds) { - var agent = agentService.LoadAgent(agentId).ConfigureAwait(false).GetAwaiter().GetResult(); + var agent = await agentService.LoadAgent(agentId); if (agent == null) { continue; } agent.Disabled = false; - agentService.UpdateAgent(agent, AgentField.Disabled); + await agentService.UpdateAgent(agent, AgentField.Disabled); if (agent.InheritAgentId != null) { - agent = agentService.LoadAgent(agent.InheritAgentId).ConfigureAwait(false).GetAwaiter().GetResult(); + agent = await agentService.LoadAgent(agent.InheritAgentId); agent.Disabled = false; - agentService.UpdateAgent(agent, AgentField.Disabled); + await agentService.UpdateAgent(agent, AgentField.Disabled); } } } @@ -181,21 +181,21 @@ public PluginDef UpdatePluginStatus(IServiceProvider services, string id, bool e if (config.EnabledPlugins.Exists(x => x == id)) { config.EnabledPlugins.Remove(id); - db.SavePluginConfig(config); + await db.SavePluginConfig(config); } // disable agents var agentService = services.GetRequiredService(); foreach (var agentId in plugin.AgentIds) { - var agent = agentService.LoadAgent(agentId).ConfigureAwait(false).GetAwaiter().GetResult(); + var agent = await agentService.LoadAgent(agentId); if (agent == null) { continue; } agent.Disabled = true; - agentService.UpdateAgent(agent, AgentField.Disabled); + await agentService.UpdateAgent(agent, AgentField.Disabled); } } return plugin; diff --git a/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.Agent.cs b/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.Agent.cs index c6b1e3a45..c71805458 100644 --- a/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.Agent.cs +++ b/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.Agent.cs @@ -7,80 +7,80 @@ namespace BotSharp.Core.Repository { public partial class FileRepository { - public void UpdateAgent(Agent agent, AgentField field) + public async Task UpdateAgent(Agent agent, AgentField field) { if (agent == null || string.IsNullOrEmpty(agent.Id)) return; switch (field) { case AgentField.Name: - UpdateAgentName(agent.Id, agent.Name); + await UpdateAgentName(agent.Id, agent.Name); break; case AgentField.Description: - UpdateAgentDescription(agent.Id, agent.Description); + await UpdateAgentDescription(agent.Id, agent.Description); break; case AgentField.IsPublic: - UpdateAgentIsPublic(agent.Id, agent.IsPublic); + await UpdateAgentIsPublic(agent.Id, agent.IsPublic); break; case AgentField.Disabled: - UpdateAgentDisabled(agent.Id, agent.Disabled); + await UpdateAgentDisabled(agent.Id, agent.Disabled); break; case AgentField.Type: - UpdateAgentType(agent.Id, agent.Type); + await UpdateAgentType(agent.Id, agent.Type); break; case AgentField.RoutingMode: - UpdateAgentRoutingMode(agent.Id, agent.Mode); + await UpdateAgentRoutingMode(agent.Id, agent.Mode); break; case AgentField.FuncVisMode: - UpdateAgentFuncVisMode(agent.Id, agent.FuncVisMode); + await UpdateAgentFuncVisMode(agent.Id, agent.FuncVisMode); break; case AgentField.InheritAgentId: - UpdateAgentInheritAgentId(agent.Id, agent.InheritAgentId); + await UpdateAgentInheritAgentId(agent.Id, agent.InheritAgentId); break; case AgentField.Profile: - UpdateAgentProfiles(agent.Id, agent.Profiles); + await UpdateAgentProfiles(agent.Id, agent.Profiles); break; case AgentField.Label: - UpdateAgentLabels(agent.Id, agent.Labels); + await UpdateAgentLabels(agent.Id, agent.Labels); break; case AgentField.RoutingRule: - UpdateAgentRoutingRules(agent.Id, agent.RoutingRules); + await UpdateAgentRoutingRules(agent.Id, agent.RoutingRules); break; case AgentField.Instruction: - UpdateAgentInstructions(agent.Id, agent.Instruction, agent.ChannelInstructions); + await UpdateAgentInstructions(agent.Id, agent.Instruction, agent.ChannelInstructions); break; case AgentField.Function: - UpdateAgentFunctions(agent.Id, agent.Functions); + await UpdateAgentFunctions(agent.Id, agent.Functions); break; case AgentField.Template: - UpdateAgentTemplates(agent.Id, agent.Templates); + await UpdateAgentTemplates(agent.Id, agent.Templates); break; case AgentField.Response: - UpdateAgentResponses(agent.Id, agent.Responses); + await UpdateAgentResponses(agent.Id, agent.Responses); break; case AgentField.Sample: - UpdateAgentSamples(agent.Id, agent.Samples); + await UpdateAgentSamples(agent.Id, agent.Samples); break; case AgentField.LlmConfig: - UpdateAgentLlmConfig(agent.Id, agent.LlmConfig); + await UpdateAgentLlmConfig(agent.Id, agent.LlmConfig); break; case AgentField.Utility: - UpdateAgentUtilities(agent.Id, agent.MergeUtility, agent.Utilities); + await UpdateAgentUtilities(agent.Id, agent.MergeUtility, agent.Utilities); break; case AgentField.McpTool: - UpdateAgentMcpTools(agent.Id, agent.McpTools); + await UpdateAgentMcpTools(agent.Id, agent.McpTools); break; case AgentField.KnowledgeBase: - UpdateAgentKnowledgeBases(agent.Id, agent.KnowledgeBases); + await UpdateAgentKnowledgeBases(agent.Id, agent.KnowledgeBases); break; case AgentField.Rule: - UpdateAgentRules(agent.Id, agent.Rules); + await UpdateAgentRules(agent.Id, agent.Rules); break; case AgentField.MaxMessageCount: - UpdateAgentMaxMessageCount(agent.Id, agent.MaxMessageCount); + await UpdateAgentMaxMessageCount(agent.Id, agent.MaxMessageCount); break; case AgentField.All: - UpdateAgentAllFields(agent); + await UpdateAgentAllFields(agent); break; default: break; @@ -90,7 +90,7 @@ public void UpdateAgent(Agent agent, AgentField field) } #region Update Agent Fields - private void UpdateAgentName(string agentId, string name) + private async Task UpdateAgentName(string agentId, string name) { if (string.IsNullOrWhiteSpace(name)) { @@ -106,10 +106,10 @@ private void UpdateAgentName(string agentId, string name) agent.Name = name; agent.UpdatedDateTime = DateTime.UtcNow; var json = JsonSerializer.Serialize(agent, _options); - File.WriteAllText(agentFile, json); + await File.WriteAllTextAsync(agentFile, json); } - private void UpdateAgentDescription(string agentId, string description) + private async Task UpdateAgentDescription(string agentId, string description) { if (string.IsNullOrWhiteSpace(description)) { @@ -125,10 +125,10 @@ private void UpdateAgentDescription(string agentId, string description) agent.Description = description; agent.UpdatedDateTime = DateTime.UtcNow; var json = JsonSerializer.Serialize(agent, _options); - File.WriteAllText(agentFile, json); + await File.WriteAllTextAsync(agentFile, json); } - private void UpdateAgentIsPublic(string agentId, bool isPublic) + private async Task UpdateAgentIsPublic(string agentId, bool isPublic) { var (agent, agentFile) = GetAgentFromFile(agentId); if (agent == null) @@ -139,10 +139,10 @@ private void UpdateAgentIsPublic(string agentId, bool isPublic) agent.IsPublic = isPublic; agent.UpdatedDateTime = DateTime.UtcNow; var json = JsonSerializer.Serialize(agent, _options); - File.WriteAllText(agentFile, json); + await File.WriteAllTextAsync(agentFile, json); } - private void UpdateAgentDisabled(string agentId, bool disabled) + private async Task UpdateAgentDisabled(string agentId, bool disabled) { var (agent, agentFile) = GetAgentFromFile(agentId); if (agent == null) @@ -153,10 +153,10 @@ private void UpdateAgentDisabled(string agentId, bool disabled) agent.Disabled = disabled; agent.UpdatedDateTime = DateTime.UtcNow; var json = JsonSerializer.Serialize(agent, _options); - File.WriteAllText(agentFile, json); + await File.WriteAllTextAsync(agentFile, json); } - private void UpdateAgentType(string agentId, string type) + private async Task UpdateAgentType(string agentId, string type) { var (agent, agentFile) = GetAgentFromFile(agentId); if (agent == null) @@ -167,10 +167,10 @@ private void UpdateAgentType(string agentId, string type) agent.Type = type; agent.UpdatedDateTime = DateTime.UtcNow; var json = JsonSerializer.Serialize(agent, _options); - File.WriteAllText(agentFile, json); + await File.WriteAllTextAsync(agentFile, json); } - private void UpdateAgentRoutingMode(string agentId, string? mode) + private async Task UpdateAgentRoutingMode(string agentId, string? mode) { var (agent, agentFile) = GetAgentFromFile(agentId); if (agent == null) @@ -181,10 +181,10 @@ private void UpdateAgentRoutingMode(string agentId, string? mode) agent.Mode = mode; agent.UpdatedDateTime = DateTime.UtcNow; var json = JsonSerializer.Serialize(agent, _options); - File.WriteAllText(agentFile, json); + await File.WriteAllTextAsync(agentFile, json); } - private void UpdateAgentFuncVisMode(string agentId, string? visMode) + private async Task UpdateAgentFuncVisMode(string agentId, string? visMode) { var (agent, agentFile) = GetAgentFromFile(agentId); if (agent == null) @@ -195,10 +195,10 @@ private void UpdateAgentFuncVisMode(string agentId, string? visMode) agent.FuncVisMode = visMode; agent.UpdatedDateTime = DateTime.UtcNow; var json = JsonSerializer.Serialize(agent, _options); - File.WriteAllText(agentFile, json); + await File.WriteAllTextAsync(agentFile, json); } - private void UpdateAgentInheritAgentId(string agentId, string? inheritAgentId) + private async Task UpdateAgentInheritAgentId(string agentId, string? inheritAgentId) { var (agent, agentFile) = GetAgentFromFile(agentId); if (agent == null) @@ -209,10 +209,10 @@ private void UpdateAgentInheritAgentId(string agentId, string? inheritAgentId) agent.InheritAgentId = inheritAgentId; agent.UpdatedDateTime = DateTime.UtcNow; var json = JsonSerializer.Serialize(agent, _options); - File.WriteAllText(agentFile, json); + await File.WriteAllTextAsync(agentFile, json); } - private void UpdateAgentProfiles(string agentId, List profiles) + private async Task UpdateAgentProfiles(string agentId, List profiles) { if (profiles == null) { @@ -228,10 +228,10 @@ private void UpdateAgentProfiles(string agentId, List profiles) agent.Profiles = profiles; agent.UpdatedDateTime = DateTime.UtcNow; var json = JsonSerializer.Serialize(agent, _options); - File.WriteAllText(agentFile, json); + await File.WriteAllTextAsync(agentFile, json); } - public bool UpdateAgentLabels(string agentId, List labels) + public async Task UpdateAgentLabels(string agentId, List labels) { if (labels == null) { @@ -247,11 +247,11 @@ public bool UpdateAgentLabels(string agentId, List labels) agent.Labels = labels; agent.UpdatedDateTime = DateTime.UtcNow; var json = JsonSerializer.Serialize(agent, _options); - File.WriteAllText(agentFile, json); + await File.WriteAllTextAsync(agentFile, json); return true; } - private void UpdateAgentUtilities(string agentId, bool mergeUtility, List utilities) + private async Task UpdateAgentUtilities(string agentId, bool mergeUtility, List utilities) { if (utilities == null) { @@ -268,10 +268,10 @@ private void UpdateAgentUtilities(string agentId, bool mergeUtility, List mcptools) + private async Task UpdateAgentMcpTools(string agentId, List mcptools) { if (mcptools == null) { @@ -287,10 +287,10 @@ private void UpdateAgentMcpTools(string agentId, List mcptools) agent.McpTools = mcptools; agent.UpdatedDateTime = DateTime.UtcNow; var json = JsonSerializer.Serialize(agent, _options); - File.WriteAllText(agentFile, json); + await File.WriteAllTextAsync(agentFile, json); } - private void UpdateAgentKnowledgeBases(string agentId, List knowledgeBases) + private async Task UpdateAgentKnowledgeBases(string agentId, List knowledgeBases) { if (knowledgeBases == null) { @@ -306,10 +306,10 @@ private void UpdateAgentKnowledgeBases(string agentId, List agent.KnowledgeBases = knowledgeBases; agent.UpdatedDateTime = DateTime.UtcNow; var json = JsonSerializer.Serialize(agent, _options); - File.WriteAllText(agentFile, json); + await File.WriteAllTextAsync(agentFile, json); } - private void UpdateAgentRules(string agentId, List rules) + private async Task UpdateAgentRules(string agentId, List rules) { if (rules == null) { @@ -325,10 +325,10 @@ private void UpdateAgentRules(string agentId, List rules) agent.Rules = rules; agent.UpdatedDateTime = DateTime.UtcNow; var json = JsonSerializer.Serialize(agent, _options); - File.WriteAllText(agentFile, json); + await File.WriteAllTextAsync(agentFile, json); } - private void UpdateAgentRoutingRules(string agentId, List rules) + private async Task UpdateAgentRoutingRules(string agentId, List rules) { if (rules == null) { @@ -344,10 +344,10 @@ private void UpdateAgentRoutingRules(string agentId, List rules) agent.RoutingRules = rules; agent.UpdatedDateTime = DateTime.UtcNow; var json = JsonSerializer.Serialize(agent, _options); - File.WriteAllText(agentFile, json); + await File.WriteAllTextAsync(agentFile, json); } - private void UpdateAgentInstructions(string agentId, string instruction, List channelInstructions) + private async Task UpdateAgentInstructions(string agentId, string instruction, List channelInstructions) { if (string.IsNullOrWhiteSpace(instruction)) { @@ -365,8 +365,8 @@ private void UpdateAgentInstructions(string agentId, string instruction, List inputFunctions) + private async Task UpdateAgentFunctions(string agentId, List inputFunctions) { if (inputFunctions == null) { @@ -407,12 +407,12 @@ private void UpdateAgentFunctions(string agentId, List inputFunctio var text = JsonSerializer.Serialize(func, _options); var file = Path.Combine(functionDir, $"{func.Name}.json"); - File.WriteAllText(file, text); - Thread.Sleep(50); + await File.WriteAllTextAsync(file, text); + await Task.Delay(50); } } - private void UpdateAgentTemplates(string agentId, List templates) + private async Task UpdateAgentTemplates(string agentId, List templates) { if (templates == null) { @@ -431,11 +431,11 @@ private void UpdateAgentTemplates(string agentId, List templates) foreach (var template in templates) { var file = Path.Combine(templateDir, $"{template.Name}.{_agentSettings.TemplateFormat}"); - File.WriteAllText(file, template.Content); + await File.WriteAllTextAsync(file, template.Content); } } - private void UpdateAgentResponses(string agentId, List responses) + private async Task UpdateAgentResponses(string agentId, List responses) { if (responses == null) { @@ -456,11 +456,11 @@ private void UpdateAgentResponses(string agentId, List responses) var response = responses[i]; var fileName = $"{response.Prefix}.{response.Intent}.{i}.{_agentSettings.TemplateFormat}"; var file = Path.Combine(responseDir, fileName); - File.WriteAllText(file, response.Content); + await File.WriteAllTextAsync(file, response.Content); } } - private void UpdateAgentSamples(string agentId, List samples) + private async Task UpdateAgentSamples(string agentId, List samples) { if (samples == null) { @@ -474,10 +474,10 @@ private void UpdateAgentSamples(string agentId, List samples) } var file = Path.Combine(_dbSettings.FileRepository, _agentSettings.DataDir, agentId, AGENT_SAMPLES_FILE); - File.WriteAllLines(file, samples); + await File.WriteAllLinesAsync(file, samples); } - private void UpdateAgentLlmConfig(string agentId, AgentLlmConfig? config) + private async Task UpdateAgentLlmConfig(string agentId, AgentLlmConfig? config) { var (agent, agentFile) = GetAgentFromFile(agentId); if (agent == null) @@ -488,10 +488,10 @@ private void UpdateAgentLlmConfig(string agentId, AgentLlmConfig? config) agent.LlmConfig = config; agent.UpdatedDateTime = DateTime.UtcNow; var json = JsonSerializer.Serialize(agent, _options); - File.WriteAllText(agentFile, json); + await File.WriteAllTextAsync(agentFile, json); } - private void UpdateAgentMaxMessageCount(string agentId, int? maxMessageCount) + private async Task UpdateAgentMaxMessageCount(string agentId, int? maxMessageCount) { var (agent, agentFile) = GetAgentFromFile(agentId); if (agent == null) @@ -502,10 +502,10 @@ private void UpdateAgentMaxMessageCount(string agentId, int? maxMessageCount) agent.MaxMessageCount = maxMessageCount; agent.UpdatedDateTime = DateTime.UtcNow; var json = JsonSerializer.Serialize(agent, _options); - File.WriteAllText(agentFile, json); + await File.WriteAllTextAsync(agentFile, json); } - private void UpdateAgentAllFields(Agent inputAgent) + private async Task UpdateAgentAllFields(Agent inputAgent) { var (agent, agentFile) = GetAgentFromFile(inputAgent.Id); if (agent == null) @@ -532,17 +532,17 @@ private void UpdateAgentAllFields(Agent inputAgent) agent.MaxMessageCount = inputAgent.MaxMessageCount; agent.UpdatedDateTime = DateTime.UtcNow; var json = JsonSerializer.Serialize(agent, _options); - File.WriteAllText(agentFile, json); + await File.WriteAllTextAsync(agentFile, json); - UpdateAgentInstructions(inputAgent.Id, inputAgent.Instruction, inputAgent.ChannelInstructions); - UpdateAgentResponses(inputAgent.Id, inputAgent.Responses); - UpdateAgentTemplates(inputAgent.Id, inputAgent.Templates); - UpdateAgentFunctions(inputAgent.Id, inputAgent.Functions); - UpdateAgentSamples(inputAgent.Id, inputAgent.Samples); + await UpdateAgentInstructions(inputAgent.Id, inputAgent.Instruction, inputAgent.ChannelInstructions); + await UpdateAgentResponses(inputAgent.Id, inputAgent.Responses); + await UpdateAgentTemplates(inputAgent.Id, inputAgent.Templates); + await UpdateAgentFunctions(inputAgent.Id, inputAgent.Functions); + await UpdateAgentSamples(inputAgent.Id, inputAgent.Samples); } #endregion - public List GetAgentResponses(string agentId, string prefix, string intent) + public async Task> GetAgentResponses(string agentId, string prefix, string intent) { var responses = new List(); var dir = Path.Combine(_dbSettings.FileRepository, _agentSettings.DataDir, agentId, AGENT_RESPONSES_FOLDER); @@ -557,7 +557,7 @@ public List GetAgentResponses(string agentId, string prefix, string inte .Last() .StartsWith(prefix + "." + intent)) { - responses.Add(File.ReadAllText(file)); + responses.Add(await File.ReadAllTextAsync(file)); } } @@ -604,7 +604,13 @@ public List GetAgentResponses(string agentId, string prefix, string inte return null; } - public List GetAgents(AgentFilter filter) + public Task GetAgentAsync(string agentId, bool basicsOnly = false) + { + var agent = GetAgent(agentId, basicsOnly); + return Task.FromResult(agent); + } + + public Task> GetAgents(AgentFilter filter) { if (filter == null) { @@ -648,10 +654,10 @@ public List GetAgents(AgentFilter filter) query = query.Where(x => x.IsPublic == filter.IsPublic); } - return query.ToList(); + return Task.FromResult(query.ToList()); } - public List GetUserAgents(string userId) + public async Task> GetUserAgents(string userId) { var found = (from ua in UserAgents join u in Users on ua.UserId equals u.Id @@ -664,7 +670,7 @@ join u in Users on ua.UserId equals u.Id } var agentIds = found.Select(x => x.AgentId).Distinct().ToList(); - var agents = GetAgents(new AgentFilter { AgentIds = agentIds }); + var agents = await GetAgents(new AgentFilter { AgentIds = agentIds }); foreach (var item in found) { var agent = agents.FirstOrDefault(x => x.Id == item.AgentId); @@ -680,7 +686,7 @@ join u in Users on ua.UserId equals u.Id } - public string GetAgentTemplate(string agentId, string templateName) + public async Task GetAgentTemplate(string agentId, string templateName) { if (string.IsNullOrWhiteSpace(agentId) || string.IsNullOrWhiteSpace(templateName)) @@ -702,14 +708,14 @@ public string GetAgentTemplate(string agentId, string templateName) var extension = fileName.Substring(splitIdx + 1); if (name.IsEqualTo(templateName) && extension.IsEqualTo(_agentSettings.TemplateFormat)) { - return File.ReadAllText(file); + return await File.ReadAllTextAsync(file); } } return string.Empty; } - public bool PatchAgentTemplate(string agentId, AgentTemplate template) + public async Task PatchAgentTemplate(string agentId, AgentTemplate template) { if (string.IsNullOrEmpty(agentId) || template == null) { @@ -734,11 +740,11 @@ public bool PatchAgentTemplate(string agentId, AgentTemplate template) return false; } - File.WriteAllText(foundTemplate, template.Content); + await File.WriteAllTextAsync(foundTemplate, template.Content); return true; } - public bool AppendAgentLabels(string agentId, List labels) + public async Task AppendAgentLabels(string agentId, List labels) { if (labels.IsNullOrEmpty()) { @@ -756,11 +762,11 @@ public bool AppendAgentLabels(string agentId, List labels) agent.Labels = curLabels; agent.UpdatedDateTime = DateTime.UtcNow; var json = JsonSerializer.Serialize(agent, _options); - File.WriteAllText(agentFile, json); + await File.WriteAllTextAsync(agentFile, json); return true; } - public void BulkInsertAgents(List agents) + public async Task BulkInsertAgents(List agents) { if (agents.IsNullOrEmpty()) { @@ -777,25 +783,25 @@ public void BulkInsertAgents(List agents) } Directory.CreateDirectory(dir); - Thread.Sleep(50); + await Task.Delay(50); var agentFile = Path.Combine(dir, AGENT_FILE); var json = JsonSerializer.Serialize(agent, _options); - File.WriteAllText(agentFile, json); + await File.WriteAllTextAsync(agentFile, json); if (!string.IsNullOrWhiteSpace(agent.Instruction)) { var instDir = Path.Combine(dir, AGENT_INSTRUCTIONS_FOLDER); Directory.CreateDirectory(instDir); var instFile = Path.Combine(instDir, $"{AGENT_INSTRUCTION_FILE}.{_agentSettings.TemplateFormat}"); - File.WriteAllText(instFile, agent.Instruction); + await File.WriteAllTextAsync(instFile, agent.Instruction); } } ResetInnerAgents(); } - public void BulkInsertUserAgents(List userAgents) + public async Task BulkInsertUserAgents(List userAgents) { if (userAgents.IsNullOrEmpty()) { @@ -825,24 +831,24 @@ public void BulkInsertUserAgents(List userAgents) var list = new List(); if (File.Exists(userAgentFile)) { - var str = File.ReadAllText(userAgentFile); + var str = await File.ReadAllTextAsync(userAgentFile); list = JsonSerializer.Deserialize>(str, _options); } list.AddRange(filtered); - File.WriteAllText(userAgentFile, JsonSerializer.Serialize(list, _options)); - Thread.Sleep(50); + await File.WriteAllTextAsync(userAgentFile, JsonSerializer.Serialize(list, _options)); + await Task.Delay(50); } ResetInnerAgents(); } - public bool DeleteAgents() + public Task DeleteAgents() { - return false; + return Task.FromResult(false); } - public bool DeleteAgent(string agentId, AgentDeleteOptions? options = null) + public async Task DeleteAgent(string agentId, AgentDeleteOptions? options = null) { if (string.IsNullOrEmpty(agentId)) { @@ -871,7 +877,7 @@ public bool DeleteAgent(string agentId, AgentDeleteOptions? options = null) continue; } - var text = File.ReadAllText(userAgentFile); + var text = await File.ReadAllTextAsync(userAgentFile); var userAgents = JsonSerializer.Deserialize>(text, _options); if (userAgents.IsNullOrEmpty()) { @@ -879,7 +885,7 @@ public bool DeleteAgent(string agentId, AgentDeleteOptions? options = null) } userAgents = userAgents?.Where(x => x.AgentId != agentId)?.ToList() ?? []; - File.WriteAllText(userAgentFile, JsonSerializer.Serialize(userAgents, _options)); + await File.WriteAllTextAsync(userAgentFile, JsonSerializer.Serialize(userAgents, _options)); } } } @@ -898,7 +904,7 @@ public bool DeleteAgent(string agentId, AgentDeleteOptions? options = null) continue; } - var text = File.ReadAllText(roleAgentFile); + var text = await File.ReadAllTextAsync(roleAgentFile); var roleAgents = JsonSerializer.Deserialize>(text, _options); if (roleAgents.IsNullOrEmpty()) { @@ -906,7 +912,7 @@ public bool DeleteAgent(string agentId, AgentDeleteOptions? options = null) } roleAgents = roleAgents?.Where(x => x.AgentId != agentId)?.ToList() ?? []; - File.WriteAllText(roleAgentFile, JsonSerializer.Serialize(roleAgents, _options)); + await File.WriteAllTextAsync(roleAgentFile, JsonSerializer.Serialize(roleAgents, _options)); } } } diff --git a/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.AgentCodeScript.cs b/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.AgentCodeScript.cs index 82f40e761..bd2333bb0 100644 --- a/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.AgentCodeScript.cs +++ b/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.AgentCodeScript.cs @@ -5,7 +5,7 @@ namespace BotSharp.Core.Repository; public partial class FileRepository { #region Code script - public List GetAgentCodeScripts(string agentId, AgentCodeScriptFilter? filter = null) + public async Task> GetAgentCodeScripts(string agentId, AgentCodeScriptFilter? filter = null) { if (string.IsNullOrWhiteSpace(agentId)) { @@ -42,7 +42,7 @@ public List GetAgentCodeScripts(string agentId, AgentCodeScript AgentId = agentId, Name = fileName, ScriptType = scriptType, - Content = File.ReadAllText(file) + Content = await File.ReadAllTextAsync(file) }); } } @@ -82,7 +82,7 @@ public List GetAgentCodeScripts(string agentId, AgentCodeScript }; } - public bool UpdateAgentCodeScripts(string agentId, List scripts, AgentCodeScriptDbUpdateOptions? options = null) + public async Task UpdateAgentCodeScripts(string agentId, List scripts, AgentCodeScriptDbUpdateOptions? options = null) { if (string.IsNullOrWhiteSpace(agentId) || scripts.IsNullOrEmpty()) { @@ -113,19 +113,19 @@ public bool UpdateAgentCodeScripts(string agentId, List scripts var file = Path.Combine(dir, script.Name); if (options?.IsUpsert == true || File.Exists(file)) { - File.WriteAllText(file, script.Content); + await File.WriteAllTextAsync(file, script.Content); } } return true; } - public bool BulkInsertAgentCodeScripts(string agentId, List scripts) + public async Task BulkInsertAgentCodeScripts(string agentId, List scripts) { - return UpdateAgentCodeScripts(agentId, scripts, options: new() { IsUpsert = true }); + return await UpdateAgentCodeScripts(agentId, scripts, options: new() { IsUpsert = true }); } - public bool DeleteAgentCodeScripts(string agentId, List? scripts = null) + public async Task DeleteAgentCodeScripts(string agentId, List? scripts = null) { if (string.IsNullOrWhiteSpace(agentId)) { @@ -161,7 +161,7 @@ public bool DeleteAgentCodeScripts(string agentId, List? script } } - return true; + return await Task.FromResult(true); } #endregion diff --git a/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.AgentTask.cs b/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.AgentTask.cs index b847b0be6..91aaf95e7 100644 --- a/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.AgentTask.cs +++ b/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.AgentTask.cs @@ -50,7 +50,7 @@ public async ValueTask> GetAgentTasks(AgentTaskFilter filt var curTasks = new List(); foreach (var taskFile in Directory.EnumerateFiles(taskDir)) { - var task = ParseAgentTask(taskFile); + var task = await ParseAgentTask(taskFile); if (task == null) { continue; @@ -94,7 +94,7 @@ public async ValueTask> GetAgentTasks(AgentTaskFilter filt continue; } - var agent = ParseAgent(agentDir); + var agent = await ParseAgent(agentDir); curTasks.ForEach(t => { t.AgentId = agentId; @@ -110,7 +110,7 @@ public async ValueTask> GetAgentTasks(AgentTaskFilter filt }; } - public AgentTask? GetAgentTask(string agentId, string taskId) + public async Task GetAgentTask(string agentId, string taskId) { var agentDir = Path.Combine(_dbSettings.FileRepository, _agentSettings.DataDir, agentId); if (!Directory.Exists(agentDir)) @@ -130,19 +130,19 @@ public async ValueTask> GetAgentTasks(AgentTaskFilter filt return null; } - var task = ParseAgentTask(taskFile); + var task = await ParseAgentTask(taskFile); if (task == null) { return null; } - var agent = ParseAgent(agentDir); + var agent = await ParseAgent(agentDir); task.AgentId = agentId; task.Agent = agent; return task; } - public void InsertAgentTask(AgentTask task) + public async Task InsertAgentTask(AgentTask task) { if (task == null || string.IsNullOrEmpty(task.AgentId)) { @@ -173,15 +173,24 @@ public void InsertAgentTask(AgentTask task) }; var fileContent = BuildAgentTaskFileContent(metaData, task.Content); - File.WriteAllText(taskFile, fileContent); + await File.WriteAllTextAsync(taskFile, fileContent); } - public void BulkInsertAgentTasks(string agentId, List tasks) + public async Task BulkInsertAgentTasks(string agentId, List tasks) { - + if (tasks.IsNullOrEmpty()) + { + return; + } + + foreach (var task in tasks) + { + task.AgentId = agentId; + await InsertAgentTask(task); + } } - public void UpdateAgentTask(AgentTask task, AgentTaskField field) + public async Task UpdateAgentTask(AgentTask task, AgentTaskField field) { if (task == null || string.IsNullOrEmpty(task.Id)) { @@ -206,7 +215,7 @@ public void UpdateAgentTask(AgentTask task, AgentTaskField field) return; } - var parsedTask = ParseAgentTask(taskFile); + var parsedTask = await ParseAgentTask(taskFile); if (parsedTask == null) { return; @@ -245,10 +254,10 @@ public void UpdateAgentTask(AgentTask task, AgentTaskField field) } var fileContent = BuildAgentTaskFileContent(metaData, content); - File.WriteAllText(taskFile, fileContent); + await File.WriteAllTextAsync(taskFile, fileContent); } - public bool DeleteAgentTasks(string agentId, List? taskIds = null) + public async Task DeleteAgentTasks(string agentId, List? taskIds = null) { var agentDir = Path.Combine(_dbSettings.FileRepository, _agentSettings.DataDir, agentId); if (!Directory.Exists(agentDir)) @@ -281,7 +290,7 @@ public bool DeleteAgentTasks(string agentId, List? taskIds = null) deletedTasks.Add(taskId); } - return deletedTasks.Any(); + return await Task.FromResult(deletedTasks.Any()); } private string? FindTaskFileById(string taskDir, string taskId) diff --git a/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.Conversation.cs b/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.Conversation.cs index ed88eda44..51e328e93 100644 --- a/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.Conversation.cs +++ b/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.Conversation.cs @@ -1,12 +1,13 @@ using BotSharp.Abstraction.Loggers.Models; using System.IO; +using System.Threading; namespace BotSharp.Core.Repository; public partial class FileRepository { - private static readonly object _dialogLock = new object(); - private static readonly object _stateLock = new object(); + private static readonly SemaphoreSlim _dialogLock = new SemaphoreSlim(1, 1); + private static readonly SemaphoreSlim _stateLock = new SemaphoreSlim(1, 1); public async Task CreateNewConversation(Conversation conversation) { @@ -52,11 +53,11 @@ public async Task CreateNewConversation(Conversation conversation) } } - public bool DeleteConversations(IEnumerable conversationIds) + public Task DeleteConversations(IEnumerable conversationIds) { if (conversationIds.IsNullOrEmpty()) { - return false; + return Task.FromResult(false); } foreach (var conversationId in conversationIds) @@ -70,25 +71,26 @@ public bool DeleteConversations(IEnumerable conversationIds) Directory.Delete(convDir, true); } - return true; + return Task.FromResult(true); } [SideCar] - public List GetConversationDialogs(string conversationId) + public async Task> GetConversationDialogs(string conversationId) { var dialogs = new List(); var convDir = FindConversationDirectory(conversationId); if (!string.IsNullOrEmpty(convDir)) { var dialogDir = Path.Combine(convDir, DIALOG_FILE); - lock (_dialogLock) + await _dialogLock.WaitAsync(); + try { if (!File.Exists(dialogDir)) { return dialogs; } - var texts = File.ReadAllText(dialogDir); + var texts = await File.ReadAllTextAsync(dialogDir); try { dialogs = JsonSerializer.Deserialize>(texts, _options) ?? new List(); @@ -98,23 +100,28 @@ public List GetConversationDialogs(string conversationId) dialogs = new List(); } } + finally + { + _dialogLock.Release(); + } } return dialogs; } [SideCar] - public void AppendConversationDialogs(string conversationId, List dialogs) + public async Task AppendConversationDialogs(string conversationId, List dialogs) { var convDir = FindConversationDirectory(conversationId); if (!string.IsNullOrEmpty(convDir)) { - lock (_dialogLock) + await _dialogLock.WaitAsync(); + try { var dialogFile = Path.Combine(convDir, DIALOG_FILE); if (File.Exists(dialogFile)) { - var prevDialogs = File.ReadAllText(dialogFile); + var prevDialogs = await File.ReadAllTextAsync(dialogFile); var elements = JsonSerializer.Deserialize>(prevDialogs, _options); if (elements != null) { @@ -125,59 +132,63 @@ public void AppendConversationDialogs(string conversationId, List elements = elements ?? new List(); } - File.WriteAllText(dialogFile, JsonSerializer.Serialize(elements, _options)); + await File.WriteAllTextAsync(dialogFile, JsonSerializer.Serialize(elements, _options)); } var convFile = Path.Combine(convDir, CONVERSATION_FILE); if (File.Exists(convFile)) { - var json = File.ReadAllText(convFile); + var json = await File.ReadAllTextAsync(convFile); var conv = JsonSerializer.Deserialize(json, _options); if (conv != null) { conv.DialogCount += dialogs.Count(); conv.UpdatedTime = DateTime.UtcNow; - File.WriteAllText(convFile, JsonSerializer.Serialize(conv, _options)); + await File.WriteAllTextAsync(convFile, JsonSerializer.Serialize(conv, _options)); } } } + finally + { + _dialogLock.Release(); + } } } - public void UpdateConversationTitle(string conversationId, string title) + public async Task UpdateConversationTitle(string conversationId, string title) { var convDir = FindConversationDirectory(conversationId); if (!string.IsNullOrEmpty(convDir)) { var convFile = Path.Combine(convDir, CONVERSATION_FILE); - var content = File.ReadAllText(convFile); + var content = await File.ReadAllTextAsync(convFile); var record = JsonSerializer.Deserialize(content, _options); if (record != null) { record.Title = title; record.UpdatedTime = DateTime.UtcNow; - File.WriteAllText(convFile, JsonSerializer.Serialize(record, _options)); + await File.WriteAllTextAsync(convFile, JsonSerializer.Serialize(record, _options)); } } } - public void UpdateConversationTitleAlias(string conversationId, string titleAlias) + public async Task UpdateConversationTitleAlias(string conversationId, string titleAlias) { var convDir = FindConversationDirectory(conversationId); if (!string.IsNullOrEmpty(convDir)) { var convFile = Path.Combine(convDir, CONVERSATION_FILE); - var content = File.ReadAllText(convFile); + var content = await File.ReadAllTextAsync(convFile); var record = JsonSerializer.Deserialize(content, _options); if (record != null) { record.TitleAlias = titleAlias; record.UpdatedTime = DateTime.UtcNow; - File.WriteAllText(convFile, JsonSerializer.Serialize(record, _options)); + await File.WriteAllTextAsync(convFile, JsonSerializer.Serialize(record, _options)); } } } - public bool UpdateConversationTags(string conversationId, List toAddTags, List toDeleteTags) + public async Task UpdateConversationTags(string conversationId, List toAddTags, List toDeleteTags) { if (string.IsNullOrEmpty(conversationId)) { @@ -196,7 +207,7 @@ public bool UpdateConversationTags(string conversationId, List toAddTags return false; } - var json = File.ReadAllText(convFile); + var json = await File.ReadAllTextAsync(convFile); var conv = JsonSerializer.Deserialize(json, _options); var tags = conv.Tags ?? []; @@ -204,11 +215,11 @@ public bool UpdateConversationTags(string conversationId, List toAddTags conv.Tags = tags.Where(x => !toDeleteTags.Contains(x, StringComparer.OrdinalIgnoreCase)).ToList(); conv.UpdatedTime = DateTime.UtcNow; - File.WriteAllText(convFile, JsonSerializer.Serialize(conv, _options)); + await File.WriteAllTextAsync(convFile, JsonSerializer.Serialize(conv, _options)); return true; } - public bool AppendConversationTags(string conversationId, List tags) + public async Task AppendConversationTags(string conversationId, List tags) { if (string.IsNullOrEmpty(conversationId) || tags.IsNullOrEmpty()) { @@ -227,25 +238,25 @@ public bool AppendConversationTags(string conversationId, List tags) return false; } - var json = File.ReadAllText(convFile); + var json = await File.ReadAllTextAsync(convFile); var conv = JsonSerializer.Deserialize(json, _options); var curTags = conv.Tags ?? new(); var newTags = curTags.Concat(tags).Distinct(StringComparer.InvariantCultureIgnoreCase).ToList(); conv.Tags = newTags; conv.UpdatedTime = DateTime.UtcNow; - File.WriteAllText(convFile, JsonSerializer.Serialize(conv, _options)); + await File.WriteAllTextAsync(convFile, JsonSerializer.Serialize(conv, _options)); return true; } - public bool UpdateConversationMessage(string conversationId, UpdateMessageRequest request) + public async Task UpdateConversationMessage(string conversationId, UpdateMessageRequest request) { if (string.IsNullOrEmpty(conversationId)) { return false; } - var dialogs = GetConversationDialogs(conversationId); + var dialogs = await GetConversationDialogs(conversationId); var candidates = dialogs.Where(x => x.MetaData.MessageId == request.Message.MetaData.MessageId && x.MetaData.Role == request.Message.MetaData.Role).ToList(); @@ -280,7 +291,7 @@ public bool UpdateConversationMessage(string conversationId, UpdateMessageReques } [SideCar] - public void UpdateConversationBreakpoint(string conversationId, ConversationBreakpoint breakpoint) + public async Task UpdateConversationBreakpoint(string conversationId, ConversationBreakpoint breakpoint) { var convDir = FindConversationDirectory(conversationId); if (!string.IsNullOrEmpty(convDir)) @@ -288,7 +299,7 @@ public void UpdateConversationBreakpoint(string conversationId, ConversationBrea var breakpointFile = Path.Combine(convDir, BREAKPOINT_FILE); if (!File.Exists(breakpointFile)) { - File.WriteAllText(breakpointFile, "[]"); + await File.WriteAllTextAsync(breakpointFile, "[]"); } var content = File.ReadAllText(breakpointFile); @@ -313,12 +324,12 @@ public void UpdateConversationBreakpoint(string conversationId, ConversationBrea records = newBreakpoint; } - File.WriteAllText(breakpointFile, JsonSerializer.Serialize(records, _options)); + await File.WriteAllTextAsync(breakpointFile, JsonSerializer.Serialize(records, _options)); } } [SideCar] - public ConversationBreakpoint? GetConversationBreakpoint(string conversationId) + public async Task GetConversationBreakpoint(string conversationId) { var convDir = FindConversationDirectory(conversationId); if (string.IsNullOrEmpty(convDir)) @@ -329,30 +340,30 @@ public void UpdateConversationBreakpoint(string conversationId, ConversationBrea var breakpointFile = Path.Combine(convDir, BREAKPOINT_FILE); if (!File.Exists(breakpointFile)) { - File.WriteAllText(breakpointFile, "[]"); + await File.WriteAllTextAsync(breakpointFile, "[]"); } - var content = File.ReadAllText(breakpointFile); + var content = await File.ReadAllTextAsync(breakpointFile); var records = JsonSerializer.Deserialize>(content, _options); return records?.LastOrDefault(); } - public ConversationState GetConversationStates(string conversationId) + public async Task GetConversationStates(string conversationId) { var states = new List(); var convDir = FindConversationDirectory(conversationId); if (!string.IsNullOrEmpty(convDir)) { var stateFile = Path.Combine(convDir, STATE_FILE); - states = CollectConversationStates(stateFile); + states = await CollectConversationStates(stateFile); } return new ConversationState(states); } [SideCar] - public void UpdateConversationStates(string conversationId, List states) + public async Task UpdateConversationStates(string conversationId, List states) { if (states.IsNullOrEmpty()) { @@ -365,13 +376,14 @@ public void UpdateConversationStates(string conversationId, List return; } - lock (_stateLock) + await _stateLock.WaitAsync(); + try { var stateFile = Path.Combine(convDir, STATE_FILE); if (File.Exists(stateFile)) { var stateStr = JsonSerializer.Serialize(states, _options); - File.WriteAllText(stateFile, stateStr); + await File.WriteAllTextAsync(stateFile, stateStr); } var latestStateFile = Path.Combine(convDir, CONV_LATEST_STATE_FILE); @@ -379,12 +391,16 @@ public void UpdateConversationStates(string conversationId, List { var latestStates = BuildLatestStates(states); var stateStr = JsonSerializer.Serialize(latestStates, _options); - File.WriteAllText(latestStateFile, stateStr); + await File.WriteAllTextAsync(latestStateFile, stateStr); } } + finally + { + _stateLock.Release(); + } } - public void UpdateConversationStatus(string conversationId, string status) + public async Task UpdateConversationStatus(string conversationId, string status) { var convDir = FindConversationDirectory(conversationId); if (!string.IsNullOrEmpty(convDir)) @@ -392,11 +408,11 @@ public void UpdateConversationStatus(string conversationId, string status) var convFile = Path.Combine(convDir, CONVERSATION_FILE); if (File.Exists(convFile)) { - var json = File.ReadAllText(convFile); + var json = await File.ReadAllTextAsync(convFile); var conv = JsonSerializer.Deserialize(json, _options); conv.Status = status; conv.UpdatedTime = DateTime.UtcNow; - File.WriteAllText(convFile, JsonSerializer.Serialize(conv, _options)); + await File.WriteAllTextAsync(convFile, JsonSerializer.Serialize(conv, _options)); } } } @@ -416,7 +432,7 @@ public async Task GetConversation(string conversationId, bool isLo var dialogFile = Path.Combine(convDir, DIALOG_FILE); if (record != null) { - record.Dialogs = CollectDialogElements(dialogFile); + record.Dialogs = await CollectDialogElements(dialogFile); } if (isLoadStates) @@ -621,7 +637,7 @@ public async ValueTask> GetConversations(ConversationFi }; } - public List GetLastConversations() + public async Task> GetLastConversations() { var records = new List(); var dir = Path.Combine(_dbSettings.FileRepository, _conversationSettings.DataDir); @@ -634,7 +650,7 @@ public List GetLastConversations() continue; } - var json = File.ReadAllText(path); + var json = await File.ReadAllTextAsync(path); var record = JsonSerializer.Deserialize(json, _options); if (record == null) { @@ -648,7 +664,7 @@ public List GetLastConversations() .ToList(); } - public List GetIdleConversations(int batchSize, int messageLimit, int bufferHours, IEnumerable excludeAgentIds) + public async Task> GetIdleConversations(int batchSize, int messageLimit, int bufferHours, IEnumerable excludeAgentIds) { var ids = new List(); var batchLimit = 100; @@ -674,7 +690,7 @@ public List GetIdleConversations(int batchSize, int messageLimit, int bu continue; } - var json = File.ReadAllText(convFile); + var json = await File.ReadAllTextAsync(convFile); var conv = JsonSerializer.Deserialize(json, _options); if (conv == null) @@ -702,7 +718,7 @@ public List GetIdleConversations(int batchSize, int messageLimit, int bu } - public List TruncateConversation(string conversationId, string messageId, bool cleanLog = false) + public async Task> TruncateConversation(string conversationId, string messageId, bool cleanLog = false) { var deletedMessageIds = new List(); if (string.IsNullOrEmpty(conversationId) || string.IsNullOrEmpty(messageId)) @@ -719,7 +735,7 @@ public List TruncateConversation(string conversationId, string messageId } var dialogDir = Path.Combine(convDir, DIALOG_FILE); - dialogs = CollectDialogElements(dialogDir); + dialogs = await CollectDialogElements(dialogDir); if (dialogs.IsNullOrEmpty()) { return deletedMessageIds; @@ -741,7 +757,7 @@ public List TruncateConversation(string conversationId, string messageId var refTime = dialogs.ElementAt(foundIdx).MetaData.CreatedTime; var stateDir = Path.Combine(convDir, STATE_FILE); var latestStateDir = Path.Combine(convDir, CONV_LATEST_STATE_FILE); - var states = CollectConversationStates(stateDir); + var states = await CollectConversationStates(stateDir); isSaved = HandleTruncatedStates(stateDir, latestStateDir, states, messageId, refTime); // Handle truncated breakpoints @@ -761,7 +777,7 @@ public List TruncateConversation(string conversationId, string messageId #if !DEBUG [SharpCache(10)] #endif - public List GetConversationStateSearchKeys(ConversationStateKeysFilter filter) + public async Task> GetConversationStateSearchKeys(ConversationStateKeysFilter filter) { var dir = Path.Combine(_dbSettings.FileRepository, _conversationSettings.DataDir); if (!Directory.Exists(dir)) @@ -781,8 +797,8 @@ public List GetConversationStateSearchKeys(ConversationStateKeysFilter f continue; } - var convJson = File.ReadAllText(convFile); - var stateJson = File.ReadAllText(latestStateFile); + var convJson = await File.ReadAllTextAsync(convFile); + var stateJson = await File.ReadAllTextAsync(latestStateFile); var conv = JsonSerializer.Deserialize(convJson, _options); var states = JsonSerializer.Deserialize>(stateJson, _options); if (conv == null @@ -810,15 +826,15 @@ public List GetConversationStateSearchKeys(ConversationStateKeysFilter f - public List GetConversationsToMigrate(int batchSize = 100) + public Task> GetConversationsToMigrate(int batchSize = 100) { + var convIds = new List(); var baseDir = Path.Combine(_dbSettings.FileRepository, _conversationSettings.DataDir); if (!Directory.Exists(baseDir)) { - return []; + return Task.FromResult(convIds); } - var convIds = new List(); foreach (var dir in Directory.EnumerateDirectories(baseDir)) { var latestStateFile = Path.Combine(dir, CONV_LATEST_STATE_FILE); @@ -840,11 +856,11 @@ public List GetConversationsToMigrate(int batchSize = 100) } } - return convIds; + return Task.FromResult(convIds); } - public bool MigrateConvsersationLatestStates(string conversationId) + public async Task MigrateConvsersationLatestStates(string conversationId) { if (string.IsNullOrEmpty(conversationId)) { @@ -858,12 +874,12 @@ public bool MigrateConvsersationLatestStates(string conversationId) } var stateFile = Path.Combine(convDir, STATE_FILE); - var states = CollectConversationStates(stateFile); + var states = await CollectConversationStates(stateFile); var latestStates = BuildLatestStates(states); var latestStateFile = Path.Combine(convDir, CONV_LATEST_STATE_FILE); var stateStr = JsonSerializer.Serialize(latestStates, _options); - File.WriteAllText(latestStateFile, stateStr); + await File.WriteAllTextAsync(latestStateFile, stateStr); return true; } @@ -885,9 +901,10 @@ public bool MigrateConvsersationLatestStates(string conversationId) return dir; } - private List CollectDialogElements(string dialogDir) + private async Task> CollectDialogElements(string dialogDir) { - lock (_dialogLock) + await _dialogLock.WaitAsync(); + try { var dialogs = new List(); @@ -896,10 +913,14 @@ private List CollectDialogElements(string dialogDir) return dialogs; } - var texts = File.ReadAllText(dialogDir); + var texts = await File.ReadAllTextAsync(dialogDir); dialogs = JsonSerializer.Deserialize>(texts) ?? new List(); return dialogs; } + finally + { + _dialogLock.Release(); + } } private string ParseDialogElements(List dialogs) @@ -912,9 +933,10 @@ private string ParseDialogElements(List dialogs) return JsonSerializer.Serialize(dialogs, _options) ?? "[]"; } - private List CollectConversationStates(string stateFile) + private async Task> CollectConversationStates(string stateFile) { - lock (_stateLock) + await _stateLock.WaitAsync(); + try { var states = new List(); if (!File.Exists(stateFile)) @@ -922,7 +944,7 @@ private List CollectConversationStates(string stateFile) return states; } - var stateStr = File.ReadAllText(stateFile); + var stateStr = await File.ReadAllTextAsync(stateFile); if (string.IsNullOrEmpty(stateStr)) { return states; @@ -931,6 +953,10 @@ private List CollectConversationStates(string stateFile) states = JsonSerializer.Deserialize>(stateStr, _options); return states ?? new List(); } + finally + { + _stateLock.Release(); + } } private List CollectConversationBreakpoints(string breakpointFile) diff --git a/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.Crontab.cs b/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.Crontab.cs index ded762c85..b48d9f62c 100644 --- a/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.Crontab.cs +++ b/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.Crontab.cs @@ -4,7 +4,7 @@ namespace BotSharp.Core.Repository; public partial class FileRepository { - public bool UpsertCrontabItem(CrontabItem cron) + public async Task UpsertCrontabItem(CrontabItem cron) { if (cron == null || string.IsNullOrWhiteSpace(cron.ConversationId)) { @@ -21,7 +21,7 @@ public bool UpsertCrontabItem(CrontabItem cron) var cronFile = Path.Combine(baseDir, CRON_FILE); var json = JsonSerializer.Serialize(cron, _options); - File.WriteAllText(cronFile, json); + await File.WriteAllTextAsync(cronFile, json); return true; } catch (Exception ex) @@ -31,7 +31,7 @@ public bool UpsertCrontabItem(CrontabItem cron) } } - public bool DeleteCrontabItem(string conversationId) + public async Task DeleteCrontabItem(string conversationId) { if (string.IsNullOrWhiteSpace(conversationId)) { @@ -53,7 +53,7 @@ public bool DeleteCrontabItem(string conversationId) } File.Delete(cronFile); - return true; + return await Task.FromResult(true); } catch (Exception ex) { @@ -86,7 +86,7 @@ public async ValueTask> GetCrontabItems(CrontabItemFilte continue; } - var json = File.ReadAllText(file); + var json = await File.ReadAllTextAsync(file); var record = JsonSerializer.Deserialize(json, _options); if (record == null) { diff --git a/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.KnowledgeBase.cs b/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.KnowledgeBase.cs index d8e30975f..bd22c2731 100644 --- a/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.KnowledgeBase.cs +++ b/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.KnowledgeBase.cs @@ -8,7 +8,7 @@ namespace BotSharp.Core.Repository; public partial class FileRepository { #region Configs - public bool AddKnowledgeCollectionConfigs(List configs, bool reset = false) + public async Task AddKnowledgeCollectionConfigs(List configs, bool reset = false) { var vectorDir = BuildKnowledgeCollectionConfigDir(); if (!Directory.Exists(vectorDir)) @@ -19,16 +19,16 @@ public bool AddKnowledgeCollectionConfigs(List configs, var configFile = Path.Combine(vectorDir, COLLECTION_CONFIG_FILE); if (!File.Exists(configFile)) { - File.WriteAllText(configFile, "[]"); + await File.WriteAllTextAsync(configFile, "[]"); } if (reset) { - File.WriteAllText(configFile, JsonSerializer.Serialize(configs ?? new(), _options)); + await File.WriteAllTextAsync(configFile, JsonSerializer.Serialize(configs ?? new(), _options)); return true; } - var str = File.ReadAllText(configFile); + var str = await File.ReadAllTextAsync(configFile); var savedConfigs = JsonSerializer.Deserialize>(str, _options) ?? new(); // Update if collection already exists, otherwise insert @@ -48,11 +48,11 @@ public bool AddKnowledgeCollectionConfigs(List configs, } } - File.WriteAllText(configFile, JsonSerializer.Serialize(savedConfigs ?? new(), _options)); + await File.WriteAllTextAsync(configFile, JsonSerializer.Serialize(savedConfigs ?? new(), _options)); return true; } - public bool DeleteKnowledgeCollectionConfig(string collectionName) + public async Task DeleteKnowledgeCollectionConfig(string collectionName) { if (string.IsNullOrWhiteSpace(collectionName)) { @@ -66,10 +66,10 @@ public bool DeleteKnowledgeCollectionConfig(string collectionName) return false; } - var str = File.ReadAllText(configFile); + var str = await File.ReadAllTextAsync(configFile); var savedConfigs = JsonSerializer.Deserialize>(str, _options) ?? new(); savedConfigs = savedConfigs.Where(x => x.Name != collectionName).ToList(); - File.WriteAllText(configFile, JsonSerializer.Serialize(savedConfigs ?? new(), _options)); + await File.WriteAllTextAsync(configFile, JsonSerializer.Serialize(savedConfigs ?? new(), _options)); return true; } @@ -125,7 +125,7 @@ public async Task GetKnowledgeCollectionConfig(string co #region Documents - public bool SaveKnolwedgeBaseFileMeta(KnowledgeDocMetaData metaData) + public async Task SaveKnolwedgeBaseFileMeta(KnowledgeDocMetaData metaData) { if (metaData == null || string.IsNullOrWhiteSpace(metaData.Collection) @@ -143,11 +143,11 @@ public bool SaveKnolwedgeBaseFileMeta(KnowledgeDocMetaData metaData) var metaFile = Path.Combine(docDir, KNOWLEDGE_DOC_META_FILE); var content = JsonSerializer.Serialize(metaData, _options); - File.WriteAllText(metaFile, content); + await File.WriteAllTextAsync(metaFile, content); return true; } - public bool DeleteKnolwedgeBaseFileMeta(string collectionName, string vectorStoreProvider, Guid? fileId = null) + public async Task DeleteKnolwedgeBaseFileMeta(string collectionName, string vectorStoreProvider, Guid? fileId = null) { if (string.IsNullOrWhiteSpace(collectionName) || string.IsNullOrWhiteSpace(vectorStoreProvider)) @@ -174,7 +174,7 @@ public bool DeleteKnolwedgeBaseFileMeta(string collectionName, string vectorStor } } - return true; + return await Task.FromResult(true); } public async ValueTask> GetKnowledgeBaseFileMeta(string collectionName, string vectorStoreProvider, KnowledgeFileFilter filter) diff --git a/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.Log.cs b/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.Log.cs index e15f70221..624803793 100644 --- a/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.Log.cs +++ b/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.Log.cs @@ -7,7 +7,7 @@ namespace BotSharp.Core.Repository; public partial class FileRepository { #region LLM Completion Log - public void SaveLlmCompletionLog(LlmCompletionLog log) + public async Task SaveLlmCompletionLog(LlmCompletionLog log) { if (log == null) { @@ -32,12 +32,12 @@ public void SaveLlmCompletionLog(LlmCompletionLog log) var index = GetNextLogIndex(logDir, log.MessageId); var file = Path.Combine(logDir, $"{log.MessageId}.{index}.log"); - File.WriteAllText(file, JsonSerializer.Serialize(log, _options)); + await File.WriteAllTextAsync(file, JsonSerializer.Serialize(log, _options)); } #endregion #region Conversation Content Log - public void SaveConversationContentLog(ContentLogOutputModel log) + public async Task SaveConversationContentLog(ContentLogOutputModel log) { if (log == null) { @@ -61,10 +61,10 @@ public void SaveConversationContentLog(ContentLogOutputModel log) var index = GetNextLogIndex(logDir, log.MessageId); var file = Path.Combine(logDir, $"{log.MessageId}.{index}.log"); - File.WriteAllText(file, JsonSerializer.Serialize(log, _options)); + await File.WriteAllTextAsync(file, JsonSerializer.Serialize(log, _options)); } - public DateTimePagination GetConversationContentLogs(string conversationId, ConversationLogFilter filter) + public async Task> GetConversationContentLogs(string conversationId, ConversationLogFilter filter) { if (string.IsNullOrEmpty(conversationId)) { @@ -86,7 +86,7 @@ public DateTimePagination GetConversationContentLogs(stri var logs = new List(); foreach (var file in Directory.EnumerateFiles(logDir)) { - var text = File.ReadAllText(file); + var text = await File.ReadAllTextAsync(file); var log = JsonSerializer.Deserialize(text); if (log == null || log.CreatedTime >= filter.StartTime) { @@ -108,7 +108,7 @@ public DateTimePagination GetConversationContentLogs(stri #endregion #region Conversation State Log - public void SaveConversationStateLog(ConversationStateLogModel log) + public async Task SaveConversationStateLog(ConversationStateLogModel log) { if (log == null) { @@ -132,10 +132,10 @@ public void SaveConversationStateLog(ConversationStateLogModel log) var index = GetNextLogIndex(logDir, log.MessageId); var file = Path.Combine(logDir, $"{log.MessageId}.{index}.log"); - File.WriteAllText(file, JsonSerializer.Serialize(log, _options)); + await File.WriteAllTextAsync(file, JsonSerializer.Serialize(log, _options)); } - public DateTimePagination GetConversationStateLogs(string conversationId, ConversationLogFilter filter) + public async Task> GetConversationStateLogs(string conversationId, ConversationLogFilter filter) { if (string.IsNullOrEmpty(conversationId)) { @@ -157,7 +157,7 @@ public DateTimePagination GetConversationStateLogs(st var logs = new List(); foreach (var file in Directory.EnumerateFiles(logDir)) { - var text = File.ReadAllText(file); + var text = await File.ReadAllTextAsync(file); var log = JsonSerializer.Deserialize(text); if (log == null || log.CreatedTime >= filter.StartTime) { diff --git a/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.Plugin.cs b/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.Plugin.cs index 787c57b79..f5c1f7d16 100644 --- a/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.Plugin.cs +++ b/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.Plugin.cs @@ -5,7 +5,7 @@ namespace BotSharp.Core.Repository; public partial class FileRepository { - public PluginConfig GetPluginConfig() + public async Task GetPluginConfig() { if (_pluginConfig != null) { @@ -24,16 +24,16 @@ public PluginConfig GetPluginConfig() return _pluginConfig; } - var json = File.ReadAllText(configFile); + var json = await File.ReadAllTextAsync(configFile); _pluginConfig = JsonSerializer.Deserialize(json, _options); return _pluginConfig; } - public void SavePluginConfig(PluginConfig config) + public async Task SavePluginConfig(PluginConfig config) { var configFile = Path.Combine(_dbSettings.FileRepository, "plugins", "config.json"); - File.WriteAllText(configFile, JsonSerializer.Serialize(config, _options)); + await File.WriteAllTextAsync(configFile, JsonSerializer.Serialize(config, _options)); _pluginConfig = null; } } diff --git a/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.Role.cs b/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.Role.cs index 825df02f0..131d05500 100644 --- a/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.Role.cs +++ b/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.Role.cs @@ -4,7 +4,7 @@ namespace BotSharp.Core.Repository; public partial class FileRepository { - public bool RefreshRoles(IEnumerable roles) + public async Task RefreshRoles(IEnumerable roles) { if (roles.IsNullOrEmpty()) { @@ -30,17 +30,17 @@ public bool RefreshRoles(IEnumerable roles) { var dir = Path.Combine(baseDir, role.Id); Directory.CreateDirectory(dir); - Thread.Sleep(50); + await Task.Delay(50); var roleFile = Path.Combine(dir, ROLE_FILE); role.CreatedTime = DateTime.UtcNow; role.UpdatedTime = DateTime.UtcNow; - File.WriteAllText(roleFile, JsonSerializer.Serialize(role, _options)); + await File.WriteAllTextAsync(roleFile, JsonSerializer.Serialize(role, _options)); } return true; } - public IEnumerable GetRoles(RoleFilter filter) + public async Task> GetRoles(RoleFilter filter) { var roles = Roles; if (filter == null) @@ -59,10 +59,10 @@ public IEnumerable GetRoles(RoleFilter filter) roles = roles.Where(x => !filter.ExcludeRoles.Contains(x.Name)); } - return roles.ToList(); + return await Task.FromResult(roles.ToList()); } - public Role? GetRoleDetails(string roleId, bool includeAgent = false) + public async Task GetRoleDetails(string roleId, bool includeAgent = false) { if (string.IsNullOrWhiteSpace(roleId)) { @@ -93,7 +93,7 @@ public IEnumerable GetRoles(RoleFilter filter) var agentIds = roleAgents.Select(x => x.AgentId).Distinct().ToList(); if (!agentIds.IsNullOrEmpty()) { - var agents = GetAgents(new AgentFilter { AgentIds = agentIds }); + var agents = await GetAgents(new AgentFilter { AgentIds = agentIds }); foreach (var item in roleAgents) { @@ -114,7 +114,7 @@ public IEnumerable GetRoles(RoleFilter filter) return role; } - public bool UpdateRole(Role role, bool updateRoleAgents = false) + public async Task UpdateRole(Role role, bool updateRoleAgents = false) { if (string.IsNullOrEmpty(role?.Id) || string.IsNullOrEmpty(role?.Name)) { @@ -130,7 +130,7 @@ public bool UpdateRole(Role role, bool updateRoleAgents = false) var roleFile = Path.Combine(dir, ROLE_FILE); role.CreatedTime = DateTime.UtcNow; role.UpdatedTime = DateTime.UtcNow; - File.WriteAllText(roleFile, JsonSerializer.Serialize(role, _options)); + await File.WriteAllTextAsync(roleFile, JsonSerializer.Serialize(role, _options)); if (updateRoleAgents) { @@ -145,7 +145,7 @@ public bool UpdateRole(Role role, bool updateRoleAgents = false) })?.ToList() ?? []; var roleAgentFile = Path.Combine(dir, ROLE_AGENT_FILE); - File.WriteAllText(roleAgentFile, JsonSerializer.Serialize(roleAgents, _options)); + await File.WriteAllTextAsync(roleAgentFile, JsonSerializer.Serialize(roleAgents, _options)); _roleAgents = []; } diff --git a/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.Stats.cs b/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.Stats.cs index 9a15f1778..ee9ac48ca 100644 --- a/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.Stats.cs +++ b/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.Stats.cs @@ -4,7 +4,7 @@ namespace BotSharp.Core.Repository; public partial class FileRepository { - public BotSharpStats? GetGlobalStats(string agentId, DateTime recordTime, StatsInterval interval) + public async Task GetGlobalStats(string agentId, DateTime recordTime, StatsInterval interval) { if (string.IsNullOrWhiteSpace(agentId)) { @@ -25,7 +25,7 @@ public partial class FileRepository return null; } - var text = File.ReadAllText(file); + var text = await File.ReadAllTextAsync(file); var list = JsonSerializer.Deserialize>(text, _options); var found = list?.FirstOrDefault(x => x.AgentId.IsEqualTo(agentId) && x.StartTime == startTime @@ -34,7 +34,7 @@ public partial class FileRepository return found; } - public bool SaveGlobalStats(BotSharpStatsDelta delta) + public async Task SaveGlobalStats(BotSharpStatsDelta delta) { if (delta == null || string.IsNullOrWhiteSpace(delta.AgentId)) { @@ -74,11 +74,11 @@ public bool SaveGlobalStats(BotSharpStatsDelta delta) if (!File.Exists(file)) { var list = new List { newItem }; - File.WriteAllText(file, JsonSerializer.Serialize(list, _options)); + await File.WriteAllTextAsync(file, JsonSerializer.Serialize(list, _options)); } else { - var text = File.ReadAllText(file); + var text = await File.ReadAllTextAsync(file); var list = JsonSerializer.Deserialize>(text, _options); var found = list?.FirstOrDefault(x => x.AgentId.IsEqualTo(delta.AgentId) && x.StartTime == startTime @@ -106,7 +106,7 @@ public bool SaveGlobalStats(BotSharpStatsDelta delta) list = [newItem]; } - File.WriteAllText(file, JsonSerializer.Serialize(list, _options)); + await File.WriteAllTextAsync(file, JsonSerializer.Serialize(list, _options)); } return true; diff --git a/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.Translation.cs b/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.Translation.cs index e4f016a02..3e15d335c 100644 --- a/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.Translation.cs +++ b/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.Translation.cs @@ -5,7 +5,7 @@ namespace BotSharp.Core.Repository; public partial class FileRepository { - public IEnumerable GetTranslationMemories(IEnumerable queries) + public async Task> GetTranslationMemories(IEnumerable queries) { var list = new List(); if (queries.IsNullOrEmpty()) @@ -20,7 +20,7 @@ public IEnumerable GetTranslationMemories(IEnumerable GetTranslationMemories(IEnumerable inputs) + public async Task SaveTranslationMemories(IEnumerable inputs) { if (inputs.IsNullOrEmpty()) return false; @@ -68,7 +68,7 @@ public bool SaveTranslationMemories(IEnumerable inputs) var content = string.Empty; if (File.Exists(file)) { - content = File.ReadAllText(file); + content = await File.ReadAllTextAsync(file); } var memories = ReadTranslationMemoryContent(content); @@ -129,7 +129,7 @@ public bool SaveTranslationMemories(IEnumerable inputs) } var json = JsonSerializer.Serialize(memories, _options); - File.WriteAllText(file, json); + await File.WriteAllTextAsync(file, json); return true; } catch (Exception ex) diff --git a/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.User.cs b/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.User.cs index 155fe9842..6ad065983 100644 --- a/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.User.cs +++ b/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.User.cs @@ -212,7 +212,7 @@ public Task> SearchLoginUsers(User filter, string source = UserSource return Task.FromResult(searchResult); } - public Task GetUserDetails(string userId, bool includeAgent = false) + public async Task GetUserDetails(string userId, bool includeAgent = false) { if (string.IsNullOrWhiteSpace(userId)) return null; @@ -231,13 +231,13 @@ public Task> SearchLoginUsers(User filter, string source = UserSource Actions = x.Actions }).ToList(); user.AgentActions = agentActions; - return Task.FromResult(user); + return user; } var agentIds = userAgents.Select(x => x.AgentId)?.Distinct().ToList(); if (!agentIds.IsNullOrEmpty()) { - var agents = GetAgents(new AgentFilter { AgentIds = agentIds }); + var agents = await GetAgents(new AgentFilter { AgentIds = agentIds }); foreach (var item in userAgents) { @@ -255,7 +255,7 @@ public Task> SearchLoginUsers(User filter, string source = UserSource } user.AgentActions = agentActions; - return Task.FromResult(user); + return user; } public async Task UpdateUser(User user, bool updateUserAgents = false) diff --git a/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.cs b/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.cs index 69e8bd38f..98872c505 100644 --- a/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.cs +++ b/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.cs @@ -402,7 +402,7 @@ private List FetchTemplates(string fileDir) return templates; } - private List FetchTasks(string fileDir) + private async Task> FetchTasks(string fileDir) { var tasks = new List(); var taskDir = Path.Combine(fileDir, AGENT_TASKS_FOLDER); @@ -410,7 +410,7 @@ private List FetchTasks(string fileDir) foreach (var file in Directory.GetFiles(taskDir)) { - var task = ParseAgentTask(file); + var task = await ParseAgentTask(file); if (task == null) continue; tasks.Add(task); @@ -438,11 +438,11 @@ private List FetchResponses(string fileDir) return responses; } - private Agent? ParseAgent(string agentDir) + private async Task ParseAgent(string agentDir) { if (string.IsNullOrEmpty(agentDir)) return null; - var agentJson = File.ReadAllText(Path.Combine(agentDir, AGENT_FILE)); + var agentJson = await File.ReadAllTextAsync(Path.Combine(agentDir, AGENT_FILE)); if (string.IsNullOrEmpty(agentJson)) return null; var agent = JsonSerializer.Deserialize(agentJson, _options); @@ -462,13 +462,13 @@ private List FetchResponses(string fileDir) .SetResponses(responses); } - private AgentTask? ParseAgentTask(string taskFile) + private async Task ParseAgentTask(string taskFile) { if (string.IsNullOrWhiteSpace(taskFile)) return null; var fileName = taskFile.Split(Path.DirectorySeparatorChar).Last(); var id = fileName.Split('.').First(); - var data = File.ReadAllText(taskFile); + var data = await File.ReadAllTextAsync(taskFile); var pattern = $@"{AGENT_TASK_PREFIX}.+{AGENT_TASK_SUFFIX}"; var metaData = Regex.Match(data, pattern, RegexOptions.Singleline); diff --git a/src/Infrastructure/BotSharp.Core/Roles/Services/RoleService.cs b/src/Infrastructure/BotSharp.Core/Roles/Services/RoleService.cs index 9636fe188..35515297d 100644 --- a/src/Infrastructure/BotSharp.Core/Roles/Services/RoleService.cs +++ b/src/Infrastructure/BotSharp.Core/Roles/Services/RoleService.cs @@ -22,30 +22,31 @@ public async Task RefreshRoles() var roles = allRoles.Select(x => new Role { Id = Guid.NewGuid().ToString(), Name = x }).ToList(); var db = _services.GetRequiredService(); - return db.RefreshRoles(roles); + return await db.RefreshRoles(roles); } - public async Task> GetRoleOptions() + public Task> GetRoleOptions() { var fields = typeof(UserRole).GetFields(BindingFlags.Public | BindingFlags.Static) .Where(x => x.IsLiteral && !x.IsInitOnly).ToList(); - return fields.Select(x => x.GetValue(null)?.ToString()) + var roleOptions = fields.Select(x => x.GetValue(null)?.ToString()) .Where(x => !string.IsNullOrWhiteSpace(x)) - .Distinct() - .ToList(); + .Distinct(); + + return Task.FromResult(roleOptions); } public async Task> GetRoles(RoleFilter filter) { var db = _services.GetRequiredService(); - var roles = db.GetRoles(filter); + var roles = await db.GetRoles(filter); if (filter.IsInit() && roles.IsNullOrEmpty()) { await RefreshRoles(); await Task.Delay(100); - roles = db.GetRoles(filter); + roles = await db.GetRoles(filter); } return roles; @@ -54,7 +55,7 @@ public async Task> GetRoles(RoleFilter filter) public async Task GetRoleDetails(string roleId, bool includeAgent = false) { var db = _services.GetRequiredService(); - var role = db.GetRoleDetails(roleId, includeAgent); + var role = await db.GetRoleDetails(roleId, includeAgent); return role; } @@ -68,6 +69,6 @@ public async Task UpdateRole(Role role, bool isUpdateRoleAgents = false) } var db = _services.GetRequiredService(); - return db.UpdateRole(role, isUpdateRoleAgents); + return await db.UpdateRole(role, isUpdateRoleAgents); } } diff --git a/src/Infrastructure/BotSharp.Core/Routing/Executor/DummyFunctionExecutor.cs b/src/Infrastructure/BotSharp.Core/Routing/Executor/DummyFunctionExecutor.cs index 91e0bc97e..4c8b681fe 100644 --- a/src/Infrastructure/BotSharp.Core/Routing/Executor/DummyFunctionExecutor.cs +++ b/src/Infrastructure/BotSharp.Core/Routing/Executor/DummyFunctionExecutor.cs @@ -30,8 +30,8 @@ public async Task ExecuteAsync(RoleDialogModel message) return true; } - public async Task GetIndicatorAsync(RoleDialogModel message) + public Task GetIndicatorAsync(RoleDialogModel message) { - return "Running"; + return Task.FromResult("Running"); } } diff --git a/src/Infrastructure/BotSharp.Core/Routing/Executor/MCPToolExecutor.cs b/src/Infrastructure/BotSharp.Core/Routing/Executor/MCPToolExecutor.cs index 8cf7d18e5..e402de841 100644 --- a/src/Infrastructure/BotSharp.Core/Routing/Executor/MCPToolExecutor.cs +++ b/src/Infrastructure/BotSharp.Core/Routing/Executor/MCPToolExecutor.cs @@ -50,9 +50,9 @@ public async Task ExecuteAsync(RoleDialogModel message) } } - public async Task GetIndicatorAsync(RoleDialogModel message) + public Task GetIndicatorAsync(RoleDialogModel message) { - return message.Indication ?? string.Empty; + return Task.FromResult(message.Indication ?? string.Empty); } diff --git a/src/Infrastructure/BotSharp.Core/Routing/Functions/RouteToAgentFn.cs b/src/Infrastructure/BotSharp.Core/Routing/Functions/RouteToAgentFn.cs index 9125d2b4c..2b14a9efa 100644 --- a/src/Infrastructure/BotSharp.Core/Routing/Functions/RouteToAgentFn.cs +++ b/src/Infrastructure/BotSharp.Core/Routing/Functions/RouteToAgentFn.cs @@ -48,7 +48,7 @@ public async Task Execute(RoleDialogModel message) var db = _services.GetRequiredService(); var filter = new AgentFilter { AgentNames = [args.OriginalAgent] }; - var originalAgent = db.GetAgents(filter).FirstOrDefault(); + var originalAgent = (await db.GetAgents(filter)).FirstOrDefault(); if (originalAgent != null) { _context.Push(originalAgent.Id, $"user goal agent{(correctToOriginalAgent ? " " + originalAgent.Name + " & is corrected" : "")}"); @@ -70,7 +70,7 @@ public async Task Execute(RoleDialogModel message) { var db = _services.GetRequiredService(); var filter = new AgentFilter { AgentNames = [args.AgentName] }; - var targetAgent = db.GetAgents(filter).FirstOrDefault(); + var targetAgent = (await db.GetAgents(filter)).FirstOrDefault(); if (targetAgent == null) { message.Data = JsonSerializer.Deserialize(message.FunctionArgs); diff --git a/src/Infrastructure/BotSharp.Core/Routing/Reasoning/HFReasoner.cs b/src/Infrastructure/BotSharp.Core/Routing/Reasoning/HFReasoner.cs index 55867e003..b63ca4947 100644 --- a/src/Infrastructure/BotSharp.Core/Routing/Reasoning/HFReasoner.cs +++ b/src/Infrastructure/BotSharp.Core/Routing/Reasoning/HFReasoner.cs @@ -66,7 +66,7 @@ public async Task AgentExecuting(Agent router, FunctionCallFromLlm inst, R { var db = _services.GetRequiredService(); var filter = new AgentFilter { AgentNames = [inst.AgentName] }; - var agent = db.GetAgents(filter).FirstOrDefault(); + var agent = (await db.GetAgents(filter)).FirstOrDefault(); var context = _services.GetRequiredService(); context.Push(agent.Id, reason: inst.NextActionReason); @@ -79,11 +79,11 @@ public async Task AgentExecuting(Agent router, FunctionCallFromLlm inst, R return true; } - public async Task AgentExecuted(Agent router, FunctionCallFromLlm inst, RoleDialogModel message, List dialogs) + public Task AgentExecuted(Agent router, FunctionCallFromLlm inst, RoleDialogModel message, List dialogs) { var context = _services.GetRequiredService(); context.Empty(reason: $"Agent queue is cleared by {nameof(HFReasoner)}"); - return true; + return Task.FromResult(true); } private string GetNextStepPrompt(Agent router) diff --git a/src/Infrastructure/BotSharp.Core/Routing/Reasoning/NaiveReasoner.cs b/src/Infrastructure/BotSharp.Core/Routing/Reasoning/NaiveReasoner.cs index 6adc9bc53..48c8423d2 100644 --- a/src/Infrastructure/BotSharp.Core/Routing/Reasoning/NaiveReasoner.cs +++ b/src/Infrastructure/BotSharp.Core/Routing/Reasoning/NaiveReasoner.cs @@ -64,16 +64,16 @@ public async Task GetNextInstruction(Agent router, string m return inst; } - public async Task AgentExecuting(Agent router, FunctionCallFromLlm inst, RoleDialogModel message, List dialogs) + public Task AgentExecuting(Agent router, FunctionCallFromLlm inst, RoleDialogModel message, List dialogs) { // Set user content as Planner's question message.FunctionName = inst.Function; message.FunctionArgs = inst.Arguments == null ? "{}" : JsonSerializer.Serialize(inst.Arguments); - return true; + return Task.FromResult(true); } - public async Task AgentExecuted(Agent router, FunctionCallFromLlm inst, RoleDialogModel message, List dialogs) + public Task AgentExecuted(Agent router, FunctionCallFromLlm inst, RoleDialogModel message, List dialogs) { var context = _services.GetRequiredService(); if (inst.UnmatchedAgent) @@ -92,7 +92,7 @@ public async Task AgentExecuted(Agent router, FunctionCallFromLlm inst, Ro context.Empty(reason: $"Agent queue is cleared by {nameof(NaiveReasoner)}"); // context.Push(inst.OriginalAgent, "Push user goal agent"); } - return true; + return Task.FromResult(true); } private string GetNextStepPrompt(Agent router) diff --git a/src/Infrastructure/BotSharp.Core/Routing/Reasoning/OneStepForwardReasoner.cs b/src/Infrastructure/BotSharp.Core/Routing/Reasoning/OneStepForwardReasoner.cs index 800b2457e..f2d7ed868 100644 --- a/src/Infrastructure/BotSharp.Core/Routing/Reasoning/OneStepForwardReasoner.cs +++ b/src/Infrastructure/BotSharp.Core/Routing/Reasoning/OneStepForwardReasoner.cs @@ -71,16 +71,16 @@ public async Task GetNextInstruction(Agent router, string m return inst; } - public async Task AgentExecuting(Agent router, FunctionCallFromLlm inst, RoleDialogModel message, List dialogs) + public Task AgentExecuting(Agent router, FunctionCallFromLlm inst, RoleDialogModel message, List dialogs) { // Set user content as Planner's question message.FunctionName = inst.Function; message.FunctionArgs = inst.Arguments == null ? "{}" : JsonSerializer.Serialize(inst.Arguments); - return true; + return Task.FromResult(true); } - public async Task AgentExecuted(Agent router, FunctionCallFromLlm inst, RoleDialogModel message, List dialogs) + public Task AgentExecuted(Agent router, FunctionCallFromLlm inst, RoleDialogModel message, List dialogs) { var context = _services.GetRequiredService(); if (inst.UnmatchedAgent) @@ -99,7 +99,7 @@ public async Task AgentExecuted(Agent router, FunctionCallFromLlm inst, Ro context.Empty(reason: $"Agent queue is cleared by {nameof(OneStepForwardReasoner)}"); // context.Push(inst.OriginalAgent, "Push user goal agent"); } - return true; + return Task.FromResult(true); } private string GetNextStepPrompt(Agent router) diff --git a/src/Infrastructure/BotSharp.Core/Routing/RoutingService.InstructLoop.cs b/src/Infrastructure/BotSharp.Core/Routing/RoutingService.InstructLoop.cs index fb2044ea0..e49410616 100644 --- a/src/Infrastructure/BotSharp.Core/Routing/RoutingService.InstructLoop.cs +++ b/src/Infrastructure/BotSharp.Core/Routing/RoutingService.InstructLoop.cs @@ -41,7 +41,7 @@ public async Task InstructLoop(Agent agent, RoleDialogModel mes dialogs.Add(message); Context.SetDialogs(dialogs); - storage.Append(convService.ConversationId, message); + await storage.Append(convService.ConversationId, message); // Get first instruction _router.TemplateDict["conversation"] = await GetConversationContent(dialogs); diff --git a/src/Infrastructure/BotSharp.Core/Routing/RoutingService.cs b/src/Infrastructure/BotSharp.Core/Routing/RoutingService.cs index 4e43cbd52..398f0c40d 100644 --- a/src/Infrastructure/BotSharp.Core/Routing/RoutingService.cs +++ b/src/Infrastructure/BotSharp.Core/Routing/RoutingService.cs @@ -30,7 +30,7 @@ public async Task InstructDirect(Agent agent, RoleDialogModel m { var conv = _services.GetRequiredService(); var storage = _services.GetRequiredService(); - storage.Append(conv.ConversationId, message); + await storage.Append(conv.ConversationId, message); dialogs.Add(message); Context.SetDialogs(dialogs); @@ -78,7 +78,7 @@ protected RoutingRule[] GetRoutingRecords() { Disabled = false }; - var agents = db.GetAgents(filter); + var agents = db.GetAgents(filter).ConfigureAwait(false).GetAwaiter().GetResult(); var records = agents.Where(x => x.Type == AgentType.Task || x.Type == AgentType.Planning).SelectMany(x => { x.RoutingRules.ForEach(r => @@ -104,7 +104,7 @@ public RoutableAgent[] GetRoutableAgents(List profiles) Disabled = false }; - var agents = db.GetAgents(filter); + var agents = db.GetAgents(filter).ConfigureAwait(false).GetAwaiter().GetResult(); var routableAgents = agents.Where(x => x.Type == AgentType.Task || x.Type == AgentType.Planning).Select(x => new RoutableAgent { AgentId = x.Id, diff --git a/src/Infrastructure/BotSharp.Core/Statistics/Services/BotSharpStatsService.cs b/src/Infrastructure/BotSharp.Core/Statistics/Services/BotSharpStatsService.cs index 8cbf974f8..018e3d2c1 100644 --- a/src/Infrastructure/BotSharp.Core/Statistics/Services/BotSharpStatsService.cs +++ b/src/Infrastructure/BotSharp.Core/Statistics/Services/BotSharpStatsService.cs @@ -19,7 +19,7 @@ public BotSharpStatsService( } - public bool UpdateStats(string @event, BotSharpStatsDelta delta) + public async Task UpdateStats(string @event, BotSharpStatsDelta delta) { try { @@ -31,7 +31,7 @@ public bool UpdateStats(string @event, BotSharpStatsDelta delta) } var db = _services.GetRequiredService(); - var isSaved = db.SaveGlobalStats(delta); + var isSaved = await db.SaveGlobalStats(delta); return isSaved; } catch (Exception ex) diff --git a/src/Infrastructure/BotSharp.Core/Tasks/Services/AgentTaskService.cs b/src/Infrastructure/BotSharp.Core/Tasks/Services/AgentTaskService.cs index c6ac878db..77a9cd553 100644 --- a/src/Infrastructure/BotSharp.Core/Tasks/Services/AgentTaskService.cs +++ b/src/Infrastructure/BotSharp.Core/Tasks/Services/AgentTaskService.cs @@ -40,7 +40,7 @@ public async Task> GetTasks(AgentTaskFilter filter) { var db = _services.GetRequiredService(); var pagedTasks = await db.GetAgentTasks(filter); - return await Task.FromResult(pagedTasks); + return pagedTasks; } } @@ -53,8 +53,8 @@ public async Task> GetTasks(AgentTaskFilter filter) public async Task GetTask(string agentId, string taskId) { var db = _services.GetRequiredService(); - var task = db.GetAgentTask(agentId, taskId); - return await Task.FromResult(task); + var task = await db.GetAgentTask(agentId, taskId); + return task; } /// @@ -65,8 +65,7 @@ public async Task> GetTasks(AgentTaskFilter filter) public async Task CreateTask(AgentTask task) { var db = _services.GetRequiredService(); - db.InsertAgentTask(task); - await Task.CompletedTask; + await db.InsertAgentTask(task); } /// @@ -78,8 +77,7 @@ public async Task CreateTask(AgentTask task) public async Task UpdateTask(AgentTask task, AgentTaskField field) { var db = _services.GetRequiredService(); - db.UpdateAgentTask(task, field); - await Task.CompletedTask; + await db.UpdateAgentTask(task, field); } /// @@ -91,7 +89,7 @@ public async Task UpdateTask(AgentTask task, AgentTaskField field) public async Task DeleteTask(string agentId, string taskId) { var db = _services.GetRequiredService(); - var isDeleted = db.DeleteAgentTasks(agentId, new List { taskId }); - return await Task.FromResult(isDeleted); + var isDeleted = await db.DeleteAgentTasks(agentId, new List { taskId }); + return isDeleted; } } diff --git a/src/Infrastructure/BotSharp.Core/Templating/ResponseTemplateService.cs b/src/Infrastructure/BotSharp.Core/Templating/ResponseTemplateService.cs index 463013bfb..8aecc9cd1 100644 --- a/src/Infrastructure/BotSharp.Core/Templating/ResponseTemplateService.cs +++ b/src/Infrastructure/BotSharp.Core/Templating/ResponseTemplateService.cs @@ -18,7 +18,7 @@ public async Task RenderFunctionResponse(string agentId, RoleDialogModel { // Find response template var db = _services.GetRequiredService(); - var responses = db.GetAgentResponses(agentId, "func", message.FunctionName); + var responses = await db.GetAgentResponses(agentId, "func", message.FunctionName); if (responses.Count == 0) { @@ -67,7 +67,7 @@ public async Task RenderIntentResponse(string agentId, RoleDialogModel m var db = _services.GetRequiredService(); var routing = _services.GetRequiredService(); - var responses = db.GetAgentResponses(agentId, "intent", routing.IntentName); + var responses = await db.GetAgentResponses(agentId, "intent", routing.IntentName); if (responses.Count == 0) { diff --git a/src/Infrastructure/BotSharp.Core/Translation/TranslationService.cs b/src/Infrastructure/BotSharp.Core/Translation/TranslationService.cs index 91cb9c4a6..9fde3ad0e 100644 --- a/src/Infrastructure/BotSharp.Core/Translation/TranslationService.cs +++ b/src/Infrastructure/BotSharp.Core/Translation/TranslationService.cs @@ -92,7 +92,7 @@ public async Task Translate(Agent router, string messageId, T data, string if (_convSettings.EnableTranslationMemory) { - var memories = _db.GetTranslationMemories(queries); + var memories = await _db.GetTranslationMemories(queries); var memoryHashes = memories.Select(x => x.HashText).ToList(); foreach (var memory in memories) @@ -149,7 +149,7 @@ public async Task Translate(Agent router, string messageId, T data, string if (_convSettings.EnableTranslationMemory) { - _db.SaveTranslationMemories(memoryInputs); + await _db.SaveTranslationMemories(memoryInputs); } } diff --git a/src/Infrastructure/BotSharp.Core/Users/Services/UserService.Token.cs b/src/Infrastructure/BotSharp.Core/Users/Services/UserService.Token.cs index fc3fc2d92..7aa485775 100644 --- a/src/Infrastructure/BotSharp.Core/Users/Services/UserService.Token.cs +++ b/src/Infrastructure/BotSharp.Core/Users/Services/UserService.Token.cs @@ -232,7 +232,7 @@ record = await db.GetUserByPhone(id, regionCode: (string.IsNullOrWhiteSpace(mode return token; } - public async Task CreateTokenByUser(User user) + public Task CreateTokenByUser(User user) { var accessToken = GenerateJwtToken(user); var jwt = new JwtSecurityTokenHandler().ReadJwtToken(accessToken); @@ -243,7 +243,7 @@ public async Task CreateTokenByUser(User user) TokenType = "Bearer", Scope = "api" }; - return token; + return Task.FromResult(token); } public async Task GetAffiliateToken(string authorization) diff --git a/src/Infrastructure/BotSharp.Core/Users/Services/UserService.cs b/src/Infrastructure/BotSharp.Core/Users/Services/UserService.cs index 56988d6e1..8f5e8cc67 100644 --- a/src/Infrastructure/BotSharp.Core/Users/Services/UserService.cs +++ b/src/Infrastructure/BotSharp.Core/Users/Services/UserService.cs @@ -218,7 +218,8 @@ public async Task GetUserAuthorizations(IEnumerable? if (user == null) return auth; auth.IsAdmin = isAdmin; - var role = db.GetRoles(new RoleFilter { Names = [user.Role] }).FirstOrDefault(); + var roles = await db.GetRoles(new RoleFilter { Names = [user.Role] }); + var role = roles.FirstOrDefault(); var permissions = user.Permissions?.Any() == true ? user.Permissions : role?.Permissions ?? []; auth.Permissions = permissions; @@ -235,7 +236,8 @@ public async Task GetUserAuthorizations(IEnumerable? }).ToList() ?? []; var userAgentIds = userAgents.Select(x => x.AgentId).ToList(); - var roleAgents = db.GetRoleDetails(role?.Id)?.AgentActions? + var roleDetails = role?.Id != null ? await db.GetRoleDetails(role.Id) : null; + var roleAgents = roleDetails?.AgentActions? .Where(x => !userAgentIds.Contains(x.AgentId))?.Select(x => new UserAgent { AgentId = x.AgentId, diff --git a/src/Infrastructure/BotSharp.Core/WebSearch/Functions/WebIntelligentSearchFn.cs b/src/Infrastructure/BotSharp.Core/WebSearch/Functions/WebIntelligentSearchFn.cs index 7cdeaa95e..18cf6542f 100644 --- a/src/Infrastructure/BotSharp.Core/WebSearch/Functions/WebIntelligentSearchFn.cs +++ b/src/Infrastructure/BotSharp.Core/WebSearch/Functions/WebIntelligentSearchFn.cs @@ -41,7 +41,7 @@ public async Task Execute(RoleDialogModel message) var dialogs = routingCtx.GetDialogs(); if (dialogs.IsNullOrEmpty()) { - dialogs = conv.GetDialogHistory(); + dialogs = await conv.GetDialogHistory(); } var response = await GetChatCompletion(agent, dialogs); diff --git a/src/Infrastructure/BotSharp.Logger/Hooks/CommonContentGeneratingHook.cs b/src/Infrastructure/BotSharp.Logger/Hooks/CommonContentGeneratingHook.cs index f30559046..421c459dc 100644 --- a/src/Infrastructure/BotSharp.Logger/Hooks/CommonContentGeneratingHook.cs +++ b/src/Infrastructure/BotSharp.Logger/Hooks/CommonContentGeneratingHook.cs @@ -11,11 +11,10 @@ public CommonContentGeneratingHook(IServiceProvider services) public async Task AfterGenerated(RoleDialogModel message, TokenStatsModel tokenStats) { - SaveLlmCompletionLog(message, tokenStats); - await Task.CompletedTask; + await SaveLlmCompletionLog(message, tokenStats); } - private void SaveLlmCompletionLog(RoleDialogModel message, TokenStatsModel tokenStats) + private async Task SaveLlmCompletionLog(RoleDialogModel message, TokenStatsModel tokenStats) { var convSettings = _services.GetRequiredService(); var conv = _services.GetRequiredService(); @@ -39,6 +38,6 @@ private void SaveLlmCompletionLog(RoleDialogModel message, TokenStatsModel token CreatedTime = DateTime.UtcNow }; - db.SaveLlmCompletionLog(completionLog); + await db.SaveLlmCompletionLog(completionLog); } } diff --git a/src/Infrastructure/BotSharp.Logger/Hooks/TokenStatsConversationHook.cs b/src/Infrastructure/BotSharp.Logger/Hooks/TokenStatsConversationHook.cs index c365eaafc..88de2583a 100644 --- a/src/Infrastructure/BotSharp.Logger/Hooks/TokenStatsConversationHook.cs +++ b/src/Infrastructure/BotSharp.Logger/Hooks/TokenStatsConversationHook.cs @@ -18,7 +18,6 @@ public async Task BeforeGenerating(Agent agent, List conversati public async Task AfterGenerated(RoleDialogModel message, TokenStatsModel tokenStats) { _tokenStatistics.StopTimer(); - _tokenStatistics.AddToken(tokenStats, message); - await Task.CompletedTask; + await _tokenStatistics.AddToken(tokenStats, message); } } diff --git a/src/Infrastructure/BotSharp.OpenAPI/Controllers/Conversation/ConversationController.File.cs b/src/Infrastructure/BotSharp.OpenAPI/Controllers/Conversation/ConversationController.File.cs index 0a259304b..3bf272c6f 100644 --- a/src/Infrastructure/BotSharp.OpenAPI/Controllers/Conversation/ConversationController.File.cs +++ b/src/Infrastructure/BotSharp.OpenAPI/Controllers/Conversation/ConversationController.File.cs @@ -66,7 +66,7 @@ public IActionResult UploadAttachments([FromRoute] string conversationId, IFormF public async Task UploadConversationMessageFiles([FromRoute] string agentId, [FromRoute] string conversationId, [FromBody] InputMessageFiles input) { var convService = _services.GetRequiredService(); - convService.SetConversationId(conversationId, input.States); + await convService.SetConversationId(conversationId, input.States); var conv = await convService.GetConversationRecordOrCreateNew(agentId); var fileStorage = _services.GetRequiredService(); var messageId = Guid.NewGuid().ToString(); diff --git a/src/Infrastructure/BotSharp.OpenAPI/Controllers/Conversation/ConversationController.cs b/src/Infrastructure/BotSharp.OpenAPI/Controllers/Conversation/ConversationController.cs index a0ca1b424..7ec5c3ae7 100644 --- a/src/Infrastructure/BotSharp.OpenAPI/Controllers/Conversation/ConversationController.cs +++ b/src/Infrastructure/BotSharp.OpenAPI/Controllers/Conversation/ConversationController.cs @@ -41,7 +41,7 @@ public async Task NewConversation([FromRoute] string agen TaskId = config.TaskId }; conv = await service.NewConversation(conv); - service.SetConversationId(conv.Id, config.States); + await service.SetConversationId(conv.Id, config.States); return ConversationViewModel.FromSession(conv); } @@ -87,8 +87,8 @@ public async Task> GetConversations([FromQuery public async Task> GetDialogs([FromRoute] string conversationId, [FromQuery] int count = 100) { var conv = _services.GetRequiredService(); - conv.SetConversationId(conversationId, [], isReadOnly: true); - var history = conv.GetDialogHistory(lastCount: count, fromBreakpoint: false); + await conv.SetConversationId(conversationId, [], isReadOnly: true); + var history = await conv.GetDialogHistory(lastCount: count, fromBreakpoint: false); var userService = _services.GetRequiredService(); var agentService = _services.GetRequiredService(); @@ -309,7 +309,7 @@ public async Task SendNotification([FromRoute] string convers var routing = _services.GetRequiredService(); var userService = _services.GetRequiredService(); - conv.SetConversationId(conversationId, new List(), isReadOnly: true); + await conv.SetConversationId(conversationId, new List(), isReadOnly: true); var inputMsg = new RoleDialogModel(AgentRole.User, input.Text) { @@ -360,7 +360,7 @@ public async Task SendMessage( var routing = _services.GetRequiredService(); routing.Context.SetMessageId(conversationId, inputMsg.MessageId); - conv.SetConversationId(conversationId, input.States); + await conv.SetConversationId(conversationId, input.States); SetStates(conv, input); var response = new ChatResponseModel(); @@ -406,7 +406,7 @@ public async Task SendMessageSse([FromRoute] string agentId, [FromRoute] string var routing = _services.GetRequiredService(); routing.Context.SetMessageId(conversationId, inputMsg.MessageId); - conv.SetConversationId(conversationId, input.States); + await conv.SetConversationId(conversationId, input.States); SetStates(conv, input); var response = new ChatResponseModel diff --git a/src/Infrastructure/BotSharp.OpenAPI/Controllers/Setting/PluginController.cs b/src/Infrastructure/BotSharp.OpenAPI/Controllers/Setting/PluginController.cs index b74cc68a8..bd1fe439a 100644 --- a/src/Infrastructure/BotSharp.OpenAPI/Controllers/Setting/PluginController.cs +++ b/src/Infrastructure/BotSharp.OpenAPI/Controllers/Setting/PluginController.cs @@ -16,7 +16,7 @@ public class PluginController(IServiceProvider services, IUserIdentity user, Plu public async Task> GetPlugins([FromQuery] PluginFilter filter) { var loader = services.GetRequiredService(); - return loader.GetPagedPlugins(services, filter); + return await loader.GetPagedPlugins(services, filter); } [HttpGet("/plugin/menu")] @@ -52,7 +52,8 @@ public async Task> GetPluginMenu() ]; var loader = services.GetRequiredService(); - foreach (var plugin in loader.GetPlugins(services)) + var plugins = await loader.GetPlugins(services); + foreach (var plugin in plugins) { if (!plugin.Enabled) { @@ -70,17 +71,17 @@ public async Task> GetPluginMenu() [BotSharpAuth] [HttpPost("/plugin/{id}/install")] - public PluginDef InstallPlugin([FromRoute] string id) + public async Task InstallPlugin([FromRoute] string id) { var loader = services.GetRequiredService(); - return loader.UpdatePluginStatus(services, id, true); + return await loader.UpdatePluginStatus(services, id, true); } [BotSharpAuth] [HttpPost("/plugin/{id}/remove")] - public PluginDef RemovePluginStats([FromRoute] string id) + public async Task RemovePluginStats([FromRoute] string id) { var loader = services.GetRequiredService(); - return loader.UpdatePluginStatus(services, id, false); + return await loader.UpdatePluginStatus(services, id, false); } } diff --git a/src/Infrastructure/BotSharp.OpenAPI/Controllers/Setting/SettingController.cs b/src/Infrastructure/BotSharp.OpenAPI/Controllers/Setting/SettingController.cs index cc15e5576..59fbec195 100644 --- a/src/Infrastructure/BotSharp.OpenAPI/Controllers/Setting/SettingController.cs +++ b/src/Infrastructure/BotSharp.OpenAPI/Controllers/Setting/SettingController.cs @@ -18,10 +18,10 @@ public SettingController(IServiceProvider services, } [HttpGet("/settings")] - public List GetSettings() + public async Task> GetSettings() { var pluginService = _services.GetRequiredService(); - var plugins = pluginService.GetPlugins(_services); + var plugins = await pluginService.GetPlugins(_services); return plugins.Where(x => x.Module.Settings != null && !string.IsNullOrEmpty(x.Module.Settings.Name)) .Select(x => x.Module.Settings.Name) @@ -30,8 +30,8 @@ public List GetSettings() } [HttpGet("/setting/{id}")] - public object GetSettingDetail([FromRoute] string id) + public async Task GetSettingDetail([FromRoute] string id) { - return _settingService.GetDetail(id, mask: true); + return await _settingService.GetDetail(id, mask: true); } } diff --git a/src/Plugins/BotSharp.Plugin.AudioHandler/Functions/ReadAudioFn.cs b/src/Plugins/BotSharp.Plugin.AudioHandler/Functions/ReadAudioFn.cs index 4dd1ada3d..244262ffb 100644 --- a/src/Plugins/BotSharp.Plugin.AudioHandler/Functions/ReadAudioFn.cs +++ b/src/Plugins/BotSharp.Plugin.AudioHandler/Functions/ReadAudioFn.cs @@ -43,7 +43,7 @@ public async Task Execute(RoleDialogModel message) var wholeDialogs = routingCtx.GetDialogs(); if (wholeDialogs.IsNullOrEmpty()) { - wholeDialogs = conv.GetDialogHistory(); + wholeDialogs = await conv.GetDialogHistory(); } var dialogs = AssembleFiles(conv.ConversationId, wholeDialogs); diff --git a/src/Plugins/BotSharp.Plugin.ChartHandler/Functions/PlotChartFn.cs b/src/Plugins/BotSharp.Plugin.ChartHandler/Functions/PlotChartFn.cs index d311aac61..9c423bc31 100644 --- a/src/Plugins/BotSharp.Plugin.ChartHandler/Functions/PlotChartFn.cs +++ b/src/Plugins/BotSharp.Plugin.ChartHandler/Functions/PlotChartFn.cs @@ -31,7 +31,7 @@ public async Task Execute(RoleDialogModel message) var args = JsonSerializer.Deserialize(message.FunctionArgs); var agent = await agentService.GetAgent(message.CurrentAgentId); - var inst = GetChartPlotInstruction(message.CurrentAgentId); + var inst = await GetChartPlotInstruction(message.CurrentAgentId); var innerAgent = new Agent { Id = agent?.Id ?? BuiltInAgentId.AIProgrammer, @@ -48,7 +48,7 @@ public async Task Execute(RoleDialogModel message) var dialogs = routingCtx.GetDialogs(); if (dialogs.IsNullOrEmpty()) { - dialogs = convService.GetDialogHistory(); + dialogs = await convService.GetDialogHistory(); } var messageLimit = _settings.ChartPlot?.MessageLimit > 0 ? _settings.ChartPlot.MessageLimit.Value : 50; @@ -109,7 +109,7 @@ private async Task GetChatCompletion(Agent agent, List } } - private string GetChartPlotInstruction(string agentId) + private async Task GetChartPlotInstruction(string agentId) { var db = _services.GetRequiredService(); var state = _services.GetRequiredService(); @@ -119,12 +119,12 @@ private string GetChartPlotInstruction(string agentId) if (!string.IsNullOrEmpty(templateName)) { - templateContent = db.GetAgentTemplate(agentId, templateName); + templateContent = await db.GetAgentTemplate(agentId, templateName); } else { templateName = "chart-js-generate_instruction"; - templateContent = db.GetAgentTemplate(BuiltInAgentId.AIProgrammer, templateName); + templateContent = await db.GetAgentTemplate(BuiltInAgentId.AIProgrammer, templateName); } return templateContent; diff --git a/src/Plugins/BotSharp.Plugin.ChatHub/ChatStreamMiddleware.cs b/src/Plugins/BotSharp.Plugin.ChatHub/ChatStreamMiddleware.cs index 1fa61cdcc..2f89252ba 100644 --- a/src/Plugins/BotSharp.Plugin.ChatHub/ChatStreamMiddleware.cs +++ b/src/Plugins/BotSharp.Plugin.ChatHub/ChatStreamMiddleware.cs @@ -66,7 +66,7 @@ private async Task HandleWebSocket(IServiceProvider services, string agentId, st // load conversation and state var convService = services.GetRequiredService(); var state = services.GetRequiredService(); - convService.SetConversationId(conversationId, []); + await convService.SetConversationId(conversationId, []); await convService.GetConversationRecordOrCreateNew(agentId); await foreach (ChatSessionUpdate update in session.ReceiveUpdatesAsync(CancellationToken.None)) @@ -99,7 +99,7 @@ private async Task HandleWebSocket(IServiceProvider services, string agentId, st } } - convService.SaveStates(); + await convService.SaveStates(); await session.DisconnectAsync(); } diff --git a/src/Plugins/BotSharp.Plugin.ChatHub/Hooks/StreamingLogHook.cs b/src/Plugins/BotSharp.Plugin.ChatHub/Hooks/StreamingLogHook.cs index 31e6056dd..63f037784 100644 --- a/src/Plugins/BotSharp.Plugin.ChatHub/Hooks/StreamingLogHook.cs +++ b/src/Plugins/BotSharp.Plugin.ChatHub/Hooks/StreamingLogHook.cs @@ -60,7 +60,7 @@ public override async Task OnMessageReceived(RoleDialogModel message) Source = ContentLogSource.UserInput, Log = log }; - await SendEvent(ChatEvent.OnConversationContentLogGenerated, conversationId, BuildContentLog(input)); + await SendEvent(ChatEvent.OnConversationContentLogGenerated, conversationId, await BuildContentLog(input)); } public override async Task OnPostbackMessageReceived(RoleDialogModel message, PostbackMessageModel replyMsg) @@ -78,7 +78,7 @@ public override async Task OnPostbackMessageReceived(RoleDialogModel message, Po Source = ContentLogSource.UserInput, Log = log }; - await SendEvent(ChatEvent.OnConversationContentLogGenerated, conversationId, BuildContentLog(input)); + await SendEvent(ChatEvent.OnConversationContentLogGenerated, conversationId, await BuildContentLog(input)); } public async Task OnSessionUpdated(Agent agent, string instruction, FunctionDef[] functions, bool isInit = false) @@ -107,7 +107,7 @@ public async Task OnSessionUpdated(Agent agent, string instruction, FunctionDef[ Source = ContentLogSource.Prompt, Log = log }; - await SendEvent(ChatEvent.OnConversationContentLogGenerated, conversationId, BuildContentLog(input)); + await SendEvent(ChatEvent.OnConversationContentLogGenerated, conversationId, await BuildContentLog(input)); } public async Task OnRenderingTemplate(Agent agent, string name, string content) @@ -129,7 +129,7 @@ public async Task OnRenderingTemplate(Agent agent, string name, string content) Source = ContentLogSource.HardRule, Log = log }; - await SendEvent(ChatEvent.OnConversationContentLogGenerated, conversationId, BuildContentLog(input)); + await SendEvent(ChatEvent.OnConversationContentLogGenerated, conversationId, await BuildContentLog(input)); } public async Task BeforeGenerating(Agent agent, List conversations) @@ -157,7 +157,7 @@ public override async Task OnFunctionExecuting(RoleDialogModel message, InvokeFu Source = ContentLogSource.FunctionCall, Log = log }; - await SendEvent(ChatEvent.OnConversationContentLogGenerated, conversationId, BuildContentLog(input)); + await SendEvent(ChatEvent.OnConversationContentLogGenerated, conversationId, await BuildContentLog(input)); } public override async Task OnFunctionExecuted(RoleDialogModel message, InvokeFunctionOptions? options = null) @@ -178,7 +178,7 @@ public override async Task OnFunctionExecuted(RoleDialogModel message, InvokeFun Source = ContentLogSource.FunctionCall, Log = log }; - await SendEvent(ChatEvent.OnConversationContentLogGenerated, conversationId, BuildContentLog(input)); + await SendEvent(ChatEvent.OnConversationContentLogGenerated, conversationId, await BuildContentLog(input)); } /// @@ -205,7 +205,7 @@ public async Task AfterGenerated(RoleDialogModel message, TokenStatsModel tokenS Source = ContentLogSource.Prompt, Log = tokenStats.Prompt }; - await SendEvent(ChatEvent.OnConversationContentLogGenerated, conversationId, BuildContentLog(input)); + await SendEvent(ChatEvent.OnConversationContentLogGenerated, conversationId, await BuildContentLog(input)); } /// @@ -220,7 +220,7 @@ public override async Task OnResponseGenerated(RoleDialogModel message) var conv = _services.GetRequiredService(); var routingCtx = _services.GetRequiredService(); - var stateLog = BuildStateLog(conv.ConversationId, routingCtx.EntryAgentId, _state.GetStates(), message); + var stateLog = await BuildStateLog(conv.ConversationId, routingCtx.EntryAgentId, _state.GetStates(), message); await SendEvent(ChatEvent.OnConversateStateLogGenerated, conv.ConversationId, stateLog); if (message.Role == AgentRole.Assistant) @@ -240,7 +240,7 @@ public override async Task OnResponseGenerated(RoleDialogModel message) Source = ContentLogSource.AgentResponse, Log = log }; - await SendEvent(ChatEvent.OnConversationContentLogGenerated, conversationId, BuildContentLog(input)); + await SendEvent(ChatEvent.OnConversationContentLogGenerated, conversationId, await BuildContentLog(input)); } } @@ -258,7 +258,7 @@ public override async Task OnTaskCompleted(RoleDialogModel message) Source = ContentLogSource.FunctionCall, Log = log }; - await SendEvent(ChatEvent.OnConversationContentLogGenerated, conversationId, BuildContentLog(input)); + await SendEvent(ChatEvent.OnConversationContentLogGenerated, conversationId, await BuildContentLog(input)); } public override async Task OnConversationEnding(RoleDialogModel message) @@ -275,7 +275,7 @@ public override async Task OnConversationEnding(RoleDialogModel message) Source = ContentLogSource.FunctionCall, Log = log }; - await SendEvent(ChatEvent.OnConversationContentLogGenerated, conversationId, BuildContentLog(input)); + await SendEvent(ChatEvent.OnConversationContentLogGenerated, conversationId, await BuildContentLog(input)); } public override async Task OnBreakpointUpdated(string conversationId, bool resetStates) @@ -303,7 +303,7 @@ public override async Task OnBreakpointUpdated(string conversationId, bool reset }, Log = log }; - await SendEvent(ChatEvent.OnConversationContentLogGenerated, conversationId, BuildContentLog(input)); + await SendEvent(ChatEvent.OnConversationContentLogGenerated, conversationId, await BuildContentLog(input)); } public override async Task OnStateChanged(StateChangeModel stateChange) @@ -342,7 +342,7 @@ public async Task OnAgentEnqueued(string agentId, string preAgentId, string? rea Source = ContentLogSource.HardRule, Log = log }; - await SendEvent(ChatEvent.OnConversationContentLogGenerated, conversationId, BuildContentLog(input)); + await SendEvent(ChatEvent.OnConversationContentLogGenerated, conversationId, await BuildContentLog(input)); } public async Task OnAgentDequeued(string agentId, string currentAgentId, string? reason = null) @@ -370,7 +370,7 @@ public async Task OnAgentDequeued(string agentId, string currentAgentId, string? Source = ContentLogSource.HardRule, Log = log }; - await SendEvent(ChatEvent.OnConversationContentLogGenerated, conversationId, BuildContentLog(input)); + await SendEvent(ChatEvent.OnConversationContentLogGenerated, conversationId, await BuildContentLog(input)); } public async Task OnAgentReplaced(string fromAgentId, string toAgentId, string? reason = null) @@ -398,7 +398,7 @@ public async Task OnAgentReplaced(string fromAgentId, string toAgentId, string? Source = ContentLogSource.HardRule, Log = log }; - await SendEvent(ChatEvent.OnConversationContentLogGenerated, conversationId, BuildContentLog(input)); + await SendEvent(ChatEvent.OnConversationContentLogGenerated, conversationId, await BuildContentLog(input)); } public async Task OnAgentQueueEmptied(string agentId, string? reason = null) @@ -423,7 +423,7 @@ public async Task OnAgentQueueEmptied(string agentId, string? reason = null) Source = ContentLogSource.HardRule, Log = log }; - await SendEvent(ChatEvent.OnConversationContentLogGenerated, conversationId, BuildContentLog(input)); + await SendEvent(ChatEvent.OnConversationContentLogGenerated, conversationId, await BuildContentLog(input)); } public async Task OnRoutingInstructionReceived(FunctionCallFromLlm instruct, RoleDialogModel message) @@ -442,7 +442,7 @@ public async Task OnRoutingInstructionReceived(FunctionCallFromLlm instruct, Rol Source = ContentLogSource.AgentResponse, Log = log }; - await SendEvent(ChatEvent.OnConversationContentLogGenerated, conversationId, BuildContentLog(input)); + await SendEvent(ChatEvent.OnConversationContentLogGenerated, conversationId, await BuildContentLog(input)); } public async Task OnRoutingInstructionRevised(FunctionCallFromLlm instruct, RoleDialogModel message) @@ -460,7 +460,7 @@ public async Task OnRoutingInstructionRevised(FunctionCallFromLlm instruct, Role Source = ContentLogSource.HardRule, Log = log }; - await SendEvent(ChatEvent.OnConversationContentLogGenerated, conversationId, BuildContentLog(input)); + await SendEvent(ChatEvent.OnConversationContentLogGenerated, conversationId, await BuildContentLog(input)); } #endregion @@ -473,7 +473,7 @@ private async Task SendEvent(string @event, string conversationId, T data, [C await EventEmitter.SendChatEvent(_services, _logger, @event, conversationId, user?.Id, json, nameof(StreamingLogHook), callerName); } - private ContentLogOutputModel BuildContentLog(ContentLogInputModel input) + private async Task BuildContentLog(ContentLogInputModel input) { var output = new ContentLogOutputModel { @@ -491,13 +491,13 @@ private ContentLogOutputModel BuildContentLog(ContentLogInputModel input) if (convSettings.EnableContentLog) { var db = _services.GetRequiredService(); - db.SaveConversationContentLog(output); + await db.SaveConversationContentLog(output); } return output; } - private ConversationStateLogModel BuildStateLog(string conversationId, string agentId, Dictionary states, RoleDialogModel message) + private async Task BuildStateLog(string conversationId, string agentId, Dictionary states, RoleDialogModel message) { var log = new ConversationStateLogModel { @@ -512,7 +512,7 @@ private ConversationStateLogModel BuildStateLog(string conversationId, string ag if (convSettings.EnableStateLog) { var db = _services.GetRequiredService(); - db.SaveConversationStateLog(log); + await db.SaveConversationStateLog(log); } return log; diff --git a/src/Plugins/BotSharp.Plugin.ChatHub/Hooks/WelcomeHook.cs b/src/Plugins/BotSharp.Plugin.ChatHub/Hooks/WelcomeHook.cs index 23bc4a1a3..3bf22d779 100644 --- a/src/Plugins/BotSharp.Plugin.ChatHub/Hooks/WelcomeHook.cs +++ b/src/Plugins/BotSharp.Plugin.ChatHub/Hooks/WelcomeHook.cs @@ -78,7 +78,7 @@ public override async Task OnUserAgentConnectedInitially(Conversation conversati }; await Task.Delay(300); - _storage.Append(conversation.Id, dialog); + await _storage.Append(conversation.Id, dialog); await SendEvent(ChatEvent.OnMessageReceivedFromAssistant, conversation.Id, data); } } diff --git a/src/Plugins/BotSharp.Plugin.ChatbotUI/ChatbotUiController.cs b/src/Plugins/BotSharp.Plugin.ChatbotUI/ChatbotUiController.cs index 601b62425..b8885360b 100644 --- a/src/Plugins/BotSharp.Plugin.ChatbotUI/ChatbotUiController.cs +++ b/src/Plugins/BotSharp.Plugin.ChatbotUI/ChatbotUiController.cs @@ -76,7 +76,7 @@ public async Task SendMessage([FromBody] OpenAiMessageInput input) var routing = _services.GetRequiredService(); routing.Context.SetMessageId(input.ConversationId, message.MessageId); - conv.SetConversationId(input.ConversationId, input.States); + await conv.SetConversationId(input.ConversationId, input.States); conv.States.SetState("channel", input.Channel) .SetState("provider", "azure-openai") .SetState("model", model) diff --git a/src/Plugins/BotSharp.Plugin.EmailHandler/Functions/HandleEmailReaderFn.cs b/src/Plugins/BotSharp.Plugin.EmailHandler/Functions/HandleEmailReaderFn.cs index 957bce442..d778db17c 100644 --- a/src/Plugins/BotSharp.Plugin.EmailHandler/Functions/HandleEmailReaderFn.cs +++ b/src/Plugins/BotSharp.Plugin.EmailHandler/Functions/HandleEmailReaderFn.cs @@ -71,7 +71,7 @@ public async Task Execute(RoleDialogModel message) var completion = CompletionProvider.GetChatCompletion(_services, provider: provider, model: model?.Name); var convService = _services.GetRequiredService(); var conversationId = convService.ConversationId; - var dialogs = convService.GetDialogHistory(fromBreakpoint: false); + var dialogs = await convService.GetDialogHistory(fromBreakpoint: false); var response = await completion.GetChatCompletions(agent, dialogs); var content = response?.Content ?? string.Empty; message.Content = content; diff --git a/src/Plugins/BotSharp.Plugin.ExcelHandler/Functions/ReadExcelFn.cs b/src/Plugins/BotSharp.Plugin.ExcelHandler/Functions/ReadExcelFn.cs index bb36b38c0..f0bd87755 100644 --- a/src/Plugins/BotSharp.Plugin.ExcelHandler/Functions/ReadExcelFn.cs +++ b/src/Plugins/BotSharp.Plugin.ExcelHandler/Functions/ReadExcelFn.cs @@ -41,7 +41,7 @@ public async Task Execute(RoleDialogModel message) var dialogs = routingCtx.GetDialogs(); if (dialogs.IsNullOrEmpty()) { - dialogs = conv.GetDialogHistory(); + dialogs = await conv.GetDialogHistory(); } var isExcelExist = AssembleFiles(conv.ConversationId, dialogs); diff --git a/src/Plugins/BotSharp.Plugin.FileHandler/Functions/ReadPdfFn.cs b/src/Plugins/BotSharp.Plugin.FileHandler/Functions/ReadPdfFn.cs index c0f9236ee..3edc4afb8 100644 --- a/src/Plugins/BotSharp.Plugin.FileHandler/Functions/ReadPdfFn.cs +++ b/src/Plugins/BotSharp.Plugin.FileHandler/Functions/ReadPdfFn.cs @@ -45,7 +45,7 @@ public async Task Execute(RoleDialogModel message) var wholeDialogs = routingCtx.GetDialogs(); if (wholeDialogs.IsNullOrEmpty()) { - wholeDialogs = conv.GetDialogHistory(); + wholeDialogs = await conv.GetDialogHistory(); } var dialogs = await AssembleFiles(conv.ConversationId, wholeDialogs); diff --git a/src/Plugins/BotSharp.Plugin.ImageHandler/Functions/ReadImageFn.cs b/src/Plugins/BotSharp.Plugin.ImageHandler/Functions/ReadImageFn.cs index c88f7420e..d2c4b0f3b 100644 --- a/src/Plugins/BotSharp.Plugin.ImageHandler/Functions/ReadImageFn.cs +++ b/src/Plugins/BotSharp.Plugin.ImageHandler/Functions/ReadImageFn.cs @@ -46,7 +46,7 @@ public async Task Execute(RoleDialogModel message) var wholeDialogs = routingCtx.GetDialogs(); if (wholeDialogs.IsNullOrEmpty()) { - wholeDialogs = conv.GetDialogHistory(); + wholeDialogs = await conv.GetDialogHistory(); } var dialogs = AssembleFiles(conv.ConversationId, args?.ImageUrls, wholeDialogs); diff --git a/src/Plugins/BotSharp.Plugin.KnowledgeBase/Services/KnowledgeService.Common.cs b/src/Plugins/BotSharp.Plugin.KnowledgeBase/Services/KnowledgeService.Common.cs index 7fc31f2d2..aa758d796 100644 --- a/src/Plugins/BotSharp.Plugin.KnowledgeBase/Services/KnowledgeService.Common.cs +++ b/src/Plugins/BotSharp.Plugin.KnowledgeBase/Services/KnowledgeService.Common.cs @@ -6,7 +6,7 @@ public async Task RefreshVectorKnowledgeConfigs(VectorCollectionConfigsMod { var db = _services.GetRequiredService(); var collections = configs.Collections ?? new(); - var saved = db.AddKnowledgeCollectionConfigs(collections, reset: true); - return await Task.FromResult(saved); + var saved = await db.AddKnowledgeCollectionConfigs(collections, reset: true); + return saved; } } diff --git a/src/Plugins/BotSharp.Plugin.KnowledgeBase/Services/KnowledgeService.Document.cs b/src/Plugins/BotSharp.Plugin.KnowledgeBase/Services/KnowledgeService.Document.cs index 613a2df8b..5f2222eb9 100644 --- a/src/Plugins/BotSharp.Plugin.KnowledgeBase/Services/KnowledgeService.Document.cs +++ b/src/Plugins/BotSharp.Plugin.KnowledgeBase/Services/KnowledgeService.Document.cs @@ -199,7 +199,7 @@ public async Task DeleteKnowledgeDocument(string collectionName, Guid file await vectorDb.DeleteCollectionData(collectionName, guids); } - db.DeleteKnolwedgeBaseFileMeta(collectionName, vectorStoreProvider, fileId); + await db.DeleteKnolwedgeBaseFileMeta(collectionName, vectorStoreProvider, fileId); return true; } catch (Exception ex) @@ -458,7 +458,7 @@ private async Task HandleKnowledgeFiles( if (!dataIds.IsNullOrEmpty()) { - db.SaveKnolwedgeBaseFileMeta(new KnowledgeDocMetaData + await db.SaveKnolwedgeBaseFileMeta(new KnowledgeDocMetaData { Collection = collectionName, FileId = item.FileId, diff --git a/src/Plugins/BotSharp.Plugin.KnowledgeBase/Services/KnowledgeService.Vector.cs b/src/Plugins/BotSharp.Plugin.KnowledgeBase/Services/KnowledgeService.Vector.cs index aab50869b..85f81a1e1 100644 --- a/src/Plugins/BotSharp.Plugin.KnowledgeBase/Services/KnowledgeService.Vector.cs +++ b/src/Plugins/BotSharp.Plugin.KnowledgeBase/Services/KnowledgeService.Vector.cs @@ -33,7 +33,7 @@ public async Task CreateVectorCollection(string collectionName, string col } var db = _services.GetRequiredService(); - var created = db.AddKnowledgeCollectionConfigs(new List + var created = await db.AddKnowledgeCollectionConfigs(new List { new VectorCollectionConfig { @@ -138,9 +138,9 @@ public async Task DeleteVectorCollection(string collectionName) var fileStorage = _services.GetRequiredService(); var vectorStoreProvider = _settings.VectorDb.Provider; - db.DeleteKnowledgeCollectionConfig(collectionName); + await db.DeleteKnowledgeCollectionConfig(collectionName); fileStorage.DeleteKnowledgeFile(collectionName, vectorStoreProvider); - db.DeleteKnolwedgeBaseFileMeta(collectionName, vectorStoreProvider); + await db.DeleteKnolwedgeBaseFileMeta(collectionName, vectorStoreProvider); } return deleted; diff --git a/src/Plugins/BotSharp.Plugin.MetaMessenger/Services/MessageHandleService.cs b/src/Plugins/BotSharp.Plugin.MetaMessenger/Services/MessageHandleService.cs index 5cb3fdc3c..b367640bb 100644 --- a/src/Plugins/BotSharp.Plugin.MetaMessenger/Services/MessageHandleService.cs +++ b/src/Plugins/BotSharp.Plugin.MetaMessenger/Services/MessageHandleService.cs @@ -59,7 +59,7 @@ await messenger.SendMessage(setting.ApiVersion, setting.PageId, var conv = _services.GetRequiredService(); var routing = _services.GetRequiredService(); routing.Context.SetMessageId(sender, inputMsg.MessageId); - conv.SetConversationId(sender, new List + await conv.SetConversationId(sender, new List { new MessageState("channel", ConversationChannel.Messenger) }); diff --git a/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.Agent.cs b/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.Agent.cs index 75737ddde..3c1e14f03 100644 --- a/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.Agent.cs +++ b/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.Agent.cs @@ -9,7 +9,7 @@ namespace BotSharp.Plugin.MongoStorage.Repository; public partial class MongoRepository { - public void UpdateAgent(Agent agent, AgentField field) + public async Task UpdateAgent(Agent agent, AgentField field) { if (agent == null || string.IsNullOrWhiteSpace(agent.Id)) { @@ -19,73 +19,73 @@ public void UpdateAgent(Agent agent, AgentField field) switch (field) { case AgentField.Name: - UpdateAgentName(agent.Id, agent.Name); + await UpdateAgentName(agent.Id, agent.Name); break; case AgentField.Description: - UpdateAgentDescription(agent.Id, agent.Description); + await UpdateAgentDescription(agent.Id, agent.Description); break; case AgentField.IsPublic: - UpdateAgentIsPublic(agent.Id, agent.IsPublic); + await UpdateAgentIsPublic(agent.Id, agent.IsPublic); break; case AgentField.Disabled: - UpdateAgentDisabled(agent.Id, agent.Disabled); + await UpdateAgentDisabled(agent.Id, agent.Disabled); break; case AgentField.Type: - UpdateAgentType(agent.Id, agent.Type); + await UpdateAgentType(agent.Id, agent.Type); break; case AgentField.RoutingMode: - UpdateAgentRoutingMode(agent.Id, agent.Mode); + await UpdateAgentRoutingMode(agent.Id, agent.Mode); break; case AgentField.FuncVisMode: - UpdateAgentFuncVisMode(agent.Id, agent.FuncVisMode); + await UpdateAgentFuncVisMode(agent.Id, agent.FuncVisMode); break; case AgentField.InheritAgentId: - UpdateAgentInheritAgentId(agent.Id, agent.InheritAgentId); + await UpdateAgentInheritAgentId(agent.Id, agent.InheritAgentId); break; case AgentField.Profile: - UpdateAgentProfiles(agent.Id, agent.Profiles); + await UpdateAgentProfiles(agent.Id, agent.Profiles); break; case AgentField.Label: - UpdateAgentLabels(agent.Id, agent.Labels); + await UpdateAgentLabels(agent.Id, agent.Labels); break; case AgentField.RoutingRule: - UpdateAgentRoutingRules(agent.Id, agent.RoutingRules); + await UpdateAgentRoutingRules(agent.Id, agent.RoutingRules); break; case AgentField.Instruction: - UpdateAgentInstructions(agent.Id, agent.Instruction, agent.ChannelInstructions); + await UpdateAgentInstructions(agent.Id, agent.Instruction, agent.ChannelInstructions); break; case AgentField.Function: - UpdateAgentFunctions(agent.Id, agent.Functions); + await UpdateAgentFunctions(agent.Id, agent.Functions); break; case AgentField.Template: - UpdateAgentTemplates(agent.Id, agent.Templates); + await UpdateAgentTemplates(agent.Id, agent.Templates); break; case AgentField.Response: - UpdateAgentResponses(agent.Id, agent.Responses); + await UpdateAgentResponses(agent.Id, agent.Responses); break; case AgentField.Sample: - UpdateAgentSamples(agent.Id, agent.Samples); + await UpdateAgentSamples(agent.Id, agent.Samples); break; case AgentField.LlmConfig: - UpdateAgentLlmConfig(agent.Id, agent.LlmConfig); + await UpdateAgentLlmConfig(agent.Id, agent.LlmConfig); break; case AgentField.Utility: - UpdateAgentUtilities(agent.Id, agent.MergeUtility, agent.Utilities); + await UpdateAgentUtilities(agent.Id, agent.MergeUtility, agent.Utilities); break; case AgentField.McpTool: - UpdateAgentMcpTools(agent.Id, agent.McpTools); + await UpdateAgentMcpTools(agent.Id, agent.McpTools); break; case AgentField.KnowledgeBase: - UpdateAgentKnowledgeBases(agent.Id, agent.KnowledgeBases); + await UpdateAgentKnowledgeBases(agent.Id, agent.KnowledgeBases); break; case AgentField.Rule: - UpdateAgentRules(agent.Id, agent.Rules); + await UpdateAgentRules(agent.Id, agent.Rules); break; case AgentField.MaxMessageCount: - UpdateAgentMaxMessageCount(agent.Id, agent.MaxMessageCount); + await UpdateAgentMaxMessageCount(agent.Id, agent.MaxMessageCount); break; case AgentField.All: - UpdateAgentAllFields(agent); + await UpdateAgentAllFields(agent); break; default: break; @@ -93,7 +93,7 @@ public void UpdateAgent(Agent agent, AgentField field) } #region Update Agent Fields - private void UpdateAgentName(string agentId, string name) + private async Task UpdateAgentName(string agentId, string name) { if (string.IsNullOrWhiteSpace(name)) { @@ -105,10 +105,10 @@ private void UpdateAgentName(string agentId, string name) .Set(x => x.Name, name) .Set(x => x.UpdatedTime, DateTime.UtcNow); - _dc.Agents.UpdateOne(filter, update); + await _dc.Agents.UpdateOneAsync(filter, update); } - private void UpdateAgentDescription(string agentId, string description) + private async Task UpdateAgentDescription(string agentId, string description) { if (string.IsNullOrWhiteSpace(description)) { @@ -120,70 +120,70 @@ private void UpdateAgentDescription(string agentId, string description) .Set(x => x.Description, description) .Set(x => x.UpdatedTime, DateTime.UtcNow); - _dc.Agents.UpdateOne(filter, update); + await _dc.Agents.UpdateOneAsync(filter, update); } - private void UpdateAgentIsPublic(string agentId, bool isPublic) + private async Task UpdateAgentIsPublic(string agentId, bool isPublic) { var filter = Builders.Filter.Eq(x => x.Id, agentId); var update = Builders.Update .Set(x => x.IsPublic, isPublic) .Set(x => x.UpdatedTime, DateTime.UtcNow); - _dc.Agents.UpdateOne(filter, update); + await _dc.Agents.UpdateOneAsync(filter, update); } - private void UpdateAgentDisabled(string agentId, bool disabled) + private async Task UpdateAgentDisabled(string agentId, bool disabled) { var filter = Builders.Filter.Eq(x => x.Id, agentId); var update = Builders.Update .Set(x => x.Disabled, disabled) .Set(x => x.UpdatedTime, DateTime.UtcNow); - _dc.Agents.UpdateOne(filter, update); + await _dc.Agents.UpdateOneAsync(filter, update); } - private void UpdateAgentType(string agentId, string type) + private async Task UpdateAgentType(string agentId, string type) { var filter = Builders.Filter.Eq(x => x.Id, agentId); var update = Builders.Update .Set(x => x.Type, type) .Set(x => x.UpdatedTime, DateTime.UtcNow); - _dc.Agents.UpdateOne(filter, update); + await _dc.Agents.UpdateOneAsync(filter, update); } - private void UpdateAgentRoutingMode(string agentId, string? mode) + private async Task UpdateAgentRoutingMode(string agentId, string? mode) { var filter = Builders.Filter.Eq(x => x.Id, agentId); var update = Builders.Update .Set(x => x.Mode, mode) .Set(x => x.UpdatedTime, DateTime.UtcNow); - _dc.Agents.UpdateOne(filter, update); + await _dc.Agents.UpdateOneAsync(filter, update); } - private void UpdateAgentFuncVisMode(string agentId, string? visMode) + private async Task UpdateAgentFuncVisMode(string agentId, string? visMode) { var filter = Builders.Filter.Eq(x => x.Id, agentId); var update = Builders.Update .Set(x => x.FunctionVisibilityMode, visMode) .Set(x => x.UpdatedTime, DateTime.UtcNow); - _dc.Agents.UpdateOne(filter, update); + await _dc.Agents.UpdateOneAsync(filter, update); } - private void UpdateAgentInheritAgentId(string agentId, string? inheritAgentId) + private async Task UpdateAgentInheritAgentId(string agentId, string? inheritAgentId) { var filter = Builders.Filter.Eq(x => x.Id, agentId); var update = Builders.Update .Set(x => x.InheritAgentId, inheritAgentId) .Set(x => x.UpdatedTime, DateTime.UtcNow); - _dc.Agents.UpdateOne(filter, update); + await _dc.Agents.UpdateOneAsync(filter, update); } - private void UpdateAgentProfiles(string agentId, List profiles) + private async Task UpdateAgentProfiles(string agentId, List profiles) { if (profiles == null) { @@ -195,10 +195,10 @@ private void UpdateAgentProfiles(string agentId, List profiles) .Set(x => x.Profiles, profiles) .Set(x => x.UpdatedTime, DateTime.UtcNow); - _dc.Agents.UpdateOne(filter, update); + await _dc.Agents.UpdateOneAsync(filter, update); } - public bool UpdateAgentLabels(string agentId, List labels) + public async Task UpdateAgentLabels(string agentId, List labels) { if (labels == null) { @@ -210,11 +210,11 @@ public bool UpdateAgentLabels(string agentId, List labels) .Set(x => x.Labels, labels) .Set(x => x.UpdatedTime, DateTime.UtcNow); - var result = _dc.Agents.UpdateOne(filter, update); + var result = await _dc.Agents.UpdateOneAsync(filter, update); return result.ModifiedCount > 0; } - private void UpdateAgentRoutingRules(string agentId, List rules) + private async Task UpdateAgentRoutingRules(string agentId, List rules) { if (rules == null) { @@ -227,10 +227,10 @@ private void UpdateAgentRoutingRules(string agentId, List rules) .Set(x => x.RoutingRules, ruleElements) .Set(x => x.UpdatedTime, DateTime.UtcNow); - _dc.Agents.UpdateOne(filter, update); + await _dc.Agents.UpdateOneAsync(filter, update); } - private void UpdateAgentInstructions(string agentId, string instruction, List? channelInstructions) + private async Task UpdateAgentInstructions(string agentId, string instruction, List? channelInstructions) { var instructionElements = channelInstructions?.Select(x => ChannelInstructionMongoElement.ToMongoElement(x))? .ToList() ?? []; @@ -241,10 +241,10 @@ private void UpdateAgentInstructions(string agentId, string instruction, List x.ChannelInstructions, instructionElements) .Set(x => x.UpdatedTime, DateTime.UtcNow); - _dc.Agents.UpdateOne(filter, update); + await _dc.Agents.UpdateOneAsync(filter, update); } - private void UpdateAgentFunctions(string agentId, List functions) + private async Task UpdateAgentFunctions(string agentId, List functions) { if (functions == null) { @@ -257,10 +257,10 @@ private void UpdateAgentFunctions(string agentId, List functions) .Set(x => x.Functions, functionsToUpdate) .Set(x => x.UpdatedTime, DateTime.UtcNow); - _dc.Agents.UpdateOne(filter, update); + await _dc.Agents.UpdateOneAsync(filter, update); } - private void UpdateAgentTemplates(string agentId, List templates) + private async Task UpdateAgentTemplates(string agentId, List templates) { if (templates == null) { @@ -273,10 +273,10 @@ private void UpdateAgentTemplates(string agentId, List templates) .Set(x => x.Templates, templatesToUpdate) .Set(x => x.UpdatedTime, DateTime.UtcNow); - _dc.Agents.UpdateOne(filter, update); + await _dc.Agents.UpdateOneAsync(filter, update); } - private void UpdateAgentResponses(string agentId, List responses) + private async Task UpdateAgentResponses(string agentId, List responses) { if (responses == null || string.IsNullOrWhiteSpace(agentId)) { @@ -289,10 +289,10 @@ private void UpdateAgentResponses(string agentId, List responses) .Set(x => x.Responses, responsesToUpdate) .Set(x => x.UpdatedTime, DateTime.UtcNow); - _dc.Agents.UpdateOne(filter, update); + await _dc.Agents.UpdateOneAsync(filter, update); } - private void UpdateAgentSamples(string agentId, List samples) + private async Task UpdateAgentSamples(string agentId, List samples) { if (samples == null) { @@ -304,10 +304,10 @@ private void UpdateAgentSamples(string agentId, List samples) .Set(x => x.Samples, samples) .Set(x => x.UpdatedTime, DateTime.UtcNow); - _dc.Agents.UpdateOne(filter, update); + await _dc.Agents.UpdateOneAsync(filter, update); } - private void UpdateAgentUtilities(string agentId, bool mergeUtility, List utilities) + private async Task UpdateAgentUtilities(string agentId, bool mergeUtility, List utilities) { if (utilities == null) { @@ -322,10 +322,10 @@ private void UpdateAgentUtilities(string agentId, bool mergeUtility, List x.Utilities, elements) .Set(x => x.UpdatedTime, DateTime.UtcNow); - _dc.Agents.UpdateOne(filter, update); + await _dc.Agents.UpdateOneAsync(filter, update); } - private void UpdateAgentMcpTools(string agentId, List mcps) + private async Task UpdateAgentMcpTools(string agentId, List mcps) { if (mcps == null) { @@ -339,9 +339,9 @@ private void UpdateAgentMcpTools(string agentId, List mcps) .Set(x => x.McpTools, elements) .Set(x => x.UpdatedTime, DateTime.UtcNow); - _dc.Agents.UpdateOne(filter, update); + await _dc.Agents.UpdateOneAsync(filter, update); } - private void UpdateAgentKnowledgeBases(string agentId, List knowledgeBases) + private async Task UpdateAgentKnowledgeBases(string agentId, List knowledgeBases) { if (knowledgeBases == null) { @@ -355,10 +355,10 @@ private void UpdateAgentKnowledgeBases(string agentId, List .Set(x => x.KnowledgeBases, elements) .Set(x => x.UpdatedTime, DateTime.UtcNow); - _dc.Agents.UpdateOne(filter, update); + await _dc.Agents.UpdateOneAsync(filter, update); } - private void UpdateAgentRules(string agentId, List rules) + private async Task UpdateAgentRules(string agentId, List rules) { if (rules == null) { @@ -372,10 +372,10 @@ private void UpdateAgentRules(string agentId, List rules) .Set(x => x.Rules, elements) .Set(x => x.UpdatedTime, DateTime.UtcNow); - _dc.Agents.UpdateOne(filter, update); + await _dc.Agents.UpdateOneAsync(filter, update); } - private void UpdateAgentLlmConfig(string agentId, AgentLlmConfig? config) + private async Task UpdateAgentLlmConfig(string agentId, AgentLlmConfig? config) { var llmConfig = AgentLlmConfigMongoModel.ToMongoElement(config); var filter = Builders.Filter.Eq(x => x.Id, agentId); @@ -383,20 +383,20 @@ private void UpdateAgentLlmConfig(string agentId, AgentLlmConfig? config) .Set(x => x.LlmConfig, llmConfig) .Set(x => x.UpdatedTime, DateTime.UtcNow); - _dc.Agents.UpdateOne(filter, update); + await _dc.Agents.UpdateOneAsync(filter, update); } - private void UpdateAgentMaxMessageCount(string agentId, int? maxMessageCount) + private async Task UpdateAgentMaxMessageCount(string agentId, int? maxMessageCount) { var filter = Builders.Filter.Eq(x => x.Id, agentId); var update = Builders.Update .Set(x => x.MaxMessageCount, maxMessageCount) .Set(x => x.UpdatedTime, DateTime.UtcNow); - _dc.Agents.UpdateOne(filter, update); + await _dc.Agents.UpdateOneAsync(filter, update); } - private void UpdateAgentAllFields(Agent agent) + private async Task UpdateAgentAllFields(Agent agent) { var filter = Builders.Filter.Eq(x => x.Id, agent.Id); var update = Builders.Update @@ -425,7 +425,7 @@ private void UpdateAgentAllFields(Agent agent) .Set(x => x.IsPublic, agent.IsPublic) .Set(x => x.UpdatedTime, DateTime.UtcNow); - var res = _dc.Agents.UpdateOne(filter, update); + var res = await _dc.Agents.UpdateOneAsync(filter, update); } #endregion @@ -452,7 +452,7 @@ private void UpdateAgentAllFields(Agent agent) return TransformAgentDocument(agent); } - public List GetAgents(AgentFilter filter) + public async Task> GetAgents(AgentFilter filter) { if (filter == null) { @@ -498,16 +498,16 @@ public List GetAgents(AgentFilter filter) filters.Add(builder.Eq(x => x.IsPublic, filter.IsPublic.Value)); } - var agentDocs = _dc.Agents.Find(builder.And(filters)).ToList(); + var agentDocs = await _dc.Agents.Find(builder.And(filters)).ToListAsync(); return agentDocs.Select(x => TransformAgentDocument(x)).ToList(); } - public List GetUserAgents(string userId) + public async Task> GetUserAgents(string userId) { - var found = (from ua in _dc.UserAgents.AsQueryable() - join u in _dc.Users.AsQueryable() on ua.UserId equals u.Id - where ua.UserId == userId || u.ExternalId == userId - select ua).ToList(); + var found = await (from ua in _dc.UserAgents.AsQueryable() + join u in _dc.Users.AsQueryable() on ua.UserId equals u.Id + where ua.UserId == userId || u.ExternalId == userId + select ua).ToListAsync(); if (found.IsNullOrEmpty()) { @@ -525,7 +525,7 @@ join u in _dc.Users.AsQueryable() on ua.UserId equals u.Id }).ToList(); var agentIds = found.Select(x => x.AgentId).Distinct().ToList(); - var agents = GetAgents(new AgentFilter { AgentIds = agentIds }); + var agents = await GetAgents(new AgentFilter { AgentIds = agentIds }); foreach (var item in res) { var agent = agents.FirstOrDefault(x => x.Id == item.AgentId); @@ -540,21 +540,23 @@ join u in _dc.Users.AsQueryable() on ua.UserId equals u.Id return res; } - public List GetAgentResponses(string agentId, string prefix, string intent) + public async Task> GetAgentResponses(string agentId, string prefix, string intent) { var responses = new List(); - var agent = _dc.Agents.AsQueryable().FirstOrDefault(x => x.Id == agentId); + var filter = Builders.Filter.Eq(x => x.Id, agentId); + var agent = await _dc.Agents.Find(filter).FirstOrDefaultAsync(); if (agent == null) { return responses; } - return agent.Responses.Where(x => x.Prefix == prefix && x.Intent == intent).Select(x => x.Content).ToList(); + return agent.Responses?.Where(x => x.Prefix == prefix && x.Intent == intent).Select(x => x.Content).ToList() ?? []; } - public string GetAgentTemplate(string agentId, string templateName) + public async Task GetAgentTemplate(string agentId, string templateName) { - var agent = _dc.Agents.AsQueryable().FirstOrDefault(x => x.Id == agentId); + var filter = Builders.Filter.Eq(x => x.Id, agentId); + var agent = await _dc.Agents.Find(filter).FirstOrDefaultAsync(); if (agent == null) { return string.Empty; @@ -563,7 +565,7 @@ public string GetAgentTemplate(string agentId, string templateName) return agent.Templates?.FirstOrDefault(x => x.Name.IsEqualTo(templateName))?.Content ?? string.Empty; } - public bool PatchAgentTemplate(string agentId, AgentTemplate template) + public async Task PatchAgentTemplate(string agentId, AgentTemplate template) { if (string.IsNullOrEmpty(agentId) || template == null) { @@ -571,7 +573,7 @@ public bool PatchAgentTemplate(string agentId, AgentTemplate template) } var filter = Builders.Filter.Eq(x => x.Id, agentId); - var agent = _dc.Agents.Find(filter).FirstOrDefault(); + var agent = await _dc.Agents.Find(filter).FirstOrDefaultAsync(); if (agent == null || agent.Templates.IsNullOrEmpty()) { return false; @@ -585,11 +587,11 @@ public bool PatchAgentTemplate(string agentId, AgentTemplate template) foundTemplate.Content = template.Content; var update = Builders.Update.Set(x => x.Templates, agent.Templates); - _dc.Agents.UpdateOne(filter, update); + await _dc.Agents.UpdateOneAsync(filter, update); return true; } - public bool AppendAgentLabels(string agentId, List labels) + public async Task AppendAgentLabels(string agentId, List labels) { if (labels.IsNullOrEmpty()) { @@ -597,7 +599,7 @@ public bool AppendAgentLabels(string agentId, List labels) } var filter = Builders.Filter.Eq(x => x.Id, agentId); - var agent = _dc.Agents.Find(filter).FirstOrDefault(); + var agent = await _dc.Agents.Find(filter).FirstOrDefaultAsync(); if (agent == null) { return false; @@ -609,11 +611,11 @@ public bool AppendAgentLabels(string agentId, List labels) .Set(x => x.Labels, curLabels) .Set(x => x.UpdatedTime, DateTime.UtcNow); - var result = _dc.Agents.UpdateOne(filter, update); + var result = await _dc.Agents.UpdateOneAsync(filter, update); return result.ModifiedCount > 0; } - public void BulkInsertAgents(List agents) + public async Task BulkInsertAgents(List agents) { if (agents.IsNullOrEmpty()) { @@ -652,10 +654,10 @@ public void BulkInsertAgents(List agents) UpdatedTime = x.UpdatedDateTime }).ToList(); - _dc.Agents.InsertMany(agentDocs); + await _dc.Agents.InsertManyAsync(agentDocs); } - public void BulkInsertUserAgents(List userAgents) + public async Task BulkInsertUserAgents(List userAgents) { if (userAgents.IsNullOrEmpty()) { @@ -678,18 +680,18 @@ public void BulkInsertUserAgents(List userAgents) UpdatedTime = x.UpdatedTime }).ToList(); - _dc.UserAgents.InsertMany(userAgentDocs); + await _dc.UserAgents.InsertManyAsync(userAgentDocs); } - public bool DeleteAgents() + public async Task DeleteAgents() { try { - _dc.UserAgents.DeleteMany(Builders.Filter.Empty); - _dc.RoleAgents.DeleteMany(Builders.Filter.Empty); - _dc.AgentTasks.DeleteMany(Builders.Filter.Empty); - _dc.AgentCodeScripts.DeleteMany(Builders.Filter.Empty); - _dc.Agents.DeleteMany(Builders.Filter.Empty); + await _dc.UserAgents.DeleteManyAsync(Builders.Filter.Empty); + await _dc.RoleAgents.DeleteManyAsync(Builders.Filter.Empty); + await _dc.AgentTasks.DeleteManyAsync(Builders.Filter.Empty); + await _dc.AgentCodeScripts.DeleteManyAsync(Builders.Filter.Empty); + await _dc.Agents.DeleteManyAsync(Builders.Filter.Empty); return true; } catch @@ -698,7 +700,7 @@ public bool DeleteAgents() } } - public bool DeleteAgent(string agentId, AgentDeleteOptions? options = null) + public async Task DeleteAgent(string agentId, AgentDeleteOptions? options = null) { try { @@ -710,22 +712,22 @@ public bool DeleteAgent(string agentId, AgentDeleteOptions? options = null) if (options == null || options.DeleteUserAgents) { var userAgentFilter = Builders.Filter.Eq(x => x.AgentId, agentId); - _dc.UserAgents.DeleteMany(userAgentFilter); + await _dc.UserAgents.DeleteManyAsync(userAgentFilter); } if (options == null || options.DeleteRoleAgents) { var roleAgentFilter = Builders.Filter.Eq(x => x.AgentId, agentId); - _dc.RoleAgents.DeleteMany(roleAgentFilter); + await _dc.RoleAgents.DeleteManyAsync(roleAgentFilter); } - DeleteAgentCodeScripts(agentId, options?.ToDeleteCodeScripts); + await DeleteAgentCodeScripts(agentId, options?.ToDeleteCodeScripts); var agentTaskFilter = Builders.Filter.Eq(x => x.AgentId, agentId); - _dc.AgentTasks.DeleteMany(agentTaskFilter); + await _dc.AgentTasks.DeleteManyAsync(agentTaskFilter); var agentFilter = Builders.Filter.Eq(x => x.Id, agentId); - _dc.Agents.DeleteOne(agentFilter); + await _dc.Agents.DeleteOneAsync(agentFilter); return true; } catch diff --git a/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.AgentCodeScript.cs b/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.AgentCodeScript.cs index ae02f91fe..b044da782 100644 --- a/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.AgentCodeScript.cs +++ b/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.AgentCodeScript.cs @@ -7,7 +7,7 @@ namespace BotSharp.Plugin.MongoStorage.Repository; public partial class MongoRepository { #region Code script - public List GetAgentCodeScripts(string agentId, AgentCodeScriptFilter? filter = null) + public async Task> GetAgentCodeScripts(string agentId, AgentCodeScriptFilter? filter = null) { if (string.IsNullOrWhiteSpace(agentId)) { @@ -31,7 +31,7 @@ public List GetAgentCodeScripts(string agentId, AgentCodeScript filters.Add(builder.In(x => x.ScriptType, filter.ScriptTypes)); } - var found = _dc.AgentCodeScripts.Find(builder.And(filters)).ToList(); + var found = await _dc.AgentCodeScripts.Find(builder.And(filters)).ToListAsync(); return found.Select(x => AgentCodeScriptDocument.ToDomainModel(x)).ToList(); } @@ -56,7 +56,7 @@ public List GetAgentCodeScripts(string agentId, AgentCodeScript return found != null ? AgentCodeScriptDocument.ToDomainModel(found) : null; } - public bool UpdateAgentCodeScripts(string agentId, List scripts, AgentCodeScriptDbUpdateOptions? options = null) + public async Task UpdateAgentCodeScripts(string agentId, List scripts, AgentCodeScriptDbUpdateOptions? options = null) { if (string.IsNullOrWhiteSpace(agentId) || scripts.IsNullOrEmpty()) { @@ -88,11 +88,11 @@ public bool UpdateAgentCodeScripts(string agentId, List scripts }) .ToList(); - var result = _dc.AgentCodeScripts.BulkWrite(ops, new BulkWriteOptions { IsOrdered = false }); + await _dc.AgentCodeScripts.BulkWriteAsync(ops, new BulkWriteOptions { IsOrdered = false }); return true; } - public bool BulkInsertAgentCodeScripts(string agentId, List scripts) + public async Task BulkInsertAgentCodeScripts(string agentId, List scripts) { if (string.IsNullOrWhiteSpace(agentId) || scripts.IsNullOrEmpty()) { @@ -109,11 +109,11 @@ public bool BulkInsertAgentCodeScripts(string agentId, List scr return script; }).ToList(); - _dc.AgentCodeScripts.InsertMany(docs); + await _dc.AgentCodeScripts.InsertManyAsync(docs); return true; } - public bool DeleteAgentCodeScripts(string agentId, List? scripts = null) + public async Task DeleteAgentCodeScripts(string agentId, List? scripts = null) { if (string.IsNullOrWhiteSpace(agentId)) { @@ -136,11 +136,11 @@ public bool DeleteAgentCodeScripts(string agentId, List? script builder.Eq(x => x.AgentId, agentId), new BsonDocumentFilterDefinition(exprFilter) ); - deleted = _dc.AgentCodeScripts.DeleteMany(filterDef); + deleted = await _dc.AgentCodeScripts.DeleteManyAsync(filterDef); } else { - deleted = _dc.AgentCodeScripts.DeleteMany(builder.Eq(x => x.AgentId, agentId)); + deleted = await _dc.AgentCodeScripts.DeleteManyAsync(builder.Eq(x => x.AgentId, agentId)); } return deleted.DeletedCount > 0; diff --git a/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.AgentTask.cs b/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.AgentTask.cs index f4f4fb656..a222b5915 100644 --- a/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.AgentTask.cs +++ b/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.AgentTask.cs @@ -1,5 +1,6 @@ using BotSharp.Abstraction.Repositories.Filters; using BotSharp.Abstraction.Tasks.Models; +using MongoDB.Driver.Linq; namespace BotSharp.Plugin.MongoStorage.Repository; @@ -48,7 +49,7 @@ public async ValueTask> GetAgentTasks(AgentTaskFilter filt var count = countTask.Result; var agentIds = docs.Select(x => x.AgentId).Distinct().ToList(); - var agents = GetAgents(new AgentFilter { AgentIds = agentIds }); + var agents = await GetAgents(new AgentFilter { AgentIds = agentIds }); var tasks = docs.Select(x => { @@ -64,20 +65,22 @@ public async ValueTask> GetAgentTasks(AgentTaskFilter filt }; } - public AgentTask? GetAgentTask(string agentId, string taskId) + public async Task GetAgentTask(string agentId, string taskId) { if (string.IsNullOrEmpty(taskId)) { return null; } - var taskDoc = _dc.AgentTasks.AsQueryable().FirstOrDefault(x => x.Id == taskId); + var taskFilter = Builders.Filter.Eq(x => x.Id, taskId); + var taskDoc = await _dc.AgentTasks.Find(taskFilter).FirstOrDefaultAsync(); if (taskDoc == null) { return null; } - var agentDoc = _dc.Agents.AsQueryable().FirstOrDefault(x => x.Id == taskDoc.AgentId); + var agentFilter = Builders.Filter.Eq(x => x.Id, taskDoc.AgentId); + var agentDoc = await _dc.Agents.Find(agentFilter).FirstOrDefaultAsync(); var agent = TransformAgentDocument(agentDoc); var task = AgentTaskDocument.ToDomainModel(taskDoc); @@ -85,14 +88,14 @@ public async ValueTask> GetAgentTasks(AgentTaskFilter filt return task; } - public void InsertAgentTask(AgentTask task) + public async Task InsertAgentTask(AgentTask task) { var taskDoc = AgentTaskDocument.ToMongoModel(task); taskDoc.Id = Guid.NewGuid().ToString(); - _dc.AgentTasks.InsertOne(taskDoc); + await _dc.AgentTasks.InsertOneAsync(taskDoc); } - public void BulkInsertAgentTasks(string agentId, List tasks) + public async Task BulkInsertAgentTasks(string agentId, List tasks) { if (string.IsNullOrWhiteSpace(agentId) || tasks.IsNullOrEmpty()) { @@ -107,10 +110,10 @@ public void BulkInsertAgentTasks(string agentId, List tasks) return task; }).ToList(); - _dc.AgentTasks.InsertMany(taskDocs); + await _dc.AgentTasks.InsertManyAsync(taskDocs); } - public void UpdateAgentTask(AgentTask task, AgentTaskField field) + public async Task UpdateAgentTask(AgentTask task, AgentTaskField field) { if (task == null || string.IsNullOrEmpty(task.Id)) { @@ -118,7 +121,7 @@ public void UpdateAgentTask(AgentTask task, AgentTaskField field) } var filter = Builders.Filter.Eq(x => x.Id, task.Id); - var taskDoc = _dc.AgentTasks.Find(filter).FirstOrDefault(); + var taskDoc = await _dc.AgentTasks.Find(filter).FirstOrDefaultAsync(); if (taskDoc == null) { return; @@ -151,10 +154,10 @@ public void UpdateAgentTask(AgentTask task, AgentTaskField field) } taskDoc.UpdatedTime = DateTime.UtcNow; - _dc.AgentTasks.ReplaceOne(filter, taskDoc); + await _dc.AgentTasks.ReplaceOneAsync(filter, taskDoc); } - public bool DeleteAgentTasks(string agentId, List? taskIds = null) + public async Task DeleteAgentTasks(string agentId, List? taskIds = null) { var filterDef = Builders.Filter.Empty; if (taskIds != null) @@ -167,7 +170,7 @@ public bool DeleteAgentTasks(string agentId, List? taskIds = null) filterDef = builder.And(filters); } - var taskDeleted = _dc.AgentTasks.DeleteMany(filterDef); + var taskDeleted = await _dc.AgentTasks.DeleteManyAsync(filterDef); return taskDeleted.DeletedCount > 0; } #endregion diff --git a/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.Conversation.cs b/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.Conversation.cs index e83d7b51e..ae3383311 100644 --- a/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.Conversation.cs +++ b/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.Conversation.cs @@ -1,5 +1,6 @@ using BotSharp.Abstraction.Conversations.Models; using BotSharp.Abstraction.Repositories.Filters; +using MongoDB.Driver.Linq; using System.Text.Json; namespace BotSharp.Plugin.MongoStorage.Repository; @@ -62,7 +63,7 @@ public async Task CreateNewConversation(Conversation conversation) } } - public bool DeleteConversations(IEnumerable conversationIds) + public async Task DeleteConversations(IEnumerable conversationIds) { if (conversationIds.IsNullOrEmpty()) return false; @@ -74,13 +75,13 @@ public bool DeleteConversations(IEnumerable conversationIds) var filterStateLog = Builders.Filter.In(x => x.ConversationId, conversationIds); var conbTabItems = Builders.Filter.In(x => x.ConversationId, conversationIds); - var promptLogDeleted = _dc.LlmCompletionLogs.DeleteMany(filterPromptLog); - var contentLogDeleted = _dc.ContentLogs.DeleteMany(filterContentLog); - var stateLogDeleted = _dc.StateLogs.DeleteMany(filterStateLog); - var statesDeleted = _dc.ConversationStates.DeleteMany(filterSates); - var dialogDeleted = _dc.ConversationDialogs.DeleteMany(filterDialog); - var cronDeleted = _dc.CrontabItems.DeleteMany(conbTabItems); - var convDeleted = _dc.Conversations.DeleteMany(filterConv); + var promptLogDeleted = await _dc.LlmCompletionLogs.DeleteManyAsync(filterPromptLog); + var contentLogDeleted = await _dc.ContentLogs.DeleteManyAsync(filterContentLog); + var stateLogDeleted = await _dc.StateLogs.DeleteManyAsync(filterStateLog); + var statesDeleted = await _dc.ConversationStates.DeleteManyAsync(filterSates); + var dialogDeleted = await _dc.ConversationDialogs.DeleteManyAsync(filterDialog); + var cronDeleted = await _dc.CrontabItems.DeleteManyAsync(conbTabItems); + var convDeleted = await _dc.Conversations.DeleteManyAsync(filterConv); return convDeleted.DeletedCount > 0 || dialogDeleted.DeletedCount > 0 || statesDeleted.DeletedCount > 0 || promptLogDeleted.DeletedCount > 0 || contentLogDeleted.DeletedCount > 0 @@ -88,13 +89,13 @@ public bool DeleteConversations(IEnumerable conversationIds) } [SideCar] - public List GetConversationDialogs(string conversationId) + public async Task> GetConversationDialogs(string conversationId) { var dialogs = new List(); if (string.IsNullOrEmpty(conversationId)) return dialogs; var filter = Builders.Filter.Eq(x => x.ConversationId, conversationId); - var foundDialog = _dc.ConversationDialogs.Find(filter).FirstOrDefault(); + var foundDialog = await _dc.ConversationDialogs.Find(filter).FirstOrDefaultAsync(); if (foundDialog == null) return dialogs; var formattedDialog = foundDialog.Dialogs?.Select(x => DialogMongoElement.ToDomainElement(x))?.ToList(); @@ -102,7 +103,7 @@ public List GetConversationDialogs(string conversationId) } [SideCar] - public void AppendConversationDialogs(string conversationId, List dialogs) + public async Task AppendConversationDialogs(string conversationId, List dialogs) { if (string.IsNullOrEmpty(conversationId)) return; @@ -114,11 +115,11 @@ public void AppendConversationDialogs(string conversationId, List var updateConv = Builders.Update.Set(x => x.UpdatedTime, DateTime.UtcNow) .Inc(x => x.DialogCount, dialogs.Count); - _dc.ConversationDialogs.UpdateOne(filterDialog, updateDialog); - _dc.Conversations.UpdateOne(filterConv, updateConv); + await _dc.ConversationDialogs.UpdateOneAsync(filterDialog, updateDialog); + await _dc.Conversations.UpdateOneAsync(filterConv, updateConv); } - public void UpdateConversationTitle(string conversationId, string title) + public async Task UpdateConversationTitle(string conversationId, string title) { if (string.IsNullOrEmpty(conversationId)) return; @@ -127,9 +128,9 @@ public void UpdateConversationTitle(string conversationId, string title) .Set(x => x.UpdatedTime, DateTime.UtcNow) .Set(x => x.Title, title); - _dc.Conversations.UpdateOne(filterConv, updateConv); + await _dc.Conversations.UpdateOneAsync(filterConv, updateConv); } - public void UpdateConversationTitleAlias(string conversationId, string titleAlias) + public async Task UpdateConversationTitleAlias(string conversationId, string titleAlias) { if (string.IsNullOrEmpty(conversationId)) return; @@ -138,15 +139,15 @@ public void UpdateConversationTitleAlias(string conversationId, string titleAlia .Set(x => x.UpdatedTime, DateTime.UtcNow) .Set(x => x.TitleAlias, titleAlias); - _dc.Conversations.UpdateOne(filterConv, updateConv); + await _dc.Conversations.UpdateOneAsync(filterConv, updateConv); } - public bool UpdateConversationTags(string conversationId, List toAddTags, List toDeleteTags) + public async Task UpdateConversationTags(string conversationId, List toAddTags, List toDeleteTags) { if (string.IsNullOrEmpty(conversationId)) return false; var filter = Builders.Filter.Eq(x => x.Id, conversationId); - var conv = _dc.Conversations.Find(filter).FirstOrDefault(); + var conv = await _dc.Conversations.Find(filter).FirstOrDefaultAsync(); if (conv == null) return false; var tags = conv.Tags ?? []; @@ -157,16 +158,16 @@ public bool UpdateConversationTags(string conversationId, List toAddTags .Set(x => x.Tags, tags) .Set(x => x.UpdatedTime, DateTime.UtcNow); - var res = _dc.Conversations.UpdateOne(filter, update); + var res = await _dc.Conversations.UpdateOneAsync(filter, update); return res.ModifiedCount > 0; } - public bool AppendConversationTags(string conversationId, List tags) + public async Task AppendConversationTags(string conversationId, List tags) { if (string.IsNullOrEmpty(conversationId) || tags.IsNullOrEmpty()) return false; var filter = Builders.Filter.Eq(x => x.Id, conversationId); - var conv = _dc.Conversations.Find(filter).FirstOrDefault(); + var conv = await _dc.Conversations.Find(filter).FirstOrDefaultAsync(); if (conv == null) return false; var curTags = conv.Tags ?? new(); @@ -175,16 +176,16 @@ public bool AppendConversationTags(string conversationId, List tags) .Set(x => x.Tags, newTags) .Set(x => x.UpdatedTime, DateTime.UtcNow); - var res = _dc.Conversations.UpdateOne(filter, update); + var res = await _dc.Conversations.UpdateOneAsync(filter, update); return res.ModifiedCount > 0; } - public bool UpdateConversationMessage(string conversationId, UpdateMessageRequest request) + public async Task UpdateConversationMessage(string conversationId, UpdateMessageRequest request) { if (string.IsNullOrEmpty(conversationId)) return false; var filter = Builders.Filter.Eq(x => x.ConversationId, conversationId); - var foundDialog = _dc.ConversationDialogs.Find(filter).FirstOrDefault(); + var foundDialog = await _dc.ConversationDialogs.Find(filter).FirstOrDefaultAsync(); if (foundDialog == null || foundDialog.Dialogs.IsNullOrEmpty()) { return false; @@ -212,12 +213,12 @@ public bool UpdateConversationMessage(string conversationId, UpdateMessageReques var update = Builders.Update.Set(x => x.Dialogs, dialogs) .Set(x => x.UpdatedTime, DateTime.UtcNow); - _dc.ConversationDialogs.UpdateOne(filter, update); + await _dc.ConversationDialogs.UpdateOneAsync(filter, update); return true; } [SideCar] - public void UpdateConversationBreakpoint(string conversationId, ConversationBreakpoint breakpoint) + public async Task UpdateConversationBreakpoint(string conversationId, ConversationBreakpoint breakpoint) { if (string.IsNullOrEmpty(conversationId)) return; @@ -232,11 +233,11 @@ public void UpdateConversationBreakpoint(string conversationId, ConversationBrea var updateState = Builders.Update.Push(x => x.Breakpoints, newBreakpoint) .Set(x => x.UpdatedTime, DateTime.UtcNow); - _dc.ConversationStates.UpdateOne(filterState, updateState); + await _dc.ConversationStates.UpdateOneAsync(filterState, updateState); } [SideCar] - public ConversationBreakpoint? GetConversationBreakpoint(string conversationId) + public async Task GetConversationBreakpoint(string conversationId) { if (string.IsNullOrEmpty(conversationId)) { @@ -244,7 +245,7 @@ public void UpdateConversationBreakpoint(string conversationId, ConversationBrea } var filter = Builders.Filter.Eq(x => x.ConversationId, conversationId); - var state = _dc.ConversationStates.Find(filter).FirstOrDefault(); + var state = await _dc.ConversationStates.Find(filter).FirstOrDefaultAsync(); var leafNode = state?.Breakpoints?.LastOrDefault(); if (leafNode == null) @@ -261,13 +262,13 @@ public void UpdateConversationBreakpoint(string conversationId, ConversationBrea }; } - public ConversationState GetConversationStates(string conversationId) + public async Task GetConversationStates(string conversationId) { var states = new ConversationState(); if (string.IsNullOrEmpty(conversationId)) return states; var filter = Builders.Filter.Eq(x => x.ConversationId, conversationId); - var foundStates = _dc.ConversationStates.Find(filter).FirstOrDefault(); + var foundStates = await _dc.ConversationStates.Find(filter).FirstOrDefaultAsync(); if (foundStates == null || foundStates.States.IsNullOrEmpty()) return states; var savedStates = foundStates.States.Select(x => StateMongoElement.ToDomainElement(x)).ToList(); @@ -275,7 +276,7 @@ public ConversationState GetConversationStates(string conversationId) } [SideCar] - public void UpdateConversationStates(string conversationId, List states) + public async Task UpdateConversationStates(string conversationId, List states) { if (string.IsNullOrEmpty(conversationId) || states == null) return; @@ -284,7 +285,7 @@ public void UpdateConversationStates(string conversationId, List var updateStates = Builders.Update.Set(x => x.States, saveStates) .Set(x => x.UpdatedTime, DateTime.UtcNow); - _dc.ConversationStates.UpdateOne(filterStates, updateStates); + await _dc.ConversationStates.UpdateOneAsync(filterStates, updateStates); // Update latest states var endNodes = BuildLatestStates(saveStates); @@ -292,10 +293,10 @@ public void UpdateConversationStates(string conversationId, List var update = Builders.Update.Set(x => x.LatestStates, endNodes) .Set(x => x.UpdatedTime, DateTime.UtcNow); - _dc.Conversations.UpdateOne(filter, update); + await _dc.Conversations.UpdateOneAsync(filter, update); } - public void UpdateConversationStatus(string conversationId, string status) + public async Task UpdateConversationStatus(string conversationId, string status) { if (string.IsNullOrEmpty(conversationId) || string.IsNullOrEmpty(status)) return; @@ -304,7 +305,7 @@ public void UpdateConversationStatus(string conversationId, string status) .Set(x => x.Status, status) .Set(x => x.UpdatedTime, DateTime.UtcNow); - _dc.Conversations.UpdateOne(filter, update); + await _dc.Conversations.UpdateOneAsync(filter, update); } public async Task GetConversation(string conversationId, bool isLoadStates = false) @@ -523,12 +524,12 @@ public async ValueTask> GetConversations(ConversationFi }; } - public List GetLastConversations() + public async Task> GetLastConversations() { var records = new List(); - var conversations = _dc.Conversations.Aggregate() - .Group(c => c.UserId, g => g.First(x => x.CreatedTime == g.Select(y => y.CreatedTime).Max())) - .ToList(); + var conversations = await _dc.Conversations.Aggregate() + .Group(c => c.UserId, g => g.First(x => x.CreatedTime == g.Select(y => y.CreatedTime).Max())) + .ToListAsync(); return conversations.Select(c => new Conversation() { Id = c.Id.ToString(), @@ -544,12 +545,13 @@ public List GetLastConversations() }).ToList(); } - public List GetIdleConversations(int batchSize, int messageLimit, int bufferHours, IEnumerable excludeAgentIds) + public async Task> GetIdleConversations(int batchSize, int messageLimit, int bufferHours, IEnumerable excludeAgentIds) { var page = 1; var batchLimit = 100; var utcNow = DateTime.UtcNow; var conversationIds = new List(); + var excludeAgentIdsList = excludeAgentIds?.ToList() ?? new List(); if (batchSize <= 0 || batchSize > batchLimit) { @@ -559,14 +561,14 @@ public List GetIdleConversations(int batchSize, int messageLimit, int bu while (true) { var skip = (page - 1) * batchSize; - var candidates = _dc.Conversations.AsQueryable() + var candidates = await _dc.Conversations.AsQueryable() .Where(x => ((!excludeAgentIds.Contains(x.AgentId) && x.DialogCount <= messageLimit) || (excludeAgentIds.Contains(x.AgentId) && x.DialogCount == 0)) && x.UpdatedTime <= utcNow.AddHours(-bufferHours)) .Skip(skip) .Take(batchSize) .Select(x => x.Id) - .ToList(); + .ToListAsync(); if (candidates.IsNullOrEmpty()) { @@ -585,7 +587,7 @@ public List GetIdleConversations(int batchSize, int messageLimit, int bu return conversationIds.Take(batchSize).ToList(); } - public List TruncateConversation(string conversationId, string messageId, bool cleanLog = false) + public async Task> TruncateConversation(string conversationId, string messageId, bool cleanLog = false) { var deletedMessageIds = new List(); if (string.IsNullOrEmpty(conversationId) || string.IsNullOrEmpty(messageId)) @@ -594,7 +596,7 @@ public List TruncateConversation(string conversationId, string messageId } var dialogFilter = Builders.Filter.Eq(x => x.ConversationId, conversationId); - var foundDialog = _dc.ConversationDialogs.Find(dialogFilter).FirstOrDefault(); + var foundDialog = await _dc.ConversationDialogs.Find(dialogFilter).FirstOrDefaultAsync(); if (foundDialog == null || foundDialog.Dialogs.IsNullOrEmpty()) { return deletedMessageIds; @@ -615,7 +617,7 @@ public List TruncateConversation(string conversationId, string messageId // Handle truncated states var refTime = foundDialog.Dialogs.ElementAt(foundIdx).MetaData.CreateTime; var stateFilter = Builders.Filter.Eq(x => x.ConversationId, conversationId); - var foundStates = _dc.ConversationStates.Find(stateFilter).FirstOrDefault(); + var foundStates = await _dc.ConversationStates.Find(stateFilter).FirstOrDefaultAsync(); var endNodes = new Dictionary(); if (foundStates != null) @@ -654,20 +656,20 @@ public List TruncateConversation(string conversationId, string messageId // Update foundStates.UpdatedTime = DateTime.UtcNow; - _dc.ConversationStates.ReplaceOne(stateFilter, foundStates); + await _dc.ConversationStates.ReplaceOneAsync(stateFilter, foundStates); } // Save dialogs foundDialog.Dialogs = truncatedDialogs; foundDialog.UpdatedTime = DateTime.UtcNow; - _dc.ConversationDialogs.ReplaceOne(dialogFilter, foundDialog); + await _dc.ConversationDialogs.ReplaceOneAsync(dialogFilter, foundDialog); // Update conversation var convFilter = Builders.Filter.Eq(x => x.Id, conversationId); var updateConv = Builders.Update.Set(x => x.UpdatedTime, DateTime.UtcNow) .Set(x => x.LatestStates, endNodes) .Set(x => x.DialogCount, truncatedDialogs.Count); - _dc.Conversations.UpdateOne(convFilter, updateConv); + await _dc.Conversations.UpdateOneAsync(convFilter, updateConv); // Remove logs if (cleanLog) @@ -686,8 +688,8 @@ public List TruncateConversation(string conversationId, string messageId stateLogBuilder.Gte(x => x.CreatedTime, refTime) }; - _dc.ContentLogs.DeleteMany(contentLogBuilder.And(contentLogFilters)); - _dc.StateLogs.DeleteMany(stateLogBuilder.And(stateLogFilters)); + await _dc.ContentLogs.DeleteManyAsync(contentLogBuilder.And(contentLogFilters)); + await _dc.StateLogs.DeleteManyAsync(stateLogBuilder.And(stateLogFilters)); } return deletedMessageIds; @@ -696,7 +698,7 @@ public List TruncateConversation(string conversationId, string messageId #if !DEBUG [SharpCache(10)] #endif - public List GetConversationStateSearchKeys(ConversationStateKeysFilter filter) + public async Task> GetConversationStateSearchKeys(ConversationStateKeysFilter filter) { var builder = Builders.Filter; var sortDef = Builders.Sort.Descending(x => x.UpdatedTime); @@ -723,32 +725,34 @@ public List GetConversationStateSearchKeys(ConversationStateKeysFilter f filters.Add(builder.Lte(x => x.CreatedTime, filter.EndTime.Value)); } - var convDocs = _dc.Conversations.Find(builder.And(filters)) - .Sort(sortDef) - .Limit(filter.ConvLimit) - .ToList(); + var convDocs = await _dc.Conversations.Find(builder.And(filters)) + .Sort(sortDef) + .Limit(filter.ConvLimit) + .ToListAsync(); var keys = convDocs.SelectMany(x => x.LatestStates.Select(x => x.Key)).Distinct().ToList(); return keys; } - public List GetConversationsToMigrate(int batchSize = 100) + public async Task> GetConversationsToMigrate(int batchSize = 100) { var convFilter = Builders.Filter.Exists(x => x.LatestStates, false); var sortDef = Builders.Sort.Ascending(x => x.CreatedTime); - var convIds = _dc.Conversations.Find(convFilter).Sort(sortDef) - .Limit(batchSize).ToEnumerable() - .Select(x => x.Id).ToList(); + var convIds = await _dc.Conversations.Find(convFilter) + .Sort(sortDef) + .Limit(batchSize) + .Project(x => x.Id) + .ToListAsync(); return convIds ?? []; } - public bool MigrateConvsersationLatestStates(string conversationId) + public async Task MigrateConvsersationLatestStates(string conversationId) { if (string.IsNullOrEmpty(conversationId)) return false; var stateFilter = Builders.Filter.Eq(x => x.ConversationId, conversationId); - var foundStates = _dc.ConversationStates.Find(stateFilter).FirstOrDefault(); + var foundStates = await _dc.ConversationStates.Find(stateFilter).FirstOrDefaultAsync(); if (foundStates?.States == null) return false; var states = foundStates.States.ToList(); @@ -756,7 +760,7 @@ public bool MigrateConvsersationLatestStates(string conversationId) var convFilter = Builders.Filter.Eq(x => x.Id, conversationId); var convUpdate = Builders.Update.Set(x => x.LatestStates, latestStates); - _dc.Conversations.UpdateOne(convFilter, convUpdate); + await _dc.Conversations.UpdateOneAsync(convFilter, convUpdate); return true; } diff --git a/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.Crontab.cs b/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.Crontab.cs index 4c307222e..2ac676acb 100644 --- a/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.Crontab.cs +++ b/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.Crontab.cs @@ -5,7 +5,7 @@ namespace BotSharp.Plugin.MongoStorage.Repository; public partial class MongoRepository { - public bool UpsertCrontabItem(CrontabItem item) + public async Task UpsertCrontabItem(CrontabItem item) { if (item == null || string.IsNullOrWhiteSpace(item.ConversationId)) { @@ -18,7 +18,7 @@ public bool UpsertCrontabItem(CrontabItem item) cronDoc.Id = Guid.NewGuid().ToString(); var filter = Builders.Filter.Eq(x => x.ConversationId, item.ConversationId); - var result = _dc.CrontabItems.ReplaceOne(filter, cronDoc, new ReplaceOptions + await _dc.CrontabItems.ReplaceOneAsync(filter, cronDoc, new ReplaceOptions { IsUpsert = true }); @@ -32,7 +32,7 @@ public bool UpsertCrontabItem(CrontabItem item) } - public bool DeleteCrontabItem(string conversationId) + public async Task DeleteCrontabItem(string conversationId) { if (string.IsNullOrWhiteSpace(conversationId)) { @@ -40,7 +40,7 @@ public bool DeleteCrontabItem(string conversationId) } var filter = Builders.Filter.Eq(x => x.ConversationId, conversationId); - var result = _dc.CrontabItems.DeleteMany(filter); + var result = await _dc.CrontabItems.DeleteManyAsync(filter); return result.DeletedCount > 0; } diff --git a/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.KnowledgeBase.cs b/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.KnowledgeBase.cs index 635221688..2b5b7daf1 100644 --- a/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.KnowledgeBase.cs +++ b/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.KnowledgeBase.cs @@ -8,7 +8,7 @@ namespace BotSharp.Plugin.MongoStorage.Repository; public partial class MongoRepository { #region Configs - public bool AddKnowledgeCollectionConfigs(List configs, bool reset = false) + public async Task AddKnowledgeCollectionConfigs(List configs, bool reset = false) { var filter = Builders.Filter.Empty; var docs = configs?.Where(x => !string.IsNullOrWhiteSpace(x.Name)) @@ -23,8 +23,8 @@ public bool AddKnowledgeCollectionConfigs(List configs, if (reset) { - _dc.KnowledgeCollectionConfigs.DeleteMany(filter); - _dc.KnowledgeCollectionConfigs.InsertMany(docs); + await _dc.KnowledgeCollectionConfigs.DeleteManyAsync(filter); + await _dc.KnowledgeCollectionConfigs.InsertManyAsync(docs); return true; } @@ -34,7 +34,7 @@ public bool AddKnowledgeCollectionConfigs(List configs, var names = docs.Select(x => x.Name).ToList(); filter = Builders.Filter.In(x => x.Name, names); - var savedConfigs = _dc.KnowledgeCollectionConfigs.Find(filter).ToList(); + var savedConfigs = await _dc.KnowledgeCollectionConfigs.Find(filter).ToListAsync(); foreach (var doc in docs) { @@ -54,7 +54,7 @@ public bool AddKnowledgeCollectionConfigs(List configs, if (!insertDocs.IsNullOrEmpty()) { - _dc.KnowledgeCollectionConfigs.InsertMany(docs); + await _dc.KnowledgeCollectionConfigs.InsertManyAsync(docs); } if (!updateDocs.IsNullOrEmpty()) @@ -62,19 +62,19 @@ public bool AddKnowledgeCollectionConfigs(List configs, foreach (var doc in updateDocs) { filter = Builders.Filter.Eq(x => x.Id, doc.Id); - _dc.KnowledgeCollectionConfigs.ReplaceOne(filter, doc); + await _dc.KnowledgeCollectionConfigs.ReplaceOneAsync(filter, doc); } } return true; } - public bool DeleteKnowledgeCollectionConfig(string collectionName) + public async Task DeleteKnowledgeCollectionConfig(string collectionName) { if (string.IsNullOrWhiteSpace(collectionName)) return false; var filter = Builders.Filter.Eq(x => x.Name, collectionName); - var deleted = _dc.KnowledgeCollectionConfigs.DeleteMany(filter); + var deleted = await _dc.KnowledgeCollectionConfigs.DeleteManyAsync(filter); return deleted.DeletedCount > 0; } @@ -129,7 +129,7 @@ public async Task GetKnowledgeCollectionConfig(string co #endregion #region Documents - public bool SaveKnolwedgeBaseFileMeta(KnowledgeDocMetaData metaData) + public async Task SaveKnolwedgeBaseFileMeta(KnowledgeDocMetaData metaData) { if (metaData == null || string.IsNullOrWhiteSpace(metaData.Collection) @@ -153,11 +153,11 @@ public bool SaveKnolwedgeBaseFileMeta(KnowledgeDocMetaData metaData) CreateUserId = metaData.CreateUserId }; - _dc.KnowledgeCollectionFileMeta.InsertOne(doc); + await _dc.KnowledgeCollectionFileMeta.InsertOneAsync(doc); return true; } - public bool DeleteKnolwedgeBaseFileMeta(string collectionName, string vectorStoreProvider, Guid? fileId = null) + public async Task DeleteKnolwedgeBaseFileMeta(string collectionName, string vectorStoreProvider, Guid? fileId = null) { if (string.IsNullOrWhiteSpace(collectionName) || string.IsNullOrWhiteSpace(vectorStoreProvider)) @@ -177,7 +177,7 @@ public bool DeleteKnolwedgeBaseFileMeta(string collectionName, string vectorStor filters.Add(builder.Eq(x => x.FileId, fileId)); } - var res = _dc.KnowledgeCollectionFileMeta.DeleteMany(builder.And(filters)); + var res = await _dc.KnowledgeCollectionFileMeta.DeleteManyAsync(builder.And(filters)); return res.DeletedCount > 0; } diff --git a/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.Log.cs b/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.Log.cs index 503576101..1dc042e88 100644 --- a/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.Log.cs +++ b/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.Log.cs @@ -9,7 +9,7 @@ namespace BotSharp.Plugin.MongoStorage.Repository; public partial class MongoRepository { #region LLM Completion Log - public void SaveLlmCompletionLog(LlmCompletionLog log) + public async Task SaveLlmCompletionLog(LlmCompletionLog log) { if (log == null) return; @@ -26,18 +26,18 @@ public void SaveLlmCompletionLog(LlmCompletionLog log) Response = log.Response, CreatedTime = log.CreatedTime }; - _dc.LlmCompletionLogs.InsertOne(data); + await _dc.LlmCompletionLogs.InsertOneAsync(data); } #endregion #region Conversation Content Log - public void SaveConversationContentLog(ContentLogOutputModel log) + public async Task SaveConversationContentLog(ContentLogOutputModel log) { if (log == null) return; var filter = Builders.Filter.Eq(x => x.Id, log.ConversationId); - var found = _dc.Conversations.Find(filter).FirstOrDefault(); + var found = await _dc.Conversations.Find(filter).FirstOrDefaultAsync(); if (found == null) return; var logDoc = new ConversationContentLogDocument @@ -52,10 +52,10 @@ public void SaveConversationContentLog(ContentLogOutputModel log) CreatedTime = log.CreatedTime }; - _dc.ContentLogs.InsertOne(logDoc); + await _dc.ContentLogs.InsertOneAsync(logDoc); } - public DateTimePagination GetConversationContentLogs(string conversationId, ConversationLogFilter filter) + public async Task> GetConversationContentLogs(string conversationId, ConversationLogFilter filter) { var builder = Builders.Filter; var logFilters = new List> @@ -65,7 +65,7 @@ public DateTimePagination GetConversationContentLogs(stri }; var logSortDef = Builders.Sort.Descending(x => x.CreatedTime); - var docs = _dc.ContentLogs.Find(builder.And(logFilters)).Sort(logSortDef).Limit(filter.Size).ToList(); + var docs = await _dc.ContentLogs.Find(builder.And(logFilters)).Sort(logSortDef).Limit(filter.Size).ToListAsync(); var logs = docs.Select(x => new ContentLogOutputModel { ConversationId = x.ConversationId, @@ -89,12 +89,12 @@ public DateTimePagination GetConversationContentLogs(stri #endregion #region Conversation State Log - public void SaveConversationStateLog(ConversationStateLogModel log) + public async Task SaveConversationStateLog(ConversationStateLogModel log) { if (log == null) return; var filter = Builders.Filter.Eq(x => x.Id, log.ConversationId); - var found = _dc.Conversations.Find(filter).FirstOrDefault(); + var found = await _dc.Conversations.Find(filter).FirstOrDefaultAsync(); if (found == null) return; var logDoc = new ConversationStateLogDocument @@ -106,10 +106,10 @@ public void SaveConversationStateLog(ConversationStateLogModel log) CreatedTime = log.CreatedTime }; - _dc.StateLogs.InsertOne(logDoc); + await _dc.StateLogs.InsertOneAsync(logDoc); } - public DateTimePagination GetConversationStateLogs(string conversationId, ConversationLogFilter filter) + public async Task> GetConversationStateLogs(string conversationId, ConversationLogFilter filter) { var builder = Builders.Filter; var logFilters = new List> @@ -119,7 +119,7 @@ public DateTimePagination GetConversationStateLogs(st }; var logSortDef = Builders.Sort.Descending(x => x.CreatedTime); - var docs = _dc.StateLogs.Find(builder.And(logFilters)).Sort(logSortDef).Limit(filter.Size).ToList(); + var docs = await _dc.StateLogs.Find(builder.And(logFilters)).Sort(logSortDef).Limit(filter.Size).ToListAsync(); var logs = docs.Select(x => new ConversationStateLogModel { ConversationId = x.ConversationId, diff --git a/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.Plugin.cs b/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.Plugin.cs index b26da408e..28275d762 100644 --- a/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.Plugin.cs +++ b/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.Plugin.cs @@ -5,10 +5,10 @@ namespace BotSharp.Plugin.MongoStorage.Repository; public partial class MongoRepository { #region Plugin - public PluginConfig GetPluginConfig() + public async Task GetPluginConfig() { var config = new PluginConfig(); - var found = _dc.Plugins.AsQueryable().FirstOrDefault(); + var found = await _dc.Plugins.Find(Builders.Filter.Empty).FirstOrDefaultAsync(); if (found != null) { config = new PluginConfig() @@ -19,7 +19,7 @@ public PluginConfig GetPluginConfig() return config; } - public void SavePluginConfig(PluginConfig config) + public async Task SavePluginConfig(PluginConfig config) { if (config == null || config.EnabledPlugins == null) return; @@ -28,7 +28,7 @@ public void SavePluginConfig(PluginConfig config) .Set(x => x.EnabledPlugins, config.EnabledPlugins) .SetOnInsert(x => x.Id, Guid.NewGuid().ToString()); - _dc.Plugins.UpdateOne(filter, update, _options); + await _dc.Plugins.UpdateOneAsync(filter, update, _options); } #endregion } diff --git a/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.Role.cs b/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.Role.cs index 958edf0fd..73e43833c 100644 --- a/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.Role.cs +++ b/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.Role.cs @@ -5,7 +5,7 @@ namespace BotSharp.Plugin.MongoStorage.Repository; public partial class MongoRepository { - public bool RefreshRoles(IEnumerable roles) + public async Task RefreshRoles(IEnumerable roles) { if (roles.IsNullOrEmpty()) return false; @@ -15,8 +15,8 @@ public bool RefreshRoles(IEnumerable roles) // Clear data - _dc.RoleAgents.DeleteMany(Builders.Filter.Empty); - _dc.Roles.DeleteMany(Builders.Filter.Empty); + await _dc.RoleAgents.DeleteManyAsync(Builders.Filter.Empty); + await _dc.Roles.DeleteManyAsync(Builders.Filter.Empty); var roleDocs = validRoles.Select(x => new RoleDocument { @@ -26,13 +26,13 @@ public bool RefreshRoles(IEnumerable roles) CreatedTime = DateTime.UtcNow, UpdatedTime = DateTime.UtcNow }); - _dc.Roles.InsertMany(roleDocs); + await _dc.Roles.InsertManyAsync(roleDocs); return true; } - public IEnumerable GetRoles(RoleFilter filter) + public async Task> GetRoles(RoleFilter filter) { if (filter == null) { @@ -54,22 +54,22 @@ public IEnumerable GetRoles(RoleFilter filter) } // Search - var roleDocs = _dc.Roles.Find(roleBuilder.And(roleFilters)).ToList(); + var roleDocs = await _dc.Roles.Find(roleBuilder.And(roleFilters)).ToListAsync(); var roles = roleDocs.Select(x => x.ToRole()).ToList(); return roles; } - public Role? GetRoleDetails(string roleId, bool includeAgent = false) + public async Task GetRoleDetails(string roleId, bool includeAgent = false) { if (string.IsNullOrWhiteSpace(roleId)) return null; - var roleDoc = _dc.Roles.Find(Builders.Filter.Eq(x => x.Id, roleId)).FirstOrDefault(); + var roleDoc = await _dc.Roles.Find(Builders.Filter.Eq(x => x.Id, roleId)).FirstOrDefaultAsync(); if (roleDoc == null) return null; var agentActions = new List(); var role = roleDoc.ToRole(); - var roleAgentDocs = _dc.RoleAgents.Find(Builders.Filter.Eq(x => x.RoleId, roleId)).ToList(); + var roleAgentDocs = await _dc.RoleAgents.Find(Builders.Filter.Eq(x => x.RoleId, roleId)).ToListAsync(); if (!includeAgent) { @@ -86,7 +86,7 @@ public IEnumerable GetRoles(RoleFilter filter) var agentIds = roleAgentDocs.Select(x => x.AgentId).Distinct().ToList(); if (!agentIds.IsNullOrEmpty()) { - var agents = GetAgents(new AgentFilter { AgentIds = agentIds }); + var agents = await GetAgents(new AgentFilter { AgentIds = agentIds }); foreach (var item in roleAgentDocs) { @@ -107,7 +107,7 @@ public IEnumerable GetRoles(RoleFilter filter) return role; } - public bool UpdateRole(Role role, bool updateRoleAgents = false) + public async Task UpdateRole(Role role, bool updateRoleAgents = false) { if (string.IsNullOrEmpty(role?.Id)) return false; @@ -118,7 +118,7 @@ public bool UpdateRole(Role role, bool updateRoleAgents = false) .Set(x => x.CreatedTime, DateTime.UtcNow) .Set(x => x.UpdatedTime, DateTime.UtcNow); - _dc.Roles.UpdateOne(roleFilter, roleUpdate, _options); + await _dc.Roles.UpdateOneAsync(roleFilter, roleUpdate, _options); if (updateRoleAgents) { @@ -132,12 +132,12 @@ public bool UpdateRole(Role role, bool updateRoleAgents = false) UpdatedTime = DateTime.UtcNow })?.ToList() ?? []; - var toDelete = _dc.RoleAgents.Find(Builders.Filter.And( + var toDelete = await _dc.RoleAgents.Find(Builders.Filter.And( Builders.Filter.Eq(x => x.RoleId, role.Id), Builders.Filter.Nin(x => x.Id, roleAgentDocs.Select(x => x.Id)) - )).ToList(); + )).ToListAsync(); - _dc.RoleAgents.DeleteMany(Builders.Filter.In(x => x.Id, toDelete.Select(x => x.Id))); + await _dc.RoleAgents.DeleteManyAsync(Builders.Filter.In(x => x.Id, toDelete.Select(x => x.Id))); foreach (var doc in roleAgentDocs) { var roleAgentFilter = Builders.Filter.Eq(x => x.Id, doc.Id); @@ -148,7 +148,7 @@ public bool UpdateRole(Role role, bool updateRoleAgents = false) .Set(x => x.Actions, doc.Actions) .Set(x => x.UpdatedTime, DateTime.UtcNow); - _dc.RoleAgents.UpdateOne(roleAgentFilter, roleAgentUpdate, _options); + await _dc.RoleAgents.UpdateOneAsync(roleAgentFilter, roleAgentUpdate, _options); } } diff --git a/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.Stats.cs b/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.Stats.cs index 3c245643c..4010bae2d 100644 --- a/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.Stats.cs +++ b/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.Stats.cs @@ -5,7 +5,7 @@ namespace BotSharp.Plugin.MongoStorage.Repository; public partial class MongoRepository { - public BotSharpStats? GetGlobalStats(string agentId, DateTime recordTime, StatsInterval interval) + public async Task GetGlobalStats(string agentId, DateTime recordTime, StatsInterval interval) { if (string.IsNullOrWhiteSpace(agentId)) { @@ -23,7 +23,7 @@ public partial class MongoRepository }; var filterDef = builder.And(filters); - var found = _dc.GlobalStats.Find(filterDef).FirstOrDefault(); + var found = await _dc.GlobalStats.Find(filterDef).FirstOrDefaultAsync(); return found != null ? new BotSharpStats { @@ -48,7 +48,7 @@ public partial class MongoRepository } : null; } - public bool SaveGlobalStats(BotSharpStatsDelta delta) + public async Task SaveGlobalStats(BotSharpStatsDelta delta) { if (delta == null || string.IsNullOrWhiteSpace(delta.AgentId)) { @@ -81,7 +81,7 @@ public bool SaveGlobalStats(BotSharpStatsDelta delta) .Set(x => x.Interval, delta.Interval) .Set(x => x.RecordTime, delta.RecordTime); - _dc.GlobalStats.UpdateOne(filterDef, updateDef, _options); + await _dc.GlobalStats.UpdateOneAsync(filterDef, updateDef, _options); return true; } } diff --git a/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.Translation.cs b/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.Translation.cs index 6ab267928..77ecbab3a 100644 --- a/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.Translation.cs +++ b/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.Translation.cs @@ -5,7 +5,7 @@ namespace BotSharp.Plugin.MongoStorage.Repository; public partial class MongoRepository { - public IEnumerable GetTranslationMemories(IEnumerable queries) + public async Task> GetTranslationMemories(IEnumerable queries) { var list = new List(); if (queries.IsNullOrEmpty()) @@ -15,7 +15,7 @@ public IEnumerable GetTranslationMemories(IEnumerable !string.IsNullOrEmpty(x.HashText)).Select(x => x.HashText).ToList(); var filter = Builders.Filter.In(x => x.HashText, hashTexts); - var memories = _dc.TranslationMemories.Find(filter).ToList(); + var memories = await _dc.TranslationMemories.Find(filter).ToListAsync(); if (memories.IsNullOrEmpty()) return list; foreach (var query in queries) @@ -43,13 +43,13 @@ public IEnumerable GetTranslationMemories(IEnumerable inputs) + public async Task SaveTranslationMemories(IEnumerable inputs) { if (inputs.IsNullOrEmpty()) return false; var hashTexts = inputs.Where(x => !string.IsNullOrEmpty(x.HashText)).Select(x => x.HashText).ToList(); var filter = Builders.Filter.In(x => x.HashText, hashTexts); - var memories = _dc.TranslationMemories.Find(filter)?.ToList() ?? []; + var memories = await _dc.TranslationMemories.Find(filter).ToListAsync() ?? []; var newMemories = new List(); var updateMemories = new List(); @@ -102,7 +102,7 @@ public bool SaveTranslationMemories(IEnumerable inputs) if (!newMemories.IsNullOrEmpty()) { - _dc.TranslationMemories.InsertMany(newMemories); + await _dc.TranslationMemories.InsertManyAsync(newMemories); } if (!updateMemories.IsNullOrEmpty()) @@ -110,8 +110,8 @@ public bool SaveTranslationMemories(IEnumerable inputs) foreach (var mem in updateMemories) { var updateFilter = Builders.Filter.Eq(x => x.Id, mem.Id); - _dc.TranslationMemories.ReplaceOne(updateFilter, mem); - Thread.Sleep(50); + await _dc.TranslationMemories.ReplaceOneAsync(updateFilter, mem); + await Task.Delay(50); } } return true; diff --git a/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.User.cs b/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.User.cs index 19a177f4f..774b3dae9 100644 --- a/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.User.cs +++ b/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.User.cs @@ -370,7 +370,7 @@ public async Task> SearchLoginUsers(User filter, string source = User var agentIds = userAgents.Select(x => x.AgentId)?.Distinct().ToList(); if (!agentIds.IsNullOrEmpty()) { - var agents = GetAgents(new AgentFilter { AgentIds = agentIds }); + var agents = await GetAgents(new AgentFilter { AgentIds = agentIds }); foreach (var item in userAgents) { diff --git a/src/Plugins/BotSharp.Plugin.Planner/Sequential/SequentialPlanner.cs b/src/Plugins/BotSharp.Plugin.Planner/Sequential/SequentialPlanner.cs index a3c7e9464..975a60e9b 100644 --- a/src/Plugins/BotSharp.Plugin.Planner/Sequential/SequentialPlanner.cs +++ b/src/Plugins/BotSharp.Plugin.Planner/Sequential/SequentialPlanner.cs @@ -121,23 +121,23 @@ public bool AfterHandleContext(List dialogs, List AgentExecuting(Agent router, FunctionCallFromLlm inst, RoleDialogModel message, List dialogs) + public Task AgentExecuting(Agent router, FunctionCallFromLlm inst, RoleDialogModel message, List dialogs) { // Set user content as Planner's question message.FunctionName = inst.Function; message.FunctionArgs = inst.Arguments == null ? "{}" : JsonSerializer.Serialize(inst.Arguments); - return true; + return Task.FromResult(true); } - public async Task AgentExecuted(Agent router, FunctionCallFromLlm inst, RoleDialogModel message, List dialogs) + public Task AgentExecuted(Agent router, FunctionCallFromLlm inst, RoleDialogModel message, List dialogs) { var context = _services.GetRequiredService(); if (message.StopCompletion) { context.Empty(reason: $"Agent queue is cleared by {nameof(SequentialPlanner)}"); - return false; + return Task.FromResult(false); } // Handover to Router; @@ -146,7 +146,7 @@ public async Task AgentExecuted(Agent router, FunctionCallFromLlm inst, Ro var routing = _services.GetRequiredService(); routing.Context.ResetRecursiveCounter(); - return true; + return Task.FromResult(true); } private string GetNextStepPrompt(Agent router) diff --git a/src/Plugins/BotSharp.Plugin.Planner/SqlGeneration/Functions/SqlGenerationFn.cs b/src/Plugins/BotSharp.Plugin.Planner/SqlGeneration/Functions/SqlGenerationFn.cs index e98a11ca1..857f4c602 100644 --- a/src/Plugins/BotSharp.Plugin.Planner/SqlGeneration/Functions/SqlGenerationFn.cs +++ b/src/Plugins/BotSharp.Plugin.Planner/SqlGeneration/Functions/SqlGenerationFn.cs @@ -113,7 +113,7 @@ await HookEmitter.Emit(_services, async x => private async Task GetAiResponse(Agent plannerAgent) { var conv = _services.GetRequiredService(); - var wholeDialogs = conv.GetDialogHistory(); + var wholeDialogs = await conv.GetDialogHistory(); // Append text wholeDialogs.Last().Content += "\n\nIf the table structure didn't mention auto incremental, the data field id needs to insert id manually and you need to use max(id).\nFor example, you should use SET @id = select max(id) from table;"; diff --git a/src/Plugins/BotSharp.Plugin.Planner/SqlGeneration/Functions/SqlPrimaryStageFn.cs b/src/Plugins/BotSharp.Plugin.Planner/SqlGeneration/Functions/SqlPrimaryStageFn.cs index eb5c2686f..be6b8078b 100644 --- a/src/Plugins/BotSharp.Plugin.Planner/SqlGeneration/Functions/SqlPrimaryStageFn.cs +++ b/src/Plugins/BotSharp.Plugin.Planner/SqlGeneration/Functions/SqlPrimaryStageFn.cs @@ -93,7 +93,7 @@ private async Task GetPrimaryStagePlanPrompt(RoleDialogModel message, st private async Task GetAiResponse(Agent plannerAgent) { var conv = _services.GetRequiredService(); - var wholeDialogs = conv.GetDialogHistory(); + var wholeDialogs = await conv.GetDialogHistory(); var completion = CompletionProvider.GetChatCompletion(_services, provider: plannerAgent.LlmConfig.Provider, diff --git a/src/Plugins/BotSharp.Plugin.Planner/SqlGeneration/Functions/SqlSecondaryStageFn.cs b/src/Plugins/BotSharp.Plugin.Planner/SqlGeneration/Functions/SqlSecondaryStageFn.cs index 44e27da46..a13c093ee 100644 --- a/src/Plugins/BotSharp.Plugin.Planner/SqlGeneration/Functions/SqlSecondaryStageFn.cs +++ b/src/Plugins/BotSharp.Plugin.Planner/SqlGeneration/Functions/SqlSecondaryStageFn.cs @@ -92,7 +92,7 @@ private async Task GetSecondStagePlanPrompt(string taskDescription, stri private async Task GetAiResponse(Agent plannerAgent) { var conv = _services.GetRequiredService(); - var wholeDialogs = conv.GetDialogHistory(); + var wholeDialogs = await conv.GetDialogHistory(); wholeDialogs.Last().Content += "\r\nOutput in JSON format."; diff --git a/src/Plugins/BotSharp.Plugin.Planner/SqlGeneration/SqlGenerationPlanner.cs b/src/Plugins/BotSharp.Plugin.Planner/SqlGeneration/SqlGenerationPlanner.cs index a804b0736..a295ad2f9 100644 --- a/src/Plugins/BotSharp.Plugin.Planner/SqlGeneration/SqlGenerationPlanner.cs +++ b/src/Plugins/BotSharp.Plugin.Planner/SqlGeneration/SqlGenerationPlanner.cs @@ -62,12 +62,12 @@ public bool AfterHandleContext(List dialogs, List AgentExecuting(Agent router, FunctionCallFromLlm inst, RoleDialogModel message, List dialogs) + public Task AgentExecuting(Agent router, FunctionCallFromLlm inst, RoleDialogModel message, List dialogs) { // Set user content as Planner's question message.FunctionName = inst.Function; message.FunctionArgs = inst.Arguments == null ? "{}" : JsonSerializer.Serialize(inst.Arguments); - return true; + return Task.FromResult(true); } public async Task AgentExecuted(Agent router, FunctionCallFromLlm inst, RoleDialogModel message, List dialogs) diff --git a/src/Plugins/BotSharp.Plugin.Planner/TwoStaging/Functions/PrimaryStagePlanFn.cs b/src/Plugins/BotSharp.Plugin.Planner/TwoStaging/Functions/PrimaryStagePlanFn.cs index e03f57c5e..0d726b876 100644 --- a/src/Plugins/BotSharp.Plugin.Planner/TwoStaging/Functions/PrimaryStagePlanFn.cs +++ b/src/Plugins/BotSharp.Plugin.Planner/TwoStaging/Functions/PrimaryStagePlanFn.cs @@ -93,7 +93,7 @@ private async Task GetFirstStagePlanPrompt(RoleDialogModel message, stri private async Task GetAiResponse(Agent plannerAgent) { var conv = _services.GetRequiredService(); - var wholeDialogs = conv.GetDialogHistory(); + var wholeDialogs = await conv.GetDialogHistory(); var completion = CompletionProvider.GetChatCompletion(_services, provider: plannerAgent.LlmConfig.Provider, diff --git a/src/Plugins/BotSharp.Plugin.Planner/TwoStaging/Functions/SecondaryStagePlanFn.cs b/src/Plugins/BotSharp.Plugin.Planner/TwoStaging/Functions/SecondaryStagePlanFn.cs index 23daedf50..ab4b01f2d 100644 --- a/src/Plugins/BotSharp.Plugin.Planner/TwoStaging/Functions/SecondaryStagePlanFn.cs +++ b/src/Plugins/BotSharp.Plugin.Planner/TwoStaging/Functions/SecondaryStagePlanFn.cs @@ -92,7 +92,7 @@ private async Task GetSecondStagePlanPrompt(string taskDescription, stri private async Task GetAiResponse(Agent plannerAgent) { var conv = _services.GetRequiredService(); - var wholeDialogs = conv.GetDialogHistory(); + var wholeDialogs = await conv.GetDialogHistory(); wholeDialogs.Last().Content += "\r\nOutput in JSON format."; diff --git a/src/Plugins/BotSharp.Plugin.Planner/TwoStaging/Functions/SummaryPlanFn.cs b/src/Plugins/BotSharp.Plugin.Planner/TwoStaging/Functions/SummaryPlanFn.cs index 2654c7408..f1f28c2b8 100644 --- a/src/Plugins/BotSharp.Plugin.Planner/TwoStaging/Functions/SummaryPlanFn.cs +++ b/src/Plugins/BotSharp.Plugin.Planner/TwoStaging/Functions/SummaryPlanFn.cs @@ -112,7 +112,7 @@ await HookEmitter.Emit(_services, async x => private async Task GetAiResponse(Agent plannerAgent) { var conv = _services.GetRequiredService(); - var wholeDialogs = conv.GetDialogHistory(); + var wholeDialogs = await conv.GetDialogHistory(); // Append text wholeDialogs.Last().Content += "\n\nIf the table structure didn't mention auto incremental, the data field id needs to insert id manually and you need to use max(id).\nFor example, you should use SET @id = select max(id) from table;"; diff --git a/src/Plugins/BotSharp.Plugin.Planner/TwoStaging/TwoStageTaskPlanner.cs b/src/Plugins/BotSharp.Plugin.Planner/TwoStaging/TwoStageTaskPlanner.cs index ea89cd715..42303e9e3 100644 --- a/src/Plugins/BotSharp.Plugin.Planner/TwoStaging/TwoStageTaskPlanner.cs +++ b/src/Plugins/BotSharp.Plugin.Planner/TwoStaging/TwoStageTaskPlanner.cs @@ -90,12 +90,12 @@ public bool AfterHandleContext(List dialogs, List AgentExecuting(Agent router, FunctionCallFromLlm inst, RoleDialogModel message, List dialogs) + public Task AgentExecuting(Agent router, FunctionCallFromLlm inst, RoleDialogModel message, List dialogs) { // Set user content as Planner's question message.FunctionName = inst.Function; message.FunctionArgs = inst.Arguments == null ? "{}" : JsonSerializer.Serialize(inst.Arguments); - return true; + return Task.FromResult(true); } public async Task AgentExecuted(Agent router, FunctionCallFromLlm inst, RoleDialogModel message, List dialogs) diff --git a/src/Plugins/BotSharp.Plugin.PythonInterpreter/Functions/PyProgrammerFn.cs b/src/Plugins/BotSharp.Plugin.PythonInterpreter/Functions/PyProgrammerFn.cs index 642336aaf..73277d2ec 100644 --- a/src/Plugins/BotSharp.Plugin.PythonInterpreter/Functions/PyProgrammerFn.cs +++ b/src/Plugins/BotSharp.Plugin.PythonInterpreter/Functions/PyProgrammerFn.cs @@ -38,7 +38,7 @@ public async Task Execute(RoleDialogModel message) var args = JsonSerializer.Deserialize(message.FunctionArgs); var agent = await agentService.GetAgent(message.CurrentAgentId); - var inst = GetPyCodeInterpreterInstruction(message.CurrentAgentId); + var inst = await GetPyCodeInterpreterInstruction(message.CurrentAgentId); var innerAgent = new Agent { Id = agent.Id, @@ -55,7 +55,7 @@ public async Task Execute(RoleDialogModel message) var dialogs = routingCtx.GetDialogs(); if (dialogs.IsNullOrEmpty()) { - dialogs = convService.GetDialogHistory(); + dialogs = await convService.GetDialogHistory(); } var messageLimit = _codingSettings.CodeGeneration?.MessageLimit > 0 ? _codingSettings.CodeGeneration.MessageLimit.Value : 50; @@ -153,7 +153,7 @@ private async Task GetChatCompletion(Agent agent, List } } - private string GetPyCodeInterpreterInstruction(string agentId) + private async Task GetPyCodeInterpreterInstruction(string agentId) { var db = _services.GetRequiredService(); var state = _services.GetRequiredService(); @@ -163,12 +163,12 @@ private string GetPyCodeInterpreterInstruction(string agentId) if (!string.IsNullOrEmpty(templateName)) { - templateContent = db.GetAgentTemplate(agentId, templateName); + templateContent = await db.GetAgentTemplate(agentId, templateName); } else { templateName = "py-code_generate_instruction"; - templateContent = db.GetAgentTemplate(BuiltInAgentId.AIProgrammer, templateName); + templateContent = await db.GetAgentTemplate(BuiltInAgentId.AIProgrammer, templateName); } return templateContent; diff --git a/src/Plugins/BotSharp.Plugin.SqlDriver/Controllers/SqlDriverController.cs b/src/Plugins/BotSharp.Plugin.SqlDriver/Controllers/SqlDriverController.cs index 8a688b0d8..a39b04cff 100644 --- a/src/Plugins/BotSharp.Plugin.SqlDriver/Controllers/SqlDriverController.cs +++ b/src/Plugins/BotSharp.Plugin.SqlDriver/Controllers/SqlDriverController.cs @@ -28,7 +28,7 @@ public async Task ExecuteSqlQuery([FromRoute] string conversation var fn = _services.GetRequiredService(); var conv = _services.GetRequiredService(); - conv.SetConversationId(conversationId, + await conv.SetConversationId(conversationId, [ new MessageState("database_type", sqlQueryRequest.DbType), new MessageState("data_source_name", sqlQueryRequest.DataSource), @@ -60,7 +60,7 @@ public async Task ExecuteSqlQuery([FromRoute] string conversation MessageId = msg.MessageId, CreatedAt = DateTime.UtcNow }; - storage.Append(conversationId, dialog); + await storage.Append(conversationId, dialog); } } @@ -72,7 +72,7 @@ public async Task ExecuteSqlQuery([FromRoute] string conversation public async Task AddQueryExecutionResult([FromRoute] string conversationId, [FromBody] SqlQueryExecutionResult sqlQueryResult) { var conv = _services.GetRequiredService(); - conv.SetConversationId(conversationId, []); + await conv.SetConversationId(conversationId, []); var storage = _services.GetRequiredService(); var dialog = new RoleDialogModel(AgentRole.Assistant, sqlQueryResult.Results) @@ -80,7 +80,7 @@ public async Task AddQueryExecutionResult([FromRoute] string conv CurrentAgentId = sqlQueryResult.AgentId, CreatedAt = DateTime.UtcNow }; - storage.Append(conversationId, dialog); + await storage.Append(conversationId, dialog); return Ok(dialog); } diff --git a/src/Plugins/BotSharp.Plugin.SqlDriver/Hooks/SqlDriverCrontabHook.cs b/src/Plugins/BotSharp.Plugin.SqlDriver/Hooks/SqlDriverCrontabHook.cs index d76d8610f..21ad01636 100644 --- a/src/Plugins/BotSharp.Plugin.SqlDriver/Hooks/SqlDriverCrontabHook.cs +++ b/src/Plugins/BotSharp.Plugin.SqlDriver/Hooks/SqlDriverCrontabHook.cs @@ -19,7 +19,7 @@ public SqlDriverCrontabHook(IServiceProvider services, ILogger(); - conv.SetConversationId(item.ConversationId, []); + await conv.SetConversationId(item.ConversationId, []); _logger.LogWarning($"Crontab item triggered: {item.Title}. {item.Description}"); diff --git a/src/Plugins/BotSharp.Plugin.SqlDriver/Services/SqlChartProcessor.cs b/src/Plugins/BotSharp.Plugin.SqlDriver/Services/SqlChartProcessor.cs index 858cf11f0..add8c9dff 100644 --- a/src/Plugins/BotSharp.Plugin.SqlDriver/Services/SqlChartProcessor.cs +++ b/src/Plugins/BotSharp.Plugin.SqlDriver/Services/SqlChartProcessor.cs @@ -28,7 +28,7 @@ public SqlChartProcessor( if (!string.IsNullOrWhiteSpace(options?.TargetStateName)) { var db = _services.GetRequiredService(); - var states = db.GetConversationStates(conversationId); + var states = await db.GetConversationStates(conversationId); var value = states?.GetValueOrDefault(options?.TargetStateName)?.Values?.LastOrDefault()?.Data; // To do diff --git a/src/Plugins/BotSharp.Plugin.Twilio/Controllers/TwilioInboundController.cs b/src/Plugins/BotSharp.Plugin.Twilio/Controllers/TwilioInboundController.cs index b5dbfb2e5..40ba7d80d 100644 --- a/src/Plugins/BotSharp.Plugin.Twilio/Controllers/TwilioInboundController.cs +++ b/src/Plugins/BotSharp.Plugin.Twilio/Controllers/TwilioInboundController.cs @@ -200,20 +200,20 @@ protected Dictionary ParseStates(List states) states.Add(new(StateConst.ROUTING_MODE, agent.Mode)); } - convService.SetConversationId(conversation.Id, states); + await convService.SetConversationId(conversation.Id, states); if (!string.IsNullOrEmpty(request.Intent)) { var storage = _services.GetRequiredService(); - storage.Append(conversation.Id, new RoleDialogModel(AgentRole.User, request.Intent) + await storage.Append(conversation.Id, new RoleDialogModel(AgentRole.User, request.Intent) { CurrentAgentId = agent.Id, CreatedAt = DateTime.UtcNow }); } - convService.SaveStates(); + await convService.SaveStates(); // reload agent rendering with states agent = await agentService.LoadAgent(request.AgentId); diff --git a/src/Plugins/BotSharp.Plugin.Twilio/Controllers/TwilioRecordController.cs b/src/Plugins/BotSharp.Plugin.Twilio/Controllers/TwilioRecordController.cs index 3d13dc739..c6690d84c 100644 --- a/src/Plugins/BotSharp.Plugin.Twilio/Controllers/TwilioRecordController.cs +++ b/src/Plugins/BotSharp.Plugin.Twilio/Controllers/TwilioRecordController.cs @@ -30,11 +30,11 @@ public async Task PhoneRecordingStatus(ConversationalVoiceRequest // Set the recording URL to the conversation state var convService = _services.GetRequiredService(); - convService.SetConversationId(request.ConversationId, new List + await convService.SetConversationId(request.ConversationId, new List { new("phone_recording_url", request.RecordingUrl) }); - convService.SaveStates(); + await convService.SaveStates(); // recording completed await HookEmitter.Emit(_services, x => x.OnRecordingCompleted(request), request.AgentId); diff --git a/src/Plugins/BotSharp.Plugin.Twilio/Controllers/TwilioTranscribeController.cs b/src/Plugins/BotSharp.Plugin.Twilio/Controllers/TwilioTranscribeController.cs index 778aa29dd..d5af9d299 100644 --- a/src/Plugins/BotSharp.Plugin.Twilio/Controllers/TwilioTranscribeController.cs +++ b/src/Plugins/BotSharp.Plugin.Twilio/Controllers/TwilioTranscribeController.cs @@ -39,13 +39,13 @@ public async Task PhoneRecordingTranscribe(ConversationalVoiceRequ { CurrentAgentId = request.AgentId }; - storage.Append(request.ConversationId, message); + await storage.Append(request.ConversationId, message); var routing = _services.GetRequiredService(); routing.Context.SetMessageId(request.ConversationId, message.MessageId); var convService = _services.GetRequiredService(); - convService.SetConversationId(request.ConversationId, []); + await convService.SetConversationId(request.ConversationId, []); // transcription completed transcript.Language = request.LanguageCode; diff --git a/src/Plugins/BotSharp.Plugin.Twilio/Hooks/TwilioConversationHook.cs b/src/Plugins/BotSharp.Plugin.Twilio/Hooks/TwilioConversationHook.cs index bf856ee4f..3ccc4f4d6 100644 --- a/src/Plugins/BotSharp.Plugin.Twilio/Hooks/TwilioConversationHook.cs +++ b/src/Plugins/BotSharp.Plugin.Twilio/Hooks/TwilioConversationHook.cs @@ -53,7 +53,7 @@ public override async Task OnFunctionExecuted(RoleDialogModel message, InvokeFun } // Save all states before reconnect - states.Save(); + await states.Save(); CallResource.Update( pathSid: sid, diff --git a/src/Plugins/BotSharp.Plugin.Twilio/OutboundPhoneCallHandler/Functions/OutboundPhoneCallFn.cs b/src/Plugins/BotSharp.Plugin.Twilio/OutboundPhoneCallHandler/Functions/OutboundPhoneCallFn.cs index 3c5532bdb..3ef92d3c5 100644 --- a/src/Plugins/BotSharp.Plugin.Twilio/OutboundPhoneCallHandler/Functions/OutboundPhoneCallFn.cs +++ b/src/Plugins/BotSharp.Plugin.Twilio/OutboundPhoneCallHandler/Functions/OutboundPhoneCallFn.cs @@ -162,7 +162,7 @@ private async Task ForkConversation( }); var messageId = Guid.NewGuid().ToString(); - convStorage.Append(newConversationId, new List + await convStorage.Append(newConversationId, new List { new RoleDialogModel(AgentRole.User, "Hi") { @@ -203,7 +203,7 @@ private async Task ForkConversation( var mappedSubConvStates = MapStates(subConvStates, messageId, utcNow); var allStates = mappedCurConvStates.Concat(mappedSubConvStates).ToList(); - db.UpdateConversationStates(newConversationId, allStates); + await db.UpdateConversationStates(newConversationId, allStates); } private IEnumerable MapStates(IEnumerable states, string messageId, DateTime updateTime) diff --git a/src/Plugins/BotSharp.Plugin.Twilio/Services/TwilioMessageQueueService.cs b/src/Plugins/BotSharp.Plugin.Twilio/Services/TwilioMessageQueueService.cs index 6b06f147b..35cf78aed 100644 --- a/src/Plugins/BotSharp.Plugin.Twilio/Services/TwilioMessageQueueService.cs +++ b/src/Plugins/BotSharp.Plugin.Twilio/Services/TwilioMessageQueueService.cs @@ -90,7 +90,7 @@ private async Task ProcessUserMessageAsync(CallerMessage message) { { ChatEvent.OnIndicationReceived, async data => await OnReceiveToolCallIndication(data.Data, message, sessionManager) } }); - InitConversation(message, inputMsg, conv, routing); + await InitConversation(message, inputMsg, conv, routing); // Need to consider Inbound and Outbound call var conversation = await conv.GetConversation(message.ConversationId); @@ -98,7 +98,7 @@ private async Task ProcessUserMessageAsync(CallerMessage message) var result = await conv.SendMessage(agentId, inputMsg, - replyMessage: BuildPostbackMessageModel(conv, message), + replyMessage: await BuildPostbackMessageModel(conv, message), async msg => { reply = new AssistantMessage() @@ -121,9 +121,9 @@ private async Task ProcessUserMessageAsync(CallerMessage message) await sessionManager.SetAssistantReplyAsync(message.ConversationId, message.SeqNumber, reply); } - private PostbackMessageModel BuildPostbackMessageModel(IConversationService conv, CallerMessage message) + private async Task BuildPostbackMessageModel(IConversationService conv, CallerMessage message) { - var messages = conv.GetDialogHistory(1); + var messages = await conv.GetDialogHistory(1); if (!messages.Any()) return null; var lastMessage = messages[0]; if (string.IsNullOrEmpty(lastMessage.PostbackFunctionName)) return null; @@ -135,7 +135,7 @@ private PostbackMessageModel BuildPostbackMessageModel(IConversationService conv }; } - private static void InitConversation(CallerMessage message, RoleDialogModel inputMsg, IConversationService conv, IRoutingService routing) + private static async Task InitConversation(CallerMessage message, RoleDialogModel inputMsg, IConversationService conv, IRoutingService routing) { routing.Context.SetMessageId(message.ConversationId, inputMsg.MessageId); var states = new List @@ -145,7 +145,7 @@ private static void InitConversation(CallerMessage message, RoleDialogModel inpu new("calling_phone", message.From) }; states.AddRange(message.States.Select(kvp => new MessageState(kvp.Key, kvp.Value))); - conv.SetConversationId(message.ConversationId, states); + await conv.SetConversationId(message.ConversationId, states); } private static async Task GetReplySpeechFileName(string conversationId, AssistantMessage reply, IServiceProvider sp) diff --git a/src/Plugins/BotSharp.Plugin.Twilio/TwilioStreamMiddleware.cs b/src/Plugins/BotSharp.Plugin.Twilio/TwilioStreamMiddleware.cs index 78bcafed1..a3a532684 100644 --- a/src/Plugins/BotSharp.Plugin.Twilio/TwilioStreamMiddleware.cs +++ b/src/Plugins/BotSharp.Plugin.Twilio/TwilioStreamMiddleware.cs @@ -77,7 +77,7 @@ private async Task HandleWebSocket(IServiceProvider services, string agentId, st // load conversation and state var convService = services.GetRequiredService(); - convService.SetConversationId(conversationId, []); + await convService.SetConversationId(conversationId, []); var hooks = services.GetHooks(agentId); foreach (var hook in hooks) @@ -85,7 +85,7 @@ private async Task HandleWebSocket(IServiceProvider services, string agentId, st await hook.OnStreamingStarted(conn); } - convService.States.Save(); + await convService.States.Save(); var routing = services.GetRequiredService(); routing.Context.Push(agentId); @@ -141,7 +141,7 @@ private async Task HandleWebSocket(IServiceProvider services, string agentId, st } } - convService.SaveStates(); + await convService.SaveStates(); await session.DisconnectAsync(); } @@ -275,7 +275,7 @@ private async Task HandleUserDtmfReceived(IServiceProvider _services, RealtimeHu dialogs.Add(message); var storage = _services.GetRequiredService(); - storage.Append(conn.ConversationId, message); + await storage.Append(conn.ConversationId, message); var hooks = _services.GetHooksOrderByPriority(conn.CurrentAgentId); foreach (var hook in hooks) diff --git a/src/Plugins/BotSharp.Plugin.WeChat/WeChatBackgroundService.cs b/src/Plugins/BotSharp.Plugin.WeChat/WeChatBackgroundService.cs index 2bb01d392..ee05f2b9d 100644 --- a/src/Plugins/BotSharp.Plugin.WeChat/WeChatBackgroundService.cs +++ b/src/Plugins/BotSharp.Plugin.WeChat/WeChatBackgroundService.cs @@ -57,7 +57,7 @@ private async Task HandleTextMessageAsync(string openid, string message) var routing = _service.GetRequiredService(); routing.Context.SetMessageId(latestConversationId, inputMsg.MessageId); - conversationService.SetConversationId(latestConversationId, new List + await conversationService.SetConversationId(latestConversationId, new List { new MessageState("channel", "wechat") });