@sathamai/livekit-plugin 0.1.5 → 0.1.7

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 CHANGED
@@ -15,8 +15,9 @@ npm install @sathamai/livekit-plugin @livekit/agents @livekit/rtc-node
15
15
 
16
16
  ## Use
17
17
 
18
- You pass your satham endpoint and API key (or set `SATHAM_URL` and `SATHAM_API_KEY`); there is no
19
- default server.
18
+ You pass your API key, created in the console at https://console.satham.ai/keys (or set
19
+ `SATHAM_API_KEY`). The endpoint is `https://api.satham.ai`; pass `url` or set `SATHAM_URL` only to use
20
+ another. Calls with satham-1 are enabled on request (https://satham.ai/contact).
20
21
 
21
22
  ```ts
22
23
  import { fileURLToPath } from 'node:url';
@@ -39,7 +40,6 @@ export default defineAgent({
39
40
  const session = new voice.AgentSession({
40
41
  // was: new openai.realtime.RealtimeModel({ voice: ... })
41
42
  llm: new satham.RealtimeModel({
42
- url: process.env.SATHAM_URL,
43
43
  apiKey: process.env.SATHAM_API_KEY,
44
44
  greeting: 'Hello, this is Asha from Example Homes. Is this a good time?',
45
45
  language: 'en',
@@ -86,7 +86,7 @@ cli.runApp(new ServerOptions({ agent: fileURLToPath(import.meta.url) }));
86
86
  | `speed` | `speed` | yes | |
87
87
  | `toolChoice` | `toolChoice` | yes | preview: needs a server with tool support |
88
88
  | `apiKey` | `apiKey` | yes | your satham key |
89
- | `baseURL` | `url` | yes | your satham endpoint |
89
+ | `baseURL` | `url` | yes | optional: `https://api.satham.ai` by default |
90
90
  | Agent instructions | Agent instructions | yes | can change mid-call |
91
91
  | `generateReply()` | `generateReply()` | yes | `response.create`; `instructions` guide that reply alone |
92
92
  | `say()` | `RealtimeSession.say()` | yes | `response.say`, no TTS needed (agents-js: call it on the realtime session) |
@@ -187,10 +187,14 @@ const ambience = new satham.AmbiencePlayer({ bed: voice.BuiltinAudioClip.OFFICE_
187
187
  await ambience.start({ room: ctx.room, agentSession: session });
188
188
  ```
189
189
 
190
- `bed` is a file or a LiveKit clip (or none, for typing alone), measured when the player starts so that
191
- strength 1.0 is -35 dBFS RMS. Typing (`@sathamai/sdk`'s CC0 takes, or `typingSounds: [...]`) plays while
192
- the agent is `'thinking'`, which is when a realtime agent's tools run, from 0.4 s in, capped at `strength`.
193
- `ambience.update({ strength, typing, typingStrength })` changes it mid-call.
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`).
194
198
 
195
199
  ## Text to speech
196
200
 
@@ -204,7 +208,7 @@ import * as satham from '@sathamai/livekit-plugin';
204
208
  const session = new voice.AgentSession({
205
209
  stt,
206
210
  llm,
207
- // SATHAM_URL and SATHAM_API_KEY, or url and apiKey here
211
+ // SATHAM_API_KEY, or apiKey here (url or SATHAM_URL only for another endpoint)
208
212
  tts: new satham.TTS({ voice: 'malar', language: 'ta', tone: 'professional' }),
209
213
  });
210
214
  ```
@@ -213,7 +217,7 @@ const session = new voice.AgentSession({
213
217
  sentences (its `tts.StreamAdapter`, as for any TTS without streaming input), and the plugin asks for
214
218
  them one at a time. Each comes back streamed as raw PCM and goes out frame by frame as it arrives,
215
219
  so the audio starts as soon as it is made. 24 kHz, mono.
216
- - **Options:** `url`, `apiKey`, `voice` (a named voice such as `naina`, `malar` or `vikram`, or an
220
+ - **Options:** `url`, `apiKey`, `voice` (a named voice such as `saanvi`, `malar` or `vikram`, or an
217
221
  alias such as `hi-female`), `language`, `tone`, `pace`, `instructions`, `breaths`, `naturalPauses`, `pronunciations`, `pronunciationDictionaryId`, `variant` and `model`, as in
218
222
  `@sathamai/sdk`'s `audio.speech` (its README lists the values). `tts.updateOptions({...})` changes
219
223
  them from the next sentence on; set one to `undefined` to clear it. A pause marker in the text
@@ -2,12 +2,12 @@
2
2
  * Background sound for a LiveKit agent (agents-js), played by LiveKit's own `BackgroundAudioPlayer`.
3
3
  *
4
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 while a tool call runs longer than 0.4 s, from
6
- * the typing takes bundled with `@sathamai/sdk`. It is on satham's scale: strength 1.0 puts any sound at
7
- * -35 dBFS RMS (each file is measured when the player starts), and typing is capped at the room sound's
8
- * level. It works with any LiveKit realtime model, satham's or the OpenAI plugin pointed at satham's
9
- * OpenAI-compatible endpoint, since it follows the session's agent state: LiveKit puts a realtime agent in
10
- * `'thinking'` while its tools run.
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
11
  *
12
12
  * LiveKit plays the agent's audio only during replies, so satham's server-mixed ambience is not used in
13
13
  * LiveKit: the room sound would stop between replies. `RealtimeModel` refuses `ambience`, and turns a
@@ -24,21 +24,28 @@ export type Sound = string | voice.BuiltinAudioClip;
24
24
  */
25
25
  export declare function measure(path: string, activeOnly: boolean): Promise<number>;
26
26
  export interface AmbiencePlayerOptions {
27
- /** The room sound, looped: a file path or a `BuiltinAudioClip` (such as `OFFICE_AMBIENCE`); none for typing alone. */
27
+ /** The room sound, looped: a file path or a `BuiltinAudioClip` (such as `OFFICE_AMBIENCE`); none for the tool sounds alone. */
28
28
  bed?: Sound | null;
29
29
  /** 0 to 2; 1.0 puts the room sound at -35 dBFS RMS, a linear gain (default 1.0). */
30
30
  strength?: number;
31
31
  /** Keyboard typing while a tool call runs longer than 0.4 s (default true). */
32
32
  typing?: boolean;
33
- /** 0 to 2, the same scale, capped at `strength` (default 1.0). */
33
+ /** 0 to 2, the same scale, capped at `strength` (default 1.0); clicks play at this level too. */
34
34
  typingStrength?: number;
35
35
  /** Typing takes (file paths or clips); by default `@sathamai/sdk`'s own. */
36
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[];
37
44
  /** How long a tool runs before typing starts (ms; default 400). */
38
45
  typingAfterMs?: number;
39
46
  }
40
47
  /**
41
- * Room sound and keyboard typing for a LiveKit `AgentSession`, on satham's scale.
48
+ * Room sound, keyboard typing and mouse clicks for a LiveKit `AgentSession`, on satham's scale.
42
49
  *
43
50
  * `await player.start({ room: ctx.room, agentSession: session })` after the session starts, and
44
51
  * `await player.close()` at the end.
@@ -52,11 +59,15 @@ export declare class AmbiencePlayer {
52
59
  agentSession: voice.AgentSession;
53
60
  trackPublishOptions?: TrackPublishOptions;
54
61
  }): Promise<void>;
55
- /** Change the level or typing mid-call; a new strength restarts the room sound at its new level. */
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
+ */
56
66
  update(changes: {
57
67
  strength?: number;
58
68
  typing?: boolean;
59
69
  typingStrength?: number;
70
+ clicks?: boolean | null;
60
71
  }): void;
61
72
  close(): Promise<void>;
62
73
  }
package/dist/ambience.js CHANGED
@@ -2,19 +2,19 @@
2
2
  * Background sound for a LiveKit agent (agents-js), played by LiveKit's own `BackgroundAudioPlayer`.
3
3
  *
4
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 while a tool call runs longer than 0.4 s, from
6
- * the typing takes bundled with `@sathamai/sdk`. It is on satham's scale: strength 1.0 puts any sound at
7
- * -35 dBFS RMS (each file is measured when the player starts), and typing is capped at the room sound's
8
- * level. It works with any LiveKit realtime model, satham's or the OpenAI plugin pointed at satham's
9
- * OpenAI-compatible endpoint, since it follows the session's agent state: LiveKit puts a realtime agent in
10
- * `'thinking'` while its tools run.
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
11
  *
12
12
  * LiveKit plays the agent's audio only during replies, so satham's server-mixed ambience is not used in
13
13
  * LiveKit: the room sound would stop between replies. `RealtimeModel` refuses `ambience`, and turns a
14
14
  * preset agent's server ambience off; use this player instead.
15
15
  */
16
16
  import { audioFramesFromFile, voice } from '@livekit/agents';
17
- import { checkStrength, gainFor, TYPING_AFTER_MS, TypingCue, typingSounds } from '@sathamai/sdk';
17
+ import { checkClicks, checkStrength, clickSounds, clickWaitMs, gainFor, TYPING_AFTER_MS, TypingCue, typingSounds, } from '@sathamai/sdk';
18
18
  /**
19
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
20
  * of the loudest), as satham's server measures room sound and typing.
@@ -69,7 +69,7 @@ function pathOf(sound) {
69
69
  : String(sound);
70
70
  }
71
71
  /**
72
- * Room sound and keyboard typing for a LiveKit `AgentSession`, on satham's scale.
72
+ * Room sound, keyboard typing and mouse clicks for a LiveKit `AgentSession`, on satham's scale.
73
73
  *
74
74
  * `await player.start({ room: ctx.room, agentSession: session })` after the session starts, and
75
75
  * `await player.close()` at the end.
@@ -80,14 +80,22 @@ export class AmbiencePlayer {
80
80
  #strength;
81
81
  #typing;
82
82
  #typingStrength;
83
+ #clicks;
83
84
  #typingPaths;
85
+ #clickPaths;
84
86
  #rms = new Map();
85
87
  #player = null;
86
88
  #session = null;
87
89
  #bedHandle = null;
88
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;
89
96
  #onState = (ev) => {
90
- if (ev.newState === 'thinking' && this.#typing)
97
+ this.#thinking = ev.newState === 'thinking';
98
+ if (this.#thinking && (this.#typing || this.#clicksOn))
91
99
  this.cue.toolStarted('thinking');
92
100
  else
93
101
  this.cue.interrupt();
@@ -97,9 +105,13 @@ export class AmbiencePlayer {
97
105
  this.#strength = checkStrength(options.strength ?? 1, 'strength');
98
106
  this.#typing = options.typing ?? true;
99
107
  this.#typingStrength = checkStrength(options.typingStrength ?? 1, 'typing_strength');
108
+ this.#clicks = checkClicks(options.clicks ?? null);
100
109
  this.#typingPaths = (options.typingSounds ?? typingSounds()).map(pathOf);
101
110
  if (this.#typing && !this.#typingPaths.length)
102
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');
103
115
  this.cue = new TypingCue((on) => this.#onTyping(on), {
104
116
  afterMs: options.typingAfterMs ?? TYPING_AFTER_MS,
105
117
  });
@@ -112,7 +124,7 @@ export class AmbiencePlayer {
112
124
  }
113
125
  /** @internal Measure every sound's level (`start` does this). */
114
126
  async measureSounds() {
115
- const paths = [...(this.#bed ? [this.#bed] : []), ...this.#typingPaths];
127
+ const paths = [...(this.#bed ? [this.#bed] : []), ...this.#typingPaths, ...this.#clickPaths];
116
128
  const levels = await Promise.all(paths.map((p) => measure(p, p !== this.#bed)));
117
129
  paths.forEach((p, i) => {
118
130
  this.#rms.set(p, levels[i]);
@@ -129,15 +141,30 @@ export class AmbiencePlayer {
129
141
  onAgentState(ev) {
130
142
  this.#onState(ev);
131
143
  }
132
- /** Change the level or typing mid-call; a new strength restarts the room sound at its new level. */
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
+ */
133
148
  update(changes) {
134
149
  if (changes.typingStrength !== undefined) {
135
150
  this.#typingStrength = checkStrength(changes.typingStrength, 'typing_strength');
136
151
  }
137
- if (changes.typing !== undefined) {
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)
138
158
  this.#typing = changes.typing;
139
- if (!this.#typing)
159
+ if (changes.typing !== undefined || changes.clicks !== undefined) {
160
+ if (!this.#typing && !this.#clicksOn) {
140
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
+ }
141
168
  }
142
169
  if (changes.strength !== undefined) {
143
170
  this.#strength = checkStrength(changes.strength, 'strength');
@@ -155,6 +182,10 @@ export class AmbiencePlayer {
155
182
  this.#player = null;
156
183
  await player?.close();
157
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
+ }
158
189
  #playBed() {
159
190
  if (!this.#player || !this.#bed || this.#strength <= 0)
160
191
  return;
@@ -162,13 +193,38 @@ export class AmbiencePlayer {
162
193
  this.#bedHandle = this.#player.play({ source: this.#bed, volume }, true);
163
194
  }
164
195
  #onTyping(on) {
165
- if (!on) {
166
- this.#typingHandle?.stop();
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();
167
209
  this.#typingHandle = null;
168
- return;
169
210
  }
170
- const level = Math.min(this.#typingStrength, this.#strength);
171
- if (!this.#player || level <= 0 || this.#typingHandle)
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)
172
228
  return;
173
229
  const configs = this.#typingPaths.map((p) => ({
174
230
  source: p,
@@ -177,4 +233,29 @@ export class AmbiencePlayer {
177
233
  }));
178
234
  this.#typingHandle = this.#player.play(configs, true);
179
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
+ }
180
261
  }
@@ -42,9 +42,9 @@ export declare const REPLY_TIMEOUT_MS = 10000;
42
42
  * decide what the model tells LiveKit it does, so they are fixed for a model once made.
43
43
  */
44
44
  export interface RealtimeModelOptions extends Controls {
45
- /** Your satham endpoint (`wss://...` or `https://...`); `$SATHAM_URL`. */
45
+ /** The satham endpoint: `https://api.satham.ai` unless set here or in `$SATHAM_URL`. */
46
46
  url?: string;
47
- /** Your API key; `$SATHAM_API_KEY`. */
47
+ /** Your API key, from https://console.satham.ai/keys; `$SATHAM_API_KEY`. */
48
48
  apiKey?: string;
49
49
  /** `"auto"`, `"none"`, `"required"` or one function. */
50
50
  toolChoice?: llm.ToolChoice | null;
package/dist/tts.d.ts CHANGED
@@ -47,9 +47,9 @@ export interface TTSVoiceOptions {
47
47
  variant?: SpeechCreateParams['variant'];
48
48
  }
49
49
  export interface TTSOptions extends TTSVoiceOptions {
50
- /** Your satham endpoint (`https://...`); `$SATHAM_URL`. */
50
+ /** The satham endpoint: `https://api.satham.ai` unless set here or in `$SATHAM_URL`. */
51
51
  url?: string;
52
- /** Your API key; `$SATHAM_API_KEY`. */
52
+ /** Your API key, from https://console.satham.ai/keys; `$SATHAM_API_KEY`. */
53
53
  apiKey?: string;
54
54
  }
55
55
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sathamai/livekit-plugin",
3
- "version": "0.1.5",
3
+ "version": "0.1.7",
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.5"
43
+ "@sathamai/sdk": "^0.1.7"
44
44
  },
45
45
  "peerDependencies": {
46
46
  "@livekit/agents": "~1.10.0",