diff --git a/src/Infrastructure/BotSharp.Abstraction/Agents/Models/Agent.cs b/src/Infrastructure/BotSharp.Abstraction/Agents/Models/Agent.cs index 0e9a33cd..68be9939 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Agents/Models/Agent.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Agents/Models/Agent.cs @@ -26,7 +26,7 @@ public class Agent /// /// Responses /// - public List Responses { get; set; } + public List Responses { get; set; } /// /// Domain knowledges @@ -69,9 +69,9 @@ public class Agent return this; } - public Agent SetResponses(List responses) + public Agent SetResponses(List responses) { - Responses = responses ?? new List(); ; + Responses = responses ?? new List(); ; return this; } diff --git a/src/Infrastructure/BotSharp.Abstraction/Agents/Models/AgentResponse.cs b/src/Infrastructure/BotSharp.Abstraction/Agents/Models/AgentResponse.cs new file mode 100644 index 00000000..9eac0d1a --- /dev/null +++ b/src/Infrastructure/BotSharp.Abstraction/Agents/Models/AgentResponse.cs @@ -0,0 +1,20 @@ +namespace BotSharp.Abstraction.Agents.Models; + +public class AgentResponse +{ + public string Prefix { get; set; } + public string Intent { get; set; } + public string Content { get; set; } + + public AgentResponse() + { + + } + + public AgentResponse(string prefix, string intent, string content) + { + Prefix = prefix; + Intent = intent; + Content = content; + } +} diff --git a/src/Infrastructure/BotSharp.Abstraction/Conversations/Models/ConversationState.cs b/src/Infrastructure/BotSharp.Abstraction/Conversations/Models/ConversationState.cs index 8cd753a1..f957f184 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Conversations/Models/ConversationState.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Conversations/Models/ConversationState.cs @@ -1,5 +1,3 @@ -using BotSharp.Abstraction.Repositories.Models; - namespace BotSharp.Abstraction.Conversations.Models; public class ConversationState : Dictionary @@ -9,7 +7,7 @@ public class ConversationState : Dictionary } - public ConversationState(List pairs) + public ConversationState(List pairs) { foreach (var pair in pairs) { @@ -17,8 +15,8 @@ public class ConversationState : Dictionary } } - public List ToKeyValueList() + public List ToKeyValueList() { - return this.Select(x => new KeyValueModel(x.Key, x.Value)).ToList(); + return this.Select(x => new StateKeyValue(x.Key, x.Value)).ToList(); } } diff --git a/src/Infrastructure/BotSharp.Abstraction/Conversations/Models/StateKeyValue.cs b/src/Infrastructure/BotSharp.Abstraction/Conversations/Models/StateKeyValue.cs new file mode 100644 index 00000000..ad02628f --- /dev/null +++ b/src/Infrastructure/BotSharp.Abstraction/Conversations/Models/StateKeyValue.cs @@ -0,0 +1,18 @@ +namespace BotSharp.Abstraction.Conversations.Models; + +public class StateKeyValue +{ + public string Key { get; set; } + public string Value { get; set; } + + public StateKeyValue() + { + + } + + public StateKeyValue(string key, string value) + { + Key = key; + Value = value; + } +} diff --git a/src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs b/src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs index 8ce4f486..350fbaa4 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs @@ -1,4 +1,3 @@ -using BotSharp.Abstraction.Repositories.Models; using BotSharp.Abstraction.Routing.Models; using BotSharp.Abstraction.Users.Models; @@ -32,8 +31,8 @@ public interface IBotSharpRepository string GetConversationDialog(string conversationId); void UpdateConversationDialog(string conversationId, string dialogs); - List GetConversationStates(string conversationId); - void UpdateConversationStates(string conversationId, List states); + List GetConversationStates(string conversationId); + void UpdateConversationStates(string conversationId, List states); Conversation GetConversation(string conversationId); List GetConversations(string userId); diff --git a/src/Infrastructure/BotSharp.Abstraction/Repositories/Models/KeyValueModel.cs b/src/Infrastructure/BotSharp.Abstraction/Repositories/Models/KeyValueModel.cs deleted file mode 100644 index 412e618b..00000000 --- a/src/Infrastructure/BotSharp.Abstraction/Repositories/Models/KeyValueModel.cs +++ /dev/null @@ -1,19 +0,0 @@ - -namespace BotSharp.Abstraction.Repositories.Models; - -public class KeyValueModel -{ - public string Key { get; set; } - public string Value { get; set; } - - public KeyValueModel() - { - - } - - public KeyValueModel(string key, string value) - { - Key = key; - Value = value; - } -} diff --git a/src/Infrastructure/BotSharp.Abstraction/Repositories/Records/ConversationRecord.cs b/src/Infrastructure/BotSharp.Abstraction/Repositories/Records/ConversationRecord.cs index 5b5c58b8..7a2f7dde 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Repositories/Records/ConversationRecord.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Repositories/Records/ConversationRecord.cs @@ -1,4 +1,3 @@ -using BotSharp.Abstraction.Repositories.Models; using System.Text.Json.Serialization; namespace BotSharp.Abstraction.Repositories.Records; @@ -20,7 +19,7 @@ public class ConversationRecord : RecordBase public string Dialog { get; set; } [JsonIgnore] - public List States { get; set; } + public List States { get; set; } [Required] public DateTime UpdatedTime { get; set; } = DateTime.UtcNow; diff --git a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.CreateAgent.cs b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.CreateAgent.cs index fb120037..4089d430 100644 --- a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.CreateAgent.cs +++ b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.CreateAgent.cs @@ -108,15 +108,20 @@ public partial class AgentService return functions; } - private List FetchResponsesFromFile(string fileDir) + private List FetchResponsesFromFile(string fileDir) { - var responses = new List(); + var responses = new List(); var responseDir = Path.Combine(fileDir, "responses"); if (!Directory.Exists(responseDir)) return responses; foreach (var file in Directory.GetFiles(responseDir)) { - responses.Add(File.ReadAllText(file)); + var fileName = file.Split(Path.DirectorySeparatorChar).Last(); + var splits = fileName.Split('.'); + var prefix = splits[0]; + var intent = splits[1]; + var content = File.ReadAllText(file); + responses.Add(new AgentResponse(prefix, intent, content)); } return responses; } diff --git a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationStateService.cs b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationStateService.cs index f7899710..b7a52f7a 100644 --- a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationStateService.cs +++ b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationStateService.cs @@ -1,5 +1,4 @@ using BotSharp.Abstraction.Repositories; -using BotSharp.Abstraction.Repositories.Models; using System.IO; namespace BotSharp.Core.Conversations.Services; @@ -16,7 +15,7 @@ public class ConversationStateService : IConversationStateService, IDisposable private string _conversationId; private string _file; private readonly IBotSharpRepository _db; - private List _savedStates; + private List _savedStates; public ConversationStateService(ILogger logger, IServiceProvider services, @@ -78,11 +77,11 @@ public class ConversationStateService : IConversationStateService, IDisposable return; } - var states = new List(); + var states = new List(); foreach (var dic in _states) { - states.Add(new KeyValueModel(dic.Key, dic.Value)); + states.Add(new StateKeyValue(dic.Key, dic.Value)); } _db.UpdateConversationStates(_conversationId, states); diff --git a/src/Infrastructure/BotSharp.Core/Repository/BotSharpDbContext.cs b/src/Infrastructure/BotSharp.Core/Repository/BotSharpDbContext.cs index 1804a787..8747735e 100644 --- a/src/Infrastructure/BotSharp.Core/Repository/BotSharpDbContext.cs +++ b/src/Infrastructure/BotSharp.Core/Repository/BotSharpDbContext.cs @@ -1,6 +1,5 @@ using BotSharp.Abstraction.Agents.Models; using BotSharp.Abstraction.Repositories; -using BotSharp.Abstraction.Repositories.Models; using BotSharp.Abstraction.Routing.Models; using BotSharp.Abstraction.Users.Models; using Microsoft.EntityFrameworkCore.Infrastructure; @@ -128,7 +127,7 @@ public class BotSharpDbContext : Database, IBotSharpRepository throw new NotImplementedException(); } - public List GetConversationStates(string conversationId) + public List GetConversationStates(string conversationId) { throw new NotImplementedException(); } @@ -148,7 +147,7 @@ public class BotSharpDbContext : Database, IBotSharpRepository throw new NotImplementedException(); } - public void UpdateConversationStates(string conversationId, List states) + public void UpdateConversationStates(string conversationId, List states) { throw new NotImplementedException(); } diff --git a/src/Infrastructure/BotSharp.Core/Repository/FileRepository.cs b/src/Infrastructure/BotSharp.Core/Repository/FileRepository.cs index 53ed4780..107cbd50 100644 --- a/src/Infrastructure/BotSharp.Core/Repository/FileRepository.cs +++ b/src/Infrastructure/BotSharp.Core/Repository/FileRepository.cs @@ -1,5 +1,4 @@ using BotSharp.Abstraction.Repositories; -using BotSharp.Abstraction.Repositories.Models; using System.IO; using FunctionDef = BotSharp.Abstraction.Functions.Models.FunctionDef; using BotSharp.Abstraction.Users.Models; @@ -454,9 +453,9 @@ public class FileRepository : IBotSharpRepository return; } - public List GetConversationStates(string conversationId) + public List GetConversationStates(string conversationId) { - var curStates = new List(); + var curStates = new List(); var convDir = FindConversationDirectory(conversationId); if (!string.IsNullOrEmpty(convDir)) { @@ -467,7 +466,7 @@ public class FileRepository : IBotSharpRepository foreach (var line in dict) { var data = line.Split('='); - curStates.Add(new KeyValueModel(data[0], data[1])); + curStates.Add(new StateKeyValue(data[0], data[1])); } } } @@ -495,7 +494,7 @@ public class FileRepository : IBotSharpRepository return null; } - public void UpdateConversationStates(string conversationId, List states) + public void UpdateConversationStates(string conversationId, List states) { var localStates = new List(); var convDir = FindConversationDirectory(conversationId); @@ -532,7 +531,7 @@ public class FileRepository : IBotSharpRepository if (record != null && File.Exists(stateFile)) { var states = File.ReadLines(stateFile); - record.States = new ConversationState(states.Select(x => new KeyValueModel(x.Split('=')[0], x.Split('=')[1])).ToList()); + record.States = new ConversationState(states.Select(x => new StateKeyValue(x.Split('=')[0], x.Split('=')[1])).ToList()); } return record; diff --git a/src/Infrastructure/BotSharp.Core/Templating/ResponseTemplateService.cs b/src/Infrastructure/BotSharp.Core/Templating/ResponseTemplateService.cs index 583ccb2f..8fa6101d 100644 --- a/src/Infrastructure/BotSharp.Core/Templating/ResponseTemplateService.cs +++ b/src/Infrastructure/BotSharp.Core/Templating/ResponseTemplateService.cs @@ -19,12 +19,6 @@ public class ResponseTemplateService : IResponseTemplateService public async Task RenderFunctionResponse(string agentId, RoleDialogModel message) { // Find response template - //var agentService = _services.GetRequiredService(); - //var dir = Path.Combine(agentService.GetAgentDataDir(agentId), "responses"); - //var responses = Directory.GetFiles(dir) - // .Where(f => f.Split(Path.DirectorySeparatorChar).Last().Split('.')[1] == message.FunctionName) - // .ToList(); - var db = _services.GetRequiredService(); var responses = db.GetAgentResponses(agentId, "func", message.FunctionName); @@ -34,7 +28,6 @@ public class ResponseTemplateService : IResponseTemplateService } var randomIndex = new Random().Next(0, responses.Count); - //var template = File.ReadAllText(responses[randomIndex]); var template = responses[randomIndex]; var render = _services.GetRequiredService(); diff --git a/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/AgentCreationModel.cs b/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/AgentCreationModel.cs index 34dac0b1..24e92f37 100644 --- a/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/AgentCreationModel.cs +++ b/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/AgentCreationModel.cs @@ -8,7 +8,7 @@ public class AgentCreationModel public string Description { get; set; } public string Instruction { get; set; } public List Functions { get; set; } - public List Responses { get; set; } + public List Responses { get; set; } public bool IsPublic { get; set; } public Agent ToAgent() diff --git a/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/AgentUpdateModel.cs b/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/AgentUpdateModel.cs index 98502ec4..af38f81a 100644 --- a/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/AgentUpdateModel.cs +++ b/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/AgentUpdateModel.cs @@ -25,7 +25,7 @@ public class AgentUpdateModel /// /// Routes /// - public List Responses { get; set; } + public List Responses { get; set; } public Agent ToAgent() { diff --git a/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/AgentViewModel.cs b/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/AgentViewModel.cs index fcf09026..e0d1ebb5 100644 --- a/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/AgentViewModel.cs +++ b/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/AgentViewModel.cs @@ -9,7 +9,7 @@ public class AgentViewModel public string Description { get; set; } public string Instruction { get; set; } public List Functions { get; set; } - public List Responses { get; set; } + public List Responses { get; set; } public bool IsPublic { get; set; } public DateTime UpdatedDateTime { get; set; } diff --git a/src/Plugins/BotSharp.Plugin.MongoStorage/Collections/AgentCollection.cs b/src/Plugins/BotSharp.Plugin.MongoStorage/Collections/AgentCollection.cs index 3799af15..adfb89d2 100644 --- a/src/Plugins/BotSharp.Plugin.MongoStorage/Collections/AgentCollection.cs +++ b/src/Plugins/BotSharp.Plugin.MongoStorage/Collections/AgentCollection.cs @@ -1,3 +1,5 @@ +using BotSharp.Abstraction.Agents.Models; + namespace BotSharp.Plugin.MongoStorage.Collections; public class AgentCollection : MongoBase @@ -6,7 +8,7 @@ public class AgentCollection : MongoBase public string Description { get; set; } public string Instruction { get; set; } public List Functions { get; set; } - public List Responses { get; set; } + public List Responses { get; set; } public bool IsPublic { get; set; } public DateTime CreatedTime { get; set; } diff --git a/src/Plugins/BotSharp.Plugin.MongoStorage/Collections/ConversationCollection.cs b/src/Plugins/BotSharp.Plugin.MongoStorage/Collections/ConversationCollection.cs index 4ae900e7..7dbb51af 100644 --- a/src/Plugins/BotSharp.Plugin.MongoStorage/Collections/ConversationCollection.cs +++ b/src/Plugins/BotSharp.Plugin.MongoStorage/Collections/ConversationCollection.cs @@ -1,4 +1,4 @@ -using BotSharp.Abstraction.Repositories.Models; +using BotSharp.Abstraction.Conversations.Models; namespace BotSharp.Plugin.MongoStorage.Collections; @@ -7,7 +7,7 @@ public class ConversationCollection : MongoBase public Guid AgentId { get; set; } public Guid UserId { get; set; } public string Title { get; set; } - public List States { get; set; } + public List States { get; set; } public DateTime CreatedTime { get; set; } public DateTime UpdatedTime { get; set; } } diff --git a/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.cs b/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.cs index d00414cf..eaf1a4ce 100644 --- a/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.cs +++ b/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.cs @@ -1,6 +1,5 @@ using BotSharp.Abstraction.Agents.Models; using BotSharp.Abstraction.Conversations.Models; -using BotSharp.Abstraction.Repositories.Models; using BotSharp.Abstraction.Routing.Models; using BotSharp.Abstraction.Users.Models; using BotSharp.Plugin.MongoStorage.Collections; @@ -225,7 +224,7 @@ public class MongoRepository : IBotSharpRepository AgentId = Guid.Parse(x.AgentId), UserId = Guid.Parse(x.UserId), Title = x.Title, - States = x.States?.ToKeyValueList() ?? new List(), + States = x.States?.ToKeyValueList() ?? new List(), CreatedTime = x.CreatedTime, UpdatedTime = x.UpdatedTime }).ToList(); @@ -457,8 +456,7 @@ public class MongoRepository : IBotSharpRepository var agent = Agents.FirstOrDefault(x => x.Id == agentId); if (agent == null) return responses; - // Should use name to filter by prefix - return agent.Responses.Where(x => x.StartsWith(prefix + "." + intent)).ToList(); + return agent.Responses.Where(x => x.Prefix == prefix && x.Intent == intent).Select(x => x.Content).ToList(); } public Agent GetAgent(string agentId) @@ -477,7 +475,7 @@ public class MongoRepository : IBotSharpRepository AgentId = Guid.Parse(conversation.AgentId), UserId = Guid.Parse(conversation.UserId), Title = conversation.Title, - States = conversation.States?.ToKeyValueList() ?? new List(), + States = conversation.States?.ToKeyValueList() ?? new List(), CreatedTime = DateTime.UtcNow, UpdatedTime = DateTime.UtcNow, }; @@ -523,18 +521,18 @@ public class MongoRepository : IBotSharpRepository _dc.Conversations.UpdateOne(filterConv, updateConv); } - public List GetConversationStates(string conversationId) + public List GetConversationStates(string conversationId) { - var states = new List(); + var states = new List(); if (string.IsNullOrEmpty(conversationId)) return states; var filter = Builders.Filter.Eq(x => x.Id, Guid.Parse(conversationId)); var foundConversation = _dc.Conversations.Find(filter).FirstOrDefault(); - var savedStates = foundConversation?.States ?? new List(); + var savedStates = foundConversation?.States ?? new List(); return savedStates; } - public void UpdateConversationStates(string conversationId, List states) + public void UpdateConversationStates(string conversationId, List states) { if (string.IsNullOrEmpty(conversationId)) return; @@ -568,7 +566,7 @@ public class MongoRepository : IBotSharpRepository UserId = conv.UserId.ToString(), Title = conv.Title, Dialog = dialog?.Dialog ?? string.Empty, - States = new ConversationState(conv.States ?? new List()), + States = new ConversationState(conv.States ?? new List()), CreatedTime = conv.CreatedTime, UpdatedTime = conv.UpdatedTime };