@cartesia/cartesia-js 1.2.2-alpha.0 → 1.3.0

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.
Files changed (45) hide show
  1. package/README.md +6 -2
  2. package/dist/{chunk-HQOC2S6C.js → chunk-CJZSHIRI.js} +28 -18
  3. package/dist/{chunk-EYRYHK3X.js → chunk-QT5LHJ3D.js} +1 -1
  4. package/dist/{chunk-62LXGS65.js → chunk-RJICGVPL.js} +0 -37
  5. package/dist/{chunk-MGVQHO25.js → chunk-RYHHRY64.js} +4 -4
  6. package/dist/index.cjs +21 -48
  7. package/dist/index.d.cts +2 -1
  8. package/dist/index.d.ts +2 -1
  9. package/dist/index.js +11 -11
  10. package/dist/lib/client.d.cts +1 -0
  11. package/dist/lib/client.d.ts +1 -0
  12. package/dist/lib/index.cjs +21 -48
  13. package/dist/lib/index.d.cts +1 -0
  14. package/dist/lib/index.d.ts +1 -0
  15. package/dist/lib/index.js +6 -6
  16. package/dist/react/index.cjs +21 -48
  17. package/dist/react/index.d.cts +1 -0
  18. package/dist/react/index.d.ts +1 -0
  19. package/dist/react/index.js +10 -10
  20. package/dist/tts/index.cjs +21 -11
  21. package/dist/tts/index.d.cts +1 -0
  22. package/dist/tts/index.d.ts +1 -0
  23. package/dist/tts/index.js +4 -4
  24. package/dist/tts/player.d.cts +1 -0
  25. package/dist/tts/player.d.ts +1 -0
  26. package/dist/tts/source.d.cts +1 -0
  27. package/dist/tts/source.d.ts +1 -0
  28. package/dist/tts/utils.d.cts +1 -0
  29. package/dist/tts/utils.d.ts +1 -0
  30. package/dist/tts/websocket.cjs +21 -11
  31. package/dist/tts/websocket.d.cts +3 -2
  32. package/dist/tts/websocket.d.ts +3 -2
  33. package/dist/tts/websocket.js +3 -3
  34. package/dist/types/index.d.cts +3 -9
  35. package/dist/types/index.d.ts +3 -9
  36. package/dist/voice-changer/index.d.cts +1 -0
  37. package/dist/voice-changer/index.d.ts +1 -0
  38. package/dist/voices/index.cjs +0 -37
  39. package/dist/voices/index.d.cts +2 -2
  40. package/dist/voices/index.d.ts +2 -2
  41. package/dist/voices/index.js +1 -1
  42. package/package.json +1 -1
  43. package/src/tts/websocket.ts +21 -9
  44. package/src/types/index.ts +3 -9
  45. package/src/voices/index.ts +0 -40
@@ -448,7 +448,8 @@ var WebSocket = class extends Client {
448
448
  streamCompleteController.abort();
449
449
  },
450
450
  {
451
- once: true
451
+ once: true,
452
+ signal: streamCompleteController.signal
452
453
  }
453
454
  );
454
455
  (_d = this.socket) == null ? void 0 : _d.addEventListener(
@@ -457,7 +458,8 @@ var WebSocket = class extends Client {
457
458
  streamCompleteController.abort();
458
459
  },
459
460
  {
460
- once: true
461
+ once: true,
462
+ signal: streamCompleteController.signal
461
463
  }
462
464
  );
