Skip to content

add invoke source #1103

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ public virtual Task OnTaskCompleted(RoleDialogModel message)
public virtual Task OnHumanInterventionNeeded(RoleDialogModel message)
=> Task.CompletedTask;

public virtual Task OnFunctionExecuting(RoleDialogModel message)
public virtual Task OnFunctionExecuting(RoleDialogModel message, string from = InvokeSource.Manual)
=> Task.CompletedTask;

public virtual Task OnFunctionExecuted(RoleDialogModel message)
public virtual Task OnFunctionExecuted(RoleDialogModel message, string from = InvokeSource.Manual)
=> Task.CompletedTask;

public virtual Task OnMessageReceived(RoleDialogModel message)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,17 @@ public interface IConversationHook : IHookBase
/// Triggered before LLM calls function.
/// </summary>
/// <param name="message"></param>
/// <param name="from"></param>
/// <returns></returns>
Task OnFunctionExecuting(RoleDialogModel message);
Task OnFunctionExecuting(RoleDialogModel message, string from = InvokeSource.Manual);

/// <summary>
/// Triggered when the function calling completed.
/// </summary>
/// <param name="message"></param>
/// <param name="from"></param>
/// <returns></returns>
Task OnFunctionExecuted(RoleDialogModel message);
Task OnFunctionExecuted(RoleDialogModel message, string from = InvokeSource.Manual);

Task OnResponseGenerated(RoleDialogModel message);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
namespace BotSharp.Abstraction.Routing.Enums;

public static class InvokeSource
{
/// <summary>
/// Invoke manually
/// </summary>
public const string Manual = "manual";

/// <summary>
/// Invoke by LLM directly
/// </summary>
public const string Llm = "llm";

/// <summary>
/// Invoke by agent routing
/// </summary>
public const string Routing = "routing";
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ public interface IRoutingService
//int GetRecursiveCounter();
//void SetRecursiveCounter(int counter);

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

/// <summary>
Expand Down
1 change: 1 addition & 0 deletions src/Infrastructure/BotSharp.Abstraction/Using.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
global using BotSharp.Abstraction.Infrastructures.Enums;
global using BotSharp.Abstraction.Models;
global using BotSharp.Abstraction.Routing.Models;
global using BotSharp.Abstraction.Routing.Enums;
global using BotSharp.Abstraction.Templating;
global using BotSharp.Abstraction.Translation.Attributes;
global using BotSharp.Abstraction.Messaging.Enums;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using BotSharp.Abstraction.Routing.Enums;
using BotSharp.Abstraction.Utilities;

namespace BotSharp.Core.Realtime.Hooks;
Expand All @@ -10,7 +11,7 @@ public RealtimeConversationHook(IServiceProvider services)
_services = services;
}

public async Task OnFunctionExecuting(RoleDialogModel message)
public async Task OnFunctionExecuting(RoleDialogModel message, string from = InvokeSource.Manual)
{
var hub = _services.GetRequiredService<IRealtimeHub>();
if (hub.HubConn == null)
Expand All @@ -31,10 +32,10 @@ public async Task OnFunctionExecuting(RoleDialogModel message)
}
}

