Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added conversation update. #601

Merged
merged 3 commits into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ public class ConversationFilter
/// Conversation Id
/// </summary>
public string? Id { get; set; }
public string? Title { get; set; }
public string? AgentId { get; set; }
public string? Status { get; set; }
public string? Channel { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,10 @@ public PagedItems<Conversation> GetConversations(ConversationFilter filter)
{
matched = matched && record.Id == filter.Id;
}
if (filter?.Title != null)
{
matched = matched && record.Title.Contains(filter.Title);
}
if (filter?.AgentId != null)
{
matched = matched && record.AgentId == filter.AgentId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public async Task<PagedItems<ConversationViewModel>> GetConversations([FromBody]
return new PagedItems<ConversationViewModel>();
}

filter.UserId = user.Role != UserRole.Admin ? user.Id : null;
filter.UserId = user.Role != UserRole.Admin ? user.Id : filter.UserId;
var conversations = await convService.GetConversations(filter);
var agentService = _services.GetRequiredService<IAgentService>();
var list = conversations.Items.Select(x => ConversationViewModel.FromSession(x)).ToList();
Expand Down Expand Up @@ -154,6 +154,7 @@ public async Task<IEnumerable<ChatResponseModel>> GetDialogs([FromRoute] string
var result = ConversationViewModel.FromSession(conversations.Items.First());
var state = _services.GetRequiredService<IConversationStateService>();
result.States = state.Load(conversationId, isReadOnly: true);
user = await userService.GetUser(result.User.Id);
result.User = UserViewModel.FromUser(user);

return result;
Expand Down Expand Up @@ -195,6 +196,29 @@ public async Task<UserViewModel> GetConversationUser([FromRoute] string conversa
return UserViewModel.FromUser(user);
}

[HttpPut("/conversation/{conversationId}/update-title")]
public async Task<bool> UpdateConversationTitle([FromRoute] string conversationId, [FromBody] UpdateConversationTitleModel newTile)
{
var userService = _services.GetRequiredService<IUserService>();
var conversationService = _services.GetRequiredService<IConversationService>();

var user = await userService.GetUser(_user.Id);
var filter = new ConversationFilter
{
Id = conversationId,
UserId = user.Role != UserRole.Admin ? user.Id : null
};
var conversations = await conversationService.GetConversations(filter);

if (conversations.Items.IsNullOrEmpty())
{
return false;
}

var response = await conversationService.UpdateConversationTitle(conversationId, newTile.NewTitle);
return response != null;
}

[HttpDelete("/conversation/{conversationId}")]
public async Task<bool> DeleteConversation([FromRoute] string conversationId)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.ComponentModel.DataAnnotations;

namespace BotSharp.OpenAPI.ViewModels.Conversations;

public class UpdateConversationTitleModel
{
[Required]
public string NewTitle { get; set; }
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
using Amazon.Util.Internal;
using BotSharp.Abstraction.Conversations.Models;
using BotSharp.Abstraction.Repositories.Filters;
using MongoDB.Bson.Serialization;
using MongoDB.Driver;
using System.Collections.Immutable;

namespace BotSharp.Plugin.MongoStorage.Repository;

Expand Down Expand Up @@ -66,7 +62,7 @@ public bool DeleteConversations(IEnumerable<string> conversationIds)
var statesDeleted = _dc.ConversationStates.DeleteMany(filterSates);
var dialogDeleted = _dc.ConversationDialogs.DeleteMany(filterDialog);
var convDeleted = _dc.Conversations.DeleteMany(filterConv);

return convDeleted.DeletedCount > 0 || dialogDeleted.DeletedCount > 0 || statesDeleted.DeletedCount > 0
|| exeLogDeleted.DeletedCount > 0 || promptLogDeleted.DeletedCount > 0
|| contentLogDeleted.DeletedCount > 0 || stateLogDeleted.DeletedCount > 0;
Expand Down Expand Up @@ -237,6 +233,10 @@ public PagedItems<Conversation> GetConversations(ConversationFilter filter)
{
convFilters.Add(convBuilder.Eq(x => x.Id, filter.Id));
}
if (!string.IsNullOrEmpty(filter?.Title))
{
convFilters.Add(convBuilder.Regex(x => x.Title, new BsonRegularExpression(filter.Title, "i")));
}
if (!string.IsNullOrEmpty(filter?.AgentId))
{
convFilters.Add(convBuilder.Eq(x => x.AgentId, filter.AgentId));
Expand Down Expand Up @@ -284,6 +284,22 @@ public PagedItems<Conversation> GetConversations(ConversationFilter filter)
var filterDef = convBuilder.And(convFilters);
var sortDef = Builders<ConversationDocument>.Sort.Descending(x => x.CreatedTime);
var pager = filter?.Pager ?? new Pagination();

// Apply sorting based on sort and order fields
if (!string.IsNullOrEmpty(pager?.Sort))
{
var sortField = ConvertSnakeCaseToPascalCase(pager.Sort);

if (pager.Order == "asc")
{
sortDef = Builders<ConversationDocument>.Sort.Ascending(sortField);
}
else if (pager.Order == "desc")
{
sortDef = Builders<ConversationDocument>.Sort.Descending(sortField);
}
}

var conversationDocs = _dc.Conversations.Find(filterDef).Sort(sortDef).Skip(pager.Offset).Limit(pager.Size).ToList();
var count = _dc.Conversations.CountDocuments(filterDef);

Expand Down Expand Up @@ -364,7 +380,7 @@ public List<string> GetIdleConversations(int batchSize, int messageLimit, int bu
{
break;
}

conversationIds = conversationIds.Concat(candidates).Distinct().ToList();
if (conversationIds.Count >= batchSize)
{
Expand Down Expand Up @@ -403,7 +419,7 @@ public IEnumerable<string> TruncateConversation(string conversationId, string me

// Handle truncated dialogs
var truncatedDialogs = foundDialog.Dialogs.Where((x, idx) => idx < foundIdx).ToList();

// Handle truncated states
var refTime = foundDialog.Dialogs.ElementAt(foundIdx).MetaData.CreateTime;
var stateFilter = Builders<ConversationStateDocument>.Filter.Eq(x => x.ConversationId, conversationId);
Expand Down Expand Up @@ -441,7 +457,7 @@ public IEnumerable<string> TruncateConversation(string conversationId, string me
var truncatedBreakpoints = breakpoints.Where(x => x.CreatedTime < refTime).ToList();
foundStates.Breakpoints = truncatedBreakpoints;
}

// Update
_dc.ConversationStates.ReplaceOne(stateFilter, foundStates);
}
Expand Down Expand Up @@ -476,7 +492,25 @@ public IEnumerable<string> TruncateConversation(string conversationId, string me
_dc.ContentLogs.DeleteMany(contentLogBuilder.And(contentLogFilters));
_dc.StateLogs.DeleteMany(stateLogBuilder.And(stateLogFilters));
}

return deletedMessageIds;
}

private string ConvertSnakeCaseToPascalCase(string snakeCase)
{
string[] words = snakeCase.Split('_');
StringBuilder pascalCase = new();

foreach (string word in words)
{
if (!string.IsNullOrEmpty(word))
{
string firstLetter = word[..1].ToUpper();
string restOfWord = word[1..].ToLower();
pascalCase.Append(firstLetter + restOfWord);
}
}

return pascalCase.ToString();
}
}