Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/workbench/api/common/extHostChatInputNotification.ts
13397 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 type * as vscode from 'vscode';
7
import * as extHostProtocol from './extHost.protocol.js';
8
import { ExtensionIdentifier, IExtensionDescription } from '../../../platform/extensions/common/extensions.js';
9
10
export class ExtHostChatInputNotification {
11
12
private readonly _proxy: extHostProtocol.MainThreadChatInputNotificationShape;
13
14
private readonly _items = new Map<string, vscode.ChatInputNotification>();
15
16
constructor(
17
mainContext: extHostProtocol.IMainContext
18
) {
19
this._proxy = mainContext.getProxy(extHostProtocol.MainContext.MainThreadChatInputNotification);
20
}
21
22
createInputNotification(extension: IExtensionDescription, id: string): vscode.ChatInputNotification {
23
const internalId = asNotificationIdentifier(extension.identifier, id);
24
if (this._items.has(internalId)) {
25
throw new Error(`Chat input notification '${id}' already exists`);
26
}
27
28
const state: extHostProtocol.ChatInputNotificationDto = {
29
id: internalId,
30
severity: extHostProtocol.ChatInputNotificationSeverityDto.Info,
31
message: '',
32
description: undefined,
33
actions: [],
34
dismissible: true,
35
autoDismissOnMessage: false,
36
};
37
38
let disposed = false;
39
let visible = false;
40
const syncState = () => {
41
if (disposed) {
42
throw new Error('Chat input notification is disposed');
43
}
44
45
if (!visible) {
46
return;
47
}
48
49
this._proxy.$setNotification({ ...state });
50
};
51
52
const item = Object.freeze<vscode.ChatInputNotification>({
53
id,
54
55
get severity(): vscode.ChatInputNotificationSeverity {
56
return state.severity as number as vscode.ChatInputNotificationSeverity;
57
},
58
set severity(value: vscode.ChatInputNotificationSeverity) {
59
state.severity = value as number as extHostProtocol.ChatInputNotificationSeverityDto;
60
syncState();
61
},
62
63
get message(): string {
64
return state.message;
65
},
66
set message(value: string) {
67
state.message = value;
68
syncState();
69
},
70
71
get description(): string | undefined {
72
return state.description;
73
},
74
set description(value: string | undefined) {
75
state.description = value;
76
syncState();
77
},
78
79
get actions(): vscode.ChatInputNotificationAction[] {
80
return state.actions;
81
},
82
set actions(value: vscode.ChatInputNotificationAction[]) {
83
state.actions = value.map(a => ({ label: a.label, commandId: a.commandId, commandArgs: a.commandArgs }));
84
syncState();
85
},
86
87
get dismissible(): boolean {
88
return state.dismissible;
89
},
90
set dismissible(value: boolean) {
91
state.dismissible = value;
92
syncState();
93
},
94
95
get autoDismissOnMessage(): boolean {
96
return state.autoDismissOnMessage;
97
},
98
set autoDismissOnMessage(value: boolean) {
99
state.autoDismissOnMessage = value;
100
syncState();
101
},
102
103
show: () => {
104
visible = true;
105
syncState();
106
},
107
hide: () => {
108
if (disposed) {
109
return;
110
}
111
visible = false;
112
this._proxy.$disposeNotification(internalId);
113
},
114
dispose: () => {
115
if (disposed) {
116
return;
117
}
118
disposed = true;
119
visible = false;
120
this._proxy.$disposeNotification(internalId);
121
this._items.delete(internalId);
122
},
123
});
124
125
this._items.set(internalId, item);
126
return item;
127
}
128
}
129
130
function asNotificationIdentifier(extension: ExtensionIdentifier, id: string): string {
131
return `${ExtensionIdentifier.toKey(extension)}.${id}`;
132
}
133
134