@@ -25,6 +25,8 @@ import ai.tock.bot.api.model.message.bot.Card
2525import ai.tock.bot.api.model.message.bot.Carousel
2626import ai.tock.bot.api.model.message.bot.CustomMessage
2727import ai.tock.bot.api.model.message.bot.Debug
28+ import ai.tock.bot.api.model.message.bot.Event
29+ import ai.tock.bot.api.model.message.bot.EventCategory
2830import ai.tock.bot.api.model.message.bot.I18nText
2931import ai.tock.bot.api.model.message.bot.Sentence
3032import ai.tock.bot.api.model.message.bot.Suggestion
@@ -41,8 +43,8 @@ import ai.tock.translator.I18nLabelValue
4143import ai.tock.translator.TranslatedSequence
4244import ai.tock.translator.UserInterfaceType
4345import ai.tock.translator.raw
46+ import kotlinx.coroutines.runBlocking
4447import java.util.Locale
45- import java.util.concurrent.CopyOnWriteArrayList
4648
4749class TockClientBus (
4850 override val botDefinition : ClientBotDefinition ,
@@ -68,7 +70,7 @@ class TockClientBus(
6870 // Target connector : is the connector for which the message is produced
6971 override val targetConnectorType: ConnectorType = request.context.targetConnectorType
7072
71- override val contextId: String? = request.context.userId.id
73+ override val contextId: String = request.context.userId.id
7274 private var _currentAnswerIndex : Int = 0
7375 override val currentAnswerIndex: Int get() = _currentAnswerIndex
7476 override val entities: MutableList <Entity > = request.entities.toMutableList()
@@ -81,6 +83,12 @@ class TockClientBus(
8183
8284 override val stepName: String? = null
8385
86+ @Volatile
87+ private var streaming: Boolean = false
88+
89+ @Volatile
90+ private var streamedData: String = " "
91+
8492 override fun handle () {
8593 story =
8694 if (request.storyId == botDefinition.unknownStory.storyId) {
@@ -91,11 +99,25 @@ class TockClientBus(
9199 ? : botDefinition.unknownStory
92100 }
93101 step = story.steps.find { it.name == request.step }
94- story.handler.handle(this )
102+ runBlocking {
103+ story.handler.handle(this @TockClientBus)
104+ }
95105 }
96106
97107 override fun defaultDelay (answerIndex : Int ): Long = 0
98108
109+ override suspend fun enableStreaming () {
110+ streaming = true
111+ streamedData = " "
112+ addMessage(Event (EventCategory .METADATA , " TOCK_STREAM_RESPONSE" , " true" ))
113+ }
114+
115+ override suspend fun disableStreaming () {
116+ streaming = false
117+ streamedData = " "
118+ addMessage(Event (EventCategory .METADATA , " TOCK_STREAM_RESPONSE" , " false" ))
119+ }
120+
99121 private fun addMessage (message : BotMessage ? , lastResponse : Boolean = false) {
100122 if (message != null ) {
101123 answer(message, lastResponse)
@@ -112,17 +134,33 @@ class TockClientBus(
112134 answer(CustomMessage (ConstrainedValueWrapper (it), delay), lastResponse && plainText == null )
113135 }
114136 if (plainText != null ) {
137+ val text : CharSequence = if (streaming) {
138+ streamedData + = plainText
139+ streamedData
140+ } else {
141+ plainText
142+ }
143+
115144 answer(
116- when (plainText) {
117- is String -> Sentence (I18nText (plainText), delay = delay, suggestions = suggestions)
145+ when (text) {
146+ is String -> Sentence (
147+ I18nText (text = text, toBeTranslated = ! streaming),
148+ delay = delay,
149+ suggestions = suggestions
150+ )
151+
152+ is I18nText -> Sentence (text, delay = delay, suggestions = suggestions)
118153 is TranslatedSequence -> Sentence (
119- I18nText (plainText .toString(), toBeTranslated = false ),
154+ I18nText (text .toString(), toBeTranslated = false ),
120155 delay = delay,
121156 suggestions = suggestions
122157 )
123158
124- is I18nText -> Sentence (plainText, delay = delay, suggestions = suggestions)
125- else -> Sentence (I18nText (plainText.toString()), delay = delay, suggestions = suggestions)
159+ else -> Sentence (
160+ I18nText (text.toString(), toBeTranslated = ! streaming),
161+ delay = delay,
162+ suggestions = suggestions
163+ )
126164 },
127165 lastResponse
128166 )
@@ -154,7 +192,7 @@ class TockClientBus(
154192 return this
155193 }
156194
157- override fun send (
195+ override suspend fun send (
158196 i18nText : CharSequence ,
159197 suggestions : List <Suggestion >,
160198 delay : Long ,
@@ -164,7 +202,7 @@ class TockClientBus(
164202 return this
165203 }
166204
167- override fun end (
205+ override suspend fun end (
168206 i18nText : CharSequence ,
169207 suggestions : List <Suggestion >,
170208 delay : Long ,
@@ -174,12 +212,12 @@ class TockClientBus(
174212 return this
175213 }
176214
177- override fun end (card : Card ): ClientBus {
215+ override suspend fun end (card : Card ): ClientBus {
178216 addMessage(card, lastResponse = true )
179217 return this
180218 }
181219
182- override fun end (carousel : Carousel ): ClientBus {
220+ override suspend fun end (carousel : Carousel ): ClientBus {
183221 addMessage(carousel, lastResponse = true )
184222 return this
185223 }
@@ -202,12 +240,12 @@ class TockClientBus(
202240 )
203241 }
204242
205- override fun send (carousel : Carousel ): ClientBus {
243+ override suspend fun send (carousel : Carousel ): ClientBus {
206244 addMessage(carousel)
207245 return this
208246 }
209247
210- override fun send (card : Card ): ClientBus {
248+ override suspend fun send (card : Card ): ClientBus {
211249 addMessage(card)
212250 return this
213251 }
0 commit comments