Merge pull request #757 from hchen2020/master
Fix validate_sql removing comments in sql.
This commit is contained in:
commit
b6f83e3722
|
|
@ -1,19 +1,3 @@
|
|||
using BotSharp.Abstraction.Agents.Enums;
|
||||
using BotSharp.Abstraction.Agents.Models;
|
||||
using BotSharp.Abstraction.Instructs;
|
||||
using BotSharp.Abstraction.Instructs.Models;
|
||||
using BotSharp.Abstraction.Routing;
|
||||
using BotSharp.Core.Agents.Services;
|
||||
using BotSharp.Core.Infrastructures;
|
||||
using BotSharp.Core.Instructs;
|
||||
using BotSharp.Plugin.SqlDriver.Interfaces;
|
||||
using BotSharp.Plugin.SqlDriver.Models;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data.Common;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace BotSharp.Plugin.SqlDriver.Functions;
|
||||
|
||||
public class SqlValidateFn : IFunctionCallback
|
||||
|
|
@ -22,26 +6,33 @@ public class SqlValidateFn : IFunctionCallback
|
|||
public string Indication => "Performing data validate operation.";
|
||||
private readonly IServiceProvider _services;
|
||||
private readonly ILogger _logger;
|
||||
public SqlValidateFn(IServiceProvider services)
|
||||
public SqlValidateFn(IServiceProvider services, ILogger<SqlValidateFn> logger)
|
||||
{
|
||||
_services = services;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public async Task<bool> Execute(RoleDialogModel message)
|
||||
{
|
||||
string pattern = @"```sql\s*([\s\S]*?)\s*```";
|
||||
var sqls = Regex.Match(message.Content, pattern);
|
||||
if (!sqls.Success)
|
||||
// remove comments start with "--"
|
||||
string pattern = @"--.*";
|
||||
string sql = Regex.Replace(message.Content, pattern, string.Empty);
|
||||
|
||||
pattern = @"```sql\s*([\s\S]*?)\s*```";
|
||||
sql = Regex.Match(sql, pattern)?.Value;
|
||||
|
||||
if (!Regex.IsMatch(sql, pattern))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
var sql = sqls.Groups[1].Value;
|
||||
|
||||
sql = Regex.Match(sql, pattern).Groups[1].Value;
|
||||
|
||||
var dbHook = _services.GetRequiredService<ISqlDriverHook>();
|
||||
var dbType = dbHook.GetDatabaseType(message);
|
||||
var validateSql = dbType.ToLower() switch
|
||||
{
|
||||
"mysql" => $"explain\r\n{sql.Replace("SET ", "-- SET ", StringComparison.InvariantCultureIgnoreCase).Replace(";", "; explain ").TrimEnd("explain ".ToCharArray())}",
|
||||
"mysql" => $"EXPLAIN\r\n{sql.Replace("SET ", "-- SET ", StringComparison.InvariantCultureIgnoreCase).Replace(";", "; EXPLAIN ").TrimEnd("EXPLAIN ".ToCharArray())}",
|
||||
"sqlserver" => $"SET PARSEONLY ON;\r\n{sql}\r\nSET PARSEONLY OFF;",
|
||||
"redshift" => $"explain\r\n{sql}",
|
||||
_ => throw new NotImplementedException($"Database type {dbType} is not supported.")
|
||||
|
|
|
|||
|
|
@ -1,17 +1,23 @@
|
|||
global using System;
|
||||
global using System.Collections.Generic;
|
||||
global using System.Text;
|
||||
global using System.Data.Common;
|
||||
global using System.Text.RegularExpressions;
|
||||
global using System.Threading.Tasks;
|
||||
global using System.Linq;
|
||||
global using System.Text.Json;
|
||||
|
||||
global using Microsoft.Extensions.Configuration;
|
||||
global using Microsoft.Extensions.Logging;
|
||||
|
||||
global using BotSharp.Abstraction.Conversations;
|
||||
global using BotSharp.Abstraction.Plugins;
|
||||
global using System.Text.Json;
|
||||
global using BotSharp.Abstraction.Conversations.Models;
|
||||
global using Microsoft.Extensions.Configuration;
|
||||
global using System.Threading.Tasks;
|
||||
global using BotSharp.Plugin.SqlDriver.Models;
|
||||
global using BotSharp.Abstraction.Functions;
|
||||
global using BotSharp.Abstraction.Agents.Models;
|
||||
global using BotSharp.Abstraction.Templating;
|
||||
global using Microsoft.Extensions.DependencyInjection;
|
||||
global using System.Linq;
|
||||
global using BotSharp.Abstraction.Agents;
|
||||
global using BotSharp.Abstraction.Utilities;
|
||||
global using BotSharp.Abstraction.Knowledges;
|
||||
|
|
@ -21,4 +27,8 @@ global using BotSharp.Plugin.SqlDriver.Hooks;
|
|||
global using BotSharp.Plugin.SqlDriver.Services;
|
||||
global using BotSharp.Plugin.SqlDriver.Enum;
|
||||
global using BotSharp.Plugin.SqlHero.Settings;
|
||||
global using System.Drawing;
|
||||
global using BotSharp.Abstraction.Agents.Enums;
|
||||
global using BotSharp.Abstraction.Instructs;
|
||||
global using BotSharp.Abstraction.Instructs.Models;
|
||||
global using BotSharp.Abstraction.Routing;
|
||||
global using BotSharp.Plugin.SqlDriver.Interfaces;
|
||||
|
|
|
|||
|
|
@ -1,13 +1,12 @@
|
|||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace BotSharp.Plugin.Twilio.OutboundPhoneCallHandler.LlmContexts
|
||||
{
|
||||
public class LlmContextIn
|
||||
{
|
||||
[JsonPropertyName("phone_number")]
|
||||
public string PhoneNumber { get; set; }
|
||||
namespace BotSharp.Plugin.Twilio.OutboundPhoneCallHandler.LlmContexts;
|
||||
|
||||
[JsonPropertyName("initial_message")]
|
||||
public string InitialMessage { get; set; }
|
||||
}
|
||||
public class LlmContextIn
|
||||
{
|
||||
[JsonPropertyName("phone_number")]
|
||||
public string PhoneNumber { get; set; }
|
||||
|
||||
[JsonPropertyName("initial_message")]
|
||||
public string InitialMessage { get; set; }
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,11 +1,10 @@
|
|||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace BotSharp.Plugin.Twilio.OutboundPhoneCallHandler.LlmContexts
|
||||
namespace BotSharp.Plugin.Twilio.OutboundPhoneCallHandler.LlmContexts;
|
||||
|
||||
public class LlmContextOut
|
||||
{
|
||||
public class LlmContextOut
|
||||
{
|
||||
[JsonPropertyName("conversation_id")]
|
||||
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
|
||||
public string ConversationId { get; set; }
|
||||
}
|
||||
[JsonPropertyName("conversation_id")]
|
||||
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
|
||||
public string ConversationId { get; set; }
|
||||
}
|
||||
|
|
|
|||
|
|
@ -12,6 +12,7 @@ public class TwilioPlugin : IBotSharpPlugin
|
|||
public string Id => "943ffd4d-ac8b-44aa-8a1c-38c9279c1b65";
|
||||
public string Name => "Twilio";
|
||||
public string Description => "Communication APIs for SMS, Voice, Video & Authentication";
|
||||
public string IconUrl => "https://w7.pngwing.com/pngs/918/671/png-transparent-twilio-full-logo-tech-companies.png";
|
||||
|
||||
public void RegisterDI(IServiceCollection services, IConfiguration config)
|
||||
{
|
||||
|
|
@ -20,10 +21,10 @@ public class TwilioPlugin : IBotSharpPlugin
|
|||
var settingService = provider.GetRequiredService<ISettingService>();
|
||||
return settingService.Bind<TwilioSetting>("Twilio");
|
||||
});
|
||||
TwilioClient.Init(config["Twilio:AccountSid"], config["Twilio:AuthToken"]);
|
||||
TwilioClient.Init(config["Twilio:AccountSid"], config["Twilio:RequestValidation:AuthToken"]);
|
||||
services.AddScoped<TwilioService>();
|
||||
|
||||
var conn = ConnectionMultiplexer.Connect(config["Twilio:RedisConnectionString"]);
|
||||
var conn = ConnectionMultiplexer.Connect(config["Database:Redis"]);
|
||||
var sessionManager = new TwilioSessionManager(conn);
|
||||
|
||||
services.AddSingleton<ITwilioSessionManager>(sessionManager);
|
||||
|
|
|
|||
Loading…
Reference in a new issue