@mastra/core 0.7.0 → 0.8.0-alpha.2

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 (149) hide show
  1. package/README.md +76 -74
  2. package/dist/agent/index.cjs +2 -2
  3. package/dist/agent/index.d.cts +5 -3
  4. package/dist/agent/index.d.ts +5 -3
  5. package/dist/agent/index.js +1 -1
  6. package/dist/{base-Cmunaaxb.d.ts → base-BA_in99t.d.ts} +4 -1
  7. package/dist/{base-Cyl73WbV.d.ts → base-CQuRWXAH.d.ts} +985 -913
  8. package/dist/{base-ObPJ-w8K.d.cts → base-CvQbEqGB.d.cts} +4 -1
  9. package/dist/{base-C0wILuA9.d.cts → base-Dpt4tO56.d.cts} +985 -913
  10. package/dist/base.cjs +2 -2
  11. package/dist/base.d.cts +1 -1
  12. package/dist/base.d.ts +1 -1
  13. package/dist/base.js +1 -1
  14. package/dist/bundler/index.cjs +2 -2
  15. package/dist/bundler/index.d.cts +1 -1
  16. package/dist/bundler/index.d.ts +1 -1
  17. package/dist/bundler/index.js +1 -1
  18. package/dist/{chunk-L7CR75HA.js → chunk-2BVZNKLX.js} +1 -1
  19. package/dist/{chunk-V5ORZPFW.cjs → chunk-3C6V2FEP.cjs} +1 -1
  20. package/dist/chunk-3HE5CJBG.cjs +4604 -0
  21. package/dist/{chunk-PNZK456O.js → chunk-4RMSGSQN.js} +4 -4
  22. package/dist/{chunk-WESJ2ZY7.cjs → chunk-57LXIDIK.cjs} +2 -2
  23. package/dist/chunk-5RRJEWMA.cjs +107 -0
  24. package/dist/{chunk-3CNO7YB5.js → chunk-5TFGORLG.js} +1 -1
  25. package/dist/{chunk-5FAJ6HUC.cjs → chunk-64VPB7ZD.cjs} +23 -2
  26. package/dist/chunk-6XLV4KSD.js +100 -0
  27. package/dist/{chunk-XLSROQ26.cjs → chunk-7I7AKQH3.cjs} +5 -5
  28. package/dist/{chunk-RUA6BSB6.cjs → chunk-7JBINHJX.cjs} +2 -2
  29. package/dist/chunk-A6MBM56X.js +268 -0
  30. package/dist/{chunk-2YF5JYTJ.js → chunk-BC5B4EGI.js} +21 -2
  31. package/dist/chunk-BF7ZLRGO.cjs +271 -0
  32. package/dist/{chunk-SMBKF6K5.js → chunk-BRBHQ6KS.js} +10 -1
  33. package/dist/{chunk-VN4M67DA.js → chunk-CLJQYXNM.js} +1 -1
  34. package/dist/{chunk-PL7PVTGF.cjs → chunk-D63P5O4Q.cjs} +4 -4
  35. package/dist/chunk-DCGLGWHI.cjs +141 -0
  36. package/dist/{chunk-YXJQFZOW.cjs → chunk-DL4QPJKI.cjs} +24 -10
  37. package/dist/{chunk-ZABXT2MN.js → chunk-FY57LEMC.js} +1 -1
  38. package/dist/{chunk-XLXJUYKH.js → chunk-GHK3HBWN.js} +1 -1
  39. package/dist/{chunk-ONDCHP6G.cjs → chunk-GO2D7FEK.cjs} +2 -2
  40. package/dist/{chunk-7VTZI3YN.js → chunk-H6ZU5N2C.js} +1 -1
  41. package/dist/{chunk-WBE5RTFI.js → chunk-HAWAUEWK.js} +2 -2
  42. package/dist/chunk-HCDXBSMK.js +4577 -0
  43. package/dist/{chunk-RU7CSPAV.js → chunk-JGJMVCJ4.js} +7 -84
  44. package/dist/{chunk-W5HVJX45.js → chunk-N5ZYOQBL.js} +46 -5
  45. package/dist/{chunk-NPOKIPWC.cjs → chunk-O7IW545H.cjs} +1 -1
  46. package/dist/{chunk-XB2TJ7LX.cjs → chunk-PHWEC4VD.cjs} +47 -6
  47. package/dist/{chunk-NUDAZEOG.js → chunk-PK5QRKSG.js} +1 -1
  48. package/dist/{chunk-43SD5CUE.js → chunk-QDHRRKQ5.js} +63 -9
  49. package/dist/{chunk-ZBKJDQPM.js → chunk-RBWBJC6D.js} +17 -3
  50. package/dist/{chunk-4PYORXWM.cjs → chunk-RXDLSCBA.cjs} +4 -4
  51. package/dist/{chunk-IQLRSKED.cjs → chunk-RZCYBC7D.cjs} +2 -2
  52. package/dist/{chunk-ASFUEC75.cjs → chunk-TNNHN4ZY.cjs} +67 -12
  53. package/dist/{chunk-QM6WIIPM.js → chunk-UF4LZV3D.js} +1 -1
  54. package/dist/{chunk-JJ4YQTFT.cjs → chunk-WAW7QBY4.cjs} +2 -2
  55. package/dist/{chunk-U7ONOIBO.cjs → chunk-YZDUZFVZ.cjs} +11 -2
  56. package/dist/deployer/index.cjs +2 -2
  57. package/dist/deployer/index.d.cts +1 -1
  58. package/dist/deployer/index.d.ts +1 -1
  59. package/dist/deployer/index.js +1 -1
  60. package/dist/eval/index.cjs +3 -3
  61. package/dist/eval/index.d.cts +8 -5
  62. package/dist/eval/index.d.ts +8 -5
  63. package/dist/eval/index.js +1 -1
  64. package/dist/hooks/index.d.cts +1 -1
  65. package/dist/hooks/index.d.ts +1 -1
  66. package/dist/index.cjs +102 -92
  67. package/dist/index.d.cts +11 -9
  68. package/dist/index.d.ts +11 -9
  69. package/dist/index.js +18 -19
  70. package/dist/integration/index.cjs +3 -3
  71. package/dist/integration/index.d.cts +5 -3
  72. package/dist/integration/index.d.ts +5 -3
  73. package/dist/integration/index.js +1 -1
  74. package/dist/llm/index.d.cts +5 -3
  75. package/dist/llm/index.d.ts +5 -3
  76. package/dist/logger/index.cjs +9 -9
  77. package/dist/logger/index.js +1 -1
  78. package/dist/mastra/index.cjs +2 -2
  79. package/dist/mastra/index.d.cts +5 -3
  80. package/dist/mastra/index.d.ts +5 -3
  81. package/dist/mastra/index.js +1 -1
  82. package/dist/memory/index.cjs +6 -2
  83. package/dist/memory/index.d.cts +5 -3
  84. package/dist/memory/index.d.ts +5 -3
  85. package/dist/memory/index.js +1 -1
  86. package/dist/network/index.cjs +8 -8
  87. package/dist/network/index.d.cts +5 -3
  88. package/dist/network/index.d.ts +5 -3
  89. package/dist/network/index.js +4 -4
  90. package/dist/relevance/index.cjs +4 -4
  91. package/dist/relevance/index.d.cts +10 -8
  92. package/dist/relevance/index.d.ts +10 -8
  93. package/dist/relevance/index.js +1 -1
  94. package/dist/server/index.cjs +17 -0
  95. package/dist/server/index.d.cts +37 -0
  96. package/dist/server/index.d.ts +37 -0
  97. package/dist/server/index.js +15 -0
  98. package/dist/storage/index.cjs +12 -8
  99. package/dist/storage/index.d.cts +5 -3
  100. package/dist/storage/index.d.ts +5 -3
  101. package/dist/storage/index.js +2 -2
  102. package/dist/storage/libsql/index.cjs +24 -24
  103. package/dist/storage/libsql/index.d.cts +5 -3
  104. package/dist/storage/libsql/index.d.ts +5 -3
  105. package/dist/storage/libsql/index.js +12 -12
  106. package/dist/telemetry/index.cjs +6 -6
  107. package/dist/telemetry/index.d.cts +5 -3
  108. package/dist/telemetry/index.d.ts +5 -3
  109. package/dist/telemetry/index.js +1 -1
  110. package/dist/tools/index.cjs +7 -3
  111. package/dist/tools/index.d.cts +6 -3
  112. package/dist/tools/index.d.ts +6 -3
  113. package/dist/tools/index.js +1 -1
  114. package/dist/tts/index.cjs +2 -2
  115. package/dist/tts/index.d.cts +1 -1
  116. package/dist/tts/index.d.ts +1 -1
  117. package/dist/tts/index.js +1 -1
  118. package/dist/{types-CwTG2XyQ.d.cts → types-BtMyV38I.d.cts} +4 -1
  119. package/dist/{types-CwTG2XyQ.d.ts → types-BtMyV38I.d.ts} +4 -1
  120. package/dist/utils.cjs +13 -13
  121. package/dist/utils.d.cts +7 -5
  122. package/dist/utils.d.ts +7 -5
  123. package/dist/utils.js +1 -1
  124. package/dist/vector/index.cjs +2 -2
  125. package/dist/vector/index.d.cts +1 -1
  126. package/dist/vector/index.d.ts +1 -1
  127. package/dist/vector/index.js +1 -1
  128. package/dist/vector/libsql/index.cjs +3 -3
  129. package/dist/vector/libsql/index.d.cts +1 -1
  130. package/dist/vector/libsql/index.d.ts +1 -1
  131. package/dist/vector/libsql/index.js +1 -1
  132. package/dist/voice/index.cjs +13 -249
  133. package/dist/voice/index.d.cts +22 -8
  134. package/dist/voice/index.d.ts +22 -8
  135. package/dist/voice/index.js +1 -252
  136. package/dist/workflows/index.cjs +28 -20
  137. package/dist/workflows/index.d.cts +18 -7
  138. package/dist/workflows/index.d.ts +18 -7
  139. package/dist/workflows/index.js +1 -1
  140. package/package.json +5 -3
  141. package/dist/chunk-2W2GYEYQ.cjs +0 -25
  142. package/dist/chunk-C6BBAS4I.cjs +0 -1715
  143. package/dist/chunk-F5UYWPV4.cjs +0 -14
  144. package/dist/chunk-GG6TEAMJ.cjs +0 -2289
  145. package/dist/chunk-R2M5CZ5U.js +0 -2264
  146. package/dist/chunk-RG66XEJT.js +0 -8
  147. package/dist/chunk-VNQRLYIA.js +0 -1715
  148. package/dist/chunk-WVVKLIUW.cjs +0 -218
  149. package/dist/chunk-ZINPRHAN.js +0 -22
@@ -1,1715 +0,0 @@
1
- 'use strict';
2
-
3
- var chunkXB2TJ7LX_cjs = require('./chunk-XB2TJ7LX.cjs');
4
- var chunk5FAJ6HUC_cjs = require('./chunk-5FAJ6HUC.cjs');
5
- var chunkPL7PVTGF_cjs = require('./chunk-PL7PVTGF.cjs');
6
- var chunkNPOKIPWC_cjs = require('./chunk-NPOKIPWC.cjs');
7
- var chunkST5RMVLG_cjs = require('./chunk-ST5RMVLG.cjs');
8
- var chunkRWTSGWWL_cjs = require('./chunk-RWTSGWWL.cjs');
9
- var crypto$1 = require('crypto');
10
- var ai = require('ai');
11
- var zod = require('zod');
12
-
13
- // src/llm/model/base.ts
14
- var MastraLLMBase = class extends chunkPL7PVTGF_cjs.MastraBase {
15
- // @ts-ignore
16
- #mastra;
17
- #model;
18
- constructor({
19
- name,
20
- model
21
- }) {
22
- super({
23
- component: chunkNPOKIPWC_cjs.RegisteredLogger.LLM,
24
- name
25
- });
26
- this.#model = model;
27
- }
28
- getProvider() {
29
- return this.#model.provider;
30
- }
31
- getModelId() {
32
- return this.#model.modelId;
33
- }
34
- getModel() {
35
- return this.#model;
36
- }
37
- convertToMessages(messages) {
38
- if (Array.isArray(messages)) {
39
- return messages.map(m => {
40
- if (typeof m === "string") {
41
- return {
42
- role: "user",
43
- content: m
44
- };
45
- }
46
- return m;
47
- });
48
- }
49
- return [{
50
- role: "user",
51
- content: messages
52
- }];
53
- }
54
- __registerPrimitives(p) {
55
- if (p.telemetry) {
56
- this.__setTelemetry(p.telemetry);
57
- }
58
- if (p.logger) {
59
- this.__setLogger(p.logger);
60
- }
61
- }
62
- __registerMastra(p) {
63
- this.#mastra = p;
64
- }
65
- async __text(input) {
66
- this.logger.debug(`[LLMs:${this.name}] Generating text.`, {
67
- input
68
- });
69
- throw new Error("Method not implemented.");
70
- }
71
- async __textObject(input) {
72
- this.logger.debug(`[LLMs:${this.name}] Generating object.`, {
73
- input
74
- });
75
- throw new Error("Method not implemented.");
76
- }
77
- async generate(messages, options = {}) {
78
- this.logger.debug(`[LLMs:${this.name}] Generating text.`, {
79
- messages,
80
- options
81
- });
82
- throw new Error("Method not implemented.");
83
- }
84
- async __stream(input) {
85
- this.logger.debug(`[LLMs:${this.name}] Streaming text.`, {
86
- input
87
- });
88
- throw new Error("Method not implemented.");
89
- }
90
- async __streamObject(input) {
91
- this.logger.debug(`[LLMs:${this.name}] Streaming object.`, {
92
- input
93
- });
94
- throw new Error("Method not implemented.");
95
- }
96
- async stream(messages, options = {}) {
97
- this.logger.debug(`[LLMs:${this.name}] Streaming text.`, {
98
- messages,
99
- options
100
- });
101
- throw new Error("Method not implemented.");
102
- }
103
- };
104
- var MastraLLM = class extends MastraLLMBase {
105
- #model;
106
- #mastra;
107
- constructor({
108
- model,
109
- mastra
110
- }) {
111
- super({
112
- name: "aisdk",
113
- model
114
- });
115
- this.#model = model;
116
- if (mastra) {
117
- this.#mastra = mastra;
118
- if (mastra.getLogger()) {
119
- this.__setLogger(mastra.getLogger());
120
- }
121
- }
122
- }
123
- __registerPrimitives(p) {
124
- if (p.telemetry) {
125
- this.__setTelemetry(p.telemetry);
126
- }
127
- if (p.logger) {
128
- this.__setLogger(p.logger);
129
- }
130
- }
131
- __registerMastra(p) {
132
- this.#mastra = p;
133
- }
134
- getProvider() {
135
- return this.#model.provider;
136
- }
137
- getModelId() {
138
- return this.#model.modelId;
139
- }
140
- getModel() {
141
- return this.#model;
142
- }
143
- convertTools({
144
- tools,
145
- runId,
146
- threadId,
147
- resourceId,
148
- memory
149
- } = {}) {
150
- this.logger.debug("Starting tool conversion for LLM");
151
- let mastraProxy = void 0;
152
- const logger = this.logger;
153
- if (this.#mastra) {
154
- mastraProxy = chunk5FAJ6HUC_cjs.createMastraProxy({
155
- mastra: this.#mastra,
156
- logger
157
- });
158
- }
159
- const converted = Object.entries(tools || {}).reduce((memo, value) => {
160
- const k = value[0];
161
- const tool = value[1];
162
- if (tool) {
163
- const options = {
164
- name: k,
165
- runId,
166
- threadId,
167
- resourceId,
168
- logger: this.logger,
169
- memory,
170
- mastra: mastraProxy
171
- };
172
- memo[k] = chunk5FAJ6HUC_cjs.makeCoreTool(tool, options);
173
- }
174
- return memo;
175
- }, {});
176
- this.logger.debug(`Converted tools for LLM`);
177
- return converted;
178
- }
179
- async __text({
180
- runId,
181
- messages,
182
- maxSteps,
183
- tools,
184
- convertedTools,
185
- temperature,
186
- toolChoice = "auto",
187
- onStepFinish,
188
- experimental_output,
189
- telemetry,
190
- threadId,
191
- resourceId,
192
- memory,
193
- ...rest
194
- }) {
195
- const model = this.#model;
196
- this.logger.debug(`[LLM] - Generating text`, {
197
- runId,
198
- messages,
199
- maxSteps,
200
- threadId,
201
- resourceId,
202
- tools: Object.keys(tools || convertedTools || {})
203
- });
204
- const finalTools = convertedTools || this.convertTools({
205
- tools,
206
- runId,
207
- threadId,
208
- resourceId,
209
- memory
210
- });
211
- const argsForExecute = {
212
- model,
213
- temperature,
214
- tools: {
215
- ...finalTools
216
- },
217
- toolChoice,
218
- maxSteps,
219
- onStepFinish: async props => {
220
- void onStepFinish?.(props);
221
- this.logger.debug("[LLM] - Step Change:", {
222
- text: props?.text,
223
- toolCalls: props?.toolCalls,
224
- toolResults: props?.toolResults,
225
- finishReason: props?.finishReason,
226
- usage: props?.usage,
227
- runId
228
- });
229
- if (props?.response?.headers?.["x-ratelimit-remaining-tokens"] && parseInt(props?.response?.headers?.["x-ratelimit-remaining-tokens"], 10) < 2e3) {
230
- this.logger.warn("Rate limit approaching, waiting 10 seconds", {
231
- runId
232
- });
233
- await chunk5FAJ6HUC_cjs.delay(10 * 1e3);
234
- }
235
- },
236
- ...rest
237
- };
238
- let schema;
239
- if (experimental_output) {
240
- this.logger.debug("[LLM] - Using experimental output", {
241
- runId
242
- });
243
- if (typeof experimental_output.parse === "function") {
244
- schema = experimental_output;
245
- if (schema instanceof zod.z.ZodArray) {
246
- schema = schema._def.type;
247
- }
248
- } else {
249
- schema = ai.jsonSchema(experimental_output);
250
- }
251
- }
252
- return await ai.generateText({
253
- messages,
254
- ...argsForExecute,
255
- experimental_telemetry: {
256
- ...this.experimental_telemetry,
257
- ...telemetry
258
- },
259
- experimental_output: schema ? ai.Output.object({
260
- schema
261
- }) : void 0
262
- });
263
- }
264
- async __textObject({
265
- messages,
266
- onStepFinish,
267
- maxSteps = 5,
268
- tools,
269
- convertedTools,
270
- structuredOutput,
271
- runId,
272
- temperature,
273
- toolChoice = "auto",
274
- telemetry,
275
- threadId,
276
- resourceId,
277
- memory,
278
- ...rest
279
- }) {
280
- const model = this.#model;
281
- this.logger.debug(`[LLM] - Generating a text object`, {
282
- runId
283
- });
284
- const finalTools = convertedTools || this.convertTools({
285
- tools,
286
- runId,
287
- threadId,
288
- resourceId,
289
- memory
290
- });
291
- const argsForExecute = {
292
- model,
293
- temperature,
294
- tools: {
295
- ...finalTools
296
- },
297
- maxSteps,
298
- toolChoice,
299
- onStepFinish: async props => {
300
- void onStepFinish?.(props);
301
- this.logger.debug("[LLM] - Step Change:", {
302
- text: props?.text,
303
- toolCalls: props?.toolCalls,
304
- toolResults: props?.toolResults,
305
- finishReason: props?.finishReason,
306
- usage: props?.usage,
307
- runId
308
- });
309
- if (props?.response?.headers?.["x-ratelimit-remaining-tokens"] && parseInt(props?.response?.headers?.["x-ratelimit-remaining-tokens"], 10) < 2e3) {
310
- this.logger.warn("Rate limit approaching, waiting 10 seconds", {
311
- runId
312
- });
313
- await chunk5FAJ6HUC_cjs.delay(10 * 1e3);
314
- }
315
- },
316
- ...rest
317
- };
318
- let schema;
319
- let output = "object";
320
- if (typeof structuredOutput.parse === "function") {
321
- schema = structuredOutput;
322
- if (schema instanceof zod.z.ZodArray) {
323
- output = "array";
324
- schema = schema._def.type;
325
- }
326
- } else {
327
- schema = ai.jsonSchema(structuredOutput);
328
- }
329
- return await ai.generateObject({
330
- messages,
331
- ...argsForExecute,
332
- output,
333
- schema,
334
- experimental_telemetry: {
335
- ...this.experimental_telemetry,
336
- ...telemetry
337
- }
338
- });
339
- }
340
- async __stream({
341
- messages,
342
- onStepFinish,
343
- onFinish,
344
- maxSteps = 5,
345
- tools,
346
- convertedTools,
347
- runId,
348
- temperature,
349
- toolChoice = "auto",
350
- experimental_output,
351
- telemetry,
352
- threadId,
353
- resourceId,
354
- memory,
355
- ...rest
356
- }) {
357
- const model = this.#model;
358
- this.logger.debug(`[LLM] - Streaming text`, {
359
- runId,
360
- threadId,
361
- resourceId,
362
- messages,
363
- maxSteps,
364
- tools: Object.keys(tools || convertedTools || {})
365
- });
366
- const finalTools = convertedTools || this.convertTools({
367
- tools,
368
- runId,
369
- threadId,
370
- resourceId,
371
- memory
372
- });
373
- const argsForExecute = {
374
- model,
375
- temperature,
376
- tools: {
377
- ...finalTools
378
- },
379
- maxSteps,
380
- toolChoice,
381
- onStepFinish: async props => {
382
- void onStepFinish?.(props);
383
- this.logger.debug("[LLM] - Stream Step Change:", {
384
- text: props?.text,
385
- toolCalls: props?.toolCalls,
386
- toolResults: props?.toolResults,
387
- finishReason: props?.finishReason,
388
- usage: props?.usage,
389
- runId
390
- });
391
- if (props?.response?.headers?.["x-ratelimit-remaining-tokens"] && parseInt(props?.response?.headers?.["x-ratelimit-remaining-tokens"], 10) < 2e3) {
392
- this.logger.warn("Rate limit approaching, waiting 10 seconds", {
393
- runId
394
- });
395
- await chunk5FAJ6HUC_cjs.delay(10 * 1e3);
396
- }
397
- },
398
- onFinish: async props => {
399
- void onFinish?.(props);
400
- this.logger.debug("[LLM] - Stream Finished:", {
401
- text: props?.text,
402
- toolCalls: props?.toolCalls,
403
- toolResults: props?.toolResults,
404
- finishReason: props?.finishReason,
405
- usage: props?.usage,
406
- runId,
407
- threadId,
408
- resourceId
409
- });
410
- },
411
- ...rest
412
- };
413
- let schema;
414
- if (experimental_output) {
415
- this.logger.debug("[LLM] - Using experimental output", {
416
- runId
417
- });
418
- if (typeof experimental_output.parse === "function") {
419
- schema = experimental_output;
420
- if (schema instanceof zod.z.ZodArray) {
421
- schema = schema._def.type;
422
- }
423
- } else {
424
- schema = ai.jsonSchema(experimental_output);
425
- }
426
- }
427
- return await ai.streamText({
428
- messages,
429
- ...argsForExecute,
430
- experimental_telemetry: {
431
- ...this.experimental_telemetry,
432
- ...telemetry
433
- },
434
- experimental_output: schema ? ai.Output.object({
435
- schema
436
- }) : void 0
437
- });
438
- }
439
- async __streamObject({
440
- messages,
441
- onStepFinish,
442
- onFinish,
443
- maxSteps = 5,
444
- tools,
445
- convertedTools,
446
- structuredOutput,
447
- runId,
448
- temperature,
449
- toolChoice = "auto",
450
- telemetry,
451
- threadId,
452
- resourceId,
453
- memory,
454
- ...rest
455
- }) {
456
- const model = this.#model;
457
- this.logger.debug(`[LLM] - Streaming structured output`, {
458
- runId,
459
- messages,
460
- maxSteps,
461
- tools: Object.keys(tools || convertedTools || {})
462
- });
463
- const finalTools = convertedTools || this.convertTools({
464
- tools,
465
- runId,
466
- threadId,
467
- resourceId,
468
- memory
469
- });
470
- const argsForExecute = {
471
- model,
472
- temperature,
473
- tools: {
474
- ...finalTools
475
- },
476
- maxSteps,
477
- toolChoice,
478
- onStepFinish: async props => {
479
- void onStepFinish?.(props);
480
- this.logger.debug("[LLM] - Stream Step Change:", {
481
- text: props?.text,
482
- toolCalls: props?.toolCalls,
483
- toolResults: props?.toolResults,
484
- finishReason: props?.finishReason,
485
- usage: props?.usage,
486
- runId,
487
- threadId,
488
- resourceId
489
- });
490
- if (props?.response?.headers?.["x-ratelimit-remaining-tokens"] && parseInt(props?.response?.headers?.["x-ratelimit-remaining-tokens"], 10) < 2e3) {
491
- this.logger.warn("Rate limit approaching, waiting 10 seconds", {
492
- runId
493
- });
494
- await chunk5FAJ6HUC_cjs.delay(10 * 1e3);
495
- }
496
- },
497
- onFinish: async props => {
498
- void onFinish?.(props);
499
- this.logger.debug("[LLM] - Stream Finished:", {
500
- text: props?.text,
501
- toolCalls: props?.toolCalls,
502
- toolResults: props?.toolResults,
503
- finishReason: props?.finishReason,
504
- usage: props?.usage,
505
- runId,
506
- threadId,
507
- resourceId
508
- });
509
- },
510
- ...rest
511
- };
512
- let schema;
513
- let output = "object";
514
- if (typeof structuredOutput.parse === "function") {
515
- schema = structuredOutput;
516
- if (schema instanceof zod.z.ZodArray) {
517
- output = "array";
518
- schema = schema._def.type;
519
- }
520
- } else {
521
- schema = ai.jsonSchema(structuredOutput);
522
- }
523
- return ai.streamObject({
524
- messages,
525
- ...argsForExecute,
526
- output,
527
- schema,
528
- experimental_telemetry: {
529
- ...this.experimental_telemetry,
530
- ...telemetry
531
- }
532
- });
533
- }
534
- async generate(messages, {
535
- maxSteps = 5,
536
- onStepFinish,
537
- tools,
538
- convertedTools,
539
- runId,
540
- output,
541
- temperature,
542
- telemetry,
543
- memory,
544
- ...rest
545
- } = {}) {
546
- const msgs = this.convertToMessages(messages);
547
- if (!output) {
548
- return await this.__text({
549
- messages: msgs,
550
- onStepFinish,
551
- maxSteps,
552
- tools,
553
- convertedTools,
554
- runId,
555
- temperature,
556
- memory,
557
- ...rest
558
- });
559
- }
560
- return await this.__textObject({
561
- messages: msgs,
562
- structuredOutput: output,
563
- onStepFinish,
564
- maxSteps,
565
- tools,
566
- convertedTools,
567
- runId,
568
- telemetry,
569
- memory,
570
- ...rest
571
- });
572
- }
573
- async stream(messages, {
574
- maxSteps = 5,
575
- onFinish,
576
- onStepFinish,
577
- tools,
578
- convertedTools,
579
- runId,
580
- output,
581
- temperature,
582
- telemetry,
583
- ...rest
584
- } = {}) {
585
- const msgs = this.convertToMessages(messages);
586
- if (!output) {
587
- return await this.__stream({
588
- messages: msgs,
589
- onStepFinish,
590
- onFinish,
591
- maxSteps,
592
- tools,
593
- convertedTools,
594
- runId,
595
- temperature,
596
- telemetry,
597
- ...rest
598
- });
599
- }
600
- return await this.__streamObject({
601
- messages: msgs,
602
- structuredOutput: output,
603
- onStepFinish,
604
- onFinish,
605
- maxSteps,
606
- tools,
607
- convertedTools,
608
- runId,
609
- temperature,
610
- telemetry,
611
- ...rest
612
- });
613
- }
614
- convertToUIMessages(messages) {
615
- function addToolMessageToChat({
616
- toolMessage,
617
- messages: messages2,
618
- toolResultContents
619
- }) {
620
- const chatMessages2 = messages2.map(message => {
621
- if (message.toolInvocations) {
622
- return {
623
- ...message,
624
- toolInvocations: message.toolInvocations.map(toolInvocation => {
625
- const toolResult = toolMessage.content.find(tool => tool.toolCallId === toolInvocation.toolCallId);
626
- if (toolResult) {
627
- return {
628
- ...toolInvocation,
629
- state: "result",
630
- result: toolResult.result
631
- };
632
- }
633
- return toolInvocation;
634
- })
635
- };
636
- }
637
- return message;
638
- });
639
- const resultContents = [...toolResultContents, ...toolMessage.content];
640
- return {
641
- chatMessages: chatMessages2,
642
- toolResultContents: resultContents
643
- };
644
- }
645
- const {
646
- chatMessages
647
- } = messages.reduce((obj, message) => {
648
- if (message.role === "tool") {
649
- return addToolMessageToChat({
650
- toolMessage: message,
651
- messages: obj.chatMessages,
652
- toolResultContents: obj.toolResultContents
653
- });
654
- }
655
- let textContent = "";
656
- let toolInvocations = [];
657
- if (typeof message.content === "string") {
658
- textContent = message.content;
659
- } else if (typeof message.content === "number") {
660
- textContent = String(message.content);
661
- } else if (Array.isArray(message.content)) {
662
- for (const content of message.content) {
663
- if (content.type === "text") {
664
- textContent += content.text;
665
- } else if (content.type === "tool-call") {
666
- const toolResult = obj.toolResultContents.find(tool => tool.toolCallId === content.toolCallId);
667
- toolInvocations.push({
668
- state: toolResult ? "result" : "call",
669
- toolCallId: content.toolCallId,
670
- toolName: content.toolName,
671
- args: content.args,
672
- result: toolResult?.result
673
- });
674
- }
675
- }
676
- }
677
- obj.chatMessages.push({
678
- id: message.id,
679
- role: message.role,
680
- content: textContent,
681
- toolInvocations
682
- });
683
- return obj;
684
- }, {
685
- chatMessages: [],
686
- toolResultContents: []
687
- });
688
- return chatMessages;
689
- }
690
- };
691
-
692
- // src/agent/index.ts
693
- var _Agent_decorators, _init, _a;
694
- _Agent_decorators = [chunkXB2TJ7LX_cjs.InstrumentClass({
695
- prefix: "agent",
696
- excludeMethods: ["__setTools", "__setLogger", "__setTelemetry", "log"]
697
- })];
698
- exports.Agent = class Agent extends (_a = chunkPL7PVTGF_cjs.MastraBase) {
699
- name;
700
- llm;
701
- instructions;
702
- model;
703
- #mastra;
704
- #memory;
705
- tools;
706
- /** @deprecated This property is deprecated. Use evals instead. */
707
- metrics;
708
- evals;
709
- voice;
710
- constructor(config) {
711
- super({
712
- component: chunkNPOKIPWC_cjs.RegisteredLogger.AGENT
713
- });
714
- this.name = config.name;
715
- this.instructions = config.instructions;
716
- if (!config.model) {
717
- throw new Error(`LanguageModel is required to create an Agent. Please provide the 'model'.`);
718
- }
719
- this.llm = new MastraLLM({
720
- model: config.model,
721
- mastra: config.mastra
722
- });
723
- this.tools = {};
724
- this.metrics = {};
725
- this.evals = {};
726
- if (config.tools) {
727
- this.tools = chunk5FAJ6HUC_cjs.ensureToolProperties(config.tools);
728
- }
729
- if (config.mastra) {
730
- this.__registerMastra(config.mastra);
731
- this.__registerPrimitives({
732
- telemetry: config.mastra.getTelemetry(),
733
- logger: config.mastra.getLogger()
734
- });
735
- }
736
- if (config.metrics) {
737
- this.logger.warn("The metrics property is deprecated. Please use evals instead to add evaluation metrics.");
738
- this.metrics = config.metrics;
739
- this.evals = config.metrics;
740
- }
741
- if (config.evals) {
742
- this.evals = config.evals;
743
- }
744
- if (config.memory) {
745
- this.#memory = config.memory;
746
- }
747
- if (config.voice) {
748
- this.voice = config.voice;
749
- this.voice?.addTools(this.tools);
750
- this.voice?.addInstructions(config.instructions);
751
- }
752
- }
753
- hasOwnMemory() {
754
- return Boolean(this.#memory);
755
- }
756
- getMemory() {
757
- return this.#memory ?? this.#mastra?.memory;
758
- }
759
- __updateInstructions(newInstructions) {
760
- this.instructions = newInstructions;
761
- this.logger.debug(`[Agents:${this.name}] Instructions updated.`, {
762
- model: this.model,
763
- name: this.name
764
- });
765
- }
766
- __registerPrimitives(p) {
767
- if (p.telemetry) {
768
- this.__setTelemetry(p.telemetry);
769
- }
770
- if (p.logger) {
771
- this.__setLogger(p.logger);
772
- }
773
- this.llm.__registerPrimitives(p);
774
- this.logger.debug(`[Agents:${this.name}] initialized.`, {
775
- model: this.model,
776
- name: this.name
777
- });
778
- }
779
- __registerMastra(mastra) {
780
- this.#mastra = mastra;
781
- this.llm.__registerMastra(mastra);
782
- }
783
- /**
784
- * Set the concrete tools for the agent
785
- * @param tools
786
- */
787
- __setTools(tools) {
788
- this.tools = tools;
789
- this.logger.debug(`[Agents:${this.name}] Tools set for agent ${this.name}`, {
790
- model: this.model,
791
- name: this.name
792
- });
793
- }
794
- async generateTitleFromUserMessage({
795
- message
796
- }) {
797
- const {
798
- text
799
- } = await this.llm.__text({
800
- messages: [{
801
- role: "system",
802
- content: `
803
-
804
- - you will generate a short title based on the first message a user begins a conversation with
805
- - ensure it is not more than 80 characters long
806
- - the title should be a summary of the user's message
807
- - do not use quotes or colons
808
- - the entire text you return will be used as the title`
809
- }, {
810
- role: "user",
811
- content: JSON.stringify(message)
812
- }]
813
- });
814
- const cleanedText = text.replace(/<think>[\s\S]*?<\/think>/g, "").trim();
815
- return cleanedText;
816
- }
817
- getMostRecentUserMessage(messages) {
818
- const userMessages = messages.filter(message => message.role === "user");
819
- return userMessages.at(-1);
820
- }
821
- async genTitle(userMessage) {
822
- let title = `New Thread ${(/* @__PURE__ */new Date()).toISOString()}`;
823
- try {
824
- if (userMessage) {
825
- title = await this.generateTitleFromUserMessage({
826
- message: userMessage
827
- });
828
- }
829
- } catch (e) {
830
- console.error("Error generating title:", e);
831
- }
832
- return title;
833
- }
834
- async fetchMemory({
835
- threadId,
836
- memoryConfig,
837
- resourceId,
838
- userMessages,
839
- runId
840
- }) {
841
- const memory = this.getMemory();
842
- if (memory) {
843
- const thread = await memory.getThreadById({
844
- threadId
845
- });
846
- if (!thread) {
847
- return {
848
- threadId: threadId || "",
849
- messages: userMessages
850
- };
851
- }
852
- const newMessages = chunk5FAJ6HUC_cjs.ensureAllMessagesAreCoreMessages(userMessages);
853
- const messages = newMessages.map(u => {
854
- return {
855
- id: this.getMemory()?.generateId(),
856
- createdAt: /* @__PURE__ */new Date(),
857
- threadId,
858
- ...u,
859
- content: u.content,
860
- role: u.role,
861
- type: "text"
862
- };
863
- });
864
- const [memoryMessages, memorySystemMessage] = threadId && memory ? await Promise.all([memory.rememberMessages({
865
- threadId,
866
- resourceId,
867
- config: memoryConfig,
868
- vectorMessageSearch: messages.slice(-1).map(m => {
869
- if (typeof m === `string`) {
870
- return m;
871
- }
872
- return m?.content || ``;
873
- }).join(`
874
- `)
875
- }).then(r => r.messages), memory.getSystemMessage({
876
- threadId,
877
- memoryConfig
878
- })]) : [[], null];
879
- this.logger.debug("Saved messages to memory", {
880
- threadId,
881
- runId
882
- });
883
- return {
884
- threadId: thread.id,
885
- messages: [memorySystemMessage ? {
886
- role: "system",
887
- content: memorySystemMessage
888
- } : null, ...this.sanitizeResponseMessages(memoryMessages), ...newMessages].filter(message => Boolean(message))
889
- };
890
- }
891
- return {
892
- threadId: threadId || "",
893
- messages: userMessages
894
- };
895
- }
896
- async saveResponse({
897
- result,
898
- threadId,
899
- resourceId,
900
- runId,
901
- memoryConfig
902
- }) {
903
- const {
904
- response
905
- } = result;
906
- try {
907
- if (response.messages) {
908
- const ms = Array.isArray(response.messages) ? response.messages : [response.messages];
909
- const responseMessagesWithoutIncompleteToolCalls = this.sanitizeResponseMessages(ms);
910
- const memory = this.getMemory();
911
- if (memory) {
912
- this.logger.debug(`[Agent:${this.name}] - Memory persistence: store=${this.getMemory()?.constructor.name} threadId=${threadId}`, {
913
- runId,
914
- resourceId,
915
- threadId,
916
- memoryStore: this.getMemory()?.constructor.name
917
- });
918
- await memory.saveMessages({
919
- memoryConfig,
920
- messages: responseMessagesWithoutIncompleteToolCalls.map((message, index) => {
921
- const messageId = crypto$1.randomUUID();
922
- let toolCallIds;
923
- let toolCallArgs;
924
- let toolNames;
925
- let type = "text";
926
- if (message.role === "tool") {
927
- toolCallIds = message.content.map(content => content.toolCallId);
928
- type = "tool-result";
929
- }
930
- if (message.role === "assistant") {
931
- const assistantContent = message.content;
932
- const assistantToolCalls = assistantContent.map(content => {
933
- if (content.type === "tool-call") {
934
- return {
935
- toolCallId: content.toolCallId,
936
- toolArgs: content.args,
937
- toolName: content.toolName
938
- };
939
- }
940
- return void 0;
941
- })?.filter(Boolean);
942
- toolCallIds = assistantToolCalls?.map(toolCall => toolCall.toolCallId);
943
- toolCallArgs = assistantToolCalls?.map(toolCall => toolCall.toolArgs);
944
- toolNames = assistantToolCalls?.map(toolCall => toolCall.toolName);
945
- type = assistantContent?.[0]?.type;
946
- }
947
- return {
948
- id: messageId,
949
- threadId,
950
- role: message.role,
951
- content: message.content,
952
- createdAt: new Date(Date.now() + index),
953
- // use Date.now() + index to make sure every message is atleast one millisecond apart
954
- toolCallIds: toolCallIds?.length ? toolCallIds : void 0,
955
- toolCallArgs: toolCallArgs?.length ? toolCallArgs : void 0,
956
- toolNames: toolNames?.length ? toolNames : void 0,
957
- type
958
- };
959
- })
960
- });
961
- }
962
- }
963
- } catch (err) {
964
- this.logger.error(`[Agent:${this.name}] - Failed to save assistant response`, {
965
- error: err,
966
- runId
967
- });
968
- }
969
- }
970
- sanitizeResponseMessages(messages) {
971
- let toolResultIds = [];
972
- let toolCallIds = [];
973
- for (const message of messages) {
974
- if (!Array.isArray(message.content)) continue;
975
- if (message.role === "tool") {
976
- for (const content of message.content) {
977
- if (content.type === "tool-result") {
978
- toolResultIds.push(content.toolCallId);
979
- }
980
- }
981
- } else if (message.role === "assistant" || message.role === "user") {
982
- for (const content of message.content) {
983
- if (typeof content !== `string`) {
984
- if (content.type === `tool-call`) {
985
- toolCallIds.push(content.toolCallId);
986
- }
987
- }
988
- }
989
- }
990
- }
991
- const messagesBySanitizedContent = messages.map(message => {
992
- if (message.role !== "assistant" && message.role !== `tool` && message.role !== `user`) return message;
993
- if (!message.content || typeof message.content === "string" || typeof message.content === "number") {
994
- return message;
995
- }
996
- const sanitizedContent = message.content.filter(content => {
997
- if (content.type === `tool-call`) {
998
- return toolResultIds.includes(content.toolCallId);
999
- }
1000
- if (content.type === `text`) {
1001
- return content.text.trim() !== ``;
1002
- }
1003
- if (content.type === `tool-result`) {
1004
- return toolCallIds.includes(content.toolCallId);
1005
- }
1006
- return true;
1007
- });
1008
- return {
1009
- ...message,
1010
- content: sanitizedContent
1011
- };
1012
- });
1013
- return messagesBySanitizedContent.filter(message => {
1014
- if (typeof message.content === `string`) {
1015
- return message.content !== "";
1016
- }
1017
- if (Array.isArray(message.content)) {
1018
- return message.content.length && message.content.every(c => {
1019
- if (c.type === `text`) {
1020
- return c.text && c.text !== "";
1021
- }
1022
- return true;
1023
- });
1024
- }
1025
- return true;
1026
- });
1027
- }
1028
- convertTools({
1029
- toolsets,
1030
- threadId,
1031
- resourceId,
1032
- runId
1033
- }) {
1034
- this.logger.debug(`[Agents:${this.name}] - Assigning tools`, {
1035
- runId,
1036
- threadId,
1037
- resourceId
1038
- });
1039
- const memory = this.getMemory();
1040
- const memoryTools = memory?.getTools?.();
1041
- let mastraProxy = void 0;
1042
- const logger = this.logger;
1043
- if (this.#mastra) {
1044
- mastraProxy = chunk5FAJ6HUC_cjs.createMastraProxy({
1045
- mastra: this.#mastra,
1046
- logger
1047
- });
1048
- }
1049
- const converted = Object.entries(this.tools || {}).reduce((memo, value) => {
1050
- const k = value[0];
1051
- const tool = this.tools[k];
1052
- if (tool) {
1053
- const options = {
1054
- name: k,
1055
- runId,
1056
- threadId,
1057
- resourceId,
1058
- logger: this.logger,
1059
- mastra: mastraProxy,
1060
- memory,
1061
- agentName: this.name
1062
- };
1063
- memo[k] = chunk5FAJ6HUC_cjs.makeCoreTool(tool, options);
1064
- }
1065
- return memo;
1066
- }, {});
1067
- const convertedMemoryTools = memoryTools ? Object.entries(memoryTools).reduce((memo, [k, tool]) => {
1068
- memo[k] = {
1069
- description: tool.description,
1070
- parameters: tool.parameters,
1071
- execute: typeof tool?.execute === "function" ? async (args, options) => {
1072
- try {
1073
- this.logger.debug(`[Agent:${this.name}] - Executing memory tool ${k}`, {
1074
- name: k,
1075
- description: tool.description,
1076
- args,
1077
- runId,
1078
- threadId,
1079
- resourceId
1080
- });
1081
- return tool?.execute?.({
1082
- context: args,
1083
- mastra: mastraProxy,
1084
- memory,
1085
- runId,
1086
- threadId,
1087
- resourceId
1088
- }, options) ?? void 0;
1089
- } catch (err) {
1090
- this.logger.error(`[Agent:${this.name}] - Failed memory tool execution`, {
1091
- error: err,
1092
- runId,
1093
- threadId,
1094
- resourceId
1095
- });
1096
- throw err;
1097
- }
1098
- } : void 0
1099
- };
1100
- return memo;
1101
- }, {}) : {};
1102
- const toolsFromToolsetsConverted = {
1103
- ...converted,
1104
- ...convertedMemoryTools
1105
- };
1106
- const toolsFromToolsets = Object.values(toolsets || {});
1107
- if (toolsFromToolsets.length > 0) {
1108
- this.logger.debug(`[Agent:${this.name}] - Adding tools from toolsets ${Object.keys(toolsets || {}).join(", ")}`, {
1109
- runId
1110
- });
1111
- toolsFromToolsets.forEach(toolset => {
1112
- Object.entries(toolset).forEach(([toolName, tool]) => {
1113
- const toolObj = tool;
1114
- const options = {
1115
- name: toolName,
1116
- runId,
1117
- threadId,
1118
- resourceId,
1119
- logger: this.logger,
1120
- agentName: this.name
1121
- };
1122
- toolsFromToolsetsConverted[toolName] = chunk5FAJ6HUC_cjs.makeCoreTool(toolObj, options, "toolset");
1123
- });
1124
- });
1125
- }
1126
- return toolsFromToolsetsConverted;
1127
- }
1128
- async preExecute({
1129
- resourceId,
1130
- runId,
1131
- threadId,
1132
- memoryConfig,
1133
- messages
1134
- }) {
1135
- let coreMessages = [];
1136
- let threadIdToUse = threadId;
1137
- this.logger.debug(`Saving user messages in memory for agent ${this.name}`, {
1138
- runId
1139
- });
1140
- const saveMessageResponse = await this.fetchMemory({
1141
- threadId,
1142
- resourceId,
1143
- userMessages: messages,
1144
- memoryConfig
1145
- });
1146
- coreMessages = saveMessageResponse.messages;
1147
- threadIdToUse = saveMessageResponse.threadId;
1148
- return {
1149
- coreMessages,
1150
- threadIdToUse
1151
- };
1152
- }
1153
- __primitive({
1154
- instructions,
1155
- messages,
1156
- context,
1157
- threadId,
1158
- memoryConfig,
1159
- resourceId,
1160
- runId,
1161
- toolsets
1162
- }) {
1163
- return {
1164
- before: async () => {
1165
- if (process.env.NODE_ENV !== "test") {
1166
- this.logger.debug(`[Agents:${this.name}] - Starting generation`, {
1167
- runId
1168
- });
1169
- }
1170
- const systemMessage = {
1171
- role: "system",
1172
- content: instructions || `${this.instructions}.`
1173
- };
1174
- let coreMessages = messages;
1175
- let threadIdToUse = threadId;
1176
- const memory = this.getMemory();
1177
- if (threadId && memory && !resourceId) {
1178
- throw new Error(`A resourceId must be provided when passing a threadId and using Memory. Saw threadId ${threadId} but resourceId is ${resourceId}`);
1179
- }
1180
- if (memory && resourceId) {
1181
- this.logger.debug(`[Agent:${this.name}] - Memory persistence enabled: store=${this.getMemory()?.constructor.name}, resourceId=${resourceId}`, {
1182
- runId,
1183
- resourceId,
1184
- threadId: threadIdToUse,
1185
- memoryStore: this.getMemory()?.constructor.name
1186
- });
1187
- let thread = threadIdToUse ? await memory.getThreadById({
1188
- threadId: threadIdToUse
1189
- }) : void 0;
1190
- if (!thread) {
1191
- thread = await memory.createThread({
1192
- threadId: threadIdToUse,
1193
- resourceId,
1194
- memoryConfig
1195
- });
1196
- }
1197
- threadIdToUse = thread.id;
1198
- const preExecuteResult = await this.preExecute({
1199
- resourceId,
1200
- runId,
1201
- threadId: threadIdToUse,
1202
- memoryConfig,
1203
- messages
1204
- });
1205
- coreMessages = preExecuteResult.coreMessages;
1206
- threadIdToUse = preExecuteResult.threadIdToUse;
1207
- }
1208
- let convertedTools;
1209
- if (toolsets && Object.keys(toolsets || {}).length > 0 || this.getMemory() && resourceId) {
1210
- const reasons = [];
1211
- if (toolsets && Object.keys(toolsets || {}).length > 0) {
1212
- reasons.push(`toolsets present (${Object.keys(toolsets || {}).length} tools)`);
1213
- }
1214
- if (this.getMemory() && resourceId) {
1215
- reasons.push("memory and resourceId available");
1216
- }
1217
- this.logger.debug(`[Agent:${this.name}] - Enhancing tools: ${reasons.join(", ")}`, {
1218
- runId,
1219
- toolsets: toolsets ? Object.keys(toolsets) : void 0,
1220
- hasMemory: !!this.getMemory(),
1221
- hasResourceId: !!resourceId
1222
- });
1223
- convertedTools = this.convertTools({
1224
- toolsets,
1225
- threadId: threadIdToUse,
1226
- resourceId,
1227
- runId
1228
- });
1229
- }
1230
- const messageObjects = [systemMessage, ...(context || []), ...coreMessages];
1231
- return {
1232
- messageObjects,
1233
- convertedTools,
1234
- threadId: threadIdToUse
1235
- };
1236
- },
1237
- after: async ({
1238
- result,
1239
- threadId: threadId2,
1240
- memoryConfig: memoryConfig2,
1241
- outputText,
1242
- runId: runId2
1243
- }) => {
1244
- const resToLog = {
1245
- text: result?.text,
1246
- object: result?.object,
1247
- toolResults: result?.toolResults,
1248
- toolCalls: result?.toolCalls,
1249
- usage: result?.usage,
1250
- steps: result?.steps?.map(s => {
1251
- return {
1252
- stepType: s?.stepType,
1253
- text: result?.text,
1254
- object: result?.object,
1255
- toolResults: result?.toolResults,
1256
- toolCalls: result?.toolCalls,
1257
- usage: result?.usage
1258
- };
1259
- })
1260
- };
1261
- this.logger.debug(`[Agent:${this.name}] - Post processing LLM response`, {
1262
- runId: runId2,
1263
- result: resToLog,
1264
- threadId: threadId2
1265
- });
1266
- const memory = this.getMemory();
1267
- const thread = threadId2 ? await memory?.getThreadById({
1268
- threadId: threadId2
1269
- }) : void 0;
1270
- if (memory && resourceId && thread) {
1271
- try {
1272
- const userMessage = this.getMostRecentUserMessage(messages);
1273
- const newMessages = userMessage ? [userMessage] : messages;
1274
- const threadMessages = newMessages.map(u => {
1275
- return {
1276
- id: this.getMemory()?.generateId(),
1277
- createdAt: /* @__PURE__ */new Date(),
1278
- threadId: thread.id,
1279
- ...u,
1280
- content: u.content,
1281
- role: u.role,
1282
- type: "text"
1283
- };
1284
- });
1285
- await Promise.all([(async () => {
1286
- await memory.saveMessages({
1287
- messages: threadMessages,
1288
- memoryConfig: memoryConfig2
1289
- });
1290
- await this.saveResponse({
1291
- result,
1292
- threadId: threadId2,
1293
- resourceId,
1294
- memoryConfig: memoryConfig2,
1295
- runId: runId2
1296
- });
1297
- })(), (async () => {
1298
- if (!thread.title?.startsWith("New Thread")) {
1299
- return;
1300
- }
1301
- const config = memory.getMergedThreadConfig(memoryConfig2);
1302
- const title = config?.threads?.generateTitle ? await this.genTitle(userMessage) : void 0;
1303
- if (!title) {
1304
- return;
1305
- }
1306
- return memory.createThread({
1307
- threadId: thread.id,
1308
- resourceId,
1309
- memoryConfig: memoryConfig2,
1310
- title
1311
- });
1312
- })()]);
1313
- } catch (e) {
1314
- this.logger.error("Error saving response", {
1315
- error: e,
1316
- runId: runId2,
1317
- result: resToLog,
1318
- threadId: threadId2
1319
- });
1320
- }
1321
- }
1322
- if (Object.keys(this.evals || {}).length > 0) {
1323
- const input = messages.map(message => message.content).join("\n");
1324
- const runIdToUse = runId2 || crypto.randomUUID();
1325
- for (const metric of Object.values(this.evals || {})) {
1326
- chunkST5RMVLG_cjs.executeHook("onGeneration" /* ON_GENERATION */, {
1327
- input,
1328
- output: outputText,
1329
- runId: runIdToUse,
1330
- metric,
1331
- agentName: this.name,
1332
- instructions: instructions || this.instructions
1333
- });
1334
- }
1335
- }
1336
- }
1337
- };
1338
- }
1339
- async generate(messages, {
1340
- instructions,
1341
- context,
1342
- threadId: threadIdInFn,
1343
- memoryOptions,
1344
- resourceId,
1345
- maxSteps = 5,
1346
- onStepFinish,
1347
- runId,
1348
- output,
1349
- toolsets,
1350
- temperature,
1351
- toolChoice = "auto",
1352
- experimental_output,
1353
- telemetry,
1354
- ...rest
1355
- } = {}) {
1356
- let messagesToUse = [];
1357
- if (typeof messages === `string`) {
1358
- messagesToUse = [{
1359
- role: "user",
1360
- content: messages
1361
- }];
1362
- } else if (Array.isArray(messages)) {
1363
- messagesToUse = messages.map(message => {
1364
- if (typeof message === `string`) {
1365
- return {
1366
- role: "user",
1367
- content: message
1368
- };
1369
- }
1370
- return message;
1371
- });
1372
- } else {
1373
- messagesToUse = [messages];
1374
- }
1375
- const runIdToUse = runId || crypto$1.randomUUID();
1376
- const {
1377
- before,
1378
- after
1379
- } = this.__primitive({
1380
- instructions,
1381
- messages: messagesToUse,
1382
- context,
1383
- threadId: threadIdInFn,
1384
- memoryConfig: memoryOptions,
1385
- resourceId,
1386
- runId: runIdToUse,
1387
- toolsets
1388
- });
1389
- const {
1390
- threadId,
1391
- messageObjects,
1392
- convertedTools
1393
- } = await before();
1394
- if (!output && experimental_output) {
1395
- const result2 = await this.llm.__text({
1396
- messages: messageObjects,
1397
- tools: this.tools,
1398
- convertedTools,
1399
- onStepFinish: result3 => {
1400
- void onStepFinish?.(result3);
1401
- },
1402
- maxSteps: maxSteps || 5,
1403
- runId: runIdToUse,
1404
- temperature,
1405
- toolChoice: toolChoice || "auto",
1406
- experimental_output,
1407
- threadId,
1408
- resourceId,
1409
- memory: this.getMemory(),
1410
- ...rest
1411
- });
1412
- const outputText2 = result2.text;
1413
- await after({
1414
- result: result2,
1415
- threadId,
1416
- memoryConfig: memoryOptions,
1417
- outputText: outputText2,
1418
- runId: runIdToUse
1419
- });
1420
- const newResult = result2;
1421
- newResult.object = result2.experimental_output;
1422
- return newResult;
1423
- }
1424
- if (!output) {
1425
- const result2 = await this.llm.__text({
1426
- messages: messageObjects,
1427
- tools: this.tools,
1428
- convertedTools,
1429
- onStepFinish: result3 => {
1430
- void onStepFinish?.(result3);
1431
- },
1432
- maxSteps,
1433
- runId: runIdToUse,
1434
- temperature,
1435
- toolChoice,
1436
- telemetry,
1437
- threadId,
1438
- resourceId,
1439
- memory: this.getMemory(),
1440
- ...rest
1441
- });
1442
- const outputText2 = result2.text;
1443
- await after({
1444
- result: result2,
1445
- threadId,
1446
- memoryConfig: memoryOptions,
1447
- outputText: outputText2,
1448
- runId: runIdToUse
1449
- });
1450
- return result2;
1451
- }
1452
- const result = await this.llm.__textObject({
1453
- messages: messageObjects,
1454
- tools: this.tools,
1455
- structuredOutput: output,
1456
- convertedTools,
1457
- onStepFinish: result2 => {
1458
- void onStepFinish?.(result2);
1459
- },
1460
- maxSteps,
1461
- runId: runIdToUse,
1462
- temperature,
1463
- toolChoice,
1464
- telemetry,
1465
- memory: this.getMemory(),
1466
- ...rest
1467
- });
1468
- const outputText = JSON.stringify(result.object);
1469
- await after({
1470
- result,
1471
- threadId,
1472
- memoryConfig: memoryOptions,
1473
- outputText,
1474
- runId: runIdToUse
1475
- });
1476
- return result;
1477
- }
1478
- async stream(messages, {
1479
- instructions,
1480
- context,
1481
- threadId: threadIdInFn,
1482
- memoryOptions,
1483
- resourceId,
1484
- maxSteps = 5,
1485
- onFinish,
1486
- onStepFinish,
1487
- runId,
1488
- toolsets,
1489
- output,
1490
- temperature,
1491
- toolChoice = "auto",
1492
- experimental_output,
1493
- telemetry,
1494
- ...rest
1495
- } = {}) {
1496
- const runIdToUse = runId || crypto$1.randomUUID();
1497
- let messagesToUse = [];
1498
- if (typeof messages === `string`) {
1499
- messagesToUse = [{
1500
- role: "user",
1501
- content: messages
1502
- }];
1503
- } else {
1504
- messagesToUse = messages.map(message => {
1505
- if (typeof message === `string`) {
1506
- return {
1507
- role: "user",
1508
- content: message
1509
- };
1510
- }
1511
- return message;
1512
- });
1513
- }
1514
- const {
1515
- before,
1516
- after
1517
- } = this.__primitive({
1518
- instructions,
1519
- messages: messagesToUse,
1520
- context,
1521
- threadId: threadIdInFn,
1522
- memoryConfig: memoryOptions,
1523
- resourceId,
1524
- runId: runIdToUse,
1525
- toolsets
1526
- });
1527
- const {
1528
- threadId,
1529
- messageObjects,
1530
- convertedTools
1531
- } = await before();
1532
- if (!output && experimental_output) {
1533
- this.logger.debug(`Starting agent ${this.name} llm stream call`, {
1534
- runId
1535
- });
1536
- const streamResult = await this.llm.__stream({
1537
- messages: messageObjects,
1538
- temperature,
1539
- tools: this.tools,
1540
- convertedTools,
1541
- onStepFinish: result => {
1542
- void onStepFinish?.(result);
1543
- },
1544
- onFinish: async result => {
1545
- try {
1546
- const outputText = result.text;
1547
- await after({
1548
- result,
1549
- threadId,
1550
- memoryConfig: memoryOptions,
1551
- outputText,
1552
- runId: runIdToUse
1553
- });
1554
- } catch (e) {
1555
- this.logger.error("Error saving memory on finish", {
1556
- error: e,
1557
- runId
1558
- });
1559
- }
1560
- void onFinish?.(result);
1561
- },
1562
- maxSteps,
1563
- runId: runIdToUse,
1564
- toolChoice,
1565
- experimental_output,
1566
- memory: this.getMemory(),
1567
- ...rest
1568
- });
1569
- const newStreamResult = streamResult;
1570
- newStreamResult.partialObjectStream = streamResult.experimental_partialOutputStream;
1571
- return newStreamResult;
1572
- } else if (!output) {
1573
- this.logger.debug(`Starting agent ${this.name} llm stream call`, {
1574
- runId
1575
- });
1576
- return this.llm.__stream({
1577
- messages: messageObjects,
1578
- temperature,
1579
- tools: this.tools,
1580
- convertedTools,
1581
- onStepFinish: result => {
1582
- void onStepFinish?.(result);
1583
- },
1584
- onFinish: async result => {
1585
- try {
1586
- const outputText = result.text;
1587
- await after({
1588
- result,
1589
- threadId,
1590
- memoryConfig: memoryOptions,
1591
- outputText,
1592
- runId: runIdToUse
1593
- });
1594
- } catch (e) {
1595
- this.logger.error("Error saving memory on finish", {
1596
- error: e,
1597
- runId
1598
- });
1599
- }
1600
- void onFinish?.(result);
1601
- },
1602
- maxSteps,
1603
- runId: runIdToUse,
1604
- toolChoice,
1605
- telemetry,
1606
- memory: this.getMemory(),
1607
- ...rest
1608
- });
1609
- }
1610
- this.logger.debug(`Starting agent ${this.name} llm streamObject call`, {
1611
- runId
1612
- });
1613
- return this.llm.__streamObject({
1614
- messages: messageObjects,
1615
- tools: this.tools,
1616
- temperature,
1617
- structuredOutput: output,
1618
- convertedTools,
1619
- onStepFinish: result => {
1620
- void onStepFinish?.(result);
1621
- },
1622
- onFinish: async result => {
1623
- try {
1624
- const outputText = JSON.stringify(result.object);
1625
- await after({
1626
- result,
1627
- threadId,
1628
- memoryConfig: memoryOptions,
1629
- outputText,
1630
- runId: runIdToUse
1631
- });
1632
- } catch (e) {
1633
- this.logger.error("Error saving memory on finish", {
1634
- error: e,
1635
- runId
1636
- });
1637
- }
1638
- void onFinish?.(result);
1639
- },
1640
- runId: runIdToUse,
1641
- toolChoice,
1642
- telemetry,
1643
- memory: this.getMemory(),
1644
- ...rest
1645
- });
1646
- }
1647
- /**
1648
- * Convert text to speech using the configured voice provider
1649
- * @param input Text or text stream to convert to speech
1650
- * @param options Speech options including speaker and provider-specific options
1651
- * @returns Audio stream
1652
- * @deprecated Use agent.voice.speak() instead
1653
- */
1654
- async speak(input, options) {
1655
- if (!this.voice) {
1656
- throw new Error("No voice provider configured");
1657
- }
1658
- this.logger.warn("Warning: agent.speak() is deprecated. Please use agent.voice.speak() instead.");
1659
- try {
1660
- return this.voice.speak(input, options);
1661
- } catch (e) {
1662
- this.logger.error("Error during agent speak", {
1663
- error: e
1664
- });
1665
- throw e;
1666
- }
1667
- }
1668
- /**
1669
- * Convert speech to text using the configured voice provider
1670
- * @param audioStream Audio stream to transcribe
1671
- * @param options Provider-specific transcription options
1672
- * @returns Text or text stream
1673
- * @deprecated Use agent.voice.listen() instead
1674
- */
1675
- async listen(audioStream, options) {
1676
- if (!this.voice) {
1677
- throw new Error("No voice provider configured");
1678
- }
1679
- this.logger.warn("Warning: agent.listen() is deprecated. Please use agent.voice.listen() instead");
1680
- try {
1681
- return this.voice.listen(audioStream, options);
1682
- } catch (e) {
1683
- this.logger.error("Error during agent listen", {
1684
- error: e
1685
- });
1686
- throw e;
1687
- }
1688
- }
1689
- /**
1690
- * Get a list of available speakers from the configured voice provider
1691
- * @throws {Error} If no voice provider is configured
1692
- * @returns {Promise<Array<{voiceId: string}>>} List of available speakers
1693
- * @deprecated Use agent.voice.getSpeakers() instead
1694
- */
1695
- async getSpeakers() {
1696
- if (!this.voice) {
1697
- throw new Error("No voice provider configured");
1698
- }
1699
- this.logger.warn("Warning: agent.getSpeakers() is deprecated. Please use agent.voice.getSpeakers() instead.");
1700
- try {
1701
- return await this.voice.getSpeakers();
1702
- } catch (e) {
1703
- this.logger.error("Error during agent getSpeakers", {
1704
- error: e
1705
- });
1706
- throw e;
1707
- }
1708
- }
1709
- };
1710
- exports.Agent = /*@__PURE__*/(_ => {
1711
- _init = chunkRWTSGWWL_cjs.__decoratorStart(_a);
1712
- exports.Agent = chunkRWTSGWWL_cjs.__decorateElement(_init, 0, "Agent", _Agent_decorators, exports.Agent);
1713
- chunkRWTSGWWL_cjs.__runInitializers(_init, 1, exports.Agent);
1714
- return exports.Agent;
1715
- })();