Path: blob/main/extensions/copilot/src/extension/inlineChat/vscode-node/inlineChatNotebookActions.ts
13399 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import * as l10n from '@vscode/l10n';6import * as vscode from 'vscode';7import { ILogService } from '../../../platform/log/common/logService';89const ERROR_OUTPUT_MIME_TYPE = 'application/vnd.code.notebook.error';10export class NotebookExectionStatusBarItemProvider implements vscode.NotebookCellStatusBarItemProvider {11constructor(@ILogService private readonly logService: ILogService) { }1213async provideCellStatusBarItems(14cell: vscode.NotebookCell,15token: vscode.CancellationToken16): Promise<vscode.NotebookCellStatusBarItem[]> {17// check if quickfix contributions should come from core instead of copilot18const coreQuickFix = vscode.workspace.getConfiguration('notebook').get<boolean>('cellFailureDiagnostics');19if (coreQuickFix) {20return [];21}2223if (cell.kind === vscode.NotebookCellKind.Markup) {24// show generate button for markdown cells25// only show this when `notebook.experimental.cellChat` is enabled and the cell is not empty26const enabled = vscode.workspace.getConfiguration('notebook.experimental').get<boolean>('cellChat');2728if (!enabled) {29return [];30}3132const documentContent = cell.document.getText().trim();33if (!enabled || documentContent.length === 0) {34return [];35}3637const title = l10n.t('Generate code from markdown content');38const message = documentContent;3940return [41{42text: `$(sparkle)`,43alignment: vscode.NotebookCellStatusBarAlignment.Left,44priority: Number.MAX_SAFE_INTEGER - 1,45tooltip: title,46command: {47title: title,48command: 'notebook.cell.chat.start',49arguments: [50{51index: cell.index + 1,52input: message,53autoSend: true54},55],56},57},58];59}606162const outputItem = cell.outputs63.flatMap(output => output.items)64.find(item => item.mime === ERROR_OUTPUT_MIME_TYPE);6566if (!outputItem) {67return [];68}6970type ErrorLike = Partial<Error>;7172let err: ErrorLike;73try {74const textDecoder = new TextDecoder();75err = <ErrorLike>JSON.parse(textDecoder.decode(outputItem.data));7677if (!err.name && !err.message) {78return [];79}8081const title = l10n.t('Fix using Copilot');82// remove the file and line number from the error message as they are in-memory83const joinedMessage = [err.name, err.message].filter(Boolean).join(': ').replace(/\s*\(\S+,\s*line\s*\d+\)/, '');8485return [86{87text: `$(sparkle)`,88alignment: vscode.NotebookCellStatusBarAlignment.Left,89priority: Number.MAX_SAFE_INTEGER - 1,90tooltip: title,91command: {92title: title,93command: 'vscode.editorChat.start',94arguments: [95{96autoSend: true,97message: `/fix ${joinedMessage}`,98},99],100},101}102];103} catch (e) {104this.logService.error(`Failed to parse error output ${e}`);105}106107return [];108}109}110111112