-
Notifications
You must be signed in to change notification settings - Fork 1
/
kernel.ts
417 lines (378 loc) · 19.1 KB
/
kernel.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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
/* eslint-disable @typescript-eslint/naming-convention */
import { NotebookDocument, NotebookCell, NotebookController, NotebookCellOutput, NotebookCellOutputItem, NotebookRange, NotebookEdit, WorkspaceEdit, workspace } from 'vscode';
import { processCellsRust } from "./languages/rust";
import { processCellsGo } from "./languages/go";
import { processCellsJavascript } from "./languages/javascript";
import { processCellsTypescript } from "./languages/typescript";
import { ChildProcessWithoutNullStreams, spawnSync } from 'child_process';
import { processShell as processShell } from './languages/shell';
import { processCellsPython } from './languages/python';
import * as vscode from 'vscode';
import { processCellsMojo } from './languages/mojo';
import { getOpenAIKey, getOpenAIModel, getOpenAIOrgID, getGroqAIKey } from "./config"
import { Cell, ChatMessage, ChatRequest, CommentDecorator } from "./types"
import { commandNotOnPath, post } from './utils';
export let lastRunLanguage = '';
// Kernel in this case matches Jupyter definition i.e. this is responsible for taking the frontend notebook
// and running it through different languages, then returning results in the same format.
export class Kernel {
async executeCells(doc: NotebookDocument, cells: NotebookCell[], ctrl: NotebookController): Promise<void> {
for (const cell of cells) {
await this.executeCell(doc, [cell], ctrl)
}
}
async executeCell(doc: NotebookDocument, cells: NotebookCell[], ctrl: NotebookController): Promise<void> {
let decoder = new TextDecoder;
let encoder = new TextEncoder;
let exec = ctrl.createNotebookCellExecution(cells[0]);
let currentCell = cells[cells.length - 1];
// Allow for the ability to cancel execution
let token = exec.token;
token.onCancellationRequested(() => {
exec.end(false, (new Date).getTime());
});
// Used for the cell timer counter
exec.start((new Date).getTime());
// TODO check lang and change comment symbols
if (currentCell.document.getText().trimStart().startsWith("#" + CommentDecorator.skip)) {
exec.end(true, (new Date).getTime());
return
}
exec.clearOutput(cells[0]);
// Get all cells up to this one
let range = new NotebookRange(0, cells[0].index + 1);
let cellsUpToCurrent = doc.getCells(range);
// Build a object containing languages and their cells
let cellsStripped: Cell[] = [];
let matchingCells = 0;
let pythonMatchingCells = 0;
let pythonCells: Cell[] = [];
for (const cell of cellsUpToCurrent) {
if (cell.document.languageId === cells[0].document.languageId) {
matchingCells++;
cellsStripped.push({
index: matchingCells,
contents: cell.document.getText(),
cell: cell,
});
}
// Also capture python cells if they exist when running Mojo
if (cells[0].document.languageId === "mojo") {
if (cell.document.languageId === "python") {
pythonMatchingCells++
pythonCells.push({
index: pythonMatchingCells,
contents: cell.document.getText(),
cell: cell,
});
}
}
}
// Get language that was used to run this cell
const lang = cells[0].document.languageId;
// Check if clearing output at the end
let clearOutput = false;
// AI Model related, generates new code blocks, may expand this later
if (lang === "llama3-8b") {
lastRunLanguage = "llama3-8b";
const url = 'https://api.groq.com/openai/v1/chat/completions';
const headers = {
'Content-Type': 'application/json',
'Authorization': `Bearer ${getGroqAIKey()}`,
}
const messages: ChatMessage[] = [{ "role": "user", "content": "You're generating codeblocks to help users solve programming problems, make sure that you put the name of the language in the markdown blocks like ```python" }]
for (const message of cellsStripped) {
messages.push({ role: "user", content: message.contents });
}
const data: ChatRequest = {
"model": "llama3-8b-8192",
messages
};
let body = JSON.stringify(data);
let result = await post(url, headers, body)
if (!result) {
exec.end(false, (new Date).getTime());
return
}
vscode.window.showInformationMessage(body)
let text = result.choices[0].message.content;
let code_blocks = text.split("```");
let edits: vscode.NotebookCellData[] = [];
for (let [i, block] of code_blocks.entries()) {
// If there was any text after split, get the type of language
if (block[0] != "\n" && i != 0) {
let language = block.split("\n")[0]
block = block.substring(language.length);
let blockTrimmed = block.trim().replace("\n\n", "\n");
if (blockTrimmed !== "") {
edits.push(new vscode.NotebookCellData(vscode.NotebookCellKind.Code, blockTrimmed, language));
}
}
else {
let blockTrimmed = block.trim().replace("\n\n", "");
if (blockTrimmed !== "") {
edits.push(new vscode.NotebookCellData(vscode.NotebookCellKind.Markup, blockTrimmed, "markdown"));
}
}
}
const edit = new WorkspaceEdit();
let notebook_edit = NotebookEdit.insertCells(cells[0].index + 1, edits);
edit.set(cells[0].notebook.uri, [notebook_edit]);
workspace.applyEdit(edit);
exec.end(true, (new Date).getTime());
}
else if (lang === "openai") {
lastRunLanguage = "openai";
const url = 'https://api.openai.com/v1/chat/completions';
const headers = {
'Content-Type': 'application/json',
'Authorization': `Bearer ${getOpenAIKey()}`,
};
let orgId = getOpenAIOrgID()
let model = getOpenAIModel() || "couldn't get model"
if (orgId) {
headers['OpenAI-Organization'] = orgId
}
const messages: ChatMessage[] = [{ role: "system", content: "You are a helpful bot named mdl, that generates concise code blocks to solve programming problems" }];
for (const message of cellsStripped) {
messages.push({ role: "user", content: message.contents });
}
const data: ChatRequest = {
model,
messages
};
let body = JSON.stringify(data);
let result = await post(url, headers, body)
if (!result) {
exec.end(false, (new Date).getTime());
return
}
let text = result.choices[0].message.content;
let code_blocks = text.split("```");
let edits: vscode.NotebookCellData[] = [];
for (let [i, block] of code_blocks.entries()) {
// If there was any text after split, get the type of language
if (block[0] != "\n" && i != 0) {
let language = block.split("\n")[0]
block = block.substring(language.length);
let blockTrimmed = block.trim().replace("\n\n", "\n");
if (blockTrimmed !== "") {
edits.push(new vscode.NotebookCellData(vscode.NotebookCellKind.Code, blockTrimmed, language));
}
}
else {
let blockTrimmed = block.trim().replace("\n\n", "");
if (blockTrimmed !== "") {
edits.push(new vscode.NotebookCellData(vscode.NotebookCellKind.Markup, blockTrimmed, "markdown"));
}
}
}
const edit = new WorkspaceEdit();
let notebook_edit = NotebookEdit.insertCells(cells[0].index + 1, edits);
edit.set(cells[0].notebook.uri, [notebook_edit]);
workspace.applyEdit(edit);
exec.end(true, (new Date).getTime());
// Normal language related execution
} else {
let output: ChildProcessWithoutNullStreams;
// Now there's an output stream, kill that as well on cancel request
token.onCancellationRequested(() => {
output.kill();
exec.end(false, (new Date).getTime());
});
const mimeType = `text/plain`;
switch (lang) {
case "mojo":
if (commandNotOnPath('mojo', "https://modular.com/mojo")) {
exec.end(false, (new Date).getTime());
return
}
lastRunLanguage = "mojo";
let mojoResult = processCellsMojo(cellsStripped, pythonCells);
output = mojoResult.stream
clearOutput = mojoResult.clearOutput
break;
case "rust":
if (commandNotOnPath('cargo', "https://rustup.rs")) {
exec.end(false, (new Date).getTime());
return
}
lastRunLanguage = "rust";
output = processCellsRust(cellsStripped);
break;
case "go":
if (commandNotOnPath("go", "https://go.dev/doc/install")) {
exec.end(false, (new Date).getTime());
return
}
lastRunLanguage = "go";
output = processCellsGo(cellsStripped);
break;
case "python":
let command = "python3"
if (commandNotOnPath(command, "")) {
if (commandNotOnPath("python", "https://www.python.org/downloads/")) {
exec.end(false, (new Date).getTime());
return
}
command = "python"
}
lastRunLanguage = "python";
let pyResult = processCellsPython(cellsStripped, command);
output = pyResult.stream
clearOutput = pyResult.clearOutput
break;
case "javascript":
if (commandNotOnPath("node", "https://nodejs.org/en/download/package-manager")) {
exec.end(false, (new Date).getTime());
return
}
lastRunLanguage = "javascript";
output = processCellsJavascript(cellsStripped);
break;
case "typescript":
let esr = spawnSync("esr");
if (esr.stdout === null) {
let response = encoder.encode("To make TypeScript run fast install esr globally:\nnpm install -g esbuild-runner");
const x = new NotebookCellOutputItem(response, mimeType);
exec.appendOutput([new NotebookCellOutput([x])], cells[0]);
exec.end(false, (new Date).getTime());
return;
}
lastRunLanguage = "typescript";
output = processCellsTypescript(cellsStripped);
break;
case "bash":
if (commandNotOnPath("bash", "https://hackernoon.com/how-to-install-bash-on-windows-10-lqb73yj3")) {
exec.end(false, (new Date).getTime());
return
}
lastRunLanguage = "shell";
var result = processShell(currentCell, "bash");
output = result.stream
clearOutput = result.clearOutput
break;
case "zsh":
if (commandNotOnPath("zsh", "https://github.com/ohmyzsh/ohmyzsh/wiki/Installing-ZSH")) {
exec.end(false, (new Date).getTime());
return
}
lastRunLanguage = "shell";
var result = processShell(currentCell, "zsh");
output = result.stream
clearOutput = result.clearOutput
break;
case "fish":
if (commandNotOnPath("fish", "https://fishshell.com/")) {
exec.end(false, (new Date).getTime());
return
}
lastRunLanguage = "shell";
var result = processShell(currentCell, "fish");
output = result.stream
clearOutput = result.clearOutput
break;
case "nushell":
if (commandNotOnPath("nushell", "https://www.nushell.sh/book/installation.html")) {
exec.end(false, (new Date).getTime());
return
}
lastRunLanguage = "shell";
var result = processShell(currentCell, "nushell");
output = result.stream
clearOutput = result.clearOutput
break;
case "shellscript":
if (commandNotOnPath("bash", "https://hackernoon.com/how-to-install-bash-on-windows-10-lqb73yj3")) {
exec.end(false, (new Date).getTime());
return
}
lastRunLanguage = "shell";
var result = processShell(currentCell, "bash");
output = result.stream
clearOutput = result.clearOutput
break;
default:
exec.end(true, (new Date).getTime());
return;
}
let errorText = "";
output.stderr.on("data", async (data: Uint8Array) => {
errorText = data.toString();
if (errorText) {
exec.appendOutput([new NotebookCellOutput([NotebookCellOutputItem.text(errorText, mimeType)])]);
}
});
let buf = Buffer.from([]);
let currentCellLang = cellsStripped[cellsStripped.length - 1] as Cell;
output.stdout.on('data', (data: Uint8Array) => {
let arr = [buf, data];
buf = Buffer.concat(arr);
let outputs = decoder.decode(buf).split(/!!output-start-cell[\n,""," "]/g);
let currentCellOutput: string
if (lastRunLanguage == "shell") {
currentCellOutput = outputs[1]
} else {
currentCellOutput = outputs[currentCellLang.index + pythonCells.length];
}
if (!clearOutput && currentCellOutput.trim()) {
exec.replaceOutput([new NotebookCellOutput([NotebookCellOutputItem.text(currentCellOutput)])]);
}
});
output.on('close', (_) => {
// If stdout returned anything consider it a success
if (buf.length === 0) {
exec.end(false, (new Date).getTime());
} else {
exec.end(true, (new Date).getTime());
}
// Loop through all the cells and increment version of image if it exists
if (doc.getCells().length >= (cells[0].index + 1)) {
let cell = doc.getCells(new NotebookRange(cells[0].index + 1, cells[0].index + 2))[0]
if (cell.kind === vscode.NotebookCellKind.Markup) {
let text = cell.document.getText();
text.replace(/(.*[^`]*<img\s*src\s*=\s*".*?)(\?version=(\d+))?"(.*)/g, (match, prefix, versionQuery, versionNum, suffix) => {
if (match) {
let replaceText = ""
if (versionQuery) {
// If ?version= is present, increment the version number
let newVersionNum = parseInt(versionNum, 10) + 1;
replaceText = `${prefix}?version=${newVersionNum}"${suffix}`;
} else {
// If ?version= is not present, add ?version=1
replaceText = `${prefix}?version=1"${suffix}`;
}
let workspaceEdit = new vscode.WorkspaceEdit();
let fullRange = new vscode.Range(
0,
0,
cell.document.lineCount - 1,
cell.document.lineAt(cell.document.lineCount - 1).text.length
);
workspaceEdit.replace(cell.document.uri, fullRange, replaceText);
vscode.workspace.applyEdit(workspaceEdit);
vscode.window.showNotebookDocument(vscode.window.activeNotebookEditor?.notebook as NotebookDocument, {
viewColumn: vscode.window.activeNotebookEditor?.viewColumn,
selections: [new NotebookRange(cell.index, cell.index + 1)],
preserveFocus: true,
}).then(() => {
// Execute commands to toggle cell edit mode and then toggle it back to preview.
vscode.commands.executeCommand('notebook.cell.edit').then(() => {
vscode.commands.executeCommand('notebook.cell.quitEdit').then(() => {
// Optionally, add any additional logic that needs to run after the refresh.
});
});
});
vscode.window.showNotebookDocument(vscode.window.activeNotebookEditor?.notebook as NotebookDocument, {
viewColumn: vscode.window.activeNotebookEditor?.viewColumn,
selections: [new NotebookRange(cell.index - 1, cell.index)],
preserveFocus: false,
})
}
});
}
}
});
}
}
}