OnConversationRedirected

This commit is contained in:
Haiping Chen 2024-02-22 15:14:13 -06:00
parent baae5bc938
commit f4b4ca4329
5 changed files with 43 additions and 9 deletions

View file

@ -91,4 +91,9 @@ public abstract class ConversationHookBase : IConversationHook
{
return Task.CompletedTask;
}
public virtual Task OnConversationRedirected(string toAgentId, RoleDialogModel message)
{
return Task.CompletedTask;
}
}

View file

@ -82,4 +82,12 @@ public interface IConversationHook
/// <param name="conversation"></param>
/// <returns></returns>
Task OnHumanInterventionNeeded(RoleDialogModel message);
/// <summary>
/// Conversation is redirected to another agent
/// </summary>
/// <param name="toAgentId"></param>
/// <param name="message"></param>
/// <returns></returns>
Task OnConversationRedirected(string toAgentId, RoleDialogModel message);
}

View file

@ -6,4 +6,5 @@ public static class ContentLogSource
public const string Prompt = "prompt";
public const string FunctionCall = "function call";
public const string AgentResponse = "agent response";
public const string HardRule = "hard rule";
}

View file

@ -153,6 +153,11 @@ public class RouteToAgentFn : IFunctionCallback
#else
logger.LogInformation($"*** Routing redirect to {record.Name.ToUpper()} ***");
#endif
var hooks = _services.GetServices<IConversationHook>();
foreach (var hook in hooks)
{
hook.OnConversationRedirected(routingRule.RedirectTo, message);
}
}
else
{

View file

@ -46,6 +46,18 @@ public class StreamingLogHook : ConversationHookBase, IContentGeneratingHook
BuildContentLog(conversationId, _user.UserName, log, ContentLogSource.UserInput, message));
}
public override async Task OnConversationRedirected(string toAgentId, RoleDialogModel message)
{
var agentService = _services.GetRequiredService<IAgentService>();
var conversationId = _state.GetConversationId();
var fromAgent = await agentService.LoadAgent(message.CurrentAgentId);
var toAgent = await agentService.LoadAgent(toAgentId);
var log = $"{message.Content}\r\n=====\r\nREDIRECTED TO {toAgent.Name}";
await _chatHub.Clients.User(_user.Id).SendAsync("OnConversationContentLogGenerated",
BuildContentLog(conversationId, fromAgent.Name, log, ContentLogSource.HardRule, message));
}
public async Task BeforeGenerating(Agent agent, List<RoleDialogModel> conversations)
{
if (!_convSettings.ShowVerboseLog) return;
@ -83,23 +95,26 @@ public class StreamingLogHook : ConversationHookBase, IContentGeneratingHook
var agent = await agentService.LoadAgent(message.CurrentAgentId);
var logSource = string.Empty;
var log = tokenStats.Prompt;
logSource = ContentLogSource.Prompt;
await _chatHub.Clients.User(_user.Id).SendAsync("OnConversationContentLogGenerated",
BuildContentLog(conversationId, agent?.Name, log, logSource, message));
// Log routing output
try
{
var inst = message.Content.JsonContent<FunctionCallFromLlm>();
logSource = ContentLogSource.AgentResponse;
await _chatHub.Clients.User(_user.Id).SendAsync("OnConversationContentLogGenerated",
BuildContentLog(conversationId, agent?.Name, message.Content, logSource, message));
if (!string.IsNullOrEmpty(inst.Function))
{
logSource = ContentLogSource.AgentResponse;
await _chatHub.Clients.User(_user.Id).SendAsync("OnConversationContentLogGenerated",
BuildContentLog(conversationId, agent?.Name, message.Content, logSource, message));
}
}
catch
{
// ignore
}
var log = tokenStats.Prompt;
logSource = ContentLogSource.Prompt;
await _chatHub.Clients.User(_user.Id).SendAsync("OnConversationContentLogGenerated",
BuildContentLog(conversationId, agent?.Name, log, logSource, message));
}
/// <summary>
@ -118,7 +133,7 @@ public class StreamingLogHook : ConversationHookBase, IContentGeneratingHook
{
var agentService = _services.GetRequiredService<IAgentService>();
var agent = await agentService.LoadAgent(message.CurrentAgentId);
var log = $"[{agent?.Name}]: {message.Content}";
var log = $"{message.Content}";
if (message.RichContent != null && message.RichContent.Message.RichType != "text")
{
var richContent = JsonSerializer.Serialize(message.RichContent, _serializerOptions);