Add ITool + ToolRegistry + GetCurrentTimeTool
This commit is contained in:
@@ -0,0 +1,20 @@
|
||||
using System.Text.Json;
|
||||
|
||||
namespace backend.Tools;
|
||||
|
||||
public class GetCurrentTimeTool : ITool
|
||||
{
|
||||
public string Name => "get_current_time";
|
||||
public string Description => "Returns the current UTC time as an ISO-8601 string.";
|
||||
public bool RunsDuringResponse => false;
|
||||
|
||||
public JsonElement ParameterSchema { get; } =
|
||||
JsonDocument.Parse("""{"type":"object","properties":{},"required":[]}""").RootElement;
|
||||
|
||||
public Task<ToolResult> ExecuteAsync(ToolInvocation invocation, DeviceContext device, CancellationToken ct)
|
||||
{
|
||||
var now = DateTimeOffset.UtcNow.ToString("yyyy-MM-ddTHH:mm:ssZ");
|
||||
var output = JsonDocument.Parse($$"""{"now":"{{now}}"}""").RootElement;
|
||||
return Task.FromResult(new ToolResult(output));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
using System.Text.Json;
|
||||
|
||||
namespace backend.Tools;
|
||||
|
||||
public interface IDeviceChannel
|
||||
{
|
||||
Task<JsonElement> CallPiToolAsync(string name, JsonElement args, CancellationToken ct);
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
using System.Text.Json;
|
||||
|
||||
namespace backend.Tools;
|
||||
|
||||
public record DeviceContext(Guid DeviceId, Guid ConversationId, IDeviceChannel Channel);
|
||||
public record ToolInvocation(string CallId, JsonElement Arguments);
|
||||
public record ToolResult(JsonElement Output);
|
||||
|
||||
public interface ITool
|
||||
{
|
||||
string Name { get; }
|
||||
string Description { get; }
|
||||
JsonElement ParameterSchema { get; }
|
||||
bool RunsDuringResponse { get; }
|
||||
Task<ToolResult> ExecuteAsync(
|
||||
ToolInvocation invocation,
|
||||
DeviceContext device,
|
||||
CancellationToken ct);
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
namespace backend.Tools;
|
||||
|
||||
public class ToolRegistry
|
||||
{
|
||||
private readonly Dictionary<string, ITool> _byName;
|
||||
private readonly List<ITool> _all;
|
||||
|
||||
public ToolRegistry(IEnumerable<ITool> tools)
|
||||
{
|
||||
_all = tools.ToList();
|
||||
_byName = _all.ToDictionary(t => t.Name);
|
||||
}
|
||||
|
||||
public IReadOnlyList<ITool> All => _all;
|
||||
|
||||
public ITool? Get(string name) => _byName.GetValueOrDefault(name);
|
||||
|
||||
public IEnumerable<ITool> EnabledFor(IReadOnlySet<string> enabled)
|
||||
=> _all.Where(t => enabled.Contains(t.Name));
|
||||
}
|
||||
Reference in New Issue
Block a user