@scenario-labs/sdk 0.3.0 → 1.0.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.
Files changed (180) hide show
  1. package/CHANGELOG.md +22 -0
  2. package/README.md +40 -6
  3. package/client.d.mts +34 -14
  4. package/client.d.mts.map +1 -1
  5. package/client.d.ts +34 -14
  6. package/client.d.ts.map +1 -1
  7. package/client.js +10 -0
  8. package/client.js.map +1 -1
  9. package/client.mjs +10 -0
  10. package/client.mjs.map +1 -1
  11. package/core/pagination.d.mts +153 -0
  12. package/core/pagination.d.mts.map +1 -0
  13. package/core/pagination.d.ts +153 -0
  14. package/core/pagination.d.ts.map +1 -0
  15. package/core/pagination.js +276 -0
  16. package/core/pagination.js.map +1 -0
  17. package/core/pagination.mjs +263 -0
  18. package/core/pagination.mjs.map +1 -0
  19. package/index.d.mts +2 -0
  20. package/index.d.mts.map +1 -1
  21. package/index.d.ts +2 -0
  22. package/index.d.ts.map +1 -1
  23. package/index.js +7 -1
  24. package/index.js.map +1 -1
  25. package/index.mjs +2 -0
  26. package/index.mjs.map +1 -1
  27. package/lib/models.d.mts +34 -2
  28. package/lib/models.d.mts.map +1 -1
  29. package/lib/models.d.ts +34 -2
  30. package/lib/models.d.ts.map +1 -1
  31. package/lib/models.js +39 -2
  32. package/lib/models.js.map +1 -1
  33. package/lib/models.mjs +37 -1
  34. package/lib/models.mjs.map +1 -1
  35. package/lib/scenario.d.mts +2 -0
  36. package/lib/scenario.d.mts.map +1 -1
  37. package/lib/scenario.d.ts +2 -0
  38. package/lib/scenario.d.ts.map +1 -1
  39. package/lib/scenario.js +2 -0
  40. package/lib/scenario.js.map +1 -1
  41. package/lib/scenario.mjs +2 -0
  42. package/lib/scenario.mjs.map +1 -1
  43. package/lib/upload.d.mts +89 -0
  44. package/lib/upload.d.mts.map +1 -0
  45. package/lib/upload.d.ts +89 -0
  46. package/lib/upload.d.ts.map +1 -0
  47. package/lib/upload.js +193 -0
  48. package/lib/upload.js.map +1 -0
  49. package/lib/upload.mjs +156 -0
  50. package/lib/upload.mjs.map +1 -0
  51. package/lib/workflow.d.mts +13 -2
  52. package/lib/workflow.d.mts.map +1 -1
  53. package/lib/workflow.d.ts +13 -2
  54. package/lib/workflow.d.ts.map +1 -1
  55. package/lib/workflow.js +17 -4
  56. package/lib/workflow.js.map +1 -1
  57. package/lib/workflow.mjs +17 -4
  58. package/lib/workflow.mjs.map +1 -1
  59. package/lib/workflow_import_validator.d.mts +40 -0
  60. package/lib/workflow_import_validator.d.mts.map +1 -0
  61. package/lib/workflow_import_validator.d.ts +40 -0
  62. package/lib/workflow_import_validator.d.ts.map +1 -0
  63. package/lib/workflow_import_validator.js +69 -0
  64. package/lib/workflow_import_validator.js.map +1 -0
  65. package/lib/workflow_import_validator.mjs +64 -0
  66. package/lib/workflow_import_validator.mjs.map +1 -0
  67. package/package.json +11 -1
  68. package/pagination.d.mts +2 -0
  69. package/pagination.d.mts.map +1 -0
  70. package/pagination.d.ts +2 -0
  71. package/pagination.d.ts.map +1 -0
  72. package/pagination.js +6 -0
  73. package/pagination.js.map +1 -0
  74. package/pagination.mjs +2 -0
  75. package/pagination.mjs.map +1 -0
  76. package/resources/assets/assets.d.mts +623 -646
  77. package/resources/assets/assets.d.mts.map +1 -1
  78. package/resources/assets/assets.d.ts +623 -646
  79. package/resources/assets/assets.d.ts.map +1 -1
  80. package/resources/assets/assets.js +3 -2
  81. package/resources/assets/assets.js.map +1 -1
  82. package/resources/assets/assets.mjs +3 -2
  83. package/resources/assets/assets.mjs.map +1 -1
  84. package/resources/assets/index.d.mts +1 -1
  85. package/resources/assets/index.d.mts.map +1 -1
  86. package/resources/assets/index.d.ts +1 -1
  87. package/resources/assets/index.d.ts.map +1 -1
  88. package/resources/assets/index.js.map +1 -1
  89. package/resources/assets/index.mjs.map +1 -1
  90. package/resources/collections/collections.d.mts +38 -49
  91. package/resources/collections/collections.d.mts.map +1 -1
  92. package/resources/collections/collections.d.ts +38 -49
  93. package/resources/collections/collections.d.ts.map +1 -1
  94. package/resources/collections/collections.js +5 -1
  95. package/resources/collections/collections.js.map +1 -1
  96. package/resources/collections/collections.mjs +5 -1
  97. package/resources/collections/collections.mjs.map +1 -1
  98. package/resources/collections/index.d.mts +1 -1
  99. package/resources/collections/index.d.mts.map +1 -1
  100. package/resources/collections/index.d.ts +1 -1
  101. package/resources/collections/index.d.ts.map +1 -1
  102. package/resources/collections/index.js.map +1 -1
  103. package/resources/collections/index.mjs.map +1 -1
  104. package/resources/index.d.mts +7 -7
  105. package/resources/index.d.mts.map +1 -1
  106. package/resources/index.d.ts +7 -7
  107. package/resources/index.d.ts.map +1 -1
  108. package/resources/index.js.map +1 -1
  109. package/resources/index.mjs.map +1 -1
  110. package/resources/jobs.d.mts +369 -380
  111. package/resources/jobs.d.mts.map +1 -1
  112. package/resources/jobs.d.ts +369 -380
  113. package/resources/jobs.d.ts.map +1 -1
  114. package/resources/jobs.js +2 -1
  115. package/resources/jobs.js.map +1 -1
  116. package/resources/jobs.mjs +2 -1
  117. package/resources/jobs.mjs.map +1 -1
  118. package/resources/models/index.d.mts +1 -1
  119. package/resources/models/index.d.mts.map +1 -1
  120. package/resources/models/index.d.ts +1 -1
  121. package/resources/models/index.d.ts.map +1 -1
  122. package/resources/models/index.js.map +1 -1
  123. package/resources/models/index.mjs.map +1 -1
  124. package/resources/models/models.d.mts +839 -850
  125. package/resources/models/models.d.mts.map +1 -1
  126. package/resources/models/models.d.ts +839 -850
  127. package/resources/models/models.d.ts.map +1 -1
  128. package/resources/models/models.js +2 -1
  129. package/resources/models/models.js.map +1 -1
  130. package/resources/models/models.mjs +2 -1
  131. package/resources/models/models.mjs.map +1 -1
  132. package/resources/search.d.mts +215 -295
  133. package/resources/search.d.mts.map +1 -1
  134. package/resources/search.d.ts +215 -295
  135. package/resources/search.d.ts.map +1 -1
  136. package/resources/search.js +9 -2
  137. package/resources/search.js.map +1 -1
  138. package/resources/search.mjs +9 -2
  139. package/resources/search.mjs.map +1 -1
  140. package/resources/tags.d.mts +23 -35
  141. package/resources/tags.d.mts.map +1 -1
  142. package/resources/tags.d.ts +23 -35
  143. package/resources/tags.d.ts.map +1 -1
  144. package/resources/tags.js +2 -1
  145. package/resources/tags.js.map +1 -1
  146. package/resources/tags.mjs +2 -1
  147. package/resources/tags.mjs.map +1 -1
  148. package/resources/workflows.d.mts +688 -704
  149. package/resources/workflows.d.mts.map +1 -1
  150. package/resources/workflows.d.ts +688 -704
  151. package/resources/workflows.d.ts.map +1 -1
  152. package/resources/workflows.js +5 -1
  153. package/resources/workflows.js.map +1 -1
  154. package/resources/workflows.mjs +5 -1
  155. package/resources/workflows.mjs.map +1 -1
  156. package/src/client.ts +103 -2
  157. package/src/core/pagination.ts +497 -0
  158. package/src/index.ts +8 -0
  159. package/src/lib/models.ts +62 -2
  160. package/src/lib/scenario.ts +2 -0
  161. package/src/lib/upload.ts +250 -0
  162. package/src/lib/workflow.ts +29 -4
  163. package/src/lib/workflow_import_validator.ts +104 -0
  164. package/src/pagination.ts +2 -0
  165. package/src/resources/assets/assets.ts +887 -902
  166. package/src/resources/assets/index.ts +2 -0
  167. package/src/resources/collections/collections.ts +45 -54
  168. package/src/resources/collections/index.ts +1 -0
  169. package/src/resources/index.ts +9 -1
  170. package/src/resources/jobs.ts +613 -622
  171. package/src/resources/models/index.ts +1 -0
  172. package/src/resources/models/models.ts +1052 -1064
  173. package/src/resources/search.ts +239 -327
  174. package/src/resources/tags.ts +32 -39
  175. package/src/resources/workflows.ts +850 -864
  176. package/src/version.ts +1 -1
  177. package/version.d.mts +1 -1
  178. package/version.d.ts +1 -1
  179. package/version.js +1 -1
  180. package/version.mjs +1 -1
