/
types.ts
158 lines (149 loc) · 6.67 KB
/
types.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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
'use strict';
import type { KernelMessage, Session } from '@jupyterlab/services';
import type { Observable } from 'rxjs/Observable';
import type { CancellationToken, Event, QuickPickItem, Uri } from 'vscode';
import { NotebookCell, NotebookDocument } from '../../../../../types/vscode-proposed';
import type { ServerStatus } from '../../../../datascience-ui/interactive-common/mainState';
import type { IAsyncDisposable, Resource } from '../../../common/types';
import type { PythonEnvironment } from '../../../pythonEnvironments/info';
import type {
IJupyterKernel,
IJupyterKernelSpec,
IJupyterSessionManager,
InterruptResult,
KernelSocketInformation
} from '../../types';
export type LiveKernelModel = IJupyterKernel & Partial<IJupyterKernelSpec> & { session: Session.IModel };
/**
* Connection metadata for Live Kernels.
* With this we are able connect to an existing kernel (instead of starting a new session).
*/
export type LiveKernelConnectionMetadata = {
kernelModel: LiveKernelModel;
/**
* Python interpreter will be used for intellisense & the like.
*/
interpreter?: PythonEnvironment;
kind: 'connectToLiveKernel';
};
/**
* Connection metadata for Kernels started using kernelspec (JSON).
* This could be a raw kernel (spec might have path to executable for .NET or the like).
* If the executable is not defined in kernelspec json, & it is a Python kernel, then we'll use the provided python interpreter.
*/
export type KernelSpecConnectionMetadata = {
kernelModel?: undefined;
kernelSpec: IJupyterKernelSpec;
/**
* Indicates the interpreter that may be used to start the kernel.
* If possible to start a kernel without this Python interpreter, then this Python interpreter will be used for intellisense & the like.
* This interpreter could also be the interpreter associated with the kernel spec that we are supposed to start.
*/
interpreter?: PythonEnvironment;
kind: 'startUsingKernelSpec';
};
/**
* Connection metadata for Kernels started using default kernel.
* Here we tell Jupyter to start a session and let it decide what kernel is to be started.
* (could apply to either local or remote sessions when dealing with Jupyter Servers).
*/
export type DefaultKernelConnectionMetadata = {
/**
* This will be empty as we do not have a kernel spec.
* Left for type compatibility with other types that have kernel spec property.
*/
kernelSpec?: IJupyterKernelSpec;
/**
* Python interpreter will be used for intellisense & the like.
*/
interpreter?: PythonEnvironment;
kind: 'startUsingDefaultKernel';
};
/**
* Connection metadata for Kernels started using Python interpreter.
* These are not necessarily raw (it could be plain old Jupyter Kernels, where we register Python interpreter as a kernel).
* We can have KernelSpec information here as well, however that is totally optional.
* We will always start this kernel using old Jupyter style (provided we first register this intrepreter as a kernel) or raw.
*/
export type PythonKernelConnectionMetadata = {
kernelSpec?: IJupyterKernelSpec;
interpreter: PythonEnvironment;
kind: 'startUsingPythonInterpreter';
};
export type KernelConnectionMetadata =
| LiveKernelConnectionMetadata
| KernelSpecConnectionMetadata
| PythonKernelConnectionMetadata
| DefaultKernelConnectionMetadata;
/**
* Returns a string that can be used to uniquely identify a Kernel Connection.
*/
export function getKernelConnectionId(kernelConnection: KernelConnectionMetadata) {
switch (kernelConnection.kind) {
case 'connectToLiveKernel':
return `${kernelConnection.kind}#${kernelConnection.kernelModel.name}.${kernelConnection.kernelModel.session.id}.${kernelConnection.kernelModel.session.name}`;
case 'startUsingDefaultKernel':
return `${kernelConnection.kind}#${kernelConnection}`;
case 'startUsingKernelSpec':
return `${kernelConnection.kind}#${kernelConnection.kernelSpec.name}.${kernelConnection.kernelSpec.display_name}`;
case 'startUsingPythonInterpreter':
return `${kernelConnection.kind}#${kernelConnection.interpreter.path}`;
default:
throw new Error(`Unsupported Kernel Connection ${kernelConnection}`);
}
}
export interface IKernelSpecQuickPickItem<T extends KernelConnectionMetadata = KernelConnectionMetadata>
extends QuickPickItem {
selection: T;
}
export interface IKernelSelectionListProvider<T extends KernelConnectionMetadata = KernelConnectionMetadata> {
getKernelSelections(resource: Resource, cancelToken?: CancellationToken): Promise<IKernelSpecQuickPickItem<T>[]>;
}
export interface IKernelSelectionUsage {
/**
* Given a kernel selection, this method will attempt to use that kernel and return the corresponding Interpreter, Kernel Spec and the like.
* This method will also check if required dependencies are installed or not, and will install them if required.
*/
useSelectedKernel(
selection: KernelConnectionMetadata,
resource: Resource,
type: 'raw' | 'jupyter' | 'noConnection',
session?: IJupyterSessionManager,
cancelToken?: CancellationToken
): Promise<KernelConnectionMetadata | undefined>;
}
export interface IKernel extends IAsyncDisposable {
readonly uri: Uri;
readonly metadata: Readonly<KernelConnectionMetadata>;
readonly onStatusChanged: Event<ServerStatus>;
readonly onDisposed: Event<void>;
readonly onRestarted: Event<void>;
readonly status: ServerStatus;
readonly disposed: boolean;
/**
* Kernel information, used to save in ipynb in the metadata.
* Crucial for non-python notebooks, else we save the incorrect information.
*/
readonly info?: KernelMessage.IInfoReplyMsg['content'];
readonly kernelSocket: Observable<KernelSocketInformation | undefined>;
start(): Promise<void>;
interrupt(): Promise<InterruptResult>;
restart(): Promise<void>;
executeCell(cell: NotebookCell): Promise<void>;
executeAllCells(document: NotebookDocument): Promise<void>;
}
export type KernelOptions = { metadata: KernelConnectionMetadata };
export const IKernelProvider = Symbol('IKernelProvider');
export interface IKernelProvider {
/**
* Get hold of the active kernel for a given Uri (Notebook or other file).
*/
get(uri: Uri): IKernel | undefined;
/**
* Gets or creates a kernel for a given Uri.
* WARNING: If called with different options for same Uri, old kernel associated with the Uri will be disposed.
*/
getOrCreate(uri: Uri, options: KernelOptions): IKernel | undefined;
}