Skip to content

Commit 31322b2

Browse files
committed
2 parents 30591f4 + 6ae74a4 commit 31322b2

File tree

12 files changed

+144
-17
lines changed

12 files changed

+144
-17
lines changed

src/Infrastructure/BotSharp.Abstraction/Conversations/IConversationService.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ public interface IConversationService
1212
Task<Conversation> GetConversation(string id);
1313
Task<PagedItems<Conversation>> GetConversations(ConversationFilter filter);
1414
Task<Conversation> UpdateConversationTitle(string id, string title);
15+
Task<bool> UpdateConversationMessage(string conversationId, UpdateMessageRequest request);
1516
Task<List<Conversation>> GetLastConversations();
1617
Task<List<string>> GetIdleConversations(int batchSize, int messageLimit, int bufferHours, IEnumerable<string> excludeAgentIds);
1718
Task<bool> DeleteConversations(IEnumerable<string> ids);
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace BotSharp.Abstraction.Conversations.Models;
2+
3+
public class UpdateMessageRequest
4+
{
5+
public DialogElement Message { get; set; } = null!;
6+
public int InnderIndex { get; set; }
7+
}

src/Infrastructure/BotSharp.Abstraction/Instructs/Models/InstructResult.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ public class InstructResult : ITrackableMessage
55
[JsonPropertyName("message_id")]
66
public string MessageId { get; set; }
77
public string Text { get; set; }
8-
public object Data { get; set; }
9-
public Dictionary<string, string> States { get; set; }
8+
public object? Data { get; set; }
9+
public Dictionary<string, string>? States { get; set; } = new();
1010
}

src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ public interface IBotSharpRepository
7272
Conversation GetConversation(string conversationId);
7373
PagedItems<Conversation> GetConversations(ConversationFilter filter);
7474
void UpdateConversationTitle(string conversationId, string title);
75+
bool UpdateConversationMessage(string conversationId, UpdateMessageRequest request);
7576
void UpdateConversationBreakpoint(string conversationId, ConversationBreakpoint breakpoint);
7677
ConversationBreakpoint? GetConversationBreakpoint(string conversationId);
7778
List<Conversation> GetLastConversations();

src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,13 @@ public async Task<Conversation> UpdateConversationTitle(string id, string title)
5050
var conversation = db.GetConversation(id);
5151
return conversation;
5252
}
53+
54+
public async Task<bool> UpdateConversationMessage(string conversationId, UpdateMessageRequest request)
55+
{
56+
var db = _services.GetRequiredService<IBotSharpRepository>();
57+
return db.UpdateConversationMessage(conversationId, request);
58+
}
59+
5360
public async Task<Conversation> GetConversation(string id)
5461
{
5562
var db = _services.GetRequiredService<IBotSharpRepository>();

src/Infrastructure/BotSharp.Core/Repository/BotSharpDbContext.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -156,22 +156,25 @@ public ConversationState GetConversationStates(string conversationId)
156156
=> throw new NotImplementedException();
157157

158158
public void AppendConversationDialogs(string conversationId, List<DialogElement> dialogs)
159-
=> new NotImplementedException();
159+
=> throw new NotImplementedException();
160160

161161
public void UpdateConversationTitle(string conversationId, string title)
162-
=> new NotImplementedException();
162+
=> throw new NotImplementedException();
163+
164+
public bool UpdateConversationMessage(string conversationId, UpdateMessageRequest request)
165+
=> throw new NotImplementedException();
163166

164167
public void UpdateConversationBreakpoint(string conversationId, ConversationBreakpoint breakpoint)
165-
=> new NotImplementedException();
168+
=> throw new NotImplementedException();
166169

167170
public ConversationBreakpoint? GetConversationBreakpoint(string conversationId)
168171
=> throw new NotImplementedException();
169172

170173
public void UpdateConversationStates(string conversationId, List<StateKeyValue> states)
171-
=> new NotImplementedException();
174+
=> throw new NotImplementedException();
172175

173176
public void UpdateConversationStatus(string conversationId, string status)
174-
=> new NotImplementedException();
177+
=> throw new NotImplementedException();
175178

176179
public IEnumerable<string> TruncateConversation(string conversationId, string messageId, bool cleanLog = false)
177180
=> throw new NotImplementedException();

src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.Conversation.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using BotSharp.Abstraction.Repositories.Models;
33
using System.Globalization;
44
using System.IO;
5+
using System.Xml.Linq;
56

67
namespace BotSharp.Core.Repository
78
{
@@ -133,6 +134,38 @@ public void UpdateConversationTitle(string conversationId, string title)
133134
}
134135
}
135136

