public async Task <ICommandDeliveryResult> Handle( ICommandDelivery <JupyterRequestContext> delivery) { switch (delivery.Command.Request.Header.MessageType) { case MessageTypeValues.ExecuteRequest: var transient = new Dictionary <string, object> { { "display_id", Guid.NewGuid().ToString() } }; var jObject = (JObject)delivery.Command.Request.Content; var executeRequest = jObject.ToObject <ExecuteRequest>(); var code = executeRequest.Code; var workspace = new Workspace( files: new[] { new File("Program.cs", Scaffold()) }, buffers: new[] { new Buffer(new BufferId("Program.cs", "main"), code), }, workspaceType: "console"); var workspaceRequest = new WorkspaceRequest(workspace); var server = new WorkspaceServerMultiplexer(new PackageRegistry()); var result = await server.Run(workspaceRequest); var messageBuilder = delivery.Command.Builder; var ioPubChannel = delivery.Command.IoPubChannel; var serverChannel = delivery.Command.ServerChannel; if (!executeRequest.Silent) { _executionCount++; var executeInput = messageBuilder.CreateMessage( MessageTypeValues.ExecuteInput, new ExecuteInput { Code = code, ExecutionCount = _executionCount }, delivery.Command.Request.Header); ioPubChannel.Send(executeInput); } // execute result var output = string.Join("\n", result.Output); // executeResult data var executeResultData = new ExecuteResult() { Data = new JObject { { "text/html", output }, { "text/plain", output } }, Transient = transient, ExecutionCount = _executionCount }; var resultSucceeded = result.Succeeded && result.Exception == null; if (resultSucceeded) { // reply ok var executeReplyPayload = new ExecuteReplyOk { ExecutionCount = _executionCount }; // send to server var executeReply = messageBuilder.CreateMessage( MessageTypeValues.ExecuteReply, executeReplyPayload, delivery.Command.Request.Header); executeReply.Identifiers = delivery.Command.Request.Identifiers; serverChannel.Send(executeReply); } else { var errorContent = new Error { EName = string.IsNullOrWhiteSpace(result.Exception) ? "Compile Error" : "Unhandled Exception", EValue = output, Traceback = new List <string>() }; // reply Error var executeReplyPayload = new ExecuteReplyError(errorContent) { ExecutionCount = _executionCount }; // send to server var executeReply = messageBuilder.CreateMessage( MessageTypeValues.ExecuteReply, executeReplyPayload, delivery.Command.Request.Header); executeReply.Identifiers = delivery.Command.Request.Identifiers; serverChannel.Send(executeReply); if (!executeRequest.Silent) { // send on io var error = messageBuilder.CreateMessage( MessageTypeValues.Error, errorContent, delivery.Command.Request.Header); ioPubChannel.Send(error); // send on stderr var stdErr = new StdErrStream { Text = errorContent.EValue }; var stream = messageBuilder.CreateMessage( MessageTypeValues.Stream, stdErr, delivery.Command.Request.Header); ioPubChannel.Send(stream); } } if (!executeRequest.Silent && resultSucceeded) { // send on io var executeResultMessage = messageBuilder.CreateMessage( MessageTypeValues.ExecuteResult, executeResultData, delivery.Command.Request.Header); ioPubChannel.Send(executeResultMessage); } break; } return(delivery.Complete()); }
private async Task HandleExecuteRequest(ICommandDelivery <JupyterRequestContext> delivery) { var ioPubChannel = delivery.Command.IoPubChannel; var serverChannel = delivery.Command.ServerChannel; var transient = new Dictionary <string, object> { { "display_id", Guid.NewGuid().ToString() } }; var executeRequest = delivery.Command.GetRequestContent <ExecuteRequest>(); var code = executeRequest.Code; var workspace = CreateScaffoldWorkspace(code); var workspaceRequest = new WorkspaceRequest(workspace); var result = await _server.Run(workspaceRequest); if (!executeRequest.Silent) { _executionCount++; var executeInput = Message.Create( new ExecuteInput(code: code, executionCount: _executionCount), delivery.Command.Request.Header); ioPubChannel.Send(executeInput); } // execute result var output = string.Join("\n", result.Output); // executeResult data var executeResultData = new ExecuteResult( _executionCount, transient: transient, data: new Dictionary <string, object> { { "text/html", output }, { "text/plain", output } }); var resultSucceeded = result.Succeeded && result.Exception == null; if (resultSucceeded) { // reply ok var executeReplyPayload = new ExecuteReplyOk(executionCount: _executionCount); // send to server var executeReply = Message.CreateResponse( executeReplyPayload, delivery.Command.Request); serverChannel.Send(executeReply); } else { var errorContent = new Error( eName: string.IsNullOrWhiteSpace(result.Exception) ? "Compile Error" : "Unhandled Exception", eValue: output ); // reply Error var executeReplyPayload = new ExecuteReplyError(errorContent, executionCount: _executionCount); // send to server var executeReply = Message.CreateResponse( executeReplyPayload, delivery.Command.Request); serverChannel.Send(executeReply); if (!executeRequest.Silent) { // send on io var error = Message.Create( errorContent, delivery.Command.Request.Header); ioPubChannel.Send(error); // send on stderr var stdErr = new StdErrStream(errorContent.EValue); var stream = Message.Create( stdErr, delivery.Command.Request.Header); ioPubChannel.Send(stream); } } if (!executeRequest.Silent && resultSucceeded) { // send on io var executeResultMessage = Message.Create( executeResultData, delivery.Command.Request.Header); ioPubChannel.Send(executeResultMessage); } }