@agentuity/core 0.0.104 → 0.0.106

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,542 @@
1
+ import { StructuredError } from '../error';
2
+
3
+ /**
4
+ * Resource limits for a sandbox using Kubernetes-style units
5
+ */
6
+ export interface SandboxResources {
7
+ /**
8
+ * Memory limit (e.g., "500Mi", "1Gi")
9
+ */
10
+ memory?: string;
11
+
12
+ /**
13
+ * CPU limit in millicores (e.g., "500m", "1000m")
14
+ */
15
+ cpu?: string;
16
+
17
+ /**
18
+ * Disk limit (e.g., "500Mi", "1Gi")
19
+ */
20
+ disk?: string;
21
+ }
22
+
23
+ /**
24
+ * Sandbox status
25
+ */
26
+ export type SandboxStatus = 'creating' | 'idle' | 'running' | 'terminated' | 'failed';
27
+
28
+ /**
29
+ * Execution status
30
+ */
31
+ export type ExecutionStatus =
32
+ | 'queued'
33
+ | 'running'
34
+ | 'completed'
35
+ | 'failed'
36
+ | 'timeout'
37
+ | 'cancelled';
38
+
39
+ /**
40
+ * Read-only stream interface for consuming streams without write access
41
+ */
42
+ export interface StreamReader {
43
+ /**
44
+ * Unique stream identifier
45
+ */
46
+ id: string;
47
+
48
+ /**
49
+ * Public URL to access the stream
50
+ */
51
+ url: string;
52
+
53
+ /**
54
+ * Indicates this is a read-only stream
55
+ */
56
+ readonly: true;
57
+
58
+ /**
59
+ * Get a ReadableStream that streams from the URL
60
+ *
61
+ * @returns a ReadableStream that can be consumed
62
+ */
63
+ getReader(): ReadableStream<Uint8Array>;
64
+ }
65
+
66
+ /**
67
+ * Stream configuration for sandbox output
68
+ */
69
+ export interface SandboxStreamConfig {
70
+ /**
71
+ * Stream ID for stdout (or "ignore" to discard)
72
+ */
73
+ stdout?: string;
74
+
75
+ /**
76
+ * Stream ID for stderr (or "ignore" to discard)
77
+ */
78
+ stderr?: string;
79
+
80
+ /**
81
+ * Stream ID for stdin input
82
+ */
83
+ stdin?: string;
84
+
85
+ /**
86
+ * Include timestamps in output (default: true)
87
+ */
88
+ timestamps?: boolean;
89
+ }
90
+
91
+ /**
92
+ * Command to execute in a sandbox
93
+ */
94
+ export interface SandboxCommand {
95
+ /**
96
+ * Command and arguments to execute
97
+ */
98
+ exec: string[];
99
+
100
+ /**
101
+ * Files to create before execution
102
+ */
103
+ files?: FileToWrite[];
104
+
105
+ /**
106
+ * Execution mode: "oneshot" auto-destroys sandbox on exit
107
+ */
108
+ mode?: 'oneshot' | 'interactive';
109
+ }
110
+
111
+ /**
112
+ * Network configuration for sandbox
113
+ */
114
+ export interface SandboxNetworkConfig {
115
+ /**
116
+ * Whether to enable outbound network access (default: false)
117
+ */
118
+ enabled?: boolean;
119
+ }
120
+
121
+ /**
122
+ * Timeout configuration for sandbox
123
+ */
124
+ export interface SandboxTimeoutConfig {
125
+ /**
126
+ * Idle timeout before sandbox is reaped (e.g., "10m", "1h")
127
+ */
128
+ idle?: string;
129
+
130
+ /**
131
+ * Maximum execution time per command (e.g., "5m", "1h")
132
+ */
133
+ execution?: string;
134
+ }
135
+
136
+ /**
137
+ * Options for creating a sandbox
138
+ */
139
+ export interface SandboxCreateOptions {
140
+ /**
141
+ * Resource limits
142
+ */
143
+ resources?: SandboxResources;
144
+
145
+ /**
146
+ * Environment variables
147
+ */
148
+ env?: Record<string, string>;
149
+
150
+ /**
151
+ * Network configuration
152
+ */
153
+ network?: SandboxNetworkConfig;
154
+
155
+ /**
156
+ * Stream configuration for output
157
+ */
158
+ stream?: SandboxStreamConfig;
159
+
160
+ /**
161
+ * Timeout configuration
162
+ */
163
+ timeout?: SandboxTimeoutConfig;
164
+
165
+ /**
166
+ * Command to execute (if provided, creates a sandbox with initial execution)
167
+ */
168
+ command?: SandboxCommand;
169
+
170
+ /**
171
+ * Snapshot ID or tag to restore from when creating the sandbox.
172
+ * The sandbox will start with the filesystem state from the snapshot.
173
+ */
174
+ snapshot?: string;
175
+
176
+ /**
177
+ * Apt packages to install when creating the sandbox.
178
+ * These are installed via `apt install` before executing any commands.
179
+ */
180
+ dependencies?: string[];
181
+ }
182
+
183
+ /**
184
+ * A sandbox instance with methods for interaction
185
+ */
186
+ export interface Sandbox {
187
+ /**
188
+ * Unique sandbox identifier
189
+ */
190
+ id: string;
191
+
192
+ /**
193
+ * Current status
194
+ */
195
+ status: SandboxStatus;
196
+
197
+ /**
198
+ * Read-only stream for stdout.
199
+ * When no separate streams are configured, stdout and stderr point to the same
200
+ * combined stream with interleaved output.
201
+ */
202
+ stdout: StreamReader;
203
+
204
+ /**
205
+ * Read-only stream for stderr.
206
+ * When no separate streams are configured, stdout and stderr point to the same
207
+ * combined stream with interleaved output.
208
+ */
209
+ stderr: StreamReader;
210
+
211
+ /**
212
+ * True if stdout and stderr are using the same stream (interleaved output).
213
+ * When true, reading from stdout or stderr will return the same interleaved data.
214
+ */
215
+ interleaved: boolean;
216
+
217
+ /**
218
+ * Execute a command in the sandbox
219
+ */
220
+ execute(options: ExecuteOptions): Promise<Execution>;
221
+
222
+ /**
223
+ * Write files to the sandbox workspace.
224
+ *
225
+ * @param files - Array of FileToWrite objects with path and Buffer content
226
+ */
227
+ writeFiles(files: FileToWrite[]): Promise<void>;
228
+
229
+ /**
230
+ * Read a file from the sandbox workspace.
231
+ * Returns a ReadableStream for efficient streaming of large files.
232
+ *
233
+ * @param path - Path to the file relative to the sandbox workspace
234
+ * @returns ReadableStream of the file contents
235
+ */
236
+ readFile(path: string): Promise<ReadableStream<Uint8Array>>;
237
+
238
+ /**
239
+ * Destroy the sandbox
240
+ */
241
+ destroy(): Promise<void>;
242
+ }
243
+
244
+ /**
245
+ * Represents a file to write to the sandbox
246
+ */
247
+ export interface FileToWrite {
248
+ /**
249
+ * Path to the file relative to the sandbox workspace
250
+ */
251
+ path: string;
252
+
253
+ /**
254
+ * File content as a Buffer
255
+ */
256
+ content: Buffer;
257
+ }
258
+
259
+ /**
260
+ * Information about a sandbox
261
+ */
262
+ export interface SandboxInfo {
263
+ /**
264
+ * Unique sandbox identifier
265
+ */
266
+ sandboxId: string;
267
+
268
+ /**
269
+ * Current status
270
+ */
271
+ status: SandboxStatus;
272
+
273
+ /**
274
+ * Creation timestamp (ISO 8601)
275
+ */
276
+ createdAt: string;
277
+
278
+ /**
279
+ * Region where the sandbox is running
280
+ */
281
+ region?: string;
282
+
283
+ /**
284
+ * Snapshot ID this sandbox was created from
285
+ */
286
+ snapshotId?: string;
287
+
288
+ /**
289
+ * Snapshot tag this sandbox was created from (if the snapshot had a tag)
290
+ */
291
+ snapshotTag?: string;
292
+
293
+ /**
294
+ * Number of executions run in this sandbox
295
+ */
296
+ executions: number;
297
+
298
+ /**
299
+ * URL to the stdout output stream
300
+ */
301
+ stdoutStreamUrl?: string;
302
+
303
+ /**
304
+ * URL to the stderr output stream
305
+ */
306
+ stderrStreamUrl?: string;
307
+
308
+ /**
309
+ * Apt packages installed in the sandbox
310
+ */
311
+ dependencies?: string[];
312
+ }
313
+
314
+ /**
315
+ * Parameters for listing sandboxes
316
+ */
317
+ export interface ListSandboxesParams {
318
+ /**
319
+ * Filter by project ID
320
+ */
321
+ projectId?: string;
322
+
323
+ /**
324
+ * Filter by snapshot ID
325
+ */
326
+ snapshotId?: string;
327
+
328
+ /**
329
+ * Filter by status
330
+ */
331
+ status?: SandboxStatus;
332
+
333
+ /**
334
+ * Maximum number of results (default: 50, max: 100)
335
+ */
336
+ limit?: number;
337
+
338
+ /**
339
+ * Pagination offset
340
+ */
341
+ offset?: number;
342
+ }
343
+
344
+ /**
345
+ * Response from listing sandboxes
346
+ */
347
+ export interface ListSandboxesResponse {
348
+ /**
349
+ * Array of sandbox information
350
+ */
351
+ sandboxes: SandboxInfo[];
352
+
353
+ /**
354
+ * Total count of sandboxes matching the filter
355
+ */
356
+ total: number;
357
+ }
358
+
359
+ /**
360
+ * Options for executing a command in a sandbox
361
+ */
362
+ export interface ExecuteOptions {
363
+ /**
364
+ * Command and arguments to execute
365
+ */
366
+ command: string[];
367
+
368
+ /**
369
+ * Files to create/update before execution
370
+ */
371
+ files?: FileToWrite[];
372
+
373
+ /**
374
+ * Execution timeout (e.g., "5m")
375
+ */
376
+ timeout?: string;
377
+
378
+ /**
379
+ * Stream configuration (can override sandbox defaults)
380
+ */
381
+ stream?: {
382
+ stdout?: string;
383
+ stderr?: string;
384
+ timestamps?: boolean;
385
+ };
386
+
387
+ /**
388
+ * AbortSignal to cancel the operation
389
+ */
390
+ signal?: AbortSignal;
391
+ }
392
+
393
+ /**
394
+ * An execution instance
395
+ */
396
+ export interface Execution {
397
+ /**
398
+ * Unique execution identifier
399
+ */
400
+ executionId: string;
401
+
402
+ /**
403
+ * Current status
404
+ */
405
+ status: ExecutionStatus;
406
+
407
+ /**
408
+ * Exit code (set when completed or failed)
409
+ */
410
+ exitCode?: number;
411
+
412
+ /**
413
+ * Duration in milliseconds (set when completed)
414
+ */
415
+ durationMs?: number;
416
+
417
+ /**
418
+ * URL to stream stdout output for this execution
419
+ */
420
+ stdoutStreamUrl?: string;
421
+
422
+ /**
423
+ * URL to stream stderr output for this execution
424
+ */
425
+ stderrStreamUrl?: string;
426
+ }
427
+
428
+ /**
429
+ * Options for one-shot sandbox execution
430
+ */
431
+ export interface SandboxRunOptions extends Omit<SandboxCreateOptions, 'command'> {
432
+ /**
433
+ * Command to execute (required for run)
434
+ */
435
+ command: {
436
+ exec: string[];
437
+ files?: FileToWrite[];
438
+ };
439
+ }
440
+
441
+ /**
442
+ * Result from one-shot sandbox execution
443
+ */
444
+ export interface SandboxRunResult {
445
+ /**
446
+ * Sandbox ID
447
+ */
448
+ sandboxId: string;
449
+
450
+ /**
451
+ * Exit code from the process
452
+ */
453
+ exitCode: number;
454
+
455
+ /**
456
+ * Duration in milliseconds
457
+ */
458
+ durationMs: number;
459
+
460
+ /**
461
+ * Stdout content (if captured)
462
+ */
463
+ stdout?: string;
464
+
465
+ /**
466
+ * Stderr content (if captured)
467
+ */
468
+ stderr?: string;
469
+ }
470
+
471
+ /**
472
+ * Sandbox service for creating and managing isolated execution environments
473
+ */
474
+ export interface SandboxService {
475
+ /**
476
+ * Run a one-shot command in a new sandbox (creates, executes, destroys)
477
+ *
478
+ * @param options - execution options
479
+ * @returns result with exit code and optional output
480
+ *
481
+ * @example
482
+ * ```typescript
483
+ * const result = await ctx.sandbox.run({
484
+ * command: {
485
+ * exec: ['bun', 'run', 'index.ts'],
486
+ * files: [{ path: 'index.ts', content: Buffer.from('console.log("hello")') }]
487
+ * }
488
+ * });
489
+ * console.log('Exit:', result.exitCode);
490
+ * ```
491
+ */
492
+ run(options: SandboxRunOptions): Promise<SandboxRunResult>;
493
+
494
+ /**
495
+ * Create an interactive sandbox for multiple executions
496
+ *
497
+ * @param options - sandbox configuration
498
+ * @returns sandbox instance
499
+ *
500
+ * @example
501
+ * ```typescript
502
+ * const sandbox = await ctx.sandbox.create({
503
+ * resources: { memory: '1Gi', cpu: '1000m' }
504
+ * });
505
+ * await sandbox.execute({ command: ['bun', 'init'] });
506
+ * await sandbox.execute({ command: ['bun', 'add', 'zod'] });
507
+ * await sandbox.destroy();
508
+ * ```
509
+ */
510
+ create(options?: SandboxCreateOptions): Promise<Sandbox>;
511
+
512
+ /**
513
+ * Get sandbox information by ID
514
+ *
515
+ * @param sandboxId - sandbox identifier
516
+ * @returns sandbox information
517
+ */
518
+ get(sandboxId: string): Promise<SandboxInfo>;
519
+
520
+ /**
521
+ * List sandboxes with optional filtering
522
+ *
523
+ * @param params - filter and pagination parameters
524
+ * @returns list of sandboxes
525
+ */
526
+ list(params?: ListSandboxesParams): Promise<ListSandboxesResponse>;
527
+
528
+ /**
529
+ * Destroy a sandbox by ID
530
+ *
531
+ * @param sandboxId - sandbox identifier
532
+ */
533
+ destroy(sandboxId: string): Promise<void>;
534
+ }
535
+
536
+ /**
537
+ * Structured error for sandbox operations
538
+ */
539
+ export const SandboxError = StructuredError('SandboxError')<{
540
+ sandboxId?: string;
541
+ executionId?: string;
542
+ }>();