@cartesia/cartesia-js 3.0.0-b4 → 3.0.0-b6
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/CHANGELOG.md +12 -0
- package/README.md +25 -21
- package/backcompat/index.d.mts +16 -0
- package/backcompat/index.d.mts.map +1 -0
- package/backcompat/index.d.ts +16 -0
- package/backcompat/index.d.ts.map +1 -0
- package/backcompat/index.js +36 -0
- package/backcompat/index.js.map +1 -0
- package/backcompat/index.mjs +31 -0
- package/backcompat/index.mjs.map +1 -0
- package/backcompat/tts-wrapper.d.mts +66 -0
- package/backcompat/tts-wrapper.d.mts.map +1 -0
- package/backcompat/tts-wrapper.d.ts +66 -0
- package/backcompat/tts-wrapper.d.ts.map +1 -0
- package/backcompat/tts-wrapper.js +260 -0
- package/backcompat/tts-wrapper.js.map +1 -0
- package/backcompat/tts-wrapper.mjs +254 -0
- package/backcompat/tts-wrapper.mjs.map +1 -0
- package/backcompat/types.d.mts +18 -0
- package/backcompat/types.d.mts.map +1 -0
- package/backcompat/types.d.ts +18 -0
- package/backcompat/types.d.ts.map +1 -0
- package/backcompat/types.js +3 -0
- package/backcompat/types.js.map +1 -0
- package/backcompat/types.mjs +2 -0
- package/backcompat/types.mjs.map +1 -0
- package/backcompat/voice-changer-wrapper.d.mts +17 -0
- package/backcompat/voice-changer-wrapper.d.mts.map +1 -0
- package/backcompat/voice-changer-wrapper.d.ts +17 -0
- package/backcompat/voice-changer-wrapper.d.ts.map +1 -0
- package/backcompat/voice-changer-wrapper.js +46 -0
- package/backcompat/voice-changer-wrapper.js.map +1 -0
- package/backcompat/voice-changer-wrapper.mjs +42 -0
- package/backcompat/voice-changer-wrapper.mjs.map +1 -0
- package/backcompat/voices-wrapper.d.mts +32 -0
- package/backcompat/voices-wrapper.d.mts.map +1 -0
- package/backcompat/voices-wrapper.d.ts +32 -0
- package/backcompat/voices-wrapper.d.ts.map +1 -0
- package/backcompat/voices-wrapper.js +77 -0
- package/backcompat/voices-wrapper.js.map +1 -0
- package/backcompat/voices-wrapper.mjs +73 -0
- package/backcompat/voices-wrapper.mjs.map +1 -0
- package/index.d.mts +1 -0
- package/index.d.mts.map +1 -1
- package/index.d.ts +1 -0
- package/index.d.ts.map +1 -1
- package/index.js +3 -1
- package/index.js.map +1 -1
- package/index.mjs +1 -0
- package/index.mjs.map +1 -1
- package/internal/tslib.js +4 -0
- package/package.json +11 -1
- package/src/backcompat/index.ts +40 -0
- package/src/backcompat/tts-wrapper.ts +324 -0
- package/src/backcompat/types.ts +19 -0
- package/src/backcompat/voice-changer-wrapper.ts +67 -0
- package/src/backcompat/voices-wrapper.ts +158 -0
- package/src/index.ts +1 -0
- package/src/version.ts +1 -1
- package/version.d.mts +1 -1
- package/version.d.ts +1 -1
- package/version.js +1 -1
- package/version.mjs +1 -1
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.VoicesWrapper = void 0;
|
|
4
|
+
class VoicesWrapper {
|
|
5
|
+
constructor(client) {
|
|
6
|
+
this.client = client;
|
|
7
|
+
}
|
|
8
|
+
async clone(clip, request, requestOptions) {
|
|
9
|
+
const params = {
|
|
10
|
+
clip: clip,
|
|
11
|
+
name: request.name,
|
|
12
|
+
language: request.language,
|
|
13
|
+
};
|
|
14
|
+
if (request.description !== undefined) {
|
|
15
|
+
params.description = request.description;
|
|
16
|
+
}
|
|
17
|
+
if (request.baseVoiceId !== undefined) {
|
|
18
|
+
params.base_voice_id = request.baseVoiceId;
|
|
19
|
+
}
|
|
20
|
+
const options = {};
|
|
21
|
+
if (requestOptions) {
|
|
22
|
+
if (requestOptions.timeoutInSeconds) {
|
|
23
|
+
options.timeout = requestOptions.timeoutInSeconds * 1000;
|
|
24
|
+
}
|
|
25
|
+
if (requestOptions.maxRetries !== undefined) {
|
|
26
|
+
options.maxRetries = requestOptions.maxRetries;
|
|
27
|
+
}
|
|
28
|
+
options.headers = requestOptions.headers;
|
|
29
|
+
options.signal = requestOptions.abortSignal;
|
|
30
|
+
}
|
|
31
|
+
return this.client.voices.clone(params, options);
|
|
32
|
+
}
|
|
33
|
+
async update(id, request, requestOptions) {
|
|
34
|
+
const params = {
|
|
35
|
+
name: request.name,
|
|
36
|
+
description: request.description,
|
|
37
|
+
};
|
|
38
|
+
const options = {};
|
|
39
|
+
if (requestOptions) {
|
|
40
|
+
if (requestOptions.timeoutInSeconds) {
|
|
41
|
+
options.timeout = requestOptions.timeoutInSeconds * 1000;
|
|
42
|
+
}
|
|
43
|
+
if (requestOptions.maxRetries !== undefined) {
|
|
44
|
+
options.maxRetries = requestOptions.maxRetries;
|
|
45
|
+
}
|
|
46
|
+
options.headers = requestOptions.headers;
|
|
47
|
+
options.signal = requestOptions.abortSignal;
|
|
48
|
+
}
|
|
49
|
+
return this.client.voices.update(id, params, options);
|
|
50
|
+
}
|
|
51
|
+
async localize(request, requestOptions) {
|
|
52
|
+
const params = {
|
|
53
|
+
voice_id: request.voiceId,
|
|
54
|
+
name: request.name,
|
|
55
|
+
description: request.description,
|
|
56
|
+
language: request.language,
|
|
57
|
+
original_speaker_gender: request.originalSpeakerGender,
|
|
58
|
+
};
|
|
59
|
+
if (request.dialect) {
|
|
60
|
+
params.dialect = request.dialect; // Cast dialect as list might vary slightly or be strict
|
|
61
|
+
}
|
|
62
|
+
const options = {};
|
|
63
|
+
if (requestOptions) {
|
|
64
|
+
if (requestOptions.timeoutInSeconds) {
|
|
65
|
+
options.timeout = requestOptions.timeoutInSeconds * 1000;
|
|
66
|
+
}
|
|
67
|
+
if (requestOptions.maxRetries !== undefined) {
|
|
68
|
+
options.maxRetries = requestOptions.maxRetries;
|
|
69
|
+
}
|
|
70
|
+
options.headers = requestOptions.headers;
|
|
71
|
+
options.signal = requestOptions.abortSignal;
|
|
72
|
+
}
|
|
73
|
+
return this.client.voices.localize(params, options);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
exports.VoicesWrapper = VoicesWrapper;
|
|
77
|
+
//# sourceMappingURL=voices-wrapper.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"voices-wrapper.js","sourceRoot":"","sources":["../src/backcompat/voices-wrapper.ts"],"names":[],"mappings":";;;AA8DA,MAAa,aAAa;IAGzB,YAAY,MAAgB;QAC3B,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;IACtB,CAAC;IAED,KAAK,CAAC,KAAK,CACV,IAAiC,EACjC,OAAoC,EACpC,cAAyC;QAEzC,MAAM,MAAM,GAAqB;YAChC,IAAI,EAAE,IAAkB;YACxB,IAAI,EAAE,OAAO,CAAC,IAAI;YAClB,QAAQ,EAAE,OAAO,CAAC,QAA6B;SAC/C,CAAC;QAEF,IAAI,OAAO,CAAC,WAAW,KAAK,SAAS,EAAE,CAAC;YACvC,MAAM,CAAC,WAAW,GAAG,OAAO,CAAC,WAAW,CAAC;QAC1C,CAAC;QACD,IAAI,OAAO,CAAC,WAAW,KAAK,SAAS,EAAE,CAAC;YACvC,MAAM,CAAC,aAAa,GAAG,OAAO,CAAC,WAAW,CAAC;QAC5C,CAAC;QAED,MAAM,OAAO,GAA2B,EAAE,CAAC;QAC3C,IAAI,cAAc,EAAE,CAAC;YACpB,IAAI,cAAc,CAAC,gBAAgB,EAAE,CAAC;gBACrC,OAAO,CAAC,OAAO,GAAG,cAAc,CAAC,gBAAgB,GAAG,IAAI,CAAC;YAC1D,CAAC;YACD,IAAI,cAAc,CAAC,UAAU,KAAK,SAAS,EAAE,CAAC;gBAC7C,OAAO,CAAC,UAAU,GAAG,cAAc,CAAC,UAAU,CAAC;YAChD,CAAC;YACD,OAAO,CAAC,OAAO,GAAG,cAAc,CAAC,OAAO,CAAC;YACzC,OAAO,CAAC,MAAM,GAAG,cAAc,CAAC,WAAW,CAAC;QAC7C,CAAC;QAED,OAAO,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,KAAK,CAAC,MAAM,EAAE,OAAO,CAAC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AAgB,EAAE,CAAC;gBACrC,OAAO,CAAC,OAAO,GAAG,cAAc,CAAC,gBAAgB,GAAG,IAAI,CAAC;YAC1D,CAAC;YACD,IAAI,cAAc,CAAC,UAAU,KAAK,SAAS,EAAE,CAAC;gBAC7C,OAAO,CAAC,UAAU,GAAG,cAAc,CAAC,UAAU,CAAC;YAChD,CAAC;YACD,OAAO,CAAC,OAAO,GAAG,cAAc,CAAC,OAAO,CAAC;YACzC,OAAO,CAAC,MAAM,GAAG,cAAc,CAAC,WAAW,CAAC;QAC7C,CAAC;QAED,OAAO,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,QAAQ,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IACrD,CAAC;CACD;AA/FD,sCA+FC"}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
export class VoicesWrapper {
|
|
2
|
+
constructor(client) {
|
|
3
|
+
this.client = client;
|
|
4
|
+
}
|
|
5
|
+
async clone(clip, request, requestOptions) {
|
|
6
|
+
const params = {
|
|
7
|
+
clip: clip,
|
|
8
|
+
name: request.name,
|
|
9
|
+
language: request.language,
|
|
10
|
+
};
|
|
11
|
+
if (request.description !== undefined) {
|
|
12
|
+
params.description = request.description;
|
|
13
|
+
}
|
|
14
|
+
if (request.baseVoiceId !== undefined) {
|
|
15
|
+
params.base_voice_id = request.baseVoiceId;
|
|
16
|
+
}
|
|
17
|
+
const options = {};
|
|
18
|
+
if (requestOptions) {
|
|
19
|
+
if (requestOptions.timeoutInSeconds) {
|
|
20
|
+
options.timeout = requestOptions.timeoutInSeconds * 1000;
|
|
21
|
+
}
|
|
22
|
+
if (requestOptions.maxRetries !== undefined) {
|
|
23
|
+
options.maxRetries = requestOptions.maxRetries;
|
|
24
|
+
}
|
|
25
|
+
options.headers = requestOptions.headers;
|
|
26
|
+
options.signal = requestOptions.abortSignal;
|
|
27
|
+
}
|
|
28
|
+
return this.client.voices.clone(params, options);
|
|
29
|
+
}
|
|
30
|
+
async update(id, request, requestOptions) {
|
|
31
|
+
const params = {
|
|
32
|
+
name: request.name,
|
|
33
|
+
description: request.description,
|
|
34
|
+
};
|
|
35
|
+
const options = {};
|
|
36
|
+
if (requestOptions) {
|
|
37
|
+
if (requestOptions.timeoutInSeconds) {
|
|
38
|
+
options.timeout = requestOptions.timeoutInSeconds * 1000;
|
|
39
|
+
}
|
|
40
|
+
if (requestOptions.maxRetries !== undefined) {
|
|
41
|
+
options.maxRetries = requestOptions.maxRetries;
|
|
42
|
+
}
|
|
43
|
+
options.headers = requestOptions.headers;
|
|
44
|
+
options.signal = requestOptions.abortSignal;
|
|
45
|
+
}
|
|
46
|
+
return this.client.voices.update(id, params, options);
|
|
47
|
+
}
|
|
48
|
+
async localize(request, requestOptions) {
|
|
49
|
+
const params = {
|
|
50
|
+
voice_id: request.voiceId,
|
|
51
|
+
name: request.name,
|
|
52
|
+
description: request.description,
|
|
53
|
+
language: request.language,
|
|
54
|
+
original_speaker_gender: request.originalSpeakerGender,
|
|
55
|
+
};
|
|
56
|
+
if (request.dialect) {
|
|
57
|
+
params.dialect = request.dialect; // Cast dialect as list might vary slightly or be strict
|
|
58
|
+
}
|
|
59
|
+
const options = {};
|
|
60
|
+
if (requestOptions) {
|
|
61
|
+
if (requestOptions.timeoutInSeconds) {
|
|
62
|
+
options.timeout = requestOptions.timeoutInSeconds * 1000;
|
|
63
|
+
}
|
|
64
|
+
if (requestOptions.maxRetries !== undefined) {
|
|
65
|
+
options.maxRetries = requestOptions.maxRetries;
|
|
66
|
+
}
|
|
67
|
+
options.headers = requestOptions.headers;
|
|
68
|
+
options.signal = requestOptions.abortSignal;
|
|
69
|
+
}
|
|
70
|
+
return this.client.voices.localize(params, options);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
//# sourceMappingURL=voices-wrapper.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"voices-wrapper.mjs","sourceRoot":"","sources":["../src/backcompat/voices-wrapper.ts"],"names":[],"mappings":"A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AC,UAAU,GAAG,cAAc,CAAC,UAAU,CAAC;YAChD,CAAC;YACD,OAAO,CAAC,OAAO,GAAG,cAAc,CAAC,OAAO,CAAC;YACzC,OAAO,CAAC,MAAM,GAAG,cAAc,CAAC,WAAW,CAAC;QAC7C,CAAC;QAED,OAAO,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,QAAQ,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IACrD,CAAC;CACD"}
|
package/index.d.mts
CHANGED
|
@@ -2,6 +2,7 @@ export { Cartesia as default } from "./client.mjs";
|
|
|
2
2
|
export { type Uploadable, toFile } from "./core/uploads.mjs";
|
|
3
3
|
export { APIPromise } from "./core/api-promise.mjs";
|
|
4
4
|
export { Cartesia, type ClientOptions } from "./client.mjs";
|
|
5
|
+
export { CartesiaClient } from "./backcompat/index.mjs";
|
|
5
6
|
export { PagePromise } from "./core/pagination.mjs";
|
|
6
7
|
export { CartesiaError, APIError, APIConnectionError, APIConnectionTimeoutError, APIUserAbortError, NotFoundError, ConflictError, RateLimitError, BadRequestError, AuthenticationError, InternalServerError, PermissionDeniedError, UnprocessableEntityError, } from "./core/error.mjs";
|
|
7
8
|
//# sourceMappingURL=index.d.mts.map
|
package/index.d.mts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.mts","sourceRoot":"","sources":["src/index.ts"],"names":[],"mappings":"OAEO,EAAE,QAAQ,IAAI,OAAO,EAAE;OAEvB,EAAE,KAAK,UAAU,EAAE,MAAM,EAAE;OAC3B,EAAE,UAAU,EAAE;OACd,EAAE,QAAQ,EAAE,KAAK,aAAa,EAAE;OAChC,EAAE,WAAW,EAAE;OACf,EACL,aAAa,EACb,QAAQ,EACR,kBAAkB,EAClB,yBAAyB,EACzB,iBAAiB,EACjB,aAAa,EACb,aAAa,EACb,cAAc,EACd,eAAe,EACf,mBAAmB,EACnB,mBAAmB,EACnB,qBAAqB,EACrB,wBAAwB,GACzB"}
|
|
1
|
+
{"version":3,"file":"index.d.mts","sourceRoot":"","sources":["src/index.ts"],"names":[],"mappings":"OAEO,EAAE,QAAQ,IAAI,OAAO,EAAE;OAEvB,EAAE,KAAK,UAAU,EAAE,MAAM,EAAE;OAC3B,EAAE,UAAU,EAAE;OACd,EAAE,QAAQ,EAAE,KAAK,aAAa,EAAE;OAChC,EAAE,cAAc,EAAE;OAClB,EAAE,WAAW,EAAE;OACf,EACL,aAAa,EACb,QAAQ,EACR,kBAAkB,EAClB,yBAAyB,EACzB,iBAAiB,EACjB,aAAa,EACb,aAAa,EACb,cAAc,EACd,eAAe,EACf,mBAAmB,EACnB,mBAAmB,EACnB,qBAAqB,EACrB,wBAAwB,GACzB"}
|
package/index.d.ts
CHANGED
|
@@ -2,6 +2,7 @@ export { Cartesia as default } from "./client.js";
|
|
|
2
2
|
export { type Uploadable, toFile } from "./core/uploads.js";
|
|
3
3
|
export { APIPromise } from "./core/api-promise.js";
|
|
4
4
|
export { Cartesia, type ClientOptions } from "./client.js";
|
|
5
|
+
export { CartesiaClient } from "./backcompat/index.js";
|
|
5
6
|
export { PagePromise } from "./core/pagination.js";
|
|
6
7
|
export { CartesiaError, APIError, APIConnectionError, APIConnectionTimeoutError, APIUserAbortError, NotFoundError, ConflictError, RateLimitError, BadRequestError, AuthenticationError, InternalServerError, PermissionDeniedError, UnprocessableEntityError, } from "./core/error.js";
|
|
7
8
|
//# sourceMappingURL=index.d.ts.map
|
package/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["src/index.ts"],"names":[],"mappings":"OAEO,EAAE,QAAQ,IAAI,OAAO,EAAE;OAEvB,EAAE,KAAK,UAAU,EAAE,MAAM,EAAE;OAC3B,EAAE,UAAU,EAAE;OACd,EAAE,QAAQ,EAAE,KAAK,aAAa,EAAE;OAChC,EAAE,WAAW,EAAE;OACf,EACL,aAAa,EACb,QAAQ,EACR,kBAAkB,EAClB,yBAAyB,EACzB,iBAAiB,EACjB,aAAa,EACb,aAAa,EACb,cAAc,EACd,eAAe,EACf,mBAAmB,EACnB,mBAAmB,EACnB,qBAAqB,EACrB,wBAAwB,GACzB"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["src/index.ts"],"names":[],"mappings":"OAEO,EAAE,QAAQ,IAAI,OAAO,EAAE;OAEvB,EAAE,KAAK,UAAU,EAAE,MAAM,EAAE;OAC3B,EAAE,UAAU,EAAE;OACd,EAAE,QAAQ,EAAE,KAAK,aAAa,EAAE;OAChC,EAAE,cAAc,EAAE;OAClB,EAAE,WAAW,EAAE;OACf,EACL,aAAa,EACb,QAAQ,EACR,kBAAkB,EAClB,yBAAyB,EACzB,iBAAiB,EACjB,aAAa,EACb,aAAa,EACb,cAAc,EACd,eAAe,EACf,mBAAmB,EACnB,mBAAmB,EACnB,qBAAqB,EACrB,wBAAwB,GACzB"}
|
package/index.js
CHANGED
|
@@ -4,7 +4,7 @@ exports = module.exports = function (...args) {
|
|
|
4
4
|
return new exports.default(...args)
|
|
5
5
|
}
|
|
6
6
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
-
exports.UnprocessableEntityError = exports.PermissionDeniedError = exports.InternalServerError = exports.AuthenticationError = exports.BadRequestError = exports.RateLimitError = exports.ConflictError = exports.NotFoundError = exports.APIUserAbortError = exports.APIConnectionTimeoutError = exports.APIConnectionError = exports.APIError = exports.CartesiaError = exports.PagePromise = exports.Cartesia = exports.APIPromise = exports.toFile = exports.default = void 0;
|
|
7
|
+
exports.UnprocessableEntityError = exports.PermissionDeniedError = exports.InternalServerError = exports.AuthenticationError = exports.BadRequestError = exports.RateLimitError = exports.ConflictError = exports.NotFoundError = exports.APIUserAbortError = exports.APIConnectionTimeoutError = exports.APIConnectionError = exports.APIError = exports.CartesiaError = exports.PagePromise = exports.CartesiaClient = exports.Cartesia = exports.APIPromise = exports.toFile = exports.default = void 0;
|
|
8
8
|
var client_1 = require("./client.js");
|
|
9
9
|
Object.defineProperty(exports, "default", { enumerable: true, get: function () { return client_1.Cartesia; } });
|
|
10
10
|
var uploads_1 = require("./core/uploads.js");
|
|
@@ -13,6 +13,8 @@ var api_promise_1 = require("./core/api-promise.js");
|
|
|
13
13
|
Object.defineProperty(exports, "APIPromise", { enumerable: true, get: function () { return api_promise_1.APIPromise; } });
|
|
14
14
|
var client_2 = require("./client.js");
|
|
15
15
|
Object.defineProperty(exports, "Cartesia", { enumerable: true, get: function () { return client_2.Cartesia; } });
|
|
16
|
+
var backcompat_1 = require("./backcompat/index.js");
|
|
17
|
+
Object.defineProperty(exports, "CartesiaClient", { enumerable: true, get: function () { return backcompat_1.CartesiaClient; } });
|
|
16
18
|
var pagination_1 = require("./core/pagination.js");
|
|
17
19
|
Object.defineProperty(exports, "PagePromise", { enumerable: true, get: function () { return pagination_1.PagePromise; } });
|
|
18
20
|
var error_1 = require("./core/error.js");
|
package/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["src/index.ts"],"names":[],"mappings":";AAAA,sFAAsF;;;AAEtF,sCAA+C;AAAtC,iGAAA,QAAQ,OAAW;AAE5B,6CAAyD;AAA/B,iGAAA,MAAM,OAAA;AAChC,qDAAgD;AAAvC,yGAAA,UAAU,OAAA;AACnB,sCAAwD;AAA/C,kGAAA,QAAQ,OAAA;AACjB,mDAAgD;AAAvC,yGAAA,WAAW,OAAA;AACpB,yCAcsB;AAbpB,sGAAA,aAAa,OAAA;AACb,iGAAA,QAAQ,OAAA;AACR,2GAAA,kBAAkB,OAAA;AAClB,kHAAA,yBAAyB,OAAA;AACzB,0GAAA,iBAAiB,OAAA;AACjB,sGAAA,aAAa,OAAA;AACb,sGAAA,aAAa,OAAA;AACb,uGAAA,cAAc,OAAA;AACd,wGAAA,eAAe,OAAA;AACf,4GAAA,mBAAmB,OAAA;AACnB,4GAAA,mBAAmB,OAAA;AACnB,8GAAA,qBAAqB,OAAA;AACrB,iHAAA,wBAAwB,OAAA"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["src/index.ts"],"names":[],"mappings":";AAAA,sFAAsF;;;AAEtF,sCAA+C;AAAtC,iGAAA,QAAQ,OAAW;AAE5B,6CAAyD;AAA/B,iGAAA,MAAM,OAAA;AAChC,qDAAgD;AAAvC,yGAAA,UAAU,OAAA;AACnB,sCAAwD;AAA/C,kGAAA,QAAQ,OAAA;AACjB,oDAA8C;AAArC,4GAAA,cAAc,OAAA;AACvB,mDAAgD;AAAvC,yGAAA,WAAW,OAAA;AACpB,yCAcsB;AAbpB,sGAAA,aAAa,OAAA;AACb,iGAAA,QAAQ,OAAA;AACR,2GAAA,kBAAkB,OAAA;AAClB,kHAAA,yBAAyB,OAAA;AACzB,0GAAA,iBAAiB,OAAA;AACjB,sGAAA,aAAa,OAAA;AACb,sGAAA,aAAa,OAAA;AACb,uGAAA,cAAc,OAAA;AACd,wGAAA,eAAe,OAAA;AACf,4GAAA,mBAAmB,OAAA;AACnB,4GAAA,mBAAmB,OAAA;AACnB,8GAAA,qBAAqB,OAAA;AACrB,iHAAA,wBAAwB,OAAA"}
|
package/index.mjs
CHANGED
|
@@ -3,6 +3,7 @@ export { Cartesia as default } from "./client.mjs";
|
|
|
3
3
|
export { toFile } from "./core/uploads.mjs";
|
|
4
4
|
export { APIPromise } from "./core/api-promise.mjs";
|
|
5
5
|
export { Cartesia } from "./client.mjs";
|
|
6
|
+
export { CartesiaClient } from "./backcompat/index.mjs";
|
|
6
7
|
export { PagePromise } from "./core/pagination.mjs";
|
|
7
8
|
export { CartesiaError, APIError, APIConnectionError, APIConnectionTimeoutError, APIUserAbortError, NotFoundError, ConflictError, RateLimitError, BadRequestError, AuthenticationError, InternalServerError, PermissionDeniedError, UnprocessableEntityError, } from "./core/error.mjs";
|
|
8
9
|
//# sourceMappingURL=index.mjs.map
|
package/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.mjs","sourceRoot":"","sources":["src/index.ts"],"names":[],"mappings":"AAAA,sFAAsF;OAE/E,EAAE,QAAQ,IAAI,OAAO,EAAE;OAEvB,EAAmB,MAAM,EAAE;OAC3B,EAAE,UAAU,EAAE;OACd,EAAE,QAAQ,EAAsB;OAChC,EAAE,WAAW,EAAE;OACf,EACL,aAAa,EACb,QAAQ,EACR,kBAAkB,EAClB,yBAAyB,EACzB,iBAAiB,EACjB,aAAa,EACb,aAAa,EACb,cAAc,EACd,eAAe,EACf,mBAAmB,EACnB,mBAAmB,EACnB,qBAAqB,EACrB,wBAAwB,GACzB"}
|
|
1
|
+
{"version":3,"file":"index.mjs","sourceRoot":"","sources":["src/index.ts"],"names":[],"mappings":"AAAA,sFAAsF;OAE/E,EAAE,QAAQ,IAAI,OAAO,EAAE;OAEvB,EAAmB,MAAM,EAAE;OAC3B,EAAE,UAAU,EAAE;OACd,EAAE,QAAQ,EAAsB;OAChC,EAAE,cAAc,EAAE;OAClB,EAAE,WAAW,EAAE;OACf,EACL,aAAa,EACb,QAAQ,EACR,kBAAkB,EAClB,yBAAyB,EACzB,iBAAiB,EACjB,aAAa,EACb,aAAa,EACb,cAAc,EACd,eAAe,EACf,mBAAmB,EACnB,mBAAmB,EACnB,qBAAqB,EACrB,wBAAwB,GACzB"}
|
package/internal/tslib.js
CHANGED
|
@@ -5,6 +5,7 @@ exports.__classPrivateFieldSet = __classPrivateFieldSet;
|
|
|
5
5
|
exports.__classPrivateFieldGet = __classPrivateFieldGet;
|
|
6
6
|
exports.__importStar = __importStar;
|
|
7
7
|
exports.__exportStar = __exportStar;
|
|
8
|
+
exports.__importDefault = __importDefault;
|
|
8
9
|
function __classPrivateFieldSet(receiver, state, value, kind, f) {
|
|
9
10
|
if (kind === "m")
|
|
10
11
|
throw new TypeError("Private method is not writable");
|
|
@@ -79,3 +80,6 @@ function __exportStar(m, o) {
|
|
|
79
80
|
if (p !== "default" && !Object.prototype.hasOwnProperty.call(o, p))
|
|
80
81
|
__createBinding(o, m, p);
|
|
81
82
|
}
|
|
83
|
+
function __importDefault(mod) {
|
|
84
|
+
return mod && mod.__esModule ? mod : { default: mod };
|
|
85
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cartesia/cartesia-js",
|
|
3
|
-
"version": "3.0.0-
|
|
3
|
+
"version": "3.0.0-b6",
|
|
4
4
|
"description": "The official TypeScript library for the Cartesia API",
|
|
5
5
|
"author": "Cartesia <>",
|
|
6
6
|
"types": "./index.d.ts",
|
|
@@ -52,6 +52,16 @@
|
|
|
52
52
|
"./api-promise.mjs": {
|
|
53
53
|
"default": "./api-promise.mjs"
|
|
54
54
|
},
|
|
55
|
+
"./backcompat/*.mjs": {
|
|
56
|
+
"default": "./backcompat/*.mjs"
|
|
57
|
+
},
|
|
58
|
+
"./backcompat/*.js": {
|
|
59
|
+
"default": "./backcompat/*.js"
|
|
60
|
+
},
|
|
61
|
+
"./backcompat/*": {
|
|
62
|
+
"import": "./backcompat/*.mjs",
|
|
63
|
+
"require": "./backcompat/*.js"
|
|
64
|
+
},
|
|
55
65
|
"./client": {
|
|
56
66
|
"import": "./client.mjs",
|
|
57
67
|
"require": "./client.js"
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { Cartesia, type ClientOptions } from "../client";
|
|
2
|
+
import { TTSWrapper } from "./tts-wrapper";
|
|
3
|
+
import { VoicesWrapper } from "./voices-wrapper";
|
|
4
|
+
import { VoiceChangerWrapper } from "./voice-changer-wrapper";
|
|
5
|
+
import type { CartesiaClientOptions } from "./types";
|
|
6
|
+
|
|
7
|
+
/*
|
|
8
|
+
* CartesiaClient - deprecated backcompat class.
|
|
9
|
+
* New code should use Cartesia class directly instead.
|
|
10
|
+
*/
|
|
11
|
+
export class CartesiaClient {
|
|
12
|
+
private client: Cartesia;
|
|
13
|
+
public tts: TTSWrapper;
|
|
14
|
+
public voices: VoicesWrapper;
|
|
15
|
+
public voiceChanger: VoiceChangerWrapper;
|
|
16
|
+
|
|
17
|
+
constructor(options: CartesiaClientOptions = {}) {
|
|
18
|
+
const newOptions: ClientOptions = {};
|
|
19
|
+
|
|
20
|
+
if (options.apiKey) {
|
|
21
|
+
newOptions.apiKey = options.apiKey as any;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
if (options.baseUrl) {
|
|
25
|
+
newOptions.baseURL = options.baseUrl as any;
|
|
26
|
+
} else if (options.environment) {
|
|
27
|
+
newOptions.baseURL = options.environment as any;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
this.client = new Cartesia(newOptions);
|
|
31
|
+
this.tts = new TTSWrapper(this.client);
|
|
32
|
+
this.voices = new VoicesWrapper(this.client);
|
|
33
|
+
this.voiceChanger = new VoiceChangerWrapper(this.client);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export * from "./tts-wrapper";
|
|
38
|
+
export * from "./voices-wrapper";
|
|
39
|
+
export * from "./voice-changer-wrapper";
|
|
40
|
+
export * from "./types";
|
|
@@ -0,0 +1,324 @@
|
|
|
1
|
+
import WebSocket from "ws";
|
|
2
|
+
import { Cartesia } from "../client";
|
|
3
|
+
import { type RequestOptions as InternalRequestOptions } from "../internal/request-options";
|
|
4
|
+
import { BackCompatRequestOptions } from "./types";
|
|
5
|
+
|
|
6
|
+
// Define compatible interfaces to match the old SDK types for WebSocket
|
|
7
|
+
export interface BackCompatWebSocketOptions {
|
|
8
|
+
container?: "raw" | "wav" | "mp3";
|
|
9
|
+
encoding?: "pcm_f32le" | "pcm_s16le" | "pcm_alaw" | "pcm_mulaw";
|
|
10
|
+
sampleRate: number;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export type BackCompatTtsRequestVoiceSpecifier =
|
|
14
|
+
| { mode: "id"; id: string }
|
|
15
|
+
| { mode: "embedding"; embedding: number[] };
|
|
16
|
+
|
|
17
|
+
export interface BackCompatGenerationConfig {
|
|
18
|
+
volume?: number;
|
|
19
|
+
speed?: number;
|
|
20
|
+
emotion?: string[]; // Simplified from strict union for backcompat flexibility
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface BackCompatWebSocketTtsRequest {
|
|
24
|
+
modelId: string;
|
|
25
|
+
transcript: string;
|
|
26
|
+
voice: BackCompatTtsRequestVoiceSpecifier;
|
|
27
|
+
generationConfig?: BackCompatGenerationConfig;
|
|
28
|
+
outputFormat?: {
|
|
29
|
+
container?: "raw" | "wav" | "mp3";
|
|
30
|
+
encoding?: "pcm_f32le" | "pcm_s16le" | "pcm_alaw" | "pcm_mulaw";
|
|
31
|
+
sampleRate?: number;
|
|
32
|
+
bitRate?: number;
|
|
33
|
+
};
|
|
34
|
+
contextId?: string; // Backcompat might pass this in request?
|
|
35
|
+
// Add other fields as needed
|
|
36
|
+
continue?: boolean;
|
|
37
|
+
duration?: number;
|
|
38
|
+
addTimestamps?: boolean;
|
|
39
|
+
addPhonemeTimestamps?: boolean;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// Helper for generating UUIDs. Not cryptographically secure.
|
|
43
|
+
function uuidv4() {
|
|
44
|
+
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
|
|
45
|
+
var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
|
|
46
|
+
return v.toString(16);
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
class AudioSource {
|
|
51
|
+
private buffers: Buffer[] = [];
|
|
52
|
+
private waiter: ((val?: any) => void) | null = null;
|
|
53
|
+
public isDone = false;
|
|
54
|
+
|
|
55
|
+
push(data: Buffer) {
|
|
56
|
+
this.buffers.push(data);
|
|
57
|
+
if (this.waiter) {
|
|
58
|
+
this.waiter();
|
|
59
|
+
this.waiter = null;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
markDone() {
|
|
64
|
+
this.isDone = true;
|
|
65
|
+
if (this.waiter) {
|
|
66
|
+
this.waiter();
|
|
67
|
+
this.waiter = null;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
async read(outBuffer: Float32Array): Promise<number> {
|
|
72
|
+
if (this.buffers.length === 0 && !this.isDone) {
|
|
73
|
+
await new Promise<void>((resolve) => { this.waiter = resolve; });
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
if (this.buffers.length === 0 && this.isDone) {
|
|
77
|
+
return 0;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
let totalFloatsRead = 0;
|
|
81
|
+
let outOffset = 0;
|
|
82
|
+
const maxFloats = outBuffer.length;
|
|
83
|
+
|
|
84
|
+
while (this.buffers.length > 0 && totalFloatsRead < maxFloats) {
|
|
85
|
+
const buf = this.buffers[0] as Buffer; // ts not smart enough to check loop condition
|
|
86
|
+
const floatsInBuf = buf.length / 4;
|
|
87
|
+
const floatsNeeded = maxFloats - totalFloatsRead;
|
|
88
|
+
|
|
89
|
+
const floatsToCopy = Math.min(floatsInBuf, floatsNeeded);
|
|
90
|
+
const bytesToCopy = floatsToCopy * 4;
|
|
91
|
+
|
|
92
|
+
// Copy to outBuffer.
|
|
93
|
+
// Create a view on the buffer to read floats.
|
|
94
|
+
|
|
95
|
+
// We need to ensure byteOffset is a multiple of 4.
|
|
96
|
+
// If not, we must copy the buffer to a new one.
|
|
97
|
+
let srcFloats: Float32Array;
|
|
98
|
+
if (buf.byteOffset % 4 === 0) {
|
|
99
|
+
srcFloats = new Float32Array(buf.buffer, buf.byteOffset, floatsInBuf);
|
|
100
|
+
} else {
|
|
101
|
+
const alignedBuf = new Uint8Array(buf);
|
|
102
|
+
srcFloats = new Float32Array(alignedBuf.buffer, alignedBuf.byteOffset, floatsInBuf);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
outBuffer.set(srcFloats.subarray(0, floatsToCopy), outOffset);
|
|
106
|
+
|
|
107
|
+
totalFloatsRead += floatsToCopy;
|
|
108
|
+
outOffset += floatsToCopy;
|
|
109
|
+
|
|
110
|
+
if (floatsToCopy < floatsInBuf) {
|
|
111
|
+
// We didn't use the whole buffer. Update it.
|
|
112
|
+
this.buffers[0] = buf.subarray(bytesToCopy);
|
|
113
|
+
} else {
|
|
114
|
+
// We used the whole buffer. Remove it.
|
|
115
|
+
this.buffers.shift();
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
return totalFloatsRead;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
export class WebSocketWrapper {
|
|
124
|
+
private client: Cartesia;
|
|
125
|
+
private config: BackCompatWebSocketOptions;
|
|
126
|
+
private socket: WebSocket | null = null;
|
|
127
|
+
private sources: Map<string, AudioSource> = new Map();
|
|
128
|
+
// Fallback source for messages without context_id or if we just want to capture everything (legacy behavior?)
|
|
129
|
+
// The original test didn't use context_id explicitly in send() but expected a response source.
|
|
130
|
+
// We'll map context_id to source.
|
|
131
|
+
private defaultSource: AudioSource | null = null;
|
|
132
|
+
|
|
133
|
+
constructor(client: Cartesia, config: BackCompatWebSocketOptions) {
|
|
134
|
+
this.client = client;
|
|
135
|
+
this.config = config;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
async connect() {
|
|
139
|
+
const baseURL = this.client.baseURL;
|
|
140
|
+
// Construct WebSocket URL
|
|
141
|
+
// baseURL is like https://api.cartesia.ai
|
|
142
|
+
let urlStr = baseURL.replace(/^http/, "ws");
|
|
143
|
+
if (!urlStr.includes("/tts/websocket")) {
|
|
144
|
+
if (urlStr.endsWith("/")) {
|
|
145
|
+
urlStr += "tts/websocket";
|
|
146
|
+
} else {
|
|
147
|
+
urlStr += "/tts/websocket";
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
const url = new URL(urlStr);
|
|
152
|
+
|
|
153
|
+
const headers: any = {
|
|
154
|
+
"cartesia-version": "2025-04-16",
|
|
155
|
+
};
|
|
156
|
+
if (this.client.apiKey) {
|
|
157
|
+
headers["Authorization"] = `Bearer ${this.client.apiKey}`;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
this.socket = new WebSocket(url.toString(), {
|
|
161
|
+
headers: headers,
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
return new Promise<void>((resolve, reject) => {
|
|
165
|
+
this.socket!.on("open", () => {
|
|
166
|
+
console.log("WebSocket connected.");
|
|
167
|
+
resolve();
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
this.socket!.on("error", (err) => {
|
|
171
|
+
console.error("WebSocket error:", err);
|
|
172
|
+
reject(err);
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
this.socket!.on("message", (data) => {
|
|
176
|
+
this.handleMessage(data);
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
this.socket!.on("close", () => {
|
|
180
|
+
console.log("WebSocket closed.");
|
|
181
|
+
this.sources.forEach((s) => { s.markDone(); });
|
|
182
|
+
if (this.defaultSource) this.defaultSource.markDone();
|
|
183
|
+
});
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
private handleMessage(data: WebSocket.Data) {
|
|
188
|
+
try {
|
|
189
|
+
const str = data.toString();
|
|
190
|
+
const msg = JSON.parse(str);
|
|
191
|
+
|
|
192
|
+
const contextId = msg.context_id;
|
|
193
|
+
let source = contextId ? this.sources.get(contextId) : this.defaultSource;
|
|
194
|
+
|
|
195
|
+
// If we received a message for a context we don't know about, and we have a default source, use it
|
|
196
|
+
if (!source && this.defaultSource) {
|
|
197
|
+
source = this.defaultSource;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
if (msg.type === "chunk" && msg.data) {
|
|
201
|
+
const audioData = Buffer.from(msg.data, "base64");
|
|
202
|
+
if (source) source.push(audioData);
|
|
203
|
+
} else if (msg.type === "done") {
|
|
204
|
+
if (source) source.markDone();
|
|
205
|
+
} else if (msg.type === "error") {
|
|
206
|
+
console.error("Server error:", msg);
|
|
207
|
+
if (source) source.markDone(); // Fail the stream?
|
|
208
|
+
}
|
|
209
|
+
} catch (e) {
|
|
210
|
+
console.error("Error parsing message:", e);
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
async send(request: BackCompatWebSocketTtsRequest) {
|
|
215
|
+
if (!this.socket) {
|
|
216
|
+
throw new Error("WebSocket not connected");
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
// Ensure request has a context_id so we can route the response
|
|
220
|
+
const contextId = request.contextId || uuidv4();
|
|
221
|
+
|
|
222
|
+
const source = new AudioSource();
|
|
223
|
+
this.sources.set(contextId, source);
|
|
224
|
+
// Also set as default source if none exists, for compatibility with simple tests
|
|
225
|
+
if (!this.defaultSource) {
|
|
226
|
+
this.defaultSource = source;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
// Construct payload
|
|
230
|
+
const payload: any = {
|
|
231
|
+
model_id: request.modelId,
|
|
232
|
+
transcript: request.transcript,
|
|
233
|
+
voice: request.voice,
|
|
234
|
+
context_id: contextId,
|
|
235
|
+
};
|
|
236
|
+
|
|
237
|
+
// Output Format
|
|
238
|
+
if (request.outputFormat) {
|
|
239
|
+
payload.output_format = {
|
|
240
|
+
container: request.outputFormat.container,
|
|
241
|
+
encoding: request.outputFormat.encoding,
|
|
242
|
+
sample_rate: request.outputFormat.sampleRate,
|
|
243
|
+
bit_rate: request.outputFormat.bitRate,
|
|
244
|
+
};
|
|
245
|
+
} else if (this.config) {
|
|
246
|
+
payload.output_format = {
|
|
247
|
+
container: this.config.container,
|
|
248
|
+
encoding: this.config.encoding,
|
|
249
|
+
sample_rate: this.config.sampleRate,
|
|
250
|
+
};
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
// Generation Config
|
|
254
|
+
if (request.generationConfig) {
|
|
255
|
+
payload.generation_config = request.generationConfig;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
// Other fields
|
|
259
|
+
if (request.continue !== undefined) payload.continue = request.continue;
|
|
260
|
+
if (request.duration !== undefined) payload.duration = request.duration;
|
|
261
|
+
if (request.addTimestamps !== undefined) payload.add_timestamps = request.addTimestamps;
|
|
262
|
+
if (request.addPhonemeTimestamps !== undefined) payload.add_phoneme_timestamps = request.addPhonemeTimestamps;
|
|
263
|
+
|
|
264
|
+
this.socket.send(JSON.stringify(payload));
|
|
265
|
+
|
|
266
|
+
return {
|
|
267
|
+
source: source
|
|
268
|
+
};
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
disconnect() {
|
|
272
|
+
if (this.socket) {
|
|
273
|
+
this.socket.close();
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
export class TTSWrapper {
|
|
279
|
+
private client: Cartesia;
|
|
280
|
+
|
|
281
|
+
constructor(client: Cartesia) {
|
|
282
|
+
this.client = client;
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
websocket(config: BackCompatWebSocketOptions) {
|
|
286
|
+
return new WebSocketWrapper(this.client, config);
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
async bytes(request: any, requestOptions?: BackCompatRequestOptions) {
|
|
290
|
+
const params: any = {
|
|
291
|
+
model_id: request.modelId,
|
|
292
|
+
transcript: request.transcript,
|
|
293
|
+
voice: request.voice,
|
|
294
|
+
generation_config: request.generationConfig,
|
|
295
|
+
context_id: request.contextId,
|
|
296
|
+
};
|
|
297
|
+
|
|
298
|
+
if (request.outputFormat) {
|
|
299
|
+
params.output_format = {
|
|
300
|
+
...request.outputFormat,
|
|
301
|
+
sample_rate: request.outputFormat.sampleRate,
|
|
302
|
+
bit_rate: request.outputFormat.bitRate,
|
|
303
|
+
};
|
|
304
|
+
// Remove camelCase keys
|
|
305
|
+
delete params.output_format.sampleRate;
|
|
306
|
+
delete params.output_format.bitRate;
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
const options: any = {};
|
|
310
|
+
if (requestOptions) {
|
|
311
|
+
if (requestOptions.timeoutInSeconds) {
|
|
312
|
+
options.timeout = requestOptions.timeoutInSeconds * 1000;
|
|
313
|
+
}
|
|
314
|
+
if (requestOptions.maxRetries !== undefined) {
|
|
315
|
+
options.maxRetries = requestOptions.maxRetries;
|
|
316
|
+
}
|
|
317
|
+
options.headers = requestOptions.headers;
|
|
318
|
+
options.signal = requestOptions.abortSignal;
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
// @ts-ignore
|
|
322
|
+
return this.client.tts.synthesizeBytes(params, options);
|
|
323
|
+
}
|
|
324
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export type Supplier<T> = T | Promise<T> | (() => T | Promise<T>);
|
|
2
|
+
|
|
3
|
+
export interface CartesiaClientOptions {
|
|
4
|
+
environment?: Supplier<"https://api.cartesia.ai" | string>;
|
|
5
|
+
/** Specify a custom URL to connect the client to. */
|
|
6
|
+
baseUrl?: Supplier<string>;
|
|
7
|
+
apiKey?: Supplier<string | undefined>;
|
|
8
|
+
/** Override the Cartesia-Version header */
|
|
9
|
+
cartesiaVersion?: string;
|
|
10
|
+
fetcher?: any;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export interface BackCompatRequestOptions {
|
|
14
|
+
timeoutInSeconds?: number;
|
|
15
|
+
maxRetries?: number;
|
|
16
|
+
abortSignal?: AbortSignal;
|
|
17
|
+
cartesiaVersion?: string;
|
|
18
|
+
headers?: Record<string, string>;
|
|
19
|
+
}
|