@layer_ai/sdk 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.
@@ -0,0 +1,70 @@
1
+ import type { EstimateInferencePriceV2Output, EstimateInferencePriceV2Request, ExecuteInferenceV2Output, ExecuteInferenceV2Request, GetForgeRunOutput, GetInferenceRunV2Output } from "../generated/types.js";
2
+ import type { HttpClient } from "../http.js";
3
+ export interface RunOptions {
4
+ workspaceId?: string;
5
+ /** Replay key. One is minted per request when this is omitted. */
6
+ idempotencyKey?: string;
7
+ signal?: AbortSignal;
8
+ }
9
+ export interface WaitOptions {
10
+ workspaceId?: string;
11
+ /** Give up after this long. Defaults to 30 minutes. */
12
+ timeoutMs?: number;
13
+ /** Called on every poll that finds the run still running. */
14
+ onProgress?: (run: GetInferenceRunV2Output) => void;
15
+ signal?: AbortSignal;
16
+ }
17
+ export interface GenerateOptions extends RunOptions, Omit<WaitOptions, "workspaceId"> {
18
+ /**
19
+ * Refuse the run if it is priced above this many Creative Units.
20
+ *
21
+ * Pricing runs through the same model selection the run will use, so the ceiling is
22
+ * checked against what the run will actually cost. Nothing is submitted when it trips.
23
+ */
24
+ maxCreativeUnits?: number;
25
+ }
26
+ /**
27
+ * Generation runs: price one, start one, wait for one.
28
+ *
29
+ * The API is asynchronous — a run is accepted and then polled — so waiting lives here
30
+ * rather than in every caller. `generate()` is the whole sequence in one call.
31
+ */
32
+ export declare class Inferences {
33
+ private readonly http;
34
+ private readonly workspace;
35
+ constructor(http: HttpClient, workspace: (override?: string) => Promise<string>);
36
+ /**
37
+ * Price a run without starting it.
38
+ *
39
+ * Read `has_sufficient_creative_units` before submitting: balance is not checked at
40
+ * submission, so an underfunded run is accepted and then fails with
41
+ * `INSUFFICIENT_BALANCE` rather than being refused up front.
42
+ */
43
+ estimate(request: EstimateInferencePriceV2Request, options?: RunOptions): Promise<EstimateInferencePriceV2Output>;
44
+ /** Start a run and return as soon as it is accepted. */
45
+ create(request: ExecuteInferenceV2Request, options?: RunOptions): Promise<ExecuteInferenceV2Output>;
46
+ /** The current state of a run, including its outputs once it is complete. */
47
+ get(inferenceId: string, options?: {
48
+ workspaceId?: string;
49
+ signal?: AbortSignal;
50
+ }): Promise<GetInferenceRunV2Output>;
51
+ /** Ask for a run to stop. Already-finished runs are unaffected. */
52
+ cancel(inferenceId: string, options?: {
53
+ workspaceId?: string;
54
+ signal?: AbortSignal;
55
+ }): Promise<GetForgeRunOutput>;
56
+ /**
57
+ * Poll a run until it finishes, and throw if it finished badly.
58
+ *
59
+ * The server says how often to ask, and that interval is honoured rather than guessed:
60
+ * it is how a busy deployment tells clients to back off.
61
+ */
62
+ wait(inferenceId: string, options?: WaitOptions): Promise<GetInferenceRunV2Output>;
63
+ /**
64
+ * Price a run, start it, and wait for its outputs.
65
+ *
66
+ * The whole sequence most callers want. Pass `maxCreativeUnits` to put a ceiling on
67
+ * what a single call may spend.
68
+ */
69
+ generate(request: ExecuteInferenceV2Request, options?: GenerateOptions): Promise<GetInferenceRunV2Output>;
70
+ }
@@ -0,0 +1,126 @@
1
+ import { LayerBudgetExceededError, LayerRunFailedError, LayerTimeoutError, } from "../errors.js";
2
+ /** Statuses a run does not move on from. */
3
+ const TERMINAL_FAILURES = new Set(["failed", "cancelled", "deleted"]);
4
+ /** Used only if the API stops saying how often to poll. */
5
+ const FALLBACK_POLL_MS = 2_000;
6
+ /** A ceiling on the total wait, so a wedged run cannot hang a pipeline forever. */
7
+ const DEFAULT_WAIT_TIMEOUT_MS = 30 * 60 * 1_000;
8
+ const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
9
+ /**
10
+ * Generation runs: price one, start one, wait for one.
11
+ *
12
+ * The API is asynchronous — a run is accepted and then polled — so waiting lives here
13
+ * rather than in every caller. `generate()` is the whole sequence in one call.
14
+ */
15
+ export class Inferences {
16
+ http;
17
+ workspace;
18
+ constructor(http, workspace) {
19
+ this.http = http;
20
+ this.workspace = workspace;
21
+ }
22
+ /**
23
+ * Price a run without starting it.
24
+ *
25
+ * Read `has_sufficient_creative_units` before submitting: balance is not checked at
26
+ * submission, so an underfunded run is accepted and then fails with
27
+ * `INSUFFICIENT_BALANCE` rather than being refused up front.
28
+ */
29
+ async estimate(request, options = {}) {
30
+ const id = await this.workspace(options.workspaceId);
31
+ return this.http.post(`/v2/workspaces/${id}/inferences/estimate`, {
32
+ body: request,
33
+ idempotencyKey: options.idempotencyKey,
34
+ signal: options.signal,
35
+ });
36
+ }
37
+ /** Start a run and return as soon as it is accepted. */
38
+ async create(request, options = {}) {
39
+ const id = await this.workspace(options.workspaceId);
40
+ return this.http.post(`/v2/workspaces/${id}/inferences`, {
41
+ body: request,
42
+ idempotencyKey: options.idempotencyKey,
43
+ signal: options.signal,
44
+ });
45
+ }
46
+ /** The current state of a run, including its outputs once it is complete. */
47
+ async get(inferenceId, options = {}) {
48
+ const id = await this.workspace(options.workspaceId);
49
+ return this.http.get(`/v2/workspaces/${id}/inferences/${inferenceId}`, {
50
+ signal: options.signal,
51
+ });
52
+ }
53
+ /** Ask for a run to stop. Already-finished runs are unaffected. */
54
+ async cancel(inferenceId, options = {}) {
55
+ const id = await this.workspace(options.workspaceId);
56
+ return this.http.post(`/v2/workspaces/${id}/inferences/${inferenceId}/cancel`, {
57
+ signal: options.signal,
58
+ });
59
+ }
60
+ /**
61
+ * Poll a run until it finishes, and throw if it finished badly.
62
+ *
63
+ * The server says how often to ask, and that interval is honoured rather than guessed:
64
+ * it is how a busy deployment tells clients to back off.
65
+ */
66
+ async wait(inferenceId, options = {}) {
67
+ const workspaceId = await this.workspace(options.workspaceId);
68
+ const timeoutMs = options.timeoutMs ?? DEFAULT_WAIT_TIMEOUT_MS;
69
+ const started = Date.now();
70
+ let intervalMs = FALLBACK_POLL_MS;
71
+ for (;;) {
72
+ const run = await this.get(inferenceId, { workspaceId, signal: options.signal });
73
+ const status = (run.status ?? "").toLowerCase();
74
+ if (status === "complete")
75
+ return run;
76
+ if (TERMINAL_FAILURES.has(status)) {
77
+ throw new LayerRunFailedError({
78
+ message: run.error ?? `Run ${inferenceId} ${status}.`,
79
+ runId: inferenceId,
80
+ status,
81
+ code: run.error_code ?? null,
82
+ });
83
+ }
84
+ if (run.poll_interval_seconds)
85
+ intervalMs = run.poll_interval_seconds * 1_000;
86
+ options.onProgress?.(run);
87
+ const remaining = timeoutMs - (Date.now() - started);
88
+ if (remaining <= 0) {
89
+ throw new LayerTimeoutError(`Gave up waiting for ${inferenceId} after ${Math.round(timeoutMs / 1_000)}s. ` +
90
+ "The run may still finish; fetch it again with inferences.get().");
91
+ }
92
+ // A server interval longer than the time left would sleep straight through the
93
+ // deadline — an hour's wait against a half-hour timeout.
94
+ await sleep(Math.min(intervalMs, remaining));
95
+ }
96
+ }
97
+ /**
98
+ * Price a run, start it, and wait for its outputs.
99
+ *
100
+ * The whole sequence most callers want. Pass `maxCreativeUnits` to put a ceiling on
101
+ * what a single call may spend.
102
+ */
103
+ async generate(request, options = {}) {
104
+ const workspaceId = await this.workspace(options.workspaceId);
105
+ if (options.maxCreativeUnits !== undefined) {
106
+ const estimate = await this.estimate(request, { workspaceId, signal: options.signal });
107
+ if (estimate.estimated_price_creative_units > options.maxCreativeUnits) {
108
+ throw new LayerBudgetExceededError({
109
+ estimatedCreativeUnits: estimate.estimated_price_creative_units,
110
+ maxCreativeUnits: options.maxCreativeUnits,
111
+ });
112
+ }
113
+ }
114
+ const accepted = await this.create(request, {
115
+ workspaceId,
116
+ idempotencyKey: options.idempotencyKey,
117
+ signal: options.signal,
118
+ });
119
+ return this.wait(accepted.inference_id, {
120
+ workspaceId,
121
+ timeoutMs: options.timeoutMs,
122
+ onProgress: options.onProgress,
123
+ signal: options.signal,
124
+ });
125
+ }
126
+ }
@@ -0,0 +1,42 @@
1
+ import { type ListOptions, type Page } from "../pagination.js";
2
+ import type { CreateProjectBody, ProjectDetail, ProjectMembersOutput, ProjectSummary, UpdateProjectBody } from "../generated/types.js";
3
+ import type { HttpClient } from "../http.js";
4
+ export interface ProjectListOptions extends ListOptions {
5
+ workspaceId?: string;
6
+ /** Include archived projects in the results. */
7
+ includeArchived?: boolean;
8
+ }
9
+ /** Projects: the folders a workspace's generations and files are organized into. */
10
+ export declare class Projects {
11
+ private readonly http;
12
+ private readonly workspace;
13
+ constructor(http: HttpClient, workspace: (override?: string) => Promise<string>);
14
+ list(options?: ProjectListOptions): Promise<Page<ProjectSummary>>;
15
+ listAll(options?: Omit<ProjectListOptions, "cursor">): AsyncGenerator<ProjectSummary, void, undefined>;
16
+ get(projectId: string, options?: {
17
+ workspaceId?: string;
18
+ signal?: AbortSignal;
19
+ }): Promise<ProjectDetail>;
20
+ create(body: CreateProjectBody, options?: {
21
+ workspaceId?: string;
22
+ idempotencyKey?: string;
23
+ signal?: AbortSignal;
24
+ }): Promise<ProjectDetail>;
25
+ update(projectId: string, body: UpdateProjectBody, options?: {
26
+ workspaceId?: string;
27
+ signal?: AbortSignal;
28
+ }): Promise<ProjectDetail>;
29
+ delete(projectId: string, options?: {
30
+ workspaceId?: string;
31
+ signal?: AbortSignal;
32
+ }): Promise<void>;
33
+ /** Add workspace members to a project. All must already be active workspace members. */
34
+ addMembers(projectId: string, userIds: string[], options?: {
35
+ workspaceId?: string;
36
+ signal?: AbortSignal;
37
+ }): Promise<ProjectMembersOutput>;
38
+ removeMember(projectId: string, userId: string, options?: {
39
+ workspaceId?: string;
40
+ signal?: AbortSignal;
41
+ }): Promise<void>;
42
+ }
@@ -0,0 +1,66 @@
1
+ import { walkPages } from "../pagination.js";
2
+ /** Projects: the folders a workspace's generations and files are organized into. */
3
+ export class Projects {
4
+ http;
5
+ workspace;
6
+ constructor(http, workspace) {
7
+ this.http = http;
8
+ this.workspace = workspace;
9
+ }
10
+ async list(options = {}) {
11
+ const id = await this.workspace(options.workspaceId);
12
+ const body = await this.http.get(`/v2/workspaces/${id}/projects`, {
13
+ query: {
14
+ limit: options.limit,
15
+ cursor: options.cursor,
16
+ include_archived: options.includeArchived,
17
+ },
18
+ signal: options.signal,
19
+ });
20
+ return { items: body.projects, pagination: body.pagination };
21
+ }
22
+ listAll(options = {}) {
23
+ return walkPages((cursor) => this.list({ ...options, cursor }));
24
+ }
25
+ async get(projectId, options = {}) {
26
+ const id = await this.workspace(options.workspaceId);
27
+ return this.http.get(`/v2/workspaces/${id}/projects/${projectId}`, {
28
+ signal: options.signal,
29
+ });
30
+ }
31
+ async create(body, options = {}) {
32
+ const id = await this.workspace(options.workspaceId);
33
+ return this.http.post(`/v2/workspaces/${id}/projects`, {
34
+ body,
35
+ idempotencyKey: options.idempotencyKey,
36
+ signal: options.signal,
37
+ });
38
+ }
39
+ async update(projectId, body, options = {}) {
40
+ const id = await this.workspace(options.workspaceId);
41
+ return this.http.patch(`/v2/workspaces/${id}/projects/${projectId}`, {
42
+ body,
43
+ signal: options.signal,
44
+ });
45
+ }
46
+ async delete(projectId, options = {}) {
47
+ const id = await this.workspace(options.workspaceId);
48
+ await this.http.delete(`/v2/workspaces/${id}/projects/${projectId}`, {
49
+ signal: options.signal,
50
+ });
51
+ }
52
+ /** Add workspace members to a project. All must already be active workspace members. */
53
+ async addMembers(projectId, userIds, options = {}) {
54
+ const id = await this.workspace(options.workspaceId);
55
+ return this.http.post(`/v2/workspaces/${id}/projects/${projectId}/members`, {
56
+ body: { user_ids: userIds },
57
+ signal: options.signal,
58
+ });
59
+ }
60
+ async removeMember(projectId, userId, options = {}) {
61
+ const id = await this.workspace(options.workspaceId);
62
+ await this.http.delete(`/v2/workspaces/${id}/projects/${projectId}/members/${userId}`, {
63
+ signal: options.signal,
64
+ });
65
+ }
66
+ }
@@ -0,0 +1,30 @@
1
+ import { type ListOptions, type Page } from "../pagination.js";
2
+ import type { CreateReferenceSetRequest, CreateReferenceSetResponse, GetReferenceSetOutput, ReferenceSetSummary } from "../generated/types.js";
3
+ import type { HttpClient } from "../http.js";
4
+ export interface ReferenceSetListOptions extends ListOptions {
5
+ workspaceId?: string;
6
+ /** Full-text search on name and description. */
7
+ search?: string;
8
+ }
9
+ /**
10
+ * Reference sets: the trained subjects, styles, and characters a generation can apply.
11
+ *
12
+ * Creating one uploads nothing — it groups files already in the workspace, which is what
13
+ * `files.upload()` returns ids for. Training one is `trainingRuns.start()`.
14
+ */
15
+ export declare class ReferenceSets {
16
+ private readonly http;
17
+ private readonly workspace;
18
+ constructor(http: HttpClient, workspace: (override?: string) => Promise<string>);
19
+ list(options?: ReferenceSetListOptions): Promise<Page<ReferenceSetSummary>>;
20
+ listAll(options?: Omit<ReferenceSetListOptions, "cursor">): AsyncGenerator<ReferenceSetSummary, void, undefined>;
21
+ get(referenceSetId: string, options?: {
22
+ workspaceId?: string;
23
+ signal?: AbortSignal;
24
+ }): Promise<GetReferenceSetOutput>;
25
+ create(body: CreateReferenceSetRequest, options?: {
26
+ workspaceId?: string;
27
+ idempotencyKey?: string;
28
+ signal?: AbortSignal;
29
+ }): Promise<CreateReferenceSetResponse>;
30
+ }
@@ -0,0 +1,40 @@
1
+ import { walkPages } from "../pagination.js";
2
+ /**
3
+ * Reference sets: the trained subjects, styles, and characters a generation can apply.
4
+ *
5
+ * Creating one uploads nothing — it groups files already in the workspace, which is what
6
+ * `files.upload()` returns ids for. Training one is `trainingRuns.start()`.
7
+ */
8
+ export class ReferenceSets {
9
+ http;
10
+ workspace;
11
+ constructor(http, workspace) {
12
+ this.http = http;
13
+ this.workspace = workspace;
14
+ }
15
+ async list(options = {}) {
16
+ const id = await this.workspace(options.workspaceId);
17
+ const body = await this.http.get(`/v2/workspaces/${id}/reference-sets`, {
18
+ query: { limit: options.limit, cursor: options.cursor, search: options.search },
19
+ signal: options.signal,
20
+ });
21
+ return { items: body.reference_sets, pagination: body.pagination };
22
+ }
23
+ listAll(options = {}) {
24
+ return walkPages((cursor) => this.list({ ...options, cursor }));
25
+ }
26
+ async get(referenceSetId, options = {}) {
27
+ const id = await this.workspace(options.workspaceId);
28
+ return this.http.get(`/v2/workspaces/${id}/reference-sets/${referenceSetId}`, {
29
+ signal: options.signal,
30
+ });
31
+ }
32
+ async create(body, options = {}) {
33
+ const id = await this.workspace(options.workspaceId);
34
+ return this.http.post(`/v2/workspaces/${id}/reference-sets`, {
35
+ body,
36
+ idempotencyKey: options.idempotencyKey,
37
+ signal: options.signal,
38
+ });
39
+ }
40
+ }
@@ -0,0 +1,24 @@
1
+ import type { ScoreFilesBody, ScoreFilesOutput, ScoringRuleSummary } from "../generated/types.js";
2
+ import type { HttpClient } from "../http.js";
3
+ /** Output scoring: the workspace's own quality rules, and running them over files. */
4
+ export declare class Scores {
5
+ private readonly http;
6
+ private readonly workspace;
7
+ constructor(http: HttpClient, workspace: (override?: string) => Promise<string>);
8
+ /** The scoring rules configured for this workspace. */
9
+ rules(options?: {
10
+ workspaceId?: string;
11
+ signal?: AbortSignal;
12
+ }): Promise<ScoringRuleSummary[]>;
13
+ /**
14
+ * Queue files for scoring.
15
+ *
16
+ * Scoring is asynchronous — this reports what was accepted, and the verdicts arrive on
17
+ * the files themselves, readable through `files.scores()`.
18
+ */
19
+ score(body: ScoreFilesBody, options?: {
20
+ workspaceId?: string;
21
+ idempotencyKey?: string;
22
+ signal?: AbortSignal;
23
+ }): Promise<ScoreFilesOutput>;
24
+ }
@@ -0,0 +1,31 @@
1
+ /** Output scoring: the workspace's own quality rules, and running them over files. */
2
+ export class Scores {
3
+ http;
4
+ workspace;
5
+ constructor(http, workspace) {
6
+ this.http = http;
7
+ this.workspace = workspace;
8
+ }
9
+ /** The scoring rules configured for this workspace. */
10
+ async rules(options = {}) {
11
+ const id = await this.workspace(options.workspaceId);
12
+ const body = await this.http.get(`/v2/workspaces/${id}/scoring-rules`, {
13
+ signal: options.signal,
14
+ });
15
+ return body.rules;
16
+ }
17
+ /**
18
+ * Queue files for scoring.
19
+ *
20
+ * Scoring is asynchronous — this reports what was accepted, and the verdicts arrive on
21
+ * the files themselves, readable through `files.scores()`.
22
+ */
23
+ async score(body, options = {}) {
24
+ const id = await this.workspace(options.workspaceId);
25
+ return this.http.post(`/v2/workspaces/${id}/scores`, {
26
+ body,
27
+ idempotencyKey: options.idempotencyKey,
28
+ signal: options.signal,
29
+ });
30
+ }
31
+ }
@@ -0,0 +1,50 @@
1
+ import { type ListOptions, type Page } from "../pagination.js";
2
+ import type { CancelTrainingRunResponse, EstimateTrainingPriceRequest, EstimateTrainingPriceResponse, GetTrainingRunResponse, StartTrainingRequest, StartTrainingResponse, TrainedModelResponse, TrainingRunSummary } from "../generated/types.js";
3
+ import type { HttpClient } from "../http.js";
4
+ export interface TrainingRunListOptions extends ListOptions {
5
+ workspaceId?: string;
6
+ /** Filter to one reference set. */
7
+ referenceSetId?: string;
8
+ /** Filter by coarse state. */
9
+ status?: "any" | "in_progress" | "terminal_success" | "terminal_failure";
10
+ }
11
+ export interface TrainingWaitOptions {
12
+ workspaceId?: string;
13
+ /** Give up after this long. Defaults to six hours. */
14
+ timeoutMs?: number;
15
+ onProgress?: (run: GetTrainingRunResponse) => void;
16
+ signal?: AbortSignal;
17
+ }
18
+ /** Training runs: teaching a reference set's look to a base model. */
19
+ export declare class TrainingRuns {
20
+ private readonly http;
21
+ private readonly workspace;
22
+ constructor(http: HttpClient, workspace: (override?: string) => Promise<string>);
23
+ list(options?: TrainingRunListOptions): Promise<Page<TrainingRunSummary>>;
24
+ listAll(options?: Omit<TrainingRunListOptions, "cursor">): AsyncGenerator<TrainingRunSummary, void, undefined>;
25
+ /** Price a training run without starting it. */
26
+ estimate(request: EstimateTrainingPriceRequest, options?: {
27
+ workspaceId?: string;
28
+ signal?: AbortSignal;
29
+ }): Promise<EstimateTrainingPriceResponse>;
30
+ start(request: StartTrainingRequest, options?: {
31
+ workspaceId?: string;
32
+ idempotencyKey?: string;
33
+ signal?: AbortSignal;
34
+ }): Promise<StartTrainingResponse>;
35
+ get(trainingRunId: string, options?: {
36
+ workspaceId?: string;
37
+ signal?: AbortSignal;
38
+ }): Promise<GetTrainingRunResponse>;
39
+ cancel(trainingRunId: string, options?: {
40
+ workspaceId?: string;
41
+ signal?: AbortSignal;
42
+ }): Promise<CancelTrainingRunResponse>;
43
+ /** The trained model a finished run produced, ready to pass as a reference set. */
44
+ model(trainingRunId: string, options?: {
45
+ workspaceId?: string;
46
+ signal?: AbortSignal;
47
+ }): Promise<TrainedModelResponse>;
48
+ /** Poll a training run until it finishes, and throw if it finished badly. */
49
+ wait(trainingRunId: string, options?: TrainingWaitOptions): Promise<GetTrainingRunResponse>;
50
+ }
@@ -0,0 +1,93 @@
1
+ import { LayerRunFailedError, LayerTimeoutError } from "../errors.js";
2
+ import { walkPages } from "../pagination.js";
3
+ /** Training is long — minutes to hours — so the default wait is generous. */
4
+ const DEFAULT_WAIT_TIMEOUT_MS = 6 * 60 * 60 * 1_000;
5
+ const FALLBACK_POLL_MS = 15_000;
6
+ const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
7
+ /** Training runs: teaching a reference set's look to a base model. */
8
+ export class TrainingRuns {
9
+ http;
10
+ workspace;
11
+ constructor(http, workspace) {
12
+ this.http = http;
13
+ this.workspace = workspace;
14
+ }
15
+ async list(options = {}) {
16
+ const id = await this.workspace(options.workspaceId);
17
+ const body = await this.http.get(`/v2/workspaces/${id}/training-runs`, {
18
+ query: {
19
+ limit: options.limit,
20
+ cursor: options.cursor,
21
+ reference_set_id: options.referenceSetId,
22
+ status_filter: options.status,
23
+ },
24
+ signal: options.signal,
25
+ });
26
+ return { items: body.runs, pagination: body.pagination };
27
+ }
28
+ listAll(options = {}) {
29
+ return walkPages((cursor) => this.list({ ...options, cursor }));
30
+ }
31
+ /** Price a training run without starting it. */
32
+ async estimate(request, options = {}) {
33
+ const id = await this.workspace(options.workspaceId);
34
+ return this.http.post(`/v2/workspaces/${id}/training-runs/estimate`, {
35
+ body: request,
36
+ signal: options.signal,
37
+ });
38
+ }
39
+ async start(request, options = {}) {
40
+ const id = await this.workspace(options.workspaceId);
41
+ return this.http.post(`/v2/workspaces/${id}/training-runs`, {
42
+ body: request,
43
+ idempotencyKey: options.idempotencyKey,
44
+ signal: options.signal,
45
+ });
46
+ }
47
+ async get(trainingRunId, options = {}) {
48
+ const id = await this.workspace(options.workspaceId);
49
+ return this.http.get(`/v2/workspaces/${id}/training-runs/${trainingRunId}`, {
50
+ signal: options.signal,
51
+ });
52
+ }
53
+ async cancel(trainingRunId, options = {}) {
54
+ const id = await this.workspace(options.workspaceId);
55
+ return this.http.post(`/v2/workspaces/${id}/training-runs/${trainingRunId}/cancel`, { signal: options.signal });
56
+ }
57
+ /** The trained model a finished run produced, ready to pass as a reference set. */
58
+ async model(trainingRunId, options = {}) {
59
+ const id = await this.workspace(options.workspaceId);
60
+ return this.http.get(`/v2/workspaces/${id}/training-runs/${trainingRunId}/model`, {
61
+ signal: options.signal,
62
+ });
63
+ }
64
+ /** Poll a training run until it finishes, and throw if it finished badly. */
65
+ async wait(trainingRunId, options = {}) {
66
+ const workspaceId = await this.workspace(options.workspaceId);
67
+ const timeoutMs = options.timeoutMs ?? DEFAULT_WAIT_TIMEOUT_MS;
68
+ const started = Date.now();
69
+ let intervalMs = FALLBACK_POLL_MS;
70
+ for (;;) {
71
+ const run = await this.get(trainingRunId, { workspaceId, signal: options.signal });
72
+ if (run.state === "terminal_success")
73
+ return run;
74
+ if (run.state === "terminal_failure") {
75
+ throw new LayerRunFailedError({
76
+ message: run.error ?? `Training run ${trainingRunId} failed.`,
77
+ runId: trainingRunId,
78
+ status: run.state,
79
+ code: run.error_code ?? null,
80
+ });
81
+ }
82
+ if (run.poll_interval_seconds)
83
+ intervalMs = run.poll_interval_seconds * 1_000;
84
+ options.onProgress?.(run);
85
+ const remaining = timeoutMs - (Date.now() - started);
86
+ if (remaining <= 0) {
87
+ throw new LayerTimeoutError(`Gave up waiting for training run ${trainingRunId} after ${Math.round(timeoutMs / 1_000)}s. ` +
88
+ "It may still finish; fetch it again with trainingRuns.get().");
89
+ }
90
+ await sleep(Math.min(intervalMs, remaining));
91
+ }
92
+ }
93
+ }
@@ -0,0 +1,37 @@
1
+ import { type ListOptions, type Page } from "../pagination.js";
2
+ import type { EstimateWorkflowPriceOutput, ExecuteWorkflowOutput, GetWorkflowRunOutput, WorkflowSummary } from "../generated/types.js";
3
+ import type { HttpClient } from "../http.js";
4
+ export interface WorkflowRunRequest {
5
+ /** Values for the workflow's declared inputs, keyed by input name. */
6
+ inputs: Record<string, unknown>;
7
+ /** Run inside an existing session instead of a new one. */
8
+ session_id?: string;
9
+ /** Name for the session the run creates. */
10
+ session_name?: string;
11
+ }
12
+ /** Saved multi-step pipelines — Blueprints — and the runs made from them. */
13
+ export declare class Workflows {
14
+ private readonly http;
15
+ private readonly workspace;
16
+ constructor(http: HttpClient, workspace: (override?: string) => Promise<string>);
17
+ list(options?: ListOptions & {
18
+ workspaceId?: string;
19
+ }): Promise<Page<WorkflowSummary>>;
20
+ listAll(options?: Omit<ListOptions & {
21
+ workspaceId?: string;
22
+ }, "cursor">): AsyncGenerator<WorkflowSummary, void, undefined>;
23
+ /** Price a workflow run without starting it. */
24
+ estimate(workflowId: string, inputs: Record<string, unknown>, options?: {
25
+ workspaceId?: string;
26
+ signal?: AbortSignal;
27
+ }): Promise<EstimateWorkflowPriceOutput>;
28
+ run(workflowId: string, request: WorkflowRunRequest, options?: {
29
+ workspaceId?: string;
30
+ idempotencyKey?: string;
31
+ signal?: AbortSignal;
32
+ }): Promise<ExecuteWorkflowOutput>;
33
+ getRun(workflowId: string, runId: string, options?: {
34
+ workspaceId?: string;
35
+ signal?: AbortSignal;
36
+ }): Promise<GetWorkflowRunOutput>;
37
+ }
@@ -0,0 +1,38 @@
1
+ import { walkPages } from "../pagination.js";
2
+ /** Saved multi-step pipelines — Blueprints — and the runs made from them. */
3
+ export class Workflows {
4
+ http;
5
+ workspace;
6
+ constructor(http, workspace) {
7
+ this.http = http;
8
+ this.workspace = workspace;
9
+ }
10
+ async list(options = {}) {
11
+ const id = await this.workspace(options.workspaceId);
12
+ const body = await this.http.get(`/v2/workspaces/${id}/workflows`, {
13
+ query: { limit: options.limit, cursor: options.cursor },
14
+ signal: options.signal,
15
+ });
16
+ return { items: body.workflows, pagination: body.pagination };
17
+ }
18
+ listAll(options = {}) {
19
+ return walkPages((cursor) => this.list({ ...options, cursor }));
20
+ }
21
+ /** Price a workflow run without starting it. */
22
+ async estimate(workflowId, inputs, options = {}) {
23
+ const id = await this.workspace(options.workspaceId);
24
+ return this.http.post(`/v2/workspaces/${id}/workflows/${workflowId}/estimate`, { body: { inputs }, signal: options.signal });
25
+ }
26
+ async run(workflowId, request, options = {}) {
27
+ const id = await this.workspace(options.workspaceId);
28
+ return this.http.post(`/v2/workspaces/${id}/workflows/${workflowId}/runs`, {
29
+ body: request,
30
+ idempotencyKey: options.idempotencyKey,
31
+ signal: options.signal,
32
+ });
33
+ }
34
+ async getRun(workflowId, runId, options = {}) {
35
+ const id = await this.workspace(options.workspaceId);
36
+ return this.http.get(`/v2/workspaces/${id}/workflows/${workflowId}/runs/${runId}`, { signal: options.signal });
37
+ }
38
+ }