@cartesia/cartesia-js 2.2.2 → 2.2.4
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/README.md +58 -7
- package/api/resources/apiStatus/client/Client.js +1 -1
- package/api/resources/auth/client/Client.js +1 -1
- package/api/resources/index.d.ts +2 -0
- package/api/resources/index.js +3 -1
- package/api/resources/infill/client/Client.js +1 -1
- package/api/resources/stt/client/Client.d.ts +28 -0
- package/api/resources/stt/client/Client.js +58 -0
- package/api/resources/stt/client/index.d.ts +1 -0
- package/api/resources/stt/client/index.js +5 -0
- package/api/resources/stt/index.d.ts +1 -0
- package/api/resources/stt/index.js +17 -0
- package/api/resources/stt/types/DoneMessage.d.ts +10 -0
- package/api/resources/stt/types/DoneMessage.js +5 -0
- package/api/resources/stt/types/ErrorMessage.d.ts +9 -0
- package/api/resources/stt/types/ErrorMessage.js +5 -0
- package/api/resources/stt/types/FlushDoneMessage.d.ts +10 -0
- package/api/resources/stt/types/FlushDoneMessage.js +5 -0
- package/api/resources/stt/types/StreamingTranscriptionResponse.d.ts +22 -0
- package/api/resources/stt/types/StreamingTranscriptionResponse.js +5 -0
- package/api/resources/stt/types/SttEncoding.d.ts +15 -0
- package/api/resources/stt/types/SttEncoding.js +9 -0
- package/api/resources/stt/types/TranscriptMessage.d.ts +19 -0
- package/api/resources/stt/types/TranscriptMessage.js +5 -0
- package/api/resources/stt/types/TranscriptionResponse.d.ts +11 -0
- package/api/resources/stt/types/TranscriptionResponse.js +5 -0
- package/api/resources/stt/types/index.d.ts +7 -0
- package/api/resources/stt/types/index.js +23 -0
- package/api/resources/tts/types/GenerationRequest.d.ts +1 -8
- package/api/resources/tts/types/ModelSpeed.d.ts +16 -0
- package/api/resources/tts/types/ModelSpeed.js +11 -0
- package/api/resources/tts/types/TtsRequest.d.ts +1 -8
- package/api/resources/tts/types/WebSocketTtsRequest.d.ts +1 -1
- package/api/resources/tts/types/index.d.ts +1 -0
- package/api/resources/tts/types/index.js +1 -0
- package/api/resources/voices/client/Client.d.ts +2 -5
- package/api/resources/voices/client/Client.js +12 -15
- package/api/resources/voices/client/requests/CloneVoiceRequest.d.ts +5 -8
- package/dist/api/resources/apiStatus/client/Client.js +1 -1
- package/dist/api/resources/auth/client/Client.js +1 -1
- package/dist/api/resources/index.d.ts +2 -0
- package/dist/api/resources/index.js +3 -1
- package/dist/api/resources/infill/client/Client.js +1 -1
- package/dist/api/resources/stt/client/Client.d.ts +28 -0
- package/dist/api/resources/stt/client/Client.js +58 -0
- package/dist/api/resources/stt/client/index.d.ts +1 -0
- package/dist/api/resources/stt/client/index.js +5 -0
- package/dist/api/resources/stt/index.d.ts +1 -0
- package/dist/api/resources/stt/index.js +17 -0
- package/dist/api/resources/stt/types/DoneMessage.d.ts +10 -0
- package/dist/api/resources/stt/types/DoneMessage.js +5 -0
- package/dist/api/resources/stt/types/ErrorMessage.d.ts +9 -0
- package/dist/api/resources/stt/types/ErrorMessage.js +5 -0
- package/dist/api/resources/stt/types/FlushDoneMessage.d.ts +10 -0
- package/dist/api/resources/stt/types/FlushDoneMessage.js +5 -0
- package/dist/api/resources/stt/types/StreamingTranscriptionResponse.d.ts +22 -0
- package/dist/api/resources/stt/types/StreamingTranscriptionResponse.js +5 -0
- package/dist/api/resources/stt/types/SttEncoding.d.ts +15 -0
- package/dist/api/resources/stt/types/SttEncoding.js +9 -0
- package/dist/api/resources/stt/types/TranscriptMessage.d.ts +19 -0
- package/dist/api/resources/stt/types/TranscriptMessage.js +5 -0
- package/dist/api/resources/stt/types/TranscriptionResponse.d.ts +11 -0
- package/dist/api/resources/stt/types/TranscriptionResponse.js +5 -0
- package/dist/api/resources/stt/types/index.d.ts +7 -0
- package/dist/api/resources/stt/types/index.js +23 -0
- package/dist/api/resources/tts/types/GenerationRequest.d.ts +1 -8
- package/dist/api/resources/tts/types/ModelSpeed.d.ts +16 -0
- package/dist/api/resources/tts/types/ModelSpeed.js +11 -0
- package/dist/api/resources/tts/types/TtsRequest.d.ts +1 -8
- package/dist/api/resources/tts/types/WebSocketTtsRequest.d.ts +1 -1
- package/dist/api/resources/tts/types/index.d.ts +1 -0
- package/dist/api/resources/tts/types/index.js +1 -0
- package/dist/api/resources/voices/client/Client.d.ts +2 -5
- package/dist/api/resources/voices/client/Client.js +12 -15
- package/dist/api/resources/voices/client/requests/CloneVoiceRequest.d.ts +5 -8
- package/dist/serialization/resources/index.d.ts +2 -0
- package/dist/serialization/resources/index.js +3 -1
- package/dist/serialization/resources/stt/index.d.ts +1 -0
- package/dist/serialization/resources/stt/index.js +17 -0
- package/dist/serialization/resources/stt/types/DoneMessage.d.ts +12 -0
- package/dist/serialization/resources/stt/types/DoneMessage.js +43 -0
- package/dist/serialization/resources/stt/types/ErrorMessage.d.ts +13 -0
- package/dist/serialization/resources/stt/types/ErrorMessage.js +44 -0
- package/dist/serialization/resources/stt/types/FlushDoneMessage.d.ts +12 -0
- package/dist/serialization/resources/stt/types/FlushDoneMessage.js +43 -0
- package/dist/serialization/resources/stt/types/StreamingTranscriptionResponse.d.ts +26 -0
- package/dist/serialization/resources/stt/types/StreamingTranscriptionResponse.js +55 -0
- package/dist/serialization/resources/stt/types/SttEncoding.d.ts +10 -0
- package/dist/serialization/resources/stt/types/SttEncoding.js +41 -0
- package/dist/serialization/resources/stt/types/TranscriptMessage.d.ts +16 -0
- package/dist/serialization/resources/stt/types/TranscriptMessage.js +47 -0
- package/dist/serialization/resources/stt/types/TranscriptionResponse.d.ts +14 -0
- package/dist/serialization/resources/stt/types/TranscriptionResponse.js +45 -0
- package/dist/serialization/resources/stt/types/index.d.ts +7 -0
- package/dist/serialization/resources/stt/types/index.js +23 -0
- package/dist/serialization/resources/tts/types/GenerationRequest.d.ts +2 -1
- package/dist/serialization/resources/tts/types/GenerationRequest.js +2 -1
- package/dist/serialization/resources/tts/types/ModelSpeed.d.ts +10 -0
- package/dist/serialization/resources/tts/types/ModelSpeed.js +41 -0
- package/dist/serialization/resources/tts/types/TtsRequest.d.ts +2 -1
- package/dist/serialization/resources/tts/types/TtsRequest.js +2 -1
- package/dist/serialization/resources/tts/types/WebSocketTtsRequest.d.ts +2 -1
- package/dist/serialization/resources/tts/types/WebSocketTtsRequest.js +2 -1
- package/dist/serialization/resources/tts/types/index.d.ts +1 -0
- package/dist/serialization/resources/tts/types/index.js +1 -0
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/dist/wrapper/Client.d.ts +3 -0
- package/dist/wrapper/Client.js +5 -0
- package/dist/wrapper/StreamingSTTClient.d.ts +6 -0
- package/dist/wrapper/StreamingSTTClient.js +17 -0
- package/dist/wrapper/SttWebsocket.d.ts +29 -0
- package/dist/wrapper/SttWebsocket.js +198 -0
- package/dist/wrapper/Websocket.d.ts +1 -1
- package/dist/wrapper/Websocket.js +24 -16
- package/package.json +1 -1
- package/reference.md +0 -1
- package/serialization/resources/index.d.ts +2 -0
- package/serialization/resources/index.js +3 -1
- package/serialization/resources/stt/index.d.ts +1 -0
- package/serialization/resources/stt/index.js +17 -0
- package/serialization/resources/stt/types/DoneMessage.d.ts +12 -0
- package/serialization/resources/stt/types/DoneMessage.js +43 -0
- package/serialization/resources/stt/types/ErrorMessage.d.ts +13 -0
- package/serialization/resources/stt/types/ErrorMessage.js +44 -0
- package/serialization/resources/stt/types/FlushDoneMessage.d.ts +12 -0
- package/serialization/resources/stt/types/FlushDoneMessage.js +43 -0
- package/serialization/resources/stt/types/StreamingTranscriptionResponse.d.ts +26 -0
- package/serialization/resources/stt/types/StreamingTranscriptionResponse.js +55 -0
- package/serialization/resources/stt/types/SttEncoding.d.ts +10 -0
- package/serialization/resources/stt/types/SttEncoding.js +41 -0
- package/serialization/resources/stt/types/TranscriptMessage.d.ts +16 -0
- package/serialization/resources/stt/types/TranscriptMessage.js +47 -0
- package/serialization/resources/stt/types/TranscriptionResponse.d.ts +14 -0
- package/serialization/resources/stt/types/TranscriptionResponse.js +45 -0
- package/serialization/resources/stt/types/index.d.ts +7 -0
- package/serialization/resources/stt/types/index.js +23 -0
- package/serialization/resources/tts/types/GenerationRequest.d.ts +2 -1
- package/serialization/resources/tts/types/GenerationRequest.js +2 -1
- package/serialization/resources/tts/types/ModelSpeed.d.ts +10 -0
- package/serialization/resources/tts/types/ModelSpeed.js +41 -0
- package/serialization/resources/tts/types/TtsRequest.d.ts +2 -1
- package/serialization/resources/tts/types/TtsRequest.js +2 -1
- package/serialization/resources/tts/types/WebSocketTtsRequest.d.ts +2 -1
- package/serialization/resources/tts/types/WebSocketTtsRequest.js +2 -1
- package/serialization/resources/tts/types/index.d.ts +1 -0
- package/serialization/resources/tts/types/index.js +1 -0
- package/version.d.ts +1 -1
- package/version.js +1 -1
- package/wrapper/Client.d.ts +3 -0
- package/wrapper/Client.js +5 -0
- package/wrapper/StreamingSTTClient.d.ts +6 -0
- package/wrapper/StreamingSTTClient.js +17 -0
- package/wrapper/SttWebsocket.d.ts +29 -0
- package/wrapper/SttWebsocket.js +198 -0
- package/wrapper/Websocket.d.ts +1 -1
- package/wrapper/Websocket.js +24 -16
|
@@ -6,6 +6,7 @@ import * as Cartesia from "../../../../api/index";
|
|
|
6
6
|
import * as core from "../../../../core";
|
|
7
7
|
import { OutputFormat } from "./OutputFormat";
|
|
8
8
|
import { TtsRequestVoiceSpecifier } from "./TtsRequestVoiceSpecifier";
|
|
9
|
+
import { ModelSpeed } from "./ModelSpeed";
|
|
9
10
|
export declare const WebSocketTtsRequest: core.serialization.ObjectSchema<serializers.WebSocketTtsRequest.Raw, Cartesia.WebSocketTtsRequest>;
|
|
10
11
|
export declare namespace WebSocketTtsRequest {
|
|
11
12
|
interface Raw {
|
|
@@ -21,6 +22,6 @@ export declare namespace WebSocketTtsRequest {
|
|
|
21
22
|
continue?: boolean | null;
|
|
22
23
|
context_id?: string | null;
|
|
23
24
|
max_buffer_delay_ms?: number | null;
|
|
24
|
-
|
|
25
|
+
speed?: ModelSpeed.Raw | null;
|
|
25
26
|
}
|
|
26
27
|
}
|
|
@@ -40,6 +40,7 @@ exports.WebSocketTtsRequest = void 0;
|
|
|
40
40
|
const core = __importStar(require("../../../../core"));
|
|
41
41
|
const OutputFormat_1 = require("./OutputFormat");
|
|
42
42
|
const TtsRequestVoiceSpecifier_1 = require("./TtsRequestVoiceSpecifier");
|
|
43
|
+
const ModelSpeed_1 = require("./ModelSpeed");
|
|
43
44
|
exports.WebSocketTtsRequest = core.serialization.object({
|
|
44
45
|
modelId: core.serialization.property("model_id", core.serialization.string()),
|
|
45
46
|
outputFormat: core.serialization.property("output_format", OutputFormat_1.OutputFormat.optional()),
|
|
@@ -53,5 +54,5 @@ exports.WebSocketTtsRequest = core.serialization.object({
|
|
|
53
54
|
continue: core.serialization.boolean().optional(),
|
|
54
55
|
contextId: core.serialization.property("context_id", core.serialization.string().optional()),
|
|
55
56
|
maxBufferDelayMs: core.serialization.property("max_buffer_delay_ms", core.serialization.number().optional()),
|
|
56
|
-
|
|
57
|
+
speed: ModelSpeed_1.ModelSpeed.optional(),
|
|
57
58
|
});
|
|
@@ -16,6 +16,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
17
|
__exportStar(require("./ContextId"), exports);
|
|
18
18
|
__exportStar(require("./FlushId"), exports);
|
|
19
|
+
__exportStar(require("./ModelSpeed"), exports);
|
|
19
20
|
__exportStar(require("./WebSocketBaseResponse"), exports);
|
|
20
21
|
__exportStar(require("./WebSocketResponse"), exports);
|
|
21
22
|
__exportStar(require("./WebSocketErrorResponse"), exports);
|
package/version.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare const SDK_VERSION = "2.2.
|
|
1
|
+
export declare const SDK_VERSION = "2.2.4";
|
package/version.js
CHANGED
package/wrapper/Client.d.ts
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
import { CartesiaClient as FernCartesiaClient } from "../Client";
|
|
2
2
|
import { StreamingTTSClient } from "./StreamingTTSClient";
|
|
3
|
+
import { StreamingSTTClient } from "./StreamingSTTClient";
|
|
3
4
|
export declare class CartesiaClient extends FernCartesiaClient {
|
|
5
|
+
protected _stt: StreamingSTTClient | undefined;
|
|
4
6
|
protected _tts: StreamingTTSClient | undefined;
|
|
7
|
+
get stt(): StreamingSTTClient;
|
|
5
8
|
get tts(): StreamingTTSClient;
|
|
6
9
|
}
|
package/wrapper/Client.js
CHANGED
|
@@ -3,7 +3,12 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.CartesiaClient = void 0;
|
|
4
4
|
const Client_1 = require("../Client");
|
|
5
5
|
const StreamingTTSClient_1 = require("./StreamingTTSClient");
|
|
6
|
+
const StreamingSTTClient_1 = require("./StreamingSTTClient");
|
|
6
7
|
class CartesiaClient extends Client_1.CartesiaClient {
|
|
8
|
+
get stt() {
|
|
9
|
+
var _a;
|
|
10
|
+
return ((_a = this._stt) !== null && _a !== void 0 ? _a : (this._stt = new StreamingSTTClient_1.StreamingSTTClient(this._options)));
|
|
11
|
+
}
|
|
7
12
|
get tts() {
|
|
8
13
|
var _a;
|
|
9
14
|
return ((_a = this._tts) !== null && _a !== void 0 ? _a : (this._tts = new StreamingTTSClient_1.StreamingTTSClient(this._options)));
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { Stt } from "../api/resources/stt/client/Client";
|
|
2
|
+
import SttWebsocket, { SttWebSocketOptions } from "./SttWebsocket";
|
|
3
|
+
export declare class StreamingSTTClient extends Stt {
|
|
4
|
+
constructor(options?: Stt.Options);
|
|
5
|
+
websocket(options?: SttWebSocketOptions): SttWebsocket;
|
|
6
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.StreamingSTTClient = void 0;
|
|
7
|
+
const Client_1 = require("../api/resources/stt/client/Client");
|
|
8
|
+
const SttWebsocket_1 = __importDefault(require("./SttWebsocket"));
|
|
9
|
+
class StreamingSTTClient extends Client_1.Stt {
|
|
10
|
+
constructor(options = {}) {
|
|
11
|
+
super(options);
|
|
12
|
+
}
|
|
13
|
+
websocket(options = {}) {
|
|
14
|
+
return new SttWebsocket_1.default(options, Object.assign({ cartesiaVersion: "2024-06-10" }, this._options));
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
exports.StreamingSTTClient = StreamingSTTClient;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { ReconnectingWebSocket, Options } from "../core/websocket";
|
|
2
|
+
import { Stt } from "../api/resources/stt/client/Client";
|
|
3
|
+
export interface SttWebSocketOptions {
|
|
4
|
+
model?: string;
|
|
5
|
+
language?: string;
|
|
6
|
+
encoding?: string;
|
|
7
|
+
sampleRate?: number;
|
|
8
|
+
}
|
|
9
|
+
export interface TranscriptionResult {
|
|
10
|
+
type: "transcript" | "flush_done" | "done" | "error";
|
|
11
|
+
requestId: string;
|
|
12
|
+
text?: string;
|
|
13
|
+
isFinal?: boolean;
|
|
14
|
+
duration?: number;
|
|
15
|
+
language?: string;
|
|
16
|
+
message?: string;
|
|
17
|
+
}
|
|
18
|
+
export default class SttWebsocket {
|
|
19
|
+
#private;
|
|
20
|
+
private readonly options;
|
|
21
|
+
socket?: ReconnectingWebSocket;
|
|
22
|
+
constructor({ model, language, encoding, sampleRate, }: SttWebSocketOptions, options: Stt.Options);
|
|
23
|
+
send(audioData: ArrayBuffer): Promise<void>;
|
|
24
|
+
finalize(): Promise<void>;
|
|
25
|
+
done(): Promise<void>;
|
|
26
|
+
onMessage(callback: (result: TranscriptionResult) => void): Promise<void>;
|
|
27
|
+
connect(connectOptions?: Options): Promise<void>;
|
|
28
|
+
disconnect(): void;
|
|
29
|
+
}
|
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
36
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
37
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
38
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
39
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
40
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
41
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
42
|
+
});
|
|
43
|
+
};
|
|
44
|
+
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
45
|
+
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
46
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
47
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
48
|
+
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
49
|
+
};
|
|
50
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
51
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
52
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
53
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
54
|
+
};
|
|
55
|
+
var _SttWebsocket_instances, _SttWebsocket_isConnected, _SttWebsocket_model, _SttWebsocket_language, _SttWebsocket_encoding, _SttWebsocket_sampleRate, _SttWebsocket_connectionPromise, _SttWebsocket_ensureConnected;
|
|
56
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
57
|
+
const core = __importStar(require("../core"));
|
|
58
|
+
const environments = __importStar(require("../environments"));
|
|
59
|
+
const websocket_1 = require("../core/websocket");
|
|
60
|
+
class SttWebsocket {
|
|
61
|
+
constructor({ model = "ink-whisper", language = "en", encoding = "pcm_s16le", sampleRate = 16000, }, options) {
|
|
62
|
+
_SttWebsocket_instances.add(this);
|
|
63
|
+
this.options = options;
|
|
64
|
+
_SttWebsocket_isConnected.set(this, false);
|
|
65
|
+
_SttWebsocket_model.set(this, void 0);
|
|
66
|
+
_SttWebsocket_language.set(this, void 0);
|
|
67
|
+
_SttWebsocket_encoding.set(this, void 0);
|
|
68
|
+
_SttWebsocket_sampleRate.set(this, void 0);
|
|
69
|
+
_SttWebsocket_connectionPromise.set(this, void 0);
|
|
70
|
+
__classPrivateFieldSet(this, _SttWebsocket_model, model, "f");
|
|
71
|
+
__classPrivateFieldSet(this, _SttWebsocket_language, language, "f");
|
|
72
|
+
__classPrivateFieldSet(this, _SttWebsocket_encoding, encoding, "f");
|
|
73
|
+
__classPrivateFieldSet(this, _SttWebsocket_sampleRate, sampleRate, "f");
|
|
74
|
+
}
|
|
75
|
+
send(audioData) {
|
|
76
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
77
|
+
var _a;
|
|
78
|
+
yield __classPrivateFieldGet(this, _SttWebsocket_instances, "m", _SttWebsocket_ensureConnected).call(this);
|
|
79
|
+
(_a = this.socket) === null || _a === void 0 ? void 0 : _a.send(audioData);
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
finalize() {
|
|
83
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
84
|
+
var _a;
|
|
85
|
+
yield __classPrivateFieldGet(this, _SttWebsocket_instances, "m", _SttWebsocket_ensureConnected).call(this);
|
|
86
|
+
(_a = this.socket) === null || _a === void 0 ? void 0 : _a.send("finalize");
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
done() {
|
|
90
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
91
|
+
var _a;
|
|
92
|
+
yield __classPrivateFieldGet(this, _SttWebsocket_instances, "m", _SttWebsocket_ensureConnected).call(this);
|
|
93
|
+
(_a = this.socket) === null || _a === void 0 ? void 0 : _a.send("done");
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
onMessage(callback) {
|
|
97
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
98
|
+
yield __classPrivateFieldGet(this, _SttWebsocket_instances, "m", _SttWebsocket_ensureConnected).call(this);
|
|
99
|
+
if (!this.socket) {
|
|
100
|
+
throw new Error("WebSocket failed to initialize after connection attempt.");
|
|
101
|
+
}
|
|
102
|
+
this.socket.addEventListener("message", (event) => {
|
|
103
|
+
try {
|
|
104
|
+
const data = JSON.parse(event.data);
|
|
105
|
+
const result = {
|
|
106
|
+
type: data.type,
|
|
107
|
+
requestId: data.request_id || "",
|
|
108
|
+
};
|
|
109
|
+
// Handle different message types according to stt.yml spec
|
|
110
|
+
if (data.type === "transcript") {
|
|
111
|
+
result.text = data.text || "";
|
|
112
|
+
result.isFinal = data.is_final || false;
|
|
113
|
+
result.duration = data.duration;
|
|
114
|
+
result.language = data.language;
|
|
115
|
+
}
|
|
116
|
+
else if (data.type === "flush_done") {
|
|
117
|
+
// Acknowledgment for finalize command
|
|
118
|
+
// Only requestId is needed for flush_done
|
|
119
|
+
}
|
|
120
|
+
else if (data.type === "done") {
|
|
121
|
+
// Acknowledgment for done command - session complete
|
|
122
|
+
// Only requestId is needed for done
|
|
123
|
+
}
|
|
124
|
+
else if (data.type === "error") {
|
|
125
|
+
result.message = data.message;
|
|
126
|
+
}
|
|
127
|
+
callback(result);
|
|
128
|
+
}
|
|
129
|
+
catch (error) {
|
|
130
|
+
callback({
|
|
131
|
+
type: "error",
|
|
132
|
+
requestId: "",
|
|
133
|
+
message: `Failed to parse message: ${error}`,
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
});
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
connect() {
|
|
140
|
+
return __awaiter(this, arguments, void 0, function* (connectOptions = {}) {
|
|
141
|
+
if (__classPrivateFieldGet(this, _SttWebsocket_isConnected, "f")) {
|
|
142
|
+
throw new Error("WebSocket is already connected.");
|
|
143
|
+
}
|
|
144
|
+
this.socket = new websocket_1.ReconnectingWebSocket(() => __awaiter(this, void 0, void 0, function* () {
|
|
145
|
+
var _a;
|
|
146
|
+
const baseUrl = ((_a = (yield core.Supplier.get(this.options.environment))) !== null && _a !== void 0 ? _a : environments.CartesiaEnvironment.Production).replace(/^https?:\/\//, "");
|
|
147
|
+
const params = {
|
|
148
|
+
model: __classPrivateFieldGet(this, _SttWebsocket_model, "f"),
|
|
149
|
+
cartesia_version: this.options.cartesiaVersion || "2024-06-10",
|
|
150
|
+
encoding: __classPrivateFieldGet(this, _SttWebsocket_encoding, "f"),
|
|
151
|
+
sample_rate: __classPrivateFieldGet(this, _SttWebsocket_sampleRate, "f").toString(),
|
|
152
|
+
};
|
|
153
|
+
if (__classPrivateFieldGet(this, _SttWebsocket_language, "f"))
|
|
154
|
+
params.language = __classPrivateFieldGet(this, _SttWebsocket_language, "f");
|
|
155
|
+
const apiKey = yield core.Supplier.get(this.options.apiKey);
|
|
156
|
+
if (apiKey) {
|
|
157
|
+
params.api_key = apiKey;
|
|
158
|
+
}
|
|
159
|
+
else if (connectOptions.accessToken) {
|
|
160
|
+
params.access_token = connectOptions.accessToken;
|
|
161
|
+
}
|
|
162
|
+
const queryString = Object.keys(params)
|
|
163
|
+
.map(key => `${encodeURIComponent(key)}=${encodeURIComponent(params[key])}`)
|
|
164
|
+
.join('&');
|
|
165
|
+
return `wss://${baseUrl}/stt/websocket?${queryString}`;
|
|
166
|
+
}), undefined, connectOptions);
|
|
167
|
+
return new Promise((resolve, reject) => {
|
|
168
|
+
this.socket.onopen = () => {
|
|
169
|
+
__classPrivateFieldSet(this, _SttWebsocket_isConnected, true, "f");
|
|
170
|
+
resolve();
|
|
171
|
+
};
|
|
172
|
+
this.socket.onclose = () => {
|
|
173
|
+
__classPrivateFieldSet(this, _SttWebsocket_isConnected, false, "f");
|
|
174
|
+
};
|
|
175
|
+
this.socket.onerror = () => {
|
|
176
|
+
reject(new Error("WebSocket failed to connect."));
|
|
177
|
+
};
|
|
178
|
+
this.socket.reconnect();
|
|
179
|
+
});
|
|
180
|
+
});
|
|
181
|
+
}
|
|
182
|
+
disconnect() {
|
|
183
|
+
var _a;
|
|
184
|
+
(_a = this.socket) === null || _a === void 0 ? void 0 : _a.close();
|
|
185
|
+
__classPrivateFieldSet(this, _SttWebsocket_isConnected, false, "f");
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
_SttWebsocket_isConnected = new WeakMap(), _SttWebsocket_model = new WeakMap(), _SttWebsocket_language = new WeakMap(), _SttWebsocket_encoding = new WeakMap(), _SttWebsocket_sampleRate = new WeakMap(), _SttWebsocket_connectionPromise = new WeakMap(), _SttWebsocket_instances = new WeakSet(), _SttWebsocket_ensureConnected = function _SttWebsocket_ensureConnected() {
|
|
189
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
190
|
+
if (__classPrivateFieldGet(this, _SttWebsocket_isConnected, "f"))
|
|
191
|
+
return;
|
|
192
|
+
if (!__classPrivateFieldGet(this, _SttWebsocket_connectionPromise, "f")) {
|
|
193
|
+
__classPrivateFieldSet(this, _SttWebsocket_connectionPromise, this.connect(), "f");
|
|
194
|
+
}
|
|
195
|
+
yield __classPrivateFieldGet(this, _SttWebsocket_connectionPromise, "f");
|
|
196
|
+
});
|
|
197
|
+
};
|
|
198
|
+
exports.default = SttWebsocket;
|
package/wrapper/Websocket.d.ts
CHANGED
package/wrapper/Websocket.js
CHANGED
|
@@ -55,7 +55,7 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
|
|
|
55
55
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
56
56
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
57
57
|
};
|
|
58
|
-
var _Websocket_instances, _Websocket_isConnected, _Websocket_sampleRate, _Websocket_container, _Websocket_encoding, _Websocket_generateId;
|
|
58
|
+
var _Websocket_instances, _Websocket_isConnected, _Websocket_sampleRate, _Websocket_container, _Websocket_encoding, _Websocket_connectionPromise, _Websocket_ensureConnected, _Websocket_generateId;
|
|
59
59
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
60
60
|
const core = __importStar(require("../core"));
|
|
61
61
|
const environments = __importStar(require("../environments"));
|
|
@@ -74,6 +74,7 @@ class Websocket {
|
|
|
74
74
|
_Websocket_sampleRate.set(this, void 0);
|
|
75
75
|
_Websocket_container.set(this, void 0);
|
|
76
76
|
_Websocket_encoding.set(this, void 0);
|
|
77
|
+
_Websocket_connectionPromise.set(this, void 0);
|
|
77
78
|
__classPrivateFieldSet(this, _Websocket_sampleRate, sampleRate, "f");
|
|
78
79
|
__classPrivateFieldSet(this, _Websocket_container, container !== null && container !== void 0 ? container : "raw", "f");
|
|
79
80
|
__classPrivateFieldSet(this, _Websocket_encoding, encoding !== null && encoding !== void 0 ? encoding : "pcm_f32le", "f");
|
|
@@ -91,9 +92,7 @@ class Websocket {
|
|
|
91
92
|
send(inputs_1) {
|
|
92
93
|
return __awaiter(this, arguments, void 0, function* (inputs, { timeout = 0 } = {}) {
|
|
93
94
|
var _a, _b, _c, _d;
|
|
94
|
-
|
|
95
|
-
throw new Error("Not connected to WebSocket. Call .connect() first.");
|
|
96
|
-
}
|
|
95
|
+
yield __classPrivateFieldGet(this, _Websocket_instances, "m", _Websocket_ensureConnected).call(this);
|
|
97
96
|
if (!inputs.contextId) {
|
|
98
97
|
inputs.contextId = __classPrivateFieldGet(this, _Websocket_instances, "m", _Websocket_generateId).call(this);
|
|
99
98
|
}
|
|
@@ -153,17 +152,17 @@ class Websocket {
|
|
|
153
152
|
});
|
|
154
153
|
}
|
|
155
154
|
continue(inputs) {
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
155
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
156
|
+
var _a;
|
|
157
|
+
yield __classPrivateFieldGet(this, _Websocket_instances, "m", _Websocket_ensureConnected).call(this);
|
|
158
|
+
if (!inputs.contextId) {
|
|
159
|
+
throw new Error("context_id is required to continue a context.");
|
|
160
|
+
}
|
|
161
|
+
if (!inputs.outputFormat) {
|
|
162
|
+
inputs.outputFormat = (0, utils_1.resolveOutputFormat)(__classPrivateFieldGet(this, _Websocket_container, "f"), __classPrivateFieldGet(this, _Websocket_encoding, "f"), __classPrivateFieldGet(this, _Websocket_sampleRate, "f"));
|
|
163
|
+
}
|
|
164
|
+
(_a = this.socket) === null || _a === void 0 ? void 0 : _a.send(JSON.stringify(Object.assign({ continue: true }, serializers.WebSocketTtsRequest.jsonOrThrow(inputs, { unrecognizedObjectKeys: "strip" }))));
|
|
165
|
+
});
|
|
167
166
|
}
|
|
168
167
|
/**
|
|
169
168
|
* Authenticate and connect to a Cartesia streaming WebSocket.
|
|
@@ -226,7 +225,16 @@ class Websocket {
|
|
|
226
225
|
(_a = this.socket) === null || _a === void 0 ? void 0 : _a.close();
|
|
227
226
|
}
|
|
228
227
|
}
|
|
229
|
-
_Websocket_isConnected = new WeakMap(), _Websocket_sampleRate = new WeakMap(), _Websocket_container = new WeakMap(), _Websocket_encoding = new WeakMap(), _Websocket_instances = new WeakSet(),
|
|
228
|
+
_Websocket_isConnected = new WeakMap(), _Websocket_sampleRate = new WeakMap(), _Websocket_container = new WeakMap(), _Websocket_encoding = new WeakMap(), _Websocket_connectionPromise = new WeakMap(), _Websocket_instances = new WeakSet(), _Websocket_ensureConnected = function _Websocket_ensureConnected() {
|
|
229
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
230
|
+
if (__classPrivateFieldGet(this, _Websocket_isConnected, "f"))
|
|
231
|
+
return;
|
|
232
|
+
if (!__classPrivateFieldGet(this, _Websocket_connectionPromise, "f")) {
|
|
233
|
+
__classPrivateFieldSet(this, _Websocket_connectionPromise, this.connect(), "f");
|
|
234
|
+
}
|
|
235
|
+
yield __classPrivateFieldGet(this, _Websocket_connectionPromise, "f");
|
|
236
|
+
});
|
|
237
|
+
}, _Websocket_generateId = function _Websocket_generateId() {
|
|
230
238
|
return (0, human_id_1.humanId)({
|
|
231
239
|
separator: "-",
|
|
232
240
|
capitalize: false,
|