Skip to content

Commit c67d0cc

Browse files
committed
Add hook of OnConversationRouting.
1 parent 7500546 commit c67d0cc

File tree

5 files changed

+57
-71
lines changed

5 files changed

+57
-71
lines changed
Lines changed: 18 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
using BotSharp.Abstraction.Functions.Models;
2+
13
namespace BotSharp.Abstraction.Conversations;
24

35
public abstract class ConversationHookBase : IConversationHook
@@ -27,19 +29,13 @@ public IConversationHook SetConversation(Conversation conversation)
2729
}
2830

2931
public virtual Task OnStateLoaded(ConversationState state)
30-
{
31-
return Task.CompletedTask;
32-
}
32+
=> Task.CompletedTask;
3333

3434
public virtual Task OnStateChanged(string name, string preValue, string currentValue)
35-
{
36-
return Task.CompletedTask;
37-
}
35+
=> Task.CompletedTask;
3836

3937
public virtual Task OnDialogRecordLoaded(RoleDialogModel dialog)
40-
{
41-
return Task.CompletedTask;
42-
}
38+
=> Task.CompletedTask;
4339

4440
public virtual Task OnDialogsLoaded(List<RoleDialogModel> dialogs)
4541
{
@@ -48,52 +44,35 @@ public virtual Task OnDialogsLoaded(List<RoleDialogModel> dialogs)
4844
}
4945

5046
public virtual Task OnConversationEnding(RoleDialogModel message)
51-
{
52-
return Task.CompletedTask;
53-
}
47+
=> Task.CompletedTask;
5448

5549
public virtual Task OnCurrentTaskEnding(RoleDialogModel message)
56-
{
57-
return Task.CompletedTask;
58-
}
50+
=> Task.CompletedTask;
5951

6052
public virtual Task OnHumanInterventionNeeded(RoleDialogModel message)
61-
{
62-
return Task.CompletedTask;
63-
}
53+
=> Task.CompletedTask;
6454

6555
public virtual Task OnFunctionExecuting(RoleDialogModel message)
66-
{
67-
return Task.CompletedTask;
68-
}
56+
=> Task.CompletedTask;
6957

7058
public virtual Task OnFunctionExecuted(RoleDialogModel message)
71-
{
72-
return Task.CompletedTask;
73-
}
59+
=> Task.CompletedTask;
7460

7561
public virtual Task OnMessageReceived(RoleDialogModel message)
76-
{
77-
return Task.CompletedTask;
78-
}
62+
=> Task.CompletedTask;
7963

8064
public virtual Task OnResponseGenerated(RoleDialogModel message)
81-
{
82-
return Task.CompletedTask;
83-
}
65+
=> Task.CompletedTask;
8466

8567
public virtual Task OnConversationInitialized(Conversation conversation)
86-
{
87-
return Task.CompletedTask;
88-
}
68+
=> Task.CompletedTask;
8969

9070
public virtual Task OnUserAgentConnectedInitially(Conversation conversation)
91-
{
92-
return Task.CompletedTask;
93-
}
71+
=> Task.CompletedTask;
9472

9573
public virtual Task OnConversationRedirected(string toAgentId, RoleDialogModel message)
96-
{
97-
return Task.CompletedTask;
98-
}
74+
=> Task.CompletedTask;
75+
76+
public virtual Task OnConversationRouting(FunctionCallFromLlm instruct, RoleDialogModel message)
77+
=> Task.CompletedTask;
9978
}

src/Infrastructure/BotSharp.Abstraction/Conversations/IConversationHook.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
using BotSharp.Abstraction.Functions.Models;
2+
13
namespace BotSharp.Abstraction.Conversations;
24

35
public interface IConversationHook
@@ -90,4 +92,12 @@ public interface IConversationHook
9092
/// <param name="message"></param>
9193
/// <returns></returns>
9294
Task OnConversationRedirected(string toAgentId, RoleDialogModel message);
95+
96+
/// <summary>
97+
/// Routing instruction is received from Router
98+
/// </summary>
99+
/// <param name="instruct">routing instruction</param>
100+
/// <param name="message">message</param>
101+
/// <returns></returns>
102+
Task OnConversationRouting(FunctionCallFromLlm instruct, RoleDialogModel message);
93103
}

