Path: blob/main/src/vs/workbench/api/common/extHostChatInputNotification.ts
13397 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 type * as vscode from 'vscode';6import * as extHostProtocol from './extHost.protocol.js';7import { ExtensionIdentifier, IExtensionDescription } from '../../../platform/extensions/common/extensions.js';89export class ExtHostChatInputNotification {1011private readonly _proxy: extHostProtocol.MainThreadChatInputNotificationShape;1213private readonly _items = new Map<string, vscode.ChatInputNotification>();1415constructor(16mainContext: extHostProtocol.IMainContext17) {18this._proxy = mainContext.getProxy(extHostProtocol.MainContext.MainThreadChatInputNotification);19}2021createInputNotification(extension: IExtensionDescription, id: string): vscode.ChatInputNotification {22const internalId = asNotificationIdentifier(extension.identifier, id);23if (this._items.has(internalId)) {24throw new Error(`Chat input notification '${id}' already exists`);25}2627const state: extHostProtocol.ChatInputNotificationDto = {28id: internalId,29severity: extHostProtocol.ChatInputNotificationSeverityDto.Info,30message: '',31description: undefined,32actions: [],33dismissible: true,34autoDismissOnMessage: false,35};3637let disposed = false;38let visible = false;39const syncState = () => {40if (disposed) {41throw new Error('Chat input notification is disposed');42}4344if (!visible) {45return;46}4748this._proxy.$setNotification({ ...state });49};5051const item = Object.freeze<vscode.ChatInputNotification>({52id,5354get severity(): vscode.ChatInputNotificationSeverity {55return state.severity as number as vscode.ChatInputNotificationSeverity;56},57set severity(value: vscode.ChatInputNotificationSeverity) {58state.severity = value as number as extHostProtocol.ChatInputNotificationSeverityDto;59syncState();60},6162get message(): string {63return state.message;64},65set message(value: string) {66state.message = value;67syncState();68},6970get description(): string | undefined {71return state.description;72},73set description(value: string | undefined) {74state.description = value;75syncState();76},7778get actions(): vscode.ChatInputNotificationAction[] {79return state.actions;80},81set actions(value: vscode.ChatInputNotificationAction[]) {82state.actions = value.map(a => ({ label: a.label, commandId: a.commandId, commandArgs: a.commandArgs }));83syncState();84},8586get dismissible(): boolean {87return state.dismissible;88},89set dismissible(value: boolean) {90state.dismissible = value;91syncState();92},9394get autoDismissOnMessage(): boolean {95return state.autoDismissOnMessage;96},97set autoDismissOnMessage(value: boolean) {98state.autoDismissOnMessage = value;99syncState();100},101102show: () => {103visible = true;104syncState();105},106hide: () => {107if (disposed) {108return;109}110visible = false;111this._proxy.$disposeNotification(internalId);112},113dispose: () => {114if (disposed) {115return;116}117disposed = true;118visible = false;119this._proxy.$disposeNotification(internalId);120this._items.delete(internalId);121},122});123124this._items.set(internalId, item);125return item;126}127}128129function asNotificationIdentifier(extension: ExtensionIdentifier, id: string): string {130return `${ExtensionIdentifier.toKey(extension)}.${id}`;131}132133134