Skip to content

Commit b29a8bc

Browse files
authored
Merge pull request #864 from iceljc/master
add routing dialogs
2 parents a8165df + 16bea29 commit b29a8bc

File tree

8 files changed

+33
-2
lines changed

8 files changed

+33
-2
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ public class ConversationContext
44
{
55
public ConversationState State { get; set; }
66
public List<DialogElement> Dialogs { get; set; } = new();
7+
public List<RoleDialogModel> RoutingDialogs { get; set; } = new();
78
public List<ConversationBreakpoint> Breakpoints { get; set; } = new();
89
public int RecursiveCounter { get; set; }
910
public Stack<string> RoutingStack { get; set; } = new();

src/Infrastructure/BotSharp.Abstraction/Routing/IRoutingContext.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,8 @@ public interface IRoutingContext
2929
Stack<string> GetAgentStack();
3030
void SetAgentStack(Stack<string> stack);
3131
void ResetAgentStack();
32+
33+
void SetDialogs(List<RoleDialogModel> dialogs);
34+
List<RoleDialogModel> GetDialogs();
35+
void ResetDialogs();
3236
}

src/Infrastructure/BotSharp.Core.SideCar/Services/BotSharpConversationSideCar.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ private void BeforeExecute(List<DialogElement>? dialogs)
124124
{
125125
State = state.GetCurrentState(),
126126
Dialogs = dialogs ?? [],
127+
RoutingDialogs = routing.Context.GetDialogs(),
127128
Breakpoints = [],
128129
RecursiveCounter = routing.Context.GetRecursiveCounter(),
129130
RoutingStack = routing.Context.GetAgentStack()
@@ -134,6 +135,7 @@ private void BeforeExecute(List<DialogElement>? dialogs)
134135
state.ResetCurrentState();
135136
routing.Context.ResetRecursiveCounter();
136137
routing.Context.ResetAgentStack();
138+
routing.Context.ResetDialogs();
137139
Utilities.ClearCache();
138140
}
139141

@@ -148,6 +150,7 @@ private void AfterExecute()
148150
state.SetCurrentState(node.State);
149151
routing.Context.SetRecursiveCounter(node.RecursiveCounter);
150152
routing.Context.SetAgentStack(node.RoutingStack);
153+
routing.Context.SetDialogs(node.RoutingDialogs);
151154
Utilities.ClearCache();
152155
enabled = false;
153156
}

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ public class RoutingContext : IRoutingContext
1010
private string _conversationId;
1111
private string _messageId;
1212
private int _currentRecursionDepth = 0;
13+
private List<RoleDialogModel> _dialogs = [];
1314

1415
public RoutingContext(IServiceProvider services, RoutingSettings setting)
1516
{
@@ -259,4 +260,19 @@ public void ResetAgentStack()
259260
{
260261
_stack.Clear();
261262
}
263+
264+
public void SetDialogs(List<RoleDialogModel> dialogs)
265+
{
266+
_dialogs = dialogs ?? [];
267+
}
268+
269+
public List<RoleDialogModel> GetDialogs()
270+
{
271+
return _dialogs ?? [];
272+
}
273+
274+
public void ResetDialogs()
275+
{
276+
_dialogs = [];
277+
}
262278
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public async Task<RoleDialogModel> InstructLoop(RoleDialogModel message, List<Ro
4141
}
4242

4343
dialogs.Add(message);
44+
Context.SetDialogs(dialogs);
4445
storage.Append(convService.ConversationId, message);
4546

4647
// Get first instruction

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ public async Task<bool> InvokeAgent(string agentId, List<RoleDialogModel> dialog
5959
message = RoleDialogModel.From(message, role: AgentRole.Assistant, content: response.Content);
6060
message.CurrentAgentId = agent.Id;
6161
dialogs.Add(message);
62+
Context.SetDialogs(dialogs);
6263
}
6364

6465
return true;
@@ -86,6 +87,7 @@ private async Task<bool> InvokeFunction(RoleDialogModel message, List<RoleDialog
8687
dialogs.Add(RoleDialogModel.From(message,
8788
role: AgentRole.Assistant,
8889
content: responseTemplate));
90+
Context.SetDialogs(dialogs);
8991
}
9092
else
9193
{
@@ -95,6 +97,7 @@ private async Task<bool> InvokeFunction(RoleDialogModel message, List<RoleDialog
9597
content: message.Content);
9698

9799
dialogs.Add(msg);
100+
Context.SetDialogs(dialogs);
98101

99102
// Send to Next LLM
100103
var agentId = routing.Context.GetCurrentAgentId();
@@ -106,6 +109,7 @@ private async Task<bool> InvokeFunction(RoleDialogModel message, List<RoleDialog
106109
dialogs.Add(RoleDialogModel.From(message,
107110
role: AgentRole.Assistant,
108111
content: message.Content));
112+
Context.SetDialogs(dialogs);
109113
}
110114

111115
return true;

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using BotSharp.Abstraction.Routing.Models;
22
using BotSharp.Abstraction.Routing.Settings;
3-
using BotSharp.Core.Infrastructures;
43

54
namespace BotSharp.Core.Routing;
65

src/Plugins/BotSharp.Plugin.FileHandler/Functions/ReadImageFn.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
using BotSharp.Abstraction.Routing;
2+
13
namespace BotSharp.Plugin.FileHandler.Functions;
24

35
public class ReadImageFn : IFunctionCallback
@@ -20,6 +22,7 @@ public async Task<bool> Execute(RoleDialogModel message)
2022
{
2123
var args = JsonSerializer.Deserialize<LlmContextIn>(message.FunctionArgs);
2224
var conv = _services.GetRequiredService<IConversationService>();
25+
var routingCtx = _services.GetRequiredService<IRoutingContext>();
2326
var agentService = _services.GetRequiredService<IAgentService>();
2427

2528
Agent? fromAgent = null;
@@ -28,7 +31,7 @@ public async Task<bool> Execute(RoleDialogModel message)
2831
fromAgent = await agentService.LoadAgent(message.CurrentAgentId);
2932
}
3033

31-
var wholeDialogs = conv.GetDialogHistory();
34+
var wholeDialogs = routingCtx.GetDialogs();
3235
var dialogs = AssembleFiles(conv.ConversationId, args?.ImageUrls, wholeDialogs);
3336
var agent = new Agent
3437
{

0 commit comments

Comments
 (0)