Skip to content

Commit

Permalink
KTNB-794: Fix initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
ileasile committed Oct 18, 2024
1 parent f178f0b commit 4dfb70c
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -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)
}
2 changes: 1 addition & 1 deletion jupyter-lib/spring-starter/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ object SpringProcessKernelRunMode : KernelRunMode by EmbeddedKernelRunMode {
evaluator: CodeEvaluator,
) {
notebook.sessionOptions.serializeScriptData = true
// evaluator.eval("1")
evaluator.eval("1")
}
}
38 changes: 36 additions & 2 deletions src/main/kotlin/org/jetbrains/kotlinx/jupyter/Ikotlin.kt
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -192,6 +198,7 @@ fun kernelServer(replSettings: DefaultReplSettings) {

val socketManager = conn.socketManager
val messageHandler = createMessageHandler(replSettings, socketManager)
initializeKernelSession(messageHandler, replSettings)

val mainThread = Thread.currentThread()

Expand Down Expand Up @@ -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,
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -434,10 +434,6 @@ class ReplForJupyterImpl(

private val executor: CellExecutor = CellExecutorImpl(sharedContext)

init {
kernelRunMode.initializeSession(notebook, ::eval)
}

private fun onAnnotationsHandler(context: ScriptConfigurationRefinementContext): ResultWithDiagnostics<ScriptCompilationConfiguration> {
return if (evalContextEnabled) {
fileAnnotationsProcessor.process(context, hostProvider.host!!)
Expand All @@ -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)
Expand Down

0 comments on commit 4dfb70c

Please sign in to comment.