Skip to content

Commit bca845e

Browse files
authored
Merge pull request #509 from iceljc/features/add-file-upload-endpoint
refine message files
2 parents bfe8936 + 6b3c12f commit bca845e

File tree

8 files changed

+53
-50
lines changed

8 files changed

+53
-50
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,6 @@ Task<bool> SendMessage(string agentId,
5656
Task UpdateBreakpoint(bool resetStates = false, string? reason = null, params string[] excludedStates);
5757

5858
Task<string> GetConversationSummary(IEnumerable<string> conversationId);
59+
60+
Task<Conversation> GetConversationRecordOrCreateNew(string agentId);
5961
}

src/Infrastructure/BotSharp.Abstraction/Conversations/Models/IncomingMessageModel.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ public class IncomingMessageModel : MessageConfig
55
public string Text { get; set; } = string.Empty;
66
public virtual string Channel { get; set; } = string.Empty;
77

8+
[JsonPropertyName("input_message_id")]
9+
public string? InputMessageId { get; set; }
10+
811
/// <summary>
912
/// Postback message
1013
/// </summary>

src/Infrastructure/BotSharp.Abstraction/Conversations/Models/TruncateMessageRequest.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@ namespace BotSharp.Abstraction.Conversations.Models;
22

33
public class TruncateMessageRequest
44
{
5-
public string? TruncateMessageId { get; set; }
5+
[JsonPropertyName("is_new_message")]
6+
public bool isNewMessage { get; set; }
67
}

src/Infrastructure/BotSharp.Abstraction/Models/MessageConfig.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
namespace BotSharp.Abstraction.Models;
22

3-
public class MessageConfig : TruncateMessageRequest
3+
public class MessageConfig
44
{
55
/// <summary>
66
/// Completion Provider

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

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,7 @@ public async Task<bool> SendMessage(string agentId,
1414
Func<RoleDialogModel, Task> onFunctionExecuting,
1515
Func<RoleDialogModel, Task> onFunctionExecuted)
1616
{
17-
var conversation = await GetConversationRecord(agentId);
18-
19-
// Save message files
20-
var fileService = _services.GetRequiredService<IBotSharpFileService>();
21-
fileService.SaveMessageFiles(_conversationId, message.MessageId, FileSourceType.User, message.Files);
22-
message.Files?.Clear();
23-
17+
var conversation = await GetConversationRecordOrCreateNew(agentId);
2418
var agentService = _services.GetRequiredService<IAgentService>();
2519
Agent agent = await agentService.LoadAgent(agentId);
2620

@@ -99,27 +93,6 @@ await routing.InstructLoop(message, dialogs, onFunctionExecuting) :
9993
return true;
10094
}
10195

102-
private async Task<Conversation> GetConversationRecord(string agentId)
103-
{
104-
var converation = await GetConversation(_conversationId);
105-
106-
// Create conversation if this conversation does not exist
107-
if (converation == null)
108-
{
109-
var state = _services.GetRequiredService<IConversationStateService>();
110-
var channel = state.GetState("channel");
111-
var sess = new Conversation
112-
{
113-
Id = _conversationId,
114-
Channel = channel,
115-
AgentId = agentId
116-
};
117-
converation = await NewConversation(sess);
118-
}
119-
120-
return converation;
121-
}
122-
12396
private async Task HandleAssistantMessage(RoleDialogModel response, Func<RoleDialogModel, Task> onResponseReceived)
12497
{
12598
var agentService = _services.GetRequiredService<IAgentService>();

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,4 +140,25 @@ public void SetConversationId(string conversationId, List<MessageState> states)
140140
_state.Load(_conversationId);
141141
states.ForEach(x => _state.SetState(x.Key, x.Value, activeRounds: x.ActiveRounds, source: StateSource.External));
142142
}
143+
144+
public async Task<Conversation> GetConversationRecordOrCreateNew(string agentId)
145+
{
146+
var converation = await GetConversation(_conversationId);
147+
148+
// Create conversation if this conversation does not exist
149+
if (converation == null)
150+
{
151+
var state = _services.GetRequiredService<IConversationStateService>();
152+
var channel = state.GetState("channel");
153+
var sess = new Conversation
154+
{
155+
Id = _conversationId,
156+
Channel = channel,
157+
AgentId = agentId
158+
};
159+
converation = await NewConversation(sess);
160+
}
161+
162+
return converation;
163+
}
143164
}

src/Infrastructure/BotSharp.Core/Files/BotSharpFileService.Conversation.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,6 @@ public bool SaveMessageFiles(string conversationId, string messageId, string sou
230230
using var fs = new FileStream(Path.Combine(subDir, file.FileName), FileMode.Create);
231231
fs.Write(bytes, 0, bytes.Length);
232232
fs.Flush(true);
233-
Thread.Sleep(2000);
234233
}
235234

236235
return true;

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

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using BotSharp.Abstraction.Files.Enums;
12
using BotSharp.Abstraction.Options;
23
using BotSharp.Abstraction.Routing;
34
using BotSharp.Abstraction.Users.Enums;
@@ -142,7 +143,7 @@ public async Task<IEnumerable<ChatResponseModel>> GetDialogs([FromRoute] string
142143
{
143144
return null;
144145
}
145-
146+
146147
var result = ConversationViewModel.FromSession(conversations.Items.First());
147148
var state = _services.GetRequiredService<IConversationStateService>();
148149
result.States = state.Load(conversationId, isReadOnly: true);
@@ -211,11 +212,12 @@ public async Task<bool> DeleteConversation([FromRoute] string conversationId)
211212
}
212213

