Skip to content

Commit cf5b850

Browse files
authored
Merge pull request #682 from Qtoss-AI/master
optimize sql driver
2 parents e0ceec0 + 31322b2 commit cf5b850

File tree

26 files changed

+366
-63
lines changed

26 files changed

+366
-63
lines changed

src/Infrastructure/BotSharp.Abstraction/Browsing/Models/PageActionArgs.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,6 @@ public class PageActionArgs
4343
/// Wait time in seconds after page is opened
4444
/// </summary>
4545
public int WaitTime { get; set; }
46+
47+
public bool ReadInnerHTMLAsBody { get; set; } = false;
4648
}

src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public interface IBotSharpRepository
2727
User? GetUserByAffiliateId(string affiliateId) => throw new NotImplementedException();
2828
User? GetUserByUserName(string userName) => throw new NotImplementedException();
2929
void CreateUser(User user) => throw new NotImplementedException();
30+
void UpdateExistUser(string userId, User user) => throw new NotImplementedException();
3031
void UpdateUserVerified(string userId) => throw new NotImplementedException();
3132
void UpdateUserVerificationCode(string userId, string verficationCode) => throw new NotImplementedException();
3233
void UpdateUserPassword(string userId, string password) => throw new NotImplementedException();
Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,19 @@
1-
using BotSharp.Abstraction.Infrastructures.Enums;
2-
31
namespace BotSharp.OpenAPI.ViewModels.Translations;
42

53
public class TranslationRequestModel
64
{
75
public string Text { get; set; } = null!;
86
public string ToLang { get; set; } = LanguageType.CHINESE;
97
}
8+
9+
public class TranslationScriptTimestamp
10+
{
11+
public string Text { set; get; } = null!;
12+
public string Timestamp { get; set; } = null!;
13+
}
14+
15+
public class TranslationLongTextRequestModel
16+
{
17+
public TranslationScriptTimestamp[] Texts { get; set; } = null!;
18+
public string ToLang { get; set; } = LanguageType.CHINESE;
19+
}

src/Infrastructure/BotSharp.Abstraction/Users/Enums/UserRole.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,6 @@ public class UserRole
3333
/// AI Assistant
3434
/// </summary>
3535
public const string Assistant = "assistant";
36+
37+
public const string Root = "root";
3638
}

src/Infrastructure/BotSharp.Abstraction/Users/IUserService.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ public interface IUserService
1313
Task<User> GetMyProfile();
1414
Task<bool> VerifyUserNameExisting(string userName);
1515
Task<bool> VerifyEmailExisting(string email);
16-
Task<bool> SendVerificationCodeResetPassword(User user);
16+
Task<bool> SendVerificationCodeResetPasswordNoLogin(User user);
17+
Task<bool> SendVerificationCodeResetPasswordLogin();
1718
Task<bool> ResetUserPassword(User user);
1819
Task<bool> ModifyUserEmail(string email);
1920
Task<bool> ModifyUserPhone(string phone);

