agent.libx.js 0.93.1 → 0.93.2

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.
package/dist/index.d.ts CHANGED
@@ -967,7 +967,12 @@ declare class CartesiaTTS {
967
967
  onDone: () => void;
968
968
  firstAudioAt: number;
969
969
  constructor(options?: Partial<CartesiaTTSOptions>);
970
+ private closed;
971
+ private connecting;
970
972
  connect(): Promise<void>;
973
+ private doConnect;
974
+ /** Ensure the WS is open before sending — reconnects if idle-closed. */
975
+ private ensureConnected;
971
976
  newContext(): string;
972
977
  private frame;
973
978
  speak(text: string, cont: boolean): void;
package/dist/index.js CHANGED
@@ -4629,7 +4629,15 @@ var CartesiaTTS = class {
4629
4629
  constructor(options) {
4630
4630
  this.options = { ...new CartesiaTTSOptions(), ...options };
4631
4631
  }
4632
+ closed = false;
4633
+ connecting = null;
4632
4634
  async connect() {
4635
+ this.closed = false;
4636
+ this.connecting = this.doConnect();
4637
+ await this.connecting;
4638
+ this.connecting = null;
4639
+ }
4640
+ async doConnect() {
4633
4641
  const key = await resolveAuth(this.options.auth);
4634
4642
  const param = this.options.authMode === "token" ? "access_token" : "api_key";
4635
4643
  this.ws = new WebSocket(`wss://api.cartesia.ai/tts/websocket?cartesia_version=2026-03-01&${param}=${key}`);
@@ -4637,7 +4645,12 @@ var CartesiaTTS = class {
4637
4645
  this.ws.onopen = () => res();
4638
4646
  this.ws.onerror = (e) => rej(new Error(`cartesia ws: ${e.message || "connect failed"}`));
4639
4647
  });
4640
- this.ws.onclose = (ev) => log10.warn(`cartesia ws closed (${ev.code} ${ev.reason || ""})`);
4648
+ this.ws.onclose = (ev) => {
4649
+ log10.warn(`cartesia ws closed (${ev.code} ${ev.reason || ""})`);
4650
+ if (!this.closed) {
4651
+ this.connecting = this.doConnect().catch((e) => log10.error(`cartesia reconnect failed: ${e.message}`));
4652
+ }
4653
+ };
4641
4654
  this.ws.onmessage = (ev) => {
4642
4655
  const m = JSON.parse(String(ev.data));
4643
4656
  if (m.context_id && m.context_id !== this.ctxId) return;
@@ -4648,6 +4661,11 @@ var CartesiaTTS = class {
4648
4661
  else if (m.type === "error" && !/already been cancelled|does not exist/.test(m.message || "")) log10.warn(`cartesia: ${JSON.stringify(m)}`);
4649
4662
  };
4650
4663
  }
4664
+ /** Ensure the WS is open before sending — reconnects if idle-closed. */
4665
+ async ensureConnected() {
4666
+ if (this.connecting) await this.connecting;
4667
+ if (this.ws?.readyState !== WebSocket.OPEN) await this.connect();
4668
+ }
4651
4669
  newContext() {
4652
4670
  this.ctxId = `ctx-${++this.ctxSeq}`;
4653
4671
  this.firstAudioAt = 0;
@@ -4665,6 +4683,7 @@ var CartesiaTTS = class {
4665
4683
  }
4666
4684
  speak(text, cont) {
4667
4685
  if (this.ws?.readyState === WebSocket.OPEN) this.ws.send(this.frame(text, cont));
4686
+ else void this.ensureConnected().then(() => this.ws?.readyState === WebSocket.OPEN && this.ws.send(this.frame(text, cont)));
4668
4687
  }
4669
4688
  end() {
4670
4689
  if (this.ws?.readyState === WebSocket.OPEN) this.ws.send(this.frame("", false));
@@ -4673,6 +4692,7 @@ var CartesiaTTS = class {
4673
4692
  if (this.ws?.readyState === WebSocket.OPEN) this.ws.send(JSON.stringify({ context_id: this.ctxId, cancel: true }));
4674
4693
  }
4675
4694
  close() {
4695
+ this.closed = true;
4676
4696
  if (this.ws) this.ws.onclose = null;
4677
4697
  this.ws?.close();
4678
4698
  }