Improve IRoutingHook.
This commit is contained in:
parent
6aa8e3dc83
commit
72c5ec7bd9
|
|
@ -23,13 +23,13 @@ public interface IConversationService
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="agentId"></param>
|
/// <param name="agentId"></param>
|
||||||
/// <param name="lastDalog"></param>
|
/// <param name="lastDalog"></param>
|
||||||
/// <param name="onMessageReceived"></param>
|
/// <param name="onResponseReceived">Received the response from AI Agent</param>
|
||||||
/// <param name="onFunctionExecuting">This delegate is useful when you want to report progress on UI</param>
|
/// <param name="onFunctionExecuting">This delegate is useful when you want to report progress on UI</param>
|
||||||
/// <param name="onFunctionExecuted">This delegate is useful when you want to report progress on UI</param>
|
/// <param name="onFunctionExecuted">This delegate is useful when you want to report progress on UI</param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
Task<bool> SendMessage(string agentId,
|
Task<bool> SendMessage(string agentId,
|
||||||
RoleDialogModel lastDalog,
|
RoleDialogModel lastDalog,
|
||||||
Func<RoleDialogModel, Task> onMessageReceived,
|
Func<RoleDialogModel, Task> onResponseReceived,
|
||||||
Func<RoleDialogModel, Task> onFunctionExecuting,
|
Func<RoleDialogModel, Task> onFunctionExecuting,
|
||||||
Func<RoleDialogModel, Task> onFunctionExecuted);
|
Func<RoleDialogModel, Task> onFunctionExecuted);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -3,11 +3,16 @@ namespace BotSharp.Abstraction.Routing;
|
||||||
public interface IRoutingContext
|
public interface IRoutingContext
|
||||||
{
|
{
|
||||||
string GetCurrentAgentId();
|
string GetCurrentAgentId();
|
||||||
|
string PreviousAgentId();
|
||||||
string OriginAgentId { get; }
|
string OriginAgentId { get; }
|
||||||
|
string ConversationId { get; }
|
||||||
|
string MessageId { get; }
|
||||||
|
void SetMessageId(string conversationId, string messageId);
|
||||||
bool IsEmpty { get; }
|
bool IsEmpty { get; }
|
||||||
string IntentName { get; set; }
|
string IntentName { get; set; }
|
||||||
void Push(string agentId);
|
int AgentCount { get; }
|
||||||
void Pop();
|
void Push(string agentId, string? reason = null);
|
||||||
void Replace(string agentId);
|
void Pop(string? reason = null);
|
||||||
void Empty();
|
void Replace(string agentId, string? reason = null);
|
||||||
|
void Empty(string? reason = null);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,27 +4,19 @@ namespace BotSharp.Abstraction.Routing;
|
||||||
|
|
||||||
public interface IRoutingHook
|
public interface IRoutingHook
|
||||||
{
|
{
|
||||||
/// <summary>
|
|
||||||
/// Conversation is redirected to another agent
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="toAgentId"></param>
|
|
||||||
/// <param name="message"></param>
|
|
||||||
/// <returns></returns>
|
|
||||||
Task OnConversationRedirected(string toAgentId, RoleDialogModel message);
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Routing instruction is received from Router
|
/// Routing instruction is received from Router
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="instruct">routing instruction</param>
|
/// <param name="instruct">routing instruction</param>
|
||||||
/// <param name="message">message</param>
|
/// <param name="message">message</param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
Task OnConversationRouting(FunctionCallFromLlm instruct, RoleDialogModel message);
|
Task OnRoutingInstructionReceived(FunctionCallFromLlm instruct, RoleDialogModel message);
|
||||||
|
|
||||||
Task OnAgentEnqueued(string agentId, string preAgentId);
|
Task OnAgentEnqueued(string agentId, string preAgentId, string? reason = null);
|
||||||
|
|
||||||
Task OnAgentDequeued(string agentId, string currentAgentId);
|
Task OnAgentDequeued(string agentId, string currentAgentId, string? reason = null);
|
||||||
|
|
||||||
Task OnAgentReplaced(string fromAgentId, string toAgentId);
|
Task OnAgentReplaced(string fromAgentId, string toAgentId, string? reason = null);
|
||||||
|
|
||||||
Task OnAgentQueueEmptied(string agentId);
|
Task OnAgentQueueEmptied(string agentId, string? reason = null);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@ namespace BotSharp.Abstraction.Routing;
|
||||||
public interface IRoutingService
|
public interface IRoutingService
|
||||||
{
|
{
|
||||||
Agent Router { get; }
|
Agent Router { get; }
|
||||||
|
IRoutingContext Context { get; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Get routable agents
|
/// Get routable agents
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,6 @@
|
||||||
using BotSharp.Abstraction.Agents.Models;
|
using BotSharp.Abstraction.Agents.Models;
|
||||||
using BotSharp.Abstraction.Messaging;
|
using BotSharp.Abstraction.Messaging;
|
||||||
using BotSharp.Abstraction.Messaging.Models.RichContent;
|
using BotSharp.Abstraction.Messaging.Models.RichContent;
|
||||||
using BotSharp.Abstraction.Routing;
|
|
||||||
using BotSharp.Abstraction.Routing.Settings;
|
using BotSharp.Abstraction.Routing.Settings;
|
||||||
using System.Drawing;
|
using System.Drawing;
|
||||||
|
|
||||||
|
|
@ -38,6 +37,11 @@ public partial class ConversationService
|
||||||
RoleDialogModel response = message;
|
RoleDialogModel response = message;
|
||||||
bool stopCompletion = false;
|
bool stopCompletion = false;
|
||||||
|
|
||||||
|
// Enqueue receiving agent first in case it stop completion by OnMessageReceived
|
||||||
|
var routing = _services.GetRequiredService<IRoutingService>();
|
||||||
|
routing.Context.SetMessageId(_conversationId, message.MessageId);
|
||||||
|
routing.Context.Push(agent.Id);
|
||||||
|
|
||||||
// Before chat completion hook
|
// Before chat completion hook
|
||||||
foreach (var hook in hooks)
|
foreach (var hook in hooks)
|
||||||
{
|
{
|
||||||
|
|
@ -50,13 +54,14 @@ public partial class ConversationService
|
||||||
if (message.StopCompletion)
|
if (message.StopCompletion)
|
||||||
{
|
{
|
||||||
stopCompletion = true;
|
stopCompletion = true;
|
||||||
|
routing.Context.Pop();
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!stopCompletion)
|
if (!stopCompletion)
|
||||||
{
|
{
|
||||||
// Routing with reasoning
|
// Routing with reasoning
|
||||||
var routing = _services.GetRequiredService<IRoutingService>();
|
|
||||||
var settings = _services.GetRequiredService<RoutingSettings>();
|
var settings = _services.GetRequiredService<RoutingSettings>();
|
||||||
|
|
||||||
response = agent.Type == AgentType.Routing ?
|
response = agent.Type == AgentType.Routing ?
|
||||||
|
|
@ -93,7 +98,7 @@ public partial class ConversationService
|
||||||
return converation;
|
return converation;
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task HandleAssistantMessage(RoleDialogModel response, Func<RoleDialogModel, Task> onMessageReceived)
|
private async Task HandleAssistantMessage(RoleDialogModel response, Func<RoleDialogModel, Task> onResponseReceived)
|
||||||
{
|
{
|
||||||
var agentService = _services.GetRequiredService<IAgentService>();
|
var agentService = _services.GetRequiredService<IAgentService>();
|
||||||
var agent = await agentService.GetAgent(response.CurrentAgentId);
|
var agent = await agentService.GetAgent(response.CurrentAgentId);
|
||||||
|
|
@ -130,7 +135,7 @@ public partial class ConversationService
|
||||||
await hook.OnResponseGenerated(response);
|
await hook.OnResponseGenerated(response);
|
||||||
}
|
}
|
||||||
|
|
||||||
await onMessageReceived(response);
|
await onResponseReceived(response);
|
||||||
|
|
||||||
// Add to dialog history
|
// Add to dialog history
|
||||||
_storage.Append(_conversationId, response);
|
_storage.Append(_conversationId, response);
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,3 @@
|
||||||
using BotSharp.Abstraction.Repositories;
|
|
||||||
using System.Linq;
|
|
||||||
|
|
||||||
namespace BotSharp.Core.Conversations.Services;
|
namespace BotSharp.Core.Conversations.Services;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|
@ -26,7 +23,6 @@ public class ConversationStateService : IConversationStateService, IDisposable
|
||||||
|
|
||||||
public string GetConversationId() => _conversationId;
|
public string GetConversationId() => _conversationId;
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Set conversation state
|
/// Set conversation state
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|
|
||||||
|
|
@ -25,22 +25,49 @@ public class RouteToAgentFn : IFunctionCallback
|
||||||
public async Task<bool> Execute(RoleDialogModel message)
|
public async Task<bool> Execute(RoleDialogModel message)
|
||||||
{
|
{
|
||||||
var args = JsonSerializer.Deserialize<RoutingArgs>(message.FunctionArgs);
|
var args = JsonSerializer.Deserialize<RoutingArgs>(message.FunctionArgs);
|
||||||
|
var states = _services.GetRequiredService<IConversationStateService>();
|
||||||
|
|
||||||
// Push original task agent
|
// Push original task agent
|
||||||
if (!string.IsNullOrEmpty(args.OriginalAgent) && args.OriginalAgent.Length < 32)
|
if (!string.IsNullOrEmpty(args.OriginalAgent) && args.OriginalAgent.Length < 32)
|
||||||
{
|
{
|
||||||
|
// Correct user goal agent to keep orignal task
|
||||||
|
var goalAgentInState = states.GetState("user_goal_agent", string.Empty);
|
||||||
|
if (goalAgentInState == string.Empty)
|
||||||
|
{
|
||||||
|
states.SetState("user_goal_agent", args.OriginalAgent, isNeedVersion: true);
|
||||||
|
}
|
||||||
|
else if (args.OriginalAgent == args.AgentName && args.OriginalAgent != goalAgentInState)
|
||||||
|
{
|
||||||
|
// Correct to original agent
|
||||||
|
args.OriginalAgent = goalAgentInState;
|
||||||
|
}
|
||||||
|
else if (args.OriginalAgent != args.AgentName && args.OriginalAgent != goalAgentInState)
|
||||||
|
{
|
||||||
|
// Correct to original agent
|
||||||
|
states.SetState("user_goal_agent", args.OriginalAgent, isNeedVersion: true);
|
||||||
|
}
|
||||||
|
|
||||||
var db = _services.GetRequiredService<IBotSharpRepository>();
|
var db = _services.GetRequiredService<IBotSharpRepository>();
|
||||||
var filter = new AgentFilter { AgentName = args.OriginalAgent };
|
var filter = new AgentFilter { AgentName = args.OriginalAgent };
|
||||||
var originalAgent = db.GetAgents(filter).FirstOrDefault();
|
var originalAgent = db.GetAgents(filter).FirstOrDefault();
|
||||||
if (originalAgent != null)
|
if (originalAgent != null)
|
||||||
{
|
{
|
||||||
_context.Push(originalAgent.Id);
|
_context.Push(originalAgent.Id, $"user goal agent");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
|
||||||
|
// Push next action agent
|
||||||
|
if (!string.IsNullOrEmpty(args.AgentName) && args.AgentName.Length < 32)
|
||||||
{
|
{
|
||||||
// Push current agent to routing stack
|
var db = _services.GetRequiredService<IBotSharpRepository>();
|
||||||
_context.Push(message.CurrentAgentId);
|
var filter = new AgentFilter { AgentName = args.AgentName };
|
||||||
|
var actionAgent = db.GetAgents(filter).FirstOrDefault();
|
||||||
|
if (actionAgent != null)
|
||||||
|
{
|
||||||
|
_context.Push(actionAgent.Id, args.Reason);
|
||||||
|
}
|
||||||
|
|
||||||
|
states.SetState("last_action_agent", args.AgentName, isNeedVersion: true);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (string.IsNullOrEmpty(args.AgentName))
|
if (string.IsNullOrEmpty(args.AgentName))
|
||||||
|
|
@ -67,17 +94,11 @@ public class RouteToAgentFn : IFunctionCallback
|
||||||
if (missingfield && message.CurrentAgentId != agentId)
|
if (missingfield && message.CurrentAgentId != agentId)
|
||||||
{
|
{
|
||||||
// Stack original Agent
|
// Stack original Agent
|
||||||
_context.Push(targetAgent.Id);
|
_context.Push(agentId, reason: "redirection rule");
|
||||||
|
|
||||||
message.CurrentAgentId = agentId;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
message.CurrentAgentId = targetAgent.Id;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
_context.Push(message.CurrentAgentId);
|
message.CurrentAgentId = _context.GetCurrentAgentId();
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
@ -153,9 +174,6 @@ public class RouteToAgentFn : IFunctionCallback
|
||||||
#else
|
#else
|
||||||
logger.LogInformation($"*** Routing redirect to {record.Name.ToUpper()} ***");
|
logger.LogInformation($"*** Routing redirect to {record.Name.ToUpper()} ***");
|
||||||
#endif
|
#endif
|
||||||
HookEmitter.Emit<IRoutingHook>(_services, async hook =>
|
|
||||||
await hook.OnConversationRedirected(routingRule.RedirectTo, message)
|
|
||||||
).Wait();
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -85,7 +85,7 @@ public class HFPlanner : IPlaner
|
||||||
public async Task<bool> AgentExecuted(Agent router, FunctionCallFromLlm inst, RoleDialogModel message, List<RoleDialogModel> dialogs)
|
public async Task<bool> AgentExecuted(Agent router, FunctionCallFromLlm inst, RoleDialogModel message, List<RoleDialogModel> dialogs)
|
||||||
{
|
{
|
||||||
var context = _services.GetRequiredService<IRoutingContext>();
|
var context = _services.GetRequiredService<IRoutingContext>();
|
||||||
context.Empty();
|
context.Empty(reason: $"Agent queue is cleared by {nameof(HFPlanner)}");
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -101,7 +101,7 @@ public class NaivePlanner : IPlaner
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
context.Empty();
|
context.Empty(reason: $"Agent queue is cleared by {nameof(NaivePlanner)}");
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -144,7 +144,7 @@ public class SequentialPlanner : IPlaner
|
||||||
|
|
||||||
if (message.StopCompletion)
|
if (message.StopCompletion)
|
||||||
{
|
{
|
||||||
context.Empty();
|
context.Empty(reason: $"Agent queue is cleared by {nameof(SequentialPlanner)}");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -159,7 +159,7 @@ public partial class TwoStagePlanner : IPlaner
|
||||||
|
|
||||||
if (message.StopCompletion || _isTaskCompleted)
|
if (message.StopCompletion || _isTaskCompleted)
|
||||||
{
|
{
|
||||||
context.Empty();
|
context.Empty(reason: $"Agent queue is cleared by {nameof(TwoStagePlanner)}");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,3 @@
|
||||||
using BotSharp.Abstraction.Repositories.Filters;
|
|
||||||
using BotSharp.Abstraction.Routing.Settings;
|
using BotSharp.Abstraction.Routing.Settings;
|
||||||
|
|
||||||
namespace BotSharp.Core.Routing;
|
namespace BotSharp.Core.Routing;
|
||||||
|
|
@ -8,6 +7,8 @@ public class RoutingContext : IRoutingContext
|
||||||
private readonly IServiceProvider _services;
|
private readonly IServiceProvider _services;
|
||||||
private readonly RoutingSettings _setting;
|
private readonly RoutingSettings _setting;
|
||||||
private string[] _routerAgentIds;
|
private string[] _routerAgentIds;
|
||||||
|
private string _conversationId;
|
||||||
|
private string _messageId;
|
||||||
|
|
||||||
public RoutingContext(IServiceProvider services, RoutingSettings setting)
|
public RoutingContext(IServiceProvider services, RoutingSettings setting)
|
||||||
{
|
{
|
||||||
|
|
@ -15,6 +16,10 @@ public class RoutingContext : IRoutingContext
|
||||||
_setting = setting;
|
_setting = setting;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public int AgentCount => _stack.Count;
|
||||||
|
public string ConversationId => _conversationId;
|
||||||
|
public string MessageId => _messageId;
|
||||||
|
|
||||||
private Stack<string> _stack { get; set; }
|
private Stack<string> _stack { get; set; }
|
||||||
= new Stack<string>();
|
= new Stack<string>();
|
||||||
|
|
||||||
|
|
@ -45,12 +50,13 @@ public class RoutingContext : IRoutingContext
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool IsEmpty => !_stack.Any();
|
public bool IsEmpty => !_stack.Any();
|
||||||
|
|
||||||
public string GetCurrentAgentId()
|
public string GetCurrentAgentId()
|
||||||
{
|
{
|
||||||
return _stack.Peek();
|
return _stack.Peek();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Push(string agentId)
|
public void Push(string agentId, string? reason = null)
|
||||||
{
|
{
|
||||||
if (_stack.Count == 0 || _stack.Peek() != agentId)
|
if (_stack.Count == 0 || _stack.Peek() != agentId)
|
||||||
{
|
{
|
||||||
|
|
@ -58,7 +64,7 @@ public class RoutingContext : IRoutingContext
|
||||||
_stack.Push(agentId);
|
_stack.Push(agentId);
|
||||||
|
|
||||||
HookEmitter.Emit<IRoutingHook>(_services, async hook =>
|
HookEmitter.Emit<IRoutingHook>(_services, async hook =>
|
||||||
await hook.OnAgentEnqueued(agentId, preAgentId)
|
await hook.OnAgentEnqueued(agentId, preAgentId, reason: reason)
|
||||||
).Wait();
|
).Wait();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -66,7 +72,7 @@ public class RoutingContext : IRoutingContext
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Pop current agent
|
/// Pop current agent
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public void Pop()
|
public void Pop(string? reason = null)
|
||||||
{
|
{
|
||||||
if (_stack.Count == 0)
|
if (_stack.Count == 0)
|
||||||
{
|
{
|
||||||
|
|
@ -76,11 +82,25 @@ public class RoutingContext : IRoutingContext
|
||||||
var agentId = _stack.Pop();
|
var agentId = _stack.Pop();
|
||||||
|
|
||||||
HookEmitter.Emit<IRoutingHook>(_services, async hook =>
|
HookEmitter.Emit<IRoutingHook>(_services, async hook =>
|
||||||
await hook.OnAgentDequeued(agentId, _stack.Peek())
|
await hook.OnAgentDequeued(agentId, _stack.Peek(), reason: reason)
|
||||||
).Wait();
|
).Wait();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Replace(string agentId)
|
public string PreviousAgentId()
|
||||||
|
{
|
||||||
|
if (_stack.Count == 1)
|
||||||
|
{
|
||||||
|
return GetCurrentAgentId();
|
||||||
|
}
|
||||||
|
else if (_stack.Count > 1)
|
||||||
|
{
|
||||||
|
return _stack.ToArray()[1];
|
||||||
|
}
|
||||||
|
|
||||||
|
return string.Empty;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Replace(string agentId, string? reason = null)
|
||||||
{
|
{
|
||||||
var fromAgent = agentId;
|
var fromAgent = agentId;
|
||||||
var toAgent = agentId;
|
var toAgent = agentId;
|
||||||
|
|
@ -96,12 +116,12 @@ public class RoutingContext : IRoutingContext
|
||||||
_stack.Push(agentId);
|
_stack.Push(agentId);
|
||||||
|
|
||||||
HookEmitter.Emit<IRoutingHook>(_services, async hook =>
|
HookEmitter.Emit<IRoutingHook>(_services, async hook =>
|
||||||
await hook.OnAgentReplaced(fromAgent, toAgent)
|
await hook.OnAgentReplaced(fromAgent, toAgent, reason: reason)
|
||||||
).Wait();
|
).Wait();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Empty()
|
public void Empty(string? reason = null)
|
||||||
{
|
{
|
||||||
if (_stack.Count == 0)
|
if (_stack.Count == 0)
|
||||||
{
|
{
|
||||||
|
|
@ -111,7 +131,13 @@ public class RoutingContext : IRoutingContext
|
||||||
var agentId = GetCurrentAgentId();
|
var agentId = GetCurrentAgentId();
|
||||||
_stack.Clear();
|
_stack.Clear();
|
||||||
HookEmitter.Emit<IRoutingHook>(_services, async hook =>
|
HookEmitter.Emit<IRoutingHook>(_services, async hook =>
|
||||||
await hook.OnAgentQueueEmptied(agentId)
|
await hook.OnAgentQueueEmptied(agentId, reason: reason)
|
||||||
).Wait();
|
).Wait();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void SetMessageId(string conversationId, string messageId)
|
||||||
|
{
|
||||||
|
_conversationId = conversationId;
|
||||||
|
_messageId = messageId;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,3 @@
|
||||||
using BotSharp.Abstraction.Agents.Models;
|
|
||||||
using BotSharp.Abstraction.Functions.Models;
|
|
||||||
using BotSharp.Abstraction.Repositories;
|
|
||||||
using BotSharp.Abstraction.Repositories.Filters;
|
|
||||||
using BotSharp.Abstraction.Routing;
|
|
||||||
using BotSharp.Abstraction.Routing.Models;
|
using BotSharp.Abstraction.Routing.Models;
|
||||||
using BotSharp.Abstraction.Routing.Planning;
|
using BotSharp.Abstraction.Routing.Planning;
|
||||||
using BotSharp.Abstraction.Routing.Settings;
|
using BotSharp.Abstraction.Routing.Settings;
|
||||||
|
|
@ -14,8 +9,11 @@ public partial class RoutingService : IRoutingService
|
||||||
{
|
{
|
||||||
private readonly IServiceProvider _services;
|
private readonly IServiceProvider _services;
|
||||||
private readonly RoutingSettings _settings;
|
private readonly RoutingSettings _settings;
|
||||||
|
private readonly IRoutingContext _context;
|
||||||
private readonly ILogger _logger;
|
private readonly ILogger _logger;
|
||||||
private Agent _router;
|
private Agent _router;
|
||||||
|
|
||||||
|
public IRoutingContext Context => _context;
|
||||||
public Agent Router => _router;
|
public Agent Router => _router;
|
||||||
|
|
||||||
public void ResetRecursiveCounter()
|
public void ResetRecursiveCounter()
|
||||||
|
|
@ -25,10 +23,12 @@ public partial class RoutingService : IRoutingService
|
||||||
|
|
||||||
public RoutingService(IServiceProvider services,
|
public RoutingService(IServiceProvider services,
|
||||||
RoutingSettings settings,
|
RoutingSettings settings,
|
||||||
|
IRoutingContext context,
|
||||||
ILogger<RoutingService> logger)
|
ILogger<RoutingService> logger)
|
||||||
{
|
{
|
||||||
_services = services;
|
_services = services;
|
||||||
_settings = settings;
|
_settings = settings;
|
||||||
|
_context = context;
|
||||||
_logger = logger;
|
_logger = logger;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -80,15 +80,14 @@ public partial class RoutingService : IRoutingService
|
||||||
var conv = _services.GetRequiredService<IConversationService>();
|
var conv = _services.GetRequiredService<IConversationService>();
|
||||||
var dialogs = conv.GetDialogHistory();
|
var dialogs = conv.GetDialogHistory();
|
||||||
|
|
||||||
var context = _services.GetRequiredService<IRoutingContext>();
|
|
||||||
var executor = _services.GetRequiredService<IExecutor>();
|
var executor = _services.GetRequiredService<IExecutor>();
|
||||||
|
|
||||||
var planner = GetPlanner(_router);
|
var planner = GetPlanner(_router);
|
||||||
|
|
||||||
context.Push(_router.Id);
|
_context.Push(_router.Id);
|
||||||
|
|
||||||
int loopCount = 0;
|
int loopCount = 0;
|
||||||
while (loopCount < planner.MaxLoopCount && !context.IsEmpty)
|
while (loopCount < planner.MaxLoopCount && !_context.IsEmpty)
|
||||||
{
|
{
|
||||||
loopCount++;
|
loopCount++;
|
||||||
|
|
||||||
|
|
@ -99,7 +98,7 @@ public partial class RoutingService : IRoutingService
|
||||||
var inst = await planner.GetNextInstruction(_router, message.MessageId, dialogs);
|
var inst = await planner.GetNextInstruction(_router, message.MessageId, dialogs);
|
||||||
|
|
||||||
await HookEmitter.Emit<IRoutingHook>(_services, async hook =>
|
await HookEmitter.Emit<IRoutingHook>(_services, async hook =>
|
||||||
await hook.OnConversationRouting(inst, message)
|
await hook.OnRoutingInstructionReceived(inst, message)
|
||||||
);
|
);
|
||||||
|
|
||||||
// Save states
|
// Save states
|
||||||
|
|
|
||||||
|
|
@ -18,6 +18,10 @@ global using BotSharp.Abstraction.Conversations.Models;
|
||||||
global using BotSharp.Abstraction.Agents.Settings;
|
global using BotSharp.Abstraction.Agents.Settings;
|
||||||
global using BotSharp.Abstraction.Conversations.Settings;
|
global using BotSharp.Abstraction.Conversations.Settings;
|
||||||
global using BotSharp.Abstraction.Agents.Enums;
|
global using BotSharp.Abstraction.Agents.Enums;
|
||||||
|
global using BotSharp.Abstraction.Agents.Models;
|
||||||
|
global using BotSharp.Abstraction.Functions.Models;
|
||||||
|
global using BotSharp.Abstraction.Repositories;
|
||||||
|
global using BotSharp.Abstraction.Repositories.Filters;
|
||||||
global using BotSharp.Core.Repository;
|
global using BotSharp.Core.Repository;
|
||||||
global using BotSharp.Core.Routing;
|
global using BotSharp.Core.Routing;
|
||||||
global using BotSharp.Core.Agents.Services;
|
global using BotSharp.Core.Agents.Services;
|
||||||
|
|
|
||||||
|
|
@ -18,7 +18,7 @@ public class StreamingLogHook : ConversationHookBase, IContentGeneratingHook, IR
|
||||||
private readonly IConversationStateService _state;
|
private readonly IConversationStateService _state;
|
||||||
private readonly IUserIdentity _user;
|
private readonly IUserIdentity _user;
|
||||||
private readonly IAgentService _agentService;
|
private readonly IAgentService _agentService;
|
||||||
private string _messageId;
|
private readonly IRoutingContext _routingCtx;
|
||||||
|
|
||||||
public StreamingLogHook(
|
public StreamingLogHook(
|
||||||
ConversationSetting convSettings,
|
ConversationSetting convSettings,
|
||||||
|
|
@ -26,7 +26,8 @@ public class StreamingLogHook : ConversationHookBase, IContentGeneratingHook, IR
|
||||||
IHubContext<SignalRHub> chatHub,
|
IHubContext<SignalRHub> chatHub,
|
||||||
IConversationStateService state,
|
IConversationStateService state,
|
||||||
IUserIdentity user,
|
IUserIdentity user,
|
||||||
IAgentService agentService)
|
IAgentService agentService,
|
||||||
|
IRoutingContext routingCtx)
|
||||||
{
|
{
|
||||||
_convSettings = convSettings;
|
_convSettings = convSettings;
|
||||||
_services = serivces;
|
_services = serivces;
|
||||||
|
|
@ -34,6 +35,7 @@ public class StreamingLogHook : ConversationHookBase, IContentGeneratingHook, IR
|
||||||
_state = state;
|
_state = state;
|
||||||
_user = user;
|
_user = user;
|
||||||
_agentService = agentService;
|
_agentService = agentService;
|
||||||
|
_routingCtx = routingCtx;
|
||||||
_serializerOptions = new JsonSerializerOptions
|
_serializerOptions = new JsonSerializerOptions
|
||||||
{
|
{
|
||||||
PropertyNameCaseInsensitive = true,
|
PropertyNameCaseInsensitive = true,
|
||||||
|
|
@ -45,7 +47,6 @@ public class StreamingLogHook : ConversationHookBase, IContentGeneratingHook, IR
|
||||||
|
|
||||||
public override async Task OnMessageReceived(RoleDialogModel message)
|
public override async Task OnMessageReceived(RoleDialogModel message)
|
||||||
{
|
{
|
||||||
_messageId = message.MessageId;
|
|
||||||
var conversationId = _state.GetConversationId();
|
var conversationId = _state.GetConversationId();
|
||||||
var log = $"{message.Content}";
|
var log = $"{message.Content}";
|
||||||
await _chatHub.Clients.User(_user.Id).SendAsync("OnConversationContentLogGenerated",
|
await _chatHub.Clients.User(_user.Id).SendAsync("OnConversationContentLogGenerated",
|
||||||
|
|
@ -163,79 +164,68 @@ public class StreamingLogHook : ConversationHookBase, IContentGeneratingHook, IR
|
||||||
}
|
}
|
||||||
|
|
||||||
#region IRoutingHook
|
#region IRoutingHook
|
||||||
public async Task OnAgentEnqueued(string agentId, string preAgentId)
|
public async Task OnAgentEnqueued(string agentId, string preAgentId, string? reason = null)
|
||||||
{
|
{
|
||||||
var conversationId = _state.GetConversationId();
|
var conversationId = _state.GetConversationId();
|
||||||
var agent = await _agentService.LoadAgent(agentId);
|
var agent = await _agentService.LoadAgent(agentId);
|
||||||
var preAgent = await _agentService.LoadAgent(preAgentId);
|
var preAgent = await _agentService.LoadAgent(preAgentId);
|
||||||
|
|
||||||
var log = $"{agent.Name} is enqueued";
|
var log = $"{agent.Name} is enqueued{(reason != null ? $" ({reason})" : "")}";
|
||||||
await _chatHub.Clients.User(_user.Id).SendAsync("OnConversationContentLogGenerated",
|
await _chatHub.Clients.User(_user.Id).SendAsync("OnConversationContentLogGenerated",
|
||||||
BuildContentLog(conversationId, preAgent.Name, log, ContentLogSource.HardRule, new RoleDialogModel(AgentRole.System, log)
|
BuildContentLog(conversationId, "Router", log, ContentLogSource.HardRule, new RoleDialogModel(AgentRole.System, log)
|
||||||
{
|
{
|
||||||
MessageId = _messageId
|
MessageId = _routingCtx.MessageId
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task OnAgentDequeued(string agentId, string currentAgentId)
|
public async Task OnAgentDequeued(string agentId, string currentAgentId, string? reason = null)
|
||||||
{
|
{
|
||||||
var conversationId = _state.GetConversationId();
|
var conversationId = _state.GetConversationId();
|
||||||
var agent = await _agentService.LoadAgent(agentId);
|
var agent = await _agentService.LoadAgent(agentId);
|
||||||
var currentAgent = await _agentService.LoadAgent(currentAgentId);
|
var currentAgent = await _agentService.LoadAgent(currentAgentId);
|
||||||
|
|
||||||
var log = $"{agent.Name} is dequeued";
|
var log = $"{agent.Name} is dequeued{(reason != null ? $" ({reason})" : "")}, current agent is {currentAgent.Name}";
|
||||||
await _chatHub.Clients.User(_user.Id).SendAsync("OnConversationContentLogGenerated",
|
await _chatHub.Clients.User(_user.Id).SendAsync("OnConversationContentLogGenerated",
|
||||||
BuildContentLog(conversationId, agent.Name, log, ContentLogSource.HardRule, new RoleDialogModel(AgentRole.System, log)
|
BuildContentLog(conversationId, "Router", log, ContentLogSource.HardRule, new RoleDialogModel(AgentRole.System, log)
|
||||||
{
|
{
|
||||||
MessageId = _messageId
|
MessageId = _routingCtx.MessageId
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task OnAgentReplaced(string fromAgentId, string toAgentId)
|
public async Task OnAgentReplaced(string fromAgentId, string toAgentId, string? reason = null)
|
||||||
{
|
{
|
||||||
var conversationId = _state.GetConversationId();
|
var conversationId = _state.GetConversationId();
|
||||||
var fromAgent = await _agentService.LoadAgent(fromAgentId);
|
var fromAgent = await _agentService.LoadAgent(fromAgentId);
|
||||||
var toAgent = await _agentService.LoadAgent(toAgentId);
|
var toAgent = await _agentService.LoadAgent(toAgentId);
|
||||||
|
|
||||||
var log = $"{fromAgent.Name} is replaced to {toAgent.Name}";
|
var log = $"{fromAgent.Name} is replaced to {toAgent.Name}{(reason != null ? $" ({reason})" : "")}";
|
||||||
await _chatHub.Clients.User(_user.Id).SendAsync("OnConversationContentLogGenerated",
|
await _chatHub.Clients.User(_user.Id).SendAsync("OnConversationContentLogGenerated",
|
||||||
BuildContentLog(conversationId, toAgent.Name, log, ContentLogSource.HardRule, new RoleDialogModel(AgentRole.System, log)
|
BuildContentLog(conversationId, "Router", log, ContentLogSource.HardRule, new RoleDialogModel(AgentRole.System, log)
|
||||||
{
|
{
|
||||||
MessageId = _messageId
|
MessageId = _routingCtx.MessageId
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task OnAgentQueueEmptied(string agentId)
|
public async Task OnAgentQueueEmptied(string agentId, string? reason = null)
|
||||||
{
|
{
|
||||||
var conversationId = _state.GetConversationId();
|
var conversationId = _state.GetConversationId();
|
||||||
var agent = await _agentService.LoadAgent(agentId);
|
var agent = await _agentService.LoadAgent(agentId);
|
||||||
|
|
||||||
var log = $"Agent queue is cleared.";
|
var log = reason ?? "Agent queue is cleared";
|
||||||
await _chatHub.Clients.User(_user.Id).SendAsync("OnConversationContentLogGenerated",
|
await _chatHub.Clients.User(_user.Id).SendAsync("OnConversationContentLogGenerated",
|
||||||
BuildContentLog(conversationId, agent.Name, log, ContentLogSource.HardRule, new RoleDialogModel(AgentRole.System, log)
|
BuildContentLog(conversationId, "Router", log, ContentLogSource.HardRule, new RoleDialogModel(AgentRole.System, log)
|
||||||
{
|
{
|
||||||
MessageId = _messageId
|
MessageId = _routingCtx.MessageId
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task OnConversationRouting(FunctionCallFromLlm instruct, RoleDialogModel message)
|
public async Task OnRoutingInstructionReceived(FunctionCallFromLlm instruct, RoleDialogModel message)
|
||||||
{
|
{
|
||||||
var conversationId = _state.GetConversationId();
|
var conversationId = _state.GetConversationId();
|
||||||
var agent = await _agentService.LoadAgent(message.CurrentAgentId);
|
var agent = await _agentService.LoadAgent(message.CurrentAgentId);
|
||||||
var log = JsonSerializer.Serialize(instruct, _serializerOptions);
|
var log = JsonSerializer.Serialize(instruct, _serializerOptions);
|
||||||
await _chatHub.Clients.User(_user.Id).SendAsync("OnConversationContentLogGenerated",
|
await _chatHub.Clients.User(_user.Id).SendAsync("OnConversationContentLogGenerated",
|
||||||
BuildContentLog(conversationId, agent?.Name, log, ContentLogSource.AgentResponse, message));
|
BuildContentLog(conversationId, agent.Name, log, ContentLogSource.AgentResponse, message));
|
||||||
}
|
|
||||||
|
|
||||||
public async Task OnConversationRedirected(string toAgentId, RoleDialogModel message)
|
|
||||||
{
|
|
||||||
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));
|
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue