Skip to content

Commit f4b4ca4

Browse files
author
Haiping Chen
committed
OnConversationRedirected
1 parent baae5bc commit f4b4ca4

File tree

5 files changed

+43
-9
lines changed

5 files changed

+43
-9
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,4 +91,9 @@ public virtual Task OnUserAgentConnectedInitially(Conversation conversation)
9191
{
9292
return Task.CompletedTask;
9393
}
94+
95+
public virtual Task OnConversationRedirected(string toAgentId, RoleDialogModel message)
96+
{
97+
return Task.CompletedTask;
98+
}
9499
}

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,12 @@ public interface IConversationHook
8282
/// <param name="conversation"></param>
8383
/// <returns></returns>
8484
Task OnHumanInterventionNeeded(RoleDialogModel message);
85+
86+
/// <summary>
87+
/// Conversation is redirected to another agent
88+
/// </summary>
89+
/// <param name="toAgentId"></param>
90+
/// <param name="message"></param>
91+
/// <returns></returns>
92+
Task OnConversationRedirected(string toAgentId, RoleDialogModel message);
8593
}

src/Infrastructure/BotSharp.Abstraction/Loggers/Enums/ContentLogSource.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ public static class ContentLogSource
66
public const string Prompt = "prompt";
77
public const string FunctionCall = "function call";
88
public const string AgentResponse = "agent response";
9+
public const string HardRule = "hard rule";
910
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,11 @@ private bool HasMissingRequiredField(RoleDialogModel message, out string agentId
153153
#else
154154
logger.LogInformation($"*** Routing redirect to {record.Name.ToUpper()} ***");
155155
#endif
156+
var hooks = _services.GetServices<IConversationHook>();
157+
foreach (var hook in hooks)
158+
{
159+
hook.OnConversationRedirected(routingRule.RedirectTo, message);
160+
}
156161
}
157162
else
158163
{

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

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,18 @@ await _chatHub.Clients.User(_user.Id).SendAsync("OnConversationContentLogGenerat
4646
BuildContentLog(conversationId, _user.UserName, log, ContentLogSource.UserInput, message));
4747
}
4848

49+
public override async Task OnConversationRedirected(string toAgentId, RoleDialogModel message)
50+
{
51+
var agentService = _services.GetRequiredService<IAgentService>();
52+
var conversationId = _state.GetConversationId();
53+
var fromAgent = await agentService.LoadAgent(message.CurrentAgentId);
54+
var toAgent = await agentService.LoadAgent(toAgentId);
55+
56+
var log = $"{message.Content}\r\n=====\r\nREDIRECTED TO {toAgent.Name}";
57+
await _chatHub.Clients.User(_user.Id).SendAsync("OnConversationContentLogGenerated",
58+
BuildContentLog(conversationId, fromAgent.Name, log, ContentLogSource.HardRule, message));
59+
}
60+
4961
public async Task BeforeGenerating(Agent agent, List<RoleDialogModel> conversations)
5062
{
5163
if (!_convSettings.ShowVerboseLog) return;
@@ -83,23 +95,26 @@ public async Task AfterGenerated(RoleDialogModel message, TokenStatsModel tokenS
8395
var agent = await agentService.LoadAgent(message.CurrentAgentId);
8496
var logSource = string.Empty;
8597

98+
var log = tokenStats.Prompt;
99+
logSource = ContentLogSource.Prompt;
100+
await _chatHub.Clients.User(_user.Id).SendAsync("OnConversationContentLogGenerated",
101+
BuildContentLog(conversationId, agent?.Name, log, logSource, message));
102+
86103
// Log routing output
87104
try
88105
{
89106
var inst = message.Content.JsonContent<FunctionCallFromLlm>();
90-
logSource = ContentLogSource.AgentResponse;
91-
await _chatHub.Clients.User(_user.Id).SendAsync("OnConversationContentLogGenerated",
92-
BuildContentLog(conversationId, agent?.Name, message.Content, logSource, message));
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+
}
93113
}
94114
catch
95115
{
96116
// ignore
97117
}
98-
99-
var log = tokenStats.Prompt;
100-
logSource = ContentLogSource.Prompt;
101-
await _chatHub.Clients.User(_user.Id).SendAsync("OnConversationContentLogGenerated",
102-
BuildContentLog(conversationId, agent?.Name, log, logSource, message));
103118
}
104119

105120
/// <summary>
@@ -118,7 +133,7 @@ public override async Task OnResponseGenerated(RoleDialogModel message)
118133
{
119134
var agentService = _services.GetRequiredService<IAgentService>();
120135
var agent = await agentService.LoadAgent(message.CurrentAgentId);
121-
var log = $"[{agent?.Name}]: {message.Content}";
136+
var log = $"{message.Content}";
122137
if (message.RichContent != null && message.RichContent.Message.RichType != "text")
123138
{
124139
var richContent = JsonSerializer.Serialize(message.RichContent, _serializerOptions);

0 commit comments

Comments
 (0)