@scenario-labs/sdk 0.1.3 → 0.2.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.
- package/CHANGELOG.md +23 -0
- package/LICENSE +1 -1
- package/README.md +29 -29
- package/client.d.mts +35 -18
- package/client.d.mts.map +1 -1
- package/client.d.ts +35 -18
- package/client.d.ts.map +1 -1
- package/client.js +88 -50
- package/client.js.map +1 -1
- package/client.mjs +86 -48
- package/client.mjs.map +1 -1
- package/core/api-promise.d.mts +2 -2
- package/core/api-promise.d.mts.map +1 -1
- package/core/api-promise.d.ts +2 -2
- package/core/api-promise.d.ts.map +1 -1
- package/core/api-promise.js.map +1 -1
- package/core/api-promise.mjs.map +1 -1
- package/core/error.d.mts +2 -2
- package/core/error.d.mts.map +1 -1
- package/core/error.d.ts +2 -2
- package/core/error.d.ts.map +1 -1
- package/core/error.js +4 -4
- package/core/error.js.map +1 -1
- package/core/error.mjs +2 -2
- package/core/error.mjs.map +1 -1
- package/core/resource.d.mts +3 -3
- package/core/resource.d.mts.map +1 -1
- package/core/resource.d.ts +3 -3
- package/core/resource.d.ts.map +1 -1
- package/core/resource.js.map +1 -1
- package/core/resource.mjs.map +1 -1
- package/index.d.mts +8 -3
- package/index.d.mts.map +1 -1
- package/index.d.ts +8 -3
- package/index.d.ts.map +1 -1
- package/index.js +16 -6
- package/index.js.map +1 -1
- package/index.mjs +7 -3
- package/index.mjs.map +1 -1
- package/internal/parse.d.mts +2 -2
- package/internal/parse.d.mts.map +1 -1
- package/internal/parse.d.ts +2 -2
- package/internal/parse.d.ts.map +1 -1
- package/internal/parse.js.map +1 -1
- package/internal/parse.mjs.map +1 -1
- package/internal/request-options.d.mts +4 -0
- package/internal/request-options.d.mts.map +1 -1
- package/internal/request-options.d.ts +4 -0
- package/internal/request-options.d.ts.map +1 -1
- package/internal/request-options.js.map +1 -1
- package/internal/request-options.mjs.map +1 -1
- package/internal/shims.js +1 -1
- package/internal/shims.js.map +1 -1
- package/internal/shims.mjs +1 -1
- package/internal/shims.mjs.map +1 -1
- package/internal/uploads.d.mts +4 -4
- package/internal/uploads.d.mts.map +1 -1
- package/internal/uploads.d.ts +4 -4
- package/internal/uploads.d.ts.map +1 -1
- package/internal/uploads.js.map +1 -1
- package/internal/uploads.mjs.map +1 -1
- package/internal/utils/base64.js +2 -2
- package/internal/utils/base64.js.map +1 -1
- package/internal/utils/base64.mjs +3 -3
- package/internal/utils/base64.mjs.map +1 -1
- package/internal/utils/log.d.mts +3 -3
- package/internal/utils/log.d.mts.map +1 -1
- package/internal/utils/log.d.ts +3 -3
- package/internal/utils/log.d.ts.map +1 -1
- package/internal/utils/log.js.map +1 -1
- package/internal/utils/log.mjs.map +1 -1
- package/internal/utils/path.js +1 -1
- package/internal/utils/path.js.map +1 -1
- package/internal/utils/path.mjs +2 -2
- package/internal/utils/path.mjs.map +1 -1
- package/internal/utils/values.js +5 -5
- package/internal/utils/values.js.map +1 -1
- package/internal/utils/values.mjs +6 -6
- package/internal/utils/values.mjs.map +1 -1
- package/lib/generate.d.mts +19 -0
- package/lib/generate.d.mts.map +1 -0
- package/lib/generate.d.ts +19 -0
- package/lib/generate.d.ts.map +1 -0
- package/lib/generate.js +35 -0
- package/lib/generate.js.map +1 -0
- package/lib/generate.mjs +31 -0
- package/lib/generate.mjs.map +1 -0
- package/lib/job.d.mts +48 -0
- package/lib/job.d.mts.map +1 -0
- package/lib/job.d.ts +48 -0
- package/lib/job.d.ts.map +1 -0
- package/lib/job.js +50 -0
- package/lib/job.js.map +1 -0
- package/lib/job.mjs +45 -0
- package/lib/job.mjs.map +1 -0
- package/lib/models.d.mts +20 -0
- package/lib/models.d.mts.map +1 -0
- package/lib/models.d.ts +20 -0
- package/lib/models.d.ts.map +1 -0
- package/lib/models.js +28 -0
- package/lib/models.js.map +1 -0
- package/lib/models.mjs +23 -0
- package/lib/models.mjs.map +1 -0
- package/lib/scenario.d.mts +39 -0
- package/lib/scenario.d.mts.map +1 -0
- package/lib/scenario.d.ts +39 -0
- package/lib/scenario.d.ts.map +1 -0
- package/lib/scenario.js +46 -0
- package/lib/scenario.js.map +1 -0
- package/lib/scenario.mjs +42 -0
- package/lib/scenario.mjs.map +1 -0
- package/lib/workflow.d.mts +48 -0
- package/lib/workflow.d.mts.map +1 -0
- package/lib/workflow.d.ts +48 -0
- package/lib/workflow.d.ts.map +1 -0
- package/lib/workflow.js +59 -0
- package/lib/workflow.js.map +1 -0
- package/lib/workflow.mjs +54 -0
- package/lib/workflow.mjs.map +1 -0
- package/package.json +13 -3
- package/resources/assets/assets.d.mts +401 -96
- package/resources/assets/assets.d.mts.map +1 -1
- package/resources/assets/assets.d.ts +401 -96
- package/resources/assets/assets.d.ts.map +1 -1
- package/resources/assets/assets.js +2 -2
- package/resources/assets/assets.js.map +1 -1
- package/resources/assets/assets.mjs +2 -2
- package/resources/assets/assets.mjs.map +1 -1
- package/resources/assets/index.d.mts +1 -1
- package/resources/assets/index.d.mts.map +1 -1
- package/resources/assets/index.d.ts +1 -1
- package/resources/assets/index.d.ts.map +1 -1
- package/resources/assets/index.js.map +1 -1
- package/resources/assets/index.mjs.map +1 -1
- package/resources/assets/public.d.mts +30 -27
- package/resources/assets/public.d.mts.map +1 -1
- package/resources/assets/public.d.ts +30 -27
- package/resources/assets/public.d.ts.map +1 -1
- package/resources/assets/public.js +2 -2
- package/resources/assets/public.js.map +1 -1
- package/resources/assets/public.mjs +2 -2
- package/resources/assets/public.mjs.map +1 -1
- package/resources/collections/assets.d.mts +2 -8
- package/resources/collections/assets.d.mts.map +1 -1
- package/resources/collections/assets.d.ts +2 -8
- package/resources/collections/assets.d.ts.map +1 -1
- package/resources/collections/assets.js +2 -2
- package/resources/collections/assets.js.map +1 -1
- package/resources/collections/assets.mjs +2 -2
- package/resources/collections/assets.mjs.map +1 -1
- package/resources/collections/collections.d.mts +5 -5
- package/resources/collections/collections.d.mts.map +1 -1
- package/resources/collections/collections.d.ts +5 -5
- package/resources/collections/collections.d.ts.map +1 -1
- package/resources/collections/collections.js.map +1 -1
- package/resources/collections/collections.mjs.map +1 -1
- package/resources/collections/index.d.mts +2 -2
- package/resources/collections/index.d.mts.map +1 -1
- package/resources/collections/index.d.ts +2 -2
- package/resources/collections/index.d.ts.map +1 -1
- package/resources/collections/index.js.map +1 -1
- package/resources/collections/index.mjs +2 -2
- package/resources/collections/index.mjs.map +1 -1
- package/resources/collections/models.d.mts +2 -8
- package/resources/collections/models.d.mts.map +1 -1
- package/resources/collections/models.d.ts +2 -8
- package/resources/collections/models.d.ts.map +1 -1
- package/resources/collections/models.js +2 -2
- package/resources/collections/models.js.map +1 -1
- package/resources/collections/models.mjs +2 -2
- package/resources/collections/models.mjs.map +1 -1
- package/resources/generate.d.mts +4188 -1370
- package/resources/generate.d.mts.map +1 -1
- package/resources/generate.d.ts +4188 -1370
- package/resources/generate.d.ts.map +1 -1
- package/resources/index.d.mts +2 -2
- package/resources/index.d.mts.map +1 -1
- package/resources/index.d.ts +2 -2
- package/resources/index.d.ts.map +1 -1
- package/resources/index.js.map +1 -1
- package/resources/index.mjs.map +1 -1
- package/resources/jobs.d.mts +943 -33
- package/resources/jobs.d.mts.map +1 -1
- package/resources/jobs.d.ts +943 -33
- package/resources/jobs.d.ts.map +1 -1
- package/resources/jobs.js +3 -2
- package/resources/jobs.js.map +1 -1
- package/resources/jobs.mjs +3 -2
- package/resources/jobs.mjs.map +1 -1
- package/resources/models/description.d.mts +8 -8
- package/resources/models/description.d.mts.map +1 -1
- package/resources/models/description.d.ts +8 -8
- package/resources/models/description.d.ts.map +1 -1
- package/resources/models/examples.d.mts +640 -34
- package/resources/models/examples.d.mts.map +1 -1
- package/resources/models/examples.d.ts +640 -34
- package/resources/models/examples.d.ts.map +1 -1
- package/resources/models/models.d.mts +1323 -301
- package/resources/models/models.d.mts.map +1 -1
- package/resources/models/models.d.ts +1323 -301
- package/resources/models/models.d.ts.map +1 -1
- package/resources/models/models.js.map +1 -1
- package/resources/models/models.mjs.map +1 -1
- package/resources/models/train.d.mts +718 -90
- package/resources/models/train.d.mts.map +1 -1
- package/resources/models/train.d.ts +718 -90
- package/resources/models/train.d.ts.map +1 -1
- package/resources/models/train.js +9 -4
- package/resources/models/train.js.map +1 -1
- package/resources/models/train.mjs +9 -4
- package/resources/models/train.mjs.map +1 -1
- package/resources/models/training-images.d.mts +2 -2
- package/resources/models/training-images.d.mts.map +1 -1
- package/resources/models/training-images.d.ts +2 -2
- package/resources/models/training-images.d.ts.map +1 -1
- package/resources/search.d.mts +6 -31
- package/resources/search.d.mts.map +1 -1
- package/resources/search.d.ts +6 -31
- package/resources/search.d.ts.map +1 -1
- package/resources/search.js +6 -2
- package/resources/search.js.map +1 -1
- package/resources/search.mjs +6 -2
- package/resources/search.mjs.map +1 -1
- package/resources/tags.d.mts +1 -1
- package/resources/tags.d.ts +1 -1
- package/resources/usages.d.mts +401 -16
- package/resources/usages.d.mts.map +1 -1
- package/resources/usages.d.ts +401 -16
- package/resources/usages.d.ts.map +1 -1
- package/resources/workflows.d.mts +2738 -101
- package/resources/workflows.d.mts.map +1 -1
- package/resources/workflows.d.ts +2738 -101
- package/resources/workflows.d.ts.map +1 -1
- package/src/client.ts +102 -44
- package/src/core/api-promise.ts +4 -4
- package/src/core/error.ts +2 -2
- package/src/core/resource.ts +3 -3
- package/src/index.ts +8 -3
- package/src/internal/parse.ts +2 -2
- package/src/internal/request-options.ts +2 -0
- package/src/internal/shims.ts +1 -1
- package/src/internal/uploads.ts +5 -5
- package/src/internal/utils/base64.ts +3 -3
- package/src/internal/utils/log.ts +3 -3
- package/src/internal/utils/path.ts +2 -2
- package/src/internal/utils/values.ts +6 -6
- package/src/lib/generate.ts +77 -0
- package/src/lib/job.ts +78 -0
- package/src/lib/models.ts +27 -0
- package/src/lib/scenario.ts +39 -0
- package/src/lib/workflow.ts +90 -0
- package/src/resources/assets/assets.ts +742 -212
- package/src/resources/assets/index.ts +0 -1
- package/src/resources/assets/public.ts +151 -29
- package/src/resources/collections/assets.ts +2 -14
- package/src/resources/collections/collections.ts +3 -5
- package/src/resources/collections/index.ts +2 -14
- package/src/resources/collections/models.ts +2 -14
- package/src/resources/generate.ts +6254 -2217
- package/src/resources/index.ts +2 -3
- package/src/resources/jobs.ts +1355 -26
- package/src/resources/models/description.ts +50 -8
- package/src/resources/models/examples.ts +906 -56
- package/src/resources/models/models.ts +1867 -330
- package/src/resources/models/train.ts +976 -98
- package/src/resources/models/training-images.ts +2 -2
- package/src/resources/search.ts +13 -39
- package/src/resources/tags.ts +1 -1
- package/src/resources/usages.ts +544 -11
- package/src/resources/workflows.ts +4586 -914
- package/src/version.ts +1 -1
- package/version.d.mts +1 -1
- package/version.d.ts +1 -1
- package/version.js +1 -1
- package/version.mjs +1 -1
package/src/core/error.ts
CHANGED
|
@@ -2,13 +2,13 @@
|
|
|
2
2
|
|
|
3
3
|
import { castToError } from '../internal/errors';
|
|
4
4
|
|
|
5
|
-
export class
|
|
5
|
+
export class ScenarioError extends Error {}
|
|
6
6
|
|
|
7
7
|
export class APIError<
|
|
8
8
|
TStatus extends number | undefined = number | undefined,
|
|
9
9
|
THeaders extends Headers | undefined = Headers | undefined,
|
|
10
10
|
TError extends Object | undefined = Object | undefined,
|
|
11
|
-
> extends
|
|
11
|
+
> extends ScenarioError {
|
|
12
12
|
/** HTTP status for the response that caused the error */
|
|
13
13
|
readonly status: TStatus;
|
|
14
14
|
/** HTTP headers for the response that caused the error */
|
package/src/core/resource.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
|
|
2
2
|
|
|
3
|
-
import type {
|
|
3
|
+
import type { Scenario } from '../client';
|
|
4
4
|
|
|
5
5
|
export abstract class APIResource {
|
|
6
|
-
protected _client:
|
|
6
|
+
protected _client: Scenario;
|
|
7
7
|
|
|
8
|
-
constructor(client:
|
|
8
|
+
constructor(client: Scenario) {
|
|
9
9
|
this._client = client;
|
|
10
10
|
}
|
|
11
11
|
}
|
package/src/index.ts
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
|
|
2
2
|
|
|
3
|
-
export {
|
|
3
|
+
export { Scenario as default } from './lib/scenario';
|
|
4
4
|
|
|
5
5
|
export { type Uploadable, toFile } from './core/uploads';
|
|
6
6
|
export { APIPromise } from './core/api-promise';
|
|
7
|
-
export {
|
|
7
|
+
export { Scenario } from './lib/scenario';
|
|
8
|
+
export { type ClientOptions } from './client';
|
|
8
9
|
export {
|
|
9
|
-
|
|
10
|
+
ScenarioError,
|
|
10
11
|
APIError,
|
|
11
12
|
APIConnectionError,
|
|
12
13
|
APIConnectionTimeoutError,
|
|
@@ -20,3 +21,7 @@ export {
|
|
|
20
21
|
PermissionDeniedError,
|
|
21
22
|
UnprocessableEntityError,
|
|
22
23
|
} from './core/error';
|
|
24
|
+
export { Job, type WaitOptions, type WithJob } from './lib/job';
|
|
25
|
+
export { EnhancedWorkflows, WorkflowEntity } from './lib/workflow';
|
|
26
|
+
export { EnhancedGenerate } from './lib/generate';
|
|
27
|
+
export { EnhancedModels, EnhancedTrain } from './lib/models';
|
package/src/internal/parse.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
|
|
2
2
|
|
|
3
3
|
import type { FinalRequestOptions } from './request-options';
|
|
4
|
-
import { type
|
|
4
|
+
import { type Scenario } from '../client';
|
|
5
5
|
import { formatRequestDetails, loggerFor } from './utils/log';
|
|
6
6
|
|
|
7
7
|
export type APIResponseProps = {
|
|
@@ -13,7 +13,7 @@ export type APIResponseProps = {
|
|
|
13
13
|
startTime: number;
|
|
14
14
|
};
|
|
15
15
|
|
|
16
|
-
export async function defaultParseResponse<T>(client:
|
|
16
|
+
export async function defaultParseResponse<T>(client: Scenario, props: APIResponseProps): Promise<T> {
|
|
17
17
|
const { response, requestLogID, retryOfRequestLogID, startTime } = props;
|
|
18
18
|
const body = await (async () => {
|
|
19
19
|
// fetch refuses to read the body when the status code is 204.
|
package/src/internal/shims.ts
CHANGED
|
@@ -16,7 +16,7 @@ export function getDefaultFetch(): Fetch {
|
|
|
16
16
|
}
|
|
17
17
|
|
|
18
18
|
throw new Error(
|
|
19
|
-
'`fetch` is not defined as a global; Either pass `fetch` to the client, `new
|
|
19
|
+
'`fetch` is not defined as a global; Either pass `fetch` to the client, `new Scenario({ fetch })` or polyfill the global, `globalThis.fetch = fetch`',
|
|
20
20
|
);
|
|
21
21
|
}
|
|
22
22
|
|
package/src/internal/uploads.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { type RequestOptions } from './request-options';
|
|
2
2
|
import type { FilePropertyBag, Fetch } from './builtin-types';
|
|
3
|
-
import type {
|
|
3
|
+
import type { Scenario } from '../client';
|
|
4
4
|
import { ReadableStreamFrom } from './shims';
|
|
5
5
|
|
|
6
6
|
export type BlobPart = string | ArrayBuffer | ArrayBufferView | Blob | DataView;
|
|
@@ -74,7 +74,7 @@ export const isAsyncIterable = (value: any): value is AsyncIterable<any> =>
|
|
|
74
74
|
*/
|
|
75
75
|
export const maybeMultipartFormRequestOptions = async (
|
|
76
76
|
opts: RequestOptions,
|
|
77
|
-
fetch:
|
|
77
|
+
fetch: Scenario | Fetch,
|
|
78
78
|
): Promise<RequestOptions> => {
|
|
79
79
|
if (!hasUploadableValue(opts.body)) return opts;
|
|
80
80
|
|
|
@@ -85,7 +85,7 @@ type MultipartFormRequestOptions = Omit<RequestOptions, 'body'> & { body: unknow
|
|
|
85
85
|
|
|
86
86
|
export const multipartFormRequestOptions = async (
|
|
87
87
|
opts: MultipartFormRequestOptions,
|
|
88
|
-
fetch:
|
|
88
|
+
fetch: Scenario | Fetch,
|
|
89
89
|
): Promise<RequestOptions> => {
|
|
90
90
|
return { ...opts, body: await createForm(opts.body, fetch) };
|
|
91
91
|
};
|
|
@@ -98,7 +98,7 @@ const supportsFormDataMap = /* @__PURE__ */ new WeakMap<Fetch, Promise<boolean>>
|
|
|
98
98
|
* This function detects if the fetch function provided supports the global FormData object to avoid
|
|
99
99
|
* confusing error messages later on.
|
|
100
100
|
*/
|
|
101
|
-
function supportsFormData(fetchObject:
|
|
101
|
+
function supportsFormData(fetchObject: Scenario | Fetch): Promise<boolean> {
|
|
102
102
|
const fetch: Fetch = typeof fetchObject === 'function' ? fetchObject : (fetchObject as any).fetch;
|
|
103
103
|
const cached = supportsFormDataMap.get(fetch);
|
|
104
104
|
if (cached) return cached;
|
|
@@ -124,7 +124,7 @@ function supportsFormData(fetchObject: ScenarioSDK | Fetch): Promise<boolean> {
|
|
|
124
124
|
|
|
125
125
|
export const createForm = async <T = Record<string, unknown>>(
|
|
126
126
|
body: T | undefined,
|
|
127
|
-
fetch:
|
|
127
|
+
fetch: Scenario | Fetch,
|
|
128
128
|
): Promise<FormData> => {
|
|
129
129
|
if (!(await supportsFormData(fetch))) {
|
|
130
130
|
throw new TypeError(
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
|
|
2
2
|
|
|
3
|
-
import {
|
|
3
|
+
import { ScenarioError } from '../../core/error';
|
|
4
4
|
import { encodeUTF8 } from './bytes';
|
|
5
5
|
|
|
6
6
|
export const toBase64 = (data: string | Uint8Array | null | undefined): string => {
|
|
@@ -18,7 +18,7 @@ export const toBase64 = (data: string | Uint8Array | null | undefined): string =
|
|
|
18
18
|
return btoa(String.fromCharCode.apply(null, data as any));
|
|
19
19
|
}
|
|
20
20
|
|
|
21
|
-
throw new
|
|
21
|
+
throw new ScenarioError('Cannot generate base64 string; Expected `Buffer` or `btoa` to be defined');
|
|
22
22
|
};
|
|
23
23
|
|
|
24
24
|
export const fromBase64 = (str: string): Uint8Array => {
|
|
@@ -36,5 +36,5 @@ export const fromBase64 = (str: string): Uint8Array => {
|
|
|
36
36
|
return buf;
|
|
37
37
|
}
|
|
38
38
|
|
|
39
|
-
throw new
|
|
39
|
+
throw new ScenarioError('Cannot decode base64 string; Expected `Buffer` or `atob` to be defined');
|
|
40
40
|
};
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
|
|
2
2
|
|
|
3
3
|
import { hasOwn } from './values';
|
|
4
|
-
import { type
|
|
4
|
+
import { type Scenario } from '../../client';
|
|
5
5
|
import { RequestOptions } from '../request-options';
|
|
6
6
|
|
|
7
7
|
type LogFn = (message: string, ...rest: unknown[]) => void;
|
|
@@ -24,7 +24,7 @@ const levelNumbers = {
|
|
|
24
24
|
export const parseLogLevel = (
|
|
25
25
|
maybeLevel: string | undefined,
|
|
26
26
|
sourceName: string,
|
|
27
|
-
client:
|
|
27
|
+
client: Scenario,
|
|
28
28
|
): LogLevel | undefined => {
|
|
29
29
|
if (!maybeLevel) {
|
|
30
30
|
return undefined;
|
|
@@ -60,7 +60,7 @@ const noopLogger = {
|
|
|
60
60
|
|
|
61
61
|
let cachedLoggers = /* @__PURE__ */ new WeakMap<Logger, [LogLevel, Logger]>();
|
|
62
62
|
|
|
63
|
-
export function loggerFor(client:
|
|
63
|
+
export function loggerFor(client: Scenario): Logger {
|
|
64
64
|
const logger = client.logger;
|
|
65
65
|
const logLevel = client.logLevel ?? 'off';
|
|
66
66
|
if (!logger) {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { ScenarioError } from '../../core/error';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* Percent-encode everything that isn't safe to have in a path without encoding safe chars.
|
|
@@ -72,7 +72,7 @@ export const createPathTagFunction = (pathEncoder = encodeURIPath) =>
|
|
|
72
72
|
return acc + spaces + arrows;
|
|
73
73
|
}, '');
|
|
74
74
|
|
|
75
|
-
throw new
|
|
75
|
+
throw new ScenarioError(
|
|
76
76
|
`Path parameters result in path with invalid segments:\n${invalidSegments
|
|
77
77
|
.map((e) => e.error)
|
|
78
78
|
.join('\n')}\n${path}\n${underline}`,
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
|
|
2
2
|
|
|
3
|
-
import {
|
|
3
|
+
import { ScenarioError } from '../../core/error';
|
|
4
4
|
|
|
5
5
|
// https://url.spec.whatwg.org/#url-scheme-string
|
|
6
6
|
const startsWithSchemeRegexp = /^[a-z][a-z0-9+.-]*:/i;
|
|
@@ -39,7 +39,7 @@ export function isObj(obj: unknown): obj is Record<string, unknown> {
|
|
|
39
39
|
|
|
40
40
|
export const ensurePresent = <T>(value: T | null | undefined): T => {
|
|
41
41
|
if (value == null) {
|
|
42
|
-
throw new
|
|
42
|
+
throw new ScenarioError(`Expected a value to be given but received ${value} instead.`);
|
|
43
43
|
}
|
|
44
44
|
|
|
45
45
|
return value;
|
|
@@ -47,10 +47,10 @@ export const ensurePresent = <T>(value: T | null | undefined): T => {
|
|
|
47
47
|
|
|
48
48
|
export const validatePositiveInteger = (name: string, n: unknown): number => {
|
|
49
49
|
if (typeof n !== 'number' || !Number.isInteger(n)) {
|
|
50
|
-
throw new
|
|
50
|
+
throw new ScenarioError(`${name} must be an integer`);
|
|
51
51
|
}
|
|
52
52
|
if (n < 0) {
|
|
53
|
-
throw new
|
|
53
|
+
throw new ScenarioError(`${name} must be a positive integer`);
|
|
54
54
|
}
|
|
55
55
|
return n;
|
|
56
56
|
};
|
|
@@ -59,14 +59,14 @@ export const coerceInteger = (value: unknown): number => {
|
|
|
59
59
|
if (typeof value === 'number') return Math.round(value);
|
|
60
60
|
if (typeof value === 'string') return parseInt(value, 10);
|
|
61
61
|
|
|
62
|
-
throw new
|
|
62
|
+
throw new ScenarioError(`Could not coerce ${value} (type: ${typeof value}) into a number`);
|
|
63
63
|
};
|
|
64
64
|
|
|
65
65
|
export const coerceFloat = (value: unknown): number => {
|
|
66
66
|
if (typeof value === 'number') return value;
|
|
67
67
|
if (typeof value === 'string') return parseFloat(value);
|
|
68
68
|
|
|
69
|
-
throw new
|
|
69
|
+
throw new ScenarioError(`Could not coerce ${value} (type: ${typeof value}) into a number`);
|
|
70
70
|
};
|
|
71
71
|
|
|
72
72
|
export const coerceBoolean = (value: unknown): boolean => {
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import type { APIPromise } from '../core/api-promise';
|
|
2
|
+
import type { RequestOptions } from '../internal/request-options';
|
|
3
|
+
import {
|
|
4
|
+
Generate,
|
|
5
|
+
type GenerateCaptionParams,
|
|
6
|
+
type GenerateCaptionResponse,
|
|
7
|
+
type GenerateDescribeStyleParams,
|
|
8
|
+
type GenerateDescribeStyleResponse,
|
|
9
|
+
type GenerateDetectParams,
|
|
10
|
+
type GenerateDetectResponse,
|
|
11
|
+
type GenerateEmbedParams,
|
|
12
|
+
type GenerateEmbedResponse,
|
|
13
|
+
type GeneratePatchParams,
|
|
14
|
+
type GeneratePatchResponse,
|
|
15
|
+
type GenerateRunModelParams,
|
|
16
|
+
type GenerateRunModelResponse,
|
|
17
|
+
type GenerateTranslateParams,
|
|
18
|
+
type GenerateTranslateResponse,
|
|
19
|
+
} from '../resources/generate';
|
|
20
|
+
import { type WithJob, enhanceJob } from './job';
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Enhanced Generate resource.
|
|
24
|
+
* All original methods are inherited. Job-producing methods return
|
|
25
|
+
* responses where `job` has `.wait()`.
|
|
26
|
+
*/
|
|
27
|
+
export class EnhancedGenerate extends Generate {
|
|
28
|
+
override caption(
|
|
29
|
+
params: GenerateCaptionParams,
|
|
30
|
+
options?: RequestOptions,
|
|
31
|
+
): APIPromise<WithJob<GenerateCaptionResponse>> {
|
|
32
|
+
return enhanceJob(this._client, super.caption(params, options));
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
override describeStyle(
|
|
36
|
+
params: GenerateDescribeStyleParams,
|
|
37
|
+
options?: RequestOptions,
|
|
38
|
+
): APIPromise<WithJob<GenerateDescribeStyleResponse>> {
|
|
39
|
+
return enhanceJob(this._client, super.describeStyle(params, options));
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
override detect(
|
|
43
|
+
params: GenerateDetectParams,
|
|
44
|
+
options?: RequestOptions,
|
|
45
|
+
): APIPromise<WithJob<GenerateDetectResponse>> {
|
|
46
|
+
return enhanceJob(this._client, super.detect(params, options));
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
override embed(
|
|
50
|
+
params: GenerateEmbedParams,
|
|
51
|
+
options?: RequestOptions,
|
|
52
|
+
): APIPromise<WithJob<GenerateEmbedResponse>> {
|
|
53
|
+
return enhanceJob(this._client, super.embed(params, options));
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
override patch(
|
|
57
|
+
params: GeneratePatchParams,
|
|
58
|
+
options?: RequestOptions,
|
|
59
|
+
): APIPromise<WithJob<GeneratePatchResponse>> {
|
|
60
|
+
return enhanceJob(this._client, super.patch(params, options));
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
override runModel(
|
|
64
|
+
modelID: string,
|
|
65
|
+
params: GenerateRunModelParams,
|
|
66
|
+
options?: RequestOptions,
|
|
67
|
+
): APIPromise<WithJob<GenerateRunModelResponse>> {
|
|
68
|
+
return enhanceJob(this._client, super.runModel(modelID, params, options));
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
override translate(
|
|
72
|
+
params: GenerateTranslateParams,
|
|
73
|
+
options?: RequestOptions,
|
|
74
|
+
): APIPromise<WithJob<GenerateTranslateResponse>> {
|
|
75
|
+
return enhanceJob(this._client, super.translate(params, options));
|
|
76
|
+
}
|
|
77
|
+
}
|
package/src/lib/job.ts
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import type { Scenario } from '../client';
|
|
2
|
+
import type { APIPromise } from '../core/api-promise';
|
|
3
|
+
import type { JobRetrieveResponse } from '../resources/jobs';
|
|
4
|
+
|
|
5
|
+
export interface WaitOptions {
|
|
6
|
+
/** Polling interval in milliseconds. Default: 3000 */
|
|
7
|
+
intervalMs?: number;
|
|
8
|
+
/** Maximum wait time in milliseconds. Default: 120000 */
|
|
9
|
+
timeoutMs?: number;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
type JobStatus = JobRetrieveResponse.Job['status'];
|
|
13
|
+
|
|
14
|
+
const TERMINAL_STATUSES: Set<JobStatus> = new Set(['success', 'failure', 'canceled']);
|
|
15
|
+
|
|
16
|
+
/** Job methods added on top of the original job fields. */
|
|
17
|
+
export class JobMethods {
|
|
18
|
+
/** @internal */ declare readonly _client: Scenario;
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Poll until the job reaches a terminal status (success, failure, or canceled).
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* ```ts
|
|
25
|
+
* const response = await client.workflows.run(workflowId, { body });
|
|
26
|
+
* const completed = await response.job.wait();
|
|
27
|
+
* console.log(completed.status);
|
|
28
|
+
* ```
|
|
29
|
+
*/
|
|
30
|
+
async wait(this: Job, options?: WaitOptions): Promise<Job> {
|
|
31
|
+
const intervalMs = options?.intervalMs ?? 3_000;
|
|
32
|
+
const timeoutMs = options?.timeoutMs ?? 120_000;
|
|
33
|
+
let elapsed = 0;
|
|
34
|
+
|
|
35
|
+
while (elapsed < timeoutMs) {
|
|
36
|
+
const response = await this._client.jobs.retrieve(this.jobId);
|
|
37
|
+
|
|
38
|
+
if (TERMINAL_STATUSES.has(response.job.status)) {
|
|
39
|
+
return Job.from(this._client, response.job);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
await new Promise((resolve) => setTimeout(resolve, intervalMs));
|
|
43
|
+
elapsed += intervalMs;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
throw new Error(`Job ${this.jobId} did not complete within ${timeoutMs / 1_000}s`);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** @internal Create a Job from raw job data. */
|
|
50
|
+
static from(client: Scenario, data: JobRetrieveResponse['job']): Job {
|
|
51
|
+
const job = Object.assign(Object.create(JobMethods.prototype), data) as Job;
|
|
52
|
+
Object.defineProperty(job, '_client', { value: client, enumerable: false });
|
|
53
|
+
return job;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** A job with all original fields plus `.wait()`. */
|
|
58
|
+
export type Job = JobRetrieveResponse.Job & JobMethods;
|
|
59
|
+
export const Job = JobMethods;
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* @internal Helper type: adds `.wait()` to the `job` field of a response.
|
|
63
|
+
* Uses intersection so the original response type is preserved — a `WithJob<T>` is still assignable to `T`.
|
|
64
|
+
*/
|
|
65
|
+
export type WithJob<T extends { job: unknown }> = T & { job: JobMethods };
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* @internal Wrap `_thenUnwrap` to replace `response.job` with an enhanced Job.
|
|
69
|
+
*/
|
|
70
|
+
export function enhanceJob<T extends { job: { jobId: string; status: string } }>(
|
|
71
|
+
client: Scenario,
|
|
72
|
+
promise: APIPromise<T>,
|
|
73
|
+
): APIPromise<WithJob<T>> {
|
|
74
|
+
return promise._thenUnwrap((data) => ({
|
|
75
|
+
...data,
|
|
76
|
+
job: Job.from(client, data.job as JobRetrieveResponse['job']),
|
|
77
|
+
}));
|
|
78
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { APIPromise } from '../core/api-promise';
|
|
2
|
+
import type { RequestOptions } from '../internal/request-options';
|
|
3
|
+
import { Models } from '../resources/models/models';
|
|
4
|
+
import { Train, type TrainTriggerParams, type TrainTriggerResponse } from '../resources/models/train';
|
|
5
|
+
import { type WithJob, enhanceJob } from './job';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Enhanced Train resource.
|
|
9
|
+
* `trigger()` returns a response where `job` has `.wait()`.
|
|
10
|
+
*/
|
|
11
|
+
export class EnhancedTrain extends Train {
|
|
12
|
+
override trigger(
|
|
13
|
+
modelID: string,
|
|
14
|
+
params: TrainTriggerParams,
|
|
15
|
+
options?: RequestOptions,
|
|
16
|
+
): APIPromise<WithJob<TrainTriggerResponse>> {
|
|
17
|
+
return enhanceJob(this._client, super.trigger(modelID, params, options));
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Enhanced Models resource.
|
|
23
|
+
* All original methods inherited. `train` is an {@link EnhancedTrain}.
|
|
24
|
+
*/
|
|
25
|
+
export class EnhancedModels extends Models {
|
|
26
|
+
override train: EnhancedTrain = new EnhancedTrain(this._client);
|
|
27
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { Scenario as BaseScenario } from '../client';
|
|
2
|
+
import { EnhancedGenerate } from './generate';
|
|
3
|
+
import { EnhancedModels } from './models';
|
|
4
|
+
import { EnhancedWorkflows } from './workflow';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Enhanced Scenario client.
|
|
8
|
+
*
|
|
9
|
+
* Extends the generated client — all original methods are inherited.
|
|
10
|
+
* Job-producing methods return responses where `job` has `.wait()`.
|
|
11
|
+
* Workflow retrieve returns a response where `workflow` has `.findNode()`.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```ts
|
|
15
|
+
* import Scenario from '@scenario-labs/sdk';
|
|
16
|
+
*
|
|
17
|
+
* const client = new Scenario({ apiKey: '...' });
|
|
18
|
+
*
|
|
19
|
+
* // All generated methods work as-is
|
|
20
|
+
* const list = await client.assets.list({});
|
|
21
|
+
*
|
|
22
|
+
* // Enhanced: response.job.wait()
|
|
23
|
+
* const run = await client.workflows.run(workflowId, { body });
|
|
24
|
+
* const completed = await run.job.wait();
|
|
25
|
+
*
|
|
26
|
+
* // Enhanced: response.workflow.findNode()
|
|
27
|
+
* const wf = await client.workflows.retrieve(workflowId);
|
|
28
|
+
* wf.workflow.findNode('final_image');
|
|
29
|
+
*
|
|
30
|
+
* // Enhanced: generate job.wait()
|
|
31
|
+
* const gen = await client.generate.runModel(modelId, { body });
|
|
32
|
+
* const done = await gen.job.wait();
|
|
33
|
+
* ```
|
|
34
|
+
*/
|
|
35
|
+
export class Scenario extends BaseScenario {
|
|
36
|
+
override workflows: EnhancedWorkflows = new EnhancedWorkflows(this);
|
|
37
|
+
override generate: EnhancedGenerate = new EnhancedGenerate(this);
|
|
38
|
+
override models: EnhancedModels = new EnhancedModels(this);
|
|
39
|
+
}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import type { APIPromise } from '../core/api-promise';
|
|
2
|
+
import type { RequestOptions } from '../internal/request-options';
|
|
3
|
+
import {
|
|
4
|
+
Workflows,
|
|
5
|
+
type WorkflowApproveUserParams,
|
|
6
|
+
type WorkflowApproveUserResponse,
|
|
7
|
+
type WorkflowRetrieveResponse,
|
|
8
|
+
type WorkflowRunParams,
|
|
9
|
+
type WorkflowRunResponse,
|
|
10
|
+
} from '../resources/workflows';
|
|
11
|
+
import { type WithJob, enhanceJob } from './job';
|
|
12
|
+
|
|
13
|
+
type DefinitionFlow = WorkflowRetrieveResponse.Workflow.Flow;
|
|
14
|
+
type NodeType = DefinitionFlow['type'];
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Enhanced workflow object with helper methods.
|
|
18
|
+
* Has all original workflow fields plus `findNode()`, `getNodesByType()`.
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* ```ts
|
|
22
|
+
* const response = await client.workflows.retrieve(workflowId);
|
|
23
|
+
* response.workflow.findNode('final_image');
|
|
24
|
+
* response.workflow.getNodesByType('custom-model');
|
|
25
|
+
* response.workflow.name; // original fields still work
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
export class WorkflowEntity {
|
|
29
|
+
/** Find a node in the workflow definition by ID. */
|
|
30
|
+
findNode(
|
|
31
|
+
this: WorkflowEntity & WorkflowRetrieveResponse.Workflow,
|
|
32
|
+
nodeId: string,
|
|
33
|
+
): DefinitionFlow | undefined {
|
|
34
|
+
return this.flow.find((node) => node.id === nodeId);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** Get all nodes of a specific type from the workflow definition. */
|
|
38
|
+
getNodesByType(this: WorkflowEntity & WorkflowRetrieveResponse.Workflow, type: NodeType): DefinitionFlow[] {
|
|
39
|
+
return this.flow.filter((node) => node.type === type);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** @internal Create a WorkflowEntity from raw workflow data. */
|
|
43
|
+
static from(data: WorkflowRetrieveResponse.Workflow): WorkflowRetrieveResponse.Workflow & WorkflowEntity {
|
|
44
|
+
return Object.assign(Object.create(WorkflowEntity.prototype), data);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** Enhanced retrieve response with workflow entity methods. */
|
|
49
|
+
export type EnhancedWorkflowRetrieveResponse = Omit<WorkflowRetrieveResponse, 'workflow'> & {
|
|
50
|
+
workflow: WorkflowRetrieveResponse.Workflow & WorkflowEntity;
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Enhanced Workflows resource.
|
|
55
|
+
* Extends the generated {@link Workflows} — all original methods are inherited.
|
|
56
|
+
* Overrides `retrieve()`, `run()`, and `approveUser()` to return enriched response objects.
|
|
57
|
+
*/
|
|
58
|
+
export class EnhancedWorkflows extends Workflows {
|
|
59
|
+
/**
|
|
60
|
+
* Get workflow by ID. Returns an enhanced response where `workflow` has
|
|
61
|
+
* `findNode()` and `getNodesByType()`.
|
|
62
|
+
*/
|
|
63
|
+
override retrieve(
|
|
64
|
+
workflowID: string,
|
|
65
|
+
options?: RequestOptions,
|
|
66
|
+
): APIPromise<EnhancedWorkflowRetrieveResponse> {
|
|
67
|
+
return super.retrieve(workflowID, options)._thenUnwrap((data) => ({
|
|
68
|
+
...data,
|
|
69
|
+
workflow: WorkflowEntity.from(data.workflow),
|
|
70
|
+
}));
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** Run a workflow. Returns an enhanced response where `job` has `.wait()`. */
|
|
74
|
+
override run(
|
|
75
|
+
workflowID: string,
|
|
76
|
+
params: WorkflowRunParams,
|
|
77
|
+
options?: RequestOptions,
|
|
78
|
+
): APIPromise<WithJob<WorkflowRunResponse>> {
|
|
79
|
+
return enhanceJob(this._client, super.run(workflowID, params, options));
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** Approve a user approval node. Returns an enhanced response where `job` has `.wait()`. */
|
|
83
|
+
override approveUser(
|
|
84
|
+
workflowID: string,
|
|
85
|
+
body: WorkflowApproveUserParams,
|
|
86
|
+
options?: RequestOptions,
|
|
87
|
+
): APIPromise<WithJob<WorkflowApproveUserResponse>> {
|
|
88
|
+
return enhanceJob(this._client, super.approveUser(workflowID, body, options));
|
|
89
|
+
}
|
|
90
|
+
}
|