@zenera/neo 1.1.55 → 1.1.57
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/dist/memory/files.d.ts +37 -3
- package/dist/memory/files.js +26 -14
- package/dist/memory/grep.js +2 -2
- package/dist/memory/index.d.ts +14 -3
- package/dist/memory/index.js +17 -0
- package/dist/memory/render.js +2 -1
- package/dist/memory/tools.js +62 -9
- package/dist/memory/types.d.ts +1 -1
- package/dist/memory/types.js +1 -1
- package/dist/project/config.d.ts +1 -24
- package/dist/project/config.js +18 -5
- package/dist/project/load.d.ts +6 -8
- package/dist/project/load.js +3 -4
- package/dist/runner.d.ts +3 -2
- package/dist/runner.js +1 -1
- package/dist/services.d.ts +8 -6
- package/dist/services.js +7 -6
- package/dist/tools/files.d.ts +28 -37
- package/dist/tools/files.js +106 -84
- package/dist/tools/identity.d.ts +12 -0
- package/dist/tools/identity.js +14 -0
- package/dist/tools/index.d.ts +1 -0
- package/dist/tools/index.js +3 -0
- package/dist/tools/sandbox.d.ts +2 -269
- package/dist/tools/sandbox.js +10 -741
- package/dist/types.d.ts +1 -0
- package/dist/types.js +1 -0
- package/package.json +2 -1
- package/dist/tools/workspace.d.ts +0 -79
- package/dist/tools/workspace.js +0 -1470
package/dist/tools/files.d.ts
CHANGED
|
@@ -1,44 +1,21 @@
|
|
|
1
|
+
import type { FileSource, WorkspaceFiles } from '../memory/files.ts';
|
|
1
2
|
import { type AnyTool } from '../types.ts';
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
* call it by the same name.
|
|
11
|
-
*/
|
|
12
|
-
export interface WorkspaceMount {
|
|
13
|
-
/** absolute host path; must already exist */
|
|
14
|
-
host: string;
|
|
15
|
-
/** absolute path the agent names it by, e.g. `/assets` */
|
|
16
|
-
at: string;
|
|
17
|
-
/** defaults to true — an extra tree is reference material unless said otherwise */
|
|
18
|
-
readOnly?: boolean;
|
|
19
|
-
}
|
|
20
|
-
export interface WorkspaceOptions {
|
|
21
|
-
root: string;
|
|
22
|
-
/** refuse every mutating tool; `zen run --read-only` */
|
|
23
|
-
readOnly?: boolean;
|
|
24
|
-
/**
|
|
25
|
-
* An absolute path the root is *also* known by, because something else has
|
|
26
|
-
* mounted it there — the sandbox bind-mounts the workspace at `/workspace`,
|
|
27
|
-
* so `run_command` returns compiler errors, stack traces and `pwd` output
|
|
28
|
-
* naming files under a prefix the file tools would otherwise refuse. Set
|
|
29
|
-
* it and both spellings reach the same file.
|
|
30
|
-
*/
|
|
31
|
-
mount?: string;
|
|
32
|
-
/** further trees, each under its own name; read-only unless one says otherwise */
|
|
33
|
-
mounts?: readonly WorkspaceMount[];
|
|
34
|
-
}
|
|
35
|
-
export declare class Workspace {
|
|
3
|
+
import { type FilesView, type FsDriver, type Mount } from '@zenera/sandbox';
|
|
4
|
+
/** The trees the file tools reach — `FilesView` from `@zenera/sandbox`, under the name these tools use. */
|
|
5
|
+
export type WorkspaceOptions = FilesView;
|
|
6
|
+
/** A mount as the workspace holds it: resolved, with its permission decided. */
|
|
7
|
+
export type ResolvedMount = Mount & {
|
|
8
|
+
readOnly: boolean;
|
|
9
|
+
};
|
|
10
|
+
export declare class Workspace implements WorkspaceFiles {
|
|
36
11
|
#private;
|
|
37
12
|
readonly root: string;
|
|
38
13
|
readonly readOnly: boolean;
|
|
39
14
|
readonly mount: string | undefined;
|
|
40
15
|
/** the extra trees, resolved — the primary root is not among them */
|
|
41
|
-
readonly mounts: readonly
|
|
16
|
+
readonly mounts: readonly ResolvedMount[];
|
|
17
|
+
/** where the bytes are; every resolved path is in its namespace */
|
|
18
|
+
readonly fs: FsDriver;
|
|
42
19
|
constructor(opts: WorkspaceOptions);
|
|
43
20
|
/**
|
|
44
21
|
* The one gate. Returns an absolute path inside one of the roots, or throws.
|
|
@@ -65,6 +42,22 @@ export declare class Workspace {
|
|
|
65
42
|
show(path: string): string;
|
|
66
43
|
/** Whether a resolved path *is* one of the roots. Nothing may move or delete one. */
|
|
67
44
|
isRoot(path: string): boolean;
|
|
45
|
+
/** Whether resolved `inner` sits strictly below resolved `outer`. */
|
|
46
|
+
isInside(inner: string, outer: string): boolean;
|
|
47
|
+
/**
|
|
48
|
+
* A file the agent named, in the form memory copies from: contained like
|
|
49
|
+
* any read, and read through this workspace's driver, so remembering works
|
|
50
|
+
* the same whether the bytes are on this machine or in a remote sandbox.
|
|
51
|
+
*/
|
|
52
|
+
source(path: string): FileSource;
|
|
53
|
+
/**
|
|
54
|
+
* Writes bytes the agent did not type — a remembered file being copied
|
|
55
|
+
* back — under the same rules as `write_file`: contained, refused in a
|
|
56
|
+
* read-only tree or run, and never over an existing file unless asked.
|
|
57
|
+
*/
|
|
58
|
+
write(path: string, data: Uint8Array, opts?: {
|
|
59
|
+
overwrite?: boolean;
|
|
60
|
+
}): Promise<string>;
|
|
68
61
|
mutable(): void;
|
|
69
62
|
}
|
|
70
63
|
export type FileFormat = 'text' | 'binary' | 'image' | 'audio' | 'video' | 'pdf' | 'archive';
|
|
@@ -76,6 +69,4 @@ export interface FileInfo {
|
|
|
76
69
|
lines?: number;
|
|
77
70
|
}
|
|
78
71
|
export declare function fileTools<TCtx = unknown>(opts: WorkspaceOptions): AnyTool<TCtx>[];
|
|
79
|
-
/** @deprecated The group has been `files` for a while; this is the old spelling. */
|
|
80
|
-
export declare const workspaceTools: typeof fileTools;
|
|
81
72
|
//# sourceMappingURL=files.d.ts.map
|
package/dist/tools/files.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { cp, mkdir, open, readFile, readdir, rename, rm, stat, writeFile } from 'node:fs/promises';
|
|
3
|
-
import { dirname, extname, join, relative, resolve, sep } from 'node:path';
|
|
1
|
+
import { extname } from 'node:path';
|
|
4
2
|
import { tool } from "../types.js";
|
|
3
|
+
import { localFs } from '@zenera/sandbox';
|
|
5
4
|
// ---------------------------------------------------------------------------
|
|
6
5
|
// File tools
|
|
7
6
|
//
|
|
@@ -28,16 +27,14 @@ const SNIFF_BYTES = 8 * 1024;
|
|
|
28
27
|
const MAX_SCAN = 1024 * 1024;
|
|
29
28
|
/** ...and one listing only spends this much on line counting in total. */
|
|
30
29
|
const LIST_SCAN_BUDGET = 8 * 1024 * 1024;
|
|
31
|
-
/** Where a project's `assets/` directory appears in the agent's namespace. */
|
|
32
|
-
export const ASSETS_MOUNT = '/assets';
|
|
33
|
-
/** ...and where its skill catalog does, so a skill's scripts can be run. */
|
|
34
|
-
export const SKILLS_MOUNT = '/skills';
|
|
35
30
|
export class Workspace {
|
|
36
31
|
root;
|
|
37
32
|
readOnly;
|
|
38
33
|
mount;
|
|
39
34
|
/** the extra trees, resolved — the primary root is not among them */
|
|
40
35
|
mounts;
|
|
36
|
+
/** where the bytes are; every resolved path is in its namespace */
|
|
37
|
+
fs;
|
|
41
38
|
/** primary first */
|
|
42
39
|
#roots;
|
|
43
40
|
/** the ones with a name, longest name first */
|
|
@@ -45,9 +42,10 @@ export class Workspace {
|
|
|
45
42
|
/** every root, deepest host path first, for attributing a resolved path to one */
|
|
46
43
|
#deep;
|
|
47
44
|
constructor(opts) {
|
|
45
|
+
this.fs = opts.fs ?? localFs;
|
|
48
46
|
// Resolve the root's own symlinks once, so a workspace that *is* a
|
|
49
47
|
// symlink does not fail every containment check against itself.
|
|
50
|
-
this.root = realpathSync(resolve(opts.root));
|
|
48
|
+
this.root = this.fs.realpathSync(this.fs.path.resolve(opts.root));
|
|
51
49
|
this.readOnly = opts.readOnly ?? false;
|
|
52
50
|
this.mount = mountPoint(opts.mount);
|
|
53
51
|
const roots = [{ path: this.root, at: this.mount, readOnly: this.readOnly }];
|
|
@@ -64,10 +62,10 @@ export class Workspace {
|
|
|
64
62
|
throw new Error(`workspace mounts overlap: ${at} and ${taken.at}`);
|
|
65
63
|
}
|
|
66
64
|
}
|
|
67
|
-
const host = directory(m.host, at);
|
|
65
|
+
const host = directory(this.fs, m.host, at);
|
|
68
66
|
const readOnly = m.readOnly ?? true;
|
|
69
67
|
roots.push({ path: host, at, readOnly });
|
|
70
|
-
mounts.push({ host, at, readOnly });
|
|
68
|
+
mounts.push({ host, at, readOnly, ...(m.source ? { source: m.source } : {}) });
|
|
71
69
|
}
|
|
72
70
|
this.mounts = mounts;
|
|
73
71
|
this.#roots = roots;
|
|
@@ -92,6 +90,7 @@ export class Workspace {
|
|
|
92
90
|
throw new Error('path contains a null byte');
|
|
93
91
|
}
|
|
94
92
|
const { root, rest } = this.#locate(input);
|
|
93
|
+
const { resolve, sep } = this.fs.path;
|
|
95
94
|
const real = this.#realish(resolve(root.path, rest));
|
|
96
95
|
if (real !== root.path && !real.startsWith(root.path + sep)) {
|
|
97
96
|
throw new Error(`outside the workspace: ${input}`);
|
|
@@ -137,15 +136,17 @@ export class Workspace {
|
|
|
137
136
|
}
|
|
138
137
|
/** The root a resolved path belongs to; the primary when nothing else claims it. */
|
|
139
138
|
#owner(path) {
|
|
139
|
+
const { sep } = this.fs.path;
|
|
140
140
|
return (this.#deep.find((r) => path === r.path || path.startsWith(r.path + sep)) ??
|
|
141
141
|
this.#roots[0]);
|
|
142
142
|
}
|
|
143
143
|
#realish(path) {
|
|
144
|
+
const { dirname, join } = this.fs.path;
|
|
144
145
|
const missing = [];
|
|
145
146
|
let at = path;
|
|
146
147
|
for (;;) {
|
|
147
148
|
try {
|
|
148
|
-
return join(realpathSync(at), ...missing.reverse());
|
|
149
|
+
return join(this.fs.realpathSync(at), ...missing.reverse());
|
|
149
150
|
}
|
|
150
151
|
catch (err) {
|
|
151
152
|
if (err.code !== 'ENOENT') {
|
|
@@ -164,6 +165,7 @@ export class Workspace {
|
|
|
164
165
|
}
|
|
165
166
|
/** Where a path sits under its own root, in posix form. `.` is that root. */
|
|
166
167
|
rel(path) {
|
|
168
|
+
const { relative, sep } = this.fs.path;
|
|
167
169
|
return relative(this.#owner(path).path, path).split(sep).join('/') || '.';
|
|
168
170
|
}
|
|
169
171
|
/**
|
|
@@ -178,7 +180,7 @@ export class Workspace {
|
|
|
178
180
|
*/
|
|
179
181
|
show(path) {
|
|
180
182
|
const root = this.#owner(path);
|
|
181
|
-
const rel =
|
|
183
|
+
const rel = this.rel(path);
|
|
182
184
|
if (root.at === undefined) {
|
|
183
185
|
return rel;
|
|
184
186
|
}
|
|
@@ -188,6 +190,42 @@ export class Workspace {
|
|
|
188
190
|
isRoot(path) {
|
|
189
191
|
return this.#roots.some((r) => r.path === path);
|
|
190
192
|
}
|
|
193
|
+
/** Whether resolved `inner` sits strictly below resolved `outer`. */
|
|
194
|
+
isInside(inner, outer) {
|
|
195
|
+
return inner.startsWith(outer + this.fs.path.sep);
|
|
196
|
+
}
|
|
197
|
+
/**
|
|
198
|
+
* A file the agent named, in the form memory copies from: contained like
|
|
199
|
+
* any read, and read through this workspace's driver, so remembering works
|
|
200
|
+
* the same whether the bytes are on this machine or in a remote sandbox.
|
|
201
|
+
*/
|
|
202
|
+
source(path) {
|
|
203
|
+
const at = this.within(path);
|
|
204
|
+
const fs = this.fs;
|
|
205
|
+
return { path: at, stat: () => fs.stat(at), read: () => fs.readFile(at) };
|
|
206
|
+
}
|
|
207
|
+
/**
|
|
208
|
+
* Writes bytes the agent did not type — a remembered file being copied
|
|
209
|
+
* back — under the same rules as `write_file`: contained, refused in a
|
|
210
|
+
* read-only tree or run, and never over an existing file unless asked.
|
|
211
|
+
*/
|
|
212
|
+
async write(path, data, opts = {}) {
|
|
213
|
+
this.mutable();
|
|
214
|
+
const at = this.within(path, { write: true });
|
|
215
|
+
if (this.isRoot(at)) {
|
|
216
|
+
throw new Error('refusing to overwrite the workspace root');
|
|
217
|
+
}
|
|
218
|
+
const there = await this.fs.stat(at).catch(() => undefined);
|
|
219
|
+
if (there?.isDirectory()) {
|
|
220
|
+
throw new Error(`${this.show(at)} is a directory`);
|
|
221
|
+
}
|
|
222
|
+
if (there && !opts.overwrite) {
|
|
223
|
+
throw new Error(`${this.show(at)} already exists — pass overwrite to replace it`);
|
|
224
|
+
}
|
|
225
|
+
await this.fs.mkdirp(this.fs.path.dirname(at));
|
|
226
|
+
await this.fs.writeFile(at, data);
|
|
227
|
+
return this.show(at);
|
|
228
|
+
}
|
|
191
229
|
mutable() {
|
|
192
230
|
if (this.readOnly) {
|
|
193
231
|
throw new Error('the workspace is read-only for this run');
|
|
@@ -206,19 +244,18 @@ function mountPoint(at) {
|
|
|
206
244
|
function under(inner, outer) {
|
|
207
245
|
return inner.startsWith(outer + '/');
|
|
208
246
|
}
|
|
209
|
-
/**
|
|
210
|
-
|
|
211
|
-
|
|
247
|
+
/**
|
|
248
|
+
* The host side of a mount, resolved. It has to exist: a mount of nothing is a
|
|
249
|
+
* typo. That it is a directory is left to the first listing, which says so in
|
|
250
|
+
* the model's words — a driver on another machine could not answer sooner.
|
|
251
|
+
*/
|
|
252
|
+
function directory(fs, host, at) {
|
|
212
253
|
try {
|
|
213
|
-
|
|
254
|
+
return fs.realpathSync(fs.path.resolve(host));
|
|
214
255
|
}
|
|
215
256
|
catch {
|
|
216
257
|
throw new Error(`workspace mount ${at}: no such directory: ${host}`);
|
|
217
258
|
}
|
|
218
|
-
if (!statSync(path).isDirectory()) {
|
|
219
|
-
throw new Error(`workspace mount ${at}: not a directory: ${host}`);
|
|
220
|
-
}
|
|
221
|
-
return path;
|
|
222
259
|
}
|
|
223
260
|
/** `a`, `a and b`, `a, b and c` — these end up in sentences the model reads. */
|
|
224
261
|
function nameList(items) {
|
|
@@ -309,7 +346,7 @@ function lineCount(text) {
|
|
|
309
346
|
* it out from a budget so that one `list_dir` of a build output directory does
|
|
310
347
|
* not read a gigabyte to report line counts nobody asked for.
|
|
311
348
|
*/
|
|
312
|
-
async function inspectFile(at, bytes, scan) {
|
|
349
|
+
async function inspectFile(fs, at, bytes, scan) {
|
|
313
350
|
const known = BY_EXTENSION[extname(at).toLowerCase()];
|
|
314
351
|
if (known) {
|
|
315
352
|
return { format: known };
|
|
@@ -318,37 +355,20 @@ async function inspectFile(at, bytes, scan) {
|
|
|
318
355
|
return { format: 'text', lines: 0 };
|
|
319
356
|
}
|
|
320
357
|
if (scan && bytes <= MAX_SCAN) {
|
|
321
|
-
const buf = await readFile(at);
|
|
358
|
+
const buf = await fs.readFile(at);
|
|
322
359
|
return looksBinary(buf, false)
|
|
323
360
|
? { format: 'binary' }
|
|
324
361
|
: { format: 'text', lines: countLines(buf) };
|
|
325
362
|
}
|
|
326
|
-
const
|
|
327
|
-
|
|
328
|
-
const buf = Buffer.alloc(Math.min(bytes, SNIFF_BYTES));
|
|
329
|
-
const { bytesRead } = await handle.read(buf, 0, buf.length, 0);
|
|
330
|
-
return {
|
|
331
|
-
format: looksBinary(buf.subarray(0, bytesRead), bytesRead < bytes) ? 'binary' : 'text',
|
|
332
|
-
};
|
|
333
|
-
}
|
|
334
|
-
finally {
|
|
335
|
-
await handle.close();
|
|
336
|
-
}
|
|
363
|
+
const buf = await fs.readHead(at, Math.min(bytes, SNIFF_BYTES));
|
|
364
|
+
return { format: looksBinary(buf, buf.length < bytes) ? 'binary' : 'text' };
|
|
337
365
|
}
|
|
338
366
|
/** Reads a file as text, taking only the head of one too large to hold. */
|
|
339
|
-
async function readText(at, bytes) {
|
|
367
|
+
async function readText(fs, at, bytes) {
|
|
340
368
|
if (bytes <= MAX_OPEN) {
|
|
341
|
-
return { body: await readFile(at
|
|
342
|
-
}
|
|
343
|
-
const handle = await open(at, 'r');
|
|
344
|
-
try {
|
|
345
|
-
const buf = Buffer.alloc(MAX_OPEN);
|
|
346
|
-
const { bytesRead } = await handle.read(buf, 0, MAX_OPEN, 0);
|
|
347
|
-
return { body: buf.subarray(0, bytesRead).toString('utf8'), whole: false };
|
|
348
|
-
}
|
|
349
|
-
finally {
|
|
350
|
-
await handle.close();
|
|
369
|
+
return { body: (await fs.readFile(at)).toString('utf8'), whole: true };
|
|
351
370
|
}
|
|
371
|
+
return { body: (await fs.readHead(at, MAX_OPEN)).toString('utf8'), whole: false };
|
|
352
372
|
}
|
|
353
373
|
/** Splits into lines without inventing a trailing empty one. */
|
|
354
374
|
function toLines(body) {
|
|
@@ -764,6 +784,8 @@ function patchLines(lines, chunks, file) {
|
|
|
764
784
|
const GROUP = 'files';
|
|
765
785
|
export function fileTools(opts) {
|
|
766
786
|
const ws = new Workspace(opts);
|
|
787
|
+
const fs = ws.fs;
|
|
788
|
+
const { dirname, join } = fs.path;
|
|
767
789
|
// What the model is told about naming a file. The long form goes in
|
|
768
790
|
// `read_file`, which is the first of these any model reaches for; the short
|
|
769
791
|
// one is repeated on every `path` argument, which is where the value is
|
|
@@ -824,11 +846,11 @@ export function fileTools(opts) {
|
|
|
824
846
|
},
|
|
825
847
|
execute: async ({ path, start_line, end_line }) => {
|
|
826
848
|
const at = ws.within(path);
|
|
827
|
-
const info = await stat(at);
|
|
849
|
+
const info = await fs.stat(at);
|
|
828
850
|
if (info.isDirectory()) {
|
|
829
851
|
return { error: `${ws.show(at)} is a directory`, hint: 'use list_dir' };
|
|
830
852
|
}
|
|
831
|
-
const shape = await inspectFile(at, info.size, false);
|
|
853
|
+
const shape = await inspectFile(fs, at, info.size, false);
|
|
832
854
|
if (shape.format !== 'text') {
|
|
833
855
|
return {
|
|
834
856
|
error: `${ws.show(at)} is not a text file`,
|
|
@@ -836,7 +858,7 @@ export function fileTools(opts) {
|
|
|
836
858
|
bytes: info.size,
|
|
837
859
|
};
|
|
838
860
|
}
|
|
839
|
-
const { body, whole } = await readText(at, info.size);
|
|
861
|
+
const { body, whole } = await readText(fs, at, info.size);
|
|
840
862
|
const all = toLines(body);
|
|
841
863
|
const total = all.length;
|
|
842
864
|
const from = Math.max(1, Math.trunc(start_line ?? 1));
|
|
@@ -897,7 +919,7 @@ export function fileTools(opts) {
|
|
|
897
919
|
return { path: '/', entries: trees };
|
|
898
920
|
}
|
|
899
921
|
const at = root(path);
|
|
900
|
-
const entries = await readdir(at
|
|
922
|
+
const entries = await fs.readdir(at);
|
|
901
923
|
const listed = [];
|
|
902
924
|
let budget = LIST_SCAN_BUDGET;
|
|
903
925
|
for (const entry of entries.slice(0, MAX_ENTRIES)) {
|
|
@@ -916,8 +938,8 @@ export function fileTools(opts) {
|
|
|
916
938
|
// A file can vanish between the readdir and the stat; that is a
|
|
917
939
|
// race, not a failure of the listing.
|
|
918
940
|
try {
|
|
919
|
-
const info = await stat(join(at, entry.name));
|
|
920
|
-
const shape = await inspectFile(join(at, entry.name), info.size, info.size <= budget);
|
|
941
|
+
const info = await fs.stat(join(at, entry.name));
|
|
942
|
+
const shape = await inspectFile(fs, join(at, entry.name), info.size, info.size <= budget);
|
|
921
943
|
budget -= info.size;
|
|
922
944
|
listed.push({ name: entry.name, kind: 'file', bytes: info.size, ...shape });
|
|
923
945
|
}
|
|
@@ -952,8 +974,8 @@ export function fileTools(opts) {
|
|
|
952
974
|
return { error: `content exceeds ${MAX_WRITE} bytes` };
|
|
953
975
|
}
|
|
954
976
|
const at = ws.within(path, { write: true });
|
|
955
|
-
await
|
|
956
|
-
await writeFile(at, content
|
|
977
|
+
await fs.mkdirp(dirname(at));
|
|
978
|
+
await fs.writeFile(at, content);
|
|
957
979
|
return {
|
|
958
980
|
path: ws.show(at),
|
|
959
981
|
bytes: Buffer.byteLength(content),
|
|
@@ -1045,11 +1067,11 @@ export function fileTools(opts) {
|
|
|
1045
1067
|
if (source === target) {
|
|
1046
1068
|
return { error: 'the source and the destination are the same path' };
|
|
1047
1069
|
}
|
|
1048
|
-
if (
|
|
1070
|
+
if (ws.isInside(target, source)) {
|
|
1049
1071
|
return { error: `cannot move ${ws.show(source)} into itself` };
|
|
1050
1072
|
}
|
|
1051
|
-
await stat(source); // ENOENT here is the honest error
|
|
1052
|
-
if (await statOrNull(target)) {
|
|
1073
|
+
await fs.stat(source); // ENOENT here is the honest error
|
|
1074
|
+
if (await statOrNull(fs, target)) {
|
|
1053
1075
|
if (!overwrite) {
|
|
1054
1076
|
return {
|
|
1055
1077
|
error: `${ws.show(target)} already exists`,
|
|
@@ -1057,10 +1079,10 @@ export function fileTools(opts) {
|
|
|
1057
1079
|
};
|
|
1058
1080
|
}
|
|
1059
1081
|
// rename() will not replace a non-empty directory on its own.
|
|
1060
|
-
await rm(target, { recursive: true, force: true });
|
|
1082
|
+
await fs.rm(target, { recursive: true, force: true });
|
|
1061
1083
|
}
|
|
1062
|
-
await
|
|
1063
|
-
await rename(source, target);
|
|
1084
|
+
await fs.mkdirp(dirname(target));
|
|
1085
|
+
await fs.rename(source, target);
|
|
1064
1086
|
return { from: ws.show(source), to: ws.show(target), moved: true };
|
|
1065
1087
|
},
|
|
1066
1088
|
});
|
|
@@ -1105,7 +1127,7 @@ export function fileTools(opts) {
|
|
|
1105
1127
|
if (source === target) {
|
|
1106
1128
|
return { error: 'the source and the destination are the same path' };
|
|
1107
1129
|
}
|
|
1108
|
-
const info = await stat(source); // ENOENT here is the honest error
|
|
1130
|
+
const info = await fs.stat(source); // ENOENT here is the honest error
|
|
1109
1131
|
if (info.isDirectory()) {
|
|
1110
1132
|
if (!recursive) {
|
|
1111
1133
|
return {
|
|
@@ -1113,11 +1135,11 @@ export function fileTools(opts) {
|
|
|
1113
1135
|
hint: 'pass recursive to copy it and everything in it',
|
|
1114
1136
|
};
|
|
1115
1137
|
}
|
|
1116
|
-
if (
|
|
1138
|
+
if (ws.isInside(target, source)) {
|
|
1117
1139
|
return { error: `cannot copy ${ws.show(source)} into itself` };
|
|
1118
1140
|
}
|
|
1119
1141
|
}
|
|
1120
|
-
if (await statOrNull(target)) {
|
|
1142
|
+
if (await statOrNull(fs, target)) {
|
|
1121
1143
|
if (!overwrite) {
|
|
1122
1144
|
return {
|
|
1123
1145
|
error: `${ws.show(target)} already exists`,
|
|
@@ -1125,10 +1147,10 @@ export function fileTools(opts) {
|
|
|
1125
1147
|
};
|
|
1126
1148
|
}
|
|
1127
1149
|
// cp() will not replace a directory with a file, or the reverse.
|
|
1128
|
-
await rm(target, { recursive: true, force: true });
|
|
1150
|
+
await fs.rm(target, { recursive: true, force: true });
|
|
1129
1151
|
}
|
|
1130
|
-
await
|
|
1131
|
-
await
|
|
1152
|
+
await fs.mkdirp(dirname(target));
|
|
1153
|
+
await fs.copy(source, target, { recursive: recursive === true });
|
|
1132
1154
|
const out = { from: ws.show(source), to: ws.show(target), copied: true };
|
|
1133
1155
|
return info.isDirectory() ? { ...out, directory: true } : { ...out, bytes: info.size };
|
|
1134
1156
|
},
|
|
@@ -1156,7 +1178,7 @@ export function fileTools(opts) {
|
|
|
1156
1178
|
if (ws.isRoot(at)) {
|
|
1157
1179
|
return { error: 'refusing to delete the workspace root' };
|
|
1158
1180
|
}
|
|
1159
|
-
const info = await stat(at);
|
|
1181
|
+
const info = await fs.stat(at);
|
|
1160
1182
|
if (info.isDirectory()) {
|
|
1161
1183
|
if (!recursive) {
|
|
1162
1184
|
return {
|
|
@@ -1164,10 +1186,10 @@ export function fileTools(opts) {
|
|
|
1164
1186
|
hint: 'pass recursive to delete it and everything in it',
|
|
1165
1187
|
};
|
|
1166
1188
|
}
|
|
1167
|
-
await rm(at, { recursive: true });
|
|
1189
|
+
await fs.rm(at, { recursive: true });
|
|
1168
1190
|
return { path: ws.show(at), deleted: true, directory: true };
|
|
1169
1191
|
}
|
|
1170
|
-
await rm(at);
|
|
1192
|
+
await fs.rm(at);
|
|
1171
1193
|
return { path: ws.show(at), deleted: true };
|
|
1172
1194
|
},
|
|
1173
1195
|
});
|
|
@@ -1234,8 +1256,6 @@ export function fileTools(opts) {
|
|
|
1234
1256
|
deleteFile,
|
|
1235
1257
|
]);
|
|
1236
1258
|
}
|
|
1237
|
-
/** @deprecated The group has been `files` for a while; this is the old spelling. */
|
|
1238
|
-
export const workspaceTools = fileTools;
|
|
1239
1259
|
/** What each errno means, said the way the rest of these tools say things. */
|
|
1240
1260
|
const ERRNO = {
|
|
1241
1261
|
ENOENT: 'does not exist',
|
|
@@ -1275,6 +1295,8 @@ function named(ws, tools) {
|
|
|
1275
1295
|
}));
|
|
1276
1296
|
}
|
|
1277
1297
|
async function runPatch(ws, patch) {
|
|
1298
|
+
const fs = ws.fs;
|
|
1299
|
+
const { dirname } = fs.path;
|
|
1278
1300
|
const ops = parsePatch(patch);
|
|
1279
1301
|
const steps = [];
|
|
1280
1302
|
const touched = [];
|
|
@@ -1301,7 +1323,7 @@ async function runPatch(ws, patch) {
|
|
|
1301
1323
|
}
|
|
1302
1324
|
seen.add(at);
|
|
1303
1325
|
if (op.kind === 'delete') {
|
|
1304
|
-
const info = await statOrNull(at);
|
|
1326
|
+
const info = await statOrNull(fs, at);
|
|
1305
1327
|
if (!info) {
|
|
1306
1328
|
throw new PatchError(`${ws.show(at)} does not exist`);
|
|
1307
1329
|
}
|
|
@@ -1314,7 +1336,7 @@ async function runPatch(ws, patch) {
|
|
|
1314
1336
|
continue;
|
|
1315
1337
|
}
|
|
1316
1338
|
if (op.kind === 'add') {
|
|
1317
|
-
if (await statOrNull(at)) {
|
|
1339
|
+
if (await statOrNull(fs, at)) {
|
|
1318
1340
|
throw new PatchError(`${ws.show(at)} already exists — update it instead of adding it`);
|
|
1319
1341
|
}
|
|
1320
1342
|
const content = op.lines.length > 0 ? op.lines.join('\n') + '\n' : '';
|
|
@@ -1330,7 +1352,7 @@ async function runPatch(ws, patch) {
|
|
|
1330
1352
|
});
|
|
1331
1353
|
continue;
|
|
1332
1354
|
}
|
|
1333
|
-
const info = await statOrNull(at);
|
|
1355
|
+
const info = await statOrNull(fs, at);
|
|
1334
1356
|
if (!info) {
|
|
1335
1357
|
throw new PatchError(`${ws.show(at)} does not exist`);
|
|
1336
1358
|
}
|
|
@@ -1340,11 +1362,11 @@ async function runPatch(ws, patch) {
|
|
|
1340
1362
|
if (info.size > MAX_WRITE) {
|
|
1341
1363
|
throw new PatchError(`${ws.show(at)} is too large to patch`);
|
|
1342
1364
|
}
|
|
1343
|
-
const shape = await inspectFile(at, info.size, false);
|
|
1365
|
+
const shape = await inspectFile(fs, at, info.size, false);
|
|
1344
1366
|
if (shape.format !== 'text') {
|
|
1345
1367
|
throw new PatchError(`${ws.show(at)} is a ${shape.format} file, not text`);
|
|
1346
1368
|
}
|
|
1347
|
-
const body = await readFile(at
|
|
1369
|
+
const body = (await fs.readFile(at)).toString('utf8');
|
|
1348
1370
|
// Matched against LF-only lines. A patch never carries a CR, so a CRLF
|
|
1349
1371
|
// file would miss exactly, match on the trailing-whitespace pass, and be
|
|
1350
1372
|
// spliced with bare LF — mixed endings behind a successful apply.
|
|
@@ -1377,7 +1399,7 @@ async function runPatch(ws, patch) {
|
|
|
1377
1399
|
if (targets.has(to) || (seen.has(to) && !removed.has(to))) {
|
|
1378
1400
|
throw new PatchError(`${ws.show(to)} is already written by another part of this patch`);
|
|
1379
1401
|
}
|
|
1380
|
-
if (!removed.has(to) && (await statOrNull(to))) {
|
|
1402
|
+
if (!removed.has(to) && (await statOrNull(fs, to))) {
|
|
1381
1403
|
throw new PatchError(`${ws.show(to)} already exists`);
|
|
1382
1404
|
}
|
|
1383
1405
|
targets.add(to);
|
|
@@ -1391,15 +1413,15 @@ async function runPatch(ws, patch) {
|
|
|
1391
1413
|
for (const step of steps) {
|
|
1392
1414
|
switch (step.kind) {
|
|
1393
1415
|
case 'write':
|
|
1394
|
-
await
|
|
1395
|
-
await writeFile(step.at, step.content
|
|
1416
|
+
await fs.mkdirp(dirname(step.at));
|
|
1417
|
+
await fs.writeFile(step.at, step.content);
|
|
1396
1418
|
break;
|
|
1397
1419
|
case 'move':
|
|
1398
|
-
await
|
|
1399
|
-
await rename(step.from, step.to);
|
|
1420
|
+
await fs.mkdirp(dirname(step.to));
|
|
1421
|
+
await fs.rename(step.from, step.to);
|
|
1400
1422
|
break;
|
|
1401
1423
|
case 'delete':
|
|
1402
|
-
await rm(step.at);
|
|
1424
|
+
await fs.rm(step.at);
|
|
1403
1425
|
break;
|
|
1404
1426
|
}
|
|
1405
1427
|
}
|
|
@@ -1415,9 +1437,9 @@ async function runPatch(ws, patch) {
|
|
|
1415
1437
|
ignoredHeadings: ignored > 0 ? ignored : undefined,
|
|
1416
1438
|
};
|
|
1417
1439
|
}
|
|
1418
|
-
async function statOrNull(at) {
|
|
1440
|
+
async function statOrNull(fs, at) {
|
|
1419
1441
|
try {
|
|
1420
|
-
return await stat(at);
|
|
1442
|
+
return await fs.stat(at);
|
|
1421
1443
|
}
|
|
1422
1444
|
catch {
|
|
1423
1445
|
return null;
|
|
@@ -1443,7 +1465,7 @@ const SKIP = new Set(['.git', 'node_modules', '.data', 'dist', '.venv', '__pycac
|
|
|
1443
1465
|
async function walk(dir, ws, visit) {
|
|
1444
1466
|
let entries;
|
|
1445
1467
|
try {
|
|
1446
|
-
entries = await readdir(dir
|
|
1468
|
+
entries = await ws.fs.readdir(dir);
|
|
1447
1469
|
}
|
|
1448
1470
|
catch {
|
|
1449
1471
|
return true;
|
|
@@ -1452,7 +1474,7 @@ async function walk(dir, ws, visit) {
|
|
|
1452
1474
|
if (SKIP.has(entry.name)) {
|
|
1453
1475
|
continue;
|
|
1454
1476
|
}
|
|
1455
|
-
const child = join(dir, entry.name);
|
|
1477
|
+
const child = ws.fs.path.join(dir, entry.name);
|
|
1456
1478
|
// Symlinks are not followed: a link out of the workspace would
|
|
1457
1479
|
// otherwise let a listing report paths the tools cannot open anyway.
|
|
1458
1480
|
if (entry.isSymbolicLink()) {
|
package/dist/tools/identity.d.ts
CHANGED
|
@@ -28,6 +28,18 @@ export declare const IDENTITY_ENV: {
|
|
|
28
28
|
/** Every name under it is the host's to set; a project may not. */
|
|
29
29
|
export declare const IDENTITY_ENV_PREFIX = "ZEN_USER_";
|
|
30
30
|
export declare function isIdentityEnv(name: string): boolean;
|
|
31
|
+
/**
|
|
32
|
+
* Where a sandbox service is and the credential that reaches it — read by the
|
|
33
|
+
* plugin that provides the sandbox, from the machine's environment only.
|
|
34
|
+
*/
|
|
35
|
+
export declare const SANDBOX_SERVICE_ENV_PREFIX = "ZEN_SANDBOX_";
|
|
36
|
+
/**
|
|
37
|
+
* Variables that are the host's alone: who a run acts for, which plugins load
|
|
38
|
+
* into the engine, and which sandbox service it uses. A project that could set
|
|
39
|
+
* one in a committed file — or forward one into the sandbox it runs code in —
|
|
40
|
+
* could be anyone, run anything, or send its users' work anywhere.
|
|
41
|
+
*/
|
|
42
|
+
export declare function isHostOnlyEnv(name: string): boolean;
|
|
31
43
|
/** The identity as the variables a command sees; nothing at all when there is none. */
|
|
32
44
|
export declare function identityEnv(identity: RunIdentity | undefined): Record<string, string>;
|
|
33
45
|
/**
|
package/dist/tools/identity.js
CHANGED
|
@@ -27,6 +27,20 @@ export const IDENTITY_ENV_PREFIX = 'ZEN_USER_';
|
|
|
27
27
|
export function isIdentityEnv(name) {
|
|
28
28
|
return name.startsWith(IDENTITY_ENV_PREFIX);
|
|
29
29
|
}
|
|
30
|
+
/**
|
|
31
|
+
* Where a sandbox service is and the credential that reaches it — read by the
|
|
32
|
+
* plugin that provides the sandbox, from the machine's environment only.
|
|
33
|
+
*/
|
|
34
|
+
export const SANDBOX_SERVICE_ENV_PREFIX = 'ZEN_SANDBOX_';
|
|
35
|
+
/**
|
|
36
|
+
* Variables that are the host's alone: who a run acts for, which plugins load
|
|
37
|
+
* into the engine, and which sandbox service it uses. A project that could set
|
|
38
|
+
* one in a committed file — or forward one into the sandbox it runs code in —
|
|
39
|
+
* could be anyone, run anything, or send its users' work anywhere.
|
|
40
|
+
*/
|
|
41
|
+
export function isHostOnlyEnv(name) {
|
|
42
|
+
return (isIdentityEnv(name) || name === 'ZEN_PLUGINS' || name.startsWith(SANDBOX_SERVICE_ENV_PREFIX));
|
|
43
|
+
}
|
|
30
44
|
/** The identity as the variables a command sees; nothing at all when there is none. */
|
|
31
45
|
export function identityEnv(identity) {
|
|
32
46
|
if (!identity) {
|
package/dist/tools/index.d.ts
CHANGED
package/dist/tools/index.js
CHANGED
|
@@ -4,4 +4,7 @@ export * from "./exa.js";
|
|
|
4
4
|
export * from "./files.js";
|
|
5
5
|
export * from "./identity.js";
|
|
6
6
|
export * from "./sandbox.js";
|
|
7
|
+
// The sandbox itself lives in its own package; re-exported so existing
|
|
8
|
+
// `@zenera/neo` imports keep working.
|
|
9
|
+
export * from '@zenera/sandbox';
|
|
7
10
|
//# sourceMappingURL=index.js.map
|