Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/src/extension/prompts/node/agent/promptRegistry.ts
13405 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import { BasePromptElementProps, PromptElement } from '@vscode/prompt-tsx';
7
import type { IChatEndpoint } from '../../../../platform/networking/common/networking';
8
import { IInstantiationService } from '../../../../util/vs/platform/instantiation/common/instantiation';
9
import { CopilotIdentityRules } from '../base/copilotIdentity';
10
import { SafetyRules } from '../base/safetyRules';
11
import { DefaultAgentPrompt, DefaultAgentPromptProps, DefaultReminderInstructions, DefaultToolReferencesHint, ReminderInstructionsProps, ToolReferencesHintProps } from './defaultAgentInstructions';
12
13
export type SystemPrompt = new (props: DefaultAgentPromptProps, ...args: any[]) => PromptElement<DefaultAgentPromptProps>;
14
15
export type ReminderInstructionsConstructor = new (props: ReminderInstructionsProps, ...args: any[]) => PromptElement<ReminderInstructionsProps>;
16
17
export type ToolReferencesHintConstructor = new (props: ToolReferencesHintProps, ...args: any[]) => PromptElement<ToolReferencesHintProps>;
18
19
export type CopilotIdentityRulesConstructor = new (props: BasePromptElementProps, ...args: any[]) => PromptElement<BasePromptElementProps>;
20
21
export type SafetyRulesConstructor = new (props: BasePromptElementProps, ...args: any[]) => PromptElement<BasePromptElementProps>;
22
23
export interface IAgentPrompt {
24
resolveSystemPrompt(endpoint: IChatEndpoint): SystemPrompt | undefined;
25
resolveReminderInstructions?(endpoint: IChatEndpoint): ReminderInstructionsConstructor | undefined;
26
resolveToolReferencesHint?(endpoint: IChatEndpoint): ToolReferencesHintConstructor | undefined;
27
resolveCopilotIdentityRules?(endpoint: IChatEndpoint): CopilotIdentityRulesConstructor | undefined;
28
resolveSafetyRules?(endpoint: IChatEndpoint): SafetyRulesConstructor | undefined;
29
resolveUserQueryTagName?(endpoint: IChatEndpoint): string | undefined;
30
}
31
32
export interface IAgentPromptCtor {
33
readonly familyPrefixes: readonly string[];
34
matchesModel?(endpoint: IChatEndpoint): Promise<boolean> | boolean;
35
new(...args: any[]): IAgentPrompt;
36
}
37
38
export type AgentPromptClass = IAgentPromptCtor & (new (...args: any[]) => IAgentPrompt);
39
40
type PromptWithMatcher = IAgentPromptCtor & {
41
matchesModel: (endpoint: IChatEndpoint) => Promise<boolean> | boolean;
42
};
43
44
export interface AgentPromptCustomizations {
45
readonly SystemPrompt: SystemPrompt;
46
readonly ReminderInstructionsClass: ReminderInstructionsConstructor;
47
readonly ToolReferencesHintClass: ToolReferencesHintConstructor;
48
readonly CopilotIdentityRulesClass: CopilotIdentityRulesConstructor;
49
readonly SafetyRulesClass: SafetyRulesConstructor;
50
readonly userQueryTagName?: string;
51
}
52
53
export const PromptRegistry = new class {
54
private readonly promptsWithMatcher: PromptWithMatcher[] = [];
55
private readonly familyPrefixList: { prefix: string; prompt: IAgentPromptCtor }[] = [];
56
57
registerPrompt(prompt: IAgentPromptCtor): void {
58
if (prompt.matchesModel) {
59
this.promptsWithMatcher.push(prompt as PromptWithMatcher);
60
}
61
62
for (const prefix of prompt.familyPrefixes) {
63
this.familyPrefixList.push({ prefix, prompt });
64
}
65
}
66
67
private async getPromptResolver(
68
endpoint: IChatEndpoint
69
): Promise<IAgentPromptCtor | undefined> {
70
71
for (const prompt of this.promptsWithMatcher) {
72
const matches = await prompt.matchesModel(endpoint);
73
if (matches) {
74
return prompt;
75
}
76
}
77
78
for (const { prefix, prompt } of this.familyPrefixList) {
79
if (endpoint.family.startsWith(prefix)) {
80
return prompt;
81
}
82
}
83
84
return undefined;
85
}
86
87
/**
88
* Resolves all customizations from the prompt registry for a given endpoint.
89
* This is the main method to call to get all per-model customizations in one place.
90
* @param instantiationService The instantiation service to create the agent prompt instance
91
* @param endpoint The chat endpoint to resolve customizations for
92
* @returns All resolved customizations for the endpoint
93
*/
94
async resolveAllCustomizations(
95
instantiationService: IInstantiationService,
96
endpoint: IChatEndpoint,
97
): Promise<AgentPromptCustomizations> {
98
const promptResolverCtor = await this.getPromptResolver(endpoint);
99
const agentPrompt = promptResolverCtor ? instantiationService.createInstance(promptResolverCtor) : undefined;
100
101
return {
102
SystemPrompt: agentPrompt?.resolveSystemPrompt(endpoint) ?? DefaultAgentPrompt,
103
ReminderInstructionsClass: agentPrompt?.resolveReminderInstructions?.(endpoint) ?? DefaultReminderInstructions,
104
ToolReferencesHintClass: agentPrompt?.resolveToolReferencesHint?.(endpoint) ?? DefaultToolReferencesHint,
105
CopilotIdentityRulesClass: agentPrompt?.resolveCopilotIdentityRules?.(endpoint) ?? CopilotIdentityRules,
106
SafetyRulesClass: agentPrompt?.resolveSafetyRules?.(endpoint) ?? SafetyRules,
107
userQueryTagName: agentPrompt?.resolveUserQueryTagName?.(endpoint) ?? 'userRequest',
108
};
109
}
110
}();
111
112