@sogni-ai/sogni-client 5.70.0 → 5.71.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/CHANGELOG.md +14 -0
- package/dist/ApiClient/WebSocketClient/events.d.ts +2 -0
- package/dist/ApiClient/WebSocketClient/index.d.ts +2 -0
- package/dist/ApiClient/WebSocketClient/index.js +26 -14
- package/dist/ApiClient/WebSocketClient/index.js.map +1 -1
- package/dist/Projects/index.d.ts +19 -0
- package/dist/Projects/index.js +17 -1
- package/dist/Projects/index.js.map +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.js.map +1 -1
- package/dist/types/ErrorData.d.ts +9 -0
- package/dist/types/ErrorData.js.map +1 -1
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/dist-esm/ApiClient/WebSocketClient/index.js +26 -14
- package/dist-esm/ApiClient/WebSocketClient/index.js.map +1 -1
- package/dist-esm/Projects/index.js +17 -1
- package/dist-esm/Projects/index.js.map +1 -1
- package/dist-esm/index.js.map +1 -1
- package/dist-esm/types/ErrorData.js.map +1 -1
- package/dist-esm/version.js +1 -1
- package/package.json +2 -1
- package/src/ApiClient/WebSocketClient/events.ts +2 -0
- package/src/ApiClient/WebSocketClient/index.ts +24 -12
- package/src/Projects/index.ts +36 -0
- package/src/index.ts +2 -0
- package/src/types/ErrorData.ts +9 -0
- package/src/version.ts +1 -1
|
@@ -46,6 +46,8 @@ class WebSocketClient extends RestClient<SocketEventMap> implements IWebSocketCl
|
|
|
46
46
|
socketEventSubscriptions?: SocketEventSubscriptions;
|
|
47
47
|
private projectQueueSubscription = true;
|
|
48
48
|
private socket: WebSocket | null = null;
|
|
49
|
+
/** Blob decoding must preserve wire order, independently for each connection. */
|
|
50
|
+
private readonly messageQueues = new WeakMap<WebSocket, Promise<void>>();
|
|
49
51
|
private _supernetType: SupernetType;
|
|
50
52
|
private _pingInterval: NodeJS.Timeout | null = null;
|
|
51
53
|
/** The socket the server has sent `authenticated` on, i.e. one that accepts work. */
|
|
@@ -355,19 +357,26 @@ class WebSocketClient extends RestClient<SocketEventMap> implements IWebSocketCl
|
|
|
355
357
|
}
|
|
356
358
|
|
|
357
359
|
private handleMessage(e: MessageEvent) {
|
|
358
|
-
const source =
|
|
359
|
-
|
|
360
|
-
//
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
360
|
+
const source = this.socket;
|
|
361
|
+
if (!source || e.target !== source || !this.isConnected) return;
|
|
362
|
+
// Decode concurrently, but capture rejection immediately: this frame may
|
|
363
|
+
// have to wait behind an earlier read before its result can be delivered.
|
|
364
|
+
const decoded = Promise.resolve()
|
|
365
|
+
.then(() => (isNodejs ? e.data.toString() : (e.data as unknown as Blob).text()))
|
|
366
|
+
.then(
|
|
367
|
+
(text: string) => ({ ok: true as const, text }),
|
|
368
|
+
(error: unknown) => ({ ok: false as const, error })
|
|
369
|
+
);
|
|
370
|
+
const previous = this.messageQueues.get(source) ?? Promise.resolve();
|
|
371
|
+
const next = previous
|
|
372
|
+
.then(async () => {
|
|
373
|
+
if (source !== this.socket || !this.isConnected) return;
|
|
374
|
+
// Independent Blob.text() promises can finish out of order, including
|
|
375
|
+
// a terminal frame overtaking earlier tokens. Emit in arrival order.
|
|
376
|
+
const result = await decoded;
|
|
369
377
|
if (source !== this.socket || !this.isConnected) return;
|
|
370
|
-
|
|
378
|
+
if (!result.ok) throw result.error;
|
|
379
|
+
const data = JSON.parse(result.text);
|
|
371
380
|
let payload = null;
|
|
372
381
|
if (data.data) {
|
|
373
382
|
payload = JSON.parse(base64Decode(data.data));
|
|
@@ -387,6 +396,9 @@ class WebSocketClient extends RestClient<SocketEventMap> implements IWebSocketCl
|
|
|
387
396
|
.catch((err: any) => {
|
|
388
397
|
this._logger.error('Failed to parse WebSocket message:', err);
|
|
389
398
|
});
|
|
399
|
+
// A failed frame must not poison later frames. A new socket has its own
|
|
400
|
+
// queue, so a pending decode on an old connection cannot stall reconnect.
|
|
401
|
+
this.messageQueues.set(source, next);
|
|
390
402
|
}
|
|
391
403
|
|
|
392
404
|
async send<T extends MessageType>(
|
package/src/Projects/index.ts
CHANGED
|
@@ -394,10 +394,39 @@ export type ProjectStatusSnapshot = Partial<
|
|
|
394
394
|
finished: boolean;
|
|
395
395
|
workerJobs: RawProject['workerJobs'];
|
|
396
396
|
completedWorkerJobs: RawProject['completedWorkerJobs'];
|
|
397
|
+
/** For a failed project: why, as its owner was told (status-only records keep it for a day). */
|
|
398
|
+
failure?: ProjectFailure;
|
|
397
399
|
};
|
|
398
400
|
|
|
399
401
|
const IN_FLIGHT_LOOKUP_STATUSES: ReadonlySet<string> = new Set(['pending', 'queued', 'processing']);
|
|
400
402
|
|
|
403
|
+
/** The server's failure explanation, its string fields only; null when it has none. */
|
|
404
|
+
function projectFailure(value: unknown): ProjectFailure | null {
|
|
405
|
+
if (!value || typeof value !== 'object') return null;
|
|
406
|
+
const record = value as Record<string, unknown>;
|
|
407
|
+
const failure: ProjectFailure = {};
|
|
408
|
+
for (const key of ['code', 'category', 'message'] as const) {
|
|
409
|
+
if (typeof record[key] === 'string' && record[key]) failure[key] = record[key] as string;
|
|
410
|
+
}
|
|
411
|
+
return Object.keys(failure).length ? failure : null;
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
/**
|
|
415
|
+
* Why a failed project failed, as its owner was told when it did: the provider's
|
|
416
|
+
* code, Sogni's category and the public message (the `jobError` event's
|
|
417
|
+
* `vendorErrorCode`, `vendorFailureCategory` and `error_message`). Kept with the
|
|
418
|
+
* project's status for a day, so a client that reconnects, or follows the
|
|
419
|
+
* project from another app, can say why it failed.
|
|
420
|
+
*/
|
|
421
|
+
export interface ProjectFailure {
|
|
422
|
+
/** The provider's error code, e.g. `OutputAudioSensitiveContentDetected.PolicyViolation`. */
|
|
423
|
+
code?: string;
|
|
424
|
+
/** Sogni's category for it, e.g. `content_policy`, `vendor_transient`, `timeout`. */
|
|
425
|
+
category?: string;
|
|
426
|
+
/** The message shown to the account when it failed. */
|
|
427
|
+
message?: string;
|
|
428
|
+
}
|
|
429
|
+
|
|
401
430
|
/**
|
|
402
431
|
* One render of a project returned by {@link ProjectsApi.getResult}.
|
|
403
432
|
*/
|
|
@@ -444,6 +473,8 @@ export interface ProjectResult {
|
|
|
444
473
|
* did not report one.
|
|
445
474
|
*/
|
|
446
475
|
waitingReason?: WaitingReason | null;
|
|
476
|
+
/** For a failed project: why, as its owner was told. Absent when the server recorded no explanation. */
|
|
477
|
+
failure?: ProjectFailure;
|
|
447
478
|
jobs: ProjectResultJob[];
|
|
448
479
|
}
|
|
449
480
|
|
|
@@ -1081,6 +1112,9 @@ class ProjectsApi extends ApiGroup<ProjectApiEvents> {
|
|
|
1081
1112
|
if (data.vendorFailureCategory) {
|
|
1082
1113
|
error.vendorFailureCategory = data.vendorFailureCategory;
|
|
1083
1114
|
}
|
|
1115
|
+
if (typeof data.vendorErrorCode === 'string' && data.vendorErrorCode) {
|
|
1116
|
+
error.vendorErrorCode = data.vendorErrorCode;
|
|
1117
|
+
}
|
|
1084
1118
|
if (data.subscriptionLimit) {
|
|
1085
1119
|
error.subscriptionLimit = true;
|
|
1086
1120
|
if (data.requiredPlans) error.requiredPlans = data.requiredPlans;
|
|
@@ -2633,6 +2667,7 @@ class ProjectsApi extends ApiGroup<ProjectApiEvents> {
|
|
|
2633
2667
|
// The stored record's model carries its media type even though ProjectModel does not declare it.
|
|
2634
2668
|
const recordType = (snapshot.model as { type?: unknown } | undefined)?.type;
|
|
2635
2669
|
const projectType = typeof recordType === 'string' ? recordType : undefined;
|
|
2670
|
+
const failure = projectFailure(snapshot.failure);
|
|
2636
2671
|
const jobs: ProjectResultJob[] = [];
|
|
2637
2672
|
const seen = new Set<string>();
|
|
2638
2673
|
for (const job of [...(snapshot.completedWorkerJobs || []), ...(snapshot.workerJobs || [])]) {
|
|
@@ -2684,6 +2719,7 @@ class ProjectsApi extends ApiGroup<ProjectApiEvents> {
|
|
|
2684
2719
|
finished: snapshot.finished,
|
|
2685
2720
|
...(modelId ? { modelId } : {}),
|
|
2686
2721
|
...(snapshot.waitingReason !== undefined ? { waitingReason: snapshot.waitingReason } : {}),
|
|
2722
|
+
...(failure ? { failure } : {}),
|
|
2687
2723
|
jobs
|
|
2688
2724
|
};
|
|
2689
2725
|
}
|
package/src/index.ts
CHANGED
|
@@ -108,6 +108,7 @@ import type {
|
|
|
108
108
|
ListRecentProjectsOptions,
|
|
109
109
|
ProjectLookupStatus,
|
|
110
110
|
ProjectResolution,
|
|
111
|
+
ProjectFailure,
|
|
111
112
|
ProjectResult,
|
|
112
113
|
ProjectResultJob,
|
|
113
114
|
ProjectStatusSnapshot,
|
|
@@ -469,6 +470,7 @@ export type {
|
|
|
469
470
|
ProjectLookupStatus,
|
|
470
471
|
ProjectRecoverySnapshot,
|
|
471
472
|
ProjectResolution,
|
|
473
|
+
ProjectFailure,
|
|
472
474
|
ProjectResult,
|
|
473
475
|
ProjectResultJob,
|
|
474
476
|
ProjectStatusSnapshot,
|
package/src/types/ErrorData.ts
CHANGED
|
@@ -12,6 +12,15 @@ interface ErrorData {
|
|
|
12
12
|
* with a generic failure message.
|
|
13
13
|
*/
|
|
14
14
|
vendorFailureCategory?: string;
|
|
15
|
+
/**
|
|
16
|
+
* The provider's own error code for an external generation failure, when it
|
|
17
|
+
* gave one, e.g. `OutputAudioSensitiveContentDetected.PolicyViolation` (the
|
|
18
|
+
* provider flagged the soundtrack it generated, usually as possibly matching
|
|
19
|
+
* copyrighted audio) or `OutputVideoSensitiveContentDetected` (the picture).
|
|
20
|
+
* Lets an app tell kinds of refusal apart within one category without
|
|
21
|
+
* reading the message.
|
|
22
|
+
*/
|
|
23
|
+
vendorErrorCode?: string;
|
|
15
24
|
/**
|
|
16
25
|
* Discriminator set to `true` when this render-path error is a subscription
|
|
17
26
|
* FEATURE-gate denial (socket error code 4081). The four fields below are
|
package/src/version.ts
CHANGED