Skip to content

Commit a3f0f57

Browse files
authored
Merge pull request #1103 from iceljc/refine/add-invoke-source
add invoke source
2 parents d56d405 + 9a384ed commit a3f0f57

File tree

18 files changed

+61
-33
lines changed

18 files changed

+61
-33
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,10 @@ public virtual Task OnTaskCompleted(RoleDialogModel message)
4949
public virtual Task OnHumanInterventionNeeded(RoleDialogModel message)
5050
=> Task.CompletedTask;
5151

52-
public virtual Task OnFunctionExecuting(RoleDialogModel message)
52+
public virtual Task OnFunctionExecuting(RoleDialogModel message, string from = InvokeSource.Manual)
5353
=> Task.CompletedTask;
5454

55-
public virtual Task OnFunctionExecuted(RoleDialogModel message)
55+
public virtual Task OnFunctionExecuted(RoleDialogModel message, string from = InvokeSource.Manual)
5656
=> Task.CompletedTask;
5757

5858
public virtual Task OnMessageReceived(RoleDialogModel message)

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,17 @@ public interface IConversationHook : IHookBase
5959
/// Triggered before LLM calls function.
6060
/// </summary>
6161
/// <param name="message"></param>
62+
/// <param name="from"></param>
6263
/// <returns></returns>
63-
Task OnFunctionExecuting(RoleDialogModel message);
64+
Task OnFunctionExecuting(RoleDialogModel message, string from = InvokeSource.Manual);
6465

6566
/// <summary>
6667
/// Triggered when the function calling completed.
6768
/// </summary>
6869
/// <param name="message"></param>
70+
/// <param name="from"></param>
6971
/// <returns></returns>
70-
Task OnFunctionExecuted(RoleDialogModel message);
72+
Task OnFunctionExecuted(RoleDialogModel message, string from = InvokeSource.Manual);
7173

7274
Task OnResponseGenerated(RoleDialogModel message);
7375

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
namespace BotSharp.Abstraction.Routing.Enums;
2+
3+
public static class InvokeSource
4+
{
5+
/// <summary>
6+
/// Invoke manually
7+
/// </summary>
8+
public const string Manual = "manual";
9+
10+
/// <summary>
11+
/// Invoke by LLM directly
12+
/// </summary>
13+
public const string Llm = "llm";
14+
15+
/// <summary>
16+
/// Invoke by agent routing
17+
/// </summary>
18+
public const string Routing = "routing";
19+
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ public interface IRoutingService
3030
//int GetRecursiveCounter();
3131
//void SetRecursiveCounter(int counter);
3232

33-
Task<bool> InvokeAgent(string agentId, List<RoleDialogModel> dialogs);
34-
Task<bool> InvokeFunction(string name, RoleDialogModel messages);
33+
Task<bool> InvokeAgent(string agentId, List<RoleDialogModel> dialogs, string from = InvokeSource.Manual);
34+
Task<bool> InvokeFunction(string name, RoleDialogModel messages, string from = InvokeSource.Manual);
3535
Task<RoleDialogModel> InstructLoop(Agent agent, RoleDialogModel message, List<RoleDialogModel> dialogs);
3636

3737
/// <summary>

src/Infrastructure/BotSharp.Abstraction/Using.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
global using BotSharp.Abstraction.Infrastructures.Enums;
1313
global using BotSharp.Abstraction.Models;
1414
global using BotSharp.Abstraction.Routing.Models;
15+
global using BotSharp.Abstraction.Routing.Enums;
1516
global using BotSharp.Abstraction.Templating;
1617
global using BotSharp.Abstraction.Translation.Attributes;
1718
global using BotSharp.Abstraction.Messaging.Enums;

src/Infrastructure/BotSharp.Core.Realtime/Hooks/RealtimeConversationHook.cs

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

34
namespace BotSharp.Core.Realtime.Hooks;
@@ -10,7 +11,7 @@ public RealtimeConversationHook(IServiceProvider services)
1011
_services = services;
1112
}
1213

