diff --git a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.CreateAgent.cs b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.CreateAgent.cs index ae429484..43cb25df 100644 --- a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.CreateAgent.cs +++ b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.CreateAgent.cs @@ -102,11 +102,28 @@ public partial class AgentService private List FetchFunctionsFromFile(string fileDir) { - var file = Path.Combine(fileDir, "functions.json"); - if (!File.Exists(file)) return new List(); + var functions = new List(); + var functionDir = Path.Combine(fileDir, "functions"); - var functionsJson = File.ReadAllText(file); - var functions = JsonSerializer.Deserialize>(functionsJson, _options); + if (!Directory.Exists(functionDir)) return functions; + + foreach (var file in Directory.GetFiles(functionDir)) + { + try + { + var extension = Path.GetExtension(file).Substring(1); + if (extension != "json") continue; + + var json = File.ReadAllText(file); + var function = JsonSerializer.Deserialize(json, _options); + functions.Add(function); + } + catch + { + continue; + } + + } return functions; } diff --git a/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.Agent.cs b/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.Agent.cs index da0f2030..2c464c32 100644 --- a/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.Agent.cs +++ b/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.Agent.cs @@ -1,5 +1,6 @@ using BotSharp.Abstraction.Routing.Models; using System.IO; +using System.Threading; namespace BotSharp.Core.Repository { @@ -194,11 +195,24 @@ namespace BotSharp.Core.Repository var (agent, agentFile) = GetAgentFromFile(agentId); if (agent == null) return; - var functionFile = Path.Combine(_dbSettings.FileRepository, _agentSettings.DataDir, - agentId, AGENT_FUNCTIONS_FILE); + var functionDir = Path.Combine(_dbSettings.FileRepository, _agentSettings.DataDir, + agentId, AGENT_FUNCTIONS_FOLDER); - var functionText = JsonSerializer.Serialize(inputFunctions, _options); - File.WriteAllText(functionFile, functionText); + if (Directory.Exists(functionDir)) + { + Directory.Delete(functionDir, true); + } + Directory.CreateDirectory(functionDir); + + foreach (var func in inputFunctions) + { + if (string.IsNullOrWhiteSpace(func.Name)) continue; + + var text = JsonSerializer.Serialize(func, _options); + var file = Path.Combine(functionDir, $"{func.Name}.json"); + File.WriteAllText(file, text); + Thread.Sleep(200); + } } private void UpdateAgentTemplates(string agentId, List templates) diff --git a/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.cs b/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.cs index c7b6462b..ed953c17 100644 --- a/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.cs +++ b/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.cs @@ -22,7 +22,6 @@ public partial class FileRepository : IBotSharpRepository private const string AGENT_FILE = "agent.json"; private const string AGENT_INSTRUCTION_FILE = "instruction"; - private const string AGENT_FUNCTIONS_FILE = "functions.json"; private const string AGENT_SAMPLES_FILE = "samples.txt"; private const string USER_FILE = "user.json"; private const string USER_AGENT_FILE = "agents.json";