public async Task OnFunctionExecuted(RoleDialogModel message)
public async Task OnFunctionExecuted(RoleDialogModel message, string from = InvokeSource.Manual)
{
var hub = _services.GetRequiredService<IRealtimeHub>();
if (hub.HubConn == null)
if (from != InvokeSource.Llm || hub.HubConn == null)
{
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using BotSharp.Abstraction.Hooks;
using BotSharp.Abstraction.Models;
using BotSharp.Abstraction.Options;
using BotSharp.Abstraction.Routing.Enums;
using BotSharp.Core.Infrastructures;

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

await routing.InvokeFunction(message.FunctionName, message);
await routing.InvokeFunction(message.FunctionName, message, from: InvokeSource.Llm);
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using BotSharp.Abstraction.Infrastructures.Enums;
using BotSharp.Abstraction.Messaging;
using BotSharp.Abstraction.Messaging.Models.RichContent;
using BotSharp.Abstraction.Routing.Enums;
using BotSharp.Abstraction.Routing.Settings;

namespace BotSharp.Core.Conversations.Services;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ public override Task OnMessageReceived(RoleDialogModel message)
return base.OnMessageReceived(message);
}

public override Task OnFunctionExecuted(RoleDialogModel message)
public override Task OnFunctionExecuted(RoleDialogModel message, string from = InvokeSource.Manual)
{
if (Conversation != null && _convSettings.EnableExecutionLog)
{
_logger.Append(Conversation.Id, $"[{DateTime.Now}] {message.Role}: {message.FunctionName}({message.FunctionArgs}) => {message.Content}");
}
return base.OnFunctionExecuted(message);
return base.OnFunctionExecuted(message, from: from);
}

public override Task OnResponseGenerated(RoleDialogModel message)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ await HookEmitter.Emit<IRoutingHook>(_services, async hook => await hook.OnRouti
if (message.FunctionName != null)
{
var msg = RoleDialogModel.From(message, role: AgentRole.Function);
await routing.InvokeFunction(message.FunctionName, msg);
await routing.InvokeFunction(message.FunctionName, msg, from: InvokeSource.Llm);
}

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

var response = dialogs.Last();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace BotSharp.Core.Routing;

public partial class RoutingService
{
public async Task<bool> InvokeAgent(string agentId, List<RoleDialogModel> dialogs)
public async Task<bool> InvokeAgent(string agentId, List<RoleDialogModel> dialogs, string from = InvokeSource.Manual)
{
var agentService = _services.GetRequiredService<IAgentService>();
var agent = await agentService.LoadAgent(agentId);
Expand Down Expand Up @@ -46,7 +46,7 @@ public async Task<bool> InvokeAgent(string agentId, List<RoleDialogModel> dialog
message.Indication = response.Indication;
message.CurrentAgentId = agent.Id;

await InvokeFunction(message, dialogs);
await InvokeFunction(message, dialogs, from: from);
}
else
{
Expand All @@ -66,7 +66,7 @@ public async Task<bool> InvokeAgent(string agentId, List<RoleDialogModel> dialog
return true;
}

private async Task<bool> InvokeFunction(RoleDialogModel message, List<RoleDialogModel> dialogs)
private async Task<bool> InvokeFunction(RoleDialogModel message, List<RoleDialogModel> dialogs, string from)
{
// execute function
// Save states
Expand All @@ -75,7 +75,7 @@ private async Task<bool> InvokeFunction(RoleDialogModel message, List<RoleDialog

var routing = _services.GetRequiredService<IRoutingService>();
// Call functions
await routing.InvokeFunction(message.FunctionName, message);
await routing.InvokeFunction(message.FunctionName, message, from: from);

// Pass execution result to LLM to get response
if (!message.StopCompletion)
Expand All @@ -102,7 +102,7 @@ private async Task<bool> InvokeFunction(RoleDialogModel message, List<RoleDialog

// Send to Next LLM
var curAgentId = routing.Context.GetCurrentAgentId();
await InvokeAgent(curAgentId, dialogs);
await InvokeAgent(curAgentId, dialogs, from);
}
}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace BotSharp.Core.Routing;

public partial class RoutingService
{
public async Task<bool> InvokeFunction(string name, RoleDialogModel message)
public async Task<bool> InvokeFunction(string name, RoleDialogModel message, string from = InvokeSource.Manual)
{
var currentAgentId = message.CurrentAgentId;
var agentService = _services.GetRequiredService<IAgentService>();
Expand Down Expand Up @@ -36,7 +36,7 @@ public async Task<bool> InvokeFunction(string name, RoleDialogModel message)
foreach (var hook in hooks)
{
hook.SetAgent(agent);
await hook.OnFunctionExecuting(clonedMessage);
await hook.OnFunctionExecuting(clonedMessage, from: from);
}

bool result = false;
Expand All @@ -48,7 +48,7 @@ public async Task<bool> InvokeFunction(string name, RoleDialogModel message)
// After functions have been executed
foreach (var hook in hooks)
{
await hook.OnFunctionExecuted(clonedMessage);
await hook.OnFunctionExecuted(clonedMessage, from: from);
}

// Set result to original message
Expand Down
2 changes: 1 addition & 1 deletion src/Infrastructure/BotSharp.Core/Routing/RoutingService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public async Task<RoleDialogModel> InstructDirect(Agent agent, RoleDialogModel m
}
else
{
var ret = await routing.InvokeAgent(agentId, dialogs);
var ret = await routing.InvokeAgent(agentId, dialogs, from: InvokeSource.Routing);
}

var response = dialogs.Last();
Expand Down
1 change: 1 addition & 0 deletions src/Infrastructure/BotSharp.Core/Using.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
global using BotSharp.Abstraction.Repositories.Filters;
global using BotSharp.Abstraction.Roles;
global using BotSharp.Abstraction.Roles.Models;
global using BotSharp.Abstraction.Routing.Enums;
global using BotSharp.Abstraction.Routing;
global using BotSharp.Abstraction.SideCar.Attributes;
global using BotSharp.Abstraction.Statistics.Enums;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using BotSharp.Abstraction.Routing;
using BotSharp.Abstraction.Routing.Enums;

namespace BotSharp.OpenAPI.Controllers;

Expand All @@ -25,7 +26,7 @@ public async Task<string> ExecuteFunction(string agentId, string functionName, [
FunctionName = functionName,
FunctionArgs = JsonSerializer.Serialize(args)
};
await routing.InvokeFunction(functionName, message);
await routing.InvokeFunction(functionName, message, from: InvokeSource.Llm);
return message.Content;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using BotSharp.Abstraction.Conversations.Dtos;
using BotSharp.Abstraction.Routing.Enums;
using BotSharp.Abstraction.SideCar;
using BotSharp.Abstraction.Users.Dtos;
using Microsoft.AspNetCore.SignalR;
Expand Down Expand Up @@ -84,7 +85,7 @@ public override async Task OnMessageReceived(RoleDialogModel message)
await base.OnMessageReceived(message);
}

public override async Task OnFunctionExecuting(RoleDialogModel message)
public override async Task OnFunctionExecuting(RoleDialogModel message, string from = InvokeSource.Manual)
{
var conv = _services.GetRequiredService<IConversationService>();
var action = new ConversationSenderActionModel
Expand All @@ -95,7 +96,7 @@ public override async Task OnFunctionExecuting(RoleDialogModel message)
};

await GenerateSenderAction(conv.ConversationId, action);
await base.OnFunctionExecuting(message);
await base.OnFunctionExecuting(message, from: from);
}

public override async Task OnPostbackMessageReceived(RoleDialogModel message, PostbackMessageModel replyMsg)
Expand Down
5 changes: 3 additions & 2 deletions src/Plugins/BotSharp.Plugin.ChatHub/Hooks/StreamingLogHook.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using BotSharp.Abstraction.Routing.Enums;
using Microsoft.AspNetCore.SignalR;
using System.Text.Encodings.Web;
using System.Text.Unicode;
Expand Down Expand Up @@ -141,7 +142,7 @@ public async Task BeforeGenerating(Agent agent, List<RoleDialogModel> conversati
if (!_convSettings.ShowVerboseLog) return;
}

public override async Task OnFunctionExecuting(RoleDialogModel message)
public override async Task OnFunctionExecuting(RoleDialogModel message, string from = InvokeSource.Manual)
{
var conversationId = _state.GetConversationId();
if (string.IsNullOrEmpty(conversationId)) return;
Expand All @@ -164,7 +165,7 @@ public override async Task OnFunctionExecuting(RoleDialogModel message)
await SendContentLog(conversationId, input);
}

public override async Task OnFunctionExecuted(RoleDialogModel message)
public override async Task OnFunctionExecuted(RoleDialogModel message, string from = InvokeSource.Manual)
{
var conversationId = _state.GetConversationId();
if (string.IsNullOrEmpty(conversationId)) return;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
using BotSharp.Abstraction.Hooks;
using BotSharp.Abstraction.Routing;
using Task = System.Threading.Tasks.Task;
using Twilio.Rest.Api.V2010.Account;
using BotSharp.Abstraction.Routing.Enums;
using BotSharp.Plugin.Twilio.Interfaces;
using BotSharp.Plugin.Twilio.Models;
using BotSharp.Abstraction.Hooks;
using Twilio.Rest.Api.V2010.Account;
using Task = System.Threading.Tasks.Task;

namespace BotSharp.Plugin.Twilio.Hooks;

Expand All @@ -22,7 +23,7 @@ public TwilioConversationHook(IServiceProvider services,
_logger = logger;
}

public override async Task OnFunctionExecuted(RoleDialogModel message)
public override async Task OnFunctionExecuted(RoleDialogModel message, string from = InvokeSource.Manual)
{
var hooks = _services.GetHooks<ITwilioSessionHook>(message.CurrentAgentId);

Expand Down
Loading