src/Infrastructure/BotSharp.Core/Routing/Functions/RouteToAgentFn.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ private bool HasMissingRequiredField(RoleDialogModel message, out string agentId
156156
var hooks = _services.GetServices<IConversationHook>();
157157
foreach (var hook in hooks)
158158
{
159-
hook.OnConversationRedirected(routingRule.RedirectTo, message);
159+
hook.OnConversationRedirected(routingRule.RedirectTo, message).Wait();
160160
}
161161
}
162162
else

src/Infrastructure/BotSharp.Core/Routing/RoutingService.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,12 @@ public async Task<RoleDialogModel> InstructLoop(RoleDialogModel message)
9898
// Get instruction from Planner
9999
var inst = await planner.GetNextInstruction(_router, message.MessageId, dialogs);
100100

101+
var hooks = _services.GetServices<IConversationHook>();
102+
foreach (var hook in hooks)
103+
{
104+
await hook.OnConversationRouting(inst, message);
105+
}
106+
101107
// Save states
102108
states.SaveStateByArgs(inst.Arguments);
103109

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

Lines changed: 22 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
using BotSharp.Abstraction.Loggers.Models;
66
using BotSharp.Abstraction.Repositories;
77
using Microsoft.AspNetCore.SignalR;
8-
using Serilog;
98

109
namespace BotSharp.Plugin.ChatHub.Hooks;
1110

@@ -17,24 +16,28 @@ public class StreamingLogHook : ConversationHookBase, IContentGeneratingHook
1716
private readonly IHubContext<SignalRHub> _chatHub;
1817
private readonly IConversationStateService _state;
1918
private readonly IUserIdentity _user;
19+
private readonly IAgentService _agentService;
2020

2121
public StreamingLogHook(
2222
ConversationSetting convSettings,
2323
IServiceProvider serivces,
2424
IHubContext<SignalRHub> chatHub,
2525
IConversationStateService state,
26-
IUserIdentity user)
26+
IUserIdentity user,
27+
IAgentService agentService)
2728
{
2829
_convSettings = convSettings;
2930
_services = serivces;
3031
_chatHub = chatHub;
3132
_state = state;
3233
_user = user;
34+
_agentService = agentService;
3335
_serializerOptions = new JsonSerializerOptions
3436
{
3537
PropertyNameCaseInsensitive = true,
3638
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
37-
AllowTrailingCommas = true
39+
AllowTrailingCommas = true,
40+
WriteIndented = true
3841
};
3942
}
4043

@@ -46,12 +49,20 @@ await _chatHub.Clients.User(_user.Id).SendAsync("OnConversationContentLogGenerat
4649
BuildContentLog(conversationId, _user.UserName, log, ContentLogSource.UserInput, message));
4750
}
4851

