Merge pull request #290 from iceljc/features/migrate-agent-task-mongo
add task migration
This commit is contained in:
commit
08620abe2b
|
|
@ -39,8 +39,10 @@ public interface IBotSharpRepository
|
||||||
PagedItems<AgentTask> GetAgentTasks(AgentTaskFilter filter);
|
PagedItems<AgentTask> GetAgentTasks(AgentTaskFilter filter);
|
||||||
AgentTask? GetAgentTask(string agentId, string taskId);
|
AgentTask? GetAgentTask(string agentId, string taskId);
|
||||||
void InsertAgentTask(AgentTask task);
|
void InsertAgentTask(AgentTask task);
|
||||||
|
void BulkInsertAgentTasks(List<AgentTask> tasks);
|
||||||
void UpdateAgentTask(AgentTask task, AgentTaskField field);
|
void UpdateAgentTask(AgentTask task, AgentTaskField field);
|
||||||
bool DeleteAgentTask(string agentId, string taskId);
|
bool DeleteAgentTask(string agentId, string taskId);
|
||||||
|
bool DeleteAgentTasks();
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Conversation
|
#region Conversation
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,10 @@
|
||||||
using BotSharp.Abstraction.Agents.Models;
|
using BotSharp.Abstraction.Agents.Models;
|
||||||
using BotSharp.Abstraction.Functions.Models;
|
using BotSharp.Abstraction.Functions.Models;
|
||||||
using BotSharp.Abstraction.Repositories;
|
using BotSharp.Abstraction.Repositories;
|
||||||
|
using BotSharp.Abstraction.Tasks.Models;
|
||||||
|
using BotSharp.Abstraction.Users.Models;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
|
using System.Text.RegularExpressions;
|
||||||
|
|
||||||
namespace BotSharp.Core.Agents.Services;
|
namespace BotSharp.Core.Agents.Services;
|
||||||
|
|
||||||
|
|
@ -156,4 +159,71 @@ public partial class AgentService
|
||||||
var samples = File.ReadAllLines(file);
|
var samples = File.ReadAllLines(file);
|
||||||
return samples?.ToList() ?? new List<string>();
|
return samples?.ToList() ?? new List<string>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private List<AgentTask> FetchTasksFromFile(string fileDir)
|
||||||
|
{
|
||||||
|
var tasks = new List<AgentTask>();
|
||||||
|
var taskDir = Path.Combine(fileDir, "tasks");
|
||||||
|
if (!Directory.Exists(taskDir)) return tasks;
|
||||||
|
|
||||||
|
var agentId = fileDir.Split(Path.DirectorySeparatorChar).Last();
|
||||||
|
foreach (var file in Directory.GetFiles(taskDir))
|
||||||
|
{
|
||||||
|
var parsedTask = ParseAgentTask(file);
|
||||||
|
if (parsedTask == null) continue;
|
||||||
|
|
||||||
|
var task = new AgentTask
|
||||||
|
{
|
||||||
|
Id = parsedTask.Id,
|
||||||
|
Name = parsedTask.Name,
|
||||||
|
Description = parsedTask.Description,
|
||||||
|
Enabled = parsedTask.Enabled,
|
||||||
|
DirectAgentId = parsedTask.DirectAgentId,
|
||||||
|
Content = parsedTask.Content,
|
||||||
|
AgentId = agentId,
|
||||||
|
CreatedDateTime = parsedTask.CreatedDateTime,
|
||||||
|
UpdatedDateTime = parsedTask.UpdatedDateTime
|
||||||
|
};
|
||||||
|
tasks.Add(task);
|
||||||
|
}
|
||||||
|
|
||||||
|
return tasks;
|
||||||
|
}
|
||||||
|
|
||||||
|
private AgentTask? ParseAgentTask(string taskFile)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrWhiteSpace(taskFile)) return null;
|
||||||
|
|
||||||
|
var prefix = @"#metadata";
|
||||||
|
var suffix = @"/metadata";
|
||||||
|
var fileName = taskFile.Split(Path.DirectorySeparatorChar).Last();
|
||||||
|
var id = fileName.Split('.').First();
|
||||||
|
var data = File.ReadAllText(taskFile);
|
||||||
|
var pattern = $@"{prefix}.+{suffix}";
|
||||||
|
var metaData = Regex.Match(data, pattern, RegexOptions.Singleline);
|
||||||
|
|
||||||
|
if (!metaData.Success) return null;
|
||||||
|
|
||||||
|
var task = metaData.Value.JsonContent<AgentTask>();
|
||||||
|
if (task == null) return null;
|
||||||
|
|
||||||
|
task.Id = id;
|
||||||
|
pattern = $@"{suffix}.+";
|
||||||
|
var content = Regex.Match(data, pattern, RegexOptions.Singleline).Value;
|
||||||
|
task.Content = content.Substring(suffix.Length).Trim();
|
||||||
|
return task;
|
||||||
|
}
|
||||||
|
|
||||||
|
private UserAgent BuildUserAgent(string agentId, string userId, bool editable = false)
|
||||||
|
{
|
||||||
|
return new UserAgent
|
||||||
|
{
|
||||||
|
Id = Guid.NewGuid().ToString(),
|
||||||
|
UserId = userId,
|
||||||
|
AgentId = agentId,
|
||||||
|
Editable = editable,
|
||||||
|
CreatedTime = DateTime.UtcNow,
|
||||||
|
UpdatedTime = DateTime.UtcNow
|
||||||
|
};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,8 @@
|
||||||
using BotSharp.Abstraction.Agents.Models;
|
using BotSharp.Abstraction.Agents.Models;
|
||||||
using BotSharp.Abstraction.Repositories;
|
using BotSharp.Abstraction.Repositories;
|
||||||
|
using BotSharp.Abstraction.Tasks.Models;
|
||||||
using Microsoft.Extensions.Caching.Memory;
|
using Microsoft.Extensions.Caching.Memory;
|
||||||
|
using System.Collections.Generic;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
|
|
||||||
namespace BotSharp.Core.Agents.Services;
|
namespace BotSharp.Core.Agents.Services;
|
||||||
|
|
@ -9,8 +11,9 @@ public partial class AgentService
|
||||||
{
|
{
|
||||||
public async Task RefreshAgents()
|
public async Task RefreshAgents()
|
||||||
{
|
{
|
||||||
var isDeleted = _db.DeleteAgents();
|
var isAgentDeleted = _db.DeleteAgents();
|
||||||
if (!isDeleted) return;
|
var isTaskDeleted = _db.DeleteAgentTasks();
|
||||||
|
if (!isAgentDeleted) return;
|
||||||
|
|
||||||
var dbSettings = _services.GetRequiredService<BotSharpDatabaseSettings>();
|
var dbSettings = _services.GetRequiredService<BotSharpDatabaseSettings>();
|
||||||
var agentDir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory,
|
var agentDir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory,
|
||||||
|
|
@ -20,6 +23,7 @@ public partial class AgentService
|
||||||
var user = _db.GetUserById(_user.Id);
|
var user = _db.GetUserById(_user.Id);
|
||||||
var agents = new List<Agent>();
|
var agents = new List<Agent>();
|
||||||
var userAgents = new List<UserAgent>();
|
var userAgents = new List<UserAgent>();
|
||||||
|
var agentTasks = new List<AgentTask>();
|
||||||
|
|
||||||
foreach (var dir in Directory.GetDirectories(agentDir))
|
foreach (var dir in Directory.GetDirectories(agentDir))
|
||||||
{
|
{
|
||||||
|
|
@ -37,23 +41,18 @@ public partial class AgentService
|
||||||
.SetFunctions(functions)
|
.SetFunctions(functions)
|
||||||
.SetResponses(responses)
|
.SetResponses(responses)
|
||||||
.SetSamples(samples);
|
.SetSamples(samples);
|
||||||
|
|
||||||
var userAgent = new UserAgent
|
|
||||||
{
|
|
||||||
Id = Guid.NewGuid().ToString(),
|
|
||||||
UserId = user.Id,
|
|
||||||
AgentId = agent.Id,
|
|
||||||
Editable = false,
|
|
||||||
CreatedTime = DateTime.UtcNow,
|
|
||||||
UpdatedTime = DateTime.UtcNow
|
|
||||||
};
|
|
||||||
|
|
||||||
agents.Add(agent);
|
agents.Add(agent);
|
||||||
|
|
||||||
|
var userAgent = BuildUserAgent(agent.Id, user.Id);
|
||||||
userAgents.Add(userAgent);
|
userAgents.Add(userAgent);
|
||||||
|
|
||||||
|
var tasks = FetchTasksFromFile(dir);
|
||||||
|
agentTasks.AddRange(tasks);
|
||||||
}
|
}
|
||||||
|
|
||||||
_db.BulkInsertAgents(agents);
|
_db.BulkInsertAgents(agents);
|
||||||
_db.BulkInsertUserAgents(userAgents);
|
_db.BulkInsertUserAgents(userAgents);
|
||||||
|
_db.BulkInsertAgentTasks(agentTasks);
|
||||||
|
|
||||||
Utilities.ClearCache();
|
Utilities.ClearCache();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -136,6 +136,11 @@ public class BotSharpDbContext : Database, IBotSharpRepository
|
||||||
throw new NotImplementedException();
|
throw new NotImplementedException();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void BulkInsertAgentTasks(List<AgentTask> tasks)
|
||||||
|
{
|
||||||
|
throw new NotImplementedException();
|
||||||
|
}
|
||||||
|
|
||||||
public void UpdateAgentTask(AgentTask task, AgentTaskField field)
|
public void UpdateAgentTask(AgentTask task, AgentTaskField field)
|
||||||
{
|
{
|
||||||
throw new NotImplementedException();
|
throw new NotImplementedException();
|
||||||
|
|
@ -145,6 +150,11 @@ public class BotSharpDbContext : Database, IBotSharpRepository
|
||||||
{
|
{
|
||||||
throw new NotImplementedException();
|
throw new NotImplementedException();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public bool DeleteAgentTasks()
|
||||||
|
{
|
||||||
|
throw new NotImplementedException();
|
||||||
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Conversation
|
#region Conversation
|
||||||
|
|
|
||||||
|
|
@ -130,6 +130,11 @@ public partial class FileRepository
|
||||||
File.WriteAllText(taskFile, fileContent);
|
File.WriteAllText(taskFile, fileContent);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void BulkInsertAgentTasks(List<AgentTask> tasks)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
public void UpdateAgentTask(AgentTask task, AgentTaskField field)
|
public void UpdateAgentTask(AgentTask task, AgentTaskField field)
|
||||||
{
|
{
|
||||||
if (task == null || string.IsNullOrEmpty(task.Id)) return;
|
if (task == null || string.IsNullOrEmpty(task.Id)) return;
|
||||||
|
|
@ -202,6 +207,11 @@ public partial class FileRepository
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public bool DeleteAgentTasks()
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
private string? FindTaskFileById(string taskDir, string taskId)
|
private string? FindTaskFileById(string taskDir, string taskId)
|
||||||
{
|
{
|
||||||
if (!Directory.Exists(taskDir) || string.IsNullOrEmpty(taskId)) return null;
|
if (!Directory.Exists(taskDir) || string.IsNullOrEmpty(taskId)) return null;
|
||||||
|
|
|
||||||
|
|
@ -97,6 +97,26 @@ public partial class MongoRepository
|
||||||
_dc.AgentTasks.InsertOne(taskDoc);
|
_dc.AgentTasks.InsertOne(taskDoc);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void BulkInsertAgentTasks(List<AgentTask> tasks)
|
||||||
|
{
|
||||||
|
if (tasks.IsNullOrEmpty()) return;
|
||||||
|
|
||||||
|
var taskDocs = tasks.Select(x => new AgentTaskDocument
|
||||||
|
{
|
||||||
|
Id = string.IsNullOrEmpty(x.Id) ? Guid.NewGuid().ToString() : x.Id,
|
||||||
|
Name = x.Name,
|
||||||
|
Description = x.Description,
|
||||||
|
Enabled = x.Enabled,
|
||||||
|
AgentId = x.AgentId,
|
||||||
|
DirectAgentId = x.DirectAgentId,
|
||||||
|
Content = x.Content,
|
||||||
|
CreatedTime = x.CreatedDateTime,
|
||||||
|
UpdatedTime = x.UpdatedDateTime
|
||||||
|
}).ToList();
|
||||||
|
|
||||||
|
_dc.AgentTasks.InsertMany(taskDocs);
|
||||||
|
}
|
||||||
|
|
||||||
public void UpdateAgentTask(AgentTask task, AgentTaskField field)
|
public void UpdateAgentTask(AgentTask task, AgentTaskField field)
|
||||||
{
|
{
|
||||||
if (task == null || string.IsNullOrEmpty(task.Id)) return;
|
if (task == null || string.IsNullOrEmpty(task.Id)) return;
|
||||||
|
|
@ -143,5 +163,18 @@ public partial class MongoRepository
|
||||||
var taskDeleted = _dc.AgentTasks.DeleteOne(filter);
|
var taskDeleted = _dc.AgentTasks.DeleteOne(filter);
|
||||||
return taskDeleted.DeletedCount > 0;
|
return taskDeleted.DeletedCount > 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public bool DeleteAgentTasks()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
_dc.AgentTasks.DeleteMany(Builders<AgentTaskDocument>.Filter.Empty);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
#endregion
|
#endregion
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue