Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/src/extension/context/node/resolvers/genericInlineIntentInvocation.ts
13405 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
7
import { BasePromptElementProps } from '@vscode/prompt-tsx';
8
import type { CancellationToken, ChatResponseProgressPart, ChatResponseReferencePart, ChatResponseStream, Progress } from 'vscode';
9
import { IResponsePart } from '../../../../platform/chat/common/chatMLFetcher';
10
import { ChatLocation } from '../../../../platform/chat/common/commonTypes';
11
import { IChatEndpoint } from '../../../../platform/networking/common/networking';
12
import { Schemas } from '../../../../util/vs/base/common/network';
13
import { IInstantiationService } from '../../../../util/vs/platform/instantiation/common/instantiation';
14
import { IBuildPromptContext } from '../../../prompt/common/intents';
15
import { IDocumentContext } from '../../../prompt/node/documentContext';
16
import { EditStrategy } from '../../../prompt/node/editGeneration';
17
import { IBuildPromptResult, IIntent, IIntentInvocation, IResponseProcessorContext, NoopReplyInterpreter, ReplyInterpreter, ReplyInterpreterMetaData } from '../../../prompt/node/intents';
18
import { PromptElementCtor } from '../../../prompts/node/base/promptElement';
19
import { PromptRenderer } from '../../../prompts/node/base/promptRenderer';
20
import { InlineChatEditCodePrompt } from '../../../prompts/node/inline/inlineChatEditCodePrompt';
21
import { InlineChatEditMarkdownPrompt } from '../../../prompts/node/inline/inlineChatEditMarkdownPrompt';
22
import { InlineChatGenerateCodePrompt } from '../../../prompts/node/inline/inlineChatGenerateCodePrompt';
23
import { InlineChatGenerateMarkdownPrompt } from '../../../prompts/node/inline/inlineChatGenerateMarkdownPrompt';
24
import { InlineChatNotebookEditPrompt } from '../../../prompts/node/inline/inlineChatNotebookEditPrompt';
25
import { InlineChatNotebookGeneratePrompt } from '../../../prompts/node/inline/inlineChatNotebookGeneratePrompt';
26
27
export interface GenericInlinePromptProps extends BasePromptElementProps {
28
documentContext: IDocumentContext;
29
promptContext: IBuildPromptContext;
30
}
31
32
export class GenericInlineIntentInvocation implements IIntentInvocation {
33
34
private replyInterpreter: ReplyInterpreter | null = null;
35
36
constructor(
37
readonly intent: IIntent,
38
readonly location: ChatLocation,
39
readonly endpoint: IChatEndpoint,
40
private readonly documentContext: IDocumentContext,
41
private readonly editStrategy: EditStrategy,
42
@IInstantiationService private readonly instantiationService: IInstantiationService,
43
) { }
44
45
async buildPrompt(
46
promptContext: IBuildPromptContext,
47
progress: Progress<ChatResponseReferencePart | ChatResponseProgressPart>,
48
token: CancellationToken
49
): Promise<IBuildPromptResult> {
50
let prompt: PromptElementCtor<GenericInlinePromptProps, any>;
51
if (this.documentContext.document.uri.scheme === Schemas.vscodeNotebookCell) {
52
prompt = (this.editStrategy === EditStrategy.ForceInsertion ? InlineChatNotebookGeneratePrompt : InlineChatNotebookEditPrompt);
53
} else if (this.documentContext.document.languageId === 'markdown') {
54
prompt = (this.editStrategy === EditStrategy.ForceInsertion ? InlineChatGenerateMarkdownPrompt : InlineChatEditMarkdownPrompt);
55
} else {
56
prompt = (this.editStrategy === EditStrategy.ForceInsertion ? InlineChatGenerateCodePrompt : InlineChatEditCodePrompt);
57
}
58
const renderer = PromptRenderer.create(this.instantiationService, this.endpoint, prompt, {
59
documentContext: this.documentContext,
60
promptContext
61
});
62
const result = await renderer.render(progress, token);
63
64
this.replyInterpreter = result.metadata.get(ReplyInterpreterMetaData)?.replyInterpreter ?? null;
65
66
if (!this.replyInterpreter && result.hasIgnoredFiles) {
67
this.replyInterpreter = new NoopReplyInterpreter();
68
}
69
70
71
return result;
72
}
73
74
public processResponse(context: IResponseProcessorContext, inputStream: AsyncIterable<IResponsePart>, outputStream: ChatResponseStream, token: CancellationToken): Promise<void> {
75
if (!this.replyInterpreter) {
76
throw new Error(`Could not process response without a reply interpreter!`);
77
}
78
return this.replyInterpreter.processResponse(context, inputStream, outputStream, token);
79
}
80
}
81
82