diff --git a/src/Infrastructure/BotSharp.Abstraction/Loggers/Models/AgentQueueChangedLogModel.cs b/src/Infrastructure/BotSharp.Abstraction/Loggers/Models/AgentQueueChangedLogModel.cs new file mode 100644 index 000000000..0fca07642 --- /dev/null +++ b/src/Infrastructure/BotSharp.Abstraction/Loggers/Models/AgentQueueChangedLogModel.cs @@ -0,0 +1,13 @@ +namespace BotSharp.Abstraction.Loggers.Models; + +public class AgentQueueChangedLogModel +{ + [JsonPropertyName("conversation_id")] + public string ConversationId { get; set; } + + [JsonPropertyName("log")] + public string Log { get; set; } + + [JsonPropertyName("created_at")] + public DateTime CreateTime { get; set; } +} diff --git a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.RefreshAgents.cs b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.RefreshAgents.cs index 2831a669e..9c3b34f9b 100644 --- a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.RefreshAgents.cs +++ b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.RefreshAgents.cs @@ -1,8 +1,4 @@ -using BotSharp.Abstraction.Agents.Models; -using BotSharp.Abstraction.Repositories; using BotSharp.Abstraction.Tasks.Models; -using Microsoft.Extensions.Caching.Memory; -using System.Collections.Generic; using System.IO; namespace BotSharp.Core.Agents.Services; diff --git a/src/Plugins/BotSharp.Plugin.ChatHub/Hooks/StreamingLogHook.cs b/src/Plugins/BotSharp.Plugin.ChatHub/Hooks/StreamingLogHook.cs index 320dd21fd..2162bcb97 100644 --- a/src/Plugins/BotSharp.Plugin.ChatHub/Hooks/StreamingLogHook.cs +++ b/src/Plugins/BotSharp.Plugin.ChatHub/Hooks/StreamingLogHook.cs @@ -247,9 +247,13 @@ public async Task OnAgentEnqueued(string agentId, string preAgentId, string? rea { var conversationId = _state.GetConversationId(); var agent = await _agentService.LoadAgent(agentId); - var preAgent = await _agentService.LoadAgent(preAgentId); - var log = $"{agent.Name} is enqueued{(reason != null ? $" ({reason})" : "")}"; + // Agent queue log + var log = $"{agent.Name} is enqueued"; + await _chatHub.Clients.User(_user.Id).SendAsync("OnAgentQueueChanged", BuildAgentQueueChangedLog(conversationId, log)); + + // Content log + log = $"{agent.Name} is enqueued{(reason != null ? $" ({reason})" : "")}"; var message = new RoleDialogModel(AgentRole.System, log) { MessageId = _routingCtx.MessageId @@ -270,7 +274,12 @@ public async Task OnAgentDequeued(string agentId, string currentAgentId, string? var agent = await _agentService.LoadAgent(agentId); var currentAgent = await _agentService.LoadAgent(currentAgentId); - var log = $"{agent.Name} is dequeued{(reason != null ? $" ({reason})" : "")}, current agent is {currentAgent?.Name}"; + // Agent queue log + var log = $"{agent.Name} is dequeued"; + await _chatHub.Clients.User(_user.Id).SendAsync("OnAgentQueueChanged", BuildAgentQueueChangedLog(conversationId, log)); + + // Content log + log = $"{agent.Name} is dequeued{(reason != null ? $" ({reason})" : "")}, current agent is {currentAgent?.Name}"; var message = new RoleDialogModel(AgentRole.System, log) { MessageId = _routingCtx.MessageId @@ -291,7 +300,12 @@ public async Task OnAgentReplaced(string fromAgentId, string toAgentId, string? var fromAgent = await _agentService.LoadAgent(fromAgentId); var toAgent = await _agentService.LoadAgent(toAgentId); - var log = $"{fromAgent.Name} is replaced to {toAgent.Name}{(reason != null ? $" ({reason})" : "")}"; + // Agent queue log + var log = $"Agent queue is replaced from {fromAgent.Name} to {toAgent.Name}"; + await _chatHub.Clients.User(_user.Id).SendAsync("OnAgentQueueChanged", BuildAgentQueueChangedLog(conversationId, log)); + + // Content log + log = $"{fromAgent.Name} is replaced to {toAgent.Name}{(reason != null ? $" ({reason})" : "")}"; var message = new RoleDialogModel(AgentRole.System, log) { MessageId = _routingCtx.MessageId @@ -309,9 +323,13 @@ public async Task OnAgentReplaced(string fromAgentId, string toAgentId, string? public async Task OnAgentQueueEmptied(string agentId, string? reason = null) { var conversationId = _state.GetConversationId(); - var agent = await _agentService.LoadAgent(agentId); - var log = reason ?? "Agent queue is cleared"; + // Agent queue log + var log = $"Agent queue is empty"; + await _chatHub.Clients.User(_user.Id).SendAsync("OnAgentQueueChanged", BuildAgentQueueChangedLog(conversationId, log)); + + // Content log + log = reason ?? "Agent queue is cleared"; var message = new RoleDialogModel(AgentRole.System, log) { MessageId = _routingCtx.MessageId @@ -423,4 +441,16 @@ private string BuildStateChangeLog(StateChangeModel stateChange) return JsonSerializer.Serialize(log, _options.JsonSerializerOptions); } + + private string BuildAgentQueueChangedLog(string conversationId, string log) + { + var model = new AgentQueueChangedLogModel + { + ConversationId = conversationId, + Log = log, + CreateTime = DateTime.UtcNow + }; + + return JsonSerializer.Serialize(model, _options.JsonSerializerOptions); + } } diff --git a/src/Plugins/BotSharp.Plugin.MongoStorage/Models/FunctionDefMongoElement.cs b/src/Plugins/BotSharp.Plugin.MongoStorage/Models/FunctionDefMongoElement.cs index 5910661fa..dac77d693 100644 --- a/src/Plugins/BotSharp.Plugin.MongoStorage/Models/FunctionDefMongoElement.cs +++ b/src/Plugins/BotSharp.Plugin.MongoStorage/Models/FunctionDefMongoElement.cs @@ -8,6 +8,7 @@ public class FunctionDefMongoElement { public string Name { get; set; } public string Description { get; set; } + public string? VisibilityExpression { get; set; } public string? Impact { get; set; } public FunctionParametersDefMongoElement Parameters { get; set; } = new FunctionParametersDefMongoElement(); @@ -22,6 +23,7 @@ public static FunctionDefMongoElement ToMongoElement(FunctionDef function) { Name = function.Name, Description = function.Description, + VisibilityExpression = function.VisibilityExpression, Impact = function.Impact, Parameters = new FunctionParametersDefMongoElement { @@ -38,6 +40,7 @@ public static FunctionDef ToDomainElement(FunctionDefMongoElement mongoFunction) { Name = mongoFunction.Name, Description = mongoFunction.Description, + VisibilityExpression = mongoFunction.VisibilityExpression, Impact = mongoFunction.Impact, Parameters = new FunctionParametersDef { diff --git a/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.Agent.cs b/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.Agent.cs index 75d496f88..3272930b3 100644 --- a/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.Agent.cs +++ b/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.Agent.cs @@ -1,11 +1,7 @@ using BotSharp.Abstraction.Agents.Models; -using BotSharp.Abstraction.Conversations.Models; -using BotSharp.Abstraction.Evaluations.Settings; using BotSharp.Abstraction.Functions.Models; using BotSharp.Abstraction.Repositories.Filters; using BotSharp.Abstraction.Routing.Models; -using BotSharp.Abstraction.Routing.Settings; -using BotSharp.Abstraction.Tasks.Models; using BotSharp.Plugin.MongoStorage.Collections; using BotSharp.Plugin.MongoStorage.Models;