forked from jupyterlab/jupyterlab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kernelconnector.ts
77 lines (64 loc) · 1.9 KB
/
kernelconnector.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
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
import { IClientSession } from '@jupyterlab/apputils';
import { DataConnector } from '@jupyterlab/coreutils';
import { KernelMessage } from '@jupyterlab/services';
import { InspectionHandler } from './handler';
/**
* The default connector for making inspection requests from the Jupyter API.
*/
export class KernelConnector extends DataConnector<
InspectionHandler.IReply,
void,
InspectionHandler.IRequest
> {
/**
* Create a new kernel connector for inspection requests.
*
* @param options - The instatiation options for the kernel connector.
*/
constructor(options: KernelConnector.IOptions) {
super();
this._session = options.session;
}
/**
* Fetch inspection requests.
*
* @param request - The inspection request text and details.
*/
fetch(
request: InspectionHandler.IRequest
): Promise<InspectionHandler.IReply> {
const kernel = this._session.kernel;
if (!kernel) {
return Promise.reject(new Error('Inspection fetch requires a kernel.'));
}
const contents: KernelMessage.IInspectRequestMsg['content'] = {
code: request.text,
cursor_pos: request.offset,
detail_level: 0
};
return kernel.requestInspect(contents).then(msg => {
const response = msg.content;
if (response.status !== 'ok' || !response.found) {
throw new Error('Inspection fetch failed to return successfully.');
}
return { data: response.data, metadata: response.metadata };
});
}
private _session: IClientSession;
}
/**
* A namespace for kernel connector statics.
*/
export namespace KernelConnector {
/**
* The instantiation options for an inspection handler.
*/
export interface IOptions {
/**
* The session used to make API requests to the kernel.
*/
session: IClientSession;
}
}