Skip to content

Commit b6f83e3

Browse files
authored
Merge pull request #757 from hchen2020/master
Fix validate_sql removing comments in sql.
2 parents 45c1706 + b03c776 commit b6f83e3

File tree

5 files changed

+44
-44
lines changed

5 files changed

+44
-44
lines changed

src/Plugins/BotSharp.Plugin.SqlDriver/Functions/SqlValidateFn.cs

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,3 @@
1-
using BotSharp.Abstraction.Agents.Enums;
2-
using BotSharp.Abstraction.Agents.Models;
3-
using BotSharp.Abstraction.Instructs;
4-
using BotSharp.Abstraction.Instructs.Models;
5-
using BotSharp.Abstraction.Routing;
6-
using BotSharp.Core.Agents.Services;
7-
using BotSharp.Core.Infrastructures;
8-
using BotSharp.Core.Instructs;
9-
using BotSharp.Plugin.SqlDriver.Interfaces;
10-
using BotSharp.Plugin.SqlDriver.Models;
11-
using Microsoft.Extensions.Logging;
12-
using System;
13-
using System.Collections.Generic;
14-
using System.Data.Common;
15-
using System.Text.RegularExpressions;
16-
171
namespace BotSharp.Plugin.SqlDriver.Functions;
182

193
public class SqlValidateFn : IFunctionCallback
@@ -22,26 +6,33 @@ public class SqlValidateFn : IFunctionCallback
226
public string Indication => "Performing data validate operation.";
237
private readonly IServiceProvider _services;
248
private readonly ILogger _logger;
25-
public SqlValidateFn(IServiceProvider services)
9+
public SqlValidateFn(IServiceProvider services, ILogger<SqlValidateFn> logger)
2610
{
2711
_services = services;
12+
_logger = logger;
2813
}
2914

