add memory
All checks were successful
Build and Publish Docker Image / build-and-push (push) Successful in 16s
All checks were successful
Build and Publish Docker Image / build-and-push (push) Successful in 16s
This commit is contained in:
55
Services/Tools/ToolRegistry.cs
Normal file
55
Services/Tools/ToolRegistry.cs
Normal file
@@ -0,0 +1,55 @@
|
||||
using System.Text.Json;
|
||||
|
||||
namespace TentacleBot.Services.Tools;
|
||||
|
||||
public class ToolRegistry
|
||||
{
|
||||
private readonly Dictionary<string, ToolDefinition> _tools = new();
|
||||
|
||||
public void Register(string name, string description, Dictionary<string, object> parameters, Func<string, Task<string>> handler)
|
||||
{
|
||||
_tools[name] = new ToolDefinition(name, description, parameters, handler);
|
||||
}
|
||||
|
||||
public List<object> BuildToolDefinitions()
|
||||
{
|
||||
return _tools.Values.Select(t =>
|
||||
{
|
||||
var functionDef = new Dictionary<string, object>
|
||||
{
|
||||
["name"] = t.Name,
|
||||
["description"] = t.Description,
|
||||
["parameters"] = t.Parameters
|
||||
};
|
||||
|
||||
return (object)new Dictionary<string, object>
|
||||
{
|
||||
["type"] = "function",
|
||||
["function"] = functionDef
|
||||
};
|
||||
}).ToList();
|
||||
}
|
||||
|
||||
public async Task<string> ExecuteAsync(string toolName, string argumentsJson)
|
||||
{
|
||||
if (!_tools.TryGetValue(toolName, out var tool))
|
||||
return "Error: unknown tool";
|
||||
|
||||
try
|
||||
{
|
||||
return await tool.Handler(argumentsJson);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return $"Error executing tool '{toolName}': {ex.Message}";
|
||||
}
|
||||
}
|
||||
|
||||
public bool HasTool(string name) => _tools.ContainsKey(name);
|
||||
|
||||
private record ToolDefinition(
|
||||
string Name,
|
||||
string Description,
|
||||
Dictionary<string, object> Parameters,
|
||||
Func<string, Task<string>> Handler);
|
||||
}
|
||||
Reference in New Issue
Block a user