@agentuity/server 0.1.15 → 0.1.17

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.
Files changed (113) hide show
  1. package/dist/api/api.d.ts +11 -6
  2. package/dist/api/api.d.ts.map +1 -1
  3. package/dist/api/api.js +19 -12
  4. package/dist/api/api.js.map +1 -1
  5. package/dist/api/index.d.ts +1 -0
  6. package/dist/api/index.d.ts.map +1 -1
  7. package/dist/api/index.js +1 -0
  8. package/dist/api/index.js.map +1 -1
  9. package/dist/api/org/env-delete.d.ts +16 -0
  10. package/dist/api/org/env-delete.d.ts.map +1 -0
  11. package/dist/api/org/env-delete.js +25 -0
  12. package/dist/api/org/env-delete.js.map +1 -0
  13. package/dist/api/org/env-get.d.ts +20 -0
  14. package/dist/api/org/env-get.d.ts.map +1 -0
  15. package/dist/api/org/env-get.js +26 -0
  16. package/dist/api/org/env-get.js.map +1 -0
  17. package/dist/api/org/env-update.d.ts +17 -0
  18. package/dist/api/org/env-update.d.ts.map +1 -0
  19. package/dist/api/org/env-update.js +26 -0
  20. package/dist/api/org/env-update.js.map +1 -0
  21. package/dist/api/org/index.d.ts +3 -0
  22. package/dist/api/org/index.d.ts.map +1 -1
  23. package/dist/api/org/index.js +3 -0
  24. package/dist/api/org/index.js.map +1 -1
  25. package/dist/api/queue/analytics.d.ts +108 -0
  26. package/dist/api/queue/analytics.d.ts.map +1 -0
  27. package/dist/api/queue/analytics.js +245 -0
  28. package/dist/api/queue/analytics.js.map +1 -0
  29. package/dist/api/queue/destinations.d.ts +108 -0
  30. package/dist/api/queue/destinations.d.ts.map +1 -0
  31. package/dist/api/queue/destinations.js +238 -0
  32. package/dist/api/queue/destinations.js.map +1 -0
  33. package/dist/api/queue/dlq.d.ts +100 -0
  34. package/dist/api/queue/dlq.d.ts.map +1 -0
  35. package/dist/api/queue/dlq.js +204 -0
  36. package/dist/api/queue/dlq.js.map +1 -0
  37. package/dist/api/queue/index.d.ts +55 -0
  38. package/dist/api/queue/index.d.ts.map +1 -0
  39. package/dist/api/queue/index.js +86 -0
  40. package/dist/api/queue/index.js.map +1 -0
  41. package/dist/api/queue/messages.d.ts +332 -0
  42. package/dist/api/queue/messages.d.ts.map +1 -0
  43. package/dist/api/queue/messages.js +637 -0
  44. package/dist/api/queue/messages.js.map +1 -0
  45. package/dist/api/queue/queues.d.ts +153 -0
  46. package/dist/api/queue/queues.d.ts.map +1 -0
  47. package/dist/api/queue/queues.js +319 -0
  48. package/dist/api/queue/queues.js.map +1 -0
  49. package/dist/api/queue/sources.d.ts +132 -0
  50. package/dist/api/queue/sources.d.ts.map +1 -0
  51. package/dist/api/queue/sources.js +285 -0
  52. package/dist/api/queue/sources.js.map +1 -0
  53. package/dist/api/queue/types.d.ts +1129 -0
  54. package/dist/api/queue/types.d.ts.map +1 -0
  55. package/dist/api/queue/types.js +949 -0
  56. package/dist/api/queue/types.js.map +1 -0
  57. package/dist/api/queue/util.d.ts +262 -0
  58. package/dist/api/queue/util.d.ts.map +1 -0
  59. package/dist/api/queue/util.js +171 -0
  60. package/dist/api/queue/util.js.map +1 -0
  61. package/dist/api/queue/validation.d.ts +247 -0
  62. package/dist/api/queue/validation.d.ts.map +1 -0
  63. package/dist/api/queue/validation.js +513 -0
  64. package/dist/api/queue/validation.js.map +1 -0
  65. package/dist/api/sandbox/client.d.ts +56 -2
  66. package/dist/api/sandbox/client.d.ts.map +1 -1
  67. package/dist/api/sandbox/client.js +51 -0
  68. package/dist/api/sandbox/client.js.map +1 -1
  69. package/dist/api/sandbox/get.d.ts.map +1 -1
  70. package/dist/api/sandbox/get.js +5 -0
  71. package/dist/api/sandbox/get.js.map +1 -1
  72. package/dist/api/sandbox/index.d.ts +3 -3
  73. package/dist/api/sandbox/index.d.ts.map +1 -1
  74. package/dist/api/sandbox/index.js +1 -1
  75. package/dist/api/sandbox/index.js.map +1 -1
  76. package/dist/api/sandbox/run.d.ts.map +1 -1
  77. package/dist/api/sandbox/run.js +5 -2
  78. package/dist/api/sandbox/run.js.map +1 -1
  79. package/dist/api/sandbox/runtime.d.ts.map +1 -1
  80. package/dist/api/sandbox/runtime.js +14 -0
  81. package/dist/api/sandbox/runtime.js.map +1 -1
  82. package/dist/api/sandbox/snapshot-build.d.ts +2 -0
  83. package/dist/api/sandbox/snapshot-build.d.ts.map +1 -1
  84. package/dist/api/sandbox/snapshot-build.js +4 -0
  85. package/dist/api/sandbox/snapshot-build.js.map +1 -1
  86. package/dist/api/sandbox/snapshot.d.ts +44 -1
  87. package/dist/api/sandbox/snapshot.d.ts.map +1 -1
  88. package/dist/api/sandbox/snapshot.js +77 -4
  89. package/dist/api/sandbox/snapshot.js.map +1 -1
  90. package/package.json +4 -4
  91. package/src/api/api.ts +62 -13
  92. package/src/api/index.ts +1 -0
  93. package/src/api/org/env-delete.ts +37 -0
  94. package/src/api/org/env-get.ts +43 -0
  95. package/src/api/org/env-update.ts +38 -0
  96. package/src/api/org/index.ts +3 -0
  97. package/src/api/queue/analytics.ts +313 -0
  98. package/src/api/queue/destinations.ts +321 -0
  99. package/src/api/queue/dlq.ts +283 -0
  100. package/src/api/queue/index.ts +261 -0
  101. package/src/api/queue/messages.ts +875 -0
  102. package/src/api/queue/queues.ts +448 -0
  103. package/src/api/queue/sources.ts +384 -0
  104. package/src/api/queue/types.ts +1253 -0
  105. package/src/api/queue/util.ts +204 -0
  106. package/src/api/queue/validation.ts +560 -0
  107. package/src/api/sandbox/client.ts +86 -1
  108. package/src/api/sandbox/get.ts +5 -0
  109. package/src/api/sandbox/index.ts +9 -1
  110. package/src/api/sandbox/run.ts +5 -2
  111. package/src/api/sandbox/runtime.ts +15 -0
  112. package/src/api/sandbox/snapshot-build.ts +4 -0
  113. package/src/api/sandbox/snapshot.ts +96 -5
