UNPKG

10.3 kBTypeScriptView Raw
1import { IDisposable, IObservableDisposable } from '@lumino/disposable';
2import { ISignal } from '@lumino/signaling';
3import { Kernel, KernelMessage } from '../kernel';
4import { ServerConnection } from '..';
5import { IChangedArgs } from '@jupyterlab/coreutils';
6/**
7 * Interface of a session object.
8 *
9 * A session object represents a live connection to a session kernel.
10 *
11 * This represents a persistent kernel connection with a particular key,
12 * that persists across changing kernels and kernels getting terminated. As
13 * such, a number of signals are proxied from the current kernel for
14 * convenience.
15 *
16 * The kernel is owned by the session, in that the session creates the
17 * kernel and manages its lifecycle.
18 */
19export interface ISessionConnection extends IObservableDisposable {
20 /**
21 * A signal emitted when a session property changes.
22 */
23 readonly propertyChanged: ISignal<this, 'path' | 'name' | 'type'>;
24 /**
25 * A signal emitted when the kernel changes.
26 */
27 kernelChanged: ISignal<this, IChangedArgs<Kernel.IKernelConnection | null, Kernel.IKernelConnection | null, 'kernel'>>;
28 /**
29 * The kernel statusChanged signal, proxied from the current kernel.
30 */
31 statusChanged: ISignal<this, Kernel.Status>;
32 /**
33 * The kernel connectionStatusChanged signal, proxied from the current
34 * kernel.
35 */
36 connectionStatusChanged: ISignal<this, Kernel.ConnectionStatus>;
37 /**
38 * The kernel pendingInput signal, proxied from the current
39 * kernel.
40 */
41 pendingInput: ISignal<this, boolean>;
42 /**
43 * The kernel iopubMessage signal, proxied from the current kernel.
44 */
45 iopubMessage: ISignal<this, KernelMessage.IIOPubMessage>;
46 /**
47 * The kernel unhandledMessage signal, proxied from the current kernel.
48 */
49 unhandledMessage: ISignal<this, KernelMessage.IMessage>;
50 /**
51 * The kernel anyMessage signal, proxied from the current kernel.
52 */
53 anyMessage: ISignal<this, Kernel.IAnyMessageArgs>;
54 /**
55 * Unique id of the session.
56 */
57 readonly id: string;
58 /**
59 * The current path associated with the session.
60 */
61 readonly path: string;
62 /**
63 * The current name associated with the session.
64 */
65 readonly name: string;
66 /**
67 * The type of the session.
68 */
69 readonly type: string;
70 /**
71 * The server settings of the session.
72 */
73 readonly serverSettings: ServerConnection.ISettings;
74 /**
75 * The model associated with the session.
76 */
77 readonly model: IModel;
78 /**
79 * The kernel.
80 *
81 * #### Notes
82 * This is a read-only property, and can be altered by [changeKernel].
83 *
84 * A number of kernel signals are proxied through the session from
85 * whatever the current kernel is for convenience.
86 */
87 readonly kernel: Kernel.IKernelConnection | null;
88 /**
89 * Change the session path.
90 *
91 * @param path - The new session path.
92 *
93 * @returns A promise that resolves when the session has renamed.
94 *
95 * #### Notes
96 * This uses the Jupyter REST API, and the response is validated.
97 * The promise is fulfilled on a valid response and rejected otherwise.
98 */
99 setPath(path: string): Promise<void>;
100 /**
101 * Change the session name.
102 *
103 * @returns A promise that resolves when the session has renamed.
104 *
105 * #### Notes
106 * This uses the Jupyter REST API, and the response is validated.
107 * The promise is fulfilled on a valid response and rejected otherwise.
108 */
109 setName(name: string): Promise<void>;
110 /**
111 * Change the session type.
112 *
113 * @returns A promise that resolves when the session has renamed.
114 *
115 * #### Notes
116 * This uses the Jupyter REST API, and the response is validated.
117 * The promise is fulfilled on a valid response and rejected otherwise.
118 */
119 setType(type: string): Promise<void>;
120 /**
121 * Change the kernel.
122 *
123 * @param options - The name or id of the new kernel.
124 *
125 * @returns A promise that resolves with the new kernel model.
126 *
127 * #### Notes
128 * This shuts down the existing kernel and creates a new kernel, keeping
129 * the existing session ID and path. The session assumes it owns the
130 * kernel.
131 *
132 * To start now kernel, pass an empty dictionary.
133 */
134 changeKernel(options: Partial<Kernel.IModel>): Promise<Kernel.IKernelConnection | null>;
135 /**
136 * Kill the kernel and shutdown the session.
137 *
138 * @returns A promise that resolves when the session is shut down.
139 *
140 * #### Notes
141 * This uses the Jupyter REST API, and the response is validated.
142 * The promise is fulfilled on a valid response and rejected otherwise.
143 */
144 shutdown(): Promise<void>;
145}
146export declare namespace ISessionConnection {
147 /**
148 * The session initialization options.
149 */
150 interface IOptions {
151 /**
152 * Session model.
153 */
154 model: IModel;
155 /**
156 * Connects to an existing kernel
157 */
158 connectToKernel(options: Kernel.IKernelConnection.IOptions): Kernel.IKernelConnection;
159 /**
160 * The server settings.
161 */
162 serverSettings?: ServerConnection.ISettings;
163 /**
164 * The username of the session client.
165 */
166 username?: string;
167 /**
168 * The unique identifier for the session client.
169 */
170 clientId?: string;
171 /**
172 * Kernel connection options
173 */
174 kernelConnectionOptions?: Omit<Kernel.IKernelConnection.IOptions, 'model' | 'username' | 'clientId' | 'serverSettings'>;
175 }
176 /**
177 * An arguments object for the kernel changed signal.
178 */
179 type IKernelChangedArgs = IChangedArgs<Kernel.IKernelConnection | null, Kernel.IKernelConnection | null, 'kernel'>;
180}
181/**
182 * Object which manages session instances.
183 *
184 * #### Notes
185 * The manager is responsible for maintaining the state of running
186 * sessions.
187 */
188export interface IManager extends IDisposable {
189 /**
190 * A signal emitted when the running sessions change.
191 */
192 runningChanged: ISignal<this, IModel[]>;
193 /**
194 * A signal emitted when there is a connection failure.
195 */
196 connectionFailure: ISignal<IManager, ServerConnection.NetworkError>;
197 /**
198 * The server settings for the manager.
199 */
200 serverSettings?: ServerConnection.ISettings;
201 /**
202 * Test whether the manager is ready.
203 */
204 readonly isReady: boolean;
205 /**
206 * A promise that is fulfilled when the manager is ready.
207 */
208 readonly ready: Promise<void>;
209 /**
210 * Create an iterator over the known running sessions.
211 *
212 * @returns A new iterator over the running sessions.
213 */
214 running(): IterableIterator<IModel>;
215 /**
216 * Start a new session.
217 *
218 * @param createOptions - Options for creating the session
219 *
220 * @param connectOptions - Options for connecting to the session
221 *
222 * @returns A promise that resolves with a session connection instance.
223 *
224 * #### Notes
225 * The `serverSettings` and `connectToKernel` options of the manager will be used.
226 */
227 startNew(createOptions: ISessionOptions, connectOptions?: Omit<ISessionConnection.IOptions, 'model' | 'connectToKernel' | 'serverSettings'>): Promise<ISessionConnection>;
228 /**
229 * Find a session by id.
230 *
231 * @param id - The id of the target session.
232 *
233 * @returns A promise that resolves with the session's model.
234 */
235 findById(id: string): Promise<IModel | undefined>;
236 /**
237 * Find a session by path.
238 *
239 * @param path - The path of the target session.
240 *
241 * @returns A promise that resolves with the session's model.
242 */
243 findByPath(path: string): Promise<IModel | undefined>;
244 /**
245 * Connect to a running session.
246 *
247 * @param model - The model of the target session.
248 *
249 * @param options - The session options to use.
250 *
251 * @returns The new session instance.
252 */
253 connectTo(options: Omit<ISessionConnection.IOptions, 'connectToKernel' | 'serverSettings'>): ISessionConnection;
254 /**
255 * Shut down a session by id.
256 *
257 * @param id - The id of the target kernel.
258 *
259 * @returns A promise that resolves when the operation is complete.
260 */
261 shutdown(id: string): Promise<void>;
262 /**
263 * Shut down all sessions.
264 *
265 * @returns A promise that resolves when all of the sessions are shut down.
266 */
267 shutdownAll(): Promise<void>;
268 /**
269 * Force a refresh of the running sessions.
270 *
271 * @returns A promise that resolves when the models are refreshed.
272 *
273 * #### Notes
274 * This is intended to be called only in response to a user action,
275 * since the manager maintains its internal state.
276 */
277 refreshRunning(): Promise<void>;
278 /**
279 * Find a session associated with a path and stop it is the only session
280 * using that kernel.
281 *
282 * @param path - The path in question.
283 *
284 * @returns A promise that resolves when the relevant sessions are stopped.
285 */
286 stopIfNeeded(path: string): Promise<void>;
287}
288/**
289 * The session model returned by the server.
290 *
291 * #### Notes
292 * See the [Jupyter Server API](https://petstore.swagger.io/?url=https://raw.githubusercontent.com/jupyter-server/jupyter_server/main/jupyter_server/services/api/api.yaml#!/sessions).
293 */
294export interface IModel {
295 /**
296 * The unique identifier for the session client.
297 */
298 readonly id: string;
299 readonly name: string;
300 readonly path: string;
301 readonly type: string;
302 readonly kernel: Kernel.IModel | null;
303}
304/**
305 * A session request.
306 *
307 * #### Notes
308 * The `path` and `type` session model parameters are required. The `name`
309 * parameter is not technically required, but is often assumed to be nonempty,
310 * so we require it too.
311 *
312 * See the [Jupyter Server API](https://petstore.swagger.io/?url=https://raw.githubusercontent.com/jupyter-server/jupyter_server/main/jupyter_server/services/api/api.yaml#!/sessions).
313 */
314export type ISessionOptions = Pick<IModel, 'path' | 'type' | 'name'> & {
315 kernel?: Partial<Pick<Kernel.IModel, 'name'>>;
316};