Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 4 additions & 0 deletions src/client/chat/configurePythonEnvTool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { TerminalCodeExecutionProvider } from '../terminals/codeExecution/termin
import {
getEnvDetailsForResponse,
getToolResponseIfNotebook,
getUntrustedWorkspaceResponse,
IResourceReference,
isCancellationError,
raceCancellationError,
Expand Down Expand Up @@ -54,6 +55,9 @@ export class ConfigurePythonEnvTool implements LanguageModelTool<IResourceRefere
options: LanguageModelToolInvocationOptions<IResourceReference>,
token: CancellationToken,
): Promise<LanguageModelToolResult> {
if (!workspace.isTrusted) {
return getUntrustedWorkspaceResponse();
}
const resource = resolveFilePath(options.input.resourcePath);
const notebookResponse = getToolResponseIfNotebook(resource);
if (notebookResponse) {
Expand Down
4 changes: 4 additions & 0 deletions src/client/chat/createVirtualEnvTool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
doesWorkspaceHaveVenvOrCondaEnv,
getDisplayVersion,
getEnvDetailsForResponse,
getUntrustedWorkspaceResponse,
IResourceReference,
isCancellationError,
raceCancellationError,
Expand Down Expand Up @@ -69,6 +70,9 @@ export class CreateVirtualEnvTool implements LanguageModelTool<ICreateVirtualEnv
options: LanguageModelToolInvocationOptions<IResourceReference>,
token: CancellationToken,
): Promise<LanguageModelToolResult> {
if (!workspace.isTrusted) {
return getUntrustedWorkspaceResponse();
}
const resource = resolveFilePath(options.input.resourcePath);
let info = await this.getPreferredEnvForCreation(resource);
if (!info) {
Expand Down
6 changes: 6 additions & 0 deletions src/client/chat/getExecutableTool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
LanguageModelToolInvocationPrepareOptions,
LanguageModelToolResult,
PreparedToolInvocation,
workspace,
} from 'vscode';
import { PythonExtension } from '../api/types';
import { IServiceContainer } from '../ioc/types';
Expand All @@ -19,6 +20,7 @@ import {
getEnvDisplayName,
getEnvironmentDetails,
getToolResponseIfNotebook,
getUntrustedWorkspaceResponse,
IResourceReference,
raceCancellationError,
} from './utils';
Expand All @@ -45,6 +47,10 @@ export class GetExecutableTool implements LanguageModelTool<IResourceReference>
options: LanguageModelToolInvocationOptions<IResourceReference>,
token: CancellationToken,
): Promise<LanguageModelToolResult> {
if (!workspace.isTrusted) {
return getUntrustedWorkspaceResponse();
}

const resourcePath = resolveFilePath(options.input.resourcePath);
const notebookResponse = getToolResponseIfNotebook(resourcePath);
if (notebookResponse) {
Expand Down
13 changes: 12 additions & 1 deletion src/client/chat/getPythonEnvTool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,20 @@ import {
LanguageModelToolInvocationPrepareOptions,
LanguageModelToolResult,
PreparedToolInvocation,
workspace,
} from 'vscode';
import { PythonExtension } from '../api/types';
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, getToolResponseIfNotebook, IResourceReference, raceCancellationError } from './utils';
import {
getEnvironmentDetails,
getToolResponseIfNotebook,
getUntrustedWorkspaceResponse,
IResourceReference,
raceCancellationError,
} from './utils';
import { resolveFilePath } from './utils';
import { getPythonPackagesResponse } from './listPackagesTool';
import { ITerminalHelper } from '../common/terminal/types';
Expand Down Expand Up @@ -44,6 +51,10 @@ export class GetEnvironmentInfoTool implements LanguageModelTool<IResourceRefere
options: LanguageModelToolInvocationOptions<IResourceReference>,
token: CancellationToken,
): Promise<LanguageModelToolResult> {
if (!workspace.isTrusted) {
return getUntrustedWorkspaceResponse();
}

const resourcePath = resolveFilePath(options.input.resourcePath);
const notebookResponse = getToolResponseIfNotebook(resourcePath);
if (notebookResponse) {
Expand Down
6 changes: 6 additions & 0 deletions src/client/chat/installPackagesTool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ import {
LanguageModelToolInvocationPrepareOptions,
LanguageModelToolResult,
PreparedToolInvocation,
workspace,
} from 'vscode';
import { PythonExtension } from '../api/types';
import { IServiceContainer } from '../ioc/types';
import {
getEnvDisplayName,
getToolResponseIfNotebook,
getUntrustedWorkspaceResponse,
IResourceReference,
isCancellationError,
isCondaEnv,
Expand All @@ -42,6 +44,10 @@ export class InstallPackagesTool implements LanguageModelTool<IInstallPackageArg
options: LanguageModelToolInvocationOptions<IInstallPackageArgs>,
token: CancellationToken,
): Promise<LanguageModelToolResult> {
if (!workspace.isTrusted) {
return getUntrustedWorkspaceResponse();
}

const resourcePath = resolveFilePath(options.input.resourcePath);
const packageCount = options.input.packageList.length;
const packagePlurality = packageCount === 1 ? 'package' : 'packages';
Expand Down
5 changes: 5 additions & 0 deletions src/client/chat/selectEnvTool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
doesWorkspaceHaveVenvOrCondaEnv,
getEnvDetailsForResponse,
getToolResponseIfNotebook,
getUntrustedWorkspaceResponse,
IResourceReference,
} from './utils';
import { resolveFilePath } from './utils';
Expand Down Expand Up @@ -61,6 +62,10 @@ export class SelectPythonEnvTool implements LanguageModelTool<ISelectPythonEnvTo
options: LanguageModelToolInvocationOptions<ISelectPythonEnvToolArguments>,
token: CancellationToken,
): Promise<LanguageModelToolResult> {
if (!workspace.isTrusted) {
return getUntrustedWorkspaceResponse();
}

const resource = resolveFilePath(options.input.resourcePath);
let selected: boolean | undefined = false;
const hasVenvOrCondaEnvInWorkspaceFolder = doesWorkspaceHaveVenvOrCondaEnv(resource, this.api);
Expand Down
7 changes: 7 additions & 0 deletions src/client/chat/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ export async function getEnvironmentDetails(
return message.join('\n');
}

export function getUntrustedWorkspaceResponse() {
return new LanguageModelToolResult([new LanguageModelTextPart('Cannot use this tool in an untrusted workspace.')]);
}

export async function getTerminalCommand(
environment: ResolvedEnvironment,
resource: Uri | undefined,
Expand Down Expand Up @@ -208,6 +212,9 @@ export async function getEnvDetailsForResponse(
resource: Uri | undefined,
token: CancellationToken,
): Promise<LanguageModelToolResult> {
if (!workspace.isTrusted) {
throw new Error('Cannot use this tool in an untrusted workspace.');
}
const envPath = api.getActiveEnvironmentPath(resource);
environment = environment || (await raceCancellationError(api.resolveEnvironment(envPath), token));
if (!environment || !environment.version) {
Expand Down