Path: blob/main/src/vs/workbench/contrib/chat/browser/chatContentParts/chatConfirmationContentPart.ts
3296 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 { Emitter } from '../../../../../base/common/event.js';6import { Disposable, IDisposable } from '../../../../../base/common/lifecycle.js';7import { localize } from '../../../../../nls.js';8import { IInstantiationService } from '../../../../../platform/instantiation/common/instantiation.js';9import { IChatProgressRenderableResponseContent } from '../../common/chatModel.js';10import { IChatConfirmation, IChatSendRequestOptions, IChatService } from '../../common/chatService.js';11import { isResponseVM } from '../../common/chatViewModel.js';12import { IChatWidgetService } from '../chat.js';13import { SimpleChatConfirmationWidget } from './chatConfirmationWidget.js';14import { IChatContentPart, IChatContentPartRenderContext } from './chatContentParts.js';1516export class ChatConfirmationContentPart extends Disposable implements IChatContentPart {17public readonly domNode: HTMLElement;1819private readonly _onDidChangeHeight = this._register(new Emitter<void>());20public readonly onDidChangeHeight = this._onDidChangeHeight.event;2122constructor(23confirmation: IChatConfirmation,24context: IChatContentPartRenderContext,25@IInstantiationService private readonly instantiationService: IInstantiationService,26@IChatService private readonly chatService: IChatService,27@IChatWidgetService chatWidgetService: IChatWidgetService,28) {29super();3031const element = context.element;32const buttons = confirmation.buttons33? confirmation.buttons.map(button => ({34label: button,35data: confirmation.data,36isSecondary: button !== confirmation.buttons?.[0],37}))38: [39{ label: localize('accept', "Accept"), data: confirmation.data },40{ label: localize('dismiss', "Dismiss"), data: confirmation.data, isSecondary: true },41];42const confirmationWidget = this._register(this.instantiationService.createInstance(SimpleChatConfirmationWidget, context.container, { title: confirmation.title, buttons, message: confirmation.message }));43confirmationWidget.setShowButtons(!confirmation.isUsed);4445this._register(confirmationWidget.onDidChangeHeight(() => this._onDidChangeHeight.fire()));4647this._register(confirmationWidget.onDidClick(async e => {48if (isResponseVM(element)) {49const prompt = `${e.label}: "${confirmation.title}"`;50const options: IChatSendRequestOptions = e.isSecondary ?51{ rejectedConfirmationData: [e.data] } :52{ acceptedConfirmationData: [e.data] };53options.agentId = element.agent?.id;54options.slashCommand = element.slashCommand?.name;55options.confirmation = e.label;56const widget = chatWidgetService.getWidgetBySessionId(element.sessionId);57options.userSelectedModelId = widget?.input.currentLanguageModel;58options.modeInfo = widget?.input.currentModeInfo;59options.location = widget?.location;60Object.assign(options, widget?.getModeRequestOptions());6162if (await this.chatService.sendRequest(element.sessionId, prompt, options)) {63confirmation.isUsed = true;64confirmationWidget.setShowButtons(false);65this._onDidChangeHeight.fire();66}67}68}));6970this.domNode = confirmationWidget.domNode;71}7273hasSameContent(other: IChatProgressRenderableResponseContent): boolean {74// No other change allowed for this content type75return other.kind === 'confirmation';76}7778addDisposable(disposable: IDisposable): void {79this._register(disposable);80}81}828384