From 1d468e0d184ded449b2ea5644dab6804361ac01a Mon Sep 17 00:00:00 2001 From: xbotter Date: Wed, 18 Oct 2023 12:58:56 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A7=20Update=20FileRepository=20to=20h?= =?UTF-8?q?andle=20missing=20directories?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - The FileRepository class in the BotSharp.Core.Repository namespace has been updated to handle cases where the "users" and agent directories are missing. - Previously, the code would throw an exception if the directories were not found, but now it checks for their existence before attempting to read files from them. - This change ensures that the repository can gracefully handle missing directories and continue execution without errors. - The update includes changes to the GetUserAgents, GetAgents, and GetConversations methods, as well as the UpdateAgent, UpdateInstruction, and UpdateFunctions methods. - Additionally, the code now properly checks for the existence of the "agents.json" file before attempting to read from it. - These changes improve the robustness and reliability of the FileRepository class. --- .../Repository/FileRepository.cs | 62 +++++++++++-------- 1 file changed, 37 insertions(+), 25 deletions(-) diff --git a/src/Infrastructure/BotSharp.Core/Repository/FileRepository.cs b/src/Infrastructure/BotSharp.Core/Repository/FileRepository.cs index 8958356e3..1bd49e990 100644 --- a/src/Infrastructure/BotSharp.Core/Repository/FileRepository.cs +++ b/src/Infrastructure/BotSharp.Core/Repository/FileRepository.cs @@ -43,10 +43,13 @@ public IQueryable Users var dir = Path.Combine(_dbSettings.FileRepository, "users"); _users = new List(); - foreach (var d in Directory.GetDirectories(dir)) + if (Directory.Exists(dir)) { - var json = File.ReadAllText(Path.Combine(d, "user.json")); - _users.Add(JsonSerializer.Deserialize(json, _options)); + foreach (var d in Directory.GetDirectories(dir)) + { + var json = File.ReadAllText(Path.Combine(d, "user.json")); + _users.Add(JsonSerializer.Deserialize(json, _options)); + } } return _users.AsQueryable(); } @@ -64,17 +67,20 @@ public IQueryable Agents var dir = Path.Combine(_dbSettings.FileRepository, _agentSettings.DataDir); _agents = new List(); - foreach (var d in Directory.GetDirectories(dir)) + if (Directory.Exists(dir)) { - var json = File.ReadAllText(Path.Combine(d, "agent.json")); - var agent = JsonSerializer.Deserialize(json, _options); - if (agent != null) + foreach (var d in Directory.GetDirectories(dir)) { - agent = agent.SetInstruction(FetchInstruction(d)) - .SetTemplates(FetchTemplates(d)) - .SetFunctions(FetchFunctions(d)) - .SetResponses(FetchResponses(d)); - _agents.Add(agent); + var json = File.ReadAllText(Path.Combine(d, "agent.json")); + var agent = JsonSerializer.Deserialize(json, _options); + if (agent != null) + { + agent = agent.SetInstruction(FetchInstruction(d)) + .SetTemplates(FetchTemplates(d)) + .SetFunctions(FetchFunctions(d)) + .SetResponses(FetchResponses(d)); + _agents.Add(agent); + } } } return _agents.AsQueryable(); @@ -93,13 +99,16 @@ public IQueryable UserAgents var dir = Path.Combine(_dbSettings.FileRepository, "users"); _userAgents = new List(); - foreach (var d in Directory.GetDirectories(dir)) + if (Directory.Exists(dir)) { - var file = Path.Combine(d, "agents.json"); - if (Directory.Exists(d) && File.Exists(file)) + foreach (var d in Directory.GetDirectories(dir)) { - var json = File.ReadAllText(file); - _userAgents.AddRange(JsonSerializer.Deserialize>(json, _options)); + var file = Path.Combine(d, "agents.json"); + if (Directory.Exists(d) && File.Exists(file)) + { + var json = File.ReadAllText(file); + _userAgents.AddRange(JsonSerializer.Deserialize>(json, _options)); + } } } return _userAgents.AsQueryable(); @@ -118,13 +127,16 @@ public IQueryable Conversations var dir = Path.Combine(_dbSettings.FileRepository, _conversationSettings.DataDir); _conversations = new List(); - foreach (var d in Directory.GetDirectories(dir)) + if (Directory.Exists(dir)) { - var path = Path.Combine(d, "conversation.json"); - if (File.Exists(path)) + foreach (var d in Directory.GetDirectories(dir)) { - var json = File.ReadAllText(path); - _conversations.Add(JsonSerializer.Deserialize(json, _options)); + var path = Path.Combine(d, "conversation.json"); + if (File.Exists(path)) + { + var json = File.ReadAllText(path); + _conversations.Add(JsonSerializer.Deserialize(json, _options)); + } } } return _conversations.AsQueryable(); @@ -223,7 +235,7 @@ public int Transaction(Action action) return _changedTableNames.Count; } - + #region Agent public void UpdateAgent(Agent agent, AgentField field) { @@ -365,7 +377,7 @@ private void UpdateAgentInstruction(string agentId, string instruction) var (agent, agentFile) = GetAgentFromFile(agentId); if (agent == null) return; - var instructionFile = Path.Combine(_dbSettings.FileRepository, _agentSettings.DataDir, + var instructionFile = Path.Combine(_dbSettings.FileRepository, _agentSettings.DataDir, agentId, $"instruction.{_agentSettings.TemplateFormat}"); File.WriteAllText(instructionFile, instruction); @@ -380,7 +392,7 @@ private void UpdateAgentFunctions(string agentId, List inputFunctio var functionFile = Path.Combine(_dbSettings.FileRepository, _agentSettings.DataDir, agentId, "functions.json"); - + var functions = new List(); foreach (var function in inputFunctions) {