Skip to content

Commit 88a9201

Browse files
authored
Merge pull request #110 from hchen2020/master
Apply centralized routing by routing table.
2 parents 7cf649d + e9b34dc commit 88a9201

File tree

9 files changed

+99
-22
lines changed

9 files changed

+99
-22
lines changed

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

Lines changed: 0 additions & 9 deletions
This file was deleted.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System.Text.Json.Serialization;
2+
3+
namespace BotSharp.Abstraction.Agents.Models;
4+
5+
public class RoutingArgs
6+
{
7+
[JsonPropertyName("agent_name")]
8+
public string AgentName { get; set; }
9+
10+
public override string ToString()
11+
{
12+
return AgentName;
13+
}
14+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
namespace BotSharp.Abstraction.Agents.Models;
2+
3+
public class RoutingResult
4+
{
5+
public string Result { get; set; }
6+
7+
public RoutingResult(string result)
8+
{
9+
Result = result;
10+
}
11+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using System.Text.Json.Serialization;
2+
3+
namespace BotSharp.Abstraction.Agents.Models;
4+
5+
public class RoutingTable
6+
{
7+
[JsonPropertyName("agent_id")]
8+
public string AgentId { get; set; }
9+
10+
[JsonPropertyName("name")]
11+
public string AgentName { get; set; }
12+
13+
[JsonPropertyName("required")]
14+
public List<string> RequiredFields { get; set; }
15+
16+
public override string ToString()
17+
{
18+
return AgentName;
19+
}
20+
}

src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.GetChatCompletionsAsyncRecursively.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,18 +53,19 @@ await HandleAssistantMessage(new RoleDialogModel(AgentRole.Assistant, fn.Content
5353
return;
5454
}
5555

56-
fn.Content = fn.ExecutionResult;
56+
fn.Content = fn.FunctionArgs.Replace("\r", " ").Replace("\n", " ").Trim() + " => " + fn.ExecutionResult;
5757

5858
// Agent has been transferred
59+
var agentSettings = _services.GetRequiredService<AgentSettings>();
5960
if (fn.CurrentAgentId != preAgentId)
6061
{
61-
var agentSettings = _services.GetRequiredService<AgentSettings>();
6262
var agentService = _services.GetRequiredService<IAgentService>();
6363
agent = await agentService.LoadAgent(fn.CurrentAgentId);
6464
}
6565

6666
// Add to dialog history
67-
_storage.Append(conversationId, preAgentId, fn);
67+
// The server had an error processing your request. Sorry about that!
68+
// _storage.Append(conversationId, preAgentId, fn);
6869

6970
// After function is executed, pass the result to LLM to get a natural response
7071
wholeDialogs.Add(fn);

src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.SendMessage.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,10 @@ private void SaveStateByArgs(string args)
8585
{
8686
foreach (JsonProperty property in root.EnumerateObject())
8787
{
88-
stateService.SetState(property.Name, property.Value.ToString());
88+
if (!string.IsNullOrEmpty(property.Value.ToString()))
89+
{
90+
stateService.SetState(property.Name, property.Value.ToString());
91+
}
8992
}
9093
}
9194
}

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

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
using BotSharp.Abstraction.Conversations.Models;
33
using BotSharp.Abstraction.Functions;
44
using BotSharp.Abstraction.Functions.Models;
5+
using Microsoft.Extensions.Logging;
6+
using System.IO;
57

68
namespace BotSharp.Core.Functions;
79

@@ -17,20 +19,51 @@ public RouteToAgentFn(IServiceProvider services)
1719

1820
public async Task<bool> Execute(RoleDialogModel message)
1921
{
20-
var args = JsonSerializer.Deserialize<AgentRoutingArgs>(message.FunctionArgs);
22+
var args = JsonSerializer.Deserialize<RoutingArgs>(message.FunctionArgs);
23+
var result = new RoutingResult($"Routed to {args.AgentName}");
2124

22-
if (string.IsNullOrEmpty(args.AgentId))
25+
if (string.IsNullOrEmpty(args.AgentName))
2326
{
24-
var result = new FunctionExecutionValidationResult("false", "agent_id can't be parsed.");
25-
message.ExecutionResult = JsonSerializer.Serialize(result);
27+
result = new RoutingResult($"Can't find {args.AgentName}");
2628
}
2729
else
2830
{
29-
var result = new FunctionExecutionValidationResult("true");
30-
message.ExecutionResult = JsonSerializer.Serialize(result);
31-
message.CurrentAgentId = args.AgentId;
31+
var agentSettings = _services.GetRequiredService<AgentSettings>();
32+
var dbSettings = _services.GetRequiredService<MyDatabaseSettings>();
33+
var filePath = Path.Combine(dbSettings.FileRepository, agentSettings.DataDir, agentSettings.RouterId, "route.json");
34+
var routes = JsonSerializer.Deserialize<RoutingTable[]>(File.ReadAllText(filePath));
35+
36+
var agent = routes.FirstOrDefault(x => x.AgentName.ToLower() == args.AgentName.ToLower());
37+
if (agent == null)
38+
{
39+
result = new RoutingResult($"Can't find agent {args.AgentName}.");
40+
}
41+
else
42+
{
43+
// Check required fields
44+
var jo = JsonSerializer.Deserialize<object>(message.FunctionArgs);
45+
bool hasMissingField = false;
46+
foreach (var field in agent.RequiredFields)
47+
{
48+
if (jo is JsonElement root)
49+
{
50+
if (!root.EnumerateObject().Any(x => x.Name == field))
51+
{
52+
result = new RoutingResult($"Please provide {field}.");
53+
hasMissingField = true;
54+
break;
55+
}
56+
}
57+
}
58+
59+
if (!hasMissingField)
60+
{
61+
message.CurrentAgentId = agent.AgentId;
62+
}
63+
}
3264
}
3365

66+
message.ExecutionResult = JsonSerializer.Serialize(result);
3467
return true;
3568
}
3669
}

src/Plugins/BotSharp.Plugin.MetaMessenger/Controllers/WebhookController.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,10 @@ public async Task<ActionResult<WebhookResponse>> Messages([FromRoute] string age
124124
try
125125
{
126126
var parsed = JsonSerializer.Deserialize<QuickReplyMessageItem[]>(json, jsonOpt);
127-
reply.QuickReplies = parsed;
127+
if (parsed.Length > 0)
128+
{
129+
reply.QuickReplies = parsed;
130+
}
128131
}
129132
catch(Exception ex)
130133
{

src/Plugins/BotSharp.Plugin.MetaMessenger/MessagingModels/QuickReplyMessage.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,6 @@ public class QuickReplyMessage : IResponseMessage
1313
public string Text { get; set; }
1414

1515
[JsonPropertyName("quick_replies")]
16-
public QuickReplyMessageItem[] QuickReplies { get; set; }
16+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
17+
public QuickReplyMessageItem[]? QuickReplies { get; set; }
1718
}

0 commit comments

Comments
 (0)