|
| 1 | +using BotSharp.Abstraction.Routing.Models; |
| 2 | +using System.Collections.Concurrent; |
| 3 | + |
1 | 4 | namespace BotSharp.Core.Agents.Services;
|
2 | 5 |
|
3 | 6 | public partial class AgentService
|
4 | 7 | {
|
| 8 | + public static ConcurrentDictionary<string, Dictionary<string,string>> AgentParameterTypes = new(); |
| 9 | + |
5 | 10 | [MemoryCache(10 * 60, perInstanceCache: true)]
|
6 | 11 | public async Task<Agent> LoadAgent(string id)
|
7 | 12 | {
|
@@ -49,6 +54,7 @@ public async Task<Agent> LoadAgent(string id)
|
49 | 54 | agent.Instruction = inheritedAgent.Instruction;
|
50 | 55 | }
|
51 | 56 | }
|
| 57 | + AddOrUpdateParameters(agent); |
52 | 58 |
|
53 | 59 | agent.TemplateDict = new Dictionary<string, object>();
|
54 | 60 |
|
@@ -96,4 +102,43 @@ private void PopulateState(Dictionary<string, object> dict)
|
96 | 102 | dict[t.Key] = t.Value;
|
97 | 103 | }
|
98 | 104 | }
|
| 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 | + } |
99 | 144 | }
|
0 commit comments