diff --git a/ShoppingAssistantApi.Infrastructure/Services/ProductService.cs b/ShoppingAssistantApi.Infrastructure/Services/ProductService.cs index a6f6ce1..e18da09 100644 --- a/ShoppingAssistantApi.Infrastructure/Services/ProductService.cs +++ b/ShoppingAssistantApi.Infrastructure/Services/ProductService.cs @@ -19,6 +19,10 @@ public class ProductService : IProductService private readonly IOpenAiService _openAiService; private readonly IMessagesRepository _messagesRepository; + + private bool mqchecker = false; + + private SearchEventType currentDataType = SearchEventType.Wishlist; public ProductService(IOpenAiService openAiService, IWishlistsService wishlistsService, IMessagesRepository messagesRepository) { @@ -79,20 +83,26 @@ public class ProductService : IProductService var suggestionBuffer = new Suggestion(); var messageBuffer = new MessagePart(); var productBuffer = new ProductName(); - var currentDataType = SearchEventType.Wishlist; var dataTypeHolder = string.Empty; + var counter = 0; 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() { Text = messageBuffer.Text, }, cancellationToken); + mqchecker = false; } + } + + if (data.Contains("[")) + { dataTypeHolder = string.Empty; dataTypeHolder += data; } @@ -101,13 +111,17 @@ public class ProductService : IProductService { dataTypeHolder += data; currentDataType = DetermineDataType(dataTypeHolder); + if (currentDataType == SearchEventType.Message) + { + mqchecker = true; + } } else if (dataTypeHolder=="[" && !data.Contains("[")) { dataTypeHolder += data; } - + else { switch (currentDataType) @@ -118,6 +132,7 @@ public class ProductService : IProductService Event = SearchEventType.Message, Data = data }; + currentDataType = SearchEventType.Message; messageBuffer.Text += data; 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) diff --git a/ShoppingAssistantApi.UnitTests/ProductTests.cs b/ShoppingAssistantApi.UnitTests/ProductTests.cs index 9887d90..18b83ea 100644 --- a/ShoppingAssistantApi.UnitTests/ProductTests.cs +++ b/ShoppingAssistantApi.UnitTests/ProductTests.cs @@ -46,10 +46,10 @@ public class ProductTests { "[", "Message", "]", " What", " u", " want", " ?", "[", "Options", "]", " USB-C", " ;", " Keyboard", " ultra", " ;", "[", "Options", "]", " USB", "-C", " ;", "[", "Products", "]", " GTX", " 3090", " ;", " GTX", - " 3070TI", " ;", " GTX", " 4070TI", " ;", " ?" + " 3070TI", " ;", " GTX", " 4070TI", " ;", " ?", "[", "Message", "]", " What", " u", " want", " ?" }; - var expectedMessages = new List { " What", " u", " want", " ?" }; + var expectedMessages = new List { " What", " u", " want", " ?", " What", " u", " want", " ?" }; var expectedSuggestion = new List { " USB-C", " Keyboard ultra", " USB-C" }; // Mock the GetChatCompletionStream method to provide the expected SSE data