@mastra/livekit 0.0.0-aggregate-trace-20261015182851

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.
Files changed (83) hide show
  1. package/LICENSE.md +32 -0
  2. package/README.md +48 -0
  3. package/dist/bridge.d.ts +256 -0
  4. package/dist/bridge.d.ts.map +1 -0
  5. package/dist/client-entry.cjs +17 -0
  6. package/dist/client-entry.cjs.map +1 -0
  7. package/dist/client-entry.d.ts +8 -0
  8. package/dist/client-entry.d.ts.map +1 -0
  9. package/dist/client-entry.js +16 -0
  10. package/dist/client-entry.js.map +1 -0
  11. package/dist/consent.d.ts +55 -0
  12. package/dist/consent.d.ts.map +1 -0
  13. package/dist/constants.d.ts +3 -0
  14. package/dist/constants.d.ts.map +1 -0
  15. package/dist/dispatch.d.ts +26 -0
  16. package/dist/dispatch.d.ts.map +1 -0
  17. package/dist/end-call.d.ts +50 -0
  18. package/dist/end-call.d.ts.map +1 -0
  19. package/dist/index.cjs +290 -0
  20. package/dist/index.cjs.map +1 -0
  21. package/dist/index.d.ts +20 -0
  22. package/dist/index.d.ts.map +1 -0
  23. package/dist/index.js +281 -0
  24. package/dist/index.js.map +1 -0
  25. package/dist/llm-plugin.d.ts +117 -0
  26. package/dist/llm-plugin.d.ts.map +1 -0
  27. package/dist/messages.d.ts +50 -0
  28. package/dist/messages.d.ts.map +1 -0
  29. package/dist/metadata.d.ts +17 -0
  30. package/dist/metadata.d.ts.map +1 -0
  31. package/dist/observability.d.ts +49 -0
  32. package/dist/observability.d.ts.map +1 -0
  33. package/dist/plugin-entry.cjs +222 -0
  34. package/dist/plugin-entry.cjs.map +1 -0
  35. package/dist/plugin-entry.d.ts +10 -0
  36. package/dist/plugin-entry.d.ts.map +1 -0
  37. package/dist/plugin-entry.js +217 -0
  38. package/dist/plugin-entry.js.map +1 -0
  39. package/dist/recording-route.d.ts +31 -0
  40. package/dist/recording-route.d.ts.map +1 -0
  41. package/dist/recording-types.d.ts +11 -0
  42. package/dist/recording-types.d.ts.map +1 -0
  43. package/dist/recording.d.ts +21 -0
  44. package/dist/recording.d.ts.map +1 -0
  45. package/dist/remote-CIMCGicy.js +822 -0
  46. package/dist/remote-CIMCGicy.js.map +1 -0
  47. package/dist/remote-GSOxa--f.cjs +875 -0
  48. package/dist/remote-GSOxa--f.cjs.map +1 -0
  49. package/dist/remote.d.ts +68 -0
  50. package/dist/remote.d.ts.map +1 -0
  51. package/dist/routes.d.ts +66 -0
  52. package/dist/routes.d.ts.map +1 -0
  53. package/dist/run.d.ts +32 -0
  54. package/dist/run.d.ts.map +1 -0
  55. package/dist/speech.d.ts +19 -0
  56. package/dist/speech.d.ts.map +1 -0
  57. package/dist/turn-metrics-CqqzdLDe.js +53 -0
  58. package/dist/turn-metrics-CqqzdLDe.js.map +1 -0
  59. package/dist/turn-metrics-CtVHHdJj.cjs +82 -0
  60. package/dist/turn-metrics-CtVHHdJj.cjs.map +1 -0
  61. package/dist/turn-metrics.d.ts +69 -0
  62. package/dist/turn-metrics.d.ts.map +1 -0
  63. package/dist/voice-stream.d.ts +3 -0
  64. package/dist/voice-stream.d.ts.map +1 -0
  65. package/dist/voice-thread.d.ts +23 -0
  66. package/dist/voice-thread.d.ts.map +1 -0
  67. package/dist/worker-entry.cjs +727 -0
  68. package/dist/worker-entry.cjs.map +1 -0
  69. package/dist/worker-entry.d.ts +15 -0
  70. package/dist/worker-entry.d.ts.map +1 -0
  71. package/dist/worker-entry.js +711 -0
  72. package/dist/worker-entry.js.map +1 -0
  73. package/dist/worker-setup.d.ts +5 -0
  74. package/dist/worker-setup.d.ts.map +1 -0
  75. package/dist/worker.d.ts +487 -0
  76. package/dist/worker.d.ts.map +1 -0
  77. package/dist/workflow-generator-5nHqlZV7.js +210 -0
  78. package/dist/workflow-generator-5nHqlZV7.js.map +1 -0
  79. package/dist/workflow-generator-D67FvbFe.cjs +239 -0
  80. package/dist/workflow-generator-D67FvbFe.cjs.map +1 -0
  81. package/dist/workflow-generator.d.ts +92 -0
  82. package/dist/workflow-generator.d.ts.map +1 -0
  83. package/package.json +121 -0
