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/cli.js CHANGED
@@ -4434,7 +4434,15 @@ var CartesiaTTS = class {
4434
4434
  constructor(options) {
4435
4435
  this.options = { ...new CartesiaTTSOptions(), ...options };
4436
4436
  }
4437
+ closed = false;
4438
+ connecting = null;
4437
4439
  async connect() {
4440
+ this.closed = false;
4441
+ this.connecting = this.doConnect();
4442
+ await this.connecting;
4443
+ this.connecting = null;
4444
+ }
4445
+ async doConnect() {
4438
4446
  const key = await resolveAuth(this.options.auth);
4439
4447
  const param = this.options.authMode === "token" ? "access_token" : "api_key";
4440
4448
  this.ws = new WebSocket(`wss://api.cartesia.ai/tts/websocket?cartesia_version=2026-03-01&${param}=${key}`);
@@ -4442,7 +4450,12 @@ var CartesiaTTS = class {
4442
4450
  this.ws.onopen = () => res();
4443
4451
  this.ws.onerror = (e) => rej(new Error(`cartesia ws: ${e.message || "connect failed"}`));
4444
4452
  });
4445
- this.ws.onclose = (ev) => log9.warn(`cartesia ws closed (${ev.code} ${ev.reason || ""})`);
4453
+ this.ws.onclose = (ev) => {
4454
+ log9.warn(`cartesia ws closed (${ev.code} ${ev.reason || ""})`);
4455
+ if (!this.closed) {
4456
+ this.connecting = this.doConnect().catch((e) => log9.error(`cartesia reconnect failed: ${e.message}`));
4457
+ }
4458
+ };
4446
4459
  this.ws.onmessage = (ev) => {
4447
4460
  const m = JSON.parse(String(ev.data));
4448
4461
  if (m.context_id && m.context_id !== this.ctxId) return;
@@ -4453,6 +4466,11 @@ var CartesiaTTS = class {
4453
4466
  else if (m.type === "error" && !/already been cancelled|does not exist/.test(m.message || "")) log9.warn(`cartesia: ${JSON.stringify(m)}`);
4454
4467
  };
4455
4468
  }
4469
+ /** Ensure the WS is open before sending — reconnects if idle-closed. */
4470
+ async ensureConnected() {
4471
+ if (this.connecting) await this.connecting;
4472
+ if (this.ws?.readyState !== WebSocket.OPEN) await this.connect();
4473
+ }
4456
4474
  newContext() {
4457
4475
  this.ctxId = `ctx-${++this.ctxSeq}`;
4458
4476
  this.firstAudioAt = 0;
@@ -4470,6 +4488,7 @@ var CartesiaTTS = class {
4470
4488
  }
4471
4489
  speak(text, cont) {
4472
4490
  if (this.ws?.readyState === WebSocket.OPEN) this.ws.send(this.frame(text, cont));
4491
+ else void this.ensureConnected().then(() => this.ws?.readyState === WebSocket.OPEN && this.ws.send(this.frame(text, cont)));
4473
4492
  }
4474
4493
  end() {
4475
4494
  if (this.ws?.readyState === WebSocket.OPEN) this.ws.send(this.frame("", false));
@@ -4478,6 +4497,7 @@ var CartesiaTTS = class {
4478
4497
  if (this.ws?.readyState === WebSocket.OPEN) this.ws.send(JSON.stringify({ context_id: this.ctxId, cancel: true }));
4479
4498
  }
4480
4499
  close() {
4500
+ this.closed = true;
4481
4501
  if (this.ws) this.ws.onclose = null;
4482
4502
  this.ws?.close();
4483
4503
  }