1616
1717package io.livekit.android.room.types
1818
19- import android.annotation.SuppressLint
2019import androidx.annotation.Keep
21- import com.beust.klaxon.Converter
22- import com.beust.klaxon.Json
23- import com.beust.klaxon.JsonValue
24- import com.beust.klaxon.Klaxon
2520import io.livekit.android.util.LKLog
21+ import kotlinx.serialization.KSerializer
22+ import kotlinx.serialization.SerialName
2623import kotlinx.serialization.Serializable
27-
28- private fun <T > Klaxon.convert (k : kotlin.reflect.KClass <* >, fromJson : (JsonValue ) -> T , toJson : (T ) -> String , isUnion : Boolean = false) =
29- this .converter(
30- object : Converter {
31- @Suppress(" UNCHECKED_CAST" )
32- override fun toJson (value : Any ) = toJson(value as T )
33- override fun fromJson (jv : JsonValue ) = fromJson(jv) as Any?
34- override fun canConvert (cls : Class <* >) = cls == k.java || (isUnion && cls.superclass == k.java)
35- },
36- )
37-
38- internal val klaxon = Klaxon ()
39- .convert(AgentInput ::class , { it.string?.let { AgentInput .fromValue(it) } }, { " \" ${it?.value} \" " })
40- .convert(AgentOutput ::class , { it.string?.let { AgentOutput .fromValue(it) } }, { " \" ${it?.value} \" " })
41- .convert(AgentSdkState ::class , { it.string?.let { AgentSdkState .fromValue(it) } }, { " \" ${it?.value} \" " })
24+ import kotlinx.serialization.descriptors.PrimitiveKind
25+ import kotlinx.serialization.descriptors.PrimitiveSerialDescriptor
26+ import kotlinx.serialization.descriptors.SerialDescriptor
27+ import kotlinx.serialization.encoding.Decoder
28+ import kotlinx.serialization.encoding.Encoder
4229
4330@Keep
31+ @Serializable
4432data class AgentAttributes (
45- @Json(name = " lk.agent.inputs" )
33+ @SerialName( " lk.agent.inputs" )
4634 val lkAgentInputs : List <AgentInput >? = null ,
4735
48- @Json(name = " lk.agent.outputs" )
36+ @SerialName( " lk.agent.outputs" )
4937 val lkAgentOutputs : List <AgentOutput >? = null ,
5038
51- @Json(name = " lk.agent.state" )
39+ @SerialName( " lk.agent.state" )
5240 val lkAgentState : AgentSdkState ? = null ,
5341
54- @Json(name = " lk.publish_on_behalf" )
42+ @SerialName( " lk.publish_on_behalf" )
5543 val lkPublishOnBehalf : String? = null ,
56- ) {
57- fun toJson () = klaxon.toJsonString(this )
58-
59- companion object {
60- fun fromJson (json : String ) = klaxon.parse<AgentAttributes >(json)
61- }
62- }
44+ )
6345
6446@Keep
47+ @Serializable(with = AgentInputSerializer ::class )
6548enum class AgentInput (val value : String ) {
49+ @SerialName(" audio" )
6650 Audio (" audio" ),
51+
52+ @SerialName(" text" )
6753 Text (" text" ),
54+
55+ @SerialName(" video" )
6856 Video (" video" ),
69- Unknown (" unknown" ),
70- ;
57+
58+ @SerialName(" unknown" )
59+ Unknown (" unknown" );
7160
7261 companion object {
73- fun fromValue (value : String ): AgentInput ? = when (value) {
62+ fun fromValue (value : String ): AgentInput = when (value) {
7463 " audio" -> Audio
7564 " text" -> Text
7665 " video" -> Video
@@ -83,14 +72,34 @@ enum class AgentInput(val value: String) {
8372}
8473
8574@Keep
75+ internal object AgentInputSerializer : KSerializer<AgentInput> {
76+ // Serial names of descriptors should be unique, this is why we advise including app package in the name.
77+ override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor (" io.livekit.android.room.types.AgentInput" , PrimitiveKind .STRING )
78+
79+ override fun serialize (encoder : Encoder , value : AgentInput ) {
80+ encoder.encodeString(value.value)
81+ }
82+
83+ override fun deserialize (decoder : Decoder ): AgentInput {
84+ val string = decoder.decodeString()
85+ return AgentInput .fromValue(string)
86+ }
87+ }
88+
89+ @Keep
90+ @Serializable(with = AgentOutputSerializer ::class )
8691enum class AgentOutput (val value : String ) {
92+ @SerialName(" audio" )
8793 Audio (" audio" ),
94+
95+ @SerialName(" transcription" )
8896 Transcription (" transcription" ),
89- Unknown (" unknown" ),
90- ;
97+
98+ @SerialName(" unknown" )
99+ Unknown (" unknown" );
91100
92101 companion object {
93- fun fromValue (value : String ): AgentOutput ? = when (value) {
102+ fun fromValue (value : String ): AgentOutput = when (value) {
94103 " audio" -> Audio
95104 " transcription" -> Transcription
96105 else -> {
@@ -101,19 +110,45 @@ enum class AgentOutput(val value: String) {
101110 }
102111}
103112
113+ @Keep
114+ internal object AgentOutputSerializer : KSerializer<AgentOutput> {
115+ // Serial names of descriptors should be unique, this is why we advise including app package in the name.
116+ override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor (" io.livekit.android.room.types.AgentOutput" , PrimitiveKind .STRING )
117+
118+ override fun serialize (encoder : Encoder , value : AgentOutput ) {
119+ encoder.encodeString(value.value)
120+ }
121+
122+ override fun deserialize (decoder : Decoder ): AgentOutput {
123+ val string = decoder.decodeString()
124+ return AgentOutput .fromValue(string)
125+ }
126+ }
127+
104128// Renamed from AgentState to AgentSdkState to avoid naming conflicts elsewhere.
105129@Keep
130+ @Serializable(with = AgentSdkStateSerializer ::class )
106131enum class AgentSdkState (val value : String ) {
132+ @SerialName(" idle" )
107133 Idle (" idle" ),
134+
135+ @SerialName(" initializing" )
108136 Initializing (" initializing" ),
137+
138+ @SerialName(" listening" )
109139 Listening (" listening" ),
140+
141+ @SerialName(" speaking" )
110142 Speaking (" speaking" ),
143+
144+ @SerialName(" thinking" )
111145 Thinking (" thinking" ),
112- Unknown (" unknown" ),
113- ;
146+
147+ @SerialName(" unknown" )
148+ Unknown (" unknown" );
114149
115150 companion object {
116- fun fromValue (value : String ): AgentSdkState ? = when (value) {
151+ fun fromValue (value : String ): AgentSdkState = when (value) {
117152 " idle" -> Idle
118153 " initializing" -> Initializing
119154 " listening" -> Listening
@@ -127,34 +162,42 @@ enum class AgentSdkState(val value: String) {
127162 }
128163}
129164
165+ @Keep
166+ internal object AgentSdkStateSerializer : KSerializer<AgentSdkState> {
167+ // Serial names of descriptors should be unique, this is why we advise including app package in the name.
168+ override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor (" io.livekit.android.room.types.AgentSdkState" , PrimitiveKind .STRING )
169+
170+ override fun serialize (encoder : Encoder , value : AgentSdkState ) {
171+ encoder.encodeString(value.value)
172+ }
173+
174+ override fun deserialize (decoder : Decoder ): AgentSdkState {
175+ val string = decoder.decodeString()
176+ return AgentSdkState .fromValue(string)
177+ }
178+ }
179+
130180/* *
131181 * Schema for transcription-related attributes
132182 */
133183@Keep
134- @SuppressLint(" UnsafeOptInUsageError" )
135184@Serializable
136185data class TranscriptionAttributes (
137186 /* *
138187 * The segment id of the transcription
139188 */
140- @Json(name = " lk.segment_id" )
189+ @SerialName( " lk.segment_id" )
141190 val lkSegmentID : String? = null ,
142191
143192 /* *
144193 * The associated track id of the transcription
145194 */
146- @Json(name = " lk.transcribed_track_id" )
195+ @SerialName( " lk.transcribed_track_id" )
147196 val lkTranscribedTrackID : String? = null ,
148197
149198 /* *
150199 * Whether the transcription is final
151200 */
152- @Json(name = " lk.transcription_final" )
201+ @SerialName( " lk.transcription_final" )
153202 val lkTranscriptionFinal : Boolean? = null ,
154- ) {
155- fun toJson () = klaxon.toJsonString(this )
156-
157- companion object {
158- fun fromJson (json : String ) = klaxon.parse<TranscriptionAttributes >(json)
159- }
160- }
203+ )
0 commit comments