@gladiaio/sdk
Version:
Gladia JavaScript/TypeScript SDK
134 lines (133 loc) • 5.86 kB
TypeScript
import { LiveV2InitRequest, LiveV2InitResponse, LiveV2WebSocketMessage } from "./generated-types.js";
import { HttpClient } from "../../network/httpClient.js";
import { WebSocketClient } from "../../network/wsClient.js";
import { LiveV2SessionStatus } from "./types.js";
//#region src/v2/live/session.d.ts
type EventMap = {
started: [message: LiveV2InitResponse];
connecting: [message: {
attempt: number;
}];
connected: [message: {
attempt: number;
}];
ending: [message: {
code: number;
reason?: string;
}];
ended: [message: {
code: number;
reason?: string;
}];
message: [message: LiveV2WebSocketMessage];
error: [error: Error];
};
declare class LiveV2Session {
private sessionOptions;
private httpClient;
private webSocketClient;
private abortController;
private initSessionPromise;
private initSession;
private webSocketSession;
private eventEmitter;
private bytesSent;
private audioBuffer;
private _status;
constructor({
options,
httpClient,
webSocketClient
}: {
options: LiveV2InitRequest;
httpClient: HttpClient;
webSocketClient: WebSocketClient;
});
/**
* Get the session id. The promise is resolved when the session is started.
* @returns the session id
*/
getSessionId(): Promise<string>;
/**
* The session id or null if the session is not started yet.
*/
get sessionId(): string | null;
/**
* The current status of the session.
* - `starting`: the session is not started yet
* - `started`: the session is started but not connected to the websocket
* - `connecting`: the session is connecting to the websocket. If the connection is lost and it's retryable, the session will reconnect and the status will be `connecting` again.
* - `connected`: the session is connected to the websocket.
* - `ending`: the session is ending because of a user action or an error. In this status, sendAudio and stop are no-op.
* - `ended`: the session is ended. In this status, sendAudio and stop are no-op and listeners are removed.
*/
get status(): LiveV2SessionStatus;
/**
* Send an audio chunk to the current live session.
* If not connected, the audio will be buffered and sent when the session is connected.
*
* @param audio the audio chunk to send
*/
sendAudio(audio: ArrayBufferLike | Buffer<ArrayBufferLike> | ArrayLike<number>): void;
/**
* Stop the recording.
* The session will process the remaining audio and emit the `ended` event when the post-processing is done.
*/
stopRecording(): void;
/**
* Force the end of the session without waiting for the post-processing to be done.
* `ending` and `ended` events are emitted, pending requests/connections are cancelled and listeners are removed.
*/
endSession(): void;
private doStop;
private doDestroy;
private connectToWebSocket;
private startSession;
private parseMessage;
on(type: "started", cb: (...args: EventMap["started"]) => void): void;
on(type: "connecting", cb: (...args: EventMap["connecting"]) => void): void;
on(type: "connected", cb: (...args: EventMap["connected"]) => void): void;
on(type: "ending", cb: (...args: EventMap["ending"]) => void): void;
on(type: "ended", cb: (...args: EventMap["ended"]) => void): void;
on(type: "message", cb: (...args: EventMap["message"]) => void): void;
on(type: "error", cb: (...args: EventMap["error"]) => void): void;
once(type: "started", cb: (...args: EventMap["started"]) => void): void;
once(type: "connecting", cb: (...args: EventMap["connecting"]) => void): void;
once(type: "connected", cb: (...args: EventMap["connected"]) => void): void;
once(type: "ending", cb: (...args: EventMap["ending"]) => void): void;
once(type: "ended", cb: (...args: EventMap["ended"]) => void): void;
once(type: "message", cb: (...args: EventMap["message"]) => void): void;
once(type: "error", cb: (...args: EventMap["error"]) => void): void;
off(type: "started", cb?: (...args: EventMap["started"]) => void): void;
off(type: "connecting", cb?: (...args: EventMap["connecting"]) => void): void;
off(type: "connected", cb?: (...args: EventMap["connected"]) => void): void;
off(type: "ending", cb?: (...args: EventMap["ending"]) => void): void;
off(type: "ended", cb?: (...args: EventMap["ended"]) => void): void;
off(type: "message", cb?: (...args: EventMap["message"]) => void): void;
off(type: "error", cb?: (...args: EventMap["error"]) => void): void;
addListener(type: "started", cb: (...args: EventMap["started"]) => void): void;
addListener(type: "connecting", cb: (...args: EventMap["connecting"]) => void): void;
addListener(type: "connected", cb: (...args: EventMap["connected"]) => void): void;
addListener(type: "ending", cb: (...args: EventMap["ending"]) => void): void;
addListener(type: "ended", cb: (...args: EventMap["ended"]) => void): void;
addListener(type: "message", cb: (...args: EventMap["message"]) => void): void;
addListener(type: "error", cb: (...args: EventMap["error"]) => void): void;
removeListener(type: "started", cb?: (...args: EventMap["started"]) => void): void;
removeListener(type: "connecting", cb?: (...args: EventMap["connecting"]) => void): void;
removeListener(type: "connected", cb?: (...args: EventMap["connected"]) => void): void;
removeListener(type: "ending", cb?: (...args: EventMap["ending"]) => void): void;
removeListener(type: "ended", cb?: (...args: EventMap["ended"]) => void): void;
removeListener(type: "message", cb?: (...args: EventMap["message"]) => void): void;
removeListener(type: "error", cb?: (...args: EventMap["error"]) => void): void;
removeAllListeners(): void;
private emit;
private emit;
private emit;
private emit;
private emit;
private emit;
private emit;
}
//#endregion
export { LiveV2Session };
//# sourceMappingURL=session.d.ts.map