13-
public async Task OnFunctionExecuting(RoleDialogModel message)
14+
public async Task OnFunctionExecuting(RoleDialogModel message, string from = InvokeSource.Manual)
1415
{
1516
var hub = _services.GetRequiredService<IRealtimeHub>();
1617
if (hub.HubConn == null)
@@ -31,10 +32,10 @@ public async Task OnFunctionExecuting(RoleDialogModel message)
3132
}
3233
}
3334

34-
public async Task OnFunctionExecuted(RoleDialogModel message)
35+
public async Task OnFunctionExecuted(RoleDialogModel message, string from = InvokeSource.Manual)
3536
{
3637
var hub = _services.GetRequiredService<IRealtimeHub>();
37-
if (hub.HubConn == null)
38+
if (from != InvokeSource.Llm || hub.HubConn == null)
3839
{
3940
return;
4041
}

src/Infrastructure/BotSharp.Core.Realtime/Services/RealtimeHub.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using BotSharp.Abstraction.Hooks;
33
using BotSharp.Abstraction.Models;
44
using BotSharp.Abstraction.Options;
5+
using BotSharp.Abstraction.Routing.Enums;
56
using BotSharp.Core.Infrastructures;
67

78
namespace BotSharp.Core.Realtime.Services;
@@ -98,7 +99,7 @@ await HookEmitter.Emit<IRoutingHook>(_services, async hook => await hook.OnRouti
9899
agent.Id);
99100
}
100101

101-
await routing.InvokeFunction(message.FunctionName, message);
102+
await routing.InvokeFunction(message.FunctionName, message, from: InvokeSource.Llm);
102103
}
103104
else
104105
{

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
using BotSharp.Abstraction.Infrastructures.Enums;
33
using BotSharp.Abstraction.Messaging;
44
using BotSharp.Abstraction.Messaging.Models.RichContent;
5-
using BotSharp.Abstraction.Routing.Enums;
65
using BotSharp.Abstraction.Routing.Settings;
76

87
namespace BotSharp.Core.Conversations.Services;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@ public override Task OnMessageReceived(RoleDialogModel message)
2222
return base.OnMessageReceived(message);
2323
}
2424

25-
public override Task OnFunctionExecuted(RoleDialogModel message)
25+
public override Task OnFunctionExecuted(RoleDialogModel message, string from = InvokeSource.Manual)
2626
{
2727
if (Conversation != null && _convSettings.EnableExecutionLog)
2828
{
2929
_logger.Append(Conversation.Id, $"[{DateTime.Now}] {message.Role}: {message.FunctionName}({message.FunctionArgs}) => {message.Content}");
3030
}
31-
return base.OnFunctionExecuted(message);
31+
return base.OnFunctionExecuted(message, from: from);
3232
}
3333

3434
public override Task OnResponseGenerated(RoleDialogModel message)

src/Infrastructure/BotSharp.Core/Routing/Reasoning/InstructExecutor.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ await HookEmitter.Emit<IRoutingHook>(_services, async hook => await hook.OnRouti
3333
if (message.FunctionName != null)
3434
{
3535
var msg = RoleDialogModel.From(message, role: AgentRole.Function);
36-
await routing.InvokeFunction(message.FunctionName, msg);
36+
await routing.InvokeFunction(message.FunctionName, msg, from: InvokeSource.Llm);
3737
}
3838

3939
var agentId = routing.Context.GetCurrentAgentId();
@@ -57,7 +57,7 @@ await HookEmitter.Emit<IRoutingHook>(_services, async hook => await hook.OnRouti
5757
}
5858
else
5959
{
60-
var ret = await routing.InvokeAgent(agentId, dialogs);
60+
var ret = await routing.InvokeAgent(agentId, dialogs, from: InvokeSource.Routing);
6161
}
6262

6363
var response = dialogs.Last();

0 commit comments

Comments
 (0)