|
|
|
@ -15,7 +15,8 @@ import kotlinx.coroutines.launch |
|
|
|
import android.content.Intent |
|
|
|
import android.os.Build |
|
|
|
|
|
|
|
class AgentServicePlugin : FlutterPlugin, MethodCallHandler, EventChannel.StreamHandler, AgentServiceListener { |
|
|
|
class AgentServicePlugin : FlutterPlugin, MethodCallHandler, EventChannel.StreamHandler, |
|
|
|
AgentServiceListener { |
|
|
|
private val TAG = "AgentServicePlugin" |
|
|
|
private lateinit var context: Context |
|
|
|
private lateinit var methodChannel: MethodChannel |
|
|
|
@ -24,12 +25,16 @@ class AgentServicePlugin : FlutterPlugin, MethodCallHandler, EventChannel.Stream |
|
|
|
|
|
|
|
override fun onAttachedToEngine(@NonNull flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) { |
|
|
|
context = flutterPluginBinding.applicationContext |
|
|
|
methodChannel = MethodChannel(flutterPluginBinding.binaryMessenger, "com.yunqiinnovation.agent_service") |
|
|
|
methodChannel = |
|
|
|
MethodChannel(flutterPluginBinding.binaryMessenger, "com.yunqiinnovation.agent_service") |
|
|
|
methodChannel.setMethodCallHandler(this) |
|
|
|
|
|
|
|
eventChannel = EventChannel(flutterPluginBinding.binaryMessenger, "com.yunqiinnovation.agent_service/events") |
|
|
|
|
|
|
|
eventChannel = EventChannel( |
|
|
|
flutterPluginBinding.binaryMessenger, |
|
|
|
"com.yunqiinnovation.agent_service/events" |
|
|
|
) |
|
|
|
eventChannel.setStreamHandler(this) |
|
|
|
|
|
|
|
|
|
|
|
// 添加监听器 |
|
|
|
AgentService.addListener(this) |
|
|
|
|
|
|
|
@ -45,37 +50,42 @@ class AgentServicePlugin : FlutterPlugin, MethodCallHandler, EventChannel.Stream |
|
|
|
result.error("INVALID_ARGUMENTS", "配置参数不能为空", null) |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
Log.d(TAG, "启动BleMediaService,配置参数: $config") |
|
|
|
|
|
|
|
|
|
|
|
// 创建Intent启动BleMediaService |
|
|
|
val intent = Intent(context, BleMediaService::class.java) |
|
|
|
|
|
|
|
|
|
|
|
// 将参数添加到Intent |
|
|
|
config["azureSpeechKey"]?.toString()?.let { intent.putExtra("azureSpeechKey", it) } |
|
|
|
config["azureSpeechRegion"]?.toString()?.let { intent.putExtra("azureSpeechRegion", it) } |
|
|
|
config["azureSpeechKey"]?.toString() |
|
|
|
?.let { intent.putExtra("azureSpeechKey", it) } |
|
|
|
config["azureSpeechRegion"]?.toString() |
|
|
|
?.let { intent.putExtra("azureSpeechRegion", it) } |
|
|
|
config["openaiApiKey"]?.toString()?.let { intent.putExtra("openaiApiKey", it) } |
|
|
|
config["openaiBaseUrl"]?.toString()?.let { intent.putExtra("openaiBaseUrl", it) } |
|
|
|
config["openaiBaseUrl"]?.toString() |
|
|
|
?.let { intent.putExtra("openaiBaseUrl", it) } |
|
|
|
config["openaiModel"]?.toString()?.let { intent.putExtra("openaiModel", it) } |
|
|
|
config["openaivisionModel"]?.toString()?.let { intent.putExtra("openaivisionModel", it) } |
|
|
|
config["openaivisionModel"]?.toString() |
|
|
|
?.let { intent.putExtra("openaivisionModel", it) } |
|
|
|
config["systemPrompt"]?.toString()?.let { intent.putExtra("systemPrompt", it) } |
|
|
|
config["mcpServer"]?.toString()?.let { intent.putExtra("mcpServer", it) } |
|
|
|
config["volcanoAppId"]?.toString()?.let { intent.putExtra("volcanoAppId", it) } |
|
|
|
config["volcanoToken"]?.toString()?.let { intent.putExtra("volcanoToken", it) } |
|
|
|
|
|
|
|
|
|
|
|
// 启动前台服务 |
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { |
|
|
|
context.startForegroundService(intent) |
|
|
|
} else { |
|
|
|
context.startService(intent) |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
result.success(true) |
|
|
|
} catch (e: Exception) { |
|
|
|
Log.e(TAG, "启动AgentService失败", e) |
|
|
|
result.error("SERVICE_START_ERROR", "启动AgentService失败: ${e.message}", null) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
"stopAgentService" -> { |
|
|
|
try { |
|
|
|
val intent = Intent(context, BleMediaService::class.java) |
|
|
|
@ -86,6 +96,7 @@ class AgentServicePlugin : FlutterPlugin, MethodCallHandler, EventChannel.Stream |
|
|
|
result.error("SERVICE_STOP_ERROR", "停止AgentService失败: ${e.message}", null) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
"startConversation" -> { |
|
|
|
try { |
|
|
|
val isStarted = AgentService.startRecognition(false) |
|
|
|
@ -95,6 +106,11 @@ class AgentServicePlugin : FlutterPlugin, MethodCallHandler, EventChannel.Stream |
|
|
|
result.error("START_ERROR", "启动对话失败: ${e.message}", null) |
|
|
|
} |
|
|
|
} |
|
|
|
"recognizeCallback" -> { |
|
|
|
val isStarted = AgentService.recognizeCallback() |
|
|
|
result.success(isStarted) |
|
|
|
} |
|
|
|
|
|
|
|
"stopConversation" -> { |
|
|
|
try { |
|
|
|
AgentService.stopRecognition() |
|
|
|
@ -104,45 +120,29 @@ class AgentServicePlugin : FlutterPlugin, MethodCallHandler, EventChannel.Stream |
|
|
|
result.error("STOP_ERROR", "停止对话失败: ${e.message}", null) |
|
|
|
} |
|
|
|
} |
|
|
|
"audioContinue" -> { |
|
|
|
try { |
|
|
|
AgentService.audioContinue() |
|
|
|
result.success(true) |
|
|
|
} catch (e: Exception) { |
|
|
|
Log.e(TAG, "audioContinue失败", e) |
|
|
|
result.error("STOP_ERROR", "audioContinue失败: ${e.message}", null) |
|
|
|
} |
|
|
|
} |
|
|
|
"audioPause" -> { |
|
|
|
try { |
|
|
|
AgentService.audioPause() |
|
|
|
result.success(true) |
|
|
|
} catch (e: Exception) { |
|
|
|
Log.e(TAG, "audioContinue失败", e) |
|
|
|
result.error("STOP_ERROR", "audioContinue失败: ${e.message}", null) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
"processTextInput" -> { |
|
|
|
val text = call.argument<String>("text") |
|
|
|
if (text.isNullOrEmpty()) { |
|
|
|
result.error("INVALID_ARGUMENTS", "文本不能为空", null) |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
val speakResponse = call.argument<Boolean>("speakResponse") ?: false |
|
|
|
|
|
|
|
|
|
|
|
try { |
|
|
|
val isProcessing = AgentService.processTextInput( |
|
|
|
text = text, |
|
|
|
speakResponse = speakResponse |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
result.success(isProcessing) |
|
|
|
} catch (e: Exception) { |
|
|
|
Log.e(TAG, "处理文本输入失败", e) |
|
|
|
result.error("TEXT_PROCESS_ERROR", "处理文本输入失败: ${e.message}", null) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
"processImageInput" -> { |
|
|
|
val imagePath = call.argument<String>("imagePath") |
|
|
|
if (imagePath.isNullOrEmpty()) { |
|
|
|
@ -156,7 +156,7 @@ class AgentServicePlugin : FlutterPlugin, MethodCallHandler, EventChannel.Stream |
|
|
|
} |
|
|
|
val text = call.argument<String>("text") ?: "" |
|
|
|
val speakResponse = call.argument<Boolean>("speakResponse") ?: false |
|
|
|
|
|
|
|
|
|
|
|
try { |
|
|
|
val isProcessing = AgentService.processImageInput( |
|
|
|
imagePath = imagePath, |
|
|
|
@ -164,20 +164,21 @@ class AgentServicePlugin : FlutterPlugin, MethodCallHandler, EventChannel.Stream |
|
|
|
text = text, |
|
|
|
speakResponse = speakResponse |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
result.success(isProcessing) |
|
|
|
} catch (e: Exception) { |
|
|
|
Log.e(TAG, "处理图片输入失败", e) |
|
|
|
result.error("IMAGE_PROCESS_ERROR", "处理图片输入失败: ${e.message}", null) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
"speakText" -> { |
|
|
|
val text = call.argument<String>("text") |
|
|
|
if (text.isNullOrEmpty()) { |
|
|
|
result.error("INVALID_ARGUMENTS", "文本不能为空", null) |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
try { |
|
|
|
AgentService.speakText(text) |
|
|
|
result.success(true) |
|
|
|
@ -186,6 +187,7 @@ class AgentServicePlugin : FlutterPlugin, MethodCallHandler, EventChannel.Stream |
|
|
|
result.error("TTS_ERROR", "语音合成失败: ${e.message}", null) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
"stopTts" -> { |
|
|
|
try { |
|
|
|
AgentService.stopTts() |
|
|
|
@ -195,6 +197,7 @@ class AgentServicePlugin : FlutterPlugin, MethodCallHandler, EventChannel.Stream |
|
|
|
result.error("STOP_TTS_ERROR", "停止语音合成失败: ${e.message}", null) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
"stopAiSteam" -> { |
|
|
|
try { |
|
|
|
AgentService.stopAiStream() |
|
|
|
@ -215,6 +218,7 @@ class AgentServicePlugin : FlutterPlugin, MethodCallHandler, EventChannel.Stream |
|
|
|
result.error("CLEAR_HISTORY_ERROR", "清除聊天历史失败: ${e.message}", null) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
"interruptCurrentResponse" -> { |
|
|
|
try { |
|
|
|
AgentService.interruptCurrentResponse() |
|
|
|
@ -224,6 +228,7 @@ class AgentServicePlugin : FlutterPlugin, MethodCallHandler, EventChannel.Stream |
|
|
|
result.error("INTERRUPT_ERROR", "中断响应失败: ${e.message}", null) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
"dispose" -> { |
|
|
|
try { |
|
|
|
AgentService.dispose() |
|
|
|
@ -233,6 +238,7 @@ class AgentServicePlugin : FlutterPlugin, MethodCallHandler, EventChannel.Stream |
|
|
|
result.error("DISPOSE_ERROR", "释放资源失败: ${e.message}", null) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
else -> { |
|
|
|
result.notImplemented() |
|
|
|
} |
|
|
|
@ -246,7 +252,7 @@ class AgentServicePlugin : FlutterPlugin, MethodCallHandler, EventChannel.Stream |
|
|
|
override fun onCancel(arguments: Any?) { |
|
|
|
this.eventSink = null |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
* 实现AgentServiceListener接口 |
|
|
|
* 接收来自AgentService的事件,并转发到Flutter |
|
|
|
@ -257,7 +263,7 @@ class AgentServicePlugin : FlutterPlugin, MethodCallHandler, EventChannel.Stream |
|
|
|
put("event", eventName) |
|
|
|
put("data", JSONObject(data)) |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 在主线程发送事件到Flutter |
|
|
|
MainScope().launch { |
|
|
|
eventSink?.success(eventData.toString()) |
|
|
|
|