Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unified datetime field name #925

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
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 @@ -43,7 +43,7 @@
List<User> GetUsersByAffiliateId(string affiliateId) => throw new NotImplementedException();
User? GetUserByUserName(string userName) => throw new NotImplementedException();
void UpdateUserName(string userId, string userName) => throw new NotImplementedException();
Dashboard? GetDashboard(string id = null) => throw new NotImplementedException();

Check warning on line 46 in src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest)

Cannot convert null literal to non-nullable reference type.

Check warning on line 46 in src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

Cannot convert null literal to non-nullable reference type.
void CreateUser(User user) => throw new NotImplementedException();
void UpdateExistUser(string userId, User user) => throw new NotImplementedException();
void UpdateUserVerified(string userId) => throw new NotImplementedException();
Expand Down Expand Up @@ -73,10 +73,18 @@
=> 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 @@
=> 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 @@
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 @@
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 @@
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 @@
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 @@
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 @@
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 @@
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 @@
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 @@

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 @@ -203,7 +203,7 @@


agent.McpTools = mcptools;
agent.UpdatedDateTime = DateTime.UtcNow;

Check failure on line 206 in src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.Agent.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest)

'Agent' does not contain a definition for 'UpdatedDateTime' and no accessible extension method 'UpdatedDateTime' accepting a first argument of type 'Agent' could be found (are you missing a using directive or an assembly reference?)
var json = JsonSerializer.Serialize(agent, _options);
File.WriteAllText(agentFile, json);
}
Expand All @@ -216,7 +216,7 @@
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 @@
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 @@
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 @@
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 @@
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 @@
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 @@
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 @@
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 @@
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
Loading