|
| 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 { IFileSystemService } from '../../../../platform/filesystem/common/fileSystemService'; |
| 9 | +import { IIgnoreService } from '../../../../platform/ignore/common/ignoreService'; |
| 10 | +import { ILogService } from '../../../../platform/log/common/logService'; |
| 11 | +import { IPromptPathRepresentationService } from '../../../../platform/prompts/common/promptPathRepresentationService'; |
| 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 | +import { FilePathMode } from './fileVariable'; |
| 18 | + |
| 19 | +export interface PromptFileProps extends BasePromptElementProps, EmbeddedInsideUserMessage { |
| 20 | + readonly variable: PromptVariable; |
| 21 | + readonly omitReferences?: boolean; |
| 22 | + readonly filePathMode: FilePathMode; |
| 23 | +} |
| 24 | + |
| 25 | +export class PromptFile extends PromptElement<PromptFileProps, void> { |
| 26 | + |
| 27 | + constructor( |
| 28 | + props: PromptFileProps, |
| 29 | + @IFileSystemService private readonly fileSystemService: IFileSystemService, |
| 30 | + @IPromptVariablesService private readonly promptVariablesService: IPromptVariablesService, |
| 31 | + @ILogService private readonly logService: ILogService, |
| 32 | + @IPromptPathRepresentationService private readonly promptPathRepresentationService: IPromptPathRepresentationService, |
| 33 | + @IIgnoreService private readonly ignoreService: IIgnoreService |
| 34 | + ) { |
| 35 | + super(props); |
| 36 | + } |
| 37 | + |
| 38 | + override async render(state: void, sizing: PromptSizing) { |
| 39 | + const variable = this.props.variable.reference; |
| 40 | + const uri = variable.value; |
| 41 | + if (!URI.isUri(uri)) { |
| 42 | + this.logService.debug(`Prompt file variable does not have a URI value: ${variable.value}`); |
| 43 | + return undefined; |
| 44 | + } |
| 45 | + |
| 46 | + if (await this.ignoreService.isCopilotIgnored(uri)) { |
| 47 | + return <ignoredFiles value={[uri]} />; |
| 48 | + } |
| 49 | + |
| 50 | + const content = await this.getBodyContent(uri, variable.toolReferences); |
| 51 | + const attrs: Record<string, string> = {}; |
| 52 | + attrs.id = variable.name; |
| 53 | + if (this.props.filePathMode === FilePathMode.AsAttribute) { |
| 54 | + attrs.filePath = this.promptPathRepresentationService.getFilePath(uri); |
| 55 | + } |
| 56 | + return <Tag name='attachment' attrs={attrs}> |
| 57 | + {!this.props.omitReferences && <references value={[new PromptReference({ variableName: variable.name, value: uri }, undefined)]} />} |
| 58 | + Prompt instructions file:<br /> |
| 59 | + {content} |
| 60 | + </Tag>; |
| 61 | + } |
| 62 | + |
| 63 | + private async getBodyContent(fileUri: URI, toolReferences: readonly ChatLanguageModelToolReference[] | undefined): Promise<string | undefined> { |
| 64 | + try { |
| 65 | + const fileContents = await this.fileSystemService.readFile(fileUri); |
| 66 | + let content = new TextDecoder().decode(fileContents); |
| 67 | + if (toolReferences && toolReferences.length > 0) { |
| 68 | + content = await this.promptVariablesService.resolveToolReferencesInPrompt(content, toolReferences); |
| 69 | + } |
| 70 | + |
| 71 | + let bodyOffset = 0; |
| 72 | + if (content.match(/^---[\s\r\n]/)) { |
| 73 | + // find the start of the body |
| 74 | + const match = content.slice(3).match(/[\r\n]---[\s\r\n]*/); |
| 75 | + if (match) { |
| 76 | + bodyOffset = match.index! + match[0].length; |
| 77 | + } |
| 78 | + } |
| 79 | + return content.substring(bodyOffset); |
| 80 | + } catch (e) { |
| 81 | + this.logService.debug(`Prompt file not found: ${fileUri.toString()}`); |
| 82 | + return undefined; |
| 83 | + } |
| 84 | + } |
| 85 | +} |
0 commit comments