|
|
@ -205,7 +205,8 @@ object AgentService : CoroutineScope { |
|
|
serverurl = config["serverurl"]?.toString() ?: ""; |
|
|
serverurl = config["serverurl"]?.toString() ?: ""; |
|
|
usertoken = config["usertoken"]?.toString() ?: ""; |
|
|
usertoken = config["usertoken"]?.toString() ?: ""; |
|
|
callingTool = config["callingTool"]?.toString() ?: ""; |
|
|
callingTool = config["callingTool"]?.toString() ?: ""; |
|
|
aiintegral = (config["aiintegral"] as? Number)?.toInt() ?: 0 |
|
|
aiintegral = config["aiintegral"]?.toString()?.toIntOrNull() ?: 0; |
|
|
|
|
|
|
|
|
// 初始化上下文和服务 |
|
|
// 初始化上下文和服务 |
|
|
this.context = appContext.applicationContext |
|
|
this.context = appContext.applicationContext |
|
|
|
|
|
|
|
|
@ -1007,7 +1008,6 @@ object AgentService : CoroutineScope { |
|
|
// 兜底处理,直接添加原消息 |
|
|
// 兜底处理,直接添加原消息 |
|
|
addToHistoryMessages(userMessage) |
|
|
addToHistoryMessages(userMessage) |
|
|
} |
|
|
} |
|
|
// Log.e("ChatApiService", "liwei---------sendMessageStream messages:${messagesWithSystemPrompt}") |
|
|
|
|
|
chatApiService.sendMessageStream( |
|
|
chatApiService.sendMessageStream( |
|
|
sessionid, |
|
|
sessionid, |
|
|
messagesWithSystemPrompt, |
|
|
messagesWithSystemPrompt, |
|
|
@ -1017,16 +1017,19 @@ object AgentService : CoroutineScope { |
|
|
sessionid: String, |
|
|
sessionid: String, |
|
|
prompt_tokens: Int?, |
|
|
prompt_tokens: Int?, |
|
|
completion_tokens: Int?, |
|
|
completion_tokens: Int?, |
|
|
total_tokens: Int? |
|
|
callfunctions: List<String>? |
|
|
) { |
|
|
) { |
|
|
try { |
|
|
try { |
|
|
sendStatisticsEvent( |
|
|
// 显式构造 Map<String, Int>,避免 mapOf 推断为 Map<String, Int> |
|
|
mapOf( |
|
|
val actions = mutableMapOf<String, Int>() |
|
|
"CHAT_PROMPT_TOKENS" to (prompt_tokens ?: 0), |
|
|
actions["AICHAT_PROMPT_TOKENS"] = (prompt_tokens ?: 0) |
|
|
"CHAT_COMPLETION_TOKENS" to (completion_tokens ?: 0), |
|
|
actions["AICHAT_COMPLETION_TOKENS"] = (completion_tokens ?: 0) |
|
|
"CHAT_TOTAL_TOKENS" to (total_tokens ?: 0) |
|
|
// 累计函数调用次数:按函数名分别累加 value += 1 |
|
|
) |
|
|
callfunctions?.forEach { funcName -> |
|
|
) |
|
|
val key = "AICHAT_FUNCTION_" + funcName |
|
|
|
|
|
actions[key] = (actions[key] ?: 0) + 1 |
|
|
|
|
|
} |
|
|
|
|
|
sendStatisticsEvent(actions) |
|
|
}catch (e: Exception) { |
|
|
}catch (e: Exception) { |
|
|
Log.e(TAG, "liwei--------------- AI Call onUsage 异常", e) |
|
|
Log.e(TAG, "liwei--------------- AI Call onUsage 异常", e) |
|
|
} |
|
|
} |
|
|
@ -1035,11 +1038,9 @@ object AgentService : CoroutineScope { |
|
|
override fun onToken(sessionid: String, token: String) { |
|
|
override fun onToken(sessionid: String, token: String) { |
|
|
try { |
|
|
try { |
|
|
if (sessionid != currsessionId) { |
|
|
if (sessionid != currsessionId) { |
|
|
// Log.d(TAG, "liwei--------------- AI Call onToken 会话过期 $sessionid-------$currsessionId") |
|
|
|
|
|
return |
|
|
return |
|
|
} |
|
|
} |
|
|
responseBuilder.append(token) |
|
|
responseBuilder.append(token) |
|
|
// Log.d(TAG, "liwei--------------- AI Call onToken:$token speakResponse:$speakResponse reply:$reply broadcast:$broadcast") |
|
|
|
|
|
if (speakResponse && reply && broadcast && token.isNotEmpty()) { |
|
|
if (speakResponse && reply && broadcast && token.isNotEmpty()) { |
|
|
ttsService?.speakStream(token) |
|
|
ttsService?.speakStream(token) |
|
|
} |
|
|
} |
|
|
@ -1309,25 +1310,17 @@ object AgentService : CoroutineScope { |
|
|
language = languages[0] |
|
|
language = languages[0] |
|
|
return try { |
|
|
return try { |
|
|
Log.i(TAG, "设置支持的语言: ${languages.joinToString(", ")}") |
|
|
Log.i(TAG, "设置支持的语言: ${languages.joinToString(", ")}") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
val asrSuccess = azureAsrHelper?.initialize( |
|
|
val asrSuccess = azureAsrHelper?.initialize( |
|
|
subscriptionKey = this.azureSpeechKey, |
|
|
subscriptionKey = this.azureSpeechKey, |
|
|
region = this.azureSpeechRegion, |
|
|
region = this.azureSpeechRegion, |
|
|
supportedLanguages = languages.toTypedArray(), |
|
|
supportedLanguages = languages.toTypedArray(), |
|
|
audioSourceType = audioSourceType |
|
|
audioSourceType = audioSourceType |
|
|
) ?: false |
|
|
) ?: false |
|
|
|
|
|
|
|
|
if (asrSuccess) { |
|
|
if (asrSuccess) { |
|
|
|
|
|
|
|
|
recognizeCallback() |
|
|
recognizeCallback() |
|
|
|
|
|
|
|
|
} else { |
|
|
} else { |
|
|
Log.w(TAG, "Azure ASR初始化失败") |
|
|
Log.w(TAG, "Azure ASR初始化失败") |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 同时更新TTS语言设置 |
|
|
// 同时更新TTS语言设置 |
|
|
val ttsSuccess = updateTtsLanguage(languages.first()) // 使用第一个语言作为TTS语言 |
|
|
val ttsSuccess = updateTtsLanguage(languages.first()) // 使用第一个语言作为TTS语言 |
|
|
|
|
|
|
|
|
@ -2550,15 +2543,17 @@ object AgentService : CoroutineScope { |
|
|
actionsObject.put(key, value) |
|
|
actionsObject.put(key, value) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
jsonObject.put("actions", actionsObject) |
|
|
jsonObject.put("usagetype", 3) // 3 表示AICHAT |
|
|
|
|
|
jsonObject.put("usages", actionsObject) |
|
|
|
|
|
|
|
|
val json = jsonObject.toString() |
|
|
val json = jsonObject.toString() |
|
|
|
|
|
Log.d(TAG, "统计事件发送成功,请求: $json") |
|
|
val mediaType = "application/json".toMediaType() |
|
|
val mediaType = "application/json".toMediaType() |
|
|
val body = json.toRequestBody(mediaType) |
|
|
val body = json.toRequestBody(mediaType) |
|
|
|
|
|
|
|
|
// 构建请求 |
|
|
// 构建请求 |
|
|
val request = Request.Builder() |
|
|
val request = Request.Builder() |
|
|
.url("$serverurl/api/home/user_pushaction") |
|
|
.url("$serverurl/api/home/user_usages") |
|
|
.addHeader("Authorization", usertoken) |
|
|
.addHeader("Authorization", usertoken) |
|
|
.addHeader("Content-Type", "application/json") |
|
|
.addHeader("Content-Type", "application/json") |
|
|
.post(body) |
|
|
.post(body) |
|
|
@ -2573,8 +2568,42 @@ object AgentService : CoroutineScope { |
|
|
|
|
|
|
|
|
override fun onResponse(call: Call, response: Response) { |
|
|
override fun onResponse(call: Call, response: Response) { |
|
|
response.use { |
|
|
response.use { |
|
|
|
|
|
// 仅读取一次响应体,避免重复读取导致异常,同时统一后续处理使用 |
|
|
|
|
|
val bodyStr = response.body?.string() ?: "" |
|
|
if (response.isSuccessful) { |
|
|
if (response.isSuccessful) { |
|
|
Log.d(TAG, "统计事件发送成功") |
|
|
Log.d(TAG, "统计事件发送成功,响应: $bodyStr") |
|
|
|
|
|
try { |
|
|
|
|
|
val json = JSONObject(bodyStr) |
|
|
|
|
|
val code = json.optInt("code", -1) |
|
|
|
|
|
val msg = json.optString("msg", "") |
|
|
|
|
|
val dataObj = json.optJSONObject("data") |
|
|
|
|
|
|
|
|
|
|
|
// 解析 data.usages 为 Map<String, Int> |
|
|
|
|
|
val dataMap = mutableMapOf<String, Int>() |
|
|
|
|
|
val usagesObj = dataObj?.optJSONObject("usages") |
|
|
|
|
|
if (usagesObj != null) { |
|
|
|
|
|
val keys = usagesObj.keys() |
|
|
|
|
|
while (keys.hasNext()) { |
|
|
|
|
|
val k = keys.next() |
|
|
|
|
|
dataMap[k] = usagesObj.optInt(k, 0) |
|
|
|
|
|
} |
|
|
|
|
|
for ((k, v) in dataMap) { |
|
|
|
|
|
Log.d(TAG, "统计事件发送成功,响应: $k -> $v") |
|
|
|
|
|
if(k == "3"){ //AI积分更新 |
|
|
|
|
|
aiintegral = v |
|
|
|
|
|
// 广播一个细化事件,便于前端单独处理 AI 积分更新 |
|
|
|
|
|
sendEvent( |
|
|
|
|
|
"assistant_usages", |
|
|
|
|
|
mapOf( |
|
|
|
|
|
"integral" to aiintegral |
|
|
|
|
|
) |
|
|
|
|
|
) |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
} catch (e: Exception) { |
|
|
|
|
|
Log.w(TAG, "解析统计响应失败: ${e.message}") |
|
|
|
|
|
} |
|
|
} else { |
|
|
} else { |
|
|
Log.w(TAG, "统计事件发送失败,HTTP状态码: ${response.code}") |
|
|
Log.w(TAG, "统计事件发送失败,HTTP状态码: ${response.code}") |
|
|
} |
|
|
} |
|
|
|