diff --git a/src/Infrastructure/BotSharp.Abstraction/Agents/Enums/BuiltInAgentId.cs b/src/Infrastructure/BotSharp.Abstraction/Agents/Enums/BuiltInAgentId.cs new file mode 100644 index 00000000..98aaf454 --- /dev/null +++ b/src/Infrastructure/BotSharp.Abstraction/Agents/Enums/BuiltInAgentId.cs @@ -0,0 +1,8 @@ +namespace BotSharp.Abstraction.Agents.Enums; + +public class BuiltInAgentId +{ + public const string AIAssistant = "01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a"; + public const string Chatbot = "01e2fc5c-2c89-4ec7-8470-7688608b496c"; + public const string HumanSupport = "01dcc3e5-0af7-49e6-ad7a-a760bd12dc4b"; +} diff --git a/src/Infrastructure/BotSharp.Core/Agents/AgentPlugin.cs b/src/Infrastructure/BotSharp.Core/Agents/AgentPlugin.cs index fa572e67..43bd9c7f 100644 --- a/src/Infrastructure/BotSharp.Core/Agents/AgentPlugin.cs +++ b/src/Infrastructure/BotSharp.Core/Agents/AgentPlugin.cs @@ -17,9 +17,9 @@ public class AgentPlugin : IBotSharpPlugin public string[] AgentIds => new string[] { - "01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a", - "01e2fc5c-2c89-4ec7-8470-7688608b496c", - "01dcc3e5-0af7-49e6-ad7a-a760bd12dc4b" + BuiltInAgentId.AIAssistant, + BuiltInAgentId.Chatbot, + BuiltInAgentId.HumanSupport }; public object GetNewSettingsInstance() => diff --git a/src/Infrastructure/BotSharp.OpenAPI/Controllers/TranslationController.cs b/src/Infrastructure/BotSharp.OpenAPI/Controllers/TranslationController.cs new file mode 100644 index 00000000..ac7686a1 --- /dev/null +++ b/src/Infrastructure/BotSharp.OpenAPI/Controllers/TranslationController.cs @@ -0,0 +1,30 @@ +using BotSharp.Abstraction.Agents.Models; +using BotSharp.Abstraction.Translation; +using BotSharp.OpenAPI.ViewModels.Translations; + +namespace BotSharp.OpenAPI.Controllers; + +[Authorize] +[ApiController] +public class TranslationController : ControllerBase +{ + private readonly IServiceProvider _services; + + public TranslationController(IServiceProvider services) + { + _services = services; + } + + [HttpPost("/translate")] + public async Task Translate([FromBody] TranslationRequestModel model) + { + var agentService = _services.GetRequiredService(); + var agent = await agentService.LoadAgent(BuiltInAgentId.AIAssistant); + var translator = _services.GetRequiredService(); + var text = await translator.Translate(agent, Guid.NewGuid().ToString(), model.Text, language: model.ToLang); + return new TranslationResponseModel + { + Text = text + }; + } +} diff --git a/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Translations/TranslationRequestModel.cs b/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Translations/TranslationRequestModel.cs new file mode 100644 index 00000000..588dedfc --- /dev/null +++ b/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Translations/TranslationRequestModel.cs @@ -0,0 +1,9 @@ +using BotSharp.Abstraction.Infrastructures.Enums; + +namespace BotSharp.OpenAPI.ViewModels.Translations; + +public class TranslationRequestModel +{ + public string Text { get; set; } = null!; + public string ToLang { get; set; } = LanguageType.CHINESE; +} diff --git a/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Translations/TranslationResponseModel.cs b/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Translations/TranslationResponseModel.cs new file mode 100644 index 00000000..0c547d2b --- /dev/null +++ b/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Translations/TranslationResponseModel.cs @@ -0,0 +1,11 @@ +using System.Text.Json.Serialization; + +namespace BotSharp.OpenAPI.ViewModels.Translations; + +public class TranslationResponseModel +{ + public string Text { get; set; } = null!; + + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] + public string FromLang { get; set; } = null!; +} diff --git a/src/Plugins/BotSharp.Plugin.WebDriver/Drivers/PlaywrightDriver/PlaywrightInstance.cs b/src/Plugins/BotSharp.Plugin.WebDriver/Drivers/PlaywrightDriver/PlaywrightInstance.cs index 1218e524..69e94f13 100644 --- a/src/Plugins/BotSharp.Plugin.WebDriver/Drivers/PlaywrightDriver/PlaywrightInstance.cs +++ b/src/Plugins/BotSharp.Plugin.WebDriver/Drivers/PlaywrightDriver/PlaywrightInstance.cs @@ -71,7 +71,10 @@ public class PlaywrightInstance : IDisposable Serilog.Log.Information($"Page is closed: {e.Url}"); }; Serilog.Log.Information($"New page is created: {page.Url}"); - await page.SetViewportSizeAsync(1600, 900); + if (!page.IsClosed) + { + await page.SetViewportSizeAsync(1600, 900); + } /*page.Response += async (sender, e) => {