Skip to content

Commit 38b50a3

Browse files
authored
Merge pull request #573 from yileicn/master
optimize paramter type check
2 parents ea441c7 + e5f1180 commit 38b50a3

File tree

2 files changed

+65
-1
lines changed

2 files changed

+65
-1
lines changed

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

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1+
using BotSharp.Abstraction.Routing.Models;
2+
using System.Collections.Concurrent;
3+
14
namespace BotSharp.Core.Agents.Services;
25

36
public partial class AgentService
47
{
8+
public static ConcurrentDictionary<string, Dictionary<string,string>> AgentParameterTypes = new();
9+
510
[MemoryCache(10 * 60, perInstanceCache: true)]
611
public async Task<Agent> LoadAgent(string id)
712
{
@@ -49,6 +54,7 @@ public async Task<Agent> LoadAgent(string id)
4954
agent.Instruction = inheritedAgent.Instruction;
5055
}
5156
}
57+
AddOrUpdateParameters(agent);
5258

5359
agent.TemplateDict = new Dictionary<string, object>();
5460

@@ -96,4 +102,43 @@ private void PopulateState(Dictionary<string, object> dict)
96102
dict[t.Key] = t.Value;
97103
}
98104
}
105+
106+
private void AddOrUpdateParameters(Agent agent)
107+
{
108+
var agentId = agent.Id ?? agent.Name;
109+
if (AgentParameterTypes.ContainsKey(agentId)) return;
110+
111+
AddOrUpdateRoutesParameters(agentId, agent.RoutingRules);
112+
AddOrUpdateFunctionsParameters(agentId, agent.Functions);
113+
}
114+
115+
private void AddOrUpdateRoutesParameters(string agentId, List<RoutingRule> routingRules)
116+
{
117+
if(!AgentParameterTypes.TryGetValue(agentId, out var parameterTypes)) parameterTypes = new();
118+
foreach (var rule in routingRules.Where(x => x.Required))
119+
{
120+
if (string.IsNullOrEmpty(rule.FieldType)) continue;
121+
parameterTypes.TryAdd(rule.Field, rule.FieldType);
122+
}
123+
AgentParameterTypes.TryAdd(agentId, parameterTypes);
124+
}
125+
126+
private void AddOrUpdateFunctionsParameters(string agentId, List<FunctionDef> functions)
127+
{
128+
if (!AgentParameterTypes.TryGetValue(agentId, out var parameterTypes)) parameterTypes = new();
129+
var parameters = functions.Select(p => p.Parameters);
130+
foreach (var param in parameters)
131+
{
132+
foreach (JsonProperty prop in param.Properties.RootElement.EnumerateObject())
133+
{
134+
var name = prop.Name;
135+
var node = prop.Value;
136+
if (node.TryGetProperty("type", out var type))
137+
{
138+
parameterTypes.TryAdd(name, type.GetString());
139+
}
140+
}
141+
}
142+
AgentParameterTypes.TryAdd(agentId, parameterTypes);
143+
}
99144
}

src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationStateService.cs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,9 +360,28 @@ public void SaveStateByArgs(JsonDocument args)
360360
stateValue = stateValue?.ToLower();
361361
}
362362

363-
SetState(property.Name, stateValue, source: StateSource.Application);
363+
if (CheckArgType(property.Name, stateValue))
364+
{
365+
SetState(property.Name, stateValue, source: StateSource.Application);
366+
}
364367
}
365368
}
366369
}
367370
}
371+
372+
private bool CheckArgType(string name, string value)
373+
{
374+
var agentTypes = AgentService.AgentParameterTypes.SelectMany(p => p.Value).ToList();
375+
var filed = agentTypes.FirstOrDefault(t => t.Key == name);
376+
if (filed.Key != null)
377+
{
378+
return filed.Value switch
379+
{
380+
"boolean" => bool.TryParse(value, out _),
381+
"number" => long.TryParse(value, out _),
382+
_ => true,
383+
};
384+
}
385+
return true;
386+
}
368387
}

0 commit comments

Comments
 (0)