Skip to content

Commit

Permalink
(feat): support debug, reconnectAttempts, and arbitrary query par…
Browse files Browse the repository at this point in the history
…am EVI args (#57)
  • Loading branch information
dsinghvi authored Jul 12, 2024
1 parent 3726cd2 commit 0e09fbf
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "hume",
"version": "0.8.1-beta5",
"version": "0.8.1-beta6",
"private": false,
"repository": "https://github.com/HumeAI/hume-typescript-sdk",
"main": "./index.js",
Expand Down
21 changes: 20 additions & 1 deletion src/api/resources/empathicVoice/resources/chat/client/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ export declare namespace Chat {
}

interface ConnectArgs {
/** Enable debug mode on the websocket. Defaults to false. */
debug?: boolean;

/** Number of reconnect attempts. Defaults to 30. */
reconnectAttempts?: number;

/** The ID of the configuration. */
configId?: string;

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

/** The ID of a chat group, used to resume a previous chat. */
resumedChatGroupId?: string;

/** Extra query parameters sent at WebSocket connection */
queryParams?: Record<string, string | string[] | object | object[]>;
}
}

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

const socket = new core.ReconnectingWebSocket(`wss://api.hume.ai/v0/evi/chat?${qs.stringify(queryParams)}`);
if (args.queryParams != null) {
for (const [name, value] of Object.entries(args.queryParams)) {
queryParams[name] = value;
}
}

const socket = new core.ReconnectingWebSocket(`wss://api.hume.ai/v0/evi/chat?${qs.stringify(queryParams)}`, [], {
startClosed: true,
debug: args.debug ?? false,
maxRetries: args.reconnectAttempts,
});

return new ChatSocket({
socket,
Expand Down
38 changes: 37 additions & 1 deletion src/api/resources/empathicVoice/resources/chat/client/Socket.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as core from "../../../../../../core";
import * as errors from "../../../../../../errors";
import * as Hume from "../../../../../index";
import * as serializers from "../../../../../../serialization/index";
import { MaybeValid } from "core/schemas/Schema";

export declare namespace ChatSocket {
interface Args {
Expand Down Expand Up @@ -54,6 +54,7 @@ export class ChatSocket{
* Send audio input
*/
public sendAudioInput(message: Omit<Hume.empathicVoice.AudioInput, "type">): void {
this.assertSocketIsOpen();
this.sendJson({
type: "audio_input",
...message,
Expand All @@ -64,6 +65,7 @@ export class ChatSocket{
* Send session settings
*/
public sendSessionSettings(message: Omit<Hume.empathicVoice.SessionSettings, "type">): void {
this.assertSocketIsOpen();
this.sendJson({
type: "session_settings",
...message,
Expand All @@ -74,6 +76,7 @@ export class ChatSocket{
* Send assistant input
*/
public sendAssistantInput(message: Omit<Hume.empathicVoice.AssistantInput, "type">): void {
this.assertSocketIsOpen();
this.sendJson({
type: "assistant_input",
...message,
Expand All @@ -84,6 +87,7 @@ export class ChatSocket{
* Send pause assistant message
*/
public pauseAssistant(message: Omit<Hume.empathicVoice.PauseAssistantMessage, "type">): void {
this.assertSocketIsOpen();
this.sendJson({
type: "pause_assistant_message",
...message,
Expand All @@ -94,6 +98,7 @@ export class ChatSocket{
* Send resume assistant message
*/
public resumeAssistant(message: Omit<Hume.empathicVoice.ResumeAssistantMessage, "type">): void {
this.assertSocketIsOpen();
this.sendJson({
type: "resume_assistant_message",
...message,
Expand All @@ -104,6 +109,7 @@ export class ChatSocket{
* Send tool response message
*/
public sendToolResponseMessage(message: Omit<Hume.empathicVoice.ToolResponseMessage, "type">): void {
this.assertSocketIsOpen();
this.sendJson({
type: "tool_response",
...message,
Expand All @@ -114,6 +120,7 @@ export class ChatSocket{
* Send tool error message
*/
public sendToolErrorMessage(message: Omit<Hume.empathicVoice.ToolErrorMessage, "type">): void {
this.assertSocketIsOpen();
this.sendJson({
type: "tool_error",
...message,
Expand All @@ -124,24 +131,53 @@ export class ChatSocket{
* Send text input
*/
public sendUserInput(text: string): void {
this.assertSocketIsOpen();
this.sendJson({
type: "user_input",
text,
});
}

/**
* @name connect
* @description
* Connect to the websocket.
*/
public connect(): ChatSocket {
this.socket.reconnect();

this.socket.addEventListener('open', this.handleOpen);
this.socket.addEventListener('message', this.handleMessage);
this.socket.addEventListener('close', this.handleClose);
this.socket.addEventListener('error', this.handleError);

return this;
}

/**
* Closes the underlying socket.
*/
public close(): void {
this.socket.close();

this.handleClose({ code: 1000 } as CloseEvent);

this.socket.removeEventListener('open', this.handleOpen);
this.socket.removeEventListener('message', this.handleMessage);
this.socket.removeEventListener('close', this.handleClose);
this.socket.removeEventListener('error', this.handleError);
}

private assertSocketIsOpen(): void {
if (!this.socket) {
throw new errors.HumeError({ message: 'Socket is not connected.'});
}

if (this.socket.readyState !== WebSocket.OPEN) {
throw new errors.HumeError({ message: 'Socket is not open.' });
}
}

private sendJson(payload: Hume.empathicVoice.PublishEvent): void {
const jsonPayload = serializers.empathicVoice.PublishEvent.jsonOrThrow(payload, {
unrecognizedObjectKeys: "strip",
Expand Down

0 comments on commit 0e09fbf

Please sign in to comment.