Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ public class Agent
/// Agent Type
/// </summary>
public string Type { get; set; } = AgentType.Task;
public DateTime CreatedDateTime { get; set; }
public DateTime UpdatedDateTime { get; set; }
public DateTime CreatedTime { get; set; }
public DateTime UpdatedTime { get; set; }

/// <summary>
/// Default LLM settings
Expand Down Expand Up @@ -174,8 +174,8 @@ public static Agent Clone(Agent agent)
Rules = agent.Rules,
LlmConfig = agent.LlmConfig,
KnowledgeBases = agent.KnowledgeBases,
CreatedDateTime = agent.CreatedDateTime,
UpdatedDateTime = agent.UpdatedDateTime,
CreatedTime = agent.CreatedTime,
UpdatedTime = agent.UpdatedTime,
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,18 @@ List<UserAgent> GetUserAgents(string userId)
=> throw new NotImplementedException();
void BulkInsertAgents(List<Agent> agents)
=> throw new NotImplementedException();
ValueTask BulkInsertAgentsAsync(List<Agent> agents)
=> throw new NotImplementedException();
void BulkInsertUserAgents(List<UserAgent> userAgents)
=> throw new NotImplementedException();
ValueTask BulkInsertUserAgentsAsync(List<UserAgent> userAgents)
=> throw new NotImplementedException();
bool DeleteAgents()
=> throw new NotImplementedException();
Task<bool> DeleteAgentsAsync()
=> throw new NotImplementedException();
ValueTask<bool> DeleteAgentsAsync(List<string> agentIds)
=> throw new NotImplementedException();
bool DeleteAgent(string agentId)
=> throw new NotImplementedException();
List<string> GetAgentResponses(string agentId, string prefix, string intent)
Expand All @@ -101,6 +109,8 @@ void InsertAgentTask(AgentTask task)
=> throw new NotImplementedException();
void BulkInsertAgentTasks(List<AgentTask> tasks)
=> throw new NotImplementedException();
ValueTask BulkInsertAgentTasksAsync(List<AgentTask> tasks)
=> throw new NotImplementedException();
void UpdateAgentTask(AgentTask task, AgentTaskField field)
=> throw new NotImplementedException();
bool DeleteAgentTask(string agentId, List<string> taskIds)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ public async Task<Agent> CreateAgent(Agent agent)

var agentRecord = Agent.Clone(agent);
agentRecord.Id = Guid.NewGuid().ToString();
agentRecord.CreatedDateTime = DateTime.UtcNow;
agentRecord.UpdatedDateTime = DateTime.UtcNow;
agentRecord.CreatedTime = DateTime.UtcNow;
agentRecord.UpdatedTime = DateTime.UtcNow;

var dbSettings = _services.GetRequiredService<BotSharpDatabaseSettings>();
var agentSettings = _services.GetRequiredService<AgentSettings>();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using BotSharp.Abstraction.Repositories.Enums;
using BotSharp.Abstraction.Tasks.Models;
using System.IO;

namespace BotSharp.Core.Agents.Services;
Expand All @@ -24,27 +25,24 @@ public async Task<string> RefreshAgents()
return "Unauthorized user.";
}

var agentDir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory,
dbSettings.FileRepository,
_agentSettings.DataDir);

var agentDir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, dbSettings.FileRepository, _agentSettings.DataDir);
if (!Directory.Exists(agentDir))
{
refreshResult = $"Cannot find the directory: {agentDir}";
return refreshResult;
}

var refreshedAgents = new List<string>();

List<Agent> agents = [];
List<AgentTask> agentTasks = [];

foreach (var dir in Directory.GetDirectories(agentDir))
{
try
{
var agentJson = File.ReadAllText(Path.Combine(dir, "agent.json"));
var agent = JsonSerializer.Deserialize<Agent>(agentJson, _options);

var (agent, msg) = GetAgentFormJson(Path.Combine(dir, "agent.json"));
if (agent == null)
{
_logger.LogError($"Cannot find agent in file directory: {dir}");
_logger.LogError(msg);
continue;
}

Expand All @@ -61,27 +59,26 @@ public async Task<string> RefreshAgents()
.SetSamples(samples);

var tasks = GetTasksFromFile(dir);

var isAgentDeleted = _db.DeleteAgent(agent.Id);
if (isAgentDeleted)
{
await Task.Delay(100);
_db.BulkInsertAgents(new List<Agent> { agent });
_db.BulkInsertAgentTasks(tasks);
refreshedAgents.Add(agent.Name);
_logger.LogInformation($"Agent {agent.Name} has been migrated.");
}
if (!tasks.IsNullOrEmpty()) agentTasks.AddRange(tasks);
agents.Add(agent);
}
catch (Exception ex)
{
_logger.LogError($"Failed to migrate agent in file directory: {dir}\r\nError: {ex.Message}");
}
}

