@vite-hub/shell 0.0.4 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +29 -6
- package/dist/analyze-BXwz20IH.js +247 -0
- package/dist/index.d.ts +4 -5
- package/dist/index.js +2 -2
- package/dist/{just-bash-D84udCFy.d.ts → just-bash-DAyZQWLK.d.ts} +2 -2
- package/dist/providers/just-bash.d.ts +1 -1
- package/dist/providers/just-bash.js +132 -59
- package/dist/session-CIy2VLp9.js +245 -0
- package/dist/{types-CNRdehmi.d.ts → types-2uHfTVHi.d.ts} +23 -3
- package/dist/workspace.d.ts +14 -11
- package/dist/workspace.js +237 -209
- package/package.json +29 -14
- package/dist/analyze-Bu5Ueqph.js +0 -126
- package/dist/parse-D9VT4P6N.js +0 -41
- package/dist/providers/cloudflare.d.ts +0 -28
- package/dist/providers/cloudflare.js +0 -44
- package/dist/session-0OrrlmE9.js +0 -178
|
@@ -0,0 +1,245 @@
|
|
|
1
|
+
import { i as shellErrorDiagnostics, t as analyzeShellCommand } from "./analyze-BXwz20IH.js";
|
|
2
|
+
//#region src/runtime/session.ts
|
|
3
|
+
function applyOutputLimit(result, maxLength) {
|
|
4
|
+
const limit = normalizeOutputLength(maxLength);
|
|
5
|
+
if (limit === void 0) return result;
|
|
6
|
+
const next = {
|
|
7
|
+
command: result.command,
|
|
8
|
+
cwd: result.cwd,
|
|
9
|
+
durationMs: result.durationMs,
|
|
10
|
+
event: result.event,
|
|
11
|
+
exitCode: result.exitCode,
|
|
12
|
+
maxOutputLength: limit,
|
|
13
|
+
outputTruncated: result.outputTruncated,
|
|
14
|
+
stderr: result.stderr,
|
|
15
|
+
stdout: result.stdout,
|
|
16
|
+
timedOut: result.timedOut,
|
|
17
|
+
workspaceGuardrail: result.workspaceGuardrail
|
|
18
|
+
};
|
|
19
|
+
let truncated = false;
|
|
20
|
+
if (next.stdout.length > limit) {
|
|
21
|
+
next.stdout = `${next.stdout.slice(0, limit)}\n[output truncated to ${limit} characters]\n`;
|
|
22
|
+
truncated = true;
|
|
23
|
+
}
|
|
24
|
+
if (next.stderr.length > limit) {
|
|
25
|
+
next.stderr = `${next.stderr.slice(0, limit)}\n[output truncated to ${limit} characters]\n`;
|
|
26
|
+
truncated = true;
|
|
27
|
+
}
|
|
28
|
+
return truncated ? {
|
|
29
|
+
...next,
|
|
30
|
+
outputTruncated: true
|
|
31
|
+
} : next;
|
|
32
|
+
}
|
|
33
|
+
function normalizeOutputLength(value) {
|
|
34
|
+
if (value === void 0) return void 0;
|
|
35
|
+
if (value === Number.POSITIVE_INFINITY) return void 0;
|
|
36
|
+
return Number.isFinite(value) ? Math.max(0, Math.floor(value)) : 0;
|
|
37
|
+
}
|
|
38
|
+
function limitOutputOptions(options, policy) {
|
|
39
|
+
const requestedOutputLength = normalizeOutputLength(options.maxOutputLength);
|
|
40
|
+
const policyOutputLength = normalizeOutputLength(policy.maxOutputLength);
|
|
41
|
+
const maxOutputLength = requestedOutputLength === void 0 ? policyOutputLength : policyOutputLength === void 0 ? requestedOutputLength : Math.min(requestedOutputLength, policyOutputLength);
|
|
42
|
+
let stdoutRemaining = maxOutputLength;
|
|
43
|
+
let stderrRemaining = maxOutputLength;
|
|
44
|
+
const limitedCallback = (callback, remaining, consume) => callback && ((data) => {
|
|
45
|
+
const limit = remaining();
|
|
46
|
+
if (limit === void 0) return callback(data);
|
|
47
|
+
if (limit <= 0) return;
|
|
48
|
+
const chunk = data.slice(0, limit);
|
|
49
|
+
consume(chunk.length);
|
|
50
|
+
if (chunk) callback(chunk);
|
|
51
|
+
});
|
|
52
|
+
return {
|
|
53
|
+
...options,
|
|
54
|
+
maxOutputLength,
|
|
55
|
+
onStdout: limitedCallback(options.onStdout, () => stdoutRemaining, (value) => {
|
|
56
|
+
stdoutRemaining = Math.max(0, (stdoutRemaining ?? 0) - value);
|
|
57
|
+
}),
|
|
58
|
+
onStderr: limitedCallback(options.onStderr, () => stderrRemaining, (value) => {
|
|
59
|
+
stderrRemaining = Math.max(0, (stderrRemaining ?? 0) - value);
|
|
60
|
+
})
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
function createPolicyObservation(command, cwd, message) {
|
|
64
|
+
return {
|
|
65
|
+
command,
|
|
66
|
+
cwd,
|
|
67
|
+
event: "policy_denied",
|
|
68
|
+
exitCode: 126,
|
|
69
|
+
stderr: message.endsWith("\n") ? message : `${message}\n`,
|
|
70
|
+
stdout: ""
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
const shellSessionDisposedMessage = "[vitehub] Shell session is disposed.";
|
|
74
|
+
var RuntimeShellSession = class {
|
|
75
|
+
provider;
|
|
76
|
+
env;
|
|
77
|
+
boundary;
|
|
78
|
+
policy;
|
|
79
|
+
#disposed = false;
|
|
80
|
+
#disposeTask;
|
|
81
|
+
#processes = /* @__PURE__ */ new Set();
|
|
82
|
+
#starts = /* @__PURE__ */ new Set();
|
|
83
|
+
#shellCalls = 0;
|
|
84
|
+
constructor(provider, policy, env) {
|
|
85
|
+
this.provider = provider;
|
|
86
|
+
this.env = env;
|
|
87
|
+
this.boundary = provider.boundary;
|
|
88
|
+
this.policy = policy;
|
|
89
|
+
}
|
|
90
|
+
async analyze(command, options) {
|
|
91
|
+
return await (this.provider.analyze ?? analyzeShellCommand).call(this.provider, command, options);
|
|
92
|
+
}
|
|
93
|
+
async exec(command, options = {}) {
|
|
94
|
+
if (this.#disposed) return createPolicyObservation(command, options.cwd, shellSessionDisposedMessage);
|
|
95
|
+
if (!options.budgetCharged) options.budget?.recordToolCall(new TextEncoder().encode(command).byteLength);
|
|
96
|
+
if (typeof this.policy.maxShellCalls === "number" && this.#shellCalls >= this.policy.maxShellCalls) return createPolicyObservation(command, options.cwd, `[vitehub] Shell session command budget exhausted after ${this.policy.maxShellCalls} calls. The Shell Session policy limits command calls for this run.`);
|
|
97
|
+
this.#shellCalls += 1;
|
|
98
|
+
const started = Date.now();
|
|
99
|
+
options = limitOutputOptions(options, this.policy);
|
|
100
|
+
const maxOutputLength = options.maxOutputLength;
|
|
101
|
+
const signal = options.budget && options.signal && options.signal !== options.budget.signal ? AbortSignal.any([options.signal, options.budget.signal]) : options.budget?.signal ?? options.signal;
|
|
102
|
+
const result = await this.provider.exec(command, {
|
|
103
|
+
...options,
|
|
104
|
+
env: {
|
|
105
|
+
...this.env,
|
|
106
|
+
...options.env
|
|
107
|
+
},
|
|
108
|
+
maxOutputLength,
|
|
109
|
+
signal,
|
|
110
|
+
timeout: options.timeout ?? this.policy.timeout
|
|
111
|
+
});
|
|
112
|
+
options.budget?.recordOutputBytes(new TextEncoder().encode(result.stdout).byteLength + new TextEncoder().encode(result.stderr).byteLength);
|
|
113
|
+
const timedOut = result.timedOut;
|
|
114
|
+
return applyOutputLimit({
|
|
115
|
+
command: result.command ?? command,
|
|
116
|
+
cwd: result.cwd ?? options.cwd,
|
|
117
|
+
durationMs: result.durationMs ?? Date.now() - started,
|
|
118
|
+
event: timedOut ? "command_timed_out" : result.event,
|
|
119
|
+
exitCode: result.exitCode,
|
|
120
|
+
maxOutputLength: result.maxOutputLength,
|
|
121
|
+
outputTruncated: result.outputTruncated,
|
|
122
|
+
stderr: result.stderr,
|
|
123
|
+
stdout: result.stdout,
|
|
124
|
+
timedOut,
|
|
125
|
+
workspaceGuardrail: result.workspaceGuardrail
|
|
126
|
+
}, maxOutputLength);
|
|
127
|
+
}
|
|
128
|
+
async startProcess(command, options = {}) {
|
|
129
|
+
if (this.#disposed) throw shellErrorDiagnostics.SHELL_R0008({ message: shellSessionDisposedMessage });
|
|
130
|
+
if (!this.provider.startProcess || !this.boundary.processes.background) throw shellErrorDiagnostics.SHELL_R0009({ message: "[vitehub] Shell provider does not support long-running processes." });
|
|
131
|
+
if (!options.budgetCharged) options.budget?.recordToolCall(new TextEncoder().encode(command).byteLength);
|
|
132
|
+
const maxProcesses = this.policy.maxProcesses;
|
|
133
|
+
if (maxProcesses !== void 0 && this.#processes.size + this.#starts.size >= maxProcesses) throw shellErrorDiagnostics.SHELL_R0010({ message: `[vitehub] Shell session process budget exhausted after ${this.policy.maxProcesses} processes.` });
|
|
134
|
+
options = limitOutputOptions(options, this.policy);
|
|
135
|
+
const signal = options.budget && options.signal && options.signal !== options.budget.signal ? AbortSignal.any([options.signal, options.budget.signal]) : options.budget?.signal ?? options.signal;
|
|
136
|
+
let finishStart;
|
|
137
|
+
const start = new Promise((resolve) => {
|
|
138
|
+
finishStart = resolve;
|
|
139
|
+
});
|
|
140
|
+
this.#starts.add(start);
|
|
141
|
+
start.finally(() => this.#starts.delete(start));
|
|
142
|
+
let process;
|
|
143
|
+
try {
|
|
144
|
+
process = await this.provider.startProcess(command, {
|
|
145
|
+
...options,
|
|
146
|
+
signal,
|
|
147
|
+
env: {
|
|
148
|
+
...this.env,
|
|
149
|
+
...options.env
|
|
150
|
+
},
|
|
151
|
+
timeout: options.timeout ?? this.policy.timeout
|
|
152
|
+
});
|
|
153
|
+
} catch (error) {
|
|
154
|
+
finishStart();
|
|
155
|
+
throw error;
|
|
156
|
+
}
|
|
157
|
+
let stopTask;
|
|
158
|
+
let trackedProcess;
|
|
159
|
+
const stop = () => {
|
|
160
|
+
if (stopTask) return stopTask;
|
|
161
|
+
stopTask = Promise.resolve().then(() => process.stop()).then((result) => {
|
|
162
|
+
this.#processes.delete(trackedProcess);
|
|
163
|
+
return applyOutputLimit(result, options.maxOutputLength);
|
|
164
|
+
}, (error) => {
|
|
165
|
+
stopTask = void 0;
|
|
166
|
+
throw error;
|
|
167
|
+
});
|
|
168
|
+
return stopTask;
|
|
169
|
+
};
|
|
170
|
+
trackedProcess = {
|
|
171
|
+
get id() {
|
|
172
|
+
return process.id;
|
|
173
|
+
},
|
|
174
|
+
get command() {
|
|
175
|
+
return process.command;
|
|
176
|
+
},
|
|
177
|
+
get cwd() {
|
|
178
|
+
return process.cwd;
|
|
179
|
+
},
|
|
180
|
+
stop
|
|
181
|
+
};
|
|
182
|
+
this.#processes.add(trackedProcess);
|
|
183
|
+
finishStart();
|
|
184
|
+
if (this.#disposed) {
|
|
185
|
+
try {
|
|
186
|
+
await stop();
|
|
187
|
+
} catch (error) {
|
|
188
|
+
throw new AggregateError([shellErrorDiagnostics.SHELL_R0011({ message: shellSessionDisposedMessage }), error], "[vitehub] Shell session was disposed while starting a background process, and stopping it failed.");
|
|
189
|
+
}
|
|
190
|
+
throw shellErrorDiagnostics.SHELL_R0012({ message: shellSessionDisposedMessage });
|
|
191
|
+
}
|
|
192
|
+
return trackedProcess;
|
|
193
|
+
}
|
|
194
|
+
async listProcesses() {
|
|
195
|
+
return [...this.#processes];
|
|
196
|
+
}
|
|
197
|
+
dispose() {
|
|
198
|
+
this.#disposed = true;
|
|
199
|
+
if (this.#disposeTask) return this.#disposeTask;
|
|
200
|
+
this.#disposeTask = (async () => {
|
|
201
|
+
await Promise.all(this.#starts);
|
|
202
|
+
const failures = [];
|
|
203
|
+
for (const process of [...this.#processes].reverse()) try {
|
|
204
|
+
await process.stop();
|
|
205
|
+
} catch (error) {
|
|
206
|
+
failures.push(error);
|
|
207
|
+
}
|
|
208
|
+
if (failures.length === 1) throw failures[0];
|
|
209
|
+
if (failures.length > 1) throw new AggregateError(failures, "[vitehub] Shell session failed to stop multiple background processes.");
|
|
210
|
+
return {
|
|
211
|
+
event: "session_disposed",
|
|
212
|
+
exitCode: null,
|
|
213
|
+
stderr: "",
|
|
214
|
+
stdout: ""
|
|
215
|
+
};
|
|
216
|
+
})().catch((error) => {
|
|
217
|
+
this.#disposeTask = void 0;
|
|
218
|
+
throw error;
|
|
219
|
+
});
|
|
220
|
+
return this.#disposeTask;
|
|
221
|
+
}
|
|
222
|
+
};
|
|
223
|
+
function createShellRuntime(options) {
|
|
224
|
+
const { provider } = options;
|
|
225
|
+
const policy = { ...options.policy ?? {} };
|
|
226
|
+
return {
|
|
227
|
+
boundary: provider.boundary,
|
|
228
|
+
createSession(sessionOptions = {}) {
|
|
229
|
+
return new RuntimeShellSession(provider, {
|
|
230
|
+
...policy,
|
|
231
|
+
...sessionOptions.policy
|
|
232
|
+
}, sessionOptions.env);
|
|
233
|
+
},
|
|
234
|
+
async exec(command, execOptions = {}) {
|
|
235
|
+
const session = new RuntimeShellSession(provider, policy);
|
|
236
|
+
try {
|
|
237
|
+
return await session.exec(command, execOptions);
|
|
238
|
+
} finally {
|
|
239
|
+
await session.dispose();
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
};
|
|
243
|
+
}
|
|
244
|
+
//#endregion
|
|
245
|
+
export { createShellRuntime as t };
|
|
@@ -1,8 +1,16 @@
|
|
|
1
|
+
import { ExecutionBudget } from "@vite-hub/runtime";
|
|
1
2
|
//#region src/runtime/types.d.ts
|
|
2
3
|
type ShellObservationEvent = "command_finished" | "command_timed_out" | "policy_denied" | "session_disposed";
|
|
3
4
|
interface ShellRuntimeExecOptions {
|
|
5
|
+
budget?: ExecutionBudget;
|
|
6
|
+
/** Set when the caller already charged this command against the budget. */
|
|
7
|
+
budgetCharged?: boolean;
|
|
4
8
|
cwd?: string;
|
|
5
9
|
env?: Record<string, string>;
|
|
10
|
+
/** Abort the command and any provider-owned work, such as a Source request. */
|
|
11
|
+
signal?: AbortSignal;
|
|
12
|
+
/** A provider may enforce this before buffering output. The runtime always enforces its policy limit. */
|
|
13
|
+
maxOutputLength?: number;
|
|
6
14
|
onStderr?: (data: string) => void;
|
|
7
15
|
onStdout?: (data: string) => void;
|
|
8
16
|
stdin?: string;
|
|
@@ -62,11 +70,23 @@ interface ShellBoundary {
|
|
|
62
70
|
enforcedBy: "provider" | "runtime" | "unsupported";
|
|
63
71
|
supported: boolean;
|
|
64
72
|
};
|
|
73
|
+
/** Provider execution details that callers can inspect without knowing the host implementation. */
|
|
74
|
+
execution?: {
|
|
75
|
+
isolation: "in_process" | "process" | "provider_managed" | "unknown";
|
|
76
|
+
model: "virtual" | "host" | "unknown";
|
|
77
|
+
};
|
|
78
|
+
/** Resource controls that this provider can enforce before buffering work. */
|
|
79
|
+
resources?: {
|
|
80
|
+
output: {
|
|
81
|
+
enforcedBy: "provider" | "runtime" | "unsupported";
|
|
82
|
+
unit: "characters";
|
|
83
|
+
};
|
|
84
|
+
};
|
|
65
85
|
}
|
|
66
86
|
interface ShellProcess {
|
|
67
|
-
id: string;
|
|
68
|
-
command: string;
|
|
69
|
-
cwd?: string;
|
|
87
|
+
readonly id: string;
|
|
88
|
+
readonly command: string;
|
|
89
|
+
readonly cwd?: string;
|
|
70
90
|
stop(): Promise<ShellObservation>;
|
|
71
91
|
}
|
|
72
92
|
interface ShellExecutionProvider {
|
package/dist/workspace.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { o as ShellExecutionProvider, s as ShellObservation } from "./types-
|
|
2
|
-
import { r as ShellNetworkGrantExecutor } from "./just-bash-
|
|
1
|
+
import { o as ShellExecutionProvider, s as ShellObservation } from "./types-2uHfTVHi.js";
|
|
2
|
+
import { r as ShellNetworkGrantExecutor } from "./just-bash-DAyZQWLK.js";
|
|
3
|
+
import { ExecutionBudget } from "@vite-hub/runtime";
|
|
3
4
|
import { IFileSystem } from "just-bash";
|
|
4
|
-
|
|
5
5
|
//#region src/workspace/types.d.ts
|
|
6
6
|
type ShellContent = string | Uint8Array;
|
|
7
7
|
interface ShellReadFileOptions {
|
|
@@ -47,7 +47,7 @@ interface SearchableShellWorkspace extends ReadonlyShellWorkspace {
|
|
|
47
47
|
search(query: ShellSearchQuery): Promise<ShellSearchHit[]>;
|
|
48
48
|
}
|
|
49
49
|
interface WritableShellWorkspace extends ReadonlyShellWorkspace {
|
|
50
|
-
writeFile(path: string, content: ShellContent): Promise<
|
|
50
|
+
writeFile(path: string, content: ShellContent): Promise<unknown>;
|
|
51
51
|
mkdir(path: string, options?: ShellMkdirOptions): Promise<void>;
|
|
52
52
|
rm(path: string, options?: ShellRmOptions): Promise<void>;
|
|
53
53
|
}
|
|
@@ -56,12 +56,14 @@ interface WritableShellWorkspace extends ReadonlyShellWorkspace {
|
|
|
56
56
|
interface WorkspaceShellFileSystem extends IFileSystem {
|
|
57
57
|
readonly writeFs: boolean;
|
|
58
58
|
}
|
|
59
|
-
declare const workspaceMountPoint = "/workspace";
|
|
60
|
-
declare function createReadonlyWorkspaceFs(workspace: ReadonlyShellWorkspace): WorkspaceShellFileSystem;
|
|
61
|
-
declare function createWritableWorkspaceFs(workspace: WritableShellWorkspace): WorkspaceShellFileSystem;
|
|
59
|
+
export declare const workspaceMountPoint = "/workspace";
|
|
60
|
+
export declare function createReadonlyWorkspaceFs(workspace: ReadonlyShellWorkspace): WorkspaceShellFileSystem;
|
|
61
|
+
export declare function createWritableWorkspaceFs(workspace: WritableShellWorkspace): WorkspaceShellFileSystem;
|
|
62
62
|
//#endregion
|
|
63
63
|
//#region src/workspace/inspection.d.ts
|
|
64
64
|
interface WorkspaceInspectionCommandOptions {
|
|
65
|
+
budget?: ExecutionBudget;
|
|
66
|
+
budgetCharged?: boolean;
|
|
65
67
|
broadSearchPaths?: string[];
|
|
66
68
|
commands?: string[];
|
|
67
69
|
cwd?: string;
|
|
@@ -69,12 +71,13 @@ interface WorkspaceInspectionCommandOptions {
|
|
|
69
71
|
maxOutputLength?: number;
|
|
70
72
|
networkGrants?: ShellNetworkGrantExecutor;
|
|
71
73
|
provider?: ShellExecutionProvider;
|
|
74
|
+
signal?: AbortSignal;
|
|
72
75
|
timeout?: number;
|
|
73
76
|
}
|
|
74
|
-
declare function runWorkspaceInspectionCommand(_input: SearchableShellWorkspace, command: string, options: WorkspaceInspectionCommandOptions): Promise<ShellObservation>;
|
|
77
|
+
export declare function runWorkspaceInspectionCommand(_input: SearchableShellWorkspace, command: string, options: WorkspaceInspectionCommandOptions): Promise<ShellObservation>;
|
|
75
78
|
//#endregion
|
|
76
79
|
//#region src/workspace/path.d.ts
|
|
77
|
-
declare function cleanWorkspaceShellPath(path?: string): string;
|
|
78
|
-
declare function cleanWorkspaceMutationPath(path: string): string;
|
|
80
|
+
export declare function cleanWorkspaceShellPath(path?: string): string;
|
|
81
|
+
export declare function cleanWorkspaceMutationPath(path: string): string;
|
|
79
82
|
//#endregion
|
|
80
|
-
export {
|
|
83
|
+
export type { ReadonlyShellWorkspace, SearchableShellWorkspace, ShellContent, ShellEntry, ShellListOptions, ShellMkdirOptions, ShellReadFileOptions, ShellRmOptions, ShellSearchHit, ShellSearchQuery, ShellStat, WorkspaceShellFileSystem, WritableShellWorkspace };
|