Skip to content

Commit 933fc3f

Browse files
committed
Share one message instance through the conversation.
1 parent 492a1d7 commit 933fc3f

23 files changed

Lines changed: 117 additions & 158 deletions

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

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

34
namespace BotSharp.Abstraction.Conversations.Models;
@@ -38,6 +39,8 @@ public class RoleDialogModel : ITrackableMessage
3839
[JsonIgnore(Condition = JsonIgnoreCondition.Always)]
3940
public bool StopCompletion { get; set; }
4041

42+
public FunctionCallFromLlm Instruction { get; set; }
43+
4144
private RoleDialogModel()
4245
{
4346
}

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using BotSharp.Abstraction.Functions.Models;
2-
using BotSharp.Abstraction.Models;
32

43
namespace BotSharp.Abstraction.Routing;
54

@@ -19,5 +18,5 @@ void SetRouter(Agent router) { }
1918

2019
void SetDialogs(List<RoleDialogModel> dialogs) { }
2120

22-
Task<RoleDialogModel> Handle(IRoutingService routing, FunctionCallFromLlm inst);
21+
Task<bool> Handle(IRoutingService routing, FunctionCallFromLlm inst, RoleDialogModel message);
2322
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ public interface IRoutingService
88
void ResetRecursiveCounter();
99
void RefreshDialogs();
1010
Task<FunctionCallFromLlm> GetNextInstruction();
11-
Task<RoleDialogModel> InvokeAgent(string agentId);
12-
Task<RoleDialogModel> InstructLoop();
13-
Task<RoleDialogModel> ExecuteOnce(Agent agent);
11+
Task<bool> InvokeAgent(string agentId, RoleDialogModel message);
12+
Task<bool> InstructLoop(RoleDialogModel message);
13+
Task<bool> ExecuteOnce(Agent agent, RoleDialogModel message);
1414
}

src/Infrastructure/BotSharp.Abstraction/Routing/Models/RoutingArgs.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
namespace BotSharp.Abstraction.Routing.Models;
22

