@cartesia/cartesia-js 2.2.5 → 2.2.8
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/Client.d.ts +3 -0
- package/Client.js +11 -6
- package/README.md +469 -144
- package/api/resources/apiStatus/client/Client.js +1 -1
- package/api/resources/auth/client/Client.d.ts +17 -0
- package/api/resources/auth/client/Client.js +18 -1
- package/api/resources/auth/types/TokenGrant.d.ts +3 -1
- package/api/resources/auth/types/TokenRequest.d.ts +2 -2
- package/api/resources/index.d.ts +1 -0
- package/api/resources/index.js +1 -0
- package/api/resources/infill/client/Client.js +1 -1
- package/api/resources/infill/client/requests/InfillBytesRequest.d.ts +3 -11
- package/api/resources/stt/client/Client.d.ts +43 -0
- package/api/resources/stt/client/Client.js +108 -0
- package/api/resources/stt/client/index.d.ts +1 -1
- package/api/resources/stt/client/index.js +15 -3
- package/api/resources/stt/client/requests/TranscriptionRequest.d.ts +147 -0
- package/api/resources/stt/client/requests/TranscriptionRequest.js +5 -0
- package/api/resources/stt/client/requests/index.d.ts +1 -0
- package/api/resources/stt/client/requests/index.js +2 -0
- package/api/resources/stt/index.d.ts +1 -0
- package/api/resources/stt/index.js +1 -0
- package/api/resources/stt/types/TimestampGranularity.d.ts +12 -0
- package/api/resources/stt/types/TimestampGranularity.js +9 -0
- package/api/resources/stt/types/TranscriptMessage.d.ts +1 -1
- package/api/resources/stt/types/TranscriptionResponse.d.ts +2 -2
- package/api/resources/stt/types/index.d.ts +1 -0
- package/api/resources/stt/types/index.js +1 -0
- package/api/resources/tts/client/Client.d.ts +7 -2
- package/api/resources/tts/client/Client.js +8 -8
- package/api/resources/tts/types/Controls.d.ts +1 -1
- package/api/resources/tts/types/Emotion.d.ts +2 -33
- package/api/resources/tts/types/Emotion.js +0 -28
- package/api/resources/tts/types/EmotionDeprecated.d.ts +38 -0
- package/api/resources/tts/types/EmotionDeprecated.js +33 -0
- package/api/resources/tts/types/GenerationConfig.d.ts +15 -0
- package/api/resources/tts/types/GenerationConfig.js +5 -0
- package/api/resources/tts/types/GenerationRequest.d.ts +5 -4
- package/api/resources/tts/types/Mp3OutputFormat.d.ts +1 -0
- package/api/resources/tts/types/RawOutputFormat.d.ts +1 -0
- package/api/resources/tts/types/SseOutputFormat.d.ts +10 -0
- package/api/resources/tts/types/SseOutputFormat.js +5 -0
- package/api/resources/tts/types/TtsRequest.d.ts +1 -0
- package/api/resources/tts/types/TtssseRequest.d.ts +27 -0
- package/api/resources/tts/types/TtssseRequest.js +5 -0
- package/api/resources/tts/types/WebSocketRawOutputFormat.d.ts +1 -0
- package/api/resources/tts/types/WebSocketRequest.d.ts +2 -4
- package/api/resources/tts/types/WebSocketTtsRequest.d.ts +4 -1
- package/api/resources/tts/types/index.d.ts +5 -1
- package/api/resources/tts/types/index.js +5 -1
- package/api/resources/voiceChanger/client/Client.d.ts +9 -4
- package/api/resources/voiceChanger/client/Client.js +24 -20
- package/api/resources/voiceChanger/client/requests/VoiceChangerBytesRequest.d.ts +3 -8
- package/api/resources/voiceChanger/client/requests/VoiceChangerSseRequest.d.ts +3 -8
- package/api/resources/voices/client/Client.js +8 -8
- package/api/resources/voices/client/requests/CloneVoiceRequest.d.ts +6 -24
- package/api/resources/voices/types/LocalizeDialect.d.ts +4 -8
- package/core/fetcher/Fetcher.d.ts +2 -2
- package/core/fetcher/Fetcher.js +4 -3
- package/core/fetcher/getResponseBody.js +3 -3
- package/dist/Client.d.ts +3 -0
- package/dist/Client.js +11 -6
- package/dist/api/resources/apiStatus/client/Client.js +1 -1
- package/dist/api/resources/auth/client/Client.d.ts +17 -0
- package/dist/api/resources/auth/client/Client.js +18 -1
- package/dist/api/resources/auth/types/TokenGrant.d.ts +3 -1
- package/dist/api/resources/auth/types/TokenRequest.d.ts +2 -2
- package/dist/api/resources/index.d.ts +1 -0
- package/dist/api/resources/index.js +1 -0
- package/dist/api/resources/infill/client/Client.js +1 -1
- package/dist/api/resources/infill/client/requests/InfillBytesRequest.d.ts +3 -11
- package/dist/api/resources/stt/client/Client.d.ts +43 -0
- package/dist/api/resources/stt/client/Client.js +108 -0
- package/dist/api/resources/stt/client/index.d.ts +1 -1
- package/dist/api/resources/stt/client/index.js +15 -3
- package/dist/api/resources/stt/client/requests/TranscriptionRequest.d.ts +147 -0
- package/dist/api/resources/stt/client/requests/TranscriptionRequest.js +5 -0
- package/dist/api/resources/stt/client/requests/index.d.ts +1 -0
- package/dist/api/resources/stt/client/requests/index.js +2 -0
- package/dist/api/resources/stt/index.d.ts +1 -0
- package/dist/api/resources/stt/index.js +1 -0
- package/dist/api/resources/stt/types/TimestampGranularity.d.ts +12 -0
- package/dist/api/resources/stt/types/TimestampGranularity.js +9 -0
- package/dist/api/resources/stt/types/TranscriptMessage.d.ts +1 -1
- package/dist/api/resources/stt/types/TranscriptionResponse.d.ts +2 -2
- package/dist/api/resources/stt/types/index.d.ts +1 -0
- package/dist/api/resources/stt/types/index.js +1 -0
- package/dist/api/resources/tts/client/Client.d.ts +7 -2
- package/dist/api/resources/tts/client/Client.js +8 -8
- package/dist/api/resources/tts/types/Controls.d.ts +1 -1
- package/dist/api/resources/tts/types/Emotion.d.ts +2 -33
- package/dist/api/resources/tts/types/Emotion.js +0 -28
- package/dist/api/resources/tts/types/EmotionDeprecated.d.ts +38 -0
- package/dist/api/resources/tts/types/EmotionDeprecated.js +33 -0
- package/dist/api/resources/tts/types/GenerationConfig.d.ts +15 -0
- package/dist/api/resources/tts/types/GenerationConfig.js +5 -0
- package/dist/api/resources/tts/types/GenerationRequest.d.ts +5 -4
- package/dist/api/resources/tts/types/Mp3OutputFormat.d.ts +1 -0
- package/dist/api/resources/tts/types/RawOutputFormat.d.ts +1 -0
- package/dist/api/resources/tts/types/SseOutputFormat.d.ts +10 -0
- package/dist/api/resources/tts/types/SseOutputFormat.js +5 -0
- package/dist/api/resources/tts/types/TtsRequest.d.ts +1 -0
- package/dist/api/resources/tts/types/TtssseRequest.d.ts +27 -0
- package/dist/api/resources/tts/types/TtssseRequest.js +5 -0
- package/dist/api/resources/tts/types/WebSocketRawOutputFormat.d.ts +1 -0
- package/dist/api/resources/tts/types/WebSocketRequest.d.ts +2 -4
- package/dist/api/resources/tts/types/WebSocketTtsRequest.d.ts +4 -1
- package/dist/api/resources/tts/types/index.d.ts +5 -1
- package/dist/api/resources/tts/types/index.js +5 -1
- package/dist/api/resources/voiceChanger/client/Client.d.ts +9 -4
- package/dist/api/resources/voiceChanger/client/Client.js +24 -20
- package/dist/api/resources/voiceChanger/client/requests/VoiceChangerBytesRequest.d.ts +3 -8
- package/dist/api/resources/voiceChanger/client/requests/VoiceChangerSseRequest.d.ts +3 -8
- package/dist/api/resources/voices/client/Client.js +8 -8
- package/dist/api/resources/voices/client/requests/CloneVoiceRequest.d.ts +6 -24
- package/dist/api/resources/voices/types/LocalizeDialect.d.ts +4 -8
- package/dist/core/fetcher/Fetcher.d.ts +2 -2
- package/dist/core/fetcher/Fetcher.js +4 -3
- package/dist/core/fetcher/getResponseBody.js +3 -3
- package/dist/index.d.ts +2 -0
- package/dist/index.js +8 -1
- package/dist/serialization/resources/auth/types/TokenGrant.d.ts +2 -1
- package/dist/serialization/resources/auth/types/TokenGrant.js +2 -1
- package/dist/serialization/resources/auth/types/TokenRequest.d.ts +1 -1
- package/dist/serialization/resources/auth/types/TokenRequest.js +1 -1
- package/dist/serialization/resources/stt/types/TimestampGranularity.d.ts +10 -0
- package/dist/serialization/resources/stt/types/TimestampGranularity.js +41 -0
- package/dist/serialization/resources/stt/types/index.d.ts +1 -0
- package/dist/serialization/resources/stt/types/index.js +1 -0
- package/dist/serialization/resources/tts/types/Controls.d.ts +2 -2
- package/dist/serialization/resources/tts/types/Controls.js +2 -2
- package/dist/serialization/resources/tts/types/Emotion.d.ts +1 -1
- package/dist/serialization/resources/tts/types/Emotion.js +1 -27
- package/dist/serialization/resources/tts/types/EmotionDeprecated.d.ts +10 -0
- package/dist/serialization/resources/tts/types/EmotionDeprecated.js +67 -0
- package/dist/serialization/resources/tts/types/GenerationConfig.d.ts +15 -0
- package/dist/serialization/resources/tts/types/GenerationConfig.js +46 -0
- package/dist/serialization/resources/tts/types/GenerationRequest.d.ts +3 -1
- package/dist/serialization/resources/tts/types/GenerationRequest.js +3 -1
- package/dist/serialization/resources/tts/types/SseOutputFormat.d.ts +15 -0
- package/dist/serialization/resources/tts/types/SseOutputFormat.js +46 -0
- package/dist/serialization/resources/tts/types/TtsRequest.d.ts +2 -0
- package/dist/serialization/resources/tts/types/TtsRequest.js +2 -0
- package/dist/serialization/resources/tts/types/TtssseRequest.d.ts +29 -0
- package/dist/serialization/resources/tts/types/TtssseRequest.js +60 -0
- package/dist/serialization/resources/tts/types/WebSocketTtsRequest.d.ts +3 -1
- package/dist/serialization/resources/tts/types/WebSocketTtsRequest.js +3 -1
- package/dist/serialization/resources/tts/types/index.d.ts +5 -1
- package/dist/serialization/resources/tts/types/index.js +5 -1
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/dist/wrapper/StreamingSTTClient.d.ts +22 -2
- package/dist/wrapper/StreamingSTTClient.js +124 -1
- package/dist/wrapper/SttWebsocket.d.ts +8 -3
- package/dist/wrapper/SttWebsocket.js +24 -3
- package/dist/wrapper/Websocket.js +1 -1
- package/index.d.ts +2 -0
- package/index.js +8 -1
- package/package.json +1 -1
- package/reference.md +89 -1
- package/serialization/resources/auth/types/TokenGrant.d.ts +2 -1
- package/serialization/resources/auth/types/TokenGrant.js +2 -1
- package/serialization/resources/auth/types/TokenRequest.d.ts +1 -1
- package/serialization/resources/auth/types/TokenRequest.js +1 -1
- package/serialization/resources/stt/types/TimestampGranularity.d.ts +10 -0
- package/serialization/resources/stt/types/TimestampGranularity.js +41 -0
- package/serialization/resources/stt/types/index.d.ts +1 -0
- package/serialization/resources/stt/types/index.js +1 -0
- package/serialization/resources/tts/types/Controls.d.ts +2 -2
- package/serialization/resources/tts/types/Controls.js +2 -2
- package/serialization/resources/tts/types/Emotion.d.ts +1 -1
- package/serialization/resources/tts/types/Emotion.js +1 -27
- package/serialization/resources/tts/types/EmotionDeprecated.d.ts +10 -0
- package/serialization/resources/tts/types/EmotionDeprecated.js +67 -0
- package/serialization/resources/tts/types/GenerationConfig.d.ts +15 -0
- package/serialization/resources/tts/types/GenerationConfig.js +46 -0
- package/serialization/resources/tts/types/GenerationRequest.d.ts +3 -1
- package/serialization/resources/tts/types/GenerationRequest.js +3 -1
- package/serialization/resources/tts/types/SseOutputFormat.d.ts +15 -0
- package/serialization/resources/tts/types/SseOutputFormat.js +46 -0
- package/serialization/resources/tts/types/TtsRequest.d.ts +2 -0
- package/serialization/resources/tts/types/TtsRequest.js +2 -0
- package/serialization/resources/tts/types/TtssseRequest.d.ts +29 -0
- package/serialization/resources/tts/types/TtssseRequest.js +60 -0
- package/serialization/resources/tts/types/WebSocketTtsRequest.d.ts +3 -1
- package/serialization/resources/tts/types/WebSocketTtsRequest.js +3 -1
- package/serialization/resources/tts/types/index.d.ts +5 -1
- package/serialization/resources/tts/types/index.js +5 -1
- package/version.d.ts +1 -1
- package/version.js +1 -1
- package/wrapper/StreamingSTTClient.d.ts +22 -2
- package/wrapper/StreamingSTTClient.js +124 -1
- package/wrapper/SttWebsocket.d.ts +8 -3
- package/wrapper/SttWebsocket.js +24 -3
- package/wrapper/Websocket.js +1 -1
package/README.md
CHANGED
|
@@ -1,13 +1,9 @@
|
|
|
1
|
-
# Cartesia TypeScript
|
|
1
|
+
# Cartesia TypeScript Library
|
|
2
2
|
|
|
3
3
|
[](https://buildwithfern.com?utm_source=github&utm_medium=github&utm_campaign=readme&utm_source=https%3A%2F%2Fgithub.com%2Fcartesia-ai%2Fcartesia-js)
|
|
4
4
|
[](https://www.npmjs.com/package/@cartesia/cartesia-js)
|
|
5
|
-
[](https://discord.gg/cartesia)
|
|
6
5
|
|
|
7
|
-
The Cartesia TypeScript library provides convenient access to the Cartesia
|
|
8
|
-
|
|
9
|
-
> [!TIP]
|
|
10
|
-
> **[@cartesia-ai/cartesia-nextjs-demo](https://github.com/cartesia-ai/cartesia-nextjs-demo)** is our demo app that shows how to use Cartesia text-to-speech in a browser-based application.
|
|
6
|
+
The Cartesia TypeScript library provides convenient access to the Cartesia APIs from TypeScript.
|
|
11
7
|
|
|
12
8
|
## Installation
|
|
13
9
|
|
|
@@ -17,128 +13,22 @@ npm i -s @cartesia/cartesia-js
|
|
|
17
13
|
|
|
18
14
|
## Reference
|
|
19
15
|
|
|
20
|
-
A full reference for this library is available [here](
|
|
16
|
+
A full reference for this library is available [here](https://github.com/cartesia-ai/cartesia-js/blob/HEAD/./reference.md).
|
|
21
17
|
|
|
22
18
|
## Usage
|
|
23
19
|
|
|
24
|
-
### Instantiation
|
|
25
|
-
|
|
26
20
|
Instantiate and use the client with the following:
|
|
27
21
|
|
|
28
22
|
```typescript
|
|
29
23
|
import { CartesiaClient } from "@cartesia/cartesia-js";
|
|
30
|
-
import process from "node:process"
|
|
31
|
-
import fs from "node:fs"
|
|
32
|
-
|
|
33
|
-
// Set up the client.
|
|
34
|
-
const client = new CartesiaClient({ apiKey: process.env.CARTESIA_API_KEY });
|
|
35
|
-
|
|
36
|
-
// Call the TTS API's bytes endpoint, which returns binary audio data as an ArrayBuffer.
|
|
37
|
-
const response = await client.tts.bytes({
|
|
38
|
-
modelId: "sonic-2",
|
|
39
|
-
transcript: "Hello, world!",
|
|
40
|
-
voice: {
|
|
41
|
-
mode: "id",
|
|
42
|
-
id: "694f9389-aac1-45b6-b726-9d9369183238",
|
|
43
|
-
},
|
|
44
|
-
language: "en",
|
|
45
|
-
outputFormat: {
|
|
46
|
-
container: "wav",
|
|
47
|
-
sampleRate: 44100,
|
|
48
|
-
encoding: "pcm_f32le",
|
|
49
|
-
},
|
|
50
|
-
});
|
|
51
|
-
|
|
52
|
-
// Write the response to a file.
|
|
53
|
-
fs.writeFileSync("sonic.wav", new Uint8Array(response));
|
|
54
|
-
```
|
|
55
|
-
|
|
56
|
-
### TTS over WebSocket
|
|
57
|
-
|
|
58
|
-
```js
|
|
59
|
-
import { CartesiaClient } from "@cartesia/cartesia-js";
|
|
60
|
-
|
|
61
|
-
const cartesia = new CartesiaClient({
|
|
62
|
-
apiKey: process.env.CARTESIA_API_KEY,
|
|
63
|
-
});
|
|
64
24
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
sampleRate: 44100,
|
|
70
|
-
});
|
|
71
|
-
|
|
72
|
-
// Create a stream.
|
|
73
|
-
const response = await websocket.send({
|
|
74
|
-
modelId: "sonic-2",
|
|
75
|
-
voice: {
|
|
76
|
-
mode: "id",
|
|
77
|
-
id: "a0e99841-438c-4a64-b679-ae501e7d6091",
|
|
25
|
+
const client = new CartesiaClient({ apiKey: "YOUR_API_KEY" });
|
|
26
|
+
await client.auth.accessToken({
|
|
27
|
+
grants: {
|
|
28
|
+
stt: true,
|
|
78
29
|
},
|
|
79
|
-
|
|
80
|
-
// The WebSocket sets output_format on your behalf.
|
|
81
|
-
});
|
|
82
|
-
|
|
83
|
-
// Access the raw messages from the WebSocket.
|
|
84
|
-
response.on("message", (message) => {
|
|
85
|
-
// Raw message.
|
|
86
|
-
console.log("Received message:", message);
|
|
30
|
+
expiresIn: 60,
|
|
87
31
|
});
|
|
88
|
-
|
|
89
|
-
// You can also access messages using a for-await-of loop.
|
|
90
|
-
for await (const message of response.events("message")) {
|
|
91
|
-
// Raw message.
|
|
92
|
-
console.log("Received message:", message);
|
|
93
|
-
}
|
|
94
|
-
```
|
|
95
|
-
|
|
96
|
-
#### Input Streaming with Contexts
|
|
97
|
-
|
|
98
|
-
```js
|
|
99
|
-
const contextOptions = {
|
|
100
|
-
contextId: "my-context",
|
|
101
|
-
modelId: "sonic-2",
|
|
102
|
-
voice: {
|
|
103
|
-
mode: "id",
|
|
104
|
-
id: "a0e99841-438c-4a64-b679-ae501e7d6091",
|
|
105
|
-
},
|
|
106
|
-
};
|
|
107
|
-
|
|
108
|
-
// Initial request on the context uses websocket.send().
|
|
109
|
-
// This response object will aggregate the results of all the inputs sent on the context.
|
|
110
|
-
const response = await websocket.send({
|
|
111
|
-
...contextOptions,
|
|
112
|
-
transcript: "Hello, world!",
|
|
113
|
-
});
|
|
114
|
-
|
|
115
|
-
// Subsequent requests on the same context use websocket.continue().
|
|
116
|
-
await websocket.continue({
|
|
117
|
-
...contextOptions,
|
|
118
|
-
transcript: " How are you today?",
|
|
119
|
-
});
|
|
120
|
-
```
|
|
121
|
-
|
|
122
|
-
See the [input streaming docs](https://docs.cartesia.ai/reference/web-socket/stream-speech/working-with-web-sockets#input-streaming-with-contexts) for more information.
|
|
123
|
-
|
|
124
|
-
### Playing audio in the browser
|
|
125
|
-
|
|
126
|
-
(The `WebPlayer` class only supports playing audio in the browser and the raw PCM format with fp32le encoding.)
|
|
127
|
-
|
|
128
|
-
```js
|
|
129
|
-
// If you're using the client in the browser, you can control audio playback using our WebPlayer:
|
|
130
|
-
import { WebPlayer } from "@cartesia/cartesia-js";
|
|
131
|
-
|
|
132
|
-
console.log("Playing stream...");
|
|
133
|
-
|
|
134
|
-
// Create a Player object.
|
|
135
|
-
const player = new WebPlayer();
|
|
136
|
-
|
|
137
|
-
// Play the audio. (`response` includes a custom Source object that the Player can play.)
|
|
138
|
-
// The call resolves when the audio finishes playing.
|
|
139
|
-
await player.play(response.source);
|
|
140
|
-
|
|
141
|
-
console.log("Done playing.");
|
|
142
32
|
```
|
|
143
33
|
|
|
144
34
|
## Speech-to-Text (STT)
|
|
@@ -152,11 +42,14 @@ async function streamingSTTExample() {
|
|
|
152
42
|
apiKey: process.env.CARTESIA_API_KEY,
|
|
153
43
|
});
|
|
154
44
|
|
|
45
|
+
// Create websocket connection with endpointing parameters
|
|
155
46
|
const sttWs = client.stt.websocket({
|
|
156
47
|
model: "ink-whisper",
|
|
157
|
-
language: "en",
|
|
158
|
-
encoding: "pcm_s16le",
|
|
159
|
-
sampleRate: 16000,
|
|
48
|
+
language: "en", // Language of your audio
|
|
49
|
+
encoding: "pcm_s16le", // Audio encoding format (required)
|
|
50
|
+
sampleRate: 16000, // Audio sample rate (required)
|
|
51
|
+
minVolume: 0.1, // Volume threshold for voice activity detection (0.0-1.0)
|
|
52
|
+
maxSilenceDurationSecs: 2.0, // Maximum silence duration before endpointing
|
|
160
53
|
});
|
|
161
54
|
|
|
162
55
|
// Concurrent audio sending
|
|
@@ -169,10 +62,7 @@ async function streamingSTTExample() {
|
|
|
169
62
|
|
|
170
63
|
for (let i = 0; i < audioBuffer.length; i += chunkSize) {
|
|
171
64
|
const chunk = audioBuffer.subarray(i, i + chunkSize);
|
|
172
|
-
const arrayBuffer = chunk.buffer.slice(
|
|
173
|
-
chunk.byteOffset,
|
|
174
|
-
chunk.byteOffset + chunk.byteLength,
|
|
175
|
-
);
|
|
65
|
+
const arrayBuffer = chunk.buffer.slice(chunk.byteOffset, chunk.byteOffset + chunk.byteLength);
|
|
176
66
|
|
|
177
67
|
await sttWs.send(arrayBuffer);
|
|
178
68
|
console.log(`Sent chunk ${Math.floor(i / chunkSize) + 1}`);
|
|
@@ -188,7 +78,7 @@ async function streamingSTTExample() {
|
|
|
188
78
|
}
|
|
189
79
|
}
|
|
190
80
|
|
|
191
|
-
// Concurrent transcript receiving
|
|
81
|
+
// Concurrent transcript receiving with word-level timestamps
|
|
192
82
|
async function receiveTranscripts(): Promise<string> {
|
|
193
83
|
return new Promise((resolve) => {
|
|
194
84
|
let fullTranscript = "";
|
|
@@ -198,6 +88,14 @@ async function streamingSTTExample() {
|
|
|
198
88
|
const status = result.isFinal ? "FINAL" : "INTERIM";
|
|
199
89
|
console.log(`[${status}] "${result.text}"`);
|
|
200
90
|
|
|
91
|
+
// Handle word-level timestamps if available
|
|
92
|
+
if (result.words && result.words.length > 0) {
|
|
93
|
+
console.log("Word-level timestamps:");
|
|
94
|
+
result.words.forEach((word) => {
|
|
95
|
+
console.log(` "${word.word}": ${word.start.toFixed(2)}s - ${word.end.toFixed(2)}s`);
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
|
|
201
99
|
if (result.isFinal) {
|
|
202
100
|
fullTranscript += `${result.text} `;
|
|
203
101
|
}
|
|
@@ -219,10 +117,7 @@ async function streamingSTTExample() {
|
|
|
219
117
|
console.log("Starting STT processing...");
|
|
220
118
|
|
|
221
119
|
// Run audio sending and transcript receiving concurrently
|
|
222
|
-
const [, finalTranscript] = await Promise.all([
|
|
223
|
-
sendAudio(),
|
|
224
|
-
receiveTranscripts(),
|
|
225
|
-
]);
|
|
120
|
+
const [, finalTranscript] = await Promise.all([sendAudio(), receiveTranscripts()]);
|
|
226
121
|
|
|
227
122
|
console.log(`\nFinal transcript: ${finalTranscript}`);
|
|
228
123
|
|
|
@@ -249,7 +144,7 @@ following namespace:
|
|
|
249
144
|
```typescript
|
|
250
145
|
import { Cartesia } from "@cartesia/cartesia-js";
|
|
251
146
|
|
|
252
|
-
const request: Cartesia.
|
|
147
|
+
const request: Cartesia.InfillBytesRequest = {
|
|
253
148
|
...
|
|
254
149
|
};
|
|
255
150
|
```
|
|
@@ -263,7 +158,7 @@ will be thrown.
|
|
|
263
158
|
import { CartesiaError } from "@cartesia/cartesia-js";
|
|
264
159
|
|
|
265
160
|
try {
|
|
266
|
-
await client.
|
|
161
|
+
await client.auth.accessToken(...);
|
|
267
162
|
} catch (err) {
|
|
268
163
|
if (err instanceof CartesiaError) {
|
|
269
164
|
console.log(err.statusCode);
|
|
@@ -273,8 +168,434 @@ try {
|
|
|
273
168
|
}
|
|
274
169
|
```
|
|
275
170
|
|
|
171
|
+
## Binary Response
|
|
172
|
+
|
|
173
|
+
You can consume binary data from endpoints using the `BinaryResponse` type which lets you choose how to consume the data:
|
|
174
|
+
|
|
175
|
+
```typescript
|
|
176
|
+
const response = await client.agents.downloadCallAudio(...);
|
|
177
|
+
const stream: ReadableStream<Uint8Array> = response.stream();
|
|
178
|
+
// const arrayBuffer: ArrayBuffer = await response.arrayBuffer();
|
|
179
|
+
// const blob: Blob = response.blob();
|
|
180
|
+
// const bytes: Uint8Array = response.bytes();
|
|
181
|
+
// You can only use the response body once, so you must choose one of the above methods.
|
|
182
|
+
// If you want to check if the response body has been used, you can use the following property.
|
|
183
|
+
const bodyUsed = response.bodyUsed;
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
<details>
|
|
187
|
+
<summary>Save binary response to a file</summary>
|
|
188
|
+
|
|
189
|
+
<blockquote>
|
|
190
|
+
<details>
|
|
191
|
+
<summary>Node.js</summary>
|
|
192
|
+
|
|
193
|
+
<blockquote>
|
|
194
|
+
<details>
|
|
195
|
+
<summary>ReadableStream (most-efficient)</summary>
|
|
196
|
+
|
|
197
|
+
```ts
|
|
198
|
+
import { createWriteStream } from 'fs';
|
|
199
|
+
import { Readable } from 'stream';
|
|
200
|
+
import { pipeline } from 'stream/promises';
|
|
201
|
+
|
|
202
|
+
const response = await client.agents.downloadCallAudio(...);
|
|
203
|
+
|
|
204
|
+
const stream = response.stream();
|
|
205
|
+
const nodeStream = Readable.fromWeb(stream);
|
|
206
|
+
const writeStream = createWriteStream('path/to/file');
|
|
207
|
+
|
|
208
|
+
await pipeline(nodeStream, writeStream);
|
|
209
|
+
```
|
|
210
|
+
|
|
211
|
+
</details>
|
|
212
|
+
</blockquote>
|
|
213
|
+
|
|
214
|
+
<blockquote>
|
|
215
|
+
<details>
|
|
216
|
+
<summary>ArrayBuffer</summary>
|
|
217
|
+
|
|
218
|
+
```ts
|
|
219
|
+
import { writeFile } from 'fs/promises';
|
|
220
|
+
|
|
221
|
+
const response = await client.agents.downloadCallAudio(...);
|
|
222
|
+
|
|
223
|
+
const arrayBuffer = await response.arrayBuffer();
|
|
224
|
+
await writeFile('path/to/file', Buffer.from(arrayBuffer));
|
|
225
|
+
```
|
|
226
|
+
|
|
227
|
+
</details>
|
|
228
|
+
</blockquote>
|
|
229
|
+
|
|
230
|
+
<blockquote>
|
|
231
|
+
<details>
|
|
232
|
+
<summary>Blob</summary>
|
|
233
|
+
|
|
234
|
+
```ts
|
|
235
|
+
import { writeFile } from 'fs/promises';
|
|
236
|
+
|
|
237
|
+
const response = await client.agents.downloadCallAudio(...);
|
|
238
|
+
|
|
239
|
+
const blob = await response.blob();
|
|
240
|
+
const arrayBuffer = await blob.arrayBuffer();
|
|
241
|
+
await writeFile('output.bin', Buffer.from(arrayBuffer));
|
|
242
|
+
```
|
|
243
|
+
|
|
244
|
+
</details>
|
|
245
|
+
</blockquote>
|
|
246
|
+
|
|
247
|
+
<blockquote>
|
|
248
|
+
<details>
|
|
249
|
+
<summary>Bytes (UIntArray8)</summary>
|
|
250
|
+
|
|
251
|
+
```ts
|
|
252
|
+
import { writeFile } from 'fs/promises';
|
|
253
|
+
|
|
254
|
+
const response = await client.agents.downloadCallAudio(...);
|
|
255
|
+
|
|
256
|
+
const bytes = await response.bytes();
|
|
257
|
+
await writeFile('path/to/file', bytes);
|
|
258
|
+
```
|
|
259
|
+
|
|
260
|
+
</details>
|
|
261
|
+
</blockquote>
|
|
262
|
+
|
|
263
|
+
</details>
|
|
264
|
+
</blockquote>
|
|
265
|
+
|
|
266
|
+
<blockquote>
|
|
267
|
+
<details>
|
|
268
|
+
<summary>Bun</summary>
|
|
269
|
+
|
|
270
|
+
<blockquote>
|
|
271
|
+
<details>
|
|
272
|
+
<summary>ReadableStream (most-efficient)</summary>
|
|
273
|
+
|
|
274
|
+
```ts
|
|
275
|
+
const response = await client.agents.downloadCallAudio(...);
|
|
276
|
+
|
|
277
|
+
const stream = response.stream();
|
|
278
|
+
await Bun.write('path/to/file', stream);
|
|
279
|
+
```
|
|
280
|
+
|
|
281
|
+
</details>
|
|
282
|
+
</blockquote>
|
|
283
|
+
|
|
284
|
+
<blockquote>
|
|
285
|
+
<details>
|
|
286
|
+
<summary>ArrayBuffer</summary>
|
|
287
|
+
|
|
288
|
+
```ts
|
|
289
|
+
const response = await client.agents.downloadCallAudio(...);
|
|
290
|
+
|
|
291
|
+
const arrayBuffer = await response.arrayBuffer();
|
|
292
|
+
await Bun.write('path/to/file', arrayBuffer);
|
|
293
|
+
```
|
|
294
|
+
|
|
295
|
+
</details>
|
|
296
|
+
</blockquote>
|
|
297
|
+
|
|
298
|
+
<blockquote>
|
|
299
|
+
<details>
|
|
300
|
+
<summary>Blob</summary>
|
|
301
|
+
|
|
302
|
+
```ts
|
|
303
|
+
const response = await client.agents.downloadCallAudio(...);
|
|
304
|
+
|
|
305
|
+
const blob = await response.blob();
|
|
306
|
+
await Bun.write('path/to/file', blob);
|
|
307
|
+
```
|
|
308
|
+
|
|
309
|
+
</details>
|
|
310
|
+
</blockquote>
|
|
311
|
+
|
|
312
|
+
<blockquote>
|
|
313
|
+
<details>
|
|
314
|
+
<summary>Bytes (UIntArray8)</summary>
|
|
315
|
+
|
|
316
|
+
```ts
|
|
317
|
+
const response = await client.agents.downloadCallAudio(...);
|
|
318
|
+
|
|
319
|
+
const bytes = await response.bytes();
|
|
320
|
+
await Bun.write('path/to/file', bytes);
|
|
321
|
+
```
|
|
322
|
+
|
|
323
|
+
</details>
|
|
324
|
+
</blockquote>
|
|
325
|
+
|
|
326
|
+
</details>
|
|
327
|
+
</blockquote>
|
|
328
|
+
|
|
329
|
+
<blockquote>
|
|
330
|
+
<details>
|
|
331
|
+
<summary>Deno</summary>
|
|
332
|
+
|
|
333
|
+
<blockquote>
|
|
334
|
+
<details>
|
|
335
|
+
<summary>ReadableStream (most-efficient)</summary>
|
|
336
|
+
|
|
337
|
+
```ts
|
|
338
|
+
const response = await client.agents.downloadCallAudio(...);
|
|
339
|
+
|
|
340
|
+
const stream = response.stream();
|
|
341
|
+
const file = await Deno.open('path/to/file', { write: true, create: true });
|
|
342
|
+
await stream.pipeTo(file.writable);
|
|
343
|
+
```
|
|
344
|
+
|
|
345
|
+
</details>
|
|
346
|
+
</blockquote>
|
|
347
|
+
|
|
348
|
+
<blockquote>
|
|
349
|
+
<details>
|
|
350
|
+
<summary>ArrayBuffer</summary>
|
|
351
|
+
|
|
352
|
+
```ts
|
|
353
|
+
const response = await client.agents.downloadCallAudio(...);
|
|
354
|
+
|
|
355
|
+
const arrayBuffer = await response.arrayBuffer();
|
|
356
|
+
await Deno.writeFile('path/to/file', new Uint8Array(arrayBuffer));
|
|
357
|
+
```
|
|
358
|
+
|
|
359
|
+
</details>
|
|
360
|
+
</blockquote>
|
|
361
|
+
|
|
362
|
+
<blockquote>
|
|
363
|
+
<details>
|
|
364
|
+
<summary>Blob</summary>
|
|
365
|
+
|
|
366
|
+
```ts
|
|
367
|
+
const response = await client.agents.downloadCallAudio(...);
|
|
368
|
+
|
|
369
|
+
const blob = await response.blob();
|
|
370
|
+
const arrayBuffer = await blob.arrayBuffer();
|
|
371
|
+
await Deno.writeFile('path/to/file', new Uint8Array(arrayBuffer));
|
|
372
|
+
```
|
|
373
|
+
|
|
374
|
+
</details>
|
|
375
|
+
</blockquote>
|
|
376
|
+
|
|
377
|
+
<blockquote>
|
|
378
|
+
<details>
|
|
379
|
+
<summary>Bytes (UIntArray8)</summary>
|
|
380
|
+
|
|
381
|
+
```ts
|
|
382
|
+
const response = await client.agents.downloadCallAudio(...);
|
|
383
|
+
|
|
384
|
+
const bytes = await response.bytes();
|
|
385
|
+
await Deno.writeFile('path/to/file', bytes);
|
|
386
|
+
```
|
|
387
|
+
|
|
388
|
+
</details>
|
|
389
|
+
</blockquote>
|
|
390
|
+
|
|
391
|
+
</details>
|
|
392
|
+
</blockquote>
|
|
393
|
+
|
|
394
|
+
<blockquote>
|
|
395
|
+
<details>
|
|
396
|
+
<summary>Browser</summary>
|
|
397
|
+
|
|
398
|
+
<blockquote>
|
|
399
|
+
<details>
|
|
400
|
+
<summary>Blob (most-efficient)</summary>
|
|
401
|
+
|
|
402
|
+
```ts
|
|
403
|
+
const response = await client.agents.downloadCallAudio(...);
|
|
404
|
+
|
|
405
|
+
const blob = await response.blob();
|
|
406
|
+
const url = URL.createObjectURL(blob);
|
|
407
|
+
|
|
408
|
+
// trigger download
|
|
409
|
+
const a = document.createElement('a');
|
|
410
|
+
a.href = url;
|
|
411
|
+
a.download = 'filename';
|
|
412
|
+
a.click();
|
|
413
|
+
URL.revokeObjectURL(url);
|
|
414
|
+
```
|
|
415
|
+
|
|
416
|
+
</details>
|
|
417
|
+
</blockquote>
|
|
418
|
+
|
|
419
|
+
<blockquote>
|
|
420
|
+
<details>
|
|
421
|
+
<summary>ReadableStream</summary>
|
|
422
|
+
|
|
423
|
+
```ts
|
|
424
|
+
const response = await client.agents.downloadCallAudio(...);
|
|
425
|
+
|
|
426
|
+
const stream = response.stream();
|
|
427
|
+
const reader = stream.getReader();
|
|
428
|
+
const chunks = [];
|
|
429
|
+
|
|
430
|
+
while (true) {
|
|
431
|
+
const { done, value } = await reader.read();
|
|
432
|
+
if (done) break;
|
|
433
|
+
chunks.push(value);
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
const blob = new Blob(chunks);
|
|
437
|
+
const url = URL.createObjectURL(blob);
|
|
438
|
+
|
|
439
|
+
// trigger download
|
|
440
|
+
const a = document.createElement('a');
|
|
441
|
+
a.href = url;
|
|
442
|
+
a.download = 'filename';
|
|
443
|
+
a.click();
|
|
444
|
+
URL.revokeObjectURL(url);
|
|
445
|
+
```
|
|
446
|
+
|
|
447
|
+
</details>
|
|
448
|
+
</blockquote>
|
|
449
|
+
|
|
450
|
+
<blockquote>
|
|
451
|
+
<details>
|
|
452
|
+
<summary>ArrayBuffer</summary>
|
|
453
|
+
|
|
454
|
+
```ts
|
|
455
|
+
const response = await client.agents.downloadCallAudio(...);
|
|
456
|
+
|
|
457
|
+
const arrayBuffer = await response.arrayBuffer();
|
|
458
|
+
const blob = new Blob([arrayBuffer]);
|
|
459
|
+
const url = URL.createObjectURL(blob);
|
|
460
|
+
|
|
461
|
+
// trigger download
|
|
462
|
+
const a = document.createElement('a');
|
|
463
|
+
a.href = url;
|
|
464
|
+
a.download = 'filename';
|
|
465
|
+
a.click();
|
|
466
|
+
URL.revokeObjectURL(url);
|
|
467
|
+
```
|
|
468
|
+
|
|
469
|
+
</details>
|
|
470
|
+
</blockquote>
|
|
471
|
+
|
|
472
|
+
<blockquote>
|
|
473
|
+
<details>
|
|
474
|
+
<summary>Bytes (UIntArray8)</summary>
|
|
475
|
+
|
|
476
|
+
```ts
|
|
477
|
+
const response = await client.agents.downloadCallAudio(...);
|
|
478
|
+
|
|
479
|
+
const bytes = await response.bytes();
|
|
480
|
+
const blob = new Blob([bytes]);
|
|
481
|
+
const url = URL.createObjectURL(blob);
|
|
482
|
+
|
|
483
|
+
// trigger download
|
|
484
|
+
const a = document.createElement('a');
|
|
485
|
+
a.href = url;
|
|
486
|
+
a.download = 'filename';
|
|
487
|
+
a.click();
|
|
488
|
+
URL.revokeObjectURL(url);
|
|
489
|
+
```
|
|
490
|
+
|
|
491
|
+
</details>
|
|
492
|
+
</blockquote>
|
|
493
|
+
|
|
494
|
+
</details>
|
|
495
|
+
</blockquote>
|
|
496
|
+
|
|
497
|
+
</details>
|
|
498
|
+
</blockquote>
|
|
499
|
+
|
|
500
|
+
<details>
|
|
501
|
+
<summary>Convert binary response to text</summary>
|
|
502
|
+
|
|
503
|
+
<blockquote>
|
|
504
|
+
<details>
|
|
505
|
+
<summary>ReadableStream</summary>
|
|
506
|
+
|
|
507
|
+
```ts
|
|
508
|
+
const response = await client.agents.downloadCallAudio(...);
|
|
509
|
+
|
|
510
|
+
const stream = response.stream();
|
|
511
|
+
const text = await new Response(stream).text();
|
|
512
|
+
```
|
|
513
|
+
|
|
514
|
+
</details>
|
|
515
|
+
</blockquote>
|
|
516
|
+
|
|
517
|
+
<blockquote>
|
|
518
|
+
<details>
|
|
519
|
+
<summary>ArrayBuffer</summary>
|
|
520
|
+
|
|
521
|
+
```ts
|
|
522
|
+
const response = await client.agents.downloadCallAudio(...);
|
|
523
|
+
|
|
524
|
+
const arrayBuffer = await response.arrayBuffer();
|
|
525
|
+
const text = new TextDecoder().decode(arrayBuffer);
|
|
526
|
+
```
|
|
527
|
+
|
|
528
|
+
</details>
|
|
529
|
+
</blockquote>
|
|
530
|
+
|
|
531
|
+
<blockquote>
|
|
532
|
+
<details>
|
|
533
|
+
<summary>Blob</summary>
|
|
534
|
+
|
|
535
|
+
```ts
|
|
536
|
+
const response = await client.agents.downloadCallAudio(...);
|
|
537
|
+
|
|
538
|
+
const blob = await response.blob();
|
|
539
|
+
const text = await blob.text();
|
|
540
|
+
```
|
|
541
|
+
|
|
542
|
+
</details>
|
|
543
|
+
</blockquote>
|
|
544
|
+
|
|
545
|
+
<blockquote>
|
|
546
|
+
<details>
|
|
547
|
+
<summary>Bytes (UIntArray8)</summary>
|
|
548
|
+
|
|
549
|
+
```ts
|
|
550
|
+
const response = await client.agents.downloadCallAudio(...);
|
|
551
|
+
|
|
552
|
+
const bytes = await response.bytes();
|
|
553
|
+
const text = new TextDecoder().decode(bytes);
|
|
554
|
+
```
|
|
555
|
+
|
|
556
|
+
</details>
|
|
557
|
+
</blockquote>
|
|
558
|
+
|
|
559
|
+
</details>
|
|
560
|
+
|
|
561
|
+
## Pagination
|
|
562
|
+
|
|
563
|
+
List endpoints are paginated. The SDK provides an iterator so that you can simply loop over the items:
|
|
564
|
+
|
|
565
|
+
```typescript
|
|
566
|
+
import { CartesiaClient } from "@cartesia/cartesia-js";
|
|
567
|
+
|
|
568
|
+
const client = new CartesiaClient({ token: "YOUR_TOKEN" });
|
|
569
|
+
const response = await client.agents.listCalls({
|
|
570
|
+
agentId: "agent_id",
|
|
571
|
+
});
|
|
572
|
+
for await (const item of response) {
|
|
573
|
+
console.log(item);
|
|
574
|
+
}
|
|
575
|
+
|
|
576
|
+
// Or you can manually iterate page-by-page
|
|
577
|
+
let page = await client.agents.listCalls({
|
|
578
|
+
agentId: "agent_id",
|
|
579
|
+
});
|
|
580
|
+
while (page.hasNextPage()) {
|
|
581
|
+
page = page.getNextPage();
|
|
582
|
+
}
|
|
583
|
+
```
|
|
584
|
+
|
|
276
585
|
## Advanced
|
|
277
586
|
|
|
587
|
+
### Additional Headers
|
|
588
|
+
|
|
589
|
+
If you would like to send additional headers as part of the request, use the `headers` request option.
|
|
590
|
+
|
|
591
|
+
```typescript
|
|
592
|
+
const response = await client.auth.accessToken(..., {
|
|
593
|
+
headers: {
|
|
594
|
+
'X-Custom-Header': 'custom value'
|
|
595
|
+
}
|
|
596
|
+
});
|
|
597
|
+
```
|
|
598
|
+
|
|
278
599
|
### Retries
|
|
279
600
|
|
|
280
601
|
The SDK is instrumented with automatic retries with exponential backoff. A request will be retried as long
|
|
@@ -283,14 +604,14 @@ retry limit (default: 2).
|
|
|
283
604
|
|
|
284
605
|
A request is deemed retriable when any of the following HTTP status codes is returned:
|
|
285
606
|
|
|
286
|
-
-
|
|
287
|
-
-
|
|
288
|
-
-
|
|
607
|
+
- [408](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/408) (Timeout)
|
|
608
|
+
- [429](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/429) (Too Many Requests)
|
|
609
|
+
- [5XX](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/500) (Internal Server Errors)
|
|
289
610
|
|
|
290
611
|
Use the `maxRetries` request option to configure this behavior.
|
|
291
612
|
|
|
292
613
|
```typescript
|
|
293
|
-
const response = await client.
|
|
614
|
+
const response = await client.auth.accessToken(..., {
|
|
294
615
|
maxRetries: 0 // override maxRetries at the request level
|
|
295
616
|
});
|
|
296
617
|
```
|
|
@@ -300,7 +621,7 @@ const response = await client.tts.bytes(..., {
|
|
|
300
621
|
The SDK defaults to a 60 second timeout. Use the `timeoutInSeconds` option to configure this behavior.
|
|
301
622
|
|
|
302
623
|
```typescript
|
|
303
|
-
const response = await client.
|
|
624
|
+
const response = await client.auth.accessToken(..., {
|
|
304
625
|
timeoutInSeconds: 30 // override timeout to 30s
|
|
305
626
|
});
|
|
306
627
|
```
|
|
@@ -311,7 +632,7 @@ The SDK allows users to abort requests at any point by passing in an abort signa
|
|
|
311
632
|
|
|
312
633
|
```typescript
|
|
313
634
|
const controller = new AbortController();
|
|
314
|
-
const response = await client.
|
|
635
|
+
const response = await client.auth.accessToken(..., {
|
|
315
636
|
abortSignal: controller.signal
|
|
316
637
|
});
|
|
317
638
|
controller.abort(); // aborts the request
|
|
@@ -322,12 +643,12 @@ controller.abort(); // aborts the request
|
|
|
322
643
|
The SDK defaults to `node-fetch` but will use the global fetch client if present. The SDK works in the following
|
|
323
644
|
runtimes:
|
|
324
645
|
|
|
325
|
-
-
|
|
326
|
-
-
|
|
327
|
-
-
|
|
328
|
-
-
|
|
329
|
-
-
|
|
330
|
-
-
|
|
646
|
+
- Node.js 18+
|
|
647
|
+
- Vercel
|
|
648
|
+
- Cloudflare Workers
|
|
649
|
+
- Deno v1.25+
|
|
650
|
+
- Bun 1.0+
|
|
651
|
+
- React Native
|
|
331
652
|
|
|
332
653
|
### Customizing Fetch Client
|
|
333
654
|
|
|
@@ -352,3 +673,7 @@ a proof of concept, but know that we will not be able to merge it as-is. We sugg
|
|
|
352
673
|
an issue first to discuss with us!
|
|
353
674
|
|
|
354
675
|
On the other hand, contributions to the README are always very welcome!
|
|
676
|
+
|
|
677
|
+
## Documentation
|
|
678
|
+
|
|
679
|
+
API reference documentation is available [here](https://docs.cartesia.ai/).
|