52+
public override async Task OnConversationRouting(FunctionCallFromLlm instruct, RoleDialogModel message)
53+
{
54+
var conversationId = _state.GetConversationId();
55+
var agent = await _agentService.LoadAgent(message.CurrentAgentId);
56+
var log = JsonSerializer.Serialize(instruct, _serializerOptions);
57+
await _chatHub.Clients.User(_user.Id).SendAsync("OnConversationContentLogGenerated",
58+
BuildContentLog(conversationId, agent?.Name, log, ContentLogSource.AgentResponse, message));
59+
}
60+
4961
public override async Task OnConversationRedirected(string toAgentId, RoleDialogModel message)
5062
{
51-
var agentService = _services.GetRequiredService<IAgentService>();
5263
var conversationId = _state.GetConversationId();
53-
var fromAgent = await agentService.LoadAgent(message.CurrentAgentId);
54-
var toAgent = await agentService.LoadAgent(toAgentId);
64+
var fromAgent = await _agentService.LoadAgent(message.CurrentAgentId);
65+
var toAgent = await _agentService.LoadAgent(toAgentId);
5566

5667
var log = $"{message.Content}\r\n=====\r\nREDIRECTED TO {toAgent.Name}";
5768
await _chatHub.Clients.User(_user.Id).SendAsync("OnConversationContentLogGenerated",
@@ -71,10 +82,9 @@ public async Task BeforeGenerating(Agent agent, List<RoleDialogModel> conversati
7182

7283
public override async Task OnFunctionExecuted(RoleDialogModel message)
7384
{
74-
var agentService = _services.GetRequiredService<IAgentService>();
7585
var conversationId = _state.GetConversationId();
76-
var agent = await agentService.LoadAgent(message.CurrentAgentId);
77-
var log = $"[{agent?.Name}]: {message.FunctionName}({message.FunctionArgs}) => {message.Content}";
86+
var agent = await _agentService.LoadAgent(message.CurrentAgentId);
87+
var log = $"{message.FunctionName}({message.FunctionArgs})\r\n => {message.Content}";
7888
log += $"\r\n<== MessageId: {message.MessageId}";
7989
await _chatHub.Clients.User(_user.Id).SendAsync("OnConversationContentLogGenerated",
8090
BuildContentLog(conversationId, agent?.Name, log, ContentLogSource.FunctionCall, message));
@@ -90,31 +100,14 @@ public async Task AfterGenerated(RoleDialogModel message, TokenStatsModel tokenS
90100
{
91101
if (!_convSettings.ShowVerboseLog) return;
92102

93-
var agentService = _services.GetRequiredService<IAgentService>();
94103
var conversationId = _state.GetConversationId();
95-
var agent = await agentService.LoadAgent(message.CurrentAgentId);
104+
var agent = await _agentService.LoadAgent(message.CurrentAgentId);
96105
var logSource = string.Empty;
97106

98107
var log = tokenStats.Prompt;
99108
logSource = ContentLogSource.Prompt;
100109
await _chatHub.Clients.User(_user.Id).SendAsync("OnConversationContentLogGenerated",
101110
BuildContentLog(conversationId, agent?.Name, log, logSource, message));
102-
103-
// Log routing output
104-
try
105-
{
106-
var inst = message.Content.JsonContent<FunctionCallFromLlm>();
107-
if (!string.IsNullOrEmpty(inst.Function))
108-
{
109-
logSource = ContentLogSource.AgentResponse;
110-
await _chatHub.Clients.User(_user.Id).SendAsync("OnConversationContentLogGenerated",
111-
BuildContentLog(conversationId, agent?.Name, message.Content, logSource, message));
112-
}
113-
}
114-
catch
115-
{
116-
// ignore
117-
}
118111
}
119112

120113
/// <summary>
@@ -125,14 +118,12 @@ await _chatHub.Clients.User(_user.Id).SendAsync("OnConversationContentLogGenerat
125118
public override async Task OnResponseGenerated(RoleDialogModel message)
126119
{
127120
var conv = _services.GetRequiredService<IConversationService>();
128-
var state = _services.GetRequiredService<IConversationStateService>();
129121

130-
await _chatHub.Clients.User(_user.Id).SendAsync("OnConversateStateLogGenerated", BuildStateLog(conv.ConversationId, state.GetStates(), message));
122+
await _chatHub.Clients.User(_user.Id).SendAsync("OnConversateStateLogGenerated", BuildStateLog(conv.ConversationId, _state.GetStates(), message));
131123

132124
if (message.Role == AgentRole.Assistant)
133125
{
134-
var agentService = _services.GetRequiredService<IAgentService>();
135-
var agent = await agentService.LoadAgent(message.CurrentAgentId);
126+
var agent = await _agentService.LoadAgent(message.CurrentAgentId);
136127
var log = $"{message.Content}";
137128
if (message.RichContent != null && message.RichContent.Message.RichType != "text")
138129
{

0 commit comments

Comments
 (0)