|
1 | 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
2 | 2 | // Licensed under the MIT License. |
3 | 3 |
|
4 | | -import { CancellationError, CancellationToken, extensions, NotebookDocument, Uri, workspace } from 'vscode'; |
| 4 | +import { |
| 5 | + CancellationError, |
| 6 | + CancellationToken, |
| 7 | + extensions, |
| 8 | + LanguageModelTextPart, |
| 9 | + LanguageModelToolResult, |
| 10 | + NotebookDocument, |
| 11 | + Uri, |
| 12 | + workspace, |
| 13 | +} from 'vscode'; |
5 | 14 | import { IDiscoveryAPI } from '../pythonEnvironments/base/locator'; |
6 | 15 | import { PythonExtension, ResolvedEnvironment } from '../api/types'; |
7 | 16 | import { ITerminalHelper, TerminalShellType } from '../common/terminal/types'; |
@@ -117,27 +126,28 @@ async function getCondaRunCommand(environment: ResolvedEnvironment) { |
117 | 126 | return { command: cmd[0], args: cmd.slice(1) }; |
118 | 127 | } |
119 | 128 |
|
120 | | -export function throwIfNotebookUri(resource: Uri | undefined) { |
| 129 | +export function getToolResponseIfNotebook(resource: Uri | undefined) { |
121 | 130 | if (!resource) { |
122 | 131 | return; |
123 | 132 | } |
124 | 133 | const notebook = workspace.notebookDocuments.find( |
125 | 134 | (doc) => doc.uri.toString() === resource.toString() || doc.uri.path === resource.path, |
126 | 135 | ); |
| 136 | + let message = 'This tool cannot be used for Notebooks, try using notebook specific tools instead.'; |
127 | 137 | if ((notebook && isJupyterNotebook(notebook)) || resource.path.toLowerCase().endsWith('.ipynb')) { |
128 | 138 | const isJupyterExtensionAvailable = extensions.getExtension(JUPYTER_EXTENSION_ID); |
129 | 139 | if (isJupyterExtensionAvailable) { |
130 | | - throw new Error( |
131 | | - 'This tool cannot be used for Jupyter Notebooks, try using notebook specific tools instead.', |
132 | | - ); |
| 140 | + message = `This tool cannot be used for Jupyter Notebooks, try using notebook specific tools instead.`; |
| 141 | + } else { |
| 142 | + message = [ |
| 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'); |
133 | 148 | } |
134 | | - throw new Error( |
135 | | - `This tool cannot be used for Jupyter Notebooks. Install the Jupyter Extension (${JUPYTER_EXTENSION_ID}) & try using notebook specific tools instead.`, |
136 | | - ); |
137 | | - } |
138 | | - if (notebook) { |
139 | | - throw new Error('This tool cannot be used for Notebooks, try using notebook specific tools instead.'); |
140 | 149 | } |
| 150 | + return new LanguageModelToolResult([new LanguageModelTextPart(message)]); |
141 | 151 | } |
142 | 152 |
|
143 | 153 | function isJupyterNotebook(notebook: NotebookDocument) { |
|
0 commit comments