@difizen/libro-kernel 0.0.0-snapshot-20241017072317

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (188) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +1 -0
  3. package/es/basemanager.d.ts +94 -0
  4. package/es/basemanager.d.ts.map +1 -0
  5. package/es/basemanager.js +110 -0
  6. package/es/contents/contents-drive.d.ts +189 -0
  7. package/es/contents/contents-drive.d.ts.map +1 -0
  8. package/es/contents/contents-drive.js +802 -0
  9. package/es/contents/contents-manager.d.ts +229 -0
  10. package/es/contents/contents-manager.d.ts.map +1 -0
  11. package/es/contents/contents-manager.js +551 -0
  12. package/es/contents/contents-module.d.ts +3 -0
  13. package/es/contents/contents-module.d.ts.map +1 -0
  14. package/es/contents/contents-module.js +4 -0
  15. package/es/contents/contents-protocol.d.ts +487 -0
  16. package/es/contents/contents-protocol.d.ts.map +1 -0
  17. package/es/contents/contents-protocol.js +1 -0
  18. package/es/contents/index.d.ts +6 -0
  19. package/es/contents/index.d.ts.map +1 -0
  20. package/es/contents/index.js +5 -0
  21. package/es/contents/validate.d.ts +10 -0
  22. package/es/contents/validate.d.ts.map +1 -0
  23. package/es/contents/validate.js +22 -0
  24. package/es/index.d.ts +10 -0
  25. package/es/index.d.ts.map +1 -0
  26. package/es/index.js +9 -0
  27. package/es/kernel/comm.d.ts +92 -0
  28. package/es/kernel/comm.d.ts.map +1 -0
  29. package/es/kernel/comm.js +216 -0
  30. package/es/kernel/future.d.ts +178 -0
  31. package/es/kernel/future.d.ts.map +1 -0
  32. package/es/kernel/future.js +593 -0
  33. package/es/kernel/index.d.ts +8 -0
  34. package/es/kernel/index.d.ts.map +1 -0
  35. package/es/kernel/index.js +8 -0
  36. package/es/kernel/kernel-connection.d.ts +553 -0
  37. package/es/kernel/kernel-connection.d.ts.map +1 -0
  38. package/es/kernel/kernel-connection.js +1974 -0
  39. package/es/kernel/kernel-module.d.ts +3 -0
  40. package/es/kernel/kernel-module.d.ts.map +1 -0
  41. package/es/kernel/kernel-module.js +32 -0
  42. package/es/kernel/libro-kernel-manager.d.ts +89 -0
  43. package/es/kernel/libro-kernel-manager.d.ts.map +1 -0
  44. package/es/kernel/libro-kernel-manager.js +469 -0
  45. package/es/kernel/libro-kernel-protocol.d.ts +678 -0
  46. package/es/kernel/libro-kernel-protocol.d.ts.map +1 -0
  47. package/es/kernel/libro-kernel-protocol.js +60 -0
  48. package/es/kernel/libro-kernel-utils.d.ts +95 -0
  49. package/es/kernel/libro-kernel-utils.d.ts.map +1 -0
  50. package/es/kernel/libro-kernel-utils.js +130 -0
  51. package/es/kernel/libro-kernel.d.ts +14 -0
  52. package/es/kernel/libro-kernel.d.ts.map +1 -0
  53. package/es/kernel/libro-kernel.js +54 -0
  54. package/es/kernel/messages.d.ts +845 -0
  55. package/es/kernel/messages.d.ts.map +1 -0
  56. package/es/kernel/messages.js +513 -0
  57. package/es/kernel/restapi.d.ts +78 -0
  58. package/es/kernel/restapi.d.ts.map +1 -0
  59. package/es/kernel/restapi.js +372 -0
  60. package/es/kernel/serialize.d.ts +10 -0
  61. package/es/kernel/serialize.d.ts.map +1 -0
  62. package/es/kernel/serialize.js +213 -0
  63. package/es/kernel/validate.d.ts +15 -0
  64. package/es/kernel/validate.d.ts.map +1 -0
  65. package/es/kernel/validate.js +125 -0
  66. package/es/kernelspec/index.d.ts +5 -0
  67. package/es/kernelspec/index.d.ts.map +1 -0
  68. package/es/kernelspec/index.js +4 -0
  69. package/es/kernelspec/kernelspec-module.d.ts +3 -0
  70. package/es/kernelspec/kernelspec-module.d.ts.map +1 -0
  71. package/es/kernelspec/kernelspec-module.js +4 -0
  72. package/es/kernelspec/kernelspec.d.ts +33 -0
  73. package/es/kernelspec/kernelspec.d.ts.map +1 -0
  74. package/es/kernelspec/kernelspec.js +1 -0
  75. package/es/kernelspec/manager.d.ts +81 -0
  76. package/es/kernelspec/manager.d.ts.map +1 -0
  77. package/es/kernelspec/manager.js +245 -0
  78. package/es/kernelspec/restapi.d.ts +71 -0
  79. package/es/kernelspec/restapi.d.ts.map +1 -0
  80. package/es/kernelspec/restapi.js +107 -0
  81. package/es/kernelspec/validate.d.ts +10 -0
  82. package/es/kernelspec/validate.d.ts.map +1 -0
  83. package/es/kernelspec/validate.js +70 -0
  84. package/es/libro-kernel-connection-manager.d.ts +20 -0
  85. package/es/libro-kernel-connection-manager.d.ts.map +1 -0
  86. package/es/libro-kernel-connection-manager.js +146 -0
  87. package/es/module.d.ts +3 -0
  88. package/es/module.d.ts.map +1 -0
  89. package/es/module.js +9 -0
  90. package/es/page-config.d.ts +36 -0
  91. package/es/page-config.d.ts.map +1 -0
  92. package/es/page-config.js +129 -0
  93. package/es/protocol.d.ts +13 -0
  94. package/es/protocol.d.ts.map +1 -0
  95. package/es/protocol.js +8 -0
  96. package/es/server/connection-error.d.ts +36 -0
  97. package/es/server/connection-error.d.ts.map +1 -0
  98. package/es/server/connection-error.js +109 -0
  99. package/es/server/index.d.ts +6 -0
  100. package/es/server/index.d.ts.map +1 -0
  101. package/es/server/index.js +5 -0
  102. package/es/server/server-connection-protocol.d.ts +49 -0
  103. package/es/server/server-connection-protocol.d.ts.map +1 -0
  104. package/es/server/server-connection-protocol.js +0 -0
  105. package/es/server/server-connection.d.ts +25 -0
  106. package/es/server/server-connection.d.ts.map +1 -0
  107. package/es/server/server-connection.js +159 -0
  108. package/es/server/server-manager.d.ts +23 -0
  109. package/es/server/server-manager.d.ts.map +1 -0
  110. package/es/server/server-manager.js +178 -0
  111. package/es/server/server-module.d.ts +3 -0
  112. package/es/server/server-module.d.ts.map +1 -0
  113. package/es/server/server-module.js +4 -0
  114. package/es/session/index.d.ts +5 -0
  115. package/es/session/index.d.ts.map +1 -0
  116. package/es/session/index.js +4 -0
  117. package/es/session/libro-session-manager.d.ts +73 -0
  118. package/es/session/libro-session-manager.d.ts.map +1 -0
  119. package/es/session/libro-session-manager.js +568 -0
  120. package/es/session/libro-session-protocol.d.ts +50 -0
  121. package/es/session/libro-session-protocol.d.ts.map +1 -0
  122. package/es/session/libro-session-protocol.js +21 -0
  123. package/es/session/libro-session.d.ts +12 -0
  124. package/es/session/libro-session.d.ts.map +1 -0
  125. package/es/session/libro-session.js +19 -0
  126. package/es/session/restapi.d.ts +28 -0
  127. package/es/session/restapi.d.ts.map +1 -0
  128. package/es/session/restapi.js +215 -0
  129. package/es/session/session-module.d.ts +3 -0
  130. package/es/session/session-module.d.ts.map +1 -0
  131. package/es/session/session-module.js +18 -0
  132. package/es/session/validate.d.ts +14 -0
  133. package/es/session/validate.d.ts.map +1 -0
  134. package/es/session/validate.js +38 -0
  135. package/es/utils.d.ts +4 -0
  136. package/es/utils.d.ts.map +1 -0
  137. package/es/utils.js +29 -0
  138. package/es/validate-property.d.ts +2 -0
  139. package/es/validate-property.d.ts.map +1 -0
  140. package/es/validate-property.js +35 -0
  141. package/package.json +62 -0
  142. package/src/basemanager.ts +133 -0
  143. package/src/contents/contents-drive.ts +496 -0
  144. package/src/contents/contents-manager.ts +465 -0
  145. package/src/contents/contents-module.ts +6 -0
  146. package/src/contents/contents-protocol.ts +604 -0
  147. package/src/contents/index.ts +5 -0
  148. package/src/contents/validate.ts +29 -0
  149. package/src/index.spec.ts +16 -0
  150. package/src/index.tsx +9 -0
  151. package/src/kernel/comm.ts +220 -0
  152. package/src/kernel/future.ts +477 -0
  153. package/src/kernel/index.ts +7 -0
  154. package/src/kernel/kernel-connection.ts +1780 -0
  155. package/src/kernel/kernel-module.ts +50 -0
  156. package/src/kernel/libro-kernel-manager.ts +274 -0
  157. package/src/kernel/libro-kernel-protocol.ts +861 -0
  158. package/src/kernel/libro-kernel-utils.ts +152 -0
  159. package/src/kernel/libro-kernel.ts +39 -0
  160. package/src/kernel/messages.ts +1104 -0
  161. package/src/kernel/restapi.ts +183 -0
  162. package/src/kernel/serialize.ts +262 -0
  163. package/src/kernel/validate.ts +101 -0
  164. package/src/kernelspec/index.ts +5 -0
  165. package/src/kernelspec/kernelspec-module.ts +9 -0
  166. package/src/kernelspec/kernelspec.ts +37 -0
  167. package/src/kernelspec/manager.ts +162 -0
  168. package/src/kernelspec/restapi.ts +104 -0
  169. package/src/kernelspec/validate.ts +80 -0
  170. package/src/libro-kernel-connection-manager.ts +76 -0
  171. package/src/module.ts +19 -0
  172. package/src/page-config.ts +106 -0
  173. package/src/protocol.ts +24 -0
  174. package/src/server/connection-error.ts +60 -0
  175. package/src/server/index.ts +5 -0
  176. package/src/server/server-connection-protocol.ts +57 -0
  177. package/src/server/server-connection.ts +144 -0
  178. package/src/server/server-manager.ts +90 -0
  179. package/src/server/server-module.ts +9 -0
  180. package/src/session/index.ts +4 -0
  181. package/src/session/libro-session-manager.ts +406 -0
  182. package/src/session/libro-session-protocol.ts +61 -0
  183. package/src/session/libro-session.ts +33 -0
  184. package/src/session/restapi.ts +126 -0
  185. package/src/session/session-module.ts +26 -0
  186. package/src/session/validate.ts +39 -0
  187. package/src/utils.ts +28 -0
  188. package/src/validate-property.ts +38 -0
