Skip to content

Commit

Permalink
update models
Browse files Browse the repository at this point in the history
  • Loading branch information
Данила Беляков authored and Данила Беляков committed Nov 19, 2024
1 parent c41d16e commit e33fa1d
Show file tree
Hide file tree
Showing 75 changed files with 740 additions and 265 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,7 @@ bin/
.vscode/

### Mac OS ###
.DS_Store
.DS_Store

.idea
.kotlin
2 changes: 1 addition & 1 deletion alice-ktx/src/main/kotlin/com/github/alice/ktx/Skill.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package com.github.alice.ktx
import com.github.alice.ktx.api.dialog.DialogApi
import com.github.alice.ktx.common.AliceDsl
import com.github.alice.ktx.middleware.MiddlewareType
import com.github.alice.ktx.models.FSMStrategy
import com.github.alice.ktx.fsm.models.FSMStrategy
import com.github.alice.ktx.models.request.MessageRequest
import com.github.alice.ktx.models.response.MessageResponse
import com.github.alice.ktx.server.WebServer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ fun Skill.Builder.ktorYandexDialogApi(body: KtorYandexDialogApi.Builder.() -> Un
* @param oauthToken Токен для загрузки аудио и изображений.
* [Source](https://yandex.ru/dev/direct/doc/start/token.html)
* */
class KtorYandexDialogApi(
class KtorYandexDialogApi internal constructor(
private val oauthToken: String,
private val skillId: String,
private val json: Json,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package com.github.alice.ktx.common.serializers

import com.github.alice.ktx.models.request.nlu.NluEntity
import com.github.alice.ktx.models.request.nlu.NluEntityToken
import com.github.alice.ktx.models.request.nlu.NluEntityType
import com.github.alice.ktx.models.request.nlu.NluValue
import kotlinx.serialization.KSerializer
import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.descriptors.buildClassSerialDescriptor
import kotlinx.serialization.descriptors.element
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder
import kotlinx.serialization.json.*

internal object NluEntitySerializer : KSerializer<NluEntity> {
override val descriptor: SerialDescriptor = buildClassSerialDescriptor("NluEntity") {
element<NluEntityToken>("tokens")
element<NluEntityType>("type")
element<JsonElement>("value")
}

override fun serialize(encoder: Encoder, value: NluEntity) {
val jsonEncoder = encoder as? JsonEncoder ?: error("Only Json is supported")
val json = buildJsonObject {
put("tokens", jsonEncoder.json.encodeToJsonElement(value.tokens))
put("type", value.type.serialName())
put(
"value",
when (val nluValue = value.value) {
is NluValue.GeoValue -> jsonEncoder.json.encodeToJsonElement(nluValue)
is NluValue.FioValue -> jsonEncoder.json.encodeToJsonElement(nluValue)
is NluValue.DateTimeValue -> jsonEncoder.json.encodeToJsonElement(nluValue)
is NluValue.NumberValue -> JsonPrimitive(nluValue.number)
}
)
}
jsonEncoder.encodeJsonElement(json)
}

override fun deserialize(decoder: Decoder): NluEntity {
val jsonDecoder = decoder as? JsonDecoder ?: error("Only Json is supported")
val jsonObject = jsonDecoder.decodeJsonElement().jsonObject

val tokensJson = jsonObject["tokens"] ?: error("Missing 'tokens'")
val typeJson = jsonObject["type"]?.jsonPrimitive?.content ?: error("Missing 'type'")
val valueJson = jsonObject["value"] ?: error("Missing 'value'")

val nluEntityToken = jsonDecoder.json.decodeFromJsonElement<NluEntityToken>(tokensJson)
val type = fromSerialName(typeJson)

val nluValue: NluValue = when (type) {
NluEntityType.GEO -> jsonDecoder.json.decodeFromJsonElement<NluValue.GeoValue>(valueJson)
NluEntityType.FIO -> jsonDecoder.json.decodeFromJsonElement<NluValue.FioValue>(valueJson)
NluEntityType.DATETIME -> jsonDecoder.json.decodeFromJsonElement<NluValue.DateTimeValue>(valueJson)
NluEntityType.NUMBER -> NluValue.NumberValue(valueJson.jsonPrimitive.float)
}

return NluEntity(nluEntityToken, type, nluValue)
}
}

private fun NluEntityType.serialName(): String {
return when (this) {
NluEntityType.FIO -> "YANDEX.FIO"
NluEntityType.GEO -> "YANDEX.GEO"
NluEntityType.DATETIME -> "YANDEX.DATETIME"
NluEntityType.NUMBER -> "YANDEX.NUMBER"
}
}

private fun fromSerialName(serialName: String): NluEntityType {
return when (serialName) {
"YANDEX.FIO" -> NluEntityType.FIO
"YANDEX.GEO" -> NluEntityType.GEO
"YANDEX.DATETIME" -> NluEntityType.DATETIME
"YANDEX.NUMBER" -> NluEntityType.NUMBER
else -> throw IllegalArgumentException("Unknown type: $serialName")
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.github.alice.ktx.fsm

import com.github.alice.ktx.models.FSMStrategy
import com.github.alice.ktx.fsm.models.FSMStrategy
import kotlin.reflect.KClass

interface MutableFSMContext : ReadOnlyFSMContext {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.github.alice.ktx.fsm

import com.github.alice.ktx.models.FSMStrategy
import com.github.alice.ktx.fsm.models.FSMStrategy
import kotlin.reflect.KClass

interface ReadOnlyFSMContext {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.github.alice.ktx.fsm.impl

import com.github.alice.ktx.models.FSMStrategy
import com.github.alice.ktx.fsm.models.FSMStrategy
import com.github.alice.ktx.models.request.MessageRequest
import com.github.alice.ktx.fsm.FSMContext
import com.github.alice.ktx.storage.Storage
Expand All @@ -14,7 +14,7 @@ import kotlin.reflect.KClass
* @param message Сообщение запроса, содержащего начальное состояние и данные пользователя.
* @param defaultStrategy Стратегия конечного автомата состояний (FSM), используемая для управления состояниями.
*/
class BaseFSMContext(
class BaseFSMContext internal constructor(
private val storage: Storage,
private val defaultStrategy: FSMStrategy,
private val message: MessageRequest,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.github.alice.ktx.models
package com.github.alice.ktx.fsm.models

/**
* Перечисление `FSMStrategy` представляет различные стратегии конечного автомата состояний (FSM), которые могут использоваться
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package com.github.alice.ktx.handlers.environments

import com.github.alice.ktx.common.AliceDsl
import com.github.alice.ktx.fsm.MutableFSMContext
import com.github.alice.ktx.models.FSMStrategy
import com.github.alice.ktx.fsm.models.FSMStrategy

@AliceDsl
interface ProcessRequestEnvironment : ShouldRequestEnvironment {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.github.alice.ktx.handlers.filters

import com.github.alice.ktx.handlers.environments.ShouldRequestEnvironment
import com.github.alice.ktx.models.request.RequestContentType
import com.github.alice.ktx.models.request.content.RequestContentType

interface Filter {
fun checkFor(request: ShouldRequestEnvironment): Boolean = request.predicate()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import com.github.alice.ktx.common.AliceDsl
import com.github.alice.ktx.handlers.Handler
import com.github.alice.ktx.handlers.environments.ProcessRequestEnvironment
import com.github.alice.ktx.handlers.environments.ShouldRequestEnvironment
import com.github.alice.ktx.models.request.RequestContentType
import com.github.alice.ktx.models.request.content.RequestContentType
import com.github.alice.ktx.models.response.MessageResponse

@AliceDsl
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
package com.github.alice.ktx.models.request

import com.github.alice.ktx.models.request.content.RequestContent
import com.github.alice.ktx.models.request.meta.AccountLinking
import com.github.alice.ktx.models.request.meta.Metadata
import com.github.alice.ktx.models.request.session.Session
import com.github.alice.ktx.models.request.state.State
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

/**
* [Source](https://yandex.ru/dev/dialogs/alice/doc/ru/request)
*
* @param meta Информация об устройстве, с помощью которого пользователь разговаривает с Алисой.
* @param version Версия протокола.
* @param session Данные о сессии. Сессия — это период относительно непрерывного взаимодействия пользователя с навыком.
* @param request Данные, полученные от пользователя.
* @param state Данные о сохраненном состоянии.
* */
@Serializable
data class MessageRequest(
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Loading

0 comments on commit e33fa1d

Please sign in to comment.