Add ITool + ToolRegistry + GetCurrentTimeTool

This commit is contained in:
2026-06-11 21:19:23 +00:00
parent 13d8773704
commit 9325d21f9e
6 changed files with 125 additions and 0 deletions
+20
View File
@@ -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));
}
}
+8
View File
@@ -0,0 +1,8 @@
using System.Text.Json;
namespace backend.Tools;
public interface IDeviceChannel
{
Task<JsonElement> CallPiToolAsync(string name, JsonElement args, CancellationToken ct);
}
+19
View File
@@ -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);
}
+20
View File
@@ -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));
}