package/src/index.ts CHANGED
@@ -6,6 +6,7 @@ export { type Uploadable, toFile } from './core/uploads';
6
6
  export { APIPromise } from './core/api-promise';
7
7
  export { Scenario } from './lib/scenario';
8
8
  export { type ClientOptions } from './client';
9
+ export { PagePromise } from './core/pagination';
9
10
  export {
10
11
  ScenarioError,
11
12
  APIError,
@@ -24,5 +25,12 @@ export {
24
25
  export { Job, type WaitOptions, type WithJob } from './lib/job';
25
26
  export { EnhancedWorkflows, WorkflowEntity } from './lib/workflow';
26
27
  export { validateWorkflowFlow } from './lib/workflow_validator';
28
+ export {
29
+ validateEditorInfo,
30
+ WorkflowImportError,
31
+ VALID_EDITOR_NODE_TYPES,
32
+ type WorkflowExportData,
33
+ type WorkflowEditorNodeType,
34
+ } from './lib/workflow_import_validator';
27
35
  export { EnhancedGenerate } from './lib/generate';
28
36
  export { EnhancedModels, EnhancedTrain } from './lib/models';
package/src/lib/models.ts CHANGED
@@ -1,8 +1,55 @@
1
1
  import type { APIPromise } from '../core/api-promise';
2
2
  import type { RequestOptions } from '../internal/request-options';
3
- import { Models } from '../resources/models/models';
3
+ import type { GenerateRunModelParams, GenerateRunModelResponse } from '../resources/generate';
4
+ import { Models, type ModelRetrieveParams, type ModelRetrieveResponse } from '../resources/models/models';
4
5
  import { Train, type TrainTriggerParams, type TrainTriggerResponse } from '../resources/models/train';
5
6
  import { type WithJob, enhanceJob } from './job';
7
+ // Type-only import to break the circular `scenario.ts` ↔ `models.ts` graph at runtime.
8
+ import type { Scenario } from './scenario';
9
+
10
+ /**
11
+ * Enhanced Model entity with helper methods.
12
+ * Has all original model fields plus `.run()`.
13
+ *
14
+ * @example
15
+ * ```ts
16
+ * const { model } = await client.models.retrieve(modelId);
17
+ * const gen = await model.run({ body: { prompt: 'a red car' } });
18
+ * const completed = await gen.job.wait();
19
+ * ```
20
+ */
21
+ export class ModelEntity {
22
+ /** @internal */ declare readonly _client: Scenario;
23
+
24
+ /**
25
+ * Generate with this model. Shortcut for
26
+ * `client.generate.runModel(this.id, params)`.
27
+ *
28
+ * Returns an enhanced response where `response.job` has `.wait()`.
29
+ */
30
+ run(
31
+ this: ModelEntity & ModelRetrieveResponse.Model,
32
+ params: GenerateRunModelParams,
33
+ options?: RequestOptions,
34
+ ): APIPromise<WithJob<GenerateRunModelResponse>> {
35
+ return this._client.generate.runModel(this.id, params, options);
36
+ }
37
+
38
+ /** @internal Create a ModelEntity wrapping raw model data. */
39
+ static from(
40
+ client: Scenario,
41
+ data: ModelRetrieveResponse.Model,
42
+ ): ModelRetrieveResponse.Model & ModelEntity {
43
+ const entity = Object.assign(Object.create(ModelEntity.prototype), data);
44
+ Object.defineProperty(entity, '_client', { value: client, enumerable: false });
45
+ return entity;
46
+ }
47
+ }
48
+
49
+ /** Enhanced retrieve response where `model` has `.run()` on top of its original fields. */
50
+ export type EnhancedModelRetrieveResponse = Omit<ModelRetrieveResponse, 'model'> & {
51
+ model: ModelRetrieveResponse.Model & ModelEntity;
52
+ };
6
53
 
7
54
  /**
8
55
  * Enhanced Train resource.
@@ -20,8 +67,21 @@ export class EnhancedTrain extends Train {
20
67
 
21
68
  /**
22
69
  * Enhanced Models resource.
23
- * All original methods inherited. `train` is an {@link EnhancedTrain}.
70
+ * All original methods inherited.
71
+ * - `train` is an {@link EnhancedTrain}
72
+ * - `retrieve()` returns a response where `model` has `.run()`
24
73
  */
25
74
  export class EnhancedModels extends Models {
26
75
  override train: EnhancedTrain = new EnhancedTrain(this._client);
76
+
77
+ override retrieve(
78
+ modelID: string,
79
+ query: ModelRetrieveParams | null | undefined = {},
80
+ options?: RequestOptions,
81
+ ): APIPromise<EnhancedModelRetrieveResponse> {
82
+ return super.retrieve(modelID, query, options)._thenUnwrap((data) => ({
83
+ ...data,
84
+ model: ModelEntity.from(this._client as Scenario, data.model),
85
+ }));
86
+ }
27
87
  }
@@ -1,6 +1,7 @@
1
1
  import { Scenario as BaseScenario } from '../client';
2
2
  import { EnhancedGenerate } from './generate';
3
3
  import { EnhancedModels } from './models';
4
+ import { EnhancedUploads } from './upload';
4
5
  import { EnhancedWorkflows } from './workflow';
5
6
 
6
7
  /**
@@ -36,4 +37,5 @@ export class Scenario extends BaseScenario {
36
37
  override workflows: EnhancedWorkflows = new EnhancedWorkflows(this);
37
38
  override generate: EnhancedGenerate = new EnhancedGenerate(this);
38
39
  override models: EnhancedModels = new EnhancedModels(this);
40
+ override uploads: EnhancedUploads = new EnhancedUploads(this);
39
41
  }
@@ -0,0 +1,250 @@
1
+ import type { RequestOptions } from '../internal/request-options';
2
+ import {
3
+ Uploads,
4
+ type UploadCreateParams,
5
+ type UploadCreateResponse,
6
+ type UploadRetrieveResponse,
7
+ } from '../resources/uploads';
8
+ import type { AssetRetrieveResponse } from '../resources/assets/assets';
9
+ import type { ModelRetrieveResponse } from '../resources/models/models';
10
+
11
+ type UploadKind = NonNullable<UploadCreateParams['kind']>;
12
+
13
+ /** The resolved entity returned after a successful upload, narrowed by `kind`. */
14
+ export type UploadResult<K extends UploadKind> =
15
+ K extends 'model' ? ModelRetrieveResponse : AssetRetrieveResponse;
16
+
17
+ /**
18
+ * Accepted upload inputs.
19
+ * - `string` — a local file path, read from disk (Node only; throws in browsers).
20
+ * - `Uint8Array` — raw bytes. `Buffer` is a `Uint8Array` and works too.
21
+ * - `Blob` / `File` — browser-native, or Node ≥ 18 via `fs.openAsBlob()` /
22
+ * constructors.
23
+ */
24
+ export type UploadFileInput = string | Uint8Array | Blob;
25
+
26
+ export interface UploadFileParams<K extends UploadKind> {
27
+ /**
28
+ * The file to upload. Accepts a local path (Node), a `Uint8Array` / `Buffer`,
29
+ * or a `Blob` / `File`.
30
+ */
31
+ file: UploadFileInput;
32
+ /** Original filename (e.g. `"my-model.safetensors"`). */
33
+ fileName: string;
34
+ /** MIME type (e.g. `"application/octet-stream"`, `"image/png"`). */
35
+ contentType: string;
36
+ /** The kind of upload. Determines the returned entity type. */
37
+ kind: K;
38
+ /**
39
+ * Extra asset options. Ignored when `kind === 'model'` — models don't
40
+ * produce an asset, so these options are not applicable server-side.
41
+ */
42
+ assetOptions?: UploadCreateParams['assetOptions'];
43
+ /**
44
+ * Maximum number of parts uploaded to S3 in parallel.
45
+ * @default 4
46
+ */
47
+ partConcurrency?: number;
48
+ /**
49
+ * Polling interval for the final "upload imported" check, in ms.
50
+ * @default 2000
51
+ */
52
+ pollIntervalMs?: number;
53
+ /**
54
+ * Maximum time to wait for the upload to reach a terminal state, in ms.
55
+ * Throws if exceeded.
56
+ * @default 300000
57
+ */
58
+ pollTimeoutMs?: number;
59
+ }
60
+
61
+ // AWS S3 multipart upload limits.
62
+ const MIN_PART_SIZE = 6_000_000; // 6 MB (spec minimum is 5 MiB; a bit over to be safe)
63
+ const MAX_PART_SIZE = 5_368_709_120; // 5 GiB
64
+ const MAX_PARTS = 10_000;
65
+ const MAX_FILE_SIZE = 5_497_558_138_880; // 5 TiB
66
+
67
+ // `validated` is intermediate — the server has validated the upload but
68
+ // hasn't created the asset/model yet, so `entityId` isn't populated.
69
+ // Real terminals are `imported` (asset/model created), `complete`, and
70
+ // `failed`. Everything else means "keep polling".
71
+ const TERMINAL_STATUSES = new Set(['complete', 'failed', 'imported']);
72
+
73
+ /**
74
+ * Enhanced Uploads resource.
75
+ * All original methods (`create`, `retrieve`, `triggerAction`) are inherited.
76
+ * Adds `uploadFile()` — a one-call wrapper around the 4-step upload flow:
77
+ * init → PUT parts to S3 → trigger complete → poll until imported.
78
+ */
79
+ export class EnhancedUploads extends Uploads {
80
+ /**
81
+ * Upload a file end-to-end. Computes an optimal part size (within AWS's
82
+ * 5 MiB / 5 GiB / 10,000-part bounds), uploads all parts in parallel,
83
+ * triggers completion, polls until the server has validated and imported
84
+ * the file, and returns the resolved entity.
85
+ *
86
+ * The returned type narrows on `kind`: `'model'` returns a Model,
87
+ * everything else returns an Asset.
88
+ *
89
+ * @example
90
+ * ```ts
91
+ * // From a file path (Node)
92
+ * const { asset } = await client.uploads.uploadFile({
93
+ * file: './photo.jpg',
94
+ * fileName: 'photo.jpg',
95
+ * contentType: 'image/jpeg',
96
+ * kind: 'image',
97
+ * });
98
+ * console.log(asset.id, asset.url);
99
+ *
100
+ * // Or from bytes / Blob — anything you already have in memory
101
+ * await client.uploads.uploadFile({
102
+ * file: new Blob([pngBytes], { type: 'image/png' }),
103
+ * fileName: 'generated.png',
104
+ * contentType: 'image/png',
105
+ * kind: 'image',
106
+ * });
107
+ * ```
108
+ */
109
+ async uploadFile<K extends UploadKind>(
110
+ params: UploadFileParams<K>,
111
+ options?: RequestOptions,
112
+ ): Promise<UploadResult<K>> {
113
+ const bytes = await normalizeToBytes(params.file);
114
+ const fileSize = bytes.byteLength;
115
+
116
+ if (fileSize === 0) {
117
+ throw new Error('Cannot upload an empty file');
118
+ }
119
+ if (fileSize > MAX_FILE_SIZE) {
120
+ throw new Error(
121
+ `File size (${fileSize} bytes) exceeds the maximum allowed size of ${MAX_FILE_SIZE} bytes (5 TiB)`,
122
+ );
123
+ }
124
+
125
+ // Pick the smallest legal part size that fits within MAX_PARTS.
126
+ const requiredPartSize = Math.ceil(fileSize / MAX_PARTS);
127
+ const partSize = Math.max(MIN_PART_SIZE, requiredPartSize);
128
+ if (partSize > MAX_PART_SIZE) {
129
+ throw new Error(
130
+ `File size (${fileSize} bytes) requires a part size of ${partSize} bytes, ` +
131
+ `which exceeds the maximum part size of ${MAX_PART_SIZE} bytes (5 GiB)`,
132
+ );
133
+ }
134
+ const partsCount = Math.max(1, Math.ceil(fileSize / partSize));
135
+
136
+ // Step 1 — init upload session and receive presigned S3 URLs.
137
+ const createBody: UploadCreateParams = {
138
+ fileName: params.fileName,
139
+ fileSize,
140
+ contentType: params.contentType,
141
+ kind: params.kind,
142
+ parts: partsCount,
143
+ ...(params.kind !== 'model' && params.assetOptions ? { assetOptions: params.assetOptions } : {}),
144
+ };
145
+ const createRes: UploadCreateResponse = await this.create(createBody, options);
146
+ const session = createRes.upload;
147
+ const uploadId = session.id;
148
+ const parts = session.parts;
149
+ if (parts?.length !== partsCount) {
150
+ throw new Error(`Expected ${partsCount} presigned part URL(s), got ${parts?.length ?? 0}`);
151
+ }
152
+
153
+ // Step 2 — PUT each chunk to its presigned S3 URL, with a concurrency cap.
154
+ const concurrency = Math.max(1, params.partConcurrency ?? 4);
155
+ await uploadPartsWithConcurrency(bytes, parts, partSize, concurrency);
156
+
157
+ // Step 3 — tell the server all parts are uploaded.
158
+ await this.triggerAction(uploadId, { action: 'complete' }, options);
159
+
160
+ // Step 4 — poll until the upload has been processed into an entity.
161
+ const final = await pollUntilImported(
162
+ () => this.retrieve(uploadId, options),
163
+ params.pollIntervalMs ?? 2_000,
164
+ params.pollTimeoutMs ?? 300_000,
165
+ );
166
+
167
+ if (final.upload.status === 'failed') {
168
+ throw new Error(`Upload ${uploadId} failed: ${final.upload.errorMessage ?? '<no error message>'}`);
169
+ }
170
+ const entityId = final.upload.entityId;
171
+ if (!entityId) {
172
+ throw new Error(`Upload ${uploadId} reached status "${final.upload.status}" but has no entityId`);
173
+ }
174
+
175
+ // Fetch and return the resolved entity (narrowed by kind).
176
+ if (params.kind === 'model') {
177
+ const model = await this._client.models.retrieve(entityId);
178
+ return model as UploadResult<K>;
179
+ }
180
+ const asset = await this._client.assets.retrieve(entityId);
181
+ return asset as UploadResult<K>;
182
+ }
183
+ }
184
+
185
+ // ─────────────────────── helpers ───────────────────────
186
+
187
+ async function normalizeToBytes(input: UploadFileInput): Promise<Uint8Array> {
188
+ if (typeof input === 'string') {
189
+ // Dynamic import so browser bundlers don't pull in node:fs.
190
+ const { readFile } = await import('node:fs/promises');
191
+ return new Uint8Array(await readFile(input));
192
+ }
193
+ if (input instanceof Uint8Array) {
194
+ return input;
195
+ }
196
+ if (typeof Blob !== 'undefined' && input instanceof Blob) {
197
+ return new Uint8Array(await input.arrayBuffer());
198
+ }
199
+ throw new Error('uploadFile: unsupported input type — expected a path string, Uint8Array, or Blob');
200
+ }
201
+
202
+ async function uploadPartsWithConcurrency(
203
+ bytes: Uint8Array,
204
+ parts: Array<{ number: number; url: string; expires: string }>,
205
+ partSize: number,
206
+ concurrency: number,
207
+ ): Promise<void> {
208
+ // Sort by part number so slicing lines up with the presigned URL order.
209
+ const sorted = [...parts].sort((a, b) => a.number - b.number);
210
+
211
+ let nextIndex = 0;
212
+ const workers = Array.from({ length: concurrency }, () => worker());
213
+ await Promise.all(workers);
214
+
215
+ async function worker() {
216
+ while (true) {
217
+ const i = nextIndex++;
218
+ if (i >= sorted.length) return;
219
+ const part = sorted[i];
220
+ if (!part) continue;
221
+ const start = i * partSize;
222
+ const end = Math.min(bytes.byteLength, start + partSize);
223
+ const chunk = bytes.subarray(start, end);
224
+ const res = await fetch(part.url, { method: 'PUT', body: chunk });
225
+ if (!res.ok) {
226
+ throw new Error(
227
+ `Failed to upload part ${part.number}/${sorted.length}: ${res.status} ${res.statusText}`,
228
+ );
229
+ }
230
+ }
231
+ }
232
+ }
233
+
234
+ async function pollUntilImported(
235
+ retrieve: () => Promise<UploadRetrieveResponse>,
236
+ intervalMs: number,
237
+ timeoutMs: number,
238
+ ): Promise<UploadRetrieveResponse> {
239
+ const deadline = Date.now() + timeoutMs;
240
+ while (Date.now() < deadline) {
241
+ const res = await retrieve();
242
+ const { status } = res.upload;
243
+ // Return as soon as we have an entityId OR the upload reached a terminal state.
244
+ if (res.upload.entityId || TERMINAL_STATUSES.has(status)) {
245
+ return res;
246
+ }
247
+ await new Promise((r) => setTimeout(r, intervalMs));
248
+ }
249
+ throw new Error(`Upload did not reach a terminal status within ${timeoutMs / 1000}s`);
250
+ }
@@ -10,23 +10,29 @@ import {
10
10
  } from '../resources/workflows';
11
11
  import { type WithJob, enhanceJob } from './job';
12
12
  import { validateWorkflowFlow } from './workflow_validator';
13
+ // Type-only import to break the circular `scenario.ts` ↔ `workflow.ts` graph at runtime.
14
+ import type { Scenario } from './scenario';
13
15
 
14
16
  type DefinitionFlow = WorkflowRetrieveResponse.Workflow.Flow;
15
17
  type NodeType = DefinitionFlow['type'];
16
18
 
17
19
  /**
18
20
  * Enhanced workflow object with helper methods.
19
- * Has all original workflow fields plus `findNode()`, `getNodesByType()`.
21
+ * Has all original workflow fields plus `findNode()`, `getNodesByType()`,
22
+ * `validate()`, and `run()`.
20
23
  *
21
24
  * @example
22
25
  * ```ts
23
26
  * const response = await client.workflows.retrieve(workflowId);
24
27
  * response.workflow.findNode('final_image');
25
28
  * response.workflow.getNodesByType('custom-model');
29
+ * const gen = await response.workflow.run({ body: { prompt: '...' } });
26
30
  * response.workflow.name; // original fields still work
27
31
  * ```
28
32
  */
29
33
  export class WorkflowEntity {
34
+ /** @internal */ declare readonly _client: Scenario;
35
+
30
36
  /** Find a node in the workflow definition by ID. */
31
37
  findNode(
32
38
  this: WorkflowEntity & WorkflowRetrieveResponse.Workflow,
@@ -48,9 +54,28 @@ export class WorkflowEntity {
48
54
  validateWorkflowFlow(this.flow);
49
55
  }
50
56
 
57
+ /**
58
+ * Run this workflow. Shortcut for
59
+ * `client.workflows.run(this.id, params)`.
60
+ *
61
+ * Returns an enhanced response where `response.job` has `.wait()`.
62
+ */
63
+ run(
64
+ this: WorkflowEntity & WorkflowRetrieveResponse.Workflow,
65
+ params: WorkflowRunParams,
66
+ options?: RequestOptions,
67
+ ): APIPromise<WithJob<WorkflowRunResponse>> {
68
+ return this._client.workflows.run(this.id, params, options);
69
+ }
70
+
51
71
  /** @internal Create a WorkflowEntity from raw workflow data. */
52
- static from(data: WorkflowRetrieveResponse.Workflow): WorkflowRetrieveResponse.Workflow & WorkflowEntity {
53
- return Object.assign(Object.create(WorkflowEntity.prototype), data);
72
+ static from(
73
+ client: Scenario,
74
+ data: WorkflowRetrieveResponse.Workflow,
75
+ ): WorkflowRetrieveResponse.Workflow & WorkflowEntity {
76
+ const entity = Object.assign(Object.create(WorkflowEntity.prototype), data);
77
+ Object.defineProperty(entity, '_client', { value: client, enumerable: false });
78
+ return entity;
54
79
  }
55
80
  }
56
81
 
@@ -75,7 +100,7 @@ export class EnhancedWorkflows extends Workflows {
75
100
  ): APIPromise<EnhancedWorkflowRetrieveResponse> {
76
101
  return super.retrieve(workflowID, options)._thenUnwrap((data) => ({
77
102
  ...data,
78
- workflow: WorkflowEntity.from(data.workflow),
103
+ workflow: WorkflowEntity.from(this._client as Scenario, data.workflow),
79
104
  }));
80
105
  }
81
106
 
@@ -0,0 +1,104 @@
1
+ export class WorkflowImportError extends Error {
2
+ constructor(message: string) {
3
+ super(message);
4
+ this.name = 'WorkflowImportError';
5
+ }
6
+ }
7
+
8
+ export type WorkflowEditorNodeType =
9
+ | 'text'
10
+ | 'asset'
11
+ | 'model'
12
+ | 'llm'
13
+ | 'transformText'
14
+ | 'splitText'
15
+ | 'ifElse'
16
+ | 'groupItems'
17
+ | 'sliceAssets'
18
+ | 'forEach'
19
+ | 'forEachEnd'
20
+ | 'stickyNote';
21
+
22
+ export const VALID_EDITOR_NODE_TYPES: readonly WorkflowEditorNodeType[] = [
23
+ 'text',
24
+ 'asset',
25
+ 'model',
26
+ 'llm',
27
+ 'transformText',
28
+ 'splitText',
29
+ 'ifElse',
30
+ 'groupItems',
31
+ 'sliceAssets',
32
+ 'forEach',
33
+ 'forEachEnd',
34
+ 'stickyNote',
35
+ ];
36
+
37
+ export interface WorkflowExportData {
38
+ version: '1.0';
39
+ name: string;
40
+ description: string;
41
+ editorInfo: {
42
+ nodes: Array<{ type: WorkflowEditorNodeType; [key: string]: unknown }>;
43
+ edges: Array<{ [key: string]: unknown }>;
44
+ inputKeys: string[];
45
+ };
46
+ inputs: unknown[];
47
+ tagSet: string[];
48
+ uiConfig?: object;
49
+ exportedAt: string;
50
+ exportedBy: string;
51
+ }
52
+
53
+ /**
54
+ * Validates a workflow export payload (e.g. from a JSON file import).
55
+ *
56
+ * Throws a `WorkflowImportError` with a human-readable message on the first
57
+ * violation found. Returns the validated data cast to `WorkflowExportData`.
58
+ *
59
+ * Checks performed:
60
+ * 1. Data is a non-null object
61
+ * 2. `version` field is present and equals "1.0"
62
+ * 3. `editorInfo` has non-empty `nodes` and `edges` arrays
63
+ * 4. Each node's `type` is a recognized editor node type
64
+ * 5. `inputs` is an array
65
+ */
66
+ export function validateEditorInfo(data: unknown): WorkflowExportData {
67
+ if (typeof data !== 'object' || data === null) {
68
+ throw new WorkflowImportError('Invalid JSON format');
69
+ }
70
+
71
+ const workflow = data as Partial<WorkflowExportData>;
72
+
73
+ if (!workflow.version) {
74
+ throw new WorkflowImportError('Missing version field');
75
+ }
76
+
77
+ if (workflow.version !== '1.0') {
78
+ throw new WorkflowImportError(`Unsupported version: ${workflow.version}`);
79
+ }
80
+
81
+ if (!workflow.editorInfo || !workflow.editorInfo.nodes || !workflow.editorInfo.edges) {
82
+ throw new WorkflowImportError('Missing editorInfo structure');
83
+ }
84
+
85
+ if (!Array.isArray(workflow.editorInfo.nodes)) {
86
+ throw new WorkflowImportError('Invalid nodes array');
87
+ }
88
+
89
+ if (!Array.isArray(workflow.editorInfo.edges)) {
90
+ throw new WorkflowImportError('Invalid edges array');
91
+ }
92
+
93
+ if (!workflow.inputs || !Array.isArray(workflow.inputs)) {
94
+ throw new WorkflowImportError('Invalid inputs array');
95
+ }
96
+
97
+ for (const node of workflow.editorInfo.nodes) {
98
+ if (!(VALID_EDITOR_NODE_TYPES as readonly string[]).includes(node.type)) {
99
+ throw new WorkflowImportError(`Invalid node type: ${node.type}`);
100
+ }
101
+ }
102
+
103
+ return workflow as WorkflowExportData;
104
+ }
@@ -0,0 +1,2 @@
1
+ /** @deprecated Import from ./core/pagination instead */
2
+ export * from './core/pagination';