@mastra/livekit 0.0.0-esbuild-bundle-worker-20260807173433
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/CHANGELOG.md +433 -0
- package/LICENSE.md +30 -0
- package/README.md +448 -0
- package/dist/bridge.d.ts +229 -0
- package/dist/bridge.d.ts.map +1 -0
- package/dist/consent.d.ts +55 -0
- package/dist/consent.d.ts.map +1 -0
- package/dist/constants.d.ts +3 -0
- package/dist/constants.d.ts.map +1 -0
- package/dist/dispatch.d.ts +20 -0
- package/dist/dispatch.d.ts.map +1 -0
- package/dist/end-call.d.ts +50 -0
- package/dist/end-call.d.ts.map +1 -0
- package/dist/index.cjs +189 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +14 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +182 -0
- package/dist/index.js.map +1 -0
- package/dist/llm-plugin.d.ts +112 -0
- package/dist/llm-plugin.d.ts.map +1 -0
- package/dist/messages.d.ts +50 -0
- package/dist/messages.d.ts.map +1 -0
- package/dist/metadata.d.ts +17 -0
- package/dist/metadata.d.ts.map +1 -0
- package/dist/observability.d.ts +46 -0
- package/dist/observability.d.ts.map +1 -0
- package/dist/plugin-entry.cjs +209 -0
- package/dist/plugin-entry.cjs.map +1 -0
- package/dist/plugin-entry.d.ts +6 -0
- package/dist/plugin-entry.d.ts.map +1 -0
- package/dist/plugin-entry.js +207 -0
- package/dist/plugin-entry.js.map +1 -0
- package/dist/remote-C9K3UzKv.js +629 -0
- package/dist/remote-C9K3UzKv.js.map +1 -0
- package/dist/remote-D0Y5P6e4.cjs +658 -0
- package/dist/remote-D0Y5P6e4.cjs.map +1 -0
- package/dist/remote.d.ts +68 -0
- package/dist/remote.d.ts.map +1 -0
- package/dist/routes.d.ts +52 -0
- package/dist/routes.d.ts.map +1 -0
- package/dist/run.d.ts +32 -0
- package/dist/run.d.ts.map +1 -0
- package/dist/voice-thread.d.ts +23 -0
- package/dist/voice-thread.d.ts.map +1 -0
- package/dist/worker-entry.cjs +686 -0
- package/dist/worker-entry.cjs.map +1 -0
- package/dist/worker-entry.d.ts +10 -0
- package/dist/worker-entry.d.ts.map +1 -0
- package/dist/worker-entry.js +675 -0
- package/dist/worker-entry.js.map +1 -0
- package/dist/worker-setup.d.ts +5 -0
- package/dist/worker-setup.d.ts.map +1 -0
- package/dist/worker.d.ts +461 -0
- package/dist/worker.d.ts.map +1 -0
- package/dist/workflow-generator-B67QrY8L.cjs +209 -0
- package/dist/workflow-generator-B67QrY8L.cjs.map +1 -0
- package/dist/workflow-generator-BtfClQcM.js +180 -0
- package/dist/workflow-generator-BtfClQcM.js.map +1 -0
- package/dist/workflow-generator.d.ts +92 -0
- package/dist/workflow-generator.d.ts.map +1 -0
- package/package.json +107 -0
|
@@ -0,0 +1,686 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
const require_workflow_generator = require("./workflow-generator-B67QrY8L.cjs");
|
|
3
|
+
const require_remote = require("./remote-D0Y5P6e4.cjs");
|
|
4
|
+
let crypto = require("crypto");
|
|
5
|
+
let _livekit_agents = require("@livekit/agents");
|
|
6
|
+
let _mastra_core_request_context = require("@mastra/core/request-context");
|
|
7
|
+
let _mastra_core_observability = require("@mastra/core/observability");
|
|
8
|
+
let url = require("url");
|
|
9
|
+
//#region src/observability.ts
|
|
10
|
+
function modelMeta(metadata) {
|
|
11
|
+
const out = {};
|
|
12
|
+
if (metadata?.modelProvider) out.modelProvider = metadata.modelProvider;
|
|
13
|
+
if (metadata?.modelName) out.modelName = metadata.modelName;
|
|
14
|
+
return out;
|
|
15
|
+
}
|
|
16
|
+
const ms = (n) => `${Math.round(n)}ms`;
|
|
17
|
+
const secs = (n) => `${(n / 1e3).toFixed(1)}s`;
|
|
18
|
+
/**
|
|
19
|
+
* Maps a LiveKit pipeline metric to an event-span name and the latency/usage fields worth
|
|
20
|
+
* recording. The name carries the headline number so it reads in the trace timeline without
|
|
21
|
+
* opening the span. Metrics this release does not surface (interruption, EOT inference, avatar)
|
|
22
|
+
* return `undefined` — they still feed the session usage roll-up via the ModelUsageCollector.
|
|
23
|
+
*/
|
|
24
|
+
function describeMetric(metric) {
|
|
25
|
+
switch (metric.type) {
|
|
26
|
+
case "eou_metrics": return {
|
|
27
|
+
name: `eou ${ms(metric.endOfUtteranceDelayMs)}`,
|
|
28
|
+
data: {
|
|
29
|
+
endOfUtteranceDelayMs: metric.endOfUtteranceDelayMs,
|
|
30
|
+
transcriptionDelayMs: metric.transcriptionDelayMs,
|
|
31
|
+
onUserTurnCompletedDelayMs: metric.onUserTurnCompletedDelayMs
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
case "stt_metrics": return {
|
|
35
|
+
name: `stt ${secs(metric.audioDurationMs)}`,
|
|
36
|
+
data: {
|
|
37
|
+
audioDurationMs: metric.audioDurationMs,
|
|
38
|
+
durationMs: metric.durationMs,
|
|
39
|
+
streamed: metric.streamed,
|
|
40
|
+
...modelMeta(metric.metadata)
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
case "llm_metrics": return {
|
|
44
|
+
name: `llm ttft ${ms(metric.ttftMs)}`,
|
|
45
|
+
data: {
|
|
46
|
+
ttftMs: metric.ttftMs,
|
|
47
|
+
durationMs: metric.durationMs,
|
|
48
|
+
tokensPerSecond: metric.tokensPerSecond,
|
|
49
|
+
promptTokens: metric.promptTokens,
|
|
50
|
+
completionTokens: metric.completionTokens,
|
|
51
|
+
totalTokens: metric.totalTokens,
|
|
52
|
+
cancelled: metric.cancelled,
|
|
53
|
+
...modelMeta(metric.metadata)
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
case "tts_metrics": return {
|
|
57
|
+
name: `tts ttfb ${ms(metric.ttfbMs)}`,
|
|
58
|
+
data: {
|
|
59
|
+
ttfbMs: metric.ttfbMs,
|
|
60
|
+
durationMs: metric.durationMs,
|
|
61
|
+
audioDurationMs: metric.audioDurationMs,
|
|
62
|
+
charactersCount: metric.charactersCount,
|
|
63
|
+
cancelled: metric.cancelled,
|
|
64
|
+
streamed: metric.streamed,
|
|
65
|
+
...modelMeta(metric.metadata)
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
case "vad_metrics": return {
|
|
69
|
+
name: "vad",
|
|
70
|
+
data: {
|
|
71
|
+
idleTimeMs: metric.idleTimeMs,
|
|
72
|
+
inferenceCount: metric.inferenceCount,
|
|
73
|
+
inferenceDurationTotalMs: metric.inferenceDurationTotalMs
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
case "realtime_model_metrics": return {
|
|
77
|
+
name: `realtime ttft ${ms(metric.ttftMs)}`,
|
|
78
|
+
data: {
|
|
79
|
+
ttftMs: metric.ttftMs,
|
|
80
|
+
durationMs: metric.durationMs,
|
|
81
|
+
tokensPerSecond: metric.tokensPerSecond,
|
|
82
|
+
promptTokens: metric.inputTokens,
|
|
83
|
+
completionTokens: metric.outputTokens,
|
|
84
|
+
totalTokens: metric.totalTokens,
|
|
85
|
+
...modelMeta(metric.metadata)
|
|
86
|
+
}
|
|
87
|
+
};
|
|
88
|
+
default: return;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Opens a `voice call` trace for one LiveKit session and bridges LiveKit's voice-pipeline
|
|
93
|
+
* metrics into Mastra observability.
|
|
94
|
+
*
|
|
95
|
+
* The returned root span groups everything about the call: each conversation turn's Mastra
|
|
96
|
+
* agent run (nested via {@link VoiceCallObservability.tracingContext}) plus an event span for
|
|
97
|
+
* every STT, TTS, end-of-utterance, VAD, and LLM-latency metric LiveKit emits. Metrics are
|
|
98
|
+
* point-in-time, so they're recorded as event spans (no duration) with the value in the name.
|
|
99
|
+
* The root closes on the session `close` event with a per-model usage roll-up (token, character,
|
|
100
|
+
* and audio totals for the whole call) from a `ModelUsageCollector`.
|
|
101
|
+
*
|
|
102
|
+
* Returns `undefined` when the Mastra instance has no observability configured, so callers
|
|
103
|
+
* can treat instrumentation as a no-op without branching on config.
|
|
104
|
+
*/
|
|
105
|
+
function startVoiceCallObservability(options) {
|
|
106
|
+
const span = (0, _mastra_core_observability.getOrCreateSpan)({
|
|
107
|
+
mastra: options.mastra,
|
|
108
|
+
type: _mastra_core_observability.SpanType.GENERIC,
|
|
109
|
+
name: "voice call",
|
|
110
|
+
requestContext: options.requestContext,
|
|
111
|
+
metadata: {
|
|
112
|
+
agentId: options.agentId,
|
|
113
|
+
roomName: options.roomName,
|
|
114
|
+
...options.metadata.threadId ? { threadId: options.metadata.threadId } : {},
|
|
115
|
+
...options.metadata.resourceId ? { resourceId: options.metadata.resourceId } : {}
|
|
116
|
+
}
|
|
117
|
+
});
|
|
118
|
+
if (!span) return void 0;
|
|
119
|
+
const usage = new _livekit_agents.metrics.ModelUsageCollector();
|
|
120
|
+
let finalized = false;
|
|
121
|
+
const finalize = (opts) => {
|
|
122
|
+
if (finalized) return;
|
|
123
|
+
finalized = true;
|
|
124
|
+
const summary = usage.flatten();
|
|
125
|
+
if (opts?.error) span.error({
|
|
126
|
+
error: opts.error instanceof Error ? opts.error : new Error(String(opts.error)),
|
|
127
|
+
metadata: { usage: summary }
|
|
128
|
+
});
|
|
129
|
+
else span.end({ output: { usage: summary } });
|
|
130
|
+
};
|
|
131
|
+
return {
|
|
132
|
+
span,
|
|
133
|
+
tracingContext: { currentSpan: span },
|
|
134
|
+
attach(session) {
|
|
135
|
+
session.on(_livekit_agents.voice.AgentSessionEventTypes.MetricsCollected, (event) => {
|
|
136
|
+
usage.collect(event.metrics);
|
|
137
|
+
const described = describeMetric(event.metrics);
|
|
138
|
+
if (!described) return;
|
|
139
|
+
span.createEventSpan({
|
|
140
|
+
type: _mastra_core_observability.SpanType.GENERIC,
|
|
141
|
+
name: described.name,
|
|
142
|
+
output: described.data
|
|
143
|
+
});
|
|
144
|
+
});
|
|
145
|
+
session.on(_livekit_agents.voice.AgentSessionEventTypes.Close, (event) => {
|
|
146
|
+
finalize({ error: event?.error ?? void 0 });
|
|
147
|
+
});
|
|
148
|
+
},
|
|
149
|
+
finalize
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
//#endregion
|
|
153
|
+
//#region src/voice-thread.ts
|
|
154
|
+
/**
|
|
155
|
+
* Creates the call's memory thread up front when it doesn't exist yet, titled and tagged
|
|
156
|
+
* so it reads as a voice call in Studio's thread list instead of an untitled thread that
|
|
157
|
+
* only appears after the first exchange.
|
|
158
|
+
*/
|
|
159
|
+
async function ensureVoiceCallThread({ memory, threadId, resourceId, roomName }) {
|
|
160
|
+
if (await memory.getThreadById({ threadId })) return;
|
|
161
|
+
await memory.createThread({
|
|
162
|
+
threadId,
|
|
163
|
+
resourceId,
|
|
164
|
+
title: "Voice call",
|
|
165
|
+
metadata: {
|
|
166
|
+
source: "livekit",
|
|
167
|
+
roomName
|
|
168
|
+
}
|
|
169
|
+
});
|
|
170
|
+
}
|
|
171
|
+
/**
|
|
172
|
+
* Persists the spoken greeting as an assistant message. The greeting is spoken via TTS
|
|
173
|
+
* without going through the Mastra agent, so without this the saved thread would start
|
|
174
|
+
* at the caller's first words instead of being a faithful call transcript.
|
|
175
|
+
*/
|
|
176
|
+
async function persistSpokenGreeting({ memory, threadId, resourceId, greeting }) {
|
|
177
|
+
if (!greeting.trim()) return;
|
|
178
|
+
const message = {
|
|
179
|
+
id: (0, crypto.randomUUID)(),
|
|
180
|
+
role: "assistant",
|
|
181
|
+
type: "text",
|
|
182
|
+
createdAt: /* @__PURE__ */ new Date(),
|
|
183
|
+
threadId,
|
|
184
|
+
resourceId,
|
|
185
|
+
content: {
|
|
186
|
+
format: 2,
|
|
187
|
+
parts: [{
|
|
188
|
+
type: "text",
|
|
189
|
+
text: greeting
|
|
190
|
+
}],
|
|
191
|
+
metadata: {
|
|
192
|
+
source: "voice",
|
|
193
|
+
kind: "greeting"
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
};
|
|
197
|
+
await memory.saveMessages({ messages: [message] });
|
|
198
|
+
}
|
|
199
|
+
//#endregion
|
|
200
|
+
//#region src/worker-setup.ts
|
|
201
|
+
/**
|
|
202
|
+
* Coordination between worker definition and worker startup. LiveKit plugins register
|
|
203
|
+
* their inference runners at module-import time, and the AgentServer only spawns the
|
|
204
|
+
* inference process for runners registered before it starts — so plugin imports kicked
|
|
205
|
+
* off by `createLiveKitWorker()` must complete before `runLiveKitWorker()` boots the
|
|
206
|
+
* server.
|
|
207
|
+
*/
|
|
208
|
+
const pendingSetup = [];
|
|
209
|
+
const requestedEouMethods = /* @__PURE__ */ new Set();
|
|
210
|
+
function queueWorkerSetup(setup) {
|
|
211
|
+
pendingSetup.push(setup);
|
|
212
|
+
}
|
|
213
|
+
function workerSetupComplete() {
|
|
214
|
+
return Promise.allSettled(pendingSetup);
|
|
215
|
+
}
|
|
216
|
+
function requestEouMethod(method) {
|
|
217
|
+
requestedEouMethods.add(method);
|
|
218
|
+
}
|
|
219
|
+
function isEouMethodRequested(method) {
|
|
220
|
+
return requestedEouMethods.has(method);
|
|
221
|
+
}
|
|
222
|
+
//#endregion
|
|
223
|
+
//#region src/worker.ts
|
|
224
|
+
const EOU_METHODS = {
|
|
225
|
+
english: "lk_end_of_utterance_en",
|
|
226
|
+
multilingual: "lk_end_of_utterance_multilingual"
|
|
227
|
+
};
|
|
228
|
+
async function loadSileroVad() {
|
|
229
|
+
let silero;
|
|
230
|
+
try {
|
|
231
|
+
silero = await import("@livekit/agents-plugin-silero");
|
|
232
|
+
} catch (error) {
|
|
233
|
+
throw new Error("@mastra/livekit: voice activity detection requires '@livekit/agents-plugin-silero'. Install it, pass your own `vad` instance, or set `vad: false`.", { cause: error });
|
|
234
|
+
}
|
|
235
|
+
return silero.VAD.load();
|
|
236
|
+
}
|
|
237
|
+
async function loadTurnDetector(kind) {
|
|
238
|
+
let plugin;
|
|
239
|
+
try {
|
|
240
|
+
plugin = await import("@livekit/agents-plugin-livekit");
|
|
241
|
+
} catch (error) {
|
|
242
|
+
throw new Error(`@mastra/livekit: turnDetection '${kind}' requires '@livekit/agents-plugin-livekit'. Install it or use a built-in mode like 'vad' or 'stt'.`, { cause: error });
|
|
243
|
+
}
|
|
244
|
+
return kind === "english" ? new plugin.turnDetector.EnglishModel() : new plugin.turnDetector.MultilingualModel();
|
|
245
|
+
}
|
|
246
|
+
async function resolveMastraAgent(options, args) {
|
|
247
|
+
let ref = typeof options.agent === "function" ? await options.agent(args) : options.agent;
|
|
248
|
+
ref ??= args.metadata.agentId;
|
|
249
|
+
if (!ref) throw new Error("@mastra/livekit: no Mastra agent specified. Set `agent` on createLiveKitWorker or pass `agentId` in the dispatch metadata (e.g. via liveKitConnectionRoute).");
|
|
250
|
+
if (typeof ref !== "string") return ref;
|
|
251
|
+
try {
|
|
252
|
+
return options.mastra.getAgentById(ref);
|
|
253
|
+
} catch {
|
|
254
|
+
return options.mastra.getAgent(ref);
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
async function resolveWorkflow(options, args) {
|
|
258
|
+
const resolver = options.workflow;
|
|
259
|
+
const ref = typeof resolver === "function" ? await resolver(args) : resolver ?? "";
|
|
260
|
+
if (!ref) throw new Error("@mastra/livekit: no workflow specified. Set `workflow` on createLiveKitWorker.");
|
|
261
|
+
if (typeof ref !== "string") return { workflow: ref };
|
|
262
|
+
return { workflow: options.mastra.getWorkflowById(ref) };
|
|
263
|
+
}
|
|
264
|
+
function resolveMemory(options, mastraAgent, args, roomName) {
|
|
265
|
+
if (options.memory === false) return false;
|
|
266
|
+
if (typeof options.memory === "function") return options.memory({
|
|
267
|
+
...args,
|
|
268
|
+
roomName
|
|
269
|
+
});
|
|
270
|
+
if (mastraAgent && !mastraAgent.hasOwnMemory()) return false;
|
|
271
|
+
const thread = args.metadata.threadId ?? roomName;
|
|
272
|
+
return {
|
|
273
|
+
thread,
|
|
274
|
+
resource: args.metadata.resourceId ?? thread
|
|
275
|
+
};
|
|
276
|
+
}
|
|
277
|
+
async function resolveMemoryInstance(options, mastraAgent, args, requestContext) {
|
|
278
|
+
if (mastraAgent) return await mastraAgent.getMemory({ requestContext }) ?? null;
|
|
279
|
+
const resolver = options.memoryInstance;
|
|
280
|
+
if (!resolver) return null;
|
|
281
|
+
const instance = typeof resolver === "function" ? await resolver(args) : resolver;
|
|
282
|
+
if (!instance) return null;
|
|
283
|
+
instance.__registerMastra(options.mastra);
|
|
284
|
+
if (!instance.hasOwnStorage) {
|
|
285
|
+
const storage = options.mastra.getStorage();
|
|
286
|
+
if (storage) instance.setStorage(storage);
|
|
287
|
+
}
|
|
288
|
+
return instance;
|
|
289
|
+
}
|
|
290
|
+
function buildTurnHandling(options, turnDetection) {
|
|
291
|
+
return {
|
|
292
|
+
...turnDetection ? { turnDetection } : {},
|
|
293
|
+
preemptiveGeneration: { enabled: false },
|
|
294
|
+
...options.turnHandling
|
|
295
|
+
};
|
|
296
|
+
}
|
|
297
|
+
async function resolveInstructions(mastraAgent, requestContext) {
|
|
298
|
+
try {
|
|
299
|
+
const instructions = await mastraAgent.getInstructions({ requestContext });
|
|
300
|
+
return typeof instructions === "string" ? instructions : void 0;
|
|
301
|
+
} catch {
|
|
302
|
+
return;
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
/**
|
|
306
|
+
* Resolves the effective greeting from the canonical `configuration.greeting` and the deprecated
|
|
307
|
+
* top-level `greeting` / `persistGreeting` options. The legacy options are the base so existing
|
|
308
|
+
* worker configs keep working unchanged; `configuration.greeting` overrides field-by-field. Exported
|
|
309
|
+
* for unit testing.
|
|
310
|
+
*/
|
|
311
|
+
function resolveGreetingConfig(options) {
|
|
312
|
+
return {
|
|
313
|
+
text: options.greeting,
|
|
314
|
+
persist: options.persistGreeting,
|
|
315
|
+
...options.configuration?.greeting
|
|
316
|
+
};
|
|
317
|
+
}
|
|
318
|
+
/**
|
|
319
|
+
* Resolves the greeting text for a call: returns a fixed string as-is, or invokes the resolver form
|
|
320
|
+
* with the call context to produce a per-tenant greeting. Empty / whitespace-nothing results
|
|
321
|
+
* normalize to `undefined` (no greeting). Exported for unit testing.
|
|
322
|
+
*/
|
|
323
|
+
async function resolveGreetingText(text, context) {
|
|
324
|
+
return (typeof text === "function" ? await text(context) : text) || void 0;
|
|
325
|
+
}
|
|
326
|
+
/**
|
|
327
|
+
* Resolves a per-call session component (STT / TTS): invokes the `configuration` resolver with the
|
|
328
|
+
* call context, falling back to the static top-level option when there is no resolver or it
|
|
329
|
+
* resolves to `undefined`. Exported for unit testing.
|
|
330
|
+
*/
|
|
331
|
+
async function resolveSessionComponent(resolver, fallback, context) {
|
|
332
|
+
return (resolver ? await resolver(context) : void 0) ?? fallback;
|
|
333
|
+
}
|
|
334
|
+
/**
|
|
335
|
+
* Speaks the session's opening greeting, honoring the interruption / playout options. Takes the
|
|
336
|
+
* already-resolved greeting (see {@link resolveGreetingText}) and returns the LiveKit `SpeechHandle`,
|
|
337
|
+
* or `undefined` when there is no greeting text. Extracted and exported so the greeting behavior is
|
|
338
|
+
* unit-testable without a live room.
|
|
339
|
+
*/
|
|
340
|
+
async function speakGreeting(session, greeting) {
|
|
341
|
+
if (!greeting.text) return void 0;
|
|
342
|
+
const handle = session.say(greeting.text, greeting.allowInterruptions === void 0 ? void 0 : { allowInterruptions: greeting.allowInterruptions });
|
|
343
|
+
if (greeting.awaitPlayout) await handle.waitForPlayout().catch(() => {});
|
|
344
|
+
return handle;
|
|
345
|
+
}
|
|
346
|
+
/** Default tool name the worker watches for to end the call. Matches `createEndCallTool`'s default. */
|
|
347
|
+
const DEFAULT_END_CALL_TOOL = "endCall";
|
|
348
|
+
/** Default shutdown reason recorded when the agent ends the call. */
|
|
349
|
+
const DEFAULT_END_CALL_REASON = "agent ended call";
|
|
350
|
+
/** Default safety cap on waiting for the agent's closing words to finish before hanging up. */
|
|
351
|
+
const DEFAULT_END_CALL_MAX_WAIT_MS = 3e4;
|
|
352
|
+
/**
|
|
353
|
+
* Default post-playout drain before the room is deleted. LiveKit's "playout completed" is
|
|
354
|
+
* worker-local; the caller's client still holds network + jitter-buffer audio, so hanging up the
|
|
355
|
+
* instant the state clears clips the tail of the goodbye.
|
|
356
|
+
*/
|
|
357
|
+
const DEFAULT_END_CALL_DRAIN_MS = 800;
|
|
358
|
+
/** Agent states where the agent is still busy producing / playing a reply (not done speaking). */
|
|
359
|
+
const AGENT_BUSY_STATES = /* @__PURE__ */ new Set(["thinking", "speaking"]);
|
|
360
|
+
/**
|
|
361
|
+
* Resolves once the agent is no longer producing or playing a reply — i.e. its state has left
|
|
362
|
+
* `thinking`/`speaking` for `listening`/`idle`. Returns immediately when it's already idle. A
|
|
363
|
+
* `maxWaitMs` safety cap guarantees it resolves even if the speaking state never clears. Used before
|
|
364
|
+
* an agent-initiated hang-up so the closing words play out fully instead of being cut off by the
|
|
365
|
+
* session close. Exported for unit testing.
|
|
366
|
+
*/
|
|
367
|
+
function waitForAgentDoneSpeaking(session, maxWaitMs = DEFAULT_END_CALL_MAX_WAIT_MS) {
|
|
368
|
+
if (!AGENT_BUSY_STATES.has(session.agentState)) return Promise.resolve();
|
|
369
|
+
return new Promise((resolve) => {
|
|
370
|
+
let timer;
|
|
371
|
+
const onChange = (ev) => {
|
|
372
|
+
if (AGENT_BUSY_STATES.has(ev.newState)) return;
|
|
373
|
+
cleanup();
|
|
374
|
+
resolve();
|
|
375
|
+
};
|
|
376
|
+
const cleanup = () => {
|
|
377
|
+
session.off(_livekit_agents.voice.AgentSessionEventTypes.AgentStateChanged, onChange);
|
|
378
|
+
if (timer) clearTimeout(timer);
|
|
379
|
+
};
|
|
380
|
+
session.on(_livekit_agents.voice.AgentSessionEventTypes.AgentStateChanged, onChange);
|
|
381
|
+
timer = setTimeout(() => {
|
|
382
|
+
cleanup();
|
|
383
|
+
resolve();
|
|
384
|
+
}, maxWaitMs);
|
|
385
|
+
timer.unref?.();
|
|
386
|
+
});
|
|
387
|
+
}
|
|
388
|
+
/**
|
|
389
|
+
* Ends the call after the agent asked to (via its end-call tool): wait for the agent's closing words
|
|
390
|
+
* to finish, speak an optional final `message`, hold a short drain (`drainMs`) so audio buffered at
|
|
391
|
+
* the caller finishes playing, then disconnect. The teardown's session close
|
|
392
|
+
* force-interrupts any playing speech, so the waits here MUST complete before we disconnect — that's
|
|
393
|
+
* the whole point of the sequence. `ctx.deleteRoom()` hangs up the caller (SIP-safe); `ctx.shutdown()`
|
|
394
|
+
* ends the job and runs the registered shutdown callbacks (`onCallEnd`), so end-of-call work happens
|
|
395
|
+
* exactly as it does on a caller hang-up. Both run in a `finally` so a hiccup while waiting still ends
|
|
396
|
+
* the call. Never rejects — every step is guarded and failures are logged, so callers can safely
|
|
397
|
+
* fire-and-forget it (`void runEndCall(...)`) from hooks. Exported for unit testing.
|
|
398
|
+
*/
|
|
399
|
+
async function runEndCall(session, ctx, config, logger) {
|
|
400
|
+
try {
|
|
401
|
+
await waitForAgentDoneSpeaking(session, config.maxWaitMs ?? 3e4);
|
|
402
|
+
if (config.message) await session.say(config.message, { allowInterruptions: false }).waitForPlayout().catch(() => {});
|
|
403
|
+
const drainMs = config.drainMs ?? 800;
|
|
404
|
+
if (drainMs > 0) await new Promise((resolve) => setTimeout(resolve, drainMs));
|
|
405
|
+
} catch (error) {
|
|
406
|
+
logger.warn("@mastra/livekit: waiting for the agent to finish before ending the call failed", error);
|
|
407
|
+
} finally {
|
|
408
|
+
try {
|
|
409
|
+
await ctx.deleteRoom();
|
|
410
|
+
} catch (error) {
|
|
411
|
+
logger.warn("@mastra/livekit: deleteRoom while ending the call failed", error);
|
|
412
|
+
}
|
|
413
|
+
try {
|
|
414
|
+
ctx.shutdown(config.reason ?? "agent ended call");
|
|
415
|
+
} catch (error) {
|
|
416
|
+
logger.warn("@mastra/livekit: shutdown while ending the call failed", error);
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
/**
|
|
421
|
+
* Builds the per-turn hook that detects the agent's end-call tool and kicks off the hang-up once, or
|
|
422
|
+
* `undefined` when end-call isn't configured. The detector fires the teardown fire-and-forget (the
|
|
423
|
+
* turn never waits on it) and de-dupes so a repeated tool call can't start two hang-ups.
|
|
424
|
+
*/
|
|
425
|
+
function buildEndCallDetector(config, getSession, ctx, logger) {
|
|
426
|
+
if (!config) return void 0;
|
|
427
|
+
const toolName = config.tool ?? "endCall";
|
|
428
|
+
let triggered = false;
|
|
429
|
+
return (turnCtx) => {
|
|
430
|
+
if (triggered) return;
|
|
431
|
+
if (!turnCtx.result.toolCalls.some((call) => call.toolName === toolName)) return;
|
|
432
|
+
const session = getSession();
|
|
433
|
+
if (!session) return;
|
|
434
|
+
triggered = true;
|
|
435
|
+
runEndCall(session, ctx, config, logger);
|
|
436
|
+
};
|
|
437
|
+
}
|
|
438
|
+
/** Runs the worker's own turn-complete detector alongside the user's `onTurnComplete`, if any. */
|
|
439
|
+
function composeTurnComplete(user, detector) {
|
|
440
|
+
if (!detector) return user;
|
|
441
|
+
return (ctx) => {
|
|
442
|
+
detector(ctx);
|
|
443
|
+
return user?.(ctx);
|
|
444
|
+
};
|
|
445
|
+
}
|
|
446
|
+
/**
|
|
447
|
+
* Builds a LiveKit agent worker definition that answers voice sessions with Mastra agents.
|
|
448
|
+
*
|
|
449
|
+
* Use as the default export of your worker entry file, then run it with the LiveKit
|
|
450
|
+
* agents CLI:
|
|
451
|
+
*
|
|
452
|
+
* ```ts
|
|
453
|
+
* // src/mastra/voice-worker.ts
|
|
454
|
+
* import { fileURLToPath } from 'node:url';
|
|
455
|
+
* import { createLiveKitWorker, runLiveKitWorker } from '@mastra/livekit/worker';
|
|
456
|
+
* import { mastra } from './index';
|
|
457
|
+
*
|
|
458
|
+
* export default createLiveKitWorker({
|
|
459
|
+
* mastra,
|
|
460
|
+
* stt: 'deepgram/nova-3',
|
|
461
|
+
* tts: 'cartesia/sonic-3',
|
|
462
|
+
* turnDetection: 'multilingual',
|
|
463
|
+
* });
|
|
464
|
+
*
|
|
465
|
+
* if (process.argv[1] === fileURLToPath(import.meta.url)) {
|
|
466
|
+
* runLiveKitWorker({ entry: import.meta.url, agentName: 'mastra-voice' });
|
|
467
|
+
* }
|
|
468
|
+
* ```
|
|
469
|
+
*/
|
|
470
|
+
function createLiveKitWorker(options) {
|
|
471
|
+
if (options.generate && (options.agent || options.workflow)) throw new Error("@mastra/livekit: set exactly one reply generator — `generate`, `agent`, or `workflow` — not a combination.");
|
|
472
|
+
if (options.agent && options.workflow) throw new Error("@mastra/livekit: set `agent` or `workflow`, not both — they are mutually exclusive reply generators.");
|
|
473
|
+
if (options.workflow && !options.workflowInput) throw new Error("@mastra/livekit: `workflowInput` is required when `workflow` is set. Map the turn into the workflow inputData, e.g. workflowInput: ({ chatCtx }) => ({ history: chatContextToMessages(chatCtx) }).");
|
|
474
|
+
if (options.generate && options.configuration?.endCall) throw new Error("@mastra/livekit: `configuration.endCall` has no effect with `generate` — the worker cannot observe tool calls from a custom reply generator. Detect the end-call tool inside your generator and call `runEndCall` directly instead.");
|
|
475
|
+
const wantsSileroVad = options.vad === void 0 || options.vad === "silero";
|
|
476
|
+
if (options.turnDetection === "multilingual" || options.turnDetection === "english") {
|
|
477
|
+
requestEouMethod(EOU_METHODS[options.turnDetection]);
|
|
478
|
+
queueWorkerSetup(import("@livekit/agents-plugin-livekit").then(() => {
|
|
479
|
+
for (const method of Object.values(EOU_METHODS)) if (!isEouMethodRequested(method)) delete _livekit_agents.InferenceRunner.registeredRunners[method];
|
|
480
|
+
}).catch(() => {}));
|
|
481
|
+
}
|
|
482
|
+
return (0, _livekit_agents.defineAgent)({
|
|
483
|
+
prewarm: async (proc) => {
|
|
484
|
+
if (wantsSileroVad) proc.userData.vad = await loadSileroVad();
|
|
485
|
+
},
|
|
486
|
+
entry: async (ctx) => {
|
|
487
|
+
const logger = options.mastra.getLogger();
|
|
488
|
+
const metadata = require_workflow_generator.parseSessionMetadata(ctx.job.metadata);
|
|
489
|
+
const args = {
|
|
490
|
+
metadata,
|
|
491
|
+
ctx
|
|
492
|
+
};
|
|
493
|
+
const requestContext = metadata.requestContext ? new _mastra_core_request_context.RequestContext(Object.entries(metadata.requestContext)) : void 0;
|
|
494
|
+
const endCallDetector = buildEndCallDetector(options.configuration?.endCall, () => session, ctx, logger);
|
|
495
|
+
const onTurnComplete = composeTurnComplete(options.onTurnComplete, endCallDetector);
|
|
496
|
+
let mastraAgent;
|
|
497
|
+
let replyGenerator;
|
|
498
|
+
let agentLabel;
|
|
499
|
+
if (options.generate) {
|
|
500
|
+
replyGenerator = options.generate;
|
|
501
|
+
agentLabel = "mastra-voice";
|
|
502
|
+
} else if (options.workflow) {
|
|
503
|
+
const { workflow } = await resolveWorkflow(options, args);
|
|
504
|
+
agentLabel = workflow.id;
|
|
505
|
+
const mapInput = options.workflowInput;
|
|
506
|
+
replyGenerator = require_workflow_generator.createWorkflowReplyGenerator({
|
|
507
|
+
workflow,
|
|
508
|
+
workflowInput: (turnCtx) => mapInput({
|
|
509
|
+
...turnCtx,
|
|
510
|
+
metadata
|
|
511
|
+
}),
|
|
512
|
+
replyStep: options.replyStep,
|
|
513
|
+
resultText: options.resultText,
|
|
514
|
+
toolFeedback: options.toolFeedback,
|
|
515
|
+
onTurnComplete
|
|
516
|
+
});
|
|
517
|
+
} else {
|
|
518
|
+
mastraAgent = await resolveMastraAgent(options, args);
|
|
519
|
+
agentLabel = mastraAgent.id ?? mastraAgent.name;
|
|
520
|
+
}
|
|
521
|
+
await ctx.connect();
|
|
522
|
+
const roomName = ctx.room.name ?? "mastra-voice";
|
|
523
|
+
let vad;
|
|
524
|
+
if (options.vad && options.vad !== "silero") vad = options.vad;
|
|
525
|
+
else if (wantsSileroVad) vad = ctx.proc.userData.vad ?? await loadSileroVad();
|
|
526
|
+
let turnDetection;
|
|
527
|
+
if (options.turnDetection === "multilingual" || options.turnDetection === "english") turnDetection = await loadTurnDetector(options.turnDetection);
|
|
528
|
+
else turnDetection = options.turnDetection;
|
|
529
|
+
const memory = resolveMemory(options, mastraAgent, args, roomName);
|
|
530
|
+
const memoryInstance = memory ? await resolveMemoryInstance(options, mastraAgent, args, requestContext) : null;
|
|
531
|
+
if (memory && memoryInstance) try {
|
|
532
|
+
await ensureVoiceCallThread({
|
|
533
|
+
memory: memoryInstance,
|
|
534
|
+
threadId: memory.thread,
|
|
535
|
+
resourceId: memory.resource ?? memory.thread,
|
|
536
|
+
roomName
|
|
537
|
+
});
|
|
538
|
+
} catch (error) {
|
|
539
|
+
logger.warn("@mastra/livekit: failed to create the voice call thread", error);
|
|
540
|
+
}
|
|
541
|
+
const voiceObs = options.observability === false ? void 0 : startVoiceCallObservability({
|
|
542
|
+
mastra: options.mastra,
|
|
543
|
+
agentId: agentLabel,
|
|
544
|
+
roomName,
|
|
545
|
+
metadata,
|
|
546
|
+
requestContext
|
|
547
|
+
});
|
|
548
|
+
if (voiceObs) ctx.addShutdownCallback(async () => {
|
|
549
|
+
voiceObs.finalize();
|
|
550
|
+
});
|
|
551
|
+
if (options.onCallEnd) {
|
|
552
|
+
const onCallEnd = options.onCallEnd;
|
|
553
|
+
ctx.addShutdownCallback(async () => {
|
|
554
|
+
try {
|
|
555
|
+
await onCallEnd({
|
|
556
|
+
memory,
|
|
557
|
+
memoryInstance,
|
|
558
|
+
metadata,
|
|
559
|
+
requestContext,
|
|
560
|
+
configuration: options.configuration,
|
|
561
|
+
roomName,
|
|
562
|
+
ctx
|
|
563
|
+
});
|
|
564
|
+
} catch (error) {
|
|
565
|
+
logger.warn("@mastra/livekit: onCallEnd hook threw", error);
|
|
566
|
+
}
|
|
567
|
+
});
|
|
568
|
+
}
|
|
569
|
+
const greetingConfig = resolveGreetingConfig(options);
|
|
570
|
+
const greetingReminder = greetingConfig.repeatEvery && greetingConfig.repeatEvery > 0 ? {
|
|
571
|
+
everyMs: greetingConfig.repeatEvery,
|
|
572
|
+
text: greetingConfig.repeatText
|
|
573
|
+
} : void 0;
|
|
574
|
+
const agent = require_remote.createMastraVoiceAgent({
|
|
575
|
+
...replyGenerator ? { generate: replyGenerator } : { agent: mastraAgent },
|
|
576
|
+
instructions: mastraAgent ? await resolveInstructions(mastraAgent, requestContext) : void 0,
|
|
577
|
+
memory,
|
|
578
|
+
requestContext,
|
|
579
|
+
toolFeedback: options.toolFeedback,
|
|
580
|
+
onTurnComplete,
|
|
581
|
+
greetingReminder,
|
|
582
|
+
streamOptions: voiceObs ? { tracingContext: voiceObs.tracingContext } : void 0
|
|
583
|
+
});
|
|
584
|
+
const callContext = {
|
|
585
|
+
metadata,
|
|
586
|
+
requestContext,
|
|
587
|
+
roomName,
|
|
588
|
+
ctx
|
|
589
|
+
};
|
|
590
|
+
const [stt, tts] = await Promise.all([resolveSessionComponent(options.configuration?.stt, options.stt, callContext), resolveSessionComponent(options.configuration?.tts, options.tts, callContext)]);
|
|
591
|
+
const session = new _livekit_agents.voice.AgentSession({
|
|
592
|
+
stt,
|
|
593
|
+
tts,
|
|
594
|
+
vad,
|
|
595
|
+
turnHandling: buildTurnHandling(options, turnDetection),
|
|
596
|
+
...options.sessionOptions
|
|
597
|
+
});
|
|
598
|
+
voiceObs?.attach(session);
|
|
599
|
+
try {
|
|
600
|
+
await session.start({
|
|
601
|
+
agent,
|
|
602
|
+
room: ctx.room,
|
|
603
|
+
inputOptions: options.inputOptions,
|
|
604
|
+
outputOptions: options.outputOptions
|
|
605
|
+
});
|
|
606
|
+
if (greetingConfig.text) {
|
|
607
|
+
const greetingText = await resolveGreetingText(greetingConfig.text, callContext);
|
|
608
|
+
if (greetingText) {
|
|
609
|
+
await speakGreeting(session, {
|
|
610
|
+
...greetingConfig,
|
|
611
|
+
text: greetingText
|
|
612
|
+
});
|
|
613
|
+
if (greetingConfig.persist !== false && memory && memoryInstance) try {
|
|
614
|
+
await persistSpokenGreeting({
|
|
615
|
+
memory: memoryInstance,
|
|
616
|
+
threadId: memory.thread,
|
|
617
|
+
resourceId: memory.resource ?? memory.thread,
|
|
618
|
+
greeting: greetingText
|
|
619
|
+
});
|
|
620
|
+
} catch (error) {
|
|
621
|
+
logger.warn("@mastra/livekit: failed to persist the greeting", error);
|
|
622
|
+
}
|
|
623
|
+
}
|
|
624
|
+
}
|
|
625
|
+
await options.onSessionStart?.({
|
|
626
|
+
session,
|
|
627
|
+
ctx,
|
|
628
|
+
agent,
|
|
629
|
+
metadata
|
|
630
|
+
});
|
|
631
|
+
} catch (error) {
|
|
632
|
+
voiceObs?.finalize({ error });
|
|
633
|
+
throw error;
|
|
634
|
+
}
|
|
635
|
+
}
|
|
636
|
+
});
|
|
637
|
+
}
|
|
638
|
+
//#endregion
|
|
639
|
+
//#region src/run.ts
|
|
640
|
+
function resolveWorkerEntryPath(entry) {
|
|
641
|
+
if (entry instanceof URL) return (0, url.fileURLToPath)(entry);
|
|
642
|
+
return entry.startsWith("file:") ? (0, url.fileURLToPath)(entry) : entry;
|
|
643
|
+
}
|
|
644
|
+
/**
|
|
645
|
+
* Starts the LiveKit agent worker CLI (`dev` / `start` / `connect` subcommands) for a
|
|
646
|
+
* worker entry file. Call it from the same file that default-exports
|
|
647
|
+
* {@link createLiveKitWorker}, guarded so it only runs when executed directly:
|
|
648
|
+
*
|
|
649
|
+
* ```ts
|
|
650
|
+
* import { fileURLToPath } from 'node:url';
|
|
651
|
+
* import { createLiveKitWorker, runLiveKitWorker } from '@mastra/livekit/worker';
|
|
652
|
+
* import { mastra } from './index';
|
|
653
|
+
*
|
|
654
|
+
* export default createLiveKitWorker({ mastra, agent: 'support' });
|
|
655
|
+
*
|
|
656
|
+
* if (process.argv[1] === fileURLToPath(import.meta.url)) {
|
|
657
|
+
* runLiveKitWorker({ entry: import.meta.url, agentName: 'mastra-voice' });
|
|
658
|
+
* }
|
|
659
|
+
* ```
|
|
660
|
+
*
|
|
661
|
+
* Using this helper (instead of `cli.runApp` from `@livekit/agents`) guarantees the worker
|
|
662
|
+
* runtime and the bridge share one copy of the LiveKit SDK.
|
|
663
|
+
*/
|
|
664
|
+
function runLiveKitWorker(options) {
|
|
665
|
+
workerSetupComplete().then(() => {
|
|
666
|
+
_livekit_agents.cli.runApp(new _livekit_agents.ServerOptions({
|
|
667
|
+
agent: resolveWorkerEntryPath(options.entry),
|
|
668
|
+
agentName: options.agentName ?? "mastra-voice",
|
|
669
|
+
...options.serverOptions
|
|
670
|
+
}));
|
|
671
|
+
});
|
|
672
|
+
}
|
|
673
|
+
//#endregion
|
|
674
|
+
exports.DEFAULT_END_CALL_DRAIN_MS = DEFAULT_END_CALL_DRAIN_MS;
|
|
675
|
+
exports.DEFAULT_END_CALL_MAX_WAIT_MS = DEFAULT_END_CALL_MAX_WAIT_MS;
|
|
676
|
+
exports.DEFAULT_END_CALL_REASON = DEFAULT_END_CALL_REASON;
|
|
677
|
+
exports.DEFAULT_END_CALL_TOOL = DEFAULT_END_CALL_TOOL;
|
|
678
|
+
exports.chatContextToMessages = require_remote.chatContextToMessages;
|
|
679
|
+
exports.createLiveKitWorker = createLiveKitWorker;
|
|
680
|
+
exports.createRemoteAgentReplyGenerator = require_remote.createRemoteAgentReplyGenerator;
|
|
681
|
+
exports.runEndCall = runEndCall;
|
|
682
|
+
exports.runLiveKitWorker = runLiveKitWorker;
|
|
683
|
+
exports.speakGreeting = speakGreeting;
|
|
684
|
+
exports.waitForAgentDoneSpeaking = waitForAgentDoneSpeaking;
|
|
685
|
+
|
|
686
|
+
//# sourceMappingURL=worker-entry.cjs.map
|