RealtimeSession: idle-timeout watchdog (resets on speech_started)
This commit is contained in:
@@ -25,6 +25,7 @@ public class RealtimeSession
|
||||
private Conversation? _conversation;
|
||||
private EndReason _endReason = EndReason.Unknown;
|
||||
private bool _endRequested;
|
||||
private DateTime _lastSpeechAt = DateTime.UtcNow;
|
||||
private readonly List<(string CallId, string Name, string ArgsJson)> _pendingCalls = new();
|
||||
|
||||
private readonly System.Threading.Channels.Channel<byte[]> _uplink =
|
||||
@@ -79,11 +80,14 @@ public class RealtimeSession
|
||||
["type"] = "session_started",
|
||||
["conversation_id"] = _conversation.Id.ToString(),
|
||||
}, ct);
|
||||
_lastSpeechAt = DateTime.UtcNow;
|
||||
|
||||
var uplinkTask = Task.Run(() => PumpUplinkAsync(ct), ct);
|
||||
var idleTask = Task.Run(() => WatchdogAsync(ct), ct);
|
||||
await PumpAsync(ct);
|
||||
_uplink.Writer.TryComplete();
|
||||
await uplinkTask;
|
||||
await idleTask;
|
||||
}
|
||||
catch (OperationCanceledException)
|
||||
{
|
||||
@@ -114,11 +118,16 @@ public class RealtimeSession
|
||||
{
|
||||
while (!ct.IsCancellationRequested && !_endRequested)
|
||||
{
|
||||
var evt = await _upstream.ReceiveJsonAsync(ct);
|
||||
if (evt is null) { _endReason = EndReason.Error; return; }
|
||||
var evt = await ReceiveOrIdleAsync(ct);
|
||||
if (evt is null && _endRequested) return;
|
||||
if (evt is null && ct.IsCancellationRequested) return;
|
||||
if (evt is null) continue;
|
||||
var type = (string?)evt["type"];
|
||||
switch (type)
|
||||
{
|
||||
case "input_audio_buffer.speech_started":
|
||||
_lastSpeechAt = DateTime.UtcNow;
|
||||
break;
|
||||
case "response.audio.delta":
|
||||
await ForwardAudioDeltaAsync(evt, ct);
|
||||
break;
|
||||
@@ -215,6 +224,37 @@ public class RealtimeSession
|
||||
await _output.WriteBinaryAsync(bytes, ct);
|
||||
}
|
||||
|
||||
private async Task WatchdogAsync(CancellationToken ct)
|
||||
{
|
||||
try
|
||||
{
|
||||
var timeout = TimeSpan.FromSeconds(_cfg.IdleTimeoutSeconds);
|
||||
while (!ct.IsCancellationRequested && !_endRequested)
|
||||
{
|
||||
await Task.Delay(250, ct);
|
||||
if (DateTime.UtcNow - _lastSpeechAt > timeout)
|
||||
{
|
||||
_endReason = EndReason.Idle;
|
||||
_endRequested = true;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (OperationCanceledException) { }
|
||||
}
|
||||
|
||||
private async Task<JsonObject?> ReceiveOrIdleAsync(CancellationToken ct)
|
||||
{
|
||||
using var timeout = CancellationTokenSource.CreateLinkedTokenSource(ct);
|
||||
var receive = _upstream.ReceiveJsonAsync(timeout.Token);
|
||||
var delay = Task.Delay(250, timeout.Token);
|
||||
var done = await Task.WhenAny(receive, delay);
|
||||
if (done == receive) return await receive;
|
||||
timeout.Cancel();
|
||||
try { await receive; } catch { /* ignore */ }
|
||||
return null;
|
||||
}
|
||||
|
||||
private async Task PumpUplinkAsync(CancellationToken ct)
|
||||
{
|
||||
try
|
||||
|
||||
Reference in New Issue
Block a user