Skip to content

Commit

Permalink
fixes theopenconversationkit#1829 bot_engine: rename applicationId to…
Browse files Browse the repository at this point in the history
… connectorId
  • Loading branch information
Fabilin committed Jan 22, 2025
1 parent 963c8b6 commit 52ed41d
Show file tree
Hide file tree
Showing 28 changed files with 303 additions and 125 deletions.
4 changes: 2 additions & 2 deletions bot/api/client/src/main/kotlin/TockClientBus.kt
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class TockClientBus(
constructor(botDefinition: ClientBotDefinition, data: RequestData, sendAnswer: (BotResponse) -> Unit) :
this(botDefinition, data.requestId, data.botRequest!!, sendAnswer)

override val applicationId: String = request.context.applicationId
override val connectorId: String = request.context.applicationId
override val userId: PlayerId = request.context.userId
override val botId: PlayerId = request.context.botId
override val intent: IntentAware? = request.intent?.let { Intent(it) }
Expand Down Expand Up @@ -191,7 +191,7 @@ class TockClientBus(
}

override fun withMessage(connectorType: ConnectorType, connectorId: String, messageProvider: () -> ConnectorMessage): ClientBus {
if (applicationId == connectorId && targetConnectorType == connectorType) {
if (this.connectorId == connectorId && targetConnectorType == connectorType) {
context.connectorMessages[connectorType] = messageProvider()
}
return this
Expand Down
16 changes: 8 additions & 8 deletions bot/api/service/src/main/kotlin/BotApiHandler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ internal class BotApiHandler(
botDefinition.botId,
story.definition.id
)
val endingStoryId = storySetting?.findEnabledEndWithStoryId(applicationId)
val endingStoryId = storySetting?.findEnabledEndWithStoryId(connectorId)

val messages = response.messages
if (messages.isEmpty()) {
Expand Down Expand Up @@ -143,7 +143,7 @@ internal class BotApiHandler(
userTimelineDAO.save(userTimeline, botDefinition, asynchronousProcess = false)
}

val targetStory = botDefinition.findStoryDefinitionById(endingStoryId, applicationId)
val targetStory = botDefinition.findStoryDefinitionById(endingStoryId, connectorId)
switchEndingStory(targetStory)
}
}
Expand Down Expand Up @@ -185,7 +185,7 @@ internal class BotApiHandler(
private fun BotBus.toAction(message: CustomMessage): Action {
return SendSentence(
botId,
applicationId,
connectorId,
userId,
null,
listOfNotNull(message.message.value).toMutableList()
Expand All @@ -201,7 +201,7 @@ internal class BotApiHandler(
if (message != null) {
return SendSentence(
botId,
applicationId,
connectorId,
userId,
null,
mutableListOf(message)
Expand All @@ -210,7 +210,7 @@ internal class BotApiHandler(
}
return SendSentence(
botId,
applicationId,
connectorId,
userId,
text
)
Expand All @@ -219,7 +219,7 @@ internal class BotApiHandler(
private fun BotBus.toAction(data: Debug): Action {
return SendDebug(
botId,
applicationId,
connectorId,
userId,
data.text,
data.data
Expand All @@ -237,7 +237,7 @@ internal class BotApiHandler(
return connectorMessages?.map {
SendSentence(
botId,
applicationId,
connectorId,
userId,
null,
mutableListOf(it)
Expand All @@ -256,7 +256,7 @@ internal class BotApiHandler(
return connectorMessages?.map {
SendSentence(
botId,
applicationId,
connectorId,
userId,
null,
mutableListOf(it)
Expand Down
2 changes: 1 addition & 1 deletion bot/api/service/src/main/kotlin/Transformers.kt
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ private fun BotBus.toRequestContext(): RequestContext =
sourceConnectorType,
targetConnectorType,
userInterfaceType,
applicationId,
connectorId,
userId,
botId,
userPreferences.toUserData(),
Expand Down
32 changes: 17 additions & 15 deletions bot/connector-web/src/main/kotlin/WebConnector.kt
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,11 @@ val webConnectorUseExtraHeadersAsMetadata: Boolean =
booleanProperty("tock_web_connector_use_extra_header_as_metadata_request", false)

class WebConnector internal constructor(
val applicationId: String,
val connectorId: String,
val path: String
) : ConnectorBase(webConnectorType, setOf(CAROUSEL)), OrchestrationConnector {
@Deprecated("Use the more aptly named connectorId field", ReplaceWith("connectorId"))
val applicationId: String get() = connectorId

companion object {
private val logger = KotlinLogging.logger {}
Expand Down Expand Up @@ -186,7 +188,7 @@ class WebConnector internal constructor(
val timerId = vertx.setPeriodic(Duration.ofSeconds(sseKeepaliveDelay).toMillis()) {
response.sendSsePing()
}
val channelId = channels.register(applicationId, userId) { webConnectorResponse ->
val channelId = channels.register(connectorId, userId) { webConnectorResponse ->
response.sendSseResponse(webConnectorResponse)
}
response.closeHandler {
Expand Down Expand Up @@ -288,11 +290,11 @@ class WebConnector internal constructor(
val applicationId =
if (request.connectorId?.isNotBlank() == true)
if (webConnectorBridgeEnabled)
request.connectorId.also { logger.debug { "Web bridge: $applicationId -> $it" } }
request.connectorId.also { logger.debug { "Web bridge: $connectorId -> $it" } }
else
applicationId.also { logger.warn { "Web bridge disabled." } }
connectorId.also { logger.warn { "Web bridge disabled." } }
else
applicationId
connectorId

val event = request.toEvent(applicationId)
val requestInfos = WebRequestInfos(context.request())
Expand Down Expand Up @@ -349,12 +351,12 @@ class WebConnector internal constructor(
throw UnsupportedOperationException("Web Connector only supports notifications when SSE is enabled")
}
handleEvent(
applicationId = applicationId,
applicationId = connectorId,
locale = defaultLocale,
event = SendChoice(
recipientId,
applicationId,
PlayerId(applicationId, bot),
connectorId,
PlayerId(connectorId, bot),
intent.wrappedIntent().name,
step,
parameters
Expand Down Expand Up @@ -392,14 +394,14 @@ class WebConnector internal constructor(
val request: ResumeOrchestrationRequest = mapper.readValue(context.body().asString())
val callback = RestOrchestrationCallback(
webConnectorType,
applicationId = applicationId,
applicationId = connectorId,
context = context,
orchestrationMapper = webMapper
)

controller.handle(request.toAction(), ConnectorData(callback))
} catch (t: Throwable) {
RestOrchestrationCallback(webConnectorType, applicationId, context = context).sendError()
RestOrchestrationCallback(webConnectorType, connectorId, context = context).sendError()
BotRepository.requestTimer.throwable(t, timerData)
} finally {
BotRepository.requestTimer.end(timerData)
Expand All @@ -416,23 +418,23 @@ class WebConnector internal constructor(
val request: AskEligibilityToOrchestratedBotRequest = mapper.readValue(context.body().asString())
val callback = RestOrchestrationCallback(
webConnectorType,
applicationId,
connectorId,
context = context,
orchestrationMapper = webMapper
)

val support = controller.support(request.toAction(applicationId), ConnectorData(callback))
val support = controller.support(request.toAction(connectorId), ConnectorData(callback))
val sendEligibility = SecondaryBotEligibilityResponse(
support,
OrchestrationMetaData(
playerId = PlayerId(applicationId, bot),
applicationId = applicationId,
playerId = PlayerId(connectorId, bot),
applicationId = connectorId,
recipientId = request.metadata?.playerId ?: PlayerId(Dice.newId(), user)
)
)
callback.sendResponse(sendEligibility)
} catch (t: Throwable) {
RestOrchestrationCallback(webConnectorType, applicationId, context = context).sendError()
RestOrchestrationCallback(webConnectorType, connectorId, context = context).sendError()
BotRepository.requestTimer.throwable(t, timerData)
} finally {
BotRepository.requestTimer.end(timerData)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,5 +137,5 @@ data class BotApplicationConfigurationKey(
botDefinition.namespace
)

constructor(bus: BotBus) : this(bus.applicationId, bus.botDefinition)
constructor(bus: BotBus) : this(bus.connectorId, bus.botDefinition)
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ interface MessageConfiguration {

fun toAction(bus: BotBus): Action = toAction(
bus.userId,
bus.applicationId,
bus.connectorId,
bus.botId,
bus.userLocale,
bus.userInterfaceType
Expand Down
2 changes: 1 addition & 1 deletion bot/engine/src/main/kotlin/definition/StoryHandlerBase.kt
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ abstract class StoryHandlerBase<out T : StoryHandlerDefinition>(
* Finds the story definition of this handler.
*/
open fun findStoryDefinition(bus: BotBus): StoryDefinition? =
bus.botDefinition.findStoryByStoryHandler(this, bus.applicationId)
bus.botDefinition.findStoryByStoryHandler(this, bus.connectorId)

/**
* Handles the action and switches the context to the underlying story definition.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ import ai.tock.bot.connector.ConnectorType
import ai.tock.bot.engine.BotBus
import ai.tock.shared.injector
import ai.tock.shared.provide
import mu.KotlinLogging
import kotlin.LazyThreadSafetyMode.PUBLICATION
import kotlin.reflect.KClass
import kotlin.reflect.full.isSubtypeOf
import kotlin.reflect.full.primaryConstructor
import kotlin.reflect.full.starProjectedType
import mu.KotlinLogging

/**
* Base implementation of [StoryHandlerDefinition].
Expand Down Expand Up @@ -95,8 +95,8 @@ abstract class StoryHandlerDefinitionBase<T : ConnectorStoryHandlerBase<*>>(val
connectorProvider.provide(this, connectorId) as? T?

private val cachedConnector: T? by lazy(PUBLICATION) {
(findConnector(applicationId) ?: findConnector(connectorType))
.also { if (it == null) logger.warn { "unsupported connector type $applicationId or $connectorType for ${this::class}" } }
(findConnector(connectorId) ?: findConnector(connectorType))
.also { if (it == null) logger.warn { "unsupported connector type $connectorId or $connectorType for ${this::class}" } }
}

/**
Expand Down
11 changes: 4 additions & 7 deletions bot/engine/src/main/kotlin/engine/BotBus.kt
Original file line number Diff line number Diff line change
Expand Up @@ -75,16 +75,13 @@ interface BotBus : Bus<BotBus> {
}

/**
* The current TOCK application id.
* The connector ID.
*
* This identifier matches the identifier of the [underlyingConnector], as defined in TOCK Studio.
*
* *This identifier is not to be confused with the chat platform's application id (this is not a Messenger/Whatsapp
* application ID).*
*
* @see ConnectorConfiguration.connectorId
*/
override val applicationId: String
override val connectorId: String

/**
* The bot definition of the current bot.
Expand Down Expand Up @@ -373,7 +370,7 @@ interface BotBus : Bus<BotBus> {
* Sends text that should not be translated as last bot answer.
*/
override fun endRawText(plainText: CharSequence?, delay: Long): BotBus {
return end(SendSentence(botId, applicationId, userId, plainText), delay)
return end(SendSentence(botId, connectorId, userId, plainText), delay)
}

/**
Expand Down Expand Up @@ -507,7 +504,7 @@ interface BotBus : Bus<BotBus> {
*/
fun isFeatureEnabled(feature: FeatureType, default: Boolean = false) =
injector.provide<FeatureDAO>()
.isEnabled(botDefinition.botId, botDefinition.namespace, feature, applicationId, default, userId.id)
.isEnabled(botDefinition.botId, botDefinition.namespace, feature, connectorId, default, userId.id)

/**
* Marks the current as not understood in the nlp model.
Expand Down
11 changes: 10 additions & 1 deletion bot/engine/src/main/kotlin/engine/Bus.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,19 @@ import ai.tock.bot.engine.user.PlayerId
*/
interface Bus<T : Bus<T>> : I18nTranslator {

/**
* The connector ID.
*/
val connectorId: String

/**
* The current TOCK application id.
*
* *This identifier is not to be confused with the chat platform's application id (this is not a Messenger/Whatsapp
* application ID).*
*/
val applicationId: String
@Deprecated("Use more appropriately named connectorId", ReplaceWith("connectorId"))
val applicationId: String get() = connectorId

/**
* The current bot id.
Expand Down
8 changes: 4 additions & 4 deletions bot/engine/src/main/kotlin/engine/TockBotBus.kt
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ internal class TockBotBus(
currentDialog.stories.add(value)
}
override val botDefinition: BotDefinition = bot.botDefinition
override val applicationId = action.applicationId
override val connectorId = action.applicationId
override val botId = action.recipientId
override val userId = action.playerId
override val userPreferences: UserPreferences = userTimeline.userPreferences
Expand Down Expand Up @@ -166,11 +166,11 @@ internal class TockBotBus(
}

override fun sendRawText(plainText: CharSequence?, delay: Long): BotBus {
return answer(SendSentence(botId, applicationId, userId, plainText), delay)
return answer(SendSentence(botId, connectorId, userId, plainText), delay)
}

override fun sendDebugData(title: String, data: Any?): BotBus {
return answer(SendDebug(botId, applicationId, userId, title, data), 0)
return answer(SendDebug(botId, connectorId, userId, title, data), 0)
}

override fun send(action: Action, delay: Long): BotBus {
Expand Down Expand Up @@ -200,7 +200,7 @@ internal class TockBotBus(
}

override fun withMessage(connectorType: ConnectorType, connectorId: String, messageProvider: () -> ConnectorMessage): BotBus {
if (applicationId == connectorId && isCompatibleWith(connectorType)) {
if (this.connectorId == connectorId && isCompatibleWith(connectorType)) {
context.addMessage(messageProvider.invoke())
}
return this
Expand Down
18 changes: 14 additions & 4 deletions bot/engine/src/main/kotlin/engine/action/Action.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,31 @@ import ai.tock.shared.jackson.mapper
import com.fasterxml.jackson.module.kotlin.readValue
import java.time.Instant
import org.litote.kmongo.Id
import org.litote.kmongo.newId

/**
* A user (or bot) action.
*
* @param applicationId the TOCK application id (matches the id of the connector)
*/
abstract class Action(
val playerId: PlayerId,
val recipientId: PlayerId,
applicationId: String,
connectorId: String,
id: Id<Action>,
date: Instant,
state: EventState,
val metadata: ActionMetadata = ActionMetadata()
) : Event(applicationId, id, date, state) {
) : Event(connectorId, id, date, state) {
@Deprecated("Use constructor with connectorId", ReplaceWith("Action(playerId = playerId, recipientId = recipientId, connectorId = applicationId, id = id, date = date, state = state, metadata = metadata)"))
constructor(
playerId: PlayerId,
recipientId: PlayerId,
applicationId: String,
id: Id<Action> = newId(),
date: Instant = Instant.now(),
state: EventState = EventState(),
metadata: ActionMetadata = ActionMetadata(),
_deprecatedConstructor: Nothing? = null,
): this(playerId, recipientId, applicationId, id, date, state, metadata)

abstract fun toMessage(): Message

Expand Down
Loading

0 comments on commit 52ed41d

Please sign in to comment.