Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/src/platform/promptFiles/test/common/mockPromptsService.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 type { ChatCustomAgent, ChatHook, ChatInstruction, ChatPlugin, ChatSkill, ChatSlashCommand } from 'vscode';
7
import { CancellationToken } from '../../../../util/vs/base/common/cancellation';
8
import { Emitter, Event } from '../../../../util/vs/base/common/event';
9
import { Disposable } from '../../../../util/vs/base/common/lifecycle';
10
import { URI } from '../../../../util/vs/base/common/uri';
11
import { PromptFileParser } from '../../../../util/vs/workbench/contrib/chat/common/promptSyntax/promptFileParser';
12
import { IPromptsService, ParsedPromptFile } from '../../common/promptsService';
13
import { ResourceMap } from '../../../../util/vs/base/common/map';
14
15
export class MockPromptsService extends Disposable implements IPromptsService {
16
declare readonly _serviceBrand: undefined;
17
18
private readonly _onDidChangeCustomAgents = this._register(new Emitter<void>());
19
readonly onDidChangeCustomAgents: Event<void> = this._onDidChangeCustomAgents.event;
20
21
private readonly _onDidChangeInstructions = this._register(new Emitter<void>());
22
readonly onDidChangeInstructions: Event<void> = this._onDidChangeInstructions.event;
23
24
private readonly _onDidChangeSkills = this._register(new Emitter<void>());
25
readonly onDidChangeSkills: Event<void> = this._onDidChangeSkills.event;
26
27
private readonly _onDidChangeHooks = this._register(new Emitter<void>());
28
readonly onDidChangeHooks: Event<void> = this._onDidChangeHooks.event;
29
30
private readonly _onDidChangePlugins = this._register(new Emitter<void>());
31
readonly onDidChangePlugins: Event<void> = this._onDidChangePlugins.event;
32
33
private _customAgents: readonly ChatCustomAgent[] = [];
34
private _slashCommands: readonly ChatSlashCommand[] = [];
35
private _instructions: readonly ChatInstruction[] = [];
36
private _skills: readonly ChatSkill[] = [];
37
private _hooks: readonly ChatHook[] = [];
38
private _plugins: readonly ChatPlugin[] = [];
39
private _fileContents = new ResourceMap<string>();
40
41
setCustomAgents(agents: readonly ChatCustomAgent[]): void {
42
this._customAgents = agents;
43
this._onDidChangeCustomAgents.fire();
44
}
45
46
fireCustomAgentsChanged(): void {
47
this._onDidChangeCustomAgents.fire();
48
}
49
50
setSlashCommands(commands: readonly ChatSlashCommand[]): void {
51
this._slashCommands = commands;
52
}
53
54
setInstructions(instructions: readonly ChatInstruction[]): void {
55
this._instructions = instructions;
56
this._onDidChangeInstructions.fire();
57
}
58
59
fireInstructionsChanged(): void {
60
this._onDidChangeInstructions.fire();
61
}
62
63
setSkills(skills: readonly ChatSkill[]): void {
64
this._skills = skills;
65
this._onDidChangeSkills.fire();
66
}
67
68
fireSkillsChanged(): void {
69
this._onDidChangeSkills.fire();
70
}
71
72
setHooks(hooks: readonly ChatHook[]): void {
73
this._hooks = hooks;
74
this._onDidChangeHooks.fire();
75
}
76
77
firePluginsChanged(): void {
78
this._onDidChangePlugins.fire();
79
}
80
81
setPlugins(plugins: readonly ChatPlugin[]): void {
82
this._plugins = plugins;
83
this._onDidChangePlugins.fire();
84
}
85
86
getCustomAgents(_token: CancellationToken): Promise<readonly ChatCustomAgent[]> {
87
return Promise.resolve(this._customAgents);
88
}
89
90
getSlashCommands(_token: CancellationToken): Promise<readonly ChatSlashCommand[]> {
91
return Promise.resolve(this._slashCommands);
92
}
93
94
getInstructions(_token: CancellationToken): Promise<readonly ChatInstruction[]> {
95
return Promise.resolve(this._instructions);
96
}
97
98
getSkills(_token: CancellationToken): Promise<readonly ChatSkill[]> {
99
return Promise.resolve(this._skills);
100
}
101
102
getHooks(_token: CancellationToken): Promise<readonly ChatHook[]> {
103
return Promise.resolve(this._hooks);
104
}
105
106
fireHooksChanged(): void {
107
this._onDidChangeHooks.fire();
108
}
109
110
getPlugins(_token: CancellationToken): Promise<readonly ChatPlugin[]> {
111
return Promise.resolve(this._plugins);
112
}
113
114
/** Register content so parseFile returns a parsed result for the given URI. */
115
setFileContent(uri: URI, content: string) {
116
this._fileContents.set(uri, content);
117
}
118
119
parseFile(uri: URI, _token: CancellationToken): Promise<ParsedPromptFile> {
120
const content = this._fileContents.get(uri) ?? '';
121
return Promise.resolve(new PromptFileParser().parse(uri, content));
122
}
123
}
124
125