Path: blob/main/src/vs/sessions/contrib/tunnelHost/electron-browser/tunnelHost.contribution.ts
13401 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import { Codicon } from '../../../../base/common/codicons.js';6import { Disposable } from '../../../../base/common/lifecycle.js';7import { localize, localize2 } from '../../../../nls.js';8import { Action2, MenuId, registerAction2 } from '../../../../platform/actions/common/actions.js';9import { IActionViewItemService } from '../../../../platform/actions/browser/actionViewItemService.js';10import { ConfigurationScope, Extensions as ConfigurationExtensions, IConfigurationRegistry } from '../../../../platform/configuration/common/configurationRegistry.js';11import { ContextKeyExpr, IContextKey, IContextKeyService, RawContextKey } from '../../../../platform/contextkey/common/contextkey.js';12import { InstantiationType, registerSingleton } from '../../../../platform/instantiation/common/extensions.js';13import { ServicesAccessor } from '../../../../platform/instantiation/common/instantiation.js';14import { INotificationService, Severity } from '../../../../platform/notification/common/notification.js';15import { Registry } from '../../../../platform/registry/common/platform.js';16import { IWorkbenchContribution, registerWorkbenchContribution2, WorkbenchPhase } from '../../../../workbench/common/contributions.js';17import { IOutputService } from '../../../../workbench/services/output/common/output.js';18import { ChatContextKeys } from '../../../../workbench/contrib/chat/common/actions/chatContextKeys.js';19import { ChatAgentLocation } from '../../../../workbench/contrib/chat/common/constants.js';20import { ITunnelHostService } from '../common/tunnelHost.js';21import { TUNNEL_HOST_LOG_ID } from '../../../../platform/agentHost/common/tunnelAgentHost.js';22import { CONFIGURATION_KEY_MICROSOFT_AUTH, SHOW_TUNNEL_HOST_OUTPUT_ID, TunnelHostService } from './tunnelHostService.js';23import { ToggleRemoteConnectionsActionViewItem } from './toggleRemoteConnectionsActionViewItem.js';2425const TUNNEL_HOST_SHARING_KEY = 'tunnelHostSharing';26const TUNNEL_HOST_SHARING_CONTEXT = new RawContextKey<boolean>(TUNNEL_HOST_SHARING_KEY, false);27const TOGGLE_SHARING_ID = 'sessions.tunnelHost.toggleSharing';2829const CATEGORY = localize2('tunnelHost.category', 'Remote Connections');3031// Register the renderer-side service32registerSingleton(ITunnelHostService, TunnelHostService, InstantiationType.Delayed);3334/**35* Contribution that manages the tunnel host sharing context key36* and registers the toggle action in the sessions titlebar.37*/38class TunnelHostContribution extends Disposable implements IWorkbenchContribution {3940static readonly ID = 'workbench.contrib.tunnelHost';4142private readonly _sharingContext: IContextKey<boolean>;4344constructor(45@IContextKeyService contextKeyService: IContextKeyService,46@ITunnelHostService tunnelHostService: ITunnelHostService,47@IActionViewItemService actionViewItemService: IActionViewItemService,48) {49super();5051this._sharingContext = TUNNEL_HOST_SHARING_CONTEXT.bindTo(contextKeyService);5253// Keep context key in sync with service state54this._register(tunnelHostService.onDidChangeStatus(() => {55this._sharingContext.set(tunnelHostService.isSharing);56}));5758// Register custom action view item with pulse, hover, and toast59this._register(actionViewItemService.register(60MenuId.ChatInputSecondary,61TOGGLE_SHARING_ID,62(action, _options, instaService) => instaService.createInstance(ToggleRemoteConnectionsActionViewItem, action),63tunnelHostService.onDidChangeStatus,64));65}66}6768// Register the toggle action69registerAction2(class ToggleRemoteConnectionsAction extends Action2 {70constructor() {71super({72id: TOGGLE_SHARING_ID,73title: localize2("toggleSharing", "Allow Remote Connections"),74category: CATEGORY,75icon: Codicon.radioTower,76toggled: ContextKeyExpr.equals(TUNNEL_HOST_SHARING_KEY, true),77menu: {78id: MenuId.ChatInputSecondary,79order: 10,80group: 'navigation',81when: ContextKeyExpr.and(82ChatContextKeys.enabled,83ChatContextKeys.location.isEqualTo(ChatAgentLocation.Chat),84ChatContextKeys.inQuickChat.negate(),85ContextKeyExpr.regex(ChatContextKeys.lockedCodingAgentId.key, /^agent-host-/),86)87}88});89}9091async run(accessor: ServicesAccessor): Promise<void> {92const tunnelHostService = accessor.get(ITunnelHostService);93const notificationService = accessor.get(INotificationService);9495try {96if (tunnelHostService.isSharing) {97await tunnelHostService.stopSharing();98} else {99await tunnelHostService.startSharing();100}101} catch (err) {102notificationService.notify({103severity: Severity.Error,104message: localize('tunnelHost.error', "Failed to toggle remote connections: {0}", String(err)),105});106}107}108});109110// Register the show output action111registerAction2(class ShowTunnelHostOutputAction extends Action2 {112constructor() {113super({114id: SHOW_TUNNEL_HOST_OUTPUT_ID,115title: localize2('showTunnelHostOutput', "Show Remote Connections Output"),116category: CATEGORY,117});118}119120async run(accessor: ServicesAccessor): Promise<void> {121const outputService = accessor.get(IOutputService);122await outputService.showChannel(TUNNEL_HOST_LOG_ID);123}124});125126registerWorkbenchContribution2(TunnelHostContribution.ID, TunnelHostContribution, WorkbenchPhase.AfterRestored);127128Registry.as<IConfigurationRegistry>(ConfigurationExtensions.Configuration).registerConfiguration({129type: 'object',130properties: {131[CONFIGURATION_KEY_MICROSOFT_AUTH]: {132description: localize('tunnelHost.enableMicrosoftAuth', "Enable Microsoft account authentication for agent host tunnels. When disabled, only GitHub authentication is used."),133type: 'boolean',134scope: ConfigurationScope.APPLICATION,135default: false,136tags: ['usesOnlineServices'],137},138}139});140141142