@cartesia/cartesia-js 0.0.4-alpha.0 → 1.0.0-alpha.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.
Files changed (89) hide show
  1. package/.turbo/turbo-build.log +63 -45
  2. package/CHANGELOG.md +12 -0
  3. package/README.md +123 -16
  4. package/dist/chunk-36JBKJUN.js +119 -0
  5. package/dist/chunk-3F5E46FT.js +212 -0
  6. package/dist/{chunk-XPIMIAAE.js → chunk-3FL2SNIR.js} +1 -1
  7. package/dist/chunk-JGP5BIUV.js +34 -0
  8. package/dist/chunk-KWBSQZTY.js +25 -0
  9. package/dist/chunk-PQ6CIPFW.js +120 -0
  10. package/dist/chunk-RO7TY474.js +81 -0
  11. package/dist/chunk-T3RG6WV4.js +22 -0
  12. package/dist/{chunk-R4P7LWVZ.js → chunk-WIFMLPT5.js} +31 -6
  13. package/dist/chunk-WVTITUXX.js +58 -0
  14. package/dist/chunk-XHTDPLFR.js +19 -0
  15. package/dist/index.cjs +425 -166
  16. package/dist/index.d.cts +7 -3
  17. package/dist/index.d.ts +7 -3
  18. package/dist/index.js +13 -6
  19. package/dist/lib/client.cjs +49 -1
  20. package/dist/lib/client.d.cts +2 -0
  21. package/dist/lib/client.d.ts +2 -0
  22. package/dist/lib/client.js +3 -3
  23. package/dist/lib/constants.cjs +15 -8
  24. package/dist/lib/constants.d.cts +4 -4
  25. package/dist/lib/constants.d.ts +4 -4
  26. package/dist/lib/constants.js +6 -6
  27. package/dist/lib/index.cjs +310 -171
  28. package/dist/lib/index.d.cts +6 -2
  29. package/dist/lib/index.d.ts +6 -2
  30. package/dist/lib/index.js +9 -6
  31. package/dist/react/index.cjs +573 -290
  32. package/dist/react/index.d.cts +20 -14
  33. package/dist/react/index.d.ts +20 -14
  34. package/dist/react/index.js +157 -105
  35. package/dist/react/utils.js +2 -2
  36. package/dist/tts/index.cjs +496 -0
  37. package/dist/tts/index.d.cts +17 -0
  38. package/dist/tts/index.d.ts +17 -0
  39. package/dist/tts/index.js +12 -0
  40. package/dist/tts/player.cjs +198 -0
  41. package/dist/tts/player.d.cts +43 -0
  42. package/dist/tts/player.d.ts +43 -0
  43. package/dist/tts/player.js +8 -0
  44. package/dist/tts/source.cjs +181 -0
  45. package/dist/tts/source.d.cts +53 -0
  46. package/dist/tts/source.d.ts +53 -0
  47. package/dist/tts/source.js +7 -0
  48. package/dist/{audio → tts}/utils.cjs +25 -60
  49. package/dist/tts/utils.d.cts +67 -0
  50. package/dist/tts/utils.d.ts +67 -0
  51. package/dist/{audio → tts}/utils.js +2 -7
  52. package/dist/tts/websocket.cjs +479 -0
  53. package/dist/tts/websocket.d.cts +53 -0
  54. package/dist/tts/websocket.d.ts +53 -0
  55. package/dist/tts/websocket.js +11 -0
  56. package/dist/types/index.d.cts +50 -1
  57. package/dist/types/index.d.ts +50 -1
  58. package/dist/voices/index.cjs +157 -0
  59. package/dist/voices/index.d.cts +12 -0
  60. package/dist/voices/index.d.ts +12 -0
  61. package/dist/voices/index.js +9 -0
  62. package/package.json +2 -1
  63. package/src/index.ts +1 -0
  64. package/src/lib/client.ts +15 -1
  65. package/src/lib/constants.ts +15 -4
  66. package/src/lib/index.ts +6 -3
  67. package/src/react/index.ts +176 -110
  68. package/src/tts/index.ts +17 -0
  69. package/src/tts/player.ts +110 -0
  70. package/src/tts/source.ts +115 -0
  71. package/src/tts/utils.ts +150 -0
  72. package/src/tts/websocket.ts +214 -0
  73. package/src/types/index.ts +63 -0
  74. package/src/voices/index.ts +47 -0
  75. package/dist/audio/index.cjs +0 -404
  76. package/dist/audio/index.d.cts +0 -5
  77. package/dist/audio/index.d.ts +0 -5
  78. package/dist/audio/index.js +0 -10
  79. package/dist/audio/utils.d.cts +0 -5
  80. package/dist/audio/utils.d.ts +0 -5
  81. package/dist/chunk-4MHF74A7.js +0 -272
  82. package/dist/chunk-5TSWLYOW.js +0 -113
  83. package/dist/chunk-MJIFZWHS.js +0 -18
  84. package/dist/chunk-OVI3W3GG.js +0 -12
  85. package/dist/chunk-S6A27RQL.js +0 -18
  86. package/dist/index-C2_3XFxn.d.cts +0 -163
  87. package/dist/index-DgwnZezj.d.ts +0 -163
  88. package/src/audio/index.ts +0 -297
  89. package/src/audio/utils.ts +0 -220
