From a55917e4d424ed0231355d0f15a027f41f61eb0e Mon Sep 17 00:00:00 2001 From: Guilherme Caponetto <638737+caponetto@users.noreply.github.com> Date: Mon, 18 Mar 2024 13:52:35 -0300 Subject: [PATCH] Fix message handler on ScriptRunner --- packages/script-editor/src/ScriptRunner.ts | 41 +++++++++++++--------- 1 file changed, 25 insertions(+), 16 deletions(-) diff --git a/packages/script-editor/src/ScriptRunner.ts b/packages/script-editor/src/ScriptRunner.ts index ca868ae24..2072fd573 100644 --- a/packages/script-editor/src/ScriptRunner.ts +++ b/packages/script-editor/src/ScriptRunner.ts @@ -22,6 +22,14 @@ import { Session, SessionManager } from '@jupyterlab/services'; +import { + IDisplayDataMsg, + IErrorMsg, + IExecuteResultMsg, + IIOPubMessage, + IStatusMsg, + IStreamMsg +} from '@jupyterlab/services/lib/kernel/messages'; const KERNEL_ERROR_MSG = 'Could not run script because no supporting kernel is defined.'; @@ -93,28 +101,29 @@ export class ScriptRunner { const future = this.sessionConnection.kernel.requestExecute({ code }); - future.onIOPub = (msg: any): void => { + future.onIOPub = (msg: IIOPubMessage): void => { + const msgType = msg.header.msg_type; const msgOutput: any = {}; - if (msg.msg_type === 'error') { + if (msgType === 'error') { + const errorMsg = msg as IErrorMsg; msgOutput.error = { - type: msg.content.ename, - output: msg.content.evalue + type: errorMsg.content.ename, + output: errorMsg.content.evalue }; - } else if ( - msg.msg_type === 'execute_result' || - msg.msg_type === 'display_data' - ) { - if ('text/plain' in msg.content.data) { - msgOutput.output = msg.content.data['text/plain']; + } else if (msgType === 'execute_result' || msgType === 'display_data') { + const resultMsg = msg as IExecuteResultMsg | IDisplayDataMsg; + if ('text/plain' in resultMsg.content.data) { + msgOutput.output = resultMsg.content.data['text/plain']; } else { - // ignore - console.log('Ignoring received message ' + msg); + console.log('Ignoring received message ' + JSON.stringify(msg)); } - } else if (msg.msg_type === 'stream') { - msgOutput.output = msg.content.text; - } else if (msg.msg_type === 'status') { - msgOutput.status = msg.content.execution_state; + } else if (msgType === 'stream') { + const streamMsg = msg as IStreamMsg; + msgOutput.output = streamMsg.content.text; + } else if (msgType === 'status') { + const statusMsg = msg as IStatusMsg; + msgOutput.status = statusMsg.content.execution_state; } else { // ignore other message types }