UNPKG

19.7 kBTypeScriptView Raw
1import { JSONObject } from '@lumino/coreutils';
2import { ISignal } from '@lumino/signaling';
3import { ServerConnection } from '..';
4import * as Kernel from './kernel';
5import * as KernelMessage from './messages';
6import { KernelSpec } from '../kernelspec';
7/**
8 * Implementation of the Kernel object.
9 *
10 * #### Notes
11 * Messages from the server are handled in the order they were received and
12 * asynchronously. Any message handler can return a promise, and message
13 * handling will pause until the promise is fulfilled.
14 */
15export declare class KernelConnection implements Kernel.IKernelConnection {
16 /**
17 * Construct a kernel object.
18 */
19 constructor(options: Kernel.IKernelConnection.IOptions);
20 get disposed(): ISignal<this, void>;
21 /**
22 * The server settings for the kernel.
23 */
24 readonly serverSettings: ServerConnection.ISettings;
25 /**
26 * Handle comm messages
27 *
28 * #### Notes
29 * The comm message protocol currently has implicit assumptions that only
30 * one kernel connection is handling comm messages. This option allows a
31 * kernel connection to opt out of handling comms.
32 *
33 * See https://github.com/jupyter/jupyter_client/issues/263
34 */
35 readonly handleComms: boolean;
36 /**
37 * A signal emitted when the kernel status changes.
38 */
39 get statusChanged(): ISignal<this, KernelMessage.Status>;
40 /**
41 * A signal emitted when the kernel status changes.
42 */
43 get connectionStatusChanged(): ISignal<this, Kernel.ConnectionStatus>;
44 /**
45 * A signal emitted for iopub kernel messages.
46 *
47 * #### Notes
48 * This signal is emitted after the iopub message is handled asynchronously.
49 */
50 get iopubMessage(): ISignal<this, KernelMessage.IIOPubMessage>;
51 /**
52 * A signal emitted for unhandled kernel message.
53 *
54 * #### Notes
55 * This signal is emitted for a message that was not handled. It is emitted
56 * during the asynchronous message handling code.
57 */
58 get unhandledMessage(): ISignal<this, KernelMessage.IMessage>;
59 /**
60 * The kernel model
61 */
62 get model(): Kernel.IModel;
63 /**
64 * A signal emitted for any kernel message.
65 *
66 * #### Notes
67 * This signal is emitted when a message is received, before it is handled
68 * asynchronously.
69 *
70 * This message is emitted when a message is queued for sending (either in
71 * the websocket buffer, or our own pending message buffer). The message may
72 * actually be sent across the wire at a later time.
73 *
74 * The message emitted in this signal should not be modified in any way.
75 */
76 get anyMessage(): ISignal<this, Kernel.IAnyMessageArgs>;
77 /**
78 * A signal emitted when a kernel has pending inputs from the user.
79 */
80 get pendingInput(): ISignal<this, boolean>;
81 /**
82 * The id of the server-side kernel.
83 */
84 get id(): string;
85 /**
86 * The name of the server-side kernel.
87 */
88 get name(): string;
89 /**
90 * The client username.
91 */
92 get username(): string;
93 /**
94 * The client unique id.
95 */
96 get clientId(): string;
97 /**
98 * The current status of the kernel.
99 */
100 get status(): KernelMessage.Status;
101 /**
102 * The current connection status of the kernel connection.
103 */
104 get connectionStatus(): Kernel.ConnectionStatus;
105 /**
106 * Test whether the kernel has been disposed.
107 */
108 get isDisposed(): boolean;
109 /**
110 * The cached kernel info.
111 *
112 * @returns A promise that resolves to the kernel info.
113 */
114 get info(): Promise<KernelMessage.IInfoReply>;
115 /**
116 * The kernel spec.
117 *
118 * @returns A promise that resolves to the kernel spec.
119 */
120 get spec(): Promise<KernelSpec.ISpecModel | undefined>;
121 /**
122 * Clone the current kernel with a new clientId.
123 */
124 clone(options?: Pick<Kernel.IKernelConnection.IOptions, 'clientId' | 'username' | 'handleComms'>): Kernel.IKernelConnection;
125 /**
126 * Dispose of the resources held by the kernel.
127 */
128 dispose(): void;
129 /**
130 * Send a shell message to the kernel.
131 *
132 * #### Notes
133 * Send a message to the kernel's shell channel, yielding a future object
134 * for accepting replies.
135 *
136 * If `expectReply` is given and `true`, the future is disposed when both a
137 * shell reply and an idle status message are received. If `expectReply`
138 * is not given or is `false`, the future is resolved when an idle status
139 * message is received.
140 * If `disposeOnDone` is not given or is `true`, the Future is disposed at this point.
141 * If `disposeOnDone` is given and `false`, it is up to the caller to dispose of the Future.
142 *
143 * All replies are validated as valid kernel messages.
144 *
145 * If the kernel status is `dead`, this will throw an error.
146 */
147 sendShellMessage<T extends KernelMessage.ShellMessageType>(msg: KernelMessage.IShellMessage<T>, expectReply?: boolean, disposeOnDone?: boolean): Kernel.IShellFuture<KernelMessage.IShellMessage<T>>;
148 /**
149 * Send a control message to the kernel.
150 *
151 * #### Notes
152 * Send a message to the kernel's control channel, yielding a future object
153 * for accepting replies.
154 *
155 * If `expectReply` is given and `true`, the future is disposed when both a
156 * control reply and an idle status message are received. If `expectReply`
157 * is not given or is `false`, the future is resolved when an idle status
158 * message is received.
159 * If `disposeOnDone` is not given or is `true`, the Future is disposed at this point.
160 * If `disposeOnDone` is given and `false`, it is up to the caller to dispose of the Future.
161 *
162 * All replies are validated as valid kernel messages.
163 *
164 * If the kernel status is `dead`, this will throw an error.
165 */
166 sendControlMessage<T extends KernelMessage.ControlMessageType>(msg: KernelMessage.IControlMessage<T>, expectReply?: boolean, disposeOnDone?: boolean): Kernel.IControlFuture<KernelMessage.IControlMessage<T>>;
167 private _sendKernelShellControl;
168 /**
169 * Send a message on the websocket.
170 *
171 * If queue is true, queue the message for later sending if we cannot send
172 * now. Otherwise throw an error.
173 *
174 * #### Notes
175 * As an exception to the queueing, if we are sending a kernel_info_request
176 * message while we think the kernel is restarting, we send the message
177 * immediately without queueing. This is so that we can trigger a message
178 * back, which will then clear the kernel restarting state.
179 */
180 private _sendMessage;
181 /**
182 * Interrupt a kernel.
183 *
184 * #### Notes
185 * Uses the [Jupyter Notebook API](http://petstore.swagger.io/?url=https://raw.githubusercontent.com/jupyter/notebook/master/notebook/services/api/api.yaml#!/kernels).
186 *
187 * The promise is fulfilled on a valid response and rejected otherwise.
188 *
189 * It is assumed that the API call does not mutate the kernel id or name.
190 *
191 * The promise will be rejected if the kernel status is `Dead` or if the
192 * request fails or the response is invalid.
193 */
194 interrupt(): Promise<void>;
195 /**
196 * Request a kernel restart.
197 *
198 * #### Notes
199 * Uses the [Jupyter Notebook API](http://petstore.swagger.io/?url=https://raw.githubusercontent.com/jupyter/notebook/master/notebook/services/api/api.yaml#!/kernels)
200 * and validates the response model.
201 *
202 * Any existing Future or Comm objects are cleared once the kernel has
203 * actually be restarted.
204 *
205 * The promise is fulfilled on a valid server response (after the kernel restarts)
206 * and rejected otherwise.
207 *
208 * It is assumed that the API call does not mutate the kernel id or name.
209 *
210 * The promise will be rejected if the request fails or the response is
211 * invalid.
212 */
213 restart(): Promise<void>;
214 /**
215 * Reconnect to a kernel.
216 *
217 * #### Notes
218 * This may try multiple times to reconnect to a kernel, and will sever any
219 * existing connection.
220 */
221 reconnect(): Promise<void>;
222 /**
223 * Shutdown a kernel.
224 *
225 * #### Notes
226 * Uses the [Jupyter Notebook API](http://petstore.swagger.io/?url=https://raw.githubusercontent.com/jupyter/notebook/master/notebook/services/api/api.yaml#!/kernels).
227 *
228 * The promise is fulfilled on a valid response and rejected otherwise.
229 *
230 * On a valid response, disposes this kernel connection.
231 *
232 * If the kernel is already `dead`, disposes this kernel connection without
233 * a server request.
234 */
235 shutdown(): Promise<void>;
236 /**
237 * Handles a kernel shutdown.
238 *
239 * #### Notes
240 * This method should be called if we know from outside information that a
241 * kernel is dead (for example, we cannot find the kernel model on the
242 * server).
243 */
244 handleShutdown(): void;
245 /**
246 * Send a `kernel_info_request` message.
247 *
248 * #### Notes
249 * See [Messaging in Jupyter](https://jupyter-client.readthedocs.io/en/latest/messaging.html#kernel-info).
250 *
251 * Fulfills with the `kernel_info_response` content when the shell reply is
252 * received and validated.
253 */
254 requestKernelInfo(): Promise<KernelMessage.IInfoReplyMsg | undefined>;
255 /**
256 * Send a `complete_request` message.
257 *
258 * #### Notes
259 * See [Messaging in Jupyter](https://jupyter-client.readthedocs.io/en/latest/messaging.html#completion).
260 *
261 * Fulfills with the `complete_reply` content when the shell reply is
262 * received and validated.
263 */
264 requestComplete(content: KernelMessage.ICompleteRequestMsg['content']): Promise<KernelMessage.ICompleteReplyMsg>;
265 /**
266 * Send an `inspect_request` message.
267 *
268 * #### Notes
269 * See [Messaging in Jupyter](https://jupyter-client.readthedocs.io/en/latest/messaging.html#introspection).
270 *
271 * Fulfills with the `inspect_reply` content when the shell reply is
272 * received and validated.
273 */
274 requestInspect(content: KernelMessage.IInspectRequestMsg['content']): Promise<KernelMessage.IInspectReplyMsg>;
275 /**
276 * Send a `history_request` message.
277 *
278 * #### Notes
279 * See [Messaging in Jupyter](https://jupyter-client.readthedocs.io/en/latest/messaging.html#history).
280 *
281 * Fulfills with the `history_reply` content when the shell reply is
282 * received and validated.
283 */
284 requestHistory(content: KernelMessage.IHistoryRequestMsg['content']): Promise<KernelMessage.IHistoryReplyMsg>;
285 /**
286 * Send an `execute_request` message.
287 *
288 * #### Notes
289 * See [Messaging in Jupyter](https://jupyter-client.readthedocs.io/en/latest/messaging.html#execute).
290 *
291 * Future `onReply` is called with the `execute_reply` content when the
292 * shell reply is received and validated. The future will resolve when
293 * this message is received and the `idle` iopub status is received.
294 * The future will also be disposed at this point unless `disposeOnDone`
295 * is specified and `false`, in which case it is up to the caller to dispose
296 * of the future.
297 *
298 * **See also:** [[IExecuteReply]]
299 */
300 requestExecute(content: KernelMessage.IExecuteRequestMsg['content'], disposeOnDone?: boolean, metadata?: JSONObject): Kernel.IShellFuture<KernelMessage.IExecuteRequestMsg, KernelMessage.IExecuteReplyMsg>;
301 /**
302 * Send an experimental `debug_request` message.
303 *
304 * @hidden
305 *
306 * #### Notes
307 * Debug messages are experimental messages that are not in the official
308 * kernel message specification. As such, this function is *NOT* considered
309 * part of the public API, and may change without notice.
310 */
311 requestDebug(content: KernelMessage.IDebugRequestMsg['content'], disposeOnDone?: boolean): Kernel.IControlFuture<KernelMessage.IDebugRequestMsg, KernelMessage.IDebugReplyMsg>;
312 /**
313 * Send an `is_complete_request` message.
314 *
315 * #### Notes
316 * See [Messaging in Jupyter](https://jupyter-client.readthedocs.io/en/latest/messaging.html#code-completeness).
317 *
318 * Fulfills with the `is_complete_response` content when the shell reply is
319 * received and validated.
320 */
321 requestIsComplete(content: KernelMessage.IIsCompleteRequestMsg['content']): Promise<KernelMessage.IIsCompleteReplyMsg>;
322 /**
323 * Send a `comm_info_request` message.
324 *
325 * #### Notes
326 * Fulfills with the `comm_info_reply` content when the shell reply is
327 * received and validated.
328 */
329 requestCommInfo(content: KernelMessage.ICommInfoRequestMsg['content']): Promise<KernelMessage.ICommInfoReplyMsg>;
330 /**
331 * Send an `input_reply` message.
332 *
333 * #### Notes
334 * See [Messaging in Jupyter](https://jupyter-client.readthedocs.io/en/latest/messaging.html#messages-on-the-stdin-router-dealer-sockets).
335 */
336 sendInputReply(content: KernelMessage.IInputReplyMsg['content'], parent_header?: KernelMessage.IInputReplyMsg['parent_header']): void;
337 /**
338 * Create a new comm.
339 *
340 * #### Notes
341 * If a client-side comm already exists with the given commId, an error is thrown.
342 * If the kernel does not handle comms, an error is thrown.
343 */
344 createComm(targetName: string, commId?: string): Kernel.IComm;
345 /**
346 * Check if a comm exists.
347 */
348 hasComm(commId: string): boolean;
349 /**
350 * Register a comm target handler.
351 *
352 * @param targetName - The name of the comm target.
353 *
354 * @param callback - The callback invoked for a comm open message.
355 *
356 * @returns A disposable used to unregister the comm target.
357 *
358 * #### Notes
359 * Only one comm target can be registered to a target name at a time, an
360 * existing callback for the same target name will be overridden. A registered
361 * comm target handler will take precedence over a comm which specifies a
362 * `target_module`.
363 *
364 * If the callback returns a promise, kernel message processing will pause
365 * until the returned promise is fulfilled.
366 */
367 registerCommTarget(targetName: string, callback: (comm: Kernel.IComm, msg: KernelMessage.ICommOpenMsg) => void | PromiseLike<void>): void;
368 /**
369 * Remove a comm target handler.
370 *
371 * @param targetName - The name of the comm target to remove.
372 *
373 * @param callback - The callback to remove.
374 *
375 * #### Notes
376 * The comm target is only removed if the callback argument matches.
377 */
378 removeCommTarget(targetName: string, callback: (comm: Kernel.IComm, msg: KernelMessage.ICommOpenMsg) => void | PromiseLike<void>): void;
379 /**
380 * Register an IOPub message hook.
381 *
382 * @param msg_id - The parent_header message id the hook will intercept.
383 *
384 * @param hook - The callback invoked for the message.
385 *
386 * #### Notes
387 * The IOPub hook system allows you to preempt the handlers for IOPub
388 * messages that are responses to a given message id.
389 *
390 * The most recently registered hook is run first. A hook can return a
391 * boolean or a promise to a boolean, in which case all kernel message
392 * processing pauses until the promise is fulfilled. If a hook return value
393 * resolves to false, any later hooks will not run and the function will
394 * return a promise resolving to false. If a hook throws an error, the error
395 * is logged to the console and the next hook is run. If a hook is
396 * registered during the hook processing, it will not run until the next
397 * message. If a hook is removed during the hook processing, it will be
398 * deactivated immediately.
399 *
400 * See also [[IFuture.registerMessageHook]].
401 */
402 registerMessageHook(msgId: string, hook: (msg: KernelMessage.IIOPubMessage) => boolean | PromiseLike<boolean>): void;
403 /**
404 * Remove an IOPub message hook.
405 *
406 * @param msg_id - The parent_header message id the hook intercepted.
407 *
408 * @param hook - The callback invoked for the message.
409 *
410 */
411 removeMessageHook(msgId: string, hook: (msg: KernelMessage.IIOPubMessage) => boolean | PromiseLike<boolean>): void;
412 /**
413 * Remove the input guard, if any.
414 */
415 removeInputGuard(): void;
416 /**
417 * Handle a message with a display id.
418 *
419 * @returns Whether the message was handled.
420 */
421 private _handleDisplayId;
422 /**
423 * Forcefully clear the socket state.
424 *
425 * #### Notes
426 * This will clear all socket state without calling any handlers and will
427 * not update the connection status. If you call this method, you are
428 * responsible for updating the connection status as needed and recreating
429 * the socket if you plan to reconnect.
430 */
431 private _clearSocket;
432 /**
433 * Handle status iopub messages from the kernel.
434 */
435 private _updateStatus;
436 /**
437 * Send pending messages to the kernel.
438 */
439 private _sendPending;
440 /**
441 * Clear the internal state.
442 */
443 private _clearKernelState;
444 /**
445 * Check to make sure it is okay to proceed to handle a message.
446 *
447 * #### Notes
448 * Because we handle messages asynchronously, before a message is handled the
449 * kernel might be disposed or restarted (and have a different session id).
450 * This function throws an error in each of these cases. This is meant to be
451 * called at the start of an asynchronous message handler to cancel message
452 * processing if the message no longer is valid.
453 */
454 private _assertCurrentMessage;
455 /**
456 * Handle a `comm_open` kernel message.
457 */
458 private _handleCommOpen;
459 /**
460 * Handle 'comm_close' kernel message.
461 */
462 private _handleCommClose;
463 /**
464 * Handle a 'comm_msg' kernel message.
465 */
466 private _handleCommMsg;
467 /**
468 * Unregister a comm instance.
469 */
470 private _unregisterComm;
471 /**
472 * Create the kernel websocket connection and add socket status handlers.
473 */
474 private _createSocket;
475 /**
476 * Handle connection status changes.
477 */
478 private _updateConnectionStatus;
479 private _handleMessage;
480 /**
481 * Attempt a connection if we have not exhausted connection attempts.
482 */
483 private _reconnect;
484 /**
485 * Utility function to throw an error if this instance is disposed.
486 */
487 private _errorIfDisposed;
488 /**
489 * Handle a websocket open event.
490 */
491 private _onWSOpen;
492 /**
493 * Handle a websocket message, validating and routing appropriately.
494 */
495 private _onWSMessage;
496 /**
497 * Handle a websocket close event.
498 */
499 private _onWSClose;
500 get hasPendingInput(): boolean;
501 set hasPendingInput(value: boolean);
502 private _id;
503 private _name;
504 private _model;
505 private _status;
506 private _connectionStatus;
507 private _kernelSession;
508 private _clientId;
509 private _isDisposed;
510 /**
511 * Websocket to communicate with kernel.
512 */
513 private _ws;
514 private _username;
515 private _reconnectLimit;
516 private _reconnectAttempt;
517 private _reconnectTimeout;
518 private _futures;
519 private _comms;
520 private _targetRegistry;
521 private _info;
522 private _pendingMessages;
523 private _specPromise;
524 private _statusChanged;
525 private _connectionStatusChanged;
526 private _disposed;
527 private _iopubMessage;
528 private _anyMessage;
529 private _pendingInput;
530 private _unhandledMessage;
531 private _displayIdToParentIds;
532 private _msgIdToDisplayIds;
533 private _msgChain;
534 private _hasPendingInput;
535 private _reason;
536 private _noOp;
537}
538
\No newline at end of file