@sogni-ai/sogni-client 4.0.0-alpha.3 → 4.0.0-alpha.30

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.
Files changed (76) hide show
  1. package/CHANGELOG.md +213 -0
  2. package/README.md +279 -28
  3. package/dist/Account/index.d.ts +18 -16
  4. package/dist/Account/index.js +31 -20
  5. package/dist/Account/index.js.map +1 -1
  6. package/dist/ApiClient/WebSocketClient/BrowserWebSocketClient/ChannelCoordinator.d.ts +66 -0
  7. package/dist/ApiClient/WebSocketClient/BrowserWebSocketClient/ChannelCoordinator.js +332 -0
  8. package/dist/ApiClient/WebSocketClient/BrowserWebSocketClient/ChannelCoordinator.js.map +1 -0
  9. package/dist/ApiClient/WebSocketClient/BrowserWebSocketClient/index.d.ts +28 -0
  10. package/dist/ApiClient/WebSocketClient/BrowserWebSocketClient/index.js +203 -0
  11. package/dist/ApiClient/WebSocketClient/BrowserWebSocketClient/index.js.map +1 -0
  12. package/dist/ApiClient/WebSocketClient/events.d.ts +11 -0
  13. package/dist/ApiClient/WebSocketClient/index.d.ts +2 -2
  14. package/dist/ApiClient/WebSocketClient/index.js +13 -3
  15. package/dist/ApiClient/WebSocketClient/index.js.map +1 -1
  16. package/dist/ApiClient/WebSocketClient/types.d.ts +13 -0
  17. package/dist/ApiClient/index.d.ts +4 -4
  18. package/dist/ApiClient/index.js +23 -4
  19. package/dist/ApiClient/index.js.map +1 -1
  20. package/dist/Projects/Job.d.ts +24 -4
  21. package/dist/Projects/Job.js +58 -16
  22. package/dist/Projects/Job.js.map +1 -1
  23. package/dist/Projects/Project.d.ts +8 -0
  24. package/dist/Projects/Project.js +27 -6
  25. package/dist/Projects/Project.js.map +1 -1
  26. package/dist/Projects/createJobRequestMessage.js +109 -15
  27. package/dist/Projects/createJobRequestMessage.js.map +1 -1
  28. package/dist/Projects/index.d.ts +110 -11
  29. package/dist/Projects/index.js +412 -42
  30. package/dist/Projects/index.js.map +1 -1
  31. package/dist/Projects/types/EstimationResponse.d.ts +2 -0
  32. package/dist/Projects/types/SamplerParams.d.ts +13 -0
  33. package/dist/Projects/types/SamplerParams.js +26 -0
  34. package/dist/Projects/types/SamplerParams.js.map +1 -0
  35. package/dist/Projects/types/SchedulerParams.d.ts +14 -0
  36. package/dist/Projects/types/SchedulerParams.js +24 -0
  37. package/dist/Projects/types/SchedulerParams.js.map +1 -0
  38. package/dist/Projects/types/events.d.ts +5 -1
  39. package/dist/Projects/types/index.d.ts +150 -39
  40. package/dist/Projects/types/index.js +13 -0
  41. package/dist/Projects/types/index.js.map +1 -1
  42. package/dist/Projects/utils.d.ts +19 -1
  43. package/dist/Projects/utils.js +68 -0
  44. package/dist/Projects/utils.js.map +1 -1
  45. package/dist/index.d.ts +12 -4
  46. package/dist/index.js +12 -4
  47. package/dist/index.js.map +1 -1
  48. package/dist/lib/AuthManager/TokenAuthManager.js +0 -2
  49. package/dist/lib/AuthManager/TokenAuthManager.js.map +1 -1
  50. package/dist/lib/DataEntity.js +4 -2
  51. package/dist/lib/DataEntity.js.map +1 -1
  52. package/dist/lib/validation.d.ts +7 -0
  53. package/dist/lib/validation.js +36 -0
  54. package/dist/lib/validation.js.map +1 -1
  55. package/package.json +4 -4
  56. package/src/Account/index.ts +30 -19
  57. package/src/ApiClient/WebSocketClient/BrowserWebSocketClient/ChannelCoordinator.ts +426 -0
  58. package/src/ApiClient/WebSocketClient/BrowserWebSocketClient/index.ts +237 -0
  59. package/src/ApiClient/WebSocketClient/events.ts +13 -0
  60. package/src/ApiClient/WebSocketClient/index.ts +15 -5
  61. package/src/ApiClient/WebSocketClient/types.ts +16 -0
  62. package/src/ApiClient/index.ts +30 -8
  63. package/src/Projects/Job.ts +64 -16
  64. package/src/Projects/Project.ts +29 -9
  65. package/src/Projects/createJobRequestMessage.ts +155 -36
  66. package/src/Projects/index.ts +437 -46
  67. package/src/Projects/types/EstimationResponse.ts +2 -0
  68. package/src/Projects/types/SamplerParams.ts +24 -0
  69. package/src/Projects/types/SchedulerParams.ts +22 -0
  70. package/src/Projects/types/events.ts +6 -0
  71. package/src/Projects/types/index.ts +181 -47
  72. package/src/Projects/utils.ts +66 -1
  73. package/src/index.ts +38 -11
  74. package/src/lib/AuthManager/TokenAuthManager.ts +0 -2
  75. package/src/lib/DataEntity.ts +4 -2
  76. package/src/lib/validation.ts +41 -0
