Skip to content

Commit b7d9469

Browse files
authored
Merge pull request #219 from iceljc/features/add-conversation-filter
Features/add conversation filter
2 parents 87d2c7f + bd4ec13 commit b7d9469

File tree

17 files changed

+139
-152
lines changed

17 files changed

+139
-152
lines changed

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,12 @@ public interface IConversationService
1616
/// Send message to LLM
1717
/// </summary>
1818
/// <param name="agentId"></param>
19-
/// <param name="conversationId"></param>
2019
/// <param name="lastDalog"></param>
2120
/// <param name="onMessageReceived"></param>
2221
/// <param name="onFunctionExecuting">This delegate is useful when you want to report progress on UI</param>
2322
/// <param name="onFunctionExecuted">This delegate is useful when you want to report progress on UI</param>
2423
/// <returns></returns>
25-
Task<bool> SendMessage(string agentId,
24+
Task<bool> SendMessage(string agentId,
2625
RoleDialogModel lastDalog,
2726
Func<RoleDialogModel, Task> onMessageReceived,
2827
Func<RoleDialogModel, Task> onFunctionExecuting,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ List<Agent> GetAgents(string? name = null, bool? disabled = null, bool? allowRou
3535
void UpdateConversationStates(string conversationId, List<StateKeyValue> states);
3636
void UpdateConversationStatus(string conversationId, string status);
3737
Conversation GetConversation(string conversationId);
38-
List<Conversation> GetConversations(string userId);
38+
List<Conversation> GetConversations(string? agentId = null, string? status = null, string? channel = null, string? userId = null);
3939
void UpdateConversationTitle(string conversationId, string title);
4040
List<Conversation> GetLastConversations();
4141
void AddExectionLogs(string conversationId, List<string> logs);

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace BotSharp.Core.Conversations.Services;
99

1010
public partial class ConversationService
1111
{
12-
public async Task<bool> SendMessage(string agentId,
12+
public async Task<bool> SendMessage(string agentId,
1313
RoleDialogModel message,
1414
Func<RoleDialogModel, Task> onMessageReceived,
1515
Func<RoleDialogModel, Task> onFunctionExecuting,
@@ -73,12 +73,15 @@ private async Task<Conversation> GetConversationRecord(string agentId)
7373
{
7474
var converation = await GetConversation(_conversationId);
7575

76-
// Create conversation if this conversation not exists
76+
// Create conversation if this conversation does not exist
7777
if (converation == null)
7878
{
79+
var state = _services.GetRequiredService<IConversationStateService>();
80+
var channel = state.GetState("channel");
7981
var sess = new Conversation
8082
{
8183
Id = _conversationId,
84+
Channel = channel,
8285
AgentId = agentId
8386
};
8487
converation = await NewConversation(sess);

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ public async Task<List<Conversation>> GetConversations()
5757
{
5858
var db = _services.GetRequiredService<IBotSharpRepository>();
5959
var user = db.GetUserById(_user.Id);
60-
var conversations = db.GetConversations(user.Role == UserRole.CSR ? null : user?.Id);
60+
var targetUserId = user.Role == UserRole.CSR ? null : user?.Id;
61+
var conversations = db.GetConversations(userId: targetUserId);
6162
return conversations.OrderByDescending(x => x.CreatedTime).ToList();
6263
}
6364

src/Infrastructure/BotSharp.Core/Evaluations/EvaluatingService.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using BotSharp.Abstraction.Conversations.Enums;
12
using BotSharp.Abstraction.Evaluations;
23
using BotSharp.Abstraction.Evaluations.Models;
34
using BotSharp.Abstraction.Evaluations.Settings;
@@ -87,12 +88,15 @@ public async Task<EvaluationResult> Evaluate(string conversationId, EvaluationRe
8788
private async Task<RoleDialogModel> SendMessage(string agentId, string conversationId, string text)
8889
{
8990
var conv = _services.GetRequiredService<IConversationService>();
90-
conv.SetConversationId(conversationId, new List<string>());
91+
conv.SetConversationId(conversationId, new List<string>
92+
{
93+
$"channel={ConversationChannel.OpenAPI}"
94+
});
9195

9296
RoleDialogModel response = default;
9397

9498
await conv.SendMessage(agentId,
95-
new RoleDialogModel("user", text),
99+
new RoleDialogModel(AgentRole.User, text),
96100
async msg => response = msg,
97101
fnExecuting => Task.CompletedTask,
98102
fnExecuted => Task.CompletedTask);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ public Conversation GetConversation(string conversationId)
131131
throw new NotImplementedException();
132132
}
133133

134-
public List<Conversation> GetConversations(string userId)
134+
public List<Conversation> GetConversations(string? agentId = null, string? status = null, string? channel = null, string? userId = null)
135135
{
136136
throw new NotImplementedException();
137137
}

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

Lines changed: 28 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -128,12 +128,7 @@ private IQueryable<UserAgent> UserAgents
128128

129129
public void Add<TTableInterface>(object entity)
130130
{
131-
if (entity is Conversation conversation)
132-
{
133-
_conversations.Add(conversation);
134-
_changedTableNames.Add(nameof(Conversation));
135-
}
136-
else if (entity is Agent agent)
131+
if (entity is Agent agent)
137132
{
138133
_agents.Add(agent);
139134
_changedTableNames.Add(nameof(Agent));
@@ -159,20 +154,7 @@ public int Transaction<TTableInterface>(Action action)
159154
// Persist to disk
160155
foreach (var table in _changedTableNames)
161156
{
162-
if (table == nameof(Conversation))
163-
{
164-
foreach (var conversation in _conversations)
165-
{
166-
var dir = Path.Combine(_dbSettings.FileRepository, _conversationSettings.DataDir, conversation.Id);
167-
if (!Directory.Exists(dir))
168-
{
169-
Directory.CreateDirectory(dir);
170-
}
171-
var path = Path.Combine(dir, "conversation.json");
172-
File.WriteAllText(path, JsonSerializer.Serialize(conversation, _options));
173-
}
174-
}
175-
else if (table == nameof(Agent))
157+
if (table == nameof(Agent))
176158
{
177159
foreach (var agent in _agents)
178160
{
@@ -730,32 +712,29 @@ public void UpdateConversationStatus(string conversationId, string status)
730712
public Conversation GetConversation(string conversationId)
731713
{
732714
var convDir = FindConversationDirectory(conversationId);
733-
if (!string.IsNullOrEmpty(convDir))
734-
{
735-
var convFile = Path.Combine(convDir, "conversation.json");
736-
var content = File.ReadAllText(convFile);
737-
var record = JsonSerializer.Deserialize<Conversation>(content, _options);
715+
if (string.IsNullOrEmpty(convDir)) return null;
738716

739-
var dialogFile = Path.Combine(convDir, "dialogs.txt");
740-
if (record != null)
741-
{
742-
record.Dialogs = CollectDialogElements(dialogFile);
743-
}
717+
var convFile = Path.Combine(convDir, "conversation.json");
718+
var content = File.ReadAllText(convFile);
719+
var record = JsonSerializer.Deserialize<Conversation>(content, _options);
744720

745-
var stateFile = Path.Combine(convDir, "state.dict");
746-
if (record != null)
747-
{
748-
var states = CollectConversationStates(stateFile);
749-
record.States = new ConversationState(states);
750-
}
721+
var dialogFile = Path.Combine(convDir, "dialogs.txt");
722+
if (record != null)
723+
{
724+
record.Dialogs = CollectDialogElements(dialogFile);
725+
}
751726

752-
return record;
727+
var stateFile = Path.Combine(convDir, "state.dict");
728+
if (record != null)
729+
{
730+
var states = CollectConversationStates(stateFile);
731+
record.States = new ConversationState(states);
753732
}
754733

755-
return null;
734+
return record;
756735
}
757736

758-
public List<Conversation> GetConversations(string userId)
737+
public List<Conversation> GetConversations(string? agentId = null, string? status = null, string? channel = null, string? userId = null)
759738
{
760739
var records = new List<Conversation>();
761740
var dir = Path.Combine(_dbSettings.FileRepository, _conversationSettings.DataDir);
@@ -767,10 +746,16 @@ public List<Conversation> GetConversations(string userId)
767746

768747
var json = File.ReadAllText(path);
769748
var record = JsonSerializer.Deserialize<Conversation>(json, _options);
770-
if (record != null && (record.UserId == userId || userId == null))
771-
{
772-
records.Add(record);
773-
}
749+
if (record == null) continue;
750+
751+
var matched = true;
752+
if (!string.IsNullOrEmpty(agentId)) matched = matched && record.AgentId == agentId;
753+
if (!string.IsNullOrEmpty(status)) matched = matched && record.Status == status;
754+
if (!string.IsNullOrEmpty(channel)) matched = matched && record.Channel == channel;
755+
if (!string.IsNullOrEmpty(userId)) matched = matched && record.UserId == userId;
756+
757+
if (!matched) continue;
758+
records.Add(record);
774759
}
775760

776761
return records;

src/Infrastructure/BotSharp.Core/Routing/RoutingService.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,11 +143,11 @@ protected RoutingRule[] GetRoutingRecords()
143143

144144
// Filter agents by profile
145145
var state = _services.GetRequiredService<IConversationStateService>();
146-
var name = state.GetState("channel");
147-
var specifiedProfile = agents.FirstOrDefault(x => x.Profiles.Contains(name));
146+
var channel = state.GetState("channel");
147+
var specifiedProfile = agents.FirstOrDefault(x => x.Profiles.Contains(channel));
148148
if (specifiedProfile != null)
149149
{
150-
records = records.Where(x => specifiedProfile.Profiles.Contains(name)).ToArray();
150+
records = records.Where(x => specifiedProfile.Profiles.Contains(channel)).ToArray();
151151
}
152152

153153
return records;

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
using BotSharp.Abstraction.Agents.Enums;
12
using BotSharp.Abstraction.ApiAdapters;
3+
using BotSharp.Abstraction.Conversations.Enums;
24
using BotSharp.Abstraction.Conversations.Models;
35
using BotSharp.Abstraction.Models;
46
using BotSharp.OpenAPI.ViewModels.Conversations;
@@ -30,6 +32,7 @@ public async Task<ConversationViewModel> NewConversation([FromRoute] string agen
3032
var conv = new Conversation
3133
{
3234
AgentId = agentId,
35+
Channel = ConversationChannel.OpenAPI,
3336
UserId = _user.Id
3437
};
3538
conv = await service.NewConversation(conv);
@@ -98,13 +101,13 @@ public async Task<ChatResponseModel> SendMessage([FromRoute] string agentId,
98101
var conv = _services.GetRequiredService<IConversationService>();
99102
conv.SetConversationId(conversationId, input.States);
100103
conv.States.SetState("channel", input.Channel)
101-
.SetState("provider", input.Provider)
102-
.SetState("model", input.Model)
103-
.SetState("temperature", input.Temperature)
104-
.SetState("sampling_factor", input.SamplingFactor);
104+
.SetState("provider", input.Provider)
105+
.SetState("model", input.Model)
106+
.SetState("temperature", input.Temperature)
107+
.SetState("sampling_factor", input.SamplingFactor);
105108

106109
var response = new ChatResponseModel();
107-
var inputMsg = new RoleDialogModel("user", input.Text);
110+
var inputMsg = new RoleDialogModel(AgentRole.User, input.Text);
108111
await conv.SendMessage(agentId, inputMsg,
109112
async msg =>
110113
{
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
using BotSharp.Abstraction.Conversations.Enums;
12
using BotSharp.Abstraction.Conversations.Models;
23

34
namespace BotSharp.OpenAPI.ViewModels.Conversations;
45

56
public class NewMessageModel : IncomingMessageModel
67
{
7-
public override string Channel { get; set; } = "openapi";
8+
public override string Channel { get; set; } = ConversationChannel.OpenAPI;
89
}

0 commit comments

Comments
 (0)