From 4dfb70c4deff57531668450e72c97b6c44a6a03d Mon Sep 17 00:00:00 2001 From: Ilya Muradyan Date: Fri, 18 Oct 2024 07:07:34 +0200 Subject: [PATCH] KTNB-794: Fix initialization --- .../kotlinx/jupyter/api/CodeEvaluator.kt | 4 +- jupyter-lib/spring-starter/build.gradle.kts | 2 +- .../starter/SpringProcessKernelRunMode.kt | 2 +- .../org/jetbrains/kotlinx/jupyter/Ikotlin.kt | 38 ++++++++++++++++++- .../jupyter/messaging/MessageHandlerImpl.kt | 2 +- .../jupyter/repl/impl/ReplForJupyterImpl.kt | 10 ----- 6 files changed, 41 insertions(+), 17 deletions(-) diff --git a/jupyter-lib/api/src/main/kotlin/org/jetbrains/kotlinx/jupyter/api/CodeEvaluator.kt b/jupyter-lib/api/src/main/kotlin/org/jetbrains/kotlinx/jupyter/api/CodeEvaluator.kt index 19bbebeb..ec183d3f 100644 --- a/jupyter-lib/api/src/main/kotlin/org/jetbrains/kotlinx/jupyter/api/CodeEvaluator.kt +++ b/jupyter-lib/api/src/main/kotlin/org/jetbrains/kotlinx/jupyter/api/CodeEvaluator.kt @@ -1,8 +1,8 @@ package org.jetbrains.kotlinx.jupyter.api /** - * Evaluates code. Returns rendered result or null in case of the error. + * Evaluates code. Ignores the result */ fun interface CodeEvaluator { - fun eval(code: Code): Any? + fun eval(code: Code) } diff --git a/jupyter-lib/spring-starter/build.gradle.kts b/jupyter-lib/spring-starter/build.gradle.kts index 862e597f..b638ca36 100644 --- a/jupyter-lib/spring-starter/build.gradle.kts +++ b/jupyter-lib/spring-starter/build.gradle.kts @@ -84,7 +84,7 @@ val springKernelJar = ) tasks.processJupyterApiResources { - // libraryProducers = listOf("org.jetbrains.kotlinx.jupyter.spring.starter.SpringJupyterIntegration") + libraryProducers = listOf("org.jetbrains.kotlinx.jupyter.spring.starter.SpringJupyterIntegration") } kotlinPublications { diff --git a/jupyter-lib/spring-starter/src/main/kotlin/org/jetbrains/kotlinx/jupyter/spring/starter/SpringProcessKernelRunMode.kt b/jupyter-lib/spring-starter/src/main/kotlin/org/jetbrains/kotlinx/jupyter/spring/starter/SpringProcessKernelRunMode.kt index b599df42..11786e9c 100644 --- a/jupyter-lib/spring-starter/src/main/kotlin/org/jetbrains/kotlinx/jupyter/spring/starter/SpringProcessKernelRunMode.kt +++ b/jupyter-lib/spring-starter/src/main/kotlin/org/jetbrains/kotlinx/jupyter/spring/starter/SpringProcessKernelRunMode.kt @@ -11,6 +11,6 @@ object SpringProcessKernelRunMode : KernelRunMode by EmbeddedKernelRunMode { evaluator: CodeEvaluator, ) { notebook.sessionOptions.serializeScriptData = true - // evaluator.eval("1") + evaluator.eval("1") } } diff --git a/src/main/kotlin/org/jetbrains/kotlinx/jupyter/Ikotlin.kt b/src/main/kotlin/org/jetbrains/kotlinx/jupyter/Ikotlin.kt index e273aef0..d54c5416 100644 --- a/src/main/kotlin/org/jetbrains/kotlinx/jupyter/Ikotlin.kt +++ b/src/main/kotlin/org/jetbrains/kotlinx/jupyter/Ikotlin.kt @@ -1,5 +1,6 @@ package org.jetbrains.kotlinx.jupyter +import org.jetbrains.kotlinx.jupyter.api.CodeEvaluator import org.jetbrains.kotlinx.jupyter.api.EmbeddedKernelRunMode import org.jetbrains.kotlinx.jupyter.api.KernelLoggerFactory import org.jetbrains.kotlinx.jupyter.api.KernelRunMode @@ -13,17 +14,22 @@ import org.jetbrains.kotlinx.jupyter.execution.JupyterExecutor import org.jetbrains.kotlinx.jupyter.execution.JupyterExecutorImpl import org.jetbrains.kotlinx.jupyter.libraries.DefaultResolutionInfoProviderFactory import org.jetbrains.kotlinx.jupyter.libraries.EmptyResolutionInfoProvider +import org.jetbrains.kotlinx.jupyter.messaging.ExecuteRequest import org.jetbrains.kotlinx.jupyter.messaging.JupyterBaseSockets import org.jetbrains.kotlinx.jupyter.messaging.JupyterCommunicationFacility import org.jetbrains.kotlinx.jupyter.messaging.JupyterCommunicationFacilityImpl import org.jetbrains.kotlinx.jupyter.messaging.JupyterConnectionImpl import org.jetbrains.kotlinx.jupyter.messaging.JupyterConnectionInternal +import org.jetbrains.kotlinx.jupyter.messaging.Message +import org.jetbrains.kotlinx.jupyter.messaging.MessageData import org.jetbrains.kotlinx.jupyter.messaging.MessageFactoryProvider import org.jetbrains.kotlinx.jupyter.messaging.MessageFactoryProviderImpl -import org.jetbrains.kotlinx.jupyter.messaging.MessageHandler import org.jetbrains.kotlinx.jupyter.messaging.MessageHandlerImpl +import org.jetbrains.kotlinx.jupyter.messaging.MessageType import org.jetbrains.kotlinx.jupyter.messaging.comms.CommManagerImpl import org.jetbrains.kotlinx.jupyter.messaging.comms.CommManagerInternal +import org.jetbrains.kotlinx.jupyter.messaging.makeHeader +import org.jetbrains.kotlinx.jupyter.messaging.toRawMessage import org.jetbrains.kotlinx.jupyter.repl.ReplConfig import org.jetbrains.kotlinx.jupyter.repl.ResolutionInfoProviderFactory import org.jetbrains.kotlinx.jupyter.repl.config.DefaultReplSettings @@ -167,7 +173,7 @@ fun startKernel( fun createMessageHandler( replSettings: DefaultReplSettings, socketManager: JupyterBaseSockets, -): MessageHandler { +): MessageHandlerImpl { val loggerFactory = replSettings.loggerFactory val messageFactoryProvider: MessageFactoryProvider = MessageFactoryProviderImpl() val communicationFacility: JupyterCommunicationFacility = JupyterCommunicationFacilityImpl(socketManager, messageFactoryProvider) @@ -192,6 +198,7 @@ fun kernelServer(replSettings: DefaultReplSettings) { val socketManager = conn.socketManager val messageHandler = createMessageHandler(replSettings, socketManager) + initializeKernelSession(messageHandler, replSettings) val mainThread = Thread.currentThread() @@ -250,3 +257,30 @@ fun kernelServer(replSettings: DefaultReplSettings) { logger.info("Shutdown server") } } + +private fun initializeKernelSession( + messageHandler: MessageHandlerImpl, + replSettings: DefaultReplSettings, +) { + val codeEvaluator = + CodeEvaluator { code -> + val messageData = + MessageData( + header = makeHeader(MessageType.EXECUTE_REQUEST), + content = ExecuteRequest(code), + ) + val message = + Message( + data = messageData, + ) + messageHandler.handleMessage( + JupyterSocketType.SHELL, + message.toRawMessage(), + ) + } + + replSettings.replConfig.kernelRunMode.initializeSession( + messageHandler.repl.notebook, + codeEvaluator, + ) +} diff --git a/src/main/kotlin/org/jetbrains/kotlinx/jupyter/messaging/MessageHandlerImpl.kt b/src/main/kotlin/org/jetbrains/kotlinx/jupyter/messaging/MessageHandlerImpl.kt index 36a042c4..5b13bbc8 100644 --- a/src/main/kotlin/org/jetbrains/kotlinx/jupyter/messaging/MessageHandlerImpl.kt +++ b/src/main/kotlin/org/jetbrains/kotlinx/jupyter/messaging/MessageHandlerImpl.kt @@ -10,7 +10,7 @@ import java.io.Closeable class MessageHandlerImpl( private val loggerFactory: KernelLoggerFactory, - private val repl: ReplForJupyter, + val repl: ReplForJupyter, private val commManager: CommManagerInternal, private val messageFactoryProvider: MessageFactoryProvider, private val socketManager: JupyterBaseSockets, diff --git a/src/main/kotlin/org/jetbrains/kotlinx/jupyter/repl/impl/ReplForJupyterImpl.kt b/src/main/kotlin/org/jetbrains/kotlinx/jupyter/repl/impl/ReplForJupyterImpl.kt index 9f179147..754ea7cb 100644 --- a/src/main/kotlin/org/jetbrains/kotlinx/jupyter/repl/impl/ReplForJupyterImpl.kt +++ b/src/main/kotlin/org/jetbrains/kotlinx/jupyter/repl/impl/ReplForJupyterImpl.kt @@ -434,10 +434,6 @@ class ReplForJupyterImpl( private val executor: CellExecutor = CellExecutorImpl(sharedContext) - init { - kernelRunMode.initializeSession(notebook, ::eval) - } - private fun onAnnotationsHandler(context: ScriptConfigurationRefinementContext): ResultWithDiagnostics { return if (evalContextEnabled) { fileAnnotationsProcessor.process(context, hostProvider.host!!) @@ -446,12 +442,6 @@ class ReplForJupyterImpl( } } - private fun eval(code: Code): Any? { - val requestData = EvalRequestData(code) - val result = evalEx(requestData) - return (result as? EvalResultEx.Success)?.renderedValue - } - override fun evalEx(evalData: EvalRequestData): EvalResultEx { return withEvalContext { evalExImpl(evalData)