Skip to content

Commit 7cf649d

Browse files
authored
Merge pull request #108 from hchen2020/master
Support rich content in UI.
2 parents 883ef65 + 7d8226c commit 7cf649d

File tree

26 files changed

+260
-53
lines changed

26 files changed

+260
-53
lines changed

src/Infrastructure/BotSharp.Abstraction/Agents/IAgentRouting.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@ namespace BotSharp.Abstraction.Agents;
22

33
public interface IAgentRouting
44
{
5+
Task<Agent> LoadRouter();
56
Task<Agent> LoadCurrentAgent();
67
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using System.Text.Json.Serialization;
2+
3+
namespace BotSharp.Abstraction.Agents.Models;
4+
5+
public class AgentRoutingArgs
6+
{
7+
[JsonPropertyName("agent_id")]
8+
public string AgentId { get; set; }
9+
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,14 @@ public interface IConversationService
2020
/// <param name="lastDalog"></param>
2121
/// <param name="onMessageReceived"></param>
2222
/// <param name="onFunctionExecuting">This delegate is useful when you want to report progress on UI</param>
23+
/// <param name="onFunctionExecuted">This delegate is useful when you want to report progress on UI</param>
2324
/// <returns></returns>
2425
Task<bool> SendMessage(string agentId,
2526
string conversationId,
2627
RoleDialogModel lastDalog,
2728
Func<RoleDialogModel, Task> onMessageReceived,
28-
Func<RoleDialogModel, Task> onFunctionExecuting);
29+
Func<RoleDialogModel, Task> onFunctionExecuting,
30+
Func<RoleDialogModel, Task> onFunctionExecuted);
2931

3032
List<RoleDialogModel> GetDialogHistory(string conversationId, int lastCount = 20);
3133
Task CleanHistory(string agentId);

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,16 @@ public class RoleDialogModel
2020
public string? FunctionArgs { get; set; }
2121

2222
/// <summary>
23-
/// Function execution result
23+
/// Function execution result, this result will be seen by LLM.
2424
/// </summary>
2525
public string? ExecutionResult { get; set; }
2626

27+
/// <summary>
28+
/// Function execution structured data, this data won't pass to LLM.
29+
/// It's ideal to render in rich content in UI.
30+
/// </summary>
31+
public object ExecutionData { get; set; }
32+
2733
public bool IsConversationEnd { get; set; }
2834

2935
public bool NeedReloadAgent { get; set; }

src/Infrastructure/BotSharp.Abstraction/Conversations/Settings/ConversationSetting.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ public class ConversationSetting
55
public string DataDir { get; set; }
66
public string ChatCompletion { get; set; }
77
public bool EnableKnowledgeBase { get; set; }
8+
public bool ShowVerboseLog { get; set; }
89
}

src/Infrastructure/BotSharp.Core/Agents/Services/AgentRouter.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using BotSharp.Abstraction.Agents;
12
using BotSharp.Abstraction.Agents.Models;
23

34
namespace BotSharp.Core.Agents.Services;
@@ -17,11 +18,18 @@ public AgentRouter(IServiceProvider services,
1718
_settings = settings;
1819
}
1920

