Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/src/extension/prompts/node/inline/summarizedDocument/summarizeDocumentHelpers.ts
13406 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
import type * as vscode from 'vscode';
6
import type { Range } from 'vscode';
7
import { VsCodeTextDocument } from '../../../../../platform/editing/common/abstractText';
8
import { NotebookDocumentSnapshot } from '../../../../../platform/editing/common/notebookDocumentSnapshot';
9
import { TextDocumentSnapshot } from '../../../../../platform/editing/common/textDocumentSnapshot';
10
import { OverlayNode } from '../../../../../platform/parser/node/nodes';
11
import { IParserService } from '../../../../../platform/parser/node/parserService';
12
import { StringEdit } from '../../../../../util/vs/editor/common/core/edits/stringEdit';
13
import { OffsetRange } from '../../../../../util/vs/editor/common/core/ranges/offsetRange';
14
import { getStructure } from '../../../../context/node/resolvers/selectionContextHelpers';
15
import { getAdjustedSelection } from '../adjustSelection';
16
import { IDocumentSummarizationItem, ISummarizedDocumentSettings, ProjectedDocument, summarizeDocumentsSync } from './summarizeDocument';
17
18
export function getCharLimit(tokensBudget: number): number {
19
return tokensBudget * 4; // roughly 4 chars per token
20
}
21
22
/**
23
* The selection is first adjusted {@link getAdjustedSelection} and then the document is summarized using the adjusted selection.
24
*/
25
export async function adjustSelectionAndSummarizeDocument(
26
parserService: IParserService,
27
document: TextDocumentSnapshot,
28
formattingOptions: vscode.FormattingOptions | undefined,
29
selection: Range,
30
tokensBudget: number,
31
settings?: ISummarizedDocumentSettings
32
): Promise<{ document: ProjectedDocument; selection: OffsetRange; adjustedSelection: OffsetRange }> {
33
const structure = await getStructure(parserService, document, formattingOptions);
34
const result = getAdjustedSelection(structure, new VsCodeTextDocument(document), selection);
35
const doc = summarizeDocumentSync(getCharLimit(tokensBudget), document, selection, structure, settings);
36
return {
37
document: doc,
38
adjustedSelection: doc.projectOffsetRange(result.adjusted),
39
selection: doc.projectOffsetRange(result.original),
40
};
41
}
42
43
export class NotebookDocumentSummarizer {
44
constructor(
45
) { }
46
47
async summarizeDocument(
48
document: NotebookDocumentSnapshot,
49
_formattingOptions: vscode.FormattingOptions | undefined,
50
_selection: Range | undefined,
51
_tokensBudget: number,
52
_settings?: ISummarizedDocumentSettings
53
): Promise<ProjectedDocument> {
54
return new ProjectedDocument(document.getText(), StringEdit.empty, document.languageId);
55
}
56
}
57
58
export class DocumentSummarizer {
59
constructor(
60
@IParserService private readonly _parserService: IParserService
61
) { }
62
63
summarizeDocument(
64
document: TextDocumentSnapshot,
65
formattingOptions: vscode.FormattingOptions | undefined,
66
selection: Range | undefined,
67
tokensBudget: number,
68
settings?: ISummarizedDocumentSettings
69
): Promise<ProjectedDocument> {
70
return summarizeDocument(this._parserService, document, formattingOptions, selection, tokensBudget, settings);
71
}
72
}
73
74
export async function summarizeDocument(
75
parserService: IParserService,
76
document: TextDocumentSnapshot,
77
formattingOptions: vscode.FormattingOptions | undefined,
78
selection: Range | undefined,
79
tokensBudget: number,
80
settings?: ISummarizedDocumentSettings
81
): Promise<ProjectedDocument> {
82
const structure = await getStructure(parserService, document, formattingOptions);
83
return summarizeDocumentSync(getCharLimit(tokensBudget), document, selection, structure, settings);
84
}
85
86
export function summarizeDocumentSync(
87
charLimit: number,
88
document: TextDocumentSnapshot,
89
selection: Range | undefined,
90
overlayNodeRoot: OverlayNode,
91
settings: ISummarizedDocumentSettings = {}
92
): ProjectedDocument {
93
const result = summarizeDocumentsSync(charLimit, settings, [{ document, overlayNodeRoot, selection }]);
94
return result[0];
95
}
96
97
export interface SummarizeDocumentsItem {
98
document: TextDocumentSnapshot;
99
formattingOptions: vscode.FormattingOptions | undefined;
100
selection: Range | undefined;
101
}
102
103
/**
104
* Summarizes multiple tokens against a shared token budget
105
*/
106
export async function summarizeDocuments(
107
parserService: IParserService,
108
documentData: SummarizeDocumentsItem[],
109
tokensBudget: number,
110
settings?: ISummarizedDocumentSettings
111
): Promise<ProjectedDocument[]> {
112
113
const items: IDocumentSummarizationItem[] = [];
114
115
await Promise.all(documentData.map(async (data) => {
116
const overlayNodeRoot = await getStructure(parserService, data.document, data.formattingOptions);
117
items.push({
118
document: data.document,
119
selection: data.selection,
120
overlayNodeRoot
121
});
122
}));
123
124
return summarizeDocumentsSync(tokensBudget, settings ?? {}, items);
125
}
126
127