UNPKG

18.7 kBTypeScriptView Raw
1import { IChangedArgs } from '@jupyterlab/coreutils';
2import { Kernel, KernelMessage, KernelSpec, Session } from '@jupyterlab/services';
3import { ITranslator } from '@jupyterlab/translation';
4import { IDisposable, IObservableDisposable } from '@lumino/disposable';
5import { ISignal } from '@lumino/signaling';
6/**
7 * A context object to manage a widget's kernel session connection.
8 *
9 * #### Notes
10 * The current session connection is `.session`, the current session's kernel
11 * connection is `.session.kernel`. For convenience, we proxy several kernel
12 * connection and session connection signals up to the session context so
13 * that you do not have to manage slots as sessions and kernels change. For
14 * example, to act on whatever the current kernel's iopubMessage signal is
15 * producing, connect to the session context `.iopubMessage` signal.
16 *
17 */
18export interface ISessionContext extends IObservableDisposable {
19 /**
20 * The current session connection.
21 */
22 session: Session.ISessionConnection | null;
23 /**
24 * Initialize the session context.
25 *
26 * @returns A promise that resolves with whether to ask the user to select a kernel.
27 *
28 * #### Notes
29 * This includes starting up an initial kernel if needed.
30 */
31 initialize(): Promise<boolean>;
32 /**
33 * Whether the session context is ready.
34 */
35 readonly isReady: boolean;
36 /**
37 * Whether the session context is terminating.
38 */
39 readonly isTerminating: boolean;
40 /**
41 * Whether the session context is restarting.
42 */
43 readonly isRestarting: boolean;
44 /**
45 * A promise that is fulfilled when the session context is ready.
46 */
47 readonly ready: Promise<void>;
48 /**
49 * A signal emitted when the session connection changes.
50 */
51 readonly sessionChanged: ISignal<this, IChangedArgs<Session.ISessionConnection | null, Session.ISessionConnection | null, 'session'>>;
52 /**
53 * A signal emitted when the kernel changes, proxied from the session connection.
54 */
55 readonly kernelChanged: ISignal<this, IChangedArgs<Kernel.IKernelConnection | null, Kernel.IKernelConnection | null, 'kernel'>>;
56 /**
57 * Signal emitted if the kernel preference changes.
58 */
59 readonly kernelPreferenceChanged: ISignal<this, IChangedArgs<ISessionContext.IKernelPreference>>;
60 /**
61 * A signal emitted when the kernel status changes, proxied from the session connection.
62 */
63 readonly statusChanged: ISignal<this, Kernel.Status>;
64 /**
65 * A signal emitted when the kernel connection status changes, proxied from the session connection.
66 */
67 readonly connectionStatusChanged: ISignal<this, Kernel.ConnectionStatus>;
68 /**
69 * A flag indicating if session is has pending input, proxied from the session connection.
70 */
71 readonly pendingInput: boolean;
72 /**
73 * A signal emitted for a kernel messages, proxied from the session connection.
74 */
75 readonly iopubMessage: ISignal<this, KernelMessage.IMessage>;
76 /**
77 * A signal emitted for an unhandled kernel message, proxied from the session connection.
78 */
79 readonly unhandledMessage: ISignal<this, KernelMessage.IMessage>;
80 /**
81 * A signal emitted when a session property changes, proxied from the session connection.
82 */
83 readonly propertyChanged: ISignal<this, 'path' | 'name' | 'type'>;
84 /**
85 * The kernel preference for starting new kernels.
86 */
87 kernelPreference: ISessionContext.IKernelPreference;
88 /**
89 * Whether the kernel is "No Kernel" or not.
90 *
91 * #### Notes
92 * As the displayed name is translated, this can be used directly.
93 */
94 readonly hasNoKernel: boolean;
95 /**
96 * The sensible display name for the kernel, or translated "No Kernel"
97 *
98 * #### Notes
99 * This is at this level since the underlying kernel connection does not
100 * have access to the kernel spec manager.
101 */
102 readonly kernelDisplayName: string;
103 /**
104 * A sensible status to display
105 *
106 * #### Notes
107 * This combines the status and connection status into a single status for the user.
108 */
109 readonly kernelDisplayStatus: ISessionContext.KernelDisplayStatus;
110 /**
111 * The session path.
112 *
113 * #### Notes
114 * Typically `.session.path` should be used. This attribute is useful if
115 * there is no current session.
116 */
117 readonly path: string;
118 /**
119 * The session type.
120 *
121 * #### Notes
122 * Typically `.session.type` should be used. This attribute is useful if
123 * there is no current session.
124 */
125 readonly type: string;
126 /**
127 * The session name.
128 *
129 * #### Notes
130 * Typically `.session.name` should be used. This attribute is useful if
131 * there is no current session.
132 */
133 readonly name: string;
134 /**
135 * The previous kernel name.
136 */
137 readonly prevKernelName: string;
138 /**
139 * The session manager used by the session.
140 */
141 readonly sessionManager: Session.IManager;
142 /**
143 * The kernel spec manager
144 */
145 readonly specsManager: KernelSpec.IManager;
146 /**
147 * Starts new Kernel.
148 *
149 * @returns Whether to ask the user to pick a kernel.
150 */
151 startKernel(): Promise<boolean>;
152 /**
153 * Restart the current Kernel.
154 *
155 * @returns A promise that resolves when the kernel is restarted.
156 */
157 restartKernel(): Promise<void>;
158 /**
159 * Kill the kernel and shutdown the session.
160 *
161 * @returns A promise that resolves when the session is shut down.
162 */
163 shutdown(): Promise<void>;
164 /**
165 * Change the kernel associated with the session.
166 *
167 * @param options The optional kernel model parameters to use for the new kernel.
168 *
169 * @returns A promise that resolves with the new kernel connection.
170 */
171 changeKernel(options?: Partial<Kernel.IModel>): Promise<Kernel.IKernelConnection | null>;
172}
173/**
174 * The namespace for session context related interfaces.
175 */
176export declare namespace ISessionContext {
177 /**
178 * A kernel preference.
179 *
180 * #### Notes
181 * Preferences for a kernel are considered in the order `id`, `name`,
182 * `language`. If no matching kernels can be found and `autoStartDefault` is
183 * `true`, then the default kernel for the server is preferred.
184 */
185 interface IKernelPreference {
186 /**
187 * The name of the kernel.
188 */
189 readonly name?: string;
190 /**
191 * The preferred kernel language.
192 */
193 readonly language?: string;
194 /**
195 * The id of an existing kernel.
196 */
197 readonly id?: string;
198 /**
199 * A kernel should be started automatically (default `true`).
200 */
201 readonly shouldStart?: boolean;
202 /**
203 * A kernel can be started (default `true`).
204 */
205 readonly canStart?: boolean;
206 /**
207 * Shut down the session when session context is disposed (default `false`).
208 */
209 readonly shutdownOnDispose?: boolean;
210 /**
211 * Automatically start the default kernel if no other matching kernel is
212 * found (default `false`).
213 */
214 readonly autoStartDefault?: boolean;
215 }
216 type KernelDisplayStatus = Kernel.Status | Kernel.ConnectionStatus | 'initializing' | '';
217 /**
218 * An interface for a session context dialog provider.
219 */
220 interface IDialogs {
221 /**
222 * Select a kernel for the session.
223 */
224 selectKernel(session: ISessionContext): Promise<void>;
225 /**
226 * Restart the session context.
227 *
228 * @returns A promise that resolves with whether the kernel has restarted.
229 *
230 * #### Notes
231 * If there is a running kernel, present a dialog.
232 * If there is no kernel, we start a kernel with the last run
233 * kernel name and resolves with `true`. If no kernel has been started,
234 * this is a no-op, and resolves with `false`.
235 */
236 restart(session: ISessionContext): Promise<boolean>;
237 }
238 /**
239 * Session context dialog options
240 */
241 interface IDialogsOptions {
242 /**
243 * Application translator object
244 */
245 translator?: ITranslator;
246 }
247}
248/**
249 * The default implementation for a session context object.
250 */
251export declare class SessionContext implements ISessionContext {
252 /**
253 * Construct a new session context.
254 */
255 constructor(options: SessionContext.IOptions);
256 /**
257 * The current session connection.
258 */
259 get session(): Session.ISessionConnection | null;
260 /**
261 * The session path.
262 *
263 * #### Notes
264 * Typically `.session.path` should be used. This attribute is useful if
265 * there is no current session.
266 */
267 get path(): string;
268 /**
269 * The session type.
270 *
271 * #### Notes
272 * Typically `.session.type` should be used. This attribute is useful if
273 * there is no current session.
274 */
275 get type(): string;
276 /**
277 * The session name.
278 *
279 * #### Notes
280 * Typically `.session.name` should be used. This attribute is useful if
281 * there is no current session.
282 */
283 get name(): string;
284 /**
285 * A signal emitted when the kernel connection changes, proxied from the session connection.
286 */
287 get kernelChanged(): ISignal<this, Session.ISessionConnection.IKernelChangedArgs>;
288 /**
289 * A signal emitted when the session connection changes.
290 */
291 get sessionChanged(): ISignal<this, IChangedArgs<Session.ISessionConnection | null, Session.ISessionConnection | null, 'session'>>;
292 /**
293 * A signal emitted when the kernel status changes, proxied from the kernel.
294 */
295 get statusChanged(): ISignal<this, Kernel.Status>;
296 /**
297 * A flag indicating if the session has ending input, proxied from the kernel.
298 */
299 get pendingInput(): boolean;
300 /**
301 * A signal emitted when the kernel status changes, proxied from the kernel.
302 */
303 get connectionStatusChanged(): ISignal<this, Kernel.ConnectionStatus>;
304 /**
305 * A signal emitted for iopub kernel messages, proxied from the kernel.
306 */
307 get iopubMessage(): ISignal<this, KernelMessage.IIOPubMessage>;
308 /**
309 * A signal emitted for an unhandled kernel message, proxied from the kernel.
310 */
311 get unhandledMessage(): ISignal<this, KernelMessage.IMessage>;
312 /**
313 * A signal emitted when a session property changes, proxied from the current session.
314 */
315 get propertyChanged(): ISignal<this, 'path' | 'name' | 'type'>;
316 /**
317 * The kernel preference of this client session.
318 *
319 * This is used when selecting a new kernel, and should reflect the sort of
320 * kernel the activity prefers.
321 */
322 get kernelPreference(): ISessionContext.IKernelPreference;
323 set kernelPreference(value: ISessionContext.IKernelPreference);
324 /**
325 * Signal emitted if the kernel preference changes.
326 */
327 get kernelPreferenceChanged(): ISignal<this, IChangedArgs<ISessionContext.IKernelPreference>>;
328 /**
329 * Whether the context is ready.
330 */
331 get isReady(): boolean;
332 /**
333 * A promise that is fulfilled when the context is ready.
334 */
335 get ready(): Promise<void>;
336 /**
337 * Whether the context is terminating.
338 */
339 get isTerminating(): boolean;
340 /**
341 * Whether the context is restarting.
342 */
343 get isRestarting(): boolean;
344 /**
345 * The session manager used by the session.
346 */
347 readonly sessionManager: Session.IManager;
348 /**
349 * The kernel spec manager
350 */
351 readonly specsManager: KernelSpec.IManager;
352 /**
353 * Whether the kernel is "No Kernel" or not.
354 *
355 * #### Notes
356 * As the displayed name is translated, this can be used directly.
357 */
358 get hasNoKernel(): boolean;
359 /**
360 * The display name of the current kernel, or a sensible alternative.
361 *
362 * #### Notes
363 * This is a convenience function to have a consistent sensible name for the
364 * kernel.
365 */
366 get kernelDisplayName(): string;
367 /**
368 * A sensible status to display
369 *
370 * #### Notes
371 * This combines the status and connection status into a single status for
372 * the user.
373 */
374 get kernelDisplayStatus(): ISessionContext.KernelDisplayStatus;
375 /**
376 * The name of the previously started kernel.
377 */
378 get prevKernelName(): string;
379 /**
380 * Test whether the context is disposed.
381 */
382 get isDisposed(): boolean;
383 /**
384 * A signal emitted when the poll is disposed.
385 */
386 get disposed(): ISignal<this, void>;
387 /**
388 * Get the constant displayed name for "No Kernel"
389 */
390 protected get noKernelName(): string;
391 /**
392 * Dispose of the resources held by the context.
393 */
394 dispose(): void;
395 /**
396 * Starts new Kernel.
397 *
398 * @returns Whether to ask the user to pick a kernel.
399 */
400 startKernel(): Promise<boolean>;
401 /**
402 * Restart the current Kernel.
403 *
404 * @returns A promise that resolves when the kernel is restarted.
405 */
406 restartKernel(): Promise<void>;
407 /**
408 * Change the current kernel associated with the session.
409 */
410 changeKernel(options?: Partial<Kernel.IModel>): Promise<Kernel.IKernelConnection | null>;
411 /**
412 * Kill the kernel and shutdown the session.
413 *
414 * @returns A promise that resolves when the session is shut down.
415 */
416 shutdown(): Promise<void>;
417 /**
418 * Initialize the session context
419 *
420 * @returns A promise that resolves with whether to ask the user to select a kernel.
421 *
422 * #### Notes
423 * If a server session exists on the current path, we will connect to it.
424 * If preferences include disabling `canStart` or `shouldStart`, no
425 * server session will be started.
426 * If a kernel id is given, we attempt to start a session with that id.
427 * If a default kernel is available, we connect to it.
428 * Otherwise we ask the user to select a kernel.
429 */
430 initialize(): Promise<boolean>;
431 /**
432 * Inner initialize function that doesn't handle promises.
433 * This makes it easier to consolidate promise handling logic.
434 */
435 _initialize(): Promise<boolean>;
436 /**
437 * Shut down the current session.
438 */
439 private _shutdownSession;
440 /**
441 * Start the session if necessary.
442 *
443 * @returns Whether to ask the user to pick a kernel.
444 */
445 private _startIfNecessary;
446 /**
447 * Change the kernel.
448 */
449 private _changeKernel;
450 /**
451 * Handle a new session object.
452 */
453 private _handleNewSession;
454 /**
455 * Handle an error in session startup.
456 */
457 private _handleSessionError;
458 /**
459 * Display kernel error
460 */
461 private _displayKernelError;
462 /**
463 * Handle a session termination.
464 */
465 private _onSessionDisposed;
466 /**
467 * Handle a change to a session property.
468 */
469 private _onPropertyChanged;
470 /**
471 * Handle a change to the kernel.
472 */
473 private _onKernelChanged;
474 /**
475 * Handle a change to the session status.
476 */
477 private _onStatusChanged;
478 /**
479 * Handle a change to the session status.
480 */
481 private _onConnectionStatusChanged;
482 /**
483 * Handle a change to the pending input.
484 */
485 private _onPendingInput;
486 /**
487 * Handle an iopub message.
488 */
489 private _onIopubMessage;
490 /**
491 * Handle an unhandled message.
492 */
493 private _onUnhandledMessage;
494 private _path;
495 private _name;
496 private _type;
497 private _prevKernelName;
498 private _kernelPreference;
499 private _isDisposed;
500 private _disposed;
501 private _session;
502 private _ready;
503 private _initializing;
504 private _initStarted;
505 private _initPromise;
506 private _isReady;
507 private _isTerminating;
508 private _isRestarting;
509 private _kernelChanged;
510 private _preferenceChanged;
511 private _sessionChanged;
512 private _statusChanged;
513 private _connectionStatusChanged;
514 private translator;
515 private _trans;
516 private _pendingInput;
517 private _iopubMessage;
518 private _unhandledMessage;
519 private _propertyChanged;
520 private _dialog;
521 private _setBusy;
522 private _busyDisposable;
523 private _pendingKernelName;
524 private _pendingSessionRequest;
525}
526/**
527 * A namespace for `SessionContext` statics.
528 */
529export declare namespace SessionContext {
530 /**
531 * The options used to initialize a context.
532 */
533 interface IOptions {
534 /**
535 * A session manager instance.
536 */
537 sessionManager: Session.IManager;
538 /**
539 * A kernel spec manager instance.
540 */
541 specsManager: KernelSpec.IManager;
542 /**
543 * The initial path of the file.
544 */
545 path?: string;
546 /**
547 * The name of the session.
548 */
549 name?: string;
550 /**
551 * The type of the session.
552 */
553 type?: string;
554 /**
555 * A kernel preference.
556 */
557 kernelPreference?: ISessionContext.IKernelPreference;
558 /**
559 * The application language translator.
560 */
561 translator?: ITranslator;
562 /**
563 * A function to call when the session becomes busy.
564 */
565 setBusy?: () => IDisposable;
566 }
567 /**
568 * An interface for populating a kernel selector.
569 */
570 interface IKernelSearch {
571 /**
572 * The Kernel specs.
573 */
574 specs: KernelSpec.ISpecModels | null;
575 /**
576 * The kernel preference.
577 */
578 preference: ISessionContext.IKernelPreference;
579 /**
580 * The current running sessions.
581 */
582 sessions?: Iterable<Session.IModel>;
583 }
584 /**
585 * Get the default kernel name given select options.
586 */
587 function getDefaultKernel(options: IKernelSearch): string | null;
588}
589/**
590 * The default implementation of the client session dialog provider.
591 */
592export declare class SessionContextDialogs implements ISessionContext.IDialogs {
593 constructor(options?: ISessionContext.IDialogsOptions);
594 /**
595 * Select a kernel for the session.
596 */
597 selectKernel(sessionContext: ISessionContext): Promise<void>;
598 /**
599 * Restart the session.
600 *
601 * @returns A promise that resolves with whether the kernel has restarted.
602 *
603 * #### Notes
604 * If there is a running kernel, present a dialog.
605 * If there is no kernel, we start a kernel with the last run
606 * kernel name and resolves with `true`.
607 */
608 restart(sessionContext: ISessionContext): Promise<boolean>;
609 private _translator;
610}