@@ -1,6 +1,55 @@
1
+ import emittery__default from 'emittery';
2
+
1
3
  interface ClientOptions {
2
4
  apiKey?: string;
3
5
  baseUrl?: string;
4
6
  }
7
+ type Sentinel = null;
8
+ type Chunk = string | Sentinel;
9
+ type ConnectionEventData = {
10
+ open: never;
11
+ close: never;
12
+ };
13
+ type StreamRequest = {
14
+ inputs: object;
15
+ options: {
16
+ timeout?: number;
17
+ };
18
+ };
19
+ type EmitteryCallbacks<T> = {
20
+ on: emittery__default<T>["on"];
21
+ off: emittery__default<T>["off"];
22
+ once: emittery__default<T>["once"];
23
+ events: emittery__default<T>["events"];
24
+ };
25
+ type CloneOptions = {
26
+ mode: "url";
27
+ link: string;
28
+ } | {
29
+ mode: "clip";
30
+ clip: Blob;
31
+ };
32
+ type Voice = {
33
+ id: string;
34
+ name: string;
35
+ description: string;
36
+ embedding: number[];
37
+ is_public: boolean;
38
+ user_id: string;
39
+ created_at: string;
40
+ };
41
+ type CreateVoice = Pick<Voice, "name" | "description" | "embedding"> & Partial<Omit<Voice, "name" | "description" | "embedding">>;
42
+ type CloneResponse = {
43
+ embedding: number[];
44
+ };
45
+ type WebSocketOptions = {
46
+ sampleRate: number;
47
+ };
48
+ type SourceEventData = {
49
+ enqueue: never;
50
+ close: never;
51
+ wait: never;
52
+ read: never;
53
+ };
5
54
 