@@ -0,0 +1,560 @@
1
+ /**
2
+ * @module validation
3
+ * Queue validation utilities with constants matching the Catalyst backend.
4
+ *
5
+ * These validation functions perform client-side validation before API calls,
6
+ * providing immediate feedback and reducing unnecessary network requests.
7
+ */
8
+
9
+ import { StructuredError } from '@agentuity/core';
10
+
11
+ // ============================================================================
12
+ // Validation Constants
13
+ // ============================================================================
14
+
15
+ /** Maximum allowed length for queue names. */
16
+ export const MAX_QUEUE_NAME_LENGTH = 256;
17
+
18
+ /** Minimum allowed length for queue names. */
19
+ export const MIN_QUEUE_NAME_LENGTH = 1;
20
+
21
+ /** Maximum payload size in bytes (1MB). */
22
+ export const MAX_PAYLOAD_SIZE = 1048576;
23
+
24
+ /** Maximum description length in characters. */
25
+ export const MAX_DESCRIPTION_LENGTH = 1024;
26
+
27
+ /** Maximum number of messages in a single batch operation. */
28
+ export const MAX_BATCH_SIZE = 1000;
29
+
30
+ /** Maximum metadata size in bytes (64KB). */
31
+ export const MAX_METADATA_SIZE = 65536;
32
+
33
+ /** Maximum partition key length in characters. */
34
+ export const MAX_PARTITION_KEY_LENGTH = 256;
35
+
36
+ /** Maximum idempotency key length in characters. */
37
+ export const MAX_IDEMPOTENCY_KEY_LENGTH = 256;
38
+
39
+ /** Maximum visibility timeout in seconds (12 hours). */
40
+ export const MAX_VISIBILITY_TIMEOUT = 43200;
41
+
42
+ /** Maximum number of retry attempts allowed. */
43
+ export const MAX_RETRIES = 100;
44
+
45
+ /** Maximum number of in-flight messages per client. */
46
+ export const MAX_IN_FLIGHT = 1000;
47
+
48
+ /** Queue name pattern: starts with letter/underscore, contains lowercase alphanumerics, underscores, hyphens. */
49
+ const VALID_QUEUE_NAME_REGEX = /^[a-z_][a-z0-9_-]*$/;
50
+
51
+ /** Message ID pattern: must start with qmsg_ prefix. */
52
+ const VALID_MESSAGE_ID_REGEX = /^qmsg_[a-zA-Z0-9]+$/;
53
+
54
+ /** Destination ID pattern: must start with qdest_ prefix. */
55
+ const VALID_DESTINATION_ID_REGEX = /^qdest_[a-zA-Z0-9]+$/;
56
+
57
+ /** Source ID pattern: must start with qsrc_ prefix. */
58
+ const VALID_SOURCE_ID_REGEX = /^qsrc_[a-zA-Z0-9]+$/;
59
+
60
+ /** Maximum source name length. */
61
+ export const MAX_SOURCE_NAME_LENGTH = 256;
62
+
63
+ // ============================================================================
64
+ // Validation Error
65
+ // ============================================================================
66
+
67
+ /**
68
+ * Error thrown when validation fails for queue operations.
69
+ *
70
+ * Includes the field name and optionally the invalid value for debugging.
71
+ *
72
+ * @example
73
+ * ```typescript
74
+ * try {
75
+ * validateQueueName('Invalid Name!');
76
+ * } catch (error) {
77
+ * if (error instanceof QueueValidationError) {
78
+ * console.error(`Invalid ${error.field}: ${error.message}`);
79
+ * }
80
+ * }
81
+ * ```
82
+ */
83
+ export const QueueValidationError = StructuredError('QueueValidationError')<{
84
+ field: string;
85
+ value?: unknown;
86
+ }>();
87
+
88
+ // ============================================================================
89
+ // Validation Functions
90
+ // ============================================================================
91
+
92
+ /**
93
+ * Validates a queue name against naming rules.
94
+ *
95
+ * Queue names must:
96
+ * - Be 1-256 characters long
97
+ * - Start with a lowercase letter or underscore
98
+ * - Contain only lowercase letters, digits, underscores, and hyphens
99
+ *
100
+ * @param name - The queue name to validate
101
+ * @throws {QueueValidationError} If the name is invalid
102
+ *
103
+ * @example
104
+ * ```typescript
105
+ * validateQueueName('my_queue'); // OK
106
+ * validateQueueName('order-queue'); // OK
107
+ * validateQueueName('Invalid Name!'); // Throws
108
+ * ```
109
+ */
110
+ export function validateQueueName(name: string): void {
111
+ if (!name || name.length < MIN_QUEUE_NAME_LENGTH) {
112
+ throw new QueueValidationError({
113
+ message: 'Queue name cannot be empty',
114
+ field: 'name',
115
+ value: name,
116
+ });
117
+ }
118
+ if (name.length > MAX_QUEUE_NAME_LENGTH) {
119
+ throw new QueueValidationError({
120
+ message: `Queue name must not exceed ${MAX_QUEUE_NAME_LENGTH} characters`,
121
+ field: 'name',
122
+ value: name,
123
+ });
124
+ }
125
+ if (!VALID_QUEUE_NAME_REGEX.test(name)) {
126
+ throw new QueueValidationError({
127
+ message:
128
+ 'Queue name must start with a letter or underscore and contain only lowercase letters, digits, underscores, and hyphens',
129
+ field: 'name',
130
+ value: name,
131
+ });
132
+ }
133
+ }
134
+
135
+ /**
136
+ * Validates a queue type.
137
+ *
138
+ * @param type - The queue type to validate
139
+ * @throws {QueueValidationError} If the type is not 'worker' or 'pubsub'
140
+ */
141
+ export function validateQueueType(type: string): void {
142
+ if (type !== 'worker' && type !== 'pubsub') {
143
+ throw new QueueValidationError({
144
+ message: "Queue type must be 'worker' or 'pubsub'",
145
+ field: 'queue_type',
146
+ value: type,
147
+ });
148
+ }
149
+ }
150
+
151
+ /**
152
+ * Validates a message payload.
153
+ *
154
+ * Payloads must be non-empty JSON and not exceed 1MB when serialized.
155
+ *
156
+ * @param payload - The payload to validate (must be JSON-serializable)
157
+ * @throws {QueueValidationError} If the payload is empty or too large
158
+ */
159
+ export function validatePayload(payload: unknown): void {
160
+ if (payload === undefined || payload === null) {
161
+ throw new QueueValidationError({
162
+ message: 'Payload cannot be empty',
163
+ field: 'payload',
164
+ });
165
+ }
166
+ const serialized = JSON.stringify(payload);
167
+ const payloadBytes = new TextEncoder().encode(serialized).length;
168
+ if (payloadBytes > MAX_PAYLOAD_SIZE) {
169
+ throw new QueueValidationError({
170
+ message: `Payload size exceeds ${MAX_PAYLOAD_SIZE} byte limit (${payloadBytes} bytes)`,
171
+ field: 'payload',
172
+ value: payloadBytes,
173
+ });
174
+ }
175
+ }
176
+
177
+ /**
178
+ * Validates a message ID format.
179
+ *
180
+ * Message IDs must start with the `qmsg_` prefix.
181
+ *
182
+ * @param id - The message ID to validate
183
+ * @throws {QueueValidationError} If the ID format is invalid
184
+ */
185
+ export function validateMessageId(id: string): void {
186
+ if (!id || !VALID_MESSAGE_ID_REGEX.test(id)) {
187
+ throw new QueueValidationError({
188
+ message: 'Invalid message ID format (must start with qmsg_ prefix)',
189
+ field: 'message_id',
190
+ value: id,
191
+ });
192
+ }
193
+ }
194
+
195
+ /**
196
+ * Validates a destination ID format.
197
+ *
198
+ * Destination IDs must start with the `qdest_` prefix.
199
+ *
200
+ * @param id - The destination ID to validate
201
+ * @throws {QueueValidationError} If the ID format is invalid
202
+ */
203
+ export function validateDestinationId(id: string): void {
204
+ if (!id || !VALID_DESTINATION_ID_REGEX.test(id)) {
205
+ throw new QueueValidationError({
206
+ message: 'Invalid destination ID format (must start with qdest_ prefix)',
207
+ field: 'destination_id',
208
+ value: id,
209
+ });
210
+ }
211
+ }
212
+
213
+ /**
214
+ * Validates a queue or message description.
215
+ *
216
+ * @param description - The description to validate (optional)
217
+ * @throws {QueueValidationError} If the description exceeds the maximum length
218
+ */
219
+ export function validateDescription(description?: string): void {
220
+ if (description && description.length > MAX_DESCRIPTION_LENGTH) {
221
+ throw new QueueValidationError({
222
+ message: `Description must not exceed ${MAX_DESCRIPTION_LENGTH} characters`,
223
+ field: 'description',
224
+ value: description.length,
225
+ });
226
+ }
227
+ }
228
+
229
+ /**
230
+ * Validates a partition key length.
231
+ *
232
+ * Partition keys are used for message ordering within a queue.
233
+ *
234
+ * @param key - The partition key to validate (optional)
235
+ * @throws {QueueValidationError} If the key exceeds the maximum length
236
+ */
237
+ export function validatePartitionKey(key?: string): void {
238
+ if (key && key.length > MAX_PARTITION_KEY_LENGTH) {
239
+ throw new QueueValidationError({
240
+ message: `Partition key must not exceed ${MAX_PARTITION_KEY_LENGTH} characters`,
241
+ field: 'partition_key',
242
+ value: key.length,
243
+ });
244
+ }
245
+ }
246
+
247
+ /**
248
+ * Validates an idempotency key length.
249
+ *
250
+ * Idempotency keys are used to prevent duplicate message processing.
251
+ *
252
+ * @param key - The idempotency key to validate (optional)
253
+ * @throws {QueueValidationError} If the key exceeds the maximum length
254
+ */
255
+ export function validateIdempotencyKey(key?: string): void {
256
+ if (key && key.length > MAX_IDEMPOTENCY_KEY_LENGTH) {
257
+ throw new QueueValidationError({
258
+ message: `Idempotency key must not exceed ${MAX_IDEMPOTENCY_KEY_LENGTH} characters`,
259
+ field: 'idempotency_key',
260
+ value: key.length,
261
+ });
262
+ }
263
+ }
264
+
265
+ /**
266
+ * Validates a time-to-live value.
267
+ *
268
+ * TTL specifies how long a message should be kept before expiring.
269
+ *
270
+ * @param ttl - The TTL in seconds to validate (optional)
271
+ * @throws {QueueValidationError} If the TTL is negative
272
+ */
273
+ export function validateTTL(ttl?: number): void {
274
+ if (ttl !== undefined && ttl < 0) {
275
+ throw new QueueValidationError({
276
+ message: 'TTL cannot be negative',
277
+ field: 'ttl',
278
+ value: ttl,
279
+ });
280
+ }
281
+ }
282
+
283
+ /**
284
+ * Validates a visibility timeout.
285
+ *
286
+ * Visibility timeout is how long a message is hidden after being received,
287
+ * giving the consumer time to process it before it becomes visible again.
288
+ *
289
+ * @param timeout - The timeout in seconds to validate (optional)
290
+ * @throws {QueueValidationError} If the timeout is out of valid range (1-43200 seconds)
291
+ */
292
+ export function validateVisibilityTimeout(timeout?: number): void {
293
+ if (timeout !== undefined) {
294
+ if (timeout < 1) {
295
+ throw new QueueValidationError({
296
+ message: 'Visibility timeout must be at least 1 second',
297
+ field: 'visibility_timeout',
298
+ value: timeout,
299
+ });
300
+ }
301
+ if (timeout > MAX_VISIBILITY_TIMEOUT) {
302
+ throw new QueueValidationError({
303
+ message: `Visibility timeout must not exceed ${MAX_VISIBILITY_TIMEOUT} seconds (12 hours)`,
304
+ field: 'visibility_timeout',
305
+ value: timeout,
306
+ });
307
+ }
308
+ }
309
+ }
310
+
311
+ /**
312
+ * Validates the maximum retry count.
313
+ *
314
+ * @param retries - The max retries value to validate (optional)
315
+ * @throws {QueueValidationError} If retries is negative or exceeds maximum
316
+ */
317
+ export function validateMaxRetries(retries?: number): void {
318
+ if (retries !== undefined) {
319
+ if (retries < 0) {
320
+ throw new QueueValidationError({
321
+ message: 'Max retries cannot be negative',
322
+ field: 'max_retries',
323
+ value: retries,
324
+ });
325
+ }
326
+ if (retries > MAX_RETRIES) {
327
+ throw new QueueValidationError({
328
+ message: `Max retries must not exceed ${MAX_RETRIES}`,
329
+ field: 'max_retries',
330
+ value: retries,
331
+ });
332
+ }
333
+ }
334
+ }
335
+
336
+ /**
337
+ * Validates the maximum in-flight messages per client.
338
+ *
339
+ * This controls how many messages a single consumer can process concurrently.
340
+ *
341
+ * @param maxInFlight - The max in-flight value to validate (optional)
342
+ * @throws {QueueValidationError} If the value is out of valid range (1-1000)
343
+ */
344
+ export function validateMaxInFlight(maxInFlight?: number): void {
345
+ if (maxInFlight !== undefined) {
346
+ if (maxInFlight < 1) {
347
+ throw new QueueValidationError({
348
+ message: 'Max in-flight per client must be at least 1',
349
+ field: 'max_in_flight',
350
+ value: maxInFlight,
351
+ });
352
+ }
353
+ if (maxInFlight > MAX_IN_FLIGHT) {
354
+ throw new QueueValidationError({
355
+ message: `Max in-flight per client must not exceed ${MAX_IN_FLIGHT}`,
356
+ field: 'max_in_flight',
357
+ value: maxInFlight,
358
+ });
359
+ }
360
+ }
361
+ }
362
+
363
+ /**
364
+ * Validates a message offset.
365
+ *
366
+ * Offsets are sequential positions in the queue, starting from 0.
367
+ *
368
+ * @param offset - The offset value to validate
369
+ * @throws {QueueValidationError} If the offset is negative
370
+ */
371
+ export function validateOffset(offset: number): void {
372
+ if (offset < 0) {
373
+ throw new QueueValidationError({
374
+ message: 'Offset cannot be negative',
375
+ field: 'offset',
376
+ value: offset,
377
+ });
378
+ }
379
+ }
380
+
381
+ /**
382
+ * Validates a limit for list/consume operations.
383
+ *
384
+ * @param limit - The limit value to validate
385
+ * @throws {QueueValidationError} If the limit is less than 1 or exceeds maximum
386
+ */
387
+ export function validateLimit(limit: number): void {
388
+ if (limit < 1) {
389
+ throw new QueueValidationError({
390
+ message: 'Limit must be at least 1',
391
+ field: 'limit',
392
+ value: limit,
393
+ });
394
+ }
395
+ if (limit > MAX_BATCH_SIZE) {
396
+ throw new QueueValidationError({
397
+ message: `Limit must not exceed ${MAX_BATCH_SIZE}`,
398
+ field: 'limit',
399
+ value: limit,
400
+ });
401
+ }
402
+ }
403
+
404
+ /**
405
+ * Validates a batch size for batch operations.
406
+ *
407
+ * @param size - The batch size to validate
408
+ * @throws {QueueValidationError} If the size is less than 1 or exceeds maximum
409
+ */
410
+ export function validateBatchSize(size: number): void {
411
+ if (size <= 0) {
412
+ throw new QueueValidationError({
413
+ message: 'Batch size must be greater than 0',
414
+ field: 'batch_size',
415
+ value: size,
416
+ });
417
+ }
418
+ if (size > MAX_BATCH_SIZE) {
419
+ throw new QueueValidationError({
420
+ message: `Batch size must not exceed ${MAX_BATCH_SIZE}`,
421
+ field: 'batch_size',
422
+ value: size,
423
+ });
424
+ }
425
+ }
426
+
427
+ /**
428
+ * Validates a webhook URL for destinations.
429
+ *
430
+ * URLs must use HTTP or HTTPS protocol.
431
+ *
432
+ * @param url - The URL to validate
433
+ * @throws {QueueValidationError} If the URL is missing or not HTTP/HTTPS
434
+ */
435
+ export function validateWebhookUrl(url: string): void {
436
+ if (!url) {
437
+ throw new QueueValidationError({
438
+ message: 'Webhook URL is required',
439
+ field: 'url',
440
+ });
441
+ }
442
+ if (!url.startsWith('http://') && !url.startsWith('https://')) {
443
+ throw new QueueValidationError({
444
+ message: 'Webhook URL must be a valid HTTP or HTTPS URL',
445
+ field: 'url',
446
+ value: url,
447
+ });
448
+ }
449
+ }
450
+
451
+ /**
452
+ * Validates a destination configuration object.
453
+ *
454
+ * Checks that the config contains a valid URL and optional method/timeout settings.
455
+ *
456
+ * @param config - The destination config object to validate
457
+ * @throws {QueueValidationError} If the config is invalid
458
+ */
459
+ export function validateDestinationConfig(config: Record<string, unknown>): void {
460
+ if (!config) {
461
+ throw new QueueValidationError({
462
+ message: 'Destination config is required',
463
+ field: 'config',
464
+ });
465
+ }
466
+
467
+ const url = config.url;
468
+ if (typeof url !== 'string' || !url) {
469
+ throw new QueueValidationError({
470
+ message: 'config.url is required',
471
+ field: 'config.url',
472
+ });
473
+ }
474
+ validateWebhookUrl(url);
475
+
476
+ const method = config.method;
477
+ if (method !== undefined) {
478
+ if (method !== 'POST' && method !== 'PUT' && method !== 'PATCH') {
479
+ throw new QueueValidationError({
480
+ message: 'config.method must be POST, PUT, or PATCH',
481
+ field: 'config.method',
482
+ value: method,
483
+ });
484
+ }
485
+ }
486
+
487
+ const timeoutMs = config.timeout_ms;
488
+ if (timeoutMs !== undefined) {
489
+ if (typeof timeoutMs !== 'number' || timeoutMs < 1000 || timeoutMs > 300000) {
490
+ throw new QueueValidationError({
491
+ message: 'config.timeout_ms must be between 1000 and 300000',
492
+ field: 'config.timeout_ms',
493
+ value: timeoutMs,
494
+ });
495
+ }
496
+ }
497
+ }
498
+
499
+ /**
500
+ * Validates a source ID format.
501
+ *
502
+ * Source IDs must start with the `qsrc_` prefix.
503
+ *
504
+ * @param id - The source ID to validate
505
+ * @throws {QueueValidationError} If the ID format is invalid
506
+ *
507
+ * @example
508
+ * ```typescript
509
+ * validateSourceId('qsrc_abc123'); // OK
510
+ * validateSourceId('invalid'); // Throws
511
+ * ```
512
+ */
513
+ export function validateSourceId(id: string): void {
514
+ if (!id || typeof id !== 'string') {
515
+ throw new QueueValidationError({
516
+ field: 'source_id',
517
+ value: id,
518
+ message: 'Source ID is required',
519
+ });
520
+ }
521
+ if (!VALID_SOURCE_ID_REGEX.test(id)) {
522
+ throw new QueueValidationError({
523
+ field: 'source_id',
524
+ value: id,
525
+ message:
526
+ 'Source ID must start with "qsrc_" prefix and contain only alphanumeric characters',
527
+ });
528
+ }
529
+ }
530
+
531
+ /**
532
+ * Validates a source name.
533
+ *
534
+ * Source names must be non-empty and not exceed the maximum length.
535
+ *
536
+ * @param name - The source name to validate
537
+ * @throws {QueueValidationError} If the name is invalid
538
+ *
539
+ * @example
540
+ * ```typescript
541
+ * validateSourceName('my-source'); // OK
542
+ * validateSourceName(''); // Throws
543
+ * ```
544
+ */
545
+ export function validateSourceName(name: string): void {
546
+ if (!name || typeof name !== 'string') {
547
+ throw new QueueValidationError({
548
+ field: 'source_name',
549
+ value: name,
550
+ message: 'Source name is required',
551
+ });
552
+ }
553
+ if (name.length > MAX_SOURCE_NAME_LENGTH) {
554
+ throw new QueueValidationError({
555
+ field: 'source_name',
556
+ value: name,
557
+ message: `Source name must be at most ${MAX_SOURCE_NAME_LENGTH} characters`,
558
+ });
559
+ }
560
+ }
@@ -5,14 +5,17 @@ import type {
5
5
  ExecuteOptions as CoreExecuteOptions,
6
6
  Execution,
7
7
  FileToWrite,
8
+ SandboxRunOptions,
9
+ SandboxRunResult,
8
10
  } from '@agentuity/core';