213214
[HttpDelete("/conversation/{conversationId}/message/{messageId}")]
214-
public async Task<bool> DeleteConversationMessage([FromRoute] string conversationId, [FromRoute] string messageId)
215+
public async Task<string?> DeleteConversationMessage([FromRoute] string conversationId, [FromRoute] string messageId, [FromBody] TruncateMessageRequest request)
215216
{
216217
var conversationService = _services.GetRequiredService<IConversationService>();
217-
var response = await conversationService.TruncateConversation(conversationId, messageId);
218-
return response;
218+
var newMessageId = request.isNewMessage ? Guid.NewGuid().ToString() : null;
219+
var isSuccess = await conversationService.TruncateConversation(conversationId, messageId, newMessageId);
220+
return isSuccess ? newMessageId : string.Empty;
219221
}
220222

221223
#region Send message
@@ -227,23 +229,18 @@ public async Task<ChatResponseModel> SendMessage([FromRoute] string agentId,
227229
var conv = _services.GetRequiredService<IConversationService>();
228230
var inputMsg = new RoleDialogModel(AgentRole.User, input.Text)
229231
{
230-
Files = input.Files,
232+
MessageId = !string.IsNullOrWhiteSpace(input.InputMessageId) ? input.InputMessageId : Guid.NewGuid().ToString(),
231233
CreatedAt = DateTime.UtcNow
232234
};
233235

234-
if (!string.IsNullOrEmpty(input.TruncateMessageId))
235-
{
236-
await conv.TruncateConversation(conversationId, input.TruncateMessageId, inputMsg.MessageId);
237-
}
238-
239236
var routing = _services.GetRequiredService<IRoutingService>();
240237
routing.Context.SetMessageId(conversationId, inputMsg.MessageId);
241238

242239
conv.SetConversationId(conversationId, input.States);
243240
SetStates(conv, input);
244241

245242
var response = new ChatResponseModel();
246-
243+
247244
await conv.SendMessage(agentId, inputMsg,
248245
replyMessage: input.Postback,
249246
async msg =>
@@ -273,15 +270,10 @@ public async Task SendMessageSse([FromRoute] string agentId,
273270
var conv = _services.GetRequiredService<IConversationService>();
274271
var inputMsg = new RoleDialogModel(AgentRole.User, input.Text)
275272
{
276-
Files = input.Files,
273+
MessageId = !string.IsNullOrWhiteSpace(input.InputMessageId) ? input.InputMessageId : Guid.NewGuid().ToString(),
277274
CreatedAt = DateTime.UtcNow
278275
};
279276

280-
if (!string.IsNullOrEmpty(input.TruncateMessageId))
281-
{
282-
await conv.TruncateConversation(conversationId, input.TruncateMessageId, inputMsg.MessageId);
283-
}
284-
285277
var state = _services.GetRequiredService<IConversationStateService>();
286278

287279
var routing = _services.GetRequiredService<IRoutingService>();
@@ -322,7 +314,7 @@ await conv.SendMessage(agentId, inputMsg,
322314
{
323315
ConversationId = conversationId,
324316
MessageId = msg.MessageId,
325-
Text = msg.Indication,
317+
Text = msg.Indication,
326318
Function = "indicating",
327319
Instruction = msg.Instruction,
328320
States = new Dictionary<string, string>()
@@ -370,8 +362,20 @@ public IActionResult UploadAttachments([FromRoute] string conversationId,
370362
return BadRequest(new { message = "Invalid file." });
371363
}
372364

365+
[HttpPost("/agent/{agentId}/conversation/{conversationId}/upload")]
366+
public async Task<string> UploadConversationMessageFiles([FromRoute] string agentId, [FromRoute] string conversationId, [FromBody] NewMessageModel input)
367+
{
368+
var convService = _services.GetRequiredService<IConversationService>();
369+
convService.SetConversationId(conversationId, input.States);
370+
var conv = await convService.GetConversationRecordOrCreateNew(agentId);
371+
var fileService = _services.GetRequiredService<IBotSharpFileService>();
372+
var messageId = Guid.NewGuid().ToString();
373+
var isSaved = fileService.SaveMessageFiles(conv.Id, messageId, FileSourceType.User, input.Files);
374+
return isSaved ? messageId : string.Empty;
375+
}
376+
373377
[HttpGet("/conversation/{conversationId}/files/{messageId}/{source}")]
374-
public IEnumerable<MessageFileViewModel> GetMessageFiles([FromRoute] string conversationId, [FromRoute] string messageId, [FromRoute] string source)
378+
public IEnumerable<MessageFileViewModel> GetConversationMessageFiles([FromRoute] string conversationId, [FromRoute] string messageId, [FromRoute] string source)
375379
{
376380
var fileService = _services.GetRequiredService<IBotSharpFileService>();
377381
var files = fileService.GetMessageFiles(conversationId, new List<string> { messageId }, source, imageOnly: false);

0 commit comments

Comments
 (0)