1
1
import * as core from "../../../../../../core" ;
2
+ import * as errors from "../../../../../../errors" ;
2
3
import * as Hume from "../../../../../index" ;
3
4
import * as serializers from "../../../../../../serialization/index" ;
4
- import { MaybeValid } from "core/schemas/Schema" ;
5
5
6
6
export declare namespace ChatSocket {
7
7
interface Args {
@@ -54,6 +54,7 @@ export class ChatSocket{
54
54
* Send audio input
55
55
*/
56
56
public sendAudioInput ( message : Omit < Hume . empathicVoice . AudioInput , "type" > ) : void {
57
+ this . assertSocketIsOpen ( ) ;
57
58
this . sendJson ( {
58
59
type : "audio_input" ,
59
60
...message ,
@@ -64,6 +65,7 @@ export class ChatSocket{
64
65
* Send session settings
65
66
*/
66
67
public sendSessionSettings ( message : Omit < Hume . empathicVoice . SessionSettings , "type" > ) : void {
68
+ this . assertSocketIsOpen ( ) ;
67
69
this . sendJson ( {
68
70
type : "session_settings" ,
69
71
...message ,
@@ -74,6 +76,7 @@ export class ChatSocket{
74
76
* Send assistant input
75
77
*/
76
78
public sendAssistantInput ( message : Omit < Hume . empathicVoice . AssistantInput , "type" > ) : void {
79
+ this . assertSocketIsOpen ( ) ;
77
80
this . sendJson ( {
78
81
type : "assistant_input" ,
79
82
...message ,
@@ -84,6 +87,7 @@ export class ChatSocket{
84
87
* Send pause assistant message
85
88
*/
86
89
public pauseAssistant ( message : Omit < Hume . empathicVoice . PauseAssistantMessage , "type" > ) : void {
90
+ this . assertSocketIsOpen ( ) ;
87
91
this . sendJson ( {
88
92
type : "pause_assistant_message" ,
89
93
...message ,
@@ -94,6 +98,7 @@ export class ChatSocket{
94
98
* Send resume assistant message
95
99
*/
96
100
public resumeAssistant ( message : Omit < Hume . empathicVoice . ResumeAssistantMessage , "type" > ) : void {
101
+ this . assertSocketIsOpen ( ) ;
97
102
this . sendJson ( {
98
103
type : "resume_assistant_message" ,
99
104
...message ,
@@ -104,6 +109,7 @@ export class ChatSocket{
104
109
* Send tool response message
105
110
*/
106
111
public sendToolResponseMessage ( message : Omit < Hume . empathicVoice . ToolResponseMessage , "type" > ) : void {
112
+ this . assertSocketIsOpen ( ) ;
107
113
this . sendJson ( {
108
114
type : "tool_response" ,
109
115
...message ,
@@ -114,6 +120,7 @@ export class ChatSocket{
114
120
* Send tool error message
115
121
*/
116
122
public sendToolErrorMessage ( message : Omit < Hume . empathicVoice . ToolErrorMessage , "type" > ) : void {
123
+ this . assertSocketIsOpen ( ) ;
117
124
this . sendJson ( {
118
125
type : "tool_error" ,
119
126
...message ,
@@ -124,24 +131,53 @@ export class ChatSocket{
124
131
* Send text input
125
132
*/
126
133
public sendUserInput ( text : string ) : void {
134
+ this . assertSocketIsOpen ( ) ;
127
135
this . sendJson ( {
128
136
type : "user_input" ,
129
137
text,
130
138
} ) ;
131
139
}
132
140
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
+
133
157
/**
134
158
* Closes the underlying socket.
135
159
*/
136
160
public close ( ) : void {
137
161
this . socket . close ( ) ;
138
162
163
+ this . handleClose ( { code : 1000 } as CloseEvent ) ;
164
+
139
165
this . socket . removeEventListener ( 'open' , this . handleOpen ) ;
140
166
this . socket . removeEventListener ( 'message' , this . handleMessage ) ;
141
167
this . socket . removeEventListener ( 'close' , this . handleClose ) ;
142
168
this . socket . removeEventListener ( 'error' , this . handleError ) ;
143
169
}
144
170
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
+
145
181
private sendJson ( payload : Hume . empathicVoice . PublishEvent ) : void {
146
182
const jsonPayload = serializers . empathicVoice . PublishEvent . jsonOrThrow ( payload , {
147
183
unrecognizedObjectKeys : "strip" ,
0 commit comments