123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050 |
- // Copyright (c) Jupyter Development Team.
- // Distributed under the terms of the Modified BSD License.
- import { IIterator } from '@phosphor/algorithm';
- import { JSONObject, JSONValue } from '@phosphor/coreutils';
- import { IDisposable } from '@phosphor/disposable';
- import { ISignal } from '@phosphor/signaling';
- import { ServerConnection } from '..';
- import { DefaultKernel } from './default';
- import { KernelMessage } from './messages';
- /**
- * A namespace for kernel types, interfaces, and type checker functions.
- */
- export namespace Kernel {
- /**
- * Interface of a Kernel connection that is managed by a session.
- *
- * #### Notes
- * The Kernel object is tied to the lifetime of the Kernel id, which is a
- * unique id for the Kernel session on the server. The Kernel object manages
- * a websocket connection internally, and will auto-restart if the websocket
- * temporarily loses connection. Restarting creates a new Kernel process on
- * the server, but preserves the Kernel id.
- */
- export interface IKernelConnection extends IDisposable {
- /**
- * The id of the server-side kernel.
- */
- readonly id: string;
- /**
- * The name of the server-side kernel.
- */
- readonly name: string;
- /**
- * The model associated with the kernel.
- */
- readonly model: Kernel.IModel;
- /**
- * The client username.
- */
- readonly username: string;
- /**
- * The client unique id.
- *
- * #### Notes
- * This should be unique for a particular kernel connection object.
- */
- readonly clientId: string;
- /**
- * The current status of the kernel.
- */
- readonly status: Kernel.Status;
- /**
- * The cached kernel info.
- *
- * #### Notes
- * This value will be null until the kernel is ready.
- */
- readonly info: KernelMessage.IInfoReply | null;
- /**
- * Test whether the kernel is ready.
- *
- * #### Notes
- * A kernel is ready when the communication channel is active and we have
- * cached the kernel info.
- */
- readonly isReady: boolean;
- /**
- * A promise that resolves when the kernel is initially ready after a start
- * or restart.
- *
- * #### Notes
- * A kernel is ready when the communication channel is active and we have
- * cached the kernel info.
- */
- readonly ready: Promise<void>;
- /**
- * Get the kernel spec.
- *
- * @returns A promise that resolves with the kernel spec for this kernel.
- */
- getSpec(): Promise<Kernel.ISpecModel>;
- /**
- * Send a shell message to the kernel.
- *
- * @param msg - The fully-formed shell message to send.
- *
- * @param expectReply - Whether to expect a shell reply message.
- *
- * @param disposeOnDone - Whether to dispose of the future when done.
- *
- * #### Notes
- * Send a message to the kernel's shell channel, yielding a future object
- * for accepting replies.
- *
- * If `expectReply` is given and `true`, the future is done when both a
- * shell reply and an idle status message are received with the appropriate
- * parent header, in which case the `.done` promise resolves to the reply.
- * If `expectReply` is not given or is `false`, the future is done when an
- * idle status message with the appropriate parent header is received, in
- * which case the `.done` promise resolves to `undefined`.
- *
- * If `disposeOnDone` is given and `false`, the future will not be disposed
- * of when the future is done, instead relying on the caller to dispose of
- * it. This allows for the handling of out-of-order output from ill-behaved
- * kernels.
- *
- * All replies are validated as valid kernel messages.
- *
- * If the kernel status is `'dead'`, this will throw an error.
- */
- sendShellMessage<T extends KernelMessage.ShellMessageType>(
- msg: KernelMessage.IShellMessage<T>,
- expectReply?: boolean,
- disposeOnDone?: boolean
- ): Kernel.IShellFuture<KernelMessage.IShellMessage<T>>;
- sendControlMessage<T extends KernelMessage.ControlMessageType>(
- msg: KernelMessage.IControlMessage<T>,
- expectReply?: boolean,
- disposeOnDone?: boolean
- ): Kernel.IControlFuture<KernelMessage.IControlMessage<T>>;
- /**
- * Reconnect to a disconnected kernel.
- *
- * @returns A promise that resolves when the kernel has reconnected.
- *
- * #### Notes
- * This just refreshes the connection to an existing kernel, and does not
- * perform an HTTP request to the server or restart the kernel.
- */
- reconnect(): Promise<void>;
- /**
- * Interrupt a kernel.
- *
- * @returns A promise that resolves when the kernel has interrupted.
- *
- * #### Notes
- * Uses the [Jupyter Notebook API](http://petstore.swagger.io/?url=https://raw.githubusercontent.com/jupyter/notebook/master/notebook/services/api/api.yaml#!/kernels).
- *
- * The promise is fulfilled on a valid response and rejected otherwise.
- *
- * It is assumed that the API call does not mutate the kernel id or name.
- *
- * The promise will be rejected if the kernel status is `'dead'` or if the
- * request fails or the response is invalid.
- */
- interrupt(): Promise<void>;
- /**
- * Restart a kernel.
- *
- * @returns A promise that resolves when the kernel has restarted.
- *
- * #### Notes
- * Uses the [Jupyter Notebook API](http://petstore.swagger.io/?url=https://raw.githubusercontent.com/jupyter/notebook/master/notebook/services/api/api.yaml#!/kernels) and validates the response model.
- *
- * Any existing Future or Comm objects are cleared.
- *
- * It is assumed that the API call does not mutate the kernel id or name.
- *
- * The promise will be rejected if the kernel status is `'dead'` or if the
- * request fails or the response is invalid.
- */
- restart(): Promise<void>;
- /**
- * Send a `kernel_info_request` message.
- *
- * @param content - The content of the request.
- *
- * @returns A promise that resolves with the response message.
- *
- * #### Notes
- * See [Messaging in Jupyter](https://jupyter-client.readthedocs.io/en/latest/messaging.html#kernel-info).
- *
- * Fulfills with the `kernel_info_response` content when the shell reply is
- * received and validated.
- */
- requestKernelInfo(): Promise<KernelMessage.IInfoReplyMsg>;
- /**
- * Send a `complete_request` message.
- *
- * @param content - The content of the request.
- *
- * @returns A promise that resolves with the response message.
- *
- * #### Notes
- * See [Messaging in Jupyter](https://jupyter-client.readthedocs.io/en/latest/messaging.html#completion).
- *
- * Fulfills with the `complete_reply` content when the shell reply is
- * received and validated.
- */
- requestComplete(
- content: KernelMessage.ICompleteRequestMsg['content']
- ): Promise<KernelMessage.ICompleteReplyMsg>;
- /**
- * Send an `inspect_request` message.
- *
- * @param content - The content of the request.
- *
- * @returns A promise that resolves with the response message.
- *
- * #### Notes
- * See [Messaging in Jupyter](https://jupyter-client.readthedocs.io/en/latest/messaging.html#introspection).
- *
- * Fulfills with the `inspect_reply` content when the shell reply is
- * received and validated.
- */
- requestInspect(
- content: KernelMessage.IInspectRequestMsg['content']
- ): Promise<KernelMessage.IInspectReplyMsg>;
- /**
- * Send a `history_request` message.
- *
- * @param content - The content of the request.
- *
- * @returns A promise that resolves with the response message.
- *
- * #### Notes
- * See [Messaging in Jupyter](https://jupyter-client.readthedocs.io/en/latest/messaging.html#history).
- *
- * Fulfills with the `history_reply` content when the shell reply is
- * received and validated.
- */
- requestHistory(
- content: KernelMessage.IHistoryRequestMsg['content']
- ): Promise<KernelMessage.IHistoryReplyMsg>;
- /**
- * Send an `execute_request` message.
- *
- * @param content - The content of the request.
- *
- * @param disposeOnDone - Whether to dispose of the future when done.
- *
- * @returns A kernel future.
- *
- * #### Notes
- * See [Messaging in
- * Jupyter](https://jupyter-client.readthedocs.io/en/latest/messaging.html#execute).
- *
- * This method returns a kernel future, rather than a promise, since execution may
- * have many response messages (for example, many iopub display messages).
- *
- * Future `onReply` is called with the `execute_reply` content when the
- * shell reply is received and validated.
- *
- * **See also:** [[IExecuteReply]]
- */
- requestExecute(
- content: KernelMessage.IExecuteRequestMsg['content'],
- disposeOnDone?: boolean,
- metadata?: JSONObject
- ): Kernel.IShellFuture<
- KernelMessage.IExecuteRequestMsg,
- KernelMessage.IExecuteReplyMsg
- >;
- /**
- * Send an `is_complete_request` message.
- *
- * @param content - The content of the request.
- *
- * @returns A promise that resolves with the response message.
- *
- * #### Notes
- * See [Messaging in Jupyter](https://jupyter-client.readthedocs.io/en/latest/messaging.html#code-completeness).
- *
- * Fulfills with the `is_complete_response` content when the shell reply is
- * received and validated.
- */
- requestIsComplete(
- content: KernelMessage.IIsCompleteRequestMsg['content']
- ): Promise<KernelMessage.IIsCompleteReplyMsg>;
- /**
- * Send a `comm_info_request` message.
- *
- * @param content - The content of the request.
- *
- * @returns A promise that resolves with the response message.
- *
- * #### Notes
- * See [Messaging in Jupyter](https://jupyter-client.readthedocs.io/en/latest/messaging.html#comm_info).
- *
- * Fulfills with the `comm_info_reply` content when the shell reply is
- * received and validated.
- */
- requestCommInfo(
- content: KernelMessage.ICommInfoRequestMsg['content']
- ): Promise<KernelMessage.ICommInfoReplyMsg>;
- /**
- * Send an `input_reply` message.
- *
- * @param content - The content of the reply.
- *
- * #### Notes
- * See [Messaging in Jupyter](https://jupyter-client.readthedocs.io/en/latest/messaging.html#messages-on-the-stdin-router-dealer-sockets).
- */
- sendInputReply(content: KernelMessage.IInputReplyMsg['content']): void;
- /**
- * Connect to a comm, or create a new one.
- *
- * @param targetName - The name of the comm target.
- *
- * @param id - The comm id.
- *
- * @returns A comm instance.
- */
- connectToComm(targetName: string, commId?: string): Kernel.IComm;
- /**
- * Register a comm target handler.
- *
- * @param targetName - The name of the comm target.
- *
- * @param callback - The callback invoked for a comm open message.
- *
- * #### Notes
- * Only one comm target can be registered to a target name at a time, an
- * existing callback for the same target name will be overridden. A registered
- * comm target handler will take precedence over a comm which specifies a
- * `target_module`.
- *
- * If the callback returns a promise, kernel message processing will pause
- * until the returned promise is fulfilled.
- */
- registerCommTarget(
- targetName: string,
- callback: (
- comm: Kernel.IComm,
- msg: KernelMessage.ICommOpenMsg
- ) => void | PromiseLike<void>
- ): void;
- /**
- * Remove a comm target handler.
- *
- * @param targetName - The name of the comm target to remove.
- *
- * @param callback - The callback to remove.
- *
- * #### Notes
- * The comm target is only removed if it matches the callback argument.
- */
- removeCommTarget(
- targetName: string,
- callback: (
- comm: Kernel.IComm,
- msg: KernelMessage.ICommOpenMsg
- ) => void | PromiseLike<void>
- ): void;
- /**
- * Register an IOPub message hook.
- *
- * @param msg_id - The parent_header message id in messages the hook should
- * intercept.
- *
- * @param hook - The callback invoked for the message.
- *
- * #### Notes
- * The IOPub hook system allows you to preempt the handlers for IOPub
- * messages with a given parent_header message id. The most recently
- * registered hook is run first. If a hook return value resolves to false,
- * any later hooks and the future's onIOPub handler will not run. If a hook
- * throws an error, the error is logged to the console and the next hook is
- * run. If a hook is registered during the hook processing, it will not run
- * until the next message. If a hook is disposed during the hook processing,
- * it will be deactivated immediately.
- *
- * See also [[IFuture.registerMessageHook]].
- */
- registerMessageHook(
- msgId: string,
- hook: (msg: KernelMessage.IIOPubMessage) => boolean | PromiseLike<boolean>
- ): void;
- /**
- * Remove an IOPub message hook.
- *
- * @param msg_id - The parent_header message id the hook intercepted.
- *
- * @param hook - The callback invoked for the message.
- *
- */
- removeMessageHook(
- msgId: string,
- hook: (msg: KernelMessage.IIOPubMessage) => boolean | PromiseLike<boolean>
- ): void;
- }
- /**
- * The full interface of a kernel.
- */
- export interface IKernel extends IKernelConnection {
- /**
- * A signal emitted when the kernel is shut down.
- */
- terminated: ISignal<this, void>;
- /**
- * A signal emitted when the kernel status changes.
- */
- statusChanged: ISignal<this, Kernel.Status>;
- /**
- * A signal emitted after an iopub kernel message is handled.
- */
- iopubMessage: ISignal<this, KernelMessage.IIOPubMessage>;
- /**
- * A signal emitted for unhandled non-iopub kernel messages that claimed to
- * be responses for messages we sent using this kernel object.
- */
- unhandledMessage: ISignal<this, KernelMessage.IMessage>;
- /**
- * A signal emitted when any kernel message is sent or received.
- *
- * #### Notes
- * This signal is emitted before any message handling has happened. The
- * message should be treated as read-only.
- */
- anyMessage: ISignal<this, IAnyMessageArgs>;
- /**
- * The server settings for the kernel.
- */
- readonly serverSettings: ServerConnection.ISettings;
- /**
- * Shutdown a kernel.
- *
- * @returns A promise that resolves when the kernel has shut down.
- *
- * #### Notes
- * Uses the [Jupyter Notebook
- * API](http://petstore.swagger.io/?url=https://raw.githubusercontent.com/jupyter/notebook/master/notebook/services/api/api.yaml#!/kernels).
- *
- * On a valid response, closes the websocket, emits the [[terminated]]
- * signal, disposes of the kernel object, and fulfills the promise.
- *
- * The promise will be rejected if the kernel status is `'dead'`, the
- * request fails, or the response is invalid.
- */
- shutdown(): Promise<void>;
- }
- /**
- * Find a kernel by id.
- *
- * @param id - The id of the kernel of interest.
- *
- * @param settings - The optional server settings.
- *
- * @returns A promise that resolves with the model for the kernel.
- *
- * #### Notes
- * If the kernel was already started via `startNewKernel`, we return its
- * `Kernel.IModel`. Otherwise, we attempt to find the existing kernel. The
- * promise is fulfilled when the kernel is found, otherwise the promise is
- * rejected.
- */
- export function findById(
- id: string,
- settings?: ServerConnection.ISettings
- ): Promise<IModel> {
- return DefaultKernel.findById(id, settings);
- }
- /**
- * Fetch all of the kernel specs.
- *
- * @param settings - The optional server settings.
- *
- * @returns A promise that resolves with the kernel specs.
- *
- * #### Notes
- * Uses the [Jupyter Notebook API](http://petstore.swagger.io/?url=https://raw.githubusercontent.com/jupyter/notebook/master/notebook/services/api/api.yaml#!/kernelspecs).
- */
- export function getSpecs(
- settings?: ServerConnection.ISettings
- ): Promise<Kernel.ISpecModels> {
- return DefaultKernel.getSpecs(settings);
- }
- /**
- * Fetch the running kernels.
- *
- * @param settings - The optional server settings.
- *
- * @returns A promise that resolves with the list of running kernels.
- *
- * #### Notes
- * Uses the [Jupyter Notebook API](http://petstore.swagger.io/?url=https://raw.githubusercontent.com/jupyter/notebook/master/notebook/services/api/api.yaml#!/kernels) and validates the response model.
- *
- * The promise is fulfilled on a valid response and rejected otherwise.
- */
- export function listRunning(
- settings?: ServerConnection.ISettings
- ): Promise<Kernel.IModel[]> {
- return DefaultKernel.listRunning(settings);
- }
- /**
- * Start a new kernel.
- *
- * @param options - The options used to create the kernel.
- *
- * @returns A promise that resolves with a kernel object.
- *
- * #### Notes
- * Uses the [Jupyter Notebook API](http://petstore.swagger.io/?url=https://raw.githubusercontent.com/jupyter/notebook/master/notebook/services/api/api.yaml#!/kernels) and validates the response model.
- *
- * If no options are given or the kernel name is not given, the
- * default kernel will by started by the server.
- *
- * Wraps the result in a Kernel object. The promise is fulfilled
- * when the kernel is started by the server, otherwise the promise is rejected.
- */
- export function startNew(options: Kernel.IOptions = {}): Promise<IKernel> {
- return DefaultKernel.startNew(options);
- }
- /**
- * Connect to a running kernel.
- *
- * @param model - The model of the running kernel.
- *
- * @param settings - The server settings for the request.
- *
- * @returns The kernel object.
- *
- * #### Notes
- * If the kernel was already started via `startNewKernel`, the existing
- * Kernel object info is used to create another instance.
- */
- export function connectTo(
- model: Kernel.IModel,
- settings?: ServerConnection.ISettings
- ): IKernel {
- return DefaultKernel.connectTo(model, settings);
- }
- /**
- * Shut down a kernel by id.
- *
- * @param id - The id of the running kernel.
- *
- * @param settings - The server settings for the request.
- *
- * @returns A promise that resolves when the kernel is shut down.
- */
- export function shutdown(
- id: string,
- settings?: ServerConnection.ISettings
- ): Promise<void> {
- return DefaultKernel.shutdown(id, settings);
- }
- /**
- * Shut down all kernels.
- *
- * @returns A promise that resolves when all of the kernels are shut down.
- */
- export function shutdownAll(
- settings?: ServerConnection.ISettings
- ): Promise<void> {
- return DefaultKernel.shutdownAll(settings);
- }
- /**
- * The options object used to initialize a kernel.
- */
- export interface IOptions {
- /**
- * The kernel type (e.g. python3).
- */
- name?: string;
- /**
- * The server settings for the kernel.
- */
- serverSettings?: ServerConnection.ISettings;
- /**
- * The username of the kernel client.
- */
- username?: string;
- /**
- * The unique identifier for the kernel client.
- */
- clientId?: string;
- }
- /**
- * Object which manages kernel instances for a given base url.
- *
- * #### Notes
- * The manager is responsible for maintaining the state of running
- * kernels and the initial fetch of kernel specs.
- */
- export interface IManager extends IDisposable {
- /**
- * A signal emitted when the kernel specs change.
- */
- specsChanged: ISignal<IManager, ISpecModels>;
- /**
- * A signal emitted when the running kernels change.
- */
- runningChanged: ISignal<IManager, IModel[]>;
- /**
- * A signal emitted when there is a connection failure.
- */
- connectionFailure: ISignal<IManager, ServerConnection.NetworkError>;
- /**
- * The server settings for the manager.
- */
- serverSettings?: ServerConnection.ISettings;
- /**
- * The kernel spec models.
- *
- * #### Notes
- * The value will be null until the manager is ready.
- */
- readonly specs: Kernel.ISpecModels | null;
- /**
- * Whether the manager is ready.
- */
- readonly isReady: boolean;
- /**
- * A promise that resolves when the manager is initially ready.
- */
- readonly ready: Promise<void>;
- /**
- * Create an iterator over the known running kernels.
- *
- * @returns A new iterator over the running kernels.
- */
- running(): IIterator<IModel>;
- /**
- * Force a refresh of the specs from the server.
- *
- * @returns A promise that resolves when the specs are fetched.
- *
- * #### Notes
- * This is intended to be called only in response to a user action,
- * since the manager maintains its internal state.
- */
- refreshSpecs(): Promise<void>;
- /**
- * Force a refresh of the running kernels.
- *
- * @returns A promise that resolves when the models are refreshed.
- *
- * #### Notes
- * This is intended to be called only in response to a user action,
- * since the manager maintains its internal state.
- */
- refreshRunning(): Promise<void>;
- /**
- * Start a new kernel.
- *
- * @param options - The kernel options to use.
- *
- * @returns A promise that resolves with the kernel instance.
- *
- * #### Notes
- * The manager `serverSettings` will be always be used.
- */
- startNew(options?: IOptions): Promise<IKernel>;
- /**
- * Find a kernel by id.
- *
- * @param id - The id of the target kernel.
- *
- * @returns A promise that resolves with the kernel's model.
- */
- findById(id: string): Promise<IModel>;
- /**
- * Connect to an existing kernel.
- *
- * @param model - The model of the target kernel.
- *
- * @returns A promise that resolves with the new kernel instance.
- */
- connectTo(model: Kernel.IModel): IKernel;
- /**
- * Shut down a kernel by id.
- *
- * @param id - The id of the target kernel.
- *
- * @returns A promise that resolves when the operation is complete.
- */
- shutdown(id: string): Promise<void>;
- /**
- * Shut down all kernels.
- *
- * @returns A promise that resolves when all of the kernels are shut down.
- */
- shutdownAll(): Promise<void>;
- }
- /**
- * A Future interface for responses from the kernel.
- *
- * When a message is sent to a kernel, a Future is created to handle any
- * responses that may come from the kernel.
- */
- export interface IFuture<
- REQUEST extends KernelMessage.IShellControlMessage,
- REPLY extends KernelMessage.IShellControlMessage
- > extends IDisposable {
- /**
- * The original outgoing message.
- */
- readonly msg: REQUEST;
- /**
- * A promise that resolves when the future is done.
- *
- * #### Notes
- * The future is done when there are no more responses expected from the
- * kernel.
- *
- * The `done` promise resolves to the reply message if there is one,
- * otherwise it resolves to `undefined`.
- */
- readonly done: Promise<REPLY | undefined>;
- /**
- * The reply handler for the kernel future.
- *
- * #### Notes
- * If the handler returns a promise, all kernel message processing pauses
- * until the promise is resolved. If there is a reply message, the future
- * `done` promise also resolves to the reply message after this handler has
- * been called.
- */
- onReply: (msg: REPLY) => void | PromiseLike<void>;
- /**
- * The iopub handler for the kernel future.
- *
- * #### Notes
- * If the handler returns a promise, all kernel message processing pauses
- * until the promise is resolved.
- */
- onIOPub: (msg: KernelMessage.IIOPubMessage) => void | PromiseLike<void>;
- /**
- * The stdin handler for the kernel future.
- *
- * #### Notes
- * If the handler returns a promise, all kernel message processing pauses
- * until the promise is resolved.
- */
- onStdin: (msg: KernelMessage.IStdinMessage) => void | PromiseLike<void>;
- /**
- * Register hook for IOPub messages.
- *
- * @param hook - The callback invoked for an IOPub message.
- *
- * #### Notes
- * The IOPub hook system allows you to preempt the handlers for IOPub
- * messages handled by the future.
- *
- * The most recently registered hook is run first. A hook can return a
- * boolean or a promise to a boolean, in which case all kernel message
- * processing pauses until the promise is fulfilled. If a hook return value
- * resolves to false, any later hooks will not run and the function will
- * return a promise resolving to false. If a hook throws an error, the error
- * is logged to the console and the next hook is run. If a hook is
- * registered during the hook processing, it will not run until the next
- * message. If a hook is removed during the hook processing, it will be
- * deactivated immediately.
- */
- registerMessageHook(
- hook: (msg: KernelMessage.IIOPubMessage) => boolean | PromiseLike<boolean>
- ): void;
- /**
- * Remove a hook for IOPub messages.
- *
- * @param hook - The hook to remove.
- *
- * #### Notes
- * If a hook is removed during the hook processing, it will be deactivated immediately.
- */
- removeMessageHook(
- hook: (msg: KernelMessage.IIOPubMessage) => boolean | PromiseLike<boolean>
- ): void;
- /**
- * Send an `input_reply` message.
- */
- sendInputReply(content: KernelMessage.IInputReplyMsg['content']): void;
- }
- export interface IShellFuture<
- REQUEST extends KernelMessage.IShellMessage = KernelMessage.IShellMessage,
- REPLY extends KernelMessage.IShellMessage = KernelMessage.IShellMessage
- > extends IFuture<REQUEST, REPLY> {}
- export interface IControlFuture<
- REQUEST extends KernelMessage.IControlMessage = KernelMessage.IControlMessage,
- REPLY extends KernelMessage.IControlMessage = KernelMessage.IControlMessage
- > extends IFuture<REQUEST, REPLY> {}
- /**
- * A client side Comm interface.
- */
- export interface IComm extends IDisposable {
- /**
- * The unique id for the comm channel.
- */
- readonly commId: string;
- /**
- * The target name for the comm channel.
- */
- readonly targetName: string;
- /**
- * Callback for a comm close event.
- *
- * #### Notes
- * This is called when the comm is closed from either the server or client.
- * If this is called in response to a kernel message and the handler returns
- * a promise, all kernel message processing pauses until the promise is
- * resolved.
- */
- onClose: (msg: KernelMessage.ICommCloseMsg) => void | PromiseLike<void>;
- /**
- * Callback for a comm message received event.
- *
- * #### Notes
- * If the handler returns a promise, all kernel message processing pauses
- * until the promise is resolved.
- */
- onMsg: (msg: KernelMessage.ICommMsgMsg) => void | PromiseLike<void>;
- /**
- * Open a comm with optional data and metadata.
- *
- * @param data - The data to send to the server on opening.
- *
- * @param metadata - Additional metatada for the message.
- *
- * @returns A future for the generated message.
- *
- * #### Notes
- * This sends a `comm_open` message to the server.
- */
- open(
- data?: JSONValue,
- metadata?: JSONObject,
- buffers?: (ArrayBuffer | ArrayBufferView)[]
- ): IShellFuture;
- /**
- * Send a `comm_msg` message to the kernel.
- *
- * @param data - The data to send to the server on opening.
- *
- * @param metadata - Additional metatada for the message.
- *
- * @param buffers - Optional buffer data.
- *
- * @param disposeOnDone - Whether to dispose of the future when done.
- *
- * @returns A future for the generated message.
- *
- * #### Notes
- * This is a no-op if the comm has been closed.
- */
- send(
- data: JSONValue,
- metadata?: JSONObject,
- buffers?: (ArrayBuffer | ArrayBufferView)[],
- disposeOnDone?: boolean
- ): IShellFuture;
- /**
- * Close the comm.
- *
- * @param data - The data to send to the server on opening.
- *
- * @param metadata - Additional metatada for the message.
- *
- * @returns A future for the generated message.
- *
- * #### Notes
- * This will send a `comm_close` message to the kernel, and call the
- * `onClose` callback if set.
- *
- * This is a no-op if the comm is already closed.
- */
- close(
- data?: JSONValue,
- metadata?: JSONObject,
- buffers?: (ArrayBuffer | ArrayBufferView)[]
- ): IShellFuture;
- }
- /**
- * The valid Kernel status states.
- */
- export type Status =
- | 'unknown'
- | 'starting'
- | 'reconnecting'
- | 'idle'
- | 'busy'
- | 'restarting'
- | 'autorestarting'
- | 'dead'
- | 'connected';
- /**
- * The kernel model provided by the server.
- *
- * #### Notes
- * See the [Jupyter Notebook API](http://petstore.swagger.io/?url=https://raw.githubusercontent.com/jupyter/notebook/master/notebook/services/api/api.yaml#!/kernels).
- */
- export interface IModel extends JSONObject {
- /**
- * Unique identifier of the kernel server session.
- */
- readonly id: string;
- /**
- * The name of the kernel.
- */
- readonly name: string;
- }
- /**
- * Kernel Spec interface.
- *
- * #### Notes
- * See [Kernel specs](https://jupyter-client.readthedocs.io/en/latest/kernels.html#kernelspecs).
- */
- export interface ISpecModel extends JSONObject {
- /**
- * The name of the kernel spec.
- */
- readonly name: string;
- /**
- * The name of the language of the kernel.
- */
- readonly language: string;
- /**
- * A list of command line arguments used to start the kernel.
- */
- readonly argv: string[];
- /**
- * The kernel’s name as it should be displayed in the UI.
- */
- readonly display_name: string;
- /**
- * A dictionary of environment variables to set for the kernel.
- */
- readonly env?: JSONObject;
- /**
- * A mapping of resource file name to download path.
- */
- readonly resources: { [key: string]: string };
- }
- /**
- * The available kernelSpec models.
- *
- * #### Notes
- * See the [Jupyter Notebook API](http://petstore.swagger.io/?url=https://raw.githubusercontent.com/jupyter/notebook/master/notebook/services/api/api.yaml#!/kernelspecs).
- */
- export interface ISpecModels extends JSONObject {
- /**
- * The name of the default kernel spec.
- */
- default: string;
- /**
- * A mapping of kernel spec name to spec.
- */
- readonly kernelspecs: { [key: string]: ISpecModel };
- }
- /**
- * Arguments interface for the anyMessage signal.
- */
- export interface IAnyMessageArgs {
- /**
- * The message that is being signaled.
- */
- msg: Readonly<KernelMessage.IMessage>;
- /**
- * The direction of the message.
- */
- direction: 'send' | 'recv';
- }
- }
|