@demicodes/shell 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.
@@ -0,0 +1,296 @@
1
+ import { a as HostProcess, c as HostSpawnHandle, i as HostFileSystem, l as HostSpawnParams, n as HostDirent, o as HostProcessOutputChunk, r as HostFileStat, s as HostSpawnExit, t as Host, u as HostStore } from "./host-BfjoIvF3.mjs";
2
+ import { HostBackedFileSystem, VirtualFileSystemNode, VirtualFileSystemProvider, virtualDirectory, virtualFile } from "./host-fs.mjs";
3
+ import { _ as isCommandGroup, a as CommandIO, b as runRegisteredCommand, c as CommandOutputSpec, d as CommandRunResult, f as CommandSpec, g as ParsedCommandInput, h as CommandSubcommandSpec, i as CommandExecutionContext, l as CommandRegistry, m as CommandStorage, n as COMMAND_PROMPT_DEFAULTS, o as CommandInputSpec, p as CommandStdin, r as CommandAsset, s as CommandNode, t as AgentSessionCommandStorage, u as CommandRunContext, v as parseCommandInput, y as renderCommandPrompt } from "./storage-DJxxqyZA.mjs";
4
+ import { Interpreter, InterpreterState } from "@demicodes/just-bash/interpreter";
5
+ import { CommandName } from "@demicodes/just-bash/commands";
6
+ import { CommandRegistry as CommandRegistry$1, ExecResult } from "@demicodes/just-bash/types";
7
+
8
+ //#region src/command-projection.d.ts
9
+ /**
10
+ * A registered command leaf described as a structured operation: the second
11
+ * projection of a CommandSpec. The shell invocation (argv + heredoc) and this
12
+ * form share one implementation — `renderScript` produces a script that runs
13
+ * through the normal BashEnvironment path, so audit, command records, and
14
+ * `/@/commands` artifacts behave identically for both projections.
15
+ */
16
+ interface RegisteredCommandOperation {
17
+ /** Path segments from root command to leaf, e.g. ['editor', 'create']. */
18
+ path: string[];
19
+ description: string;
20
+ /** JSON Schema for the operation input, derived from the leaf's zod spec. */
21
+ inputSchema: Record<string, unknown>;
22
+ /**
23
+ * Renders a shell script invoking this operation with the given input.
24
+ * The stdin field (if any) is delivered as a quoted heredoc, which — like
25
+ * any heredoc — normalizes the body to end with a newline.
26
+ */
27
+ renderScript(values: Record<string, unknown>): string;
28
+ }
29
+ declare function listRegisteredCommandOperations(commands: CommandRegistry | CommandSpec[]): RegisteredCommandOperation[];
30
+ //#endregion
31
+ //#region src/environment-state.d.ts
32
+ interface ExecAccumulator {
33
+ stdout: string;
34
+ stderr: string;
35
+ audit: BashAuditEvent[];
36
+ commandMetadata: CommandMetadataRecord[];
37
+ assets: CommandAsset[];
38
+ }
39
+ interface ShellSession {
40
+ id: string;
41
+ commandScopeId: string;
42
+ state: InterpreterState;
43
+ fs: HostBackedFileSystem;
44
+ interpreter: Interpreter;
45
+ forkCommands: CommandRegistry$1;
46
+ accumulator: ExecAccumulator;
47
+ foreground?: ForegroundProcess;
48
+ activeCommandId?: string;
49
+ backgroundJobs: Map<number, BackgroundJob>;
50
+ nextBackgroundJobId: number;
51
+ pendingExec?: Promise<ExecResult | Error>;
52
+ foregroundWaiters: Set<(foreground: ForegroundProcess) => void>;
53
+ exited: boolean;
54
+ abortController?: AbortController;
55
+ }
56
+ interface BackgroundJob {
57
+ id: number;
58
+ command: string;
59
+ args: string[];
60
+ display: string;
61
+ cwd: string;
62
+ handle: HostSpawnHandle;
63
+ stdoutBuffer: string;
64
+ stderrBuffer: string;
65
+ stdoutPump: Promise<void>;
66
+ stderrPump: Promise<void>;
67
+ exitPromise: Promise<{
68
+ exitCode: number | null;
69
+ signal?: string;
70
+ }>;
71
+ }
72
+ interface ForegroundProcess {
73
+ commandId: string;
74
+ command: string;
75
+ args: string[];
76
+ cwd: string;
77
+ handle: HostSpawnHandle;
78
+ startedAt: number;
79
+ lastOutputAt: number;
80
+ /** Everything the process wrote, including redirected output — this is what
81
+ * the interpreter observes as the command's stdout/stderr. */
82
+ rawStdoutBuffer: string;
83
+ rawStderrBuffer: string;
84
+ /** Output routed to the visible sinks only (redirections excluded) — this is
85
+ * what command records and model previews show. */
86
+ stdoutBuffer: string;
87
+ stderrBuffer: string;
88
+ /** Interleaved visible chunks with running byte offsets, for merged replay. */
89
+ outputChunks: ShellOutputRecordChunk[];
90
+ outputBytes: number;
91
+ audit: BashAuditEvent[];
92
+ stdoutPump: Promise<void>;
93
+ stderrPump: Promise<void>;
94
+ exitPromise: Promise<{
95
+ exitCode: number | null;
96
+ signal?: string;
97
+ }>;
98
+ outputSinks: Record<1 | 2, ForegroundSink>;
99
+ abortController: AbortController;
100
+ }
101
+ interface ForegroundSink {
102
+ kind: 'visible' | 'file' | 'null';
103
+ fd?: 1 | 2;
104
+ path?: string;
105
+ append?: boolean;
106
+ bytes: Uint8Array[];
107
+ }
108
+ //#endregion
109
+ //#region src/environment.d.ts
110
+ interface BashEnvironmentOptions {
111
+ host: Host;
112
+ commands?: CommandRegistry;
113
+ shellIdFactory?: () => string;
114
+ commandIdFactory?: () => string;
115
+ initialEnv?: Record<string, string>;
116
+ /**
117
+ * Per-exec env injection for agent-owned shells. Evaluated on every exec with
118
+ * the owning agent session id; returned vars are set and exported, so both
119
+ * registered commands (ctx.env) and spawned external processes observe them.
120
+ * Lets a product harness expose session-scoped context (identity, routing)
121
+ * that changes between execs, which static initialEnv cannot express.
122
+ */
123
+ execEnv?: (agentSessionId: string) => Record<string, string>;
124
+ maxOutputBytes?: number;
125
+ }
126
+ interface ShellExecInput {
127
+ script: string;
128
+ shellId?: string;
129
+ agentSessionId?: string;
130
+ timeoutMs?: number;
131
+ maxOutputBytes?: number;
132
+ signal?: AbortSignal;
133
+ }
134
+ interface ShellStatusInput {
135
+ commandId: string;
136
+ stdoutOffset?: number;
137
+ stderrOffset?: number;
138
+ outputOffset?: number;
139
+ maxOutputBytes?: number;
140
+ }
141
+ interface ShellWriteInput {
142
+ commandId: string;
143
+ stdin: string | Uint8Array;
144
+ maxOutputBytes?: number;
145
+ signal?: AbortSignal;
146
+ }
147
+ interface ShellAbortInput {
148
+ commandId: string;
149
+ maxOutputBytes?: number;
150
+ }
151
+ interface StreamArtifact {
152
+ path: string;
153
+ offset: number;
154
+ delta: string;
155
+ tail: string;
156
+ bytes: number;
157
+ truncated: boolean;
158
+ }
159
+ interface ShellOutputChunk {
160
+ stream: 'stdout' | 'stderr';
161
+ text: string;
162
+ }
163
+ interface ShellOutputRecordChunk extends ShellOutputChunk {
164
+ offset: number;
165
+ bytes: number;
166
+ }
167
+ interface ShellOutputArtifact {
168
+ path: string;
169
+ offset: number;
170
+ text: string;
171
+ tail: string;
172
+ chunks: ShellOutputChunk[];
173
+ bytes: number;
174
+ truncated: boolean;
175
+ }
176
+ type BashAuditEvent = {
177
+ kind: 'registered-command';
178
+ name: string;
179
+ args: string[];
180
+ exitCode: number;
181
+ } | {
182
+ kind: 'portable-command';
183
+ name: string;
184
+ args: string[];
185
+ cwd: string;
186
+ exitCode: number;
187
+ } | {
188
+ kind: 'system-command';
189
+ name: string;
190
+ args: string[];
191
+ cwd: string;
192
+ exitCode: number | null;
193
+ };
194
+ interface CommandMetadataRecord {
195
+ kind: 'registered-command';
196
+ name: string;
197
+ args: string[];
198
+ metadata: unknown;
199
+ }
200
+ type ShellCommandSnapshot = {
201
+ status: 'exited';
202
+ shellId: string;
203
+ commandId: string;
204
+ exitCode: number;
205
+ stdout: StreamArtifact;
206
+ stderr: StreamArtifact;
207
+ output: ShellOutputArtifact;
208
+ runningMs: number;
209
+ idleMs: number;
210
+ audit: BashAuditEvent[];
211
+ commandMetadata?: CommandMetadataRecord[];
212
+ assets?: CommandAsset[];
213
+ } | {
214
+ status: 'running';
215
+ shellId: string;
216
+ commandId: string;
217
+ stdout: StreamArtifact;
218
+ stderr: StreamArtifact;
219
+ output: ShellOutputArtifact;
220
+ runningMs: number;
221
+ idleMs: number;
222
+ } | {
223
+ status: 'aborted';
224
+ shellId: string;
225
+ commandId: string;
226
+ stdout: StreamArtifact;
227
+ stderr: StreamArtifact;
228
+ output: ShellOutputArtifact;
229
+ runningMs: number;
230
+ idleMs: number;
231
+ };
232
+ declare class BashEnvironment {
233
+ private readonly host;
234
+ private readonly commands;
235
+ private readonly shellIdFactory;
236
+ private readonly commandIdFactory;
237
+ private readonly initialEnv;
238
+ private readonly execEnv?;
239
+ private readonly defaultOutputLimitBytes;
240
+ private readonly shells;
241
+ private readonly defaultShellByAgentSessionId;
242
+ private readonly commandsById;
243
+ private readonly artifacts;
244
+ constructor(options: BashEnvironmentOptions);
245
+ getShell(shellId: string): ShellSession | null;
246
+ registerCommand(spec: CommandSpec): void;
247
+ registeredCommands(): CommandSpec[];
248
+ exec(input: ShellExecInput): Promise<ShellCommandSnapshot>;
249
+ status(input: ShellStatusInput): Promise<ShellCommandSnapshot>;
250
+ write(input: ShellWriteInput): Promise<ShellCommandSnapshot>;
251
+ abort(input: ShellAbortInput): Promise<ShellCommandSnapshot>;
252
+ releaseCommand(commandId: string): Promise<boolean>;
253
+ disposeShell(shellId: string): Promise<boolean>;
254
+ disposeAllShells(): Promise<void>;
255
+ private killShell;
256
+ private requireShell;
257
+ private requireCommand;
258
+ private requireForegroundCommand;
259
+ private defaultShell;
260
+ private availableDefaultShell;
261
+ private createShell;
262
+ private runScript;
263
+ private createCommandRecord;
264
+ private startBackgroundJob;
265
+ private listBackgroundJobs;
266
+ private waitForBackgroundJob;
267
+ private exportedEnv;
268
+ private raceForeground;
269
+ private waitForBoundary;
270
+ private hostSpawn;
271
+ private collectExited;
272
+ private finishExited;
273
+ private collectAborted;
274
+ private collectAbortedWithoutForeground;
275
+ private snapshotCommand;
276
+ private lookupVirtualArtifact;
277
+ private commandArtifactIds;
278
+ private commandArtifact;
279
+ private persistCommandArtifact;
280
+ private syncRunningRecord;
281
+ }
282
+ //#endregion
283
+ //#region src/portable-commands.d.ts
284
+ /**
285
+ * Fork portable commands BashEnvironment registers in every shell, so
286
+ * cat/ls/grep-class tools work without local coreutils on any Host backend.
287
+ */
288
+ declare const DEMI_PORTABLE_COMMANDS: readonly CommandName[];
289
+ /**
290
+ * Names registered commands must not shadow, derived from the actual portable
291
+ * command set plus interpreter builtins and pass-through system tools — not a
292
+ * hand-maintained parallel list.
293
+ */
294
+ declare const RESERVED_COMMAND_NAMES: ReadonlySet<string>;
295
+ //#endregion
296
+ export { AgentSessionCommandStorage, BashAuditEvent, BashEnvironment, BashEnvironmentOptions, COMMAND_PROMPT_DEFAULTS, CommandAsset, CommandExecutionContext, CommandIO, CommandInputSpec, CommandMetadataRecord, CommandNode, CommandOutputSpec, CommandRegistry, CommandRunContext, CommandRunResult, CommandSpec, CommandStdin, CommandStorage, CommandSubcommandSpec, DEMI_PORTABLE_COMMANDS, Host, HostBackedFileSystem, HostDirent, HostFileStat, HostFileSystem, HostProcess, HostProcessOutputChunk, HostSpawnExit, HostSpawnHandle, HostSpawnParams, HostStore, ParsedCommandInput, RESERVED_COMMAND_NAMES, RegisteredCommandOperation, ShellAbortInput, ShellCommandSnapshot, ShellExecInput, ShellOutputArtifact, ShellOutputChunk, ShellOutputRecordChunk, ShellStatusInput, ShellWriteInput, StreamArtifact, VirtualFileSystemNode, VirtualFileSystemProvider, isCommandGroup, listRegisteredCommandOperations, parseCommandInput, renderCommandPrompt, runRegisteredCommand, virtualDirectory, virtualFile };