9
- import type { Writable } from 'node:stream';
11
+ import type { Readable, Writable } from 'node:stream';
10
12
  import { APIClient } from '../api';
11
13
  import { sandboxCreate, type SandboxCreateResponse } from './create';
12
14
  import { sandboxDestroy } from './destroy';
13
15
  import { sandboxGet } from './get';
14
16
  import { sandboxExecute } from './execute';
15
17
  import { sandboxWriteFiles, sandboxReadFile } from './files';
18
+ import { sandboxRun } from './run';
16
19
  import { executionGet, type ExecutionInfo } from './execution';
17
20
  import { ConsoleLogger } from '../../logger';
18
21
  import { getServiceUrls } from '../../config';
@@ -129,6 +132,36 @@ export interface SandboxClientOptions {
129
132
  logger?: Logger;
130
133
  }
131
134
 
135
+ /**
136
+ * I/O options for one-shot sandbox execution via run()
137
+ */
138
+ export interface SandboxClientRunIO {
139
+ /**
140
+ * AbortSignal to cancel the execution
141
+ */
142
+ signal?: AbortSignal;
143
+
144
+ /**
145
+ * Readable stream for stdin input
146
+ */
147
+ stdin?: Readable;
148
+
149
+ /**
150
+ * Writable stream for stdout output
151
+ */
152
+ stdout?: Writable;
153
+
154
+ /**
155
+ * Writable stream for stderr output
156
+ */
157
+ stderr?: Writable;
158
+
159
+ /**
160
+ * Optional logger override for this run
161
+ */
162
+ logger?: Logger;
163
+ }
164
+
132
165
  /**
133
166
  * A sandbox instance returned by SandboxClient.create()
134
167
  */
