@zmskco/dsh-notebook 0.1.0 → 0.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/LICENSE +23 -23
- package/README.md +29 -75
- package/assets/material-icons/LICENSE.txt +8 -0
- package/assets/material-icons/NOTICE.md +13 -0
- package/cordis.patch.yml +4 -2
- package/icon.png +0 -0
- package/lib/client.js +6803 -261
- package/lib/http/api.js +69 -14
- package/lib/index.js +19 -8
- package/lib/story/bootstrap.js +4 -9
- package/lib/story/manuscript.js +137 -14
- package/lib/story/project.js +263 -30
- package/lib/story/readme.js +9 -9
- package/lib/story/root.js +12 -9
- package/lib/story/sandbox.js +33 -106
- package/lib/story/workspace.js +1 -15
- package/locale/en.json +6 -6
- package/locale/zh.json +6 -6
- package/package.json +94 -89
- package/icon.svg +0 -9
- package/lib/config.js +0 -43
- package/lib/http/media.js +0 -187
- package/lib/render/backend.js +0 -247
- package/lib/render/jobs.js +0 -267
- package/lib/render/png.js +0 -125
- package/lib/tools.js +0 -341
package/lib/story/project.js
CHANGED
|
@@ -6,22 +6,39 @@
|
|
|
6
6
|
* returns is relative to it.
|
|
7
7
|
*
|
|
8
8
|
* Text goes through `ctx.fs`, so reads and writes inherit the Harness filesystem
|
|
9
|
-
* provider, its sandbox policy, and its canonicalization.
|
|
10
|
-
*
|
|
11
|
-
*
|
|
9
|
+
* provider, its sandbox policy, and its canonicalization. Two operations have no
|
|
10
|
+
* provider equivalent and reach `node:fs` instead — creating a directory, and
|
|
11
|
+
* moving or removing one — always at a path the provider already resolved and
|
|
12
|
+
* proved is inside the root, exactly as {@link ProjectStore.createDirectory} does.
|
|
13
|
+
*
|
|
14
|
+
* Files are different, and deliberately so: deleting and renaming one goes through
|
|
15
|
+
* `ctx.fs.deleteFile` / `moveFile`, which stamp the same sandbox policy a write
|
|
16
|
+
* does. A workspace pinned outside the deployment boundary is therefore writable
|
|
17
|
+
* here and still stamped correctly, and a read-only deployment refuses it at the
|
|
18
|
+
* seam rather than trusting this file to remember the rule.
|
|
12
19
|
*/
|
|
13
|
-
import { mkdir } from 'node:fs/promises';
|
|
14
|
-
import { join } from 'node:path';
|
|
20
|
+
import { mkdir, rename, rm } from 'node:fs/promises';
|
|
21
|
+
import { dirname, join } from 'node:path';
|
|
15
22
|
import { NotebookError, asNotebookError } from '../errors.js';
|
|
16
23
|
import { assertPathName, assertRelativePath } from './paths.js';
|
|
17
|
-
import { countChars, isMissing,
|
|
24
|
+
import { countChars, classifyFileName, isMissing, isProbablyText, MAX_TREE_DEPTH, MAX_TREE_ENTRIES, SNIFF_BYTES } from './manuscript.js';
|
|
18
25
|
/**
|
|
19
|
-
*
|
|
26
|
+
* Directory names the tree never descends into.
|
|
20
27
|
*
|
|
21
|
-
*
|
|
22
|
-
*
|
|
28
|
+
* These are the two trees that are not the author's work and are large enough to
|
|
29
|
+
* bury it: version-control internals and installed packages. Everything else is
|
|
30
|
+
* listed — including dotfiles and dot-folders — because a tree that hides a file
|
|
31
|
+
* the filesystem has is a tree the user cannot trust.
|
|
23
32
|
*/
|
|
24
|
-
const SKIP_DIRS = new Set(['node_modules']);
|
|
33
|
+
const SKIP_DIRS = new Set(['.git', '.hg', '.svn', 'node_modules']);
|
|
34
|
+
/**
|
|
35
|
+
* How many unknown-extension files one listing may sniff.
|
|
36
|
+
*
|
|
37
|
+
* A workspace can hold thousands of extensionless blobs, and the tree is drawn on
|
|
38
|
+
* every page load; this keeps that bounded while still covering the normal case —
|
|
39
|
+
* a repository whose files are simply extensions this build has never heard of.
|
|
40
|
+
*/
|
|
41
|
+
const MAX_SNIFFS_PER_LISTING = 400;
|
|
25
42
|
/**
|
|
26
43
|
* The project id the panel uses for the workspace itself.
|
|
27
44
|
*
|
|
@@ -132,17 +149,35 @@ export class ProjectStore {
|
|
|
132
149
|
/**
|
|
133
150
|
* List every file and directory under the root.
|
|
134
151
|
*
|
|
135
|
-
* Dot entries are
|
|
136
|
-
*
|
|
137
|
-
*
|
|
152
|
+
* **Everything is listed.** Dot entries are files and folders like any other
|
|
153
|
+
* (`.gitignore`, `.vscode/`), so they are listed too; only the two tool trees
|
|
154
|
+
* that are not the author's work — version-control internals and installed
|
|
155
|
+
* packages — are skipped, because they would bury the workspace instead of
|
|
156
|
+
* describing it. A file the editor cannot open is still listed, as `binary`:
|
|
157
|
+
* the panel explains on click why it cannot be previewed, which is an answer a
|
|
158
|
+
* reader can act on, while a missing row is indistinguishable from a lost file.
|
|
159
|
+
*
|
|
160
|
+
* Sniffing is for classification, not for filtering, and is bounded per listing:
|
|
161
|
+
* a workspace of thousands of extensionless blobs would otherwise open thousands
|
|
162
|
+
* of files to draw one tree. Past the budget an unknown type is labelled `text`
|
|
163
|
+
* — the panel's read decides for real, and a mislabel costs one explanatory
|
|
164
|
+
* message rather than a hidden file.
|
|
138
165
|
* @param project - an opened root.
|
|
139
|
-
* @returns the entries, sorted by path.
|
|
166
|
+
* @returns the entries, sorted by path, and whether the walk hit its cap.
|
|
140
167
|
*/
|
|
141
168
|
async listFiles(project) {
|
|
142
169
|
const out = [];
|
|
170
|
+
// Shared by the whole walk: one budget per listing, not per directory.
|
|
171
|
+
let sniffs = MAX_SNIFFS_PER_LISTING;
|
|
172
|
+
let truncated = false;
|
|
143
173
|
const walk = async (dir, relDir, depth) => {
|
|
144
|
-
|
|
174
|
+
// Both guards are *partial* listings, not a complete one: nesting past the
|
|
175
|
+
// depth cap or entries past the count cap means files exist that this answer
|
|
176
|
+
// does not contain, and the panel has to be able to say so.
|
|
177
|
+
if (depth > MAX_TREE_DEPTH) {
|
|
178
|
+
truncated = true;
|
|
145
179
|
return;
|
|
180
|
+
}
|
|
146
181
|
let entries;
|
|
147
182
|
try {
|
|
148
183
|
entries = await this.fs.listDir(dir);
|
|
@@ -152,10 +187,10 @@ export class ProjectStore {
|
|
|
152
187
|
return;
|
|
153
188
|
}
|
|
154
189
|
for (const entry of entries) {
|
|
155
|
-
if (out.length >= MAX_TREE_ENTRIES)
|
|
190
|
+
if (out.length >= MAX_TREE_ENTRIES) {
|
|
191
|
+
truncated = true;
|
|
156
192
|
return;
|
|
157
|
-
|
|
158
|
-
continue;
|
|
193
|
+
}
|
|
159
194
|
if (entry.type === 'directory' && SKIP_DIRS.has(entry.name))
|
|
160
195
|
continue;
|
|
161
196
|
// Re-checked per child rather than trusting the walk, so a symlink
|
|
@@ -170,31 +205,66 @@ export class ProjectStore {
|
|
|
170
205
|
}
|
|
171
206
|
if (entry.type !== 'file')
|
|
172
207
|
continue;
|
|
173
|
-
if (!isTextFile(entry.name))
|
|
174
|
-
continue;
|
|
175
208
|
const size = entry.size ?? (await this.fs.stat(entry.target).catch(() => undefined))?.size ?? 0;
|
|
176
|
-
|
|
209
|
+
const classified = classifyFileName(entry.name);
|
|
210
|
+
let kind = 'text';
|
|
211
|
+
if (classified === 'binary') {
|
|
212
|
+
kind = 'binary';
|
|
213
|
+
}
|
|
214
|
+
else if (classified === 'unknown' && sniffs > 0) {
|
|
215
|
+
// An unknown type is decided by its bytes — but listed either way.
|
|
216
|
+
sniffs -= 1;
|
|
217
|
+
kind = (await this.looksText(entry.target)) ? 'text' : 'binary';
|
|
218
|
+
}
|
|
219
|
+
out.push({ path: rel, name: entry.name, bytes: size, kind });
|
|
177
220
|
}
|
|
178
221
|
};
|
|
179
222
|
await walk(project.dir, '', 0);
|
|
180
223
|
// `numeric` so 第2章 sorts before 第10章 rather than after it, and Chinese
|
|
181
|
-
// collation so the folder names group the way a reader expects.
|
|
224
|
+
// collation so the folder names group the way a reader expects. The panel sorts
|
|
225
|
+
// the tree again by name (`state/file-tree`), which is the order the reader
|
|
226
|
+
// sees; this one only keeps the wire format predictable.
|
|
182
227
|
out.sort((left, right) => left.path.localeCompare(right.path, 'zh-Hans-CN', { numeric: true }));
|
|
183
|
-
return out;
|
|
228
|
+
return { files: out, truncated };
|
|
229
|
+
}
|
|
230
|
+
/**
|
|
231
|
+
* Decide by content whether an unknown-extension file is text.
|
|
232
|
+
*
|
|
233
|
+
* The window is a few kilobytes rather than the file: a 1 GiB blob must not be
|
|
234
|
+
* read to draw a row. A read that is refused is treated as text — the refusal
|
|
235
|
+
* says this session may not read the file, not that it is binary, and the open
|
|
236
|
+
* will report the real error at the call site.
|
|
237
|
+
* @param target - the resolved file.
|
|
238
|
+
* @returns true when its first bytes look like text.
|
|
239
|
+
*/
|
|
240
|
+
async looksText(target) {
|
|
241
|
+
try {
|
|
242
|
+
const head = await this.fs.readByteRange(target, { offset: 0, length: SNIFF_BYTES });
|
|
243
|
+
return isProbablyText(head);
|
|
244
|
+
}
|
|
245
|
+
catch {
|
|
246
|
+
return true;
|
|
247
|
+
}
|
|
184
248
|
}
|
|
185
249
|
/**
|
|
186
250
|
* Read one text file.
|
|
187
251
|
* @param project - an opened root.
|
|
188
252
|
* @param rel - project-relative POSIX path.
|
|
189
253
|
* @returns the document.
|
|
190
|
-
* @throws {NotebookError} `bad-request` when the
|
|
191
|
-
*
|
|
254
|
+
* @throws {NotebookError} `bad-request` when the file is binary, `not-found` when
|
|
255
|
+
* it is absent.
|
|
192
256
|
*/
|
|
193
257
|
async readTextFile(project, rel) {
|
|
194
258
|
const safe = assertRelativePath(rel);
|
|
195
|
-
|
|
196
|
-
|
|
259
|
+
const kind = classifyFileName(safe);
|
|
260
|
+
if (kind === 'binary')
|
|
261
|
+
throw new NotebookError('bad-request', `${safe} is a binary file this editor does not open`);
|
|
197
262
|
const target = await this.resolveInside(project, safe);
|
|
263
|
+
// An extension the editor does not know is opened unless its bytes say
|
|
264
|
+
// otherwise — that is the whole point of listing those files.
|
|
265
|
+
if (kind === 'unknown' && !(await this.looksText(target))) {
|
|
266
|
+
throw new NotebookError('bad-request', `${safe} is a binary file this editor does not open`);
|
|
267
|
+
}
|
|
198
268
|
let content;
|
|
199
269
|
try {
|
|
200
270
|
content = await this.fs.readText(target);
|
|
@@ -212,12 +282,12 @@ export class ProjectStore {
|
|
|
212
282
|
* @param rel - project-relative POSIX path.
|
|
213
283
|
* @param content - the full new contents.
|
|
214
284
|
* @returns the stored document.
|
|
215
|
-
* @throws {NotebookError} `bad-request` when the
|
|
285
|
+
* @throws {NotebookError} `bad-request` when the name is a binary type.
|
|
216
286
|
*/
|
|
217
287
|
async writeTextFile(project, rel, content) {
|
|
218
288
|
const safe = assertRelativePath(rel);
|
|
219
|
-
if (
|
|
220
|
-
throw new NotebookError('bad-request', `${safe} is
|
|
289
|
+
if (classifyFileName(safe) === 'binary')
|
|
290
|
+
throw new NotebookError('bad-request', `${safe} is a binary file this editor does not write`);
|
|
221
291
|
await this.ensureParent(project, safe);
|
|
222
292
|
const target = await this.resolveInside(project, safe);
|
|
223
293
|
try {
|
|
@@ -286,4 +356,167 @@ export class ProjectStore {
|
|
|
286
356
|
assertName(value) {
|
|
287
357
|
return assertPathName(value);
|
|
288
358
|
}
|
|
359
|
+
/**
|
|
360
|
+
* Whether a project-relative path names a directory.
|
|
361
|
+
*
|
|
362
|
+
* Asked before a rename or a delete decides *how* to do it: a file goes through
|
|
363
|
+
* the filesystem provider (which stamps the sandbox policy), a directory through
|
|
364
|
+
* `node:fs` (which has no provider equivalent). Guessing from a trailing slash
|
|
365
|
+
* would be guessing about the caller's intent rather than about the disk.
|
|
366
|
+
* @param project - an opened root.
|
|
367
|
+
* @param rel - a project-relative POSIX path.
|
|
368
|
+
* @returns true for a directory; false for a file or anything absent.
|
|
369
|
+
*/
|
|
370
|
+
async isDirectory(project, rel) {
|
|
371
|
+
const target = await this.resolveInside(project, rel);
|
|
372
|
+
const info = await this.fs.stat(target).catch(() => undefined);
|
|
373
|
+
return info?.type === 'directory';
|
|
374
|
+
}
|
|
375
|
+
/**
|
|
376
|
+
* Rename one file or directory, in place in its own folder.
|
|
377
|
+
*
|
|
378
|
+
* The new name is one path segment, validated exactly as a create name is, so a
|
|
379
|
+
* rename can never turn into a move: the folder stays where it was and only the
|
|
380
|
+
* last segment changes. That restriction is deliberate — "rename" in a file tree
|
|
381
|
+
* means the thing stays put, and a control that could silently relocate a chapter
|
|
382
|
+
* folder is not the gesture the author made.
|
|
383
|
+
*
|
|
384
|
+
* A file keeps its content byte for byte: `moveFile` publishes the source text at
|
|
385
|
+
* the destination and removes the source, both guarded by the write policy. A
|
|
386
|
+
* directory is moved with `rename(2)`, which is atomic and cheap even for a
|
|
387
|
+
* thousand chapters.
|
|
388
|
+
* @param project - an opened root.
|
|
389
|
+
* @param rel - the existing project-relative path.
|
|
390
|
+
* @param name - the new name for it, one segment.
|
|
391
|
+
* @returns the new project-relative path.
|
|
392
|
+
* @throws {NotebookError} `not-found` when the source is absent, `conflict` when
|
|
393
|
+
* the destination is taken.
|
|
394
|
+
*/
|
|
395
|
+
async renameEntry(project, rel, name) {
|
|
396
|
+
const safe = assertRelativePath(rel);
|
|
397
|
+
const newName = assertPathName(name);
|
|
398
|
+
const parent = safe.includes('/') ? safe.slice(0, safe.lastIndexOf('/')) : '';
|
|
399
|
+
const next = parent === '' ? newName : `${parent}/${newName}`;
|
|
400
|
+
// A rename to the name it already has is a no-op, not a conflict: the user may
|
|
401
|
+
// simply have confirmed an unchanged field, and both branches below would
|
|
402
|
+
// otherwise refuse a state that is already what was asked for.
|
|
403
|
+
if (next === safe)
|
|
404
|
+
return { path: safe };
|
|
405
|
+
const source = await this.resolveInside(project, safe);
|
|
406
|
+
if (await this.fs.stat(source) === undefined) {
|
|
407
|
+
throw new NotebookError('not-found', `${safe} does not exist`);
|
|
408
|
+
}
|
|
409
|
+
const destination = await this.resolveInside(project, next);
|
|
410
|
+
if (await this.fs.stat(destination) !== undefined) {
|
|
411
|
+
throw new NotebookError('conflict', `${next} already exists`);
|
|
412
|
+
}
|
|
413
|
+
if (await this.isDirectory(project, safe)) {
|
|
414
|
+
try {
|
|
415
|
+
await rename(this.fs.processPath(source), this.fs.processPath(destination));
|
|
416
|
+
}
|
|
417
|
+
catch (error) {
|
|
418
|
+
throw asNotebookError(error, `cannot rename ${safe}`);
|
|
419
|
+
}
|
|
420
|
+
return { path: next };
|
|
421
|
+
}
|
|
422
|
+
try {
|
|
423
|
+
await this.fs.moveFile(source, destination, {}, undefined, this.writePolicy);
|
|
424
|
+
}
|
|
425
|
+
catch (error) {
|
|
426
|
+
throw asNotebookError(error, `cannot rename ${safe}`);
|
|
427
|
+
}
|
|
428
|
+
return { path: next };
|
|
429
|
+
}
|
|
430
|
+
/**
|
|
431
|
+
* Delete one file or directory.
|
|
432
|
+
*
|
|
433
|
+
* A directory goes with everything under it, which is what a file manager does
|
|
434
|
+
* and what "delete this folder" means. The panel asks first and says how much is
|
|
435
|
+
* inside (see `api.delete`), because this is the one operation in the plugin that
|
|
436
|
+
* cannot be undone by typing.
|
|
437
|
+
*
|
|
438
|
+
* Containment is checked before anything is removed, so a symlink pointing out of
|
|
439
|
+
* the workspace is refused rather than followed.
|
|
440
|
+
* @param project - an opened root.
|
|
441
|
+
* @param rel - the project-relative path to remove.
|
|
442
|
+
* @returns the path that was removed, and whether it was a directory.
|
|
443
|
+
* @throws {NotebookError} `not-found` when the path is absent.
|
|
444
|
+
*/
|
|
445
|
+
async deleteEntry(project, rel) {
|
|
446
|
+
const safe = assertRelativePath(rel);
|
|
447
|
+
const target = await this.resolveInside(project, safe);
|
|
448
|
+
if (await this.fs.stat(target) === undefined) {
|
|
449
|
+
throw new NotebookError('not-found', `${safe} does not exist`);
|
|
450
|
+
}
|
|
451
|
+
if (await this.isDirectory(project, safe)) {
|
|
452
|
+
try {
|
|
453
|
+
await rm(this.fs.processPath(target), { recursive: true, force: false });
|
|
454
|
+
}
|
|
455
|
+
catch (error) {
|
|
456
|
+
throw asNotebookError(error, `cannot delete ${safe}`);
|
|
457
|
+
}
|
|
458
|
+
return { path: safe, directory: true };
|
|
459
|
+
}
|
|
460
|
+
try {
|
|
461
|
+
await this.fs.deleteFile(target, undefined, undefined, this.writePolicy);
|
|
462
|
+
}
|
|
463
|
+
catch (error) {
|
|
464
|
+
throw asNotebookError(error, `cannot delete ${safe}`);
|
|
465
|
+
}
|
|
466
|
+
return { path: safe, directory: false };
|
|
467
|
+
}
|
|
468
|
+
/**
|
|
469
|
+
* The absolute directory a path should be revealed in.
|
|
470
|
+
*
|
|
471
|
+
* "Show this in the file manager" means: the folder itself for a folder, and the
|
|
472
|
+
* folder containing it for a file — the file manager's own selection gesture is
|
|
473
|
+
* platform-specific and unreliable (`explorer /select,` starts a second shell that
|
|
474
|
+
* does not always surface), while opening a directory is the one launch this
|
|
475
|
+
* plugin has already measured on every platform.
|
|
476
|
+
* @param project - an opened root.
|
|
477
|
+
* @param rel - a project-relative path, or `''` for the workspace itself.
|
|
478
|
+
* @returns an absolute directory path, proven to be inside the root.
|
|
479
|
+
*/
|
|
480
|
+
async revealDirectoryFor(project, rel) {
|
|
481
|
+
if (rel === '')
|
|
482
|
+
return project.abs;
|
|
483
|
+
const target = await this.resolveInside(project, rel);
|
|
484
|
+
if (await this.isDirectory(project, rel))
|
|
485
|
+
return this.fs.processPath(target);
|
|
486
|
+
return dirname(this.fs.processPath(target));
|
|
487
|
+
}
|
|
488
|
+
/**
|
|
489
|
+
* How many files a directory holds, for a delete confirmation.
|
|
490
|
+
*
|
|
491
|
+
* Read through the same provider and the same skips as the tree, so the number
|
|
492
|
+
* the dialog shows is the number the author can see. A directory that cannot be
|
|
493
|
+
* read counts as unknown (`null`) rather than as empty.
|
|
494
|
+
* @param project - an opened root.
|
|
495
|
+
* @param rel - a project-relative directory path.
|
|
496
|
+
* @returns the recursive file count, or null when it could not be read.
|
|
497
|
+
*/
|
|
498
|
+
async countFilesUnder(project, rel) {
|
|
499
|
+
try {
|
|
500
|
+
let total = 0;
|
|
501
|
+
const walk = async (dir, depth) => {
|
|
502
|
+
if (depth > MAX_TREE_DEPTH || total >= MAX_TREE_ENTRIES)
|
|
503
|
+
return;
|
|
504
|
+
for (const entry of await this.fs.listDir(dir)) {
|
|
505
|
+
if (entry.type === 'directory') {
|
|
506
|
+
if (SKIP_DIRS.has(entry.name))
|
|
507
|
+
continue;
|
|
508
|
+
await walk(entry.target, depth + 1);
|
|
509
|
+
}
|
|
510
|
+
else if (entry.type === 'file') {
|
|
511
|
+
total += 1;
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
};
|
|
515
|
+
await walk(await this.resolveInside(project, rel), 0);
|
|
516
|
+
return total;
|
|
517
|
+
}
|
|
518
|
+
catch {
|
|
519
|
+
return null;
|
|
520
|
+
}
|
|
521
|
+
}
|
|
289
522
|
}
|
package/lib/story/readme.js
CHANGED
|
@@ -13,14 +13,14 @@
|
|
|
13
13
|
* @returns the markdown.
|
|
14
14
|
*/
|
|
15
15
|
export function workspaceReadme() {
|
|
16
|
-
return `# dsh-notepad
|
|
17
|
-
|
|
18
|
-
deepseek harness 简易的markdown笔记插件
|
|
19
|
-
|
|
20
|
-
- 支持目录树浏览,创建文件夹,创建文件
|
|
21
|
-
- 支持字体样式,大小,背景颜色调整
|
|
22
|
-
- 支持字数显示,自动保存
|
|
23
|
-
- 支持AI对话框功能
|
|
24
|
-
- 重命名,删除,需要在系统资源管理器完成,或提示AI操作
|
|
16
|
+
return `# dsh-notepad
|
|
17
|
+
|
|
18
|
+
deepseek harness 简易的markdown笔记插件
|
|
19
|
+
|
|
20
|
+
- 支持目录树浏览,创建文件夹,创建文件
|
|
21
|
+
- 支持字体样式,大小,背景颜色调整
|
|
22
|
+
- 支持字数显示,自动保存
|
|
23
|
+
- 支持AI对话框功能
|
|
24
|
+
- 重命名,删除,需要在系统资源管理器完成,或提示AI操作
|
|
25
25
|
`;
|
|
26
26
|
}
|
package/lib/story/root.js
CHANGED
|
@@ -12,17 +12,20 @@ function liveSession(agents, sessionId) {
|
|
|
12
12
|
return agents.get(sessionId)?.session;
|
|
13
13
|
}
|
|
14
14
|
/**
|
|
15
|
-
* Decide which directory a call's
|
|
16
|
-
*
|
|
15
|
+
* Decide which directory a call's notes live in.
|
|
16
|
+
*
|
|
17
|
+
* There is nothing to choose between: a live Session *is* the root.
|
|
18
|
+
* @param fallbackRoot - the activation default, for a caller with no Session.
|
|
17
19
|
* @param workspace - the calling Session's workspace, when known.
|
|
18
20
|
* @returns the root and the rule that chose it.
|
|
19
21
|
*/
|
|
20
|
-
export function storyRootFor(
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
22
|
+
export function storyRootFor(fallbackRoot, workspace) {
|
|
23
|
+
// The path is used verbatim: a trailing space is legal input, and trimming the
|
|
24
|
+
// value would resolve a directory the operator did not open. Only the
|
|
25
|
+
// emptiness test looks at the trimmed form.
|
|
26
|
+
if (workspace !== undefined && workspace.trim().length > 0)
|
|
24
27
|
return { dir: workspace, source: 'workspace' };
|
|
25
|
-
return { dir:
|
|
28
|
+
return { dir: fallbackRoot, source: 'fallback' };
|
|
26
29
|
}
|
|
27
30
|
/** Roots kept as live stores; a session beyond this evicts the least recently added. */
|
|
28
31
|
const MAX_CACHED_ROOTS = 8;
|
|
@@ -35,14 +38,14 @@ const MAX_CACHED_ROOTS = 8;
|
|
|
35
38
|
* switched while the plugin is serving it, and the write policy was stamped with
|
|
36
39
|
* the old one — a second store costs a directory listing, a stale mode costs a
|
|
37
40
|
* write that should have been allowed.
|
|
38
|
-
* @param deps - filesystem,
|
|
41
|
+
* @param deps - filesystem, the session-less fallback root, sandbox policy, agent registry, logger.
|
|
39
42
|
* @returns a resolver from an optional Session id to that call's store and root.
|
|
40
43
|
*/
|
|
41
44
|
export function createRootResolver(deps) {
|
|
42
45
|
const cache = new Map();
|
|
43
46
|
return (sessionId) => {
|
|
44
47
|
const session = liveSession(deps.agents, sessionId);
|
|
45
|
-
const root = storyRootFor(deps.
|
|
48
|
+
const root = storyRootFor(deps.fallbackRoot, session?.header.cwd);
|
|
46
49
|
const writePolicy = storyWritePolicy(root.dir, deps.policy, session);
|
|
47
50
|
const key = `${root.dir}\u0000${writePolicy?.mode ?? ''}`;
|
|
48
51
|
let store = cache.get(key);
|
package/lib/story/sandbox.js
CHANGED
|
@@ -1,55 +1,33 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* The plugin's write boundary
|
|
3
|
-
* it.
|
|
2
|
+
* The plugin's write boundary.
|
|
4
3
|
*
|
|
5
|
-
* Every write this plugin makes — from the panel and from
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
4
|
+
* Every write this plugin makes — from the panel, and from the model going
|
|
5
|
+
* through the same store — goes through `ctx.fs` with an explicit policy. That
|
|
6
|
+
* matters because `fs-sandbox` otherwise calls `ctx.sandboxPolicy.resolve()` with
|
|
7
|
+
* **no session**: a call with no policy of its own is judged against the
|
|
8
|
+
* *deployment* fallback, `defaultMode` plus `workspaceRoot` (whose own default is
|
|
9
|
+
* wherever dsh was launched), never against the workspace it is actually writing
|
|
10
|
+
* to.
|
|
12
11
|
*
|
|
13
|
-
* So {@link storyWritePolicy} states the boundary per call: the
|
|
14
|
-
* is
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
* actually stops a path escaping.
|
|
12
|
+
* So {@link storyWritePolicy} states the boundary per call: the resolved root,
|
|
13
|
+
* which is the calling Session's own workspace. Nothing is widened by that — the
|
|
14
|
+
* boundary the plugin stamps is the boundary the Session already has — and
|
|
15
|
+
* `fs-sandbox` still re-canonicalizes the target and enforces containment against
|
|
16
|
+
* it, which is the check that actually stops a path escaping.
|
|
19
17
|
*
|
|
20
|
-
* The **mode** is not invented either: it comes from `resolve({ session })`,
|
|
21
|
-
*
|
|
18
|
+
* The **mode** is not invented either: it comes from `resolve({ session })`, the
|
|
19
|
+
* same call `fs-sandbox` makes, so a Session the operator has switched to
|
|
22
20
|
* `danger-full-access` (or a deployment still under `read-only`) behaves here
|
|
23
21
|
* exactly as it does for the model's own `write`/`edit` and `pwsh`.
|
|
24
22
|
*
|
|
25
|
-
* What is left to report is therefore
|
|
26
|
-
*
|
|
27
|
-
*
|
|
28
|
-
* `rootDir` outside the Session's workspace, where the plugin's own writes
|
|
29
|
-
* succeed but the model's tools and scripts are refused by the very same
|
|
30
|
-
* boundary they are confined to.
|
|
23
|
+
* What is left to report is therefore a single arrangement: a `read-only`
|
|
24
|
+
* deployment, where nothing may write at all — the panel and the model's tools
|
|
25
|
+
* alike, because they are the same boundary.
|
|
31
26
|
*
|
|
32
27
|
* @module notebook/story/sandbox
|
|
33
28
|
*/
|
|
34
29
|
import { homedir } from 'node:os';
|
|
35
|
-
import { join
|
|
36
|
-
/**
|
|
37
|
-
* Whether `child` is `parent` or lives under it.
|
|
38
|
-
*
|
|
39
|
-
* Advisory only — this decides what to *say*, while `fs-sandbox` does the
|
|
40
|
-
* canonical containment check that decides what happens. Windows paths are
|
|
41
|
-
* compared case-insensitively because the filesystem is.
|
|
42
|
-
*/
|
|
43
|
-
function isUnder(child, parent) {
|
|
44
|
-
const outer = resolve(parent);
|
|
45
|
-
const inner = resolve(child);
|
|
46
|
-
const fold = (value) => (process.platform === 'win32' ? value.toLowerCase() : value);
|
|
47
|
-
const outerKey = fold(outer);
|
|
48
|
-
const innerKey = fold(inner);
|
|
49
|
-
if (innerKey === outerKey)
|
|
50
|
-
return true;
|
|
51
|
-
return innerKey.startsWith(outerKey.endsWith(sep) ? outerKey : outerKey + sep);
|
|
52
|
-
}
|
|
30
|
+
import { join } from 'node:path';
|
|
53
31
|
/**
|
|
54
32
|
* The mode one call runs under, resolved exactly as `fs-sandbox` resolves it.
|
|
55
33
|
*
|
|
@@ -65,11 +43,11 @@ export function callMode(policy, session) {
|
|
|
65
43
|
return policy.resolve(session === undefined ? {} : { session }).mode;
|
|
66
44
|
}
|
|
67
45
|
/**
|
|
68
|
-
* The policy to stamp on every write of one
|
|
46
|
+
* The policy to stamp on every write of one root.
|
|
69
47
|
*
|
|
70
|
-
* The mode is the caller's own; the boundary
|
|
71
|
-
*
|
|
72
|
-
* @param rootDir - the resolved absolute
|
|
48
|
+
* The mode is the caller's own; the boundary is the directory the notes actually
|
|
49
|
+
* live in, which is that Session's workspace.
|
|
50
|
+
* @param rootDir - the resolved absolute root for this call.
|
|
73
51
|
* @param policy - the sandbox policy service, when the profile has one.
|
|
74
52
|
* @param session - the calling Session, when the caller named one.
|
|
75
53
|
* @returns the per-call policy, or `undefined` when nothing confines this
|
|
@@ -81,13 +59,12 @@ export function storyWritePolicy(rootDir, policy, session) {
|
|
|
81
59
|
return { mode: callMode(policy, session), workspaceRoot: rootDir };
|
|
82
60
|
}
|
|
83
61
|
/**
|
|
84
|
-
* Decide whether a write to `rootDir` should be expected to work
|
|
85
|
-
* the model's own tools will not be able to follow.
|
|
62
|
+
* Decide whether a write to `rootDir` should be expected to work.
|
|
86
63
|
*
|
|
87
64
|
* Advisory only: the caller reports this and uses it to explain a failure, but
|
|
88
65
|
* still attempts the write, because refusing a write that would have succeeded
|
|
89
66
|
* is worse than a warning that turns out to be unnecessary.
|
|
90
|
-
* @param rootDir - the resolved absolute
|
|
67
|
+
* @param rootDir - the resolved absolute root for this call.
|
|
91
68
|
* @param policy - the sandbox policy service, when this profile has one.
|
|
92
69
|
* @param session - the calling Session, when the caller named one.
|
|
93
70
|
* @returns the assessment shown in the panel and used to explain failures.
|
|
@@ -98,19 +75,6 @@ export function assessWriteAccess(rootDir, policy, session) {
|
|
|
98
75
|
const mode = callMode(policy, session);
|
|
99
76
|
if (mode === 'read-only')
|
|
100
77
|
return { writable: false, mode, workspaceRoot: rootDir, reason: 'read-only' };
|
|
101
|
-
const workspace = session?.header.cwd;
|
|
102
|
-
if (workspace !== undefined && workspace.trim().length > 0 && !isUnder(rootDir, workspace.trim())) {
|
|
103
|
-
// Not a refusal: the plugin stamps its own boundary and writes here happily.
|
|
104
|
-
// It is the model's half that cannot follow, and silence about that is how a
|
|
105
|
-
// half-working setup looks like a working one.
|
|
106
|
-
return {
|
|
107
|
-
writable: true,
|
|
108
|
-
mode,
|
|
109
|
-
workspaceRoot: rootDir,
|
|
110
|
-
reason: 'outside-workspace',
|
|
111
|
-
sessionWorkspace: workspace.trim(),
|
|
112
|
-
};
|
|
113
|
-
}
|
|
114
78
|
return { writable: true, mode, workspaceRoot: rootDir };
|
|
115
79
|
}
|
|
116
80
|
/** The profile patch file a user edits to change the sandbox mode. */
|
|
@@ -123,18 +87,18 @@ export function profilePatchPath() {
|
|
|
123
87
|
/**
|
|
124
88
|
* Explain a read-only deployment and give the exact edit that lifts it.
|
|
125
89
|
*
|
|
126
|
-
* The mode is the only knob that can be turned from here — the
|
|
127
|
-
*
|
|
128
|
-
* not a directory move.
|
|
129
|
-
*
|
|
130
|
-
*
|
|
131
|
-
* replaced wholesale, never deep-merged — an entry that set only `mode` would
|
|
132
|
-
* reset `workspaceRoot` to `process.cwd()`.
|
|
90
|
+
* The mode is the only knob that can be turned from here — the root follows the
|
|
91
|
+
* calling Session, so the remedy for a *read-only* deployment is a mode change,
|
|
92
|
+
* not a directory move. The YAML is spelled out rather than described because
|
|
93
|
+
* `config` in a Loader patch is replaced wholesale, never deep-merged — an entry
|
|
94
|
+
* that set only `mode` would reset `workspaceRoot` to `process.cwd()`.
|
|
133
95
|
* @param rootDir - the root the refused call resolved to.
|
|
134
96
|
* @param access - the assessment for that root.
|
|
135
97
|
* @returns a multi-line message suitable for the panel and for a tool result.
|
|
136
98
|
*/
|
|
137
|
-
function
|
|
99
|
+
export function writeAccessGuidance(rootDir, access) {
|
|
100
|
+
if (access.reason !== 'read-only')
|
|
101
|
+
return null;
|
|
138
102
|
const patch = profilePatchPath();
|
|
139
103
|
return [
|
|
140
104
|
`目录 ${rootDir} 不可写:当前部署的文件策略是 read-only,插件的每一次写入都会被拒绝。`,
|
|
@@ -147,40 +111,3 @@ function readOnlyGuidance(rootDir, access) {
|
|
|
147
111
|
` workspaceRoot: ${access.workspaceRoot ?? rootDir}`,
|
|
148
112
|
].join('\n');
|
|
149
113
|
}
|
|
150
|
-
/**
|
|
151
|
-
* Explain a root that sits outside the open workspace.
|
|
152
|
-
*
|
|
153
|
-
* Nothing here is broken from the plugin's side — which is exactly why it has to
|
|
154
|
-
* be said out loud, in both directions: what still works, and what does not.
|
|
155
|
-
* @param rootDir - the root the call resolved to.
|
|
156
|
-
* @param access - the assessment for that root.
|
|
157
|
-
* @returns a multi-line message suitable for the panel and for a tool result.
|
|
158
|
-
*/
|
|
159
|
-
function outsideWorkspaceGuidance(rootDir, access) {
|
|
160
|
-
const workspace = access.sessionWorkspace ?? '当前工作区';
|
|
161
|
-
return [
|
|
162
|
-
`目录 ${rootDir} 不在当前工作区 ${workspace} 之内(由 profile 里的 rootDir 固定)。`,
|
|
163
|
-
'',
|
|
164
|
-
'面板照常读写:插件给每次写入盖的边界就是这个目录本身。',
|
|
165
|
-
'但模型自己的 write/edit 工具,以及 pwsh / python 脚本,边界是本会话的工作区,',
|
|
166
|
-
'在这里会被沙箱拒绝(读不受影响)。',
|
|
167
|
-
'',
|
|
168
|
-
'两种改法,任选其一:',
|
|
169
|
-
` · 去掉 profile 里的 rootDir,笔记本就落在工作区 ${workspace};`,
|
|
170
|
-
' · 或者把这个目录本身作为工作区打开。',
|
|
171
|
-
].join('\n');
|
|
172
|
-
}
|
|
173
|
-
/**
|
|
174
|
-
* The remedy text for a root that cannot be written, or that only this plugin
|
|
175
|
-
* can write.
|
|
176
|
-
* @param rootDir - the story root the call resolved to.
|
|
177
|
-
* @param access - the assessment for that root.
|
|
178
|
-
* @returns a multi-line message, or `null` when there is nothing to say.
|
|
179
|
-
*/
|
|
180
|
-
export function writeAccessGuidance(rootDir, access) {
|
|
181
|
-
if (access.reason === 'read-only')
|
|
182
|
-
return readOnlyGuidance(rootDir, access);
|
|
183
|
-
if (access.reason === 'outside-workspace')
|
|
184
|
-
return outsideWorkspaceGuidance(rootDir, access);
|
|
185
|
-
return null;
|
|
186
|
-
}
|
package/lib/story/workspace.js
CHANGED
|
@@ -1,19 +1,5 @@
|
|
|
1
1
|
import { ProjectStore } from './project.js';
|
|
2
2
|
import { storyWritePolicy } from './sandbox.js';
|
|
3
|
-
/**
|
|
4
|
-
* The directory the panel's tree shows.
|
|
5
|
-
*
|
|
6
|
-
* A live Session knows it exactly (`header.cwd`, the folder the operator opened).
|
|
7
|
-
* Without one — a scripted caller, or the moment before the page binds — the
|
|
8
|
-
* session-less fallback root *is* the workspace, so it is used as is.
|
|
9
|
-
* @param session - the calling Session, when live.
|
|
10
|
-
* @param fallbackRoot - the store root resolved for this call.
|
|
11
|
-
* @returns an absolute path to the workspace.
|
|
12
|
-
*/
|
|
13
|
-
export function workspaceDirFor(session, fallbackRoot) {
|
|
14
|
-
const cwd = session?.header.cwd;
|
|
15
|
-
return typeof cwd === 'string' && cwd.length > 0 ? cwd : fallbackRoot;
|
|
16
|
-
}
|
|
17
3
|
/** Workspaces kept as live stores; beyond this the oldest is dropped. */
|
|
18
4
|
const MAX_CACHED_WORKSPACES = 8;
|
|
19
5
|
/** One store per (workspace, sandbox mode). */
|
|
@@ -26,7 +12,7 @@ const cache = new Map();
|
|
|
26
12
|
* entry costs one listing.
|
|
27
13
|
* @param deps - filesystem, sandbox policy, logger.
|
|
28
14
|
* @param session - the calling Session, for the write policy.
|
|
29
|
-
* @param dir - the workspace directory.
|
|
15
|
+
* @param dir - the workspace directory, as resolved for this call.
|
|
30
16
|
* @returns a store whose root is `dir` and whose only project is that directory.
|
|
31
17
|
*/
|
|
32
18
|
export function workspaceStore(deps, session, dir) {
|