@@ -0,0 +1,875 @@
1
+ const require_turn_metrics = require("./turn-metrics-CtVHHdJj.cjs");
2
+ let stream_web = require("stream/web");
3
+ let _livekit_agents = require("@livekit/agents");
4
+ let _mastra_core_request_context = require("@mastra/core/request-context");
5
+ function textOfMessage(message) {
6
+ const parts = [];
7
+ for (const part of message.content) if (typeof part === "string") parts.push(part);
8
+ else if (part.type === "instructions") parts.push(part.value);
9
+ else if (part.type === "audio_content" && part.transcript) parts.push(part.transcript);
10
+ return parts.join("\n").trim();
11
+ }
12
+ function toVoiceTurnMessage(item) {
13
+ if (item.type !== "message") return void 0;
14
+ const content = textOfMessage(item);
15
+ if (!content) return void 0;
16
+ const id = item.id;
17
+ if (item.role === "user") return {
18
+ role: "user",
19
+ content,
20
+ id
21
+ };
22
+ if (item.role === "assistant") return {
23
+ role: "assistant",
24
+ content,
25
+ id
26
+ };
27
+ return {
28
+ role: "system",
29
+ content,
30
+ id
31
+ };
32
+ }
33
+ /**
34
+ * Extracts only the messages added since the agent last spoke. Used when Mastra Memory is
35
+ * the source of truth for conversation history: prior turns are already persisted in the
36
+ * thread, so re-sending them would duplicate history.
37
+ *
38
+ * Two extensions over the naive "slice after the last assistant message":
39
+ *
40
+ * - **Interrupted-turn self-heal:** when the last assistant message was cut off by barge-in
41
+ * (`interrupted: true`), the server never persisted it — aborted runs skip persistence — so
42
+ * its heard-only text is missing from the thread. Re-send that fragment (ordered first) this
43
+ * turn to backfill it. It stops being "the last assistant message" once a full reply lands,
44
+ * so each interrupted fragment is sent exactly once, on the following turn.
45
+ * - **Instructions filter:** LiveKit injects the customer Agent's `instructions` as a
46
+ * leading `system` message ({@link LIVEKIT_INSTRUCTIONS_MESSAGE_ID}); the server-side Mastra
47
+ * agent owns its own system prompt, so drop it (it would otherwise ship on the first turn,
48
+ * before any assistant message).
49
+ */
50
+ function extractNewTurnMessages(chatCtx) {
51
+ const items = chatCtx.items;
52
+ let lastAssistantIdx = -1;
53
+ for (let i = items.length - 1; i >= 0; i--) {
54
+ const item = items[i];
55
+ if (item?.type === "message" && item.role === "assistant") {
56
+ lastAssistantIdx = i;
57
+ break;
58
+ }
59
+ }
60
+ const lastAssistant = lastAssistantIdx >= 0 ? items[lastAssistantIdx] : void 0;
61
+ const startIdx = lastAssistant?.type === "message" && lastAssistant.role === "assistant" && lastAssistant.interrupted ? lastAssistantIdx : lastAssistantIdx + 1;
62
+ const messages = [];
63
+ for (const item of items.slice(startIdx)) {
64
+ if (item.type === "message" && item.id === "lk.agent_task.instructions") continue;
65
+ const message = toVoiceTurnMessage(item);
66
+ if (message) messages.push(message);
67
+ }
68
+ return messages;
69
+ }
70
+ /**
71
+ * Converts the full LiveKit chat context to Mastra messages. Used when the bridge runs
72
+ * without Mastra Memory and LiveKit's in-session context is the only history. The agent's
73
+ * LiveKit-level instructions are excluded — the Mastra agent applies its own instructions.
74
+ */
75
+ function chatContextToMessages(chatCtx) {
76
+ const withoutInstructions = chatCtx.copy({
77
+ excludeInstructions: true,
78
+ excludeFunctionCall: true
79
+ });
80
+ const messages = [];
81
+ for (const item of withoutInstructions.items) {
82
+ const message = toVoiceTurnMessage(item);
83
+ if (message) messages.push(message);
84
+ }
85
+ return messages;
86
+ }
87
+ //#endregion
88
+ //#region src/voice-stream.ts
89
+ /** A one-to-one mapping that awaits upstream cancellation, including pending reads. */
90
+ function mapVoiceStream(source, map) {
91
+ const reader = source.getReader();
92
+ let cancelled = false;
93
+ return new ReadableStream({
94
+ async pull(controller) {
95
+ try {
96
+ const { done, value } = await reader.read();
97
+ if (cancelled) return;
98
+ if (done) controller.close();
99
+ else controller.enqueue(map(value));
100
+ } catch (error) {
101
+ if (!cancelled) controller.error(error);
102
+ }
103
+ },
104
+ cancel(reason) {
105
+ cancelled = true;
106
+ return reader.cancel(reason);
107
+ }
108
+ });
109
+ }
110
+ //#endregion
111
+ //#region src/bridge.ts
112
+ const DEFAULT_INSTRUCTIONS = "You are a helpful voice assistant powered by a Mastra agent.";
113
+ /**
114
+ * Tracks periodic AI re-disclosure for a single call. `due()` returns the reminder text once
115
+ * `everyMs` has elapsed since the last disclosure (resetting the clock), otherwise `undefined`.
116
+ * Time is injectable so the interval logic is deterministically testable.
117
+ */
118
+ var DisclosureReminder = class {
119
+ everyMs;
120
+ text;
121
+ lastAt;
122
+ constructor(everyMs, text, now = Date.now()) {
123
+ this.everyMs = everyMs;
124
+ this.text = text;
125
+ this.lastAt = now;
126
+ }
127
+ /** Call once per turn: the reminder text if it's due, else `undefined`. Does not reset the clock —
128
+ * call {@link DisclosureReminder.markDelivered} once the reminder is actually threaded into the
129
+ * outgoing reply, so a reminder that never makes it out isn't silently skipped for a full interval. */
130
+ due(now = Date.now()) {
131
+ if (now - this.lastAt < this.everyMs) return void 0;
132
+ return this.text;
133
+ }
134
+ /** Resets the clock. Call only once the reminder text from {@link due} was actually emitted. */
135
+ markDelivered(now = Date.now()) {
136
+ this.lastAt = now;
137
+ }
138
+ };
139
+ /**
140
+ * Wraps `source` in a stream that emits `text` as a single leading chunk (with a trailing space, so
141
+ * TTS pauses before the reply) before piping the rest of `source` through unchanged. Cancelling the
142
+ * wrapper cancels `source` — so barge-in still aborts the underlying generation.
143
+ */
144
+ function prependText(source, text) {
145
+ const prefix = text.endsWith(" ") ? text : `${text} `;
146
+ const reader = source.getReader();
147
+ return new stream_web.ReadableStream({
148
+ start(controller) {
149
+ controller.enqueue(prefix);
150
+ },
151
+ async pull(controller) {
152
+ try {
153
+ const { done, value } = await reader.read();
154
+ if (done) controller.close();
155
+ else controller.enqueue(value);
156
+ } catch (error) {
157
+ controller.error(error);
158
+ }
159
+ },
160
+ cancel(reason) {
161
+ return reader.cancel(reason);
162
+ }
163
+ });
164
+ }
165
+ /**
166
+ * Maps a Mastra `finish` chunk's usage (`payload.output.usage`, AI-SDK `LanguageModelUsage`) to the
167
+ * LiveKit-shaped {@link VoiceTurnUsage}, or `undefined` when the chunk carries no token counts.
168
+ * Handles both the flat V2 usage shape (`inputTokens`/`outputTokens`) and the nested V3 shape
169
+ * (`inputTokens.total`/`outputTokens.total`).
170
+ */
171
+ function mapTurnUsage(usage) {
172
+ if (!usage || typeof usage !== "object") return void 0;
173
+ const u = usage;
174
+ const totalOf = (v) => {
175
+ if (typeof v === "number") return v;
176
+ if (v && typeof v === "object" && typeof v.total === "number") return v.total;
177
+ };
178
+ const cacheReadOf = (v) => v && typeof v === "object" && typeof v.cacheRead === "number" ? v.cacheRead : void 0;
179
+ const promptTokens = totalOf(u.inputTokens) ?? 0;
180
+ const completionTokens = totalOf(u.outputTokens) ?? 0;
181
+ const promptCachedTokens = (typeof u.cachedInputTokens === "number" ? u.cachedInputTokens : cacheReadOf(u.inputTokens)) ?? 0;
182
+ const totalTokens = typeof u.totalTokens === "number" ? u.totalTokens : promptTokens + completionTokens;
183
+ if (promptTokens === 0 && completionTokens === 0 && totalTokens === 0 && promptCachedTokens === 0) return;
184
+ return {
185
+ promptTokens,
186
+ completionTokens,
187
+ promptCachedTokens,
188
+ totalTokens
189
+ };
190
+ }
191
+ /**
192
+ * A {@link VoiceReplyGenerator} backed by a Mastra agent: runs the agent's full loop (model,
193
+ * tools, memory) and streams its text deltas. On barge-in the returned stream is cancelled,
194
+ * which aborts the in-flight `agent.stream()`.
195
+ */
196
+ function createAgentReplyGenerator(options) {
197
+ const { agent, streamOptions, toolFeedback, onToolCall, onTurnComplete } = options;
198
+ return (ctx) => {
199
+ if (ctx.messages.length === 0) return null;
200
+ const abortController = new AbortController();
201
+ const mergedOptions = {
202
+ ...streamOptions,
203
+ abortSignal: abortController.signal
204
+ };
205
+ if (ctx.memory) mergedOptions.memory = ctx.memory;
206
+ if (ctx.requestContext) mergedOptions.requestContext = ctx.requestContext;
207
+ let cancelled = false;
208
+ let replyText = "";
209
+ const toolCalls = [];
210
+ let usage;
211
+ const emitTurnComplete = (interrupted) => {
212
+ if (!onTurnComplete) return;
213
+ const completeCtx = {
214
+ ...ctx,
215
+ result: {
216
+ text: replyText,
217
+ toolCalls,
218
+ interrupted,
219
+ usage
220
+ }
221
+ };
222
+ Promise.resolve().then(() => onTurnComplete(completeCtx)).catch((error) => {
223
+ console.warn("@mastra/livekit: onTurnComplete hook threw", error);
224
+ });
225
+ };
226
+ return new stream_web.ReadableStream({
227
+ start: async (controller) => {
228
+ try {
229
+ const result = await agent.stream(ctx.messages, mergedOptions);
230
+ for await (const chunk of result.fullStream) {
231
+ if (cancelled) break;
232
+ if (chunk.type === "text-delta") {
233
+ if (chunk.payload.text) {
234
+ replyText += chunk.payload.text;
235
+ controller.enqueue(chunk.payload.text);
236
+ }
237
+ } else if (chunk.type === "text-end" || chunk.type === "step-finish") controller.enqueue(require_turn_metrics.VOICE_TEXT_FLUSH);
238
+ else if (chunk.type === "tool-result") ctx.metrics?.toolEnd(chunk.payload.toolCallId);
239
+ else if (chunk.type === "tool-call") {
240
+ const toolCall = {
241
+ toolCallId: chunk.payload.toolCallId,
242
+ toolName: chunk.payload.toolName,
243
+ args: chunk.payload.args
244
+ };
245
+ toolCalls.push(toolCall);
246
+ ctx.metrics?.toolStart({
247
+ toolCallId: toolCall.toolCallId,
248
+ toolName: toolCall.toolName
249
+ });
250
+ controller.enqueue(require_turn_metrics.VOICE_TEXT_FLUSH);
251
+ try {
252
+ onToolCall?.(toolCall);
253
+ } catch (error) {
254
+ console.warn("@mastra/livekit: onToolCall hook threw", error);
255
+ }
256
+ if (toolFeedback) {
257
+ let filler;
258
+ try {
259
+ filler = toolFeedback(toolCall);
260
+ } catch (error) {
261
+ console.warn("@mastra/livekit: toolFeedback hook threw", error);
262
+ }
263
+ if (filler) {
264
+ controller.enqueue(filler.endsWith(" ") ? filler : `${filler} `);
265
+ controller.enqueue(require_turn_metrics.VOICE_TEXT_FLUSH);
266
+ }
267
+ }
268
+ } else if (chunk.type === "finish") {
269
+ const output = chunk.payload.output;
270
+ const turnUsage = mapTurnUsage(output?.usage);
271
+ if (turnUsage) {
272
+ usage = turnUsage;
273
+ try {
274
+ ctx.onUsage?.(turnUsage);
275
+ } catch (error) {
276
+ console.warn("@mastra/livekit: onUsage hook threw", error);
277
+ }
278
+ }
279
+ } else if (chunk.type === "error") {
280
+ const error = chunk.payload.error;
281
+ throw error instanceof Error ? error : new Error(String(error));
282
+ }
283
+ }
284
+ if (!cancelled) controller.close();
285
+ emitTurnComplete(cancelled);
286
+ } catch (error) {
287
+ if (cancelled || abortController.signal.aborted) {
288
+ emitTurnComplete(true);
289
+ return;
290
+ }
291
+ controller.error(error);
292
+ }
293
+ },
294
+ cancel: () => {
295
+ cancelled = true;
296
+ abortController.abort();
297
+ }
298
+ });
299
+ };
300
+ }
301
+ function toRequestContext(value) {
302
+ if (!value) return void 0;
303
+ if (value instanceof _mastra_core_request_context.RequestContext) return value;
304
+ return new _mastra_core_request_context.RequestContext(Object.entries(value));
305
+ }
306
+ /**
307
+ * The session only runs its cascaded reply pipeline when an `llm` instance is present —
308
+ * `llmNode` replaces the inference step, but the gate checks `llm instanceof LLM`. This
309
+ * placeholder satisfies the gate; the Mastra agent/workflow does the actual generation.
310
+ */
311
+ var MastraPlaceholderLLM = class extends _livekit_agents.llm.LLM {
312
+ label() {
313
+ return "mastra.MastraVoiceAgent";
314
+ }
315
+ get model() {
316
+ return "mastra-agent";
317
+ }
318
+ get provider() {
319
+ return "mastra";
320
+ }
321
+ chat() {
322
+ throw new Error("@mastra/livekit: reply generation runs through the Mastra agent via llmNode; the placeholder LLM cannot be used for inference.");
323
+ }
324
+ };
325
+ /**
326
+ * A LiveKit `voice.Agent` whose replies come from a Mastra agent or workflow.
327
+ *
328
+ * LiveKit keeps ownership of the audio loop (VAD, STT, turn detection, TTS, barge-in) and calls
329
+ * `llmNode` once per detected user turn; the node delegates to a {@link VoiceReplyGenerator}
330
+ * which streams text deltas back. On barge-in LiveKit cancels the returned stream, which aborts
331
+ * the in-flight generation.
332
+ */
333
+ var MastraVoiceAgent = class extends _livekit_agents.voice.Agent {
334
+ mastraAgent;
335
+ memory;
336
+ requestContext;
337
+ streamOptions;
338
+ replyGenerator;
339
+ onTurnMetrics;
340
+ reminder;
341
+ constructor(options) {
342
+ if (options.agent && options.generate) throw new Error("@mastra/livekit: MastraVoiceAgent requires `agent` or `generate`, not both — they are mutually exclusive reply sources.");
343
+ super({
344
+ id: options.id,
345
+ instructions: options.instructions ?? DEFAULT_INSTRUCTIONS,
346
+ stt: options.stt,
347
+ vad: options.vad,
348
+ llm: new MastraPlaceholderLLM(),
349
+ tts: options.tts,
350
+ turnHandling: options.turnHandling
351
+ });
352
+ this.onTurnMetrics = options.onTurnMetrics;
353
+ this.memory = options.memory ?? false;
354
+ this.requestContext = toRequestContext(options.requestContext);
355
+ this.streamOptions = options.streamOptions;
356
+ if (options.greetingReminder) this.reminder = new DisclosureReminder(options.greetingReminder.everyMs, options.greetingReminder.text?.trim() || "Just a reminder, you're speaking with an AI assistant.");
357
+ if (options.generate) this.replyGenerator = options.generate;
358
+ else if (options.agent) {
359
+ this.mastraAgent = options.agent;
360
+ this.replyGenerator = createAgentReplyGenerator({
361
+ agent: options.agent,
362
+ streamOptions: options.streamOptions,
363
+ toolFeedback: options.toolFeedback,
364
+ onToolCall: options.onToolCall,
365
+ onTurnComplete: options.onTurnComplete
366
+ });
367
+ } else throw new Error("@mastra/livekit: MastraVoiceAgent requires `agent` or `generate`.");
368
+ }
369
+ async llmNode(chatCtx, _toolCtx, _modelSettings) {
370
+ const messages = this.memory === false ? chatContextToMessages(chatCtx) : extractNewTurnMessages(chatCtx);
371
+ if (messages.length === 0) return null;
372
+ const ctx = {
373
+ messages,
374
+ chatCtx,
375
+ memory: this.memory,
376
+ requestContext: this.requestContext,
377
+ tracingContext: this.streamOptions?.tracingContext
378
+ };
379
+ const { reply, identity } = await instrumentVoiceReply(this.replyGenerator, ctx, this.onTurnMetrics);
380
+ if (!reply) return null;
381
+ const reminder = this.reminder?.due();
382
+ if (reminder) this.reminder?.markDelivered();
383
+ return mapVoiceStream(reminder ? prependText(reply, reminder) : reply, (chunk) => {
384
+ if (typeof chunk === "string") return {
385
+ id: identity.attemptId,
386
+ delta: {
387
+ role: "assistant",
388
+ content: chunk,
389
+ extra: { [require_turn_metrics.VOICE_TURN_METADATA]: identity }
390
+ }
391
+ };
392
+ else return _livekit_agents.FlushSentinel;
393
+ });
394
+ }
395
+ };
396
+ function createMastraVoiceAgent(options) {
397
+ return new MastraVoiceAgent(options);
398
+ }
399
+ /** @internal Wrap every reply source, including custom generators, with one terminal metric. */
400
+ async function instrumentVoiceReply(generator, ctx, onTurnMetrics) {
401
+ const identity = {
402
+ turnId: ctx.turnId ?? ctx.messages.findLast((message) => message.role === "user")?.id ?? crypto.randomUUID(),
403
+ attemptId: crypto.randomUUID()
404
+ };
405
+ const metrics = require_turn_metrics.createGenerationMetrics(identity, onTurnMetrics);
406
+ let reply;
407
+ try {
408
+ reply = await generator({
409
+ ...ctx,
410
+ ...identity,
411
+ metrics
412
+ });
413
+ } catch (error) {
414
+ metrics.end("failed");
415
+ throw error;
416
+ }
417
+ if (!reply) {
418
+ metrics.end("completed");
419
+ return {
420
+ reply: null,
421
+ identity
422
+ };
423
+ }
424
+ const reader = reply.getReader();
425
+ let cancelled = false;
426
+ return {
427
+ identity,
428
+ reply: new stream_web.ReadableStream({
429
+ async pull(controller) {
430
+ try {
431
+ const { done, value } = await reader.read();
432
+ if (cancelled) return;
433
+ if (done) {
434
+ metrics.end("completed");
435
+ controller.close();
436
+ } else {
437
+ if (typeof value === "string" && value) metrics.text();
438
+ controller.enqueue(value);
439
+ }
440
+ } catch (error) {
441
+ if (cancelled) return;
442
+ metrics.end("failed");
443
+ controller.error(error);
444
+ }
445
+ },
446
+ cancel(reason) {
447
+ cancelled = true;
448
+ metrics.end("interrupted");
449
+ return reader.cancel(reason);
450
+ }
451
+ })
452
+ };
453
+ }
454
+ //#endregion
455
+ //#region src/speech.ts
456
+ /**
457
+ * Use from a custom Agent.llmNode when using MastraLLM. LiveKit's LLMStream only accepts
458
+ * ChatChunks; this node translates our boundary metadata into LiveKit's supported marker.
459
+ */
460
+ async function mastraLLMNode(agent, chatCtx, toolCtx, modelSettings) {
461
+ const source = await _livekit_agents.voice.Agent.default.llmNode(agent, chatCtx, toolCtx, modelSettings);
462
+ if (!source) return null;
463
+ return mapVoiceStream(source, (chunk) => {
464
+ if (typeof chunk === "object" && chunk.delta?.extra?.["mastra.voice.flush"] === true) return _livekit_agents.FlushSentinel;
465
+ else return chunk;
466
+ });
467
+ }
468
+ function speechResult(handle, outcome) {
469
+ const messages = handle.chatItems.filter((item) => item.type === "message" && item.role === "assistant");
470
+ const identities = messages.map((message) => message.extra[require_turn_metrics.VOICE_TURN_METADATA]);
471
+ const identity = identities[0];
472
+ const sameIdentity = identities.every((value) => value?.turnId === identity?.turnId && value?.attemptId === identity?.attemptId);
473
+ const starts = messages.map((message) => message.metrics.startedSpeakingAt).filter((n) => typeof n === "number" && Number.isFinite(n));
474
+ const stops = messages.map((message) => message.metrics.stoppedSpeakingAt).filter((n) => typeof n === "number" && Number.isFinite(n));
475
+ const firstAudio = messages.map((message) => message.metrics.e2eLatency).find((n) => typeof n === "number" && Number.isFinite(n));
476
+ const speechStartedAt = starts.length ? Math.min(...starts) * 1e3 : void 0;
477
+ const speechEndedAt = stops.length ? Math.max(...stops) * 1e3 : void 0;
478
+ const firstAudioMs = firstAudio === void 0 ? void 0 : firstAudio * 1e3;
479
+ const playedText = messages.map((message) => message.textContent).join("");
480
+ return {
481
+ version: 1,
482
+ phase: "speech",
483
+ turnId: sameIdentity && typeof identity?.turnId === "string" ? identity.turnId : handle.id,
484
+ attemptId: sameIdentity && typeof identity?.attemptId === "string" ? identity.attemptId : void 0,
485
+ speechId: handle.id,
486
+ outcome,
487
+ measurement: "server-playout",
488
+ speechStartedAt,
489
+ speechEndedAt,
490
+ firstAudioMs,
491
+ completionMs: outcome === "completed" && firstAudioMs !== void 0 && speechStartedAt !== void 0 && speechEndedAt !== void 0 ? firstAudioMs + speechEndedAt - speechStartedAt : void 0,
492
+ playedText: messages.length ? playedText : void 0,
493
+ transcriptSource: messages.length ? "livekit" : "unavailable"
494
+ };
495
+ }
496
+ /**
497
+ * Attach before session.start(). Observes generation-independent playback completion;
498
+ * never writes memory. Return value detaches listeners and finalizes pending speech as cancelled.
499
+ * LiveKit's transcript can be estimated. Server output cannot confirm remote human hearing.
500
+ */
501
+ function observeVoiceSession(session, options) {
502
+ const pending = /* @__PURE__ */ new Set();
503
+ let detached = false;
504
+ const finish = (handle, outcome) => {
505
+ if (!pending.delete(handle)) return;
506
+ const result = speechResult(handle, outcome);
507
+ const { playedText: _text, transcriptSource: _source, ...metrics } = result;
508
+ require_turn_metrics.notifyVoiceObserver(options.onTurnMetrics, metrics);
509
+ require_turn_metrics.notifyVoiceObserver(options.onSpeechComplete, result);
510
+ };
511
+ const onSpeech = ({ speechHandle }) => {
512
+ if (detached || pending.has(speechHandle)) return;
513
+ pending.add(speechHandle);
514
+ speechHandle.waitForPlayout().then(() => {
515
+ finish(speechHandle, speechHandle.exception() ? "failed" : speechHandle.interrupted ? "interrupted" : "completed");
516
+ }, () => finish(speechHandle, "failed"));
517
+ };
518
+ const detach = (outcome) => {
519
+ if (detached) return;
520
+ detached = true;
521
+ session.off(_livekit_agents.voice.AgentSessionEventTypes.SpeechCreated, onSpeech);
522
+ session.off(_livekit_agents.voice.AgentSessionEventTypes.Close, onClose);
523
+ for (const handle of pending) finish(handle, outcome);
524
+ };
525
+ const onClose = (event) => detach(event.error ? "failed" : "cancelled");
526
+ session.on(_livekit_agents.voice.AgentSessionEventTypes.SpeechCreated, onSpeech);
527
+ session.on(_livekit_agents.voice.AgentSessionEventTypes.Close, onClose);
528
+ return () => detach("cancelled");
529
+ }
530
+ //#endregion
531
+ //#region src/remote.ts
532
+ const DEFAULT_API_PREFIX = "/api";
533
+ /** Connect + first-token budget when not overridden. Plugin mode passes `connOptions.timeoutMs`. */
534
+ const DEFAULT_REMOTE_TIMEOUT_MS = 1e4;
535
+ /** Thrown (as a plain, non-retryable error) when the server emits a chunk that needs client action. */
536
+ const HITL_UNSUPPORTED_MESSAGE = "@mastra/livekit: the agent requested tool approval or suspended a tool call; human-in-the-loop flows (approve-tool-call / resume-stream) are not supported on the voice path. Remove requireApproval or suspend from the tools this agent uses on voice calls.";
537
+ function trimTrailingSlash(url) {
538
+ return url.endsWith("/") ? url.slice(0, -1) : url;
539
+ }
540
+ function toMessage(error) {
541
+ return error instanceof Error ? error.message : String(error);
542
+ }
543
+ async function resolveHeaders(headers) {
544
+ if (!headers) return {};
545
+ if (typeof headers === "function") return await headers() ?? {};
546
+ return headers;
547
+ }
548
+ function serializeRequestContext(requestContext) {
549
+ if (!requestContext) return void 0;
550
+ if (requestContext instanceof _mastra_core_request_context.RequestContext) return Object.fromEntries(requestContext.entries());
551
+ return requestContext;
552
+ }
553
+ async function safeReadBody(response) {
554
+ try {
555
+ const text = await response.text();
556
+ if (!text) return null;
557
+ try {
558
+ const parsed = JSON.parse(text);
559
+ return parsed && typeof parsed === "object" ? parsed : { message: String(parsed) };
560
+ } catch {
561
+ return { message: text };
562
+ }
563
+ } catch {
564
+ return null;
565
+ }
566
+ }
567
+ /**
568
+ * Reads a Mastra SSE stream and yields each event's parsed JSON. Framing matches the server's
569
+ * `processMastraStream` (buffer, split on `\n\n`, strip `data: `, stop on `[DONE]`); undecodable
570
+ * `data:` lines are skipped. Aborting `signal` cancels the underlying reader.
571
+ */
572
+ async function* readMastraSSE(body, signal) {
573
+ const reader = body.getReader();
574
+ const decoder = new TextDecoder();
575
+ let buffer = "";
576
+ const onAbort = () => void reader.cancel().catch(() => {});
577
+ if (signal.aborted) {
578
+ reader.cancel().catch(() => {});
579
+ return;
580
+ }
581
+ signal.addEventListener("abort", onAbort, { once: true });
582
+ try {
583
+ for (;;) {
584
+ const { done, value } = await reader.read();
585
+ if (done) break;
586
+ buffer += decoder.decode(value, { stream: true });
587
+ const events = buffer.split("\n\n");
588
+ buffer = events.pop() ?? "";
589
+ for (const event of events) {
590
+ if (!event.startsWith("data:")) continue;
591
+ const data = event.slice(event.startsWith("data: ") ? 6 : 5).trim();
592
+ if (data === "[DONE]") return;
593
+ if (!data) continue;
594
+ let json;
595
+ try {
596
+ json = JSON.parse(data);
597
+ } catch {
598
+ continue;
599
+ }
600
+ if (json && typeof json === "object") yield json;
601
+ }
602
+ }
603
+ } finally {
604
+ signal.removeEventListener("abort", onAbort);
605
+ try {
606
+ reader.releaseLock();
607
+ } catch {}
608
+ }
609
+ }
610
+ /**
611
+ * A {@link VoiceReplyGenerator} that runs the Mastra agent loop on a **remote** Mastra server over
612
+ * HTTP/SSE. Shaped exactly like the in-process `createAgentReplyGenerator`: it consumes the same
613
+ * chunk vocabulary, drives the same `toolFeedback` / `onToolCall` / `onTurnComplete` seams, and
614
+ * cancelling the returned stream (LiveKit does this on barge-in) tears down the HTTP request so the
615
+ * server aborts generation.
616
+ *
617
+ * Usable standalone via the worker's `generate:` hatch (a minimum-viable remote worker mode), and as
618
+ * the transport `MastraLLM` wraps. Errors are thrown as LiveKit `APIError` subclasses so the plugin's
619
+ * base-class retry loop and `FallbackAdapter` behave; a connect + first-token watchdog prevents
620
+ * indefinite dead air.
621
+ */
622
+ function createRemoteAgentReplyGenerator(options) {
623
+ const { baseUrl, agentId, apiPrefix = DEFAULT_API_PREFIX, headers, fetch: fetchImpl = globalThis.fetch, timeoutMs = DEFAULT_REMOTE_TIMEOUT_MS, retries = 2, body: extraBody, toolFeedback, onToolCall, onTurnComplete } = options;
624
+ if (!fetchImpl) throw new Error("@mastra/livekit: no fetch implementation available; pass `fetch` or run on Node ≥ 22.");
625
+ const url = `${trimTrailingSlash(baseUrl)}${apiPrefix}/agents/${agentId}/stream`;
626
+ return (ctx) => {
627
+ if (ctx.messages.length === 0) return null;
628
+ let currentAbortController;
629
+ let cancelled = false;
630
+ let replyText = "";
631
+ const toolCalls = [];
632
+ let usage;
633
+ const emitTurnComplete = (interrupted) => {
634
+ if (!onTurnComplete) return;
635
+ const completeCtx = {
636
+ ...ctx,
637
+ result: {
638
+ text: replyText,
639
+ toolCalls,
640
+ interrupted,
641
+ usage
642
+ }
643
+ };
644
+ Promise.resolve().then(() => onTurnComplete(completeCtx)).catch((error) => {
645
+ console.warn("@mastra/livekit: onTurnComplete hook threw", error);
646
+ });
647
+ };
648
+ const requestBody = {
649
+ messages: ctx.messages,
650
+ memory: ctx.memory ? {
651
+ thread: ctx.memory.thread,
652
+ resource: ctx.memory.resource ?? ctx.memory.thread,
653
+ ...ctx.memory.options ? { options: ctx.memory.options } : {}
654
+ } : void 0,
655
+ requestContext: serializeRequestContext(ctx.requestContext),
656
+ ...extraBody
657
+ };
658
+ return new stream_web.ReadableStream({
659
+ start: async (controller) => {
660
+ let retryable = true;
661
+ try {
662
+ for (let attempt = 0;; attempt++) {
663
+ const abortController = new AbortController();
664
+ currentAbortController = abortController;
665
+ let timedOut = false;
666
+ let watchdog;
667
+ const clearWatchdog = () => {
668
+ if (watchdog) {
669
+ clearTimeout(watchdog);
670
+ watchdog = void 0;
671
+ }
672
+ };
673
+ try {
674
+ watchdog = setTimeout(() => {
675
+ timedOut = true;
676
+ abortController.abort();
677
+ }, timeoutMs);
678
+ watchdog.unref?.();
679
+ const resolvedHeaders = await resolveHeaders(headers);
680
+ if (cancelled) {
681
+ clearWatchdog();
682
+ break;
683
+ }
684
+ const response = await fetchImpl(url, {
685
+ method: "POST",
686
+ headers: {
687
+ "content-type": "application/json",
688
+ accept: "text/event-stream",
689
+ ...resolvedHeaders
690
+ },
691
+ body: JSON.stringify(requestBody),
692
+ signal: abortController.signal
693
+ });
694
+ if (!response.ok) {
695
+ const errorBody = await safeReadBody(response);
696
+ throw new _livekit_agents.APIStatusError({
697
+ message: `@mastra/livekit: Mastra agent stream request failed with status ${response.status}`,
698
+ options: {
699
+ statusCode: response.status,
700
+ body: errorBody,
701
+ retryable
702
+ }
703
+ });
704
+ }
705
+ if (!response.body) throw new _livekit_agents.APIConnectionError({
706
+ message: "@mastra/livekit: Mastra agent stream returned an empty response body",
707
+ options: { retryable }
708
+ });
709
+ for await (const chunk of readMastraSSE(response.body, abortController.signal)) {
710
+ if (cancelled) break;
711
+ retryable = false;
712
+ const payload = chunk.payload ?? {};
713
+ switch (chunk.type) {
714
+ case "text-delta": {
715
+ const text = payload.text;
716
+ if (typeof text === "string" && text) {
717
+ clearWatchdog();
718
+ replyText += text;
719
+ controller.enqueue(text);
720
+ }
721
+ break;
722
+ }
723
+ case "text-end":
724
+ case "step-finish":
725
+ controller.enqueue(require_turn_metrics.VOICE_TEXT_FLUSH);
726
+ break;
727
+ case "tool-result":
728
+ if (typeof payload.toolCallId === "string") ctx.metrics?.toolEnd(payload.toolCallId);
729
+ break;
730
+ case "tool-call": {
731
+ clearWatchdog();
732
+ const toolCall = {
733
+ toolCallId: String(payload.toolCallId ?? ""),
734
+ toolName: String(payload.toolName ?? ""),
735
+ args: payload.args
736
+ };
737
+ toolCalls.push(toolCall);
738
+ ctx.metrics?.toolStart({
739
+ toolCallId: toolCall.toolCallId,
740
+ toolName: toolCall.toolName
741
+ });
742
+ controller.enqueue(require_turn_metrics.VOICE_TEXT_FLUSH);
743
+ try {
744
+ onToolCall?.(toolCall);
745
+ } catch (error) {
746
+ console.warn("@mastra/livekit: onToolCall hook threw", error);
747
+ }
748
+ if (toolFeedback) {
749
+ let filler;
750
+ try {
751
+ filler = toolFeedback(toolCall);
752
+ } catch (error) {
753
+ console.warn("@mastra/livekit: toolFeedback hook threw", error);
754
+ }
755
+ if (filler) {
756
+ controller.enqueue(filler.endsWith(" ") ? filler : `${filler} `);
757
+ controller.enqueue(require_turn_metrics.VOICE_TEXT_FLUSH);
758
+ }
759
+ }
760
+ break;
761
+ }
762
+ case "finish": {
763
+ clearWatchdog();
764
+ const output = payload.output;
765
+ const turnUsage = mapTurnUsage(output?.usage);
766
+ if (turnUsage) {
767
+ usage = turnUsage;
768
+ try {
769
+ ctx.onUsage?.(turnUsage);
770
+ } catch (error) {
771
+ console.warn("@mastra/livekit: onUsage hook threw", error);
772
+ }
773
+ }
774
+ break;
775
+ }
776
+ case "tool-call-approval":
777
+ case "tool-call-suspended": throw new Error(HITL_UNSUPPORTED_MESSAGE);
778
+ case "error": {
779
+ const error = payload.error;
780
+ throw error instanceof Error ? error : new Error(String(error));
781
+ }
782
+ default: break;
783
+ }
784
+ }
785
+ clearWatchdog();
786
+ break;
787
+ } catch (error) {
788
+ clearWatchdog();
789
+ if (cancelled) break;
790
+ let typed;
791
+ if (timedOut) typed = new _livekit_agents.APITimeoutError({ options: { retryable } });
792
+ else if (error instanceof _livekit_agents.APIError) typed = error;
793
+ else if (retryable) typed = new _livekit_agents.APIConnectionError({
794
+ message: toMessage(error),
795
+ options: { retryable }
796
+ });
797
+ else throw error;
798
+ if (typed.retryable && attempt < retries) continue;
799
+ throw typed;
800
+ }
801
+ }
802
+ if (!cancelled) controller.close();
803
+ emitTurnComplete(cancelled);
804
+ } catch (error) {
805
+ if (cancelled) {
806
+ emitTurnComplete(true);
807
+ return;
808
+ }
809
+ controller.error(error);
810
+ }
811
+ },
812
+ cancel: () => {
813
+ cancelled = true;
814
+ currentAbortController?.abort();
815
+ }
816
+ });
817
+ };
818
+ }
819
+ //#endregion
820
+ Object.defineProperty(exports, "MastraVoiceAgent", {
821
+ enumerable: true,
822
+ get: function() {
823
+ return MastraVoiceAgent;
824
+ }
825
+ });
826
+ Object.defineProperty(exports, "chatContextToMessages", {
827
+ enumerable: true,
828
+ get: function() {
829
+ return chatContextToMessages;
830
+ }
831
+ });
832
+ Object.defineProperty(exports, "createAgentReplyGenerator", {
833
+ enumerable: true,
834
+ get: function() {
835
+ return createAgentReplyGenerator;
836
+ }
837
+ });
838
+ Object.defineProperty(exports, "createMastraVoiceAgent", {
839
+ enumerable: true,
840
+ get: function() {
841
+ return createMastraVoiceAgent;
842
+ }
843
+ });
844
+ Object.defineProperty(exports, "createRemoteAgentReplyGenerator", {
845
+ enumerable: true,
846
+ get: function() {
847
+ return createRemoteAgentReplyGenerator;
848
+ }
849
+ });
850
+ Object.defineProperty(exports, "extractNewTurnMessages", {
851
+ enumerable: true,
852
+ get: function() {
853
+ return extractNewTurnMessages;
854
+ }
855
+ });
856
+ Object.defineProperty(exports, "instrumentVoiceReply", {
857
+ enumerable: true,
858
+ get: function() {
859
+ return instrumentVoiceReply;
860
+ }
861
+ });
862
+ Object.defineProperty(exports, "mastraLLMNode", {
863
+ enumerable: true,
864
+ get: function() {
865
+ return mastraLLMNode;
866
+ }
867
+ });
868
+ Object.defineProperty(exports, "observeVoiceSession", {
869
+ enumerable: true,
870
+ get: function() {
871
+ return observeVoiceSession;
872
+ }
873
+ });
874
+
875
+ //# sourceMappingURL=remote-GSOxa--f.cjs.map