Merge pull request #290 from iceljc/features/migrate-agent-task-mongo

add task migration
This commit is contained in:
Haiping 2024-02-05 17:02:23 -06:00 committed by GitHub
commit 08620abe2b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 137 additions and 13 deletions

View file

@ -39,8 +39,10 @@ public interface IBotSharpRepository
PagedItems<AgentTask> GetAgentTasks(AgentTaskFilter filter);
AgentTask? GetAgentTask(string agentId, string taskId);
void InsertAgentTask(AgentTask task);
void BulkInsertAgentTasks(List<AgentTask> tasks);
void UpdateAgentTask(AgentTask task, AgentTaskField field);
bool DeleteAgentTask(string agentId, string taskId);
bool DeleteAgentTasks();
#endregion
#region Conversation

View file

@ -1,7 +1,10 @@
using BotSharp.Abstraction.Agents.Models;
using BotSharp.Abstraction.Functions.Models;
using BotSharp.Abstraction.Repositories;
using BotSharp.Abstraction.Tasks.Models;
using BotSharp.Abstraction.Users.Models;
using System.IO;
using System.Text.RegularExpressions;
namespace BotSharp.Core.Agents.Services;
@ -156,4 +159,71 @@ public partial class AgentService
var samples = File.ReadAllLines(file);
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
};
}
}

View file

@ -1,6 +1,8 @@
using BotSharp.Abstraction.Agents.Models;
using BotSharp.Abstraction.Repositories;
using BotSharp.Abstraction.Tasks.Models;
using Microsoft.Extensions.Caching.Memory;
using System.Collections.Generic;
using System.IO;
namespace BotSharp.Core.Agents.Services;
@ -9,8 +11,9 @@ public partial class AgentService
{
public async Task RefreshAgents()
{
var isDeleted = _db.DeleteAgents();
if (!isDeleted) return;
var isAgentDeleted = _db.DeleteAgents();
var isTaskDeleted = _db.DeleteAgentTasks();
if (!isAgentDeleted) return;
var dbSettings = _services.GetRequiredService<BotSharpDatabaseSettings>();
var agentDir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory,
@ -20,6 +23,7 @@ public partial class AgentService
var user = _db.GetUserById(_user.Id);
var agents = new List<Agent>();
var userAgents = new List<UserAgent>();
var agentTasks = new List<AgentTask>();
foreach (var dir in Directory.GetDirectories(agentDir))
{
@ -37,23 +41,18 @@ public partial class AgentService
.SetFunctions(functions)
.SetResponses(responses)
.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);
var userAgent = BuildUserAgent(agent.Id, user.Id);
userAgents.Add(userAgent);
var tasks = FetchTasksFromFile(dir);
agentTasks.AddRange(tasks);
}
_db.BulkInsertAgents(agents);
_db.BulkInsertUserAgents(userAgents);
_db.BulkInsertAgentTasks(agentTasks);
Utilities.ClearCache();
}

View file

@ -136,6 +136,11 @@ public class BotSharpDbContext : Database, IBotSharpRepository
throw new NotImplementedException();
}
public void BulkInsertAgentTasks(List<AgentTask> tasks)
{
throw new NotImplementedException();
}
public void UpdateAgentTask(AgentTask task, AgentTaskField field)
{
throw new NotImplementedException();
@ -145,6 +150,11 @@ public class BotSharpDbContext : Database, IBotSharpRepository
{
throw new NotImplementedException();
}
public bool DeleteAgentTasks()
{
throw new NotImplementedException();
}
#endregion
#region Conversation

View file

@ -130,6 +130,11 @@ public partial class FileRepository
File.WriteAllText(taskFile, fileContent);
}
public void BulkInsertAgentTasks(List<AgentTask> tasks)
{
}
public void UpdateAgentTask(AgentTask task, AgentTaskField field)
{
if (task == null || string.IsNullOrEmpty(task.Id)) return;
@ -202,6 +207,11 @@ public partial class FileRepository
return true;
}
public bool DeleteAgentTasks()
{
return false;
}
private string? FindTaskFileById(string taskDir, string taskId)
{
if (!Directory.Exists(taskDir) || string.IsNullOrEmpty(taskId)) return null;

View file

@ -97,6 +97,26 @@ public partial class MongoRepository
_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)
{
if (task == null || string.IsNullOrEmpty(task.Id)) return;
@ -143,5 +163,18 @@ public partial class MongoRepository
var taskDeleted = _dc.AgentTasks.DeleteOne(filter);
return taskDeleted.DeletedCount > 0;
}
public bool DeleteAgentTasks()
{
try
{
_dc.AgentTasks.DeleteMany(Builders<AgentTaskDocument>.Filter.Empty);
return true;
}
catch
{
return false;
}
}
#endregion
}