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