All checks were successful
Build and Publish Docker Image / build-and-push (push) Successful in 16s
56 lines
1.5 KiB
C#
56 lines
1.5 KiB
C#
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);
|
|
}
|