-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
Copy pathsession.ts
395 lines (347 loc) · 9.78 KB
/
session.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
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(): IterableIterator<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 Server API](https://petstore.swagger.io/?url=https://raw.githubusercontent.com/jupyter-server/jupyter_server/main/jupyter_server/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 Server API](https://petstore.swagger.io/?url=https://raw.githubusercontent.com/jupyter-server/jupyter_server/main/jupyter_server/services/api/api.yaml#!/sessions).
*/
export type ISessionOptions = Pick<IModel, 'path' | 'type' | 'name'> & {
kernel?: Partial<Pick<Kernel.IModel, 'name'>>;
};