6
- export type { ClientOptions };
55
+ export type { Chunk, ClientOptions, CloneOptions, CloneResponse, ConnectionEventData, CreateVoice, EmitteryCallbacks, Sentinel, SourceEventData, StreamRequest, Voice, WebSocketOptions };
@@ -0,0 +1,157 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __defProps = Object.defineProperties;
5
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
6
+ var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
7
+ var __getOwnPropNames = Object.getOwnPropertyNames;
8
+ var __getOwnPropSymbols = Object.getOwnPropertySymbols;
9
+ var __getProtoOf = Object.getPrototypeOf;
10
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
11
+ var __propIsEnum = Object.prototype.propertyIsEnumerable;
12
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
13
+ var __spreadValues = (a, b) => {
14
+ for (var prop in b || (b = {}))
15
+ if (__hasOwnProp.call(b, prop))
16
+ __defNormalProp(a, prop, b[prop]);
17
+ if (__getOwnPropSymbols)
18
+ for (var prop of __getOwnPropSymbols(b)) {
19
+ if (__propIsEnum.call(b, prop))
20
+ __defNormalProp(a, prop, b[prop]);
21
+ }
22
+ return a;
23
+ };
24
+ var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
25
+ var __export = (target, all) => {
26
+ for (var name in all)
27
+ __defProp(target, name, { get: all[name], enumerable: true });
28
+ };
29
+ var __copyProps = (to, from, except, desc) => {
30
+ if (from && typeof from === "object" || typeof from === "function") {
31
+ for (let key of __getOwnPropNames(from))
32
+ if (!__hasOwnProp.call(to, key) && key !== except)
33
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
34
+ }
35
+ return to;
36
+ };
37
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
38
+ // If the importer is in node compatibility mode or this is not an ESM
39
+ // file that has been converted to a CommonJS file using a Babel-
40
+ // compatible transform (i.e. "__esModule" has not been set), then set
41
+ // "default" to the CommonJS "module.exports" for node compatibility.
42
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
43
+ mod
44
+ ));
45
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
46
+ var __async = (__this, __arguments, generator) => {
47
+ return new Promise((resolve, reject) => {
48
+ var fulfilled = (value) => {
49
+ try {
50
+ step(generator.next(value));
51
+ } catch (e) {
52
+ reject(e);
53
+ }
54
+ };
55
+ var rejected = (value) => {
56
+ try {
57
+ step(generator.throw(value));
58
+ } catch (e) {
59
+ reject(e);
60
+ }
61
+ };
62
+ var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
63
+ step((generator = generator.apply(__this, __arguments)).next());
64
+ });
65
+ };
66
+
67
+ // src/voices/index.ts
68
+ var voices_exports = {};
69
+ __export(voices_exports, {
70
+ default: () => Voices
71
+ });
72
+ module.exports = __toCommonJS(voices_exports);
73
+
74
+ // src/lib/client.ts
75
+ var import_cross_fetch = __toESM(require("cross-fetch"), 1);
76
+
77
+ // src/lib/constants.ts
78
+ var BASE_URL = "https://api.cartesia.ai/";
79
+ var CARTESIA_VERSION = "2024-06-10";
80
+ var constructApiUrl = (baseUrl, path, protocol) => {
81
+ const normalizedPath = path.startsWith("/") ? path : `/${path}`;
82
+ if (!protocol) {
83
+ return new URL(`${baseUrl}${normalizedPath}`);
84
+ }
85
+ if (!["http", "ws"].includes(protocol)) {
86
+ throw new Error(`Invalid protocol: ${protocol}`);
87
+ }
88
+ return new URL(`${baseUrl.replace(/^http/, protocol)}${normalizedPath}`);
89
+ };
90
+
91
+ // src/lib/client.ts
92
+ var Client = class {
93
+ constructor(options = {}) {
94
+ if (!(options.apiKey || process.env.CARTESIA_API_KEY)) {
95
+ throw new Error("Missing Cartesia API key.");
96
+ }
97
+ this.apiKey = options.apiKey || process.env.CARTESIA_API_KEY;
98
+ this.baseUrl = options.baseUrl || BASE_URL;
99
+ }
100
+ fetch(path, options = {}) {
101
+ const url = constructApiUrl(this.baseUrl, path);
102
+ return (0, import_cross_fetch.default)(url.toString(), __spreadProps(__spreadValues({}, options), {
103
+ headers: __spreadValues({
104
+ "X-API-Key": this.apiKey,
105
+ "Cartesia-Version": CARTESIA_VERSION
106
+ }, options.headers)
107
+ }));
108
+ }
109
+ };
110
+
111
+ // src/voices/index.ts
112
+ var Voices = class extends Client {
113
+ list() {
114
+ return __async(this, null, function* () {
115
+ const response = yield this.fetch("/voices");
116
+ return response.json();
117
+ });
118
+ }
119
+ get(voiceId) {
120
+ return __async(this, null, function* () {
121
+ const response = yield this.fetch(`/voices/${voiceId}`);
122
+ return response.json();
123
+ });
124
+ }
125
+ create(voice) {
126
+ return __async(this, null, function* () {
127
+ const response = yield this.fetch("/voices", {
128
+ method: "POST",
129
+ body: JSON.stringify(voice)
130
+ });
131
+ return response.json();
132
+ });
133
+ }
134
+ clone(options) {
135
+ return __async(this, null, function* () {
136
+ if (options.mode === "url") {
137
+ const response = yield this.fetch(
138
+ `/voices/clone/url?link=${options.link}`,
139
+ {
140
+ method: "POST"
141
+ }
142
+ );
143
+ return response.json();
144
+ }
145
+ if (options.mode === "clip") {
146
+ const formData = new FormData();
147
+ formData.append("clip", options.clip);
148
+ const response = yield this.fetch("/voices/clone/clip", {
149
+ method: "POST",
150
+ body: formData
151
+ });
152
+ return response.json();
153
+ }
154
+ throw new Error("Invalid mode for clone()");
155
+ });
156
+ }
157
+ };
@@ -0,0 +1,12 @@
1
+ import { Client } from '../lib/client.cjs';
2
+ import { Voice, CreateVoice, CloneOptions, CloneResponse } from '../types/index.cjs';
3
+ import 'emittery';
4
+
5
+ declare class Voices extends Client {
6
+ list(): Promise<Voice[]>;
7
+ get(voiceId: string): Promise<Voice>;
8
+ create(voice: CreateVoice): Promise<Voice>;
9
+ clone(options: CloneOptions): Promise<CloneResponse>;
10
+ }
11
+
12
+ export { Voices as default };
@@ -0,0 +1,12 @@
1
+ import { Client } from '../lib/client.js';
2
+ import { Voice, CreateVoice, CloneOptions, CloneResponse } from '../types/index.js';
3
+ import 'emittery';
4
+
5
+ declare class Voices extends Client {
6
+ list(): Promise<Voice[]>;
7
+ get(voiceId: string): Promise<Voice>;
8
+ create(voice: CreateVoice): Promise<Voice>;
9
+ clone(options: CloneOptions): Promise<CloneResponse>;
10
+ }
11
+
12
+ export { Voices as default };
@@ -0,0 +1,9 @@
1
+ import {
2
+ Voices
3
+ } from "../chunk-WVTITUXX.js";
4
+ import "../chunk-JGP5BIUV.js";
5
+ import "../chunk-XHTDPLFR.js";
6
+ import "../chunk-WIFMLPT5.js";
7
+ export {
8
+ Voices as default
9
+ };
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "name": "Cartesia",
5
5
  "url": "https://cartesia.ai"
