add create agent and update agent

This commit is contained in:
Jicheng Lu 2023-08-28 00:28:14 -05:00
parent 00d61501cc
commit d7d0af2c15
19 changed files with 342 additions and 281 deletions

View file

@ -28,6 +28,11 @@ public class Agent
/// </summary>
public string Knowledges { get; set; }
/// <summary>
/// Routes
/// </summary>
public List<string> Routes { get; set; }
public override string ToString()
=> $"{Name} {Id}";
}

View file

@ -9,6 +9,12 @@ public class AgentRecord : RecordBase
[MaxLength(512)]
public string? Description { get; set; }
public string Instruction { get; set; }
public string Functions { get; set; }
public List<string> Routes { get; set; }
[Required]
public DateTime CreatedTime { get; set; }
@ -21,7 +27,10 @@ public class AgentRecord : RecordBase
{
Id = agent.Id,
Name = agent.Name,
Description = agent.Description
Description = agent.Description,
Instruction = agent.Instruction,
Functions = agent.Functions,
Routes = agent.Routes,
};
}
@ -32,6 +41,9 @@ public class AgentRecord : RecordBase
Id = Id,
Name = Name,
Description = Description,
Instruction = Instruction,
Functions = Functions,
Routes = Routes,
CreatedDateTime = CreatedTime,
UpdatedDateTime = UpdatedTime
};

View file

@ -1,6 +1,7 @@
global using System;
global using System.Collections.Generic;
global using System.Text;
global using System.Linq;
global using System.Threading.Tasks;
global using System.ComponentModel.DataAnnotations;
global using BotSharp.Abstraction.Agents.Models;

View file

@ -0,0 +1,9 @@
namespace BotSharp.Abstraction.Utilities;
public static class ListExtenstion
{
public static bool IsEmpty(this IEnumerable<string> strList)
{
return strList == null || !strList.Any();
}
}

View file

@ -1,6 +1,7 @@
using BotSharp.Abstraction.Agents.Models;
using BotSharp.Abstraction.Repositories;
using BotSharp.Abstraction.Repositories.Records;
using MongoDB.Bson;
namespace BotSharp.Core.Agents.Services;
@ -22,13 +23,12 @@ public partial class AgentService
}
record = AgentRecord.FromAgent(agent);
record.Id = Guid.NewGuid().ToString();
record.Id = ObjectId.GenerateNewId().ToString();
record.CreatedTime = DateTime.UtcNow;
record.UpdatedTime = DateTime.UtcNow;
var userAgentRecord = new UserAgentRecord
{
Id = Guid.NewGuid().ToString(),
UserId = _user.Id,
AgentId = record.Id,
CreatedTime = DateTime.UtcNow,

View file

@ -44,7 +44,7 @@ public partial class AgentService
var functionsFile = Path.Combine(dir, "functions.json");
if (File.Exists(functionsFile))
{
profile.Functions = File.ReadAllText(functionsFile);
//profile.Functions = File.ReadAllText(functionsFile);
}
return profile;

View file

@ -17,19 +17,34 @@ public partial class AgentService
join u in db.User on ua.UserId equals u.Id
where (ua.UserId == _user.Id || u.ExternalId == _user.Id) &&
a.Id == agent.Id
select a).First();
select a).FirstOrDefault();
if (record == null) return;
record.Name = agent.Name;
record.Description = agent.Description;
if (!string.IsNullOrEmpty(agent.Description))
record.Description = agent.Description;
if (!string.IsNullOrEmpty(agent.Instruction))
record.Instruction = agent.Instruction;
if (!string.IsNullOrEmpty(agent.Functions))
record.Functions = agent.Functions;
if (!agent.Routes.IsEmpty())
record.Routes = agent.Routes;
record.UpdatedTime = DateTime.UtcNow;
db.Add<IBotSharpTable>(record);
});
// Save instruction to file
var dir = GetAgentDataDir(agent.Id);
var instructionFile = Path.Combine(dir, "instruction.txt");
File.WriteAllText(instructionFile, agent.Instruction);
//var dir = GetAgentDataDir(agent.Id);
//var instructionFile = Path.Combine(dir, "instruction.txt");
//File.WriteAllText(instructionFile, agent.Instruction);
var samplesFile = Path.Combine(dir, "samples.txt");
File.WriteAllText(samplesFile, agent.Samples);
//var samplesFile = Path.Combine(dir, "samples.txt");
//File.WriteAllText(samplesFile, agent.Samples);
}
}