463
465
  streamCompleteController.signal.addEventListener("abort", () => {
@@ -465,6 +467,7 @@ var WebSocket = class extends Client {
465
467
  if (timeoutId) {
466
468
  clearTimeout(timeoutId);
467
469
  }
470
+ emitter.clearListeners();
468
471
  });
469
472
  return __spreadProps(__spreadValues({
470
473
  source
@@ -505,16 +508,23 @@ var WebSocket = class extends Client {
505
508
  * @throws {Error} If the WebSocket fails to connect.
506
509
  */
507
510
  connect() {
508
- return __async(this, null, function* () {
511
+ return __async(this, arguments, function* (options = {}) {
512
+ if (__privateGet(this, _isConnected)) {
513
+ throw new Error("WebSocket is already connected.");
514
+ }
509
515
  const emitter = new import_emittery2.default();
510
- this.socket = new import_partysocket.WebSocket(() => __async(this, null, function* () {
511
- const url = constructApiUrl(this.baseUrl, "/tts/websocket", {
512
- websocket: true
513
- });
514
- url.searchParams.set("api_key", yield this.apiKey());
515
- url.searchParams.set("cartesia_version", CARTESIA_VERSION);
516
- return url.toString();
517
- }));
516
+ this.socket = new import_partysocket.WebSocket(
517
+ () => __async(this, null, function* () {
518
+ const url = constructApiUrl(this.baseUrl, "/tts/websocket", {
519
+ websocket: true
520
+ });
521
+ url.searchParams.set("api_key", yield this.apiKey());
522
+ url.searchParams.set("cartesia_version", CARTESIA_VERSION);
523
+ return url.toString();
524
+ }),
525
+ void 0,
526
+ options
527
+ );
518
528
  this.socket.binaryType = "arraybuffer";
519
529
  this.socket.onopen = () => {
520
530
  __privateSet(this, _isConnected, true);
@@ -2,6 +2,7 @@ import { Client } from '../lib/client.cjs';
2
2
  import { WebSocketOptions, BytesRequest } from '../types/index.cjs';
3
3
  import WebSocket from './websocket.cjs';
4
4
  import 'emittery';
5
+ import 'partysocket/ws';
5
6
  import 'partysocket';
6
7
  import './source.cjs';
7
8
 
@@ -2,6 +2,7 @@ import { Client } from '../lib/client.js';
2
2
  import { WebSocketOptions, BytesRequest } from '../types/index.js';
3
3
  import WebSocket from './websocket.js';
4
4
  import 'emittery';
5
+ import 'partysocket/ws';
5
6
  import 'partysocket';
6
7
  import './source.js';
7
8
 
package/dist/tts/index.js CHANGED
@@ -1,11 +1,11 @@
1
1
  import {
2
2
  TTS
3
- } from "../chunk-EYRYHK3X.js";
4
- import "../chunk-HQOC2S6C.js";
5
- import "../chunk-5SBAQNWQ.js";
6
- import "../chunk-I5YVYTNK.js";
3
+ } from "../chunk-QT5LHJ3D.js";
4
+ import "../chunk-CJZSHIRI.js";
7
5
  import "../chunk-FLWYXP5Z.js";
8
6
  import "../chunk-2BFEKY3F.js";
7
+ import "../chunk-5SBAQNWQ.js";
8
+ import "../chunk-I5YVYTNK.js";
9
9
  import "../chunk-NJDRWDQ3.js";
10
10
  export {
11
11
  TTS as default
@@ -1,6 +1,7 @@
1
1
  import Source from './source.cjs';
2
2
  import 'emittery';
3
3
  import '../types/index.cjs';
4
+ import 'partysocket/ws';
4
5
 
5
6
  declare class Player {
6
7
  #private;
@@ -1,6 +1,7 @@
1
1
  import Source from './source.js';
2
2
  import 'emittery';
3
3
  import '../types/index.js';
4
+ import 'partysocket/ws';
4
5
 
5
6
  declare class Player {
6
7
  #private;
@@ -1,5 +1,6 @@
1
1
  import * as emittery from 'emittery';
2
2
  import { Encoding, SourceEventData, TypedArray } from '../types/index.cjs';
3
+ import 'partysocket/ws';
3
4
 
4
5
  type EncodingInfo = {
5
6
  arrayType: Float32ArrayConstructor | Int16ArrayConstructor | Uint8ArrayConstructor;
@@ -1,5 +1,6 @@
1
1
  import * as emittery from 'emittery';
2
2
  import { Encoding, SourceEventData, TypedArray } from '../types/index.js';
3
+ import 'partysocket/ws';
3
4
 
4
5
  type EncodingInfo = {
5
6
  arrayType: Float32ArrayConstructor | Int16ArrayConstructor | Uint8ArrayConstructor;
@@ -1,5 +1,6 @@
1
1
  import emittery__default from 'emittery';
2
2
  import { Chunk, TypedArray, WebSocketResponse, Sentinel, EmitteryCallbacks } from '../types/index.cjs';
3
+ import 'partysocket/ws';
3
4
 
4
5
  /**
5
6
  * Convert base64-encoded audio buffer(s) to a TypedArray.
@@ -1,5 +1,6 @@
1
1
  import emittery__default from 'emittery';
2
2
  import { Chunk, TypedArray, WebSocketResponse, Sentinel, EmitteryCallbacks } from '../types/index.js';
3
+ import 'partysocket/ws';
3
4
 
4
5
  /**
5
6
  * Convert base64-encoded audio buffer(s) to a TypedArray.
@@ -446,7 +446,8 @@ var WebSocket = class extends Client {
446
446
  streamCompleteController.abort();
447
447
  },
448
448
  {
449
- once: true
449
+ once: true,
450
+ signal: streamCompleteController.signal
450
451
  }
451
452
  );
452
453
  (_d = this.socket) == null ? void 0 : _d.addEventListener(
@@ -455,7 +456,8 @@ var WebSocket = class extends Client {
455
456
  streamCompleteController.abort();
456
457
  },
457
458
  {
458
- once: true
459
+ once: true,
460
+ signal: streamCompleteController.signal
459
461
  }
460
462
  );
461
463
  streamCompleteController.signal.addEventListener("abort", () => {
@@ -463,6 +465,7 @@ var WebSocket = class extends Client {
463
465
  if (timeoutId) {
464
466
  clearTimeout(timeoutId);
465
467
  }
468
+ emitter.clearListeners();
466
469
  });
467
470
  return __spreadProps(__spreadValues({
468
471
  source
@@ -503,16 +506,23 @@ var WebSocket = class extends Client {
503
506
  * @throws {Error} If the WebSocket fails to connect.
504
507
  */
505
508
  connect() {
506
- return __async(this, null, function* () {
509
+ return __async(this, arguments, function* (options = {}) {
510
+ if (__privateGet(this, _isConnected)) {
511
+ throw new Error("WebSocket is already connected.");
512
+ }
507
513
  const emitter = new import_emittery2.default();
508
- this.socket = new import_partysocket.WebSocket(() => __async(this, null, function* () {
509
- const url = constructApiUrl(this.baseUrl, "/tts/websocket", {
510
- websocket: true
511
- });
512
- url.searchParams.set("api_key", yield this.apiKey());
513
- url.searchParams.set("cartesia_version", CARTESIA_VERSION);
514
- return url.toString();
515
- }));
514
+ this.socket = new import_partysocket.WebSocket(
515
+ () => __async(this, null, function* () {
516
+ const url = constructApiUrl(this.baseUrl, "/tts/websocket", {
517
+ websocket: true
518
+ });
519
+ url.searchParams.set("api_key", yield this.apiKey());
520
+ url.searchParams.set("cartesia_version", CARTESIA_VERSION);
521
+ return url.toString();
522
+ }),
523
+ void 0,
524
+ options
525
+ );
516
526
  this.socket.binaryType = "arraybuffer";
517
527
  this.socket.onopen = () => {
518
528
  __privateSet(this, _isConnected, true);
@@ -1,8 +1,9 @@
1
1
  import * as emittery from 'emittery';
2
2
  import { WebSocket as WebSocket$1 } from 'partysocket';
3
3
  import { Client } from '../lib/client.cjs';
4
- import { WebSocketOptions, StreamRequest, StreamOptions, WordTimestamps, ContinueRequest, EmitteryCallbacks, ConnectionEventData } from '../types/index.cjs';
4
+ import { WebSocketOptions, StreamRequest, StreamOptions, WordTimestamps, ContinueRequest, ConnectOptions, EmitteryCallbacks, ConnectionEventData } from '../types/index.cjs';
5
5
  import Source from './source.cjs';
6
+ import 'partysocket/ws';
6
7
 
7
8
  declare class WebSocket extends Client {
8
9
  #private;
@@ -59,7 +60,7 @@ declare class WebSocket extends Client {
59
60
  * @returns A promise that resolves when the WebSocket is connected.
60
61
  * @throws {Error} If the WebSocket fails to connect.
61
62
  */
62
- connect(): Promise<EmitteryCallbacks<ConnectionEventData>>;
63
+ connect(options?: ConnectOptions): Promise<EmitteryCallbacks<ConnectionEventData>>;
63
64
  /**
64
65
  * Disconnect from the Cartesia streaming WebSocket.
65
66
  */
@@ -1,8 +1,9 @@
1
1
  import * as emittery from 'emittery';
2
2
  import { WebSocket as WebSocket$1 } from 'partysocket';
3
3
  import { Client } from '../lib/client.js';
4
- import { WebSocketOptions, StreamRequest, StreamOptions, WordTimestamps, ContinueRequest, EmitteryCallbacks, ConnectionEventData } from '../types/index.js';
4
+ import { WebSocketOptions, StreamRequest, StreamOptions, WordTimestamps, ContinueRequest, ConnectOptions, EmitteryCallbacks, ConnectionEventData } from '../types/index.js';
5
5
  import Source from './source.js';
6
+ import 'partysocket/ws';
6
7
 
7
8
  declare class WebSocket extends Client {
8
9
  #private;
@@ -59,7 +60,7 @@ declare class WebSocket extends Client {
59
60
  * @returns A promise that resolves when the WebSocket is connected.
60
61
  * @throws {Error} If the WebSocket fails to connect.
61
62
  */
62
- connect(): Promise<EmitteryCallbacks<ConnectionEventData>>;
63
+ connect(options?: ConnectOptions): Promise<EmitteryCallbacks<ConnectionEventData>>;
63
64
  /**
64
65
  * Disconnect from the Cartesia streaming WebSocket.
65
66
  */
@@ -1,10 +1,10 @@
1
1
  import {
2
2
  WebSocket
3
- } from "../chunk-HQOC2S6C.js";
4
- import "../chunk-5SBAQNWQ.js";
5
- import "../chunk-I5YVYTNK.js";
3
+ } from "../chunk-CJZSHIRI.js";
6
4
  import "../chunk-FLWYXP5Z.js";
7
5
  import "../chunk-2BFEKY3F.js";
6
+ import "../chunk-5SBAQNWQ.js";
7
+ import "../chunk-I5YVYTNK.js";
8
8
  import "../chunk-NJDRWDQ3.js";
9
9
  export {
10
10
  WebSocket as default
@@ -1,4 +1,5 @@
1
1
  import emittery__default from 'emittery';
2
+ import { Options } from 'partysocket/ws';
2
3
 
3
4
  interface ClientOptions {
4
5
  apiKey?: string | (() => Promise<string>);
@@ -88,14 +89,6 @@ type CloneOptions = {
88
89
  clip: Blob;
89
90
  enhance?: boolean;
90
91
  };
91
- type HifiCloneOptions = CloneOptions & {
92
- name: string;
93
- description: string;
94
- language: Language;
95
- model_id: string;
96
- transcript?: string;
97
- base_voice_id?: string;
98
- };
99
92
  type VoiceChangerOptions = {
100
93
  clip: File;
101
94
  voice: {
@@ -161,6 +154,7 @@ type WebSocketOptions = {
161
154
  encoding?: string;
162
155
  sampleRate: number;
163
156
  };
157
+ type ConnectOptions = Pick<Options, "WebSocket">;
164
158
  type SourceEventData = {
165
159
  enqueue: never;
166
160
  close: never;
@@ -170,4 +164,4 @@ type SourceEventData = {
170
164
  type TypedArray = Float32Array | Int16Array | Uint8Array;
171
165
  type Encoding = "pcm_f32le" | "pcm_s16le" | "pcm_alaw" | "pcm_mulaw";
172
166
 
173
- export type { BytesRequest, Chunk, ClientOptions, CloneOptions, CloneResponse, ConnectionEventData, ContinueRequest, CreateVoice, EmitteryCallbacks, Emotion, EmotionControl, Encoding, HifiCloneOptions, Intensity, Language, LocalizeOptions, LocalizeResponse, MixVoicesOptions, MixVoicesResponse, Sentinel, SourceEventData, StreamOptions, StreamRequest, TypedArray, UpdateVoice, Voice, VoiceChangerBytesResponse, VoiceChangerOptions, VoiceOptions, VoiceSpecifier, VoiceToMix, WebSocketBaseResponse, WebSocketChunkResponse, WebSocketErrorResponse, WebSocketOptions, WebSocketResponse, WebSocketTimestampsResponse, WordTimestamps };
167
+ export type { BytesRequest, Chunk, ClientOptions, CloneOptions, CloneResponse, ConnectOptions, ConnectionEventData, ContinueRequest, CreateVoice, EmitteryCallbacks, Emotion, EmotionControl, Encoding, Intensity, Language, LocalizeOptions, LocalizeResponse, MixVoicesOptions, MixVoicesResponse, Sentinel, SourceEventData, StreamOptions, StreamRequest, TypedArray, UpdateVoice, Voice, VoiceChangerBytesResponse, VoiceChangerOptions, VoiceOptions, VoiceSpecifier, VoiceToMix, WebSocketBaseResponse, WebSocketChunkResponse, WebSocketErrorResponse, WebSocketOptions, WebSocketResponse, WebSocketTimestampsResponse, WordTimestamps };
@@ -1,4 +1,5 @@
1
1
  import emittery__default from 'emittery';
2
+ import { Options } from 'partysocket/ws';
2
3
 
3
4
  interface ClientOptions {
4
5
  apiKey?: string | (() => Promise<string>);
@@ -88,14 +89,6 @@ type CloneOptions = {
88
89
  clip: Blob;
89
90
  enhance?: boolean;
90
91
  };
91
- type HifiCloneOptions = CloneOptions & {
92
- name: string;
93
- description: string;
94
- language: Language;
95
- model_id: string;
96
- transcript?: string;
97
- base_voice_id?: string;
98
- };
99
92
  type VoiceChangerOptions = {
100
93
  clip: File;
101
94
  voice: {
@@ -161,6 +154,7 @@ type WebSocketOptions = {
161
154
  encoding?: string;
162
155
  sampleRate: number;
163
156
  };
157
+ type ConnectOptions = Pick<Options, "WebSocket">;
164
158
  type SourceEventData = {
165
159
  enqueue: never;
166
160
  close: never;
@@ -170,4 +164,4 @@ type SourceEventData = {
170
164
  type TypedArray = Float32Array | Int16Array | Uint8Array;
171
165
  type Encoding = "pcm_f32le" | "pcm_s16le" | "pcm_alaw" | "pcm_mulaw";
172
166
 
173
- export type { BytesRequest, Chunk, ClientOptions, CloneOptions, CloneResponse, ConnectionEventData, ContinueRequest, CreateVoice, EmitteryCallbacks, Emotion, EmotionControl, Encoding, HifiCloneOptions, Intensity, Language, LocalizeOptions, LocalizeResponse, MixVoicesOptions, MixVoicesResponse, Sentinel, SourceEventData, StreamOptions, StreamRequest, TypedArray, UpdateVoice, Voice, VoiceChangerBytesResponse, VoiceChangerOptions, VoiceOptions, VoiceSpecifier, VoiceToMix, WebSocketBaseResponse, WebSocketChunkResponse, WebSocketErrorResponse, WebSocketOptions, WebSocketResponse, WebSocketTimestampsResponse, WordTimestamps };
167
+ export type { BytesRequest, Chunk, ClientOptions, CloneOptions, CloneResponse, ConnectOptions, ConnectionEventData, ContinueRequest, CreateVoice, EmitteryCallbacks, Emotion, EmotionControl, Encoding, Intensity, Language, LocalizeOptions, LocalizeResponse, MixVoicesOptions, MixVoicesResponse, Sentinel, SourceEventData, StreamOptions, StreamRequest, TypedArray, UpdateVoice, Voice, VoiceChangerBytesResponse, VoiceChangerOptions, VoiceOptions, VoiceSpecifier, VoiceToMix, WebSocketBaseResponse, WebSocketChunkResponse, WebSocketErrorResponse, WebSocketOptions, WebSocketResponse, WebSocketTimestampsResponse, WordTimestamps };
@@ -1,6 +1,7 @@
1
1
  import { Client } from '../lib/client.cjs';
2
2
  import { VoiceChangerOptions, VoiceChangerBytesResponse } from '../types/index.cjs';
3
3
  import 'emittery';
4
+ import 'partysocket/ws';
4
5
 
5
6
  declare class VoiceChanger extends Client {
6
7
  bytes(options: VoiceChangerOptions): Promise<VoiceChangerBytesResponse>;
@@ -1,6 +1,7 @@
1
1
  import { Client } from '../lib/client.js';
2
2
  import { VoiceChangerOptions, VoiceChangerBytesResponse } from '../types/index.js';
3
3
  import 'emittery';
4
+ import 'partysocket/ws';
4
5
 
5
6
  declare class VoiceChanger extends Client {
6
7
  bytes(options: VoiceChangerOptions): Promise<VoiceChangerBytesResponse>;
@@ -159,43 +159,6 @@ var Voices = class extends Client {
159
159
  throw new Error("Invalid mode for clone()");
160
160
  });
161
161
  }
162
- hifiClone(options) {
163
- return __async(this, null, function* () {
164
- var _a;
165
- if (options.mode !== "clip") {
166
- throw new Error("Invalid mode for hifiClone()");
167
- }
168
- const formData = new FormData();
169
- formData.append("clip", options.clip);
170
- formData.append("name", options.name);
171
- formData.append("description", options.description);
172
- formData.append("language", options.language);
173
- formData.append("model_id", options.model_id);
174
- if (options.enhance !== void 0) {
175
- formData.append("enhance", options.enhance.toString());
176
- }
177
- if (options.transcript) {
178
- formData.append("transcript", options.transcript);
179
- }
180
- if (options.base_voice_id) {
181
- formData.append("base_voice_id", options.base_voice_id);
182
- }
183
- const response = yield this._fetch("/voices/clone/hifi", {
184
- method: "POST",
185
- body: formData
186
- });
187
- if (!response.ok) {
188
- if ((_a = response.headers.get("content-type")) == null ? void 0 : _a.includes("application/json")) {
189
- const errorData = yield response.json();
190
- throw new Error(errorData.message || "HiFi clone failed");
191
- }
192
- const errorText = yield response.text();
193
- throw new Error(errorText || "HiFi clone failed");
194
- }
195
- const data = yield response.json();
196
- return data;
197
- });
198
- }
199
162
  mix(options) {
200
163
  return __async(this, null, function* () {
201
164
  const response = yield this._fetch("/voices/mix", {
@@ -1,6 +1,7 @@
1
1
  import { Client } from '../lib/client.cjs';
2
- import { Voice, CreateVoice, UpdateVoice, CloneOptions, CloneResponse, HifiCloneOptions, MixVoicesOptions, MixVoicesResponse, LocalizeOptions, LocalizeResponse } from '../types/index.cjs';
2
+ import { Voice, CreateVoice, UpdateVoice, CloneOptions, CloneResponse, MixVoicesOptions, MixVoicesResponse, LocalizeOptions, LocalizeResponse } from '../types/index.cjs';
3
3
  import 'emittery';
4
+ import 'partysocket/ws';
4
5
 
5
6
  declare class Voices extends Client {
6
7
  list(): Promise<Voice[]>;
@@ -8,7 +9,6 @@ declare class Voices extends Client {
8
9
  create(voice: CreateVoice): Promise<Voice>;
9
10
  update(id: string, voice: UpdateVoice): Promise<Voice>;
10
11
  clone(options: CloneOptions): Promise<CloneResponse>;
11
- hifiClone(options: HifiCloneOptions): Promise<Voice>;
12
12
  mix(options: MixVoicesOptions): Promise<MixVoicesResponse>;
13
13
  localize(options: LocalizeOptions): Promise<LocalizeResponse>;
14
14
  }
@@ -1,6 +1,7 @@
1
1
  import { Client } from '../lib/client.js';
2
- import { Voice, CreateVoice, UpdateVoice, CloneOptions, CloneResponse, HifiCloneOptions, MixVoicesOptions, MixVoicesResponse, LocalizeOptions, LocalizeResponse } from '../types/index.js';
2
+ import { Voice, CreateVoice, UpdateVoice, CloneOptions, CloneResponse, MixVoicesOptions, MixVoicesResponse, LocalizeOptions, LocalizeResponse } from '../types/index.js';
3
3
  import 'emittery';
4
+ import 'partysocket/ws';
4
5
 
5
6
  declare class Voices extends Client {
6
7
  list(): Promise<Voice[]>;
@@ -8,7 +9,6 @@ declare class Voices extends Client {
8
9
  create(voice: CreateVoice): Promise<Voice>;
9
10
  update(id: string, voice: UpdateVoice): Promise<Voice>;
10
11
  clone(options: CloneOptions): Promise<CloneResponse>;
11
- hifiClone(options: HifiCloneOptions): Promise<Voice>;
12
12
  mix(options: MixVoicesOptions): Promise<MixVoicesResponse>;
13
13
  localize(options: LocalizeOptions): Promise<LocalizeResponse>;
14
14
  }
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  Voices
3
- } from "../chunk-62LXGS65.js";
3
+ } from "../chunk-RJICGVPL.js";
4
4
  import "../chunk-FLWYXP5Z.js";
5
5
  import "../chunk-2BFEKY3F.js";
6
6
  import "../chunk-NJDRWDQ3.js";
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "name": "Cartesia",
5
5
  "url": "https://cartesia.ai"
6
6
  },
7
- "version": "1.2.2-alpha.0",
7
+ "version": "1.3.0",
8
8
  "description": "Client for the Cartesia API.",
9
9
  "type": "module",
10
10
  "module": "./dist/index.js",
@@ -5,6 +5,7 @@ import { Client } from "../lib/client";
5
5
  import { CARTESIA_VERSION, constructApiUrl } from "../lib/constants";
6
6
  import type {
7
7
  ConnectionEventData,
8
+ ConnectOptions,
8
9
  ContinueRequest,
9
10
  EmitteryCallbacks,
10
11
  StreamOptions,
@@ -127,6 +128,7 @@ export default class WebSocket extends Client {
127
128
  },
128
129
  {
129
130
  once: true,
131
+ signal: streamCompleteController.signal,
130
132
  },
131
133
  );
132
134
  this.socket?.addEventListener(
@@ -136,6 +138,7 @@ export default class WebSocket extends Client {
136
138
  },
137
139
  {
138
140
  once: true,
141
+ signal: streamCompleteController.signal,
139
142
  },
140
143
  );
141
144
  streamCompleteController.signal.addEventListener("abort", () => {
@@ -143,6 +146,7 @@ export default class WebSocket extends Client {
143
146
  if (timeoutId) {
144
147
  clearTimeout(timeoutId);
145
148
  }
149
+ emitter.clearListeners();
146
150
  });
147
151
 
148
152
  return {
@@ -203,16 +207,24 @@ export default class WebSocket extends Client {
203
207
  * @returns A promise that resolves when the WebSocket is connected.
204
208
  * @throws {Error} If the WebSocket fails to connect.
205
209
  */
206
- async connect() {
210
+ async connect(options: ConnectOptions = {}) {
211
+ if (this.#isConnected) {
212
+ throw new Error("WebSocket is already connected.");
213
+ }
214
+
207
215
  const emitter = new Emittery<ConnectionEventData>();
208
- this.socket = new PartySocketWebSocket(async () => {
209
- const url = constructApiUrl(this.baseUrl, "/tts/websocket", {
210
- websocket: true,
211
- });
212
- url.searchParams.set("api_key", await this.apiKey());
213
- url.searchParams.set("cartesia_version", CARTESIA_VERSION);
214
- return url.toString();
215
- });
216
+ this.socket = new PartySocketWebSocket(
217
+ async () => {
218
+ const url = constructApiUrl(this.baseUrl, "/tts/websocket", {
219
+ websocket: true,
220
+ });
221
+ url.searchParams.set("api_key", await this.apiKey());
222
+ url.searchParams.set("cartesia_version", CARTESIA_VERSION);
223
+ return url.toString();
224
+ },
225
+ undefined,
226
+ options,
227
+ );
216
228
  this.socket.binaryType = "arraybuffer";
217
229
 
218
230
  this.socket.onopen = () => {
@@ -1,4 +1,5 @@
1
1
  import type Emittery from "emittery";
2
+ import type { Options } from "partysocket/ws";
2
3
 
3
4
  export interface ClientOptions {
4
5
  apiKey?: string | (() => Promise<string>);
@@ -131,15 +132,6 @@ export type CloneOptions =
131
132
  enhance?: boolean;
132
133
  };
133
134
 
134
- export type HifiCloneOptions = CloneOptions & {
135
- name: string;
136
- description: string;
137
- language: Language;
138
- model_id: string;
139
- transcript?: string;
140
- base_voice_id?: string;
141
- };
142
-
143
135
  export type VoiceChangerOptions = {
144
136
  clip: File;
145
137
  voice: { id: string }; // match VoiceSpecifier shape, but only id is supported for now
@@ -221,6 +213,8 @@ export type WebSocketOptions = {
221
213
  sampleRate: number;
222
214
  };
223
215
 
216
+ export type ConnectOptions = Pick<Options, "WebSocket">;
217
+
224
218
  export type SourceEventData = {
225
219
  enqueue: never;
226
220
  close: never;
@@ -3,7 +3,6 @@ import type {
3
3
  CloneOptions,
4
4
  CloneResponse,
5
5
  CreateVoice,
6
- HifiCloneOptions,
7
6
  LocalizeOptions,
8
7
  LocalizeResponse,
9
8
  MixVoicesOptions,
@@ -57,45 +56,6 @@ export default class Voices extends Client {
57
56
  throw new Error("Invalid mode for clone()");
58
57
  }
59
58
 
60
- async hifiClone(options: HifiCloneOptions): Promise<Voice> {
61
- if (options.mode !== "clip") {
62
- throw new Error("Invalid mode for hifiClone()");
63
- }
64
-
65
- const formData = new FormData();
66
- formData.append("clip", options.clip);
67
- formData.append("name", options.name);
68
- formData.append("description", options.description);
69
- formData.append("language", options.language);
70
- formData.append("model_id", options.model_id);
71
- if (options.enhance !== undefined) {
72
- formData.append("enhance", options.enhance.toString());
73
- }
74
- if (options.transcript) {
75
- formData.append("transcript", options.transcript);
76
- }
77
- if (options.base_voice_id) {
78
- formData.append("base_voice_id", options.base_voice_id);
79
- }
80
-
81
- const response = await this._fetch("/voices/clone/hifi", {
82
- method: "POST",
83
- body: formData,
84
- });
85
-
86
- if (!response.ok) {
87
- if (response.headers.get("content-type")?.includes("application/json")) {
88
- const errorData = await response.json();
89
- throw new Error(errorData.message || "HiFi clone failed");
90
- }
91
- const errorText = await response.text();
92
- throw new Error(errorText || "HiFi clone failed");
93
- }
94
-
95
- const data = await response.json();
96
- return data as Voice;
97
- }
98
-
99
59
  async mix(options: MixVoicesOptions): Promise<MixVoicesResponse> {
100
60
  const response = await this._fetch("/voices/mix", {
101
61
  method: "POST",