-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathVoiceBridge.cs
More file actions
164 lines (150 loc) · 7.66 KB
/
Copy pathVoiceBridge.cs
File metadata and controls
164 lines (150 loc) · 7.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
using System.Diagnostics;
using System.Text;
using System.Text.Json;
// ═══════════════════════════════════════════════════════════════════════
// VoiceBridge — one-shot speech recognition via the AIOffice.VoiceAgent.Win.exe
// subprocess (Windows only; same JSON-Lines stdin/stdout protocol used by the
// AIOffice Voice panel: {"cmd":"start","lang":...} → {"type":"transcript","text":...}).
//
// The endpoint POST /v1/voice/listen reports itself unavailable (501) when the
// platform or the executable is missing — the client activates voice speech only
// where it really runs. The microphone is exclusive, so recognition is serialized
// across requests with a static gate.
// ═══════════════════════════════════════════════════════════════════════
/// <summary>One-shot speech recognition bridge to the Windows VoiceAgent subprocess.</summary>
public static class VoiceBridge
{
/// <summary>Serializes microphone access (one listener at a time).</summary>
private static readonly SemaphoreSlim ListenGate = new(1, 1);
/// <summary>
/// Path to AIOffice.VoiceAgent.Win.exe. Defaults to the server base directory;
/// overridable with the "Voice:ExePath" appsettings key / --Voice:ExePath CLI override.
/// </summary>
public static string? ExePath { get; set; }
/// <summary>True when the platform is Windows and the VoiceAgent executable is present.</summary>
public static bool IsAvailable
{
get
{
if (!OperatingSystem.IsWindows()) return false;
var exe = ResolveExe();
return exe != null && File.Exists(exe);
}
}
/// <summary>Human-readable reason when <see cref="IsAvailable"/> is false. Written for the
/// END USER of the app (the detail is shown verbatim in the TUI note): no paths, no
/// project/build jargon — just what the user can do about it. Where the payload actually
/// comes from (release archives ship it; developers can point to a local build via
/// Voice:ExePath) is documented in the csproj targets and Program.cs, not here.</summary>
public static string UnavailableReason
{
get
{
if (!OperatingSystem.IsWindows())
return "Voice speech recognition runs only on Windows.";
var exe = ResolveExe();
if (exe == null || !File.Exists(exe))
return "The speech-recognition component is missing from this installation. Update the app to the latest version — voice comes bundled with it.";
return "";
}
}
/// <summary>
/// Listens once with the server microphone and returns the recognized phrase.
/// </summary>
/// <param name="lang">Two-letter ISO language code; default = the machine's UI/system language.</param>
/// <param name="timeoutSeconds">Seconds to wait for speech (clamped 1–60; default 15).</param>
/// <param name="ct">Cancellation token (client disconnect).</param>
/// <exception cref="TimeoutException">No speech recognized within the timeout.</exception>
public static async Task<string> ListenOnceAsync(string? lang, int timeoutSeconds, CancellationToken ct)
{
if (!IsAvailable) throw new PlatformNotSupportedException(UnavailableReason);
var timeout = TimeSpan.FromSeconds(Math.Clamp(timeoutSeconds <= 0 ? 15 : timeoutSeconds, 1, 60));
var timeoutCts = CancellationTokenSource.CreateLinkedTokenSource(ct);
timeoutCts.CancelAfter(timeout);
// Speech recognition speaks the machine's language (never a hardcoded default).
lang = string.IsNullOrWhiteSpace(lang) ? SystemLang.Get() : lang.Trim();
await ListenGate.WaitAsync(ct);
Process? process = null;
try
{
var exe = ResolveExe()!;
process = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = exe,
// Anchor the child to its own folder: the host may have been launched from
// any directory, and the child must not inherit an arbitrary CWD.
WorkingDirectory = Path.GetDirectoryName(exe) ?? AppContext.BaseDirectory,
UseShellExecute = false,
RedirectStandardInput = true,
RedirectStandardOutput = true,
CreateNoWindow = true,
StandardInputEncoding = new UTF8Encoding(false)
}
};
if (!process.Start())
throw new InvalidOperationException($"Failed to start {ResolveExe()}.");
var input = process.StandardInput;
var output = process.StandardOutput;
while (await output.ReadLineAsync(timeoutCts.Token) is { } line)
{
if (!TryParseLine(line, out var type, out var text)) continue;
switch (type)
{
case "ready":
await WriteCommandAsync(input, new { cmd = "start", lang });
break;
case "transcript" when !string.IsNullOrWhiteSpace(text):
return text;
case "error":
throw new InvalidOperationException($"VoiceAgent error: {text}");
}
}
throw new InvalidOperationException("VoiceAgent exited before recognizing speech.");
}
catch (OperationCanceledException) when (timeoutCts.IsCancellationRequested && !ct.IsCancellationRequested)
{
throw new TimeoutException($"No speech recognized within {timeout.TotalSeconds:0}s.");
}
finally
{
try { if (process != null && !process.HasExited) { process.StandardInput.WriteLine("{\"cmd\":\"stop\"}"); process.StandardInput.Flush(); } } catch { }
try { if (process != null && !process.HasExited) process.Kill(entireProcessTree: true); } catch { }
process?.Dispose();
ListenGate.Release();
}
}
private static string? ResolveExe()
{
if (!string.IsNullOrWhiteSpace(ExePath))
return Path.IsPathRooted(ExePath) ? ExePath : Path.Combine(AppDomain.CurrentDomain.BaseDirectory, ExePath);
// The csproj copies the VoiceAgent output into the voiceagent/ subfolder — NOT
// flat next to the server: VoiceAgent.Win pins an older KokoroSharp (0.6.7) that
// would shadow this server's 0.8.4 one and break the in-process TTS.
return Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "voiceagent", "AIOffice.VoiceAgent.Win.exe");
}
private static async Task WriteCommandAsync(StreamWriter input, object command)
{
var json = JsonSerializer.Serialize(command, new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase });
await input.WriteLineAsync(json);
await input.FlushAsync();
}
private static bool TryParseLine(string line, out string type, out string text)
{
type = "";
text = "";
try
{
using var doc = JsonDocument.Parse(line);
var root = doc.RootElement;
type = root.TryGetProperty("type", out var t) ? t.GetString() ?? "" : "";
text = root.TryGetProperty("text", out var x) ? x.GetString() ?? "" : "";
return true;
}
catch (JsonException)
{
return false;
}
}
}