Merge pull request #795 from GreenShadeZhang/dev_title_alias

Add Conversation Title Alias
This commit is contained in:
Haiping 2024-12-16 22:11:49 +00:00 committed by GitHub
commit cd7fdf466d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
12 changed files with 87 additions and 0 deletions

View file

@ -12,6 +12,7 @@ public interface IConversationService
Task<Conversation> GetConversation(string id);
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> UpdateConversationMessage(string conversationId, UpdateMessageRequest request);
Task<List<Conversation>> GetLastConversations();

View file

@ -13,6 +13,7 @@ public class Conversation
/// </summary>
public string? TaskId { get; set; }
public string Title { get; set; } = string.Empty;
public string TitleAlias { get; set; } = string.Empty;
[JsonIgnore]
public List<DialogElement> Dialogs { get; set; } = new();

View file

@ -8,6 +8,7 @@ public class ConversationFilter
/// </summary>
public string? Id { get; set; }
public string? Title { get; set; }
public string? TitleAlias { get; set; }
public string? AgentId { get; set; }
public string? Status { get; set; }
public string? Channel { get; set; }

View file

@ -86,6 +86,7 @@ public interface IBotSharpRepository : IHaveServiceProvider
Conversation GetConversation(string conversationId);
PagedItems<Conversation> GetConversations(ConversationFilter filter);
void UpdateConversationTitle(string conversationId, string title);
void UpdateConversationTitleAlias(string conversationId, string titleAlias);
bool UpdateConversationTags(string conversationId, List<string> tags);
bool UpdateConversationMessage(string conversationId, UpdateMessageRequest request);
void UpdateConversationBreakpoint(string conversationId, ConversationBreakpoint breakpoint);

View file

@ -51,6 +51,14 @@ public partial class ConversationService : IConversationService
return conversation;
}
public async Task<Conversation> UpdateConversationTitleAlias(string id, string titleAlias)
{
var db = _services.GetRequiredService<IBotSharpRepository>();
db.UpdateConversationTitleAlias(id, titleAlias);
var conversation = db.GetConversation(id);
return conversation;
}
public async Task<bool> UpdateConversationTags(string conversationId, List<string> tags)
{
var db = _services.GetRequiredService<IBotSharpRepository>();

View file

@ -105,6 +105,8 @@ public class BotSharpDbContext : Database, IBotSharpRepository
public void UpdateConversationTitle(string conversationId, string title)
=> throw new NotImplementedException();
public void UpdateConversationTitleAlias(string conversationId, string titleAlias)
=> throw new NotImplementedException();
public bool UpdateConversationTags(string conversationId, List<string> tags)
=> throw new NotImplementedException();

View file

@ -134,6 +134,22 @@ namespace BotSharp.Core.Repository
}
}
}
public void UpdateConversationTitleAlias(string conversationId, string titleAlias)
{
var convDir = FindConversationDirectory(conversationId);
if (!string.IsNullOrEmpty(convDir))
{
var convFile = Path.Combine(convDir, CONVERSATION_FILE);
var content = File.ReadAllText(convFile);
var record = JsonSerializer.Deserialize<Conversation>(content, _options);
if (record != null)
{
record.TitleAlias = titleAlias;
record.UpdatedTime = DateTime.UtcNow;
File.WriteAllText(convFile, JsonSerializer.Serialize(record, _options));
}
}
}
public bool UpdateConversationTags(string conversationId, List<string> tags)
{
@ -356,6 +372,10 @@ namespace BotSharp.Core.Repository
{
matched = matched && record.Title.Contains(filter.Title);
}
if (filter?.TitleAlias != null)
{
matched = matched && record.TitleAlias.Contains(filter.TitleAlias);
}
if (filter?.AgentId != null)
{
matched = matched && record.AgentId == filter.AgentId;

View file

@ -223,6 +223,30 @@ public class ConversationController : ControllerBase
return response != null;
}
[HttpPut("/conversation/{conversationId}/update-title-alias")]
public async Task<bool> UpdateConversationTitleAlias([FromRoute] string conversationId, [FromBody] UpdateConversationTitleAliasModel 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.UpdateConversationTitleAlias(conversationId, newTile.NewTitleAlias);
return response != null;
}
[HttpPut("/conversation/{conversationId}/update-tags")]
public async Task<bool> UpdateConversationTags([FromRoute] string conversationId, [FromBody] UpdateConversationRequest request)
{

View file

@ -15,6 +15,9 @@ public class ConversationViewModel
[JsonPropertyName("title")]
public string Title { get; set; } = string.Empty;
[JsonPropertyName("title_alias")]
public string TitleAlias { get; set; } = string.Empty;
public UserViewModel User { get; set; } = new UserViewModel();
public string Event { get; set; }
@ -48,6 +51,7 @@ public class ConversationViewModel
},
AgentId = sess.AgentId,
Title = sess.Title,
TitleAlias = sess.TitleAlias,
Channel = sess.Channel,
Status = sess.Status,
TaskId = sess.TaskId,

View file

@ -0,0 +1,9 @@
using System.ComponentModel.DataAnnotations;
namespace BotSharp.OpenAPI.ViewModels.Conversations;
public class UpdateConversationTitleAliasModel
{
[Required]
public string NewTitleAlias { get; set; }
}

View file

@ -6,6 +6,7 @@ public class ConversationDocument : MongoBase
public string UserId { get; set; }
public string? TaskId { get; set; }
public string Title { get; set; }
public string TitleAlias { get; set; }
public string Channel { get; set; }
public string ChannelId { get; set; }
public string Status { get; set; }

View file

@ -114,6 +114,17 @@ public partial class MongoRepository
_dc.Conversations.UpdateOne(filterConv, updateConv);
}
public void UpdateConversationTitleAlias(string conversationId, string titleAlias)
{
if (string.IsNullOrEmpty(conversationId)) return;
var filterConv = Builders<ConversationDocument>.Filter.Eq(x => x.Id, conversationId);
var updateConv = Builders<ConversationDocument>.Update
.Set(x => x.UpdatedTime, DateTime.UtcNow)
.Set(x => x.TitleAlias, titleAlias);
_dc.Conversations.UpdateOne(filterConv, updateConv);
}
public bool UpdateConversationTags(string conversationId, List<string> tags)
{
@ -301,6 +312,10 @@ public partial class MongoRepository
{
convFilters.Add(convBuilder.Regex(x => x.Title, new BsonRegularExpression(filter.Title, "i")));
}
if (!string.IsNullOrEmpty(filter?.TitleAlias))
{
convFilters.Add(convBuilder.Regex(x => x.Title, new BsonRegularExpression(filter.TitleAlias, "i")));
}
if (!string.IsNullOrEmpty(filter?.AgentId))
{
convFilters.Add(convBuilder.Eq(x => x.AgentId, filter.AgentId));