vzcode 3.1.0 → 3.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +185 -40
- package/dist/assets/App-C9mRI7eH.js +387 -0
- package/dist/assets/App-CxiKy3Rs.css +1 -0
- package/dist/assets/bindings_wasm_bg-IWNG_AjZ.wasm +0 -0
- package/dist/assets/buildWorker-N5tFTOqM.js +794 -0
- package/dist/assets/index-COLyIkCc.js +43 -0
- package/dist/assets/index-CZT-v4sH.css +1 -0
- package/dist/assets/rolldown-runtime-COiEfhsA.js +1 -0
- package/dist/assets/worker-BpV7Ustv.js +223 -0
- package/dist/assets/worker-CLeezw6h.js +458 -0
- package/dist/assets/worker-DaSIYBQX.js +327 -0
- package/dist/cli.js +170 -99
- package/dist/index.html +3 -2
- package/dist/server/accessControl.js +104 -0
- package/dist/server/computeInitialDocument.js +4 -1
- package/dist/server/createServer.js +356 -104
- package/dist/server/index.js +4 -3
- package/dist/server/workspaceSecurity.js +115 -0
- package/dist/server/workspaceSync.js +141 -0
- package/package.json +74 -72
- package/src/cli.ts +203 -182
- package/src/client/App/index.tsx +2 -0
- package/src/client/App/usePending.ts +3 -1
- package/src/client/CodeEditor/style.scss +18 -11
- package/src/client/RunCodeWidget/index.tsx +1 -0
- package/src/client/TabList/Tab.tsx +13 -5
- package/src/client/TabList/style.scss +32 -5
- package/src/client/VZCodeContext/types.ts +6 -0
- package/src/client/VZCodeContext/useVZCodeState.ts +2 -0
- package/src/client/VZResizer/styles.scss +1 -1
- package/src/client/VZSidebar/AIChatWrapper.tsx +2 -0
- package/src/client/VZSidebar/DeleteConfirmationModal.tsx +1 -1
- package/src/client/VZSidebar/Item.tsx +8 -4
- package/src/client/VZSidebar/VisualEditor/CheckboxWidget.tsx +22 -30
- package/src/client/VZSidebar/VisualEditor/ColorWidget.tsx +198 -126
- package/src/client/VZSidebar/VisualEditor/DropdownWidget.tsx +25 -67
- package/src/client/VZSidebar/VisualEditor/PropertySection.tsx +41 -0
- package/src/client/VZSidebar/VisualEditor/SegmentedWidget.tsx +34 -0
- package/src/client/VZSidebar/VisualEditor/SliderWidget.tsx +60 -12
- package/src/client/VZSidebar/VisualEditor/VisualEditor.tsx +31 -393
- package/src/client/VZSidebar/VisualEditor/VisualEditorControls.tsx +478 -0
- package/src/client/VZSidebar/VisualEditor/colorUtils.ts +12 -0
- package/src/client/VZSidebar/VisualEditor/status.ts +47 -0
- package/src/client/VZSidebar/VisualEditor/updateConfig.ts +51 -24
- package/src/client/VZSidebar/VisualEditor/utils.ts +1 -1
- package/src/client/VZSidebar/index.tsx +55 -23
- package/src/client/VZSidebar/styles.scss +426 -6
- package/src/client/WorkspaceConflictBanner.tsx +120 -0
- package/src/client/index.ts +2 -0
- package/src/client/main.jsx +54 -12
- package/src/client/ui.scss +156 -4
- package/src/client/ui.tsx +1 -1
- package/src/llm-streaming-server/README.md +4 -16
- package/src/llm-streaming-ui/README.md +41 -2
- package/src/llm-streaming-ui/components/ChatInput.tsx +65 -13
- package/src/llm-streaming-ui/components/index.tsx +2 -0
- package/src/llm-streaming-ui/components/styles.scss +46 -0
- package/src/llm-streaming-ui/hooks/useAutoScroll.ts +1 -1
- package/src/llm-streaming-ui/index.ts +1 -0
- package/src/llm-streaming-ui/props.ts +26 -2
- package/src/server/accessControl.ts +151 -0
- package/src/server/computeInitialDocument.ts +6 -2
- package/src/server/createServer.ts +487 -138
- package/src/server/index.ts +4 -5
- package/src/server/workspaceSecurity.ts +160 -0
- package/src/server/workspaceSync.ts +203 -0
- package/src/types.ts +29 -27
- package/dist/assets/bindings_wasm_bg-9w8E-TmY.wasm +0 -0
- package/dist/assets/buildWorker-BZlya278.js +0 -687
- package/dist/assets/index-5qf102c2.css +0 -1
- package/dist/assets/index-dSJDl5x9.js +0 -356
- package/dist/assets/worker-1oQYaz-F.js +0 -342
- package/dist/assets/worker-BfgPD8zR.js +0 -125
- package/dist/assets/worker-CoJaqtpi.js +0 -436
|
@@ -4,6 +4,8 @@ import express from 'express';
|
|
|
4
4
|
import fs from 'fs';
|
|
5
5
|
import http from 'http';
|
|
6
6
|
import path from 'path';
|
|
7
|
+
import { createAccessControl } from './accessControl.js';
|
|
8
|
+
import { createWorkspacePathResolver } from './workspaceSecurity.js';
|
|
7
9
|
import ShareDB from 'sharedb';
|
|
8
10
|
import { fileURLToPath } from 'url';
|
|
9
11
|
import { WebSocketServer } from 'ws';
|
|
@@ -15,6 +17,14 @@ import { handleAIChatMessage } from './handleAIChatMessage.js';
|
|
|
15
17
|
import { isDirectory } from './isDirectory.js';
|
|
16
18
|
import { createToken } from './livekit.js';
|
|
17
19
|
import './setupEnv.js';
|
|
20
|
+
import { diff } from '../ot.js';
|
|
21
|
+
import {
|
|
22
|
+
copyFiles,
|
|
23
|
+
diskMatchesBaseline,
|
|
24
|
+
readWorkspaceDisk,
|
|
25
|
+
reconcileDisk,
|
|
26
|
+
type WorkspaceFiles,
|
|
27
|
+
} from './workspaceSync.js';
|
|
18
28
|
|
|
19
29
|
// The time in milliseconds by which auto-saving is debounced.
|
|
20
30
|
const autoSaveDebounceTimeMS = 800;
|
|
@@ -45,6 +55,12 @@ export interface CreateVZCodeServerOptions {
|
|
|
45
55
|
* `true`. Tests can disable this and call `save()` directly.
|
|
46
56
|
*/
|
|
47
57
|
autoSave?: boolean;
|
|
58
|
+
/** Optional fixed capability token (otherwise generated per server). */
|
|
59
|
+
accessToken?: string;
|
|
60
|
+
/** Watch workspace files for out-of-editor edits (defaults to autoSave). */
|
|
61
|
+
watchFiles?: boolean;
|
|
62
|
+
/** Portable background rescan interval (default 1500ms). */
|
|
63
|
+
scanIntervalMS?: number;
|
|
48
64
|
}
|
|
49
65
|
|
|
50
66
|
export interface VZCodeServer {
|
|
@@ -53,12 +69,21 @@ export interface VZCodeServer {
|
|
|
53
69
|
shareDBBackend: ShareDB;
|
|
54
70
|
/** The server-side ShareDB document for the workspace. */
|
|
55
71
|
shareDBDoc: any;
|
|
72
|
+
/** Random per-server token used to mint scoped browser sessions. */
|
|
73
|
+
accessToken: string;
|
|
56
74
|
/** Resolves once the initial ShareDB document has been created. */
|
|
57
75
|
ready: Promise<void>;
|
|
58
76
|
/** Write changed files to disk (same routine used by auto-save). */
|
|
59
77
|
save: () => void;
|
|
78
|
+
/** Reconcile disk edits immediately; also useful for deterministic tests. */
|
|
79
|
+
syncFromDisk: () => Promise<void>;
|
|
80
|
+
/** Resolve a conflict explicitly by choosing the disk or editor version. */
|
|
81
|
+
resolveConflict: (
|
|
82
|
+
name: string,
|
|
83
|
+
choice: 'disk' | 'editor',
|
|
84
|
+
) => Promise<void>;
|
|
60
85
|
/** Start listening; resolves with the actual bound port. */
|
|
61
|
-
listen: (port: number) => Promise<number>;
|
|
86
|
+
listen: (port: number, host?: string) => Promise<number>;
|
|
62
87
|
/** Stop the HTTP/WebSocket servers and close ShareDB. */
|
|
63
88
|
close: () => Promise<void>;
|
|
64
89
|
}
|
|
@@ -75,12 +100,15 @@ export const createVZCodeServer = (
|
|
|
75
100
|
): VZCodeServer => {
|
|
76
101
|
const cwd = options.cwd ?? process.cwd();
|
|
77
102
|
const autoSave = options.autoSave ?? true;
|
|
103
|
+
const watchFiles = options.watchFiles ?? autoSave;
|
|
104
|
+
const scanIntervalMS = options.scanIntervalMS ?? 1500;
|
|
105
|
+
const workspace = createWorkspacePathResolver(cwd);
|
|
106
|
+
const access = createAccessControl(options.accessToken);
|
|
78
107
|
|
|
79
108
|
// Resolve file names from the ShareDB document against the workspace
|
|
80
109
|
// root, so the server can operate on a directory other than the
|
|
81
110
|
// process's current working directory.
|
|
82
|
-
const resolvePath =
|
|
83
|
-
path.resolve(cwd, name);
|
|
111
|
+
const resolvePath = workspace.resolve;
|
|
84
112
|
|
|
85
113
|
const serveStaticDir =
|
|
86
114
|
options.serveStaticDir === undefined
|
|
@@ -92,15 +120,20 @@ export const createVZCodeServer = (
|
|
|
92
120
|
)
|
|
93
121
|
: options.serveStaticDir;
|
|
94
122
|
|
|
95
|
-
const initialDocument =
|
|
96
|
-
|
|
97
|
-
|
|
123
|
+
const initialDocument = {
|
|
124
|
+
...computeInitialDocument({
|
|
125
|
+
fullPath: workspace.root,
|
|
126
|
+
}),
|
|
127
|
+
workspaceConflicts: [] as string[],
|
|
128
|
+
};
|
|
129
|
+
workspace.validateFiles(initialDocument.files);
|
|
98
130
|
|
|
99
131
|
// Register our custom OT type,
|
|
100
132
|
// because it does not ship with ShareDB.
|
|
101
133
|
ShareDB.types.register(json1Presence.type);
|
|
102
134
|
|
|
103
135
|
const app = express();
|
|
136
|
+
access.install(app);
|
|
104
137
|
|
|
105
138
|
// Use ShareDB over WebSocket
|
|
106
139
|
const shareDBBackend = new ShareDB({
|
|
@@ -108,8 +141,47 @@ export const createVZCodeServer = (
|
|
|
108
141
|
presence: true,
|
|
109
142
|
doNotForwardSendPresenceErrorsToClient: true,
|
|
110
143
|
});
|
|
144
|
+
// Validate the committed post-operation snapshot, not merely the
|
|
145
|
+
// operation payload. A rejected submission must never reach autosave.
|
|
146
|
+
shareDBBackend.use('commit', (context: any, next) => {
|
|
147
|
+
try {
|
|
148
|
+
if (
|
|
149
|
+
context.collection !== 'documents' ||
|
|
150
|
+
context.id !== '1'
|
|
151
|
+
) {
|
|
152
|
+
throw new Error('Unauthorized ShareDB document');
|
|
153
|
+
}
|
|
154
|
+
workspace.validateFiles(
|
|
155
|
+
context.snapshot?.data?.files,
|
|
156
|
+
);
|
|
157
|
+
next();
|
|
158
|
+
} catch (error: any) {
|
|
159
|
+
next(error);
|
|
160
|
+
}
|
|
161
|
+
});
|
|
111
162
|
const httpServer = http.createServer(app);
|
|
112
|
-
|
|
163
|
+
// Authentication is enforced before ShareDB attaches to the socket.
|
|
164
|
+
const wss = new WebSocketServer({ noServer: true });
|
|
165
|
+
httpServer.on('upgrade', (req, socket, head) => {
|
|
166
|
+
const pathname = (req.url || '').split('?')[0];
|
|
167
|
+
if (pathname !== '/ws') {
|
|
168
|
+
socket.write(
|
|
169
|
+
'HTTP/1.1 404 Not Found\r\nConnection: close\r\n\r\n',
|
|
170
|
+
);
|
|
171
|
+
socket.destroy();
|
|
172
|
+
return;
|
|
173
|
+
}
|
|
174
|
+
if (!access.authorized(req)) {
|
|
175
|
+
socket.write(
|
|
176
|
+
'HTTP/1.1 401 Unauthorized\r\nConnection: close\r\n\r\n',
|
|
177
|
+
);
|
|
178
|
+
socket.destroy();
|
|
179
|
+
return;
|
|
180
|
+
}
|
|
181
|
+
wss.handleUpgrade(req, socket, head, (ws) => {
|
|
182
|
+
wss.emit('connection', ws, req);
|
|
183
|
+
});
|
|
184
|
+
});
|
|
113
185
|
wss.on('connection', (ws) => {
|
|
114
186
|
const clientStream = new WebSocketJSONStream(ws);
|
|
115
187
|
shareDBBackend.listen(clientStream);
|
|
@@ -144,144 +216,338 @@ export const createVZCodeServer = (
|
|
|
144
216
|
);
|
|
145
217
|
|
|
146
218
|
// The state of the document when files were last auto-saved.
|
|
147
|
-
let previousDocument =
|
|
219
|
+
let previousDocument: { files: WorkspaceFiles } = {
|
|
220
|
+
...initialDocument,
|
|
221
|
+
files: copyFiles(initialDocument.files),
|
|
222
|
+
};
|
|
223
|
+
let identities: Record<string, string | undefined> = {};
|
|
224
|
+
const initialDisk = readWorkspaceDisk(workspace.root);
|
|
225
|
+
for (const [id, file] of Object.entries(
|
|
226
|
+
previousDocument.files,
|
|
227
|
+
)) {
|
|
228
|
+
identities[id] = initialDisk.get(file.name)?.identity;
|
|
229
|
+
}
|
|
230
|
+
let conflictNames: string[] = [];
|
|
231
|
+
let runningSync: Promise<void> | null = null;
|
|
232
|
+
let closing = false;
|
|
233
|
+
let watcher: fs.FSWatcher | undefined;
|
|
234
|
+
let interval: ReturnType<typeof setInterval> | undefined;
|
|
235
|
+
let changeTimer:
|
|
236
|
+
ReturnType<typeof setTimeout> | undefined;
|
|
148
237
|
|
|
149
238
|
// Saves the files that changed.
|
|
150
239
|
const save = () => {
|
|
151
|
-
const currentDocument =
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
240
|
+
const currentDocument: { files: WorkspaceFiles } =
|
|
241
|
+
shareDBDoc.data;
|
|
242
|
+
if (conflictNames.length) {
|
|
243
|
+
throw new Error(
|
|
244
|
+
'Workspace conflicts require resolution: ' +
|
|
245
|
+
conflictNames.join(', '),
|
|
246
|
+
);
|
|
247
|
+
}
|
|
248
|
+
// Do not write a single byte if the disk has changed since the
|
|
249
|
+
// last successful save/import. This also protects against events
|
|
250
|
+
// arriving before a filesystem watch callback is delivered.
|
|
251
|
+
if (
|
|
252
|
+
!diskMatchesBaseline(
|
|
253
|
+
previousDocument.files,
|
|
254
|
+
readWorkspaceDisk(workspace.root),
|
|
255
|
+
)
|
|
256
|
+
) {
|
|
257
|
+
void syncFromDisk().catch((error) =>
|
|
258
|
+
console.error(
|
|
259
|
+
'VZCode disk reconciliation failed:',
|
|
260
|
+
error,
|
|
261
|
+
),
|
|
262
|
+
);
|
|
263
|
+
throw new Error(
|
|
264
|
+
'External filesystem change detected; save deferred',
|
|
265
|
+
);
|
|
266
|
+
}
|
|
267
|
+
// Fail closed before the first write in this batch. Repeat checks
|
|
268
|
+
// at each filesystem call to reject newly created symlinks.
|
|
269
|
+
workspace.validateFiles(previousDocument.files);
|
|
270
|
+
workspace.validateFiles(currentDocument.files);
|
|
271
|
+
|
|
272
|
+
const before = previousDocument.files;
|
|
273
|
+
const after = currentDocument.files;
|
|
274
|
+
const oldEntries = Object.entries(before);
|
|
275
|
+
const newEntries = Object.entries(after);
|
|
276
|
+
const depth = (name: string) => name.split('/').length;
|
|
277
|
+
const removed = oldEntries.filter(([id]) => !after[id]);
|
|
278
|
+
const changed = newEntries.filter(
|
|
279
|
+
([id]) => !!before[id],
|
|
280
|
+
);
|
|
281
|
+
const created = newEntries.filter(
|
|
282
|
+
([id]) => !before[id],
|
|
283
|
+
);
|
|
158
284
|
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
const buffer = Buffer.from(
|
|
172
|
-
current.text,
|
|
173
|
-
'base64',
|
|
174
|
-
);
|
|
175
|
-
fs.writeFileSync(
|
|
176
|
-
resolvePath(current.name),
|
|
177
|
-
buffer,
|
|
178
|
-
);
|
|
179
|
-
} else {
|
|
180
|
-
// Write non-image files as text
|
|
181
|
-
fs.writeFileSync(
|
|
182
|
-
resolvePath(current.name),
|
|
183
|
-
current.text,
|
|
184
|
-
);
|
|
185
|
-
}
|
|
186
|
-
}
|
|
285
|
+
// Create destination directories first, including renamed folders.
|
|
286
|
+
for (const [, file] of newEntries
|
|
287
|
+
.filter(([, file]) => isDirectory(file.name))
|
|
288
|
+
.sort(
|
|
289
|
+
(a, b) => depth(a[1].name) - depth(b[1].name),
|
|
290
|
+
)) {
|
|
291
|
+
if (!fs.existsSync(resolvePath(file.name))) {
|
|
292
|
+
fs.mkdirSync(resolvePath(file.name), {
|
|
293
|
+
recursive: true,
|
|
294
|
+
});
|
|
295
|
+
}
|
|
296
|
+
}
|
|
187
297
|
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
// all the files moved.
|
|
195
|
-
directoriesToDelete.push(previous.name);
|
|
196
|
-
} else {
|
|
197
|
-
const newDir = path.dirname(current.name);
|
|
198
|
-
|
|
199
|
-
// Check if the new directory exists, if not, create it
|
|
200
|
-
if (!fs.existsSync(resolvePath(newDir))) {
|
|
201
|
-
fs.mkdirSync(resolvePath(newDir), {
|
|
202
|
-
recursive: true,
|
|
203
|
-
});
|
|
204
|
-
}
|
|
298
|
+
// Delete removed files before moving another file into their path.
|
|
299
|
+
for (const [, file] of removed.filter(
|
|
300
|
+
([, file]) => !isDirectory(file.name),
|
|
301
|
+
)) {
|
|
302
|
+
fs.unlinkSync(resolvePath(file.name));
|
|
303
|
+
}
|
|
205
304
|
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
305
|
+
// Move surviving files before writing their updated contents. Moving
|
|
306
|
+
// only the leaf (not directories) preserves file IDs across folder
|
|
307
|
+
// renames and avoids writing to the old location.
|
|
308
|
+
for (const [id, current] of changed) {
|
|
309
|
+
const previous = before[id];
|
|
310
|
+
if (
|
|
311
|
+
previous.name !== current.name &&
|
|
312
|
+
!isDirectory(current.name)
|
|
313
|
+
) {
|
|
314
|
+
const destinationDir = path.dirname(current.name);
|
|
315
|
+
if (destinationDir !== '.') {
|
|
316
|
+
fs.mkdirSync(resolvePath(destinationDir), {
|
|
317
|
+
recursive: true,
|
|
318
|
+
});
|
|
212
319
|
}
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
// handle deleting files and directories.
|
|
216
|
-
if (previous && !current) {
|
|
217
|
-
const stats = fs.statSync(
|
|
320
|
+
fs.renameSync(
|
|
218
321
|
resolvePath(previous.name),
|
|
322
|
+
resolvePath(current.name),
|
|
219
323
|
);
|
|
220
|
-
//Check if the file path we are trying to delete is a directory
|
|
221
|
-
if (!stats.isDirectory()) {
|
|
222
|
-
fs.unlinkSync(resolvePath(previous.name));
|
|
223
|
-
} else {
|
|
224
|
-
//Performs directory deletion.
|
|
225
|
-
fs.rm(
|
|
226
|
-
resolvePath(previous.name),
|
|
227
|
-
{
|
|
228
|
-
recursive: true, //Makes sure that all files in directory are deleted.
|
|
229
|
-
},
|
|
230
|
-
(error) => {
|
|
231
|
-
if (error) {
|
|
232
|
-
console.log(error);
|
|
233
|
-
}
|
|
234
|
-
},
|
|
235
|
-
);
|
|
236
|
-
}
|
|
237
324
|
}
|
|
325
|
+
}
|
|
238
326
|
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
current.text,
|
|
247
|
-
'base64',
|
|
248
|
-
);
|
|
249
|
-
fs.writeFileSync(
|
|
250
|
-
resolvePath(current.name),
|
|
251
|
-
buffer,
|
|
252
|
-
);
|
|
253
|
-
} else {
|
|
254
|
-
// Write non-image files as text
|
|
255
|
-
fs.writeFileSync(
|
|
256
|
-
resolvePath(current.name),
|
|
257
|
-
current.text,
|
|
258
|
-
);
|
|
259
|
-
}
|
|
260
|
-
} else {
|
|
261
|
-
fs.mkdirSync(resolvePath(current.name), {
|
|
327
|
+
// Apply edited contents and new files, including binary images.
|
|
328
|
+
for (const [id, current] of [...changed, ...created]) {
|
|
329
|
+
if (isDirectory(current.name)) continue;
|
|
330
|
+
if (!before[id] || before[id].text !== current.text) {
|
|
331
|
+
const parent = path.dirname(current.name);
|
|
332
|
+
if (parent !== '.') {
|
|
333
|
+
fs.mkdirSync(resolvePath(parent), {
|
|
262
334
|
recursive: true,
|
|
263
335
|
});
|
|
264
336
|
}
|
|
337
|
+
const data = isImageFile(current.name)
|
|
338
|
+
? Buffer.from(current.text as string, 'base64')
|
|
339
|
+
: (current.text as string);
|
|
340
|
+
workspace.writeFile(current.name, data);
|
|
265
341
|
}
|
|
266
342
|
}
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
343
|
+
|
|
344
|
+
// Remove obsolete directories after moving or removing children.
|
|
345
|
+
const obsoleteDirs = [
|
|
346
|
+
...removed
|
|
347
|
+
.filter(([, file]) => isDirectory(file.name))
|
|
348
|
+
.map(([, file]) => file.name),
|
|
349
|
+
...changed
|
|
350
|
+
.filter(
|
|
351
|
+
([id, file]) =>
|
|
352
|
+
isDirectory(before[id].name) &&
|
|
353
|
+
before[id].name !== file.name,
|
|
354
|
+
)
|
|
355
|
+
.map(([id]) => before[id].name),
|
|
356
|
+
];
|
|
357
|
+
for (const name of [...new Set(obsoleteDirs)].sort(
|
|
358
|
+
(a, b) => depth(b) - depth(a),
|
|
359
|
+
)) {
|
|
360
|
+
const full = resolvePath(name);
|
|
361
|
+
if (fs.existsSync(full)) {
|
|
362
|
+
fs.rmSync(full, { recursive: true });
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
previousDocument = {
|
|
367
|
+
...currentDocument,
|
|
368
|
+
files: copyFiles(currentDocument.files),
|
|
369
|
+
};
|
|
370
|
+
const disk = readWorkspaceDisk(workspace.root);
|
|
371
|
+
identities = Object.fromEntries(
|
|
372
|
+
Object.entries(previousDocument.files).map(
|
|
373
|
+
([id, file]) => [id, disk.get(file.name)?.identity],
|
|
374
|
+
),
|
|
375
|
+
);
|
|
376
|
+
};
|
|
377
|
+
|
|
378
|
+
const publishDiskUpdate = (
|
|
379
|
+
files: WorkspaceFiles,
|
|
380
|
+
conflicts: string[],
|
|
381
|
+
): Promise<void> => {
|
|
382
|
+
const current = shareDBDoc.data;
|
|
383
|
+
const next = {
|
|
384
|
+
...current,
|
|
385
|
+
files,
|
|
386
|
+
workspaceConflicts: conflicts,
|
|
387
|
+
};
|
|
388
|
+
const operation = diff(current, next);
|
|
389
|
+
if (
|
|
390
|
+
!operation ||
|
|
391
|
+
(Array.isArray(operation) && !operation.length)
|
|
392
|
+
) {
|
|
393
|
+
return Promise.resolve();
|
|
394
|
+
}
|
|
395
|
+
return new Promise<void>((resolve, reject) => {
|
|
396
|
+
shareDBDoc.submitOp(
|
|
397
|
+
operation,
|
|
398
|
+
{ source: 'filesystem' },
|
|
399
|
+
(error: Error | null) => {
|
|
400
|
+
if (error) reject(error);
|
|
401
|
+
else resolve();
|
|
280
402
|
},
|
|
281
403
|
);
|
|
404
|
+
});
|
|
405
|
+
};
|
|
406
|
+
|
|
407
|
+
/** Serialized rescans avoid overlapping external imports. */
|
|
408
|
+
const syncFromDisk = (): Promise<void> => {
|
|
409
|
+
if (runningSync) return runningSync;
|
|
410
|
+
if (closing) return Promise.resolve();
|
|
411
|
+
const scan = async () => {
|
|
412
|
+
const disk = readWorkspaceDisk(workspace.root);
|
|
413
|
+
if (
|
|
414
|
+
!conflictNames.length &&
|
|
415
|
+
diskMatchesBaseline(previousDocument.files, disk)
|
|
416
|
+
) {
|
|
417
|
+
identities = Object.fromEntries(
|
|
418
|
+
Object.entries(previousDocument.files).map(
|
|
419
|
+
([id, file]) => [
|
|
420
|
+
id,
|
|
421
|
+
disk.get(file.name)?.identity,
|
|
422
|
+
],
|
|
423
|
+
),
|
|
424
|
+
);
|
|
425
|
+
return;
|
|
426
|
+
}
|
|
427
|
+
const plan = reconcileDisk({
|
|
428
|
+
baseline: previousDocument.files,
|
|
429
|
+
current: shareDBDoc.data.files,
|
|
430
|
+
disk,
|
|
431
|
+
identities,
|
|
432
|
+
});
|
|
433
|
+
await publishDiskUpdate(plan.files, plan.conflicts);
|
|
434
|
+
previousDocument = {
|
|
435
|
+
...previousDocument,
|
|
436
|
+
files: plan.baseline,
|
|
437
|
+
};
|
|
438
|
+
identities = plan.identities;
|
|
439
|
+
conflictNames = plan.conflicts;
|
|
440
|
+
|
|
441
|
+
// A local edit may have been pending while an unrelated external
|
|
442
|
+
// change was imported. Resume its save after the OT is committed.
|
|
443
|
+
if (
|
|
444
|
+
autoSave &&
|
|
445
|
+
!conflictNames.length &&
|
|
446
|
+
!diskMatchesBaseline(plan.files, disk)
|
|
447
|
+
) {
|
|
448
|
+
debounceSave();
|
|
449
|
+
}
|
|
450
|
+
};
|
|
451
|
+
runningSync = scan().finally(() => {
|
|
452
|
+
runningSync = null;
|
|
453
|
+
});
|
|
454
|
+
return runningSync;
|
|
455
|
+
};
|
|
456
|
+
|
|
457
|
+
const resolveConflict = async (
|
|
458
|
+
name: string,
|
|
459
|
+
choice: 'disk' | 'editor',
|
|
460
|
+
): Promise<void> => {
|
|
461
|
+
await syncFromDisk();
|
|
462
|
+
if (!conflictNames.includes(name)) {
|
|
463
|
+
throw new Error(
|
|
464
|
+
'Unknown or already resolved conflict',
|
|
465
|
+
);
|
|
466
|
+
}
|
|
467
|
+
const disk = readWorkspaceDisk(workspace.root);
|
|
468
|
+
const nextFiles = copyFiles(shareDBDoc.data.files);
|
|
469
|
+
const nextBaseline = copyFiles(previousDocument.files);
|
|
470
|
+
const id =
|
|
471
|
+
Object.keys(nextBaseline).find(
|
|
472
|
+
(key) => nextBaseline[key].name === name,
|
|
473
|
+
) ??
|
|
474
|
+
Object.keys(nextFiles).find(
|
|
475
|
+
(key) => nextFiles[key].name === name,
|
|
476
|
+
);
|
|
477
|
+
if (!id) throw new Error('Conflict entry disappeared');
|
|
478
|
+
|
|
479
|
+
let observed = disk.get(name);
|
|
480
|
+
// External renames may have changed the path but preserved the inode.
|
|
481
|
+
if (!observed && identities[id]) {
|
|
482
|
+
const candidates = Array.from(disk.values()).filter(
|
|
483
|
+
(file) => file.identity === identities[id],
|
|
484
|
+
);
|
|
485
|
+
if (candidates.length === 1) observed = candidates[0];
|
|
282
486
|
}
|
|
283
487
|
|
|
284
|
-
|
|
488
|
+
if (choice === 'disk') {
|
|
489
|
+
if (observed) {
|
|
490
|
+
nextFiles[id] = {
|
|
491
|
+
name: observed.name,
|
|
492
|
+
text: observed.text,
|
|
493
|
+
};
|
|
494
|
+
} else {
|
|
495
|
+
delete nextFiles[id];
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
if (observed) {
|
|
499
|
+
nextBaseline[id] = {
|
|
500
|
+
name: observed.name,
|
|
501
|
+
text: observed.text,
|
|
502
|
+
};
|
|
503
|
+
identities[id] = observed.identity;
|
|
504
|
+
} else {
|
|
505
|
+
delete nextBaseline[id];
|
|
506
|
+
delete identities[id];
|
|
507
|
+
}
|
|
508
|
+
const remaining = conflictNames.filter(
|
|
509
|
+
(conflict) => conflict !== name,
|
|
510
|
+
);
|
|
511
|
+
// All other tracked files must still match disk before allowing an
|
|
512
|
+
// editor-wins write. A new disk change requires another reconciliation.
|
|
513
|
+
if (
|
|
514
|
+
choice === 'editor' &&
|
|
515
|
+
!remaining.length &&
|
|
516
|
+
!diskMatchesBaseline(nextBaseline, disk)
|
|
517
|
+
) {
|
|
518
|
+
throw new Error(
|
|
519
|
+
'Disk changed again; retry after reconciling',
|
|
520
|
+
);
|
|
521
|
+
}
|
|
522
|
+
await publishDiskUpdate(nextFiles, remaining);
|
|
523
|
+
previousDocument = {
|
|
524
|
+
...previousDocument,
|
|
525
|
+
files: nextBaseline,
|
|
526
|
+
};
|
|
527
|
+
conflictNames = remaining;
|
|
528
|
+
if (autoSave && !remaining.length) {
|
|
529
|
+
debounceSave();
|
|
530
|
+
}
|
|
531
|
+
};
|
|
532
|
+
|
|
533
|
+
const scheduleSync = () => {
|
|
534
|
+
if (closing) return;
|
|
535
|
+
clearTimeout(changeTimer);
|
|
536
|
+
changeTimer = setTimeout(() => {
|
|
537
|
+
void syncFromDisk().catch((error) =>
|
|
538
|
+
console.error('VZCode disk watcher failed:', error),
|
|
539
|
+
);
|
|
540
|
+
}, 80);
|
|
541
|
+
};
|
|
542
|
+
|
|
543
|
+
// Invalid paths or external filesystem changes must not crash the
|
|
544
|
+
// process from a delayed timer. Expose errors in logs for operators.
|
|
545
|
+
const autoSaveSafely = () => {
|
|
546
|
+
try {
|
|
547
|
+
save();
|
|
548
|
+
} catch (error) {
|
|
549
|
+
console.error('VZCode autosave refused:', error);
|
|
550
|
+
}
|
|
285
551
|
};
|
|
286
552
|
|
|
287
553
|
let lastExecutedTime = Date.now();
|
|
@@ -293,13 +559,13 @@ export const createVZCodeServer = (
|
|
|
293
559
|
const timeSinceLastExecution = now - lastExecutedTime;
|
|
294
560
|
|
|
295
561
|
if (timeSinceLastExecution > throttleTimeMS) {
|
|
296
|
-
|
|
562
|
+
autoSaveSafely();
|
|
297
563
|
lastExecutedTime = now;
|
|
298
564
|
} else {
|
|
299
565
|
// Clear the previous timeout and set a new one to save the last change
|
|
300
566
|
clearTimeout(lastChangeTimeout);
|
|
301
567
|
lastChangeTimeout = setTimeout(() => {
|
|
302
|
-
|
|
568
|
+
autoSaveSafely();
|
|
303
569
|
lastExecutedTime = now;
|
|
304
570
|
}, throttleTimeMS - timeSinceLastExecution);
|
|
305
571
|
}
|
|
@@ -310,7 +576,7 @@ export const createVZCodeServer = (
|
|
|
310
576
|
function debounceSave() {
|
|
311
577
|
clearTimeout(debounceTimeout);
|
|
312
578
|
debounceTimeout = setTimeout(
|
|
313
|
-
|
|
579
|
+
autoSaveSafely,
|
|
314
580
|
autoSaveDebounceTimeMS,
|
|
315
581
|
);
|
|
316
582
|
}
|
|
@@ -327,8 +593,9 @@ export const createVZCodeServer = (
|
|
|
327
593
|
if (subscribeError) return reject(subscribeError);
|
|
328
594
|
|
|
329
595
|
if (autoSave) {
|
|
330
|
-
//
|
|
331
|
-
shareDBDoc.on('op', () => {
|
|
596
|
+
// Filesystem imports must not trigger a write-back loop.
|
|
597
|
+
shareDBDoc.on('op', (_op, source) => {
|
|
598
|
+
if (source === 'filesystem') return;
|
|
332
599
|
if (shareDBDoc.data.isInteracting) {
|
|
333
600
|
throttleSave();
|
|
334
601
|
} else {
|
|
@@ -337,15 +604,68 @@ export const createVZCodeServer = (
|
|
|
337
604
|
});
|
|
338
605
|
}
|
|
339
606
|
|
|
607
|
+
if (watchFiles) {
|
|
608
|
+
// Root watcher gives low-latency notifications. The periodic
|
|
609
|
+
// recursive scan handles nested files and lost/coalesced events.
|
|
610
|
+
try {
|
|
611
|
+
watcher = fs.watch(
|
|
612
|
+
workspace.root,
|
|
613
|
+
scheduleSync,
|
|
614
|
+
);
|
|
615
|
+
watcher.on('error', (error) => {
|
|
616
|
+
console.error(
|
|
617
|
+
'VZCode watcher error:',
|
|
618
|
+
error,
|
|
619
|
+
);
|
|
620
|
+
});
|
|
621
|
+
} catch (error) {
|
|
622
|
+
// Some filesystems do not offer watch notifications.
|
|
623
|
+
console.warn(
|
|
624
|
+
'VZCode using polling-only filesystem monitoring:',
|
|
625
|
+
error,
|
|
626
|
+
);
|
|
627
|
+
}
|
|
628
|
+
interval = setInterval(
|
|
629
|
+
scheduleSync,
|
|
630
|
+
scanIntervalMS,
|
|
631
|
+
);
|
|
632
|
+
interval.unref();
|
|
633
|
+
}
|
|
340
634
|
resolve();
|
|
341
635
|
});
|
|
342
636
|
},
|
|
343
637
|
);
|
|
344
638
|
});
|
|
345
639
|
|
|
640
|
+
// Conflict decisions are explicit, authenticated user actions.
|
|
641
|
+
app.post(
|
|
642
|
+
'/workspace-conflicts/resolve',
|
|
643
|
+
access.requireAuth,
|
|
644
|
+
bodyParser.json(),
|
|
645
|
+
async (req, res) => {
|
|
646
|
+
const { name, choice } = req.body || {};
|
|
647
|
+
if (
|
|
648
|
+
typeof name !== 'string' ||
|
|
649
|
+
(choice !== 'disk' && choice !== 'editor')
|
|
650
|
+
) {
|
|
651
|
+
res
|
|
652
|
+
.status(400)
|
|
653
|
+
.json({ error: 'Invalid conflict resolution' });
|
|
654
|
+
return;
|
|
655
|
+
}
|
|
656
|
+
try {
|
|
657
|
+
await resolveConflict(name, choice);
|
|
658
|
+
res.sendStatus(204);
|
|
659
|
+
} catch (error: any) {
|
|
660
|
+
res.status(409).json({ error: error.message });
|
|
661
|
+
}
|
|
662
|
+
},
|
|
663
|
+
);
|
|
664
|
+
|
|
346
665
|
// Handle AI Assist requests.
|
|
347
666
|
app.post(
|
|
348
667
|
'/ai-assist',
|
|
668
|
+
access.requireAuth,
|
|
349
669
|
bodyParser.json(),
|
|
350
670
|
handleAIAssist(shareDBDoc),
|
|
351
671
|
);
|
|
@@ -353,6 +673,7 @@ export const createVZCodeServer = (
|
|
|
353
673
|
// Handle AI Copilot requests.
|
|
354
674
|
app.post(
|
|
355
675
|
'/ai-copilot',
|
|
676
|
+
access.requireAuth,
|
|
356
677
|
bodyParser.json(),
|
|
357
678
|
handleAICopilot(),
|
|
358
679
|
);
|
|
@@ -360,6 +681,7 @@ export const createVZCodeServer = (
|
|
|
360
681
|
// Handle AI Chat Message requests.
|
|
361
682
|
app.post(
|
|
362
683
|
'/ai-chat-message',
|
|
684
|
+
access.requireAuth,
|
|
363
685
|
bodyParser.json(),
|
|
364
686
|
handleAIChatMessage({
|
|
365
687
|
shareDBDoc,
|
|
@@ -368,16 +690,26 @@ export const createVZCodeServer = (
|
|
|
368
690
|
);
|
|
369
691
|
|
|
370
692
|
// Livekit Token Generator
|
|
371
|
-
app.get(
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
693
|
+
app.get(
|
|
694
|
+
'/livekit-token',
|
|
695
|
+
access.requireAuth,
|
|
696
|
+
async (req, res) => {
|
|
697
|
+
const { room, username } = req.query;
|
|
698
|
+
res.send(await createToken(room, username));
|
|
699
|
+
},
|
|
700
|
+
);
|
|
375
701
|
|
|
376
|
-
const listen = (
|
|
702
|
+
const listen = (
|
|
703
|
+
port: number,
|
|
704
|
+
host?: string,
|
|
705
|
+
): Promise<number> =>
|
|
377
706
|
new Promise((resolve, reject) => {
|
|
378
|
-
const onError = (error: Error) =>
|
|
707
|
+
const onError = (error: Error) => {
|
|
708
|
+
httpServer.removeListener('error', onError);
|
|
709
|
+
reject(error);
|
|
710
|
+
};
|
|
379
711
|
httpServer.once('error', onError);
|
|
380
|
-
|
|
712
|
+
const onListening = () => {
|
|
381
713
|
httpServer.removeListener('error', onError);
|
|
382
714
|
const address = httpServer.address();
|
|
383
715
|
const actualPort =
|
|
@@ -385,13 +717,27 @@ export const createVZCodeServer = (
|
|
|
385
717
|
? address.port
|
|
386
718
|
: port;
|
|
387
719
|
resolve(actualPort);
|
|
388
|
-
}
|
|
720
|
+
};
|
|
721
|
+
if (host) {
|
|
722
|
+
httpServer.listen(port, host, onListening);
|
|
723
|
+
} else {
|
|
724
|
+
httpServer.listen(port, onListening);
|
|
725
|
+
}
|
|
389
726
|
});
|
|
390
727
|
|
|
391
728
|
const close = (): Promise<void> =>
|
|
392
729
|
new Promise((resolve, reject) => {
|
|
730
|
+
closing = true;
|
|
731
|
+
watcher?.close();
|
|
732
|
+
clearInterval(interval);
|
|
733
|
+
clearTimeout(changeTimer);
|
|
393
734
|
clearTimeout(lastChangeTimeout);
|
|
394
735
|
clearTimeout(debounceTimeout);
|
|
736
|
+
access.close();
|
|
737
|
+
// An upgraded WebSocket otherwise prevents CLI shutdown.
|
|
738
|
+
for (const client of wss.clients) {
|
|
739
|
+
client.terminate();
|
|
740
|
+
}
|
|
395
741
|
wss.close(() => {
|
|
396
742
|
httpServer.close((error) => {
|
|
397
743
|
if (error) return reject(error);
|
|
@@ -405,8 +751,11 @@ export const createVZCodeServer = (
|
|
|
405
751
|
httpServer,
|
|
406
752
|
shareDBBackend,
|
|
407
753
|
shareDBDoc,
|
|
754
|
+
accessToken: access.token,
|
|
408
755
|
ready,
|
|
409
756
|
save,
|
|
757
|
+
syncFromDisk,
|
|
758
|
+
resolveConflict,
|
|
410
759
|
listen,
|
|
411
760
|
close,
|
|
412
761
|
};
|