Skip to content

Commit

Permalink
Handle each asynckey's state #2497
Browse files Browse the repository at this point in the history
  • Loading branch information
sangho.lee authored and shshshl09 committed Jul 1, 2024
1 parent cc2b5c0 commit 739bfc8
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -590,8 +590,7 @@ class DefaultClientSpeechRecognizer(

override fun onReceiveDirectives(
dialogRequestId: String,
directives: List<Directive>,
asyncKey: AsyncKey?
directiveAndAsyncKeys: List<Pair<Directive, AsyncKey?>>
): Boolean {
val request = currentRequest
if (request == null) {
Expand All @@ -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) {
Expand All @@ -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())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -372,8 +372,7 @@ class DefaultServerSpeechRecognizer(

override fun onReceiveDirectives(
dialogRequestId: String,
directives: List<Directive>,
asyncKey: AsyncKey?
directiveAndAsyncKeys: List<Pair<Directive, AsyncKey?>>
): Boolean {
val request = currentRequest
if (request == null) {
Expand All @@ -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()) {
Expand All @@ -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())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -100,4 +100,4 @@ class InputProcessorManagerTest {
it.join()
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Directive>, asyncKey: AsyncKey? = null): Boolean
fun onReceiveDirectives(dialogRequestId: String, directiveAndAsyncKeys: List<Pair<Directive, AsyncKey?>>): Boolean
fun onResponseTimeout(dialogRequestId: String)
}

0 comments on commit 739bfc8

Please sign in to comment.