Skip to content

add agent queue changed event #384

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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; }
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
42 changes: 36 additions & 6 deletions src/Plugins/BotSharp.Plugin.ChatHub/Hooks/StreamingLogHook.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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
{
Expand All @@ -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
{
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand Down