Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/workbench/contrib/chat/browser/chatContentParts/chatConfirmationContentPart.ts
3296 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 { Emitter } from '../../../../../base/common/event.js';
7
import { Disposable, IDisposable } from '../../../../../base/common/lifecycle.js';
8
import { localize } from '../../../../../nls.js';
9
import { IInstantiationService } from '../../../../../platform/instantiation/common/instantiation.js';
10
import { IChatProgressRenderableResponseContent } from '../../common/chatModel.js';
11
import { IChatConfirmation, IChatSendRequestOptions, IChatService } from '../../common/chatService.js';
12
import { isResponseVM } from '../../common/chatViewModel.js';
13
import { IChatWidgetService } from '../chat.js';
14
import { SimpleChatConfirmationWidget } from './chatConfirmationWidget.js';
15
import { IChatContentPart, IChatContentPartRenderContext } from './chatContentParts.js';
16
17
export class ChatConfirmationContentPart extends Disposable implements IChatContentPart {
18
public readonly domNode: HTMLElement;
19
20
private readonly _onDidChangeHeight = this._register(new Emitter<void>());
21
public readonly onDidChangeHeight = this._onDidChangeHeight.event;
22
23
constructor(
24
confirmation: IChatConfirmation,
25
context: IChatContentPartRenderContext,
26
@IInstantiationService private readonly instantiationService: IInstantiationService,
27
@IChatService private readonly chatService: IChatService,
28
@IChatWidgetService chatWidgetService: IChatWidgetService,
29
) {
30
super();
31
32
const element = context.element;
33
const buttons = confirmation.buttons
34
? confirmation.buttons.map(button => ({
35
label: button,
36
data: confirmation.data,
37
isSecondary: button !== confirmation.buttons?.[0],
38
}))
39
: [
40
{ label: localize('accept', "Accept"), data: confirmation.data },
41
{ label: localize('dismiss', "Dismiss"), data: confirmation.data, isSecondary: true },
42
];
43
const confirmationWidget = this._register(this.instantiationService.createInstance(SimpleChatConfirmationWidget, context.container, { title: confirmation.title, buttons, message: confirmation.message }));
44
confirmationWidget.setShowButtons(!confirmation.isUsed);
45
46
this._register(confirmationWidget.onDidChangeHeight(() => this._onDidChangeHeight.fire()));
47
48
this._register(confirmationWidget.onDidClick(async e => {
49
if (isResponseVM(element)) {
50
const prompt = `${e.label}: "${confirmation.title}"`;
51
const options: IChatSendRequestOptions = e.isSecondary ?
52
{ rejectedConfirmationData: [e.data] } :
53
{ acceptedConfirmationData: [e.data] };
54
options.agentId = element.agent?.id;
55
options.slashCommand = element.slashCommand?.name;
56
options.confirmation = e.label;
57
const widget = chatWidgetService.getWidgetBySessionId(element.sessionId);
58
options.userSelectedModelId = widget?.input.currentLanguageModel;
59
options.modeInfo = widget?.input.currentModeInfo;
60
options.location = widget?.location;
61
Object.assign(options, widget?.getModeRequestOptions());
62
63
if (await this.chatService.sendRequest(element.sessionId, prompt, options)) {
64
confirmation.isUsed = true;
65
confirmationWidget.setShowButtons(false);
66
this._onDidChangeHeight.fire();
67
}
68
}
69
}));
70
71
this.domNode = confirmationWidget.domNode;
72
}
73
74
hasSameContent(other: IChatProgressRenderableResponseContent): boolean {
75
// No other change allowed for this content type
76
return other.kind === 'confirmation';
77
}
78
79
addDisposable(disposable: IDisposable): void {
80
this._register(disposable);
81
}
82
}
83
84