@@ -0,0 +1,861 @@
1
+ /* eslint-disable @typescript-eslint/no-namespace */
2
+ import type { JSONObject } from '@difizen/libro-common';
3
+ import type { Event, Disposable } from '@difizen/mana-app';
4
+
5
+ import type { ISpecModel } from '../kernelspec/index.js';
6
+ import type { ISettings } from '../server/index.js';
7
+
8
+ import type { LibroKernel } from './libro-kernel.js';
9
+ import type * as KernelMessage from './messages.js';
10
+
11
+ /**
12
+ * A disposable object with an observable `disposed` signal.
13
+ */
14
+ export interface ObservableDisposable extends Disposable {
15
+ /**
16
+ * A signal emitted when the object is disposed.
17
+ */
18
+ readonly onDisposed: Event<void>;
19
+ }
20
+
21
+ export type KernelStatus =
22
+ | 'unknown'
23
+ | 'starting'
24
+ | 'idle'
25
+ | 'busy'
26
+ | 'terminating'
27
+ | 'restarting'
28
+ | 'autorestarting'
29
+ | 'dead';
30
+
31
+ export interface KernelMeta {
32
+ id: KernelId;
33
+ name: string;
34
+ last_activity: string;
35
+ execution_state: KernelStatus;
36
+ connections: number;
37
+ readonly reason?: string;
38
+ readonly traceback?: string;
39
+ }
40
+
41
+ export const KernelMetaOption = Symbol('KernelMetaOption');
42
+
43
+ export const KernelId = Symbol('LibroKernelId');
44
+ export type KernelId = string;
45
+
46
+ export const LibroKernelFactory = Symbol('LibroKernelFactory');
47
+ export type LibroKernelFactory = (kernel: KernelMeta) => LibroKernel;
48
+
49
+ export const LibroKernelConnectionFactory = Symbol('LibroKernelConnectionFactory');
50
+ export type LibroKernelConnectionFactory = (
51
+ option: KernelConnectionOptions,
52
+ ) => IKernelConnection;
53
+
54
+ export type ConnectionStatus = 'connected' | 'connecting' | 'disconnected';
55
+
56
+ export class KernelError extends Error {
57
+ /**
58
+ * Exception name
59
+ */
60
+ readonly errorName: string;
61
+ /**
62
+ * Exception value
63
+ */
64
+ readonly errorValue: string;
65
+ /**
66
+ * Traceback
67
+ */
68
+ readonly traceback: string[];
69
+
70
+ /**
71
+ * Construct the kernel error.
72
+ */
73
+ constructor(content: KernelMessage.IExecuteReplyMsg['content']) {
74
+ const errorContent = content as KernelMessage.IReplyErrorContent;
75
+ const errorName = errorContent.ename;
76
+ const errorValue = errorContent.evalue;
77
+ super(`KernelReplyNotOK: ${errorName} ${errorValue}`);
78
+
79
+ this.errorName = errorName;
80
+ this.errorValue = errorValue;
81
+ this.traceback = errorContent.traceback;
82
+ Object.setPrototypeOf(this, KernelError.prototype);
83
+ }
84
+ }
85
+
86
+ export interface IFuture<
87
+ REQUEST extends KernelMessage.IShellControlMessage,
88
+ REPLY extends KernelMessage.IShellControlMessage,
89
+ > extends Disposable {
90
+ /**
91
+ * The original outgoing message.
92
+ */
93
+ readonly msg: REQUEST;
94
+
95
+ /**
96
+ * A promise that resolves when the future is done.
97
+ *
98
+ * #### Notes
99
+ * The future is done when there are no more responses expected from the
100
+ * kernel.
101
+ *
102
+ * The `done` promise resolves to the reply message.
103
+ */
104
+ readonly done: Promise<REPLY>;
105
+
106
+ /**
107
+ * The reply handler for the kernel future.
108
+ *
109
+ * #### Notes
110
+ * If the handler returns a promise, all kernel message processing pauses
111
+ * until the promise is resolved. If there is a reply message, the future
112
+ * `done` promise also resolves to the reply message after this handler has
113
+ * been called.
114
+ */
115
+ onReply: (msg: REPLY) => void | PromiseLike<void>;
116
+
117
+ /**
118
+ * The iopub handler for the kernel future.
119
+ *
120
+ * #### Notes
121
+ * If the handler returns a promise, all kernel message processing pauses
122
+ * until the promise is resolved.
123
+ */
124
+ onIOPub: (msg: KernelMessage.IIOPubMessage) => void | PromiseLike<void>;
125
+
126
+ /**
127
+ * The stdin handler for the kernel future.
128
+ *
129
+ * #### Notes
130
+ * If the handler returns a promise, all kernel message processing pauses
131
+ * until the promise is resolved.
132
+ */
133
+ onStdin: (msg: KernelMessage.IStdinMessage) => void | PromiseLike<void>;
134
+
135
+ /**
136
+ * Register hook for IOPub messages.
137
+ *
138
+ * @param hook - The callback invoked for an IOPub message.
139
+ *
140
+ * #### Notes
141
+ * The IOPub hook system allows you to preempt the handlers for IOPub
142
+ * messages handled by the future.
143
+ *
144
+ * The most recently registered hook is run first. A hook can return a
145
+ * boolean or a promise to a boolean, in which case all kernel message
146
+ * processing pauses until the promise is fulfilled. If a hook return value
147
+ * resolves to false, any later hooks will not run and the function will
148
+ * return a promise resolving to false. If a hook throws an error, the error
149
+ * is logged to the console and the next hook is run. If a hook is
150
+ * registered during the hook processing, it will not run until the next
151
+ * message. If a hook is removed during the hook processing, it will be
152
+ * deactivated immediately.
153
+ */
154
+ registerMessageHook: (
155
+ hook: (msg: KernelMessage.IIOPubMessage) => boolean | PromiseLike<boolean>,
156
+ ) => void;
157
+
158
+ /**
159
+ * Remove a hook for IOPub messages.
160
+ *
161
+ * @param hook - The hook to remove.
162
+ *
163
+ * #### Notes
164
+ * If a hook is removed during the hook processing, it will be deactivated immediately.
165
+ */
166
+ removeMessageHook: (
167
+ hook: (msg: KernelMessage.IIOPubMessage) => boolean | PromiseLike<boolean>,
168
+ ) => void;
169
+
170
+ /**
171
+ * Send an `input_reply` message.
172
+ */
173
+ sendInputReply: (
174
+ content: KernelMessage.IInputReplyMsg['content'],
175
+ parent_header: KernelMessage.IInputReplyMsg['parent_header'],
176
+ ) => void;
177
+ }
178
+
179
+ export type IShellFuture<
180
+ REQUEST extends KernelMessage.IShellMessage = KernelMessage.IShellMessage,
181
+ REPLY extends KernelMessage.IShellMessage = KernelMessage.IShellMessage,
182
+ > = IFuture<REQUEST, REPLY>;
183
+
184
+ export type IControlFuture<
185
+ REQUEST extends KernelMessage.IControlMessage = KernelMessage.IControlMessage,
186
+ REPLY extends KernelMessage.IControlMessage = KernelMessage.IControlMessage,
187
+ > = IFuture<REQUEST, REPLY>;
188
+
189
+ export interface IComm extends Disposable {
190
+ /**
191
+ * The unique id for the comm channel.
192
+ */
193
+ readonly commId: string;
194
+
195
+ /**
196
+ * The target name for the comm channel.
197
+ */
198
+ readonly targetName: string;
199
+
200
+ /**
201
+ * Callback for a comm close event.
202
+ *
203
+ * #### Notes
204
+ * This is called when the comm is closed from either the server or client.
205
+ * If this is called in response to a kernel message and the handler returns
206
+ * a promise, all kernel message processing pauses until the promise is
207
+ * resolved.
208
+ */
209
+ onClose: (msg: KernelMessage.ICommCloseMsg) => void | PromiseLike<void>;
210
+
211
+ /**
212
+ * Callback for a comm message received event.
213
+ *
214
+ * #### Notes
215
+ * If the handler returns a promise, all kernel message processing pauses
216
+ * until the promise is resolved.
217
+ */
218
+ onMsg: (msg: KernelMessage.ICommMsgMsg) => void | PromiseLike<void>;
219
+
220
+ /**
221
+ * Open a comm with optional data and metadata.
222
+ *
223
+ * @param data - The data to send to the server on opening.
224
+ *
225
+ * @param metadata - Additional metadata for the message.
226
+ *
227
+ * @returns A future for the generated message.
228
+ *
229
+ * #### Notes
230
+ * This sends a `comm_open` message to the server.
231
+ */
232
+ open: (
233
+ data?: JSONObject,
234
+ metadata?: JSONObject,
235
+ buffers?: (ArrayBuffer | ArrayBufferView)[],
236
+ ) => IShellFuture;
237
+
238
+ /**
239
+ * Send a `comm_msg` message to the kernel.
240
+ *
241
+ * @param data - The data to send to the server on opening.
242
+ *
243
+ * @param metadata - Additional metadata for the message.
244
+ *
245
+ * @param buffers - Optional buffer data.
246
+ *
247
+ * @param disposeOnDone - Whether to dispose of the future when done.
248
+ *
249
+ * @returns A future for the generated message.
250
+ *
251
+ * #### Notes
252
+ * This is a no-op if the comm has been closed.
253
+ */
254
+ send: (
255
+ data: JSONObject,
256
+ metadata?: JSONObject,
257
+ buffers?: (ArrayBuffer | ArrayBufferView)[],
258
+ disposeOnDone?: boolean,
259
+ ) => IShellFuture;
260
+
261
+ /**
262
+ * Close the comm.
263
+ *
264
+ * @param data - The data to send to the server on opening.
265
+ *
266
+ * @param metadata - Additional metadata for the message.
267
+ *
268
+ * @returns A future for the generated message.
269
+ *
270
+ * #### Notes
271
+ * This will send a `comm_close` message to the kernel, and call the
272
+ * `onClose` callback if set.
273
+ *
274
+ * This is a no-op if the comm is already closed.
275
+ */
276
+ close: (
277
+ data?: JSONObject,
278
+ metadata?: JSONObject,
279
+ buffers?: (ArrayBuffer | ArrayBufferView)[],
280
+ ) => IShellFuture;
281
+ }
282
+
283
+ export interface IAnyMessageArgs {
284
+ /**
285
+ * The message that is being signaled.
286
+ */
287
+ msg: Readonly<KernelMessage.IMessage>;
288
+
289
+ /**
290
+ * The direction of the message.
291
+ */
292
+ direction: 'send' | 'recv';
293
+ }
294
+
295
+ export interface IKernelModel {
296
+ /**
297
+ * Unique identifier of the kernel on the server.
298
+ */
299
+ readonly id: string;
300
+
301
+ /**
302
+ * The name of the kernel.
303
+ */
304
+ readonly name: string;
305
+
306
+ /**
307
+ * The kernel execution state.
308
+ */
309
+ readonly execution_state?: string;
310
+
311
+ /**
312
+ * The timestamp of the last activity on the kernel.
313
+ */
314
+ // eslint-disable-next-line camelcase
315
+ readonly last_activity?: string;
316
+
317
+ /**
318
+ * The number of active connections to the kernel.
319
+ */
320
+ readonly connections?: number;
321
+
322
+ /**
323
+ * The reason the kernel died, if applicable.
324
+ */
325
+ readonly reason?: string;
326
+
327
+ /**
328
+ * The traceback for a dead kernel, if applicable.
329
+ */
330
+ readonly traceback?: string;
331
+ }
332
+
333
+ export interface IKernelConnection extends ObservableDisposable {
334
+ /**
335
+ * The id of the server-side kernel.
336
+ */
337
+ readonly id: string;
338
+
339
+ /**
340
+ * The name of the server-side kernel.
341
+ */
342
+ readonly name: string;
343
+
344
+ /**
345
+ * The kernel model, for convenience.
346
+ */
347
+ readonly model: IKernelModel;
348
+
349
+ /**
350
+ * The client username.
351
+ */
352
+ readonly username: string;
353
+
354
+ /**
355
+ * The client unique id.
356
+ *
357
+ * #### Notes
358
+ * This should be unique for a particular kernel connection object.
359
+ */
360
+ readonly clientId: string;
361
+
362
+ /**
363
+ * The current status of the kernel.
364
+ */
365
+ readonly status: KernelMessage.Status;
366
+
367
+ /**
368
+ * The current connection status of the kernel.
369
+ */
370
+ readonly connectionStatus: ConnectionStatus;
371
+
372
+ readonly isDisposed: boolean;
373
+
374
+ /**
375
+ * The kernel info
376
+ *
377
+ * #### Notes
378
+ * This promise only resolves at startup, and is not refreshed on every
379
+ * restart.
380
+ */
381
+ readonly info: Promise<KernelMessage.IInfoReply>;
382
+
383
+ /**
384
+ * Get the kernel spec.
385
+ *
386
+ * @returns A promise that resolves with the kernel spec for this kernel.
387
+ *
388
+ * #### Notes
389
+ * This may make a server request to retrieve the spec.
390
+ */
391
+ readonly spec?: Promise<ISpecModel | undefined>;
392
+
393
+ /**
394
+ * Whether the kernel connection handles comm messages.
395
+ *
396
+ * #### Notes
397
+ * The comm message protocol currently has implicit assumptions that only
398
+ * one kernel connection is handling comm messages. This option allows a
399
+ * kernel connection to opt out of handling comms.
400
+ *
401
+ * See https://github.com/jupyter/jupyter_client/issues/263
402
+ */
403
+ handleComms: boolean;
404
+
405
+ /**
406
+ * Whether the kernel connection has pending input.
407
+ *
408
+ * #### Notes
409
+ * This is a guard to avoid deadlock is the user asks input
410
+ * as second time before submitting his first input
411
+ */
412
+ hasPendingInput: boolean;
413
+
414
+ /**
415
+ * Send a shell message to the kernel.
416
+ *
417
+ * @param msg - The fully-formed shell message to send.
418
+ *
419
+ * @param expectReply - Whether to expect a shell reply message.
420
+ *
421
+ * @param disposeOnDone - Whether to dispose of the future when done.
422
+ *
423
+ * #### Notes
424
+ * Send a message to the kernel's shell channel, yielding a future object
425
+ * for accepting replies.
426
+ *
427
+ * If `expectReply` is given and `true`, the future is done when both a
428
+ * shell reply and an idle status message are received with the appropriate
429
+ * parent header, in which case the `.done` promise resolves to the reply.
430
+ * If `expectReply` is not given or is `false`, the future is done when an
431
+ * idle status message with the appropriate parent header is received, in
432
+ * which case the `.done` promise resolves to `undefined`.
433
+ *
434
+ * If `disposeOnDone` is given and `false`, the future will not be disposed
435
+ * of when the future is done, instead relying on the caller to dispose of
436
+ * it. This allows for the handling of out-of-order output from ill-behaved
437
+ * kernels.
438
+ *
439
+ * All replies are validated as valid kernel messages.
440
+ *
441
+ * If the kernel status is `'dead'`, this will throw an error.
442
+ */
443
+ sendShellMessage: <T extends KernelMessage.ShellMessageType>(
444
+ msg: KernelMessage.IShellMessage<T>,
445
+ expectReply?: boolean,
446
+ disposeOnDone?: boolean,
447
+ ) => IShellFuture<KernelMessage.IShellMessage<T>>;
448
+
449
+ sendControlMessage: <T extends KernelMessage.ControlMessageType>(
450
+ msg: KernelMessage.IControlMessage<T>,
451
+ expectReply?: boolean,
452
+ disposeOnDone?: boolean,
453
+ ) => IControlFuture<KernelMessage.IControlMessage<T>>;
454
+
455
+ /**
456
+ * Reconnect to a disconnected kernel.
457
+ *
458
+ * @returns A promise that resolves when the kernel has reconnected.
459
+ *
460
+ * #### Notes
461
+ * This just refreshes the connection to an existing kernel, and does not
462
+ * perform an HTTP request to the server or restart the kernel.
463
+ */
464
+ reconnect: () => Promise<void>;
465
+
466
+ /**
467
+ * Interrupt a kernel.
468
+ *
469
+ * @returns A promise that resolves when the kernel has interrupted.
470
+ *
471
+ * #### Notes
472
+ * Uses the [Jupyter Notebook API](http://petstore.swagger.io/?url=https://raw.githubusercontent.com/jupyter/notebook/master/notebook/services/api/api.yaml#!/kernels).
473
+ *
474
+ * The promise is fulfilled on a valid response and rejected otherwise.
475
+ *
476
+ * It is assumed that the API call does not mutate the kernel id or name.
477
+ *
478
+ * The promise will be rejected if the kernel status is `'dead'` or if the
479
+ * request fails or the response is invalid.
480
+ */
481
+ interrupt: () => Promise<void>;
482
+
483
+ /**
484
+ * Restart a kernel.
485
+ *
486
+ * @returns A promise that resolves when the kernel has restarted.
487
+ *
488
+ * #### Notes
489
+ * Uses the [Jupyter Notebook API](http://petstore.swagger.io/?url=https://raw.githubusercontent.com/jupyter/notebook/master/notebook/services/api/api.yaml#!/kernels) and validates the response model.
490
+ *
491
+ * Any existing Future or Comm objects are cleared.
492
+ *
493
+ * It is assumed that the API call does not mutate the kernel id or name.
494
+ *
495
+ * The promise will be rejected if the kernel status is `'dead'` or if the
496
+ * request fails or the response is invalid.
497
+ */
498
+ restart: () => Promise<void>;
499
+
500
+ /**
501
+ * Send a `kernel_info_request` message.
502
+ *
503
+ * @param content - The content of the request.
504
+ *
505
+ * @returns A promise that resolves with the response message.
506
+ *
507
+ * #### Notes
508
+ * See [Messaging in Jupyter](https://jupyter-client.readthedocs.io/en/latest/messaging.html#kernel-info).
509
+ *
510
+ * Fulfills with the `kernel_info_response` content when the shell reply is
511
+ * received and validated.
512
+ */
513
+ requestKernelInfo?: () => Promise<KernelMessage.IInfoReplyMsg | undefined>;
514
+
515
+ /**
516
+ * Send a `complete_request` message.
517
+ *
518
+ * @param content - The content of the request.
519
+ *
520
+ * @returns A promise that resolves with the response message.
521
+ *
522
+ * #### Notes
523
+ * See [Messaging in Jupyter](https://jupyter-client.readthedocs.io/en/latest/messaging.html#completion).
524
+ *
525
+ * Fulfills with the `complete_reply` content when the shell reply is
526
+ * received and validated.
527
+ */
528
+ requestComplete: (
529
+ content: KernelMessage.ICompleteRequestMsg['content'],
530
+ ) => Promise<KernelMessage.ICompleteReplyMsg>;
531
+
532
+ /**
533
+ * Send an `inspect_request` message.
534
+ *
535
+ * @param content - The content of the request.
536
+ *
537
+ * @returns A promise that resolves with the response message.
538
+ *
539
+ * #### Notes
540
+ * See [Messaging in Jupyter](https://jupyter-client.readthedocs.io/en/latest/messaging.html#introspection).
541
+ *
542
+ * Fulfills with the `inspect_reply` content when the shell reply is
543
+ * received and validated.
544
+ */
545
+ requestInspect: (
546
+ content: KernelMessage.IInspectRequestMsg['content'],
547
+ ) => Promise<KernelMessage.IInspectReplyMsg>;
548
+
549
+ /**
550
+ * Send a `history_request` message.
551
+ *
552
+ * @param content - The content of the request.
553
+ *
554
+ * @returns A promise that resolves with the response message.
555
+ *
556
+ * #### Notes
557
+ * See [Messaging in Jupyter](https://jupyter-client.readthedocs.io/en/latest/messaging.html#history).
558
+ *
559
+ * Fulfills with the `history_reply` content when the shell reply is
560
+ * received and validated.
561
+ */
562
+ requestHistory: (
563
+ content: KernelMessage.IHistoryRequestMsg['content'],
564
+ ) => Promise<KernelMessage.IHistoryReplyMsg>;
565
+
566
+ /**
567
+ * Send an `execute_request` message.
568
+ *
569
+ * @param content - The content of the request.
570
+ *
571
+ * @param disposeOnDone - Whether to dispose of the future when done.
572
+ *
573
+ * @returns A kernel future.
574
+ *
575
+ * #### Notes
576
+ * See [Messaging in
577
+ * Jupyter](https://jupyter-client.readthedocs.io/en/latest/messaging.html#execute).
578
+ *
579
+ * This method returns a kernel future, rather than a promise, since execution may
580
+ * have many response messages (for example, many iopub display messages).
581
+ *
582
+ * Future `onReply` is called with the `execute_reply` content when the
583
+ * shell reply is received and validated.
584
+ *
585
+ * **See also:** [[IExecuteReply]]
586
+ */
587
+ requestExecute: (
588
+ content: KernelMessage.IExecuteRequestMsg['content'],
589
+ disposeOnDone?: boolean,
590
+ metadata?: JSONObject,
591
+ ) => IShellFuture<KernelMessage.IExecuteRequestMsg, KernelMessage.IExecuteReplyMsg>;
592
+
593
+ /**
594
+ * Send an experimental `debug_request` message.
595
+ *
596
+ * @hidden
597
+ *
598
+ * @param content - The content of the request.
599
+ *
600
+ * @param disposeOnDone - Whether to dispose of the future when done.
601
+ *
602
+ * @returns A kernel future.
603
+ *
604
+ * #### Notes
605
+ * Debug messages are experimental messages that are not in the official
606
+ * kernel message specification. As such, this function is *NOT* considered
607
+ * part of the public API, and may change without notice.
608
+ */
609
+ requestDebug: (
610
+ content: KernelMessage.IDebugRequestMsg['content'],
611
+ disposeOnDone?: boolean,
612
+ ) => IControlFuture<KernelMessage.IDebugRequestMsg, KernelMessage.IDebugReplyMsg>;
613
+
614
+ /**
615
+ * Send an `is_complete_request` message.
616
+ *
617
+ * @param content - The content of the request.
618
+ *
619
+ * @returns A promise that resolves with the response message.
620
+ *
621
+ * #### Notes
622
+ * See [Messaging in Jupyter](https://jupyter-client.readthedocs.io/en/latest/messaging.html#code-completeness).
623
+ *
624
+ * Fulfills with the `is_complete_response` content when the shell reply is
625
+ * received and validated.
626
+ */
627
+ requestIsComplete: (
628
+ content: KernelMessage.IIsCompleteRequestMsg['content'],
629
+ ) => Promise<KernelMessage.IIsCompleteReplyMsg>;
630
+
631
+ /**
632
+ * Send a `comm_info_request` message.
633
+ *
634
+ * @param content - The content of the request.
635
+ *
636
+ * @returns A promise that resolves with the response message.
637
+ *
638
+ * #### Notes
639
+ * See [Messaging in Jupyter](https://jupyter-client.readthedocs.io/en/latest/messaging.html#comm_info).
640
+ *
641
+ * Fulfills with the `comm_info_reply` content when the shell reply is
642
+ * received and validated.
643
+ */
644
+ requestCommInfo: (
645
+ content: KernelMessage.ICommInfoRequestMsg['content'],
646
+ ) => Promise<KernelMessage.ICommInfoReplyMsg>;
647
+
648
+ /**
649
+ * Send an `input_reply` message.
650
+ *
651
+ * @param content - The content of the reply.
652
+ *
653
+ * #### Notes
654
+ * See [Messaging in Jupyter](https://jupyter-client.readthedocs.io/en/latest/messaging.html#messages-on-the-stdin-router-dealer-sockets).
655
+ */
656
+ sendInputReply: (
657
+ content: KernelMessage.IInputReplyMsg['content'],
658
+ parent_header: KernelMessage.IInputReplyMsg['parent_header'],
659
+ ) => void;
660
+
661
+ /**
662
+ * Create a new comm.
663
+ *
664
+ * @param targetName - The name of the comm target.
665
+ *
666
+ * @param id - The comm id.
667
+ *
668
+ * @returns A comm instance.
669
+ */
670
+ createComm: (targetName: string, commId?: string) => IComm;
671
+
672
+ /**
673
+ * Check if a comm exists.
674
+ */
675
+ hasComm: (commId: string) => boolean;
676
+
677
+ /**
678
+ * Register a comm target handler.
679
+ *
680
+ * @param targetName - The name of the comm target.
681
+ *
682
+ * @param callback - The callback invoked for a comm open message.
683
+ *
684
+ * #### Notes
685
+ * Only one comm target can be registered to a target name at a time, an
686
+ * existing callback for the same target name will be overridden. A registered
687
+ * comm target handler will take precedence over a comm which specifies a
688
+ * `target_module`.
689
+ *
690
+ * If the callback returns a promise, kernel message processing will pause
691
+ * until the returned promise is fulfilled.
692
+ */
693
+ registerCommTarget: (
694
+ targetName: string,
695
+ callback: (
696
+ comm: IComm,
697
+ msg: KernelMessage.ICommOpenMsg,
698
+ ) => void | PromiseLike<void>,
699
+ ) => void;
700
+
701
+ /**
702
+ * Remove a comm target handler.
703
+ *
704
+ * @param targetName - The name of the comm target to remove.
705
+ *
706
+ * @param callback - The callback to remove.
707
+ *
708
+ * #### Notes
709
+ * The comm target is only removed if it matches the callback argument.
710
+ */
711
+ removeCommTarget: (
712
+ targetName: string,
713
+ callback: (
714
+ comm: IComm,
715
+ msg: KernelMessage.ICommOpenMsg,
716
+ ) => void | PromiseLike<void>,
717
+ ) => void;
718
+
719
+ /**
720
+ * Register an IOPub message hook.
721
+ *
722
+ * @param msg_id - The parent_header message id in messages the hook should
723
+ * intercept.
724
+ *
725
+ * @param hook - The callback invoked for the message.
726
+ *
727
+ * #### Notes
728
+ * The IOPub hook system allows you to preempt the handlers for IOPub
729
+ * messages with a given parent_header message id. The most recently
730
+ * registered hook is run first. If a hook return value resolves to false,
731
+ * any later hooks and the future's onIOPub handler will not run. If a hook
732
+ * throws an error, the error is logged to the console and the next hook is
733
+ * run. If a hook is registered during the hook processing, it will not run
734
+ * until the next message. If a hook is disposed during the hook processing,
735
+ * it will be deactivated immediately.
736
+ *
737
+ * See also [[IFuture.registerMessageHook]].
738
+ */
739
+ registerMessageHook: (
740
+ msgId: string,
741
+ hook: (msg: KernelMessage.IIOPubMessage) => boolean | PromiseLike<boolean>,
742
+ ) => Disposable;
743
+
744
+ /**
745
+ * Remove an IOPub message hook.
746
+ *
747
+ * @param msg_id - The parent_header message id the hook intercepted.
748
+ *
749
+ * @param hook - The callback invoked for the message.
750
+ *
751
+ */
752
+ removeMessageHook: (
753
+ msgId: string,
754
+ hook: (msg: KernelMessage.IIOPubMessage) => boolean | PromiseLike<boolean>,
755
+ ) => void;
756
+
757
+ /**
758
+ * Remove the input guard, if any.
759
+ */
760
+ removeInputGuard: () => void;
761
+
762
+ /**
763
+ * A signal emitted when the kernel status changes.
764
+ */
765
+ statusChanged: Event<KernelMessage.Status>;
766
+
767
+ /**
768
+ * A signal emitted when the kernel connection status changes.
769
+ */
770
+ connectionStatusChanged: Event<ConnectionStatus>;
771
+
772
+ /**
773
+ * A signal emitted after an iopub kernel message is handled.
774
+ */
775
+ iopubMessage: Event<KernelMessage.IIOPubMessage>;
776
+
777
+ futureMessage: Event<KernelMessage.IMessage<KernelMessage.MessageType>>;
778
+ /**
779
+ * A signal emitted for unhandled non-iopub kernel messages that claimed to
780
+ * be responses for messages we sent using this kernel object.
781
+ */
782
+ unhandledMessage: Event<KernelMessage.IMessage>;
783
+
784
+ /**
785
+ * A signal emitted when any kernel message is sent or received.
786
+ *
787
+ * #### Notes
788
+ * This signal is emitted before any message handling has happened. The
789
+ * message should be treated as read-only.
790
+ */
791
+ anyMessage: Event<IAnyMessageArgs>;
792
+
793
+ /**
794
+ * A signal emitted when a kernel has pending inputs from the user.
795
+ */
796
+ pendingInput: Event<boolean>;
797
+
798
+ /**
799
+ * The server settings for the kernel.
800
+ */
801
+ readonly serverSettings: ISettings;
802
+
803
+ /**
804
+ * Shutdown a kernel.
805
+ *
806
+ * @returns A promise that resolves when the kernel has shut down.
807
+ *
808
+ * #### Notes
809
+ * Uses the [Jupyter Notebook
810
+ * API](http://petstore.swagger.io/?url=https://raw.githubusercontent.com/jupyter/notebook/master/notebook/services/api/api.yaml#!/kernels).
811
+ *
812
+ * On a valid response, closes the websocket, disposes of the kernel
813
+ * object, and fulfills the promise.
814
+ *
815
+ * The promise will be rejected if the kernel status is `'dead'`, the
816
+ * request fails, or the response is invalid.
817
+ */
818
+ shutdown: () => Promise<void>;
819
+
820
+ /**
821
+ * Clone the current kernel with a new clientId.
822
+ */
823
+ clone: (
824
+ options?: Pick<KernelConnectionOptions, 'clientId' | 'username' | 'handleComms'>,
825
+ ) => IKernelConnection;
826
+ }
827
+
828
+ export const KernelConnectionOptions = Symbol('IOptions');
829
+ export interface KernelConnectionOptions {
830
+ /**
831
+ * The kernel model.
832
+ */
833
+ model: IKernelModel;
834
+
835
+ /**
836
+ * The server settings for the kernel.
837
+ */
838
+ serverSettings?: Partial<ISettings>;
839
+
840
+ /**
841
+ * The username of the kernel client.
842
+ */
843
+ username?: string;
844
+
845
+ /**
846
+ * Whether the kernel connection should handle comm messages
847
+ *
848
+ * #### Notes
849
+ * The comm message protocol currently has implicit assumptions that only
850
+ * one kernel connection is handling comm messages. This option allows a
851
+ * kernel connection to opt out of handling comms.
852
+ *
853
+ * See https://github.com/jupyter/jupyter_client/issues/263
854
+ */
855
+ handleComms?: boolean;
856
+
857
+ /**
858
+ * The unique identifier for the kernel client.
859
+ */
860
+ clientId?: string;
861
+ }