Path: blob/main/src/vs/workbench/contrib/chat/browser/chatVariables.ts
3296 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 { IChatVariablesService, IDynamicVariable } from '../common/chatVariables.js';6import { IToolAndToolSetEnablementMap } from '../common/languageModelToolsService.js';7import { IChatWidgetService } from './chat.js';8import { ChatDynamicVariableModel } from './contrib/chatDynamicVariables.js';9import { Range } from '../../../../editor/common/core/range.js';1011export class ChatVariablesService implements IChatVariablesService {12declare _serviceBrand: undefined;1314constructor(15@IChatWidgetService private readonly chatWidgetService: IChatWidgetService,16) { }1718getDynamicVariables(sessionId: string): ReadonlyArray<IDynamicVariable> {19// This is slightly wrong... the parser pulls dynamic references from the input widget, but there is no guarantee that message came from the input here.20// Need to ...21// - Parser takes list of dynamic references (annoying)22// - Or the parser is known to implicitly act on the input widget, and we need to call it before calling the chat service (maybe incompatible with the future, but easy)23const widget = this.chatWidgetService.getWidgetBySessionId(sessionId);24if (!widget || !widget.viewModel || !widget.supportsFileReferences) {25return [];26}2728const model = widget.getContrib<ChatDynamicVariableModel>(ChatDynamicVariableModel.ID);29if (!model) {30return [];31}3233if (widget.input.attachmentModel.attachments.length > 0 && widget.viewModel.editing) {34const references: IDynamicVariable[] = [];35for (const attachment of widget.input.attachmentModel.attachments) {36// If the attachment has a range, it is a dynamic variable37if (attachment.range) {38const referenceObj: IDynamicVariable = {39id: attachment.id,40fullName: attachment.name,41modelDescription: attachment.modelDescription,42range: new Range(1, attachment.range.start + 1, 1, attachment.range.endExclusive + 1),43icon: attachment.icon,44isFile: attachment.kind === 'file',45isDirectory: attachment.kind === 'directory',46data: attachment.value47};48references.push(referenceObj);49}50}5152return [...model.variables, ...references];53}5455return model.variables;56}5758getSelectedToolAndToolSets(sessionId: string): IToolAndToolSetEnablementMap {59const widget = this.chatWidgetService.getWidgetBySessionId(sessionId);60if (!widget) {61return new Map();62}63return widget.input.selectedToolsModel.entriesMap.get();6465}6667}686970