UNPKG

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