Skip to content

Commit 5ca7548

Browse files
committed
fix prediction terminal status handling
1 parent a4dbc9a commit 5ca7548

4 files changed

Lines changed: 32 additions & 11 deletions

File tree

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "wavespeed",
3-
"version": "0.3.1",
3+
"version": "0.3.2",
44
"description": "WaveSpeed Client SDK for Wavespeed API",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",

src/api/client.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const RETRYABLE_STATUS_CODES = new Set([429, 500, 502, 503, 504]);
2020
* Statuses that mean the task has reached a terminal, non-successful state
2121
* and polling must stop.
2222
*/
23-
const TERMINAL_FAILURE_STATUSES = new Set(['failed', 'cancelled', 'timeout']);
23+
const TERMINAL_FAILURE_STATUSES = new Set(['failed', 'cancelled', 'timeout', 'deleted']);
2424

2525
/**
2626
* Minimal filename-extension to MIME type mapping used to populate the
@@ -554,7 +554,7 @@ export class Client {
554554
}
555555

556556
if (TERMINAL_FAILURE_STATUSES.has(status)) {
557-
// cancelled and timeout are terminal too: keep polling and the task
557+
// cancelled, timeout, and deleted are terminal too: keep polling and the task
558558
// will never complete, so surface the API's error text and stop.
559559
const error = data.error || `Task ${status}`;
560560
throw new Error(`Prediction failed (task_id: ${requestId}): ${error}`);
@@ -598,10 +598,7 @@ export class Client {
598598
}
599599

600600
private _resultUrlFromData(data: Record<string, any>): string | undefined {
601-
const urls = data.urls;
602-
return urls && typeof urls === 'object' && typeof urls.get === 'string'
603-
? urls.get
604-
: undefined;
601+
return data.id ? `${this.baseUrl}/api/v3/predictions/${data.id}/result` : undefined;
605602
}
606603

607604
private _isSyncTimeoutData(data: Record<string, any>): boolean {

tests/test_api.ts

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,32 @@ describe('Client', () => {
241241
expect(global.fetch).toHaveBeenCalledTimes(2);
242242
});
243243

244+
test('run treats deleted status as terminal failure', async () => {
245+
const mockSubmitResponse = {
246+
ok: true,
247+
status: 200,
248+
json: async () => ({ data: { id: 'req-123' } }),
249+
};
250+
const mockGetResponse = {
251+
ok: true,
252+
status: 200,
253+
json: async () => ({
254+
data: { status: 'deleted', error: 'Task was deleted' }
255+
}),
256+
};
257+
258+
(global.fetch as jest.Mock)
259+
.mockResolvedValueOnce(mockSubmitResponse)
260+
.mockResolvedValueOnce(mockGetResponse);
261+
262+
const client = new Client('test-key');
263+
264+
await expect(
265+
client.run('wavespeed-ai/z-image/turbo', { prompt: 'test' })
266+
).rejects.toThrow('Task was deleted');
267+
expect(global.fetch).toHaveBeenCalledTimes(2);
268+
});
269+
244270
test('run timeout', async () => {
245271
const mockSubmitResponse = {
246272
ok: true,
@@ -324,7 +350,6 @@ describe('Client', () => {
324350
status: 'processing',
325351
code: 5004,
326352
error: 'Sync mode timed out after 90 seconds. The prediction is still processing asynchronously.',
327-
urls: { get: resultUrl }
328353
}
329354
}),
330355
};
@@ -353,7 +378,6 @@ describe('Client', () => {
353378
status: 'processing',
354379
code: 5004,
355380
error: 'Sync mode timed out after 90 seconds. The prediction is still processing asynchronously.',
356-
urls: { get: resultUrl }
357381
}
358382
}),
359383
};

0 commit comments

Comments
 (0)