3015
public async Task<bool> Execute(RoleDialogModel message)
3116
{
32-
string pattern = @"```sql\s*([\s\S]*?)\s*```";
33-
var sqls = Regex.Match(message.Content, pattern);
34-
if (!sqls.Success)
17+
// remove comments start with "--"
18+
string pattern = @"--.*";
19+
string sql = Regex.Replace(message.Content, pattern, string.Empty);
20+
21+
pattern = @"```sql\s*([\s\S]*?)\s*```";
22+
sql = Regex.Match(sql, pattern)?.Value;
23+
24+
if (!Regex.IsMatch(sql, pattern))
3525
{
3626
return false;
3727
}
38-
var sql = sqls.Groups[1].Value;
28+
29+
sql = Regex.Match(sql, pattern).Groups[1].Value;
3930

4031
var dbHook = _services.GetRequiredService<ISqlDriverHook>();
4132
var dbType = dbHook.GetDatabaseType(message);
4233
var validateSql = dbType.ToLower() switch
4334
{
44-
"mysql" => $"explain\r\n{sql.Replace("SET ", "-- SET ", StringComparison.InvariantCultureIgnoreCase).Replace(";", "; explain ").TrimEnd("explain ".ToCharArray())}",
35+
"mysql" => $"EXPLAIN\r\n{sql.Replace("SET ", "-- SET ", StringComparison.InvariantCultureIgnoreCase).Replace(";", "; EXPLAIN ").TrimEnd("EXPLAIN ".ToCharArray())}",
4536
"sqlserver" => $"SET PARSEONLY ON;\r\n{sql}\r\nSET PARSEONLY OFF;",
4637
"redshift" => $"explain\r\n{sql}",
4738
_ => throw new NotImplementedException($"Database type {dbType} is not supported.")
Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,23 @@
11
global using System;
22
global using System.Collections.Generic;
33
global using System.Text;
4+
global using System.Data.Common;
5+
global using System.Text.RegularExpressions;
6+
global using System.Threading.Tasks;
7+
global using System.Linq;
8+
global using System.Text.Json;
9+
10+
global using Microsoft.Extensions.Configuration;
11+
global using Microsoft.Extensions.Logging;
12+
413
global using BotSharp.Abstraction.Conversations;
514
global using BotSharp.Abstraction.Plugins;
6-
global using System.Text.Json;
715
global using BotSharp.Abstraction.Conversations.Models;
8-
global using Microsoft.Extensions.Configuration;
9-
global using System.Threading.Tasks;
16+
global using BotSharp.Plugin.SqlDriver.Models;
1017
global using BotSharp.Abstraction.Functions;
1118
global using BotSharp.Abstraction.Agents.Models;
1219
global using BotSharp.Abstraction.Templating;
1320
global using Microsoft.Extensions.DependencyInjection;
14-
global using System.Linq;
1521
global using BotSharp.Abstraction.Agents;
1622
global using BotSharp.Abstraction.Utilities;
1723
global using BotSharp.Abstraction.Knowledges;
@@ -21,4 +27,8 @@
2127
global using BotSharp.Plugin.SqlDriver.Services;
2228
global using BotSharp.Plugin.SqlDriver.Enum;
2329
global using BotSharp.Plugin.SqlHero.Settings;
24-
global using System.Drawing;
30+
global using BotSharp.Abstraction.Agents.Enums;
31+
global using BotSharp.Abstraction.Instructs;
32+
global using BotSharp.Abstraction.Instructs.Models;
33+
global using BotSharp.Abstraction.Routing;
34+
global using BotSharp.Plugin.SqlDriver.Interfaces;
Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
using System.Text.Json.Serialization;
22

3-
namespace BotSharp.Plugin.Twilio.OutboundPhoneCallHandler.LlmContexts
3+
namespace BotSharp.Plugin.Twilio.OutboundPhoneCallHandler.LlmContexts;
4+
5+
public class LlmContextIn
46
{
5-
public class LlmContextIn
6-
{
7-
[JsonPropertyName("phone_number")]
8-
public string PhoneNumber { get; set; }
7+
[JsonPropertyName("phone_number")]
8+
public string PhoneNumber { get; set; }
99

10-
[JsonPropertyName("initial_message")]
11-
public string InitialMessage { get; set; }
12-
}
10+
[JsonPropertyName("initial_message")]
11+
public string InitialMessage { get; set; }
1312
}
Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
using System.Text.Json.Serialization;
22

3-
namespace BotSharp.Plugin.Twilio.OutboundPhoneCallHandler.LlmContexts
3+
namespace BotSharp.Plugin.Twilio.OutboundPhoneCallHandler.LlmContexts;
4+
5+
public class LlmContextOut
46
{
5-
public class LlmContextOut
6-
{
7-
[JsonPropertyName("conversation_id")]
8-
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
9-
public string ConversationId { get; set; }
10-
}
7+
[JsonPropertyName("conversation_id")]
8+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
9+
public string ConversationId { get; set; }
1110
}

src/Plugins/BotSharp.Plugin.Twilio/TwilioPlugin.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ public class TwilioPlugin : IBotSharpPlugin
1212
public string Id => "943ffd4d-ac8b-44aa-8a1c-38c9279c1b65";
1313
public string Name => "Twilio";
1414
public string Description => "Communication APIs for SMS, Voice, Video & Authentication";
15+
public string IconUrl => "https://w7.pngwing.com/pngs/918/671/png-transparent-twilio-full-logo-tech-companies.png";
1516

1617
public void RegisterDI(IServiceCollection services, IConfiguration config)
1718
{
@@ -20,10 +21,10 @@ public void RegisterDI(IServiceCollection services, IConfiguration config)
2021
var settingService = provider.GetRequiredService<ISettingService>();
2122
return settingService.Bind<TwilioSetting>("Twilio");
2223
});
23-
TwilioClient.Init(config["Twilio:AccountSid"], config["Twilio:AuthToken"]);
24+
TwilioClient.Init(config["Twilio:AccountSid"], config["Twilio:RequestValidation:AuthToken"]);
2425
services.AddScoped<TwilioService>();
2526

26-
var conn = ConnectionMultiplexer.Connect(config["Twilio:RedisConnectionString"]);
27+
var conn = ConnectionMultiplexer.Connect(config["Database:Redis"]);
2728
var sessionManager = new TwilioSessionManager(conn);
2829

2930
services.AddSingleton<ITwilioSessionManager>(sessionManager);

0 commit comments

Comments
 (0)