Path: blob/main/extensions/copilot/src/extension/intents/node/intentService.ts
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 { ChatLocation } from '../../../platform/chat/common/commonTypes';6import { createServiceIdentifier } from '../../../util/common/services';7import { IInstantiationService } from '../../../util/vs/platform/instantiation/common/instantiation';8import { Intent } from '../../common/constants';9import { IntentRegistry } from '../../prompt/node/intentRegistry';10import { IIntent } from '../../prompt/node/intents';111213export const IIntentService = createServiceIdentifier<IIntentService>('IIntentService');1415export interface IIntentService {16readonly _serviceBrand: undefined;1718readonly unknownIntent: IIntent;19getIntents(location: ChatLocation): IIntent[];20getIntent(id: string, location: ChatLocation): IIntent | undefined;21}2223export class IntentService implements IIntentService {24_serviceBrand: undefined;2526private _intents: IIntent[] | null = null;2728constructor(29@IInstantiationService private readonly _instantiationService: IInstantiationService30) { }3132private _getOrCreateIntents(): IIntent[] {33if (!this._intents) {34this._intents = IntentRegistry.getIntents().map(d => this._instantiationService.createInstance(d));35}36return this._intents;37}3839public get unknownIntent(): IIntent {40const intents = this._getOrCreateIntents();41const result = intents.find(i => i.id === Intent.Unknown);42if (!result) {43throw new Error(`Unknown intent not found`);44}45return result;46}4748public getIntents(location: ChatLocation): IIntent[] {49const intents = this._getOrCreateIntents();50return intents.filter(i => i.locations.includes(location));51}5253public getIntent(id: string, location: ChatLocation): IIntent | undefined {54return this.getIntents(location).find(i => i.id === id);55}56}575859