Skip to content

Commit 8cc9776

Browse files
authored
Merge pull request #384 from iceljc/features/add-agent-queue-event
add agent queue changed event
2 parents e3e769e + a649b65 commit 8cc9776

File tree

5 files changed

+52
-14
lines changed

5 files changed

+52
-14
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
namespace BotSharp.Abstraction.Loggers.Models;
2+
3+
public class AgentQueueChangedLogModel
4+
{
5+
[JsonPropertyName("conversation_id")]
6+
public string ConversationId { get; set; }
7+
8+
[JsonPropertyName("log")]
9+
public string Log { get; set; }
10+
11+
[JsonPropertyName("created_at")]
12+
public DateTime CreateTime { get; set; }
13+
}

src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.RefreshAgents.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
using BotSharp.Abstraction.Agents.Models;
2-
using BotSharp.Abstraction.Repositories;
31
using BotSharp.Abstraction.Tasks.Models;
4-
using Microsoft.Extensions.Caching.Memory;
5-
using System.Collections.Generic;
62
using System.IO;
73

84
namespace BotSharp.Core.Agents.Services;

src/Plugins/BotSharp.Plugin.ChatHub/Hooks/StreamingLogHook.cs

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -247,9 +247,13 @@ public async Task OnAgentEnqueued(string agentId, string preAgentId, string? rea
247247
{
248248
var conversationId = _state.GetConversationId();
249249
var agent = await _agentService.LoadAgent(agentId);
250-
var preAgent = await _agentService.LoadAgent(preAgentId);
251250

252-
var log = $"{agent.Name} is enqueued{(reason != null ? $" ({reason})" : "")}";
251+
// Agent queue log
252+
var log = $"{agent.Name} is enqueued";
253+
await _chatHub.Clients.User(_user.Id).SendAsync("OnAgentQueueChanged", BuildAgentQueueChangedLog(conversationId, log));
254+
255+
// Content log
256+
log = $"{agent.Name} is enqueued{(reason != null ? $" ({reason})" : "")}";
253257
var message = new RoleDialogModel(AgentRole.System, log)
254258
{
255259
MessageId = _routingCtx.MessageId
@@ -270,7 +274,12 @@ public async Task OnAgentDequeued(string agentId, string currentAgentId, string?
270274
var agent = await _agentService.LoadAgent(agentId);
271275
var currentAgent = await _agentService.LoadAgent(currentAgentId);
272276

273-
var log = $"{agent.Name} is dequeued{(reason != null ? $" ({reason})" : "")}, current agent is {currentAgent?.Name}";
277+
// Agent queue log
278+
var log = $"{agent.Name} is dequeued";
279+
await _chatHub.Clients.User(_user.Id).SendAsync("OnAgentQueueChanged", BuildAgentQueueChangedLog(conversationId, log));
280+
281+
// Content log
282+
log = $"{agent.Name} is dequeued{(reason != null ? $" ({reason})" : "")}, current agent is {currentAgent?.Name}";
274283
var message = new RoleDialogModel(AgentRole.System, log)
275284
{
276285
MessageId = _routingCtx.MessageId
@@ -291,7 +300,12 @@ public async Task OnAgentReplaced(string fromAgentId, string toAgentId, string?
291300
var fromAgent = await _agentService.LoadAgent(fromAgentId);
292301
var toAgent = await _agentService.LoadAgent(toAgentId);
293302

294-
var log = $"{fromAgent.Name} is replaced to {toAgent.Name}{(reason != null ? $" ({reason})" : "")}";
303+
// Agent queue log
304+
var log = $"Agent queue is replaced from {fromAgent.Name} to {toAgent.Name}";
305+
await _chatHub.Clients.User(_user.Id).SendAsync("OnAgentQueueChanged", BuildAgentQueueChangedLog(conversationId, log));
306+
307+
// Content log
308+
log = $"{fromAgent.Name} is replaced to {toAgent.Name}{(reason != null ? $" ({reason})" : "")}";
295309
var message = new RoleDialogModel(AgentRole.System, log)
296310
{
297311
MessageId = _routingCtx.MessageId
@@ -309,9 +323,13 @@ public async Task OnAgentReplaced(string fromAgentId, string toAgentId, string?
309323
public async Task OnAgentQueueEmptied(string agentId, string? reason = null)
310324
{
311325
var conversationId = _state.GetConversationId();
312-
var agent = await _agentService.LoadAgent(agentId);
313326

314-
var log = reason ?? "Agent queue is cleared";
327+
// Agent queue log
328+
var log = $"Agent queue is empty";
329+
await _chatHub.Clients.User(_user.Id).SendAsync("OnAgentQueueChanged", BuildAgentQueueChangedLog(conversationId, log));
330+
331+
// Content log
332+
log = reason ?? "Agent queue is cleared";
315333
var message = new RoleDialogModel(AgentRole.System, log)
316334
{
317335
MessageId = _routingCtx.MessageId
@@ -423,4 +441,16 @@ private string BuildStateChangeLog(StateChangeModel stateChange)
423441

424442
return JsonSerializer.Serialize(log, _options.JsonSerializerOptions);
425443
}
444+
445+
private string BuildAgentQueueChangedLog(string conversationId, string log)
446+
{
447+
var model = new AgentQueueChangedLogModel
448+
{
449+
ConversationId = conversationId,
450+
Log = log,
451+
CreateTime = DateTime.UtcNow
452+
};
453+
454+
return JsonSerializer.Serialize(model, _options.JsonSerializerOptions);
455+
}
426456
}

src/Plugins/BotSharp.Plugin.MongoStorage/Models/FunctionDefMongoElement.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ public class FunctionDefMongoElement
88
{
99
public string Name { get; set; }
1010
public string Description { get; set; }
11+
public string? VisibilityExpression { get; set; }
1112
public string? Impact { get; set; }
1213
public FunctionParametersDefMongoElement Parameters { get; set; } = new FunctionParametersDefMongoElement();
1314

@@ -22,6 +23,7 @@ public static FunctionDefMongoElement ToMongoElement(FunctionDef function)
2223
{
2324
Name = function.Name,
2425
Description = function.Description,
26+
VisibilityExpression = function.VisibilityExpression,
2527
Impact = function.Impact,
2628
Parameters = new FunctionParametersDefMongoElement
2729
{
@@ -38,6 +40,7 @@ public static FunctionDef ToDomainElement(FunctionDefMongoElement mongoFunction)
3840
{
3941
Name = mongoFunction.Name,
4042
Description = mongoFunction.Description,
43+
VisibilityExpression = mongoFunction.VisibilityExpression,
4144
Impact = mongoFunction.Impact,
4245
Parameters = new FunctionParametersDef
4346
{

src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.Agent.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
using BotSharp.Abstraction.Agents.Models;
2-
using BotSharp.Abstraction.Conversations.Models;
3-
using BotSharp.Abstraction.Evaluations.Settings;
42
using BotSharp.Abstraction.Functions.Models;
53
using BotSharp.Abstraction.Repositories.Filters;
64
using BotSharp.Abstraction.Routing.Models;
7-
using BotSharp.Abstraction.Routing.Settings;
8-
using BotSharp.Abstraction.Tasks.Models;
95
using BotSharp.Plugin.MongoStorage.Collections;
106
using BotSharp.Plugin.MongoStorage.Models;
117

0 commit comments

Comments
 (0)