@sogni-ai/sogni-client 5.80.1 → 5.82.0

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 (215) hide show
  1. package/AGENTS.md +2 -1
  2. package/CHANGELOG.md +25 -0
  3. package/README.md +192 -2
  4. package/dist/Account/TwoFactorRequiredError.d.ts +8 -0
  5. package/dist/Account/TwoFactorRequiredError.js +14 -0
  6. package/dist/Account/TwoFactorRequiredError.js.map +1 -0
  7. package/dist/Account/index.d.ts +43 -0
  8. package/dist/Account/index.js +97 -13
  9. package/dist/Account/index.js.map +1 -1
  10. package/dist/ApiClient/WebSocketClient/BrowserWebSocketClient/ChannelCoordinator.js +6 -5
  11. package/dist/ApiClient/WebSocketClient/BrowserWebSocketClient/ChannelCoordinator.js.map +1 -1
  12. package/dist/ApiClient/WebSocketClient/BrowserWebSocketClient/index.d.ts +11 -0
  13. package/dist/ApiClient/WebSocketClient/BrowserWebSocketClient/index.js +43 -5
  14. package/dist/ApiClient/WebSocketClient/BrowserWebSocketClient/index.js.map +1 -1
  15. package/dist/ApiClient/WebSocketClient/events.d.ts +27 -0
  16. package/dist/ApiClient/WebSocketClient/index.d.ts +13 -0
  17. package/dist/ApiClient/WebSocketClient/index.js +33 -1
  18. package/dist/ApiClient/WebSocketClient/index.js.map +1 -1
  19. package/dist/ApiClient/WebSocketClient/messages.d.ts +8 -0
  20. package/dist/ApiClient/WebSocketClient/types.d.ts +4 -0
  21. package/dist/ApiClient/index.d.ts +19 -1
  22. package/dist/ApiClient/index.js +23 -1
  23. package/dist/ApiClient/index.js.map +1 -1
  24. package/dist/Chat/modelRouting.d.ts +1 -0
  25. package/dist/Chat/modelRouting.js +7 -1
  26. package/dist/Chat/modelRouting.js.map +1 -1
  27. package/dist/MediaEncryption/index.d.ts +153 -0
  28. package/dist/MediaEncryption/index.js +434 -0
  29. package/dist/MediaEncryption/index.js.map +1 -0
  30. package/dist/Projects/Job.d.ts +27 -1
  31. package/dist/Projects/Job.js +51 -4
  32. package/dist/Projects/Job.js.map +1 -1
  33. package/dist/Projects/Project.d.ts +29 -0
  34. package/dist/Projects/Project.js +43 -2
  35. package/dist/Projects/Project.js.map +1 -1
  36. package/dist/Projects/createJobRequestMessage.js +16 -0
  37. package/dist/Projects/createJobRequestMessage.js.map +1 -1
  38. package/dist/Projects/index.d.ts +63 -2
  39. package/dist/Projects/index.js +391 -71
  40. package/dist/Projects/index.js.map +1 -1
  41. package/dist/Projects/recovery.js +4 -0
  42. package/dist/Projects/recovery.js.map +1 -1
  43. package/dist/Projects/types/JobProvenance.d.ts +2 -0
  44. package/dist/Projects/types/PayAsYouGo.d.ts +88 -0
  45. package/dist/Projects/types/PayAsYouGo.js +60 -0
  46. package/dist/Projects/types/PayAsYouGo.js.map +1 -0
  47. package/dist/Projects/types/RawProject.d.ts +2 -0
  48. package/dist/Projects/types/events.d.ts +9 -1
  49. package/dist/Projects/types/index.d.ts +143 -1
  50. package/dist/Projects/types/index.js.map +1 -1
  51. package/dist/Projects/utils/imageLayers.d.ts +7 -0
  52. package/dist/Projects/utils/imageLayers.js +123 -0
  53. package/dist/Projects/utils/imageLayers.js.map +1 -0
  54. package/dist/browser/mediaEncryptionPlayback/encryptedMediaResponse.d.ts +11 -0
  55. package/dist/browser/mediaEncryptionPlayback/encryptedMediaResponse.js +97 -0
  56. package/dist/browser/mediaEncryptionPlayback/encryptedMediaResponse.js.map +1 -0
  57. package/dist/browser/mediaEncryptionPlayback/encryptedMediaWorker.d.ts +5 -0
  58. package/dist/browser/mediaEncryptionPlayback/encryptedMediaWorker.js +109 -0
  59. package/dist/browser/mediaEncryptionPlayback/encryptedMediaWorker.js.map +1 -0
  60. package/dist/browser/mediaEncryptionPlayback/encryptedPlayback.d.ts +12 -0
  61. package/dist/browser/mediaEncryptionPlayback/encryptedPlayback.js +169 -0
  62. package/dist/browser/mediaEncryptionPlayback/encryptedPlayback.js.map +1 -0
  63. package/dist/browser/mediaEncryptionPlayback/index.d.ts +3 -0
  64. package/dist/browser/mediaEncryptionPlayback/index.js +20 -0
  65. package/dist/browser/mediaEncryptionPlayback/index.js.map +1 -0
  66. package/dist/index.d.ts +47 -6
  67. package/dist/index.js +24 -8
  68. package/dist/index.js.map +1 -1
  69. package/dist/lib/RestClient.d.ts +6 -0
  70. package/dist/lib/RestClient.js +12 -0
  71. package/dist/lib/RestClient.js.map +1 -1
  72. package/dist/lib/mediaEncryption/capability.d.ts +16 -0
  73. package/dist/lib/mediaEncryption/capability.js +12 -0
  74. package/dist/lib/mediaEncryption/capability.js.map +1 -0
  75. package/dist/lib/mediaEncryption/crypto.d.ts +36 -0
  76. package/dist/lib/mediaEncryption/crypto.js +169 -0
  77. package/dist/lib/mediaEncryption/crypto.js.map +1 -0
  78. package/dist/lib/mediaEncryption/format.d.ts +62 -0
  79. package/dist/lib/mediaEncryption/format.js +167 -0
  80. package/dist/lib/mediaEncryption/format.js.map +1 -0
  81. package/dist/lib/mediaEncryption/index.d.ts +12 -0
  82. package/dist/lib/mediaEncryption/index.js +46 -0
  83. package/dist/lib/mediaEncryption/index.js.map +1 -0
  84. package/dist/lib/mediaEncryption/inputKeys.d.ts +24 -0
  85. package/dist/lib/mediaEncryption/inputKeys.js +102 -0
  86. package/dist/lib/mediaEncryption/inputKeys.js.map +1 -0
  87. package/dist/lib/mediaEncryption/networks.d.ts +14 -0
  88. package/dist/lib/mediaEncryption/networks.js +22 -0
  89. package/dist/lib/mediaEncryption/networks.js.map +1 -0
  90. package/dist/lib/mediaEncryption/recipient.d.ts +17 -0
  91. package/dist/lib/mediaEncryption/recipient.js +106 -0
  92. package/dist/lib/mediaEncryption/recipient.js.map +1 -0
  93. package/dist/lib/mediaEncryption/redact.d.ts +5 -0
  94. package/dist/lib/mediaEncryption/redact.js +34 -0
  95. package/dist/lib/mediaEncryption/redact.js.map +1 -0
  96. package/dist/lib/mediaEncryption/remote.d.ts +18 -0
  97. package/dist/lib/mediaEncryption/remote.js +243 -0
  98. package/dist/lib/mediaEncryption/remote.js.map +1 -0
  99. package/dist/lib/validation.d.ts +12 -2
  100. package/dist/lib/validation.js +100 -10
  101. package/dist/lib/validation.js.map +1 -1
  102. package/dist/types/ErrorData.d.ts +15 -0
  103. package/dist/types/ErrorData.js +15 -1
  104. package/dist/types/ErrorData.js.map +1 -1
  105. package/dist/version.d.ts +1 -1
  106. package/dist/version.js +1 -1
  107. package/dist-esm/Account/TwoFactorRequiredError.js +11 -0
  108. package/dist-esm/Account/TwoFactorRequiredError.js.map +1 -0
  109. package/dist-esm/Account/index.js +97 -13
  110. package/dist-esm/Account/index.js.map +1 -1
  111. package/dist-esm/ApiClient/WebSocketClient/BrowserWebSocketClient/ChannelCoordinator.js +6 -5
  112. package/dist-esm/ApiClient/WebSocketClient/BrowserWebSocketClient/ChannelCoordinator.js.map +1 -1
  113. package/dist-esm/ApiClient/WebSocketClient/BrowserWebSocketClient/index.js +43 -5
  114. package/dist-esm/ApiClient/WebSocketClient/BrowserWebSocketClient/index.js.map +1 -1
  115. package/dist-esm/ApiClient/WebSocketClient/index.js +33 -1
  116. package/dist-esm/ApiClient/WebSocketClient/index.js.map +1 -1
  117. package/dist-esm/ApiClient/index.js +23 -1
  118. package/dist-esm/ApiClient/index.js.map +1 -1
  119. package/dist-esm/Chat/modelRouting.js +8 -2
  120. package/dist-esm/Chat/modelRouting.js.map +1 -1
  121. package/dist-esm/MediaEncryption/index.js +429 -0
  122. package/dist-esm/MediaEncryption/index.js.map +1 -0
  123. package/dist-esm/Projects/Job.js +51 -4
  124. package/dist-esm/Projects/Job.js.map +1 -1
  125. package/dist-esm/Projects/Project.js +43 -2
  126. package/dist-esm/Projects/Project.js.map +1 -1
  127. package/dist-esm/Projects/createJobRequestMessage.js +16 -0
  128. package/dist-esm/Projects/createJobRequestMessage.js.map +1 -1
  129. package/dist-esm/Projects/index.js +351 -31
  130. package/dist-esm/Projects/index.js.map +1 -1
  131. package/dist-esm/Projects/recovery.js +4 -0
  132. package/dist-esm/Projects/recovery.js.map +1 -1
  133. package/dist-esm/Projects/types/PayAsYouGo.js +55 -0
  134. package/dist-esm/Projects/types/PayAsYouGo.js.map +1 -0
  135. package/dist-esm/Projects/types/index.js.map +1 -1
  136. package/dist-esm/Projects/utils/imageLayers.js +120 -0
  137. package/dist-esm/Projects/utils/imageLayers.js.map +1 -0
  138. package/dist-esm/browser/mediaEncryptionPlayback/encryptedMediaResponse.js +93 -0
  139. package/dist-esm/browser/mediaEncryptionPlayback/encryptedMediaResponse.js.map +1 -0
  140. package/dist-esm/browser/mediaEncryptionPlayback/encryptedMediaWorker.js +106 -0
  141. package/dist-esm/browser/mediaEncryptionPlayback/encryptedMediaWorker.js.map +1 -0
  142. package/dist-esm/browser/mediaEncryptionPlayback/encryptedPlayback.js +166 -0
  143. package/dist-esm/browser/mediaEncryptionPlayback/encryptedPlayback.js.map +1 -0
  144. package/dist-esm/browser/mediaEncryptionPlayback/index.js +4 -0
  145. package/dist-esm/browser/mediaEncryptionPlayback/index.js.map +1 -0
  146. package/dist-esm/index.js +16 -5
  147. package/dist-esm/index.js.map +1 -1
  148. package/dist-esm/lib/RestClient.js +12 -0
  149. package/dist-esm/lib/RestClient.js.map +1 -1
  150. package/dist-esm/lib/mediaEncryption/capability.js +9 -0
  151. package/dist-esm/lib/mediaEncryption/capability.js.map +1 -0
  152. package/dist-esm/lib/mediaEncryption/crypto.js +157 -0
  153. package/dist-esm/lib/mediaEncryption/crypto.js.map +1 -0
  154. package/dist-esm/lib/mediaEncryption/format.js +157 -0
  155. package/dist-esm/lib/mediaEncryption/format.js.map +1 -0
  156. package/dist-esm/lib/mediaEncryption/index.js +17 -0
  157. package/dist-esm/lib/mediaEncryption/index.js.map +1 -0
  158. package/dist-esm/lib/mediaEncryption/inputKeys.js +94 -0
  159. package/dist-esm/lib/mediaEncryption/inputKeys.js.map +1 -0
  160. package/dist-esm/lib/mediaEncryption/networks.js +17 -0
  161. package/dist-esm/lib/mediaEncryption/networks.js.map +1 -0
  162. package/dist-esm/lib/mediaEncryption/recipient.js +100 -0
  163. package/dist-esm/lib/mediaEncryption/recipient.js.map +1 -0
  164. package/dist-esm/lib/mediaEncryption/redact.js +31 -0
  165. package/dist-esm/lib/mediaEncryption/redact.js.map +1 -0
  166. package/dist-esm/lib/mediaEncryption/remote.js +240 -0
  167. package/dist-esm/lib/mediaEncryption/remote.js.map +1 -0
  168. package/dist-esm/lib/validation.js +99 -9
  169. package/dist-esm/lib/validation.js.map +1 -1
  170. package/dist-esm/types/ErrorData.js +14 -0
  171. package/dist-esm/types/ErrorData.js.map +1 -1
  172. package/dist-esm/version.js +1 -1
  173. package/llms-full.txt +73 -0
  174. package/llms.txt +7 -2
  175. package/package.json +15 -3
  176. package/src/Account/TwoFactorRequiredError.ts +12 -0
  177. package/src/Account/index.ts +183 -3
  178. package/src/ApiClient/WebSocketClient/BrowserWebSocketClient/ChannelCoordinator.ts +9 -5
  179. package/src/ApiClient/WebSocketClient/BrowserWebSocketClient/index.ts +42 -4
  180. package/src/ApiClient/WebSocketClient/events.ts +27 -0
  181. package/src/ApiClient/WebSocketClient/index.ts +32 -1
  182. package/src/ApiClient/WebSocketClient/messages.ts +4 -0
  183. package/src/ApiClient/WebSocketClient/types.ts +4 -0
  184. package/src/ApiClient/index.ts +31 -1
  185. package/src/Chat/modelRouting.ts +9 -2
  186. package/src/MediaEncryption/index.ts +500 -0
  187. package/src/Projects/Job.ts +57 -4
  188. package/src/Projects/Project.ts +56 -2
  189. package/src/Projects/createJobRequestMessage.ts +16 -0
  190. package/src/Projects/index.ts +383 -31
  191. package/src/Projects/recovery.ts +4 -0
  192. package/src/Projects/types/JobProvenance.ts +2 -0
  193. package/src/Projects/types/PayAsYouGo.ts +162 -0
  194. package/src/Projects/types/RawProject.ts +2 -0
  195. package/src/Projects/types/events.ts +9 -1
  196. package/src/Projects/types/index.ts +143 -1
  197. package/src/Projects/utils/imageLayers.ts +131 -0
  198. package/src/browser/mediaEncryptionPlayback/encryptedMediaResponse.ts +88 -0
  199. package/src/browser/mediaEncryptionPlayback/encryptedMediaWorker.ts +103 -0
  200. package/src/browser/mediaEncryptionPlayback/encryptedPlayback.ts +166 -0
  201. package/src/browser/mediaEncryptionPlayback/index.ts +3 -0
  202. package/src/index.ts +91 -4
  203. package/src/lib/RestClient.ts +13 -0
  204. package/src/lib/mediaEncryption/capability.ts +17 -0
  205. package/src/lib/mediaEncryption/crypto.ts +217 -0
  206. package/src/lib/mediaEncryption/format.ts +209 -0
  207. package/src/lib/mediaEncryption/index.ts +43 -0
  208. package/src/lib/mediaEncryption/inputKeys.ts +130 -0
  209. package/src/lib/mediaEncryption/networks.ts +31 -0
  210. package/src/lib/mediaEncryption/recipient.ts +118 -0
  211. package/src/lib/mediaEncryption/redact.ts +30 -0
  212. package/src/lib/mediaEncryption/remote.ts +259 -0
  213. package/src/lib/validation.ts +106 -9
  214. package/src/types/ErrorData.ts +18 -0
  215. package/src/version.ts +1 -1
