Skip to content

Fix IndexOutOfRangeException caused by CheckArgType concurrency #1063

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -101,26 +101,18 @@ private void AddOrUpdateParameters(Agent agent)

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)
Expand All @@ -131,11 +123,9 @@ private void AddOrUpdateFunctionsParameters(string agentId, List<FunctionDef> fu
var node = prop.Value;
if (node.TryGetProperty("type", out var type))
{
parameterTypes.TryAdd(name, type.GetString());
parameterTypes[name] = type.GetString();
}
}
}

AgentParameterTypes.TryAdd(agentId, parameterTypes);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ public void SaveStateByArgs(JsonDocument args)
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
Expand Down
Loading