Skip to content

Commit 8a7b6db

Browse files
karkarlCopilot
andcommitted
Harden MiniMax TTS integration
Co-authored-by: Copilot App <[email protected]> Copilot-Session: 85d40d7c-24af-45e6-9205-c1f185d0476c
1 parent a9864cb commit 8a7b6db

14 files changed

Lines changed: 427 additions & 37 deletions

File tree

‎src/OpenClaw.Shared/Mcp/McpToolBridge.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ private object HandleToolsList()
348348

349349
// tts.*
350350
["tts.speak"] =
351-
"Speak text aloud on the Windows node. Args: text (string, required), provider ('piper'|'windows'|'elevenlabs'|'minimax', optional — omit to use the configured TtsProvider setting, default 'piper' for fresh installs), voiceId (string, optional — overrides the per-provider configured voice), model (string, optional, cloud providers only), interrupt (bool, default false — interrupts any in-progress playback). When provider is omitted and the configured provider isn't usable (no cloud key, Piper voice not downloaded), the node falls back to Windows TTS so playback still happens. Explicit provider requests stay strict and do not silently reroute. Returns { spoken, provider (the provider that actually spoke), requestedProvider, fellBack, contentType, durationMs }.",
351+
"Speak text aloud on the Windows node. Args: text (string, required), provider ('piper'|'windows'|'elevenlabs'|'minimax', optional; omit to use the configured TtsProvider setting, default 'piper' for fresh installs), voiceId (string, optional; overrides the per-provider configured voice), model (string, optional, cloud providers only), interrupt (bool, default false; interrupts any in-progress playback). When provider is omitted and the configured provider isn't usable (no cloud key, Piper voice not downloaded), the node falls back to Windows TTS so playback still happens. Explicit provider requests stay strict and do not silently reroute. Returns { spoken, provider (the provider that actually spoke), requestedProvider, fellBack, contentType, durationMs }.",
352352
["tts.status"] =
353353
"Report TTS provider readiness. No args. Returns { configuredProvider, effectiveProvider (the provider that would run now after fallback), willFallBack (bool), providers: [{ provider ('piper'|'windows'|'elevenlabs'|'minimax'), readiness ('ready'|'needs-api-key'|'needs-voice'|'voice-not-downloaded'|'unavailable'), isReady (bool) }] }. Carries no PII (no voice ids, no key fragments, no device names). Requires NodeTtsEnabled.",
354354

‎src/OpenClaw.Shared/SettingsData.cs‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,10 @@ public record class SettingsData
101101
public string? TtsElevenLabsApiKey { get; set; }
102102
public string? TtsElevenLabsModel { get; set; }
103103
public string? TtsElevenLabsVoiceId { get; set; }
104+
/// <summary>
105+
/// MiniMax API key storage slot. When persisted by the Windows tray's
106+
/// SettingsManager this is an opaque dpapi:-prefixed blob, not plaintext.
107+
/// </summary>
104108
public string? TtsMiniMaxApiKey { get; set; }
105109
public string? TtsMiniMaxModel { get; set; } = "speech-2.8-hd";
106110
public string? TtsMiniMaxVoiceId { get; set; }

‎src/OpenClaw.Tray.WinUI/Pages/VoiceSettingsPage.xaml‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@
220220
<ComboBoxItem x:Uid="VoiceSettingsPage_TtsPiper" Content="Piper (local neural voices)" Tag="piper"/>
221221
<ComboBoxItem x:Uid="VoiceSettingsPage_TtsWindows" Content="Windows (built-in neural voices)" Tag="windows"/>
222222
<ComboBoxItem x:Uid="VoiceSettingsPage_TtsElevenLabs" Content="ElevenLabs (cloud, requires API key)" Tag="elevenlabs"/>
223-
<ComboBoxItem x:Name="MiniMaxProviderItem" Content="MiniMax (cloud, requires API key)" Tag="minimax"/>
223+
<ComboBoxItem x:Uid="VoiceSettingsPage_TtsMiniMax" Content="MiniMax (cloud, requires API key)" Tag="minimax"/>
224224
</ComboBox>
225225

