using BotSharp.Abstraction.Agents.Models; using BotSharp.Abstraction.Functions.Models; using BotSharp.Abstraction.Repositories; using BotSharp.Abstraction.Routing.Models; using System.IO; namespace BotSharp.Core.Agents.Services; public partial class AgentService { public async Task UpdateAgent(Agent agent, AgentField updateField) { if (agent == null || string.IsNullOrEmpty(agent.Id)) return; var record = _db.GetAgent(agent.Id); if (record == null) return; record.Name = agent.Name ?? string.Empty; record.Description = agent.Description ?? string.Empty; record.IsPublic = agent.IsPublic; record.Disabled = agent.Disabled; record.AllowRouting = agent.AllowRouting; record.Profiles = agent.Profiles ?? new List(); record.RoutingRules = agent.RoutingRules ?? new List(); record.Instruction = agent.Instruction ?? string.Empty; record.Functions = agent.Functions ?? new List(); record.Templates = agent.Templates ?? new List(); record.Responses = agent.Responses ?? new List(); record.Samples = agent.Samples ?? new List(); _db.UpdateAgent(record, updateField); await Task.CompletedTask; } public async Task UpdateAgentFromFile(string id) { var agent = _db.GetAgent(id); if (agent == null) return; var dbSettings = _services.GetRequiredService(); var agentSettings = _services.GetRequiredService(); var filePath = Path.Combine(dbSettings.FileRepository, agentSettings.DataDir); var clonedAgent = Agent.Clone(agent); var foundAgent = FetchAgentFileById(agent.Id, filePath); if (foundAgent != null) { clonedAgent.SetId(foundAgent.Id) .SetName(foundAgent.Name) .SetDescription(foundAgent.Description) .SetIsPublic(foundAgent.IsPublic) .SetDisabled(foundAgent.Disabled) .SetAllowRouting(foundAgent.AllowRouting) .SetProfiles(foundAgent.Profiles) .SetRoutingRules(foundAgent.RoutingRules) .SetInstruction(foundAgent.Instruction) .SetTemplates(foundAgent.Templates) .SetFunctions(foundAgent.Functions) .SetResponses(foundAgent.Responses) .SetSamples(foundAgent.Samples); _db.UpdateAgent(clonedAgent, AgentField.All); } await Task.CompletedTask; } private Agent FetchAgentFileById(string agentId, string filePath) { foreach (var dir in Directory.GetDirectories(filePath)) { var agentJson = File.ReadAllText(Path.Combine(dir, "agent.json")); var agent = JsonSerializer.Deserialize(agentJson, _options); if (agent != null && agent.Id == agentId) { var functions = FetchFunctionsFromFile(dir); var instruction = FetchInstructionFromFile(dir); var responses = FetchResponsesFromFile(dir); var templates = FetchTemplatesFromFile(dir); var samples = FetchSamplesFromFile(dir); return agent.SetInstruction(instruction) .SetTemplates(templates) .SetFunctions(functions) .SetResponses(responses) .SetSamples(samples); } } return null; } }