@vite-hub/shell 0.0.0 → 0.0.2

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.
@@ -0,0 +1,178 @@
1
+ import { t as analyzeShellCommand } from "./analyze-Bu5Ueqph.js";
2
+ //#region src/runtime/session.ts
3
+ function applyOutputLimit(result, maxLength) {
4
+ if (!maxLength) return result;
5
+ const next = {
6
+ ...result,
7
+ maxOutputLength: maxLength
8
+ };
9
+ let truncated = false;
10
+ if (next.stdout.length > maxLength) {
11
+ next.stdout = `${next.stdout.slice(0, maxLength)}\n[output truncated to ${maxLength} characters]\n`;
12
+ truncated = true;
13
+ }
14
+ if (next.stderr.length > maxLength) {
15
+ next.stderr = `${next.stderr.slice(0, maxLength)}\n[output truncated to ${maxLength} characters]\n`;
16
+ truncated = true;
17
+ }
18
+ return truncated ? {
19
+ ...next,
20
+ outputTruncated: true
21
+ } : next;
22
+ }
23
+ function createPolicyObservation(command, cwd, message) {
24
+ return {
25
+ command,
26
+ cwd,
27
+ event: "policy_denied",
28
+ exitCode: 126,
29
+ stderr: message.endsWith("\n") ? message : `${message}\n`,
30
+ stdout: ""
31
+ };
32
+ }
33
+ const shellSessionDisposedMessage = "[vitehub] Shell session is disposed.";
34
+ var RuntimeShellSession = class {
35
+ provider;
36
+ env;
37
+ boundary;
38
+ policy;
39
+ #disposed = false;
40
+ #disposeTask;
41
+ #processes = /* @__PURE__ */ new Set();
42
+ #starts = /* @__PURE__ */ new Set();
43
+ #shellCalls = 0;
44
+ constructor(provider, policy, env) {
45
+ this.provider = provider;
46
+ this.env = env;
47
+ this.boundary = provider.boundary;
48
+ this.policy = policy;
49
+ }
50
+ async analyze(command, options) {
51
+ return await (this.provider.analyze ?? analyzeShellCommand)(command, options);
52
+ }
53
+ async exec(command, options = {}) {
54
+ if (this.#disposed) return createPolicyObservation(command, options.cwd, shellSessionDisposedMessage);
55
+ 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.`);
56
+ this.#shellCalls += 1;
57
+ const started = Date.now();
58
+ const result = await this.provider.exec(command, {
59
+ ...options,
60
+ env: {
61
+ ...this.env,
62
+ ...options.env
63
+ },
64
+ timeout: options.timeout ?? this.policy.timeout
65
+ });
66
+ return applyOutputLimit({
67
+ ...result,
68
+ command: result.command ?? command,
69
+ cwd: result.cwd ?? options.cwd,
70
+ durationMs: result.durationMs ?? Date.now() - started,
71
+ event: result.timedOut ? "command_timed_out" : result.event
72
+ }, this.policy.maxOutputLength);
73
+ }
74
+ async startProcess(command, options = {}) {
75
+ if (this.#disposed) throw new Error(shellSessionDisposedMessage);
76
+ if (!this.provider.startProcess || !this.boundary.processes.background) throw new Error("[vitehub] Shell provider does not support long-running processes.");
77
+ if (typeof this.policy.maxProcesses === "number" && this.#processes.size >= this.policy.maxProcesses) throw new Error(`[vitehub] Shell session process budget exhausted after ${this.policy.maxProcesses} processes.`);
78
+ let finishStart;
79
+ const start = new Promise((resolve) => {
80
+ finishStart = resolve;
81
+ });
82
+ this.#starts.add(start);
83
+ start.finally(() => this.#starts.delete(start));
84
+ let process;
85
+ try {
86
+ process = await this.provider.startProcess(command, {
87
+ ...options,
88
+ env: {
89
+ ...this.env,
90
+ ...options.env
91
+ },
92
+ timeout: options.timeout ?? this.policy.timeout
93
+ });
94
+ } catch (error) {
95
+ finishStart();
96
+ throw error;
97
+ }
98
+ let stopTask;
99
+ let trackedProcess;
100
+ const stop = () => {
101
+ if (stopTask) return stopTask;
102
+ stopTask = Promise.resolve().then(() => process.stop()).then((result) => {
103
+ this.#processes.delete(trackedProcess);
104
+ return result;
105
+ }, (error) => {
106
+ stopTask = void 0;
107
+ throw error;
108
+ });
109
+ return stopTask;
110
+ };
111
+ trackedProcess = {
112
+ ...process,
113
+ stop
114
+ };
115
+ this.#processes.add(trackedProcess);
116
+ finishStart();
117
+ if (this.#disposed) {
118
+ try {
119
+ await stop();
120
+ } catch (error) {
121
+ throw new AggregateError([/* @__PURE__ */ new Error(shellSessionDisposedMessage), error], "[vitehub] Shell session was disposed while starting a background process, and stopping it failed.");
122
+ }
123
+ throw new Error(shellSessionDisposedMessage);
124
+ }
125
+ return trackedProcess;
126
+ }
127
+ async listProcesses() {
128
+ return [...this.#processes];
129
+ }
130
+ dispose() {
131
+ this.#disposed = true;
132
+ if (this.#disposeTask) return this.#disposeTask;
133
+ this.#disposeTask = (async () => {
134
+ await Promise.all(this.#starts);
135
+ const failures = [];
136
+ for (const process of [...this.#processes].reverse()) try {
137
+ await process.stop();
138
+ } catch (error) {
139
+ failures.push(error);
140
+ }
141
+ if (failures.length === 1) throw failures[0];
142
+ if (failures.length > 1) throw new AggregateError(failures, "[vitehub] Shell session failed to stop multiple background processes.");
143
+ return {
144
+ event: "session_disposed",
145
+ exitCode: null,
146
+ stderr: "",
147
+ stdout: ""
148
+ };
149
+ })().catch((error) => {
150
+ this.#disposeTask = void 0;
151
+ throw error;
152
+ });
153
+ return this.#disposeTask;
154
+ }
155
+ };
156
+ function createShellRuntime(options) {
157
+ const { provider } = options;
158
+ const policy = options.policy ?? {};
159
+ return {
160
+ boundary: provider.boundary,
161
+ createSession(sessionOptions = {}) {
162
+ return new RuntimeShellSession(provider, {
163
+ ...policy,
164
+ ...sessionOptions.policy
165
+ }, sessionOptions.env);
166
+ },
167
+ async exec(command, execOptions = {}) {
168
+ const session = new RuntimeShellSession(provider, policy);
169
+ try {
170
+ return await session.exec(command, execOptions);
171
+ } finally {
172
+ await session.dispose();
173
+ }
174
+ }
175
+ };
176
+ }
177
+ //#endregion
178
+ export { createShellRuntime as t };
@@ -0,0 +1,101 @@
1
+ //#region src/runtime/types.d.ts
2
+ type ShellObservationEvent = "command_finished" | "command_timed_out" | "policy_denied" | "session_disposed";
3
+ interface ShellRuntimeExecOptions {
4
+ cwd?: string;
5
+ env?: Record<string, string>;
6
+ onStderr?: (data: string) => void;
7
+ onStdout?: (data: string) => void;
8
+ stdin?: string;
9
+ timeout?: number;
10
+ workspacePaths?: string[];
11
+ }
12
+ interface ShellObservation {
13
+ command?: string;
14
+ cwd?: string;
15
+ durationMs?: number;
16
+ event: ShellObservationEvent;
17
+ exitCode: number | null;
18
+ maxOutputLength?: number;
19
+ outputTruncated?: boolean;
20
+ stderr: string;
21
+ stdout: string;
22
+ timedOut?: boolean;
23
+ workspaceGuardrail?: {
24
+ kind: "broad_search" | "missing_path" | "no_match" | "timeout";
25
+ path?: string;
26
+ };
27
+ }
28
+ interface ShellAnalyzeOptions {
29
+ maxInputBytes?: number;
30
+ timeoutMs?: number;
31
+ }
32
+ interface ShellAnalyzeResult {
33
+ commands?: string[];
34
+ error?: string;
35
+ hasCommandSubstitution?: boolean;
36
+ hasHeredocs?: boolean;
37
+ hasPipelines?: boolean;
38
+ hasRedirects?: boolean;
39
+ ok: boolean;
40
+ parser: "sh-syntax";
41
+ }
42
+ interface ShellSessionPolicy {
43
+ maxOutputLength?: number;
44
+ maxShellCalls?: number;
45
+ maxProcesses?: number;
46
+ timeout?: number;
47
+ }
48
+ interface ShellBoundary {
49
+ cwd: boolean;
50
+ env: boolean;
51
+ filesystem: {
52
+ mountPoint?: string;
53
+ writable: boolean;
54
+ };
55
+ network: boolean | "unknown";
56
+ processes: {
57
+ background: boolean;
58
+ interactive: boolean;
59
+ };
60
+ streaming: boolean;
61
+ timeout: {
62
+ enforcedBy: "provider" | "runtime" | "unsupported";
63
+ supported: boolean;
64
+ };
65
+ }
66
+ interface ShellProcess {
67
+ id: string;
68
+ command: string;
69
+ cwd?: string;
70
+ stop(): Promise<ShellObservation>;
71
+ }
72
+ interface ShellExecutionProvider {
73
+ analyze?: (command: string, options?: ShellAnalyzeOptions) => Promise<ShellAnalyzeResult>;
74
+ boundary: ShellBoundary;
75
+ exec(command: string, options?: ShellRuntimeExecOptions): Promise<ShellObservation>;
76
+ startProcess?: (command: string, options?: ShellRuntimeExecOptions) => Promise<ShellProcess>;
77
+ }
78
+ interface ShellSession {
79
+ analyze(command: string, options?: ShellAnalyzeOptions): Promise<ShellAnalyzeResult>;
80
+ boundary: ShellBoundary;
81
+ dispose(): Promise<ShellObservation>;
82
+ exec(command: string, options?: ShellRuntimeExecOptions): Promise<ShellObservation>;
83
+ listProcesses(): Promise<ShellProcess[]>;
84
+ policy: ShellSessionPolicy;
85
+ startProcess(command: string, options?: ShellRuntimeExecOptions): Promise<ShellProcess>;
86
+ }
87
+ interface ShellRuntime {
88
+ boundary: ShellBoundary;
89
+ createSession(options?: CreateShellSessionOptions): ShellSession;
90
+ exec(command: string, options?: ShellRuntimeExecOptions): Promise<ShellObservation>;
91
+ }
92
+ interface CreateShellRuntimeOptions {
93
+ provider: ShellExecutionProvider;
94
+ policy?: ShellSessionPolicy;
95
+ }
96
+ interface CreateShellSessionOptions {
97
+ env?: Record<string, string>;
98
+ policy?: ShellSessionPolicy;
99
+ }
100
+ //#endregion
101
+ export { ShellBoundary as a, ShellObservationEvent as c, ShellRuntimeExecOptions as d, ShellSession as f, ShellAnalyzeResult as i, ShellProcess as l, CreateShellSessionOptions as n, ShellExecutionProvider as o, ShellSessionPolicy as p, ShellAnalyzeOptions as r, ShellObservation as s, CreateShellRuntimeOptions as t, ShellRuntime as u };
@@ -0,0 +1,80 @@
1
+ import { o as ShellExecutionProvider, s as ShellObservation } from "./types-CNRdehmi.js";
2
+ import { r as ShellNetworkGrantExecutor } from "./just-bash-D84udCFy.js";
3
+ import { IFileSystem } from "just-bash";
4
+
5
+ //#region src/workspace/types.d.ts
6
+ type ShellContent = string | Uint8Array;
7
+ interface ShellReadFileOptions {
8
+ encoding?: "utf8" | "binary";
9
+ }
10
+ interface ShellListOptions {
11
+ recursive?: boolean;
12
+ }
13
+ interface ShellMkdirOptions {
14
+ recursive?: boolean;
15
+ }
16
+ interface ShellRmOptions {
17
+ force?: boolean;
18
+ recursive?: boolean;
19
+ }
20
+ interface ShellEntry {
21
+ path: string;
22
+ type: "file" | "directory";
23
+ size?: number;
24
+ }
25
+ interface ShellStat extends ShellEntry {}
26
+ interface ShellSearchQuery {
27
+ pattern: string;
28
+ cwd?: string;
29
+ paths?: string[];
30
+ regex?: boolean;
31
+ caseSensitive?: boolean;
32
+ limit?: number;
33
+ }
34
+ interface ShellSearchHit {
35
+ path: string;
36
+ line: number;
37
+ column: number;
38
+ text: string;
39
+ }
40
+ interface ReadonlyShellWorkspace {
41
+ readFile(path: string, options?: ShellReadFileOptions): Promise<string | Uint8Array>;
42
+ exists(path: string): Promise<boolean>;
43
+ stat(path: string): Promise<ShellStat>;
44
+ list(path?: string, options?: ShellListOptions): Promise<ShellEntry[]>;
45
+ }
46
+ interface SearchableShellWorkspace extends ReadonlyShellWorkspace {
47
+ search(query: ShellSearchQuery): Promise<ShellSearchHit[]>;
48
+ }
49
+ interface WritableShellWorkspace extends ReadonlyShellWorkspace {
50
+ writeFile(path: string, content: ShellContent): Promise<void>;
51
+ mkdir(path: string, options?: ShellMkdirOptions): Promise<void>;
52
+ rm(path: string, options?: ShellRmOptions): Promise<void>;
53
+ }
54
+ //#endregion
55
+ //#region src/workspace/filesystem.d.ts
56
+ interface WorkspaceShellFileSystem extends IFileSystem {
57
+ readonly writeFs: boolean;
58
+ }
59
+ declare const workspaceMountPoint = "/workspace";
60
+ declare function createReadonlyWorkspaceFs(workspace: ReadonlyShellWorkspace): WorkspaceShellFileSystem;
61
+ declare function createWritableWorkspaceFs(workspace: WritableShellWorkspace): WorkspaceShellFileSystem;
62
+ //#endregion
63
+ //#region src/workspace/inspection.d.ts
64
+ interface WorkspaceInspectionCommandOptions {
65
+ broadSearchPaths?: string[];
66
+ commands?: string[];
67
+ cwd?: string;
68
+ fs: WorkspaceShellFileSystem;
69
+ maxOutputLength?: number;
70
+ networkGrants?: ShellNetworkGrantExecutor;
71
+ provider?: ShellExecutionProvider;
72
+ timeout?: number;
73
+ }
74
+ declare function runWorkspaceInspectionCommand(_input: SearchableShellWorkspace, command: string, options: WorkspaceInspectionCommandOptions): Promise<ShellObservation>;
75
+ //#endregion
76
+ //#region src/workspace/path.d.ts
77
+ declare function cleanWorkspaceShellPath(path?: string): string;
78
+ declare function cleanWorkspaceMutationPath(path: string): string;
79
+ //#endregion
80
+ export { type ReadonlyShellWorkspace, type SearchableShellWorkspace, type ShellContent, type ShellEntry, type ShellListOptions, type ShellMkdirOptions, type ShellReadFileOptions, type ShellRmOptions, type ShellSearchHit, type ShellSearchQuery, type ShellStat, type WorkspaceShellFileSystem, type WritableShellWorkspace, cleanWorkspaceMutationPath, cleanWorkspaceShellPath, createReadonlyWorkspaceFs, createWritableWorkspaceFs, runWorkspaceInspectionCommand, workspaceMountPoint };