From 45fc4c91d852f8ba77b4712848055b516603bb45 Mon Sep 17 00:00:00 2001 From: Jicheng Lu Date: Wed, 14 Feb 2024 19:13:35 -0600 Subject: [PATCH 1/2] add role in content log --- .../Loggers/Models/StreamingLogModel.cs | 2 ++ .../BotSharp.Plugin.ChatHub/Hooks/StreamingLogHook.cs | 9 +++++---- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/Infrastructure/BotSharp.Abstraction/Loggers/Models/StreamingLogModel.cs b/src/Infrastructure/BotSharp.Abstraction/Loggers/Models/StreamingLogModel.cs index 51941138e..e09862722 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Loggers/Models/StreamingLogModel.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Loggers/Models/StreamingLogModel.cs @@ -6,6 +6,8 @@ public class StreamingLogModel public string ConversationId { get; set; } [JsonPropertyName("name")] public string? Name { get; set; } + [JsonPropertyName("role")] + public string Role { get; set; } [JsonPropertyName("content")] public string Content { get; set; } diff --git a/src/Plugins/BotSharp.Plugin.ChatHub/Hooks/StreamingLogHook.cs b/src/Plugins/BotSharp.Plugin.ChatHub/Hooks/StreamingLogHook.cs index 8b0aa2cd5..c4e74ec41 100644 --- a/src/Plugins/BotSharp.Plugin.ChatHub/Hooks/StreamingLogHook.cs +++ b/src/Plugins/BotSharp.Plugin.ChatHub/Hooks/StreamingLogHook.cs @@ -37,7 +37,7 @@ public override async Task OnMessageReceived(RoleDialogModel message) { var conversationId = _state.GetConversationId(); var log = $"MessageId: {message.MessageId} ==>\r\n{message.Role}: {message.Content}"; - await _chatHub.Clients.User(_user.Id).SendAsync("OnContentLogGenerated", BuildLog(conversationId, _user.UserName, log)); + await _chatHub.Clients.User(_user.Id).SendAsync("OnContentLogGenerated", BuildLog(conversationId, _user.UserName, message.Role, log)); } public async Task BeforeGenerating(Agent agent, List conversations) @@ -64,21 +64,22 @@ public async Task AfterGenerated(RoleDialogModel message, TokenStatsModel tokenS var conversationId = _state.GetConversationId(); var agent = await agentService.LoadAgent(message.CurrentAgentId); - await _chatHub.Clients.User(_user.Id).SendAsync("OnContentLogGenerated", BuildLog(conversationId, agent?.Name, tokenStats.Prompt)); + await _chatHub.Clients.User(_user.Id).SendAsync("OnContentLogGenerated", BuildLog(conversationId, agent?.Name, message.Role, tokenStats.Prompt)); var log = message.Role == AgentRole.Function ? $"[{agent?.Name}]: {message.FunctionName}({message.FunctionArgs})" : $"[{agent?.Name}]: {message.Content}"; log += $"\r\n<== MessageId: {message.MessageId}"; - await _chatHub.Clients.User(_user.Id).SendAsync("OnContentLogGenerated", BuildLog(conversationId, agent?.Name, log)); + await _chatHub.Clients.User(_user.Id).SendAsync("OnContentLogGenerated", BuildLog(conversationId, agent?.Name, message.Role, log)); } - private string BuildLog(string conversationId, string? name, string content) + private string BuildLog(string conversationId, string? name, string role, string content) { var log = new StreamingLogModel { ConversationId = conversationId, Name = name, + Role = role, Content = content, CreateTime = DateTime.UtcNow }; From 7d1aa8fcf5c2c9eb10172b65de7b907565b93702 Mon Sep 17 00:00:00 2001 From: Jicheng Lu Date: Wed, 14 Feb 2024 19:48:03 -0600 Subject: [PATCH 2/2] fix oauth config --- .../FileRepository.Conversation.cs | 7 +++- .../BotSharpOpenApiExtensions.cs | 37 +++++++++++-------- 2 files changed, 27 insertions(+), 17 deletions(-) diff --git a/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.Conversation.cs b/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.Conversation.cs index 0b17b4927..2ae0d1a30 100644 --- a/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.Conversation.cs +++ b/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.Conversation.cs @@ -206,8 +206,13 @@ public PagedItems GetConversations(ConversationFilter filter) var records = new List(); var dir = Path.Combine(_dbSettings.FileRepository, _conversationSettings.DataDir); var pager = filter?.Pager ?? new Pagination(); - var totalDirs = Directory.GetDirectories(dir); + if (!Directory.Exists(dir)) + { + Directory.CreateDirectory(dir); + } + + var totalDirs = Directory.GetDirectories(dir); foreach (var d in totalDirs) { var path = Path.Combine(d, CONVERSATION_FILE); diff --git a/src/Infrastructure/BotSharp.OpenAPI/BotSharpOpenApiExtensions.cs b/src/Infrastructure/BotSharp.OpenAPI/BotSharpOpenApiExtensions.cs index 7d6796c52..5d744f8e6 100644 --- a/src/Infrastructure/BotSharp.OpenAPI/BotSharpOpenApiExtensions.cs +++ b/src/Infrastructure/BotSharp.OpenAPI/BotSharpOpenApiExtensions.cs @@ -32,7 +32,7 @@ public static IServiceCollection AddBotSharpOpenAPI(this IServiceCollection serv // Add bearer authentication var schema = "MIXED_SCHEME"; - services.AddAuthentication(options => + var builder = services.AddAuthentication(options => { // custom scheme defined in .AddPolicyScheme() below // inspired from https://weblog.west-wind.com/posts/2022/Mar/29/Combining-Bearer-Token-and-Cookie-Auth-in-ASPNET @@ -59,11 +59,6 @@ public static IServiceCollection AddBotSharpOpenAPI(this IServiceCollection serv } }).AddCookie(options => { - }).AddGitHub(options => - { - options.ClientId = config["OAuth:GitHub:ClientId"]; - options.ClientSecret = config["OAuth:GitHub:ClientSecret"]; - options.Scope.Add("user:email"); }).AddPolicyScheme(schema, "Mixed authentication", options => { // runs on each request @@ -85,6 +80,16 @@ public static IServiceCollection AddBotSharpOpenAPI(this IServiceCollection serv }; }); + if (!string.IsNullOrWhiteSpace(config["OAuth:GitHub:ClientId"]) && !string.IsNullOrWhiteSpace(config["OAuth:GitHub:ClientSecret"])) + { + builder = builder.AddGitHub(options => + { + options.ClientId = config["OAuth:GitHub:ClientId"]; + options.ClientSecret = config["OAuth:GitHub:ClientSecret"]; + options.Scope.Add("user:email"); + }); + } + // Add services to the container. services.AddControllers() .AddJsonOptions(options => @@ -106,18 +111,18 @@ public static IServiceCollection AddBotSharpOpenAPI(this IServiceCollection serv Type = SecuritySchemeType.ApiKey }); c.AddSecurityRequirement(new OpenApiSecurityRequirement { - { - new OpenApiSecurityScheme - { - Reference = new OpenApiReference { - Type = ReferenceType.SecurityScheme, - Id = "Bearer" + new OpenApiSecurityScheme + { + Reference = new OpenApiReference + { + Type = ReferenceType.SecurityScheme, + Id = "Bearer" + } + }, + Array.Empty() } - }, - Array.Empty() - } - }); + }); } );