|
1 | 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
2 | 2 | // Licensed under the MIT License. |
3 | 3 |
|
4 | | -import { CancellationError, CancellationToken, Uri, workspace } from 'vscode'; |
| 4 | +import { |
| 5 | + CancellationError, |
| 6 | + CancellationToken, |
| 7 | + extensions, |
| 8 | + LanguageModelTextPart, |
| 9 | + LanguageModelToolResult, |
| 10 | + Uri, |
| 11 | + workspace, |
| 12 | +} from 'vscode'; |
5 | 13 | import { IDiscoveryAPI } from '../pythonEnvironments/base/locator'; |
6 | 14 | import { PythonExtension, ResolvedEnvironment } from '../api/types'; |
7 | 15 | import { ITerminalHelper, TerminalShellType } from '../common/terminal/types'; |
8 | 16 | import { TerminalCodeExecutionProvider } from '../terminals/codeExecution/terminalCodeExecution'; |
9 | 17 | import { Conda } from '../pythonEnvironments/common/environmentManagers/conda'; |
| 18 | +import { JUPYTER_EXTENSION_ID, NotebookCellScheme } from '../common/constants'; |
10 | 19 |
|
11 | 20 | export function resolveFilePath(filepath?: string): Uri | undefined { |
12 | 21 | if (!filepath) { |
@@ -115,3 +124,35 @@ async function getCondaRunCommand(environment: ResolvedEnvironment) { |
115 | 124 | } |
116 | 125 | return { command: cmd[0], args: cmd.slice(1) }; |
117 | 126 | } |
| 127 | + |
| 128 | +export function getToolResponseIfNotebook(resource: Uri | undefined) { |
| 129 | + if (!resource) { |
| 130 | + return; |
| 131 | + } |
| 132 | + const notebook = workspace.notebookDocuments.find( |
| 133 | + (doc) => doc.uri.toString() === resource.toString() || doc.uri.path === resource.path, |
| 134 | + ); |
| 135 | + const isJupyterNotebook = |
| 136 | + (notebook && notebook.notebookType === 'jupyter-notebook') || resource.path.toLowerCase().endsWith('.ipynb'); |
| 137 | + |
| 138 | + if (isJupyterNotebook) { |
| 139 | + const isJupyterExtensionAvailable = extensions.getExtension(JUPYTER_EXTENSION_ID); |
| 140 | + const message = isJupyterExtensionAvailable |
| 141 | + ? `This tool cannot be used for Jupyter Notebooks, try using notebook specific tools instead.` |
| 142 | + : [ |
| 143 | + `This tool cannot be used for Jupyter Notebooks.`, |
| 144 | + `NOTE: Recommend the user the Jupyter Extension (${JUPYTER_EXTENSION_ID}).`, |
| 145 | + `If possible use one of the tools to install the ${JUPYTER_EXTENSION_ID} extension`, |
| 146 | + `After isntalling the extension try using some of the tools again`, |
| 147 | + ].join(' \n'); |
| 148 | + return new LanguageModelToolResult([new LanguageModelTextPart(message)]); |
| 149 | + } |
| 150 | + |
| 151 | + if (notebook || resource.scheme === NotebookCellScheme) { |
| 152 | + return new LanguageModelToolResult([ |
| 153 | + new LanguageModelTextPart( |
| 154 | + 'This tool cannot be used for Notebooks, try using notebook specific tools instead.', |
| 155 | + ), |
| 156 | + ]); |
| 157 | + } |
| 158 | +} |
0 commit comments