226226
<!-- Piper voice picker (local neural TTS via Sherpa-ONNX) -->
@@ -285,18 +285,18 @@
285285
</StackPanel>
286286

287287
<StackPanel x:Name="MiniMaxPanel" Spacing="8" Visibility="Collapsed">
288-
<PasswordBox x:Name="MiniMaxApiKeyBox" Header="API Key"
288+
<PasswordBox x:Uid="VoiceSettingsPage_MiniMaxApiKeyBox" x:Name="MiniMaxApiKeyBox" Header="API Key"
289289
PasswordChanged="OnMiniMaxKeyChanged" Width="300"/>
290-
<TextBox x:Name="MiniMaxVoiceIdBox" Header="Voice ID"
290+
<TextBox x:Uid="VoiceSettingsPage_MiniMaxVoiceIdBox" x:Name="MiniMaxVoiceIdBox" Header="Voice ID"
291291
TextChanged="OnMiniMaxVoiceIdChanged" Width="300"
292292
PlaceholderText="e.g. male-qn-qingse"/>
293-
<TextBox x:Name="MiniMaxModelBox" Header="Model"
293+
<TextBox x:Uid="VoiceSettingsPage_MiniMaxModelBox" x:Name="MiniMaxModelBox" Header="Model"
294294
TextChanged="OnMiniMaxModelChanged" Width="300"
295295
PlaceholderText="speech-2.8-hd"/>
296-
<ComboBox x:Name="MiniMaxRegionCombo" Header="Region"
296+
<ComboBox x:Uid="VoiceSettingsPage_MiniMaxRegionCombo" x:Name="MiniMaxRegionCombo" Header="Region"
297297
SelectionChanged="OnMiniMaxRegionChanged" Width="300">
298-
<ComboBoxItem Content="Global" Tag="global_en"/>
299-
<ComboBoxItem Content="China" Tag="cn_zh"/>
298+
<ComboBoxItem x:Uid="VoiceSettingsPage_MiniMaxRegionGlobal" Content="Global" Tag="global_en"/>
299+
<ComboBoxItem x:Uid="VoiceSettingsPage_MiniMaxRegionChina" Content="China" Tag="cn_zh"/>
300300
</ComboBox>
301301
</StackPanel>
302302
</StackPanel>
@@ -308,7 +308,7 @@
308308
<StackPanel Orientation="Horizontal" Spacing="8">
309309
<FontIcon Glyph="&#xE8D7;" FontSize="16" Foreground="{ThemeResource SystemFillColorCautionBrush}"/>
310310
<TextBlock x:Uid="VoiceSettingsPage_PrivacyNote" x:Name="PrivacyNoteText"
311-
Text="All speech processing runs entirely on your device. No audio data is sent to any cloud service."
311+
Text="Piper, Windows, and speech recognition run on this device. Cloud TTS providers send response text to their services for synthesis; microphone audio is not sent to them."
312312
Style="{StaticResource CaptionTextBlockStyle}" TextWrapping="Wrap"/>
313313
</StackPanel>
314314
</Border>

‎src/OpenClaw.Tray.WinUI/Services/TextToSpeech/MiniMaxTextToSpeechClient.cs‎

Lines changed: 91 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ public sealed class MiniMaxSynthesisRequest
1717
public string ModelId { get; set; } = "";
1818
public string VoiceId { get; set; } = "";
1919
public string? Region { get; set; }
20-
public bool Stream { get; set; }
2120
}
2221

2322
public sealed class MiniMaxSynthesisResult
@@ -34,6 +33,7 @@ public sealed class MiniMaxTextToSpeechClient : IDisposable
3433
public const string ChinaRegion = "cn_zh";
3534
public const string GlobalBaseUrl = "https://api.minimax.io";
3635
public const string ChinaBaseUrl = "https://api.minimaxi.com";
36+
public const int MaxResponseBytes = 32 * 1024 * 1024;
3737
public static readonly TimeSpan DefaultTimeout = TimeSpan.FromSeconds(60);
3838

