Path: blob/main/src/vs/workbench/contrib/chat/browser/promptSyntax/promptUrlHandler.ts
5267 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 { streamToBuffer, VSBuffer } from '../../../../../base/common/buffer.js';6import { CancellationToken } from '../../../../../base/common/cancellation.js';7import { Disposable } from '../../../../../base/common/lifecycle.js';8import { URI } from '../../../../../base/common/uri.js';9import { IFileService } from '../../../../../platform/files/common/files.js';10import { IInstantiationService } from '../../../../../platform/instantiation/common/instantiation.js';11import { INotificationService } from '../../../../../platform/notification/common/notification.js';12import { IOpenerService } from '../../../../../platform/opener/common/opener.js';13import { IRequestService } from '../../../../../platform/request/common/request.js';14import { IURLHandler, IURLService } from '../../../../../platform/url/common/url.js';15import { IWorkbenchContribution } from '../../../../common/contributions.js';16import { askForPromptFileName } from './pickers/askForPromptName.js';17import { askForPromptSourceFolder } from './pickers/askForPromptSourceFolder.js';18import { getCleanPromptName } from '../../common/promptSyntax/config/promptFileLocations.js';19import { PromptsType } from '../../common/promptSyntax/promptTypes.js';20import { ILogService } from '../../../../../platform/log/common/log.js';21import { localize } from '../../../../../nls.js';22import { IDialogService } from '../../../../../platform/dialogs/common/dialogs.js';23import { Schemas } from '../../../../../base/common/network.js';24import { MarkdownString } from '../../../../../base/common/htmlContent.js';25import { IHostService } from '../../../../services/host/browser/host.js';26import { mainWindow } from '../../../../../base/browser/window.js';2728// example URL: code-oss:chat-prompt/install?url=https://gist.githubusercontent.com/aeschli/43fe78babd5635f062aef0195a476aad/raw/dfd71f60058a4dd25f584b55de3e20f5fd580e63/filterEvenNumbers.prompt.md2930export class PromptUrlHandler extends Disposable implements IWorkbenchContribution, IURLHandler {3132static readonly ID = 'workbench.contrib.promptUrlHandler';3334constructor(35@IURLService urlService: IURLService,36@INotificationService private readonly notificationService: INotificationService,37@IRequestService private readonly requestService: IRequestService,38@IInstantiationService private readonly instantiationService: IInstantiationService,39@IFileService private readonly fileService: IFileService,40@IOpenerService private readonly openerService: IOpenerService,41@ILogService private readonly logService: ILogService,42@IDialogService private readonly dialogService: IDialogService,4344@IHostService private readonly hostService: IHostService,45) {46super();47this._register(urlService.registerHandler(this));48}4950async handleURL(uri: URI): Promise<boolean> {51let promptType: PromptsType | undefined;52switch (uri.path) {53case 'chat-prompt/install':54promptType = PromptsType.prompt;55break;56case 'chat-instructions/install':57promptType = PromptsType.instructions;58break;59case 'chat-mode/install':60case 'chat-agent/install':61promptType = PromptsType.agent;62break;63default:64return false;65}6667try {68const query = decodeURIComponent(uri.query);69if (!query || !query.startsWith('url=')) {70return true;71}7273const urlString = query.substring(4);74const url = URI.parse(urlString);75if (url.scheme !== Schemas.https && url.scheme !== Schemas.http) {76this.logService.error(`[PromptUrlHandler] Invalid URL: ${urlString}`);77return true;78}7980await this.hostService.focus(mainWindow);8182if (await this.shouldBlockInstall(promptType, url)) {83return true;84}8586const result = await this.requestService.request({ type: 'GET', url: urlString }, CancellationToken.None);87if (result.res.statusCode !== 200) {88this.logService.error(`[PromptUrlHandler] Failed to fetch URL: ${urlString}`);89this.notificationService.error(localize('failed', 'Failed to fetch URL: {0}', urlString));90return true;91}9293const responseData = (await streamToBuffer(result.stream)).toString();9495const newFolder = await this.instantiationService.invokeFunction(askForPromptSourceFolder, promptType);96if (!newFolder) {97return true;98}99100const newName = await this.instantiationService.invokeFunction(askForPromptFileName, promptType, newFolder.uri, getCleanPromptName(url));101if (!newName) {102return true;103}104105const promptUri = URI.joinPath(newFolder.uri, newName);106107await this.fileService.createFolder(newFolder.uri);108await this.fileService.createFile(promptUri, VSBuffer.fromString(responseData));109110await this.openerService.open(promptUri);111return true;112113} catch (error) {114this.logService.error(`Error handling prompt URL ${uri.toString()}`, error);115return true;116}117}118119private async shouldBlockInstall(promptType: PromptsType, url: URI): Promise<boolean> {120let uriLabel = url.toString();121if (uriLabel.length > 50) {122uriLabel = `${uriLabel.substring(0, 35)}...${uriLabel.substring(uriLabel.length - 15)}`;123}124125const detail = new MarkdownString('', { supportHtml: true });126detail.appendMarkdown(localize('confirmOpenDetail2', "This will access {0}.\n\n", `[${uriLabel}](${url.toString()})`));127detail.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'"));128129let message: string;130switch (promptType) {131case PromptsType.prompt:132message = 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?");133break;134case PromptsType.instructions:135message = 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?");136break;137default:138message = 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?");139break;140}141142const { confirmed } = await this.dialogService.confirm({143type: 'warning',144primaryButton: localize({ key: 'yesButton', comment: ['&& denotes a mnemonic'] }, "&&Yes"),145cancelButton: localize('noButton', "No"),146message,147custom: {148markdownDetails: [{149markdown: detail150}]151}152});153154return !confirmed;155156}157}158159160