137+
public bool UpdateConversationMessage(string conversationId, UpdateMessageRequest request)
138+
{
139+
if (string.IsNullOrEmpty(conversationId)) return false;
140+
141+
var dialogs = GetConversationDialogs(conversationId);
142+
var candidates = dialogs.Where(x => x.MetaData.MessageId == request.Message.MetaData.MessageId
143+
&& x.MetaData.Role == request.Message.MetaData.Role).ToList();
144+
145+
var found = candidates.Where((_, idx) => idx == request.InnderIndex).FirstOrDefault();
146+
if (found == null) return false;
147+
148+
found.Content = request.Message.Content;
149+
found.RichContent = request.Message.RichContent;
150+
151+
if (!string.IsNullOrEmpty(found.SecondaryContent))
152+
{
153+
found.SecondaryContent = request.Message.Content;
154+
}
155+
156+
if (!string.IsNullOrEmpty(found.SecondaryRichContent))
157+
{
158+
found.SecondaryRichContent = request.Message.RichContent;
159+
}
160+
161+
var convDir = FindConversationDirectory(conversationId);
162+
if (string.IsNullOrEmpty(convDir)) return false;
163+
164+
var dialogFile = Path.Combine(convDir, DIALOG_FILE);
165+
File.WriteAllText(dialogFile, JsonSerializer.Serialize(dialogs, _options));
166+
return true;
167+
}
168+
136169
public void UpdateConversationBreakpoint(string conversationId, ConversationBreakpoint breakpoint)
137170
{
138171
var convDir = FindConversationDirectory(conversationId);

src/Infrastructure/BotSharp.OpenAPI/Controllers/ConversationController.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,29 @@ public async Task<bool> UpdateConversationTitle([FromRoute] string conversationI
221221
return response != null;
222222
}
223223

224+
[HttpPut("/conversation/{conversationId}/update-message")]
225+
public async Task<bool> UpdateConversationMessage([FromRoute] string conversationId, [FromBody] UpdateMessageModel model)
226+
{
227+
var conversationService = _services.GetRequiredService<IConversationService>();
228+
var request = new UpdateMessageRequest
229+
{
230+
Message = new DialogElement
231+
{
232+
MetaData = new DialogMetaData
233+
{
234+
MessageId = model.Message.MessageId,
235+
Role = model.Message.Sender?.Role
236+
},
237+
Content = model.Message.Text,
238+
RichContent = JsonSerializer.Serialize(model.Message.RichContent, _jsonOptions),
239+
},
240+
InnderIndex = model.InnerIndex
241+
};
242+
243+
return await conversationService.UpdateConversationMessage(conversationId, request);
244+
}
245+
246+
224247
[HttpDelete("/conversation/{conversationId}")]
225248
public async Task<bool> DeleteConversation([FromRoute] string conversationId)
226249
{
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using System.Text.Json.Serialization;
2+
3+
namespace BotSharp.OpenAPI.ViewModels.Conversations;
4+
5+
public class UpdateMessageModel
6+
{
7+
[JsonPropertyName("message")]
8+
public ChatResponseModel Message { get; set; } = null!;
9+
10+
[JsonPropertyName("inner_index")]
11+
public int InnerIndex { get; set; }
12+
}

src/Infrastructure/BotSharp.OpenAPI/ViewModels/Users/UserViewModel.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ namespace BotSharp.OpenAPI.ViewModels.Users;
55

66
public class UserViewModel
77
{
8-
public string Id { get; set; } = null!;
8+
public string Id { get; set; } = string.Empty;
99
[JsonPropertyName("user_name")]
10-
public string UserName { get; set; } = null!;
10+
public string UserName { get; set; } = string.Empty;
1111
[JsonPropertyName("first_name")]
12-
public string FirstName { get; set; } = null!;
12+
public string FirstName { get; set; } = string.Empty;
1313
[JsonPropertyName("last_name")]
1414
public string? LastName { get; set; }
1515
public string? Email { get; set; }
@@ -18,7 +18,7 @@ public class UserViewModel
1818
public string Role { get; set; } = UserRole.User;
1919
[JsonPropertyName("full_name")]
2020
public string FullName => $"{FirstName} {LastName}".Trim();
21-
public string Source { get; set; }
21+
public string? Source { get; set; }
2222
[JsonPropertyName("external_id")]
2323
public string? ExternalId { get; set; }
2424
public string Avatar { get; set; } = "/user/avatar";

0 commit comments

Comments
 (0)