DeviceHub: wake → session lifecycle + uplink relay + tool_result routing
This commit is contained in:
@@ -2,9 +2,13 @@ using System.Net.WebSockets;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Nodes;
|
||||
using backend.Conversations;
|
||||
using backend.Data;
|
||||
using backend.Devices;
|
||||
using backend.Realtime;
|
||||
using backend.Tools;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace backend.DeviceHub;
|
||||
|
||||
@@ -12,7 +16,13 @@ public static class DeviceHubEndpoint
|
||||
{
|
||||
public static IEndpointRouteBuilder MapDeviceHub(this IEndpointRouteBuilder app)
|
||||
{
|
||||
app.Map("/device", async (HttpContext ctx, DeviceAuth auth, DeviceRegistry reg, AppDbContext db) =>
|
||||
app.Map("/device", async (
|
||||
HttpContext ctx,
|
||||
DeviceAuth auth,
|
||||
DeviceRegistry reg,
|
||||
AppDbContext db,
|
||||
RealtimeSessionFactory sessions,
|
||||
IServiceScopeFactory scopes) =>
|
||||
{
|
||||
if (!ctx.WebSockets.IsWebSocketRequest)
|
||||
{
|
||||
@@ -39,7 +49,7 @@ public static class DeviceHubEndpoint
|
||||
|
||||
try
|
||||
{
|
||||
await HandleAsync(active, device, db, ctx.RequestAborted);
|
||||
await HandleAsync(active, device, db, sessions, scopes, ctx.RequestAborted);
|
||||
}
|
||||
finally
|
||||
{
|
||||
@@ -49,7 +59,10 @@ public static class DeviceHubEndpoint
|
||||
return app;
|
||||
}
|
||||
|
||||
private static async Task HandleAsync(ActiveDevice active, Device device, AppDbContext db, CancellationToken ct)
|
||||
private static async Task HandleAsync(
|
||||
ActiveDevice active, Device device, AppDbContext db,
|
||||
RealtimeSessionFactory sessions, IServiceScopeFactory scopes,
|
||||
CancellationToken ct)
|
||||
{
|
||||
var buffer = new byte[8192];
|
||||
var ws = active.Ws;
|
||||
@@ -68,8 +81,10 @@ public static class DeviceHubEndpoint
|
||||
|
||||
if (res.MessageType == WebSocketMessageType.Binary)
|
||||
{
|
||||
if (msg.Length != 3840) continue;
|
||||
// Binary routing into a session lives in Task 18.
|
||||
if (msg.Length == 3840 && active.CurrentSession is not null)
|
||||
{
|
||||
await active.CurrentSession.WriteUplinkAsync(msg.ToArray(), ct);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -87,10 +102,75 @@ public static class DeviceHubEndpoint
|
||||
await db.SaveChangesAsync(ct);
|
||||
await active.SendEnvelopeAsync(new PongEnvelope("pong"), ct);
|
||||
break;
|
||||
|
||||
case "wake":
|
||||
if (active.CurrentSession is null)
|
||||
await StartSessionAsync(active, device, sessions, scopes, ct);
|
||||
break;
|
||||
|
||||
case "cancel":
|
||||
active.SessionCts?.Cancel();
|
||||
break;
|
||||
|
||||
case "playback_done":
|
||||
break; // v1: client coordination only
|
||||
|
||||
case "tool_result":
|
||||
var env = JsonSerializer.Deserialize<ToolResultEnvelope>(text)!;
|
||||
active.CompleteToolResult(env);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static async Task StartSessionAsync(
|
||||
ActiveDevice active, Device device,
|
||||
RealtimeSessionFactory sessions, IServiceScopeFactory scopes,
|
||||
CancellationToken parentCt)
|
||||
{
|
||||
var enabledTools = JsonSerializer.Deserialize<string[]>(device.Config?.EnabledToolsJson ?? "[]")
|
||||
?? Array.Empty<string>();
|
||||
var cfg = new RealtimeSettings(
|
||||
Model: device.Config?.Model ?? "gpt-4o-realtime-preview",
|
||||
Voice: device.Config?.Voice ?? "alloy",
|
||||
SystemPrompt: device.Config?.SystemPrompt ?? "",
|
||||
IdleTimeoutSeconds: device.Config?.IdleTimeoutSeconds ?? 30,
|
||||
EnabledTools: new HashSet<string>(enabledTools));
|
||||
|
||||
active.SessionCts = CancellationTokenSource.CreateLinkedTokenSource(parentCt);
|
||||
var ct = active.SessionCts.Token;
|
||||
|
||||
var sessionScope = scopes.CreateAsyncScope();
|
||||
var scopedSessions = sessionScope.ServiceProvider.GetRequiredService<RealtimeSessionFactory>();
|
||||
|
||||
RealtimeSession session;
|
||||
try
|
||||
{
|
||||
session = await scopedSessions.CreateAsync(
|
||||
device.Id, cfg, active, active, ct);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
await active.SendEnvelopeAsync(
|
||||
new ErrorEnvelope("error", "upstream", ex.Message, true), parentCt);
|
||||
await sessionScope.DisposeAsync();
|
||||
return;
|
||||
}
|
||||
|
||||
active.CurrentSession = session;
|
||||
_ = Task.Run(async () =>
|
||||
{
|
||||
try { await session.RunAsync(ct); }
|
||||
finally
|
||||
{
|
||||
active.CurrentSession = null;
|
||||
active.SessionCts?.Dispose();
|
||||
active.SessionCts = null;
|
||||
await sessionScope.DisposeAsync();
|
||||
}
|
||||
}, ct);
|
||||
}
|
||||
|
||||
private static async Task SendHelloAckAsync(ActiveDevice active, Device device, CancellationToken ct)
|
||||
{
|
||||
var enabled = device.Config?.EnabledToolsJson ?? "[]";
|
||||
|
||||
Reference in New Issue
Block a user