@@ -0,0 +1,434 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ const ApiGroup_js_1 = __importDefault(require("../ApiGroup.js"));
16
+ const requestSession_js_1 = require("../lib/requestSession.js");
17
+ const remote_js_1 = require("../lib/mediaEncryption/remote.js");
18
+ const index_js_1 = require("../lib/mediaEncryption/index.js");
19
+ const ACCOUNT_MEDIA_KEY_PATH = '/v1/account/media-key';
20
+ const ACCOUNT_MEDIA_KEY_VERSION = 1;
21
+ /** After a failed key request, the next one waits this long, or for the next sign-in. */
22
+ const ACCOUNT_KEY_RETRY_MS = 5 * 60 * 1000;
23
+ /** How long a new project waits for a key request already in flight. */
24
+ const KEY_WAIT_MS = 5000;
25
+ /**
26
+ * Optional encryption of project media for the signed-in account.
27
+ *
28
+ * Create the client with `mediaEncryption: 'auto'` to encrypt every eligible
29
+ * project once media encryption is available for the account. Without it, the
30
+ * client works as before. See {@link SogniClientConfig.mediaEncryption}.
31
+ *
32
+ * @example Encrypt every eligible project, and read a result
33
+ * ```typescript
34
+ * const sogni = await SogniClient.createInstance({ appId, mediaEncryption: 'auto' });
35
+ * await sogni.account.login('username', 'password');
36
+ * const project = await sogni.projects.create(params);
37
+ * await project.waitForCompletion();
38
+ * const image = await project.jobs[0].getResultData(); // decrypted Blob
39
+ * ```
40
+ */
41
+ class MediaEncryptionApi extends ApiGroup_js_1.default {
42
+ constructor(config) {
43
+ super(config);
44
+ this.keyring = null;
45
+ /** Invalidates asynchronous key work when the key is replaced or dropped. */
46
+ this.keyGeneration = 0;
47
+ this.mediaReaders = new Set();
48
+ /** The last `mediaEncryptionAvailable` the server sent on this connection. */
49
+ this.serverAvailable = false;
50
+ /** The account-key request in flight, for the session that started it. */
51
+ this.keyRequest = null;
52
+ /** No new account-key request before this time, unless the account signs in again. */
53
+ this.keyRetryAt = 0;
54
+ /** Media encryption was turned off for the account: no key is requested again this session. */
55
+ this.keyTurnedOff = false;
56
+ this.client.auth.on('updated', (isAuthenticated) => {
57
+ if (isAuthenticated)
58
+ return;
59
+ this.setServerAvailable(false);
60
+ this.forgetKeys();
61
+ });
62
+ this.client.auth.on('sessionChanged', () => {
63
+ // A different account: its own connection says whether it may encrypt.
64
+ this.setServerAvailable(false);
65
+ this.forgetKeys();
66
+ });
67
+ this.client.socket.on('authenticated', (data) => {
68
+ this.setServerAvailable(data.mediaEncryptionAvailable === true);
69
+ });
70
+ this.client.socket.on('mediaEncryptionAvailabilityUpdated', (available) => {
71
+ this.setServerAvailable(available === true);
72
+ });
73
+ if (this.declaresCapability) {
74
+ this.client._setMediaCapability({ declared: () => this.holdsKey() });
75
+ }
76
+ }
77
+ /**
78
+ * `true` when this client was created with `mediaEncryption: 'auto'`.
79
+ * @internal
80
+ */
81
+ get automatic() {
82
+ return this.client.mediaEncryption === 'auto';
83
+ }
84
+ /** An 'auto' client that tells the servers it reads encrypted media. */
85
+ get declaresCapability() {
86
+ return this.automatic && this.client.declareMediaCapability;
87
+ }
88
+ /**
89
+ * Whether media encryption is available for this account right now. A
90
+ * client created with `mediaEncryption: 'auto'` encrypts new projects only
91
+ * while this is `true`.
92
+ */
93
+ get available() {
94
+ return this.serverAvailable;
95
+ }
96
+ setServerAvailable(available) {
97
+ if (this.serverAvailable === available)
98
+ return;
99
+ this.serverAvailable = available;
100
+ this.emit('availabilityChanged', available);
101
+ // Available: get the key now. No longer available: keep it for reading.
102
+ if (available && this.automatic)
103
+ void this.ensureAccountKey();
104
+ }
105
+ /** True when this data starts with the encrypted-media signature. Needs the first 8 bytes. */
106
+ isEncrypted(bytes) {
107
+ return (0, index_js_1.isEncryptedMedia)(bytes instanceof Uint8Array ? bytes : new Uint8Array(bytes));
108
+ }
109
+ /** Whether this client can open the signed-in account's encrypted media. */
110
+ isUnlocked() {
111
+ return __awaiter(this, void 0, void 0, function* () {
112
+ if (this.automatic)
113
+ yield this.ensureAccountKey();
114
+ return this.holdsKey();
115
+ });
116
+ }
117
+ /**
118
+ * Use these 32 bytes as the signed-in account's media key. A client created
119
+ * with `mediaEncryption: 'auto'` manages the key itself. The key is dropped
120
+ * when the account signs out or changes.
121
+ */
122
+ setAccountKey(secret) {
123
+ return __awaiter(this, void 0, void 0, function* () {
124
+ if (!this.client.auth.isAuthenticated) {
125
+ throw new index_js_1.MediaEncryptionError('locked', 'Sign in before setting the media key');
126
+ }
127
+ const isCurrent = this.beginKeyChange();
128
+ const key = yield (0, index_js_1.importAccountKey)(secret);
129
+ this.assertCurrentKeyChange(isCurrent);
130
+ this.setKeyring([key]);
131
+ });
132
+ }
133
+ /**
134
+ * Drop the account key from this client. With `mediaEncryption: 'auto'` it
135
+ * is obtained again the next time it is needed.
136
+ */
137
+ lock() {
138
+ return __awaiter(this, void 0, void 0, function* () {
139
+ this.forgetKeys();
140
+ });
141
+ }
142
+ /**
143
+ * Decrypt media downloaded from a result, preview or input URL. Returns a
144
+ * Blob typed with the media's real MIME type. Pass `key` only for media that
145
+ * the account key cannot open.
146
+ */
147
+ decrypt(media_1) {
148
+ return __awaiter(this, arguments, void 0, function* (media, options = {}) {
149
+ const assertCurrent = this.captureMediaSession();
150
+ const { data } = yield (0, index_js_1.decryptMedia)(media, (header) => this.resolveProjectKey(header, options));
151
+ assertCurrent();
152
+ return data;
153
+ });
154
+ }
155
+ /**
156
+ * Open encrypted video/audio for seeking without downloading the whole file.
157
+ * The URL must support HTTP byte ranges and expose Content-Range to this
158
+ * origin. Close the reader when playback no longer needs it; locking or
159
+ * changing the account also closes it and aborts pending requests.
160
+ *
161
+ * Ranges use [start, end) offsets in the DECRYPTED file. Apps can serve these
162
+ * through their service worker to a normal audio/video element. Prefer
163
+ * `stream()` for long ranges; `read()` collects the requested range in memory.
164
+ */
165
+ openDecrypted(url_1) {
166
+ return __awaiter(this, arguments, void 0, function* (url, options = {}) {
167
+ var _a, _b, _c, _d;
168
+ const assertCurrent = this.captureMediaSession();
169
+ const lifetime = new AbortController();
170
+ this.mediaReaders.add(lifetime);
171
+ const abort = () => lifetime.abort();
172
+ (_b = (_a = options.init) === null || _a === void 0 ? void 0 : _a.signal) === null || _b === void 0 ? void 0 : _b.addEventListener('abort', abort, { once: true });
173
+ if ((_d = (_c = options.init) === null || _c === void 0 ? void 0 : _c.signal) === null || _d === void 0 ? void 0 : _d.aborted)
174
+ abort();
175
+ const onClose = () => {
176
+ var _a, _b;
177
+ this.mediaReaders.delete(lifetime);
178
+ (_b = (_a = options.init) === null || _a === void 0 ? void 0 : _a.signal) === null || _b === void 0 ? void 0 : _b.removeEventListener('abort', abort);
179
+ };
180
+ return (0, remote_js_1.openRemoteEncryptedMedia)(url, (header) => this.resolveProjectKey(header, {}), {
181
+ init: Object.assign(Object.assign({}, options.init), { signal: lifetime.signal }),
182
+ assertCurrent,
183
+ onClose
184
+ });
185
+ });
186
+ }
187
+ /**
188
+ * Download a URL and return its content decrypted. Content that is not
189
+ * encrypted is returned as downloaded, so callers can use one path for both.
190
+ * Pass `requireEncrypted` to treat unencrypted content as an error instead.
191
+ */
192
+ fetchDecrypted(url_1) {
193
+ return __awaiter(this, arguments, void 0, function* (url, options = {}) {
194
+ const assertCurrent = this.captureMediaSession();
195
+ const response = yield fetch(url, options.init);
196
+ if (!response.ok) {
197
+ throw new Error(`Failed to download media: ${response.status} ${response.statusText}`);
198
+ }
199
+ const blob = yield response.blob();
200
+ assertCurrent();
201
+ return this._openIfEncrypted(blob, options.requireEncrypted);
202
+ });
203
+ }
204
+ /**
205
+ * Decrypt downloaded media when it is encrypted, and return anything else as it is.
206
+ * @internal
207
+ */
208
+ _openIfEncrypted(blob_1) {
209
+ return __awaiter(this, arguments, void 0, function* (blob, requireEncrypted = false) {
210
+ const signature = new Uint8Array(yield blob.slice(0, 8).arrayBuffer());
211
+ if ((0, index_js_1.isEncryptedMedia)(signature))
212
+ return this.decrypt(blob);
213
+ if (requireEncrypted) {
214
+ throw new index_js_1.MediaEncryptionError('not_encrypted', 'Media is not Sogni-encrypted');
215
+ }
216
+ return blob;
217
+ });
218
+ }
219
+ /**
220
+ * For `mediaEncryption: 'auto'`: whether a project submitted now can be encrypted.
221
+ * @internal
222
+ */
223
+ _canEncryptAutomatically() {
224
+ return __awaiter(this, void 0, void 0, function* () {
225
+ if (!this.automatic || !this.serverAvailable)
226
+ return false;
227
+ let timer;
228
+ const waited = new Promise((resolve) => {
229
+ timer = setTimeout(() => resolve(false), KEY_WAIT_MS);
230
+ });
231
+ try {
232
+ yield Promise.race([this.ensureAccountKey(), waited]);
233
+ }
234
+ finally {
235
+ clearTimeout(timer);
236
+ }
237
+ return this.holdsKey() && this.serverAvailable;
238
+ });
239
+ }
240
+ /**
241
+ * Start encrypting a new project.
242
+ * @internal
243
+ */
244
+ _createProjectEncryption() {
245
+ return __awaiter(this, void 0, void 0, function* () {
246
+ const accountKey = (yield this.requireAccountKeys())[0];
247
+ const projectKey = (0, index_js_1.randomBytes)(index_js_1.MEDIA_KEY_BYTES);
248
+ const recipient = yield (0, index_js_1.wrapProjectKey)(accountKey, projectKey);
249
+ // One entry per distinct input.
250
+ const inputKeys = new Map();
251
+ const sealedInputs = new WeakMap();
252
+ const encryptInput = (media_1, ...args_1) => __awaiter(this, [media_1, ...args_1], void 0, function* (media, contentType = '') {
253
+ const material = yield (0, index_js_1.inputMaterialForMedia)(accountKey, media, contentType);
254
+ const id = (0, index_js_1.bytesToBase64)(material.key);
255
+ if (!inputKeys.has(id)) {
256
+ if (inputKeys.size >= index_js_1.MAX_INPUT_KEYS) {
257
+ throw new index_js_1.MediaEncryptionError('unsupported', `An encrypted project can carry at most ${index_js_1.MAX_INPUT_KEYS} different input files`);
258
+ }
259
+ inputKeys.set(id, (0, index_js_1.bytesToBase64)(yield (0, index_js_1.wrapInputKey)(projectKey, material.key)));
260
+ }
261
+ return (0, index_js_1.encryptMedia)(media, {
262
+ key: material.key,
263
+ contentType,
264
+ recipient: yield (0, index_js_1.wrapProjectKey)(accountKey, material.key, material.recipientNonce),
265
+ deterministic: { salt: material.salt, noncePrefix: material.noncePrefix }
266
+ });
267
+ });
268
+ return {
269
+ wire: () => (Object.assign({ version: index_js_1.MEDIA_ENCRYPTION_VERSION, key: (0, index_js_1.bytesToBase64)(projectKey), recipient: (0, index_js_1.bytesToBase64)(recipient) }, (inputKeys.size ? { inputKeys: [...inputKeys.values()] } : {}))),
270
+ encrypt: (media, contentType) => {
271
+ let sealed = sealedInputs.get(media);
272
+ if (!sealed) {
273
+ sealed = encryptInput(media, contentType);
274
+ sealedInputs.set(media, sealed);
275
+ }
276
+ return sealed;
277
+ }
278
+ };
279
+ });
280
+ }
281
+ holdsKey() {
282
+ return (!!this.keyring &&
283
+ this.keyring.session === this.client.auth.sessionVersion &&
284
+ this.client.auth.isAuthenticated);
285
+ }
286
+ /**
287
+ * For `mediaEncryption: 'auto'`: get the signed-in account's key once media
288
+ * encryption is available for it. Resolves `false`, and never throws, when
289
+ * there is no key to use.
290
+ */
291
+ ensureAccountKey() {
292
+ var _a;
293
+ if (this.holdsKey())
294
+ return Promise.resolve(true);
295
+ if (!this.automatic || !this.client.auth.isAuthenticated || !this.serverAvailable) {
296
+ return Promise.resolve(false);
297
+ }
298
+ const session = this.client.auth.sessionVersion;
299
+ if (((_a = this.keyRequest) === null || _a === void 0 ? void 0 : _a.session) === session)
300
+ return this.keyRequest.promise;
301
+ if (this.keyTurnedOff || Date.now() < this.keyRetryAt)
302
+ return Promise.resolve(false);
303
+ const promise = this.fetchAccountKey().finally(() => {
304
+ var _a;
305
+ if (((_a = this.keyRequest) === null || _a === void 0 ? void 0 : _a.promise) === promise)
306
+ this.keyRequest = null;
307
+ });
308
+ this.keyRequest = { session, promise };
309
+ return promise;
310
+ }
311
+ fetchAccountKey() {
312
+ return __awaiter(this, void 0, void 0, function* () {
313
+ const assertSession = (0, requestSession_js_1.captureRequestSession)(this.client.auth);
314
+ const generation = this.keyGeneration;
315
+ try {
316
+ const response = yield this.client.rest.get(ACCOUNT_MEDIA_KEY_PATH);
317
+ assertSession();
318
+ const keys = yield importAccountKeys(response === null || response === void 0 ? void 0 : response.data);
319
+ assertSession();
320
+ // Replaced or dropped while the request was out: keep what is there now.
321
+ if (generation !== this.keyGeneration)
322
+ return this.holdsKey();
323
+ this.keyRetryAt = 0;
324
+ this.setKeyring(keys);
325
+ return true;
326
+ }
327
+ catch (error) {
328
+ // A request that outlived its sign-in belongs to nobody; the next sign-in asks again.
329
+ if (error instanceof requestSession_js_1.RequestSessionError)
330
+ return false;
331
+ const { status, payload } = error;
332
+ if (status === 409) {
333
+ // Turned off for the account; only the account holder turns it back on.
334
+ this.keyTurnedOff = true;
335
+ this.client.logger.warn('Media encryption is turned off for this account; projects are sent unencrypted');
336
+ return false;
337
+ }
338
+ this.keyRetryAt = Date.now() + ACCOUNT_KEY_RETRY_MS;
339
+ this.client.logger.warn('Media key unavailable; projects are sent unencrypted until it can be fetched', status !== undefined
340
+ ? { status, errorCode: payload === null || payload === void 0 ? void 0 : payload.errorCode }
341
+ : (error === null || error === void 0 ? void 0 : error.message) || error);
342
+ return false;
343
+ }
344
+ });
345
+ }
346
+ resolveProjectKey(header, options) {
347
+ return __awaiter(this, void 0, void 0, function* () {
348
+ if (options.key)
349
+ return options.key;
350
+ return (0, index_js_1.unwrapProjectKey)(yield this.requireAccountKeys(), header.recipient);
351
+ });
352
+ }
353
+ requireAccountKeys() {
354
+ return __awaiter(this, void 0, void 0, function* () {
355
+ if (this.automatic)
356
+ yield this.ensureAccountKey();
357
+ if (!this.holdsKey()) {
358
+ throw new index_js_1.MediaEncryptionError('locked', this.automatic
359
+ ? 'The media key for this account is not available right now'
360
+ : "This client has no media key for the signed-in account. Create it with mediaEncryption: 'auto'");
361
+ }
362
+ return this.keyring.keys;
363
+ });
364
+ }
365
+ setKeyring(keys) {
366
+ this.keyring = { keys, session: this.client.auth.sessionVersion };
367
+ this.emit('unlocked', undefined);
368
+ // An open socket that connected without the capability declares it now.
369
+ if (this.automatic)
370
+ this.client._declareMediaCapability();
371
+ }
372
+ forgetKeys() {
373
+ this.closeMediaReaders();
374
+ this.keyGeneration++;
375
+ this.keyRequest = null;
376
+ // The next sign-in asks for its key at once.
377
+ this.keyRetryAt = 0;
378
+ this.keyTurnedOff = false;
379
+ const hadKey = this.keyring !== null;
380
+ this.keyring = null;
381
+ if (hadKey)
382
+ this.emit('locked', undefined);
383
+ }
384
+ beginKeyChange() {
385
+ this.closeMediaReaders();
386
+ const generation = ++this.keyGeneration;
387
+ const session = this.client.auth.sessionVersion;
388
+ return () => generation === this.keyGeneration && session === this.client.auth.sessionVersion;
389
+ }
390
+ closeMediaReaders() {
391
+ for (const reader of this.mediaReaders)
392
+ reader.abort();
393
+ this.mediaReaders.clear();
394
+ }
395
+ captureMediaSession() {
396
+ const generation = this.keyGeneration;
397
+ const session = this.client.auth.sessionVersion;
398
+ return () => this.assertCurrentKeyChange(() => generation === this.keyGeneration && session === this.client.auth.sessionVersion);
399
+ }
400
+ assertCurrentKeyChange(isCurrent) {
401
+ if (!isCurrent()) {
402
+ throw new index_js_1.MediaEncryptionError('locked', 'Media encryption was locked or the account changed');
403
+ }
404
+ }
405
+ }
406
+ /** The account's keys, current first. */
407
+ function importAccountKeys(data) {
408
+ return __awaiter(this, void 0, void 0, function* () {
409
+ var _a;
410
+ if ((data === null || data === void 0 ? void 0 : data.version) !== ACCOUNT_MEDIA_KEY_VERSION || typeof ((_a = data.current) === null || _a === void 0 ? void 0 : _a.key) !== 'string') {
411
+ throw new index_js_1.MediaEncryptionError('malformed', 'The media key response is not recognized');
412
+ }
413
+ const encoded = [
414
+ data.current.key,
415
+ ...(Array.isArray(data.previous) ? data.previous.map((entry) => entry === null || entry === void 0 ? void 0 : entry.key) : [])
416
+ ];
417
+ const keys = [];
418
+ for (const value of encoded) {
419
+ if (typeof value !== 'string') {
420
+ throw new index_js_1.MediaEncryptionError('malformed', 'The media key response is not recognized');
421
+ }
422
+ const secret = (0, index_js_1.base64ToBytes)(value);
423
+ try {
424
+ keys.push(yield (0, index_js_1.importAccountKey)(secret));
425
+ }
426
+ finally {
427
+ secret.fill(0);
428
+ }
429
+ }
430
+ return keys;
431
+ });
432
+ }
433
+ exports.default = MediaEncryptionApi;
434
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/MediaEncryption/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;AAAA,iEAAqD;AAErD,gEAAsF;AACtF,gEAG0C;AAC1C,8DAmByC;AAwCzC,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AAK,uBAAL,KAAK,CAAE,GAAG,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;SAClF,CAAC;QACF,MAAM,IAAI,GAAsB,EAAE,CAAC;QACnC,KAAK,MAAM,KAAK,IAAI,OAAO,EAAE,CAAC;YAC5B,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE,CAAC;gBAC9B,MAAM,IAAI,+BAAoB,CAAC,WAAW,EAAE,0CAA0C,CAAC,CAAC;YAC1F,CAAC;YACD,MAAM,MAAM,GAAG,IAAA,wBAAa,EAAC,KAAK,CAAC,CAAC;YACpC,IAAI,CAAC;gBACH,IAAI,CAAC,IAAI,CAAC,MAAM,IAAA,2BAAgB,EAAC,MAAM,CAAC,CAAC,CAAC;YAC5C,CAAC;oBAAS,CAAC;gBACT,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;YACjB,CAAC;QACH,CAAC;QACD,OAAO,IAAI,CAAC;IACd,CAAC;CAAA;AAED,kBAAe,kBAAkB,CAAC"}
@@ -3,7 +3,7 @@ import ErrorData from '../types/ErrorData.js';
3
3
  import { RawJob, RawProject } from './types/RawProject.js';
