Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/src/extension/inlineChat/vscode-node/inlineChatNotebookActions.ts
13399 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import * as l10n from '@vscode/l10n';
7
import * as vscode from 'vscode';
8
import { ILogService } from '../../../platform/log/common/logService';
9
10
const ERROR_OUTPUT_MIME_TYPE = 'application/vnd.code.notebook.error';
11
export class NotebookExectionStatusBarItemProvider implements vscode.NotebookCellStatusBarItemProvider {
12
constructor(@ILogService private readonly logService: ILogService) { }
13
14
async provideCellStatusBarItems(
15
cell: vscode.NotebookCell,
16
token: vscode.CancellationToken
17
): Promise<vscode.NotebookCellStatusBarItem[]> {
18
// check if quickfix contributions should come from core instead of copilot
19
const coreQuickFix = vscode.workspace.getConfiguration('notebook').get<boolean>('cellFailureDiagnostics');
20
if (coreQuickFix) {
21
return [];
22
}
23
24
if (cell.kind === vscode.NotebookCellKind.Markup) {
25
// show generate button for markdown cells
26
// only show this when `notebook.experimental.cellChat` is enabled and the cell is not empty
27
const enabled = vscode.workspace.getConfiguration('notebook.experimental').get<boolean>('cellChat');
28
29
if (!enabled) {
30
return [];
31
}
32
33
const documentContent = cell.document.getText().trim();
34
if (!enabled || documentContent.length === 0) {
35
return [];
36
}
37
38
const title = l10n.t('Generate code from markdown content');
39
const message = documentContent;
40
41
return [
42
{
43
text: `$(sparkle)`,
44
alignment: vscode.NotebookCellStatusBarAlignment.Left,
45
priority: Number.MAX_SAFE_INTEGER - 1,
46
tooltip: title,
47
command: {
48
title: title,
49
command: 'notebook.cell.chat.start',
50
arguments: [
51
{
52
index: cell.index + 1,
53
input: message,
54
autoSend: true
55
},
56
],
57
},
58
},
59
];
60
}
61
62
63
const outputItem = cell.outputs
64
.flatMap(output => output.items)
65
.find(item => item.mime === ERROR_OUTPUT_MIME_TYPE);
66
67
if (!outputItem) {
68
return [];
69
}
70
71
type ErrorLike = Partial<Error>;
72
73
let err: ErrorLike;
74
try {
75
const textDecoder = new TextDecoder();
76
err = <ErrorLike>JSON.parse(textDecoder.decode(outputItem.data));
77
78
if (!err.name && !err.message) {
79
return [];
80
}
81
82
const title = l10n.t('Fix using Copilot');
83
// remove the file and line number from the error message as they are in-memory
84
const joinedMessage = [err.name, err.message].filter(Boolean).join(': ').replace(/\s*\(\S+,\s*line\s*\d+\)/, '');
85
86
return [
87
{
88
text: `$(sparkle)`,
89
alignment: vscode.NotebookCellStatusBarAlignment.Left,
90
priority: Number.MAX_SAFE_INTEGER - 1,
91
tooltip: title,
92
command: {
93
title: title,
94
command: 'vscode.editorChat.start',
95
arguments: [
96
{
97
autoSend: true,
98
message: `/fix ${joinedMessage}`,
99
},
100
],
101
},
102
}
103
];
104
} catch (e) {
105
this.logService.error(`Failed to parse error output ${e}`);
106
}
107
108
return [];
109
}
110
}
111
112