Skip to content

Commit 0182d6d

Browse files
committed
use correct chat variable IDs for implicit prompt attachments
1 parent 639875b commit 0182d6d

File tree

2 files changed

+45
-12
lines changed

2 files changed

+45
-12
lines changed

src/vs/workbench/contrib/chat/browser/chatAttachmentModel/chatPromptAttachmentsCollection.ts

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import { URI } from '../../../../../base/common/uri.js';
77
import { Emitter } from '../../../../../base/common/event.js';
8+
import { basename } from '../../../../../base/common/resources.js';
89
import { IChatRequestVariableEntry } from '../../common/chatModel.js';
910
import { ChatPromptAttachmentModel } from './chatPromptAttachmentModel.js';
1011
import { PromptsConfig } from '../../../../../platform/prompts/common/config.js';
@@ -13,6 +14,30 @@ import { Disposable, DisposableMap } from '../../../../../base/common/lifecycle.
1314
import { IInstantiationService } from '../../../../../platform/instantiation/common/instantiation.js';
1415
import { IConfigurationService } from '../../../../../platform/configuration/common/configuration.js';
1516

17+
/**
18+
* Prompt IDs start with a well-defined prefix that is used by
19+
* the copilot extension to identify prompt references.
20+
*
21+
* @param uri The URI of the prompt file.
22+
* @param isRoot Whether the prompt file is the root file, or a
23+
* child reference that is nested inside the root file.
24+
*/
25+
export const createPromptVariableId = (
26+
uri: URI,
27+
isRoot: boolean,
28+
): string => {
29+
// the default prefix that is used for all prompt files
30+
let prefix = 'vscode.prompt.instructions';
31+
// if the reference is the root object, add the `.root` suffix
32+
if (isRoot) {
33+
prefix += '.root';
34+
}
35+
36+
// final `id` for all `prompt files` starts with the well-defined
37+
// part that the copilot extension(or other chatbot) can rely on
38+
return `${prefix}__${uri}`;
39+
};
40+
1641
/**
1742
* Utility to convert a {@link reference} to a chat variable entry.
1843
* The `id` of the chat variable can be one of the following:
@@ -34,23 +59,14 @@ export const toChatVariable = (
3459
// default `id` is the stringified `URI`
3560
let id = `${uri}`;
3661

37-
// for prompt files, we add a prefix to the `id`
62+
// prompts have special `id`s that are used by the copilot extension
3863
if (isPromptFile) {
39-
// the default prefix that is used for all prompt files
40-
let prefix = 'vscode.prompt.instructions';
41-
// if the reference is the root object, add the `.root` suffix
42-
if (isRoot) {
43-
prefix += '.root';
44-
}
45-
46-
// final `id` for all `prompt files` starts with the well-defined
47-
// part that the copilot extension(or other chatbot) can rely on
48-
id = `${prefix}__${id}`;
64+
id = createPromptVariableId(uri, isRoot);
4965
}
5066

5167
return {
5268
id,
53-
name: uri.fsPath,
69+
name: `file:${basename(uri)}`,
5470
value: uri,
5571
kind: 'file',
5672
};

src/vs/workbench/contrib/chat/browser/contrib/chatImplicitContext.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { Disposable, DisposableStore, MutableDisposable } from '../../../../../b
99
import { Schemas } from '../../../../../base/common/network.js';
1010
import { autorun } from '../../../../../base/common/observable.js';
1111
import { basename, isEqual } from '../../../../../base/common/resources.js';
12+
import { assertDefined } from '../../../../../base/common/types.js';
1213
import { URI } from '../../../../../base/common/uri.js';
1314
import { ICodeEditor, isCodeEditor, isDiffEditor } from '../../../../../editor/browser/editorBrowser.js';
1415
import { ICodeEditorService } from '../../../../../editor/browser/services/codeEditorService.js';
@@ -25,6 +26,7 @@ import { ChatAgentLocation } from '../../common/constants.js';
2526
import { ILanguageModelIgnoredFilesService } from '../../common/ignoredFiles.js';
2627
import { PROMPT_LANGUAGE_ID } from '../../common/promptSyntax/constants.js';
2728
import { IChatWidget, IChatWidgetService } from '../chat.js';
29+
import { createPromptVariableId } from '../chatAttachmentModel/chatPromptAttachmentsCollection.js';
2830

2931
export class ChatImplicitContextContribution extends Disposable implements IWorkbenchContribution {
3032
static readonly ID = 'chat.implicitContext';
@@ -234,6 +236,21 @@ export class ChatImplicitContextContribution extends Disposable implements IWork
234236

235237
export class ChatImplicitContext extends Disposable implements IChatRequestImplicitVariableEntry {
236238
get id() {
239+
// IDs for prompt files need to start with a special prefix
240+
// that is used by the copilot extension to identify them
241+
if (this.isPrompt) {
242+
assertDefined(
243+
this.value,
244+
'Implicit prompt attachments must have a value.',
245+
);
246+
247+
if (URI.isUri(this.value)) {
248+
return createPromptVariableId(this.value, true);
249+
}
250+
251+
return createPromptVariableId(this.value.uri, true);
252+
}
253+
237254
if (URI.isUri(this.value)) {
238255
return 'vscode.implicit.file';
239256
} else if (this.value) {

0 commit comments

Comments
 (0)