@bitbybit-dev/cad-cloud-sdk 1.1.1 → 1.2.0-rc.1
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/dist/client.d.ts.map +1 -1
- package/dist/client.js +5 -15
- package/dist/client.js.map +1 -1
- package/dist/endpoints/cad.d.ts.map +1 -1
- package/dist/endpoints/cad.js +0 -3
- package/dist/endpoints/cad.js.map +1 -1
- package/dist/endpoints/convert.d.ts.map +1 -1
- package/dist/endpoints/convert.js +0 -3
- package/dist/endpoints/convert.js.map +1 -1
- package/dist/endpoints/files.d.ts.map +1 -1
- package/dist/endpoints/files.js +0 -3
- package/dist/endpoints/files.js.map +1 -1
- package/dist/endpoints/models.d.ts.map +1 -1
- package/dist/endpoints/models.js +0 -3
- package/dist/endpoints/models.js.map +1 -1
- package/dist/endpoints/tasks.d.ts.map +1 -1
- package/dist/endpoints/tasks.js +0 -3
- package/dist/endpoints/tasks.js.map +1 -1
- package/dist/errors.d.ts +4 -4
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js +0 -3
- package/dist/errors.js.map +1 -1
- package/dist/fetch-downloads.d.ts.map +1 -1
- package/dist/fetch-downloads.js +0 -3
- package/dist/fetch-downloads.js.map +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +0 -8
- package/dist/index.js.map +1 -1
- package/dist/polling.d.ts.map +1 -1
- package/dist/polling.js +0 -3
- package/dist/polling.js.map +1 -1
- package/dist/types/custom.d.ts.map +1 -1
- package/dist/types/custom.js +0 -5
- package/dist/types/custom.js.map +1 -1
- package/dist/types/generated.d.ts +19 -6
- package/dist/types/generated.d.ts.map +1 -1
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/index.js +0 -7
- package/dist/types/index.js.map +1 -1
- package/dist/types/pipeline-operations.d.ts +54 -46
- package/dist/types/pipeline-operations.d.ts.map +1 -1
- package/dist/types/pipeline-operations.js +1 -1
- package/dist/types/pipeline-operations.js.map +1 -1
- package/dist/unwrap.d.ts.map +1 -1
- package/dist/unwrap.js +0 -3
- package/dist/unwrap.js.map +1 -1
- package/dist/validation/validator.d.ts.map +1 -1
- package/dist/validation/validator.js +0 -8
- package/dist/validation/validator.js.map +1 -1
- package/package.json +8 -4
- package/src/__test__/helpers.ts +71 -0
- package/src/__test__/index.ts +1 -0
- package/src/client.test.ts +206 -0
- package/src/client.ts +153 -0
- package/src/endpoints/cad.test.ts +101 -0
- package/src/endpoints/cad.ts +53 -0
- package/src/endpoints/convert.test.ts +154 -0
- package/src/endpoints/convert.ts +83 -0
- package/src/endpoints/files.test.ts +170 -0
- package/src/endpoints/files.ts +89 -0
- package/src/endpoints/index.ts +5 -0
- package/src/endpoints/models.test.ts +228 -0
- package/src/endpoints/models.ts +131 -0
- package/src/endpoints/tasks.test.ts +193 -0
- package/src/endpoints/tasks.ts +93 -0
- package/src/errors.test.ts +57 -0
- package/src/errors.ts +21 -0
- package/src/fetch-downloads.test.ts +67 -0
- package/src/fetch-downloads.ts +18 -0
- package/src/index.ts +19 -0
- package/src/polling.test.ts +194 -0
- package/src/polling.ts +81 -0
- package/src/types/custom.ts +45 -0
- package/src/types/generated.ts +3136 -0
- package/src/types/index.ts +3 -0
- package/src/types/pipeline-operations.ts +9008 -0
- package/src/types/schema-exports.ts +228 -0
- package/src/unwrap.test.ts +75 -0
- package/src/unwrap.ts +20 -0
- package/src/validation/index.ts +1 -0
- package/src/validation/request-schemas.ts +1001 -0
- package/src/validation/validator.test.ts +218 -0
- package/src/validation/validator.ts +60 -0
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
import { describe, it, expect } from "vitest";
|
|
2
|
+
import { ModelsEndpoint } from "./models.js";
|
|
3
|
+
import { NO_WAIT, downloadsResponse, errorResponse, okResponse, spyFetcher, taskResponse } from "../__test__/helpers.js";
|
|
4
|
+
|
|
5
|
+
describe("ModelsEndpoint", () => {
|
|
6
|
+
describe("list", () => {
|
|
7
|
+
it("sends GET to /api/v1/models and returns data", async () => {
|
|
8
|
+
// Arrange
|
|
9
|
+
const modelList = { models: ["dragon-cup", "phone-nest"] };
|
|
10
|
+
const { fn, calls } = spyFetcher(okResponse(modelList));
|
|
11
|
+
const models = new ModelsEndpoint(fn);
|
|
12
|
+
|
|
13
|
+
// Act
|
|
14
|
+
const result = await models.list();
|
|
15
|
+
|
|
16
|
+
// Assert
|
|
17
|
+
expect(calls[0]!.method).toBe("GET");
|
|
18
|
+
expect(calls[0]!.path).toBe("/api/v1/models");
|
|
19
|
+
expect(result).toStrictEqual(modelList);
|
|
20
|
+
});
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
describe("getParams", () => {
|
|
24
|
+
it("sends GET to /api/v1/models/{name}/params", async () => {
|
|
25
|
+
// Arrange
|
|
26
|
+
const paramDef = { parameters: { height: { type: "number" } } };
|
|
27
|
+
const { fn, calls } = spyFetcher(okResponse(paramDef));
|
|
28
|
+
const models = new ModelsEndpoint(fn);
|
|
29
|
+
|
|
30
|
+
// Act
|
|
31
|
+
const result = await models.getParams("dragon-cup");
|
|
32
|
+
|
|
33
|
+
// Assert
|
|
34
|
+
expect(calls[0]!.method).toBe("GET");
|
|
35
|
+
expect(calls[0]!.path).toBe("/api/v1/models/dragon-cup/params");
|
|
36
|
+
expect(result).toStrictEqual(paramDef);
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it("encodes special characters in model name", async () => {
|
|
40
|
+
// Arrange
|
|
41
|
+
const { fn, calls } = spyFetcher(okResponse({}));
|
|
42
|
+
const models = new ModelsEndpoint(fn);
|
|
43
|
+
|
|
44
|
+
// Act
|
|
45
|
+
await models.getParams("my model");
|
|
46
|
+
|
|
47
|
+
// Assert
|
|
48
|
+
expect(calls[0]!.path).toBe("/api/v1/models/my%20model/params");
|
|
49
|
+
});
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
describe("getDefinitions", () => {
|
|
53
|
+
it("sends POST to /api/v1/models/definitions with names array", async () => {
|
|
54
|
+
// Arrange
|
|
55
|
+
const defs = { definitions: {} };
|
|
56
|
+
const { fn, calls } = spyFetcher(okResponse(defs));
|
|
57
|
+
const models = new ModelsEndpoint(fn);
|
|
58
|
+
|
|
59
|
+
// Act
|
|
60
|
+
const result = await models.getDefinitions(["dragon-cup", "phone-nest"]);
|
|
61
|
+
|
|
62
|
+
// Assert
|
|
63
|
+
expect(calls[0]!.method).toBe("POST");
|
|
64
|
+
expect(calls[0]!.path).toBe("/api/v1/models/definitions");
|
|
65
|
+
expect(calls[0]!.body).toStrictEqual({ names: ["dragon-cup", "phone-nest"] });
|
|
66
|
+
expect(result).toStrictEqual(defs);
|
|
67
|
+
});
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
describe("submit", () => {
|
|
71
|
+
it("sends POST to /api/v1/models/{name} with body", async () => {
|
|
72
|
+
// Arrange
|
|
73
|
+
const taskResult = { taskId: "t-1", status: "queued" };
|
|
74
|
+
const { fn, calls } = spyFetcher(okResponse(taskResult));
|
|
75
|
+
const models = new ModelsEndpoint(fn);
|
|
76
|
+
const body = { params: { height: 10 }, outputs: { formats: ["gltf"] } };
|
|
77
|
+
|
|
78
|
+
// Act
|
|
79
|
+
const result = await models.submit("dragon-cup", body as never);
|
|
80
|
+
|
|
81
|
+
// Assert
|
|
82
|
+
expect(calls[0]!.method).toBe("POST");
|
|
83
|
+
expect(calls[0]!.path).toBe("/api/v1/models/dragon-cup");
|
|
84
|
+
expect(calls[0]!.body).toStrictEqual(body);
|
|
85
|
+
expect(result).toStrictEqual(taskResult);
|
|
86
|
+
});
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
describe("batchSubmit", () => {
|
|
90
|
+
it("sends POST to /api/v1/models/{name}/batch", async () => {
|
|
91
|
+
// Arrange
|
|
92
|
+
const compoundResult = { taskId: "c-1", subTasks: [{ taskId: "s-1", index: 0 }] };
|
|
93
|
+
const { fn, calls } = spyFetcher(okResponse(compoundResult));
|
|
94
|
+
const models = new ModelsEndpoint(fn);
|
|
95
|
+
const body = { variations: [{ params: { height: 10 } }], outputs: { formats: ["gltf"] } };
|
|
96
|
+
|
|
97
|
+
// Act
|
|
98
|
+
const result = await models.batchSubmit("dragon-cup", body as never);
|
|
99
|
+
|
|
100
|
+
// Assert
|
|
101
|
+
expect(calls[0]!.method).toBe("POST");
|
|
102
|
+
expect(calls[0]!.path).toBe("/api/v1/models/dragon-cup/batch");
|
|
103
|
+
expect(result).toStrictEqual(compoundResult);
|
|
104
|
+
});
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
describe("run", () => {
|
|
108
|
+
it("submits the model, polls it, and returns its downloads", async () => {
|
|
109
|
+
// Arrange
|
|
110
|
+
const { fn, calls } = spyFetcher(
|
|
111
|
+
okResponse({ taskId: "t-1", status: "queued" }),
|
|
112
|
+
taskResponse("t-1", "completed"),
|
|
113
|
+
downloadsResponse({ format: "glb", url: "https://example.test/model.glb" }),
|
|
114
|
+
);
|
|
115
|
+
const models = new ModelsEndpoint(fn);
|
|
116
|
+
|
|
117
|
+
// Act
|
|
118
|
+
const result = await models.run("gear", { outputs: { glb: true } } as never, NO_WAIT);
|
|
119
|
+
|
|
120
|
+
// Assert
|
|
121
|
+
expect(calls.map((call) => call.path)).toStrictEqual([
|
|
122
|
+
"/api/v1/models/gear",
|
|
123
|
+
"/api/v1/tasks/t-1",
|
|
124
|
+
"/api/v1/tasks/t-1/results",
|
|
125
|
+
]);
|
|
126
|
+
expect(result).toStrictEqual({ taskId: "t-1", downloads: [{ format: "glb", url: "https://example.test/model.glb" }] });
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
it("escapes a model name that needs it", async () => {
|
|
130
|
+
// Arrange
|
|
131
|
+
const { fn, calls } = spyFetcher(
|
|
132
|
+
okResponse({ taskId: "t-1", status: "queued" }),
|
|
133
|
+
taskResponse("t-1", "completed"),
|
|
134
|
+
downloadsResponse(),
|
|
135
|
+
);
|
|
136
|
+
const models = new ModelsEndpoint(fn);
|
|
137
|
+
|
|
138
|
+
// Act
|
|
139
|
+
await models.run("gear/v2", { outputs: {} } as never, NO_WAIT);
|
|
140
|
+
|
|
141
|
+
// Assert
|
|
142
|
+
expect(calls[0]!.path).toBe("/api/v1/models/gear%2Fv2");
|
|
143
|
+
});
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
describe("batchSubmit", () => {
|
|
147
|
+
it("sends POST to the batch path and returns the compound task", async () => {
|
|
148
|
+
// Arrange
|
|
149
|
+
const compound = { taskId: "c-1", subTasks: [] };
|
|
150
|
+
const { fn, calls } = spyFetcher(okResponse(compound));
|
|
151
|
+
const models = new ModelsEndpoint(fn);
|
|
152
|
+
|
|
153
|
+
// Act
|
|
154
|
+
const result = await models.batchSubmit("gear", { variations: [] } as never);
|
|
155
|
+
|
|
156
|
+
// Assert
|
|
157
|
+
expect(calls[0]!.path).toBe("/api/v1/models/gear/batch");
|
|
158
|
+
expect(result).toStrictEqual(compound);
|
|
159
|
+
});
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
describe("batchRun", () => {
|
|
163
|
+
it("returns the downloads of every sub task that completed", async () => {
|
|
164
|
+
// Arrange
|
|
165
|
+
const { fn } = spyFetcher(
|
|
166
|
+
okResponse({ taskId: "c-1", subTasks: [{ taskId: "s-1", index: 0, status: "queued" }] }),
|
|
167
|
+
taskResponse("c-1", "completed", { subTasks: [{ taskId: "s-1", index: 0, status: "completed" }] }),
|
|
168
|
+
downloadsResponse({ format: "glb", url: "https://example.test/0.glb" }),
|
|
169
|
+
);
|
|
170
|
+
const models = new ModelsEndpoint(fn);
|
|
171
|
+
|
|
172
|
+
// Act
|
|
173
|
+
const result = await models.batchRun("gear", { variations: [] } as never, NO_WAIT);
|
|
174
|
+
|
|
175
|
+
// Assert
|
|
176
|
+
expect(result.subTasks).toStrictEqual([
|
|
177
|
+
{ taskId: "s-1", index: 0, downloads: [{ format: "glb", url: "https://example.test/0.glb" }] },
|
|
178
|
+
]);
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
it("returns no downloads for a sub task that did not complete", async () => {
|
|
182
|
+
// Arrange
|
|
183
|
+
const { fn } = spyFetcher(
|
|
184
|
+
okResponse({ taskId: "c-1", subTasks: [{ taskId: "s-1", index: 0, status: "queued" }] }),
|
|
185
|
+
taskResponse("c-1", "completed", { subTasks: [{ taskId: "s-1", index: 0, status: "failed" }] }),
|
|
186
|
+
);
|
|
187
|
+
const models = new ModelsEndpoint(fn);
|
|
188
|
+
|
|
189
|
+
// Act
|
|
190
|
+
const result = await models.batchRun("gear", { variations: [] } as never, NO_WAIT);
|
|
191
|
+
|
|
192
|
+
// Assert
|
|
193
|
+
expect(result.subTasks).toStrictEqual([{ taskId: "s-1", index: 0, downloads: [] }]);
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
it("returns no downloads for a sub task whose results cannot be fetched", async () => {
|
|
197
|
+
// Arrange
|
|
198
|
+
const { fn } = spyFetcher(
|
|
199
|
+
okResponse({ taskId: "c-1", subTasks: [{ taskId: "s-1", index: 0, status: "queued" }] }),
|
|
200
|
+
taskResponse("c-1", "completed", { subTasks: [{ taskId: "s-1", index: 0, status: "completed" }] }),
|
|
201
|
+
errorResponse("NOT_FOUND", "results are gone", 404),
|
|
202
|
+
);
|
|
203
|
+
const models = new ModelsEndpoint(fn);
|
|
204
|
+
|
|
205
|
+
// Act
|
|
206
|
+
const result = await models.batchRun("gear", { variations: [] } as never, NO_WAIT);
|
|
207
|
+
|
|
208
|
+
// Assert
|
|
209
|
+
expect(result.subTasks).toStrictEqual([{ taskId: "s-1", index: 0, downloads: [] }]);
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
it("falls back to the sub tasks the submission reported when the compound task lists none", async () => {
|
|
213
|
+
// Arrange
|
|
214
|
+
const { fn } = spyFetcher(
|
|
215
|
+
okResponse({ taskId: "c-1", subTasks: [{ taskId: "s-1", index: 0, status: "completed" }] }),
|
|
216
|
+
taskResponse("c-1", "completed"),
|
|
217
|
+
downloadsResponse({ format: "glb", url: "https://example.test/0.glb" }),
|
|
218
|
+
);
|
|
219
|
+
const models = new ModelsEndpoint(fn);
|
|
220
|
+
|
|
221
|
+
// Act
|
|
222
|
+
const result = await models.batchRun("gear", { variations: [] } as never, NO_WAIT);
|
|
223
|
+
|
|
224
|
+
// Assert
|
|
225
|
+
expect(result.subTasks[0]!.downloads).toHaveLength(1);
|
|
226
|
+
});
|
|
227
|
+
});
|
|
228
|
+
});
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
ModelList,
|
|
3
|
+
ModelDefinition,
|
|
4
|
+
ModelBatchDefinitions,
|
|
5
|
+
ModelSubmission,
|
|
6
|
+
BatchModelSubmission,
|
|
7
|
+
ModelParamMap,
|
|
8
|
+
OutputOptions,
|
|
9
|
+
TaskCreatedResult,
|
|
10
|
+
CompoundTaskCreatedResult,
|
|
11
|
+
CompoundTaskDetail,
|
|
12
|
+
SubTaskSummary,
|
|
13
|
+
TaskDownload,
|
|
14
|
+
} from "../types/index.js";
|
|
15
|
+
import type { PollOptions } from "../polling.js";
|
|
16
|
+
import { pollTask } from "../polling.js";
|
|
17
|
+
import { unwrap } from "../unwrap.js";
|
|
18
|
+
import { fetchDownloads } from "../fetch-downloads.js";
|
|
19
|
+
|
|
20
|
+
type Fetcher = (method: string, path: string, body?: unknown) => Promise<Response>;
|
|
21
|
+
|
|
22
|
+
export class ModelsEndpoint {
|
|
23
|
+
constructor(private readonly fetch: Fetcher) {}
|
|
24
|
+
|
|
25
|
+
/** List all registered parametric model names. */
|
|
26
|
+
async list(): Promise<ModelList> {
|
|
27
|
+
return unwrap(await this.fetch("GET", "/api/v1/models"));
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** Get parameter definitions for a single model. */
|
|
31
|
+
async getParams(modelName: string): Promise<ModelDefinition> {
|
|
32
|
+
return unwrap(await this.fetch("GET", `/api/v1/models/${encodeURIComponent(modelName)}/params`));
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** Batch-fetch parameter definitions for multiple models. */
|
|
36
|
+
async getDefinitions(names: string[]): Promise<ModelBatchDefinitions> {
|
|
37
|
+
return unwrap(await this.fetch("POST", "/api/v1/models/definitions", { names }));
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Submit a model for generation. Returns immediately with task info.
|
|
42
|
+
* Use `runAndPoll` for a higher-level API that waits for the result.
|
|
43
|
+
*/
|
|
44
|
+
async submit<K extends string>(
|
|
45
|
+
modelName: K,
|
|
46
|
+
body: K extends keyof ModelParamMap
|
|
47
|
+
? ModelSubmission<ModelParamMap[K]>
|
|
48
|
+
: ModelSubmission,
|
|
49
|
+
): Promise<TaskCreatedResult> {
|
|
50
|
+
const res = await this.fetch("POST", `/api/v1/models/${encodeURIComponent(modelName)}`, body);
|
|
51
|
+
return unwrap(res);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Submit a model, poll until complete, and return download URLs for all requested formats.
|
|
56
|
+
* Metadata is always included when available.
|
|
57
|
+
*
|
|
58
|
+
* This is the main high-level API for generating a model and getting results.
|
|
59
|
+
*/
|
|
60
|
+
async run<K extends string>(
|
|
61
|
+
modelName: K,
|
|
62
|
+
body: K extends keyof ModelParamMap
|
|
63
|
+
? { params?: ModelParamMap[K]; outputs: OutputOptions }
|
|
64
|
+
: ModelSubmission,
|
|
65
|
+
opts?: PollOptions,
|
|
66
|
+
): Promise<{ taskId: string; downloads: TaskDownload[] }> {
|
|
67
|
+
const res = await this.fetch("POST", `/api/v1/models/${encodeURIComponent(modelName)}`, body);
|
|
68
|
+
const created = await unwrap<TaskCreatedResult>(res);
|
|
69
|
+
const task = await pollTask(this.fetch, created.taskId, opts);
|
|
70
|
+
|
|
71
|
+
const downloads = await fetchDownloads(this.fetch, task.taskId);
|
|
72
|
+
return { taskId: task.taskId, downloads };
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Submit a batch of model variations for parallel generation.
|
|
77
|
+
* Returns immediately with compound task info and sub-task IDs.
|
|
78
|
+
*/
|
|
79
|
+
async batchSubmit<K extends string>(
|
|
80
|
+
modelName: K,
|
|
81
|
+
body: K extends keyof ModelParamMap
|
|
82
|
+
? BatchModelSubmission<ModelParamMap[K]>
|
|
83
|
+
: BatchModelSubmission,
|
|
84
|
+
): Promise<CompoundTaskCreatedResult> {
|
|
85
|
+
const res = await this.fetch(
|
|
86
|
+
"POST",
|
|
87
|
+
`/api/v1/models/${encodeURIComponent(modelName)}/batch`,
|
|
88
|
+
body,
|
|
89
|
+
);
|
|
90
|
+
return unwrap(res);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Submit a batch, poll the compound task until all sub-tasks finish,
|
|
95
|
+
* and return download URLs for each sub-task (all requested formats + metadata).
|
|
96
|
+
*/
|
|
97
|
+
async batchRun<K extends string>(
|
|
98
|
+
modelName: K,
|
|
99
|
+
body: K extends keyof ModelParamMap
|
|
100
|
+
? BatchModelSubmission<ModelParamMap[K]>
|
|
101
|
+
: BatchModelSubmission,
|
|
102
|
+
opts?: PollOptions,
|
|
103
|
+
): Promise<{
|
|
104
|
+
taskId: string;
|
|
105
|
+
subTasks: readonly { taskId: string; index: number; downloads: TaskDownload[] }[];
|
|
106
|
+
}> {
|
|
107
|
+
const res = await this.fetch(
|
|
108
|
+
"POST",
|
|
109
|
+
`/api/v1/models/${encodeURIComponent(modelName)}/batch`,
|
|
110
|
+
body,
|
|
111
|
+
);
|
|
112
|
+
const compound = await unwrap<CompoundTaskCreatedResult>(res);
|
|
113
|
+
const task = await pollTask(this.fetch, compound.taskId, opts) as CompoundTaskDetail;
|
|
114
|
+
|
|
115
|
+
const subTasks = await Promise.all(
|
|
116
|
+
(task.subTasks ?? compound.subTasks).map(async (sub: SubTaskSummary) => {
|
|
117
|
+
if (sub.status !== "completed") {
|
|
118
|
+
return { taskId: sub.taskId, index: sub.index, downloads: [] as TaskDownload[] };
|
|
119
|
+
}
|
|
120
|
+
try {
|
|
121
|
+
const downloads = await fetchDownloads(this.fetch, sub.taskId);
|
|
122
|
+
return { taskId: sub.taskId, index: sub.index, downloads };
|
|
123
|
+
} catch {
|
|
124
|
+
return { taskId: sub.taskId, index: sub.index, downloads: [] as TaskDownload[] };
|
|
125
|
+
}
|
|
126
|
+
}),
|
|
127
|
+
);
|
|
128
|
+
|
|
129
|
+
return { taskId: compound.taskId, subTasks };
|
|
130
|
+
}
|
|
131
|
+
}
|
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
import { describe, it, expect } from "vitest";
|
|
2
|
+
import { TasksEndpoint } from "./tasks.js";
|
|
3
|
+
import { NO_WAIT, downloadsResponse, okResponse, spyFetcher, taskResponse } from "../__test__/helpers.js";
|
|
4
|
+
|
|
5
|
+
describe("TasksEndpoint", () => {
|
|
6
|
+
describe("get", () => {
|
|
7
|
+
it("sends GET to /api/v1/tasks/{id} and returns task detail", async () => {
|
|
8
|
+
// Arrange
|
|
9
|
+
const task = { taskId: "t-1", status: "completed", kind: "model" };
|
|
10
|
+
const { fn, calls } = spyFetcher(okResponse(task));
|
|
11
|
+
const tasks = new TasksEndpoint(fn);
|
|
12
|
+
|
|
13
|
+
// Act
|
|
14
|
+
const result = await tasks.get("t-1");
|
|
15
|
+
|
|
16
|
+
// Assert
|
|
17
|
+
expect(calls[0]!.method).toBe("GET");
|
|
18
|
+
expect(calls[0]!.path).toBe("/api/v1/tasks/t-1");
|
|
19
|
+
expect(result).toStrictEqual(task);
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
it("encodes special characters in taskId", async () => {
|
|
23
|
+
// Arrange
|
|
24
|
+
const { fn, calls } = spyFetcher(okResponse({}));
|
|
25
|
+
const tasks = new TasksEndpoint(fn);
|
|
26
|
+
|
|
27
|
+
// Act
|
|
28
|
+
await tasks.get("task/special");
|
|
29
|
+
|
|
30
|
+
// Assert
|
|
31
|
+
expect(calls[0]!.path).toBe("/api/v1/tasks/task%2Fspecial");
|
|
32
|
+
});
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
describe("list", () => {
|
|
36
|
+
it("sends GET to /api/v1/tasks without query when none provided", async () => {
|
|
37
|
+
// Arrange
|
|
38
|
+
const taskList = { tasks: [], total: 0 };
|
|
39
|
+
const { fn, calls } = spyFetcher(okResponse(taskList));
|
|
40
|
+
const tasks = new TasksEndpoint(fn);
|
|
41
|
+
|
|
42
|
+
// Act
|
|
43
|
+
const result = await tasks.list();
|
|
44
|
+
|
|
45
|
+
// Assert
|
|
46
|
+
expect(calls[0]!.method).toBe("GET");
|
|
47
|
+
expect(calls[0]!.path).toBe("/api/v1/tasks");
|
|
48
|
+
expect(result).toStrictEqual(taskList);
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it("appends query parameters when provided", async () => {
|
|
52
|
+
// Arrange
|
|
53
|
+
const { fn, calls } = spyFetcher(okResponse({ tasks: [], total: 0 }));
|
|
54
|
+
const tasks = new TasksEndpoint(fn);
|
|
55
|
+
|
|
56
|
+
// Act
|
|
57
|
+
await tasks.list({ page: 2, limit: 10, status: "completed", kind: "model" });
|
|
58
|
+
|
|
59
|
+
// Assert
|
|
60
|
+
expect(calls[0]!.path).toContain("page=2");
|
|
61
|
+
expect(calls[0]!.path).toContain("limit=10");
|
|
62
|
+
expect(calls[0]!.path).toContain("status=completed");
|
|
63
|
+
expect(calls[0]!.path).toContain("kind=model");
|
|
64
|
+
});
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
describe("getResult", () => {
|
|
68
|
+
it("sends GET to /api/v1/tasks/{id}/result without format", async () => {
|
|
69
|
+
// Arrange
|
|
70
|
+
const { fn, calls } = spyFetcher(okResponse({ downloadUrl: "https://example.com/r" }));
|
|
71
|
+
const tasks = new TasksEndpoint(fn);
|
|
72
|
+
|
|
73
|
+
// Act
|
|
74
|
+
await tasks.getResult("t-1");
|
|
75
|
+
|
|
76
|
+
// Assert
|
|
77
|
+
expect(calls[0]!.path).toBe("/api/v1/tasks/t-1/result");
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
it("appends format to path when provided", async () => {
|
|
81
|
+
// Arrange
|
|
82
|
+
const { fn, calls } = spyFetcher(okResponse({ downloadUrl: "https://example.com/r" }));
|
|
83
|
+
const tasks = new TasksEndpoint(fn);
|
|
84
|
+
|
|
85
|
+
// Act
|
|
86
|
+
await tasks.getResult("t-1", "glb");
|
|
87
|
+
|
|
88
|
+
// Assert
|
|
89
|
+
expect(calls[0]!.path).toBe("/api/v1/tasks/t-1/result/glb");
|
|
90
|
+
});
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
describe("getResults", () => {
|
|
94
|
+
it("sends GET to /api/v1/tasks/{id}/results and returns downloads array", async () => {
|
|
95
|
+
// Arrange
|
|
96
|
+
const downloads = [{ format: "glb", downloadUrl: "https://example.com/r.glb" }];
|
|
97
|
+
const { fn, calls } = spyFetcher(okResponse({ downloads }));
|
|
98
|
+
const tasks = new TasksEndpoint(fn);
|
|
99
|
+
|
|
100
|
+
// Act
|
|
101
|
+
const result = await tasks.getResults("t-1");
|
|
102
|
+
|
|
103
|
+
// Assert
|
|
104
|
+
expect(calls[0]!.path).toBe("/api/v1/tasks/t-1/results");
|
|
105
|
+
expect(result).toStrictEqual(downloads);
|
|
106
|
+
});
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
describe("cancel", () => {
|
|
110
|
+
it("sends DELETE to /api/v1/tasks/{id}", async () => {
|
|
111
|
+
// Arrange
|
|
112
|
+
const { fn, calls } = spyFetcher(okResponse({ cancelled: true }));
|
|
113
|
+
const tasks = new TasksEndpoint(fn);
|
|
114
|
+
|
|
115
|
+
// Act
|
|
116
|
+
const result = await tasks.cancel("t-1");
|
|
117
|
+
|
|
118
|
+
// Assert
|
|
119
|
+
expect(calls[0]!.method).toBe("DELETE");
|
|
120
|
+
expect(calls[0]!.path).toBe("/api/v1/tasks/t-1");
|
|
121
|
+
expect(result).toStrictEqual({ cancelled: true });
|
|
122
|
+
});
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
describe("retry", () => {
|
|
126
|
+
it("sends POST to /api/v1/tasks/{id}/retry", async () => {
|
|
127
|
+
// Arrange
|
|
128
|
+
const taskResult = { taskId: "t-2", status: "queued" };
|
|
129
|
+
const { fn, calls } = spyFetcher(okResponse(taskResult));
|
|
130
|
+
const tasks = new TasksEndpoint(fn);
|
|
131
|
+
|
|
132
|
+
// Act
|
|
133
|
+
const result = await tasks.retry("t-1");
|
|
134
|
+
|
|
135
|
+
// Assert
|
|
136
|
+
expect(calls[0]!.method).toBe("POST");
|
|
137
|
+
expect(calls[0]!.path).toBe("/api/v1/tasks/t-1/retry");
|
|
138
|
+
expect(result).toStrictEqual(taskResult);
|
|
139
|
+
});
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
describe("getCompoundResult", () => {
|
|
143
|
+
it("sends GET to /api/v1/tasks/{id}/result", async () => {
|
|
144
|
+
// Arrange
|
|
145
|
+
const manifest = { subTasks: [{ taskId: "s-1", downloads: [] }] };
|
|
146
|
+
const { fn, calls } = spyFetcher(okResponse(manifest));
|
|
147
|
+
const tasks = new TasksEndpoint(fn);
|
|
148
|
+
|
|
149
|
+
// Act
|
|
150
|
+
const result = await tasks.getCompoundResult("c-1");
|
|
151
|
+
|
|
152
|
+
// Assert
|
|
153
|
+
expect(calls[0]!.path).toBe("/api/v1/tasks/c-1/result");
|
|
154
|
+
expect(result).toStrictEqual(manifest);
|
|
155
|
+
});
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
describe("poll", () => {
|
|
159
|
+
it("polls until the task reaches a terminal status", async () => {
|
|
160
|
+
// Arrange
|
|
161
|
+
const { fn, calls } = spyFetcher(
|
|
162
|
+
taskResponse("t-1", "running"),
|
|
163
|
+
taskResponse("t-1", "completed"),
|
|
164
|
+
);
|
|
165
|
+
const tasks = new TasksEndpoint(fn);
|
|
166
|
+
|
|
167
|
+
// Act
|
|
168
|
+
const task = await tasks.poll("t-1", NO_WAIT);
|
|
169
|
+
|
|
170
|
+
// Assert
|
|
171
|
+
expect(calls).toHaveLength(2);
|
|
172
|
+
expect(task.status).toBe("completed");
|
|
173
|
+
});
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
describe("pollAndDownload", () => {
|
|
177
|
+
it("polls the task and then asks for its downloads", async () => {
|
|
178
|
+
// Arrange
|
|
179
|
+
const { fn, calls } = spyFetcher(
|
|
180
|
+
taskResponse("t-1", "completed"),
|
|
181
|
+
downloadsResponse({ format: "glb", url: "https://example.test/part.glb" }),
|
|
182
|
+
);
|
|
183
|
+
const tasks = new TasksEndpoint(fn);
|
|
184
|
+
|
|
185
|
+
// Act
|
|
186
|
+
const result = await tasks.pollAndDownload("t-1", NO_WAIT);
|
|
187
|
+
|
|
188
|
+
// Assert
|
|
189
|
+
expect(calls.map((call) => call.path)).toStrictEqual(["/api/v1/tasks/t-1", "/api/v1/tasks/t-1/results"]);
|
|
190
|
+
expect(result.downloads).toStrictEqual([{ format: "glb", url: "https://example.test/part.glb" }]);
|
|
191
|
+
});
|
|
192
|
+
});
|
|
193
|
+
});
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
TaskDetail,
|
|
3
|
+
TaskList,
|
|
4
|
+
TaskListQuery,
|
|
5
|
+
TaskResultDownload,
|
|
6
|
+
TaskCreatedResult,
|
|
7
|
+
ResultFormat,
|
|
8
|
+
CompoundResultManifest,
|
|
9
|
+
TaskDownload,
|
|
10
|
+
TaskResultsDownload,
|
|
11
|
+
} from "../types/index.js";
|
|
12
|
+
import type { PollOptions } from "../polling.js";
|
|
13
|
+
import { pollTask } from "../polling.js";
|
|
14
|
+
import { unwrap } from "../unwrap.js";
|
|
15
|
+
|
|
16
|
+
type Fetcher = (method: string, path: string, body?: unknown) => Promise<Response>;
|
|
17
|
+
|
|
18
|
+
export class TasksEndpoint {
|
|
19
|
+
constructor(private readonly fetch: Fetcher) {}
|
|
20
|
+
|
|
21
|
+
/** Get the status and details of a single task. */
|
|
22
|
+
async get(taskId: string): Promise<TaskDetail> {
|
|
23
|
+
return unwrap(await this.fetch("GET", `/api/v1/tasks/${encodeURIComponent(taskId)}`));
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** List tasks with optional filters. */
|
|
27
|
+
async list(query?: TaskListQuery): Promise<TaskList> {
|
|
28
|
+
const params = new URLSearchParams();
|
|
29
|
+
if (query?.page != null) params.set("page", String(query.page));
|
|
30
|
+
if (query?.limit != null) params.set("limit", String(query.limit));
|
|
31
|
+
if (query?.status) params.set("status", query.status);
|
|
32
|
+
if (query?.kind) params.set("kind", query.kind);
|
|
33
|
+
const qs = params.toString();
|
|
34
|
+
return unwrap(await this.fetch("GET", `/api/v1/tasks${qs ? `?${qs}` : ""}`));
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** Get the download URL for a completed task result. */
|
|
38
|
+
async getResult(taskId: string, format?: ResultFormat): Promise<TaskResultDownload> {
|
|
39
|
+
const path = format
|
|
40
|
+
? `/api/v1/tasks/${encodeURIComponent(taskId)}/result/${encodeURIComponent(format)}`
|
|
41
|
+
: `/api/v1/tasks/${encodeURIComponent(taskId)}/result`;
|
|
42
|
+
return unwrap(await this.fetch("GET", path));
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** Get download URLs for all available result formats of a completed task. */
|
|
46
|
+
async getResults(taskId: string): Promise<TaskDownload[]> {
|
|
47
|
+
const res = await this.fetch(
|
|
48
|
+
"GET",
|
|
49
|
+
`/api/v1/tasks/${encodeURIComponent(taskId)}/results`,
|
|
50
|
+
);
|
|
51
|
+
const data = await unwrap<TaskResultsDownload>(res);
|
|
52
|
+
return data.downloads;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** Get the compound result manifest for a compound task. */
|
|
56
|
+
async getCompoundResult(taskId: string): Promise<CompoundResultManifest> {
|
|
57
|
+
return unwrap(
|
|
58
|
+
await this.fetch("GET", `/api/v1/tasks/${encodeURIComponent(taskId)}/result`),
|
|
59
|
+
);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** Cancel a running or queued task. */
|
|
63
|
+
async cancel(taskId: string): Promise<{ cancelled: boolean }> {
|
|
64
|
+
return unwrap(await this.fetch("DELETE", `/api/v1/tasks/${encodeURIComponent(taskId)}`));
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** Retry a failed or cancelled task. Returns 202 with new task info. */
|
|
68
|
+
async retry(taskId: string): Promise<TaskCreatedResult> {
|
|
69
|
+
return unwrap(
|
|
70
|
+
await this.fetch("POST", `/api/v1/tasks/${encodeURIComponent(taskId)}/retry`),
|
|
71
|
+
);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Poll a task until it reaches a terminal status.
|
|
76
|
+
* Returns the final TaskDetail on completion; throws on failure/timeout.
|
|
77
|
+
*/
|
|
78
|
+
async poll(taskId: string, opts?: PollOptions): Promise<TaskDetail> {
|
|
79
|
+
return pollTask(this.fetch, taskId, opts);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Poll a task and retrieve all download URLs once completed.
|
|
84
|
+
*/
|
|
85
|
+
async pollAndDownload(
|
|
86
|
+
taskId: string,
|
|
87
|
+
opts?: PollOptions,
|
|
88
|
+
): Promise<{ task: TaskDetail; downloads: TaskDownload[] }> {
|
|
89
|
+
const task = await pollTask(this.fetch, taskId, opts);
|
|
90
|
+
const downloads = await this.getResults(task.taskId);
|
|
91
|
+
return { task, downloads };
|
|
92
|
+
}
|
|
93
|
+
}
|