@@ -20,9 +20,37 @@ const getUUID_1 = __importDefault(require("../lib/getUUID"));
20
20
  const Cache_1 = __importDefault(require("../lib/Cache"));
21
21
  const Job_1 = require("./Job");
22
22
  const utils_1 = require("./utils");
23
+ const validation_1 = require("../lib/validation");
23
24
  const sizePresetCache = new Cache_1.default(10 * 60 * 1000);
24
25
  const GARBAGE_COLLECT_TIMEOUT = 30000;
25
26
  const MODELS_REFRESH_INTERVAL = 1000 * 60 * 60 * 24; // 24 hours
27
+ /**
28
+ * Detect content type from a file object.
29
+ * For File objects in browser, uses the type property.
30
+ * Returns undefined if content type cannot be detected.
31
+ */
32
+ function getFileContentType(file) {
33
+ if (file instanceof Blob && 'type' in file && file.type) {
34
+ return file.type;
35
+ }
36
+ return undefined;
37
+ }
38
+ /**
39
+ * Convert file to a format compatible with fetch body.
40
+ * Converts Node.js Buffer to Blob for cross-platform compatibility.
41
+ */
42
+ function toFetchBody(file) {
43
+ // Node.js Buffer is not supported in browsers, so we can skip this conversion
44
+ if (typeof Buffer === 'undefined') {
45
+ return file;
46
+ }
47
+ if (Buffer.isBuffer(file)) {
48
+ // Copy Buffer data to a new ArrayBuffer to ensure type compatibility
49
+ const arrayBuffer = file.buffer.slice(file.byteOffset, file.byteOffset + file.byteLength);
50
+ return new Blob([arrayBuffer]);
51
+ }
52
+ return file;
53
+ }
26
54
  function mapErrorCodes(code) {
27
55
  switch (code) {
28
56
  case 'serverRestarting':
@@ -43,6 +71,20 @@ class ProjectsApi extends ApiGroup_1.default {
43
71
  get availableModels() {
44
72
  return this._availableModels;
45
73
  }
74
+ /**
75
+ * Check if a model produces video output using the cached models list.
76
+ * Uses the `media` property from the models API when available,
77
+ * falls back to model ID prefix check if models aren't loaded yet.
78
+ */
79
+ isVideoModelId(modelId) {
80
+ var _a;
81
+ const model = (_a = this._supportedModels.data) === null || _a === void 0 ? void 0 : _a.find((m) => m.id === modelId);
82
+ if (model) {
83
+ return model.media === 'video';
84
+ }
85
+ // Fallback to prefix check if models not loaded
86
+ return (0, utils_1.isVideoModel)(modelId);
87
+ }
46
88
  constructor(config) {
47
89
  super(config);
48
90
  this._availableModels = [];
@@ -56,6 +98,7 @@ class ProjectsApi extends ApiGroup_1.default {
56
98
  this.client.socket.on('swarmModels', this.handleSwarmModels.bind(this));
57
99
  this.client.socket.on('jobState', this.handleJobState.bind(this));
58
100
  this.client.socket.on('jobProgress', this.handleJobProgress.bind(this));
101
+ this.client.socket.on('jobETA', this.handleJobETA.bind(this));
59
102
  this.client.socket.on('jobError', this.handleJobError.bind(this));
60
103
  this.client.socket.on('jobResult', this.handleJobResult.bind(this));
61
104
  // Listen to the server disconnect event
@@ -64,6 +107,16 @@ class ProjectsApi extends ApiGroup_1.default {
64
107
  this.on('project', this.handleProjectEvent.bind(this));
65
108
  this.on('job', this.handleJobEvent.bind(this));
66
109
  }
110
+ /**
111
+ * Retrieves a list of projects created and tracked by this SogniClient instance.
112
+ *
113
+ * Note: When a project is finished, it will be removed from this list after 30 seconds
114
+ *
115
+ * @return {Array} A copy of the array containing the tracked projects.
116
+ */
117
+ get trackedProjects() {
118
+ return this.projects.slice(0);
119
+ }
67
120
  handleChangeNetwork() {
68
121
  this._availableModels = [];
69
122
  this.emit('availableModels', this._availableModels);
@@ -82,11 +135,12 @@ class ProjectsApi extends ApiGroup_1.default {
82
135
  return acc;
83
136
  }, {});
84
137
  this._availableModels = Object.entries(data).map(([id, workerCount]) => {
85
- var _a;
138
+ var _a, _b;
86
139
  return ({
87
140
  id,
88
141
  name: ((_a = modelIndex[id]) === null || _a === void 0 ? void 0 : _a.name) || id.replace(/-/g, ' '),
89
- workerCount
142
+ workerCount,
143
+ media: ((_b = modelIndex[id]) === null || _b === void 0 ? void 0 : _b.media) || 'image'
90
144
  });
91
145
  });
92
146
  this.emit('availableModels', this._availableModels);
@@ -154,6 +208,16 @@ class ProjectsApi extends ApiGroup_1.default {
154
208
  }
155
209
  });
156
210
  }
211
+ handleJobETA(data) {
212
+ return __awaiter(this, void 0, void 0, function* () {
213
+ this.emit('job', {
214
+ type: 'jobETA',
215
+ projectId: data.jobID,
216
+ jobId: data.imgID || '',
217
+ etaSeconds: data.etaSeconds
218
+ });
219
+ });
220
+ }
157
221
  handleJobResult(data) {
158
222
  return __awaiter(this, void 0, void 0, function* () {
159
223
  const project = this.projects.find((p) => p.id === data.jobID);
@@ -162,11 +226,22 @@ class ProjectsApi extends ApiGroup_1.default {
162
226
  // If NSFW filter is triggered, image will be only available for download if user explicitly
163
227
  // disabled the filter for this project
164
228
  if (passNSFWCheck && !data.userCanceled) {
165
- downloadUrl = yield this.downloadUrl({
166
- jobId: data.jobID,
167
- imageId: data.imgID,
168
- type: 'complete'
169
- });
229
+ // Use media endpoint for video models, image endpoint for image models
230
+ const isVideo = project && this.isVideoModelId(project.params.modelId);
231
+ if (isVideo) {
232
+ downloadUrl = yield this.mediaDownloadUrl({
233
+ jobId: data.jobID,
234
+ id: data.imgID,
235
+ type: 'complete'
236
+ });
237
+ }
238
+ else {
239
+ downloadUrl = yield this.downloadUrl({
240
+ jobId: data.jobID,
241
+ imageId: data.imgID,
242
+ type: 'complete'
243
+ });
244
+ }
170
245
  }
171
246
  this.emit('job', {
172
247
  type: 'completed',
@@ -238,7 +313,11 @@ class ProjectsApi extends ApiGroup_1.default {
238
313
  if (project.finished) {
239
314
  // Sync project data with the server and remove it from the list after some time
240
315
  project._syncToServer().catch((e) => {
241
- this.client.logger.error(e);
316
+ // 404 errors are expected when project is still initializing
317
+ // Only log non-404 errors to avoid confusing users
318
+ if (e.status !== 404) {
319
+ this.client.logger.error(e);
320
+ }
242
321
  });
243
322
  setTimeout(() => {
244
323
  this.projects = this.projects.filter((p) => !p.finished);
@@ -246,6 +325,7 @@ class ProjectsApi extends ApiGroup_1.default {
246
325
  }
247
326
  }
248
327
  handleJobEvent(event) {
328
+ var _a;
249
329
  let project = this.projects.find((p) => p.id === event.projectId);
250
330
  if (!project) {
251
331
  return;
@@ -257,7 +337,7 @@ class ProjectsApi extends ApiGroup_1.default {
257
337
  projectId: event.projectId,
258
338
  status: 'pending',
259
339
  step: 0,
260
- stepCount: project.params.steps
340
+ stepCount: (_a = project.params.steps) !== null && _a !== void 0 ? _a : 0
261
341
  });
262
342
  }
263
343
  switch (event.type) {
@@ -284,7 +364,7 @@ class ProjectsApi extends ApiGroup_1.default {
284
364
  case 'progress':
285
365
  job._update({
286
366
  status: 'processing',
287
- // Jus in case event comes out of order
367
+ // Just in case event comes out of order
288
368
  step: Math.max(event.step, job.step),
289
369
  stepCount: event.stepCount
290
370
  });
@@ -292,6 +372,15 @@ class ProjectsApi extends ApiGroup_1.default {
292
372
  project._update({ status: 'processing' });
293
373
  }
294
374
  break;
375
+ case 'jobETA':
376
+ // ETA updates keep the project alive (refreshes lastUpdated) and store the ETA value.
377
+ // This is critical for long-running jobs like video generation that can take several
378
+ // minutes and may not send frequent progress updates.
379
+ // We call _keepAlive() directly to ensure lastUpdated is refreshed even if the ETA
380
+ // value hasn't changed (job._update only emits 'updated' when values actually change).
381
+ project._keepAlive();
382
+ job._update({ etaSeconds: event.etaSeconds });
383
+ break;
295
384
  case 'preview':
296
385
  job._update({ previewUrl: event.url });
297
386
  break;
@@ -308,6 +397,17 @@ class ProjectsApi extends ApiGroup_1.default {
308
397
  }
309
398
  case 'error':
310
399
  job._update({ status: 'failed', error: event.error });
400
+ // Check if project should also fail when a job fails
401
+ // For video jobs (single image) or when all jobs have failed, propagate to project
402
+ const allJobsStarted = project.jobs.length >= project.params.numberOfMedia;
403
+ const allJobsFailed = allJobsStarted && project.jobs.every((j) => j.status === 'failed');
404
+ const isSingleJobProject = project.params.numberOfMedia === 1;
405
+ if (isSingleJobProject || allJobsFailed) {
406
+ project._update({
407
+ status: 'failed',
408
+ error: event.error
409
+ });
410
+ }
311
411
  break;
312
412
  }
313
413
  }
@@ -328,41 +428,67 @@ class ProjectsApi extends ApiGroup_1.default {
328
428
  return Promise.resolve(this._availableModels);
329
429
  }
330
430
  return new Promise((resolve, reject) => {
431
+ let settled = false;
331
432
  const timeoutId = setTimeout(() => {
332
- reject(new Error('Timeout waiting for models'));
433
+ if (!settled) {
434
+ settled = true;
435
+ this.off('availableModels', handler);
436
+ reject(new Error('Timeout waiting for models'));
437
+ }
333
438
  }, timeout);
334
- this.once('availableModels', (models) => {
335
- clearTimeout(timeoutId);
336
- if (models.length) {
439
+ const handler = (models) => {
440
+ // Only resolve when we get a non-empty models list
441
+ // Empty arrays may be emitted during disconnects/reconnects
442
+ if (models.length && !settled) {
443
+ settled = true;
444
+ clearTimeout(timeoutId);
445
+ this.off('availableModels', handler);
337
446
  resolve(models);
338
447
  }
339
- else {
340
- reject(new Error('No models available'));
341
- }
342
- });
448
+ };
449
+ this.on('availableModels', handler);
343
450
  });
344
451
  }
345
452
  /**
346
453
  * Send new project request to the network. Returns project instance which can be used to track
347
- * progress and get resulting images.
454
+ * progress and get resulting images or videos.
348
455
  * @param data
349
456
  */
350
457
  create(data) {
351
458
  return __awaiter(this, void 0, void 0, function* () {
352
- var _a, _b;
353
459
  const project = new Project_1.default(Object.assign({}, data), { api: this, logger: this.client.logger });
460
+ const request = (0, createJobRequestMessage_1.default)(project.id, data);
461
+ switch (data.type) {
462
+ case 'image':
463
+ yield this._processImageAssets(project, data);
464
+ break;
465
+ case 'video':
466
+ yield this._processVideoAssets(project, data);
467
+ break;
468
+ }
469
+ yield this.client.socket.send('jobRequest', request);
470
+ this.projects.push(project);
471
+ return project;
472
+ });
473
+ }
474
+ _processImageAssets(project, data) {
475
+ return __awaiter(this, void 0, void 0, function* () {
476
+ var _a, _b;
477
+ //Guide image
354
478
  if (data.startingImage && data.startingImage !== true) {
355
479
  yield this.uploadGuideImage(project.id, data.startingImage);
356
480
  }
481
+ // ControlNet image
357
482
  if (((_a = data.controlNet) === null || _a === void 0 ? void 0 : _a.image) && data.controlNet.image !== true) {
358
483
  yield this.uploadCNImage(project.id, data.controlNet.image);
359
484
  }
485
+ // Context images (Flux.2 Dev, Qwen Image Edit Plus support up to 3; Flux Kontext supports up to 2)
360
486
  if ((_b = data.contextImages) === null || _b === void 0 ? void 0 : _b.length) {
361
- if (data.contextImages.length > 2) {
487
+ if (data.contextImages.length > 3) {
362
488
  throw new ApiClient_1.ApiError(500, {
363
489
  status: 'error',
364
490
  errorCode: 0,
365
- message: `Up to 2 context images are supported`
491
+ message: `Up to 3 context images are supported`
366
492
  });
367
493
  }
368
494
  yield Promise.all(data.contextImages.map((image, index) => {
@@ -371,10 +497,22 @@ class ProjectsApi extends ApiGroup_1.default {
371
497
  }
372
498
  }));
373
499
  }
374
- const request = (0, createJobRequestMessage_1.default)(project.id, data);
375
- yield this.client.socket.send('jobRequest', request);
376
- this.projects.push(project);
377
- return project;
500
+ });
501
+ }
502
+ _processVideoAssets(project, data) {
503
+ return __awaiter(this, void 0, void 0, function* () {
504
+ if ((data === null || data === void 0 ? void 0 : data.referenceImage) && data.referenceImage !== true) {
505
+ yield this.uploadReferenceImage(project.id, data.referenceImage);
506
+ }
507
+ if ((data === null || data === void 0 ? void 0 : data.referenceImageEnd) && data.referenceImageEnd !== true) {
508
+ yield this.uploadReferenceImageEnd(project.id, data.referenceImageEnd);
509
+ }
510
+ if ((data === null || data === void 0 ? void 0 : data.referenceAudio) && data.referenceAudio !== true) {
511
+ yield this.uploadReferenceAudio(project.id, data.referenceAudio);
512
+ }
513
+ if ((data === null || data === void 0 ? void 0 : data.referenceVideo) && data.referenceVideo !== true) {
514
+ yield this.uploadReferenceVideo(project.id, data.referenceVideo);
515
+ }
378
516
  });
379
517
  }
380
518
  /**
@@ -425,13 +563,13 @@ class ProjectsApi extends ApiGroup_1.default {
425
563
  return __awaiter(this, void 0, void 0, function* () {
426
564
  const imageId = (0, getUUID_1.default)();
427
565
  const presignedUrl = yield this.uploadUrl({
428
- imageId: imageId,
566
+ imageId,
429
567
  jobId: projectId,
430
568
  type: 'startingImage'
431
569
  });
432
570
  const res = yield fetch(presignedUrl, {
433
571
  method: 'PUT',
434
- body: file
572
+ body: toFetchBody(file)
435
573
  });
436
574
  if (!res.ok) {
437
575
  throw new ApiClient_1.ApiError(res.status, {
@@ -447,13 +585,13 @@ class ProjectsApi extends ApiGroup_1.default {
447
585
  return __awaiter(this, void 0, void 0, function* () {
448
586
  const imageId = (0, getUUID_1.default)();
449
587
  const presignedUrl = yield this.uploadUrl({
450
- imageId: imageId,
588
+ imageId,
451
589
  jobId: projectId,
452
590
  type: 'cnImage'
453
591
  });
454
592
  const res = yield fetch(presignedUrl, {
455
593
  method: 'PUT',
456
- body: file
594
+ body: toFetchBody(file)
457
595
  });
458
596
  if (!res.ok) {
459
597
  throw new ApiClient_1.ApiError(res.status, {
@@ -476,7 +614,7 @@ class ProjectsApi extends ApiGroup_1.default {
476
614
  });
477
615
  const res = yield fetch(presignedUrl, {
478
616
  method: 'PUT',
479
- body: file
617
+ body: toFetchBody(file)
480
618
  });
481
619
  if (!res.ok) {
482
620
  throw new ApiClient_1.ApiError(res.status, {
@@ -488,14 +626,132 @@ class ProjectsApi extends ApiGroup_1.default {
488
626
  return imageId;
489
627
  });
490
628
  }
629
+ // ============================================
630
+ // VIDEO WORKFLOW UPLOADS (WAN 2.2)
631
+ // ============================================
632
+ /**
633
+ * Upload reference image for WAN video workflows
634
+ * @internal
635
+ */
636
+ uploadReferenceImage(projectId, file) {
637
+ return __awaiter(this, void 0, void 0, function* () {
638
+ const imageId = (0, getUUID_1.default)();
639
+ const presignedUrl = yield this.uploadUrl({
640
+ imageId,
641
+ jobId: projectId,
642
+ type: 'referenceImage'
643
+ });
644
+ const res = yield fetch(presignedUrl, {
645
+ method: 'PUT',
646
+ body: toFetchBody(file)
647
+ });
648
+ if (!res.ok) {
649
+ throw new ApiClient_1.ApiError(res.status, {
650
+ status: 'error',
651
+ errorCode: 0,
652
+ message: 'Failed to upload reference image'
653
+ });
654
+ }
655
+ return imageId;
656
+ });
657
+ }
491
658
  /**
492
- * Estimate project cost
659
+ * Upload reference image end for i2v interpolation
660
+ * @internal
661
+ */
662
+ uploadReferenceImageEnd(projectId, file) {
663
+ return __awaiter(this, void 0, void 0, function* () {
664
+ const imageId = (0, getUUID_1.default)();
665
+ const presignedUrl = yield this.uploadUrl({
666
+ imageId,
667
+ jobId: projectId,
668
+ type: 'referenceImageEnd'
669
+ });
670
+ const res = yield fetch(presignedUrl, {
671
+ method: 'PUT',
672
+ body: toFetchBody(file)
673
+ });
674
+ if (!res.ok) {
675
+ throw new ApiClient_1.ApiError(res.status, {
676
+ status: 'error',
677
+ errorCode: 0,
678
+ message: 'Failed to upload reference image end'
679
+ });
680
+ }
681
+ return imageId;
682
+ });
683
+ }
684
+ /**
685
+ * Upload reference audio for s2v workflows
686
+ * Supported formats: mp3, m4a, wav
687
+ * @internal
688
+ */
689
+ uploadReferenceAudio(projectId, file) {
690
+ return __awaiter(this, void 0, void 0, function* () {
691
+ const contentType = getFileContentType(file);
692
+ const presignedUrl = yield this.mediaUploadUrl({
693
+ jobId: projectId,
694
+ type: 'referenceAudio'
695
+ });
696
+ const headers = {};
697
+ if (contentType) {
698
+ headers['Content-Type'] = contentType;
699
+ }
700
+ const res = yield fetch(presignedUrl, {
701
+ method: 'PUT',
702
+ body: toFetchBody(file),
703
+ headers
704
+ });
705
+ if (!res.ok) {
706
+ throw new ApiClient_1.ApiError(res.status, {
707
+ status: 'error',
708
+ errorCode: 0,
709
+ message: 'Failed to upload reference audio'
710
+ });
711
+ }
712
+ });
713
+ }
714
+ /**
715
+ * Upload reference video for animate workflows
716
+ * Supported formats: mp4, mov
717
+ * @internal
718
+ */
719
+ uploadReferenceVideo(projectId, file) {
720
+ return __awaiter(this, void 0, void 0, function* () {
721
+ const contentType = getFileContentType(file);
722
+ const presignedUrl = yield this.mediaUploadUrl({
723
+ jobId: projectId,
724
+ type: 'referenceVideo'
725
+ });
726
+ const headers = {};
727
+ if (contentType) {
728
+ headers['Content-Type'] = contentType;
729
+ }
730
+ const res = yield fetch(presignedUrl, {
731
+ method: 'PUT',
732
+ body: toFetchBody(file),
733
+ headers
734
+ });
735
+ if (!res.ok) {
736
+ throw new ApiClient_1.ApiError(res.status, {
737
+ status: 'error',
738
+ errorCode: 0,
739
+ message: 'Failed to upload reference video'
740
+ });
741
+ }
742
+ });
743
+ }
744
+ // ============================================
745
+ // COST ESTIMATION
746
+ // ============================================
747
+ /**
748
+ * Estimate image project cost
493
749
  */
494
750
  estimateCost(_a) {
495
- return __awaiter(this, arguments, void 0, function* ({ network, tokenType, model, imageCount, stepCount, previewCount, cnEnabled, startingImageStrength, width, height, sizePreset, guidance, scheduler, contextImages }) {
751
+ return __awaiter(this, arguments, void 0, function* ({ network, tokenType, model, imageCount, stepCount, previewCount, cnEnabled, startingImageStrength, width, height, sizePreset, guidance, sampler, contextImages }) {
496
752
  let apiVersion = 2;
497
753
  const pathParams = [
498
- tokenType || 'sogni',
754
+ tokenType || 'spark',
499
755
  network,
500
756
  model,
501
757
  imageCount,
@@ -518,21 +774,28 @@ class ProjectsApi extends ApiGroup_1.default {
518
774
  else {
519
775
  pathParams.push(0, 0);
520
776
  }
521
- if (scheduler) {
777
+ if (sampler) {
522
778
  apiVersion = 3;
523
779
  pathParams.push(guidance || 0);
524
- pathParams.push(scheduler || '');
780
+ pathParams.push((0, validation_1.validateSampler)(sampler));
525
781
  pathParams.push(contextImages || 0);
526
782
  }
527
783
  const r = yield this.client.socket.get(`/api/v${apiVersion}/job/estimate/${pathParams.join('/')}`);
528
784
  return {
529
785
  token: r.quote.project.costInToken,
530
- usd: r.quote.project.costInUSD
786
+ usd: r.quote.project.costInUSD,
787
+ spark: r.quote.project.costInSpark,
788
+ sogni: r.quote.project.costInSogni
531
789
  };
532
790
  });
533
791
  }
792
+ /**
793
+ * Estimate image enhancement cost
794
+ * @param strength
795
+ * @param tokenType
796
+ */
534
797
  estimateEnhancementCost(strength_1) {
535
- return __awaiter(this, arguments, void 0, function* (strength, tokenType = 'sogni') {
798
+ return __awaiter(this, arguments, void 0, function* (strength, tokenType = 'spark') {
536
799
  return this.estimateCost({
537
800
  network: Job_1.enhancementDefaults.network,
538
801
  tokenType,
@@ -545,10 +808,50 @@ class ProjectsApi extends ApiGroup_1.default {
545
808
  });
546
809
  });
547
810
  }
811
+ /**
812
+ * Estimates the cost of generating a video based on the provided parameters.
813
+ *
814
+ * @param {VideoEstimateRequest} params - The parameters required for video cost estimation. This includes:
815
+ * - tokenType: The type of token to be used for generation.
816
+ * - model: The model to be used for video generation.
817
+ * - width: The width of the video in pixels.
818
+ * - height: The height of the video in pixels.
819
+ * - frames: The total number of frames in the video.
820
+ * - fps: The frames per second for the video.
821
+ * - steps: Number of steps.
822
+ * @return {Promise<Object>} Returns an object containing the estimated costs for the video in different units:
823
+ * - token: Cost in tokens.
824
+ * - usd: Cost in USD.
825
+ * - spark: Cost in Spark.
826
+ * - sogni: Cost in Sogni.
827
+ */
828
+ estimateVideoCost(params) {
829
+ return __awaiter(this, void 0, void 0, function* () {
830
+ const pathParams = [
831
+ params.tokenType,
832
+ params.model,
833
+ params.width,
834
+ params.height,
835
+ params.frames,
836
+ params.fps,
837
+ params.steps
838
+ ];
839
+ const path = pathParams.map((p) => encodeURIComponent(p)).join('/');
840
+ const r = yield this.client.socket.get(`/api/v1/job-video/estimate/${path}`);
841
+ return {
842
+ token: r.quote.project.costInToken,
843
+ usd: r.quote.project.costInUSD,
844
+ spark: r.quote.project.costInSpark,
845
+ sogni: r.quote.project.costInSogni
846
+ };
847
+ });
848
+ }
849
+ // ============================================
850
+ // URL HELPERS
851
+ // ============================================
548
852
  /**
549
853
  * Get upload URL for image
550
854
  * @internal
551
- * @param params
552
855
  */
553
856
  uploadUrl(params) {
554
857
  return __awaiter(this, void 0, void 0, function* () {
@@ -559,7 +862,6 @@ class ProjectsApi extends ApiGroup_1.default {
559
862
  /**
560
863
  * Get download URL for image
561
864
  * @internal
562
- * @param params
563
865
  */
564
866
  downloadUrl(params) {
565
867
  return __awaiter(this, void 0, void 0, function* () {
@@ -567,6 +869,29 @@ class ProjectsApi extends ApiGroup_1.default {
567
869
  return r.data.downloadUrl;
568
870
  });
569
871
  }
872
+ /**
873
+ * Get upload URL for media (video/audio)
874
+ * @internal
875
+ */
876
+ mediaUploadUrl(params) {
877
+ return __awaiter(this, void 0, void 0, function* () {
878
+ const r = yield this.client.rest.get(`/v1/media/uploadUrl`, params);
879
+ return r.data.uploadUrl;
880
+ });
881
+ }
882
+ /**
883
+ * Get download URL for media (video/audio)
884
+ * @internal
885
+ */
886
+ mediaDownloadUrl(params) {
887
+ return __awaiter(this, void 0, void 0, function* () {
888
+ const r = yield this.client.rest.get(`/v1/media/downloadUrl`, params);
889
+ return r.data.downloadUrl;
890
+ });
891
+ }
892
+ // ============================================
893
+ // MODEL/PRESET HELPERS
894
+ // ============================================
570
895
  getSupportedModels() {
571
896
  return __awaiter(this, arguments, void 0, function* (forceRefresh = false) {
572
897
  if (this._supportedModels.data &&
@@ -584,7 +909,7 @@ class ProjectsApi extends ApiGroup_1.default {
584
909
  *
585
910
  * @example
586
911
  * ```ts
587
- * const presets = await client.projects.getSizePresets('fast', 'flux1-schnell-fp8');
912
+ * const presets = await sogni.projects.getSizePresets('fast', 'flux1-schnell-fp8');
588
913
  * console.log(presets);
589
914
  * ```
590
915
  *
@@ -612,6 +937,50 @@ class ProjectsApi extends ApiGroup_1.default {
612
937
  return data;
613
938
  });
614
939
  }
940
+ /**
941
+ * Retrieves the video asset configuration for a given video model identifier.
942
+ * Validates whether the provided model ID corresponds to a video model. If it does,
943
+ * returns the appropriate video asset configuration based on the workflow type.
944
+ *
945
+ * @example Returned object for a model that implements image to video workflow:
946
+ * ```json
947
+ * {
948
+ * "workflowType": "i2v",
949
+ * "assets": {
950
+ * "referenceImage": "required",
951
+ * "referenceImageEnd": "optional",
952
+ * "referenceAudio": "forbidden",
953
+ * "referenceVideo": "forbidden"
954
+ * }
955
+ * }
956
+ * ```
957
+ *
958
+ * @param {string} modelId - The identifier of the video model to retrieve the configuration for.
959
+ * @return {Object} The video asset configuration object where key is asset field and value is
960
+ * either `required`, `forbidden` or `optional`. Returns `null` if no rules defined for the model.
961
+ * @throws {ApiError} Throws an error if the provided model ID is not a video model.
962
+ */
963
+ getVideoAssetConfig(modelId) {
964
+ return __awaiter(this, void 0, void 0, function* () {
965
+ if (!this.isVideoModelId(modelId)) {
966
+ throw new ApiClient_1.ApiError(400, {
967
+ status: 'error',
968
+ errorCode: 0,
969
+ message: `Model ${modelId} is not a video model`
970
+ });
971
+ }
972
+ const workflow = (0, utils_1.getVideoWorkflowType)(modelId);
973
+ if (!workflow) {
974
+ return {
975
+ workflowType: null
976
+ };
977
+ }
978
+ return {
979
+ workflowType: workflow,
980
+ assets: utils_1.VIDEO_WORKFLOW_ASSETS[workflow]
981
+ };
982
+ });
983
+ }
615
984
  /**
616
985
  * Get available models and their worker counts. Normally, you would get list once you connect
617
986
  * to the server, but you can also call this method to get the list of available models manually.
@@ -627,7 +996,8 @@ class ProjectsApi extends ApiGroup_1.default {
627
996
  return {
628
997
  id: (model === null || model === void 0 ? void 0 : model.id) || sid,
629
998
  name: (model === null || model === void 0 ? void 0 : model.name) || sid.replace(/-/g, ' '),
630
- workerCount
999
+ workerCount,
1000
+ media: (model === null || model === void 0 ? void 0 : model.media) || 'image'
631
1001
  };
632
1002
  });
633
1003
  });