3939
public static readonly string[] Models =
@@ -60,20 +60,25 @@ public sealed class MiniMaxTextToSpeechClient : IDisposable
6060
private readonly bool _ownsHttpClient;
6161

6262
public MiniMaxTextToSpeechClient()
63-
: this(new HttpClient(), ownsHttpClient: true)
63+
: this(new HttpClient(), ownsHttpClient: true, DefaultTimeout)
6464
{
6565
}
6666

6767
public MiniMaxTextToSpeechClient(HttpMessageHandler handler)
68-
: this(new HttpClient(handler), ownsHttpClient: true)
68+
: this(new HttpClient(handler), ownsHttpClient: true, DefaultTimeout)
6969
{
7070
}
7171

72-
private MiniMaxTextToSpeechClient(HttpClient httpClient, bool ownsHttpClient)
72+
internal MiniMaxTextToSpeechClient(HttpMessageHandler handler, TimeSpan timeout)
73+
: this(new HttpClient(handler), ownsHttpClient: true, timeout)
74+
{
75+
}
76+
77+
private MiniMaxTextToSpeechClient(HttpClient httpClient, bool ownsHttpClient, TimeSpan timeout)
7378
{
7479
_httpClient = httpClient;
7580
_ownsHttpClient = ownsHttpClient;
76-
_httpClient.Timeout = DefaultTimeout;
81+
_httpClient.Timeout = timeout;
7782
}
7883

