@mastra/core 0.2.0 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/agent/index.d.ts +1 -1
- package/dist/agent/index.js +1 -15
- package/dist/{base-Bpb7Dmwe.d.ts → base-C7R9FwZ6.d.ts} +7 -7
- package/dist/base.js +0 -2
- package/dist/bundler/index.js +0 -3
- package/dist/{chunk-FGZVE4CM.js → chunk-4ZRHVG25.js} +2 -4
- package/dist/chunk-55GTEVHJ.js +1 -1
- package/dist/{chunk-U6J2FOU4.js → chunk-5HXXWLRX.js} +103 -101
- package/dist/{chunk-SB37QG7O.js → chunk-6TCTOQ3G.js} +38 -37
- package/dist/{chunk-OJ26F3J4.js → chunk-AN562ICT.js} +2 -0
- package/dist/chunk-AWEACB2T.js +1 -1
- package/dist/{chunk-MLWGYRJR.js → chunk-BB4KXGBU.js} +1 -5
- package/dist/chunk-C6A6W6XS.js +48 -20
- package/dist/{chunk-K4DFI76V.js → chunk-CUIUUULJ.js} +313 -204
- package/dist/{chunk-SDBM53G4.js → chunk-HT63PEVD.js} +10 -5
- package/dist/{chunk-VB7CO5ND.js → chunk-NUDAZEOG.js} +6 -2
- package/dist/{chunk-MEISIZMP.js → chunk-PNZK456O.js} +1 -1
- package/dist/chunk-SIFBBGY6.js +2 -2
- package/dist/{chunk-2JL6DQMZ.js → chunk-V4WSAQOP.js} +14 -14
- package/dist/{chunk-65VPTVVP.js → chunk-V5UMPUKC.js} +2 -2
- package/dist/{chunk-J3W3IHDO.js → chunk-VE4JJJSW.js} +43 -21
- package/dist/{chunk-3HBFW3Q7.js → chunk-ZINPRHAN.js} +0 -2
- package/dist/chunk-ZJOXJFJI.js +1 -2
- package/dist/deployer/index.d.ts +1 -1
- package/dist/deployer/index.js +0 -4
- package/dist/eval/index.d.ts +1 -1
- package/dist/eval/index.js +1 -3
- package/dist/filter/index.js +0 -1
- package/dist/hooks/index.js +1 -2
- package/dist/index.d.ts +5 -5
- package/dist/index.js +18 -22
- package/dist/integration/index.d.ts +2 -2
- package/dist/integration/index.js +1 -6
- package/dist/llm/index.d.ts +1 -1
- package/dist/llm/index.js +1 -1
- package/dist/logger/index.js +0 -1
- package/dist/mastra/index.d.ts +15 -14
- package/dist/mastra/index.js +1 -20
- package/dist/memory/index.d.ts +1 -1
- package/dist/memory/index.js +1 -11
- package/dist/relevance/index.js +1 -16
- package/dist/storage/index.d.ts +2 -2
- package/dist/storage/index.js +2 -7
- package/dist/telemetry/index.d.ts +1 -1
- package/dist/telemetry/index.js +1 -3
- package/dist/telemetry/otel-vendor.js +0 -1
- package/dist/tools/index.d.ts +2 -2
- package/dist/tools/index.js +1 -2
- package/dist/tts/index.js +1 -6
- package/dist/utils.js +0 -1
- package/dist/vector/index.d.ts +1 -1
- package/dist/vector/index.js +0 -3
- package/dist/vector/libsql/index.js +1 -6
- package/dist/{workflow-Cy8UTGCt.d.ts → workflow-7xHmmFH5.d.ts} +1 -1
- package/dist/workflows/index.d.ts +3 -3
- package/dist/workflows/index.js +1 -4
- package/package.json +10 -10
- package/dist/chunk-2SAHBQEF.js +0 -3
- /package/dist/{chunk-VDOJTUYY.js → chunk-ZDWFBE5L.js} +0 -0
|
@@ -1,19 +1,22 @@
|
|
|
1
|
-
import { InstrumentClass } from './chunk-
|
|
1
|
+
import { InstrumentClass } from './chunk-4ZRHVG25.js';
|
|
2
2
|
import { delay } from './chunk-55GTEVHJ.js';
|
|
3
3
|
import { MastraBase } from './chunk-AWEACB2T.js';
|
|
4
4
|
import { RegisteredLogger, LogLevel } from './chunk-O2VP5JBC.js';
|
|
5
|
-
import { executeHook } from './chunk-
|
|
5
|
+
import { executeHook } from './chunk-BB4KXGBU.js';
|
|
6
6
|
import { __decoratorStart, __decorateElement, __runInitializers } from './chunk-C6A6W6XS.js';
|
|
7
|
-
import { generateText, jsonSchema, generateObject, streamText, streamObject } from 'ai';
|
|
8
7
|
import { randomUUID } from 'crypto';
|
|
9
|
-
import 'json-schema';
|
|
10
8
|
import { z } from 'zod';
|
|
9
|
+
import { generateText, jsonSchema, generateObject, streamText, streamObject } from 'ai';
|
|
11
10
|
|
|
11
|
+
// src/llm/model/base.ts
|
|
12
12
|
var MastraLLMBase = class extends MastraBase {
|
|
13
13
|
// @ts-ignore
|
|
14
14
|
#mastra;
|
|
15
15
|
#model;
|
|
16
|
-
constructor({
|
|
16
|
+
constructor({
|
|
17
|
+
name,
|
|
18
|
+
model
|
|
19
|
+
}) {
|
|
17
20
|
super({
|
|
18
21
|
component: RegisteredLogger.LLM,
|
|
19
22
|
name
|
|
@@ -31,7 +34,7 @@ var MastraLLMBase = class extends MastraBase {
|
|
|
31
34
|
}
|
|
32
35
|
convertToMessages(messages) {
|
|
33
36
|
if (Array.isArray(messages)) {
|
|
34
|
-
return messages.map(
|
|
37
|
+
return messages.map(m => {
|
|
35
38
|
if (typeof m === "string") {
|
|
36
39
|
return {
|
|
37
40
|
role: "user",
|
|
@@ -41,12 +44,10 @@ var MastraLLMBase = class extends MastraBase {
|
|
|
41
44
|
return m;
|
|
42
45
|
});
|
|
43
46
|
}
|
|
44
|
-
return [
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
}
|
|
49
|
-
];
|
|
47
|
+
return [{
|
|
48
|
+
role: "user",
|
|
49
|
+
content: messages
|
|
50
|
+
}];
|
|
50
51
|
}
|
|
51
52
|
__registerPrimitives(p) {
|
|
52
53
|
if (p.telemetry) {
|
|
@@ -58,35 +59,55 @@ var MastraLLMBase = class extends MastraBase {
|
|
|
58
59
|
this.#mastra = p;
|
|
59
60
|
}
|
|
60
61
|
async __text(input) {
|
|
61
|
-
this.logger.debug(`[LLMs:${this.name}] Generating text.`, {
|
|
62
|
+
this.logger.debug(`[LLMs:${this.name}] Generating text.`, {
|
|
63
|
+
input
|
|
64
|
+
});
|
|
62
65
|
throw new Error("Method not implemented.");
|
|
63
66
|
}
|
|
64
67
|
async __textObject(input) {
|
|
65
|
-
this.logger.debug(`[LLMs:${this.name}] Generating object.`, {
|
|
68
|
+
this.logger.debug(`[LLMs:${this.name}] Generating object.`, {
|
|
69
|
+
input
|
|
70
|
+
});
|
|
66
71
|
throw new Error("Method not implemented.");
|
|
67
72
|
}
|
|
68
73
|
async generate(messages, options = {}) {
|
|
69
|
-
this.logger.debug(`[LLMs:${this.name}] Generating text.`, {
|
|
74
|
+
this.logger.debug(`[LLMs:${this.name}] Generating text.`, {
|
|
75
|
+
messages,
|
|
76
|
+
options
|
|
77
|
+
});
|
|
70
78
|
throw new Error("Method not implemented.");
|
|
71
79
|
}
|
|
72
80
|
async __stream(input) {
|
|
73
|
-
this.logger.debug(`[LLMs:${this.name}] Streaming text.`, {
|
|
81
|
+
this.logger.debug(`[LLMs:${this.name}] Streaming text.`, {
|
|
82
|
+
input
|
|
83
|
+
});
|
|
74
84
|
throw new Error("Method not implemented.");
|
|
75
85
|
}
|
|
76
86
|
async __streamObject(input) {
|
|
77
|
-
this.logger.debug(`[LLMs:${this.name}] Streaming object.`, {
|
|
87
|
+
this.logger.debug(`[LLMs:${this.name}] Streaming object.`, {
|
|
88
|
+
input
|
|
89
|
+
});
|
|
78
90
|
throw new Error("Method not implemented.");
|
|
79
91
|
}
|
|
80
92
|
async stream(messages, options = {}) {
|
|
81
|
-
this.logger.debug(`[LLMs:${this.name}] Streaming text.`, {
|
|
93
|
+
this.logger.debug(`[LLMs:${this.name}] Streaming text.`, {
|
|
94
|
+
messages,
|
|
95
|
+
options
|
|
96
|
+
});
|
|
82
97
|
throw new Error("Method not implemented.");
|
|
83
98
|
}
|
|
84
99
|
};
|
|
85
100
|
var MastraLLM = class extends MastraLLMBase {
|
|
86
101
|
#model;
|
|
87
102
|
#mastra;
|
|
88
|
-
constructor({
|
|
89
|
-
|
|
103
|
+
constructor({
|
|
104
|
+
model,
|
|
105
|
+
mastra
|
|
106
|
+
}) {
|
|
107
|
+
super({
|
|
108
|
+
name: "aisdk",
|
|
109
|
+
model
|
|
110
|
+
});
|
|
90
111
|
this.#model = model;
|
|
91
112
|
if (mastra) {
|
|
92
113
|
this.#mastra = mastra;
|
|
@@ -113,42 +134,42 @@ var MastraLLM = class extends MastraLLMBase {
|
|
|
113
134
|
getModel() {
|
|
114
135
|
return this.#model;
|
|
115
136
|
}
|
|
116
|
-
convertTools({
|
|
137
|
+
convertTools({
|
|
138
|
+
tools,
|
|
139
|
+
runId
|
|
140
|
+
} = {}) {
|
|
117
141
|
this.logger.debug("Starting tool conversion for LLM");
|
|
118
|
-
const converted = Object.entries(tools || {}).reduce(
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
throw error;
|
|
144
|
-
}
|
|
142
|
+
const converted = Object.entries(tools || {}).reduce((memo, value) => {
|
|
143
|
+
const k = value[0];
|
|
144
|
+
const tool = value[1];
|
|
145
|
+
if (tool) {
|
|
146
|
+
memo[k] = {
|
|
147
|
+
description: tool.description,
|
|
148
|
+
parameters: tool.inputSchema,
|
|
149
|
+
execute: async props => {
|
|
150
|
+
try {
|
|
151
|
+
this.logger.debug("Executing tool", {
|
|
152
|
+
tool: k,
|
|
153
|
+
props
|
|
154
|
+
});
|
|
155
|
+
return tool.execute({
|
|
156
|
+
context: props,
|
|
157
|
+
mastra: this.#mastra,
|
|
158
|
+
runId
|
|
159
|
+
});
|
|
160
|
+
} catch (error) {
|
|
161
|
+
this.logger.error("Error executing tool", {
|
|
162
|
+
tool: k,
|
|
163
|
+
props,
|
|
164
|
+
error
|
|
165
|
+
});
|
|
166
|
+
throw error;
|
|
145
167
|
}
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
);
|
|
168
|
+
}
|
|
169
|
+
};
|
|
170
|
+
}
|
|
171
|
+
return memo;
|
|
172
|
+
}, {});
|
|
152
173
|
this.logger.debug(`Converted tools for LLM`);
|
|
153
174
|
return converted;
|
|
154
175
|
}
|
|
@@ -169,7 +190,10 @@ var MastraLLM = class extends MastraLLMBase {
|
|
|
169
190
|
maxSteps,
|
|
170
191
|
tools: Object.keys(tools || convertedTools || {})
|
|
171
192
|
});
|
|
172
|
-
const finalTools = convertedTools || this.convertTools({
|
|
193
|
+
const finalTools = convertedTools || this.convertTools({
|
|
194
|
+
tools,
|
|
195
|
+
runId
|
|
196
|
+
});
|
|
173
197
|
const argsForExecute = {
|
|
174
198
|
model,
|
|
175
199
|
temperature,
|
|
@@ -178,7 +202,7 @@ var MastraLLM = class extends MastraLLMBase {
|
|
|
178
202
|
},
|
|
179
203
|
toolChoice,
|
|
180
204
|
maxSteps,
|
|
181
|
-
onStepFinish: async
|
|
205
|
+
onStepFinish: async props => {
|
|
182
206
|
onStepFinish?.(JSON.stringify(props, null, 2));
|
|
183
207
|
this.logger.debug("[LLM] - Step Change:", {
|
|
184
208
|
text: props?.text,
|
|
@@ -212,8 +236,13 @@ var MastraLLM = class extends MastraLLMBase {
|
|
|
212
236
|
toolChoice = "auto"
|
|
213
237
|
}) {
|
|
214
238
|
const model = this.#model;
|
|
215
|
-
this.logger.debug(`[LLM] - Generating a text object`, {
|
|
216
|
-
|
|
239
|
+
this.logger.debug(`[LLM] - Generating a text object`, {
|
|
240
|
+
runId
|
|
241
|
+
});
|
|
242
|
+
const finalTools = convertedTools || this.convertTools({
|
|
243
|
+
tools,
|
|
244
|
+
runId
|
|
245
|
+
});
|
|
217
246
|
const argsForExecute = {
|
|
218
247
|
model,
|
|
219
248
|
temperature,
|
|
@@ -222,7 +251,7 @@ var MastraLLM = class extends MastraLLMBase {
|
|
|
222
251
|
},
|
|
223
252
|
maxSteps,
|
|
224
253
|
toolChoice,
|
|
225
|
-
onStepFinish: async
|
|
254
|
+
onStepFinish: async props => {
|
|
226
255
|
onStepFinish?.(JSON.stringify(props, null, 2));
|
|
227
256
|
this.logger.debug("[LLM] - Step Change:", {
|
|
228
257
|
text: props?.text,
|
|
@@ -233,7 +262,9 @@ var MastraLLM = class extends MastraLLMBase {
|
|
|
233
262
|
runId
|
|
234
263
|
});
|
|
235
264
|
if (props?.response?.headers?.["x-ratelimit-remaining-tokens"] && parseInt(props?.response?.headers?.["x-ratelimit-remaining-tokens"], 10) < 2e3) {
|
|
236
|
-
this.logger.warn("Rate limit approaching, waiting 10 seconds", {
|
|
265
|
+
this.logger.warn("Rate limit approaching, waiting 10 seconds", {
|
|
266
|
+
runId
|
|
267
|
+
});
|
|
237
268
|
await delay(10 * 1e3);
|
|
238
269
|
}
|
|
239
270
|
}
|
|
@@ -275,7 +306,10 @@ var MastraLLM = class extends MastraLLMBase {
|
|
|
275
306
|
maxSteps,
|
|
276
307
|
tools: Object.keys(tools || convertedTools || {})
|
|
277
308
|
});
|
|
278
|
-
const finalTools = convertedTools || this.convertTools({
|
|
309
|
+
const finalTools = convertedTools || this.convertTools({
|
|
310
|
+
tools,
|
|
311
|
+
runId
|
|
312
|
+
});
|
|
279
313
|
const argsForExecute = {
|
|
280
314
|
model,
|
|
281
315
|
temperature,
|
|
@@ -284,7 +318,7 @@ var MastraLLM = class extends MastraLLMBase {
|
|
|
284
318
|
},
|
|
285
319
|
maxSteps,
|
|
286
320
|
toolChoice,
|
|
287
|
-
onStepFinish: async
|
|
321
|
+
onStepFinish: async props => {
|
|
288
322
|
onStepFinish?.(JSON.stringify(props, null, 2));
|
|
289
323
|
this.logger.debug("[LLM] - Stream Step Change:", {
|
|
290
324
|
text: props?.text,
|
|
@@ -295,11 +329,13 @@ var MastraLLM = class extends MastraLLMBase {
|
|
|
295
329
|
runId
|
|
296
330
|
});
|
|
297
331
|
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", {
|
|
332
|
+
this.logger.warn("Rate limit approaching, waiting 10 seconds", {
|
|
333
|
+
runId
|
|
334
|
+
});
|
|
299
335
|
await delay(10 * 1e3);
|
|
300
336
|
}
|
|
301
337
|
},
|
|
302
|
-
onFinish: async
|
|
338
|
+
onFinish: async props => {
|
|
303
339
|
onFinish?.(JSON.stringify(props, null, 2));
|
|
304
340
|
this.logger.debug("[LLM] - Stream Finished:", {
|
|
305
341
|
text: props?.text,
|
|
@@ -336,7 +372,10 @@ var MastraLLM = class extends MastraLLMBase {
|
|
|
336
372
|
maxSteps,
|
|
337
373
|
tools: Object.keys(tools || convertedTools || {})
|
|
338
374
|
});
|
|
339
|
-
const finalTools = convertedTools || this.convertTools({
|
|
375
|
+
const finalTools = convertedTools || this.convertTools({
|
|
376
|
+
tools,
|
|
377
|
+
runId
|
|
378
|
+
});
|
|
340
379
|
const argsForExecute = {
|
|
341
380
|
model,
|
|
342
381
|
temperature,
|
|
@@ -345,7 +384,7 @@ var MastraLLM = class extends MastraLLMBase {
|
|
|
345
384
|
},
|
|
346
385
|
maxSteps,
|
|
347
386
|
toolChoice,
|
|
348
|
-
onStepFinish: async
|
|
387
|
+
onStepFinish: async props => {
|
|
349
388
|
onStepFinish?.(JSON.stringify(props, null, 2));
|
|
350
389
|
this.logger.debug("[LLM] - Stream Step Change:", {
|
|
351
390
|
text: props?.text,
|
|
@@ -356,11 +395,13 @@ var MastraLLM = class extends MastraLLMBase {
|
|
|
356
395
|
runId
|
|
357
396
|
});
|
|
358
397
|
if (props?.response?.headers?.["x-ratelimit-remaining-tokens"] && parseInt(props?.response?.headers?.["x-ratelimit-remaining-tokens"], 10) < 2e3) {
|
|
359
|
-
this.logger.warn("Rate limit approaching, waiting 10 seconds", {
|
|
398
|
+
this.logger.warn("Rate limit approaching, waiting 10 seconds", {
|
|
399
|
+
runId
|
|
400
|
+
});
|
|
360
401
|
await delay(10 * 1e3);
|
|
361
402
|
}
|
|
362
403
|
},
|
|
363
|
-
onFinish: async
|
|
404
|
+
onFinish: async props => {
|
|
364
405
|
onFinish?.(JSON.stringify(props, null, 2));
|
|
365
406
|
this.logger.debug("[LLM] - Stream Finished:", {
|
|
366
407
|
text: props?.text,
|
|
@@ -477,13 +518,17 @@ var Agent = class extends (_a = MastraBase) {
|
|
|
477
518
|
metrics;
|
|
478
519
|
evals;
|
|
479
520
|
constructor(config) {
|
|
480
|
-
super({
|
|
521
|
+
super({
|
|
522
|
+
component: RegisteredLogger.AGENT
|
|
523
|
+
});
|
|
481
524
|
this.name = config.name;
|
|
482
525
|
this.instructions = config.instructions;
|
|
483
526
|
if (!config.model) {
|
|
484
527
|
throw new Error(`LanugageModel is required to create an Agent. Please provider the 'model'.`);
|
|
485
528
|
}
|
|
486
|
-
this.llm = new MastraLLM({
|
|
529
|
+
this.llm = new MastraLLM({
|
|
530
|
+
model: config.model
|
|
531
|
+
});
|
|
487
532
|
this.tools = {};
|
|
488
533
|
this.metrics = {};
|
|
489
534
|
this.evals = {};
|
|
@@ -513,7 +558,10 @@ var Agent = class extends (_a = MastraBase) {
|
|
|
513
558
|
}
|
|
514
559
|
__updateInstructions(newInstructions) {
|
|
515
560
|
this.instructions = newInstructions;
|
|
516
|
-
this.logger.debug(`[Agents:${this.name}] Instructions updated.`, {
|
|
561
|
+
this.logger.debug(`[Agents:${this.name}] Instructions updated.`, {
|
|
562
|
+
model: this.model,
|
|
563
|
+
name: this.name
|
|
564
|
+
});
|
|
517
565
|
}
|
|
518
566
|
__registerPrimitives(p) {
|
|
519
567
|
if (p.telemetry) {
|
|
@@ -524,7 +572,10 @@ var Agent = class extends (_a = MastraBase) {
|
|
|
524
572
|
}
|
|
525
573
|
this.llm.__registerPrimitives(p);
|
|
526
574
|
this.#mastra = p;
|
|
527
|
-
this.logger.debug(`[Agents:${this.name}] initialized.`, {
|
|
575
|
+
this.logger.debug(`[Agents:${this.name}] initialized.`, {
|
|
576
|
+
model: this.model,
|
|
577
|
+
name: this.name
|
|
578
|
+
});
|
|
528
579
|
}
|
|
529
580
|
/**
|
|
530
581
|
* Set the concrete tools for the agent
|
|
@@ -532,25 +583,29 @@ var Agent = class extends (_a = MastraBase) {
|
|
|
532
583
|
*/
|
|
533
584
|
__setTools(tools) {
|
|
534
585
|
this.tools = tools;
|
|
535
|
-
this.logger.debug(`[Agents:${this.name}] Tools set for agent ${this.name}`, {
|
|
586
|
+
this.logger.debug(`[Agents:${this.name}] Tools set for agent ${this.name}`, {
|
|
587
|
+
model: this.model,
|
|
588
|
+
name: this.name
|
|
589
|
+
});
|
|
536
590
|
}
|
|
537
|
-
async generateTitleFromUserMessage({
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
591
|
+
async generateTitleFromUserMessage({
|
|
592
|
+
message
|
|
593
|
+
}) {
|
|
594
|
+
const {
|
|
595
|
+
object
|
|
596
|
+
} = await this.llm.__textObject({
|
|
597
|
+
messages: [{
|
|
598
|
+
role: "system",
|
|
599
|
+
content: `
|
|
543
600
|
|
|
544
601
|
- you will generate a short title based on the first message a user begins a conversation with
|
|
545
602
|
- ensure it is not more than 80 characters long
|
|
546
603
|
- the title should be a summary of the user's message
|
|
547
604
|
- do not use quotes or colons`
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
}
|
|
553
|
-
],
|
|
605
|
+
}, {
|
|
606
|
+
role: "user",
|
|
607
|
+
content: JSON.stringify(message)
|
|
608
|
+
}],
|
|
554
609
|
structuredOutput: z.object({
|
|
555
610
|
title: z.string()
|
|
556
611
|
})
|
|
@@ -558,7 +613,7 @@ var Agent = class extends (_a = MastraBase) {
|
|
|
558
613
|
return object.title;
|
|
559
614
|
}
|
|
560
615
|
getMostRecentUserMessage(messages) {
|
|
561
|
-
const userMessages = messages.filter(
|
|
616
|
+
const userMessages = messages.filter(message => message.role === "user");
|
|
562
617
|
return userMessages.at(-1);
|
|
563
618
|
}
|
|
564
619
|
async genTitle(userMessage) {
|
|
@@ -597,7 +652,9 @@ var Agent = class extends (_a = MastraBase) {
|
|
|
597
652
|
memoryConfig
|
|
598
653
|
});
|
|
599
654
|
} else {
|
|
600
|
-
thread = await memory.getThreadById({
|
|
655
|
+
thread = await memory.getThreadById({
|
|
656
|
+
threadId
|
|
657
|
+
});
|
|
601
658
|
if (!thread) {
|
|
602
659
|
this.logger.debug(`Thread with id ${threadId} not found, creating new thread for agent ${this.name}`, {
|
|
603
660
|
runId: runId || this.name
|
|
@@ -613,10 +670,10 @@ var Agent = class extends (_a = MastraBase) {
|
|
|
613
670
|
}
|
|
614
671
|
const newMessages = userMessage ? [userMessage] : userMessages;
|
|
615
672
|
if (thread) {
|
|
616
|
-
const messages = newMessages.map(
|
|
673
|
+
const messages = newMessages.map(u => {
|
|
617
674
|
return {
|
|
618
675
|
id: this.getMemory()?.generateId(),
|
|
619
|
-
createdAt: /* @__PURE__ */
|
|
676
|
+
createdAt: /* @__PURE__ */new Date(),
|
|
620
677
|
threadId: thread.id,
|
|
621
678
|
...u,
|
|
622
679
|
content: u.content,
|
|
@@ -627,7 +684,7 @@ var Agent = class extends (_a = MastraBase) {
|
|
|
627
684
|
const memoryMessages = threadId && memory ? (await memory.rememberMessages({
|
|
628
685
|
threadId,
|
|
629
686
|
config: memoryConfig,
|
|
630
|
-
vectorMessageSearch: messages.slice(-1).map(
|
|
687
|
+
vectorMessageSearch: messages.slice(-1).map(m => {
|
|
631
688
|
if (typeof m === `string`) {
|
|
632
689
|
return m;
|
|
633
690
|
}
|
|
@@ -636,19 +693,24 @@ var Agent = class extends (_a = MastraBase) {
|
|
|
636
693
|
`)
|
|
637
694
|
})).messages : [];
|
|
638
695
|
if (memory) {
|
|
639
|
-
await memory.saveMessages({
|
|
696
|
+
await memory.saveMessages({
|
|
697
|
+
messages,
|
|
698
|
+
memoryConfig
|
|
699
|
+
});
|
|
640
700
|
}
|
|
641
701
|
this.log(LogLevel.DEBUG, "Saved messages to memory", {
|
|
642
702
|
threadId: thread.id,
|
|
643
703
|
runId
|
|
644
704
|
});
|
|
645
|
-
const memorySystemMessage = memory && threadId ? await memory.getSystemMessage({
|
|
705
|
+
const memorySystemMessage = memory && threadId ? await memory.getSystemMessage({
|
|
706
|
+
threadId,
|
|
707
|
+
memoryConfig
|
|
708
|
+
}) : null;
|
|
646
709
|
return {
|
|
647
710
|
threadId: thread.id,
|
|
648
|
-
messages: [
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
content: `
|
|
711
|
+
messages: [{
|
|
712
|
+
role: "system",
|
|
713
|
+
content: `
|
|
652
714
|
|
|
653
715
|
Analyze this message to determine if the user is referring to a previous conversation with the LLM.
|
|
654
716
|
Specifically, identify if the user wants to reference specific information from that chat or if they want the LLM to use the previous chat messages as context for the current conversation.
|
|
@@ -656,13 +718,14 @@ var Agent = class extends (_a = MastraBase) {
|
|
|
656
718
|
Return dates in ISO format.
|
|
657
719
|
If no specific dates are mentioned but time periods are (like "last week" or "past month"), calculate the appropriate date range.
|
|
658
720
|
For the end date, return the date 1 day after the end of the time period.
|
|
659
|
-
Today's date is ${(/* @__PURE__ */
|
|
721
|
+
Today's date is ${(/* @__PURE__ */new Date()).toISOString()} and the time is ${(/* @__PURE__ */new Date()).toLocaleTimeString("en-US", {
|
|
722
|
+
hour: "numeric",
|
|
723
|
+
minute: "numeric",
|
|
724
|
+
hour12: true
|
|
725
|
+
})} ${memorySystemMessage ? `
|
|
660
726
|
|
|
661
727
|
${memorySystemMessage}` : ""}`
|
|
662
|
-
|
|
663
|
-
...this.sanitizeResponseMessages(memoryMessages),
|
|
664
|
-
...newMessages
|
|
665
|
-
]
|
|
728
|
+
}, ...this.sanitizeResponseMessages(memoryMessages), ...newMessages]
|
|
666
729
|
};
|
|
667
730
|
}
|
|
668
731
|
return {
|
|
@@ -670,7 +733,10 @@ ${memorySystemMessage}` : ""}`
|
|
|
670
733
|
messages: userMessages
|
|
671
734
|
};
|
|
672
735
|
}
|
|
673
|
-
return {
|
|
736
|
+
return {
|
|
737
|
+
threadId: threadId || "",
|
|
738
|
+
messages: userMessages
|
|
739
|
+
};
|
|
674
740
|
}
|
|
675
741
|
async saveResponse({
|
|
676
742
|
result,
|
|
@@ -679,37 +745,36 @@ ${memorySystemMessage}` : ""}`
|
|
|
679
745
|
runId,
|
|
680
746
|
memoryConfig
|
|
681
747
|
}) {
|
|
682
|
-
const {
|
|
748
|
+
const {
|
|
749
|
+
response
|
|
750
|
+
} = result;
|
|
683
751
|
try {
|
|
684
752
|
if (response.messages) {
|
|
685
753
|
const ms = Array.isArray(response.messages) ? response.messages : [response.messages];
|
|
686
754
|
const responseMessagesWithoutIncompleteToolCalls = this.sanitizeResponseMessages(ms);
|
|
687
755
|
const memory = this.getMemory();
|
|
688
756
|
if (memory) {
|
|
689
|
-
this.logger.debug(
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
memoryStore: this.getMemory()?.constructor.name
|
|
696
|
-
}
|
|
697
|
-
);
|
|
757
|
+
this.logger.debug(`[Agent:${this.name}] - Memory persistence: store=${this.getMemory()?.constructor.name} threadId=${threadId}`, {
|
|
758
|
+
runId,
|
|
759
|
+
resourceId,
|
|
760
|
+
threadId,
|
|
761
|
+
memoryStore: this.getMemory()?.constructor.name
|
|
762
|
+
});
|
|
698
763
|
await memory.saveMessages({
|
|
699
764
|
memoryConfig,
|
|
700
|
-
messages: responseMessagesWithoutIncompleteToolCalls.map(
|
|
765
|
+
messages: responseMessagesWithoutIncompleteToolCalls.map(message => {
|
|
701
766
|
const messageId = randomUUID();
|
|
702
767
|
let toolCallIds;
|
|
703
768
|
let toolCallArgs;
|
|
704
769
|
let toolNames;
|
|
705
770
|
let type = "text";
|
|
706
771
|
if (message.role === "tool") {
|
|
707
|
-
toolCallIds = message.content.map(
|
|
772
|
+
toolCallIds = message.content.map(content => content.toolCallId);
|
|
708
773
|
type = "tool-result";
|
|
709
774
|
}
|
|
710
775
|
if (message.role === "assistant") {
|
|
711
776
|
const assistantContent = message.content;
|
|
712
|
-
const assistantToolCalls = assistantContent.map(
|
|
777
|
+
const assistantToolCalls = assistantContent.map(content => {
|
|
713
778
|
if (content.type === "tool-call") {
|
|
714
779
|
return {
|
|
715
780
|
toolCallId: content.toolCallId,
|
|
@@ -719,9 +784,9 @@ ${memorySystemMessage}` : ""}`
|
|
|
719
784
|
}
|
|
720
785
|
return void 0;
|
|
721
786
|
})?.filter(Boolean);
|
|
722
|
-
toolCallIds = assistantToolCalls?.map(
|
|
723
|
-
toolCallArgs = assistantToolCalls?.map(
|
|
724
|
-
toolNames = assistantToolCalls?.map(
|
|
787
|
+
toolCallIds = assistantToolCalls?.map(toolCall => toolCall.toolCallId);
|
|
788
|
+
toolCallArgs = assistantToolCalls?.map(toolCall => toolCall.toolArgs);
|
|
789
|
+
toolNames = assistantToolCalls?.map(toolCall => toolCall.toolName);
|
|
725
790
|
type = assistantContent?.[0]?.type;
|
|
726
791
|
}
|
|
727
792
|
return {
|
|
@@ -729,7 +794,7 @@ ${memorySystemMessage}` : ""}`
|
|
|
729
794
|
threadId,
|
|
730
795
|
role: message.role,
|
|
731
796
|
content: message.content,
|
|
732
|
-
createdAt: /* @__PURE__ */
|
|
797
|
+
createdAt: /* @__PURE__ */new Date(),
|
|
733
798
|
toolCallIds: toolCallIds?.length ? toolCallIds : void 0,
|
|
734
799
|
toolCallArgs: toolCallArgs?.length ? toolCallArgs : void 0,
|
|
735
800
|
toolNames: toolNames?.length ? toolNames : void 0,
|
|
@@ -767,10 +832,10 @@ ${memorySystemMessage}` : ""}`
|
|
|
767
832
|
}
|
|
768
833
|
}
|
|
769
834
|
}
|
|
770
|
-
const messagesBySanitizedContent = messages.map(
|
|
835
|
+
const messagesBySanitizedContent = messages.map(message => {
|
|
771
836
|
if (message.role !== "assistant" && message.role !== `tool` && message.role !== `user`) return message;
|
|
772
837
|
if (typeof message.content === "string") return message;
|
|
773
|
-
const sanitizedContent = message.content.filter(
|
|
838
|
+
const sanitizedContent = message.content.filter(content => {
|
|
774
839
|
if (content.type === `tool-call`) {
|
|
775
840
|
return toolResultIds.includes(content.toolCallId);
|
|
776
841
|
}
|
|
@@ -787,12 +852,12 @@ ${memorySystemMessage}` : ""}`
|
|
|
787
852
|
content: sanitizedContent
|
|
788
853
|
};
|
|
789
854
|
});
|
|
790
|
-
return messagesBySanitizedContent.filter(
|
|
855
|
+
return messagesBySanitizedContent.filter(message => {
|
|
791
856
|
if (typeof message.content === `string`) {
|
|
792
857
|
return message.content !== "";
|
|
793
858
|
}
|
|
794
859
|
if (Array.isArray(message.content)) {
|
|
795
|
-
return message.content.length && message.content.every(
|
|
860
|
+
return message.content.length && message.content.every(c => {
|
|
796
861
|
if (c.type === `text`) {
|
|
797
862
|
return c.text && c.text !== "";
|
|
798
863
|
}
|
|
@@ -807,41 +872,40 @@ ${memorySystemMessage}` : ""}`
|
|
|
807
872
|
// threadId,
|
|
808
873
|
runId
|
|
809
874
|
}) {
|
|
810
|
-
this.logger.debug(`[Agents:${this.name}] - Assigning tools`, {
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
}
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
875
|
+
this.logger.debug(`[Agents:${this.name}] - Assigning tools`, {
|
|
876
|
+
runId
|
|
877
|
+
});
|
|
878
|
+
const converted = Object.entries(this.tools || {}).reduce((memo, value) => {
|
|
879
|
+
const k = value[0];
|
|
880
|
+
const tool = this.tools[k];
|
|
881
|
+
if (tool) {
|
|
882
|
+
memo[k] = {
|
|
883
|
+
description: tool.description,
|
|
884
|
+
parameters: tool.inputSchema,
|
|
885
|
+
execute: async args => {
|
|
886
|
+
try {
|
|
887
|
+
this.logger.debug(`[Agent:${this.name}] - Executing tool ${k}`, {
|
|
888
|
+
name: k,
|
|
889
|
+
description: tool.description,
|
|
890
|
+
args
|
|
891
|
+
});
|
|
892
|
+
return tool.execute({
|
|
893
|
+
context: args,
|
|
894
|
+
mastra: this.#mastra,
|
|
895
|
+
runId
|
|
896
|
+
});
|
|
897
|
+
} catch (err) {
|
|
898
|
+
this.logger.error(`[Agent:${this.name}] - Failed execution`, {
|
|
899
|
+
error: err,
|
|
900
|
+
runId
|
|
901
|
+
});
|
|
902
|
+
throw err;
|
|
838
903
|
}
|
|
839
|
-
}
|
|
840
|
-
}
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
);
|
|
904
|
+
}
|
|
905
|
+
};
|
|
906
|
+
}
|
|
907
|
+
return memo;
|
|
908
|
+
}, {});
|
|
845
909
|
const toolsFromToolsetsConverted = {
|
|
846
910
|
...converted
|
|
847
911
|
};
|
|
@@ -850,13 +914,13 @@ ${memorySystemMessage}` : ""}`
|
|
|
850
914
|
this.logger.debug(`[Agent:${this.name}] - Adding tools from toolsets ${Object.keys(toolsets || {}).join(", ")}`, {
|
|
851
915
|
runId
|
|
852
916
|
});
|
|
853
|
-
toolsFromToolsets.forEach(
|
|
917
|
+
toolsFromToolsets.forEach(toolset => {
|
|
854
918
|
Object.entries(toolset).forEach(([toolName, tool]) => {
|
|
855
919
|
const toolObj = tool;
|
|
856
920
|
toolsFromToolsetsConverted[toolName] = {
|
|
857
921
|
description: toolObj.description || "",
|
|
858
922
|
parameters: toolObj.inputSchema,
|
|
859
|
-
execute: async
|
|
923
|
+
execute: async args => {
|
|
860
924
|
try {
|
|
861
925
|
this.logger.debug(`[Agent:${this.name}] - Executing tool ${toolName}`, {
|
|
862
926
|
name: toolName,
|
|
@@ -890,7 +954,9 @@ ${memorySystemMessage}` : ""}`
|
|
|
890
954
|
}) {
|
|
891
955
|
let coreMessages = [];
|
|
892
956
|
let threadIdToUse = threadId;
|
|
893
|
-
this.log(LogLevel.DEBUG, `Saving user messages in memory for agent ${this.name}`, {
|
|
957
|
+
this.log(LogLevel.DEBUG, `Saving user messages in memory for agent ${this.name}`, {
|
|
958
|
+
runId
|
|
959
|
+
});
|
|
894
960
|
const saveMessageResponse = await this.saveMemory({
|
|
895
961
|
threadId,
|
|
896
962
|
resourceId,
|
|
@@ -899,7 +965,10 @@ ${memorySystemMessage}` : ""}`
|
|
|
899
965
|
});
|
|
900
966
|
coreMessages = saveMessageResponse.messages;
|
|
901
967
|
threadIdToUse = saveMessageResponse.threadId;
|
|
902
|
-
return {
|
|
968
|
+
return {
|
|
969
|
+
coreMessages,
|
|
970
|
+
threadIdToUse
|
|
971
|
+
};
|
|
903
972
|
}
|
|
904
973
|
__primitive({
|
|
905
974
|
messages,
|
|
@@ -913,24 +982,23 @@ ${memorySystemMessage}` : ""}`
|
|
|
913
982
|
return {
|
|
914
983
|
before: async () => {
|
|
915
984
|
if (process.env.NODE_ENV !== "test") {
|
|
916
|
-
this.logger.debug(`[Agents:${this.name}] - Starting generation`, {
|
|
985
|
+
this.logger.debug(`[Agents:${this.name}] - Starting generation`, {
|
|
986
|
+
runId
|
|
987
|
+
});
|
|
917
988
|
}
|
|
918
989
|
const systemMessage = {
|
|
919
990
|
role: "system",
|
|
920
|
-
content: `${this.instructions}. Today's date is ${(/* @__PURE__ */
|
|
991
|
+
content: `${this.instructions}. Today's date is ${(/* @__PURE__ */new Date()).toISOString()}`
|
|
921
992
|
};
|
|
922
993
|
let coreMessages = messages;
|
|
923
994
|
let threadIdToUse = threadId;
|
|
924
995
|
if (this.getMemory() && resourceId) {
|
|
925
|
-
this.logger.debug(
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
memoryStore: this.getMemory()?.constructor.name
|
|
932
|
-
}
|
|
933
|
-
);
|
|
996
|
+
this.logger.debug(`[Agent:${this.name}] - Memory persistence enabled: store=${this.getMemory()?.constructor.name}, resourceId=${resourceId}`, {
|
|
997
|
+
runId,
|
|
998
|
+
resourceId,
|
|
999
|
+
threadId: threadIdToUse,
|
|
1000
|
+
memoryStore: this.getMemory()?.constructor.name
|
|
1001
|
+
});
|
|
934
1002
|
const preExecuteResult = await this.preExecute({
|
|
935
1003
|
resourceId,
|
|
936
1004
|
runId,
|
|
@@ -952,7 +1020,7 @@ ${memorySystemMessage}` : ""}`
|
|
|
952
1020
|
}
|
|
953
1021
|
this.logger.debug(`[Agent:${this.name}] - Enhancing tools: ${reasons.join(", ")}`, {
|
|
954
1022
|
runId,
|
|
955
|
-
toolsets: toolsets ? Object.keys(toolsets) :
|
|
1023
|
+
toolsets: toolsets ? Object.keys(toolsets) : void 0,
|
|
956
1024
|
hasMemory: !!this.getMemory(),
|
|
957
1025
|
hasResourceId: !!resourceId
|
|
958
1026
|
});
|
|
@@ -962,8 +1030,12 @@ ${memorySystemMessage}` : ""}`
|
|
|
962
1030
|
runId
|
|
963
1031
|
});
|
|
964
1032
|
}
|
|
965
|
-
const messageObjects = [systemMessage, ...context || [], ...coreMessages];
|
|
966
|
-
return {
|
|
1033
|
+
const messageObjects = [systemMessage, ...(context || []), ...coreMessages];
|
|
1034
|
+
return {
|
|
1035
|
+
messageObjects,
|
|
1036
|
+
convertedTools,
|
|
1037
|
+
threadId: threadIdToUse
|
|
1038
|
+
};
|
|
967
1039
|
},
|
|
968
1040
|
after: async ({
|
|
969
1041
|
result,
|
|
@@ -978,7 +1050,7 @@ ${memorySystemMessage}` : ""}`
|
|
|
978
1050
|
toolResults: result?.toolResults,
|
|
979
1051
|
toolCalls: result?.toolCalls,
|
|
980
1052
|
usage: result?.usage,
|
|
981
|
-
steps: result?.steps?.map(
|
|
1053
|
+
steps: result?.steps?.map(s => {
|
|
982
1054
|
return {
|
|
983
1055
|
stepType: s?.stepType,
|
|
984
1056
|
text: result?.text,
|
|
@@ -1013,7 +1085,7 @@ ${memorySystemMessage}` : ""}`
|
|
|
1013
1085
|
}
|
|
1014
1086
|
}
|
|
1015
1087
|
if (Object.keys(this.evals || {}).length > 0) {
|
|
1016
|
-
const input = messages.map(
|
|
1088
|
+
const input = messages.map(message => message.content).join("\n");
|
|
1017
1089
|
const runIdToUse = runId2 || crypto.randomUUID();
|
|
1018
1090
|
for (const metric of Object.values(this.evals || {})) {
|
|
1019
1091
|
executeHook("onGeneration" /* ON_GENERATION */, {
|
|
@@ -1044,14 +1116,12 @@ ${memorySystemMessage}` : ""}`
|
|
|
1044
1116
|
} = {}) {
|
|
1045
1117
|
let messagesToUse = [];
|
|
1046
1118
|
if (typeof messages === `string`) {
|
|
1047
|
-
messagesToUse = [
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
}
|
|
1052
|
-
];
|
|
1119
|
+
messagesToUse = [{
|
|
1120
|
+
role: "user",
|
|
1121
|
+
content: messages
|
|
1122
|
+
}];
|
|
1053
1123
|
} else {
|
|
1054
|
-
messagesToUse = messages.map(
|
|
1124
|
+
messagesToUse = messages.map(message => {
|
|
1055
1125
|
if (typeof message === `string`) {
|
|
1056
1126
|
return {
|
|
1057
1127
|
role: "user",
|
|
@@ -1062,7 +1132,10 @@ ${memorySystemMessage}` : ""}`
|
|
|
1062
1132
|
});
|
|
1063
1133
|
}
|
|
1064
1134
|
const runIdToUse = runId || randomUUID();
|
|
1065
|
-
const {
|
|
1135
|
+
const {
|
|
1136
|
+
before,
|
|
1137
|
+
after
|
|
1138
|
+
} = this.__primitive({
|
|
1066
1139
|
messages: messagesToUse,
|
|
1067
1140
|
context,
|
|
1068
1141
|
threadId: threadIdInFn,
|
|
@@ -1071,7 +1144,11 @@ ${memorySystemMessage}` : ""}`
|
|
|
1071
1144
|
runId: runIdToUse,
|
|
1072
1145
|
toolsets
|
|
1073
1146
|
});
|
|
1074
|
-
const {
|
|
1147
|
+
const {
|
|
1148
|
+
threadId,
|
|
1149
|
+
messageObjects,
|
|
1150
|
+
convertedTools
|
|
1151
|
+
} = await before();
|
|
1075
1152
|
if (output === "text") {
|
|
1076
1153
|
const result2 = await this.llm.__text({
|
|
1077
1154
|
messages: messageObjects,
|
|
@@ -1084,7 +1161,13 @@ ${memorySystemMessage}` : ""}`
|
|
|
1084
1161
|
toolChoice
|
|
1085
1162
|
});
|
|
1086
1163
|
const outputText2 = result2.text;
|
|
1087
|
-
await after({
|
|
1164
|
+
await after({
|
|
1165
|
+
result: result2,
|
|
1166
|
+
threadId,
|
|
1167
|
+
memoryConfig: memoryOptions,
|
|
1168
|
+
outputText: outputText2,
|
|
1169
|
+
runId: runIdToUse
|
|
1170
|
+
});
|
|
1088
1171
|
return result2;
|
|
1089
1172
|
}
|
|
1090
1173
|
const result = await this.llm.__textObject({
|
|
@@ -1099,7 +1182,13 @@ ${memorySystemMessage}` : ""}`
|
|
|
1099
1182
|
toolChoice
|
|
1100
1183
|
});
|
|
1101
1184
|
const outputText = JSON.stringify(result.object);
|
|
1102
|
-
await after({
|
|
1185
|
+
await after({
|
|
1186
|
+
result,
|
|
1187
|
+
threadId,
|
|
1188
|
+
memoryConfig: memoryOptions,
|
|
1189
|
+
outputText,
|
|
1190
|
+
runId: runIdToUse
|
|
1191
|
+
});
|
|
1103
1192
|
return result;
|
|
1104
1193
|
}
|
|
1105
1194
|
async stream(messages, {
|
|
@@ -1119,14 +1208,12 @@ ${memorySystemMessage}` : ""}`
|
|
|
1119
1208
|
const runIdToUse = runId || randomUUID();
|
|
1120
1209
|
let messagesToUse = [];
|
|
1121
1210
|
if (typeof messages === `string`) {
|
|
1122
|
-
messagesToUse = [
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
}
|
|
1127
|
-
];
|
|
1211
|
+
messagesToUse = [{
|
|
1212
|
+
role: "user",
|
|
1213
|
+
content: messages
|
|
1214
|
+
}];
|
|
1128
1215
|
} else {
|
|
1129
|
-
messagesToUse = messages.map(
|
|
1216
|
+
messagesToUse = messages.map(message => {
|
|
1130
1217
|
if (typeof message === `string`) {
|
|
1131
1218
|
return {
|
|
1132
1219
|
role: "user",
|
|
@@ -1136,7 +1223,10 @@ ${memorySystemMessage}` : ""}`
|
|
|
1136
1223
|
return message;
|
|
1137
1224
|
});
|
|
1138
1225
|
}
|
|
1139
|
-
const {
|
|
1226
|
+
const {
|
|
1227
|
+
before,
|
|
1228
|
+
after
|
|
1229
|
+
} = this.__primitive({
|
|
1140
1230
|
messages: messagesToUse,
|
|
1141
1231
|
context,
|
|
1142
1232
|
threadId: threadIdInFn,
|
|
@@ -1145,7 +1235,11 @@ ${memorySystemMessage}` : ""}`
|
|
|
1145
1235
|
runId: runIdToUse,
|
|
1146
1236
|
toolsets
|
|
1147
1237
|
});
|
|
1148
|
-
const {
|
|
1238
|
+
const {
|
|
1239
|
+
threadId,
|
|
1240
|
+
messageObjects,
|
|
1241
|
+
convertedTools
|
|
1242
|
+
} = await before();
|
|
1149
1243
|
if (output === "text") {
|
|
1150
1244
|
this.logger.debug(`Starting agent ${this.name} llm stream call`, {
|
|
1151
1245
|
runId
|
|
@@ -1156,11 +1250,17 @@ ${memorySystemMessage}` : ""}`
|
|
|
1156
1250
|
tools: this.tools,
|
|
1157
1251
|
convertedTools,
|
|
1158
1252
|
onStepFinish,
|
|
1159
|
-
onFinish: async
|
|
1253
|
+
onFinish: async result => {
|
|
1160
1254
|
try {
|
|
1161
1255
|
const res = JSON.parse(result) || {};
|
|
1162
1256
|
const outputText = res.text;
|
|
1163
|
-
await after({
|
|
1257
|
+
await after({
|
|
1258
|
+
result: res,
|
|
1259
|
+
threadId,
|
|
1260
|
+
memoryConfig: memoryOptions,
|
|
1261
|
+
outputText,
|
|
1262
|
+
runId: runIdToUse
|
|
1263
|
+
});
|
|
1164
1264
|
} catch (e) {
|
|
1165
1265
|
this.logger.error("Error saving memory on finish", {
|
|
1166
1266
|
error: e,
|
|
@@ -1184,11 +1284,17 @@ ${memorySystemMessage}` : ""}`
|
|
|
1184
1284
|
structuredOutput: output,
|
|
1185
1285
|
convertedTools,
|
|
1186
1286
|
onStepFinish,
|
|
1187
|
-
onFinish: async
|
|
1287
|
+
onFinish: async result => {
|
|
1188
1288
|
try {
|
|
1189
1289
|
const res = JSON.parse(result) || {};
|
|
1190
1290
|
const outputText = JSON.stringify(res.object);
|
|
1191
|
-
await after({
|
|
1291
|
+
await after({
|
|
1292
|
+
result: res,
|
|
1293
|
+
threadId,
|
|
1294
|
+
memoryConfig: memoryOptions,
|
|
1295
|
+
outputText,
|
|
1296
|
+
runId: runIdToUse
|
|
1297
|
+
});
|
|
1192
1298
|
} catch (e) {
|
|
1193
1299
|
this.logger.error("Error saving memory on finish", {
|
|
1194
1300
|
error: e,
|
|
@@ -1203,8 +1309,11 @@ ${memorySystemMessage}` : ""}`
|
|
|
1203
1309
|
});
|
|
1204
1310
|
}
|
|
1205
1311
|
};
|
|
1206
|
-
|
|
1207
|
-
|
|
1208
|
-
|
|
1312
|
+
Agent = /*@__PURE__*/(_ => {
|
|
1313
|
+
_init = __decoratorStart(_a);
|
|
1314
|
+
Agent = __decorateElement(_init, 0, "Agent", _Agent_decorators, Agent);
|
|
1315
|
+
__runInitializers(_init, 1, Agent);
|
|
1316
|
+
return Agent;
|
|
1317
|
+
})();
|
|
1209
1318
|
|
|
1210
1319
|
export { Agent };
|