Merge pull request #15 from Shchoholiev/bug/SA-150-fix-adding-message

bug/SA-150-fix-adding-message
This commit is contained in:
Mykhailo Bilodid 2023-11-12 01:01:03 +02:00 committed by GitHub
commit 93d2e9e0c2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 6 deletions

View File

@ -19,6 +19,10 @@ public class ProductService : IProductService
private readonly IOpenAiService _openAiService; private readonly IOpenAiService _openAiService;
private readonly IMessagesRepository _messagesRepository; private readonly IMessagesRepository _messagesRepository;
private bool mqchecker = false;
private SearchEventType currentDataType = SearchEventType.Wishlist;
public ProductService(IOpenAiService openAiService, IWishlistsService wishlistsService, IMessagesRepository messagesRepository) public ProductService(IOpenAiService openAiService, IWishlistsService wishlistsService, IMessagesRepository messagesRepository)
{ {
@ -79,20 +83,26 @@ public class ProductService : IProductService
var suggestionBuffer = new Suggestion(); var suggestionBuffer = new Suggestion();
var messageBuffer = new MessagePart(); var messageBuffer = new MessagePart();
var productBuffer = new ProductName(); var productBuffer = new ProductName();
var currentDataType = SearchEventType.Wishlist;
var dataTypeHolder = string.Empty; var dataTypeHolder = string.Empty;
var counter = 0;
await foreach (var data in _openAiService.GetChatCompletionStream(chatRequest, cancellationToken)) await foreach (var data in _openAiService.GetChatCompletionStream(chatRequest, cancellationToken))
{ {
if (data.Contains("[")) counter++;
if (mqchecker && currentDataType == SearchEventType.Message && messageBuffer != null)
{ {
if (dataTypeHolder=="[Message]" && messageBuffer.Text!=null) if (data == "[")
{ {
_wishlistsService.AddMessageToPersonalWishlistAsync(wishlistId, new MessageCreateDto() _wishlistsService.AddMessageToPersonalWishlistAsync(wishlistId, new MessageCreateDto()
{ {
Text = messageBuffer.Text, Text = messageBuffer.Text,
}, cancellationToken); }, cancellationToken);
mqchecker = false;
} }
}
if (data.Contains("["))
{
dataTypeHolder = string.Empty; dataTypeHolder = string.Empty;
dataTypeHolder += data; dataTypeHolder += data;
} }
@ -101,13 +111,17 @@ public class ProductService : IProductService
{ {
dataTypeHolder += data; dataTypeHolder += data;
currentDataType = DetermineDataType(dataTypeHolder); currentDataType = DetermineDataType(dataTypeHolder);
if (currentDataType == SearchEventType.Message)
{
mqchecker = true;
}
} }
else if (dataTypeHolder=="[" && !data.Contains("[")) else if (dataTypeHolder=="[" && !data.Contains("["))
{ {
dataTypeHolder += data; dataTypeHolder += data;
} }
else else
{ {
switch (currentDataType) switch (currentDataType)
@ -118,6 +132,7 @@ public class ProductService : IProductService
Event = SearchEventType.Message, Event = SearchEventType.Message,
Data = data Data = data
}; };
currentDataType = SearchEventType.Message;
messageBuffer.Text += data; messageBuffer.Text += data;
break; break;
@ -163,6 +178,14 @@ public class ProductService : IProductService
} }
} }
} }
if (currentDataType == SearchEventType.Message)
{
_wishlistsService.AddMessageToPersonalWishlistAsync(wishlistId, new MessageCreateDto()
{
Text = messageBuffer.Text,
}, cancellationToken);
mqchecker = false;
}
} }
private SearchEventType DetermineDataType(string dataTypeHolder) private SearchEventType DetermineDataType(string dataTypeHolder)

View File

@ -46,10 +46,10 @@ public class ProductTests
{ {
"[", "Message", "]", " What", " u", " want", " ?", "[", "Options", "]", " USB-C", " ;", " Keyboard", " ultra", "[", "Message", "]", " What", " u", " want", " ?", "[", "Options", "]", " USB-C", " ;", " Keyboard", " ultra",
" ;", "[", "Options", "]", " USB", "-C", " ;", "[", "Products", "]", " GTX", " 3090", " ;", " GTX", " ;", "[", "Options", "]", " USB", "-C", " ;", "[", "Products", "]", " GTX", " 3090", " ;", " GTX",
" 3070TI", " ;", " GTX", " 4070TI", " ;", " ?" " 3070TI", " ;", " GTX", " 4070TI", " ;", " ?", "[", "Message", "]", " What", " u", " want", " ?"
}; };
var expectedMessages = new List<string> { " What", " u", " want", " ?" }; var expectedMessages = new List<string> { " What", " u", " want", " ?", " What", " u", " want", " ?" };
var expectedSuggestion = new List<string> { " USB-C", " Keyboard ultra", " USB-C" }; var expectedSuggestion = new List<string> { " USB-C", " Keyboard ultra", " USB-C" };
// Mock the GetChatCompletionStream method to provide the expected SSE data // Mock the GetChatCompletionStream method to provide the expected SSE data