Path: blob/main/extensions/copilot/src/extension/intents/node/newNotebookIntent.contribution.ts
13399 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*--------------------------------------------------------------------------------------------*/4import * as l10n from '@vscode/l10n';5import type * as vscode from 'vscode';6import { IResponsePart } from '../../../platform/chat/common/chatMLFetcher';7import { ChatLocation } from '../../../platform/chat/common/commonTypes';8import { IEndpointProvider } from '../../../platform/endpoint/common/endpointProvider';9import { IChatEndpoint } from '../../../platform/networking/common/networking';10import { CancellationToken } from '../../../util/vs/base/common/cancellation';11import { IInstantiationService } from '../../../util/vs/platform/instantiation/common/instantiation';12import { Intent } from '../../common/constants';13import { IBuildPromptContext } from '../../prompt/common/intents';14import { IIntent, IIntentInvocation, IIntentInvocationContext, IntentLinkificationOptions, IResponseProcessorContext } from '../../prompt/node/intents';15import { PromptRenderer } from '../../prompts/node/base/promptRenderer';16import { NewNotebookPlanningPrompt } from '../../prompts/node/panel/newNotebook';17import { NewNotebookResponseProcessor } from './newNotebookIntent';181920export class NewNotebookIntent implements IIntent {21static readonly ID = Intent.NewNotebook;22readonly id: string = Intent.NewNotebook;23readonly description = l10n.t('Create a new Jupyter Notebook');24readonly locations = [ChatLocation.Panel];2526readonly commandInfo = {27allowsEmptyArgs: false,28yieldsTo: [29{ command: 'fix' },30{ command: 'explain' },31{ command: 'workspace' },32{ command: 'tests' },33],34defaultEnablement: true,35sampleRequest: l10n.t('How do I create a notebook to load data from a csv file?')36};3738constructor(39@IEndpointProvider private readonly endpointProvider: IEndpointProvider,40@IInstantiationService private readonly instantiationService: IInstantiationService,41) { }4243async invoke(invocationContext: IIntentInvocationContext): Promise<IIntentInvocation> {44const location = invocationContext.location;45const endpoint = await this.endpointProvider.getChatEndpoint(invocationContext.request);4647return this.instantiationService.createInstance(NewNotebookPlanningInvocation, this, endpoint, location, invocationContext.request.prompt);48}49}5051class NewNotebookPlanningInvocation implements IIntentInvocation {5253readonly linkification: IntentLinkificationOptions = { disable: true };5455private context: IBuildPromptContext | undefined;5657constructor(58readonly intent: NewNotebookIntent,59readonly endpoint: IChatEndpoint,60readonly location: ChatLocation,61readonly query: string,62@IInstantiationService private readonly instantiationService: IInstantiationService,63) { }6465async buildPrompt(promptContext: IBuildPromptContext, progress: vscode.Progress<vscode.ChatResponseProgressPart | vscode.ChatResponseReferencePart>, token: vscode.CancellationToken) {66this.context = promptContext;6768const renderer = PromptRenderer.create(this.instantiationService, this.endpoint, NewNotebookPlanningPrompt, {69promptContext,70endpoint: this.endpoint,71});7273return await renderer.render(progress, token);74}7576processResponse(context: IResponseProcessorContext, inputStream: AsyncIterable<IResponsePart>, outputStream: vscode.ChatResponseStream, token: CancellationToken): Promise<void> {77outputStream.markdown(l10n.t('Creating a new notebook:\n'));7879const responseProcessor = this.instantiationService.createInstance(NewNotebookResponseProcessor, this.endpoint, this.context);8081return responseProcessor.processResponse(context, inputStream, outputStream, token);82}83}848586