@mastra/server 0.10.13 → 0.10.15-alpha.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/_tsup-dts-rollup.d.cts +1107 -0
- package/dist/_tsup-dts-rollup.d.ts +1107 -0
- package/dist/chunk-2KZFMI6P.cjs +37 -0
- package/dist/chunk-2SLFAFTR.cjs +148 -0
- package/dist/chunk-3CNDE7QY.cjs +308 -0
- package/dist/chunk-4D66QEKC.js +81 -0
- package/dist/chunk-5PQQ42EZ.js +320 -0
- package/dist/chunk-75ZPJI57.cjs +9 -0
- package/dist/chunk-7TP2LX5L.cjs +129 -0
- package/dist/chunk-B2PAS2IB.cjs +514 -0
- package/dist/chunk-BK4XT6EG.js +270 -0
- package/dist/chunk-CCGRCYWJ.cjs +18 -0
- package/dist/chunk-CRCR3ZUK.js +142 -0
- package/dist/chunk-FRVBFMO2.cjs +128 -0
- package/dist/chunk-G7KH752Y.cjs +145 -0
- package/dist/chunk-GHC4YV6R.js +504 -0
- package/dist/chunk-H7DMHBKY.js +498 -0
- package/dist/chunk-JMLYCXMK.cjs +332 -0
- package/dist/chunk-KHZKYUNR.js +112 -0
- package/dist/chunk-KOHWJYJT.js +296 -0
- package/dist/chunk-LCM566I4.js +35 -0
- package/dist/chunk-LF7P5PLR.js +14 -0
- package/dist/chunk-LI436ITD.cjs +2042 -0
- package/dist/chunk-LRCAAFUA.js +122 -0
- package/dist/chunk-LZ3VJXSO.cjs +278 -0
- package/dist/chunk-MEGCYGBU.js +2039 -0
- package/dist/chunk-MLKGABMK.js +7 -0
- package/dist/chunk-PZQDCRPV.cjs +16 -0
- package/dist/chunk-QGX47B5D.cjs +86 -0
- package/dist/chunk-RG473F6Y.js +141 -0
- package/dist/chunk-RHSWAXKB.cjs +118 -0
- package/dist/chunk-RSEO4XPX.js +16 -0
- package/dist/chunk-SDPGVWQJ.cjs +512 -0
- package/dist/chunk-WNVFNNWN.js +121 -0
- package/dist/index.cjs +2 -0
- package/dist/index.d.cts +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/server/handlers/a2a.cjs +30 -0
- package/dist/server/handlers/a2a.d.cts +6 -0
- package/dist/server/handlers/a2a.d.ts +6 -0
- package/dist/server/handlers/a2a.js +1 -0
- package/dist/server/handlers/agents.cjs +30 -0
- package/dist/server/handlers/agents.d.cts +6 -0
- package/dist/server/handlers/agents.d.ts +6 -0
- package/dist/server/handlers/agents.js +1 -0
- package/dist/server/handlers/error.cjs +10 -0
- package/dist/server/handlers/error.d.cts +1 -0
- package/dist/server/handlers/error.d.ts +1 -0
- package/dist/server/handlers/error.js +1 -0
- package/dist/server/handlers/legacyWorkflows.cjs +46 -0
- package/dist/server/handlers/legacyWorkflows.d.cts +10 -0
- package/dist/server/handlers/legacyWorkflows.d.ts +10 -0
- package/dist/server/handlers/legacyWorkflows.js +1 -0
- package/dist/server/handlers/logs.cjs +18 -0
- package/dist/server/handlers/logs.d.cts +3 -0
- package/dist/server/handlers/logs.d.ts +3 -0
- package/dist/server/handlers/logs.js +1 -0
- package/dist/server/handlers/memory.cjs +46 -0
- package/dist/server/handlers/memory.d.cts +10 -0
- package/dist/server/handlers/memory.d.ts +10 -0
- package/dist/server/handlers/memory.js +1 -0
- package/dist/server/handlers/network.cjs +22 -0
- package/dist/server/handlers/network.d.cts +4 -0
- package/dist/server/handlers/network.d.ts +4 -0
- package/dist/server/handlers/network.js +1 -0
- package/dist/server/handlers/telemetry.cjs +14 -0
- package/dist/server/handlers/telemetry.d.cts +2 -0
- package/dist/server/handlers/telemetry.d.ts +2 -0
- package/dist/server/handlers/telemetry.js +1 -0
- package/dist/server/handlers/tools.cjs +22 -0
- package/dist/server/handlers/tools.d.cts +4 -0
- package/dist/server/handlers/tools.d.ts +4 -0
- package/dist/server/handlers/tools.js +1 -0
- package/dist/server/handlers/utils.cjs +10 -0
- package/dist/server/handlers/utils.d.cts +1 -0
- package/dist/server/handlers/utils.d.ts +1 -0
- package/dist/server/handlers/utils.js +1 -0
- package/dist/server/handlers/vNextNetwork.cjs +218 -0
- package/dist/server/handlers/vNextNetwork.d.cts +6 -0
- package/dist/server/handlers/vNextNetwork.d.ts +6 -0
- package/dist/server/handlers/vNextNetwork.js +211 -0
- package/dist/server/handlers/vector.cjs +30 -0
- package/dist/server/handlers/vector.d.cts +6 -0
- package/dist/server/handlers/vector.d.ts +6 -0
- package/dist/server/handlers/vector.js +1 -0
- package/dist/server/handlers/voice.cjs +22 -0
- package/dist/server/handlers/voice.d.cts +4 -0
- package/dist/server/handlers/voice.d.ts +4 -0
- package/dist/server/handlers/voice.js +1 -0
- package/dist/server/handlers/workflows.cjs +62 -0
- package/dist/server/handlers/workflows.d.cts +14 -0
- package/dist/server/handlers/workflows.d.ts +14 -0
- package/dist/server/handlers/workflows.js +1 -0
- package/dist/server/handlers.cjs +60 -0
- package/dist/server/handlers.d.cts +11 -0
- package/dist/server/handlers.d.ts +11 -0
- package/dist/server/handlers.js +11 -0
- package/package.json +3 -3
|
@@ -0,0 +1,296 @@
|
|
|
1
|
+
import { validateBody } from './chunk-RSEO4XPX.js';
|
|
2
|
+
import { handleError } from './chunk-LF7P5PLR.js';
|
|
3
|
+
import { HTTPException } from './chunk-LCM566I4.js';
|
|
4
|
+
import { __export } from './chunk-MLKGABMK.js';
|
|
5
|
+
import { generateEmptyFromSchema } from '@mastra/core';
|
|
6
|
+
|
|
7
|
+
// src/server/handlers/memory.ts
|
|
8
|
+
var memory_exports = {};
|
|
9
|
+
__export(memory_exports, {
|
|
10
|
+
createThreadHandler: () => createThreadHandler,
|
|
11
|
+
deleteThreadHandler: () => deleteThreadHandler,
|
|
12
|
+
getMemoryStatusHandler: () => getMemoryStatusHandler,
|
|
13
|
+
getMessagesHandler: () => getMessagesHandler,
|
|
14
|
+
getThreadByIdHandler: () => getThreadByIdHandler,
|
|
15
|
+
getThreadsHandler: () => getThreadsHandler,
|
|
16
|
+
getWorkingMemoryHandler: () => getWorkingMemoryHandler,
|
|
17
|
+
saveMessagesHandler: () => saveMessagesHandler,
|
|
18
|
+
updateThreadHandler: () => updateThreadHandler,
|
|
19
|
+
updateWorkingMemoryHandler: () => updateWorkingMemoryHandler
|
|
20
|
+
});
|
|
21
|
+
async function getMemoryFromContext({
|
|
22
|
+
mastra,
|
|
23
|
+
agentId,
|
|
24
|
+
networkId,
|
|
25
|
+
runtimeContext
|
|
26
|
+
}) {
|
|
27
|
+
const agent = agentId ? mastra.getAgent(agentId) : null;
|
|
28
|
+
if (agentId && !agent) {
|
|
29
|
+
throw new HTTPException(404, { message: "Agent not found" });
|
|
30
|
+
}
|
|
31
|
+
const network = networkId ? mastra.vnext_getNetwork(networkId) : null;
|
|
32
|
+
if (networkId && !network) {
|
|
33
|
+
throw new HTTPException(404, { message: "Network not found" });
|
|
34
|
+
}
|
|
35
|
+
if (agent) {
|
|
36
|
+
return agent?.getMemory() || mastra.getMemory();
|
|
37
|
+
}
|
|
38
|
+
if (network) {
|
|
39
|
+
return await network?.getMemory({ runtimeContext }) || mastra.getMemory();
|
|
40
|
+
}
|
|
41
|
+
return mastra.getMemory();
|
|
42
|
+
}
|
|
43
|
+
async function getMemoryStatusHandler({
|
|
44
|
+
mastra,
|
|
45
|
+
agentId,
|
|
46
|
+
networkId,
|
|
47
|
+
runtimeContext
|
|
48
|
+
}) {
|
|
49
|
+
try {
|
|
50
|
+
const memory = await getMemoryFromContext({ mastra, agentId, networkId, runtimeContext });
|
|
51
|
+
if (!memory) {
|
|
52
|
+
return { result: false };
|
|
53
|
+
}
|
|
54
|
+
return { result: true };
|
|
55
|
+
} catch (error) {
|
|
56
|
+
return handleError(error, "Error getting memory status");
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
async function getThreadsHandler({
|
|
60
|
+
mastra,
|
|
61
|
+
agentId,
|
|
62
|
+
resourceId,
|
|
63
|
+
networkId,
|
|
64
|
+
runtimeContext
|
|
65
|
+
}) {
|
|
66
|
+
try {
|
|
67
|
+
const memory = await getMemoryFromContext({ mastra, agentId, networkId, runtimeContext });
|
|
68
|
+
if (!memory) {
|
|
69
|
+
throw new HTTPException(400, { message: "Memory is not initialized" });
|
|
70
|
+
}
|
|
71
|
+
validateBody({ resourceId });
|
|
72
|
+
const threads = await memory.getThreadsByResourceId({ resourceId });
|
|
73
|
+
return threads;
|
|
74
|
+
} catch (error) {
|
|
75
|
+
return handleError(error, "Error getting threads");
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
async function getThreadByIdHandler({
|
|
79
|
+
mastra,
|
|
80
|
+
agentId,
|
|
81
|
+
threadId,
|
|
82
|
+
networkId,
|
|
83
|
+
runtimeContext
|
|
84
|
+
}) {
|
|
85
|
+
try {
|
|
86
|
+
validateBody({ threadId });
|
|
87
|
+
const memory = await getMemoryFromContext({ mastra, agentId, networkId, runtimeContext });
|
|
88
|
+
if (!memory) {
|
|
89
|
+
throw new HTTPException(400, { message: "Memory is not initialized" });
|
|
90
|
+
}
|
|
91
|
+
const thread = await memory.getThreadById({ threadId });
|
|
92
|
+
if (!thread) {
|
|
93
|
+
throw new HTTPException(404, { message: "Thread not found" });
|
|
94
|
+
}
|
|
95
|
+
return thread;
|
|
96
|
+
} catch (error) {
|
|
97
|
+
return handleError(error, "Error getting thread");
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
async function saveMessagesHandler({
|
|
101
|
+
mastra,
|
|
102
|
+
agentId,
|
|
103
|
+
body,
|
|
104
|
+
networkId,
|
|
105
|
+
runtimeContext
|
|
106
|
+
}) {
|
|
107
|
+
try {
|
|
108
|
+
const memory = await getMemoryFromContext({ mastra, agentId, networkId, runtimeContext });
|
|
109
|
+
if (!memory) {
|
|
110
|
+
throw new HTTPException(400, { message: "Memory is not initialized" });
|
|
111
|
+
}
|
|
112
|
+
if (!body?.messages) {
|
|
113
|
+
throw new HTTPException(400, { message: "Messages are required" });
|
|
114
|
+
}
|
|
115
|
+
if (!Array.isArray(body.messages)) {
|
|
116
|
+
throw new HTTPException(400, { message: "Messages should be an array" });
|
|
117
|
+
}
|
|
118
|
+
const processedMessages = body.messages.map((message) => ({
|
|
119
|
+
...message,
|
|
120
|
+
id: memory.generateId(),
|
|
121
|
+
createdAt: message.createdAt ? new Date(message.createdAt) : /* @__PURE__ */ new Date()
|
|
122
|
+
}));
|
|
123
|
+
const result = await memory.saveMessages({ messages: processedMessages, memoryConfig: {} });
|
|
124
|
+
return result;
|
|
125
|
+
} catch (error) {
|
|
126
|
+
return handleError(error, "Error saving messages");
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
async function createThreadHandler({
|
|
130
|
+
mastra,
|
|
131
|
+
agentId,
|
|
132
|
+
body,
|
|
133
|
+
networkId,
|
|
134
|
+
runtimeContext
|
|
135
|
+
}) {
|
|
136
|
+
try {
|
|
137
|
+
const memory = await getMemoryFromContext({ mastra, agentId, networkId, runtimeContext });
|
|
138
|
+
if (!memory) {
|
|
139
|
+
throw new HTTPException(400, { message: "Memory is not initialized" });
|
|
140
|
+
}
|
|
141
|
+
validateBody({ resourceId: body?.resourceId });
|
|
142
|
+
const result = await memory.createThread({
|
|
143
|
+
resourceId: body?.resourceId,
|
|
144
|
+
title: body?.title,
|
|
145
|
+
metadata: body?.metadata,
|
|
146
|
+
threadId: body?.threadId
|
|
147
|
+
});
|
|
148
|
+
return result;
|
|
149
|
+
} catch (error) {
|
|
150
|
+
return handleError(error, "Error saving thread to memory");
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
async function updateThreadHandler({
|
|
154
|
+
mastra,
|
|
155
|
+
agentId,
|
|
156
|
+
threadId,
|
|
157
|
+
body,
|
|
158
|
+
networkId,
|
|
159
|
+
runtimeContext
|
|
160
|
+
}) {
|
|
161
|
+
try {
|
|
162
|
+
const memory = await getMemoryFromContext({ mastra, agentId, networkId, runtimeContext });
|
|
163
|
+
if (!body) {
|
|
164
|
+
throw new HTTPException(400, { message: "Body is required" });
|
|
165
|
+
}
|
|
166
|
+
const { title, metadata, resourceId } = body;
|
|
167
|
+
const updatedAt = /* @__PURE__ */ new Date();
|
|
168
|
+
validateBody({ threadId });
|
|
169
|
+
if (!memory) {
|
|
170
|
+
throw new HTTPException(400, { message: "Memory is not initialized" });
|
|
171
|
+
}
|
|
172
|
+
const thread = await memory.getThreadById({ threadId });
|
|
173
|
+
if (!thread) {
|
|
174
|
+
throw new HTTPException(404, { message: "Thread not found" });
|
|
175
|
+
}
|
|
176
|
+
const updatedThread = {
|
|
177
|
+
...thread,
|
|
178
|
+
title: title || thread.title,
|
|
179
|
+
metadata: metadata || thread.metadata,
|
|
180
|
+
resourceId: resourceId || thread.resourceId,
|
|
181
|
+
createdAt: thread.createdAt,
|
|
182
|
+
updatedAt
|
|
183
|
+
};
|
|
184
|
+
const result = await memory.saveThread({ thread: updatedThread });
|
|
185
|
+
return result;
|
|
186
|
+
} catch (error) {
|
|
187
|
+
return handleError(error, "Error updating thread");
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
async function deleteThreadHandler({
|
|
191
|
+
mastra,
|
|
192
|
+
agentId,
|
|
193
|
+
threadId,
|
|
194
|
+
networkId,
|
|
195
|
+
runtimeContext
|
|
196
|
+
}) {
|
|
197
|
+
try {
|
|
198
|
+
validateBody({ threadId });
|
|
199
|
+
const memory = await getMemoryFromContext({ mastra, agentId, networkId, runtimeContext });
|
|
200
|
+
if (!memory) {
|
|
201
|
+
throw new HTTPException(400, { message: "Memory is not initialized" });
|
|
202
|
+
}
|
|
203
|
+
const thread = await memory.getThreadById({ threadId });
|
|
204
|
+
if (!thread) {
|
|
205
|
+
throw new HTTPException(404, { message: "Thread not found" });
|
|
206
|
+
}
|
|
207
|
+
await memory.deleteThread(threadId);
|
|
208
|
+
return { result: "Thread deleted" };
|
|
209
|
+
} catch (error) {
|
|
210
|
+
return handleError(error, "Error deleting thread");
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
async function getMessagesHandler({
|
|
214
|
+
mastra,
|
|
215
|
+
agentId,
|
|
216
|
+
threadId,
|
|
217
|
+
limit,
|
|
218
|
+
networkId,
|
|
219
|
+
runtimeContext
|
|
220
|
+
}) {
|
|
221
|
+
if (limit !== void 0 && (!Number.isInteger(limit) || limit <= 0)) {
|
|
222
|
+
throw new HTTPException(400, { message: "Invalid limit: must be a positive integer" });
|
|
223
|
+
}
|
|
224
|
+
try {
|
|
225
|
+
validateBody({ threadId });
|
|
226
|
+
const memory = await getMemoryFromContext({ mastra, agentId, networkId, runtimeContext });
|
|
227
|
+
if (!memory) {
|
|
228
|
+
throw new HTTPException(400, { message: "Memory is not initialized" });
|
|
229
|
+
}
|
|
230
|
+
const thread = await memory.getThreadById({ threadId });
|
|
231
|
+
if (!thread) {
|
|
232
|
+
throw new HTTPException(404, { message: "Thread not found" });
|
|
233
|
+
}
|
|
234
|
+
const result = await memory.query({
|
|
235
|
+
threadId,
|
|
236
|
+
...limit && { selectBy: { last: limit } }
|
|
237
|
+
});
|
|
238
|
+
return { messages: result.messages, uiMessages: result.uiMessages };
|
|
239
|
+
} catch (error) {
|
|
240
|
+
return handleError(error, "Error getting messages");
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
async function getWorkingMemoryHandler({
|
|
244
|
+
mastra,
|
|
245
|
+
agentId,
|
|
246
|
+
threadId,
|
|
247
|
+
resourceId,
|
|
248
|
+
networkId,
|
|
249
|
+
runtimeContext,
|
|
250
|
+
memoryConfig
|
|
251
|
+
}) {
|
|
252
|
+
try {
|
|
253
|
+
const memory = await getMemoryFromContext({ mastra, agentId, networkId, runtimeContext });
|
|
254
|
+
validateBody({ threadId });
|
|
255
|
+
if (!memory) {
|
|
256
|
+
throw new HTTPException(400, { message: "Memory is not initialized" });
|
|
257
|
+
}
|
|
258
|
+
const thread = await memory.getThreadById({ threadId });
|
|
259
|
+
const threadExists = !!thread;
|
|
260
|
+
const template = await memory.getWorkingMemoryTemplate({ memoryConfig });
|
|
261
|
+
const workingMemoryTemplate = template?.format === "json" ? { ...template, content: JSON.stringify(generateEmptyFromSchema(template.content)) } : template;
|
|
262
|
+
const workingMemory = await memory.getWorkingMemory({ threadId, resourceId, memoryConfig });
|
|
263
|
+
const config = memory.getMergedThreadConfig(memoryConfig || {});
|
|
264
|
+
const source = config.workingMemory?.scope === "resource" && resourceId ? "resource" : "thread";
|
|
265
|
+
return { workingMemory, source, workingMemoryTemplate, threadExists };
|
|
266
|
+
} catch (error) {
|
|
267
|
+
return handleError(error, "Error getting working memory");
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
async function updateWorkingMemoryHandler({
|
|
271
|
+
mastra,
|
|
272
|
+
agentId,
|
|
273
|
+
threadId,
|
|
274
|
+
body,
|
|
275
|
+
networkId,
|
|
276
|
+
runtimeContext
|
|
277
|
+
}) {
|
|
278
|
+
try {
|
|
279
|
+
validateBody({ threadId });
|
|
280
|
+
const memory = await getMemoryFromContext({ mastra, agentId, networkId, runtimeContext });
|
|
281
|
+
const { resourceId, memoryConfig, workingMemory } = body;
|
|
282
|
+
if (!memory) {
|
|
283
|
+
throw new HTTPException(400, { message: "Memory is not initialized" });
|
|
284
|
+
}
|
|
285
|
+
const thread = await memory.getThreadById({ threadId });
|
|
286
|
+
if (!thread) {
|
|
287
|
+
throw new HTTPException(404, { message: "Thread not found" });
|
|
288
|
+
}
|
|
289
|
+
await memory.updateWorkingMemory({ threadId, resourceId, workingMemory, memoryConfig });
|
|
290
|
+
return { success: true };
|
|
291
|
+
} catch (error) {
|
|
292
|
+
return handleError(error, "Error updating working memory");
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
export { createThreadHandler, deleteThreadHandler, getMemoryStatusHandler, getMessagesHandler, getThreadByIdHandler, getThreadsHandler, getWorkingMemoryHandler, memory_exports, saveMessagesHandler, updateThreadHandler, updateWorkingMemoryHandler };
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
// src/server/http-exception.ts
|
|
2
|
+
var HTTPException = class extends Error {
|
|
3
|
+
res;
|
|
4
|
+
status;
|
|
5
|
+
/**
|
|
6
|
+
* Creates an instance of `HTTPException`.
|
|
7
|
+
* @param status - HTTP status code for the exception. Defaults to 500.
|
|
8
|
+
* @param options - Additional options for the exception.
|
|
9
|
+
*/
|
|
10
|
+
constructor(status = 500, options) {
|
|
11
|
+
super(options?.message, { cause: options?.cause });
|
|
12
|
+
this.res = options?.res;
|
|
13
|
+
this.status = status;
|
|
14
|
+
this.stack = options?.stack || this.stack;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Returns the response object associated with the exception.
|
|
18
|
+
* If a response object is not provided, a new response is created with the error message and status code.
|
|
19
|
+
* @returns The response object.
|
|
20
|
+
*/
|
|
21
|
+
getResponse() {
|
|
22
|
+
if (this.res) {
|
|
23
|
+
const newResponse = new Response(this.res.body, {
|
|
24
|
+
status: this.status,
|
|
25
|
+
headers: this.res.headers
|
|
26
|
+
});
|
|
27
|
+
return newResponse;
|
|
28
|
+
}
|
|
29
|
+
return new Response(this.message, {
|
|
30
|
+
status: this.status
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
export { HTTPException };
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { HTTPException } from './chunk-LCM566I4.js';
|
|
2
|
+
|
|
3
|
+
// src/server/handlers/error.ts
|
|
4
|
+
function handleError(error, defaultMessage) {
|
|
5
|
+
const apiError = error;
|
|
6
|
+
const apiErrorStatus = apiError.status || apiError.details?.status || 500;
|
|
7
|
+
throw new HTTPException(apiErrorStatus, {
|
|
8
|
+
message: apiError.message || defaultMessage,
|
|
9
|
+
stack: apiError.stack,
|
|
10
|
+
cause: apiError.cause
|
|
11
|
+
});
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export { handleError };
|