Add SetVolumeTool (validates 0..100, delegates to Pi via IDeviceChannel)
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
using System.Text.Json;
|
||||
|
||||
namespace backend.Tools;
|
||||
|
||||
public class SetVolumeTool : ITool
|
||||
{
|
||||
public string Name => "set_volume";
|
||||
public string Description =>
|
||||
"Sets the assistant's audio output volume on the device. Level is 0..100.";
|
||||
public bool RunsDuringResponse => true;
|
||||
|
||||
public JsonElement ParameterSchema { get; } = JsonDocument.Parse(
|
||||
"""
|
||||
{
|
||||
"type":"object",
|
||||
"properties":{"level":{"type":"integer","minimum":0,"maximum":100}},
|
||||
"required":["level"]
|
||||
}
|
||||
""").RootElement;
|
||||
|
||||
public async Task<ToolResult> ExecuteAsync(ToolInvocation invocation, DeviceContext device, CancellationToken ct)
|
||||
{
|
||||
if (!invocation.Arguments.TryGetProperty("level", out var lvlEl)
|
||||
|| !lvlEl.TryGetInt32(out var level)
|
||||
|| level < 0 || level > 100)
|
||||
{
|
||||
var err = JsonDocument.Parse("""{"ok":false,"error":"level must be an integer in 0..100"}""").RootElement;
|
||||
return new ToolResult(err);
|
||||
}
|
||||
|
||||
var piReply = await device.Channel.CallPiToolAsync(Name, invocation.Arguments, ct);
|
||||
return new ToolResult(piReply);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user