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