123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397 |
- // Copyright (c) Jupyter Development Team.
- // Distributed under the terms of the Modified BSD License.
- import { IIterator } from '@lumino/algorithm';
- import { IDisposable, IObservableDisposable } from '@lumino/disposable';
- import { ISignal } from '@lumino/signaling';
- import { Kernel, KernelMessage } from '../kernel';
- import { ServerConnection } from '..';
- import { IChangedArgs } from '@jupyterlab/coreutils';
- /**
- * Interface of a session object.
- *
- * A session object represents a live connection to a session kernel.
- *
- * This represents a persistent kernel connection with a particular key,
- * that persists across changing kernels and kernels getting terminated. As
- * such, a number of signals are proxied from the current kernel for
- * convenience.
- *
- * The kernel is owned by the session, in that the session creates the
- * kernel and manages its lifecycle.
- */
- export interface ISessionConnection extends IObservableDisposable {
- /**
- * A signal emitted when a session property changes.
- */
- readonly propertyChanged: ISignal<this, 'path' | 'name' | 'type'>;
- /**
- * A signal emitted when the kernel changes.
- */
- kernelChanged: ISignal<
- this,
- IChangedArgs<
- Kernel.IKernelConnection | null,
- Kernel.IKernelConnection | null,
- 'kernel'
- >
- >;
- /**
- * The kernel statusChanged signal, proxied from the current kernel.
- */
- statusChanged: ISignal<this, Kernel.Status>;
- /**
- * The kernel connectionStatusChanged signal, proxied from the current
- * kernel.
- */
- connectionStatusChanged: ISignal<this, Kernel.ConnectionStatus>;
- /**
- * The kernel pendingInput signal, proxied from the current
- * kernel.
- */
- pendingInput: ISignal<this, boolean>;
- /**
- * The kernel iopubMessage signal, proxied from the current kernel.
- */
- iopubMessage: ISignal<this, KernelMessage.IIOPubMessage>;
- /**
- * The kernel unhandledMessage signal, proxied from the current kernel.
- */
- unhandledMessage: ISignal<this, KernelMessage.IMessage>;
- /**
- * The kernel anyMessage signal, proxied from the current kernel.
- */
- anyMessage: ISignal<this, Kernel.IAnyMessageArgs>;
- /**
- * Unique id of the session.
- */
- readonly id: string;
- /**
- * The current path associated with the session.
- */
- readonly path: string;
- /**
- * The current name associated with the session.
- */
- readonly name: string;
- /**
- * The type of the session.
- */
- readonly type: string;
- /**
- * The server settings of the session.
- */
- readonly serverSettings: ServerConnection.ISettings;
- /**
- * The model associated with the session.
- */
- readonly model: IModel;
- /**
- * The kernel.
- *
- * #### Notes
- * This is a read-only property, and can be altered by [changeKernel].
- *
- * A number of kernel signals are proxied through the session from
- * whatever the current kernel is for convenience.
- */
- readonly kernel: Kernel.IKernelConnection | null;
- /**
- * Change the session path.
- *
- * @param path - The new session path.
- *
- * @returns A promise that resolves when the session has renamed.
- *
- * #### Notes
- * This uses the Jupyter REST API, and the response is validated.
- * The promise is fulfilled on a valid response and rejected otherwise.
- */
- setPath(path: string): Promise<void>;
- /**
- * Change the session name.
- *
- * @returns A promise that resolves when the session has renamed.
- *
- * #### Notes
- * This uses the Jupyter REST API, and the response is validated.
- * The promise is fulfilled on a valid response and rejected otherwise.
- */
- setName(name: string): Promise<void>;
- /**
- * Change the session type.
- *
- * @returns A promise that resolves when the session has renamed.
- *
- * #### Notes
- * This uses the Jupyter REST API, and the response is validated.
- * The promise is fulfilled on a valid response and rejected otherwise.
- */
- setType(type: string): Promise<void>;
- /**
- * Change the kernel.
- *
- * @param options - The name or id of the new kernel.
- *
- * @returns A promise that resolves with the new kernel model.
- *
- * #### Notes
- * This shuts down the existing kernel and creates a new kernel, keeping
- * the existing session ID and path. The session assumes it owns the
- * kernel.
- *
- * To start now kernel, pass an empty dictionary.
- */
- changeKernel(
- options: Partial<Kernel.IModel>
- ): Promise<Kernel.IKernelConnection | null>;
- /**
- * Kill the kernel and shutdown the session.
- *
- * @returns A promise that resolves when the session is shut down.
- *
- * #### Notes
- * This uses the Jupyter REST API, and the response is validated.
- * The promise is fulfilled on a valid response and rejected otherwise.
- */
- shutdown(): Promise<void>;
- }
- export namespace ISessionConnection {
- /**
- * The session initialization options.
- */
- export interface IOptions {
- /**
- * Session model.
- */
- model: IModel;
- /**
- * Connects to an existing kernel
- */
- connectToKernel(
- options: Kernel.IKernelConnection.IOptions
- ): Kernel.IKernelConnection;
- /**
- * The server settings.
- */
- serverSettings?: ServerConnection.ISettings;
- /**
- * The username of the session client.
- */
- username?: string;
- /**
- * The unique identifier for the session client.
- */
- clientId?: string;
- /**
- * Kernel connection options
- */
- kernelConnectionOptions?: Omit<
- Kernel.IKernelConnection.IOptions,
- 'model' | 'username' | 'clientId' | 'serverSettings'
- >;
- }
- /**
- * An arguments object for the kernel changed signal.
- */
- export type IKernelChangedArgs = IChangedArgs<
- Kernel.IKernelConnection | null,
- Kernel.IKernelConnection | null,
- 'kernel'
- >;
- }
- /**
- * Object which manages session instances.
- *
- * #### Notes
- * The manager is responsible for maintaining the state of running
- * sessions.
- */
- export interface IManager extends IDisposable {
- /**
- * A signal emitted when the running sessions change.
- */
- runningChanged: ISignal<this, IModel[]>;
- /**
- * A signal emitted when there is a connection failure.
- */
- connectionFailure: ISignal<IManager, ServerConnection.NetworkError>;
- /**
- * The server settings for the manager.
- */
- serverSettings?: ServerConnection.ISettings;
- /**
- * Test whether the manager is ready.
- */
- readonly isReady: boolean;
- /**
- * A promise that is fulfilled when the manager is ready.
- */
- readonly ready: Promise<void>;
- /**
- * Create an iterator over the known running sessions.
- *
- * @returns A new iterator over the running sessions.
- */
- running(): IIterator<IModel>;
- /**
- * Start a new session.
- *
- * @param createOptions - Options for creating the session
- *
- * @param connectOptions - Options for connecting to the session
- *
- * @returns A promise that resolves with a session connection instance.
- *
- * #### Notes
- * The `serverSettings` and `connectToKernel` options of the manager will be used.
- */
- startNew(
- createOptions: ISessionOptions,
- connectOptions?: Omit<
- ISessionConnection.IOptions,
- 'model' | 'connectToKernel' | 'serverSettings'
- >
- ): Promise<ISessionConnection>;
- /**
- * Find a session by id.
- *
- * @param id - The id of the target session.
- *
- * @returns A promise that resolves with the session's model.
- */
- findById(id: string): Promise<IModel | undefined>;
- /**
- * Find a session by path.
- *
- * @param path - The path of the target session.
- *
- * @returns A promise that resolves with the session's model.
- */
- findByPath(path: string): Promise<IModel | undefined>;
- /**
- * Connect to a running session.
- *
- * @param model - The model of the target session.
- *
- * @param options - The session options to use.
- *
- * @returns The new session instance.
- */
- connectTo(
- options: Omit<
- ISessionConnection.IOptions,
- 'connectToKernel' | 'serverSettings'
- >
- ): ISessionConnection;
- /**
- * Shut down a session 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 sessions.
- *
- * @returns A promise that resolves when all of the sessions are shut down.
- */
- shutdownAll(): Promise<void>;
- /**
- * Force a refresh of the running sessions.
- *
- * @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>;
- /**
- * Find a session associated with a path and stop it is the only session
- * using that kernel.
- *
- * @param path - The path in question.
- *
- * @returns A promise that resolves when the relevant sessions are stopped.
- */
- stopIfNeeded(path: string): Promise<void>;
- }
- /**
- * The session model returned 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#!/sessions).
- */
- export interface IModel {
- /**
- * The unique identifier for the session client.
- */
- readonly id: string;
- readonly name: string;
- readonly path: string;
- readonly type: string;
- readonly kernel: Kernel.IModel | null;
- }
- /**
- * A session request.
- *
- * #### Notes
- * The `path` and `type` session model parameters are required. The `name`
- * parameter is not technically required, but is often assumed to be nonempty,
- * so we require it too.
- *
- * See the [Jupyter Notebook API](http://petstore.swagger.io/?url=https://raw.githubusercontent.com/jupyter/notebook/master/notebook/services/api/api.yaml#!/sessions).
- */
- export type ISessionOptions = Pick<IModel, 'path' | 'type' | 'name'> & {
- kernel?: Partial<Pick<Kernel.IModel, 'name'>>;
- };
|