Merge pull request #789 from iceljc/feature/refine-cronttab
Feature/refine cronttab
This commit is contained in:
commit
ba96443c15
|
|
@ -2,9 +2,6 @@ namespace BotSharp.Abstraction.Crontab.Models;
|
|||
|
||||
public class CrontabItem : ScheduleTaskArgs
|
||||
{
|
||||
[JsonPropertyName("id")]
|
||||
public string Id { get; set; }
|
||||
|
||||
[JsonPropertyName("user_id")]
|
||||
public string UserId { get; set; } = null!;
|
||||
|
||||
|
|
|
|||
|
|
@ -11,9 +11,6 @@ public class CrontabItemFilter : Pagination
|
|||
[JsonPropertyName("conversation_ids")]
|
||||
public IEnumerable<string>? ConversationIds { get; set; }
|
||||
|
||||
[JsonPropertyName("titles")]
|
||||
public IEnumerable<string>? Titles { get; set; }
|
||||
|
||||
public CrontabItemFilter()
|
||||
{
|
||||
|
||||
|
|
|
|||
|
|
@ -12,7 +12,7 @@ public class ScheduleTaskArgs
|
|||
public string Description { get; set; } = null!;
|
||||
|
||||
[JsonPropertyName("to_do_list")]
|
||||
public ScheduleTaskItemArgs[] Tasks { get; set; } = null!;
|
||||
public ScheduleTaskItemArgs[] Tasks { get; set; } = [];
|
||||
}
|
||||
|
||||
public class ScheduleTaskItemArgs
|
||||
|
|
|
|||
|
|
@ -147,7 +147,7 @@ public interface IBotSharpRepository : IHaveServiceProvider
|
|||
#endregion
|
||||
|
||||
#region Crontab
|
||||
bool InsertCrontabItem(CrontabItem item) => throw new NotImplementedException();
|
||||
bool InsertCrontabItem(CrontabItem cron) => throw new NotImplementedException();
|
||||
PagedItems<CrontabItem> GetCrontabItems(CrontabItemFilter filter) => throw new NotImplementedException();
|
||||
#endregion
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,30 +5,24 @@ namespace BotSharp.Core.Repository;
|
|||
|
||||
public partial class FileRepository
|
||||
{
|
||||
public bool InsertCrontabItem(CrontabItem item)
|
||||
public bool InsertCrontabItem(CrontabItem cron)
|
||||
{
|
||||
if (item == null)
|
||||
if (cron == null || string.IsNullOrWhiteSpace(cron.ConversationId))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var baseDir = Path.Combine(_dbSettings.FileRepository, CRONTAB_FOLDER);
|
||||
item.Id = Guid.NewGuid().ToString();
|
||||
var dir = Path.Combine(baseDir, item.Id);
|
||||
|
||||
if (Directory.Exists(dir))
|
||||
var baseDir = Path.Combine(_dbSettings.FileRepository, _conversationSettings.DataDir, cron.ConversationId);
|
||||
if (!Directory.Exists(baseDir))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
Directory.CreateDirectory(dir);
|
||||
Thread.Sleep(50);
|
||||
|
||||
var itemFile = Path.Combine(dir, CRONTAB_FILE);
|
||||
var json = JsonSerializer.Serialize(item, _options);
|
||||
File.WriteAllText(itemFile, json);
|
||||
var cronFile = Path.Combine(baseDir, CRON_FILE);
|
||||
var json = JsonSerializer.Serialize(cron, _options);
|
||||
File.WriteAllText(cronFile, json);
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
|
|
@ -48,17 +42,17 @@ public partial class FileRepository
|
|||
}
|
||||
|
||||
var records = new List<CrontabItem>();
|
||||
var dir = Path.Combine(_dbSettings.FileRepository, CRONTAB_FOLDER);
|
||||
var baseDir = Path.Combine(_dbSettings.FileRepository, _conversationSettings.DataDir);
|
||||
|
||||
if (!Directory.Exists(dir))
|
||||
if (!Directory.Exists(baseDir))
|
||||
{
|
||||
Directory.CreateDirectory(dir);
|
||||
Directory.CreateDirectory(baseDir);
|
||||
}
|
||||
|
||||
var totalDirs = Directory.GetDirectories(dir);
|
||||
var totalDirs = Directory.GetDirectories(baseDir);
|
||||
foreach (var d in totalDirs)
|
||||
{
|
||||
var file = Path.Combine(d, CRONTAB_FILE);
|
||||
var file = Path.Combine(d, CRON_FILE);
|
||||
if (!File.Exists(file)) continue;
|
||||
|
||||
var json = File.ReadAllText(file);
|
||||
|
|
@ -78,10 +72,6 @@ public partial class FileRepository
|
|||
{
|
||||
matched = matched && filter.UserIds.Contains(record.UserId);
|
||||
}
|
||||
if (filter?.Titles != null)
|
||||
{
|
||||
matched = matched && filter.Titles.Contains(record.Title);
|
||||
}
|
||||
|
||||
if (!matched) continue;
|
||||
|
||||
|
|
|
|||
|
|
@ -55,8 +55,7 @@ public partial class FileRepository : IBotSharpRepository
|
|||
private const string PLUGIN_CONFIG_FILE = "config.json";
|
||||
private const string STATS_FILE = "stats.json";
|
||||
|
||||
private const string CRONTAB_FOLDER = "crontabs";
|
||||
private const string CRONTAB_FILE = "crontab.json";
|
||||
private const string CRON_FILE = "cron.json";
|
||||
|
||||
public FileRepository(
|
||||
IServiceProvider services,
|
||||
|
|
|
|||
|
|
@ -11,13 +11,13 @@ public class CrontabItemDocument : MongoBase
|
|||
public string Cron { get; set; }
|
||||
public string Title { get; set; }
|
||||
public string Description { get; set; }
|
||||
public IEnumerable<CronTaskMongoElement> Tasks { get; set; } = [];
|
||||
public DateTime CreatedTime { get; set; } = DateTime.UtcNow;
|
||||
|
||||
public static CrontabItem ToDomainModel(CrontabItemDocument item)
|
||||
{
|
||||
return new CrontabItem
|
||||
{
|
||||
Id = item.Id,
|
||||
UserId = item.UserId,
|
||||
AgentId = item.AgentId,
|
||||
ConversationId = item.ConversationId,
|
||||
|
|
@ -25,6 +25,7 @@ public class CrontabItemDocument : MongoBase
|
|||
Cron = item.Cron,
|
||||
Title = item.Title,
|
||||
Description = item.Description,
|
||||
Tasks = item.Tasks?.Select(x => CronTaskMongoElement.ToDomainElement(x))?.ToArray() ?? [],
|
||||
CreatedTime = item.CreatedTime
|
||||
};
|
||||
}
|
||||
|
|
@ -33,7 +34,6 @@ public class CrontabItemDocument : MongoBase
|
|||
{
|
||||
return new CrontabItemDocument
|
||||
{
|
||||
Id = item.Id,
|
||||
UserId = item.UserId,
|
||||
AgentId = item.AgentId,
|
||||
ConversationId = item.ConversationId,
|
||||
|
|
@ -41,6 +41,7 @@ public class CrontabItemDocument : MongoBase
|
|||
Cron = item.Cron,
|
||||
Title = item.Title,
|
||||
Description = item.Description,
|
||||
Tasks = item.Tasks?.Select(x => CronTaskMongoElement.ToMongoElement(x))?.ToList() ?? [],
|
||||
CreatedTime = item.CreatedTime
|
||||
};
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,30 @@
|
|||
using BotSharp.Abstraction.Crontab.Models;
|
||||
|
||||
namespace BotSharp.Plugin.MongoStorage.Models;
|
||||
|
||||
public class CronTaskMongoElement
|
||||
{
|
||||
public string Topic { get; set; }
|
||||
public string Script { get; set; }
|
||||
public string Language { get; set; }
|
||||
|
||||
public static CronTaskMongoElement ToMongoElement(ScheduleTaskItemArgs model)
|
||||
{
|
||||
return new CronTaskMongoElement
|
||||
{
|
||||
Topic = model.Topic,
|
||||
Script = model.Script,
|
||||
Language = model.Language
|
||||
};
|
||||
}
|
||||
|
||||
public static ScheduleTaskItemArgs ToDomainElement(CronTaskMongoElement model)
|
||||
{
|
||||
return new ScheduleTaskItemArgs
|
||||
{
|
||||
Topic = model.Topic,
|
||||
Script = model.Script,
|
||||
Language = model.Language
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
@ -56,6 +56,7 @@ public partial class MongoRepository
|
|||
var filterPromptLog = Builders<LlmCompletionLogDocument>.Filter.In(x => x.ConversationId, conversationIds);
|
||||
var filterContentLog = Builders<ConversationContentLogDocument>.Filter.In(x => x.ConversationId, conversationIds);
|
||||
var filterStateLog = Builders<ConversationStateLogDocument>.Filter.In(x => x.ConversationId, conversationIds);
|
||||
var conbTabItems = Builders<CrontabItemDocument>.Filter.In(x => x.ConversationId, conversationIds);
|
||||
|
||||
var exeLogDeleted = _dc.ExectionLogs.DeleteMany(filterExeLog);
|
||||
var promptLogDeleted = _dc.LlmCompletionLogs.DeleteMany(filterPromptLog);
|
||||
|
|
@ -63,11 +64,13 @@ public partial class MongoRepository
|
|||
var stateLogDeleted = _dc.StateLogs.DeleteMany(filterStateLog);
|
||||
var statesDeleted = _dc.ConversationStates.DeleteMany(filterSates);
|
||||
var dialogDeleted = _dc.ConversationDialogs.DeleteMany(filterDialog);
|
||||
var cronDeleted = _dc.CrontabItems.DeleteMany(conbTabItems);
|
||||
var convDeleted = _dc.Conversations.DeleteMany(filterConv);
|
||||
|
||||
return convDeleted.DeletedCount > 0 || dialogDeleted.DeletedCount > 0 || statesDeleted.DeletedCount > 0
|
||||
|| exeLogDeleted.DeletedCount > 0 || promptLogDeleted.DeletedCount > 0
|
||||
|| contentLogDeleted.DeletedCount > 0 || stateLogDeleted.DeletedCount > 0;
|
||||
|| contentLogDeleted.DeletedCount > 0 || stateLogDeleted.DeletedCount > 0
|
||||
|| convDeleted.DeletedCount > 0;
|
||||
}
|
||||
|
||||
[SideCar]
|
||||
|
|
|
|||
|
|
@ -14,8 +14,8 @@ public partial class MongoRepository
|
|||
|
||||
try
|
||||
{
|
||||
item.Id = Guid.NewGuid().ToString();
|
||||
var cronDoc = CrontabItemDocument.ToMongoModel(item);
|
||||
cronDoc.Id = Guid.NewGuid().ToString();
|
||||
_dc.CrontabItems.InsertOne(cronDoc);
|
||||
return true;
|
||||
}
|
||||
|
|
@ -46,10 +46,6 @@ public partial class MongoRepository
|
|||
{
|
||||
cronFilters.Add(cronBuilder.In(x => x.ConversationId, filter.ConversationIds));
|
||||
}
|
||||
if (filter?.Titles != null)
|
||||
{
|
||||
cronFilters.Add(cronBuilder.In(x => x.Title, filter.Titles));
|
||||
}
|
||||
if (filter?.UserIds != null)
|
||||
{
|
||||
cronFilters.Add(cronBuilder.In(x => x.UserId, filter.UserIds));
|
||||
|
|
|
|||
Loading…
Reference in a new issue