View file

@ -29,7 +29,6 @@ public class UserService : IUserService
}
record = UserRecord.FromUser(user);
//record.Id = Guid.NewGuid().ToString();
record.Email = user.Email.ToLower();
record.Salt = Guid.NewGuid().ToString("N");
record.Password = Utilities.HashText(user.Password, record.Salt);

View file

@ -6,13 +6,19 @@ public class AgentCreationModel
{
public string Name { get; set; }
public string Description { get; set; }
public string Instruction { get; set; }
public string Functions { get; set; }
public List<string> Routes { get; set; }
public Agent ToAgent()
{
return new Agent
{
Name = Name,
Description = Description
Description = Description,
Instruction = Instruction,
Functions = Functions,
Routes = Routes
};
}
}

View file

@ -22,6 +22,11 @@ public class AgentUpdateModel
/// </summary>
public string? Functions { get; set; }
/// <summary>
/// Routes
/// </summary>
public List<string>? Routes { get; set; }
public Agent ToAgent()
{
var agent = new Agent
@ -41,6 +46,9 @@ public class AgentUpdateModel
if (Functions != null)
agent.Functions = Functions;
if (!Routes.IsEmpty())
agent.Routes = Routes;
return agent;
}
}

View file

@ -1,14 +1,13 @@
namespace BotSharp.Plugin.MongoRepository.Collections
{
public class AgentCollection : MongoBase
{
public string Name { get; set; }
public string Description { get; set; }
public List<string> Functions { get; set; }
public string Instruction { get; set; }
public List<string> Routes { get; set; }
namespace BotSharp.Plugin.Mongo.Collections;
public DateTime CreatedTime { get; set; }
public DateTime UpdatedTime { get; set; }
}
}
public class AgentCollection : MongoBase
{
public string Name { get; set; }
public string Description { get; set; }
public string Functions { get; set; }
public string Instruction { get; set; }
public List<string> Routes { get; set; }
public DateTime CreatedTime { get; set; }
public DateTime UpdatedTime { get; set; }
}

View file

@ -1,4 +1,4 @@
namespace BotSharp.Plugin.MongoRepository.Collections;
namespace BotSharp.Plugin.Mongo.Collections;
public class ConversationCollection : MongoBase
{

View file

@ -1,11 +1,10 @@
namespace BotSharp.Plugin.MongoRepository.Collections
{
public class UserAgentCollection : MongoBase
{
public string UserId { get; set; }
public string AgentId { get; set; }
namespace BotSharp.Plugin.Mongo.Collections;
public DateTime CreatedTime { get; set; }
public DateTime UpdatedTime { get; set; }
}
public class UserAgentCollection : MongoBase
{
public string UserId { get; set; }
public string AgentId { get; set; }
public DateTime CreatedTime { get; set; }
public DateTime UpdatedTime { get; set; }
}

View file

@ -1,15 +1,14 @@
namespace BotSharp.Plugin.MongoRepository.Collections
{
public class UserCollection : MongoBase
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public string Salt { get; set; }
public string Password { get; set; }
public string? ExternalId { get; set; }
namespace BotSharp.Plugin.Mongo.Collections;
public DateTime CreatedTime { get; set; }
public DateTime UpdatedTime { get; set; }
}
}
public class UserCollection : MongoBase
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public string Salt { get; set; }
public string Password { get; set; }
public string? ExternalId { get; set; }
public DateTime CreatedTime { get; set; }
public DateTime UpdatedTime { get; set; }
}

View file

@ -1,7 +1,7 @@
using MongoDB.Bson.Serialization.Attributes;
using MongoDB.Bson.Serialization.IdGenerators;
namespace BotSharp.Plugin.MongoRepository;
namespace BotSharp.Plugin.Mongo;
[BsonIgnoreExtraElements(Inherited = true)]
public class MongoBase

