-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Add support for organization custom instructions #2310
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
pwang347
wants to merge
14
commits into
main
Choose a base branch
from
pawang/customAgentProviderFollowups
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
14 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 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
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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.UseOrganizationInstructions)) { | ||
| 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 === this.getCacheFilename(orgLogin)) { | ||
| 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; | ||
| } | ||
| } |
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.
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.