Merge branch 'master' of https://github.com/Ai4c-AI/BotSharp
11
BotSharp.sln
|
|
@ -121,6 +121,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BotSharp.Core.SideCar", "sr
|
|||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BotSharp.Plugin.VertexAI", "src\Plugins\BotSharp.Plugin.LangChain\BotSharp.Plugin.VertexAI.csproj", "{7DA2DCD0-551B-432E-AA5C-22DDD3ED459B}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BotSharp.Core.Crontab", "src\Infrastructure\BotSharp.Core.Crontab\BotSharp.Core.Crontab.csproj", "{F812BAAE-5A7D-4DF7-8E71-70696B51C61F}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
|
|
@ -489,6 +491,14 @@ Global
|
|||
{7DA2DCD0-551B-432E-AA5C-22DDD3ED459B}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{7DA2DCD0-551B-432E-AA5C-22DDD3ED459B}.Release|x64.ActiveCfg = Release|Any CPU
|
||||
{7DA2DCD0-551B-432E-AA5C-22DDD3ED459B}.Release|x64.Build.0 = Release|Any CPU
|
||||
{F812BAAE-5A7D-4DF7-8E71-70696B51C61F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{F812BAAE-5A7D-4DF7-8E71-70696B51C61F}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{F812BAAE-5A7D-4DF7-8E71-70696B51C61F}.Debug|x64.ActiveCfg = Debug|Any CPU
|
||||
{F812BAAE-5A7D-4DF7-8E71-70696B51C61F}.Debug|x64.Build.0 = Debug|Any CPU
|
||||
{F812BAAE-5A7D-4DF7-8E71-70696B51C61F}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{F812BAAE-5A7D-4DF7-8E71-70696B51C61F}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{F812BAAE-5A7D-4DF7-8E71-70696B51C61F}.Release|x64.ActiveCfg = Release|Any CPU
|
||||
{F812BAAE-5A7D-4DF7-8E71-70696B51C61F}.Release|x64.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
|
|
@ -547,6 +557,7 @@ Global
|
|||
{F57F4862-F8D4-44A1-AC12-5C131B5C9785} = {51AFE054-AE99-497D-A593-69BAEFB5106F}
|
||||
{6D3A54F9-4792-41DB-BE7D-4F7B1D918EAE} = {E29DC6C4-5E57-48C5-BCB0-6B8F84782749}
|
||||
{7DA2DCD0-551B-432E-AA5C-22DDD3ED459B} = {D5293208-2BEF-42FC-A64C-5954F61720BA}
|
||||
{F812BAAE-5A7D-4DF7-8E71-70696B51C61F} = {E29DC6C4-5E57-48C5-BCB0-6B8F84782749}
|
||||
EndGlobalSection
|
||||
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||
SolutionGuid = {A9969D89-C98B-40A5-A12B-FC87E55B3A19}
|
||||
|
|
|
|||
459
docs/architecture/agent-utility.md
Normal file
|
|
@ -0,0 +1,459 @@
|
|||
# Agent Utility
|
||||
|
||||
## Introduction
|
||||
This document aims to introduce the agent utility concept and provide an instruction on adding custom agent utilities in AI project “BotSharp”. We will start by explaining the mechanism of agent utility in [Section 2](#agent-utility). Then, we illustrate the custom agent utility setup and integration in [Section 3](#agent-utility-setup) and [Section 4](#agent-utility-integration), respectively. A use case will be demonstrated in [Section 5](#use-case-demo). We wrap up the document with a short summary.
|
||||
|
||||
## Agent Utility
|
||||
The agent utility is a unique feature that can be integrated into agent to enhance its capability. Its core principle is that it can dynamically and seamlessly add extra prompts and add task-oriented functions (or tools) during conversation, without disrupting the agent’s primary purpose. In other words, the agent utility can perform extra tasks based on the context of conversation. Typical examples of agent utility include reading images/pdf, generating image, and sending http requests. [Fig 2.1.1](#agent-utility-example) demonstrates an example of these utilities. In this example, “Chatbot” is a simple agent to answer user questions, and the utilities extend its capability to explain image content, generate requested image, and send a specific http request.
|
||||
|
||||
<div id="agent-utility-example">
|
||||
<img src="assets/agent-utility/agent-utility-example.png" style="display: block; margin: auto;" />
|
||||
</div>
|
||||
<br />
|
||||
|
||||
## Agent Utility Setup
|
||||
In this section, we outline the steps to set up a custom agent utility. We start with the basic code structure and then add essential utility data, such as prompts and functions. The utility hooks are used to incorporate the utility into the agent. Finally, we give a brief overview of the utility implementation.
|
||||
|
||||
### Basic Code Structure
|
||||
The basic code structure of a typical agent utility includes prompt/function data, hooks, and function implementation. We can add specific utility prompts and functions in different projects. Note that the agent **“6745151e-6d46-4a02-8de4-1c4f21c7da95”** is considered as a dedicated utility assistant, and every prompt and function can be optionally used as a utility. [Fig 3.1.1](#agent-utility-code-structure) presents the structure of prompt, function, hooks, and implementation of an http utility.
|
||||
|
||||
<div id="agent-utility-code-structure">
|
||||
<img src="assets/agent-utility/agent-utility-code-structure.png" style="display: block; margin: auto;" />
|
||||
</div>
|
||||
<br />
|
||||
|
||||
### Utility Data
|
||||
For a typical agent utility, it is essential to add at least one prompt and one function. The utility name is formatted as **“[plugin name].[utility name]”**, such as “http.http-handler”. The prompt is added under the “templates” folder and its recommended name is **“util-[plugin name]-[function name].fn.liquid”**, while the function is added under the “functions” folder and its recommended name is **“util-[plugin name]-[function name].json”**. Once we compile the project, we can find the aggregated utility assistant folder at location: **“\BotSharp\src\WebStarter\bin\Debug\net8.0\data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95”**.
|
||||
|
||||
### Utility Hooks
|
||||
The utility hooks are used to connect the agent utility to the agent system. The code snippet below demonstrates an implementation of the http utility hook, where we define the utility name, prompts and functions. Note that each utility can be used across different agents.
|
||||
|
||||
```csharp
|
||||
public class HttpHandlerPlugin : IBotSharpPlugin
|
||||
{
|
||||
public string Id => "2c1eb1c4-16e5-4c65-8ee4-032324c26b81";
|
||||
public string Name => "HTTP Handler";
|
||||
public string Description => "Empower agent to handle HTTP request in RESTful API or GraphQL";
|
||||
public string IconUrl => "https://lirp.cdn-website.com/6f8d6d8a/dms3rep/multi/opt/API_Icon-640w.png";
|
||||
public string[] AgentIds => new[] { "87c458fc-ec5f-40ae-8ed6-05dda8a07523" };
|
||||
|
||||
public void RegisterDI(IServiceCollection services, IConfiguration config)
|
||||
{
|
||||
services.AddScoped(provider =>
|
||||
{
|
||||
var settingService = provider.GetRequiredService<ISettingService>();
|
||||
return settingService.Bind<HttpHandlerSettings>("HttpHandler");
|
||||
});
|
||||
|
||||
services.AddScoped<IAgentUtilityHook, HttpHandlerUtilityHook>();
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
The agent hook is used to append the utility prompt and function during the conversation. **Note that the utility data is only allowed to be included in the context of conversation**. The utility mechanism is implemented in the **“OnAgentUtilityLoaded”** hook, and it is invoked when we load any agent.
|
||||
|
||||
```csharp
|
||||
public async Task<Agent> LoadAgent(string id)
|
||||
{
|
||||
if (string.IsNullOrEmpty(id) || id == Guid.Empty.ToString())
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var hooks = _services.GetServices<IAgentHook>();
|
||||
|
||||
// Before agent is loaded.
|
||||
foreach (var hook in hooks)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(hook.SelfId) && hook.SelfId != id)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
hook.OnAgentLoading(ref id);
|
||||
}
|
||||
|
||||
var agent = await GetAgent(id);
|
||||
if (agent == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
await InheritAgent(agent);
|
||||
OverrideInstructionByChannel(agent);
|
||||
AddOrUpdateParameters(agent);
|
||||
|
||||
// Populate state into dictionary
|
||||
agent.TemplateDict = new Dictionary<string, object>();
|
||||
PopulateState(agent.TemplateDict);
|
||||
|
||||
// After agent is loaded
|
||||
foreach (var hook in hooks)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(hook.SelfId) && hook.SelfId != id)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
hook.SetAget(agent);
|
||||
|
||||
if (!string.IsNullOrEmpty(agent.Instruction))
|
||||
{
|
||||
hook.OnInstructionLoaded(agent.Instruction, agent.TemplateDict);
|
||||
}
|
||||
|
||||
if (agent.Functions != null)
|
||||
{
|
||||
hook.OnFunctionsLoaded(agent.Functions);
|
||||
}
|
||||
|
||||
if (agent.Samples != null)
|
||||
{
|
||||
hook.OnSamplesLoaded(agent.Samples);
|
||||
}
|
||||
|
||||
hook.OnAgentUtilityLoaded(agent);
|
||||
hook.OnAgentLoaded(agent);
|
||||
}
|
||||
|
||||
_logger.LogInformation($"Loaded agent {agent}.");
|
||||
|
||||
return agent;
|
||||
}
|
||||
```
|
||||
|
||||
### Utility Function Implementation
|
||||
Here we introduce a simple utility function implementation. The actual content depends on what task you want this utility to fulfill. The code snippet below illustrates the implementation of the “handle http request” function. Note that the property “Name” must be consistent with the function added in the utility data. The “Indication” is an optional property whose content will be displayed in the chat while the user is waiting for the assistant response.
|
||||
|
||||
```csharp
|
||||
public class HandleHttpRequestFn : IFunctionCallback
|
||||
{
|
||||
public string Name => "util-http-handle_http_request";
|
||||
public string Indication => "Handling http request";
|
||||
|
||||
private readonly IServiceProvider _services;
|
||||
private readonly ILogger<HandleHttpRequestFn> _logger;
|
||||
private readonly IHttpClientFactory _httpClientFactory;
|
||||
private readonly IHttpContextAccessor _context;
|
||||
private readonly BotSharpOptions _options;
|
||||
|
||||
public HandleHttpRequestFn(IServiceProvider services,
|
||||
ILogger<HandleHttpRequestFn> logger,
|
||||
IHttpClientFactory httpClientFactory,
|
||||
IHttpContextAccessor context,
|
||||
BotSharpOptions options)
|
||||
{
|
||||
_services = services;
|
||||
_logger = logger;
|
||||
_httpClientFactory = httpClientFactory;
|
||||
_context = context;
|
||||
_options = options;
|
||||
}
|
||||
|
||||
public async Task<bool> Execute(RoleDialogModel message)
|
||||
{
|
||||
var args = JsonSerializer.Deserialize<LlmContextIn>(message.FunctionArgs, _options.JsonSerializerOptions);
|
||||
var url = args?.RequestUrl;
|
||||
var method = args?.HttpMethod;
|
||||
var content = args?.RequestContent;
|
||||
|
||||
try
|
||||
{
|
||||
var response = await SendHttpRequest(url, method, content);
|
||||
var responseContent = await HandleHttpResponse(response);
|
||||
message.Content = responseContent;
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
var msg = $"Fail when sending http request. Url: {url}, method: {method}, content: {content}";
|
||||
_logger.LogWarning($"{msg}\n(Error: {ex.Message})");
|
||||
message.Content = msg;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private async Task<HttpResponseMessage?> SendHttpRequest(string? url, string? method, string? content)
|
||||
{
|
||||
if (string.IsNullOrEmpty(url)) return null;
|
||||
|
||||
using var client = _httpClientFactory.CreateClient();
|
||||
AddRequestHeaders(client);
|
||||
|
||||
var (uri, request) = BuildHttpRequest(url, method, content);
|
||||
var response = await client.SendAsync(request);
|
||||
if (response == null || !response.IsSuccessStatusCode)
|
||||
{
|
||||
_logger.LogWarning($"Response status code: {response?.StatusCode}");
|
||||
}
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
private void AddRequestHeaders(HttpClient client)
|
||||
{
|
||||
client.DefaultRequestHeaders.Add("Authorization", $"{_context.HttpContext.Request.Headers["Authorization"]}");
|
||||
|
||||
var settings = _services.GetRequiredService<HttpHandlerSettings>();
|
||||
var origin = !string.IsNullOrEmpty(settings.Origin) ? settings.Origin : $"{_context.HttpContext.Request.Headers["Origin"]}";
|
||||
if (!string.IsNullOrEmpty(origin))
|
||||
{
|
||||
client.DefaultRequestHeaders.Add("Origin", origin);
|
||||
}
|
||||
}
|
||||
|
||||
private (Uri, HttpRequestMessage) BuildHttpRequest(string url, string? method, string? content)
|
||||
{
|
||||
var httpMethod = GetHttpMethod(method);
|
||||
StringContent httpContent;
|
||||
|
||||
var requestUrl = url;
|
||||
if (httpMethod == HttpMethod.Get)
|
||||
{
|
||||
httpContent = BuildHttpContent("{}");
|
||||
requestUrl = BuildQuery(url, content);
|
||||
}
|
||||
else
|
||||
{
|
||||
httpContent = BuildHttpContent(content);
|
||||
}
|
||||
|
||||
if (!Uri.TryCreate(requestUrl, UriKind.Absolute, out var uri))
|
||||
{
|
||||
var settings = _services.GetRequiredService<HttpHandlerSettings>();
|
||||
var baseUri = new Uri(settings.BaseAddress);
|
||||
uri = new Uri(baseUri, requestUrl);
|
||||
}
|
||||
|
||||
return (uri, new HttpRequestMessage
|
||||
{
|
||||
RequestUri = uri,
|
||||
Method = httpMethod,
|
||||
Content = httpContent
|
||||
});
|
||||
}
|
||||
|
||||
private HttpMethod GetHttpMethod(string? method)
|
||||
{
|
||||
var localMethod = method?.Trim()?.ToUpper();
|
||||
HttpMethod matchMethod;
|
||||
|
||||
switch (localMethod)
|
||||
{
|
||||
case "GET":
|
||||
matchMethod = HttpMethod.Get;
|
||||
break;
|
||||
case "DELETE":
|
||||
matchMethod = HttpMethod.Delete;
|
||||
break;
|
||||
case "PUT":
|
||||
matchMethod = HttpMethod.Put;
|
||||
break;
|
||||
case "Patch":
|
||||
matchMethod = HttpMethod.Patch;
|
||||
break;
|
||||
default:
|
||||
matchMethod = HttpMethod.Post;
|
||||
break;
|
||||
}
|
||||
return matchMethod;
|
||||
}
|
||||
|
||||
private StringContent BuildHttpContent(string? content)
|
||||
{
|
||||
var str = string.Empty;
|
||||
try
|
||||
{
|
||||
var json = JsonSerializer.Deserialize<JsonDocument>(content ?? "{}", _options.JsonSerializerOptions);
|
||||
str = JsonSerializer.Serialize(json, _options.JsonSerializerOptions);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogWarning($"Error when build http content: {content}\n(Error: {ex.Message})");
|
||||
}
|
||||
|
||||
return new StringContent(str, Encoding.UTF8, MediaTypeNames.Application.Json);
|
||||
}
|
||||
|
||||
private string BuildQuery(string url, string? content)
|
||||
{
|
||||
if (string.IsNullOrEmpty(url) || string.IsNullOrEmpty(content)) return url;
|
||||
|
||||
try
|
||||
{
|
||||
var queries = new List<string>();
|
||||
var json = JsonSerializer.Deserialize<JsonDocument>(content, _options.JsonSerializerOptions);
|
||||
var root = json.RootElement;
|
||||
foreach (var prop in root.EnumerateObject())
|
||||
{
|
||||
var name = prop.Name.Trim();
|
||||
var value = prop.Value.ToString().Trim();
|
||||
if (string.IsNullOrEmpty(name) || string.IsNullOrEmpty(value))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
queries.Add($"{name}={value}");
|
||||
}
|
||||
|
||||
if (!queries.IsNullOrEmpty())
|
||||
{
|
||||
url += $"?{string.Join('&', queries)}";
|
||||
}
|
||||
return url;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogWarning($"Error when building url query. Url: {url}, Content: {content}\n(Error: {ex.Message})");
|
||||
return url;
|
||||
}
|
||||
}
|
||||
|
||||
private async Task<string> HandleHttpResponse(HttpResponseMessage? response)
|
||||
{
|
||||
if (response == null) return string.Empty;
|
||||
|
||||
return await response.Content.ReadAsStringAsync();
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Utility Inheritance
|
||||
Here we introduce a new feature: utility inheritance. As we are using the routing-based multi-agent architecture, different agents may come into the call stack while we are handling user requests. With the utility inheritance, the agent can not only use the utilities added to itself but also inherit the utilities from the entry agent, which is the first agent that comes into the call stack, such as a router agent. [Fig 3.5.1](#routing-architecture) illustrates a typical example of routing-based multi-agent architecture, where “Pizza Bot” is the router and "Order Inquery", "Ordering", "Payment" are task agents. When the user starts chatting, “Pizza Bot” first comes into the call stack, with "Order Inquery", "Ordering", or "Payment" joining next depending on what the user actually requests. For example, if we allow "Order Inquery" to inherit utilities, all the utilities from itself as well as “Pizza Bot” will be invoked once the "Order Inquery" agent is in action.
|
||||
|
||||
<div id="routing-architecture">
|
||||
<img src="assets/agent-utility/routing-arch.png" style="display: block; margin: auto;" />
|
||||
</div>
|
||||
<br />
|
||||
|
||||
### Agent Setup
|
||||
Here we introduce the agent setup with utilities and inheritance. As we introduced in [Section 3.3](#utility-hooks), each utility of an agent is structured with utility name, functions, and prompts. [Fig 3.6.1](#router-utility-ui) and [Fig 3.6.2](#task-agent-utility-ui) presents the utility configuration and utility inheritance of “Pizza Bot” and “Order Inquery”, respectively. As is displayed, we can apply the utility configuration and inheritance via agent files or agent detail ui. Note that we can uncheck the box to disable a utility ([Fig 3.6.2](#router-utility-ui)).
|
||||
|
||||
```json
|
||||
{
|
||||
"id": "8970b1e5-d260-4e2c-90b1-f1415a257c18",
|
||||
"name": "Pizza Bot",
|
||||
"description": "AI assistant that can help customer place pizza order.",
|
||||
"type": "routing",
|
||||
"inheritAgentId": "01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a",
|
||||
"createdDateTime": "2023-08-18T10:39:32.2349685Z",
|
||||
"updatedDateTime": "2023-08-18T14:39:32.2349686Z",
|
||||
"iconUrl": "https://cdn-icons-png.flaticon.com/512/6978/6978255.png",
|
||||
"disabled": false,
|
||||
"isPublic": true,
|
||||
"profiles": [ "pizza" ],
|
||||
"utilities": [
|
||||
{
|
||||
"name": "http.http-handler",
|
||||
"functions": [
|
||||
{ "name": "util-http-handle_http_request" }
|
||||
],
|
||||
"templates": [
|
||||
{ "name": "util-http-handle_http_request.fn" }
|
||||
]
|
||||
}
|
||||
],
|
||||
"routingRules": [
|
||||
{
|
||||
"type": "reasoner",
|
||||
"field": "NaiveReasoner"
|
||||
}
|
||||
]
|
||||
}
|
||||
```
|
||||
<br />
|
||||
|
||||
|
||||
<div id="router-utility-ui">
|
||||
<img src="assets/agent-utility/router-utility-ui.png" style="display: block; margin: auto;" />
|
||||
</div>
|
||||
<br />
|
||||
<br />
|
||||
<br />
|
||||
|
||||
```json
|
||||
{
|
||||
"name": "Order Inquiry",
|
||||
"description": "Check the order status like payment, delivery or baking.",
|
||||
"createdDateTime": "2023-08-18T14:39:32.2349685Z",
|
||||
"updatedDateTime": "2023-08-18T14:39:32.2349686Z",
|
||||
"id": "b284db86-e9c2-4c25-a59e-4649797dd130",
|
||||
"disabled": false,
|
||||
"isPublic": true,
|
||||
"mergeUtility": true,
|
||||
"profiles": [ "pizza" ]
|
||||
}
|
||||
```
|
||||
<br />
|
||||
|
||||
<div id="task-agent-utility-ui">
|
||||
<img src="assets/agent-utility/task-agent-utility-ui.png" style="display: block; margin: auto;" />
|
||||
</div>
|
||||
<br />
|
||||
|
||||
|
||||
## Agent Utility Integration
|
||||
In this section, we outline the steps to integrate a custom agent utility, including registering plugin, registering assembly, and adding project reference.
|
||||
|
||||
The code snippet below presents the “Http Handler Plugin” in the “BotSharp.Plugin.HttpHandler”, where we can register the hooks and other essential settings. Note that there is no need to register the function here, since it is automatically registered on the application level.
|
||||
|
||||
```csharp
|
||||
public class HttpHandlerPlugin : IBotSharpPlugin
|
||||
{
|
||||
public string Id => "2c1eb1c4-16e5-4c65-8ee4-032324c26b81";
|
||||
public string Name => "HTTP Handler";
|
||||
public string Description => "Empower agent to handle HTTP request in RESTful API or GraphQL";
|
||||
public string IconUrl => "https://lirp.cdn-website.com/6f8d6d8a/dms3rep/multi/opt/API_Icon-640w.png";
|
||||
public string[] AgentIds => new[] { "87c458fc-ec5f-40ae-8ed6-05dda8a07523" };
|
||||
|
||||
public void RegisterDI(IServiceCollection services, IConfiguration config)
|
||||
{
|
||||
services.AddScoped(provider =>
|
||||
{
|
||||
var settingService = provider.GetRequiredService<ISettingService>();
|
||||
return settingService.Bind<HttpHandlerSettings>("HttpHandler");
|
||||
});
|
||||
|
||||
services.AddScoped<IAgentUtilityHook, HttpHandlerUtilityHook>();
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
[Fig 4.1.1](#register-assembly) demonstrates the utility assembly registration in “appsettings.json”. It is important to note that we are required to add the project reference to the Startup project, e.g., WebStarter. Moreover, we are required to add any new custom agent utility in the “Plugin” folder instead of the “BotSharp” folder.
|
||||
|
||||
<div style="text-align: center;" id="register-assembly">
|
||||
<img src="assets/agent-utility/register-assembly.png" />
|
||||
</div>
|
||||
<br />
|
||||
|
||||
|
||||
## Use Case Demo
|
||||
In this section, we demonstrate an http utility. After we set up and integrate the custom agent utility in backend, we can start the BotSharp-UI and go to any specific agent. [Fig 5.1.1](#add-utility) shows an example of the “Chatbot” agent, where we can add any registered utilities in the highlight section.
|
||||
|
||||
<div style="text-align: center;" id="add-utility">
|
||||
<img src="assets/agent-utility/add-utility.png" />
|
||||
</div>
|
||||
<br />
|
||||
|
||||
Once we add the utility, we can initialize a conversation by clicking the bot icon at the top left corner. [Fig 5.1.2](#chat-window-demo) shows the conversation window, where we can find the number of utilities at the left panel. We can also click the agent name to go back to the agent page.
|
||||
|
||||
<div style="text-align: center;" id="chat-window-demo">
|
||||
<img src="assets/agent-utility/chat-window-demo.png" />
|
||||
</div>
|
||||
<br />
|
||||
|
||||
Here we use dummy rest APIs (source: https://dummy.restapiexample.com/) for the demo purpose. [Fig 5.1.3](#dummy-http) displays the various http requests sent in the conversation with “Chatbot”. We can see that the “Http Handler” utility has successfully extends the agent to send http request and receive response.
|
||||
|
||||
<div style="text-align: center;" id="dummy-http">
|
||||
<img src="assets/agent-utility/dummy-http.png" />
|
||||
</div>
|
||||
<br />
|
||||
|
||||
## Summary
|
||||
In this document, we introduce the agent utility concept and provide a step-by-step instruction on adding custom agent utilities in AI project “BotSharp”.
|
||||
|
||||
The agent utility is designed for enhancing the agent capability to perform dedicated tasks, such as sending http request, reading images, and generating images, by adding extra prompts and functions.
|
||||
|
||||
The agent utility setup and integration are explained step by step in [Section 3](#agent-utility-setup) and [Section 4](#agent-utility-integration), respectively.
|
||||
|
||||
We end up the document by demonstrating the Http utility, where we prove the utility can handle various http requests in the chat with agents.
|
||||
BIN
docs/architecture/assets/agent-utility/add-utility.png
Normal file
|
After Width: | Height: | Size: 32 KiB |
|
After Width: | Height: | Size: 81 KiB |
BIN
docs/architecture/assets/agent-utility/agent-utility-example.png
Normal file
|
After Width: | Height: | Size: 107 KiB |
BIN
docs/architecture/assets/agent-utility/chat-window-demo.png
Normal file
|
After Width: | Height: | Size: 14 KiB |
BIN
docs/architecture/assets/agent-utility/dummy-http.png
Normal file
|
After Width: | Height: | Size: 103 KiB |
BIN
docs/architecture/assets/agent-utility/register-assembly.png
Normal file
|
After Width: | Height: | Size: 29 KiB |
BIN
docs/architecture/assets/agent-utility/router-utility-ui.png
Normal file
|
After Width: | Height: | Size: 13 KiB |
BIN
docs/architecture/assets/agent-utility/routing-arch.png
Normal file
|
After Width: | Height: | Size: 18 KiB |
BIN
docs/architecture/assets/agent-utility/task-agent-utility-ui.png
Normal file
|
After Width: | Height: | Size: 4.5 KiB |
|
|
@ -115,6 +115,7 @@ The main documentation for the site is organized into the following sections:
|
|||
architecture/plugin
|
||||
architecture/hooks
|
||||
architecture/routing
|
||||
architecture/agent-utility
|
||||
architecture/logging
|
||||
architecture/data-persistence
|
||||
|
||||
|
|
|
|||
|
|
@ -49,7 +49,11 @@ public abstract class AgentHookBase : IAgentHook
|
|||
return true;
|
||||
}
|
||||
|
||||
public virtual void OnAgentLoaded(Agent agent)
|
||||
public virtual void OnAgentLoaded(Agent agent)
|
||||
{
|
||||
}
|
||||
|
||||
public virtual void OnAgentUtilityLoaded(Agent agent)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -17,7 +17,8 @@ public enum AgentField
|
|||
Response,
|
||||
Sample,
|
||||
LlmConfig,
|
||||
Utility
|
||||
Utility,
|
||||
MaxMessageCount
|
||||
}
|
||||
|
||||
public enum AgentTaskField
|
||||
|
|
|
|||
|
|
@ -10,7 +10,7 @@ public class AgentType
|
|||
/// <summary>
|
||||
/// Planning agent
|
||||
/// </summary>
|
||||
public const string Planning = "plan";
|
||||
public const string Planning = "planning";
|
||||
|
||||
public const string Evaluating = "evaluation";
|
||||
|
||||
|
|
|
|||
|
|
@ -32,11 +32,6 @@ public class BuiltInAgentId
|
|||
/// </summary>
|
||||
public const string Learner = "01acc3e5-0af7-49e6-ad7a-a760bd12dc40";
|
||||
|
||||
/// <summary>
|
||||
/// Plan feasible implementation steps for complex problems
|
||||
/// </summary>
|
||||
public const string Planner = "282a7128-69a1-44b0-878c-a9159b88f3b9";
|
||||
|
||||
/// <summary>
|
||||
/// SQL statement generation
|
||||
/// </summary>
|
||||
|
|
@ -51,4 +46,9 @@ public class BuiltInAgentId
|
|||
/// Evaluate prompt and conversation
|
||||
/// </summary>
|
||||
public const string Evaluator = "dfd9b46d-d00c-40af-8a75-3fbdc2b89869";
|
||||
|
||||
/// <summary>
|
||||
/// Translates user-defined natural language rules into programmatic code
|
||||
/// </summary>
|
||||
public const string RuleEncoder = "6acfb93c-3412-402e-9ba5-c5d3cd8f0161";
|
||||
}
|
||||
|
|
|
|||
|
|
@ -25,6 +25,8 @@ public interface IAgentHook
|
|||
|
||||
bool OnSamplesLoaded(List<string> samples);
|
||||
|
||||
void OnAgentUtilityLoaded(Agent agent);
|
||||
|
||||
/// <summary>
|
||||
/// Triggered when agent is loaded completely.
|
||||
/// </summary>
|
||||
|
|
|
|||
|
|
@ -59,5 +59,5 @@ public interface IAgentService
|
|||
|
||||
PluginDef GetPlugin(string agentId);
|
||||
|
||||
IEnumerable<string> GetAgentUtilities();
|
||||
IEnumerable<AgentUtility> GetAgentUtilityOptions();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,5 +2,5 @@ namespace BotSharp.Abstraction.Agents;
|
|||
|
||||
public interface IAgentUtilityHook
|
||||
{
|
||||
void AddUtilities(List<string> utilities);
|
||||
void AddUtilities(List<AgentUtility> utilities);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -87,19 +87,29 @@ public class Agent
|
|||
/// <summary>
|
||||
/// Profile by channel
|
||||
/// </summary>
|
||||
public List<string> Profiles { get; set; }
|
||||
= new List<string>();
|
||||
public List<string> Profiles { get; set; } = new();
|
||||
|
||||
/// <summary>
|
||||
/// Merge utilities from entry agent
|
||||
/// </summary>
|
||||
public bool MergeUtility { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Agent utilities
|
||||
/// </summary>
|
||||
public List<string> Utilities { get; set; } = new();
|
||||
public List<AgentUtility> Utilities { get; set; } = new();
|
||||
|
||||
/// <summary>
|
||||
/// Inherit from agent
|
||||
/// </summary>
|
||||
public string? InheritAgentId { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Maximum message count when load conversation
|
||||
/// </summary>
|
||||
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
|
||||
public int? MaxMessageCount { get; set; }
|
||||
|
||||
public List<RoutingRule> RoutingRules { get; set; } = new();
|
||||
|
||||
/// <summary>
|
||||
|
|
@ -129,6 +139,8 @@ public class Agent
|
|||
Knowledges = agent.Knowledges,
|
||||
IsPublic = agent.IsPublic,
|
||||
Disabled = agent.Disabled,
|
||||
MergeUtility = agent.MergeUtility,
|
||||
MaxMessageCount = agent.MaxMessageCount,
|
||||
Profiles = agent.Profiles,
|
||||
RoutingRules = agent.RoutingRules,
|
||||
LlmConfig = agent.LlmConfig,
|
||||
|
|
@ -173,9 +185,9 @@ public class Agent
|
|||
return this;
|
||||
}
|
||||
|
||||
public Agent SetUtilities(List<string> utilities)
|
||||
public Agent SetUtilities(List<AgentUtility> utilities)
|
||||
{
|
||||
Utilities = utilities ?? new List<string>();
|
||||
Utilities = utilities ?? new List<AgentUtility>();
|
||||
return this;
|
||||
}
|
||||
|
||||
|
|
@ -215,6 +227,12 @@ public class Agent
|
|||
return this;
|
||||
}
|
||||
|
||||
public Agent SetMergeUtility(bool merge)
|
||||
{
|
||||
MergeUtility = merge;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Agent SetAgentType(string type)
|
||||
{
|
||||
Type = type;
|
||||
|
|
|
|||
|
|
@ -0,0 +1,61 @@
|
|||
namespace BotSharp.Abstraction.Agents.Models;
|
||||
|
||||
public class AgentUtility
|
||||
{
|
||||
public string Name { get; set; }
|
||||
public bool Disabled { get; set; }
|
||||
public IEnumerable<UtilityFunction> Functions { get; set; } = [];
|
||||
public IEnumerable<UtilityTemplate> Templates { get; set; } = [];
|
||||
|
||||
public AgentUtility()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public AgentUtility(
|
||||
string name,
|
||||
IEnumerable<UtilityFunction>? functions = null,
|
||||
IEnumerable<UtilityTemplate>? templates = null)
|
||||
{
|
||||
Name = name;
|
||||
Functions = functions ?? [];
|
||||
Templates = templates ?? [];
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return Name;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public class UtilityFunction : UtilityBase
|
||||
{
|
||||
public UtilityFunction()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public UtilityFunction(string name)
|
||||
{
|
||||
Name = name;
|
||||
}
|
||||
}
|
||||
|
||||
public class UtilityTemplate : UtilityBase
|
||||
{
|
||||
public UtilityTemplate()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public UtilityTemplate(string name)
|
||||
{
|
||||
Name = name;
|
||||
}
|
||||
}
|
||||
|
||||
public class UtilityBase
|
||||
{
|
||||
public string Name { get; set; }
|
||||
}
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>$(TargetFramework)</TargetFramework>
|
||||
|
|
|
|||
|
|
@ -7,4 +7,5 @@ public enum BroswerActionEnum
|
|||
Typing = 3,
|
||||
Hover = 4,
|
||||
Scroll = 5,
|
||||
DragAndDrop = 6
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,4 +5,9 @@ public class ElementPosition
|
|||
public float X { get; set; } = default!;
|
||||
|
||||
public float Y { get; set; } = default!;
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return $"[{X}, {Y}]";
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,27 +2,23 @@ namespace BotSharp.Abstraction.Conversations;
|
|||
|
||||
public abstract class ConversationHookBase : IConversationHook
|
||||
{
|
||||
protected Agent _agent;
|
||||
public Agent Agent => _agent;
|
||||
public Agent Agent { get; private set; }
|
||||
|
||||
protected Conversation _conversation;
|
||||
public Conversation Conversation => _conversation;
|
||||
public Conversation Conversation { get; private set; }
|
||||
|
||||
protected List<RoleDialogModel> _dialogs;
|
||||
public List<RoleDialogModel> Dialogs => _dialogs;
|
||||
public List<RoleDialogModel> Dialogs { get; private set; }
|
||||
|
||||
protected int _priority = 0;
|
||||
public int Priority => _priority;
|
||||
public int Priority { get; protected set; } = 0;
|
||||
|
||||
public IConversationHook SetAgent(Agent agent)
|
||||
{
|
||||
_agent = agent;
|
||||
Agent = agent;
|
||||
return this;
|
||||
}
|
||||
|
||||
public IConversationHook SetConversation(Conversation conversation)
|
||||
{
|
||||
_conversation = conversation;
|
||||
Conversation = conversation;
|
||||
return this;
|
||||
}
|
||||
|
||||
|
|
@ -37,7 +33,7 @@ public abstract class ConversationHookBase : IConversationHook
|
|||
|
||||
public virtual Task OnDialogsLoaded(List<RoleDialogModel> dialogs)
|
||||
{
|
||||
_dialogs = dialogs;
|
||||
Dialogs = dialogs;
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,20 @@
|
|||
namespace BotSharp.Abstraction.Conversations;
|
||||
|
||||
public class ConversationHookProvider
|
||||
{
|
||||
public IEnumerable<IConversationHook> Hooks { get; }
|
||||
|
||||
private readonly Lazy<IEnumerable<IConversationHook>> _hooksOrderByPriority;
|
||||
|
||||
public IEnumerable<IConversationHook> HooksOrderByPriority
|
||||
=> _hooksOrderByPriority.Value;
|
||||
|
||||
public ConversationHookProvider(IEnumerable<IConversationHook> conversationHooks)
|
||||
{
|
||||
Hooks = conversationHooks;
|
||||
_hooksOrderByPriority = new Lazy<IEnumerable<IConversationHook>>(() =>
|
||||
{
|
||||
return conversationHooks.OrderBy(hook => hook.Priority).ToArray();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
@ -7,4 +7,5 @@ public class ConversationChannel
|
|||
public const string Phone = "phone";
|
||||
public const string Messenger = "messenger";
|
||||
public const string Email = "email";
|
||||
public const string Cron = "cron";
|
||||
}
|
||||
|
|
|
|||
|
|
@ -12,6 +12,7 @@ public interface IConversationService
|
|||
Task<Conversation> GetConversation(string id);
|
||||
Task<PagedItems<Conversation>> GetConversations(ConversationFilter filter);
|
||||
Task<Conversation> UpdateConversationTitle(string id, string title);
|
||||
Task<Conversation> UpdateConversationTitleAlias(string id, string titleAlias);
|
||||
Task<bool> UpdateConversationTags(string conversationId, List<string> tags);
|
||||
Task<bool> UpdateConversationMessage(string conversationId, UpdateMessageRequest request);
|
||||
Task<List<Conversation>> GetLastConversations();
|
||||
|
|
|
|||
|
|
@ -13,6 +13,7 @@ public class Conversation
|
|||
/// </summary>
|
||||
public string? TaskId { get; set; }
|
||||
public string Title { get; set; } = string.Empty;
|
||||
public string TitleAlias { get; set; } = string.Empty;
|
||||
|
||||
[JsonIgnore]
|
||||
public List<DialogElement> Dialogs { get; set; } = new();
|
||||
|
|
|
|||
|
|
@ -2,9 +2,11 @@ using BotSharp.Abstraction.Conversations.Enums;
|
|||
using BotSharp.Abstraction.Functions.Models;
|
||||
using BotSharp.Abstraction.Messaging;
|
||||
using BotSharp.Abstraction.Messaging.Models.RichContent;
|
||||
using System.Diagnostics;
|
||||
|
||||
namespace BotSharp.Abstraction.Conversations.Models;
|
||||
|
||||
[DebuggerStepThrough]
|
||||
public class RoleDialogModel : ITrackableMessage
|
||||
{
|
||||
/// <summary>
|
||||
|
|
|
|||
|
|
@ -0,0 +1,33 @@
|
|||
namespace BotSharp.Abstraction.Crontab.Models;
|
||||
|
||||
public class CrontabItem : ScheduleTaskArgs
|
||||
{
|
||||
[JsonPropertyName("user_id")]
|
||||
public string UserId { get; set; } = null!;
|
||||
|
||||
[JsonPropertyName("agent_id")]
|
||||
public string AgentId { get; set; } = null!;
|
||||
|
||||
[JsonPropertyName("conversation_id")]
|
||||
public string ConversationId { get; set; } = null!;
|
||||
|
||||
[JsonPropertyName("execution_result")]
|
||||
public string ExecutionResult { get; set; } = null!;
|
||||
|
||||
[JsonPropertyName("execution_count")]
|
||||
public int ExecutionCount { get; set; }
|
||||
|
||||
[JsonPropertyName("max_execution_count")]
|
||||
public int MaxExecutionCount { get; set; }
|
||||
|
||||
[JsonPropertyName("expire_seconds")]
|
||||
public int ExpireSeconds { get; set; } = 60;
|
||||
|
||||
[JsonPropertyName("created_time")]
|
||||
public DateTime CreatedTime { get; set; } = DateTime.UtcNow;
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return $"{Title}: {Description} [AgentId: {AgentId}, UserId: {UserId}]";
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,23 @@
|
|||
namespace BotSharp.Abstraction.Crontab.Models;
|
||||
|
||||
public class CrontabItemFilter : Pagination
|
||||
{
|
||||
[JsonPropertyName("user_ids")]
|
||||
public IEnumerable<string>? UserIds { get; set; }
|
||||
|
||||
[JsonPropertyName("agent_ids")]
|
||||
public IEnumerable<string>? AgentIds { get; set; }
|
||||
|
||||
[JsonPropertyName("conversation_ids")]
|
||||
public IEnumerable<string>? ConversationIds { get; set; }
|
||||
|
||||
public CrontabItemFilter()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public static CrontabItemFilter Empty()
|
||||
{
|
||||
return new CrontabItemFilter();
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,31 @@
|
|||
namespace BotSharp.Abstraction.Crontab.Models;
|
||||
|
||||
public class ScheduleTaskArgs
|
||||
{
|
||||
[JsonPropertyName("cron_expression")]
|
||||
public string Cron { get; set; } = null!;
|
||||
|
||||
[JsonPropertyName("less_than_60_seconds")]
|
||||
public bool LessThan60Seconds { get; set; } = false;
|
||||
|
||||
[JsonPropertyName("title")]
|
||||
public string Title { get; set; } = null!;
|
||||
|
||||
[JsonPropertyName("description")]
|
||||
public string Description { get; set; } = null!;
|
||||
|
||||
[JsonPropertyName("to_do_list")]
|
||||
public ScheduleTaskItemArgs[] Tasks { get; set; } = [];
|
||||
}
|
||||
|
||||
public class ScheduleTaskItemArgs
|
||||
{
|
||||
[JsonPropertyName("topic")]
|
||||
public string Topic { get; set; } = null!;
|
||||
|
||||
[JsonPropertyName("script")]
|
||||
public string Script { get; set; } = null!;
|
||||
|
||||
[JsonPropertyName("language")]
|
||||
public string Language { get; set; } = null!;
|
||||
}
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
namespace BotSharp.Abstraction.Infrastructures.Enums;
|
||||
|
||||
public enum EventPriority
|
||||
{
|
||||
Low = 1,
|
||||
Medium = 2,
|
||||
High = 3
|
||||
}
|
||||
|
|
@ -10,7 +10,11 @@ public interface IEventPublisher
|
|||
/// <returns></returns>
|
||||
Task BroadcastAsync(string channel, string message);
|
||||
|
||||
Task PublishAsync(string channel, string message);
|
||||
Task<string?> PublishAsync(string channel, string message, EventPriority? priority = null);
|
||||
|
||||
Task ReDispatchAsync(string channel, int count = 10, string order = "asc");
|
||||
|
||||
Task ReDispatchPendingAsync(string channel, string group, int count = 10);
|
||||
|
||||
Task RemoveAsync(string channel, int count = 10);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,8 +1,12 @@
|
|||
using System.Threading;
|
||||
|
||||
namespace BotSharp.Abstraction.Infrastructures.Events;
|
||||
|
||||
public interface IEventSubscriber
|
||||
{
|
||||
Task SubscribeAsync(string channel, Func<string, string, Task> received);
|
||||
|
||||
Task SubscribeAsync(string channel, string group, Func<string, string, Task> received);
|
||||
Task SubscribeAsync(string channel, string group, int? port, bool priorityEnabled,
|
||||
Func<string, string, Task> received,
|
||||
CancellationToken? stoppingToken = null);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,7 @@
|
|||
namespace BotSharp.Abstraction.Infrastructures;
|
||||
|
||||
public interface IDistributedLocker
|
||||
{
|
||||
bool Lock(string resource, Action action, int timeout = 30);
|
||||
Task<bool> LockAsync(string resource, Func<Task> action, int timeout = 30);
|
||||
}
|
||||
|
|
@ -50,7 +50,9 @@ public class SharpCacheAttribute : MoAttribute
|
|||
}
|
||||
|
||||
var httpContext = Services.GetRequiredService<IHttpContextAccessor>();
|
||||
if (httpContext.HttpContext.Response.Headers["Cache-Control"].ToString().Contains("no-store"))
|
||||
if (httpContext != null &&
|
||||
httpContext.HttpContext != null &&
|
||||
httpContext.HttpContext.Response.Headers["Cache-Control"].ToString().Contains("no-store"))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
using BotSharp.Abstraction.Functions.Models;
|
||||
using BotSharp.Abstraction.Routing;
|
||||
|
||||
namespace BotSharp.Abstraction.Routing.Planning;
|
||||
namespace BotSharp.Abstraction.Planning;
|
||||
|
||||
public interface IExecutor
|
||||
{
|
||||
|
|
@ -5,6 +5,9 @@ public interface IPlanningHook
|
|||
Task<string> GetSummaryAdditionalRequirements(string planner, RoleDialogModel message)
|
||||
=> Task.FromResult(string.Empty);
|
||||
|
||||
Task OnSourceCodeGenerated(string planner, RoleDialogModel msg, string language)
|
||||
=> Task.CompletedTask;
|
||||
|
||||
Task OnPlanningCompleted(string planner, RoleDialogModel msg)
|
||||
=> Task.CompletedTask;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,9 +1,20 @@
|
|||
using BotSharp.Abstraction.Functions.Models;
|
||||
|
||||
namespace BotSharp.Abstraction.Planning;
|
||||
|
||||
/// <summary>
|
||||
/// Planning process for Task Agent
|
||||
/// https://www.promptingguide.ai/techniques/cot
|
||||
/// </summary>
|
||||
public class ITaskPlanner
|
||||
public interface ITaskPlanner
|
||||
{
|
||||
|
||||
string Name => "Unamed Task Planner";
|
||||
Task<FunctionCallFromLlm> GetNextInstruction(Agent router, string messageId, List<RoleDialogModel> dialogs);
|
||||
Task<bool> AgentExecuting(Agent router, FunctionCallFromLlm inst, RoleDialogModel message, List<RoleDialogModel> dialogs);
|
||||
Task<bool> AgentExecuted(Agent router, FunctionCallFromLlm inst, RoleDialogModel message, List<RoleDialogModel> dialogs);
|
||||
List<RoleDialogModel> BeforeHandleContext(FunctionCallFromLlm inst, RoleDialogModel message, List<RoleDialogModel> dialogs)
|
||||
=> dialogs;
|
||||
bool AfterHandleContext(List<RoleDialogModel> dialogs, List<RoleDialogModel> taskAgentDialogs)
|
||||
=> true;
|
||||
int MaxLoopCount => 5;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,14 +6,15 @@ public class PluginDef
|
|||
public string Name { get; set; }
|
||||
public string Description { get; set; }
|
||||
public string Assembly { get; set; }
|
||||
|
||||
[JsonPropertyName("is_core")]
|
||||
public bool IsCore => Assembly == "BotSharp.Core" || Assembly == "BotSharp.Core.SideCar";
|
||||
public bool IsCore => Assembly?.StartsWith("BotSharp.Core") == true;
|
||||
|
||||
[JsonPropertyName("icon_url")]
|
||||
public string? IconUrl { get; set; }
|
||||
|
||||
[JsonPropertyName("agent_ids")]
|
||||
public string[] AgentIds { get; set; } = new string[0];
|
||||
public string[] AgentIds { get; set; } = [];
|
||||
|
||||
[JsonPropertyName("settings_name")]
|
||||
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
|
||||
|
|
|
|||
|
|
@ -2,18 +2,19 @@ namespace BotSharp.Abstraction.Repositories;
|
|||
|
||||
public class BotSharpDatabaseSettings : DatabaseBasicSettings
|
||||
{
|
||||
public string[] Assemblies { get; set; }
|
||||
public string FileRepository { get; set; }
|
||||
public string BotSharpMongoDb { get; set; }
|
||||
public string TablePrefix { get; set; }
|
||||
public DbConnectionSetting BotSharp { get; set; }
|
||||
public string Redis { get; set; }
|
||||
public string[] Assemblies { get; set; } = [];
|
||||
public string FileRepository { get; set; } = string.Empty;
|
||||
public string BotSharpMongoDb { get; set; } = string.Empty;
|
||||
public string TablePrefix { get; set; } = string.Empty;
|
||||
public DbConnectionSetting BotSharp { get; set; } = new();
|
||||
public string Redis { get; set; } = string.Empty;
|
||||
public bool EnableReplica { get; set; } = true;
|
||||
}
|
||||
|
||||
public class DatabaseBasicSettings
|
||||
{
|
||||
public string Default { get; set; }
|
||||
public DbConnectionSetting DefaultConnection { get; set; }
|
||||
public string Default { get; set; } = string.Empty;
|
||||
public DbConnectionSetting DefaultConnection { get; set; } = new();
|
||||
public bool EnableSqlLog { get; set; }
|
||||
public bool EnableSensitiveDataLogging { get; set; }
|
||||
public bool EnableRetryOnFailure { get; set; }
|
||||
|
|
@ -23,9 +24,11 @@ public class DbConnectionSetting
|
|||
{
|
||||
public string Master { get; set; }
|
||||
public string[] Slavers { get; set; }
|
||||
public int ConnectionTimeout { get; set; } = 30;
|
||||
public int ExecutionTimeout { get; set; } = 30;
|
||||
|
||||
public DbConnectionSetting()
|
||||
{
|
||||
Slavers = new string[0];
|
||||
Slavers = [];
|
||||
}
|
||||
}
|
||||
|
|
@ -8,6 +8,7 @@ public class ConversationFilter
|
|||
/// </summary>
|
||||
public string? Id { get; set; }
|
||||
public string? Title { get; set; }
|
||||
public string? TitleAlias { get; set; }
|
||||
public string? AgentId { get; set; }
|
||||
public string? Status { get; set; }
|
||||
public string? Channel { get; set; }
|
||||
|
|
|
|||
|
|
@ -15,4 +15,9 @@ public class RoleFilter
|
|||
{
|
||||
return new RoleFilter();
|
||||
}
|
||||
|
||||
public bool IsInit()
|
||||
{
|
||||
return Names.IsNullOrEmpty();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ using BotSharp.Abstraction.Roles.Models;
|
|||
using BotSharp.Abstraction.Shared;
|
||||
using BotSharp.Abstraction.Tasks.Models;
|
||||
using BotSharp.Abstraction.Translation.Models;
|
||||
using BotSharp.Abstraction.Users.Enums;
|
||||
using BotSharp.Abstraction.Users.Models;
|
||||
using BotSharp.Abstraction.VectorStorage.Models;
|
||||
|
||||
|
|
@ -26,15 +27,19 @@ public interface IBotSharpRepository : IHaveServiceProvider
|
|||
|
||||
#region User
|
||||
User? GetUserByEmail(string email) => throw new NotImplementedException();
|
||||
User? GetUserByPhone(string phone, string role = null, string regionCode = "CN") => throw new NotImplementedException();
|
||||
User? GetUserByPhone(string phone, string type = UserType.Client, string regionCode = "CN") => throw new NotImplementedException();
|
||||
User? GetAffiliateUserByPhone(string phone) => throw new NotImplementedException();
|
||||
User? GetUserById(string id) => throw new NotImplementedException();
|
||||
List<User> GetUserByIds(List<string> ids) => throw new NotImplementedException();
|
||||
List<User> GetUsersByAffiliateId(string affiliateId) => throw new NotImplementedException();
|
||||
User? GetUserByUserName(string userName) => throw new NotImplementedException();
|
||||
Dashboard? GetDashboard(string id = null) => throw new NotImplementedException();
|
||||
void CreateUser(User user) => throw new NotImplementedException();
|
||||
void UpdateExistUser(string userId, User user) => throw new NotImplementedException();
|
||||
void UpdateUserVerified(string userId) => throw new NotImplementedException();
|
||||
void AddDashboardConversation(string userId, string conversationId) => throw new NotImplementedException();
|
||||
void RemoveDashboardConversation(string userId, string conversationId) => throw new NotImplementedException();
|
||||
void UpdateDashboardConversation(string userId, DashboardConversation dashConv) => throw new NotImplementedException();
|
||||
void UpdateUserVerificationCode(string userId, string verficationCode) => throw new NotImplementedException();
|
||||
void UpdateUserPassword(string userId, string password) => throw new NotImplementedException();
|
||||
void UpdateUserEmail(string userId, string email) => throw new NotImplementedException();
|
||||
|
|
@ -48,7 +53,7 @@ public interface IBotSharpRepository : IHaveServiceProvider
|
|||
|
||||
#region Agent
|
||||
void UpdateAgent(Agent agent, AgentField field);
|
||||
Agent? GetAgent(string agentId);
|
||||
Agent? GetAgent(string agentId, bool basicsOnly = false);
|
||||
List<Agent> GetAgents(AgentFilter filter);
|
||||
List<UserAgent> GetUserAgents(string userId);
|
||||
void BulkInsertAgents(List<Agent> agents);
|
||||
|
|
@ -81,6 +86,7 @@ public interface IBotSharpRepository : IHaveServiceProvider
|
|||
Conversation GetConversation(string conversationId);
|
||||
PagedItems<Conversation> GetConversations(ConversationFilter filter);
|
||||
void UpdateConversationTitle(string conversationId, string title);
|
||||
void UpdateConversationTitleAlias(string conversationId, string titleAlias);
|
||||
bool UpdateConversationTags(string conversationId, List<string> tags);
|
||||
bool UpdateConversationMessage(string conversationId, UpdateMessageRequest request);
|
||||
void UpdateConversationBreakpoint(string conversationId, ConversationBreakpoint breakpoint);
|
||||
|
|
@ -140,4 +146,10 @@ public interface IBotSharpRepository : IHaveServiceProvider
|
|||
bool DeleteKnolwedgeBaseFileMeta(string collectionName, string vectorStoreProvider, Guid? fileId = null);
|
||||
PagedItems<KnowledgeDocMetaData> GetKnowledgeBaseFileMeta(string collectionName, string vectorStoreProvider, KnowledgeFileFilter filter);
|
||||
#endregion
|
||||
|
||||
#region Crontab
|
||||
bool UpsertCrontabItem(CrontabItem cron) => throw new NotImplementedException();
|
||||
bool DeleteCrontabItem(string conversationId) => throw new NotImplementedException();
|
||||
PagedItems<CrontabItem> GetCrontabItems(CrontabItemFilter filter) => throw new NotImplementedException();
|
||||
#endregion
|
||||
}
|
||||
|
|
|
|||
|
|
@ -12,6 +12,11 @@ public class RuleType
|
|||
/// </summary>
|
||||
public const string DataValidation = "data-validation";
|
||||
|
||||
/// <summary>
|
||||
/// The reasoning approach name for next step
|
||||
/// </summary>
|
||||
public const string Reasoner = "reasoner";
|
||||
|
||||
/// <summary>
|
||||
/// The planning approach name for next step
|
||||
/// </summary>
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ public interface IRoutingContext
|
|||
string FirstGoalAgentId();
|
||||
bool ContainsAgentId(string agentId);
|
||||
string OriginAgentId { get; }
|
||||
string EntryAgentId { get; }
|
||||
string ConversationId { get; }
|
||||
string MessageId { get; }
|
||||
void SetMessageId(string conversationId, string messageId);
|
||||
|
|
|
|||
|
|
@ -13,6 +13,9 @@ public class RoutableAgent
|
|||
[JsonPropertyName("description")]
|
||||
public string Description { get; set; } = string.Empty;
|
||||
|
||||
[JsonPropertyName("type")]
|
||||
public string Type { get; set; } = AgentType.Task;
|
||||
|
||||
[JsonPropertyName("profiles")]
|
||||
public List<string> Profiles { get; set; }
|
||||
= new List<string>();
|
||||
|
|
|
|||
|
|
@ -1,19 +0,0 @@
|
|||
using BotSharp.Abstraction.Functions.Models;
|
||||
|
||||
namespace BotSharp.Abstraction.Routing.Planning;
|
||||
|
||||
/// <summary>
|
||||
/// Task breakdown and execution plan
|
||||
/// https://www.promptingguide.ai/techniques/cot
|
||||
/// </summary>
|
||||
public interface IRoutingPlaner
|
||||
{
|
||||
Task<FunctionCallFromLlm> GetNextInstruction(Agent router, string messageId, List<RoleDialogModel> dialogs);
|
||||
Task<bool> AgentExecuting(Agent router, FunctionCallFromLlm inst, RoleDialogModel message, List<RoleDialogModel> dialogs);
|
||||
Task<bool> AgentExecuted(Agent router, FunctionCallFromLlm inst, RoleDialogModel message, List<RoleDialogModel> dialogs);
|
||||
List<RoleDialogModel> BeforeHandleContext(FunctionCallFromLlm inst, RoleDialogModel message, List<RoleDialogModel> dialogs)
|
||||
=> dialogs;
|
||||
bool AfterHandleContext(List<RoleDialogModel> dialogs, List<RoleDialogModel> taskAgentDialogs)
|
||||
=> true;
|
||||
int MaxLoopCount => 5;
|
||||
}
|
||||
|
|
@ -0,0 +1,30 @@
|
|||
using BotSharp.Abstraction.Functions.Models;
|
||||
|
||||
namespace BotSharp.Abstraction.Routing.Reasoning;
|
||||
|
||||
/// <summary>
|
||||
/// Reasoning approaches for large language models (LLMs) help enhance their ability to solve complex problems,
|
||||
/// handle tasks requiring logic, and provide accurate and contextually appropriate responses.
|
||||
/// </summary>
|
||||
public interface IRoutingReasoner
|
||||
{
|
||||
string Name => "Unnamed Reasoner";
|
||||
string Description => "Each of these approaches leverages the capabilities of LLMs to reason more effectively, " +
|
||||
"ensuring better performance and more coherent outputs across various types of complex tasks.";
|
||||
|
||||
int MaxLoopCount => 5;
|
||||
|
||||
Task<FunctionCallFromLlm> GetNextInstruction(Agent router, string messageId, List<RoleDialogModel> dialogs);
|
||||
|
||||
Task<bool> AgentExecuting(Agent router, FunctionCallFromLlm inst, RoleDialogModel message, List<RoleDialogModel> dialogs)
|
||||
=> Task.FromResult(true);
|
||||
|
||||
Task<bool> AgentExecuted(Agent router, FunctionCallFromLlm inst, RoleDialogModel message, List<RoleDialogModel> dialogs)
|
||||
=> Task.FromResult(true);
|
||||
|
||||
List<RoleDialogModel> BeforeHandleContext(FunctionCallFromLlm inst, RoleDialogModel message, List<RoleDialogModel> dialogs)
|
||||
=> dialogs;
|
||||
|
||||
bool AfterHandleContext(List<RoleDialogModel> dialogs, List<RoleDialogModel> taskAgentDialogs)
|
||||
=> true;
|
||||
}
|
||||
|
|
@ -9,5 +9,6 @@ public interface IConversationSideCar
|
|||
List<DialogElement> GetConversationDialogs(string conversationId);
|
||||
void UpdateConversationBreakpoint(string conversationId, ConversationBreakpoint breakpoint);
|
||||
ConversationBreakpoint? GetConversationBreakpoint(string conversationId);
|
||||
Task<RoleDialogModel> SendMessage(string agentId, string text, PostbackMessageModel? postback = null, List<MessageState>? states = null);
|
||||
Task<RoleDialogModel> SendMessage(string agentId, string text,
|
||||
PostbackMessageModel? postback = null, List<MessageState>? states = null, List<DialogElement>? dialogs = null);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,4 +3,5 @@ namespace BotSharp.Abstraction.Templating;
|
|||
public interface ITemplateRender
|
||||
{
|
||||
string Render(string template, Dictionary<string, object> dict);
|
||||
void Register(Type type);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,8 @@
|
|||
namespace BotSharp.Abstraction.Users.Enums;
|
||||
|
||||
/// <summary>
|
||||
/// User actions on agent level
|
||||
/// </summary>
|
||||
public static class UserAction
|
||||
{
|
||||
public const string Edit = "edit";
|
||||
|
|
|
|||
|
|
@ -1,5 +1,8 @@
|
|||
namespace BotSharp.Abstraction.Users.Enums;
|
||||
|
||||
/// <summary>
|
||||
/// User permission
|
||||
/// </summary>
|
||||
public static class UserPermission
|
||||
{
|
||||
public const string CreateAgent = "create-agent";
|
||||
|
|
|
|||
|
|
@ -29,4 +29,8 @@ public interface IUserService
|
|||
Task<bool> UpdatePassword(string newPassword, string verificationCode);
|
||||
Task<DateTime> GetUserTokenExpires();
|
||||
Task<bool> UpdateUsersIsDisable(List<string> userIds, bool isDisable);
|
||||
Task<bool> AddDashboardConversation(string userId, string conversationId);
|
||||
Task<bool> RemoveDashboardConversation(string userId, string conversationId);
|
||||
Task UpdateDashboardConversation(string userId, DashboardConversation dashConv);
|
||||
Task<Dashboard?> GetDashboard(string userId);
|
||||
}
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
|
||||
namespace BotSharp.Abstraction.Users.Models;
|
||||
|
||||
public class Dashboard
|
||||
{
|
||||
public IList<DashboardConversation> ConversationList { get; set; } = [];
|
||||
}
|
||||
|
||||
public class DashboardComponent
|
||||
{
|
||||
public required string Id { get; set; }
|
||||
public string? Name { get; set; }
|
||||
}
|
||||
|
||||
public class DashboardConversation : DashboardComponent
|
||||
{
|
||||
public string? ConversationId { get; set; }
|
||||
public string? Instruction { get; set; } = "";
|
||||
}
|
||||
|
||||
|
|
@ -23,6 +23,7 @@ public class User
|
|||
public bool Verified { get; set; }
|
||||
public string RegionCode { get; set; } = "CN";
|
||||
public string? AffiliateId { get; set; }
|
||||
public string? ReferralCode { get; set; }
|
||||
public string? EmployeeId { get; set; }
|
||||
public bool IsDisabled { get; set; }
|
||||
public IEnumerable<string> Permissions { get; set; } = [];
|
||||
|
|
|
|||
|
|
@ -22,4 +22,26 @@ public static class UserAuthorizationExtension
|
|||
var actions = found.Actions ?? [];
|
||||
return actions.Any(x => x == targetAction);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get allowed user actions on the agent. If user is admin, returns null;
|
||||
/// </summary>
|
||||
/// <param name="auth"></param>
|
||||
/// <param name="agentId"></param>
|
||||
/// <returns></returns>
|
||||
public static IEnumerable<string>? GetAllowedAgentActions(this UserAuthorization auth, string agentId)
|
||||
{
|
||||
if (auth == null || string.IsNullOrEmpty(agentId))
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
if (auth.IsAdmin)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var found = auth.AgentActions.FirstOrDefault(x => x.AgentId == agentId);
|
||||
return found?.Actions ?? [];
|
||||
}
|
||||
}
|
||||
|
|
@ -12,10 +12,10 @@ global using BotSharp.Abstraction.Agents.Enums;
|
|||
global using BotSharp.Abstraction.Infrastructures.Enums;
|
||||
global using BotSharp.Abstraction.Models;
|
||||
global using BotSharp.Abstraction.Routing.Models;
|
||||
global using BotSharp.Abstraction.Routing.Planning;
|
||||
global using BotSharp.Abstraction.Templating;
|
||||
global using BotSharp.Abstraction.Translation.Attributes;
|
||||
global using BotSharp.Abstraction.Messaging.Enums;
|
||||
global using BotSharp.Abstraction.Files.Models;
|
||||
global using BotSharp.Abstraction.Files.Enums;
|
||||
global using BotSharp.Abstraction.Knowledges.Models;
|
||||
global using BotSharp.Abstraction.Knowledges.Models;
|
||||
global using BotSharp.Abstraction.Crontab.Models;
|
||||
14
src/Infrastructure/BotSharp.Abstraction/Utilities/MathExt.cs
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
namespace BotSharp.Abstraction.Utilities;
|
||||
|
||||
public static class MathExt
|
||||
{
|
||||
public static int Max(int a, int b, int c)
|
||||
{
|
||||
return Math.Max(Math.Max(a, b), c);
|
||||
}
|
||||
|
||||
public static long Max(long a, long b, long c)
|
||||
{
|
||||
return Math.Max(Math.Max(a, b), c);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,7 +1,9 @@
|
|||
using BotSharp.Abstraction.Infrastructures;
|
||||
using System.Diagnostics;
|
||||
|
||||
namespace BotSharp.Abstraction.Utilities;
|
||||
|
||||
[DebuggerStepThrough]
|
||||
public class Pagination : ICacheKey
|
||||
{
|
||||
private int _page;
|
||||
|
|
|
|||
|
|
@ -0,0 +1,6 @@
|
|||
namespace BotSharp.Core.Crontab.Abstraction;
|
||||
|
||||
public interface ICrontabHook
|
||||
{
|
||||
Task OnCronTriggered(CrontabItem item);
|
||||
}
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
namespace BotSharp.Core.Crontab.Abstraction;
|
||||
|
||||
public interface ICrontabService
|
||||
{
|
||||
Task<List<CrontabItem>> GetCrontable();
|
||||
Task ScheduledTimeArrived(CrontabItem item);
|
||||
}
|
||||
|
|
@ -0,0 +1,35 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<None Remove="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\functions\util-crontab-schedule_task.json" />
|
||||
<None Remove="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\templates\util-crontab-schedule_task.fn.liquid" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Content Include="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\functions\util-crontab-schedule_task.json">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</Content>
|
||||
<Content Include="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\templates\util-crontab-schedule_task.fn.liquid">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</Content>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\BotSharp.Core\BotSharp.Core.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="NCrontab" Version="3.3.3" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="Models\" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
38
src/Infrastructure/BotSharp.Core.Crontab/CrontabPlugin.cs
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
/*****************************************************************************
|
||||
Copyright 2024 Written by Haiping Chen. All Rights Reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
******************************************************************************/
|
||||
|
||||
using BotSharp.Core.Crontab.Hooks;
|
||||
|
||||
namespace BotSharp.Core.Crontab;
|
||||
|
||||
/// <summary>
|
||||
/// Crontab plugin is a time-based job scheduler in agent framework.
|
||||
/// The cron system is used for automating repetitive tasks, such as trigger AI Agent to do specific task periodically.
|
||||
/// </summary>
|
||||
public class CrontabPlugin : IBotSharpPlugin
|
||||
{
|
||||
public string Id => "3155c15e-28d3-43f7-8ead-fc43324ec21a";
|
||||
public string Name => "BotSharp Crontab";
|
||||
public string Description => "Crontab plugin is a time-based job scheduler in agent framework. The cron system is used to trigger AI Agent to do specific task periodically.";
|
||||
public string IconUrl => "https://icon-library.com/images/stop-watch-icon/stop-watch-icon-10.jpg";
|
||||
|
||||
public void RegisterDI(IServiceCollection services, IConfiguration config)
|
||||
{
|
||||
services.AddScoped<IAgentUtilityHook, CrontabUtilityHook>();
|
||||
services.AddScoped<ICrontabService, CrontabService>();
|
||||
services.AddHostedService<CrontabWatcher>();
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
namespace BotSharp.Core.Crontab.Enum;
|
||||
|
||||
public class UtilityName
|
||||
{
|
||||
public const string ScheduleTask = "crontab.schedule-task";
|
||||
}
|
||||
|
|
@ -0,0 +1,55 @@
|
|||
using BotSharp.Abstraction.Repositories;
|
||||
using BotSharp.Abstraction.Routing;
|
||||
using BotSharp.Abstraction.Users;
|
||||
using BotSharp.Core.Crontab.Hooks;
|
||||
|
||||
namespace BotSharp.Core.Crontab.Functions;
|
||||
|
||||
public class ScheduleTaskFn : IFunctionCallback
|
||||
{
|
||||
public string Name => $"{CrontabUtilityHook.PREFIX}schedule_task";
|
||||
private readonly IServiceProvider _services;
|
||||
|
||||
public ScheduleTaskFn(IServiceProvider services)
|
||||
{
|
||||
_services = services;
|
||||
}
|
||||
|
||||
public async Task<bool> Execute(RoleDialogModel message)
|
||||
{
|
||||
var args = JsonSerializer.Deserialize<ScheduleTaskArgs>(message.FunctionArgs);
|
||||
if (args.LessThan60Seconds)
|
||||
{
|
||||
message.Content = "Cron expression should not include seconds.";
|
||||
return false;
|
||||
}
|
||||
|
||||
var routing = _services.GetRequiredService<IRoutingContext>();
|
||||
var user = _services.GetRequiredService<IUserIdentity>();
|
||||
var db = _services.GetRequiredService<IBotSharpRepository>();
|
||||
|
||||
if (string.IsNullOrEmpty(args.Cron))
|
||||
{
|
||||
var ret = db.DeleteCrontabItem(routing.ConversationId);
|
||||
message.Content = $"Task schedule canceled result: {ret}";
|
||||
}
|
||||
else
|
||||
{
|
||||
var crontabItem = new CrontabItem
|
||||
{
|
||||
Title = args.Title,
|
||||
Description = args.Description,
|
||||
Cron = args.Cron,
|
||||
UserId = user.Id,
|
||||
AgentId = routing.EntryAgentId,
|
||||
ConversationId = routing.ConversationId,
|
||||
Tasks = args.Tasks,
|
||||
};
|
||||
|
||||
var ret = db.UpsertCrontabItem(crontabItem);
|
||||
message.Content = $"Task scheduled result: {ret}";
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,25 @@
|
|||
using BotSharp.Abstraction.Agents.Models;
|
||||
using BotSharp.Core.Crontab.Enum;
|
||||
|
||||
namespace BotSharp.Core.Crontab.Hooks;
|
||||
|
||||
public class CrontabUtilityHook : IAgentUtilityHook
|
||||
{
|
||||
public const string PREFIX = "util-crontab-";
|
||||
private const string SCHEDULE_TASK_FN = $"{PREFIX}schedule_task";
|
||||
|
||||
public void AddUtilities(List<AgentUtility> utilities)
|
||||
{
|
||||
var items = new List<AgentUtility>
|
||||
{
|
||||
new AgentUtility
|
||||
{
|
||||
Name = UtilityName.ScheduleTask,
|
||||
Functions = [new(SCHEDULE_TASK_FN)],
|
||||
Templates = [new($"{SCHEDULE_TASK_FN}.fn")]
|
||||
}
|
||||
};
|
||||
|
||||
utilities.AddRange(items);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,54 @@
|
|||
/*****************************************************************************
|
||||
Copyright 2024 Written by Haiping Chen. All Rights Reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
******************************************************************************/
|
||||
|
||||
using BotSharp.Abstraction.Repositories;
|
||||
using BotSharp.Core.Infrastructures;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace BotSharp.Core.Crontab.Services;
|
||||
|
||||
/// <summary>
|
||||
/// The Crontab service schedules distributed events based on the execution times provided by users.
|
||||
/// In a scalable environment, distributed locks are used to ensure that each event is triggered only once.
|
||||
/// </summary>
|
||||
public class CrontabService : ICrontabService
|
||||
{
|
||||
private readonly IServiceProvider _services;
|
||||
private ILogger _logger;
|
||||
|
||||
public CrontabService(IServiceProvider services, ILogger<CrontabService> logger)
|
||||
{
|
||||
_services = services;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public async Task<List<CrontabItem>> GetCrontable()
|
||||
{
|
||||
var repo = _services.GetRequiredService<IBotSharpRepository>();
|
||||
var crontable = repo.GetCrontabItems(CrontabItemFilter.Empty());
|
||||
return crontable.Items.ToList();
|
||||
}
|
||||
|
||||
public async Task ScheduledTimeArrived(CrontabItem item)
|
||||
{
|
||||
_logger.LogDebug($"ScheduledTimeArrived {item}");
|
||||
|
||||
await HookEmitter.Emit<ICrontabHook>(_services, async hook =>
|
||||
await hook.OnCronTriggered(item)
|
||||
);
|
||||
await Task.Delay(1000 * 10);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,80 @@
|
|||
using BotSharp.Abstraction.Infrastructures;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using NCrontab;
|
||||
|
||||
namespace BotSharp.Core.Crontab.Services;
|
||||
|
||||
public class CrontabWatcher : BackgroundService
|
||||
{
|
||||
private readonly ILogger _logger;
|
||||
private readonly IServiceProvider _services;
|
||||
|
||||
public CrontabWatcher(IServiceProvider services, ILogger<CrontabWatcher> logger)
|
||||
{
|
||||
_logger = logger;
|
||||
_services = services;
|
||||
}
|
||||
|
||||
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||
{
|
||||
_logger.LogInformation("Crontab Watcher background service is running.");
|
||||
|
||||
using (var scope = _services.CreateScope())
|
||||
{
|
||||
var locker = scope.ServiceProvider.GetRequiredService<IDistributedLocker>();
|
||||
|
||||
while (!stoppingToken.IsCancellationRequested)
|
||||
{
|
||||
var delay = Task.Delay(1000, stoppingToken);
|
||||
|
||||
await locker.LockAsync("CrontabWatcher", async () =>
|
||||
{
|
||||
await RunCronChecker(scope.ServiceProvider);
|
||||
});
|
||||
|
||||
await delay;
|
||||
}
|
||||
|
||||
_logger.LogWarning("Crontab Watcher background service is stopped.");
|
||||
}
|
||||
}
|
||||
|
||||
private async Task RunCronChecker(IServiceProvider services)
|
||||
{
|
||||
var cron = services.GetRequiredService<ICrontabService>();
|
||||
var crons = await cron.GetCrontable();
|
||||
foreach (var item in crons)
|
||||
{
|
||||
try
|
||||
{
|
||||
// strip seconds from cron expression
|
||||
item.Cron = string.Join(" ", item.Cron.Split(' ').TakeLast(5));
|
||||
var schedule = CrontabSchedule.Parse(item.Cron, new CrontabSchedule.ParseOptions
|
||||
{
|
||||
IncludingSeconds = false // Ensure you account for seconds
|
||||
});
|
||||
|
||||
// Get the current time
|
||||
var currentTime = DateTime.UtcNow;
|
||||
|
||||
// Get the next occurrence from the schedule
|
||||
var nextOccurrence = schedule.GetNextOccurrence(currentTime.AddSeconds(-1));
|
||||
|
||||
// Check if the current time matches the schedule
|
||||
bool matches = currentTime >= nextOccurrence && currentTime < nextOccurrence.AddSeconds(1);
|
||||
|
||||
if (matches)
|
||||
{
|
||||
_logger.LogDebug($"The current time matches the cron expression {item}");
|
||||
cron.ScheduledTimeArrived(item);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogWarning($"Error when running cron task ({item.ConversationId}, {item.Title}, {item.Cron}): {ex.Message}\r\n{ex.InnerException}");
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
13
src/Infrastructure/BotSharp.Core.Crontab/Using.cs
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
global using System.Text.Json;
|
||||
|
||||
global using Microsoft.Extensions.Configuration;
|
||||
global using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
global using BotSharp.Abstraction.Agents.Enums;
|
||||
global using BotSharp.Abstraction.Crontab.Models;
|
||||
global using BotSharp.Abstraction.Agents;
|
||||
global using BotSharp.Abstraction.Plugins;
|
||||
global using BotSharp.Abstraction.Conversations.Models;
|
||||
global using BotSharp.Abstraction.Functions;
|
||||
global using BotSharp.Core.Crontab.Services;
|
||||
global using BotSharp.Core.Crontab.Abstraction;
|
||||
|
|
@ -0,0 +1,48 @@
|
|||
{
|
||||
"name": "util-crontab-schedule_task",
|
||||
"description": "Set up or cancel a scheduled task",
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"cron_expression": {
|
||||
"type": "string",
|
||||
"description": "cron expression. Set value as empty if user wants to cancel schedule."
|
||||
},
|
||||
"less_than_60_seconds": {
|
||||
"type": "boolean",
|
||||
"description": "whether schedule interval is less than 60 seconds"
|
||||
},
|
||||
"title": {
|
||||
"type": "string",
|
||||
"description": "task short title"
|
||||
},
|
||||
"description": {
|
||||
"type": "string",
|
||||
"description": "the task summary"
|
||||
},
|
||||
"to_do_list": {
|
||||
"type": "array",
|
||||
"description": "task to do list, should include all key business entities",
|
||||
"items": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"topic": {
|
||||
"type": "string",
|
||||
"description": "task topic in 1-3 keywords related to the step"
|
||||
},
|
||||
"script": {
|
||||
"type": "string",
|
||||
"description": "task related script, function with parameters or plain text"
|
||||
},
|
||||
"language": {
|
||||
"type": "string",
|
||||
"enum": [ "function", "sql", "python", "text" ],
|
||||
"description": "programming language script, function or plain text"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"required": [ "cron_expression", "include_seconds", "title", "description", "to_do_list" ]
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
Call util-crontab-schedule_task if user needs to set up a scheduled task with appropriate programming script and language type.
|
||||
Set cron_expression as empty if user wants to cancel schedule.
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\Infrastructure\BotSharp.Abstraction\BotSharp.Abstraction.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
19
src/Infrastructure/BotSharp.Core.Rules/RulesPlugin.cs
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
namespace BotSharp.Core.Rules;
|
||||
|
||||
public class RulesPlugin : IBotSharpPlugin
|
||||
{
|
||||
public string Id => "0197c1bc-9ae6-4c56-a305-8a1b4095bebc";
|
||||
public string Name => "BotSharp Rules";
|
||||
public string Description => "Translates user-defined natural language rules into programmatic code and is responsible for executing these rules under user-specified conditions.";
|
||||
public string IconUrl => "https://w7.pngwing.com/pngs/442/614/png-transparent-regulation-computer-icons-regulatory-compliance-medical-device-manufacturing-others-miscellaneous-blue-text-thumbnail.png";
|
||||
|
||||
public string[] AgentIds =
|
||||
[
|
||||
BuiltInAgentId.RuleEncoder
|
||||
];
|
||||
|
||||
public void RegisterDI(IServiceCollection services, IConfiguration config)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
4
src/Infrastructure/BotSharp.Core.Rules/Using.cs
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
global using BotSharp.Abstraction.Agents.Enums;
|
||||
global using BotSharp.Abstraction.Plugins;
|
||||
global using Microsoft.Extensions.Configuration;
|
||||
global using Microsoft.Extensions.DependencyInjection;
|
||||
|
|
@ -10,7 +10,7 @@
|
|||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\BotSharp.Abstraction\BotSharp.Abstraction.csproj" />
|
||||
<ProjectReference Include="..\BotSharp.Core\BotSharp.Core.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
|
|
|||
|
|
@ -8,8 +8,10 @@ namespace BotSharp.Core.SideCar;
|
|||
public class BotSharpSideCarPlugin : IBotSharpPlugin
|
||||
{
|
||||
public string Id => "06e5a276-bba0-45af-9625-889267c341c9";
|
||||
public string Name => "Side Car";
|
||||
public string Description => "Provides side car for calling agent cluster in conversation";
|
||||
public string Name => "BotSharp SideCar";
|
||||
public string Description => "Provide side car pattern to to better handle Agent Cluster calls in the same conversation. Agent cluster is composed of multiple Routing Agents.";
|
||||
public string? IconUrl => "https://icons.veryicon.com/png/128/internet--web/2022-alibaba-cloud-product-icon-cloud/aliyuncvc-cloud-video-conference.png";
|
||||
|
||||
|
||||
public SettingsMeta Settings => new SettingsMeta("SideCar");
|
||||
public object GetNewSettingsInstance() => new SideCarSettings();
|
||||
|
|
|
|||
|
|
@ -1,3 +1,21 @@
|
|||
/*****************************************************************************
|
||||
Copyright 2024 Written by Jicheng Lu. All Rights Reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
******************************************************************************/
|
||||
|
||||
using BotSharp.Core.Infrastructures;
|
||||
|
||||
namespace BotSharp.Core.SideCar.Services;
|
||||
|
||||
public class BotSharpConversationSideCar : IConversationSideCar
|
||||
|
|
@ -62,9 +80,9 @@ public class BotSharpConversationSideCar : IConversationSideCar
|
|||
}
|
||||
|
||||
public async Task<RoleDialogModel> SendMessage(string agentId, string text,
|
||||
PostbackMessageModel? postback = null, List<MessageState>? states = null)
|
||||
PostbackMessageModel? postback = null, List<MessageState>? states = null, List<DialogElement>? dialogs = null)
|
||||
{
|
||||
BeforeExecute();
|
||||
BeforeExecute(dialogs);
|
||||
var response = await InnerExecute(agentId, text, postback, states);
|
||||
AfterExecute();
|
||||
return response;
|
||||
|
|
@ -96,7 +114,7 @@ public class BotSharpConversationSideCar : IConversationSideCar
|
|||
return response;
|
||||
}
|
||||
|
||||
private void BeforeExecute()
|
||||
private void BeforeExecute(List<DialogElement>? dialogs)
|
||||
{
|
||||
enabled = true;
|
||||
var state = _services.GetRequiredService<IConversationStateService>();
|
||||
|
|
@ -105,8 +123,8 @@ public class BotSharpConversationSideCar : IConversationSideCar
|
|||
var node = new ConversationContext
|
||||
{
|
||||
State = state.GetCurrentState(),
|
||||
Dialogs = new(),
|
||||
Breakpoints = new(),
|
||||
Dialogs = dialogs ?? [],
|
||||
Breakpoints = [],
|
||||
RecursiveCounter = routing.Context.GetRecursiveCounter(),
|
||||
RoutingStack = routing.Context.GetAgentStack()
|
||||
};
|
||||
|
|
@ -116,7 +134,7 @@ public class BotSharpConversationSideCar : IConversationSideCar
|
|||
state.ResetCurrentState();
|
||||
routing.Context.ResetRecursiveCounter();
|
||||
routing.Context.ResetAgentStack();
|
||||
|
||||
Utilities.ClearCache();
|
||||
}
|
||||
|
||||
private void AfterExecute()
|
||||
|
|
@ -130,6 +148,7 @@ public class BotSharpConversationSideCar : IConversationSideCar
|
|||
state.SetCurrentState(node.State);
|
||||
routing.Context.SetRecursiveCounter(node.RecursiveCounter);
|
||||
routing.Context.SetAgentStack(node.RoutingStack);
|
||||
Utilities.ClearCache();
|
||||
enabled = false;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,7 +1,9 @@
|
|||
using BotSharp.Abstraction.MLTasks;
|
||||
using BotSharp.Abstraction.Plugins.Models;
|
||||
using BotSharp.Abstraction.Settings;
|
||||
using BotSharp.Abstraction.Templating;
|
||||
using BotSharp.Abstraction.Users.Enums;
|
||||
using BotSharp.Core.Agents.Hooks;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
|
||||
namespace BotSharp.Core.Agents;
|
||||
|
|
@ -29,10 +31,13 @@ public class AgentPlugin : IBotSharpPlugin
|
|||
{
|
||||
services.AddScoped<ILlmProviderService, LlmProviderService>();
|
||||
services.AddScoped<IAgentService, AgentService>();
|
||||
services.AddScoped<IAgentHook, BasicAgentHook>();
|
||||
|
||||
services.AddScoped(provider =>
|
||||
{
|
||||
var settingService = provider.GetRequiredService<ISettingService>();
|
||||
var render = provider.GetRequiredService<ITemplateRender>();
|
||||
render.Register(typeof(AgentSettings));
|
||||
return settingService.Bind<AgentSettings>("Agent");
|
||||
});
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,83 @@
|
|||
namespace BotSharp.Core.Agents.Hooks;
|
||||
|
||||
public class BasicAgentHook : AgentHookBase
|
||||
{
|
||||
public override string SelfId => string.Empty;
|
||||
|
||||
public BasicAgentHook(IServiceProvider services, AgentSettings settings)
|
||||
: base(services, settings)
|
||||
{
|
||||
}
|
||||
|
||||
public override void OnAgentUtilityLoaded(Agent agent)
|
||||
{
|
||||
if (agent.Type == AgentType.Routing) return;
|
||||
|
||||
var conv = _services.GetRequiredService<IConversationService>();
|
||||
var isConvMode = conv.IsConversationMode();
|
||||
if (!isConvMode) return;
|
||||
|
||||
agent.Functions ??= [];
|
||||
agent.Utilities ??= [];
|
||||
|
||||
var (functions, templates) = GetUtilityContent(agent);
|
||||
|
||||
foreach (var fn in functions)
|
||||
{
|
||||
if (!agent.Functions.Any(x => x.Name.Equals(fn.Name, StringComparison.OrdinalIgnoreCase)))
|
||||
{
|
||||
agent.Functions.Add(fn);
|
||||
}
|
||||
}
|
||||
|
||||
foreach (var prompt in templates)
|
||||
{
|
||||
agent.Instruction += $"\r\n\r\n{prompt}\r\n\r\n";
|
||||
}
|
||||
}
|
||||
|
||||
private (IEnumerable<FunctionDef>, IEnumerable<string>) GetUtilityContent(Agent agent)
|
||||
{
|
||||
var db = _services.GetRequiredService<IBotSharpRepository>();
|
||||
var (functionNames, templateNames) = GetUniqueContent(agent.Utilities);
|
||||
|
||||
if (agent.MergeUtility)
|
||||
{
|
||||
var routing = _services.GetRequiredService<IRoutingContext>();
|
||||
var entryAgentId = routing.EntryAgentId;
|
||||
if (!string.IsNullOrEmpty(entryAgentId))
|
||||
{
|
||||
var entryAgent = db.GetAgent(entryAgentId, basicsOnly: true);
|
||||
var (fns, tps) = GetUniqueContent(entryAgent?.Utilities);
|
||||
functionNames = functionNames.Concat(fns).Distinct().ToList();
|
||||
templateNames = templateNames.Concat(tps).Distinct().ToList();
|
||||
}
|
||||
}
|
||||
|
||||
var ua = db.GetAgent(BuiltInAgentId.UtilityAssistant);
|
||||
var functions = ua?.Functions?.Where(x => functionNames.Contains(x.Name, StringComparer.OrdinalIgnoreCase))?.ToList() ?? [];
|
||||
var templates = ua?.Templates?.Where(x => templateNames.Contains(x.Name, StringComparer.OrdinalIgnoreCase))?.Select(x => x.Content)?.ToList() ?? [];
|
||||
return (functions, templates);
|
||||
}
|
||||
|
||||
private (IEnumerable<string>, IEnumerable<string>) GetUniqueContent(IEnumerable<AgentUtility>? utilities)
|
||||
{
|
||||
if (utilities.IsNullOrEmpty())
|
||||
{
|
||||
return ([], []);
|
||||
}
|
||||
|
||||
var prefix = "util-";
|
||||
utilities = utilities?.Where(x => !string.IsNullOrEmpty(x.Name) && !x.Disabled)?.ToList() ?? [];
|
||||
var functionNames = utilities.SelectMany(x => x.Functions)
|
||||
.Where(x => !string.IsNullOrEmpty(x.Name) && x.Name.StartsWith(prefix))
|
||||
.Select(x => x.Name)
|
||||
.Distinct().ToList();
|
||||
var templateNames = utilities.SelectMany(x => x.Templates)
|
||||
.Where(x => !string.IsNullOrEmpty(x.Name) && x.Name.StartsWith(prefix))
|
||||
.Select(x => x.Name)
|
||||
.Distinct().ToList();
|
||||
|
||||
return (functionNames, templateNames);
|
||||
}
|
||||
}
|
||||
|
|
@ -5,7 +5,7 @@ namespace BotSharp.Core.Agents.Services;
|
|||
|
||||
public partial class AgentService
|
||||
{
|
||||
public static ConcurrentDictionary<string, Dictionary<string,string>> AgentParameterTypes = new();
|
||||
public static ConcurrentDictionary<string, Dictionary<string, string>> AgentParameterTypes = new();
|
||||
|
||||
[MemoryCache(10 * 60, perInstanceCache: true)]
|
||||
public async Task<Agent> LoadAgent(string id)
|
||||
|
|
@ -67,6 +67,7 @@ public partial class AgentService
|
|||
hook.OnSamplesLoaded(agent.Samples);
|
||||
}
|
||||
|
||||
hook.OnAgentUtilityLoaded(agent);
|
||||
hook.OnAgentLoaded(agent);
|
||||
}
|
||||
|
||||
|
|
@ -105,14 +106,14 @@ public partial class AgentService
|
|||
{
|
||||
var agentId = agent.Id ?? agent.Name;
|
||||
if (AgentParameterTypes.ContainsKey(agentId)) return;
|
||||
|
||||
|
||||
AddOrUpdateRoutesParameters(agentId, agent.RoutingRules);
|
||||
AddOrUpdateFunctionsParameters(agentId, agent.Functions);
|
||||
}
|
||||
|
||||
private void AddOrUpdateRoutesParameters(string agentId, List<RoutingRule> routingRules)
|
||||
{
|
||||
if(!AgentParameterTypes.TryGetValue(agentId, out var parameterTypes))
|
||||
if (!AgentParameterTypes.TryGetValue(agentId, out var parameterTypes))
|
||||
{
|
||||
parameterTypes = new();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,13 +9,16 @@ public partial class AgentService
|
|||
public string RenderedInstruction(Agent agent)
|
||||
{
|
||||
var render = _services.GetRequiredService<ITemplateRender>();
|
||||
// update states
|
||||
var conv = _services.GetRequiredService<IConversationService>();
|
||||
|
||||
// update states
|
||||
foreach (var t in conv.States.GetStates())
|
||||
{
|
||||
agent.TemplateDict[t.Key] = t.Value;
|
||||
}
|
||||
return render.Render(agent.Instruction, agent.TemplateDict);
|
||||
|
||||
var res = render.Render(agent.Instruction, agent.TemplateDict);
|
||||
return res;
|
||||
}
|
||||
|
||||
public bool RenderFunction(Agent agent, FunctionDef def)
|
||||
|
|
@ -108,16 +111,18 @@ public partial class AgentService
|
|||
|
||||
public string RenderedTemplate(Agent agent, string templateName)
|
||||
{
|
||||
// render liquid template
|
||||
var render = _services.GetRequiredService<ITemplateRender>();
|
||||
var template = agent.Templates.First(x => x.Name == templateName).Content;
|
||||
// update states
|
||||
var conv = _services.GetRequiredService<IConversationService>();
|
||||
var render = _services.GetRequiredService<ITemplateRender>();
|
||||
|
||||
var template = agent.Templates.First(x => x.Name == templateName).Content;
|
||||
|
||||
// update states
|
||||
foreach (var t in conv.States.GetStates())
|
||||
{
|
||||
agent.TemplateDict[t.Key] = t.Value;
|
||||
}
|
||||
|
||||
// render liquid template
|
||||
var content = render.Render(template, agent.TemplateDict);
|
||||
|
||||
HookEmitter.Emit<IContentGeneratingHook>(_services, async hook =>
|
||||
|
|
@ -126,4 +131,4 @@ public partial class AgentService
|
|||
|
||||
return content;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,5 +1,4 @@
|
|||
using BotSharp.Abstraction.Repositories.Enums;
|
||||
using BotSharp.Abstraction.Routing.Models;
|
||||
using BotSharp.Abstraction.Users.Enums;
|
||||
using BotSharp.Abstraction.Users.Models;
|
||||
using System.IO;
|
||||
|
|
@ -28,16 +27,18 @@ public partial class AgentService
|
|||
record.Description = agent.Description ?? string.Empty;
|
||||
record.IsPublic = agent.IsPublic;
|
||||
record.Disabled = agent.Disabled;
|
||||
record.MergeUtility = agent.MergeUtility;
|
||||
record.MaxMessageCount = agent.MaxMessageCount;
|
||||
record.Type = agent.Type;
|
||||
record.Profiles = agent.Profiles ?? new List<string>();
|
||||
record.RoutingRules = agent.RoutingRules ?? new List<RoutingRule>();
|
||||
record.Profiles = agent.Profiles ?? [];
|
||||
record.RoutingRules = agent.RoutingRules ?? [];
|
||||
record.Instruction = agent.Instruction ?? string.Empty;
|
||||
record.ChannelInstructions = agent.ChannelInstructions ?? new List<ChannelInstruction>();
|
||||
record.Functions = agent.Functions ?? new List<FunctionDef>();
|
||||
record.Templates = agent.Templates ?? new List<AgentTemplate>();
|
||||
record.Responses = agent.Responses ?? new List<AgentResponse>();
|
||||
record.Samples = agent.Samples ?? new List<string>();
|
||||
record.Utilities = agent.Utilities ?? new List<string>();
|
||||
record.ChannelInstructions = agent.ChannelInstructions ?? [];
|
||||
record.Functions = agent.Functions ?? [];
|
||||
record.Templates = agent.Templates ?? [];
|
||||
record.Responses = agent.Responses ?? [];
|
||||
record.Samples = agent.Samples ?? [];
|
||||
record.Utilities = agent.Utilities ?? [];
|
||||
if (agent.LlmConfig != null && !agent.LlmConfig.IsInherit)
|
||||
{
|
||||
record.LlmConfig = agent.LlmConfig;
|
||||
|
|
@ -90,6 +91,7 @@ public partial class AgentService
|
|||
.SetDescription(foundAgent.Description)
|
||||
.SetIsPublic(foundAgent.IsPublic)
|
||||
.SetDisabled(foundAgent.Disabled)
|
||||
.SetMergeUtility(foundAgent.MergeUtility)
|
||||
.SetAgentType(foundAgent.Type)
|
||||
.SetProfiles(foundAgent.Profiles)
|
||||
.SetRoutingRules(foundAgent.RoutingRules)
|
||||
|
|
|
|||
|
|
@ -57,14 +57,14 @@ public partial class AgentService : IAgentService
|
|||
return userAgents;
|
||||
}
|
||||
|
||||
public IEnumerable<string> GetAgentUtilities()
|
||||
public IEnumerable<AgentUtility> GetAgentUtilityOptions()
|
||||
{
|
||||
var utilities = new List<string>();
|
||||
var utilities = new List<AgentUtility>();
|
||||
var hooks = _services.GetServices<IAgentUtilityHook>();
|
||||
foreach (var hook in hooks)
|
||||
{
|
||||
hook.AddUtilities(utilities);
|
||||
}
|
||||
return utilities.Where(x => !string.IsNullOrWhiteSpace(x)).Distinct().OrderBy(x => x).ToList();
|
||||
return utilities.Where(x => !string.IsNullOrWhiteSpace(x.Name)).OrderBy(x => x.Name).ToList();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -59,6 +59,11 @@
|
|||
|
||||
<ItemGroup>
|
||||
<None Remove="data\agents\01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a\templates\database_knowledge.liquid" />
|
||||
<None Remove="data\agents\01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a\templates\reasoner.hf.liquid" />
|
||||
<None Remove="data\agents\01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a\templates\reasoner.naive.liquid" />
|
||||
<None Remove="data\agents\01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a\templates\reasoner.one-step-forward.liquid" />
|
||||
<None Remove="data\agents\01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a\templates\reasoner.sequential.get_remaining_task.liquid" />
|
||||
<None Remove="data\agents\01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a\templates\reasoner.sequential.liquid" />
|
||||
<None Remove="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\agent.json" />
|
||||
<None Remove="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\instructions\instruction.liquid" />
|
||||
<None Remove="data\agents\01dcc3e5-0af7-49e6-ad7a-a760bd12dc4b\agent.json" />
|
||||
|
|
@ -73,10 +78,6 @@
|
|||
<None Remove="data\agents\01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a\instructions\instruction.liquid" />
|
||||
<None Remove="data\agents\01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a\templates\.welcome.liquid" />
|
||||
<None Remove="data\agents\01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a\templates\conversation.summary.liquid" />
|
||||
<None Remove="data\agents\01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a\templates\planner_prompt.hf.liquid" />
|
||||
<None Remove="data\agents\01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a\templates\planner_prompt.naive.liquid" />
|
||||
<None Remove="data\agents\01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a\templates\planner_prompt.sequential.get_remaining_task.liquid" />
|
||||
<None Remove="data\agents\01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a\templates\planner_prompt.sequential.liquid" />
|
||||
<None Remove="data\agents\01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a\templates\response_with_function.liquid" />
|
||||
<None Remove="data\agents\01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a\templates\translation_prompt.liquid" />
|
||||
<None Remove="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\templates\select_file_prompt.liquid" />
|
||||
|
|
@ -120,16 +121,19 @@
|
|||
<Content Include="data\agents\01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a\templates\database_knowledge.liquid">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</Content>
|
||||
<Content Include="data\agents\01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a\templates\planner_prompt.sequential.get_remaining_task.liquid">
|
||||
<Content Include="data\agents\01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a\templates\reasoner.sequential.get_remaining_task.liquid">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</Content>
|
||||
<Content Include="data\agents\01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a\templates\planner_prompt.sequential.liquid">
|
||||
<Content Include="data\agents\01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a\templates\reasoner.sequential.liquid">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</Content>
|
||||
<Content Include="data\agents\01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a\templates\planner_prompt.hf.liquid">
|
||||
<Content Include="data\agents\01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a\templates\reasoner.hf.liquid">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</Content>
|
||||
<Content Include="data\agents\01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a\templates\planner_prompt.naive.liquid">
|
||||
<Content Include="data\agents\01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a\templates\reasoner.naive.liquid">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</Content>
|
||||
<Content Include="data\agents\01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a\templates\reasoner.one-step-forward.liquid">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</Content>
|
||||
<Content Include="data\agents\01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a\templates\response_with_function.liquid">
|
||||
|
|
@ -186,7 +190,7 @@
|
|||
<ItemGroup>
|
||||
<PackageReference Include="Aspects.Cache" Version="2.0.4" />
|
||||
<PackageReference Include="DistributedLock.Redis" Version="1.0.3" />
|
||||
<PackageReference Include="EntityFrameworkCore.BootKit" Version="8.5.1" />
|
||||
<PackageReference Include="EntityFrameworkCore.BootKit" Version="8.6.0" />
|
||||
<PackageReference Include="Fluid.Core" Version="2.11.1" />
|
||||
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="8.0.1" />
|
||||
<PackageReference Include="Microsoft.Extensions.Http" Version="8.0.0" />
|
||||
|
|
|
|||
|
|
@ -12,6 +12,8 @@ using BotSharp.Core.Processors;
|
|||
using StackExchange.Redis;
|
||||
using BotSharp.Core.Infrastructures.Events;
|
||||
using BotSharp.Core.Roles.Services;
|
||||
using BotSharp.Abstraction.Templating;
|
||||
using BotSharp.Core.Templating;
|
||||
|
||||
namespace BotSharp.Core;
|
||||
|
||||
|
|
@ -23,7 +25,9 @@ public static class BotSharpCoreExtensions
|
|||
config.Bind("Interpreter", interpreterSettings);
|
||||
services.AddSingleton(x => interpreterSettings);
|
||||
|
||||
services.AddSingleton<DistributedLocker>();
|
||||
services.AddSingleton<IDistributedLocker, DistributedLocker>();
|
||||
// Register template render
|
||||
services.AddSingleton<ITemplateRender, TemplateRender>();
|
||||
|
||||
services.AddScoped<ISettingService, SettingService>();
|
||||
services.AddScoped<IRoleService, RoleService>();
|
||||
|
|
|
|||
|
|
@ -1,13 +1,13 @@
|
|||
using BotSharp.Abstraction.Google.Settings;
|
||||
using BotSharp.Abstraction.Instructs;
|
||||
using BotSharp.Abstraction.Messaging;
|
||||
using BotSharp.Abstraction.Planning;
|
||||
using BotSharp.Abstraction.Plugins.Models;
|
||||
using BotSharp.Abstraction.Routing.Planning;
|
||||
using BotSharp.Abstraction.Settings;
|
||||
using BotSharp.Abstraction.Templating;
|
||||
using BotSharp.Core.Instructs;
|
||||
using BotSharp.Core.Messaging;
|
||||
using BotSharp.Core.Routing.Planning;
|
||||
using BotSharp.Core.Routing.Reasoning;
|
||||
using BotSharp.Core.Templating;
|
||||
using BotSharp.Core.Translation;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
|
|
@ -30,6 +30,8 @@ public class ConversationPlugin : IBotSharpPlugin
|
|||
services.AddScoped(provider =>
|
||||
{
|
||||
var settingService = provider.GetRequiredService<ISettingService>();
|
||||
var render = provider.GetRequiredService<ITemplateRender>();
|
||||
render.Register(typeof(ConversationSetting));
|
||||
return settingService.Bind<ConversationSetting>("Conversation");
|
||||
});
|
||||
|
||||
|
|
@ -48,8 +50,6 @@ public class ConversationPlugin : IBotSharpPlugin
|
|||
// Rich content messaging
|
||||
services.AddScoped<IRichContentService, RichContentService>();
|
||||
|
||||
// Register template render
|
||||
services.AddSingleton<ITemplateRender, TemplateRender>();
|
||||
services.AddScoped<IResponseTemplateService, ResponseTemplateService>();
|
||||
|
||||
services.AddScoped<IExecutor, InstructExecutor>();
|
||||
|
|
|
|||
|
|
@ -28,7 +28,7 @@ public partial class ConversationService
|
|||
var dialogs = conv.GetDialogHistory();
|
||||
|
||||
var statistics = _services.GetRequiredService<ITokenStatistics>();
|
||||
var hooks = _services.GetServices<IConversationHook>().ToList();
|
||||
var hookProvider = _services.GetRequiredService<ConversationHookProvider>();
|
||||
|
||||
RoleDialogModel response = message;
|
||||
bool stopCompletion = false;
|
||||
|
|
@ -44,9 +44,7 @@ public partial class ConversationService
|
|||
message.Payload = replyMessage.Payload;
|
||||
}
|
||||
|
||||
// Before chat completion hook
|
||||
hooks = ReOrderConversationHooks(hooks);
|
||||
foreach (var hook in hooks)
|
||||
foreach (var hook in hookProvider.HooksOrderByPriority)
|
||||
{
|
||||
hook.SetAgent(agent)
|
||||
.SetConversation(conversation);
|
||||
|
|
@ -173,18 +171,4 @@ public partial class ConversationService
|
|||
// Add to dialog history
|
||||
_storage.Append(_conversationId, response);
|
||||
}
|
||||
|
||||
private List<IConversationHook> ReOrderConversationHooks(List<IConversationHook> hooks)
|
||||
{
|
||||
var target = "ChatHubConversationHook";
|
||||
var chathub = hooks.FirstOrDefault(x => x.GetType().Name == target);
|
||||
var otherHooks = hooks.Where(x => x.GetType().Name != target).ToList();
|
||||
|
||||
if (chathub != null)
|
||||
{
|
||||
var newHooks = new List<IConversationHook> { chathub }.Concat(otherHooks);
|
||||
return newHooks.ToList();
|
||||
}
|
||||
return hooks;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,7 +9,7 @@ public partial class ConversationService : IConversationService
|
|||
var deleteMessageIds = db.TruncateConversation(conversationId, messageId, cleanLog: true);
|
||||
fileStorage.DeleteMessageFiles(conversationId, deleteMessageIds, messageId, newMessageId);
|
||||
|
||||
var hooks = _services.GetServices<IConversationHook>().ToList();
|
||||
var hooks = _services.GetServices<IConversationHook>();
|
||||
foreach (var hook in hooks)
|
||||
{
|
||||
await hook.OnMessageDeleted(conversationId, messageId);
|
||||
|
|
|
|||
|
|
@ -31,9 +31,9 @@ public partial class ConversationService : IConversationService
|
|||
states.CleanStates(excludedStates);
|
||||
}
|
||||
|
||||
var hooks = _services.GetServices<IConversationHook>()
|
||||
.OrderBy(x => x.Priority)
|
||||
.ToList();
|
||||
var hooks = _services
|
||||
.GetRequiredService<ConversationHookProvider>()
|
||||
.HooksOrderByPriority;
|
||||
|
||||
// Before executing functions
|
||||
foreach (var hook in hooks)
|
||||
|
|
|
|||
|
|
@ -51,6 +51,14 @@ public partial class ConversationService : IConversationService
|
|||
return conversation;
|
||||
}
|
||||
|
||||
public async Task<Conversation> UpdateConversationTitleAlias(string id, string titleAlias)
|
||||
{
|
||||
var db = _services.GetRequiredService<IBotSharpRepository>();
|
||||
db.UpdateConversationTitleAlias(id, titleAlias);
|
||||
var conversation = db.GetConversation(id);
|
||||
return conversation;
|
||||
}
|
||||
|
||||
public async Task<bool> UpdateConversationTags(string conversationId, List<string> tags)
|
||||
{
|
||||
var db = _services.GetRequiredService<IBotSharpRepository>();
|
||||
|
|
@ -103,7 +111,8 @@ public partial class ConversationService : IConversationService
|
|||
|
||||
db.CreateNewConversation(record);
|
||||
|
||||
var hooks = _services.GetServices<IConversationHook>().ToList();
|
||||
var hooks = _services.GetServices<IConversationHook>();
|
||||
|
||||
foreach (var hook in hooks)
|
||||
{
|
||||
// If user connect agent first time
|
||||
|
|
@ -153,7 +162,10 @@ public partial class ConversationService : IConversationService
|
|||
}
|
||||
}
|
||||
|
||||
return dialogs.TakeLast(lastCount).ToList();
|
||||
var agentMsgCount = GetAgentMessageCount();
|
||||
var count = agentMsgCount.HasValue && agentMsgCount.Value > 0 ? agentMsgCount.Value : lastCount;
|
||||
|
||||
return dialogs.TakeLast(count).ToList();
|
||||
}
|
||||
|
||||
public void SetConversationId(string conversationId, List<MessageState> states, bool isReadOnly = false)
|
||||
|
|
@ -173,12 +185,14 @@ public partial class ConversationService : IConversationService
|
|||
var state = _services.GetRequiredService<IConversationStateService>();
|
||||
var channel = state.GetState("channel");
|
||||
var channelId = state.GetState("channel_id");
|
||||
var userId = state.GetState("current_user_id");
|
||||
var sess = new Conversation
|
||||
{
|
||||
Id = _conversationId,
|
||||
Channel = channel,
|
||||
ChannelId = channelId,
|
||||
AgentId = agentId
|
||||
AgentId = agentId,
|
||||
UserId = userId,
|
||||
};
|
||||
converation = await NewConversation(sess);
|
||||
}
|
||||
|
|
@ -190,4 +204,16 @@ public partial class ConversationService : IConversationService
|
|||
{
|
||||
return !string.IsNullOrWhiteSpace(_conversationId);
|
||||
}
|
||||
|
||||
|
||||
private int? GetAgentMessageCount()
|
||||
{
|
||||
var db = _services.GetRequiredService<IBotSharpRepository>();
|
||||
var routingCtx = _services.GetRequiredService<IRoutingContext>();
|
||||
|
||||
if (string.IsNullOrEmpty(routingCtx.EntryAgentId)) return null;
|
||||
|
||||
var agent = db.GetAgent(routingCtx.EntryAgentId, basicsOnly: true);
|
||||
return agent?.MaxMessageCount;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,20 @@
|
|||
/*****************************************************************************
|
||||
Copyright 2024 Written by Jicheng Lu. All Rights Reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
******************************************************************************/
|
||||
|
||||
using BotSharp.Abstraction.Conversations.Enums;
|
||||
using BotSharp.Abstraction.Users.Enums;
|
||||
|
||||
namespace BotSharp.Core.Conversations.Services;
|
||||
|
||||
|
|
@ -21,13 +36,14 @@ public class ConversationStateService : IConversationStateService, IDisposable
|
|||
/// </summary>
|
||||
private ConversationState _historyStates;
|
||||
|
||||
public ConversationStateService(ILogger<ConversationStateService> logger,
|
||||
public ConversationStateService(
|
||||
IServiceProvider services,
|
||||
IBotSharpRepository db)
|
||||
IBotSharpRepository db,
|
||||
ILogger<ConversationStateService> logger)
|
||||
{
|
||||
_logger = logger;
|
||||
_services = services;
|
||||
_db = db;
|
||||
_logger = logger;
|
||||
_curStates = new ConversationState();
|
||||
_historyStates = new ConversationState();
|
||||
}
|
||||
|
|
@ -125,8 +141,13 @@ public class ConversationStateService : IConversationStateService, IDisposable
|
|||
var curMsgId = routingCtx.MessageId;
|
||||
|
||||
_historyStates = _db.GetConversationStates(conversationId);
|
||||
|
||||
var endNodes = new Dictionary<string, string>();
|
||||
|
||||
if (_historyStates.IsNullOrEmpty()) return endNodes;
|
||||
|
||||
var dialogs = _db.GetConversationDialogs(conversationId);
|
||||
var userDialogs = dialogs.Where(x => x.MetaData?.Role == AgentRole.User || x.MetaData?.Role == UserRole.User)
|
||||
var userDialogs = dialogs.Where(x => x.MetaData?.Role == AgentRole.User)
|
||||
.GroupBy(x => x.MetaData?.MessageId)
|
||||
.Select(g => g.First())
|
||||
.OrderBy(x => x.MetaData?.CreateTime)
|
||||
|
|
@ -134,9 +155,6 @@ public class ConversationStateService : IConversationStateService, IDisposable
|
|||
var curMsgIndex = userDialogs.FindIndex(x => !string.IsNullOrEmpty(curMsgId) && x.MetaData?.MessageId == curMsgId);
|
||||
curMsgIndex = curMsgIndex < 0 ? userDialogs.Count() : curMsgIndex;
|
||||
|
||||
var endNodes = new Dictionary<string, string>();
|
||||
if (_historyStates.IsNullOrEmpty()) return endNodes;
|
||||
|
||||
foreach (var state in _historyStates)
|
||||
{
|
||||
var key = state.Key;
|
||||
|
|
|
|||
|
|
@ -15,45 +15,45 @@ public class EvaluationConversationHook : ConversationHookBase
|
|||
|
||||
public override Task OnMessageReceived(RoleDialogModel message)
|
||||
{
|
||||
if (_conversation != null && _convSettings.EnableExecutionLog)
|
||||
if (Conversation != null && _convSettings.EnableExecutionLog)
|
||||
{
|
||||
_logger.Append(_conversation.Id, $"[{DateTime.Now}] {message.Role}: {message.Content}");
|
||||
_logger.Append(Conversation.Id, $"[{DateTime.Now}] {message.Role}: {message.Content}");
|
||||
}
|
||||
return base.OnMessageReceived(message);
|
||||
}
|
||||
|
||||
public override Task OnFunctionExecuted(RoleDialogModel message)
|
||||
{
|
||||
if (_conversation != null && _convSettings.EnableExecutionLog)
|
||||
if (Conversation != null && _convSettings.EnableExecutionLog)
|
||||
{
|
||||
_logger.Append(_conversation.Id, $"[{DateTime.Now}] {message.Role}: {message.FunctionName}({message.FunctionArgs}) => {message.Content}");
|
||||
_logger.Append(Conversation.Id, $"[{DateTime.Now}] {message.Role}: {message.FunctionName}({message.FunctionArgs}) => {message.Content}");
|
||||
}
|
||||
return base.OnFunctionExecuted(message);
|
||||
}
|
||||
|
||||
public override Task OnResponseGenerated(RoleDialogModel message)
|
||||
{
|
||||
if (_conversation != null && _convSettings.EnableExecutionLog)
|
||||
if (Conversation != null && _convSettings.EnableExecutionLog)
|
||||
{
|
||||
_logger.Append(_conversation.Id, $"[{DateTime.Now}] {message.Role}: {message.Content}");
|
||||
}
|
||||
_logger.Append(Conversation.Id, $"[{DateTime.Now}] {message.Role}: {message.Content}");
|
||||
}
|
||||
return base.OnResponseGenerated(message);
|
||||
}
|
||||
|
||||
public override Task OnHumanInterventionNeeded(RoleDialogModel message)
|
||||
{
|
||||
if (_conversation != null && _convSettings.EnableExecutionLog)
|
||||
if (Conversation != null && _convSettings.EnableExecutionLog)
|
||||
{
|
||||
_logger.Append(_conversation.Id, $"[{DateTime.Now}] {AgentRole.Function}: trigger_event({{\"event\": \"{message.FunctionName}\"}})");
|
||||
_logger.Append(Conversation.Id, $"[{DateTime.Now}] {AgentRole.Function}: trigger_event({{\"event\": \"{message.FunctionName}\"}})");
|
||||
}
|
||||
return base.OnHumanInterventionNeeded(message);
|
||||
}
|
||||
|
||||
public override Task OnConversationEnding(RoleDialogModel message)
|
||||
{
|
||||
if (_conversation != null && _convSettings.EnableExecutionLog)
|
||||
if (Conversation != null && _convSettings.EnableExecutionLog)
|
||||
{
|
||||
_logger.Append(_conversation.Id, $"[{DateTime.Now}] {AgentRole.Function}: trigger_event({{\"event\": \"{message.FunctionName}\"}})");
|
||||
_logger.Append(Conversation.Id, $"[{DateTime.Now}] {AgentRole.Function}: trigger_event({{\"event\": \"{message.FunctionName}\"}})");
|
||||
}
|
||||
return base.OnConversationEnding(message);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,9 +1,10 @@
|
|||
using BotSharp.Abstraction.Infrastructures;
|
||||
using Medallion.Threading.Redis;
|
||||
using StackExchange.Redis;
|
||||
|
||||
namespace BotSharp.Core.Infrastructures;
|
||||
|
||||
public class DistributedLocker
|
||||
public class DistributedLocker : IDistributedLocker
|
||||
{
|
||||
private readonly IConnectionMultiplexer _redis;
|
||||
private readonly ILogger _logger;
|
||||
|
|
@ -14,7 +15,7 @@ public class DistributedLocker
|
|||
_logger = logger;
|
||||
}
|
||||
|
||||
public async Task<T> Lock<T>(string resource, Func<Task<T>> action, int timeoutInSeconds = 30)
|
||||
public async Task<bool> LockAsync(string resource, Func<Task> action, int timeoutInSeconds = 30)
|
||||
{
|
||||
var timeout = TimeSpan.FromSeconds(timeoutInSeconds);
|
||||
|
||||
|
|
@ -24,9 +25,11 @@ public class DistributedLocker
|
|||
if (handle == null)
|
||||
{
|
||||
_logger.LogWarning($"Acquire lock for {resource} failed due to after {timeout}s timeout.");
|
||||
return false;
|
||||
}
|
||||
|
||||
return await action();
|
||||
await action();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,3 +1,4 @@
|
|||
using BotSharp.Abstraction.Infrastructures.Enums;
|
||||
using StackExchange.Redis;
|
||||
|
||||
namespace BotSharp.Core.Infrastructures.Events;
|
||||
|
|
@ -20,14 +21,65 @@ public class RedisPublisher : IEventPublisher
|
|||
await _subscriber.PublishAsync(channel, message);
|
||||
}
|
||||
|
||||
public async Task PublishAsync(string channel, string message)
|
||||
public async Task<string?> PublishAsync(string channel, string message, EventPriority? priority = null)
|
||||
{
|
||||
var db = _redis.GetDatabase();
|
||||
|
||||
// convert to apporiate channel by priority
|
||||
if (priority != null)
|
||||
{
|
||||
channel = $"{channel}-{priority}";
|
||||
}
|
||||
|
||||
if (CheckMessageExists(db, channel, "message", message))
|
||||
{
|
||||
_logger.LogError($"The message already exists {channel} {message}");
|
||||
return null;
|
||||
}
|
||||
|
||||
// Add a message to the stream, keeping only the latest 1 million messages
|
||||
await db.StreamAddAsync(channel, "message", message,
|
||||
var messageId = await db.StreamAddAsync(channel,
|
||||
[
|
||||
new NameValueEntry("message", message),
|
||||
new NameValueEntry("timestamp", DateTime.UtcNow.ToString("o"))
|
||||
],
|
||||
maxLength: 1000 * 10000);
|
||||
|
||||
_logger.LogInformation($"Published message {channel} {message}");
|
||||
_logger.LogInformation($"Published message {channel} {message} ({messageId})");
|
||||
|
||||
return messageId;
|
||||
}
|
||||
|
||||
private bool CheckMessageExists(IDatabase db, string channel, string fieldName, string desiredValue)
|
||||
{
|
||||
// Define the range to fetch all messages
|
||||
RedisValue start = "-"; // Start from the smallest ID
|
||||
RedisValue end = "+"; // End at the largest ID
|
||||
int count = 10; // Number of messages to retrieve
|
||||
|
||||
// Fetch the latest 10 messages
|
||||
var streamEntries = db.StreamRange(channel, start, end, count, Order.Descending);
|
||||
|
||||
if (streamEntries.Length == 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Check if any message contains the specific field value
|
||||
bool exists = streamEntries.Any(entry =>
|
||||
{
|
||||
// Each entry contains a collection of Name-Value pairs
|
||||
foreach (var nameValue in entry.Values)
|
||||
{
|
||||
if (nameValue.Name == fieldName && nameValue.Value == desiredValue)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
});
|
||||
|
||||
return exists;
|
||||
}
|
||||
|
||||
public async Task ReDispatchAsync(string channel, int count = 10, string order = "asc")
|
||||
|
|
@ -41,7 +93,10 @@ public class RedisPublisher : IEventPublisher
|
|||
|
||||
try
|
||||
{
|
||||
var messageId = await db.StreamAddAsync(channel, "message", entry.Values[0].Value);
|
||||
var messageId = await db.StreamAddAsync(channel, [
|
||||
new NameValueEntry("message", entry.Values[0].Value),
|
||||
new NameValueEntry("timestamp", DateTime.UtcNow.ToString("o"))
|
||||
]);
|
||||
|
||||
_logger.LogWarning($"ReDispatched message: {channel} {entry.Values[0].Value} ({messageId})");
|
||||
|
||||
|
|
@ -50,7 +105,78 @@ public class RedisPublisher : IEventPublisher
|
|||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError($"Error processing message: {ex.Message}, event id: {channel} {entry.Id}");
|
||||
_logger.LogError($"Error processing message: {ex.Message}, event id: {channel} {entry.Id}\r\n{ex}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public async Task ReDispatchPendingAsync(string channel, string group, int count = 10)
|
||||
{
|
||||
var db = _redis.GetDatabase();
|
||||
|
||||
// Step 1: Fetch pending messages using XPending
|
||||
try
|
||||
{
|
||||
var pendingInfo = await db.StreamPendingAsync(channel, group);
|
||||
|
||||
if (pendingInfo.PendingMessageCount == 0)
|
||||
{
|
||||
Console.WriteLine("No pending messages found.");
|
||||
return;
|
||||
}
|
||||
|
||||
// Step 2: Fetch pending message IDs
|
||||
List<StreamEntry> pendingMessages = new List<StreamEntry>();
|
||||
foreach (var consumer in pendingInfo.Consumers)
|
||||
{
|
||||
var pendingMessageIds = await db.StreamPendingMessagesAsync(channel, group, count: consumer.PendingMessageCount, consumerName: consumer.Name);
|
||||
var messageIds = pendingMessageIds.Select(x => x.MessageId).ToArray();
|
||||
// Step 3: Use XClaim to fetch the actual message
|
||||
var claimedMessages = await db.StreamClaimAsync(channel, group, consumer.Name, minIdleTimeInMs: 60 * 1000, messageIds);
|
||||
pendingMessages.AddRange(claimedMessages);
|
||||
|
||||
await db.StreamAcknowledgeAsync(channel, group, messageIds);
|
||||
}
|
||||
|
||||
// Step 4: Process the messages
|
||||
foreach (var message in pendingMessages)
|
||||
{
|
||||
/*if (message.IsNull)
|
||||
{
|
||||
await db.StreamAcknowledgeAsync(channel, group, [message.Id]);
|
||||
}
|
||||
else
|
||||
{
|
||||
var messageId = await db.StreamAddAsync(channel, "message", message.Values[0].Value);
|
||||
_logger.LogWarning($"ReDispatched message: {channel} {message.Values[0].Value} ({messageId})");
|
||||
await db.StreamDeleteAsync(channel, [messageId]);
|
||||
}*/
|
||||
}
|
||||
}
|
||||
catch (RedisException ex)
|
||||
{
|
||||
Console.WriteLine($"Redis error: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
public async Task RemoveAsync(string channel, int count = 10)
|
||||
{
|
||||
var db = _redis.GetDatabase();
|
||||
|
||||
var entries = await db.StreamRangeAsync(channel, "-", "+", count: count, messageOrder: Order.Ascending);
|
||||
foreach (var entry in entries)
|
||||
{
|
||||
_logger.LogInformation($"Fetched message: {channel} {entry.Values[0].Value} ({entry.Id})");
|
||||
|
||||
try
|
||||
{
|
||||
await db.StreamDeleteAsync(channel, [entry.Id]);
|
||||
|
||||
_logger.LogWarning($"Deleted message: {channel} {entry.Values[0].Value} ({entry.Id})");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError($"Error processing message: {ex.Message}, event id: {channel} {entry.Id}\r\n{ex}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,3 +1,4 @@
|
|||
using BotSharp.Abstraction.Infrastructures.Enums;
|
||||
using StackExchange.Redis;
|
||||
|
||||
namespace BotSharp.Core.Infrastructures.Events;
|
||||
|
|
@ -24,10 +25,86 @@ public class RedisSubscriber : IEventSubscriber
|
|||
});
|
||||
}
|
||||
|
||||
public async Task SubscribeAsync(string channel, string group, Func<string, string, Task> received)
|
||||
public async Task SubscribeAsync(string channel, string group, int? port, bool priorityEnabled,
|
||||
Func<string, string, Task> received,
|
||||
CancellationToken? stoppingToken = null)
|
||||
{
|
||||
var db = _redis.GetDatabase();
|
||||
|
||||
if (priorityEnabled)
|
||||
{
|
||||
await CreateConsumerGroup(db, $"{channel}-{EventPriority.Low}", group);
|
||||
await CreateConsumerGroup(db, $"{channel}-{EventPriority.Medium}", group);
|
||||
await CreateConsumerGroup(db, $"{channel}-{EventPriority.High}", group);
|
||||
}
|
||||
else
|
||||
{
|
||||
await CreateConsumerGroup(db, channel, group);
|
||||
}
|
||||
|
||||
var consumer = Environment.MachineName;
|
||||
if (port.HasValue)
|
||||
{
|
||||
consumer += $"-{port}";
|
||||
}
|
||||
|
||||
while (true)
|
||||
{
|
||||
await Task.Delay(100);
|
||||
|
||||
if (stoppingToken.HasValue && stoppingToken.Value.IsCancellationRequested)
|
||||
{
|
||||
_logger.LogInformation($"Stopping consumer channel & group: [{channel}, {group}]");
|
||||
break;
|
||||
}
|
||||
|
||||
if (priorityEnabled)
|
||||
{
|
||||
if (await HandleGroupMessage(db, $"{channel}-{EventPriority.High}", group, consumer, received) > 0)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (await HandleGroupMessage(db, $"{channel}-{EventPriority.Medium}", group, consumer, received) > 0)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
await HandleGroupMessage(db, $"{channel}-{EventPriority.Low}", group, consumer, received);
|
||||
}
|
||||
else
|
||||
{
|
||||
await HandleGroupMessage(db, channel, group, consumer, received);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private async Task<int> HandleGroupMessage(IDatabase db, string channel, string group, string consumer, Func<string, string, Task> received)
|
||||
{
|
||||
var entries = await db.StreamReadGroupAsync(channel, group, consumer, count: 1);
|
||||
foreach (var entry in entries)
|
||||
{
|
||||
_logger.LogInformation($"Consumer {Environment.MachineName} received: {channel} {entry.Values[0].Value}");
|
||||
await db.StreamAcknowledgeAsync(channel, group, entry.Id);
|
||||
|
||||
try
|
||||
{
|
||||
await received(channel, entry.Values[0].Value);
|
||||
|
||||
// Optionally delete the message to save space
|
||||
await db.StreamDeleteAsync(channel, [entry.Id]);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError($"Error processing message: {ex.Message}, event id: {channel} {entry.Id}\r\n{ex}");
|
||||
}
|
||||
}
|
||||
|
||||
return entries.Length;
|
||||
}
|
||||
|
||||
private async Task CreateConsumerGroup(IDatabase db, string channel, string group)
|
||||
{
|
||||
// Create the consumer group if it doesn't exist
|
||||
try
|
||||
{
|
||||
|
|
@ -43,33 +120,5 @@ public class RedisSubscriber : IEventSubscriber
|
|||
_logger.LogError($"Error creating consumer group: '{group}' {ex.Message}");
|
||||
throw;
|
||||
}
|
||||
|
||||
while (true)
|
||||
{
|
||||
var entries = await db.StreamReadGroupAsync(channel, group, Environment.MachineName, count: 1);
|
||||
foreach (var entry in entries)
|
||||
{
|
||||
_logger.LogInformation($"Consumer {Environment.MachineName} received: {channel} {entry.Values[0].Value}");
|
||||
|
||||
try
|
||||
{
|
||||
await received(channel, entry.Values[0].Value);
|
||||
|
||||
// Optionally delete the message to save space
|
||||
await db.StreamDeleteAsync(channel, [entry.Id]);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError($"Error processing message: {ex.Message}, event id: {channel} {entry.Id}");
|
||||
}
|
||||
finally
|
||||
{
|
||||
await db.StreamAcknowledgeAsync(channel, group, entry.Id);
|
||||
}
|
||||
}
|
||||
|
||||
await Task.Delay(Random.Shared.Next(1, 11) * 100);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -15,7 +15,7 @@ public static class HookEmitter
|
|||
{
|
||||
try
|
||||
{
|
||||
logger.LogInformation($"Emit hook action on {action.Method.Name}({hook.GetType().Name})");
|
||||
logger.LogDebug($"Emit hook action on {action.Method.Name}({hook.GetType().Name})");
|
||||
action(hook);
|
||||
|
||||
if (option.OnlyOnce)
|
||||
|
|
@ -43,7 +43,7 @@ public static class HookEmitter
|
|||
{
|
||||
try
|
||||
{
|
||||
logger.LogInformation($"Emit hook action on {action.Method.Name}({hook.GetType().Name})");
|
||||
logger.LogDebug($"Emit hook action on {action.Method.Name}({hook.GetType().Name})");
|
||||
await action(hook);
|
||||
|
||||
if (option.OnlyOnce)
|
||||
|
|
|
|||
|
|
@ -23,17 +23,23 @@ public static class Utilities
|
|||
|
||||
var data = sha256.ComputeHash(Encoding.UTF8.GetBytes(text));
|
||||
var sb = new StringBuilder();
|
||||
foreach(var c in data)
|
||||
foreach (var c in data)
|
||||
{
|
||||
sb.Append(c.ToString("x2"));
|
||||
}
|
||||
return sb.ToString();
|
||||
}
|
||||
|
||||
public static (string, string) SplitAsTuple(this string str, string sep)
|
||||
public static (string, string, string) SplitAsTuple(this string str, string sep)
|
||||
{
|
||||
var splits = str.Split(sep);
|
||||
return (splits[0], splits[1]);
|
||||
|
||||
if (splits.Length == 2 || string.IsNullOrWhiteSpace(splits[2]))
|
||||
{
|
||||
return (splits[0], splits[1], "CN");
|
||||
}
|
||||
|
||||
return (splits[0], splits[1], splits[2]);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
@ -47,4 +53,36 @@ public static class Utilities
|
|||
memcache.Compact(100);
|
||||
}
|
||||
}
|
||||
|
||||
public static string HideMiddleDigits(string input, bool isEmail = false)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(input))
|
||||
{
|
||||
return input;
|
||||
}
|
||||
|
||||
if (isEmail)
|
||||
{
|
||||
int atIndex = input.IndexOf('@');
|
||||
if (atIndex > 1)
|
||||
{
|
||||
string localPart = input.Substring(0, atIndex);
|
||||
if (localPart.Length > 2)
|
||||
{
|
||||
string maskedLocalPart = $"{localPart[0]}{new string('*', localPart.Length - 2)}{localPart[^1]}";
|
||||
return $"{maskedLocalPart}@{input.Substring(atIndex + 1)}";
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (input.Length > 6)
|
||||
{
|
||||
return $"{input.Substring(0, 3)}{new string('*', input.Length - 6)}{input.Substring(input.Length - 3)}";
|
||||
}
|
||||
}
|
||||
|
||||
return input;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||