Skip to content

Commit 0e09fbf

Browse files
authored
(feat): support debug, reconnectAttempts, and arbitrary query param EVI args (#57)
1 parent 3726cd2 commit 0e09fbf

File tree

3 files changed

+58
-3
lines changed

3 files changed

+58
-3
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-beta5",
3+
"version": "0.8.1-beta6",
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

+20-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ export declare namespace Chat {
99
}
1010

1111
interface ConnectArgs {
12+
/** Enable debug mode on the websocket. Defaults to false. */
13+
debug?: boolean;
14+
15+
/** Number of reconnect attempts. Defaults to 30. */
16+
reconnectAttempts?: number;
17+
1218
/** The ID of the configuration. */
1319
configId?: string;
1420

@@ -17,6 +23,9 @@ export declare namespace Chat {
1723

1824
/** The ID of a chat group, used to resume a previous chat. */
1925
resumedChatGroupId?: string;
26+
27+
/** Extra query parameters sent at WebSocket connection */
28+
queryParams?: Record<string, string | string[] | object | object[]>;
2029
}
2130
}
2231

@@ -41,7 +50,17 @@ export class Chat {
4150
queryParams["resumed_chat_group_id"] = args.resumedChatGroupId;
4251
}
4352

44-
const socket = new core.ReconnectingWebSocket(`wss://api.hume.ai/v0/evi/chat?${qs.stringify(queryParams)}`);
53+
if (args.queryParams != null) {
54+
for (const [name, value] of Object.entries(args.queryParams)) {
55+
queryParams[name] = value;
56+
}
57+
}
58+
59+
const socket = new core.ReconnectingWebSocket(`wss://api.hume.ai/v0/evi/chat?${qs.stringify(queryParams)}`, [], {
60+
startClosed: true,
61+
debug: args.debug ?? false,
62+
maxRetries: args.reconnectAttempts,
63+
});
4564

4665
return new ChatSocket({
4766
socket,

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

+37-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as core from "../../../../../../core";
2+
import * as errors from "../../../../../../errors";
23
import * as Hume from "../../../../../index";
34
import * as serializers from "../../../../../../serialization/index";
4-
import { MaybeValid } from "core/schemas/Schema";
55

66
export declare namespace ChatSocket {
77
interface Args {
@@ -54,6 +54,7 @@ export class ChatSocket{
5454
* Send audio input
5555
*/
5656
public sendAudioInput(message: Omit<Hume.empathicVoice.AudioInput, "type">): void {
57+
this.assertSocketIsOpen();
5758
this.sendJson({
5859
type: "audio_input",
5960
...message,
@@ -64,6 +65,7 @@ export class ChatSocket{
6465
* Send session settings
6566
*/
6667
public sendSessionSettings(message: Omit<Hume.empathicVoice.SessionSettings, "type">): void {
68+
this.assertSocketIsOpen();
6769
this.sendJson({
6870
type: "session_settings",
6971
...message,
@@ -74,6 +76,7 @@ export class ChatSocket{
7476
* Send assistant input
7577
*/
7678
public sendAssistantInput(message: Omit<Hume.empathicVoice.AssistantInput, "type">): void {
79+
this.assertSocketIsOpen();
7780
this.sendJson({
7881
type: "assistant_input",
7982
...message,
@@ -84,6 +87,7 @@ export class ChatSocket{
8487
* Send pause assistant message
8588
*/
8689
public pauseAssistant(message: Omit<Hume.empathicVoice.PauseAssistantMessage, "type">): void {
90+
this.assertSocketIsOpen();
8791
this.sendJson({
8892
type: "pause_assistant_message",
8993
...message,
@@ -94,6 +98,7 @@ export class ChatSocket{
9498
* Send resume assistant message
9599
*/
96100
public resumeAssistant(message: Omit<Hume.empathicVoice.ResumeAssistantMessage, "type">): void {
101+
this.assertSocketIsOpen();
97102
this.sendJson({
98103
type: "resume_assistant_message",
99104
...message,
@@ -104,6 +109,7 @@ export class ChatSocket{
104109
* Send tool response message
105110
*/
106111
public sendToolResponseMessage(message: Omit<Hume.empathicVoice.ToolResponseMessage, "type">): void {
112+
this.assertSocketIsOpen();
107113
this.sendJson({
108114
type: "tool_response",
109115
...message,
@@ -114,6 +120,7 @@ export class ChatSocket{
114120
* Send tool error message
115121
*/
116122
public sendToolErrorMessage(message: Omit<Hume.empathicVoice.ToolErrorMessage, "type">): void {
123+
this.assertSocketIsOpen();
117124
this.sendJson({
118125
type: "tool_error",
119126
...message,
@@ -124,24 +131,53 @@ export class ChatSocket{
124131
* Send text input
125132
*/
126133
public sendUserInput(text: string): void {
134+
this.assertSocketIsOpen();
127135
this.sendJson({
128136
type: "user_input",
129137
text,
130138
});
131139
}
132140

141+
/**
142+
* @name connect
143+
* @description
144+
* Connect to the websocket.
145+
*/
146+
public connect(): ChatSocket {
147+
this.socket.reconnect();
148+
149+
this.socket.addEventListener('open', this.handleOpen);
150+
this.socket.addEventListener('message', this.handleMessage);
151+
this.socket.addEventListener('close', this.handleClose);
152+
this.socket.addEventListener('error', this.handleError);
153+
154+
return this;
155+
}
156+
133157
/**
134158
* Closes the underlying socket.
135159
*/
136160
public close(): void {
137161
this.socket.close();
138162

163+
this.handleClose({ code: 1000 } as CloseEvent);
164+
139165
this.socket.removeEventListener('open', this.handleOpen);
140166
this.socket.removeEventListener('message', this.handleMessage);
141167
this.socket.removeEventListener('close', this.handleClose);
142168
this.socket.removeEventListener('error', this.handleError);
143169
}
144170

171+
private assertSocketIsOpen(): void {
172+
if (!this.socket) {
173+
throw new errors.HumeError({ message: 'Socket is not connected.'});
174+
}
175+
176+
if (this.socket.readyState !== WebSocket.OPEN) {
177+
throw new errors.HumeError({ message: 'Socket is not open.' });
178+
}
179+
}
180+
145181
private sendJson(payload: Hume.empathicVoice.PublishEvent): void {
146182
const jsonPayload = serializers.empathicVoice.PublishEvent.jsonOrThrow(payload, {
147183
unrecognizedObjectKeys: "strip",

0 commit comments

Comments
 (0)