Merge pull request #722 from iceljc/master

fix create agent issue
This commit is contained in:
iceljc 2024-11-04 11:23:00 -06:00 committed by GitHub
commit f34326939d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 82 additions and 6 deletions

View file

@ -1,4 +1,5 @@
using BotSharp.Abstraction.Tasks.Models;
using BotSharp.Abstraction.Users.Enums;
using System.IO;
using System.Text.RegularExpressions;
@ -25,6 +26,20 @@ public partial class AgentService
var user = _db.GetUserById(_user.Id);
_db.BulkInsertAgents(new List<Agent> { agentRecord });
if (!UserConstant.AdminRoles.Contains(user.Role))
{
_db.BulkInsertUserAgents(new List<UserAgent>
{
new UserAgent
{
UserId = user.Id,
AgentId = agentRecord.Id,
Actions = new List<string> { UserAction.Edit },
CreatedTime = DateTime.UtcNow,
UpdatedTime = DateTime.UtcNow
}
});
}
Utilities.ClearCache();
return await Task.FromResult(agentRecord);

View file

@ -1,5 +1,7 @@
using BotSharp.Abstraction.Agents.Models;
using BotSharp.Abstraction.Routing.Models;
using BotSharp.Abstraction.Users.Models;
using Microsoft.Extensions.Logging;
using System.IO;
namespace BotSharp.Core.Repository
@ -451,12 +453,63 @@ namespace BotSharp.Core.Repository
public void BulkInsertAgents(List<Agent> agents)
{
_agents = [];
if (agents.IsNullOrEmpty()) return;
var baseDir = Path.Combine(_dbSettings.FileRepository, _agentSettings.DataDir);
foreach (var agent in agents)
{
var dir = Path.Combine(baseDir, agent.Id);
if (Directory.Exists(dir)) continue;
Directory.CreateDirectory(dir);
Thread.Sleep(50);
var agentFile = Path.Combine(dir, AGENT_FILE);
var json = JsonSerializer.Serialize(agent, _options);
File.WriteAllText(agentFile, json);
if (!string.IsNullOrWhiteSpace(agent.Instruction))
{
var instDir = Path.Combine(dir, AGENT_INSTRUCTIONS_FOLDER);
Directory.CreateDirectory(instDir);
var instFile = Path.Combine(instDir, $"{AGENT_INSTRUCTION_FILE}.{_agentSettings.TemplateFormat}");
File.WriteAllText(instFile, agent.Instruction);
}
}
Reset();
}
public void BulkInsertUserAgents(List<UserAgent> userAgents)
{
_userAgents = [];
if (userAgents.IsNullOrEmpty()) return;
var groups = userAgents.GroupBy(x => x.UserId);
var usersDir = Path.Combine(_dbSettings.FileRepository, USERS_FOLDER);
foreach (var group in groups)
{
var filtered = group.Where(x => !string.IsNullOrEmpty(x.UserId) && !string.IsNullOrEmpty(x.AgentId)).ToList();
if (filtered.IsNullOrEmpty()) continue;
filtered.ForEach(x => x.Id = Guid.NewGuid().ToString());
var userId = filtered.First().UserId;
var userDir = Path.Combine(usersDir, userId);
if (!Directory.Exists(userDir)) continue;
var userAgentFile = Path.Combine(userDir, USER_AGENT_FILE);
var list = new List<UserAgent>();
if (File.Exists(userAgentFile))
{
var str = File.ReadAllText(userAgentFile);
list = JsonSerializer.Deserialize<List<UserAgent>>(str, _options);
}
list.AddRange(filtered);
File.WriteAllText(userAgentFile, JsonSerializer.Serialize(list, _options));
Thread.Sleep(50);
}
Reset();
}
public bool DeleteAgents()
@ -493,8 +546,7 @@ namespace BotSharp.Core.Repository
// Delete agent folder
Directory.Delete(agentDir, true);
_agents = [];
_userAgents = [];
Reset();
return true;
}
catch
@ -502,5 +554,11 @@ namespace BotSharp.Core.Repository
return false;
}
}
private void Reset()
{
_agents = [];
_userAgents = [];
}
}
}

View file

@ -429,10 +429,13 @@ public partial class MongoRepository
{
if (userAgents.IsNullOrEmpty()) return;
var userAgentDocs = userAgents.Select(x => new UserAgentDocument
var filtered = userAgents.Where(x => !string.IsNullOrEmpty(x.UserId) && !string.IsNullOrEmpty(x.AgentId)).ToList();
if (filtered.IsNullOrEmpty()) return;
var userAgentDocs = filtered.Select(x => new UserAgentDocument
{
Id = !string.IsNullOrEmpty(x.Id) ? x.Id : Guid.NewGuid().ToString(),
UserId = !string.IsNullOrEmpty(x.UserId) ? x.UserId : string.Empty,
UserId = x.UserId,
AgentId = x.AgentId,
Actions = x.Actions,
CreatedTime = x.CreatedTime,