|
|
|
@ -52,6 +52,9 @@ interface AgentServiceListener { |
|
|
|
object AgentService : CoroutineScope { |
|
|
|
private val TAG = "AgentService" |
|
|
|
|
|
|
|
private var azureSpeechKey: String = "" |
|
|
|
private var azureSpeechRegion: String = "" |
|
|
|
|
|
|
|
// 协程相关 |
|
|
|
private val job = SupervisorJob() |
|
|
|
override val coroutineContext: CoroutineContext |
|
|
|
@ -179,6 +182,9 @@ object AgentService : CoroutineScope { |
|
|
|
LocationService.initialize(appContext) |
|
|
|
// 设置系统提示词 |
|
|
|
val customSystemPrompt = config["systemPrompt"] as? String |
|
|
|
//保存配置 |
|
|
|
this.azureSpeechKey = config["azureSpeechKey"]?.toString() ?: "" |
|
|
|
this.azureSpeechRegion = config["azureSpeechRegion"]?.toString() ?: "" |
|
|
|
if (!customSystemPrompt.isNullOrBlank()) { |
|
|
|
systemPrompt = customSystemPrompt |
|
|
|
} else { |
|
|
|
@ -188,9 +194,15 @@ object AgentService : CoroutineScope { |
|
|
|
azureAsrHelper = AzureAsrHelper(context).apply { |
|
|
|
// 使用协程处理异步初始化 |
|
|
|
CoroutineScope(Dispatchers.IO).launch { |
|
|
|
//获取语音识别的语种 |
|
|
|
val supportedLanguagesConfig = config["supportedLanguages"] as? List<String> |
|
|
|
val supportedLanguages = supportedLanguagesConfig?.toTypedArray() |
|
|
|
?: arrayOf("zh-CN", "en-US") // 默认支持多语言 |
|
|
|
|
|
|
|
val asrInitSuccess = initialize( |
|
|
|
subscriptionKey = config["azureSpeechKey"]?.toString() ?: "", |
|
|
|
region = config["azureSpeechRegion"]?.toString() ?: "", |
|
|
|
supportedLanguages = supportedLanguages, |
|
|
|
audioSourceType = if (isExternalActive) AzureAsrHelper.AudioSourceType.EXTERNAL |
|
|
|
else AzureAsrHelper.AudioSourceType.MICROPHONE |
|
|
|
) |
|
|
|
@ -1091,6 +1103,58 @@ object AgentService : CoroutineScope { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 设置支持的语音识别语言 |
|
|
|
*/ |
|
|
|
fun setSupportedLanguages(languages: List<String>): Boolean { |
|
|
|
if (!isInitialized) { |
|
|
|
Log.e(TAG, "服务未初始化") |
|
|
|
sendEvent("error", mapOf("code" to "NOT_INITIALIZED", "message" to "服务未初始化")) |
|
|
|
return false |
|
|
|
} |
|
|
|
|
|
|
|
if (languages.isEmpty()) { |
|
|
|
Log.e(TAG, "语言列表不能为空") |
|
|
|
sendEvent("error", mapOf("code" to "EMPTY_LANGUAGES", "message" to "语言列表不能为空")) |
|
|
|
return false |
|
|
|
} |
|
|
|
|
|
|
|
return try { |
|
|
|
Log.i(TAG, "设置支持的语言: ${languages.joinToString(", ")}") |
|
|
|
|
|
|
|
// 重新初始化ASR服务以应用新的语言设置 |
|
|
|
val success = azureAsrHelper?.initialize( |
|
|
|
subscriptionKey = this.azureSpeechKey, |
|
|
|
region = this.azureSpeechRegion, |
|
|
|
supportedLanguages = languages.toTypedArray(), |
|
|
|
audioSourceType = audioSourceType |
|
|
|
) ?: false |
|
|
|
|
|
|
|
if (success) { |
|
|
|
Log.i(TAG, "语言设置更新成功") |
|
|
|
sendEvent("languages_updated", mapOf( |
|
|
|
"languages" to languages, |
|
|
|
"status" to "success" |
|
|
|
)) |
|
|
|
} else { |
|
|
|
Log.e(TAG, "语言设置更新失败") |
|
|
|
sendEvent("error", mapOf( |
|
|
|
"code" to "LANGUAGE_UPDATE_FAILED", |
|
|
|
"message" to "语言设置更新失败" |
|
|
|
)) |
|
|
|
} |
|
|
|
|
|
|
|
success |
|
|
|
} catch (e: Exception) { |
|
|
|
Log.e(TAG, "设置支持语言异常: ${e.message}") |
|
|
|
sendEvent("error", mapOf( |
|
|
|
"code" to "LANGUAGE_UPDATE_ERROR", |
|
|
|
"message" to e.message.toString() |
|
|
|
)) |
|
|
|
false |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 保存聊天消息 |
|
|
|
*/ |
|
|
|
|