Skip to content

Commit db1b050

Browse files
authored
(feat): export tillSocketOpen on ChatSocket (#60)
1 parent 1434f0e commit db1b050

File tree

5 files changed

+34
-16
lines changed

5 files changed

+34
-16
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "hume",
3-
"version": "0.8.1-beta8",
3+
"version": "0.8.1-beta9",
44
"private": false,
55
"repository": "https://github.com/HumeAI/hume-typescript-sdk",
66
"main": "./index.js",

src/api/resources/empathicVoice/resources/chat/client/Client.ts

+3-4
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ export class Chat {
3636
const queryParams: Record<string, string | string[] | object | object[]> = {};
3737

3838
if (this._options.accessToken != null) {
39-
queryParams["accessToken"] = core.Supplier.get(this._options.accessToken);
39+
queryParams["accessToken"] = this._options.accessToken;
4040
} else if (this._options.apiKey != null) {
41-
queryParams["apiKey"] = core.Supplier.get(this._options.apiKey);
41+
queryParams["apiKey"] = this._options.apiKey;
4242
}
4343
if (args.configId != null) {
4444
queryParams["config_id"] = args.configId;
@@ -57,9 +57,8 @@ export class Chat {
5757
}
5858

5959
const socket = new core.ReconnectingWebSocket(`wss://api.hume.ai/v0/evi/chat?${qs.stringify(queryParams)}`, [], {
60-
startClosed: true,
6160
debug: args.debug ?? false,
62-
maxRetries: args.reconnectAttempts,
61+
maxRetries: args.reconnectAttempts ?? 30,
6362
});
6463

6564
return new ChatSocket({

src/api/resources/empathicVoice/resources/chat/client/Socket.ts

+18-3
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ export class ChatSocket{
141141
/**
142142
* @name connect
143143
* @description
144-
* Connect to the websocket.
144+
* Connect to the core.ReconnectingWebSocket.
145145
*/
146146
public connect(): ChatSocket {
147147
this.socket.reconnect();
@@ -168,12 +168,27 @@ export class ChatSocket{
168168
this.socket.removeEventListener('error', this.handleError);
169169
}
170170

171+
public async tillSocketOpen(): Promise<core.ReconnectingWebSocket> {
172+
if (this.socket.readyState === core.ReconnectingWebSocket.OPEN) {
173+
return this.socket;
174+
}
175+
return new Promise((resolve, reject) => {
176+
this.socket.addEventListener("open", () => {
177+
resolve(this.socket);
178+
});
179+
180+
this.socket.addEventListener("error", (event: any) => {
181+
reject(event);
182+
});
183+
});
184+
}
185+
171186
private assertSocketIsOpen(): void {
172187
if (!this.socket) {
173188
throw new errors.HumeError({ message: 'Socket is not connected.'});
174189
}
175190

176-
if (this.socket.readyState !== WebSocket.OPEN) {
191+
if (this.socket.readyState !== core.ReconnectingWebSocket.OPEN) {
177192
throw new errors.HumeError({ message: 'Socket is not open.' });
178193
}
179194
}
@@ -210,7 +225,7 @@ export class ChatSocket{
210225
};
211226

212227
private handleError = (event: core.ErrorEvent) => {
213-
const message = event.message ?? 'WebSocket error';
228+
const message = event.message ?? 'core.ReconnectingWebSocket error';
214229
this.eventHandlers.error?.(new Error(message));
215230
};
216231
}

src/core/websocket/ws.ts

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
import * as Events from './events';
2+
import { WebSocket as NodeWebSocket } from 'ws';
23

34
const getGlobalWebSocket = (): WebSocket | undefined => {
4-
if (typeof WebSocket !== 'undefined') {
5-
// @ts-ignore
6-
return WebSocket;
7-
}
5+
return (global as any).WebSocket ??= NodeWebSocket;
86
};
97

108
/**

tests/empathicVoice/chat.test.ts

+10-4
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,24 @@ import { HumeClient } from "../../src/";
33
describe("Empathic Voice Interface", () => {
44
it.skip("Chat", async () => {
55
const hume = new HumeClient({
6-
apiKey: "<>",
7-
secretKey: "<>",
6+
accessToken: '<>'
87
});
98

10-
const socket = hume.empathicVoice.chat.connect();
9+
const socket = hume.empathicVoice.chat.connect({
10+
debug: true,
11+
reconnectAttempts: 30,
12+
});
13+
14+
await socket.tillSocketOpen();
1115

1216
socket.on("message", (message) => {
1317
if (message.type === "audio_output") {
1418
Buffer.from(message.data, "base64");
1519
}
1620
});
1721

18-
await socket.sendUserInput("Hello, how are you?");
22+
socket.sendUserInput("Hello, how are you?");
23+
24+
1925
}, 100000);
2026
});

0 commit comments

Comments
 (0)