@mastra/server 0.0.0-tool-call-parts-20250630193309 → 0.0.0-transpile-packages-20250730132657
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/LICENSE.md +11 -42
- package/dist/_tsup-dts-rollup.d.cts +212 -23
- package/dist/_tsup-dts-rollup.d.ts +212 -23
- package/dist/{chunk-OCWPVYNI.cjs → chunk-2KZFMI6P.cjs} +1 -0
- package/dist/{chunk-VPNDC2DI.cjs → chunk-2SLFAFTR.cjs} +12 -12
- package/dist/{chunk-MEGCYGBU.js → chunk-36BLNJHS.js} +37 -37
- package/dist/chunk-424T5F2J.cjs +157 -0
- package/dist/{chunk-QLG2PFHE.js → chunk-4D66QEKC.js} +2 -2
- package/dist/{chunk-L3VURYKY.cjs → chunk-6KP2OBYA.cjs} +177 -68
- package/dist/chunk-6T5JUKOQ.js +149 -0
- package/dist/{chunk-BNEY4P4P.cjs → chunk-7TP2LX5L.cjs} +16 -16
- package/dist/{chunk-57CJTIPW.cjs → chunk-CCGRCYWJ.cjs} +2 -2
- package/dist/{chunk-C7564HUT.js → chunk-CRCR3ZUK.js} +3 -3
- package/dist/{chunk-B4MQFJ7G.cjs → chunk-G7KH752Y.cjs} +5 -5
- package/dist/{chunk-LI436ITD.cjs → chunk-HISYIDYH.cjs} +37 -37
- package/dist/{chunk-Z3PTO2AK.cjs → chunk-HRSAZUIK.cjs} +20 -20
- package/dist/{chunk-EP3XVEVC.cjs → chunk-I5JKUCGE.cjs} +50 -20
- package/dist/{chunk-R5VGYBV6.js → chunk-KHZKYUNR.js} +3 -3
- package/dist/{chunk-NYN7KFXL.js → chunk-LCM566I4.js} +1 -0
- package/dist/chunk-LF7P5PLR.js +14 -0
- package/dist/chunk-PZQDCRPV.cjs +16 -0
- package/dist/{chunk-IMBY5XUG.cjs → chunk-QGX47B5D.cjs} +7 -7
- package/dist/chunk-R3I7AJVL.js +507 -0
- package/dist/{chunk-BFOA2QQY.js → chunk-RG473F6Y.js} +2 -2
- package/dist/{chunk-WJY57THV.cjs → chunk-RHSWAXKB.cjs} +22 -22
- package/dist/{chunk-H5PTF3Y4.js → chunk-RSEO4XPX.js} +1 -1
- package/dist/{chunk-72LOJGUV.js → chunk-T7HAZC2E.js} +118 -12
- package/dist/chunk-TTPJHP42.cjs +522 -0
- package/dist/{chunk-NGHSYB5B.cjs → chunk-UCV4247U.cjs} +43 -43
- package/dist/{chunk-2BCWG3EZ.js → chunk-VTTN2FW3.js} +36 -7
- package/dist/{chunk-EWLR2PNT.js → chunk-WBRFIHSF.js} +4 -4
- package/dist/{chunk-55DOQLP6.js → chunk-WNVFNNWN.js} +2 -2
- package/dist/{chunk-E2LP4J6K.js → chunk-ZYAFP2AV.js} +8 -8
- package/dist/server/handlers/agents.cjs +11 -7
- package/dist/server/handlers/agents.d.cts +1 -0
- package/dist/server/handlers/agents.d.ts +1 -0
- package/dist/server/handlers/agents.js +1 -1
- package/dist/server/handlers/error.cjs +2 -2
- package/dist/server/handlers/error.js +1 -1
- package/dist/server/handlers/legacyWorkflows.cjs +11 -11
- package/dist/server/handlers/legacyWorkflows.js +1 -1
- package/dist/server/handlers/logs.cjs +4 -4
- package/dist/server/handlers/logs.js +1 -1
- package/dist/server/handlers/memory.cjs +29 -9
- package/dist/server/handlers/memory.d.cts +5 -0
- package/dist/server/handlers/memory.d.ts +5 -0
- package/dist/server/handlers/memory.js +1 -1
- package/dist/server/handlers/network.cjs +5 -5
- package/dist/server/handlers/network.js +1 -1
- package/dist/server/handlers/scores.cjs +30 -0
- package/dist/server/handlers/scores.d.cts +6 -0
- package/dist/server/handlers/scores.d.ts +6 -0
- package/dist/server/handlers/scores.js +1 -0
- package/dist/server/handlers/telemetry.cjs +3 -3
- package/dist/server/handlers/telemetry.js +1 -1
- package/dist/server/handlers/tools.cjs +5 -5
- package/dist/server/handlers/tools.js +1 -1
- package/dist/server/handlers/utils.cjs +2 -2
- package/dist/server/handlers/utils.js +1 -1
- package/dist/server/handlers/vNextNetwork.cjs +23 -23
- package/dist/server/handlers/vNextNetwork.js +4 -4
- package/dist/server/handlers/vector.cjs +7 -7
- package/dist/server/handlers/vector.js +1 -1
- package/dist/server/handlers/voice.cjs +5 -5
- package/dist/server/handlers/voice.js +1 -1
- package/dist/server/handlers/workflows.cjs +25 -13
- package/dist/server/handlers/workflows.d.cts +3 -0
- package/dist/server/handlers/workflows.d.ts +3 -0
- package/dist/server/handlers/workflows.js +1 -1
- package/dist/server/handlers.cjs +28 -23
- package/dist/server/handlers.d.cts +3 -2
- package/dist/server/handlers.d.ts +3 -2
- package/dist/server/handlers.js +11 -10
- package/package.json +8 -7
- package/dist/chunk-64U3UDTH.cjs +0 -13
- package/dist/chunk-M5ABIP7D.js +0 -11
- package/dist/chunk-RQ2Z56EC.cjs +0 -251
- package/dist/chunk-XUIJ7WRT.js +0 -241
|
@@ -0,0 +1,507 @@
|
|
|
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
|
+
getMemoryConfigHandler: () => getMemoryConfigHandler,
|
|
13
|
+
getMemoryStatusHandler: () => getMemoryStatusHandler,
|
|
14
|
+
getMessagesHandler: () => getMessagesHandler,
|
|
15
|
+
getMessagesPaginatedHandler: () => getMessagesPaginatedHandler,
|
|
16
|
+
getThreadByIdHandler: () => getThreadByIdHandler,
|
|
17
|
+
getThreadsHandler: () => getThreadsHandler,
|
|
18
|
+
getWorkingMemoryHandler: () => getWorkingMemoryHandler,
|
|
19
|
+
saveMessagesHandler: () => saveMessagesHandler,
|
|
20
|
+
searchMemoryHandler: () => searchMemoryHandler,
|
|
21
|
+
updateThreadHandler: () => updateThreadHandler,
|
|
22
|
+
updateWorkingMemoryHandler: () => updateWorkingMemoryHandler
|
|
23
|
+
});
|
|
24
|
+
async function getMemoryFromContext({
|
|
25
|
+
mastra,
|
|
26
|
+
agentId,
|
|
27
|
+
networkId,
|
|
28
|
+
runtimeContext
|
|
29
|
+
}) {
|
|
30
|
+
const agent = agentId ? mastra.getAgent(agentId) : null;
|
|
31
|
+
if (agentId && !agent) {
|
|
32
|
+
throw new HTTPException(404, { message: "Agent not found" });
|
|
33
|
+
}
|
|
34
|
+
const network = networkId ? mastra.vnext_getNetwork(networkId) : null;
|
|
35
|
+
if (networkId && !network) {
|
|
36
|
+
throw new HTTPException(404, { message: "Network not found" });
|
|
37
|
+
}
|
|
38
|
+
if (agent) {
|
|
39
|
+
return await agent?.getMemory() || mastra.getMemory();
|
|
40
|
+
}
|
|
41
|
+
if (network) {
|
|
42
|
+
return await network?.getMemory({ runtimeContext }) || mastra.getMemory();
|
|
43
|
+
}
|
|
44
|
+
return mastra.getMemory();
|
|
45
|
+
}
|
|
46
|
+
async function getMemoryStatusHandler({
|
|
47
|
+
mastra,
|
|
48
|
+
agentId,
|
|
49
|
+
networkId,
|
|
50
|
+
runtimeContext
|
|
51
|
+
}) {
|
|
52
|
+
try {
|
|
53
|
+
const memory = await getMemoryFromContext({ mastra, agentId, networkId, runtimeContext });
|
|
54
|
+
if (!memory) {
|
|
55
|
+
return { result: false };
|
|
56
|
+
}
|
|
57
|
+
return { result: true };
|
|
58
|
+
} catch (error) {
|
|
59
|
+
return handleError(error, "Error getting memory status");
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
async function getMemoryConfigHandler({
|
|
63
|
+
mastra,
|
|
64
|
+
agentId,
|
|
65
|
+
networkId,
|
|
66
|
+
runtimeContext
|
|
67
|
+
}) {
|
|
68
|
+
try {
|
|
69
|
+
const memory = await getMemoryFromContext({ mastra, agentId, networkId, runtimeContext });
|
|
70
|
+
if (!memory) {
|
|
71
|
+
throw new HTTPException(400, { message: "Memory is not initialized" });
|
|
72
|
+
}
|
|
73
|
+
const config = memory.getMergedThreadConfig({});
|
|
74
|
+
return { config };
|
|
75
|
+
} catch (error) {
|
|
76
|
+
return handleError(error, "Error getting memory configuration");
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
async function getThreadsHandler({
|
|
80
|
+
mastra,
|
|
81
|
+
agentId,
|
|
82
|
+
resourceId,
|
|
83
|
+
networkId,
|
|
84
|
+
runtimeContext
|
|
85
|
+
}) {
|
|
86
|
+
try {
|
|
87
|
+
const memory = await getMemoryFromContext({ mastra, agentId, networkId, runtimeContext });
|
|
88
|
+
if (!memory) {
|
|
89
|
+
throw new HTTPException(400, { message: "Memory is not initialized" });
|
|
90
|
+
}
|
|
91
|
+
validateBody({ resourceId });
|
|
92
|
+
const threads = await memory.getThreadsByResourceId({ resourceId });
|
|
93
|
+
return threads;
|
|
94
|
+
} catch (error) {
|
|
95
|
+
return handleError(error, "Error getting threads");
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
async function getThreadByIdHandler({
|
|
99
|
+
mastra,
|
|
100
|
+
agentId,
|
|
101
|
+
threadId,
|
|
102
|
+
networkId,
|
|
103
|
+
runtimeContext
|
|
104
|
+
}) {
|
|
105
|
+
try {
|
|
106
|
+
validateBody({ threadId });
|
|
107
|
+
const memory = await getMemoryFromContext({ mastra, agentId, networkId, runtimeContext });
|
|
108
|
+
if (!memory) {
|
|
109
|
+
throw new HTTPException(400, { message: "Memory is not initialized" });
|
|
110
|
+
}
|
|
111
|
+
const thread = await memory.getThreadById({ threadId });
|
|
112
|
+
if (!thread) {
|
|
113
|
+
throw new HTTPException(404, { message: "Thread not found" });
|
|
114
|
+
}
|
|
115
|
+
return thread;
|
|
116
|
+
} catch (error) {
|
|
117
|
+
return handleError(error, "Error getting thread");
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
async function saveMessagesHandler({
|
|
121
|
+
mastra,
|
|
122
|
+
agentId,
|
|
123
|
+
body,
|
|
124
|
+
networkId,
|
|
125
|
+
runtimeContext
|
|
126
|
+
}) {
|
|
127
|
+
try {
|
|
128
|
+
const memory = await getMemoryFromContext({ mastra, agentId, networkId, runtimeContext });
|
|
129
|
+
if (!memory) {
|
|
130
|
+
throw new HTTPException(400, { message: "Memory is not initialized" });
|
|
131
|
+
}
|
|
132
|
+
if (!body?.messages) {
|
|
133
|
+
throw new HTTPException(400, { message: "Messages are required" });
|
|
134
|
+
}
|
|
135
|
+
if (!Array.isArray(body.messages)) {
|
|
136
|
+
throw new HTTPException(400, { message: "Messages should be an array" });
|
|
137
|
+
}
|
|
138
|
+
const invalidMessages = body.messages.filter((message) => !message.threadId || !message.resourceId);
|
|
139
|
+
if (invalidMessages.length > 0) {
|
|
140
|
+
throw new HTTPException(400, {
|
|
141
|
+
message: `All messages must have threadId and resourceId fields. Found ${invalidMessages.length} invalid message(s).`
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
const processedMessages = body.messages.map((message) => ({
|
|
145
|
+
...message,
|
|
146
|
+
id: message.id || memory.generateId(),
|
|
147
|
+
createdAt: message.createdAt ? new Date(message.createdAt) : /* @__PURE__ */ new Date()
|
|
148
|
+
}));
|
|
149
|
+
const result = await memory.saveMessages({ messages: processedMessages, memoryConfig: {} });
|
|
150
|
+
return result;
|
|
151
|
+
} catch (error) {
|
|
152
|
+
return handleError(error, "Error saving messages");
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
async function createThreadHandler({
|
|
156
|
+
mastra,
|
|
157
|
+
agentId,
|
|
158
|
+
body,
|
|
159
|
+
networkId,
|
|
160
|
+
runtimeContext
|
|
161
|
+
}) {
|
|
162
|
+
try {
|
|
163
|
+
const memory = await getMemoryFromContext({ mastra, agentId, networkId, runtimeContext });
|
|
164
|
+
if (!memory) {
|
|
165
|
+
throw new HTTPException(400, { message: "Memory is not initialized" });
|
|
166
|
+
}
|
|
167
|
+
validateBody({ resourceId: body?.resourceId });
|
|
168
|
+
const result = await memory.createThread({
|
|
169
|
+
resourceId: body?.resourceId,
|
|
170
|
+
title: body?.title,
|
|
171
|
+
metadata: body?.metadata,
|
|
172
|
+
threadId: body?.threadId
|
|
173
|
+
});
|
|
174
|
+
return result;
|
|
175
|
+
} catch (error) {
|
|
176
|
+
return handleError(error, "Error saving thread to memory");
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
async function updateThreadHandler({
|
|
180
|
+
mastra,
|
|
181
|
+
agentId,
|
|
182
|
+
threadId,
|
|
183
|
+
body,
|
|
184
|
+
networkId,
|
|
185
|
+
runtimeContext
|
|
186
|
+
}) {
|
|
187
|
+
try {
|
|
188
|
+
const memory = await getMemoryFromContext({ mastra, agentId, networkId, runtimeContext });
|
|
189
|
+
if (!body) {
|
|
190
|
+
throw new HTTPException(400, { message: "Body is required" });
|
|
191
|
+
}
|
|
192
|
+
const { title, metadata, resourceId } = body;
|
|
193
|
+
const updatedAt = /* @__PURE__ */ new Date();
|
|
194
|
+
validateBody({ threadId });
|
|
195
|
+
if (!memory) {
|
|
196
|
+
throw new HTTPException(400, { message: "Memory is not initialized" });
|
|
197
|
+
}
|
|
198
|
+
const thread = await memory.getThreadById({ threadId });
|
|
199
|
+
if (!thread) {
|
|
200
|
+
throw new HTTPException(404, { message: "Thread not found" });
|
|
201
|
+
}
|
|
202
|
+
const updatedThread = {
|
|
203
|
+
...thread,
|
|
204
|
+
title: title || thread.title,
|
|
205
|
+
metadata: metadata || thread.metadata,
|
|
206
|
+
resourceId: resourceId || thread.resourceId,
|
|
207
|
+
createdAt: thread.createdAt,
|
|
208
|
+
updatedAt
|
|
209
|
+
};
|
|
210
|
+
const result = await memory.saveThread({ thread: updatedThread });
|
|
211
|
+
return result;
|
|
212
|
+
} catch (error) {
|
|
213
|
+
return handleError(error, "Error updating thread");
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
async function deleteThreadHandler({
|
|
217
|
+
mastra,
|
|
218
|
+
agentId,
|
|
219
|
+
threadId,
|
|
220
|
+
networkId,
|
|
221
|
+
runtimeContext
|
|
222
|
+
}) {
|
|
223
|
+
try {
|
|
224
|
+
validateBody({ threadId });
|
|
225
|
+
const memory = await getMemoryFromContext({ mastra, agentId, networkId, runtimeContext });
|
|
226
|
+
if (!memory) {
|
|
227
|
+
throw new HTTPException(400, { message: "Memory is not initialized" });
|
|
228
|
+
}
|
|
229
|
+
const thread = await memory.getThreadById({ threadId });
|
|
230
|
+
if (!thread) {
|
|
231
|
+
throw new HTTPException(404, { message: "Thread not found" });
|
|
232
|
+
}
|
|
233
|
+
await memory.deleteThread(threadId);
|
|
234
|
+
return { result: "Thread deleted" };
|
|
235
|
+
} catch (error) {
|
|
236
|
+
return handleError(error, "Error deleting thread");
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
async function getMessagesPaginatedHandler({
|
|
240
|
+
mastra,
|
|
241
|
+
threadId,
|
|
242
|
+
resourceId,
|
|
243
|
+
selectBy,
|
|
244
|
+
format
|
|
245
|
+
}) {
|
|
246
|
+
try {
|
|
247
|
+
validateBody({ threadId });
|
|
248
|
+
const storage = mastra.getStorage();
|
|
249
|
+
if (!storage) {
|
|
250
|
+
throw new HTTPException(400, { message: "Storage is not initialized" });
|
|
251
|
+
}
|
|
252
|
+
const thread = await storage.getThreadById({ threadId });
|
|
253
|
+
if (!thread) {
|
|
254
|
+
throw new HTTPException(404, { message: "Thread not found" });
|
|
255
|
+
}
|
|
256
|
+
const result = await storage.getMessagesPaginated({ threadId, resourceId, selectBy, format });
|
|
257
|
+
return result;
|
|
258
|
+
} catch (error) {
|
|
259
|
+
return handleError(error, "Error getting messages");
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
async function getMessagesHandler({
|
|
263
|
+
mastra,
|
|
264
|
+
agentId,
|
|
265
|
+
threadId,
|
|
266
|
+
limit,
|
|
267
|
+
networkId,
|
|
268
|
+
runtimeContext
|
|
269
|
+
}) {
|
|
270
|
+
if (limit !== void 0 && (!Number.isInteger(limit) || limit <= 0)) {
|
|
271
|
+
throw new HTTPException(400, { message: "Invalid limit: must be a positive integer" });
|
|
272
|
+
}
|
|
273
|
+
try {
|
|
274
|
+
validateBody({ threadId });
|
|
275
|
+
const memory = await getMemoryFromContext({ mastra, agentId, networkId, runtimeContext });
|
|
276
|
+
if (!memory) {
|
|
277
|
+
throw new HTTPException(400, { message: "Memory is not initialized" });
|
|
278
|
+
}
|
|
279
|
+
const thread = await memory.getThreadById({ threadId });
|
|
280
|
+
if (!thread) {
|
|
281
|
+
throw new HTTPException(404, { message: "Thread not found" });
|
|
282
|
+
}
|
|
283
|
+
const result = await memory.query({
|
|
284
|
+
threadId,
|
|
285
|
+
...limit && { selectBy: { last: limit } }
|
|
286
|
+
});
|
|
287
|
+
return { messages: result.messages, uiMessages: result.uiMessages };
|
|
288
|
+
} catch (error) {
|
|
289
|
+
return handleError(error, "Error getting messages");
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
async function getWorkingMemoryHandler({
|
|
293
|
+
mastra,
|
|
294
|
+
agentId,
|
|
295
|
+
threadId,
|
|
296
|
+
resourceId,
|
|
297
|
+
networkId,
|
|
298
|
+
runtimeContext,
|
|
299
|
+
memoryConfig
|
|
300
|
+
}) {
|
|
301
|
+
try {
|
|
302
|
+
const memory = await getMemoryFromContext({ mastra, agentId, networkId, runtimeContext });
|
|
303
|
+
validateBody({ threadId });
|
|
304
|
+
if (!memory) {
|
|
305
|
+
throw new HTTPException(400, { message: "Memory is not initialized" });
|
|
306
|
+
}
|
|
307
|
+
const thread = await memory.getThreadById({ threadId });
|
|
308
|
+
const threadExists = !!thread;
|
|
309
|
+
const template = await memory.getWorkingMemoryTemplate({ memoryConfig });
|
|
310
|
+
const workingMemoryTemplate = template?.format === "json" ? { ...template, content: JSON.stringify(generateEmptyFromSchema(template.content)) } : template;
|
|
311
|
+
const workingMemory = await memory.getWorkingMemory({ threadId, resourceId, memoryConfig });
|
|
312
|
+
const config = memory.getMergedThreadConfig(memoryConfig || {});
|
|
313
|
+
const source = config.workingMemory?.scope === "resource" && resourceId ? "resource" : "thread";
|
|
314
|
+
return { workingMemory, source, workingMemoryTemplate, threadExists };
|
|
315
|
+
} catch (error) {
|
|
316
|
+
return handleError(error, "Error getting working memory");
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
async function updateWorkingMemoryHandler({
|
|
320
|
+
mastra,
|
|
321
|
+
agentId,
|
|
322
|
+
threadId,
|
|
323
|
+
body,
|
|
324
|
+
networkId,
|
|
325
|
+
runtimeContext
|
|
326
|
+
}) {
|
|
327
|
+
try {
|
|
328
|
+
validateBody({ threadId });
|
|
329
|
+
const memory = await getMemoryFromContext({ mastra, agentId, networkId, runtimeContext });
|
|
330
|
+
const { resourceId, memoryConfig, workingMemory } = body;
|
|
331
|
+
if (!memory) {
|
|
332
|
+
throw new HTTPException(400, { message: "Memory is not initialized" });
|
|
333
|
+
}
|
|
334
|
+
const thread = await memory.getThreadById({ threadId });
|
|
335
|
+
if (!thread) {
|
|
336
|
+
throw new HTTPException(404, { message: "Thread not found" });
|
|
337
|
+
}
|
|
338
|
+
await memory.updateWorkingMemory({ threadId, resourceId, workingMemory, memoryConfig });
|
|
339
|
+
return { success: true };
|
|
340
|
+
} catch (error) {
|
|
341
|
+
return handleError(error, "Error updating working memory");
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
async function searchMemoryHandler({
|
|
345
|
+
mastra,
|
|
346
|
+
agentId,
|
|
347
|
+
searchQuery,
|
|
348
|
+
resourceId,
|
|
349
|
+
threadId,
|
|
350
|
+
limit = 20,
|
|
351
|
+
networkId,
|
|
352
|
+
runtimeContext,
|
|
353
|
+
memoryConfig
|
|
354
|
+
}) {
|
|
355
|
+
try {
|
|
356
|
+
validateBody({ searchQuery, resourceId });
|
|
357
|
+
const memory = await getMemoryFromContext({ mastra, agentId, networkId, runtimeContext });
|
|
358
|
+
if (!memory) {
|
|
359
|
+
throw new HTTPException(400, { message: "Memory is not initialized" });
|
|
360
|
+
}
|
|
361
|
+
const config = memory.getMergedThreadConfig(memoryConfig || {});
|
|
362
|
+
const hasSemanticRecall = !!config?.semanticRecall;
|
|
363
|
+
const resourceScope = typeof config?.semanticRecall === "object" && config?.semanticRecall?.scope === "resource";
|
|
364
|
+
if (threadId && !resourceScope) {
|
|
365
|
+
const thread = await memory.getThreadById({ threadId });
|
|
366
|
+
if (!thread) {
|
|
367
|
+
throw new HTTPException(404, { message: "Thread not found" });
|
|
368
|
+
}
|
|
369
|
+
if (thread.resourceId !== resourceId) {
|
|
370
|
+
throw new HTTPException(403, { message: "Thread does not belong to the specified resource" });
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
const searchResults = [];
|
|
374
|
+
const messageMap = /* @__PURE__ */ new Map();
|
|
375
|
+
if (threadId && !resourceScope) {
|
|
376
|
+
const thread = await memory.getThreadById({ threadId });
|
|
377
|
+
if (!thread) {
|
|
378
|
+
return {
|
|
379
|
+
results: [],
|
|
380
|
+
count: 0,
|
|
381
|
+
query: searchQuery,
|
|
382
|
+
searchScope: "thread",
|
|
383
|
+
searchType: hasSemanticRecall ? "semantic" : "text"
|
|
384
|
+
};
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
if (!threadId || resourceScope) {
|
|
388
|
+
const threads = await memory.getThreadsByResourceId({ resourceId });
|
|
389
|
+
if (threads.length === 0) {
|
|
390
|
+
return {
|
|
391
|
+
results: [],
|
|
392
|
+
count: 0,
|
|
393
|
+
query: searchQuery,
|
|
394
|
+
searchScope: "resource",
|
|
395
|
+
searchType: hasSemanticRecall ? "semantic" : "text"
|
|
396
|
+
};
|
|
397
|
+
}
|
|
398
|
+
for (const thread of threads) {
|
|
399
|
+
const result = await memory.rememberMessages({
|
|
400
|
+
threadId: thread.id,
|
|
401
|
+
resourceId,
|
|
402
|
+
vectorMessageSearch: searchQuery,
|
|
403
|
+
config
|
|
404
|
+
});
|
|
405
|
+
const threadMessages = (await memory.query({ threadId: thread.id })).uiMessages;
|
|
406
|
+
result.messagesV2.forEach((msg) => {
|
|
407
|
+
if (messageMap.has(msg.id)) return;
|
|
408
|
+
messageMap.set(msg.id, true);
|
|
409
|
+
const content = msg.content.content || msg.content.parts?.map((p) => p.type === "text" ? p.text : "").join(" ") || "";
|
|
410
|
+
if (!hasSemanticRecall && !content.toLowerCase().includes(searchQuery.toLowerCase())) {
|
|
411
|
+
return;
|
|
412
|
+
}
|
|
413
|
+
const messageIndex = threadMessages.findIndex((m) => m.id === msg.id);
|
|
414
|
+
const searchResult = {
|
|
415
|
+
id: msg.id,
|
|
416
|
+
role: msg.role,
|
|
417
|
+
content,
|
|
418
|
+
createdAt: msg.createdAt,
|
|
419
|
+
threadId: msg.threadId || thread.id,
|
|
420
|
+
threadTitle: thread.title || msg.threadId || thread.id
|
|
421
|
+
};
|
|
422
|
+
if (messageIndex !== -1) {
|
|
423
|
+
searchResult.context = {
|
|
424
|
+
before: threadMessages.slice(Math.max(0, messageIndex - 2), messageIndex).map((m) => ({
|
|
425
|
+
id: m.id,
|
|
426
|
+
role: m.role,
|
|
427
|
+
content: m.content,
|
|
428
|
+
createdAt: m.createdAt || /* @__PURE__ */ new Date()
|
|
429
|
+
})),
|
|
430
|
+
after: threadMessages.slice(messageIndex + 1, messageIndex + 3).map((m) => ({
|
|
431
|
+
id: m.id,
|
|
432
|
+
role: m.role,
|
|
433
|
+
content: m.content,
|
|
434
|
+
createdAt: m.createdAt || /* @__PURE__ */ new Date()
|
|
435
|
+
}))
|
|
436
|
+
};
|
|
437
|
+
}
|
|
438
|
+
searchResults.push(searchResult);
|
|
439
|
+
});
|
|
440
|
+
}
|
|
441
|
+
} else if (threadId) {
|
|
442
|
+
const thread = await memory.getThreadById({ threadId });
|
|
443
|
+
if (!thread) {
|
|
444
|
+
return {
|
|
445
|
+
results: [],
|
|
446
|
+
count: 0,
|
|
447
|
+
query: searchQuery,
|
|
448
|
+
searchScope: "thread",
|
|
449
|
+
searchType: hasSemanticRecall ? "semantic" : "text"
|
|
450
|
+
};
|
|
451
|
+
}
|
|
452
|
+
const result = await memory.rememberMessages({
|
|
453
|
+
threadId,
|
|
454
|
+
resourceId,
|
|
455
|
+
vectorMessageSearch: searchQuery,
|
|
456
|
+
config
|
|
457
|
+
});
|
|
458
|
+
const threadMessages = (await memory.query({ threadId })).uiMessages;
|
|
459
|
+
result.messagesV2.forEach((msg) => {
|
|
460
|
+
if (messageMap.has(msg.id)) return;
|
|
461
|
+
messageMap.set(msg.id, true);
|
|
462
|
+
const content = msg.content.content || msg.content.parts?.map((p) => p.type === "text" ? p.text : "").join(" ") || "";
|
|
463
|
+
if (!hasSemanticRecall && !content.toLowerCase().includes(searchQuery.toLowerCase())) {
|
|
464
|
+
return;
|
|
465
|
+
}
|
|
466
|
+
const messageIndex = threadMessages.findIndex((m) => m.id === msg.id);
|
|
467
|
+
const searchResult = {
|
|
468
|
+
id: msg.id,
|
|
469
|
+
role: msg.role,
|
|
470
|
+
content,
|
|
471
|
+
createdAt: msg.createdAt,
|
|
472
|
+
threadId,
|
|
473
|
+
threadTitle: thread?.title || threadId
|
|
474
|
+
};
|
|
475
|
+
if (messageIndex !== -1) {
|
|
476
|
+
searchResult.context = {
|
|
477
|
+
before: threadMessages.slice(Math.max(0, messageIndex - 2), messageIndex).map((m) => ({
|
|
478
|
+
id: m.id,
|
|
479
|
+
role: m.role,
|
|
480
|
+
content: m.content,
|
|
481
|
+
createdAt: m.createdAt || /* @__PURE__ */ new Date()
|
|
482
|
+
})),
|
|
483
|
+
after: threadMessages.slice(messageIndex + 1, messageIndex + 3).map((m) => ({
|
|
484
|
+
id: m.id,
|
|
485
|
+
role: m.role,
|
|
486
|
+
content: m.content,
|
|
487
|
+
createdAt: m.createdAt || /* @__PURE__ */ new Date()
|
|
488
|
+
}))
|
|
489
|
+
};
|
|
490
|
+
}
|
|
491
|
+
searchResults.push(searchResult);
|
|
492
|
+
});
|
|
493
|
+
}
|
|
494
|
+
const sortedResults = searchResults.sort((a, b) => new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime()).slice(0, limit);
|
|
495
|
+
return {
|
|
496
|
+
results: sortedResults,
|
|
497
|
+
count: sortedResults.length,
|
|
498
|
+
query: searchQuery,
|
|
499
|
+
searchScope: resourceScope ? "resource" : "thread",
|
|
500
|
+
searchType: hasSemanticRecall ? "semantic" : "text"
|
|
501
|
+
};
|
|
502
|
+
} catch (error) {
|
|
503
|
+
return handleError(error, "Error searching memory");
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
export { createThreadHandler, deleteThreadHandler, getMemoryConfigHandler, getMemoryStatusHandler, getMessagesHandler, getMessagesPaginatedHandler, getThreadByIdHandler, getThreadsHandler, getWorkingMemoryHandler, memory_exports, saveMessagesHandler, searchMemoryHandler, updateThreadHandler, updateWorkingMemoryHandler };
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { handleError } from './chunk-
|
|
2
|
-
import { HTTPException } from './chunk-
|
|
1
|
+
import { handleError } from './chunk-LF7P5PLR.js';
|
|
2
|
+
import { HTTPException } from './chunk-LCM566I4.js';
|
|
3
3
|
import { __export } from './chunk-MLKGABMK.js';
|
|
4
4
|
|
|
5
5
|
// src/server/handlers/telemetry.ts
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var
|
|
4
|
-
var
|
|
5
|
-
var
|
|
3
|
+
var chunkCCGRCYWJ_cjs = require('./chunk-CCGRCYWJ.cjs');
|
|
4
|
+
var chunkPZQDCRPV_cjs = require('./chunk-PZQDCRPV.cjs');
|
|
5
|
+
var chunk2KZFMI6P_cjs = require('./chunk-2KZFMI6P.cjs');
|
|
6
6
|
var chunk75ZPJI57_cjs = require('./chunk-75ZPJI57.cjs');
|
|
7
7
|
var stream = require('stream');
|
|
8
8
|
|
|
@@ -17,20 +17,20 @@ chunk75ZPJI57_cjs.__export(voice_exports, {
|
|
|
17
17
|
async function getSpeakersHandler({ mastra, agentId }) {
|
|
18
18
|
try {
|
|
19
19
|
if (!agentId) {
|
|
20
|
-
throw new
|
|
20
|
+
throw new chunk2KZFMI6P_cjs.HTTPException(400, { message: "Agent ID is required" });
|
|
21
21
|
}
|
|
22
22
|
const agent = mastra.getAgent(agentId);
|
|
23
23
|
if (!agent) {
|
|
24
|
-
throw new
|
|
24
|
+
throw new chunk2KZFMI6P_cjs.HTTPException(404, { message: "Agent not found" });
|
|
25
25
|
}
|
|
26
26
|
const voice = await agent.getVoice();
|
|
27
27
|
if (!voice) {
|
|
28
|
-
throw new
|
|
28
|
+
throw new chunk2KZFMI6P_cjs.HTTPException(400, { message: "Agent does not have voice capabilities" });
|
|
29
29
|
}
|
|
30
30
|
const speakers = await voice.getSpeakers();
|
|
31
31
|
return speakers;
|
|
32
32
|
} catch (error) {
|
|
33
|
-
return
|
|
33
|
+
return chunkPZQDCRPV_cjs.handleError(error, "Error getting speakers");
|
|
34
34
|
}
|
|
35
35
|
}
|
|
36
36
|
async function generateSpeechHandler({
|
|
@@ -40,26 +40,26 @@ async function generateSpeechHandler({
|
|
|
40
40
|
}) {
|
|
41
41
|
try {
|
|
42
42
|
if (!agentId) {
|
|
43
|
-
throw new
|
|
43
|
+
throw new chunk2KZFMI6P_cjs.HTTPException(400, { message: "Agent ID is required" });
|
|
44
44
|
}
|
|
45
|
-
|
|
45
|
+
chunkCCGRCYWJ_cjs.validateBody({
|
|
46
46
|
text: body?.text
|
|
47
47
|
});
|
|
48
48
|
const agent = mastra.getAgent(agentId);
|
|
49
49
|
if (!agent) {
|
|
50
|
-
throw new
|
|
50
|
+
throw new chunk2KZFMI6P_cjs.HTTPException(404, { message: "Agent not found" });
|
|
51
51
|
}
|
|
52
52
|
const voice = await agent.getVoice();
|
|
53
53
|
if (!voice) {
|
|
54
|
-
throw new
|
|
54
|
+
throw new chunk2KZFMI6P_cjs.HTTPException(400, { message: "Agent does not have voice capabilities" });
|
|
55
55
|
}
|
|
56
56
|
const audioStream = await voice.speak(body.text, { speaker: body.speakerId });
|
|
57
57
|
if (!audioStream) {
|
|
58
|
-
throw new
|
|
58
|
+
throw new chunk2KZFMI6P_cjs.HTTPException(500, { message: "Failed to generate speech" });
|
|
59
59
|
}
|
|
60
60
|
return audioStream;
|
|
61
61
|
} catch (error) {
|
|
62
|
-
return
|
|
62
|
+
return chunkPZQDCRPV_cjs.handleError(error, "Error generating speech");
|
|
63
63
|
}
|
|
64
64
|
}
|
|
65
65
|
async function transcribeSpeechHandler({
|
|
@@ -69,18 +69,18 @@ async function transcribeSpeechHandler({
|
|
|
69
69
|
}) {
|
|
70
70
|
try {
|
|
71
71
|
if (!agentId) {
|
|
72
|
-
throw new
|
|
72
|
+
throw new chunk2KZFMI6P_cjs.HTTPException(400, { message: "Agent ID is required" });
|
|
73
73
|
}
|
|
74
74
|
if (!body?.audioData) {
|
|
75
|
-
throw new
|
|
75
|
+
throw new chunk2KZFMI6P_cjs.HTTPException(400, { message: "Audio data is required" });
|
|
76
76
|
}
|
|
77
77
|
const agent = mastra.getAgent(agentId);
|
|
78
78
|
if (!agent) {
|
|
79
|
-
throw new
|
|
79
|
+
throw new chunk2KZFMI6P_cjs.HTTPException(404, { message: "Agent not found" });
|
|
80
80
|
}
|
|
81
81
|
const voice = await agent.getVoice();
|
|
82
82
|
if (!voice) {
|
|
83
|
-
throw new
|
|
83
|
+
throw new chunk2KZFMI6P_cjs.HTTPException(400, { message: "Agent does not have voice capabilities" });
|
|
84
84
|
}
|
|
85
85
|
const audioStream = new stream.Readable();
|
|
86
86
|
audioStream.push(body.audioData);
|
|
@@ -88,26 +88,26 @@ async function transcribeSpeechHandler({
|
|
|
88
88
|
const text = await voice.listen(audioStream, body.options);
|
|
89
89
|
return { text };
|
|
90
90
|
} catch (error) {
|
|
91
|
-
return
|
|
91
|
+
return chunkPZQDCRPV_cjs.handleError(error, "Error transcribing speech");
|
|
92
92
|
}
|
|
93
93
|
}
|
|
94
94
|
async function getListenerHandler({ mastra, agentId }) {
|
|
95
95
|
try {
|
|
96
96
|
if (!agentId) {
|
|
97
|
-
throw new
|
|
97
|
+
throw new chunk2KZFMI6P_cjs.HTTPException(400, { message: "Agent ID is required" });
|
|
98
98
|
}
|
|
99
99
|
const agent = mastra.getAgent(agentId);
|
|
100
100
|
if (!agent) {
|
|
101
|
-
throw new
|
|
101
|
+
throw new chunk2KZFMI6P_cjs.HTTPException(404, { message: "Agent not found" });
|
|
102
102
|
}
|
|
103
103
|
const voice = await agent.getVoice();
|
|
104
104
|
if (!voice) {
|
|
105
|
-
throw new
|
|
105
|
+
throw new chunk2KZFMI6P_cjs.HTTPException(400, { message: "Agent does not have voice capabilities" });
|
|
106
106
|
}
|
|
107
107
|
const listeners = await voice.getListener();
|
|
108
108
|
return listeners;
|
|
109
109
|
} catch (error) {
|
|
110
|
-
return
|
|
110
|
+
return chunkPZQDCRPV_cjs.handleError(error, "Error getting listeners");
|
|
111
111
|
}
|
|
112
112
|
}
|
|
113
113
|
|