From 9bbbede619cc1ea63c69e4f223243c8afa33fbab Mon Sep 17 00:00:00 2001
From: hchen2020 <101423@smsassist.com>
Date: Fri, 18 Aug 2023 19:15:02 -0500
Subject: [PATCH 1/2] OpenAPI separation
---
.gitignore | 3 +-
BotSharp.sln | 11 +++
.../BotSharp.OpenAPI/BotSharp.OpenAPI.csproj | 19 +++++
.../Controllers/AgentController.cs | 38 +++++++++
.../Controllers/ConversationController.cs | 62 +++++++++++++++
.../Controllers/KnowledgeController.cs | 77 +++++++++++++++++++
.../Controllers/UserController.cs | 44 +++++++++++
src/Infrastructure/BotSharp.OpenAPI/Using.cs | 18 +++++
.../ViewModels/Agents/AgentCreationModel.cs | 18 +++++
.../ViewModels/Agents/AgentUpdateModel.cs | 46 +++++++++++
.../ViewModels/Agents/AgentViewModel.cs | 22 ++++++
.../ConversationCreationModel.cs | 6 ++
.../Conversations/ConversationViewModel.cs | 24 ++++++
.../Conversations/MessageResponseModel.cs | 6 ++
.../Conversations/NewMessageModel.cs | 6 ++
.../ViewModels/Users/UserCreationModel.cs | 22 ++++++
.../ViewModels/Users/UserViewModel.cs | 22 ++++++
src/WebStarter/WebStarter.csproj | 1 +
18 files changed, 444 insertions(+), 1 deletion(-)
create mode 100644 src/Infrastructure/BotSharp.OpenAPI/BotSharp.OpenAPI.csproj
create mode 100644 src/Infrastructure/BotSharp.OpenAPI/Controllers/AgentController.cs
create mode 100644 src/Infrastructure/BotSharp.OpenAPI/Controllers/ConversationController.cs
create mode 100644 src/Infrastructure/BotSharp.OpenAPI/Controllers/KnowledgeController.cs
create mode 100644 src/Infrastructure/BotSharp.OpenAPI/Controllers/UserController.cs
create mode 100644 src/Infrastructure/BotSharp.OpenAPI/Using.cs
create mode 100644 src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/AgentCreationModel.cs
create mode 100644 src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/AgentUpdateModel.cs
create mode 100644 src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/AgentViewModel.cs
create mode 100644 src/Infrastructure/BotSharp.OpenAPI/ViewModels/Conversations/ConversationCreationModel.cs
create mode 100644 src/Infrastructure/BotSharp.OpenAPI/ViewModels/Conversations/ConversationViewModel.cs
create mode 100644 src/Infrastructure/BotSharp.OpenAPI/ViewModels/Conversations/MessageResponseModel.cs
create mode 100644 src/Infrastructure/BotSharp.OpenAPI/ViewModels/Conversations/NewMessageModel.cs
create mode 100644 src/Infrastructure/BotSharp.OpenAPI/ViewModels/Users/UserCreationModel.cs
create mode 100644 src/Infrastructure/BotSharp.OpenAPI/ViewModels/Users/UserViewModel.cs
diff --git a/.gitignore b/.gitignore
index 1f5c1ad7..b529228b 100644
--- a/.gitignore
+++ b/.gitignore
@@ -283,6 +283,7 @@ __pycache__/
*.btm.cs
*.odx.cs
*.xsd.cs
-conversations
+
/docs/_build
*.bin
+/src/WebStarter/data/conversations
diff --git a/BotSharp.sln b/BotSharp.sln
index e8de4184..258fd099 100644
--- a/BotSharp.sln
+++ b/BotSharp.sln
@@ -43,6 +43,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BotSharp.Plugin.PaddleSharp
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BotSharp.Plugin.MetaMessenger", "src\Plugins\BotSharp.Plugin.MetaMessenger\BotSharp.Plugin.MetaMessenger.csproj", "{8300F66D-9EB8-438A-BF0F-70DFBE07D9DE}"
EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BotSharp.OpenAPI", "src\Infrastructure\BotSharp.OpenAPI\BotSharp.OpenAPI.csproj", "{7E63F5F8-4EA0-498B-ABFE-2BBE4D7DDBA7}"
+EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@@ -147,6 +149,14 @@ Global
{8300F66D-9EB8-438A-BF0F-70DFBE07D9DE}.Release|Any CPU.Build.0 = Release|Any CPU
{8300F66D-9EB8-438A-BF0F-70DFBE07D9DE}.Release|x64.ActiveCfg = Release|Any CPU
{8300F66D-9EB8-438A-BF0F-70DFBE07D9DE}.Release|x64.Build.0 = Release|Any CPU
+ {7E63F5F8-4EA0-498B-ABFE-2BBE4D7DDBA7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {7E63F5F8-4EA0-498B-ABFE-2BBE4D7DDBA7}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {7E63F5F8-4EA0-498B-ABFE-2BBE4D7DDBA7}.Debug|x64.ActiveCfg = Debug|Any CPU
+ {7E63F5F8-4EA0-498B-ABFE-2BBE4D7DDBA7}.Debug|x64.Build.0 = Debug|Any CPU
+ {7E63F5F8-4EA0-498B-ABFE-2BBE4D7DDBA7}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {7E63F5F8-4EA0-498B-ABFE-2BBE4D7DDBA7}.Release|Any CPU.Build.0 = Release|Any CPU
+ {7E63F5F8-4EA0-498B-ABFE-2BBE4D7DDBA7}.Release|x64.ActiveCfg = Release|Any CPU
+ {7E63F5F8-4EA0-498B-ABFE-2BBE4D7DDBA7}.Release|x64.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
@@ -168,6 +178,7 @@ Global
{B797EC3E-B5B9-4047-A6C8-6D330B7C6763} = {2635EC9B-2E5F-4313-AC21-0B847F31F36C}
{0308FBFD-57EB-4709-9AE4-A80D516AD84D} = {B797EC3E-B5B9-4047-A6C8-6D330B7C6763}
{8300F66D-9EB8-438A-BF0F-70DFBE07D9DE} = {64264688-0F5C-4AB0-8F2B-B59B717CCE00}
+ {7E63F5F8-4EA0-498B-ABFE-2BBE4D7DDBA7} = {E29DC6C4-5E57-48C5-BCB0-6B8F84782749}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {A9969D89-C98B-40A5-A12B-FC87E55B3A19}
diff --git a/src/Infrastructure/BotSharp.OpenAPI/BotSharp.OpenAPI.csproj b/src/Infrastructure/BotSharp.OpenAPI/BotSharp.OpenAPI.csproj
new file mode 100644
index 00000000..ef6a4193
--- /dev/null
+++ b/src/Infrastructure/BotSharp.OpenAPI/BotSharp.OpenAPI.csproj
@@ -0,0 +1,19 @@
+
+
+
+ net6.0
+ enable
+ enable
+ 0.8.0
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/src/Infrastructure/BotSharp.OpenAPI/Controllers/AgentController.cs b/src/Infrastructure/BotSharp.OpenAPI/Controllers/AgentController.cs
new file mode 100644
index 00000000..fb19ac5a
--- /dev/null
+++ b/src/Infrastructure/BotSharp.OpenAPI/Controllers/AgentController.cs
@@ -0,0 +1,38 @@
+using BotSharp.Abstraction.ApiAdapters;
+using BotSharp.OpenAPI.ViewModels.Agents;
+
+namespace BotSharp.OpenAPI.Controllers;
+
+[Authorize]
+[ApiController]
+public class AgentController : ControllerBase, IApiAdapter
+{
+ private readonly IAgentService _agentService;
+ public AgentController(IAgentService agentService)
+ {
+ _agentService = agentService;
+ }
+
+ [HttpPost("/agent")]
+ public async Task CreateAgent(AgentCreationModel agent)
+ {
+ var createdAgent = await _agentService.CreateAgent(agent.ToAgent());
+ return AgentViewModel.FromAgent(createdAgent);
+ }
+
+ [HttpPut("/agent/{agentId}")]
+ public async Task UpdateAgent([FromRoute] string agentId,
+ [FromBody] AgentUpdateModel agent)
+ {
+ var model = agent.ToAgent();
+ model.Id = agentId;
+ await _agentService.UpdateAgent(model);
+ }
+
+ [HttpGet("/agents")]
+ public async Task> GetAgents()
+ {
+ var agents = await _agentService.GetAgents();
+ return agents.Select(x => AgentViewModel.FromAgent(x)).ToList();
+ }
+}
\ No newline at end of file
diff --git a/src/Infrastructure/BotSharp.OpenAPI/Controllers/ConversationController.cs b/src/Infrastructure/BotSharp.OpenAPI/Controllers/ConversationController.cs
new file mode 100644
index 00000000..dab95e50
--- /dev/null
+++ b/src/Infrastructure/BotSharp.OpenAPI/Controllers/ConversationController.cs
@@ -0,0 +1,62 @@
+using BotSharp.Abstraction.ApiAdapters;
+using BotSharp.Abstraction.Conversations.Models;
+using BotSharp.OpenAPI.ViewModels.Conversations;
+using Microsoft.AspNetCore.Authorization;
+using Microsoft.AspNetCore.Mvc;
+
+namespace BotSharp.OpenAPI.Controllers;
+
+[Authorize]
+[ApiController]
+public class ConversationController : ControllerBase, IApiAdapter
+{
+ private readonly IServiceProvider _services;
+ private readonly IUserIdentity _user;
+
+ public ConversationController(IServiceProvider services,
+ IUserIdentity user)
+ {
+ _services = services;
+ _user = user;
+ }
+
+ [HttpPost("/conversation/{agentId}")]
+ public async Task NewConversation([FromRoute] string agentId)
+ {
+ var service = _services.GetRequiredService();
+ var sess = new Conversation
+ {
+ UserId = _user.Id,
+ AgentId = agentId
+ };
+ sess = await service.NewConversation(sess);
+ return ConversationViewModel.FromSession(sess);
+ }
+
+ [HttpDelete("/conversation/{agentId}/{conversationId}")]
+ public async Task DeleteConversation([FromRoute] string agentId, [FromRoute] string conversationId)
+ {
+ var service = _services.GetRequiredService();
+ }
+
+ [HttpPost("/conversation/{agentId}/{conversationId}")]
+ public async Task SendMessage([FromRoute] string agentId,
+ [FromRoute] string conversationId,
+ [FromBody] NewMessageModel input)
+ {
+ var conv = _services.GetRequiredService();
+
+ var response = new MessageResponseModel();
+ var stackMsg = new List();
+
+ await conv.SendMessage(agentId, conversationId,
+ new RoleDialogModel("user", input.Text),
+ async msg =>
+ stackMsg.Add(msg),
+ async fn
+ => await Task.CompletedTask);
+
+ response.Text = string.Join("\r\n", stackMsg.Select(x => x.Content));
+ return response;
+ }
+}
diff --git a/src/Infrastructure/BotSharp.OpenAPI/Controllers/KnowledgeController.cs b/src/Infrastructure/BotSharp.OpenAPI/Controllers/KnowledgeController.cs
new file mode 100644
index 00000000..4d63885e
--- /dev/null
+++ b/src/Infrastructure/BotSharp.OpenAPI/Controllers/KnowledgeController.cs
@@ -0,0 +1,77 @@
+using BotSharp.Abstraction.ApiAdapters;
+using BotSharp.Abstraction.Knowledges.Models;
+using Microsoft.AspNetCore.Http;
+using UglyToad.PdfPig.Content;
+using UglyToad.PdfPig;
+
+namespace BotSharp.OpenAPI.Controllers;
+
+[Authorize]
+[ApiController]
+public class KnowledgeController : ControllerBase, IApiAdapter
+{
+ private readonly IKnowledgeService _knowledgeService;
+ public KnowledgeController(IKnowledgeService knowledgeService)
+ {
+ _knowledgeService = knowledgeService;
+ }
+
+ [HttpGet("/knowledge/{agentId}")]
+ public async Task> RetrieveKnowledge([FromRoute] string agentId, [FromQuery(Name = "q")] string question)
+ {
+ return await _knowledgeService.GetAnswer(new KnowledgeRetrievalModel
+ {
+ AgentId = agentId,
+ Question = question
+ });
+ }
+
+ [HttpPost("/knowledge/{agentId}")]
+ public async Task FeedKnowledge([FromRoute] string agentId, List files, [FromQuery] int? startPageNum, [FromQuery] int? endPageNum)
+ {
+ long size = files.Sum(f => f.Length);
+
+ foreach (var formFile in files)
+ {
+ if (formFile.Length <= 0)
+ {
+ continue;
+ }
+
+ var filePath = Path.GetTempFileName();
+
+ using (var stream = System.IO.File.Create(filePath))
+ {
+ await formFile.CopyToAsync(stream);
+ }
+
+ var document = PdfDocument.Open(filePath);
+ var content = "";
+ foreach (Page page in document.GetPages())
+ {
+ if (startPageNum.HasValue && page.Number < startPageNum.Value)
+ {
+ continue;
+ }
+
+ if (endPageNum.HasValue && page.Number > endPageNum.Value)
+ {
+ continue;
+ }
+
+ content += page.Text;
+ }
+
+ // Process uploaded files
+ // Don't rely on or trust the FileName property without validation.
+
+ await _knowledgeService.Feed(new KnowledgeFeedModel
+ {
+ AgentId = agentId,
+ Content = content
+ });
+ }
+
+ return Ok(new { count = files.Count, size });
+ }
+}
diff --git a/src/Infrastructure/BotSharp.OpenAPI/Controllers/UserController.cs b/src/Infrastructure/BotSharp.OpenAPI/Controllers/UserController.cs
new file mode 100644
index 00000000..1952c9c1
--- /dev/null
+++ b/src/Infrastructure/BotSharp.OpenAPI/Controllers/UserController.cs
@@ -0,0 +1,44 @@
+using BotSharp.Abstraction.ApiAdapters;
+using BotSharp.Abstraction.Users.Models;
+using BotSharp.OpenAPI.ViewModels.Users;
+
+namespace BotSharp.OpenAPI.Controllers;
+
+[Authorize]
+[ApiController]
+public class UserController : ControllerBase, IApiAdapter
+{
+ private readonly IUserService _userService;
+ public UserController(IUserService userService)
+ {
+ _userService = userService;
+ }
+
+ [AllowAnonymous]
+ [HttpPost("/token")]
+ public async Task> GetToken()
+ {
+ var authcode = Request.Headers["Authorization"].ToString();
+ var token = await _userService.GetToken(authcode.Split(' ')[1]);
+ if (token == null)
+ {
+ return Unauthorized();
+ }
+ return Ok(token);
+ }
+
+ [AllowAnonymous]
+ [HttpPost("/user")]
+ public async Task CreateUser(UserCreationModel user)
+ {
+ var createdUser = await _userService.CreateUser(user.ToUser());
+ return UserViewModel.FromUser(createdUser);
+ }
+
+ [HttpGet("/user/my")]
+ public async Task GetMyUserProfile()
+ {
+ var user = await _userService.GetMyProfile();
+ return UserViewModel.FromUser(user);
+ }
+}
diff --git a/src/Infrastructure/BotSharp.OpenAPI/Using.cs b/src/Infrastructure/BotSharp.OpenAPI/Using.cs
new file mode 100644
index 00000000..75f7274b
--- /dev/null
+++ b/src/Infrastructure/BotSharp.OpenAPI/Using.cs
@@ -0,0 +1,18 @@
+global using System;
+global using System.Collections.Generic;
+global using System.Text;
+global using System.Threading.Tasks;
+global using System.Linq;
+global using System.Text.Json;
+global using Microsoft.AspNetCore.Authorization;
+global using Microsoft.AspNetCore.Mvc;
+global using Microsoft.Extensions.DependencyInjection;
+global using Microsoft.Extensions.Logging;
+global using BotSharp.Abstraction.Plugins;
+global using BotSharp.Abstraction.Agents;
+global using BotSharp.Abstraction.Conversations;
+global using BotSharp.Abstraction.Knowledges;
+global using BotSharp.Abstraction.Users;
+global using BotSharp.Abstraction.Utilities;
+global using BotSharp.Abstraction.Agents.Settings;
+global using BotSharp.Abstraction.Conversations.Settings;
\ No newline at end of file
diff --git a/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/AgentCreationModel.cs b/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/AgentCreationModel.cs
new file mode 100644
index 00000000..f8fead86
--- /dev/null
+++ b/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/AgentCreationModel.cs
@@ -0,0 +1,18 @@
+using BotSharp.Abstraction.Agents.Models;
+
+namespace BotSharp.OpenAPI.ViewModels.Agents;
+
+public class AgentCreationModel
+{
+ public string Name { get; set; }
+ public string Description { get; set; }
+
+ public Agent ToAgent()
+ {
+ return new Agent
+ {
+ Name = Name,
+ Description = Description
+ };
+ }
+}
diff --git a/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/AgentUpdateModel.cs b/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/AgentUpdateModel.cs
new file mode 100644
index 00000000..1a87a2d7
--- /dev/null
+++ b/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/AgentUpdateModel.cs
@@ -0,0 +1,46 @@
+using BotSharp.Abstraction.Agents.Models;
+
+namespace BotSharp.OpenAPI.ViewModels.Agents;
+
+public class AgentUpdateModel
+{
+ public string Name { get; set; } = string.Empty;
+ public string? Description { get; set; }
+
+ ///
+ /// Instruction
+ ///
+ public string? Instruction { get; set; }
+
+ ///
+ /// Samples
+ ///
+ public string? Samples { get; set; }
+
+ ///
+ /// Functions
+ ///
+ public string? Functions { get; set; }
+
+ public Agent ToAgent()
+ {
+ var agent = new Agent
+ {
+ Name = Name
+ };
+
+ if (Description != null)
+ agent.Description = Description;
+
+ if (Instruction != null)
+ agent.Instruction = Instruction;
+
+ if (Samples != null)
+ agent.Samples = Samples;
+
+ if (Functions != null)
+ agent.Functions = Functions;
+
+ return agent;
+ }
+}
diff --git a/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/AgentViewModel.cs b/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/AgentViewModel.cs
new file mode 100644
index 00000000..5a2b8969
--- /dev/null
+++ b/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/AgentViewModel.cs
@@ -0,0 +1,22 @@
+using BotSharp.Abstraction.Agents.Models;
+
+namespace BotSharp.OpenAPI.ViewModels.Agents;
+
+public class AgentViewModel
+{
+ public string Id { get; set; }
+ public string Name { get; set; }
+ public string Description { get; set; }
+ public DateTime UpdatedDateTime { get; set; }
+
+ public static AgentViewModel FromAgent(Agent agent)
+ {
+ return new AgentViewModel
+ {
+ Id = agent.Id,
+ Name = agent.Name,
+ Description = agent.Description,
+ UpdatedDateTime = agent.UpdatedDateTime
+ };
+ }
+}
diff --git a/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Conversations/ConversationCreationModel.cs b/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Conversations/ConversationCreationModel.cs
new file mode 100644
index 00000000..645de634
--- /dev/null
+++ b/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Conversations/ConversationCreationModel.cs
@@ -0,0 +1,6 @@
+namespace BotSharp.OpenAPI.ViewModels.Conversations;
+
+public class ConversationCreationModel
+{
+
+}
diff --git a/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Conversations/ConversationViewModel.cs b/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Conversations/ConversationViewModel.cs
new file mode 100644
index 00000000..f47c5cd6
--- /dev/null
+++ b/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Conversations/ConversationViewModel.cs
@@ -0,0 +1,24 @@
+using BotSharp.Abstraction.Conversations.Models;
+
+namespace BotSharp.OpenAPI.ViewModels.Conversations;
+
+public class ConversationViewModel
+{
+ public string Id { get; set; }
+ public string AgentId { get; set; }
+ public string Title { get; set; } = string.Empty;
+ public DateTime UpdatedTime { get; set; } = DateTime.UtcNow;
+ public DateTime CreatedTime { get; set; } = DateTime.UtcNow;
+
+ public static ConversationViewModel FromSession(Conversation sess)
+ {
+ return new ConversationViewModel
+ {
+ Id = sess.Id,
+ AgentId = sess.AgentId,
+ Title = sess.Title,
+ CreatedTime = sess.CreatedTime,
+ UpdatedTime = sess.UpdatedTime
+ };
+ }
+}
diff --git a/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Conversations/MessageResponseModel.cs b/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Conversations/MessageResponseModel.cs
new file mode 100644
index 00000000..b6a6e1e4
--- /dev/null
+++ b/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Conversations/MessageResponseModel.cs
@@ -0,0 +1,6 @@
+namespace BotSharp.OpenAPI.ViewModels.Conversations;
+
+public class MessageResponseModel
+{
+ public string Text { get; set; }
+}
diff --git a/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Conversations/NewMessageModel.cs b/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Conversations/NewMessageModel.cs
new file mode 100644
index 00000000..45716e21
--- /dev/null
+++ b/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Conversations/NewMessageModel.cs
@@ -0,0 +1,6 @@
+namespace BotSharp.OpenAPI.ViewModels.Conversations;
+
+public class NewMessageModel
+{
+ public string Text { get; set; }
+}
diff --git a/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Users/UserCreationModel.cs b/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Users/UserCreationModel.cs
new file mode 100644
index 00000000..76fdb443
--- /dev/null
+++ b/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Users/UserCreationModel.cs
@@ -0,0 +1,22 @@
+using BotSharp.Abstraction.Users.Models;
+
+namespace BotSharp.OpenAPI.ViewModels.Users;
+
+public class UserCreationModel
+{
+ public string FirstName { get; set; }
+ public string LastName { get; set; }
+ public string Email { get; set; }
+ public string Password { get; set; }
+
+ public User ToUser()
+ {
+ return new User
+ {
+ FirstName = FirstName,
+ LastName = LastName,
+ Email = Email,
+ Password = Password
+ };
+ }
+}
diff --git a/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Users/UserViewModel.cs b/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Users/UserViewModel.cs
new file mode 100644
index 00000000..4e452334
--- /dev/null
+++ b/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Users/UserViewModel.cs
@@ -0,0 +1,22 @@
+using BotSharp.Abstraction.Users.Models;
+
+namespace BotSharp.OpenAPI.ViewModels.Users;
+
+public class UserViewModel
+{
+ public string Id { get; set; }
+ public string FirstName { get; set; }
+ public string LastName { get; set; }
+ public string Email { get; set; }
+
+ public static UserViewModel FromUser(User user)
+ {
+ return new UserViewModel
+ {
+ Id = user.Id,
+ FirstName = user.FirstName,
+ LastName = user.LastName,
+ Email = user.Email
+ };
+ }
+}
diff --git a/src/WebStarter/WebStarter.csproj b/src/WebStarter/WebStarter.csproj
index 41765502..d03a2f39 100644
--- a/src/WebStarter/WebStarter.csproj
+++ b/src/WebStarter/WebStarter.csproj
@@ -35,6 +35,7 @@
+
From d9c5f5a0ad4f6ffc3eefbdff330caa2a12d8d43f Mon Sep 17 00:00:00 2001
From: hchen2020 <101423@smsassist.com>
Date: Fri, 18 Aug 2023 19:15:50 -0500
Subject: [PATCH 2/2] Enhance Agent Rounter mechanism
---
.../Conversations/IConversationStorage.cs | 2 +-
.../BotSharp.Core/Agents/AgentController.cs | 40 ----------
.../Agents/Services/AgentHookBase.cs | 1 -
.../Agents/Services/AgentService.GetAgents.cs | 9 ---
.../Agents/ViewModels/AgentCreationModel.cs | 18 -----
.../Agents/ViewModels/AgentUpdateModel.cs | 46 -----------
.../Agents/ViewModels/AgentViewModel.cs | 22 -----
.../BotSharp.Core/BotSharp.Core.csproj | 2 -
.../BotSharpServiceCollectionExtensions.cs | 4 +
.../Conversations/ConversationController.cs | 62 --------------
.../Services/ConversationService.cs | 19 ++---
.../Services/ConversationStorage.cs | 24 +++---
.../ViewModels/ConversationCreationModel.cs | 6 --
.../ViewModels/ConversationViewModel.cs | 24 ------
.../ViewModels/MessageResponseModel.cs | 6 --
.../ViewModels/NewMessageModel.cs | 6 --
.../BotSharp.Core/Functions/GoToRouterFn.cs | 27 +++++++
.../Plugins/Knowledges/KnowledgeController.cs | 80 -------------------
.../BotSharp.Core/Users/UserController.cs | 46 -----------
.../Users/ViewModels/UserCreationModel.cs | 22 -----
.../Users/ViewModels/UserViewModel.cs | 22 -----
21 files changed, 51 insertions(+), 437 deletions(-)
delete mode 100644 src/Infrastructure/BotSharp.Core/Agents/AgentController.cs
delete mode 100644 src/Infrastructure/BotSharp.Core/Agents/ViewModels/AgentCreationModel.cs
delete mode 100644 src/Infrastructure/BotSharp.Core/Agents/ViewModels/AgentUpdateModel.cs
delete mode 100644 src/Infrastructure/BotSharp.Core/Agents/ViewModels/AgentViewModel.cs
delete mode 100644 src/Infrastructure/BotSharp.Core/Conversations/ConversationController.cs
delete mode 100644 src/Infrastructure/BotSharp.Core/Conversations/ViewModels/ConversationCreationModel.cs
delete mode 100644 src/Infrastructure/BotSharp.Core/Conversations/ViewModels/ConversationViewModel.cs
delete mode 100644 src/Infrastructure/BotSharp.Core/Conversations/ViewModels/MessageResponseModel.cs
delete mode 100644 src/Infrastructure/BotSharp.Core/Conversations/ViewModels/NewMessageModel.cs
create mode 100644 src/Infrastructure/BotSharp.Core/Functions/GoToRouterFn.cs
delete mode 100644 src/Infrastructure/BotSharp.Core/Plugins/Knowledges/KnowledgeController.cs
delete mode 100644 src/Infrastructure/BotSharp.Core/Users/UserController.cs
delete mode 100644 src/Infrastructure/BotSharp.Core/Users/ViewModels/UserCreationModel.cs
delete mode 100644 src/Infrastructure/BotSharp.Core/Users/ViewModels/UserViewModel.cs
diff --git a/src/Infrastructure/BotSharp.Abstraction/Conversations/IConversationStorage.cs b/src/Infrastructure/BotSharp.Abstraction/Conversations/IConversationStorage.cs
index 8c01cbbc..ed5044b0 100644
--- a/src/Infrastructure/BotSharp.Abstraction/Conversations/IConversationStorage.cs
+++ b/src/Infrastructure/BotSharp.Abstraction/Conversations/IConversationStorage.cs
@@ -5,6 +5,6 @@ namespace BotSharp.Abstraction.Conversations;
public interface IConversationStorage
{
void InitStorage(string conversationId);
- void Append(string conversationId, Agent agent, RoleDialogModel dialog);
+ void Append(string conversationId, string agentId, RoleDialogModel dialog);
List GetDialogs(string conversationId);
}
diff --git a/src/Infrastructure/BotSharp.Core/Agents/AgentController.cs b/src/Infrastructure/BotSharp.Core/Agents/AgentController.cs
deleted file mode 100644
index 3e17e193..00000000
--- a/src/Infrastructure/BotSharp.Core/Agents/AgentController.cs
+++ /dev/null
@@ -1,40 +0,0 @@
-using BotSharp.Abstraction.ApiAdapters;
-using BotSharp.Core.Agents.ViewModels;
-using Microsoft.AspNetCore.Authorization;
-using Microsoft.AspNetCore.Mvc;
-
-namespace BotSharp.Core.Agents;
-
-[Authorize]
-[ApiController]
-public class AgentController : ControllerBase, IApiAdapter
-{
- private readonly IAgentService _agentService;
- public AgentController(IAgentService agentService)
- {
- _agentService = agentService;
- }
-
- [HttpPost("/agent")]
- public async Task CreateAgent(AgentCreationModel agent)
- {
- var createdAgent = await _agentService.CreateAgent(agent.ToAgent());
- return AgentViewModel.FromAgent(createdAgent);
- }
-
- [HttpPut("/agent/{agentId}")]
- public async Task UpdateAgent([FromRoute] string agentId,
- [FromBody] AgentUpdateModel agent)
- {
- var model = agent.ToAgent();
- model.Id = agentId;
- await _agentService.UpdateAgent(model);
- }
-
- [HttpGet("/agents")]
- public async Task> GetAgents()
- {
- var agents = await _agentService.GetAgents();
- return agents.Select(x => AgentViewModel.FromAgent(x)).ToList();
- }
-}
\ No newline at end of file
diff --git a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentHookBase.cs b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentHookBase.cs
index 88e3ec1f..0a9e2668 100644
--- a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentHookBase.cs
+++ b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentHookBase.cs
@@ -1,7 +1,6 @@
using BotSharp.Abstraction.Agents.Models;
using BotSharp.Abstraction.Conversations.Models;
using Fluid;
-using Microsoft.AspNetCore.Mvc;
namespace BotSharp.Core.Agents.Services;
diff --git a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.GetAgents.cs b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.GetAgents.cs
index 7bf27a44..420784c3 100644
--- a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.GetAgents.cs
+++ b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.GetAgents.cs
@@ -40,20 +40,11 @@ public partial class AgentService
{
profile.Samples = File.ReadAllText(samplesFile);
}
- else
- {
- _logger.LogWarning($"Can't find samples file from {samplesFile}");
- }
-
var functionsFile = Path.Combine(dir, "functions.json");
if (File.Exists(functionsFile))
{
profile.Functions = File.ReadAllText(functionsFile);
}
- else
- {
- _logger.LogInformation($"Can't find functions file from {functionsFile}");
- }
return profile;
}
diff --git a/src/Infrastructure/BotSharp.Core/Agents/ViewModels/AgentCreationModel.cs b/src/Infrastructure/BotSharp.Core/Agents/ViewModels/AgentCreationModel.cs
deleted file mode 100644
index 00bd7d92..00000000
--- a/src/Infrastructure/BotSharp.Core/Agents/ViewModels/AgentCreationModel.cs
+++ /dev/null
@@ -1,18 +0,0 @@
-using BotSharp.Abstraction.Agents.Models;
-
-namespace BotSharp.Core.Agents.ViewModels;
-
-public class AgentCreationModel
-{
- public string Name { get; set; }
- public string Description { get; set; }
-
- public Agent ToAgent()
- {
- return new Agent
- {
- Name = Name,
- Description = Description
- };
- }
-}
diff --git a/src/Infrastructure/BotSharp.Core/Agents/ViewModels/AgentUpdateModel.cs b/src/Infrastructure/BotSharp.Core/Agents/ViewModels/AgentUpdateModel.cs
deleted file mode 100644
index 5b519953..00000000
--- a/src/Infrastructure/BotSharp.Core/Agents/ViewModels/AgentUpdateModel.cs
+++ /dev/null
@@ -1,46 +0,0 @@
-using BotSharp.Abstraction.Agents.Models;
-
-namespace BotSharp.Core.Agents.ViewModels;
-
-public class AgentUpdateModel
-{
- public string Name { get; set; } = string.Empty;
- public string? Description { get; set; }
-
- ///
- /// Instruction
- ///
- public string? Instruction { get; set; }
-
- ///
- /// Samples
- ///
- public string? Samples { get; set; }
-
- ///
- /// Functions
- ///
- public string? Functions { get; set; }
-
- public Agent ToAgent()
- {
- var agent = new Agent
- {
- Name = Name
- };
-
- if (Description != null)
- agent.Description = Description;
-
- if (Instruction != null)
- agent.Instruction = Instruction;
-
- if (Samples != null)
- agent.Samples = Samples;
-
- if (Functions != null)
- agent.Functions = Functions;
-
- return agent;
- }
-}
diff --git a/src/Infrastructure/BotSharp.Core/Agents/ViewModels/AgentViewModel.cs b/src/Infrastructure/BotSharp.Core/Agents/ViewModels/AgentViewModel.cs
deleted file mode 100644
index 1115d582..00000000
--- a/src/Infrastructure/BotSharp.Core/Agents/ViewModels/AgentViewModel.cs
+++ /dev/null
@@ -1,22 +0,0 @@
-using BotSharp.Abstraction.Agents.Models;
-
-namespace BotSharp.Core.Agents.ViewModels;
-
-public class AgentViewModel
-{
- public string Id { get; set; }
- public string Name { get; set; }
- public string Description { get; set; }
- public DateTime UpdatedDateTime { get; set; }
-
- public static AgentViewModel FromAgent(Agent agent)
- {
- return new AgentViewModel
- {
- Id = agent.Id,
- Name = agent.Name,
- Description = agent.Description,
- UpdatedDateTime = agent.UpdatedDateTime
- };
- }
-}
diff --git a/src/Infrastructure/BotSharp.Core/BotSharp.Core.csproj b/src/Infrastructure/BotSharp.Core/BotSharp.Core.csproj
index c3a188fd..6b4b0970 100644
--- a/src/Infrastructure/BotSharp.Core/BotSharp.Core.csproj
+++ b/src/Infrastructure/BotSharp.Core/BotSharp.Core.csproj
@@ -77,9 +77,7 @@
-
-
diff --git a/src/Infrastructure/BotSharp.Core/BotSharpServiceCollectionExtensions.cs b/src/Infrastructure/BotSharp.Core/BotSharpServiceCollectionExtensions.cs
index c746438e..4607086a 100644
--- a/src/Infrastructure/BotSharp.Core/BotSharpServiceCollectionExtensions.cs
+++ b/src/Infrastructure/BotSharp.Core/BotSharpServiceCollectionExtensions.cs
@@ -1,3 +1,5 @@
+using BotSharp.Abstraction.Functions;
+using BotSharp.Core.Functions;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Configuration;
@@ -35,6 +37,8 @@ public static class BotSharpServiceCollectionExtensions
services.AddScoped();
+ services.AddScoped();
+
return services;
}
diff --git a/src/Infrastructure/BotSharp.Core/Conversations/ConversationController.cs b/src/Infrastructure/BotSharp.Core/Conversations/ConversationController.cs
deleted file mode 100644
index 8a26a569..00000000
--- a/src/Infrastructure/BotSharp.Core/Conversations/ConversationController.cs
+++ /dev/null
@@ -1,62 +0,0 @@
-using BotSharp.Abstraction.ApiAdapters;
-using BotSharp.Abstraction.Conversations.Models;
-using BotSharp.Core.Conversations.ViewModels;
-using Microsoft.AspNetCore.Authorization;
-using Microsoft.AspNetCore.Mvc;
-
-namespace BotSharp.Core.Conversations;
-
-[Authorize]
-[ApiController]
-public class ConversationController : ControllerBase, IApiAdapter
-{
- private readonly IServiceProvider _services;
- private readonly IUserIdentity _user;
-
- public ConversationController(IServiceProvider services,
- IUserIdentity user)
- {
- _services = services;
- _user = user;
- }
-
- [HttpPost("/conversation/{agentId}")]
- public async Task NewConversation([FromRoute] string agentId)
- {
- var service = _services.GetRequiredService();
- var sess = new Conversation
- {
- UserId = _user.Id,
- AgentId = agentId
- };
- sess = await service.NewConversation(sess);
- return ConversationViewModel.FromSession(sess);
- }
-
- [HttpDelete("/conversation/{agentId}/{conversationId}")]
- public async Task DeleteConversation([FromRoute] string agentId, [FromRoute] string conversationId)
- {
- var service = _services.GetRequiredService();
- }
-
- [HttpPost("/conversation/{agentId}/{conversationId}")]
- public async Task SendMessage([FromRoute] string agentId,
- [FromRoute] string conversationId,
- [FromBody] NewMessageModel input)
- {
- var conv = _services.GetRequiredService();
-
- var response = new MessageResponseModel();
- var stackMsg = new List();
-
- await conv.SendMessage(agentId, conversationId,
- new RoleDialogModel("user", input.Text),
- async msg =>
- stackMsg.Add(msg),
- async fn
- => await Task.CompletedTask);
-
- response.Text = string.Join("\r\n", stackMsg.Select(x => x.Content));
- return response;
- }
-}
diff --git a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.cs b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.cs
index 78516b22..b5e351e6 100644
--- a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.cs
+++ b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.cs
@@ -1,5 +1,3 @@
-using Amazon.SecurityToken.Model.Internal.MarshallTransformations;
-using BotSharp.Abstraction.Agents.Enums;
using BotSharp.Abstraction.Agents.Models;
using BotSharp.Abstraction.Conversations.Models;
using BotSharp.Abstraction.Functions;
@@ -98,8 +96,10 @@ public class ConversationService : IConversationService
var router = _services.GetRequiredService();
var agent = await router.LoadCurrentAgent();
+ _logger.LogInformation($"[{agent.Name}] {lastDialog.Role}: {lastDialog.Content}");
+
lastDialog.CurrentAgentId = agent.Id;
- _storage.Append(conversationId, agent, lastDialog);
+ _storage.Append(conversationId, agent.Id, lastDialog);
var wholeDialogs = GetDialogHistory(conversationId);
@@ -149,28 +149,29 @@ public class ConversationService : IConversationService
await HandleAssistantMessage(msg, onMessageReceived);
// Add to dialog history
- _storage.Append(conversationId, agent, msg);
+ _storage.Append(conversationId, agent.Id, msg);
}, async fn =>
{
var preAgentId = agent.Id;
await HandleFunctionMessage(fn, onFunctionExecuting);
+ fn.Content = fn.ExecutionResult;
+
// Agent has been transferred
if (fn.CurrentAgentId != preAgentId)
{
+ var agentSettings = _services.GetRequiredService();
var agentService = _services.GetRequiredService();
agent = await agentService.LoadAgent(fn.CurrentAgentId);
// Set state to make next conversation will go to this agent directly
- var state = _services.GetRequiredService();
- state.SetState("agentId", fn.CurrentAgentId);
+ // var state = _services.GetRequiredService();
+ // state.SetState("agentId", fn.CurrentAgentId);
}
- fn.Content = fn.ExecutionResult;
-
// Add to dialog history
- _storage.Append(conversationId, agent, fn);
+ _storage.Append(conversationId, preAgentId, fn);
// After function is executed, pass the result to LLM to get a natural response
wholeDialogs.Add(fn);
diff --git a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationStorage.cs b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationStorage.cs
index 38097106..8e0b08dd 100644
--- a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationStorage.cs
+++ b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationStorage.cs
@@ -1,5 +1,4 @@
using BotSharp.Abstraction.Agents.Enums;
-using BotSharp.Abstraction.Agents.Models;
using BotSharp.Abstraction.Conversations.Models;
using System.IO;
@@ -8,12 +7,14 @@ namespace BotSharp.Core.Conversations.Services;
public class ConversationStorage : IConversationStorage
{
private readonly MyDatabaseSettings _dbSettings;
- public ConversationStorage(MyDatabaseSettings dbSettings)
+ private readonly IServiceProvider _services;
+ public ConversationStorage(MyDatabaseSettings dbSettings, IServiceProvider services)
{
_dbSettings = dbSettings;
+ _services = services;
}
- public void Append(string conversationId, Agent agent, RoleDialogModel dialog)
+ public void Append(string conversationId, string agentId, RoleDialogModel dialog)
{
var conversationFile = GetStorageFile(conversationId);
var sb = new StringBuilder();
@@ -22,7 +23,7 @@ public class ConversationStorage : IConversationStorage
{
var args = dialog.FunctionArgs.Replace("\r", " ").Replace("\n", " ").Trim();
- sb.AppendLine($"{dialog.CreatedAt}|{dialog.Role}|{agent.Name}|{dialog.FunctionName}|{args}");
+ sb.AppendLine($"{dialog.CreatedAt}|{dialog.Role}|{agentId}|{dialog.FunctionName}|{args}");
var content = dialog.ExecutionResult.Replace("\r", " ").Replace("\n", " ").Trim();
if (string.IsNullOrEmpty(content))
@@ -31,19 +32,12 @@ public class ConversationStorage : IConversationStorage
}
sb.AppendLine($" - {content}");
}
- else if (dialog.Role == AgentRole.Assistant)
- {
- sb.AppendLine($"{dialog.CreatedAt}|{dialog.Role}|{agent.Name}||");
- var content = dialog.Content.Replace("\r", " ").Replace("\n", " ").Trim();
- if (string.IsNullOrEmpty(content))
- {
- return;
- }
- sb.AppendLine($" - {content}");
- }
else
{
- sb.AppendLine($"{dialog.CreatedAt}|{dialog.Role}|{agent.Name}||");
+ var db = _services.GetRequiredService();
+ var agent = db.Agent.First(x => x.Id == agentId);
+
+ sb.AppendLine($"{dialog.CreatedAt}|{dialog.Role}|{agentId}|{agent.Name}|");
var content = dialog.Content.Replace("\r", " ").Replace("\n", " ").Trim();
if (string.IsNullOrEmpty(content))
{
diff --git a/src/Infrastructure/BotSharp.Core/Conversations/ViewModels/ConversationCreationModel.cs b/src/Infrastructure/BotSharp.Core/Conversations/ViewModels/ConversationCreationModel.cs
deleted file mode 100644
index 300254a3..00000000
--- a/src/Infrastructure/BotSharp.Core/Conversations/ViewModels/ConversationCreationModel.cs
+++ /dev/null
@@ -1,6 +0,0 @@
-namespace BotSharp.Core.Conversations.ViewModels;
-
-public class ConversationCreationModel
-{
-
-}
diff --git a/src/Infrastructure/BotSharp.Core/Conversations/ViewModels/ConversationViewModel.cs b/src/Infrastructure/BotSharp.Core/Conversations/ViewModels/ConversationViewModel.cs
deleted file mode 100644
index 3289a1d5..00000000
--- a/src/Infrastructure/BotSharp.Core/Conversations/ViewModels/ConversationViewModel.cs
+++ /dev/null
@@ -1,24 +0,0 @@
-using BotSharp.Abstraction.Conversations.Models;
-
-namespace BotSharp.Core.Conversations.ViewModels;
-
-public class ConversationViewModel
-{
- public string Id { get; set; }
- public string AgentId { get; set; }
- public string Title { get; set; } = string.Empty;
- public DateTime UpdatedTime { get; set; } = DateTime.UtcNow;
- public DateTime CreatedTime { get; set; } = DateTime.UtcNow;
-
- public static ConversationViewModel FromSession(Conversation sess)
- {
- return new ConversationViewModel
- {
- Id = sess.Id,
- AgentId = sess.AgentId,
- Title = sess.Title,
- CreatedTime = sess.CreatedTime,
- UpdatedTime = sess.UpdatedTime
- };
- }
-}
diff --git a/src/Infrastructure/BotSharp.Core/Conversations/ViewModels/MessageResponseModel.cs b/src/Infrastructure/BotSharp.Core/Conversations/ViewModels/MessageResponseModel.cs
deleted file mode 100644
index 68f58a5f..00000000
--- a/src/Infrastructure/BotSharp.Core/Conversations/ViewModels/MessageResponseModel.cs
+++ /dev/null
@@ -1,6 +0,0 @@
-namespace BotSharp.Core.Conversations.ViewModels;
-
-public class MessageResponseModel
-{
- public string Text { get; set; }
-}
diff --git a/src/Infrastructure/BotSharp.Core/Conversations/ViewModels/NewMessageModel.cs b/src/Infrastructure/BotSharp.Core/Conversations/ViewModels/NewMessageModel.cs
deleted file mode 100644
index 714f3500..00000000
--- a/src/Infrastructure/BotSharp.Core/Conversations/ViewModels/NewMessageModel.cs
+++ /dev/null
@@ -1,6 +0,0 @@
-namespace BotSharp.Core.Conversations.ViewModels;
-
-public class NewMessageModel
-{
- public string Text { get; set; }
-}
diff --git a/src/Infrastructure/BotSharp.Core/Functions/GoToRouterFn.cs b/src/Infrastructure/BotSharp.Core/Functions/GoToRouterFn.cs
new file mode 100644
index 00000000..e7a3455a
--- /dev/null
+++ b/src/Infrastructure/BotSharp.Core/Functions/GoToRouterFn.cs
@@ -0,0 +1,27 @@
+using BotSharp.Abstraction.Conversations.Models;
+using BotSharp.Abstraction.Functions;
+using BotSharp.Abstraction.Functions.Models;
+
+namespace BotSharp.Core.Functions;
+
+public class GoToRouterFn : IFunctionCallback
+{
+ public string Name => "go_to_router";
+ private readonly IServiceProvider _services;
+
+ public GoToRouterFn(IServiceProvider services)
+ {
+ _services = services;
+ }
+
+ public async Task Execute(RoleDialogModel message)
+ {
+ var settings = _services.GetRequiredService();
+ message.CurrentAgentId = settings.RouterId;
+
+ var result = new FunctionExecutionValidationResult("true");
+ message.ExecutionResult = JsonSerializer.Serialize(result);
+
+ return true;
+ }
+}
diff --git a/src/Infrastructure/BotSharp.Core/Plugins/Knowledges/KnowledgeController.cs b/src/Infrastructure/BotSharp.Core/Plugins/Knowledges/KnowledgeController.cs
deleted file mode 100644
index d990e41d..00000000
--- a/src/Infrastructure/BotSharp.Core/Plugins/Knowledges/KnowledgeController.cs
+++ /dev/null
@@ -1,80 +0,0 @@
-using BotSharp.Abstraction.ApiAdapters;
-using BotSharp.Abstraction.Knowledges.Models;
-using Microsoft.AspNetCore.Authorization;
-using Microsoft.AspNetCore.Http;
-using Microsoft.AspNetCore.Mvc;
-using System.IO;
-using UglyToad.PdfPig.Content;
-using UglyToad.PdfPig;
-
-namespace BotSharp.Core.Plugins.Knowledges;
-
-[Authorize]
-[ApiController]
-public class KnowledgeController : ControllerBase, IApiAdapter
-{
- private readonly IKnowledgeService _knowledgeService;
- public KnowledgeController(IKnowledgeService knowledgeService)
- {
- _knowledgeService = knowledgeService;
- }
-
- [HttpGet("/knowledge/{agentId}")]
- public async Task> RetrieveKnowledge([FromRoute] string agentId, [FromQuery(Name = "q")] string question)
- {
- return await _knowledgeService.GetAnswer(new KnowledgeRetrievalModel
- {
- AgentId = agentId,
- Question = question
- });
- }
-
- [HttpPost("/knowledge/{agentId}")]
- public async Task FeedKnowledge([FromRoute] string agentId, List files, [FromQuery] int? startPageNum, [FromQuery] int? endPageNum)
- {
- long size = files.Sum(f => f.Length);
-
- foreach (var formFile in files)
- {
- if (formFile.Length <= 0)
- {
- continue;
- }
-
- var filePath = Path.GetTempFileName();
-
- using (var stream = System.IO.File.Create(filePath))
- {
- await formFile.CopyToAsync(stream);
- }
-
- var document = PdfDocument.Open(filePath);
- var content = "";
- foreach (Page page in document.GetPages())
- {
- if (startPageNum.HasValue && page.Number < startPageNum.Value)
- {
- continue;
- }
-
- if (endPageNum.HasValue && page.Number > endPageNum.Value)
- {
- continue;
- }
-
- content += page.Text;
- }
-
- // Process uploaded files
- // Don't rely on or trust the FileName property without validation.
-
- await _knowledgeService.Feed(new KnowledgeFeedModel
- {
- AgentId = agentId,
- Content = content
- });
- }
-
- return Ok(new { count = files.Count, size });
- }
-}
diff --git a/src/Infrastructure/BotSharp.Core/Users/UserController.cs b/src/Infrastructure/BotSharp.Core/Users/UserController.cs
deleted file mode 100644
index c03d82bc..00000000
--- a/src/Infrastructure/BotSharp.Core/Users/UserController.cs
+++ /dev/null
@@ -1,46 +0,0 @@
-using BotSharp.Abstraction.ApiAdapters;
-using BotSharp.Abstraction.Users.Models;
-using BotSharp.Core.Users.ViewModels;
-using Microsoft.AspNetCore.Authorization;
-using Microsoft.AspNetCore.Mvc;
-
-namespace BotSharp.Core.Users;
-
-[Authorize]
-[ApiController]
-public class UserController : ControllerBase, IApiAdapter
-{
- private readonly IUserService _userService;
- public UserController(IUserService userService)
- {
- _userService = userService;
- }
-
- [AllowAnonymous]
- [HttpPost("/token")]
- public async Task> GetToken()
- {
- var authcode = Request.Headers["Authorization"].ToString();
- var token = await _userService.GetToken(authcode.Split(' ')[1]);
- if (token == null)
- {
- return Unauthorized();
- }
- return Ok(token);
- }
-
- [AllowAnonymous]
- [HttpPost("/user")]
- public async Task CreateUser(UserCreationModel user)
- {
- var createdUser = await _userService.CreateUser(user.ToUser());
- return UserViewModel.FromUser(createdUser);
- }
-
- [HttpGet("/user/my")]
- public async Task GetMyUserProfile()
- {
- var user = await _userService.GetMyProfile();
- return UserViewModel.FromUser(user);
- }
-}
diff --git a/src/Infrastructure/BotSharp.Core/Users/ViewModels/UserCreationModel.cs b/src/Infrastructure/BotSharp.Core/Users/ViewModels/UserCreationModel.cs
deleted file mode 100644
index 23aa7e08..00000000
--- a/src/Infrastructure/BotSharp.Core/Users/ViewModels/UserCreationModel.cs
+++ /dev/null
@@ -1,22 +0,0 @@
-using BotSharp.Abstraction.Users.Models;
-
-namespace BotSharp.Core.Users.ViewModels;
-
-public class UserCreationModel
-{
- public string FirstName { get; set; }
- public string LastName { get; set; }
- public string Email { get; set; }
- public string Password { get; set; }
-
- public User ToUser()
- {
- return new User
- {
- FirstName = FirstName,
- LastName = LastName,
- Email = Email,
- Password = Password
- };
- }
-}
diff --git a/src/Infrastructure/BotSharp.Core/Users/ViewModels/UserViewModel.cs b/src/Infrastructure/BotSharp.Core/Users/ViewModels/UserViewModel.cs
deleted file mode 100644
index ecb2c16b..00000000
--- a/src/Infrastructure/BotSharp.Core/Users/ViewModels/UserViewModel.cs
+++ /dev/null
@@ -1,22 +0,0 @@
-using BotSharp.Abstraction.Users.Models;
-
-namespace BotSharp.Core.Users.ViewModels;
-
-public class UserViewModel
-{
- public string Id { get; set; }
- public string FirstName { get; set; }
- public string LastName { get; set; }
- public string Email { get; set; }
-
- public static UserViewModel FromUser(User user)
- {
- return new UserViewModel
- {
- Id = user.Id,
- FirstName = user.FirstName,
- LastName = user.LastName,
- Email = user.Email
- };
- }
-}