-
-
Notifications
You must be signed in to change notification settings - Fork 193
Expand file tree
/
Copy pathAISnapshotStore.js
More file actions
304 lines (277 loc) · 10.5 KB
/
AISnapshotStore.js
File metadata and controls
304 lines (277 loc) · 10.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/*
* GNU AGPL-3.0 License
*
* Copyright (c) 2021 - present core.ai . All rights reserved.
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License
* for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see https://opensource.org/licenses/AGPL-3.0.
*
*/
/**
* AI Snapshot Store — content-addressable store and snapshot/restore logic
* for tracking file states across AI responses. Extracted from AIChatPanel
* to separate data/logic concerns from the DOM/UI layer.
*/
define(function (require, exports, module) {
const DocumentManager = require("document/DocumentManager"),
CommandManager = require("command/CommandManager"),
Commands = require("command/Commands"),
FileSystem = require("filesystem/FileSystem");
// --- Private state ---
const _contentStore = {}; // hash → content string (content-addressable dedup)
let _snapshots = []; // flat: _snapshots[i] = { filePath: hash|null }
let _pendingBeforeSnap = {}; // built during current response: filePath → hash|null
// --- Path utility ---
/**
* Convert a real filesystem path back to a VFS path that Phoenix understands.
*/
function realToVfsPath(realPath) {
// If it already looks like a VFS path, return as-is
if (realPath.startsWith("/tauri/") || realPath.startsWith("/mnt/")) {
return realPath;
}
// Desktop builds use /tauri/ prefix
if (Phoenix.isNativeApp) {
return "/tauri" + realPath;
}
return realPath;
}
// --- Content-addressable store ---
function _hashContent(str) {
let h = 0x811c9dc5; // FNV-1a
for (let i = 0; i < str.length; i++) {
h ^= str.charCodeAt(i); // eslint-disable-line no-bitwise
h = (h * 0x01000193) >>> 0; // eslint-disable-line no-bitwise
}
return h.toString(36);
}
function storeContent(content) {
const hash = _hashContent(content);
_contentStore[hash] = content;
return hash;
}
// --- File operations ---
/**
* Save a document's current content to disk so editors and disk stay in sync.
* @param {Document} doc - Brackets document to save
* @return {$.Promise}
*/
function saveDocToDisk(doc) {
const d = new $.Deferred();
const file = doc.file;
const content = doc.getText();
file.write(content, function (err) {
if (err) {
console.error("[AI UI] Save to disk failed:", doc.file.fullPath, err);
d.reject(err);
} else {
doc.notifySaved();
d.resolve();
}
});
return d.promise();
}
/**
* Close a document tab (if open) and delete the file from disk.
* Used during restore to remove files that were created by the AI.
* @param {string} filePath - real filesystem path
* @return {$.Promise}
*/
function _closeAndDeleteFile(filePath) {
const result = new $.Deferred();
const vfsPath = realToVfsPath(filePath);
const file = FileSystem.getFileForPath(vfsPath);
const openDoc = DocumentManager.getOpenDocumentForPath(vfsPath);
if (openDoc) {
if (openDoc.isDirty) {
openDoc.setText("");
}
CommandManager.execute(Commands.FILE_CLOSE, { file: file, _forceClose: true })
.always(function () {
file.unlink(function (err) {
if (err) {
result.reject(err);
} else {
result.resolve();
}
});
});
} else {
file.unlink(function (err) {
if (err) {
result.reject(err);
} else {
result.resolve();
}
});
}
return result.promise();
}
/**
* Create or update a file with the given content.
* @param {string} filePath - real filesystem path
* @param {string} content - content to set
* @return {$.Promise}
*/
function _createOrUpdateFile(filePath, content) {
const result = new $.Deferred();
const vfsPath = realToVfsPath(filePath);
function _setContent() {
DocumentManager.getDocumentForPath(vfsPath)
.done(function (doc) {
try {
doc.setText(content);
saveDocToDisk(doc).always(function () {
CommandManager.execute(Commands.CMD_OPEN, { fullPath: vfsPath });
result.resolve();
});
} catch (err) {
result.reject(err);
}
})
.fail(function (err) {
result.reject(err || new Error("Could not open document"));
});
}
const file = FileSystem.getFileForPath(vfsPath);
file.exists(function (existErr, exists) {
if (exists) {
_setContent();
} else {
file.write("", function (writeErr) {
if (writeErr) {
result.reject(new Error("Could not create file: " + writeErr));
return;
}
_setContent();
});
}
});
return result.promise();
}
// --- Snapshot logic ---
/**
* Apply a snapshot to files. hash=null means delete the file.
* @param {Object} snapshot - { filePath: hash|null }
* @return {$.Promise} resolves with errorCount
*/
function _applySnapshot(snapshot) {
const result = new $.Deferred();
const filePaths = Object.keys(snapshot);
const promises = [];
let errorCount = 0;
filePaths.forEach(function (fp) {
const hash = snapshot[fp];
const p = hash === null
? _closeAndDeleteFile(fp)
: _createOrUpdateFile(fp, _contentStore[hash]);
p.fail(function () { errorCount++; });
promises.push(p);
});
if (promises.length === 0) {
return result.resolve(0).promise();
}
$.when.apply($, promises).always(function () { result.resolve(errorCount); });
return result.promise();
}
// --- Public API ---
/**
* Record a file's pre-edit state into the pending snapshot and back-fill
* existing snapshots. Called once per file per response (first edit wins).
* @param {string} filePath - real filesystem path
* @param {string} previousContent - content before edit
* @param {boolean} isNewFile - true if the file was created by this edit
*/
function recordFileBeforeEdit(filePath, previousContent, isNewFile) {
if (!_pendingBeforeSnap.hasOwnProperty(filePath)) {
const hash = isNewFile ? null : storeContent(previousContent);
_pendingBeforeSnap[filePath] = hash;
// Back-fill all existing snapshots with this file's pre-AI state
_snapshots.forEach(function (snap) {
if (snap[filePath] === undefined) {
snap[filePath] = hash;
}
});
}
}
/**
* Create the initial snapshot (snapshot 0) capturing file state before any
* AI edits. Called once per session on the first edit.
* @return {number} the snapshot index (always 0)
*/
function createInitialSnapshot() {
_snapshots.push({});
return 0;
}
/**
* Finalize snapshot state when a response completes.
* Builds an "after" snapshot from current document content for edited files,
* pushes it, and resets transient tracking variables.
* @return {number} the after-snapshot index, or -1 if no edits happened
*/
function finalizeResponse() {
let afterIndex = -1;
if (Object.keys(_pendingBeforeSnap).length > 0) {
// Build "after" snapshot = last snapshot + current content of edited files
const afterSnap = Object.assign({}, _snapshots[_snapshots.length - 1]);
Object.keys(_pendingBeforeSnap).forEach(function (fp) {
const vfsPath = realToVfsPath(fp);
const openDoc = DocumentManager.getOpenDocumentForPath(vfsPath);
if (openDoc) {
afterSnap[fp] = storeContent(openDoc.getText());
}
});
_snapshots.push(afterSnap);
afterIndex = _snapshots.length - 1;
}
_pendingBeforeSnap = {};
return afterIndex;
}
/**
* Restore files to the state captured in a specific snapshot.
* @param {number} index - index into _snapshots
* @param {Function} onComplete - callback(errorCount)
*/
function restoreToSnapshot(index, onComplete) {
if (index < 0 || index >= _snapshots.length) {
onComplete(0);
return;
}
_applySnapshot(_snapshots[index]).done(function (errorCount) {
onComplete(errorCount);
});
}
/**
* @return {number} number of snapshots
*/
function getSnapshotCount() {
return _snapshots.length;
}
/**
* Clear all snapshot state. Called when starting a new session.
*/
function reset() {
Object.keys(_contentStore).forEach(function (k) { delete _contentStore[k]; });
_snapshots = [];
_pendingBeforeSnap = {};
}
exports.realToVfsPath = realToVfsPath;
exports.saveDocToDisk = saveDocToDisk;
exports.storeContent = storeContent;
exports.recordFileBeforeEdit = recordFileBeforeEdit;
exports.createInitialSnapshot = createInitialSnapshot;
exports.finalizeResponse = finalizeResponse;
exports.restoreToSnapshot = restoreToSnapshot;
exports.getSnapshotCount = getSnapshotCount;
exports.reset = reset;
});