Path: blob/main/src/vs/workbench/contrib/chat/browser/chatEditing/chatEditingOperations.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 { StringSHA1 } from '../../../../../base/common/hash.js';6import { URI } from '../../../../../base/common/uri.js';7import { TextEdit } from '../../../../../editor/common/languages.js';8import { ICellEditOperation } from '../../../notebook/common/notebookCommon.js';9import { IModifiedEntryTelemetryInfo } from '../../common/editing/chatEditingService.js';10import { LocalChatSessionUri } from '../../common/model/chatUri.js';1112export enum FileOperationType {13Create = 'create',14Delete = 'delete',15Rename = 'rename',16TextEdit = 'textEdit',17NotebookEdit = 'notebookEdit'18}1920/**21* Base interface for all file operations in the checkpoint timeline22*/23export interface IFileOperation {24readonly type: FileOperationType;25readonly uri: URI;26readonly requestId: string;27readonly epoch: number;28}2930/**31* Operation representing the creation of a new file32*/33export interface IFileCreateOperation extends IFileOperation {34readonly type: FileOperationType.Create;35readonly initialContent: string;36readonly notebookViewType?: string;37readonly telemetryInfo: IModifiedEntryTelemetryInfo;38}3940/**41* Operation representing the deletion of a file42*/43export interface IFileDeleteOperation extends IFileOperation {44readonly type: FileOperationType.Delete;45readonly finalContent: string; // content before deletion for potential restoration46}4748/**49* Operation representing the renaming/moving of a file50*/51export interface IFileRenameOperation extends IFileOperation {52readonly type: FileOperationType.Rename;53readonly oldUri: URI;54readonly newUri: URI;55}5657/**58* Operation representing text edits applied to a file59*/60export interface ITextEditOperation extends IFileOperation {61readonly type: FileOperationType.TextEdit;62readonly edits: readonly TextEdit[];63/**64* For cell URIs, the cell index that was edited. Needed because the original65* edit URI only contains the `handle` which is not portable between notebooks.66*/67readonly cellIndex?: number;68}6970/**71* Operation representing notebook cell edits applied to a notebook72*/73export interface INotebookEditOperation extends IFileOperation {74readonly type: FileOperationType.NotebookEdit;75readonly cellEdits: readonly ICellEditOperation[];76}7778/**79* Union type of all possible file operations80*/81export type FileOperation = IFileCreateOperation | IFileDeleteOperation | IFileRenameOperation | ITextEditOperation | INotebookEditOperation;8283/**84* File baseline represents the initial state of a file when first edited in a request85*/86export interface IFileBaseline {87readonly uri: URI;88readonly requestId: string;89readonly content: string;90readonly epoch: number;91readonly telemetryInfo: IModifiedEntryTelemetryInfo;92readonly notebookViewType?: string;93}9495export interface IReconstructedFileExistsState {96readonly exists: true;97readonly content: string;98readonly uri: URI;99readonly telemetryInfo: IModifiedEntryTelemetryInfo;100readonly notebookViewType?: string;101}102103export interface IReconstructedFileNotExistsState {104readonly exists: false;105readonly uri: URI;106}107108/**109* The reconstructed state of a file at a specific checkpoint110*/111export type IReconstructedFileState = IReconstructedFileNotExistsState | IReconstructedFileExistsState;112113/**114* Checkpoint represents a stable state that can be navigated to115*/116export interface ICheckpoint {117readonly checkpointId: string;118readonly requestId: string | undefined; // undefined for initial state119readonly undoStopId: string | undefined;120readonly epoch: number;121readonly label: string; // for UI display122readonly description?: string; // optional detailed description123}124125/**126* State that can be persisted and restored for the checkpoint timeline127*/128export interface IChatEditingTimelineState {129readonly checkpoints: readonly ICheckpoint[];130readonly fileBaselines: [string, IFileBaseline][]; // key: `${uri}::${requestId}`131readonly operations: readonly FileOperation[];132readonly currentEpoch: number;133readonly epochCounter: number;134}135136export function getKeyForChatSessionResource(chatSessionResource: URI) {137const sessionId = LocalChatSessionUri.parseLocalSessionId(chatSessionResource);138if (sessionId) {139return sessionId;140}141142const sha = new StringSHA1();143sha.update(chatSessionResource.toString());144return sha.digest();145}146147148