All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jetbrains.kotlinx.jupyter.messaging.MessageFactory.kt Maven / Gradle / Ivy

Go to download

Implementation of REPL compiler and preprocessor for Jupyter dialect of Kotlin (IDE-compatible)

There is a newer version: 0.12.0-333
Show newest version
package org.jetbrains.kotlinx.jupyter.messaging

import kotlinx.serialization.json.JsonElement
import org.jetbrains.kotlinx.jupyter.api.libraries.RawMessage

interface MessageFactory {
    val messageId: List
    val sessionId: String
    val username: String
    val contextMessage: RawMessage?

    fun updateSessionInfo(message: RawMessage)

    fun updateContextMessage(contextMessage: RawMessage?)

    fun makeReplyMessageOrNull(
        msgType: MessageType? = null,
        sessionId: String? = null,
        header: MessageHeader? = null,
        parentHeader: MessageHeader? = null,
        metadata: JsonElement? = null,
        content: MessageContent? = null,
    ): Message?
}

fun MessageFactory.makeDefaultHeader(msgType: MessageType): MessageHeader {
    return makeHeader(msgType, sessionId = sessionId, username = username)
}

fun MessageFactory.makeSimpleMessage(
    msgType: MessageType,
    content: AbstractMessageContent,
): Message {
    return Message(
        id = messageId,
        data =
            MessageData(
                header = makeDefaultHeader(msgType),
                content = content,
            ),
    )
}

fun MessageFactory.makeReplyMessage(
    msgType: MessageType? = null,
    sessionId: String? = null,
    header: MessageHeader? = null,
    parentHeader: MessageHeader? = null,
    metadata: JsonElement? = null,
    content: MessageContent? = null,
): Message {
    return makeReplyMessageOrNull(
        msgType,
        sessionId,
        header,
        parentHeader,
        metadata,
        content,
    ) ?: error("Context message is needed for reply")
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy