@mastra/livekit 0.3.0 → 0.3.1-alpha.1

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.
@@ -1,182 +1,209 @@
1
- 'use strict';
2
-
3
- var chunkDBVKNDAQ_cjs = require('./chunk-DBVKNDAQ.cjs');
4
- var agents = require('@livekit/agents');
5
- var requestContext = require('@mastra/core/request-context');
6
-
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
+ const require_remote = require("./remote-BZ7eyB1q.cjs");
3
+ let _livekit_agents = require("@livekit/agents");
4
+ let _mastra_core_request_context = require("@mastra/core/request-context");
5
+ //#region src/llm-plugin.ts
7
6
  function toRequestContext(value) {
8
- if (!value) return void 0;
9
- if (value instanceof requestContext.RequestContext) return value;
10
- return new requestContext.RequestContext(Object.entries(value));
7
+ if (!value) return void 0;
8
+ if (value instanceof _mastra_core_request_context.RequestContext) return value;
9
+ return new _mastra_core_request_context.RequestContext(Object.entries(value));
11
10
  }
12
11
  function mapUsageToLiveKit(usage) {
13
- return {
14
- completionTokens: usage.completionTokens,
15
- promptTokens: usage.promptTokens,
16
- promptCachedTokens: usage.promptCachedTokens,
17
- totalTokens: usage.totalTokens
18
- };
12
+ return {
13
+ completionTokens: usage.completionTokens,
14
+ promptTokens: usage.promptTokens,
15
+ promptCachedTokens: usage.promptCachedTokens,
16
+ totalTokens: usage.totalTokens
17
+ };
19
18
  }
19
+ /**
20
+ * Tool names carried by a `toolCtx`, across @livekit/agents versions: 1.5+ always passes a
21
+ * `ToolContext` class instance (tools behind getters, `Object.keys` sees only private fields),
22
+ * while 1.4 and the object shorthand pass a plain name→tool map.
23
+ */
20
24
  function livekitToolNames(toolCtx) {
21
- const instance = toolCtx;
22
- if (typeof instance.flatten === "function") {
23
- return instance.flatten().map((tool) => {
24
- const { id, name } = tool;
25
- return id ?? name ?? "unknown";
26
- });
27
- }
28
- return Object.keys(toolCtx);
25
+ const instance = toolCtx;
26
+ if (typeof instance.flatten === "function") return instance.flatten().map((tool) => {
27
+ const { id, name } = tool;
28
+ return id ?? name ?? "unknown";
29
+ });
30
+ return Object.keys(toolCtx);
29
31
  }
