@hautechai/sdk 0.2.1 → 0.2.3

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.
@@ -202,7 +202,7 @@ export declare const createSDK: (options: SDKOptions) => {
202
202
  }) => Promise<void>;
203
203
  wait: <T extends import("../types").OperationEntity | {
204
204
  id: string;
205
- }, N extends number | undefined>(props: T, timeoutMs?: N) => Promise<((T extends import("../types").OperationEntity ? T : import("../types").OperationEntity) & ({
205
+ }, N extends number | undefined = undefined>(props: T, timeoutMs?: N) => Promise<((T extends import("../types").OperationEntity ? T : import("../types").OperationEntity) & ({
206
206
  status: "failed";
207
207
  output: null;
208
208
  } | {
@@ -1,11 +1,12 @@
1
1
  import { OperationEntity, OperationsApi } from '../../autogenerated';
2
- import { WebSocket } from 'ws';
2
+ import { client as WebSocketClient, connection as Connection } from 'websocket';
3
3
  export declare class OperationsListener {
4
4
  useWebsocket: {
5
5
  endpoint: string;
6
6
  token: () => string | Promise<string>;
7
7
  } | null;
8
- ws: WebSocket | null;
8
+ ws: WebSocketClient | null;
9
+ connection: Connection | null;
9
10
  operations: () => Promise<OperationsApi>;
10
11
  constructor({ ws, operations, }: {
11
12
  ws: {
@@ -20,6 +21,6 @@ export declare class OperationsListener {
20
21
  subscribe(): Promise<void>;
21
22
  onOpen(): void;
22
23
  onMessage(msg: string): void;
23
- onClose(number: number, reason: Buffer): void;
24
+ onClose(number: number, reason: string): void;
24
25
  close(): void;
25
26
  }
@@ -1,10 +1,11 @@
1
- import { WebSocket } from 'ws';
1
+ import { client as WebSocketClient } from 'websocket';
2
2
  import { HautechException } from '../exceptions';
3
3
  // This is pretty much a dirty solution until we need to rework this part.
4
4
  export class OperationsListener {
5
5
  constructor({ ws, operations, }) {
6
6
  this.useWebsocket = null;
7
7
  this.ws = null;
8
+ this.connection = null;
8
9
  this.operationsStore = {};
9
10
  if (ws) {
10
11
  this.useWebsocket = {
@@ -15,7 +16,7 @@ export class OperationsListener {
15
16
  this.operations = operations;
16
17
  }
17
18
  async getOperation(id) {
18
- const isWsReady = this.ws?.readyState == WebSocket.OPEN;
19
+ const isWsReady = this.connection?.connected;
19
20
  if (!this.operationsStore[id] || !isWsReady) {
20
21
  const api = await this.operations();
21
22
  const operation = await api.operationsControllerGetOperationV1(id);
@@ -34,19 +35,28 @@ export class OperationsListener {
34
35
  if (!this.useWebsocket)
35
36
  return;
36
37
  try {
37
- this.ws = new WebSocket(this.useWebsocket.endpoint, ['1', await this.useWebsocket.token()]);
38
- this.ws.on('open', () => this.onOpen());
39
- this.ws.on('message', (msg) => this.onMessage(msg));
40
- this.ws.on('close', (number, reason) => this.onClose(number, reason));
38
+ this.ws = new WebSocketClient();
39
+ this.ws.connect(this.useWebsocket.endpoint, ['1', await this.useWebsocket.token()]);
40
+ this.ws.on('connect', (connection) => {
41
+ this.connection = connection;
42
+ this.onOpen();
43
+ connection.on('message', (msg) => {
44
+ if (msg.type === 'utf8')
45
+ this.onMessage(msg.utf8Data);
46
+ else if (msg.type === 'binary')
47
+ this.onMessage(msg.binaryData.toString('utf8'));
48
+ });
49
+ connection.on('close', (number, reason) => this.onClose(number, reason));
50
+ });
41
51
  }
42
52
  catch (err) {
43
53
  throw new HautechException(`SDK failed to open websocket: ${err}`);
44
54
  }
45
55
  }
46
56
  onOpen() {
47
- if (!this.ws)
57
+ if (!this.connection)
48
58
  throw new HautechException('Semantics error: this is a bug.');
49
- this.ws.send(JSON.stringify({
59
+ this.connection.send(JSON.stringify({
50
60
  event: 'subscribe',
51
61
  data: {
52
62
  channel: 'own_resources',
@@ -54,7 +64,7 @@ export class OperationsListener {
54
64
  }));
55
65
  }
56
66
  onMessage(msg) {
57
- if (!this.ws)
67
+ if (!this.connection)
58
68
  throw new HautechException('Semantics error: this is a bug.');
59
69
  const { event, data } = JSON.parse(msg);
60
70
  switch (event) {
@@ -67,15 +77,14 @@ export class OperationsListener {
67
77
  }
68
78
  }
69
79
  onClose(number, reason) {
70
- if (!this.ws)
80
+ if (!this.connection)
71
81
  throw new HautechException('Semantics error: this is a bug.');
72
82
  // Reset dirty state.
73
83
  this.operationsStore = {};
74
84
  this.ws = null;
75
85
  }
76
86
  close() {
77
- if (!this.ws)
78
- return;
79
- this.ws.close();
87
+ this.ws?.abort();
88
+ this.connection?.close();
80
89
  }
81
90
  }
@@ -121,6 +121,6 @@ declare const operations: (options: SDKOptions, relevantOperations: OperationsLi
121
121
  }) => Promise<void>;
122
122
  wait: <T extends OperationEntity | {
123
123
  id: string;
124
- }, N extends number | undefined>(props: T, timeoutMs?: N) => Promise<Waited<T extends OperationEntity ? T : OperationEntity> | (N extends undefined ? never : null)>;
124
+ }, N extends number | undefined = undefined>(props: T, timeoutMs?: N) => Promise<Waited<T extends OperationEntity ? T : OperationEntity> | (N extends undefined ? never : null)>;
125
125
  };
126
126
  export default operations;
@@ -55,23 +55,23 @@ const pipelines = (options) => {
55
55
  run: {
56
56
  haute: {
57
57
  linda: {
58
- v1: callMethod((methods) => methods.callControllerCallOperationsRunHauteLindaV1V1)
58
+ v1: callMethod((methods) => methods.callControllerCallOperationsRunHauteLindaV1V1),
59
59
  },
60
60
  naomi: {
61
- v1: callMethod((methods) => methods.callControllerCallOperationsRunHauteNaomiV1V1)
61
+ v1: callMethod((methods) => methods.callControllerCallOperationsRunHauteNaomiV1V1),
62
62
  },
63
63
  },
64
64
  inpaint: {
65
65
  kate: {
66
66
  v1: callMethod((methods) => methods.callControllerCallOperationsRunInpaintKateV1V1),
67
- }
67
+ },
68
68
  },
69
69
  gpt: {
70
- v1: callMethod((methods) => methods.callControllerCallOperationsRunGptV1V1)
70
+ v1: callMethod((methods) => methods.callControllerCallOperationsRunGptV1V1),
71
71
  },
72
72
  imagine: {
73
73
  kate: {
74
- v1: callMethod((methods) => methods.callControllerCallOperationsRunImagineKateV1V1)
74
+ v1: callMethod((methods) => methods.callControllerCallOperationsRunImagineKateV1V1),
75
75
  },
76
76
  },
77
77
  upscale: {
@@ -86,7 +86,7 @@ const pipelines = (options) => {
86
86
  },
87
87
  mask: {
88
88
  v1: callMethod((methods) => methods.callControllerCallOperationsRunSegmentAnythingMaskV1V1),
89
- }
89
+ },
90
90
  },
91
91
  poseEstimation: {
92
92
  v1: callMethod((methods) => methods.callControllerCallOperationsRunPoseEstimationV1V1),
@@ -100,7 +100,7 @@ const pipelines = (options) => {
100
100
  vton: {
101
101
  gisele: {
102
102
  v1: callMethod((methods) => methods.callControllerCallOperationsRunVtonGiseleV1V1),
103
- }
103
+ },
104
104
  },
105
105
  negateImage: {
106
106
  v1: callMethod((methods) => methods.callControllerCallOperationsRunNegateImageV1V1),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hautechai/sdk",
3
- "version": "0.2.1",
3
+ "version": "0.2.3",
4
4
  "license": "MIT",
5
5
  "keywords": [],
6
6
  "repository": {
@@ -13,7 +13,7 @@
13
13
  "@hautechai/pipelines": "0.3.2",
14
14
  "axios": "1.6.1",
15
15
  "jose": "5.9.6",
16
- "ws": "^8.18.0"
16
+ "websocket": "^1.0.35"
17
17
  },
18
18
  "devDependencies": {
19
19
  "@jest/globals": "^29.7.0",
@@ -21,10 +21,12 @@
21
21
  "@types/jest": "29.5.12",
22
22
  "@types/jsonwebtoken": "9.0.7",
23
23
  "@types/node": "22.5.2",
24
+ "@types/websocket": "^1.0.10",
24
25
  "@types/ws": "^8.5.14",
25
26
  "dotenv": "16.4.7",
26
27
  "jest": "29.7.0",
27
28
  "jest-environment-jsdom": "29.7.0",
29
+ "prettier": "^3.5.1",
28
30
  "ts-jest": "29.2.5",
29
31
  "ts-node": "10.9.2",
30
32
  "tslib": "2.7.0",