if (!refreshedAgents.IsNullOrEmpty())
if (agents.Count > 0)
{
var agentIds = agents.Select(a => a.Id).ToList();
await _db.DeleteAgentsAsync(agentIds);
await Task.Delay(200);
await _db.BulkInsertAgentsAsync(agents);
await Task.Delay(200);
await _db.BulkInsertAgentTasksAsync(agentTasks);

Utilities.ClearCache();
refreshResult = $"Agents are migrated!\r\n{string.Join("\r\n", refreshedAgents)}";
refreshResult = $"Agents are migrated!\r\n{string.Join("\r\n", agents.Select(a => a.Name))}";
}
else
{
Expand All @@ -91,4 +88,36 @@ public async Task<string> RefreshAgents()
_logger.LogInformation(refreshResult);
return refreshResult;
}

private (Agent? agent, string msg) GetAgentFormJson(string agentPath)
{
var agentJson = File.ReadAllText(agentPath);
if (string.IsNullOrWhiteSpace(agentJson))
return (null, $"Cannot find agent in file path: {agentPath}");

var isJson = IsValidedJson(agentJson);
if (isJson)
{
var agent = JsonSerializer.Deserialize<Agent>(agentJson, _options);
return (agent, "ok");
}
else
{
return (null, "The agent.json file data is not in JSON format!");
}
}

private bool IsValidedJson(string jsonString)
{
try
{
JsonDocument.Parse(jsonString);
return true;
}
catch (JsonException ex)
{
return false;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ private void UpdateAgentName(string agentId, string name)
if (agent == null) return;

agent.Name = name;
agent.UpdatedDateTime = DateTime.UtcNow;
agent.UpdatedTime = DateTime.UtcNow;
var json = JsonSerializer.Serialize(agent, _options);
File.WriteAllText(agentFile, json);
}
Expand All @@ -104,7 +104,7 @@ private void UpdateAgentDescription(string agentId, string description)
if (agent == null) return;

agent.Description = description;
agent.UpdatedDateTime = DateTime.UtcNow;
agent.UpdatedTime = DateTime.UtcNow;
var json = JsonSerializer.Serialize(agent, _options);
File.WriteAllText(agentFile, json);
}
Expand All @@ -115,7 +115,7 @@ private void UpdateAgentIsPublic(string agentId, bool isPublic)
if (agent == null) return;

agent.IsPublic = isPublic;
agent.UpdatedDateTime = DateTime.UtcNow;
agent.UpdatedTime = DateTime.UtcNow;
var json = JsonSerializer.Serialize(agent, _options);
File.WriteAllText(agentFile, json);
}
Expand All @@ -126,7 +126,7 @@ private void UpdateAgentDisabled(string agentId, bool disabled)
if (agent == null) return;

agent.Disabled = disabled;
agent.UpdatedDateTime = DateTime.UtcNow;
agent.UpdatedTime = DateTime.UtcNow;
var json = JsonSerializer.Serialize(agent, _options);
File.WriteAllText(agentFile, json);
}
Expand All @@ -137,7 +137,7 @@ private void UpdateAgentType(string agentId, string type)
if (agent == null) return;

agent.Type = type;
agent.UpdatedDateTime = DateTime.UtcNow;
agent.UpdatedTime = DateTime.UtcNow;
var json = JsonSerializer.Serialize(agent, _options);
File.WriteAllText(agentFile, json);
}
Expand All @@ -148,7 +148,7 @@ private void UpdateAgentInheritAgentId(string agentId, string? inheritAgentId)
if (agent == null) return;

agent.InheritAgentId = inheritAgentId;
agent.UpdatedDateTime = DateTime.UtcNow;
agent.UpdatedTime = DateTime.UtcNow;
var json = JsonSerializer.Serialize(agent, _options);
File.WriteAllText(agentFile, json);
}
Expand All @@ -161,7 +161,7 @@ private void UpdateAgentProfiles(string agentId, List<string> profiles)
if (agent == null) return;

agent.Profiles = profiles;
agent.UpdatedDateTime = DateTime.UtcNow;
agent.UpdatedTime = DateTime.UtcNow;
var json = JsonSerializer.Serialize(agent, _options);
File.WriteAllText(agentFile, json);
}
Expand All @@ -174,7 +174,7 @@ public bool UpdateAgentLabels(string agentId, List<string> labels)
if (agent == null) return false;

agent.Labels = labels;
agent.UpdatedDateTime = DateTime.UtcNow;
agent.UpdatedTime = DateTime.UtcNow;
var json = JsonSerializer.Serialize(agent, _options);
File.WriteAllText(agentFile, json);
return true;
Expand All @@ -189,7 +189,7 @@ private void UpdateAgentUtilities(string agentId, bool mergeUtility, List<AgentU

agent.MergeUtility = mergeUtility;
agent.Utilities = utilities;
agent.UpdatedDateTime = DateTime.UtcNow;
agent.UpdatedTime = DateTime.UtcNow;
var json = JsonSerializer.Serialize(agent, _options);
File.WriteAllText(agentFile, json);
}
Expand All @@ -216,7 +216,7 @@ private void UpdateAgentKnowledgeBases(string agentId, List<AgentKnowledgeBase>
if (agent == null) return;

agent.KnowledgeBases = knowledgeBases;
agent.UpdatedDateTime = DateTime.UtcNow;
agent.UpdatedTime = DateTime.UtcNow;
var json = JsonSerializer.Serialize(agent, _options);
File.WriteAllText(agentFile, json);
}
Expand All @@ -229,7 +229,7 @@ private void UpdateAgentRules(string agentId, List<AgentRule> rules)
if (agent == null) return;

