Browse Source

上传代码

newdev_shunjiawei
liwei1dao 1 year ago
parent
commit
71d8a0ca99
  1. 2
      local_plugins/agent_service/android/src/main/kotlin/com/yunqiinnovation/agent_service/AgentService.kt
  2. 30
      local_plugins/chat_api/android/src/main/kotlin/com/yunqiinnovation/chat_api/ChatApiService.kt
  3. 13
      local_plugins/chat_api/android/src/main/kotlin/com/yunqiinnovation/chat_api/MCPClient.kt

2
local_plugins/agent_service/android/src/main/kotlin/com/yunqiinnovation/agent_service/AgentService.kt

@ -900,7 +900,7 @@ object AgentService : CoroutineScope {
return
}
responseBuilder.append(token)
if (speakResponse && reply && broadcast) {
if (speakResponse && reply && broadcast && token.isNotEmpty()) {
ttsService?.speakStream(token)
// 在开始流式TTS时立即停止气泡音
audioPlayer?.stopAudio()

30
local_plugins/chat_api/android/src/main/kotlin/com/yunqiinnovation/chat_api/ChatApiService.kt

@ -364,7 +364,7 @@ class ChatApiService(private val context: android.content.Context? = null) : Cor
toolCalls.clear()
// 检查是否包含图片,决定使用哪个模型
val currentModel = if (containsImage(messages)) visionModel else model
// Log.d(TAG, "liwei-------------------------开始AI 对话 0")
Log.d(TAG, "liwei-------------------------开始AI 对话 0")
currentStreamJob = launch {
try {
// 转换消息格式
@ -374,22 +374,22 @@ class ChatApiService(private val context: android.content.Context? = null) : Cor
Log.e("ChatApiService", "转换消息格式失败: ${e.message}", e)
throw e
}
// Log.d(TAG, "liwei-------------------------开始AI 对话 1")
Log.d(TAG, "liwei-------------------------开始AI 对话 1")
// 直接获取工具列表
val tools = getOpenAiTools()
// Log.d(TAG, "liwei-------------------------开始AI 对话 2")
Log.d(TAG, "liwei-------------------------开始AI 对话 2")
// 构建请求
if (currentModel.isEmpty()) {
Log.e("ChatApiService", "模型名称为空")
throw IllegalArgumentException("模型名称不能为空")
}
// Log.d(TAG, "liwei-------------------------开始AI 对话 3")
Log.d(TAG, "liwei-------------------------开始AI 对话 3")
if (chatMessages.isEmpty()) {
Log.e("ChatApiService", "消息列表为空")
throw IllegalArgumentException("消息列表不能为空")
}
// Log.e("ChatApiService", "AI解析工具列表: ${tools}")
// Log.d(TAG, "liwei-------------------------开始AI 对话 4")
Log.d(TAG, "liwei-------------------------开始AI 对话 4")
val chatsFlow = try {
val chatCompletionRequest = ChatCompletionRequest(
model = ModelId(currentModel),
@ -398,14 +398,14 @@ class ChatApiService(private val context: android.content.Context? = null) : Cor
temperature = 0.7,
tools = if (tools.isNotEmpty()) tools else null
)
// Log.d(TAG, "liwei-------------------------开始AI 对话 5")
Log.d(TAG, "liwei-------------------------开始AI 对话 5")
if (openAI == null) {
Log.e("ChatApiService", "openAI对象为null")
throw IllegalStateException("OpenAI客户端未初始化")
}
Log.e("ChatApiService", "liwei---------,发送AI请求 model:${currentModel} messages:${chatMessages}")
val flow = openAI!!.chatCompletions(chatCompletionRequest)
// Log.d(TAG, "liwei-------------------------开始AI 对话 6")
Log.d(TAG, "liwei-------------------------开始AI 对话 6")
flow
} catch (e: Exception) {
Log.e("ChatApiService", "创建ChatCompletionRequest或调用chatCompletions失败: ${e.message}", e)
@ -413,7 +413,7 @@ class ChatApiService(private val context: android.content.Context? = null) : Cor
}
var replyContext = "";
// Log.d(TAG, "liwei-------------------------开始AI 对话 7")
Log.d(TAG, "liwei-------------------------开始AI 对话 7")
chatsFlow.collect { result ->
// val usage = result.usage
// if (usage != null){ //发现流式不返回这歌数据
@ -424,19 +424,19 @@ class ChatApiService(private val context: android.content.Context? = null) : Cor
// throw CancellationException("Session cancelled")
return@collect
}
// Log.d(TAG, "liwei-------------------------开始AI 对话 7-1")
Log.d(TAG, "liwei-------------------------开始AI 对话 7-1")
val choice = result.choices.firstOrNull()
if (choice == null) {
Log.w("ChatApiService", "[Session: $sessionid] choice为null")
return@collect
}
// Log.d(TAG, "liwei-------------------------开始AI 对话 7-2")
Log.d(TAG, "liwei-------------------------开始AI 对话 7-2")
val delta = choice.delta
if (delta == null) {
Log.w("ChatApiService", "[Session: $sessionid] delta为null")
return@collect
}
// Log.d(TAG, "liwei-------------------------开始AI 对话 7-3")
Log.d(TAG, "liwei-------------------------开始AI 对话 7-3")
// 处理普通文本内容
delta.content?.let { content ->
try {
@ -447,7 +447,7 @@ class ChatApiService(private val context: android.content.Context? = null) : Cor
// 不重新抛出异常,继续处理后续内容
}
}
// Log.d(TAG, "liwei-------------------------开始AI 对话 7-4")
Log.d(TAG, "liwei-------------------------开始AI 对话 7-4")
// 收集工具调用信息
delta.toolCalls?.forEach { toolCall ->
try {
@ -499,10 +499,10 @@ class ChatApiService(private val context: android.content.Context? = null) : Cor
val promptTokens = estimatePromptTokens(chatMessages); //计算上传token
val completionTokens = roughTokenCount(replyContext); //计算上传token
getSessionCallback(sessionid)?.onUsage(sessionid,promptTokens,completionTokens,promptTokens+completionTokens)
// Log.d(TAG, "liwei-------------------------开始AI 对话 8")
Log.d(TAG, "liwei-------------------------开始AI 对话 8")
// 完成处理
if (sessionid == currSessionId) {
// Log.d(TAG, "liwei-------------------------开始AI 对话 9")
Log.d(TAG, "liwei-------------------------开始AI 对话 9")
// 检查是否有工具调用需要处理
val hasToolCalls = processToolCalls(sessionid)
if (!hasToolCalls) {
@ -514,7 +514,7 @@ class ChatApiService(private val context: android.content.Context? = null) : Cor
clearCurrentSession()
}
}
// Log.d(TAG, "liwei-------------------------开始AI 对话 10")
Log.d(TAG, "liwei-------------------------开始AI 对话 10")
} catch (e: CancellationException) {
// 会话被取消,不需要处理
Log.d(TAG, "Session $sessionid cancelled")

13
local_plugins/chat_api/android/src/main/kotlin/com/yunqiinnovation/chat_api/MCPClient.kt

@ -68,6 +68,19 @@ class MCPClient(private val context: Context? = null) : AutoCloseable {
Log.w(TAG, "Failed to initialize system functions", e)
}
}
/**
* 初始化音乐函数
*/
private fun initializeQQMusiceFunctions() {
try {
val handler = QQMusicSystemFunctionHandler(context)
handler.registerAllFunctions(this)
} catch (e: Exception) {
Log.w(TAG, "Failed to initialize system functions", e)
}
}
/**
* 连接到SSE服务器
* 直接接收完整的JSON配置字符串

Loading…
Cancel
Save