@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
|
@@ -0,0 +1,588 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var chunkYTTBFAQJ_cjs = require('./chunk-YTTBFAQJ.cjs');
|
|
4
|
+
var chunkD63P5O4Q_cjs = require('./chunk-D63P5O4Q.cjs');
|
|
5
|
+
var chunkO7IW545H_cjs = require('./chunk-O7IW545H.cjs');
|
|
6
|
+
var ai = require('ai');
|
|
7
|
+
var zod = require('zod');
|
|
8
|
+
|
|
9
|
+
// src/llm/model/base.ts
|
|
10
|
+
var MastraLLMBase = class extends chunkD63P5O4Q_cjs.MastraBase {
|
|
11
|
+
// @ts-ignore
|
|
12
|
+
#mastra;
|
|
13
|
+
#model;
|
|
14
|
+
constructor({ name, model }) {
|
|
15
|
+
super({
|
|
16
|
+
component: chunkO7IW545H_cjs.RegisteredLogger.LLM,
|
|
17
|
+
name
|
|
18
|
+
});
|
|
19
|
+
this.#model = model;
|
|
20
|
+
}
|
|
21
|
+
getProvider() {
|
|
22
|
+
return this.#model.provider;
|
|
23
|
+
}
|
|
24
|
+
getModelId() {
|
|
25
|
+
return this.#model.modelId;
|
|
26
|
+
}
|
|
27
|
+
getModel() {
|
|
28
|
+
return this.#model;
|
|
29
|
+
}
|
|
30
|
+
convertToMessages(messages) {
|
|
31
|
+
if (Array.isArray(messages)) {
|
|
32
|
+
return messages.map((m) => {
|
|
33
|
+
if (typeof m === "string") {
|
|
34
|
+
return {
|
|
35
|
+
role: "user",
|
|
36
|
+
content: m
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
return m;
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
return [
|
|
43
|
+
{
|
|
44
|
+
role: "user",
|
|
45
|
+
content: messages
|
|
46
|
+
}
|
|
47
|
+
];
|
|
48
|
+
}
|
|
49
|
+
__registerPrimitives(p) {
|
|
50
|
+
if (p.telemetry) {
|
|
51
|
+
this.__setTelemetry(p.telemetry);
|
|
52
|
+
}
|
|
53
|
+
if (p.logger) {
|
|
54
|
+
this.__setLogger(p.logger);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
__registerMastra(p) {
|
|
58
|
+
this.#mastra = p;
|
|
59
|
+
}
|
|
60
|
+
async __text(input) {
|
|
61
|
+
this.logger.debug(`[LLMs:${this.name}] Generating text.`, { input });
|
|
62
|
+
throw new Error("Method not implemented.");
|
|
63
|
+
}
|
|
64
|
+
async __textObject(input) {
|
|
65
|
+
this.logger.debug(`[LLMs:${this.name}] Generating object.`, { input });
|
|
66
|
+
throw new Error("Method not implemented.");
|
|
67
|
+
}
|
|
68
|
+
async generate(messages, options) {
|
|
69
|
+
this.logger.debug(`[LLMs:${this.name}] Generating text.`, { messages, options });
|
|
70
|
+
throw new Error("Method not implemented.");
|
|
71
|
+
}
|
|
72
|
+
async __stream(input) {
|
|
73
|
+
this.logger.debug(`[LLMs:${this.name}] Streaming text.`, { input });
|
|
74
|
+
throw new Error("Method not implemented.");
|
|
75
|
+
}
|
|
76
|
+
async __streamObject(input) {
|
|
77
|
+
this.logger.debug(`[LLMs:${this.name}] Streaming object.`, { input });
|
|
78
|
+
throw new Error("Method not implemented.");
|
|
79
|
+
}
|
|
80
|
+
async stream(messages, options) {
|
|
81
|
+
this.logger.debug(`[LLMs:${this.name}] Streaming text.`, { messages, options });
|
|
82
|
+
throw new Error("Method not implemented.");
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
// src/llm/model/model.ts
|
|
87
|
+
var MastraLLM = class extends MastraLLMBase {
|
|
88
|
+
#model;
|
|
89
|
+
#mastra;
|
|
90
|
+
constructor({ model, mastra }) {
|
|
91
|
+
super({ name: "aisdk", model });
|
|
92
|
+
this.#model = model;
|
|
93
|
+
if (mastra) {
|
|
94
|
+
this.#mastra = mastra;
|
|
95
|
+
if (mastra.getLogger()) {
|
|
96
|
+
this.__setLogger(mastra.getLogger());
|
|
97
|
+
}
|
|
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
|
+
getProvider() {
|
|
112
|
+
return this.#model.provider;
|
|
113
|
+
}
|
|
114
|
+
getModelId() {
|
|
115
|
+
return this.#model.modelId;
|
|
116
|
+
}
|
|
117
|
+
getModel() {
|
|
118
|
+
return this.#model;
|
|
119
|
+
}
|
|
120
|
+
convertTools({
|
|
121
|
+
tools,
|
|
122
|
+
runId,
|
|
123
|
+
threadId,
|
|
124
|
+
resourceId,
|
|
125
|
+
memory,
|
|
126
|
+
runtimeContext
|
|
127
|
+
}) {
|
|
128
|
+
this.logger.debug("Starting tool conversion for LLM");
|
|
129
|
+
let mastraProxy = void 0;
|
|
130
|
+
const logger = this.logger;
|
|
131
|
+
if (this.#mastra) {
|
|
132
|
+
mastraProxy = chunkYTTBFAQJ_cjs.createMastraProxy({ mastra: this.#mastra, logger });
|
|
133
|
+
}
|
|
134
|
+
const converted = Object.entries(tools || {}).reduce(
|
|
135
|
+
(memo, value) => {
|
|
136
|
+
const k = value[0];
|
|
137
|
+
const tool = value[1];
|
|
138
|
+
if (tool) {
|
|
139
|
+
const options = {
|
|
140
|
+
name: k,
|
|
141
|
+
runId,
|
|
142
|
+
threadId,
|
|
143
|
+
resourceId,
|
|
144
|
+
logger: this.logger,
|
|
145
|
+
memory,
|
|
146
|
+
mastra: mastraProxy,
|
|
147
|
+
runtimeContext
|
|
148
|
+
};
|
|
149
|
+
memo[k] = chunkYTTBFAQJ_cjs.makeCoreTool(tool, options);
|
|
150
|
+
}
|
|
151
|
+
return memo;
|
|
152
|
+
},
|
|
153
|
+
{}
|
|
154
|
+
);
|
|
155
|
+
this.logger.debug(`Converted tools for LLM`);
|
|
156
|
+
return converted;
|
|
157
|
+
}
|
|
158
|
+
async __text({
|
|
159
|
+
runId,
|
|
160
|
+
messages,
|
|
161
|
+
maxSteps = 5,
|
|
162
|
+
tools,
|
|
163
|
+
convertedTools,
|
|
164
|
+
temperature,
|
|
165
|
+
toolChoice = "auto",
|
|
166
|
+
onStepFinish,
|
|
167
|
+
experimental_output,
|
|
168
|
+
telemetry,
|
|
169
|
+
threadId,
|
|
170
|
+
resourceId,
|
|
171
|
+
memory,
|
|
172
|
+
runtimeContext,
|
|
173
|
+
...rest
|
|
174
|
+
}) {
|
|
175
|
+
const model = this.#model;
|
|
176
|
+
this.logger.debug(`[LLM] - Generating text`, {
|
|
177
|
+
runId,
|
|
178
|
+
messages,
|
|
179
|
+
maxSteps,
|
|
180
|
+
threadId,
|
|
181
|
+
resourceId,
|
|
182
|
+
tools: Object.keys(tools || convertedTools || {})
|
|
183
|
+
});
|
|
184
|
+
const finalTools = convertedTools || this.convertTools({ tools, runId, threadId, resourceId, memory, runtimeContext });
|
|
185
|
+
const argsForExecute = {
|
|
186
|
+
model,
|
|
187
|
+
temperature,
|
|
188
|
+
tools: {
|
|
189
|
+
...finalTools
|
|
190
|
+
},
|
|
191
|
+
toolChoice,
|
|
192
|
+
maxSteps,
|
|
193
|
+
onStepFinish: async (props) => {
|
|
194
|
+
void onStepFinish?.(props);
|
|
195
|
+
this.logger.debug("[LLM] - Step Change:", {
|
|
196
|
+
text: props?.text,
|
|
197
|
+
toolCalls: props?.toolCalls,
|
|
198
|
+
toolResults: props?.toolResults,
|
|
199
|
+
finishReason: props?.finishReason,
|
|
200
|
+
usage: props?.usage,
|
|
201
|
+
runId
|
|
202
|
+
});
|
|
203
|
+
if (props?.response?.headers?.["x-ratelimit-remaining-tokens"] && parseInt(props?.response?.headers?.["x-ratelimit-remaining-tokens"], 10) < 2e3) {
|
|
204
|
+
this.logger.warn("Rate limit approaching, waiting 10 seconds", { runId });
|
|
205
|
+
await chunkYTTBFAQJ_cjs.delay(10 * 1e3);
|
|
206
|
+
}
|
|
207
|
+
},
|
|
208
|
+
...rest
|
|
209
|
+
};
|
|
210
|
+
let schema;
|
|
211
|
+
if (experimental_output) {
|
|
212
|
+
this.logger.debug("[LLM] - Using experimental output", {
|
|
213
|
+
runId
|
|
214
|
+
});
|
|
215
|
+
if (typeof experimental_output.parse === "function") {
|
|
216
|
+
schema = experimental_output;
|
|
217
|
+
if (schema instanceof zod.z.ZodArray) {
|
|
218
|
+
schema = schema._def.type;
|
|
219
|
+
}
|
|
220
|
+
} else {
|
|
221
|
+
schema = ai.jsonSchema(experimental_output);
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
return await ai.generateText({
|
|
225
|
+
messages,
|
|
226
|
+
...argsForExecute,
|
|
227
|
+
experimental_telemetry: {
|
|
228
|
+
...this.experimental_telemetry,
|
|
229
|
+
...telemetry
|
|
230
|
+
},
|
|
231
|
+
experimental_output: schema ? ai.Output.object({
|
|
232
|
+
schema
|
|
233
|
+
}) : void 0
|
|
234
|
+
});
|
|
235
|
+
}
|
|
236
|
+
async __textObject({
|
|
237
|
+
messages,
|
|
238
|
+
onStepFinish,
|
|
239
|
+
maxSteps = 5,
|
|
240
|
+
tools,
|
|
241
|
+
convertedTools,
|
|
242
|
+
structuredOutput,
|
|
243
|
+
runId,
|
|
244
|
+
temperature,
|
|
245
|
+
toolChoice = "auto",
|
|
246
|
+
telemetry,
|
|
247
|
+
threadId,
|
|
248
|
+
resourceId,
|
|
249
|
+
memory,
|
|
250
|
+
runtimeContext,
|
|
251
|
+
...rest
|
|
252
|
+
}) {
|
|
253
|
+
const model = this.#model;
|
|
254
|
+
this.logger.debug(`[LLM] - Generating a text object`, { runId });
|
|
255
|
+
const finalTools = convertedTools || this.convertTools({ tools, runId, threadId, resourceId, memory, runtimeContext });
|
|
256
|
+
const argsForExecute = {
|
|
257
|
+
model,
|
|
258
|
+
temperature,
|
|
259
|
+
tools: {
|
|
260
|
+
...finalTools
|
|
261
|
+
},
|
|
262
|
+
maxSteps,
|
|
263
|
+
toolChoice,
|
|
264
|
+
onStepFinish: async (props) => {
|
|
265
|
+
void onStepFinish?.(props);
|
|
266
|
+
this.logger.debug("[LLM] - Step Change:", {
|
|
267
|
+
text: props?.text,
|
|
268
|
+
toolCalls: props?.toolCalls,
|
|
269
|
+
toolResults: props?.toolResults,
|
|
270
|
+
finishReason: props?.finishReason,
|
|
271
|
+
usage: props?.usage,
|
|
272
|
+
runId
|
|
273
|
+
});
|
|
274
|
+
if (props?.response?.headers?.["x-ratelimit-remaining-tokens"] && parseInt(props?.response?.headers?.["x-ratelimit-remaining-tokens"], 10) < 2e3) {
|
|
275
|
+
this.logger.warn("Rate limit approaching, waiting 10 seconds", { runId });
|
|
276
|
+
await chunkYTTBFAQJ_cjs.delay(10 * 1e3);
|
|
277
|
+
}
|
|
278
|
+
},
|
|
279
|
+
...rest
|
|
280
|
+
};
|
|
281
|
+
let schema;
|
|
282
|
+
let output = "object";
|
|
283
|
+
if (typeof structuredOutput.parse === "function") {
|
|
284
|
+
schema = structuredOutput;
|
|
285
|
+
if (schema instanceof zod.z.ZodArray) {
|
|
286
|
+
output = "array";
|
|
287
|
+
schema = schema._def.type;
|
|
288
|
+
}
|
|
289
|
+
} else {
|
|
290
|
+
schema = ai.jsonSchema(structuredOutput);
|
|
291
|
+
}
|
|
292
|
+
return await ai.generateObject({
|
|
293
|
+
messages,
|
|
294
|
+
...argsForExecute,
|
|
295
|
+
output,
|
|
296
|
+
schema,
|
|
297
|
+
experimental_telemetry: {
|
|
298
|
+
...this.experimental_telemetry,
|
|
299
|
+
...telemetry
|
|
300
|
+
}
|
|
301
|
+
});
|
|
302
|
+
}
|
|
303
|
+
async __stream({
|
|
304
|
+
messages,
|
|
305
|
+
onStepFinish,
|
|
306
|
+
onFinish,
|
|
307
|
+
maxSteps = 5,
|
|
308
|
+
tools,
|
|
309
|
+
convertedTools,
|
|
310
|
+
runId,
|
|
311
|
+
temperature,
|
|
312
|
+
toolChoice = "auto",
|
|
313
|
+
experimental_output,
|
|
314
|
+
telemetry,
|
|
315
|
+
threadId,
|
|
316
|
+
resourceId,
|
|
317
|
+
memory,
|
|
318
|
+
runtimeContext,
|
|
319
|
+
...rest
|
|
320
|
+
}) {
|
|
321
|
+
const model = this.#model;
|
|
322
|
+
this.logger.debug(`[LLM] - Streaming text`, {
|
|
323
|
+
runId,
|
|
324
|
+
threadId,
|
|
325
|
+
resourceId,
|
|
326
|
+
messages,
|
|
327
|
+
maxSteps,
|
|
328
|
+
tools: Object.keys(tools || convertedTools || {})
|
|
329
|
+
});
|
|
330
|
+
const finalTools = convertedTools || this.convertTools({ tools, runId, threadId, resourceId, memory, runtimeContext });
|
|
331
|
+
const argsForExecute = {
|
|
332
|
+
model,
|
|
333
|
+
temperature,
|
|
334
|
+
tools: {
|
|
335
|
+
...finalTools
|
|
336
|
+
},
|
|
337
|
+
maxSteps,
|
|
338
|
+
toolChoice,
|
|
339
|
+
onStepFinish: async (props) => {
|
|
340
|
+
void onStepFinish?.(props);
|
|
341
|
+
this.logger.debug("[LLM] - Stream Step Change:", {
|
|
342
|
+
text: props?.text,
|
|
343
|
+
toolCalls: props?.toolCalls,
|
|
344
|
+
toolResults: props?.toolResults,
|
|
345
|
+
finishReason: props?.finishReason,
|
|
346
|
+
usage: props?.usage,
|
|
347
|
+
runId
|
|
348
|
+
});
|
|
349
|
+
if (props?.response?.headers?.["x-ratelimit-remaining-tokens"] && parseInt(props?.response?.headers?.["x-ratelimit-remaining-tokens"], 10) < 2e3) {
|
|
350
|
+
this.logger.warn("Rate limit approaching, waiting 10 seconds", { runId });
|
|
351
|
+
await chunkYTTBFAQJ_cjs.delay(10 * 1e3);
|
|
352
|
+
}
|
|
353
|
+
},
|
|
354
|
+
onFinish: async (props) => {
|
|
355
|
+
void onFinish?.(props);
|
|
356
|
+
this.logger.debug("[LLM] - Stream Finished:", {
|
|
357
|
+
text: props?.text,
|
|
358
|
+
toolCalls: props?.toolCalls,
|
|
359
|
+
toolResults: props?.toolResults,
|
|
360
|
+
finishReason: props?.finishReason,
|
|
361
|
+
usage: props?.usage,
|
|
362
|
+
runId,
|
|
363
|
+
threadId,
|
|
364
|
+
resourceId
|
|
365
|
+
});
|
|
366
|
+
},
|
|
367
|
+
...rest
|
|
368
|
+
};
|
|
369
|
+
let schema;
|
|
370
|
+
if (experimental_output) {
|
|
371
|
+
this.logger.debug("[LLM] - Using experimental output", {
|
|
372
|
+
runId
|
|
373
|
+
});
|
|
374
|
+
if (typeof experimental_output.parse === "function") {
|
|
375
|
+
schema = experimental_output;
|
|
376
|
+
if (schema instanceof zod.z.ZodArray) {
|
|
377
|
+
schema = schema._def.type;
|
|
378
|
+
}
|
|
379
|
+
} else {
|
|
380
|
+
schema = ai.jsonSchema(experimental_output);
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
return await ai.streamText({
|
|
384
|
+
messages,
|
|
385
|
+
...argsForExecute,
|
|
386
|
+
experimental_telemetry: {
|
|
387
|
+
...this.experimental_telemetry,
|
|
388
|
+
...telemetry
|
|
389
|
+
},
|
|
390
|
+
experimental_output: schema ? ai.Output.object({
|
|
391
|
+
schema
|
|
392
|
+
}) : void 0
|
|
393
|
+
});
|
|
394
|
+
}
|
|
395
|
+
async __streamObject({
|
|
396
|
+
messages,
|
|
397
|
+
runId,
|
|
398
|
+
tools,
|
|
399
|
+
convertedTools,
|
|
400
|
+
maxSteps = 5,
|
|
401
|
+
toolChoice = "auto",
|
|
402
|
+
runtimeContext,
|
|
403
|
+
threadId,
|
|
404
|
+
resourceId,
|
|
405
|
+
memory,
|
|
406
|
+
temperature,
|
|
407
|
+
onStepFinish,
|
|
408
|
+
onFinish,
|
|
409
|
+
structuredOutput,
|
|
410
|
+
telemetry,
|
|
411
|
+
...rest
|
|
412
|
+
}) {
|
|
413
|
+
const model = this.#model;
|
|
414
|
+
this.logger.debug(`[LLM] - Streaming structured output`, {
|
|
415
|
+
runId,
|
|
416
|
+
messages,
|
|
417
|
+
maxSteps,
|
|
418
|
+
tools: Object.keys(tools || convertedTools || {})
|
|
419
|
+
});
|
|
420
|
+
const finalTools = convertedTools || this.convertTools({ tools, runId, threadId, resourceId, memory, runtimeContext });
|
|
421
|
+
const argsForExecute = {
|
|
422
|
+
model,
|
|
423
|
+
temperature,
|
|
424
|
+
tools: {
|
|
425
|
+
...finalTools
|
|
426
|
+
},
|
|
427
|
+
maxSteps,
|
|
428
|
+
toolChoice,
|
|
429
|
+
onStepFinish: async (props) => {
|
|
430
|
+
void onStepFinish?.(props);
|
|
431
|
+
this.logger.debug("[LLM] - Stream Step Change:", {
|
|
432
|
+
text: props?.text,
|
|
433
|
+
toolCalls: props?.toolCalls,
|
|
434
|
+
toolResults: props?.toolResults,
|
|
435
|
+
finishReason: props?.finishReason,
|
|
436
|
+
usage: props?.usage,
|
|
437
|
+
runId,
|
|
438
|
+
threadId,
|
|
439
|
+
resourceId
|
|
440
|
+
});
|
|
441
|
+
if (props?.response?.headers?.["x-ratelimit-remaining-tokens"] && parseInt(props?.response?.headers?.["x-ratelimit-remaining-tokens"], 10) < 2e3) {
|
|
442
|
+
this.logger.warn("Rate limit approaching, waiting 10 seconds", { runId });
|
|
443
|
+
await chunkYTTBFAQJ_cjs.delay(10 * 1e3);
|
|
444
|
+
}
|
|
445
|
+
},
|
|
446
|
+
onFinish: async (props) => {
|
|
447
|
+
void onFinish?.(props);
|
|
448
|
+
this.logger.debug("[LLM] - Stream Finished:", {
|
|
449
|
+
text: props?.text,
|
|
450
|
+
toolCalls: props?.toolCalls,
|
|
451
|
+
toolResults: props?.toolResults,
|
|
452
|
+
finishReason: props?.finishReason,
|
|
453
|
+
usage: props?.usage,
|
|
454
|
+
runId,
|
|
455
|
+
threadId,
|
|
456
|
+
resourceId
|
|
457
|
+
});
|
|
458
|
+
},
|
|
459
|
+
...rest
|
|
460
|
+
};
|
|
461
|
+
let schema;
|
|
462
|
+
let output = "object";
|
|
463
|
+
if (typeof structuredOutput.parse === "function") {
|
|
464
|
+
schema = structuredOutput;
|
|
465
|
+
if (schema instanceof zod.z.ZodArray) {
|
|
466
|
+
output = "array";
|
|
467
|
+
schema = schema._def.type;
|
|
468
|
+
}
|
|
469
|
+
} else {
|
|
470
|
+
schema = ai.jsonSchema(structuredOutput);
|
|
471
|
+
}
|
|
472
|
+
return ai.streamObject({
|
|
473
|
+
messages,
|
|
474
|
+
...argsForExecute,
|
|
475
|
+
output,
|
|
476
|
+
schema,
|
|
477
|
+
experimental_telemetry: {
|
|
478
|
+
...this.experimental_telemetry,
|
|
479
|
+
...telemetry
|
|
480
|
+
}
|
|
481
|
+
});
|
|
482
|
+
}
|
|
483
|
+
async generate(messages, { maxSteps = 5, output, ...rest }) {
|
|
484
|
+
const msgs = this.convertToMessages(messages);
|
|
485
|
+
if (!output) {
|
|
486
|
+
return await this.__text({
|
|
487
|
+
messages: msgs,
|
|
488
|
+
maxSteps,
|
|
489
|
+
...rest
|
|
490
|
+
});
|
|
491
|
+
}
|
|
492
|
+
return await this.__textObject({
|
|
493
|
+
messages: msgs,
|
|
494
|
+
structuredOutput: output,
|
|
495
|
+
maxSteps,
|
|
496
|
+
...rest
|
|
497
|
+
});
|
|
498
|
+
}
|
|
499
|
+
async stream(messages, { maxSteps = 5, output, ...rest }) {
|
|
500
|
+
const msgs = this.convertToMessages(messages);
|
|
501
|
+
if (!output) {
|
|
502
|
+
return await this.__stream({
|
|
503
|
+
messages: msgs,
|
|
504
|
+
maxSteps,
|
|
505
|
+
...rest
|
|
506
|
+
});
|
|
507
|
+
}
|
|
508
|
+
return await this.__streamObject({
|
|
509
|
+
messages: msgs,
|
|
510
|
+
structuredOutput: output,
|
|
511
|
+
maxSteps,
|
|
512
|
+
...rest
|
|
513
|
+
});
|
|
514
|
+
}
|
|
515
|
+
convertToUIMessages(messages) {
|
|
516
|
+
function addToolMessageToChat({
|
|
517
|
+
toolMessage,
|
|
518
|
+
messages: messages2,
|
|
519
|
+
toolResultContents
|
|
520
|
+
}) {
|
|
521
|
+
const chatMessages2 = messages2.map((message) => {
|
|
522
|
+
if (message.toolInvocations) {
|
|
523
|
+
return {
|
|
524
|
+
...message,
|
|
525
|
+
toolInvocations: message.toolInvocations.map((toolInvocation) => {
|
|
526
|
+
const toolResult = toolMessage.content.find((tool) => tool.toolCallId === toolInvocation.toolCallId);
|
|
527
|
+
if (toolResult) {
|
|
528
|
+
return {
|
|
529
|
+
...toolInvocation,
|
|
530
|
+
state: "result",
|
|
531
|
+
result: toolResult.result
|
|
532
|
+
};
|
|
533
|
+
}
|
|
534
|
+
return toolInvocation;
|
|
535
|
+
})
|
|
536
|
+
};
|
|
537
|
+
}
|
|
538
|
+
return message;
|
|
539
|
+
});
|
|
540
|
+
const resultContents = [...toolResultContents, ...toolMessage.content];
|
|
541
|
+
return { chatMessages: chatMessages2, toolResultContents: resultContents };
|
|
542
|
+
}
|
|
543
|
+
const { chatMessages } = messages.reduce(
|
|
544
|
+
(obj, message) => {
|
|
545
|
+
if (message.role === "tool") {
|
|
546
|
+
return addToolMessageToChat({
|
|
547
|
+
toolMessage: message,
|
|
548
|
+
messages: obj.chatMessages,
|
|
549
|
+
toolResultContents: obj.toolResultContents
|
|
550
|
+
});
|
|
551
|
+
}
|
|
552
|
+
let textContent = "";
|
|
553
|
+
let toolInvocations = [];
|
|
554
|
+
if (typeof message.content === "string") {
|
|
555
|
+
textContent = message.content;
|
|
556
|
+
} else if (typeof message.content === "number") {
|
|
557
|
+
textContent = String(message.content);
|
|
558
|
+
} else if (Array.isArray(message.content)) {
|
|
559
|
+
for (const content of message.content) {
|
|
560
|
+
if (content.type === "text") {
|
|
561
|
+
textContent += content.text;
|
|
562
|
+
} else if (content.type === "tool-call") {
|
|
563
|
+
const toolResult = obj.toolResultContents.find((tool) => tool.toolCallId === content.toolCallId);
|
|
564
|
+
toolInvocations.push({
|
|
565
|
+
state: toolResult ? "result" : "call",
|
|
566
|
+
toolCallId: content.toolCallId,
|
|
567
|
+
toolName: content.toolName,
|
|
568
|
+
args: content.args,
|
|
569
|
+
result: toolResult?.result
|
|
570
|
+
});
|
|
571
|
+
}
|
|
572
|
+
}
|
|
573
|
+
}
|
|
574
|
+
obj.chatMessages.push({
|
|
575
|
+
id: message.id,
|
|
576
|
+
role: message.role,
|
|
577
|
+
content: textContent,
|
|
578
|
+
toolInvocations
|
|
579
|
+
});
|
|
580
|
+
return obj;
|
|
581
|
+
},
|
|
582
|
+
{ chatMessages: [], toolResultContents: [] }
|
|
583
|
+
);
|
|
584
|
+
return chatMessages;
|
|
585
|
+
}
|
|
586
|
+
};
|
|
587
|
+
|
|
588
|
+
exports.MastraLLM = MastraLLM;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { DefaultProxyStorage, augmentWithInit } from './chunk-
|
|
1
|
+
import { DefaultProxyStorage, augmentWithInit } from './chunk-SGTFVHOZ.js';
|
|
2
2
|
import { InstrumentClass, Telemetry } from './chunk-HFOT2NFM.js';
|
|
3
3
|
import { createLogger, LogLevel, noopLogger } from './chunk-2BVZNKLX.js';
|
|
4
4
|
import { __decoratorStart, __decorateElement, __runInitializers } from './chunk-C6A6W6XS.js';
|
|
@@ -14,6 +14,7 @@ var Mastra = class {
|
|
|
14
14
|
#agents;
|
|
15
15
|
#logger;
|
|
16
16
|
#workflows;
|
|
17
|
+
#vnext_workflows;
|
|
17
18
|
#tts;
|
|
18
19
|
#deployer;
|
|
19
20
|
#serverMiddleware = [];
|
|
@@ -181,6 +182,22 @@ This is a warning for now, but will throw an error in the future
|
|
|
181
182
|
}
|
|
182
183
|
});
|
|
183
184
|
}
|
|
185
|
+
this.#vnext_workflows = {};
|
|
186
|
+
if (config?.vnext_workflows) {
|
|
187
|
+
Object.entries(config.vnext_workflows).forEach(([key, workflow]) => {
|
|
188
|
+
workflow.__registerMastra(this);
|
|
189
|
+
workflow.__registerPrimitives({
|
|
190
|
+
logger: this.getLogger(),
|
|
191
|
+
telemetry: this.#telemetry,
|
|
192
|
+
storage: this.storage,
|
|
193
|
+
memory: this.memory,
|
|
194
|
+
agents,
|
|
195
|
+
tts: this.#tts,
|
|
196
|
+
vectors: this.#vectors
|
|
197
|
+
});
|
|
198
|
+
this.#vnext_workflows[key] = workflow;
|
|
199
|
+
});
|
|
200
|
+
}
|
|
184
201
|
if (config?.server) {
|
|
185
202
|
this.#server = config.server;
|
|
186
203
|
}
|
|
@@ -225,6 +242,20 @@ This is a warning for now, but will throw an error in the future
|
|
|
225
242
|
}
|
|
226
243
|
return workflow;
|
|
227
244
|
}
|
|
245
|
+
vnext_getWorkflow(id, {
|
|
246
|
+
serialized
|
|
247
|
+
} = {}) {
|
|
248
|
+
const workflow = this.#vnext_workflows?.[id];
|
|
249
|
+
if (!workflow) {
|
|
250
|
+
throw new Error(`Workflow with ID ${String(id)} not found`);
|
|
251
|
+
}
|
|
252
|
+
if (serialized) {
|
|
253
|
+
return {
|
|
254
|
+
name: workflow.name
|
|
255
|
+
};
|
|
256
|
+
}
|
|
257
|
+
return workflow;
|
|
258
|
+
}
|
|
228
259
|
getWorkflows(props = {}) {
|
|
229
260
|
if (props.serialized) {
|
|
230
261
|
return Object.entries(this.#workflows).reduce((acc, [k, v]) => {
|
|
@@ -238,7 +269,30 @@ This is a warning for now, but will throw an error in the future
|
|
|
238
269
|
}
|
|
239
270
|
return this.#workflows;
|
|
240
271
|
}
|
|
272
|
+
vnext_getWorkflows(props = {}) {
|
|
273
|
+
if (props.serialized) {
|
|
274
|
+
return Object.entries(this.#vnext_workflows).reduce((acc, [k, v]) => {
|
|
275
|
+
return {
|
|
276
|
+
...acc,
|
|
277
|
+
[k]: {
|
|
278
|
+
name: v.name
|
|
279
|
+
}
|
|
280
|
+
};
|
|
281
|
+
}, {});
|
|
282
|
+
}
|
|
283
|
+
return this.#vnext_workflows;
|
|
284
|
+
}
|
|
241
285
|
setStorage(storage) {
|
|
286
|
+
if (storage instanceof DefaultProxyStorage) {
|
|
287
|
+
this.#logger.warn(`Importing "DefaultStorage" from '@mastra/core/storage/libsql' is deprecated.
|
|
288
|
+
|
|
289
|
+
Instead of:
|
|
290
|
+
import { DefaultStorage } from '@mastra/core/storage/libsql';
|
|
291
|
+
|
|
292
|
+
Do:
|
|
293
|
+
import { LibSQLStore } from '@mastra/libsql';
|
|
294
|
+
`);
|
|
295
|
+
}
|
|
242
296
|
this.#storage = augmentWithInit(storage);
|
|
243
297
|
}
|
|
244
298
|
setLogger({
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var
|
|
3
|
+
var chunk422BXQRT_cjs = require('./chunk-422BXQRT.cjs');
|
|
4
4
|
var cohereAi = require('cohere-ai');
|
|
5
5
|
|
|
6
6
|
var CohereRelevanceScorer = class {
|
|
@@ -38,7 +38,7 @@ Relevance score (0-1):`;
|
|
|
38
38
|
var MastraAgentRelevanceScorer = class {
|
|
39
39
|
agent;
|
|
40
40
|
constructor(name, model) {
|
|
41
|
-
this.agent = new
|
|
41
|
+
this.agent = new chunk422BXQRT_cjs.Agent({
|
|
42
42
|
name: `Relevance Scorer ${name}`,
|
|
43
43
|
instructions: `You are a specialized agent for evaluating the relevance of text to queries.
|
|
44
44
|
Your task is to rate how well a text passage answers a given query.
|