Skip to content

Prefer using Notebook tools over Python tools for notebooks #25098

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 21, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/client/chat/configurePythonEnvTool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { PythonExtension, ResolvedEnvironment } from '../api/types';
import { IServiceContainer } from '../ioc/types';
import { ICodeExecutionService } from '../terminals/types';
import { TerminalCodeExecutionProvider } from '../terminals/codeExecution/terminalCodeExecution';
import { getEnvironmentDetails, raceCancellationError } from './utils';
import { getEnvironmentDetails, raceCancellationError, throwIfNotebookUri } from './utils';
import { resolveFilePath } from './utils';
import { IRecommendedEnvironmentService } from '../interpreter/configuration/types';
import { ITerminalHelper } from '../common/terminal/types';
Expand Down Expand Up @@ -135,6 +135,7 @@ export class ConfigurePythonEnvTool implements LanguageModelTool<IResourceRefere
return {};
}
const resource = resolveFilePath(options.input.resourcePath);
throwIfNotebookUri(resource);
const recommededEnv = await this.recommendedEnvService.getRecommededEnvironment(resource);
// Already selected workspace env, hence nothing to do.
if (recommededEnv?.reason === 'workspaceUserSelected' && workspace.workspaceFolders?.length) {
Expand Down
3 changes: 2 additions & 1 deletion src/client/chat/getExecutableTool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { PythonExtension } from '../api/types';
import { IServiceContainer } from '../ioc/types';
import { ICodeExecutionService } from '../terminals/types';
import { TerminalCodeExecutionProvider } from '../terminals/codeExecution/terminalCodeExecution';
import { getEnvDisplayName, getEnvironmentDetails, raceCancellationError } from './utils';
import { getEnvDisplayName, getEnvironmentDetails, raceCancellationError, throwIfNotebookUri } from './utils';
import { resolveFilePath } from './utils';
import { traceError } from '../logging';
import { ITerminalHelper } from '../common/terminal/types';
Expand Down Expand Up @@ -72,6 +72,7 @@ export class GetExecutableTool implements LanguageModelTool<IResourceReference>
token: CancellationToken,
): Promise<PreparedToolInvocation> {
const resourcePath = resolveFilePath(options.input.resourcePath);
throwIfNotebookUri(resourcePath);
const envName = await raceCancellationError(getEnvDisplayName(this.discovery, resourcePath, this.api), token);
return {
invocationMessage: envName
Expand Down
6 changes: 4 additions & 2 deletions src/client/chat/getPythonEnvTool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { IServiceContainer } from '../ioc/types';
import { ICodeExecutionService } from '../terminals/types';
import { TerminalCodeExecutionProvider } from '../terminals/codeExecution/terminalCodeExecution';
import { IProcessServiceFactory, IPythonExecutionFactory } from '../common/process/types';
import { getEnvironmentDetails, raceCancellationError } from './utils';
import { getEnvironmentDetails, raceCancellationError, throwIfNotebookUri } from './utils';
import { resolveFilePath } from './utils';
import { getPythonPackagesResponse } from './listPackagesTool';
import { ITerminalHelper } from '../common/terminal/types';
Expand Down Expand Up @@ -91,9 +91,11 @@ export class GetEnvironmentInfoTool implements LanguageModelTool<IResourceRefere
}

async prepareInvocation?(
_options: LanguageModelToolInvocationPrepareOptions<IResourceReference>,
options: LanguageModelToolInvocationPrepareOptions<IResourceReference>,
_token: CancellationToken,
): Promise<PreparedToolInvocation> {
const resourcePath = resolveFilePath(options.input.resourcePath);
throwIfNotebookUri(resourcePath);
return {
invocationMessage: l10n.t('Fetching Python environment information'),
};
Expand Down
3 changes: 2 additions & 1 deletion src/client/chat/installPackagesTool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
} from 'vscode';
import { PythonExtension } from '../api/types';
import { IServiceContainer } from '../ioc/types';
import { getEnvDisplayName, raceCancellationError } from './utils';
import { getEnvDisplayName, raceCancellationError, throwIfNotebookUri } from './utils';
import { resolveFilePath } from './utils';
import { IModuleInstaller } from '../common/installer/types';
import { ModuleInstallerType } from '../pythonEnvironments/info';
Expand Down Expand Up @@ -84,6 +84,7 @@ export class InstallPackagesTool implements LanguageModelTool<IInstallPackageArg
): Promise<PreparedToolInvocation> {
const resourcePath = resolveFilePath(options.input.resourcePath);
const packageCount = options.input.packageList.length;
throwIfNotebookUri(resourcePath);

const envName = await raceCancellationError(getEnvDisplayName(this.discovery, resourcePath, this.api), token);
let title = '';
Expand Down
30 changes: 29 additions & 1 deletion src/client/chat/utils.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

import { CancellationError, CancellationToken, Uri, workspace } from 'vscode';
import { CancellationError, CancellationToken, extensions, NotebookDocument, Uri, workspace } from 'vscode';
import { IDiscoveryAPI } from '../pythonEnvironments/base/locator';
import { PythonExtension, ResolvedEnvironment } from '../api/types';
import { ITerminalHelper, TerminalShellType } from '../common/terminal/types';
import { TerminalCodeExecutionProvider } from '../terminals/codeExecution/terminalCodeExecution';
import { Conda } from '../pythonEnvironments/common/environmentManagers/conda';
import { JUPYTER_EXTENSION_ID } from '../common/constants';

export function resolveFilePath(filepath?: string): Uri | undefined {
if (!filepath) {
Expand Down Expand Up @@ -115,3 +116,30 @@ async function getCondaRunCommand(environment: ResolvedEnvironment) {
}
return { command: cmd[0], args: cmd.slice(1) };
}

export function throwIfNotebookUri(resource: Uri | undefined) {
if (!resource) {
return;
}
const notebook = workspace.notebookDocuments.find(
(doc) => doc.uri.toString() === resource.toString() || doc.uri.path === resource.path,
);
if ((notebook && isJupyterNotebook(notebook)) || resource.path.toLowerCase().endsWith('.ipynb')) {
const isJupyterExtensionAvailable = extensions.getExtension(JUPYTER_EXTENSION_ID);
if (isJupyterExtensionAvailable) {
throw new Error(
'This tool cannot be used for Jupyter Notebooks, try using notebook specific tools instead.',
);
}
throw new Error(
`This tool cannot be used for Jupyter Notebooks. Install the Jupyter Extension (${JUPYTER_EXTENSION_ID}) & try using notebook specific tools instead.`,
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rebornix From my testing this works as expected.

);
}
if (notebook) {
throw new Error('This tool cannot be used for Notebooks, try using notebook specific tools instead.');
}
}

function isJupyterNotebook(notebook: NotebookDocument) {
return notebook.notebookType === 'jupyter-notebook';
}
Loading