4
4
  import ProjectsApi from './index.js';
5
5
  import { Logger } from '../lib/DefaultLogger.js';
6
- import { EnhancementStrength } from './types/index.js';
6
+ import { EnhancementStrength, ImageLayer } from './types/index.js';
7
7
  import Project from './Project.js';
8
8
  import { SupernetType } from '../ApiClient/WebSocketClient/types.js';
9
9
  import { TokenType } from '../types/token.js';
@@ -71,6 +71,11 @@ export interface JobData {
71
71
  /** Seedance 2.5 final: the Sogni job id of the draft it finalized. */
72
72
  seedanceDraftJobId?: string;
73
73
  provenance?: JobProvenance;
74
+ /**
75
+ * Seedream layer decomposition: the layers above the base image, ordered by
76
+ * `zIndex`. `resultUrl` is the base image.
77
+ */
78
+ layers?: ImageLayer[];
74
79
  error?: ErrorData;
75
80
  positivePrompt?: string;
76
81
  negativePrompt?: string;
@@ -167,6 +172,12 @@ declare class Job extends DataEntity<JobData, JobEventMap> {
167
172
  * it was not disabled explicitly.
168
173
  */
169
174
  get resultUrl(): string | null | undefined;
175
+ /**
176
+ * Seedream layer decomposition: the RGBA PNG layers above the base image,
177
+ * ordered by `zIndex`, each with its box on the base image. `resultUrl` is
178
+ * the base image. Undefined for every other job.
179
+ */
180
+ get layers(): ImageLayer[] | undefined;
170
181
  /** Worker-attested input/output hashes for this result, when available. */
171
182
  get provenance(): JobProvenance | undefined;
172
183
  get imageUrl(): string | undefined;
@@ -208,6 +219,11 @@ declare class Job extends DataEntity<JobData, JobEventMap> {
208
219
  private get _imageContentType();
209
220
  /** Ask the API for this job's result URL on the endpoint its media type needs. */
210
221
  private _mintResultUrl;
222
+ /**
223
+ * Mint a fresh, ownership-checked PNG URL for an extracted layer, by its
224
+ * `zIndex` (1-99) in `job.layers`.
225
+ */
226
+ getLayerUrl(zIndex: number): Promise<string>;
211
227
  /**
212
228
  * Get the result URL of the job. This method will make a request to the API to get signed URL.
213
229
  * IMPORTANT: URL expires after 30 minutes, so make sure to download the result as soon as possible.
@@ -298,6 +314,16 @@ declare class Job extends DataEntity<JobData, JobEventMap> {
298
314
  private _startRuntimeTimeout;
299
315
  private handleUpdated;
300
316
  private handleEnhancementUpdate;
317
+ /**
318
+ * Whether this job's project was submitted with encrypted media. Read its
319
+ * results with {@link Job.getResultData} or
320
+ * {@link MediaEncryptionApi.fetchDecrypted}, which return the media either way.
321
+ */
322
+ get encrypted(): boolean;
323
+ /**
324
+ * Download the result. Encrypted results are returned decrypted, typed with
325
+ * their real MIME type.
326
+ */
301
327
  getResultData(): Promise<Blob>;
302
328
  /**
303
329
  * The parent render's canvas as explicit dimensions. The enhancer is a different model
@@ -17,6 +17,7 @@ exports.seedanceDraftResultFields = seedanceDraftResultFields;
17
17
  const DataEntity_js_1 = __importDefault(require("../lib/DataEntity.js"));
18
18
  const getUUID_js_1 = __importDefault(require("../lib/getUUID.js"));
19
19
  const index_js_1 = require("./utils/index.js");
20
+ const imageLayers_js_1 = require("./utils/imageLayers.js");
20
21
  const has_js_1 = __importDefault(require("lodash/has.js"));
21
22
  const MINUTE_MS = 60 * 1000;
22
23
  const HOUR_MS = 60 * MINUTE_MS;
@@ -243,6 +244,14 @@ class Job extends DataEntity_js_1.default {
243
244
  get resultUrl() {
244
245
  return this.data.resultUrl;
245
246
  }
247
+ /**
248
+ * Seedream layer decomposition: the RGBA PNG layers above the base image,
249
+ * ordered by `zIndex`, each with its box on the base image. `resultUrl` is
250
+ * the base image. Undefined for every other job.
251
+ */
252
+ get layers() {
253
+ return this.data.layers;
254
+ }
246
255
  /** Worker-attested input/output hashes for this result, when available. */
247
256
  get provenance() {
248
257
  return this.data.provenance;
@@ -336,6 +345,23 @@ class Job extends DataEntity_js_1.default {
336
345
  imageContentType: this._imageContentType
337
346
  });
338
347
  }
348
+ /**
349
+ * Mint a fresh, ownership-checked PNG URL for an extracted layer, by its
350
+ * `zIndex` (1-99) in `job.layers`.
351
+ */
352
+ getLayerUrl(zIndex) {
353
+ return __awaiter(this, void 0, void 0, function* () {
354
+ var _a;
355
+ if (this.data.status !== 'completed' || !((_a = this.layers) === null || _a === void 0 ? void 0 : _a.some((layer) => layer.zIndex === zIndex)))
356
+ throw new Error('Layer is not available');
357
+ return this._api.downloadUrl({
358
+ jobId: this.projectId,
359
+ imageId: this.id,
360
+ type: 'complete',
361
+ layerIndex: zIndex
362
+ });
363
+ });
364
+ }
339
365
  /**
340
366
  * Get the result URL of the job. This method will make a request to the API to get signed URL.
341
367
  * IMPORTANT: URL expires after 30 minutes, so make sure to download the result as soon as possible.
@@ -429,9 +455,10 @@ class Job extends DataEntity_js_1.default {
429
455
  */
430
456
  _syncWithRestData(data) {
431
457
  return __awaiter(this, void 0, void 0, function* () {
432
- var _a, _b, _c, _d, _e, _f, _g;
458
+ var _a, _b, _c, _d, _e, _f, _g, _h;
433
459
  const directResultUrl = directResultUrlFromRawJob(data);
434
- const delta = Object.assign(Object.assign(Object.assign(Object.assign({ step: data.performedSteps, workerName: (_a = data.worker) === null || _a === void 0 ? void 0 : _a.name, seed: data.seedUsed, isNSFW: data.triggeredNSFWFilter, nsfwDetected: data.nsfwDetected === true }, (data.nsfwSources ? { nsfwSources: [...data.nsfwSources] } : {})), (data.result ? { provenance: data.result } : {})), { lastFrameUrl: (_b = data.lastFrameUrl) !== null && _b !== void 0 ? _b : (_c = data.result) === null || _c === void 0 ? void 0 : _c.lastFrameUrl, lastFrameKey: (_d = data.lastFrameKey) !== null && _d !== void 0 ? _d : (_e = data.result) === null || _e === void 0 ? void 0 : _e.lastFrameKey, outputFormat: (_f = data.outputFormat) !== null && _f !== void 0 ? _f : (_g = data.result) === null || _g === void 0 ? void 0 : _g.outputFormat }), seedanceDraftResultFields(data));
460
+ const layers = (0, imageLayers_js_1.imageLayersFromResult)((_a = data.result) === null || _a === void 0 ? void 0 : _a.layers);
461
+ const delta = Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({ step: data.performedSteps, workerName: (_b = data.worker) === null || _b === void 0 ? void 0 : _b.name, seed: data.seedUsed, isNSFW: data.triggeredNSFWFilter, nsfwDetected: data.nsfwDetected === true }, (data.nsfwSources ? { nsfwSources: [...data.nsfwSources] } : {})), (data.result ? { provenance: data.result } : {})), { lastFrameUrl: (_c = data.lastFrameUrl) !== null && _c !== void 0 ? _c : (_d = data.result) === null || _d === void 0 ? void 0 : _d.lastFrameUrl, lastFrameKey: (_e = data.lastFrameKey) !== null && _e !== void 0 ? _e : (_f = data.result) === null || _f === void 0 ? void 0 : _f.lastFrameKey, outputFormat: (_g = data.outputFormat) !== null && _g !== void 0 ? _g : (_h = data.result) === null || _h === void 0 ? void 0 : _h.outputFormat }), seedanceDraftResultFields(data)), (layers ? { layers } : {}));
435
462
  if (JOB_STATUS_MAP[data.status]) {
436
463
  delta.status = JOB_STATUS_MAP[data.status];
437
464
  }
@@ -556,6 +583,18 @@ class Job extends DataEntity_js_1.default {
556
583
  handleEnhancementUpdate() {
557
584
  this.emit('updated', ['enhancedImage']);
558
585
  }
586
+ /**
587
+ * Whether this job's project was submitted with encrypted media. Read its
588
+ * results with {@link Job.getResultData} or
589
+ * {@link MediaEncryptionApi.fetchDecrypted}, which return the media either way.
590
+ */
591
+ get encrypted() {
592
+ return this._project.encrypted;
593
+ }
594
+ /**
595
+ * Download the result. Encrypted results are returned decrypted, typed with
596
+ * their real MIME type.
597
+ */
559
598
  getResultData() {
560
599
  return __awaiter(this, void 0, void 0, function* () {
561
600
  if (!this.hasResultMedia) {
@@ -566,7 +605,15 @@ class Job extends DataEntity_js_1.default {
566
605
  if (!response.ok) {
567
606
  throw new Error(`Failed to fetch image: ${response.statusText}`);
568
607
  }
569
- return response.blob();
608
+ const blob = yield response.blob();
609
+ if (!this.encrypted)
610
+ return blob;
611
+ const mediaEncryption = this._api._mediaEncryption;
612
+ if (!mediaEncryption) {
613
+ throw new Error('Media encryption is not available on this client instance');
614
+ }
615
+ // Judge by the bytes: only encrypted content is decrypted.
616
+ return mediaEncryption._openIfEncrypted(blob);
570
617
  });
571
618
  }
572
619
  /**
@@ -629,7 +676,7 @@ class Job extends DataEntity_js_1.default {
629
676
  }
630
677
  const size = yield this.enhancementSize();
631
678
  const imageData = yield this.getResultData();
632
- const project = yield this._api.create(Object.assign(Object.assign(Object.assign({ type: 'image' }, exports.enhancementDefaults), { positivePrompt: overrides.positivePrompt || this._project.params.positivePrompt, stylePrompt: overrides.stylePrompt || this._project.params.stylePrompt, tokenType: overrides.tokenType || this._project.params.tokenType, seed: (_a = this.seed) !== null && _a !== void 0 ? _a : this._project.params.seed, startingImage: imageData, startingImageStrength: 1 - (0, index_js_1.getEnhacementStrength)(strength) }), (size ? { sizePreset: 'custom', width: size.width, height: size.height } : {})));
679
+ const project = yield this._api.create(Object.assign(Object.assign(Object.assign(Object.assign({ type: 'image' }, exports.enhancementDefaults), { positivePrompt: overrides.positivePrompt || this._project.params.positivePrompt, stylePrompt: overrides.stylePrompt || this._project.params.stylePrompt, tokenType: overrides.tokenType || this._project.params.tokenType, seed: (_a = this.seed) !== null && _a !== void 0 ? _a : this._project.params.seed, startingImage: imageData, startingImageStrength: 1 - (0, index_js_1.getEnhacementStrength)(strength) }), (size ? { sizePreset: 'custom', width: size.width, height: size.height } : {})), (parentProjectParams.mediaEncryption ? { mediaEncryption: true } : {})));
633
680
  this._enhancementProject = project;
634
681
  this._enhancementProject.on('updated', this.handleEnhancementUpdate);
635
682
  const images = yield project.waitForCompletion();