Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/src/platform/notebook/common/alternativeContent.ts
13401 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 type { LanguageModelChat, NotebookDocument, Uri } from 'vscode';
7
import { findCell } from '../../../util/common/notebooks';
8
import { createServiceIdentifier } from '../../../util/common/services';
9
import { Range } from '../../../vscodeTypes';
10
import { ConfigKey, IConfigurationService } from '../../configuration/common/configurationService';
11
import { modelPrefersJsonNotebookRepresentation } from '../../endpoint/common/chatModelCapabilities';
12
import { IChatEndpoint } from '../../networking/common/networking';
13
import { IExperimentationService } from '../../telemetry/common/nullExperimentationService';
14
import { BaseAlternativeNotebookContentProvider } from './alternativeContentProvider';
15
import { AlternativeJsonNotebookContentProvider, isJsonContent } from './alternativeContentProvider.json';
16
import { AlternativeTextNotebookContentProvider } from './alternativeContentProvider.text';
17
import { AlternativeXmlNotebookContentProvider, isXmlContent } from './alternativeContentProvider.xml';
18
19
export type AlternativeContentFormat = 'xml' | 'text' | 'json';
20
21
export function getAlternativeNotebookDocumentProvider(kind: 'xml' | 'text' | 'json'): BaseAlternativeNotebookContentProvider {
22
switch (kind) {
23
case 'xml':
24
return new AlternativeXmlNotebookContentProvider();
25
case 'text':
26
return new AlternativeTextNotebookContentProvider();
27
case 'json':
28
return new AlternativeJsonNotebookContentProvider();
29
default:
30
throw new Error(`Unsupported kind '${kind}'`);
31
}
32
}
33
34
/**
35
* Given the content, determine the format of the content.
36
*/
37
export function inferAlternativeNotebookContentFormat(content: string): AlternativeContentFormat {
38
if (isXmlContent(content)) {
39
return 'xml';
40
}
41
if (isJsonContent(content)) {
42
return 'json';
43
}
44
return 'text';
45
}
46
47
48
export const IAlternativeNotebookContentService = createServiceIdentifier<IAlternativeNotebookContentService>('IAlternativeNotebookContentService');
49
50
export interface IAlternativeNotebookContentService {
51
readonly _serviceBrand: undefined;
52
getFormat(options: LanguageModelChat | IChatEndpoint | undefined): AlternativeContentFormat;
53
create(format: AlternativeContentFormat): BaseAlternativeNotebookContentProvider;
54
}
55
56
export class AlternativeNotebookContentService implements IAlternativeNotebookContentService {
57
declare readonly _serviceBrand: undefined;
58
constructor(
59
@IConfigurationService private readonly configurationService: IConfigurationService,
60
@IExperimentationService private readonly experimentationService: IExperimentationService,
61
) {
62
//
63
}
64
getFormat(options: LanguageModelChat | IChatEndpoint | undefined): AlternativeContentFormat {
65
// 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).
66
if (options && modelPrefersJsonNotebookRepresentation(options)) {
67
return 'json';
68
}
69
70
return this.configurationService.getExperimentBasedConfig(ConfigKey.Advanced.NotebookAlternativeDocumentFormat, this.experimentationService);
71
}
72
73
create(format: AlternativeContentFormat): BaseAlternativeNotebookContentProvider {
74
return getAlternativeNotebookDocumentProvider(format);
75
}
76
}
77
78
export function getAltNotebookRange(range: Range, cellUri: Uri, notebook: NotebookDocument, format: AlternativeContentFormat) {
79
// If we have a range for cell, then translate that from notebook cell range to alternative range.
80
const cell = findCell(cellUri, notebook);
81
if (!cell) {
82
return undefined;
83
}
84
const doc = getAlternativeNotebookDocumentProvider(format).getAlternativeDocument(notebook);
85
return new Range(
86
doc.fromCellPosition(cell, range.start),
87
doc.fromCellPosition(cell, range.end),
88
);
89
}
90
91