fix(speech): 智能体的音色不生效(用户报的)—— 界面朗读时没传 agentId(v2.0.54)

内核一直是对的:speech.voice_for() 有'智能体 > 全局'的覆盖,
/api/tts 和 /api/tts/stream 都收 agentId。

漏的是界面:client.speak(text) 只传正文,内核拿不到人就永远用全局音色 ——
用户在智能体编辑页选的音色配了等于没配。

  · SpeechController 加 agentId: () -> String(lambda,切换智能体要跟着变)
  · KernelClient.speak / streamTts 加 agentId 参数
  · ChatScreen 的 remember 里传 vm.state.settings.activeAgentId
  · 流式那条(自动朗读/长句)漏的是同一处,一起补

探针新增 16 节 4 项:界面侧没法测(Kotlin),所以钉**契约** ——
只要 agentId 传进来音色就必须跟着换、没配的回落全局。
This commit is contained in:
l
2026-10-16 16:21:39 +08:00
parent 182c0005a4
commit 08eecb9e0a
4 changed files with 47 additions and 7 deletions

View File

@@ -30,8 +30,8 @@ android {
* versionCode 每次发版 +1(Android 靠它判断"能不能覆盖安装",
* 改小了会装不上)。
*/
versionCode = 253
versionName = "2.0.53"
versionCode = 254
versionName = "2.0.54"
ndk {
// Chaquopy 只支持这两个;Termux bootstrap 也只准备了这两份

View File

@@ -1,4 +1,4 @@
package com.doubao.lynxclone.speech
package com.doubao.lynxclone.speech
import android.content.Context
import android.util.Log
@@ -26,6 +26,16 @@ class SpeechController(
private val context: Context,
private val client: KernelClient,
private val scope: CoroutineScope,
/**
* 当前说话的是哪个智能体 —— 朗读时把它带给内核,用**它自己的音色**。
*
* ★★ 这个参数是补上来的:以前 `client.speak(text)` 只传正文,
* 内核拿不到 agentId 就一律用全局音色,于是**智能体编辑页里选的声音
* 配了也没用**(用户报的"智能体语音咋没生效")。
* 做成 lambda 而不是字符串,是因为它会随用户切换智能体而变,
* 而这个 controller 是在进聊天页时 remember 一次的。
*/
private val agentId: () -> String = { "" },
) {
private val speaker = Speaker(context)
private val recorder = Recorder(context)
@@ -89,7 +99,7 @@ class SpeechController(
// 用户看到的就是"一读语音就闪退"。
val played = withContext(Dispatchers.IO) {
try {
client.streamTts(text).use { r ->
client.streamTts(text, agentId()).use { r ->
val mime = r.body?.contentType().toString()
val source = r.body?.source()
source != null && speaker.playStream(mime, source)
@@ -107,7 +117,7 @@ class SpeechController(
}
try {
val result = client.speak(text)
val result = client.speak(text, agentId())
if (!result.optBoolean("ok")) {
// 云端失败就退回系统 TTS —— 用户点了朗读却一点动静都没有是最差的体验
val reason = result.optString("error").ifBlank { "云端合成失败" }

View File

@@ -1,4 +1,4 @@
package com.doubao.lynxclone.ui
package com.doubao.lynxclone.ui
import androidx.activity.compose.rememberLauncherForActivityResult
import androidx.activity.result.contract.ActivityResultContracts
@@ -182,7 +182,13 @@ fun ChatScreen(vm: AppViewModel) {
val onPickFile: () -> Unit = { picker.launch(arrayOf("*/*")) }
// ---------------- 语音 ----------------
val speech = remember { SpeechController(context, vm.client, scope) }
val speech = remember {
SpeechController(context, vm.client, scope) {
// ★ 带上当前智能体 —— 不带的话内核一律用全局音色,
// 智能体编辑页里选的声音就白配了(用户报的"语音没生效")
vm.state.settings.activeAgentId
}
}
DisposableEffect(Unit) {
onDispose { speech.release() }
}