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() }
}

View File

@@ -449,6 +449,30 @@ def main():
check("置顶数超过窗口时,置顶的全部保留(6 条)", kept == 6, f"实际 {kept} 条:{seg4[-200:]}")
store.write_settings({"memoryWindow": 50})
# ---------------------------------------------------------------- 16
section("16. 语音:智能体自己的音色要真的生效(用户报的「语音没生效」)")
#
# 内核这边一直是对的(`voice_for` 做"智能体 > 全局"),
# 真正漏的是**界面朗读时不传 agentId** —— 于是内核拿不到人,
# 永远用全局那个嗓子,智能体编辑页里选的音色白配。
# 界面那一侧没法在这里测,所以这里把**契约**钉死:
# 只要 agentId 传进来,音色必须跟着换。
import speech
a_voice = store.create_agent({"name": "嗓音测试"})
store.update_agent(a_voice["id"], {"ttsVoice": "my-voice-123", "ttsEngine": "volc"})
st = store.read_settings()
merged = speech.voice_for(st, store.get_agent(a_voice["id"]))
check("智能体的音色覆盖了全局", merged.get("ttsVoice") == "my-voice-123", str(merged.get("ttsVoice")))
check("引擎也跟着覆盖", merged.get("ttsEngine") == "volc", str(merged.get("ttsEngine")))
plain = speech.voice_for(st, {})
check("没配的智能体回落全局", plain.get("ttsVoice") == speech.voice_for(st, {}).get("ttsVoice"))
# 半配:只设了音色、没设引擎 → 引擎回落,音色用它的
half = store.create_agent({"name": "半配"})
store.update_agent(half["id"], {"ttsVoice": "only-voice"})
m2 = speech.voice_for(st, store.get_agent(half["id"]))
check("只设音色时引擎回落全局", m2.get("ttsVoice") == "only-voice", str(m2.get("ttsVoice")))
# ---------------------------------------------------------------- 汇总
print()
if fails: