-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Add Cpp Context Traits to Completions Prompt #12821
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
Merged
benmcmorran
merged 1 commit into
microsoft:main
from
kuchungmsft:kuchung/ProvideCppContextToCompletionPrompts
Oct 14, 2024
+512
−82
Merged
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,134 @@ | ||
| /* -------------------------------------------------------------------------------------------- | ||
| * Copyright (c) Microsoft Corporation. All Rights Reserved. | ||
| * See 'LICENSE' in the project root for license information. | ||
| * ------------------------------------------------------------------------------------------ */ | ||
| 'use strict'; | ||
|
|
||
| import * as vscode from 'vscode'; | ||
| import * as util from '../common'; | ||
| import * as telemetry from '../telemetry'; | ||
| import { ChatContextResult, GetIncludesResult } from './client'; | ||
| import { getActiveClient } from './extension'; | ||
|
|
||
| let isRelatedFilesApiEnabled: boolean | undefined; | ||
|
|
||
| export interface CopilotTrait { | ||
| name: string; | ||
| value: string; | ||
| includeInPrompt?: boolean; | ||
| promptTextOverride?: string; | ||
| } | ||
|
|
||
| export interface CopilotApi { | ||
| registerRelatedFilesProvider( | ||
| providerId: { extensionId: string; languageId: string }, | ||
| callback: ( | ||
| uri: vscode.Uri, | ||
| context: { flags: Record<string, unknown> }, | ||
| cancellationToken: vscode.CancellationToken | ||
| ) => Promise<{ entries: vscode.Uri[]; traits?: CopilotTrait[] }> | ||
| ): Disposable; | ||
| } | ||
|
|
||
| export async function registerRelatedFilesProvider(): Promise<void> { | ||
| if (!await getIsRelatedFilesApiEnabled()) { | ||
| return; | ||
| } | ||
|
|
||
| const api = await getCopilotApi(); | ||
| if (util.extensionContext && api) { | ||
| try { | ||
| for (const languageId of ['c', 'cpp', 'cuda-cpp']) { | ||
| api.registerRelatedFilesProvider( | ||
| { extensionId: util.extensionContext.extension.id, languageId }, | ||
| async (_uri: vscode.Uri, context: { flags: Record<string, unknown> }, token: vscode.CancellationToken) => { | ||
|
|
||
| const getIncludesHandler = async () => (await getIncludesWithCancellation(1, token))?.includedFiles.map(file => vscode.Uri.file(file)) ?? []; | ||
| const getTraitsHandler = async () => { | ||
| const chatContext: ChatContextResult | undefined = await (getActiveClient().getChatContext(token) ?? undefined); | ||
|
|
||
| if (!chatContext) { | ||
| return undefined; | ||
| } | ||
|
|
||
| let traits: CopilotTrait[] = [ | ||
| { name: "language", value: chatContext.language, includeInPrompt: true, promptTextOverride: `The language is ${chatContext.language}.` }, | ||
kuchungmsft marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| { name: "compiler", value: chatContext.compiler, includeInPrompt: true, promptTextOverride: `This project compiles using ${chatContext.compiler}.` }, | ||
| { name: "standardVersion", value: chatContext.standardVersion, includeInPrompt: true, promptTextOverride: `This project uses the ${chatContext.standardVersion} language standard.` }, | ||
| { name: "targetPlatform", value: chatContext.targetPlatform, includeInPrompt: true, promptTextOverride: `This build targets ${chatContext.targetPlatform}.` }, | ||
| { name: "targetArchitecture", value: chatContext.targetArchitecture, includeInPrompt: true, promptTextOverride: `This build targets ${chatContext.targetArchitecture}.` } | ||
| ]; | ||
|
|
||
| const excludeTraits = context.flags.copilotcppExcludeTraits as string[] ?? []; | ||
| traits = traits.filter(trait => !excludeTraits.includes(trait.name)); | ||
|
|
||
| return traits.length > 0 ? traits : undefined; | ||
| }; | ||
|
|
||
| // Call both handlers in parallel | ||
| const traitsPromise = ((context.flags.copilotcppTraits as boolean) ?? false) ? getTraitsHandler() : Promise.resolve(undefined); | ||
| const includesPromise = getIncludesHandler(); | ||
|
|
||
| return { entries: await includesPromise, traits: await traitsPromise }; | ||
| } | ||
| ); | ||
| } | ||
| } catch { | ||
| console.log("Failed to register Copilot related files provider."); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| export async function registerRelatedFilesCommands(commandDisposables: vscode.Disposable[], enabled: boolean): Promise<void> { | ||
| if (await getIsRelatedFilesApiEnabled()) { | ||
| commandDisposables.push(vscode.commands.registerCommand('C_Cpp.getIncludes', enabled ? (maxDepth: number) => getIncludes(maxDepth) : () => Promise.resolve())); | ||
| } | ||
| } | ||
|
|
||
| async function getIncludesWithCancellation(maxDepth: number, token: vscode.CancellationToken): Promise<GetIncludesResult> { | ||
| const activeClient = getActiveClient(); | ||
| const includes = await activeClient.getIncludes(maxDepth, token); | ||
| const wksFolder = activeClient.RootUri?.toString(); | ||
|
|
||
| if (!wksFolder) { | ||
| return includes; | ||
| } | ||
|
|
||
| includes.includedFiles = includes.includedFiles.filter(header => vscode.Uri.file(header).toString().startsWith(wksFolder)); | ||
| return includes; | ||
| } | ||
|
|
||
| async function getIncludes(maxDepth: number): Promise<GetIncludesResult> { | ||
| const tokenSource = new vscode.CancellationTokenSource(); | ||
| try { | ||
| const includes = await getIncludesWithCancellation(maxDepth, tokenSource.token); | ||
| return includes; | ||
| } finally { | ||
| tokenSource.dispose(); | ||
| } | ||
| } | ||
|
|
||
| async function getIsRelatedFilesApiEnabled(): Promise<boolean> { | ||
| if (isRelatedFilesApiEnabled === undefined) { | ||
| isRelatedFilesApiEnabled = await telemetry.isExperimentEnabled("CppToolsRelatedFilesApi"); | ||
| } | ||
|
|
||
| return isRelatedFilesApiEnabled; | ||
| } | ||
|
|
||
| export async function getCopilotApi(): Promise<CopilotApi | undefined> { | ||
| const copilotExtension = vscode.extensions.getExtension<CopilotApi>('github.copilot'); | ||
| if (!copilotExtension) { | ||
| return undefined; | ||
| } | ||
|
|
||
| if (!copilotExtension.isActive) { | ||
| try { | ||
| return await copilotExtension.activate(); | ||
| } catch { | ||
| return undefined; | ||
| } | ||
| } else { | ||
| return copilotExtension.exports; | ||
| } | ||
| } | ||
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
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.