Skip to content

Commit

Permalink
Initial support of updated Alice API (#19)
Browse files Browse the repository at this point in the history
* Initial support of updated Alice API

* fixes according to reviewer comments

Co-authored-by: Pavel Kaplya <[email protected]>
  • Loading branch information
Pazus and Pavel Kaplya authored Jun 23, 2020
1 parent 7566f43 commit 5820b31
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import com.justai.jaicf.channel.yandexalice.api.model.Image
import com.justai.jaicf.channel.yandexalice.api.model.ItemsList
import com.justai.jaicf.reactions.Reactions
import com.justai.jaicf.reactions.ResponseReactions
import kotlinx.serialization.json.JsonElement
import kotlinx.serialization.json.JsonObject

val Reactions.alice
Expand Down Expand Up @@ -71,4 +72,16 @@ class AliceReactions(
response.response = null
response.startAccountLinking = JsonObject(mapOf())
}
}

fun sessionState(state: JsonObject) {
response.sessionState = state
}

fun updateUserState(key: String, value: JsonElement?) {
response.userStateUpdate[key] = value
}

fun deleteFromUserState(key: String) {
response.userStateUpdate[key] = null
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.justai.jaicf.channel.yandexalice.api
import com.justai.jaicf.api.BotRequest
import com.justai.jaicf.api.BotRequestType
import com.justai.jaicf.channel.yandexalice.AliceEvent
import com.justai.jaicf.channel.yandexalice.api.model.AliceIntent
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.JsonElement
Expand All @@ -17,10 +18,11 @@ data class AliceBotRequest(
val meta: Meta? = null,
val session: Session,
val request: Request? = null,
val state: State? = null,
@SerialName("account_linking_complete_event")
val accountLinkingCompleteEvent: JsonObject? = null
): BotRequest {
override val clientId = session.userId
override val clientId = session.application.applicationId
val headers = mutableMapOf<String, List<String>>()

val accessToken by lazy {
Expand Down Expand Up @@ -59,10 +61,39 @@ data class Session(
val messageId: Int,
@SerialName("session_id")
val sessionId: String,
@SerialName("skill_id")
val skillId: String,
val application: Application,
val user: User? = null
) {
// see: https://yandex.ru/dev/dialogs/alice/doc/protocol-docpage/
// application.applicationId contains the same value `user_id` contained
@Deprecated(
message = "use application.applicationId instead as recommended in documentation",
replaceWith = ReplaceWith(expression = "application.applicationId")
)
val userId: String
get() = application.applicationId
}

@Serializable
data class User(
@SerialName("user_id")
val userId: String,
@SerialName("skill_id")
val skillId: String
@SerialName("access_token")
val accessToken: String?
)

@Serializable
data class Application(
@SerialName("application_id")
val applicationId: String
)

@Serializable
data class State(
val session: JsonObject? = null,
val user: Map<String, JsonElement> = emptyMap()
)

@Serializable
Expand All @@ -78,7 +109,11 @@ data class Request(
@Serializable
data class Nlu(
val tokens: List<String>,
val entities: List<Entity>
val entities: List<Entity>,
/**
* @see AliceIntent for predefined
* */
val intents: Map<String, Intent>
) {
@Serializable
data class Entity(
Expand All @@ -92,5 +127,23 @@ data class Request(
val end: Int
)
}

@Serializable
data class Intent(
val slots: Map<String, Slot> = emptyMap()
)

@Serializable
data class Slot(
val type: String,
val value: JsonElement,
val tokens: Tokens?
)

@Serializable
data class Tokens(
val start: Int,
val end: Int
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,29 @@ import com.justai.jaicf.channel.yandexalice.api.model.Button
import com.justai.jaicf.channel.yandexalice.api.model.Card
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.JsonElement
import kotlinx.serialization.json.JsonObject

@Serializable
data class AliceBotResponse(
var response: Response?,
@SerialName("start_account_linking")
var startAccountLinking: JsonObject? = null,
val session: Session,
val version: String
val version: String,
@Deprecated(message = "May be omitted")
val session: Session? = null,
/**
* User state is incremental, meaning that the skill returns only increment to the user_state in response as it's
* shared among all surfaces of the user and there might be a race in changing it. That's why it's of [MutableMap]
* type.
* By design a developer has to pass `null` value associated with a key to remove the key's entry from the user
* state.
*/
val userStateUpdate: MutableMap<String, JsonElement?> = mutableMapOf(),
var sessionState: JsonObject? = null
): BotResponse {

constructor(request: AliceBotRequest): this(Response(), null, request.session, request.version)
constructor(request: AliceBotRequest): this(Response(), null, request.version)

@Serializable
data class Response(
Expand All @@ -27,4 +38,4 @@ data class AliceBotResponse(
var card: Card? = null,
val buttons: MutableList<Button> = mutableListOf()
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.justai.jaicf.channel.yandexalice.api.model

object AliceIntent {
const val CONFIRM = "YANDEX.CONFIRM"
const val REJECT = "YANDEX.REJECT"
const val HELP = "YANDEX.HELP"
const val REPEAT = "YANDEX.REPEAT"
}

0 comments on commit 5820b31

Please sign in to comment.