@@ -174,15 +207,25 @@ export interface SandboxInstance {
174
207
  *
175
208
  * @example
176
209
  * ```typescript
210
+ * // Interactive sandbox usage
177
211
  * const client = new SandboxClient();
178
212
  * const sandbox = await client.create();
179
213
  * const result = await sandbox.execute({ command: ['echo', 'hello'] });
180
214
  * await sandbox.destroy();
215
+ *
216
+ * // One-shot execution with streaming
217
+ * const result = await client.run(
218
+ * { command: { exec: ['bun', 'run', 'script.ts'] } },
219
+ * { stdout: process.stdout, stderr: process.stderr }
220
+ * );
181
221
  * ```
182
222
  */
183
223
  export class SandboxClient {
184
224
  readonly #client: APIClient;
185
225
  readonly #orgId?: string;
226
+ readonly #apiKey?: string;
227
+ readonly #region: string;
228
+ readonly #logger: Logger;
186
229
 
187
230
  constructor(options: SandboxClientOptions = {}) {
188
231
  const apiKey =
@@ -202,6 +245,48 @@ export class SandboxClient {
202
245
 
203
246
  this.#client = new APIClient(url, logger, apiKey ?? '', {});
204
247
  this.#orgId = options.orgId;
248
+ this.#apiKey = apiKey;
249
+ this.#region = region;
250
+ this.#logger = logger;
251
+ }
252
+
253
+ /**
254
+ * Run a one-shot command in a new sandbox (creates, executes, destroys)
255
+ *
256
+ * This is a high-level convenience method that handles the full lifecycle:
257
+ * creating a sandbox, streaming I/O, polling for completion, and cleanup.
258
+ *
259
+ * @param options - Execution options including command and configuration
260
+ * @param io - Optional I/O streams and abort signal
261
+ * @returns The run result including exit code and duration
262
+ * @throws {Error} If stdin is provided without an API key
263
+ *
264
+ * @example
265
+ * ```typescript
266
+ * const client = new SandboxClient();
267
+ * const result = await client.run(
268
+ * { command: { exec: ['bun', 'run', 'script.ts'] } },
269
+ * { stdout: process.stdout, stderr: process.stderr }
270
+ * );
271
+ * console.log('Exit code:', result.exitCode);
272
+ * ```
273
+ */
274
+ async run(options: SandboxRunOptions, io: SandboxClientRunIO = {}): Promise<SandboxRunResult> {
275
+ if (io.stdin && !this.#apiKey) {
276
+ throw new Error('SandboxClient.run(): stdin streaming requires an API key');
277
+ }
278
+
279
+ return sandboxRun(this.#client, {
280
+ options,
281
+ orgId: this.#orgId,
282
+ region: this.#region,
283
+ apiKey: this.#apiKey,
284
+ signal: io.signal,
285
+ stdin: io.stdin,
286
+ stdout: io.stdout,
287
+ stderr: io.stderr,
288
+ logger: io.logger ?? this.#logger,
289
+ });
205
290
  }
206
291
 
207
292
  /**
@@ -58,6 +58,10 @@ const SandboxInfoDataSchema = z
58
58
  snapshotId: z.string().optional().describe('Snapshot ID this sandbox was created from'),
59
59
  snapshotTag: z.string().optional().describe('Snapshot tag this sandbox was created from'),
60
60
  executions: z.number().describe('Total number of executions in this sandbox'),
61
+ exitCode: z
62
+ .number()
63
+ .optional()
64
+ .describe('Exit code from the last execution (only for terminated/failed sandboxes)'),
61
65
  stdoutStreamUrl: z.string().optional().describe('URL for streaming stdout output'),
62
66
  stderrStreamUrl: z.string().optional().describe('URL for streaming stderr output'),
63
67
  dependencies: z
@@ -137,6 +141,7 @@ export async function sandboxGet(
137
141
  snapshotId: resp.data.snapshotId,
138
142
  snapshotTag: resp.data.snapshotTag,
139
143
  executions: resp.data.executions,
144
+ exitCode: resp.data.exitCode,
140
145
  stdoutStreamUrl: resp.data.stdoutStreamUrl,
141
146
  stderrStreamUrl: resp.data.stderrStreamUrl,
142
147
  dependencies: resp.data.dependencies,