Skip to content

refine conv tags #942

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

Merged
merged 3 commits into from
Mar 14, 2025
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 @@ -13,7 +13,7 @@ public interface IConversationService
Task<PagedItems<Conversation>> GetConversations(ConversationFilter filter);
Task<Conversation> UpdateConversationTitle(string id, string title);
Task<Conversation> UpdateConversationTitleAlias(string id, string titleAlias);
Task<bool> UpdateConversationTags(string conversationId, List<string> tags);
Task<bool> UpdateConversationTags(string conversationId, List<string> toAddTags, List<string> toDeleteTags);
Task<bool> UpdateConversationMessage(string conversationId, UpdateMessageRequest request);
Task<List<Conversation>> GetLastConversations();
Task<List<string>> GetIdleConversations(int batchSize, int messageLimit, int bufferHours, IEnumerable<string> excludeAgentIds);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
List<User> GetUsersByAffiliateId(string affiliateId) => throw new NotImplementedException();
User? GetUserByUserName(string userName) => throw new NotImplementedException();
void UpdateUserName(string userId, string userName) => throw new NotImplementedException();
Dashboard? GetDashboard(string id = null) => throw new NotImplementedException();

Check warning on line 46 in src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest)

Cannot convert null literal to non-nullable reference type.

Check warning on line 46 in src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest)

Cannot convert null literal to non-nullable reference type.

Check warning on line 46 in src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

Cannot convert null literal to non-nullable reference type.

Check warning on line 46 in src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

Cannot convert null literal to non-nullable reference type.

Check warning on line 46 in src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest)

Cannot convert null literal to non-nullable reference type.

Check warning on line 46 in src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest)

Cannot convert null literal to non-nullable reference type.
void CreateUser(User user) => throw new NotImplementedException();
void UpdateExistUser(string userId, User user) => throw new NotImplementedException();
void UpdateUserVerified(string userId) => throw new NotImplementedException();
Expand Down Expand Up @@ -132,7 +132,7 @@
=> throw new NotImplementedException();
void UpdateConversationTitleAlias(string conversationId, string titleAlias)
=> throw new NotImplementedException();
bool UpdateConversationTags(string conversationId, List<string> tags)
bool UpdateConversationTags(string conversationId, List<string> toAddTags, List<string> toDeleteTags)
=> throw new NotImplementedException();
bool AppendConversationTags(string conversationId, List<string> tags)
=> throw new NotImplementedException();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ public async Task<Conversation> UpdateConversationTitleAlias(string id, string t
return conversation;
}

public async Task<bool> UpdateConversationTags(string conversationId, List<string> tags)
public async Task<bool> UpdateConversationTags(string conversationId, List<string> toAddTags, List<string> toDeleteTags)
{
var db = _services.GetRequiredService<IBotSharpRepository>();
return db.UpdateConversationTags(conversationId, tags);
return db.UpdateConversationTags(conversationId, toAddTags, toDeleteTags);
}

public async Task<bool> UpdateConversationMessage(string conversationId, UpdateMessageRequest request)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ public void UpdateConversationTitleAlias(string conversationId, string titleAlia
}
}

public bool UpdateConversationTags(string conversationId, List<string> tags)
public bool UpdateConversationTags(string conversationId, List<string> toAddTags, List<string> toDeleteTags)
{
if (string.IsNullOrEmpty(conversationId)) return false;

Expand All @@ -170,7 +170,11 @@ public bool UpdateConversationTags(string conversationId, List<string> tags)

var json = File.ReadAllText(convFile);
var conv = JsonSerializer.Deserialize<Conversation>(json, _options);
conv.Tags = tags ?? new();

var tags = conv.Tags ?? [];
tags = tags.Concat(toAddTags).Distinct().ToList();
conv.Tags = tags.Where(x => !toDeleteTags.Contains(x, StringComparer.OrdinalIgnoreCase)).ToList();

conv.UpdatedTime = DateTime.UtcNow;
File.WriteAllText(convFile, JsonSerializer.Serialize(conv, _options));
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ public async Task<bool> UpdateConversationTitleAlias([FromRoute] string conversa
public async Task<bool> UpdateConversationTags([FromRoute] string conversationId, [FromBody] UpdateConversationRequest request)
{
var conv = _services.GetRequiredService<IConversationService>();
return await conv.UpdateConversationTags(conversationId, request.Tags);
return await conv.UpdateConversationTags(conversationId, request.ToAddTags, request.ToDeleteTags);
}

[HttpPut("/conversation/{conversationId}/update-message")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ namespace BotSharp.OpenAPI.ViewModels.Conversations;

public class UpdateConversationRequest
{
public List<string> Tags { get; set; } = [];
public List<string> ToAddTags { get; set; } = [];
public List<string> ToDeleteTags { get; set; } = [];
}
Original file line number Diff line number Diff line change
Expand Up @@ -134,13 +134,20 @@ public void UpdateConversationTitleAlias(string conversationId, string titleAlia
_dc.Conversations.UpdateOne(filterConv, updateConv);
}

public bool UpdateConversationTags(string conversationId, List<string> tags)
public bool UpdateConversationTags(string conversationId, List<string> toAddTags, List<string> toDeleteTags)
{
if (string.IsNullOrEmpty(conversationId)) return false;

var filter = Builders<ConversationDocument>.Filter.Eq(x => x.Id, conversationId);
var conv = _dc.Conversations.Find(filter).FirstOrDefault();
if (conv == null) return false;

var tags = conv.Tags ?? [];
tags = tags.Concat(toAddTags).Distinct().ToList();
tags = tags.Where(x => !toDeleteTags.Contains(x, StringComparer.OrdinalIgnoreCase)).ToList();

var update = Builders<ConversationDocument>.Update
.Set(x => x.Tags, tags ?? new())
.Set(x => x.Tags, tags)
.Set(x => x.UpdatedTime, DateTime.UtcNow);

var res = _dc.Conversations.UpdateOne(filter, update);
Expand Down
Loading