@celljs/ta-exec 0.0.0-stage → 4.5.1
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 +16 -2
- package/esm/node/exec-protocol.d.ts +61 -0
- package/esm/node/exec-protocol.js +2 -0
- package/esm/node/exec-protocol.js.map +1 -0
- package/esm/node/index.d.ts +2 -0
- package/esm/node/index.js +3 -0
- package/esm/node/index.js.map +1 -0
- package/esm/node/local-exec-environment.d.ts +20 -0
- package/esm/node/local-exec-environment.js +108 -0
- package/esm/node/local-exec-environment.js.map +1 -0
- package/esm/package.json +3 -0
- package/lib/node/exec-protocol.d.ts +62 -0
- package/lib/node/exec-protocol.d.ts.map +1 -0
- package/lib/node/exec-protocol.js +5 -0
- package/lib/node/exec-protocol.js.map +1 -0
- package/lib/node/index.d.ts +3 -0
- package/lib/node/index.d.ts.map +1 -0
- package/lib/node/index.js +19 -0
- package/lib/node/index.js.map +1 -0
- package/lib/node/local-exec-environment.d.ts +21 -0
- package/lib/node/local-exec-environment.d.ts.map +1 -0
- package/lib/node/local-exec-environment.js +112 -0
- package/lib/node/local-exec-environment.js.map +1 -0
- package/lib/node/local-exec-environment.spec.d.ts +2 -0
- package/lib/node/local-exec-environment.spec.d.ts.map +1 -0
- package/lib/node/local-exec-environment.spec.js +55 -0
- package/lib/node/local-exec-environment.spec.js.map +1 -0
- package/package.json +52 -4
- package/src/node/exec-protocol.ts +67 -0
- package/src/node/index.ts +2 -0
- package/src/node/local-exec-environment.spec.ts +58 -0
- package/src/node/local-exec-environment.ts +111 -0
package/README.md
CHANGED
|
@@ -1,3 +1,17 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Cell - ta Exec
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Agent 执行命令、读写文件的地方,作用相当于 Codex 的 exec-server:Agent 循环留在宿主,进程与文件操作交给执行环境。不依赖 pi。
|
|
4
|
+
|
|
5
|
+
```ts
|
|
6
|
+
interface ExecEnvironment {
|
|
7
|
+
readonly cwd: string; // 执行环境里的路径
|
|
8
|
+
readonly local: boolean; // 是否就是本机
|
|
9
|
+
exec(command, options?): Promise<ExecResult>; // 一次性执行,流式输出,超时/中止时杀掉整个进程组
|
|
10
|
+
spawn(command, args, options?): ExecProcess; // 长驻进程,stdin/stdout 通信(MCP、LSP)
|
|
11
|
+
readFile / writeFile / stat / readdir / mkdir / remove
|
|
12
|
+
}
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
路径一律是执行环境里的路径:不要交给 `node:fs`,交给模型的也只能是这些路径。
|
|
16
|
+
|
|
17
|
+
目前只有 `LocalExecEnvironment`(本机)。远程实现绑定为 `ExecEnvironmentFactory`(`rebind: true`)即可,工具层不用改。
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
export declare const ExecEnvironmentFactory: unique symbol;
|
|
2
|
+
export interface ExecOptions {
|
|
3
|
+
/** Default: the `cwd` of the environment. */
|
|
4
|
+
cwd?: string;
|
|
5
|
+
env?: NodeJS.ProcessEnv;
|
|
6
|
+
/** Milliseconds; the process is killed afterwards. */
|
|
7
|
+
timeout?: number;
|
|
8
|
+
signal?: AbortSignal;
|
|
9
|
+
onData?: (data: Uint8Array) => void;
|
|
10
|
+
}
|
|
11
|
+
export interface ExecResult {
|
|
12
|
+
/** null when the process was killed. */
|
|
13
|
+
exitCode: number | null;
|
|
14
|
+
}
|
|
15
|
+
export interface SpawnOptions {
|
|
16
|
+
cwd?: string;
|
|
17
|
+
env?: NodeJS.ProcessEnv;
|
|
18
|
+
}
|
|
19
|
+
/** A running program, for long-lived ones that talk over stdin and stdout (MCP servers, language servers). */
|
|
20
|
+
export interface ExecProcess {
|
|
21
|
+
write(data: Uint8Array | string): void;
|
|
22
|
+
onStdout(listener: (data: Uint8Array) => void): void;
|
|
23
|
+
onStderr(listener: (data: Uint8Array) => void): void;
|
|
24
|
+
/** Also called when the program could not start, with a null code. */
|
|
25
|
+
onExit(listener: (exitCode: number | null) => void): void;
|
|
26
|
+
kill(): void;
|
|
27
|
+
}
|
|
28
|
+
export interface FileStat {
|
|
29
|
+
isDirectory: boolean;
|
|
30
|
+
size: number;
|
|
31
|
+
mtimeMs: number;
|
|
32
|
+
}
|
|
33
|
+
export interface DirEntry {
|
|
34
|
+
name: string;
|
|
35
|
+
isDirectory: boolean;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Where an agent runs commands and touches files. Paths are paths inside this environment, which may
|
|
39
|
+
* be another machine: never hand one to `node:fs`, and hand the model only these.
|
|
40
|
+
*/
|
|
41
|
+
export interface ExecEnvironment {
|
|
42
|
+
/** Working directory, inside the environment. */
|
|
43
|
+
readonly cwd: string;
|
|
44
|
+
/** This machine: pi's own implementations of its tools do the same work, so they stay in use. */
|
|
45
|
+
readonly local: boolean;
|
|
46
|
+
exec(command: string, options?: ExecOptions): Promise<ExecResult>;
|
|
47
|
+
spawn(command: string, args: string[], options?: SpawnOptions): ExecProcess;
|
|
48
|
+
readFile(path: string): Promise<Uint8Array>;
|
|
49
|
+
writeFile(path: string, data: Uint8Array | string): Promise<void>;
|
|
50
|
+
/** undefined when nothing is there. */
|
|
51
|
+
stat(path: string): Promise<FileStat | undefined>;
|
|
52
|
+
readdir(path: string): Promise<DirEntry[]>;
|
|
53
|
+
mkdir(path: string): Promise<void>;
|
|
54
|
+
remove(path: string, options?: {
|
|
55
|
+
recursive?: boolean;
|
|
56
|
+
}): Promise<void>;
|
|
57
|
+
}
|
|
58
|
+
/** Bind another implementation with `rebind: true` to run sessions elsewhere. */
|
|
59
|
+
export interface ExecEnvironmentFactory {
|
|
60
|
+
create(cwd: string): ExecEnvironment;
|
|
61
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"exec-protocol.js","sourceRoot":"","sources":["../../src/node/exec-protocol.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,sBAAsB,GAAG,MAAM,CAAC,wBAAwB,CAAC,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/node/index.ts"],"names":[],"mappings":"AAAA,cAAc,iBAAiB,CAAC;AAChC,cAAc,0BAA0B,CAAC"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { type ChildProcess } from 'node:child_process';
|
|
2
|
+
import type { DirEntry, ExecEnvironment, ExecOptions, ExecProcess, ExecResult, FileStat, SpawnOptions } from './exec-protocol.js';
|
|
3
|
+
/** This machine. */
|
|
4
|
+
export declare class LocalExecEnvironment implements ExecEnvironment {
|
|
5
|
+
readonly cwd: string;
|
|
6
|
+
readonly local: boolean;
|
|
7
|
+
constructor(cwd: string);
|
|
8
|
+
exec(command: string, options?: ExecOptions): Promise<ExecResult>;
|
|
9
|
+
spawn(command: string, args: string[], options?: SpawnOptions): ExecProcess;
|
|
10
|
+
readFile(path: string): Promise<Uint8Array>;
|
|
11
|
+
writeFile(path: string, data: Uint8Array | string): Promise<void>;
|
|
12
|
+
stat(path: string): Promise<FileStat | undefined>;
|
|
13
|
+
readdir(path: string): Promise<DirEntry[]>;
|
|
14
|
+
mkdir(path: string): Promise<void>;
|
|
15
|
+
remove(path: string, options?: {
|
|
16
|
+
recursive?: boolean;
|
|
17
|
+
}): Promise<void>;
|
|
18
|
+
protected resolve(path: string): string;
|
|
19
|
+
protected killGroup(child: ChildProcess): void;
|
|
20
|
+
}
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import { spawn } from 'node:child_process';
|
|
2
|
+
import { mkdir, readdir, readFile, rm, stat, writeFile } from 'node:fs/promises';
|
|
3
|
+
import { resolve } from 'node:path';
|
|
4
|
+
/** This machine. */
|
|
5
|
+
export class LocalExecEnvironment {
|
|
6
|
+
constructor(cwd) {
|
|
7
|
+
this.cwd = cwd;
|
|
8
|
+
this.local = true;
|
|
9
|
+
}
|
|
10
|
+
exec(command, options = {}) {
|
|
11
|
+
var _a, _b;
|
|
12
|
+
const [shell, ...args] = process.platform === 'win32' ? ['cmd.exe', '/d', '/s', '/c'] : ['/bin/sh', '-c'];
|
|
13
|
+
const child = spawn(shell, [...args, command], {
|
|
14
|
+
cwd: resolve(this.cwd, (_a = options.cwd) !== null && _a !== void 0 ? _a : '.'),
|
|
15
|
+
env: (_b = options.env) !== null && _b !== void 0 ? _b : process.env,
|
|
16
|
+
stdio: ['ignore', 'pipe', 'pipe'],
|
|
17
|
+
// Its own process group: killing the shell must also end what the command started.
|
|
18
|
+
detached: process.platform !== 'win32',
|
|
19
|
+
});
|
|
20
|
+
return new Promise(done => {
|
|
21
|
+
var _a, _b, _c, _d;
|
|
22
|
+
const kill = () => this.killGroup(child);
|
|
23
|
+
const timer = options.timeout ? setTimeout(kill, options.timeout) : undefined;
|
|
24
|
+
(_a = options.signal) === null || _a === void 0 ? void 0 : _a.addEventListener('abort', kill, { once: true });
|
|
25
|
+
if ((_b = options.signal) === null || _b === void 0 ? void 0 : _b.aborted) {
|
|
26
|
+
kill();
|
|
27
|
+
}
|
|
28
|
+
(_c = child.stdout) === null || _c === void 0 ? void 0 : _c.on('data', data => { var _a; return (_a = options.onData) === null || _a === void 0 ? void 0 : _a.call(options, data); });
|
|
29
|
+
(_d = child.stderr) === null || _d === void 0 ? void 0 : _d.on('data', data => { var _a; return (_a = options.onData) === null || _a === void 0 ? void 0 : _a.call(options, data); });
|
|
30
|
+
const finish = (exitCode) => {
|
|
31
|
+
var _a;
|
|
32
|
+
clearTimeout(timer);
|
|
33
|
+
(_a = options.signal) === null || _a === void 0 ? void 0 : _a.removeEventListener('abort', kill);
|
|
34
|
+
done({ exitCode });
|
|
35
|
+
};
|
|
36
|
+
child.once('error', () => finish(null));
|
|
37
|
+
child.once('close', finish);
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
spawn(command, args, options = {}) {
|
|
41
|
+
var _a, _b;
|
|
42
|
+
const child = spawn(command, args, {
|
|
43
|
+
cwd: resolve(this.cwd, (_a = options.cwd) !== null && _a !== void 0 ? _a : '.'),
|
|
44
|
+
env: (_b = options.env) !== null && _b !== void 0 ? _b : process.env,
|
|
45
|
+
stdio: ['pipe', 'pipe', 'pipe'],
|
|
46
|
+
});
|
|
47
|
+
// Writes after the program ended surface as EPIPE here; onExit tells the owner.
|
|
48
|
+
child.stdin.on('error', () => undefined);
|
|
49
|
+
return {
|
|
50
|
+
write: data => child.stdin.write(data),
|
|
51
|
+
onStdout: listener => child.stdout.on('data', listener),
|
|
52
|
+
onStderr: listener => child.stderr.on('data', listener),
|
|
53
|
+
onExit: listener => {
|
|
54
|
+
child.once('error', () => listener(null));
|
|
55
|
+
child.once('close', listener);
|
|
56
|
+
},
|
|
57
|
+
kill: () => child.kill(),
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
readFile(path) {
|
|
61
|
+
return readFile(this.resolve(path));
|
|
62
|
+
}
|
|
63
|
+
writeFile(path, data) {
|
|
64
|
+
return writeFile(this.resolve(path), data);
|
|
65
|
+
}
|
|
66
|
+
async stat(path) {
|
|
67
|
+
try {
|
|
68
|
+
const info = await stat(this.resolve(path));
|
|
69
|
+
return { isDirectory: info.isDirectory(), size: info.size, mtimeMs: info.mtimeMs };
|
|
70
|
+
}
|
|
71
|
+
catch (error) {
|
|
72
|
+
if (error.code === 'ENOENT') {
|
|
73
|
+
return undefined;
|
|
74
|
+
}
|
|
75
|
+
throw error;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
async readdir(path) {
|
|
79
|
+
const entries = await readdir(this.resolve(path), { withFileTypes: true });
|
|
80
|
+
return entries.map(entry => ({ name: entry.name, isDirectory: entry.isDirectory() }));
|
|
81
|
+
}
|
|
82
|
+
async mkdir(path) {
|
|
83
|
+
await mkdir(this.resolve(path), { recursive: true });
|
|
84
|
+
}
|
|
85
|
+
remove(path, options = {}) {
|
|
86
|
+
return rm(this.resolve(path), { recursive: options.recursive, force: true });
|
|
87
|
+
}
|
|
88
|
+
resolve(path) {
|
|
89
|
+
return resolve(this.cwd, path);
|
|
90
|
+
}
|
|
91
|
+
killGroup(child) {
|
|
92
|
+
if (child.pid === undefined) {
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
try {
|
|
96
|
+
if (process.platform === 'win32') {
|
|
97
|
+
child.kill();
|
|
98
|
+
}
|
|
99
|
+
else {
|
|
100
|
+
process.kill(-child.pid, 'SIGKILL');
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
catch (_a) {
|
|
104
|
+
// It already ended.
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
//# sourceMappingURL=local-exec-environment.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"local-exec-environment.js","sourceRoot":"","sources":["../../src/node/local-exec-environment.ts"],"names":[],"mappings":"AAAA,OAAO,EAAqB,KAAK,EAAE,MAAM,oBAAoB,CAAC;AAC9D,OAAO,EAAE,KAAK,EAAE,OAAO,EAAE,QAAQ,EAAE,EAAE,EAAE,IAAI,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AACjF,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAGpC,oBAAoB;AACpB,MAAM,OAAO,oBAAoB;IAG7B,YAAqB,GAAW;QAAX,QAAG,GAAH,GAAG,CAAQ;QAFvB,UAAK,GAAY,IAAI,CAAC;IAEI,CAAC;IAEpC,IAAI,CAAC,OAAe,EAAE,OAAO,GAAgB,EAAE;;QAC3C,MAAM,CAAC,KAAK,EAAE,GAAG,IAAI,CAAC,GAAG,OAAO,CAAC,QAAQ,KAAK,OAAO,CAAC,CAAC,CAAC,CAAC,SAAS,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,SAAS,EAAE,IAAI,CAAC,CAAC;QAC1G,MAAM,KAAK,GAAG,KAAK,CAAC,KAAK,EAAE,CAAC,GAAG,IAAI,EAAE,OAAO,CAAC,EAAE;YAC3C,GAAG,EAAE,OAAO,CAAC,IAAI,CAAC,GAAG,QAAE,OA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E,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC,CAAC;IACzD,CAAC;IAED,MAAM,CAAC,IAAY,EAAE,OAAO,GAA4B,EAAE;QACtD,OAAO,EAAE,CAAC,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE,EAAE,SAAS,EAAE,OAAO,CAAC,SAAS,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC,CAAC;IACjF,CAAC;IAES,OAAO,CAAC,IAAY;QAC1B,OAAO,OAAO,CAAC,IAAI,CAAC,GAAG,EAAE,IAAI,CAAC,CAAC;IACnC,CAAC;IAES,SAAS,CAAC,KAAmB;QACnC,IAAI,KAAK,CAAC,GAAG,KAAK,SAAS,EAAE,CAAC;YAC1B,OAAO;QACX,CAAC;QACD,IAAI,CAAC;YACD,IAAI,OAAO,CAAC,QAAQ,KAAK,OAAO,EAAE,CAAC;gBAC/B,KAAK,CAAC,IAAI,EAAE,CAAC;YACjB,CAAC;iBAAM,CAAC;gBACJ,OAAO,CAAC,IAAI,CAAC,CAAC,KAAK,CAAC,GAAG,EAAE,SAAS,CAAC,CAAC;YACxC,CAAC;QACL,CAAC;mBAAO,CAAC;YACL,oBAAoB;QACxB,CAAC;IACL,CAAC;CACJ"}
|
package/esm/package.json
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
export declare const ExecEnvironmentFactory: unique symbol;
|
|
2
|
+
export interface ExecOptions {
|
|
3
|
+
/** Default: the `cwd` of the environment. */
|
|
4
|
+
cwd?: string;
|
|
5
|
+
env?: NodeJS.ProcessEnv;
|
|
6
|
+
/** Milliseconds; the process is killed afterwards. */
|
|
7
|
+
timeout?: number;
|
|
8
|
+
signal?: AbortSignal;
|
|
9
|
+
onData?: (data: Uint8Array) => void;
|
|
10
|
+
}
|
|
11
|
+
export interface ExecResult {
|
|
12
|
+
/** null when the process was killed. */
|
|
13
|
+
exitCode: number | null;
|
|
14
|
+
}
|
|
15
|
+
export interface SpawnOptions {
|
|
16
|
+
cwd?: string;
|
|
17
|
+
env?: NodeJS.ProcessEnv;
|
|
18
|
+
}
|
|
19
|
+
/** A running program, for long-lived ones that talk over stdin and stdout (MCP servers, language servers). */
|
|
20
|
+
export interface ExecProcess {
|
|
21
|
+
write(data: Uint8Array | string): void;
|
|
22
|
+
onStdout(listener: (data: Uint8Array) => void): void;
|
|
23
|
+
onStderr(listener: (data: Uint8Array) => void): void;
|
|
24
|
+
/** Also called when the program could not start, with a null code. */
|
|
25
|
+
onExit(listener: (exitCode: number | null) => void): void;
|
|
26
|
+
kill(): void;
|
|
27
|
+
}
|
|
28
|
+
export interface FileStat {
|
|
29
|
+
isDirectory: boolean;
|
|
30
|
+
size: number;
|
|
31
|
+
mtimeMs: number;
|
|
32
|
+
}
|
|
33
|
+
export interface DirEntry {
|
|
34
|
+
name: string;
|
|
35
|
+
isDirectory: boolean;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Where an agent runs commands and touches files. Paths are paths inside this environment, which may
|
|
39
|
+
* be another machine: never hand one to `node:fs`, and hand the model only these.
|
|
40
|
+
*/
|
|
41
|
+
export interface ExecEnvironment {
|
|
42
|
+
/** Working directory, inside the environment. */
|
|
43
|
+
readonly cwd: string;
|
|
44
|
+
/** This machine: pi's own implementations of its tools do the same work, so they stay in use. */
|
|
45
|
+
readonly local: boolean;
|
|
46
|
+
exec(command: string, options?: ExecOptions): Promise<ExecResult>;
|
|
47
|
+
spawn(command: string, args: string[], options?: SpawnOptions): ExecProcess;
|
|
48
|
+
readFile(path: string): Promise<Uint8Array>;
|
|
49
|
+
writeFile(path: string, data: Uint8Array | string): Promise<void>;
|
|
50
|
+
/** undefined when nothing is there. */
|
|
51
|
+
stat(path: string): Promise<FileStat | undefined>;
|
|
52
|
+
readdir(path: string): Promise<DirEntry[]>;
|
|
53
|
+
mkdir(path: string): Promise<void>;
|
|
54
|
+
remove(path: string, options?: {
|
|
55
|
+
recursive?: boolean;
|
|
56
|
+
}): Promise<void>;
|
|
57
|
+
}
|
|
58
|
+
/** Bind another implementation with `rebind: true` to run sessions elsewhere. */
|
|
59
|
+
export interface ExecEnvironmentFactory {
|
|
60
|
+
create(cwd: string): ExecEnvironment;
|
|
61
|
+
}
|
|
62
|
+
//# sourceMappingURL=exec-protocol.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"exec-protocol.d.ts","sourceRoot":"","sources":["../../src/node/exec-protocol.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,sBAAsB,eAAmC,CAAC;AAEvE,MAAM,WAAW,WAAW;IACxB,6CAA6C;IAC7C,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,GAAG,CAAC,EAAE,MAAM,CAAC,UAAU,CAAC;IACxB,sDAAsD;IACtD,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,MAAM,CAAC,EAAE,WAAW,CAAC;IACrB,MAAM,CAAC,EAAE,CAAC,IAAI,EAAE,UAAU,KAAK,IAAI,CAAC;CACvC;AAED,MAAM,WAAW,UAAU;IACvB,wCAAwC;IACxC,QAAQ,EAAE,MAAM,GAAG,IAAI,CAAC;CAC3B;AAED,MAAM,WAAW,YAAY;IACzB,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,GAAG,CAAC,EAAE,MAAM,CAAC,UAAU,CAAC;CAC3B;AAED,8GAA8G;AAC9G,MAAM,WAAW,WAAW;IACxB,KAAK,CAAC,IAAI,EAAE,UAAU,GAAG,MAAM,GAAG,IAAI,CAAC;IACvC,QAAQ,CAAC,QAAQ,EAAE,CAAC,IAAI,EAAE,UAAU,KAAK,IAAI,GAAG,IAAI,CAAC;IACrD,QAAQ,CAAC,QAAQ,EAAE,CAAC,IAAI,EAAE,UAAU,KAAK,IAAI,GAAG,IAAI,CAAC;IACrD,sEAAsE;IACtE,MAAM,CAAC,QAAQ,EAAE,CAAC,QAAQ,EAAE,MAAM,GAAG,IAAI,KAAK,IAAI,GAAG,IAAI,CAAC;IAC1D,IAAI,IAAI,IAAI,CAAC;CAChB;AAED,M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AG,eAAe,CAAC;CACxC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"exec-protocol.js","sourceRoot":"","sources":["../../src/node/exec-protocol.ts"],"names":[],"mappings":";;;AAAa,QAAA,sBAAsB,GAAG,MAAM,CAAC,wBAAwB,CAAC,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/node/index.ts"],"names":[],"mappings":"AAAA,cAAc,iBAAiB,CAAC;AAChC,cAAc,0BAA0B,CAAC"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./exec-protocol"), exports);
|
|
18
|
+
__exportStar(require("./local-exec-environment"), exports);
|
|
19
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/node/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,kDAAgC;AAChC,2DAAyC"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { type ChildProcess } from 'node:child_process';
|
|
2
|
+
import type { DirEntry, ExecEnvironment, ExecOptions, ExecProcess, ExecResult, FileStat, SpawnOptions } from './exec-protocol';
|
|
3
|
+
/** This machine. */
|
|
4
|
+
export declare class LocalExecEnvironment implements ExecEnvironment {
|
|
5
|
+
readonly cwd: string;
|
|
6
|
+
readonly local: boolean;
|
|
7
|
+
constructor(cwd: string);
|
|
8
|
+
exec(command: string, options?: ExecOptions): Promise<ExecResult>;
|
|
9
|
+
spawn(command: string, args: string[], options?: SpawnOptions): ExecProcess;
|
|
10
|
+
readFile(path: string): Promise<Uint8Array>;
|
|
11
|
+
writeFile(path: string, data: Uint8Array | string): Promise<void>;
|
|
12
|
+
stat(path: string): Promise<FileStat | undefined>;
|
|
13
|
+
readdir(path: string): Promise<DirEntry[]>;
|
|
14
|
+
mkdir(path: string): Promise<void>;
|
|
15
|
+
remove(path: string, options?: {
|
|
16
|
+
recursive?: boolean;
|
|
17
|
+
}): Promise<void>;
|
|
18
|
+
protected resolve(path: string): string;
|
|
19
|
+
protected killGroup(child: ChildProcess): void;
|
|
20
|
+
}
|
|
21
|
+
//# sourceMappingURL=local-exec-environment.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"local-exec-environment.d.ts","sourceRoot":"","sources":["../../src/node/local-exec-environment.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,YAAY,EAAS,MAAM,oBAAoB,CAAC;AAG9D,OAAO,KAAK,EAAE,QAAQ,EAAE,eAAe,EAAE,WAAW,EAAE,WAAW,EAAE,UAAU,EAAE,QAAQ,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAE/H,oBAAoB;AACpB,qBAAa,oBAAqB,YAAW,eAAe;IAG5C,QAAQ,CAAC,GAAG,EAAE,MAAM;IAFhC,QAAQ,CAAC,KAAK,EAAE,OAAO,CAAQ;IAE/B,YAAqB,GAAG,EAAE,MAAM,EAAI;IAEpC,IAAI,CAAC,OAAO,EAAE,MAAM,EAAE,OAAO,GAAE,WAAgB,GAAG,OAAO,CAAC,UAAU,CAAC,CA0BpE;IAED,KAAK,CAAC,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,EAAE,OAAO,GAAE,YAAiB,GAAG,WAAW,CAkB9E;IAED,QAAQ,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,UAAU,CAAC,CAE1C;IAED,SAAS,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,UAAU,GAAG,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAEhE;IAEK,IAAI,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,QAAQ,GAAG,SAAS,CAAC,CAUtD;IAEK,OAAO,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,QAAQ,EAAE,CAAC,CAG/C;IAEK,KAAK,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAEvC;IAED,MAAM,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,GAAE;QAAE,SAAS,CAAC,EAAE,OAAO,CAAA;KAAO,GAAG,OAAO,CAAC,IAAI,CAAC,CAEzE;IAED,SAAS,CAAC,OAAO,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAEtC;IAED,SAAS,CAAC,SAAS,CAAC,KAAK,EAAE,YAAY,GAAG,IAAI,CAa7C;CACJ"}
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.LocalExecEnvironment = void 0;
|
|
4
|
+
const node_child_process_1 = require("node:child_process");
|
|
5
|
+
const promises_1 = require("node:fs/promises");
|
|
6
|
+
const node_path_1 = require("node:path");
|
|
7
|
+
/** This machine. */
|
|
8
|
+
class LocalExecEnvironment {
|
|
9
|
+
constructor(cwd) {
|
|
10
|
+
this.cwd = cwd;
|
|
11
|
+
this.local = true;
|
|
12
|
+
}
|
|
13
|
+
exec(command, options = {}) {
|
|
14
|
+
var _a, _b;
|
|
15
|
+
const [shell, ...args] = process.platform === 'win32' ? ['cmd.exe', '/d', '/s', '/c'] : ['/bin/sh', '-c'];
|
|
16
|
+
const child = (0, node_child_process_1.spawn)(shell, [...args, command], {
|
|
17
|
+
cwd: (0, node_path_1.resolve)(this.cwd, (_a = options.cwd) !== null && _a !== void 0 ? _a : '.'),
|
|
18
|
+
env: (_b = options.env) !== null && _b !== void 0 ? _b : process.env,
|
|
19
|
+
stdio: ['ignore', 'pipe', 'pipe'],
|
|
20
|
+
// Its own process group: killing the shell must also end what the command started.
|
|
21
|
+
detached: process.platform !== 'win32',
|
|
22
|
+
});
|
|
23
|
+
return new Promise(done => {
|
|
24
|
+
var _a, _b, _c, _d;
|
|
25
|
+
const kill = () => this.killGroup(child);
|
|
26
|
+
const timer = options.timeout ? setTimeout(kill, options.timeout) : undefined;
|
|
27
|
+
(_a = options.signal) === null || _a === void 0 ? void 0 : _a.addEventListener('abort', kill, { once: true });
|
|
28
|
+
if ((_b = options.signal) === null || _b === void 0 ? void 0 : _b.aborted) {
|
|
29
|
+
kill();
|
|
30
|
+
}
|
|
31
|
+
(_c = child.stdout) === null || _c === void 0 ? void 0 : _c.on('data', data => { var _a; return (_a = options.onData) === null || _a === void 0 ? void 0 : _a.call(options, data); });
|
|
32
|
+
(_d = child.stderr) === null || _d === void 0 ? void 0 : _d.on('data', data => { var _a; return (_a = options.onData) === null || _a === void 0 ? void 0 : _a.call(options, data); });
|
|
33
|
+
const finish = (exitCode) => {
|
|
34
|
+
var _a;
|
|
35
|
+
clearTimeout(timer);
|
|
36
|
+
(_a = options.signal) === null || _a === void 0 ? void 0 : _a.removeEventListener('abort', kill);
|
|
37
|
+
done({ exitCode });
|
|
38
|
+
};
|
|
39
|
+
child.once('error', () => finish(null));
|
|
40
|
+
child.once('close', finish);
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
spawn(command, args, options = {}) {
|
|
44
|
+
var _a, _b;
|
|
45
|
+
const child = (0, node_child_process_1.spawn)(command, args, {
|
|
46
|
+
cwd: (0, node_path_1.resolve)(this.cwd, (_a = options.cwd) !== null && _a !== void 0 ? _a : '.'),
|
|
47
|
+
env: (_b = options.env) !== null && _b !== void 0 ? _b : process.env,
|
|
48
|
+
stdio: ['pipe', 'pipe', 'pipe'],
|
|
49
|
+
});
|
|
50
|
+
// Writes after the program ended surface as EPIPE here; onExit tells the owner.
|
|
51
|
+
child.stdin.on('error', () => undefined);
|
|
52
|
+
return {
|
|
53
|
+
write: data => child.stdin.write(data),
|
|
54
|
+
onStdout: listener => child.stdout.on('data', listener),
|
|
55
|
+
onStderr: listener => child.stderr.on('data', listener),
|
|
56
|
+
onExit: listener => {
|
|
57
|
+
child.once('error', () => listener(null));
|
|
58
|
+
child.once('close', listener);
|
|
59
|
+
},
|
|
60
|
+
kill: () => child.kill(),
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
readFile(path) {
|
|
64
|
+
return (0, promises_1.readFile)(this.resolve(path));
|
|
65
|
+
}
|
|
66
|
+
writeFile(path, data) {
|
|
67
|
+
return (0, promises_1.writeFile)(this.resolve(path), data);
|
|
68
|
+
}
|
|
69
|
+
async stat(path) {
|
|
70
|
+
try {
|
|
71
|
+
const info = await (0, promises_1.stat)(this.resolve(path));
|
|
72
|
+
return { isDirectory: info.isDirectory(), size: info.size, mtimeMs: info.mtimeMs };
|
|
73
|
+
}
|
|
74
|
+
catch (error) {
|
|
75
|
+
if (error.code === 'ENOENT') {
|
|
76
|
+
return undefined;
|
|
77
|
+
}
|
|
78
|
+
throw error;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
async readdir(path) {
|
|
82
|
+
const entries = await (0, promises_1.readdir)(this.resolve(path), { withFileTypes: true });
|
|
83
|
+
return entries.map(entry => ({ name: entry.name, isDirectory: entry.isDirectory() }));
|
|
84
|
+
}
|
|
85
|
+
async mkdir(path) {
|
|
86
|
+
await (0, promises_1.mkdir)(this.resolve(path), { recursive: true });
|
|
87
|
+
}
|
|
88
|
+
remove(path, options = {}) {
|
|
89
|
+
return (0, promises_1.rm)(this.resolve(path), { recursive: options.recursive, force: true });
|
|
90
|
+
}
|
|
91
|
+
resolve(path) {
|
|
92
|
+
return (0, node_path_1.resolve)(this.cwd, path);
|
|
93
|
+
}
|
|
94
|
+
killGroup(child) {
|
|
95
|
+
if (child.pid === undefined) {
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
try {
|
|
99
|
+
if (process.platform === 'win32') {
|
|
100
|
+
child.kill();
|
|
101
|
+
}
|
|
102
|
+
else {
|
|
103
|
+
process.kill(-child.pid, 'SIGKILL');
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
catch (_a) {
|
|
107
|
+
// It already ended.
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
exports.LocalExecEnvironment = LocalExecEnvironment;
|
|
112
|
+
//# sourceMappingURL=local-exec-environment.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"local-exec-environment.js","sourceRoot":"","sources":["../../src/node/local-exec-environment.ts"],"names":[],"mappings":";;;AAAA,2DAA8D;AAC9D,+CAAiF;AACjF,yCAAoC;AAGpC,oBAAoB;AACpB;IAGI,YAAqB,GAAW;QAAX,QAAG,GAAH,GAAG,CAAQ;QAFvB,UAAK,GAAY,IAAI,CAAC;IAEI,CAAC;IAEpC,IAAI,CAAC,OAAe,EAAE,OAAO,GAAgB,EAAE;;QAC3C,MAAM,CAAC,KAAK,EAAE,GAAG,IAAI,CAAC,GAAG,OAAO,CAAC,QAAQ,KAAK,OAAO,CAAC,CAAC,CAAC,CAAC,SAAS,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,SAAS,EAAE,IAAI,CAAC,CAAC;QAC1G,MAAM,KAAK,GAAG,IAAA,0BAAK,E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EAAE,KAAK,CAAC,WAAW,EAAE,EAAE,CAAC,CAAC,CAAC;IAC1F,CAAC;IAED,KAAK,CAAC,KAAK,CAAC,IAAY;QACpB,MAAM,IAAA,gBAAK,EAAC,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC,CAAC;IACzD,CAAC;IAED,MAAM,CAAC,IAAY,EAAE,OAAO,GAA4B,EAAE;QACtD,OAAO,IAAA,aAAE,EAAC,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE,EAAE,SAAS,EAAE,OAAO,CAAC,SAAS,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC,CAAC;IACjF,CAAC;IAES,OAAO,CAAC,IAAY;QAC1B,OAAO,IAAA,mBAAO,EAAC,IAAI,CAAC,GAAG,EAAE,IAAI,CAAC,CAAC;IACnC,CAAC;IAES,SAAS,CAAC,KAAmB;QACnC,IAAI,KAAK,CAAC,GAAG,KAAK,SAAS,EAAE,CAAC;YAC1B,OAAO;QACX,CAAC;QACD,IAAI,CAAC;YACD,IAAI,OAAO,CAAC,QAAQ,KAAK,OAAO,EAAE,CAAC;gBAC/B,KAAK,CAAC,IAAI,EAAE,CAAC;YACjB,CAAC;iBAAM,CAAC;gBACJ,OAAO,CAAC,IAAI,CAAC,CAAC,KAAK,CAAC,GAAG,EAAE,SAAS,CAAC,CAAC;YACxC,CAAC;QACL,CAAC;mBAAO,CAAC;YACL,oBAAoB;QACxB,CAAC;IACL,CAAC;CACJ"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"local-exec-environment.spec.d.ts","sourceRoot":"","sources":["../../src/node/local-exec-environment.spec.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const bun_test_1 = require("bun:test");
|
|
4
|
+
const node_fs_1 = require("node:fs");
|
|
5
|
+
const node_os_1 = require("node:os");
|
|
6
|
+
const node_path_1 = require("node:path");
|
|
7
|
+
const local_exec_environment_1 = require("./local-exec-environment");
|
|
8
|
+
const environment = () => new local_exec_environment_1.LocalExecEnvironment((0, node_fs_1.mkdtempSync)((0, node_path_1.join)((0, node_os_1.tmpdir)(), 'ta-exec-')));
|
|
9
|
+
const text = (chunks) => Buffer.concat(chunks).toString();
|
|
10
|
+
(0, bun_test_1.describe)('LocalExecEnvironment', () => {
|
|
11
|
+
(0, bun_test_1.it)('runs a command in its directory and streams the output', async () => {
|
|
12
|
+
const env = environment();
|
|
13
|
+
const chunks = [];
|
|
14
|
+
const result = await env.exec('pwd; echo out; echo err >&2; exit 3', { onData: data => chunks.push(data) });
|
|
15
|
+
(0, bun_test_1.expect)(result.exitCode).toBe(3);
|
|
16
|
+
(0, bun_test_1.expect)(text(chunks)).toContain('out');
|
|
17
|
+
(0, bun_test_1.expect)(text(chunks)).toContain('err');
|
|
18
|
+
});
|
|
19
|
+
(0, bun_test_1.it)('kills what the command started when aborted', async () => {
|
|
20
|
+
const controller = new AbortController();
|
|
21
|
+
const running = environment().exec('sleep 30 & wait', { signal: controller.signal });
|
|
22
|
+
controller.abort();
|
|
23
|
+
(0, bun_test_1.expect)((await running).exitCode).not.toBe(0);
|
|
24
|
+
});
|
|
25
|
+
(0, bun_test_1.it)('kills the command at the timeout', async () => {
|
|
26
|
+
const start = Date.now();
|
|
27
|
+
await environment().exec('sleep 30', { timeout: 50 });
|
|
28
|
+
(0, bun_test_1.expect)(Date.now() - start).toBeLessThan(5000);
|
|
29
|
+
});
|
|
30
|
+
(0, bun_test_1.it)('reports a program that cannot start', async () => {
|
|
31
|
+
const exit = await new Promise(done => environment().spawn('/no/such/program', []).onExit(done));
|
|
32
|
+
(0, bun_test_1.expect)(exit).toBeNull();
|
|
33
|
+
});
|
|
34
|
+
(0, bun_test_1.it)('talks to a long-lived program over stdin and stdout', async () => {
|
|
35
|
+
const child = environment().spawn('cat', []);
|
|
36
|
+
const output = new Promise(done => child.onStdout(data => done(Buffer.from(data).toString())));
|
|
37
|
+
child.write('hello');
|
|
38
|
+
(0, bun_test_1.expect)(await output).toBe('hello');
|
|
39
|
+
const exit = new Promise(done => child.onExit(done));
|
|
40
|
+
child.kill();
|
|
41
|
+
await exit;
|
|
42
|
+
});
|
|
43
|
+
(0, bun_test_1.it)('reads, writes, lists and removes files relative to its directory', async () => {
|
|
44
|
+
var _a;
|
|
45
|
+
const env = environment();
|
|
46
|
+
await env.mkdir('a/b');
|
|
47
|
+
await env.writeFile('a/b/c.txt', 'data');
|
|
48
|
+
(0, bun_test_1.expect)(Buffer.from(await env.readFile((0, node_path_1.join)(env.cwd, 'a/b/c.txt'))).toString()).toBe('data');
|
|
49
|
+
(0, bun_test_1.expect)(await env.readdir('a')).toEqual([{ name: 'b', isDirectory: true }]);
|
|
50
|
+
(0, bun_test_1.expect)((_a = (await env.stat('a/b'))) === null || _a === void 0 ? void 0 : _a.isDirectory).toBe(true);
|
|
51
|
+
await env.remove('a', { recursive: true });
|
|
52
|
+
(0, bun_test_1.expect)(await env.stat('a')).toBeUndefined();
|
|
53
|
+
});
|
|
54
|
+
});
|
|
55
|
+
//# sourceMappingURL=local-exec-environment.spec.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"local-exec-environment.spec.js","sourceRoot":"","sources":["../../src/node/local-exec-environment.spec.ts"],"names":[],"mappings":";;AAAA,uCAAgD;AAChD,qCAAsC;AACtC,qCAAiC;AACjC,yCAAiC;AACjC,qEAAgE;AAEhE,MAAM,WAAW,GAAG,GAAG,EAAE,CAAC,IAAI,6CAAoB,CAAC,IAAA,qBAAW,EAAC,IAAA,gBAAI,EAAC,IAAA,gBAAM,GAAE,EAAE,UAAU,CAAC,CAAC,CAAC,CAAC;AAC5F,MAAM,IAAI,GAAG,CAAC,MAAoB,EAAE,EAAE,CAAC,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,QAAQ,EAAE,CAA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WAAW,EAAE,CAAC;QAC1B,MAAM,GAAG,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;QACvB,MAAM,GAAG,CAAC,SAAS,CAAC,WAAW,EAAE,MAAM,CAAC,CAAC;QACzC,IAAA,iBAAM,EAAC,MAAM,CAAC,IAAI,CAAC,MAAM,GAAG,CAAC,QAAQ,CAAC,IAAA,gBAAI,EAAC,GAAG,CAAC,GAAG,EAAE,WAAW,CAAC,CAAC,CAAC,CAAC,QAAQ,EAAE,CAAC,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;QAC5F,IAAA,iBAAM,EAAC,MAAM,GAAG,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,EAAE,IAAI,EAAE,GAAG,EAAE,WAAW,EAAE,IAAI,EAAE,CAAC,CAAC,CAAC;QAC3E,IAAA,iBAAM,EAAC,MAAA,CAAC,MAAM,GAAG,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,0CAAE,WAAW,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QACxD,MAAM,GAAG,CAAC,MAAM,CAAC,GAAG,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC,CAAC;QAC3C,IAAA,iBAAM,EAAC,MAAM,GAAG,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,aAAa,EAAE,CAAC;IAChD,CAAC,CAAC,CAAC;AACP,CAAC,CAAC,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,54 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@celljs/ta-exec",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "4.5.1",
|
|
4
|
+
"description": "Where ta runs commands and touches files: the ExecEnvironment interface and its local implementation",
|
|
5
|
+
"homepage": "https://github.com/cellbang/cell",
|
|
6
|
+
"bugs": {
|
|
7
|
+
"url": "https://github.com/cellbang/cell/issues"
|
|
8
|
+
},
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "https://github.com/cellbang/cell.git"
|
|
12
|
+
},
|
|
13
|
+
"license": "MIT",
|
|
14
|
+
"engines": {
|
|
15
|
+
"node": ">=22.19.0"
|
|
16
|
+
},
|
|
17
|
+
"main": "lib/node/index.js",
|
|
18
|
+
"typings": "lib/node/index.d.ts",
|
|
19
|
+
"files": [
|
|
20
|
+
"lib",
|
|
21
|
+
"esm",
|
|
22
|
+
"src"
|
|
23
|
+
],
|
|
24
|
+
"scripts": {
|
|
25
|
+
"build": "tsc -b && bun ../../scripts/build-esm.ts",
|
|
26
|
+
"clean": "rm -rf lib esm *.tsbuildinfo",
|
|
27
|
+
"test": "bun test ./lib",
|
|
28
|
+
"watch": "tsc -b -w --preserveWatchOutput"
|
|
29
|
+
},
|
|
30
|
+
"dependencies": {},
|
|
31
|
+
"publishConfig": {
|
|
32
|
+
"access": "public"
|
|
33
|
+
},
|
|
34
|
+
"exports": {
|
|
35
|
+
".": {
|
|
36
|
+
"import": "./esm/node/index.js",
|
|
37
|
+
"default": "./lib/node/index.js"
|
|
38
|
+
},
|
|
39
|
+
"./package.json": "./package.json",
|
|
40
|
+
"./lib/node": {
|
|
41
|
+
"import": "./esm/node/index.js",
|
|
42
|
+
"default": "./lib/node/index.js"
|
|
43
|
+
},
|
|
44
|
+
"./lib/*.js": {
|
|
45
|
+
"import": "./esm/*.js",
|
|
46
|
+
"default": "./lib/*.js"
|
|
47
|
+
},
|
|
48
|
+
"./lib/*": {
|
|
49
|
+
"import": "./esm/*.js",
|
|
50
|
+
"default": "./lib/*.js"
|
|
51
|
+
},
|
|
52
|
+
"./*": "./*"
|
|
53
|
+
}
|
|
54
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
export const ExecEnvironmentFactory = Symbol('ExecEnvironmentFactory');
|
|
2
|
+
|
|
3
|
+
export interface ExecOptions {
|
|
4
|
+
/** Default: the `cwd` of the environment. */
|
|
5
|
+
cwd?: string;
|
|
6
|
+
env?: NodeJS.ProcessEnv;
|
|
7
|
+
/** Milliseconds; the process is killed afterwards. */
|
|
8
|
+
timeout?: number;
|
|
9
|
+
signal?: AbortSignal;
|
|
10
|
+
onData?: (data: Uint8Array) => void;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export interface ExecResult {
|
|
14
|
+
/** null when the process was killed. */
|
|
15
|
+
exitCode: number | null;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface SpawnOptions {
|
|
19
|
+
cwd?: string;
|
|
20
|
+
env?: NodeJS.ProcessEnv;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** A running program, for long-lived ones that talk over stdin and stdout (MCP servers, language servers). */
|
|
24
|
+
export interface ExecProcess {
|
|
25
|
+
write(data: Uint8Array | string): void;
|
|
26
|
+
onStdout(listener: (data: Uint8Array) => void): void;
|
|
27
|
+
onStderr(listener: (data: Uint8Array) => void): void;
|
|
28
|
+
/** Also called when the program could not start, with a null code. */
|
|
29
|
+
onExit(listener: (exitCode: number | null) => void): void;
|
|
30
|
+
kill(): void;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export interface FileStat {
|
|
34
|
+
isDirectory: boolean;
|
|
35
|
+
size: number;
|
|
36
|
+
mtimeMs: number;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export interface DirEntry {
|
|
40
|
+
name: string;
|
|
41
|
+
isDirectory: boolean;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Where an agent runs commands and touches files. Paths are paths inside this environment, which may
|
|
46
|
+
* be another machine: never hand one to `node:fs`, and hand the model only these.
|
|
47
|
+
*/
|
|
48
|
+
export interface ExecEnvironment {
|
|
49
|
+
/** Working directory, inside the environment. */
|
|
50
|
+
readonly cwd: string;
|
|
51
|
+
/** This machine: pi's own implementations of its tools do the same work, so they stay in use. */
|
|
52
|
+
readonly local: boolean;
|
|
53
|
+
exec(command: string, options?: ExecOptions): Promise<ExecResult>;
|
|
54
|
+
spawn(command: string, args: string[], options?: SpawnOptions): ExecProcess;
|
|
55
|
+
readFile(path: string): Promise<Uint8Array>;
|
|
56
|
+
writeFile(path: string, data: Uint8Array | string): Promise<void>;
|
|
57
|
+
/** undefined when nothing is there. */
|
|
58
|
+
stat(path: string): Promise<FileStat | undefined>;
|
|
59
|
+
readdir(path: string): Promise<DirEntry[]>;
|
|
60
|
+
mkdir(path: string): Promise<void>;
|
|
61
|
+
remove(path: string, options?: { recursive?: boolean }): Promise<void>;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** Bind another implementation with `rebind: true` to run sessions elsewhere. */
|
|
65
|
+
export interface ExecEnvironmentFactory {
|
|
66
|
+
create(cwd: string): ExecEnvironment;
|
|
67
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { describe, expect, it } from 'bun:test';
|
|
2
|
+
import { mkdtempSync } from 'node:fs';
|
|
3
|
+
import { tmpdir } from 'node:os';
|
|
4
|
+
import { join } from 'node:path';
|
|
5
|
+
import { LocalExecEnvironment } from './local-exec-environment';
|
|
6
|
+
|
|
7
|
+
const environment = () => new LocalExecEnvironment(mkdtempSync(join(tmpdir(), 'ta-exec-')));
|
|
8
|
+
const text = (chunks: Uint8Array[]) => Buffer.concat(chunks).toString();
|
|
9
|
+
|
|
10
|
+
describe('LocalExecEnvironment', () => {
|
|
11
|
+
it('runs a command in its directory and streams the output', async () => {
|
|
12
|
+
const env = environment();
|
|
13
|
+
const chunks: Uint8Array[] = [];
|
|
14
|
+
const result = await env.exec('pwd; echo out; echo err >&2; exit 3', { onData: data => chunks.push(data) });
|
|
15
|
+
expect(result.exitCode).toBe(3);
|
|
16
|
+
expect(text(chunks)).toContain('out');
|
|
17
|
+
expect(text(chunks)).toContain('err');
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
it('kills what the command started when aborted', async () => {
|
|
21
|
+
const controller = new AbortController();
|
|
22
|
+
const running = environment().exec('sleep 30 & wait', { signal: controller.signal });
|
|
23
|
+
controller.abort();
|
|
24
|
+
expect((await running).exitCode).not.toBe(0);
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
it('kills the command at the timeout', async () => {
|
|
28
|
+
const start = Date.now();
|
|
29
|
+
await environment().exec('sleep 30', { timeout: 50 });
|
|
30
|
+
expect(Date.now() - start).toBeLessThan(5000);
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
it('reports a program that cannot start', async () => {
|
|
34
|
+
const exit = await new Promise(done => environment().spawn('/no/such/program', []).onExit(done));
|
|
35
|
+
expect(exit).toBeNull();
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it('talks to a long-lived program over stdin and stdout', async () => {
|
|
39
|
+
const child = environment().spawn('cat', []);
|
|
40
|
+
const output = new Promise<string>(done => child.onStdout(data => done(Buffer.from(data).toString())));
|
|
41
|
+
child.write('hello');
|
|
42
|
+
expect(await output).toBe('hello');
|
|
43
|
+
const exit = new Promise(done => child.onExit(done));
|
|
44
|
+
child.kill();
|
|
45
|
+
await exit;
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it('reads, writes, lists and removes files relative to its directory', async () => {
|
|
49
|
+
const env = environment();
|
|
50
|
+
await env.mkdir('a/b');
|
|
51
|
+
await env.writeFile('a/b/c.txt', 'data');
|
|
52
|
+
expect(Buffer.from(await env.readFile(join(env.cwd, 'a/b/c.txt'))).toString()).toBe('data');
|
|
53
|
+
expect(await env.readdir('a')).toEqual([{ name: 'b', isDirectory: true }]);
|
|
54
|
+
expect((await env.stat('a/b'))?.isDirectory).toBe(true);
|
|
55
|
+
await env.remove('a', { recursive: true });
|
|
56
|
+
expect(await env.stat('a')).toBeUndefined();
|
|
57
|
+
});
|
|
58
|
+
});
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import { type ChildProcess, spawn } from 'node:child_process';
|
|
2
|
+
import { mkdir, readdir, readFile, rm, stat, writeFile } from 'node:fs/promises';
|
|
3
|
+
import { resolve } from 'node:path';
|
|
4
|
+
import type { DirEntry, ExecEnvironment, ExecOptions, ExecProcess, ExecResult, FileStat, SpawnOptions } from './exec-protocol';
|
|
5
|
+
|
|
6
|
+
/** This machine. */
|
|
7
|
+
export class LocalExecEnvironment implements ExecEnvironment {
|
|
8
|
+
readonly local: boolean = true;
|
|
9
|
+
|
|
10
|
+
constructor(readonly cwd: string) {}
|
|
11
|
+
|
|
12
|
+
exec(command: string, options: ExecOptions = {}): Promise<ExecResult> {
|
|
13
|
+
const [shell, ...args] = process.platform === 'win32' ? ['cmd.exe', '/d', '/s', '/c'] : ['/bin/sh', '-c'];
|
|
14
|
+
const child = spawn(shell, [...args, command], {
|
|
15
|
+
cwd: resolve(this.cwd, options.cwd ?? '.'),
|
|
16
|
+
env: options.env ?? process.env,
|
|
17
|
+
stdio: ['ignore', 'pipe', 'pipe'],
|
|
18
|
+
// Its own process group: killing the shell must also end what the command started.
|
|
19
|
+
detached: process.platform !== 'win32',
|
|
20
|
+
});
|
|
21
|
+
return new Promise(done => {
|
|
22
|
+
const kill = () => this.killGroup(child);
|
|
23
|
+
const timer = options.timeout ? setTimeout(kill, options.timeout) : undefined;
|
|
24
|
+
options.signal?.addEventListener('abort', kill, { once: true });
|
|
25
|
+
if (options.signal?.aborted) {
|
|
26
|
+
kill();
|
|
27
|
+
}
|
|
28
|
+
child.stdout?.on('data', data => options.onData?.(data));
|
|
29
|
+
child.stderr?.on('data', data => options.onData?.(data));
|
|
30
|
+
const finish = (exitCode: number | null) => {
|
|
31
|
+
clearTimeout(timer);
|
|
32
|
+
options.signal?.removeEventListener('abort', kill);
|
|
33
|
+
done({ exitCode });
|
|
34
|
+
};
|
|
35
|
+
child.once('error', () => finish(null));
|
|
36
|
+
child.once('close', finish);
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
spawn(command: string, args: string[], options: SpawnOptions = {}): ExecProcess {
|
|
41
|
+
const child = spawn(command, args, {
|
|
42
|
+
cwd: resolve(this.cwd, options.cwd ?? '.'),
|
|
43
|
+
env: options.env ?? process.env,
|
|
44
|
+
stdio: ['pipe', 'pipe', 'pipe'],
|
|
45
|
+
});
|
|
46
|
+
// Writes after the program ended surface as EPIPE here; onExit tells the owner.
|
|
47
|
+
child.stdin.on('error', () => undefined);
|
|
48
|
+
return {
|
|
49
|
+
write: data => child.stdin.write(data),
|
|
50
|
+
onStdout: listener => child.stdout.on('data', listener),
|
|
51
|
+
onStderr: listener => child.stderr.on('data', listener),
|
|
52
|
+
onExit: listener => {
|
|
53
|
+
child.once('error', () => listener(null));
|
|
54
|
+
child.once('close', listener);
|
|
55
|
+
},
|
|
56
|
+
kill: () => child.kill(),
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
readFile(path: string): Promise<Uint8Array> {
|
|
61
|
+
return readFile(this.resolve(path));
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
writeFile(path: string, data: Uint8Array | string): Promise<void> {
|
|
65
|
+
return writeFile(this.resolve(path), data);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
async stat(path: string): Promise<FileStat | undefined> {
|
|
69
|
+
try {
|
|
70
|
+
const info = await stat(this.resolve(path));
|
|
71
|
+
return { isDirectory: info.isDirectory(), size: info.size, mtimeMs: info.mtimeMs };
|
|
72
|
+
} catch (error) {
|
|
73
|
+
if ((error as NodeJS.ErrnoException).code === 'ENOENT') {
|
|
74
|
+
return undefined;
|
|
75
|
+
}
|
|
76
|
+
throw error;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
async readdir(path: string): Promise<DirEntry[]> {
|
|
81
|
+
const entries = await readdir(this.resolve(path), { withFileTypes: true });
|
|
82
|
+
return entries.map(entry => ({ name: entry.name, isDirectory: entry.isDirectory() }));
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
async mkdir(path: string): Promise<void> {
|
|
86
|
+
await mkdir(this.resolve(path), { recursive: true });
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
remove(path: string, options: { recursive?: boolean } = {}): Promise<void> {
|
|
90
|
+
return rm(this.resolve(path), { recursive: options.recursive, force: true });
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
protected resolve(path: string): string {
|
|
94
|
+
return resolve(this.cwd, path);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
protected killGroup(child: ChildProcess): void {
|
|
98
|
+
if (child.pid === undefined) {
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
try {
|
|
102
|
+
if (process.platform === 'win32') {
|
|
103
|
+
child.kill();
|
|
104
|
+
} else {
|
|
105
|
+
process.kill(-child.pid, 'SIGKILL');
|
|
106
|
+
}
|
|
107
|
+
} catch {
|
|
108
|
+
// It already ended.
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
}
|