Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/workbench/contrib/chat/browser/chatEditing/chatEditingOperations.ts
4780 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 { StringSHA1 } from '../../../../../base/common/hash.js';
7
import { URI } from '../../../../../base/common/uri.js';
8
import { TextEdit } from '../../../../../editor/common/languages.js';
9
import { ICellEditOperation } from '../../../notebook/common/notebookCommon.js';
10
import { IModifiedEntryTelemetryInfo } from '../../common/editing/chatEditingService.js';
11
import { LocalChatSessionUri } from '../../common/model/chatUri.js';
12
13
export enum FileOperationType {
14
Create = 'create',
15
Delete = 'delete',
16
Rename = 'rename',
17
TextEdit = 'textEdit',
18
NotebookEdit = 'notebookEdit'
19
}
20
21
/**
22
* Base interface for all file operations in the checkpoint timeline
23
*/
24
export interface IFileOperation {
25
readonly type: FileOperationType;
26
readonly uri: URI;
27
readonly requestId: string;
28
readonly epoch: number;
29
}
30
31
/**
32
* Operation representing the creation of a new file
33
*/
34
export interface IFileCreateOperation extends IFileOperation {
35
readonly type: FileOperationType.Create;
36
readonly initialContent: string;
37
readonly notebookViewType?: string;
38
readonly telemetryInfo: IModifiedEntryTelemetryInfo;
39
}
40
41
/**
42
* Operation representing the deletion of a file
43
*/
44
export interface IFileDeleteOperation extends IFileOperation {
45
readonly type: FileOperationType.Delete;
46
readonly finalContent: string; // content before deletion for potential restoration
47
}
48
49
/**
50
* Operation representing the renaming/moving of a file
51
*/
52
export interface IFileRenameOperation extends IFileOperation {
53
readonly type: FileOperationType.Rename;
54
readonly oldUri: URI;
55
readonly newUri: URI;
56
}
57
58
/**
59
* Operation representing text edits applied to a file
60
*/
61
export interface ITextEditOperation extends IFileOperation {
62
readonly type: FileOperationType.TextEdit;
63
readonly edits: readonly TextEdit[];
64
/**
65
* For cell URIs, the cell index that was edited. Needed because the original
66
* edit URI only contains the `handle` which is not portable between notebooks.
67
*/
68
readonly cellIndex?: number;
69
}
70
71
/**
72
* Operation representing notebook cell edits applied to a notebook
73
*/
74
export interface INotebookEditOperation extends IFileOperation {
75
readonly type: FileOperationType.NotebookEdit;
76
readonly cellEdits: readonly ICellEditOperation[];
77
}
78
79
/**
80
* Union type of all possible file operations
81
*/
82
export type FileOperation = IFileCreateOperation | IFileDeleteOperation | IFileRenameOperation | ITextEditOperation | INotebookEditOperation;
83
84
/**
85
* File baseline represents the initial state of a file when first edited in a request
86
*/
87
export interface IFileBaseline {
88
readonly uri: URI;
89
readonly requestId: string;
90
readonly content: string;
91
readonly epoch: number;
92
readonly telemetryInfo: IModifiedEntryTelemetryInfo;
93
readonly notebookViewType?: string;
94
}
95
96
export interface IReconstructedFileExistsState {
97
readonly exists: true;
98
readonly content: string;
99
readonly uri: URI;
100
readonly telemetryInfo: IModifiedEntryTelemetryInfo;
101
readonly notebookViewType?: string;
102
}
103
104
export interface IReconstructedFileNotExistsState {
105
readonly exists: false;
106
readonly uri: URI;
107
}
108
109
/**
110
* The reconstructed state of a file at a specific checkpoint
111
*/
112
export type IReconstructedFileState = IReconstructedFileNotExistsState | IReconstructedFileExistsState;
113
114
/**
115
* Checkpoint represents a stable state that can be navigated to
116
*/
117
export interface ICheckpoint {
118
readonly checkpointId: string;
119
readonly requestId: string | undefined; // undefined for initial state
120
readonly undoStopId: string | undefined;
121
readonly epoch: number;
122
readonly label: string; // for UI display
123
readonly description?: string; // optional detailed description
124
}
125
126
/**
127
* State that can be persisted and restored for the checkpoint timeline
128
*/
129
export interface IChatEditingTimelineState {
130
readonly checkpoints: readonly ICheckpoint[];
131
readonly fileBaselines: [string, IFileBaseline][]; // key: `${uri}::${requestId}`
132
readonly operations: readonly FileOperation[];
133
readonly currentEpoch: number;
134
readonly epochCounter: number;
135
}
136
137
export function getKeyForChatSessionResource(chatSessionResource: URI) {
138
const sessionId = LocalChatSessionUri.parseLocalSessionId(chatSessionResource);
139
if (sessionId) {
140
return sessionId;
141
}
142
143
const sha = new StringSHA1();
144
sha.update(chatSessionResource.toString());
145
return sha.digest();
146
}
147
148