@mastra/core 0.9.0 → 0.9.1-alpha.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/agent/index.cjs +2 -2
- package/dist/agent/index.d.cts +4 -2
- package/dist/agent/index.d.ts +4 -2
- package/dist/agent/index.js +1 -1
- package/dist/{base-oP3DoUrm.d.cts → base-CZmT-p10.d.cts} +524 -53
- package/dist/{base-Bk5V1doj.d.ts → base-DDoWFpFc.d.ts} +524 -53
- package/dist/{chunk-NTHHPNOW.js → chunk-34622N67.js} +1 -1
- package/dist/{chunk-ATXF6TIO.cjs → chunk-422BXQRT.cjs} +24 -677
- package/dist/{chunk-HARYMLZH.js → chunk-4K4DHXRF.js} +6 -658
- package/dist/{chunk-N2ONCUUS.js → chunk-6AHC67YY.js} +132 -12
- package/dist/chunk-7AGAXO6B.cjs +588 -0
- package/dist/{chunk-GEIPVIW4.js → chunk-BFF2O6RO.js} +1 -1
- package/dist/{chunk-RASVJ3TR.js → chunk-BU45BMXY.js} +55 -1
- package/dist/{chunk-DIZZQ3E5.cjs → chunk-CEXM6WP5.cjs} +2 -2
- package/dist/chunk-HNEE7IF4.js +60 -0
- package/dist/{chunk-NZDXKMDP.cjs → chunk-LABUWBKX.cjs} +1 -1
- package/dist/chunk-MUNFCOMB.cjs +62 -0
- package/dist/{chunk-GJWCFDFN.js → chunk-PS2ZF6MA.js} +1 -1
- package/dist/{chunk-M472GIT6.js → chunk-SGGPJWRQ.js} +1 -1
- package/dist/{chunk-7CSNWYGJ.cjs → chunk-U5DGGGS2.cjs} +2 -2
- package/dist/{chunk-W5IA5OGL.cjs → chunk-V7IE36YV.cjs} +58 -4
- package/dist/chunk-VMVXIPGW.js +586 -0
- package/dist/{chunk-53P5ZBJE.cjs → chunk-YTTBFAQJ.cjs} +2 -2
- package/dist/{chunk-KZWBYRXW.cjs → chunk-YZFNMXY4.cjs} +135 -14
- package/dist/di/index.cjs +2 -2
- package/dist/di/index.d.cts +1 -52
- package/dist/di/index.d.ts +1 -52
- package/dist/di/index.js +1 -1
- package/dist/eval/index.d.cts +4 -2
- package/dist/eval/index.d.ts +4 -2
- package/dist/index.cjs +60 -55
- package/dist/index.d.cts +5 -3
- package/dist/index.d.ts +5 -3
- package/dist/index.js +10 -9
- package/dist/integration/index.cjs +3 -3
- package/dist/integration/index.d.cts +4 -2
- package/dist/integration/index.d.ts +4 -2
- package/dist/integration/index.js +1 -1
- package/dist/llm/index.cjs +8 -0
- package/dist/llm/index.d.cts +4 -2
- package/dist/llm/index.d.ts +4 -2
- package/dist/llm/index.js +1 -1
- package/dist/mastra/index.cjs +2 -2
- package/dist/mastra/index.d.cts +4 -2
- package/dist/mastra/index.d.ts +4 -2
- package/dist/mastra/index.js +1 -1
- package/dist/memory/index.cjs +7 -3
- package/dist/memory/index.d.cts +4 -2
- package/dist/memory/index.d.ts +4 -2
- package/dist/memory/index.js +1 -1
- package/dist/network/index.cjs +4 -4
- package/dist/network/index.d.cts +4 -2
- package/dist/network/index.d.ts +4 -2
- package/dist/network/index.js +2 -2
- package/dist/relevance/index.cjs +4 -4
- package/dist/relevance/index.d.cts +4 -2
- package/dist/relevance/index.d.ts +4 -2
- package/dist/relevance/index.js +1 -1
- package/dist/runtime-context/index.cjs +7 -68
- package/dist/runtime-context/index.js +1 -69
- package/dist/server/index.d.cts +4 -2
- package/dist/server/index.d.ts +4 -2
- package/dist/storage/index.d.cts +4 -2
- package/dist/storage/index.d.ts +4 -2
- package/dist/storage/libsql/index.cjs +19 -0
- package/dist/storage/libsql/index.d.cts +7 -8
- package/dist/storage/libsql/index.d.ts +7 -8
- package/dist/storage/libsql/index.js +19 -0
- package/dist/telemetry/index.d.cts +4 -2
- package/dist/telemetry/index.d.ts +4 -2
- package/dist/tools/index.cjs +4 -4
- package/dist/tools/index.d.cts +4 -2
- package/dist/tools/index.d.ts +4 -2
- package/dist/tools/index.js +1 -1
- package/dist/utils.cjs +14 -14
- package/dist/utils.d.cts +6 -4
- package/dist/utils.d.ts +6 -4
- package/dist/utils.js +1 -1
- package/dist/voice/index.d.cts +5 -3
- package/dist/voice/index.d.ts +5 -3
- package/dist/workflows/index.cjs +22 -26
- package/dist/workflows/index.d.cts +5 -3
- package/dist/workflows/index.d.ts +5 -3
- package/dist/workflows/index.js +1 -1
- package/dist/workflows/vNext/index.cjs +1058 -0
- package/dist/workflows/vNext/index.d.cts +180 -0
- package/dist/workflows/vNext/index.d.ts +180 -0
- package/dist/workflows/vNext/index.js +1046 -0
- package/package.json +11 -1
- package/workflows/vNext.d.ts +1 -0
- /package/dist/{chunk-WEYWYKLG.cjs → chunk-27PAET7X.cjs} +0 -0
- /package/dist/{chunk-FRQFWZDN.cjs → chunk-HSVOEWAM.cjs} +0 -0
- /package/dist/{chunk-ZDWFBE5L.js → chunk-NH5WJNNS.js} +0 -0
- /package/dist/{chunk-LANFNMEE.js → chunk-SGTFVHOZ.js} +0 -0
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
import { DefaultVoice } from './chunk-MWLZJBPF.js';
|
|
2
|
+
import { MastraLLM } from './chunk-VMVXIPGW.js';
|
|
2
3
|
import { InstrumentClass } from './chunk-HFOT2NFM.js';
|
|
3
|
-
import { ensureToolProperties, ensureAllMessagesAreCoreMessages, createMastraProxy, makeCoreTool
|
|
4
|
+
import { ensureToolProperties, ensureAllMessagesAreCoreMessages, createMastraProxy, makeCoreTool } from './chunk-34622N67.js';
|
|
4
5
|
import { MastraBase } from './chunk-CLJQYXNM.js';
|
|
5
6
|
import { RegisteredLogger } from './chunk-2BVZNKLX.js';
|
|
6
|
-
import { RuntimeContext } from './chunk-
|
|
7
|
+
import { RuntimeContext } from './chunk-SGGPJWRQ.js';
|
|
7
8
|
import { executeHook } from './chunk-BB4KXGBU.js';
|
|
8
9
|
import { __decoratorStart, __decorateElement, __runInitializers } from './chunk-C6A6W6XS.js';
|
|
9
10
|
import { context, trace } from '@opentelemetry/api';
|
|
10
11
|
import { z } from 'zod';
|
|
11
12
|
import { get } from 'radash';
|
|
12
13
|
import { randomUUID } from 'crypto';
|
|
13
|
-
import { jsonSchema, generateText, Output, generateObject, streamText, streamObject } from 'ai';
|
|
14
14
|
import EventEmitter from 'node:events';
|
|
15
15
|
import sift from 'sift';
|
|
16
16
|
import { createActor, assign, fromPromise, setup } from 'xstate';
|
|
@@ -43,9 +43,6 @@ var Step = class {
|
|
|
43
43
|
this.retryConfig = retryConfig;
|
|
44
44
|
}
|
|
45
45
|
};
|
|
46
|
-
function createStep(opts) {
|
|
47
|
-
return new Step(opts);
|
|
48
|
-
}
|
|
49
46
|
|
|
50
47
|
// src/workflows/types.ts
|
|
51
48
|
var WhenConditionReturnValue = /* @__PURE__ */(WhenConditionReturnValue2 => {
|
|
@@ -55,663 +52,13 @@ var WhenConditionReturnValue = /* @__PURE__ */(WhenConditionReturnValue2 => {
|
|
|
55
52
|
WhenConditionReturnValue2["LIMBO"] = "limbo";
|
|
56
53
|
return WhenConditionReturnValue2;
|
|
57
54
|
})(WhenConditionReturnValue || {});
|
|
58
|
-
|
|
59
|
-
// src/llm/model/base.ts
|
|
60
|
-
var MastraLLMBase = class extends MastraBase {
|
|
61
|
-
// @ts-ignore
|
|
62
|
-
#mastra;
|
|
63
|
-
#model;
|
|
64
|
-
constructor({
|
|
65
|
-
name,
|
|
66
|
-
model
|
|
67
|
-
}) {
|
|
68
|
-
super({
|
|
69
|
-
component: RegisteredLogger.LLM,
|
|
70
|
-
name
|
|
71
|
-
});
|
|
72
|
-
this.#model = model;
|
|
73
|
-
}
|
|
74
|
-
getProvider() {
|
|
75
|
-
return this.#model.provider;
|
|
76
|
-
}
|
|
77
|
-
getModelId() {
|
|
78
|
-
return this.#model.modelId;
|
|
79
|
-
}
|
|
80
|
-
getModel() {
|
|
81
|
-
return this.#model;
|
|
82
|
-
}
|
|
83
|
-
convertToMessages(messages) {
|
|
84
|
-
if (Array.isArray(messages)) {
|
|
85
|
-
return messages.map(m => {
|
|
86
|
-
if (typeof m === "string") {
|
|
87
|
-
return {
|
|
88
|
-
role: "user",
|
|
89
|
-
content: m
|
|
90
|
-
};
|
|
91
|
-
}
|
|
92
|
-
return m;
|
|
93
|
-
});
|
|
94
|
-
}
|
|
95
|
-
return [{
|
|
96
|
-
role: "user",
|
|
97
|
-
content: messages
|
|
98
|
-
}];
|
|
99
|
-
}
|
|
100
|
-
__registerPrimitives(p) {
|
|
101
|
-
if (p.telemetry) {
|
|
102
|
-
this.__setTelemetry(p.telemetry);
|
|
103
|
-
}
|
|
104
|
-
if (p.logger) {
|
|
105
|
-
this.__setLogger(p.logger);
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
__registerMastra(p) {
|
|
109
|
-
this.#mastra = p;
|
|
110
|
-
}
|
|
111
|
-
async __text(input) {
|
|
112
|
-
this.logger.debug(`[LLMs:${this.name}] Generating text.`, {
|
|
113
|
-
input
|
|
114
|
-
});
|
|
115
|
-
throw new Error("Method not implemented.");
|
|
116
|
-
}
|
|
117
|
-
async __textObject(input) {
|
|
118
|
-
this.logger.debug(`[LLMs:${this.name}] Generating object.`, {
|
|
119
|
-
input
|
|
120
|
-
});
|
|
121
|
-
throw new Error("Method not implemented.");
|
|
122
|
-
}
|
|
123
|
-
async generate(messages, options) {
|
|
124
|
-
this.logger.debug(`[LLMs:${this.name}] Generating text.`, {
|
|
125
|
-
messages,
|
|
126
|
-
options
|
|
127
|
-
});
|
|
128
|
-
throw new Error("Method not implemented.");
|
|
129
|
-
}
|
|
130
|
-
async __stream(input) {
|
|
131
|
-
this.logger.debug(`[LLMs:${this.name}] Streaming text.`, {
|
|
132
|
-
input
|
|
133
|
-
});
|
|
134
|
-
throw new Error("Method not implemented.");
|
|
135
|
-
}
|
|
136
|
-
async __streamObject(input) {
|
|
137
|
-
this.logger.debug(`[LLMs:${this.name}] Streaming object.`, {
|
|
138
|
-
input
|
|
139
|
-
});
|
|
140
|
-
throw new Error("Method not implemented.");
|
|
141
|
-
}
|
|
142
|
-
async stream(messages, options) {
|
|
143
|
-
this.logger.debug(`[LLMs:${this.name}] Streaming text.`, {
|
|
144
|
-
messages,
|
|
145
|
-
options
|
|
146
|
-
});
|
|
147
|
-
throw new Error("Method not implemented.");
|
|
148
|
-
}
|
|
149
|
-
};
|
|
150
|
-
var MastraLLM = class extends MastraLLMBase {
|
|
151
|
-
#model;
|
|
152
|
-
#mastra;
|
|
153
|
-
constructor({
|
|
154
|
-
model,
|
|
155
|
-
mastra
|
|
156
|
-
}) {
|
|
157
|
-
super({
|
|
158
|
-
name: "aisdk",
|
|
159
|
-
model
|
|
160
|
-
});
|
|
161
|
-
this.#model = model;
|
|
162
|
-
if (mastra) {
|
|
163
|
-
this.#mastra = mastra;
|
|
164
|
-
if (mastra.getLogger()) {
|
|
165
|
-
this.__setLogger(mastra.getLogger());
|
|
166
|
-
}
|
|
167
|
-
}
|
|
168
|
-
}
|
|
169
|
-
__registerPrimitives(p) {
|
|
170
|
-
if (p.telemetry) {
|
|
171
|
-
this.__setTelemetry(p.telemetry);
|
|
172
|
-
}
|
|
173
|
-
if (p.logger) {
|
|
174
|
-
this.__setLogger(p.logger);
|
|
175
|
-
}
|
|
176
|
-
}
|
|
177
|
-
__registerMastra(p) {
|
|
178
|
-
this.#mastra = p;
|
|
179
|
-
}
|
|
180
|
-
getProvider() {
|
|
181
|
-
return this.#model.provider;
|
|
182
|
-
}
|
|
183
|
-
getModelId() {
|
|
184
|
-
return this.#model.modelId;
|
|
185
|
-
}
|
|
186
|
-
getModel() {
|
|
187
|
-
return this.#model;
|
|
188
|
-
}
|
|
189
|
-
convertTools({
|
|
190
|
-
tools,
|
|
191
|
-
runId,
|
|
192
|
-
threadId,
|
|
193
|
-
resourceId,
|
|
194
|
-
memory,
|
|
195
|
-
runtimeContext
|
|
196
|
-
}) {
|
|
197
|
-
this.logger.debug("Starting tool conversion for LLM");
|
|
198
|
-
let mastraProxy = void 0;
|
|
199
|
-
const logger = this.logger;
|
|
200
|
-
if (this.#mastra) {
|
|
201
|
-
mastraProxy = createMastraProxy({
|
|
202
|
-
mastra: this.#mastra,
|
|
203
|
-
logger
|
|
204
|
-
});
|
|
205
|
-
}
|
|
206
|
-
const converted = Object.entries(tools || {}).reduce((memo, value) => {
|
|
207
|
-
const k = value[0];
|
|
208
|
-
const tool = value[1];
|
|
209
|
-
if (tool) {
|
|
210
|
-
const options = {
|
|
211
|
-
name: k,
|
|
212
|
-
runId,
|
|
213
|
-
threadId,
|
|
214
|
-
resourceId,
|
|
215
|
-
logger: this.logger,
|
|
216
|
-
memory,
|
|
217
|
-
mastra: mastraProxy,
|
|
218
|
-
runtimeContext
|
|
219
|
-
};
|
|
220
|
-
memo[k] = makeCoreTool(tool, options);
|
|
221
|
-
}
|
|
222
|
-
return memo;
|
|
223
|
-
}, {});
|
|
224
|
-
this.logger.debug(`Converted tools for LLM`);
|
|
225
|
-
return converted;
|
|
226
|
-
}
|
|
227
|
-
async __text({
|
|
228
|
-
runId,
|
|
229
|
-
messages,
|
|
230
|
-
maxSteps = 5,
|
|
231
|
-
tools,
|
|
232
|
-
convertedTools,
|
|
233
|
-
temperature,
|
|
234
|
-
toolChoice = "auto",
|
|
235
|
-
onStepFinish,
|
|
236
|
-
experimental_output,
|
|
237
|
-
telemetry,
|
|
238
|
-
threadId,
|
|
239
|
-
resourceId,
|
|
240
|
-
memory,
|
|
241
|
-
runtimeContext,
|
|
242
|
-
...rest
|
|
243
|
-
}) {
|
|
244
|
-
const model = this.#model;
|
|
245
|
-
this.logger.debug(`[LLM] - Generating text`, {
|
|
246
|
-
runId,
|
|
247
|
-
messages,
|
|
248
|
-
maxSteps,
|
|
249
|
-
threadId,
|
|
250
|
-
resourceId,
|
|
251
|
-
tools: Object.keys(tools || convertedTools || {})
|
|
252
|
-
});
|
|
253
|
-
const finalTools = convertedTools || this.convertTools({
|
|
254
|
-
tools,
|
|
255
|
-
runId,
|
|
256
|
-
threadId,
|
|
257
|
-
resourceId,
|
|
258
|
-
memory,
|
|
259
|
-
runtimeContext
|
|
260
|
-
});
|
|
261
|
-
const argsForExecute = {
|
|
262
|
-
model,
|
|
263
|
-
temperature,
|
|
264
|
-
tools: {
|
|
265
|
-
...finalTools
|
|
266
|
-
},
|
|
267
|
-
toolChoice,
|
|
268
|
-
maxSteps,
|
|
269
|
-
onStepFinish: async props => {
|
|
270
|
-
void onStepFinish?.(props);
|
|
271
|
-
this.logger.debug("[LLM] - Step Change:", {
|
|
272
|
-
text: props?.text,
|
|
273
|
-
toolCalls: props?.toolCalls,
|
|
274
|
-
toolResults: props?.toolResults,
|
|
275
|
-
finishReason: props?.finishReason,
|
|
276
|
-
usage: props?.usage,
|
|
277
|
-
runId
|
|
278
|
-
});
|
|
279
|
-
if (props?.response?.headers?.["x-ratelimit-remaining-tokens"] && parseInt(props?.response?.headers?.["x-ratelimit-remaining-tokens"], 10) < 2e3) {
|
|
280
|
-
this.logger.warn("Rate limit approaching, waiting 10 seconds", {
|
|
281
|
-
runId
|
|
282
|
-
});
|
|
283
|
-
await delay(10 * 1e3);
|
|
284
|
-
}
|
|
285
|
-
},
|
|
286
|
-
...rest
|
|
287
|
-
};
|
|
288
|
-
let schema;
|
|
289
|
-
if (experimental_output) {
|
|
290
|
-
this.logger.debug("[LLM] - Using experimental output", {
|
|
291
|
-
runId
|
|
292
|
-
});
|
|
293
|
-
if (typeof experimental_output.parse === "function") {
|
|
294
|
-
schema = experimental_output;
|
|
295
|
-
if (schema instanceof z.ZodArray) {
|
|
296
|
-
schema = schema._def.type;
|
|
297
|
-
}
|
|
298
|
-
} else {
|
|
299
|
-
schema = jsonSchema(experimental_output);
|
|
300
|
-
}
|
|
301
|
-
}
|
|
302
|
-
return await generateText({
|
|
303
|
-
messages,
|
|
304
|
-
...argsForExecute,
|
|
305
|
-
experimental_telemetry: {
|
|
306
|
-
...this.experimental_telemetry,
|
|
307
|
-
...telemetry
|
|
308
|
-
},
|
|
309
|
-
experimental_output: schema ? Output.object({
|
|
310
|
-
schema
|
|
311
|
-
}) : void 0
|
|
312
|
-
});
|
|
313
|
-
}
|
|
314
|
-
async __textObject({
|
|
315
|
-
messages,
|
|
316
|
-
onStepFinish,
|
|
317
|
-
maxSteps = 5,
|
|
318
|
-
tools,
|
|
319
|
-
convertedTools,
|
|
320
|
-
structuredOutput,
|
|
321
|
-
runId,
|
|
322
|
-
temperature,
|
|
323
|
-
toolChoice = "auto",
|
|
324
|
-
telemetry,
|
|
325
|
-
threadId,
|
|
326
|
-
resourceId,
|
|
327
|
-
memory,
|
|
328
|
-
runtimeContext,
|
|
329
|
-
...rest
|
|
330
|
-
}) {
|
|
331
|
-
const model = this.#model;
|
|
332
|
-
this.logger.debug(`[LLM] - Generating a text object`, {
|
|
333
|
-
runId
|
|
334
|
-
});
|
|
335
|
-
const finalTools = convertedTools || this.convertTools({
|
|
336
|
-
tools,
|
|
337
|
-
runId,
|
|
338
|
-
threadId,
|
|
339
|
-
resourceId,
|
|
340
|
-
memory,
|
|
341
|
-
runtimeContext
|
|
342
|
-
});
|
|
343
|
-
const argsForExecute = {
|
|
344
|
-
model,
|
|
345
|
-
temperature,
|
|
346
|
-
tools: {
|
|
347
|
-
...finalTools
|
|
348
|
-
},
|
|
349
|
-
maxSteps,
|
|
350
|
-
toolChoice,
|
|
351
|
-
onStepFinish: async props => {
|
|
352
|
-
void onStepFinish?.(props);
|
|
353
|
-
this.logger.debug("[LLM] - Step Change:", {
|
|
354
|
-
text: props?.text,
|
|
355
|
-
toolCalls: props?.toolCalls,
|
|
356
|
-
toolResults: props?.toolResults,
|
|
357
|
-
finishReason: props?.finishReason,
|
|
358
|
-
usage: props?.usage,
|
|
359
|
-
runId
|
|
360
|
-
});
|
|
361
|
-
if (props?.response?.headers?.["x-ratelimit-remaining-tokens"] && parseInt(props?.response?.headers?.["x-ratelimit-remaining-tokens"], 10) < 2e3) {
|
|
362
|
-
this.logger.warn("Rate limit approaching, waiting 10 seconds", {
|
|
363
|
-
runId
|
|
364
|
-
});
|
|
365
|
-
await delay(10 * 1e3);
|
|
366
|
-
}
|
|
367
|
-
},
|
|
368
|
-
...rest
|
|
369
|
-
};
|
|
370
|
-
let schema;
|
|
371
|
-
let output = "object";
|
|
372
|
-
if (typeof structuredOutput.parse === "function") {
|
|
373
|
-
schema = structuredOutput;
|
|
374
|
-
if (schema instanceof z.ZodArray) {
|
|
375
|
-
output = "array";
|
|
376
|
-
schema = schema._def.type;
|
|
377
|
-
}
|
|
378
|
-
} else {
|
|
379
|
-
schema = jsonSchema(structuredOutput);
|
|
380
|
-
}
|
|
381
|
-
return await generateObject({
|
|
382
|
-
messages,
|
|
383
|
-
...argsForExecute,
|
|
384
|
-
output,
|
|
385
|
-
schema,
|
|
386
|
-
experimental_telemetry: {
|
|
387
|
-
...this.experimental_telemetry,
|
|
388
|
-
...telemetry
|
|
389
|
-
}
|
|
390
|
-
});
|
|
391
|
-
}
|
|
392
|
-
async __stream({
|
|
393
|
-
messages,
|
|
394
|
-
onStepFinish,
|
|
395
|
-
onFinish,
|
|
396
|
-
maxSteps = 5,
|
|
397
|
-
tools,
|
|
398
|
-
convertedTools,
|
|
399
|
-
runId,
|
|
400
|
-
temperature,
|
|
401
|
-
toolChoice = "auto",
|
|
402
|
-
experimental_output,
|
|
403
|
-
telemetry,
|
|
404
|
-
threadId,
|
|
405
|
-
resourceId,
|
|
406
|
-
memory,
|
|
407
|
-
runtimeContext,
|
|
408
|
-
...rest
|
|
409
|
-
}) {
|
|
410
|
-
const model = this.#model;
|
|
411
|
-
this.logger.debug(`[LLM] - Streaming text`, {
|
|
412
|
-
runId,
|
|
413
|
-
threadId,
|
|
414
|
-
resourceId,
|
|
415
|
-
messages,
|
|
416
|
-
maxSteps,
|
|
417
|
-
tools: Object.keys(tools || convertedTools || {})
|
|
418
|
-
});
|
|
419
|
-
const finalTools = convertedTools || this.convertTools({
|
|
420
|
-
tools,
|
|
421
|
-
runId,
|
|
422
|
-
threadId,
|
|
423
|
-
resourceId,
|
|
424
|
-
memory,
|
|
425
|
-
runtimeContext
|
|
426
|
-
});
|
|
427
|
-
const argsForExecute = {
|
|
428
|
-
model,
|
|
429
|
-
temperature,
|
|
430
|
-
tools: {
|
|
431
|
-
...finalTools
|
|
432
|
-
},
|
|
433
|
-
maxSteps,
|
|
434
|
-
toolChoice,
|
|
435
|
-
onStepFinish: async props => {
|
|
436
|
-
void onStepFinish?.(props);
|
|
437
|
-
this.logger.debug("[LLM] - Stream Step Change:", {
|
|
438
|
-
text: props?.text,
|
|
439
|
-
toolCalls: props?.toolCalls,
|
|
440
|
-
toolResults: props?.toolResults,
|
|
441
|
-
finishReason: props?.finishReason,
|
|
442
|
-
usage: props?.usage,
|
|
443
|
-
runId
|
|
444
|
-
});
|
|
445
|
-
if (props?.response?.headers?.["x-ratelimit-remaining-tokens"] && parseInt(props?.response?.headers?.["x-ratelimit-remaining-tokens"], 10) < 2e3) {
|
|
446
|
-
this.logger.warn("Rate limit approaching, waiting 10 seconds", {
|
|
447
|
-
runId
|
|
448
|
-
});
|
|
449
|
-
await delay(10 * 1e3);
|
|
450
|
-
}
|
|
451
|
-
},
|
|
452
|
-
onFinish: async props => {
|
|
453
|
-
void onFinish?.(props);
|
|
454
|
-
this.logger.debug("[LLM] - Stream Finished:", {
|
|
455
|
-
text: props?.text,
|
|
456
|
-
toolCalls: props?.toolCalls,
|
|
457
|
-
toolResults: props?.toolResults,
|
|
458
|
-
finishReason: props?.finishReason,
|
|
459
|
-
usage: props?.usage,
|
|
460
|
-
runId,
|
|
461
|
-
threadId,
|
|
462
|
-
resourceId
|
|
463
|
-
});
|
|
464
|
-
},
|
|
465
|
-
...rest
|
|
466
|
-
};
|
|
467
|
-
let schema;
|
|
468
|
-
if (experimental_output) {
|
|
469
|
-
this.logger.debug("[LLM] - Using experimental output", {
|
|
470
|
-
runId
|
|
471
|
-
});
|
|
472
|
-
if (typeof experimental_output.parse === "function") {
|
|
473
|
-
schema = experimental_output;
|
|
474
|
-
if (schema instanceof z.ZodArray) {
|
|
475
|
-
schema = schema._def.type;
|
|
476
|
-
}
|
|
477
|
-
} else {
|
|
478
|
-
schema = jsonSchema(experimental_output);
|
|
479
|
-
}
|
|
480
|
-
}
|
|
481
|
-
return await streamText({
|
|
482
|
-
messages,
|
|
483
|
-
...argsForExecute,
|
|
484
|
-
experimental_telemetry: {
|
|
485
|
-
...this.experimental_telemetry,
|
|
486
|
-
...telemetry
|
|
487
|
-
},
|
|
488
|
-
experimental_output: schema ? Output.object({
|
|
489
|
-
schema
|
|
490
|
-
}) : void 0
|
|
491
|
-
});
|
|
492
|
-
}
|
|
493
|
-
async __streamObject({
|
|
494
|
-
messages,
|
|
495
|
-
runId,
|
|
496
|
-
tools,
|
|
497
|
-
convertedTools,
|
|
498
|
-
maxSteps = 5,
|
|
499
|
-
toolChoice = "auto",
|
|
500
|
-
runtimeContext,
|
|
501
|
-
threadId,
|
|
502
|
-
resourceId,
|
|
503
|
-
memory,
|
|
504
|
-
temperature,
|
|
505
|
-
onStepFinish,
|
|
506
|
-
onFinish,
|
|
507
|
-
structuredOutput,
|
|
508
|
-
telemetry,
|
|
509
|
-
...rest
|
|
510
|
-
}) {
|
|
511
|
-
const model = this.#model;
|
|
512
|
-
this.logger.debug(`[LLM] - Streaming structured output`, {
|
|
513
|
-
runId,
|
|
514
|
-
messages,
|
|
515
|
-
maxSteps,
|
|
516
|
-
tools: Object.keys(tools || convertedTools || {})
|
|
517
|
-
});
|
|
518
|
-
const finalTools = convertedTools || this.convertTools({
|
|
519
|
-
tools,
|
|
520
|
-
runId,
|
|
521
|
-
threadId,
|
|
522
|
-
resourceId,
|
|
523
|
-
memory,
|
|
524
|
-
runtimeContext
|
|
525
|
-
});
|
|
526
|
-
const argsForExecute = {
|
|
527
|
-
model,
|
|
528
|
-
temperature,
|
|
529
|
-
tools: {
|
|
530
|
-
...finalTools
|
|
531
|
-
},
|
|
532
|
-
maxSteps,
|
|
533
|
-
toolChoice,
|
|
534
|
-
onStepFinish: async props => {
|
|
535
|
-
void onStepFinish?.(props);
|
|
536
|
-
this.logger.debug("[LLM] - Stream Step Change:", {
|
|
537
|
-
text: props?.text,
|
|
538
|
-
toolCalls: props?.toolCalls,
|
|
539
|
-
toolResults: props?.toolResults,
|
|
540
|
-
finishReason: props?.finishReason,
|
|
541
|
-
usage: props?.usage,
|
|
542
|
-
runId,
|
|
543
|
-
threadId,
|
|
544
|
-
resourceId
|
|
545
|
-
});
|
|
546
|
-
if (props?.response?.headers?.["x-ratelimit-remaining-tokens"] && parseInt(props?.response?.headers?.["x-ratelimit-remaining-tokens"], 10) < 2e3) {
|
|
547
|
-
this.logger.warn("Rate limit approaching, waiting 10 seconds", {
|
|
548
|
-
runId
|
|
549
|
-
});
|
|
550
|
-
await delay(10 * 1e3);
|
|
551
|
-
}
|
|
552
|
-
},
|
|
553
|
-
onFinish: async props => {
|
|
554
|
-
void onFinish?.(props);
|
|
555
|
-
this.logger.debug("[LLM] - Stream Finished:", {
|
|
556
|
-
text: props?.text,
|
|
557
|
-
toolCalls: props?.toolCalls,
|
|
558
|
-
toolResults: props?.toolResults,
|
|
559
|
-
finishReason: props?.finishReason,
|
|
560
|
-
usage: props?.usage,
|
|
561
|
-
runId,
|
|
562
|
-
threadId,
|
|
563
|
-
resourceId
|
|
564
|
-
});
|
|
565
|
-
},
|
|
566
|
-
...rest
|
|
567
|
-
};
|
|
568
|
-
let schema;
|
|
569
|
-
let output = "object";
|
|
570
|
-
if (typeof structuredOutput.parse === "function") {
|
|
571
|
-
schema = structuredOutput;
|
|
572
|
-
if (schema instanceof z.ZodArray) {
|
|
573
|
-
output = "array";
|
|
574
|
-
schema = schema._def.type;
|
|
575
|
-
}
|
|
576
|
-
} else {
|
|
577
|
-
schema = jsonSchema(structuredOutput);
|
|
578
|
-
}
|
|
579
|
-
return streamObject({
|
|
580
|
-
messages,
|
|
581
|
-
...argsForExecute,
|
|
582
|
-
output,
|
|
583
|
-
schema,
|
|
584
|
-
experimental_telemetry: {
|
|
585
|
-
...this.experimental_telemetry,
|
|
586
|
-
...telemetry
|
|
587
|
-
}
|
|
588
|
-
});
|
|
589
|
-
}
|
|
590
|
-
async generate(messages, {
|
|
591
|
-
maxSteps = 5,
|
|
592
|
-
output,
|
|
593
|
-
...rest
|
|
594
|
-
}) {
|
|
595
|
-
const msgs = this.convertToMessages(messages);
|
|
596
|
-
if (!output) {
|
|
597
|
-
return await this.__text({
|
|
598
|
-
messages: msgs,
|
|
599
|
-
maxSteps,
|
|
600
|
-
...rest
|
|
601
|
-
});
|
|
602
|
-
}
|
|
603
|
-
return await this.__textObject({
|
|
604
|
-
messages: msgs,
|
|
605
|
-
structuredOutput: output,
|
|
606
|
-
maxSteps,
|
|
607
|
-
...rest
|
|
608
|
-
});
|
|
609
|
-
}
|
|
610
|
-
async stream(messages, {
|
|
611
|
-
maxSteps = 5,
|
|
612
|
-
output,
|
|
613
|
-
...rest
|
|
614
|
-
}) {
|
|
615
|
-
const msgs = this.convertToMessages(messages);
|
|
616
|
-
if (!output) {
|
|
617
|
-
return await this.__stream({
|
|
618
|
-
messages: msgs,
|
|
619
|
-
maxSteps,
|
|
620
|
-
...rest
|
|
621
|
-
});
|
|
622
|
-
}
|
|
623
|
-
return await this.__streamObject({
|
|
624
|
-
messages: msgs,
|
|
625
|
-
structuredOutput: output,
|
|
626
|
-
maxSteps,
|
|
627
|
-
...rest
|
|
628
|
-
});
|
|
629
|
-
}
|
|
630
|
-
convertToUIMessages(messages) {
|
|
631
|
-
function addToolMessageToChat({
|
|
632
|
-
toolMessage,
|
|
633
|
-
messages: messages2,
|
|
634
|
-
toolResultContents
|
|
635
|
-
}) {
|
|
636
|
-
const chatMessages2 = messages2.map(message => {
|
|
637
|
-
if (message.toolInvocations) {
|
|
638
|
-
return {
|
|
639
|
-
...message,
|
|
640
|
-
toolInvocations: message.toolInvocations.map(toolInvocation => {
|
|
641
|
-
const toolResult = toolMessage.content.find(tool => tool.toolCallId === toolInvocation.toolCallId);
|
|
642
|
-
if (toolResult) {
|
|
643
|
-
return {
|
|
644
|
-
...toolInvocation,
|
|
645
|
-
state: "result",
|
|
646
|
-
result: toolResult.result
|
|
647
|
-
};
|
|
648
|
-
}
|
|
649
|
-
return toolInvocation;
|
|
650
|
-
})
|
|
651
|
-
};
|
|
652
|
-
}
|
|
653
|
-
return message;
|
|
654
|
-
});
|
|
655
|
-
const resultContents = [...toolResultContents, ...toolMessage.content];
|
|
656
|
-
return {
|
|
657
|
-
chatMessages: chatMessages2,
|
|
658
|
-
toolResultContents: resultContents
|
|
659
|
-
};
|
|
660
|
-
}
|
|
661
|
-
const {
|
|
662
|
-
chatMessages
|
|
663
|
-
} = messages.reduce((obj, message) => {
|
|
664
|
-
if (message.role === "tool") {
|
|
665
|
-
return addToolMessageToChat({
|
|
666
|
-
toolMessage: message,
|
|
667
|
-
messages: obj.chatMessages,
|
|
668
|
-
toolResultContents: obj.toolResultContents
|
|
669
|
-
});
|
|
670
|
-
}
|
|
671
|
-
let textContent = "";
|
|
672
|
-
let toolInvocations = [];
|
|
673
|
-
if (typeof message.content === "string") {
|
|
674
|
-
textContent = message.content;
|
|
675
|
-
} else if (typeof message.content === "number") {
|
|
676
|
-
textContent = String(message.content);
|
|
677
|
-
} else if (Array.isArray(message.content)) {
|
|
678
|
-
for (const content of message.content) {
|
|
679
|
-
if (content.type === "text") {
|
|
680
|
-
textContent += content.text;
|
|
681
|
-
} else if (content.type === "tool-call") {
|
|
682
|
-
const toolResult = obj.toolResultContents.find(tool => tool.toolCallId === content.toolCallId);
|
|
683
|
-
toolInvocations.push({
|
|
684
|
-
state: toolResult ? "result" : "call",
|
|
685
|
-
toolCallId: content.toolCallId,
|
|
686
|
-
toolName: content.toolName,
|
|
687
|
-
args: content.args,
|
|
688
|
-
result: toolResult?.result
|
|
689
|
-
});
|
|
690
|
-
}
|
|
691
|
-
}
|
|
692
|
-
}
|
|
693
|
-
obj.chatMessages.push({
|
|
694
|
-
id: message.id,
|
|
695
|
-
role: message.role,
|
|
696
|
-
content: textContent,
|
|
697
|
-
toolInvocations
|
|
698
|
-
});
|
|
699
|
-
return obj;
|
|
700
|
-
}, {
|
|
701
|
-
chatMessages: [],
|
|
702
|
-
toolResultContents: []
|
|
703
|
-
});
|
|
704
|
-
return chatMessages;
|
|
705
|
-
}
|
|
706
|
-
};
|
|
707
|
-
|
|
708
|
-
// src/agent/index.ts
|
|
709
55
|
var _Agent_decorators, _init, _a;
|
|
710
56
|
_Agent_decorators = [InstrumentClass({
|
|
711
57
|
prefix: "agent",
|
|
712
58
|
excludeMethods: ["hasOwnMemory", "getMemory", "__primitive", "__setTools", "__setLogger", "__setTelemetry", "log"]
|
|
713
59
|
})];
|
|
714
60
|
var Agent = class extends (_a = MastraBase) {
|
|
61
|
+
id;
|
|
715
62
|
name;
|
|
716
63
|
llm;
|
|
717
64
|
instructions;
|
|
@@ -730,6 +77,7 @@ var Agent = class extends (_a = MastraBase) {
|
|
|
730
77
|
component: RegisteredLogger.AGENT
|
|
731
78
|
});
|
|
732
79
|
this.name = config.name;
|
|
80
|
+
this.id = config.name;
|
|
733
81
|
this.instructions = config.instructions;
|
|
734
82
|
if (!config.model) {
|
|
735
83
|
throw new Error(`LanguageModel is required to create an Agent. Please provide the 'model'.`);
|
|
@@ -4727,4 +4075,4 @@ var Workflow = class extends MastraBase {
|
|
|
4727
4075
|
}
|
|
4728
4076
|
};
|
|
4729
4077
|
|
|
4730
|
-
export { Agent, Step, WhenConditionReturnValue, Workflow, agentToStep,
|
|
4078
|
+
export { Agent, Step, WhenConditionReturnValue, Workflow, agentToStep, getActivePathsAndStatus, getResultActivePaths, getStepResult, getSuspendedPaths, isAgent, isConditionalKey, isErrorEvent, isFinalState, isLimboState, isTransitionEvent, isVariableReference, isWorkflow, mergeChildValue, recursivelyCheckForFinalState, resolveVariables, updateStepInHierarchy, workflowToStep };
|