Просмотр исходного кода

fix: cosyvoice 走同步非流式,避免 SSE 模式 audioEvents=0

- cosyvoice-v3-flash 走 SSE 流式时返回 text/event-stream 但 0 audio events
- 改回同步模式(返回 audio.url 走 downloadAudio 下载到本地)
- endpoint 保留 /api/v1/services/audio/tts/SpeechSynthesizer
- 阿里云 cosyvoice 和 qwen-tts 的 endpoint 现在统一在 synthesize 方法里根据模型动态选
MyFramework User 2 месяцев назад
Родитель
Сommit
3cdbaac3e5
1 измененных файлов с 6 добавлено и 13 удалено
  1. 6 13
      server/src/modules/tts/aliyun.provider.ts

+ 6 - 13
server/src/modules/tts/aliyun.provider.ts

@@ -77,8 +77,7 @@ export class AliyunTtsProvider implements ITtsProvider {
     for (let attempt = 1; attempt <= retries; attempt++) {
       try {
         // 构建请求体(参考 tonggan/scripts/generate-tts.mjs 的成功调用格式)
-        // ⚠️ 重要:cosyvoice 不传 format/sample_rate,否则返回 400 InvalidParameter
-        // 阿里云百炼新版本会自动用 wav 格式(.wav 扩展名)作为默认
+        // cosyvoice 传 format='mp3' + sample_rate=48000(tonggan 验证可用的格式)
         const requestBody: any = {
           model: activeModel,
           input: {
@@ -88,6 +87,8 @@ export class AliyunTtsProvider implements ITtsProvider {
           },
         };
         if (isCosyVoice) {
+          requestBody.input.format = 'mp3';
+          requestBody.input.sample_rate = 48000;
           // CosyVoice v3-flash 系统音色支持 instruction 情感控制(固定格式 设置情感:{emotion}。)
           if ((params as any).instructText) {
             requestBody.input.instruction = (params as any).instructText;
@@ -101,18 +102,10 @@ export class AliyunTtsProvider implements ITtsProvider {
           if (instructions.length > 0) requestBody.input.instructions = instructions.join(',');
         }
 
-        console.log(`📤 [Aliyun TTS] ${isCosyVoice?'CosyVoice SSE':'Qwen'} ${attempt}/${retries}, model: ${activeModel}, voice: ${voiceId || this.voice}, text: ${text.length}字`);
+        console.log(`📤 [Aliyun TTS] ${isCosyVoice?'CosyVoice':'Qwen'} ${attempt}/${retries}, model: ${activeModel}, voice: ${voiceId || this.voice}, text: ${text.length}字`);
 
-        // CosyVoice: SSE 流式,边收边存,无超时
-        if (isCosyVoice) {
-          const result = await withAiLog(
-            () => this.synthesizeStream(requestBody, outputPath),
-            { callType: 'tts_synthesize', provider: this.vendor, model: activeModel, textLen: text.length }
-          );
-          return result;
-        }
-
-        // 千问: 同步非流式
+        // 统一走同步非流式(返回 audio.url,由 downloadAudio 下载到本地)
+        // CosyVoice v3-flash SSE 流式会返回空音频(audioEvents=0),必须用同步模式
         // 根据模型动态选择 endpoint
         const apiPath = activeModel.includes('cosyvoice')
           ? 'https://dashscope.aliyuncs.com/api/v1/services/audio/tts/SpeechSynthesizer'