Skip to content

Commit

Permalink
Make kernel message typing follow the spec more closely.
Browse files Browse the repository at this point in the history
All kernel message replies have a status field. This ensures that reply messages reflect this and handle the abort and error status appropriately.

We also simplify typings since we can refer to most content typings using the field accessor.
  • Loading branch information
jasongrout committed Jun 7, 2019
1 parent 1d655d5 commit 726ce5e
Show file tree
Hide file tree
Showing 14 changed files with 307 additions and 315 deletions.
2 changes: 1 addition & 1 deletion packages/completer/src/kernelconnector.ts
Expand Up @@ -41,7 +41,7 @@ export class KernelConnector extends DataConnector<
return Promise.reject(new Error('No kernel for completion request.'));
}

const contents: KernelMessage.ICompleteRequest = {
const contents: KernelMessage.ICompleteRequestMsg['content'] = {
code: request.text,
cursor_pos: request.offset
};
Expand Down
12 changes: 7 additions & 5 deletions packages/console/src/history.ts
Expand Up @@ -224,10 +224,12 @@ export class ConsoleHistory implements IConsoleHistory {
this._history.length = 0;
let last = '';
let current = '';
for (let i = 0; i < value.content.history.length; i++) {
current = (value.content.history[i] as string[])[2];
if (current !== last) {
this._history.push((last = current));
if (value.content.status === 'ok') {
for (let i = 0; i < value.content.history.length; i++) {
current = (value.content.history[i] as string[])[2];
if (current !== last) {
this._history.push((last = current));
}
}
}
// Reset the history navigation cursor back to the bottom.
Expand Down Expand Up @@ -360,7 +362,7 @@ export namespace ConsoleHistory {
* A namespace for private data.
*/
namespace Private {
export const initialRequest: KernelMessage.IHistoryRequest = {
export const initialRequest: KernelMessage.IHistoryRequestMsg['content'] = {
output: false,
raw: true,
hist_access_type: 'tail',
Expand Down
8 changes: 6 additions & 2 deletions packages/console/src/widget.ts
Expand Up @@ -646,7 +646,7 @@ export class CodeConsole extends Widget {
return;
}
if (value && value.content.status === 'ok') {
let content = value.content as KernelMessage.IExecuteOkReply;
let content = value.content;
// Use deprecated payloads for backwards compatibility.
if (content.payload && content.payload.length) {
let setNextInput = content.payload.filter(i => {
Expand Down Expand Up @@ -682,7 +682,11 @@ export class CodeConsole extends Widget {
/**
* Update the console based on the kernel info.
*/
private _handleInfo(info: KernelMessage.IInfoReply): void {
private _handleInfo(info: KernelMessage.IInfoReplyMsg['content']): void {
if (info.status !== 'ok') {
this._banner.model.value.text = 'Error in getting kernel banner';
return;
}
this._banner.model.value.text = info.banner;
let lang = info.language_info as nbformat.ILanguageInfoMetadata;
this._mimetype = this._mimeTypeService.getMimeTypeByLanguage(lang);
Expand Down
5 changes: 4 additions & 1 deletion packages/help-extension/src/index.tsx
Expand Up @@ -164,7 +164,10 @@ function activate(
helpMenu.addGroup(resourcesGroup, 10);

// Generate a cache of the kernel help links.
const kernelInfoCache = new Map<string, KernelMessage.IInfoReply>();
const kernelInfoCache = new Map<
string,
KernelMessage.IInfoReplyMsg['content']
>();
serviceManager.sessions.runningChanged.connect((m, sessions) => {
// If a new session has been added, it is at the back
// of the session list. If one has changed or stopped,
Expand Down
2 changes: 1 addition & 1 deletion packages/inspector/src/kernelconnector.ts
Expand Up @@ -41,7 +41,7 @@ export class KernelConnector extends DataConnector<
return Promise.reject(new Error('Inspection fetch requires a kernel.'));
}

const contents: KernelMessage.IInspectRequest = {
const contents: KernelMessage.IInspectRequestMsg['content'] = {
code: request.text,
cursor_pos: request.offset,
detail_level: 0
Expand Down
4 changes: 2 additions & 2 deletions packages/notebook/src/actions.tsx
Expand Up @@ -1488,7 +1488,7 @@ namespace Private {
}

if (reply.content.status === 'ok') {
const content = reply.content as KernelMessage.IExecuteOkReply;
const content = reply.content;

if (content.payload && content.payload.length) {
handlePayload(content, notebook, cell);
Expand Down Expand Up @@ -1532,7 +1532,7 @@ namespace Private {
* See [Payloads (DEPRECATED)](https://jupyter-client.readthedocs.io/en/latest/messaging.html#payloads-deprecated).
*/
function handlePayload(
content: KernelMessage.IExecuteOkReply,
content: KernelMessage.IExecuteReply,
notebook: Notebook,
cell: Cell
) {
Expand Down
8 changes: 6 additions & 2 deletions packages/outputarea/src/widget.ts
Expand Up @@ -468,7 +468,10 @@ export class OutputArea extends Widget {
// is overridden from 'execute_reply' to 'display_data' in order to
// render output.
let model = this.model;
let content = msg.content as KernelMessage.IExecuteOkReply;
let content = msg.content;
if (content.status !== 'ok') {
return;
}
let payload = content && content.payload;
if (!payload || !payload.length) {
return;
Expand Down Expand Up @@ -557,7 +560,7 @@ export namespace OutputArea {
) {
stopOnError = false;
}
let content: KernelMessage.IExecuteRequest = {
let content: KernelMessage.IExecuteRequestMsg['content'] = {
code,
stop_on_error: stopOnError
};
Expand Down Expand Up @@ -712,6 +715,7 @@ export class Stdin extends Widget implements IStdin {
if ((event as KeyboardEvent).keyCode === 13) {
// Enter
this._future.sendInputReply({
status: 'ok',
value: input.value
});
if (input.type === 'password') {
Expand Down
17 changes: 10 additions & 7 deletions packages/services/src/kernel/default.ts
Expand Up @@ -415,6 +415,9 @@ export class DefaultKernel implements Kernel.IKernel {
if (this.isDisposed) {
throw new Error('Disposed kernel');
}
if (reply.content.status !== 'ok') {
throw new Error('Kernel info reply errored');
}
this._info = reply.content;
return reply;
}
Expand All @@ -429,7 +432,7 @@ export class DefaultKernel implements Kernel.IKernel {
* received and validated.
*/
requestComplete(
content: KernelMessage.ICompleteRequest
content: KernelMessage.ICompleteRequestMsg['content']
): Promise<KernelMessage.ICompleteReplyMsg> {
let msg = KernelMessage.createMessage({
msgType: 'complete_request',
Expand All @@ -453,7 +456,7 @@ export class DefaultKernel implements Kernel.IKernel {
* received and validated.
*/
requestInspect(
content: KernelMessage.IInspectRequest
content: KernelMessage.IInspectRequestMsg['content']
): Promise<KernelMessage.IInspectReplyMsg> {
let msg = KernelMessage.createMessage({
msgType: 'inspect_request',
Expand All @@ -477,7 +480,7 @@ export class DefaultKernel implements Kernel.IKernel {
* received and validated.
*/
requestHistory(
content: KernelMessage.IHistoryRequest
content: KernelMessage.IHistoryRequestMsg['content']
): Promise<KernelMessage.IHistoryReplyMsg> {
let msg = KernelMessage.createMessage({
msgType: 'history_request',
Expand Down Expand Up @@ -507,7 +510,7 @@ export class DefaultKernel implements Kernel.IKernel {
* **See also:** [[IExecuteReply]]
*/
requestExecute(
content: KernelMessage.IExecuteRequest,
content: KernelMessage.IExecuteRequestMsg['content'],
disposeOnDone: boolean = true,
metadata?: JSONObject
): Kernel.IFuture<
Expand Down Expand Up @@ -544,7 +547,7 @@ export class DefaultKernel implements Kernel.IKernel {
* received and validated.
*/
requestIsComplete(
content: KernelMessage.IIsCompleteRequest
content: KernelMessage.IIsCompleteRequestMsg['content']
): Promise<KernelMessage.IIsCompleteReplyMsg> {
let msg = KernelMessage.createMessage({
msgType: 'is_complete_request',
Expand All @@ -566,7 +569,7 @@ export class DefaultKernel implements Kernel.IKernel {
* received and validated.
*/
requestCommInfo(
content: KernelMessage.ICommInfoRequest
content: KernelMessage.ICommInfoRequestMsg['content']
): Promise<KernelMessage.ICommInfoReplyMsg> {
let msg = KernelMessage.createMessage({
msgType: 'comm_info_request',
Expand All @@ -586,7 +589,7 @@ export class DefaultKernel implements Kernel.IKernel {
* #### Notes
* See [Messaging in Jupyter](https://jupyter-client.readthedocs.io/en/latest/messaging.html#messages-on-the-stdin-router-dealer-sockets).
*/
sendInputReply(content: KernelMessage.IInputReply): void {
sendInputReply(content: KernelMessage.IInputReplyMsg['content']): void {
if (this.status === 'dead') {
throw new Error('Kernel is dead');
}
Expand Down
2 changes: 1 addition & 1 deletion packages/services/src/kernel/future.ts
Expand Up @@ -154,7 +154,7 @@ export class KernelFutureHandler<
/**
* Send an `input_reply` message.
*/
sendInputReply(content: KernelMessage.IInputReply): void {
sendInputReply(content: KernelMessage.IInputReplyMsg['content']): void {
this._kernel.sendInputReply(content);
}

Expand Down
16 changes: 8 additions & 8 deletions packages/services/src/kernel/kernel.ts
Expand Up @@ -206,7 +206,7 @@ export namespace Kernel {
* received and validated.
*/
requestComplete(
content: KernelMessage.ICompleteRequest
content: KernelMessage.ICompleteRequestMsg['content']
): Promise<KernelMessage.ICompleteReplyMsg>;

/**
Expand All @@ -223,7 +223,7 @@ export namespace Kernel {
* received and validated.
*/
requestInspect(
content: KernelMessage.IInspectRequest
content: KernelMessage.IInspectRequestMsg['content']
): Promise<KernelMessage.IInspectReplyMsg>;

/**
Expand All @@ -240,7 +240,7 @@ export namespace Kernel {
* received and validated.
*/
requestHistory(
content: KernelMessage.IHistoryRequest
content: KernelMessage.IHistoryRequestMsg['content']
): Promise<KernelMessage.IHistoryReplyMsg>;

/**
Expand All @@ -265,7 +265,7 @@ export namespace Kernel {
* **See also:** [[IExecuteReply]]
*/
requestExecute(
content: KernelMessage.IExecuteRequest,
content: KernelMessage.IExecuteRequestMsg['content'],
disposeOnDone?: boolean,
metadata?: JSONObject
): Kernel.IFuture<
Expand All @@ -287,7 +287,7 @@ export namespace Kernel {
* received and validated.
*/
requestIsComplete(
content: KernelMessage.IIsCompleteRequest
content: KernelMessage.IIsCompleteRequestMsg['content']
): Promise<KernelMessage.IIsCompleteReplyMsg>;

/**
Expand All @@ -304,7 +304,7 @@ export namespace Kernel {
* received and validated.
*/
requestCommInfo(
content: KernelMessage.ICommInfoRequest
content: KernelMessage.ICommInfoRequestMsg['content']
): Promise<KernelMessage.ICommInfoReplyMsg>;

/**
Expand All @@ -315,7 +315,7 @@ export namespace Kernel {
* #### Notes
* See [Messaging in Jupyter](https://jupyter-client.readthedocs.io/en/latest/messaging.html#messages-on-the-stdin-router-dealer-sockets).
*/
sendInputReply(content: KernelMessage.IInputReply): void;
sendInputReply(content: KernelMessage.IInputReplyMsg['content']): void;

/**
* Connect to a comm, or create a new one.
Expand Down Expand Up @@ -830,7 +830,7 @@ export namespace Kernel {
/**
* Send an `input_reply` message.
*/
sendInputReply(content: KernelMessage.IInputReply): void;
sendInputReply(content: KernelMessage.IInputReplyMsg['content']): void;
}

/**
Expand Down

0 comments on commit 726ce5e

Please sign in to comment.