add agent queue changed event

This commit is contained in:
Jicheng Lu 2024-03-31 23:55:13 -05:00
parent e3e769ee80
commit a649b658e4
5 changed files with 52 additions and 14 deletions

View file

@ -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; }
}

View file

@ -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;

View file

@ -247,9 +247,13 @@ public class StreamingLogHook : ConversationHookBase, IContentGeneratingHook, IR
{
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 class StreamingLogHook : ConversationHookBase, IContentGeneratingHook, IR
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 class StreamingLogHook : ConversationHookBase, IContentGeneratingHook, IR
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 class StreamingLogHook : ConversationHookBase, IContentGeneratingHook, IR
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 @@ public class StreamingLogHook : ConversationHookBase, IContentGeneratingHook, IR
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);
}
}

View file

@ -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 class FunctionDefMongoElement
{
Name = function.Name,
Description = function.Description,
VisibilityExpression = function.VisibilityExpression,
Impact = function.Impact,
Parameters = new FunctionParametersDefMongoElement
{
@ -38,6 +40,7 @@ public class FunctionDefMongoElement
{
Name = mongoFunction.Name,
Description = mongoFunction.Description,
VisibilityExpression = mongoFunction.VisibilityExpression,
Impact = mongoFunction.Impact,
Parameters = new FunctionParametersDef
{

View file

@ -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;