Skip to content

Commit 5e82976

Browse files
authored
Merge pull request #104 from hchen2020/master
Refactor to support agent router.
2 parents 5c4f221 + 15fb98c commit 5e82976

File tree

18 files changed

+335
-119
lines changed

18 files changed

+335
-119
lines changed

src/Infrastructure/BotSharp.Abstraction/Agents/AgentHookBase.cs

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

35
public abstract class AgentHookBase : IAgentHook
@@ -33,8 +35,12 @@ public virtual bool OnSamplesLoaded(ref string samples)
3335
return true;
3436
}
3537

36-
public virtual Agent OnAgentLoaded()
38+
public virtual void OnAgentLoaded(Agent agent)
3739
{
38-
return _agent;
40+
}
41+
42+
public virtual bool OnAgentRouting(RoleDialogModel message, ref string id)
43+
{
44+
return true;
3945
}
4046
}

src/Infrastructure/BotSharp.Abstraction/Agents/IAgentHook.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,5 @@ public interface IAgentHook
2424
/// </summary>
2525
/// <param name="agent"></param>
2626
/// <returns></returns>
27-
Agent OnAgentLoaded();
27+
void OnAgentLoaded(Agent agent);
2828
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace BotSharp.Abstraction.Agents;
2+
3+
public interface IAgentRouting
4+
{
5+
Task<Agent> LoadCurrentAgent();
6+
}

src/Infrastructure/BotSharp.Abstraction/Agents/Models/Agent.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
using BotSharp.Abstraction.Conversations.Models;
2-
31
namespace BotSharp.Abstraction.Agents.Models;
42

53
public class Agent
@@ -29,4 +27,7 @@ public class Agent
2927
/// Domain knowledges
3028
/// </summary>
3129
public string Knowledges { get; set; }
30+
31+
public override string ToString()
32+
=> $"{Name} {Id}";
3233
}

src/Infrastructure/BotSharp.Abstraction/Agents/Settings/AgentSettings.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,9 @@ namespace BotSharp.Abstraction.Agents.Settings;
22

33
public class AgentSettings
44
{
5+
/// <summary>
6+
/// Router Agent Id
7+
/// </summary>
8+
public string RouterId { get; set; }
59
public string DataDir { get; set; }
610
}

src/Infrastructure/BotSharp.Abstraction/Conversations/Models/RoleDialogModel.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,25 @@ public class RoleDialogModel
88
public string Role { get; set; }
99
public DateTime CreatedAt { get; set; } = DateTime.UtcNow;
1010
public string Content { get; set; }
11+
public string CurrentAgentId { get; set; }
1112

1213
/// <summary>
1314
/// Function name if LLM response function call
1415
/// </summary>
1516
public string? FunctionName { get; set; }
1617

18+
public string? FunctionArgs { get; set; }
19+
1720
/// <summary>
1821
/// Function execution result
1922
/// </summary>
2023
public string? ExecutionResult { get; set; }
2124

22-
/// <summary>
23-
/// When function callback has been executed, system will pass result to LLM again,
24-
/// Set this property to True to stop calling LLM.
25-
/// </summary>
26-
public bool StopSubsequentInteraction { get; set; }
27-
2825
public bool IsConversationEnd { get; set; }
2926

27+
public bool NeedReloadAgent { get; set; }
28+
public bool StopPropagate { get; set; }
29+
3030
/// <summary>
3131
/// Channel name
3232
/// </summary>

src/Infrastructure/BotSharp.Abstraction/MLTasks/IChatCompletion.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ namespace BotSharp.Abstraction.MLTasks;
44

55
public interface IChatCompletion
66
{
7-
string GetChatCompletions(Agent agent, List<RoleDialogModel> conversations);
7+
// string GetChatCompletions(Agent agent, List<RoleDialogModel> conversations, Func<RoleDialogModel, Task> onMessageReceived);
88
Task<bool> GetChatCompletionsAsync(Agent agent, List<RoleDialogModel> conversations, Func<RoleDialogModel, Task> onMessageReceived);
99
Task<bool> GetChatCompletionsStreamingAsync(Agent agent, List<RoleDialogModel> conversations, Func<RoleDialogModel, Task> onMessageReceived);
1010
}

src/Infrastructure/BotSharp.Abstraction/Utilities/StringExtensions.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
using System.Linq;
2+
using System.Text.RegularExpressions;
3+
14
namespace BotSharp.Abstraction.Utilities;
25

36
public static class StringExtensions
@@ -15,4 +18,19 @@ public static string SubstringMax(this string str, int maxLength)
1518
else
1619
return str;
1720
}
21+
22+
public static string CleanPhoneNumber(this string phoneNumber)
23+
{
24+
if (phoneNumber != null && !phoneNumber.All(char.IsDigit))
25+
{
26+
phoneNumber = Regex.Replace(phoneNumber, @"[^\d]", "");
27+
}
28+
29+
if (phoneNumber != null && phoneNumber.Length > 10)
30+
{
31+
phoneNumber = phoneNumber.Substring(1);
32+
}
33+
34+
return phoneNumber;
35+
}
1836
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using BotSharp.Abstraction.Agents.Models;
2+
3+
namespace BotSharp.Core.Agents.Services;
4+
5+
public class AgentRouter : IAgentRouting
6+
{
7+
private readonly IServiceProvider _services;
8+
private readonly ILogger _logger;
9+
private readonly AgentSettings _settings;
10+
11+
public AgentRouter(IServiceProvider services,
12+
ILogger<AgentRouter> logger,
13+
AgentSettings settings)
14+
{
15+
_services = services;
16+
_logger = logger;
17+
_settings = settings;
18+
}
19+
20+
public async Task<Agent> LoadCurrentAgent()
21+
{
22+
// Load current agent from state
23+
var stateService = _services.GetRequiredService<IConversationStateService>();
24+
var currentAgentId = stateService.GetState("agentId");
25+
if (string.IsNullOrEmpty(currentAgentId))
26+
{
27+
currentAgentId = _settings.RouterId;
28+
}
29+
var agentService = _services.GetRequiredService<IAgentService>();
30+
var agent = await agentService.LoadAgent(currentAgentId);
31+
32+
// Set agent and trigger state changed
33+
stateService.SetState("agentId", currentAgentId);
34+
35+
return agent;
36+
}
37+
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,11 @@ public async Task<Agent> LoadAgent(string id)
3939
hook.OnSamplesLoaded(ref samples);
4040
}
4141

42-
hook.OnAgentLoaded();
42+
hook.OnAgentLoaded(agent);
4343
}
4444

45+
_logger.LogInformation($"Loaded agent {agent}.");
46+
4547
return agent;
4648
}
4749
}

0 commit comments

Comments
 (0)