@mastra/editor 0.0.0-auth-rbac-cms-20260211172515
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/CHANGELOG.md +205 -0
- package/LICENSE.md +15 -0
- package/dist/index.cjs +715 -0
- package/dist/index.d.cts +209 -0
- package/dist/index.d.ts +209 -0
- package/dist/index.js +687 -0
- package/package.json +51 -0
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,715 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
MastraEditor: () => MastraEditor,
|
|
24
|
+
evaluateRuleGroup: () => evaluateRuleGroup,
|
|
25
|
+
renderTemplate: () => renderTemplate,
|
|
26
|
+
resolveInstructionBlocks: () => resolveInstructionBlocks
|
|
27
|
+
});
|
|
28
|
+
module.exports = __toCommonJS(index_exports);
|
|
29
|
+
var import_memory = require("@mastra/memory");
|
|
30
|
+
var import_core = require("@mastra/core");
|
|
31
|
+
|
|
32
|
+
// src/template-engine.ts
|
|
33
|
+
function resolvePath(context, path) {
|
|
34
|
+
const segments = path.split(".");
|
|
35
|
+
let current = context;
|
|
36
|
+
for (const segment of segments) {
|
|
37
|
+
if (current === null || current === void 0 || typeof current !== "object") {
|
|
38
|
+
return void 0;
|
|
39
|
+
}
|
|
40
|
+
current = current[segment];
|
|
41
|
+
}
|
|
42
|
+
return current;
|
|
43
|
+
}
|
|
44
|
+
var TEMPLATE_PATTERN = /\{\{\s*([a-zA-Z_][\w.]*)\s*(?:\|\|\s*(?:'([^']*)'|"([^"]*)")\s*)?\}\}/g;
|
|
45
|
+
function renderTemplate(template, context) {
|
|
46
|
+
return template.replace(
|
|
47
|
+
TEMPLATE_PATTERN,
|
|
48
|
+
(match, variablePath, singleFallback, doubleFallback) => {
|
|
49
|
+
const resolved = resolvePath(context, variablePath);
|
|
50
|
+
if (resolved !== void 0 && resolved !== null) {
|
|
51
|
+
return String(resolved);
|
|
52
|
+
}
|
|
53
|
+
const fallback = singleFallback ?? doubleFallback;
|
|
54
|
+
if (fallback !== void 0) {
|
|
55
|
+
return fallback;
|
|
56
|
+
}
|
|
57
|
+
return match;
|
|
58
|
+
}
|
|
59
|
+
);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// src/rule-evaluator.ts
|
|
63
|
+
function resolvePath2(context, path) {
|
|
64
|
+
const segments = path.split(".");
|
|
65
|
+
let current = context;
|
|
66
|
+
for (const segment of segments) {
|
|
67
|
+
if (current === null || current === void 0 || typeof current !== "object") {
|
|
68
|
+
return void 0;
|
|
69
|
+
}
|
|
70
|
+
current = current[segment];
|
|
71
|
+
}
|
|
72
|
+
return current;
|
|
73
|
+
}
|
|
74
|
+
function evaluateRule(rule, context) {
|
|
75
|
+
const fieldValue = resolvePath2(context, rule.field);
|
|
76
|
+
switch (rule.operator) {
|
|
77
|
+
case "equals":
|
|
78
|
+
return fieldValue === rule.value;
|
|
79
|
+
case "not_equals":
|
|
80
|
+
return fieldValue !== rule.value;
|
|
81
|
+
case "contains": {
|
|
82
|
+
if (typeof fieldValue === "string" && typeof rule.value === "string") {
|
|
83
|
+
return fieldValue.includes(rule.value);
|
|
84
|
+
}
|
|
85
|
+
if (Array.isArray(fieldValue)) {
|
|
86
|
+
return fieldValue.includes(rule.value);
|
|
87
|
+
}
|
|
88
|
+
return false;
|
|
89
|
+
}
|
|
90
|
+
case "not_contains": {
|
|
91
|
+
if (typeof fieldValue === "string" && typeof rule.value === "string") {
|
|
92
|
+
return !fieldValue.includes(rule.value);
|
|
93
|
+
}
|
|
94
|
+
if (Array.isArray(fieldValue)) {
|
|
95
|
+
return !fieldValue.includes(rule.value);
|
|
96
|
+
}
|
|
97
|
+
return true;
|
|
98
|
+
}
|
|
99
|
+
case "greater_than":
|
|
100
|
+
return typeof fieldValue === "number" && typeof rule.value === "number" && fieldValue > rule.value;
|
|
101
|
+
case "less_than":
|
|
102
|
+
return typeof fieldValue === "number" && typeof rule.value === "number" && fieldValue < rule.value;
|
|
103
|
+
case "greater_than_or_equal":
|
|
104
|
+
return typeof fieldValue === "number" && typeof rule.value === "number" && fieldValue >= rule.value;
|
|
105
|
+
case "less_than_or_equal":
|
|
106
|
+
return typeof fieldValue === "number" && typeof rule.value === "number" && fieldValue <= rule.value;
|
|
107
|
+
case "in":
|
|
108
|
+
return Array.isArray(rule.value) && rule.value.includes(fieldValue);
|
|
109
|
+
case "not_in":
|
|
110
|
+
return Array.isArray(rule.value) && !rule.value.includes(fieldValue);
|
|
111
|
+
case "exists":
|
|
112
|
+
return fieldValue !== void 0 && fieldValue !== null;
|
|
113
|
+
case "not_exists":
|
|
114
|
+
return fieldValue === void 0 || fieldValue === null;
|
|
115
|
+
default:
|
|
116
|
+
return false;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
function evaluateRuleGroup(ruleGroup, context) {
|
|
120
|
+
if (ruleGroup.conditions.length === 0) {
|
|
121
|
+
return true;
|
|
122
|
+
}
|
|
123
|
+
const results = ruleGroup.conditions.map((condition) => {
|
|
124
|
+
if ("conditions" in condition) {
|
|
125
|
+
return evaluateRuleGroup(condition, context);
|
|
126
|
+
}
|
|
127
|
+
return evaluateRule(condition, context);
|
|
128
|
+
});
|
|
129
|
+
if (ruleGroup.operator === "AND") {
|
|
130
|
+
return results.every(Boolean);
|
|
131
|
+
}
|
|
132
|
+
return results.some(Boolean);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
// src/instruction-builder.ts
|
|
136
|
+
async function resolveInstructionBlocks(blocks, context, deps) {
|
|
137
|
+
const segments = [];
|
|
138
|
+
const blockIds = [
|
|
139
|
+
...new Set(
|
|
140
|
+
blocks.filter((b) => b.type === "prompt_block_ref").map((b) => b.id)
|
|
141
|
+
)
|
|
142
|
+
];
|
|
143
|
+
const resolvedBlocksMap = /* @__PURE__ */ new Map();
|
|
144
|
+
if (blockIds.length > 0) {
|
|
145
|
+
const fetchResults = await Promise.all(
|
|
146
|
+
blockIds.map((id) => deps.promptBlocksStorage.getPromptBlockByIdResolved({ id }))
|
|
147
|
+
);
|
|
148
|
+
for (let i = 0; i < blockIds.length; i++) {
|
|
149
|
+
const result = fetchResults[i];
|
|
150
|
+
if (result) {
|
|
151
|
+
resolvedBlocksMap.set(blockIds[i], result);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
for (const block of blocks) {
|
|
156
|
+
if (block.type === "text") {
|
|
157
|
+
const rendered2 = renderTemplate(block.content, context);
|
|
158
|
+
if (rendered2.trim()) {
|
|
159
|
+
segments.push(rendered2);
|
|
160
|
+
}
|
|
161
|
+
continue;
|
|
162
|
+
}
|
|
163
|
+
if (block.type === "prompt_block") {
|
|
164
|
+
if (block.rules) {
|
|
165
|
+
const passes = evaluateRuleGroup(block.rules, context);
|
|
166
|
+
if (!passes) {
|
|
167
|
+
continue;
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
const rendered2 = renderTemplate(block.content, context);
|
|
171
|
+
if (rendered2.trim()) {
|
|
172
|
+
segments.push(rendered2);
|
|
173
|
+
}
|
|
174
|
+
continue;
|
|
175
|
+
}
|
|
176
|
+
const resolved = resolvedBlocksMap.get(block.id);
|
|
177
|
+
if (!resolved) {
|
|
178
|
+
continue;
|
|
179
|
+
}
|
|
180
|
+
if (resolved.status !== "published") {
|
|
181
|
+
continue;
|
|
182
|
+
}
|
|
183
|
+
if (resolved.rules) {
|
|
184
|
+
const passes = evaluateRuleGroup(resolved.rules, context);
|
|
185
|
+
if (!passes) {
|
|
186
|
+
continue;
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
const rendered = renderTemplate(resolved.content, context);
|
|
190
|
+
if (rendered.trim()) {
|
|
191
|
+
segments.push(rendered);
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
return segments.join("\n\n");
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
// src/index.ts
|
|
198
|
+
var MastraEditor = class {
|
|
199
|
+
constructor(config) {
|
|
200
|
+
this.logger = config?.logger;
|
|
201
|
+
}
|
|
202
|
+
/**
|
|
203
|
+
* Register this editor with a Mastra instance.
|
|
204
|
+
* This gives the editor access to Mastra's storage, tools, workflows, etc.
|
|
205
|
+
*/
|
|
206
|
+
registerWithMastra(mastra) {
|
|
207
|
+
this.mastra = mastra;
|
|
208
|
+
if (!this.logger) {
|
|
209
|
+
this.logger = mastra.getLogger();
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
/**
|
|
213
|
+
* Get the agents storage domain from the Mastra storage.
|
|
214
|
+
*/
|
|
215
|
+
async getAgentsStore() {
|
|
216
|
+
const storage = this.mastra.getStorage();
|
|
217
|
+
if (!storage) throw new Error("Storage is not configured");
|
|
218
|
+
const agentsStore = await storage.getStore("agents");
|
|
219
|
+
if (!agentsStore) throw new Error("Agents storage domain is not available");
|
|
220
|
+
return agentsStore;
|
|
221
|
+
}
|
|
222
|
+
/**
|
|
223
|
+
* Get the prompt blocks storage domain from the Mastra storage.
|
|
224
|
+
*/
|
|
225
|
+
async getPromptBlocksStore() {
|
|
226
|
+
const storage = this.mastra.getStorage();
|
|
227
|
+
if (!storage) throw new Error("Storage is not configured");
|
|
228
|
+
const promptBlocksStore = await storage.getStore("promptBlocks");
|
|
229
|
+
if (!promptBlocksStore) throw new Error("Prompt blocks storage domain is not available");
|
|
230
|
+
return promptBlocksStore;
|
|
231
|
+
}
|
|
232
|
+
async getStoredAgentById(id, options) {
|
|
233
|
+
if (!this.mastra) {
|
|
234
|
+
throw new Error("MastraEditor is not registered with a Mastra instance");
|
|
235
|
+
}
|
|
236
|
+
const agentsStore = await this.getAgentsStore();
|
|
237
|
+
if (options?.versionId && options?.versionNumber !== void 0) {
|
|
238
|
+
this.logger?.warn(`Both versionId and versionNumber provided for agent "${id}". Using versionId.`);
|
|
239
|
+
}
|
|
240
|
+
if (options?.versionId) {
|
|
241
|
+
const version = await agentsStore.getVersion(options.versionId);
|
|
242
|
+
if (!version) {
|
|
243
|
+
return null;
|
|
244
|
+
}
|
|
245
|
+
if (version.agentId !== id) {
|
|
246
|
+
return null;
|
|
247
|
+
}
|
|
248
|
+
const {
|
|
249
|
+
id: _versionId,
|
|
250
|
+
agentId: _agentId,
|
|
251
|
+
versionNumber: _versionNumber,
|
|
252
|
+
changedFields: _changedFields,
|
|
253
|
+
changeMessage: _changeMessage,
|
|
254
|
+
createdAt: _createdAt,
|
|
255
|
+
...snapshotConfig
|
|
256
|
+
} = version;
|
|
257
|
+
const agentRecord = await agentsStore.getAgentById({ id });
|
|
258
|
+
if (!agentRecord) {
|
|
259
|
+
return null;
|
|
260
|
+
}
|
|
261
|
+
const { activeVersionId: _activeVersionId, ...agentRecordWithoutActiveVersion } = agentRecord;
|
|
262
|
+
const resolvedAgent = { ...agentRecordWithoutActiveVersion, ...snapshotConfig };
|
|
263
|
+
if (options?.returnRaw) {
|
|
264
|
+
return resolvedAgent;
|
|
265
|
+
}
|
|
266
|
+
return this.createAgentFromStoredConfig(resolvedAgent);
|
|
267
|
+
}
|
|
268
|
+
if (options?.versionNumber !== void 0) {
|
|
269
|
+
const version = await agentsStore.getVersionByNumber(id, options.versionNumber);
|
|
270
|
+
if (!version) {
|
|
271
|
+
return null;
|
|
272
|
+
}
|
|
273
|
+
const {
|
|
274
|
+
id: _versionId,
|
|
275
|
+
agentId: _agentId,
|
|
276
|
+
versionNumber: _versionNumber,
|
|
277
|
+
changedFields: _changedFields,
|
|
278
|
+
changeMessage: _changeMessage,
|
|
279
|
+
createdAt: _createdAt,
|
|
280
|
+
...snapshotConfig
|
|
281
|
+
} = version;
|
|
282
|
+
const agentRecord = await agentsStore.getAgentById({ id });
|
|
283
|
+
if (!agentRecord) {
|
|
284
|
+
return null;
|
|
285
|
+
}
|
|
286
|
+
const { activeVersionId: _activeVersionId, ...agentRecordWithoutActiveVersion } = agentRecord;
|
|
287
|
+
const resolvedAgent = { ...agentRecordWithoutActiveVersion, ...snapshotConfig };
|
|
288
|
+
if (options?.returnRaw) {
|
|
289
|
+
return resolvedAgent;
|
|
290
|
+
}
|
|
291
|
+
return this.createAgentFromStoredConfig(resolvedAgent);
|
|
292
|
+
}
|
|
293
|
+
if (!options?.returnRaw) {
|
|
294
|
+
const agentCache2 = this.mastra.getStoredAgentCache();
|
|
295
|
+
if (agentCache2) {
|
|
296
|
+
const cached = agentCache2.get(id);
|
|
297
|
+
if (cached) {
|
|
298
|
+
return cached;
|
|
299
|
+
}
|
|
300
|
+
this.logger?.debug(`[getStoredAgentById] Cache miss for agent "${id}", fetching from storage`);
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
const storedAgent = await agentsStore.getAgentByIdResolved({ id });
|
|
304
|
+
if (!storedAgent) {
|
|
305
|
+
return null;
|
|
306
|
+
}
|
|
307
|
+
if (options?.returnRaw) {
|
|
308
|
+
return storedAgent;
|
|
309
|
+
}
|
|
310
|
+
const agent = this.createAgentFromStoredConfig(storedAgent);
|
|
311
|
+
const agentCache = this.mastra.getStoredAgentCache();
|
|
312
|
+
if (agentCache) {
|
|
313
|
+
agentCache.set(id, agent);
|
|
314
|
+
}
|
|
315
|
+
return agent;
|
|
316
|
+
}
|
|
317
|
+
async listStoredAgents(options) {
|
|
318
|
+
if (!this.mastra) {
|
|
319
|
+
throw new Error("MastraEditor is not registered with a Mastra instance");
|
|
320
|
+
}
|
|
321
|
+
const agentsStore = await this.getAgentsStore();
|
|
322
|
+
const result = await agentsStore.listAgentsResolved({
|
|
323
|
+
page: options?.page,
|
|
324
|
+
perPage: options?.pageSize,
|
|
325
|
+
orderBy: { field: "createdAt", direction: "DESC" }
|
|
326
|
+
});
|
|
327
|
+
if (options?.returnRaw) {
|
|
328
|
+
return result;
|
|
329
|
+
}
|
|
330
|
+
const agents = result.agents.map(
|
|
331
|
+
(storedAgent) => this.createAgentFromStoredConfig(storedAgent)
|
|
332
|
+
);
|
|
333
|
+
return {
|
|
334
|
+
agents,
|
|
335
|
+
total: result.total,
|
|
336
|
+
page: result.page,
|
|
337
|
+
perPage: result.perPage,
|
|
338
|
+
hasMore: result.hasMore
|
|
339
|
+
};
|
|
340
|
+
}
|
|
341
|
+
// ==========================================================================
|
|
342
|
+
// Prompt Block CRUD Methods
|
|
343
|
+
// ==========================================================================
|
|
344
|
+
/**
|
|
345
|
+
* Create a new prompt block with an initial version.
|
|
346
|
+
*/
|
|
347
|
+
async createPromptBlock(input) {
|
|
348
|
+
if (!this.mastra) throw new Error("MastraEditor is not registered with a Mastra instance");
|
|
349
|
+
const store = await this.getPromptBlocksStore();
|
|
350
|
+
await store.createPromptBlock({ promptBlock: input });
|
|
351
|
+
const resolved = await store.getPromptBlockByIdResolved({ id: input.id });
|
|
352
|
+
if (!resolved) {
|
|
353
|
+
throw new Error(`Failed to resolve prompt block ${input.id} after creation`);
|
|
354
|
+
}
|
|
355
|
+
return resolved;
|
|
356
|
+
}
|
|
357
|
+
/**
|
|
358
|
+
* Get a prompt block by ID, resolved with its active version.
|
|
359
|
+
*/
|
|
360
|
+
async getPromptBlock(id) {
|
|
361
|
+
if (!this.mastra) throw new Error("MastraEditor is not registered with a Mastra instance");
|
|
362
|
+
const store = await this.getPromptBlocksStore();
|
|
363
|
+
return store.getPromptBlockByIdResolved({ id });
|
|
364
|
+
}
|
|
365
|
+
/**
|
|
366
|
+
* Update a prompt block, creating a new version with the changes.
|
|
367
|
+
*/
|
|
368
|
+
async updatePromptBlock(input) {
|
|
369
|
+
if (!this.mastra) throw new Error("MastraEditor is not registered with a Mastra instance");
|
|
370
|
+
const store = await this.getPromptBlocksStore();
|
|
371
|
+
await store.updatePromptBlock(input);
|
|
372
|
+
const resolved = await store.getPromptBlockByIdResolved({ id: input.id });
|
|
373
|
+
if (!resolved) {
|
|
374
|
+
throw new Error(`Failed to resolve prompt block ${input.id} after update`);
|
|
375
|
+
}
|
|
376
|
+
return resolved;
|
|
377
|
+
}
|
|
378
|
+
/**
|
|
379
|
+
* Delete a prompt block and all its versions.
|
|
380
|
+
*/
|
|
381
|
+
async deletePromptBlock(id) {
|
|
382
|
+
if (!this.mastra) throw new Error("MastraEditor is not registered with a Mastra instance");
|
|
383
|
+
const store = await this.getPromptBlocksStore();
|
|
384
|
+
await store.deletePromptBlock({ id });
|
|
385
|
+
}
|
|
386
|
+
/**
|
|
387
|
+
* List prompt blocks with optional pagination and filtering.
|
|
388
|
+
*/
|
|
389
|
+
async listPromptBlocks(args) {
|
|
390
|
+
if (!this.mastra) throw new Error("MastraEditor is not registered with a Mastra instance");
|
|
391
|
+
const store = await this.getPromptBlocksStore();
|
|
392
|
+
return store.listPromptBlocks(args);
|
|
393
|
+
}
|
|
394
|
+
/**
|
|
395
|
+
* List prompt blocks resolved with their active version config.
|
|
396
|
+
*/
|
|
397
|
+
async listPromptBlocksResolved(args) {
|
|
398
|
+
if (!this.mastra) throw new Error("MastraEditor is not registered with a Mastra instance");
|
|
399
|
+
const store = await this.getPromptBlocksStore();
|
|
400
|
+
return store.listPromptBlocksResolved(args);
|
|
401
|
+
}
|
|
402
|
+
/**
|
|
403
|
+
* Preview the resolved instructions for a given set of instruction blocks and context.
|
|
404
|
+
* Useful for UI preview endpoints.
|
|
405
|
+
*/
|
|
406
|
+
async previewInstructions(blocks, context) {
|
|
407
|
+
if (!this.mastra) throw new Error("MastraEditor is not registered with a Mastra instance");
|
|
408
|
+
const store = await this.getPromptBlocksStore();
|
|
409
|
+
return resolveInstructionBlocks(blocks, context, { promptBlocksStorage: store });
|
|
410
|
+
}
|
|
411
|
+
/**
|
|
412
|
+
* Clear the stored agent cache for a specific agent ID, or all cached agents.
|
|
413
|
+
* When clearing a specific agent, also removes it from Mastra's agent registry
|
|
414
|
+
* so that fresh data is loaded on next access.
|
|
415
|
+
*/
|
|
416
|
+
clearStoredAgentCache(agentId) {
|
|
417
|
+
if (!this.mastra) return;
|
|
418
|
+
const agentCache = this.mastra.getStoredAgentCache();
|
|
419
|
+
if (agentId) {
|
|
420
|
+
if (agentCache) {
|
|
421
|
+
agentCache.delete(agentId);
|
|
422
|
+
}
|
|
423
|
+
this.mastra.removeAgent(agentId);
|
|
424
|
+
this.logger?.debug(`[clearStoredAgentCache] Cleared cache and registry for agent "${agentId}"`);
|
|
425
|
+
} else {
|
|
426
|
+
if (agentCache) {
|
|
427
|
+
agentCache.clear();
|
|
428
|
+
}
|
|
429
|
+
this.logger?.debug("[clearStoredAgentCache] Cleared all cached agents");
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
/**
|
|
433
|
+
* Create an Agent instance from stored configuration.
|
|
434
|
+
* Resolves all stored references (tools, workflows, agents, memory, scorers)
|
|
435
|
+
* and registers the agent with the Mastra instance.
|
|
436
|
+
*/
|
|
437
|
+
createAgentFromStoredConfig(storedAgent) {
|
|
438
|
+
if (!this.mastra) {
|
|
439
|
+
throw new Error("MastraEditor is not registered with a Mastra instance");
|
|
440
|
+
}
|
|
441
|
+
this.logger?.debug(`[createAgentFromStoredConfig] Creating agent from stored config "${storedAgent.id}"`);
|
|
442
|
+
const tools = this.resolveStoredTools(storedAgent.tools);
|
|
443
|
+
const workflows = this.resolveStoredWorkflows(storedAgent.workflows);
|
|
444
|
+
const agents = this.resolveStoredAgents(storedAgent.agents);
|
|
445
|
+
const memory = this.resolveStoredMemory(storedAgent.memory);
|
|
446
|
+
const scorers = this.resolveStoredScorers(storedAgent.scorers);
|
|
447
|
+
const inputProcessors = this.resolveStoredInputProcessors(storedAgent.inputProcessors);
|
|
448
|
+
const outputProcessors = this.resolveStoredOutputProcessors(storedAgent.outputProcessors);
|
|
449
|
+
const modelConfig = storedAgent.model;
|
|
450
|
+
if (!modelConfig || !modelConfig.provider || !modelConfig.name) {
|
|
451
|
+
throw new Error(
|
|
452
|
+
`Stored agent "${storedAgent.id}" has no active version or invalid model configuration. Both provider and name are required.`
|
|
453
|
+
);
|
|
454
|
+
}
|
|
455
|
+
const model = `${modelConfig.provider}/${modelConfig.name}`;
|
|
456
|
+
const defaultOptions = storedAgent.defaultOptions;
|
|
457
|
+
const instructions = this.resolveStoredInstructions(storedAgent.instructions);
|
|
458
|
+
const agent = new import_core.Agent({
|
|
459
|
+
id: storedAgent.id,
|
|
460
|
+
name: storedAgent.name,
|
|
461
|
+
description: storedAgent.description,
|
|
462
|
+
instructions: instructions ?? "",
|
|
463
|
+
model,
|
|
464
|
+
memory,
|
|
465
|
+
tools,
|
|
466
|
+
workflows,
|
|
467
|
+
agents,
|
|
468
|
+
scorers,
|
|
469
|
+
mastra: this.mastra,
|
|
470
|
+
inputProcessors,
|
|
471
|
+
outputProcessors,
|
|
472
|
+
defaultOptions: {
|
|
473
|
+
maxSteps: defaultOptions?.maxSteps,
|
|
474
|
+
modelSettings: {
|
|
475
|
+
temperature: modelConfig.temperature,
|
|
476
|
+
topP: modelConfig.topP,
|
|
477
|
+
frequencyPenalty: modelConfig.frequencyPenalty,
|
|
478
|
+
presencePenalty: modelConfig.presencePenalty,
|
|
479
|
+
maxOutputTokens: modelConfig.maxCompletionTokens
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
});
|
|
483
|
+
this.mastra?.addAgent(agent, storedAgent.id, { source: "stored" });
|
|
484
|
+
this.logger?.debug(`[createAgentFromStoredConfig] Successfully created agent "${storedAgent.id}"`);
|
|
485
|
+
return agent;
|
|
486
|
+
}
|
|
487
|
+
/**
|
|
488
|
+
* Resolve stored instructions to a value compatible with the Agent constructor.
|
|
489
|
+
* - `string` → pass through as-is (backward compatible)
|
|
490
|
+
* - `AgentInstructionBlock[]` → wrap in a DynamicArgument function that resolves at runtime
|
|
491
|
+
* - `undefined` → return undefined
|
|
492
|
+
*/
|
|
493
|
+
resolveStoredInstructions(instructions) {
|
|
494
|
+
if (instructions === void 0 || instructions === null) {
|
|
495
|
+
return void 0;
|
|
496
|
+
}
|
|
497
|
+
if (typeof instructions === "string") {
|
|
498
|
+
return instructions;
|
|
499
|
+
}
|
|
500
|
+
const blocks = instructions;
|
|
501
|
+
return async ({ requestContext }) => {
|
|
502
|
+
const store = await this.getPromptBlocksStore();
|
|
503
|
+
const context = requestContext.toJSON();
|
|
504
|
+
return resolveInstructionBlocks(blocks, context, { promptBlocksStorage: store });
|
|
505
|
+
};
|
|
506
|
+
}
|
|
507
|
+
/**
|
|
508
|
+
* Resolve stored tool IDs to actual tool instances from Mastra's registry.
|
|
509
|
+
*/
|
|
510
|
+
resolveStoredTools(storedTools) {
|
|
511
|
+
if (!storedTools || storedTools.length === 0) {
|
|
512
|
+
return {};
|
|
513
|
+
}
|
|
514
|
+
if (!this.mastra) {
|
|
515
|
+
return {};
|
|
516
|
+
}
|
|
517
|
+
const resolvedTools = {};
|
|
518
|
+
for (const toolKey of storedTools) {
|
|
519
|
+
try {
|
|
520
|
+
const tool = this.mastra.getToolById(toolKey);
|
|
521
|
+
resolvedTools[toolKey] = tool;
|
|
522
|
+
} catch {
|
|
523
|
+
this.logger?.warn(`Tool "${toolKey}" referenced in stored agent but not registered in Mastra`);
|
|
524
|
+
}
|
|
525
|
+
}
|
|
526
|
+
return resolvedTools;
|
|
527
|
+
}
|
|
528
|
+
/**
|
|
529
|
+
* Resolve stored workflow IDs to actual workflow instances from Mastra's registry.
|
|
530
|
+
*/
|
|
531
|
+
resolveStoredWorkflows(storedWorkflows) {
|
|
532
|
+
if (!storedWorkflows || storedWorkflows.length === 0) {
|
|
533
|
+
return {};
|
|
534
|
+
}
|
|
535
|
+
if (!this.mastra) {
|
|
536
|
+
return {};
|
|
537
|
+
}
|
|
538
|
+
const resolvedWorkflows = {};
|
|
539
|
+
for (const workflowKey of storedWorkflows) {
|
|
540
|
+
try {
|
|
541
|
+
const workflow = this.mastra.getWorkflow(workflowKey);
|
|
542
|
+
resolvedWorkflows[workflowKey] = workflow;
|
|
543
|
+
} catch {
|
|
544
|
+
try {
|
|
545
|
+
const workflow = this.mastra.getWorkflowById(workflowKey);
|
|
546
|
+
resolvedWorkflows[workflowKey] = workflow;
|
|
547
|
+
} catch {
|
|
548
|
+
this.logger?.warn(`Workflow "${workflowKey}" referenced in stored agent but not registered in Mastra`);
|
|
549
|
+
}
|
|
550
|
+
}
|
|
551
|
+
}
|
|
552
|
+
return resolvedWorkflows;
|
|
553
|
+
}
|
|
554
|
+
/**
|
|
555
|
+
* Resolve stored agent IDs to actual agent instances from Mastra's registry.
|
|
556
|
+
*/
|
|
557
|
+
resolveStoredAgents(storedAgents) {
|
|
558
|
+
if (!storedAgents || storedAgents.length === 0) {
|
|
559
|
+
return {};
|
|
560
|
+
}
|
|
561
|
+
if (!this.mastra) {
|
|
562
|
+
return {};
|
|
563
|
+
}
|
|
564
|
+
const resolvedAgents = {};
|
|
565
|
+
for (const agentKey of storedAgents) {
|
|
566
|
+
try {
|
|
567
|
+
const agent = this.mastra.getAgent(agentKey);
|
|
568
|
+
resolvedAgents[agentKey] = agent;
|
|
569
|
+
} catch {
|
|
570
|
+
try {
|
|
571
|
+
const agent = this.mastra.getAgentById(agentKey);
|
|
572
|
+
resolvedAgents[agentKey] = agent;
|
|
573
|
+
} catch {
|
|
574
|
+
this.logger?.warn(`Agent "${agentKey}" referenced in stored agent but not registered in Mastra`);
|
|
575
|
+
}
|
|
576
|
+
}
|
|
577
|
+
}
|
|
578
|
+
return resolvedAgents;
|
|
579
|
+
}
|
|
580
|
+
/**
|
|
581
|
+
* Resolve stored memory config to a MastraMemory instance.
|
|
582
|
+
* Uses @mastra/memory Memory class to instantiate from serialized config.
|
|
583
|
+
*/
|
|
584
|
+
resolveStoredMemory(memoryConfig) {
|
|
585
|
+
if (!memoryConfig) {
|
|
586
|
+
this.logger?.debug(`[resolveStoredMemory] No memory config provided`);
|
|
587
|
+
return void 0;
|
|
588
|
+
}
|
|
589
|
+
if (!this.mastra) {
|
|
590
|
+
this.logger?.warn("MastraEditor not registered with Mastra instance. Cannot instantiate memory.");
|
|
591
|
+
return void 0;
|
|
592
|
+
}
|
|
593
|
+
try {
|
|
594
|
+
let vector;
|
|
595
|
+
if (memoryConfig.vector) {
|
|
596
|
+
const vectors = this.mastra.listVectors();
|
|
597
|
+
vector = vectors?.[memoryConfig.vector];
|
|
598
|
+
if (!vector) {
|
|
599
|
+
this.logger?.warn(`Vector provider "${memoryConfig.vector}" not found in Mastra instance`);
|
|
600
|
+
}
|
|
601
|
+
}
|
|
602
|
+
if (memoryConfig.options?.semanticRecall && (!vector || !memoryConfig.embedder)) {
|
|
603
|
+
this.logger?.warn(
|
|
604
|
+
"Semantic recall is enabled but no vector store or embedder are configured. Creating memory without semantic recall. To use semantic recall, configure a vector store and embedder in your Mastra instance."
|
|
605
|
+
);
|
|
606
|
+
const adjustedOptions = { ...memoryConfig.options, semanticRecall: false };
|
|
607
|
+
const sharedConfig2 = {
|
|
608
|
+
storage: this.mastra.getStorage(),
|
|
609
|
+
vector,
|
|
610
|
+
options: adjustedOptions,
|
|
611
|
+
embedder: memoryConfig.embedder,
|
|
612
|
+
embedderOptions: memoryConfig.embedderOptions
|
|
613
|
+
};
|
|
614
|
+
const memoryInstance2 = new import_memory.Memory(sharedConfig2);
|
|
615
|
+
return memoryInstance2;
|
|
616
|
+
}
|
|
617
|
+
const storage = this.mastra.getStorage();
|
|
618
|
+
const sharedConfig = {
|
|
619
|
+
storage,
|
|
620
|
+
vector,
|
|
621
|
+
options: memoryConfig.options,
|
|
622
|
+
embedder: memoryConfig.embedder,
|
|
623
|
+
embedderOptions: memoryConfig.embedderOptions
|
|
624
|
+
};
|
|
625
|
+
const memoryInstance = new import_memory.Memory(sharedConfig);
|
|
626
|
+
return memoryInstance;
|
|
627
|
+
} catch (error) {
|
|
628
|
+
this.logger?.error("Failed to resolve memory from config", { error });
|
|
629
|
+
return void 0;
|
|
630
|
+
}
|
|
631
|
+
}
|
|
632
|
+
/**
|
|
633
|
+
* Resolve stored scorer configs to MastraScorers instances.
|
|
634
|
+
*/
|
|
635
|
+
resolveStoredScorers(storedScorers) {
|
|
636
|
+
if (!storedScorers || Object.keys(storedScorers).length === 0) {
|
|
637
|
+
return void 0;
|
|
638
|
+
}
|
|
639
|
+
if (!this.mastra) {
|
|
640
|
+
return void 0;
|
|
641
|
+
}
|
|
642
|
+
const resolvedScorers = {};
|
|
643
|
+
for (const [scorerKey, scorerConfig] of Object.entries(storedScorers)) {
|
|
644
|
+
try {
|
|
645
|
+
const scorer = this.mastra.getScorer(scorerKey);
|
|
646
|
+
resolvedScorers[scorerKey] = {
|
|
647
|
+
scorer,
|
|
648
|
+
sampling: scorerConfig.sampling
|
|
649
|
+
};
|
|
650
|
+
} catch {
|
|
651
|
+
try {
|
|
652
|
+
const scorer = this.mastra.getScorerById(scorerKey);
|
|
653
|
+
resolvedScorers[scorerKey] = {
|
|
654
|
+
scorer,
|
|
655
|
+
sampling: scorerConfig.sampling
|
|
656
|
+
};
|
|
657
|
+
} catch {
|
|
658
|
+
this.logger?.warn(`Scorer "${scorerKey}" referenced in stored agent but not registered in Mastra`);
|
|
659
|
+
}
|
|
660
|
+
}
|
|
661
|
+
}
|
|
662
|
+
return Object.keys(resolvedScorers).length > 0 ? resolvedScorers : void 0;
|
|
663
|
+
}
|
|
664
|
+
/**
|
|
665
|
+
* Look up a processor by key or ID from Mastra's registry.
|
|
666
|
+
*/
|
|
667
|
+
findProcessor(processorKey) {
|
|
668
|
+
if (!this.mastra) return void 0;
|
|
669
|
+
try {
|
|
670
|
+
return this.mastra.getProcessor(processorKey);
|
|
671
|
+
} catch {
|
|
672
|
+
try {
|
|
673
|
+
return this.mastra.getProcessorById(processorKey);
|
|
674
|
+
} catch {
|
|
675
|
+
this.logger?.warn(`Processor "${processorKey}" referenced in stored agent but not registered in Mastra`);
|
|
676
|
+
return void 0;
|
|
677
|
+
}
|
|
678
|
+
}
|
|
679
|
+
}
|
|
680
|
+
/**
|
|
681
|
+
* Resolve stored input processor keys to actual processor instances.
|
|
682
|
+
*/
|
|
683
|
+
resolveStoredInputProcessors(storedProcessors) {
|
|
684
|
+
if (!storedProcessors || storedProcessors.length === 0) return void 0;
|
|
685
|
+
const resolved = [];
|
|
686
|
+
for (const key of storedProcessors) {
|
|
687
|
+
const processor = this.findProcessor(key);
|
|
688
|
+
if (processor && (processor.processInput || processor.processInputStep)) {
|
|
689
|
+
resolved.push(processor);
|
|
690
|
+
}
|
|
691
|
+
}
|
|
692
|
+
return resolved.length > 0 ? resolved : void 0;
|
|
693
|
+
}
|
|
694
|
+
/**
|
|
695
|
+
* Resolve stored output processor keys to actual processor instances.
|
|
696
|
+
*/
|
|
697
|
+
resolveStoredOutputProcessors(storedProcessors) {
|
|
698
|
+
if (!storedProcessors || storedProcessors.length === 0) return void 0;
|
|
699
|
+
const resolved = [];
|
|
700
|
+
for (const key of storedProcessors) {
|
|
701
|
+
const processor = this.findProcessor(key);
|
|
702
|
+
if (processor && (processor.processOutputStream || processor.processOutputResult || processor.processOutputStep)) {
|
|
703
|
+
resolved.push(processor);
|
|
704
|
+
}
|
|
705
|
+
}
|
|
706
|
+
return resolved.length > 0 ? resolved : void 0;
|
|
707
|
+
}
|
|
708
|
+
};
|
|
709
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
710
|
+
0 && (module.exports = {
|
|
711
|
+
MastraEditor,
|
|
712
|
+
evaluateRuleGroup,
|
|
713
|
+
renderTemplate,
|
|
714
|
+
resolveInstructionBlocks
|
|
715
|
+
});
|