6
6
  },
7
- "version": "0.0.4-alpha.0",
7
+ "version": "1.0.0-alpha.2",
8
8
  "description": "Client for the Cartesia API.",
9
9
  "type": "module",
10
10
  "module": "./dist/index.js",
@@ -24,6 +24,7 @@
24
24
  },
25
25
  "dependencies": {
26
26
  "base64-js": "^1.5.1",
27
+ "cross-fetch": "^4.0.0",
27
28
  "emittery": "^1.0.3",
28
29
  "human-id": "^4.1.1",
29
30
  "partysocket": "^1.0.1"
package/src/index.ts CHANGED
@@ -1,3 +1,4 @@
1
1
  export { Cartesia as default } from "./lib";
2
2
  export * from "./lib";
3
3
  export * from "./types";
4
+ export { default as WebPlayer } from "./tts/player";
package/src/lib/client.ts CHANGED
@@ -1,5 +1,6 @@
1
+ import fetch from "cross-fetch";
1
2
  import type { ClientOptions } from "../types";
2
- import { BASE_URL } from "./constants";
3
+ import { BASE_URL, CARTESIA_VERSION, constructApiUrl } from "./constants";
3
4
 
4
5
  export class Client {
5
6
  apiKey: string;
@@ -14,4 +15,17 @@ export class Client {
14
15
  this.apiKey = (options.apiKey || process.env.CARTESIA_API_KEY)!;
15
16
  this.baseUrl = options.baseUrl || BASE_URL;
16
17
  }
18
+
19
+ fetch(path: string, options: RequestInit = {}) {
20
+ const url = constructApiUrl(this.baseUrl, path);
21
+
22
+ return fetch(url.toString(), {
23
+ ...options,
24
+ headers: {
25
+ "X-API-Key": this.apiKey,
26
+ "Cartesia-Version": CARTESIA_VERSION,
27
+ ...options.headers,
28
+ },
29
+ });
30
+ }
17
31
  }
@@ -1,6 +1,17 @@
1
- export const BASE_URL = "https://api.cartesia.ai/v0";
2
- export const SAMPLE_RATE = 44100;
1
+ export const BASE_URL = "https://api.cartesia.ai/";
2
+ export const CARTESIA_VERSION = "2024-06-10";
3
3
 
4
- export const constructWebsocketUrl = (baseUrl: string) => {
5
- return new URL(`${baseUrl.replace(/^http/, "ws")}/audio/websocket`);
4
+ export const constructApiUrl = (
5
+ baseUrl: string,
6
+ path: string,
7
+ protocol?: string,
8
+ ) => {
9
+ const normalizedPath = path.startsWith("/") ? path : `/${path}`;
10
+ if (!protocol) {
11
+ return new URL(`${baseUrl}${normalizedPath}`);
12
+ }
13
+ if (!["http", "ws"].includes(protocol)) {
14
+ throw new Error(`Invalid protocol: ${protocol}`);
15
+ }
16
+ return new URL(`${baseUrl.replace(/^http/, protocol)}${normalizedPath}`);
6
17
  };
package/src/lib/index.ts CHANGED
@@ -1,13 +1,16 @@
1
- import Audio from "../audio";
1
+ import TTS from "../tts";
2
2
  import type { ClientOptions } from "../types";
3
+ import Voices from "../voices";
3
4
  import { Client } from "./client";
4
5
 
5
6
  export class Cartesia extends Client {
6
- audio: Audio;
7
+ tts: TTS;
8
+ voices: Voices;
7
9
 
8
10
  constructor(options: ClientOptions = {}) {
9
11
  super(options);
10
12
 
11
- this.audio = new Audio(options);
13
+ this.tts = new TTS(options);
14
+ this.voices = new Voices(options);
12
15
  }
13
16
  }