Path: blob/main/extensions/copilot/src/platform/notebook/common/alternativeContent.ts
13401 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 type { LanguageModelChat, NotebookDocument, Uri } from 'vscode';6import { findCell } from '../../../util/common/notebooks';7import { createServiceIdentifier } from '../../../util/common/services';8import { Range } from '../../../vscodeTypes';9import { ConfigKey, IConfigurationService } from '../../configuration/common/configurationService';10import { modelPrefersJsonNotebookRepresentation } from '../../endpoint/common/chatModelCapabilities';11import { IChatEndpoint } from '../../networking/common/networking';12import { IExperimentationService } from '../../telemetry/common/nullExperimentationService';13import { BaseAlternativeNotebookContentProvider } from './alternativeContentProvider';14import { AlternativeJsonNotebookContentProvider, isJsonContent } from './alternativeContentProvider.json';15import { AlternativeTextNotebookContentProvider } from './alternativeContentProvider.text';16import { AlternativeXmlNotebookContentProvider, isXmlContent } from './alternativeContentProvider.xml';1718export type AlternativeContentFormat = 'xml' | 'text' | 'json';1920export function getAlternativeNotebookDocumentProvider(kind: 'xml' | 'text' | 'json'): BaseAlternativeNotebookContentProvider {21switch (kind) {22case 'xml':23return new AlternativeXmlNotebookContentProvider();24case 'text':25return new AlternativeTextNotebookContentProvider();26case 'json':27return new AlternativeJsonNotebookContentProvider();28default:29throw new Error(`Unsupported kind '${kind}'`);30}31}3233/**34* Given the content, determine the format of the content.35*/36export function inferAlternativeNotebookContentFormat(content: string): AlternativeContentFormat {37if (isXmlContent(content)) {38return 'xml';39}40if (isJsonContent(content)) {41return 'json';42}43return 'text';44}454647export const IAlternativeNotebookContentService = createServiceIdentifier<IAlternativeNotebookContentService>('IAlternativeNotebookContentService');4849export interface IAlternativeNotebookContentService {50readonly _serviceBrand: undefined;51getFormat(options: LanguageModelChat | IChatEndpoint | undefined): AlternativeContentFormat;52create(format: AlternativeContentFormat): BaseAlternativeNotebookContentProvider;53}5455export class AlternativeNotebookContentService implements IAlternativeNotebookContentService {56declare readonly _serviceBrand: undefined;57constructor(58@IConfigurationService private readonly configurationService: IConfigurationService,59@IExperimentationService private readonly experimentationService: IExperimentationService,60) {61//62}63getFormat(options: LanguageModelChat | IChatEndpoint | undefined): AlternativeContentFormat {64// GPT 4.1 supports apply_patch, such models work best with JSON format (doesn't have great support for XML yet, thats being worked on).65if (options && modelPrefersJsonNotebookRepresentation(options)) {66return 'json';67}6869return this.configurationService.getExperimentBasedConfig(ConfigKey.Advanced.NotebookAlternativeDocumentFormat, this.experimentationService);70}7172create(format: AlternativeContentFormat): BaseAlternativeNotebookContentProvider {73return getAlternativeNotebookDocumentProvider(format);74}75}7677export function getAltNotebookRange(range: Range, cellUri: Uri, notebook: NotebookDocument, format: AlternativeContentFormat) {78// If we have a range for cell, then translate that from notebook cell range to alternative range.79const cell = findCell(cellUri, notebook);80if (!cell) {81return undefined;82}83const doc = getAlternativeNotebookDocumentProvider(format).getAlternativeDocument(notebook);84return new Range(85doc.fromCellPosition(cell, range.start),86doc.fromCellPosition(cell, range.end),87);88}899091