diff --git a/Extension/package.json b/Extension/package.json index cad51c4dd2..47dacea175 100644 --- a/Extension/package.json +++ b/Extension/package.json @@ -6451,7 +6451,8 @@ "userDescription": "%c_cpp.languageModelTools.configuration.userDescription%", "modelDescription": "For the active C or C++ file, this tool provides: the language (C, C++, or CUDA), the language standard version (such as C++11, C++14, C++17, or C++20), the compiler (such as GCC, Clang, or MSVC), the target platform (such as x86, x64, or ARM), and the target architecture (such as 32-bit or 64-bit).", "icon": "$(file-code)", - "parametersSchema": {} + "parametersSchema": {}, + "when": "(config.C_Cpp.experimentalFeatures =~ /^[eE]nabled$/)" } ] }, diff --git a/Extension/src/LanguageServer/lmTool.ts b/Extension/src/LanguageServer/lmTool.ts index 65ac7d08cf..b9d92d2b77 100644 --- a/Extension/src/LanguageServer/lmTool.ts +++ b/Extension/src/LanguageServer/lmTool.ts @@ -5,7 +5,9 @@ 'use strict'; import * as vscode from 'vscode'; +import { localize } from 'vscode-nls'; import * as util from '../common'; +import * as logger from '../logger'; import * as telemetry from '../telemetry'; import { ChatContextResult } from './client'; import { getClients } from './extension'; @@ -55,33 +57,53 @@ export class CppConfigurationLanguageModelTool implements vscode.LanguageModelTo } private async getContext(token: vscode.CancellationToken): Promise { - const currentDoc = vscode.window.activeTextEditor?.document; - if (!currentDoc || (!util.isCpp(currentDoc) && !util.isHeaderFile(currentDoc.uri))) { - return 'The active document is not a C, C++, or CUDA file.'; - } + try + { + const currentDoc = vscode.window.activeTextEditor?.document; + if (!currentDoc || (!util.isCpp(currentDoc) && !util.isHeaderFile(currentDoc.uri))) { + return 'The active document is not a C, C++, or CUDA file.'; + } - const chatContext: ChatContextResult | undefined = await (getClients()?.ActiveClient?.getChatContext(token) ?? undefined); - if (!chatContext) { - return 'No configuration information is available for the active document.'; - } + const chatContext: ChatContextResult | undefined = await (getClients()?.ActiveClient?.getChatContext(token) ?? undefined); + if (!chatContext) { + return 'No configuration information is available for the active document.'; + } - telemetry.logLanguageModelToolEvent( - 'cpp', - { - "language": chatContext.language, - "compiler": chatContext.compiler, - "standardVersion": chatContext.standardVersion, - "targetPlatform": chatContext.targetPlatform, - "targetArchitecture": chatContext.targetArchitecture - }); + telemetry.logLanguageModelToolEvent( + 'cpp', + { + "language": chatContext.language, + "compiler": chatContext.compiler, + "standardVersion": chatContext.standardVersion, + "targetPlatform": chatContext.targetPlatform, + "targetArchitecture": chatContext.targetArchitecture + }); - for (const key in knownValues) { - const knownKey = key as keyof ChatContextResult; - if (knownValues[knownKey] && chatContext[knownKey]) { - chatContext[knownKey] = knownValues[knownKey][chatContext[knownKey]] || chatContext[knownKey]; + for (const key in knownValues) { + const knownKey = key as keyof ChatContextResult; + if (knownValues[knownKey] && chatContext[knownKey]) { + chatContext[knownKey] = knownValues[knownKey][chatContext[knownKey]] || chatContext[knownKey]; + } } + + return `The user is working on a ${chatContext.language} project. The project uses language version ${chatContext.standardVersion}, compiles using the ${chatContext.compiler} compiler, targets the ${chatContext.targetPlatform} platform, and targets the ${chatContext.targetArchitecture} architecture.`; } + catch (e: any) + { + await this.reportError(); + return ""; + } + } - return `The user is working on a ${chatContext.language} project. The project uses language version ${chatContext.standardVersion}, compiles using the ${chatContext.compiler} compiler, targets the ${chatContext.targetPlatform} platform, and targets the ${chatContext.targetArchitecture} architecture.`; + private async reportError(): Promise + { + try + { + logger.getOutputChannelLogger().appendLine(localize("copilot.cppcontext.error", "Error while retrieving the #cpp context.")); + } + catch + { + // Intentionally swallow any exception. + } } }