30
- var MastraLLM = class extends agents.llm.LLM {
31
- #model;
32
- #memory;
33
- #requestContext;
34
- /** Non-remote sources are built once; remote is built per turn so it can pick up `connOptions.timeoutMs`. */
35
- #staticGenerator;
36
- #remoteOptions;
37
- #warnedToolCtx = false;
38
- constructor(options) {
39
- super();
40
- const sources = [
41
- options.remote ? "remote" : void 0,
42
- options.agent ? "agent" : void 0,
43
- options.generate ? "generate" : void 0
44
- ].filter(Boolean);
45
- if (sources.length !== 1) {
46
- throw new Error(
47
- `@mastra/livekit: MastraLLM requires exactly one reply source \u2014 \`remote\`, \`agent\`, or \`generate\` \u2014 but got ${sources.length === 0 ? "none" : sources.join(" + ")}.`
48
- );
49
- }
50
- this.#memory = options.memory ?? false;
51
- this.#requestContext = toRequestContext(options.requestContext);
52
- if (options.remote) {
53
- this.#model = options.remote.agentId;
54
- this.#remoteOptions = {
55
- ...options.remote,
56
- toolFeedback: options.toolFeedback,
57
- onToolCall: options.onToolCall,
58
- onTurnComplete: options.onTurnComplete
59
- };
60
- } else if (options.agent) {
61
- this.#model = options.agent.id ?? options.agent.name;
62
- this.#staticGenerator = chunkDBVKNDAQ_cjs.createAgentReplyGenerator({
63
- agent: options.agent,
64
- toolFeedback: options.toolFeedback,
65
- onToolCall: options.onToolCall,
66
- onTurnComplete: options.onTurnComplete
67
- });
68
- } else {
69
- this.#model = "mastra-generator";
70
- this.#staticGenerator = options.generate;
71
- }
72
- }
73
- label() {
74
- return "mastra.MastraLLM";
75
- }
76
- get model() {
77
- return this.#model;
78
- }
79
- get provider() {
80
- return "mastra";
81
- }
82
- /**
83
- * Resolves the reply generator for a turn. Non-remote sources are built once; the remote transport
84
- * is built per turn so its connect + first-token timeout can come from the session's
85
- * `connOptions.timeoutMs`, with base-class retries owning retry (transport `retries: 0`).
86
- */
87
- resolveGenerator(connOptions) {
88
- if (this.#staticGenerator) return this.#staticGenerator;
89
- const remote = this.#remoteOptions;
90
- return chunkDBVKNDAQ_cjs.createRemoteAgentReplyGenerator({
91
- ...remote,
92
- retries: 0,
93
- timeoutMs: remote.timeoutMs ?? connOptions.timeoutMs
94
- });
95
- }
96
- chat({
97
- chatCtx,
98
- toolCtx,
99
- connOptions = agents.DEFAULT_API_CONNECT_OPTIONS
100
- }) {
101
- if (!this.#warnedToolCtx && toolCtx) {
102
- const ignored = livekitToolNames(toolCtx);
103
- if (ignored.length > 0) {
104
- this.#warnedToolCtx = true;
105
- console.warn(
106
- `@mastra/livekit: MastraLLM ignores LiveKit-side tools (${ignored.join(", ")}). Tools are defined and executed server-side on the Mastra agent \u2014 move them there.`
107
- );
108
- }
109
- }
110
- return new MastraLLMStream(this, {
111
- chatCtx,
112
- toolCtx,
113
- connOptions,
114
- generator: this.resolveGenerator(connOptions),
115
- memory: this.#memory,
116
- requestContext: this.#requestContext
117
- });
118
- }
119
- /** No-op in v1: nothing in the LiveKit session/worker ever calls `prewarm()` automatically. */
120
- prewarm() {
121
- }
32
+ /**
33
+ * A standard LiveKit LLM plugin (`llm.LLM`) backed by a Mastra agent. Drop it into the `llm` slot of a
34
+ * customer-owned `voice.AgentSession` and the Mastra app (agent loop, tools, memory, observability)
35
+ * runs wherever it's deployed — most importantly on a **remote** Mastra server reached over HTTP.
36
+ *
37
+ * Tools are defined and executed **server-side** on the Mastra agent; LiveKit-side `toolCtx` is
38
+ * ignored (with a one-time warning). Tool activity surfaces via `toolFeedback` (spoken) and
39
+ * `onToolCall` / `onTurnComplete` (programmatic). `voice.Agent` instructions do **not** reach the
40
+ * Mastra agent — put instructions on the Mastra agent instead.
41
+ *
42
+ * @example
43
+ * ```ts
44
+ * const session = new voice.AgentSession({
45
+ * stt: 'deepgram/nova-3',
46
+ * tts: 'cartesia/sonic-3',
47
+ * llm: new MastraLLM({
48
+ * remote: { baseUrl: process.env.MASTRA_URL!, agentId: 'callCenter' },
49
+ * memory: { thread: callId, resource: callerId },
50
+ * }),
51
+ * });
52
+ * ```
53
+ */
54
+ var MastraLLM = class extends _livekit_agents.llm.LLM {
55
+ #model;
56
+ #memory;
57
+ #requestContext;
58
+ /** Non-remote sources are built once; remote is built per turn so it can pick up `connOptions.timeoutMs`. */
59
+ #staticGenerator;
60
+ #remoteOptions;
61
+ #warnedToolCtx = false;
62
+ constructor(options) {
63
+ super();
64
+ const sources = [
65
+ options.remote ? "remote" : void 0,
66
+ options.agent ? "agent" : void 0,
67
+ options.generate ? "generate" : void 0
68
+ ].filter(Boolean);
69
+ if (sources.length !== 1) throw new Error(`@mastra/livekit: MastraLLM requires exactly one reply source — \`remote\`, \`agent\`, or \`generate\` — but got ${sources.length === 0 ? "none" : sources.join(" + ")}.`);
70
+ this.#memory = options.memory ?? false;
71
+ this.#requestContext = toRequestContext(options.requestContext);
72
+ if (options.remote) {
73
+ this.#model = options.remote.agentId;
74
+ this.#remoteOptions = {
75
+ ...options.remote,
76
+ toolFeedback: options.toolFeedback,
77
+ onToolCall: options.onToolCall,
78
+ onTurnComplete: options.onTurnComplete
79
+ };
80
+ } else if (options.agent) {
81
+ this.#model = options.agent.id ?? options.agent.name;
82
+ this.#staticGenerator = require_remote.createAgentReplyGenerator({
83
+ agent: options.agent,
84
+ toolFeedback: options.toolFeedback,
85
+ onToolCall: options.onToolCall,
86
+ onTurnComplete: options.onTurnComplete
87
+ });
88
+ } else {
89
+ this.#model = "mastra-generator";
90
+ this.#staticGenerator = options.generate;
91
+ }
92
+ }
93
+ label() {
94
+ return "mastra.MastraLLM";
95
+ }
96
+ get model() {
97
+ return this.#model;
98
+ }
99
+ get provider() {
100
+ return "mastra";
101
+ }
102
+ /**
103
+ * Resolves the reply generator for a turn. Non-remote sources are built once; the remote transport
104
+ * is built per turn so its connect + first-token timeout can come from the session's
105
+ * `connOptions.timeoutMs`, with base-class retries owning retry (transport `retries: 0`).
106
+ */
107
+ resolveGenerator(connOptions) {
108
+ if (this.#staticGenerator) return this.#staticGenerator;
109
+ const remote = this.#remoteOptions;
110
+ return require_remote.createRemoteAgentReplyGenerator({
111
+ ...remote,
112
+ retries: 0,
113
+ timeoutMs: remote.timeoutMs ?? connOptions.timeoutMs
114
+ });
115
+ }
116
+ chat({ chatCtx, toolCtx, connOptions = _livekit_agents.DEFAULT_API_CONNECT_OPTIONS }) {
117
+ if (!this.#warnedToolCtx && toolCtx) {
118
+ const ignored = livekitToolNames(toolCtx);
119
+ if (ignored.length > 0) {
120
+ this.#warnedToolCtx = true;
121
+ console.warn(`@mastra/livekit: MastraLLM ignores LiveKit-side tools (${ignored.join(", ")}). Tools are defined and executed server-side on the Mastra agent — move them there.`);
122
+ }
123
+ }
124
+ return new MastraLLMStream(this, {
125
+ chatCtx,
126
+ toolCtx,
127
+ connOptions,
128
+ generator: this.resolveGenerator(connOptions),
129
+ memory: this.#memory,
130
+ requestContext: this.#requestContext
131
+ });
132
+ }
133
+ /** No-op in v1: nothing in the LiveKit session/worker ever calls `prewarm()` automatically. */
134
+ prewarm() {}
122
135
  };
