Path: blob/main/extensions/copilot/src/extension/prompts/node/agent/promptRegistry.ts
13405 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 } from '@vscode/prompt-tsx';6import type { IChatEndpoint } from '../../../../platform/networking/common/networking';7import { IInstantiationService } from '../../../../util/vs/platform/instantiation/common/instantiation';8import { CopilotIdentityRules } from '../base/copilotIdentity';9import { SafetyRules } from '../base/safetyRules';10import { DefaultAgentPrompt, DefaultAgentPromptProps, DefaultReminderInstructions, DefaultToolReferencesHint, ReminderInstructionsProps, ToolReferencesHintProps } from './defaultAgentInstructions';1112export type SystemPrompt = new (props: DefaultAgentPromptProps, ...args: any[]) => PromptElement<DefaultAgentPromptProps>;1314export type ReminderInstructionsConstructor = new (props: ReminderInstructionsProps, ...args: any[]) => PromptElement<ReminderInstructionsProps>;1516export type ToolReferencesHintConstructor = new (props: ToolReferencesHintProps, ...args: any[]) => PromptElement<ToolReferencesHintProps>;1718export type CopilotIdentityRulesConstructor = new (props: BasePromptElementProps, ...args: any[]) => PromptElement<BasePromptElementProps>;1920export type SafetyRulesConstructor = new (props: BasePromptElementProps, ...args: any[]) => PromptElement<BasePromptElementProps>;2122export interface IAgentPrompt {23resolveSystemPrompt(endpoint: IChatEndpoint): SystemPrompt | undefined;24resolveReminderInstructions?(endpoint: IChatEndpoint): ReminderInstructionsConstructor | undefined;25resolveToolReferencesHint?(endpoint: IChatEndpoint): ToolReferencesHintConstructor | undefined;26resolveCopilotIdentityRules?(endpoint: IChatEndpoint): CopilotIdentityRulesConstructor | undefined;27resolveSafetyRules?(endpoint: IChatEndpoint): SafetyRulesConstructor | undefined;28resolveUserQueryTagName?(endpoint: IChatEndpoint): string | undefined;29}3031export interface IAgentPromptCtor {32readonly familyPrefixes: readonly string[];33matchesModel?(endpoint: IChatEndpoint): Promise<boolean> | boolean;34new(...args: any[]): IAgentPrompt;35}3637export type AgentPromptClass = IAgentPromptCtor & (new (...args: any[]) => IAgentPrompt);3839type PromptWithMatcher = IAgentPromptCtor & {40matchesModel: (endpoint: IChatEndpoint) => Promise<boolean> | boolean;41};4243export interface AgentPromptCustomizations {44readonly SystemPrompt: SystemPrompt;45readonly ReminderInstructionsClass: ReminderInstructionsConstructor;46readonly ToolReferencesHintClass: ToolReferencesHintConstructor;47readonly CopilotIdentityRulesClass: CopilotIdentityRulesConstructor;48readonly SafetyRulesClass: SafetyRulesConstructor;49readonly userQueryTagName?: string;50}5152export const PromptRegistry = new class {53private readonly promptsWithMatcher: PromptWithMatcher[] = [];54private readonly familyPrefixList: { prefix: string; prompt: IAgentPromptCtor }[] = [];5556registerPrompt(prompt: IAgentPromptCtor): void {57if (prompt.matchesModel) {58this.promptsWithMatcher.push(prompt as PromptWithMatcher);59}6061for (const prefix of prompt.familyPrefixes) {62this.familyPrefixList.push({ prefix, prompt });63}64}6566private async getPromptResolver(67endpoint: IChatEndpoint68): Promise<IAgentPromptCtor | undefined> {6970for (const prompt of this.promptsWithMatcher) {71const matches = await prompt.matchesModel(endpoint);72if (matches) {73return prompt;74}75}7677for (const { prefix, prompt } of this.familyPrefixList) {78if (endpoint.family.startsWith(prefix)) {79return prompt;80}81}8283return undefined;84}8586/**87* Resolves all customizations from the prompt registry for a given endpoint.88* This is the main method to call to get all per-model customizations in one place.89* @param instantiationService The instantiation service to create the agent prompt instance90* @param endpoint The chat endpoint to resolve customizations for91* @returns All resolved customizations for the endpoint92*/93async resolveAllCustomizations(94instantiationService: IInstantiationService,95endpoint: IChatEndpoint,96): Promise<AgentPromptCustomizations> {97const promptResolverCtor = await this.getPromptResolver(endpoint);98const agentPrompt = promptResolverCtor ? instantiationService.createInstance(promptResolverCtor) : undefined;99100return {101SystemPrompt: agentPrompt?.resolveSystemPrompt(endpoint) ?? DefaultAgentPrompt,102ReminderInstructionsClass: agentPrompt?.resolveReminderInstructions?.(endpoint) ?? DefaultReminderInstructions,103ToolReferencesHintClass: agentPrompt?.resolveToolReferencesHint?.(endpoint) ?? DefaultToolReferencesHint,104CopilotIdentityRulesClass: agentPrompt?.resolveCopilotIdentityRules?.(endpoint) ?? CopilotIdentityRules,105SafetyRulesClass: agentPrompt?.resolveSafetyRules?.(endpoint) ?? SafetyRules,106userQueryTagName: agentPrompt?.resolveUserQueryTagName?.(endpoint) ?? 'userRequest',107};108}109}();110111112