Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/src/extension/intents/node/intentService.ts
13399 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 { ChatLocation } from '../../../platform/chat/common/commonTypes';
7
import { createServiceIdentifier } from '../../../util/common/services';
8
import { IInstantiationService } from '../../../util/vs/platform/instantiation/common/instantiation';
9
import { Intent } from '../../common/constants';
10
import { IntentRegistry } from '../../prompt/node/intentRegistry';
11
import { IIntent } from '../../prompt/node/intents';
12
13
14
export const IIntentService = createServiceIdentifier<IIntentService>('IIntentService');
15
16
export interface IIntentService {
17
readonly _serviceBrand: undefined;
18
19
readonly unknownIntent: IIntent;
20
getIntents(location: ChatLocation): IIntent[];
21
getIntent(id: string, location: ChatLocation): IIntent | undefined;
22
}
23
24
export class IntentService implements IIntentService {
25
_serviceBrand: undefined;
26
27
private _intents: IIntent[] | null = null;
28
29
constructor(
30
@IInstantiationService private readonly _instantiationService: IInstantiationService
31
) { }
32
33
private _getOrCreateIntents(): IIntent[] {
34
if (!this._intents) {
35
this._intents = IntentRegistry.getIntents().map(d => this._instantiationService.createInstance(d));
36
}
37
return this._intents;
38
}
39
40
public get unknownIntent(): IIntent {
41
const intents = this._getOrCreateIntents();
42
const result = intents.find(i => i.id === Intent.Unknown);
43
if (!result) {
44
throw new Error(`Unknown intent not found`);
45
}
46
return result;
47
}
48
49
public getIntents(location: ChatLocation): IIntent[] {
50
const intents = this._getOrCreateIntents();
51
return intents.filter(i => i.locations.includes(location));
52
}
53
54
public getIntent(id: string, location: ChatLocation): IIntent | undefined {
55
return this.getIntents(location).find(i => i.id === id);
56
}
57
}
58
59