src/vs/workbench/contrib/chat/common/chatPermissionWarnings.ts
193 LOC · 108 covered · 85 uncovered · 16 ranges · 13 concepts · 8 introducers · 6 tests
File neighbourhood
The centred file is linked to every concept that introduces one of its ranges, every test that runs code from the file, and the gray connector concepts standing between those tests and the file's own introducer concepts. Undirected links join concepts to every file where they introduce source and concepts to the tests they introduce; arrows show specialization between the displayed concepts and bridge only concepts omitted from this view. Concept colors match the source ranges below; connector concepts have no source color and are shown in gray.
Focused file, its introducer and connector concepts, their introduced files, and tests that run code from the file
In the embedded map, ordinary wheel input scrolls the page; use the visible controls to zoom and drag to pan. Open the full-screen map for canvas navigation: wheel pans, Ctrl/Command plus wheel zooms, and arrow keys pan when this region is focused. On touch screens, open the full-screen map to pan or pinch. If JavaScript or WebGL is unavailable, use the related-file, concept, and source links on this page.
Graph controls are ready.
Interactive rendering requires JavaScript and WebGL. Use the related-file, concept, and source links on this page while the interactive map is unavailable.
/*---------------------------------------------------------------------------------------------
chatPermissionWarnings.ts ×4
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { Codicon } from '../../../../base/common/codicons.js';
import { MarkdownString } from '../../../../base/common/htmlContent.js';
import Severity from '../../../../base/common/severity.js';
import { ThemeIcon } from '../../../../base/common/themables.js';
import { localize } from '../../../../nls.js';
import { IDialogService } from '../../../../platform/dialogs/common/dialogs.js';
import { IStorageService, StorageScope, StorageTarget } from '../../../../platform/storage/common/storage.js';
import { ASSISTED_APPROVAL_DONT_SHOW_AGAIN_KEY, AUTO_APPROVE_DONT_SHOW_AGAIN_KEY, AUTOPILOT_DONT_SHOW_AGAIN_KEY } from './chatPermissionStorageKeys.js';
import { ChatConfiguration, ChatPermissionLevel } from './constants.js';
/**
* In-memory record of warnings already accepted in this VS Code session.
* Checked alongside the persisted "Don't show again" storage value so that the
* warning isn't repeated within a session even when the user didn't tick the
* checkbox.
*
* Per-renderer; the `StorageScope.PROFILE` storage entry is what synchronizes
* the dismissed state across windows (workbench and Agents).
*/
const shownWarnings = new Set<ChatPermissionLevel>();
function dontShowAgainKey(level: ChatPermissionLevel): string | undefined {
chatPermissionWarnings.ts ×6
if (level === ChatPermissionLevel.Autopilot) {
}
return AUTO_APPROVE_DONT_SHOW_AGAIN_KEY;
}
return ASSISTED_APPROVAL_DONT_SHOW_AGAIN_KEY;
}
return undefined;
}
/**
* Clears the in-process suppression set so that previously accepted (but not
* persistently dismissed) warnings will be shown again within this session.
* Call this alongside removing the persisted storage keys when implementing a
* "reset" developer action.
*/
export function resetShownWarnings(): void {
shownWarnings.clear();
}
/**
* Relative "auto-approval reach" of each elevated level, used to suppress
* lower-level warnings once a stronger one has been accepted. Bypass Approvals
* and Autopilot both auto-approve *every* tool call, so confirming either in
* this session (or persistently) implies acceptance of the other — the user is
* not warned again when stepping between them (e.g. Autopilot → Bypass).
*/
const ELEVATION_RANK: ReadonlyMap<ChatPermissionLevel, number> = new Map([
[ChatPermissionLevel.Assisted, 1],
[ChatPermissionLevel.AutoApprove, 2],
[ChatPermissionLevel.Autopilot, 2],
]);
export function hasShownElevatedWarning(level: ChatPermissionLevel, storageService: IStorageService): boolean {
const rank = ELEVATION_RANK.get(level);
if (rank === undefined) {
}
// Accepting any elevated level whose reach is >= this one (in this session
chatPermissionWarnings.ts ×6
// or persistently) suppresses the warning.
for (const [candidate, candidateRank] of ELEVATION_RANK) {
if (candidateRank < rank) {
}
return true;
}
if (key && storageService.getBoolean(key, StorageScope.PROFILE, false)) {
}
}
interface IElevatedWarningCopy {
readonly title: string;
readonly confirm: string;
readonly icon: ThemeIcon;
readonly detail: string;
}
function getElevatedWarningCopy(level: ChatPermissionLevel, defaultSettingKey: string, levelLabel?: string): IElevatedWarningCopy | undefined {
switch (level) {
case ChatPermissionLevel.Assisted:
levelLabel ??= localize('permissions.assisted', "Auto Approvals");
return {
title: localize('permissions.assisted.warning.title', "Enable {0}?", levelLabel),
confirm: localize('permissions.assisted.warning.confirm', "Enable"),
icon: Codicon.sparkle,
detail: localize('permissions.assisted.warning.detail', "{0} uses model recommendations to approve tool calls. Copilot will still ask when the model requires approval, excludes the request from automatic approval, or cannot make a recommendation.\n\nTo make this the starting permission level for new sessions, change the [{1}](command:workbench.action.openSettings?%5B%22{1}%22%5D) setting.", levelLabel, defaultSettingKey),
};
case ChatPermissionLevel.Autopilot:
return {
title: localize('permissions.autopilot.warning.title', "Enable Autopilot?"),
confirm: localize('permissions.autopilot.warning.confirm', "Enable"),
icon: Codicon.rocket,
detail: localize('permissions.autopilot.warning.detail', "Autopilot will auto-approve all tool calls and continue working autonomously until the task is complete. This includes terminal commands, file edits, and external tool calls. The agent will make decisions on your behalf without asking for confirmation.\n\nYou can stop the agent at any time by clicking the stop button. This applies to the current session only.\n\nTo make this the starting permission level for new sessions, change the [{0}](command:workbench.action.openSettings?%5B%22{0}%22%5D) setting.", defaultSettingKey),
};
case ChatPermissionLevel.AutoApprove:
levelLabel ??= localize('permissions.autoApprove', "Bypass Approvals");
return {
title: localize('permissions.autoApprove.warning.title', "Enable {0}?", levelLabel),
confirm: localize('permissions.autoApprove.warning.confirm', "Enable"),
icon: Codicon.warning,
detail: localize('permissions.autoApprove.warning.detail', "{0} will auto-approve all tool calls without asking for confirmation. This includes file edits, terminal commands, and external tool calls.\n\nTo make this the starting permission level for new sessions, change the [{1}](command:workbench.action.openSettings?%5B%22{1}%22%5D) setting.", levelLabel, defaultSettingKey),
};
default:
return undefined;
}
}
/**
* If `level` is an elevated permission level (Bypass Approvals or Autopilot)
* that hasn't already been confirmed in this session or persistently
* dismissed, show the corresponding warning dialog. Returns:
*
* - `true` if the level is not elevated, has already been confirmed, or the
* user accepts the dialog. In these cases the caller should proceed to apply
* the level.
* - `false` if the user cancels the dialog. The caller should abort the
* permission change.
*
* When the user ticks "Don't show again", the choice is persisted in
* `StorageScope.PROFILE`, which is shared across the workbench and Agents
* windows so a dismissal in one applies to the other.
*
* `options.defaultSettingKey` controls which "make this the default" setting
* the dialog links to — local chat uses `chat.permissions.default` (the
* default), while agent-host sessions pass
* `chat.defaultConfiguration`.
*/
export async function maybeConfirmElevatedPermissionLevel(
level: ChatPermissionLevel,
dialogService: IDialogService,
storageService: IStorageService,
options?: { readonly defaultSettingKey?: string; readonly levelLabel?: string },
): Promise<boolean> {
const key = dontShowAgainKey(level);
if (!key || hasShownElevatedWarning(level, storageService)) {
return true;
}
const copy = getElevatedWarningCopy(level, options?.defaultSettingKey ?? ChatConfiguration.DefaultPermissionLevel, options?.levelLabel);
if (!copy) {
return true;
}
const result = await dialogService.prompt({
type: Severity.Warning,
message: copy.title,
buttons: [
{
label: copy.confirm,
run: () => true,
},
{
label: localize('permissions.warning.cancel', "Cancel"),
run: () => false,
},
],
checkbox: {
label: localize('permissions.warning.dontShowAgain', "Don't show again"),
checked: false,
},
custom: {
icon: copy.icon,
markdownDetails: [{
markdown: new MarkdownString(
copy.detail,
{ isTrusted: { enabledCommands: ['workbench.action.openSettings'] } },
),
}],
},
});
if (result.result !== true) {
return false;
}
if (result.checkboxChecked) {
storageService.store(key, true, StorageScope.PROFILE, StorageTarget.USER);
}
shownWarnings.add(level);
return true;
}