123
- var MastraLLMStream = class extends agents.llm.LLMStream {
124
- #generator;
125
- #memory;
126
- #requestContext;
127
- constructor(mastraLLM, options) {
128
- super(mastraLLM, { chatCtx: options.chatCtx, toolCtx: options.toolCtx, connOptions: options.connOptions });
129
- this.#generator = options.generator;
130
- this.#memory = options.memory;
131
- this.#requestContext = options.requestContext;
132
- }
133
- async run() {
134
- const messages = this.#memory === false ? chunkDBVKNDAQ_cjs.chatContextToMessages(this.chatCtx) : chunkDBVKNDAQ_cjs.extractNewTurnMessages(this.chatCtx);
135
- if (messages.length === 0) return;
136
- let usage;
137
- const turnCtx = {
138
- messages,
139
- chatCtx: this.chatCtx,
140
- memory: this.#memory,
141
- requestContext: this.#requestContext,
142
- onUsage: (turnUsage) => {
143
- usage = turnUsage;
144
- }
145
- };
146
- const reply = await this.#generator(turnCtx);
147
- if (!reply) return;
148
- if (this.abortController.signal.aborted) {
149
- await reply.cancel().catch(() => {
150
- });
151
- return;
152
- }
153
- const id = globalThis.crypto.randomUUID();
154
- const reader = reply.getReader();
155
- const onAbort = () => void reader.cancel().catch(() => {
156
- });
157
- this.abortController.signal.addEventListener("abort", onAbort, { once: true });
158
- try {
159
- for (; ; ) {
160
- const { done, value } = await reader.read();
161
- if (done) break;
162
- if (this.abortController.signal.aborted) break;
163
- if (value) this.queue.put({ id, delta: { role: "assistant", content: value } });
164
- }
165
- } catch (error) {
166
- if (this.abortController.signal.aborted) return;
167
- throw error;
168
- } finally {
169
- this.abortController.signal.removeEventListener("abort", onAbort);
170
- }
171
- if (this.abortController.signal.aborted) return;
172
- if (usage) this.queue.put({ id, usage: mapUsageToLiveKit(usage) });
173
- }
136
+ /**
137
+ * The `llm.LLMStream` `MastraLLM` returns per turn. `run()` extracts the turn's messages, drives
138
+ * the reply generator, and pushes assistant `ChatChunk`s into `this.queue` (NOT `this.output` — the
139
+ * base class drains queue → output and computes TTFT / duration / usage). Barge-in aborts via
140
+ * `this.abortController` and `run()` returns silently.
141
+ */
142
+ var MastraLLMStream = class extends _livekit_agents.llm.LLMStream {
143
+ #generator;
144
+ #memory;
145
+ #requestContext;
146
+ constructor(mastraLLM, options) {
147
+ super(mastraLLM, {
148
+ chatCtx: options.chatCtx,
149
+ toolCtx: options.toolCtx,
150
+ connOptions: options.connOptions
151
+ });
152
+ this.#generator = options.generator;
153
+ this.#memory = options.memory;
154
+ this.#requestContext = options.requestContext;
155
+ }
156
+ async run() {
157
+ const messages = this.#memory === false ? require_remote.chatContextToMessages(this.chatCtx) : require_remote.extractNewTurnMessages(this.chatCtx);
158
+ if (messages.length === 0) return;
159
+ let usage;
160
+ const turnCtx = {
161
+ messages,
162
+ chatCtx: this.chatCtx,
163
+ memory: this.#memory,
164
+ requestContext: this.#requestContext,
165
+ onUsage: (turnUsage) => {
166
+ usage = turnUsage;
167
+ }
168
+ };
169
+ const reply = await this.#generator(turnCtx);
170
+ if (!reply) return;
171
+ if (this.abortController.signal.aborted) {
172
+ await reply.cancel().catch(() => {});
173
+ return;
174
+ }
175
+ const id = globalThis.crypto.randomUUID();
176
+ const reader = reply.getReader();
177
+ const onAbort = () => void reader.cancel().catch(() => {});
178
+ this.abortController.signal.addEventListener("abort", onAbort, { once: true });
179
+ try {
180
+ for (;;) {
181
+ const { done, value } = await reader.read();
182
+ if (done) break;
183
+ if (this.abortController.signal.aborted) break;
184
+ if (value) this.queue.put({
185
+ id,
186
+ delta: {
187
+ role: "assistant",
188
+ content: value
189
+ }
190
+ });
191
+ }
192
+ } catch (error) {
193
+ if (this.abortController.signal.aborted) return;
194
+ throw error;
195
+ } finally {
196
+ this.abortController.signal.removeEventListener("abort", onAbort);
197
+ }
198
+ if (this.abortController.signal.aborted) return;
199
+ if (usage) this.queue.put({
200
+ id,
201
+ usage: mapUsageToLiveKit(usage)
202
+ });
203
+ }
174
204
  };
175
-
176
- Object.defineProperty(exports, "createRemoteAgentReplyGenerator", {
177
- enumerable: true,
178
- get: function () { return chunkDBVKNDAQ_cjs.createRemoteAgentReplyGenerator; }
179
- });
205
+ //#endregion
180
206
  exports.MastraLLM = MastraLLM;
181
- //# sourceMappingURL=plugin-entry.cjs.map
207
+ exports.createRemoteAgentReplyGenerator = require_remote.createRemoteAgentReplyGenerator;
208
+
182
209
  //# sourceMappingURL=plugin-entry.cjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/llm-plugin.ts"],"names":["RequestContext","llm","createAgentReplyGenerator","createRemoteAgentReplyGenerator","DEFAULT_API_CONNECT_OPTIONS","chatContextToMessages","extractNewTurnMessages"],"mappings":";;;;;;AA6DA,SAAS,iBAAiB,KAAA,EAAyF;AACjH,EAAA,IAAI,CAAC,OAAO,OAAO,MAAA;AACnB,EAAA,IAAI,KAAA,YAAiBA,+BAAgB,OAAO,KAAA;AAC5C,EAAA,OAAO,IAAIA,6BAAA,CAAwB,MAAA,CAAO,OAAA,CAAQ,KAAK,CAAC,CAAA;AAC1D;AAEA,SAAS,kBAAkB,KAAA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KAAK,OAAO,MAAA,EAAO,CAAE,MAAM,MAAM;AAAA,IAAC,CAAC,CAAA;AACzD,IAAA,IAAA,CAAK,eAAA,CAAgB,OAAO,gBAAA,CAAiB,OAAA,EAAS,SAAS,EAAE,IAAA,EAAM,MAAM,CAAA;AAC7E,IAAA,IAAI;AACF,MAAA,WAAS;AACP,QAAA,MAAM,EAAE,IAAA,EAAM,KAAA,EAAM,GAAI,MAAM,OAAO,IAAA,EAAK;AAC1C,QAAA,IAAI,IAAA,EAAM;AACV,QAAA,IAAI,IAAA,CAAK,eAAA,CAAgB,MAAA,CAAO,OAAA,EAAS;AACzC,QAAA,IAAI,KAAA,EAAO,IAAA,CAAK,KAAA,CAAM,GAAA,CAAI,EAAE,EAAA,EAAI,KAAA,EAAO,EAAE,IAAA,EAAM,WAAA,EAAa,OAAA,EAAS,KAAA,IAAS,CAAA;AAAA,MAChF;AAAA,IACF,SAAS,KAAA,EAAO;AAEd,MAAA,IAAI,IAAA,CAAK,eAAA,CAAgB,MAAA,CAAO,OAAA,EAAS;AACzC,MAAA,MAAM,KAAA;AAAA,IACR,CAAA,SAAE;AACA,MAAA,IAAA,CAAK,eAAA,CAAgB,MAAA,CAAO,mBAAA,CAAoB,OAAA,EAAS,OAAO,CAAA;AAAA,IAClE;AAEA,IAAA,IAAI,IAAA,CAAK,eAAA,CAAgB,MAAA,CAAO,OAAA,EAAS;AAEzC,IAAA,IAAI,KAAA,EAAO,IAAA,CAAK,KAAA,CAAM,GAAA,CAAI,EAAE,IAAI,KAAA,EAAO,iBAAA,CAAkB,KAAK,CAAA,EAAG,CAAA;AAAA,EACnE;AACF,CAAA","file":"plugin-entry.cjs","sourcesContent":["import { DEFAULT_API_CONNECT_OPTIONS, llm } from '@livekit/agents';\nimport type { APIConnectOptions } from '@livekit/agents';\nimport type { Agent as MastraAgent } from '@mastra/core/agent';\nimport { RequestContext } from '@mastra/core/request-context';\nimport { createAgentReplyGenerator } from './bridge';\nimport type {\n MastraVoiceAgentMemory,\n VoiceReplyGenerator,\n VoiceToolCall,\n VoiceTurnCompleteHook,\n VoiceTurnContext,\n VoiceTurnUsage,\n} from './bridge';\nimport { chatContextToMessages, extractNewTurnMessages } from './messages';\nimport { createRemoteAgentReplyGenerator } from './remote';\nimport type { RemoteMastraAgentOptions } from './remote';\n\nexport type { RemoteMastraAgentOptions } from './remote';\n\n/**\n * Options for {@link MastraLLM}. Provide **exactly one** reply source — `remote` (the headline: a\n * Mastra app on a remote server), `agent` (an in-process Mastra agent), or `generate` (a custom\n * {@link VoiceReplyGenerator}). The `toolFeedback` / `onToolCall` / `onTurnComplete` hooks apply to\n * the `remote` and `agent` sources; a `generate` source owns its own hooks.\n */\nexport interface MastraLLMOptions {\n /** Remote Mastra server. Provide exactly one of `remote`, `agent`, `generate`. */\n remote?: RemoteMastraAgentOptions;\n /** In-process Mastra agent (reuses `createAgentReplyGenerator`). */\n agent?: MastraAgent;\n /** Custom reply source (escape hatch; owns its own tool-feedback / turn-complete behavior). */\n generate?: VoiceReplyGenerator;\n\n /**\n * Conversation persistence, resolved by the customer per call (from SIP/caller identity). When set,\n * only messages new since the agent last spoke are sent each turn and Mastra Memory supplies\n * history. When omitted/false, the full LiveKit chat context is sent every turn.\n *\n * NOTE: incompatible with the session's `preemptiveGeneration` option — a speculative turn that\n * completes before being discarded pollutes the thread. Leave preemptive generation off when\n * using `memory`.\n *\n * The plugin cannot detect the combination at runtime, so this stays a documented constraint\n * rather than a warning: the LLM interface never receives the session (so the option can't be\n * read), a preemptive `chat()` is shape-identical to a real turn (LiveKit drives the same\n * `generateReply` with a draft transcript and no marker), and the observable signature — a\n * cancelled stream followed by a `chat()` whose trailing user message changed — is exactly what\n * an ordinary barge-in correction looks like, so a heuristic would warn on every barge-in.\n */\n memory?: MastraVoiceAgentMemory | false;\n /** Request context forwarded to generation (tenant, dialed number, ...). */\n requestContext?: RequestContext | Record<string, unknown>;\n\n /** Speak a short filler while a (server-side) tool runs. Applies to the `remote`/`agent` sources. */\n toolFeedback?: (toolCall: VoiceToolCall) => string | undefined | void;\n /** Notified as each tool-call chunk arrives, mid-stream. Applies to the `remote`/`agent` sources. */\n onToolCall?: (toolCall: VoiceToolCall) => void;\n /** Fired off the audio path after each reply finishes. Applies to the `remote`/`agent` sources. */\n onTurnComplete?: VoiceTurnCompleteHook;\n}\n\nfunction toRequestContext(value: RequestContext | Record<string, unknown> | undefined): RequestContext | undefined {\n if (!value) return undefined;\n if (value instanceof RequestContext) return value;\n return new RequestContext<unknown>(Object.entries(value));\n}\n\nfunction mapUsageToLiveKit(usage: VoiceTurnUsage): llm.CompletionUsage {\n return {\n completionTokens: usage.completionTokens,\n promptTokens: usage.promptTokens,\n promptCachedTokens: usage.promptCachedTokens,\n totalTokens: usage.totalTokens,\n };\n}\n\n/**\n * Tool names carried by a `toolCtx`, across @livekit/agents versions: 1.5+ always passes a\n * `ToolContext` class instance (tools behind getters, `Object.keys` sees only private fields),\n * while 1.4 and the object shorthand pass a plain name→tool map.\n */\nfunction livekitToolNames(toolCtx: object): string[] {\n const instance = toolCtx as { flatten?: unknown };\n if (typeof instance.flatten === 'function') {\n return (instance.flatten as () => object[])().map(tool => {\n const { id, name } = tool as { id?: string; name?: string };\n return id ?? name ?? 'unknown';\n });\n }\n return Object.keys(toolCtx);\n}\n\n/**\n * A standard LiveKit LLM plugin (`llm.LLM`) backed by a Mastra agent. Drop it into the `llm` slot of a\n * customer-owned `voice.AgentSession` and the Mastra app (agent loop, tools, memory, observability)\n * runs wherever it's deployed — most importantly on a **remote** Mastra server reached over HTTP.\n *\n * Tools are defined and executed **server-side** on the Mastra agent; LiveKit-side `toolCtx` is\n * ignored (with a one-time warning). Tool activity surfaces via `toolFeedback` (spoken) and\n * `onToolCall` / `onTurnComplete` (programmatic). `voice.Agent` instructions do **not** reach the\n * Mastra agent — put instructions on the Mastra agent instead.\n *\n * @example\n * ```ts\n * const session = new voice.AgentSession({\n * stt: 'deepgram/nova-3',\n * tts: 'cartesia/sonic-3',\n * llm: new MastraLLM({\n * remote: { baseUrl: process.env.MASTRA_URL!, agentId: 'callCenter' },\n * memory: { thread: callId, resource: callerId },\n * }),\n * });\n * ```\n */\nexport class MastraLLM extends llm.LLM {\n readonly #model: string;\n readonly #memory: MastraVoiceAgentMemory | false;\n readonly #requestContext?: RequestContext;\n /** Non-remote sources are built once; remote is built per turn so it can pick up `connOptions.timeoutMs`. */\n readonly #staticGenerator?: VoiceReplyGenerator;\n readonly #remoteOptions?: RemoteMastraAgentOptions & {\n toolFeedback?: MastraLLMOptions['toolFeedback'];\n onToolCall?: MastraLLMOptions['onToolCall'];\n onTurnComplete?: VoiceTurnCompleteHook;\n };\n #warnedToolCtx = false;\n\n constructor(options: MastraLLMOptions) {\n super();\n const sources = [\n options.remote ? 'remote' : undefined,\n options.agent ? 'agent' : undefined,\n options.generate ? 'generate' : undefined,\n ].filter(Boolean) as string[];\n if (sources.length !== 1) {\n throw new Error(\n `@mastra/livekit: MastraLLM requires exactly one reply source — \\`remote\\`, \\`agent\\`, or \\`generate\\` — ` +\n `but got ${sources.length === 0 ? 'none' : sources.join(' + ')}.`,\n );\n }\n\n this.#memory = options.memory ?? false;\n this.#requestContext = toRequestContext(options.requestContext);\n\n if (options.remote) {\n this.#model = options.remote.agentId;\n this.#remoteOptions = {\n ...options.remote,\n toolFeedback: options.toolFeedback,\n onToolCall: options.onToolCall,\n onTurnComplete: options.onTurnComplete,\n };\n } else if (options.agent) {\n this.#model = options.agent.id ?? options.agent.name;\n this.#staticGenerator = createAgentReplyGenerator({\n agent: options.agent,\n toolFeedback: options.toolFeedback,\n onToolCall: options.onToolCall,\n onTurnComplete: options.onTurnComplete,\n });\n } else {\n this.#model = 'mastra-generator';\n this.#staticGenerator = options.generate;\n }\n }\n\n label(): string {\n return 'mastra.MastraLLM';\n }\n\n override get model(): string {\n return this.#model;\n }\n\n override get provider(): string {\n return 'mastra';\n }\n\n /**\n * Resolves the reply generator for a turn. Non-remote sources are built once; the remote transport\n * is built per turn so its connect + first-token timeout can come from the session's\n * `connOptions.timeoutMs`, with base-class retries owning retry (transport `retries: 0`).\n */\n private resolveGenerator(connOptions: APIConnectOptions): VoiceReplyGenerator {\n if (this.#staticGenerator) return this.#staticGenerator;\n const remote = this.#remoteOptions!;\n return createRemoteAgentReplyGenerator({\n ...remote,\n retries: 0,\n timeoutMs: remote.timeoutMs ?? connOptions.timeoutMs,\n });\n }\n\n override chat({\n chatCtx,\n toolCtx,\n connOptions = DEFAULT_API_CONNECT_OPTIONS,\n }: {\n chatCtx: llm.ChatContext;\n toolCtx?: llm.ToolContext;\n connOptions?: APIConnectOptions;\n parallelToolCalls?: boolean;\n toolChoice?: llm.ToolChoice;\n extraKwargs?: Record<string, unknown>;\n }): llm.LLMStream {\n // Tools run server-side; warn once if the customer wired LiveKit-side tools.\n if (!this.#warnedToolCtx && toolCtx) {\n const ignored = livekitToolNames(toolCtx);\n if (ignored.length > 0) {\n this.#warnedToolCtx = true;\n console.warn(\n `@mastra/livekit: MastraLLM ignores LiveKit-side tools (${ignored.join(', ')}). ` +\n `Tools are defined and executed server-side on the Mastra agent — move them there.`,\n );\n }\n }\n return new MastraLLMStream(this, {\n chatCtx,\n toolCtx,\n connOptions,\n generator: this.resolveGenerator(connOptions),\n memory: this.#memory,\n requestContext: this.#requestContext,\n });\n }\n\n /** No-op in v1: nothing in the LiveKit session/worker ever calls `prewarm()` automatically. */\n override prewarm(): void {}\n}\n\ninterface MastraLLMStreamOptions {\n chatCtx: llm.ChatContext;\n toolCtx?: llm.ToolContext;\n connOptions: APIConnectOptions;\n generator: VoiceReplyGenerator;\n memory: MastraVoiceAgentMemory | false;\n requestContext?: RequestContext;\n}\n\n/**\n * The `llm.LLMStream` `MastraLLM` returns per turn. `run()` extracts the turn's messages, drives\n * the reply generator, and pushes assistant `ChatChunk`s into `this.queue` (NOT `this.output` — the\n * base class drains queue → output and computes TTFT / duration / usage). Barge-in aborts via\n * `this.abortController` and `run()` returns silently.\n */\nclass MastraLLMStream extends llm.LLMStream {\n readonly #generator: VoiceReplyGenerator;\n readonly #memory: MastraVoiceAgentMemory | false;\n readonly #requestContext?: RequestContext;\n\n constructor(mastraLLM: MastraLLM, options: MastraLLMStreamOptions) {\n super(mastraLLM, { chatCtx: options.chatCtx, toolCtx: options.toolCtx, connOptions: options.connOptions });\n this.#generator = options.generator;\n this.#memory = options.memory;\n this.#requestContext = options.requestContext;\n }\n\n protected async run(): Promise<void> {\n const messages =\n this.#memory === false ? chatContextToMessages(this.chatCtx) : extractNewTurnMessages(this.chatCtx);\n // No new input to answer → close without a request (equivalent to the wrapper returning null).\n if (messages.length === 0) return;\n\n let usage: VoiceTurnUsage | undefined;\n const turnCtx: VoiceTurnContext = {\n messages,\n chatCtx: this.chatCtx,\n memory: this.#memory,\n requestContext: this.#requestContext,\n onUsage: turnUsage => {\n usage = turnUsage;\n },\n };\n\n const reply = await this.#generator(turnCtx);\n if (!reply) return;\n if (this.abortController.signal.aborted) {\n await reply.cancel().catch(() => {});\n return;\n }\n\n // A single provider response id ties all of this turn's chunks together for the base class metrics.\n const id = globalThis.crypto.randomUUID();\n const reader = reply.getReader();\n const onAbort = () => void reader.cancel().catch(() => {});\n this.abortController.signal.addEventListener('abort', onAbort, { once: true });\n try {\n for (;;) {\n const { done, value } = await reader.read();\n if (done) break;\n if (this.abortController.signal.aborted) break;\n if (value) this.queue.put({ id, delta: { role: 'assistant', content: value } });\n }\n } catch (error) {\n // Barge-in tears down the reply stream; that surfaces as a read rejection but is not a failure.\n if (this.abortController.signal.aborted) return;\n throw error;\n } finally {\n this.abortController.signal.removeEventListener('abort', onAbort);\n }\n // Return silently on barge-in — throwing would feed the base class's error/retry machinery.\n if (this.abortController.signal.aborted) return;\n // Final usage-only chunk → the base class reads usage from the last chunk that carries it.\n if (usage) this.queue.put({ id, usage: mapUsageToLiveKit(usage) });\n }\n}\n\nexport { MastraLLMStream };\n"]}
1
+ {"version":3,"file":"plugin-entry.cjs","names":["RequestContext","llm","#model","#memory","#requestContext","#staticGenerator","#remoteOptions","createAgentReplyGenerator","createRemoteAgentReplyGenerator","DEFAULT_API_CONNECT_OPTIONS","#warnedToolCtx","#generator","chatContextToMessages","extractNewTurnMessages"],"sources":["../src/llm-plugin.ts"],"sourcesContent":["import { DEFAULT_API_CONNECT_OPTIONS, llm } from '@livekit/agents';\nimport type { APIConnectOptions } from '@livekit/agents';\nimport type { Agent as MastraAgent } from '@mastra/core/agent';\nimport { RequestContext } from '@mastra/core/request-context';\nimport { createAgentReplyGenerator } from './bridge';\nimport type {\n MastraVoiceAgentMemory,\n VoiceReplyGenerator,\n VoiceToolCall,\n VoiceTurnCompleteHook,\n VoiceTurnContext,\n VoiceTurnUsage,\n} from './bridge';\nimport { chatContextToMessages, extractNewTurnMessages } from './messages';\nimport { createRemoteAgentReplyGenerator } from './remote';\nimport type { RemoteMastraAgentOptions } from './remote';\n\nexport type { RemoteMastraAgentOptions } from './remote';\n\n/**\n * Options for {@link MastraLLM}. Provide **exactly one** reply source — `remote` (the headline: a\n * Mastra app on a remote server), `agent` (an in-process Mastra agent), or `generate` (a custom\n * {@link VoiceReplyGenerator}). The `toolFeedback` / `onToolCall` / `onTurnComplete` hooks apply to\n * the `remote` and `agent` sources; a `generate` source owns its own hooks.\n */\nexport interface MastraLLMOptions {\n /** Remote Mastra server. Provide exactly one of `remote`, `agent`, `generate`. */\n remote?: RemoteMastraAgentOptions;\n /** In-process Mastra agent (reuses `createAgentReplyGenerator`). */\n agent?: MastraAgent;\n /** Custom reply source (escape hatch; owns its own tool-feedback / turn-complete behavior). */\n generate?: VoiceReplyGenerator;\n\n /**\n * Conversation persistence, resolved by the customer per call (from SIP/caller identity). When set,\n * only messages new since the agent last spoke are sent each turn and Mastra Memory supplies\n * history. When omitted/false, the full LiveKit chat context is sent every turn.\n *\n * NOTE: incompatible with the session's `preemptiveGeneration` option — a speculative turn that\n * completes before being discarded pollutes the thread. Leave preemptive generation off when\n * using `memory`.\n *\n * The plugin cannot detect the combination at runtime, so this stays a documented constraint\n * rather than a warning: the LLM interface never receives the session (so the option can't be\n * read), a preemptive `chat()` is shape-identical to a real turn (LiveKit drives the same\n * `generateReply` with a draft transcript and no marker), and the observable signature — a\n * cancelled stream followed by a `chat()` whose trailing user message changed — is exactly what\n * an ordinary barge-in correction looks like, so a heuristic would warn on every barge-in.\n */\n memory?: MastraVoiceAgentMemory | false;\n /** Request context forwarded to generation (tenant, dialed number, ...). */\n requestContext?: RequestContext | Record<string, unknown>;\n\n /** Speak a short filler while a (server-side) tool runs. Applies to the `remote`/`agent` sources. */\n toolFeedback?: (toolCall: VoiceToolCall) => string | undefined | void;\n /** Notified as each tool-call chunk arrives, mid-stream. Applies to the `remote`/`agent` sources. */\n onToolCall?: (toolCall: VoiceToolCall) => void;\n /** Fired off the audio path after each reply finishes. Applies to the `remote`/`agent` sources. */\n onTurnComplete?: VoiceTurnCompleteHook;\n}\n\nfunction toRequestContext(value: RequestContext | Record<string, unknown> | undefined): RequestContext | undefined {\n if (!value) return undefined;\n if (value instanceof RequestContext) return value;\n return new RequestContext<unknown>(Object.entries(value));\n}\n\nfunction mapUsageToLiveKit(usage: VoiceTurnUsage): llm.CompletionUsage {\n return {\n completionTokens: usage.completionTokens,\n promptTokens: usage.promptTokens,\n promptCachedTokens: usage.promptCachedTokens,\n totalTokens: usage.totalTokens,\n };\n}\n\n/**\n * Tool names carried by a `toolCtx`, across @livekit/agents versions: 1.5+ always passes a\n * `ToolContext` class instance (tools behind getters, `Object.keys` sees only private fields),\n * while 1.4 and the object shorthand pass a plain name→tool map.\n */\nfunction livekitToolNames(toolCtx: object): string[] {\n const instance = toolCtx as { flatten?: unknown };\n if (typeof instance.flatten === 'function') {\n return (instance.flatten as () => object[])().map(tool => {\n const { id, name } = tool as { id?: string; name?: string };\n return id ?? name ?? 'unknown';\n });\n }\n return Object.keys(toolCtx);\n}\n\n/**\n * A standard LiveKit LLM plugin (`llm.LLM`) backed by a Mastra agent. Drop it into the `llm` slot of a\n * customer-owned `voice.AgentSession` and the Mastra app (agent loop, tools, memory, observability)\n * runs wherever it's deployed — most importantly on a **remote** Mastra server reached over HTTP.\n *\n * Tools are defined and executed **server-side** on the Mastra agent; LiveKit-side `toolCtx` is\n * ignored (with a one-time warning). Tool activity surfaces via `toolFeedback` (spoken) and\n * `onToolCall` / `onTurnComplete` (programmatic). `voice.Agent` instructions do **not** reach the\n * Mastra agent — put instructions on the Mastra agent instead.\n *\n * @example\n * ```ts\n * const session = new voice.AgentSession({\n * stt: 'deepgram/nova-3',\n * tts: 'cartesia/sonic-3',\n * llm: new MastraLLM({\n * remote: { baseUrl: process.env.MASTRA_URL!, agentId: 'callCenter' },\n * memory: { thread: callId, resource: callerId },\n * }),\n * });\n * ```\n */\nexport class MastraLLM extends llm.LLM {\n readonly #model: string;\n readonly #memory: MastraVoiceAgentMemory | false;\n readonly #requestContext?: RequestContext;\n /** Non-remote sources are built once; remote is built per turn so it can pick up `connOptions.timeoutMs`. */\n readonly #staticGenerator?: VoiceReplyGenerator;\n readonly #remoteOptions?: RemoteMastraAgentOptions & {\n toolFeedback?: MastraLLMOptions['toolFeedback'];\n onToolCall?: MastraLLMOptions['onToolCall'];\n onTurnComplete?: VoiceTurnCompleteHook;\n };\n #warnedToolCtx = false;\n\n constructor(options: MastraLLMOptions) {\n super();\n const sources = [\n options.remote ? 'remote' : undefined,\n options.agent ? 'agent' : undefined,\n options.generate ? 'generate' : undefined,\n ].filter(Boolean) as string[];\n if (sources.length !== 1) {\n throw new Error(\n `@mastra/livekit: MastraLLM requires exactly one reply source — \\`remote\\`, \\`agent\\`, or \\`generate\\` — ` +\n `but got ${sources.length === 0 ? 'none' : sources.join(' + ')}.`,\n );\n }\n\n this.#memory = options.memory ?? false;\n this.#requestContext = toRequestContext(options.requestContext);\n\n if (options.remote) {\n this.#model = options.remote.agentId;\n this.#remoteOptions = {\n ...options.remote,\n toolFeedback: options.toolFeedback,\n onToolCall: options.onToolCall,\n onTurnComplete: options.onTurnComplete,\n };\n } else if (options.agent) {\n this.#model = options.agent.id ?? options.agent.name;\n this.#staticGenerator = createAgentReplyGenerator({\n agent: options.agent,\n toolFeedback: options.toolFeedback,\n onToolCall: options.onToolCall,\n onTurnComplete: options.onTurnComplete,\n });\n } else {\n this.#model = 'mastra-generator';\n this.#staticGenerator = options.generate;\n }\n }\n\n label(): string {\n return 'mastra.MastraLLM';\n }\n\n override get model(): string {\n return this.#model;\n }\n\n override get provider(): string {\n return 'mastra';\n }\n\n /**\n * Resolves the reply generator for a turn. Non-remote sources are built once; the remote transport\n * is built per turn so its connect + first-token timeout can come from the session's\n * `connOptions.timeoutMs`, with base-class retries owning retry (transport `retries: 0`).\n */\n private resolveGenerator(connOptions: APIConnectOptions): VoiceReplyGenerator {\n if (this.#staticGenerator) return this.#staticGenerator;\n const remote = this.#remoteOptions!;\n return createRemoteAgentReplyGenerator({\n ...remote,\n retries: 0,\n timeoutMs: remote.timeoutMs ?? connOptions.timeoutMs,\n });\n }\n\n override chat({\n chatCtx,\n toolCtx,\n connOptions = DEFAULT_API_CONNECT_OPTIONS,\n }: {\n chatCtx: llm.ChatContext;\n toolCtx?: llm.ToolContext;\n connOptions?: APIConnectOptions;\n parallelToolCalls?: boolean;\n toolChoice?: llm.ToolChoice;\n extraKwargs?: Record<string, unknown>;\n }): llm.LLMStream {\n // Tools run server-side; warn once if the customer wired LiveKit-side tools.\n if (!this.#warnedToolCtx && toolCtx) {\n const ignored = livekitToolNames(toolCtx);\n if (ignored.length > 0) {\n this.#warnedToolCtx = true;\n console.warn(\n `@mastra/livekit: MastraLLM ignores LiveKit-side tools (${ignored.join(', ')}). ` +\n `Tools are defined and executed server-side on the Mastra agent — move them there.`,\n );\n }\n }\n return new MastraLLMStream(this, {\n chatCtx,\n toolCtx,\n connOptions,\n generator: this.resolveGenerator(connOptions),\n memory: this.#memory,\n requestContext: this.#requestContext,\n });\n }\n\n /** No-op in v1: nothing in the LiveKit session/worker ever calls `prewarm()` automatically. */\n override prewarm(): void {}\n}\n\ninterface MastraLLMStreamOptions {\n chatCtx: llm.ChatContext;\n toolCtx?: llm.ToolContext;\n connOptions: APIConnectOptions;\n generator: VoiceReplyGenerator;\n memory: MastraVoiceAgentMemory | false;\n requestContext?: RequestContext;\n}\n\n/**\n * The `llm.LLMStream` `MastraLLM` returns per turn. `run()` extracts the turn's messages, drives\n * the reply generator, and pushes assistant `ChatChunk`s into `this.queue` (NOT `this.output` — the\n * base class drains queue → output and computes TTFT / duration / usage). Barge-in aborts via\n * `this.abortController` and `run()` returns silently.\n */\nclass MastraLLMStream extends llm.LLMStream {\n readonly #generator: VoiceReplyGenerator;\n readonly #memory: MastraVoiceAgentMemory | false;\n readonly #requestContext?: RequestContext;\n\n constructor(mastraLLM: MastraLLM, options: MastraLLMStreamOptions) {\n super(mastraLLM, { chatCtx: options.chatCtx, toolCtx: options.toolCtx, connOptions: options.connOptions });\n this.#generator = options.generator;\n this.#memory = options.memory;\n this.#requestContext = options.requestContext;\n }\n\n protected async run(): Promise<void> {\n const messages =\n this.#memory === false ? chatContextToMessages(this.chatCtx) : extractNewTurnMessages(this.chatCtx);\n // No new input to answer → close without a request (equivalent to the wrapper returning null).\n if (messages.length === 0) return;\n\n let usage: VoiceTurnUsage | undefined;\n const turnCtx: VoiceTurnContext = {\n messages,\n chatCtx: this.chatCtx,\n memory: this.#memory,\n requestContext: this.#requestContext,\n onUsage: turnUsage => {\n usage = turnUsage;\n },\n };\n\n const reply = await this.#generator(turnCtx);\n if (!reply) return;\n if (this.abortController.signal.aborted) {\n await reply.cancel().catch(() => {});\n return;\n }\n\n // A single provider response id ties all of this turn's chunks together for the base class metrics.\n const id = globalThis.crypto.randomUUID();\n const reader = reply.getReader();\n const onAbort = () => void reader.cancel().catch(() => {});\n this.abortController.signal.addEventListener('abort', onAbort, { once: true });\n try {\n for (;;) {\n const { done, value } = await reader.read();\n if (done) break;\n if (this.abortController.signal.aborted) break;\n if (value) this.queue.put({ id, delta: { role: 'assistant', content: value } });\n }\n } catch (error) {\n // Barge-in tears down the reply stream; that surfaces as a read rejection but is not a failure.\n if (this.abortController.signal.aborted) return;\n throw error;\n } finally {\n this.abortController.signal.removeEventListener('abort', onAbort);\n }\n // Return silently on barge-in — throwing would feed the base class's error/retry machinery.\n if (this.abortController.signal.aborted) return;\n // Final usage-only chunk → the base class reads usage from the last chunk that carries it.\n if (usage) this.queue.put({ id, usage: mapUsageToLiveKit(usage) });\n }\n}\n\nexport { MastraLLMStream };\n"],"mappings":";;;;;AA6DA,SAAS,iBAAiB,OAAyF;CACjH,IAAI,CAAC,OAAO,OAAO,KAAA;CACnB,IAAI,iBAAiBA,6BAAAA,gBAAgB,OAAO;CAC5C,OAAO,IAAIA,6BAAAA,eAAwB,OAAO,QAAQ,KAAK,CAAC;AAC1D;AAEA,SAAS,kBAAkB,OAA4C;CACrE,OAAO;EACL,kBAAkB,MAAM;EACxB,cAAc,MAAM;EACpB,oBAAoB,MAAM;EAC1B,aAAa,MAAM;CACrB;AACF;;;;;;AAOA,SAAS,iBAAiB,SAA2B;CACnD,MAAM,WAAW;CACjB,IAAI,OAAO,SAAS,YAAY,YAC9B,OAAQ,SAAS,QAA2B,CAAC,CAAC,KAAI,SAAQ;EACxD,MAAM,EAAE,IAAI,SAAS;EACrB,OAAO,MAAM,QAAQ;CACvB,CAAC;CAEH,OAAO,OAAO,KAAK,OAAO;AAC5B;;;;;;;;;;;;;;;;;;;;;;;AAwBA,IAAa,YAAb,cAA+BC,gBAAAA,IAAI,IAAI;CACrC;CACA;CACA;;CAEA;CACA;CAKA,iBAAiB;CAEjB,YAAY,SAA2B;EACrC,MAAM;EACN,MAAM,UAAU;GACd,QAAQ,SAAS,WAAW,KAAA;GAC5B,QAAQ,QAAQ,UAAU,KAAA;GAC1B,QAAQ,WAAW,aAAa,KAAA;EAClC,CAAC,CAAC,OAAO,OAAO;EAChB,IAAI,QAAQ,WAAW,GACrB,MAAM,IAAI,MACR,mHACa,QAAQ,WAAW,IAAI,SAAS,QAAQ,KAAK,KAAK,EAAE,EACnE;EAGF,KAAKE,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