Skip to content
Merged
Show file tree
Hide file tree
Changes from 48 commits
Commits
Show all changes
52 commits
Select commit Hold shift + click to select a range
62d571f
PR
pwang347 Dec 1, 2025
e552013
activation
pwang347 Dec 1, 2025
94e111e
fix test
pwang347 Dec 1, 2025
b539e0e
wip
pwang347 Dec 1, 2025
e2e9c04
update
pwang347 Dec 1, 2025
b8e5a48
Merge branch 'main' into pawang/customAgentProviderFollowups
pwang347 Dec 1, 2025
0c9900b
tests
pwang347 Dec 1, 2025
654bb5a
Merge branch 'pawang/customAgentProviderFollowups' of https://github.…
pwang347 Dec 1, 2025
22808a2
Update src/platform/github/common/githubService.ts
pwang347 Dec 1, 2025
58f0b98
Update src/extension/agents/vscode-node/organizationInstructionsProvi…
pwang347 Dec 1, 2025
8b4ca51
Update src/platform/github/common/octoKitServiceImpl.ts
pwang347 Dec 1, 2025
7e4d5b7
update
pwang347 Dec 1, 2025
9a903e7
Merge branch 'pawang/customAgentProviderFollowups' of https://github.…
pwang347 Dec 1, 2025
ac46114
update setting name
pwang347 Dec 1, 2025
c60ca8c
Merge branch 'main' of github.com:microsoft/vscode-copilot-chat into …
pwang347 Dec 31, 2025
1def2e7
wip
pwang347 Dec 31, 2025
da7039c
fix
pwang347 Dec 31, 2025
8697883
use enum
pwang347 Dec 31, 2025
28e810d
Merge branch 'main' of github.com:microsoft/vscode-copilot-chat into …
pwang347 Jan 7, 2026
e62f583
PR
pwang347 Jan 7, 2026
9a81ff6
nit
pwang347 Jan 7, 2026
147b0f0
add polling
pwang347 Jan 7, 2026
bf255df
tests
pwang347 Jan 7, 2026
1ab4167
cleanup
pwang347 Jan 7, 2026
c926baa
use helper
pwang347 Jan 7, 2026
24f3e2e
remove instructions
pwang347 Jan 7, 2026
d00db38
clean
pwang347 Jan 7, 2026
df9e168
Revert "clean"
pwang347 Jan 7, 2026
7f458c3
Revert "remove instructions"
pwang347 Jan 7, 2026
440569b
update
pwang347 Jan 7, 2026
ec91563
update per discussion
pwang347 Jan 21, 2026
01de92d
Merge branch 'main' of https://github.com/microsoft/vscode-copilot-ch…
pwang347 Jan 21, 2026
1fcf5ca
update
pwang347 Jan 21, 2026
cdf12d4
clean
pwang347 Jan 21, 2026
e4fcfc8
update
pwang347 Jan 21, 2026
fc053ff
update
pwang347 Jan 21, 2026
5605716
Update
pwang347 Jan 21, 2026
4e223db
update
pwang347 Jan 22, 2026
6c64f4c
update
pwang347 Jan 22, 2026
60f0767
clean
pwang347 Jan 22, 2026
8e35fac
test
pwang347 Jan 22, 2026
cb1cdd9
test
pwang347 Jan 22, 2026
407775b
fixes
pwang347 Jan 22, 2026
0cef17d
fixes
pwang347 Jan 22, 2026
22b8164
Merge branch 'main' of https://github.com/microsoft/vscode-copilot-ch…
pwang347 Jan 22, 2026
8ffdb6d
fix tests
pwang347 Jan 22, 2026
04244ce
update test
pwang347 Jan 22, 2026
615c57f
yaml
pwang347 Jan 22, 2026
172623b
fix
pwang347 Jan 22, 2026
a237c64
config update
pwang347 Jan 22, 2026
eda2c39
clean
pwang347 Jan 22, 2026
352d988
fix
pwang347 Jan 22, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@
"onUri",
"onFileSystem:ccreq",
"onFileSystem:ccsettings",
"onCustomAgentProvider"
"onCustomAgentProvider",
"onInstructionsProvider"
],
"main": "./dist/extension",
"l10n": "./l10n",
Expand Down Expand Up @@ -2836,6 +2837,11 @@
"default": true,
"description": "%github.copilot.config.customInstructionsInSystemMessage%"
},
"github.copilot.chat.customInstructions.useOrganizationInstructions": {
"type": "boolean",
"default": true,
"description": "%github.copilot.config.customInstructions.useOrganizationInstructions%"
},
"github.copilot.chat.agent.currentEditorContext.enabled": {
"type": "boolean",
"default": true,
Expand Down Expand Up @@ -3645,11 +3651,9 @@
},
"github.copilot.chat.customAgents.showOrganizationAndEnterpriseAgents": {
"type": "boolean",
"default": false,
"default": true,
"description": "%github.copilot.config.customAgents.showOrganizationAndEnterpriseAgents%",
"tags": [
"experimental"
]
"tags": []
}
}
},
Expand Down
1 change: 1 addition & 0 deletions package.nls.json
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,7 @@
"github.copilot.config.agent.currentEditorContext.enabled": "When enabled, Copilot will include the name of the current active editor in the context for agent mode.",
"github.copilot.config.customInstructionsInSystemMessage": "When enabled, custom instructions and mode instructions will be appended to the system message instead of a user message.",
"github.copilot.config.customAgents.showOrganizationAndEnterpriseAgents": "Enable custom agents from GitHub Enterprise and Organizations. When disabled, custom agents from your organization or enterprise will not be available in Copilot.",
"github.copilot.config.customInstructions.useOrganizationInstructions": "Enable custom instructions from GitHub Organizations. When disabled, custom instructions from your organization will not be available in Copilot.",
"copilot.toolSet.editing.description": "Edit files in your workspace",
"copilot.toolSet.read.description": "Read files in your workspace",
"copilot.toolSet.search.description": "Search files in your workspace",
Expand Down
332 changes: 332 additions & 0 deletions src/extension/agents/vscode-node/githubOrgChatResourcesService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,332 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import * as vscode from 'vscode';
import { AGENT_FILE_EXTENSION, INSTRUCTION_FILE_EXTENSION, PromptsType } from '../../../platform/customInstructions/common/promptTypes';
import { IVSCodeExtensionContext } from '../../../platform/extContext/common/extensionContext';
import { IFileSystemService } from '../../../platform/filesystem/common/fileSystemService';
import { FileType } from '../../../platform/filesystem/common/fileTypes';
import { getGithubRepoIdFromFetchUrl, IGitService } from '../../../platform/git/common/gitService';
import { IOctoKitService } from '../../../platform/github/common/githubService';
import { ILogService } from '../../../platform/log/common/logService';
import { IWorkspaceService } from '../../../platform/workspace/common/workspaceService';
import { Disposable, DisposableStore, IDisposable, toDisposable } from '../../../util/vs/base/common/lifecycle';
import { createDecorator } from '../../../util/vs/platform/instantiation/common/instantiation';

export interface IGitHubOrgChatResourcesService extends IDisposable {
/**
* Returns the organization that should be used for the current session.
*/
getPreferredOrganizationName(): Promise<string | undefined>;

/**
* Creates a polling subscription with a custom interval.
* The callback will be invoked at the specified interval.
* @param intervalMs The polling interval in milliseconds
* @param callback The callback to invoke on each poll cycle
* @returns A disposable that stops the polling when disposed
*/
startPolling(intervalMs: number, callback: (orgName: string) => Promise<void>): IDisposable;

/**
* Reads a specific cached resource.
* @returns The content of the resource, or undefined if not found
*/
readCacheFile(type: PromptsType, orgName: string, filename: string): Promise<string | undefined>;

/**
* Writes a resource to the cache.
* @returns True if the content was changed, false if unchanged
*/
writeCacheFile(type: PromptsType, orgName: string, filename: string, content: string, options?: { checkForChanges?: boolean }): Promise<boolean>;

/**
* Deletes all cached resources of specified type for an organization.
* Optionally provide set of filenames to exclude from deletion.
*/
clearCache(type: PromptsType, orgName: string, exclude?: Set<string>): Promise<void>;

/**
* Lists all cached resources for a specific organization and type.
* @returns The list of cached resources.
*/
listCachedFiles(type: PromptsType, orgName: string): Promise<vscode.ChatResource[]>;
}

export const IGitHubOrgChatResourcesService = createDecorator<IGitHubOrgChatResourcesService>('IGitHubPromptFileService');

/**
* Maps PromptsType to the cache subdirectory name.
*/
function getCacheSubdirectory(type: PromptsType): string {
switch (type) {
case PromptsType.instructions:
return 'instructions';
case PromptsType.agent:
return 'agents';
default:
throw new Error(`Unsupported PromptsType: ${type}`);
}
}

/**
* Returns true if the filename is valid for the given PromptsType.
*/
function isValidFile(type: PromptsType, fileName: string): boolean {
switch (type) {
case PromptsType.instructions:
return fileName.endsWith(INSTRUCTION_FILE_EXTENSION);
case PromptsType.agent:
return fileName.endsWith(AGENT_FILE_EXTENSION);
default:
throw new Error(`Unsupported PromptsType: ${type}`);
}
}

export class GitHubOrgChatResourcesService extends Disposable implements IGitHubOrgChatResourcesService {
private static readonly CACHE_ROOT = 'github';

private readonly _pollingSubscriptions = this._register(new DisposableStore());
private _cachedPreferredOrgName: Promise<string | undefined> | undefined;

constructor(
@IVSCodeExtensionContext private readonly extensionContext: IVSCodeExtensionContext,
@IFileSystemService private readonly fileSystem: IFileSystemService,
@IGitService private readonly gitService: IGitService,
@ILogService private readonly logService: ILogService,
@IOctoKitService private readonly octoKitService: IOctoKitService,
@IWorkspaceService private readonly workspaceService: IWorkspaceService,
) {
super();

// Invalidate cached org name when workspace folders change
this._register(this.workspaceService.onDidChangeWorkspaceFolders(() => {
this.logService.trace('[GitHubOrgChatResourcesService] Workspace folders changed, invalidating cached org name');
this._cachedPreferredOrgName = undefined;
}));
}

async getPreferredOrganizationName(): Promise<string | undefined> {
if (!this._cachedPreferredOrgName) {
this._cachedPreferredOrgName = this.computePreferredOrganizationName();
}
return this._cachedPreferredOrgName;
}

private async computePreferredOrganizationName(): Promise<string | undefined> {
// Check if user is signed in first
const currentUser = await this.octoKitService.getCurrentAuthedUser();
if (!currentUser) {
this.logService.trace('[GitHubOrgChatResourcesService] User is not signed in');
return undefined;
}

// Get the organizations the user is a member of
let userOrganizations: string[];
try {
userOrganizations = await this.octoKitService.getUserOrganizations({ createIfNone: true });
if (userOrganizations.length === 0) {
this.logService.trace('[GitHubOrgChatResourcesService] No organizations found for user');
return undefined;
}
} catch (error) {
this.logService.error(`[GitHubOrgChatResourcesService] Error getting user organizations: ${error}`);
return undefined;
}

// Check if workspace repo belongs to an organization the user is a member of
const workspaceOrg = await this.getWorkspaceRepositoryOrganization();
if (workspaceOrg && userOrganizations.includes(workspaceOrg)) {
return workspaceOrg;
}

// Fall back to the first organization the user belongs to
return userOrganizations[0];
}

/**
* Gets the organization from the current workspace's git repository, if any.
*/
private async getWorkspaceRepositoryOrganization(): Promise<string | undefined> {
const workspaceFolders = this.workspaceService.getWorkspaceFolders();
if (workspaceFolders.length === 0) {
return undefined;
}

try {
// TODO: Support multi-root workspaces by checking all folders.
// This would need workspace-aware context for deciding when to use which org, which is currently not in scope.
const repoInfo = await this.gitService.getRepositoryFetchUrls(workspaceFolders[0]);
if (!repoInfo?.remoteFetchUrls?.length) {
return undefined;
}

// Try each remote URL to find a GitHub repo
for (const fetchUrl of repoInfo.remoteFetchUrls) {
if (!fetchUrl) {
continue;
}
const repoId = getGithubRepoIdFromFetchUrl(fetchUrl);
if (repoId) {
this.logService.trace(`[GitHubOrgChatResourcesService] Found GitHub repo: ${repoId.org}/${repoId.repo}`);
return repoId.org;
}
}
} catch (error) {
this.logService.trace(`[GitHubOrgChatResourcesService] Error getting workspace repository: ${error}`);
}

return undefined;
}

startPolling(intervalMs: number, callback: (orgName: string) => Promise<void>): IDisposable {
const disposables = new DisposableStore();

let isPolling = false;
const poll = async () => {
if (isPolling) {
return;
}
isPolling = true;
try {
const orgName = await this.getPreferredOrganizationName();
if (orgName) {
try {
await callback(orgName);
} catch (error) {
this.logService.error(`[GitHubOrgChatResourcesService] Error in polling callback: ${error}`);
}
}
} finally {
isPolling = false;
}
};

// Initial poll
void poll();

// Set up interval polling
const intervalId = setInterval(() => poll(), intervalMs);
disposables.add(toDisposable(() => clearInterval(intervalId)));

this._pollingSubscriptions.add(disposables);

return disposables;
}

private getCacheDir(orgName: string, type: PromptsType): vscode.Uri {
const sanitizedOrg = this.sanitizeFilename(orgName);
const subdirectory = getCacheSubdirectory(type);
return vscode.Uri.joinPath(
this.extensionContext.globalStorageUri,
GitHubOrgChatResourcesService.CACHE_ROOT,
sanitizedOrg,
subdirectory
);
}

private getCacheFileUri(orgName: string, type: PromptsType, filename: string): vscode.Uri {
return vscode.Uri.joinPath(this.getCacheDir(orgName, type), filename);
}

private sanitizeFilename(name: string): string {
return name.replace(/[^a-z0-9_-]/gi, '_').toLowerCase();
}

private async ensureCacheDir(orgName: string, type: PromptsType): Promise<void> {
const cacheDir = this.getCacheDir(orgName, type);
try {
await this.fileSystem.stat(cacheDir);
} catch {
// createDirectory should create parent directories recursively
await this.fileSystem.createDirectory(cacheDir);
}
}

async readCacheFile(type: PromptsType, orgName: string, filename: string): Promise<string | undefined> {
try {
const fileUri = this.getCacheFileUri(orgName, type, filename);
const content = await this.fileSystem.readFile(fileUri);
return new TextDecoder().decode(content);
} catch {
this.logService.error(`[GitHubOrgChatResourcesService] Cache file not found: ${filename}`);
return undefined;
}
}

async writeCacheFile(type: PromptsType, orgName: string, filename: string, content: string, options?: { checkForChanges?: boolean }): Promise<boolean> {
await this.ensureCacheDir(orgName, type);
const fileUri = this.getCacheFileUri(orgName, type, filename);
const contentBytes = new TextEncoder().encode(content);

// Check for changes if requested
let hasChanges = true;
if (options?.checkForChanges) {
try {
hasChanges = false;

// First check file size to avoid reading file if size differs
const stat = await this.fileSystem.stat(fileUri);
if (stat.size !== contentBytes.length) {
hasChanges = true;
}

// Sizes match, need to compare content
const existingContent = await this.fileSystem.readFile(fileUri);
const existingText = new TextDecoder().decode(existingContent);
if (existingText !== content) {
this.logService.trace(`[GitHubOrgChatResourcesService] Skipped writing cache file: ${fileUri.toString()}`);
hasChanges = true;
} else {
// Content is the same, no need to write
return false;
}
} catch {
// File doesn't exist, so we have changes
hasChanges = true;
}
}

await this.fileSystem.writeFile(fileUri, contentBytes);
this.logService.trace(`[GitHubOrgChatResourcesService] Wrote cache file: ${fileUri.toString()}`);
return hasChanges;
}

async clearCache(type: PromptsType, orgName: string, exclude?: Set<string>): Promise<void> {
const cacheDir = this.getCacheDir(orgName, type);

try {
const files = await this.fileSystem.readDirectory(cacheDir);
for (const [filename, fileType] of files) {
if (fileType === FileType.File && isValidFile(type, filename) && !exclude?.has(filename)) {
await this.fileSystem.delete(vscode.Uri.joinPath(cacheDir, filename));
this.logService.trace(`[GitHubOrgChatResourcesService] Deleted cache file: ${filename}`);
}
}
} catch {
// Directory might not exist
}
}

async listCachedFiles(type: PromptsType, orgName: string): Promise<vscode.ChatResource[]> {
const resources: vscode.ChatResource[] = [];
const cacheDir = this.getCacheDir(orgName, type);

try {
const files = await this.fileSystem.readDirectory(cacheDir);
for (const [filename, fileType] of files) {
if (fileType === FileType.File && isValidFile(type, filename)) {
const fileUri = vscode.Uri.joinPath(cacheDir, filename);
resources.push({ uri: fileUri });
}
}
} catch {
// Directory might not exist yet
this.logService.trace(`[GitHubOrgChatResourcesService] Cache directory does not exist: ${cacheDir.toString()}`);
}

return resources;
}
}
Loading
Loading