View file

@ -1,6 +1,6 @@
using BotSharp.Plugin.MongoRepository.Collections;
using BotSharp.Plugin.Mongo.Collections;
namespace BotSharp.Core.Repository;
namespace BotSharp.Plugin.Mongo;
public class MongoDbContext
{

View file

@ -1,16 +1,17 @@
namespace BotSharp.Core.Repository
{
public class MongoRepositoryPlugin : IBotSharpPlugin
{
public void RegisterDI(IServiceCollection services, IConfiguration config)
{
services.AddSingleton((IServiceProvider x) =>
{
var databaseSettings = x.GetRequiredService<MyDatabaseSettings>();
return new MongoDbContext(databaseSettings.MongoDb);
});
using BotSharp.Plugin.Mongo.Repository;
services.AddScoped<IBotSharpRepository, MongoRepository>();
}
namespace BotSharp.Plugin.Mongo;
public class MongoRepositoryPlugin : IBotSharpPlugin
{
public void RegisterDI(IServiceCollection services, IConfiguration config)
{
services.AddSingleton((IServiceProvider x) =>
{
var databaseSettings = x.GetRequiredService<MyDatabaseSettings>();
return new MongoDbContext(databaseSettings.MongoDb);
});
services.AddScoped<IBotSharpRepository, MongoRepository>();
}
}

View file

@ -1,256 +1,264 @@
using BotSharp.Plugin.MongoRepository.Collections;
using BotSharp.Plugin.Mongo.Collections;
namespace BotSharp.Core.Repository
namespace BotSharp.Plugin.Mongo.Repository;
public class MongoRepository : IBotSharpRepository
{
public class MongoRepository : IBotSharpRepository
private readonly MongoDbContext _dc;
private readonly IServiceProvider _services;
private UpdateOptions _options;
public MongoRepository(MongoDbContext dc, IServiceProvider services)
{
private readonly MongoDbContext _dc;
private readonly IServiceProvider _services;
private UpdateOptions _options;
_dc = dc;
_services = services;
public MongoRepository(MongoDbContext dc, IServiceProvider services)
_options = new UpdateOptions
{
_dc = dc;
_services = services;
IsUpsert = true,
};
}
_options = new UpdateOptions
private List<AgentRecord> _agents;
public IQueryable<AgentRecord> Agent
{
get
{
if (_agents != null)
{
IsUpsert = true,
};
return _agents.AsQueryable();
}
var agentDocs = _dc.Agents?.AsQueryable()?.ToList() ?? new List<AgentCollection>();
_agents = agentDocs.Select(x => new AgentRecord
{
Id = x.Id?.ToString(),
Name = x.Name,
Description = x.Description,
Instruction = x.Instruction,
Functions = x.Functions,
Routes = x.Routes,
CreatedTime = x.CreatedTime,
UpdatedTime = x.UpdatedTime
}).ToList();
return _agents.AsQueryable();
}
}
private List<AgentRecord> _agents;
public IQueryable<AgentRecord> Agent
private List<UserRecord> _users;
public IQueryable<UserRecord> User
{
get
{
get
if (_users != null)
{
if (_agents != null)
{
return _agents.AsQueryable();
}
return _users.AsQueryable();
}
var agentDocs = _dc.Agents?.AsQueryable()?.ToList() ?? new List<AgentCollection>();
_agents = agentDocs.Select(x => new AgentRecord
{
Id = x.Id?.ToString(),
Name = x.Name,
Description = x.Description,
var userDocs = _dc.Users?.AsQueryable()?.ToList() ?? new List<UserCollection>();
_users = userDocs.Select(x => new UserRecord
{
Id = x.Id?.ToString(),
FirstName = x.FirstName,
LastName = x.LastName,
Email = x.Email,
Password = x.Password,
Salt = x.Salt,
ExternalId = x.ExternalId,
CreatedTime = x.CreatedTime,
UpdatedTime = x.UpdatedTime
}).ToList();
return _users.AsQueryable();
}
}
private List<UserAgentRecord> _userAgents;
public IQueryable<UserAgentRecord> UserAgent
{
get
{
if (_userAgents != null && _userAgents.Count > 0)
{
return _userAgents.AsQueryable();
}
var userDocs = _dc.UserAgents?.AsQueryable()?.ToList() ?? new List<UserAgentCollection>();
_userAgents = userDocs.Select(x => new UserAgentRecord
{
Id = x.Id?.ToString(),
AgentId = x.AgentId,
UserId = x.UserId,
CreatedTime = x.CreatedTime,
UpdatedTime = x.UpdatedTime
}).ToList();
return _userAgents.AsQueryable();
}
}
private List<ConversationRecord> _conversations;
public IQueryable<ConversationRecord> Conversation
{
get
{
if (_conversations != null)
{
return _conversations.AsQueryable();
}
var conversationDocs = _dc.Conversations?.AsQueryable()?.ToList() ?? new List<ConversationCollection>();
_conversations = conversationDocs.Select(x => new ConversationRecord
{
Id = x.Id?.ToString(),
AgentId = x.AgentId,
UserId = x.UserId,
Title = x.Title,
CreatedTime = x.CreatedTime,
UpdatedTime = x.UpdatedTime
}).ToList();
return _conversations.AsQueryable();
}
}
List<string> _changedTableNames = new List<string>();
public void Add<TTableInterface>(object entity)
{
if (entity is ConversationRecord conversation)
{
_conversations.Add(conversation);
_changedTableNames.Add(nameof(ConversationRecord));
}
else if (entity is AgentRecord agent)
{
_agents.Add(agent);
_changedTableNames.Add(nameof(AgentRecord));
}
else if (entity is UserRecord user)
{
_users.Add(user);
_changedTableNames.Add(nameof(UserRecord));
}
else if (entity is UserAgentRecord userAgent)
{
_userAgents.Add(userAgent);
_changedTableNames.Add(nameof(UserAgentRecord));
}
}
public int Transaction<TTableInterface>(Action action)
{
_changedTableNames.Clear();
action();
foreach (var table in _changedTableNames)
{
if (table == nameof(ConversationRecord))
{
var conversations = _conversations.Select(x => new ConversationCollection
{
Id = x.Id.IfNullOrEmptyAs(ObjectId.GenerateNewId().ToString()),
AgentId = x.AgentId,
UserId = x.UserId,
CreatedTime = x.CreatedTime,
UpdatedTime = x.UpdatedTime
}).ToList();
return _agents.AsQueryable();
}
}
private List<UserRecord> _users;
public IQueryable<UserRecord> User
{
get
{
if (_users != null)
foreach (var conversation in conversations)
{
return _users.AsQueryable();
var filter = Builders<ConversationCollection>.Filter.Eq(x => x.Id, conversation.Id);
var update = Builders<ConversationCollection>.Update
.Set(x => x.AgentId, conversation.AgentId)
.Set(x => x.UserId, conversation.UserId)
.Set(x => x.CreatedTime, conversation.CreatedTime)
.Set(x => x.UpdatedTime, conversation.UpdatedTime);
_dc.Conversations.UpdateOne(filter, update, _options);
}
var userDocs = _dc.Users?.AsQueryable()?.ToList() ?? new List<UserCollection>();
_users = userDocs.Select(x => new UserRecord
}
else if (table == nameof(AgentRecord))
{
var agents = _agents.Select(x => new AgentCollection
{
Id = x.Id?.ToString(),
Id = x.Id.IfNullOrEmptyAs(ObjectId.GenerateNewId().ToString()),
Name = x.Name,
Description = x.Description,
Instruction = x.Instruction,
Functions = x.Functions,
Routes = x.Routes,
CreatedTime = x.CreatedTime,
UpdatedTime = x.UpdatedTime
}).ToList();
foreach (var agent in agents)
{
var filter = Builders<AgentCollection>.Filter.Eq(x => x.Id, agent.Id);
var update = Builders<AgentCollection>.Update
.Set(x => x.Name, agent.Name)
.Set(x => x.Description, agent.Description)
.Set(x => x.Instruction, agent.Instruction)
.Set(x => x.Functions, agent.Functions)
.Set(x => x.Routes, agent.Routes)
.Set(x => x.CreatedTime, agent.CreatedTime)
.Set(x => x.UpdatedTime, agent.UpdatedTime);
_dc.Agents.UpdateOne(filter, update, _options);
}
}
else if (table == nameof(UserRecord))
{
var users = _users.Select(x => new UserCollection
{
Id = x.Id.IfNullOrEmptyAs(ObjectId.GenerateNewId().ToString()),
FirstName = x.FirstName,
LastName = x.LastName,
Email = x.Email,
Password = x.Password,
Salt = x.Salt,
Password = x.Password,
Email = x.Email,
ExternalId = x.ExternalId,
CreatedTime = x.CreatedTime,
UpdatedTime = x.UpdatedTime
}).ToList();
return _users.AsQueryable();
}
}
private List<UserAgentRecord> _userAgents;
public IQueryable<UserAgentRecord> UserAgent
{
get
{
if (_userAgents != null && _userAgents.Count > 0)
foreach (var user in users)
{
return _userAgents.AsQueryable();
var filter = Builders<UserCollection>.Filter.Eq(x => x.Id, user.Id);
var update = Builders<UserCollection>.Update
.Set(x => x.FirstName, user.FirstName)
.Set(x => x.LastName, user.LastName)
.Set(x => x.Email, user.Email)
.Set(x => x.Salt, user.Salt)
.Set(x => x.Password, user.Password)
.Set(x => x.ExternalId, user.ExternalId)
.Set(x => x.CreatedTime, user.CreatedTime)
.Set(x => x.UpdatedTime, user.UpdatedTime);
_dc.Users.UpdateOne(filter, update, _options);
}
var userDocs = _dc.UserAgents?.AsQueryable()?.ToList() ?? new List<UserAgentCollection>();
_userAgents = userDocs.Select(x => new UserAgentRecord
}
else if (table == nameof(UserAgentRecord))
{
var userAgents = _userAgents.Select(x => new UserAgentCollection
{
Id = x.Id?.ToString(),
Id = x.Id.IfNullOrEmptyAs(ObjectId.GenerateNewId().ToString()),
AgentId = x.AgentId,
UserId = x.UserId,
CreatedTime = x.CreatedTime,
UpdatedTime = x.UpdatedTime
}).ToList();
return _userAgents.AsQueryable();
foreach (var userAgent in userAgents)
{
var filter = Builders<UserAgentCollection>.Filter.Eq(x => x.Id, userAgent.Id);
var update = Builders<UserAgentCollection>.Update
.Set(x => x.AgentId, userAgent.AgentId)
.Set(x => x.UserId, userAgent.UserId)
.Set(x => x.CreatedTime, userAgent.CreatedTime)
.Set(x => x.UpdatedTime, userAgent.UpdatedTime);
_dc.UserAgents.UpdateOne(filter, update, _options);
}
}
}
private List<ConversationRecord> _conversations;
public IQueryable<ConversationRecord> Conversation
{
get
{
if (_conversations != null)
{
return _conversations.AsQueryable();
}
var conversationDocs = _dc.Conversations?.AsQueryable()?.ToList() ?? new List<ConversationCollection>();
_conversations = conversationDocs.Select(x => new ConversationRecord
{
Id = x.Id?.ToString(),
AgentId = x.AgentId,
UserId = x.UserId,
Title = x.Title,
CreatedTime = x.CreatedTime,
UpdatedTime = x.UpdatedTime
}).ToList();
return _conversations.AsQueryable();
}
}
List<string> _changedTableNames = new List<string>();
public void Add<TTableInterface>(object entity)
{
if (entity is ConversationRecord conversation)
{
_conversations.Add(conversation);
_changedTableNames.Add(nameof(ConversationRecord));
}
else if (entity is AgentRecord agent)
{
_agents.Add(agent);
_changedTableNames.Add(nameof(AgentRecord));
}
else if (entity is UserRecord user)
{
_users.Add(user);
_changedTableNames.Add(nameof(UserRecord));
}
else if (entity is UserAgentRecord userAgent)
{
_userAgents.Add(userAgent);
_changedTableNames.Add(nameof(UserAgentRecord));
}
}
public int Transaction<TTableInterface>(Action action)
{
_changedTableNames.Clear();
action();
foreach (var table in _changedTableNames)
{
if (table == nameof(ConversationRecord))
{
var conversations = _conversations.Select(x => new ConversationCollection
{
Id = x.Id.IfNullOrEmptyAs(ObjectId.GenerateNewId().ToString()),
AgentId = x.AgentId,
UserId = x.UserId,
CreatedTime = x.CreatedTime,
UpdatedTime = x.UpdatedTime
}).ToList();
foreach (var conversation in conversations)
{
var filter = Builders<ConversationCollection>.Filter.Eq(x => x.Id, conversation.Id);
var update = Builders<ConversationCollection>.Update
.Set(x => x.AgentId, conversation.AgentId)
.Set(x => x.UserId, conversation.UserId)
.Set(x => x.CreatedTime, conversation.CreatedTime)
.Set(x => x.UpdatedTime, conversation.UpdatedTime);
_dc.Conversations.UpdateOne(filter, update, _options);
}
}
else if (table == nameof(AgentRecord))
{
var agents = _agents.Select(x => new AgentCollection
{
Id = x.Id.IfNullOrEmptyAs(ObjectId.GenerateNewId().ToString()),
Name = x.Name,
Description = x.Description,
CreatedTime = x.CreatedTime,
UpdatedTime = x.UpdatedTime
}).ToList();
foreach (var agent in agents)
{
var filter = Builders<AgentCollection>.Filter.Eq(x => x.Id, agent.Id);
var update = Builders<AgentCollection>.Update
.Set(x => x.Name, agent.Name)
.Set(x => x.Description, agent.Description)
.Set(x => x.CreatedTime, agent.CreatedTime)
.Set(x => x.UpdatedTime, agent.UpdatedTime);
_dc.Agents.UpdateOne(filter, update, _options);
}
}
else if (table == nameof(UserRecord))
{
var users = _users.Select(x => new UserCollection
{
Id = x.Id.IfNullOrEmptyAs(ObjectId.GenerateNewId().ToString()),
FirstName = x.FirstName,
LastName = x.LastName,
Salt = x.Salt,
Password = x.Password,
Email = x.Email,
ExternalId = x.ExternalId,
CreatedTime = x.CreatedTime,
UpdatedTime = x.UpdatedTime
}).ToList();
foreach (var user in users)
{
var filter = Builders<UserCollection>.Filter.Eq(x => x.Id, user.Id);
var update = Builders<UserCollection>.Update
.Set(x => x.FirstName, user.FirstName)
.Set(x => x.LastName, user.LastName)
.Set(x => x.Email, user.Email)
.Set(x => x.Salt, user.Salt)
.Set(x => x.Password, user.Password)
.Set(x => x.ExternalId, user.ExternalId)
.Set(x => x.CreatedTime, user.CreatedTime)
.Set(x => x.UpdatedTime, user.UpdatedTime);
_dc.Users.UpdateOne(filter, update, _options);
}
}
else if (table == nameof(UserAgentRecord))
{
var userAgents = _userAgents.Select(x => new UserAgentCollection
{
Id = x.Id.IfNullOrEmptyAs(ObjectId.GenerateNewId().ToString()),
AgentId = x.AgentId,
UserId = x.UserId,
CreatedTime = x.CreatedTime,
UpdatedTime = x.UpdatedTime
}).ToList();
foreach (var userAgent in userAgents)
{
var filter = Builders<UserAgentCollection>.Filter.Eq(x => x.Id, userAgent.Id);
var update = Builders<UserAgentCollection>.Update
.Set(x => x.AgentId, userAgent.AgentId)
.Set(x => x.UserId, userAgent.UserId)
.Set(x => x.CreatedTime, userAgent.CreatedTime)
.Set(x => x.UpdatedTime, userAgent.UpdatedTime);
_dc.UserAgents.UpdateOne(filter, update, _options);
}
}
}
return _changedTableNames.Count;
}
return _changedTableNames.Count;
}
}