Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/workbench/contrib/chat/browser/promptSyntax/promptUrlHandler.ts
5267 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 { streamToBuffer, VSBuffer } from '../../../../../base/common/buffer.js';
7
import { CancellationToken } from '../../../../../base/common/cancellation.js';
8
import { Disposable } from '../../../../../base/common/lifecycle.js';
9
import { URI } from '../../../../../base/common/uri.js';
10
import { IFileService } from '../../../../../platform/files/common/files.js';
11
import { IInstantiationService } from '../../../../../platform/instantiation/common/instantiation.js';
12
import { INotificationService } from '../../../../../platform/notification/common/notification.js';
13
import { IOpenerService } from '../../../../../platform/opener/common/opener.js';
14
import { IRequestService } from '../../../../../platform/request/common/request.js';
15
import { IURLHandler, IURLService } from '../../../../../platform/url/common/url.js';
16
import { IWorkbenchContribution } from '../../../../common/contributions.js';
17
import { askForPromptFileName } from './pickers/askForPromptName.js';
18
import { askForPromptSourceFolder } from './pickers/askForPromptSourceFolder.js';
19
import { getCleanPromptName } from '../../common/promptSyntax/config/promptFileLocations.js';
20
import { PromptsType } from '../../common/promptSyntax/promptTypes.js';
21
import { ILogService } from '../../../../../platform/log/common/log.js';
22
import { localize } from '../../../../../nls.js';
23
import { IDialogService } from '../../../../../platform/dialogs/common/dialogs.js';
24
import { Schemas } from '../../../../../base/common/network.js';
25
import { MarkdownString } from '../../../../../base/common/htmlContent.js';
26
import { IHostService } from '../../../../services/host/browser/host.js';
27
import { mainWindow } from '../../../../../base/browser/window.js';
28
29
// example URL: code-oss:chat-prompt/install?url=https://gist.githubusercontent.com/aeschli/43fe78babd5635f062aef0195a476aad/raw/dfd71f60058a4dd25f584b55de3e20f5fd580e63/filterEvenNumbers.prompt.md
30
31
export class PromptUrlHandler extends Disposable implements IWorkbenchContribution, IURLHandler {
32
33
static readonly ID = 'workbench.contrib.promptUrlHandler';
34
35
constructor(
36
@IURLService urlService: IURLService,
37
@INotificationService private readonly notificationService: INotificationService,
38
@IRequestService private readonly requestService: IRequestService,
39
@IInstantiationService private readonly instantiationService: IInstantiationService,
40
@IFileService private readonly fileService: IFileService,
41
@IOpenerService private readonly openerService: IOpenerService,
42
@ILogService private readonly logService: ILogService,
43
@IDialogService private readonly dialogService: IDialogService,
44
45
@IHostService private readonly hostService: IHostService,
46
) {
47
super();
48
this._register(urlService.registerHandler(this));
49
}
50
51
async handleURL(uri: URI): Promise<boolean> {
52
let promptType: PromptsType | undefined;
53
switch (uri.path) {
54
case 'chat-prompt/install':
55
promptType = PromptsType.prompt;
56
break;
57
case 'chat-instructions/install':
58
promptType = PromptsType.instructions;
59
break;
60
case 'chat-mode/install':
61
case 'chat-agent/install':
62
promptType = PromptsType.agent;
63
break;
64
default:
65
return false;
66
}
67
68
try {
69
const query = decodeURIComponent(uri.query);
70
if (!query || !query.startsWith('url=')) {
71
return true;
72
}
73
74
const urlString = query.substring(4);
75
const url = URI.parse(urlString);
76
if (url.scheme !== Schemas.https && url.scheme !== Schemas.http) {
77
this.logService.error(`[PromptUrlHandler] Invalid URL: ${urlString}`);
78
return true;
79
}
80
81
await this.hostService.focus(mainWindow);
82
83
if (await this.shouldBlockInstall(promptType, url)) {
84
return true;
85
}
86
87
const result = await this.requestService.request({ type: 'GET', url: urlString }, CancellationToken.None);
88
if (result.res.statusCode !== 200) {
89
this.logService.error(`[PromptUrlHandler] Failed to fetch URL: ${urlString}`);
90
this.notificationService.error(localize('failed', 'Failed to fetch URL: {0}', urlString));
91
return true;
92
}
93
94
const responseData = (await streamToBuffer(result.stream)).toString();
95
96
const newFolder = await this.instantiationService.invokeFunction(askForPromptSourceFolder, promptType);
97
if (!newFolder) {
98
return true;
99
}
100
101
const newName = await this.instantiationService.invokeFunction(askForPromptFileName, promptType, newFolder.uri, getCleanPromptName(url));
102
if (!newName) {
103
return true;
104
}
105
106
const promptUri = URI.joinPath(newFolder.uri, newName);
107
108
await this.fileService.createFolder(newFolder.uri);
109
await this.fileService.createFile(promptUri, VSBuffer.fromString(responseData));
110
111
await this.openerService.open(promptUri);
112
return true;
113
114
} catch (error) {
115
this.logService.error(`Error handling prompt URL ${uri.toString()}`, error);
116
return true;
117
}
118
}
119
120
private async shouldBlockInstall(promptType: PromptsType, url: URI): Promise<boolean> {
121
let uriLabel = url.toString();
122
if (uriLabel.length > 50) {
123
uriLabel = `${uriLabel.substring(0, 35)}...${uriLabel.substring(uriLabel.length - 15)}`;
124
}
125
126
const detail = new MarkdownString('', { supportHtml: true });
127
detail.appendMarkdown(localize('confirmOpenDetail2', "This will access {0}.\n\n", `[${uriLabel}](${url.toString()})`));
128
detail.appendMarkdown(localize('confirmOpenDetail3', "If you did not initiate this request, it may represent an attempted attack on your system. Unless you took an explicit action to initiate this request, you should press 'No'"));
129
130
let message: string;
131
switch (promptType) {
132
case PromptsType.prompt:
133
message = localize('confirmInstallPrompt', "An external application wants to create a prompt file with content from a URL. Do you want to continue by selecting a destination folder and name?");
134
break;
135
case PromptsType.instructions:
136
message = localize('confirmInstallInstructions', "An external application wants to create an instructions file with content from a URL. Do you want to continue by selecting a destination folder and name?");
137
break;
138
default:
139
message = localize('confirmInstallAgent', "An external application wants to create a custom agent with content from a URL. Do you want to continue by selecting a destination folder and name?");
140
break;
141
}
142
143
const { confirmed } = await this.dialogService.confirm({
144
type: 'warning',
145
primaryButton: localize({ key: 'yesButton', comment: ['&& denotes a mnemonic'] }, "&&Yes"),
146
cancelButton: localize('noButton', "No"),
147
message,
148
custom: {
149
markdownDetails: [{
150
markdown: detail
151
}]
152
}
153
});
154
155
return !confirmed;
156
157
}
158
}
159
160