src/Infrastructure/BotSharp.Core/Translation/TranslationService.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,12 +101,12 @@ public async Task<T> Translate<T>(Agent router, string messageId, T data, string
101101
{
102102
var translatedStringList = await InnerTranslate(texts, language, template);
103103

104-
int retry = 0;
104+
/*int retry = 0;
105105
while (translatedStringList.Texts.Length != texts.Count && retry < 3)
106106
{
107107
translatedStringList = await InnerTranslate(texts, language, template);
108108
retry++;
109-
}
109+
}*/
110110

111111
// Override language if it's Unknown, it's used to output the corresponding language.
112112
var states = _services.GetRequiredService<IConversationStateService>();
@@ -119,7 +119,7 @@ public async Task<T> Translate<T>(Agent router, string messageId, T data, string
119119
var translatedTexts = translatedStringList.Texts;
120120
var memoryInputs = new List<TranslationMemoryInput>();
121121

122-
for (var i = 0; i < texts.Count; i++)
122+
for (var i = 0; i < Math.Min(texts.Count, translatedTexts.Length); i++)
123123
{
124124
map[outOfMemoryList[i].OriginalText] = translatedTexts[i].Text;
125125
memoryInputs.Add(new TranslationMemoryInput
@@ -375,6 +375,8 @@ private async Task<TranslationOutput> InnerTranslate(List<TranslationInput> text
375375
var render = _services.GetRequiredService<ITemplateRender>();
376376
var prompt = render.Render(template, translator.TemplateDict);
377377

378+
_logger.LogInformation($"Translation prompt: {prompt}");
379+
378380
var translationDialogs = new List<RoleDialogModel>
379381
{
380382
new RoleDialogModel(AgentRole.User, prompt)
@@ -384,6 +386,8 @@ private async Task<TranslationOutput> InnerTranslate(List<TranslationInput> text
384386
}
385387
};
386388
var response = await _completion.GetChatCompletions(translator, translationDialogs);
389+
390+
_logger.LogInformation(response.Content);
387391
return response.Content.JsonContent<TranslationOutput>();
388392
}
389393

src/Infrastructure/BotSharp.Core/Users/Services/UserService.cs

Lines changed: 58 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public UserService(IServiceProvider services,
3232

3333
public async Task<User> CreateUser(User user)
3434
{
35+
string hasRegisterId = null;
3536
if (string.IsNullOrEmpty(user.UserName))
3637
{
3738
// generate unique name
@@ -48,7 +49,7 @@ public async Task<User> CreateUser(User user)
4849

4950
if (record != null)
5051
{
51-
return record;
52+
hasRegisterId = record.Id;
5253
}
5354

5455
if (string.IsNullOrEmpty(user.Id))
@@ -71,7 +72,14 @@ record = user;
7172
record.Verified = false;
7273
}
7374

74-
db.CreateUser(record);
75+
if (hasRegisterId == null)
76+
{
77+
db.CreateUser(record);
78+
}
79+
else
80+
{
81+
db.UpdateExistUser(hasRegisterId, record);
82+
}
7583

7684
_logger.LogWarning($"Created new user account: {record.Id} {record.UserName}");
7785
Utilities.ClearCache();
@@ -386,8 +394,9 @@ public async Task<bool> VerifyUserNameExisting(string userName)
386394
}
387395

388396
var db = _services.GetRequiredService<IBotSharpRepository>();
397+
389398
var user = db.GetUserByUserName(userName);
390-
if (user != null)
399+
if (user != null && user.Verified)
391400
{
392401
return true;
393402
}
@@ -404,40 +413,64 @@ public async Task<bool> VerifyEmailExisting(string email)
404413

405414
var db = _services.GetRequiredService<IBotSharpRepository>();
406415
var emailName = db.GetUserByEmail(email);
407-
if (emailName != null)
416+
if (emailName != null && emailName.Verified)
408417
{
409418
return true;
410419
}
411420

412421
return false;
413422
}
414423

415-
public async Task<bool> SendVerificationCodeResetPassword(User user)
424+
public async Task<bool> SendVerificationCodeResetPasswordNoLogin(User user)
416425
{
417426
var db = _services.GetRequiredService<IBotSharpRepository>();
418427

419428
User? record = null;
420429

421-
if (!string.IsNullOrWhiteSpace(_user.Id))
430+
if (!string.IsNullOrEmpty(user.Email) && !string.IsNullOrEmpty(user.Phone))
422431
{
423-
record = db.GetUserById(_user.Id);
432+
return false;
424433
}
425-
else
434+
435+
if (!string.IsNullOrEmpty(user.Phone))
426436
{
427-
if (!string.IsNullOrEmpty(user.Email) && !string.IsNullOrEmpty(user.Phone))
428-
{
429-
return false;
430-
}
437+
record = db.GetUserByPhone(user.Phone);
438+
}
431439

432-
if (!string.IsNullOrEmpty(user.Email))
433-
{
434-
record = db.GetUserByEmail(user.Email);
435-
}
440+
if (!string.IsNullOrEmpty(user.Email))
441+
{
442+
record = db.GetUserByEmail(user.Email);
443+
}
436444

437-
if (!string.IsNullOrEmpty(user.Phone))
438-
{
439-
record = db.GetUserByPhone(user.Phone);
440-
}
445+
if (record == null)
446+
{
447+
return false;
448+
}
449+
450+
record.VerificationCode = Nanoid.Generate(alphabet: "0123456789", size: 6);
451+
452+
//update current verification code.
453+
db.UpdateUserVerificationCode(record.Id, record.VerificationCode);
454+
455+
//send code to user Email.
456+
var hooks = _services.GetServices<IAuthenticationHook>();
457+
foreach (var hook in hooks)
458+
{
459+
hook.VerificationCodeResetPassword(record);
460+
}
461+
462+
return true;
463+
}
464+
465+
public async Task<bool> SendVerificationCodeResetPasswordLogin()
466+
{
467+
var db = _services.GetRequiredService<IBotSharpRepository>();
468+
469+
User? record = null;
470+
471+
if (!string.IsNullOrWhiteSpace(_user.Id))
472+
{
473+
record = db.GetUserById(_user.Id);
441474
}
442475

443476
if (record == null)
@@ -520,6 +553,11 @@ public async Task<bool> ModifyUserPhone(string phone)
520553
return false;
521554
}
522555

556+
if ((record.UserName.Substring(0, 3) == "+86" || record.FirstName.Substring(0, 3) == "+86") && phone.Substring(0, 3) != "+86")
557+
{
558+
phone = $"+86{phone}";
559+
}
560+
523561
db.UpdateUserPhone(record.Id, phone);
524562
return true;
525563
}
Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,19 @@
11
{{ text_list }}
22

33
=====
4+
{% if language == "Chinese" %}
5+
将以上所有句子翻译成中文。
6+
7+
要求:
8+
* 以 JSON 格式输出翻译后的文本 {"input_lang":"原始文本语言", "output_count": {{ text_list_size }}, "output_lang":"{{ language }}", "texts":[{"id": 1, "text":""},{"id": 2, "text":""}]}。
9+
* output_count 必须等于输出中texts数组的长度。
10+
{% else %}
411
Translate all the above sentences into {{ language }}.
5-
Output the translated text in JSON {"input_lang":"original text language", "output_count": {{ text_list_size }}, "output_lang":"{{ language }}", "texts":[{"id": 1, "text":""},{"id": 2, "text":""}]}.
6-
The "output_count" must equal the length of the "texts" array in the output.
12+
13+
Requirements:
14+
* Output the translated text in JSON {"input_lang":"original text language", "output_count": {{ text_list_size }}, "output_lang":"{{ language }}", "texts":[{"id": 1, "text":""},{"id": 2, "text":""}]}.
15+
* The "output_count" must equal the length of the "texts" array in the output.
16+
{% endif %}
17+
18+
19+
Lines changed: 76 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using BotSharp.Abstraction.Agents.Models;
1+
using BotSharp.Abstraction.Options;
22
using BotSharp.Abstraction.Translation;
33
using BotSharp.OpenAPI.ViewModels.Translations;
44

@@ -9,10 +9,13 @@ namespace BotSharp.OpenAPI.Controllers;
99
public class TranslationController : ControllerBase
1010
{
1111
private readonly IServiceProvider _services;
12+
private readonly JsonSerializerOptions _jsonOptions;
1213

13-
public TranslationController(IServiceProvider services)
14+
public TranslationController(IServiceProvider services,
15+
BotSharpOptions options)
1416
{
1517
_services = services;
18+
_jsonOptions = InitJsonOptions(options);
1619
}
1720

1821
[HttpPost("/translate")]
@@ -21,10 +24,79 @@ public async Task<TranslationResponseModel> Translate([FromBody] TranslationRequ
2124
var agentService = _services.GetRequiredService<IAgentService>();
2225
var agent = await agentService.LoadAgent(BuiltInAgentId.AIAssistant);
2326
var translator = _services.GetRequiredService<ITranslationService>();
24-
var text = await translator.Translate(agent, Guid.NewGuid().ToString(), model.Text, language: model.ToLang);
27+
var states = _services.GetRequiredService<IConversationStateService>();
28+
states.SetState("max_tokens", "8192");
29+
var text = await translator.Translate(agent, Guid.NewGuid().ToString(), model.Text.Split("\r\n"), language: model.ToLang);
2530
return new TranslationResponseModel
2631
{
27-
Text = text
32+
Text = string.Join("\r\n", text)
2833
};
2934
}
35+
36+
[HttpPost("/translate/long-text")]
37+
public async Task SendMessageSse([FromBody] TranslationLongTextRequestModel model)
38+
{
39+
var agentService = _services.GetRequiredService<IAgentService>();
40+
var agent = await agentService.LoadAgent(BuiltInAgentId.AIAssistant);
41+
var translator = _services.GetRequiredService<ITranslationService>();
42+
43+
Response.StatusCode = 200;
44+
Response.Headers.Append(Microsoft.Net.Http.Headers.HeaderNames.ContentType, "text/event-stream");
45+
Response.Headers.Append(Microsoft.Net.Http.Headers.HeaderNames.CacheControl, "no-cache");
46+
Response.Headers.Append(Microsoft.Net.Http.Headers.HeaderNames.Connection, "keep-alive");
47+
48+
foreach (var script in model.Texts)
49+
{
50+
var translatedText = await translator.Translate(agent, Guid.NewGuid().ToString(), script.Text, language: model.ToLang);
51+
52+
var json = JsonSerializer.Serialize(new TranslationScriptTimestamp
53+
{
54+
Text = translatedText,
55+
Timestamp = script.Timestamp
56+
}, _jsonOptions);
57+
58+
await OnChunkReceived(Response, json);
59+
}
60+
61+
await OnEventCompleted(Response);
62+
}
63+
64+
private async Task OnChunkReceived(HttpResponse response, string text)
65+
{
66+
var buffer = Encoding.UTF8.GetBytes($"data:{text}\n");
67+
await response.Body.WriteAsync(buffer, 0, buffer.Length);
68+
await Task.Delay(10);
69+
70+
buffer = Encoding.UTF8.GetBytes("\n");
71+
await response.Body.WriteAsync(buffer, 0, buffer.Length);
72+
}
73+
74+
private async Task OnEventCompleted(HttpResponse response)
75+
{
76+
var buffer = Encoding.UTF8.GetBytes("data:[DONE]\n");
77+
await response.Body.WriteAsync(buffer, 0, buffer.Length);
78+
79+
buffer = Encoding.UTF8.GetBytes("\n");
80+
await response.Body.WriteAsync(buffer, 0, buffer.Length);
81+
}
82+
83+
private JsonSerializerOptions InitJsonOptions(BotSharpOptions options)
84+
{
85+
var jsonOption = new JsonSerializerOptions
86+
{
87+
PropertyNameCaseInsensitive = true,
88+
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
89+
AllowTrailingCommas = true
90+
};
91+
92+
if (options?.JsonSerializerOptions != null)
93+
{
94+
foreach (var option in options.JsonSerializerOptions.Converters)
95+
{
96+
jsonOption.Converters.Add(option);
97+
}
98+
}
99+
100+
return jsonOption;
101+
}
30102
}

src/Infrastructure/BotSharp.OpenAPI/Controllers/UserController.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,12 +108,20 @@ public async Task<bool> VerifyEmailExisting([FromQuery] string email)
108108
{
109109
return await _userService.VerifyEmailExisting(email);
110110
}
111+
111112
[AllowAnonymous]
112-
[HttpPost("/user/verifycode")]
113+
[HttpPost("/user/verifycode-out")]
113114
public async Task<bool> SendVerificationCodeResetPassword([FromBody] UserCreationModel user)
114115
{
115-
return await _userService.SendVerificationCodeResetPassword(user.ToUser());
116+
return await _userService.SendVerificationCodeResetPasswordNoLogin(user.ToUser());
116117
}
118+
119+
[HttpPost("/user/verifycode-in")]
120+
public async Task<bool> SendVerificationCodeResetPasswordLogined()
121+
{
122+
return await _userService.SendVerificationCodeResetPasswordLogin();
123+
}
124+
117125
[AllowAnonymous]
118126
[HttpPost("/user/resetpassword")]
119127
public async Task<bool> ResetUserPassword([FromBody] UserResetPasswordModel user)

0 commit comments

Comments
 (0)