Skip to content

Commit 4f7ffdf

Browse files
authored
Cloud Agent: Improve file references (#1841)
* attach untitled file refs (ref microsoft/vscode#274965) * append files that have changes * Refine TODO comments in copilotCloudSessionsProvider * fixes
1 parent 49e2cb6 commit 4f7ffdf

File tree

1 file changed

+38
-9
lines changed

1 file changed

+38
-9
lines changed

src/extension/chatSessions/vscode-node/copilotCloudSessionsProvider.ts

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,7 @@ export class CopilotCloudSessionsProvider extends Disposable implements vscode.C
426426
const result = await this.invokeRemoteAgent(
427427
prompt,
428428
[
429-
this.extractFileReferences(references),
429+
await this.extractFileReferences(references),
430430
history
431431
].join('\n\n').trim(),
432432
token,
@@ -729,28 +729,57 @@ export class CopilotCloudSessionsProvider extends Disposable implements vscode.C
729729
}
730730
}
731731

732-
private extractFileReferences(references: readonly vscode.ChatPromptReference[] | undefined): string | undefined {
732+
private async extractFileReferences(references: readonly vscode.ChatPromptReference[] | undefined): Promise<string | undefined> {
733733
if (!references || references.length === 0) {
734734
return;
735735
}
736736
// 'file:///Users/jospicer/dev/joshbot/.github/workflows/build-vsix.yml' -> '.github/workflows/build-vsix.yml'
737-
const parts: string[] = [];
737+
const fileRefs: string[] = [];
738+
const fullFileParts: string[] = [];
739+
const git = this._gitExtensionService.getExtensionApi();
738740
for (const ref of references) {
739741
if (ref.value instanceof vscode.Uri && ref.value.scheme === 'file') { // TODO: Add support for more kinds of references
740-
const git = this._gitExtensionService.getExtensionApi();
741-
const repositoryForFile = git?.getRepository(ref.value);
742+
const fileUri = ref.value;
743+
const repositoryForFile = git?.getRepository(fileUri);
742744
if (repositoryForFile) {
743-
const relativePath = pathLib.relative(repositoryForFile.rootUri.fsPath, ref.value.fsPath);
744-
parts.push(` - ${relativePath}`);
745+
const relativePath = pathLib.relative(repositoryForFile.rootUri.fsPath, fileUri.fsPath);
746+
if (repositoryForFile.state.workingTreeChanges.some(change => change.uri.fsPath === fileUri.fsPath)) {
747+
try {
748+
// TODO: Consider just showing the file diffs
749+
const document = await vscode.workspace.openTextDocument(fileUri);
750+
const content = document.getText();
751+
fullFileParts.push(`<file-start>${relativePath}</file-start>`);
752+
fullFileParts.push(content);
753+
fullFileParts.push(`<file-end>${relativePath}</file-end>`);
754+
} catch (error) {
755+
this.logService.error(`Error reading file content for reference: ${fileUri.toString()}: ${error}`);
756+
}
757+
} else {
758+
fileRefs.push(` - ${relativePath}`);
759+
}
760+
}
761+
} else if (ref.value instanceof vscode.Uri && ref.value.scheme === 'untitled') {
762+
// Get full content of untitled file
763+
try {
764+
const document = await vscode.workspace.openTextDocument(ref.value);
765+
const content = document.getText();
766+
fullFileParts.push(`<file-start>${ref.value.path}</file-start>`);
767+
fullFileParts.push(content);
768+
fullFileParts.push(`<file-end>${ref.value.path}</file-end>`);
769+
} catch (error) {
770+
this.logService.error(`Error reading untitled file content for reference: ${ref.value.toString()}: ${error}`);
745771
}
746772
}
747773
}
748774

775+
const parts: string[] = [
776+
...(fullFileParts.length ? ['The user has attached the following uncommitted or modified files as relevant context:', ...fullFileParts] : []),
777+
...(fileRefs.length ? ['The user has attached the following file paths as relevant context:', ...fileRefs] : [])
778+
];
779+
749780
if (!parts.length) {
750781
return;
751782
}
752-
753-
parts.unshift('The user has attached the following files as relevant context:');
754783
return parts.join('\n');
755784
}
756785

0 commit comments

Comments
 (0)