hume 0.3.3 → 0.4.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/Client.js +5 -5
- package/README.md +36 -10
- package/dist/Client.js +5 -5
- package/dist/wrapper/HumeStreamingClient.d.ts +10 -5
- package/dist/wrapper/HumeStreamingClient.js +68 -74
- package/dist/wrapper/StreamSocket.d.ts +15 -25
- package/dist/wrapper/StreamSocket.js +172 -45
- package/dist/wrapper/base64Encode.d.ts +1 -0
- package/dist/wrapper/base64Encode.js +17 -0
- package/package.json +1 -1
- package/wrapper/HumeStreamingClient.d.ts +10 -5
- package/wrapper/HumeStreamingClient.js +68 -74
- package/wrapper/StreamSocket.d.ts +15 -25
- package/wrapper/StreamSocket.js +172 -45
- package/wrapper/base64Encode.d.ts +1 -0
- package/wrapper/base64Encode.js +17 -0
package/Client.js
CHANGED
|
@@ -94,7 +94,7 @@ class HumeClient {
|
|
|
94
94
|
"X-Hume-Api-Key": yield core.Supplier.get(this._options.apiKey),
|
|
95
95
|
"X-Fern-Language": "JavaScript",
|
|
96
96
|
"X-Fern-SDK-Name": "hume",
|
|
97
|
-
"X-Fern-SDK-Version": "0.
|
|
97
|
+
"X-Fern-SDK-Version": "0.4.0",
|
|
98
98
|
},
|
|
99
99
|
contentType: "application/json",
|
|
100
100
|
queryParameters: _queryParams,
|
|
@@ -143,7 +143,7 @@ class HumeClient {
|
|
|
143
143
|
"X-Hume-Api-Key": yield core.Supplier.get(this._options.apiKey),
|
|
144
144
|
"X-Fern-Language": "JavaScript",
|
|
145
145
|
"X-Fern-SDK-Name": "hume",
|
|
146
|
-
"X-Fern-SDK-Version": "0.
|
|
146
|
+
"X-Fern-SDK-Version": "0.4.0",
|
|
147
147
|
},
|
|
148
148
|
contentType: "application/json",
|
|
149
149
|
body: yield serializers.BaseRequest.jsonOrThrow(request, { unrecognizedObjectKeys: "strip" }),
|
|
@@ -195,7 +195,7 @@ class HumeClient {
|
|
|
195
195
|
"X-Hume-Api-Key": yield core.Supplier.get(this._options.apiKey),
|
|
196
196
|
"X-Fern-Language": "JavaScript",
|
|
197
197
|
"X-Fern-SDK-Name": "hume",
|
|
198
|
-
"X-Fern-SDK-Version": "0.
|
|
198
|
+
"X-Fern-SDK-Version": "0.4.0",
|
|
199
199
|
},
|
|
200
200
|
contentType: "application/json",
|
|
201
201
|
timeoutMs: (requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.timeoutInSeconds) != null ? requestOptions.timeoutInSeconds * 1000 : 60000,
|
|
@@ -243,7 +243,7 @@ class HumeClient {
|
|
|
243
243
|
"X-Hume-Api-Key": yield core.Supplier.get(this._options.apiKey),
|
|
244
244
|
"X-Fern-Language": "JavaScript",
|
|
245
245
|
"X-Fern-SDK-Name": "hume",
|
|
246
|
-
"X-Fern-SDK-Version": "0.
|
|
246
|
+
"X-Fern-SDK-Version": "0.4.0",
|
|
247
247
|
},
|
|
248
248
|
contentType: "application/json",
|
|
249
249
|
responseType: "streaming",
|
|
@@ -287,7 +287,7 @@ class HumeClient {
|
|
|
287
287
|
"X-Hume-Api-Key": yield core.Supplier.get(this._options.apiKey),
|
|
288
288
|
"X-Fern-Language": "JavaScript",
|
|
289
289
|
"X-Fern-SDK-Name": "hume",
|
|
290
|
-
"X-Fern-SDK-Version": "0.
|
|
290
|
+
"X-Fern-SDK-Version": "0.4.0",
|
|
291
291
|
},
|
|
292
292
|
contentType: "application/json",
|
|
293
293
|
timeoutMs: (requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.timeoutInSeconds) != null ? requestOptions.timeoutInSeconds * 1000 : 60000,
|
package/README.md
CHANGED
|
@@ -62,23 +62,35 @@ const client = new HumeStreamingClient({
|
|
|
62
62
|
apiKey: "YOUR_API_KEY",
|
|
63
63
|
});
|
|
64
64
|
|
|
65
|
-
const stream =
|
|
65
|
+
const stream = client.connect({
|
|
66
66
|
configs: {
|
|
67
67
|
language: {},
|
|
68
68
|
},
|
|
69
|
-
onMessage: (response) => {
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
},
|
|
74
|
-
onError: (err) => {
|
|
75
|
-
console.log(err);
|
|
76
|
-
}
|
|
69
|
+
onMessage: (response) => { console.log("Socket opened") },
|
|
70
|
+
onWarning: (warning) => { console.log(warning)},
|
|
71
|
+
onError: (error) => { console.log(error)},
|
|
72
|
+
onClose: () => { console.log("Socket closed")},
|
|
77
73
|
});
|
|
78
74
|
|
|
79
|
-
stream.sendText({
|
|
75
|
+
const response = await stream.sendText({
|
|
80
76
|
text: "Mary had a little lamb,"
|
|
81
77
|
});
|
|
78
|
+
console.log(response);
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
### Sending Files
|
|
82
|
+
You can use the `sendFile` method to upload files.
|
|
83
|
+
|
|
84
|
+
```typescript
|
|
85
|
+
const response = await socket.sendFile({
|
|
86
|
+
file: fs.createReadStream(path.join(__dirname, "obama.png")),
|
|
87
|
+
config: {
|
|
88
|
+
face: {
|
|
89
|
+
identifyFaces: true,
|
|
90
|
+
},
|
|
91
|
+
},
|
|
92
|
+
});
|
|
93
|
+
console.log(response);
|
|
82
94
|
```
|
|
83
95
|
|
|
84
96
|
## Errors
|
|
@@ -124,3 +136,17 @@ await hume.submitJob(..., {
|
|
|
124
136
|
timeoutInSeconds: 10, // timeout after 10 seconds
|
|
125
137
|
});
|
|
126
138
|
```
|
|
139
|
+
|
|
140
|
+
## Beta Status
|
|
141
|
+
This SDK is in beta, and there may be breaking changes between versions without a major
|
|
142
|
+
version update. Therefore, we recommend pinning the package version to a specific version.
|
|
143
|
+
This way, you can install the same version each time without breaking changes.
|
|
144
|
+
|
|
145
|
+
## Contributing
|
|
146
|
+
While we value open-source contributions to this SDK, this library is generated programmatically.
|
|
147
|
+
Additions made directly to this library would have to be moved over to our generation code,
|
|
148
|
+
otherwise they would be overwritten upon the next generated release. Feel free to open a PR as a
|
|
149
|
+
proof of concept, but know that we will not be able to merge it as-is. We suggest opening an
|
|
150
|
+
issue first to discuss with us!
|
|
151
|
+
|
|
152
|
+
On the other hand, contributions to the README are always very welcome!
|
package/dist/Client.js
CHANGED
|
@@ -94,7 +94,7 @@ class HumeClient {
|
|
|
94
94
|
"X-Hume-Api-Key": yield core.Supplier.get(this._options.apiKey),
|
|
95
95
|
"X-Fern-Language": "JavaScript",
|
|
96
96
|
"X-Fern-SDK-Name": "hume",
|
|
97
|
-
"X-Fern-SDK-Version": "0.
|
|
97
|
+
"X-Fern-SDK-Version": "0.4.0",
|
|
98
98
|
},
|
|
99
99
|
contentType: "application/json",
|
|
100
100
|
queryParameters: _queryParams,
|
|
@@ -143,7 +143,7 @@ class HumeClient {
|
|
|
143
143
|
"X-Hume-Api-Key": yield core.Supplier.get(this._options.apiKey),
|
|
144
144
|
"X-Fern-Language": "JavaScript",
|
|
145
145
|
"X-Fern-SDK-Name": "hume",
|
|
146
|
-
"X-Fern-SDK-Version": "0.
|
|
146
|
+
"X-Fern-SDK-Version": "0.4.0",
|
|
147
147
|
},
|
|
148
148
|
contentType: "application/json",
|
|
149
149
|
body: yield serializers.BaseRequest.jsonOrThrow(request, { unrecognizedObjectKeys: "strip" }),
|
|
@@ -195,7 +195,7 @@ class HumeClient {
|
|
|
195
195
|
"X-Hume-Api-Key": yield core.Supplier.get(this._options.apiKey),
|
|
196
196
|
"X-Fern-Language": "JavaScript",
|
|
197
197
|
"X-Fern-SDK-Name": "hume",
|
|
198
|
-
"X-Fern-SDK-Version": "0.
|
|
198
|
+
"X-Fern-SDK-Version": "0.4.0",
|
|
199
199
|
},
|
|
200
200
|
contentType: "application/json",
|
|
201
201
|
timeoutMs: (requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.timeoutInSeconds) != null ? requestOptions.timeoutInSeconds * 1000 : 60000,
|
|
@@ -243,7 +243,7 @@ class HumeClient {
|
|
|
243
243
|
"X-Hume-Api-Key": yield core.Supplier.get(this._options.apiKey),
|
|
244
244
|
"X-Fern-Language": "JavaScript",
|
|
245
245
|
"X-Fern-SDK-Name": "hume",
|
|
246
|
-
"X-Fern-SDK-Version": "0.
|
|
246
|
+
"X-Fern-SDK-Version": "0.4.0",
|
|
247
247
|
},
|
|
248
248
|
contentType: "application/json",
|
|
249
249
|
responseType: "streaming",
|
|
@@ -287,7 +287,7 @@ class HumeClient {
|
|
|
287
287
|
"X-Hume-Api-Key": yield core.Supplier.get(this._options.apiKey),
|
|
288
288
|
"X-Fern-Language": "JavaScript",
|
|
289
289
|
"X-Fern-SDK-Name": "hume",
|
|
290
|
-
"X-Fern-SDK-Version": "0.
|
|
290
|
+
"X-Fern-SDK-Version": "0.4.0",
|
|
291
291
|
},
|
|
292
292
|
contentType: "application/json",
|
|
293
293
|
timeoutMs: (requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.timeoutInSeconds) != null ? requestOptions.timeoutInSeconds * 1000 : 60000,
|
|
@@ -1,23 +1,28 @@
|
|
|
1
1
|
import * as Hume from "../api";
|
|
2
|
-
import * as core from "../core";
|
|
3
2
|
import { StreamSocket } from "./StreamSocket";
|
|
3
|
+
import WebSocket from "ws";
|
|
4
4
|
export declare namespace HumeStreamingClient {
|
|
5
5
|
interface Options {
|
|
6
|
-
apiKey:
|
|
6
|
+
apiKey: string;
|
|
7
7
|
openTimeoutInSeconds?: number;
|
|
8
8
|
}
|
|
9
9
|
interface ConnectArgs {
|
|
10
10
|
config: Hume.ModelConfig;
|
|
11
11
|
streamWindowMs?: number;
|
|
12
|
-
onOpen?: () => void;
|
|
12
|
+
onOpen?: (event: WebSocket.Event) => void;
|
|
13
13
|
onMessage?: (message: Hume.ModelResponse) => void;
|
|
14
14
|
onWarning?: (error: Hume.ModelsWarning) => void;
|
|
15
15
|
onError?: (error: Hume.ModelsError) => void;
|
|
16
|
-
onClose?: () => void;
|
|
16
|
+
onClose?: (event: WebSocket.Event) => void;
|
|
17
17
|
}
|
|
18
18
|
}
|
|
19
19
|
export declare class HumeStreamingClient {
|
|
20
20
|
protected readonly _options: HumeStreamingClient.Options;
|
|
21
21
|
constructor(_options: HumeStreamingClient.Options);
|
|
22
|
-
connect(args: HumeStreamingClient.ConnectArgs):
|
|
22
|
+
connect(args: HumeStreamingClient.ConnectArgs): StreamSocket;
|
|
23
23
|
}
|
|
24
|
+
export declare function parse(data: WebSocket.Data, args?: {
|
|
25
|
+
onMessage?: (message: Hume.ModelResponse) => void;
|
|
26
|
+
onWarning?: (error: Hume.ModelsWarning) => void;
|
|
27
|
+
onError?: (error: Hume.ModelsError) => void;
|
|
28
|
+
}): Promise<Hume.ModelResponse | Hume.ModelsWarning | Hume.ModelsError | undefined>;
|
|
@@ -35,8 +35,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
35
35
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
36
36
|
};
|
|
37
37
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
38
|
-
exports.HumeStreamingClient = void 0;
|
|
39
|
-
const core = __importStar(require("../core"));
|
|
38
|
+
exports.parse = exports.HumeStreamingClient = void 0;
|
|
40
39
|
const serializers = __importStar(require("../serialization"));
|
|
41
40
|
const StreamSocket_1 = require("./StreamSocket");
|
|
42
41
|
const ws_1 = __importDefault(require("ws"));
|
|
@@ -45,81 +44,76 @@ class HumeStreamingClient {
|
|
|
45
44
|
this._options = _options;
|
|
46
45
|
}
|
|
47
46
|
connect(args) {
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
error: e.message,
|
|
64
|
-
});
|
|
65
|
-
});
|
|
66
|
-
websocket.addEventListener("message", ({ data }) => __awaiter(this, void 0, void 0, function* () {
|
|
67
|
-
var _a, _b, _c;
|
|
68
|
-
const body = JSON.parse(data);
|
|
69
|
-
const parsedResponse = yield serializers.ModelResponse.parse(body, {
|
|
70
|
-
unrecognizedObjectKeys: "passthrough",
|
|
71
|
-
allowUnrecognizedUnionMembers: true,
|
|
72
|
-
allowUnrecognizedEnumValues: true,
|
|
73
|
-
breadcrumbsPrefix: ["response"],
|
|
74
|
-
});
|
|
75
|
-
if (parsedResponse.ok) {
|
|
76
|
-
(_a = args.onMessage) === null || _a === void 0 ? void 0 : _a.call(args, parsedResponse.value);
|
|
77
|
-
return;
|
|
78
|
-
}
|
|
79
|
-
const parsedWarning = yield serializers.ModelsWarning.parse(body, {
|
|
80
|
-
unrecognizedObjectKeys: "passthrough",
|
|
81
|
-
allowUnrecognizedUnionMembers: true,
|
|
82
|
-
allowUnrecognizedEnumValues: true,
|
|
83
|
-
breadcrumbsPrefix: ["response"],
|
|
84
|
-
});
|
|
85
|
-
if (parsedWarning.ok) {
|
|
86
|
-
(_b = args.onWarning) === null || _b === void 0 ? void 0 : _b.call(args, parsedWarning.value);
|
|
87
|
-
return;
|
|
88
|
-
}
|
|
89
|
-
const parsedError = yield serializers.ModelsError.parse(body, {
|
|
90
|
-
unrecognizedObjectKeys: "passthrough",
|
|
91
|
-
allowUnrecognizedUnionMembers: true,
|
|
92
|
-
allowUnrecognizedEnumValues: true,
|
|
93
|
-
breadcrumbsPrefix: ["response"],
|
|
94
|
-
});
|
|
95
|
-
if (parsedError.ok) {
|
|
96
|
-
(_c = args.onError) === null || _c === void 0 ? void 0 : _c.call(args, parsedError.value);
|
|
97
|
-
return;
|
|
98
|
-
}
|
|
99
|
-
}));
|
|
100
|
-
websocket.addEventListener("close", () => {
|
|
101
|
-
var _a;
|
|
102
|
-
(_a = args.onClose) === null || _a === void 0 ? void 0 : _a.call(args);
|
|
47
|
+
const websocket = new ws_1.default(`wss://api.hume.ai/v0/stream/models`, {
|
|
48
|
+
headers: {
|
|
49
|
+
"X-Hume-Api-Key": this._options.apiKey,
|
|
50
|
+
},
|
|
51
|
+
timeout: this._options.openTimeoutInSeconds,
|
|
52
|
+
});
|
|
53
|
+
websocket.addEventListener("open", (event) => {
|
|
54
|
+
var _a;
|
|
55
|
+
(_a = args.onOpen) === null || _a === void 0 ? void 0 : _a.call(args, event);
|
|
56
|
+
});
|
|
57
|
+
websocket.addEventListener("error", (e) => {
|
|
58
|
+
var _a;
|
|
59
|
+
(_a = args.onError) === null || _a === void 0 ? void 0 : _a.call(args, {
|
|
60
|
+
code: e.type,
|
|
61
|
+
error: e.message,
|
|
103
62
|
});
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
config: args.config,
|
|
111
|
-
}));
|
|
112
|
-
});
|
|
113
|
-
}
|
|
114
|
-
else {
|
|
115
|
-
resolve(new StreamSocket_1.StreamSocket({
|
|
116
|
-
websocket,
|
|
117
|
-
streamWindowMs: args.streamWindowMs,
|
|
118
|
-
config: args.config,
|
|
119
|
-
}));
|
|
120
|
-
}
|
|
63
|
+
});
|
|
64
|
+
websocket.addEventListener("message", ({ data }) => __awaiter(this, void 0, void 0, function* () {
|
|
65
|
+
parse(data, {
|
|
66
|
+
onMessage: args.onMessage,
|
|
67
|
+
onWarning: args.onWarning,
|
|
68
|
+
onError: args.onError,
|
|
121
69
|
});
|
|
70
|
+
}));
|
|
71
|
+
websocket.addEventListener("close", (event) => {
|
|
72
|
+
var _a;
|
|
73
|
+
(_a = args.onClose) === null || _a === void 0 ? void 0 : _a.call(args, event);
|
|
74
|
+
});
|
|
75
|
+
return new StreamSocket_1.StreamSocket({
|
|
76
|
+
websocket,
|
|
77
|
+
streamWindowMs: args.streamWindowMs,
|
|
78
|
+
config: args.config,
|
|
122
79
|
});
|
|
123
80
|
}
|
|
124
81
|
}
|
|
125
82
|
exports.HumeStreamingClient = HumeStreamingClient;
|
|
83
|
+
function parse(data, args = {}) {
|
|
84
|
+
var _a, _b, _c;
|
|
85
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
86
|
+
const message = JSON.parse(data);
|
|
87
|
+
const parsedResponse = yield serializers.ModelResponse.parse(message, {
|
|
88
|
+
unrecognizedObjectKeys: "passthrough",
|
|
89
|
+
allowUnrecognizedUnionMembers: true,
|
|
90
|
+
allowUnrecognizedEnumValues: true,
|
|
91
|
+
breadcrumbsPrefix: ["response"],
|
|
92
|
+
});
|
|
93
|
+
if (parsedResponse.ok) {
|
|
94
|
+
(_a = args.onMessage) === null || _a === void 0 ? void 0 : _a.call(args, parsedResponse.value);
|
|
95
|
+
return parsedResponse.value;
|
|
96
|
+
}
|
|
97
|
+
const parsedWarning = yield serializers.ModelsWarning.parse(message, {
|
|
98
|
+
unrecognizedObjectKeys: "passthrough",
|
|
99
|
+
allowUnrecognizedUnionMembers: true,
|
|
100
|
+
allowUnrecognizedEnumValues: true,
|
|
101
|
+
breadcrumbsPrefix: ["response"],
|
|
102
|
+
});
|
|
103
|
+
if (parsedWarning.ok) {
|
|
104
|
+
(_b = args.onWarning) === null || _b === void 0 ? void 0 : _b.call(args, parsedWarning.value);
|
|
105
|
+
return parsedWarning.value;
|
|
106
|
+
}
|
|
107
|
+
const parsedError = yield serializers.ModelsError.parse(message, {
|
|
108
|
+
unrecognizedObjectKeys: "passthrough",
|
|
109
|
+
allowUnrecognizedUnionMembers: true,
|
|
110
|
+
allowUnrecognizedEnumValues: true,
|
|
111
|
+
breadcrumbsPrefix: ["response"],
|
|
112
|
+
});
|
|
113
|
+
if (parsedError.ok) {
|
|
114
|
+
(_c = args.onError) === null || _c === void 0 ? void 0 : _c.call(args, parsedError.value);
|
|
115
|
+
return parsedError.value;
|
|
116
|
+
}
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
exports.parse = parse;
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
2
|
import WebSocket from "ws";
|
|
3
3
|
import * as Hume from "../api";
|
|
4
|
+
import * as fs from "fs";
|
|
4
5
|
export declare namespace StreamSocket {
|
|
5
6
|
interface Args {
|
|
6
7
|
websocket: WebSocket;
|
|
@@ -14,29 +15,16 @@ export declare class StreamSocket {
|
|
|
14
15
|
private config;
|
|
15
16
|
constructor({ websocket, config, streamWindowMs }: StreamSocket.Args);
|
|
16
17
|
/**
|
|
17
|
-
* Send
|
|
18
|
+
* Send file on the `StreamSocket`
|
|
18
19
|
*
|
|
19
|
-
* @param
|
|
20
|
-
* @param config
|
|
21
|
-
*
|
|
22
|
-
* set when initializing the `StreamSocket`
|
|
23
|
-
*/
|
|
24
|
-
sendFile({ filepath, config, }: {
|
|
25
|
-
filepath: string;
|
|
26
|
-
config?: Hume.ModelConfig;
|
|
27
|
-
}): Promise<void>;
|
|
28
|
-
/**
|
|
29
|
-
* Send raw bytes on the `StreamSocket`
|
|
30
|
-
*
|
|
31
|
-
* @param buffer Raw bytes of media to send on socket connection
|
|
32
|
-
* @param config List of model configurations.
|
|
33
|
-
* If set these configurations will overwrite any configurations
|
|
34
|
-
* set when initializing the `StreamSocket`
|
|
20
|
+
* @param file A fs.ReadStream | File | Blob
|
|
21
|
+
* @param config This method is intended for use with a `LanguageConfig`.
|
|
22
|
+
* When the socket is configured for other modalities this method will fail.
|
|
35
23
|
*/
|
|
36
|
-
|
|
37
|
-
|
|
24
|
+
sendFile({ file, config, }: {
|
|
25
|
+
file: fs.ReadStream | Blob;
|
|
38
26
|
config?: Hume.ModelConfig;
|
|
39
|
-
}):
|
|
27
|
+
}): Promise<Hume.ModelResponse>;
|
|
40
28
|
/**
|
|
41
29
|
* Send text on the `StreamSocket`
|
|
42
30
|
*
|
|
@@ -44,10 +32,10 @@ export declare class StreamSocket {
|
|
|
44
32
|
* @param config This method is intended for use with a `LanguageConfig`.
|
|
45
33
|
* When the socket is configured for other modalities this method will fail.
|
|
46
34
|
*/
|
|
47
|
-
sendText({ text, config
|
|
35
|
+
sendText({ text, config }: {
|
|
48
36
|
text: string;
|
|
49
37
|
config?: Hume.ModelConfig;
|
|
50
|
-
}):
|
|
38
|
+
}): Promise<Hume.ModelResponse>;
|
|
51
39
|
/**
|
|
52
40
|
* Send facemesh landmarks on the `StreamSocket`
|
|
53
41
|
*
|
|
@@ -61,7 +49,7 @@ export declare class StreamSocket {
|
|
|
61
49
|
sendFacemesh({ landmarks, config, }: {
|
|
62
50
|
landmarks: number[][][];
|
|
63
51
|
config?: Hume.ModelConfig;
|
|
64
|
-
}):
|
|
52
|
+
}): Promise<Hume.ModelResponse>;
|
|
65
53
|
/**
|
|
66
54
|
*
|
|
67
55
|
* Reset the streaming sliding window.
|
|
@@ -69,15 +57,17 @@ export declare class StreamSocket {
|
|
|
69
57
|
* Call this method when some media has been fully processed and you want to continue using the same
|
|
70
58
|
* streaming connection without leaking context across media samples.
|
|
71
59
|
*/
|
|
72
|
-
reset(): void
|
|
60
|
+
reset(): Promise<void>;
|
|
73
61
|
/**
|
|
74
62
|
*
|
|
75
63
|
* Get details associated with the current streaming connection.
|
|
76
64
|
*
|
|
77
65
|
*/
|
|
78
|
-
getJobDetails(): void
|
|
66
|
+
getJobDetails(): Promise<void>;
|
|
79
67
|
/**
|
|
80
68
|
* Closes the underlying socket.
|
|
81
69
|
*/
|
|
82
70
|
close(): void;
|
|
71
|
+
private send;
|
|
72
|
+
private tillSocketOpen;
|
|
83
73
|
}
|
|
@@ -1,4 +1,27 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
2
25
|
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
26
|
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
27
|
return new (P || (P = Promise))(function (resolve, reject) {
|
|
@@ -8,9 +31,25 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
8
31
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
32
|
});
|
|
10
33
|
};
|
|
34
|
+
var __asyncValues = (this && this.__asyncValues) || function (o) {
|
|
35
|
+
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
|
|
36
|
+
var m = o[Symbol.asyncIterator], i;
|
|
37
|
+
return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
|
|
38
|
+
function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
|
|
39
|
+
function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
|
|
40
|
+
};
|
|
41
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
42
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
43
|
+
};
|
|
11
44
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
45
|
exports.StreamSocket = void 0;
|
|
13
|
-
const
|
|
46
|
+
const ws_1 = __importDefault(require("ws"));
|
|
47
|
+
const uuid_1 = require("uuid");
|
|
48
|
+
const HumeStreamingClient_1 = require("./HumeStreamingClient");
|
|
49
|
+
const base64Encode_1 = require("./base64Encode");
|
|
50
|
+
const serializers = __importStar(require("../serialization"));
|
|
51
|
+
const errors = __importStar(require("../errors"));
|
|
52
|
+
const fs = __importStar(require("fs"));
|
|
14
53
|
class StreamSocket {
|
|
15
54
|
constructor({ websocket, config, streamWindowMs }) {
|
|
16
55
|
this.websocket = websocket;
|
|
@@ -18,33 +57,62 @@ class StreamSocket {
|
|
|
18
57
|
this.streamWindowMs = streamWindowMs;
|
|
19
58
|
}
|
|
20
59
|
/**
|
|
21
|
-
* Send
|
|
60
|
+
* Send file on the `StreamSocket`
|
|
22
61
|
*
|
|
23
|
-
* @param
|
|
24
|
-
* @param config
|
|
25
|
-
*
|
|
26
|
-
* set when initializing the `StreamSocket`
|
|
62
|
+
* @param file A fs.ReadStream | File | Blob
|
|
63
|
+
* @param config This method is intended for use with a `LanguageConfig`.
|
|
64
|
+
* When the socket is configured for other modalities this method will fail.
|
|
27
65
|
*/
|
|
28
|
-
sendFile({
|
|
66
|
+
sendFile({ file, config, }) {
|
|
67
|
+
var file_1, file_1_1;
|
|
68
|
+
var e_1, _a;
|
|
29
69
|
return __awaiter(this, void 0, void 0, function* () {
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
70
|
+
if (config != null) {
|
|
71
|
+
this.config = config;
|
|
72
|
+
}
|
|
73
|
+
let contents = "";
|
|
74
|
+
if (file instanceof fs.ReadStream) {
|
|
75
|
+
const chunks = [];
|
|
76
|
+
try {
|
|
77
|
+
for (file_1 = __asyncValues(file); file_1_1 = yield file_1.next(), !file_1_1.done;) {
|
|
78
|
+
const chunk = file_1_1.value;
|
|
79
|
+
chunks.push(Buffer.from(chunk));
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
83
|
+
finally {
|
|
84
|
+
try {
|
|
85
|
+
if (file_1_1 && !file_1_1.done && (_a = file_1.return)) yield _a.call(file_1);
|
|
86
|
+
}
|
|
87
|
+
finally { if (e_1) throw e_1.error; }
|
|
88
|
+
}
|
|
89
|
+
contents = Buffer.concat(chunks).toString("base64");
|
|
90
|
+
}
|
|
91
|
+
else if (file instanceof Blob) {
|
|
92
|
+
const toBase64 = (file) => new Promise((res, err) => {
|
|
93
|
+
const reader = new FileReader();
|
|
94
|
+
reader.readAsDataURL(file);
|
|
95
|
+
reader.onload = () => res(reader.result);
|
|
96
|
+
});
|
|
97
|
+
contents = yield toBase64(file);
|
|
98
|
+
}
|
|
99
|
+
else {
|
|
100
|
+
throw new errors.HumeError({ message: `file must be one of ReadStream or Blob.` });
|
|
101
|
+
}
|
|
102
|
+
const request = {
|
|
103
|
+
payloadId: (0, uuid_1.v4)(),
|
|
104
|
+
data: contents,
|
|
105
|
+
models: this.config,
|
|
106
|
+
rawText: false,
|
|
107
|
+
};
|
|
108
|
+
if (this.streamWindowMs != null) {
|
|
109
|
+
request.streamWindowMs = this.streamWindowMs;
|
|
110
|
+
}
|
|
111
|
+
const response = yield this.send(request);
|
|
112
|
+
if (response == null) {
|
|
113
|
+
throw new errors.HumeError({ message: `Received no response after sending file: ${file}` });
|
|
114
|
+
}
|
|
115
|
+
return response;
|
|
48
116
|
});
|
|
49
117
|
}
|
|
50
118
|
/**
|
|
@@ -54,16 +122,26 @@ class StreamSocket {
|
|
|
54
122
|
* @param config This method is intended for use with a `LanguageConfig`.
|
|
55
123
|
* When the socket is configured for other modalities this method will fail.
|
|
56
124
|
*/
|
|
57
|
-
sendText({ text, config
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
125
|
+
sendText({ text, config }) {
|
|
126
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
127
|
+
if (config != null) {
|
|
128
|
+
this.config = config;
|
|
129
|
+
}
|
|
130
|
+
const request = {
|
|
131
|
+
payloadId: (0, uuid_1.v4)(),
|
|
132
|
+
data: text,
|
|
133
|
+
rawText: true,
|
|
134
|
+
models: this.config,
|
|
135
|
+
};
|
|
136
|
+
if (this.streamWindowMs != null) {
|
|
137
|
+
request.streamWindowMs = this.streamWindowMs;
|
|
138
|
+
}
|
|
139
|
+
const response = yield this.send(request);
|
|
140
|
+
if (response == null) {
|
|
141
|
+
throw new errors.HumeError({ message: `Received no response after sending text: ${text}` });
|
|
142
|
+
}
|
|
143
|
+
return response;
|
|
144
|
+
});
|
|
67
145
|
}
|
|
68
146
|
/**
|
|
69
147
|
* Send facemesh landmarks on the `StreamSocket`
|
|
@@ -76,10 +154,12 @@ class StreamSocket {
|
|
|
76
154
|
* If set these configurations will overwrite existing configurations
|
|
77
155
|
*/
|
|
78
156
|
sendFacemesh({ landmarks, config, }) {
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
157
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
158
|
+
const response = this.sendText({
|
|
159
|
+
text: (0, base64Encode_1.base64Encode)(JSON.stringify(landmarks)),
|
|
160
|
+
config,
|
|
161
|
+
});
|
|
162
|
+
return response;
|
|
83
163
|
});
|
|
84
164
|
}
|
|
85
165
|
/**
|
|
@@ -90,9 +170,11 @@ class StreamSocket {
|
|
|
90
170
|
* streaming connection without leaking context across media samples.
|
|
91
171
|
*/
|
|
92
172
|
reset() {
|
|
93
|
-
this
|
|
94
|
-
|
|
95
|
-
|
|
173
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
174
|
+
yield this.send({
|
|
175
|
+
resetStream: true,
|
|
176
|
+
});
|
|
177
|
+
});
|
|
96
178
|
}
|
|
97
179
|
/**
|
|
98
180
|
*
|
|
@@ -100,9 +182,11 @@ class StreamSocket {
|
|
|
100
182
|
*
|
|
101
183
|
*/
|
|
102
184
|
getJobDetails() {
|
|
103
|
-
this
|
|
104
|
-
|
|
105
|
-
|
|
185
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
186
|
+
yield this.send({
|
|
187
|
+
jobDetails: true,
|
|
188
|
+
});
|
|
189
|
+
});
|
|
106
190
|
}
|
|
107
191
|
/**
|
|
108
192
|
* Closes the underlying socket.
|
|
@@ -110,5 +194,48 @@ class StreamSocket {
|
|
|
110
194
|
close() {
|
|
111
195
|
this.websocket.close();
|
|
112
196
|
}
|
|
197
|
+
send(payload) {
|
|
198
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
199
|
+
yield this.tillSocketOpen();
|
|
200
|
+
const jsonPayload = yield serializers.ModelsInput.jsonOrThrow(payload, {
|
|
201
|
+
unrecognizedObjectKeys: "strip",
|
|
202
|
+
});
|
|
203
|
+
this.websocket.send(JSON.stringify(jsonPayload));
|
|
204
|
+
const response = yield new Promise((resolve, reject) => {
|
|
205
|
+
this.websocket.addEventListener("message", (event) => {
|
|
206
|
+
const response = (0, HumeStreamingClient_1.parse)(event.data);
|
|
207
|
+
resolve(response);
|
|
208
|
+
});
|
|
209
|
+
});
|
|
210
|
+
if (response != null && isError(response)) {
|
|
211
|
+
throw new errors.HumeError({ message: `CODE ${response.code}: ${response.error}` });
|
|
212
|
+
}
|
|
213
|
+
if (response != null && isWarning(response)) {
|
|
214
|
+
throw new errors.HumeError({ message: `CODE ${response.code}: ${response.warning}` });
|
|
215
|
+
}
|
|
216
|
+
return response;
|
|
217
|
+
});
|
|
218
|
+
}
|
|
219
|
+
tillSocketOpen() {
|
|
220
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
221
|
+
if (this.websocket.readyState === ws_1.default.OPEN) {
|
|
222
|
+
return this.websocket;
|
|
223
|
+
}
|
|
224
|
+
return new Promise((resolve, reject) => {
|
|
225
|
+
this.websocket.addEventListener("open", () => {
|
|
226
|
+
resolve(this.websocket);
|
|
227
|
+
});
|
|
228
|
+
this.websocket.addEventListener("error", (event) => {
|
|
229
|
+
reject(event);
|
|
230
|
+
});
|
|
231
|
+
});
|
|
232
|
+
});
|
|
233
|
+
}
|
|
113
234
|
}
|
|
114
235
|
exports.StreamSocket = StreamSocket;
|
|
236
|
+
function isError(response) {
|
|
237
|
+
return response.error != null;
|
|
238
|
+
}
|
|
239
|
+
function isWarning(response) {
|
|
240
|
+
return response.warning != null;
|
|
241
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function base64Encode(str: string): string;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.base64Encode = void 0;
|
|
4
|
+
function base64Encode(str) {
|
|
5
|
+
if (typeof Buffer === "function") {
|
|
6
|
+
// Node.js environment
|
|
7
|
+
return Buffer.from(str).toString("base64");
|
|
8
|
+
}
|
|
9
|
+
else if (typeof btoa === "function") {
|
|
10
|
+
// Browser environment
|
|
11
|
+
return btoa(str);
|
|
12
|
+
}
|
|
13
|
+
else {
|
|
14
|
+
throw new Error("Base64 encoding not supported in this environment.");
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
exports.base64Encode = base64Encode;
|
package/package.json
CHANGED
|
@@ -1,23 +1,28 @@
|
|
|
1
1
|
import * as Hume from "../api";
|
|
2
|
-
import * as core from "../core";
|
|
3
2
|
import { StreamSocket } from "./StreamSocket";
|
|
3
|
+
import WebSocket from "ws";
|
|
4
4
|
export declare namespace HumeStreamingClient {
|
|
5
5
|
interface Options {
|
|
6
|
-
apiKey:
|
|
6
|
+
apiKey: string;
|
|
7
7
|
openTimeoutInSeconds?: number;
|
|
8
8
|
}
|
|
9
9
|
interface ConnectArgs {
|
|
10
10
|
config: Hume.ModelConfig;
|
|
11
11
|
streamWindowMs?: number;
|
|
12
|
-
onOpen?: () => void;
|
|
12
|
+
onOpen?: (event: WebSocket.Event) => void;
|
|
13
13
|
onMessage?: (message: Hume.ModelResponse) => void;
|
|
14
14
|
onWarning?: (error: Hume.ModelsWarning) => void;
|
|
15
15
|
onError?: (error: Hume.ModelsError) => void;
|
|
16
|
-
onClose?: () => void;
|
|
16
|
+
onClose?: (event: WebSocket.Event) => void;
|
|
17
17
|
}
|
|
18
18
|
}
|
|
19
19
|
export declare class HumeStreamingClient {
|
|
20
20
|
protected readonly _options: HumeStreamingClient.Options;
|
|
21
21
|
constructor(_options: HumeStreamingClient.Options);
|
|
22
|
-
connect(args: HumeStreamingClient.ConnectArgs):
|
|
22
|
+
connect(args: HumeStreamingClient.ConnectArgs): StreamSocket;
|
|
23
23
|
}
|
|
24
|
+
export declare function parse(data: WebSocket.Data, args?: {
|
|
25
|
+
onMessage?: (message: Hume.ModelResponse) => void;
|
|
26
|
+
onWarning?: (error: Hume.ModelsWarning) => void;
|
|
27
|
+
onError?: (error: Hume.ModelsError) => void;
|
|
28
|
+
}): Promise<Hume.ModelResponse | Hume.ModelsWarning | Hume.ModelsError | undefined>;
|
|
@@ -35,8 +35,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
35
35
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
36
36
|
};
|
|
37
37
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
38
|
-
exports.HumeStreamingClient = void 0;
|
|
39
|
-
const core = __importStar(require("../core"));
|
|
38
|
+
exports.parse = exports.HumeStreamingClient = void 0;
|
|
40
39
|
const serializers = __importStar(require("../serialization"));
|
|
41
40
|
const StreamSocket_1 = require("./StreamSocket");
|
|
42
41
|
const ws_1 = __importDefault(require("ws"));
|
|
@@ -45,81 +44,76 @@ class HumeStreamingClient {
|
|
|
45
44
|
this._options = _options;
|
|
46
45
|
}
|
|
47
46
|
connect(args) {
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
error: e.message,
|
|
64
|
-
});
|
|
65
|
-
});
|
|
66
|
-
websocket.addEventListener("message", ({ data }) => __awaiter(this, void 0, void 0, function* () {
|
|
67
|
-
var _a, _b, _c;
|
|
68
|
-
const body = JSON.parse(data);
|
|
69
|
-
const parsedResponse = yield serializers.ModelResponse.parse(body, {
|
|
70
|
-
unrecognizedObjectKeys: "passthrough",
|
|
71
|
-
allowUnrecognizedUnionMembers: true,
|
|
72
|
-
allowUnrecognizedEnumValues: true,
|
|
73
|
-
breadcrumbsPrefix: ["response"],
|
|
74
|
-
});
|
|
75
|
-
if (parsedResponse.ok) {
|
|
76
|
-
(_a = args.onMessage) === null || _a === void 0 ? void 0 : _a.call(args, parsedResponse.value);
|
|
77
|
-
return;
|
|
78
|
-
}
|
|
79
|
-
const parsedWarning = yield serializers.ModelsWarning.parse(body, {
|
|
80
|
-
unrecognizedObjectKeys: "passthrough",
|
|
81
|
-
allowUnrecognizedUnionMembers: true,
|
|
82
|
-
allowUnrecognizedEnumValues: true,
|
|
83
|
-
breadcrumbsPrefix: ["response"],
|
|
84
|
-
});
|
|
85
|
-
if (parsedWarning.ok) {
|
|
86
|
-
(_b = args.onWarning) === null || _b === void 0 ? void 0 : _b.call(args, parsedWarning.value);
|
|
87
|
-
return;
|
|
88
|
-
}
|
|
89
|
-
const parsedError = yield serializers.ModelsError.parse(body, {
|
|
90
|
-
unrecognizedObjectKeys: "passthrough",
|
|
91
|
-
allowUnrecognizedUnionMembers: true,
|
|
92
|
-
allowUnrecognizedEnumValues: true,
|
|
93
|
-
breadcrumbsPrefix: ["response"],
|
|
94
|
-
});
|
|
95
|
-
if (parsedError.ok) {
|
|
96
|
-
(_c = args.onError) === null || _c === void 0 ? void 0 : _c.call(args, parsedError.value);
|
|
97
|
-
return;
|
|
98
|
-
}
|
|
99
|
-
}));
|
|
100
|
-
websocket.addEventListener("close", () => {
|
|
101
|
-
var _a;
|
|
102
|
-
(_a = args.onClose) === null || _a === void 0 ? void 0 : _a.call(args);
|
|
47
|
+
const websocket = new ws_1.default(`wss://api.hume.ai/v0/stream/models`, {
|
|
48
|
+
headers: {
|
|
49
|
+
"X-Hume-Api-Key": this._options.apiKey,
|
|
50
|
+
},
|
|
51
|
+
timeout: this._options.openTimeoutInSeconds,
|
|
52
|
+
});
|
|
53
|
+
websocket.addEventListener("open", (event) => {
|
|
54
|
+
var _a;
|
|
55
|
+
(_a = args.onOpen) === null || _a === void 0 ? void 0 : _a.call(args, event);
|
|
56
|
+
});
|
|
57
|
+
websocket.addEventListener("error", (e) => {
|
|
58
|
+
var _a;
|
|
59
|
+
(_a = args.onError) === null || _a === void 0 ? void 0 : _a.call(args, {
|
|
60
|
+
code: e.type,
|
|
61
|
+
error: e.message,
|
|
103
62
|
});
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
config: args.config,
|
|
111
|
-
}));
|
|
112
|
-
});
|
|
113
|
-
}
|
|
114
|
-
else {
|
|
115
|
-
resolve(new StreamSocket_1.StreamSocket({
|
|
116
|
-
websocket,
|
|
117
|
-
streamWindowMs: args.streamWindowMs,
|
|
118
|
-
config: args.config,
|
|
119
|
-
}));
|
|
120
|
-
}
|
|
63
|
+
});
|
|
64
|
+
websocket.addEventListener("message", ({ data }) => __awaiter(this, void 0, void 0, function* () {
|
|
65
|
+
parse(data, {
|
|
66
|
+
onMessage: args.onMessage,
|
|
67
|
+
onWarning: args.onWarning,
|
|
68
|
+
onError: args.onError,
|
|
121
69
|
});
|
|
70
|
+
}));
|
|
71
|
+
websocket.addEventListener("close", (event) => {
|
|
72
|
+
var _a;
|
|
73
|
+
(_a = args.onClose) === null || _a === void 0 ? void 0 : _a.call(args, event);
|
|
74
|
+
});
|
|
75
|
+
return new StreamSocket_1.StreamSocket({
|
|
76
|
+
websocket,
|
|
77
|
+
streamWindowMs: args.streamWindowMs,
|
|
78
|
+
config: args.config,
|
|
122
79
|
});
|
|
123
80
|
}
|
|
124
81
|
}
|
|
125
82
|
exports.HumeStreamingClient = HumeStreamingClient;
|
|
83
|
+
function parse(data, args = {}) {
|
|
84
|
+
var _a, _b, _c;
|
|
85
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
86
|
+
const message = JSON.parse(data);
|
|
87
|
+
const parsedResponse = yield serializers.ModelResponse.parse(message, {
|
|
88
|
+
unrecognizedObjectKeys: "passthrough",
|
|
89
|
+
allowUnrecognizedUnionMembers: true,
|
|
90
|
+
allowUnrecognizedEnumValues: true,
|
|
91
|
+
breadcrumbsPrefix: ["response"],
|
|
92
|
+
});
|
|
93
|
+
if (parsedResponse.ok) {
|
|
94
|
+
(_a = args.onMessage) === null || _a === void 0 ? void 0 : _a.call(args, parsedResponse.value);
|
|
95
|
+
return parsedResponse.value;
|
|
96
|
+
}
|
|
97
|
+
const parsedWarning = yield serializers.ModelsWarning.parse(message, {
|
|
98
|
+
unrecognizedObjectKeys: "passthrough",
|
|
99
|
+
allowUnrecognizedUnionMembers: true,
|
|
100
|
+
allowUnrecognizedEnumValues: true,
|
|
101
|
+
breadcrumbsPrefix: ["response"],
|
|
102
|
+
});
|
|
103
|
+
if (parsedWarning.ok) {
|
|
104
|
+
(_b = args.onWarning) === null || _b === void 0 ? void 0 : _b.call(args, parsedWarning.value);
|
|
105
|
+
return parsedWarning.value;
|
|
106
|
+
}
|
|
107
|
+
const parsedError = yield serializers.ModelsError.parse(message, {
|
|
108
|
+
unrecognizedObjectKeys: "passthrough",
|
|
109
|
+
allowUnrecognizedUnionMembers: true,
|
|
110
|
+
allowUnrecognizedEnumValues: true,
|
|
111
|
+
breadcrumbsPrefix: ["response"],
|
|
112
|
+
});
|
|
113
|
+
if (parsedError.ok) {
|
|
114
|
+
(_c = args.onError) === null || _c === void 0 ? void 0 : _c.call(args, parsedError.value);
|
|
115
|
+
return parsedError.value;
|
|
116
|
+
}
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
exports.parse = parse;
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
2
|
import WebSocket from "ws";
|
|
3
3
|
import * as Hume from "../api";
|
|
4
|
+
import * as fs from "fs";
|
|
4
5
|
export declare namespace StreamSocket {
|
|
5
6
|
interface Args {
|
|
6
7
|
websocket: WebSocket;
|
|
@@ -14,29 +15,16 @@ export declare class StreamSocket {
|
|
|
14
15
|
private config;
|
|
15
16
|
constructor({ websocket, config, streamWindowMs }: StreamSocket.Args);
|
|
16
17
|
/**
|
|
17
|
-
* Send
|
|
18
|
+
* Send file on the `StreamSocket`
|
|
18
19
|
*
|
|
19
|
-
* @param
|
|
20
|
-
* @param config
|
|
21
|
-
*
|
|
22
|
-
* set when initializing the `StreamSocket`
|
|
23
|
-
*/
|
|
24
|
-
sendFile({ filepath, config, }: {
|
|
25
|
-
filepath: string;
|
|
26
|
-
config?: Hume.ModelConfig;
|
|
27
|
-
}): Promise<void>;
|
|
28
|
-
/**
|
|
29
|
-
* Send raw bytes on the `StreamSocket`
|
|
30
|
-
*
|
|
31
|
-
* @param buffer Raw bytes of media to send on socket connection
|
|
32
|
-
* @param config List of model configurations.
|
|
33
|
-
* If set these configurations will overwrite any configurations
|
|
34
|
-
* set when initializing the `StreamSocket`
|
|
20
|
+
* @param file A fs.ReadStream | File | Blob
|
|
21
|
+
* @param config This method is intended for use with a `LanguageConfig`.
|
|
22
|
+
* When the socket is configured for other modalities this method will fail.
|
|
35
23
|
*/
|
|
36
|
-
|
|
37
|
-
|
|
24
|
+
sendFile({ file, config, }: {
|
|
25
|
+
file: fs.ReadStream | Blob;
|
|
38
26
|
config?: Hume.ModelConfig;
|
|
39
|
-
}):
|
|
27
|
+
}): Promise<Hume.ModelResponse>;
|
|
40
28
|
/**
|
|
41
29
|
* Send text on the `StreamSocket`
|
|
42
30
|
*
|
|
@@ -44,10 +32,10 @@ export declare class StreamSocket {
|
|
|
44
32
|
* @param config This method is intended for use with a `LanguageConfig`.
|
|
45
33
|
* When the socket is configured for other modalities this method will fail.
|
|
46
34
|
*/
|
|
47
|
-
sendText({ text, config
|
|
35
|
+
sendText({ text, config }: {
|
|
48
36
|
text: string;
|
|
49
37
|
config?: Hume.ModelConfig;
|
|
50
|
-
}):
|
|
38
|
+
}): Promise<Hume.ModelResponse>;
|
|
51
39
|
/**
|
|
52
40
|
* Send facemesh landmarks on the `StreamSocket`
|
|
53
41
|
*
|
|
@@ -61,7 +49,7 @@ export declare class StreamSocket {
|
|
|
61
49
|
sendFacemesh({ landmarks, config, }: {
|
|
62
50
|
landmarks: number[][][];
|
|
63
51
|
config?: Hume.ModelConfig;
|
|
64
|
-
}):
|
|
52
|
+
}): Promise<Hume.ModelResponse>;
|
|
65
53
|
/**
|
|
66
54
|
*
|
|
67
55
|
* Reset the streaming sliding window.
|
|
@@ -69,15 +57,17 @@ export declare class StreamSocket {
|
|
|
69
57
|
* Call this method when some media has been fully processed and you want to continue using the same
|
|
70
58
|
* streaming connection without leaking context across media samples.
|
|
71
59
|
*/
|
|
72
|
-
reset(): void
|
|
60
|
+
reset(): Promise<void>;
|
|
73
61
|
/**
|
|
74
62
|
*
|
|
75
63
|
* Get details associated with the current streaming connection.
|
|
76
64
|
*
|
|
77
65
|
*/
|
|
78
|
-
getJobDetails(): void
|
|
66
|
+
getJobDetails(): Promise<void>;
|
|
79
67
|
/**
|
|
80
68
|
* Closes the underlying socket.
|
|
81
69
|
*/
|
|
82
70
|
close(): void;
|
|
71
|
+
private send;
|
|
72
|
+
private tillSocketOpen;
|
|
83
73
|
}
|
package/wrapper/StreamSocket.js
CHANGED
|
@@ -1,4 +1,27 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
2
25
|
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
26
|
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
27
|
return new (P || (P = Promise))(function (resolve, reject) {
|
|
@@ -8,9 +31,25 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
8
31
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
32
|
});
|
|
10
33
|
};
|
|
34
|
+
var __asyncValues = (this && this.__asyncValues) || function (o) {
|
|
35
|
+
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
|
|
36
|
+
var m = o[Symbol.asyncIterator], i;
|
|
37
|
+
return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
|
|
38
|
+
function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
|
|
39
|
+
function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
|
|
40
|
+
};
|
|
41
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
42
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
43
|
+
};
|
|
11
44
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
45
|
exports.StreamSocket = void 0;
|
|
13
|
-
const
|
|
46
|
+
const ws_1 = __importDefault(require("ws"));
|
|
47
|
+
const uuid_1 = require("uuid");
|
|
48
|
+
const HumeStreamingClient_1 = require("./HumeStreamingClient");
|
|
49
|
+
const base64Encode_1 = require("./base64Encode");
|
|
50
|
+
const serializers = __importStar(require("../serialization"));
|
|
51
|
+
const errors = __importStar(require("../errors"));
|
|
52
|
+
const fs = __importStar(require("fs"));
|
|
14
53
|
class StreamSocket {
|
|
15
54
|
constructor({ websocket, config, streamWindowMs }) {
|
|
16
55
|
this.websocket = websocket;
|
|
@@ -18,33 +57,62 @@ class StreamSocket {
|
|
|
18
57
|
this.streamWindowMs = streamWindowMs;
|
|
19
58
|
}
|
|
20
59
|
/**
|
|
21
|
-
* Send
|
|
60
|
+
* Send file on the `StreamSocket`
|
|
22
61
|
*
|
|
23
|
-
* @param
|
|
24
|
-
* @param config
|
|
25
|
-
*
|
|
26
|
-
* set when initializing the `StreamSocket`
|
|
62
|
+
* @param file A fs.ReadStream | File | Blob
|
|
63
|
+
* @param config This method is intended for use with a `LanguageConfig`.
|
|
64
|
+
* When the socket is configured for other modalities this method will fail.
|
|
27
65
|
*/
|
|
28
|
-
sendFile({
|
|
66
|
+
sendFile({ file, config, }) {
|
|
67
|
+
var file_1, file_1_1;
|
|
68
|
+
var e_1, _a;
|
|
29
69
|
return __awaiter(this, void 0, void 0, function* () {
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
70
|
+
if (config != null) {
|
|
71
|
+
this.config = config;
|
|
72
|
+
}
|
|
73
|
+
let contents = "";
|
|
74
|
+
if (file instanceof fs.ReadStream) {
|
|
75
|
+
const chunks = [];
|
|
76
|
+
try {
|
|
77
|
+
for (file_1 = __asyncValues(file); file_1_1 = yield file_1.next(), !file_1_1.done;) {
|
|
78
|
+
const chunk = file_1_1.value;
|
|
79
|
+
chunks.push(Buffer.from(chunk));
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
83
|
+
finally {
|
|
84
|
+
try {
|
|
85
|
+
if (file_1_1 && !file_1_1.done && (_a = file_1.return)) yield _a.call(file_1);
|
|
86
|
+
}
|
|
87
|
+
finally { if (e_1) throw e_1.error; }
|
|
88
|
+
}
|
|
89
|
+
contents = Buffer.concat(chunks).toString("base64");
|
|
90
|
+
}
|
|
91
|
+
else if (file instanceof Blob) {
|
|
92
|
+
const toBase64 = (file) => new Promise((res, err) => {
|
|
93
|
+
const reader = new FileReader();
|
|
94
|
+
reader.readAsDataURL(file);
|
|
95
|
+
reader.onload = () => res(reader.result);
|
|
96
|
+
});
|
|
97
|
+
contents = yield toBase64(file);
|
|
98
|
+
}
|
|
99
|
+
else {
|
|
100
|
+
throw new errors.HumeError({ message: `file must be one of ReadStream or Blob.` });
|
|
101
|
+
}
|
|
102
|
+
const request = {
|
|
103
|
+
payloadId: (0, uuid_1.v4)(),
|
|
104
|
+
data: contents,
|
|
105
|
+
models: this.config,
|
|
106
|
+
rawText: false,
|
|
107
|
+
};
|
|
108
|
+
if (this.streamWindowMs != null) {
|
|
109
|
+
request.streamWindowMs = this.streamWindowMs;
|
|
110
|
+
}
|
|
111
|
+
const response = yield this.send(request);
|
|
112
|
+
if (response == null) {
|
|
113
|
+
throw new errors.HumeError({ message: `Received no response after sending file: ${file}` });
|
|
114
|
+
}
|
|
115
|
+
return response;
|
|
48
116
|
});
|
|
49
117
|
}
|
|
50
118
|
/**
|
|
@@ -54,16 +122,26 @@ class StreamSocket {
|
|
|
54
122
|
* @param config This method is intended for use with a `LanguageConfig`.
|
|
55
123
|
* When the socket is configured for other modalities this method will fail.
|
|
56
124
|
*/
|
|
57
|
-
sendText({ text, config
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
125
|
+
sendText({ text, config }) {
|
|
126
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
127
|
+
if (config != null) {
|
|
128
|
+
this.config = config;
|
|
129
|
+
}
|
|
130
|
+
const request = {
|
|
131
|
+
payloadId: (0, uuid_1.v4)(),
|
|
132
|
+
data: text,
|
|
133
|
+
rawText: true,
|
|
134
|
+
models: this.config,
|
|
135
|
+
};
|
|
136
|
+
if (this.streamWindowMs != null) {
|
|
137
|
+
request.streamWindowMs = this.streamWindowMs;
|
|
138
|
+
}
|
|
139
|
+
const response = yield this.send(request);
|
|
140
|
+
if (response == null) {
|
|
141
|
+
throw new errors.HumeError({ message: `Received no response after sending text: ${text}` });
|
|
142
|
+
}
|
|
143
|
+
return response;
|
|
144
|
+
});
|
|
67
145
|
}
|
|
68
146
|
/**
|
|
69
147
|
* Send facemesh landmarks on the `StreamSocket`
|
|
@@ -76,10 +154,12 @@ class StreamSocket {
|
|
|
76
154
|
* If set these configurations will overwrite existing configurations
|
|
77
155
|
*/
|
|
78
156
|
sendFacemesh({ landmarks, config, }) {
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
157
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
158
|
+
const response = this.sendText({
|
|
159
|
+
text: (0, base64Encode_1.base64Encode)(JSON.stringify(landmarks)),
|
|
160
|
+
config,
|
|
161
|
+
});
|
|
162
|
+
return response;
|
|
83
163
|
});
|
|
84
164
|
}
|
|
85
165
|
/**
|
|
@@ -90,9 +170,11 @@ class StreamSocket {
|
|
|
90
170
|
* streaming connection without leaking context across media samples.
|
|
91
171
|
*/
|
|
92
172
|
reset() {
|
|
93
|
-
this
|
|
94
|
-
|
|
95
|
-
|
|
173
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
174
|
+
yield this.send({
|
|
175
|
+
resetStream: true,
|
|
176
|
+
});
|
|
177
|
+
});
|
|
96
178
|
}
|
|
97
179
|
/**
|
|
98
180
|
*
|
|
@@ -100,9 +182,11 @@ class StreamSocket {
|
|
|
100
182
|
*
|
|
101
183
|
*/
|
|
102
184
|
getJobDetails() {
|
|
103
|
-
this
|
|
104
|
-
|
|
105
|
-
|
|
185
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
186
|
+
yield this.send({
|
|
187
|
+
jobDetails: true,
|
|
188
|
+
});
|
|
189
|
+
});
|
|
106
190
|
}
|
|
107
191
|
/**
|
|
108
192
|
* Closes the underlying socket.
|
|
@@ -110,5 +194,48 @@ class StreamSocket {
|
|
|
110
194
|
close() {
|
|
111
195
|
this.websocket.close();
|
|
112
196
|
}
|
|
197
|
+
send(payload) {
|
|
198
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
199
|
+
yield this.tillSocketOpen();
|
|
200
|
+
const jsonPayload = yield serializers.ModelsInput.jsonOrThrow(payload, {
|
|
201
|
+
unrecognizedObjectKeys: "strip",
|
|
202
|
+
});
|
|
203
|
+
this.websocket.send(JSON.stringify(jsonPayload));
|
|
204
|
+
const response = yield new Promise((resolve, reject) => {
|
|
205
|
+
this.websocket.addEventListener("message", (event) => {
|
|
206
|
+
const response = (0, HumeStreamingClient_1.parse)(event.data);
|
|
207
|
+
resolve(response);
|
|
208
|
+
});
|
|
209
|
+
});
|
|
210
|
+
if (response != null && isError(response)) {
|
|
211
|
+
throw new errors.HumeError({ message: `CODE ${response.code}: ${response.error}` });
|
|
212
|
+
}
|
|
213
|
+
if (response != null && isWarning(response)) {
|
|
214
|
+
throw new errors.HumeError({ message: `CODE ${response.code}: ${response.warning}` });
|
|
215
|
+
}
|
|
216
|
+
return response;
|
|
217
|
+
});
|
|
218
|
+
}
|
|
219
|
+
tillSocketOpen() {
|
|
220
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
221
|
+
if (this.websocket.readyState === ws_1.default.OPEN) {
|
|
222
|
+
return this.websocket;
|
|
223
|
+
}
|
|
224
|
+
return new Promise((resolve, reject) => {
|
|
225
|
+
this.websocket.addEventListener("open", () => {
|
|
226
|
+
resolve(this.websocket);
|
|
227
|
+
});
|
|
228
|
+
this.websocket.addEventListener("error", (event) => {
|
|
229
|
+
reject(event);
|
|
230
|
+
});
|
|
231
|
+
});
|
|
232
|
+
});
|
|
233
|
+
}
|
|
113
234
|
}
|
|
114
235
|
exports.StreamSocket = StreamSocket;
|
|
236
|
+
function isError(response) {
|
|
237
|
+
return response.error != null;
|
|
238
|
+
}
|
|
239
|
+
function isWarning(response) {
|
|
240
|
+
return response.warning != null;
|
|
241
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function base64Encode(str: string): string;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.base64Encode = void 0;
|
|
4
|
+
function base64Encode(str) {
|
|
5
|
+
if (typeof Buffer === "function") {
|
|
6
|
+
// Node.js environment
|
|
7
|
+
return Buffer.from(str).toString("base64");
|
|
8
|
+
}
|
|
9
|
+
else if (typeof btoa === "function") {
|
|
10
|
+
// Browser environment
|
|
11
|
+
return btoa(str);
|
|
12
|
+
}
|
|
13
|
+
else {
|
|
14
|
+
throw new Error("Base64 encoding not supported in this environment.");
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
exports.base64Encode = base64Encode;
|