Path: blob/main/extensions/copilot/src/extension/prompts/node/inline/summarizedDocument/summarizeDocumentHelpers.ts
13406 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*--------------------------------------------------------------------------------------------*/4import type * as vscode from 'vscode';5import type { Range } from 'vscode';6import { VsCodeTextDocument } from '../../../../../platform/editing/common/abstractText';7import { NotebookDocumentSnapshot } from '../../../../../platform/editing/common/notebookDocumentSnapshot';8import { TextDocumentSnapshot } from '../../../../../platform/editing/common/textDocumentSnapshot';9import { OverlayNode } from '../../../../../platform/parser/node/nodes';10import { IParserService } from '../../../../../platform/parser/node/parserService';11import { StringEdit } from '../../../../../util/vs/editor/common/core/edits/stringEdit';12import { OffsetRange } from '../../../../../util/vs/editor/common/core/ranges/offsetRange';13import { getStructure } from '../../../../context/node/resolvers/selectionContextHelpers';14import { getAdjustedSelection } from '../adjustSelection';15import { IDocumentSummarizationItem, ISummarizedDocumentSettings, ProjectedDocument, summarizeDocumentsSync } from './summarizeDocument';1617export function getCharLimit(tokensBudget: number): number {18return tokensBudget * 4; // roughly 4 chars per token19}2021/**22* The selection is first adjusted {@link getAdjustedSelection} and then the document is summarized using the adjusted selection.23*/24export async function adjustSelectionAndSummarizeDocument(25parserService: IParserService,26document: TextDocumentSnapshot,27formattingOptions: vscode.FormattingOptions | undefined,28selection: Range,29tokensBudget: number,30settings?: ISummarizedDocumentSettings31): Promise<{ document: ProjectedDocument; selection: OffsetRange; adjustedSelection: OffsetRange }> {32const structure = await getStructure(parserService, document, formattingOptions);33const result = getAdjustedSelection(structure, new VsCodeTextDocument(document), selection);34const doc = summarizeDocumentSync(getCharLimit(tokensBudget), document, selection, structure, settings);35return {36document: doc,37adjustedSelection: doc.projectOffsetRange(result.adjusted),38selection: doc.projectOffsetRange(result.original),39};40}4142export class NotebookDocumentSummarizer {43constructor(44) { }4546async summarizeDocument(47document: NotebookDocumentSnapshot,48_formattingOptions: vscode.FormattingOptions | undefined,49_selection: Range | undefined,50_tokensBudget: number,51_settings?: ISummarizedDocumentSettings52): Promise<ProjectedDocument> {53return new ProjectedDocument(document.getText(), StringEdit.empty, document.languageId);54}55}5657export class DocumentSummarizer {58constructor(59@IParserService private readonly _parserService: IParserService60) { }6162summarizeDocument(63document: TextDocumentSnapshot,64formattingOptions: vscode.FormattingOptions | undefined,65selection: Range | undefined,66tokensBudget: number,67settings?: ISummarizedDocumentSettings68): Promise<ProjectedDocument> {69return summarizeDocument(this._parserService, document, formattingOptions, selection, tokensBudget, settings);70}71}7273export async function summarizeDocument(74parserService: IParserService,75document: TextDocumentSnapshot,76formattingOptions: vscode.FormattingOptions | undefined,77selection: Range | undefined,78tokensBudget: number,79settings?: ISummarizedDocumentSettings80): Promise<ProjectedDocument> {81const structure = await getStructure(parserService, document, formattingOptions);82return summarizeDocumentSync(getCharLimit(tokensBudget), document, selection, structure, settings);83}8485export function summarizeDocumentSync(86charLimit: number,87document: TextDocumentSnapshot,88selection: Range | undefined,89overlayNodeRoot: OverlayNode,90settings: ISummarizedDocumentSettings = {}91): ProjectedDocument {92const result = summarizeDocumentsSync(charLimit, settings, [{ document, overlayNodeRoot, selection }]);93return result[0];94}9596export interface SummarizeDocumentsItem {97document: TextDocumentSnapshot;98formattingOptions: vscode.FormattingOptions | undefined;99selection: Range | undefined;100}101102/**103* Summarizes multiple tokens against a shared token budget104*/105export async function summarizeDocuments(106parserService: IParserService,107documentData: SummarizeDocumentsItem[],108tokensBudget: number,109settings?: ISummarizedDocumentSettings110): Promise<ProjectedDocument[]> {111112const items: IDocumentSummarizationItem[] = [];113114await Promise.all(documentData.map(async (data) => {115const overlayNodeRoot = await getStructure(parserService, data.document, data.formattingOptions);116items.push({117document: data.document,118selection: data.selection,119overlayNodeRoot120});121}));122123return summarizeDocumentsSync(tokensBudget, settings ?? {}, items);124}125126127