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

@ -20,6 +20,10 @@ public class ProductService : IProductService
private readonly IMessagesRepository _messagesRepository;
private bool mqchecker = false;
private SearchEventType currentDataType = SearchEventType.Wishlist;
public ProductService(IOpenAiService openAiService, IWishlistsService wishlistsService, IMessagesRepository messagesRepository)
{
_openAiService = openAiService;
@ -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,6 +111,10 @@ public class ProductService : IProductService
{
dataTypeHolder += data;
currentDataType = DetermineDataType(dataTypeHolder);
if (currentDataType == SearchEventType.Message)
{
mqchecker = true;
}
}
else if (dataTypeHolder=="[" && !data.Contains("["))
@ -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)

View File

@ -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<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" };
// Mock the GetChatCompletionStream method to provide the expected SSE data