3-
public class RoutingArgs : ITrackableMessage
3+
public class RoutingArgs
44
{
5-
[JsonPropertyName("message_id")]
6-
public string MessageId { get; set; }
7-
85
[JsonPropertyName("function")]
96
public string Function { get; set; }
107

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

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace BotSharp.Core.Conversations.Services;
88
public partial class ConversationService
99
{
1010
public async Task<bool> SendMessage(string agentId,
11-
RoleDialogModel incoming,
11+
RoleDialogModel message,
1212
Func<RoleDialogModel, Task> onMessageReceived,
1313
Func<RoleDialogModel, Task> onFunctionExecuting,
1414
Func<RoleDialogModel, Task> onFunctionExecuted)
@@ -18,16 +18,16 @@ public async Task<bool> SendMessage(string agentId,
1818
var agentService = _services.GetRequiredService<IAgentService>();
1919
Agent agent = await agentService.LoadAgent(agentId);
2020

21-
var message = $"Received [{agent.Name}] {incoming.Role}: {incoming.Content}";
21+
var content = $"Received [{agent.Name}] {message.Role}: {message.Content}";
2222
#if DEBUG
23-
Console.WriteLine(message, Color.OrangeRed);
23+
Console.WriteLine(content, Color.OrangeRed);
2424
#else
25-
_logger.LogInformation(message);
25+
_logger.LogInformation(content);
2626
#endif
2727

28-
incoming.CurrentAgentId = agent.Id;
28+
message.CurrentAgentId = agent.Id;
2929

30-
_storage.Append(_conversationId, incoming);
30+
_storage.Append(_conversationId, message);
3131

3232
var hooks = _services.GetServices<IConversationHook>().ToList();
3333

@@ -37,13 +37,13 @@ public async Task<bool> SendMessage(string agentId,
3737
hook.SetAgent(agent)
3838
.SetConversation(conversation);
3939

40-
await hook.OnMessageReceived(incoming);
40+
await hook.OnMessageReceived(message);
4141

4242
// Interrupted by hook
43-
if (incoming.StopCompletion)
43+
if (message.StopCompletion)
4444
{
45-
await onMessageReceived(incoming);
46-
_storage.Append(_conversationId, incoming);
45+
await onMessageReceived(message);
46+
_storage.Append(_conversationId, message);
4747
return true;
4848
}
4949
}
@@ -52,19 +52,19 @@ public async Task<bool> SendMessage(string agentId,
5252
var routing = _services.GetRequiredService<IRoutingService>();
5353
var settings = _services.GetRequiredService<RoutingSettings>();
5454

55-
var response = agentId == settings.RouterId ?
56-
await routing.InstructLoop() :
57-
await routing.ExecuteOnce(agent);
55+
var ret = agentId == settings.RouterId ?
56+
await routing.InstructLoop(message) :
57+
await routing.ExecuteOnce(agent, message);
5858

59-
await HandleAssistantMessage(response, onMessageReceived);
59+
await HandleAssistantMessage(message, onMessageReceived);
6060

6161
var statistics = _services.GetRequiredService<ITokenStatistics>();
6262
statistics.PrintStatistics();
6363

6464
routing.ResetRecursiveCounter();
6565
routing.RefreshDialogs();
6666

67-
return true;
67+
return ret;
6868
}
6969

7070
private async Task<Conversation> GetConversationRecord(string agentId)

src/Infrastructure/BotSharp.Core/Routing/Functions/RouteToAgentFn.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,11 @@ public async Task<bool> Execute(RoleDialogModel message)
6666
else
6767
{
6868
message.CurrentAgentId = targetAgent.Id;
69-
message.Content = $"Routing to {args.AgentName}";
7069
}
7170
}
7271

7372
_context.Push(message.CurrentAgentId);
7473

75-
// Set default execution data
76-
message.Data = JsonSerializer.Deserialize<JsonElement>(message.FunctionArgs);
7774
return true;
7875
}
7976

src/Infrastructure/BotSharp.Core/Routing/Handlers/ContinueExecuteTaskRoutingHandler.cs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,15 @@ public ContinueExecuteTaskRoutingHandler(IServiceProvider services, ILogger<Cont
2626
{
2727
}
2828

29-
public async Task<RoleDialogModel> Handle(IRoutingService routing, FunctionCallFromLlm inst)
29+
public async Task<bool> Handle(IRoutingService routing, FunctionCallFromLlm inst, RoleDialogModel message)
3030
{
3131
var db = _services.GetRequiredService<IBotSharpRepository>();
3232
var record = db.GetAgents(inst.AgentName).FirstOrDefault();
3333

34-
var result = new RoleDialogModel(AgentRole.Function, inst.Question)
35-
{
36-
MessageId = inst.MessageId,
37-
FunctionName = inst.Function,
38-
FunctionArgs = JsonSerializer.Serialize(inst.Arguments),
39-
CurrentAgentId = record.Id
40-
};
34+
message.FunctionName = inst.Function;
35+
message.CurrentAgentId = record.Id;
36+
message.FunctionArgs = JsonSerializer.Serialize(inst.Arguments);
4137

42-
return result;
38+
return true;
4339
}
4440
}

src/Infrastructure/BotSharp.Core/Routing/Handlers/ConversationEndRoutingHandler.cs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,25 +24,21 @@ public ConversationEndRoutingHandler(IServiceProvider services, ILogger<Conversa
2424
{
2525
}
2626

27-
public async Task<RoleDialogModel> Handle(IRoutingService routing, FunctionCallFromLlm inst)
27+
28+
public async Task<bool> Handle(IRoutingService routing, FunctionCallFromLlm inst, RoleDialogModel message)
2829
{
29-
var result = new RoleDialogModel(AgentRole.Assistant, inst.Response)
30-
{
31-
MessageId = inst.MessageId,
32-
CurrentAgentId = _settings.RouterId,
33-
FunctionName = inst.Function,
34-
Data = inst
35-
};
30+
message.Content = inst.Response;
31+
message.FunctionName = inst.Function;
3632

3733
var hooks = _services.GetServices<IConversationHook>()
3834
.OrderBy(x => x.Priority)
3935
.ToList();
4036

4137
foreach (var hook in hooks)
4238
{
43-
await hook.OnConversationEnding(result);
39+
await hook.OnConversationEnding(message);
4440
}
4541

46-
return result;
42+
return true;
4743
}
4844
}

src/Infrastructure/BotSharp.Core/Routing/Handlers/HumanInterventionNeededHandler.cs

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ public class HumanInterventionNeededHandler : RoutingHandlerBase, IRoutingHandle
1111

1212
public string Description => "Reach out to human being, customer service or customer representative.";
1313

14-
private readonly RoutingSettings _settings;
15-
1614
public List<NameDesc> Parameters => new List<NameDesc>
1715
{
1816
new NameDesc("reason", "why need customer service"),
@@ -22,28 +20,23 @@ public class HumanInterventionNeededHandler : RoutingHandlerBase, IRoutingHandle
2220
public HumanInterventionNeededHandler(IServiceProvider services, ILogger<HumanInterventionNeededHandler> logger, RoutingSettings settings)
2321
: base(services, logger, settings)
2422
{
25-
_settings = settings;
23+
2624
}
2725

28-
public async Task<RoleDialogModel> Handle(IRoutingService routing, FunctionCallFromLlm inst)
26+
public async Task<bool> Handle(IRoutingService routing, FunctionCallFromLlm inst, RoleDialogModel message)
2927
{
30-
var result = new RoleDialogModel(AgentRole.Assistant, inst.Response)
31-
{
32-
MessageId = inst.MessageId,
33-
CurrentAgentId = _settings.RouterId,
34-
FunctionName = inst.Function,
35-
Data = inst
36-
};
28+
message.Role = AgentRole.Assistant;
29+
message.Content = inst.Response;
3730

3831
var hooks = _services.GetServices<IConversationHook>()
3932
.OrderBy(x => x.Priority)
4033
.ToList();
4134

4235
foreach (var hook in hooks)
4336
{
44-
await hook.OnHumanInterventionNeeded(result);
37+
await hook.OnHumanInterventionNeeded(message);
4538
}
4639

47-
return result;
40+
return true;
4841
}
4942
}

src/Infrastructure/BotSharp.Core/Routing/Handlers/InterruptTaskExecutionRoutingHandler.cs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,11 @@ public InterruptTaskExecutionRoutingHandler(IServiceProvider services, ILogger<I
2424
{
2525
}
2626

27-
public async Task<RoleDialogModel> Handle(IRoutingService routing, FunctionCallFromLlm inst)
27+
public async Task<bool> Handle(IRoutingService routing, FunctionCallFromLlm inst, RoleDialogModel message)
2828
{
29-
var result = new RoleDialogModel(AgentRole.User, inst.Reason)
30-
{
31-
MessageId = inst.MessageId,
32-
FunctionName = inst.Function,
33-
StopCompletion = true
34-
};
35-
36-
return result;
29+
message.FunctionName = inst.Function;
30+
message.StopCompletion = true;
31+
32+
return true;
3733
}
3834
}

0 commit comments

Comments
 (0)