1616
1717package ai.tock.bot.connector.web
1818
19+ import ai.tock.bot.connector.ConnectorType
20+ import ai.tock.bot.connector.web.send.WebCarousel
21+ import ai.tock.bot.engine.action.SendSentence
1922import com.google.common.cache.CacheBuilder
2023import io.vertx.core.MultiMap
2124import io.vertx.core.http.Cookie
2225import io.vertx.core.http.HttpServerRequest
26+ import mu.KotlinLogging
2327import java.util.concurrent.TimeUnit
2428
2529object WebRequestInfosByEvent {
@@ -40,7 +44,8 @@ object WebRequestInfosByEvent {
4044data class WebRequestInfos (
4145 private val headers : MultiMap = MultiMap .caseInsensitiveMultiMap(),
4246 private val cookies : Set <Cookie > = emptySet(),
43- private val streamedResponse : StringBuilder = StringBuilder (),
47+ @Volatile
48+ private var streamedResponse : SendSentence ? = null ,
4449) {
4550 internal constructor (request: HttpServerRequest ) : this (request.headers(), request.cookies())
4651
@@ -50,14 +55,53 @@ data class WebRequestInfos(
5055
5156 fun firstCookie (name : String ): String? = cookies.firstOrNull { it.name == name }?.value
5257
53- internal fun addStreamedResponse (response : String? ): String {
54- if (response != null ) {
55- streamedResponse.append(response)
56- }
57- return streamedResponse.toString()
58+ internal fun addStreamedResponse (
59+ response : SendSentence ,
60+ connectorType : ConnectorType ,
61+ ): SendSentence {
62+ val s =
63+ streamedResponse?.run {
64+ val originalMessage = message(connectorType) as ? WebMessage
65+ if (originalMessage != null ) {
66+ val newMessage = response.message(connectorType) as ? WebMessage
67+ if (newMessage == null ) {
68+ logger.warn { " no custom message in streamed message - but the previous message is custom - ignore" }
69+ return this
70+ }
71+
72+ changeConnectorMessage(
73+ WebMessage (
74+ text = (originalMessage.text ? : " " ) + (newMessage.text ? : " " ),
75+ buttons = originalMessage.buttons + newMessage.buttons,
76+ card =
77+ originalMessage.card?.let {
78+ it.copy(
79+ title = (it.title?.toString() ? : " " ) + (newMessage.card?.title?.toString() ? : " " ),
80+ subTitle = (it.subTitle?.toString() ? : " " ) + (newMessage.card?.subTitle?.toString() ? : " " ),
81+ file = newMessage.card?.file ? : it.file,
82+ buttons = it.buttons + (newMessage.card?.buttons ? : emptyList()),
83+ )
84+ } ? : newMessage.card,
85+ carousel = ((originalMessage.carousel?.cards ? : emptyList()) + (newMessage.carousel?.cards ? : emptyList())).takeUnless { it.isEmpty() }?.let { WebCarousel (it) },
86+ widget = newMessage.widget ? : originalMessage.widget,
87+ image = newMessage.image ? : originalMessage.image,
88+ version = newMessage.version,
89+ deepLink = newMessage.deepLink ? : originalMessage.deepLink,
90+ footnotes = originalMessage.footnotes + newMessage.footnotes,
91+ ),
92+ )
93+ this
94+ } else {
95+ withText((text?.toString() ? : " " ) + (response.text?.toString() ? : " " ))
96+ }
97+ } ? : response
98+ streamedResponse = s
99+ return s
58100 }
59101
60102 internal fun clearStreamedResponse () {
61- streamedResponse.clear()
103+ streamedResponse = null
62104 }
63105}
106+
107+ private val logger = KotlinLogging .logger {}
0 commit comments