Path: blob/main/src/vs/workbench/contrib/chat/browser/attachments/chatVariables.ts
4780 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/attachments/chatVariables.js';6import { IToolAndToolSetEnablementMap } from '../../common/tools/languageModelToolsService.js';7import { IChatWidgetService } from '../chat.js';8import { ChatDynamicVariableModel } from './chatDynamicVariables.js';9import { Range } from '../../../../../editor/common/core/range.js';10import { URI } from '../../../../../base/common/uri.js';1112export class ChatVariablesService implements IChatVariablesService {13declare _serviceBrand: undefined;1415constructor(16@IChatWidgetService private readonly chatWidgetService: IChatWidgetService,17) { }1819getDynamicVariables(sessionResource: URI): ReadonlyArray<IDynamicVariable> {20// 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.21// Need to ...22// - Parser takes list of dynamic references (annoying)23// - 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)24const widget = this.chatWidgetService.getWidgetBySessionResource(sessionResource);25if (!widget || !widget.viewModel || !widget.supportsFileReferences) {26return [];27}2829const model = widget.getContrib<ChatDynamicVariableModel>(ChatDynamicVariableModel.ID);30if (!model) {31return [];32}3334if (widget.input.attachmentModel.attachments.length > 0 && widget.viewModel.editing) {35const references: IDynamicVariable[] = [];36for (const attachment of widget.input.attachmentModel.attachments) {37// If the attachment has a range, it is a dynamic variable38if (attachment.range) {39const referenceObj: IDynamicVariable = {40id: attachment.id,41fullName: attachment.name,42modelDescription: attachment.modelDescription,43range: new Range(1, attachment.range.start + 1, 1, attachment.range.endExclusive + 1),44icon: attachment.icon,45isFile: attachment.kind === 'file',46isDirectory: attachment.kind === 'directory',47data: attachment.value48};49references.push(referenceObj);50}51}5253return [...model.variables, ...references];54}5556return model.variables;57}5859getSelectedToolAndToolSets(sessionResource: URI): IToolAndToolSetEnablementMap {60const widget = this.chatWidgetService.getWidgetBySessionResource(sessionResource);61if (!widget) {62return new Map();63}64return widget.input.selectedToolsModel.entriesMap.get();6566}67}686970