phonic 0.30.0-rc3 → 0.30.0-rc4

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.
@@ -18,6 +18,7 @@ export declare namespace ConversationsSocket {
18
18
  export declare class ConversationsSocket {
19
19
  readonly socket: core.ReconnectingWebSocket;
20
20
  protected readonly eventHandlers: ConversationsSocket.EventHandlers;
21
+ private messageBuffer;
21
22
  private handleOpen;
22
23
  private handleMessage;
23
24
  private handleClose;
@@ -50,6 +51,12 @@ export declare class ConversationsSocket {
50
51
  waitForOpen(): Promise<core.ReconnectingWebSocket>;
51
52
  /** Asserts that the websocket is open. */
52
53
  private assertSocketIsOpen;
54
+ /** Send a message with buffering - if socket is not open, buffer the message. */
55
+ private sendWithBuffering;
56
+ /** Flush all buffered messages when the socket opens. */
57
+ private flushMessageBuffer;
58
+ /** Clear all buffered messages. */
59
+ private clearMessageBuffer;
53
60
  /** Send a binary payload to the websocket. */
54
61
  protected sendBinary(payload: ArrayBufferLike | Blob | ArrayBufferView): void;
55
62
  /** Send a JSON payload to the websocket. */
@@ -51,8 +51,10 @@ const json_js_1 = require("../../../../core/json.js");
51
51
  class ConversationsSocket {
52
52
  constructor(args) {
53
53
  this.eventHandlers = {};
54
+ this.messageBuffer = [];
54
55
  this.handleOpen = () => {
55
56
  var _a, _b;
57
+ this.flushMessageBuffer();
56
58
  (_b = (_a = this.eventHandlers).open) === null || _b === void 0 ? void 0 : _b.call(_a);
57
59
  };
58
60
  this.handleMessage = (event) => {
@@ -93,28 +95,22 @@ class ConversationsSocket {
93
95
  this.eventHandlers[event] = callback;
94
96
  }
95
97
  sendConfig(message) {
96
- this.assertSocketIsOpen();
97
- this.sendJson(message);
98
+ this.sendWithBuffering(() => this.sendJson(message));
98
99
  }
99
100
  sendAudioChunk(message) {
100
- this.assertSocketIsOpen();
101
- this.sendJson(message);
101
+ this.sendWithBuffering(() => this.sendJson(message));
102
102
  }
103
103
  sendUpdateSystemPrompt(message) {
104
- this.assertSocketIsOpen();
105
- this.sendJson(message);
104
+ this.sendWithBuffering(() => this.sendJson(message));
106
105
  }
107
106
  sendSetExternalId(message) {
108
- this.assertSocketIsOpen();
109
- this.sendJson(message);
107
+ this.sendWithBuffering(() => this.sendJson(message));
110
108
  }
111
109
  sendSetTwilioCallSid(message) {
112
- this.assertSocketIsOpen();
113
- this.sendJson(message);
110
+ this.sendWithBuffering(() => this.sendJson(message));
114
111
  }
115
112
  sendToolCallOutput(message) {
116
- this.assertSocketIsOpen();
117
- this.sendJson(message);
113
+ this.sendWithBuffering(() => this.sendJson(message));
118
114
  }
119
115
  /** Connect to the websocket and register event handlers. */
120
116
  connect() {
@@ -129,6 +125,7 @@ class ConversationsSocket {
129
125
  close() {
130
126
  this.socket.close();
131
127
  this.handleClose({ code: 1000 });
128
+ this.clearMessageBuffer();
132
129
  this.socket.removeEventListener("open", this.handleOpen);
133
130
  this.socket.removeEventListener("message", this.handleMessage);
134
131
  this.socket.removeEventListener("close", this.handleClose);
@@ -159,6 +156,33 @@ class ConversationsSocket {
159
156
  throw new Error("Socket is not open.");
160
157
  }
161
158
  }
159
+ /** Send a message with buffering - if socket is not open, buffer the message. */
160
+ sendWithBuffering(sendFn) {
161
+ if (this.socket.readyState === core.ReconnectingWebSocket.OPEN) {
162
+ sendFn();
163
+ }
164
+ else {
165
+ this.messageBuffer.push(sendFn);
166
+ }
167
+ }
168
+ /** Flush all buffered messages when the socket opens. */
169
+ flushMessageBuffer() {
170
+ while (this.messageBuffer.length > 0) {
171
+ const sendFn = this.messageBuffer.shift();
172
+ if (sendFn) {
173
+ try {
174
+ sendFn();
175
+ }
176
+ catch (error) {
177
+ console.error("Error sending buffered message:", error);
178
+ }
179
+ }
180
+ }
181
+ }
182
+ /** Clear all buffered messages. */
183
+ clearMessageBuffer() {
184
+ this.messageBuffer = [];
185
+ }
162
186
  /** Send a binary payload to the websocket. */
163
187
  sendBinary(payload) {
164
188
  this.socket.send(payload);
@@ -18,6 +18,7 @@ export declare namespace ConversationsSocket {
18
18
  export declare class ConversationsSocket {
19
19
  readonly socket: core.ReconnectingWebSocket;
20
20
  protected readonly eventHandlers: ConversationsSocket.EventHandlers;
21
+ private messageBuffer;
21
22
  private handleOpen;
22
23
  private handleMessage;
23
24
  private handleClose;
@@ -50,6 +51,12 @@ export declare class ConversationsSocket {
50
51
  waitForOpen(): Promise<core.ReconnectingWebSocket>;
51
52
  /** Asserts that the websocket is open. */
52
53
  private assertSocketIsOpen;
54
+ /** Send a message with buffering - if socket is not open, buffer the message. */
55
+ private sendWithBuffering;
56
+ /** Flush all buffered messages when the socket opens. */
57
+ private flushMessageBuffer;
58
+ /** Clear all buffered messages. */
59
+ private clearMessageBuffer;
53
60
  /** Send a binary payload to the websocket. */
54
61
  protected sendBinary(payload: ArrayBufferLike | Blob | ArrayBufferView): void;
55
62
  /** Send a JSON payload to the websocket. */
@@ -15,8 +15,10 @@ import { fromJson, toJson } from "../../../../core/json.mjs";
15
15
  export class ConversationsSocket {
16
16
  constructor(args) {
17
17
  this.eventHandlers = {};
18
+ this.messageBuffer = [];
18
19
  this.handleOpen = () => {
19
20
  var _a, _b;
21
+ this.flushMessageBuffer();
20
22
  (_b = (_a = this.eventHandlers).open) === null || _b === void 0 ? void 0 : _b.call(_a);
21
23
  };
22
24
  this.handleMessage = (event) => {
@@ -57,28 +59,22 @@ export class ConversationsSocket {
57
59
  this.eventHandlers[event] = callback;
58
60
  }
59
61
  sendConfig(message) {
60
- this.assertSocketIsOpen();
61
- this.sendJson(message);
62
+ this.sendWithBuffering(() => this.sendJson(message));
62
63
  }
63
64
  sendAudioChunk(message) {
64
- this.assertSocketIsOpen();
65
- this.sendJson(message);
65
+ this.sendWithBuffering(() => this.sendJson(message));
66
66
  }
67
67
  sendUpdateSystemPrompt(message) {
68
- this.assertSocketIsOpen();
69
- this.sendJson(message);
68
+ this.sendWithBuffering(() => this.sendJson(message));
70
69
  }
71
70
  sendSetExternalId(message) {
72
- this.assertSocketIsOpen();
73
- this.sendJson(message);
71
+ this.sendWithBuffering(() => this.sendJson(message));
74
72
  }
75
73
  sendSetTwilioCallSid(message) {
76
- this.assertSocketIsOpen();
77
- this.sendJson(message);
74
+ this.sendWithBuffering(() => this.sendJson(message));
78
75
  }
79
76
  sendToolCallOutput(message) {
80
- this.assertSocketIsOpen();
81
- this.sendJson(message);
77
+ this.sendWithBuffering(() => this.sendJson(message));
82
78
  }
83
79
  /** Connect to the websocket and register event handlers. */
84
80
  connect() {
@@ -93,6 +89,7 @@ export class ConversationsSocket {
93
89
  close() {
94
90
  this.socket.close();
95
91
  this.handleClose({ code: 1000 });
92
+ this.clearMessageBuffer();
96
93
  this.socket.removeEventListener("open", this.handleOpen);
97
94
  this.socket.removeEventListener("message", this.handleMessage);
98
95
  this.socket.removeEventListener("close", this.handleClose);
@@ -123,6 +120,33 @@ export class ConversationsSocket {
123
120
  throw new Error("Socket is not open.");
124
121
  }
125
122
  }
123
+ /** Send a message with buffering - if socket is not open, buffer the message. */
124
+ sendWithBuffering(sendFn) {
125
+ if (this.socket.readyState === core.ReconnectingWebSocket.OPEN) {
126
+ sendFn();
127
+ }
128
+ else {
129
+ this.messageBuffer.push(sendFn);
130
+ }
131
+ }
132
+ /** Flush all buffered messages when the socket opens. */
133
+ flushMessageBuffer() {
134
+ while (this.messageBuffer.length > 0) {
135
+ const sendFn = this.messageBuffer.shift();
136
+ if (sendFn) {
137
+ try {
138
+ sendFn();
139
+ }
140
+ catch (error) {
141
+ console.error("Error sending buffered message:", error);
142
+ }
143
+ }
144
+ }
145
+ }
146
+ /** Clear all buffered messages. */
147
+ clearMessageBuffer() {
148
+ this.messageBuffer = [];
149
+ }
126
150
  /** Send a binary payload to the websocket. */
127
151
  sendBinary(payload) {
128
152
  this.socket.send(payload);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "phonic",
3
- "version": "0.30.0-rc3",
3
+ "version": "0.30.0-rc4",
4
4
  "private": false,
5
5
  "repository": "github:fern-demo/phonic-ts-sdk",
6
6
  "type": "commonjs",