@sathamai/livekit-plugin 0.1.4 → 0.1.6
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 +20 -0
- package/dist/ambience.d.ts +73 -0
- package/dist/ambience.js +261 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +5 -0
- package/dist/realtime.d.ts +4 -1
- package/dist/realtime.js +13 -2
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -176,6 +176,26 @@ preset on the server and is not needed.
|
|
|
176
176
|
- **Not supported:** video, and reconnection (a dropped connection ends the call with an
|
|
177
177
|
unrecoverable error).
|
|
178
178
|
|
|
179
|
+
## Background sound
|
|
180
|
+
|
|
181
|
+
LiveKit plays the agent's audio only during replies, so satham's server-mixed background sound is not used
|
|
182
|
+
here: `RealtimeModel` refuses `ambience` with a sound, and turns a preset agent's off. `AmbiencePlayer`
|
|
183
|
+
plays it in the room instead, on LiveKit's own `BackgroundAudioPlayer`:
|
|
184
|
+
|
|
185
|
+
```ts
|
|
186
|
+
const ambience = new satham.AmbiencePlayer({ bed: voice.BuiltinAudioClip.OFFICE_AMBIENCE, strength: 1.0 });
|
|
187
|
+
await ambience.start({ room: ctx.room, agentSession: session });
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
`bed` is a file or a LiveKit clip (or none, for typing and clicks alone), measured when the player starts so
|
|
191
|
+
that strength 1.0 is -35 dBFS RMS. Typing (`@sathamai/sdk`'s CC0 takes, or `typingSounds: [...]`) plays
|
|
192
|
+
while the agent is `'thinking'`, which is when a realtime agent's tools run, from 0.4 s in, capped at
|
|
193
|
+
`strength`. Clicks (`@sathamai/sdk`'s CC0 takes, or `clickSounds: [...]`) play on typing's timing and at its
|
|
194
|
+
level: `clicks: null` (the default) follows `typing`, a click now and then among the typing; `clicks: false`
|
|
195
|
+
keeps typing alone; `clicks: true` with `typing: false` plays clicks alone, one every 1.2 to 4.5 s.
|
|
196
|
+
`ambience.update({ strength, typing, typingStrength, clicks })` changes it mid-call (`clicks: null` gives
|
|
197
|
+
clicks back to `typing`).
|
|
198
|
+
|
|
179
199
|
## Text to speech
|
|
180
200
|
|
|
181
201
|
`TTS` gives an `AgentSession` with its own STT and LLM satham's voices, spoken by kural-1, satham's
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Background sound for a LiveKit agent (agents-js), played by LiveKit's own `BackgroundAudioPlayer`.
|
|
3
|
+
*
|
|
4
|
+
* `AmbiencePlayer` publishes a second audio track in the room: an optional room sound (`bed`, a file or
|
|
5
|
+
* one of LiveKit's `BuiltinAudioClip`s), and keyboard typing and mouse clicks while a tool call runs longer
|
|
6
|
+
* than 0.4 s, from the takes bundled with `@sathamai/sdk`. It is on satham's scale: strength 1.0 puts any
|
|
7
|
+
* sound at -35 dBFS RMS (each file is measured when the player starts), and typing and clicks are capped
|
|
8
|
+
* at the room sound's level. It works with any LiveKit realtime model, satham's or the OpenAI plugin
|
|
9
|
+
* pointed at satham's OpenAI-compatible endpoint, since it follows the session's agent state: LiveKit puts
|
|
10
|
+
* a realtime agent in `'thinking'` while its tools run.
|
|
11
|
+
*
|
|
12
|
+
* LiveKit plays the agent's audio only during replies, so satham's server-mixed ambience is not used in
|
|
13
|
+
* LiveKit: the room sound would stop between replies. `RealtimeModel` refuses `ambience`, and turns a
|
|
14
|
+
* preset agent's server ambience off; use this player instead.
|
|
15
|
+
*/
|
|
16
|
+
import { voice } from '@livekit/agents';
|
|
17
|
+
import type { Room, TrackPublishOptions } from '@livekit/rtc-node';
|
|
18
|
+
import { TypingCue } from '@sathamai/sdk';
|
|
19
|
+
/** A file path, or one of LiveKit's built-in clips. */
|
|
20
|
+
export type Sound = string | voice.BuiltinAudioClip;
|
|
21
|
+
/**
|
|
22
|
+
* A file's RMS (linear, full scale 1.0): over the whole file, or over its active 20 ms frames (within 30 dB
|
|
23
|
+
* of the loudest), as satham's server measures room sound and typing.
|
|
24
|
+
*/
|
|
25
|
+
export declare function measure(path: string, activeOnly: boolean): Promise<number>;
|
|
26
|
+
export interface AmbiencePlayerOptions {
|
|
27
|
+
/** The room sound, looped: a file path or a `BuiltinAudioClip` (such as `OFFICE_AMBIENCE`); none for the tool sounds alone. */
|
|
28
|
+
bed?: Sound | null;
|
|
29
|
+
/** 0 to 2; 1.0 puts the room sound at -35 dBFS RMS, a linear gain (default 1.0). */
|
|
30
|
+
strength?: number;
|
|
31
|
+
/** Keyboard typing while a tool call runs longer than 0.4 s (default true). */
|
|
32
|
+
typing?: boolean;
|
|
33
|
+
/** 0 to 2, the same scale, capped at `strength` (default 1.0); clicks play at this level too. */
|
|
34
|
+
typingStrength?: number;
|
|
35
|
+
/** Typing takes (file paths or clips); by default `@sathamai/sdk`'s own. */
|
|
36
|
+
typingSounds?: Sound[];
|
|
37
|
+
/**
|
|
38
|
+
* Mouse clicks on typing's timing. `null` (the default) follows `typing`: a click now and then among the
|
|
39
|
+
* typing. `false` keeps typing alone; `true` with typing off plays clicks alone, one every 1.2 to 4.5 s.
|
|
40
|
+
*/
|
|
41
|
+
clicks?: boolean | null;
|
|
42
|
+
/** Click takes (file paths or clips); by default `@sathamai/sdk`'s own. */
|
|
43
|
+
clickSounds?: Sound[];
|
|
44
|
+
/** How long a tool runs before typing starts (ms; default 400). */
|
|
45
|
+
typingAfterMs?: number;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Room sound, keyboard typing and mouse clicks for a LiveKit `AgentSession`, on satham's scale.
|
|
49
|
+
*
|
|
50
|
+
* `await player.start({ room: ctx.room, agentSession: session })` after the session starts, and
|
|
51
|
+
* `await player.close()` at the end.
|
|
52
|
+
*/
|
|
53
|
+
export declare class AmbiencePlayer {
|
|
54
|
+
#private;
|
|
55
|
+
readonly cue: TypingCue;
|
|
56
|
+
constructor(options?: AmbiencePlayerOptions);
|
|
57
|
+
start(options: {
|
|
58
|
+
room: Room;
|
|
59
|
+
agentSession: voice.AgentSession;
|
|
60
|
+
trackPublishOptions?: TrackPublishOptions;
|
|
61
|
+
}): Promise<void>;
|
|
62
|
+
/**
|
|
63
|
+
* Change the level, typing or clicks mid-call; a new strength restarts the room sound at its new level.
|
|
64
|
+
* `clicks: null` gives clicks back to `typing`; leave it out to keep it as it is.
|
|
65
|
+
*/
|
|
66
|
+
update(changes: {
|
|
67
|
+
strength?: number;
|
|
68
|
+
typing?: boolean;
|
|
69
|
+
typingStrength?: number;
|
|
70
|
+
clicks?: boolean | null;
|
|
71
|
+
}): void;
|
|
72
|
+
close(): Promise<void>;
|
|
73
|
+
}
|
package/dist/ambience.js
ADDED
|
@@ -0,0 +1,261 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Background sound for a LiveKit agent (agents-js), played by LiveKit's own `BackgroundAudioPlayer`.
|
|
3
|
+
*
|
|
4
|
+
* `AmbiencePlayer` publishes a second audio track in the room: an optional room sound (`bed`, a file or
|
|
5
|
+
* one of LiveKit's `BuiltinAudioClip`s), and keyboard typing and mouse clicks while a tool call runs longer
|
|
6
|
+
* than 0.4 s, from the takes bundled with `@sathamai/sdk`. It is on satham's scale: strength 1.0 puts any
|
|
7
|
+
* sound at -35 dBFS RMS (each file is measured when the player starts), and typing and clicks are capped
|
|
8
|
+
* at the room sound's level. It works with any LiveKit realtime model, satham's or the OpenAI plugin
|
|
9
|
+
* pointed at satham's OpenAI-compatible endpoint, since it follows the session's agent state: LiveKit puts
|
|
10
|
+
* a realtime agent in `'thinking'` while its tools run.
|
|
11
|
+
*
|
|
12
|
+
* LiveKit plays the agent's audio only during replies, so satham's server-mixed ambience is not used in
|
|
13
|
+
* LiveKit: the room sound would stop between replies. `RealtimeModel` refuses `ambience`, and turns a
|
|
14
|
+
* preset agent's server ambience off; use this player instead.
|
|
15
|
+
*/
|
|
16
|
+
import { audioFramesFromFile, voice } from '@livekit/agents';
|
|
17
|
+
import { checkClicks, checkStrength, clickSounds, clickWaitMs, gainFor, TYPING_AFTER_MS, TypingCue, typingSounds, } from '@sathamai/sdk';
|
|
18
|
+
/**
|
|
19
|
+
* A file's RMS (linear, full scale 1.0): over the whole file, or over its active 20 ms frames (within 30 dB
|
|
20
|
+
* of the loudest), as satham's server measures room sound and typing.
|
|
21
|
+
*/
|
|
22
|
+
export async function measure(path, activeOnly) {
|
|
23
|
+
const parts = [];
|
|
24
|
+
const reader = audioFramesFromFile(path, { sampleRate: 24_000, numChannels: 1 }).getReader();
|
|
25
|
+
for (;;) {
|
|
26
|
+
const { value, done } = await reader.read();
|
|
27
|
+
if (done)
|
|
28
|
+
break;
|
|
29
|
+
parts.push(value.data);
|
|
30
|
+
}
|
|
31
|
+
const total = parts.reduce((n, p) => n + p.length, 0);
|
|
32
|
+
if (!total)
|
|
33
|
+
return 0;
|
|
34
|
+
const x = new Float64Array(total);
|
|
35
|
+
let at = 0;
|
|
36
|
+
for (const p of parts) {
|
|
37
|
+
for (let i = 0; i < p.length; i++)
|
|
38
|
+
x[at + i] = p[i] / 32768;
|
|
39
|
+
at += p.length;
|
|
40
|
+
}
|
|
41
|
+
let sum = 0;
|
|
42
|
+
for (const v of x)
|
|
43
|
+
sum += v * v;
|
|
44
|
+
const whole = Math.sqrt(sum / total);
|
|
45
|
+
const frames = Math.floor(total / 480);
|
|
46
|
+
if (!activeOnly || !frames)
|
|
47
|
+
return whole;
|
|
48
|
+
const levels = [];
|
|
49
|
+
for (let f = 0; f < frames; f++) {
|
|
50
|
+
let s = 0;
|
|
51
|
+
for (let i = f * 480; i < (f + 1) * 480; i++)
|
|
52
|
+
s += x[i] ** 2;
|
|
53
|
+
levels.push(Math.sqrt(s / 480));
|
|
54
|
+
}
|
|
55
|
+
const floor = Math.max(...levels) * 10 ** (-30 / 20);
|
|
56
|
+
let activeSum = 0;
|
|
57
|
+
let activeCount = 0;
|
|
58
|
+
for (const level of levels) {
|
|
59
|
+
if (level >= floor) {
|
|
60
|
+
activeSum += level * level;
|
|
61
|
+
activeCount += 1;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
return activeCount ? Math.sqrt(activeSum / activeCount) : whole;
|
|
65
|
+
}
|
|
66
|
+
function pathOf(sound) {
|
|
67
|
+
return Object.values(voice.BuiltinAudioClip).includes(sound)
|
|
68
|
+
? voice.getBuiltinAudioPath(sound)
|
|
69
|
+
: String(sound);
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Room sound, keyboard typing and mouse clicks for a LiveKit `AgentSession`, on satham's scale.
|
|
73
|
+
*
|
|
74
|
+
* `await player.start({ room: ctx.room, agentSession: session })` after the session starts, and
|
|
75
|
+
* `await player.close()` at the end.
|
|
76
|
+
*/
|
|
77
|
+
export class AmbiencePlayer {
|
|
78
|
+
cue;
|
|
79
|
+
#bed;
|
|
80
|
+
#strength;
|
|
81
|
+
#typing;
|
|
82
|
+
#typingStrength;
|
|
83
|
+
#clicks;
|
|
84
|
+
#typingPaths;
|
|
85
|
+
#clickPaths;
|
|
86
|
+
#rms = new Map();
|
|
87
|
+
#player = null;
|
|
88
|
+
#session = null;
|
|
89
|
+
#bedHandle = null;
|
|
90
|
+
#typingHandle = null;
|
|
91
|
+
#thinking = false; // the agent is thinking: its tools run
|
|
92
|
+
#sounding = false; // the cue is on: a tool has run 0.4 s
|
|
93
|
+
#clicking = false;
|
|
94
|
+
#clickTimer = null;
|
|
95
|
+
#clickHandle = null;
|
|
96
|
+
#onState = (ev) => {
|
|
97
|
+
this.#thinking = ev.newState === 'thinking';
|
|
98
|
+
if (this.#thinking && (this.#typing || this.#clicksOn))
|
|
99
|
+
this.cue.toolStarted('thinking');
|
|
100
|
+
else
|
|
101
|
+
this.cue.interrupt();
|
|
102
|
+
};
|
|
103
|
+
constructor(options = {}) {
|
|
104
|
+
this.#bed = options.bed != null ? pathOf(options.bed) : null;
|
|
105
|
+
this.#strength = checkStrength(options.strength ?? 1, 'strength');
|
|
106
|
+
this.#typing = options.typing ?? true;
|
|
107
|
+
this.#typingStrength = checkStrength(options.typingStrength ?? 1, 'typing_strength');
|
|
108
|
+
this.#clicks = checkClicks(options.clicks ?? null);
|
|
109
|
+
this.#typingPaths = (options.typingSounds ?? typingSounds()).map(pathOf);
|
|
110
|
+
if (this.#typing && !this.#typingPaths.length)
|
|
111
|
+
throw new Error('typing needs at least one typing sound');
|
|
112
|
+
this.#clickPaths = (options.clickSounds ?? clickSounds()).map(pathOf);
|
|
113
|
+
if (this.#clicks && !this.#clickPaths.length)
|
|
114
|
+
throw new Error('clicks need at least one click sound');
|
|
115
|
+
this.cue = new TypingCue((on) => this.#onTyping(on), {
|
|
116
|
+
afterMs: options.typingAfterMs ?? TYPING_AFTER_MS,
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
async start(options) {
|
|
120
|
+
await this.measureSounds();
|
|
121
|
+
const player = new voice.BackgroundAudioPlayer();
|
|
122
|
+
await player.start({ room: options.room, trackPublishOptions: options.trackPublishOptions });
|
|
123
|
+
this.attach(player, options.agentSession);
|
|
124
|
+
}
|
|
125
|
+
/** @internal Measure every sound's level (`start` does this). */
|
|
126
|
+
async measureSounds() {
|
|
127
|
+
const paths = [...(this.#bed ? [this.#bed] : []), ...this.#typingPaths, ...this.#clickPaths];
|
|
128
|
+
const levels = await Promise.all(paths.map((p) => measure(p, p !== this.#bed)));
|
|
129
|
+
paths.forEach((p, i) => {
|
|
130
|
+
this.#rms.set(p, levels[i]);
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
/** @internal Play through `player`, following `session`'s agent state (`start` does this). */
|
|
134
|
+
attach(player, session) {
|
|
135
|
+
this.#player = player;
|
|
136
|
+
this.#session = session;
|
|
137
|
+
session?.on(voice.AgentSessionEventTypes.AgentStateChanged, this.#onState);
|
|
138
|
+
this.#playBed();
|
|
139
|
+
}
|
|
140
|
+
/** @internal Follow an agent state change (as the session reports it). */
|
|
141
|
+
onAgentState(ev) {
|
|
142
|
+
this.#onState(ev);
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* Change the level, typing or clicks mid-call; a new strength restarts the room sound at its new level.
|
|
146
|
+
* `clicks: null` gives clicks back to `typing`; leave it out to keep it as it is.
|
|
147
|
+
*/
|
|
148
|
+
update(changes) {
|
|
149
|
+
if (changes.typingStrength !== undefined) {
|
|
150
|
+
this.#typingStrength = checkStrength(changes.typingStrength, 'typing_strength');
|
|
151
|
+
}
|
|
152
|
+
if (changes.clicks !== undefined) {
|
|
153
|
+
this.#clicks = checkClicks(changes.clicks);
|
|
154
|
+
if (this.#clicks && !this.#clickPaths.length)
|
|
155
|
+
throw new Error('clicks need at least one click sound');
|
|
156
|
+
}
|
|
157
|
+
if (changes.typing !== undefined)
|
|
158
|
+
this.#typing = changes.typing;
|
|
159
|
+
if (changes.typing !== undefined || changes.clicks !== undefined) {
|
|
160
|
+
if (!this.#typing && !this.#clicksOn) {
|
|
161
|
+
this.cue.interrupt();
|
|
162
|
+
}
|
|
163
|
+
else {
|
|
164
|
+
if (this.#thinking)
|
|
165
|
+
this.cue.toolStarted('thinking'); // a tool still running: from 0.4 s in
|
|
166
|
+
this.#syncToolSounds();
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
if (changes.strength !== undefined) {
|
|
170
|
+
this.#strength = checkStrength(changes.strength, 'strength');
|
|
171
|
+
const old = this.#bedHandle;
|
|
172
|
+
this.#bedHandle = null;
|
|
173
|
+
this.#playBed();
|
|
174
|
+
old?.stop();
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
async close() {
|
|
178
|
+
this.cue.close();
|
|
179
|
+
this.#session?.off(voice.AgentSessionEventTypes.AgentStateChanged, this.#onState);
|
|
180
|
+
this.#session = null;
|
|
181
|
+
const player = this.#player;
|
|
182
|
+
this.#player = null;
|
|
183
|
+
await player?.close();
|
|
184
|
+
}
|
|
185
|
+
/** Whether clicks play during tool calls: `clicks`, or `typing` when it is null. */
|
|
186
|
+
get #clicksOn() {
|
|
187
|
+
return this.#clicks ?? this.#typing;
|
|
188
|
+
}
|
|
189
|
+
#playBed() {
|
|
190
|
+
if (!this.#player || !this.#bed || this.#strength <= 0)
|
|
191
|
+
return;
|
|
192
|
+
const volume = gainFor(this.#rms.get(this.#bed) ?? 0, this.#strength);
|
|
193
|
+
this.#bedHandle = this.#player.play({ source: this.#bed, volume }, true);
|
|
194
|
+
}
|
|
195
|
+
#onTyping(on) {
|
|
196
|
+
this.#sounding = on;
|
|
197
|
+
this.#syncToolSounds();
|
|
198
|
+
}
|
|
199
|
+
/** Start the tool sounds that should play and stop the ones that should not. */
|
|
200
|
+
#syncToolSounds() {
|
|
201
|
+
const level = Math.min(this.#typingStrength, this.#strength);
|
|
202
|
+
const live = this.#sounding && this.#player !== null && level > 0;
|
|
203
|
+
if (live && this.#typing && this.#typingPaths.length) {
|
|
204
|
+
if (!this.#typingHandle)
|
|
205
|
+
this.#startTyping(level);
|
|
206
|
+
}
|
|
207
|
+
else if (this.#typingHandle) {
|
|
208
|
+
this.#typingHandle.stop();
|
|
209
|
+
this.#typingHandle = null;
|
|
210
|
+
}
|
|
211
|
+
if (live && this.#clicksOn && this.#clickPaths.length) {
|
|
212
|
+
if (!this.#clicking) {
|
|
213
|
+
this.#clicking = true;
|
|
214
|
+
this.#scheduleClick(true);
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
else if (this.#clicking) {
|
|
218
|
+
this.#clicking = false;
|
|
219
|
+
if (this.#clickTimer !== null)
|
|
220
|
+
clearTimeout(this.#clickTimer);
|
|
221
|
+
this.#clickTimer = null;
|
|
222
|
+
this.#clickHandle?.stop();
|
|
223
|
+
this.#clickHandle = null;
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
#startTyping(level) {
|
|
227
|
+
if (!this.#player)
|
|
228
|
+
return;
|
|
229
|
+
const configs = this.#typingPaths.map((p) => ({
|
|
230
|
+
source: p,
|
|
231
|
+
volume: gainFor(this.#rms.get(p) ?? 0, level),
|
|
232
|
+
probability: 1 / this.#typingPaths.length,
|
|
233
|
+
}));
|
|
234
|
+
this.#typingHandle = this.#player.play(configs, true);
|
|
235
|
+
}
|
|
236
|
+
/** The next click take, spaced as the server spaces them; one at a time until the cue stops. */
|
|
237
|
+
#scheduleClick(first) {
|
|
238
|
+
this.#clickTimer = setTimeout(() => this.#playClick(), clickWaitMs(this.#typing, first));
|
|
239
|
+
}
|
|
240
|
+
#playClick() {
|
|
241
|
+
this.#clickTimer = null;
|
|
242
|
+
const level = Math.min(this.#typingStrength, this.#strength);
|
|
243
|
+
if (!this.#clicking || !this.#player || level <= 0)
|
|
244
|
+
return;
|
|
245
|
+
const configs = this.#clickPaths.map((p) => ({
|
|
246
|
+
source: p,
|
|
247
|
+
volume: gainFor(this.#rms.get(p) ?? 0, level),
|
|
248
|
+
probability: 1 / this.#clickPaths.length,
|
|
249
|
+
}));
|
|
250
|
+
const handle = this.#player.play(configs, false);
|
|
251
|
+
this.#clickHandle = handle;
|
|
252
|
+
const next = () => {
|
|
253
|
+
if (this.#clickHandle !== handle)
|
|
254
|
+
return;
|
|
255
|
+
this.#clickHandle = null;
|
|
256
|
+
if (this.#clicking)
|
|
257
|
+
this.#scheduleClick(false);
|
|
258
|
+
};
|
|
259
|
+
handle.waitForPlayout().then(next, next);
|
|
260
|
+
}
|
|
261
|
+
}
|
package/dist/index.d.ts
CHANGED
|
@@ -12,7 +12,12 @@
|
|
|
12
12
|
* // Or your own STT and LLM, with satham's voices:
|
|
13
13
|
* const tts = new satham.TTS({ voice: 'malar', language: 'ta' });
|
|
14
14
|
* const pipeline = new voice.AgentSession({ stt, llm, tts });
|
|
15
|
+
*
|
|
16
|
+
* // Background sound in the room: a room sound, and typing while tools run.
|
|
17
|
+
* const ambience = new satham.AmbiencePlayer({ bed: voice.BuiltinAudioClip.OFFICE_AMBIENCE, strength: 1.0 });
|
|
18
|
+
* await ambience.start({ room: ctx.room, agentSession: session });
|
|
15
19
|
* ```
|
|
16
20
|
*/
|
|
21
|
+
export { AmbiencePlayer, type AmbiencePlayerOptions, measure, type Sound } from './ambience.js';
|
|
17
22
|
export { INPUT_SAMPLE_RATE, type OptionsUpdate, RealtimeModel, type RealtimeModelOptions, RealtimeSession, } from './realtime.js';
|
|
18
23
|
export { ChunkedStream, TTS, type TTSOptions, type TTSVoiceOptions } from './tts.js';
|
package/dist/index.js
CHANGED
|
@@ -12,7 +12,12 @@
|
|
|
12
12
|
* // Or your own STT and LLM, with satham's voices:
|
|
13
13
|
* const tts = new satham.TTS({ voice: 'malar', language: 'ta' });
|
|
14
14
|
* const pipeline = new voice.AgentSession({ stt, llm, tts });
|
|
15
|
+
*
|
|
16
|
+
* // Background sound in the room: a room sound, and typing while tools run.
|
|
17
|
+
* const ambience = new satham.AmbiencePlayer({ bed: voice.BuiltinAudioClip.OFFICE_AMBIENCE, strength: 1.0 });
|
|
18
|
+
* await ambience.start({ room: ctx.room, agentSession: session });
|
|
15
19
|
* ```
|
|
16
20
|
*/
|
|
21
|
+
export { AmbiencePlayer, measure } from './ambience.js';
|
|
17
22
|
export { INPUT_SAMPLE_RATE, RealtimeModel, RealtimeSession, } from './realtime.js';
|
|
18
23
|
export { ChunkedStream, TTS } from './tts.js';
|
package/dist/realtime.d.ts
CHANGED
|
@@ -20,7 +20,10 @@
|
|
|
20
20
|
* - `llm.tool()` tools are sent as OpenAI-style function definitions; each call the agent makes is run
|
|
21
21
|
* by LiveKit, and its result goes back to the server, which then replies unless the tool returned
|
|
22
22
|
* nothing;
|
|
23
|
-
* - a change of language is emitted as `session_updated
|
|
23
|
+
* - a change of language is emitted as `session_updated`;
|
|
24
|
+
* - background sound is played in the room by `AmbiencePlayer`, LiveKit's own player, not by the server
|
|
25
|
+
* (LiveKit plays the agent's audio only during replies): `ambience` with a sound is refused, and a
|
|
26
|
+
* preset agent's server ambience is turned off when its call starts.
|
|
24
27
|
*/
|
|
25
28
|
import { llm } from '@livekit/agents';
|
|
26
29
|
import { AudioFrame } from '@livekit/rtc-node';
|
package/dist/realtime.js
CHANGED
|
@@ -20,11 +20,14 @@
|
|
|
20
20
|
* - `llm.tool()` tools are sent as OpenAI-style function definitions; each call the agent makes is run
|
|
21
21
|
* by LiveKit, and its result goes back to the server, which then replies unless the tool returned
|
|
22
22
|
* nothing;
|
|
23
|
-
* - a change of language is emitted as `session_updated
|
|
23
|
+
* - a change of language is emitted as `session_updated`;
|
|
24
|
+
* - background sound is played in the room by `AmbiencePlayer`, LiveKit's own player, not by the server
|
|
25
|
+
* (LiveKit plays the agent's audio only during replies): `ambience` with a sound is refused, and a
|
|
26
|
+
* preset agent's server ambience is turned off when its call starts.
|
|
24
27
|
*/
|
|
25
28
|
import { AudioByteStream, llm, log, stream } from '@livekit/agents';
|
|
26
29
|
import { AudioFrame, AudioResampler } from '@livekit/rtc-node';
|
|
27
|
-
import { Client, responseCreate, responseSay, sessionUpdate, toolResult, toWire, } from '@sathamai/sdk';
|
|
30
|
+
import { Client, inEffect, refuseServerAmbience, responseCreate, responseSay, sessionUpdate, toolResult, toWire, } from '@sathamai/sdk';
|
|
28
31
|
/** Caller audio goes to satham as 16 kHz mono 16-bit PCM. */
|
|
29
32
|
export const INPUT_SAMPLE_RATE = 16_000;
|
|
30
33
|
/** How long `generateReply` and `say` wait for the reply to start. */
|
|
@@ -72,6 +75,7 @@ export class RealtimeModel extends llm.RealtimeModel {
|
|
|
72
75
|
#limitsLoaded = null;
|
|
73
76
|
constructor(options = {}) {
|
|
74
77
|
const controls = controlsOf(options);
|
|
78
|
+
refuseServerAmbience(controls, 'LiveKit', 'AmbiencePlayer from @sathamai/livekit-plugin');
|
|
75
79
|
const modalities = controls.modalities ?? ['text', 'audio'];
|
|
76
80
|
super({
|
|
77
81
|
messageTruncation: false,
|
|
@@ -120,6 +124,7 @@ export class RealtimeModel extends llm.RealtimeModel {
|
|
|
120
124
|
/** Change controls for new calls and every live one (`session.update`). */
|
|
121
125
|
updateOptions(changes) {
|
|
122
126
|
const controls = controlsOf(changes);
|
|
127
|
+
refuseServerAmbience(controls, 'LiveKit', 'AmbiencePlayer from @sathamai/livekit-plugin');
|
|
123
128
|
toWire(controls, 'session.update', this.client.limits);
|
|
124
129
|
Object.assign(this.controls, controls);
|
|
125
130
|
if (changes.toolChoice !== undefined)
|
|
@@ -216,6 +221,11 @@ export class RealtimeSession extends llm.RealtimeSession {
|
|
|
216
221
|
}
|
|
217
222
|
if (call.language)
|
|
218
223
|
this.language = call.language;
|
|
224
|
+
if (inEffect(call.started.ambience).sound) {
|
|
225
|
+
// A preset agent's room sound would stop between replies here: AmbiencePlayer plays it instead.
|
|
226
|
+
this.#logger.warn("the agent's server ambience is turned off in LiveKit; play background sound with AmbiencePlayer");
|
|
227
|
+
this.#send({ type: 'session.update', ambience: null });
|
|
228
|
+
}
|
|
219
229
|
call.on('event', (event) => this.#onEvent(event));
|
|
220
230
|
call.on('close', (code) => {
|
|
221
231
|
this.#ended = true;
|
|
@@ -470,6 +480,7 @@ export class RealtimeSession extends llm.RealtimeSession {
|
|
|
470
480
|
}
|
|
471
481
|
/** Change the call's controls (`session.update`); before it starts, its start. */
|
|
472
482
|
updateOptions(options) {
|
|
483
|
+
refuseServerAmbience(controlsOf(options), 'LiveKit', 'AmbiencePlayer from @sathamai/livekit-plugin');
|
|
473
484
|
if (options.toolChoice !== undefined) {
|
|
474
485
|
this.#toolChoice = options.toolChoice;
|
|
475
486
|
if (this.#call)
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sathamai/livekit-plugin",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.6",
|
|
4
4
|
"description": "satham realtime speech-to-speech for LiveKit Agents (Node.js)",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"satham",
|
|
@@ -40,7 +40,7 @@
|
|
|
40
40
|
"prepack": "npm run build"
|
|
41
41
|
},
|
|
42
42
|
"dependencies": {
|
|
43
|
-
"@sathamai/sdk": "^0.1.
|
|
43
|
+
"@sathamai/sdk": "^0.1.6"
|
|
44
44
|
},
|
|
45
45
|
"peerDependencies": {
|
|
46
46
|
"@livekit/agents": "~1.10.0",
|