Merge pull request #1063 from yileicn/master

Fix IndexOutOfRangeException caused by CheckArgType concurrency
This commit is contained in:
Haiping 2025-05-27 09:38:58 -05:00 committed by GitHub
commit f5c5f8ee35
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 6 additions and 16 deletions

View file

@ -6,7 +6,7 @@ namespace BotSharp.Core.Agents.Services;
public partial class AgentService
{
public static ConcurrentDictionary<string, Dictionary<string, string>> AgentParameterTypes = new();
public static ConcurrentDictionary<string, ConcurrentDictionary<string, string>> AgentParameterTypes = new();
// [SharpCache(10, perInstanceCache: true)]
public async Task<Agent> LoadAgent(string id, bool loadUtility = true)
@ -101,26 +101,18 @@ public partial class AgentService
private void AddOrUpdateRoutesParameters(string agentId, List<RoutingRule> routingRules)
{
if (!AgentParameterTypes.TryGetValue(agentId, out var parameterTypes))
{
parameterTypes = new();
}
var parameterTypes = AgentParameterTypes.GetOrAdd(agentId, _ => new());
foreach (var rule in routingRules.Where(x => x.Required))
{
if (string.IsNullOrEmpty(rule.FieldType)) continue;
parameterTypes.TryAdd(rule.Field, rule.FieldType);
parameterTypes[rule.Field] = rule.FieldType;
}
AgentParameterTypes.TryAdd(agentId, parameterTypes);
}
private void AddOrUpdateFunctionsParameters(string agentId, List<FunctionDef> functions)
{
if (!AgentParameterTypes.TryGetValue(agentId, out var parameterTypes))
{
parameterTypes = new();
}
var parameterTypes = AgentParameterTypes.GetOrAdd(agentId, _ => new());
var parameters = functions.Select(p => p.Parameters);
foreach (var param in parameters)
@ -131,11 +123,9 @@ public partial class AgentService
var node = prop.Value;
if (node.TryGetProperty("type", out var type))
{
parameterTypes.TryAdd(name, type.GetString());
parameterTypes[name] = type.GetString();
}
}
}
AgentParameterTypes.TryAdd(agentId, parameterTypes);
}
}

View file

@ -416,7 +416,7 @@ public class ConversationStateService : IConversationStateService
private bool CheckArgType(string name, string value)
{
// Defensive: Ensure AgentParameterTypes is not null or empty and values are not null
if (AgentService.AgentParameterTypes == null || !AgentService.AgentParameterTypes.Any())
if (AgentService.AgentParameterTypes.IsNullOrEmpty())
return true;
var agentTypes = AgentService.AgentParameterTypes