agent.Rules = rules;
agent.UpdatedDateTime = DateTime.UtcNow;
agent.UpdatedTime = DateTime.UtcNow;
var json = JsonSerializer.Serialize(agent, _options);
File.WriteAllText(agentFile, json);
}
Expand All @@ -242,7 +242,7 @@ private void UpdateAgentRoutingRules(string agentId, List<RoutingRule> rules)
if (agent == null) return;

agent.RoutingRules = rules;
agent.UpdatedDateTime = DateTime.UtcNow;
agent.UpdatedTime = DateTime.UtcNow;
var json = JsonSerializer.Serialize(agent, _options);
File.WriteAllText(agentFile, json);
}
Expand Down Expand Up @@ -347,7 +347,7 @@ private void UpdateAgentLlmConfig(string agentId, AgentLlmConfig? config)
if (agent == null) return;

agent.LlmConfig = config;
agent.UpdatedDateTime = DateTime.UtcNow;
agent.UpdatedTime = DateTime.UtcNow;
var json = JsonSerializer.Serialize(agent, _options);
File.WriteAllText(agentFile, json);
}
Expand All @@ -358,7 +358,7 @@ private void UpdateAgentMaxMessageCount(string agentId, int? maxMessageCount)
if (agent == null) return;

agent.MaxMessageCount = maxMessageCount;
agent.UpdatedDateTime = DateTime.UtcNow;
agent.UpdatedTime = DateTime.UtcNow;
var json = JsonSerializer.Serialize(agent, _options);
File.WriteAllText(agentFile, json);
}
Expand All @@ -383,7 +383,7 @@ private void UpdateAgentAllFields(Agent inputAgent)
agent.Rules = inputAgent.Rules;
agent.LlmConfig = inputAgent.LlmConfig;
agent.MaxMessageCount = inputAgent.MaxMessageCount;
agent.UpdatedDateTime = DateTime.UtcNow;
agent.UpdatedTime = DateTime.UtcNow;
var json = JsonSerializer.Serialize(agent, _options);
File.WriteAllText(agentFile, json);

Expand Down Expand Up @@ -565,7 +565,7 @@ public bool AppendAgentLabels(string agentId, List<string> labels)
var prevLabels = agent.Labels ?? [];
var curLabels = prevLabels.Concat(labels).Distinct().ToList();
agent.Labels = curLabels;
agent.UpdatedDateTime = DateTime.UtcNow;
agent.UpdatedTime = DateTime.UtcNow;
var json = JsonSerializer.Serialize(agent, _options);
File.WriteAllText(agentFile, json);
return true;
Expand Down Expand Up @@ -600,6 +600,12 @@ public void BulkInsertAgents(List<Agent> agents)
ResetInnerAgents();
}

public async ValueTask BulkInsertAgentsAsync(List<Agent> agents)
{
await Task.Delay(100);
BulkInsertAgents(agents);
}

public void BulkInsertUserAgents(List<UserAgent> userAgents)
{
if (userAgents.IsNullOrEmpty()) return;
Expand Down Expand Up @@ -633,11 +639,35 @@ public void BulkInsertUserAgents(List<UserAgent> userAgents)
ResetInnerAgents();
}

public async ValueTask BulkInsertUserAgentsAsync(List<UserAgent> userAgents)
{
await Task.Delay(200);
BulkInsertUserAgents(userAgents);
}

public bool DeleteAgents()
{
return false;
}

public async Task<bool> DeleteAgentsAsync()
{
await Task.Delay(100);
return false;
}

public async ValueTask<bool> DeleteAgentsAsync(List<string> agentIds)
{
bool isDelete = false;
foreach (var agentId in agentIds)
{
isDelete = DeleteAgent(agentId);
await Task.Delay(200);
}

return isDelete;
}

public bool DeleteAgent(string agentId)
{
if (string.IsNullOrEmpty(agentId)) return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,13 @@ public void BulkInsertAgentTasks(List<AgentTask> tasks)

}

public async ValueTask BulkInsertAgentTasksAsync(List<AgentTask> tasks)
{
if (tasks.IsNullOrEmpty()) return;

await Task.Delay(200);
}

public void UpdateAgentTask(AgentTask task, AgentTaskField field)
{
if (task == null || string.IsNullOrEmpty(task.Id)) return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ public static AgentViewModel FromAgent(Agent agent)
Rules = agent.Rules ?? [],
LlmConfig = agent.LlmConfig,
Plugin = agent.Plugin,
CreatedDateTime = agent.CreatedDateTime,
UpdatedDateTime = agent.UpdatedDateTime
CreatedDateTime = agent.CreatedTime,
UpdatedDateTime = agent.UpdatedTime
};
}
}
Loading