Path: blob/main/extensions/copilot/src/extension/onboardDebug/node/languageToolsProvider.tsx
13399 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import { BasePromptElementProps, PromptElement, PromptPiece, PromptSizing, SystemMessage, TextChunk, UserMessage } from '@vscode/prompt-tsx';6import { ChatFetchResponseType, ChatLocation } from '../../../platform/chat/common/commonTypes';7import { IEndpointProvider } from '../../../platform/endpoint/common/endpointProvider';8import { createServiceIdentifier } from '../../../util/common/services';9import { CancellationToken } from '../../../util/vs/base/common/cancellation';10import { IInstantiationService } from '../../../util/vs/platform/instantiation/common/instantiation';11import { PromptRenderer } from '../../prompts/node/base/promptRenderer';1213// Extracts the "yam" from "- yam" or "yam" or "* yam" 🍠14const LIST_RE = /\s*(?:. )?([a-z0-9_-]+)\s*/;1516export interface ILanguageToolsProvider {17readonly _serviceBrand: undefined;1819getToolsForLanguages(languages: string[], token: CancellationToken): Promise<{ ok: boolean; commands: string[] }>;20}2122export const ILanguageToolsProvider = createServiceIdentifier<ILanguageToolsProvider>('ILanguageToolsProvider');2324export class LanguageToolsProvider {25constructor(26@IEndpointProvider private readonly endpointProvider: IEndpointProvider,27@IInstantiationService private readonly instantiationService: IInstantiationService28) {29}3031public async getToolsForLanguages(languages: string[], token: CancellationToken) {32const endpoint = await this.endpointProvider.getChatEndpoint('copilot-base');33const promptRenderer = PromptRenderer.create(34this.instantiationService,35endpoint,36ToolLanguagesPrompt,37{ languages }38);3940const prompt = await promptRenderer.render(undefined, token);41const fetchResult = await endpoint.makeChatRequest(42'debugCommandIdentifier',43prompt.messages,44undefined,45token,46ChatLocation.Other47);4849if (fetchResult.type !== ChatFetchResponseType.Success) {50return { ok: false, commands: [] };51}5253return {54ok: true,55commands: fetchResult.value56.split('\n')57.map(s => LIST_RE.exec(s)?.[1])58.filter((s): s is string => !!s),59};60}61}626364class ToolLanguagesPrompt extends PromptElement<{ languages: string[] } & BasePromptElementProps, void> {65override render(_state: void, _sizing: PromptSizing): PromptPiece {66return (67<>68<SystemMessage priority={10}>69You are an AI programming assistant that is specialized for usage of command-line tools developers use to build software.<br />70I'm working on software in the given following languages. Please list the names of common command-line tools I might use to build and test my software.<br />71Do NOT list tools that don't run my code, such as those used only for linting. For example, if I ask for JavaScript, the list should include tools like node, npx, and mocha, but not eslint.<br />72Be thorough! Try to give a list of *at least* 10 such tools.<br />73Print these tools out as a list, separated by commas. Do NOT print any additional explanation or context.74<br />75<TextChunk priority={8}>76# Example<br />77## User: <br />78- python<br />79- rust<br />80## Response:<br />81- python<br />82- pip<br />83- cargo<br />84- rustc<br />85</TextChunk>86</SystemMessage>87<UserMessage priority={9}>88<TextChunk breakOnWhitespace flexGrow={1}>89The languages I'm working in are:<br />90{this.props.languages.join('\n -')}91</TextChunk>92</UserMessage>93</>94);95}96}979899