From 739bfc8156b71c865a54311631d9bd15f2d0d389 Mon Sep 17 00:00:00 2001 From: "sangho.lee" Date: Mon, 1 Jul 2024 16:25:20 +0900 Subject: [PATCH] Handle each asynckey's state #2497 --- .../asr/impl/DefaultClientSpeechRecognizer.kt | 14 +++++++------- .../asr/impl/DefaultServerSpeechRecognizer.kt | 14 ++++++++------ .../core/directivesequencer/DirectiveProcessor.kt | 2 +- .../core/inputprocessor/InputProcessorManager.kt | 10 ++++------ .../skt/nugu/sdk/core/utils/AsyncKeyExtensions.kt | 8 +++----- .../inputprocessor/InputProcessorManagerTest.kt | 6 +++--- .../interfaces/inputprocessor/InputProcessor.kt | 2 +- 7 files changed, 27 insertions(+), 29 deletions(-) diff --git a/nugu-agent/src/main/java/com/skt/nugu/sdk/agent/asr/impl/DefaultClientSpeechRecognizer.kt b/nugu-agent/src/main/java/com/skt/nugu/sdk/agent/asr/impl/DefaultClientSpeechRecognizer.kt index 42358ea78..569a099ad 100644 --- a/nugu-agent/src/main/java/com/skt/nugu/sdk/agent/asr/impl/DefaultClientSpeechRecognizer.kt +++ b/nugu-agent/src/main/java/com/skt/nugu/sdk/agent/asr/impl/DefaultClientSpeechRecognizer.kt @@ -590,8 +590,7 @@ class DefaultClientSpeechRecognizer( override fun onReceiveDirectives( dialogRequestId: String, - directives: List, - asyncKey: AsyncKey? + directiveAndAsyncKeys: List> ): Boolean { val request = currentRequest if (request == null) { @@ -611,13 +610,13 @@ class DefaultClientSpeechRecognizer( inputProcessorManager.onRequested(this, dialogRequestId) val receiveResponse = - directives.any { - it.header.namespace != DefaultASRAgent.NAMESPACE && - !(it.header.namespace == "Adot" && it.header.name == "AckMessage") && + directiveAndAsyncKeys.any {(directive, asyncKey)-> + directive.header.namespace != DefaultASRAgent.NAMESPACE && + !(directive.header.namespace == "Adot" && directive.header.name == "AckMessage") && (asyncKey == null || asyncKey.state == AsyncKey.State.END) } - Logger.d(TAG, "[onReceiveResponse] receiveResponse: $receiveResponse and asyncKey: ${asyncKey}") + Logger.d(TAG, "[onReceiveResponse] receiveResponse: $receiveResponse") return synchronized(request) { if (receiveResponse) { @@ -630,7 +629,8 @@ class DefaultClientSpeechRecognizer( } true } else { - directives.filter { it.header.namespace == DefaultASRAgent.NAMESPACE && it.header.name == DefaultASRAgent.NAME_NOTIFY_RESULT } + directiveAndAsyncKeys.filter { (directive, _) -> directive.header.namespace == DefaultASRAgent.NAMESPACE && directive.header.name == DefaultASRAgent.NAME_NOTIFY_RESULT } + .map { it.first } .forEach { request.shouldBeNotifyResult.add(it.getMessageId()) } diff --git a/nugu-agent/src/main/java/com/skt/nugu/sdk/agent/asr/impl/DefaultServerSpeechRecognizer.kt b/nugu-agent/src/main/java/com/skt/nugu/sdk/agent/asr/impl/DefaultServerSpeechRecognizer.kt index 40cca21ed..09a91bba6 100644 --- a/nugu-agent/src/main/java/com/skt/nugu/sdk/agent/asr/impl/DefaultServerSpeechRecognizer.kt +++ b/nugu-agent/src/main/java/com/skt/nugu/sdk/agent/asr/impl/DefaultServerSpeechRecognizer.kt @@ -372,8 +372,7 @@ class DefaultServerSpeechRecognizer( override fun onReceiveDirectives( dialogRequestId: String, - directives: List, - asyncKey: AsyncKey? + directiveAndAsyncKeys: List> ): Boolean { val request = currentRequest if (request == null) { @@ -393,12 +392,14 @@ class DefaultServerSpeechRecognizer( inputProcessorManager.onRequested(this, dialogRequestId) val receiveResponse = - directives.any { - it.header.namespace != DefaultASRAgent.NAMESPACE && - !(it.header.namespace == "Adot" && it.header.name == "AckMessage") && + directiveAndAsyncKeys.any {(directive, asyncKey)-> + directive.header.namespace != DefaultASRAgent.NAMESPACE && + !(directive.header.namespace == "Adot" && directive.header.name == "AckMessage") && (asyncKey == null || asyncKey.state == AsyncKey.State.END) } + Logger.d(TAG, "[onReceiveResponse] receiveResponse: $receiveResponse") + return synchronized(request) { if (receiveResponse) { if (request.shouldBeNotifyResult.isEmpty()) { @@ -410,7 +411,8 @@ class DefaultServerSpeechRecognizer( } true } else { - directives.filter { it.header.namespace == DefaultASRAgent.NAMESPACE && it.header.name == DefaultASRAgent.NAME_NOTIFY_RESULT } + directiveAndAsyncKeys.filter { (directive, _) -> directive.header.namespace == DefaultASRAgent.NAMESPACE && directive.header.name == DefaultASRAgent.NAME_NOTIFY_RESULT } + .map { it.first } .forEach { request.shouldBeNotifyResult.add(it.getMessageId()) } diff --git a/nugu-core/src/main/java/com/skt/nugu/sdk/core/directivesequencer/DirectiveProcessor.kt b/nugu-core/src/main/java/com/skt/nugu/sdk/core/directivesequencer/DirectiveProcessor.kt index be247b1e6..ab29e2a23 100644 --- a/nugu-core/src/main/java/com/skt/nugu/sdk/core/directivesequencer/DirectiveProcessor.kt +++ b/nugu-core/src/main/java/com/skt/nugu/sdk/core/directivesequencer/DirectiveProcessor.kt @@ -427,7 +427,7 @@ class DirectiveProcessor( val directive = directiveAndPolicy.directive // first get asyncKey's dialogRequestId, if not exist check dialogRequestId - val dialogRequestId = directive.getAsyncKey()?.eventDialogRequestId ?: directive.getDialogRequestId() + val dialogRequestId = directive.payload.getAsyncKey()?.eventDialogRequestId ?: directive.getDialogRequestId() val blockedMediums = blockedMediumsMap[dialogRequestId] ?: return directiveAndPolicy diff --git a/nugu-core/src/main/java/com/skt/nugu/sdk/core/inputprocessor/InputProcessorManager.kt b/nugu-core/src/main/java/com/skt/nugu/sdk/core/inputprocessor/InputProcessorManager.kt index 4679a802c..86ac90eb0 100644 --- a/nugu-core/src/main/java/com/skt/nugu/sdk/core/inputprocessor/InputProcessorManager.kt +++ b/nugu-core/src/main/java/com/skt/nugu/sdk/core/inputprocessor/InputProcessorManager.kt @@ -15,12 +15,9 @@ */ package com.skt.nugu.sdk.core.inputprocessor -import com.google.gson.Gson -import com.google.gson.annotations.SerializedName import com.skt.nugu.sdk.core.interfaces.directive.DirectiveGroupProcessorInterface import com.skt.nugu.sdk.core.interfaces.inputprocessor.InputProcessor import com.skt.nugu.sdk.core.interfaces.inputprocessor.InputProcessorManagerInterface -import com.skt.nugu.sdk.core.interfaces.message.AsyncKey import com.skt.nugu.sdk.core.interfaces.message.Directive import com.skt.nugu.sdk.core.utils.Logger import com.skt.nugu.sdk.core.utils.getAsyncKey @@ -40,8 +37,7 @@ class InputProcessorManager(private val timeoutInMilliSeconds: Long = 10 * 1000L val sampleDirective = directives.firstOrNull() ?: return val directiveDialogRequestId = sampleDirective.header.dialogRequestId - val asyncKey = sampleDirective.getAsyncKey() - val asyncKeyEventDialogRequestId = asyncKey?.eventDialogRequestId + val asyncKeyEventDialogRequestId = sampleDirective.payload.getAsyncKey()?.eventDialogRequestId val dialogRequestId = if(requests.containsKey(directiveDialogRequestId)) { directiveDialogRequestId @@ -55,7 +51,9 @@ class InputProcessorManager(private val timeoutInMilliSeconds: Long = 10 * 1000L return } - val receiveResponse = requests[dialogRequestId]?.onReceiveDirectives(dialogRequestId, directives, asyncKey) ?: false + val receiveResponse = requests[dialogRequestId]?.onReceiveDirectives(dialogRequestId, directives.map { + it to it.payload.getAsyncKey() + }) ?: false if(receiveResponse) { dialogRequestId.let { diff --git a/nugu-core/src/main/java/com/skt/nugu/sdk/core/utils/AsyncKeyExtensions.kt b/nugu-core/src/main/java/com/skt/nugu/sdk/core/utils/AsyncKeyExtensions.kt index cc2ae5309..89008afe5 100644 --- a/nugu-core/src/main/java/com/skt/nugu/sdk/core/utils/AsyncKeyExtensions.kt +++ b/nugu-core/src/main/java/com/skt/nugu/sdk/core/utils/AsyncKeyExtensions.kt @@ -3,12 +3,10 @@ package com.skt.nugu.sdk.core.utils import com.google.gson.Gson import com.google.gson.annotations.SerializedName import com.skt.nugu.sdk.core.interfaces.message.AsyncKey -import com.skt.nugu.sdk.core.interfaces.message.Directive - -fun Directive.getAsyncKey(): AsyncKey? = runCatching { - if (payload.contains("\"asyncKey\"")) { +fun String.getAsyncKey(): AsyncKey? = runCatching { + if (contains("\"asyncKey\"")) { Gson().fromJson( - payload, + this, AsyncKeyPayload::class.java ).asyncKey } else null diff --git a/nugu-core/src/test/java/com/skt/nugu/sdk/core/inputprocessor/InputProcessorManagerTest.kt b/nugu-core/src/test/java/com/skt/nugu/sdk/core/inputprocessor/InputProcessorManagerTest.kt index 077f98294..3f9929f77 100644 --- a/nugu-core/src/test/java/com/skt/nugu/sdk/core/inputprocessor/InputProcessorManagerTest.kt +++ b/nugu-core/src/test/java/com/skt/nugu/sdk/core/inputprocessor/InputProcessorManagerTest.kt @@ -67,12 +67,12 @@ class InputProcessorManagerTest { ) val inputProcessor: InputProcessor = mock() - whenever(inputProcessor.onReceiveDirectives(dialogRequestId, received)).thenReturn(true) + whenever(inputProcessor.onReceiveDirectives(dialogRequestId, received.map { it to null })).thenReturn(true) manager.onRequested(inputProcessor, dialogRequestId) manager.onPostProcessed(received) - verify(inputProcessor).onReceiveDirectives(dialogRequestId, received) + verify(inputProcessor).onReceiveDirectives(dialogRequestId, received.map { it to null }) Thread.sleep(100) verify(inputProcessor, never()).onResponseTimeout(dialogRequestId) verify(listener, never()).onResponseTimeout(dialogRequestId) @@ -100,4 +100,4 @@ class InputProcessorManagerTest { it.join() } } -} \ No newline at end of file +} diff --git a/nugu-interface/src/main/java/com/skt/nugu/sdk/core/interfaces/inputprocessor/InputProcessor.kt b/nugu-interface/src/main/java/com/skt/nugu/sdk/core/interfaces/inputprocessor/InputProcessor.kt index 862ce30a1..06d6806ea 100644 --- a/nugu-interface/src/main/java/com/skt/nugu/sdk/core/interfaces/inputprocessor/InputProcessor.kt +++ b/nugu-interface/src/main/java/com/skt/nugu/sdk/core/interfaces/inputprocessor/InputProcessor.kt @@ -20,6 +20,6 @@ import com.skt.nugu.sdk.core.interfaces.message.Directive interface InputProcessor { fun onSendEventFinished(dialogRequestId: String) - fun onReceiveDirectives(dialogRequestId: String, directives: List, asyncKey: AsyncKey? = null): Boolean + fun onReceiveDirectives(dialogRequestId: String, directiveAndAsyncKeys: List>): Boolean fun onResponseTimeout(dialogRequestId: String) }