add routing item and profile
This commit is contained in:
parent
5531944a9f
commit
ca6f60695d
|
|
@ -44,13 +44,13 @@ public class Agent
|
|||
|
||||
public Agent SetFunctions(List<string> functions)
|
||||
{
|
||||
Functions = functions;
|
||||
Functions = functions ?? new List<string>();
|
||||
return this;
|
||||
}
|
||||
|
||||
public Agent SetResponses(List<string> responses)
|
||||
{
|
||||
Responses = responses;
|
||||
Responses = responses ?? new List<string>(); ;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
using BotSharp.Abstraction.Repositories.Records;
|
||||
using BotSharp.Abstraction.Routing.Models;
|
||||
using System.Linq;
|
||||
|
||||
namespace BotSharp.Abstraction.Repositories;
|
||||
|
|
@ -18,4 +19,9 @@ public interface IBotSharpRepository
|
|||
UserRecord GetUserByEmail(string email);
|
||||
void CreateUser(UserRecord user);
|
||||
void UpdateAgent(AgentRecord agent);
|
||||
|
||||
List<RoutingItemRecord> CreateRoutingItems(List<RoutingItemRecord> routingItems);
|
||||
List<RoutingProfileRecord> CreateRoutingProfiles(List<RoutingProfileRecord> profiles);
|
||||
void DeleteRoutingItems();
|
||||
void DeleteRoutingProfiles();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,13 +8,14 @@ public class RoutingItemRecord : RecordBase
|
|||
public string Name { get; set; }
|
||||
public string Description { get; set; }
|
||||
public List<string> RequiredFields { get; set; } = new List<string>();
|
||||
public string RedirectTo { get; set; }
|
||||
public string? RedirectTo { get; set; }
|
||||
public bool Disabled { get; set; }
|
||||
|
||||
public RoutingItem ToRoutingItem()
|
||||
{
|
||||
return new RoutingItem
|
||||
{
|
||||
AgentId = AgentId,
|
||||
Name = Name,
|
||||
Description = Description,
|
||||
RequiredFields = RequiredFields,
|
||||
|
|
@ -22,4 +23,17 @@ public class RoutingItemRecord : RecordBase
|
|||
Disabled = Disabled
|
||||
};
|
||||
}
|
||||
|
||||
public static RoutingItemRecord FromRoutingItem(RoutingItem item)
|
||||
{
|
||||
return new RoutingItemRecord
|
||||
{
|
||||
AgentId = item.AgentId,
|
||||
Name = item.Name,
|
||||
Description = item.Description,
|
||||
RequiredFields = item.RequiredFields,
|
||||
RedirectTo = item.RedirectTo,
|
||||
Disabled = item.Disabled
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,7 +1,27 @@
|
|||
using BotSharp.Abstraction.Routing.Models;
|
||||
|
||||
namespace BotSharp.Abstraction.Repositories.Records;
|
||||
|
||||
public class RoutingProfileRecord : RecordBase
|
||||
{
|
||||
public string Name { get; set; }
|
||||
public List<string> AgentIds { get; set; }
|
||||
|
||||
public RoutingProfile ToRoutingProfile()
|
||||
{
|
||||
return new RoutingProfile
|
||||
{
|
||||
Name = Name,
|
||||
AgentIds = AgentIds.ToArray(),
|
||||
};
|
||||
}
|
||||
|
||||
public static RoutingProfileRecord FromRoutingProfile(RoutingProfile profile)
|
||||
{
|
||||
return new RoutingProfileRecord
|
||||
{
|
||||
Name = profile.Name,
|
||||
AgentIds = profile.AgentIds.ToList(),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,11 @@
|
|||
using BotSharp.Abstraction.Routing.Models;
|
||||
|
||||
namespace BotSharp.Abstraction.Routing;
|
||||
|
||||
public interface IRoutingService
|
||||
{
|
||||
Task<List<RoutingItem>> CreateRoutingItems(List<RoutingItem> routingItems);
|
||||
Task<List<RoutingProfile>> CreateRoutingProfiles(List<RoutingProfile> routingProfiles);
|
||||
Task DeleteRoutingItems();
|
||||
Task DeleteRoutingProfiles();
|
||||
}
|
||||
|
|
@ -17,7 +17,7 @@ public class RoutingItem
|
|||
public List<string> RequiredFields { get; set; } = new List<string>();
|
||||
|
||||
[JsonPropertyName("redirect_to")]
|
||||
public string RedirectTo { get; set; }
|
||||
public string? RedirectTo { get; set; }
|
||||
|
||||
[JsonPropertyName("disabled")]
|
||||
public bool Disabled { get; set; }
|
||||
|
|
|
|||
|
|
@ -10,6 +10,8 @@ using Microsoft.AspNetCore.Builder;
|
|||
using Microsoft.Extensions.Configuration;
|
||||
using BotSharp.Abstraction.Routing.Settings;
|
||||
using BotSharp.Abstraction.Templating;
|
||||
using BotSharp.Abstraction.Routing;
|
||||
using BotSharp.Core.Routing.Services;
|
||||
|
||||
namespace BotSharp.Core;
|
||||
|
||||
|
|
@ -20,6 +22,7 @@ public static class BotSharpServiceCollectionExtensions
|
|||
services.AddScoped<IUserService, UserService>();
|
||||
|
||||
services.AddScoped<IAgentService, AgentService>();
|
||||
services.AddScoped<IRoutingService, RoutingService>();
|
||||
|
||||
var agentSettings = new AgentSettings();
|
||||
config.Bind("Agent", agentSettings);
|
||||
|
|
|
|||
|
|
@ -76,4 +76,24 @@ public class BotSharpDbContext : Database, IBotSharpRepository
|
|||
throw ex2;
|
||||
}
|
||||
}
|
||||
|
||||
public void DeleteRoutingItems()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public void DeleteRoutingProfiles()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public List<RoutingItemRecord> CreateRoutingItems(List<RoutingItemRecord> routingItems)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public List<RoutingProfileRecord> CreateRoutingProfiles(List<RoutingProfileRecord> profiles)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,10 +1,8 @@
|
|||
using BotSharp.Abstraction.Functions.Models;
|
||||
using BotSharp.Abstraction.Repositories;
|
||||
using BotSharp.Abstraction.Repositories.Records;
|
||||
using BotSharp.Abstraction.Routing.Models;
|
||||
using SharpCompress.Common;
|
||||
using System.IO;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace BotSharp.Core.Repository;
|
||||
|
||||
public class FileRepository : IBotSharpRepository
|
||||
|
|
@ -324,4 +322,24 @@ public class FileRepository : IBotSharpRepository
|
|||
}
|
||||
return dir;
|
||||
}
|
||||
|
||||
public void DeleteRoutingItems()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public void DeleteRoutingProfiles()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public List<RoutingItemRecord> CreateRoutingItems(List<RoutingItemRecord> routingItems)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public List<RoutingProfileRecord> CreateRoutingProfiles(List<RoutingProfileRecord> profiles)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,45 @@
|
|||
using BotSharp.Abstraction.Repositories;
|
||||
using BotSharp.Abstraction.Repositories.Records;
|
||||
using BotSharp.Abstraction.Routing;
|
||||
using BotSharp.Abstraction.Routing.Models;
|
||||
|
||||
namespace BotSharp.Core.Routing.Services;
|
||||
|
||||
public class RoutingService : IRoutingService
|
||||
{
|
||||
private readonly IServiceProvider _services;
|
||||
|
||||
public RoutingService(IServiceProvider service)
|
||||
{
|
||||
_services = service;
|
||||
}
|
||||
public async Task<List<RoutingItem>> CreateRoutingItems(List<RoutingItem> routingItems)
|
||||
{
|
||||
var db = _services.GetRequiredService<IBotSharpRepository>();
|
||||
var items = routingItems?.Select(x => RoutingItemRecord.FromRoutingItem(x))?.ToList() ?? new List<RoutingItemRecord>();
|
||||
var savedItems = db.CreateRoutingItems(items);
|
||||
return await Task.FromResult(savedItems.Select(x => x.ToRoutingItem()).ToList());
|
||||
}
|
||||
|
||||
public async Task<List<RoutingProfile>> CreateRoutingProfiles(List<RoutingProfile> routingProfiles)
|
||||
{
|
||||
var db = _services.GetRequiredService<IBotSharpRepository>();
|
||||
var profiles = routingProfiles?.Select(x => RoutingProfileRecord.FromRoutingProfile(x))?.ToList() ?? new List<RoutingProfileRecord>();
|
||||
var savedProfiles = db.CreateRoutingProfiles(profiles);
|
||||
return await Task.FromResult(savedProfiles.Select(x => x.ToRoutingProfile()).ToList());
|
||||
}
|
||||
|
||||
public async Task DeleteRoutingItems()
|
||||
{
|
||||
var db = _services.GetRequiredService<IBotSharpRepository>();
|
||||
db.DeleteRoutingItems();
|
||||
await Task.CompletedTask;
|
||||
}
|
||||
|
||||
public async Task DeleteRoutingProfiles()
|
||||
{
|
||||
var db = _services.GetRequiredService<IBotSharpRepository>();
|
||||
db.DeleteRoutingProfiles();
|
||||
await Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,46 @@
|
|||
using BotSharp.Abstraction.ApiAdapters;
|
||||
using BotSharp.Abstraction.Routing;
|
||||
using BotSharp.Abstraction.Routing.Models;
|
||||
using BotSharp.OpenAPI.ViewModels.Routing;
|
||||
using NetTopologySuite.Index.HPRtree;
|
||||
|
||||
namespace BotSharp.OpenAPI.Controllers;
|
||||
|
||||
[Authorize]
|
||||
[ApiController]
|
||||
public class RoutingController : ControllerBase, IApiAdapter
|
||||
{
|
||||
private readonly IRoutingService _routingService;
|
||||
public RoutingController(IRoutingService routingService)
|
||||
{
|
||||
_routingService = routingService;
|
||||
}
|
||||
|
||||
[HttpPost("/routing-items")]
|
||||
public async Task<List<RoutingItemViewModel>> CreateRoutingItems(List<RoutingItemCreationModel> routingItems)
|
||||
{
|
||||
var items = routingItems?.Select(x => x.ToRoutingItem())?.ToList() ?? new List<RoutingItem>();
|
||||
var savedItems = await _routingService.CreateRoutingItems(items);
|
||||
return savedItems.Select(x => RoutingItemViewModel.FromRoutingItem(x)).ToList();
|
||||
}
|
||||
|
||||
[HttpPost("/routing-profiles")]
|
||||
public async Task<List<RoutingProfileViewModel>> CreateRoutingProfiles(List<RoutingProfileCreationModel> routingProfiles)
|
||||
{
|
||||
var profiles = routingProfiles?.Select(x => x.ToRoutingProfile())?.ToList() ?? new List<RoutingProfile>();
|
||||
var savedProfiles = await _routingService.CreateRoutingProfiles(profiles);
|
||||
return savedProfiles.Select(x => RoutingProfileViewModel.FromRoutingProfile(x)).ToList();
|
||||
}
|
||||
|
||||
[HttpDelete("/routing-items")]
|
||||
public async Task RemoveRoutingItems()
|
||||
{
|
||||
await _routingService.DeleteRoutingItems();
|
||||
}
|
||||
|
||||
[HttpDelete("/routing-profiles")]
|
||||
public async Task RemoveRoutingProfiles()
|
||||
{
|
||||
await _routingService.DeleteRoutingProfiles();
|
||||
}
|
||||
}
|
||||
|
|
@ -8,6 +8,7 @@ public class AgentCreationModel
|
|||
public string Description { get; set; }
|
||||
public string Instruction { get; set; }
|
||||
public List<string> Functions { get; set; }
|
||||
public List<string> Responses { get; set; }
|
||||
|
||||
public Agent ToAgent()
|
||||
{
|
||||
|
|
@ -17,6 +18,7 @@ public class AgentCreationModel
|
|||
Description = Description,
|
||||
Instruction = Instruction,
|
||||
Functions = Functions,
|
||||
Responses = Responses
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,26 @@
|
|||
using BotSharp.Abstraction.Routing.Models;
|
||||
|
||||
namespace BotSharp.OpenAPI.ViewModels.Routing;
|
||||
|
||||
public class RoutingItemCreationModel
|
||||
{
|
||||
public string AgentId { get; set; }
|
||||
public string Name { get; set; }
|
||||
public string Description { get; set; }
|
||||
public List<string> RequiredFields { get; set; } = new List<string>();
|
||||
public string? RedirectTo { get; set; }
|
||||
public bool Disabled { get; set; }
|
||||
|
||||
public RoutingItem ToRoutingItem()
|
||||
{
|
||||
return new RoutingItem
|
||||
{
|
||||
AgentId = AgentId,
|
||||
Name = Name,
|
||||
Description = Description,
|
||||
RequiredFields = RequiredFields,
|
||||
RedirectTo = RedirectTo,
|
||||
Disabled = Disabled
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,26 @@
|
|||
using BotSharp.Abstraction.Routing.Models;
|
||||
|
||||
namespace BotSharp.OpenAPI.ViewModels.Routing;
|
||||
|
||||
public class RoutingItemViewModel
|
||||
{
|
||||
public string AgentId { get; set; }
|
||||
public string Name { get; set; }
|
||||
public string Description { get; set; }
|
||||
public List<string> RequiredFields { get; set; } = new List<string>();
|
||||
public string? RedirectTo { get; set; }
|
||||
public bool Disabled { get; set; }
|
||||
|
||||
public static RoutingItemViewModel FromRoutingItem(RoutingItem routingItem)
|
||||
{
|
||||
return new RoutingItemViewModel
|
||||
{
|
||||
AgentId = routingItem.AgentId,
|
||||
Name = routingItem.Name,
|
||||
Description = routingItem.Description,
|
||||
RequiredFields = routingItem.RequiredFields,
|
||||
RedirectTo = routingItem.RedirectTo,
|
||||
Disabled = routingItem.Disabled
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
using BotSharp.Abstraction.Routing.Models;
|
||||
|
||||
namespace BotSharp.OpenAPI.ViewModels.Routing;
|
||||
|
||||
public class RoutingProfileCreationModel
|
||||
{
|
||||
public string Name { get; set; }
|
||||
public string[] AgentIds { get; set; }
|
||||
|
||||
public RoutingProfile ToRoutingProfile()
|
||||
{
|
||||
return new RoutingProfile
|
||||
{
|
||||
Name = Name,
|
||||
AgentIds = AgentIds,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
using BotSharp.Abstraction.Routing.Models;
|
||||
|
||||
namespace BotSharp.OpenAPI.ViewModels.Routing;
|
||||
|
||||
public class RoutingProfileViewModel
|
||||
{
|
||||
public string Name { get; set; }
|
||||
public string[] AgentIds { get; set; }
|
||||
|
||||
public static RoutingProfileViewModel FromRoutingProfile(RoutingProfile profile)
|
||||
{
|
||||
return new RoutingProfileViewModel
|
||||
{
|
||||
Name = profile.Name,
|
||||
AgentIds = profile.AgentIds,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
@ -6,6 +6,6 @@ public class RoutingItemCollection : MongoBase
|
|||
public string Name { get; set; }
|
||||
public string Description { get; set; }
|
||||
public List<string> RequiredFields { get; set; }
|
||||
public Guid RedirectTo { get; set; }
|
||||
public Guid? RedirectTo { get; set; }
|
||||
public bool Disabled { get; set; }
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
using BotSharp.Abstraction.Routing.Models;
|
||||
using BotSharp.Abstraction.Users.Models;
|
||||
using BotSharp.Plugin.MongoStorage.Collections;
|
||||
|
||||
namespace BotSharp.Plugin.MongoStorage.Repository;
|
||||
|
|
@ -381,4 +383,58 @@ public class MongoRepository : IBotSharpRepository
|
|||
|
||||
_dc.Agents.UpdateOne(filter, update, _options);
|
||||
}
|
||||
|
||||
public void DeleteRoutingItems()
|
||||
{
|
||||
_dc.RoutingItems.DeleteMany(Builders<RoutingItemCollection>.Filter.Empty);
|
||||
}
|
||||
|
||||
public void DeleteRoutingProfiles()
|
||||
{
|
||||
_dc.RoutingProfiles.DeleteMany(Builders<RoutingProfileCollection>.Filter.Empty);
|
||||
}
|
||||
|
||||
public List<RoutingItemRecord> CreateRoutingItems(List<RoutingItemRecord> routingItems)
|
||||
{
|
||||
var collections = routingItems?.Select(x => new RoutingItemCollection
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
AgentId = Guid.Parse(x.AgentId),
|
||||
Name = x.Name,
|
||||
Description = x.Description,
|
||||
RequiredFields = x.RequiredFields,
|
||||
RedirectTo = !string.IsNullOrEmpty(x.RedirectTo) ? Guid.Parse(x.RedirectTo) : null,
|
||||
Disabled = x.Disabled
|
||||
})?.ToList() ?? new List<RoutingItemCollection>();
|
||||
|
||||
_dc.RoutingItems.InsertMany(collections);
|
||||
return collections.Select(x => new RoutingItemRecord
|
||||
{
|
||||
Id = x.Id.ToString(),
|
||||
AgentId = x.AgentId.ToString(),
|
||||
Name = x.Name,
|
||||
Description = x.Description,
|
||||
RequiredFields = x.RequiredFields,
|
||||
RedirectTo = x.RedirectTo?.ToString(),
|
||||
Disabled = x.Disabled
|
||||
}).ToList();
|
||||
}
|
||||
|
||||
public List<RoutingProfileRecord> CreateRoutingProfiles(List<RoutingProfileRecord> profiles)
|
||||
{
|
||||
var collections = profiles?.Select(x => new RoutingProfileCollection
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
Name = x.Name,
|
||||
AgentIds = x.AgentIds.Select(x => Guid.Parse(x)).ToList()
|
||||
})?.ToList() ?? new List<RoutingProfileCollection>();
|
||||
|
||||
_dc.RoutingProfiles.InsertMany(collections);
|
||||
return collections.Select(x => new RoutingProfileRecord
|
||||
{
|
||||
Id = x.Id.ToString(),
|
||||
Name = x.Name,
|
||||
AgentIds = x.AgentIds.Select(x => x.ToString()).ToList()
|
||||
}).ToList();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue