This repository was archived by the owner on May 20, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2k
Add support for organization custom instructions #2310
Merged
Merged
Changes from 8 commits
Commits
Show all changes
52 commits
Select commit
Hold shift + click to select a range
62d571f
PR
pwang347 e552013
activation
pwang347 94e111e
fix test
pwang347 b539e0e
wip
pwang347 e2e9c04
update
pwang347 b8e5a48
Merge branch 'main' into pawang/customAgentProviderFollowups
pwang347 0c9900b
tests
pwang347 654bb5a
Merge branch 'pawang/customAgentProviderFollowups' of https://github.…
pwang347 22808a2
Update src/platform/github/common/githubService.ts
pwang347 58f0b98
Update src/extension/agents/vscode-node/organizationInstructionsProvi…
pwang347 8b4ca51
Update src/platform/github/common/octoKitServiceImpl.ts
pwang347 7e4d5b7
update
pwang347 9a903e7
Merge branch 'pawang/customAgentProviderFollowups' of https://github.…
pwang347 ac46114
update setting name
pwang347 c60ca8c
Merge branch 'main' of github.com:microsoft/vscode-copilot-chat into …
pwang347 1def2e7
wip
pwang347 da7039c
fix
pwang347 8697883
use enum
pwang347 28e810d
Merge branch 'main' of github.com:microsoft/vscode-copilot-chat into …
pwang347 e62f583
PR
pwang347 9a81ff6
nit
pwang347 147b0f0
add polling
pwang347 bf255df
tests
pwang347 1ab4167
cleanup
pwang347 c926baa
use helper
pwang347 24f3e2e
remove instructions
pwang347 d00db38
clean
pwang347 df9e168
Revert "clean"
pwang347 7f458c3
Revert "remove instructions"
pwang347 440569b
update
pwang347 ec91563
update per discussion
pwang347 01de92d
Merge branch 'main' of https://github.com/microsoft/vscode-copilot-ch…
pwang347 1fcf5ca
update
pwang347 cdf12d4
clean
pwang347 e4fcfc8
update
pwang347 fc053ff
update
pwang347 5605716
Update
pwang347 4e223db
update
pwang347 6c64f4c
update
pwang347 60f0767
clean
pwang347 8e35fac
test
pwang347 cb1cdd9
test
pwang347 407775b
fixes
pwang347 0cef17d
fixes
pwang347 22b8164
Merge branch 'main' of https://github.com/microsoft/vscode-copilot-ch…
pwang347 8ffdb6d
fix tests
pwang347 04244ce
update test
pwang347 615c57f
yaml
pwang347 172623b
fix
pwang347 a237c64
config update
pwang347 eda2c39
clean
pwang347 352d988
fix
pwang347 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
src/extension/agents/vscode-node/organizationInstructionsContrib.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| /*--------------------------------------------------------------------------------------------- | ||
| * 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 { ConfigKey, IConfigurationService } from '../../../platform/configuration/common/configurationService'; | ||
| import { Disposable } from '../../../util/vs/base/common/lifecycle'; | ||
| import { IInstantiationService } from '../../../util/vs/platform/instantiation/common/instantiation'; | ||
| import { IExtensionContribution } from '../../common/contributions'; | ||
| import { OrganizationInstructionsProvider } from './organizationInstructionsProvider'; | ||
|
|
||
| export class OrganizationInstructionsContribution extends Disposable implements IExtensionContribution { | ||
| readonly id = 'OrganizationInstructions'; | ||
|
|
||
| constructor( | ||
| @IInstantiationService instantiationService: IInstantiationService, | ||
| @IConfigurationService configurationService: IConfigurationService, | ||
| ) { | ||
| super(); | ||
|
|
||
| if ('registerInstructionsProvider' in vscode.chat) { | ||
| // Only register the provider if the setting is enabled | ||
| if (configurationService.getConfig(ConfigKey.ShowOrganizationInstructions)) { | ||
| const provider = instantiationService.createInstance(OrganizationInstructionsProvider); | ||
| this._register(vscode.chat.registerInstructionsProvider(provider)); | ||
| } | ||
| } | ||
| } | ||
| } |
179 changes: 179 additions & 0 deletions
179
src/extension/agents/vscode-node/organizationInstructionsProvider.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,179 @@ | ||
| /*--------------------------------------------------------------------------------------------- | ||
| * 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 { IVSCodeExtensionContext } from '../../../platform/extContext/common/extensionContext'; | ||
| import { IFileSystemService } from '../../../platform/filesystem/common/fileSystemService'; | ||
| import { FileType } from '../../../platform/filesystem/common/fileTypes'; | ||
| import { IGitService } from '../../../platform/git/common/gitService'; | ||
| import { IOctoKitService } from '../../../platform/github/common/githubService'; | ||
| import { ILogService } from '../../../platform/log/common/logService'; | ||
| import { Disposable } from '../../../util/vs/base/common/lifecycle'; | ||
| import { getRepoId } from '../../chatSessions/vscode/copilotCodingAgentUtils'; | ||
|
|
||
| const InstructionFileExtension = '.instruction.md'; | ||
|
|
||
| export class OrganizationInstructionsProvider extends Disposable implements vscode.InstructionsProvider { | ||
|
|
||
| private readonly _onDidChangeInstructions = this._register(new vscode.EventEmitter<void>()); | ||
| readonly onDidChangeInstructions = this._onDidChangeInstructions.event; | ||
|
|
||
| private isFetching = false; | ||
|
|
||
| constructor( | ||
| @IOctoKitService private readonly octoKitService: IOctoKitService, | ||
| @ILogService private readonly logService: ILogService, | ||
| @IGitService private readonly gitService: IGitService, | ||
| @IVSCodeExtensionContext readonly extensionContext: IVSCodeExtensionContext, | ||
| @IFileSystemService private readonly fileSystem: IFileSystemService, | ||
| ) { | ||
| super(); | ||
| } | ||
|
|
||
| private getCacheDir(): vscode.Uri | undefined { | ||
| if (!this.extensionContext.storageUri) { | ||
| return; | ||
| } | ||
| return vscode.Uri.joinPath(this.extensionContext.storageUri, 'githubInstructionsCache'); | ||
| } | ||
|
|
||
| private getCacheFilename(orgLogin: string): string { | ||
| return orgLogin + InstructionFileExtension; | ||
| } | ||
|
|
||
| async provideInstructions( | ||
| options: vscode.InstructionQueryOptions, | ||
| _token: vscode.CancellationToken | ||
| ): Promise<vscode.CustomAgentResource[]> { | ||
| try { | ||
| // Get repository information from the active git repository | ||
| const repoId = await getRepoId(this.gitService); | ||
| if (!repoId) { | ||
| this.logService.trace('[OrganizationInstructionsProvider] No active repository found'); | ||
| return []; | ||
| } | ||
|
|
||
| const orgLogin = repoId.org; | ||
|
|
||
| // Read from cache first | ||
| const cachedInstructions = await this.readFromCache(orgLogin); | ||
|
|
||
| // Trigger async fetch to update cache | ||
| this.fetchAndUpdateCache(orgLogin, options).catch(error => { | ||
| this.logService.error(`[OrganizationInstructionsProvider] Error in background fetch: ${error}`); | ||
| }); | ||
|
|
||
| return cachedInstructions; | ||
| } catch (error) { | ||
| this.logService.error(`[OrganizationInstructionsProvider] Error in provideInstructions: ${error}`); | ||
| return []; | ||
| } | ||
| } | ||
|
|
||
| private async readFromCache( | ||
| orgLogin: string, | ||
| ): Promise<vscode.CustomAgentResource[]> { | ||
| try { | ||
| const cacheDir = this.getCacheDir(); | ||
| if (!cacheDir) { | ||
| this.logService.trace('[OrganizationInstructionsProvider] No workspace open, cannot use cache'); | ||
| return []; | ||
| } | ||
|
|
||
| const cacheContents = await this.readCacheContents(orgLogin, cacheDir); | ||
| if (cacheContents === undefined) { | ||
| this.logService.trace(`[OrganizationInstructionsProvider] No cache found for org ${orgLogin}`); | ||
| return []; | ||
| } | ||
|
|
||
| const instructions: vscode.CustomAgentResource[] = []; | ||
| const fileName = this.getCacheFilename(orgLogin); | ||
| const fileUri = vscode.Uri.joinPath(cacheDir, fileName); | ||
| instructions.push({ | ||
| name: orgLogin, | ||
| description: '', | ||
| uri: fileUri, | ||
| }); | ||
|
|
||
| this.logService.trace(`[OrganizationInstructionsProvider] Loaded ${instructions.length} instructions from cache for org ${orgLogin}`); | ||
| return instructions; | ||
| } catch (error) { | ||
| this.logService.error(`[OrganizationInstructionsProvider] Error reading from cache: ${error}`); | ||
| return []; | ||
| } | ||
| } | ||
|
|
||
| private async fetchAndUpdateCache( | ||
| orgLogin: string, | ||
| options: vscode.InstructionQueryOptions | ||
| ): Promise<void> { | ||
| // Prevent concurrent fetches | ||
| if (this.isFetching) { | ||
| this.logService.trace('[OrganizationInstructionsProvider] Fetch already in progress, skipping'); | ||
| return; | ||
| } | ||
|
|
||
| this.isFetching = true; | ||
| try { | ||
| this.logService.trace(`[OrganizationInstructionsProvider] Fetching custom instructions for org ${orgLogin}`); | ||
|
|
||
| const instructions = await this.octoKitService.getOrgCustomInstructions(orgLogin); | ||
| const cacheDir = this.getCacheDir(); | ||
| if (!cacheDir) { | ||
| this.logService.trace('[OrganizationInstructionsProvider] No workspace open, cannot use cache'); | ||
| return; | ||
| } | ||
|
|
||
| if (!instructions) { | ||
| this.logService.trace(`[OrganizationInstructionsProvider] No custom instructions found for org ${orgLogin}`); | ||
| return; | ||
| } | ||
|
|
||
| // Ensure cache directory exists | ||
| try { | ||
| await this.fileSystem.stat(cacheDir); | ||
| } catch (error) { | ||
| // Directory doesn't exist, create it | ||
| await this.fileSystem.createDirectory(cacheDir); | ||
| } | ||
|
|
||
| const existingInstructions = await this.readCacheContents(orgLogin, cacheDir); | ||
| const hasChanges = instructions !== existingInstructions; | ||
|
|
||
| if (!hasChanges) { | ||
| this.logService.trace(`[OrganizationInstructionsProvider] No changes detected in cache for org ${orgLogin}`); | ||
| return; | ||
| } | ||
|
|
||
| const fileName = this.getCacheFilename(orgLogin); | ||
| const fileUri = vscode.Uri.joinPath(cacheDir, fileName); | ||
| await this.fileSystem.writeFile(fileUri, new TextEncoder().encode(instructions)); | ||
|
|
||
| this.logService.trace(`[OrganizationInstructionsProvider] Updated cache with instructions for org ${orgLogin}`); | ||
|
|
||
| // Fire event to notify consumers that instructions have changed | ||
| this._onDidChangeInstructions.fire(); | ||
| } finally { | ||
| this.isFetching = false; | ||
| } | ||
| } | ||
|
|
||
| private async readCacheContents(orgLogin: string, cacheDir: vscode.Uri): Promise<string | undefined> { | ||
| try { | ||
| const files = await this.fileSystem.readDirectory(cacheDir); | ||
| for (const [filename, fileType] of files) { | ||
| if (fileType === FileType.File && filename.endsWith(orgLogin + InstructionFileExtension)) { | ||
| const fileUri = vscode.Uri.joinPath(cacheDir, filename); | ||
| const content = await this.fileSystem.readFile(fileUri); | ||
| const text = new TextDecoder().decode(content); | ||
| return text; | ||
| } | ||
| } | ||
| } catch { | ||
| // Directory might not exist yet or other errors | ||
| } | ||
| return undefined; | ||
| } | ||
| } | ||
|
pwang347 marked this conversation as resolved.
Outdated
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.