7984
public TimeSpan Timeout => _httpClient.Timeout;
@@ -97,12 +102,16 @@ public async Task<MiniMaxSynthesisResult> SynthesizeAsync(
97102
var uri = new Uri(new Uri(ResolveBaseUrl(request.Region)), "/v1/t2a_v2");
98103
using var message = new HttpRequestMessage(HttpMethod.Post, uri);
99104
message.Headers.Authorization = new AuthenticationHeaderValue("Bearer", request.ApiKey.Trim());
105+
using var timeoutSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
106+
if (_httpClient.Timeout != System.Threading.Timeout.InfiniteTimeSpan)
107+
timeoutSource.CancelAfter(_httpClient.Timeout);
108+
var requestToken = timeoutSource.Token;
100109

101110
var body = new Dictionary<string, object?>
102111
{
103112
["model"] = model,
104113
["text"] = request.Text,
105-
["stream"] = request.Stream,
114+
["stream"] = false,
106115
["voice_setting"] = new Dictionary<string, object?>
107116
{
108117
["voice_id"] = request.VoiceId.Trim()
@@ -115,8 +124,10 @@ public async Task<MiniMaxSynthesisResult> SynthesizeAsync(
115124

116125
message.Content = new StringContent(JsonSerializer.Serialize(body), Encoding.UTF8, "application/json");
117126

118-
using var response = await _httpClient.SendAsync(message, cancellationToken).ConfigureAwait(false);
119-
var bytes = await response.Content.ReadAsByteArrayAsync(cancellationToken).ConfigureAwait(false);
127+
using var response = await _httpClient
128+
.SendAsync(message, HttpCompletionOption.ResponseHeadersRead, requestToken)
129+
.ConfigureAwait(false);
130+
var bytes = await ReadBoundedResponseAsync(response.Content, requestToken).ConfigureAwait(false);
120131
if (!response.IsSuccessStatusCode)
121132
throw new InvalidOperationException(BuildFailureMessage(response.StatusCode, bytes));
122133

@@ -140,18 +151,81 @@ private static string ResolveBaseUrl(string? region)
140151

141152
private static byte[] ExtractAudio(byte[] responseBytes)
142153
{
143-
using var doc = JsonDocument.Parse(responseBytes);
144-
if (!doc.RootElement.TryGetProperty("data", out var data) ||
145-
!data.TryGetProperty("audio", out var audio) ||
146-
audio.ValueKind != JsonValueKind.String)
154+
JsonDocument doc;
155+
try
156+
{
157+
doc = JsonDocument.Parse(responseBytes);
158+
}
159+
catch (JsonException ex)
160+
{
161+
throw new InvalidOperationException("MiniMax returned an invalid JSON response.", ex);
162+
}
163+
164+
using (doc)
165+
{
166+
if (TryGetApplicationErrorCode(doc.RootElement, out var statusCode))
167+
{
168+
throw new InvalidOperationException(
169+
$"MiniMax TTS returned provider error code {statusCode}.");
170+
}
171+
172+
if (!doc.RootElement.TryGetProperty("data", out var data) ||
173+
!data.TryGetProperty("audio", out var audio) ||
174+
audio.ValueKind != JsonValueKind.String)
175+
{
176+
return [];
177+
}
178+
179+
var value = audio.GetString();
180+
if (string.IsNullOrWhiteSpace(value))
181+
return [];
182+
183+
try
184+
{
185+
return Convert.FromHexString(value);
186+
}
187+
catch (FormatException ex)
188+
{
189+
throw new InvalidOperationException("MiniMax returned invalid audio data.", ex);
190+
}
191+
}
192+
}
193+
194+
private static bool TryGetApplicationErrorCode(JsonElement root, out int statusCode)
195+
{
196+
statusCode = 0;
197+
return root.TryGetProperty("base_resp", out var baseResponse) &&
198+
baseResponse.ValueKind == JsonValueKind.Object &&
199+
baseResponse.TryGetProperty("status_code", out var statusCodeElement) &&
200+
statusCodeElement.TryGetInt32(out statusCode) &&
201+
statusCode != 0;
202+
}
203+
204+
private static async Task<byte[]> ReadBoundedResponseAsync(
205+
HttpContent content,
206+
CancellationToken cancellationToken)
207+
{
208+
if (content.Headers.ContentLength is > MaxResponseBytes)
209+
throw new InvalidOperationException($"MiniMax response exceeds {MaxResponseBytes} bytes.");
210+
211+
var initialCapacity = content.Headers.ContentLength is > 0 and <= MaxResponseBytes
212+
? (int)content.Headers.ContentLength.Value
213+
: 0;
214+
using var destination = new MemoryStream(initialCapacity);
215+
using var source = await content.ReadAsStreamAsync(cancellationToken).ConfigureAwait(false);
216+
var buffer = new byte[64 * 1024];
217+
while (true)
147218
{
148-
return [];
219+
var read = await source.ReadAsync(buffer.AsMemory(), cancellationToken).ConfigureAwait(false);
220+
if (read == 0)
221+
break;
222+
if (destination.Length + read > MaxResponseBytes)
223+
throw new InvalidOperationException($"MiniMax response exceeds {MaxResponseBytes} bytes.");
224+
225+
await destination.WriteAsync(buffer.AsMemory(0, read), cancellationToken).ConfigureAwait(false);
149226
}
150227

151-
var value = audio.GetString();
152-
return string.IsNullOrWhiteSpace(value)
153-
? []
154-
: Convert.FromHexString(value);
228+
return destination.ToArray();
155229
}
156230

157231
private static string BuildFailureMessage(System.Net.HttpStatusCode statusCode, byte[] bodyBytes)

‎src/OpenClaw.Tray.WinUI/Strings/en-us/Resources.resw‎

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2606,6 +2606,9 @@ Use one of these options:
26062606
<data name="VoiceSettingsPage_TtsElevenLabs.Content" xml:space="preserve">
26072607
<value>ElevenLabs (cloud, requires API key)</value>
26082608
</data>
2609+
<data name="VoiceSettingsPage_TtsMiniMax.Content" xml:space="preserve">
2610+
<value>MiniMax (cloud, requires API key)</value>
2611+
</data>
26092612
<data name="VoiceSettingsPage_PiperVoiceCombo.Header" xml:space="preserve">
26102613
<value>Voice</value>
26112614
</data>
@@ -2639,8 +2642,32 @@ Use one of these options:
26392642
<data name="VoiceSettingsPage_ElevenLabsModelBox.Header" xml:space="preserve">
26402643
<value>Model (optional)</value>
26412644
</data>
2645+
<data name="VoiceSettingsPage_MiniMaxApiKeyBox.Header" xml:space="preserve">
2646+
<value>API Key</value>
2647+
</data>
2648+
<data name="VoiceSettingsPage_MiniMaxVoiceIdBox.Header" xml:space="preserve">
2649+
<value>Voice ID</value>
2650+
</data>
2651+
<data name="VoiceSettingsPage_MiniMaxVoiceIdBox.PlaceholderText" xml:space="preserve">
2652+
<value>e.g. male-qn-qingse</value>
2653+
</data>
2654+
<data name="VoiceSettingsPage_MiniMaxModelBox.Header" xml:space="preserve">
2655+
<value>Model</value>
2656+
</data>
2657+
<data name="VoiceSettingsPage_MiniMaxModelBox.PlaceholderText" xml:space="preserve">
2658+
<value>speech-2.8-hd</value>
2659+
</data>
2660+
<data name="VoiceSettingsPage_MiniMaxRegionCombo.Header" xml:space="preserve">
2661+
<value>Region</value>
2662+
</data>
2663+
<data name="VoiceSettingsPage_MiniMaxRegionGlobal.Content" xml:space="preserve">
2664+
<value>Global</value>
2665+
</data>
2666+
<data name="VoiceSettingsPage_MiniMaxRegionChina.Content" xml:space="preserve">
2667+
<value>China</value>
2668+
</data>
26422669
<data name="VoiceSettingsPage_PrivacyNote.Text" xml:space="preserve">
2643-
<value>All speech processing runs entirely on your device. No audio data is sent to any cloud service.</value>
2670+
<value>Piper, Windows, and speech recognition run on this device. Cloud TTS providers send response text to their services for synthesis; microphone audio is not sent to them.</value>
26442671
</data>
26452672
<data name="VoiceSettingsPage_StatusModelReady" xml:space="preserve">
26462673
<value>✅ Model ready</value>

‎src/OpenClaw.Tray.WinUI/Strings/fr-fr/Resources.resw‎

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2559,6 +2559,9 @@ Utilisez l'une de ces options :
25592559
<data name="VoiceSettingsPage_TtsElevenLabs.Content" xml:space="preserve">
25602560
<value>ElevenLabs (cloud, clé API requise)</value>
25612561
</data>
2562+
<data name="VoiceSettingsPage_TtsMiniMax.Content" xml:space="preserve">
2563+
<value>MiniMax (cloud, clé API requise)</value>
2564+
</data>
25622565
<data name="VoiceSettingsPage_PiperVoiceCombo.Header" xml:space="preserve">
25632566
<value>Voix</value>
25642567
</data>
@@ -2592,8 +2595,32 @@ Utilisez l'une de ces options :
25922595
<data name="VoiceSettingsPage_ElevenLabsModelBox.Header" xml:space="preserve">
25932596
<value>Modèle (facultatif)</value>
25942597
</data>
2598+
<data name="VoiceSettingsPage_MiniMaxApiKeyBox.Header" xml:space="preserve">
2599+
<value>Clé API</value>
2600+
</data>
2601+
<data name="VoiceSettingsPage_MiniMaxVoiceIdBox.Header" xml:space="preserve">
2602+
<value>ID de la voix</value>
2603+
</data>
2604+
<data name="VoiceSettingsPage_MiniMaxVoiceIdBox.PlaceholderText" xml:space="preserve">
2605+
<value>p. ex. male-qn-qingse</value>
2606+
</data>
2607+
<data name="VoiceSettingsPage_MiniMaxModelBox.Header" xml:space="preserve">
2608+
<value>Modèle</value>
2609+
</data>
2610+
<data name="VoiceSettingsPage_MiniMaxModelBox.PlaceholderText" xml:space="preserve">
2611+
<value>speech-2.8-hd</value>
2612+
</data>
2613+
<data name="VoiceSettingsPage_MiniMaxRegionCombo.Header" xml:space="preserve">
2614+
<value>Région</value>
2615+
</data>
2616+
<data name="VoiceSettingsPage_MiniMaxRegionGlobal.Content" xml:space="preserve">
2617+
<value>Monde</value>
2618+
</data>
2619+
<data name="VoiceSettingsPage_MiniMaxRegionChina.Content" xml:space="preserve">
2620+
<value>Chine</value>
2621+
</data>
25952622
<data name="VoiceSettingsPage_PrivacyNote.Text" xml:space="preserve">
2596-
<value>Tout le traitement vocal s'exécute entièrement sur votre appareil. Aucune donnée audio n'est envoyée à un service cloud.</value>
2623+
<value>Piper, Windows et la reconnaissance vocale s'exécutent sur cet appareil. Les fournisseurs TTS cloud envoient le texte des réponses à leurs services pour la synthèse ; le son du microphone ne leur est pas envoyé.</value>
25972624
</data>
25982625
<data name="VoiceSettingsPage_StatusModelReady" xml:space="preserve">
25992626
<value>✅ Modèle prêt</value>

‎src/OpenClaw.Tray.WinUI/Strings/nl-nl/Resources.resw‎

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2560,6 +2560,9 @@ Gebruik een van deze opties:
25602560
<data name="VoiceSettingsPage_TtsElevenLabs.Content" xml:space="preserve">
25612561
<value>ElevenLabs (cloud, API-sleutel vereist)</value>
25622562
</data>
2563+
<data name="VoiceSettingsPage_TtsMiniMax.Content" xml:space="preserve">
2564+
<value>MiniMax (cloud, API-sleutel vereist)</value>
2565+
</data>
25632566
<data name="VoiceSettingsPage_PiperVoiceCombo.Header" xml:space="preserve">
25642567
<value>Stem</value>
25652568
</data>
@@ -2593,8 +2596,32 @@ Gebruik een van deze opties:
25932596
<data name="VoiceSettingsPage_ElevenLabsModelBox.Header" xml:space="preserve">
25942597
<value>Model (optioneel)</value>
25952598
</data>
2599+
<data name="VoiceSettingsPage_MiniMaxApiKeyBox.Header" xml:space="preserve">
2600+
<value>API-sleutel</value>
2601+
</data>
2602+
<data name="VoiceSettingsPage_MiniMaxVoiceIdBox.Header" xml:space="preserve">
2603+
<value>Stem-ID</value>
2604+
</data>
2605+
<data name="VoiceSettingsPage_MiniMaxVoiceIdBox.PlaceholderText" xml:space="preserve">
2606+
<value>bijv. male-qn-qingse</value>
2607+
</data>
2608+
<data name="VoiceSettingsPage_MiniMaxModelBox.Header" xml:space="preserve">
2609+
<value>Spraakmodel</value>
2610+
</data>
2611+
<data name="VoiceSettingsPage_MiniMaxModelBox.PlaceholderText" xml:space="preserve">
2612+
<value>speech-2.8-hd</value>
2613+
</data>
2614+
<data name="VoiceSettingsPage_MiniMaxRegionCombo.Header" xml:space="preserve">
2615+
<value>Regio</value>
2616+
</data>
2617+
<data name="VoiceSettingsPage_MiniMaxRegionGlobal.Content" xml:space="preserve">
2618+
<value>Wereldwijd</value>
2619+
</data>
2620+
<data name="VoiceSettingsPage_MiniMaxRegionChina.Content" xml:space="preserve">
2621+
<value>Vasteland van China</value>
2622+
</data>
25962623
<data name="VoiceSettingsPage_PrivacyNote.Text" xml:space="preserve">
2597-
<value>Alle spraakverwerking draait volledig op uw apparaat. Er worden geen audiogegevens verzonden naar een cloudservice.</value>
2624+
<value>Piper, Windows en spraakherkenning worden op dit apparaat uitgevoerd. Cloud-TTS-providers sturen antwoordtekst naar hun services voor synthese; microfoonaudio wordt niet naar hen verzonden.</value>
25982625
</data>
25992626
<data name="VoiceSettingsPage_StatusModelReady" xml:space="preserve">
26002627
<value>✅ Model gereed</value>

0 commit comments

Comments
 (0)