Skip to content

Commit e3b0a7f

Browse files
author
Jicheng Lu
committed
get channel from conversation
1 parent 096868b commit e3b0a7f

File tree

12 files changed

+88
-74
lines changed

12 files changed

+88
-74
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,15 @@ public interface IConversationService
1616
/// Send message to LLM
1717
/// </summary>
1818
/// <param name="agentId"></param>
19+
/// <param name="channel"></param>
1920
/// <param name="conversationId"></param>
2021
/// <param name="lastDalog"></param>
2122
/// <param name="onMessageReceived"></param>
2223
/// <param name="onFunctionExecuting">This delegate is useful when you want to report progress on UI</param>
2324
/// <param name="onFunctionExecuted">This delegate is useful when you want to report progress on UI</param>
2425
/// <returns></returns>
25-
Task<bool> SendMessage(string agentId,
26+
Task<bool> SendMessage(string agentId,
27+
string channel,
2628
RoleDialogModel lastDalog,
2729
Func<RoleDialogModel, Task> onMessageReceived,
2830
Func<RoleDialogModel, Task> onFunctionExecuting,

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@ 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,
13+
string channel,
1314
RoleDialogModel message,
1415
Func<RoleDialogModel, Task> onMessageReceived,
1516
Func<RoleDialogModel, Task> onFunctionExecuting,
1617
Func<RoleDialogModel, Task> onFunctionExecuted)
1718
{
18-
var conversation = await GetConversationRecord(agentId);
19+
var conversation = await GetConversationRecord(agentId, channel);
1920

2021
var agentService = _services.GetRequiredService<IAgentService>();
2122
Agent agent = await agentService.LoadAgent(agentId);
@@ -69,16 +70,17 @@ await routing.InstructLoop(message) :
6970
return true;
7071
}
7172

72-
private async Task<Conversation> GetConversationRecord(string agentId)
73+
private async Task<Conversation> GetConversationRecord(string agentId, string channel)
7374
{
7475
var converation = await GetConversation(_conversationId);
7576

76-
// Create conversation if this conversation not exists
77+
// Create conversation if this conversation does not exist
7778
if (converation == null)
7879
{
7980
var sess = new Conversation
8081
{
8182
Id = _conversationId,
83+
Channel = channel,
8284
AgentId = agentId
8385
};
8486
converation = await NewConversation(sess);

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

Lines changed: 3 additions & 1 deletion
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;
@@ -92,7 +93,8 @@ private async Task<RoleDialogModel> SendMessage(string agentId, string conversat
9293
RoleDialogModel response = default;
9394

9495
await conv.SendMessage(agentId,
95-
new RoleDialogModel("user", text),
96+
ConversationChannel.OpenAPI,
97+
new RoleDialogModel(AgentRole.User, text),
9698
async msg => response = msg,
9799
fnExecuting => Task.CompletedTask,
98100
fnExecuted => Task.CompletedTask);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -712,7 +712,7 @@ public void UpdateConversationStatus(string conversationId, string status)
712712
public Conversation GetConversation(string conversationId)
713713
{
714714
var convDir = FindConversationDirectory(conversationId);
715-
if (!string.IsNullOrEmpty(convDir)) return null;
715+
if (string.IsNullOrEmpty(convDir)) return null;
716716

717717
var convFile = Path.Combine(convDir, "conversation.json");
718718
var content = File.ReadAllText(convFile);

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,11 +143,12 @@ 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 conversation = db.GetConversation(state.GetConversationId());
147+
var channel = conversation?.Channel;
148+
var specifiedProfile = agents.FirstOrDefault(x => x.Profiles.Contains(channel));
148149
if (specifiedProfile != null)
149150
{
150-
records = records.Where(x => specifiedProfile.Profiles.Contains(name)).ToArray();
151+
records = records.Where(x => specifiedProfile.Profiles.Contains(channel)).ToArray();
151152
}
152153

153154
return records;

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using BotSharp.Abstraction.ApiAdapters;
2+
using BotSharp.Abstraction.Conversations.Enums;
23
using BotSharp.Abstraction.Conversations.Models;
34
using BotSharp.Abstraction.Models;
45
using BotSharp.OpenAPI.ViewModels.Conversations;
@@ -30,6 +31,7 @@ public async Task<ConversationViewModel> NewConversation([FromRoute] string agen
3031
var conv = new Conversation
3132
{
3233
AgentId = agentId,
34+
Channel = ConversationChannel.OpenAPI,
3335
UserId = _user.Id
3436
};
3537
conv = await service.NewConversation(conv);
@@ -97,15 +99,14 @@ public async Task<ChatResponseModel> SendMessage([FromRoute] string agentId,
9799
{
98100
var conv = _services.GetRequiredService<IConversationService>();
99101
conv.SetConversationId(conversationId, input.States);
100-
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);
102+
conv.States.SetState("provider", input.Provider)
103+
.SetState("model", input.Model)
104+
.SetState("temperature", input.Temperature)
105+
.SetState("sampling_factor", input.SamplingFactor);
105106

106107
var response = new ChatResponseModel();
107108
var inputMsg = new RoleDialogModel("user", input.Text);
108-
await conv.SendMessage(agentId, inputMsg,
109+
await conv.SendMessage(agentId, input.Channel, inputMsg,
109110
async msg =>
110111
{
111112
response.Text = msg.Content;
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
}
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
namespace BotSharp.OpenAPI.ViewModels.Instructs;
34

45
public class InstructMessageModel : IncomingMessageModel
56
{
6-
public override string Channel { get; set; } = "openapi";
7+
public override string Channel { get; set; } = ConversationChannel.OpenAPI;
78
public string? Template { get; set; }
89
}

src/Plugins/BotSharp.Plugin.ChatbotUI/ChatbotUiController.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,12 @@ public async Task SendMessage([FromBody] OpenAiMessageInput input)
7777
var conv = _services.GetRequiredService<IConversationService>();
7878
conv.SetConversationId(input.ConversationId, input.States);
7979
conv.States.SetState("provider", input.Provider)
80-
.SetState("model", input.Model)
81-
.SetState("channel", input.Channel)
82-
.SetState("temperature", input.Temperature)
83-
.SetState("sampling_factor", input.SamplingFactor);
80+
.SetState("model", input.Model)
81+
.SetState("temperature", input.Temperature)
82+
.SetState("sampling_factor", input.SamplingFactor);
8483

85-
var result = await conv.SendMessage(input.AgentId,
84+
var result = await conv.SendMessage(input.AgentId,
85+
input.Channel,
8686
message,
8787
async msg =>
8888
await OnChunkReceived(outputStream, msg),

src/Plugins/BotSharp.Plugin.ChatbotUI/ViewModels/OpenAiMessageInput.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using BotSharp.Abstraction.Conversations.Enums;
12
using BotSharp.Abstraction.Conversations.Models;
23
using System.Collections.Generic;
34
using System.Linq;
@@ -9,7 +10,7 @@ public class OpenAiMessageInput : IncomingMessageModel
910
{
1011
public string AgentId { get; set; } = string.Empty;
1112
public string ConversationId { get; set; } = string.Empty;
12-
public override string Channel { get; set; } = "webchat";
13+
public override string Channel { get; set; } = ConversationChannel.WebChat;
1314

1415
public List<OpenAiMessageBody> Messages { get; set; } = new List<OpenAiMessageBody>();
1516

0 commit comments

Comments
 (0)