@sathamai/livekit-plugin 0.0.0-stage → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +202 -0
- package/README.md +208 -2
- package/dist/index.d.ts +18 -0
- package/dist/index.js +18 -0
- package/dist/realtime.d.ts +103 -0
- package/dist/realtime.js +556 -0
- package/dist/tts.d.ts +71 -0
- package/dist/tts.js +204 -0
- package/package.json +58 -4
package/dist/tts.js
ADDED
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* kural-1, satham's text-to-speech model, as a LiveKit Agents (agents-js) `TTS`.
|
|
3
|
+
*
|
|
4
|
+
* For a `voice.AgentSession` with its own STT and LLM. The session speaks each reply through satham's
|
|
5
|
+
* speech API (`POST /v1/audio/speech`) a sentence at a time: kural-1 takes whole texts, so the session
|
|
6
|
+
* wraps this TTS in its `tts.StreamAdapter`, which splits the reply into sentences, as it does for any
|
|
7
|
+
* TTS without streaming input. Each sentence comes back streamed, as raw PCM (`stream: true`,
|
|
8
|
+
* `response_format: 'pcm'`), and its frames go out as they arrive, so the audio starts as soon as it
|
|
9
|
+
* is made: 24 kHz, mono.
|
|
10
|
+
*
|
|
11
|
+
* One request runs at a time per `TTS`: the next sentence is asked for once the one before has
|
|
12
|
+
* arrived, which keeps a reply's sentences from queueing up against the server all at once.
|
|
13
|
+
*/
|
|
14
|
+
import { APIConnectionError, APIStatusError, APITimeoutError, AudioByteStream, DEFAULT_API_CONNECT_OPTIONS, shortuuid, tts, } from '@livekit/agents';
|
|
15
|
+
import { Client, SathamError, SPEECH_SAMPLE_RATE } from '@sathamai/sdk';
|
|
16
|
+
/** Frames of 50 ms go to LiveKit. */
|
|
17
|
+
const FRAME_SAMPLES = SPEECH_SAMPLE_RATE / 20;
|
|
18
|
+
function checked(voice) {
|
|
19
|
+
if (voice.instructions != null && (voice.tone != null || voice.pace != null)) {
|
|
20
|
+
throw new TypeError('satham.TTS: instructions cannot be combined with tone or pace');
|
|
21
|
+
}
|
|
22
|
+
return voice;
|
|
23
|
+
}
|
|
24
|
+
/** One request at a time, first come first served. */
|
|
25
|
+
class Turns {
|
|
26
|
+
#busy = false;
|
|
27
|
+
#queue = [];
|
|
28
|
+
/** Resolves to the release once it is this caller's turn; rejects if `signal` aborts first. */
|
|
29
|
+
take(signal) {
|
|
30
|
+
return new Promise((resolve, reject) => {
|
|
31
|
+
if (signal.aborted)
|
|
32
|
+
return reject(signal.reason);
|
|
33
|
+
const grant = () => {
|
|
34
|
+
signal.removeEventListener('abort', onAbort);
|
|
35
|
+
let released = false;
|
|
36
|
+
resolve(() => {
|
|
37
|
+
if (released)
|
|
38
|
+
return;
|
|
39
|
+
released = true;
|
|
40
|
+
const next = this.#queue.shift();
|
|
41
|
+
if (next)
|
|
42
|
+
next();
|
|
43
|
+
else
|
|
44
|
+
this.#busy = false;
|
|
45
|
+
});
|
|
46
|
+
};
|
|
47
|
+
const onAbort = () => {
|
|
48
|
+
const at = this.#queue.indexOf(grant);
|
|
49
|
+
if (at >= 0)
|
|
50
|
+
this.#queue.splice(at, 1);
|
|
51
|
+
reject(signal.reason);
|
|
52
|
+
};
|
|
53
|
+
if (!this.#busy) {
|
|
54
|
+
this.#busy = true;
|
|
55
|
+
grant();
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
this.#queue.push(grant);
|
|
59
|
+
signal.addEventListener('abort', onAbort, { once: true });
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* satham's text to speech (kural-1) for `voice.AgentSession({ stt, llm, tts })`. A pause marker in the
|
|
65
|
+
* text, `<break time="0.5s"/>` (0.1 to 3 seconds), is spoken as that much silence.
|
|
66
|
+
*/
|
|
67
|
+
export class TTS extends tts.TTS {
|
|
68
|
+
label = 'satham.TTS';
|
|
69
|
+
/** @internal */
|
|
70
|
+
client;
|
|
71
|
+
#voice;
|
|
72
|
+
#turns = new Turns();
|
|
73
|
+
constructor(options = {}) {
|
|
74
|
+
super(SPEECH_SAMPLE_RATE, 1, { streaming: false });
|
|
75
|
+
const { url, apiKey, ...voice } = options;
|
|
76
|
+
this.#voice = checked(voice);
|
|
77
|
+
// Fail now, not at the first sentence: the client checks the URL.
|
|
78
|
+
this.client = new Client({ url, apiKey });
|
|
79
|
+
}
|
|
80
|
+
get model() {
|
|
81
|
+
return this.#voice.model ?? 'kural-1';
|
|
82
|
+
}
|
|
83
|
+
get provider() {
|
|
84
|
+
return 'satham';
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Change the voice, language, tone, pace, instructions, breaths, natural pauses or model, from the next sentence on. Only the
|
|
88
|
+
* given options change; set one to `undefined` to clear it (`{ tone: undefined, instructions: '...' }`).
|
|
89
|
+
*/
|
|
90
|
+
updateOptions(options) {
|
|
91
|
+
this.#voice = checked({ ...this.#voice, ...options });
|
|
92
|
+
}
|
|
93
|
+
/** Open a connection to the server before the first sentence (best effort), so it skips the handshake. */
|
|
94
|
+
prewarm() {
|
|
95
|
+
this.client.info().catch(() => undefined);
|
|
96
|
+
}
|
|
97
|
+
synthesize(text, connOptions, abortSignal) {
|
|
98
|
+
return new ChunkedStream(this, text, { ...this.#voice }, connOptions, abortSignal);
|
|
99
|
+
}
|
|
100
|
+
/** kural-1 takes whole texts: `AgentSession` wraps this TTS in `tts.StreamAdapter`, as you can. */
|
|
101
|
+
stream() {
|
|
102
|
+
throw new Error('satham.TTS takes whole texts: call synthesize(), or wrap it in tts.StreamAdapter');
|
|
103
|
+
}
|
|
104
|
+
/** @internal Wait for this TTS's turn to send a request; resolves to the release. */
|
|
105
|
+
turn(signal) {
|
|
106
|
+
return this.#turns.take(signal);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* A failed request as the framework's API error, which the framework retries when `retryable`: an HTTP
|
|
111
|
+
* refusal as `APIStatusError` (retryable for 408, 429 and 5xx, as `APIStatusError` itself decides),
|
|
112
|
+
* anything else as `APIConnectionError`. Once part of the sentence has gone out, a retry would say it
|
|
113
|
+
* again from its start, so then it is not retried.
|
|
114
|
+
*/
|
|
115
|
+
function apiError(error, played) {
|
|
116
|
+
if (!(error instanceof SathamError))
|
|
117
|
+
return error instanceof Error ? error : new Error(String(error));
|
|
118
|
+
const message = `satham speech: ${error.message}`;
|
|
119
|
+
if (error.status !== null) {
|
|
120
|
+
return new APIStatusError({
|
|
121
|
+
message,
|
|
122
|
+
options: {
|
|
123
|
+
statusCode: error.status,
|
|
124
|
+
requestId: error.requestId,
|
|
125
|
+
body: { code: error.code, param: error.param, retry_after: error.retryAfter },
|
|
126
|
+
// Speech switched off on the server does not come back on in a retry.
|
|
127
|
+
retryable: error.code === 'tts_unavailable' ? false : undefined,
|
|
128
|
+
},
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
return new APIConnectionError({ message, options: { retryable: !played } });
|
|
132
|
+
}
|
|
133
|
+
/** One sentence (or any text) spoken by kural-1, as LiveKit audio frames. */
|
|
134
|
+
export class ChunkedStream extends tts.ChunkedStream {
|
|
135
|
+
label = 'satham.ChunkedStream';
|
|
136
|
+
#tts;
|
|
137
|
+
#voice;
|
|
138
|
+
#connOptions;
|
|
139
|
+
constructor(owner, text, voice, connOptions, abortSignal) {
|
|
140
|
+
super(text, owner, connOptions, abortSignal);
|
|
141
|
+
this.#tts = owner;
|
|
142
|
+
this.#voice = voice;
|
|
143
|
+
this.#connOptions = connOptions ?? DEFAULT_API_CONNECT_OPTIONS;
|
|
144
|
+
}
|
|
145
|
+
async run() {
|
|
146
|
+
if (!this.inputText.trim())
|
|
147
|
+
return; // nothing to say (the server would refuse it)
|
|
148
|
+
const signal = this.abortSignal;
|
|
149
|
+
let release;
|
|
150
|
+
try {
|
|
151
|
+
release = await this.#tts.turn(signal);
|
|
152
|
+
}
|
|
153
|
+
catch {
|
|
154
|
+
return; // closed while it waited
|
|
155
|
+
}
|
|
156
|
+
if (signal.aborted)
|
|
157
|
+
return release();
|
|
158
|
+
// The request stops when LiveKit closes this stream (an interruption), or when no audio has come
|
|
159
|
+
// within the connection timeout.
|
|
160
|
+
const request = new AbortController();
|
|
161
|
+
const stop = () => request.abort(signal.reason);
|
|
162
|
+
signal.addEventListener('abort', stop, { once: true });
|
|
163
|
+
let timedOut = false;
|
|
164
|
+
const timer = setTimeout(() => {
|
|
165
|
+
timedOut = true;
|
|
166
|
+
request.abort();
|
|
167
|
+
}, this.#connOptions.timeoutMs);
|
|
168
|
+
let played = false;
|
|
169
|
+
try {
|
|
170
|
+
const audio = this.#tts.client.audio.speech.stream({ ...this.#voice, input: this.inputText, responseFormat: 'pcm' }, { signal: request.signal });
|
|
171
|
+
const frames = new AudioByteStream(SPEECH_SAMPLE_RATE, 1, FRAME_SAMPLES);
|
|
172
|
+
let requestId = '';
|
|
173
|
+
const put = (frame) => {
|
|
174
|
+
played = true;
|
|
175
|
+
this.queue.put({ requestId, segmentId: requestId, frame, final: false });
|
|
176
|
+
};
|
|
177
|
+
for await (const chunk of audio) {
|
|
178
|
+
if (!requestId) {
|
|
179
|
+
clearTimeout(timer);
|
|
180
|
+
requestId = audio.requestId ?? shortuuid('satham_');
|
|
181
|
+
}
|
|
182
|
+
for (const frame of frames.write(chunk))
|
|
183
|
+
put(frame);
|
|
184
|
+
}
|
|
185
|
+
for (const frame of frames.flush())
|
|
186
|
+
put(frame);
|
|
187
|
+
}
|
|
188
|
+
catch (error) {
|
|
189
|
+
if (signal.aborted)
|
|
190
|
+
return; // closed by LiveKit: not an error
|
|
191
|
+
if (timedOut) {
|
|
192
|
+
throw new APITimeoutError({
|
|
193
|
+
message: `satham speech: no audio within ${this.#connOptions.timeoutMs} ms`,
|
|
194
|
+
});
|
|
195
|
+
}
|
|
196
|
+
throw apiError(error, played);
|
|
197
|
+
}
|
|
198
|
+
finally {
|
|
199
|
+
clearTimeout(timer);
|
|
200
|
+
signal.removeEventListener('abort', stop);
|
|
201
|
+
release();
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,60 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sathamai/livekit-plugin",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.2",
|
|
4
|
+
"description": "satham realtime speech-to-speech for LiveKit Agents (Node.js)",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"satham",
|
|
7
|
+
"livekit",
|
|
8
|
+
"livekit-agents",
|
|
9
|
+
"agents-js",
|
|
10
|
+
"plugin",
|
|
11
|
+
"voice-agent",
|
|
12
|
+
"speech-to-speech",
|
|
13
|
+
"realtime"
|
|
14
|
+
],
|
|
15
|
+
"homepage": "https://satham.ai",
|
|
16
|
+
"repository": {
|
|
17
|
+
"type": "git",
|
|
18
|
+
"url": "git+https://github.com/sathamai/satham.git",
|
|
19
|
+
"directory": "integrations/node/packages/livekit-plugin"
|
|
20
|
+
},
|
|
21
|
+
"license": "Apache-2.0",
|
|
22
|
+
"author": "satham",
|
|
23
|
+
"type": "module",
|
|
24
|
+
"main": "./dist/index.js",
|
|
25
|
+
"types": "./dist/index.d.ts",
|
|
26
|
+
"exports": {
|
|
27
|
+
".": {
|
|
28
|
+
"types": "./dist/index.d.ts",
|
|
29
|
+
"import": "./dist/index.js"
|
|
30
|
+
}
|
|
31
|
+
},
|
|
32
|
+
"files": [
|
|
33
|
+
"dist",
|
|
34
|
+
"README.md",
|
|
35
|
+
"LICENSE"
|
|
36
|
+
],
|
|
37
|
+
"sideEffects": false,
|
|
38
|
+
"scripts": {
|
|
39
|
+
"build": "rm -rf dist && tsc -p tsconfig.build.json",
|
|
40
|
+
"prepack": "npm run build"
|
|
41
|
+
},
|
|
42
|
+
"dependencies": {
|
|
43
|
+
"@sathamai/sdk": "^0.1.2"
|
|
44
|
+
},
|
|
45
|
+
"peerDependencies": {
|
|
46
|
+
"@livekit/agents": "~1.10.0",
|
|
47
|
+
"@livekit/rtc-node": "^1.2.2"
|
|
48
|
+
},
|
|
49
|
+
"devDependencies": {
|
|
50
|
+
"@livekit/agents": "1.10.0",
|
|
51
|
+
"@livekit/rtc-node": "1.2.2",
|
|
52
|
+
"zod": "4.1.12"
|
|
53
|
+
},
|
|
54
|
+
"engines": {
|
|
55
|
+
"node": ">=20"
|
|
56
|
+
},
|
|
57
|
+
"publishConfig": {
|
|
58
|
+
"access": "public"
|
|
59
|
+
}
|
|
60
|
+
}
|