Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/src/extension/prompts/node/panel/promptFile.tsx
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
import { BasePromptElementProps, PromptElement, PromptReference, PromptSizing } from '@vscode/prompt-tsx';
7
import type { ChatLanguageModelToolReference } from 'vscode';
8
import { IIgnoreService } from '../../../../platform/ignore/common/ignoreService';
9
import { ILogService } from '../../../../platform/log/common/logService';
10
import { IPromptPathRepresentationService } from '../../../../platform/prompts/common/promptPathRepresentationService';
11
import { IWorkspaceService } from '../../../../platform/workspace/common/workspaceService';
12
import { URI } from '../../../../util/vs/base/common/uri';
13
import { PromptVariable } from '../../../prompt/common/chatVariablesCollection';
14
import { IPromptVariablesService } from '../../../prompt/node/promptVariablesService';
15
import { EmbeddedInsideUserMessage } from '../base/promptElement';
16
import { Tag } from '../base/tag';
17
18
export interface PromptFileProps extends BasePromptElementProps, EmbeddedInsideUserMessage {
19
readonly variable: PromptVariable;
20
readonly omitReferences?: boolean;
21
}
22
23
export class PromptFile extends PromptElement<PromptFileProps, void> {
24
25
constructor(
26
props: PromptFileProps,
27
@IPromptVariablesService private readonly promptVariablesService: IPromptVariablesService,
28
@ILogService private readonly logService: ILogService,
29
@IPromptPathRepresentationService private readonly promptPathRepresentationService: IPromptPathRepresentationService,
30
@IIgnoreService private readonly ignoreService: IIgnoreService,
31
@IWorkspaceService private readonly workspaceService: IWorkspaceService,
32
) {
33
super(props);
34
}
35
36
override async render(state: void, sizing: PromptSizing) {
37
const variable = this.props.variable.reference;
38
const uri = variable.value;
39
if (!URI.isUri(uri)) {
40
this.logService.debug(`Prompt file variable does not have a URI value: ${variable.value}`);
41
return undefined;
42
}
43
44
if (await this.ignoreService.isCopilotIgnored(uri)) {
45
return <ignoredFiles value={[uri]} />;
46
}
47
48
const content = await this.getBodyContent(uri, variable.toolReferences);
49
const attrs: Record<string, string> = {};
50
attrs.id = variable.name;
51
attrs.filePath = this.promptPathRepresentationService.getFilePath(uri);
52
return <Tag name='attachment' attrs={attrs}>
53
{!this.props.omitReferences && <references value={[new PromptReference(uri, undefined)]} />}
54
Prompt instructions file:<br />
55
{content}
56
</Tag>;
57
}
58
59
private async getBodyContent(fileUri: URI, toolReferences: readonly ChatLanguageModelToolReference[] | undefined): Promise<string | undefined> {
60
try {
61
const doc = await this.workspaceService.openTextDocument(fileUri);
62
let content = doc.getText();
63
if (toolReferences && toolReferences.length > 0) {
64
content = await this.promptVariablesService.resolveToolReferencesInPrompt(content, toolReferences);
65
}
66
67
let bodyOffset = 0;
68
if (content.match(/^---[\s\r\n]/)) {
69
// find the start of the body
70
const match = content.slice(3).match(/[\r\n]---[\s\r\n]*/);
71
if (match) {
72
bodyOffset = match.index! + match[0].length;
73
}
74
}
75
const bodyContent = content.substring(bodyOffset);
76
77
return bodyContent;
78
} catch (e) {
79
this.logService.debug(`Prompt file not found: ${fileUri.toString()}`);
80
return undefined;
81
}
82
}
83
}
84
85