21+
public async Task<Agent> LoadRouter()
22+
{
23+
var agentService = _services.GetRequiredService<IAgentService>();
24+
var agent = await agentService.LoadAgent(_settings.RouterId);
25+
return agent;
26+
}
27+
2028
public async Task<Agent> LoadCurrentAgent()
2129
{
2230
// Load current agent from state
2331
var state = _services.GetRequiredService<IConversationStateService>();
24-
var currentAgentId = state.GetState("agentId");
32+
var currentAgentId = state.GetState("agent_id");
2533
if (string.IsNullOrEmpty(currentAgentId))
2634
{
2735
currentAgentId = _settings.RouterId;
@@ -30,7 +38,7 @@ public async Task<Agent> LoadCurrentAgent()
3038
var agent = await agentService.LoadAgent(currentAgentId);
3139

3240
// Set agent and trigger state changed
33-
state.SetState("agentId", currentAgentId);
41+
state.SetState("agent_id", currentAgentId);
3442

3543
return agent;
3644
}

src/Infrastructure/BotSharp.Core/BotSharp.Core.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@
7373
</ItemGroup>
7474

7575
<ItemGroup>
76-
<PackageReference Include="Colorful.Console" Version="1.2.15" />
7776
<PackageReference Include="EntityFrameworkCore.BootKit" Version="6.2.1" />
7877
<PackageReference Include="Fluid.Core" Version="2.4.0" />
7978
<PackageReference Include="TensorFlow.Keras" Version="0.11.2" />

src/Infrastructure/BotSharp.Core/BotSharpServiceCollectionExtensions.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public static IServiceCollection AddBotSharp(this IServiceCollection services, I
3838
services.AddScoped<IAgentRouting, AgentRouter>();
3939

4040
services.AddScoped<IFunctionCallback, GoToRouterFn>();
41+
services.AddScoped<IFunctionCallback, RouteToAgentFn>();
4142

4243
return services;
4344
}

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

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,17 @@ private async Task<bool> GetChatCompletionsAsyncRecursively(IChatCompletion chat
1515
Agent agent,
1616
List<RoleDialogModel> wholeDialogs,
1717
Func<RoleDialogModel, Task> onMessageReceived,
18-
Func<RoleDialogModel, Task> onFunctionExecuting)
18+
Func<RoleDialogModel, Task> onFunctionExecuting,
19+
Func<RoleDialogModel, Task> onFunctionExecuted)
1920
{
2021
currentRecursiveDepth++;
2122
if (currentRecursiveDepth > maxRecursiveDepth)
2223
{
2324
_logger.LogError($"Exceed max current recursive depth.");
2425
await HandleAssistantMessage(new RoleDialogModel(AgentRole.Assistant, "System has exception, please try later.")
2526
{
26-
CurrentAgentId = agent.Id
27+
CurrentAgentId = agent.Id,
28+
Channel = wholeDialogs.Last().Channel
2729
}, onMessageReceived);
2830
return false;
2931
}
@@ -38,14 +40,15 @@ await HandleAssistantMessage(new RoleDialogModel(AgentRole.Assistant, "System ha
3840
{
3941
var preAgentId = agent.Id;
4042

41-
await HandleFunctionMessage(fn, onFunctionExecuting);
43+
await HandleFunctionMessage(fn, onFunctionExecuting, onFunctionExecuted);
4244

4345
// Function executed has exception
4446
if (fn.ExecutionResult == null)
4547
{
4648
await HandleAssistantMessage(new RoleDialogModel(AgentRole.Assistant, fn.Content)
4749
{
48-
CurrentAgentId = fn.CurrentAgentId
50+
CurrentAgentId = fn.CurrentAgentId,
51+
Channel = fn.Channel
4952
}, onMessageReceived);
5053
return;
5154
}
@@ -58,10 +61,6 @@ await HandleAssistantMessage(new RoleDialogModel(AgentRole.Assistant, fn.Content
5861
var agentSettings = _services.GetRequiredService<AgentSettings>();
5962
var agentService = _services.GetRequiredService<IAgentService>();
6063
agent = await agentService.LoadAgent(fn.CurrentAgentId);
61-
62-
// Set state to make next conversation will go to this agent directly
63-
// var state = _services.GetRequiredService<IConversationStateService>();
64-
// state.SetState("agentId", fn.CurrentAgentId);
6564
}
6665

6766
// Add to dialog history
@@ -70,7 +69,13 @@ await HandleAssistantMessage(new RoleDialogModel(AgentRole.Assistant, fn.Content
7069
// After function is executed, pass the result to LLM to get a natural response
7170
wholeDialogs.Add(fn);
7271

73-
await GetChatCompletionsAsyncRecursively(chatCompletion, conversationId, agent, wholeDialogs, onMessageReceived, onFunctionExecuting);
72+
await GetChatCompletionsAsyncRecursively(chatCompletion,
73+
conversationId,
74+
agent,
75+
wholeDialogs,
76+
onMessageReceived,
77+
onFunctionExecuting,
78+
onFunctionExecuted);
7479
});
7580

7681
return result;
@@ -89,13 +94,16 @@ private async Task HandleAssistantMessage(RoleDialogModel msg, Func<RoleDialogMo
8994
await onMessageReceived(msg);
9095
}
9196

92-
private async Task HandleFunctionMessage(RoleDialogModel msg, Func<RoleDialogModel, Task> onFunctionExecuting)
97+
private async Task HandleFunctionMessage(RoleDialogModel msg,
98+
Func<RoleDialogModel, Task> onFunctionExecuting,
99+
Func<RoleDialogModel, Task> onFunctionExecuted)
93100
{
94101
// Save states
95102
SaveStateByArgs(msg.FunctionArgs);
96103

97104
// Call functions
98105
await onFunctionExecuting(msg);
99106
await CallFunctions(msg);
107+
await onFunctionExecuted(msg);
100108
}
101109
}

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ public partial class ConversationService
88
public async Task<bool> SendMessage(string agentId, string conversationId,
99
RoleDialogModel lastDialog,
1010
Func<RoleDialogModel, Task> onMessageReceived,
11-
Func<RoleDialogModel, Task> onFunctionExecuting)
11+
Func<RoleDialogModel, Task> onFunctionExecuting,
12+
Func<RoleDialogModel, Task> onFunctionExecuted)
1213
{
1314
var converation = await GetConversation(conversationId);
1415

@@ -27,16 +28,19 @@ public async Task<bool> SendMessage(string agentId, string conversationId,
2728
var stateService = _services.GetRequiredService<IConversationStateService>();
2829
stateService.SetConversation(conversationId);
2930
stateService.Load();
31+
stateService.SetState("channel", lastDialog.Channel);
3032

3133
var router = _services.GetRequiredService<IAgentRouting>();
32-
var agent = await router.LoadCurrentAgent();
34+
var agent = await router.LoadRouter();
3335

3436
_logger.LogInformation($"[{agent.Name}] {lastDialog.Role}: {lastDialog.Content}");
3537

3638
lastDialog.CurrentAgentId = agent.Id;
37-
_storage.Append(conversationId, agent.Id, lastDialog);
38-
39+
3940
var wholeDialogs = GetDialogHistory(conversationId);
41+
wholeDialogs.Add(lastDialog);
42+
43+
_storage.Append(conversationId, agent.Id, lastDialog);
4044

4145
// Get relevant domain knowledge
4246
/*if (_settings.EnableKnowledgeBase)
@@ -67,7 +71,8 @@ public async Task<bool> SendMessage(string agentId, string conversationId,
6771
agent,
6872
wholeDialogs,
6973
onMessageReceived,
70-
onFunctionExecuting);
74+
onFunctionExecuting,
75+
onFunctionExecuted);
7176

7277
return result;
7378
}

0 commit comments

Comments
 (0)