@sogni-ai/sogni-client 1.0.1 → 1.1.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.
- package/CHANGELOG.md +15 -0
- package/README.md +126 -0
- package/dist/Account/CurrentAccount.d.ts +4 -5
- package/dist/Account/CurrentAccount.js +9 -29
- package/dist/Account/CurrentAccount.js.map +1 -1
- package/dist/Account/index.d.ts +5 -3
- package/dist/Account/index.js +20 -9
- package/dist/Account/index.js.map +1 -1
- package/dist/Account/types.d.ts +2 -0
- package/dist/ApiClient/WebSocketClient/index.d.ts +4 -4
- package/dist/ApiClient/WebSocketClient/index.js +33 -49
- package/dist/ApiClient/WebSocketClient/index.js.map +1 -1
- package/dist/ApiClient/WebSocketClient/messages.d.ts +7 -0
- package/dist/ApiClient/index.d.ts +4 -10
- package/dist/ApiClient/index.js +26 -17
- package/dist/ApiClient/index.js.map +1 -1
- package/dist/Projects/Project.d.ts +4 -0
- package/dist/Projects/Project.js +8 -0
- package/dist/Projects/Project.js.map +1 -1
- package/dist/Projects/createJobRequestMessage.js +12 -1
- package/dist/Projects/createJobRequestMessage.js.map +1 -1
- package/dist/Projects/index.d.ts +40 -2
- package/dist/Projects/index.js +146 -14
- package/dist/Projects/index.js.map +1 -1
- package/dist/Projects/types/index.d.ts +40 -0
- package/dist/lib/AuthManager.d.ts +51 -0
- package/dist/lib/AuthManager.js +157 -0
- package/dist/lib/AuthManager.js.map +1 -0
- package/dist/lib/Cache.d.ts +9 -0
- package/dist/lib/Cache.js +30 -0
- package/dist/lib/Cache.js.map +1 -0
- package/dist/lib/RestClient.d.ts +4 -7
- package/dist/lib/RestClient.js +7 -7
- package/dist/lib/RestClient.js.map +1 -1
- package/dist/lib/utils.d.ts +8 -0
- package/dist/lib/utils.js +20 -0
- package/dist/lib/utils.js.map +1 -0
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/dist/version.js.map +1 -1
- package/package.json +1 -2
- package/src/Account/CurrentAccount.ts +12 -33
- package/src/Account/index.ts +18 -8
- package/src/Account/types.ts +2 -0
- package/src/ApiClient/WebSocketClient/index.ts +16 -25
- package/src/ApiClient/WebSocketClient/messages.ts +8 -0
- package/src/ApiClient/index.ts +19 -27
- package/src/Projects/Project.ts +7 -0
- package/src/Projects/createJobRequestMessage.ts +14 -1
- package/src/Projects/index.ts +150 -8
- package/src/Projects/types/index.ts +42 -0
- package/src/lib/AuthManager.ts +172 -0
- package/src/lib/Cache.ts +36 -0
- package/src/lib/RestClient.ts +8 -13
- package/src/lib/utils.ts +17 -0
- package/src/version.ts +1 -1
- package/dist/Projects/models.json +0 -8906
- package/src/Projects/models.json +0 -8906
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,18 @@
|
|
|
1
|
+
# [1.1.0](https://github.com/Sogni-AI/sogni-client/compare/v1.0.2...v1.1.0) (2025-01-29)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Features
|
|
5
|
+
|
|
6
|
+
* Custom output image size ([fe8957c](https://github.com/Sogni-AI/sogni-client/commit/fe8957cdc8b280b34dcbe5d395ffeed6de91fa56))
|
|
7
|
+
* Project cancellation ([00f704e](https://github.com/Sogni-AI/sogni-client/commit/00f704e2ef43164d024b50f30c8ea0c33c1a96cf))
|
|
8
|
+
|
|
9
|
+
## [1.0.2](https://github.com/Sogni-AI/sogni-client/compare/v1.0.1...v1.0.2) (2025-01-29)
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
### Bug Fixes
|
|
13
|
+
|
|
14
|
+
* Another fix for semantic-release gitnub plugin configuration ([b0e14b9](https://github.com/Sogni-AI/sogni-client/commit/b0e14b9a56c7c12d8d36235bd7ffaec204ed71af))
|
|
15
|
+
|
|
1
16
|
## [1.0.1](https://github.com/Sogni-AI/sogni-client/compare/v1.0.0...v1.0.1) (2025-01-29)
|
|
2
17
|
|
|
3
18
|
|
package/README.md
CHANGED
|
@@ -161,5 +161,131 @@ project.on('failed', async (errorData) => {
|
|
|
161
161
|
});
|
|
162
162
|
```
|
|
163
163
|
|
|
164
|
+
### Project parameters
|
|
165
|
+
Here is full list of project parameters that you can use:
|
|
166
|
+
- `modelId` - ID of the model to use for image generation.
|
|
167
|
+
- `positivePrompt` - text prompt that describes what you want to see in the image. Can be empty string.
|
|
168
|
+
- `negativePrompt` - text prompt that describes what you don't want to see in the image. Can be empty string.
|
|
169
|
+
- `stylePrompt` - text prompt that describes the style of the image. Can be empty string.
|
|
170
|
+
- `numberOfImages` - number of images to generate.
|
|
171
|
+
- `sizePreset` - optionally pass the ID of a size preset to use. If not passed, the default output is a square at
|
|
172
|
+
either 512x512, 768x768 or 1024x1024 (SDXL and Flux) based on the default resolution of the selected model.
|
|
173
|
+
See **Detecting available output presets** section below for available presets for your model. The token cost and
|
|
174
|
+
render time of the job is heavily influenced by total pixel count where a 2048x2048 image is 4x the cost and render
|
|
175
|
+
time of a 1024x1024 image as it is 4x the generated pixel count. You may also pass `custom` along with `width` and
|
|
176
|
+
`height` project parameters to request a custom dimension. Note that not all size presets and custom aspect ratios
|
|
177
|
+
produce consistently good results with all models. If your output features skewed anatomy or doubling of features
|
|
178
|
+
you should experiment with a different model or output size.
|
|
179
|
+
- `width` - if 'sizePreset' is set to 'custom' you may pass a custom pixel width between 256 and 2048
|
|
180
|
+
- `height` - if 'sizePreset' is set to 'custom' you may pass a custom pixel height between 256 and 2048
|
|
181
|
+
- `steps` - number of inference steps between random pixels to final image. Higher steps generally lead to higher
|
|
182
|
+
quality images and more details but varies by model, prompt, guidance, and desired look. For most Stable Diffusion
|
|
183
|
+
models 20-40 steps is ideal with 20 being 2x faster to render than 40. For Flux 4 steps is optimal. Lightning,
|
|
184
|
+
Turbo and LCM models are designed for quality output in as little as 1 step. ([More info](https://docs.sogni.ai/learn/basics/inference-steps)).
|
|
185
|
+
- `guidance` - guidance scale. For most Stable Diffusion models, optimal value is 7.5 ([More info](https://docs.sogni.ai/learn/basics/guidance-scale)).
|
|
186
|
+
- `network` - network type to use, 'fast' or 'relaxed'. This parameter allow to override default network type for this project.
|
|
187
|
+
- `disableNSFWFilter` - disable NSFW filter for this project. NSFW filter is enabled by default and workers won't upload resulting images if they are detected as NSFW.
|
|
188
|
+
- `seed` - uint32 number to use as seed. If not provided, random seed will be used. If `numberOfImages` is greater than 1, provided seed will be user only for one of them. ([More info](https://docs.sogni.ai/learn/basics/generation-seed)).
|
|
189
|
+
- `numberOfPreviews` - number of preview images to generate. If not provided, no preview images will be generated.
|
|
190
|
+
- `scheduler` - scheduler to use ([More info](https://docs.sogni.ai/learn/advanced/schedulers)). For available options see type definition.
|
|
191
|
+
- `timeStepSpacing` - time step spacing algorithm ([More info](https://docs.sogni.ai/learn/advanced/schedulers)). For available options see type definition.
|
|
192
|
+
- `startingImage` - guide image in PNG format. Can be [File](https://developer.mozilla.org/en-US/docs/Web/API/File), [Blob](https://developer.mozilla.org/en-US/docs/Web/API/Blob) or [Buffer](https://nodejs.org/api/buffer.html)
|
|
193
|
+
- `startingImageStrength` - strong effect of starting image should be. From 0 to 1, default 0.5.
|
|
194
|
+
|
|
195
|
+
TypeScript type definitions for project parameters can be found in [ProjectParams](https://sdk-docs.sogni.ai/interfaces/ProjectParams.html) docs.
|
|
196
|
+
|
|
197
|
+
### Detecting available output presets
|
|
198
|
+
You can get list of available output presets for a specific network and model using `client.projects.getOutputPresets` method.
|
|
199
|
+
```javascript
|
|
200
|
+
const presets = await client.projects.getSizePresets('fast', 'flux1-schnell-fp8');
|
|
201
|
+
console.log('Available output presets:', presets);
|
|
202
|
+
```
|
|
203
|
+
Sample response:
|
|
204
|
+
```json
|
|
205
|
+
[
|
|
206
|
+
{
|
|
207
|
+
"label": "Square",
|
|
208
|
+
"id": "square",
|
|
209
|
+
"width": 512,
|
|
210
|
+
"height": 512,
|
|
211
|
+
"ratio": "1:1",
|
|
212
|
+
"aspect": "1"
|
|
213
|
+
},
|
|
214
|
+
{
|
|
215
|
+
"label": "Square HD",
|
|
216
|
+
"id": "square_hd",
|
|
217
|
+
"width": 1024,
|
|
218
|
+
"height": 1024,
|
|
219
|
+
"ratio": "1:1",
|
|
220
|
+
"aspect": "1"
|
|
221
|
+
},
|
|
222
|
+
{
|
|
223
|
+
"label": "Portrait: Standard",
|
|
224
|
+
"id": "portrait_7_9",
|
|
225
|
+
"width": 896,
|
|
226
|
+
"height": 1152,
|
|
227
|
+
"ratio": "7:9",
|
|
228
|
+
"aspect": "0.78"
|
|
229
|
+
},
|
|
230
|
+
{
|
|
231
|
+
"label": "Portrait: 35mm",
|
|
232
|
+
"id": "portrait_13_19",
|
|
233
|
+
"width": 832,
|
|
234
|
+
"height": 1216,
|
|
235
|
+
"ratio": "13:19",
|
|
236
|
+
"aspect": "0.68"
|
|
237
|
+
},
|
|
238
|
+
{
|
|
239
|
+
"label": "Portrait: Mobile",
|
|
240
|
+
"id": "portrait_4_7",
|
|
241
|
+
"width": 768,
|
|
242
|
+
"height": 1344,
|
|
243
|
+
"ratio": "4:7",
|
|
244
|
+
"aspect": "0.57"
|
|
245
|
+
},
|
|
246
|
+
{
|
|
247
|
+
"label": "Portrait: Extended",
|
|
248
|
+
"id": "portrait_5_12",
|
|
249
|
+
"width": 640,
|
|
250
|
+
"height": 1536,
|
|
251
|
+
"ratio": "5:12",
|
|
252
|
+
"aspect": "0.42"
|
|
253
|
+
},
|
|
254
|
+
{
|
|
255
|
+
"label": "Landscape: Standard",
|
|
256
|
+
"id": "landscape_9_7",
|
|
257
|
+
"width": 1152,
|
|
258
|
+
"height": 896,
|
|
259
|
+
"ratio": "9:7",
|
|
260
|
+
"aspect": "1.28"
|
|
261
|
+
},
|
|
262
|
+
{
|
|
263
|
+
"label": "Landscape: 35mm",
|
|
264
|
+
"id": "landscape_19_13",
|
|
265
|
+
"width": 1216,
|
|
266
|
+
"height": 832,
|
|
267
|
+
"ratio": "19:13",
|
|
268
|
+
"aspect": "1.46"
|
|
269
|
+
},
|
|
270
|
+
{
|
|
271
|
+
"label": "Landscape: Widescreen",
|
|
272
|
+
"id": "landscape_7_4",
|
|
273
|
+
"width": 1344,
|
|
274
|
+
"height": 768,
|
|
275
|
+
"ratio": "7:4",
|
|
276
|
+
"aspect": "1.75"
|
|
277
|
+
},
|
|
278
|
+
{
|
|
279
|
+
"label": "Landscape: Ultrawide",
|
|
280
|
+
"id": "landscape_12_5",
|
|
281
|
+
"width": 1536,
|
|
282
|
+
"height": 640,
|
|
283
|
+
"ratio": "12:5",
|
|
284
|
+
"aspect": "2.4"
|
|
285
|
+
}
|
|
286
|
+
]
|
|
287
|
+
```
|
|
288
|
+
|
|
289
|
+
|
|
164
290
|
## Code examples
|
|
165
291
|
You can find more code examples in the [examples](https://github.com/Sogni-AI/sogni-client/tree/main/examples) directory.
|
|
@@ -5,7 +5,6 @@ import { SupernetType } from '../ApiClient/WebSocketClient/types';
|
|
|
5
5
|
* @inline
|
|
6
6
|
*/
|
|
7
7
|
export interface AccountData {
|
|
8
|
-
token: string | null;
|
|
9
8
|
/**
|
|
10
9
|
* Current network status:\
|
|
11
10
|
* - `connected` - connected to the socket
|
|
@@ -20,8 +19,9 @@ export interface AccountData {
|
|
|
20
19
|
network: SupernetType | null;
|
|
21
20
|
balance: BalanceData;
|
|
22
21
|
walletAddress?: string;
|
|
23
|
-
expiresAt?: Date;
|
|
24
22
|
username?: string;
|
|
23
|
+
token?: string;
|
|
24
|
+
refreshToken?: string;
|
|
25
25
|
}
|
|
26
26
|
/**
|
|
27
27
|
* Current account data.
|
|
@@ -29,15 +29,14 @@ export interface AccountData {
|
|
|
29
29
|
*/
|
|
30
30
|
declare class CurrentAccount extends DataEntity<AccountData> {
|
|
31
31
|
constructor(data?: AccountData);
|
|
32
|
-
_update<K extends keyof AccountData>(delta: Partial<AccountData>): void;
|
|
33
32
|
_clear(): void;
|
|
34
33
|
get isAuthenicated(): boolean;
|
|
35
34
|
get networkStatus(): "connected" | "disconnected" | "connecting" | "switching";
|
|
36
35
|
get network(): SupernetType | null;
|
|
37
36
|
get balance(): BalanceData;
|
|
38
37
|
get walletAddress(): string | undefined;
|
|
39
|
-
get expiresAt(): Date | undefined;
|
|
40
38
|
get username(): string | undefined;
|
|
41
|
-
get token(): string |
|
|
39
|
+
get token(): string | undefined;
|
|
40
|
+
get refreshToken(): string | undefined;
|
|
42
41
|
}
|
|
43
42
|
export default CurrentAccount;
|
|
@@ -4,10 +4,8 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
const DataEntity_1 = __importDefault(require("../lib/DataEntity"));
|
|
7
|
-
const jwt_decode_1 = require("jwt-decode");
|
|
8
7
|
function getDefaults() {
|
|
9
8
|
return {
|
|
10
|
-
token: null,
|
|
11
9
|
networkStatus: 'disconnected',
|
|
12
10
|
network: null,
|
|
13
11
|
balance: {
|
|
@@ -15,14 +13,11 @@ function getDefaults() {
|
|
|
15
13
|
debit: '0',
|
|
16
14
|
net: '0',
|
|
17
15
|
settled: '0'
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
return {
|
|
24
|
-
walletAddress: data.addr,
|
|
25
|
-
expiresAt: new Date(data.exp * 1000)
|
|
16
|
+
},
|
|
17
|
+
walletAddress: undefined,
|
|
18
|
+
username: undefined,
|
|
19
|
+
token: undefined,
|
|
20
|
+
refreshToken: undefined
|
|
26
21
|
};
|
|
27
22
|
}
|
|
28
23
|
/**
|
|
@@ -33,26 +28,11 @@ class CurrentAccount extends DataEntity_1.default {
|
|
|
33
28
|
constructor(data) {
|
|
34
29
|
super(data || getDefaults());
|
|
35
30
|
}
|
|
36
|
-
_update(delta) {
|
|
37
|
-
this.data = Object.assign(Object.assign({}, this.data), delta);
|
|
38
|
-
const keys = Object.keys(delta);
|
|
39
|
-
if (delta.hasOwnProperty('token')) {
|
|
40
|
-
if (delta.token) {
|
|
41
|
-
Object.assign(this.data, decodeToken(delta.token));
|
|
42
|
-
}
|
|
43
|
-
else {
|
|
44
|
-
delete this.data.walletAddress;
|
|
45
|
-
delete this.data.expiresAt;
|
|
46
|
-
}
|
|
47
|
-
keys.push('walletAddress', 'expiresAt');
|
|
48
|
-
}
|
|
49
|
-
this.emit('updated', keys);
|
|
50
|
-
}
|
|
51
31
|
_clear() {
|
|
52
32
|
this._update(getDefaults());
|
|
53
33
|
}
|
|
54
34
|
get isAuthenicated() {
|
|
55
|
-
return !!this.data.
|
|
35
|
+
return !!this.data.refreshToken;
|
|
56
36
|
}
|
|
57
37
|
get networkStatus() {
|
|
58
38
|
return this.data.networkStatus;
|
|
@@ -66,15 +46,15 @@ class CurrentAccount extends DataEntity_1.default {
|
|
|
66
46
|
get walletAddress() {
|
|
67
47
|
return this.data.walletAddress;
|
|
68
48
|
}
|
|
69
|
-
get expiresAt() {
|
|
70
|
-
return this.data.expiresAt;
|
|
71
|
-
}
|
|
72
49
|
get username() {
|
|
73
50
|
return this.data.username;
|
|
74
51
|
}
|
|
75
52
|
get token() {
|
|
76
53
|
return this.data.token;
|
|
77
54
|
}
|
|
55
|
+
get refreshToken() {
|
|
56
|
+
return this.data.refreshToken;
|
|
57
|
+
}
|
|
78
58
|
}
|
|
79
59
|
exports.default = CurrentAccount;
|
|
80
60
|
//# sourceMappingURL=CurrentAccount.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"CurrentAccount.js","sourceRoot":"","sources":["../../src/Account/CurrentAccount.ts"],"names":[],"mappings":";;;;;AAAA,mEAA2C;
|
|
1
|
+
{"version":3,"file":"CurrentAccount.js","sourceRoot":"","sources":["../../src/Account/CurrentAccount.ts"],"names":[],"mappings":";;;;;AAAA,mEAA2C;AA0B3C,SAAS,WAAW;IAClB,OAAO;QACL,aAAa,EAAE,cAAc;QAC7B,OAAO,EAAE,IAAI;QACb,OAAO,EAAE;YACP,MAAM,EAAE,GAAG;YACX,KAAK,EAAE,GAAG;YACV,GAAG,EAAE,GAAG;YACR,OAAO,EAAE,GAAG;SACb;QACD,aAAa,EAAE,SAAS;QACxB,QAAQ,EAAE,SAAS;QACnB,KAAK,EAAE,SAAS;QAChB,YAAY,EAAE,SAAS;KACxB,CAAC;AACJ,CAAC;AAED;;;GAGG;AACH,MAAM,cAAe,SAAQ,oBAAuB;IAClD,YAAY,IAAkB;QAC5B,KAAK,CAAC,IAAI,IAAI,WAAW,EAAE,CAAC,CAAC;IAC/B,CAAC;IAED,MAAM;QACJ,IAAI,CAAC,OAAO,CAAC,WAAW,EAAE,CAAC,CAAC;IAC9B,CAAC;IAED,IAAI,cAAc;QAChB,OAAO,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,YAAY,CAAC;IAClC,CAAC;IAED,IAAI,aAAa;QACf,OAAO,IAAI,CAAC,IAAI,CAAC,aAAa,CAAC;IACjC,CAAC;IAED,IAAI,OAAO;QACT,OAAO,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC;IAC3B,CAAC;IAED,IAAI,OAAO;QACT,OAAO,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC;IAC3B,CAAC;IAED,IAAI,aAAa;QACf,OAAO,IAAI,CAAC,IAAI,CAAC,aAAa,CAAC;IACjC,CAAC;IAED,IAAI,QAAQ;QACV,OAAO,IAAI,CAAC,IAAI,CAAC,QAAQ,CAAC;IAC5B,CAAC;IAED,IAAI,KAAK;QACP,OAAO,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC;IACzB,CAAC;IAED,IAAI,YAAY;QACd,OAAO,IAAI,CAAC,IAAI,CAAC,YAAY,CAAC;IAChC,CAAC;CACF;AAED,kBAAe,cAAc,CAAC"}
|
package/dist/Account/index.d.ts
CHANGED
|
@@ -4,6 +4,7 @@ import { Wallet } from 'ethers';
|
|
|
4
4
|
import { ApiReponse } from '../ApiClient';
|
|
5
5
|
import CurrentAccount from './CurrentAccount';
|
|
6
6
|
import { SupernetType } from '../ApiClient/WebSocketClient/types';
|
|
7
|
+
import { Tokens } from '../lib/AuthManager';
|
|
7
8
|
/**
|
|
8
9
|
* Account API methods that let you interact with the user's account.
|
|
9
10
|
* Can be accessed via `client.account`. Look for more samples below.
|
|
@@ -21,6 +22,7 @@ declare class AccountApi extends ApiGroup {
|
|
|
21
22
|
private handleBalanceUpdate;
|
|
22
23
|
private handleServerConnected;
|
|
23
24
|
private handleServerDisconnected;
|
|
25
|
+
private handleAuthUpdated;
|
|
24
26
|
private getNonce;
|
|
25
27
|
/**
|
|
26
28
|
* Create Ethers.js Wallet instance from username and password.
|
|
@@ -49,7 +51,7 @@ declare class AccountApi extends ApiGroup {
|
|
|
49
51
|
*/
|
|
50
52
|
create(username: string, email: string, password: string, subscribe?: boolean, referralCode?: string): Promise<AccountCreateData>;
|
|
51
53
|
/**
|
|
52
|
-
* Restore session with username and
|
|
54
|
+
* Restore session with username and refresh token.
|
|
53
55
|
*
|
|
54
56
|
* You can save access token that you get from the login method and restore the session with this method.
|
|
55
57
|
*
|
|
@@ -71,9 +73,9 @@ declare class AccountApi extends ApiGroup {
|
|
|
71
73
|
* ```
|
|
72
74
|
*
|
|
73
75
|
* @param username
|
|
74
|
-
* @param token
|
|
76
|
+
* @param tokens - Refresh token, access token pair { refreshToken: string, token: string }
|
|
75
77
|
*/
|
|
76
|
-
setToken(username: string,
|
|
78
|
+
setToken(username: string, tokens: Tokens): Promise<void>;
|
|
77
79
|
/**
|
|
78
80
|
* Login with username and password. WebSocket connection is established after successful login.
|
|
79
81
|
*
|
package/dist/Account/index.js
CHANGED
|
@@ -34,6 +34,7 @@ class AccountApi extends ApiGroup_1.default {
|
|
|
34
34
|
this.client.socket.on('balanceUpdate', this.handleBalanceUpdate.bind(this));
|
|
35
35
|
this.client.on('connected', this.handleServerConnected.bind(this));
|
|
36
36
|
this.client.on('disconnected', this.handleServerDisconnected.bind(this));
|
|
37
|
+
this.client.auth.on('updated', this.handleAuthUpdated.bind(this));
|
|
37
38
|
}
|
|
38
39
|
handleBalanceUpdate(data) {
|
|
39
40
|
this.currentAccount._update({ balance: data });
|
|
@@ -47,6 +48,14 @@ class AccountApi extends ApiGroup_1.default {
|
|
|
47
48
|
handleServerDisconnected() {
|
|
48
49
|
this.currentAccount._clear();
|
|
49
50
|
}
|
|
51
|
+
handleAuthUpdated({ refreshToken, token, walletAddress }) {
|
|
52
|
+
if (!refreshToken) {
|
|
53
|
+
this.currentAccount._clear();
|
|
54
|
+
}
|
|
55
|
+
else {
|
|
56
|
+
this.currentAccount._update({ walletAddress, token, refreshToken });
|
|
57
|
+
}
|
|
58
|
+
}
|
|
50
59
|
getNonce(walletAddress) {
|
|
51
60
|
return __awaiter(this, void 0, void 0, function* () {
|
|
52
61
|
const res = yield this.client.rest.post('/v1/account/nonce', {
|
|
@@ -99,12 +108,12 @@ class AccountApi extends ApiGroup_1.default {
|
|
|
99
108
|
const signature = yield this.eip712.signTypedData(wallet, 'signup', Object.assign(Object.assign({}, payload), { nonce }));
|
|
100
109
|
const res = yield this.client.rest.post('/v1/account/create', Object.assign(Object.assign({}, payload), { referralCode,
|
|
101
110
|
signature }));
|
|
102
|
-
this.setToken(username, res.data.token);
|
|
111
|
+
yield this.setToken(username, { refreshToken: res.data.refreshToken, token: res.data.token });
|
|
103
112
|
return res.data;
|
|
104
113
|
});
|
|
105
114
|
}
|
|
106
115
|
/**
|
|
107
|
-
* Restore session with username and
|
|
116
|
+
* Restore session with username and refresh token.
|
|
108
117
|
*
|
|
109
118
|
* You can save access token that you get from the login method and restore the session with this method.
|
|
110
119
|
*
|
|
@@ -126,13 +135,15 @@ class AccountApi extends ApiGroup_1.default {
|
|
|
126
135
|
* ```
|
|
127
136
|
*
|
|
128
137
|
* @param username
|
|
129
|
-
* @param token
|
|
138
|
+
* @param tokens - Refresh token, access token pair { refreshToken: string, token: string }
|
|
130
139
|
*/
|
|
131
|
-
setToken(username,
|
|
132
|
-
this
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
140
|
+
setToken(username, tokens) {
|
|
141
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
142
|
+
yield this.client.authenticate(tokens);
|
|
143
|
+
this.currentAccount._update({
|
|
144
|
+
username,
|
|
145
|
+
walletAddress: this.client.auth.walletAddress
|
|
146
|
+
});
|
|
136
147
|
});
|
|
137
148
|
}
|
|
138
149
|
/**
|
|
@@ -159,7 +170,7 @@ class AccountApi extends ApiGroup_1.default {
|
|
|
159
170
|
walletAddress: wallet.address,
|
|
160
171
|
signature
|
|
161
172
|
});
|
|
162
|
-
this.setToken(username, res.data.token);
|
|
173
|
+
yield this.setToken(username, { refreshToken: res.data.refreshToken, token: res.data.token });
|
|
163
174
|
return res.data;
|
|
164
175
|
});
|
|
165
176
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/Account/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;AAWA,2DAAkD;AAClD,mCAA4E;AAC5E,4CAAoD;AACpD,sEAA8C;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/Account/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;AAWA,2DAAkD;AAClD,mCAA4E;AAC5E,4CAAoD;AACpD,sEAA8C;AAI9C;;;;;;;;;;GAUG;AACH,MAAM,UAAW,SAAQ,kBAAQ;IAG/B,YAAY,MAAiB;QAC3B,KAAK,CAAC,MAAM,CAAC,CAAC;QAHP,mBAAc,GAAG,IAAI,wBAAc,EAAE,CAAC;QAI7C,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,EAAE,CAAC,eAAe,EAAE,IAAI,CAAC,mBAAmB,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC;QAC5E,IAAI,CAAC,MAAM,CAAC,EAAE,CAAC,WAAW,EAAE,IAAI,CAAC,qBAAqB,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC;QACnE,IAAI,CAAC,MAAM,CAAC,EAAE,CAAC,cAAc,EAAE,IAAI,CAAC,wBAAwB,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC;QACzE,IAAI,CAAC,MAAM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I,CAAC,OAAO,CAAC,CAAC;YACpF,MAAM,eAAe,GAAG,kBAAS,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC;YAClD,MAAM,cAAc,mCACf,OAAO,KACV,QAAQ,EACR,CAAC,EAAE,eAAe,CAAC,CAAC,EACpB,CAAC,EAAE,eAAe,CAAC,CAAC,EACpB,CAAC,EAAE,eAAe,CAAC,CAAC,GACrB,CAAC;YACF,MAAM,gBAAgB,GAAG,MAAM,IAAI,CAAC,MAAM,CAAC,aAAa,CAAC,MAAM,EAAE,SAAS,kCACrE,cAAc,KACjB,KAAK,IACL,CAAC;YACH,MAAM,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,2BAA2B,kCAClD,cAAc,KACjB,SAAS,EAAE,gBAAgB,IAC3B,CAAC;QACL,CAAC;KAAA;CACF;AAED,kBAAe,UAAU,CAAC"}
|
package/dist/Account/types.d.ts
CHANGED
|
@@ -1,19 +1,19 @@
|
|
|
1
1
|
import { MessageType, SocketMessageMap } from './messages';
|
|
2
2
|
import { SocketEventMap } from './events';
|
|
3
|
-
import RestClient
|
|
3
|
+
import RestClient from '../../lib/RestClient';
|
|
4
4
|
import { SupernetType } from './types';
|
|
5
5
|
import { Logger } from '../../lib/DefaultLogger';
|
|
6
|
+
import AuthManager from '../../lib/AuthManager';
|
|
6
7
|
declare class WebSocketClient extends RestClient<SocketEventMap> {
|
|
7
8
|
appId: string;
|
|
8
9
|
baseUrl: string;
|
|
9
10
|
private socket;
|
|
10
11
|
private _supernetType;
|
|
11
12
|
private _pingInterval;
|
|
12
|
-
constructor(baseUrl: string, appId: string, supernetType: SupernetType, logger: Logger);
|
|
13
|
-
set auth(auth: AuthData | null);
|
|
13
|
+
constructor(baseUrl: string, auth: AuthManager, appId: string, supernetType: SupernetType, logger: Logger);
|
|
14
14
|
get supernetType(): SupernetType;
|
|
15
15
|
get isConnected(): boolean;
|
|
16
|
-
connect(): void
|
|
16
|
+
connect(): Promise<void>;
|
|
17
17
|
disconnect(): void;
|
|
18
18
|
private startPing;
|
|
19
19
|
private stopPing;
|
|
@@ -16,39 +16,22 @@ const RestClient_1 = __importDefault(require("../../lib/RestClient"));
|
|
|
16
16
|
const isomorphic_ws_1 = __importDefault(require("isomorphic-ws"));
|
|
17
17
|
const base64_1 = require("../../lib/base64");
|
|
18
18
|
const isNodejs_1 = __importDefault(require("../../lib/isNodejs"));
|
|
19
|
-
const js_cookie_1 = __importDefault(require("js-cookie"));
|
|
20
19
|
const version_1 = require("../../version");
|
|
20
|
+
const PROTOCOL_VERSION = '0.4.3';
|
|
21
21
|
const PING_INTERVAL = 15000;
|
|
22
22
|
class WebSocketClient extends RestClient_1.default {
|
|
23
|
-
constructor(baseUrl, appId, supernetType, logger) {
|
|
23
|
+
constructor(baseUrl, auth, appId, supernetType, logger) {
|
|
24
24
|
const _baseUrl = new URL(baseUrl);
|
|
25
25
|
if (_baseUrl.protocol === 'wss:') {
|
|
26
26
|
_baseUrl.protocol = 'https:';
|
|
27
27
|
}
|
|
28
|
-
super(_baseUrl.toString(), logger);
|
|
28
|
+
super(_baseUrl.toString(), auth, logger);
|
|
29
29
|
this.socket = null;
|
|
30
30
|
this._pingInterval = null;
|
|
31
31
|
this.appId = appId;
|
|
32
32
|
this.baseUrl = _baseUrl.toString();
|
|
33
33
|
this._supernetType = supernetType;
|
|
34
34
|
}
|
|
35
|
-
set auth(auth) {
|
|
36
|
-
//In browser, set the cookie
|
|
37
|
-
if (!isNodejs_1.default) {
|
|
38
|
-
if (auth) {
|
|
39
|
-
js_cookie_1.default.set('authorization', auth.token, {
|
|
40
|
-
domain: '.sogni.ai',
|
|
41
|
-
expires: 1
|
|
42
|
-
});
|
|
43
|
-
}
|
|
44
|
-
else {
|
|
45
|
-
js_cookie_1.default.remove('authorization', {
|
|
46
|
-
domain: '.sogni.ai'
|
|
47
|
-
});
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
this._auth = auth;
|
|
51
|
-
}
|
|
52
35
|
get supernetType() {
|
|
53
36
|
return this._supernetType;
|
|
54
37
|
}
|
|
@@ -56,34 +39,35 @@ class WebSocketClient extends RestClient_1.default {
|
|
|
56
39
|
return !!this.socket;
|
|
57
40
|
}
|
|
58
41
|
connect() {
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
42
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
43
|
+
if (this.socket) {
|
|
44
|
+
this.disconnect();
|
|
45
|
+
}
|
|
46
|
+
const userAgent = `Sogni/${PROTOCOL_VERSION} (sogni-client) ${version_1.LIB_VERSION}`;
|
|
47
|
+
const url = new URL(this.baseUrl);
|
|
48
|
+
url.protocol = 'wss:';
|
|
49
|
+
url.searchParams.set('appId', this.appId);
|
|
50
|
+
url.searchParams.set('clientName', userAgent);
|
|
51
|
+
url.searchParams.set('clientType', 'artist');
|
|
52
|
+
//At this point 'relaxed' does not work as expected, so we use 'fast' or empty
|
|
53
|
+
url.searchParams.set('forceWorkerId', this._supernetType === 'fast' ? 'fast' : '');
|
|
54
|
+
let params;
|
|
55
|
+
// In Node.js, ws package is used, so we need to set the auth header
|
|
56
|
+
if (isNodejs_1.default) {
|
|
57
|
+
params = {
|
|
58
|
+
headers: {
|
|
59
|
+
Authorization: yield this.auth.getToken(),
|
|
60
|
+
'User-Agent': userAgent
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
this.socket = new isomorphic_ws_1.default(url.toString(), params);
|
|
65
|
+
this.socket.onerror = this.handleError.bind(this);
|
|
66
|
+
this.socket.onmessage = this.handleMessage.bind(this);
|
|
67
|
+
this.socket.onopen = this.handleOpen.bind(this);
|
|
68
|
+
this.socket.onclose = this.handleClose.bind(this);
|
|
69
|
+
this.startPing(this.socket);
|
|
70
|
+
});
|
|
87
71
|
}
|
|
88
72
|
disconnect() {
|
|
89
73
|
if (!this.socket) {
|
|
@@ -112,7 +96,7 @@ class WebSocketClient extends RestClient_1.default {
|
|
|
112
96
|
}
|
|
113
97
|
}
|
|
114
98
|
switchNetwork(supernetType) {
|
|
115
|
-
return new Promise((resolve
|
|
99
|
+
return new Promise((resolve) => __awaiter(this, void 0, void 0, function* () {
|
|
116
100
|
this.once('changeNetwork', ({ network }) => {
|
|
117
101
|
this._supernetType = network;
|
|
118
102
|
resolve(network);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/ApiClient/WebSocketClient/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;AAEA,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/ApiClient/WebSocketClient/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;AAEA,sEAA8C;AAE9C,kEAAgF;AAChF,6CAA8D;AAC9D,kEAA0C;AAC1C,2CAA4C;AAI5C,MAAM,gBAAgB,GAAG,OAAO,CAAC;AAEjC,MAAM,aAAa,GAAG,KAAK,CAAC;AAE5B,MAAM,eAAgB,SAAQ,oBAA0B;IAOtD,YACE,OAAe,EACf,IAAiB,EACjB,KAAa,EACb,YAA0B,EAC1B,MAAc;QAEd,MAAM,QAAQ,GAAG,IAAI,GAAG,CAAC,OAAO,CAAC,CAAC;QAClC,IAAI,QAAQ,CAAC,QAAQ,KAAK,MAAM,EAAE,CAAC;YACjC,QAAQ,CAAC,QAAQ,GAAG,QAAQ,CAAC;QAC/B,CAAC;QACD,KAAK,CAAC,QAAQ,CAAC,QAAQ,EAAE,EAAE,IAAI,EAAE,MAAM,CAAC,CAAC;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C"}
|
|
@@ -1,7 +1,14 @@
|
|
|
1
1
|
import { JobRequestRaw } from '../../Projects/createJobRequestMessage';
|
|
2
2
|
import { SupernetType } from './types';
|
|
3
|
+
export interface JobErrorMessage {
|
|
4
|
+
jobID: string;
|
|
5
|
+
error: 'artistCanceled';
|
|
6
|
+
error_message: 'artistCanceled';
|
|
7
|
+
isFromWorker: false;
|
|
8
|
+
}
|
|
3
9
|
export interface SocketMessageMap {
|
|
4
10
|
jobRequest: JobRequestRaw;
|
|
11
|
+
jobError: JobErrorMessage;
|
|
5
12
|
changeNetwork: SupernetType;
|
|
6
13
|
}
|
|
7
14
|
export type MessageType = keyof SocketMessageMap;
|