wavespeed 0.0.15 → 0.2.2
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/LICENSE +21 -0
- package/README.md +92 -146
- package/dist/api/client.d.ts +157 -0
- package/dist/api/client.js +402 -0
- package/dist/api/index.d.ts +86 -0
- package/dist/api/index.js +102 -0
- package/dist/config.d.ts +15 -0
- package/dist/config.js +35 -0
- package/dist/index.d.ts +20 -120
- package/dist/index.js +26 -281
- package/dist/version.d.ts +6 -0
- package/dist/version.js +25 -0
- package/package.json +6 -3
|
@@ -0,0 +1,402 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* WaveSpeed API client implementation.
|
|
4
|
+
*/
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.Client = void 0;
|
|
7
|
+
const config_1 = require("../config");
|
|
8
|
+
/**
|
|
9
|
+
* WaveSpeed API client.
|
|
10
|
+
*
|
|
11
|
+
* Example:
|
|
12
|
+
* const client = new Client("your-api-key");
|
|
13
|
+
* const output = await client.run("wavespeed-ai/z-image/turbo", { prompt: "Cat" });
|
|
14
|
+
*
|
|
15
|
+
* // With sync mode (single request, waits for result)
|
|
16
|
+
* const output2 = await client.run("wavespeed-ai/z-image/turbo", { prompt: "Cat" }, { enableSyncMode: true });
|
|
17
|
+
*
|
|
18
|
+
* // With retry
|
|
19
|
+
* const output3 = await client.run("wavespeed-ai/z-image/turbo", { prompt: "Cat" }, { maxRetries: 3 });
|
|
20
|
+
*/
|
|
21
|
+
class Client {
|
|
22
|
+
/**
|
|
23
|
+
* Initialize the client.
|
|
24
|
+
*
|
|
25
|
+
* Args:
|
|
26
|
+
* apiKey: WaveSpeed API key. If not provided, uses wavespeed.config.api.apiKey.
|
|
27
|
+
* options.baseUrl: Base URL for the API. If not provided, uses wavespeed.config.api.baseUrl.
|
|
28
|
+
* options.connectionTimeout: Timeout for HTTP requests in seconds.
|
|
29
|
+
* options.timeout: Total API call timeout in seconds.
|
|
30
|
+
* options.maxRetries: Maximum number of retries for the entire operation.
|
|
31
|
+
* options.maxConnectionRetries: Maximum retries for individual HTTP requests.
|
|
32
|
+
* options.retryInterval: Base interval between retries in seconds.
|
|
33
|
+
*/
|
|
34
|
+
constructor(apiKey, options) {
|
|
35
|
+
var _a, _b, _c, _d, _e;
|
|
36
|
+
this.apiKey = apiKey || config_1.api.apiKey || '';
|
|
37
|
+
this.baseUrl = ((options === null || options === void 0 ? void 0 : options.baseUrl) || config_1.api.baseUrl).replace(/\/+$/, '');
|
|
38
|
+
this.connectionTimeout = (_a = options === null || options === void 0 ? void 0 : options.connectionTimeout) !== null && _a !== void 0 ? _a : config_1.api.connectionTimeout;
|
|
39
|
+
this.timeout = (_b = options === null || options === void 0 ? void 0 : options.timeout) !== null && _b !== void 0 ? _b : config_1.api.timeout;
|
|
40
|
+
this.maxRetries = (_c = options === null || options === void 0 ? void 0 : options.maxRetries) !== null && _c !== void 0 ? _c : config_1.api.maxRetries;
|
|
41
|
+
this.maxConnectionRetries = (_d = options === null || options === void 0 ? void 0 : options.maxConnectionRetries) !== null && _d !== void 0 ? _d : config_1.api.maxConnectionRetries;
|
|
42
|
+
this.retryInterval = (_e = options === null || options === void 0 ? void 0 : options.retryInterval) !== null && _e !== void 0 ? _e : config_1.api.retryInterval;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Get request headers with authentication.
|
|
46
|
+
*/
|
|
47
|
+
_getHeaders() {
|
|
48
|
+
if (!this.apiKey) {
|
|
49
|
+
throw new Error('API key is required. Set WAVESPEED_API_KEY environment variable ' +
|
|
50
|
+
'or pass apiKey to Client().');
|
|
51
|
+
}
|
|
52
|
+
return {
|
|
53
|
+
'Content-Type': 'application/json',
|
|
54
|
+
'Authorization': `Bearer ${this.apiKey}`,
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Submit a prediction request.
|
|
59
|
+
*
|
|
60
|
+
* Args:
|
|
61
|
+
* model: Model identifier.
|
|
62
|
+
* input: Input parameters.
|
|
63
|
+
* enableSyncMode: If true, wait for result in single request.
|
|
64
|
+
* timeout: Request timeout in seconds.
|
|
65
|
+
*
|
|
66
|
+
* Returns:
|
|
67
|
+
* Tuple of [requestId, result]. In async mode, result is null.
|
|
68
|
+
* In sync mode, requestId is null and result contains the response.
|
|
69
|
+
*
|
|
70
|
+
* Throws:
|
|
71
|
+
* Error: If submission fails after retries.
|
|
72
|
+
*/
|
|
73
|
+
async _submit(model, input, enableSyncMode = false, timeout) {
|
|
74
|
+
var _a, _b;
|
|
75
|
+
const url = `${this.baseUrl}/api/v3/${model}`;
|
|
76
|
+
const body = input ? { ...input } : {};
|
|
77
|
+
if (enableSyncMode) {
|
|
78
|
+
body.enable_sync_mode = true;
|
|
79
|
+
}
|
|
80
|
+
const requestTimeout = timeout !== null && timeout !== void 0 ? timeout : this.timeout;
|
|
81
|
+
// Use connection timeout for connect, request_timeout for read
|
|
82
|
+
const connectTimeout = requestTimeout
|
|
83
|
+
? Math.min(this.connectionTimeout, requestTimeout)
|
|
84
|
+
: this.connectionTimeout;
|
|
85
|
+
const timeoutMs = connectTimeout * 1000;
|
|
86
|
+
let lastError;
|
|
87
|
+
// Connection-level retries
|
|
88
|
+
for (let retry = 0; retry <= this.maxConnectionRetries; retry++) {
|
|
89
|
+
const controller = new AbortController();
|
|
90
|
+
const timeoutId = setTimeout(() => controller.abort(), timeoutMs);
|
|
91
|
+
try {
|
|
92
|
+
const response = await fetch(url, {
|
|
93
|
+
method: 'POST',
|
|
94
|
+
headers: this._getHeaders(),
|
|
95
|
+
body: JSON.stringify(body),
|
|
96
|
+
signal: controller.signal,
|
|
97
|
+
});
|
|
98
|
+
clearTimeout(timeoutId);
|
|
99
|
+
if (!response.ok) {
|
|
100
|
+
const errorText = await response.text();
|
|
101
|
+
throw new Error(`Failed to submit prediction: HTTP ${response.status}: ${errorText}`);
|
|
102
|
+
}
|
|
103
|
+
const result = await response.json();
|
|
104
|
+
if (enableSyncMode) {
|
|
105
|
+
return [null, result];
|
|
106
|
+
}
|
|
107
|
+
const requestId = (_a = result.data) === null || _a === void 0 ? void 0 : _a.id;
|
|
108
|
+
if (!requestId) {
|
|
109
|
+
throw new Error(`No request ID in response: ${JSON.stringify(result)}`);
|
|
110
|
+
}
|
|
111
|
+
return [requestId, null];
|
|
112
|
+
}
|
|
113
|
+
catch (error) {
|
|
114
|
+
clearTimeout(timeoutId);
|
|
115
|
+
lastError = error;
|
|
116
|
+
const isConnectionError = error.name === 'AbortError' ||
|
|
117
|
+
error.name === 'TypeError' ||
|
|
118
|
+
((_b = error.message) === null || _b === void 0 ? void 0 : _b.includes('fetch'));
|
|
119
|
+
if (isConnectionError && retry < this.maxConnectionRetries) {
|
|
120
|
+
const delay = this.retryInterval * (retry + 1) * 1000;
|
|
121
|
+
console.log(`Connection error on attempt ${retry + 1}/${this.maxConnectionRetries + 1}:`);
|
|
122
|
+
console.error(error);
|
|
123
|
+
console.log(`Retrying in ${delay}ms...`);
|
|
124
|
+
await new Promise((resolve) => setTimeout(resolve, delay));
|
|
125
|
+
}
|
|
126
|
+
else if (retry >= this.maxConnectionRetries) {
|
|
127
|
+
throw new Error(`Failed to submit prediction after ${this.maxConnectionRetries + 1} attempts: ${lastError === null || lastError === void 0 ? void 0 : lastError.message}`);
|
|
128
|
+
}
|
|
129
|
+
else {
|
|
130
|
+
throw error;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
throw lastError;
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Get prediction result.
|
|
138
|
+
*
|
|
139
|
+
* Args:
|
|
140
|
+
* requestId: The prediction request ID.
|
|
141
|
+
* timeout: Request timeout in seconds.
|
|
142
|
+
*
|
|
143
|
+
* Returns:
|
|
144
|
+
* Full API response.
|
|
145
|
+
*
|
|
146
|
+
* Throws:
|
|
147
|
+
* Error: If fetching result fails after retries.
|
|
148
|
+
*/
|
|
149
|
+
async _getResult(requestId, timeout) {
|
|
150
|
+
var _a;
|
|
151
|
+
const url = `${this.baseUrl}/api/v3/predictions/${requestId}/result`;
|
|
152
|
+
const requestTimeout = timeout !== null && timeout !== void 0 ? timeout : this.timeout;
|
|
153
|
+
const connectTimeout = requestTimeout
|
|
154
|
+
? Math.min(this.connectionTimeout, requestTimeout)
|
|
155
|
+
: this.connectionTimeout;
|
|
156
|
+
const timeoutMs = connectTimeout * 1000;
|
|
157
|
+
let lastError;
|
|
158
|
+
// Connection-level retries
|
|
159
|
+
for (let retry = 0; retry <= this.maxConnectionRetries; retry++) {
|
|
160
|
+
const controller = new AbortController();
|
|
161
|
+
const timeoutId = setTimeout(() => controller.abort(), timeoutMs);
|
|
162
|
+
try {
|
|
163
|
+
const response = await fetch(url, {
|
|
164
|
+
method: 'GET',
|
|
165
|
+
headers: this._getHeaders(),
|
|
166
|
+
signal: controller.signal,
|
|
167
|
+
});
|
|
168
|
+
clearTimeout(timeoutId);
|
|
169
|
+
if (!response.ok) {
|
|
170
|
+
const errorText = await response.text();
|
|
171
|
+
throw new Error(`Failed to get result for task ${requestId}: HTTP ${response.status}: ${errorText}`);
|
|
172
|
+
}
|
|
173
|
+
return await response.json();
|
|
174
|
+
}
|
|
175
|
+
catch (error) {
|
|
176
|
+
clearTimeout(timeoutId);
|
|
177
|
+
lastError = error;
|
|
178
|
+
const isConnectionError = error.name === 'AbortError' ||
|
|
179
|
+
error.name === 'TypeError' ||
|
|
180
|
+
((_a = error.message) === null || _a === void 0 ? void 0 : _a.includes('fetch'));
|
|
181
|
+
if (isConnectionError && retry < this.maxConnectionRetries) {
|
|
182
|
+
const delay = this.retryInterval * (retry + 1) * 1000;
|
|
183
|
+
console.log(`Connection error getting result on attempt ${retry + 1}/${this.maxConnectionRetries + 1}:`);
|
|
184
|
+
console.error(error);
|
|
185
|
+
console.log(`Retrying in ${delay}ms...`);
|
|
186
|
+
await new Promise((resolve) => setTimeout(resolve, delay));
|
|
187
|
+
}
|
|
188
|
+
else if (retry >= this.maxConnectionRetries) {
|
|
189
|
+
throw new Error(`Failed to get result for task ${requestId} after ${this.maxConnectionRetries + 1} attempts: ${lastError === null || lastError === void 0 ? void 0 : lastError.message}`);
|
|
190
|
+
}
|
|
191
|
+
else {
|
|
192
|
+
throw error;
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
throw lastError;
|
|
197
|
+
}
|
|
198
|
+
/**
|
|
199
|
+
* Wait for prediction to complete.
|
|
200
|
+
*
|
|
201
|
+
* Args:
|
|
202
|
+
* requestId: The prediction request ID.
|
|
203
|
+
* timeout: Maximum wait time in seconds (undefined = no timeout).
|
|
204
|
+
* pollInterval: Time between polls in seconds.
|
|
205
|
+
*
|
|
206
|
+
* Returns:
|
|
207
|
+
* Dict with "outputs" array.
|
|
208
|
+
*
|
|
209
|
+
* Throws:
|
|
210
|
+
* Error: If prediction fails.
|
|
211
|
+
* Error: If prediction times out.
|
|
212
|
+
*/
|
|
213
|
+
async _wait(requestId, timeout, pollInterval) {
|
|
214
|
+
const startTime = Date.now();
|
|
215
|
+
while (true) {
|
|
216
|
+
// Check timeout
|
|
217
|
+
if (timeout !== undefined) {
|
|
218
|
+
const elapsed = (Date.now() - startTime) / 1000;
|
|
219
|
+
if (elapsed >= timeout) {
|
|
220
|
+
throw new Error(`Prediction timed out after ${timeout} seconds (task_id: ${requestId})`);
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
const result = await this._getResult(requestId, timeout);
|
|
224
|
+
const data = result.data || {};
|
|
225
|
+
const status = data.status;
|
|
226
|
+
if (status === 'completed') {
|
|
227
|
+
return { outputs: data.outputs || [] };
|
|
228
|
+
}
|
|
229
|
+
if (status === 'failed') {
|
|
230
|
+
const error = data.error || 'Unknown error';
|
|
231
|
+
throw new Error(`Prediction failed (task_id: ${requestId}): ${error}`);
|
|
232
|
+
}
|
|
233
|
+
await new Promise((resolve) => setTimeout(resolve, pollInterval * 1000));
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
/**
|
|
237
|
+
* Determine if an error is worth retrying at the task level.
|
|
238
|
+
*
|
|
239
|
+
* Args:
|
|
240
|
+
* error: The exception to check.
|
|
241
|
+
*
|
|
242
|
+
* Returns:
|
|
243
|
+
* True if the error is retryable.
|
|
244
|
+
*/
|
|
245
|
+
_isRetryableError(error) {
|
|
246
|
+
if (!error)
|
|
247
|
+
return false;
|
|
248
|
+
// Always retry timeout and connection errors
|
|
249
|
+
const errorStr = error.toString().toLowerCase();
|
|
250
|
+
if (errorStr.includes('timeout') || errorStr.includes('connection')) {
|
|
251
|
+
return true;
|
|
252
|
+
}
|
|
253
|
+
// Retry server errors (5xx) and rate limiting (429)
|
|
254
|
+
if (errorStr.includes('http 5') || errorStr.includes('429')) {
|
|
255
|
+
return true;
|
|
256
|
+
}
|
|
257
|
+
return false;
|
|
258
|
+
}
|
|
259
|
+
/**
|
|
260
|
+
* Run a model and wait for the output.
|
|
261
|
+
*
|
|
262
|
+
* Args:
|
|
263
|
+
* model: Model identifier (e.g., "wavespeed-ai/flux-dev").
|
|
264
|
+
* input: Input parameters for the model.
|
|
265
|
+
* options.timeout: Maximum time to wait for completion (undefined = no timeout).
|
|
266
|
+
* options.pollInterval: Interval between status checks in seconds.
|
|
267
|
+
* options.enableSyncMode: If true, use synchronous mode (single request).
|
|
268
|
+
* options.maxRetries: Maximum task-level retries (overrides client setting).
|
|
269
|
+
*
|
|
270
|
+
* Returns:
|
|
271
|
+
* Dict containing "outputs" array with model outputs.
|
|
272
|
+
*
|
|
273
|
+
* Throws:
|
|
274
|
+
* Error: If API key is not configured.
|
|
275
|
+
* Error: If the prediction fails.
|
|
276
|
+
* Error: If the prediction times out.
|
|
277
|
+
*/
|
|
278
|
+
async run(model, input, options) {
|
|
279
|
+
var _a, _b, _c, _d;
|
|
280
|
+
const taskRetries = (_a = options === null || options === void 0 ? void 0 : options.maxRetries) !== null && _a !== void 0 ? _a : this.maxRetries;
|
|
281
|
+
const timeout = (_b = options === null || options === void 0 ? void 0 : options.timeout) !== null && _b !== void 0 ? _b : this.timeout;
|
|
282
|
+
const pollInterval = (_c = options === null || options === void 0 ? void 0 : options.pollInterval) !== null && _c !== void 0 ? _c : 1.0;
|
|
283
|
+
const enableSyncMode = (_d = options === null || options === void 0 ? void 0 : options.enableSyncMode) !== null && _d !== void 0 ? _d : false;
|
|
284
|
+
let lastError;
|
|
285
|
+
for (let attempt = 0; attempt <= taskRetries; attempt++) {
|
|
286
|
+
try {
|
|
287
|
+
const [requestId, syncResult] = await this._submit(model, input, enableSyncMode, timeout);
|
|
288
|
+
if (enableSyncMode) {
|
|
289
|
+
// In sync mode, extract outputs from the result
|
|
290
|
+
const data = (syncResult === null || syncResult === void 0 ? void 0 : syncResult.data) || {};
|
|
291
|
+
const status = data.status;
|
|
292
|
+
if (status !== 'completed') {
|
|
293
|
+
const error = data.error || 'Unknown error';
|
|
294
|
+
const requestId = data.id || 'unknown';
|
|
295
|
+
throw new Error(`Prediction failed (task_id: ${requestId}): ${error}`);
|
|
296
|
+
}
|
|
297
|
+
return { outputs: data.outputs || [] };
|
|
298
|
+
}
|
|
299
|
+
if (requestId) {
|
|
300
|
+
return await this._wait(requestId, timeout, pollInterval);
|
|
301
|
+
}
|
|
302
|
+
throw new Error('Invalid response from _submit');
|
|
303
|
+
}
|
|
304
|
+
catch (error) {
|
|
305
|
+
lastError = error;
|
|
306
|
+
const isRetryable = this._isRetryableError(error);
|
|
307
|
+
if (!isRetryable || attempt >= taskRetries) {
|
|
308
|
+
throw error;
|
|
309
|
+
}
|
|
310
|
+
console.log(`Task attempt ${attempt + 1}/${taskRetries + 1} failed: ${error}`);
|
|
311
|
+
const delay = this.retryInterval * (attempt + 1) * 1000;
|
|
312
|
+
console.log(`Retrying in ${delay}ms...`);
|
|
313
|
+
await new Promise((resolve) => setTimeout(resolve, delay));
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
// Should not reach here, but just in case
|
|
317
|
+
if (lastError) {
|
|
318
|
+
throw lastError;
|
|
319
|
+
}
|
|
320
|
+
throw new Error(`All ${taskRetries + 1} attempts failed`);
|
|
321
|
+
}
|
|
322
|
+
/**
|
|
323
|
+
* Upload a file to WaveSpeed.
|
|
324
|
+
*
|
|
325
|
+
* Args:
|
|
326
|
+
* file: File path string to upload.
|
|
327
|
+
* options.timeout: Total API call timeout in seconds.
|
|
328
|
+
*
|
|
329
|
+
* Returns:
|
|
330
|
+
* URL of the uploaded file.
|
|
331
|
+
*
|
|
332
|
+
* Throws:
|
|
333
|
+
* Error: If API key is not configured.
|
|
334
|
+
* Error: If file path does not exist.
|
|
335
|
+
* Error: If upload fails.
|
|
336
|
+
*
|
|
337
|
+
* Example:
|
|
338
|
+
* const url = await client.upload("/path/to/image.png");
|
|
339
|
+
* console.log(url);
|
|
340
|
+
*/
|
|
341
|
+
async upload(file, options) {
|
|
342
|
+
var _a, _b;
|
|
343
|
+
if (!this.apiKey) {
|
|
344
|
+
throw new Error('API key is required. Set WAVESPEED_API_KEY environment variable ' +
|
|
345
|
+
'or pass apiKey to Client().');
|
|
346
|
+
}
|
|
347
|
+
const url = `${this.baseUrl}/api/v3/media/upload/binary`;
|
|
348
|
+
const headers = {
|
|
349
|
+
'Authorization': `Bearer ${this.apiKey}`,
|
|
350
|
+
};
|
|
351
|
+
const timeout = (_a = options === null || options === void 0 ? void 0 : options.timeout) !== null && _a !== void 0 ? _a : this.timeout;
|
|
352
|
+
const requestTimeout = Math.min(this.connectionTimeout, timeout);
|
|
353
|
+
const timeoutMs = requestTimeout * 1000;
|
|
354
|
+
// Node.js environment check
|
|
355
|
+
let fs;
|
|
356
|
+
let path;
|
|
357
|
+
try {
|
|
358
|
+
fs = require('fs');
|
|
359
|
+
path = require('path');
|
|
360
|
+
}
|
|
361
|
+
catch (err) {
|
|
362
|
+
throw new Error('File path uploads are only supported in Node.js environments.');
|
|
363
|
+
}
|
|
364
|
+
if (!fs.existsSync(file)) {
|
|
365
|
+
throw new Error(`File not found: ${file}`);
|
|
366
|
+
}
|
|
367
|
+
const data = fs.readFileSync(file);
|
|
368
|
+
const filename = path.basename(file);
|
|
369
|
+
const form = new FormData();
|
|
370
|
+
const blob = new Blob([data]);
|
|
371
|
+
form.append('file', blob, filename);
|
|
372
|
+
const controller = new AbortController();
|
|
373
|
+
const timeoutId = setTimeout(() => controller.abort(), timeoutMs);
|
|
374
|
+
try {
|
|
375
|
+
const response = await fetch(url, {
|
|
376
|
+
method: 'POST',
|
|
377
|
+
headers,
|
|
378
|
+
body: form,
|
|
379
|
+
signal: controller.signal,
|
|
380
|
+
});
|
|
381
|
+
clearTimeout(timeoutId);
|
|
382
|
+
if (!response.ok) {
|
|
383
|
+
const errorText = await response.text();
|
|
384
|
+
throw new Error(`Failed to upload file: HTTP ${response.status}: ${errorText}`);
|
|
385
|
+
}
|
|
386
|
+
const result = await response.json();
|
|
387
|
+
if (result.code !== 200) {
|
|
388
|
+
throw new Error(`Upload failed: ${result.message || 'Unknown error'}`);
|
|
389
|
+
}
|
|
390
|
+
const downloadUrl = (_b = result.data) === null || _b === void 0 ? void 0 : _b.download_url;
|
|
391
|
+
if (!downloadUrl) {
|
|
392
|
+
throw new Error('Upload failed: no download_url in response');
|
|
393
|
+
}
|
|
394
|
+
return downloadUrl;
|
|
395
|
+
}
|
|
396
|
+
catch (error) {
|
|
397
|
+
clearTimeout(timeoutId);
|
|
398
|
+
throw error;
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
exports.Client = Client;
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WaveSpeed API client module.
|
|
3
|
+
*
|
|
4
|
+
* Provides a simple interface to run WaveSpeed AI models.
|
|
5
|
+
*
|
|
6
|
+
* Example usage:
|
|
7
|
+
* import wavespeed from 'wavespeed';
|
|
8
|
+
*
|
|
9
|
+
* const output = await wavespeed.run(
|
|
10
|
+
* "wavespeed-ai/z-image/turbo",
|
|
11
|
+
* { prompt: "A beautiful sunset over mountains" }
|
|
12
|
+
* );
|
|
13
|
+
*
|
|
14
|
+
* console.log(output["outputs"][0]); // First output URL
|
|
15
|
+
*
|
|
16
|
+
* // Upload a file
|
|
17
|
+
* const url = await wavespeed.upload("/path/to/image.png");
|
|
18
|
+
* console.log(url);
|
|
19
|
+
*/
|
|
20
|
+
import { Client } from './client';
|
|
21
|
+
import type { RunOptions } from './client';
|
|
22
|
+
export { Client };
|
|
23
|
+
export type { RunOptions };
|
|
24
|
+
/**
|
|
25
|
+
* Run a model and wait for the output.
|
|
26
|
+
*
|
|
27
|
+
* Args:
|
|
28
|
+
* model: Model identifier (e.g., "wavespeed-ai/flux-dev").
|
|
29
|
+
* input: Input parameters for the model.
|
|
30
|
+
* options.timeout: Maximum time to wait for completion (undefined = no timeout).
|
|
31
|
+
* options.pollInterval: Interval between status checks in seconds.
|
|
32
|
+
* options.enableSyncMode: If true, use synchronous mode (single request).
|
|
33
|
+
* options.maxRetries: Maximum retries for this request (overrides default setting).
|
|
34
|
+
*
|
|
35
|
+
* Returns:
|
|
36
|
+
* Dict containing "outputs" array with model outputs.
|
|
37
|
+
*
|
|
38
|
+
* Throws:
|
|
39
|
+
* Error: If API key is not configured.
|
|
40
|
+
* Error: If the prediction fails.
|
|
41
|
+
* Error: If the prediction times out.
|
|
42
|
+
*
|
|
43
|
+
* Example:
|
|
44
|
+
* const output = await run(
|
|
45
|
+
* "wavespeed-ai/z-image/turbo",
|
|
46
|
+
* { prompt: "A cat sitting on a windowsill" }
|
|
47
|
+
* );
|
|
48
|
+
* console.log(output["outputs"][0]); // First output URL
|
|
49
|
+
*
|
|
50
|
+
* // With sync mode
|
|
51
|
+
* const output2 = await run(
|
|
52
|
+
* "wavespeed-ai/z-image/turbo",
|
|
53
|
+
* { prompt: "A cat" },
|
|
54
|
+
* { enableSyncMode: true }
|
|
55
|
+
* );
|
|
56
|
+
*
|
|
57
|
+
* // With retry
|
|
58
|
+
* const output3 = await run(
|
|
59
|
+
* "wavespeed-ai/z-image/turbo",
|
|
60
|
+
* { prompt: "A cat" },
|
|
61
|
+
* { maxRetries: 3 }
|
|
62
|
+
* );
|
|
63
|
+
*/
|
|
64
|
+
export declare function run(model: string, input?: Record<string, any>, options?: RunOptions): Promise<Record<string, any>>;
|
|
65
|
+
/**
|
|
66
|
+
* Upload a file to WaveSpeed.
|
|
67
|
+
*
|
|
68
|
+
* Args:
|
|
69
|
+
* file: File path string to upload.
|
|
70
|
+
* options.timeout: Total API call timeout in seconds.
|
|
71
|
+
*
|
|
72
|
+
* Returns:
|
|
73
|
+
* URL of the uploaded file.
|
|
74
|
+
*
|
|
75
|
+
* Throws:
|
|
76
|
+
* Error: If API key is not configured.
|
|
77
|
+
* Error: If file path does not exist.
|
|
78
|
+
* Error: If upload fails.
|
|
79
|
+
*
|
|
80
|
+
* Example:
|
|
81
|
+
* const url = await upload("/path/to/image.png");
|
|
82
|
+
* console.log(url);
|
|
83
|
+
*/
|
|
84
|
+
export declare function upload(file: string, options?: {
|
|
85
|
+
timeout?: number;
|
|
86
|
+
}): Promise<string>;
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* WaveSpeed API client module.
|
|
4
|
+
*
|
|
5
|
+
* Provides a simple interface to run WaveSpeed AI models.
|
|
6
|
+
*
|
|
7
|
+
* Example usage:
|
|
8
|
+
* import wavespeed from 'wavespeed';
|
|
9
|
+
*
|
|
10
|
+
* const output = await wavespeed.run(
|
|
11
|
+
* "wavespeed-ai/z-image/turbo",
|
|
12
|
+
* { prompt: "A beautiful sunset over mountains" }
|
|
13
|
+
* );
|
|
14
|
+
*
|
|
15
|
+
* console.log(output["outputs"][0]); // First output URL
|
|
16
|
+
*
|
|
17
|
+
* // Upload a file
|
|
18
|
+
* const url = await wavespeed.upload("/path/to/image.png");
|
|
19
|
+
* console.log(url);
|
|
20
|
+
*/
|
|
21
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
22
|
+
exports.Client = void 0;
|
|
23
|
+
exports.run = run;
|
|
24
|
+
exports.upload = upload;
|
|
25
|
+
const client_1 = require("./client");
|
|
26
|
+
Object.defineProperty(exports, "Client", { enumerable: true, get: function () { return client_1.Client; } });
|
|
27
|
+
// Default client instance
|
|
28
|
+
let _defaultClient = null;
|
|
29
|
+
/**
|
|
30
|
+
* Get or create the default client instance.
|
|
31
|
+
*/
|
|
32
|
+
function _getDefaultClient() {
|
|
33
|
+
if (_defaultClient === null) {
|
|
34
|
+
_defaultClient = new client_1.Client();
|
|
35
|
+
}
|
|
36
|
+
return _defaultClient;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Run a model and wait for the output.
|
|
40
|
+
*
|
|
41
|
+
* Args:
|
|
42
|
+
* model: Model identifier (e.g., "wavespeed-ai/flux-dev").
|
|
43
|
+
* input: Input parameters for the model.
|
|
44
|
+
* options.timeout: Maximum time to wait for completion (undefined = no timeout).
|
|
45
|
+
* options.pollInterval: Interval between status checks in seconds.
|
|
46
|
+
* options.enableSyncMode: If true, use synchronous mode (single request).
|
|
47
|
+
* options.maxRetries: Maximum retries for this request (overrides default setting).
|
|
48
|
+
*
|
|
49
|
+
* Returns:
|
|
50
|
+
* Dict containing "outputs" array with model outputs.
|
|
51
|
+
*
|
|
52
|
+
* Throws:
|
|
53
|
+
* Error: If API key is not configured.
|
|
54
|
+
* Error: If the prediction fails.
|
|
55
|
+
* Error: If the prediction times out.
|
|
56
|
+
*
|
|
57
|
+
* Example:
|
|
58
|
+
* const output = await run(
|
|
59
|
+
* "wavespeed-ai/z-image/turbo",
|
|
60
|
+
* { prompt: "A cat sitting on a windowsill" }
|
|
61
|
+
* );
|
|
62
|
+
* console.log(output["outputs"][0]); // First output URL
|
|
63
|
+
*
|
|
64
|
+
* // With sync mode
|
|
65
|
+
* const output2 = await run(
|
|
66
|
+
* "wavespeed-ai/z-image/turbo",
|
|
67
|
+
* { prompt: "A cat" },
|
|
68
|
+
* { enableSyncMode: true }
|
|
69
|
+
* );
|
|
70
|
+
*
|
|
71
|
+
* // With retry
|
|
72
|
+
* const output3 = await run(
|
|
73
|
+
* "wavespeed-ai/z-image/turbo",
|
|
74
|
+
* { prompt: "A cat" },
|
|
75
|
+
* { maxRetries: 3 }
|
|
76
|
+
* );
|
|
77
|
+
*/
|
|
78
|
+
async function run(model, input, options) {
|
|
79
|
+
return _getDefaultClient().run(model, input, options);
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Upload a file to WaveSpeed.
|
|
83
|
+
*
|
|
84
|
+
* Args:
|
|
85
|
+
* file: File path string to upload.
|
|
86
|
+
* options.timeout: Total API call timeout in seconds.
|
|
87
|
+
*
|
|
88
|
+
* Returns:
|
|
89
|
+
* URL of the uploaded file.
|
|
90
|
+
*
|
|
91
|
+
* Throws:
|
|
92
|
+
* Error: If API key is not configured.
|
|
93
|
+
* Error: If file path does not exist.
|
|
94
|
+
* Error: If upload fails.
|
|
95
|
+
*
|
|
96
|
+
* Example:
|
|
97
|
+
* const url = await upload("/path/to/image.png");
|
|
98
|
+
* console.log(url);
|
|
99
|
+
*/
|
|
100
|
+
async function upload(file, options) {
|
|
101
|
+
return _getDefaultClient().upload(file, options);
|
|
102
|
+
}
|
package/dist/config.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Configuration module for WaveSpeed SDK.
|
|
3
|
+
*/
|
|
4
|
+
/**
|
|
5
|
+
* API client configuration options.
|
|
6
|
+
*/
|
|
7
|
+
export declare class api {
|
|
8
|
+
static apiKey: string | undefined;
|
|
9
|
+
static baseUrl: string;
|
|
10
|
+
static connectionTimeout: number;
|
|
11
|
+
static timeout: number;
|
|
12
|
+
static maxRetries: number;
|
|
13
|
+
static maxConnectionRetries: number;
|
|
14
|
+
static retryInterval: number;
|
|
15
|
+
}
|
package/dist/config.js
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Configuration module for WaveSpeed SDK.
|
|
4
|
+
*/
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.api = void 0;
|
|
7
|
+
/**
|
|
8
|
+
* Get environment variable value (works in both Node.js and browser)
|
|
9
|
+
*/
|
|
10
|
+
function getEnv(name) {
|
|
11
|
+
if (typeof process !== 'undefined' && process.env && process.env[name]) {
|
|
12
|
+
return process.env[name];
|
|
13
|
+
}
|
|
14
|
+
return undefined;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* API client configuration options.
|
|
18
|
+
*/
|
|
19
|
+
class api {
|
|
20
|
+
}
|
|
21
|
+
exports.api = api;
|
|
22
|
+
// Authentication
|
|
23
|
+
api.apiKey = getEnv('WAVESPEED_API_KEY');
|
|
24
|
+
// API base URL
|
|
25
|
+
api.baseUrl = 'https://api.wavespeed.ai';
|
|
26
|
+
// Connection timeout in seconds
|
|
27
|
+
api.connectionTimeout = 10.0;
|
|
28
|
+
// Total API call timeout in seconds
|
|
29
|
+
api.timeout = 36000.0;
|
|
30
|
+
// Maximum number of retries for the entire operation (task-level retries)
|
|
31
|
+
api.maxRetries = 0;
|
|
32
|
+
// Maximum number of retries for individual HTTP requests (connection errors, timeouts)
|
|
33
|
+
api.maxConnectionRetries = 5;
|
|
34
|
+
// Base interval between retries in seconds (actual delay = retryInterval * attempt)
|
|
35
|
+
api.retryInterval = 1.0;
|